diff --git a/assets/javascripts/bundle.79ae519e.min.js b/assets/javascripts/bundle.79ae519e.min.js index 5545b62a8..0172d1517 100644 --- a/assets/javascripts/bundle.79ae519e.min.js +++ b/assets/javascripts/bundle.79ae519e.min.js @@ -14,4 +14,4 @@ `):"",this.name="UnsubscriptionError",this.errors=r}});function Ze(e,t){if(e){var r=e.indexOf(t);0<=r&&e.splice(r,1)}}var qe=(function(){function e(t){this.initialTeardown=t,this.closed=!1,this._parentage=null,this._finalizers=null}return e.prototype.unsubscribe=function(){var t,r,o,n,i;if(!this.closed){this.closed=!0;var s=this._parentage;if(s)if(this._parentage=null,Array.isArray(s))try{for(var a=Oe(s),c=a.next();!c.done;c=a.next()){var p=c.value;p.remove(this)}}catch(S){t={error:S}}finally{try{c&&!c.done&&(r=a.return)&&r.call(a)}finally{if(t)throw t.error}}else s.remove(this);var l=this.initialTeardown;if(I(l))try{l()}catch(S){i=S instanceof Jt?S.errors:[S]}var f=this._finalizers;if(f){this._finalizers=null;try{for(var u=Oe(f),d=u.next();!d.done;d=u.next()){var v=d.value;try{So(v)}catch(S){i=i!=null?i:[],S instanceof Jt?i=B(B([],K(i)),K(S.errors)):i.push(S)}}}catch(S){o={error:S}}finally{try{d&&!d.done&&(n=u.return)&&n.call(u)}finally{if(o)throw o.error}}}if(i)throw new Jt(i)}},e.prototype.add=function(t){var r;if(t&&t!==this)if(this.closed)So(t);else{if(t instanceof e){if(t.closed||t._hasParent(this))return;t._addParent(this)}(this._finalizers=(r=this._finalizers)!==null&&r!==void 0?r:[]).push(t)}},e.prototype._hasParent=function(t){var r=this._parentage;return r===t||Array.isArray(r)&&r.includes(t)},e.prototype._addParent=function(t){var r=this._parentage;this._parentage=Array.isArray(r)?(r.push(t),r):r?[r,t]:t},e.prototype._removeParent=function(t){var r=this._parentage;r===t?this._parentage=null:Array.isArray(r)&&Ze(r,t)},e.prototype.remove=function(t){var r=this._finalizers;r&&Ze(r,t),t instanceof e&&t._removeParent(this)},e.EMPTY=(function(){var t=new e;return t.closed=!0,t})(),e})();var $r=qe.EMPTY;function Xt(e){return e instanceof qe||e&&"closed"in e&&I(e.remove)&&I(e.add)&&I(e.unsubscribe)}function So(e){I(e)?e():e.unsubscribe()}var De={onUnhandledError:null,onStoppedNotification:null,Promise:void 0,useDeprecatedSynchronousErrorHandling:!1,useDeprecatedNextContext:!1};var xt={setTimeout:function(e,t){for(var r=[],o=2;o0},enumerable:!1,configurable:!0}),t.prototype._trySubscribe=function(r){return this._throwIfClosed(),e.prototype._trySubscribe.call(this,r)},t.prototype._subscribe=function(r){return this._throwIfClosed(),this._checkFinalizedStatuses(r),this._innerSubscribe(r)},t.prototype._innerSubscribe=function(r){var o=this,n=this,i=n.hasError,s=n.isStopped,a=n.observers;return i||s?$r:(this.currentObservers=null,a.push(r),new qe(function(){o.currentObservers=null,Ze(a,r)}))},t.prototype._checkFinalizedStatuses=function(r){var o=this,n=o.hasError,i=o.thrownError,s=o.isStopped;n?r.error(i):s&&r.complete()},t.prototype.asObservable=function(){var r=new F;return r.source=this,r},t.create=function(r,o){return new Ho(r,o)},t})(F);var Ho=(function(e){ie(t,e);function t(r,o){var n=e.call(this)||this;return n.destination=r,n.source=o,n}return t.prototype.next=function(r){var o,n;(n=(o=this.destination)===null||o===void 0?void 0:o.next)===null||n===void 0||n.call(o,r)},t.prototype.error=function(r){var o,n;(n=(o=this.destination)===null||o===void 0?void 0:o.error)===null||n===void 0||n.call(o,r)},t.prototype.complete=function(){var r,o;(o=(r=this.destination)===null||r===void 0?void 0:r.complete)===null||o===void 0||o.call(r)},t.prototype._subscribe=function(r){var o,n;return(n=(o=this.source)===null||o===void 0?void 0:o.subscribe(r))!==null&&n!==void 0?n:$r},t})(T);var jr=(function(e){ie(t,e);function t(r){var o=e.call(this)||this;return o._value=r,o}return Object.defineProperty(t.prototype,"value",{get:function(){return this.getValue()},enumerable:!1,configurable:!0}),t.prototype._subscribe=function(r){var o=e.prototype._subscribe.call(this,r);return!o.closed&&r.next(this._value),o},t.prototype.getValue=function(){var r=this,o=r.hasError,n=r.thrownError,i=r._value;if(o)throw n;return this._throwIfClosed(),i},t.prototype.next=function(r){e.prototype.next.call(this,this._value=r)},t})(T);var Rt={now:function(){return(Rt.delegate||Date).now()},delegate:void 0};var It=(function(e){ie(t,e);function t(r,o,n){r===void 0&&(r=1/0),o===void 0&&(o=1/0),n===void 0&&(n=Rt);var i=e.call(this)||this;return i._bufferSize=r,i._windowTime=o,i._timestampProvider=n,i._buffer=[],i._infiniteTimeWindow=!0,i._infiniteTimeWindow=o===1/0,i._bufferSize=Math.max(1,r),i._windowTime=Math.max(1,o),i}return t.prototype.next=function(r){var o=this,n=o.isStopped,i=o._buffer,s=o._infiniteTimeWindow,a=o._timestampProvider,c=o._windowTime;n||(i.push(r),!s&&i.push(a.now()+c)),this._trimBuffer(),e.prototype.next.call(this,r)},t.prototype._subscribe=function(r){this._throwIfClosed(),this._trimBuffer();for(var o=this._innerSubscribe(r),n=this,i=n._infiniteTimeWindow,s=n._buffer,a=s.slice(),c=0;c0?e.prototype.schedule.call(this,r,o):(this.delay=o,this.state=r,this.scheduler.flush(this),this)},t.prototype.execute=function(r,o){return o>0||this.closed?e.prototype.execute.call(this,r,o):this._execute(r,o)},t.prototype.requestAsyncId=function(r,o,n){return n===void 0&&(n=0),n!=null&&n>0||n==null&&this.delay>0?e.prototype.requestAsyncId.call(this,r,o,n):(r.flush(this),0)},t})(St);var Ro=(function(e){ie(t,e);function t(){return e!==null&&e.apply(this,arguments)||this}return t})(Ot);var Dr=new Ro(Po);var Io=(function(e){ie(t,e);function t(r,o){var n=e.call(this,r,o)||this;return n.scheduler=r,n.work=o,n}return t.prototype.requestAsyncId=function(r,o,n){return n===void 0&&(n=0),n!==null&&n>0?e.prototype.requestAsyncId.call(this,r,o,n):(r.actions.push(this),r._scheduled||(r._scheduled=Tt.requestAnimationFrame(function(){return r.flush(void 0)})))},t.prototype.recycleAsyncId=function(r,o,n){var i;if(n===void 0&&(n=0),n!=null?n>0:this.delay>0)return e.prototype.recycleAsyncId.call(this,r,o,n);var s=r.actions;o!=null&&o===r._scheduled&&((i=s[s.length-1])===null||i===void 0?void 0:i.id)!==o&&(Tt.cancelAnimationFrame(o),r._scheduled=void 0)},t})(St);var Fo=(function(e){ie(t,e);function t(){return e!==null&&e.apply(this,arguments)||this}return t.prototype.flush=function(r){this._active=!0;var o;r?o=r.id:(o=this._scheduled,this._scheduled=void 0);var n=this.actions,i;r=r||n.shift();do if(i=r.execute(r.state,r.delay))break;while((r=n[0])&&r.id===o&&n.shift());if(this._active=!1,i){for(;(r=n[0])&&r.id===o&&n.shift();)r.unsubscribe();throw i}},t})(Ot);var ye=new Fo(Io);var y=new F(function(e){return e.complete()});function tr(e){return e&&I(e.schedule)}function Vr(e){return e[e.length-1]}function pt(e){return I(Vr(e))?e.pop():void 0}function Fe(e){return tr(Vr(e))?e.pop():void 0}function rr(e,t){return typeof Vr(e)=="number"?e.pop():t}var Lt=(function(e){return e&&typeof e.length=="number"&&typeof e!="function"});function or(e){return I(e==null?void 0:e.then)}function nr(e){return I(e[wt])}function ir(e){return Symbol.asyncIterator&&I(e==null?void 0:e[Symbol.asyncIterator])}function ar(e){return new TypeError("You provided "+(e!==null&&typeof e=="object"?"an invalid object":"'"+e+"'")+" where a stream was expected. You can provide an Observable, Promise, ReadableStream, Array, AsyncIterable, or Iterable.")}function fa(){return typeof Symbol!="function"||!Symbol.iterator?"@@iterator":Symbol.iterator}var sr=fa();function cr(e){return I(e==null?void 0:e[sr])}function pr(e){return wo(this,arguments,function(){var r,o,n,i;return Gt(this,function(s){switch(s.label){case 0:r=e.getReader(),s.label=1;case 1:s.trys.push([1,,9,10]),s.label=2;case 2:return[4,dt(r.read())];case 3:return o=s.sent(),n=o.value,i=o.done,i?[4,dt(void 0)]:[3,5];case 4:return[2,s.sent()];case 5:return[4,dt(n)];case 6:return[4,s.sent()];case 7:return s.sent(),[3,2];case 8:return[3,10];case 9:return r.releaseLock(),[7];case 10:return[2]}})})}function lr(e){return I(e==null?void 0:e.getReader)}function U(e){if(e instanceof F)return e;if(e!=null){if(nr(e))return ua(e);if(Lt(e))return da(e);if(or(e))return ha(e);if(ir(e))return jo(e);if(cr(e))return ba(e);if(lr(e))return va(e)}throw ar(e)}function ua(e){return new F(function(t){var r=e[wt]();if(I(r.subscribe))return r.subscribe(t);throw new TypeError("Provided object does not correctly implement Symbol.observable")})}function da(e){return new F(function(t){for(var r=0;r=2;return function(o){return o.pipe(e?g(function(n,i){return e(n,i,o)}):be,Ee(1),r?Qe(t):tn(function(){return new fr}))}}function Yr(e){return e<=0?function(){return y}:E(function(t,r){var o=[];t.subscribe(w(r,function(n){o.push(n),e=2,!0))}function le(e){e===void 0&&(e={});var t=e.connector,r=t===void 0?function(){return new T}:t,o=e.resetOnError,n=o===void 0?!0:o,i=e.resetOnComplete,s=i===void 0?!0:i,a=e.resetOnRefCountZero,c=a===void 0?!0:a;return function(p){var l,f,u,d=0,v=!1,S=!1,X=function(){f==null||f.unsubscribe(),f=void 0},re=function(){X(),l=u=void 0,v=S=!1},ee=function(){var k=l;re(),k==null||k.unsubscribe()};return E(function(k,ut){d++,!S&&!v&&X();var je=u=u!=null?u:r();ut.add(function(){d--,d===0&&!S&&!v&&(f=Br(ee,c))}),je.subscribe(ut),!l&&d>0&&(l=new bt({next:function(R){return je.next(R)},error:function(R){S=!0,X(),f=Br(re,n,R),je.error(R)},complete:function(){v=!0,X(),f=Br(re,s),je.complete()}}),U(k).subscribe(l))})(p)}}function Br(e,t){for(var r=[],o=2;oe.next(document)),e}function M(e,t=document){return Array.from(t.querySelectorAll(e))}function j(e,t=document){let r=ue(e,t);if(typeof r=="undefined")throw new ReferenceError(`Missing element: expected "${e}" to be present`);return r}function ue(e,t=document){return t.querySelector(e)||void 0}function Ne(){var e,t,r,o;return(o=(r=(t=(e=document.activeElement)==null?void 0:e.shadowRoot)==null?void 0:t.activeElement)!=null?r:document.activeElement)!=null?o:void 0}var Ra=L(h(document.body,"focusin"),h(document.body,"focusout")).pipe(Ae(1),Q(void 0),m(()=>Ne()||document.body),Z(1));function Ye(e){return Ra.pipe(m(t=>e.contains(t)),Y())}function it(e,t){return H(()=>L(h(e,"mouseenter").pipe(m(()=>!0)),h(e,"mouseleave").pipe(m(()=>!1))).pipe(t?jt(r=>He(+!r*t)):be,Q(e.matches(":hover"))))}function sn(e,t){if(typeof t=="string"||typeof t=="number")e.innerHTML+=t.toString();else if(t instanceof Node)e.appendChild(t);else if(Array.isArray(t))for(let r of t)sn(e,r)}function x(e,t,...r){let o=document.createElement(e);if(t)for(let n of Object.keys(t))typeof t[n]!="undefined"&&(typeof t[n]!="boolean"?o.setAttribute(n,t[n]):o.setAttribute(n,""));for(let n of r)sn(o,n);return o}function br(e){if(e>999){let t=+((e-950)%1e3>99);return`${((e+1e-6)/1e3).toFixed(t)}k`}else return e.toString()}function _t(e){let t=x("script",{src:e});return H(()=>(document.head.appendChild(t),L(h(t,"load"),h(t,"error").pipe(b(()=>Nr(()=>new ReferenceError(`Invalid script: ${e}`))))).pipe(m(()=>{}),A(()=>document.head.removeChild(t)),Ee(1))))}var cn=new T,Ia=H(()=>typeof ResizeObserver=="undefined"?_t("https://unpkg.com/resize-observer-polyfill"):$(void 0)).pipe(m(()=>new ResizeObserver(e=>e.forEach(t=>cn.next(t)))),b(e=>L(tt,$(e)).pipe(A(()=>e.disconnect()))),Z(1));function de(e){return{width:e.offsetWidth,height:e.offsetHeight}}function Le(e){let t=e;for(;t.clientWidth===0&&t.parentElement;)t=t.parentElement;return Ia.pipe(O(r=>r.observe(t)),b(r=>cn.pipe(g(o=>o.target===t),A(()=>r.unobserve(t)))),m(()=>de(e)),Q(de(e)))}function At(e){return{width:e.scrollWidth,height:e.scrollHeight}}function vr(e){let t=e.parentElement;for(;t&&(e.scrollWidth<=t.scrollWidth&&e.scrollHeight<=t.scrollHeight);)t=(e=t).parentElement;return t?e:void 0}function pn(e){let t=[],r=e.parentElement;for(;r;)(e.clientWidth>r.clientWidth||e.clientHeight>r.clientHeight)&&t.push(r),r=(e=r).parentElement;return t.length===0&&t.push(document.documentElement),t}function Be(e){return{x:e.offsetLeft,y:e.offsetTop}}function ln(e){let t=e.getBoundingClientRect();return{x:t.x+window.scrollX,y:t.y+window.scrollY}}function mn(e){return L(h(window,"load"),h(window,"resize")).pipe($e(0,ye),m(()=>Be(e)),Q(Be(e)))}function gr(e){return{x:e.scrollLeft,y:e.scrollTop}}function Ge(e){return L(h(e,"scroll"),h(window,"scroll"),h(window,"resize")).pipe($e(0,ye),m(()=>gr(e)),Q(gr(e)))}var fn=new T,Fa=H(()=>$(new IntersectionObserver(e=>{for(let t of e)fn.next(t)},{threshold:0}))).pipe(b(e=>L(tt,$(e)).pipe(A(()=>e.disconnect()))),Z(1));function mt(e){return Fa.pipe(O(t=>t.observe(e)),b(t=>fn.pipe(g(({target:r})=>r===e),A(()=>t.unobserve(e)),m(({isIntersecting:r})=>r))))}function un(e,t=16){return Ge(e).pipe(m(({y:r})=>{let o=de(e),n=At(e);return r>=n.height-o.height-t}),Y())}var yr={drawer:j("[data-md-toggle=drawer]"),search:j("[data-md-toggle=search]")};function dn(e){return yr[e].checked}function at(e,t){yr[e].checked!==t&&yr[e].click()}function Je(e){let t=yr[e];return h(t,"change").pipe(m(()=>t.checked),Q(t.checked))}function ja(e,t){switch(e.constructor){case HTMLInputElement:return e.type==="radio"?/^Arrow/.test(t):!0;case HTMLSelectElement:case HTMLTextAreaElement:return!0;default:return e.isContentEditable}}function Ua(){return L(h(window,"compositionstart").pipe(m(()=>!0)),h(window,"compositionend").pipe(m(()=>!1))).pipe(Q(!1))}function hn(){let e=h(window,"keydown").pipe(g(t=>!(t.metaKey||t.ctrlKey)),m(t=>({mode:dn("search")?"search":"global",type:t.key,claim(){t.preventDefault(),t.stopPropagation()}})),g(({mode:t,type:r})=>{if(t==="global"){let o=Ne();if(typeof o!="undefined")return!ja(o,r)}return!0}),le());return Ua().pipe(b(t=>t?y:e))}function we(){return new URL(location.href)}function st(e,t=!1){if(V("navigation.instant")&&!t){let r=x("a",{href:e.href});document.body.appendChild(r),r.click(),r.remove()}else location.href=e.href}function bn(){return new T}function vn(){return location.hash.slice(1)}function gn(e){let t=x("a",{href:e});t.addEventListener("click",r=>r.stopPropagation()),t.click()}function Zr(e){return L(h(window,"hashchange"),e).pipe(m(vn),Q(vn()),g(t=>t.length>0),Z(1))}function yn(e){return Zr(e).pipe(m(t=>ue(`[id="${t}"]`)),g(t=>typeof t!="undefined"))}function Wt(e){let t=matchMedia(e);return ur(r=>t.addListener(()=>r(t.matches))).pipe(Q(t.matches))}function xn(){let e=matchMedia("print");return L(h(window,"beforeprint").pipe(m(()=>!0)),h(window,"afterprint").pipe(m(()=>!1))).pipe(Q(e.matches))}function eo(e,t){return e.pipe(b(r=>r?t():y))}function to(e,t){return new F(r=>{let o=new XMLHttpRequest;return o.open("GET",`${e}`),o.responseType="blob",o.addEventListener("load",()=>{o.status>=200&&o.status<300?(r.next(o.response),r.complete()):r.error(new Error(o.statusText))}),o.addEventListener("error",()=>{r.error(new Error("Network error"))}),o.addEventListener("abort",()=>{r.complete()}),typeof(t==null?void 0:t.progress$)!="undefined"&&(o.addEventListener("progress",n=>{var i;if(n.lengthComputable)t.progress$.next(n.loaded/n.total*100);else{let s=(i=o.getResponseHeader("Content-Length"))!=null?i:0;t.progress$.next(n.loaded/+s*100)}}),t.progress$.next(5)),o.send(),()=>o.abort()})}function ze(e,t){return to(e,t).pipe(b(r=>r.text()),m(r=>JSON.parse(r)),Z(1))}function xr(e,t){let r=new DOMParser;return to(e,t).pipe(b(o=>o.text()),m(o=>r.parseFromString(o,"text/html")),Z(1))}function En(e,t){let r=new DOMParser;return to(e,t).pipe(b(o=>o.text()),m(o=>r.parseFromString(o,"text/xml")),Z(1))}function wn(){return{x:Math.max(0,scrollX),y:Math.max(0,scrollY)}}function Tn(){return L(h(window,"scroll",{passive:!0}),h(window,"resize",{passive:!0})).pipe(m(wn),Q(wn()))}function Sn(){return{width:innerWidth,height:innerHeight}}function On(){return h(window,"resize",{passive:!0}).pipe(m(Sn),Q(Sn()))}function Ln(){return z([Tn(),On()]).pipe(m(([e,t])=>({offset:e,size:t})),Z(1))}function Er(e,{viewport$:t,header$:r}){let o=t.pipe(ne("size")),n=z([o,r]).pipe(m(()=>Be(e)));return z([r,t,n]).pipe(m(([{height:i},{offset:s,size:a},{x:c,y:p}])=>({offset:{x:s.x-c,y:s.y-p+i},size:a})))}function Wa(e){return h(e,"message",t=>t.data)}function Da(e){let t=new T;return t.subscribe(r=>e.postMessage(r)),t}function Mn(e,t=new Worker(e)){let r=Wa(t),o=Da(t),n=new T;n.subscribe(o);let i=o.pipe(oe(),ae(!0));return n.pipe(oe(),Ve(r.pipe(W(i))),le())}var Va=j("#__config"),Ct=JSON.parse(Va.textContent);Ct.base=`${new URL(Ct.base,we())}`;function Te(){return Ct}function V(e){return Ct.features.includes(e)}function Me(e,t){return typeof t!="undefined"?Ct.translations[e].replace("#",t.toString()):Ct.translations[e]}function Ce(e,t=document){return j(`[data-md-component=${e}]`,t)}function me(e,t=document){return M(`[data-md-component=${e}]`,t)}function Na(e){let t=j(".md-typeset > :first-child",e);return h(t,"click",{once:!0}).pipe(m(()=>j(".md-typeset",e)),m(r=>({hash:__md_hash(r.innerHTML)})))}function _n(e){if(!V("announce.dismiss")||!e.childElementCount)return y;if(!e.hidden){let t=j(".md-typeset",e);__md_hash(t.innerHTML)===__md_get("__announce")&&(e.hidden=!0)}return H(()=>{let t=new T;return t.subscribe(({hash:r})=>{e.hidden=!0,__md_set("__announce",r)}),Na(e).pipe(O(r=>t.next(r)),A(()=>t.complete()),m(r=>P({ref:e},r)))})}function za(e,{target$:t}){return t.pipe(m(r=>({hidden:r!==e})))}function An(e,t){let r=new T;return r.subscribe(({hidden:o})=>{e.hidden=o}),za(e,t).pipe(O(o=>r.next(o)),A(()=>r.complete()),m(o=>P({ref:e},o)))}function Dt(e,t){return t==="inline"?x("div",{class:"md-tooltip md-tooltip--inline",id:e,role:"tooltip"},x("div",{class:"md-tooltip__inner md-typeset"})):x("div",{class:"md-tooltip",id:e,role:"tooltip"},x("div",{class:"md-tooltip__inner md-typeset"}))}function wr(...e){return x("div",{class:"md-tooltip2",role:"dialog"},x("div",{class:"md-tooltip2__inner md-typeset"},e))}function Cn(...e){return x("div",{class:"md-tooltip2",role:"tooltip"},x("div",{class:"md-tooltip2__inner md-typeset"},e))}function kn(e,t){if(t=t?`${t}_annotation_${e}`:void 0,t){let r=t?`#${t}`:void 0;return x("aside",{class:"md-annotation",tabIndex:0},Dt(t),x("a",{href:r,class:"md-annotation__index",tabIndex:-1},x("span",{"data-md-annotation-id":e})))}else return x("aside",{class:"md-annotation",tabIndex:0},Dt(t),x("span",{class:"md-annotation__index",tabIndex:-1},x("span",{"data-md-annotation-id":e})))}function Hn(e){return x("button",{class:"md-code__button",title:Me("clipboard.copy"),"data-clipboard-target":`#${e} > code`,"data-md-type":"copy"})}function $n(){return x("button",{class:"md-code__button",title:"Toggle line selection","data-md-type":"select"})}function Pn(){return x("nav",{class:"md-code__nav"})}var In=$t(ro());function oo(e,t){let r=t&2,o=t&1,n=Object.keys(e.terms).filter(c=>!e.terms[c]).reduce((c,p)=>[...c,x("del",null,(0,In.default)(p))," "],[]).slice(0,-1),i=Te(),s=new URL(e.location,i.base);V("search.highlight")&&s.searchParams.set("h",Object.entries(e.terms).filter(([,c])=>c).reduce((c,[p])=>`${c} ${p}`.trim(),""));let{tags:a}=Te();return x("a",{href:`${s}`,class:"md-search-result__link",tabIndex:-1},x("article",{class:"md-search-result__article md-typeset","data-md-score":e.score.toFixed(2)},r>0&&x("div",{class:"md-search-result__icon md-icon"}),r>0&&x("h1",null,e.title),r<=0&&x("h2",null,e.title),o>0&&e.text.length>0&&e.text,e.tags&&x("nav",{class:"md-tags"},e.tags.map(c=>{let p=a?c in a?`md-tag-icon md-tag--${a[c]}`:"md-tag-icon":"";return x("span",{class:`md-tag ${p}`},c)})),o>0&&n.length>0&&x("p",{class:"md-search-result__terms"},Me("search.result.term.missing"),": ",...n)))}function Fn(e){let t=e[0].score,r=[...e],o=Te(),n=r.findIndex(l=>!`${new URL(l.location,o.base)}`.includes("#")),[i]=r.splice(n,1),s=r.findIndex(l=>l.scoreoo(l,1)),...c.length?[x("details",{class:"md-search-result__more"},x("summary",{tabIndex:-1},x("div",null,c.length>0&&c.length===1?Me("search.result.more.one"):Me("search.result.more.other",c.length))),...c.map(l=>oo(l,1)))]:[]];return x("li",{class:"md-search-result__item"},p)}function jn(e){return x("ul",{class:"md-source__facts"},Object.entries(e).map(([t,r])=>x("li",{class:`md-source__fact md-source__fact--${t}`},typeof r=="number"?br(r):r)))}function no(e){let t=`tabbed-control tabbed-control--${e}`;return x("div",{class:t,hidden:!0},x("button",{class:"tabbed-button",tabIndex:-1,"aria-hidden":"true"}))}function Un(e){return x("div",{class:"md-typeset__scrollwrap"},x("div",{class:"md-typeset__table"},e))}function Qa(e){var o;let t=Te(),r=new URL(`../${e.version}/`,t.base);return x("li",{class:"md-version__item"},x("a",{href:`${r}`,class:"md-version__link"},e.title,((o=t.version)==null?void 0:o.alias)&&e.aliases.length>0&&x("span",{class:"md-version__alias"},e.aliases[0])))}function Wn(e,t){var o;let r=Te();return e=e.filter(n=>{var i;return!((i=n.properties)!=null&&i.hidden)}),x("div",{class:"md-version"},x("button",{class:"md-version__current","aria-label":Me("select.version")},t.title,((o=r.version)==null?void 0:o.alias)&&t.aliases.length>0&&x("span",{class:"md-version__alias"},t.aliases[0])),x("ul",{class:"md-version__list"},e.map(Qa)))}var Ya=0;function Ba(e,t=250){let r=z([Ye(e),it(e,t)]).pipe(m(([n,i])=>n||i),Y()),o=H(()=>pn(e)).pipe(J(Ge),gt(1),Pe(r),m(()=>ln(e)));return r.pipe(Re(n=>n),b(()=>z([r,o])),m(([n,i])=>({active:n,offset:i})),le())}function Vt(e,t,r=250){let{content$:o,viewport$:n}=t,i=`__tooltip2_${Ya++}`;return H(()=>{let s=new T,a=new jr(!1);s.pipe(oe(),ae(!1)).subscribe(a);let c=a.pipe(jt(l=>He(+!l*250,Dr)),Y(),b(l=>l?o:y),O(l=>l.id=i),le());z([s.pipe(m(({active:l})=>l)),c.pipe(b(l=>it(l,250)),Q(!1))]).pipe(m(l=>l.some(f=>f))).subscribe(a);let p=a.pipe(g(l=>l),te(c,n),m(([l,f,{size:u}])=>{let d=e.getBoundingClientRect(),v=d.width/2;if(f.role==="tooltip")return{x:v,y:8+d.height};if(d.y>=u.height/2){let{height:S}=de(f);return{x:v,y:-16-S}}else return{x:v,y:16+d.height}}));return z([c,s,p]).subscribe(([l,{offset:f},u])=>{l.style.setProperty("--md-tooltip-host-x",`${f.x}px`),l.style.setProperty("--md-tooltip-host-y",`${f.y}px`),l.style.setProperty("--md-tooltip-x",`${u.x}px`),l.style.setProperty("--md-tooltip-y",`${u.y}px`),l.classList.toggle("md-tooltip2--top",u.y<0),l.classList.toggle("md-tooltip2--bottom",u.y>=0)}),a.pipe(g(l=>l),te(c,(l,f)=>f),g(l=>l.role==="tooltip")).subscribe(l=>{let f=de(j(":scope > *",l));l.style.setProperty("--md-tooltip-width",`${f.width}px`),l.style.setProperty("--md-tooltip-tail","0px")}),a.pipe(Y(),xe(ye),te(c)).subscribe(([l,f])=>{f.classList.toggle("md-tooltip2--active",l)}),z([a.pipe(g(l=>l)),c]).subscribe(([l,f])=>{f.role==="dialog"?(e.setAttribute("aria-controls",i),e.setAttribute("aria-haspopup","dialog")):e.setAttribute("aria-describedby",i)}),a.pipe(g(l=>!l)).subscribe(()=>{e.removeAttribute("aria-controls"),e.removeAttribute("aria-describedby"),e.removeAttribute("aria-haspopup")}),Ba(e,r).pipe(O(l=>s.next(l)),A(()=>s.complete()),m(l=>P({ref:e},l)))})}function Xe(e,{viewport$:t},r=document.body){return Vt(e,{content$:new F(o=>{let n=e.title,i=Cn(n);return o.next(i),e.removeAttribute("title"),r.append(i),()=>{i.remove(),e.setAttribute("title",n)}}),viewport$:t},0)}function Ga(e,t){let r=H(()=>z([mn(e),Ge(t)])).pipe(m(([{x:o,y:n},i])=>{let{width:s,height:a}=de(e);return{x:o-i.x+s/2,y:n-i.y+a/2}}));return Ye(e).pipe(b(o=>r.pipe(m(n=>({active:o,offset:n})),Ee(+!o||1/0))))}function Dn(e,t,{target$:r}){let[o,n]=Array.from(e.children);return H(()=>{let i=new T,s=i.pipe(oe(),ae(!0));return i.subscribe({next({offset:a}){e.style.setProperty("--md-tooltip-x",`${a.x}px`),e.style.setProperty("--md-tooltip-y",`${a.y}px`)},complete(){e.style.removeProperty("--md-tooltip-x"),e.style.removeProperty("--md-tooltip-y")}}),mt(e).pipe(W(s)).subscribe(a=>{e.toggleAttribute("data-md-visible",a)}),L(i.pipe(g(({active:a})=>a)),i.pipe(Ae(250),g(({active:a})=>!a))).subscribe({next({active:a}){a?e.prepend(o):o.remove()},complete(){e.prepend(o)}}),i.pipe($e(16,ye)).subscribe(({active:a})=>{o.classList.toggle("md-tooltip--active",a)}),i.pipe(gt(125,ye),g(()=>!!e.offsetParent),m(()=>e.offsetParent.getBoundingClientRect()),m(({x:a})=>a)).subscribe({next(a){a?e.style.setProperty("--md-tooltip-0",`${-a}px`):e.style.removeProperty("--md-tooltip-0")},complete(){e.style.removeProperty("--md-tooltip-0")}}),h(n,"click").pipe(W(s),g(a=>!(a.metaKey||a.ctrlKey))).subscribe(a=>{a.stopPropagation(),a.preventDefault()}),h(n,"mousedown").pipe(W(s),te(i)).subscribe(([a,{active:c}])=>{var p;if(a.button!==0||a.metaKey||a.ctrlKey)a.preventDefault();else if(c){a.preventDefault();let l=e.parentElement.closest(".md-annotation");l instanceof HTMLElement?l.focus():(p=Ne())==null||p.blur()}}),r.pipe(W(s),g(a=>a===o),nt(125)).subscribe(()=>e.focus()),Ga(e,t).pipe(O(a=>i.next(a)),A(()=>i.complete()),m(a=>P({ref:e},a)))})}function Ja(e){let t=Te();if(e.tagName!=="CODE")return[e];let r=[".c",".c1",".cm"];if(t.annotate&&typeof t.annotate=="object"){let o=e.closest("[class|=language]");if(o)for(let n of Array.from(o.classList)){if(!n.startsWith("language-"))continue;let[,i]=n.split("-");i in t.annotate&&r.push(...t.annotate[i])}}return M(r.join(", "),e)}function Xa(e){let t=[];for(let r of Ja(e)){let o=[],n=document.createNodeIterator(r,NodeFilter.SHOW_TEXT);for(let i=n.nextNode();i;i=n.nextNode())o.push(i);for(let i of o){let s;for(;s=/(\(\d+\))(!)?/.exec(i.textContent);){let[,a,c]=s;if(typeof c=="undefined"){let p=i.splitText(s.index);i=p.splitText(a.length),t.push(p)}else{i.textContent=a,t.push(i);break}}}}return t}function Vn(e,t){t.append(...Array.from(e.childNodes))}function Tr(e,t,{target$:r,print$:o}){let n=t.closest("[id]"),i=n==null?void 0:n.id,s=new Map;for(let a of Xa(t)){let[,c]=a.textContent.match(/\((\d+)\)/);ue(`:scope > li:nth-child(${c})`,e)&&(s.set(c,kn(c,i)),a.replaceWith(s.get(c)))}return s.size===0?y:H(()=>{let a=new T,c=a.pipe(oe(),ae(!0)),p=[];for(let[l,f]of s)p.push([j(".md-typeset",f),j(`:scope > li:nth-child(${l})`,e)]);return o.pipe(W(c)).subscribe(l=>{e.hidden=!l,e.classList.toggle("md-annotation-list",l);for(let[f,u]of p)l?Vn(f,u):Vn(u,f)}),L(...[...s].map(([,l])=>Dn(l,t,{target$:r}))).pipe(A(()=>a.complete()),le())})}function Nn(e){if(e.nextElementSibling){let t=e.nextElementSibling;if(t.tagName==="OL")return t;if(t.tagName==="P"&&!t.children.length)return Nn(t)}}function zn(e,t){return H(()=>{let r=Nn(e);return typeof r!="undefined"?Tr(r,e,t):y})}var Kn=$t(ao());var Za=0,qn=L(h(window,"keydown").pipe(m(()=>!0)),L(h(window,"keyup"),h(window,"contextmenu")).pipe(m(()=>!1))).pipe(Q(!1),Z(1));function Qn(e){if(e.nextElementSibling){let t=e.nextElementSibling;if(t.tagName==="OL")return t;if(t.tagName==="P"&&!t.children.length)return Qn(t)}}function es(e){return Le(e).pipe(m(({width:t})=>({scrollable:At(e).width>t})),ne("scrollable"))}function Yn(e,t){let{matches:r}=matchMedia("(hover)"),o=H(()=>{let n=new T,i=n.pipe(Yr(1));n.subscribe(({scrollable:d})=>{d&&r?e.setAttribute("tabindex","0"):e.removeAttribute("tabindex")});let s=[],a=e.closest("pre"),c=a.closest("[id]"),p=c?c.id:Za++;a.id=`__code_${p}`;let l=[],f=e.closest(".highlight");if(f instanceof HTMLElement){let d=Qn(f);if(typeof d!="undefined"&&(f.classList.contains("annotate")||V("content.code.annotate"))){let v=Tr(d,e,t);l.push(Le(f).pipe(W(i),m(({width:S,height:X})=>S&&X),Y(),b(S=>S?v:y)))}}let u=M(":scope > span[id]",e);if(u.length&&(e.classList.add("md-code__content"),e.closest(".select")||V("content.code.select")&&!e.closest(".no-select"))){let d=+u[0].id.split("-").pop(),v=$n();s.push(v),V("content.tooltips")&&l.push(Xe(v,{viewport$}));let S=h(v,"click").pipe(Ut(R=>!R,!1),O(()=>v.blur()),le());S.subscribe(R=>{v.classList.toggle("md-code__button--active",R)});let X=fe(u).pipe(J(R=>it(R).pipe(m(se=>[R,se]))));S.pipe(b(R=>R?X:y)).subscribe(([R,se])=>{let ce=ue(".hll.select",R);if(ce&&!se)ce.replaceWith(...Array.from(ce.childNodes));else if(!ce&&se){let he=document.createElement("span");he.className="hll select",he.append(...Array.from(R.childNodes).slice(1)),R.append(he)}});let re=fe(u).pipe(J(R=>h(R,"mousedown").pipe(O(se=>se.preventDefault()),m(()=>R)))),ee=S.pipe(b(R=>R?re:y),te(qn),m(([R,se])=>{var he;let ce=u.indexOf(R)+d;if(se===!1)return[ce,ce];{let Se=M(".hll",e).map(Ue=>u.indexOf(Ue.parentElement)+d);return(he=window.getSelection())==null||he.removeAllRanges(),[Math.min(ce,...Se),Math.max(ce,...Se)]}})),k=Zr(y).pipe(g(R=>R.startsWith(`__codelineno-${p}-`)));k.subscribe(R=>{let[,,se]=R.split("-"),ce=se.split(":").map(Se=>+Se-d+1);ce.length===1&&ce.push(ce[0]);for(let Se of M(".hll:not(.select)",e))Se.replaceWith(...Array.from(Se.childNodes));let he=u.slice(ce[0]-1,ce[1]);for(let Se of he){let Ue=document.createElement("span");Ue.className="hll",Ue.append(...Array.from(Se.childNodes).slice(1)),Se.append(Ue)}}),k.pipe(Ee(1),xe(pe)).subscribe(R=>{if(R.includes(":")){let se=document.getElementById(R.split(":")[0]);se&&setTimeout(()=>{let ce=se,he=-64;for(;ce!==document.body;)he+=ce.offsetTop,ce=ce.offsetParent;window.scrollTo({top:he})},1)}});let je=fe(M('a[href^="#__codelineno"]',f)).pipe(J(R=>h(R,"click").pipe(O(se=>se.preventDefault()),m(()=>R)))).pipe(W(i),te(qn),m(([R,se])=>{let he=+j(`[id="${R.hash.slice(1)}"]`).parentElement.id.split("-").pop();if(se===!1)return[he,he];{let Se=M(".hll",e).map(Ue=>+Ue.parentElement.id.split("-").pop());return[Math.min(he,...Se),Math.max(he,...Se)]}}));L(ee,je).subscribe(R=>{let se=`#__codelineno-${p}-`;R[0]===R[1]?se+=R[0]:se+=`${R[0]}:${R[1]}`,history.replaceState({},"",se),window.dispatchEvent(new HashChangeEvent("hashchange",{newURL:window.location.origin+window.location.pathname+se,oldURL:window.location.href}))})}if(Kn.default.isSupported()&&(e.closest(".copy")||V("content.code.copy")&&!e.closest(".no-copy"))){let d=Hn(a.id);s.push(d),V("content.tooltips")&&l.push(Xe(d,{viewport$}))}if(s.length){let d=Pn();d.append(...s),a.insertBefore(d,e)}return es(e).pipe(O(d=>n.next(d)),A(()=>n.complete()),m(d=>P({ref:e},d)),Ve(L(...l).pipe(W(i))))});return V("content.lazy")?mt(e).pipe(g(n=>n),Ee(1),b(()=>o)):o}function ts(e,{target$:t,print$:r}){let o=!0;return L(t.pipe(m(n=>n.closest("details:not([open])")),g(n=>e===n),m(()=>({action:"open",reveal:!0}))),r.pipe(g(n=>n||!o),O(()=>o=e.open),m(n=>({action:n?"open":"close"}))))}function Bn(e,t){return H(()=>{let r=new T;return r.subscribe(({action:o,reveal:n})=>{e.toggleAttribute("open",o==="open"),n&&e.scrollIntoView()}),ts(e,t).pipe(O(o=>r.next(o)),A(()=>r.complete()),m(o=>P({ref:e},o)))})}var Gn=0;function rs(e){let t=document.createElement("h3");t.innerHTML=e.innerHTML;let r=[t],o=e.nextElementSibling;for(;o&&!(o instanceof HTMLHeadingElement);)r.push(o),o=o.nextElementSibling;return r}function os(e,t){for(let r of M("[href], [src]",e))for(let o of["href","src"]){let n=r.getAttribute(o);if(n&&!/^(?:[a-z]+:)?\/\//i.test(n)){r[o]=new URL(r.getAttribute(o),t).toString();break}}for(let r of M("[name^=__], [for]",e))for(let o of["id","for","name"]){let n=r.getAttribute(o);n&&r.setAttribute(o,`${n}$preview_${Gn}`)}return Gn++,$(e)}function Jn(e,t){let{sitemap$:r}=t;if(!(e instanceof HTMLAnchorElement))return y;if(!(V("navigation.instant.preview")||e.hasAttribute("data-preview")))return y;e.removeAttribute("title");let o=z([Ye(e),it(e)]).pipe(m(([i,s])=>i||s),Y(),g(i=>i));return rt([r,o]).pipe(b(([i])=>{let s=new URL(e.href);return s.search=s.hash="",i.has(`${s}`)?$(s):y}),b(i=>xr(i).pipe(b(s=>os(s,i)))),b(i=>{let s=e.hash?`article [id="${e.hash.slice(1)}"]`:"article h1",a=ue(s,i);return typeof a=="undefined"?y:$(rs(a))})).pipe(b(i=>{let s=new F(a=>{let c=wr(...i);return a.next(c),document.body.append(c),()=>c.remove()});return Vt(e,P({content$:s},t))}))}var Xn=".node circle,.node ellipse,.node path,.node polygon,.node rect{fill:var(--md-mermaid-node-bg-color);stroke:var(--md-mermaid-node-fg-color)}marker{fill:var(--md-mermaid-edge-color)!important}.edgeLabel .label rect{fill:#0000}.flowchartTitleText{fill:var(--md-mermaid-label-fg-color)}.label{color:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}.label foreignObject{line-height:normal;overflow:visible}.label div .edgeLabel{color:var(--md-mermaid-label-fg-color)}.edgeLabel,.edgeLabel p,.label div .edgeLabel{background-color:var(--md-mermaid-label-bg-color)}.edgeLabel,.edgeLabel p{fill:var(--md-mermaid-label-bg-color);color:var(--md-mermaid-edge-color)}.edgePath .path,.flowchart-link{stroke:var(--md-mermaid-edge-color)}.edgePath .arrowheadPath{fill:var(--md-mermaid-edge-color);stroke:none}.cluster rect{fill:var(--md-default-fg-color--lightest);stroke:var(--md-default-fg-color--lighter)}.cluster span{color:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}g #flowchart-circleEnd,g #flowchart-circleStart,g #flowchart-crossEnd,g #flowchart-crossStart,g #flowchart-pointEnd,g #flowchart-pointStart{stroke:none}.classDiagramTitleText{fill:var(--md-mermaid-label-fg-color)}g.classGroup line,g.classGroup rect{fill:var(--md-mermaid-node-bg-color);stroke:var(--md-mermaid-node-fg-color)}g.classGroup text{fill:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}.classLabel .box{fill:var(--md-mermaid-label-bg-color);background-color:var(--md-mermaid-label-bg-color);opacity:1}.classLabel .label{fill:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}.node .divider{stroke:var(--md-mermaid-node-fg-color)}.relation{stroke:var(--md-mermaid-edge-color)}.cardinality{fill:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}.cardinality text{fill:inherit!important}defs marker.marker.composition.class path,defs marker.marker.dependency.class path,defs marker.marker.extension.class path{fill:var(--md-mermaid-edge-color)!important;stroke:var(--md-mermaid-edge-color)!important}defs marker.marker.aggregation.class path{fill:var(--md-mermaid-label-bg-color)!important;stroke:var(--md-mermaid-edge-color)!important}.statediagramTitleText{fill:var(--md-mermaid-label-fg-color)}g.stateGroup rect{fill:var(--md-mermaid-node-bg-color);stroke:var(--md-mermaid-node-fg-color)}g.stateGroup .state-title{fill:var(--md-mermaid-label-fg-color)!important;font-family:var(--md-mermaid-font-family)}g.stateGroup .composit{fill:var(--md-mermaid-label-bg-color)}.nodeLabel,.nodeLabel p{color:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}a .nodeLabel{text-decoration:underline}.node circle.state-end,.node circle.state-start,.start-state{fill:var(--md-mermaid-edge-color);stroke:none}.end-state-inner,.end-state-outer{fill:var(--md-mermaid-edge-color)}.end-state-inner,.node circle.state-end{stroke:var(--md-mermaid-label-bg-color)}.transition{stroke:var(--md-mermaid-edge-color)}[id^=state-fork] rect,[id^=state-join] rect{fill:var(--md-mermaid-edge-color)!important;stroke:none!important}.statediagram-cluster.statediagram-cluster .inner{fill:var(--md-default-bg-color)}.statediagram-cluster rect{fill:var(--md-mermaid-node-bg-color);stroke:var(--md-mermaid-node-fg-color)}.statediagram-state rect.divider{fill:var(--md-default-fg-color--lightest);stroke:var(--md-default-fg-color--lighter)}defs #statediagram-barbEnd{stroke:var(--md-mermaid-edge-color)}[id^=entity] path,[id^=entity] rect{fill:var(--md-default-bg-color)}.relationshipLine{stroke:var(--md-mermaid-edge-color)}defs .marker.oneOrMore.er *,defs .marker.onlyOne.er *,defs .marker.zeroOrMore.er *,defs .marker.zeroOrOne.er *{stroke:var(--md-mermaid-edge-color)!important}text:not([class]):last-child{fill:var(--md-mermaid-label-fg-color)}.actor{fill:var(--md-mermaid-sequence-actor-bg-color);stroke:var(--md-mermaid-sequence-actor-border-color)}text.actor>tspan{fill:var(--md-mermaid-sequence-actor-fg-color);font-family:var(--md-mermaid-font-family)}line{stroke:var(--md-mermaid-sequence-actor-line-color)}.actor-man circle,.actor-man line{fill:var(--md-mermaid-sequence-actorman-bg-color);stroke:var(--md-mermaid-sequence-actorman-line-color)}.messageLine0,.messageLine1{stroke:var(--md-mermaid-sequence-message-line-color)}.note{fill:var(--md-mermaid-sequence-note-bg-color);stroke:var(--md-mermaid-sequence-note-border-color)}.loopText,.loopText>tspan,.messageText,.noteText>tspan{stroke:none;font-family:var(--md-mermaid-font-family)!important}.messageText{fill:var(--md-mermaid-sequence-message-fg-color)}.loopText,.loopText>tspan{fill:var(--md-mermaid-sequence-loop-fg-color)}.noteText>tspan{fill:var(--md-mermaid-sequence-note-fg-color)}#arrowhead path{fill:var(--md-mermaid-sequence-message-line-color);stroke:none}.loopLine{fill:var(--md-mermaid-sequence-loop-bg-color);stroke:var(--md-mermaid-sequence-loop-border-color)}.labelBox{fill:var(--md-mermaid-sequence-label-bg-color);stroke:none}.labelText,.labelText>span{fill:var(--md-mermaid-sequence-label-fg-color);font-family:var(--md-mermaid-font-family)}.sequenceNumber{fill:var(--md-mermaid-sequence-number-fg-color)}rect.rect{fill:var(--md-mermaid-sequence-box-bg-color);stroke:none}rect.rect+text.text{fill:var(--md-mermaid-sequence-box-fg-color)}defs #sequencenumber{fill:var(--md-mermaid-sequence-number-bg-color)!important}";var so,is=0;function as(){return typeof mermaid=="undefined"||mermaid instanceof Element?_t("https://unpkg.com/mermaid@11/dist/mermaid.min.js"):$(void 0)}function Zn(e){return e.classList.remove("mermaid"),so||(so=as().pipe(O(()=>mermaid.initialize({startOnLoad:!1,themeCSS:Xn,sequence:{actorFontSize:"16px",messageFontSize:"16px",noteFontSize:"16px"}})),m(()=>{}),Z(1))),so.subscribe(()=>go(null,null,function*(){e.classList.add("mermaid");let t=`__mermaid_${is++}`,r=x("div",{class:"mermaid"}),o=e.textContent,{svg:n,fn:i}=yield mermaid.render(t,o),s=r.attachShadow({mode:"closed"});s.innerHTML=n,e.replaceWith(r),i==null||i(s)})),so.pipe(m(()=>({ref:e})))}var ei=x("table");function ti(e){return e.replaceWith(ei),ei.replaceWith(Un(e)),$({ref:e})}function ss(e){let t=e.find(r=>r.checked)||e[0];return L(...e.map(r=>h(r,"change").pipe(m(()=>j(`label[for="${r.id}"]`))))).pipe(Q(j(`label[for="${t.id}"]`)),m(r=>({active:r})))}function ri(e,{viewport$:t,target$:r}){let o=j(".tabbed-labels",e),n=M(":scope > input",e),i=no("prev");e.append(i);let s=no("next");return e.append(s),H(()=>{let a=new T,c=a.pipe(oe(),ae(!0));z([a,Le(e),mt(e)]).pipe(W(c),$e(1,ye)).subscribe({next([{active:p},l]){let f=Be(p),{width:u}=de(p);e.style.setProperty("--md-indicator-x",`${f.x}px`),e.style.setProperty("--md-indicator-width",`${u}px`);let d=gr(o);(f.xd.x+l.width)&&o.scrollTo({left:Math.max(0,f.x-16),behavior:"smooth"})},complete(){e.style.removeProperty("--md-indicator-x"),e.style.removeProperty("--md-indicator-width")}}),z([Ge(o),Le(o)]).pipe(W(c)).subscribe(([p,l])=>{let f=At(o);i.hidden=p.x<16,s.hidden=p.x>f.width-l.width-16}),L(h(i,"click").pipe(m(()=>-1)),h(s,"click").pipe(m(()=>1))).pipe(W(c)).subscribe(p=>{let{width:l}=de(o);o.scrollBy({left:l*p,behavior:"smooth"})}),r.pipe(W(c),g(p=>n.includes(p))).subscribe(p=>p.click()),o.classList.add("tabbed-labels--linked");for(let p of n){let l=j(`label[for="${p.id}"]`);l.replaceChildren(x("a",{href:`#${l.htmlFor}`,tabIndex:-1},...Array.from(l.childNodes))),h(l.firstElementChild,"click").pipe(W(c),g(f=>!(f.metaKey||f.ctrlKey)),O(f=>{f.preventDefault(),f.stopPropagation()})).subscribe(()=>{history.replaceState({},"",`#${l.htmlFor}`),l.click()})}return V("content.tabs.link")&&a.pipe(Ie(1),te(t)).subscribe(([{active:p},{offset:l}])=>{let f=p.innerText.trim();if(p.hasAttribute("data-md-switching"))p.removeAttribute("data-md-switching");else{let u=e.offsetTop-l.y;for(let v of M("[data-tabs]"))for(let S of M(":scope > input",v)){let X=j(`label[for="${S.id}"]`);if(X!==p&&X.innerText.trim()===f){X.setAttribute("data-md-switching",""),S.click();break}}window.scrollTo({top:e.offsetTop-u});let d=__md_get("__tabs")||[];__md_set("__tabs",[...new Set([f,...d])])}}),a.pipe(W(c)).subscribe(()=>{for(let p of M("audio, video",e))p.offsetWidth&&p.autoplay?p.play().catch(()=>{}):p.pause()}),ss(n).pipe(O(p=>a.next(p)),A(()=>a.complete()),m(p=>P({ref:e},p)))}).pipe(et(pe))}function oi(e,t){let{viewport$:r,target$:o,print$:n}=t;return L(...M(".annotate:not(.highlight)",e).map(i=>zn(i,{target$:o,print$:n})),...M("pre:not(.mermaid) > code",e).map(i=>Yn(i,{target$:o,print$:n})),...M("a",e).map(i=>Jn(i,t)),...M("pre.mermaid",e).map(i=>Zn(i)),...M("table:not([class])",e).map(i=>ti(i)),...M("details",e).map(i=>Bn(i,{target$:o,print$:n})),...M("[data-tabs]",e).map(i=>ri(i,{viewport$:r,target$:o})),...M("[title]:not([data-preview])",e).filter(()=>V("content.tooltips")).map(i=>Xe(i,{viewport$:r})),...M(".footnote-ref",e).filter(()=>V("content.footnote.tooltips")).map(i=>Vt(i,{content$:new F(s=>{let a=new URL(i.href).hash.slice(1),c=Array.from(document.getElementById(a).cloneNode(!0).children),p=wr(...c);return s.next(p),document.body.append(p),()=>p.remove()}),viewport$:r})))}function cs(e,{alert$:t}){return t.pipe(b(r=>L($(!0),$(!1).pipe(nt(2e3))).pipe(m(o=>({message:r,active:o})))))}function ni(e,t){let r=j(".md-typeset",e);return H(()=>{let o=new T;return o.subscribe(({message:n,active:i})=>{e.classList.toggle("md-dialog--active",i),r.textContent=n}),cs(e,t).pipe(O(n=>o.next(n)),A(()=>o.complete()),m(n=>P({ref:e},n)))})}var ps=0;function ls(e,t){document.body.append(e);let{width:r}=de(e);e.style.setProperty("--md-tooltip-width",`${r}px`),e.remove();let o=vr(t),n=typeof o!="undefined"?Ge(o):$({x:0,y:0}),i=L(Ye(t),it(t)).pipe(Y());return z([i,n]).pipe(m(([s,a])=>{let{x:c,y:p}=Be(t),l=de(t),f=t.closest("table");return f&&t.parentElement&&(c+=f.offsetLeft+t.parentElement.offsetLeft,p+=f.offsetTop+t.parentElement.offsetTop),{active:s,offset:{x:c-a.x+l.width/2-r/2,y:p-a.y+l.height+8}}}))}function ii(e){let t=e.title;if(!t.length)return y;let r=`__tooltip_${ps++}`,o=Dt(r,"inline"),n=j(".md-typeset",o);return n.innerHTML=t,H(()=>{let i=new T;return i.subscribe({next({offset:s}){o.style.setProperty("--md-tooltip-x",`${s.x}px`),o.style.setProperty("--md-tooltip-y",`${s.y}px`)},complete(){o.style.removeProperty("--md-tooltip-x"),o.style.removeProperty("--md-tooltip-y")}}),L(i.pipe(g(({active:s})=>s)),i.pipe(Ae(250),g(({active:s})=>!s))).subscribe({next({active:s}){s?(e.insertAdjacentElement("afterend",o),e.setAttribute("aria-describedby",r),e.removeAttribute("title")):(o.remove(),e.removeAttribute("aria-describedby"),e.setAttribute("title",t))},complete(){o.remove(),e.removeAttribute("aria-describedby"),e.setAttribute("title",t)}}),i.pipe($e(16,ye)).subscribe(({active:s})=>{o.classList.toggle("md-tooltip--active",s)}),i.pipe(gt(125,ye),g(()=>!!e.offsetParent),m(()=>e.offsetParent.getBoundingClientRect()),m(({x:s})=>s)).subscribe({next(s){s?o.style.setProperty("--md-tooltip-0",`${-s}px`):o.style.removeProperty("--md-tooltip-0")},complete(){o.style.removeProperty("--md-tooltip-0")}}),ls(o,e).pipe(O(s=>i.next(s)),A(()=>i.complete()),m(s=>P({ref:e},s)))}).pipe(et(pe))}function ms({viewport$:e}){if(!V("header.autohide"))return $(!1);let t=e.pipe(m(({offset:{y:n}})=>n),ot(2,1),m(([n,i])=>[nMath.abs(i-n.y)>100),m(([,[n]])=>n),Y()),o=Je("search");return z([e,o]).pipe(m(([{offset:n},i])=>n.y>400&&!i),Y(),b(n=>n?r:$(!1)),Q(!1))}function ai(e,t){return H(()=>z([Le(e),ms(t)])).pipe(m(([{height:r},o])=>({height:r,hidden:o})),Y((r,o)=>r.height===o.height&&r.hidden===o.hidden),Z(1))}function si(e,{header$:t,main$:r}){return H(()=>{let o=new T,n=o.pipe(oe(),ae(!0));o.pipe(ne("active"),Pe(t)).subscribe(([{active:s},{hidden:a}])=>{e.classList.toggle("md-header--shadow",s&&!a),e.hidden=a});let i=fe(M("[title]",e)).pipe(g(()=>V("content.tooltips")),J(s=>ii(s)));return r.subscribe(o),t.pipe(W(n),m(s=>P({ref:e},s)),Ve(i.pipe(W(n))))})}function fs(e,{viewport$:t,header$:r}){return Er(e,{viewport$:t,header$:r}).pipe(m(({offset:{y:o}})=>{let{height:n}=de(e);return{active:n>0&&o>=n}}),ne("active"))}function ci(e,t){return H(()=>{let r=new T;r.subscribe({next({active:n}){e.classList.toggle("md-header__title--active",n)},complete(){e.classList.remove("md-header__title--active")}});let o=ue(".md-content h1");return typeof o=="undefined"?y:fs(o,t).pipe(O(n=>r.next(n)),A(()=>r.complete()),m(n=>P({ref:e},n)))})}function pi(e,{viewport$:t,header$:r}){let o=r.pipe(m(({height:i})=>i),Y()),n=o.pipe(b(()=>Le(e).pipe(m(({height:i})=>({top:e.offsetTop,bottom:e.offsetTop+i})),ne("bottom"))));return z([o,n,t]).pipe(m(([i,{top:s,bottom:a},{offset:{y:c},size:{height:p}}])=>(p=Math.max(0,p-Math.max(0,s-c,i)-Math.max(0,p+c-a)),{offset:s-i,height:p,active:s-i<=c})),Y((i,s)=>i.offset===s.offset&&i.height===s.height&&i.active===s.active))}function us(e){let t=__md_get("__palette")||{index:e.findIndex(o=>matchMedia(o.getAttribute("data-md-color-media")).matches)},r=Math.max(0,Math.min(t.index,e.length-1));return $(...e).pipe(J(o=>h(o,"change").pipe(m(()=>o))),Q(e[r]),m(o=>({index:e.indexOf(o),color:{media:o.getAttribute("data-md-color-media"),scheme:o.getAttribute("data-md-color-scheme"),primary:o.getAttribute("data-md-color-primary"),accent:o.getAttribute("data-md-color-accent")}})),Z(1))}function li(e){let t=M("input",e),r=x("meta",{name:"theme-color"});document.head.appendChild(r);let o=x("meta",{name:"color-scheme"});document.head.appendChild(o);let n=Wt("(prefers-color-scheme: light)");return H(()=>{let i=new T;return i.subscribe(s=>{if(document.body.setAttribute("data-md-color-switching",""),s.color.media==="(prefers-color-scheme)"){let a=matchMedia("(prefers-color-scheme: light)"),c=document.querySelector(a.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");s.color.scheme=c.getAttribute("data-md-color-scheme"),s.color.primary=c.getAttribute("data-md-color-primary"),s.color.accent=c.getAttribute("data-md-color-accent")}for(let[a,c]of Object.entries(s.color))document.body.setAttribute(`data-md-color-${a}`,c);for(let a=0;as.key==="Enter"),te(i,(s,a)=>a)).subscribe(({index:s})=>{s=(s+1)%t.length,t[s].click(),t[s].focus()}),i.pipe(m(()=>{let s=Ce("header"),a=window.getComputedStyle(s);return o.content=a.colorScheme,a.backgroundColor.match(/\d+/g).map(c=>(+c).toString(16).padStart(2,"0")).join("")})).subscribe(s=>r.content=`#${s}`),i.pipe(xe(pe)).subscribe(()=>{document.body.removeAttribute("data-md-color-switching")}),us(t).pipe(W(n.pipe(Ie(1))),vt(),O(s=>i.next(s)),A(()=>i.complete()),m(s=>P({ref:e},s)))})}function mi(e,{progress$:t}){return H(()=>{let r=new T;return r.subscribe(({value:o})=>{e.style.setProperty("--md-progress-value",`${o}`)}),t.pipe(O(o=>r.next({value:o})),A(()=>r.complete()),m(o=>({ref:e,value:o})))})}function fi(e,t){return e.protocol=t.protocol,e.hostname=t.hostname,e}function ds(e,t){let r=new Map;for(let o of M("url",e)){let n=j("loc",o),i=[fi(new URL(n.textContent),t)];r.set(`${i[0]}`,i);for(let s of M("[rel=alternate]",o)){let a=s.getAttribute("href");a!=null&&i.push(fi(new URL(a),t))}}return r}function kt(e){return En(new URL("sitemap.xml",e)).pipe(m(t=>ds(t,new URL(e))),ve(()=>$(new Map)),le())}function ui({document$:e}){let t=new Map;e.pipe(b(()=>M("link[rel=alternate]")),m(r=>new URL(r.href)),g(r=>!t.has(r.toString())),J(r=>kt(r).pipe(m(o=>[r,o]),ve(()=>y)))).subscribe(([r,o])=>{t.set(r.toString().replace(/\/$/,""),o)}),h(document.body,"click").pipe(g(r=>!r.metaKey&&!r.ctrlKey),b(r=>{if(r.target instanceof Element){let o=r.target.closest("a");if(o&&!o.target){let n=[...t].find(([f])=>o.href.startsWith(`${f}/`));if(typeof n=="undefined")return y;let[i,s]=n,a=we();if(a.href.startsWith(i))return y;let c=Te(),p=a.href.replace(c.base,"");p=`${i}/${p}`;let l=s.has(p.split("#")[0])?new URL(p,c.base):new URL(i);return r.preventDefault(),$(l)}}return y})).subscribe(r=>st(r,!0))}var co=$t(ao());function hs(e){e.setAttribute("data-md-copying","");let t=e.closest("[data-copy]"),r=t?t.getAttribute("data-copy"):e.innerText;return e.removeAttribute("data-md-copying"),r.trimEnd()}function di({alert$:e}){co.default.isSupported()&&new F(t=>{new co.default("[data-clipboard-target], [data-clipboard-text]",{text:r=>r.getAttribute("data-clipboard-text")||hs(j(r.getAttribute("data-clipboard-target")))}).on("success",r=>t.next(r))}).pipe(O(t=>{t.trigger.focus()}),m(()=>Me("clipboard.copied"))).subscribe(e)}function hi(e,t){if(!(e.target instanceof Element))return y;let r=e.target.closest("a");if(r===null)return y;if(r.target||e.metaKey||e.ctrlKey)return y;let o=new URL(r.href);return o.search=o.hash="",t.has(`${o}`)?(e.preventDefault(),$(r)):y}function bi(e){let t=new Map;for(let r of M(":scope > *",e.head))t.set(r.outerHTML,r);return t}function vi(e){for(let t of M("[href], [src]",e))for(let r of["href","src"]){let o=t.getAttribute(r);if(o&&!/^(?:[a-z]+:)?\/\//i.test(o)){t[r]=t[r];break}}return $(e)}function bs(e){for(let o of["[data-md-component=announce]","[data-md-component=container]","[data-md-component=header-topic]","[data-md-component=outdated]","[data-md-component=logo]","[data-md-component=skip]",...V("navigation.tabs.sticky")?["[data-md-component=tabs]"]:[]]){let n=ue(o),i=ue(o,e);typeof n!="undefined"&&typeof i!="undefined"&&n.replaceWith(i)}let t=bi(document);for(let[o,n]of bi(e))t.has(o)?t.delete(o):document.head.appendChild(n);for(let o of t.values()){let n=o.getAttribute("name");n!=="theme-color"&&n!=="color-scheme"&&o.remove()}let r=Ce("container");return Ke(M("script",r)).pipe(b(o=>{let n=e.createElement("script");if(o.src){for(let i of o.getAttributeNames())n.setAttribute(i,o.getAttribute(i));return o.replaceWith(n),new F(i=>{n.onload=()=>i.complete()})}else return n.textContent=o.textContent,o.replaceWith(n),y}),oe(),ae(document))}function gi({sitemap$:e,location$:t,viewport$:r,progress$:o}){if(location.protocol==="file:")return y;$(document).subscribe(vi);let n=h(document.body,"click").pipe(Pe(e),b(([a,c])=>hi(a,c)),m(({href:a})=>new URL(a)),le()),i=h(window,"popstate").pipe(m(we),le());n.pipe(te(r)).subscribe(([a,{offset:c}])=>{history.replaceState(c,""),history.pushState(null,"",a)}),L(n,i).subscribe(t);let s=t.pipe(ne("pathname"),b(a=>xr(a,{progress$:o}).pipe(ve(()=>(st(a,!0),y)))),b(vi),b(bs),le());return L(s.pipe(te(t,(a,c)=>c)),s.pipe(b(()=>t),ne("hash")),t.pipe(Y((a,c)=>a.pathname===c.pathname&&a.hash===c.hash),b(()=>n),O(()=>history.back()))).subscribe(a=>{var c,p;history.state!==null||!a.hash?window.scrollTo(0,(p=(c=history.state)==null?void 0:c.y)!=null?p:0):(history.scrollRestoration="auto",gn(a.hash),history.scrollRestoration="manual")}),t.subscribe(()=>{history.scrollRestoration="manual"}),h(window,"beforeunload").subscribe(()=>{history.scrollRestoration="auto"}),r.pipe(ne("offset"),Ae(100)).subscribe(({offset:a})=>{history.replaceState(a,"")}),V("navigation.instant.prefetch")&&L(h(document.body,"mousemove"),h(document.body,"focusin")).pipe(Pe(e),b(([a,c])=>hi(a,c)),Ae(25),Qr(({href:a})=>a),hr(a=>{let c=document.createElement("link");return c.rel="prefetch",c.href=a.toString(),document.head.appendChild(c),h(c,"load").pipe(m(()=>c),Ee(1))})).subscribe(a=>a.remove()),s}var yi=$t(ro());function xi(e){let t=e.separator.split("|").map(n=>n.replace(/(\(\?[!=<][^)]+\))/g,"").length===0?"\uFFFD":n).join("|"),r=new RegExp(t,"img"),o=(n,i,s)=>`${i}${s}`;return n=>{n=n.replace(/[\s*+\-:~^]+/g," ").replace(/&/g,"&").trim();let i=new RegExp(`(^|${e.separator}|)(${n.replace(/[|\\{}()[\]^$+*?.-]/g,"\\$&").replace(r,"|")})`,"img");return s=>(0,yi.default)(s).replace(i,o).replace(/<\/mark>(\s+)]*>/img,"$1")}}function zt(e){return e.type===1}function Sr(e){return e.type===3}function Ei(e,t){let r=Mn(e);return L($(location.protocol!=="file:"),Je("search")).pipe(Re(o=>o),b(()=>t)).subscribe(({config:o,docs:n})=>r.next({type:0,data:{config:o,docs:n,options:{suggest:V("search.suggest")}}})),r}function wi(e){var l;let{selectedVersionSitemap:t,selectedVersionBaseURL:r,currentLocation:o,currentBaseURL:n}=e,i=(l=po(n))==null?void 0:l.pathname;if(i===void 0)return;let s=ys(o.pathname,i);if(s===void 0)return;let a=Es(t.keys());if(!t.has(a))return;let c=po(s,a);if(!c||!t.has(c.href))return;let p=po(s,r);if(p)return p.hash=o.hash,p.search=o.search,p}function po(e,t){try{return new URL(e,t)}catch(r){return}}function ys(e,t){if(e.startsWith(t))return e.slice(t.length)}function xs(e,t){let r=Math.min(e.length,t.length),o;for(o=0;oy)),o=r.pipe(m(n=>{let[,i]=t.base.match(/([^/]+)\/?$/);return n.find(({version:s,aliases:a})=>s===i||a.includes(i))||n[0]}));r.pipe(m(n=>new Map(n.map(i=>[`${new URL(`../${i.version}/`,t.base)}`,i]))),b(n=>h(document.body,"click").pipe(g(i=>!i.metaKey&&!i.ctrlKey),te(o),b(([i,s])=>{if(i.target instanceof Element){let a=i.target.closest("a");if(a&&!a.target&&n.has(a.href)){let c=a.href;return!i.target.closest(".md-version")&&n.get(c)===s?y:(i.preventDefault(),$(new URL(c)))}}return y}),b(i=>kt(i).pipe(m(s=>{var a;return(a=wi({selectedVersionSitemap:s,selectedVersionBaseURL:i,currentLocation:we(),currentBaseURL:t.base}))!=null?a:i})))))).subscribe(n=>st(n,!0)),z([r,o]).subscribe(([n,i])=>{j(".md-header__topic").appendChild(Wn(n,i))}),e.pipe(b(()=>o)).subscribe(n=>{var a;let i=new URL(t.base),s=__md_get("__outdated",sessionStorage,i);if(s===null){s=!0;let c=((a=t.version)==null?void 0:a.default)||"latest";Array.isArray(c)||(c=[c]);e:for(let p of c)for(let l of n.aliases.concat(n.version))if(new RegExp(p,"i").test(l)){s=!1;break e}__md_set("__outdated",s,sessionStorage,i)}if(s)for(let c of me("outdated"))c.hidden=!1})}function ws(e,{worker$:t}){let{searchParams:r}=we();r.has("q")&&(at("search",!0),e.value=r.get("q"),e.focus(),Je("search").pipe(Re(i=>!i)).subscribe(()=>{let i=we();i.searchParams.delete("q"),history.replaceState({},"",`${i}`)}));let o=Ye(e),n=L(t.pipe(Re(zt)),h(e,"keyup"),o).pipe(m(()=>e.value),Y());return z([n,o]).pipe(m(([i,s])=>({value:i,focus:s})),Z(1))}function Si(e,{worker$:t}){let r=new T,o=r.pipe(oe(),ae(!0));z([t.pipe(Re(zt)),r],(i,s)=>s).pipe(ne("value")).subscribe(({value:i})=>t.next({type:2,data:i})),r.pipe(ne("focus")).subscribe(({focus:i})=>{i&&at("search",i)}),h(e.form,"reset").pipe(W(o)).subscribe(()=>e.focus());let n=j("header [for=__search]");return h(n,"click").subscribe(()=>e.focus()),ws(e,{worker$:t}).pipe(O(i=>r.next(i)),A(()=>r.complete()),m(i=>P({ref:e},i)),Z(1))}function Oi(e,{worker$:t,query$:r}){let o=new T,n=un(e.parentElement).pipe(g(Boolean)),i=e.parentElement,s=j(":scope > :first-child",e),a=j(":scope > :last-child",e);Je("search").subscribe(l=>{a.setAttribute("role",l?"list":"presentation"),a.hidden=!l}),o.pipe(te(r),Gr(t.pipe(Re(zt)))).subscribe(([{items:l},{value:f}])=>{switch(l.length){case 0:s.textContent=f.length?Me("search.result.none"):Me("search.result.placeholder");break;case 1:s.textContent=Me("search.result.one");break;default:let u=br(l.length);s.textContent=Me("search.result.other",u)}});let c=o.pipe(O(()=>a.innerHTML=""),b(({items:l})=>L($(...l.slice(0,10)),$(...l.slice(10)).pipe(ot(4),Xr(n),b(([f])=>f)))),m(Fn),le());return c.subscribe(l=>a.appendChild(l)),c.pipe(J(l=>{let f=ue("details",l);return typeof f=="undefined"?y:h(f,"toggle").pipe(W(o),m(()=>f))})).subscribe(l=>{l.open===!1&&l.offsetTop<=i.scrollTop&&i.scrollTo({top:l.offsetTop})}),t.pipe(g(Sr),m(({data:l})=>l)).pipe(O(l=>o.next(l)),A(()=>o.complete()),m(l=>P({ref:e},l)))}function Ts(e,{query$:t}){return t.pipe(m(({value:r})=>{let o=we();return o.hash="",r=r.replace(/\s+/g,"+").replace(/&/g,"%26").replace(/=/g,"%3D"),o.search=`q=${r}`,{url:o}}))}function Li(e,t){let r=new T,o=r.pipe(oe(),ae(!0));return r.subscribe(({url:n})=>{e.setAttribute("data-clipboard-text",e.href),e.href=`${n}`}),h(e,"click").pipe(W(o)).subscribe(n=>n.preventDefault()),Ts(e,t).pipe(O(n=>r.next(n)),A(()=>r.complete()),m(n=>P({ref:e},n)))}function Mi(e,{worker$:t,keyboard$:r}){let o=new T,n=Ce("search-query"),i=L(h(n,"keydown"),h(n,"focus")).pipe(xe(pe),m(()=>n.value),Y());return o.pipe(Pe(i),m(([{suggest:a},c])=>{let p=c.split(/([\s-]+)/);if(a!=null&&a.length&&p[p.length-1]){let l=a[a.length-1];l.startsWith(p[p.length-1])&&(p[p.length-1]=l)}else p.length=0;return p})).subscribe(a=>e.innerHTML=a.join("").replace(/\s/g," ")),r.pipe(g(({mode:a})=>a==="search")).subscribe(a=>{a.type==="ArrowRight"&&e.innerText.length&&n.selectionStart===n.value.length&&(n.value=e.innerText)}),t.pipe(g(Sr),m(({data:a})=>a)).pipe(O(a=>o.next(a)),A(()=>o.complete()),m(()=>({ref:e})))}function _i(e,{index$:t,keyboard$:r}){let o=Te();try{let n=Ei(o.search,t),i=Ce("search-query",e),s=Ce("search-result",e);h(e,"click").pipe(g(({target:c})=>c instanceof Element&&!!c.closest("a"))).subscribe(()=>at("search",!1)),r.pipe(g(({mode:c})=>c==="search")).subscribe(c=>{let p=Ne();switch(c.type){case"Enter":if(p===i){let l=new Map;for(let f of M(":first-child [href]",s)){let u=f.firstElementChild;l.set(f,parseFloat(u.getAttribute("data-md-score")))}if(l.size){let[[f]]=[...l].sort(([,u],[,d])=>d-u);f.click()}c.claim()}break;case"Escape":case"Tab":at("search",!1),i.blur();break;case"ArrowUp":case"ArrowDown":if(typeof p=="undefined")i.focus();else{let l=[i,...M(":not(details) > [href], summary, details[open] [href]",s)],f=Math.max(0,(Math.max(0,l.indexOf(p))+l.length+(c.type==="ArrowUp"?-1:1))%l.length);l[f].focus()}c.claim();break;default:i!==Ne()&&i.focus()}}),r.pipe(g(({mode:c})=>c==="global")).subscribe(c=>{switch(c.type){case"f":case"s":case"/":i.focus(),i.select(),c.claim();break}});let a=Si(i,{worker$:n});return L(a,Oi(s,{worker$:n,query$:a})).pipe(Ve(...me("search-share",e).map(c=>Li(c,{query$:a})),...me("search-suggest",e).map(c=>Mi(c,{worker$:n,keyboard$:r}))))}catch(n){return e.hidden=!0,tt}}function Ai(e,{index$:t,location$:r}){return z([t,r.pipe(Q(we()),g(o=>!!o.searchParams.get("h")))]).pipe(m(([o,n])=>xi(o.config)(n.searchParams.get("h"))),m(o=>{var s;let n=new Map,i=document.createNodeIterator(e,NodeFilter.SHOW_TEXT);for(let a=i.nextNode();a;a=i.nextNode())if((s=a.parentElement)!=null&&s.offsetHeight){let c=a.textContent,p=o(c);p.length>c.length&&n.set(a,p)}for(let[a,c]of n){let{childNodes:p}=x("span",null,c);a.replaceWith(...Array.from(p))}return{ref:e,nodes:n}}))}function Ss(e,{viewport$:t,main$:r}){let o=e.closest(".md-grid"),n=o.offsetTop-o.parentElement.offsetTop;return z([r,t]).pipe(m(([{offset:i,height:s},{offset:{y:a}}])=>(s=s+Math.min(n,Math.max(0,a-i))-n,{height:s,locked:a>=i+n})),Y((i,s)=>i.height===s.height&&i.locked===s.locked))}function lo(e,o){var n=o,{header$:t}=n,r=vo(n,["header$"]);let i=j(".md-sidebar__scrollwrap",e),{y:s}=Be(i);return H(()=>{let a=new T,c=a.pipe(oe(),ae(!0)),p=a.pipe($e(0,ye));return p.pipe(te(t)).subscribe({next([{height:l},{height:f}]){i.style.height=`${l-2*s}px`,e.style.top=`${f}px`},complete(){i.style.height="",e.style.top=""}}),p.pipe(Re()).subscribe(()=>{for(let l of M(".md-nav__link--active[href]",e)){if(!l.clientHeight)continue;let f=l.closest(".md-sidebar__scrollwrap");if(typeof f!="undefined"){let u=l.offsetTop-f.offsetTop,{height:d}=de(f);f.scrollTo({top:u-d/2})}}}),fe(M("label[tabindex]",e)).pipe(J(l=>h(l,"click").pipe(xe(pe),m(()=>l),W(c)))).subscribe(l=>{let f=j(`[id="${l.htmlFor}"]`);j(`[aria-labelledby="${l.id}"]`).setAttribute("aria-expanded",`${f.checked}`)}),V("content.tooltips")&&fe(M("abbr[title]",e)).pipe(J(l=>Xe(l,{viewport$})),W(c)).subscribe(),Ss(e,r).pipe(O(l=>a.next(l)),A(()=>a.complete()),m(l=>P({ref:e},l)))})}function Ci(e,t){if(typeof t!="undefined"){let r=`https://api.github.com/repos/${e}/${t}`;return rt(ze(`${r}/releases/latest`).pipe(ve(()=>y),m(o=>({version:o.tag_name})),Qe({})),ze(r).pipe(ve(()=>y),m(o=>({stars:o.stargazers_count,forks:o.forks_count})),Qe({}))).pipe(m(([o,n])=>P(P({},o),n)))}else{let r=`https://api.github.com/users/${e}`;return ze(r).pipe(m(o=>({repositories:o.public_repos})),Qe({}))}}function ki(e,t){let r=`https://${e}/api/v4/projects/${encodeURIComponent(t)}`;return rt(ze(`${r}/releases/permalink/latest`).pipe(ve(()=>y),m(({tag_name:o})=>({version:o})),Qe({})),ze(r).pipe(ve(()=>y),m(({star_count:o,forks_count:n})=>({stars:o,forks:n})),Qe({}))).pipe(m(([o,n])=>P(P({},o),n)))}function Hi(e){let t=e.match(/^.+github\.com\/([^/]+)\/?([^/]+)?/i);if(t){let[,r,o]=t;return Ci(r,o)}if(t=e.match(/^.+?([^/]*gitlab[^/]+)\/(.+?)\/?$/i),t){let[,r,o]=t;return ki(r,o)}return y}var Os;function Ls(e){return Os||(Os=H(()=>{let t=__md_get("__source",sessionStorage);if(t)return $(t);if(me("consent").length){let o=__md_get("__consent");if(!(o&&o.github))return y}return Hi(e.href).pipe(O(o=>__md_set("__source",o,sessionStorage)))}).pipe(ve(()=>y),g(t=>Object.keys(t).length>0),m(t=>({facts:t})),Z(1)))}function $i(e){let t=j(":scope > :last-child",e);return H(()=>{let r=new T;return r.subscribe(({facts:o})=>{t.appendChild(jn(o)),t.classList.add("md-source__repository--active")}),Ls(e).pipe(O(o=>r.next(o)),A(()=>r.complete()),m(o=>P({ref:e},o)))})}function Ms(e,{viewport$:t,header$:r}){return Le(document.body).pipe(b(()=>Er(e,{header$:r,viewport$:t})),m(({offset:{y:o}})=>({hidden:o>=10})),ne("hidden"))}function Pi(e,t){return H(()=>{let r=new T;return r.subscribe({next({hidden:o}){e.hidden=o},complete(){e.hidden=!1}}),(V("navigation.tabs.sticky")?$({hidden:!1}):Ms(e,t)).pipe(O(o=>r.next(o)),A(()=>r.complete()),m(o=>P({ref:e},o)))})}function _s(e,{viewport$:t,header$:r}){let o=new Map,n=M(".md-nav__link",e);for(let a of n){let c=decodeURIComponent(a.hash.substring(1)),p=ue(`[id="${c}"]`);typeof p!="undefined"&&o.set(a,p)}let i=r.pipe(ne("height"),m(({height:a})=>{let c=Ce("main"),p=j(":scope > :first-child",c);return a+.8*(p.offsetTop-c.offsetTop)}),le());return Le(document.body).pipe(ne("height"),b(a=>H(()=>{let c=[];return $([...o].reduce((p,[l,f])=>{for(;c.length&&o.get(c[c.length-1]).tagName>=f.tagName;)c.pop();let u=f.offsetTop;for(;!u&&f.parentElement;)f=f.parentElement,u=f.offsetTop;let d=f.offsetParent;for(;d;d=d.offsetParent)u+=d.offsetTop;return p.set([...c=[...c,l]].reverse(),u)},new Map))}).pipe(m(c=>new Map([...c].sort(([,p],[,l])=>p-l))),Pe(i),b(([c,p])=>t.pipe(Ut(([l,f],{offset:{y:u},size:d})=>{let v=u+d.height>=Math.floor(a.height);for(;f.length;){let[,S]=f[0];if(S-p=u&&!v)f=[l.pop(),...f];else break}return[l,f]},[[],[...c]]),Y((l,f)=>l[0]===f[0]&&l[1]===f[1])))))).pipe(m(([a,c])=>({prev:a.map(([p])=>p),next:c.map(([p])=>p)})),Q({prev:[],next:[]}),ot(2,1),m(([a,c])=>a.prev.length{let i=new T,s=i.pipe(oe(),ae(!0));if(i.subscribe(({prev:a,next:c})=>{for(let[p]of c)p.classList.remove("md-nav__link--passed"),p.classList.remove("md-nav__link--active");for(let[p,[l]]of a.entries())l.classList.add("md-nav__link--passed"),l.classList.toggle("md-nav__link--active",p===a.length-1)}),V("toc.follow")){let a=L(t.pipe(Ae(1),m(()=>{})),t.pipe(Ae(250),m(()=>"smooth")));i.pipe(g(({prev:c})=>c.length>0),Pe(o.pipe(xe(pe))),te(a)).subscribe(([[{prev:c}],p])=>{let[l]=c[c.length-1];if(l.offsetHeight){let f=vr(l);if(typeof f!="undefined"){let u=l.offsetTop-f.offsetTop,{height:d}=de(f);f.scrollTo({top:u-d/2,behavior:p})}}})}return V("navigation.tracking")&&t.pipe(W(s),ne("offset"),Ae(250),Ie(1),W(n.pipe(Ie(1))),vt({delay:250}),te(i)).subscribe(([,{prev:a}])=>{let c=we(),p=a[a.length-1];if(p&&p.length){let[l]=p,{hash:f}=new URL(l.href);c.hash!==f&&(c.hash=f,history.replaceState({},"",`${c}`))}else c.hash="",history.replaceState({},"",`${c}`)}),_s(e,{viewport$:t,header$:r}).pipe(O(a=>i.next(a)),A(()=>i.complete()),m(a=>P({ref:e},a)))})}function As(e,{viewport$:t,main$:r,target$:o}){let n=t.pipe(m(({offset:{y:s}})=>s),ot(2,1),m(([s,a])=>s>a&&a>0),Y()),i=r.pipe(m(({active:s})=>s));return z([i,n]).pipe(m(([s,a])=>!(s&&a)),Y(),W(o.pipe(Ie(1))),ae(!0),vt({delay:250}),m(s=>({hidden:s})))}function Ii(e,{viewport$:t,header$:r,main$:o,target$:n}){let i=new T,s=i.pipe(oe(),ae(!0));return i.subscribe({next({hidden:a}){e.hidden=a,a?(e.setAttribute("tabindex","-1"),e.blur()):e.removeAttribute("tabindex")},complete(){e.style.top="",e.hidden=!0,e.removeAttribute("tabindex")}}),r.pipe(W(s),ne("height")).subscribe(({height:a})=>{e.style.top=`${a+16}px`}),h(e,"click").subscribe(a=>{a.preventDefault(),window.scrollTo({top:0})}),As(e,{viewport$:t,main$:o,target$:n}).pipe(O(a=>i.next(a)),A(()=>i.complete()),m(a=>P({ref:e},a)))}function Fi({document$:e,viewport$:t}){e.pipe(b(()=>M(".md-ellipsis")),J(r=>mt(r).pipe(W(e.pipe(Ie(1))),g(o=>o),m(()=>r),Ee(1))),g(r=>r.offsetWidth{let o=r.innerText,n=r.closest("a")||r;return n.title=o,V("content.tooltips")?Xe(n,{viewport$:t}).pipe(W(e.pipe(Ie(1))),A(()=>n.removeAttribute("title"))):y})).subscribe(),V("content.tooltips")&&e.pipe(b(()=>M(".md-status")),J(r=>Xe(r,{viewport$:t}))).subscribe()}function ji({document$:e,tablet$:t}){e.pipe(b(()=>M(".md-toggle--indeterminate")),O(r=>{r.indeterminate=!0,r.checked=!1}),J(r=>h(r,"change").pipe(Jr(()=>r.classList.contains("md-toggle--indeterminate")),m(()=>r))),te(t)).subscribe(([r,o])=>{r.classList.remove("md-toggle--indeterminate"),o&&(r.checked=!1)})}function Cs(){return/(iPad|iPhone|iPod)/.test(navigator.userAgent)}function Ui({document$:e}){e.pipe(b(()=>M("[data-md-scrollfix]")),O(t=>t.removeAttribute("data-md-scrollfix")),g(Cs),J(t=>h(t,"touchstart").pipe(m(()=>t)))).subscribe(t=>{let r=t.scrollTop;r===0?t.scrollTop=1:r+t.offsetHeight===t.scrollHeight&&(t.scrollTop=r-1)})}function Wi({viewport$:e,tablet$:t}){z([Je("search"),t]).pipe(m(([r,o])=>r&&!o),b(r=>$(r).pipe(nt(r?400:100))),te(e)).subscribe(([r,{offset:{y:o}}])=>{if(r)document.body.setAttribute("data-md-scrolllock",""),document.body.style.top=`-${o}px`;else{let n=-1*parseInt(document.body.style.top,10);document.body.removeAttribute("data-md-scrolllock"),document.body.style.top="",n&&window.scrollTo(0,n)}})}Object.entries||(Object.entries=function(e){let t=[];for(let r of Object.keys(e))t.push([r,e[r]]);return t});Object.values||(Object.values=function(e){let t=[];for(let r of Object.keys(e))t.push(e[r]);return t});typeof Element!="undefined"&&(Element.prototype.scrollTo||(Element.prototype.scrollTo=function(e,t){typeof e=="object"?(this.scrollLeft=e.left,this.scrollTop=e.top):(this.scrollLeft=e,this.scrollTop=t)}),Element.prototype.replaceWith||(Element.prototype.replaceWith=function(...e){let t=this.parentNode;if(t){e.length===0&&t.removeChild(this);for(let r=e.length-1;r>=0;r--){let o=e[r];typeof o=="string"?o=document.createTextNode(o):o.parentNode&&o.parentNode.removeChild(o),r?t.insertBefore(this.previousSibling,o):t.replaceChild(o,this)}}}));function ks(){return location.protocol==="file:"?_t(`${new URL("search/search_index.js",Or.base)}`).pipe(m(()=>__index),Z(1)):ze(new URL("search/search_index.json",Or.base))}document.documentElement.classList.remove("no-js");document.documentElement.classList.add("js");var ct=an(),Kt=bn(),Ht=yn(Kt),mo=hn(),ke=Ln(),Lr=Wt("(min-width: 60em)"),Vi=Wt("(min-width: 76.25em)"),Ni=xn(),Or=Te(),zi=document.forms.namedItem("search")?ks():tt,fo=new T;di({alert$:fo});ui({document$:ct});var uo=new T,qi=kt(Or.base);V("navigation.instant")&&gi({sitemap$:qi,location$:Kt,viewport$:ke,progress$:uo}).subscribe(ct);var Di;((Di=Or.version)==null?void 0:Di.provider)==="mike"&&Ti({document$:ct});L(Kt,Ht).pipe(nt(125)).subscribe(()=>{at("drawer",!1),at("search",!1)});mo.pipe(g(({mode:e})=>e==="global")).subscribe(e=>{switch(e.type){case"p":case",":let t=ue("link[rel=prev]");typeof t!="undefined"&&st(t);break;case"n":case".":let r=ue("link[rel=next]");typeof r!="undefined"&&st(r);break;case"Enter":let o=Ne();o instanceof HTMLLabelElement&&o.click()}});Fi({viewport$:ke,document$:ct});ji({document$:ct,tablet$:Lr});Ui({document$:ct});Wi({viewport$:ke,tablet$:Lr});var ft=ai(Ce("header"),{viewport$:ke}),qt=ct.pipe(m(()=>Ce("main")),b(e=>pi(e,{viewport$:ke,header$:ft})),Z(1)),Hs=L(...me("consent").map(e=>An(e,{target$:Ht})),...me("dialog").map(e=>ni(e,{alert$:fo})),...me("palette").map(e=>li(e)),...me("progress").map(e=>mi(e,{progress$:uo})),...me("search").map(e=>_i(e,{index$:zi,keyboard$:mo})),...me("source").map(e=>$i(e))),$s=H(()=>L(...me("announce").map(e=>_n(e)),...me("content").map(e=>oi(e,{sitemap$:qi,viewport$:ke,target$:Ht,print$:Ni})),...me("content").map(e=>V("search.highlight")?Ai(e,{index$:zi,location$:Kt}):y),...me("header").map(e=>si(e,{viewport$:ke,header$:ft,main$:qt})),...me("header-title").map(e=>ci(e,{viewport$:ke,header$:ft})),...me("sidebar").map(e=>e.getAttribute("data-md-type")==="navigation"?eo(Vi,()=>lo(e,{viewport$:ke,header$:ft,main$:qt})):eo(Lr,()=>lo(e,{viewport$:ke,header$:ft,main$:qt}))),...me("tabs").map(e=>Pi(e,{viewport$:ke,header$:ft})),...me("toc").map(e=>Ri(e,{viewport$:ke,header$:ft,main$:qt,target$:Ht})),...me("top").map(e=>Ii(e,{viewport$:ke,header$:ft,main$:qt,target$:Ht})))),Ki=ct.pipe(b(()=>$s),Ve(Hs),Z(1));Ki.subscribe();window.document$=ct;window.location$=Kt;window.target$=Ht;window.keyboard$=mo;window.viewport$=ke;window.tablet$=Lr;window.screen$=Vi;window.print$=Ni;window.alert$=fo;window.progress$=uo;window.component$=Ki;})(); //# sourceMappingURL=bundle.79ae519e.min.js.map -/*! update cache: 20260104155727 */ +/*! update cache: 20260104161427 */ diff --git a/assets/javascripts/glightbox.min.js b/assets/javascripts/glightbox.min.js index a245875f8..f80bbb027 100644 --- a/assets/javascripts/glightbox.min.js +++ b/assets/javascripts/glightbox.min.js @@ -1,2 +1,2 @@ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).GLightbox=t()}(this,(function(){"use strict";function e(t){return(e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(t)}function t(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){for(var i=0;i1&&void 0!==arguments[1]?arguments[1]:null,i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null,n=e[s]=e[s]||[],l={all:n,evt:null,found:null};return t&&i&&P(n)>0&&o(n,(function(e,n){if(e.eventName==t&&e.fn.toString()==i.toString())return l.found=!0,l.evt=n,!1})),l}function a(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=t.onElement,n=t.withCallback,s=t.avoidDuplicate,l=void 0===s||s,a=t.once,h=void 0!==a&&a,d=t.useCapture,c=void 0!==d&&d,u=arguments.length>2?arguments[2]:void 0,g=i||[];function v(e){T(n)&&n.call(u,e,this),h&&v.destroy()}return C(g)&&(g=document.querySelectorAll(g)),v.destroy=function(){o(g,(function(t){var i=r(t,e,v);i.found&&i.all.splice(i.evt,1),t.removeEventListener&&t.removeEventListener(e,v,c)}))},o(g,(function(t){var i=r(t,e,v);(t.addEventListener&&l&&!i.found||!l)&&(t.addEventListener(e,v,c),i.all.push({eventName:e,fn:v}))})),v}function h(e,t){o(t.split(" "),(function(t){return e.classList.add(t)}))}function d(e,t){o(t.split(" "),(function(t){return e.classList.remove(t)}))}function c(e,t){return e.classList.contains(t)}function u(e,t){for(;e!==document.body;){if(!(e=e.parentElement))return!1;if("function"==typeof e.matches?e.matches(t):e.msMatchesSelector(t))return e}}function g(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",i=arguments.length>2&&void 0!==arguments[2]&&arguments[2];if(!e||""===t)return!1;if("none"==t)return T(i)&&i(),!1;var n=x(),s=t.split(" ");o(s,(function(t){h(e,"g"+t)})),a(n,{onElement:e,avoidDuplicate:!1,once:!0,withCallback:function(e,t){o(s,(function(e){d(t,"g"+e)})),T(i)&&i()}})}function v(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";if(""==t)return e.style.webkitTransform="",e.style.MozTransform="",e.style.msTransform="",e.style.OTransform="",e.style.transform="",!1;e.style.webkitTransform=t,e.style.MozTransform=t,e.style.msTransform=t,e.style.OTransform=t,e.style.transform=t}function f(e){e.style.display="block"}function p(e){e.style.display="none"}function m(e){var t=document.createDocumentFragment(),i=document.createElement("div");for(i.innerHTML=e;i.firstChild;)t.appendChild(i.firstChild);return t}function y(){return{width:window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth,height:window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight}}function x(){var e,t=document.createElement("fakeelement"),i={animation:"animationend",OAnimation:"oAnimationEnd",MozAnimation:"animationend",WebkitAnimation:"webkitAnimationEnd"};for(e in i)if(void 0!==t.style[e])return i[e]}function b(e,t,i,n){if(e())t();else{var s;i||(i=100);var l=setInterval((function(){e()&&(clearInterval(l),s&&clearTimeout(s),t())}),i);n&&(s=setTimeout((function(){clearInterval(l)}),n))}}function S(e,t,i){if(I(e))console.error("Inject assets error");else if(T(t)&&(i=t,t=!1),C(t)&&t in window)T(i)&&i();else{var n;if(-1!==e.indexOf(".css")){if((n=document.querySelectorAll('link[href="'+e+'"]'))&&n.length>0)return void(T(i)&&i());var s=document.getElementsByTagName("head")[0],l=s.querySelectorAll('link[rel="stylesheet"]'),o=document.createElement("link");return o.rel="stylesheet",o.type="text/css",o.href=e,o.media="all",l?s.insertBefore(o,l[0]):s.appendChild(o),void(T(i)&&i())}if((n=document.querySelectorAll('script[src="'+e+'"]'))&&n.length>0){if(T(i)){if(C(t))return b((function(){return void 0!==window[t]}),(function(){i()})),!1;i()}}else{var r=document.createElement("script");r.type="text/javascript",r.src=e,r.onload=function(){if(T(i)){if(C(t))return b((function(){return void 0!==window[t]}),(function(){i()})),!1;i()}},document.body.appendChild(r)}}}function w(){return"navigator"in window&&window.navigator.userAgent.match(/(iPad)|(iPhone)|(iPod)|(Android)|(PlayBook)|(BB10)|(BlackBerry)|(Opera Mini)|(IEMobile)|(webOS)|(MeeGo)/i)}function T(e){return"function"==typeof e}function C(e){return"string"==typeof e}function k(e){return!(!e||!e.nodeType||1!=e.nodeType)}function E(e){return Array.isArray(e)}function A(e){return e&&e.length&&isFinite(e.length)}function L(t){return"object"===e(t)&&null!=t&&!T(t)&&!E(t)}function I(e){return null==e}function O(e,t){return null!==e&&hasOwnProperty.call(e,t)}function P(e){if(L(e)){if(e.keys)return e.keys().length;var t=0;for(var i in e)O(e,i)&&t++;return t}return e.length}function M(e){return!isNaN(parseFloat(e))&&isFinite(e)}function z(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:-1,t=document.querySelectorAll(".gbtn[data-taborder]:not(.disabled)");if(!t.length)return!1;if(1==t.length)return t[0];"string"==typeof e&&(e=parseInt(e));var i=[];o(t,(function(e){i.push(e.getAttribute("data-taborder"))}));var n=Math.max.apply(Math,i.map((function(e){return parseInt(e)}))),s=e<0?1:e+1;s>n&&(s="1");var l=i.filter((function(e){return e>=parseInt(s)})),r=l.sort()[0];return document.querySelector('.gbtn[data-taborder="'.concat(r,'"]'))}function X(e){if(e.events.hasOwnProperty("keyboard"))return!1;e.events.keyboard=a("keydown",{onElement:window,withCallback:function(t,i){var n=(t=t||window.event).keyCode;if(9==n){var s=document.querySelector(".gbtn.focused");if(!s){var l=!(!document.activeElement||!document.activeElement.nodeName)&&document.activeElement.nodeName.toLocaleLowerCase();if("input"==l||"textarea"==l||"button"==l)return}t.preventDefault();var o=document.querySelectorAll(".gbtn[data-taborder]");if(!o||o.length<=0)return;if(!s){var r=z();return void(r&&(r.focus(),h(r,"focused")))}var a=z(s.getAttribute("data-taborder"));d(s,"focused"),a&&(a.focus(),h(a,"focused"))}39==n&&e.nextSlide(),37==n&&e.prevSlide(),27==n&&e.close()}})}function Y(e){return Math.sqrt(e.x*e.x+e.y*e.y)}function q(e,t){var i=function(e,t){var i=Y(e)*Y(t);if(0===i)return 0;var n=function(e,t){return e.x*t.x+e.y*t.y}(e,t)/i;return n>1&&(n=1),Math.acos(n)}(e,t);return function(e,t){return e.x*t.y-t.x*e.y}(e,t)>0&&(i*=-1),180*i/Math.PI}var N=function(){function e(i){t(this,e),this.handlers=[],this.el=i}return n(e,[{key:"add",value:function(e){this.handlers.push(e)}},{key:"del",value:function(e){e||(this.handlers=[]);for(var t=this.handlers.length;t>=0;t--)this.handlers[t]===e&&this.handlers.splice(t,1)}},{key:"dispatch",value:function(){for(var e=0,t=this.handlers.length;e=0)console.log("ignore drag for this touched element",e.target.nodeName.toLowerCase());else{this.now=Date.now(),this.x1=e.touches[0].pageX,this.y1=e.touches[0].pageY,this.delta=this.now-(this.last||this.now),this.touchStart.dispatch(e,this.element),null!==this.preTapPosition.x&&(this.isDoubleTap=this.delta>0&&this.delta<=250&&Math.abs(this.preTapPosition.x-this.x1)<30&&Math.abs(this.preTapPosition.y-this.y1)<30,this.isDoubleTap&&clearTimeout(this.singleTapTimeout)),this.preTapPosition.x=this.x1,this.preTapPosition.y=this.y1,this.last=this.now;var t=this.preV;if(e.touches.length>1){this._cancelLongTap(),this._cancelSingleTap();var i={x:e.touches[1].pageX-this.x1,y:e.touches[1].pageY-this.y1};t.x=i.x,t.y=i.y,this.pinchStartLen=Y(t),this.multipointStart.dispatch(e,this.element)}this._preventTap=!1,this.longTapTimeout=setTimeout(function(){this.longTap.dispatch(e,this.element),this._preventTap=!0}.bind(this),750)}}}},{key:"move",value:function(e){if(e.touches){var t=this.preV,i=e.touches.length,n=e.touches[0].pageX,s=e.touches[0].pageY;if(this.isDoubleTap=!1,i>1){var l=e.touches[1].pageX,o=e.touches[1].pageY,r={x:e.touches[1].pageX-n,y:e.touches[1].pageY-s};null!==t.x&&(this.pinchStartLen>0&&(e.zoom=Y(r)/this.pinchStartLen,this.pinch.dispatch(e,this.element)),e.angle=q(r,t),this.rotate.dispatch(e,this.element)),t.x=r.x,t.y=r.y,null!==this.x2&&null!==this.sx2?(e.deltaX=(n-this.x2+l-this.sx2)/2,e.deltaY=(s-this.y2+o-this.sy2)/2):(e.deltaX=0,e.deltaY=0),this.twoFingerPressMove.dispatch(e,this.element),this.sx2=l,this.sy2=o}else{if(null!==this.x2){e.deltaX=n-this.x2,e.deltaY=s-this.y2;var a=Math.abs(this.x1-this.x2),h=Math.abs(this.y1-this.y2);(a>10||h>10)&&(this._preventTap=!0)}else e.deltaX=0,e.deltaY=0;this.pressMove.dispatch(e,this.element)}this.touchMove.dispatch(e,this.element),this._cancelLongTap(),this.x2=n,this.y2=s,i>1&&e.preventDefault()}}},{key:"end",value:function(e){if(e.changedTouches){this._cancelLongTap();var t=this;e.touches.length<2&&(this.multipointEnd.dispatch(e,this.element),this.sx2=this.sy2=null),this.x2&&Math.abs(this.x1-this.x2)>30||this.y2&&Math.abs(this.y1-this.y2)>30?(e.direction=this._swipeDirection(this.x1,this.x2,this.y1,this.y2),this.swipeTimeout=setTimeout((function(){t.swipe.dispatch(e,t.element)}),0)):(this.tapTimeout=setTimeout((function(){t._preventTap||t.tap.dispatch(e,t.element),t.isDoubleTap&&(t.doubleTap.dispatch(e,t.element),t.isDoubleTap=!1)}),0),t.isDoubleTap||(t.singleTapTimeout=setTimeout((function(){t.singleTap.dispatch(e,t.element)}),250))),this.touchEnd.dispatch(e,this.element),this.preV.x=0,this.preV.y=0,this.zoom=1,this.pinchStartLen=null,this.x1=this.x2=this.y1=this.y2=null}}},{key:"cancelAll",value:function(){this._preventTap=!0,clearTimeout(this.singleTapTimeout),clearTimeout(this.tapTimeout),clearTimeout(this.longTapTimeout),clearTimeout(this.swipeTimeout)}},{key:"cancel",value:function(e){this.cancelAll(),this.touchCancel.dispatch(e,this.element)}},{key:"_cancelLongTap",value:function(){clearTimeout(this.longTapTimeout)}},{key:"_cancelSingleTap",value:function(){clearTimeout(this.singleTapTimeout)}},{key:"_swipeDirection",value:function(e,t,i,n){return Math.abs(e-t)>=Math.abs(i-n)?e-t>0?"Left":"Right":i-n>0?"Up":"Down"}},{key:"on",value:function(e,t){this[e]&&this[e].add(t)}},{key:"off",value:function(e,t){this[e]&&this[e].del(t)}},{key:"destroy",value:function(){return this.singleTapTimeout&&clearTimeout(this.singleTapTimeout),this.tapTimeout&&clearTimeout(this.tapTimeout),this.longTapTimeout&&clearTimeout(this.longTapTimeout),this.swipeTimeout&&clearTimeout(this.swipeTimeout),this.element.removeEventListener("touchstart",this.start),this.element.removeEventListener("touchmove",this.move),this.element.removeEventListener("touchend",this.end),this.element.removeEventListener("touchcancel",this.cancel),this.rotate.del(),this.touchStart.del(),this.multipointStart.del(),this.multipointEnd.del(),this.pinch.del(),this.swipe.del(),this.tap.del(),this.doubleTap.del(),this.longTap.del(),this.singleTap.del(),this.pressMove.del(),this.twoFingerPressMove.del(),this.touchMove.del(),this.touchEnd.del(),this.touchCancel.del(),this.preV=this.pinchStartLen=this.zoom=this.isDoubleTap=this.delta=this.last=this.now=this.tapTimeout=this.singleTapTimeout=this.longTapTimeout=this.swipeTimeout=this.x1=this.x2=this.y1=this.y2=this.preTapPosition=this.rotate=this.touchStart=this.multipointStart=this.multipointEnd=this.pinch=this.swipe=this.tap=this.doubleTap=this.longTap=this.singleTap=this.pressMove=this.touchMove=this.touchEnd=this.touchCancel=this.twoFingerPressMove=null,window.removeEventListener("scroll",this._cancelAllHandler),null}}]),e}();function W(e){var t=function(){var e,t=document.createElement("fakeelement"),i={transition:"transitionend",OTransition:"oTransitionEnd",MozTransition:"transitionend",WebkitTransition:"webkitTransitionEnd"};for(e in i)if(void 0!==t.style[e])return i[e]}(),i=window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth,n=c(e,"gslide-media")?e:e.querySelector(".gslide-media"),s=u(n,".ginner-container"),l=e.querySelector(".gslide-description");i>769&&(n=s),h(n,"greset"),v(n,"translate3d(0, 0, 0)"),a(t,{onElement:n,once:!0,withCallback:function(e,t){d(n,"greset")}}),n.style.opacity="",l&&(l.style.opacity="")}function B(e){if(e.events.hasOwnProperty("touch"))return!1;var t,i,n,s=y(),l=s.width,o=s.height,r=!1,a=null,g=null,f=null,p=!1,m=1,x=1,b=!1,S=!1,w=null,T=null,C=null,k=null,E=0,A=0,L=!1,I=!1,O={},P={},M=0,z=0,X=document.getElementById("glightbox-slider"),Y=document.querySelector(".goverlay"),q=new _(X,{touchStart:function(t){if(r=!0,(c(t.targetTouches[0].target,"ginner-container")||u(t.targetTouches[0].target,".gslide-desc")||"a"==t.targetTouches[0].target.nodeName.toLowerCase())&&(r=!1),u(t.targetTouches[0].target,".gslide-inline")&&!c(t.targetTouches[0].target.parentNode,"gslide-inline")&&(r=!1),r){if(P=t.targetTouches[0],O.pageX=t.targetTouches[0].pageX,O.pageY=t.targetTouches[0].pageY,M=t.targetTouches[0].clientX,z=t.targetTouches[0].clientY,a=e.activeSlide,g=a.querySelector(".gslide-media"),n=a.querySelector(".gslide-inline"),f=null,c(g,"gslide-image")&&(f=g.querySelector("img")),(window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth)>769&&(g=a.querySelector(".ginner-container")),d(Y,"greset"),t.pageX>20&&t.pageXo){var a=O.pageX-P.pageX;if(Math.abs(a)<=13)return!1}p=!0;var h,d=s.targetTouches[0].clientX,c=s.targetTouches[0].clientY,u=M-d,m=z-c;if(Math.abs(u)>Math.abs(m)?(L=!1,I=!0):(I=!1,L=!0),t=P.pageX-O.pageX,E=100*t/l,i=P.pageY-O.pageY,A=100*i/o,L&&f&&(h=1-Math.abs(i)/o,Y.style.opacity=h,e.settings.touchFollowAxis&&(E=0)),I&&(h=1-Math.abs(t)/l,g.style.opacity=h,e.settings.touchFollowAxis&&(A=0)),!f)return v(g,"translate3d(".concat(E,"%, 0, 0)"));v(g,"translate3d(".concat(E,"%, ").concat(A,"%, 0)"))}},touchEnd:function(){if(r){if(p=!1,S||b)return C=w,void(k=T);var t=Math.abs(parseInt(A)),i=Math.abs(parseInt(E));if(!(t>29&&f))return t<29&&i<25?(h(Y,"greset"),Y.style.opacity=1,W(g)):void 0;e.close()}},multipointEnd:function(){setTimeout((function(){b=!1}),50)},multipointStart:function(){b=!0,m=x||1},pinch:function(e){if(!f||p)return!1;b=!0,f.scaleX=f.scaleY=m*e.zoom;var t=m*e.zoom;if(S=!0,t<=1)return S=!1,t=1,k=null,C=null,w=null,T=null,void f.setAttribute("style","");t>4.5&&(t=4.5),f.style.transform="scale3d(".concat(t,", ").concat(t,", 1)"),x=t},pressMove:function(e){if(S&&!b){var t=P.pageX-O.pageX,i=P.pageY-O.pageY;C&&(t+=C),k&&(i+=k),w=t,T=i;var n="translate3d(".concat(t,"px, ").concat(i,"px, 0)");x&&(n+=" scale3d(".concat(x,", ").concat(x,", 1)")),v(f,n)}},swipe:function(t){if(!S)if(b)b=!1;else{if("Left"==t.direction){if(e.index==e.elements.length-1)return W(g);e.nextSlide()}if("Right"==t.direction){if(0==e.index)return W(g);e.prevSlide()}}}});e.events.touch=q}var H=function(){function e(i,n){var s=this,l=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;if(t(this,e),this.img=i,this.slide=n,this.onclose=l,this.img.setZoomEvents)return!1;this.active=!1,this.zoomedIn=!1,this.dragging=!1,this.currentX=null,this.currentY=null,this.initialX=null,this.initialY=null,this.xOffset=0,this.yOffset=0,this.img.addEventListener("mousedown",(function(e){return s.dragStart(e)}),!1),this.img.addEventListener("mouseup",(function(e){return s.dragEnd(e)}),!1),this.img.addEventListener("mousemove",(function(e){return s.drag(e)}),!1),this.img.addEventListener("click",(function(e){return s.slide.classList.contains("dragging-nav")?(s.zoomOut(),!1):s.zoomedIn?void(s.zoomedIn&&!s.dragging&&s.zoomOut()):s.zoomIn()}),!1),this.img.setZoomEvents=!0}return n(e,[{key:"zoomIn",value:function(){var e=this.widowWidth();if(!(this.zoomedIn||e<=768)){var t=this.img;if(t.setAttribute("data-style",t.getAttribute("style")),t.style.maxWidth=t.naturalWidth+"px",t.style.maxHeight=t.naturalHeight+"px",t.naturalWidth>e){var i=e/2-t.naturalWidth/2;this.setTranslate(this.img.parentNode,i,0)}this.slide.classList.add("zoomed"),this.zoomedIn=!0}}},{key:"zoomOut",value:function(){this.img.parentNode.setAttribute("style",""),this.img.setAttribute("style",this.img.getAttribute("data-style")),this.slide.classList.remove("zoomed"),this.zoomedIn=!1,this.currentX=null,this.currentY=null,this.initialX=null,this.initialY=null,this.xOffset=0,this.yOffset=0,this.onclose&&"function"==typeof this.onclose&&this.onclose()}},{key:"dragStart",value:function(e){e.preventDefault(),this.zoomedIn?("touchstart"===e.type?(this.initialX=e.touches[0].clientX-this.xOffset,this.initialY=e.touches[0].clientY-this.yOffset):(this.initialX=e.clientX-this.xOffset,this.initialY=e.clientY-this.yOffset),e.target===this.img&&(this.active=!0,this.img.classList.add("dragging"))):this.active=!1}},{key:"dragEnd",value:function(e){var t=this;e.preventDefault(),this.initialX=this.currentX,this.initialY=this.currentY,this.active=!1,setTimeout((function(){t.dragging=!1,t.img.isDragging=!1,t.img.classList.remove("dragging")}),100)}},{key:"drag",value:function(e){this.active&&(e.preventDefault(),"touchmove"===e.type?(this.currentX=e.touches[0].clientX-this.initialX,this.currentY=e.touches[0].clientY-this.initialY):(this.currentX=e.clientX-this.initialX,this.currentY=e.clientY-this.initialY),this.xOffset=this.currentX,this.yOffset=this.currentY,this.img.isDragging=!0,this.dragging=!0,this.setTranslate(this.img,this.currentX,this.currentY))}},{key:"onMove",value:function(e){if(this.zoomedIn){var t=e.clientX-this.img.naturalWidth/2,i=e.clientY-this.img.naturalHeight/2;this.setTranslate(this.img,t,i)}}},{key:"setTranslate",value:function(e,t,i){e.style.transform="translate3d("+t+"px, "+i+"px, 0)"}},{key:"widowWidth",value:function(){return window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth}}]),e}(),V=function(){function e(){var i=this,n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,e);var s=n.dragEl,l=n.toleranceX,o=void 0===l?40:l,r=n.toleranceY,a=void 0===r?65:r,h=n.slide,d=void 0===h?null:h,c=n.instance,u=void 0===c?null:c;this.el=s,this.active=!1,this.dragging=!1,this.currentX=null,this.currentY=null,this.initialX=null,this.initialY=null,this.xOffset=0,this.yOffset=0,this.direction=null,this.lastDirection=null,this.toleranceX=o,this.toleranceY=a,this.toleranceReached=!1,this.dragContainer=this.el,this.slide=d,this.instance=u,this.el.addEventListener("mousedown",(function(e){return i.dragStart(e)}),!1),this.el.addEventListener("mouseup",(function(e){return i.dragEnd(e)}),!1),this.el.addEventListener("mousemove",(function(e){return i.drag(e)}),!1)}return n(e,[{key:"dragStart",value:function(e){if(this.slide.classList.contains("zoomed"))this.active=!1;else{"touchstart"===e.type?(this.initialX=e.touches[0].clientX-this.xOffset,this.initialY=e.touches[0].clientY-this.yOffset):(this.initialX=e.clientX-this.xOffset,this.initialY=e.clientY-this.yOffset);var t=e.target.nodeName.toLowerCase();e.target.classList.contains("nodrag")||u(e.target,".nodrag")||-1!==["input","select","textarea","button","a"].indexOf(t)?this.active=!1:(e.preventDefault(),(e.target===this.el||"img"!==t&&u(e.target,".gslide-inline"))&&(this.active=!0,this.el.classList.add("dragging"),this.dragContainer=u(e.target,".ginner-container")))}}},{key:"dragEnd",value:function(e){var t=this;e&&e.preventDefault(),this.initialX=0,this.initialY=0,this.currentX=null,this.currentY=null,this.initialX=null,this.initialY=null,this.xOffset=0,this.yOffset=0,this.active=!1,this.doSlideChange&&(this.instance.preventOutsideClick=!0,"right"==this.doSlideChange&&this.instance.prevSlide(),"left"==this.doSlideChange&&this.instance.nextSlide()),this.doSlideClose&&this.instance.close(),this.toleranceReached||this.setTranslate(this.dragContainer,0,0,!0),setTimeout((function(){t.instance.preventOutsideClick=!1,t.toleranceReached=!1,t.lastDirection=null,t.dragging=!1,t.el.isDragging=!1,t.el.classList.remove("dragging"),t.slide.classList.remove("dragging-nav"),t.dragContainer.style.transform="",t.dragContainer.style.transition=""}),100)}},{key:"drag",value:function(e){if(this.active){e.preventDefault(),this.slide.classList.add("dragging-nav"),"touchmove"===e.type?(this.currentX=e.touches[0].clientX-this.initialX,this.currentY=e.touches[0].clientY-this.initialY):(this.currentX=e.clientX-this.initialX,this.currentY=e.clientY-this.initialY),this.xOffset=this.currentX,this.yOffset=this.currentY,this.el.isDragging=!0,this.dragging=!0,this.doSlideChange=!1,this.doSlideClose=!1;var t=Math.abs(this.currentX),i=Math.abs(this.currentY);if(t>0&&t>=Math.abs(this.currentY)&&(!this.lastDirection||"x"==this.lastDirection)){this.yOffset=0,this.lastDirection="x",this.setTranslate(this.dragContainer,this.currentX,0);var n=this.shouldChange();if(!this.instance.settings.dragAutoSnap&&n&&(this.doSlideChange=n),this.instance.settings.dragAutoSnap&&n)return this.instance.preventOutsideClick=!0,this.toleranceReached=!0,this.active=!1,this.instance.preventOutsideClick=!0,this.dragEnd(null),"right"==n&&this.instance.prevSlide(),void("left"==n&&this.instance.nextSlide())}if(this.toleranceY>0&&i>0&&i>=t&&(!this.lastDirection||"y"==this.lastDirection)){this.xOffset=0,this.lastDirection="y",this.setTranslate(this.dragContainer,0,this.currentY);var s=this.shouldClose();return!this.instance.settings.dragAutoSnap&&s&&(this.doSlideClose=!0),void(this.instance.settings.dragAutoSnap&&s&&this.instance.close())}}}},{key:"shouldChange",value:function(){var e=!1;if(Math.abs(this.currentX)>=this.toleranceX){var t=this.currentX>0?"right":"left";("left"==t&&this.slide!==this.slide.parentNode.lastChild||"right"==t&&this.slide!==this.slide.parentNode.firstChild)&&(e=t)}return e}},{key:"shouldClose",value:function(){var e=!1;return Math.abs(this.currentY)>=this.toleranceY&&(e=!0),e}},{key:"setTranslate",value:function(e,t,i){var n=arguments.length>3&&void 0!==arguments[3]&&arguments[3];e.style.transition=n?"all .2s ease":"",e.style.transform="translate3d(".concat(t,"px, ").concat(i,"px, 0)")}}]),e}();function j(e,t,i,n){var s=e.querySelector(".gslide-media"),l=new Image,o="gSlideTitle_"+i,r="gSlideDesc_"+i;l.addEventListener("load",(function(){T(n)&&n()}),!1),l.src=t.href,""!=t.sizes&&""!=t.srcset&&(l.sizes=t.sizes,l.srcset=t.srcset),l.alt="",I(t.alt)||""===t.alt||(l.alt=t.alt),""!==t.title&&l.setAttribute("aria-labelledby",o),""!==t.description&&l.setAttribute("aria-describedby",r),t.hasOwnProperty("_hasCustomWidth")&&t._hasCustomWidth&&(l.style.width=t.width),t.hasOwnProperty("_hasCustomHeight")&&t._hasCustomHeight&&(l.style.height=t.height),s.insertBefore(l,s.firstChild)}function F(e,t,i,n){var s=this,l=e.querySelector(".ginner-container"),o="gvideo"+i,r=e.querySelector(".gslide-media"),a=this.getAllPlayers();h(l,"gvideo-container"),r.insertBefore(m('
'),r.firstChild);var d=e.querySelector(".gvideo-wrapper");S(this.settings.plyr.css,"Plyr");var c=t.href,u=location.protocol.replace(":",""),g="",v="",f=!1;"file"==u&&(u="http"),r.style.maxWidth=t.width,S(this.settings.plyr.js,"Plyr",(function(){if(c.match(/vimeo\.com\/([0-9]*)/)){var l=/vimeo.*\/(\d+)/i.exec(c);g="vimeo",v=l[1]}if(c.match(/(youtube\.com|youtube-nocookie\.com)\/watch\?v=([a-zA-Z0-9\-_]+)/)||c.match(/youtu\.be\/([a-zA-Z0-9\-_]+)/)||c.match(/(youtube\.com|youtube-nocookie\.com)\/embed\/([a-zA-Z0-9\-_]+)/)){var r=function(e){var t="";t=void 0!==(e=e.replace(/(>|<)/gi,"").split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/))[2]?(t=e[2].split(/[^0-9a-z_\-]/i))[0]:e;return t}(c);g="youtube",v=r}if(null!==c.match(/\.(mp4|ogg|webm|mov)$/)){g="local";var u='")}var w=f||m('
'));h(d,"".concat(g,"-video gvideo")),d.appendChild(w),d.setAttribute("data-id",o),d.setAttribute("data-index",i);var C=O(s.settings.plyr,"config")?s.settings.plyr.config:{},k=new Plyr("#"+o,C);k.on("ready",(function(e){var t=e.detail.plyr;a[o]=t,T(n)&&n()})),b((function(){return e.querySelector("iframe")&&"true"==e.querySelector("iframe").dataset.ready}),(function(){s.resize(e)})),k.on("enterfullscreen",R),k.on("exitfullscreen",R)}))}function R(e){var t=u(e.target,".gslide-media");"enterfullscreen"==e.type&&h(t,"fullscreen"),"exitfullscreen"==e.type&&d(t,"fullscreen")}function G(e,t,i,n){var s,l=this,o=e.querySelector(".gslide-media"),r=!(!O(t,"href")||!t.href)&&t.href.split("#").pop().trim(),d=!(!O(t,"content")||!t.content)&&t.content;if(d&&(C(d)&&(s=m('
'.concat(d,"
"))),k(d))){"none"==d.style.display&&(d.style.display="block");var c=document.createElement("div");c.className="ginlined-content",c.appendChild(d),s=c}if(r){var u=document.getElementById(r);if(!u)return!1;var g=u.cloneNode(!0);g.style.height=t.height,g.style.maxWidth=t.width,h(g,"ginlined-content"),s=g}if(!s)return console.error("Unable to append inline slide content",t),!1;o.style.height=t.height,o.style.width=t.width,o.appendChild(s),this.events["inlineclose"+r]=a("click",{onElement:o.querySelectorAll(".gtrigger-close"),withCallback:function(e){e.preventDefault(),l.close()}}),T(n)&&n()}function Z(e,t,i,n){var s=e.querySelector(".gslide-media"),l=function(e){var t=e.url,i=e.allow,n=e.callback,s=e.appendTo,l=document.createElement("iframe");return l.className="vimeo-video gvideo",l.src=t,l.style.width="100%",l.style.height="100%",i&&l.setAttribute("allow",i),l.onload=function(){h(l,"node-ready"),T(n)&&n()},s&&s.appendChild(l),l}({url:t.href,callback:n});s.parentNode.style.maxWidth=t.width,s.parentNode.style.height=t.height,s.appendChild(l)}var $=function(){function e(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,e),this.defaults={href:"",sizes:"",srcset:"",title:"",type:"",description:"",alt:"",descPosition:"bottom",effect:"",width:"",height:"",content:!1,zoomable:!0,draggable:!0},L(i)&&(this.defaults=l(this.defaults,i))}return n(e,[{key:"sourceType",value:function(e){var t=e;if(null!==(e=e.toLowerCase()).match(/\.(jpeg|jpg|jpe|gif|png|apn|webp|avif|svg)/))return"image";if(e.match(/(youtube\.com|youtube-nocookie\.com)\/watch\?v=([a-zA-Z0-9\-_]+)/)||e.match(/youtu\.be\/([a-zA-Z0-9\-_]+)/)||e.match(/(youtube\.com|youtube-nocookie\.com)\/embed\/([a-zA-Z0-9\-_]+)/))return"video";if(e.match(/vimeo\.com\/([0-9]*)/))return"video";if(null!==e.match(/\.(mp4|ogg|webm|mov)/))return"video";if(null!==e.match(/\.(mp3|wav|wma|aac|ogg)/))return"audio";if(e.indexOf("#")>-1&&""!==t.split("#").pop().trim())return"inline";return e.indexOf("goajax=true")>-1?"ajax":"external"}},{key:"parseConfig",value:function(e,t){var i=this,n=l({descPosition:t.descPosition},this.defaults);if(L(e)&&!k(e)){O(e,"type")||(O(e,"content")&&e.content?e.type="inline":O(e,"href")&&(e.type=this.sourceType(e.href)));var s=l(n,e);return this.setSize(s,t),s}var r="",a=e.getAttribute("data-glightbox"),h=e.nodeName.toLowerCase();if("a"===h&&(r=e.href),"img"===h&&(r=e.src,n.alt=e.alt),n.href=r,o(n,(function(s,l){O(t,l)&&"width"!==l&&(n[l]=t[l]);var o=e.dataset[l];I(o)||(n[l]=i.sanitizeValue(o))})),n.content&&(n.type="inline"),!n.type&&r&&(n.type=this.sourceType(r)),I(a)){if(!n.title&&"a"==h){var d=e.title;I(d)||""===d||(n.title=d)}if(!n.title&&"img"==h){var c=e.alt;I(c)||""===c||(n.title=c)}}else{var u=[];o(n,(function(e,t){u.push(";\\s?"+t)})),u=u.join("\\s?:|"),""!==a.trim()&&o(n,(function(e,t){var s=a,l=new RegExp("s?"+t+"s?:s?(.*?)("+u+"s?:|$)"),o=s.match(l);if(o&&o.length&&o[1]){var r=o[1].trim().replace(/;\s*$/,"");n[t]=i.sanitizeValue(r)}}))}if(n.description&&"."===n.description.substring(0,1)){var g;try{g=document.querySelector(n.description).innerHTML}catch(e){if(!(e instanceof DOMException))throw e}g&&(n.description=g)}if(!n.description){var v=e.querySelector(".glightbox-desc");v&&(n.description=v.innerHTML)}return this.setSize(n,t,e),this.slideConfig=n,n}},{key:"setSize",value:function(e,t){var i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null,n="video"==e.type?this.checkSize(t.videosWidth):this.checkSize(t.width),s=this.checkSize(t.height);return e.width=O(e,"width")&&""!==e.width?this.checkSize(e.width):n,e.height=O(e,"height")&&""!==e.height?this.checkSize(e.height):s,i&&"image"==e.type&&(e._hasCustomWidth=!!i.dataset.width,e._hasCustomHeight=!!i.dataset.height),e}},{key:"checkSize",value:function(e){return M(e)?"".concat(e,"px"):e}},{key:"sanitizeValue",value:function(e){return"true"!==e&&"false"!==e?e:"true"===e}}]),e}(),U=function(){function e(i,n,s){t(this,e),this.element=i,this.instance=n,this.index=s}return n(e,[{key:"setContent",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,i=arguments.length>1&&void 0!==arguments[1]&&arguments[1];if(c(t,"loaded"))return!1;var n=this.instance.settings,s=this.slideConfig,l=w();T(n.beforeSlideLoad)&&n.beforeSlideLoad({index:this.index,slide:t,player:!1});var o=s.type,r=s.descPosition,a=t.querySelector(".gslide-media"),d=t.querySelector(".gslide-title"),u=t.querySelector(".gslide-desc"),g=t.querySelector(".gdesc-inner"),v=i,f="gSlideTitle_"+this.index,p="gSlideDesc_"+this.index;if(T(n.afterSlideLoad)&&(v=function(){T(i)&&i(),n.afterSlideLoad({index:e.index,slide:t,player:e.instance.getSlidePlayerInstance(e.index)})}),""==s.title&&""==s.description?g&&g.parentNode.parentNode.removeChild(g.parentNode):(d&&""!==s.title?(d.id=f,d.innerHTML=s.title):d.parentNode.removeChild(d),u&&""!==s.description?(u.id=p,l&&n.moreLength>0?(s.smallDescription=this.slideShortDesc(s.description,n.moreLength,n.moreText),u.innerHTML=s.smallDescription,this.descriptionEvents(u,s)):u.innerHTML=s.description):u.parentNode.removeChild(u),h(a.parentNode,"desc-".concat(r)),h(g.parentNode,"description-".concat(r))),h(a,"gslide-".concat(o)),h(t,"loaded"),"video"!==o){if("external"!==o)return"inline"===o?(G.apply(this.instance,[t,s,this.index,v]),void(s.draggable&&new V({dragEl:t.querySelector(".gslide-inline"),toleranceX:n.dragToleranceX,toleranceY:n.dragToleranceY,slide:t,instance:this.instance}))):void("image"!==o?T(v)&&v():j(t,s,this.index,(function(){var i=t.querySelector("img");s.draggable&&new V({dragEl:i,toleranceX:n.dragToleranceX,toleranceY:n.dragToleranceY,slide:t,instance:e.instance}),s.zoomable&&i.naturalWidth>i.offsetWidth&&(h(i,"zoomable"),new H(i,t,(function(){e.instance.resize()}))),T(v)&&v()})));Z.apply(this,[t,s,this.index,v])}else F.apply(this.instance,[t,s,this.index,v])}},{key:"slideShortDesc",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:50,i=arguments.length>2&&void 0!==arguments[2]&&arguments[2],n=document.createElement("div");n.innerHTML=e;var s=n.innerText,l=i;if((e=s.trim()).length<=t)return e;var o=e.substr(0,t-1);return l?(n=null,o+'... '+i+""):o}},{key:"descriptionEvents",value:function(e,t){var i=this,n=e.querySelector(".desc-more");if(!n)return!1;a("click",{onElement:n,withCallback:function(e,n){e.preventDefault();var s=document.body,l=u(n,".gslide-desc");if(!l)return!1;l.innerHTML=t.description,h(s,"gdesc-open");var o=a("click",{onElement:[s,u(l,".gslide-description")],withCallback:function(e,n){"a"!==e.target.nodeName.toLowerCase()&&(d(s,"gdesc-open"),h(s,"gdesc-closed"),l.innerHTML=t.smallDescription,i.descriptionEvents(l,t),setTimeout((function(){d(s,"gdesc-closed")}),400),o.destroy())}})}})}},{key:"create",value:function(){return m(this.instance.settings.slideHTML)}},{key:"getConfig",value:function(){k(this.element)||this.element.hasOwnProperty("draggable")||(this.element.draggable=this.instance.settings.draggable);var e=new $(this.instance.settings.slideExtraAttributes);return this.slideConfig=e.parseConfig(this.element,this.instance.settings),this.slideConfig}}]),e}(),J=w(),K=null!==w()||void 0!==document.createTouch||"ontouchstart"in window||"onmsgesturechange"in window||navigator.msMaxTouchPoints,Q=document.getElementsByTagName("html")[0],ee={selector:".glightbox",elements:null,skin:"clean",theme:"clean",closeButton:!0,startAt:null,autoplayVideos:!0,autofocusVideos:!0,descPosition:"bottom",width:"900px",height:"506px",videosWidth:"960px",beforeSlideChange:null,afterSlideChange:null,beforeSlideLoad:null,afterSlideLoad:null,slideInserted:null,slideRemoved:null,slideExtraAttributes:null,onOpen:null,onClose:null,loop:!1,zoomable:!0,draggable:!0,dragAutoSnap:!1,dragToleranceX:40,dragToleranceY:65,preload:!0,oneSlidePerOpen:!1,touchNavigation:!0,touchFollowAxis:!0,keyboardNavigation:!0,closeOnOutsideClick:!0,plugins:!1,plyr:{css:"https://cdn.plyr.io/3.6.8/plyr.css",js:"https://cdn.plyr.io/3.6.8/plyr.js",config:{ratio:"16:9",fullscreen:{enabled:!0,iosNative:!0},youtube:{noCookie:!0,rel:0,showinfo:0,iv_load_policy:3},vimeo:{byline:!1,portrait:!1,title:!1,transparent:!1}}},openEffect:"zoom",closeEffect:"zoom",slideEffect:"slide",moreText:"See more",moreLength:60,cssEfects:{fade:{in:"fadeIn",out:"fadeOut"},zoom:{in:"zoomIn",out:"zoomOut"},slide:{in:"slideInRight",out:"slideOutLeft"},slideBack:{in:"slideInLeft",out:"slideOutRight"},none:{in:"none",out:"none"}},svg:{close:'',next:' ',prev:''},slideHTML:'
\n
\n
\n
\n
\n
\n
\n

\n
\n
\n
\n
\n
\n
',lightboxHTML:''},te=function(){function e(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,e),this.customOptions=i,this.settings=l(ee,i),this.effectsClasses=this.getAnimationClasses(),this.videoPlayers={},this.apiEvents=[],this.fullElementsList=!1}return n(e,[{key:"init",value:function(){var e=this,t=this.getSelector();t&&(this.baseEvents=a("click",{onElement:t,withCallback:function(t,i){t.preventDefault(),e.open(i)}})),this.elements=this.getElements()}},{key:"open",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;if(0==this.elements.length)return!1;this.activeSlide=null,this.prevActiveSlideIndex=null,this.prevActiveSlide=null;var i=M(t)?t:this.settings.startAt;if(k(e)){var n=e.getAttribute("data-gallery");n&&(this.fullElementsList=this.elements,this.elements=this.getGalleryElements(this.elements,n)),I(i)&&(i=this.getElementIndex(e))<0&&(i=0)}M(i)||(i=0),this.build(),g(this.overlay,"none"==this.settings.openEffect?"none":this.settings.cssEfects.fade.in);var s=document.body,l=window.innerWidth-document.documentElement.clientWidth;if(l>0){var o=document.createElement("style");o.type="text/css",o.className="gcss-styles",o.innerText=".gscrollbar-fixer {margin-right: ".concat(l,"px}"),document.head.appendChild(o),h(s,"gscrollbar-fixer")}h(s,"glightbox-open"),h(Q,"glightbox-open"),J&&(h(document.body,"glightbox-mobile"),this.settings.slideEffect="slide"),this.showSlide(i,!0),1==this.elements.length?(h(this.prevButton,"glightbox-button-hidden"),h(this.nextButton,"glightbox-button-hidden")):(d(this.prevButton,"glightbox-button-hidden"),d(this.nextButton,"glightbox-button-hidden")),this.lightboxOpen=!0,this.trigger("open"),T(this.settings.onOpen)&&this.settings.onOpen(),K&&this.settings.touchNavigation&&B(this),this.settings.keyboardNavigation&&X(this)}},{key:"openAt",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;this.open(null,e)}},{key:"showSlide",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,i=arguments.length>1&&void 0!==arguments[1]&&arguments[1];f(this.loader),this.index=parseInt(t);var n=this.slidesContainer.querySelector(".current");n&&d(n,"current"),this.slideAnimateOut();var s=this.slidesContainer.querySelectorAll(".gslide")[t];if(c(s,"loaded"))this.slideAnimateIn(s,i),p(this.loader);else{f(this.loader);var l=this.elements[t],o={index:this.index,slide:s,slideNode:s,slideConfig:l.slideConfig,slideIndex:this.index,trigger:l.node,player:null};this.trigger("slide_before_load",o),l.instance.setContent(s,(function(){p(e.loader),e.resize(),e.slideAnimateIn(s,i),e.trigger("slide_after_load",o)}))}this.slideDescription=s.querySelector(".gslide-description"),this.slideDescriptionContained=this.slideDescription&&c(this.slideDescription.parentNode,"gslide-media"),this.settings.preload&&(this.preloadSlide(t+1),this.preloadSlide(t-1)),this.updateNavigationClasses(),this.activeSlide=s}},{key:"preloadSlide",value:function(e){var t=this;if(e<0||e>this.elements.length-1)return!1;if(I(this.elements[e]))return!1;var i=this.slidesContainer.querySelectorAll(".gslide")[e];if(c(i,"loaded"))return!1;var n=this.elements[e],s=n.type,l={index:e,slide:i,slideNode:i,slideConfig:n.slideConfig,slideIndex:e,trigger:n.node,player:null};this.trigger("slide_before_load",l),"video"==s||"external"==s?setTimeout((function(){n.instance.setContent(i,(function(){t.trigger("slide_after_load",l)}))}),200):n.instance.setContent(i,(function(){t.trigger("slide_after_load",l)}))}},{key:"prevSlide",value:function(){this.goToSlide(this.index-1)}},{key:"nextSlide",value:function(){this.goToSlide(this.index+1)}},{key:"goToSlide",value:function(){var e=arguments.length>0&&void 0!==arguments[0]&&arguments[0];if(this.prevActiveSlide=this.activeSlide,this.prevActiveSlideIndex=this.index,!this.loop()&&(e<0||e>this.elements.length-1))return!1;e<0?e=this.elements.length-1:e>=this.elements.length&&(e=0),this.showSlide(e)}},{key:"insertSlide",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:-1;t<0&&(t=this.elements.length);var i=new U(e,this,t),n=i.getConfig(),s=l({},n),o=i.create(),r=this.elements.length-1;s.index=t,s.node=!1,s.instance=i,s.slideConfig=n,this.elements.splice(t,0,s);var a=null,h=null;if(this.slidesContainer){if(t>r)this.slidesContainer.appendChild(o);else{var d=this.slidesContainer.querySelectorAll(".gslide")[t];this.slidesContainer.insertBefore(o,d)}(this.settings.preload&&0==this.index&&0==t||this.index-1==t||this.index+1==t)&&this.preloadSlide(t),0==this.index&&0==t&&(this.index=1),this.updateNavigationClasses(),a=this.slidesContainer.querySelectorAll(".gslide")[t],h=this.getSlidePlayerInstance(t),s.slideNode=a}this.trigger("slide_inserted",{index:t,slide:a,slideNode:a,slideConfig:n,slideIndex:t,trigger:null,player:h}),T(this.settings.slideInserted)&&this.settings.slideInserted({index:t,slide:a,player:h})}},{key:"removeSlide",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:-1;if(e<0||e>this.elements.length-1)return!1;var t=this.slidesContainer&&this.slidesContainer.querySelectorAll(".gslide")[e];t&&(this.getActiveSlideIndex()==e&&(e==this.elements.length-1?this.prevSlide():this.nextSlide()),t.parentNode.removeChild(t)),this.elements.splice(e,1),this.trigger("slide_removed",e),T(this.settings.slideRemoved)&&this.settings.slideRemoved(e)}},{key:"slideAnimateIn",value:function(e,t){var i=this,n=e.querySelector(".gslide-media"),s=e.querySelector(".gslide-description"),l={index:this.prevActiveSlideIndex,slide:this.prevActiveSlide,slideNode:this.prevActiveSlide,slideIndex:this.prevActiveSlide,slideConfig:I(this.prevActiveSlideIndex)?null:this.elements[this.prevActiveSlideIndex].slideConfig,trigger:I(this.prevActiveSlideIndex)?null:this.elements[this.prevActiveSlideIndex].node,player:this.getSlidePlayerInstance(this.prevActiveSlideIndex)},o={index:this.index,slide:this.activeSlide,slideNode:this.activeSlide,slideConfig:this.elements[this.index].slideConfig,slideIndex:this.index,trigger:this.elements[this.index].node,player:this.getSlidePlayerInstance(this.index)};if(n.offsetWidth>0&&s&&(p(s),s.style.display=""),d(e,this.effectsClasses),t)g(e,this.settings.cssEfects[this.settings.openEffect].in,(function(){i.settings.autoplayVideos&&i.slidePlayerPlay(e),i.trigger("slide_changed",{prev:l,current:o}),T(i.settings.afterSlideChange)&&i.settings.afterSlideChange.apply(i,[l,o])}));else{var r=this.settings.slideEffect,a="none"!==r?this.settings.cssEfects[r].in:r;this.prevActiveSlideIndex>this.index&&"slide"==this.settings.slideEffect&&(a=this.settings.cssEfects.slideBack.in),g(e,a,(function(){i.settings.autoplayVideos&&i.slidePlayerPlay(e),i.trigger("slide_changed",{prev:l,current:o}),T(i.settings.afterSlideChange)&&i.settings.afterSlideChange.apply(i,[l,o])}))}setTimeout((function(){i.resize(e)}),100),h(e,"current")}},{key:"slideAnimateOut",value:function(){if(!this.prevActiveSlide)return!1;var e=this.prevActiveSlide;d(e,this.effectsClasses),h(e,"prev");var t=this.settings.slideEffect,i="none"!==t?this.settings.cssEfects[t].out:t;this.slidePlayerPause(e),this.trigger("slide_before_change",{prev:{index:this.prevActiveSlideIndex,slide:this.prevActiveSlide,slideNode:this.prevActiveSlide,slideIndex:this.prevActiveSlideIndex,slideConfig:I(this.prevActiveSlideIndex)?null:this.elements[this.prevActiveSlideIndex].slideConfig,trigger:I(this.prevActiveSlideIndex)?null:this.elements[this.prevActiveSlideIndex].node,player:this.getSlidePlayerInstance(this.prevActiveSlideIndex)},current:{index:this.index,slide:this.activeSlide,slideNode:this.activeSlide,slideIndex:this.index,slideConfig:this.elements[this.index].slideConfig,trigger:this.elements[this.index].node,player:this.getSlidePlayerInstance(this.index)}}),T(this.settings.beforeSlideChange)&&this.settings.beforeSlideChange.apply(this,[{index:this.prevActiveSlideIndex,slide:this.prevActiveSlide,player:this.getSlidePlayerInstance(this.prevActiveSlideIndex)},{index:this.index,slide:this.activeSlide,player:this.getSlidePlayerInstance(this.index)}]),this.prevActiveSlideIndex>this.index&&"slide"==this.settings.slideEffect&&(i=this.settings.cssEfects.slideBack.out),g(e,i,(function(){var t=e.querySelector(".ginner-container"),i=e.querySelector(".gslide-media"),n=e.querySelector(".gslide-description");t.style.transform="",i.style.transform="",d(i,"greset"),i.style.opacity="",n&&(n.style.opacity=""),d(e,"prev")}))}},{key:"getAllPlayers",value:function(){return this.videoPlayers}},{key:"getSlidePlayerInstance",value:function(e){var t="gvideo"+e,i=this.getAllPlayers();return!(!O(i,t)||!i[t])&&i[t]}},{key:"stopSlideVideo",value:function(e){if(k(e)){var t=e.querySelector(".gvideo-wrapper");t&&(e=t.getAttribute("data-index"))}console.log("stopSlideVideo is deprecated, use slidePlayerPause");var i=this.getSlidePlayerInstance(e);i&&i.playing&&i.pause()}},{key:"slidePlayerPause",value:function(e){if(k(e)){var t=e.querySelector(".gvideo-wrapper");t&&(e=t.getAttribute("data-index"))}var i=this.getSlidePlayerInstance(e);i&&i.playing&&i.pause()}},{key:"playSlideVideo",value:function(e){if(k(e)){var t=e.querySelector(".gvideo-wrapper");t&&(e=t.getAttribute("data-index"))}console.log("playSlideVideo is deprecated, use slidePlayerPlay");var i=this.getSlidePlayerInstance(e);i&&!i.playing&&i.play()}},{key:"slidePlayerPlay",value:function(e){if(k(e)){var t=e.querySelector(".gvideo-wrapper");t&&(e=t.getAttribute("data-index"))}var i=this.getSlidePlayerInstance(e);i&&!i.playing&&(i.play(),this.settings.autofocusVideos&&i.elements.container.focus())}},{key:"setElements",value:function(e){var t=this;this.settings.elements=!1;var i=[];e&&e.length&&o(e,(function(e,n){var s=new U(e,t,n),o=s.getConfig(),r=l({},o);r.slideConfig=o,r.instance=s,r.index=n,i.push(r)})),this.elements=i,this.lightboxOpen&&(this.slidesContainer.innerHTML="",this.elements.length&&(o(this.elements,(function(){var e=m(t.settings.slideHTML);t.slidesContainer.appendChild(e)})),this.showSlide(0,!0)))}},{key:"getElementIndex",value:function(e){var t=!1;return o(this.elements,(function(i,n){if(O(i,"node")&&i.node==e)return t=n,!0})),t}},{key:"getElements",value:function(){var e=this,t=[];this.elements=this.elements?this.elements:[],!I(this.settings.elements)&&E(this.settings.elements)&&this.settings.elements.length&&o(this.settings.elements,(function(i,n){var s=new U(i,e,n),o=s.getConfig(),r=l({},o);r.node=!1,r.index=n,r.instance=s,r.slideConfig=o,t.push(r)}));var i=!1;return this.getSelector()&&(i=document.querySelectorAll(this.getSelector())),i?(o(i,(function(i,n){var s=new U(i,e,n),o=s.getConfig(),r=l({},o);r.node=i,r.index=n,r.instance=s,r.slideConfig=o,r.gallery=i.getAttribute("data-gallery"),t.push(r)})),t):t}},{key:"getGalleryElements",value:function(e,t){return e.filter((function(e){return e.gallery==t}))}},{key:"getSelector",value:function(){return!this.settings.elements&&(this.settings.selector&&"data-"==this.settings.selector.substring(0,5)?"*[".concat(this.settings.selector,"]"):this.settings.selector)}},{key:"getActiveSlide",value:function(){return this.slidesContainer.querySelectorAll(".gslide")[this.index]}},{key:"getActiveSlideIndex",value:function(){return this.index}},{key:"getAnimationClasses",value:function(){var e=[];for(var t in this.settings.cssEfects)if(this.settings.cssEfects.hasOwnProperty(t)){var i=this.settings.cssEfects[t];e.push("g".concat(i.in)),e.push("g".concat(i.out))}return e.join(" ")}},{key:"build",value:function(){var e=this;if(this.built)return!1;var t=document.body.childNodes,i=[];o(t,(function(e){e.parentNode==document.body&&"#"!==e.nodeName.charAt(0)&&e.hasAttribute&&!e.hasAttribute("aria-hidden")&&(i.push(e),e.setAttribute("aria-hidden","true"))}));var n=O(this.settings.svg,"next")?this.settings.svg.next:"",s=O(this.settings.svg,"prev")?this.settings.svg.prev:"",l=O(this.settings.svg,"close")?this.settings.svg.close:"",r=this.settings.lightboxHTML;r=m(r=(r=(r=r.replace(/{nextSVG}/g,n)).replace(/{prevSVG}/g,s)).replace(/{closeSVG}/g,l)),document.body.appendChild(r);var d=document.getElementById("glightbox-body");this.modal=d;var g=d.querySelector(".gclose");this.prevButton=d.querySelector(".gprev"),this.nextButton=d.querySelector(".gnext"),this.overlay=d.querySelector(".goverlay"),this.loader=d.querySelector(".gloader"),this.slidesContainer=document.getElementById("glightbox-slider"),this.bodyHiddenChildElms=i,this.events={},h(this.modal,"glightbox-"+this.settings.skin),this.settings.closeButton&&g&&(this.events.close=a("click",{onElement:g,withCallback:function(t,i){t.preventDefault(),e.close()}})),g&&!this.settings.closeButton&&g.parentNode.removeChild(g),this.nextButton&&(this.events.next=a("click",{onElement:this.nextButton,withCallback:function(t,i){t.preventDefault(),e.nextSlide()}})),this.prevButton&&(this.events.prev=a("click",{onElement:this.prevButton,withCallback:function(t,i){t.preventDefault(),e.prevSlide()}})),this.settings.closeOnOutsideClick&&(this.events.outClose=a("click",{onElement:d,withCallback:function(t,i){e.preventOutsideClick||c(document.body,"glightbox-mobile")||u(t.target,".ginner-container")||u(t.target,".gbtn")||c(t.target,"gnext")||c(t.target,"gprev")||e.close()}})),o(this.elements,(function(t,i){e.slidesContainer.appendChild(t.instance.create()),t.slideNode=e.slidesContainer.querySelectorAll(".gslide")[i]})),K&&h(document.body,"glightbox-touch"),this.events.resize=a("resize",{onElement:window,withCallback:function(){e.resize()}}),this.built=!0}},{key:"resize",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;if((e=e||this.activeSlide)&&!c(e,"zoomed")){var t=y(),i=e.querySelector(".gvideo-wrapper"),n=e.querySelector(".gslide-image"),s=this.slideDescription,l=t.width,o=t.height;if(l<=768?h(document.body,"glightbox-mobile"):d(document.body,"glightbox-mobile"),i||n){var r=!1;if(s&&(c(s,"description-bottom")||c(s,"description-top"))&&!c(s,"gabsolute")&&(r=!0),n)if(l<=768)n.querySelector("img");else if(r){var a=s.offsetHeight,u=n.querySelector("img");u.setAttribute("style","max-height: calc(100vh - ".concat(a,"px)")),s.setAttribute("style","max-width: ".concat(u.offsetWidth,"px;"))}if(i){var g=O(this.settings.plyr.config,"ratio")?this.settings.plyr.config.ratio:"";if(!g){var v=i.clientWidth,f=i.clientHeight,p=v/f;g="".concat(v/p,":").concat(f/p)}var m=g.split(":"),x=this.settings.videosWidth,b=this.settings.videosWidth,S=(b=M(x)||-1!==x.indexOf("px")?parseInt(x):-1!==x.indexOf("vw")?l*parseInt(x)/100:-1!==x.indexOf("vh")?o*parseInt(x)/100:-1!==x.indexOf("%")?l*parseInt(x)/100:parseInt(i.clientWidth))/(parseInt(m[0])/parseInt(m[1]));if(S=Math.floor(S),r&&(o-=s.offsetHeight),b>l||S>o||ob){var w=i.offsetWidth,T=i.offsetHeight,C=o/T,k={width:w*C,height:T*C};i.parentNode.setAttribute("style","max-width: ".concat(k.width,"px")),r&&s.setAttribute("style","max-width: ".concat(k.width,"px;"))}else i.parentNode.style.maxWidth="".concat(x),r&&s.setAttribute("style","max-width: ".concat(x,";"))}}}}},{key:"reload",value:function(){this.init()}},{key:"updateNavigationClasses",value:function(){var e=this.loop();d(this.nextButton,"disabled"),d(this.prevButton,"disabled"),0==this.index&&this.elements.length-1==0?(h(this.prevButton,"disabled"),h(this.nextButton,"disabled")):0!==this.index||e?this.index!==this.elements.length-1||e||h(this.nextButton,"disabled"):h(this.prevButton,"disabled")}},{key:"loop",value:function(){var e=O(this.settings,"loopAtEnd")?this.settings.loopAtEnd:null;return e=O(this.settings,"loop")?this.settings.loop:e,e}},{key:"close",value:function(){var e=this;if(!this.lightboxOpen){if(this.events){for(var t in this.events)this.events.hasOwnProperty(t)&&this.events[t].destroy();this.events=null}return!1}if(this.closing)return!1;this.closing=!0,this.slidePlayerPause(this.activeSlide),this.fullElementsList&&(this.elements=this.fullElementsList),this.bodyHiddenChildElms.length&&o(this.bodyHiddenChildElms,(function(e){e.removeAttribute("aria-hidden")})),h(this.modal,"glightbox-closing"),g(this.overlay,"none"==this.settings.openEffect?"none":this.settings.cssEfects.fade.out),g(this.activeSlide,this.settings.cssEfects[this.settings.closeEffect].out,(function(){if(e.activeSlide=null,e.prevActiveSlideIndex=null,e.prevActiveSlide=null,e.built=!1,e.events){for(var t in e.events)e.events.hasOwnProperty(t)&&e.events[t].destroy();e.events=null}var i=document.body;d(Q,"glightbox-open"),d(i,"glightbox-open touching gdesc-open glightbox-touch glightbox-mobile gscrollbar-fixer"),e.modal.parentNode.removeChild(e.modal),e.trigger("close"),T(e.settings.onClose)&&e.settings.onClose();var n=document.querySelector(".gcss-styles");n&&n.parentNode.removeChild(n),e.lightboxOpen=!1,e.closing=null}))}},{key:"destroy",value:function(){this.close(),this.clearAllEvents(),this.baseEvents&&this.baseEvents.destroy()}},{key:"on",value:function(e,t){var i=arguments.length>2&&void 0!==arguments[2]&&arguments[2];if(!e||!T(t))throw new TypeError("Event name and callback must be defined");this.apiEvents.push({evt:e,once:i,callback:t})}},{key:"once",value:function(e,t){this.on(e,t,!0)}},{key:"trigger",value:function(e){var t=this,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,n=[];o(this.apiEvents,(function(t,s){var l=t.evt,o=t.once,r=t.callback;l==e&&(r(i),o&&n.push(s))})),n.length&&o(n,(function(e){return t.apiEvents.splice(e,1)}))}},{key:"clearAllEvents",value:function(){this.apiEvents.splice(0,this.apiEvents.length)}},{key:"version",value:function(){return"3.1.1"}}]),e}();return function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=new te(e);return t.init(),t}})); -/*! update cache: 20260104155727 */ +/*! update cache: 20260104161427 */ diff --git a/assets/stylesheets/glightbox.min.css b/assets/stylesheets/glightbox.min.css index 6af1b5f72..23820f2a7 100644 --- a/assets/stylesheets/glightbox.min.css +++ b/assets/stylesheets/glightbox.min.css @@ -1,2 +1,2 @@ .glightbox-container{width:100%;height:100%;position:fixed;top:0;left:0;z-index:999999!important;overflow:hidden;-ms-touch-action:none;touch-action:none;-webkit-text-size-adjust:100%;-moz-text-size-adjust:100%;-ms-text-size-adjust:100%;text-size-adjust:100%;-webkit-backface-visibility:hidden;backface-visibility:hidden;outline:0}.glightbox-container.inactive{display:none}.glightbox-container .gcontainer{position:relative;width:100%;height:100%;z-index:9999;overflow:hidden}.glightbox-container .gslider{-webkit-transition:-webkit-transform .4s ease;transition:-webkit-transform .4s ease;transition:transform .4s ease;transition:transform .4s ease,-webkit-transform .4s ease;height:100%;left:0;top:0;width:100%;position:relative;overflow:hidden;display:-webkit-box!important;display:-ms-flexbox!important;display:flex!important;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}.glightbox-container .gslide{width:100%;position:absolute;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;opacity:0}.glightbox-container .gslide.current{opacity:1;z-index:99999;position:relative}.glightbox-container .gslide.prev{opacity:1;z-index:9999}.glightbox-container .gslide-inner-content{width:100%}.glightbox-container .ginner-container{position:relative;width:100%;display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column;max-width:100%;margin:auto;height:100vh}.glightbox-container .ginner-container.gvideo-container{width:100%}.glightbox-container .ginner-container.desc-bottom,.glightbox-container .ginner-container.desc-top{-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column}.glightbox-container .ginner-container.desc-left,.glightbox-container .ginner-container.desc-right{max-width:100%!important}.gslide iframe,.gslide video{outline:0!important;border:none;min-height:165px;-webkit-overflow-scrolling:touch;-ms-touch-action:auto;touch-action:auto}.gslide:not(.current){pointer-events:none}.gslide-image{-webkit-box-align:center;-ms-flex-align:center;align-items:center}.gslide-image img{max-height:100vh;display:block;padding:0;float:none;outline:0;border:none;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;max-width:100vw;width:auto;height:auto;-o-object-fit:cover;object-fit:cover;-ms-touch-action:none;touch-action:none;margin:auto;min-width:200px}.desc-bottom .gslide-image img,.desc-top .gslide-image img{width:auto}.desc-left .gslide-image img,.desc-right .gslide-image img{width:auto;max-width:100%}.gslide-image img.zoomable{position:relative}.gslide-image img.dragging{cursor:-webkit-grabbing!important;cursor:grabbing!important;-webkit-transition:none;transition:none}.gslide-video{position:relative;max-width:100vh;width:100%!important}.gslide-video .plyr__poster-enabled.plyr--loading .plyr__poster{display:none}.gslide-video .gvideo-wrapper{width:100%;margin:auto}.gslide-video::before{content:'';position:absolute;width:100%;height:100%;background:rgba(255,0,0,.34);display:none}.gslide-video.playing::before{display:none}.gslide-video.fullscreen{max-width:100%!important;min-width:100%;height:75vh}.gslide-video.fullscreen video{max-width:100%!important;width:100%!important}.gslide-inline{background:#fff;text-align:left;max-height:calc(100vh - 40px);overflow:auto;max-width:100%;margin:auto}.gslide-inline .ginlined-content{padding:20px;width:100%}.gslide-inline .dragging{cursor:-webkit-grabbing!important;cursor:grabbing!important;-webkit-transition:none;transition:none}.ginlined-content{overflow:auto;display:block!important;opacity:1}.gslide-external{display:-webkit-box;display:-ms-flexbox;display:flex;width:100%;min-width:100%;background:#fff;padding:0;overflow:auto;max-height:75vh;height:100%}.gslide-media{display:-webkit-box;display:-ms-flexbox;display:flex;width:auto}.zoomed .gslide-media{-webkit-box-shadow:none!important;box-shadow:none!important}.desc-bottom .gslide-media,.desc-top .gslide-media{margin:0 auto;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column}.gslide-description{position:relative;-webkit-box-flex:1;-ms-flex:1 0 100%;flex:1 0 100%}.gslide-description.description-left,.gslide-description.description-right{max-width:100%}.gslide-description.description-bottom,.gslide-description.description-top{margin:0 auto;width:100%}.gslide-description p{margin-bottom:12px}.gslide-description p:last-child{margin-bottom:0}.zoomed .gslide-description{display:none}.glightbox-button-hidden{display:none}.glightbox-mobile .glightbox-container .gslide-description{height:auto!important;width:100%;position:absolute;bottom:0;padding:19px 11px;max-width:100vw!important;-webkit-box-ordinal-group:3!important;-ms-flex-order:2!important;order:2!important;max-height:78vh;overflow:auto!important;background:-webkit-gradient(linear,left top,left bottom,from(rgba(0,0,0,0)),to(rgba(0,0,0,.75)));background:linear-gradient(to bottom,rgba(0,0,0,0) 0,rgba(0,0,0,.75) 100%);-webkit-transition:opacity .3s linear;transition:opacity .3s linear;padding-bottom:50px}.glightbox-mobile .glightbox-container .gslide-title{color:#fff;font-size:1em}.glightbox-mobile .glightbox-container .gslide-desc{color:#a1a1a1}.glightbox-mobile .glightbox-container .gslide-desc a{color:#fff;font-weight:700}.glightbox-mobile .glightbox-container .gslide-desc *{color:inherit}.glightbox-mobile .glightbox-container .gslide-desc .desc-more{color:#fff;opacity:.4}.gdesc-open .gslide-media{-webkit-transition:opacity .5s ease;transition:opacity .5s ease;opacity:.4}.gdesc-open .gdesc-inner{padding-bottom:30px}.gdesc-closed .gslide-media{-webkit-transition:opacity .5s ease;transition:opacity .5s ease;opacity:1}.greset{-webkit-transition:all .3s ease;transition:all .3s ease}.gabsolute{position:absolute}.grelative{position:relative}.glightbox-desc{display:none!important}.glightbox-open{overflow:hidden}.gloader{height:25px;width:25px;-webkit-animation:lightboxLoader .8s infinite linear;animation:lightboxLoader .8s infinite linear;border:2px solid #fff;border-right-color:transparent;border-radius:50%;position:absolute;display:block;z-index:9999;left:0;right:0;margin:0 auto;top:47%}.goverlay{width:100%;height:calc(100vh + 1px);position:fixed;top:-1px;left:0;background:#000;will-change:opacity}.glightbox-mobile .goverlay{background:#000}.gclose,.gnext,.gprev{z-index:99999;cursor:pointer;width:26px;height:44px;border:none;display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column}.gclose svg,.gnext svg,.gprev svg{display:block;width:25px;height:auto;margin:0;padding:0}.gclose.disabled,.gnext.disabled,.gprev.disabled{opacity:.1}.gclose .garrow,.gnext .garrow,.gprev .garrow{stroke:#fff}.gbtn.focused{outline:2px solid #0f3d81}iframe.wait-autoplay{opacity:0}.glightbox-closing .gclose,.glightbox-closing .gnext,.glightbox-closing .gprev{opacity:0!important}.glightbox-clean .gslide-description{background:#fff}.glightbox-clean .gdesc-inner{padding:22px 20px}.glightbox-clean .gslide-title{font-size:1em;font-weight:400;font-family:arial;color:#000;margin-bottom:19px;line-height:1.4em}.glightbox-clean .gslide-desc{font-size:.86em;margin-bottom:0;font-family:arial;line-height:1.4em}.glightbox-clean .gslide-video{background:#000}.glightbox-clean .gclose,.glightbox-clean .gnext,.glightbox-clean .gprev{background-color:rgba(0,0,0,.75);border-radius:4px}.glightbox-clean .gclose path,.glightbox-clean .gnext path,.glightbox-clean .gprev path{fill:#fff}.glightbox-clean .gprev{position:absolute;top:-100%;left:30px;width:40px;height:50px}.glightbox-clean .gnext{position:absolute;top:-100%;right:30px;width:40px;height:50px}.glightbox-clean .gclose{width:35px;height:35px;top:15px;right:10px;position:absolute}.glightbox-clean .gclose svg{width:18px;height:auto}.glightbox-clean .gclose:hover{opacity:1}.gfadeIn{-webkit-animation:gfadeIn .5s ease;animation:gfadeIn .5s ease}.gfadeOut{-webkit-animation:gfadeOut .5s ease;animation:gfadeOut .5s ease}.gslideOutLeft{-webkit-animation:gslideOutLeft .3s ease;animation:gslideOutLeft .3s ease}.gslideInLeft{-webkit-animation:gslideInLeft .3s ease;animation:gslideInLeft .3s ease}.gslideOutRight{-webkit-animation:gslideOutRight .3s ease;animation:gslideOutRight .3s ease}.gslideInRight{-webkit-animation:gslideInRight .3s ease;animation:gslideInRight .3s ease}.gzoomIn{-webkit-animation:gzoomIn .5s ease;animation:gzoomIn .5s ease}.gzoomOut{-webkit-animation:gzoomOut .5s ease;animation:gzoomOut .5s ease}@-webkit-keyframes lightboxLoader{0%{-webkit-transform:rotate(0);transform:rotate(0)}100%{-webkit-transform:rotate(360deg);transform:rotate(360deg)}}@keyframes lightboxLoader{0%{-webkit-transform:rotate(0);transform:rotate(0)}100%{-webkit-transform:rotate(360deg);transform:rotate(360deg)}}@-webkit-keyframes gfadeIn{from{opacity:0}to{opacity:1}}@keyframes gfadeIn{from{opacity:0}to{opacity:1}}@-webkit-keyframes gfadeOut{from{opacity:1}to{opacity:0}}@keyframes gfadeOut{from{opacity:1}to{opacity:0}}@-webkit-keyframes gslideInLeft{from{opacity:0;-webkit-transform:translate3d(-60%,0,0);transform:translate3d(-60%,0,0)}to{visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0);opacity:1}}@keyframes gslideInLeft{from{opacity:0;-webkit-transform:translate3d(-60%,0,0);transform:translate3d(-60%,0,0)}to{visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0);opacity:1}}@-webkit-keyframes gslideOutLeft{from{opacity:1;visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}to{-webkit-transform:translate3d(-60%,0,0);transform:translate3d(-60%,0,0);opacity:0;visibility:hidden}}@keyframes gslideOutLeft{from{opacity:1;visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}to{-webkit-transform:translate3d(-60%,0,0);transform:translate3d(-60%,0,0);opacity:0;visibility:hidden}}@-webkit-keyframes gslideInRight{from{opacity:0;visibility:visible;-webkit-transform:translate3d(60%,0,0);transform:translate3d(60%,0,0)}to{-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0);opacity:1}}@keyframes gslideInRight{from{opacity:0;visibility:visible;-webkit-transform:translate3d(60%,0,0);transform:translate3d(60%,0,0)}to{-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0);opacity:1}}@-webkit-keyframes gslideOutRight{from{opacity:1;visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}to{-webkit-transform:translate3d(60%,0,0);transform:translate3d(60%,0,0);opacity:0}}@keyframes gslideOutRight{from{opacity:1;visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}to{-webkit-transform:translate3d(60%,0,0);transform:translate3d(60%,0,0);opacity:0}}@-webkit-keyframes gzoomIn{from{opacity:0;-webkit-transform:scale3d(.3,.3,.3);transform:scale3d(.3,.3,.3)}to{opacity:1}}@keyframes gzoomIn{from{opacity:0;-webkit-transform:scale3d(.3,.3,.3);transform:scale3d(.3,.3,.3)}to{opacity:1}}@-webkit-keyframes gzoomOut{from{opacity:1}50%{opacity:0;-webkit-transform:scale3d(.3,.3,.3);transform:scale3d(.3,.3,.3)}to{opacity:0}}@keyframes gzoomOut{from{opacity:1}50%{opacity:0;-webkit-transform:scale3d(.3,.3,.3);transform:scale3d(.3,.3,.3)}to{opacity:0}}@media (min-width:769px){.glightbox-container .ginner-container{width:auto;height:auto;-webkit-box-orient:horizontal;-webkit-box-direction:normal;-ms-flex-direction:row;flex-direction:row}.glightbox-container .ginner-container.desc-top .gslide-description{-webkit-box-ordinal-group:1;-ms-flex-order:0;order:0}.glightbox-container .ginner-container.desc-top .gslide-image,.glightbox-container .ginner-container.desc-top .gslide-image img{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}.glightbox-container .ginner-container.desc-left .gslide-description{-webkit-box-ordinal-group:1;-ms-flex-order:0;order:0}.glightbox-container .ginner-container.desc-left .gslide-image{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}.gslide-image img{max-height:97vh;max-width:100%}.gslide-image img.zoomable{cursor:-webkit-zoom-in;cursor:zoom-in}.zoomed .gslide-image img.zoomable{cursor:-webkit-grab;cursor:grab}.gslide-inline{max-height:95vh}.gslide-external{max-height:100vh}.gslide-description.description-left,.gslide-description.description-right{max-width:275px}.glightbox-open{height:auto}.goverlay{background:rgba(0,0,0,.92)}.glightbox-clean .gslide-media{-webkit-box-shadow:1px 2px 9px 0 rgba(0,0,0,.65);box-shadow:1px 2px 9px 0 rgba(0,0,0,.65)}.glightbox-clean .description-left .gdesc-inner,.glightbox-clean .description-right .gdesc-inner{position:absolute;height:100%;overflow-y:auto}.glightbox-clean .gclose,.glightbox-clean .gnext,.glightbox-clean .gprev{background-color:rgba(0,0,0,.32)}.glightbox-clean .gclose:hover,.glightbox-clean .gnext:hover,.glightbox-clean .gprev:hover{background-color:rgba(0,0,0,.7)}.glightbox-clean .gprev{top:45%}.glightbox-clean .gnext{top:45%}}@media (min-width:992px){.glightbox-clean .gclose{opacity:.7;right:20px}}@media screen and (max-height:420px){.goverlay{background:#000}} -/*! update cache: 20260104155727 */ +/*! update cache: 20260104161427 */ diff --git a/assets/stylesheets/main.484c7ddc.min.css b/assets/stylesheets/main.484c7ddc.min.css index 8b4e53686..9d5b1fafb 100644 --- a/assets/stylesheets/main.484c7ddc.min.css +++ b/assets/stylesheets/main.484c7ddc.min.css @@ -1,2 +1,2 @@ @charset "UTF-8";html{-webkit-text-size-adjust:none;-moz-text-size-adjust:none;text-size-adjust:none;box-sizing:border-box}*,:after,:before{box-sizing:inherit}@media (prefers-reduced-motion){*,:after,:before{transition:none!important}}body{margin:0}a,button,input,label{-webkit-tap-highlight-color:transparent}a{color:inherit;text-decoration:none}hr{border:0;box-sizing:initial;display:block;height:.05rem;overflow:visible;padding:0}small{font-size:80%}sub,sup{line-height:1em}img{border-style:none}table{border-collapse:initial;border-spacing:0}td,th{font-weight:400;vertical-align:top}button{background:#0000;border:0;font-family:inherit;font-size:inherit;margin:0;padding:0}input{border:0;outline:none}:root{--md-primary-fg-color:#4051b5;--md-primary-fg-color--light:#5d6cc0;--md-primary-fg-color--dark:#303fa1;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3;--md-accent-fg-color:#526cfe;--md-accent-fg-color--transparent:#526cfe1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-scheme=default]{color-scheme:light}[data-md-color-scheme=default] img[src$="#gh-dark-mode-only"],[data-md-color-scheme=default] img[src$="#only-dark"]{display:none}:root,[data-md-color-scheme=default]{--md-hue:225deg;--md-default-fg-color:#000000de;--md-default-fg-color--light:#0000008a;--md-default-fg-color--lighter:#00000052;--md-default-fg-color--lightest:#00000012;--md-default-bg-color:#fff;--md-default-bg-color--light:#ffffffb3;--md-default-bg-color--lighter:#ffffff4d;--md-default-bg-color--lightest:#ffffff1f;--md-code-fg-color:#36464e;--md-code-bg-color:#f5f5f5;--md-code-bg-color--light:#f5f5f5b3;--md-code-bg-color--lighter:#f5f5f54d;--md-code-hl-color:#4287ff;--md-code-hl-color--light:#4287ff1a;--md-code-hl-number-color:#d52a2a;--md-code-hl-special-color:#db1457;--md-code-hl-function-color:#a846b9;--md-code-hl-constant-color:#6e59d9;--md-code-hl-keyword-color:#3f6ec6;--md-code-hl-string-color:#1c7d4d;--md-code-hl-name-color:var(--md-code-fg-color);--md-code-hl-operator-color:var(--md-default-fg-color--light);--md-code-hl-punctuation-color:var(--md-default-fg-color--light);--md-code-hl-comment-color:var(--md-default-fg-color--light);--md-code-hl-generic-color:var(--md-default-fg-color--light);--md-code-hl-variable-color:var(--md-default-fg-color--light);--md-typeset-color:var(--md-default-fg-color);--md-typeset-a-color:var(--md-primary-fg-color);--md-typeset-del-color:#f5503d26;--md-typeset-ins-color:#0bd57026;--md-typeset-kbd-color:#fafafa;--md-typeset-kbd-accent-color:#fff;--md-typeset-kbd-border-color:#b8b8b8;--md-typeset-mark-color:#ffff0080;--md-typeset-table-color:#0000001f;--md-typeset-table-color--light:rgba(0,0,0,.035);--md-admonition-fg-color:var(--md-default-fg-color);--md-admonition-bg-color:var(--md-default-bg-color);--md-warning-fg-color:#000000de;--md-warning-bg-color:#ff9;--md-footer-fg-color:#fff;--md-footer-fg-color--light:#ffffffb3;--md-footer-fg-color--lighter:#ffffff73;--md-footer-bg-color:#000000de;--md-footer-bg-color--dark:#00000052;--md-shadow-z1:0 0.2rem 0.5rem #0000000d,0 0 0.05rem #0000001a;--md-shadow-z2:0 0.2rem 0.5rem #0000001a,0 0 0.05rem #00000040;--md-shadow-z3:0 0.2rem 0.5rem #0003,0 0 0.05rem #00000059}.md-icon svg{fill:currentcolor;display:block;height:1.2rem;width:1.2rem}body{-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;--md-text-font-family:var(--md-text-font,_),-apple-system,BlinkMacSystemFont,Helvetica,Arial,sans-serif;--md-code-font-family:var(--md-code-font,_),SFMono-Regular,Consolas,Menlo,monospace}aside,body,input{font-feature-settings:"kern","liga";color:var(--md-typeset-color);font-family:var(--md-text-font-family)}code,kbd,pre{font-feature-settings:"kern";font-family:var(--md-code-font-family)}:root{--md-typeset-table-sort-icon:url('data:image/svg+xml;charset=utf-8,');--md-typeset-table-sort-icon--asc:url('data:image/svg+xml;charset=utf-8,');--md-typeset-table-sort-icon--desc:url('data:image/svg+xml;charset=utf-8,')}.md-typeset{-webkit-print-color-adjust:exact;color-adjust:exact;font-size:.8rem;line-height:1.6;overflow-wrap:break-word}@media print{.md-typeset{font-size:.68rem}}.md-typeset blockquote,.md-typeset dl,.md-typeset figure,.md-typeset ol,.md-typeset pre,.md-typeset ul{margin-bottom:1em;margin-top:1em}.md-typeset h1{color:var(--md-default-fg-color--light);font-size:2em;line-height:1.3;margin:0 0 1.25em}.md-typeset h1,.md-typeset h2{font-weight:300;letter-spacing:-.01em}.md-typeset h2{font-size:1.5625em;line-height:1.4;margin:1.6em 0 .64em}.md-typeset h3{font-size:1.25em;font-weight:400;letter-spacing:-.01em;line-height:1.5;margin:1.6em 0 .8em}.md-typeset h2+h3{margin-top:.8em}.md-typeset h4{font-weight:700;letter-spacing:-.01em;margin:1em 0}.md-typeset h5,.md-typeset h6{color:var(--md-default-fg-color--light);font-size:.8em;font-weight:700;letter-spacing:-.01em;margin:1.25em 0}.md-typeset h5{text-transform:uppercase}.md-typeset h5 code{text-transform:none}.md-typeset hr{border-bottom:.05rem solid var(--md-default-fg-color--lightest);display:flow-root;margin:1.5em 0}.md-typeset a{color:var(--md-typeset-a-color);word-break:break-word}.md-typeset a,.md-typeset a:before{transition:color 125ms}.md-typeset a:focus,.md-typeset a:hover{color:var(--md-accent-fg-color)}.md-typeset a:focus code,.md-typeset a:hover code{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}.md-typeset a code{color:var(--md-typeset-a-color)}.md-typeset a.focus-visible{outline-color:var(--md-accent-fg-color);outline-offset:.2rem}.md-typeset code,.md-typeset kbd,.md-typeset pre{color:var(--md-code-fg-color);direction:ltr;font-variant-ligatures:none;transition:background-color 125ms}@media print{.md-typeset code,.md-typeset kbd,.md-typeset pre{white-space:pre-wrap}}.md-typeset code{background-color:var(--md-code-bg-color);border-radius:.1rem;-webkit-box-decoration-break:clone;box-decoration-break:clone;font-size:.85em;padding:0 .2941176471em;transition:color 125ms,background-color 125ms;word-break:break-word}.md-typeset code:not(.focus-visible){-webkit-tap-highlight-color:transparent;outline:none}.md-typeset pre{display:flow-root;line-height:1.4;position:relative}.md-typeset pre>code{-webkit-box-decoration-break:slice;box-decoration-break:slice;box-shadow:none;display:block;margin:0;outline-color:var(--md-accent-fg-color);overflow:auto;padding:.7720588235em 1.1764705882em;scrollbar-color:var(--md-default-fg-color--lighter) #0000;scrollbar-width:thin;touch-action:auto;word-break:normal}.md-typeset pre>code:hover{scrollbar-color:var(--md-accent-fg-color) #0000}.md-typeset pre>code::-webkit-scrollbar{height:.2rem;width:.2rem}.md-typeset pre>code::-webkit-scrollbar-thumb{background-color:var(--md-default-fg-color--lighter)}.md-typeset pre>code::-webkit-scrollbar-thumb:hover{background-color:var(--md-accent-fg-color)}.md-typeset kbd{background-color:var(--md-typeset-kbd-color);border-radius:.1rem;box-shadow:0 .1rem 0 .05rem var(--md-typeset-kbd-border-color),0 .1rem 0 var(--md-typeset-kbd-border-color),0 -.1rem .2rem var(--md-typeset-kbd-accent-color) inset;color:var(--md-default-fg-color);display:inline-block;font-size:.75em;padding:0 .6666666667em;vertical-align:text-top;word-break:break-word}.md-typeset mark{background-color:var(--md-typeset-mark-color);-webkit-box-decoration-break:clone;box-decoration-break:clone;color:inherit;word-break:break-word}.md-typeset abbr{cursor:help;text-decoration:none}.md-typeset [data-preview],.md-typeset abbr{border-bottom:.05rem dotted var(--md-default-fg-color--light)}.md-typeset small{opacity:.75}[dir=ltr] .md-typeset sub,[dir=ltr] .md-typeset sup{margin-left:.078125em}[dir=rtl] .md-typeset sub,[dir=rtl] .md-typeset sup{margin-right:.078125em}[dir=ltr] .md-typeset blockquote{padding-left:.6rem}[dir=rtl] .md-typeset blockquote{padding-right:.6rem}[dir=ltr] .md-typeset blockquote{border-left:.2rem solid var(--md-default-fg-color--lighter)}[dir=rtl] .md-typeset blockquote{border-right:.2rem solid var(--md-default-fg-color--lighter)}.md-typeset blockquote{color:var(--md-default-fg-color--light);margin-left:0;margin-right:0}.md-typeset ul{list-style-type:disc}.md-typeset ul[type]{list-style-type:revert-layer}[dir=ltr] .md-typeset ol,[dir=ltr] .md-typeset ul{margin-left:.625em}[dir=rtl] .md-typeset ol,[dir=rtl] .md-typeset ul{margin-right:.625em}.md-typeset ol,.md-typeset ul{padding:0}.md-typeset ol:not([hidden]),.md-typeset ul:not([hidden]){display:flow-root}.md-typeset ol ol,.md-typeset ul ol{list-style-type:lower-alpha}.md-typeset ol ol ol,.md-typeset ul ol ol{list-style-type:lower-roman}.md-typeset ol ol ol ol,.md-typeset ul ol ol ol{list-style-type:upper-alpha}.md-typeset ol ol ol ol ol,.md-typeset ul ol ol ol ol{list-style-type:upper-roman}.md-typeset ol[type],.md-typeset ul[type]{list-style-type:revert-layer}[dir=ltr] .md-typeset ol li,[dir=ltr] .md-typeset ul li{margin-left:1.25em}[dir=rtl] .md-typeset ol li,[dir=rtl] .md-typeset ul li{margin-right:1.25em}.md-typeset ol li,.md-typeset ul li{margin-bottom:.5em}.md-typeset ol li blockquote,.md-typeset ol li p,.md-typeset ul li blockquote,.md-typeset ul li p{margin:.5em 0}.md-typeset ol li:last-child,.md-typeset ul li:last-child{margin-bottom:0}[dir=ltr] .md-typeset ol li ol,[dir=ltr] .md-typeset ol li ul,[dir=ltr] .md-typeset ul li ol,[dir=ltr] .md-typeset ul li ul{margin-left:.625em}[dir=rtl] .md-typeset ol li ol,[dir=rtl] .md-typeset ol li ul,[dir=rtl] .md-typeset ul li ol,[dir=rtl] .md-typeset ul li ul{margin-right:.625em}.md-typeset ol li ol,.md-typeset ol li ul,.md-typeset ul li ol,.md-typeset ul li ul{margin-bottom:.5em;margin-top:.5em}[dir=ltr] .md-typeset dd{margin-left:1.875em}[dir=rtl] .md-typeset dd{margin-right:1.875em}.md-typeset dd{margin-bottom:1.5em;margin-top:1em}.md-typeset img,.md-typeset svg,.md-typeset video{height:auto;max-width:100%}.md-typeset img[align=left]{margin:1em 1em 1em 0}.md-typeset img[align=right]{margin:1em 0 1em 1em}.md-typeset img[align]:only-child{margin-top:0}.md-typeset figure{display:flow-root;margin:1em auto;max-width:100%;text-align:center;width:fit-content}.md-typeset figure img{display:block;margin:0 auto}.md-typeset figcaption{font-style:italic;margin:1em auto;max-width:24rem}.md-typeset iframe{max-width:100%}.md-typeset table:not([class]){background-color:var(--md-default-bg-color);border:.05rem solid var(--md-typeset-table-color);border-radius:.1rem;display:inline-block;font-size:.64rem;max-width:100%;overflow:auto;touch-action:auto}@media print{.md-typeset table:not([class]){display:table}}.md-typeset table:not([class])+*{margin-top:1.5em}.md-typeset table:not([class]) td>:first-child,.md-typeset table:not([class]) th>:first-child{margin-top:0}.md-typeset table:not([class]) td>:last-child,.md-typeset table:not([class]) th>:last-child{margin-bottom:0}.md-typeset table:not([class]) td:not([align]),.md-typeset table:not([class]) th:not([align]){text-align:left}[dir=rtl] .md-typeset table:not([class]) td:not([align]),[dir=rtl] .md-typeset table:not([class]) th:not([align]){text-align:right}.md-typeset table:not([class]) th{font-weight:700;min-width:5rem;padding:.9375em 1.25em;vertical-align:top}.md-typeset table:not([class]) td{border-top:.05rem solid var(--md-typeset-table-color);padding:.9375em 1.25em;vertical-align:top}.md-typeset table:not([class]) tbody tr{transition:background-color 125ms}.md-typeset table:not([class]) tbody tr:hover{background-color:var(--md-typeset-table-color--light);box-shadow:0 .05rem 0 var(--md-default-bg-color) inset}.md-typeset table:not([class]) a{word-break:normal}.md-typeset table th[role=columnheader]{cursor:pointer}[dir=ltr] .md-typeset table th[role=columnheader]:after{margin-left:.5em}[dir=rtl] .md-typeset table th[role=columnheader]:after{margin-right:.5em}.md-typeset table th[role=columnheader]:after{content:"";display:inline-block;height:1.2em;-webkit-mask-image:var(--md-typeset-table-sort-icon);mask-image:var(--md-typeset-table-sort-icon);-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;transition:background-color 125ms;vertical-align:text-bottom;width:1.2em}.md-typeset table th[role=columnheader]:hover:after{background-color:var(--md-default-fg-color--lighter)}.md-typeset table th[role=columnheader][aria-sort=ascending]:after{background-color:var(--md-default-fg-color--light);-webkit-mask-image:var(--md-typeset-table-sort-icon--asc);mask-image:var(--md-typeset-table-sort-icon--asc)}.md-typeset table th[role=columnheader][aria-sort=descending]:after{background-color:var(--md-default-fg-color--light);-webkit-mask-image:var(--md-typeset-table-sort-icon--desc);mask-image:var(--md-typeset-table-sort-icon--desc)}.md-typeset__scrollwrap{margin:1em -.8rem;overflow-x:auto;touch-action:auto}.md-typeset__table{display:inline-block;margin-bottom:.5em;padding:0 .8rem}@media print{.md-typeset__table{display:block}}html .md-typeset__table table{display:table;margin:0;overflow:hidden;width:100%}@media screen and (max-width:44.984375em){.md-content__inner>pre{margin:1em -.8rem}.md-content__inner>pre code{border-radius:0}}.md-typeset .md-author{border-radius:100%;display:block;flex-shrink:0;height:1.6rem;overflow:hidden;position:relative;transition:color 125ms,transform 125ms;width:1.6rem}.md-typeset .md-author img{display:block}.md-typeset .md-author--more{background:var(--md-default-fg-color--lightest);color:var(--md-default-fg-color--lighter);font-size:.6rem;font-weight:700;line-height:1.6rem;text-align:center}.md-typeset .md-author--long{height:2.4rem;width:2.4rem}.md-typeset a.md-author{transform:scale(1)}.md-typeset a.md-author img{border-radius:100%;filter:grayscale(100%) opacity(75%);transition:filter 125ms}.md-typeset a.md-author:focus,.md-typeset a.md-author:hover{transform:scale(1.1);z-index:1}.md-typeset a.md-author:focus img,.md-typeset a.md-author:hover img{filter:grayscale(0)}.md-banner{background-color:var(--md-footer-bg-color);color:var(--md-footer-fg-color);overflow:auto}@media print{.md-banner{display:none}}.md-banner--warning{background-color:var(--md-warning-bg-color);color:var(--md-warning-fg-color)}.md-banner__inner{font-size:.7rem;margin:.6rem auto;padding:0 .8rem}[dir=ltr] .md-banner__button{float:right}[dir=rtl] .md-banner__button{float:left}.md-banner__button{color:inherit;cursor:pointer;transition:opacity .25s}.no-js .md-banner__button{display:none}.md-banner__button:hover{opacity:.7}html{font-size:125%;height:100%;overflow-x:hidden}@media screen and (min-width:100em){html{font-size:137.5%}}@media screen and (min-width:125em){html{font-size:150%}}body{background-color:var(--md-default-bg-color);display:flex;flex-direction:column;font-size:.5rem;min-height:100%;position:relative;width:100%}@media print{body{display:block}}@media screen and (max-width:59.984375em){body[data-md-scrolllock]{position:fixed}}.md-grid{margin-left:auto;margin-right:auto;max-width:61rem}.md-container{display:flex;flex-direction:column;flex-grow:1}@media print{.md-container{display:block}}.md-main{flex-grow:1}.md-main__inner{display:flex;height:100%;margin-top:1.5rem}.md-ellipsis{overflow:hidden;text-overflow:ellipsis}.md-toggle{display:none}.md-option{height:0;opacity:0;position:absolute;width:0}.md-option:checked+label:not([hidden]){display:block}.md-option.focus-visible+label{outline-color:var(--md-accent-fg-color);outline-style:auto}.md-skip{background-color:var(--md-default-fg-color);border-radius:.1rem;color:var(--md-default-bg-color);font-size:.64rem;margin:.5rem;opacity:0;outline-color:var(--md-accent-fg-color);padding:.3rem .5rem;position:fixed;transform:translateY(.4rem);z-index:-1}.md-skip:focus{opacity:1;transform:translateY(0);transition:transform .25s cubic-bezier(.4,0,.2,1),opacity 175ms 75ms;z-index:10}@page{margin:25mm}:root{--md-clipboard-icon:url('data:image/svg+xml;charset=utf-8,')}.md-clipboard{border-radius:.1rem;color:var(--md-default-fg-color--lightest);cursor:pointer;height:1.5em;outline-color:var(--md-accent-fg-color);outline-offset:.1rem;transition:color .25s;width:1.5em;z-index:1}@media print{.md-clipboard{display:none}}.md-clipboard:not(.focus-visible){-webkit-tap-highlight-color:transparent;outline:none}:hover>.md-clipboard{color:var(--md-default-fg-color--light)}.md-clipboard:focus,.md-clipboard:hover{color:var(--md-accent-fg-color)}.md-clipboard:after{background-color:currentcolor;content:"";display:block;height:1.125em;margin:0 auto;-webkit-mask-image:var(--md-clipboard-icon);mask-image:var(--md-clipboard-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:1.125em}.md-clipboard--inline{cursor:pointer}.md-clipboard--inline code{transition:color .25s,background-color .25s}.md-clipboard--inline:focus code,.md-clipboard--inline:hover code{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}:root{--md-code-select-icon:url('data:image/svg+xml;charset=utf-8,');--md-code-copy-icon:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .md-code__content{display:grid}.md-code__nav{background-color:var(--md-code-bg-color--lighter);border-radius:.1rem;display:flex;gap:.2rem;padding:.2rem;position:absolute;right:.25em;top:.25em;transition:background-color .25s;z-index:1}:hover>.md-code__nav{background-color:var(--md-code-bg-color--light)}.md-code__button{color:var(--md-default-fg-color--lightest);cursor:pointer;display:block;height:1.5em;outline-color:var(--md-accent-fg-color);outline-offset:.1rem;transition:color .25s;width:1.5em}:hover>*>.md-code__button{color:var(--md-default-fg-color--light)}.md-code__button.focus-visible,.md-code__button:hover{color:var(--md-accent-fg-color)}.md-code__button--active{color:var(--md-default-fg-color)!important}.md-code__button:after{background-color:currentcolor;content:"";display:block;height:1.125em;margin:0 auto;-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:1.125em}.md-code__button[data-md-type=select]:after{-webkit-mask-image:var(--md-code-select-icon);mask-image:var(--md-code-select-icon)}.md-code__button[data-md-type=copy]:after{-webkit-mask-image:var(--md-code-copy-icon);mask-image:var(--md-code-copy-icon)}@keyframes consent{0%{opacity:0;transform:translateY(100%)}to{opacity:1;transform:translateY(0)}}@keyframes overlay{0%{opacity:0}to{opacity:1}}.md-consent__overlay{animation:overlay .25s both;-webkit-backdrop-filter:blur(.1rem);backdrop-filter:blur(.1rem);background-color:#0000008a;height:100%;opacity:1;position:fixed;top:0;width:100%;z-index:5}.md-consent__inner{animation:consent .5s cubic-bezier(.1,.7,.1,1) both;background-color:var(--md-default-bg-color);border:0;border-radius:.1rem;bottom:0;box-shadow:0 0 .2rem #0000001a,0 .2rem .4rem #0003;max-height:100%;overflow:auto;padding:0;position:fixed;width:100%;z-index:5}.md-consent__form{padding:.8rem}.md-consent__settings{display:none;margin:1em 0}input:checked+.md-consent__settings{display:block}.md-consent__controls{margin-bottom:.8rem}.md-typeset .md-consent__controls .md-button{display:inline}@media screen and (max-width:44.984375em){.md-typeset .md-consent__controls .md-button{display:block;margin-top:.4rem;text-align:center;width:100%}}.md-consent label{cursor:pointer}.md-content{flex-grow:1;min-width:0}.md-content__inner{margin:0 .8rem 1.2rem;padding-top:.6rem}@media screen and (min-width:76.25em){[dir=ltr] .md-sidebar--primary:not([hidden])~.md-content>.md-content__inner{margin-left:1.2rem}[dir=ltr] .md-sidebar--secondary:not([hidden])~.md-content>.md-content__inner,[dir=rtl] .md-sidebar--primary:not([hidden])~.md-content>.md-content__inner{margin-right:1.2rem}[dir=rtl] .md-sidebar--secondary:not([hidden])~.md-content>.md-content__inner{margin-left:1.2rem}}.md-content__inner:before{content:"";display:block;height:.4rem}.md-content__inner>:last-child{margin-bottom:0}[dir=ltr] .md-content__button{float:right}[dir=rtl] .md-content__button{float:left}[dir=ltr] .md-content__button{margin-left:.4rem}[dir=rtl] .md-content__button{margin-right:.4rem}.md-content__button{margin:.4rem 0;padding:0}@media print{.md-content__button{display:none}}.md-typeset .md-content__button{color:var(--md-default-fg-color--lighter)}.md-content__button svg{display:inline;vertical-align:top}[dir=rtl] .md-content__button svg{transform:scaleX(-1)}[dir=ltr] .md-dialog{right:.8rem}[dir=rtl] .md-dialog{left:.8rem}.md-dialog{background-color:var(--md-default-fg-color);border-radius:.1rem;bottom:.8rem;box-shadow:var(--md-shadow-z3);min-width:11.1rem;opacity:0;padding:.4rem .6rem;pointer-events:none;position:fixed;transform:translateY(100%);transition:transform 0ms .4s,opacity .4s;z-index:4}@media print{.md-dialog{display:none}}.md-dialog--active{opacity:1;pointer-events:auto;transform:translateY(0);transition:transform .4s cubic-bezier(.075,.85,.175,1),opacity .4s}.md-dialog__inner{color:var(--md-default-bg-color);font-size:.7rem}.md-feedback{margin:2em 0 1em;text-align:center}.md-feedback fieldset{border:none;margin:0;padding:0}.md-feedback__title{font-weight:700;margin:1em auto}.md-feedback__inner{position:relative}.md-feedback__list{display:flex;flex-wrap:wrap;place-content:baseline center;position:relative}.md-feedback__list:hover .md-icon:not(:disabled){color:var(--md-default-fg-color--lighter)}:disabled .md-feedback__list{min-height:1.8rem}.md-feedback__icon{color:var(--md-default-fg-color--light);cursor:pointer;flex-shrink:0;margin:0 .1rem;transition:color 125ms}.md-feedback__icon:not(:disabled).md-icon:hover{color:var(--md-accent-fg-color)}.md-feedback__icon:disabled{color:var(--md-default-fg-color--lightest);pointer-events:none}.md-feedback__note{opacity:0;position:relative;transform:translateY(.4rem);transition:transform .4s cubic-bezier(.1,.7,.1,1),opacity .15s}.md-feedback__note>*{margin:0 auto;max-width:16rem}:disabled .md-feedback__note{opacity:1;transform:translateY(0)}@media print{.md-feedback{display:none}}.md-footer{background-color:var(--md-footer-bg-color);color:var(--md-footer-fg-color)}@media print{.md-footer{display:none}}.md-footer__inner{justify-content:space-between;overflow:auto;padding:.2rem}.md-footer__inner:not([hidden]){display:flex}.md-footer__link{align-items:end;display:flex;flex-grow:0.01;margin-bottom:.4rem;margin-top:1rem;max-width:100%;outline-color:var(--md-accent-fg-color);overflow:hidden;transition:opacity .25s}.md-footer__link:focus,.md-footer__link:hover{opacity:.7}[dir=rtl] .md-footer__link svg{transform:scaleX(-1)}@media screen and (max-width:44.984375em){.md-footer__link--prev{flex-shrink:0}.md-footer__link--prev .md-footer__title{display:none}}[dir=ltr] .md-footer__link--next{margin-left:auto}[dir=rtl] .md-footer__link--next{margin-right:auto}.md-footer__link--next{text-align:right}[dir=rtl] .md-footer__link--next{text-align:left}.md-footer__title{flex-grow:1;font-size:.9rem;margin-bottom:.7rem;max-width:calc(100% - 2.4rem);padding:0 1rem;white-space:nowrap}.md-footer__button{margin:.2rem;padding:.4rem}.md-footer__direction{font-size:.64rem;opacity:.7}.md-footer-meta{background-color:var(--md-footer-bg-color--dark)}.md-footer-meta__inner{display:flex;flex-wrap:wrap;justify-content:space-between;padding:.2rem}html .md-footer-meta.md-typeset a{color:var(--md-footer-fg-color--light)}html .md-footer-meta.md-typeset a:focus,html .md-footer-meta.md-typeset a:hover{color:var(--md-footer-fg-color)}.md-copyright{color:var(--md-footer-fg-color--lighter);font-size:.64rem;margin:auto .6rem;padding:.4rem 0;width:100%}@media screen and (min-width:45em){.md-copyright{width:auto}}.md-copyright__highlight{color:var(--md-footer-fg-color--light)}.md-social{display:inline-flex;gap:.2rem;margin:0 .4rem;padding:.2rem 0 .6rem}@media screen and (min-width:45em){.md-social{padding:.6rem 0}}.md-social__link{display:inline-block;height:1.6rem;text-align:center;width:1.6rem}.md-social__link:before{line-height:1.9}.md-social__link svg{fill:currentcolor;max-height:.8rem;vertical-align:-25%}.md-typeset .md-button{border:.1rem solid;border-radius:.1rem;color:var(--md-primary-fg-color);cursor:pointer;display:inline-block;font-weight:700;padding:.625em 2em;transition:color 125ms,background-color 125ms,border-color 125ms}.md-typeset .md-button--primary{background-color:var(--md-primary-fg-color);border-color:var(--md-primary-fg-color);color:var(--md-primary-bg-color)}.md-typeset .md-button:focus,.md-typeset .md-button:hover{background-color:var(--md-accent-fg-color);border-color:var(--md-accent-fg-color);color:var(--md-accent-bg-color)}[dir=ltr] .md-typeset .md-input{border-top-left-radius:.1rem}[dir=ltr] .md-typeset .md-input,[dir=rtl] .md-typeset .md-input{border-top-right-radius:.1rem}[dir=rtl] .md-typeset .md-input{border-top-left-radius:.1rem}.md-typeset .md-input{border-bottom:.1rem solid var(--md-default-fg-color--lighter);box-shadow:var(--md-shadow-z1);font-size:.8rem;height:1.8rem;padding:0 .6rem;transition:border .25s,box-shadow .25s}.md-typeset .md-input:focus,.md-typeset .md-input:hover{border-bottom-color:var(--md-accent-fg-color);box-shadow:var(--md-shadow-z2)}.md-typeset .md-input--stretch{width:100%}.md-header{background-color:var(--md-primary-fg-color);box-shadow:0 0 .2rem #0000,0 .2rem .4rem #0000;color:var(--md-primary-bg-color);display:block;left:0;position:sticky;right:0;top:0;z-index:4}@media print{.md-header{display:none}}.md-header[hidden]{transform:translateY(-100%);transition:transform .25s cubic-bezier(.8,0,.6,1),box-shadow .25s}.md-header--shadow{box-shadow:0 0 .2rem #0000001a,0 .2rem .4rem #0003;transition:transform .25s cubic-bezier(.1,.7,.1,1),box-shadow .25s}.md-header__inner{align-items:center;display:flex;padding:0 .2rem}.md-header__button{color:currentcolor;cursor:pointer;margin:.2rem;outline-color:var(--md-accent-fg-color);padding:.4rem;position:relative;transition:opacity .25s;vertical-align:middle;z-index:1}.md-header__button:hover{opacity:.7}.md-header__button:not([hidden]){display:inline-block}.md-header__button:not(.focus-visible){-webkit-tap-highlight-color:transparent;outline:none}.md-header__button.md-logo{margin:.2rem;padding:.4rem}@media screen and (max-width:76.234375em){.md-header__button.md-logo{display:none}}.md-header__button.md-logo img,.md-header__button.md-logo svg{fill:currentcolor;display:block;height:1.2rem;width:auto}@media screen and (min-width:60em){.md-header__button[for=__search]{display:none}}.no-js .md-header__button[for=__search]{display:none}[dir=rtl] .md-header__button[for=__search] svg{transform:scaleX(-1)}@media screen and (min-width:76.25em){.md-header__button[for=__drawer]{display:none}}.md-header__topic{display:flex;max-width:100%;position:absolute;transition:transform .4s cubic-bezier(.1,.7,.1,1),opacity .15s;white-space:nowrap}.md-header__topic+.md-header__topic{opacity:0;pointer-events:none;transform:translateX(1.25rem);transition:transform .4s cubic-bezier(1,.7,.1,.1),opacity .15s;z-index:-1}[dir=rtl] .md-header__topic+.md-header__topic{transform:translateX(-1.25rem)}.md-header__topic:first-child{font-weight:700}[dir=ltr] .md-header__title{margin-left:1rem;margin-right:.4rem}[dir=rtl] .md-header__title{margin-left:.4rem;margin-right:1rem}.md-header__title{flex-grow:1;font-size:.9rem;height:2.4rem;line-height:2.4rem}.md-header__title--active .md-header__topic{opacity:0;pointer-events:none;transform:translateX(-1.25rem);transition:transform .4s cubic-bezier(1,.7,.1,.1),opacity .15s;z-index:-1}[dir=rtl] .md-header__title--active .md-header__topic{transform:translateX(1.25rem)}.md-header__title--active .md-header__topic+.md-header__topic{opacity:1;pointer-events:auto;transform:translateX(0);transition:transform .4s cubic-bezier(.1,.7,.1,1),opacity .15s;z-index:0}.md-header__title>.md-header__ellipsis{height:100%;position:relative;width:100%}.md-header__option{display:flex;flex-shrink:0;max-width:100%;transition:max-width 0ms .25s,opacity .25s .25s;white-space:nowrap}[data-md-toggle=search]:checked~.md-header .md-header__option{max-width:0;opacity:0;transition:max-width 0ms,opacity 0ms}.md-header__option>input{bottom:0}.md-header__source{display:none}@media screen and (min-width:60em){[dir=ltr] .md-header__source{margin-left:1rem}[dir=rtl] .md-header__source{margin-right:1rem}.md-header__source{display:block;max-width:11.7rem;width:11.7rem}}@media screen and (min-width:76.25em){[dir=ltr] .md-header__source{margin-left:1.4rem}[dir=rtl] .md-header__source{margin-right:1.4rem}}.md-meta{color:var(--md-default-fg-color--light);font-size:.7rem;line-height:1.3}.md-meta__list{display:inline-flex;flex-wrap:wrap;list-style:none;margin:0;padding:0}.md-meta__item:not(:last-child):after{content:"·";margin-left:.2rem;margin-right:.2rem}.md-meta__link{color:var(--md-typeset-a-color)}.md-meta__link:focus,.md-meta__link:hover{color:var(--md-accent-fg-color)}.md-draft{background-color:#ff1744;border-radius:.125em;color:#fff;display:inline-block;font-weight:700;padding-left:.5714285714em;padding-right:.5714285714em}:root{--md-nav-icon--prev:url('data:image/svg+xml;charset=utf-8,');--md-nav-icon--next:url('data:image/svg+xml;charset=utf-8,');--md-toc-icon:url('data:image/svg+xml;charset=utf-8,')}.md-nav{font-size:.7rem;line-height:1.3}.md-nav__title{color:var(--md-default-fg-color--light);display:block;font-weight:700;overflow:hidden;padding:0 .6rem;text-overflow:ellipsis}.md-nav__title .md-nav__button{display:none}.md-nav__title .md-nav__button img{height:100%;width:auto}.md-nav__title .md-nav__button.md-logo img,.md-nav__title .md-nav__button.md-logo svg{fill:currentcolor;display:block;height:2.4rem;max-width:100%;object-fit:contain;width:auto}.md-nav__list{list-style:none;margin:0;padding:0}.md-nav__link{align-items:flex-start;display:flex;gap:.4rem;margin-top:.625em;scroll-snap-align:start;transition:color 125ms}.md-nav__link--passed,.md-nav__link--passed code{color:var(--md-default-fg-color--light)}.md-nav__item .md-nav__link--active,.md-nav__item .md-nav__link--active code{color:var(--md-typeset-a-color)}.md-nav__link .md-ellipsis{position:relative}.md-nav__link .md-ellipsis code{word-break:normal}[dir=ltr] .md-nav__link .md-icon:last-child{margin-left:auto}[dir=rtl] .md-nav__link .md-icon:last-child{margin-right:auto}.md-nav__link .md-typeset{font-size:.7rem;line-height:1.3}.md-nav__link svg{fill:currentcolor;flex-shrink:0;height:1.3em;position:relative}.md-nav__link[for]:focus,.md-nav__link[for]:hover,.md-nav__link[href]:focus,.md-nav__link[href]:hover{color:var(--md-accent-fg-color);cursor:pointer}.md-nav__link[for]:focus code,.md-nav__link[for]:hover code,.md-nav__link[href]:focus code,.md-nav__link[href]:hover code{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}.md-nav__link.focus-visible{outline-color:var(--md-accent-fg-color);outline-offset:.2rem}.md-nav--primary .md-nav__link[for=__toc]{display:none}.md-nav--primary .md-nav__link[for=__toc] .md-icon:after{background-color:currentcolor;display:block;height:100%;-webkit-mask-image:var(--md-toc-icon);mask-image:var(--md-toc-icon);width:100%}.md-nav--primary .md-nav__link[for=__toc]~.md-nav{display:none}.md-nav__container>.md-nav__link{margin-top:0}.md-nav__container>.md-nav__link:first-child{flex-grow:1;min-width:0}.md-nav__icon{flex-shrink:0}.md-nav__source{display:none}@media screen and (max-width:76.234375em){.md-nav--primary,.md-nav--primary .md-nav{background-color:var(--md-default-bg-color);display:flex;flex-direction:column;height:100%;left:0;position:absolute;right:0;top:0;z-index:1}.md-nav--primary .md-nav__item,.md-nav--primary .md-nav__title{font-size:.8rem;line-height:1.5}.md-nav--primary .md-nav__title{background-color:var(--md-default-fg-color--lightest);color:var(--md-default-fg-color--light);cursor:pointer;height:5.6rem;line-height:2.4rem;padding:3rem .8rem .2rem;position:relative;white-space:nowrap}[dir=ltr] .md-nav--primary .md-nav__title .md-nav__icon{left:.4rem}[dir=rtl] .md-nav--primary .md-nav__title .md-nav__icon{right:.4rem}.md-nav--primary .md-nav__title .md-nav__icon{display:block;height:1.2rem;margin:.2rem;position:absolute;top:.4rem;width:1.2rem}.md-nav--primary .md-nav__title .md-nav__icon:after{background-color:currentcolor;content:"";display:block;height:100%;-webkit-mask-image:var(--md-nav-icon--prev);mask-image:var(--md-nav-icon--prev);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:100%}.md-nav--primary .md-nav__title~.md-nav__list{background-color:var(--md-default-bg-color);box-shadow:0 .05rem 0 var(--md-default-fg-color--lightest) inset;overflow-y:auto;scroll-snap-type:y mandatory;touch-action:pan-y}.md-nav--primary .md-nav__title~.md-nav__list>:first-child{border-top:0}.md-nav--primary .md-nav__title[for=__drawer]{background-color:var(--md-primary-fg-color);color:var(--md-primary-bg-color);font-weight:700}.md-nav--primary .md-nav__title .md-logo{display:block;left:.2rem;margin:.2rem;padding:.4rem;position:absolute;right:.2rem;top:.2rem}.md-nav--primary .md-nav__list{flex:1}.md-nav--primary .md-nav__item{border-top:.05rem solid var(--md-default-fg-color--lightest)}.md-nav--primary .md-nav__item--active>.md-nav__link{color:var(--md-typeset-a-color)}.md-nav--primary .md-nav__item--active>.md-nav__link:focus,.md-nav--primary .md-nav__item--active>.md-nav__link:hover{color:var(--md-accent-fg-color)}.md-nav--primary .md-nav__link{margin-top:0;padding:.6rem .8rem}.md-nav--primary .md-nav__link svg{margin-top:.1em}.md-nav--primary .md-nav__link>.md-nav__link{padding:0}[dir=ltr] .md-nav--primary .md-nav__link .md-nav__icon{margin-right:-.2rem}[dir=rtl] .md-nav--primary .md-nav__link .md-nav__icon{margin-left:-.2rem}.md-nav--primary .md-nav__link .md-nav__icon{font-size:1.2rem;height:1.2rem;width:1.2rem}.md-nav--primary .md-nav__link .md-nav__icon:after{background-color:currentcolor;content:"";display:block;height:100%;-webkit-mask-image:var(--md-nav-icon--next);mask-image:var(--md-nav-icon--next);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:100%}[dir=rtl] .md-nav--primary .md-nav__icon:after{transform:scale(-1)}.md-nav--primary .md-nav--secondary .md-nav{background-color:initial;position:static}[dir=ltr] .md-nav--primary .md-nav--secondary .md-nav .md-nav__link{padding-left:1.4rem}[dir=rtl] .md-nav--primary .md-nav--secondary .md-nav .md-nav__link{padding-right:1.4rem}[dir=ltr] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav__link{padding-left:2rem}[dir=rtl] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav__link{padding-right:2rem}[dir=ltr] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav .md-nav__link{padding-left:2.6rem}[dir=rtl] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav .md-nav__link{padding-right:2.6rem}[dir=ltr] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav .md-nav .md-nav__link{padding-left:3.2rem}[dir=rtl] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav .md-nav .md-nav__link{padding-right:3.2rem}.md-nav--secondary{background-color:initial}.md-nav__toggle~.md-nav{display:flex;opacity:0;transform:translateX(100%);transition:transform .25s cubic-bezier(.8,0,.6,1),opacity 125ms 50ms}[dir=rtl] .md-nav__toggle~.md-nav{transform:translateX(-100%)}.md-nav__toggle:checked~.md-nav{opacity:1;transform:translateX(0);transition:transform .25s cubic-bezier(.4,0,.2,1),opacity 125ms 125ms}.md-nav__toggle:checked~.md-nav>.md-nav__list{backface-visibility:hidden}}@media screen and (max-width:59.984375em){.md-nav--primary .md-nav__link[for=__toc]{display:flex}.md-nav--primary .md-nav__link[for=__toc] .md-icon:after{content:""}.md-nav--primary .md-nav__link[for=__toc]+.md-nav__link{display:none}.md-nav--primary .md-nav__link[for=__toc]~.md-nav{display:flex}.md-nav__source{background-color:var(--md-primary-fg-color--dark);color:var(--md-primary-bg-color);display:block;padding:0 .2rem}}@media screen and (min-width:60em) and (max-width:76.234375em){.md-nav--integrated .md-nav__link[for=__toc]{display:flex}.md-nav--integrated .md-nav__link[for=__toc] .md-icon:after{content:""}.md-nav--integrated .md-nav__link[for=__toc]+.md-nav__link{display:none}.md-nav--integrated .md-nav__link[for=__toc]~.md-nav{display:flex}}@media screen and (min-width:60em){.md-nav{margin-bottom:-.4rem}.md-nav--secondary .md-nav__title{background:var(--md-default-bg-color);box-shadow:0 0 .4rem .4rem var(--md-default-bg-color);position:sticky;top:0;z-index:1}.md-nav--secondary .md-nav__title[for=__toc]{scroll-snap-align:start}.md-nav--secondary .md-nav__title .md-nav__icon{display:none}[dir=ltr] .md-nav--secondary .md-nav__list{padding-left:.6rem}[dir=rtl] .md-nav--secondary .md-nav__list{padding-right:.6rem}.md-nav--secondary .md-nav__list{padding-bottom:.4rem}[dir=ltr] .md-nav--secondary .md-nav__item>.md-nav__link{margin-right:.4rem}[dir=rtl] .md-nav--secondary .md-nav__item>.md-nav__link{margin-left:.4rem}}@media screen and (min-width:76.25em){.md-nav{margin-bottom:-.4rem;transition:max-height .25s cubic-bezier(.86,0,.07,1)}.md-nav--primary .md-nav__title{background:var(--md-default-bg-color);box-shadow:0 0 .4rem .4rem var(--md-default-bg-color);position:sticky;top:0;z-index:1}.md-nav--primary .md-nav__title[for=__drawer]{scroll-snap-align:start}.md-nav--primary .md-nav__title .md-nav__icon{display:none}[dir=ltr] .md-nav--primary .md-nav__list{padding-left:.6rem}[dir=rtl] .md-nav--primary .md-nav__list{padding-right:.6rem}.md-nav--primary .md-nav__list{padding-bottom:.4rem}[dir=ltr] .md-nav--primary .md-nav__item>.md-nav__link{margin-right:.4rem}[dir=rtl] .md-nav--primary .md-nav__item>.md-nav__link{margin-left:.4rem}.md-nav__toggle~.md-nav{display:grid;grid-template-rows:minmax(.4rem,0fr);opacity:0;transition:grid-template-rows .25s cubic-bezier(.86,0,.07,1),opacity .25s,visibility 0ms .25s;visibility:collapse}.md-nav__toggle~.md-nav>.md-nav__list{overflow:hidden}.md-nav__toggle.md-toggle--indeterminate~.md-nav,.md-nav__toggle:checked~.md-nav{grid-template-rows:minmax(.4rem,1fr);opacity:1;transition:grid-template-rows .25s cubic-bezier(.86,0,.07,1),opacity .15s .1s,visibility 0ms;visibility:visible}.md-nav__toggle.md-toggle--indeterminate~.md-nav{transition:none}.md-nav__item--nested>.md-nav>.md-nav__title{display:none}.md-nav__item--section{display:block;margin:1.25em 0}.md-nav__item--section:last-child{margin-bottom:0}.md-nav__item--section>.md-nav__link{font-weight:700}.md-nav__item--section>.md-nav__link[for]{color:var(--md-default-fg-color--light)}.md-nav__item--section>.md-nav__link:not(.md-nav__container){pointer-events:none}.md-nav__item--section>.md-nav__link .md-icon,.md-nav__item--section>.md-nav__link>[for]{display:none}[dir=ltr] .md-nav__item--section>.md-nav{margin-left:-.6rem}[dir=rtl] .md-nav__item--section>.md-nav{margin-right:-.6rem}.md-nav__item--section>.md-nav{display:block;opacity:1;visibility:visible}.md-nav__item--section>.md-nav>.md-nav__list>.md-nav__item{padding:0}.md-nav__icon{border-radius:100%;height:.9rem;transition:background-color .25s;width:.9rem}.md-nav__icon:hover{background-color:var(--md-accent-fg-color--transparent)}.md-nav__icon:after{background-color:currentcolor;border-radius:100%;content:"";display:inline-block;height:100%;-webkit-mask-image:var(--md-nav-icon--next);mask-image:var(--md-nav-icon--next);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;transition:transform .25s;vertical-align:-.1rem;width:100%}[dir=rtl] .md-nav__icon:after{transform:rotate(180deg)}.md-nav__item--nested .md-nav__toggle:checked~.md-nav__link .md-nav__icon:after,.md-nav__item--nested .md-toggle--indeterminate~.md-nav__link .md-nav__icon:after{transform:rotate(90deg)}.md-nav--lifted>.md-nav__list>.md-nav__item,.md-nav--lifted>.md-nav__title{display:none}.md-nav--lifted>.md-nav__list>.md-nav__item--active{display:block}.md-nav--lifted>.md-nav__list>.md-nav__item--active>.md-nav__link{background:var(--md-default-bg-color);box-shadow:0 0 .4rem .4rem var(--md-default-bg-color);margin-top:0;position:sticky;top:0;z-index:1}.md-nav--lifted>.md-nav__list>.md-nav__item--active>.md-nav__link:not(.md-nav__container){pointer-events:none}.md-nav--lifted>.md-nav__list>.md-nav__item--active.md-nav__item--section{margin:0}[dir=ltr] .md-nav--lifted>.md-nav__list>.md-nav__item>.md-nav:not(.md-nav--secondary){margin-left:-.6rem}[dir=rtl] .md-nav--lifted>.md-nav__list>.md-nav__item>.md-nav:not(.md-nav--secondary){margin-right:-.6rem}.md-nav--lifted>.md-nav__list>.md-nav__item>[for]{color:var(--md-default-fg-color--light)}.md-nav--lifted .md-nav[data-md-level="1"]{grid-template-rows:minmax(.4rem,1fr);opacity:1;visibility:visible}[dir=ltr] .md-nav--integrated>.md-nav__list>.md-nav__item--active .md-nav--secondary{border-left:.05rem solid var(--md-primary-fg-color)}[dir=rtl] .md-nav--integrated>.md-nav__list>.md-nav__item--active .md-nav--secondary{border-right:.05rem solid var(--md-primary-fg-color)}.md-nav--integrated>.md-nav__list>.md-nav__item--active .md-nav--secondary{display:block;margin-bottom:1.25em;opacity:1;visibility:visible}.md-nav--integrated>.md-nav__list>.md-nav__item--active .md-nav--secondary>.md-nav__list{overflow:visible;padding-bottom:0}.md-nav--integrated>.md-nav__list>.md-nav__item--active .md-nav--secondary>.md-nav__title{display:none}}.md-pagination{font-size:.8rem;font-weight:700;gap:.4rem}.md-pagination,.md-pagination>*{align-items:center;display:flex;justify-content:center}.md-pagination>*{border-radius:.2rem;height:1.8rem;min-width:1.8rem;text-align:center}.md-pagination__current{background-color:var(--md-default-fg-color--lightest);color:var(--md-default-fg-color--light)}.md-pagination__link{transition:color 125ms,background-color 125ms}.md-pagination__link:focus,.md-pagination__link:hover{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}.md-pagination__link:focus svg,.md-pagination__link:hover svg{color:var(--md-accent-fg-color)}.md-pagination__link.focus-visible{outline-color:var(--md-accent-fg-color);outline-offset:.2rem}.md-pagination__link svg{fill:currentcolor;color:var(--md-default-fg-color--lighter);display:block;max-height:100%;width:1.2rem}:root{--md-path-icon:url('data:image/svg+xml;charset=utf-8,')}.md-path{font-size:.7rem;margin:0 .8rem;overflow:auto;padding-top:1.2rem}.md-path:not([hidden]){display:block}@media screen and (min-width:76.25em){.md-path{margin:0 1.2rem}}.md-path__list{align-items:center;display:flex;gap:.2rem;list-style:none;margin:0;padding:0}.md-path__item:not(:first-child){display:inline-flex;gap:.2rem;white-space:nowrap}.md-path__item:not(:first-child):before{background-color:var(--md-default-fg-color--lighter);content:"";display:inline;height:.8rem;-webkit-mask-image:var(--md-path-icon);mask-image:var(--md-path-icon);width:.8rem}.md-path__link{align-items:center;color:var(--md-default-fg-color--light);display:flex}.md-path__link:focus,.md-path__link:hover{color:var(--md-accent-fg-color)}:root{--md-post-pin-icon:url('data:image/svg+xml;charset=utf-8,')}.md-post__back{border-bottom:.05rem solid var(--md-default-fg-color--lightest);margin-bottom:1.2rem;padding-bottom:1.2rem}@media screen and (max-width:76.234375em){.md-post__back{display:none}}[dir=rtl] .md-post__back svg{transform:scaleX(-1)}.md-post__authors{display:flex;flex-direction:column;gap:.6rem;margin:0 .6rem 1.2rem}.md-post .md-post__meta a{transition:color 125ms}.md-post .md-post__meta a:focus,.md-post .md-post__meta a:hover{color:var(--md-accent-fg-color)}.md-post__title{color:var(--md-default-fg-color--light);font-weight:700}.md-post--excerpt{margin-bottom:3.2rem}.md-post--excerpt .md-post__header{align-items:center;display:flex;gap:.6rem;min-height:1.6rem}.md-post--excerpt .md-post__authors{align-items:center;display:inline-flex;flex-direction:row;gap:.2rem;margin:0;min-height:2.4rem}[dir=ltr] .md-post--excerpt .md-post__meta .md-meta__list{margin-right:.4rem}[dir=rtl] .md-post--excerpt .md-post__meta .md-meta__list{margin-left:.4rem}.md-post--excerpt .md-post__content>:first-child{--md-scroll-margin:6rem;margin-top:0}.md-post>.md-nav--secondary{margin:1em 0}.md-pin{background:var(--md-default-fg-color--lightest);border-radius:1rem;margin-top:-.05rem;padding:.2rem}.md-pin:after{background-color:currentcolor;content:"";display:block;height:.6rem;margin:0 auto;-webkit-mask-image:var(--md-post-pin-icon);mask-image:var(--md-post-pin-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:.6rem}.md-profile{align-items:center;display:flex;font-size:.7rem;gap:.6rem;line-height:1.4;width:100%}.md-profile__description{flex-grow:1}.md-content--post{display:flex}@media screen and (max-width:76.234375em){.md-content--post{flex-flow:column-reverse}}.md-content--post>.md-content__inner{flex-grow:1;min-width:0}@media screen and (min-width:76.25em){[dir=ltr] .md-content--post>.md-content__inner{margin-left:1.2rem}[dir=rtl] .md-content--post>.md-content__inner{margin-right:1.2rem}}@media screen and (max-width:76.234375em){.md-sidebar.md-sidebar--post{padding:0;position:static;width:100%}.md-sidebar.md-sidebar--post .md-sidebar__scrollwrap{overflow:visible}.md-sidebar.md-sidebar--post .md-sidebar__inner{padding:0}.md-sidebar.md-sidebar--post .md-post__meta{margin-left:.6rem;margin-right:.6rem}.md-sidebar.md-sidebar--post .md-nav__item{border:none;display:inline}.md-sidebar.md-sidebar--post .md-nav__list{display:inline-flex;flex-wrap:wrap;gap:.6rem;padding-bottom:.6rem;padding-top:.6rem}.md-sidebar.md-sidebar--post .md-nav__link{padding:0}.md-sidebar.md-sidebar--post .md-nav{height:auto;margin-bottom:0;position:static}}:root{--md-progress-value:0;--md-progress-delay:400ms}.md-progress{background:var(--md-primary-bg-color);height:.075rem;opacity:min(clamp(0,var(--md-progress-value),1),clamp(0,100 - var(--md-progress-value),1));position:fixed;top:0;transform:scaleX(calc(var(--md-progress-value)*1%));transform-origin:left;transition:transform .5s cubic-bezier(.19,1,.22,1),opacity .25s var(--md-progress-delay);width:100%;z-index:4}:root{--md-search-result-icon:url('data:image/svg+xml;charset=utf-8,')}.md-search{position:relative}@media screen and (min-width:60em){.md-search{padding:.2rem 0}}.no-js .md-search{display:none}.md-search__overlay{opacity:0;z-index:1}@media screen and (max-width:59.984375em){[dir=ltr] .md-search__overlay{left:-2.2rem}[dir=rtl] .md-search__overlay{right:-2.2rem}.md-search__overlay{background-color:var(--md-default-bg-color);border-radius:1rem;height:2rem;overflow:hidden;pointer-events:none;position:absolute;top:-1rem;transform-origin:center;transition:transform .3s .1s,opacity .2s .2s;width:2rem}[data-md-toggle=search]:checked~.md-header .md-search__overlay{opacity:1;transition:transform .4s,opacity .1s}}@media screen and (min-width:60em){[dir=ltr] .md-search__overlay{left:0}[dir=rtl] .md-search__overlay{right:0}.md-search__overlay{background-color:#0000008a;cursor:pointer;height:0;position:fixed;top:0;transition:width 0ms .25s,height 0ms .25s,opacity .25s;width:0}[data-md-toggle=search]:checked~.md-header .md-search__overlay{height:200vh;opacity:1;transition:width 0ms,height 0ms,opacity .25s;width:100%}}@media screen and (max-width:29.984375em){[data-md-toggle=search]:checked~.md-header .md-search__overlay{transform:scale(45)}}@media screen and (min-width:30em) and (max-width:44.984375em){[data-md-toggle=search]:checked~.md-header .md-search__overlay{transform:scale(60)}}@media screen and (min-width:45em) and (max-width:59.984375em){[data-md-toggle=search]:checked~.md-header .md-search__overlay{transform:scale(75)}}.md-search__inner{backface-visibility:hidden}@media screen and (max-width:59.984375em){[dir=ltr] .md-search__inner{left:0}[dir=rtl] .md-search__inner{right:0}.md-search__inner{height:0;opacity:0;overflow:hidden;position:fixed;top:0;transform:translateX(5%);transition:width 0ms .3s,height 0ms .3s,transform .15s cubic-bezier(.4,0,.2,1) .15s,opacity .15s .15s;width:0;z-index:2}[dir=rtl] .md-search__inner{transform:translateX(-5%)}[data-md-toggle=search]:checked~.md-header .md-search__inner{height:100%;opacity:1;transform:translateX(0);transition:width 0ms 0ms,height 0ms 0ms,transform .15s cubic-bezier(.1,.7,.1,1) .15s,opacity .15s .15s;width:100%}}@media screen and (min-width:60em){[dir=ltr] .md-search__inner{float:right}[dir=rtl] .md-search__inner{float:left}.md-search__inner{padding:.1rem 0;position:relative;transition:width .25s cubic-bezier(.1,.7,.1,1);width:11.7rem}}@media screen and (min-width:60em) and (max-width:76.234375em){[data-md-toggle=search]:checked~.md-header .md-search__inner{width:23.4rem}}@media screen and (min-width:76.25em){[data-md-toggle=search]:checked~.md-header .md-search__inner{width:34.4rem}}.md-search__form{background-color:var(--md-default-bg-color);box-shadow:0 0 .6rem #0000;height:2.4rem;position:relative;transition:color .25s,background-color .25s;z-index:2}@media screen and (min-width:60em){.md-search__form{background-color:#00000042;border-radius:.1rem;height:1.8rem}.md-search__form:hover{background-color:#ffffff1f}}[data-md-toggle=search]:checked~.md-header .md-search__form{background-color:var(--md-default-bg-color);border-radius:.1rem .1rem 0 0;box-shadow:0 0 .6rem #00000012;color:var(--md-default-fg-color)}[dir=ltr] .md-search__input{padding-left:3.6rem;padding-right:2.2rem}[dir=rtl] .md-search__input{padding-left:2.2rem;padding-right:3.6rem}.md-search__input{background:#0000;font-size:.9rem;height:100%;position:relative;text-overflow:ellipsis;width:100%;z-index:2}.md-search__input::placeholder{transition:color .25s}.md-search__input::placeholder,.md-search__input~.md-search__icon{color:var(--md-default-fg-color--light)}.md-search__input::-ms-clear{display:none}@media screen and (max-width:59.984375em){.md-search__input{font-size:.9rem;height:2.4rem;width:100%}}@media screen and (min-width:60em){[dir=ltr] .md-search__input{padding-left:2.2rem}[dir=rtl] .md-search__input{padding-right:2.2rem}.md-search__input{color:inherit;font-size:.8rem}.md-search__input::placeholder{color:var(--md-primary-bg-color--light)}.md-search__input+.md-search__icon{color:var(--md-primary-bg-color)}[data-md-toggle=search]:checked~.md-header .md-search__input{text-overflow:clip}[data-md-toggle=search]:checked~.md-header .md-search__input+.md-search__icon{color:var(--md-default-fg-color--light)}[data-md-toggle=search]:checked~.md-header .md-search__input::placeholder{color:#0000}}.md-search__icon{cursor:pointer;display:inline-block;height:1.2rem;transition:color .25s,opacity .25s;width:1.2rem}.md-search__icon:hover{opacity:.7}[dir=ltr] .md-search__icon[for=__search]{left:.5rem}[dir=rtl] .md-search__icon[for=__search]{right:.5rem}.md-search__icon[for=__search]{position:absolute;top:.3rem;z-index:2}[dir=rtl] .md-search__icon[for=__search] svg{transform:scaleX(-1)}@media screen and (max-width:59.984375em){[dir=ltr] .md-search__icon[for=__search]{left:.8rem}[dir=rtl] .md-search__icon[for=__search]{right:.8rem}.md-search__icon[for=__search]{top:.6rem}.md-search__icon[for=__search] svg:first-child{display:none}}@media screen and (min-width:60em){.md-search__icon[for=__search]{pointer-events:none}.md-search__icon[for=__search] svg:last-child{display:none}}[dir=ltr] .md-search__options{right:.5rem}[dir=rtl] .md-search__options{left:.5rem}.md-search__options{pointer-events:none;position:absolute;top:.3rem;z-index:2}@media screen and (max-width:59.984375em){[dir=ltr] .md-search__options{right:.8rem}[dir=rtl] .md-search__options{left:.8rem}.md-search__options{top:.6rem}}[dir=ltr] .md-search__options>.md-icon{margin-left:.2rem}[dir=rtl] .md-search__options>.md-icon{margin-right:.2rem}.md-search__options>.md-icon{color:var(--md-default-fg-color--light);opacity:0;transform:scale(.75);transition:transform .15s cubic-bezier(.1,.7,.1,1),opacity .15s}.md-search__options>.md-icon:not(.focus-visible){-webkit-tap-highlight-color:transparent;outline:none}[data-md-toggle=search]:checked~.md-header .md-search__input:valid~.md-search__options>.md-icon{opacity:1;pointer-events:auto;transform:scale(1)}[data-md-toggle=search]:checked~.md-header .md-search__input:valid~.md-search__options>.md-icon:hover{opacity:.7}[dir=ltr] .md-search__suggest{padding-left:3.6rem;padding-right:2.2rem}[dir=rtl] .md-search__suggest{padding-left:2.2rem;padding-right:3.6rem}.md-search__suggest{align-items:center;color:var(--md-default-fg-color--lighter);display:flex;font-size:.9rem;height:100%;opacity:0;position:absolute;top:0;transition:opacity 50ms;white-space:nowrap;width:100%}@media screen and (min-width:60em){[dir=ltr] .md-search__suggest{padding-left:2.2rem}[dir=rtl] .md-search__suggest{padding-right:2.2rem}.md-search__suggest{font-size:.8rem}}[data-md-toggle=search]:checked~.md-header .md-search__suggest{opacity:1;transition:opacity .3s .1s}[dir=ltr] .md-search__output{border-bottom-left-radius:.1rem}[dir=ltr] .md-search__output,[dir=rtl] .md-search__output{border-bottom-right-radius:.1rem}[dir=rtl] .md-search__output{border-bottom-left-radius:.1rem}.md-search__output{overflow:hidden;position:absolute;width:100%;z-index:1}@media screen and (max-width:59.984375em){.md-search__output{bottom:0;top:2.4rem}}@media screen and (min-width:60em){.md-search__output{opacity:0;top:1.9rem;transition:opacity .4s}[data-md-toggle=search]:checked~.md-header .md-search__output{box-shadow:var(--md-shadow-z3);opacity:1}}.md-search__scrollwrap{backface-visibility:hidden;background-color:var(--md-default-bg-color);height:100%;overflow-y:auto;touch-action:pan-y}@media (-webkit-max-device-pixel-ratio:1),(max-resolution:1dppx){.md-search__scrollwrap{transform:translateZ(0)}}@media screen and (min-width:60em) and (max-width:76.234375em){.md-search__scrollwrap{width:23.4rem}}@media screen and (min-width:76.25em){.md-search__scrollwrap{width:34.4rem}}@media screen and (min-width:60em){.md-search__scrollwrap{max-height:0;scrollbar-color:var(--md-default-fg-color--lighter) #0000;scrollbar-width:thin}[data-md-toggle=search]:checked~.md-header .md-search__scrollwrap{max-height:75vh}.md-search__scrollwrap:hover{scrollbar-color:var(--md-accent-fg-color) #0000}.md-search__scrollwrap::-webkit-scrollbar{height:.2rem;width:.2rem}.md-search__scrollwrap::-webkit-scrollbar-thumb{background-color:var(--md-default-fg-color--lighter)}.md-search__scrollwrap::-webkit-scrollbar-thumb:hover{background-color:var(--md-accent-fg-color)}}.md-search-result{color:var(--md-default-fg-color);word-break:break-word}.md-search-result__meta{background-color:var(--md-default-fg-color--lightest);color:var(--md-default-fg-color--light);font-size:.64rem;line-height:1.8rem;padding:0 .8rem;scroll-snap-align:start}@media screen and (min-width:60em){[dir=ltr] .md-search-result__meta{padding-left:2.2rem}[dir=rtl] .md-search-result__meta{padding-right:2.2rem}}.md-search-result__list{list-style:none;margin:0;padding:0;-webkit-user-select:none;user-select:none}.md-search-result__item{box-shadow:0 -.05rem var(--md-default-fg-color--lightest)}.md-search-result__item:first-child{box-shadow:none}.md-search-result__link{display:block;outline:none;scroll-snap-align:start;transition:background-color .25s}.md-search-result__link:focus,.md-search-result__link:hover{background-color:var(--md-accent-fg-color--transparent)}.md-search-result__link:last-child p:last-child{margin-bottom:.6rem}.md-search-result__more>summary{cursor:pointer;display:block;outline:none;position:sticky;scroll-snap-align:start;top:0;z-index:1}.md-search-result__more>summary::marker{display:none}.md-search-result__more>summary::-webkit-details-marker{display:none}.md-search-result__more>summary>div{color:var(--md-typeset-a-color);font-size:.64rem;padding:.75em .8rem;transition:color .25s,background-color .25s}@media screen and (min-width:60em){[dir=ltr] .md-search-result__more>summary>div{padding-left:2.2rem}[dir=rtl] .md-search-result__more>summary>div{padding-right:2.2rem}}.md-search-result__more>summary:focus>div,.md-search-result__more>summary:hover>div{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}.md-search-result__more[open]>summary{background-color:var(--md-default-bg-color)}.md-search-result__article{overflow:hidden;padding:0 .8rem;position:relative}@media screen and (min-width:60em){[dir=ltr] .md-search-result__article{padding-left:2.2rem}[dir=rtl] .md-search-result__article{padding-right:2.2rem}}[dir=ltr] .md-search-result__icon{left:0}[dir=rtl] .md-search-result__icon{right:0}.md-search-result__icon{color:var(--md-default-fg-color--light);height:1.2rem;margin:.5rem;position:absolute;width:1.2rem}@media screen and (max-width:59.984375em){.md-search-result__icon{display:none}}.md-search-result__icon:after{background-color:currentcolor;content:"";display:inline-block;height:100%;-webkit-mask-image:var(--md-search-result-icon);mask-image:var(--md-search-result-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:100%}[dir=rtl] .md-search-result__icon:after{transform:scaleX(-1)}.md-search-result .md-typeset{color:var(--md-default-fg-color--light);font-size:.64rem;line-height:1.6}.md-search-result .md-typeset h1{color:var(--md-default-fg-color);font-size:.8rem;font-weight:400;line-height:1.4;margin:.55rem 0}.md-search-result .md-typeset h1 mark{text-decoration:none}.md-search-result .md-typeset h2{color:var(--md-default-fg-color);font-size:.64rem;font-weight:700;line-height:1.6;margin:.5em 0}.md-search-result .md-typeset h2 mark{text-decoration:none}.md-search-result__terms{color:var(--md-default-fg-color);display:block;font-size:.64rem;font-style:italic;margin:.5em 0}.md-search-result mark{background-color:initial;color:var(--md-accent-fg-color);text-decoration:underline}.md-select{position:relative;z-index:1}.md-select__inner{background-color:var(--md-default-bg-color);border-radius:.1rem;box-shadow:var(--md-shadow-z2);color:var(--md-default-fg-color);left:50%;margin-top:.2rem;max-height:0;opacity:0;position:absolute;top:calc(100% - .2rem);transform:translate3d(-50%,.3rem,0);transition:transform .25s 375ms,opacity .25s .25s,max-height 0ms .5s}.md-select:focus-within .md-select__inner,.md-select:hover .md-select__inner{max-height:10rem;opacity:1;transform:translate3d(-50%,0,0);transition:transform .25s cubic-bezier(.1,.7,.1,1),opacity .25s,max-height 0ms}.md-select__inner:after{border-bottom:.2rem solid #0000;border-bottom-color:var(--md-default-bg-color);border-left:.2rem solid #0000;border-right:.2rem solid #0000;border-top:0;content:"";height:0;left:50%;margin-left:-.2rem;margin-top:-.2rem;position:absolute;top:0;width:0}.md-select__list{border-radius:.1rem;font-size:.8rem;list-style-type:none;margin:0;max-height:inherit;overflow:auto;padding:0}.md-select__item{line-height:1.8rem}[dir=ltr] .md-select__link{padding-left:.6rem;padding-right:1.2rem}[dir=rtl] .md-select__link{padding-left:1.2rem;padding-right:.6rem}.md-select__link{cursor:pointer;display:block;outline:none;scroll-snap-align:start;transition:background-color .25s,color .25s;width:100%}.md-select__link:focus,.md-select__link:hover{color:var(--md-accent-fg-color)}.md-select__link:focus{background-color:var(--md-default-fg-color--lightest)}.md-sidebar{align-self:flex-start;flex-shrink:0;padding:1.2rem 0;position:sticky;top:2.4rem;width:12.1rem}@media print{.md-sidebar{display:none}}@media screen and (max-width:76.234375em){[dir=ltr] .md-sidebar--primary{left:-12.1rem}[dir=rtl] .md-sidebar--primary{right:-12.1rem}.md-sidebar--primary{background-color:var(--md-default-bg-color);display:block;height:100%;position:fixed;top:0;transform:translateX(0);transition:transform .25s cubic-bezier(.4,0,.2,1),box-shadow .25s;width:12.1rem;z-index:5}[data-md-toggle=drawer]:checked~.md-container .md-sidebar--primary{box-shadow:var(--md-shadow-z3);transform:translateX(12.1rem)}[dir=rtl] [data-md-toggle=drawer]:checked~.md-container .md-sidebar--primary{transform:translateX(-12.1rem)}.md-sidebar--primary .md-sidebar__scrollwrap{bottom:0;left:0;margin:0;overflow:hidden;position:absolute;right:0;scroll-snap-type:none;top:0}}@media screen and (min-width:76.25em){.md-sidebar{height:0}.no-js .md-sidebar{height:auto}.md-header--lifted~.md-container .md-sidebar{top:4.8rem}}.md-sidebar--secondary{display:none;order:2}@media screen and (min-width:60em){.md-sidebar--secondary{height:0}.no-js .md-sidebar--secondary{height:auto}.md-sidebar--secondary:not([hidden]){display:block}.md-sidebar--secondary .md-sidebar__scrollwrap{touch-action:pan-y}}.md-sidebar__scrollwrap{backface-visibility:hidden;margin:0 .2rem;overflow-y:auto;scrollbar-color:var(--md-default-fg-color--lighter) #0000}@media screen and (min-width:60em){.md-sidebar__scrollwrap{scrollbar-gutter:stable;scrollbar-width:thin}}.md-sidebar__scrollwrap::-webkit-scrollbar{height:.2rem;width:.2rem}.md-sidebar__scrollwrap:focus-within,.md-sidebar__scrollwrap:hover{scrollbar-color:var(--md-accent-fg-color) #0000}.md-sidebar__scrollwrap:focus-within::-webkit-scrollbar-thumb,.md-sidebar__scrollwrap:hover::-webkit-scrollbar-thumb{background-color:var(--md-default-fg-color--lighter)}.md-sidebar__scrollwrap:focus-within::-webkit-scrollbar-thumb:hover,.md-sidebar__scrollwrap:hover::-webkit-scrollbar-thumb:hover{background-color:var(--md-accent-fg-color)}@supports selector(::-webkit-scrollbar){.md-sidebar__scrollwrap{scrollbar-gutter:auto}[dir=ltr] .md-sidebar__inner{padding-right:calc(100% - 11.5rem)}[dir=rtl] .md-sidebar__inner{padding-left:calc(100% - 11.5rem)}}@media screen and (max-width:76.234375em){.md-overlay{background-color:#0000008a;height:0;opacity:0;position:fixed;top:0;transition:width 0ms .25s,height 0ms .25s,opacity .25s;width:0;z-index:5}[data-md-toggle=drawer]:checked~.md-overlay{height:100%;opacity:1;transition:width 0ms,height 0ms,opacity .25s;width:100%}}@keyframes facts{0%{height:0}to{height:.65rem}}@keyframes fact{0%{opacity:0;transform:translateY(100%)}50%{opacity:0}to{opacity:1;transform:translateY(0)}}:root{--md-source-forks-icon:url('data:image/svg+xml;charset=utf-8,');--md-source-repositories-icon:url('data:image/svg+xml;charset=utf-8,');--md-source-stars-icon:url('data:image/svg+xml;charset=utf-8,');--md-source-version-icon:url('data:image/svg+xml;charset=utf-8,')}.md-source{backface-visibility:hidden;display:block;font-size:.65rem;line-height:1.2;outline-color:var(--md-accent-fg-color);transition:opacity .25s;white-space:nowrap}.md-source:hover{opacity:.7}.md-source__icon{display:inline-block;height:2.4rem;vertical-align:middle;width:2rem}[dir=ltr] .md-source__icon svg{margin-left:.6rem}[dir=rtl] .md-source__icon svg{margin-right:.6rem}.md-source__icon svg{margin-top:.6rem}[dir=ltr] .md-source__icon+.md-source__repository{padding-left:2rem}[dir=rtl] .md-source__icon+.md-source__repository{padding-right:2rem}[dir=ltr] .md-source__icon+.md-source__repository{margin-left:-2rem}[dir=rtl] .md-source__icon+.md-source__repository{margin-right:-2rem}[dir=ltr] .md-source__repository{margin-left:.6rem}[dir=rtl] .md-source__repository{margin-right:.6rem}.md-source__repository{display:inline-block;max-width:calc(100% - 1.2rem);overflow:hidden;text-overflow:ellipsis;vertical-align:middle}.md-source__facts{display:flex;font-size:.55rem;gap:.4rem;list-style-type:none;margin:.1rem 0 0;opacity:.75;overflow:hidden;padding:0;width:100%}.md-source__repository--active .md-source__facts{animation:facts .25s ease-in}.md-source__fact{overflow:hidden;text-overflow:ellipsis}.md-source__repository--active .md-source__fact{animation:fact .4s ease-out}[dir=ltr] .md-source__fact:before{margin-right:.1rem}[dir=rtl] .md-source__fact:before{margin-left:.1rem}.md-source__fact:before{background-color:currentcolor;content:"";display:inline-block;height:.6rem;-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;vertical-align:text-top;width:.6rem}.md-source__fact:nth-child(1n+2){flex-shrink:0}.md-source__fact--version:before{-webkit-mask-image:var(--md-source-version-icon);mask-image:var(--md-source-version-icon)}.md-source__fact--stars:before{-webkit-mask-image:var(--md-source-stars-icon);mask-image:var(--md-source-stars-icon)}.md-source__fact--forks:before{-webkit-mask-image:var(--md-source-forks-icon);mask-image:var(--md-source-forks-icon)}.md-source__fact--repositories:before{-webkit-mask-image:var(--md-source-repositories-icon);mask-image:var(--md-source-repositories-icon)}.md-source-file{margin:1em 0}[dir=ltr] .md-source-file__fact{margin-right:.6rem}[dir=rtl] .md-source-file__fact{margin-left:.6rem}.md-source-file__fact{align-items:center;color:var(--md-default-fg-color--light);display:inline-flex;font-size:.68rem;gap:.3rem}.md-source-file__fact .md-icon{flex-shrink:0;margin-bottom:.05rem}[dir=ltr] .md-source-file__fact .md-author{float:left}[dir=rtl] .md-source-file__fact .md-author{float:right}.md-source-file__fact .md-author{margin-right:.2rem}.md-source-file__fact svg{width:.9rem}:root{--md-status:url('data:image/svg+xml;charset=utf-8,');--md-status--new:url('data:image/svg+xml;charset=utf-8,');--md-status--deprecated:url('data:image/svg+xml;charset=utf-8,');--md-status--encrypted:url('data:image/svg+xml;charset=utf-8,')}.md-status:after{background-color:var(--md-default-fg-color--light);content:"";display:inline-block;height:1.125em;-webkit-mask-image:var(--md-status);mask-image:var(--md-status);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;vertical-align:text-bottom;width:1.125em}.md-status:hover:after{background-color:currentcolor}.md-status--new:after{-webkit-mask-image:var(--md-status--new);mask-image:var(--md-status--new)}.md-status--deprecated:after{-webkit-mask-image:var(--md-status--deprecated);mask-image:var(--md-status--deprecated)}.md-status--encrypted:after{-webkit-mask-image:var(--md-status--encrypted);mask-image:var(--md-status--encrypted)}.md-tabs{background-color:var(--md-primary-fg-color);color:var(--md-primary-bg-color);display:block;line-height:1.3;overflow:auto;width:100%;z-index:3}@media print{.md-tabs{display:none}}@media screen and (max-width:76.234375em){.md-tabs{display:none}}.md-tabs[hidden]{pointer-events:none}[dir=ltr] .md-tabs__list{margin-left:.2rem}[dir=rtl] .md-tabs__list{margin-right:.2rem}.md-tabs__list{contain:content;display:flex;list-style:none;margin:0;overflow:auto;padding:0;scrollbar-width:none;white-space:nowrap}.md-tabs__list::-webkit-scrollbar{display:none}.md-tabs__item{height:2.4rem;padding-left:.6rem;padding-right:.6rem}.md-tabs__item--active .md-tabs__link{color:inherit;opacity:1}.md-tabs__link{backface-visibility:hidden;display:flex;font-size:.7rem;margin-top:.8rem;opacity:.7;outline-color:var(--md-accent-fg-color);outline-offset:.2rem;transition:transform .4s cubic-bezier(.1,.7,.1,1),opacity .25s}.md-tabs__link:focus,.md-tabs__link:hover{color:inherit;opacity:1}[dir=ltr] .md-tabs__link svg{margin-right:.4rem}[dir=rtl] .md-tabs__link svg{margin-left:.4rem}.md-tabs__link svg{fill:currentcolor;height:1.3em}.md-tabs__item:nth-child(2) .md-tabs__link{transition-delay:20ms}.md-tabs__item:nth-child(3) .md-tabs__link{transition-delay:40ms}.md-tabs__item:nth-child(4) .md-tabs__link{transition-delay:60ms}.md-tabs__item:nth-child(5) .md-tabs__link{transition-delay:80ms}.md-tabs__item:nth-child(6) .md-tabs__link{transition-delay:.1s}.md-tabs__item:nth-child(7) .md-tabs__link{transition-delay:.12s}.md-tabs__item:nth-child(8) .md-tabs__link{transition-delay:.14s}.md-tabs__item:nth-child(9) .md-tabs__link{transition-delay:.16s}.md-tabs__item:nth-child(10) .md-tabs__link{transition-delay:.18s}.md-tabs__item:nth-child(11) .md-tabs__link{transition-delay:.2s}.md-tabs__item:nth-child(12) .md-tabs__link{transition-delay:.22s}.md-tabs__item:nth-child(13) .md-tabs__link{transition-delay:.24s}.md-tabs__item:nth-child(14) .md-tabs__link{transition-delay:.26s}.md-tabs__item:nth-child(15) .md-tabs__link{transition-delay:.28s}.md-tabs__item:nth-child(16) .md-tabs__link{transition-delay:.3s}.md-tabs[hidden] .md-tabs__link{opacity:0;transform:translateY(50%);transition:transform 0ms .1s,opacity .1s}:root{--md-tag-icon:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .md-tags:not([hidden]){display:inline-flex;flex-wrap:wrap;gap:.5em;margin-bottom:.75em;margin-top:-.125em}.md-typeset .md-tag{align-items:center;background:var(--md-default-fg-color--lightest);border-radius:2.4rem;display:inline-flex;font-size:.64rem;font-size:min(.8em,.64rem);font-weight:700;gap:.5em;letter-spacing:normal;line-height:1.6;padding:.3125em .78125em}.md-typeset .md-tag[href]{-webkit-tap-highlight-color:transparent;color:inherit;outline:none;transition:color 125ms,background-color 125ms}.md-typeset .md-tag[href]:focus,.md-typeset .md-tag[href]:hover{background-color:var(--md-accent-fg-color);color:var(--md-accent-bg-color)}[id]>.md-typeset .md-tag{vertical-align:text-top}.md-typeset .md-tag-shadow{opacity:.5}.md-typeset .md-tag-icon:before{background-color:var(--md-default-fg-color--lighter);content:"";display:inline-block;height:1.2em;-webkit-mask-image:var(--md-tag-icon);mask-image:var(--md-tag-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;transition:background-color 125ms;vertical-align:text-bottom;width:1.2em}.md-typeset .md-tag-icon[href]:focus:before,.md-typeset .md-tag-icon[href]:hover:before{background-color:var(--md-accent-bg-color)}@keyframes pulse{0%{transform:scale(.95)}75%{transform:scale(1)}to{transform:scale(.95)}}:root{--md-annotation-bg-icon:url('data:image/svg+xml;charset=utf-8,');--md-annotation-icon:url('data:image/svg+xml;charset=utf-8,')}.md-tooltip{backface-visibility:hidden;background-color:var(--md-default-bg-color);border-radius:.1rem;box-shadow:var(--md-shadow-z2);color:var(--md-default-fg-color);font-family:var(--md-text-font-family);left:clamp(var(--md-tooltip-0,0rem) + .8rem,var(--md-tooltip-x),100vw + var(--md-tooltip-0,0rem) + .8rem - var(--md-tooltip-width) - 2 * .8rem);max-width:calc(100vw - 1.6rem);opacity:0;position:absolute;top:var(--md-tooltip-y);transform:translateY(-.4rem);transition:transform 0ms .25s,opacity .25s,z-index .25s;width:var(--md-tooltip-width);z-index:0}.md-tooltip--active{opacity:1;transform:translateY(0);transition:transform .25s cubic-bezier(.1,.7,.1,1),opacity .25s,z-index 0ms;z-index:2}.md-tooltip--inline{font-weight:700;-webkit-user-select:none;user-select:none;width:auto}.md-tooltip--inline:not(.md-tooltip--active){transform:translateY(.2rem) scale(.9)}.md-tooltip--inline .md-tooltip__inner{font-size:.5rem;padding:.2rem .4rem}[hidden]+.md-tooltip--inline{display:none}.focus-visible>.md-tooltip,.md-tooltip:target{outline:var(--md-accent-fg-color) auto}.md-tooltip__inner{font-size:.64rem;padding:.8rem}.md-tooltip__inner.md-typeset>:first-child{margin-top:0}.md-tooltip__inner.md-typeset>:last-child{margin-bottom:0}.md-annotation{font-style:normal;font-weight:400;outline:none;text-align:initial;vertical-align:text-bottom;white-space:normal}[dir=rtl] .md-annotation{direction:rtl}code .md-annotation{font-family:var(--md-code-font-family);font-size:inherit}.md-annotation:not([hidden]){display:inline-block;line-height:1.25}.md-annotation__index{border-radius:.01px;cursor:pointer;display:inline-block;margin-left:.4ch;margin-right:.4ch;outline:none;overflow:hidden;position:relative;-webkit-user-select:none;user-select:none;vertical-align:text-top;z-index:0}.md-annotation .md-annotation__index{transition:z-index .25s}@media screen{.md-annotation__index{width:2.2ch}[data-md-visible]>.md-annotation__index{animation:pulse 2s infinite}.md-annotation__index:before{background:var(--md-default-bg-color);-webkit-mask-image:var(--md-annotation-bg-icon);mask-image:var(--md-annotation-bg-icon)}.md-annotation__index:after,.md-annotation__index:before{content:"";height:2.2ch;-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;position:absolute;top:-.1ch;width:2.2ch;z-index:-1}.md-annotation__index:after{background-color:var(--md-default-fg-color--lighter);-webkit-mask-image:var(--md-annotation-icon);mask-image:var(--md-annotation-icon);transform:scale(1.0001);transition:background-color .25s,transform .25s}.md-tooltip--active+.md-annotation__index:after{transform:rotate(45deg)}.md-tooltip--active+.md-annotation__index:after,:hover>.md-annotation__index:after{background-color:var(--md-accent-fg-color)}}.md-tooltip--active+.md-annotation__index{animation-play-state:paused;transition-duration:0ms;z-index:2}.md-annotation__index [data-md-annotation-id]{display:inline-block}@media print{.md-annotation__index [data-md-annotation-id]{background:var(--md-default-fg-color--lighter);border-radius:2ch;color:var(--md-default-bg-color);font-weight:700;padding:0 .6ch;white-space:nowrap}.md-annotation__index [data-md-annotation-id]:after{content:attr(data-md-annotation-id)}}.md-typeset .md-annotation-list{counter-reset:annotation;list-style:none!important}.md-typeset .md-annotation-list li{position:relative}[dir=ltr] .md-typeset .md-annotation-list li:before{left:-2.125em}[dir=rtl] .md-typeset .md-annotation-list li:before{right:-2.125em}.md-typeset .md-annotation-list li:before{background:var(--md-default-fg-color--lighter);border-radius:2ch;color:var(--md-default-bg-color);content:counter(annotation);counter-increment:annotation;font-size:.8875em;font-weight:700;height:2ch;line-height:1.25;min-width:2ch;padding:0 .6ch;position:absolute;text-align:center;top:.25em}:root{--md-tooltip-width:20rem;--md-tooltip-tail:0.3rem}.md-tooltip2{backface-visibility:hidden;color:var(--md-default-fg-color);font-family:var(--md-text-font-family);opacity:0;pointer-events:none;position:absolute;top:calc(var(--md-tooltip-host-y) + var(--md-tooltip-y));transform:translateY(-.4rem);transform-origin:calc(var(--md-tooltip-host-x) + var(--md-tooltip-x)) 0;transition:transform 0ms .25s,opacity .25s,z-index .25s;width:100%;z-index:0}.md-tooltip2:before{border-left:var(--md-tooltip-tail) solid #0000;border-right:var(--md-tooltip-tail) solid #0000;content:"";display:block;left:clamp(1.5 * .8rem,var(--md-tooltip-host-x) + var(--md-tooltip-x) - var(--md-tooltip-tail),100vw - 2 * var(--md-tooltip-tail) - 1.5 * .8rem);position:absolute;z-index:1}.md-tooltip2--top:before{border-top:var(--md-tooltip-tail) solid var(--md-default-bg-color);bottom:calc(var(--md-tooltip-tail)*-1 + .025rem);filter:drop-shadow(0 1px 0 hsla(0,0%,0%,.05))}.md-tooltip2--bottom:before{border-bottom:var(--md-tooltip-tail) solid var(--md-default-bg-color);filter:drop-shadow(0 -1px 0 hsla(0,0%,0%,.05));top:calc(var(--md-tooltip-tail)*-1 + .025rem)}.md-tooltip2--active{opacity:1;transform:translateY(0);transition:transform .4s cubic-bezier(0,1,.5,1),opacity .25s,z-index 0ms;z-index:2}.md-tooltip2__inner{scrollbar-gutter:stable;background-color:var(--md-default-bg-color);border-radius:.1rem;box-shadow:var(--md-shadow-z2);left:clamp(.8rem,var(--md-tooltip-host-x) - .8rem,100vw - var(--md-tooltip-width) - .8rem);max-height:40vh;max-width:calc(100vw - 1.6rem);position:relative;scrollbar-width:thin}.md-tooltip2__inner::-webkit-scrollbar{height:.2rem;width:.2rem}.md-tooltip2__inner::-webkit-scrollbar-thumb{background-color:var(--md-default-fg-color--lighter)}.md-tooltip2__inner::-webkit-scrollbar-thumb:hover{background-color:var(--md-accent-fg-color)}[role=dialog]>.md-tooltip2__inner{font-size:.64rem;overflow:auto;padding:0 .8rem;pointer-events:auto;width:var(--md-tooltip-width)}[role=dialog]>.md-tooltip2__inner:after,[role=dialog]>.md-tooltip2__inner:before{content:"";display:block;height:.8rem;position:sticky;width:100%;z-index:10}[role=dialog]>.md-tooltip2__inner:before{background:linear-gradient(var(--md-default-bg-color),#0000 75%);top:0}[role=dialog]>.md-tooltip2__inner:after{background:linear-gradient(#0000,var(--md-default-bg-color) 75%);bottom:0}[role=tooltip]>.md-tooltip2__inner{font-size:.5rem;font-weight:700;left:clamp(.8rem,var(--md-tooltip-host-x) + var(--md-tooltip-x) - var(--md-tooltip-width)/2,100vw - var(--md-tooltip-width) - .8rem);max-width:min(100vw - 2 * .8rem,400px);padding:.2rem .4rem;-webkit-user-select:none;user-select:none;width:fit-content}.md-tooltip2__inner.md-typeset>:first-child{margin-top:0}.md-tooltip2__inner.md-typeset>:last-child{margin-bottom:0}[dir=ltr] .md-top{margin-left:50%}[dir=rtl] .md-top{margin-right:50%}.md-top{background-color:var(--md-default-bg-color);border-radius:1.6rem;box-shadow:var(--md-shadow-z2);color:var(--md-default-fg-color--light);cursor:pointer;display:block;font-size:.7rem;outline:none;padding:.4rem .8rem;position:fixed;top:3.2rem;transform:translate(-50%);transition:color 125ms,background-color 125ms,transform 125ms cubic-bezier(.4,0,.2,1),opacity 125ms;z-index:2}@media print{.md-top{display:none}}[dir=rtl] .md-top{transform:translate(50%)}.md-top[hidden]{opacity:0;pointer-events:none;transform:translate(-50%,.2rem);transition-duration:0ms}[dir=rtl] .md-top[hidden]{transform:translate(50%,.2rem)}.md-top:focus,.md-top:hover{background-color:var(--md-accent-fg-color);color:var(--md-accent-bg-color)}.md-top svg{display:inline-block;vertical-align:-.5em}@keyframes hoverfix{0%{pointer-events:none}}:root{--md-version-icon:url('data:image/svg+xml;charset=utf-8,')}.md-version{flex-shrink:0;font-size:.8rem;height:2.4rem}[dir=ltr] .md-version__current{margin-left:1.4rem;margin-right:.4rem}[dir=rtl] .md-version__current{margin-left:.4rem;margin-right:1.4rem}.md-version__current{color:inherit;cursor:pointer;outline:none;position:relative;top:.05rem}[dir=ltr] .md-version__current:after{margin-left:.4rem}[dir=rtl] .md-version__current:after{margin-right:.4rem}.md-version__current:after{background-color:currentcolor;content:"";display:inline-block;height:.6rem;-webkit-mask-image:var(--md-version-icon);mask-image:var(--md-version-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:.4rem}.md-version__alias{margin-left:.3rem;opacity:.7}.md-version__list{background-color:var(--md-default-bg-color);border-radius:.1rem;box-shadow:var(--md-shadow-z2);color:var(--md-default-fg-color);list-style-type:none;margin:.2rem .8rem;max-height:0;opacity:0;overflow:auto;padding:0;position:absolute;scroll-snap-type:y mandatory;top:.15rem;transition:max-height 0ms .5s,opacity .25s .25s;z-index:3}.md-version:focus-within .md-version__list,.md-version:hover .md-version__list{max-height:10rem;opacity:1;transition:max-height 0ms,opacity .25s}@media (hover:none),(pointer:coarse){.md-version:hover .md-version__list{animation:hoverfix .25s forwards}.md-version:focus-within .md-version__list{animation:none}}.md-version__item{line-height:1.8rem}[dir=ltr] .md-version__link{padding-left:.6rem;padding-right:1.2rem}[dir=rtl] .md-version__link{padding-left:1.2rem;padding-right:.6rem}.md-version__link{cursor:pointer;display:block;outline:none;scroll-snap-align:start;transition:color .25s,background-color .25s;white-space:nowrap;width:100%}.md-version__link:focus,.md-version__link:hover{color:var(--md-accent-fg-color)}.md-version__link:focus{background-color:var(--md-default-fg-color--lightest)}:root{--md-admonition-icon--note:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--abstract:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--info:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--tip:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--success:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--question:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--warning:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--failure:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--danger:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--bug:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--example:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--quote:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .admonition,.md-typeset details{background-color:var(--md-admonition-bg-color);border:.075rem solid #448aff;border-radius:.2rem;box-shadow:var(--md-shadow-z1);color:var(--md-admonition-fg-color);display:flow-root;font-size:.64rem;margin:1.5625em 0;padding:0 .6rem;page-break-inside:avoid;transition:box-shadow 125ms}@media print{.md-typeset .admonition,.md-typeset details{box-shadow:none}}.md-typeset .admonition:focus-within,.md-typeset details:focus-within{box-shadow:0 0 0 .2rem #448aff1a}.md-typeset .admonition>*,.md-typeset details>*{box-sizing:border-box}.md-typeset .admonition .admonition,.md-typeset .admonition details,.md-typeset details .admonition,.md-typeset details details{margin-bottom:1em;margin-top:1em}.md-typeset .admonition .md-typeset__scrollwrap,.md-typeset details .md-typeset__scrollwrap{margin:1em -.6rem}.md-typeset .admonition .md-typeset__table,.md-typeset details .md-typeset__table{padding:0 .6rem}.md-typeset .admonition>.tabbed-set:only-child,.md-typeset details>.tabbed-set:only-child{margin-top:0}html .md-typeset .admonition>:last-child,html .md-typeset details>:last-child{margin-bottom:.6rem}[dir=ltr] .md-typeset .admonition-title,[dir=ltr] .md-typeset summary{padding-left:2rem;padding-right:.6rem}[dir=rtl] .md-typeset .admonition-title,[dir=rtl] .md-typeset summary{padding-left:.6rem;padding-right:2rem}[dir=ltr] .md-typeset .admonition-title,[dir=ltr] .md-typeset summary{border-left-width:.2rem}[dir=rtl] .md-typeset .admonition-title,[dir=rtl] .md-typeset summary{border-right-width:.2rem}[dir=ltr] .md-typeset .admonition-title,[dir=ltr] .md-typeset summary{border-top-left-radius:.1rem}[dir=ltr] .md-typeset .admonition-title,[dir=ltr] .md-typeset summary,[dir=rtl] .md-typeset .admonition-title,[dir=rtl] .md-typeset summary{border-top-right-radius:.1rem}[dir=rtl] .md-typeset .admonition-title,[dir=rtl] .md-typeset summary{border-top-left-radius:.1rem}.md-typeset .admonition-title,.md-typeset summary{background-color:#448aff1a;border:none;font-weight:700;margin:0 -.6rem;padding-bottom:.4rem;padding-top:.4rem;position:relative}html .md-typeset .admonition-title:last-child,html .md-typeset summary:last-child{margin-bottom:0}[dir=ltr] .md-typeset .admonition-title:before,[dir=ltr] .md-typeset summary:before{left:.6rem}[dir=rtl] .md-typeset .admonition-title:before,[dir=rtl] .md-typeset summary:before{right:.6rem}.md-typeset .admonition-title:before,.md-typeset summary:before{background-color:#448aff;content:"";height:1rem;-webkit-mask-image:var(--md-admonition-icon--note);mask-image:var(--md-admonition-icon--note);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;position:absolute;top:.625em;width:1rem}.md-typeset .admonition-title code,.md-typeset summary code{box-shadow:0 0 0 .05rem var(--md-default-fg-color--lightest)}.md-typeset .admonition.note,.md-typeset details.note{border-color:#448aff}.md-typeset .admonition.note:focus-within,.md-typeset details.note:focus-within{box-shadow:0 0 0 .2rem #448aff1a}.md-typeset .note>.admonition-title,.md-typeset .note>summary{background-color:#448aff1a}.md-typeset .note>.admonition-title:before,.md-typeset .note>summary:before{background-color:#448aff;-webkit-mask-image:var(--md-admonition-icon--note);mask-image:var(--md-admonition-icon--note)}.md-typeset .note>.admonition-title:after,.md-typeset .note>summary:after{color:#448aff}.md-typeset .admonition.abstract,.md-typeset details.abstract{border-color:#00b0ff}.md-typeset .admonition.abstract:focus-within,.md-typeset details.abstract:focus-within{box-shadow:0 0 0 .2rem #00b0ff1a}.md-typeset .abstract>.admonition-title,.md-typeset .abstract>summary{background-color:#00b0ff1a}.md-typeset .abstract>.admonition-title:before,.md-typeset .abstract>summary:before{background-color:#00b0ff;-webkit-mask-image:var(--md-admonition-icon--abstract);mask-image:var(--md-admonition-icon--abstract)}.md-typeset .abstract>.admonition-title:after,.md-typeset .abstract>summary:after{color:#00b0ff}.md-typeset .admonition.info,.md-typeset details.info{border-color:#00b8d4}.md-typeset .admonition.info:focus-within,.md-typeset details.info:focus-within{box-shadow:0 0 0 .2rem #00b8d41a}.md-typeset .info>.admonition-title,.md-typeset .info>summary{background-color:#00b8d41a}.md-typeset .info>.admonition-title:before,.md-typeset .info>summary:before{background-color:#00b8d4;-webkit-mask-image:var(--md-admonition-icon--info);mask-image:var(--md-admonition-icon--info)}.md-typeset .info>.admonition-title:after,.md-typeset .info>summary:after{color:#00b8d4}.md-typeset .admonition.tip,.md-typeset details.tip{border-color:#00bfa5}.md-typeset .admonition.tip:focus-within,.md-typeset details.tip:focus-within{box-shadow:0 0 0 .2rem #00bfa51a}.md-typeset .tip>.admonition-title,.md-typeset .tip>summary{background-color:#00bfa51a}.md-typeset .tip>.admonition-title:before,.md-typeset .tip>summary:before{background-color:#00bfa5;-webkit-mask-image:var(--md-admonition-icon--tip);mask-image:var(--md-admonition-icon--tip)}.md-typeset .tip>.admonition-title:after,.md-typeset .tip>summary:after{color:#00bfa5}.md-typeset .admonition.success,.md-typeset details.success{border-color:#00c853}.md-typeset .admonition.success:focus-within,.md-typeset details.success:focus-within{box-shadow:0 0 0 .2rem #00c8531a}.md-typeset .success>.admonition-title,.md-typeset .success>summary{background-color:#00c8531a}.md-typeset .success>.admonition-title:before,.md-typeset .success>summary:before{background-color:#00c853;-webkit-mask-image:var(--md-admonition-icon--success);mask-image:var(--md-admonition-icon--success)}.md-typeset .success>.admonition-title:after,.md-typeset .success>summary:after{color:#00c853}.md-typeset .admonition.question,.md-typeset details.question{border-color:#64dd17}.md-typeset .admonition.question:focus-within,.md-typeset details.question:focus-within{box-shadow:0 0 0 .2rem #64dd171a}.md-typeset .question>.admonition-title,.md-typeset .question>summary{background-color:#64dd171a}.md-typeset .question>.admonition-title:before,.md-typeset .question>summary:before{background-color:#64dd17;-webkit-mask-image:var(--md-admonition-icon--question);mask-image:var(--md-admonition-icon--question)}.md-typeset .question>.admonition-title:after,.md-typeset .question>summary:after{color:#64dd17}.md-typeset .admonition.warning,.md-typeset details.warning{border-color:#ff9100}.md-typeset .admonition.warning:focus-within,.md-typeset details.warning:focus-within{box-shadow:0 0 0 .2rem #ff91001a}.md-typeset .warning>.admonition-title,.md-typeset .warning>summary{background-color:#ff91001a}.md-typeset .warning>.admonition-title:before,.md-typeset .warning>summary:before{background-color:#ff9100;-webkit-mask-image:var(--md-admonition-icon--warning);mask-image:var(--md-admonition-icon--warning)}.md-typeset .warning>.admonition-title:after,.md-typeset .warning>summary:after{color:#ff9100}.md-typeset .admonition.failure,.md-typeset details.failure{border-color:#ff5252}.md-typeset .admonition.failure:focus-within,.md-typeset details.failure:focus-within{box-shadow:0 0 0 .2rem #ff52521a}.md-typeset .failure>.admonition-title,.md-typeset .failure>summary{background-color:#ff52521a}.md-typeset .failure>.admonition-title:before,.md-typeset .failure>summary:before{background-color:#ff5252;-webkit-mask-image:var(--md-admonition-icon--failure);mask-image:var(--md-admonition-icon--failure)}.md-typeset .failure>.admonition-title:after,.md-typeset .failure>summary:after{color:#ff5252}.md-typeset .admonition.danger,.md-typeset details.danger{border-color:#ff1744}.md-typeset .admonition.danger:focus-within,.md-typeset details.danger:focus-within{box-shadow:0 0 0 .2rem #ff17441a}.md-typeset .danger>.admonition-title,.md-typeset .danger>summary{background-color:#ff17441a}.md-typeset .danger>.admonition-title:before,.md-typeset .danger>summary:before{background-color:#ff1744;-webkit-mask-image:var(--md-admonition-icon--danger);mask-image:var(--md-admonition-icon--danger)}.md-typeset .danger>.admonition-title:after,.md-typeset .danger>summary:after{color:#ff1744}.md-typeset .admonition.bug,.md-typeset details.bug{border-color:#f50057}.md-typeset .admonition.bug:focus-within,.md-typeset details.bug:focus-within{box-shadow:0 0 0 .2rem #f500571a}.md-typeset .bug>.admonition-title,.md-typeset .bug>summary{background-color:#f500571a}.md-typeset .bug>.admonition-title:before,.md-typeset .bug>summary:before{background-color:#f50057;-webkit-mask-image:var(--md-admonition-icon--bug);mask-image:var(--md-admonition-icon--bug)}.md-typeset .bug>.admonition-title:after,.md-typeset .bug>summary:after{color:#f50057}.md-typeset .admonition.example,.md-typeset details.example{border-color:#7c4dff}.md-typeset .admonition.example:focus-within,.md-typeset details.example:focus-within{box-shadow:0 0 0 .2rem #7c4dff1a}.md-typeset .example>.admonition-title,.md-typeset .example>summary{background-color:#7c4dff1a}.md-typeset .example>.admonition-title:before,.md-typeset .example>summary:before{background-color:#7c4dff;-webkit-mask-image:var(--md-admonition-icon--example);mask-image:var(--md-admonition-icon--example)}.md-typeset .example>.admonition-title:after,.md-typeset .example>summary:after{color:#7c4dff}.md-typeset .admonition.quote,.md-typeset details.quote{border-color:#9e9e9e}.md-typeset .admonition.quote:focus-within,.md-typeset details.quote:focus-within{box-shadow:0 0 0 .2rem #9e9e9e1a}.md-typeset .quote>.admonition-title,.md-typeset .quote>summary{background-color:#9e9e9e1a}.md-typeset .quote>.admonition-title:before,.md-typeset .quote>summary:before{background-color:#9e9e9e;-webkit-mask-image:var(--md-admonition-icon--quote);mask-image:var(--md-admonition-icon--quote)}.md-typeset .quote>.admonition-title:after,.md-typeset .quote>summary:after{color:#9e9e9e}:root{--md-footnotes-icon:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .footnote{color:var(--md-default-fg-color--light);font-size:.64rem}[dir=ltr] .md-typeset .footnote>ol{margin-left:0}[dir=rtl] .md-typeset .footnote>ol{margin-right:0}.md-typeset .footnote>ol>li{transition:color 125ms}.md-typeset .footnote>ol>li:target{color:var(--md-default-fg-color)}.md-typeset .footnote>ol>li:focus-within .footnote-backref{opacity:1;transform:translateX(0);transition:none}.md-typeset .footnote>ol>li:hover .footnote-backref,.md-typeset .footnote>ol>li:target .footnote-backref{opacity:1;transform:translateX(0)}.md-typeset .footnote>ol>li>:first-child{margin-top:0}.md-typeset .footnote-ref{font-size:.75em;font-weight:700}html .md-typeset .footnote-ref{outline-offset:.1rem}.md-typeset [id^="fnref:"]:target>.footnote-ref{outline:auto}.md-typeset .footnote-backref{color:var(--md-typeset-a-color);display:inline-block;font-size:0;opacity:0;transform:translateX(.25rem);transition:color .25s,transform .25s .25s,opacity 125ms .25s;vertical-align:text-bottom}@media print{.md-typeset .footnote-backref{color:var(--md-typeset-a-color);opacity:1;transform:translateX(0)}}[dir=rtl] .md-typeset .footnote-backref{transform:translateX(-.25rem)}.md-typeset .footnote-backref:hover{color:var(--md-accent-fg-color)}.md-typeset .footnote-backref:before{background-color:currentcolor;content:"";display:inline-block;height:.8rem;-webkit-mask-image:var(--md-footnotes-icon);mask-image:var(--md-footnotes-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:.8rem}[dir=rtl] .md-typeset .footnote-backref:before{transform:scaleX(-1)}[dir=ltr] .md-typeset .headerlink{margin-left:.5rem}[dir=rtl] .md-typeset .headerlink{margin-right:.5rem}.md-typeset .headerlink{color:var(--md-default-fg-color--lighter);display:inline-block;opacity:0;transition:color .25s,opacity 125ms}@media print{.md-typeset .headerlink{display:none}}.md-typeset .headerlink:focus,.md-typeset :hover>.headerlink,.md-typeset :target>.headerlink{opacity:1;transition:color .25s,opacity 125ms}.md-typeset .headerlink:focus,.md-typeset .headerlink:hover,.md-typeset :target>.headerlink{color:var(--md-accent-fg-color)}.md-typeset :target{--md-scroll-margin:3.6rem;--md-scroll-offset:0rem;scroll-margin-top:calc(var(--md-scroll-margin) - var(--md-scroll-offset))}@media screen and (min-width:76.25em){.md-header--lifted~.md-container .md-typeset :target{--md-scroll-margin:6rem}}.md-typeset h1:target,.md-typeset h2:target,.md-typeset h3:target{--md-scroll-offset:0.2rem}.md-typeset h4:target{--md-scroll-offset:0.15rem}.md-typeset div.arithmatex{overflow:auto}@media screen and (max-width:44.984375em){.md-typeset div.arithmatex{margin:0 -.8rem}.md-typeset div.arithmatex>*{width:min-content}}.md-typeset div.arithmatex>*{margin-left:auto!important;margin-right:auto!important;padding:0 .8rem;touch-action:auto}.md-typeset div.arithmatex>* mjx-container{margin:0!important}.md-typeset div.arithmatex mjx-assistive-mml{height:0}.md-typeset .katex-html svg{max-width:revert}.md-typeset del.critic{background-color:var(--md-typeset-del-color)}.md-typeset del.critic,.md-typeset ins.critic{-webkit-box-decoration-break:clone;box-decoration-break:clone}.md-typeset ins.critic{background-color:var(--md-typeset-ins-color)}.md-typeset .critic.comment{-webkit-box-decoration-break:clone;box-decoration-break:clone;color:var(--md-code-hl-comment-color)}.md-typeset .critic.comment:before{content:"/* "}.md-typeset .critic.comment:after{content:" */"}.md-typeset .critic.block{box-shadow:none;display:block;margin:1em 0;overflow:auto;padding-left:.8rem;padding-right:.8rem}.md-typeset .critic.block>:first-child{margin-top:.5em}.md-typeset .critic.block>:last-child{margin-bottom:.5em}:root{--md-details-icon:url('data:image/svg+xml;charset=utf-8,')}.md-typeset details{display:flow-root;overflow:visible;padding-top:0}.md-typeset details[open]>summary:after{transform:rotate(90deg)}.md-typeset details:not([open]){box-shadow:none;padding-bottom:0}.md-typeset details:not([open])>summary{border-radius:.1rem}[dir=ltr] .md-typeset summary{padding-right:1.8rem}[dir=rtl] .md-typeset summary{padding-left:1.8rem}[dir=ltr] .md-typeset summary{border-top-left-radius:.1rem}[dir=ltr] .md-typeset summary,[dir=rtl] .md-typeset summary{border-top-right-radius:.1rem}[dir=rtl] .md-typeset summary{border-top-left-radius:.1rem}.md-typeset summary{cursor:pointer;display:block;min-height:1rem;overflow:hidden}.md-typeset summary.focus-visible{outline-color:var(--md-accent-fg-color);outline-offset:.2rem}.md-typeset summary:not(.focus-visible){-webkit-tap-highlight-color:transparent;outline:none}[dir=ltr] .md-typeset summary:after{right:.4rem}[dir=rtl] .md-typeset summary:after{left:.4rem}.md-typeset summary:after{background-color:currentcolor;content:"";height:1rem;-webkit-mask-image:var(--md-details-icon);mask-image:var(--md-details-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;position:absolute;top:.625em;transform:rotate(0deg);transition:transform .25s;width:1rem}[dir=rtl] .md-typeset summary:after{transform:rotate(180deg)}.md-typeset summary::marker{display:none}.md-typeset summary::-webkit-details-marker{display:none}.md-typeset .emojione,.md-typeset .gemoji,.md-typeset .twemoji{--md-icon-size:1.125em;display:inline-flex;height:var(--md-icon-size);vertical-align:text-top}.md-typeset .emojione svg,.md-typeset .gemoji svg,.md-typeset .twemoji svg{fill:currentcolor;max-height:100%;width:var(--md-icon-size)}.md-typeset .lg,.md-typeset .xl,.md-typeset .xxl,.md-typeset .xxxl{vertical-align:text-bottom}.md-typeset .middle{vertical-align:middle}.md-typeset .lg{--md-icon-size:1.5em}.md-typeset .xl{--md-icon-size:2.25em}.md-typeset .xxl{--md-icon-size:3em}.md-typeset .xxxl{--md-icon-size:4em}.highlight .o,.highlight .ow{color:var(--md-code-hl-operator-color)}.highlight .p{color:var(--md-code-hl-punctuation-color)}.highlight .cpf,.highlight .l,.highlight .s,.highlight .s1,.highlight .s2,.highlight .sb,.highlight .sc,.highlight .si,.highlight .ss{color:var(--md-code-hl-string-color)}.highlight .cp,.highlight .se,.highlight .sh,.highlight .sr,.highlight .sx{color:var(--md-code-hl-special-color)}.highlight .il,.highlight .m,.highlight .mb,.highlight .mf,.highlight .mh,.highlight .mi,.highlight .mo{color:var(--md-code-hl-number-color)}.highlight .k,.highlight .kd,.highlight .kn,.highlight .kp,.highlight .kr,.highlight .kt{color:var(--md-code-hl-keyword-color)}.highlight .kc,.highlight .n{color:var(--md-code-hl-name-color)}.highlight .bp,.highlight .nb,.highlight .no{color:var(--md-code-hl-constant-color)}.highlight .nc,.highlight .ne,.highlight .nf,.highlight .nn{color:var(--md-code-hl-function-color)}.highlight .nd,.highlight .ni,.highlight .nl,.highlight .nt{color:var(--md-code-hl-keyword-color)}.highlight .c,.highlight .c1,.highlight .ch,.highlight .cm,.highlight .cs,.highlight .sd{color:var(--md-code-hl-comment-color)}.highlight .na,.highlight .nv,.highlight .vc,.highlight .vg,.highlight .vi{color:var(--md-code-hl-variable-color)}.highlight .ge,.highlight .gh,.highlight .go,.highlight .gp,.highlight .gr,.highlight .gs,.highlight .gt,.highlight .gu{color:var(--md-code-hl-generic-color)}.highlight .gd,.highlight .gi{border-radius:.1rem;margin:0 -.125em;padding:0 .125em}.highlight .gd{background-color:var(--md-typeset-del-color)}.highlight .gi{background-color:var(--md-typeset-ins-color)}.highlight .hll{background-color:var(--md-code-hl-color--light);box-shadow:2px 0 0 0 var(--md-code-hl-color) inset;display:block;margin:0 -1.1764705882em;padding:0 1.1764705882em}.highlight span.filename{background-color:var(--md-code-bg-color);border-bottom:.05rem solid var(--md-default-fg-color--lightest);border-top-left-radius:.1rem;border-top-right-radius:.1rem;display:flow-root;font-size:.85em;font-weight:700;margin-top:1em;padding:.6617647059em 1.1764705882em;position:relative}.highlight span.filename+pre{margin-top:0}.highlight span.filename+pre>code{border-top-left-radius:0;border-top-right-radius:0}.highlight [data-linenos]:before{background-color:var(--md-code-bg-color);box-shadow:-.05rem 0 var(--md-default-fg-color--lightest) inset;color:var(--md-default-fg-color--light);content:attr(data-linenos);float:left;left:-1.1764705882em;margin-left:-1.1764705882em;margin-right:1.1764705882em;padding-left:1.1764705882em;position:sticky;-webkit-user-select:none;user-select:none;z-index:3}.highlight code a[id]{position:absolute;visibility:hidden}.highlight code[data-md-copying]{display:initial}.highlight code[data-md-copying] .hll{display:contents}.highlight code[data-md-copying] .md-annotation{display:none}.highlighttable{display:flow-root}.highlighttable tbody,.highlighttable td{display:block;padding:0}.highlighttable tr{display:flex}.highlighttable pre{margin:0}.highlighttable th.filename{flex-grow:1;padding:0;text-align:left}.highlighttable th.filename span.filename{margin-top:0}.highlighttable .linenos{background-color:var(--md-code-bg-color);border-bottom-left-radius:.1rem;border-top-left-radius:.1rem;font-size:.85em;padding:.7720588235em 0 .7720588235em 1.1764705882em;-webkit-user-select:none;user-select:none}.highlighttable .linenodiv{box-shadow:-.05rem 0 var(--md-default-fg-color--lightest) inset}.highlighttable .linenodiv pre{color:var(--md-default-fg-color--light);text-align:right}.highlighttable .linenodiv span[class]{padding-right:.5882352941em}.highlighttable .code{flex:1;min-width:0}.linenodiv a{color:inherit}.md-typeset .highlighttable{direction:ltr;margin:1em 0}.md-typeset .highlighttable>tbody>tr>.code>div>pre>code{border-bottom-left-radius:0;border-top-left-radius:0}.md-typeset .highlight+.result{border:.05rem solid var(--md-code-bg-color);border-bottom-left-radius:.1rem;border-bottom-right-radius:.1rem;border-top-width:.1rem;margin-top:-1.125em;overflow:visible;padding:0 1em}.md-typeset .highlight+.result:after{clear:both;content:"";display:block}@media screen and (max-width:44.984375em){.md-content__inner>.highlight{margin:1em -.8rem}.md-content__inner>.highlight>.filename,.md-content__inner>.highlight>.highlighttable>tbody>tr>.code>div>pre>code,.md-content__inner>.highlight>.highlighttable>tbody>tr>.filename span.filename,.md-content__inner>.highlight>.highlighttable>tbody>tr>.linenos,.md-content__inner>.highlight>pre>code{border-radius:0}.md-content__inner>.highlight+.result{border-left-width:0;border-radius:0;border-right-width:0;margin-left:-.8rem;margin-right:-.8rem}}.md-typeset .keys kbd:after,.md-typeset .keys kbd:before{-moz-osx-font-smoothing:initial;-webkit-font-smoothing:initial;color:inherit;margin:0;position:relative}.md-typeset .keys span{color:var(--md-default-fg-color--light);padding:0 .2em}.md-typeset .keys .key-alt:before,.md-typeset .keys .key-left-alt:before,.md-typeset .keys .key-right-alt:before{content:"⎇";padding-right:.4em}.md-typeset .keys .key-command:before,.md-typeset .keys .key-left-command:before,.md-typeset .keys .key-right-command:before{content:"⌘";padding-right:.4em}.md-typeset .keys .key-control:before,.md-typeset .keys .key-left-control:before,.md-typeset .keys .key-right-control:before{content:"⌃";padding-right:.4em}.md-typeset .keys .key-left-meta:before,.md-typeset .keys .key-meta:before,.md-typeset .keys .key-right-meta:before{content:"◆";padding-right:.4em}.md-typeset .keys .key-left-option:before,.md-typeset .keys .key-option:before,.md-typeset .keys .key-right-option:before{content:"⌥";padding-right:.4em}.md-typeset .keys .key-left-shift:before,.md-typeset .keys .key-right-shift:before,.md-typeset .keys .key-shift:before{content:"⇧";padding-right:.4em}.md-typeset .keys .key-left-super:before,.md-typeset .keys .key-right-super:before,.md-typeset .keys .key-super:before{content:"❖";padding-right:.4em}.md-typeset .keys .key-left-windows:before,.md-typeset .keys .key-right-windows:before,.md-typeset .keys .key-windows:before{content:"⊞";padding-right:.4em}.md-typeset .keys .key-arrow-down:before{content:"↓";padding-right:.4em}.md-typeset .keys .key-arrow-left:before{content:"←";padding-right:.4em}.md-typeset .keys .key-arrow-right:before{content:"→";padding-right:.4em}.md-typeset .keys .key-arrow-up:before{content:"↑";padding-right:.4em}.md-typeset .keys .key-backspace:before{content:"⌫";padding-right:.4em}.md-typeset .keys .key-backtab:before{content:"⇤";padding-right:.4em}.md-typeset .keys .key-caps-lock:before{content:"⇪";padding-right:.4em}.md-typeset .keys .key-clear:before{content:"⌧";padding-right:.4em}.md-typeset .keys .key-context-menu:before{content:"☰";padding-right:.4em}.md-typeset .keys .key-delete:before{content:"⌦";padding-right:.4em}.md-typeset .keys .key-eject:before{content:"⏏";padding-right:.4em}.md-typeset .keys .key-end:before{content:"⤓";padding-right:.4em}.md-typeset .keys .key-escape:before{content:"⎋";padding-right:.4em}.md-typeset .keys .key-home:before{content:"⤒";padding-right:.4em}.md-typeset .keys .key-insert:before{content:"⎀";padding-right:.4em}.md-typeset .keys .key-page-down:before{content:"⇟";padding-right:.4em}.md-typeset .keys .key-page-up:before{content:"⇞";padding-right:.4em}.md-typeset .keys .key-print-screen:before{content:"⎙";padding-right:.4em}.md-typeset .keys .key-tab:after{content:"⇥";padding-left:.4em}.md-typeset .keys .key-num-enter:after{content:"⌤";padding-left:.4em}.md-typeset .keys .key-enter:after{content:"⏎";padding-left:.4em}:root{--md-tabbed-icon--prev:url('data:image/svg+xml;charset=utf-8,');--md-tabbed-icon--next:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .tabbed-set{border-radius:.1rem;display:flex;flex-flow:column wrap;margin:1em 0;position:relative}.md-typeset .tabbed-set>input{height:0;opacity:0;position:absolute;width:0}.md-typeset .tabbed-set>input:target{--md-scroll-offset:0.625em}.md-typeset .tabbed-set>input.focus-visible~.tabbed-labels:before{background-color:var(--md-accent-fg-color)}.md-typeset .tabbed-labels{-ms-overflow-style:none;box-shadow:0 -.05rem var(--md-default-fg-color--lightest) inset;display:flex;max-width:100%;overflow:auto;scrollbar-width:none}@media print{.md-typeset .tabbed-labels{display:contents}}@media screen{.js .md-typeset .tabbed-labels{position:relative}.js .md-typeset .tabbed-labels:before{background:var(--md-default-fg-color);bottom:0;content:"";display:block;height:2px;left:0;position:absolute;transform:translateX(var(--md-indicator-x));transition:width 225ms,background-color .25s,transform .25s;transition-timing-function:cubic-bezier(.4,0,.2,1);width:var(--md-indicator-width)}}.md-typeset .tabbed-labels::-webkit-scrollbar{display:none}.md-typeset .tabbed-labels>label{border-bottom:.1rem solid #0000;border-radius:.1rem .1rem 0 0;color:var(--md-default-fg-color--light);cursor:pointer;flex-shrink:0;font-size:.64rem;font-weight:700;padding:.78125em 1.25em .625em;scroll-margin-inline-start:1rem;transition:background-color .25s,color .25s;white-space:nowrap;width:auto}@media print{.md-typeset .tabbed-labels>label:first-child{order:1}.md-typeset .tabbed-labels>label:nth-child(2){order:2}.md-typeset .tabbed-labels>label:nth-child(3){order:3}.md-typeset .tabbed-labels>label:nth-child(4){order:4}.md-typeset .tabbed-labels>label:nth-child(5){order:5}.md-typeset .tabbed-labels>label:nth-child(6){order:6}.md-typeset .tabbed-labels>label:nth-child(7){order:7}.md-typeset .tabbed-labels>label:nth-child(8){order:8}.md-typeset .tabbed-labels>label:nth-child(9){order:9}.md-typeset .tabbed-labels>label:nth-child(10){order:10}.md-typeset .tabbed-labels>label:nth-child(11){order:11}.md-typeset .tabbed-labels>label:nth-child(12){order:12}.md-typeset .tabbed-labels>label:nth-child(13){order:13}.md-typeset .tabbed-labels>label:nth-child(14){order:14}.md-typeset .tabbed-labels>label:nth-child(15){order:15}.md-typeset .tabbed-labels>label:nth-child(16){order:16}.md-typeset .tabbed-labels>label:nth-child(17){order:17}.md-typeset .tabbed-labels>label:nth-child(18){order:18}.md-typeset .tabbed-labels>label:nth-child(19){order:19}.md-typeset .tabbed-labels>label:nth-child(20){order:20}}.md-typeset .tabbed-labels>label:hover{color:var(--md-default-fg-color)}.md-typeset .tabbed-labels>label>[href]:first-child{color:inherit}.md-typeset .tabbed-labels--linked>label{padding:0}.md-typeset .tabbed-labels--linked>label>a{display:block;padding:.78125em 1.25em .625em}.md-typeset .tabbed-content{width:100%}@media print{.md-typeset .tabbed-content{display:contents}}.md-typeset .tabbed-block{display:none}@media print{.md-typeset .tabbed-block{display:block}.md-typeset .tabbed-block:first-child{order:1}.md-typeset .tabbed-block:nth-child(2){order:2}.md-typeset .tabbed-block:nth-child(3){order:3}.md-typeset .tabbed-block:nth-child(4){order:4}.md-typeset .tabbed-block:nth-child(5){order:5}.md-typeset .tabbed-block:nth-child(6){order:6}.md-typeset .tabbed-block:nth-child(7){order:7}.md-typeset .tabbed-block:nth-child(8){order:8}.md-typeset .tabbed-block:nth-child(9){order:9}.md-typeset .tabbed-block:nth-child(10){order:10}.md-typeset .tabbed-block:nth-child(11){order:11}.md-typeset .tabbed-block:nth-child(12){order:12}.md-typeset .tabbed-block:nth-child(13){order:13}.md-typeset .tabbed-block:nth-child(14){order:14}.md-typeset .tabbed-block:nth-child(15){order:15}.md-typeset .tabbed-block:nth-child(16){order:16}.md-typeset .tabbed-block:nth-child(17){order:17}.md-typeset .tabbed-block:nth-child(18){order:18}.md-typeset .tabbed-block:nth-child(19){order:19}.md-typeset .tabbed-block:nth-child(20){order:20}}.md-typeset .tabbed-block>.highlight:first-child>pre,.md-typeset .tabbed-block>pre:first-child{margin:0}.md-typeset .tabbed-block>.highlight:first-child>pre>code,.md-typeset .tabbed-block>pre:first-child>code{border-top-left-radius:0;border-top-right-radius:0}.md-typeset .tabbed-block>.highlight:first-child>.filename{border-top-left-radius:0;border-top-right-radius:0;margin:0}.md-typeset .tabbed-block>.highlight:first-child>.highlighttable{margin:0}.md-typeset .tabbed-block>.highlight:first-child>.highlighttable>tbody>tr>.filename span.filename,.md-typeset .tabbed-block>.highlight:first-child>.highlighttable>tbody>tr>.linenos{border-top-left-radius:0;border-top-right-radius:0;margin:0}.md-typeset .tabbed-block>.highlight:first-child>.highlighttable>tbody>tr>.code>div>pre>code{border-top-left-radius:0;border-top-right-radius:0}.md-typeset .tabbed-block>.highlight:first-child+.result{margin-top:-.125em}.md-typeset .tabbed-block>.tabbed-set{margin:0}.md-typeset .tabbed-button{align-self:center;border-radius:100%;color:var(--md-default-fg-color--light);cursor:pointer;display:block;height:.9rem;margin-top:.1rem;pointer-events:auto;transition:background-color .25s;width:.9rem}.md-typeset .tabbed-button:hover{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}.md-typeset .tabbed-button:after{background-color:currentcolor;content:"";display:block;height:100%;-webkit-mask-image:var(--md-tabbed-icon--prev);mask-image:var(--md-tabbed-icon--prev);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;transition:background-color .25s,transform .25s;width:100%}.md-typeset .tabbed-control{background:linear-gradient(to right,var(--md-default-bg-color) 60%,#0000);display:flex;height:1.9rem;justify-content:start;pointer-events:none;position:absolute;transition:opacity 125ms;width:1.2rem}[dir=rtl] .md-typeset .tabbed-control{transform:rotate(180deg)}.md-typeset .tabbed-control[hidden]{opacity:0}.md-typeset .tabbed-control--next{background:linear-gradient(to left,var(--md-default-bg-color) 60%,#0000);justify-content:end;right:0}.md-typeset .tabbed-control--next .tabbed-button:after{-webkit-mask-image:var(--md-tabbed-icon--next);mask-image:var(--md-tabbed-icon--next)}@media screen and (max-width:44.984375em){[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels{padding-left:.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels{padding-right:.8rem}.md-content__inner>.tabbed-set .tabbed-labels{margin:0 -.8rem;max-width:100vw;scroll-padding-inline-start:.8rem}[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels:after{padding-right:.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels:after{padding-left:.8rem}.md-content__inner>.tabbed-set .tabbed-labels:after{content:""}[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--prev{padding-left:.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--prev{padding-right:.8rem}[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--prev{margin-left:-.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--prev{margin-right:-.8rem}.md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--prev{width:2rem}[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--next{padding-right:.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--next{padding-left:.8rem}[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--next{margin-right:-.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--next{margin-left:-.8rem}.md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--next{width:2rem}}@media screen{.md-typeset .tabbed-set>input:first-child:checked~.tabbed-labels>:first-child,.md-typeset .tabbed-set>input:nth-child(10):checked~.tabbed-labels>:nth-child(10),.md-typeset .tabbed-set>input:nth-child(11):checked~.tabbed-labels>:nth-child(11),.md-typeset .tabbed-set>input:nth-child(12):checked~.tabbed-labels>:nth-child(12),.md-typeset .tabbed-set>input:nth-child(13):checked~.tabbed-labels>:nth-child(13),.md-typeset .tabbed-set>input:nth-child(14):checked~.tabbed-labels>:nth-child(14),.md-typeset .tabbed-set>input:nth-child(15):checked~.tabbed-labels>:nth-child(15),.md-typeset .tabbed-set>input:nth-child(16):checked~.tabbed-labels>:nth-child(16),.md-typeset .tabbed-set>input:nth-child(17):checked~.tabbed-labels>:nth-child(17),.md-typeset .tabbed-set>input:nth-child(18):checked~.tabbed-labels>:nth-child(18),.md-typeset .tabbed-set>input:nth-child(19):checked~.tabbed-labels>:nth-child(19),.md-typeset .tabbed-set>input:nth-child(2):checked~.tabbed-labels>:nth-child(2),.md-typeset .tabbed-set>input:nth-child(20):checked~.tabbed-labels>:nth-child(20),.md-typeset .tabbed-set>input:nth-child(3):checked~.tabbed-labels>:nth-child(3),.md-typeset .tabbed-set>input:nth-child(4):checked~.tabbed-labels>:nth-child(4),.md-typeset .tabbed-set>input:nth-child(5):checked~.tabbed-labels>:nth-child(5),.md-typeset .tabbed-set>input:nth-child(6):checked~.tabbed-labels>:nth-child(6),.md-typeset .tabbed-set>input:nth-child(7):checked~.tabbed-labels>:nth-child(7),.md-typeset .tabbed-set>input:nth-child(8):checked~.tabbed-labels>:nth-child(8),.md-typeset .tabbed-set>input:nth-child(9):checked~.tabbed-labels>:nth-child(9){color:var(--md-default-fg-color)}.md-typeset .no-js .tabbed-set>input:first-child:checked~.tabbed-labels>:first-child,.md-typeset .no-js .tabbed-set>input:nth-child(10):checked~.tabbed-labels>:nth-child(10),.md-typeset .no-js .tabbed-set>input:nth-child(11):checked~.tabbed-labels>:nth-child(11),.md-typeset .no-js .tabbed-set>input:nth-child(12):checked~.tabbed-labels>:nth-child(12),.md-typeset .no-js .tabbed-set>input:nth-child(13):checked~.tabbed-labels>:nth-child(13),.md-typeset .no-js .tabbed-set>input:nth-child(14):checked~.tabbed-labels>:nth-child(14),.md-typeset .no-js .tabbed-set>input:nth-child(15):checked~.tabbed-labels>:nth-child(15),.md-typeset .no-js .tabbed-set>input:nth-child(16):checked~.tabbed-labels>:nth-child(16),.md-typeset .no-js .tabbed-set>input:nth-child(17):checked~.tabbed-labels>:nth-child(17),.md-typeset .no-js .tabbed-set>input:nth-child(18):checked~.tabbed-labels>:nth-child(18),.md-typeset .no-js .tabbed-set>input:nth-child(19):checked~.tabbed-labels>:nth-child(19),.md-typeset .no-js .tabbed-set>input:nth-child(2):checked~.tabbed-labels>:nth-child(2),.md-typeset .no-js .tabbed-set>input:nth-child(20):checked~.tabbed-labels>:nth-child(20),.md-typeset .no-js .tabbed-set>input:nth-child(3):checked~.tabbed-labels>:nth-child(3),.md-typeset .no-js .tabbed-set>input:nth-child(4):checked~.tabbed-labels>:nth-child(4),.md-typeset .no-js .tabbed-set>input:nth-child(5):checked~.tabbed-labels>:nth-child(5),.md-typeset .no-js .tabbed-set>input:nth-child(6):checked~.tabbed-labels>:nth-child(6),.md-typeset .no-js .tabbed-set>input:nth-child(7):checked~.tabbed-labels>:nth-child(7),.md-typeset .no-js .tabbed-set>input:nth-child(8):checked~.tabbed-labels>:nth-child(8),.md-typeset .no-js .tabbed-set>input:nth-child(9):checked~.tabbed-labels>:nth-child(9),.md-typeset [role=dialog] .tabbed-set>input:first-child:checked~.tabbed-labels>:first-child,.md-typeset [role=dialog] .tabbed-set>input:nth-child(10):checked~.tabbed-labels>:nth-child(10),.md-typeset [role=dialog] .tabbed-set>input:nth-child(11):checked~.tabbed-labels>:nth-child(11),.md-typeset [role=dialog] .tabbed-set>input:nth-child(12):checked~.tabbed-labels>:nth-child(12),.md-typeset [role=dialog] .tabbed-set>input:nth-child(13):checked~.tabbed-labels>:nth-child(13),.md-typeset [role=dialog] .tabbed-set>input:nth-child(14):checked~.tabbed-labels>:nth-child(14),.md-typeset [role=dialog] .tabbed-set>input:nth-child(15):checked~.tabbed-labels>:nth-child(15),.md-typeset [role=dialog] .tabbed-set>input:nth-child(16):checked~.tabbed-labels>:nth-child(16),.md-typeset [role=dialog] .tabbed-set>input:nth-child(17):checked~.tabbed-labels>:nth-child(17),.md-typeset [role=dialog] .tabbed-set>input:nth-child(18):checked~.tabbed-labels>:nth-child(18),.md-typeset [role=dialog] .tabbed-set>input:nth-child(19):checked~.tabbed-labels>:nth-child(19),.md-typeset [role=dialog] .tabbed-set>input:nth-child(2):checked~.tabbed-labels>:nth-child(2),.md-typeset [role=dialog] .tabbed-set>input:nth-child(20):checked~.tabbed-labels>:nth-child(20),.md-typeset [role=dialog] .tabbed-set>input:nth-child(3):checked~.tabbed-labels>:nth-child(3),.md-typeset [role=dialog] .tabbed-set>input:nth-child(4):checked~.tabbed-labels>:nth-child(4),.md-typeset [role=dialog] .tabbed-set>input:nth-child(5):checked~.tabbed-labels>:nth-child(5),.md-typeset [role=dialog] .tabbed-set>input:nth-child(6):checked~.tabbed-labels>:nth-child(6),.md-typeset [role=dialog] .tabbed-set>input:nth-child(7):checked~.tabbed-labels>:nth-child(7),.md-typeset [role=dialog] .tabbed-set>input:nth-child(8):checked~.tabbed-labels>:nth-child(8),.md-typeset [role=dialog] .tabbed-set>input:nth-child(9):checked~.tabbed-labels>:nth-child(9),.no-js .md-typeset .tabbed-set>input:first-child:checked~.tabbed-labels>:first-child,.no-js .md-typeset .tabbed-set>input:nth-child(10):checked~.tabbed-labels>:nth-child(10),.no-js .md-typeset .tabbed-set>input:nth-child(11):checked~.tabbed-labels>:nth-child(11),.no-js .md-typeset .tabbed-set>input:nth-child(12):checked~.tabbed-labels>:nth-child(12),.no-js .md-typeset .tabbed-set>input:nth-child(13):checked~.tabbed-labels>:nth-child(13),.no-js .md-typeset .tabbed-set>input:nth-child(14):checked~.tabbed-labels>:nth-child(14),.no-js .md-typeset .tabbed-set>input:nth-child(15):checked~.tabbed-labels>:nth-child(15),.no-js .md-typeset .tabbed-set>input:nth-child(16):checked~.tabbed-labels>:nth-child(16),.no-js .md-typeset .tabbed-set>input:nth-child(17):checked~.tabbed-labels>:nth-child(17),.no-js .md-typeset .tabbed-set>input:nth-child(18):checked~.tabbed-labels>:nth-child(18),.no-js .md-typeset .tabbed-set>input:nth-child(19):checked~.tabbed-labels>:nth-child(19),.no-js .md-typeset .tabbed-set>input:nth-child(2):checked~.tabbed-labels>:nth-child(2),.no-js .md-typeset .tabbed-set>input:nth-child(20):checked~.tabbed-labels>:nth-child(20),.no-js .md-typeset .tabbed-set>input:nth-child(3):checked~.tabbed-labels>:nth-child(3),.no-js .md-typeset .tabbed-set>input:nth-child(4):checked~.tabbed-labels>:nth-child(4),.no-js .md-typeset .tabbed-set>input:nth-child(5):checked~.tabbed-labels>:nth-child(5),.no-js .md-typeset .tabbed-set>input:nth-child(6):checked~.tabbed-labels>:nth-child(6),.no-js .md-typeset .tabbed-set>input:nth-child(7):checked~.tabbed-labels>:nth-child(7),.no-js .md-typeset .tabbed-set>input:nth-child(8):checked~.tabbed-labels>:nth-child(8),.no-js .md-typeset .tabbed-set>input:nth-child(9):checked~.tabbed-labels>:nth-child(9),[role=dialog] .md-typeset .tabbed-set>input:first-child:checked~.tabbed-labels>:first-child,[role=dialog] .md-typeset .tabbed-set>input:nth-child(10):checked~.tabbed-labels>:nth-child(10),[role=dialog] .md-typeset .tabbed-set>input:nth-child(11):checked~.tabbed-labels>:nth-child(11),[role=dialog] .md-typeset .tabbed-set>input:nth-child(12):checked~.tabbed-labels>:nth-child(12),[role=dialog] .md-typeset .tabbed-set>input:nth-child(13):checked~.tabbed-labels>:nth-child(13),[role=dialog] .md-typeset .tabbed-set>input:nth-child(14):checked~.tabbed-labels>:nth-child(14),[role=dialog] .md-typeset .tabbed-set>input:nth-child(15):checked~.tabbed-labels>:nth-child(15),[role=dialog] .md-typeset .tabbed-set>input:nth-child(16):checked~.tabbed-labels>:nth-child(16),[role=dialog] .md-typeset .tabbed-set>input:nth-child(17):checked~.tabbed-labels>:nth-child(17),[role=dialog] .md-typeset .tabbed-set>input:nth-child(18):checked~.tabbed-labels>:nth-child(18),[role=dialog] .md-typeset .tabbed-set>input:nth-child(19):checked~.tabbed-labels>:nth-child(19),[role=dialog] .md-typeset .tabbed-set>input:nth-child(2):checked~.tabbed-labels>:nth-child(2),[role=dialog] .md-typeset .tabbed-set>input:nth-child(20):checked~.tabbed-labels>:nth-child(20),[role=dialog] .md-typeset .tabbed-set>input:nth-child(3):checked~.tabbed-labels>:nth-child(3),[role=dialog] .md-typeset .tabbed-set>input:nth-child(4):checked~.tabbed-labels>:nth-child(4),[role=dialog] .md-typeset .tabbed-set>input:nth-child(5):checked~.tabbed-labels>:nth-child(5),[role=dialog] .md-typeset .tabbed-set>input:nth-child(6):checked~.tabbed-labels>:nth-child(6),[role=dialog] .md-typeset .tabbed-set>input:nth-child(7):checked~.tabbed-labels>:nth-child(7),[role=dialog] .md-typeset .tabbed-set>input:nth-child(8):checked~.tabbed-labels>:nth-child(8),[role=dialog] .md-typeset .tabbed-set>input:nth-child(9):checked~.tabbed-labels>:nth-child(9){border-color:var(--md-default-fg-color)}}.md-typeset .tabbed-set>input:first-child.focus-visible~.tabbed-labels>:first-child,.md-typeset .tabbed-set>input:nth-child(10).focus-visible~.tabbed-labels>:nth-child(10),.md-typeset .tabbed-set>input:nth-child(11).focus-visible~.tabbed-labels>:nth-child(11),.md-typeset .tabbed-set>input:nth-child(12).focus-visible~.tabbed-labels>:nth-child(12),.md-typeset .tabbed-set>input:nth-child(13).focus-visible~.tabbed-labels>:nth-child(13),.md-typeset .tabbed-set>input:nth-child(14).focus-visible~.tabbed-labels>:nth-child(14),.md-typeset .tabbed-set>input:nth-child(15).focus-visible~.tabbed-labels>:nth-child(15),.md-typeset .tabbed-set>input:nth-child(16).focus-visible~.tabbed-labels>:nth-child(16),.md-typeset .tabbed-set>input:nth-child(17).focus-visible~.tabbed-labels>:nth-child(17),.md-typeset .tabbed-set>input:nth-child(18).focus-visible~.tabbed-labels>:nth-child(18),.md-typeset .tabbed-set>input:nth-child(19).focus-visible~.tabbed-labels>:nth-child(19),.md-typeset .tabbed-set>input:nth-child(2).focus-visible~.tabbed-labels>:nth-child(2),.md-typeset .tabbed-set>input:nth-child(20).focus-visible~.tabbed-labels>:nth-child(20),.md-typeset .tabbed-set>input:nth-child(3).focus-visible~.tabbed-labels>:nth-child(3),.md-typeset .tabbed-set>input:nth-child(4).focus-visible~.tabbed-labels>:nth-child(4),.md-typeset .tabbed-set>input:nth-child(5).focus-visible~.tabbed-labels>:nth-child(5),.md-typeset .tabbed-set>input:nth-child(6).focus-visible~.tabbed-labels>:nth-child(6),.md-typeset .tabbed-set>input:nth-child(7).focus-visible~.tabbed-labels>:nth-child(7),.md-typeset .tabbed-set>input:nth-child(8).focus-visible~.tabbed-labels>:nth-child(8),.md-typeset .tabbed-set>input:nth-child(9).focus-visible~.tabbed-labels>:nth-child(9){color:var(--md-accent-fg-color)}.md-typeset .tabbed-set>input:first-child:checked~.tabbed-content>:first-child,.md-typeset .tabbed-set>input:nth-child(10):checked~.tabbed-content>:nth-child(10),.md-typeset .tabbed-set>input:nth-child(11):checked~.tabbed-content>:nth-child(11),.md-typeset .tabbed-set>input:nth-child(12):checked~.tabbed-content>:nth-child(12),.md-typeset .tabbed-set>input:nth-child(13):checked~.tabbed-content>:nth-child(13),.md-typeset .tabbed-set>input:nth-child(14):checked~.tabbed-content>:nth-child(14),.md-typeset .tabbed-set>input:nth-child(15):checked~.tabbed-content>:nth-child(15),.md-typeset .tabbed-set>input:nth-child(16):checked~.tabbed-content>:nth-child(16),.md-typeset .tabbed-set>input:nth-child(17):checked~.tabbed-content>:nth-child(17),.md-typeset .tabbed-set>input:nth-child(18):checked~.tabbed-content>:nth-child(18),.md-typeset .tabbed-set>input:nth-child(19):checked~.tabbed-content>:nth-child(19),.md-typeset .tabbed-set>input:nth-child(2):checked~.tabbed-content>:nth-child(2),.md-typeset .tabbed-set>input:nth-child(20):checked~.tabbed-content>:nth-child(20),.md-typeset .tabbed-set>input:nth-child(3):checked~.tabbed-content>:nth-child(3),.md-typeset .tabbed-set>input:nth-child(4):checked~.tabbed-content>:nth-child(4),.md-typeset .tabbed-set>input:nth-child(5):checked~.tabbed-content>:nth-child(5),.md-typeset .tabbed-set>input:nth-child(6):checked~.tabbed-content>:nth-child(6),.md-typeset .tabbed-set>input:nth-child(7):checked~.tabbed-content>:nth-child(7),.md-typeset .tabbed-set>input:nth-child(8):checked~.tabbed-content>:nth-child(8),.md-typeset .tabbed-set>input:nth-child(9):checked~.tabbed-content>:nth-child(9){display:block}:root{--md-tasklist-icon:url('data:image/svg+xml;charset=utf-8,');--md-tasklist-icon--checked:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .task-list-item{list-style-type:none;position:relative}[dir=ltr] .md-typeset .task-list-item [type=checkbox]{left:-2em}[dir=rtl] .md-typeset .task-list-item [type=checkbox]{right:-2em}.md-typeset .task-list-item [type=checkbox]{position:absolute;top:.45em}.md-typeset .task-list-control [type=checkbox]{opacity:0;z-index:-1}[dir=ltr] .md-typeset .task-list-indicator:before{left:-1.5em}[dir=rtl] .md-typeset .task-list-indicator:before{right:-1.5em}.md-typeset .task-list-indicator:before{background-color:var(--md-default-fg-color--lightest);content:"";height:1.25em;-webkit-mask-image:var(--md-tasklist-icon);mask-image:var(--md-tasklist-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;position:absolute;top:.15em;width:1.25em}.md-typeset [type=checkbox]:checked+.task-list-indicator:before{background-color:#00e676;-webkit-mask-image:var(--md-tasklist-icon--checked);mask-image:var(--md-tasklist-icon--checked)}@media print{.giscus,[id=__comments]{display:none}}:root>*{--md-mermaid-font-family:var(--md-text-font-family),sans-serif;--md-mermaid-edge-color:var(--md-code-fg-color);--md-mermaid-node-bg-color:var(--md-accent-fg-color--transparent);--md-mermaid-node-fg-color:var(--md-accent-fg-color);--md-mermaid-label-bg-color:var(--md-default-bg-color);--md-mermaid-label-fg-color:var(--md-code-fg-color);--md-mermaid-sequence-actor-bg-color:var(--md-mermaid-label-bg-color);--md-mermaid-sequence-actor-fg-color:var(--md-mermaid-label-fg-color);--md-mermaid-sequence-actor-border-color:var(--md-mermaid-node-fg-color);--md-mermaid-sequence-actor-line-color:var(--md-default-fg-color--lighter);--md-mermaid-sequence-actorman-bg-color:var(--md-mermaid-label-bg-color);--md-mermaid-sequence-actorman-line-color:var(--md-mermaid-node-fg-color);--md-mermaid-sequence-box-bg-color:var(--md-mermaid-node-bg-color);--md-mermaid-sequence-box-fg-color:var(--md-mermaid-edge-color);--md-mermaid-sequence-label-bg-color:var(--md-mermaid-node-bg-color);--md-mermaid-sequence-label-fg-color:var(--md-mermaid-node-fg-color);--md-mermaid-sequence-loop-bg-color:var(--md-mermaid-node-bg-color);--md-mermaid-sequence-loop-fg-color:var(--md-mermaid-edge-color);--md-mermaid-sequence-loop-border-color:var(--md-mermaid-node-fg-color);--md-mermaid-sequence-message-fg-color:var(--md-mermaid-edge-color);--md-mermaid-sequence-message-line-color:var(--md-mermaid-edge-color);--md-mermaid-sequence-note-bg-color:var(--md-mermaid-label-bg-color);--md-mermaid-sequence-note-fg-color:var(--md-mermaid-edge-color);--md-mermaid-sequence-note-border-color:var(--md-mermaid-label-fg-color);--md-mermaid-sequence-number-bg-color:var(--md-mermaid-node-fg-color);--md-mermaid-sequence-number-fg-color:var(--md-accent-bg-color)}.mermaid{line-height:normal;margin:1em 0}.md-typeset .grid{grid-gap:.4rem;display:grid;grid-template-columns:repeat(auto-fit,minmax(min(100%,16rem),1fr));margin:1em 0}.md-typeset .grid.cards>ol,.md-typeset .grid.cards>ul{display:contents}.md-typeset .grid.cards>ol>li,.md-typeset .grid.cards>ul>li,.md-typeset .grid>.card{border:.05rem solid var(--md-default-fg-color--lightest);border-radius:.1rem;display:block;margin:0;padding:.8rem;transition:border .25s,box-shadow .25s}.md-typeset .grid.cards>ol>li:focus-within,.md-typeset .grid.cards>ol>li:hover,.md-typeset .grid.cards>ul>li:focus-within,.md-typeset .grid.cards>ul>li:hover,.md-typeset .grid>.card:focus-within,.md-typeset .grid>.card:hover{border-color:#0000;box-shadow:var(--md-shadow-z2)}.md-typeset .grid.cards>ol>li>hr,.md-typeset .grid.cards>ul>li>hr,.md-typeset .grid>.card>hr{margin-bottom:1em;margin-top:1em}.md-typeset .grid.cards>ol>li>:first-child,.md-typeset .grid.cards>ul>li>:first-child,.md-typeset .grid>.card>:first-child{margin-top:0}.md-typeset .grid.cards>ol>li>:last-child,.md-typeset .grid.cards>ul>li>:last-child,.md-typeset .grid>.card>:last-child{margin-bottom:0}.md-typeset .grid>*,.md-typeset .grid>.admonition,.md-typeset .grid>.highlight>*,.md-typeset .grid>.highlighttable,.md-typeset .grid>.md-typeset details,.md-typeset .grid>details,.md-typeset .grid>pre{margin-bottom:0;margin-top:0}.md-typeset .grid>.highlight>pre:only-child,.md-typeset .grid>.highlight>pre>code,.md-typeset .grid>.highlighttable,.md-typeset .grid>.highlighttable>tbody,.md-typeset .grid>.highlighttable>tbody>tr,.md-typeset .grid>.highlighttable>tbody>tr>.code,.md-typeset .grid>.highlighttable>tbody>tr>.code>.highlight,.md-typeset .grid>.highlighttable>tbody>tr>.code>.highlight>pre,.md-typeset .grid>.highlighttable>tbody>tr>.code>.highlight>pre>code{height:100%}.md-typeset .grid>.tabbed-set{margin-bottom:0;margin-top:0}@media screen and (min-width:45em){[dir=ltr] .md-typeset .inline{float:left}[dir=rtl] .md-typeset .inline{float:right}[dir=ltr] .md-typeset .inline{margin-right:.8rem}[dir=rtl] .md-typeset .inline{margin-left:.8rem}.md-typeset .inline{margin-bottom:.8rem;margin-top:0;width:11.7rem}[dir=ltr] .md-typeset .inline.end{float:right}[dir=rtl] .md-typeset .inline.end{float:left}[dir=ltr] .md-typeset .inline.end{margin-left:.8rem;margin-right:0}[dir=rtl] .md-typeset .inline.end{margin-left:0;margin-right:.8rem}} -/*! update cache: 20260104155727 */ +/*! update cache: 20260104161427 */ diff --git a/assets/stylesheets/palette.ab4e12ef.min.css b/assets/stylesheets/palette.ab4e12ef.min.css index 5d1744112..cbb2f55ad 100644 --- a/assets/stylesheets/palette.ab4e12ef.min.css +++ b/assets/stylesheets/palette.ab4e12ef.min.css @@ -1,2 +1,2 @@ @media screen{[data-md-color-scheme=slate]{--md-default-fg-color:hsla(var(--md-hue),15%,90%,0.82);--md-default-fg-color--light:hsla(var(--md-hue),15%,90%,0.56);--md-default-fg-color--lighter:hsla(var(--md-hue),15%,90%,0.32);--md-default-fg-color--lightest:hsla(var(--md-hue),15%,90%,0.12);--md-default-bg-color:hsla(var(--md-hue),15%,14%,1);--md-default-bg-color--light:hsla(var(--md-hue),15%,14%,0.54);--md-default-bg-color--lighter:hsla(var(--md-hue),15%,14%,0.26);--md-default-bg-color--lightest:hsla(var(--md-hue),15%,14%,0.07);--md-code-fg-color:hsla(var(--md-hue),18%,86%,0.82);--md-code-bg-color:hsla(var(--md-hue),15%,18%,1);--md-code-bg-color--light:hsla(var(--md-hue),15%,18%,0.9);--md-code-bg-color--lighter:hsla(var(--md-hue),15%,18%,0.54);--md-code-hl-color:#2977ff;--md-code-hl-color--light:#2977ff1a;--md-code-hl-number-color:#e6695b;--md-code-hl-special-color:#f06090;--md-code-hl-function-color:#c973d9;--md-code-hl-constant-color:#9383e2;--md-code-hl-keyword-color:#6791e0;--md-code-hl-string-color:#2fb170;--md-code-hl-name-color:var(--md-code-fg-color);--md-code-hl-operator-color:var(--md-default-fg-color--light);--md-code-hl-punctuation-color:var(--md-default-fg-color--light);--md-code-hl-comment-color:var(--md-default-fg-color--light);--md-code-hl-generic-color:var(--md-default-fg-color--light);--md-code-hl-variable-color:var(--md-default-fg-color--light);--md-typeset-color:var(--md-default-fg-color);--md-typeset-a-color:var(--md-primary-fg-color);--md-typeset-kbd-color:hsla(var(--md-hue),15%,90%,0.12);--md-typeset-kbd-accent-color:hsla(var(--md-hue),15%,90%,0.2);--md-typeset-kbd-border-color:hsla(var(--md-hue),15%,14%,1);--md-typeset-mark-color:#4287ff4d;--md-typeset-table-color:hsla(var(--md-hue),15%,95%,0.12);--md-typeset-table-color--light:hsla(var(--md-hue),15%,95%,0.035);--md-admonition-fg-color:var(--md-default-fg-color);--md-admonition-bg-color:var(--md-default-bg-color);--md-footer-bg-color:hsla(var(--md-hue),15%,10%,0.87);--md-footer-bg-color--dark:hsla(var(--md-hue),15%,8%,1);--md-shadow-z1:0 0.2rem 0.5rem #0000000d,0 0 0.05rem #0000001a;--md-shadow-z2:0 0.2rem 0.5rem #00000040,0 0 0.05rem #00000040;--md-shadow-z3:0 0.2rem 0.5rem #0006,0 0 0.05rem #00000059;color-scheme:dark}[data-md-color-scheme=slate] img[src$="#gh-light-mode-only"],[data-md-color-scheme=slate] img[src$="#only-light"]{display:none}[data-md-color-scheme=slate][data-md-color-primary=pink]{--md-typeset-a-color:#ed5487}[data-md-color-scheme=slate][data-md-color-primary=purple]{--md-typeset-a-color:#c46fd3}[data-md-color-scheme=slate][data-md-color-primary=deep-purple]{--md-typeset-a-color:#a47bea}[data-md-color-scheme=slate][data-md-color-primary=indigo]{--md-typeset-a-color:#5488e8}[data-md-color-scheme=slate][data-md-color-primary=teal]{--md-typeset-a-color:#00ccb8}[data-md-color-scheme=slate][data-md-color-primary=green]{--md-typeset-a-color:#71c174}[data-md-color-scheme=slate][data-md-color-primary=deep-orange]{--md-typeset-a-color:#ff764d}[data-md-color-scheme=slate][data-md-color-primary=brown]{--md-typeset-a-color:#c1775c}[data-md-color-scheme=slate][data-md-color-primary=black],[data-md-color-scheme=slate][data-md-color-primary=blue-grey],[data-md-color-scheme=slate][data-md-color-primary=grey],[data-md-color-scheme=slate][data-md-color-primary=white]{--md-typeset-a-color:#5e8bde}[data-md-color-switching] *,[data-md-color-switching] :after,[data-md-color-switching] :before{transition-duration:0ms!important}}[data-md-color-accent=red]{--md-accent-fg-color:#ff1947;--md-accent-fg-color--transparent:#ff19471a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=pink]{--md-accent-fg-color:#f50056;--md-accent-fg-color--transparent:#f500561a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=purple]{--md-accent-fg-color:#df41fb;--md-accent-fg-color--transparent:#df41fb1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=deep-purple]{--md-accent-fg-color:#7c4dff;--md-accent-fg-color--transparent:#7c4dff1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=indigo]{--md-accent-fg-color:#526cfe;--md-accent-fg-color--transparent:#526cfe1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=blue]{--md-accent-fg-color:#4287ff;--md-accent-fg-color--transparent:#4287ff1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=light-blue]{--md-accent-fg-color:#0091eb;--md-accent-fg-color--transparent:#0091eb1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=cyan]{--md-accent-fg-color:#00bad6;--md-accent-fg-color--transparent:#00bad61a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=teal]{--md-accent-fg-color:#00bda4;--md-accent-fg-color--transparent:#00bda41a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=green]{--md-accent-fg-color:#00c753;--md-accent-fg-color--transparent:#00c7531a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=light-green]{--md-accent-fg-color:#63de17;--md-accent-fg-color--transparent:#63de171a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=lime]{--md-accent-fg-color:#b0eb00;--md-accent-fg-color--transparent:#b0eb001a;--md-accent-bg-color:#000000de;--md-accent-bg-color--light:#0000008a}[data-md-color-accent=yellow]{--md-accent-fg-color:#ffd500;--md-accent-fg-color--transparent:#ffd5001a;--md-accent-bg-color:#000000de;--md-accent-bg-color--light:#0000008a}[data-md-color-accent=amber]{--md-accent-fg-color:#fa0;--md-accent-fg-color--transparent:#ffaa001a;--md-accent-bg-color:#000000de;--md-accent-bg-color--light:#0000008a}[data-md-color-accent=orange]{--md-accent-fg-color:#ff9100;--md-accent-fg-color--transparent:#ff91001a;--md-accent-bg-color:#000000de;--md-accent-bg-color--light:#0000008a}[data-md-color-accent=deep-orange]{--md-accent-fg-color:#ff6e42;--md-accent-fg-color--transparent:#ff6e421a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-primary=red]{--md-primary-fg-color:#ef5552;--md-primary-fg-color--light:#e57171;--md-primary-fg-color--dark:#e53734;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=pink]{--md-primary-fg-color:#e92063;--md-primary-fg-color--light:#ec417a;--md-primary-fg-color--dark:#c3185d;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=purple]{--md-primary-fg-color:#ab47bd;--md-primary-fg-color--light:#bb69c9;--md-primary-fg-color--dark:#8c24a8;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=deep-purple]{--md-primary-fg-color:#7e56c2;--md-primary-fg-color--light:#9574cd;--md-primary-fg-color--dark:#673ab6;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=indigo]{--md-primary-fg-color:#4051b5;--md-primary-fg-color--light:#5d6cc0;--md-primary-fg-color--dark:#303fa1;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=blue]{--md-primary-fg-color:#2094f3;--md-primary-fg-color--light:#42a5f5;--md-primary-fg-color--dark:#1975d2;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=light-blue]{--md-primary-fg-color:#02a6f2;--md-primary-fg-color--light:#28b5f6;--md-primary-fg-color--dark:#0287cf;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=cyan]{--md-primary-fg-color:#00bdd6;--md-primary-fg-color--light:#25c5da;--md-primary-fg-color--dark:#0097a8;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=teal]{--md-primary-fg-color:#009485;--md-primary-fg-color--light:#26a699;--md-primary-fg-color--dark:#007a6c;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=green]{--md-primary-fg-color:#4cae4f;--md-primary-fg-color--light:#68bb6c;--md-primary-fg-color--dark:#398e3d;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=light-green]{--md-primary-fg-color:#8bc34b;--md-primary-fg-color--light:#9ccc66;--md-primary-fg-color--dark:#689f38;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=lime]{--md-primary-fg-color:#cbdc38;--md-primary-fg-color--light:#d3e156;--md-primary-fg-color--dark:#b0b52c;--md-primary-bg-color:#000000de;--md-primary-bg-color--light:#0000008a}[data-md-color-primary=yellow]{--md-primary-fg-color:#ffec3d;--md-primary-fg-color--light:#ffee57;--md-primary-fg-color--dark:#fbc02d;--md-primary-bg-color:#000000de;--md-primary-bg-color--light:#0000008a}[data-md-color-primary=amber]{--md-primary-fg-color:#ffc105;--md-primary-fg-color--light:#ffc929;--md-primary-fg-color--dark:#ffa200;--md-primary-bg-color:#000000de;--md-primary-bg-color--light:#0000008a}[data-md-color-primary=orange]{--md-primary-fg-color:#ffa724;--md-primary-fg-color--light:#ffa724;--md-primary-fg-color--dark:#fa8900;--md-primary-bg-color:#000000de;--md-primary-bg-color--light:#0000008a}[data-md-color-primary=deep-orange]{--md-primary-fg-color:#ff6e42;--md-primary-fg-color--light:#ff8a66;--md-primary-fg-color--dark:#f4511f;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=brown]{--md-primary-fg-color:#795649;--md-primary-fg-color--light:#8d6e62;--md-primary-fg-color--dark:#5d4037;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=grey]{--md-primary-fg-color:#757575;--md-primary-fg-color--light:#9e9e9e;--md-primary-fg-color--dark:#616161;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3;--md-typeset-a-color:#4051b5}[data-md-color-primary=blue-grey]{--md-primary-fg-color:#546d78;--md-primary-fg-color--light:#607c8a;--md-primary-fg-color--dark:#455a63;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3;--md-typeset-a-color:#4051b5}[data-md-color-primary=light-green]:not([data-md-color-scheme=slate]){--md-typeset-a-color:#72ad2e}[data-md-color-primary=lime]:not([data-md-color-scheme=slate]){--md-typeset-a-color:#8b990a}[data-md-color-primary=yellow]:not([data-md-color-scheme=slate]){--md-typeset-a-color:#b8a500}[data-md-color-primary=amber]:not([data-md-color-scheme=slate]){--md-typeset-a-color:#d19d00}[data-md-color-primary=orange]:not([data-md-color-scheme=slate]){--md-typeset-a-color:#e68a00}[data-md-color-primary=white]{--md-primary-fg-color:hsla(var(--md-hue),0%,100%,1);--md-primary-fg-color--light:hsla(var(--md-hue),0%,100%,0.7);--md-primary-fg-color--dark:hsla(var(--md-hue),0%,0%,0.07);--md-primary-bg-color:hsla(var(--md-hue),0%,0%,0.87);--md-primary-bg-color--light:hsla(var(--md-hue),0%,0%,0.54);--md-typeset-a-color:#4051b5}[data-md-color-primary=white] .md-button{color:var(--md-typeset-a-color)}[data-md-color-primary=white] .md-button--primary{background-color:var(--md-typeset-a-color);border-color:var(--md-typeset-a-color);color:hsla(var(--md-hue),0%,100%,1)}@media screen and (min-width:60em){[data-md-color-primary=white] .md-search__form{background-color:hsla(var(--md-hue),0%,0%,.07)}[data-md-color-primary=white] .md-search__form:hover{background-color:hsla(var(--md-hue),0%,0%,.32)}[data-md-color-primary=white] .md-search__input+.md-search__icon{color:hsla(var(--md-hue),0%,0%,.87)}}@media screen and (min-width:76.25em){[data-md-color-primary=white] .md-tabs{border-bottom:.05rem solid #00000012}}[data-md-color-primary=black]{--md-primary-fg-color:hsla(var(--md-hue),15%,9%,1);--md-primary-fg-color--light:hsla(var(--md-hue),15%,9%,0.54);--md-primary-fg-color--dark:hsla(var(--md-hue),15%,9%,1);--md-primary-bg-color:hsla(var(--md-hue),15%,100%,1);--md-primary-bg-color--light:hsla(var(--md-hue),15%,100%,0.7);--md-typeset-a-color:#4051b5}[data-md-color-primary=black] .md-button{color:var(--md-typeset-a-color)}[data-md-color-primary=black] .md-button--primary{background-color:var(--md-typeset-a-color);border-color:var(--md-typeset-a-color);color:hsla(var(--md-hue),0%,100%,1)}[data-md-color-primary=black] .md-header{background-color:hsla(var(--md-hue),15%,9%,1)}@media screen and (max-width:59.984375em){[data-md-color-primary=black] .md-nav__source{background-color:hsla(var(--md-hue),15%,11%,.87)}}@media screen and (max-width:76.234375em){html [data-md-color-primary=black] .md-nav--primary .md-nav__title[for=__drawer]{background-color:hsla(var(--md-hue),15%,9%,1)}}@media screen and (min-width:76.25em){[data-md-color-primary=black] .md-tabs{background-color:hsla(var(--md-hue),15%,9%,1)}} -/*! update cache: 20260104155727 */ +/*! update cache: 20260104161427 */ diff --git a/chapter_preface/about_the_book/index.html b/chapter_preface/about_the_book/index.html index 3287ac787..e21cf78b1 100644 --- a/chapter_preface/about_the_book/index.html +++ b/chapter_preface/about_the_book/index.html @@ -4506,7 +4506,7 @@

0.1.3   致谢

本书在开源社区众多贡献者的共同努力下不断完善。感谢每一位投入时间与精力的撰稿人,他们是(按照 GitHub 自动生成的顺序):krahets、coderonion、Gonglja、nuomi1、Reanon、justin-tse、hpstory、danielsss、curtishd、night-cruise、S-N-O-R-L-A-X、rongyi、msk397、gvenusleo、khoaxuantu、rivertwilight、K3v123、gyt95、zhuoqinyue、yuelinxin、Zuoxun、mingXta、Phoenix0415、FangYuan33、GN-Yu、longsizhuo、IsChristina、xBLACKICEx、guowei-gong、Cathay-Chen、pengchzn、QiLOL、magentaqin、hello-ikun、JoseHung、qualifier1024、thomasq0、sunshinesDL、L-Super、Guanngxu、Transmigration-zhou、WSL0809、Slone123c、lhxsm、yuan0221、what-is-me、Shyam-Chen、theNefelibatas、longranger2、codeberg-user、xiongsp、JeffersonHuang、prinpal、seven1240、Wonderdch、malone6、xiaomiusa87、gaofer、bluebean-cloud、a16su、SamJin98、hongyun-robot、nanlei、XiaChuerwu、yd-j、iron-irax、mgisr、steventimes、junminhong、heshuyue、danny900714、MolDuM、Nigh、Dr-XYZ、XC-Zero、reeswell、PXG-XPG、NI-SW、Horbin-Magician、Enlightenus、YangXuanyi、beatrix-chan、DullSword、xjr7670、jiaxianhua、qq909244296、iStig、boloboloda、hts0000、gledfish、wenjianmin、keshida、kilikilikid、lclc6、lwbaptx、linyejoe2、liuxjerry、llql1211、fbigm、echo1937、szu17dmy、dshlstarr、Yucao-cy、coderlef、czruby、bongbongbakudan、beintentional、ZongYangL、ZhongYuuu、ZhongGuanbin、hezhizhen、linzeyan、ZJKung、luluxia、xb534、ztkuaikuai、yw-1021、ElaBosak233、baagod、zhouLion、yishangzhang、yi427、yanedie、yabo083、weibk、wangwang105、th1nk3r-ing、tao363、4yDX3906、syd168、sslmj2020、smilelsb、siqyka、selear、sdshaoda、Xi-Row、popozhu、nuquist19、noobcodemaker、XiaoK29、chadyi、lyl625760、lucaswangdev、0130w、shanghai-Jerry、EJackYang、Javesun99、eltociear、lipusheng、KNChiu、BlindTerran、ShiMaRing、lovelock、FreddieLi、FloranceYeh、fanchenggang、gltianwen、goerll、nedchu、curly210102、CuB3y0nd、KraHsu、CarrotDLaw、youshaoXG、bubble9um、Asashishi、Asa0oo0o0o、fanenr、eagleanurag、akshiterate、52coder、foursevenlove、KorsChen、GaochaoZhu、hopkings2008、yang-le、realwujing、Evilrabbit520、Umer-Jahangir、Turing-1024-Lee、Suremotoo、paoxiaomooo、Chieko-Seren、Allen-Scai、ymmmas、Risuntsy、Richard-Zhang1019、RafaelCaso、qingpeng9802、primexiao、Urbaner3、zhongfq、nidhoggfgg、MwumLi、CreatorMetaSky、martinx、ZnYang2018、hugtyftg、logan-qiu、psychelzh、Keynman、KeiichiKasai 和 KawaiiAsh。

本书的代码审阅工作由 coderonion、curtishd、Gonglja、gvenusleo、hpstory、justin-tse、khoaxuantu、krahets、night-cruise、nuomi1、Reanon 和 rongyi 完成(按照首字母顺序排列)。感谢他们付出的时间与精力,正是他们确保了各语言代码的规范与统一。

-

本书的繁体中文版由 Shyam-Chen 和 Dr-XYZ 审阅,英文版由 yuelinxin、K3v123、QiLOL、Phoenix0415、SamJin98、yanedie、RafaelCaso、pengchzn、thomasq0 和 magentaqin 审阅。正是因为他们的持续贡献,这本书才能够服务于更广泛的读者群体,感谢他们。

+

本书的繁体中文版由 Shyam-Chen 和 Dr-XYZ 审阅,英文版由 yuelinxin、K3v123、QiLOL、Phoenix0415、SamJin98、yanedie、RafaelCaso、pengchzn、thomasq0 和 magentaqin 审阅,日文版由 eltociear 审阅。正是因为他们的持续贡献,这本书才能够服务于更广泛的读者群体,感谢他们。

本书的 ePub 电子书生成工具由 zhongfq 开发。感谢他的贡献,为读者提供了更加自由的阅读方式。

在本书的创作过程中,我得到了许多人的帮助。

    diff --git a/en/assets/javascripts/bundle.79ae519e.min.js b/en/assets/javascripts/bundle.79ae519e.min.js index 64b14c0ff..b06f731c2 100644 --- a/en/assets/javascripts/bundle.79ae519e.min.js +++ b/en/assets/javascripts/bundle.79ae519e.min.js @@ -14,4 +14,4 @@ `):"",this.name="UnsubscriptionError",this.errors=r}});function Ze(e,t){if(e){var r=e.indexOf(t);0<=r&&e.splice(r,1)}}var qe=(function(){function e(t){this.initialTeardown=t,this.closed=!1,this._parentage=null,this._finalizers=null}return e.prototype.unsubscribe=function(){var t,r,o,n,i;if(!this.closed){this.closed=!0;var s=this._parentage;if(s)if(this._parentage=null,Array.isArray(s))try{for(var a=Oe(s),c=a.next();!c.done;c=a.next()){var p=c.value;p.remove(this)}}catch(S){t={error:S}}finally{try{c&&!c.done&&(r=a.return)&&r.call(a)}finally{if(t)throw t.error}}else s.remove(this);var l=this.initialTeardown;if(I(l))try{l()}catch(S){i=S instanceof Jt?S.errors:[S]}var f=this._finalizers;if(f){this._finalizers=null;try{for(var u=Oe(f),d=u.next();!d.done;d=u.next()){var v=d.value;try{So(v)}catch(S){i=i!=null?i:[],S instanceof Jt?i=B(B([],K(i)),K(S.errors)):i.push(S)}}}catch(S){o={error:S}}finally{try{d&&!d.done&&(n=u.return)&&n.call(u)}finally{if(o)throw o.error}}}if(i)throw new Jt(i)}},e.prototype.add=function(t){var r;if(t&&t!==this)if(this.closed)So(t);else{if(t instanceof e){if(t.closed||t._hasParent(this))return;t._addParent(this)}(this._finalizers=(r=this._finalizers)!==null&&r!==void 0?r:[]).push(t)}},e.prototype._hasParent=function(t){var r=this._parentage;return r===t||Array.isArray(r)&&r.includes(t)},e.prototype._addParent=function(t){var r=this._parentage;this._parentage=Array.isArray(r)?(r.push(t),r):r?[r,t]:t},e.prototype._removeParent=function(t){var r=this._parentage;r===t?this._parentage=null:Array.isArray(r)&&Ze(r,t)},e.prototype.remove=function(t){var r=this._finalizers;r&&Ze(r,t),t instanceof e&&t._removeParent(this)},e.EMPTY=(function(){var t=new e;return t.closed=!0,t})(),e})();var $r=qe.EMPTY;function Xt(e){return e instanceof qe||e&&"closed"in e&&I(e.remove)&&I(e.add)&&I(e.unsubscribe)}function So(e){I(e)?e():e.unsubscribe()}var De={onUnhandledError:null,onStoppedNotification:null,Promise:void 0,useDeprecatedSynchronousErrorHandling:!1,useDeprecatedNextContext:!1};var xt={setTimeout:function(e,t){for(var r=[],o=2;o0},enumerable:!1,configurable:!0}),t.prototype._trySubscribe=function(r){return this._throwIfClosed(),e.prototype._trySubscribe.call(this,r)},t.prototype._subscribe=function(r){return this._throwIfClosed(),this._checkFinalizedStatuses(r),this._innerSubscribe(r)},t.prototype._innerSubscribe=function(r){var o=this,n=this,i=n.hasError,s=n.isStopped,a=n.observers;return i||s?$r:(this.currentObservers=null,a.push(r),new qe(function(){o.currentObservers=null,Ze(a,r)}))},t.prototype._checkFinalizedStatuses=function(r){var o=this,n=o.hasError,i=o.thrownError,s=o.isStopped;n?r.error(i):s&&r.complete()},t.prototype.asObservable=function(){var r=new F;return r.source=this,r},t.create=function(r,o){return new Ho(r,o)},t})(F);var Ho=(function(e){ie(t,e);function t(r,o){var n=e.call(this)||this;return n.destination=r,n.source=o,n}return t.prototype.next=function(r){var o,n;(n=(o=this.destination)===null||o===void 0?void 0:o.next)===null||n===void 0||n.call(o,r)},t.prototype.error=function(r){var o,n;(n=(o=this.destination)===null||o===void 0?void 0:o.error)===null||n===void 0||n.call(o,r)},t.prototype.complete=function(){var r,o;(o=(r=this.destination)===null||r===void 0?void 0:r.complete)===null||o===void 0||o.call(r)},t.prototype._subscribe=function(r){var o,n;return(n=(o=this.source)===null||o===void 0?void 0:o.subscribe(r))!==null&&n!==void 0?n:$r},t})(T);var jr=(function(e){ie(t,e);function t(r){var o=e.call(this)||this;return o._value=r,o}return Object.defineProperty(t.prototype,"value",{get:function(){return this.getValue()},enumerable:!1,configurable:!0}),t.prototype._subscribe=function(r){var o=e.prototype._subscribe.call(this,r);return!o.closed&&r.next(this._value),o},t.prototype.getValue=function(){var r=this,o=r.hasError,n=r.thrownError,i=r._value;if(o)throw n;return this._throwIfClosed(),i},t.prototype.next=function(r){e.prototype.next.call(this,this._value=r)},t})(T);var Rt={now:function(){return(Rt.delegate||Date).now()},delegate:void 0};var It=(function(e){ie(t,e);function t(r,o,n){r===void 0&&(r=1/0),o===void 0&&(o=1/0),n===void 0&&(n=Rt);var i=e.call(this)||this;return i._bufferSize=r,i._windowTime=o,i._timestampProvider=n,i._buffer=[],i._infiniteTimeWindow=!0,i._infiniteTimeWindow=o===1/0,i._bufferSize=Math.max(1,r),i._windowTime=Math.max(1,o),i}return t.prototype.next=function(r){var o=this,n=o.isStopped,i=o._buffer,s=o._infiniteTimeWindow,a=o._timestampProvider,c=o._windowTime;n||(i.push(r),!s&&i.push(a.now()+c)),this._trimBuffer(),e.prototype.next.call(this,r)},t.prototype._subscribe=function(r){this._throwIfClosed(),this._trimBuffer();for(var o=this._innerSubscribe(r),n=this,i=n._infiniteTimeWindow,s=n._buffer,a=s.slice(),c=0;c0?e.prototype.schedule.call(this,r,o):(this.delay=o,this.state=r,this.scheduler.flush(this),this)},t.prototype.execute=function(r,o){return o>0||this.closed?e.prototype.execute.call(this,r,o):this._execute(r,o)},t.prototype.requestAsyncId=function(r,o,n){return n===void 0&&(n=0),n!=null&&n>0||n==null&&this.delay>0?e.prototype.requestAsyncId.call(this,r,o,n):(r.flush(this),0)},t})(St);var Ro=(function(e){ie(t,e);function t(){return e!==null&&e.apply(this,arguments)||this}return t})(Ot);var Dr=new Ro(Po);var Io=(function(e){ie(t,e);function t(r,o){var n=e.call(this,r,o)||this;return n.scheduler=r,n.work=o,n}return t.prototype.requestAsyncId=function(r,o,n){return n===void 0&&(n=0),n!==null&&n>0?e.prototype.requestAsyncId.call(this,r,o,n):(r.actions.push(this),r._scheduled||(r._scheduled=Tt.requestAnimationFrame(function(){return r.flush(void 0)})))},t.prototype.recycleAsyncId=function(r,o,n){var i;if(n===void 0&&(n=0),n!=null?n>0:this.delay>0)return e.prototype.recycleAsyncId.call(this,r,o,n);var s=r.actions;o!=null&&o===r._scheduled&&((i=s[s.length-1])===null||i===void 0?void 0:i.id)!==o&&(Tt.cancelAnimationFrame(o),r._scheduled=void 0)},t})(St);var Fo=(function(e){ie(t,e);function t(){return e!==null&&e.apply(this,arguments)||this}return t.prototype.flush=function(r){this._active=!0;var o;r?o=r.id:(o=this._scheduled,this._scheduled=void 0);var n=this.actions,i;r=r||n.shift();do if(i=r.execute(r.state,r.delay))break;while((r=n[0])&&r.id===o&&n.shift());if(this._active=!1,i){for(;(r=n[0])&&r.id===o&&n.shift();)r.unsubscribe();throw i}},t})(Ot);var ye=new Fo(Io);var y=new F(function(e){return e.complete()});function tr(e){return e&&I(e.schedule)}function Vr(e){return e[e.length-1]}function pt(e){return I(Vr(e))?e.pop():void 0}function Fe(e){return tr(Vr(e))?e.pop():void 0}function rr(e,t){return typeof Vr(e)=="number"?e.pop():t}var Lt=(function(e){return e&&typeof e.length=="number"&&typeof e!="function"});function or(e){return I(e==null?void 0:e.then)}function nr(e){return I(e[wt])}function ir(e){return Symbol.asyncIterator&&I(e==null?void 0:e[Symbol.asyncIterator])}function ar(e){return new TypeError("You provided "+(e!==null&&typeof e=="object"?"an invalid object":"'"+e+"'")+" where a stream was expected. You can provide an Observable, Promise, ReadableStream, Array, AsyncIterable, or Iterable.")}function fa(){return typeof Symbol!="function"||!Symbol.iterator?"@@iterator":Symbol.iterator}var sr=fa();function cr(e){return I(e==null?void 0:e[sr])}function pr(e){return wo(this,arguments,function(){var r,o,n,i;return Gt(this,function(s){switch(s.label){case 0:r=e.getReader(),s.label=1;case 1:s.trys.push([1,,9,10]),s.label=2;case 2:return[4,dt(r.read())];case 3:return o=s.sent(),n=o.value,i=o.done,i?[4,dt(void 0)]:[3,5];case 4:return[2,s.sent()];case 5:return[4,dt(n)];case 6:return[4,s.sent()];case 7:return s.sent(),[3,2];case 8:return[3,10];case 9:return r.releaseLock(),[7];case 10:return[2]}})})}function lr(e){return I(e==null?void 0:e.getReader)}function U(e){if(e instanceof F)return e;if(e!=null){if(nr(e))return ua(e);if(Lt(e))return da(e);if(or(e))return ha(e);if(ir(e))return jo(e);if(cr(e))return ba(e);if(lr(e))return va(e)}throw ar(e)}function ua(e){return new F(function(t){var r=e[wt]();if(I(r.subscribe))return r.subscribe(t);throw new TypeError("Provided object does not correctly implement Symbol.observable")})}function da(e){return new F(function(t){for(var r=0;r=2;return function(o){return o.pipe(e?g(function(n,i){return e(n,i,o)}):be,Ee(1),r?Qe(t):tn(function(){return new fr}))}}function Yr(e){return e<=0?function(){return y}:E(function(t,r){var o=[];t.subscribe(w(r,function(n){o.push(n),e=2,!0))}function le(e){e===void 0&&(e={});var t=e.connector,r=t===void 0?function(){return new T}:t,o=e.resetOnError,n=o===void 0?!0:o,i=e.resetOnComplete,s=i===void 0?!0:i,a=e.resetOnRefCountZero,c=a===void 0?!0:a;return function(p){var l,f,u,d=0,v=!1,S=!1,X=function(){f==null||f.unsubscribe(),f=void 0},re=function(){X(),l=u=void 0,v=S=!1},ee=function(){var k=l;re(),k==null||k.unsubscribe()};return E(function(k,ut){d++,!S&&!v&&X();var je=u=u!=null?u:r();ut.add(function(){d--,d===0&&!S&&!v&&(f=Br(ee,c))}),je.subscribe(ut),!l&&d>0&&(l=new bt({next:function(R){return je.next(R)},error:function(R){S=!0,X(),f=Br(re,n,R),je.error(R)},complete:function(){v=!0,X(),f=Br(re,s),je.complete()}}),U(k).subscribe(l))})(p)}}function Br(e,t){for(var r=[],o=2;oe.next(document)),e}function M(e,t=document){return Array.from(t.querySelectorAll(e))}function j(e,t=document){let r=ue(e,t);if(typeof r=="undefined")throw new ReferenceError(`Missing element: expected "${e}" to be present`);return r}function ue(e,t=document){return t.querySelector(e)||void 0}function Ne(){var e,t,r,o;return(o=(r=(t=(e=document.activeElement)==null?void 0:e.shadowRoot)==null?void 0:t.activeElement)!=null?r:document.activeElement)!=null?o:void 0}var Ra=L(h(document.body,"focusin"),h(document.body,"focusout")).pipe(Ae(1),Q(void 0),m(()=>Ne()||document.body),Z(1));function Ye(e){return Ra.pipe(m(t=>e.contains(t)),Y())}function it(e,t){return H(()=>L(h(e,"mouseenter").pipe(m(()=>!0)),h(e,"mouseleave").pipe(m(()=>!1))).pipe(t?jt(r=>He(+!r*t)):be,Q(e.matches(":hover"))))}function sn(e,t){if(typeof t=="string"||typeof t=="number")e.innerHTML+=t.toString();else if(t instanceof Node)e.appendChild(t);else if(Array.isArray(t))for(let r of t)sn(e,r)}function x(e,t,...r){let o=document.createElement(e);if(t)for(let n of Object.keys(t))typeof t[n]!="undefined"&&(typeof t[n]!="boolean"?o.setAttribute(n,t[n]):o.setAttribute(n,""));for(let n of r)sn(o,n);return o}function br(e){if(e>999){let t=+((e-950)%1e3>99);return`${((e+1e-6)/1e3).toFixed(t)}k`}else return e.toString()}function _t(e){let t=x("script",{src:e});return H(()=>(document.head.appendChild(t),L(h(t,"load"),h(t,"error").pipe(b(()=>Nr(()=>new ReferenceError(`Invalid script: ${e}`))))).pipe(m(()=>{}),A(()=>document.head.removeChild(t)),Ee(1))))}var cn=new T,Ia=H(()=>typeof ResizeObserver=="undefined"?_t("https://unpkg.com/resize-observer-polyfill"):$(void 0)).pipe(m(()=>new ResizeObserver(e=>e.forEach(t=>cn.next(t)))),b(e=>L(tt,$(e)).pipe(A(()=>e.disconnect()))),Z(1));function de(e){return{width:e.offsetWidth,height:e.offsetHeight}}function Le(e){let t=e;for(;t.clientWidth===0&&t.parentElement;)t=t.parentElement;return Ia.pipe(O(r=>r.observe(t)),b(r=>cn.pipe(g(o=>o.target===t),A(()=>r.unobserve(t)))),m(()=>de(e)),Q(de(e)))}function At(e){return{width:e.scrollWidth,height:e.scrollHeight}}function vr(e){let t=e.parentElement;for(;t&&(e.scrollWidth<=t.scrollWidth&&e.scrollHeight<=t.scrollHeight);)t=(e=t).parentElement;return t?e:void 0}function pn(e){let t=[],r=e.parentElement;for(;r;)(e.clientWidth>r.clientWidth||e.clientHeight>r.clientHeight)&&t.push(r),r=(e=r).parentElement;return t.length===0&&t.push(document.documentElement),t}function Be(e){return{x:e.offsetLeft,y:e.offsetTop}}function ln(e){let t=e.getBoundingClientRect();return{x:t.x+window.scrollX,y:t.y+window.scrollY}}function mn(e){return L(h(window,"load"),h(window,"resize")).pipe($e(0,ye),m(()=>Be(e)),Q(Be(e)))}function gr(e){return{x:e.scrollLeft,y:e.scrollTop}}function Ge(e){return L(h(e,"scroll"),h(window,"scroll"),h(window,"resize")).pipe($e(0,ye),m(()=>gr(e)),Q(gr(e)))}var fn=new T,Fa=H(()=>$(new IntersectionObserver(e=>{for(let t of e)fn.next(t)},{threshold:0}))).pipe(b(e=>L(tt,$(e)).pipe(A(()=>e.disconnect()))),Z(1));function mt(e){return Fa.pipe(O(t=>t.observe(e)),b(t=>fn.pipe(g(({target:r})=>r===e),A(()=>t.unobserve(e)),m(({isIntersecting:r})=>r))))}function un(e,t=16){return Ge(e).pipe(m(({y:r})=>{let o=de(e),n=At(e);return r>=n.height-o.height-t}),Y())}var yr={drawer:j("[data-md-toggle=drawer]"),search:j("[data-md-toggle=search]")};function dn(e){return yr[e].checked}function at(e,t){yr[e].checked!==t&&yr[e].click()}function Je(e){let t=yr[e];return h(t,"change").pipe(m(()=>t.checked),Q(t.checked))}function ja(e,t){switch(e.constructor){case HTMLInputElement:return e.type==="radio"?/^Arrow/.test(t):!0;case HTMLSelectElement:case HTMLTextAreaElement:return!0;default:return e.isContentEditable}}function Ua(){return L(h(window,"compositionstart").pipe(m(()=>!0)),h(window,"compositionend").pipe(m(()=>!1))).pipe(Q(!1))}function hn(){let e=h(window,"keydown").pipe(g(t=>!(t.metaKey||t.ctrlKey)),m(t=>({mode:dn("search")?"search":"global",type:t.key,claim(){t.preventDefault(),t.stopPropagation()}})),g(({mode:t,type:r})=>{if(t==="global"){let o=Ne();if(typeof o!="undefined")return!ja(o,r)}return!0}),le());return Ua().pipe(b(t=>t?y:e))}function we(){return new URL(location.href)}function st(e,t=!1){if(V("navigation.instant")&&!t){let r=x("a",{href:e.href});document.body.appendChild(r),r.click(),r.remove()}else location.href=e.href}function bn(){return new T}function vn(){return location.hash.slice(1)}function gn(e){let t=x("a",{href:e});t.addEventListener("click",r=>r.stopPropagation()),t.click()}function Zr(e){return L(h(window,"hashchange"),e).pipe(m(vn),Q(vn()),g(t=>t.length>0),Z(1))}function yn(e){return Zr(e).pipe(m(t=>ue(`[id="${t}"]`)),g(t=>typeof t!="undefined"))}function Wt(e){let t=matchMedia(e);return ur(r=>t.addListener(()=>r(t.matches))).pipe(Q(t.matches))}function xn(){let e=matchMedia("print");return L(h(window,"beforeprint").pipe(m(()=>!0)),h(window,"afterprint").pipe(m(()=>!1))).pipe(Q(e.matches))}function eo(e,t){return e.pipe(b(r=>r?t():y))}function to(e,t){return new F(r=>{let o=new XMLHttpRequest;return o.open("GET",`${e}`),o.responseType="blob",o.addEventListener("load",()=>{o.status>=200&&o.status<300?(r.next(o.response),r.complete()):r.error(new Error(o.statusText))}),o.addEventListener("error",()=>{r.error(new Error("Network error"))}),o.addEventListener("abort",()=>{r.complete()}),typeof(t==null?void 0:t.progress$)!="undefined"&&(o.addEventListener("progress",n=>{var i;if(n.lengthComputable)t.progress$.next(n.loaded/n.total*100);else{let s=(i=o.getResponseHeader("Content-Length"))!=null?i:0;t.progress$.next(n.loaded/+s*100)}}),t.progress$.next(5)),o.send(),()=>o.abort()})}function ze(e,t){return to(e,t).pipe(b(r=>r.text()),m(r=>JSON.parse(r)),Z(1))}function xr(e,t){let r=new DOMParser;return to(e,t).pipe(b(o=>o.text()),m(o=>r.parseFromString(o,"text/html")),Z(1))}function En(e,t){let r=new DOMParser;return to(e,t).pipe(b(o=>o.text()),m(o=>r.parseFromString(o,"text/xml")),Z(1))}function wn(){return{x:Math.max(0,scrollX),y:Math.max(0,scrollY)}}function Tn(){return L(h(window,"scroll",{passive:!0}),h(window,"resize",{passive:!0})).pipe(m(wn),Q(wn()))}function Sn(){return{width:innerWidth,height:innerHeight}}function On(){return h(window,"resize",{passive:!0}).pipe(m(Sn),Q(Sn()))}function Ln(){return z([Tn(),On()]).pipe(m(([e,t])=>({offset:e,size:t})),Z(1))}function Er(e,{viewport$:t,header$:r}){let o=t.pipe(ne("size")),n=z([o,r]).pipe(m(()=>Be(e)));return z([r,t,n]).pipe(m(([{height:i},{offset:s,size:a},{x:c,y:p}])=>({offset:{x:s.x-c,y:s.y-p+i},size:a})))}function Wa(e){return h(e,"message",t=>t.data)}function Da(e){let t=new T;return t.subscribe(r=>e.postMessage(r)),t}function Mn(e,t=new Worker(e)){let r=Wa(t),o=Da(t),n=new T;n.subscribe(o);let i=o.pipe(oe(),ae(!0));return n.pipe(oe(),Ve(r.pipe(W(i))),le())}var Va=j("#__config"),Ct=JSON.parse(Va.textContent);Ct.base=`${new URL(Ct.base,we())}`;function Te(){return Ct}function V(e){return Ct.features.includes(e)}function Me(e,t){return typeof t!="undefined"?Ct.translations[e].replace("#",t.toString()):Ct.translations[e]}function Ce(e,t=document){return j(`[data-md-component=${e}]`,t)}function me(e,t=document){return M(`[data-md-component=${e}]`,t)}function Na(e){let t=j(".md-typeset > :first-child",e);return h(t,"click",{once:!0}).pipe(m(()=>j(".md-typeset",e)),m(r=>({hash:__md_hash(r.innerHTML)})))}function _n(e){if(!V("announce.dismiss")||!e.childElementCount)return y;if(!e.hidden){let t=j(".md-typeset",e);__md_hash(t.innerHTML)===__md_get("__announce")&&(e.hidden=!0)}return H(()=>{let t=new T;return t.subscribe(({hash:r})=>{e.hidden=!0,__md_set("__announce",r)}),Na(e).pipe(O(r=>t.next(r)),A(()=>t.complete()),m(r=>P({ref:e},r)))})}function za(e,{target$:t}){return t.pipe(m(r=>({hidden:r!==e})))}function An(e,t){let r=new T;return r.subscribe(({hidden:o})=>{e.hidden=o}),za(e,t).pipe(O(o=>r.next(o)),A(()=>r.complete()),m(o=>P({ref:e},o)))}function Dt(e,t){return t==="inline"?x("div",{class:"md-tooltip md-tooltip--inline",id:e,role:"tooltip"},x("div",{class:"md-tooltip__inner md-typeset"})):x("div",{class:"md-tooltip",id:e,role:"tooltip"},x("div",{class:"md-tooltip__inner md-typeset"}))}function wr(...e){return x("div",{class:"md-tooltip2",role:"dialog"},x("div",{class:"md-tooltip2__inner md-typeset"},e))}function Cn(...e){return x("div",{class:"md-tooltip2",role:"tooltip"},x("div",{class:"md-tooltip2__inner md-typeset"},e))}function kn(e,t){if(t=t?`${t}_annotation_${e}`:void 0,t){let r=t?`#${t}`:void 0;return x("aside",{class:"md-annotation",tabIndex:0},Dt(t),x("a",{href:r,class:"md-annotation__index",tabIndex:-1},x("span",{"data-md-annotation-id":e})))}else return x("aside",{class:"md-annotation",tabIndex:0},Dt(t),x("span",{class:"md-annotation__index",tabIndex:-1},x("span",{"data-md-annotation-id":e})))}function Hn(e){return x("button",{class:"md-code__button",title:Me("clipboard.copy"),"data-clipboard-target":`#${e} > code`,"data-md-type":"copy"})}function $n(){return x("button",{class:"md-code__button",title:"Toggle line selection","data-md-type":"select"})}function Pn(){return x("nav",{class:"md-code__nav"})}var In=$t(ro());function oo(e,t){let r=t&2,o=t&1,n=Object.keys(e.terms).filter(c=>!e.terms[c]).reduce((c,p)=>[...c,x("del",null,(0,In.default)(p))," "],[]).slice(0,-1),i=Te(),s=new URL(e.location,i.base);V("search.highlight")&&s.searchParams.set("h",Object.entries(e.terms).filter(([,c])=>c).reduce((c,[p])=>`${c} ${p}`.trim(),""));let{tags:a}=Te();return x("a",{href:`${s}`,class:"md-search-result__link",tabIndex:-1},x("article",{class:"md-search-result__article md-typeset","data-md-score":e.score.toFixed(2)},r>0&&x("div",{class:"md-search-result__icon md-icon"}),r>0&&x("h1",null,e.title),r<=0&&x("h2",null,e.title),o>0&&e.text.length>0&&e.text,e.tags&&x("nav",{class:"md-tags"},e.tags.map(c=>{let p=a?c in a?`md-tag-icon md-tag--${a[c]}`:"md-tag-icon":"";return x("span",{class:`md-tag ${p}`},c)})),o>0&&n.length>0&&x("p",{class:"md-search-result__terms"},Me("search.result.term.missing"),": ",...n)))}function Fn(e){let t=e[0].score,r=[...e],o=Te(),n=r.findIndex(l=>!`${new URL(l.location,o.base)}`.includes("#")),[i]=r.splice(n,1),s=r.findIndex(l=>l.scoreoo(l,1)),...c.length?[x("details",{class:"md-search-result__more"},x("summary",{tabIndex:-1},x("div",null,c.length>0&&c.length===1?Me("search.result.more.one"):Me("search.result.more.other",c.length))),...c.map(l=>oo(l,1)))]:[]];return x("li",{class:"md-search-result__item"},p)}function jn(e){return x("ul",{class:"md-source__facts"},Object.entries(e).map(([t,r])=>x("li",{class:`md-source__fact md-source__fact--${t}`},typeof r=="number"?br(r):r)))}function no(e){let t=`tabbed-control tabbed-control--${e}`;return x("div",{class:t,hidden:!0},x("button",{class:"tabbed-button",tabIndex:-1,"aria-hidden":"true"}))}function Un(e){return x("div",{class:"md-typeset__scrollwrap"},x("div",{class:"md-typeset__table"},e))}function Qa(e){var o;let t=Te(),r=new URL(`../${e.version}/`,t.base);return x("li",{class:"md-version__item"},x("a",{href:`${r}`,class:"md-version__link"},e.title,((o=t.version)==null?void 0:o.alias)&&e.aliases.length>0&&x("span",{class:"md-version__alias"},e.aliases[0])))}function Wn(e,t){var o;let r=Te();return e=e.filter(n=>{var i;return!((i=n.properties)!=null&&i.hidden)}),x("div",{class:"md-version"},x("button",{class:"md-version__current","aria-label":Me("select.version")},t.title,((o=r.version)==null?void 0:o.alias)&&t.aliases.length>0&&x("span",{class:"md-version__alias"},t.aliases[0])),x("ul",{class:"md-version__list"},e.map(Qa)))}var Ya=0;function Ba(e,t=250){let r=z([Ye(e),it(e,t)]).pipe(m(([n,i])=>n||i),Y()),o=H(()=>pn(e)).pipe(J(Ge),gt(1),Pe(r),m(()=>ln(e)));return r.pipe(Re(n=>n),b(()=>z([r,o])),m(([n,i])=>({active:n,offset:i})),le())}function Vt(e,t,r=250){let{content$:o,viewport$:n}=t,i=`__tooltip2_${Ya++}`;return H(()=>{let s=new T,a=new jr(!1);s.pipe(oe(),ae(!1)).subscribe(a);let c=a.pipe(jt(l=>He(+!l*250,Dr)),Y(),b(l=>l?o:y),O(l=>l.id=i),le());z([s.pipe(m(({active:l})=>l)),c.pipe(b(l=>it(l,250)),Q(!1))]).pipe(m(l=>l.some(f=>f))).subscribe(a);let p=a.pipe(g(l=>l),te(c,n),m(([l,f,{size:u}])=>{let d=e.getBoundingClientRect(),v=d.width/2;if(f.role==="tooltip")return{x:v,y:8+d.height};if(d.y>=u.height/2){let{height:S}=de(f);return{x:v,y:-16-S}}else return{x:v,y:16+d.height}}));return z([c,s,p]).subscribe(([l,{offset:f},u])=>{l.style.setProperty("--md-tooltip-host-x",`${f.x}px`),l.style.setProperty("--md-tooltip-host-y",`${f.y}px`),l.style.setProperty("--md-tooltip-x",`${u.x}px`),l.style.setProperty("--md-tooltip-y",`${u.y}px`),l.classList.toggle("md-tooltip2--top",u.y<0),l.classList.toggle("md-tooltip2--bottom",u.y>=0)}),a.pipe(g(l=>l),te(c,(l,f)=>f),g(l=>l.role==="tooltip")).subscribe(l=>{let f=de(j(":scope > *",l));l.style.setProperty("--md-tooltip-width",`${f.width}px`),l.style.setProperty("--md-tooltip-tail","0px")}),a.pipe(Y(),xe(ye),te(c)).subscribe(([l,f])=>{f.classList.toggle("md-tooltip2--active",l)}),z([a.pipe(g(l=>l)),c]).subscribe(([l,f])=>{f.role==="dialog"?(e.setAttribute("aria-controls",i),e.setAttribute("aria-haspopup","dialog")):e.setAttribute("aria-describedby",i)}),a.pipe(g(l=>!l)).subscribe(()=>{e.removeAttribute("aria-controls"),e.removeAttribute("aria-describedby"),e.removeAttribute("aria-haspopup")}),Ba(e,r).pipe(O(l=>s.next(l)),A(()=>s.complete()),m(l=>P({ref:e},l)))})}function Xe(e,{viewport$:t},r=document.body){return Vt(e,{content$:new F(o=>{let n=e.title,i=Cn(n);return o.next(i),e.removeAttribute("title"),r.append(i),()=>{i.remove(),e.setAttribute("title",n)}}),viewport$:t},0)}function Ga(e,t){let r=H(()=>z([mn(e),Ge(t)])).pipe(m(([{x:o,y:n},i])=>{let{width:s,height:a}=de(e);return{x:o-i.x+s/2,y:n-i.y+a/2}}));return Ye(e).pipe(b(o=>r.pipe(m(n=>({active:o,offset:n})),Ee(+!o||1/0))))}function Dn(e,t,{target$:r}){let[o,n]=Array.from(e.children);return H(()=>{let i=new T,s=i.pipe(oe(),ae(!0));return i.subscribe({next({offset:a}){e.style.setProperty("--md-tooltip-x",`${a.x}px`),e.style.setProperty("--md-tooltip-y",`${a.y}px`)},complete(){e.style.removeProperty("--md-tooltip-x"),e.style.removeProperty("--md-tooltip-y")}}),mt(e).pipe(W(s)).subscribe(a=>{e.toggleAttribute("data-md-visible",a)}),L(i.pipe(g(({active:a})=>a)),i.pipe(Ae(250),g(({active:a})=>!a))).subscribe({next({active:a}){a?e.prepend(o):o.remove()},complete(){e.prepend(o)}}),i.pipe($e(16,ye)).subscribe(({active:a})=>{o.classList.toggle("md-tooltip--active",a)}),i.pipe(gt(125,ye),g(()=>!!e.offsetParent),m(()=>e.offsetParent.getBoundingClientRect()),m(({x:a})=>a)).subscribe({next(a){a?e.style.setProperty("--md-tooltip-0",`${-a}px`):e.style.removeProperty("--md-tooltip-0")},complete(){e.style.removeProperty("--md-tooltip-0")}}),h(n,"click").pipe(W(s),g(a=>!(a.metaKey||a.ctrlKey))).subscribe(a=>{a.stopPropagation(),a.preventDefault()}),h(n,"mousedown").pipe(W(s),te(i)).subscribe(([a,{active:c}])=>{var p;if(a.button!==0||a.metaKey||a.ctrlKey)a.preventDefault();else if(c){a.preventDefault();let l=e.parentElement.closest(".md-annotation");l instanceof HTMLElement?l.focus():(p=Ne())==null||p.blur()}}),r.pipe(W(s),g(a=>a===o),nt(125)).subscribe(()=>e.focus()),Ga(e,t).pipe(O(a=>i.next(a)),A(()=>i.complete()),m(a=>P({ref:e},a)))})}function Ja(e){let t=Te();if(e.tagName!=="CODE")return[e];let r=[".c",".c1",".cm"];if(t.annotate&&typeof t.annotate=="object"){let o=e.closest("[class|=language]");if(o)for(let n of Array.from(o.classList)){if(!n.startsWith("language-"))continue;let[,i]=n.split("-");i in t.annotate&&r.push(...t.annotate[i])}}return M(r.join(", "),e)}function Xa(e){let t=[];for(let r of Ja(e)){let o=[],n=document.createNodeIterator(r,NodeFilter.SHOW_TEXT);for(let i=n.nextNode();i;i=n.nextNode())o.push(i);for(let i of o){let s;for(;s=/(\(\d+\))(!)?/.exec(i.textContent);){let[,a,c]=s;if(typeof c=="undefined"){let p=i.splitText(s.index);i=p.splitText(a.length),t.push(p)}else{i.textContent=a,t.push(i);break}}}}return t}function Vn(e,t){t.append(...Array.from(e.childNodes))}function Tr(e,t,{target$:r,print$:o}){let n=t.closest("[id]"),i=n==null?void 0:n.id,s=new Map;for(let a of Xa(t)){let[,c]=a.textContent.match(/\((\d+)\)/);ue(`:scope > li:nth-child(${c})`,e)&&(s.set(c,kn(c,i)),a.replaceWith(s.get(c)))}return s.size===0?y:H(()=>{let a=new T,c=a.pipe(oe(),ae(!0)),p=[];for(let[l,f]of s)p.push([j(".md-typeset",f),j(`:scope > li:nth-child(${l})`,e)]);return o.pipe(W(c)).subscribe(l=>{e.hidden=!l,e.classList.toggle("md-annotation-list",l);for(let[f,u]of p)l?Vn(f,u):Vn(u,f)}),L(...[...s].map(([,l])=>Dn(l,t,{target$:r}))).pipe(A(()=>a.complete()),le())})}function Nn(e){if(e.nextElementSibling){let t=e.nextElementSibling;if(t.tagName==="OL")return t;if(t.tagName==="P"&&!t.children.length)return Nn(t)}}function zn(e,t){return H(()=>{let r=Nn(e);return typeof r!="undefined"?Tr(r,e,t):y})}var Kn=$t(ao());var Za=0,qn=L(h(window,"keydown").pipe(m(()=>!0)),L(h(window,"keyup"),h(window,"contextmenu")).pipe(m(()=>!1))).pipe(Q(!1),Z(1));function Qn(e){if(e.nextElementSibling){let t=e.nextElementSibling;if(t.tagName==="OL")return t;if(t.tagName==="P"&&!t.children.length)return Qn(t)}}function es(e){return Le(e).pipe(m(({width:t})=>({scrollable:At(e).width>t})),ne("scrollable"))}function Yn(e,t){let{matches:r}=matchMedia("(hover)"),o=H(()=>{let n=new T,i=n.pipe(Yr(1));n.subscribe(({scrollable:d})=>{d&&r?e.setAttribute("tabindex","0"):e.removeAttribute("tabindex")});let s=[],a=e.closest("pre"),c=a.closest("[id]"),p=c?c.id:Za++;a.id=`__code_${p}`;let l=[],f=e.closest(".highlight");if(f instanceof HTMLElement){let d=Qn(f);if(typeof d!="undefined"&&(f.classList.contains("annotate")||V("content.code.annotate"))){let v=Tr(d,e,t);l.push(Le(f).pipe(W(i),m(({width:S,height:X})=>S&&X),Y(),b(S=>S?v:y)))}}let u=M(":scope > span[id]",e);if(u.length&&(e.classList.add("md-code__content"),e.closest(".select")||V("content.code.select")&&!e.closest(".no-select"))){let d=+u[0].id.split("-").pop(),v=$n();s.push(v),V("content.tooltips")&&l.push(Xe(v,{viewport$}));let S=h(v,"click").pipe(Ut(R=>!R,!1),O(()=>v.blur()),le());S.subscribe(R=>{v.classList.toggle("md-code__button--active",R)});let X=fe(u).pipe(J(R=>it(R).pipe(m(se=>[R,se]))));S.pipe(b(R=>R?X:y)).subscribe(([R,se])=>{let ce=ue(".hll.select",R);if(ce&&!se)ce.replaceWith(...Array.from(ce.childNodes));else if(!ce&&se){let he=document.createElement("span");he.className="hll select",he.append(...Array.from(R.childNodes).slice(1)),R.append(he)}});let re=fe(u).pipe(J(R=>h(R,"mousedown").pipe(O(se=>se.preventDefault()),m(()=>R)))),ee=S.pipe(b(R=>R?re:y),te(qn),m(([R,se])=>{var he;let ce=u.indexOf(R)+d;if(se===!1)return[ce,ce];{let Se=M(".hll",e).map(Ue=>u.indexOf(Ue.parentElement)+d);return(he=window.getSelection())==null||he.removeAllRanges(),[Math.min(ce,...Se),Math.max(ce,...Se)]}})),k=Zr(y).pipe(g(R=>R.startsWith(`__codelineno-${p}-`)));k.subscribe(R=>{let[,,se]=R.split("-"),ce=se.split(":").map(Se=>+Se-d+1);ce.length===1&&ce.push(ce[0]);for(let Se of M(".hll:not(.select)",e))Se.replaceWith(...Array.from(Se.childNodes));let he=u.slice(ce[0]-1,ce[1]);for(let Se of he){let Ue=document.createElement("span");Ue.className="hll",Ue.append(...Array.from(Se.childNodes).slice(1)),Se.append(Ue)}}),k.pipe(Ee(1),xe(pe)).subscribe(R=>{if(R.includes(":")){let se=document.getElementById(R.split(":")[0]);se&&setTimeout(()=>{let ce=se,he=-64;for(;ce!==document.body;)he+=ce.offsetTop,ce=ce.offsetParent;window.scrollTo({top:he})},1)}});let je=fe(M('a[href^="#__codelineno"]',f)).pipe(J(R=>h(R,"click").pipe(O(se=>se.preventDefault()),m(()=>R)))).pipe(W(i),te(qn),m(([R,se])=>{let he=+j(`[id="${R.hash.slice(1)}"]`).parentElement.id.split("-").pop();if(se===!1)return[he,he];{let Se=M(".hll",e).map(Ue=>+Ue.parentElement.id.split("-").pop());return[Math.min(he,...Se),Math.max(he,...Se)]}}));L(ee,je).subscribe(R=>{let se=`#__codelineno-${p}-`;R[0]===R[1]?se+=R[0]:se+=`${R[0]}:${R[1]}`,history.replaceState({},"",se),window.dispatchEvent(new HashChangeEvent("hashchange",{newURL:window.location.origin+window.location.pathname+se,oldURL:window.location.href}))})}if(Kn.default.isSupported()&&(e.closest(".copy")||V("content.code.copy")&&!e.closest(".no-copy"))){let d=Hn(a.id);s.push(d),V("content.tooltips")&&l.push(Xe(d,{viewport$}))}if(s.length){let d=Pn();d.append(...s),a.insertBefore(d,e)}return es(e).pipe(O(d=>n.next(d)),A(()=>n.complete()),m(d=>P({ref:e},d)),Ve(L(...l).pipe(W(i))))});return V("content.lazy")?mt(e).pipe(g(n=>n),Ee(1),b(()=>o)):o}function ts(e,{target$:t,print$:r}){let o=!0;return L(t.pipe(m(n=>n.closest("details:not([open])")),g(n=>e===n),m(()=>({action:"open",reveal:!0}))),r.pipe(g(n=>n||!o),O(()=>o=e.open),m(n=>({action:n?"open":"close"}))))}function Bn(e,t){return H(()=>{let r=new T;return r.subscribe(({action:o,reveal:n})=>{e.toggleAttribute("open",o==="open"),n&&e.scrollIntoView()}),ts(e,t).pipe(O(o=>r.next(o)),A(()=>r.complete()),m(o=>P({ref:e},o)))})}var Gn=0;function rs(e){let t=document.createElement("h3");t.innerHTML=e.innerHTML;let r=[t],o=e.nextElementSibling;for(;o&&!(o instanceof HTMLHeadingElement);)r.push(o),o=o.nextElementSibling;return r}function os(e,t){for(let r of M("[href], [src]",e))for(let o of["href","src"]){let n=r.getAttribute(o);if(n&&!/^(?:[a-z]+:)?\/\//i.test(n)){r[o]=new URL(r.getAttribute(o),t).toString();break}}for(let r of M("[name^=__], [for]",e))for(let o of["id","for","name"]){let n=r.getAttribute(o);n&&r.setAttribute(o,`${n}$preview_${Gn}`)}return Gn++,$(e)}function Jn(e,t){let{sitemap$:r}=t;if(!(e instanceof HTMLAnchorElement))return y;if(!(V("navigation.instant.preview")||e.hasAttribute("data-preview")))return y;e.removeAttribute("title");let o=z([Ye(e),it(e)]).pipe(m(([i,s])=>i||s),Y(),g(i=>i));return rt([r,o]).pipe(b(([i])=>{let s=new URL(e.href);return s.search=s.hash="",i.has(`${s}`)?$(s):y}),b(i=>xr(i).pipe(b(s=>os(s,i)))),b(i=>{let s=e.hash?`article [id="${e.hash.slice(1)}"]`:"article h1",a=ue(s,i);return typeof a=="undefined"?y:$(rs(a))})).pipe(b(i=>{let s=new F(a=>{let c=wr(...i);return a.next(c),document.body.append(c),()=>c.remove()});return Vt(e,P({content$:s},t))}))}var Xn=".node circle,.node ellipse,.node path,.node polygon,.node rect{fill:var(--md-mermaid-node-bg-color);stroke:var(--md-mermaid-node-fg-color)}marker{fill:var(--md-mermaid-edge-color)!important}.edgeLabel .label rect{fill:#0000}.flowchartTitleText{fill:var(--md-mermaid-label-fg-color)}.label{color:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}.label foreignObject{line-height:normal;overflow:visible}.label div .edgeLabel{color:var(--md-mermaid-label-fg-color)}.edgeLabel,.edgeLabel p,.label div .edgeLabel{background-color:var(--md-mermaid-label-bg-color)}.edgeLabel,.edgeLabel p{fill:var(--md-mermaid-label-bg-color);color:var(--md-mermaid-edge-color)}.edgePath .path,.flowchart-link{stroke:var(--md-mermaid-edge-color)}.edgePath .arrowheadPath{fill:var(--md-mermaid-edge-color);stroke:none}.cluster rect{fill:var(--md-default-fg-color--lightest);stroke:var(--md-default-fg-color--lighter)}.cluster span{color:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}g #flowchart-circleEnd,g #flowchart-circleStart,g #flowchart-crossEnd,g #flowchart-crossStart,g #flowchart-pointEnd,g #flowchart-pointStart{stroke:none}.classDiagramTitleText{fill:var(--md-mermaid-label-fg-color)}g.classGroup line,g.classGroup rect{fill:var(--md-mermaid-node-bg-color);stroke:var(--md-mermaid-node-fg-color)}g.classGroup text{fill:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}.classLabel .box{fill:var(--md-mermaid-label-bg-color);background-color:var(--md-mermaid-label-bg-color);opacity:1}.classLabel .label{fill:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}.node .divider{stroke:var(--md-mermaid-node-fg-color)}.relation{stroke:var(--md-mermaid-edge-color)}.cardinality{fill:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}.cardinality text{fill:inherit!important}defs marker.marker.composition.class path,defs marker.marker.dependency.class path,defs marker.marker.extension.class path{fill:var(--md-mermaid-edge-color)!important;stroke:var(--md-mermaid-edge-color)!important}defs marker.marker.aggregation.class path{fill:var(--md-mermaid-label-bg-color)!important;stroke:var(--md-mermaid-edge-color)!important}.statediagramTitleText{fill:var(--md-mermaid-label-fg-color)}g.stateGroup rect{fill:var(--md-mermaid-node-bg-color);stroke:var(--md-mermaid-node-fg-color)}g.stateGroup .state-title{fill:var(--md-mermaid-label-fg-color)!important;font-family:var(--md-mermaid-font-family)}g.stateGroup .composit{fill:var(--md-mermaid-label-bg-color)}.nodeLabel,.nodeLabel p{color:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}a .nodeLabel{text-decoration:underline}.node circle.state-end,.node circle.state-start,.start-state{fill:var(--md-mermaid-edge-color);stroke:none}.end-state-inner,.end-state-outer{fill:var(--md-mermaid-edge-color)}.end-state-inner,.node circle.state-end{stroke:var(--md-mermaid-label-bg-color)}.transition{stroke:var(--md-mermaid-edge-color)}[id^=state-fork] rect,[id^=state-join] rect{fill:var(--md-mermaid-edge-color)!important;stroke:none!important}.statediagram-cluster.statediagram-cluster .inner{fill:var(--md-default-bg-color)}.statediagram-cluster rect{fill:var(--md-mermaid-node-bg-color);stroke:var(--md-mermaid-node-fg-color)}.statediagram-state rect.divider{fill:var(--md-default-fg-color--lightest);stroke:var(--md-default-fg-color--lighter)}defs #statediagram-barbEnd{stroke:var(--md-mermaid-edge-color)}[id^=entity] path,[id^=entity] rect{fill:var(--md-default-bg-color)}.relationshipLine{stroke:var(--md-mermaid-edge-color)}defs .marker.oneOrMore.er *,defs .marker.onlyOne.er *,defs .marker.zeroOrMore.er *,defs .marker.zeroOrOne.er *{stroke:var(--md-mermaid-edge-color)!important}text:not([class]):last-child{fill:var(--md-mermaid-label-fg-color)}.actor{fill:var(--md-mermaid-sequence-actor-bg-color);stroke:var(--md-mermaid-sequence-actor-border-color)}text.actor>tspan{fill:var(--md-mermaid-sequence-actor-fg-color);font-family:var(--md-mermaid-font-family)}line{stroke:var(--md-mermaid-sequence-actor-line-color)}.actor-man circle,.actor-man line{fill:var(--md-mermaid-sequence-actorman-bg-color);stroke:var(--md-mermaid-sequence-actorman-line-color)}.messageLine0,.messageLine1{stroke:var(--md-mermaid-sequence-message-line-color)}.note{fill:var(--md-mermaid-sequence-note-bg-color);stroke:var(--md-mermaid-sequence-note-border-color)}.loopText,.loopText>tspan,.messageText,.noteText>tspan{stroke:none;font-family:var(--md-mermaid-font-family)!important}.messageText{fill:var(--md-mermaid-sequence-message-fg-color)}.loopText,.loopText>tspan{fill:var(--md-mermaid-sequence-loop-fg-color)}.noteText>tspan{fill:var(--md-mermaid-sequence-note-fg-color)}#arrowhead path{fill:var(--md-mermaid-sequence-message-line-color);stroke:none}.loopLine{fill:var(--md-mermaid-sequence-loop-bg-color);stroke:var(--md-mermaid-sequence-loop-border-color)}.labelBox{fill:var(--md-mermaid-sequence-label-bg-color);stroke:none}.labelText,.labelText>span{fill:var(--md-mermaid-sequence-label-fg-color);font-family:var(--md-mermaid-font-family)}.sequenceNumber{fill:var(--md-mermaid-sequence-number-fg-color)}rect.rect{fill:var(--md-mermaid-sequence-box-bg-color);stroke:none}rect.rect+text.text{fill:var(--md-mermaid-sequence-box-fg-color)}defs #sequencenumber{fill:var(--md-mermaid-sequence-number-bg-color)!important}";var so,is=0;function as(){return typeof mermaid=="undefined"||mermaid instanceof Element?_t("https://unpkg.com/mermaid@11/dist/mermaid.min.js"):$(void 0)}function Zn(e){return e.classList.remove("mermaid"),so||(so=as().pipe(O(()=>mermaid.initialize({startOnLoad:!1,themeCSS:Xn,sequence:{actorFontSize:"16px",messageFontSize:"16px",noteFontSize:"16px"}})),m(()=>{}),Z(1))),so.subscribe(()=>go(null,null,function*(){e.classList.add("mermaid");let t=`__mermaid_${is++}`,r=x("div",{class:"mermaid"}),o=e.textContent,{svg:n,fn:i}=yield mermaid.render(t,o),s=r.attachShadow({mode:"closed"});s.innerHTML=n,e.replaceWith(r),i==null||i(s)})),so.pipe(m(()=>({ref:e})))}var ei=x("table");function ti(e){return e.replaceWith(ei),ei.replaceWith(Un(e)),$({ref:e})}function ss(e){let t=e.find(r=>r.checked)||e[0];return L(...e.map(r=>h(r,"change").pipe(m(()=>j(`label[for="${r.id}"]`))))).pipe(Q(j(`label[for="${t.id}"]`)),m(r=>({active:r})))}function ri(e,{viewport$:t,target$:r}){let o=j(".tabbed-labels",e),n=M(":scope > input",e),i=no("prev");e.append(i);let s=no("next");return e.append(s),H(()=>{let a=new T,c=a.pipe(oe(),ae(!0));z([a,Le(e),mt(e)]).pipe(W(c),$e(1,ye)).subscribe({next([{active:p},l]){let f=Be(p),{width:u}=de(p);e.style.setProperty("--md-indicator-x",`${f.x}px`),e.style.setProperty("--md-indicator-width",`${u}px`);let d=gr(o);(f.xd.x+l.width)&&o.scrollTo({left:Math.max(0,f.x-16),behavior:"smooth"})},complete(){e.style.removeProperty("--md-indicator-x"),e.style.removeProperty("--md-indicator-width")}}),z([Ge(o),Le(o)]).pipe(W(c)).subscribe(([p,l])=>{let f=At(o);i.hidden=p.x<16,s.hidden=p.x>f.width-l.width-16}),L(h(i,"click").pipe(m(()=>-1)),h(s,"click").pipe(m(()=>1))).pipe(W(c)).subscribe(p=>{let{width:l}=de(o);o.scrollBy({left:l*p,behavior:"smooth"})}),r.pipe(W(c),g(p=>n.includes(p))).subscribe(p=>p.click()),o.classList.add("tabbed-labels--linked");for(let p of n){let l=j(`label[for="${p.id}"]`);l.replaceChildren(x("a",{href:`#${l.htmlFor}`,tabIndex:-1},...Array.from(l.childNodes))),h(l.firstElementChild,"click").pipe(W(c),g(f=>!(f.metaKey||f.ctrlKey)),O(f=>{f.preventDefault(),f.stopPropagation()})).subscribe(()=>{history.replaceState({},"",`#${l.htmlFor}`),l.click()})}return V("content.tabs.link")&&a.pipe(Ie(1),te(t)).subscribe(([{active:p},{offset:l}])=>{let f=p.innerText.trim();if(p.hasAttribute("data-md-switching"))p.removeAttribute("data-md-switching");else{let u=e.offsetTop-l.y;for(let v of M("[data-tabs]"))for(let S of M(":scope > input",v)){let X=j(`label[for="${S.id}"]`);if(X!==p&&X.innerText.trim()===f){X.setAttribute("data-md-switching",""),S.click();break}}window.scrollTo({top:e.offsetTop-u});let d=__md_get("__tabs")||[];__md_set("__tabs",[...new Set([f,...d])])}}),a.pipe(W(c)).subscribe(()=>{for(let p of M("audio, video",e))p.offsetWidth&&p.autoplay?p.play().catch(()=>{}):p.pause()}),ss(n).pipe(O(p=>a.next(p)),A(()=>a.complete()),m(p=>P({ref:e},p)))}).pipe(et(pe))}function oi(e,t){let{viewport$:r,target$:o,print$:n}=t;return L(...M(".annotate:not(.highlight)",e).map(i=>zn(i,{target$:o,print$:n})),...M("pre:not(.mermaid) > code",e).map(i=>Yn(i,{target$:o,print$:n})),...M("a",e).map(i=>Jn(i,t)),...M("pre.mermaid",e).map(i=>Zn(i)),...M("table:not([class])",e).map(i=>ti(i)),...M("details",e).map(i=>Bn(i,{target$:o,print$:n})),...M("[data-tabs]",e).map(i=>ri(i,{viewport$:r,target$:o})),...M("[title]:not([data-preview])",e).filter(()=>V("content.tooltips")).map(i=>Xe(i,{viewport$:r})),...M(".footnote-ref",e).filter(()=>V("content.footnote.tooltips")).map(i=>Vt(i,{content$:new F(s=>{let a=new URL(i.href).hash.slice(1),c=Array.from(document.getElementById(a).cloneNode(!0).children),p=wr(...c);return s.next(p),document.body.append(p),()=>p.remove()}),viewport$:r})))}function cs(e,{alert$:t}){return t.pipe(b(r=>L($(!0),$(!1).pipe(nt(2e3))).pipe(m(o=>({message:r,active:o})))))}function ni(e,t){let r=j(".md-typeset",e);return H(()=>{let o=new T;return o.subscribe(({message:n,active:i})=>{e.classList.toggle("md-dialog--active",i),r.textContent=n}),cs(e,t).pipe(O(n=>o.next(n)),A(()=>o.complete()),m(n=>P({ref:e},n)))})}var ps=0;function ls(e,t){document.body.append(e);let{width:r}=de(e);e.style.setProperty("--md-tooltip-width",`${r}px`),e.remove();let o=vr(t),n=typeof o!="undefined"?Ge(o):$({x:0,y:0}),i=L(Ye(t),it(t)).pipe(Y());return z([i,n]).pipe(m(([s,a])=>{let{x:c,y:p}=Be(t),l=de(t),f=t.closest("table");return f&&t.parentElement&&(c+=f.offsetLeft+t.parentElement.offsetLeft,p+=f.offsetTop+t.parentElement.offsetTop),{active:s,offset:{x:c-a.x+l.width/2-r/2,y:p-a.y+l.height+8}}}))}function ii(e){let t=e.title;if(!t.length)return y;let r=`__tooltip_${ps++}`,o=Dt(r,"inline"),n=j(".md-typeset",o);return n.innerHTML=t,H(()=>{let i=new T;return i.subscribe({next({offset:s}){o.style.setProperty("--md-tooltip-x",`${s.x}px`),o.style.setProperty("--md-tooltip-y",`${s.y}px`)},complete(){o.style.removeProperty("--md-tooltip-x"),o.style.removeProperty("--md-tooltip-y")}}),L(i.pipe(g(({active:s})=>s)),i.pipe(Ae(250),g(({active:s})=>!s))).subscribe({next({active:s}){s?(e.insertAdjacentElement("afterend",o),e.setAttribute("aria-describedby",r),e.removeAttribute("title")):(o.remove(),e.removeAttribute("aria-describedby"),e.setAttribute("title",t))},complete(){o.remove(),e.removeAttribute("aria-describedby"),e.setAttribute("title",t)}}),i.pipe($e(16,ye)).subscribe(({active:s})=>{o.classList.toggle("md-tooltip--active",s)}),i.pipe(gt(125,ye),g(()=>!!e.offsetParent),m(()=>e.offsetParent.getBoundingClientRect()),m(({x:s})=>s)).subscribe({next(s){s?o.style.setProperty("--md-tooltip-0",`${-s}px`):o.style.removeProperty("--md-tooltip-0")},complete(){o.style.removeProperty("--md-tooltip-0")}}),ls(o,e).pipe(O(s=>i.next(s)),A(()=>i.complete()),m(s=>P({ref:e},s)))}).pipe(et(pe))}function ms({viewport$:e}){if(!V("header.autohide"))return $(!1);let t=e.pipe(m(({offset:{y:n}})=>n),ot(2,1),m(([n,i])=>[nMath.abs(i-n.y)>100),m(([,[n]])=>n),Y()),o=Je("search");return z([e,o]).pipe(m(([{offset:n},i])=>n.y>400&&!i),Y(),b(n=>n?r:$(!1)),Q(!1))}function ai(e,t){return H(()=>z([Le(e),ms(t)])).pipe(m(([{height:r},o])=>({height:r,hidden:o})),Y((r,o)=>r.height===o.height&&r.hidden===o.hidden),Z(1))}function si(e,{header$:t,main$:r}){return H(()=>{let o=new T,n=o.pipe(oe(),ae(!0));o.pipe(ne("active"),Pe(t)).subscribe(([{active:s},{hidden:a}])=>{e.classList.toggle("md-header--shadow",s&&!a),e.hidden=a});let i=fe(M("[title]",e)).pipe(g(()=>V("content.tooltips")),J(s=>ii(s)));return r.subscribe(o),t.pipe(W(n),m(s=>P({ref:e},s)),Ve(i.pipe(W(n))))})}function fs(e,{viewport$:t,header$:r}){return Er(e,{viewport$:t,header$:r}).pipe(m(({offset:{y:o}})=>{let{height:n}=de(e);return{active:n>0&&o>=n}}),ne("active"))}function ci(e,t){return H(()=>{let r=new T;r.subscribe({next({active:n}){e.classList.toggle("md-header__title--active",n)},complete(){e.classList.remove("md-header__title--active")}});let o=ue(".md-content h1");return typeof o=="undefined"?y:fs(o,t).pipe(O(n=>r.next(n)),A(()=>r.complete()),m(n=>P({ref:e},n)))})}function pi(e,{viewport$:t,header$:r}){let o=r.pipe(m(({height:i})=>i),Y()),n=o.pipe(b(()=>Le(e).pipe(m(({height:i})=>({top:e.offsetTop,bottom:e.offsetTop+i})),ne("bottom"))));return z([o,n,t]).pipe(m(([i,{top:s,bottom:a},{offset:{y:c},size:{height:p}}])=>(p=Math.max(0,p-Math.max(0,s-c,i)-Math.max(0,p+c-a)),{offset:s-i,height:p,active:s-i<=c})),Y((i,s)=>i.offset===s.offset&&i.height===s.height&&i.active===s.active))}function us(e){let t=__md_get("__palette")||{index:e.findIndex(o=>matchMedia(o.getAttribute("data-md-color-media")).matches)},r=Math.max(0,Math.min(t.index,e.length-1));return $(...e).pipe(J(o=>h(o,"change").pipe(m(()=>o))),Q(e[r]),m(o=>({index:e.indexOf(o),color:{media:o.getAttribute("data-md-color-media"),scheme:o.getAttribute("data-md-color-scheme"),primary:o.getAttribute("data-md-color-primary"),accent:o.getAttribute("data-md-color-accent")}})),Z(1))}function li(e){let t=M("input",e),r=x("meta",{name:"theme-color"});document.head.appendChild(r);let o=x("meta",{name:"color-scheme"});document.head.appendChild(o);let n=Wt("(prefers-color-scheme: light)");return H(()=>{let i=new T;return i.subscribe(s=>{if(document.body.setAttribute("data-md-color-switching",""),s.color.media==="(prefers-color-scheme)"){let a=matchMedia("(prefers-color-scheme: light)"),c=document.querySelector(a.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");s.color.scheme=c.getAttribute("data-md-color-scheme"),s.color.primary=c.getAttribute("data-md-color-primary"),s.color.accent=c.getAttribute("data-md-color-accent")}for(let[a,c]of Object.entries(s.color))document.body.setAttribute(`data-md-color-${a}`,c);for(let a=0;as.key==="Enter"),te(i,(s,a)=>a)).subscribe(({index:s})=>{s=(s+1)%t.length,t[s].click(),t[s].focus()}),i.pipe(m(()=>{let s=Ce("header"),a=window.getComputedStyle(s);return o.content=a.colorScheme,a.backgroundColor.match(/\d+/g).map(c=>(+c).toString(16).padStart(2,"0")).join("")})).subscribe(s=>r.content=`#${s}`),i.pipe(xe(pe)).subscribe(()=>{document.body.removeAttribute("data-md-color-switching")}),us(t).pipe(W(n.pipe(Ie(1))),vt(),O(s=>i.next(s)),A(()=>i.complete()),m(s=>P({ref:e},s)))})}function mi(e,{progress$:t}){return H(()=>{let r=new T;return r.subscribe(({value:o})=>{e.style.setProperty("--md-progress-value",`${o}`)}),t.pipe(O(o=>r.next({value:o})),A(()=>r.complete()),m(o=>({ref:e,value:o})))})}function fi(e,t){return e.protocol=t.protocol,e.hostname=t.hostname,e}function ds(e,t){let r=new Map;for(let o of M("url",e)){let n=j("loc",o),i=[fi(new URL(n.textContent),t)];r.set(`${i[0]}`,i);for(let s of M("[rel=alternate]",o)){let a=s.getAttribute("href");a!=null&&i.push(fi(new URL(a),t))}}return r}function kt(e){return En(new URL("sitemap.xml",e)).pipe(m(t=>ds(t,new URL(e))),ve(()=>$(new Map)),le())}function ui({document$:e}){let t=new Map;e.pipe(b(()=>M("link[rel=alternate]")),m(r=>new URL(r.href)),g(r=>!t.has(r.toString())),J(r=>kt(r).pipe(m(o=>[r,o]),ve(()=>y)))).subscribe(([r,o])=>{t.set(r.toString().replace(/\/$/,""),o)}),h(document.body,"click").pipe(g(r=>!r.metaKey&&!r.ctrlKey),b(r=>{if(r.target instanceof Element){let o=r.target.closest("a");if(o&&!o.target){let n=[...t].find(([f])=>o.href.startsWith(`${f}/`));if(typeof n=="undefined")return y;let[i,s]=n,a=we();if(a.href.startsWith(i))return y;let c=Te(),p=a.href.replace(c.base,"");p=`${i}/${p}`;let l=s.has(p.split("#")[0])?new URL(p,c.base):new URL(i);return r.preventDefault(),$(l)}}return y})).subscribe(r=>st(r,!0))}var co=$t(ao());function hs(e){e.setAttribute("data-md-copying","");let t=e.closest("[data-copy]"),r=t?t.getAttribute("data-copy"):e.innerText;return e.removeAttribute("data-md-copying"),r.trimEnd()}function di({alert$:e}){co.default.isSupported()&&new F(t=>{new co.default("[data-clipboard-target], [data-clipboard-text]",{text:r=>r.getAttribute("data-clipboard-text")||hs(j(r.getAttribute("data-clipboard-target")))}).on("success",r=>t.next(r))}).pipe(O(t=>{t.trigger.focus()}),m(()=>Me("clipboard.copied"))).subscribe(e)}function hi(e,t){if(!(e.target instanceof Element))return y;let r=e.target.closest("a");if(r===null)return y;if(r.target||e.metaKey||e.ctrlKey)return y;let o=new URL(r.href);return o.search=o.hash="",t.has(`${o}`)?(e.preventDefault(),$(r)):y}function bi(e){let t=new Map;for(let r of M(":scope > *",e.head))t.set(r.outerHTML,r);return t}function vi(e){for(let t of M("[href], [src]",e))for(let r of["href","src"]){let o=t.getAttribute(r);if(o&&!/^(?:[a-z]+:)?\/\//i.test(o)){t[r]=t[r];break}}return $(e)}function bs(e){for(let o of["[data-md-component=announce]","[data-md-component=container]","[data-md-component=header-topic]","[data-md-component=outdated]","[data-md-component=logo]","[data-md-component=skip]",...V("navigation.tabs.sticky")?["[data-md-component=tabs]"]:[]]){let n=ue(o),i=ue(o,e);typeof n!="undefined"&&typeof i!="undefined"&&n.replaceWith(i)}let t=bi(document);for(let[o,n]of bi(e))t.has(o)?t.delete(o):document.head.appendChild(n);for(let o of t.values()){let n=o.getAttribute("name");n!=="theme-color"&&n!=="color-scheme"&&o.remove()}let r=Ce("container");return Ke(M("script",r)).pipe(b(o=>{let n=e.createElement("script");if(o.src){for(let i of o.getAttributeNames())n.setAttribute(i,o.getAttribute(i));return o.replaceWith(n),new F(i=>{n.onload=()=>i.complete()})}else return n.textContent=o.textContent,o.replaceWith(n),y}),oe(),ae(document))}function gi({sitemap$:e,location$:t,viewport$:r,progress$:o}){if(location.protocol==="file:")return y;$(document).subscribe(vi);let n=h(document.body,"click").pipe(Pe(e),b(([a,c])=>hi(a,c)),m(({href:a})=>new URL(a)),le()),i=h(window,"popstate").pipe(m(we),le());n.pipe(te(r)).subscribe(([a,{offset:c}])=>{history.replaceState(c,""),history.pushState(null,"",a)}),L(n,i).subscribe(t);let s=t.pipe(ne("pathname"),b(a=>xr(a,{progress$:o}).pipe(ve(()=>(st(a,!0),y)))),b(vi),b(bs),le());return L(s.pipe(te(t,(a,c)=>c)),s.pipe(b(()=>t),ne("hash")),t.pipe(Y((a,c)=>a.pathname===c.pathname&&a.hash===c.hash),b(()=>n),O(()=>history.back()))).subscribe(a=>{var c,p;history.state!==null||!a.hash?window.scrollTo(0,(p=(c=history.state)==null?void 0:c.y)!=null?p:0):(history.scrollRestoration="auto",gn(a.hash),history.scrollRestoration="manual")}),t.subscribe(()=>{history.scrollRestoration="manual"}),h(window,"beforeunload").subscribe(()=>{history.scrollRestoration="auto"}),r.pipe(ne("offset"),Ae(100)).subscribe(({offset:a})=>{history.replaceState(a,"")}),V("navigation.instant.prefetch")&&L(h(document.body,"mousemove"),h(document.body,"focusin")).pipe(Pe(e),b(([a,c])=>hi(a,c)),Ae(25),Qr(({href:a})=>a),hr(a=>{let c=document.createElement("link");return c.rel="prefetch",c.href=a.toString(),document.head.appendChild(c),h(c,"load").pipe(m(()=>c),Ee(1))})).subscribe(a=>a.remove()),s}var yi=$t(ro());function xi(e){let t=e.separator.split("|").map(n=>n.replace(/(\(\?[!=<][^)]+\))/g,"").length===0?"\uFFFD":n).join("|"),r=new RegExp(t,"img"),o=(n,i,s)=>`${i}${s}`;return n=>{n=n.replace(/[\s*+\-:~^]+/g," ").replace(/&/g,"&").trim();let i=new RegExp(`(^|${e.separator}|)(${n.replace(/[|\\{}()[\]^$+*?.-]/g,"\\$&").replace(r,"|")})`,"img");return s=>(0,yi.default)(s).replace(i,o).replace(/<\/mark>(\s+)]*>/img,"$1")}}function zt(e){return e.type===1}function Sr(e){return e.type===3}function Ei(e,t){let r=Mn(e);return L($(location.protocol!=="file:"),Je("search")).pipe(Re(o=>o),b(()=>t)).subscribe(({config:o,docs:n})=>r.next({type:0,data:{config:o,docs:n,options:{suggest:V("search.suggest")}}})),r}function wi(e){var l;let{selectedVersionSitemap:t,selectedVersionBaseURL:r,currentLocation:o,currentBaseURL:n}=e,i=(l=po(n))==null?void 0:l.pathname;if(i===void 0)return;let s=ys(o.pathname,i);if(s===void 0)return;let a=Es(t.keys());if(!t.has(a))return;let c=po(s,a);if(!c||!t.has(c.href))return;let p=po(s,r);if(p)return p.hash=o.hash,p.search=o.search,p}function po(e,t){try{return new URL(e,t)}catch(r){return}}function ys(e,t){if(e.startsWith(t))return e.slice(t.length)}function xs(e,t){let r=Math.min(e.length,t.length),o;for(o=0;oy)),o=r.pipe(m(n=>{let[,i]=t.base.match(/([^/]+)\/?$/);return n.find(({version:s,aliases:a})=>s===i||a.includes(i))||n[0]}));r.pipe(m(n=>new Map(n.map(i=>[`${new URL(`../${i.version}/`,t.base)}`,i]))),b(n=>h(document.body,"click").pipe(g(i=>!i.metaKey&&!i.ctrlKey),te(o),b(([i,s])=>{if(i.target instanceof Element){let a=i.target.closest("a");if(a&&!a.target&&n.has(a.href)){let c=a.href;return!i.target.closest(".md-version")&&n.get(c)===s?y:(i.preventDefault(),$(new URL(c)))}}return y}),b(i=>kt(i).pipe(m(s=>{var a;return(a=wi({selectedVersionSitemap:s,selectedVersionBaseURL:i,currentLocation:we(),currentBaseURL:t.base}))!=null?a:i})))))).subscribe(n=>st(n,!0)),z([r,o]).subscribe(([n,i])=>{j(".md-header__topic").appendChild(Wn(n,i))}),e.pipe(b(()=>o)).subscribe(n=>{var a;let i=new URL(t.base),s=__md_get("__outdated",sessionStorage,i);if(s===null){s=!0;let c=((a=t.version)==null?void 0:a.default)||"latest";Array.isArray(c)||(c=[c]);e:for(let p of c)for(let l of n.aliases.concat(n.version))if(new RegExp(p,"i").test(l)){s=!1;break e}__md_set("__outdated",s,sessionStorage,i)}if(s)for(let c of me("outdated"))c.hidden=!1})}function ws(e,{worker$:t}){let{searchParams:r}=we();r.has("q")&&(at("search",!0),e.value=r.get("q"),e.focus(),Je("search").pipe(Re(i=>!i)).subscribe(()=>{let i=we();i.searchParams.delete("q"),history.replaceState({},"",`${i}`)}));let o=Ye(e),n=L(t.pipe(Re(zt)),h(e,"keyup"),o).pipe(m(()=>e.value),Y());return z([n,o]).pipe(m(([i,s])=>({value:i,focus:s})),Z(1))}function Si(e,{worker$:t}){let r=new T,o=r.pipe(oe(),ae(!0));z([t.pipe(Re(zt)),r],(i,s)=>s).pipe(ne("value")).subscribe(({value:i})=>t.next({type:2,data:i})),r.pipe(ne("focus")).subscribe(({focus:i})=>{i&&at("search",i)}),h(e.form,"reset").pipe(W(o)).subscribe(()=>e.focus());let n=j("header [for=__search]");return h(n,"click").subscribe(()=>e.focus()),ws(e,{worker$:t}).pipe(O(i=>r.next(i)),A(()=>r.complete()),m(i=>P({ref:e},i)),Z(1))}function Oi(e,{worker$:t,query$:r}){let o=new T,n=un(e.parentElement).pipe(g(Boolean)),i=e.parentElement,s=j(":scope > :first-child",e),a=j(":scope > :last-child",e);Je("search").subscribe(l=>{a.setAttribute("role",l?"list":"presentation"),a.hidden=!l}),o.pipe(te(r),Gr(t.pipe(Re(zt)))).subscribe(([{items:l},{value:f}])=>{switch(l.length){case 0:s.textContent=f.length?Me("search.result.none"):Me("search.result.placeholder");break;case 1:s.textContent=Me("search.result.one");break;default:let u=br(l.length);s.textContent=Me("search.result.other",u)}});let c=o.pipe(O(()=>a.innerHTML=""),b(({items:l})=>L($(...l.slice(0,10)),$(...l.slice(10)).pipe(ot(4),Xr(n),b(([f])=>f)))),m(Fn),le());return c.subscribe(l=>a.appendChild(l)),c.pipe(J(l=>{let f=ue("details",l);return typeof f=="undefined"?y:h(f,"toggle").pipe(W(o),m(()=>f))})).subscribe(l=>{l.open===!1&&l.offsetTop<=i.scrollTop&&i.scrollTo({top:l.offsetTop})}),t.pipe(g(Sr),m(({data:l})=>l)).pipe(O(l=>o.next(l)),A(()=>o.complete()),m(l=>P({ref:e},l)))}function Ts(e,{query$:t}){return t.pipe(m(({value:r})=>{let o=we();return o.hash="",r=r.replace(/\s+/g,"+").replace(/&/g,"%26").replace(/=/g,"%3D"),o.search=`q=${r}`,{url:o}}))}function Li(e,t){let r=new T,o=r.pipe(oe(),ae(!0));return r.subscribe(({url:n})=>{e.setAttribute("data-clipboard-text",e.href),e.href=`${n}`}),h(e,"click").pipe(W(o)).subscribe(n=>n.preventDefault()),Ts(e,t).pipe(O(n=>r.next(n)),A(()=>r.complete()),m(n=>P({ref:e},n)))}function Mi(e,{worker$:t,keyboard$:r}){let o=new T,n=Ce("search-query"),i=L(h(n,"keydown"),h(n,"focus")).pipe(xe(pe),m(()=>n.value),Y());return o.pipe(Pe(i),m(([{suggest:a},c])=>{let p=c.split(/([\s-]+)/);if(a!=null&&a.length&&p[p.length-1]){let l=a[a.length-1];l.startsWith(p[p.length-1])&&(p[p.length-1]=l)}else p.length=0;return p})).subscribe(a=>e.innerHTML=a.join("").replace(/\s/g," ")),r.pipe(g(({mode:a})=>a==="search")).subscribe(a=>{a.type==="ArrowRight"&&e.innerText.length&&n.selectionStart===n.value.length&&(n.value=e.innerText)}),t.pipe(g(Sr),m(({data:a})=>a)).pipe(O(a=>o.next(a)),A(()=>o.complete()),m(()=>({ref:e})))}function _i(e,{index$:t,keyboard$:r}){let o=Te();try{let n=Ei(o.search,t),i=Ce("search-query",e),s=Ce("search-result",e);h(e,"click").pipe(g(({target:c})=>c instanceof Element&&!!c.closest("a"))).subscribe(()=>at("search",!1)),r.pipe(g(({mode:c})=>c==="search")).subscribe(c=>{let p=Ne();switch(c.type){case"Enter":if(p===i){let l=new Map;for(let f of M(":first-child [href]",s)){let u=f.firstElementChild;l.set(f,parseFloat(u.getAttribute("data-md-score")))}if(l.size){let[[f]]=[...l].sort(([,u],[,d])=>d-u);f.click()}c.claim()}break;case"Escape":case"Tab":at("search",!1),i.blur();break;case"ArrowUp":case"ArrowDown":if(typeof p=="undefined")i.focus();else{let l=[i,...M(":not(details) > [href], summary, details[open] [href]",s)],f=Math.max(0,(Math.max(0,l.indexOf(p))+l.length+(c.type==="ArrowUp"?-1:1))%l.length);l[f].focus()}c.claim();break;default:i!==Ne()&&i.focus()}}),r.pipe(g(({mode:c})=>c==="global")).subscribe(c=>{switch(c.type){case"f":case"s":case"/":i.focus(),i.select(),c.claim();break}});let a=Si(i,{worker$:n});return L(a,Oi(s,{worker$:n,query$:a})).pipe(Ve(...me("search-share",e).map(c=>Li(c,{query$:a})),...me("search-suggest",e).map(c=>Mi(c,{worker$:n,keyboard$:r}))))}catch(n){return e.hidden=!0,tt}}function Ai(e,{index$:t,location$:r}){return z([t,r.pipe(Q(we()),g(o=>!!o.searchParams.get("h")))]).pipe(m(([o,n])=>xi(o.config)(n.searchParams.get("h"))),m(o=>{var s;let n=new Map,i=document.createNodeIterator(e,NodeFilter.SHOW_TEXT);for(let a=i.nextNode();a;a=i.nextNode())if((s=a.parentElement)!=null&&s.offsetHeight){let c=a.textContent,p=o(c);p.length>c.length&&n.set(a,p)}for(let[a,c]of n){let{childNodes:p}=x("span",null,c);a.replaceWith(...Array.from(p))}return{ref:e,nodes:n}}))}function Ss(e,{viewport$:t,main$:r}){let o=e.closest(".md-grid"),n=o.offsetTop-o.parentElement.offsetTop;return z([r,t]).pipe(m(([{offset:i,height:s},{offset:{y:a}}])=>(s=s+Math.min(n,Math.max(0,a-i))-n,{height:s,locked:a>=i+n})),Y((i,s)=>i.height===s.height&&i.locked===s.locked))}function lo(e,o){var n=o,{header$:t}=n,r=vo(n,["header$"]);let i=j(".md-sidebar__scrollwrap",e),{y:s}=Be(i);return H(()=>{let a=new T,c=a.pipe(oe(),ae(!0)),p=a.pipe($e(0,ye));return p.pipe(te(t)).subscribe({next([{height:l},{height:f}]){i.style.height=`${l-2*s}px`,e.style.top=`${f}px`},complete(){i.style.height="",e.style.top=""}}),p.pipe(Re()).subscribe(()=>{for(let l of M(".md-nav__link--active[href]",e)){if(!l.clientHeight)continue;let f=l.closest(".md-sidebar__scrollwrap");if(typeof f!="undefined"){let u=l.offsetTop-f.offsetTop,{height:d}=de(f);f.scrollTo({top:u-d/2})}}}),fe(M("label[tabindex]",e)).pipe(J(l=>h(l,"click").pipe(xe(pe),m(()=>l),W(c)))).subscribe(l=>{let f=j(`[id="${l.htmlFor}"]`);j(`[aria-labelledby="${l.id}"]`).setAttribute("aria-expanded",`${f.checked}`)}),V("content.tooltips")&&fe(M("abbr[title]",e)).pipe(J(l=>Xe(l,{viewport$})),W(c)).subscribe(),Ss(e,r).pipe(O(l=>a.next(l)),A(()=>a.complete()),m(l=>P({ref:e},l)))})}function Ci(e,t){if(typeof t!="undefined"){let r=`https://api.github.com/repos/${e}/${t}`;return rt(ze(`${r}/releases/latest`).pipe(ve(()=>y),m(o=>({version:o.tag_name})),Qe({})),ze(r).pipe(ve(()=>y),m(o=>({stars:o.stargazers_count,forks:o.forks_count})),Qe({}))).pipe(m(([o,n])=>P(P({},o),n)))}else{let r=`https://api.github.com/users/${e}`;return ze(r).pipe(m(o=>({repositories:o.public_repos})),Qe({}))}}function ki(e,t){let r=`https://${e}/api/v4/projects/${encodeURIComponent(t)}`;return rt(ze(`${r}/releases/permalink/latest`).pipe(ve(()=>y),m(({tag_name:o})=>({version:o})),Qe({})),ze(r).pipe(ve(()=>y),m(({star_count:o,forks_count:n})=>({stars:o,forks:n})),Qe({}))).pipe(m(([o,n])=>P(P({},o),n)))}function Hi(e){let t=e.match(/^.+github\.com\/([^/]+)\/?([^/]+)?/i);if(t){let[,r,o]=t;return Ci(r,o)}if(t=e.match(/^.+?([^/]*gitlab[^/]+)\/(.+?)\/?$/i),t){let[,r,o]=t;return ki(r,o)}return y}var Os;function Ls(e){return Os||(Os=H(()=>{let t=__md_get("__source",sessionStorage);if(t)return $(t);if(me("consent").length){let o=__md_get("__consent");if(!(o&&o.github))return y}return Hi(e.href).pipe(O(o=>__md_set("__source",o,sessionStorage)))}).pipe(ve(()=>y),g(t=>Object.keys(t).length>0),m(t=>({facts:t})),Z(1)))}function $i(e){let t=j(":scope > :last-child",e);return H(()=>{let r=new T;return r.subscribe(({facts:o})=>{t.appendChild(jn(o)),t.classList.add("md-source__repository--active")}),Ls(e).pipe(O(o=>r.next(o)),A(()=>r.complete()),m(o=>P({ref:e},o)))})}function Ms(e,{viewport$:t,header$:r}){return Le(document.body).pipe(b(()=>Er(e,{header$:r,viewport$:t})),m(({offset:{y:o}})=>({hidden:o>=10})),ne("hidden"))}function Pi(e,t){return H(()=>{let r=new T;return r.subscribe({next({hidden:o}){e.hidden=o},complete(){e.hidden=!1}}),(V("navigation.tabs.sticky")?$({hidden:!1}):Ms(e,t)).pipe(O(o=>r.next(o)),A(()=>r.complete()),m(o=>P({ref:e},o)))})}function _s(e,{viewport$:t,header$:r}){let o=new Map,n=M(".md-nav__link",e);for(let a of n){let c=decodeURIComponent(a.hash.substring(1)),p=ue(`[id="${c}"]`);typeof p!="undefined"&&o.set(a,p)}let i=r.pipe(ne("height"),m(({height:a})=>{let c=Ce("main"),p=j(":scope > :first-child",c);return a+.8*(p.offsetTop-c.offsetTop)}),le());return Le(document.body).pipe(ne("height"),b(a=>H(()=>{let c=[];return $([...o].reduce((p,[l,f])=>{for(;c.length&&o.get(c[c.length-1]).tagName>=f.tagName;)c.pop();let u=f.offsetTop;for(;!u&&f.parentElement;)f=f.parentElement,u=f.offsetTop;let d=f.offsetParent;for(;d;d=d.offsetParent)u+=d.offsetTop;return p.set([...c=[...c,l]].reverse(),u)},new Map))}).pipe(m(c=>new Map([...c].sort(([,p],[,l])=>p-l))),Pe(i),b(([c,p])=>t.pipe(Ut(([l,f],{offset:{y:u},size:d})=>{let v=u+d.height>=Math.floor(a.height);for(;f.length;){let[,S]=f[0];if(S-p=u&&!v)f=[l.pop(),...f];else break}return[l,f]},[[],[...c]]),Y((l,f)=>l[0]===f[0]&&l[1]===f[1])))))).pipe(m(([a,c])=>({prev:a.map(([p])=>p),next:c.map(([p])=>p)})),Q({prev:[],next:[]}),ot(2,1),m(([a,c])=>a.prev.length{let i=new T,s=i.pipe(oe(),ae(!0));if(i.subscribe(({prev:a,next:c})=>{for(let[p]of c)p.classList.remove("md-nav__link--passed"),p.classList.remove("md-nav__link--active");for(let[p,[l]]of a.entries())l.classList.add("md-nav__link--passed"),l.classList.toggle("md-nav__link--active",p===a.length-1)}),V("toc.follow")){let a=L(t.pipe(Ae(1),m(()=>{})),t.pipe(Ae(250),m(()=>"smooth")));i.pipe(g(({prev:c})=>c.length>0),Pe(o.pipe(xe(pe))),te(a)).subscribe(([[{prev:c}],p])=>{let[l]=c[c.length-1];if(l.offsetHeight){let f=vr(l);if(typeof f!="undefined"){let u=l.offsetTop-f.offsetTop,{height:d}=de(f);f.scrollTo({top:u-d/2,behavior:p})}}})}return V("navigation.tracking")&&t.pipe(W(s),ne("offset"),Ae(250),Ie(1),W(n.pipe(Ie(1))),vt({delay:250}),te(i)).subscribe(([,{prev:a}])=>{let c=we(),p=a[a.length-1];if(p&&p.length){let[l]=p,{hash:f}=new URL(l.href);c.hash!==f&&(c.hash=f,history.replaceState({},"",`${c}`))}else c.hash="",history.replaceState({},"",`${c}`)}),_s(e,{viewport$:t,header$:r}).pipe(O(a=>i.next(a)),A(()=>i.complete()),m(a=>P({ref:e},a)))})}function As(e,{viewport$:t,main$:r,target$:o}){let n=t.pipe(m(({offset:{y:s}})=>s),ot(2,1),m(([s,a])=>s>a&&a>0),Y()),i=r.pipe(m(({active:s})=>s));return z([i,n]).pipe(m(([s,a])=>!(s&&a)),Y(),W(o.pipe(Ie(1))),ae(!0),vt({delay:250}),m(s=>({hidden:s})))}function Ii(e,{viewport$:t,header$:r,main$:o,target$:n}){let i=new T,s=i.pipe(oe(),ae(!0));return i.subscribe({next({hidden:a}){e.hidden=a,a?(e.setAttribute("tabindex","-1"),e.blur()):e.removeAttribute("tabindex")},complete(){e.style.top="",e.hidden=!0,e.removeAttribute("tabindex")}}),r.pipe(W(s),ne("height")).subscribe(({height:a})=>{e.style.top=`${a+16}px`}),h(e,"click").subscribe(a=>{a.preventDefault(),window.scrollTo({top:0})}),As(e,{viewport$:t,main$:o,target$:n}).pipe(O(a=>i.next(a)),A(()=>i.complete()),m(a=>P({ref:e},a)))}function Fi({document$:e,viewport$:t}){e.pipe(b(()=>M(".md-ellipsis")),J(r=>mt(r).pipe(W(e.pipe(Ie(1))),g(o=>o),m(()=>r),Ee(1))),g(r=>r.offsetWidth{let o=r.innerText,n=r.closest("a")||r;return n.title=o,V("content.tooltips")?Xe(n,{viewport$:t}).pipe(W(e.pipe(Ie(1))),A(()=>n.removeAttribute("title"))):y})).subscribe(),V("content.tooltips")&&e.pipe(b(()=>M(".md-status")),J(r=>Xe(r,{viewport$:t}))).subscribe()}function ji({document$:e,tablet$:t}){e.pipe(b(()=>M(".md-toggle--indeterminate")),O(r=>{r.indeterminate=!0,r.checked=!1}),J(r=>h(r,"change").pipe(Jr(()=>r.classList.contains("md-toggle--indeterminate")),m(()=>r))),te(t)).subscribe(([r,o])=>{r.classList.remove("md-toggle--indeterminate"),o&&(r.checked=!1)})}function Cs(){return/(iPad|iPhone|iPod)/.test(navigator.userAgent)}function Ui({document$:e}){e.pipe(b(()=>M("[data-md-scrollfix]")),O(t=>t.removeAttribute("data-md-scrollfix")),g(Cs),J(t=>h(t,"touchstart").pipe(m(()=>t)))).subscribe(t=>{let r=t.scrollTop;r===0?t.scrollTop=1:r+t.offsetHeight===t.scrollHeight&&(t.scrollTop=r-1)})}function Wi({viewport$:e,tablet$:t}){z([Je("search"),t]).pipe(m(([r,o])=>r&&!o),b(r=>$(r).pipe(nt(r?400:100))),te(e)).subscribe(([r,{offset:{y:o}}])=>{if(r)document.body.setAttribute("data-md-scrolllock",""),document.body.style.top=`-${o}px`;else{let n=-1*parseInt(document.body.style.top,10);document.body.removeAttribute("data-md-scrolllock"),document.body.style.top="",n&&window.scrollTo(0,n)}})}Object.entries||(Object.entries=function(e){let t=[];for(let r of Object.keys(e))t.push([r,e[r]]);return t});Object.values||(Object.values=function(e){let t=[];for(let r of Object.keys(e))t.push(e[r]);return t});typeof Element!="undefined"&&(Element.prototype.scrollTo||(Element.prototype.scrollTo=function(e,t){typeof e=="object"?(this.scrollLeft=e.left,this.scrollTop=e.top):(this.scrollLeft=e,this.scrollTop=t)}),Element.prototype.replaceWith||(Element.prototype.replaceWith=function(...e){let t=this.parentNode;if(t){e.length===0&&t.removeChild(this);for(let r=e.length-1;r>=0;r--){let o=e[r];typeof o=="string"?o=document.createTextNode(o):o.parentNode&&o.parentNode.removeChild(o),r?t.insertBefore(this.previousSibling,o):t.replaceChild(o,this)}}}));function ks(){return location.protocol==="file:"?_t(`${new URL("search/search_index.js",Or.base)}`).pipe(m(()=>__index),Z(1)):ze(new URL("search/search_index.json",Or.base))}document.documentElement.classList.remove("no-js");document.documentElement.classList.add("js");var ct=an(),Kt=bn(),Ht=yn(Kt),mo=hn(),ke=Ln(),Lr=Wt("(min-width: 60em)"),Vi=Wt("(min-width: 76.25em)"),Ni=xn(),Or=Te(),zi=document.forms.namedItem("search")?ks():tt,fo=new T;di({alert$:fo});ui({document$:ct});var uo=new T,qi=kt(Or.base);V("navigation.instant")&&gi({sitemap$:qi,location$:Kt,viewport$:ke,progress$:uo}).subscribe(ct);var Di;((Di=Or.version)==null?void 0:Di.provider)==="mike"&&Ti({document$:ct});L(Kt,Ht).pipe(nt(125)).subscribe(()=>{at("drawer",!1),at("search",!1)});mo.pipe(g(({mode:e})=>e==="global")).subscribe(e=>{switch(e.type){case"p":case",":let t=ue("link[rel=prev]");typeof t!="undefined"&&st(t);break;case"n":case".":let r=ue("link[rel=next]");typeof r!="undefined"&&st(r);break;case"Enter":let o=Ne();o instanceof HTMLLabelElement&&o.click()}});Fi({viewport$:ke,document$:ct});ji({document$:ct,tablet$:Lr});Ui({document$:ct});Wi({viewport$:ke,tablet$:Lr});var ft=ai(Ce("header"),{viewport$:ke}),qt=ct.pipe(m(()=>Ce("main")),b(e=>pi(e,{viewport$:ke,header$:ft})),Z(1)),Hs=L(...me("consent").map(e=>An(e,{target$:Ht})),...me("dialog").map(e=>ni(e,{alert$:fo})),...me("palette").map(e=>li(e)),...me("progress").map(e=>mi(e,{progress$:uo})),...me("search").map(e=>_i(e,{index$:zi,keyboard$:mo})),...me("source").map(e=>$i(e))),$s=H(()=>L(...me("announce").map(e=>_n(e)),...me("content").map(e=>oi(e,{sitemap$:qi,viewport$:ke,target$:Ht,print$:Ni})),...me("content").map(e=>V("search.highlight")?Ai(e,{index$:zi,location$:Kt}):y),...me("header").map(e=>si(e,{viewport$:ke,header$:ft,main$:qt})),...me("header-title").map(e=>ci(e,{viewport$:ke,header$:ft})),...me("sidebar").map(e=>e.getAttribute("data-md-type")==="navigation"?eo(Vi,()=>lo(e,{viewport$:ke,header$:ft,main$:qt})):eo(Lr,()=>lo(e,{viewport$:ke,header$:ft,main$:qt}))),...me("tabs").map(e=>Pi(e,{viewport$:ke,header$:ft})),...me("toc").map(e=>Ri(e,{viewport$:ke,header$:ft,main$:qt,target$:Ht})),...me("top").map(e=>Ii(e,{viewport$:ke,header$:ft,main$:qt,target$:Ht})))),Ki=ct.pipe(b(()=>$s),Ve(Hs),Z(1));Ki.subscribe();window.document$=ct;window.location$=Kt;window.target$=Ht;window.keyboard$=mo;window.viewport$=ke;window.tablet$=Lr;window.screen$=Vi;window.print$=Ni;window.alert$=fo;window.progress$=uo;window.component$=Ki;})(); //# sourceMappingURL=bundle.79ae519e.min.js.map -/*! update cache: 20260104155751 */ +/*! update cache: 20260104161450 */ diff --git a/en/assets/javascripts/glightbox.min.js b/en/assets/javascripts/glightbox.min.js index d1e8fae8c..b58f0c2e2 100644 --- a/en/assets/javascripts/glightbox.min.js +++ b/en/assets/javascripts/glightbox.min.js @@ -1,2 +1,2 @@ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).GLightbox=t()}(this,(function(){"use strict";function e(t){return(e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(t)}function t(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){for(var i=0;i1&&void 0!==arguments[1]?arguments[1]:null,i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null,n=e[s]=e[s]||[],l={all:n,evt:null,found:null};return t&&i&&P(n)>0&&o(n,(function(e,n){if(e.eventName==t&&e.fn.toString()==i.toString())return l.found=!0,l.evt=n,!1})),l}function a(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=t.onElement,n=t.withCallback,s=t.avoidDuplicate,l=void 0===s||s,a=t.once,h=void 0!==a&&a,d=t.useCapture,c=void 0!==d&&d,u=arguments.length>2?arguments[2]:void 0,g=i||[];function v(e){T(n)&&n.call(u,e,this),h&&v.destroy()}return C(g)&&(g=document.querySelectorAll(g)),v.destroy=function(){o(g,(function(t){var i=r(t,e,v);i.found&&i.all.splice(i.evt,1),t.removeEventListener&&t.removeEventListener(e,v,c)}))},o(g,(function(t){var i=r(t,e,v);(t.addEventListener&&l&&!i.found||!l)&&(t.addEventListener(e,v,c),i.all.push({eventName:e,fn:v}))})),v}function h(e,t){o(t.split(" "),(function(t){return e.classList.add(t)}))}function d(e,t){o(t.split(" "),(function(t){return e.classList.remove(t)}))}function c(e,t){return e.classList.contains(t)}function u(e,t){for(;e!==document.body;){if(!(e=e.parentElement))return!1;if("function"==typeof e.matches?e.matches(t):e.msMatchesSelector(t))return e}}function g(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",i=arguments.length>2&&void 0!==arguments[2]&&arguments[2];if(!e||""===t)return!1;if("none"==t)return T(i)&&i(),!1;var n=x(),s=t.split(" ");o(s,(function(t){h(e,"g"+t)})),a(n,{onElement:e,avoidDuplicate:!1,once:!0,withCallback:function(e,t){o(s,(function(e){d(t,"g"+e)})),T(i)&&i()}})}function v(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";if(""==t)return e.style.webkitTransform="",e.style.MozTransform="",e.style.msTransform="",e.style.OTransform="",e.style.transform="",!1;e.style.webkitTransform=t,e.style.MozTransform=t,e.style.msTransform=t,e.style.OTransform=t,e.style.transform=t}function f(e){e.style.display="block"}function p(e){e.style.display="none"}function m(e){var t=document.createDocumentFragment(),i=document.createElement("div");for(i.innerHTML=e;i.firstChild;)t.appendChild(i.firstChild);return t}function y(){return{width:window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth,height:window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight}}function x(){var e,t=document.createElement("fakeelement"),i={animation:"animationend",OAnimation:"oAnimationEnd",MozAnimation:"animationend",WebkitAnimation:"webkitAnimationEnd"};for(e in i)if(void 0!==t.style[e])return i[e]}function b(e,t,i,n){if(e())t();else{var s;i||(i=100);var l=setInterval((function(){e()&&(clearInterval(l),s&&clearTimeout(s),t())}),i);n&&(s=setTimeout((function(){clearInterval(l)}),n))}}function S(e,t,i){if(I(e))console.error("Inject assets error");else if(T(t)&&(i=t,t=!1),C(t)&&t in window)T(i)&&i();else{var n;if(-1!==e.indexOf(".css")){if((n=document.querySelectorAll('link[href="'+e+'"]'))&&n.length>0)return void(T(i)&&i());var s=document.getElementsByTagName("head")[0],l=s.querySelectorAll('link[rel="stylesheet"]'),o=document.createElement("link");return o.rel="stylesheet",o.type="text/css",o.href=e,o.media="all",l?s.insertBefore(o,l[0]):s.appendChild(o),void(T(i)&&i())}if((n=document.querySelectorAll('script[src="'+e+'"]'))&&n.length>0){if(T(i)){if(C(t))return b((function(){return void 0!==window[t]}),(function(){i()})),!1;i()}}else{var r=document.createElement("script");r.type="text/javascript",r.src=e,r.onload=function(){if(T(i)){if(C(t))return b((function(){return void 0!==window[t]}),(function(){i()})),!1;i()}},document.body.appendChild(r)}}}function w(){return"navigator"in window&&window.navigator.userAgent.match(/(iPad)|(iPhone)|(iPod)|(Android)|(PlayBook)|(BB10)|(BlackBerry)|(Opera Mini)|(IEMobile)|(webOS)|(MeeGo)/i)}function T(e){return"function"==typeof e}function C(e){return"string"==typeof e}function k(e){return!(!e||!e.nodeType||1!=e.nodeType)}function E(e){return Array.isArray(e)}function A(e){return e&&e.length&&isFinite(e.length)}function L(t){return"object"===e(t)&&null!=t&&!T(t)&&!E(t)}function I(e){return null==e}function O(e,t){return null!==e&&hasOwnProperty.call(e,t)}function P(e){if(L(e)){if(e.keys)return e.keys().length;var t=0;for(var i in e)O(e,i)&&t++;return t}return e.length}function M(e){return!isNaN(parseFloat(e))&&isFinite(e)}function z(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:-1,t=document.querySelectorAll(".gbtn[data-taborder]:not(.disabled)");if(!t.length)return!1;if(1==t.length)return t[0];"string"==typeof e&&(e=parseInt(e));var i=[];o(t,(function(e){i.push(e.getAttribute("data-taborder"))}));var n=Math.max.apply(Math,i.map((function(e){return parseInt(e)}))),s=e<0?1:e+1;s>n&&(s="1");var l=i.filter((function(e){return e>=parseInt(s)})),r=l.sort()[0];return document.querySelector('.gbtn[data-taborder="'.concat(r,'"]'))}function X(e){if(e.events.hasOwnProperty("keyboard"))return!1;e.events.keyboard=a("keydown",{onElement:window,withCallback:function(t,i){var n=(t=t||window.event).keyCode;if(9==n){var s=document.querySelector(".gbtn.focused");if(!s){var l=!(!document.activeElement||!document.activeElement.nodeName)&&document.activeElement.nodeName.toLocaleLowerCase();if("input"==l||"textarea"==l||"button"==l)return}t.preventDefault();var o=document.querySelectorAll(".gbtn[data-taborder]");if(!o||o.length<=0)return;if(!s){var r=z();return void(r&&(r.focus(),h(r,"focused")))}var a=z(s.getAttribute("data-taborder"));d(s,"focused"),a&&(a.focus(),h(a,"focused"))}39==n&&e.nextSlide(),37==n&&e.prevSlide(),27==n&&e.close()}})}function Y(e){return Math.sqrt(e.x*e.x+e.y*e.y)}function q(e,t){var i=function(e,t){var i=Y(e)*Y(t);if(0===i)return 0;var n=function(e,t){return e.x*t.x+e.y*t.y}(e,t)/i;return n>1&&(n=1),Math.acos(n)}(e,t);return function(e,t){return e.x*t.y-t.x*e.y}(e,t)>0&&(i*=-1),180*i/Math.PI}var N=function(){function e(i){t(this,e),this.handlers=[],this.el=i}return n(e,[{key:"add",value:function(e){this.handlers.push(e)}},{key:"del",value:function(e){e||(this.handlers=[]);for(var t=this.handlers.length;t>=0;t--)this.handlers[t]===e&&this.handlers.splice(t,1)}},{key:"dispatch",value:function(){for(var e=0,t=this.handlers.length;e=0)console.log("ignore drag for this touched element",e.target.nodeName.toLowerCase());else{this.now=Date.now(),this.x1=e.touches[0].pageX,this.y1=e.touches[0].pageY,this.delta=this.now-(this.last||this.now),this.touchStart.dispatch(e,this.element),null!==this.preTapPosition.x&&(this.isDoubleTap=this.delta>0&&this.delta<=250&&Math.abs(this.preTapPosition.x-this.x1)<30&&Math.abs(this.preTapPosition.y-this.y1)<30,this.isDoubleTap&&clearTimeout(this.singleTapTimeout)),this.preTapPosition.x=this.x1,this.preTapPosition.y=this.y1,this.last=this.now;var t=this.preV;if(e.touches.length>1){this._cancelLongTap(),this._cancelSingleTap();var i={x:e.touches[1].pageX-this.x1,y:e.touches[1].pageY-this.y1};t.x=i.x,t.y=i.y,this.pinchStartLen=Y(t),this.multipointStart.dispatch(e,this.element)}this._preventTap=!1,this.longTapTimeout=setTimeout(function(){this.longTap.dispatch(e,this.element),this._preventTap=!0}.bind(this),750)}}}},{key:"move",value:function(e){if(e.touches){var t=this.preV,i=e.touches.length,n=e.touches[0].pageX,s=e.touches[0].pageY;if(this.isDoubleTap=!1,i>1){var l=e.touches[1].pageX,o=e.touches[1].pageY,r={x:e.touches[1].pageX-n,y:e.touches[1].pageY-s};null!==t.x&&(this.pinchStartLen>0&&(e.zoom=Y(r)/this.pinchStartLen,this.pinch.dispatch(e,this.element)),e.angle=q(r,t),this.rotate.dispatch(e,this.element)),t.x=r.x,t.y=r.y,null!==this.x2&&null!==this.sx2?(e.deltaX=(n-this.x2+l-this.sx2)/2,e.deltaY=(s-this.y2+o-this.sy2)/2):(e.deltaX=0,e.deltaY=0),this.twoFingerPressMove.dispatch(e,this.element),this.sx2=l,this.sy2=o}else{if(null!==this.x2){e.deltaX=n-this.x2,e.deltaY=s-this.y2;var a=Math.abs(this.x1-this.x2),h=Math.abs(this.y1-this.y2);(a>10||h>10)&&(this._preventTap=!0)}else e.deltaX=0,e.deltaY=0;this.pressMove.dispatch(e,this.element)}this.touchMove.dispatch(e,this.element),this._cancelLongTap(),this.x2=n,this.y2=s,i>1&&e.preventDefault()}}},{key:"end",value:function(e){if(e.changedTouches){this._cancelLongTap();var t=this;e.touches.length<2&&(this.multipointEnd.dispatch(e,this.element),this.sx2=this.sy2=null),this.x2&&Math.abs(this.x1-this.x2)>30||this.y2&&Math.abs(this.y1-this.y2)>30?(e.direction=this._swipeDirection(this.x1,this.x2,this.y1,this.y2),this.swipeTimeout=setTimeout((function(){t.swipe.dispatch(e,t.element)}),0)):(this.tapTimeout=setTimeout((function(){t._preventTap||t.tap.dispatch(e,t.element),t.isDoubleTap&&(t.doubleTap.dispatch(e,t.element),t.isDoubleTap=!1)}),0),t.isDoubleTap||(t.singleTapTimeout=setTimeout((function(){t.singleTap.dispatch(e,t.element)}),250))),this.touchEnd.dispatch(e,this.element),this.preV.x=0,this.preV.y=0,this.zoom=1,this.pinchStartLen=null,this.x1=this.x2=this.y1=this.y2=null}}},{key:"cancelAll",value:function(){this._preventTap=!0,clearTimeout(this.singleTapTimeout),clearTimeout(this.tapTimeout),clearTimeout(this.longTapTimeout),clearTimeout(this.swipeTimeout)}},{key:"cancel",value:function(e){this.cancelAll(),this.touchCancel.dispatch(e,this.element)}},{key:"_cancelLongTap",value:function(){clearTimeout(this.longTapTimeout)}},{key:"_cancelSingleTap",value:function(){clearTimeout(this.singleTapTimeout)}},{key:"_swipeDirection",value:function(e,t,i,n){return Math.abs(e-t)>=Math.abs(i-n)?e-t>0?"Left":"Right":i-n>0?"Up":"Down"}},{key:"on",value:function(e,t){this[e]&&this[e].add(t)}},{key:"off",value:function(e,t){this[e]&&this[e].del(t)}},{key:"destroy",value:function(){return this.singleTapTimeout&&clearTimeout(this.singleTapTimeout),this.tapTimeout&&clearTimeout(this.tapTimeout),this.longTapTimeout&&clearTimeout(this.longTapTimeout),this.swipeTimeout&&clearTimeout(this.swipeTimeout),this.element.removeEventListener("touchstart",this.start),this.element.removeEventListener("touchmove",this.move),this.element.removeEventListener("touchend",this.end),this.element.removeEventListener("touchcancel",this.cancel),this.rotate.del(),this.touchStart.del(),this.multipointStart.del(),this.multipointEnd.del(),this.pinch.del(),this.swipe.del(),this.tap.del(),this.doubleTap.del(),this.longTap.del(),this.singleTap.del(),this.pressMove.del(),this.twoFingerPressMove.del(),this.touchMove.del(),this.touchEnd.del(),this.touchCancel.del(),this.preV=this.pinchStartLen=this.zoom=this.isDoubleTap=this.delta=this.last=this.now=this.tapTimeout=this.singleTapTimeout=this.longTapTimeout=this.swipeTimeout=this.x1=this.x2=this.y1=this.y2=this.preTapPosition=this.rotate=this.touchStart=this.multipointStart=this.multipointEnd=this.pinch=this.swipe=this.tap=this.doubleTap=this.longTap=this.singleTap=this.pressMove=this.touchMove=this.touchEnd=this.touchCancel=this.twoFingerPressMove=null,window.removeEventListener("scroll",this._cancelAllHandler),null}}]),e}();function W(e){var t=function(){var e,t=document.createElement("fakeelement"),i={transition:"transitionend",OTransition:"oTransitionEnd",MozTransition:"transitionend",WebkitTransition:"webkitTransitionEnd"};for(e in i)if(void 0!==t.style[e])return i[e]}(),i=window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth,n=c(e,"gslide-media")?e:e.querySelector(".gslide-media"),s=u(n,".ginner-container"),l=e.querySelector(".gslide-description");i>769&&(n=s),h(n,"greset"),v(n,"translate3d(0, 0, 0)"),a(t,{onElement:n,once:!0,withCallback:function(e,t){d(n,"greset")}}),n.style.opacity="",l&&(l.style.opacity="")}function B(e){if(e.events.hasOwnProperty("touch"))return!1;var t,i,n,s=y(),l=s.width,o=s.height,r=!1,a=null,g=null,f=null,p=!1,m=1,x=1,b=!1,S=!1,w=null,T=null,C=null,k=null,E=0,A=0,L=!1,I=!1,O={},P={},M=0,z=0,X=document.getElementById("glightbox-slider"),Y=document.querySelector(".goverlay"),q=new _(X,{touchStart:function(t){if(r=!0,(c(t.targetTouches[0].target,"ginner-container")||u(t.targetTouches[0].target,".gslide-desc")||"a"==t.targetTouches[0].target.nodeName.toLowerCase())&&(r=!1),u(t.targetTouches[0].target,".gslide-inline")&&!c(t.targetTouches[0].target.parentNode,"gslide-inline")&&(r=!1),r){if(P=t.targetTouches[0],O.pageX=t.targetTouches[0].pageX,O.pageY=t.targetTouches[0].pageY,M=t.targetTouches[0].clientX,z=t.targetTouches[0].clientY,a=e.activeSlide,g=a.querySelector(".gslide-media"),n=a.querySelector(".gslide-inline"),f=null,c(g,"gslide-image")&&(f=g.querySelector("img")),(window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth)>769&&(g=a.querySelector(".ginner-container")),d(Y,"greset"),t.pageX>20&&t.pageXo){var a=O.pageX-P.pageX;if(Math.abs(a)<=13)return!1}p=!0;var h,d=s.targetTouches[0].clientX,c=s.targetTouches[0].clientY,u=M-d,m=z-c;if(Math.abs(u)>Math.abs(m)?(L=!1,I=!0):(I=!1,L=!0),t=P.pageX-O.pageX,E=100*t/l,i=P.pageY-O.pageY,A=100*i/o,L&&f&&(h=1-Math.abs(i)/o,Y.style.opacity=h,e.settings.touchFollowAxis&&(E=0)),I&&(h=1-Math.abs(t)/l,g.style.opacity=h,e.settings.touchFollowAxis&&(A=0)),!f)return v(g,"translate3d(".concat(E,"%, 0, 0)"));v(g,"translate3d(".concat(E,"%, ").concat(A,"%, 0)"))}},touchEnd:function(){if(r){if(p=!1,S||b)return C=w,void(k=T);var t=Math.abs(parseInt(A)),i=Math.abs(parseInt(E));if(!(t>29&&f))return t<29&&i<25?(h(Y,"greset"),Y.style.opacity=1,W(g)):void 0;e.close()}},multipointEnd:function(){setTimeout((function(){b=!1}),50)},multipointStart:function(){b=!0,m=x||1},pinch:function(e){if(!f||p)return!1;b=!0,f.scaleX=f.scaleY=m*e.zoom;var t=m*e.zoom;if(S=!0,t<=1)return S=!1,t=1,k=null,C=null,w=null,T=null,void f.setAttribute("style","");t>4.5&&(t=4.5),f.style.transform="scale3d(".concat(t,", ").concat(t,", 1)"),x=t},pressMove:function(e){if(S&&!b){var t=P.pageX-O.pageX,i=P.pageY-O.pageY;C&&(t+=C),k&&(i+=k),w=t,T=i;var n="translate3d(".concat(t,"px, ").concat(i,"px, 0)");x&&(n+=" scale3d(".concat(x,", ").concat(x,", 1)")),v(f,n)}},swipe:function(t){if(!S)if(b)b=!1;else{if("Left"==t.direction){if(e.index==e.elements.length-1)return W(g);e.nextSlide()}if("Right"==t.direction){if(0==e.index)return W(g);e.prevSlide()}}}});e.events.touch=q}var H=function(){function e(i,n){var s=this,l=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;if(t(this,e),this.img=i,this.slide=n,this.onclose=l,this.img.setZoomEvents)return!1;this.active=!1,this.zoomedIn=!1,this.dragging=!1,this.currentX=null,this.currentY=null,this.initialX=null,this.initialY=null,this.xOffset=0,this.yOffset=0,this.img.addEventListener("mousedown",(function(e){return s.dragStart(e)}),!1),this.img.addEventListener("mouseup",(function(e){return s.dragEnd(e)}),!1),this.img.addEventListener("mousemove",(function(e){return s.drag(e)}),!1),this.img.addEventListener("click",(function(e){return s.slide.classList.contains("dragging-nav")?(s.zoomOut(),!1):s.zoomedIn?void(s.zoomedIn&&!s.dragging&&s.zoomOut()):s.zoomIn()}),!1),this.img.setZoomEvents=!0}return n(e,[{key:"zoomIn",value:function(){var e=this.widowWidth();if(!(this.zoomedIn||e<=768)){var t=this.img;if(t.setAttribute("data-style",t.getAttribute("style")),t.style.maxWidth=t.naturalWidth+"px",t.style.maxHeight=t.naturalHeight+"px",t.naturalWidth>e){var i=e/2-t.naturalWidth/2;this.setTranslate(this.img.parentNode,i,0)}this.slide.classList.add("zoomed"),this.zoomedIn=!0}}},{key:"zoomOut",value:function(){this.img.parentNode.setAttribute("style",""),this.img.setAttribute("style",this.img.getAttribute("data-style")),this.slide.classList.remove("zoomed"),this.zoomedIn=!1,this.currentX=null,this.currentY=null,this.initialX=null,this.initialY=null,this.xOffset=0,this.yOffset=0,this.onclose&&"function"==typeof this.onclose&&this.onclose()}},{key:"dragStart",value:function(e){e.preventDefault(),this.zoomedIn?("touchstart"===e.type?(this.initialX=e.touches[0].clientX-this.xOffset,this.initialY=e.touches[0].clientY-this.yOffset):(this.initialX=e.clientX-this.xOffset,this.initialY=e.clientY-this.yOffset),e.target===this.img&&(this.active=!0,this.img.classList.add("dragging"))):this.active=!1}},{key:"dragEnd",value:function(e){var t=this;e.preventDefault(),this.initialX=this.currentX,this.initialY=this.currentY,this.active=!1,setTimeout((function(){t.dragging=!1,t.img.isDragging=!1,t.img.classList.remove("dragging")}),100)}},{key:"drag",value:function(e){this.active&&(e.preventDefault(),"touchmove"===e.type?(this.currentX=e.touches[0].clientX-this.initialX,this.currentY=e.touches[0].clientY-this.initialY):(this.currentX=e.clientX-this.initialX,this.currentY=e.clientY-this.initialY),this.xOffset=this.currentX,this.yOffset=this.currentY,this.img.isDragging=!0,this.dragging=!0,this.setTranslate(this.img,this.currentX,this.currentY))}},{key:"onMove",value:function(e){if(this.zoomedIn){var t=e.clientX-this.img.naturalWidth/2,i=e.clientY-this.img.naturalHeight/2;this.setTranslate(this.img,t,i)}}},{key:"setTranslate",value:function(e,t,i){e.style.transform="translate3d("+t+"px, "+i+"px, 0)"}},{key:"widowWidth",value:function(){return window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth}}]),e}(),V=function(){function e(){var i=this,n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,e);var s=n.dragEl,l=n.toleranceX,o=void 0===l?40:l,r=n.toleranceY,a=void 0===r?65:r,h=n.slide,d=void 0===h?null:h,c=n.instance,u=void 0===c?null:c;this.el=s,this.active=!1,this.dragging=!1,this.currentX=null,this.currentY=null,this.initialX=null,this.initialY=null,this.xOffset=0,this.yOffset=0,this.direction=null,this.lastDirection=null,this.toleranceX=o,this.toleranceY=a,this.toleranceReached=!1,this.dragContainer=this.el,this.slide=d,this.instance=u,this.el.addEventListener("mousedown",(function(e){return i.dragStart(e)}),!1),this.el.addEventListener("mouseup",(function(e){return i.dragEnd(e)}),!1),this.el.addEventListener("mousemove",(function(e){return i.drag(e)}),!1)}return n(e,[{key:"dragStart",value:function(e){if(this.slide.classList.contains("zoomed"))this.active=!1;else{"touchstart"===e.type?(this.initialX=e.touches[0].clientX-this.xOffset,this.initialY=e.touches[0].clientY-this.yOffset):(this.initialX=e.clientX-this.xOffset,this.initialY=e.clientY-this.yOffset);var t=e.target.nodeName.toLowerCase();e.target.classList.contains("nodrag")||u(e.target,".nodrag")||-1!==["input","select","textarea","button","a"].indexOf(t)?this.active=!1:(e.preventDefault(),(e.target===this.el||"img"!==t&&u(e.target,".gslide-inline"))&&(this.active=!0,this.el.classList.add("dragging"),this.dragContainer=u(e.target,".ginner-container")))}}},{key:"dragEnd",value:function(e){var t=this;e&&e.preventDefault(),this.initialX=0,this.initialY=0,this.currentX=null,this.currentY=null,this.initialX=null,this.initialY=null,this.xOffset=0,this.yOffset=0,this.active=!1,this.doSlideChange&&(this.instance.preventOutsideClick=!0,"right"==this.doSlideChange&&this.instance.prevSlide(),"left"==this.doSlideChange&&this.instance.nextSlide()),this.doSlideClose&&this.instance.close(),this.toleranceReached||this.setTranslate(this.dragContainer,0,0,!0),setTimeout((function(){t.instance.preventOutsideClick=!1,t.toleranceReached=!1,t.lastDirection=null,t.dragging=!1,t.el.isDragging=!1,t.el.classList.remove("dragging"),t.slide.classList.remove("dragging-nav"),t.dragContainer.style.transform="",t.dragContainer.style.transition=""}),100)}},{key:"drag",value:function(e){if(this.active){e.preventDefault(),this.slide.classList.add("dragging-nav"),"touchmove"===e.type?(this.currentX=e.touches[0].clientX-this.initialX,this.currentY=e.touches[0].clientY-this.initialY):(this.currentX=e.clientX-this.initialX,this.currentY=e.clientY-this.initialY),this.xOffset=this.currentX,this.yOffset=this.currentY,this.el.isDragging=!0,this.dragging=!0,this.doSlideChange=!1,this.doSlideClose=!1;var t=Math.abs(this.currentX),i=Math.abs(this.currentY);if(t>0&&t>=Math.abs(this.currentY)&&(!this.lastDirection||"x"==this.lastDirection)){this.yOffset=0,this.lastDirection="x",this.setTranslate(this.dragContainer,this.currentX,0);var n=this.shouldChange();if(!this.instance.settings.dragAutoSnap&&n&&(this.doSlideChange=n),this.instance.settings.dragAutoSnap&&n)return this.instance.preventOutsideClick=!0,this.toleranceReached=!0,this.active=!1,this.instance.preventOutsideClick=!0,this.dragEnd(null),"right"==n&&this.instance.prevSlide(),void("left"==n&&this.instance.nextSlide())}if(this.toleranceY>0&&i>0&&i>=t&&(!this.lastDirection||"y"==this.lastDirection)){this.xOffset=0,this.lastDirection="y",this.setTranslate(this.dragContainer,0,this.currentY);var s=this.shouldClose();return!this.instance.settings.dragAutoSnap&&s&&(this.doSlideClose=!0),void(this.instance.settings.dragAutoSnap&&s&&this.instance.close())}}}},{key:"shouldChange",value:function(){var e=!1;if(Math.abs(this.currentX)>=this.toleranceX){var t=this.currentX>0?"right":"left";("left"==t&&this.slide!==this.slide.parentNode.lastChild||"right"==t&&this.slide!==this.slide.parentNode.firstChild)&&(e=t)}return e}},{key:"shouldClose",value:function(){var e=!1;return Math.abs(this.currentY)>=this.toleranceY&&(e=!0),e}},{key:"setTranslate",value:function(e,t,i){var n=arguments.length>3&&void 0!==arguments[3]&&arguments[3];e.style.transition=n?"all .2s ease":"",e.style.transform="translate3d(".concat(t,"px, ").concat(i,"px, 0)")}}]),e}();function j(e,t,i,n){var s=e.querySelector(".gslide-media"),l=new Image,o="gSlideTitle_"+i,r="gSlideDesc_"+i;l.addEventListener("load",(function(){T(n)&&n()}),!1),l.src=t.href,""!=t.sizes&&""!=t.srcset&&(l.sizes=t.sizes,l.srcset=t.srcset),l.alt="",I(t.alt)||""===t.alt||(l.alt=t.alt),""!==t.title&&l.setAttribute("aria-labelledby",o),""!==t.description&&l.setAttribute("aria-describedby",r),t.hasOwnProperty("_hasCustomWidth")&&t._hasCustomWidth&&(l.style.width=t.width),t.hasOwnProperty("_hasCustomHeight")&&t._hasCustomHeight&&(l.style.height=t.height),s.insertBefore(l,s.firstChild)}function F(e,t,i,n){var s=this,l=e.querySelector(".ginner-container"),o="gvideo"+i,r=e.querySelector(".gslide-media"),a=this.getAllPlayers();h(l,"gvideo-container"),r.insertBefore(m('
    '),r.firstChild);var d=e.querySelector(".gvideo-wrapper");S(this.settings.plyr.css,"Plyr");var c=t.href,u=location.protocol.replace(":",""),g="",v="",f=!1;"file"==u&&(u="http"),r.style.maxWidth=t.width,S(this.settings.plyr.js,"Plyr",(function(){if(c.match(/vimeo\.com\/([0-9]*)/)){var l=/vimeo.*\/(\d+)/i.exec(c);g="vimeo",v=l[1]}if(c.match(/(youtube\.com|youtube-nocookie\.com)\/watch\?v=([a-zA-Z0-9\-_]+)/)||c.match(/youtu\.be\/([a-zA-Z0-9\-_]+)/)||c.match(/(youtube\.com|youtube-nocookie\.com)\/embed\/([a-zA-Z0-9\-_]+)/)){var r=function(e){var t="";t=void 0!==(e=e.replace(/(>|<)/gi,"").split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/))[2]?(t=e[2].split(/[^0-9a-z_\-]/i))[0]:e;return t}(c);g="youtube",v=r}if(null!==c.match(/\.(mp4|ogg|webm|mov)$/)){g="local";var u='")}var w=f||m('
    '));h(d,"".concat(g,"-video gvideo")),d.appendChild(w),d.setAttribute("data-id",o),d.setAttribute("data-index",i);var C=O(s.settings.plyr,"config")?s.settings.plyr.config:{},k=new Plyr("#"+o,C);k.on("ready",(function(e){var t=e.detail.plyr;a[o]=t,T(n)&&n()})),b((function(){return e.querySelector("iframe")&&"true"==e.querySelector("iframe").dataset.ready}),(function(){s.resize(e)})),k.on("enterfullscreen",R),k.on("exitfullscreen",R)}))}function R(e){var t=u(e.target,".gslide-media");"enterfullscreen"==e.type&&h(t,"fullscreen"),"exitfullscreen"==e.type&&d(t,"fullscreen")}function G(e,t,i,n){var s,l=this,o=e.querySelector(".gslide-media"),r=!(!O(t,"href")||!t.href)&&t.href.split("#").pop().trim(),d=!(!O(t,"content")||!t.content)&&t.content;if(d&&(C(d)&&(s=m('
    '.concat(d,"
    "))),k(d))){"none"==d.style.display&&(d.style.display="block");var c=document.createElement("div");c.className="ginlined-content",c.appendChild(d),s=c}if(r){var u=document.getElementById(r);if(!u)return!1;var g=u.cloneNode(!0);g.style.height=t.height,g.style.maxWidth=t.width,h(g,"ginlined-content"),s=g}if(!s)return console.error("Unable to append inline slide content",t),!1;o.style.height=t.height,o.style.width=t.width,o.appendChild(s),this.events["inlineclose"+r]=a("click",{onElement:o.querySelectorAll(".gtrigger-close"),withCallback:function(e){e.preventDefault(),l.close()}}),T(n)&&n()}function Z(e,t,i,n){var s=e.querySelector(".gslide-media"),l=function(e){var t=e.url,i=e.allow,n=e.callback,s=e.appendTo,l=document.createElement("iframe");return l.className="vimeo-video gvideo",l.src=t,l.style.width="100%",l.style.height="100%",i&&l.setAttribute("allow",i),l.onload=function(){h(l,"node-ready"),T(n)&&n()},s&&s.appendChild(l),l}({url:t.href,callback:n});s.parentNode.style.maxWidth=t.width,s.parentNode.style.height=t.height,s.appendChild(l)}var $=function(){function e(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,e),this.defaults={href:"",sizes:"",srcset:"",title:"",type:"",description:"",alt:"",descPosition:"bottom",effect:"",width:"",height:"",content:!1,zoomable:!0,draggable:!0},L(i)&&(this.defaults=l(this.defaults,i))}return n(e,[{key:"sourceType",value:function(e){var t=e;if(null!==(e=e.toLowerCase()).match(/\.(jpeg|jpg|jpe|gif|png|apn|webp|avif|svg)/))return"image";if(e.match(/(youtube\.com|youtube-nocookie\.com)\/watch\?v=([a-zA-Z0-9\-_]+)/)||e.match(/youtu\.be\/([a-zA-Z0-9\-_]+)/)||e.match(/(youtube\.com|youtube-nocookie\.com)\/embed\/([a-zA-Z0-9\-_]+)/))return"video";if(e.match(/vimeo\.com\/([0-9]*)/))return"video";if(null!==e.match(/\.(mp4|ogg|webm|mov)/))return"video";if(null!==e.match(/\.(mp3|wav|wma|aac|ogg)/))return"audio";if(e.indexOf("#")>-1&&""!==t.split("#").pop().trim())return"inline";return e.indexOf("goajax=true")>-1?"ajax":"external"}},{key:"parseConfig",value:function(e,t){var i=this,n=l({descPosition:t.descPosition},this.defaults);if(L(e)&&!k(e)){O(e,"type")||(O(e,"content")&&e.content?e.type="inline":O(e,"href")&&(e.type=this.sourceType(e.href)));var s=l(n,e);return this.setSize(s,t),s}var r="",a=e.getAttribute("data-glightbox"),h=e.nodeName.toLowerCase();if("a"===h&&(r=e.href),"img"===h&&(r=e.src,n.alt=e.alt),n.href=r,o(n,(function(s,l){O(t,l)&&"width"!==l&&(n[l]=t[l]);var o=e.dataset[l];I(o)||(n[l]=i.sanitizeValue(o))})),n.content&&(n.type="inline"),!n.type&&r&&(n.type=this.sourceType(r)),I(a)){if(!n.title&&"a"==h){var d=e.title;I(d)||""===d||(n.title=d)}if(!n.title&&"img"==h){var c=e.alt;I(c)||""===c||(n.title=c)}}else{var u=[];o(n,(function(e,t){u.push(";\\s?"+t)})),u=u.join("\\s?:|"),""!==a.trim()&&o(n,(function(e,t){var s=a,l=new RegExp("s?"+t+"s?:s?(.*?)("+u+"s?:|$)"),o=s.match(l);if(o&&o.length&&o[1]){var r=o[1].trim().replace(/;\s*$/,"");n[t]=i.sanitizeValue(r)}}))}if(n.description&&"."===n.description.substring(0,1)){var g;try{g=document.querySelector(n.description).innerHTML}catch(e){if(!(e instanceof DOMException))throw e}g&&(n.description=g)}if(!n.description){var v=e.querySelector(".glightbox-desc");v&&(n.description=v.innerHTML)}return this.setSize(n,t,e),this.slideConfig=n,n}},{key:"setSize",value:function(e,t){var i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null,n="video"==e.type?this.checkSize(t.videosWidth):this.checkSize(t.width),s=this.checkSize(t.height);return e.width=O(e,"width")&&""!==e.width?this.checkSize(e.width):n,e.height=O(e,"height")&&""!==e.height?this.checkSize(e.height):s,i&&"image"==e.type&&(e._hasCustomWidth=!!i.dataset.width,e._hasCustomHeight=!!i.dataset.height),e}},{key:"checkSize",value:function(e){return M(e)?"".concat(e,"px"):e}},{key:"sanitizeValue",value:function(e){return"true"!==e&&"false"!==e?e:"true"===e}}]),e}(),U=function(){function e(i,n,s){t(this,e),this.element=i,this.instance=n,this.index=s}return n(e,[{key:"setContent",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,i=arguments.length>1&&void 0!==arguments[1]&&arguments[1];if(c(t,"loaded"))return!1;var n=this.instance.settings,s=this.slideConfig,l=w();T(n.beforeSlideLoad)&&n.beforeSlideLoad({index:this.index,slide:t,player:!1});var o=s.type,r=s.descPosition,a=t.querySelector(".gslide-media"),d=t.querySelector(".gslide-title"),u=t.querySelector(".gslide-desc"),g=t.querySelector(".gdesc-inner"),v=i,f="gSlideTitle_"+this.index,p="gSlideDesc_"+this.index;if(T(n.afterSlideLoad)&&(v=function(){T(i)&&i(),n.afterSlideLoad({index:e.index,slide:t,player:e.instance.getSlidePlayerInstance(e.index)})}),""==s.title&&""==s.description?g&&g.parentNode.parentNode.removeChild(g.parentNode):(d&&""!==s.title?(d.id=f,d.innerHTML=s.title):d.parentNode.removeChild(d),u&&""!==s.description?(u.id=p,l&&n.moreLength>0?(s.smallDescription=this.slideShortDesc(s.description,n.moreLength,n.moreText),u.innerHTML=s.smallDescription,this.descriptionEvents(u,s)):u.innerHTML=s.description):u.parentNode.removeChild(u),h(a.parentNode,"desc-".concat(r)),h(g.parentNode,"description-".concat(r))),h(a,"gslide-".concat(o)),h(t,"loaded"),"video"!==o){if("external"!==o)return"inline"===o?(G.apply(this.instance,[t,s,this.index,v]),void(s.draggable&&new V({dragEl:t.querySelector(".gslide-inline"),toleranceX:n.dragToleranceX,toleranceY:n.dragToleranceY,slide:t,instance:this.instance}))):void("image"!==o?T(v)&&v():j(t,s,this.index,(function(){var i=t.querySelector("img");s.draggable&&new V({dragEl:i,toleranceX:n.dragToleranceX,toleranceY:n.dragToleranceY,slide:t,instance:e.instance}),s.zoomable&&i.naturalWidth>i.offsetWidth&&(h(i,"zoomable"),new H(i,t,(function(){e.instance.resize()}))),T(v)&&v()})));Z.apply(this,[t,s,this.index,v])}else F.apply(this.instance,[t,s,this.index,v])}},{key:"slideShortDesc",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:50,i=arguments.length>2&&void 0!==arguments[2]&&arguments[2],n=document.createElement("div");n.innerHTML=e;var s=n.innerText,l=i;if((e=s.trim()).length<=t)return e;var o=e.substr(0,t-1);return l?(n=null,o+'... '+i+""):o}},{key:"descriptionEvents",value:function(e,t){var i=this,n=e.querySelector(".desc-more");if(!n)return!1;a("click",{onElement:n,withCallback:function(e,n){e.preventDefault();var s=document.body,l=u(n,".gslide-desc");if(!l)return!1;l.innerHTML=t.description,h(s,"gdesc-open");var o=a("click",{onElement:[s,u(l,".gslide-description")],withCallback:function(e,n){"a"!==e.target.nodeName.toLowerCase()&&(d(s,"gdesc-open"),h(s,"gdesc-closed"),l.innerHTML=t.smallDescription,i.descriptionEvents(l,t),setTimeout((function(){d(s,"gdesc-closed")}),400),o.destroy())}})}})}},{key:"create",value:function(){return m(this.instance.settings.slideHTML)}},{key:"getConfig",value:function(){k(this.element)||this.element.hasOwnProperty("draggable")||(this.element.draggable=this.instance.settings.draggable);var e=new $(this.instance.settings.slideExtraAttributes);return this.slideConfig=e.parseConfig(this.element,this.instance.settings),this.slideConfig}}]),e}(),J=w(),K=null!==w()||void 0!==document.createTouch||"ontouchstart"in window||"onmsgesturechange"in window||navigator.msMaxTouchPoints,Q=document.getElementsByTagName("html")[0],ee={selector:".glightbox",elements:null,skin:"clean",theme:"clean",closeButton:!0,startAt:null,autoplayVideos:!0,autofocusVideos:!0,descPosition:"bottom",width:"900px",height:"506px",videosWidth:"960px",beforeSlideChange:null,afterSlideChange:null,beforeSlideLoad:null,afterSlideLoad:null,slideInserted:null,slideRemoved:null,slideExtraAttributes:null,onOpen:null,onClose:null,loop:!1,zoomable:!0,draggable:!0,dragAutoSnap:!1,dragToleranceX:40,dragToleranceY:65,preload:!0,oneSlidePerOpen:!1,touchNavigation:!0,touchFollowAxis:!0,keyboardNavigation:!0,closeOnOutsideClick:!0,plugins:!1,plyr:{css:"https://cdn.plyr.io/3.6.8/plyr.css",js:"https://cdn.plyr.io/3.6.8/plyr.js",config:{ratio:"16:9",fullscreen:{enabled:!0,iosNative:!0},youtube:{noCookie:!0,rel:0,showinfo:0,iv_load_policy:3},vimeo:{byline:!1,portrait:!1,title:!1,transparent:!1}}},openEffect:"zoom",closeEffect:"zoom",slideEffect:"slide",moreText:"See more",moreLength:60,cssEfects:{fade:{in:"fadeIn",out:"fadeOut"},zoom:{in:"zoomIn",out:"zoomOut"},slide:{in:"slideInRight",out:"slideOutLeft"},slideBack:{in:"slideInLeft",out:"slideOutRight"},none:{in:"none",out:"none"}},svg:{close:'',next:' ',prev:''},slideHTML:'
    \n
    \n
    \n
    \n
    \n
    \n
    \n

    \n
    \n
    \n
    \n
    \n
    \n
    ',lightboxHTML:''},te=function(){function e(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,e),this.customOptions=i,this.settings=l(ee,i),this.effectsClasses=this.getAnimationClasses(),this.videoPlayers={},this.apiEvents=[],this.fullElementsList=!1}return n(e,[{key:"init",value:function(){var e=this,t=this.getSelector();t&&(this.baseEvents=a("click",{onElement:t,withCallback:function(t,i){t.preventDefault(),e.open(i)}})),this.elements=this.getElements()}},{key:"open",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;if(0==this.elements.length)return!1;this.activeSlide=null,this.prevActiveSlideIndex=null,this.prevActiveSlide=null;var i=M(t)?t:this.settings.startAt;if(k(e)){var n=e.getAttribute("data-gallery");n&&(this.fullElementsList=this.elements,this.elements=this.getGalleryElements(this.elements,n)),I(i)&&(i=this.getElementIndex(e))<0&&(i=0)}M(i)||(i=0),this.build(),g(this.overlay,"none"==this.settings.openEffect?"none":this.settings.cssEfects.fade.in);var s=document.body,l=window.innerWidth-document.documentElement.clientWidth;if(l>0){var o=document.createElement("style");o.type="text/css",o.className="gcss-styles",o.innerText=".gscrollbar-fixer {margin-right: ".concat(l,"px}"),document.head.appendChild(o),h(s,"gscrollbar-fixer")}h(s,"glightbox-open"),h(Q,"glightbox-open"),J&&(h(document.body,"glightbox-mobile"),this.settings.slideEffect="slide"),this.showSlide(i,!0),1==this.elements.length?(h(this.prevButton,"glightbox-button-hidden"),h(this.nextButton,"glightbox-button-hidden")):(d(this.prevButton,"glightbox-button-hidden"),d(this.nextButton,"glightbox-button-hidden")),this.lightboxOpen=!0,this.trigger("open"),T(this.settings.onOpen)&&this.settings.onOpen(),K&&this.settings.touchNavigation&&B(this),this.settings.keyboardNavigation&&X(this)}},{key:"openAt",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;this.open(null,e)}},{key:"showSlide",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,i=arguments.length>1&&void 0!==arguments[1]&&arguments[1];f(this.loader),this.index=parseInt(t);var n=this.slidesContainer.querySelector(".current");n&&d(n,"current"),this.slideAnimateOut();var s=this.slidesContainer.querySelectorAll(".gslide")[t];if(c(s,"loaded"))this.slideAnimateIn(s,i),p(this.loader);else{f(this.loader);var l=this.elements[t],o={index:this.index,slide:s,slideNode:s,slideConfig:l.slideConfig,slideIndex:this.index,trigger:l.node,player:null};this.trigger("slide_before_load",o),l.instance.setContent(s,(function(){p(e.loader),e.resize(),e.slideAnimateIn(s,i),e.trigger("slide_after_load",o)}))}this.slideDescription=s.querySelector(".gslide-description"),this.slideDescriptionContained=this.slideDescription&&c(this.slideDescription.parentNode,"gslide-media"),this.settings.preload&&(this.preloadSlide(t+1),this.preloadSlide(t-1)),this.updateNavigationClasses(),this.activeSlide=s}},{key:"preloadSlide",value:function(e){var t=this;if(e<0||e>this.elements.length-1)return!1;if(I(this.elements[e]))return!1;var i=this.slidesContainer.querySelectorAll(".gslide")[e];if(c(i,"loaded"))return!1;var n=this.elements[e],s=n.type,l={index:e,slide:i,slideNode:i,slideConfig:n.slideConfig,slideIndex:e,trigger:n.node,player:null};this.trigger("slide_before_load",l),"video"==s||"external"==s?setTimeout((function(){n.instance.setContent(i,(function(){t.trigger("slide_after_load",l)}))}),200):n.instance.setContent(i,(function(){t.trigger("slide_after_load",l)}))}},{key:"prevSlide",value:function(){this.goToSlide(this.index-1)}},{key:"nextSlide",value:function(){this.goToSlide(this.index+1)}},{key:"goToSlide",value:function(){var e=arguments.length>0&&void 0!==arguments[0]&&arguments[0];if(this.prevActiveSlide=this.activeSlide,this.prevActiveSlideIndex=this.index,!this.loop()&&(e<0||e>this.elements.length-1))return!1;e<0?e=this.elements.length-1:e>=this.elements.length&&(e=0),this.showSlide(e)}},{key:"insertSlide",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:-1;t<0&&(t=this.elements.length);var i=new U(e,this,t),n=i.getConfig(),s=l({},n),o=i.create(),r=this.elements.length-1;s.index=t,s.node=!1,s.instance=i,s.slideConfig=n,this.elements.splice(t,0,s);var a=null,h=null;if(this.slidesContainer){if(t>r)this.slidesContainer.appendChild(o);else{var d=this.slidesContainer.querySelectorAll(".gslide")[t];this.slidesContainer.insertBefore(o,d)}(this.settings.preload&&0==this.index&&0==t||this.index-1==t||this.index+1==t)&&this.preloadSlide(t),0==this.index&&0==t&&(this.index=1),this.updateNavigationClasses(),a=this.slidesContainer.querySelectorAll(".gslide")[t],h=this.getSlidePlayerInstance(t),s.slideNode=a}this.trigger("slide_inserted",{index:t,slide:a,slideNode:a,slideConfig:n,slideIndex:t,trigger:null,player:h}),T(this.settings.slideInserted)&&this.settings.slideInserted({index:t,slide:a,player:h})}},{key:"removeSlide",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:-1;if(e<0||e>this.elements.length-1)return!1;var t=this.slidesContainer&&this.slidesContainer.querySelectorAll(".gslide")[e];t&&(this.getActiveSlideIndex()==e&&(e==this.elements.length-1?this.prevSlide():this.nextSlide()),t.parentNode.removeChild(t)),this.elements.splice(e,1),this.trigger("slide_removed",e),T(this.settings.slideRemoved)&&this.settings.slideRemoved(e)}},{key:"slideAnimateIn",value:function(e,t){var i=this,n=e.querySelector(".gslide-media"),s=e.querySelector(".gslide-description"),l={index:this.prevActiveSlideIndex,slide:this.prevActiveSlide,slideNode:this.prevActiveSlide,slideIndex:this.prevActiveSlide,slideConfig:I(this.prevActiveSlideIndex)?null:this.elements[this.prevActiveSlideIndex].slideConfig,trigger:I(this.prevActiveSlideIndex)?null:this.elements[this.prevActiveSlideIndex].node,player:this.getSlidePlayerInstance(this.prevActiveSlideIndex)},o={index:this.index,slide:this.activeSlide,slideNode:this.activeSlide,slideConfig:this.elements[this.index].slideConfig,slideIndex:this.index,trigger:this.elements[this.index].node,player:this.getSlidePlayerInstance(this.index)};if(n.offsetWidth>0&&s&&(p(s),s.style.display=""),d(e,this.effectsClasses),t)g(e,this.settings.cssEfects[this.settings.openEffect].in,(function(){i.settings.autoplayVideos&&i.slidePlayerPlay(e),i.trigger("slide_changed",{prev:l,current:o}),T(i.settings.afterSlideChange)&&i.settings.afterSlideChange.apply(i,[l,o])}));else{var r=this.settings.slideEffect,a="none"!==r?this.settings.cssEfects[r].in:r;this.prevActiveSlideIndex>this.index&&"slide"==this.settings.slideEffect&&(a=this.settings.cssEfects.slideBack.in),g(e,a,(function(){i.settings.autoplayVideos&&i.slidePlayerPlay(e),i.trigger("slide_changed",{prev:l,current:o}),T(i.settings.afterSlideChange)&&i.settings.afterSlideChange.apply(i,[l,o])}))}setTimeout((function(){i.resize(e)}),100),h(e,"current")}},{key:"slideAnimateOut",value:function(){if(!this.prevActiveSlide)return!1;var e=this.prevActiveSlide;d(e,this.effectsClasses),h(e,"prev");var t=this.settings.slideEffect,i="none"!==t?this.settings.cssEfects[t].out:t;this.slidePlayerPause(e),this.trigger("slide_before_change",{prev:{index:this.prevActiveSlideIndex,slide:this.prevActiveSlide,slideNode:this.prevActiveSlide,slideIndex:this.prevActiveSlideIndex,slideConfig:I(this.prevActiveSlideIndex)?null:this.elements[this.prevActiveSlideIndex].slideConfig,trigger:I(this.prevActiveSlideIndex)?null:this.elements[this.prevActiveSlideIndex].node,player:this.getSlidePlayerInstance(this.prevActiveSlideIndex)},current:{index:this.index,slide:this.activeSlide,slideNode:this.activeSlide,slideIndex:this.index,slideConfig:this.elements[this.index].slideConfig,trigger:this.elements[this.index].node,player:this.getSlidePlayerInstance(this.index)}}),T(this.settings.beforeSlideChange)&&this.settings.beforeSlideChange.apply(this,[{index:this.prevActiveSlideIndex,slide:this.prevActiveSlide,player:this.getSlidePlayerInstance(this.prevActiveSlideIndex)},{index:this.index,slide:this.activeSlide,player:this.getSlidePlayerInstance(this.index)}]),this.prevActiveSlideIndex>this.index&&"slide"==this.settings.slideEffect&&(i=this.settings.cssEfects.slideBack.out),g(e,i,(function(){var t=e.querySelector(".ginner-container"),i=e.querySelector(".gslide-media"),n=e.querySelector(".gslide-description");t.style.transform="",i.style.transform="",d(i,"greset"),i.style.opacity="",n&&(n.style.opacity=""),d(e,"prev")}))}},{key:"getAllPlayers",value:function(){return this.videoPlayers}},{key:"getSlidePlayerInstance",value:function(e){var t="gvideo"+e,i=this.getAllPlayers();return!(!O(i,t)||!i[t])&&i[t]}},{key:"stopSlideVideo",value:function(e){if(k(e)){var t=e.querySelector(".gvideo-wrapper");t&&(e=t.getAttribute("data-index"))}console.log("stopSlideVideo is deprecated, use slidePlayerPause");var i=this.getSlidePlayerInstance(e);i&&i.playing&&i.pause()}},{key:"slidePlayerPause",value:function(e){if(k(e)){var t=e.querySelector(".gvideo-wrapper");t&&(e=t.getAttribute("data-index"))}var i=this.getSlidePlayerInstance(e);i&&i.playing&&i.pause()}},{key:"playSlideVideo",value:function(e){if(k(e)){var t=e.querySelector(".gvideo-wrapper");t&&(e=t.getAttribute("data-index"))}console.log("playSlideVideo is deprecated, use slidePlayerPlay");var i=this.getSlidePlayerInstance(e);i&&!i.playing&&i.play()}},{key:"slidePlayerPlay",value:function(e){if(k(e)){var t=e.querySelector(".gvideo-wrapper");t&&(e=t.getAttribute("data-index"))}var i=this.getSlidePlayerInstance(e);i&&!i.playing&&(i.play(),this.settings.autofocusVideos&&i.elements.container.focus())}},{key:"setElements",value:function(e){var t=this;this.settings.elements=!1;var i=[];e&&e.length&&o(e,(function(e,n){var s=new U(e,t,n),o=s.getConfig(),r=l({},o);r.slideConfig=o,r.instance=s,r.index=n,i.push(r)})),this.elements=i,this.lightboxOpen&&(this.slidesContainer.innerHTML="",this.elements.length&&(o(this.elements,(function(){var e=m(t.settings.slideHTML);t.slidesContainer.appendChild(e)})),this.showSlide(0,!0)))}},{key:"getElementIndex",value:function(e){var t=!1;return o(this.elements,(function(i,n){if(O(i,"node")&&i.node==e)return t=n,!0})),t}},{key:"getElements",value:function(){var e=this,t=[];this.elements=this.elements?this.elements:[],!I(this.settings.elements)&&E(this.settings.elements)&&this.settings.elements.length&&o(this.settings.elements,(function(i,n){var s=new U(i,e,n),o=s.getConfig(),r=l({},o);r.node=!1,r.index=n,r.instance=s,r.slideConfig=o,t.push(r)}));var i=!1;return this.getSelector()&&(i=document.querySelectorAll(this.getSelector())),i?(o(i,(function(i,n){var s=new U(i,e,n),o=s.getConfig(),r=l({},o);r.node=i,r.index=n,r.instance=s,r.slideConfig=o,r.gallery=i.getAttribute("data-gallery"),t.push(r)})),t):t}},{key:"getGalleryElements",value:function(e,t){return e.filter((function(e){return e.gallery==t}))}},{key:"getSelector",value:function(){return!this.settings.elements&&(this.settings.selector&&"data-"==this.settings.selector.substring(0,5)?"*[".concat(this.settings.selector,"]"):this.settings.selector)}},{key:"getActiveSlide",value:function(){return this.slidesContainer.querySelectorAll(".gslide")[this.index]}},{key:"getActiveSlideIndex",value:function(){return this.index}},{key:"getAnimationClasses",value:function(){var e=[];for(var t in this.settings.cssEfects)if(this.settings.cssEfects.hasOwnProperty(t)){var i=this.settings.cssEfects[t];e.push("g".concat(i.in)),e.push("g".concat(i.out))}return e.join(" ")}},{key:"build",value:function(){var e=this;if(this.built)return!1;var t=document.body.childNodes,i=[];o(t,(function(e){e.parentNode==document.body&&"#"!==e.nodeName.charAt(0)&&e.hasAttribute&&!e.hasAttribute("aria-hidden")&&(i.push(e),e.setAttribute("aria-hidden","true"))}));var n=O(this.settings.svg,"next")?this.settings.svg.next:"",s=O(this.settings.svg,"prev")?this.settings.svg.prev:"",l=O(this.settings.svg,"close")?this.settings.svg.close:"",r=this.settings.lightboxHTML;r=m(r=(r=(r=r.replace(/{nextSVG}/g,n)).replace(/{prevSVG}/g,s)).replace(/{closeSVG}/g,l)),document.body.appendChild(r);var d=document.getElementById("glightbox-body");this.modal=d;var g=d.querySelector(".gclose");this.prevButton=d.querySelector(".gprev"),this.nextButton=d.querySelector(".gnext"),this.overlay=d.querySelector(".goverlay"),this.loader=d.querySelector(".gloader"),this.slidesContainer=document.getElementById("glightbox-slider"),this.bodyHiddenChildElms=i,this.events={},h(this.modal,"glightbox-"+this.settings.skin),this.settings.closeButton&&g&&(this.events.close=a("click",{onElement:g,withCallback:function(t,i){t.preventDefault(),e.close()}})),g&&!this.settings.closeButton&&g.parentNode.removeChild(g),this.nextButton&&(this.events.next=a("click",{onElement:this.nextButton,withCallback:function(t,i){t.preventDefault(),e.nextSlide()}})),this.prevButton&&(this.events.prev=a("click",{onElement:this.prevButton,withCallback:function(t,i){t.preventDefault(),e.prevSlide()}})),this.settings.closeOnOutsideClick&&(this.events.outClose=a("click",{onElement:d,withCallback:function(t,i){e.preventOutsideClick||c(document.body,"glightbox-mobile")||u(t.target,".ginner-container")||u(t.target,".gbtn")||c(t.target,"gnext")||c(t.target,"gprev")||e.close()}})),o(this.elements,(function(t,i){e.slidesContainer.appendChild(t.instance.create()),t.slideNode=e.slidesContainer.querySelectorAll(".gslide")[i]})),K&&h(document.body,"glightbox-touch"),this.events.resize=a("resize",{onElement:window,withCallback:function(){e.resize()}}),this.built=!0}},{key:"resize",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;if((e=e||this.activeSlide)&&!c(e,"zoomed")){var t=y(),i=e.querySelector(".gvideo-wrapper"),n=e.querySelector(".gslide-image"),s=this.slideDescription,l=t.width,o=t.height;if(l<=768?h(document.body,"glightbox-mobile"):d(document.body,"glightbox-mobile"),i||n){var r=!1;if(s&&(c(s,"description-bottom")||c(s,"description-top"))&&!c(s,"gabsolute")&&(r=!0),n)if(l<=768)n.querySelector("img");else if(r){var a=s.offsetHeight,u=n.querySelector("img");u.setAttribute("style","max-height: calc(100vh - ".concat(a,"px)")),s.setAttribute("style","max-width: ".concat(u.offsetWidth,"px;"))}if(i){var g=O(this.settings.plyr.config,"ratio")?this.settings.plyr.config.ratio:"";if(!g){var v=i.clientWidth,f=i.clientHeight,p=v/f;g="".concat(v/p,":").concat(f/p)}var m=g.split(":"),x=this.settings.videosWidth,b=this.settings.videosWidth,S=(b=M(x)||-1!==x.indexOf("px")?parseInt(x):-1!==x.indexOf("vw")?l*parseInt(x)/100:-1!==x.indexOf("vh")?o*parseInt(x)/100:-1!==x.indexOf("%")?l*parseInt(x)/100:parseInt(i.clientWidth))/(parseInt(m[0])/parseInt(m[1]));if(S=Math.floor(S),r&&(o-=s.offsetHeight),b>l||S>o||ob){var w=i.offsetWidth,T=i.offsetHeight,C=o/T,k={width:w*C,height:T*C};i.parentNode.setAttribute("style","max-width: ".concat(k.width,"px")),r&&s.setAttribute("style","max-width: ".concat(k.width,"px;"))}else i.parentNode.style.maxWidth="".concat(x),r&&s.setAttribute("style","max-width: ".concat(x,";"))}}}}},{key:"reload",value:function(){this.init()}},{key:"updateNavigationClasses",value:function(){var e=this.loop();d(this.nextButton,"disabled"),d(this.prevButton,"disabled"),0==this.index&&this.elements.length-1==0?(h(this.prevButton,"disabled"),h(this.nextButton,"disabled")):0!==this.index||e?this.index!==this.elements.length-1||e||h(this.nextButton,"disabled"):h(this.prevButton,"disabled")}},{key:"loop",value:function(){var e=O(this.settings,"loopAtEnd")?this.settings.loopAtEnd:null;return e=O(this.settings,"loop")?this.settings.loop:e,e}},{key:"close",value:function(){var e=this;if(!this.lightboxOpen){if(this.events){for(var t in this.events)this.events.hasOwnProperty(t)&&this.events[t].destroy();this.events=null}return!1}if(this.closing)return!1;this.closing=!0,this.slidePlayerPause(this.activeSlide),this.fullElementsList&&(this.elements=this.fullElementsList),this.bodyHiddenChildElms.length&&o(this.bodyHiddenChildElms,(function(e){e.removeAttribute("aria-hidden")})),h(this.modal,"glightbox-closing"),g(this.overlay,"none"==this.settings.openEffect?"none":this.settings.cssEfects.fade.out),g(this.activeSlide,this.settings.cssEfects[this.settings.closeEffect].out,(function(){if(e.activeSlide=null,e.prevActiveSlideIndex=null,e.prevActiveSlide=null,e.built=!1,e.events){for(var t in e.events)e.events.hasOwnProperty(t)&&e.events[t].destroy();e.events=null}var i=document.body;d(Q,"glightbox-open"),d(i,"glightbox-open touching gdesc-open glightbox-touch glightbox-mobile gscrollbar-fixer"),e.modal.parentNode.removeChild(e.modal),e.trigger("close"),T(e.settings.onClose)&&e.settings.onClose();var n=document.querySelector(".gcss-styles");n&&n.parentNode.removeChild(n),e.lightboxOpen=!1,e.closing=null}))}},{key:"destroy",value:function(){this.close(),this.clearAllEvents(),this.baseEvents&&this.baseEvents.destroy()}},{key:"on",value:function(e,t){var i=arguments.length>2&&void 0!==arguments[2]&&arguments[2];if(!e||!T(t))throw new TypeError("Event name and callback must be defined");this.apiEvents.push({evt:e,once:i,callback:t})}},{key:"once",value:function(e,t){this.on(e,t,!0)}},{key:"trigger",value:function(e){var t=this,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,n=[];o(this.apiEvents,(function(t,s){var l=t.evt,o=t.once,r=t.callback;l==e&&(r(i),o&&n.push(s))})),n.length&&o(n,(function(e){return t.apiEvents.splice(e,1)}))}},{key:"clearAllEvents",value:function(){this.apiEvents.splice(0,this.apiEvents.length)}},{key:"version",value:function(){return"3.1.1"}}]),e}();return function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=new te(e);return t.init(),t}})); -/*! update cache: 20260104155751 */ +/*! update cache: 20260104161450 */ diff --git a/en/assets/stylesheets/glightbox.min.css b/en/assets/stylesheets/glightbox.min.css index 568e8ada4..5e1310cfa 100644 --- a/en/assets/stylesheets/glightbox.min.css +++ b/en/assets/stylesheets/glightbox.min.css @@ -1,2 +1,2 @@ .glightbox-container{width:100%;height:100%;position:fixed;top:0;left:0;z-index:999999!important;overflow:hidden;-ms-touch-action:none;touch-action:none;-webkit-text-size-adjust:100%;-moz-text-size-adjust:100%;-ms-text-size-adjust:100%;text-size-adjust:100%;-webkit-backface-visibility:hidden;backface-visibility:hidden;outline:0}.glightbox-container.inactive{display:none}.glightbox-container .gcontainer{position:relative;width:100%;height:100%;z-index:9999;overflow:hidden}.glightbox-container .gslider{-webkit-transition:-webkit-transform .4s ease;transition:-webkit-transform .4s ease;transition:transform .4s ease;transition:transform .4s ease,-webkit-transform .4s ease;height:100%;left:0;top:0;width:100%;position:relative;overflow:hidden;display:-webkit-box!important;display:-ms-flexbox!important;display:flex!important;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}.glightbox-container .gslide{width:100%;position:absolute;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;opacity:0}.glightbox-container .gslide.current{opacity:1;z-index:99999;position:relative}.glightbox-container .gslide.prev{opacity:1;z-index:9999}.glightbox-container .gslide-inner-content{width:100%}.glightbox-container .ginner-container{position:relative;width:100%;display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column;max-width:100%;margin:auto;height:100vh}.glightbox-container .ginner-container.gvideo-container{width:100%}.glightbox-container .ginner-container.desc-bottom,.glightbox-container .ginner-container.desc-top{-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column}.glightbox-container .ginner-container.desc-left,.glightbox-container .ginner-container.desc-right{max-width:100%!important}.gslide iframe,.gslide video{outline:0!important;border:none;min-height:165px;-webkit-overflow-scrolling:touch;-ms-touch-action:auto;touch-action:auto}.gslide:not(.current){pointer-events:none}.gslide-image{-webkit-box-align:center;-ms-flex-align:center;align-items:center}.gslide-image img{max-height:100vh;display:block;padding:0;float:none;outline:0;border:none;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;max-width:100vw;width:auto;height:auto;-o-object-fit:cover;object-fit:cover;-ms-touch-action:none;touch-action:none;margin:auto;min-width:200px}.desc-bottom .gslide-image img,.desc-top .gslide-image img{width:auto}.desc-left .gslide-image img,.desc-right .gslide-image img{width:auto;max-width:100%}.gslide-image img.zoomable{position:relative}.gslide-image img.dragging{cursor:-webkit-grabbing!important;cursor:grabbing!important;-webkit-transition:none;transition:none}.gslide-video{position:relative;max-width:100vh;width:100%!important}.gslide-video .plyr__poster-enabled.plyr--loading .plyr__poster{display:none}.gslide-video .gvideo-wrapper{width:100%;margin:auto}.gslide-video::before{content:'';position:absolute;width:100%;height:100%;background:rgba(255,0,0,.34);display:none}.gslide-video.playing::before{display:none}.gslide-video.fullscreen{max-width:100%!important;min-width:100%;height:75vh}.gslide-video.fullscreen video{max-width:100%!important;width:100%!important}.gslide-inline{background:#fff;text-align:left;max-height:calc(100vh - 40px);overflow:auto;max-width:100%;margin:auto}.gslide-inline .ginlined-content{padding:20px;width:100%}.gslide-inline .dragging{cursor:-webkit-grabbing!important;cursor:grabbing!important;-webkit-transition:none;transition:none}.ginlined-content{overflow:auto;display:block!important;opacity:1}.gslide-external{display:-webkit-box;display:-ms-flexbox;display:flex;width:100%;min-width:100%;background:#fff;padding:0;overflow:auto;max-height:75vh;height:100%}.gslide-media{display:-webkit-box;display:-ms-flexbox;display:flex;width:auto}.zoomed .gslide-media{-webkit-box-shadow:none!important;box-shadow:none!important}.desc-bottom .gslide-media,.desc-top .gslide-media{margin:0 auto;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column}.gslide-description{position:relative;-webkit-box-flex:1;-ms-flex:1 0 100%;flex:1 0 100%}.gslide-description.description-left,.gslide-description.description-right{max-width:100%}.gslide-description.description-bottom,.gslide-description.description-top{margin:0 auto;width:100%}.gslide-description p{margin-bottom:12px}.gslide-description p:last-child{margin-bottom:0}.zoomed .gslide-description{display:none}.glightbox-button-hidden{display:none}.glightbox-mobile .glightbox-container .gslide-description{height:auto!important;width:100%;position:absolute;bottom:0;padding:19px 11px;max-width:100vw!important;-webkit-box-ordinal-group:3!important;-ms-flex-order:2!important;order:2!important;max-height:78vh;overflow:auto!important;background:-webkit-gradient(linear,left top,left bottom,from(rgba(0,0,0,0)),to(rgba(0,0,0,.75)));background:linear-gradient(to bottom,rgba(0,0,0,0) 0,rgba(0,0,0,.75) 100%);-webkit-transition:opacity .3s linear;transition:opacity .3s linear;padding-bottom:50px}.glightbox-mobile .glightbox-container .gslide-title{color:#fff;font-size:1em}.glightbox-mobile .glightbox-container .gslide-desc{color:#a1a1a1}.glightbox-mobile .glightbox-container .gslide-desc a{color:#fff;font-weight:700}.glightbox-mobile .glightbox-container .gslide-desc *{color:inherit}.glightbox-mobile .glightbox-container .gslide-desc .desc-more{color:#fff;opacity:.4}.gdesc-open .gslide-media{-webkit-transition:opacity .5s ease;transition:opacity .5s ease;opacity:.4}.gdesc-open .gdesc-inner{padding-bottom:30px}.gdesc-closed .gslide-media{-webkit-transition:opacity .5s ease;transition:opacity .5s ease;opacity:1}.greset{-webkit-transition:all .3s ease;transition:all .3s ease}.gabsolute{position:absolute}.grelative{position:relative}.glightbox-desc{display:none!important}.glightbox-open{overflow:hidden}.gloader{height:25px;width:25px;-webkit-animation:lightboxLoader .8s infinite linear;animation:lightboxLoader .8s infinite linear;border:2px solid #fff;border-right-color:transparent;border-radius:50%;position:absolute;display:block;z-index:9999;left:0;right:0;margin:0 auto;top:47%}.goverlay{width:100%;height:calc(100vh + 1px);position:fixed;top:-1px;left:0;background:#000;will-change:opacity}.glightbox-mobile .goverlay{background:#000}.gclose,.gnext,.gprev{z-index:99999;cursor:pointer;width:26px;height:44px;border:none;display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column}.gclose svg,.gnext svg,.gprev svg{display:block;width:25px;height:auto;margin:0;padding:0}.gclose.disabled,.gnext.disabled,.gprev.disabled{opacity:.1}.gclose .garrow,.gnext .garrow,.gprev .garrow{stroke:#fff}.gbtn.focused{outline:2px solid #0f3d81}iframe.wait-autoplay{opacity:0}.glightbox-closing .gclose,.glightbox-closing .gnext,.glightbox-closing .gprev{opacity:0!important}.glightbox-clean .gslide-description{background:#fff}.glightbox-clean .gdesc-inner{padding:22px 20px}.glightbox-clean .gslide-title{font-size:1em;font-weight:400;font-family:arial;color:#000;margin-bottom:19px;line-height:1.4em}.glightbox-clean .gslide-desc{font-size:.86em;margin-bottom:0;font-family:arial;line-height:1.4em}.glightbox-clean .gslide-video{background:#000}.glightbox-clean .gclose,.glightbox-clean .gnext,.glightbox-clean .gprev{background-color:rgba(0,0,0,.75);border-radius:4px}.glightbox-clean .gclose path,.glightbox-clean .gnext path,.glightbox-clean .gprev path{fill:#fff}.glightbox-clean .gprev{position:absolute;top:-100%;left:30px;width:40px;height:50px}.glightbox-clean .gnext{position:absolute;top:-100%;right:30px;width:40px;height:50px}.glightbox-clean .gclose{width:35px;height:35px;top:15px;right:10px;position:absolute}.glightbox-clean .gclose svg{width:18px;height:auto}.glightbox-clean .gclose:hover{opacity:1}.gfadeIn{-webkit-animation:gfadeIn .5s ease;animation:gfadeIn .5s ease}.gfadeOut{-webkit-animation:gfadeOut .5s ease;animation:gfadeOut .5s ease}.gslideOutLeft{-webkit-animation:gslideOutLeft .3s ease;animation:gslideOutLeft .3s ease}.gslideInLeft{-webkit-animation:gslideInLeft .3s ease;animation:gslideInLeft .3s ease}.gslideOutRight{-webkit-animation:gslideOutRight .3s ease;animation:gslideOutRight .3s ease}.gslideInRight{-webkit-animation:gslideInRight .3s ease;animation:gslideInRight .3s ease}.gzoomIn{-webkit-animation:gzoomIn .5s ease;animation:gzoomIn .5s ease}.gzoomOut{-webkit-animation:gzoomOut .5s ease;animation:gzoomOut .5s ease}@-webkit-keyframes lightboxLoader{0%{-webkit-transform:rotate(0);transform:rotate(0)}100%{-webkit-transform:rotate(360deg);transform:rotate(360deg)}}@keyframes lightboxLoader{0%{-webkit-transform:rotate(0);transform:rotate(0)}100%{-webkit-transform:rotate(360deg);transform:rotate(360deg)}}@-webkit-keyframes gfadeIn{from{opacity:0}to{opacity:1}}@keyframes gfadeIn{from{opacity:0}to{opacity:1}}@-webkit-keyframes gfadeOut{from{opacity:1}to{opacity:0}}@keyframes gfadeOut{from{opacity:1}to{opacity:0}}@-webkit-keyframes gslideInLeft{from{opacity:0;-webkit-transform:translate3d(-60%,0,0);transform:translate3d(-60%,0,0)}to{visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0);opacity:1}}@keyframes gslideInLeft{from{opacity:0;-webkit-transform:translate3d(-60%,0,0);transform:translate3d(-60%,0,0)}to{visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0);opacity:1}}@-webkit-keyframes gslideOutLeft{from{opacity:1;visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}to{-webkit-transform:translate3d(-60%,0,0);transform:translate3d(-60%,0,0);opacity:0;visibility:hidden}}@keyframes gslideOutLeft{from{opacity:1;visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}to{-webkit-transform:translate3d(-60%,0,0);transform:translate3d(-60%,0,0);opacity:0;visibility:hidden}}@-webkit-keyframes gslideInRight{from{opacity:0;visibility:visible;-webkit-transform:translate3d(60%,0,0);transform:translate3d(60%,0,0)}to{-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0);opacity:1}}@keyframes gslideInRight{from{opacity:0;visibility:visible;-webkit-transform:translate3d(60%,0,0);transform:translate3d(60%,0,0)}to{-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0);opacity:1}}@-webkit-keyframes gslideOutRight{from{opacity:1;visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}to{-webkit-transform:translate3d(60%,0,0);transform:translate3d(60%,0,0);opacity:0}}@keyframes gslideOutRight{from{opacity:1;visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}to{-webkit-transform:translate3d(60%,0,0);transform:translate3d(60%,0,0);opacity:0}}@-webkit-keyframes gzoomIn{from{opacity:0;-webkit-transform:scale3d(.3,.3,.3);transform:scale3d(.3,.3,.3)}to{opacity:1}}@keyframes gzoomIn{from{opacity:0;-webkit-transform:scale3d(.3,.3,.3);transform:scale3d(.3,.3,.3)}to{opacity:1}}@-webkit-keyframes gzoomOut{from{opacity:1}50%{opacity:0;-webkit-transform:scale3d(.3,.3,.3);transform:scale3d(.3,.3,.3)}to{opacity:0}}@keyframes gzoomOut{from{opacity:1}50%{opacity:0;-webkit-transform:scale3d(.3,.3,.3);transform:scale3d(.3,.3,.3)}to{opacity:0}}@media (min-width:769px){.glightbox-container .ginner-container{width:auto;height:auto;-webkit-box-orient:horizontal;-webkit-box-direction:normal;-ms-flex-direction:row;flex-direction:row}.glightbox-container .ginner-container.desc-top .gslide-description{-webkit-box-ordinal-group:1;-ms-flex-order:0;order:0}.glightbox-container .ginner-container.desc-top .gslide-image,.glightbox-container .ginner-container.desc-top .gslide-image img{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}.glightbox-container .ginner-container.desc-left .gslide-description{-webkit-box-ordinal-group:1;-ms-flex-order:0;order:0}.glightbox-container .ginner-container.desc-left .gslide-image{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}.gslide-image img{max-height:97vh;max-width:100%}.gslide-image img.zoomable{cursor:-webkit-zoom-in;cursor:zoom-in}.zoomed .gslide-image img.zoomable{cursor:-webkit-grab;cursor:grab}.gslide-inline{max-height:95vh}.gslide-external{max-height:100vh}.gslide-description.description-left,.gslide-description.description-right{max-width:275px}.glightbox-open{height:auto}.goverlay{background:rgba(0,0,0,.92)}.glightbox-clean .gslide-media{-webkit-box-shadow:1px 2px 9px 0 rgba(0,0,0,.65);box-shadow:1px 2px 9px 0 rgba(0,0,0,.65)}.glightbox-clean .description-left .gdesc-inner,.glightbox-clean .description-right .gdesc-inner{position:absolute;height:100%;overflow-y:auto}.glightbox-clean .gclose,.glightbox-clean .gnext,.glightbox-clean .gprev{background-color:rgba(0,0,0,.32)}.glightbox-clean .gclose:hover,.glightbox-clean .gnext:hover,.glightbox-clean .gprev:hover{background-color:rgba(0,0,0,.7)}.glightbox-clean .gprev{top:45%}.glightbox-clean .gnext{top:45%}}@media (min-width:992px){.glightbox-clean .gclose{opacity:.7;right:20px}}@media screen and (max-height:420px){.goverlay{background:#000}} -/*! update cache: 20260104155751 */ +/*! update cache: 20260104161450 */ diff --git a/en/assets/stylesheets/main.484c7ddc.min.css b/en/assets/stylesheets/main.484c7ddc.min.css index b700c9f17..8fd6c1472 100644 --- a/en/assets/stylesheets/main.484c7ddc.min.css +++ b/en/assets/stylesheets/main.484c7ddc.min.css @@ -1,2 +1,2 @@ @charset "UTF-8";html{-webkit-text-size-adjust:none;-moz-text-size-adjust:none;text-size-adjust:none;box-sizing:border-box}*,:after,:before{box-sizing:inherit}@media (prefers-reduced-motion){*,:after,:before{transition:none!important}}body{margin:0}a,button,input,label{-webkit-tap-highlight-color:transparent}a{color:inherit;text-decoration:none}hr{border:0;box-sizing:initial;display:block;height:.05rem;overflow:visible;padding:0}small{font-size:80%}sub,sup{line-height:1em}img{border-style:none}table{border-collapse:initial;border-spacing:0}td,th{font-weight:400;vertical-align:top}button{background:#0000;border:0;font-family:inherit;font-size:inherit;margin:0;padding:0}input{border:0;outline:none}:root{--md-primary-fg-color:#4051b5;--md-primary-fg-color--light:#5d6cc0;--md-primary-fg-color--dark:#303fa1;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3;--md-accent-fg-color:#526cfe;--md-accent-fg-color--transparent:#526cfe1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-scheme=default]{color-scheme:light}[data-md-color-scheme=default] img[src$="#gh-dark-mode-only"],[data-md-color-scheme=default] img[src$="#only-dark"]{display:none}:root,[data-md-color-scheme=default]{--md-hue:225deg;--md-default-fg-color:#000000de;--md-default-fg-color--light:#0000008a;--md-default-fg-color--lighter:#00000052;--md-default-fg-color--lightest:#00000012;--md-default-bg-color:#fff;--md-default-bg-color--light:#ffffffb3;--md-default-bg-color--lighter:#ffffff4d;--md-default-bg-color--lightest:#ffffff1f;--md-code-fg-color:#36464e;--md-code-bg-color:#f5f5f5;--md-code-bg-color--light:#f5f5f5b3;--md-code-bg-color--lighter:#f5f5f54d;--md-code-hl-color:#4287ff;--md-code-hl-color--light:#4287ff1a;--md-code-hl-number-color:#d52a2a;--md-code-hl-special-color:#db1457;--md-code-hl-function-color:#a846b9;--md-code-hl-constant-color:#6e59d9;--md-code-hl-keyword-color:#3f6ec6;--md-code-hl-string-color:#1c7d4d;--md-code-hl-name-color:var(--md-code-fg-color);--md-code-hl-operator-color:var(--md-default-fg-color--light);--md-code-hl-punctuation-color:var(--md-default-fg-color--light);--md-code-hl-comment-color:var(--md-default-fg-color--light);--md-code-hl-generic-color:var(--md-default-fg-color--light);--md-code-hl-variable-color:var(--md-default-fg-color--light);--md-typeset-color:var(--md-default-fg-color);--md-typeset-a-color:var(--md-primary-fg-color);--md-typeset-del-color:#f5503d26;--md-typeset-ins-color:#0bd57026;--md-typeset-kbd-color:#fafafa;--md-typeset-kbd-accent-color:#fff;--md-typeset-kbd-border-color:#b8b8b8;--md-typeset-mark-color:#ffff0080;--md-typeset-table-color:#0000001f;--md-typeset-table-color--light:rgba(0,0,0,.035);--md-admonition-fg-color:var(--md-default-fg-color);--md-admonition-bg-color:var(--md-default-bg-color);--md-warning-fg-color:#000000de;--md-warning-bg-color:#ff9;--md-footer-fg-color:#fff;--md-footer-fg-color--light:#ffffffb3;--md-footer-fg-color--lighter:#ffffff73;--md-footer-bg-color:#000000de;--md-footer-bg-color--dark:#00000052;--md-shadow-z1:0 0.2rem 0.5rem #0000000d,0 0 0.05rem #0000001a;--md-shadow-z2:0 0.2rem 0.5rem #0000001a,0 0 0.05rem #00000040;--md-shadow-z3:0 0.2rem 0.5rem #0003,0 0 0.05rem #00000059}.md-icon svg{fill:currentcolor;display:block;height:1.2rem;width:1.2rem}body{-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;--md-text-font-family:var(--md-text-font,_),-apple-system,BlinkMacSystemFont,Helvetica,Arial,sans-serif;--md-code-font-family:var(--md-code-font,_),SFMono-Regular,Consolas,Menlo,monospace}aside,body,input{font-feature-settings:"kern","liga";color:var(--md-typeset-color);font-family:var(--md-text-font-family)}code,kbd,pre{font-feature-settings:"kern";font-family:var(--md-code-font-family)}:root{--md-typeset-table-sort-icon:url('data:image/svg+xml;charset=utf-8,');--md-typeset-table-sort-icon--asc:url('data:image/svg+xml;charset=utf-8,');--md-typeset-table-sort-icon--desc:url('data:image/svg+xml;charset=utf-8,')}.md-typeset{-webkit-print-color-adjust:exact;color-adjust:exact;font-size:.8rem;line-height:1.6;overflow-wrap:break-word}@media print{.md-typeset{font-size:.68rem}}.md-typeset blockquote,.md-typeset dl,.md-typeset figure,.md-typeset ol,.md-typeset pre,.md-typeset ul{margin-bottom:1em;margin-top:1em}.md-typeset h1{color:var(--md-default-fg-color--light);font-size:2em;line-height:1.3;margin:0 0 1.25em}.md-typeset h1,.md-typeset h2{font-weight:300;letter-spacing:-.01em}.md-typeset h2{font-size:1.5625em;line-height:1.4;margin:1.6em 0 .64em}.md-typeset h3{font-size:1.25em;font-weight:400;letter-spacing:-.01em;line-height:1.5;margin:1.6em 0 .8em}.md-typeset h2+h3{margin-top:.8em}.md-typeset h4{font-weight:700;letter-spacing:-.01em;margin:1em 0}.md-typeset h5,.md-typeset h6{color:var(--md-default-fg-color--light);font-size:.8em;font-weight:700;letter-spacing:-.01em;margin:1.25em 0}.md-typeset h5{text-transform:uppercase}.md-typeset h5 code{text-transform:none}.md-typeset hr{border-bottom:.05rem solid var(--md-default-fg-color--lightest);display:flow-root;margin:1.5em 0}.md-typeset a{color:var(--md-typeset-a-color);word-break:break-word}.md-typeset a,.md-typeset a:before{transition:color 125ms}.md-typeset a:focus,.md-typeset a:hover{color:var(--md-accent-fg-color)}.md-typeset a:focus code,.md-typeset a:hover code{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}.md-typeset a code{color:var(--md-typeset-a-color)}.md-typeset a.focus-visible{outline-color:var(--md-accent-fg-color);outline-offset:.2rem}.md-typeset code,.md-typeset kbd,.md-typeset pre{color:var(--md-code-fg-color);direction:ltr;font-variant-ligatures:none;transition:background-color 125ms}@media print{.md-typeset code,.md-typeset kbd,.md-typeset pre{white-space:pre-wrap}}.md-typeset code{background-color:var(--md-code-bg-color);border-radius:.1rem;-webkit-box-decoration-break:clone;box-decoration-break:clone;font-size:.85em;padding:0 .2941176471em;transition:color 125ms,background-color 125ms;word-break:break-word}.md-typeset code:not(.focus-visible){-webkit-tap-highlight-color:transparent;outline:none}.md-typeset pre{display:flow-root;line-height:1.4;position:relative}.md-typeset pre>code{-webkit-box-decoration-break:slice;box-decoration-break:slice;box-shadow:none;display:block;margin:0;outline-color:var(--md-accent-fg-color);overflow:auto;padding:.7720588235em 1.1764705882em;scrollbar-color:var(--md-default-fg-color--lighter) #0000;scrollbar-width:thin;touch-action:auto;word-break:normal}.md-typeset pre>code:hover{scrollbar-color:var(--md-accent-fg-color) #0000}.md-typeset pre>code::-webkit-scrollbar{height:.2rem;width:.2rem}.md-typeset pre>code::-webkit-scrollbar-thumb{background-color:var(--md-default-fg-color--lighter)}.md-typeset pre>code::-webkit-scrollbar-thumb:hover{background-color:var(--md-accent-fg-color)}.md-typeset kbd{background-color:var(--md-typeset-kbd-color);border-radius:.1rem;box-shadow:0 .1rem 0 .05rem var(--md-typeset-kbd-border-color),0 .1rem 0 var(--md-typeset-kbd-border-color),0 -.1rem .2rem var(--md-typeset-kbd-accent-color) inset;color:var(--md-default-fg-color);display:inline-block;font-size:.75em;padding:0 .6666666667em;vertical-align:text-top;word-break:break-word}.md-typeset mark{background-color:var(--md-typeset-mark-color);-webkit-box-decoration-break:clone;box-decoration-break:clone;color:inherit;word-break:break-word}.md-typeset abbr{cursor:help;text-decoration:none}.md-typeset [data-preview],.md-typeset abbr{border-bottom:.05rem dotted var(--md-default-fg-color--light)}.md-typeset small{opacity:.75}[dir=ltr] .md-typeset sub,[dir=ltr] .md-typeset sup{margin-left:.078125em}[dir=rtl] .md-typeset sub,[dir=rtl] .md-typeset sup{margin-right:.078125em}[dir=ltr] .md-typeset blockquote{padding-left:.6rem}[dir=rtl] .md-typeset blockquote{padding-right:.6rem}[dir=ltr] .md-typeset blockquote{border-left:.2rem solid var(--md-default-fg-color--lighter)}[dir=rtl] .md-typeset blockquote{border-right:.2rem solid var(--md-default-fg-color--lighter)}.md-typeset blockquote{color:var(--md-default-fg-color--light);margin-left:0;margin-right:0}.md-typeset ul{list-style-type:disc}.md-typeset ul[type]{list-style-type:revert-layer}[dir=ltr] .md-typeset ol,[dir=ltr] .md-typeset ul{margin-left:.625em}[dir=rtl] .md-typeset ol,[dir=rtl] .md-typeset ul{margin-right:.625em}.md-typeset ol,.md-typeset ul{padding:0}.md-typeset ol:not([hidden]),.md-typeset ul:not([hidden]){display:flow-root}.md-typeset ol ol,.md-typeset ul ol{list-style-type:lower-alpha}.md-typeset ol ol ol,.md-typeset ul ol ol{list-style-type:lower-roman}.md-typeset ol ol ol ol,.md-typeset ul ol ol ol{list-style-type:upper-alpha}.md-typeset ol ol ol ol ol,.md-typeset ul ol ol ol ol{list-style-type:upper-roman}.md-typeset ol[type],.md-typeset ul[type]{list-style-type:revert-layer}[dir=ltr] .md-typeset ol li,[dir=ltr] .md-typeset ul li{margin-left:1.25em}[dir=rtl] .md-typeset ol li,[dir=rtl] .md-typeset ul li{margin-right:1.25em}.md-typeset ol li,.md-typeset ul li{margin-bottom:.5em}.md-typeset ol li blockquote,.md-typeset ol li p,.md-typeset ul li blockquote,.md-typeset ul li p{margin:.5em 0}.md-typeset ol li:last-child,.md-typeset ul li:last-child{margin-bottom:0}[dir=ltr] .md-typeset ol li ol,[dir=ltr] .md-typeset ol li ul,[dir=ltr] .md-typeset ul li ol,[dir=ltr] .md-typeset ul li ul{margin-left:.625em}[dir=rtl] .md-typeset ol li ol,[dir=rtl] .md-typeset ol li ul,[dir=rtl] .md-typeset ul li ol,[dir=rtl] .md-typeset ul li ul{margin-right:.625em}.md-typeset ol li ol,.md-typeset ol li ul,.md-typeset ul li ol,.md-typeset ul li ul{margin-bottom:.5em;margin-top:.5em}[dir=ltr] .md-typeset dd{margin-left:1.875em}[dir=rtl] .md-typeset dd{margin-right:1.875em}.md-typeset dd{margin-bottom:1.5em;margin-top:1em}.md-typeset img,.md-typeset svg,.md-typeset video{height:auto;max-width:100%}.md-typeset img[align=left]{margin:1em 1em 1em 0}.md-typeset img[align=right]{margin:1em 0 1em 1em}.md-typeset img[align]:only-child{margin-top:0}.md-typeset figure{display:flow-root;margin:1em auto;max-width:100%;text-align:center;width:fit-content}.md-typeset figure img{display:block;margin:0 auto}.md-typeset figcaption{font-style:italic;margin:1em auto;max-width:24rem}.md-typeset iframe{max-width:100%}.md-typeset table:not([class]){background-color:var(--md-default-bg-color);border:.05rem solid var(--md-typeset-table-color);border-radius:.1rem;display:inline-block;font-size:.64rem;max-width:100%;overflow:auto;touch-action:auto}@media print{.md-typeset table:not([class]){display:table}}.md-typeset table:not([class])+*{margin-top:1.5em}.md-typeset table:not([class]) td>:first-child,.md-typeset table:not([class]) th>:first-child{margin-top:0}.md-typeset table:not([class]) td>:last-child,.md-typeset table:not([class]) th>:last-child{margin-bottom:0}.md-typeset table:not([class]) td:not([align]),.md-typeset table:not([class]) th:not([align]){text-align:left}[dir=rtl] .md-typeset table:not([class]) td:not([align]),[dir=rtl] .md-typeset table:not([class]) th:not([align]){text-align:right}.md-typeset table:not([class]) th{font-weight:700;min-width:5rem;padding:.9375em 1.25em;vertical-align:top}.md-typeset table:not([class]) td{border-top:.05rem solid var(--md-typeset-table-color);padding:.9375em 1.25em;vertical-align:top}.md-typeset table:not([class]) tbody tr{transition:background-color 125ms}.md-typeset table:not([class]) tbody tr:hover{background-color:var(--md-typeset-table-color--light);box-shadow:0 .05rem 0 var(--md-default-bg-color) inset}.md-typeset table:not([class]) a{word-break:normal}.md-typeset table th[role=columnheader]{cursor:pointer}[dir=ltr] .md-typeset table th[role=columnheader]:after{margin-left:.5em}[dir=rtl] .md-typeset table th[role=columnheader]:after{margin-right:.5em}.md-typeset table th[role=columnheader]:after{content:"";display:inline-block;height:1.2em;-webkit-mask-image:var(--md-typeset-table-sort-icon);mask-image:var(--md-typeset-table-sort-icon);-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;transition:background-color 125ms;vertical-align:text-bottom;width:1.2em}.md-typeset table th[role=columnheader]:hover:after{background-color:var(--md-default-fg-color--lighter)}.md-typeset table th[role=columnheader][aria-sort=ascending]:after{background-color:var(--md-default-fg-color--light);-webkit-mask-image:var(--md-typeset-table-sort-icon--asc);mask-image:var(--md-typeset-table-sort-icon--asc)}.md-typeset table th[role=columnheader][aria-sort=descending]:after{background-color:var(--md-default-fg-color--light);-webkit-mask-image:var(--md-typeset-table-sort-icon--desc);mask-image:var(--md-typeset-table-sort-icon--desc)}.md-typeset__scrollwrap{margin:1em -.8rem;overflow-x:auto;touch-action:auto}.md-typeset__table{display:inline-block;margin-bottom:.5em;padding:0 .8rem}@media print{.md-typeset__table{display:block}}html .md-typeset__table table{display:table;margin:0;overflow:hidden;width:100%}@media screen and (max-width:44.984375em){.md-content__inner>pre{margin:1em -.8rem}.md-content__inner>pre code{border-radius:0}}.md-typeset .md-author{border-radius:100%;display:block;flex-shrink:0;height:1.6rem;overflow:hidden;position:relative;transition:color 125ms,transform 125ms;width:1.6rem}.md-typeset .md-author img{display:block}.md-typeset .md-author--more{background:var(--md-default-fg-color--lightest);color:var(--md-default-fg-color--lighter);font-size:.6rem;font-weight:700;line-height:1.6rem;text-align:center}.md-typeset .md-author--long{height:2.4rem;width:2.4rem}.md-typeset a.md-author{transform:scale(1)}.md-typeset a.md-author img{border-radius:100%;filter:grayscale(100%) opacity(75%);transition:filter 125ms}.md-typeset a.md-author:focus,.md-typeset a.md-author:hover{transform:scale(1.1);z-index:1}.md-typeset a.md-author:focus img,.md-typeset a.md-author:hover img{filter:grayscale(0)}.md-banner{background-color:var(--md-footer-bg-color);color:var(--md-footer-fg-color);overflow:auto}@media print{.md-banner{display:none}}.md-banner--warning{background-color:var(--md-warning-bg-color);color:var(--md-warning-fg-color)}.md-banner__inner{font-size:.7rem;margin:.6rem auto;padding:0 .8rem}[dir=ltr] .md-banner__button{float:right}[dir=rtl] .md-banner__button{float:left}.md-banner__button{color:inherit;cursor:pointer;transition:opacity .25s}.no-js .md-banner__button{display:none}.md-banner__button:hover{opacity:.7}html{font-size:125%;height:100%;overflow-x:hidden}@media screen and (min-width:100em){html{font-size:137.5%}}@media screen and (min-width:125em){html{font-size:150%}}body{background-color:var(--md-default-bg-color);display:flex;flex-direction:column;font-size:.5rem;min-height:100%;position:relative;width:100%}@media print{body{display:block}}@media screen and (max-width:59.984375em){body[data-md-scrolllock]{position:fixed}}.md-grid{margin-left:auto;margin-right:auto;max-width:61rem}.md-container{display:flex;flex-direction:column;flex-grow:1}@media print{.md-container{display:block}}.md-main{flex-grow:1}.md-main__inner{display:flex;height:100%;margin-top:1.5rem}.md-ellipsis{overflow:hidden;text-overflow:ellipsis}.md-toggle{display:none}.md-option{height:0;opacity:0;position:absolute;width:0}.md-option:checked+label:not([hidden]){display:block}.md-option.focus-visible+label{outline-color:var(--md-accent-fg-color);outline-style:auto}.md-skip{background-color:var(--md-default-fg-color);border-radius:.1rem;color:var(--md-default-bg-color);font-size:.64rem;margin:.5rem;opacity:0;outline-color:var(--md-accent-fg-color);padding:.3rem .5rem;position:fixed;transform:translateY(.4rem);z-index:-1}.md-skip:focus{opacity:1;transform:translateY(0);transition:transform .25s cubic-bezier(.4,0,.2,1),opacity 175ms 75ms;z-index:10}@page{margin:25mm}:root{--md-clipboard-icon:url('data:image/svg+xml;charset=utf-8,')}.md-clipboard{border-radius:.1rem;color:var(--md-default-fg-color--lightest);cursor:pointer;height:1.5em;outline-color:var(--md-accent-fg-color);outline-offset:.1rem;transition:color .25s;width:1.5em;z-index:1}@media print{.md-clipboard{display:none}}.md-clipboard:not(.focus-visible){-webkit-tap-highlight-color:transparent;outline:none}:hover>.md-clipboard{color:var(--md-default-fg-color--light)}.md-clipboard:focus,.md-clipboard:hover{color:var(--md-accent-fg-color)}.md-clipboard:after{background-color:currentcolor;content:"";display:block;height:1.125em;margin:0 auto;-webkit-mask-image:var(--md-clipboard-icon);mask-image:var(--md-clipboard-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:1.125em}.md-clipboard--inline{cursor:pointer}.md-clipboard--inline code{transition:color .25s,background-color .25s}.md-clipboard--inline:focus code,.md-clipboard--inline:hover code{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}:root{--md-code-select-icon:url('data:image/svg+xml;charset=utf-8,');--md-code-copy-icon:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .md-code__content{display:grid}.md-code__nav{background-color:var(--md-code-bg-color--lighter);border-radius:.1rem;display:flex;gap:.2rem;padding:.2rem;position:absolute;right:.25em;top:.25em;transition:background-color .25s;z-index:1}:hover>.md-code__nav{background-color:var(--md-code-bg-color--light)}.md-code__button{color:var(--md-default-fg-color--lightest);cursor:pointer;display:block;height:1.5em;outline-color:var(--md-accent-fg-color);outline-offset:.1rem;transition:color .25s;width:1.5em}:hover>*>.md-code__button{color:var(--md-default-fg-color--light)}.md-code__button.focus-visible,.md-code__button:hover{color:var(--md-accent-fg-color)}.md-code__button--active{color:var(--md-default-fg-color)!important}.md-code__button:after{background-color:currentcolor;content:"";display:block;height:1.125em;margin:0 auto;-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:1.125em}.md-code__button[data-md-type=select]:after{-webkit-mask-image:var(--md-code-select-icon);mask-image:var(--md-code-select-icon)}.md-code__button[data-md-type=copy]:after{-webkit-mask-image:var(--md-code-copy-icon);mask-image:var(--md-code-copy-icon)}@keyframes consent{0%{opacity:0;transform:translateY(100%)}to{opacity:1;transform:translateY(0)}}@keyframes overlay{0%{opacity:0}to{opacity:1}}.md-consent__overlay{animation:overlay .25s both;-webkit-backdrop-filter:blur(.1rem);backdrop-filter:blur(.1rem);background-color:#0000008a;height:100%;opacity:1;position:fixed;top:0;width:100%;z-index:5}.md-consent__inner{animation:consent .5s cubic-bezier(.1,.7,.1,1) both;background-color:var(--md-default-bg-color);border:0;border-radius:.1rem;bottom:0;box-shadow:0 0 .2rem #0000001a,0 .2rem .4rem #0003;max-height:100%;overflow:auto;padding:0;position:fixed;width:100%;z-index:5}.md-consent__form{padding:.8rem}.md-consent__settings{display:none;margin:1em 0}input:checked+.md-consent__settings{display:block}.md-consent__controls{margin-bottom:.8rem}.md-typeset .md-consent__controls .md-button{display:inline}@media screen and (max-width:44.984375em){.md-typeset .md-consent__controls .md-button{display:block;margin-top:.4rem;text-align:center;width:100%}}.md-consent label{cursor:pointer}.md-content{flex-grow:1;min-width:0}.md-content__inner{margin:0 .8rem 1.2rem;padding-top:.6rem}@media screen and (min-width:76.25em){[dir=ltr] .md-sidebar--primary:not([hidden])~.md-content>.md-content__inner{margin-left:1.2rem}[dir=ltr] .md-sidebar--secondary:not([hidden])~.md-content>.md-content__inner,[dir=rtl] .md-sidebar--primary:not([hidden])~.md-content>.md-content__inner{margin-right:1.2rem}[dir=rtl] .md-sidebar--secondary:not([hidden])~.md-content>.md-content__inner{margin-left:1.2rem}}.md-content__inner:before{content:"";display:block;height:.4rem}.md-content__inner>:last-child{margin-bottom:0}[dir=ltr] .md-content__button{float:right}[dir=rtl] .md-content__button{float:left}[dir=ltr] .md-content__button{margin-left:.4rem}[dir=rtl] .md-content__button{margin-right:.4rem}.md-content__button{margin:.4rem 0;padding:0}@media print{.md-content__button{display:none}}.md-typeset .md-content__button{color:var(--md-default-fg-color--lighter)}.md-content__button svg{display:inline;vertical-align:top}[dir=rtl] .md-content__button svg{transform:scaleX(-1)}[dir=ltr] .md-dialog{right:.8rem}[dir=rtl] .md-dialog{left:.8rem}.md-dialog{background-color:var(--md-default-fg-color);border-radius:.1rem;bottom:.8rem;box-shadow:var(--md-shadow-z3);min-width:11.1rem;opacity:0;padding:.4rem .6rem;pointer-events:none;position:fixed;transform:translateY(100%);transition:transform 0ms .4s,opacity .4s;z-index:4}@media print{.md-dialog{display:none}}.md-dialog--active{opacity:1;pointer-events:auto;transform:translateY(0);transition:transform .4s cubic-bezier(.075,.85,.175,1),opacity .4s}.md-dialog__inner{color:var(--md-default-bg-color);font-size:.7rem}.md-feedback{margin:2em 0 1em;text-align:center}.md-feedback fieldset{border:none;margin:0;padding:0}.md-feedback__title{font-weight:700;margin:1em auto}.md-feedback__inner{position:relative}.md-feedback__list{display:flex;flex-wrap:wrap;place-content:baseline center;position:relative}.md-feedback__list:hover .md-icon:not(:disabled){color:var(--md-default-fg-color--lighter)}:disabled .md-feedback__list{min-height:1.8rem}.md-feedback__icon{color:var(--md-default-fg-color--light);cursor:pointer;flex-shrink:0;margin:0 .1rem;transition:color 125ms}.md-feedback__icon:not(:disabled).md-icon:hover{color:var(--md-accent-fg-color)}.md-feedback__icon:disabled{color:var(--md-default-fg-color--lightest);pointer-events:none}.md-feedback__note{opacity:0;position:relative;transform:translateY(.4rem);transition:transform .4s cubic-bezier(.1,.7,.1,1),opacity .15s}.md-feedback__note>*{margin:0 auto;max-width:16rem}:disabled .md-feedback__note{opacity:1;transform:translateY(0)}@media print{.md-feedback{display:none}}.md-footer{background-color:var(--md-footer-bg-color);color:var(--md-footer-fg-color)}@media print{.md-footer{display:none}}.md-footer__inner{justify-content:space-between;overflow:auto;padding:.2rem}.md-footer__inner:not([hidden]){display:flex}.md-footer__link{align-items:end;display:flex;flex-grow:0.01;margin-bottom:.4rem;margin-top:1rem;max-width:100%;outline-color:var(--md-accent-fg-color);overflow:hidden;transition:opacity .25s}.md-footer__link:focus,.md-footer__link:hover{opacity:.7}[dir=rtl] .md-footer__link svg{transform:scaleX(-1)}@media screen and (max-width:44.984375em){.md-footer__link--prev{flex-shrink:0}.md-footer__link--prev .md-footer__title{display:none}}[dir=ltr] .md-footer__link--next{margin-left:auto}[dir=rtl] .md-footer__link--next{margin-right:auto}.md-footer__link--next{text-align:right}[dir=rtl] .md-footer__link--next{text-align:left}.md-footer__title{flex-grow:1;font-size:.9rem;margin-bottom:.7rem;max-width:calc(100% - 2.4rem);padding:0 1rem;white-space:nowrap}.md-footer__button{margin:.2rem;padding:.4rem}.md-footer__direction{font-size:.64rem;opacity:.7}.md-footer-meta{background-color:var(--md-footer-bg-color--dark)}.md-footer-meta__inner{display:flex;flex-wrap:wrap;justify-content:space-between;padding:.2rem}html .md-footer-meta.md-typeset a{color:var(--md-footer-fg-color--light)}html .md-footer-meta.md-typeset a:focus,html .md-footer-meta.md-typeset a:hover{color:var(--md-footer-fg-color)}.md-copyright{color:var(--md-footer-fg-color--lighter);font-size:.64rem;margin:auto .6rem;padding:.4rem 0;width:100%}@media screen and (min-width:45em){.md-copyright{width:auto}}.md-copyright__highlight{color:var(--md-footer-fg-color--light)}.md-social{display:inline-flex;gap:.2rem;margin:0 .4rem;padding:.2rem 0 .6rem}@media screen and (min-width:45em){.md-social{padding:.6rem 0}}.md-social__link{display:inline-block;height:1.6rem;text-align:center;width:1.6rem}.md-social__link:before{line-height:1.9}.md-social__link svg{fill:currentcolor;max-height:.8rem;vertical-align:-25%}.md-typeset .md-button{border:.1rem solid;border-radius:.1rem;color:var(--md-primary-fg-color);cursor:pointer;display:inline-block;font-weight:700;padding:.625em 2em;transition:color 125ms,background-color 125ms,border-color 125ms}.md-typeset .md-button--primary{background-color:var(--md-primary-fg-color);border-color:var(--md-primary-fg-color);color:var(--md-primary-bg-color)}.md-typeset .md-button:focus,.md-typeset .md-button:hover{background-color:var(--md-accent-fg-color);border-color:var(--md-accent-fg-color);color:var(--md-accent-bg-color)}[dir=ltr] .md-typeset .md-input{border-top-left-radius:.1rem}[dir=ltr] .md-typeset .md-input,[dir=rtl] .md-typeset .md-input{border-top-right-radius:.1rem}[dir=rtl] .md-typeset .md-input{border-top-left-radius:.1rem}.md-typeset .md-input{border-bottom:.1rem solid var(--md-default-fg-color--lighter);box-shadow:var(--md-shadow-z1);font-size:.8rem;height:1.8rem;padding:0 .6rem;transition:border .25s,box-shadow .25s}.md-typeset .md-input:focus,.md-typeset .md-input:hover{border-bottom-color:var(--md-accent-fg-color);box-shadow:var(--md-shadow-z2)}.md-typeset .md-input--stretch{width:100%}.md-header{background-color:var(--md-primary-fg-color);box-shadow:0 0 .2rem #0000,0 .2rem .4rem #0000;color:var(--md-primary-bg-color);display:block;left:0;position:sticky;right:0;top:0;z-index:4}@media print{.md-header{display:none}}.md-header[hidden]{transform:translateY(-100%);transition:transform .25s cubic-bezier(.8,0,.6,1),box-shadow .25s}.md-header--shadow{box-shadow:0 0 .2rem #0000001a,0 .2rem .4rem #0003;transition:transform .25s cubic-bezier(.1,.7,.1,1),box-shadow .25s}.md-header__inner{align-items:center;display:flex;padding:0 .2rem}.md-header__button{color:currentcolor;cursor:pointer;margin:.2rem;outline-color:var(--md-accent-fg-color);padding:.4rem;position:relative;transition:opacity .25s;vertical-align:middle;z-index:1}.md-header__button:hover{opacity:.7}.md-header__button:not([hidden]){display:inline-block}.md-header__button:not(.focus-visible){-webkit-tap-highlight-color:transparent;outline:none}.md-header__button.md-logo{margin:.2rem;padding:.4rem}@media screen and (max-width:76.234375em){.md-header__button.md-logo{display:none}}.md-header__button.md-logo img,.md-header__button.md-logo svg{fill:currentcolor;display:block;height:1.2rem;width:auto}@media screen and (min-width:60em){.md-header__button[for=__search]{display:none}}.no-js .md-header__button[for=__search]{display:none}[dir=rtl] .md-header__button[for=__search] svg{transform:scaleX(-1)}@media screen and (min-width:76.25em){.md-header__button[for=__drawer]{display:none}}.md-header__topic{display:flex;max-width:100%;position:absolute;transition:transform .4s cubic-bezier(.1,.7,.1,1),opacity .15s;white-space:nowrap}.md-header__topic+.md-header__topic{opacity:0;pointer-events:none;transform:translateX(1.25rem);transition:transform .4s cubic-bezier(1,.7,.1,.1),opacity .15s;z-index:-1}[dir=rtl] .md-header__topic+.md-header__topic{transform:translateX(-1.25rem)}.md-header__topic:first-child{font-weight:700}[dir=ltr] .md-header__title{margin-left:1rem;margin-right:.4rem}[dir=rtl] .md-header__title{margin-left:.4rem;margin-right:1rem}.md-header__title{flex-grow:1;font-size:.9rem;height:2.4rem;line-height:2.4rem}.md-header__title--active .md-header__topic{opacity:0;pointer-events:none;transform:translateX(-1.25rem);transition:transform .4s cubic-bezier(1,.7,.1,.1),opacity .15s;z-index:-1}[dir=rtl] .md-header__title--active .md-header__topic{transform:translateX(1.25rem)}.md-header__title--active .md-header__topic+.md-header__topic{opacity:1;pointer-events:auto;transform:translateX(0);transition:transform .4s cubic-bezier(.1,.7,.1,1),opacity .15s;z-index:0}.md-header__title>.md-header__ellipsis{height:100%;position:relative;width:100%}.md-header__option{display:flex;flex-shrink:0;max-width:100%;transition:max-width 0ms .25s,opacity .25s .25s;white-space:nowrap}[data-md-toggle=search]:checked~.md-header .md-header__option{max-width:0;opacity:0;transition:max-width 0ms,opacity 0ms}.md-header__option>input{bottom:0}.md-header__source{display:none}@media screen and (min-width:60em){[dir=ltr] .md-header__source{margin-left:1rem}[dir=rtl] .md-header__source{margin-right:1rem}.md-header__source{display:block;max-width:11.7rem;width:11.7rem}}@media screen and (min-width:76.25em){[dir=ltr] .md-header__source{margin-left:1.4rem}[dir=rtl] .md-header__source{margin-right:1.4rem}}.md-meta{color:var(--md-default-fg-color--light);font-size:.7rem;line-height:1.3}.md-meta__list{display:inline-flex;flex-wrap:wrap;list-style:none;margin:0;padding:0}.md-meta__item:not(:last-child):after{content:"·";margin-left:.2rem;margin-right:.2rem}.md-meta__link{color:var(--md-typeset-a-color)}.md-meta__link:focus,.md-meta__link:hover{color:var(--md-accent-fg-color)}.md-draft{background-color:#ff1744;border-radius:.125em;color:#fff;display:inline-block;font-weight:700;padding-left:.5714285714em;padding-right:.5714285714em}:root{--md-nav-icon--prev:url('data:image/svg+xml;charset=utf-8,');--md-nav-icon--next:url('data:image/svg+xml;charset=utf-8,');--md-toc-icon:url('data:image/svg+xml;charset=utf-8,')}.md-nav{font-size:.7rem;line-height:1.3}.md-nav__title{color:var(--md-default-fg-color--light);display:block;font-weight:700;overflow:hidden;padding:0 .6rem;text-overflow:ellipsis}.md-nav__title .md-nav__button{display:none}.md-nav__title .md-nav__button img{height:100%;width:auto}.md-nav__title .md-nav__button.md-logo img,.md-nav__title .md-nav__button.md-logo svg{fill:currentcolor;display:block;height:2.4rem;max-width:100%;object-fit:contain;width:auto}.md-nav__list{list-style:none;margin:0;padding:0}.md-nav__link{align-items:flex-start;display:flex;gap:.4rem;margin-top:.625em;scroll-snap-align:start;transition:color 125ms}.md-nav__link--passed,.md-nav__link--passed code{color:var(--md-default-fg-color--light)}.md-nav__item .md-nav__link--active,.md-nav__item .md-nav__link--active code{color:var(--md-typeset-a-color)}.md-nav__link .md-ellipsis{position:relative}.md-nav__link .md-ellipsis code{word-break:normal}[dir=ltr] .md-nav__link .md-icon:last-child{margin-left:auto}[dir=rtl] .md-nav__link .md-icon:last-child{margin-right:auto}.md-nav__link .md-typeset{font-size:.7rem;line-height:1.3}.md-nav__link svg{fill:currentcolor;flex-shrink:0;height:1.3em;position:relative}.md-nav__link[for]:focus,.md-nav__link[for]:hover,.md-nav__link[href]:focus,.md-nav__link[href]:hover{color:var(--md-accent-fg-color);cursor:pointer}.md-nav__link[for]:focus code,.md-nav__link[for]:hover code,.md-nav__link[href]:focus code,.md-nav__link[href]:hover code{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}.md-nav__link.focus-visible{outline-color:var(--md-accent-fg-color);outline-offset:.2rem}.md-nav--primary .md-nav__link[for=__toc]{display:none}.md-nav--primary .md-nav__link[for=__toc] .md-icon:after{background-color:currentcolor;display:block;height:100%;-webkit-mask-image:var(--md-toc-icon);mask-image:var(--md-toc-icon);width:100%}.md-nav--primary .md-nav__link[for=__toc]~.md-nav{display:none}.md-nav__container>.md-nav__link{margin-top:0}.md-nav__container>.md-nav__link:first-child{flex-grow:1;min-width:0}.md-nav__icon{flex-shrink:0}.md-nav__source{display:none}@media screen and (max-width:76.234375em){.md-nav--primary,.md-nav--primary .md-nav{background-color:var(--md-default-bg-color);display:flex;flex-direction:column;height:100%;left:0;position:absolute;right:0;top:0;z-index:1}.md-nav--primary .md-nav__item,.md-nav--primary .md-nav__title{font-size:.8rem;line-height:1.5}.md-nav--primary .md-nav__title{background-color:var(--md-default-fg-color--lightest);color:var(--md-default-fg-color--light);cursor:pointer;height:5.6rem;line-height:2.4rem;padding:3rem .8rem .2rem;position:relative;white-space:nowrap}[dir=ltr] .md-nav--primary .md-nav__title .md-nav__icon{left:.4rem}[dir=rtl] .md-nav--primary .md-nav__title .md-nav__icon{right:.4rem}.md-nav--primary .md-nav__title .md-nav__icon{display:block;height:1.2rem;margin:.2rem;position:absolute;top:.4rem;width:1.2rem}.md-nav--primary .md-nav__title .md-nav__icon:after{background-color:currentcolor;content:"";display:block;height:100%;-webkit-mask-image:var(--md-nav-icon--prev);mask-image:var(--md-nav-icon--prev);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:100%}.md-nav--primary .md-nav__title~.md-nav__list{background-color:var(--md-default-bg-color);box-shadow:0 .05rem 0 var(--md-default-fg-color--lightest) inset;overflow-y:auto;scroll-snap-type:y mandatory;touch-action:pan-y}.md-nav--primary .md-nav__title~.md-nav__list>:first-child{border-top:0}.md-nav--primary .md-nav__title[for=__drawer]{background-color:var(--md-primary-fg-color);color:var(--md-primary-bg-color);font-weight:700}.md-nav--primary .md-nav__title .md-logo{display:block;left:.2rem;margin:.2rem;padding:.4rem;position:absolute;right:.2rem;top:.2rem}.md-nav--primary .md-nav__list{flex:1}.md-nav--primary .md-nav__item{border-top:.05rem solid var(--md-default-fg-color--lightest)}.md-nav--primary .md-nav__item--active>.md-nav__link{color:var(--md-typeset-a-color)}.md-nav--primary .md-nav__item--active>.md-nav__link:focus,.md-nav--primary .md-nav__item--active>.md-nav__link:hover{color:var(--md-accent-fg-color)}.md-nav--primary .md-nav__link{margin-top:0;padding:.6rem .8rem}.md-nav--primary .md-nav__link svg{margin-top:.1em}.md-nav--primary .md-nav__link>.md-nav__link{padding:0}[dir=ltr] .md-nav--primary .md-nav__link .md-nav__icon{margin-right:-.2rem}[dir=rtl] .md-nav--primary .md-nav__link .md-nav__icon{margin-left:-.2rem}.md-nav--primary .md-nav__link .md-nav__icon{font-size:1.2rem;height:1.2rem;width:1.2rem}.md-nav--primary .md-nav__link .md-nav__icon:after{background-color:currentcolor;content:"";display:block;height:100%;-webkit-mask-image:var(--md-nav-icon--next);mask-image:var(--md-nav-icon--next);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:100%}[dir=rtl] .md-nav--primary .md-nav__icon:after{transform:scale(-1)}.md-nav--primary .md-nav--secondary .md-nav{background-color:initial;position:static}[dir=ltr] .md-nav--primary .md-nav--secondary .md-nav .md-nav__link{padding-left:1.4rem}[dir=rtl] .md-nav--primary .md-nav--secondary .md-nav .md-nav__link{padding-right:1.4rem}[dir=ltr] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav__link{padding-left:2rem}[dir=rtl] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav__link{padding-right:2rem}[dir=ltr] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav .md-nav__link{padding-left:2.6rem}[dir=rtl] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav .md-nav__link{padding-right:2.6rem}[dir=ltr] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav .md-nav .md-nav__link{padding-left:3.2rem}[dir=rtl] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav .md-nav .md-nav__link{padding-right:3.2rem}.md-nav--secondary{background-color:initial}.md-nav__toggle~.md-nav{display:flex;opacity:0;transform:translateX(100%);transition:transform .25s cubic-bezier(.8,0,.6,1),opacity 125ms 50ms}[dir=rtl] .md-nav__toggle~.md-nav{transform:translateX(-100%)}.md-nav__toggle:checked~.md-nav{opacity:1;transform:translateX(0);transition:transform .25s cubic-bezier(.4,0,.2,1),opacity 125ms 125ms}.md-nav__toggle:checked~.md-nav>.md-nav__list{backface-visibility:hidden}}@media screen and (max-width:59.984375em){.md-nav--primary .md-nav__link[for=__toc]{display:flex}.md-nav--primary .md-nav__link[for=__toc] .md-icon:after{content:""}.md-nav--primary .md-nav__link[for=__toc]+.md-nav__link{display:none}.md-nav--primary .md-nav__link[for=__toc]~.md-nav{display:flex}.md-nav__source{background-color:var(--md-primary-fg-color--dark);color:var(--md-primary-bg-color);display:block;padding:0 .2rem}}@media screen and (min-width:60em) and (max-width:76.234375em){.md-nav--integrated .md-nav__link[for=__toc]{display:flex}.md-nav--integrated .md-nav__link[for=__toc] .md-icon:after{content:""}.md-nav--integrated .md-nav__link[for=__toc]+.md-nav__link{display:none}.md-nav--integrated .md-nav__link[for=__toc]~.md-nav{display:flex}}@media screen and (min-width:60em){.md-nav{margin-bottom:-.4rem}.md-nav--secondary .md-nav__title{background:var(--md-default-bg-color);box-shadow:0 0 .4rem .4rem var(--md-default-bg-color);position:sticky;top:0;z-index:1}.md-nav--secondary .md-nav__title[for=__toc]{scroll-snap-align:start}.md-nav--secondary .md-nav__title .md-nav__icon{display:none}[dir=ltr] .md-nav--secondary .md-nav__list{padding-left:.6rem}[dir=rtl] .md-nav--secondary .md-nav__list{padding-right:.6rem}.md-nav--secondary .md-nav__list{padding-bottom:.4rem}[dir=ltr] .md-nav--secondary .md-nav__item>.md-nav__link{margin-right:.4rem}[dir=rtl] .md-nav--secondary .md-nav__item>.md-nav__link{margin-left:.4rem}}@media screen and (min-width:76.25em){.md-nav{margin-bottom:-.4rem;transition:max-height .25s cubic-bezier(.86,0,.07,1)}.md-nav--primary .md-nav__title{background:var(--md-default-bg-color);box-shadow:0 0 .4rem .4rem var(--md-default-bg-color);position:sticky;top:0;z-index:1}.md-nav--primary .md-nav__title[for=__drawer]{scroll-snap-align:start}.md-nav--primary .md-nav__title .md-nav__icon{display:none}[dir=ltr] .md-nav--primary .md-nav__list{padding-left:.6rem}[dir=rtl] .md-nav--primary .md-nav__list{padding-right:.6rem}.md-nav--primary .md-nav__list{padding-bottom:.4rem}[dir=ltr] .md-nav--primary .md-nav__item>.md-nav__link{margin-right:.4rem}[dir=rtl] .md-nav--primary .md-nav__item>.md-nav__link{margin-left:.4rem}.md-nav__toggle~.md-nav{display:grid;grid-template-rows:minmax(.4rem,0fr);opacity:0;transition:grid-template-rows .25s cubic-bezier(.86,0,.07,1),opacity .25s,visibility 0ms .25s;visibility:collapse}.md-nav__toggle~.md-nav>.md-nav__list{overflow:hidden}.md-nav__toggle.md-toggle--indeterminate~.md-nav,.md-nav__toggle:checked~.md-nav{grid-template-rows:minmax(.4rem,1fr);opacity:1;transition:grid-template-rows .25s cubic-bezier(.86,0,.07,1),opacity .15s .1s,visibility 0ms;visibility:visible}.md-nav__toggle.md-toggle--indeterminate~.md-nav{transition:none}.md-nav__item--nested>.md-nav>.md-nav__title{display:none}.md-nav__item--section{display:block;margin:1.25em 0}.md-nav__item--section:last-child{margin-bottom:0}.md-nav__item--section>.md-nav__link{font-weight:700}.md-nav__item--section>.md-nav__link[for]{color:var(--md-default-fg-color--light)}.md-nav__item--section>.md-nav__link:not(.md-nav__container){pointer-events:none}.md-nav__item--section>.md-nav__link .md-icon,.md-nav__item--section>.md-nav__link>[for]{display:none}[dir=ltr] .md-nav__item--section>.md-nav{margin-left:-.6rem}[dir=rtl] .md-nav__item--section>.md-nav{margin-right:-.6rem}.md-nav__item--section>.md-nav{display:block;opacity:1;visibility:visible}.md-nav__item--section>.md-nav>.md-nav__list>.md-nav__item{padding:0}.md-nav__icon{border-radius:100%;height:.9rem;transition:background-color .25s;width:.9rem}.md-nav__icon:hover{background-color:var(--md-accent-fg-color--transparent)}.md-nav__icon:after{background-color:currentcolor;border-radius:100%;content:"";display:inline-block;height:100%;-webkit-mask-image:var(--md-nav-icon--next);mask-image:var(--md-nav-icon--next);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;transition:transform .25s;vertical-align:-.1rem;width:100%}[dir=rtl] .md-nav__icon:after{transform:rotate(180deg)}.md-nav__item--nested .md-nav__toggle:checked~.md-nav__link .md-nav__icon:after,.md-nav__item--nested .md-toggle--indeterminate~.md-nav__link .md-nav__icon:after{transform:rotate(90deg)}.md-nav--lifted>.md-nav__list>.md-nav__item,.md-nav--lifted>.md-nav__title{display:none}.md-nav--lifted>.md-nav__list>.md-nav__item--active{display:block}.md-nav--lifted>.md-nav__list>.md-nav__item--active>.md-nav__link{background:var(--md-default-bg-color);box-shadow:0 0 .4rem .4rem var(--md-default-bg-color);margin-top:0;position:sticky;top:0;z-index:1}.md-nav--lifted>.md-nav__list>.md-nav__item--active>.md-nav__link:not(.md-nav__container){pointer-events:none}.md-nav--lifted>.md-nav__list>.md-nav__item--active.md-nav__item--section{margin:0}[dir=ltr] .md-nav--lifted>.md-nav__list>.md-nav__item>.md-nav:not(.md-nav--secondary){margin-left:-.6rem}[dir=rtl] .md-nav--lifted>.md-nav__list>.md-nav__item>.md-nav:not(.md-nav--secondary){margin-right:-.6rem}.md-nav--lifted>.md-nav__list>.md-nav__item>[for]{color:var(--md-default-fg-color--light)}.md-nav--lifted .md-nav[data-md-level="1"]{grid-template-rows:minmax(.4rem,1fr);opacity:1;visibility:visible}[dir=ltr] .md-nav--integrated>.md-nav__list>.md-nav__item--active .md-nav--secondary{border-left:.05rem solid var(--md-primary-fg-color)}[dir=rtl] .md-nav--integrated>.md-nav__list>.md-nav__item--active .md-nav--secondary{border-right:.05rem solid var(--md-primary-fg-color)}.md-nav--integrated>.md-nav__list>.md-nav__item--active .md-nav--secondary{display:block;margin-bottom:1.25em;opacity:1;visibility:visible}.md-nav--integrated>.md-nav__list>.md-nav__item--active .md-nav--secondary>.md-nav__list{overflow:visible;padding-bottom:0}.md-nav--integrated>.md-nav__list>.md-nav__item--active .md-nav--secondary>.md-nav__title{display:none}}.md-pagination{font-size:.8rem;font-weight:700;gap:.4rem}.md-pagination,.md-pagination>*{align-items:center;display:flex;justify-content:center}.md-pagination>*{border-radius:.2rem;height:1.8rem;min-width:1.8rem;text-align:center}.md-pagination__current{background-color:var(--md-default-fg-color--lightest);color:var(--md-default-fg-color--light)}.md-pagination__link{transition:color 125ms,background-color 125ms}.md-pagination__link:focus,.md-pagination__link:hover{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}.md-pagination__link:focus svg,.md-pagination__link:hover svg{color:var(--md-accent-fg-color)}.md-pagination__link.focus-visible{outline-color:var(--md-accent-fg-color);outline-offset:.2rem}.md-pagination__link svg{fill:currentcolor;color:var(--md-default-fg-color--lighter);display:block;max-height:100%;width:1.2rem}:root{--md-path-icon:url('data:image/svg+xml;charset=utf-8,')}.md-path{font-size:.7rem;margin:0 .8rem;overflow:auto;padding-top:1.2rem}.md-path:not([hidden]){display:block}@media screen and (min-width:76.25em){.md-path{margin:0 1.2rem}}.md-path__list{align-items:center;display:flex;gap:.2rem;list-style:none;margin:0;padding:0}.md-path__item:not(:first-child){display:inline-flex;gap:.2rem;white-space:nowrap}.md-path__item:not(:first-child):before{background-color:var(--md-default-fg-color--lighter);content:"";display:inline;height:.8rem;-webkit-mask-image:var(--md-path-icon);mask-image:var(--md-path-icon);width:.8rem}.md-path__link{align-items:center;color:var(--md-default-fg-color--light);display:flex}.md-path__link:focus,.md-path__link:hover{color:var(--md-accent-fg-color)}:root{--md-post-pin-icon:url('data:image/svg+xml;charset=utf-8,')}.md-post__back{border-bottom:.05rem solid var(--md-default-fg-color--lightest);margin-bottom:1.2rem;padding-bottom:1.2rem}@media screen and (max-width:76.234375em){.md-post__back{display:none}}[dir=rtl] .md-post__back svg{transform:scaleX(-1)}.md-post__authors{display:flex;flex-direction:column;gap:.6rem;margin:0 .6rem 1.2rem}.md-post .md-post__meta a{transition:color 125ms}.md-post .md-post__meta a:focus,.md-post .md-post__meta a:hover{color:var(--md-accent-fg-color)}.md-post__title{color:var(--md-default-fg-color--light);font-weight:700}.md-post--excerpt{margin-bottom:3.2rem}.md-post--excerpt .md-post__header{align-items:center;display:flex;gap:.6rem;min-height:1.6rem}.md-post--excerpt .md-post__authors{align-items:center;display:inline-flex;flex-direction:row;gap:.2rem;margin:0;min-height:2.4rem}[dir=ltr] .md-post--excerpt .md-post__meta .md-meta__list{margin-right:.4rem}[dir=rtl] .md-post--excerpt .md-post__meta .md-meta__list{margin-left:.4rem}.md-post--excerpt .md-post__content>:first-child{--md-scroll-margin:6rem;margin-top:0}.md-post>.md-nav--secondary{margin:1em 0}.md-pin{background:var(--md-default-fg-color--lightest);border-radius:1rem;margin-top:-.05rem;padding:.2rem}.md-pin:after{background-color:currentcolor;content:"";display:block;height:.6rem;margin:0 auto;-webkit-mask-image:var(--md-post-pin-icon);mask-image:var(--md-post-pin-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:.6rem}.md-profile{align-items:center;display:flex;font-size:.7rem;gap:.6rem;line-height:1.4;width:100%}.md-profile__description{flex-grow:1}.md-content--post{display:flex}@media screen and (max-width:76.234375em){.md-content--post{flex-flow:column-reverse}}.md-content--post>.md-content__inner{flex-grow:1;min-width:0}@media screen and (min-width:76.25em){[dir=ltr] .md-content--post>.md-content__inner{margin-left:1.2rem}[dir=rtl] .md-content--post>.md-content__inner{margin-right:1.2rem}}@media screen and (max-width:76.234375em){.md-sidebar.md-sidebar--post{padding:0;position:static;width:100%}.md-sidebar.md-sidebar--post .md-sidebar__scrollwrap{overflow:visible}.md-sidebar.md-sidebar--post .md-sidebar__inner{padding:0}.md-sidebar.md-sidebar--post .md-post__meta{margin-left:.6rem;margin-right:.6rem}.md-sidebar.md-sidebar--post .md-nav__item{border:none;display:inline}.md-sidebar.md-sidebar--post .md-nav__list{display:inline-flex;flex-wrap:wrap;gap:.6rem;padding-bottom:.6rem;padding-top:.6rem}.md-sidebar.md-sidebar--post .md-nav__link{padding:0}.md-sidebar.md-sidebar--post .md-nav{height:auto;margin-bottom:0;position:static}}:root{--md-progress-value:0;--md-progress-delay:400ms}.md-progress{background:var(--md-primary-bg-color);height:.075rem;opacity:min(clamp(0,var(--md-progress-value),1),clamp(0,100 - var(--md-progress-value),1));position:fixed;top:0;transform:scaleX(calc(var(--md-progress-value)*1%));transform-origin:left;transition:transform .5s cubic-bezier(.19,1,.22,1),opacity .25s var(--md-progress-delay);width:100%;z-index:4}:root{--md-search-result-icon:url('data:image/svg+xml;charset=utf-8,')}.md-search{position:relative}@media screen and (min-width:60em){.md-search{padding:.2rem 0}}.no-js .md-search{display:none}.md-search__overlay{opacity:0;z-index:1}@media screen and (max-width:59.984375em){[dir=ltr] .md-search__overlay{left:-2.2rem}[dir=rtl] .md-search__overlay{right:-2.2rem}.md-search__overlay{background-color:var(--md-default-bg-color);border-radius:1rem;height:2rem;overflow:hidden;pointer-events:none;position:absolute;top:-1rem;transform-origin:center;transition:transform .3s .1s,opacity .2s .2s;width:2rem}[data-md-toggle=search]:checked~.md-header .md-search__overlay{opacity:1;transition:transform .4s,opacity .1s}}@media screen and (min-width:60em){[dir=ltr] .md-search__overlay{left:0}[dir=rtl] .md-search__overlay{right:0}.md-search__overlay{background-color:#0000008a;cursor:pointer;height:0;position:fixed;top:0;transition:width 0ms .25s,height 0ms .25s,opacity .25s;width:0}[data-md-toggle=search]:checked~.md-header .md-search__overlay{height:200vh;opacity:1;transition:width 0ms,height 0ms,opacity .25s;width:100%}}@media screen and (max-width:29.984375em){[data-md-toggle=search]:checked~.md-header .md-search__overlay{transform:scale(45)}}@media screen and (min-width:30em) and (max-width:44.984375em){[data-md-toggle=search]:checked~.md-header .md-search__overlay{transform:scale(60)}}@media screen and (min-width:45em) and (max-width:59.984375em){[data-md-toggle=search]:checked~.md-header .md-search__overlay{transform:scale(75)}}.md-search__inner{backface-visibility:hidden}@media screen and (max-width:59.984375em){[dir=ltr] .md-search__inner{left:0}[dir=rtl] .md-search__inner{right:0}.md-search__inner{height:0;opacity:0;overflow:hidden;position:fixed;top:0;transform:translateX(5%);transition:width 0ms .3s,height 0ms .3s,transform .15s cubic-bezier(.4,0,.2,1) .15s,opacity .15s .15s;width:0;z-index:2}[dir=rtl] .md-search__inner{transform:translateX(-5%)}[data-md-toggle=search]:checked~.md-header .md-search__inner{height:100%;opacity:1;transform:translateX(0);transition:width 0ms 0ms,height 0ms 0ms,transform .15s cubic-bezier(.1,.7,.1,1) .15s,opacity .15s .15s;width:100%}}@media screen and (min-width:60em){[dir=ltr] .md-search__inner{float:right}[dir=rtl] .md-search__inner{float:left}.md-search__inner{padding:.1rem 0;position:relative;transition:width .25s cubic-bezier(.1,.7,.1,1);width:11.7rem}}@media screen and (min-width:60em) and (max-width:76.234375em){[data-md-toggle=search]:checked~.md-header .md-search__inner{width:23.4rem}}@media screen and (min-width:76.25em){[data-md-toggle=search]:checked~.md-header .md-search__inner{width:34.4rem}}.md-search__form{background-color:var(--md-default-bg-color);box-shadow:0 0 .6rem #0000;height:2.4rem;position:relative;transition:color .25s,background-color .25s;z-index:2}@media screen and (min-width:60em){.md-search__form{background-color:#00000042;border-radius:.1rem;height:1.8rem}.md-search__form:hover{background-color:#ffffff1f}}[data-md-toggle=search]:checked~.md-header .md-search__form{background-color:var(--md-default-bg-color);border-radius:.1rem .1rem 0 0;box-shadow:0 0 .6rem #00000012;color:var(--md-default-fg-color)}[dir=ltr] .md-search__input{padding-left:3.6rem;padding-right:2.2rem}[dir=rtl] .md-search__input{padding-left:2.2rem;padding-right:3.6rem}.md-search__input{background:#0000;font-size:.9rem;height:100%;position:relative;text-overflow:ellipsis;width:100%;z-index:2}.md-search__input::placeholder{transition:color .25s}.md-search__input::placeholder,.md-search__input~.md-search__icon{color:var(--md-default-fg-color--light)}.md-search__input::-ms-clear{display:none}@media screen and (max-width:59.984375em){.md-search__input{font-size:.9rem;height:2.4rem;width:100%}}@media screen and (min-width:60em){[dir=ltr] .md-search__input{padding-left:2.2rem}[dir=rtl] .md-search__input{padding-right:2.2rem}.md-search__input{color:inherit;font-size:.8rem}.md-search__input::placeholder{color:var(--md-primary-bg-color--light)}.md-search__input+.md-search__icon{color:var(--md-primary-bg-color)}[data-md-toggle=search]:checked~.md-header .md-search__input{text-overflow:clip}[data-md-toggle=search]:checked~.md-header .md-search__input+.md-search__icon{color:var(--md-default-fg-color--light)}[data-md-toggle=search]:checked~.md-header .md-search__input::placeholder{color:#0000}}.md-search__icon{cursor:pointer;display:inline-block;height:1.2rem;transition:color .25s,opacity .25s;width:1.2rem}.md-search__icon:hover{opacity:.7}[dir=ltr] .md-search__icon[for=__search]{left:.5rem}[dir=rtl] .md-search__icon[for=__search]{right:.5rem}.md-search__icon[for=__search]{position:absolute;top:.3rem;z-index:2}[dir=rtl] .md-search__icon[for=__search] svg{transform:scaleX(-1)}@media screen and (max-width:59.984375em){[dir=ltr] .md-search__icon[for=__search]{left:.8rem}[dir=rtl] .md-search__icon[for=__search]{right:.8rem}.md-search__icon[for=__search]{top:.6rem}.md-search__icon[for=__search] svg:first-child{display:none}}@media screen and (min-width:60em){.md-search__icon[for=__search]{pointer-events:none}.md-search__icon[for=__search] svg:last-child{display:none}}[dir=ltr] .md-search__options{right:.5rem}[dir=rtl] .md-search__options{left:.5rem}.md-search__options{pointer-events:none;position:absolute;top:.3rem;z-index:2}@media screen and (max-width:59.984375em){[dir=ltr] .md-search__options{right:.8rem}[dir=rtl] .md-search__options{left:.8rem}.md-search__options{top:.6rem}}[dir=ltr] .md-search__options>.md-icon{margin-left:.2rem}[dir=rtl] .md-search__options>.md-icon{margin-right:.2rem}.md-search__options>.md-icon{color:var(--md-default-fg-color--light);opacity:0;transform:scale(.75);transition:transform .15s cubic-bezier(.1,.7,.1,1),opacity .15s}.md-search__options>.md-icon:not(.focus-visible){-webkit-tap-highlight-color:transparent;outline:none}[data-md-toggle=search]:checked~.md-header .md-search__input:valid~.md-search__options>.md-icon{opacity:1;pointer-events:auto;transform:scale(1)}[data-md-toggle=search]:checked~.md-header .md-search__input:valid~.md-search__options>.md-icon:hover{opacity:.7}[dir=ltr] .md-search__suggest{padding-left:3.6rem;padding-right:2.2rem}[dir=rtl] .md-search__suggest{padding-left:2.2rem;padding-right:3.6rem}.md-search__suggest{align-items:center;color:var(--md-default-fg-color--lighter);display:flex;font-size:.9rem;height:100%;opacity:0;position:absolute;top:0;transition:opacity 50ms;white-space:nowrap;width:100%}@media screen and (min-width:60em){[dir=ltr] .md-search__suggest{padding-left:2.2rem}[dir=rtl] .md-search__suggest{padding-right:2.2rem}.md-search__suggest{font-size:.8rem}}[data-md-toggle=search]:checked~.md-header .md-search__suggest{opacity:1;transition:opacity .3s .1s}[dir=ltr] .md-search__output{border-bottom-left-radius:.1rem}[dir=ltr] .md-search__output,[dir=rtl] .md-search__output{border-bottom-right-radius:.1rem}[dir=rtl] .md-search__output{border-bottom-left-radius:.1rem}.md-search__output{overflow:hidden;position:absolute;width:100%;z-index:1}@media screen and (max-width:59.984375em){.md-search__output{bottom:0;top:2.4rem}}@media screen and (min-width:60em){.md-search__output{opacity:0;top:1.9rem;transition:opacity .4s}[data-md-toggle=search]:checked~.md-header .md-search__output{box-shadow:var(--md-shadow-z3);opacity:1}}.md-search__scrollwrap{backface-visibility:hidden;background-color:var(--md-default-bg-color);height:100%;overflow-y:auto;touch-action:pan-y}@media (-webkit-max-device-pixel-ratio:1),(max-resolution:1dppx){.md-search__scrollwrap{transform:translateZ(0)}}@media screen and (min-width:60em) and (max-width:76.234375em){.md-search__scrollwrap{width:23.4rem}}@media screen and (min-width:76.25em){.md-search__scrollwrap{width:34.4rem}}@media screen and (min-width:60em){.md-search__scrollwrap{max-height:0;scrollbar-color:var(--md-default-fg-color--lighter) #0000;scrollbar-width:thin}[data-md-toggle=search]:checked~.md-header .md-search__scrollwrap{max-height:75vh}.md-search__scrollwrap:hover{scrollbar-color:var(--md-accent-fg-color) #0000}.md-search__scrollwrap::-webkit-scrollbar{height:.2rem;width:.2rem}.md-search__scrollwrap::-webkit-scrollbar-thumb{background-color:var(--md-default-fg-color--lighter)}.md-search__scrollwrap::-webkit-scrollbar-thumb:hover{background-color:var(--md-accent-fg-color)}}.md-search-result{color:var(--md-default-fg-color);word-break:break-word}.md-search-result__meta{background-color:var(--md-default-fg-color--lightest);color:var(--md-default-fg-color--light);font-size:.64rem;line-height:1.8rem;padding:0 .8rem;scroll-snap-align:start}@media screen and (min-width:60em){[dir=ltr] .md-search-result__meta{padding-left:2.2rem}[dir=rtl] .md-search-result__meta{padding-right:2.2rem}}.md-search-result__list{list-style:none;margin:0;padding:0;-webkit-user-select:none;user-select:none}.md-search-result__item{box-shadow:0 -.05rem var(--md-default-fg-color--lightest)}.md-search-result__item:first-child{box-shadow:none}.md-search-result__link{display:block;outline:none;scroll-snap-align:start;transition:background-color .25s}.md-search-result__link:focus,.md-search-result__link:hover{background-color:var(--md-accent-fg-color--transparent)}.md-search-result__link:last-child p:last-child{margin-bottom:.6rem}.md-search-result__more>summary{cursor:pointer;display:block;outline:none;position:sticky;scroll-snap-align:start;top:0;z-index:1}.md-search-result__more>summary::marker{display:none}.md-search-result__more>summary::-webkit-details-marker{display:none}.md-search-result__more>summary>div{color:var(--md-typeset-a-color);font-size:.64rem;padding:.75em .8rem;transition:color .25s,background-color .25s}@media screen and (min-width:60em){[dir=ltr] .md-search-result__more>summary>div{padding-left:2.2rem}[dir=rtl] .md-search-result__more>summary>div{padding-right:2.2rem}}.md-search-result__more>summary:focus>div,.md-search-result__more>summary:hover>div{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}.md-search-result__more[open]>summary{background-color:var(--md-default-bg-color)}.md-search-result__article{overflow:hidden;padding:0 .8rem;position:relative}@media screen and (min-width:60em){[dir=ltr] .md-search-result__article{padding-left:2.2rem}[dir=rtl] .md-search-result__article{padding-right:2.2rem}}[dir=ltr] .md-search-result__icon{left:0}[dir=rtl] .md-search-result__icon{right:0}.md-search-result__icon{color:var(--md-default-fg-color--light);height:1.2rem;margin:.5rem;position:absolute;width:1.2rem}@media screen and (max-width:59.984375em){.md-search-result__icon{display:none}}.md-search-result__icon:after{background-color:currentcolor;content:"";display:inline-block;height:100%;-webkit-mask-image:var(--md-search-result-icon);mask-image:var(--md-search-result-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:100%}[dir=rtl] .md-search-result__icon:after{transform:scaleX(-1)}.md-search-result .md-typeset{color:var(--md-default-fg-color--light);font-size:.64rem;line-height:1.6}.md-search-result .md-typeset h1{color:var(--md-default-fg-color);font-size:.8rem;font-weight:400;line-height:1.4;margin:.55rem 0}.md-search-result .md-typeset h1 mark{text-decoration:none}.md-search-result .md-typeset h2{color:var(--md-default-fg-color);font-size:.64rem;font-weight:700;line-height:1.6;margin:.5em 0}.md-search-result .md-typeset h2 mark{text-decoration:none}.md-search-result__terms{color:var(--md-default-fg-color);display:block;font-size:.64rem;font-style:italic;margin:.5em 0}.md-search-result mark{background-color:initial;color:var(--md-accent-fg-color);text-decoration:underline}.md-select{position:relative;z-index:1}.md-select__inner{background-color:var(--md-default-bg-color);border-radius:.1rem;box-shadow:var(--md-shadow-z2);color:var(--md-default-fg-color);left:50%;margin-top:.2rem;max-height:0;opacity:0;position:absolute;top:calc(100% - .2rem);transform:translate3d(-50%,.3rem,0);transition:transform .25s 375ms,opacity .25s .25s,max-height 0ms .5s}.md-select:focus-within .md-select__inner,.md-select:hover .md-select__inner{max-height:10rem;opacity:1;transform:translate3d(-50%,0,0);transition:transform .25s cubic-bezier(.1,.7,.1,1),opacity .25s,max-height 0ms}.md-select__inner:after{border-bottom:.2rem solid #0000;border-bottom-color:var(--md-default-bg-color);border-left:.2rem solid #0000;border-right:.2rem solid #0000;border-top:0;content:"";height:0;left:50%;margin-left:-.2rem;margin-top:-.2rem;position:absolute;top:0;width:0}.md-select__list{border-radius:.1rem;font-size:.8rem;list-style-type:none;margin:0;max-height:inherit;overflow:auto;padding:0}.md-select__item{line-height:1.8rem}[dir=ltr] .md-select__link{padding-left:.6rem;padding-right:1.2rem}[dir=rtl] .md-select__link{padding-left:1.2rem;padding-right:.6rem}.md-select__link{cursor:pointer;display:block;outline:none;scroll-snap-align:start;transition:background-color .25s,color .25s;width:100%}.md-select__link:focus,.md-select__link:hover{color:var(--md-accent-fg-color)}.md-select__link:focus{background-color:var(--md-default-fg-color--lightest)}.md-sidebar{align-self:flex-start;flex-shrink:0;padding:1.2rem 0;position:sticky;top:2.4rem;width:12.1rem}@media print{.md-sidebar{display:none}}@media screen and (max-width:76.234375em){[dir=ltr] .md-sidebar--primary{left:-12.1rem}[dir=rtl] .md-sidebar--primary{right:-12.1rem}.md-sidebar--primary{background-color:var(--md-default-bg-color);display:block;height:100%;position:fixed;top:0;transform:translateX(0);transition:transform .25s cubic-bezier(.4,0,.2,1),box-shadow .25s;width:12.1rem;z-index:5}[data-md-toggle=drawer]:checked~.md-container .md-sidebar--primary{box-shadow:var(--md-shadow-z3);transform:translateX(12.1rem)}[dir=rtl] [data-md-toggle=drawer]:checked~.md-container .md-sidebar--primary{transform:translateX(-12.1rem)}.md-sidebar--primary .md-sidebar__scrollwrap{bottom:0;left:0;margin:0;overflow:hidden;position:absolute;right:0;scroll-snap-type:none;top:0}}@media screen and (min-width:76.25em){.md-sidebar{height:0}.no-js .md-sidebar{height:auto}.md-header--lifted~.md-container .md-sidebar{top:4.8rem}}.md-sidebar--secondary{display:none;order:2}@media screen and (min-width:60em){.md-sidebar--secondary{height:0}.no-js .md-sidebar--secondary{height:auto}.md-sidebar--secondary:not([hidden]){display:block}.md-sidebar--secondary .md-sidebar__scrollwrap{touch-action:pan-y}}.md-sidebar__scrollwrap{backface-visibility:hidden;margin:0 .2rem;overflow-y:auto;scrollbar-color:var(--md-default-fg-color--lighter) #0000}@media screen and (min-width:60em){.md-sidebar__scrollwrap{scrollbar-gutter:stable;scrollbar-width:thin}}.md-sidebar__scrollwrap::-webkit-scrollbar{height:.2rem;width:.2rem}.md-sidebar__scrollwrap:focus-within,.md-sidebar__scrollwrap:hover{scrollbar-color:var(--md-accent-fg-color) #0000}.md-sidebar__scrollwrap:focus-within::-webkit-scrollbar-thumb,.md-sidebar__scrollwrap:hover::-webkit-scrollbar-thumb{background-color:var(--md-default-fg-color--lighter)}.md-sidebar__scrollwrap:focus-within::-webkit-scrollbar-thumb:hover,.md-sidebar__scrollwrap:hover::-webkit-scrollbar-thumb:hover{background-color:var(--md-accent-fg-color)}@supports selector(::-webkit-scrollbar){.md-sidebar__scrollwrap{scrollbar-gutter:auto}[dir=ltr] .md-sidebar__inner{padding-right:calc(100% - 11.5rem)}[dir=rtl] .md-sidebar__inner{padding-left:calc(100% - 11.5rem)}}@media screen and (max-width:76.234375em){.md-overlay{background-color:#0000008a;height:0;opacity:0;position:fixed;top:0;transition:width 0ms .25s,height 0ms .25s,opacity .25s;width:0;z-index:5}[data-md-toggle=drawer]:checked~.md-overlay{height:100%;opacity:1;transition:width 0ms,height 0ms,opacity .25s;width:100%}}@keyframes facts{0%{height:0}to{height:.65rem}}@keyframes fact{0%{opacity:0;transform:translateY(100%)}50%{opacity:0}to{opacity:1;transform:translateY(0)}}:root{--md-source-forks-icon:url('data:image/svg+xml;charset=utf-8,');--md-source-repositories-icon:url('data:image/svg+xml;charset=utf-8,');--md-source-stars-icon:url('data:image/svg+xml;charset=utf-8,');--md-source-version-icon:url('data:image/svg+xml;charset=utf-8,')}.md-source{backface-visibility:hidden;display:block;font-size:.65rem;line-height:1.2;outline-color:var(--md-accent-fg-color);transition:opacity .25s;white-space:nowrap}.md-source:hover{opacity:.7}.md-source__icon{display:inline-block;height:2.4rem;vertical-align:middle;width:2rem}[dir=ltr] .md-source__icon svg{margin-left:.6rem}[dir=rtl] .md-source__icon svg{margin-right:.6rem}.md-source__icon svg{margin-top:.6rem}[dir=ltr] .md-source__icon+.md-source__repository{padding-left:2rem}[dir=rtl] .md-source__icon+.md-source__repository{padding-right:2rem}[dir=ltr] .md-source__icon+.md-source__repository{margin-left:-2rem}[dir=rtl] .md-source__icon+.md-source__repository{margin-right:-2rem}[dir=ltr] .md-source__repository{margin-left:.6rem}[dir=rtl] .md-source__repository{margin-right:.6rem}.md-source__repository{display:inline-block;max-width:calc(100% - 1.2rem);overflow:hidden;text-overflow:ellipsis;vertical-align:middle}.md-source__facts{display:flex;font-size:.55rem;gap:.4rem;list-style-type:none;margin:.1rem 0 0;opacity:.75;overflow:hidden;padding:0;width:100%}.md-source__repository--active .md-source__facts{animation:facts .25s ease-in}.md-source__fact{overflow:hidden;text-overflow:ellipsis}.md-source__repository--active .md-source__fact{animation:fact .4s ease-out}[dir=ltr] .md-source__fact:before{margin-right:.1rem}[dir=rtl] .md-source__fact:before{margin-left:.1rem}.md-source__fact:before{background-color:currentcolor;content:"";display:inline-block;height:.6rem;-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;vertical-align:text-top;width:.6rem}.md-source__fact:nth-child(1n+2){flex-shrink:0}.md-source__fact--version:before{-webkit-mask-image:var(--md-source-version-icon);mask-image:var(--md-source-version-icon)}.md-source__fact--stars:before{-webkit-mask-image:var(--md-source-stars-icon);mask-image:var(--md-source-stars-icon)}.md-source__fact--forks:before{-webkit-mask-image:var(--md-source-forks-icon);mask-image:var(--md-source-forks-icon)}.md-source__fact--repositories:before{-webkit-mask-image:var(--md-source-repositories-icon);mask-image:var(--md-source-repositories-icon)}.md-source-file{margin:1em 0}[dir=ltr] .md-source-file__fact{margin-right:.6rem}[dir=rtl] .md-source-file__fact{margin-left:.6rem}.md-source-file__fact{align-items:center;color:var(--md-default-fg-color--light);display:inline-flex;font-size:.68rem;gap:.3rem}.md-source-file__fact .md-icon{flex-shrink:0;margin-bottom:.05rem}[dir=ltr] .md-source-file__fact .md-author{float:left}[dir=rtl] .md-source-file__fact .md-author{float:right}.md-source-file__fact .md-author{margin-right:.2rem}.md-source-file__fact svg{width:.9rem}:root{--md-status:url('data:image/svg+xml;charset=utf-8,');--md-status--new:url('data:image/svg+xml;charset=utf-8,');--md-status--deprecated:url('data:image/svg+xml;charset=utf-8,');--md-status--encrypted:url('data:image/svg+xml;charset=utf-8,')}.md-status:after{background-color:var(--md-default-fg-color--light);content:"";display:inline-block;height:1.125em;-webkit-mask-image:var(--md-status);mask-image:var(--md-status);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;vertical-align:text-bottom;width:1.125em}.md-status:hover:after{background-color:currentcolor}.md-status--new:after{-webkit-mask-image:var(--md-status--new);mask-image:var(--md-status--new)}.md-status--deprecated:after{-webkit-mask-image:var(--md-status--deprecated);mask-image:var(--md-status--deprecated)}.md-status--encrypted:after{-webkit-mask-image:var(--md-status--encrypted);mask-image:var(--md-status--encrypted)}.md-tabs{background-color:var(--md-primary-fg-color);color:var(--md-primary-bg-color);display:block;line-height:1.3;overflow:auto;width:100%;z-index:3}@media print{.md-tabs{display:none}}@media screen and (max-width:76.234375em){.md-tabs{display:none}}.md-tabs[hidden]{pointer-events:none}[dir=ltr] .md-tabs__list{margin-left:.2rem}[dir=rtl] .md-tabs__list{margin-right:.2rem}.md-tabs__list{contain:content;display:flex;list-style:none;margin:0;overflow:auto;padding:0;scrollbar-width:none;white-space:nowrap}.md-tabs__list::-webkit-scrollbar{display:none}.md-tabs__item{height:2.4rem;padding-left:.6rem;padding-right:.6rem}.md-tabs__item--active .md-tabs__link{color:inherit;opacity:1}.md-tabs__link{backface-visibility:hidden;display:flex;font-size:.7rem;margin-top:.8rem;opacity:.7;outline-color:var(--md-accent-fg-color);outline-offset:.2rem;transition:transform .4s cubic-bezier(.1,.7,.1,1),opacity .25s}.md-tabs__link:focus,.md-tabs__link:hover{color:inherit;opacity:1}[dir=ltr] .md-tabs__link svg{margin-right:.4rem}[dir=rtl] .md-tabs__link svg{margin-left:.4rem}.md-tabs__link svg{fill:currentcolor;height:1.3em}.md-tabs__item:nth-child(2) .md-tabs__link{transition-delay:20ms}.md-tabs__item:nth-child(3) .md-tabs__link{transition-delay:40ms}.md-tabs__item:nth-child(4) .md-tabs__link{transition-delay:60ms}.md-tabs__item:nth-child(5) .md-tabs__link{transition-delay:80ms}.md-tabs__item:nth-child(6) .md-tabs__link{transition-delay:.1s}.md-tabs__item:nth-child(7) .md-tabs__link{transition-delay:.12s}.md-tabs__item:nth-child(8) .md-tabs__link{transition-delay:.14s}.md-tabs__item:nth-child(9) .md-tabs__link{transition-delay:.16s}.md-tabs__item:nth-child(10) .md-tabs__link{transition-delay:.18s}.md-tabs__item:nth-child(11) .md-tabs__link{transition-delay:.2s}.md-tabs__item:nth-child(12) .md-tabs__link{transition-delay:.22s}.md-tabs__item:nth-child(13) .md-tabs__link{transition-delay:.24s}.md-tabs__item:nth-child(14) .md-tabs__link{transition-delay:.26s}.md-tabs__item:nth-child(15) .md-tabs__link{transition-delay:.28s}.md-tabs__item:nth-child(16) .md-tabs__link{transition-delay:.3s}.md-tabs[hidden] .md-tabs__link{opacity:0;transform:translateY(50%);transition:transform 0ms .1s,opacity .1s}:root{--md-tag-icon:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .md-tags:not([hidden]){display:inline-flex;flex-wrap:wrap;gap:.5em;margin-bottom:.75em;margin-top:-.125em}.md-typeset .md-tag{align-items:center;background:var(--md-default-fg-color--lightest);border-radius:2.4rem;display:inline-flex;font-size:.64rem;font-size:min(.8em,.64rem);font-weight:700;gap:.5em;letter-spacing:normal;line-height:1.6;padding:.3125em .78125em}.md-typeset .md-tag[href]{-webkit-tap-highlight-color:transparent;color:inherit;outline:none;transition:color 125ms,background-color 125ms}.md-typeset .md-tag[href]:focus,.md-typeset .md-tag[href]:hover{background-color:var(--md-accent-fg-color);color:var(--md-accent-bg-color)}[id]>.md-typeset .md-tag{vertical-align:text-top}.md-typeset .md-tag-shadow{opacity:.5}.md-typeset .md-tag-icon:before{background-color:var(--md-default-fg-color--lighter);content:"";display:inline-block;height:1.2em;-webkit-mask-image:var(--md-tag-icon);mask-image:var(--md-tag-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;transition:background-color 125ms;vertical-align:text-bottom;width:1.2em}.md-typeset .md-tag-icon[href]:focus:before,.md-typeset .md-tag-icon[href]:hover:before{background-color:var(--md-accent-bg-color)}@keyframes pulse{0%{transform:scale(.95)}75%{transform:scale(1)}to{transform:scale(.95)}}:root{--md-annotation-bg-icon:url('data:image/svg+xml;charset=utf-8,');--md-annotation-icon:url('data:image/svg+xml;charset=utf-8,')}.md-tooltip{backface-visibility:hidden;background-color:var(--md-default-bg-color);border-radius:.1rem;box-shadow:var(--md-shadow-z2);color:var(--md-default-fg-color);font-family:var(--md-text-font-family);left:clamp(var(--md-tooltip-0,0rem) + .8rem,var(--md-tooltip-x),100vw + var(--md-tooltip-0,0rem) + .8rem - var(--md-tooltip-width) - 2 * .8rem);max-width:calc(100vw - 1.6rem);opacity:0;position:absolute;top:var(--md-tooltip-y);transform:translateY(-.4rem);transition:transform 0ms .25s,opacity .25s,z-index .25s;width:var(--md-tooltip-width);z-index:0}.md-tooltip--active{opacity:1;transform:translateY(0);transition:transform .25s cubic-bezier(.1,.7,.1,1),opacity .25s,z-index 0ms;z-index:2}.md-tooltip--inline{font-weight:700;-webkit-user-select:none;user-select:none;width:auto}.md-tooltip--inline:not(.md-tooltip--active){transform:translateY(.2rem) scale(.9)}.md-tooltip--inline .md-tooltip__inner{font-size:.5rem;padding:.2rem .4rem}[hidden]+.md-tooltip--inline{display:none}.focus-visible>.md-tooltip,.md-tooltip:target{outline:var(--md-accent-fg-color) auto}.md-tooltip__inner{font-size:.64rem;padding:.8rem}.md-tooltip__inner.md-typeset>:first-child{margin-top:0}.md-tooltip__inner.md-typeset>:last-child{margin-bottom:0}.md-annotation{font-style:normal;font-weight:400;outline:none;text-align:initial;vertical-align:text-bottom;white-space:normal}[dir=rtl] .md-annotation{direction:rtl}code .md-annotation{font-family:var(--md-code-font-family);font-size:inherit}.md-annotation:not([hidden]){display:inline-block;line-height:1.25}.md-annotation__index{border-radius:.01px;cursor:pointer;display:inline-block;margin-left:.4ch;margin-right:.4ch;outline:none;overflow:hidden;position:relative;-webkit-user-select:none;user-select:none;vertical-align:text-top;z-index:0}.md-annotation .md-annotation__index{transition:z-index .25s}@media screen{.md-annotation__index{width:2.2ch}[data-md-visible]>.md-annotation__index{animation:pulse 2s infinite}.md-annotation__index:before{background:var(--md-default-bg-color);-webkit-mask-image:var(--md-annotation-bg-icon);mask-image:var(--md-annotation-bg-icon)}.md-annotation__index:after,.md-annotation__index:before{content:"";height:2.2ch;-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;position:absolute;top:-.1ch;width:2.2ch;z-index:-1}.md-annotation__index:after{background-color:var(--md-default-fg-color--lighter);-webkit-mask-image:var(--md-annotation-icon);mask-image:var(--md-annotation-icon);transform:scale(1.0001);transition:background-color .25s,transform .25s}.md-tooltip--active+.md-annotation__index:after{transform:rotate(45deg)}.md-tooltip--active+.md-annotation__index:after,:hover>.md-annotation__index:after{background-color:var(--md-accent-fg-color)}}.md-tooltip--active+.md-annotation__index{animation-play-state:paused;transition-duration:0ms;z-index:2}.md-annotation__index [data-md-annotation-id]{display:inline-block}@media print{.md-annotation__index [data-md-annotation-id]{background:var(--md-default-fg-color--lighter);border-radius:2ch;color:var(--md-default-bg-color);font-weight:700;padding:0 .6ch;white-space:nowrap}.md-annotation__index [data-md-annotation-id]:after{content:attr(data-md-annotation-id)}}.md-typeset .md-annotation-list{counter-reset:annotation;list-style:none!important}.md-typeset .md-annotation-list li{position:relative}[dir=ltr] .md-typeset .md-annotation-list li:before{left:-2.125em}[dir=rtl] .md-typeset .md-annotation-list li:before{right:-2.125em}.md-typeset .md-annotation-list li:before{background:var(--md-default-fg-color--lighter);border-radius:2ch;color:var(--md-default-bg-color);content:counter(annotation);counter-increment:annotation;font-size:.8875em;font-weight:700;height:2ch;line-height:1.25;min-width:2ch;padding:0 .6ch;position:absolute;text-align:center;top:.25em}:root{--md-tooltip-width:20rem;--md-tooltip-tail:0.3rem}.md-tooltip2{backface-visibility:hidden;color:var(--md-default-fg-color);font-family:var(--md-text-font-family);opacity:0;pointer-events:none;position:absolute;top:calc(var(--md-tooltip-host-y) + var(--md-tooltip-y));transform:translateY(-.4rem);transform-origin:calc(var(--md-tooltip-host-x) + var(--md-tooltip-x)) 0;transition:transform 0ms .25s,opacity .25s,z-index .25s;width:100%;z-index:0}.md-tooltip2:before{border-left:var(--md-tooltip-tail) solid #0000;border-right:var(--md-tooltip-tail) solid #0000;content:"";display:block;left:clamp(1.5 * .8rem,var(--md-tooltip-host-x) + var(--md-tooltip-x) - var(--md-tooltip-tail),100vw - 2 * var(--md-tooltip-tail) - 1.5 * .8rem);position:absolute;z-index:1}.md-tooltip2--top:before{border-top:var(--md-tooltip-tail) solid var(--md-default-bg-color);bottom:calc(var(--md-tooltip-tail)*-1 + .025rem);filter:drop-shadow(0 1px 0 hsla(0,0%,0%,.05))}.md-tooltip2--bottom:before{border-bottom:var(--md-tooltip-tail) solid var(--md-default-bg-color);filter:drop-shadow(0 -1px 0 hsla(0,0%,0%,.05));top:calc(var(--md-tooltip-tail)*-1 + .025rem)}.md-tooltip2--active{opacity:1;transform:translateY(0);transition:transform .4s cubic-bezier(0,1,.5,1),opacity .25s,z-index 0ms;z-index:2}.md-tooltip2__inner{scrollbar-gutter:stable;background-color:var(--md-default-bg-color);border-radius:.1rem;box-shadow:var(--md-shadow-z2);left:clamp(.8rem,var(--md-tooltip-host-x) - .8rem,100vw - var(--md-tooltip-width) - .8rem);max-height:40vh;max-width:calc(100vw - 1.6rem);position:relative;scrollbar-width:thin}.md-tooltip2__inner::-webkit-scrollbar{height:.2rem;width:.2rem}.md-tooltip2__inner::-webkit-scrollbar-thumb{background-color:var(--md-default-fg-color--lighter)}.md-tooltip2__inner::-webkit-scrollbar-thumb:hover{background-color:var(--md-accent-fg-color)}[role=dialog]>.md-tooltip2__inner{font-size:.64rem;overflow:auto;padding:0 .8rem;pointer-events:auto;width:var(--md-tooltip-width)}[role=dialog]>.md-tooltip2__inner:after,[role=dialog]>.md-tooltip2__inner:before{content:"";display:block;height:.8rem;position:sticky;width:100%;z-index:10}[role=dialog]>.md-tooltip2__inner:before{background:linear-gradient(var(--md-default-bg-color),#0000 75%);top:0}[role=dialog]>.md-tooltip2__inner:after{background:linear-gradient(#0000,var(--md-default-bg-color) 75%);bottom:0}[role=tooltip]>.md-tooltip2__inner{font-size:.5rem;font-weight:700;left:clamp(.8rem,var(--md-tooltip-host-x) + var(--md-tooltip-x) - var(--md-tooltip-width)/2,100vw - var(--md-tooltip-width) - .8rem);max-width:min(100vw - 2 * .8rem,400px);padding:.2rem .4rem;-webkit-user-select:none;user-select:none;width:fit-content}.md-tooltip2__inner.md-typeset>:first-child{margin-top:0}.md-tooltip2__inner.md-typeset>:last-child{margin-bottom:0}[dir=ltr] .md-top{margin-left:50%}[dir=rtl] .md-top{margin-right:50%}.md-top{background-color:var(--md-default-bg-color);border-radius:1.6rem;box-shadow:var(--md-shadow-z2);color:var(--md-default-fg-color--light);cursor:pointer;display:block;font-size:.7rem;outline:none;padding:.4rem .8rem;position:fixed;top:3.2rem;transform:translate(-50%);transition:color 125ms,background-color 125ms,transform 125ms cubic-bezier(.4,0,.2,1),opacity 125ms;z-index:2}@media print{.md-top{display:none}}[dir=rtl] .md-top{transform:translate(50%)}.md-top[hidden]{opacity:0;pointer-events:none;transform:translate(-50%,.2rem);transition-duration:0ms}[dir=rtl] .md-top[hidden]{transform:translate(50%,.2rem)}.md-top:focus,.md-top:hover{background-color:var(--md-accent-fg-color);color:var(--md-accent-bg-color)}.md-top svg{display:inline-block;vertical-align:-.5em}@keyframes hoverfix{0%{pointer-events:none}}:root{--md-version-icon:url('data:image/svg+xml;charset=utf-8,')}.md-version{flex-shrink:0;font-size:.8rem;height:2.4rem}[dir=ltr] .md-version__current{margin-left:1.4rem;margin-right:.4rem}[dir=rtl] .md-version__current{margin-left:.4rem;margin-right:1.4rem}.md-version__current{color:inherit;cursor:pointer;outline:none;position:relative;top:.05rem}[dir=ltr] .md-version__current:after{margin-left:.4rem}[dir=rtl] .md-version__current:after{margin-right:.4rem}.md-version__current:after{background-color:currentcolor;content:"";display:inline-block;height:.6rem;-webkit-mask-image:var(--md-version-icon);mask-image:var(--md-version-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:.4rem}.md-version__alias{margin-left:.3rem;opacity:.7}.md-version__list{background-color:var(--md-default-bg-color);border-radius:.1rem;box-shadow:var(--md-shadow-z2);color:var(--md-default-fg-color);list-style-type:none;margin:.2rem .8rem;max-height:0;opacity:0;overflow:auto;padding:0;position:absolute;scroll-snap-type:y mandatory;top:.15rem;transition:max-height 0ms .5s,opacity .25s .25s;z-index:3}.md-version:focus-within .md-version__list,.md-version:hover .md-version__list{max-height:10rem;opacity:1;transition:max-height 0ms,opacity .25s}@media (hover:none),(pointer:coarse){.md-version:hover .md-version__list{animation:hoverfix .25s forwards}.md-version:focus-within .md-version__list{animation:none}}.md-version__item{line-height:1.8rem}[dir=ltr] .md-version__link{padding-left:.6rem;padding-right:1.2rem}[dir=rtl] .md-version__link{padding-left:1.2rem;padding-right:.6rem}.md-version__link{cursor:pointer;display:block;outline:none;scroll-snap-align:start;transition:color .25s,background-color .25s;white-space:nowrap;width:100%}.md-version__link:focus,.md-version__link:hover{color:var(--md-accent-fg-color)}.md-version__link:focus{background-color:var(--md-default-fg-color--lightest)}:root{--md-admonition-icon--note:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--abstract:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--info:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--tip:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--success:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--question:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--warning:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--failure:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--danger:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--bug:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--example:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--quote:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .admonition,.md-typeset details{background-color:var(--md-admonition-bg-color);border:.075rem solid #448aff;border-radius:.2rem;box-shadow:var(--md-shadow-z1);color:var(--md-admonition-fg-color);display:flow-root;font-size:.64rem;margin:1.5625em 0;padding:0 .6rem;page-break-inside:avoid;transition:box-shadow 125ms}@media print{.md-typeset .admonition,.md-typeset details{box-shadow:none}}.md-typeset .admonition:focus-within,.md-typeset details:focus-within{box-shadow:0 0 0 .2rem #448aff1a}.md-typeset .admonition>*,.md-typeset details>*{box-sizing:border-box}.md-typeset .admonition .admonition,.md-typeset .admonition details,.md-typeset details .admonition,.md-typeset details details{margin-bottom:1em;margin-top:1em}.md-typeset .admonition .md-typeset__scrollwrap,.md-typeset details .md-typeset__scrollwrap{margin:1em -.6rem}.md-typeset .admonition .md-typeset__table,.md-typeset details .md-typeset__table{padding:0 .6rem}.md-typeset .admonition>.tabbed-set:only-child,.md-typeset details>.tabbed-set:only-child{margin-top:0}html .md-typeset .admonition>:last-child,html .md-typeset details>:last-child{margin-bottom:.6rem}[dir=ltr] .md-typeset .admonition-title,[dir=ltr] .md-typeset summary{padding-left:2rem;padding-right:.6rem}[dir=rtl] .md-typeset .admonition-title,[dir=rtl] .md-typeset summary{padding-left:.6rem;padding-right:2rem}[dir=ltr] .md-typeset .admonition-title,[dir=ltr] .md-typeset summary{border-left-width:.2rem}[dir=rtl] .md-typeset .admonition-title,[dir=rtl] .md-typeset summary{border-right-width:.2rem}[dir=ltr] .md-typeset .admonition-title,[dir=ltr] .md-typeset summary{border-top-left-radius:.1rem}[dir=ltr] .md-typeset .admonition-title,[dir=ltr] .md-typeset summary,[dir=rtl] .md-typeset .admonition-title,[dir=rtl] .md-typeset summary{border-top-right-radius:.1rem}[dir=rtl] .md-typeset .admonition-title,[dir=rtl] .md-typeset summary{border-top-left-radius:.1rem}.md-typeset .admonition-title,.md-typeset summary{background-color:#448aff1a;border:none;font-weight:700;margin:0 -.6rem;padding-bottom:.4rem;padding-top:.4rem;position:relative}html .md-typeset .admonition-title:last-child,html .md-typeset summary:last-child{margin-bottom:0}[dir=ltr] .md-typeset .admonition-title:before,[dir=ltr] .md-typeset summary:before{left:.6rem}[dir=rtl] .md-typeset .admonition-title:before,[dir=rtl] .md-typeset summary:before{right:.6rem}.md-typeset .admonition-title:before,.md-typeset summary:before{background-color:#448aff;content:"";height:1rem;-webkit-mask-image:var(--md-admonition-icon--note);mask-image:var(--md-admonition-icon--note);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;position:absolute;top:.625em;width:1rem}.md-typeset .admonition-title code,.md-typeset summary code{box-shadow:0 0 0 .05rem var(--md-default-fg-color--lightest)}.md-typeset .admonition.note,.md-typeset details.note{border-color:#448aff}.md-typeset .admonition.note:focus-within,.md-typeset details.note:focus-within{box-shadow:0 0 0 .2rem #448aff1a}.md-typeset .note>.admonition-title,.md-typeset .note>summary{background-color:#448aff1a}.md-typeset .note>.admonition-title:before,.md-typeset .note>summary:before{background-color:#448aff;-webkit-mask-image:var(--md-admonition-icon--note);mask-image:var(--md-admonition-icon--note)}.md-typeset .note>.admonition-title:after,.md-typeset .note>summary:after{color:#448aff}.md-typeset .admonition.abstract,.md-typeset details.abstract{border-color:#00b0ff}.md-typeset .admonition.abstract:focus-within,.md-typeset details.abstract:focus-within{box-shadow:0 0 0 .2rem #00b0ff1a}.md-typeset .abstract>.admonition-title,.md-typeset .abstract>summary{background-color:#00b0ff1a}.md-typeset .abstract>.admonition-title:before,.md-typeset .abstract>summary:before{background-color:#00b0ff;-webkit-mask-image:var(--md-admonition-icon--abstract);mask-image:var(--md-admonition-icon--abstract)}.md-typeset .abstract>.admonition-title:after,.md-typeset .abstract>summary:after{color:#00b0ff}.md-typeset .admonition.info,.md-typeset details.info{border-color:#00b8d4}.md-typeset .admonition.info:focus-within,.md-typeset details.info:focus-within{box-shadow:0 0 0 .2rem #00b8d41a}.md-typeset .info>.admonition-title,.md-typeset .info>summary{background-color:#00b8d41a}.md-typeset .info>.admonition-title:before,.md-typeset .info>summary:before{background-color:#00b8d4;-webkit-mask-image:var(--md-admonition-icon--info);mask-image:var(--md-admonition-icon--info)}.md-typeset .info>.admonition-title:after,.md-typeset .info>summary:after{color:#00b8d4}.md-typeset .admonition.tip,.md-typeset details.tip{border-color:#00bfa5}.md-typeset .admonition.tip:focus-within,.md-typeset details.tip:focus-within{box-shadow:0 0 0 .2rem #00bfa51a}.md-typeset .tip>.admonition-title,.md-typeset .tip>summary{background-color:#00bfa51a}.md-typeset .tip>.admonition-title:before,.md-typeset .tip>summary:before{background-color:#00bfa5;-webkit-mask-image:var(--md-admonition-icon--tip);mask-image:var(--md-admonition-icon--tip)}.md-typeset .tip>.admonition-title:after,.md-typeset .tip>summary:after{color:#00bfa5}.md-typeset .admonition.success,.md-typeset details.success{border-color:#00c853}.md-typeset .admonition.success:focus-within,.md-typeset details.success:focus-within{box-shadow:0 0 0 .2rem #00c8531a}.md-typeset .success>.admonition-title,.md-typeset .success>summary{background-color:#00c8531a}.md-typeset .success>.admonition-title:before,.md-typeset .success>summary:before{background-color:#00c853;-webkit-mask-image:var(--md-admonition-icon--success);mask-image:var(--md-admonition-icon--success)}.md-typeset .success>.admonition-title:after,.md-typeset .success>summary:after{color:#00c853}.md-typeset .admonition.question,.md-typeset details.question{border-color:#64dd17}.md-typeset .admonition.question:focus-within,.md-typeset details.question:focus-within{box-shadow:0 0 0 .2rem #64dd171a}.md-typeset .question>.admonition-title,.md-typeset .question>summary{background-color:#64dd171a}.md-typeset .question>.admonition-title:before,.md-typeset .question>summary:before{background-color:#64dd17;-webkit-mask-image:var(--md-admonition-icon--question);mask-image:var(--md-admonition-icon--question)}.md-typeset .question>.admonition-title:after,.md-typeset .question>summary:after{color:#64dd17}.md-typeset .admonition.warning,.md-typeset details.warning{border-color:#ff9100}.md-typeset .admonition.warning:focus-within,.md-typeset details.warning:focus-within{box-shadow:0 0 0 .2rem #ff91001a}.md-typeset .warning>.admonition-title,.md-typeset .warning>summary{background-color:#ff91001a}.md-typeset .warning>.admonition-title:before,.md-typeset .warning>summary:before{background-color:#ff9100;-webkit-mask-image:var(--md-admonition-icon--warning);mask-image:var(--md-admonition-icon--warning)}.md-typeset .warning>.admonition-title:after,.md-typeset .warning>summary:after{color:#ff9100}.md-typeset .admonition.failure,.md-typeset details.failure{border-color:#ff5252}.md-typeset .admonition.failure:focus-within,.md-typeset details.failure:focus-within{box-shadow:0 0 0 .2rem #ff52521a}.md-typeset .failure>.admonition-title,.md-typeset .failure>summary{background-color:#ff52521a}.md-typeset .failure>.admonition-title:before,.md-typeset .failure>summary:before{background-color:#ff5252;-webkit-mask-image:var(--md-admonition-icon--failure);mask-image:var(--md-admonition-icon--failure)}.md-typeset .failure>.admonition-title:after,.md-typeset .failure>summary:after{color:#ff5252}.md-typeset .admonition.danger,.md-typeset details.danger{border-color:#ff1744}.md-typeset .admonition.danger:focus-within,.md-typeset details.danger:focus-within{box-shadow:0 0 0 .2rem #ff17441a}.md-typeset .danger>.admonition-title,.md-typeset .danger>summary{background-color:#ff17441a}.md-typeset .danger>.admonition-title:before,.md-typeset .danger>summary:before{background-color:#ff1744;-webkit-mask-image:var(--md-admonition-icon--danger);mask-image:var(--md-admonition-icon--danger)}.md-typeset .danger>.admonition-title:after,.md-typeset .danger>summary:after{color:#ff1744}.md-typeset .admonition.bug,.md-typeset details.bug{border-color:#f50057}.md-typeset .admonition.bug:focus-within,.md-typeset details.bug:focus-within{box-shadow:0 0 0 .2rem #f500571a}.md-typeset .bug>.admonition-title,.md-typeset .bug>summary{background-color:#f500571a}.md-typeset .bug>.admonition-title:before,.md-typeset .bug>summary:before{background-color:#f50057;-webkit-mask-image:var(--md-admonition-icon--bug);mask-image:var(--md-admonition-icon--bug)}.md-typeset .bug>.admonition-title:after,.md-typeset .bug>summary:after{color:#f50057}.md-typeset .admonition.example,.md-typeset details.example{border-color:#7c4dff}.md-typeset .admonition.example:focus-within,.md-typeset details.example:focus-within{box-shadow:0 0 0 .2rem #7c4dff1a}.md-typeset .example>.admonition-title,.md-typeset .example>summary{background-color:#7c4dff1a}.md-typeset .example>.admonition-title:before,.md-typeset .example>summary:before{background-color:#7c4dff;-webkit-mask-image:var(--md-admonition-icon--example);mask-image:var(--md-admonition-icon--example)}.md-typeset .example>.admonition-title:after,.md-typeset .example>summary:after{color:#7c4dff}.md-typeset .admonition.quote,.md-typeset details.quote{border-color:#9e9e9e}.md-typeset .admonition.quote:focus-within,.md-typeset details.quote:focus-within{box-shadow:0 0 0 .2rem #9e9e9e1a}.md-typeset .quote>.admonition-title,.md-typeset .quote>summary{background-color:#9e9e9e1a}.md-typeset .quote>.admonition-title:before,.md-typeset .quote>summary:before{background-color:#9e9e9e;-webkit-mask-image:var(--md-admonition-icon--quote);mask-image:var(--md-admonition-icon--quote)}.md-typeset .quote>.admonition-title:after,.md-typeset .quote>summary:after{color:#9e9e9e}:root{--md-footnotes-icon:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .footnote{color:var(--md-default-fg-color--light);font-size:.64rem}[dir=ltr] .md-typeset .footnote>ol{margin-left:0}[dir=rtl] .md-typeset .footnote>ol{margin-right:0}.md-typeset .footnote>ol>li{transition:color 125ms}.md-typeset .footnote>ol>li:target{color:var(--md-default-fg-color)}.md-typeset .footnote>ol>li:focus-within .footnote-backref{opacity:1;transform:translateX(0);transition:none}.md-typeset .footnote>ol>li:hover .footnote-backref,.md-typeset .footnote>ol>li:target .footnote-backref{opacity:1;transform:translateX(0)}.md-typeset .footnote>ol>li>:first-child{margin-top:0}.md-typeset .footnote-ref{font-size:.75em;font-weight:700}html .md-typeset .footnote-ref{outline-offset:.1rem}.md-typeset [id^="fnref:"]:target>.footnote-ref{outline:auto}.md-typeset .footnote-backref{color:var(--md-typeset-a-color);display:inline-block;font-size:0;opacity:0;transform:translateX(.25rem);transition:color .25s,transform .25s .25s,opacity 125ms .25s;vertical-align:text-bottom}@media print{.md-typeset .footnote-backref{color:var(--md-typeset-a-color);opacity:1;transform:translateX(0)}}[dir=rtl] .md-typeset .footnote-backref{transform:translateX(-.25rem)}.md-typeset .footnote-backref:hover{color:var(--md-accent-fg-color)}.md-typeset .footnote-backref:before{background-color:currentcolor;content:"";display:inline-block;height:.8rem;-webkit-mask-image:var(--md-footnotes-icon);mask-image:var(--md-footnotes-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:.8rem}[dir=rtl] .md-typeset .footnote-backref:before{transform:scaleX(-1)}[dir=ltr] .md-typeset .headerlink{margin-left:.5rem}[dir=rtl] .md-typeset .headerlink{margin-right:.5rem}.md-typeset .headerlink{color:var(--md-default-fg-color--lighter);display:inline-block;opacity:0;transition:color .25s,opacity 125ms}@media print{.md-typeset .headerlink{display:none}}.md-typeset .headerlink:focus,.md-typeset :hover>.headerlink,.md-typeset :target>.headerlink{opacity:1;transition:color .25s,opacity 125ms}.md-typeset .headerlink:focus,.md-typeset .headerlink:hover,.md-typeset :target>.headerlink{color:var(--md-accent-fg-color)}.md-typeset :target{--md-scroll-margin:3.6rem;--md-scroll-offset:0rem;scroll-margin-top:calc(var(--md-scroll-margin) - var(--md-scroll-offset))}@media screen and (min-width:76.25em){.md-header--lifted~.md-container .md-typeset :target{--md-scroll-margin:6rem}}.md-typeset h1:target,.md-typeset h2:target,.md-typeset h3:target{--md-scroll-offset:0.2rem}.md-typeset h4:target{--md-scroll-offset:0.15rem}.md-typeset div.arithmatex{overflow:auto}@media screen and (max-width:44.984375em){.md-typeset div.arithmatex{margin:0 -.8rem}.md-typeset div.arithmatex>*{width:min-content}}.md-typeset div.arithmatex>*{margin-left:auto!important;margin-right:auto!important;padding:0 .8rem;touch-action:auto}.md-typeset div.arithmatex>* mjx-container{margin:0!important}.md-typeset div.arithmatex mjx-assistive-mml{height:0}.md-typeset .katex-html svg{max-width:revert}.md-typeset del.critic{background-color:var(--md-typeset-del-color)}.md-typeset del.critic,.md-typeset ins.critic{-webkit-box-decoration-break:clone;box-decoration-break:clone}.md-typeset ins.critic{background-color:var(--md-typeset-ins-color)}.md-typeset .critic.comment{-webkit-box-decoration-break:clone;box-decoration-break:clone;color:var(--md-code-hl-comment-color)}.md-typeset .critic.comment:before{content:"/* "}.md-typeset .critic.comment:after{content:" */"}.md-typeset .critic.block{box-shadow:none;display:block;margin:1em 0;overflow:auto;padding-left:.8rem;padding-right:.8rem}.md-typeset .critic.block>:first-child{margin-top:.5em}.md-typeset .critic.block>:last-child{margin-bottom:.5em}:root{--md-details-icon:url('data:image/svg+xml;charset=utf-8,')}.md-typeset details{display:flow-root;overflow:visible;padding-top:0}.md-typeset details[open]>summary:after{transform:rotate(90deg)}.md-typeset details:not([open]){box-shadow:none;padding-bottom:0}.md-typeset details:not([open])>summary{border-radius:.1rem}[dir=ltr] .md-typeset summary{padding-right:1.8rem}[dir=rtl] .md-typeset summary{padding-left:1.8rem}[dir=ltr] .md-typeset summary{border-top-left-radius:.1rem}[dir=ltr] .md-typeset summary,[dir=rtl] .md-typeset summary{border-top-right-radius:.1rem}[dir=rtl] .md-typeset summary{border-top-left-radius:.1rem}.md-typeset summary{cursor:pointer;display:block;min-height:1rem;overflow:hidden}.md-typeset summary.focus-visible{outline-color:var(--md-accent-fg-color);outline-offset:.2rem}.md-typeset summary:not(.focus-visible){-webkit-tap-highlight-color:transparent;outline:none}[dir=ltr] .md-typeset summary:after{right:.4rem}[dir=rtl] .md-typeset summary:after{left:.4rem}.md-typeset summary:after{background-color:currentcolor;content:"";height:1rem;-webkit-mask-image:var(--md-details-icon);mask-image:var(--md-details-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;position:absolute;top:.625em;transform:rotate(0deg);transition:transform .25s;width:1rem}[dir=rtl] .md-typeset summary:after{transform:rotate(180deg)}.md-typeset summary::marker{display:none}.md-typeset summary::-webkit-details-marker{display:none}.md-typeset .emojione,.md-typeset .gemoji,.md-typeset .twemoji{--md-icon-size:1.125em;display:inline-flex;height:var(--md-icon-size);vertical-align:text-top}.md-typeset .emojione svg,.md-typeset .gemoji svg,.md-typeset .twemoji svg{fill:currentcolor;max-height:100%;width:var(--md-icon-size)}.md-typeset .lg,.md-typeset .xl,.md-typeset .xxl,.md-typeset .xxxl{vertical-align:text-bottom}.md-typeset .middle{vertical-align:middle}.md-typeset .lg{--md-icon-size:1.5em}.md-typeset .xl{--md-icon-size:2.25em}.md-typeset .xxl{--md-icon-size:3em}.md-typeset .xxxl{--md-icon-size:4em}.highlight .o,.highlight .ow{color:var(--md-code-hl-operator-color)}.highlight .p{color:var(--md-code-hl-punctuation-color)}.highlight .cpf,.highlight .l,.highlight .s,.highlight .s1,.highlight .s2,.highlight .sb,.highlight .sc,.highlight .si,.highlight .ss{color:var(--md-code-hl-string-color)}.highlight .cp,.highlight .se,.highlight .sh,.highlight .sr,.highlight .sx{color:var(--md-code-hl-special-color)}.highlight .il,.highlight .m,.highlight .mb,.highlight .mf,.highlight .mh,.highlight .mi,.highlight .mo{color:var(--md-code-hl-number-color)}.highlight .k,.highlight .kd,.highlight .kn,.highlight .kp,.highlight .kr,.highlight .kt{color:var(--md-code-hl-keyword-color)}.highlight .kc,.highlight .n{color:var(--md-code-hl-name-color)}.highlight .bp,.highlight .nb,.highlight .no{color:var(--md-code-hl-constant-color)}.highlight .nc,.highlight .ne,.highlight .nf,.highlight .nn{color:var(--md-code-hl-function-color)}.highlight .nd,.highlight .ni,.highlight .nl,.highlight .nt{color:var(--md-code-hl-keyword-color)}.highlight .c,.highlight .c1,.highlight .ch,.highlight .cm,.highlight .cs,.highlight .sd{color:var(--md-code-hl-comment-color)}.highlight .na,.highlight .nv,.highlight .vc,.highlight .vg,.highlight .vi{color:var(--md-code-hl-variable-color)}.highlight .ge,.highlight .gh,.highlight .go,.highlight .gp,.highlight .gr,.highlight .gs,.highlight .gt,.highlight .gu{color:var(--md-code-hl-generic-color)}.highlight .gd,.highlight .gi{border-radius:.1rem;margin:0 -.125em;padding:0 .125em}.highlight .gd{background-color:var(--md-typeset-del-color)}.highlight .gi{background-color:var(--md-typeset-ins-color)}.highlight .hll{background-color:var(--md-code-hl-color--light);box-shadow:2px 0 0 0 var(--md-code-hl-color) inset;display:block;margin:0 -1.1764705882em;padding:0 1.1764705882em}.highlight span.filename{background-color:var(--md-code-bg-color);border-bottom:.05rem solid var(--md-default-fg-color--lightest);border-top-left-radius:.1rem;border-top-right-radius:.1rem;display:flow-root;font-size:.85em;font-weight:700;margin-top:1em;padding:.6617647059em 1.1764705882em;position:relative}.highlight span.filename+pre{margin-top:0}.highlight span.filename+pre>code{border-top-left-radius:0;border-top-right-radius:0}.highlight [data-linenos]:before{background-color:var(--md-code-bg-color);box-shadow:-.05rem 0 var(--md-default-fg-color--lightest) inset;color:var(--md-default-fg-color--light);content:attr(data-linenos);float:left;left:-1.1764705882em;margin-left:-1.1764705882em;margin-right:1.1764705882em;padding-left:1.1764705882em;position:sticky;-webkit-user-select:none;user-select:none;z-index:3}.highlight code a[id]{position:absolute;visibility:hidden}.highlight code[data-md-copying]{display:initial}.highlight code[data-md-copying] .hll{display:contents}.highlight code[data-md-copying] .md-annotation{display:none}.highlighttable{display:flow-root}.highlighttable tbody,.highlighttable td{display:block;padding:0}.highlighttable tr{display:flex}.highlighttable pre{margin:0}.highlighttable th.filename{flex-grow:1;padding:0;text-align:left}.highlighttable th.filename span.filename{margin-top:0}.highlighttable .linenos{background-color:var(--md-code-bg-color);border-bottom-left-radius:.1rem;border-top-left-radius:.1rem;font-size:.85em;padding:.7720588235em 0 .7720588235em 1.1764705882em;-webkit-user-select:none;user-select:none}.highlighttable .linenodiv{box-shadow:-.05rem 0 var(--md-default-fg-color--lightest) inset}.highlighttable .linenodiv pre{color:var(--md-default-fg-color--light);text-align:right}.highlighttable .linenodiv span[class]{padding-right:.5882352941em}.highlighttable .code{flex:1;min-width:0}.linenodiv a{color:inherit}.md-typeset .highlighttable{direction:ltr;margin:1em 0}.md-typeset .highlighttable>tbody>tr>.code>div>pre>code{border-bottom-left-radius:0;border-top-left-radius:0}.md-typeset .highlight+.result{border:.05rem solid var(--md-code-bg-color);border-bottom-left-radius:.1rem;border-bottom-right-radius:.1rem;border-top-width:.1rem;margin-top:-1.125em;overflow:visible;padding:0 1em}.md-typeset .highlight+.result:after{clear:both;content:"";display:block}@media screen and (max-width:44.984375em){.md-content__inner>.highlight{margin:1em -.8rem}.md-content__inner>.highlight>.filename,.md-content__inner>.highlight>.highlighttable>tbody>tr>.code>div>pre>code,.md-content__inner>.highlight>.highlighttable>tbody>tr>.filename span.filename,.md-content__inner>.highlight>.highlighttable>tbody>tr>.linenos,.md-content__inner>.highlight>pre>code{border-radius:0}.md-content__inner>.highlight+.result{border-left-width:0;border-radius:0;border-right-width:0;margin-left:-.8rem;margin-right:-.8rem}}.md-typeset .keys kbd:after,.md-typeset .keys kbd:before{-moz-osx-font-smoothing:initial;-webkit-font-smoothing:initial;color:inherit;margin:0;position:relative}.md-typeset .keys span{color:var(--md-default-fg-color--light);padding:0 .2em}.md-typeset .keys .key-alt:before,.md-typeset .keys .key-left-alt:before,.md-typeset .keys .key-right-alt:before{content:"⎇";padding-right:.4em}.md-typeset .keys .key-command:before,.md-typeset .keys .key-left-command:before,.md-typeset .keys .key-right-command:before{content:"⌘";padding-right:.4em}.md-typeset .keys .key-control:before,.md-typeset .keys .key-left-control:before,.md-typeset .keys .key-right-control:before{content:"⌃";padding-right:.4em}.md-typeset .keys .key-left-meta:before,.md-typeset .keys .key-meta:before,.md-typeset .keys .key-right-meta:before{content:"◆";padding-right:.4em}.md-typeset .keys .key-left-option:before,.md-typeset .keys .key-option:before,.md-typeset .keys .key-right-option:before{content:"⌥";padding-right:.4em}.md-typeset .keys .key-left-shift:before,.md-typeset .keys .key-right-shift:before,.md-typeset .keys .key-shift:before{content:"⇧";padding-right:.4em}.md-typeset .keys .key-left-super:before,.md-typeset .keys .key-right-super:before,.md-typeset .keys .key-super:before{content:"❖";padding-right:.4em}.md-typeset .keys .key-left-windows:before,.md-typeset .keys .key-right-windows:before,.md-typeset .keys .key-windows:before{content:"⊞";padding-right:.4em}.md-typeset .keys .key-arrow-down:before{content:"↓";padding-right:.4em}.md-typeset .keys .key-arrow-left:before{content:"←";padding-right:.4em}.md-typeset .keys .key-arrow-right:before{content:"→";padding-right:.4em}.md-typeset .keys .key-arrow-up:before{content:"↑";padding-right:.4em}.md-typeset .keys .key-backspace:before{content:"⌫";padding-right:.4em}.md-typeset .keys .key-backtab:before{content:"⇤";padding-right:.4em}.md-typeset .keys .key-caps-lock:before{content:"⇪";padding-right:.4em}.md-typeset .keys .key-clear:before{content:"⌧";padding-right:.4em}.md-typeset .keys .key-context-menu:before{content:"☰";padding-right:.4em}.md-typeset .keys .key-delete:before{content:"⌦";padding-right:.4em}.md-typeset .keys .key-eject:before{content:"⏏";padding-right:.4em}.md-typeset .keys .key-end:before{content:"⤓";padding-right:.4em}.md-typeset .keys .key-escape:before{content:"⎋";padding-right:.4em}.md-typeset .keys .key-home:before{content:"⤒";padding-right:.4em}.md-typeset .keys .key-insert:before{content:"⎀";padding-right:.4em}.md-typeset .keys .key-page-down:before{content:"⇟";padding-right:.4em}.md-typeset .keys .key-page-up:before{content:"⇞";padding-right:.4em}.md-typeset .keys .key-print-screen:before{content:"⎙";padding-right:.4em}.md-typeset .keys .key-tab:after{content:"⇥";padding-left:.4em}.md-typeset .keys .key-num-enter:after{content:"⌤";padding-left:.4em}.md-typeset .keys .key-enter:after{content:"⏎";padding-left:.4em}:root{--md-tabbed-icon--prev:url('data:image/svg+xml;charset=utf-8,');--md-tabbed-icon--next:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .tabbed-set{border-radius:.1rem;display:flex;flex-flow:column wrap;margin:1em 0;position:relative}.md-typeset .tabbed-set>input{height:0;opacity:0;position:absolute;width:0}.md-typeset .tabbed-set>input:target{--md-scroll-offset:0.625em}.md-typeset .tabbed-set>input.focus-visible~.tabbed-labels:before{background-color:var(--md-accent-fg-color)}.md-typeset .tabbed-labels{-ms-overflow-style:none;box-shadow:0 -.05rem var(--md-default-fg-color--lightest) inset;display:flex;max-width:100%;overflow:auto;scrollbar-width:none}@media print{.md-typeset .tabbed-labels{display:contents}}@media screen{.js .md-typeset .tabbed-labels{position:relative}.js .md-typeset .tabbed-labels:before{background:var(--md-default-fg-color);bottom:0;content:"";display:block;height:2px;left:0;position:absolute;transform:translateX(var(--md-indicator-x));transition:width 225ms,background-color .25s,transform .25s;transition-timing-function:cubic-bezier(.4,0,.2,1);width:var(--md-indicator-width)}}.md-typeset .tabbed-labels::-webkit-scrollbar{display:none}.md-typeset .tabbed-labels>label{border-bottom:.1rem solid #0000;border-radius:.1rem .1rem 0 0;color:var(--md-default-fg-color--light);cursor:pointer;flex-shrink:0;font-size:.64rem;font-weight:700;padding:.78125em 1.25em .625em;scroll-margin-inline-start:1rem;transition:background-color .25s,color .25s;white-space:nowrap;width:auto}@media print{.md-typeset .tabbed-labels>label:first-child{order:1}.md-typeset .tabbed-labels>label:nth-child(2){order:2}.md-typeset .tabbed-labels>label:nth-child(3){order:3}.md-typeset .tabbed-labels>label:nth-child(4){order:4}.md-typeset .tabbed-labels>label:nth-child(5){order:5}.md-typeset .tabbed-labels>label:nth-child(6){order:6}.md-typeset .tabbed-labels>label:nth-child(7){order:7}.md-typeset .tabbed-labels>label:nth-child(8){order:8}.md-typeset .tabbed-labels>label:nth-child(9){order:9}.md-typeset .tabbed-labels>label:nth-child(10){order:10}.md-typeset .tabbed-labels>label:nth-child(11){order:11}.md-typeset .tabbed-labels>label:nth-child(12){order:12}.md-typeset .tabbed-labels>label:nth-child(13){order:13}.md-typeset .tabbed-labels>label:nth-child(14){order:14}.md-typeset .tabbed-labels>label:nth-child(15){order:15}.md-typeset .tabbed-labels>label:nth-child(16){order:16}.md-typeset .tabbed-labels>label:nth-child(17){order:17}.md-typeset .tabbed-labels>label:nth-child(18){order:18}.md-typeset .tabbed-labels>label:nth-child(19){order:19}.md-typeset .tabbed-labels>label:nth-child(20){order:20}}.md-typeset .tabbed-labels>label:hover{color:var(--md-default-fg-color)}.md-typeset .tabbed-labels>label>[href]:first-child{color:inherit}.md-typeset .tabbed-labels--linked>label{padding:0}.md-typeset .tabbed-labels--linked>label>a{display:block;padding:.78125em 1.25em .625em}.md-typeset .tabbed-content{width:100%}@media print{.md-typeset .tabbed-content{display:contents}}.md-typeset .tabbed-block{display:none}@media print{.md-typeset .tabbed-block{display:block}.md-typeset .tabbed-block:first-child{order:1}.md-typeset .tabbed-block:nth-child(2){order:2}.md-typeset .tabbed-block:nth-child(3){order:3}.md-typeset .tabbed-block:nth-child(4){order:4}.md-typeset .tabbed-block:nth-child(5){order:5}.md-typeset .tabbed-block:nth-child(6){order:6}.md-typeset .tabbed-block:nth-child(7){order:7}.md-typeset .tabbed-block:nth-child(8){order:8}.md-typeset .tabbed-block:nth-child(9){order:9}.md-typeset .tabbed-block:nth-child(10){order:10}.md-typeset .tabbed-block:nth-child(11){order:11}.md-typeset .tabbed-block:nth-child(12){order:12}.md-typeset .tabbed-block:nth-child(13){order:13}.md-typeset .tabbed-block:nth-child(14){order:14}.md-typeset .tabbed-block:nth-child(15){order:15}.md-typeset .tabbed-block:nth-child(16){order:16}.md-typeset .tabbed-block:nth-child(17){order:17}.md-typeset .tabbed-block:nth-child(18){order:18}.md-typeset .tabbed-block:nth-child(19){order:19}.md-typeset .tabbed-block:nth-child(20){order:20}}.md-typeset .tabbed-block>.highlight:first-child>pre,.md-typeset .tabbed-block>pre:first-child{margin:0}.md-typeset .tabbed-block>.highlight:first-child>pre>code,.md-typeset .tabbed-block>pre:first-child>code{border-top-left-radius:0;border-top-right-radius:0}.md-typeset .tabbed-block>.highlight:first-child>.filename{border-top-left-radius:0;border-top-right-radius:0;margin:0}.md-typeset .tabbed-block>.highlight:first-child>.highlighttable{margin:0}.md-typeset .tabbed-block>.highlight:first-child>.highlighttable>tbody>tr>.filename span.filename,.md-typeset .tabbed-block>.highlight:first-child>.highlighttable>tbody>tr>.linenos{border-top-left-radius:0;border-top-right-radius:0;margin:0}.md-typeset .tabbed-block>.highlight:first-child>.highlighttable>tbody>tr>.code>div>pre>code{border-top-left-radius:0;border-top-right-radius:0}.md-typeset .tabbed-block>.highlight:first-child+.result{margin-top:-.125em}.md-typeset .tabbed-block>.tabbed-set{margin:0}.md-typeset .tabbed-button{align-self:center;border-radius:100%;color:var(--md-default-fg-color--light);cursor:pointer;display:block;height:.9rem;margin-top:.1rem;pointer-events:auto;transition:background-color .25s;width:.9rem}.md-typeset .tabbed-button:hover{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}.md-typeset .tabbed-button:after{background-color:currentcolor;content:"";display:block;height:100%;-webkit-mask-image:var(--md-tabbed-icon--prev);mask-image:var(--md-tabbed-icon--prev);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;transition:background-color .25s,transform .25s;width:100%}.md-typeset .tabbed-control{background:linear-gradient(to right,var(--md-default-bg-color) 60%,#0000);display:flex;height:1.9rem;justify-content:start;pointer-events:none;position:absolute;transition:opacity 125ms;width:1.2rem}[dir=rtl] .md-typeset .tabbed-control{transform:rotate(180deg)}.md-typeset .tabbed-control[hidden]{opacity:0}.md-typeset .tabbed-control--next{background:linear-gradient(to left,var(--md-default-bg-color) 60%,#0000);justify-content:end;right:0}.md-typeset .tabbed-control--next .tabbed-button:after{-webkit-mask-image:var(--md-tabbed-icon--next);mask-image:var(--md-tabbed-icon--next)}@media screen and (max-width:44.984375em){[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels{padding-left:.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels{padding-right:.8rem}.md-content__inner>.tabbed-set .tabbed-labels{margin:0 -.8rem;max-width:100vw;scroll-padding-inline-start:.8rem}[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels:after{padding-right:.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels:after{padding-left:.8rem}.md-content__inner>.tabbed-set .tabbed-labels:after{content:""}[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--prev{padding-left:.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--prev{padding-right:.8rem}[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--prev{margin-left:-.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--prev{margin-right:-.8rem}.md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--prev{width:2rem}[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--next{padding-right:.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--next{padding-left:.8rem}[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--next{margin-right:-.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--next{margin-left:-.8rem}.md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--next{width:2rem}}@media screen{.md-typeset .tabbed-set>input:first-child:checked~.tabbed-labels>:first-child,.md-typeset .tabbed-set>input:nth-child(10):checked~.tabbed-labels>:nth-child(10),.md-typeset .tabbed-set>input:nth-child(11):checked~.tabbed-labels>:nth-child(11),.md-typeset .tabbed-set>input:nth-child(12):checked~.tabbed-labels>:nth-child(12),.md-typeset .tabbed-set>input:nth-child(13):checked~.tabbed-labels>:nth-child(13),.md-typeset .tabbed-set>input:nth-child(14):checked~.tabbed-labels>:nth-child(14),.md-typeset .tabbed-set>input:nth-child(15):checked~.tabbed-labels>:nth-child(15),.md-typeset .tabbed-set>input:nth-child(16):checked~.tabbed-labels>:nth-child(16),.md-typeset .tabbed-set>input:nth-child(17):checked~.tabbed-labels>:nth-child(17),.md-typeset .tabbed-set>input:nth-child(18):checked~.tabbed-labels>:nth-child(18),.md-typeset .tabbed-set>input:nth-child(19):checked~.tabbed-labels>:nth-child(19),.md-typeset .tabbed-set>input:nth-child(2):checked~.tabbed-labels>:nth-child(2),.md-typeset .tabbed-set>input:nth-child(20):checked~.tabbed-labels>:nth-child(20),.md-typeset .tabbed-set>input:nth-child(3):checked~.tabbed-labels>:nth-child(3),.md-typeset .tabbed-set>input:nth-child(4):checked~.tabbed-labels>:nth-child(4),.md-typeset .tabbed-set>input:nth-child(5):checked~.tabbed-labels>:nth-child(5),.md-typeset .tabbed-set>input:nth-child(6):checked~.tabbed-labels>:nth-child(6),.md-typeset .tabbed-set>input:nth-child(7):checked~.tabbed-labels>:nth-child(7),.md-typeset .tabbed-set>input:nth-child(8):checked~.tabbed-labels>:nth-child(8),.md-typeset .tabbed-set>input:nth-child(9):checked~.tabbed-labels>:nth-child(9){color:var(--md-default-fg-color)}.md-typeset .no-js .tabbed-set>input:first-child:checked~.tabbed-labels>:first-child,.md-typeset .no-js .tabbed-set>input:nth-child(10):checked~.tabbed-labels>:nth-child(10),.md-typeset .no-js .tabbed-set>input:nth-child(11):checked~.tabbed-labels>:nth-child(11),.md-typeset .no-js .tabbed-set>input:nth-child(12):checked~.tabbed-labels>:nth-child(12),.md-typeset .no-js .tabbed-set>input:nth-child(13):checked~.tabbed-labels>:nth-child(13),.md-typeset .no-js .tabbed-set>input:nth-child(14):checked~.tabbed-labels>:nth-child(14),.md-typeset .no-js .tabbed-set>input:nth-child(15):checked~.tabbed-labels>:nth-child(15),.md-typeset .no-js .tabbed-set>input:nth-child(16):checked~.tabbed-labels>:nth-child(16),.md-typeset .no-js .tabbed-set>input:nth-child(17):checked~.tabbed-labels>:nth-child(17),.md-typeset .no-js .tabbed-set>input:nth-child(18):checked~.tabbed-labels>:nth-child(18),.md-typeset .no-js .tabbed-set>input:nth-child(19):checked~.tabbed-labels>:nth-child(19),.md-typeset .no-js .tabbed-set>input:nth-child(2):checked~.tabbed-labels>:nth-child(2),.md-typeset .no-js .tabbed-set>input:nth-child(20):checked~.tabbed-labels>:nth-child(20),.md-typeset .no-js .tabbed-set>input:nth-child(3):checked~.tabbed-labels>:nth-child(3),.md-typeset .no-js .tabbed-set>input:nth-child(4):checked~.tabbed-labels>:nth-child(4),.md-typeset .no-js .tabbed-set>input:nth-child(5):checked~.tabbed-labels>:nth-child(5),.md-typeset .no-js .tabbed-set>input:nth-child(6):checked~.tabbed-labels>:nth-child(6),.md-typeset .no-js .tabbed-set>input:nth-child(7):checked~.tabbed-labels>:nth-child(7),.md-typeset .no-js .tabbed-set>input:nth-child(8):checked~.tabbed-labels>:nth-child(8),.md-typeset .no-js .tabbed-set>input:nth-child(9):checked~.tabbed-labels>:nth-child(9),.md-typeset [role=dialog] .tabbed-set>input:first-child:checked~.tabbed-labels>:first-child,.md-typeset [role=dialog] .tabbed-set>input:nth-child(10):checked~.tabbed-labels>:nth-child(10),.md-typeset [role=dialog] .tabbed-set>input:nth-child(11):checked~.tabbed-labels>:nth-child(11),.md-typeset [role=dialog] .tabbed-set>input:nth-child(12):checked~.tabbed-labels>:nth-child(12),.md-typeset [role=dialog] .tabbed-set>input:nth-child(13):checked~.tabbed-labels>:nth-child(13),.md-typeset [role=dialog] .tabbed-set>input:nth-child(14):checked~.tabbed-labels>:nth-child(14),.md-typeset [role=dialog] .tabbed-set>input:nth-child(15):checked~.tabbed-labels>:nth-child(15),.md-typeset [role=dialog] .tabbed-set>input:nth-child(16):checked~.tabbed-labels>:nth-child(16),.md-typeset [role=dialog] .tabbed-set>input:nth-child(17):checked~.tabbed-labels>:nth-child(17),.md-typeset [role=dialog] .tabbed-set>input:nth-child(18):checked~.tabbed-labels>:nth-child(18),.md-typeset [role=dialog] .tabbed-set>input:nth-child(19):checked~.tabbed-labels>:nth-child(19),.md-typeset [role=dialog] .tabbed-set>input:nth-child(2):checked~.tabbed-labels>:nth-child(2),.md-typeset [role=dialog] .tabbed-set>input:nth-child(20):checked~.tabbed-labels>:nth-child(20),.md-typeset [role=dialog] .tabbed-set>input:nth-child(3):checked~.tabbed-labels>:nth-child(3),.md-typeset [role=dialog] .tabbed-set>input:nth-child(4):checked~.tabbed-labels>:nth-child(4),.md-typeset [role=dialog] .tabbed-set>input:nth-child(5):checked~.tabbed-labels>:nth-child(5),.md-typeset [role=dialog] .tabbed-set>input:nth-child(6):checked~.tabbed-labels>:nth-child(6),.md-typeset [role=dialog] .tabbed-set>input:nth-child(7):checked~.tabbed-labels>:nth-child(7),.md-typeset [role=dialog] .tabbed-set>input:nth-child(8):checked~.tabbed-labels>:nth-child(8),.md-typeset [role=dialog] .tabbed-set>input:nth-child(9):checked~.tabbed-labels>:nth-child(9),.no-js .md-typeset .tabbed-set>input:first-child:checked~.tabbed-labels>:first-child,.no-js .md-typeset .tabbed-set>input:nth-child(10):checked~.tabbed-labels>:nth-child(10),.no-js .md-typeset .tabbed-set>input:nth-child(11):checked~.tabbed-labels>:nth-child(11),.no-js .md-typeset .tabbed-set>input:nth-child(12):checked~.tabbed-labels>:nth-child(12),.no-js .md-typeset .tabbed-set>input:nth-child(13):checked~.tabbed-labels>:nth-child(13),.no-js .md-typeset .tabbed-set>input:nth-child(14):checked~.tabbed-labels>:nth-child(14),.no-js .md-typeset .tabbed-set>input:nth-child(15):checked~.tabbed-labels>:nth-child(15),.no-js .md-typeset .tabbed-set>input:nth-child(16):checked~.tabbed-labels>:nth-child(16),.no-js .md-typeset .tabbed-set>input:nth-child(17):checked~.tabbed-labels>:nth-child(17),.no-js .md-typeset .tabbed-set>input:nth-child(18):checked~.tabbed-labels>:nth-child(18),.no-js .md-typeset .tabbed-set>input:nth-child(19):checked~.tabbed-labels>:nth-child(19),.no-js .md-typeset .tabbed-set>input:nth-child(2):checked~.tabbed-labels>:nth-child(2),.no-js .md-typeset .tabbed-set>input:nth-child(20):checked~.tabbed-labels>:nth-child(20),.no-js .md-typeset .tabbed-set>input:nth-child(3):checked~.tabbed-labels>:nth-child(3),.no-js .md-typeset .tabbed-set>input:nth-child(4):checked~.tabbed-labels>:nth-child(4),.no-js .md-typeset .tabbed-set>input:nth-child(5):checked~.tabbed-labels>:nth-child(5),.no-js .md-typeset .tabbed-set>input:nth-child(6):checked~.tabbed-labels>:nth-child(6),.no-js .md-typeset .tabbed-set>input:nth-child(7):checked~.tabbed-labels>:nth-child(7),.no-js .md-typeset .tabbed-set>input:nth-child(8):checked~.tabbed-labels>:nth-child(8),.no-js .md-typeset .tabbed-set>input:nth-child(9):checked~.tabbed-labels>:nth-child(9),[role=dialog] .md-typeset .tabbed-set>input:first-child:checked~.tabbed-labels>:first-child,[role=dialog] .md-typeset .tabbed-set>input:nth-child(10):checked~.tabbed-labels>:nth-child(10),[role=dialog] .md-typeset .tabbed-set>input:nth-child(11):checked~.tabbed-labels>:nth-child(11),[role=dialog] .md-typeset .tabbed-set>input:nth-child(12):checked~.tabbed-labels>:nth-child(12),[role=dialog] .md-typeset .tabbed-set>input:nth-child(13):checked~.tabbed-labels>:nth-child(13),[role=dialog] .md-typeset .tabbed-set>input:nth-child(14):checked~.tabbed-labels>:nth-child(14),[role=dialog] .md-typeset .tabbed-set>input:nth-child(15):checked~.tabbed-labels>:nth-child(15),[role=dialog] .md-typeset .tabbed-set>input:nth-child(16):checked~.tabbed-labels>:nth-child(16),[role=dialog] .md-typeset .tabbed-set>input:nth-child(17):checked~.tabbed-labels>:nth-child(17),[role=dialog] .md-typeset .tabbed-set>input:nth-child(18):checked~.tabbed-labels>:nth-child(18),[role=dialog] .md-typeset .tabbed-set>input:nth-child(19):checked~.tabbed-labels>:nth-child(19),[role=dialog] .md-typeset .tabbed-set>input:nth-child(2):checked~.tabbed-labels>:nth-child(2),[role=dialog] .md-typeset .tabbed-set>input:nth-child(20):checked~.tabbed-labels>:nth-child(20),[role=dialog] .md-typeset .tabbed-set>input:nth-child(3):checked~.tabbed-labels>:nth-child(3),[role=dialog] .md-typeset .tabbed-set>input:nth-child(4):checked~.tabbed-labels>:nth-child(4),[role=dialog] .md-typeset .tabbed-set>input:nth-child(5):checked~.tabbed-labels>:nth-child(5),[role=dialog] .md-typeset .tabbed-set>input:nth-child(6):checked~.tabbed-labels>:nth-child(6),[role=dialog] .md-typeset .tabbed-set>input:nth-child(7):checked~.tabbed-labels>:nth-child(7),[role=dialog] .md-typeset .tabbed-set>input:nth-child(8):checked~.tabbed-labels>:nth-child(8),[role=dialog] .md-typeset .tabbed-set>input:nth-child(9):checked~.tabbed-labels>:nth-child(9){border-color:var(--md-default-fg-color)}}.md-typeset .tabbed-set>input:first-child.focus-visible~.tabbed-labels>:first-child,.md-typeset .tabbed-set>input:nth-child(10).focus-visible~.tabbed-labels>:nth-child(10),.md-typeset .tabbed-set>input:nth-child(11).focus-visible~.tabbed-labels>:nth-child(11),.md-typeset .tabbed-set>input:nth-child(12).focus-visible~.tabbed-labels>:nth-child(12),.md-typeset .tabbed-set>input:nth-child(13).focus-visible~.tabbed-labels>:nth-child(13),.md-typeset .tabbed-set>input:nth-child(14).focus-visible~.tabbed-labels>:nth-child(14),.md-typeset .tabbed-set>input:nth-child(15).focus-visible~.tabbed-labels>:nth-child(15),.md-typeset .tabbed-set>input:nth-child(16).focus-visible~.tabbed-labels>:nth-child(16),.md-typeset .tabbed-set>input:nth-child(17).focus-visible~.tabbed-labels>:nth-child(17),.md-typeset .tabbed-set>input:nth-child(18).focus-visible~.tabbed-labels>:nth-child(18),.md-typeset .tabbed-set>input:nth-child(19).focus-visible~.tabbed-labels>:nth-child(19),.md-typeset .tabbed-set>input:nth-child(2).focus-visible~.tabbed-labels>:nth-child(2),.md-typeset .tabbed-set>input:nth-child(20).focus-visible~.tabbed-labels>:nth-child(20),.md-typeset .tabbed-set>input:nth-child(3).focus-visible~.tabbed-labels>:nth-child(3),.md-typeset .tabbed-set>input:nth-child(4).focus-visible~.tabbed-labels>:nth-child(4),.md-typeset .tabbed-set>input:nth-child(5).focus-visible~.tabbed-labels>:nth-child(5),.md-typeset .tabbed-set>input:nth-child(6).focus-visible~.tabbed-labels>:nth-child(6),.md-typeset .tabbed-set>input:nth-child(7).focus-visible~.tabbed-labels>:nth-child(7),.md-typeset .tabbed-set>input:nth-child(8).focus-visible~.tabbed-labels>:nth-child(8),.md-typeset .tabbed-set>input:nth-child(9).focus-visible~.tabbed-labels>:nth-child(9){color:var(--md-accent-fg-color)}.md-typeset .tabbed-set>input:first-child:checked~.tabbed-content>:first-child,.md-typeset .tabbed-set>input:nth-child(10):checked~.tabbed-content>:nth-child(10),.md-typeset .tabbed-set>input:nth-child(11):checked~.tabbed-content>:nth-child(11),.md-typeset .tabbed-set>input:nth-child(12):checked~.tabbed-content>:nth-child(12),.md-typeset .tabbed-set>input:nth-child(13):checked~.tabbed-content>:nth-child(13),.md-typeset .tabbed-set>input:nth-child(14):checked~.tabbed-content>:nth-child(14),.md-typeset .tabbed-set>input:nth-child(15):checked~.tabbed-content>:nth-child(15),.md-typeset .tabbed-set>input:nth-child(16):checked~.tabbed-content>:nth-child(16),.md-typeset .tabbed-set>input:nth-child(17):checked~.tabbed-content>:nth-child(17),.md-typeset .tabbed-set>input:nth-child(18):checked~.tabbed-content>:nth-child(18),.md-typeset .tabbed-set>input:nth-child(19):checked~.tabbed-content>:nth-child(19),.md-typeset .tabbed-set>input:nth-child(2):checked~.tabbed-content>:nth-child(2),.md-typeset .tabbed-set>input:nth-child(20):checked~.tabbed-content>:nth-child(20),.md-typeset .tabbed-set>input:nth-child(3):checked~.tabbed-content>:nth-child(3),.md-typeset .tabbed-set>input:nth-child(4):checked~.tabbed-content>:nth-child(4),.md-typeset .tabbed-set>input:nth-child(5):checked~.tabbed-content>:nth-child(5),.md-typeset .tabbed-set>input:nth-child(6):checked~.tabbed-content>:nth-child(6),.md-typeset .tabbed-set>input:nth-child(7):checked~.tabbed-content>:nth-child(7),.md-typeset .tabbed-set>input:nth-child(8):checked~.tabbed-content>:nth-child(8),.md-typeset .tabbed-set>input:nth-child(9):checked~.tabbed-content>:nth-child(9){display:block}:root{--md-tasklist-icon:url('data:image/svg+xml;charset=utf-8,');--md-tasklist-icon--checked:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .task-list-item{list-style-type:none;position:relative}[dir=ltr] .md-typeset .task-list-item [type=checkbox]{left:-2em}[dir=rtl] .md-typeset .task-list-item [type=checkbox]{right:-2em}.md-typeset .task-list-item [type=checkbox]{position:absolute;top:.45em}.md-typeset .task-list-control [type=checkbox]{opacity:0;z-index:-1}[dir=ltr] .md-typeset .task-list-indicator:before{left:-1.5em}[dir=rtl] .md-typeset .task-list-indicator:before{right:-1.5em}.md-typeset .task-list-indicator:before{background-color:var(--md-default-fg-color--lightest);content:"";height:1.25em;-webkit-mask-image:var(--md-tasklist-icon);mask-image:var(--md-tasklist-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;position:absolute;top:.15em;width:1.25em}.md-typeset [type=checkbox]:checked+.task-list-indicator:before{background-color:#00e676;-webkit-mask-image:var(--md-tasklist-icon--checked);mask-image:var(--md-tasklist-icon--checked)}@media print{.giscus,[id=__comments]{display:none}}:root>*{--md-mermaid-font-family:var(--md-text-font-family),sans-serif;--md-mermaid-edge-color:var(--md-code-fg-color);--md-mermaid-node-bg-color:var(--md-accent-fg-color--transparent);--md-mermaid-node-fg-color:var(--md-accent-fg-color);--md-mermaid-label-bg-color:var(--md-default-bg-color);--md-mermaid-label-fg-color:var(--md-code-fg-color);--md-mermaid-sequence-actor-bg-color:var(--md-mermaid-label-bg-color);--md-mermaid-sequence-actor-fg-color:var(--md-mermaid-label-fg-color);--md-mermaid-sequence-actor-border-color:var(--md-mermaid-node-fg-color);--md-mermaid-sequence-actor-line-color:var(--md-default-fg-color--lighter);--md-mermaid-sequence-actorman-bg-color:var(--md-mermaid-label-bg-color);--md-mermaid-sequence-actorman-line-color:var(--md-mermaid-node-fg-color);--md-mermaid-sequence-box-bg-color:var(--md-mermaid-node-bg-color);--md-mermaid-sequence-box-fg-color:var(--md-mermaid-edge-color);--md-mermaid-sequence-label-bg-color:var(--md-mermaid-node-bg-color);--md-mermaid-sequence-label-fg-color:var(--md-mermaid-node-fg-color);--md-mermaid-sequence-loop-bg-color:var(--md-mermaid-node-bg-color);--md-mermaid-sequence-loop-fg-color:var(--md-mermaid-edge-color);--md-mermaid-sequence-loop-border-color:var(--md-mermaid-node-fg-color);--md-mermaid-sequence-message-fg-color:var(--md-mermaid-edge-color);--md-mermaid-sequence-message-line-color:var(--md-mermaid-edge-color);--md-mermaid-sequence-note-bg-color:var(--md-mermaid-label-bg-color);--md-mermaid-sequence-note-fg-color:var(--md-mermaid-edge-color);--md-mermaid-sequence-note-border-color:var(--md-mermaid-label-fg-color);--md-mermaid-sequence-number-bg-color:var(--md-mermaid-node-fg-color);--md-mermaid-sequence-number-fg-color:var(--md-accent-bg-color)}.mermaid{line-height:normal;margin:1em 0}.md-typeset .grid{grid-gap:.4rem;display:grid;grid-template-columns:repeat(auto-fit,minmax(min(100%,16rem),1fr));margin:1em 0}.md-typeset .grid.cards>ol,.md-typeset .grid.cards>ul{display:contents}.md-typeset .grid.cards>ol>li,.md-typeset .grid.cards>ul>li,.md-typeset .grid>.card{border:.05rem solid var(--md-default-fg-color--lightest);border-radius:.1rem;display:block;margin:0;padding:.8rem;transition:border .25s,box-shadow .25s}.md-typeset .grid.cards>ol>li:focus-within,.md-typeset .grid.cards>ol>li:hover,.md-typeset .grid.cards>ul>li:focus-within,.md-typeset .grid.cards>ul>li:hover,.md-typeset .grid>.card:focus-within,.md-typeset .grid>.card:hover{border-color:#0000;box-shadow:var(--md-shadow-z2)}.md-typeset .grid.cards>ol>li>hr,.md-typeset .grid.cards>ul>li>hr,.md-typeset .grid>.card>hr{margin-bottom:1em;margin-top:1em}.md-typeset .grid.cards>ol>li>:first-child,.md-typeset .grid.cards>ul>li>:first-child,.md-typeset .grid>.card>:first-child{margin-top:0}.md-typeset .grid.cards>ol>li>:last-child,.md-typeset .grid.cards>ul>li>:last-child,.md-typeset .grid>.card>:last-child{margin-bottom:0}.md-typeset .grid>*,.md-typeset .grid>.admonition,.md-typeset .grid>.highlight>*,.md-typeset .grid>.highlighttable,.md-typeset .grid>.md-typeset details,.md-typeset .grid>details,.md-typeset .grid>pre{margin-bottom:0;margin-top:0}.md-typeset .grid>.highlight>pre:only-child,.md-typeset .grid>.highlight>pre>code,.md-typeset .grid>.highlighttable,.md-typeset .grid>.highlighttable>tbody,.md-typeset .grid>.highlighttable>tbody>tr,.md-typeset .grid>.highlighttable>tbody>tr>.code,.md-typeset .grid>.highlighttable>tbody>tr>.code>.highlight,.md-typeset .grid>.highlighttable>tbody>tr>.code>.highlight>pre,.md-typeset .grid>.highlighttable>tbody>tr>.code>.highlight>pre>code{height:100%}.md-typeset .grid>.tabbed-set{margin-bottom:0;margin-top:0}@media screen and (min-width:45em){[dir=ltr] .md-typeset .inline{float:left}[dir=rtl] .md-typeset .inline{float:right}[dir=ltr] .md-typeset .inline{margin-right:.8rem}[dir=rtl] .md-typeset .inline{margin-left:.8rem}.md-typeset .inline{margin-bottom:.8rem;margin-top:0;width:11.7rem}[dir=ltr] .md-typeset .inline.end{float:right}[dir=rtl] .md-typeset .inline.end{float:left}[dir=ltr] .md-typeset .inline.end{margin-left:.8rem;margin-right:0}[dir=rtl] .md-typeset .inline.end{margin-left:0;margin-right:.8rem}} -/*! update cache: 20260104155751 */ +/*! update cache: 20260104161450 */ diff --git a/en/assets/stylesheets/palette.ab4e12ef.min.css b/en/assets/stylesheets/palette.ab4e12ef.min.css index 4d9007f86..e2effea03 100644 --- a/en/assets/stylesheets/palette.ab4e12ef.min.css +++ b/en/assets/stylesheets/palette.ab4e12ef.min.css @@ -1,2 +1,2 @@ @media screen{[data-md-color-scheme=slate]{--md-default-fg-color:hsla(var(--md-hue),15%,90%,0.82);--md-default-fg-color--light:hsla(var(--md-hue),15%,90%,0.56);--md-default-fg-color--lighter:hsla(var(--md-hue),15%,90%,0.32);--md-default-fg-color--lightest:hsla(var(--md-hue),15%,90%,0.12);--md-default-bg-color:hsla(var(--md-hue),15%,14%,1);--md-default-bg-color--light:hsla(var(--md-hue),15%,14%,0.54);--md-default-bg-color--lighter:hsla(var(--md-hue),15%,14%,0.26);--md-default-bg-color--lightest:hsla(var(--md-hue),15%,14%,0.07);--md-code-fg-color:hsla(var(--md-hue),18%,86%,0.82);--md-code-bg-color:hsla(var(--md-hue),15%,18%,1);--md-code-bg-color--light:hsla(var(--md-hue),15%,18%,0.9);--md-code-bg-color--lighter:hsla(var(--md-hue),15%,18%,0.54);--md-code-hl-color:#2977ff;--md-code-hl-color--light:#2977ff1a;--md-code-hl-number-color:#e6695b;--md-code-hl-special-color:#f06090;--md-code-hl-function-color:#c973d9;--md-code-hl-constant-color:#9383e2;--md-code-hl-keyword-color:#6791e0;--md-code-hl-string-color:#2fb170;--md-code-hl-name-color:var(--md-code-fg-color);--md-code-hl-operator-color:var(--md-default-fg-color--light);--md-code-hl-punctuation-color:var(--md-default-fg-color--light);--md-code-hl-comment-color:var(--md-default-fg-color--light);--md-code-hl-generic-color:var(--md-default-fg-color--light);--md-code-hl-variable-color:var(--md-default-fg-color--light);--md-typeset-color:var(--md-default-fg-color);--md-typeset-a-color:var(--md-primary-fg-color);--md-typeset-kbd-color:hsla(var(--md-hue),15%,90%,0.12);--md-typeset-kbd-accent-color:hsla(var(--md-hue),15%,90%,0.2);--md-typeset-kbd-border-color:hsla(var(--md-hue),15%,14%,1);--md-typeset-mark-color:#4287ff4d;--md-typeset-table-color:hsla(var(--md-hue),15%,95%,0.12);--md-typeset-table-color--light:hsla(var(--md-hue),15%,95%,0.035);--md-admonition-fg-color:var(--md-default-fg-color);--md-admonition-bg-color:var(--md-default-bg-color);--md-footer-bg-color:hsla(var(--md-hue),15%,10%,0.87);--md-footer-bg-color--dark:hsla(var(--md-hue),15%,8%,1);--md-shadow-z1:0 0.2rem 0.5rem #0000000d,0 0 0.05rem #0000001a;--md-shadow-z2:0 0.2rem 0.5rem #00000040,0 0 0.05rem #00000040;--md-shadow-z3:0 0.2rem 0.5rem #0006,0 0 0.05rem #00000059;color-scheme:dark}[data-md-color-scheme=slate] img[src$="#gh-light-mode-only"],[data-md-color-scheme=slate] img[src$="#only-light"]{display:none}[data-md-color-scheme=slate][data-md-color-primary=pink]{--md-typeset-a-color:#ed5487}[data-md-color-scheme=slate][data-md-color-primary=purple]{--md-typeset-a-color:#c46fd3}[data-md-color-scheme=slate][data-md-color-primary=deep-purple]{--md-typeset-a-color:#a47bea}[data-md-color-scheme=slate][data-md-color-primary=indigo]{--md-typeset-a-color:#5488e8}[data-md-color-scheme=slate][data-md-color-primary=teal]{--md-typeset-a-color:#00ccb8}[data-md-color-scheme=slate][data-md-color-primary=green]{--md-typeset-a-color:#71c174}[data-md-color-scheme=slate][data-md-color-primary=deep-orange]{--md-typeset-a-color:#ff764d}[data-md-color-scheme=slate][data-md-color-primary=brown]{--md-typeset-a-color:#c1775c}[data-md-color-scheme=slate][data-md-color-primary=black],[data-md-color-scheme=slate][data-md-color-primary=blue-grey],[data-md-color-scheme=slate][data-md-color-primary=grey],[data-md-color-scheme=slate][data-md-color-primary=white]{--md-typeset-a-color:#5e8bde}[data-md-color-switching] *,[data-md-color-switching] :after,[data-md-color-switching] :before{transition-duration:0ms!important}}[data-md-color-accent=red]{--md-accent-fg-color:#ff1947;--md-accent-fg-color--transparent:#ff19471a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=pink]{--md-accent-fg-color:#f50056;--md-accent-fg-color--transparent:#f500561a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=purple]{--md-accent-fg-color:#df41fb;--md-accent-fg-color--transparent:#df41fb1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=deep-purple]{--md-accent-fg-color:#7c4dff;--md-accent-fg-color--transparent:#7c4dff1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=indigo]{--md-accent-fg-color:#526cfe;--md-accent-fg-color--transparent:#526cfe1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=blue]{--md-accent-fg-color:#4287ff;--md-accent-fg-color--transparent:#4287ff1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=light-blue]{--md-accent-fg-color:#0091eb;--md-accent-fg-color--transparent:#0091eb1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=cyan]{--md-accent-fg-color:#00bad6;--md-accent-fg-color--transparent:#00bad61a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=teal]{--md-accent-fg-color:#00bda4;--md-accent-fg-color--transparent:#00bda41a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=green]{--md-accent-fg-color:#00c753;--md-accent-fg-color--transparent:#00c7531a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=light-green]{--md-accent-fg-color:#63de17;--md-accent-fg-color--transparent:#63de171a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=lime]{--md-accent-fg-color:#b0eb00;--md-accent-fg-color--transparent:#b0eb001a;--md-accent-bg-color:#000000de;--md-accent-bg-color--light:#0000008a}[data-md-color-accent=yellow]{--md-accent-fg-color:#ffd500;--md-accent-fg-color--transparent:#ffd5001a;--md-accent-bg-color:#000000de;--md-accent-bg-color--light:#0000008a}[data-md-color-accent=amber]{--md-accent-fg-color:#fa0;--md-accent-fg-color--transparent:#ffaa001a;--md-accent-bg-color:#000000de;--md-accent-bg-color--light:#0000008a}[data-md-color-accent=orange]{--md-accent-fg-color:#ff9100;--md-accent-fg-color--transparent:#ff91001a;--md-accent-bg-color:#000000de;--md-accent-bg-color--light:#0000008a}[data-md-color-accent=deep-orange]{--md-accent-fg-color:#ff6e42;--md-accent-fg-color--transparent:#ff6e421a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-primary=red]{--md-primary-fg-color:#ef5552;--md-primary-fg-color--light:#e57171;--md-primary-fg-color--dark:#e53734;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=pink]{--md-primary-fg-color:#e92063;--md-primary-fg-color--light:#ec417a;--md-primary-fg-color--dark:#c3185d;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=purple]{--md-primary-fg-color:#ab47bd;--md-primary-fg-color--light:#bb69c9;--md-primary-fg-color--dark:#8c24a8;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=deep-purple]{--md-primary-fg-color:#7e56c2;--md-primary-fg-color--light:#9574cd;--md-primary-fg-color--dark:#673ab6;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=indigo]{--md-primary-fg-color:#4051b5;--md-primary-fg-color--light:#5d6cc0;--md-primary-fg-color--dark:#303fa1;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=blue]{--md-primary-fg-color:#2094f3;--md-primary-fg-color--light:#42a5f5;--md-primary-fg-color--dark:#1975d2;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=light-blue]{--md-primary-fg-color:#02a6f2;--md-primary-fg-color--light:#28b5f6;--md-primary-fg-color--dark:#0287cf;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=cyan]{--md-primary-fg-color:#00bdd6;--md-primary-fg-color--light:#25c5da;--md-primary-fg-color--dark:#0097a8;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=teal]{--md-primary-fg-color:#009485;--md-primary-fg-color--light:#26a699;--md-primary-fg-color--dark:#007a6c;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=green]{--md-primary-fg-color:#4cae4f;--md-primary-fg-color--light:#68bb6c;--md-primary-fg-color--dark:#398e3d;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=light-green]{--md-primary-fg-color:#8bc34b;--md-primary-fg-color--light:#9ccc66;--md-primary-fg-color--dark:#689f38;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=lime]{--md-primary-fg-color:#cbdc38;--md-primary-fg-color--light:#d3e156;--md-primary-fg-color--dark:#b0b52c;--md-primary-bg-color:#000000de;--md-primary-bg-color--light:#0000008a}[data-md-color-primary=yellow]{--md-primary-fg-color:#ffec3d;--md-primary-fg-color--light:#ffee57;--md-primary-fg-color--dark:#fbc02d;--md-primary-bg-color:#000000de;--md-primary-bg-color--light:#0000008a}[data-md-color-primary=amber]{--md-primary-fg-color:#ffc105;--md-primary-fg-color--light:#ffc929;--md-primary-fg-color--dark:#ffa200;--md-primary-bg-color:#000000de;--md-primary-bg-color--light:#0000008a}[data-md-color-primary=orange]{--md-primary-fg-color:#ffa724;--md-primary-fg-color--light:#ffa724;--md-primary-fg-color--dark:#fa8900;--md-primary-bg-color:#000000de;--md-primary-bg-color--light:#0000008a}[data-md-color-primary=deep-orange]{--md-primary-fg-color:#ff6e42;--md-primary-fg-color--light:#ff8a66;--md-primary-fg-color--dark:#f4511f;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=brown]{--md-primary-fg-color:#795649;--md-primary-fg-color--light:#8d6e62;--md-primary-fg-color--dark:#5d4037;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=grey]{--md-primary-fg-color:#757575;--md-primary-fg-color--light:#9e9e9e;--md-primary-fg-color--dark:#616161;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3;--md-typeset-a-color:#4051b5}[data-md-color-primary=blue-grey]{--md-primary-fg-color:#546d78;--md-primary-fg-color--light:#607c8a;--md-primary-fg-color--dark:#455a63;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3;--md-typeset-a-color:#4051b5}[data-md-color-primary=light-green]:not([data-md-color-scheme=slate]){--md-typeset-a-color:#72ad2e}[data-md-color-primary=lime]:not([data-md-color-scheme=slate]){--md-typeset-a-color:#8b990a}[data-md-color-primary=yellow]:not([data-md-color-scheme=slate]){--md-typeset-a-color:#b8a500}[data-md-color-primary=amber]:not([data-md-color-scheme=slate]){--md-typeset-a-color:#d19d00}[data-md-color-primary=orange]:not([data-md-color-scheme=slate]){--md-typeset-a-color:#e68a00}[data-md-color-primary=white]{--md-primary-fg-color:hsla(var(--md-hue),0%,100%,1);--md-primary-fg-color--light:hsla(var(--md-hue),0%,100%,0.7);--md-primary-fg-color--dark:hsla(var(--md-hue),0%,0%,0.07);--md-primary-bg-color:hsla(var(--md-hue),0%,0%,0.87);--md-primary-bg-color--light:hsla(var(--md-hue),0%,0%,0.54);--md-typeset-a-color:#4051b5}[data-md-color-primary=white] .md-button{color:var(--md-typeset-a-color)}[data-md-color-primary=white] .md-button--primary{background-color:var(--md-typeset-a-color);border-color:var(--md-typeset-a-color);color:hsla(var(--md-hue),0%,100%,1)}@media screen and (min-width:60em){[data-md-color-primary=white] .md-search__form{background-color:hsla(var(--md-hue),0%,0%,.07)}[data-md-color-primary=white] .md-search__form:hover{background-color:hsla(var(--md-hue),0%,0%,.32)}[data-md-color-primary=white] .md-search__input+.md-search__icon{color:hsla(var(--md-hue),0%,0%,.87)}}@media screen and (min-width:76.25em){[data-md-color-primary=white] .md-tabs{border-bottom:.05rem solid #00000012}}[data-md-color-primary=black]{--md-primary-fg-color:hsla(var(--md-hue),15%,9%,1);--md-primary-fg-color--light:hsla(var(--md-hue),15%,9%,0.54);--md-primary-fg-color--dark:hsla(var(--md-hue),15%,9%,1);--md-primary-bg-color:hsla(var(--md-hue),15%,100%,1);--md-primary-bg-color--light:hsla(var(--md-hue),15%,100%,0.7);--md-typeset-a-color:#4051b5}[data-md-color-primary=black] .md-button{color:var(--md-typeset-a-color)}[data-md-color-primary=black] .md-button--primary{background-color:var(--md-typeset-a-color);border-color:var(--md-typeset-a-color);color:hsla(var(--md-hue),0%,100%,1)}[data-md-color-primary=black] .md-header{background-color:hsla(var(--md-hue),15%,9%,1)}@media screen and (max-width:59.984375em){[data-md-color-primary=black] .md-nav__source{background-color:hsla(var(--md-hue),15%,11%,.87)}}@media screen and (max-width:76.234375em){html [data-md-color-primary=black] .md-nav--primary .md-nav__title[for=__drawer]{background-color:hsla(var(--md-hue),15%,9%,1)}}@media screen and (min-width:76.25em){[data-md-color-primary=black] .md-tabs{background-color:hsla(var(--md-hue),15%,9%,1)}} -/*! update cache: 20260104155751 */ +/*! update cache: 20260104161450 */ diff --git a/en/chapter_preface/about_the_book/index.html b/en/chapter_preface/about_the_book/index.html index 6bb7938d4..92fa962a1 100644 --- a/en/chapter_preface/about_the_book/index.html +++ b/en/chapter_preface/about_the_book/index.html @@ -4432,7 +4432,7 @@

    0.1.3   Acknowledgements

    This book has been continuously improved through the joint efforts of many contributors in the open-source community. Thanks to every contributor who invested time and effort, they are (in the order automatically generated by GitHub): krahets, coderonion, Gonglja, nuomi1, Reanon, justin-tse, hpstory, danielsss, curtishd, night-cruise, S-N-O-R-L-A-X, rongyi, msk397, gvenusleo, khoaxuantu, rivertwilight, K3v123, gyt95, zhuoqinyue, yuelinxin, Zuoxun, mingXta, Phoenix0415, FangYuan33, GN-Yu, longsizhuo, IsChristina, xBLACKICEx, guowei-gong, Cathay-Chen, pengchzn, QiLOL, magentaqin, hello-ikun, JoseHung, qualifier1024, thomasq0, sunshinesDL, L-Super, Guanngxu, Transmigration-zhou, WSL0809, Slone123c, lhxsm, yuan0221, what-is-me, Shyam-Chen, theNefelibatas, longranger2, codeberg-user, xiongsp, JeffersonHuang, prinpal, seven1240, Wonderdch, malone6, xiaomiusa87, gaofer, bluebean-cloud, a16su, SamJin98, hongyun-robot, nanlei, XiaChuerwu, yd-j, iron-irax, mgisr, steventimes, junminhong, heshuyue, danny900714, MolDuM, Nigh, Dr-XYZ, XC-Zero, reeswell, PXG-XPG, NI-SW, Horbin-Magician, Enlightenus, YangXuanyi, beatrix-chan, DullSword, xjr7670, jiaxianhua, qq909244296, iStig, boloboloda, hts0000, gledfish, wenjianmin, keshida, kilikilikid, lclc6, lwbaptx, linyejoe2, liuxjerry, llql1211, fbigm, echo1937, szu17dmy, dshlstarr, Yucao-cy, coderlef, czruby, bongbongbakudan, beintentional, ZongYangL, ZhongYuuu, ZhongGuanbin, hezhizhen, linzeyan, ZJKung, luluxia, xb534, ztkuaikuai, yw-1021, ElaBosak233, baagod, zhouLion, yishangzhang, yi427, yanedie, yabo083, weibk, wangwang105, th1nk3r-ing, tao363, 4yDX3906, syd168, sslmj2020, smilelsb, siqyka, selear, sdshaoda, Xi-Row, popozhu, nuquist19, noobcodemaker, XiaoK29, chadyi, lyl625760, lucaswangdev, 0130w, shanghai-Jerry, EJackYang, Javesun99, eltociear, lipusheng, KNChiu, BlindTerran, ShiMaRing, lovelock, FreddieLi, FloranceYeh, fanchenggang, gltianwen, goerll, nedchu, curly210102, CuB3y0nd, KraHsu, CarrotDLaw, youshaoXG, bubble9um, Asashishi, Asa0oo0o0o, fanenr, eagleanurag, akshiterate, 52coder, foursevenlove, KorsChen, GaochaoZhu, hopkings2008, yang-le, realwujing, Evilrabbit520, Umer-Jahangir, Turing-1024-Lee, Suremotoo, paoxiaomooo, Chieko-Seren, Allen-Scai, ymmmas, Risuntsy, Richard-Zhang1019, RafaelCaso, qingpeng9802, primexiao, Urbaner3, zhongfq, nidhoggfgg, MwumLi, CreatorMetaSky, martinx, ZnYang2018, hugtyftg, logan-qiu, psychelzh, Keynman, KeiichiKasai, and KawaiiAsh.

    The code review work for this book was completed by coderonion, curtishd, Gonglja, gvenusleo, hpstory, justin-tse, khoaxuantu, krahets, night-cruise, nuomi1, Reanon and rongyi (in alphabetical order). Thanks to them for the time and effort they put in, it is they who ensure the standardization and unity of code in various languages.

    -

    The Traditional Chinese version of this book was reviewed by Shyam-Chen and Dr-XYZ, and the English version was reviewed by yuelinxin, K3v123, QiLOL, Phoenix0415, SamJin98, yanedie, RafaelCaso, pengchzn, thomasq0 and magentaqin. It is because of their continuous contributions that this book can serve a wider readership, and we thank them.

    +

    The Traditional Chinese version of this book was reviewed by Shyam-Chen and Dr-XYZ, the English version was reviewed by yuelinxin, K3v123, QiLOL, Phoenix0415, SamJin98, yanedie, RafaelCaso, pengchzn, thomasq0 and magentaqin, and the Japanese edition was reviewed by eltociear. It is because of their continuous contributions that this book can serve a wider readership, and we thank them.

    The ePub ebook generation tool for this book was developed by zhongfq. We thank him for his contribution, which provides readers with a more flexible way to read.

    During the creation of this book, I received help from many people.

      diff --git a/en/javascripts/katex.js b/en/javascripts/katex.js index 2d85e17b6..1d696cf61 100644 --- a/en/javascripts/katex.js +++ b/en/javascripts/katex.js @@ -8,4 +8,4 @@ document$.subscribe(({ body }) => { ], }); }); -/*! update cache: 20260104155751 */ +/*! update cache: 20260104161450 */ diff --git a/en/javascripts/mathjax.js b/en/javascripts/mathjax.js index b46836e1b..d333eda98 100644 --- a/en/javascripts/mathjax.js +++ b/en/javascripts/mathjax.js @@ -15,4 +15,4 @@ window.MathJax = { document$.subscribe(() => { MathJax.typesetPromise(); }); -/*! update cache: 20260104155751 */ +/*! update cache: 20260104161450 */ diff --git a/en/javascripts/starfield.js b/en/javascripts/starfield.js index 1ea09e38d..e8b2fc32a 100644 --- a/en/javascripts/starfield.js +++ b/en/javascripts/starfield.js @@ -469,4 +469,4 @@ return Starfield; }); -/*! update cache: 20260104155751 */ +/*! update cache: 20260104161450 */ diff --git a/en/search/search_index.json b/en/search/search_index.json index adcaf4cd7..db7c5fdff 100644 --- a/en/search/search_index.json +++ b/en/search/search_index.json @@ -1 +1 @@ -{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"],"fields":{"title":{"boost":1000.0},"text":{"boost":1.0},"tags":{"boost":1000000.0}}},"docs":[{"location":"chapter_appendix/","title":"Chapter 16. \u00a0 Appendix","text":""},{"location":"chapter_appendix/#chapter-contents","title":"Chapter contents","text":"
      • 16.1 \u00a0 Programming Environment Installation
      • 16.2 \u00a0 Contributing Together
      • 16.3 \u00a0 Terminology Table
      "},{"location":"chapter_appendix/contribution/","title":"16.2 \u00a0 Contributing Together","text":"

      Due to limited capacity, there may be inevitable omissions and errors in this book. We appreciate your understanding and are grateful for your help in correcting them. If you discover typos, broken links, missing content, ambiguous wording, unclear explanations, or structural issues, please help us make corrections to provide readers with higher-quality learning resources.

      The GitHub IDs of all contributors will be displayed on the homepage of the book repository, the web version, and the PDF version to acknowledge their selfless contributions to the open source community.

      The Charm of Open Source

      The interval between two printings of a physical book is often quite long, making content updates very inconvenient.

      In this open source book, the time for content updates has been shortened to just days or even hours.

      "},{"location":"chapter_appendix/contribution/#1-minor-content-adjustments","title":"1. \u00a0 Minor Content Adjustments","text":"

      As shown in Figure 16-3, there is an \"edit icon\" in the top-right corner of each page. You can modify text or code by following these steps.

      1. Click the \"edit icon\". If you encounter a prompt asking you to \"Fork this repository\", please approve the operation.
      2. Modify the content of the Markdown source file, verify the correctness of the content, and maintain consistent formatting as much as possible.
      3. Fill in a description of your changes at the bottom of the page, then click the \"Propose file change\" button. After the page transitions, click the \"Create pull request\" button to submit your pull request.

      Figure 16-3 \u00a0 Page edit button

      Images cannot be directly modified. Please describe the issue by creating a new Issue or leaving a comment. We will promptly redraw and replace the images.

      "},{"location":"chapter_appendix/contribution/#2-content-creation","title":"2. \u00a0 Content Creation","text":"

      If you are interested in contributing to this open source project, including translating code into other programming languages or expanding article content, you will need to follow the Pull Request workflow below.

      1. Log in to GitHub and Fork the book's code repository to your personal account.
      2. Enter your forked repository webpage and use the git clone command to clone the repository to your local machine.
      3. Create content locally and conduct comprehensive tests to verify code correctness.
      4. Commit your local changes and push them to the remote repository.
      5. Refresh the repository webpage and click the \"Create pull request\" button to submit your pull request.
      "},{"location":"chapter_appendix/contribution/#3-docker-deployment","title":"3. \u00a0 Docker Deployment","text":"

      From the root directory of hello-algo, run the following Docker script to access the project at http://localhost:8000:

      docker-compose up -d\n

      Use the following command to remove the deployment:

      docker-compose down\n
      "},{"location":"chapter_appendix/installation/","title":"16.1 \u00a0 Programming Environment Installation","text":""},{"location":"chapter_appendix/installation/#1611-installing-ide","title":"16.1.1 \u00a0 Installing Ide","text":"

      We recommend using the open-source and lightweight VS Code as the local integrated development environment (IDE). Visit the VS Code official website, and download and install the appropriate version of VS Code according to your operating system.

      Figure 16-1 \u00a0 Download VS Code from the Official Website

      VS Code has a powerful ecosystem of extensions that supports running and debugging most programming languages. For example, after installing the \"Python Extension Pack\" extension, you can debug Python code. The installation steps are shown in the following figure.

      Figure 16-2 \u00a0 Install VS Code Extensions

      "},{"location":"chapter_appendix/installation/#1612-installing-language-environments","title":"16.1.2 \u00a0 Installing Language Environments","text":""},{"location":"chapter_appendix/installation/#1-python-environment","title":"1. \u00a0 Python Environment","text":"
      1. Download and install Miniconda3, which requires Python 3.10 or newer.
      2. Search for python in the VS Code extension marketplace and install the Python Extension Pack.
      3. (Optional) Enter pip install black on the command line to install the code formatter.
      "},{"location":"chapter_appendix/installation/#2-cc-environment","title":"2. \u00a0 C/c++ Environment","text":"
      1. Windows systems need to install MinGW (configuration tutorial); macOS comes with Clang built-in and does not require installation.
      2. Search for c++ in the VS Code extension marketplace and install the C/C++ Extension Pack.
      3. (Optional) Open the Settings page, search for the Clang_format_fallback Style code formatting option, and set it to { BasedOnStyle: Microsoft, BreakBeforeBraces: Attach }.
      "},{"location":"chapter_appendix/installation/#3-java-environment","title":"3. \u00a0 Java Environment","text":"
      1. Download and install OpenJDK (version must be > JDK 9).
      2. Search for java in the VS Code extension marketplace and install the Extension Pack for Java.
      "},{"location":"chapter_appendix/installation/#4-c-environment","title":"4. \u00a0 C# Environment","text":"
      1. Download and install .Net 8.0.
      2. Search for C# Dev Kit in the VS Code extension marketplace and install C# Dev Kit (configuration tutorial).
      3. You can also use Visual Studio (installation tutorial).
      "},{"location":"chapter_appendix/installation/#5-go-environment","title":"5. \u00a0 Go Environment","text":"
      1. Download and install Go.
      2. Search for go in the VS Code extension marketplace and install Go.
      3. Press Ctrl + Shift + P to open the command palette, type go, select Go: Install/Update Tools, check all options and install.
      "},{"location":"chapter_appendix/installation/#6-swift-environment","title":"6. \u00a0 Swift Environment","text":"
      1. Download and install Swift.
      2. Search for swift in the VS Code extension marketplace and install Swift for Visual Studio Code.
      "},{"location":"chapter_appendix/installation/#7-javascript-environment","title":"7. \u00a0 Javascript Environment","text":"
      1. Download and install Node.js.
      2. (Optional) Search for Prettier in the VS Code extension marketplace and install the code formatter.
      "},{"location":"chapter_appendix/installation/#8-typescript-environment","title":"8. \u00a0 Typescript Environment","text":"
      1. Follow the same installation steps as the JavaScript environment.
      2. Install TypeScript Execute (tsx).
      3. Search for typescript in the VS Code extension marketplace and install Pretty TypeScript Errors.
      "},{"location":"chapter_appendix/installation/#9-dart-environment","title":"9. \u00a0 Dart Environment","text":"
      1. Download and install Dart.
      2. Search for dart in the VS Code extension marketplace and install Dart.
      "},{"location":"chapter_appendix/installation/#10-rust-environment","title":"10. \u00a0 Rust Environment","text":"
      1. Download and install Rust.
      2. Search for rust in the VS Code extension marketplace and install rust-analyzer.
      "},{"location":"chapter_appendix/terminology/","title":"16.3 \u00a0 Terminology Table","text":"

      The following table lists important terms that appear in this book. It is worth noting the following points:

      • We recommend remembering the English names of terms to help with reading English literature.
      • Some terms have different names in Simplified Chinese and Traditional Chinese.

      Table 16-1 \u00a0 Important Terms in Data Structures and Algorithms

      English Simplified Chinese Traditional Chinese algorithm \u7b97\u6cd5 \u6f14\u7b97\u6cd5 data structure \u6570\u636e\u7ed3\u6784 \u8cc7\u6599\u7d50\u69cb code \u4ee3\u7801 \u7a0b\u5f0f\u78bc file \u6587\u4ef6 \u6a94\u6848 function \u51fd\u6570 \u51fd\u5f0f method \u65b9\u6cd5 \u65b9\u6cd5 variable \u53d8\u91cf \u8b8a\u6578 asymptotic complexity analysis \u6e10\u8fd1\u590d\u6742\u5ea6\u5206\u6790 \u6f38\u8fd1\u8907\u96dc\u5ea6\u5206\u6790 time complexity \u65f6\u95f4\u590d\u6742\u5ea6 \u6642\u9593\u8907\u96dc\u5ea6 space complexity \u7a7a\u95f4\u590d\u6742\u5ea6 \u7a7a\u9593\u8907\u96dc\u5ea6 loop \u5faa\u73af \u8ff4\u5708 iteration \u8fed\u4ee3 \u8fed\u4ee3 recursion \u9012\u5f52 \u905e\u8ff4 tail recursion \u5c3e\u9012\u5f52 \u5c3e\u905e\u8ff4 recursion tree \u9012\u5f52\u6811 \u905e\u8ff4\u6a39 big-\\(O\\) notation \u5927 \\(O\\) \u8bb0\u53f7 \u5927 \\(O\\) \u8a18\u865f asymptotic upper bound \u6e10\u8fd1\u4e0a\u754c \u6f38\u8fd1\u4e0a\u754c sign-magnitude \u539f\u7801 \u539f\u78bc 1\u2019s complement \u53cd\u7801 \u4e00\u88dc\u6578 2\u2019s complement \u8865\u7801 \u4e8c\u88dc\u6578 array \u6570\u7ec4 \u9663\u5217 index \u7d22\u5f15 \u7d22\u5f15 linked list \u94fe\u8868 \u93c8\u7d50\u4e32\u5217 linked list node, list node \u94fe\u8868\u8282\u70b9 \u93c8\u7d50\u4e32\u5217\u7bc0\u9ede head node \u5934\u8282\u70b9 \u982d\u7bc0\u9ede tail node \u5c3e\u8282\u70b9 \u5c3e\u7bc0\u9ede list \u5217\u8868 \u4e32\u5217 dynamic array \u52a8\u6001\u6570\u7ec4 \u52d5\u614b\u9663\u5217 hard disk \u786c\u76d8 \u786c\u789f random-access memory (RAM) \u5185\u5b58 \u8a18\u61b6\u9ad4 cache memory \u7f13\u5b58 \u5feb\u53d6 cache miss \u7f13\u5b58\u672a\u547d\u4e2d \u5feb\u53d6\u672a\u547d\u4e2d cache hit rate \u7f13\u5b58\u547d\u4e2d\u7387 \u5feb\u53d6\u547d\u4e2d\u7387 stack \u6808 \u5806\u758a top of the stack \u6808\u9876 \u5806\u758a\u9802 bottom of the stack \u6808\u5e95 \u5806\u758a\u5e95 queue \u961f\u5217 \u4f47\u5217 double-ended queue \u53cc\u5411\u961f\u5217 \u96d9\u5411\u4f47\u5217 front of the queue \u961f\u9996 \u4f47\u5217\u9996 rear of the queue \u961f\u5c3e \u4f47\u5217\u5c3e hash table \u54c8\u5e0c\u8868 \u96dc\u6e4a\u8868 hash set \u54c8\u5e0c\u96c6\u5408 \u96dc\u6e4a\u96c6\u5408 bucket \u6876 \u6876 hash function \u54c8\u5e0c\u51fd\u6570 \u96dc\u6e4a\u51fd\u5f0f hash collision \u54c8\u5e0c\u51b2\u7a81 \u96dc\u6e4a\u885d\u7a81 load factor \u8d1f\u8f7d\u56e0\u5b50 \u8ca0\u8f09\u56e0\u5b50 separate chaining \u94fe\u5f0f\u5730\u5740 \u93c8\u7d50\u4f4d\u5740 open addressing \u5f00\u653e\u5bfb\u5740 \u958b\u653e\u5b9a\u5740 linear probing \u7ebf\u6027\u63a2\u6d4b \u7dda\u6027\u63a2\u67e5 lazy deletion \u61d2\u5220\u9664 \u61f6\u522a\u9664 binary tree \u4e8c\u53c9\u6811 \u4e8c\u5143\u6a39 tree node \u6811\u8282\u70b9 \u6a39\u7bc0\u9ede left-child node \u5de6\u5b50\u8282\u70b9 \u5de6\u5b50\u7bc0\u9ede right-child node \u53f3\u5b50\u8282\u70b9 \u53f3\u5b50\u7bc0\u9ede parent node \u7236\u8282\u70b9 \u7236\u7bc0\u9ede left subtree \u5de6\u5b50\u6811 \u5de6\u5b50\u6a39 right subtree \u53f3\u5b50\u6811 \u53f3\u5b50\u6a39 root node \u6839\u8282\u70b9 \u6839\u7bc0\u9ede leaf node \u53f6\u8282\u70b9 \u8449\u7bc0\u9ede edge \u8fb9 \u908a level \u5c42 \u5c64 degree \u5ea6 \u5ea6 height \u9ad8\u5ea6 \u9ad8\u5ea6 depth \u6df1\u5ea6 \u6df1\u5ea6 perfect binary tree \u5b8c\u7f8e\u4e8c\u53c9\u6811 \u5b8c\u7f8e\u4e8c\u5143\u6a39 complete binary tree \u5b8c\u5168\u4e8c\u53c9\u6811 \u5b8c\u5168\u4e8c\u5143\u6a39 full binary tree \u5b8c\u6ee1\u4e8c\u53c9\u6811 \u5b8c\u6eff\u4e8c\u5143\u6a39 balanced binary tree \u5e73\u8861\u4e8c\u53c9\u6811 \u5e73\u8861\u4e8c\u5143\u6a39 binary search tree \u4e8c\u53c9\u641c\u7d22\u6811 \u4e8c\u5143\u641c\u5c0b\u6a39 AVL tree AVL \u6811 AVL \u6a39 red-black tree \u7ea2\u9ed1\u6811 \u7d05\u9ed1\u6a39 level-order traversal \u5c42\u5e8f\u904d\u5386 \u5c64\u5e8f\u8d70\u8a2a breadth-first traversal \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 \u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a depth-first traversal \u6df1\u5ea6\u4f18\u5148\u904d\u5386 \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a binary search tree \u4e8c\u53c9\u641c\u7d22\u6811 \u4e8c\u5143\u641c\u5c0b\u6a39 balanced binary search tree \u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811 \u5e73\u8861\u4e8c\u5143\u641c\u5c0b\u6a39 balance factor \u5e73\u8861\u56e0\u5b50 \u5e73\u8861\u56e0\u5b50 heap \u5806 \u5806\u7a4d max heap \u5927\u9876\u5806 \u5927\u9802\u5806\u7a4d min heap \u5c0f\u9876\u5806 \u5c0f\u9802\u5806\u7a4d priority queue \u4f18\u5148\u961f\u5217 \u512a\u5148\u4f47\u5217 heapify \u5806\u5316 \u5806\u7a4d\u5316 top-\\(k\\) problem Top-\\(k\\) \u95ee\u9898 Top-\\(k\\) \u554f\u984c graph \u56fe \u5716 vertex \u9876\u70b9 \u9802\u9ede undirected graph \u65e0\u5411\u56fe \u7121\u5411\u5716 directed graph \u6709\u5411\u56fe \u6709\u5411\u5716 connected graph \u8fde\u901a\u56fe \u9023\u901a\u5716 disconnected graph \u975e\u8fde\u901a\u56fe \u975e\u9023\u901a\u5716 weighted graph \u6709\u6743\u56fe \u6709\u6b0a\u5716 adjacency \u90bb\u63a5 \u9130\u63a5 path \u8def\u5f84 \u8def\u5f91 in-degree \u5165\u5ea6 \u5165\u5ea6 out-degree \u51fa\u5ea6 \u51fa\u5ea6 adjacency matrix \u90bb\u63a5\u77e9\u9635 \u9130\u63a5\u77e9\u9663 adjacency list \u90bb\u63a5\u8868 \u9130\u63a5\u8868 breadth-first search \u5e7f\u5ea6\u4f18\u5148\u641c\u7d22 \u5ee3\u5ea6\u512a\u5148\u641c\u5c0b depth-first search \u6df1\u5ea6\u4f18\u5148\u641c\u7d22 \u6df1\u5ea6\u512a\u5148\u641c\u5c0b binary search \u4e8c\u5206\u67e5\u627e \u4e8c\u5206\u641c\u5c0b searching algorithm \u641c\u7d22\u7b97\u6cd5 \u641c\u5c0b\u6f14\u7b97\u6cd5 sorting algorithm \u6392\u5e8f\u7b97\u6cd5 \u6392\u5e8f\u6f14\u7b97\u6cd5 selection sort \u9009\u62e9\u6392\u5e8f \u9078\u64c7\u6392\u5e8f bubble sort \u5192\u6ce1\u6392\u5e8f \u6ce1\u6cab\u6392\u5e8f insertion sort \u63d2\u5165\u6392\u5e8f \u63d2\u5165\u6392\u5e8f quick sort \u5feb\u901f\u6392\u5e8f \u5feb\u901f\u6392\u5e8f merge sort \u5f52\u5e76\u6392\u5e8f \u5408\u4f75\u6392\u5e8f heap sort \u5806\u6392\u5e8f \u5806\u7a4d\u6392\u5e8f bucket sort \u6876\u6392\u5e8f \u6876\u6392\u5e8f counting sort \u8ba1\u6570\u6392\u5e8f \u8a08\u6578\u6392\u5e8f radix sort \u57fa\u6570\u6392\u5e8f \u57fa\u6578\u6392\u5e8f divide and conquer \u5206\u6cbb \u5206\u6cbb hanota problem \u6c49\u8bfa\u5854\u95ee\u9898 \u6cb3\u5167\u5854\u554f\u984c backtracking algorithm \u56de\u6eaf\u7b97\u6cd5 \u56de\u6eaf\u6f14\u7b97\u6cd5 constraint \u7ea6\u675f \u7d04\u675f solution \u89e3 \u89e3 state \u72b6\u6001 \u72c0\u614b pruning \u526a\u679d \u526a\u679d permutations problem \u5168\u6392\u5217\u95ee\u9898 \u5168\u6392\u5217\u554f\u984c subset-sum problem \u5b50\u96c6\u548c\u95ee\u9898 \u5b50\u96c6\u5408\u554f\u984c \\(n\\)-queens problem \\(n\\) \u7687\u540e\u95ee\u9898 \\(n\\) \u7687\u540e\u554f\u984c dynamic programming \u52a8\u6001\u89c4\u5212 \u52d5\u614b\u898f\u5283 initial state \u521d\u59cb\u72b6\u6001 \u521d\u59cb\u72c0\u614b state-transition equation \u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b \u72c0\u614b\u8f49\u79fb\u65b9\u7a0b knapsack problem \u80cc\u5305\u95ee\u9898 \u80cc\u5305\u554f\u984c edit distance problem \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898 \u7de8\u8f2f\u8ddd\u96e2\u554f\u984c greedy algorithm \u8d2a\u5fc3\u7b97\u6cd5 \u8caa\u5a6a\u6f14\u7b97\u6cd5"},{"location":"chapter_array_and_linkedlist/","title":"Chapter 4. \u00a0 Array and Linked List","text":"

      Abstract

      The world of data structures is like a solid brick wall.

      Array bricks are neatly arranged, tightly packed one by one. Linked list bricks are scattered everywhere, with connecting vines freely weaving through the gaps between bricks.

      "},{"location":"chapter_array_and_linkedlist/#chapter-contents","title":"Chapter contents","text":"
      • 4.1 \u00a0 Array
      • 4.2 \u00a0 Linked List
      • 4.3 \u00a0 List
      • 4.4 \u00a0 Memory and Cache *
      • 4.5 \u00a0 Summary
      "},{"location":"chapter_array_and_linkedlist/array/","title":"4.1 \u00a0 Array","text":"

      An array is a linear data structure that stores elements of the same type in contiguous memory space. The position of an element in the array is called the element's index. Figure 4-1 illustrates the main concepts and storage method of arrays.

      Figure 4-1 \u00a0 Array definition and storage method

      "},{"location":"chapter_array_and_linkedlist/array/#411-common-array-operations","title":"4.1.1 \u00a0 Common Array Operations","text":""},{"location":"chapter_array_and_linkedlist/array/#1-initializing-arrays","title":"1. \u00a0 Initializing Arrays","text":"

      We can choose between two array initialization methods based on our needs: without initial values or with given initial values. When no initial values are specified, most programming languages will initialize array elements to \\(0\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
      # Initialize array\narr: list[int] = [0] * 5  # [ 0, 0, 0, 0, 0 ]\nnums: list[int] = [1, 3, 2, 5, 4]\n
      array.cpp
      /* Initialize array */\n// Stored on stack\nint arr[5];\nint nums[5] = { 1, 3, 2, 5, 4 };\n// Stored on heap (requires manual memory release)\nint* arr1 = new int[5];\nint* nums1 = new int[5] { 1, 3, 2, 5, 4 };\n
      array.java
      /* Initialize array */\nint[] arr = new int[5]; // { 0, 0, 0, 0, 0 }\nint[] nums = { 1, 3, 2, 5, 4 };\n
      array.cs
      /* Initialize array */\nint[] arr = new int[5]; // [ 0, 0, 0, 0, 0 ]\nint[] nums = [1, 3, 2, 5, 4];\n
      array.go
      /* Initialize array */\nvar arr [5]int\n// In Go, specifying length ([5]int) creates an array; not specifying length ([]int) creates a slice\n// Since Go's arrays are designed to have their length determined at compile time, only constants can be used to specify the length\n// For convenience in implementing the extend() method, slices are treated as arrays below\nnums := []int{1, 3, 2, 5, 4}\n
      array.swift
      /* Initialize array */\nlet arr = Array(repeating: 0, count: 5) // [0, 0, 0, 0, 0]\nlet nums = [1, 3, 2, 5, 4]\n
      array.js
      /* Initialize array */\nvar arr = new Array(5).fill(0);\nvar nums = [1, 3, 2, 5, 4];\n
      array.ts
      /* Initialize array */\nlet arr: number[] = new Array(5).fill(0);\nlet nums: number[] = [1, 3, 2, 5, 4];\n
      array.dart
      /* Initialize array */\nList<int> arr = List.filled(5, 0); // [0, 0, 0, 0, 0]\nList<int> nums = [1, 3, 2, 5, 4];\n
      array.rs
      /* Initialize array */\nlet arr: [i32; 5] = [0; 5]; // [0, 0, 0, 0, 0]\nlet slice: &[i32] = &[0; 5];\n// In Rust, specifying length ([i32; 5]) creates an array; not specifying length (&[i32]) creates a slice\n// Since Rust's arrays are designed to have their length determined at compile time, only constants can be used to specify the length\n// Vector is the type generally used as a dynamic array in Rust\n// For convenience in implementing the extend() method, vectors are treated as arrays below\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
      array.c
      /* Initialize array */\nint arr[5] = { 0 }; // { 0, 0, 0, 0, 0 }\nint nums[5] = { 1, 3, 2, 5, 4 };\n
      array.kt
      /* Initialize array */\nvar arr = IntArray(5) // { 0, 0, 0, 0, 0 }\nvar nums = intArrayOf(1, 3, 2, 5, 4)\n
      array.rb
      # Initialize array\narr = Array.new(5, 0)\nnums = [1, 3, 2, 5, 4]\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_array_and_linkedlist/array/#2-accessing-elements","title":"2. \u00a0 Accessing Elements","text":"

      Array elements are stored in contiguous memory space, which means calculating the memory address of array elements is very easy. Given the array's memory address (the memory address of the first element) and an element's index, we can use the formula shown in Figure 4-2 to calculate the element's memory address and directly access that element.

      Figure 4-2 \u00a0 Memory address calculation for array elements

      Observing Figure 4-2, we find that the first element of an array has an index of \\(0\\), which may seem counterintuitive since counting from \\(1\\) would be more natural. However, from the perspective of the address calculation formula, an index is essentially an offset from the memory address. The address offset of the first element is \\(0\\), so it is reasonable for its index to be \\(0\\).

      Accessing elements in an array is highly efficient; we can randomly access any element in the array in \\(O(1)\\) time.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
      def random_access(nums: list[int]) -> int:\n    \"\"\"Random access to element\"\"\"\n    # Randomly select a number from the interval [0, len(nums)-1]\n    random_index = random.randint(0, len(nums) - 1)\n    # Retrieve and return the random element\n    random_num = nums[random_index]\n    return random_num\n
      array.cpp
      /* Random access to element */\nint randomAccess(int *nums, int size) {\n    // Randomly select a number from interval [0, size)\n    int randomIndex = rand() % size;\n    // Retrieve and return the random element\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
      array.java
      /* Random access to element */\nint randomAccess(int[] nums) {\n    // Randomly select a number in the interval [0, nums.length)\n    int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);\n    // Retrieve and return the random element\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
      array.cs
      /* Random access to element */\nint RandomAccess(int[] nums) {\n    Random random = new();\n    // Randomly select a number in interval [0, nums.Length)\n    int randomIndex = random.Next(nums.Length);\n    // Retrieve and return the random element\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
      array.go
      /* Random access to element */\nfunc randomAccess(nums []int) (randomNum int) {\n    // Randomly select a number in the interval [0, nums.length)\n    randomIndex := rand.Intn(len(nums))\n    // Retrieve and return the random element\n    randomNum = nums[randomIndex]\n    return\n}\n
      array.swift
      /* Random access to element */\nfunc randomAccess(nums: [Int]) -> Int {\n    // Randomly select a number in interval [0, nums.count)\n    let randomIndex = nums.indices.randomElement()!\n    // Retrieve and return the random element\n    let randomNum = nums[randomIndex]\n    return randomNum\n}\n
      array.js
      /* Random access to element */\nfunction randomAccess(nums) {\n    // Randomly select a number in the interval [0, nums.length)\n    const random_index = Math.floor(Math.random() * nums.length);\n    // Retrieve and return the random element\n    const random_num = nums[random_index];\n    return random_num;\n}\n
      array.ts
      /* Random access to element */\nfunction randomAccess(nums: number[]): number {\n    // Randomly select a number in the interval [0, nums.length)\n    const random_index = Math.floor(Math.random() * nums.length);\n    // Retrieve and return the random element\n    const random_num = nums[random_index];\n    return random_num;\n}\n
      array.dart
      /* Random access to element */\nint randomAccess(List<int> nums) {\n  // Randomly select a number in the interval [0, nums.length)\n  int randomIndex = Random().nextInt(nums.length);\n  // Retrieve and return the random element\n  int randomNum = nums[randomIndex];\n  return randomNum;\n}\n
      array.rs
      /* Random access to element */\nfn random_access(nums: &[i32]) -> i32 {\n    // Randomly select a number in interval [0, nums.len())\n    let random_index = rand::thread_rng().gen_range(0..nums.len());\n    // Retrieve and return the random element\n    let random_num = nums[random_index];\n    random_num\n}\n
      array.c
      /* Random access to element */\nint randomAccess(int *nums, int size) {\n    // Randomly select a number from interval [0, size)\n    int randomIndex = rand() % size;\n    // Retrieve and return the random element\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
      array.kt
      /* Random access to element */\nfun randomAccess(nums: IntArray): Int {\n    // Randomly select a number in interval [0, nums.size)\n    val randomIndex = ThreadLocalRandom.current().nextInt(0, nums.size)\n    // Retrieve and return the random element\n    val randomNum = nums[randomIndex]\n    return randomNum\n}\n
      array.rb
      ### Random access element ###\ndef random_access(nums)\n  # Randomly select a number in the interval [0, nums.length)\n  random_index = Random.rand(0...nums.length)\n\n  # Retrieve and return the random element\n  nums[random_index]\nend\n
      "},{"location":"chapter_array_and_linkedlist/array/#3-inserting-elements","title":"3. \u00a0 Inserting Elements","text":"

      Array elements are stored \"tightly adjacent\" in memory, with no space between them to store any additional data. As shown in Figure 4-3, if we want to insert an element in the middle of an array, we need to shift all elements after that position backward by one position, and then assign the value to that index.

      Figure 4-3 \u00a0 Example of inserting an element into an array

      It is worth noting that since the length of an array is fixed, inserting an element will inevitably cause the element at the end of the array to be \"lost\". We will leave the solution to this problem for discussion in the \"List\" chapter.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
      def insert(nums: list[int], num: int, index: int):\n    \"\"\"Insert element num at index index in the array\"\"\"\n    # Move all elements at and after index index backward by one position\n    for i in range(len(nums) - 1, index, -1):\n        nums[i] = nums[i - 1]\n    # Assign num to the element at index index\n    nums[index] = num\n
      array.cpp
      /* Insert element num at index index in the array */\nvoid insert(int *nums, int size, int num, int index) {\n    // Move all elements at and after index index backward by one position\n    for (int i = size - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // Assign num to the element at index index\n    nums[index] = num;\n}\n
      array.java
      /* Insert element num at index index in the array */\nvoid insert(int[] nums, int num, int index) {\n    // Move all elements at and after index index backward by one position\n    for (int i = nums.length - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // Assign num to the element at index index\n    nums[index] = num;\n}\n
      array.cs
      /* Insert element num at index index in the array */\nvoid Insert(int[] nums, int num, int index) {\n    // Move all elements at and after index index backward by one position\n    for (int i = nums.Length - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // Assign num to the element at index index\n    nums[index] = num;\n}\n
      array.go
      /* Insert element num at index index in the array */\nfunc insert(nums []int, num int, index int) {\n    // Move all elements at and after index index backward by one position\n    for i := len(nums) - 1; i > index; i-- {\n        nums[i] = nums[i-1]\n    }\n    // Assign num to the element at index index\n    nums[index] = num\n}\n
      array.swift
      /* Insert element num at index index in the array */\nfunc insert(nums: inout [Int], num: Int, index: Int) {\n    // Move all elements at and after index index backward by one position\n    for i in nums.indices.dropFirst(index).reversed() {\n        nums[i] = nums[i - 1]\n    }\n    // Assign num to the element at index index\n    nums[index] = num\n}\n
      array.js
      /* Insert element num at index index in the array */\nfunction insert(nums, num, index) {\n    // Move all elements at and after index index backward by one position\n    for (let i = nums.length - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // Assign num to the element at index index\n    nums[index] = num;\n}\n
      array.ts
      /* Insert element num at index index in the array */\nfunction insert(nums: number[], num: number, index: number): void {\n    // Move all elements at and after index index backward by one position\n    for (let i = nums.length - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // Assign num to the element at index index\n    nums[index] = num;\n}\n
      array.dart
      /* Insert element _num at array index index */\nvoid insert(List<int> nums, int _num, int index) {\n  // Move all elements at and after index index backward by one position\n  for (var i = nums.length - 1; i > index; i--) {\n    nums[i] = nums[i - 1];\n  }\n  // Assign _num to element at index\n  nums[index] = _num;\n}\n
      array.rs
      /* Insert element num at index index in the array */\nfn insert(nums: &mut [i32], num: i32, index: usize) {\n    // Move all elements at and after index index backward by one position\n    for i in (index + 1..nums.len()).rev() {\n        nums[i] = nums[i - 1];\n    }\n    // Assign num to the element at index index\n    nums[index] = num;\n}\n
      array.c
      /* Insert element num at index index in the array */\nvoid insert(int *nums, int size, int num, int index) {\n    // Move all elements at and after index index backward by one position\n    for (int i = size - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // Assign num to the element at index index\n    nums[index] = num;\n}\n
      array.kt
      /* Insert element num at index index in the array */\nfun insert(nums: IntArray, num: Int, index: Int) {\n    // Move all elements at and after index index backward by one position\n    for (i in nums.size - 1 downTo index + 1) {\n        nums[i] = nums[i - 1]\n    }\n    // Assign num to the element at index index\n    nums[index] = num\n}\n
      array.rb
      ### Insert element num at index in array ###\ndef insert(nums, num, index)\n  # Move all elements at and after index index backward by one position\n  for i in (nums.length - 1).downto(index + 1)\n    nums[i] = nums[i - 1]\n  end\n\n  # Assign num to the element at index index\n  nums[index] = num\nend\n
      "},{"location":"chapter_array_and_linkedlist/array/#4-removing-elements","title":"4. \u00a0 Removing Elements","text":"

      Similarly, as shown in Figure 4-4, to delete the element at index \\(i\\), we need to shift all elements after index \\(i\\) forward by one position.

      Figure 4-4 \u00a0 Example of removing an element from an array

      Note that after the deletion is complete, the original last element becomes \"meaningless\", so we do not need to specifically modify it.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
      def remove(nums: list[int], index: int):\n    \"\"\"Remove the element at index index\"\"\"\n    # Move all elements after index index forward by one position\n    for i in range(index, len(nums) - 1):\n        nums[i] = nums[i + 1]\n
      array.cpp
      /* Remove the element at index index */\nvoid remove(int *nums, int size, int index) {\n    // Move all elements after index index forward by one position\n    for (int i = index; i < size - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
      array.java
      /* Remove the element at index index */\nvoid remove(int[] nums, int index) {\n    // Move all elements after index index forward by one position\n    for (int i = index; i < nums.length - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
      array.cs
      /* Remove the element at index index */\nvoid Remove(int[] nums, int index) {\n    // Move all elements after index index forward by one position\n    for (int i = index; i < nums.Length - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
      array.go
      /* Remove the element at index index */\nfunc remove(nums []int, index int) {\n    // Move all elements after index index forward by one position\n    for i := index; i < len(nums)-1; i++ {\n        nums[i] = nums[i+1]\n    }\n}\n
      array.swift
      /* Remove the element at index index */\nfunc remove(nums: inout [Int], index: Int) {\n    // Move all elements after index index forward by one position\n    for i in nums.indices.dropFirst(index).dropLast() {\n        nums[i] = nums[i + 1]\n    }\n}\n
      array.js
      /* Remove the element at index index */\nfunction remove(nums, index) {\n    // Move all elements after index index forward by one position\n    for (let i = index; i < nums.length - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
      array.ts
      /* Remove the element at index index */\nfunction remove(nums: number[], index: number): void {\n    // Move all elements after index index forward by one position\n    for (let i = index; i < nums.length - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
      array.dart
      /* Remove the element at index index */\nvoid remove(List<int> nums, int index) {\n  // Move all elements after index index forward by one position\n  for (var i = index; i < nums.length - 1; i++) {\n    nums[i] = nums[i + 1];\n  }\n}\n
      array.rs
      /* Remove the element at index index */\nfn remove(nums: &mut [i32], index: usize) {\n    // Move all elements after index index forward by one position\n    for i in index..nums.len() - 1 {\n        nums[i] = nums[i + 1];\n    }\n}\n
      array.c
      /* Remove the element at index index */\n// Note: stdio.h occupies the remove keyword\nvoid removeItem(int *nums, int size, int index) {\n    // Move all elements after index index forward by one position\n    for (int i = index; i < size - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
      array.kt
      /* Remove the element at index index */\nfun remove(nums: IntArray, index: Int) {\n    // Move all elements after index index forward by one position\n    for (i in index..<nums.size - 1) {\n        nums[i] = nums[i + 1]\n    }\n}\n
      array.rb
      ### Delete element at index ###\ndef remove(nums, index)\n  # Move all elements after index index forward by one position\n  for i in index...(nums.length - 1)\n    nums[i] = nums[i + 1]\n  end\nend\n

      Overall, array insertion and deletion operations have the following drawbacks:

      • High time complexity: The average time complexity for both insertion and deletion in arrays is \\(O(n)\\), where \\(n\\) is the length of the array.
      • Loss of elements: Since the length of an array is immutable, after inserting an element, elements that exceed the array's length will be lost.
      • Memory waste: We can initialize a relatively long array and only use the front portion, so that when inserting data, the lost elements at the end are \"meaningless\", but this causes some memory space to be wasted.
      "},{"location":"chapter_array_and_linkedlist/array/#5-traversing-arrays","title":"5. \u00a0 Traversing Arrays","text":"

      In most programming languages, we can traverse an array either by index or by directly iterating through each element in the array:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
      def traverse(nums: list[int]):\n    \"\"\"Traverse array\"\"\"\n    count = 0\n    # Traverse array by index\n    for i in range(len(nums)):\n        count += nums[i]\n    # Direct traversal of array elements\n    for num in nums:\n        count += num\n    # Traverse simultaneously data index and elements\n    for i, num in enumerate(nums):\n        count += nums[i]\n        count += num\n
      array.cpp
      /* Traverse array */\nvoid traverse(int *nums, int size) {\n    int count = 0;\n    // Traverse array by index\n    for (int i = 0; i < size; i++) {\n        count += nums[i];\n    }\n}\n
      array.java
      /* Traverse array */\nvoid traverse(int[] nums) {\n    int count = 0;\n    // Traverse array by index\n    for (int i = 0; i < nums.length; i++) {\n        count += nums[i];\n    }\n    // Direct traversal of array elements\n    for (int num : nums) {\n        count += num;\n    }\n}\n
      array.cs
      /* Traverse array */\nvoid Traverse(int[] nums) {\n    int count = 0;\n    // Traverse array by index\n    for (int i = 0; i < nums.Length; i++) {\n        count += nums[i];\n    }\n    // Direct traversal of array elements\n    foreach (int num in nums) {\n        count += num;\n    }\n}\n
      array.go
      /* Traverse array */\nfunc traverse(nums []int) {\n    count := 0\n    // Traverse array by index\n    for i := 0; i < len(nums); i++ {\n        count += nums[i]\n    }\n    count = 0\n    // Direct traversal of array elements\n    for _, num := range nums {\n        count += num\n    }\n    // Traverse simultaneously data index and elements\n    for i, num := range nums {\n        count += nums[i]\n        count += num\n    }\n}\n
      array.swift
      /* Traverse array */\nfunc traverse(nums: [Int]) {\n    var count = 0\n    // Traverse array by index\n    for i in nums.indices {\n        count += nums[i]\n    }\n    // Direct traversal of array elements\n    for num in nums {\n        count += num\n    }\n    // Traverse simultaneously data index and elements\n    for (i, num) in nums.enumerated() {\n        count += nums[i]\n        count += num\n    }\n}\n
      array.js
      /* Traverse array */\nfunction traverse(nums) {\n    let count = 0;\n    // Traverse array by index\n    for (let i = 0; i < nums.length; i++) {\n        count += nums[i];\n    }\n    // Direct traversal of array elements\n    for (const num of nums) {\n        count += num;\n    }\n}\n
      array.ts
      /* Traverse array */\nfunction traverse(nums: number[]): void {\n    let count = 0;\n    // Traverse array by index\n    for (let i = 0; i < nums.length; i++) {\n        count += nums[i];\n    }\n    // Direct traversal of array elements\n    for (const num of nums) {\n        count += num;\n    }\n}\n
      array.dart
      /* Traverse array elements */\nvoid traverse(List<int> nums) {\n  int count = 0;\n  // Traverse array by index\n  for (var i = 0; i < nums.length; i++) {\n    count += nums[i];\n  }\n  // Direct traversal of array elements\n  for (int _num in nums) {\n    count += _num;\n  }\n  // Traverse array using forEach method\n  nums.forEach((_num) {\n    count += _num;\n  });\n}\n
      array.rs
      /* Traverse array */\nfn traverse(nums: &[i32]) {\n    let mut _count = 0;\n    // Traverse array by index\n    for i in 0..nums.len() {\n        _count += nums[i];\n    }\n    // Direct traversal of array elements\n    _count = 0;\n    for &num in nums {\n        _count += num;\n    }\n}\n
      array.c
      /* Traverse array */\nvoid traverse(int *nums, int size) {\n    int count = 0;\n    // Traverse array by index\n    for (int i = 0; i < size; i++) {\n        count += nums[i];\n    }\n}\n
      array.kt
      /* Traverse array */\nfun traverse(nums: IntArray) {\n    var count = 0\n    // Traverse array by index\n    for (i in nums.indices) {\n        count += nums[i]\n    }\n    // Direct traversal of array elements\n    for (j in nums) {\n        count += j\n    }\n}\n
      array.rb
      ### Traverse array ###\ndef traverse(nums)\n  count = 0\n\n  # Traverse array by index\n  for i in 0...nums.length\n    count += nums[i]\n  end\n\n  # Direct traversal of array elements\n  for num in nums\n    count += num\n  end\nend\n
      "},{"location":"chapter_array_and_linkedlist/array/#6-finding-elements","title":"6. \u00a0 Finding Elements","text":"

      Finding a specified element in an array requires traversing the array and checking whether the element value matches in each iteration; if it matches, output the corresponding index.

      Since an array is a linear data structure, the above search operation is called a \"linear search\".

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
      def find(nums: list[int], target: int) -> int:\n    \"\"\"Find the specified element in the array\"\"\"\n    for i in range(len(nums)):\n        if nums[i] == target:\n            return i\n    return -1\n
      array.cpp
      /* Find the specified element in the array */\nint find(int *nums, int size, int target) {\n    for (int i = 0; i < size; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
      array.java
      /* Find the specified element in the array */\nint find(int[] nums, int target) {\n    for (int i = 0; i < nums.length; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
      array.cs
      /* Find the specified element in the array */\nint Find(int[] nums, int target) {\n    for (int i = 0; i < nums.Length; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
      array.go
      /* Find the specified element in the array */\nfunc find(nums []int, target int) (index int) {\n    index = -1\n    for i := 0; i < len(nums); i++ {\n        if nums[i] == target {\n            index = i\n            break\n        }\n    }\n    return\n}\n
      array.swift
      /* Find the specified element in the array */\nfunc find(nums: [Int], target: Int) -> Int {\n    for i in nums.indices {\n        if nums[i] == target {\n            return i\n        }\n    }\n    return -1\n}\n
      array.js
      /* Find the specified element in the array */\nfunction find(nums, target) {\n    for (let i = 0; i < nums.length; i++) {\n        if (nums[i] === target) return i;\n    }\n    return -1;\n}\n
      array.ts
      /* Find the specified element in the array */\nfunction find(nums: number[], target: number): number {\n    for (let i = 0; i < nums.length; i++) {\n        if (nums[i] === target) {\n            return i;\n        }\n    }\n    return -1;\n}\n
      array.dart
      /* Find the specified element in the array */\nint find(List<int> nums, int target) {\n  for (var i = 0; i < nums.length; i++) {\n    if (nums[i] == target) return i;\n  }\n  return -1;\n}\n
      array.rs
      /* Find the specified element in the array */\nfn find(nums: &[i32], target: i32) -> Option<usize> {\n    for i in 0..nums.len() {\n        if nums[i] == target {\n            return Some(i);\n        }\n    }\n    None\n}\n
      array.c
      /* Find the specified element in the array */\nint find(int *nums, int size, int target) {\n    for (int i = 0; i < size; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
      array.kt
      /* Find the specified element in the array */\nfun find(nums: IntArray, target: Int): Int {\n    for (i in nums.indices) {\n        if (nums[i] == target)\n            return i\n    }\n    return -1\n}\n
      array.rb
      ### Find specified element in array ###\ndef find(nums, target)\n  for i in 0...nums.length\n    return i if nums[i] == target\n  end\n\n  -1\nend\n
      "},{"location":"chapter_array_and_linkedlist/array/#7-expanding-arrays","title":"7. \u00a0 Expanding Arrays","text":"

      In complex system environments, programs cannot guarantee that the memory space after an array is available, making it unsafe to expand the array's capacity. Therefore, in most programming languages, the length of an array is immutable.

      If we want to expand an array, we need to create a new, larger array and then copy the original array elements to the new array one by one. This is an \\(O(n)\\) operation, which is very time-consuming when the array is large. The code is shown below:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
      def extend(nums: list[int], enlarge: int) -> list[int]:\n    \"\"\"Extend array length\"\"\"\n    # Initialize an array with extended length\n    res = [0] * (len(nums) + enlarge)\n    # Copy all elements from the original array to the new array\n    for i in range(len(nums)):\n        res[i] = nums[i]\n    # Return the extended new array\n    return res\n
      array.cpp
      /* Extend array length */\nint *extend(int *nums, int size, int enlarge) {\n    // Initialize an array with extended length\n    int *res = new int[size + enlarge];\n    // Copy all elements from the original array to the new array\n    for (int i = 0; i < size; i++) {\n        res[i] = nums[i];\n    }\n    // Free memory\n    delete[] nums;\n    // Return the extended new array\n    return res;\n}\n
      array.java
      /* Extend array length */\nint[] extend(int[] nums, int enlarge) {\n    // Initialize an array with extended length\n    int[] res = new int[nums.length + enlarge];\n    // Copy all elements from the original array to the new array\n    for (int i = 0; i < nums.length; i++) {\n        res[i] = nums[i];\n    }\n    // Return the extended new array\n    return res;\n}\n
      array.cs
      /* Extend array length */\nint[] Extend(int[] nums, int enlarge) {\n    // Initialize an array with extended length\n    int[] res = new int[nums.Length + enlarge];\n    // Copy all elements from the original array to the new array\n    for (int i = 0; i < nums.Length; i++) {\n        res[i] = nums[i];\n    }\n    // Return the extended new array\n    return res;\n}\n
      array.go
      /* Extend array length */\nfunc extend(nums []int, enlarge int) []int {\n    // Initialize an array with extended length\n    res := make([]int, len(nums)+enlarge)\n    // Copy all elements from the original array to the new array\n    for i, num := range nums {\n        res[i] = num\n    }\n    // Return the extended new array\n    return res\n}\n
      array.swift
      /* Extend array length */\nfunc extend(nums: [Int], enlarge: Int) -> [Int] {\n    // Initialize an array with extended length\n    var res = Array(repeating: 0, count: nums.count + enlarge)\n    // Copy all elements from the original array to the new array\n    for i in nums.indices {\n        res[i] = nums[i]\n    }\n    // Return the extended new array\n    return res\n}\n
      array.js
      /* Extend array length */\n// Note: JavaScript's Array is dynamic array, can be directly expanded\n// For learning purposes, this function treats Array as fixed-length array\nfunction extend(nums, enlarge) {\n    // Initialize an array with extended length\n    const res = new Array(nums.length + enlarge).fill(0);\n    // Copy all elements from the original array to the new array\n    for (let i = 0; i < nums.length; i++) {\n        res[i] = nums[i];\n    }\n    // Return the extended new array\n    return res;\n}\n
      array.ts
      /* Extend array length */\n// Note: TypeScript's Array is dynamic array, can be directly expanded\n// For learning purposes, this function treats Array as fixed-length array\nfunction extend(nums: number[], enlarge: number): number[] {\n    // Initialize an array with extended length\n    const res = new Array(nums.length + enlarge).fill(0);\n    // Copy all elements from the original array to the new array\n    for (let i = 0; i < nums.length; i++) {\n        res[i] = nums[i];\n    }\n    // Return the extended new array\n    return res;\n}\n
      array.dart
      /* Extend array length */\nList<int> extend(List<int> nums, int enlarge) {\n  // Initialize an array with extended length\n  List<int> res = List.filled(nums.length + enlarge, 0);\n  // Copy all elements from the original array to the new array\n  for (var i = 0; i < nums.length; i++) {\n    res[i] = nums[i];\n  }\n  // Return the extended new array\n  return res;\n}\n
      array.rs
      /* Extend array length */\nfn extend(nums: &[i32], enlarge: usize) -> Vec<i32> {\n    // Initialize an array with extended length\n    let mut res: Vec<i32> = vec![0; nums.len() + enlarge];\n    // Copy all elements from original array to new\n    res[0..nums.len()].copy_from_slice(nums);\n\n    // Return the extended new array\n    res\n}\n
      array.c
      /* Extend array length */\nint *extend(int *nums, int size, int enlarge) {\n    // Initialize an array with extended length\n    int *res = (int *)malloc(sizeof(int) * (size + enlarge));\n    // Copy all elements from the original array to the new array\n    for (int i = 0; i < size; i++) {\n        res[i] = nums[i];\n    }\n    // Initialize expanded space\n    for (int i = size; i < size + enlarge; i++) {\n        res[i] = 0;\n    }\n    // Return the extended new array\n    return res;\n}\n
      array.kt
      /* Extend array length */\nfun extend(nums: IntArray, enlarge: Int): IntArray {\n    // Initialize an array with extended length\n    val res = IntArray(nums.size + enlarge)\n    // Copy all elements from the original array to the new array\n    for (i in nums.indices) {\n        res[i] = nums[i]\n    }\n    // Return the extended new array\n    return res\n}\n
      array.rb
      ### Extend array length ###\n# Note: Ruby's Array is dynamic array, can be directly expanded\n# For learning purposes, this function treats Array as fixed-length array\ndef extend(nums, enlarge)\n  # Initialize an array with extended length\n  res = Array.new(nums.length + enlarge, 0)\n\n  # Copy all elements from the original array to the new array\n  for i in 0...nums.length\n    res[i] = nums[i]\n  end\n\n  # Return the extended new array\n  res\nend\n
      "},{"location":"chapter_array_and_linkedlist/array/#412-advantages-and-limitations-of-arrays","title":"4.1.2 \u00a0 Advantages and Limitations of Arrays","text":"

      Arrays are stored in contiguous memory space with elements of the same type. This approach contains rich prior information that the system can use to optimize the efficiency of data structure operations.

      • High space efficiency: Arrays allocate contiguous memory blocks for data without additional structural overhead.
      • Support for random access: Arrays allow accessing any element in \\(O(1)\\) time.
      • Cache locality: When accessing array elements, the computer not only loads the element but also caches the surrounding data, thereby leveraging the cache to improve the execution speed of subsequent operations.

      Contiguous space storage is a double-edged sword with the following limitations:

      • Low insertion and deletion efficiency: When an array has many elements, insertion and deletion operations require shifting a large number of elements.
      • Immutable length: After an array is initialized, its length is fixed. Expanding the array requires copying all data to a new array, which is very costly.
      • Space waste: If the allocated size of an array exceeds what is actually needed, the extra space is wasted.
      "},{"location":"chapter_array_and_linkedlist/array/#413-typical-applications-of-arrays","title":"4.1.3 \u00a0 Typical Applications of Arrays","text":"

      Arrays are a fundamental and common data structure, frequently used in various algorithms and for implementing various complex data structures.

      • Random access: If we want to randomly sample some items, we can use an array to store them and generate a random sequence to implement random sampling based on indices.
      • Sorting and searching: Arrays are the most commonly used data structure for sorting and searching algorithms. Quick sort, merge sort, binary search, and others are primarily performed on arrays.
      • Lookup tables: When we need to quickly find an element or its corresponding relationship, we can use an array as a lookup table. For example, if we want to implement a mapping from characters to ASCII codes, we can use the ASCII code value of a character as an index, with the corresponding element stored at that position in the array.
      • Machine learning: Neural networks make extensive use of linear algebra operations between vectors, matrices, and tensors, all of which are constructed in the form of arrays. Arrays are the most commonly used data structure in neural network programming.
      • Data structure implementation: Arrays can be used to implement stacks, queues, hash tables, heaps, graphs, and other data structures. For example, the adjacency matrix representation of a graph is essentially a two-dimensional array.
      "},{"location":"chapter_array_and_linkedlist/linked_list/","title":"4.2 \u00a0 Linked List","text":"

      Memory space is a shared resource for all programs. In a complex system runtime environment, available memory space may be scattered throughout the memory. We know that the memory space for storing an array must be contiguous, and when the array is very large, the memory may not be able to provide such a large contiguous space. This is where the flexibility advantage of linked lists becomes apparent.

      A linked list is a linear data structure in which each element is a node object, and the nodes are connected through \"references\". A reference records the memory address of the next node, through which the next node can be accessed from the current node.

      The design of linked lists allows nodes to be stored scattered throughout the memory, and their memory addresses do not need to be contiguous.

      Figure 4-5 \u00a0 Linked list definition and storage method

      Observing Figure 4-5, the basic unit of a linked list is a node object. Each node contains two pieces of data: the node's \"value\" and a \"reference\" to the next node.

      • The first node of a linked list is called the \"head node\", and the last node is called the \"tail node\".
      • The tail node points to \"null\", which is denoted as null, nullptr, and None in Java, C++, and Python, respectively.
      • In languages that support pointers, such as C, C++, Go, and Rust, the aforementioned \"reference\" should be replaced with \"pointer\".

      As shown in the following code, a linked list node ListNode contains not only a value but also an additional reference (pointer). Therefore, linked lists occupy more memory space than arrays when storing the same amount of data.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      class ListNode:\n    \"\"\"Linked list node class\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val               # Node value\n        self.next: ListNode | None = None # Reference to the next node\n
      /* Linked list node structure */\nstruct ListNode {\n    int val;         // Node value\n    ListNode *next;  // Pointer to the next node\n    ListNode(int x) : val(x), next(nullptr) {}  // Constructor\n};\n
      /* Linked list node class */\nclass ListNode {\n    int val;        // Node value\n    ListNode next;  // Reference to the next node\n    ListNode(int x) { val = x; }  // Constructor\n}\n
      /* Linked list node class */\nclass ListNode(int x) {  // Constructor\n    int val = x;         // Node value\n    ListNode? next;      // Reference to the next node\n}\n
      /* Linked list node structure */\ntype ListNode struct {\n    Val  int       // Node value\n    Next *ListNode // Pointer to the next node\n}\n\n// NewListNode Constructor, creates a new linked list\nfunc NewListNode(val int) *ListNode {\n    return &ListNode{\n        Val:  val,\n        Next: nil,\n    }\n}\n
      /* Linked list node class */\nclass ListNode {\n    var val: Int // Node value\n    var next: ListNode? // Reference to the next node\n\n    init(x: Int) { // Constructor\n        val = x\n    }\n}\n
      /* Linked list node class */\nclass ListNode {\n    constructor(val, next) {\n        this.val = (val === undefined ? 0 : val);       // Node value\n        this.next = (next === undefined ? null : next); // Reference to the next node\n    }\n}\n
      /* Linked list node class */\nclass ListNode {\n    val: number;\n    next: ListNode | null;\n    constructor(val?: number, next?: ListNode | null) {\n        this.val = val === undefined ? 0 : val;        // Node value\n        this.next = next === undefined ? null : next;  // Reference to the next node\n    }\n}\n
      /* Linked list node class */\nclass ListNode {\n  int val; // Node value\n  ListNode? next; // Reference to the next node\n  ListNode(this.val, [this.next]); // Constructor\n}\n
      use std::rc::Rc;\nuse std::cell::RefCell;\n/* Linked list node class */\n#[derive(Debug)]\nstruct ListNode {\n    val: i32, // Node value\n    next: Option<Rc<RefCell<ListNode>>>, // Pointer to the next node\n}\n
      /* Linked list node structure */\ntypedef struct ListNode {\n    int val;               // Node value\n    struct ListNode *next; // Pointer to the next node\n} ListNode;\n\n/* Constructor */\nListNode *newListNode(int val) {\n    ListNode *node;\n    node = (ListNode *) malloc(sizeof(ListNode));\n    node->val = val;\n    node->next = NULL;\n    return node;\n}\n
      /* Linked list node class */\n// Constructor\nclass ListNode(x: Int) {\n    val _val: Int = x          // Node value\n    val next: ListNode? = null // Reference to the next node\n}\n
      # Linked list node class\nclass ListNode\n  attr_accessor :val  # Node value\n  attr_accessor :next # Reference to the next node\n\n  def initialize(val=0, next_node=nil)\n    @val = val\n    @next = next_node\n  end\nend\n
      "},{"location":"chapter_array_and_linkedlist/linked_list/#421-common-linked-list-operations","title":"4.2.1 \u00a0 Common Linked List Operations","text":""},{"location":"chapter_array_and_linkedlist/linked_list/#1-initializing-a-linked-list","title":"1. \u00a0 Initializing a Linked List","text":"

      Building a linked list involves two steps: first, initializing each node object; second, constructing the reference relationships between nodes. Once initialization is complete, we can traverse all nodes starting from the head node of the linked list through the reference next.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
      # Initialize linked list 1 -> 3 -> 2 -> 5 -> 4\n# Initialize each node\nn0 = ListNode(1)\nn1 = ListNode(3)\nn2 = ListNode(2)\nn3 = ListNode(5)\nn4 = ListNode(4)\n# Build references between nodes\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
      linked_list.cpp
      /* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nListNode* n0 = new ListNode(1);\nListNode* n1 = new ListNode(3);\nListNode* n2 = new ListNode(2);\nListNode* n3 = new ListNode(5);\nListNode* n4 = new ListNode(4);\n// Build references between nodes\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
      linked_list.java
      /* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nListNode n0 = new ListNode(1);\nListNode n1 = new ListNode(3);\nListNode n2 = new ListNode(2);\nListNode n3 = new ListNode(5);\nListNode n4 = new ListNode(4);\n// Build references between nodes\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
      linked_list.cs
      /* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nListNode n0 = new(1);\nListNode n1 = new(3);\nListNode n2 = new(2);\nListNode n3 = new(5);\nListNode n4 = new(4);\n// Build references between nodes\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
      linked_list.go
      /* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nn0 := NewListNode(1)\nn1 := NewListNode(3)\nn2 := NewListNode(2)\nn3 := NewListNode(5)\nn4 := NewListNode(4)\n// Build references between nodes\nn0.Next = n1\nn1.Next = n2\nn2.Next = n3\nn3.Next = n4\n
      linked_list.swift
      /* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nlet n0 = ListNode(x: 1)\nlet n1 = ListNode(x: 3)\nlet n2 = ListNode(x: 2)\nlet n3 = ListNode(x: 5)\nlet n4 = ListNode(x: 4)\n// Build references between nodes\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
      linked_list.js
      /* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// Build references between nodes\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
      linked_list.ts
      /* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// Build references between nodes\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
      linked_list.dart
      /* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */\\\n// Initialize each node\nListNode n0 = ListNode(1);\nListNode n1 = ListNode(3);\nListNode n2 = ListNode(2);\nListNode n3 = ListNode(5);\nListNode n4 = ListNode(4);\n// Build references between nodes\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
      linked_list.rs
      /* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nlet n0 = Rc::new(RefCell::new(ListNode { val: 1, next: None }));\nlet n1 = Rc::new(RefCell::new(ListNode { val: 3, next: None }));\nlet n2 = Rc::new(RefCell::new(ListNode { val: 2, next: None }));\nlet n3 = Rc::new(RefCell::new(ListNode { val: 5, next: None }));\nlet n4 = Rc::new(RefCell::new(ListNode { val: 4, next: None }));\n\n// Build references between nodes\nn0.borrow_mut().next = Some(n1.clone());\nn1.borrow_mut().next = Some(n2.clone());\nn2.borrow_mut().next = Some(n3.clone());\nn3.borrow_mut().next = Some(n4.clone());\n
      linked_list.c
      /* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nListNode* n0 = newListNode(1);\nListNode* n1 = newListNode(3);\nListNode* n2 = newListNode(2);\nListNode* n3 = newListNode(5);\nListNode* n4 = newListNode(4);\n// Build references between nodes\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
      linked_list.kt
      /* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nval n0 = ListNode(1)\nval n1 = ListNode(3)\nval n2 = ListNode(2)\nval n3 = ListNode(5)\nval n4 = ListNode(4)\n// Build references between nodes\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
      linked_list.rb
      # Initialize linked list 1 -> 3 -> 2 -> 5 -> 4\n# Initialize each node\nn0 = ListNode.new(1)\nn1 = ListNode.new(3)\nn2 = ListNode.new(2)\nn3 = ListNode.new(5)\nn4 = ListNode.new(4)\n# Build references between nodes\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
      Code Visualization

      Full Screen >

      An array is a single variable; for example, an array nums contains elements nums[0], nums[1], etc. A linked list, however, is composed of multiple independent node objects. We typically use the head node as the reference to the linked list; for example, the linked list in the above code can be referred to as linked list n0.

      "},{"location":"chapter_array_and_linkedlist/linked_list/#2-inserting-a-node","title":"2. \u00a0 Inserting a Node","text":"

      Inserting a node in a linked list is very easy. As shown in Figure 4-6, suppose we want to insert a new node P between two adjacent nodes n0 and n1. We only need to change two node references (pointers), with a time complexity of \\(O(1)\\).

      In contrast, the time complexity of inserting an element in an array is \\(O(n)\\), which is inefficient when dealing with large amounts of data.

      Figure 4-6 \u00a0 Example of inserting a node into a linked list

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
      def insert(n0: ListNode, P: ListNode):\n    \"\"\"Insert node P after node n0 in the linked list\"\"\"\n    n1 = n0.next\n    P.next = n1\n    n0.next = P\n
      linked_list.cpp
      /* Insert node P after node n0 in the linked list */\nvoid insert(ListNode *n0, ListNode *P) {\n    ListNode *n1 = n0->next;\n    P->next = n1;\n    n0->next = P;\n}\n
      linked_list.java
      /* Insert node P after node n0 in the linked list */\nvoid insert(ListNode n0, ListNode P) {\n    ListNode n1 = n0.next;\n    P.next = n1;\n    n0.next = P;\n}\n
      linked_list.cs
      /* Insert node P after node n0 in the linked list */\nvoid Insert(ListNode n0, ListNode P) {\n    ListNode? n1 = n0.next;\n    P.next = n1;\n    n0.next = P;\n}\n
      linked_list.go
      /* Insert node P after node n0 in the linked list */\nfunc insertNode(n0 *ListNode, P *ListNode) {\n    n1 := n0.Next\n    P.Next = n1\n    n0.Next = P\n}\n
      linked_list.swift
      /* Insert node P after node n0 in the linked list */\nfunc insert(n0: ListNode, P: ListNode) {\n    let n1 = n0.next\n    P.next = n1\n    n0.next = P\n}\n
      linked_list.js
      /* Insert node P after node n0 in the linked list */\nfunction insert(n0, P) {\n    const n1 = n0.next;\n    P.next = n1;\n    n0.next = P;\n}\n
      linked_list.ts
      /* Insert node P after node n0 in the linked list */\nfunction insert(n0: ListNode, P: ListNode): void {\n    const n1 = n0.next;\n    P.next = n1;\n    n0.next = P;\n}\n
      linked_list.dart
      /* Insert node P after node n0 in the linked list */\nvoid insert(ListNode n0, ListNode P) {\n  ListNode? n1 = n0.next;\n  P.next = n1;\n  n0.next = P;\n}\n
      linked_list.rs
      /* Insert node P after node n0 in the linked list */\n#[allow(non_snake_case)]\npub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) {\n    let n1 = n0.borrow_mut().next.take();\n    P.borrow_mut().next = n1;\n    n0.borrow_mut().next = Some(P);\n}\n
      linked_list.c
      /* Insert node P after node n0 in the linked list */\nvoid insert(ListNode *n0, ListNode *P) {\n    ListNode *n1 = n0->next;\n    P->next = n1;\n    n0->next = P;\n}\n
      linked_list.kt
      /* Insert node P after node n0 in the linked list */\nfun insert(n0: ListNode?, p: ListNode?) {\n    val n1 = n0?.next\n    p?.next = n1\n    n0?.next = p\n}\n
      linked_list.rb
      ### Insert node _p after node n0 in linked list ###\n# Ruby's `p` is a built-in function, `P` is a constant, so use `_p` instead\ndef insert(n0, _p)\n  n1 = n0.next\n  _p.next = n1\n  n0.next = _p\nend\n
      "},{"location":"chapter_array_and_linkedlist/linked_list/#3-removing-a-node","title":"3. \u00a0 Removing a Node","text":"

      As shown in Figure 4-7, removing a node in a linked list is also very convenient. We only need to change one node's reference (pointer).

      Note that although node P still points to n1 after the deletion operation is complete, the linked list can no longer access P when traversing, which means P no longer belongs to this linked list.

      Figure 4-7 \u00a0 Removing a node from a linked list

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
      def remove(n0: ListNode):\n    \"\"\"Remove the first node after node n0 in the linked list\"\"\"\n    if not n0.next:\n        return\n    # n0 -> P -> n1\n    P = n0.next\n    n1 = P.next\n    n0.next = n1\n
      linked_list.cpp
      /* Remove the first node after node n0 in the linked list */\nvoid remove(ListNode *n0) {\n    if (n0->next == nullptr)\n        return;\n    // n0 -> P -> n1\n    ListNode *P = n0->next;\n    ListNode *n1 = P->next;\n    n0->next = n1;\n    // Free memory\n    delete P;\n}\n
      linked_list.java
      /* Remove the first node after node n0 in the linked list */\nvoid remove(ListNode n0) {\n    if (n0.next == null)\n        return;\n    // n0 -> P -> n1\n    ListNode P = n0.next;\n    ListNode n1 = P.next;\n    n0.next = n1;\n}\n
      linked_list.cs
      /* Remove the first node after node n0 in the linked list */\nvoid Remove(ListNode n0) {\n    if (n0.next == null)\n        return;\n    // n0 -> P -> n1\n    ListNode P = n0.next;\n    ListNode? n1 = P.next;\n    n0.next = n1;\n}\n
      linked_list.go
      /* Remove the first node after node n0 in the linked list */\nfunc removeItem(n0 *ListNode) {\n    if n0.Next == nil {\n        return\n    }\n    // n0 -> P -> n1\n    P := n0.Next\n    n1 := P.Next\n    n0.Next = n1\n}\n
      linked_list.swift
      /* Remove the first node after node n0 in the linked list */\nfunc remove(n0: ListNode) {\n    if n0.next == nil {\n        return\n    }\n    // n0 -> P -> n1\n    let P = n0.next\n    let n1 = P?.next\n    n0.next = n1\n}\n
      linked_list.js
      /* Remove the first node after node n0 in the linked list */\nfunction remove(n0) {\n    if (!n0.next) return;\n    // n0 -> P -> n1\n    const P = n0.next;\n    const n1 = P.next;\n    n0.next = n1;\n}\n
      linked_list.ts
      /* Remove the first node after node n0 in the linked list */\nfunction remove(n0: ListNode): void {\n    if (!n0.next) {\n        return;\n    }\n    // n0 -> P -> n1\n    const P = n0.next;\n    const n1 = P.next;\n    n0.next = n1;\n}\n
      linked_list.dart
      /* Remove the first node after node n0 in the linked list */\nvoid remove(ListNode n0) {\n  if (n0.next == null) return;\n  // n0 -> P -> n1\n  ListNode P = n0.next!;\n  ListNode? n1 = P.next;\n  n0.next = n1;\n}\n
      linked_list.rs
      /* Remove the first node after node n0 in the linked list */\n#[allow(non_snake_case)]\npub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {\n    // n0 -> P -> n1\n    let P = n0.borrow_mut().next.take();\n    if let Some(node) = P {\n        let n1 = node.borrow_mut().next.take();\n        n0.borrow_mut().next = n1;\n    }\n}\n
      linked_list.c
      /* Remove the first node after node n0 in the linked list */\n// Note: stdio.h occupies the remove keyword\nvoid removeItem(ListNode *n0) {\n    if (!n0->next)\n        return;\n    // n0 -> P -> n1\n    ListNode *P = n0->next;\n    ListNode *n1 = P->next;\n    n0->next = n1;\n    // Free memory\n    free(P);\n}\n
      linked_list.kt
      /* Remove the first node after node n0 in the linked list */\nfun remove(n0: ListNode?) {\n    if (n0?.next == null)\n        return\n    // n0 -> P -> n1\n    val p = n0.next\n    val n1 = p?.next\n    n0.next = n1\n}\n
      linked_list.rb
      ### Delete first node after node n0 in linked list ###\ndef remove(n0)\n  return if n0.next.nil?\n\n  # n0 -> remove_node -> n1\n  remove_node = n0.next\n  n1 = remove_node.next\n  n0.next = n1\nend\n
      "},{"location":"chapter_array_and_linkedlist/linked_list/#4-accessing-a-node","title":"4. \u00a0 Accessing a Node","text":"

      Accessing nodes in a linked list is less efficient. As mentioned in the previous section, we can access any element in an array in \\(O(1)\\) time. This is not the case with linked lists. The program needs to start from the head node and traverse backward one by one until the target node is found. That is, accessing the \\(i\\)-th node in a linked list requires \\(i - 1\\) iterations, with a time complexity of \\(O(n)\\).

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
      def access(head: ListNode, index: int) -> ListNode | None:\n    \"\"\"Access the node at index index in the linked list\"\"\"\n    for _ in range(index):\n        if not head:\n            return None\n        head = head.next\n    return head\n
      linked_list.cpp
      /* Access the node at index index in the linked list */\nListNode *access(ListNode *head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == nullptr)\n            return nullptr;\n        head = head->next;\n    }\n    return head;\n}\n
      linked_list.java
      /* Access the node at index index in the linked list */\nListNode access(ListNode head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == null)\n            return null;\n        head = head.next;\n    }\n    return head;\n}\n
      linked_list.cs
      /* Access the node at index index in the linked list */\nListNode? Access(ListNode? head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == null)\n            return null;\n        head = head.next;\n    }\n    return head;\n}\n
      linked_list.go
      /* Access the node at index index in the linked list */\nfunc access(head *ListNode, index int) *ListNode {\n    for i := 0; i < index; i++ {\n        if head == nil {\n            return nil\n        }\n        head = head.Next\n    }\n    return head\n}\n
      linked_list.swift
      /* Access the node at index index in the linked list */\nfunc access(head: ListNode, index: Int) -> ListNode? {\n    var head: ListNode? = head\n    for _ in 0 ..< index {\n        if head == nil {\n            return nil\n        }\n        head = head?.next\n    }\n    return head\n}\n
      linked_list.js
      /* Access the node at index index in the linked list */\nfunction access(head, index) {\n    for (let i = 0; i < index; i++) {\n        if (!head) {\n            return null;\n        }\n        head = head.next;\n    }\n    return head;\n}\n
      linked_list.ts
      /* Access the node at index index in the linked list */\nfunction access(head: ListNode | null, index: number): ListNode | null {\n    for (let i = 0; i < index; i++) {\n        if (!head) {\n            return null;\n        }\n        head = head.next;\n    }\n    return head;\n}\n
      linked_list.dart
      /* Access the node at index index in the linked list */\nListNode? access(ListNode? head, int index) {\n  for (var i = 0; i < index; i++) {\n    if (head == null) return null;\n    head = head.next;\n  }\n  return head;\n}\n
      linked_list.rs
      /* Access the node at index index in the linked list */\npub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Option<Rc<RefCell<ListNode<T>>>> {\n    fn dfs<T>(\n        head: Option<&Rc<RefCell<ListNode<T>>>>,\n        index: i32,\n    ) -> Option<Rc<RefCell<ListNode<T>>>> {\n        if index <= 0 {\n            return head.cloned();\n        }\n\n        if let Some(node) = head {\n            dfs(node.borrow().next.as_ref(), index - 1)\n        } else {\n            None\n        }\n    }\n\n    dfs(Some(head).as_ref(), index)\n}\n
      linked_list.c
      /* Access the node at index index in the linked list */\nListNode *access(ListNode *head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == NULL)\n            return NULL;\n        head = head->next;\n    }\n    return head;\n}\n
      linked_list.kt
      /* Access the node at index index in the linked list */\nfun access(head: ListNode?, index: Int): ListNode? {\n    var h = head\n    for (i in 0..<index) {\n        if (h == null)\n            return null\n        h = h.next\n    }\n    return h\n}\n
      linked_list.rb
      ### Access node at index in linked list ###\ndef access(head, index)\n  for i in 0...index\n    return nil if head.nil?\n    head = head.next\n  end\n\n  head\nend\n
      "},{"location":"chapter_array_and_linkedlist/linked_list/#5-finding-a-node","title":"5. \u00a0 Finding a Node","text":"

      Traverse the linked list to find a node with value target, and output the index of that node in the linked list. This process is also a linear search. The code is shown below:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
      def find(head: ListNode, target: int) -> int:\n    \"\"\"Find the first node with value target in the linked list\"\"\"\n    index = 0\n    while head:\n        if head.val == target:\n            return index\n        head = head.next\n        index += 1\n    return -1\n
      linked_list.cpp
      /* Find the first node with value target in the linked list */\nint find(ListNode *head, int target) {\n    int index = 0;\n    while (head != nullptr) {\n        if (head->val == target)\n            return index;\n        head = head->next;\n        index++;\n    }\n    return -1;\n}\n
      linked_list.java
      /* Find the first node with value target in the linked list */\nint find(ListNode head, int target) {\n    int index = 0;\n    while (head != null) {\n        if (head.val == target)\n            return index;\n        head = head.next;\n        index++;\n    }\n    return -1;\n}\n
      linked_list.cs
      /* Find the first node with value target in the linked list */\nint Find(ListNode? head, int target) {\n    int index = 0;\n    while (head != null) {\n        if (head.val == target)\n            return index;\n        head = head.next;\n        index++;\n    }\n    return -1;\n}\n
      linked_list.go
      /* Find the first node with value target in the linked list */\nfunc findNode(head *ListNode, target int) int {\n    index := 0\n    for head != nil {\n        if head.Val == target {\n            return index\n        }\n        head = head.Next\n        index++\n    }\n    return -1\n}\n
      linked_list.swift
      /* Find the first node with value target in the linked list */\nfunc find(head: ListNode, target: Int) -> Int {\n    var head: ListNode? = head\n    var index = 0\n    while head != nil {\n        if head?.val == target {\n            return index\n        }\n        head = head?.next\n        index += 1\n    }\n    return -1\n}\n
      linked_list.js
      /* Find the first node with value target in the linked list */\nfunction find(head, target) {\n    let index = 0;\n    while (head !== null) {\n        if (head.val === target) {\n            return index;\n        }\n        head = head.next;\n        index += 1;\n    }\n    return -1;\n}\n
      linked_list.ts
      /* Find the first node with value target in the linked list */\nfunction find(head: ListNode | null, target: number): number {\n    let index = 0;\n    while (head !== null) {\n        if (head.val === target) {\n            return index;\n        }\n        head = head.next;\n        index += 1;\n    }\n    return -1;\n}\n
      linked_list.dart
      /* Find the first node with value target in the linked list */\nint find(ListNode? head, int target) {\n  int index = 0;\n  while (head != null) {\n    if (head.val == target) {\n      return index;\n    }\n    head = head.next;\n    index++;\n  }\n  return -1;\n}\n
      linked_list.rs
      /* Find the first node with value target in the linked list */\npub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T) -> i32 {\n    fn find<T: PartialEq>(head: Option<&Rc<RefCell<ListNode<T>>>>, target: T, idx: i32) -> i32 {\n        if let Some(node) = head {\n            if node.borrow().val == target {\n                return idx;\n            }\n            return find(node.borrow().next.as_ref(), target, idx + 1);\n        } else {\n            -1\n        }\n    }\n\n    find(Some(head).as_ref(), target, 0)\n}\n
      linked_list.c
      /* Find the first node with value target in the linked list */\nint find(ListNode *head, int target) {\n    int index = 0;\n    while (head) {\n        if (head->val == target)\n            return index;\n        head = head->next;\n        index++;\n    }\n    return -1;\n}\n
      linked_list.kt
      /* Find the first node with value target in the linked list */\nfun find(head: ListNode?, target: Int): Int {\n    var index = 0\n    var h = head\n    while (h != null) {\n        if (h._val == target)\n            return index\n        h = h.next\n        index++\n    }\n    return -1\n}\n
      linked_list.rb
      ### Find first node with value target in linked list ###\ndef find(head, target)\n  index = 0\n  while head\n    return index if head.val == target\n    head = head.next\n    index += 1\n  end\n\n  -1\nend\n
      "},{"location":"chapter_array_and_linkedlist/linked_list/#422-arrays-vs-linked-lists","title":"4.2.2 \u00a0 Arrays vs. Linked Lists","text":"

      Table 4-1 summarizes the characteristics of arrays and linked lists and compares their operational efficiencies. Since they employ two opposite storage strategies, their various properties and operational efficiencies also exhibit contrasting characteristics.

      Table 4-1 \u00a0 Comparison of array and linked list efficiencies

      Array Linked List Storage method Contiguous memory space Scattered memory space Capacity expansion Immutable length Flexible expansion Memory efficiency Elements occupy less memory, but space may be wasted Elements occupy more memory Accessing an element \\(O(1)\\) \\(O(n)\\) Adding an element \\(O(n)\\) \\(O(1)\\) Removing an element \\(O(n)\\) \\(O(1)\\)"},{"location":"chapter_array_and_linkedlist/linked_list/#423-common-types-of-linked-lists","title":"4.2.3 \u00a0 Common Types of Linked Lists","text":"

      As shown in Figure 4-8, there are three common types of linked lists:

      • Singly linked list: This is the ordinary linked list introduced earlier. The nodes of a singly linked list contain a value and a reference to the next node. We call the first node the head node and the last node the tail node, which points to null None.
      • Circular linked list: If we make the tail node of a singly linked list point to the head node (connecting the tail to the head), we get a circular linked list. In a circular linked list, any node can be viewed as the head node.
      • Doubly linked list: Compared to a singly linked list, a doubly linked list records references in both directions. The node definition of a doubly linked list includes references to both the successor node (next node) and the predecessor node (previous node). Compared to a singly linked list, a doubly linked list is more flexible and can traverse the linked list in both directions, but it also requires more memory space.
      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      class ListNode:\n    \"\"\"Doubly linked list node class\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val                # Node value\n        self.next: ListNode | None = None  # Reference to the successor node\n        self.prev: ListNode | None = None  # Reference to the predecessor node\n
      /* Doubly linked list node structure */\nstruct ListNode {\n    int val;         // Node value\n    ListNode *next;  // Pointer to the successor node\n    ListNode *prev;  // Pointer to the predecessor node\n    ListNode(int x) : val(x), next(nullptr), prev(nullptr) {}  // Constructor\n};\n
      /* Doubly linked list node class */\nclass ListNode {\n    int val;        // Node value\n    ListNode next;  // Reference to the successor node\n    ListNode prev;  // Reference to the predecessor node\n    ListNode(int x) { val = x; }  // Constructor\n}\n
      /* Doubly linked list node class */\nclass ListNode(int x) {  // Constructor\n    int val = x;    // Node value\n    ListNode next;  // Reference to the successor node\n    ListNode prev;  // Reference to the predecessor node\n}\n
      /* Doubly linked list node structure */\ntype DoublyListNode struct {\n    Val  int             // Node value\n    Next *DoublyListNode // Pointer to the successor node\n    Prev *DoublyListNode // Pointer to the predecessor node\n}\n\n// NewDoublyListNode Initialization\nfunc NewDoublyListNode(val int) *DoublyListNode {\n    return &DoublyListNode{\n        Val:  val,\n        Next: nil,\n        Prev: nil,\n    }\n}\n
      /* Doubly linked list node class */\nclass ListNode {\n    var val: Int // Node value\n    var next: ListNode? // Reference to the successor node\n    var prev: ListNode? // Reference to the predecessor node\n\n    init(x: Int) { // Constructor\n        val = x\n    }\n}\n
      /* Doubly linked list node class */\nclass ListNode {\n    constructor(val, next, prev) {\n        this.val = val  ===  undefined ? 0 : val;        // Node value\n        this.next = next  ===  undefined ? null : next;  // Reference to the successor node\n        this.prev = prev  ===  undefined ? null : prev;  // Reference to the predecessor node\n    }\n}\n
      /* Doubly linked list node class */\nclass ListNode {\n    val: number;\n    next: ListNode | null;\n    prev: ListNode | null;\n    constructor(val?: number, next?: ListNode | null, prev?: ListNode | null) {\n        this.val = val  ===  undefined ? 0 : val;        // Node value\n        this.next = next  ===  undefined ? null : next;  // Reference to the successor node\n        this.prev = prev  ===  undefined ? null : prev;  // Reference to the predecessor node\n    }\n}\n
      /* Doubly linked list node class */\nclass ListNode {\n    int val;        // Node value\n    ListNode? next;  // Reference to the successor node\n    ListNode? prev;  // Reference to the predecessor node\n    ListNode(this.val, [this.next, this.prev]);  // Constructor\n}\n
      use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* Doubly linked list node type */\n#[derive(Debug)]\nstruct ListNode {\n    val: i32, // Node value\n    next: Option<Rc<RefCell<ListNode>>>, // Pointer to the successor node\n    prev: Option<Rc<RefCell<ListNode>>>, // Pointer to the predecessor node\n}\n\n/* Constructor */\nimpl ListNode {\n    fn new(val: i32) -> Self {\n        ListNode {\n            val,\n            next: None,\n            prev: None,\n        }\n    }\n}\n
      /* Doubly linked list node structure */\ntypedef struct ListNode {\n    int val;               // Node value\n    struct ListNode *next; // Pointer to the successor node\n    struct ListNode *prev; // Pointer to the predecessor node\n} ListNode;\n\n/* Constructor */\nListNode *newListNode(int val) {\n    ListNode *node;\n    node = (ListNode *) malloc(sizeof(ListNode));\n    node->val = val;\n    node->next = NULL;\n    node->prev = NULL;\n    return node;\n}\n
      /* Doubly linked list node class */\n// Constructor\nclass ListNode(x: Int) {\n    val _val: Int = x           // Node value\n    val next: ListNode? = null  // Reference to the successor node\n    val prev: ListNode? = null  // Reference to the predecessor node\n}\n
      # Doubly linked list node class\nclass ListNode\n  attr_accessor :val    # Node value\n  attr_accessor :next   # Reference to the successor node\n  attr_accessor :prev   # Reference to the predecessor node\n\n  def initialize(val=0, next_node=nil, prev_node=nil)\n    @val = val\n    @next = next_node\n    @prev = prev_node\n  end\nend\n

      Figure 4-8 \u00a0 Common types of linked lists

      "},{"location":"chapter_array_and_linkedlist/linked_list/#424-typical-applications-of-linked-lists","title":"4.2.4 \u00a0 Typical Applications of Linked Lists","text":"

      Singly linked lists are commonly used to implement stacks, queues, hash tables, and graphs.

      • Stacks and queues: When insertion and deletion operations both occur at one end of the linked list, it exhibits last-in-first-out characteristics, corresponding to a stack. When insertion operations occur at one end of the linked list and deletion operations occur at the other end, it exhibits first-in-first-out characteristics, corresponding to a queue.
      • Hash tables: Separate chaining is one of the mainstream solutions for resolving hash collisions. In this approach, all colliding elements are placed in a linked list.
      • Graphs: An adjacency list is a common way to represent a graph, where each vertex in the graph is associated with a linked list, and each element in the linked list represents another vertex connected to that vertex.

      Doubly linked lists are commonly used in scenarios where quick access to the previous and next elements is needed.

      • Advanced data structures: For example, in red-black trees and B-trees, we need to access the parent node of a node, which can be achieved by saving a reference to the parent node in the node, similar to a doubly linked list.
      • Browser history: In web browsers, when a user clicks the forward or backward button, the browser needs to know the previous and next web pages the user visited. The characteristics of doubly linked lists make this operation simple.
      • LRU algorithm: In cache eviction (LRU) algorithms, we need to quickly find the least recently used data and support quick addition and deletion of nodes. Using a doubly linked list is very suitable for this.

      Circular linked lists are commonly used in scenarios that require periodic operations, such as operating system resource scheduling.

      • Round-robin scheduling algorithm: In operating systems, round-robin scheduling is a common CPU scheduling algorithm that needs to cycle through a set of processes. Each process is assigned a time slice, and when the time slice expires, the CPU switches to the next process. This cyclic operation can be implemented using a circular linked list.
      • Data buffers: In some data buffer implementations, circular linked lists may also be used. For example, in audio and video players, the data stream may be divided into multiple buffer blocks and placed in a circular linked list to achieve seamless playback.
      "},{"location":"chapter_array_and_linkedlist/list/","title":"4.3 \u00a0 List","text":"

      A list is an abstract data structure concept that represents an ordered collection of elements, supporting operations such as element access, modification, insertion, deletion, and traversal, without requiring users to consider capacity limitations. Lists can be implemented based on linked lists or arrays.

      • A linked list can naturally be viewed as a list, supporting element insertion, deletion, search, and modification operations, and can flexibly expand dynamically.
      • An array also supports element insertion, deletion, search, and modification, but since its length is immutable, it can only be viewed as a list with length limitations.

      When implementing lists using arrays, the immutable length property reduces the practicality of the list. This is because we usually cannot determine in advance how much data we need to store, making it difficult to choose an appropriate list length. If the length is too small, it may fail to meet usage requirements; if the length is too large, it will waste memory space.

      To solve this problem, we can use a dynamic array to implement a list. It inherits all the advantages of arrays and can dynamically expand during program execution.

      In fact, the lists provided in the standard libraries of many programming languages are implemented based on dynamic arrays, such as list in Python, ArrayList in Java, vector in C++, and List in C#. In the following discussion, we will treat \"list\" and \"dynamic array\" as equivalent concepts.

      "},{"location":"chapter_array_and_linkedlist/list/#431-common-list-operations","title":"4.3.1 \u00a0 Common List Operations","text":""},{"location":"chapter_array_and_linkedlist/list/#1-initialize-a-list","title":"1. \u00a0 Initialize a List","text":"

      We typically use two initialization methods: \"without initial values\" and \"with initial values\":

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
      # Initialize a list\n# Without initial values\nnums1: list[int] = []\n# With initial values\nnums: list[int] = [1, 3, 2, 5, 4]\n
      list.cpp
      /* Initialize a list */\n// Note that vector in C++ is equivalent to nums as described in this article\n// Without initial values\nvector<int> nums1;\n// With initial values\nvector<int> nums = { 1, 3, 2, 5, 4 };\n
      list.java
      /* Initialize a list */\n// Without initial values\nList<Integer> nums1 = new ArrayList<>();\n// With initial values (note that array elements should use the wrapper class Integer[] instead of int[])\nInteger[] numbers = new Integer[] { 1, 3, 2, 5, 4 };\nList<Integer> nums = new ArrayList<>(Arrays.asList(numbers));\n
      list.cs
      /* Initialize a list */\n// Without initial values\nList<int> nums1 = [];\n// With initial values\nint[] numbers = [1, 3, 2, 5, 4];\nList<int> nums = [.. numbers];\n
      list_test.go
      /* Initialize a list */\n// Without initial values\nnums1 := []int{}\n// With initial values\nnums := []int{1, 3, 2, 5, 4}\n
      list.swift
      /* Initialize a list */\n// Without initial values\nlet nums1: [Int] = []\n// With initial values\nvar nums = [1, 3, 2, 5, 4]\n
      list.js
      /* Initialize a list */\n// Without initial values\nconst nums1 = [];\n// With initial values\nconst nums = [1, 3, 2, 5, 4];\n
      list.ts
      /* Initialize a list */\n// Without initial values\nconst nums1: number[] = [];\n// With initial values\nconst nums: number[] = [1, 3, 2, 5, 4];\n
      list.dart
      /* Initialize a list */\n// Without initial values\nList<int> nums1 = [];\n// With initial values\nList<int> nums = [1, 3, 2, 5, 4];\n
      list.rs
      /* Initialize a list */\n// Without initial values\nlet nums1: Vec<i32> = Vec::new();\n// With initial values\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
      list.c
      // C does not provide built-in dynamic arrays\n
      list.kt
      /* Initialize a list */\n// Without initial values\nvar nums1 = listOf<Int>()\n// With initial values\nvar numbers = arrayOf(1, 3, 2, 5, 4)\nvar nums = numbers.toMutableList()\n
      list.rb
      # Initialize a list\n# Without initial values\nnums1 = []\n# With initial values\nnums = [1, 3, 2, 5, 4]\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_array_and_linkedlist/list/#2-access-elements","title":"2. \u00a0 Access Elements","text":"

      Since a list is essentially an array, we can access and update elements in \\(O(1)\\) time complexity, which is very efficient.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
      # Access an element\nnum: int = nums[1]  # Access element at index 1\n\n# Update an element\nnums[1] = 0    # Update element at index 1 to 0\n
      list.cpp
      /* Access an element */\nint num = nums[1];  // Access element at index 1\n\n/* Update an element */\nnums[1] = 0;  // Update element at index 1 to 0\n
      list.java
      /* Access an element */\nint num = nums.get(1);  // Access element at index 1\n\n/* Update an element */\nnums.set(1, 0);  // Update element at index 1 to 0\n
      list.cs
      /* Access an element */\nint num = nums[1];  // Access element at index 1\n\n/* Update an element */\nnums[1] = 0;  // Update element at index 1 to 0\n
      list_test.go
      /* Access an element */\nnum := nums[1]  // Access element at index 1\n\n/* Update an element */\nnums[1] = 0     // Update element at index 1 to 0\n
      list.swift
      /* Access an element */\nlet num = nums[1] // Access element at index 1\n\n/* Update an element */\nnums[1] = 0 // Update element at index 1 to 0\n
      list.js
      /* Access an element */\nconst num = nums[1];  // Access element at index 1\n\n/* Update an element */\nnums[1] = 0;  // Update element at index 1 to 0\n
      list.ts
      /* Access an element */\nconst num: number = nums[1];  // Access element at index 1\n\n/* Update an element */\nnums[1] = 0;  // Update element at index 1 to 0\n
      list.dart
      /* Access an element */\nint num = nums[1];  // Access element at index 1\n\n/* Update an element */\nnums[1] = 0;  // Update element at index 1 to 0\n
      list.rs
      /* Access an element */\nlet num: i32 = nums[1];  // Access element at index 1\n/* Update an element */\nnums[1] = 0;             // Update element at index 1 to 0\n
      list.c
      // C does not provide built-in dynamic arrays\n
      list.kt
      /* Access an element */\nval num = nums[1]       // Access element at index 1\n/* Update an element */\nnums[1] = 0             // Update element at index 1 to 0\n
      list.rb
      # Access an element\nnum = nums[1] # Access element at index 1\n# Update an element\nnums[1] = 0 # Update element at index 1 to 0\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_array_and_linkedlist/list/#3-insert-and-delete-elements","title":"3. \u00a0 Insert and Delete Elements","text":"

      Compared to arrays, lists can freely add and delete elements. Adding an element at the end of a list has a time complexity of \\(O(1)\\), but inserting and deleting elements still have the same efficiency as arrays, with a time complexity of \\(O(n)\\).

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
      # Clear the list\nnums.clear()\n\n# Add elements at the end\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n# Insert an element in the middle\nnums.insert(3, 6)  # Insert number 6 at index 3\n\n# Delete an element\nnums.pop(3)        # Delete element at index 3\n
      list.cpp
      /* Clear the list */\nnums.clear();\n\n/* Add elements at the end */\nnums.push_back(1);\nnums.push_back(3);\nnums.push_back(2);\nnums.push_back(5);\nnums.push_back(4);\n\n/* Insert an element in the middle */\nnums.insert(nums.begin() + 3, 6);  // Insert number 6 at index 3\n\n/* Delete an element */\nnums.erase(nums.begin() + 3);      // Delete element at index 3\n
      list.java
      /* Clear the list */\nnums.clear();\n\n/* Add elements at the end */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* Insert an element in the middle */\nnums.add(3, 6);  // Insert number 6 at index 3\n\n/* Delete an element */\nnums.remove(3);  // Delete element at index 3\n
      list.cs
      /* Clear the list */\nnums.Clear();\n\n/* Add elements at the end */\nnums.Add(1);\nnums.Add(3);\nnums.Add(2);\nnums.Add(5);\nnums.Add(4);\n\n/* Insert an element in the middle */\nnums.Insert(3, 6);  // Insert number 6 at index 3\n\n/* Delete an element */\nnums.RemoveAt(3);  // Delete element at index 3\n
      list_test.go
      /* Clear the list */\nnums = nil\n\n/* Add elements at the end */\nnums = append(nums, 1)\nnums = append(nums, 3)\nnums = append(nums, 2)\nnums = append(nums, 5)\nnums = append(nums, 4)\n\n/* Insert an element in the middle */\nnums = append(nums[:3], append([]int{6}, nums[3:]...)...) // Insert number 6 at index 3\n\n/* Delete an element */\nnums = append(nums[:3], nums[4:]...) // Delete element at index 3\n
      list.swift
      /* Clear the list */\nnums.removeAll()\n\n/* Add elements at the end */\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n/* Insert an element in the middle */\nnums.insert(6, at: 3) // Insert number 6 at index 3\n\n/* Delete an element */\nnums.remove(at: 3) // Delete element at index 3\n
      list.js
      /* Clear the list */\nnums.length = 0;\n\n/* Add elements at the end */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* Insert an element in the middle */\nnums.splice(3, 0, 6); // Insert number 6 at index 3\n\n/* Delete an element */\nnums.splice(3, 1);  // Delete element at index 3\n
      list.ts
      /* Clear the list */\nnums.length = 0;\n\n/* Add elements at the end */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* Insert an element in the middle */\nnums.splice(3, 0, 6); // Insert number 6 at index 3\n\n/* Delete an element */\nnums.splice(3, 1);  // Delete element at index 3\n
      list.dart
      /* Clear the list */\nnums.clear();\n\n/* Add elements at the end */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* Insert an element in the middle */\nnums.insert(3, 6); // Insert number 6 at index 3\n\n/* Delete an element */\nnums.removeAt(3); // Delete element at index 3\n
      list.rs
      /* Clear the list */\nnums.clear();\n\n/* Add elements at the end */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* Insert an element in the middle */\nnums.insert(3, 6);  // Insert number 6 at index 3\n\n/* Delete an element */\nnums.remove(3);    // Delete element at index 3\n
      list.c
      // C does not provide built-in dynamic arrays\n
      list.kt
      /* Clear the list */\nnums.clear();\n\n/* Add elements at the end */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* Insert an element in the middle */\nnums.add(3, 6);  // Insert number 6 at index 3\n\n/* Delete an element */\nnums.remove(3);  // Delete element at index 3\n
      list.rb
      # Clear the list\nnums.clear\n\n# Add elements at the end\nnums << 1\nnums << 3\nnums << 2\nnums << 5\nnums << 4\n\n# Insert an element in the middle\nnums.insert(3, 6) # Insert number 6 at index 3\n\n# Delete an element\nnums.delete_at(3) # Delete element at index 3\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_array_and_linkedlist/list/#4-traverse-a-list","title":"4. \u00a0 Traverse a List","text":"

      Like arrays, lists can be traversed by index or by directly iterating through elements.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
      # Traverse the list by index\ncount = 0\nfor i in range(len(nums)):\n    count += nums[i]\n\n# Traverse list elements directly\nfor num in nums:\n    count += num\n
      list.cpp
      /* Traverse the list by index */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\n    count += nums[i];\n}\n\n/* Traverse list elements directly */\ncount = 0;\nfor (int num : nums) {\n    count += num;\n}\n
      list.java
      /* Traverse the list by index */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\n    count += nums.get(i);\n}\n\n/* Traverse list elements directly */\nfor (int num : nums) {\n    count += num;\n}\n
      list.cs
      /* Traverse the list by index */\nint count = 0;\nfor (int i = 0; i < nums.Count; i++) {\n    count += nums[i];\n}\n\n/* Traverse list elements directly */\ncount = 0;\nforeach (int num in nums) {\n    count += num;\n}\n
      list_test.go
      /* Traverse the list by index */\ncount := 0\nfor i := 0; i < len(nums); i++ {\n    count += nums[i]\n}\n\n/* Traverse list elements directly */\ncount = 0\nfor _, num := range nums {\n    count += num\n}\n
      list.swift
      /* Traverse the list by index */\nvar count = 0\nfor i in nums.indices {\n    count += nums[i]\n}\n\n/* Traverse list elements directly */\ncount = 0\nfor num in nums {\n    count += num\n}\n
      list.js
      /* Traverse the list by index */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\n    count += nums[i];\n}\n\n/* Traverse list elements directly */\ncount = 0;\nfor (const num of nums) {\n    count += num;\n}\n
      list.ts
      /* Traverse the list by index */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\n    count += nums[i];\n}\n\n/* Traverse list elements directly */\ncount = 0;\nfor (const num of nums) {\n    count += num;\n}\n
      list.dart
      /* Traverse the list by index */\nint count = 0;\nfor (var i = 0; i < nums.length; i++) {\n    count += nums[i];\n}\n\n/* Traverse list elements directly */\ncount = 0;\nfor (var num in nums) {\n    count += num;\n}\n
      list.rs
      // Traverse the list by index\nlet mut _count = 0;\nfor i in 0..nums.len() {\n    _count += nums[i];\n}\n\n// Traverse list elements directly\n_count = 0;\nfor num in &nums {\n    _count += num;\n}\n
      list.c
      // C does not provide built-in dynamic arrays\n
      list.kt
      /* Traverse the list by index */\nvar count = 0\nfor (i in nums.indices) {\n    count += nums[i]\n}\n\n/* Traverse list elements directly */\nfor (num in nums) {\n    count += num\n}\n
      list.rb
      # Traverse the list by index\ncount = 0\nfor i in 0...nums.length\n    count += nums[i]\nend\n\n# Traverse list elements directly\ncount = 0\nfor num in nums\n    count += num\nend\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_array_and_linkedlist/list/#5-concatenate-lists","title":"5. \u00a0 Concatenate Lists","text":"

      Given a new list nums1, we can concatenate it to the end of the original list.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
      # Concatenate two lists\nnums1: list[int] = [6, 8, 7, 10, 9]\nnums += nums1  # Concatenate list nums1 to the end of nums\n
      list.cpp
      /* Concatenate two lists */\nvector<int> nums1 = { 6, 8, 7, 10, 9 };\n// Concatenate list nums1 to the end of nums\nnums.insert(nums.end(), nums1.begin(), nums1.end());\n
      list.java
      /* Concatenate two lists */\nList<Integer> nums1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));\nnums.addAll(nums1);  // Concatenate list nums1 to the end of nums\n
      list.cs
      /* Concatenate two lists */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.AddRange(nums1);  // Concatenate list nums1 to the end of nums\n
      list_test.go
      /* Concatenate two lists */\nnums1 := []int{6, 8, 7, 10, 9}\nnums = append(nums, nums1...)  // Concatenate list nums1 to the end of nums\n
      list.swift
      /* Concatenate two lists */\nlet nums1 = [6, 8, 7, 10, 9]\nnums.append(contentsOf: nums1) // Concatenate list nums1 to the end of nums\n
      list.js
      /* Concatenate two lists */\nconst nums1 = [6, 8, 7, 10, 9];\nnums.push(...nums1);  // Concatenate list nums1 to the end of nums\n
      list.ts
      /* Concatenate two lists */\nconst nums1: number[] = [6, 8, 7, 10, 9];\nnums.push(...nums1);  // Concatenate list nums1 to the end of nums\n
      list.dart
      /* Concatenate two lists */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.addAll(nums1);  // Concatenate list nums1 to the end of nums\n
      list.rs
      /* Concatenate two lists */\nlet nums1: Vec<i32> = vec![6, 8, 7, 10, 9];\nnums.extend(nums1);\n
      list.c
      // C does not provide built-in dynamic arrays\n
      list.kt
      /* Concatenate two lists */\nval nums1 = intArrayOf(6, 8, 7, 10, 9).toMutableList()\nnums.addAll(nums1)  // Concatenate list nums1 to the end of nums\n
      list.rb
      # Concatenate two lists\nnums1 = [6, 8, 7, 10, 9]\nnums += nums1\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_array_and_linkedlist/list/#6-sort-a-list","title":"6. \u00a0 Sort a List","text":"

      After sorting a list, we can use \"binary search\" and \"two-pointer\" algorithms, which are frequently tested in array algorithm problems.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
      # Sort a list\nnums.sort()  # After sorting, list elements are arranged from smallest to largest\n
      list.cpp
      /* Sort a list */\nsort(nums.begin(), nums.end());  // After sorting, list elements are arranged from smallest to largest\n
      list.java
      /* Sort a list */\nCollections.sort(nums);  // After sorting, list elements are arranged from smallest to largest\n
      list.cs
      /* Sort a list */\nnums.Sort(); // After sorting, list elements are arranged from smallest to largest\n
      list_test.go
      /* Sort a list */\nsort.Ints(nums)  // After sorting, list elements are arranged from smallest to largest\n
      list.swift
      /* Sort a list */\nnums.sort() // After sorting, list elements are arranged from smallest to largest\n
      list.js
      /* Sort a list */\nnums.sort((a, b) => a - b);  // After sorting, list elements are arranged from smallest to largest\n
      list.ts
      /* Sort a list */\nnums.sort((a, b) => a - b);  // After sorting, list elements are arranged from smallest to largest\n
      list.dart
      /* Sort a list */\nnums.sort(); // After sorting, list elements are arranged from smallest to largest\n
      list.rs
      /* Sort a list */\nnums.sort(); // After sorting, list elements are arranged from smallest to largest\n
      list.c
      // C does not provide built-in dynamic arrays\n
      list.kt
      /* Sort a list */\nnums.sort() // After sorting, list elements are arranged from smallest to largest\n
      list.rb
      # Sort a list\nnums = nums.sort { |a, b| a <=> b } # After sorting, list elements are arranged from smallest to largest\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_array_and_linkedlist/list/#432-list-implementation","title":"4.3.2 \u00a0 List Implementation","text":"

      Many programming languages have built-in lists, such as Java, C++, and Python. Their implementations are quite complex, and the parameters are carefully considered, such as initial capacity, expansion multiples, and so on. Interested readers can consult the source code to learn more.

      To deepen our understanding of how lists work, we attempt to implement a simple list with three key design considerations:

      • Initial capacity: Select a reasonable initial capacity for the underlying array. In this example, we choose 10 as the initial capacity.
      • Size tracking: Declare a variable size to record the current number of elements in the list and update it in real-time as elements are inserted and deleted. Based on this variable, we can locate the end of the list and determine whether expansion is needed.
      • Expansion mechanism: When the list capacity is full upon inserting an element, we need to expand. We create a larger array based on the expansion multiple and then move all elements from the current array to the new array in order. In this example, we specify that the array should be expanded to 2 times its previous size each time.
      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_list.py
      class MyList:\n    \"\"\"List class\"\"\"\n\n    def __init__(self):\n        \"\"\"Constructor\"\"\"\n        self._capacity: int = 10  # List capacity\n        self._arr: list[int] = [0] * self._capacity  # Array (stores list elements)\n        self._size: int = 0  # List length (current number of elements)\n        self._extend_ratio: int = 2  # Multiple by which the list capacity is extended each time\n\n    def size(self) -> int:\n        \"\"\"Get list length (current number of elements)\"\"\"\n        return self._size\n\n    def capacity(self) -> int:\n        \"\"\"Get list capacity\"\"\"\n        return self._capacity\n\n    def get(self, index: int) -> int:\n        \"\"\"Access element\"\"\"\n        # If the index is out of bounds, throw an exception, as below\n        if index < 0 or index >= self._size:\n            raise IndexError(\"Index out of bounds\")\n        return self._arr[index]\n\n    def set(self, num: int, index: int):\n        \"\"\"Update element\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"Index out of bounds\")\n        self._arr[index] = num\n\n    def add(self, num: int):\n        \"\"\"Add element at the end\"\"\"\n        # When the number of elements exceeds capacity, trigger the extension mechanism\n        if self.size() == self.capacity():\n            self.extend_capacity()\n        self._arr[self._size] = num\n        self._size += 1\n\n    def insert(self, num: int, index: int):\n        \"\"\"Insert element in the middle\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"Index out of bounds\")\n        # When the number of elements exceeds capacity, trigger the extension mechanism\n        if self._size == self.capacity():\n            self.extend_capacity()\n        # Move all elements at and after index index backward by one position\n        for j in range(self._size - 1, index - 1, -1):\n            self._arr[j + 1] = self._arr[j]\n        self._arr[index] = num\n        # Update the number of elements\n        self._size += 1\n\n    def remove(self, index: int) -> int:\n        \"\"\"Remove element\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"Index out of bounds\")\n        num = self._arr[index]\n        # Move all elements after index index forward by one position\n        for j in range(index, self._size - 1):\n            self._arr[j] = self._arr[j + 1]\n        # Update the number of elements\n        self._size -= 1\n        # Return the removed element\n        return num\n\n    def extend_capacity(self):\n        \"\"\"Extend list capacity\"\"\"\n        # Create a new array with length _extend_ratio times the original array, and copy the original array to the new array\n        self._arr = self._arr + [0] * self.capacity() * (self._extend_ratio - 1)\n        # Update list capacity\n        self._capacity = len(self._arr)\n\n    def to_array(self) -> list[int]:\n        \"\"\"Return list with valid length\"\"\"\n        return self._arr[: self._size]\n
      my_list.cpp
      /* List class */\nclass MyList {\n  private:\n    int *arr;             // Array (stores list elements)\n    int arrCapacity = 10; // List capacity\n    int arrSize = 0;      // List length (current number of elements)\n    int extendRatio = 2;   // Multiple by which the list capacity is extended each time\n\n  public:\n    /* Constructor */\n    MyList() {\n        arr = new int[arrCapacity];\n    }\n\n    /* Destructor */\n    ~MyList() {\n        delete[] arr;\n    }\n\n    /* Get list length (current number of elements)*/\n    int size() {\n        return arrSize;\n    }\n\n    /* Get list capacity */\n    int capacity() {\n        return arrCapacity;\n    }\n\n    /* Update element */\n    int get(int index) {\n        // If the index is out of bounds, throw an exception, as below\n        if (index < 0 || index >= size())\n            throw out_of_range(\"Index out of bounds\");\n        return arr[index];\n    }\n\n    /* Add elements at the end */\n    void set(int index, int num) {\n        if (index < 0 || index >= size())\n            throw out_of_range(\"Index out of bounds\");\n        arr[index] = num;\n    }\n\n    /* Direct traversal of list elements */\n    void add(int num) {\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if (size() == capacity())\n            extendCapacity();\n        arr[size()] = num;\n        // Update the number of elements\n        arrSize++;\n    }\n\n    /* Sort list */\n    void insert(int index, int num) {\n        if (index < 0 || index >= size())\n            throw out_of_range(\"Index out of bounds\");\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if (size() == capacity())\n            extendCapacity();\n        // Move all elements after index index forward by one position\n        for (int j = size() - 1; j >= index; j--) {\n            arr[j + 1] = arr[j];\n        }\n        arr[index] = num;\n        // Update the number of elements\n        arrSize++;\n    }\n\n    /* Remove element */\n    int remove(int index) {\n        if (index < 0 || index >= size())\n            throw out_of_range(\"Index out of bounds\");\n        int num = arr[index];\n        // Create a new array with length _extend_ratio times the original array, and copy the original array to the new array\n        for (int j = index; j < size() - 1; j++) {\n            arr[j] = arr[j + 1];\n        }\n        // Update the number of elements\n        arrSize--;\n        // Return the removed element\n        return num;\n    }\n\n    /* Driver Code */\n    void extendCapacity() {\n        // Create a new array with length extendRatio times the original array\n        int newCapacity = capacity() * extendRatio;\n        int *tmp = arr;\n        arr = new int[newCapacity];\n        // Copy all elements from the original array to the new array\n        for (int i = 0; i < size(); i++) {\n            arr[i] = tmp[i];\n        }\n        // Free memory\n        delete[] tmp;\n        arrCapacity = newCapacity;\n    }\n\n    /* Convert list to Vector for printing */\n    vector<int> toVector() {\n        // Elements enqueue\n        vector<int> vec(size());\n        for (int i = 0; i < size(); i++) {\n            vec[i] = arr[i];\n        }\n        return vec;\n    }\n};\n
      my_list.java
      /* List class */\nclass MyList {\n    private int[] arr; // Array (stores list elements)\n    private int capacity = 10; // List capacity\n    private int size = 0; // List length (current number of elements)\n    private int extendRatio = 2; // Multiple by which the list capacity is extended each time\n\n    /* Constructor */\n    public MyList() {\n        arr = new int[capacity];\n    }\n\n    /* Get list length (current number of elements) */\n    public int size() {\n        return size;\n    }\n\n    /* Get list capacity */\n    public int capacity() {\n        return capacity;\n    }\n\n    /* Update element */\n    public int get(int index) {\n        // If the index is out of bounds, throw an exception, as below\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"Index out of bounds\");\n        return arr[index];\n    }\n\n    /* Add elements at the end */\n    public void set(int index, int num) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"Index out of bounds\");\n        arr[index] = num;\n    }\n\n    /* Direct traversal of list elements */\n    public void add(int num) {\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if (size == capacity())\n            extendCapacity();\n        arr[size] = num;\n        // Update the number of elements\n        size++;\n    }\n\n    /* Sort list */\n    public void insert(int index, int num) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"Index out of bounds\");\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if (size == capacity())\n            extendCapacity();\n        // Move all elements after index index forward by one position\n        for (int j = size - 1; j >= index; j--) {\n            arr[j + 1] = arr[j];\n        }\n        arr[index] = num;\n        // Update the number of elements\n        size++;\n    }\n\n    /* Remove element */\n    public int remove(int index) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"Index out of bounds\");\n        int num = arr[index];\n        // Move all elements after index forward by one position\n        for (int j = index; j < size - 1; j++) {\n            arr[j] = arr[j + 1];\n        }\n        // Update the number of elements\n        size--;\n        // Return the removed element\n        return num;\n    }\n\n    /* Driver Code */\n    public void extendCapacity() {\n        // Create a new array with length extendRatio times the original array and copy the original array to the new array\n        arr = Arrays.copyOf(arr, capacity() * extendRatio);\n        // Add elements at the end\n        capacity = arr.length;\n    }\n\n    /* Convert list to array */\n    public int[] toArray() {\n        int size = size();\n        // Elements enqueue\n        int[] arr = new int[size];\n        for (int i = 0; i < size; i++) {\n            arr[i] = get(i);\n        }\n        return arr;\n    }\n}\n
      my_list.cs
      /* List class */\nclass MyList {\n    private int[] arr;           // Array (stores list elements)\n    private int arrCapacity = 10;    // List capacity\n    private int arrSize = 0;         // List length (current number of elements)\n    private readonly int extendRatio = 2;  // Multiple by which the list capacity is extended each time\n\n    /* Constructor */\n    public MyList() {\n        arr = new int[arrCapacity];\n    }\n\n    /* Get list length (current number of elements) */\n    public int Size() {\n        return arrSize;\n    }\n\n    /* Get list capacity */\n    public int Capacity() {\n        return arrCapacity;\n    }\n\n    /* Update element */\n    public int Get(int index) {\n        // If the index is out of bounds, throw an exception, as below\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"Index out of bounds\");\n        return arr[index];\n    }\n\n    /* Add elements at the end */\n    public void Set(int index, int num) {\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"Index out of bounds\");\n        arr[index] = num;\n    }\n\n    /* Direct traversal of list elements */\n    public void Add(int num) {\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if (arrSize == arrCapacity)\n            ExtendCapacity();\n        arr[arrSize] = num;\n        // Update the number of elements\n        arrSize++;\n    }\n\n    /* Sort list */\n    public void Insert(int index, int num) {\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"Index out of bounds\");\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if (arrSize == arrCapacity)\n            ExtendCapacity();\n        // Move all elements after index index forward by one position\n        for (int j = arrSize - 1; j >= index; j--) {\n            arr[j + 1] = arr[j];\n        }\n        arr[index] = num;\n        // Update the number of elements\n        arrSize++;\n    }\n\n    /* Remove element */\n    public int Remove(int index) {\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"Index out of bounds\");\n        int num = arr[index];\n        // Move all elements after index forward by one position\n        for (int j = index; j < arrSize - 1; j++) {\n            arr[j] = arr[j + 1];\n        }\n        // Update the number of elements\n        arrSize--;\n        // Return the removed element\n        return num;\n    }\n\n    /* Driver Code */\n    public void ExtendCapacity() {\n        // Create new array of length arrCapacity * extendRatio and copy original array to new array\n        Array.Resize(ref arr, arrCapacity * extendRatio);\n        // Add elements at the end\n        arrCapacity = arr.Length;\n    }\n\n    /* Convert list to array */\n    public int[] ToArray() {\n        // Elements enqueue\n        int[] arr = new int[arrSize];\n        for (int i = 0; i < arrSize; i++) {\n            arr[i] = Get(i);\n        }\n        return arr;\n    }\n}\n
      my_list.go
      /* List class */\ntype myList struct {\n    arrCapacity int\n    arr         []int\n    arrSize     int\n    extendRatio int\n}\n\n/* Constructor */\nfunc newMyList() *myList {\n    return &myList{\n        arrCapacity: 10,              // List capacity\n        arr:         make([]int, 10), // Array (stores list elements)\n        arrSize:     0,               // List length (current number of elements)\n        extendRatio: 2,               // Multiple by which the list capacity is extended each time\n    }\n}\n\n/* Get list length (current number of elements) */\nfunc (l *myList) size() int {\n    return l.arrSize\n}\n\n/* Get list capacity */\nfunc (l *myList) capacity() int {\n    return l.arrCapacity\n}\n\n/* Update element */\nfunc (l *myList) get(index int) int {\n    // If the index is out of bounds, throw an exception, as below\n    if index < 0 || index >= l.arrSize {\n        panic(\"Index out of bounds\")\n    }\n    return l.arr[index]\n}\n\n/* Add elements at the end */\nfunc (l *myList) set(num, index int) {\n    if index < 0 || index >= l.arrSize {\n        panic(\"Index out of bounds\")\n    }\n    l.arr[index] = num\n}\n\n/* Direct traversal of list elements */\nfunc (l *myList) add(num int) {\n    // When the number of elements exceeds capacity, trigger the extension mechanism\n    if l.arrSize == l.arrCapacity {\n        l.extendCapacity()\n    }\n    l.arr[l.arrSize] = num\n    // Update the number of elements\n    l.arrSize++\n}\n\n/* Sort list */\nfunc (l *myList) insert(num, index int) {\n    if index < 0 || index >= l.arrSize {\n        panic(\"Index out of bounds\")\n    }\n    // When the number of elements exceeds capacity, trigger the extension mechanism\n    if l.arrSize == l.arrCapacity {\n        l.extendCapacity()\n    }\n    // Move all elements after index index forward by one position\n    for j := l.arrSize - 1; j >= index; j-- {\n        l.arr[j+1] = l.arr[j]\n    }\n    l.arr[index] = num\n    // Update the number of elements\n    l.arrSize++\n}\n\n/* Remove element */\nfunc (l *myList) remove(index int) int {\n    if index < 0 || index >= l.arrSize {\n        panic(\"Index out of bounds\")\n    }\n    num := l.arr[index]\n    // Create a new array with length _extend_ratio times the original array, and copy the original array to the new array\n    for j := index; j < l.arrSize-1; j++ {\n        l.arr[j] = l.arr[j+1]\n    }\n    // Update the number of elements\n    l.arrSize--\n    // Return the removed element\n    return num\n}\n\n/* Driver Code */\nfunc (l *myList) extendCapacity() {\n    // Create a new array with length extendRatio times the original array and copy the original array to the new array\n    l.arr = append(l.arr, make([]int, l.arrCapacity*(l.extendRatio-1))...)\n    // Add elements at the end\n    l.arrCapacity = len(l.arr)\n}\n\n/* Return list with valid length */\nfunc (l *myList) toArray() []int {\n    // Elements enqueue\n    return l.arr[:l.arrSize]\n}\n
      my_list.swift
      /* List class */\nclass MyList {\n    private var arr: [Int] // Array (stores list elements)\n    private var _capacity: Int // List capacity\n    private var _size: Int // List length (current number of elements)\n    private let extendRatio: Int // Multiple by which the list capacity is extended each time\n\n    /* Constructor */\n    init() {\n        _capacity = 10\n        _size = 0\n        extendRatio = 2\n        arr = Array(repeating: 0, count: _capacity)\n    }\n\n    /* Get list length (current number of elements) */\n    func size() -> Int {\n        _size\n    }\n\n    /* Get list capacity */\n    func capacity() -> Int {\n        _capacity\n    }\n\n    /* Update element */\n    func get(index: Int) -> Int {\n        // Throw error if index out of bounds, same below\n        if index < 0 || index >= size() {\n            fatalError(\"Index out of bounds\")\n        }\n        return arr[index]\n    }\n\n    /* Add elements at the end */\n    func set(index: Int, num: Int) {\n        if index < 0 || index >= size() {\n            fatalError(\"Index out of bounds\")\n        }\n        arr[index] = num\n    }\n\n    /* Direct traversal of list elements */\n    func add(num: Int) {\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if size() == capacity() {\n            extendCapacity()\n        }\n        arr[size()] = num\n        // Update the number of elements\n        _size += 1\n    }\n\n    /* Sort list */\n    func insert(index: Int, num: Int) {\n        if index < 0 || index >= size() {\n            fatalError(\"Index out of bounds\")\n        }\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if size() == capacity() {\n            extendCapacity()\n        }\n        // Move all elements after index index forward by one position\n        for j in (index ..< size()).reversed() {\n            arr[j + 1] = arr[j]\n        }\n        arr[index] = num\n        // Update the number of elements\n        _size += 1\n    }\n\n    /* Remove element */\n    @discardableResult\n    func remove(index: Int) -> Int {\n        if index < 0 || index >= size() {\n            fatalError(\"Index out of bounds\")\n        }\n        let num = arr[index]\n        // Move all elements after index forward by one position\n        for j in index ..< (size() - 1) {\n            arr[j] = arr[j + 1]\n        }\n        // Update the number of elements\n        _size -= 1\n        // Return the removed element\n        return num\n    }\n\n    /* Driver Code */\n    func extendCapacity() {\n        // Create a new array with length extendRatio times the original array and copy the original array to the new array\n        arr = arr + Array(repeating: 0, count: capacity() * (extendRatio - 1))\n        // Add elements at the end\n        _capacity = arr.count\n    }\n\n    /* Convert list to array */\n    func toArray() -> [Int] {\n        Array(arr.prefix(size()))\n    }\n}\n
      my_list.js
      /* List class */\nclass MyList {\n    #arr = new Array(); // Array (stores list elements)\n    #capacity = 10; // List capacity\n    #size = 0; // List length (current number of elements)\n    #extendRatio = 2; // Multiple by which the list capacity is extended each time\n\n    /* Constructor */\n    constructor() {\n        this.#arr = new Array(this.#capacity);\n    }\n\n    /* Get list length (current number of elements) */\n    size() {\n        return this.#size;\n    }\n\n    /* Get list capacity */\n    capacity() {\n        return this.#capacity;\n    }\n\n    /* Update element */\n    get(index) {\n        // If the index is out of bounds, throw an exception, as below\n        if (index < 0 || index >= this.#size) throw new Error('Index out of bounds');\n        return this.#arr[index];\n    }\n\n    /* Add elements at the end */\n    set(index, num) {\n        if (index < 0 || index >= this.#size) throw new Error('Index out of bounds');\n        this.#arr[index] = num;\n    }\n\n    /* Direct traversal of list elements */\n    add(num) {\n        // If length equals capacity, need to expand\n        if (this.#size === this.#capacity) {\n            this.extendCapacity();\n        }\n        // Add new element to end of list\n        this.#arr[this.#size] = num;\n        this.#size++;\n    }\n\n    /* Sort list */\n    insert(index, num) {\n        if (index < 0 || index >= this.#size) throw new Error('Index out of bounds');\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if (this.#size === this.#capacity) {\n            this.extendCapacity();\n        }\n        // Move all elements after index index forward by one position\n        for (let j = this.#size - 1; j >= index; j--) {\n            this.#arr[j + 1] = this.#arr[j];\n        }\n        // Update the number of elements\n        this.#arr[index] = num;\n        this.#size++;\n    }\n\n    /* Remove element */\n    remove(index) {\n        if (index < 0 || index >= this.#size) throw new Error('Index out of bounds');\n        let num = this.#arr[index];\n        // Create a new array with length _extend_ratio times the original array, and copy the original array to the new array\n        for (let j = index; j < this.#size - 1; j++) {\n            this.#arr[j] = this.#arr[j + 1];\n        }\n        // Update the number of elements\n        this.#size--;\n        // Return the removed element\n        return num;\n    }\n\n    /* Driver Code */\n    extendCapacity() {\n        // Create a new array with length extendRatio times the original array and copy the original array to the new array\n        this.#arr = this.#arr.concat(\n            new Array(this.capacity() * (this.#extendRatio - 1))\n        );\n        // Add elements at the end\n        this.#capacity = this.#arr.length;\n    }\n\n    /* Convert list to array */\n    toArray() {\n        let size = this.size();\n        // Elements enqueue\n        const arr = new Array(size);\n        for (let i = 0; i < size; i++) {\n            arr[i] = this.get(i);\n        }\n        return arr;\n    }\n}\n
      my_list.ts
      /* List class */\nclass MyList {\n    private arr: Array<number>; // Array (stores list elements)\n    private _capacity: number = 10; // List capacity\n    private _size: number = 0; // List length (current number of elements)\n    private extendRatio: number = 2; // Multiple by which the list capacity is extended each time\n\n    /* Constructor */\n    constructor() {\n        this.arr = new Array(this._capacity);\n    }\n\n    /* Get list length (current number of elements) */\n    public size(): number {\n        return this._size;\n    }\n\n    /* Get list capacity */\n    public capacity(): number {\n        return this._capacity;\n    }\n\n    /* Update element */\n    public get(index: number): number {\n        // If the index is out of bounds, throw an exception, as below\n        if (index < 0 || index >= this._size) throw new Error('Index out of bounds');\n        return this.arr[index];\n    }\n\n    /* Add elements at the end */\n    public set(index: number, num: number): void {\n        if (index < 0 || index >= this._size) throw new Error('Index out of bounds');\n        this.arr[index] = num;\n    }\n\n    /* Direct traversal of list elements */\n    public add(num: number): void {\n        // If length equals capacity, need to expand\n        if (this._size === this._capacity) this.extendCapacity();\n        // Add new element to end of list\n        this.arr[this._size] = num;\n        this._size++;\n    }\n\n    /* Sort list */\n    public insert(index: number, num: number): void {\n        if (index < 0 || index >= this._size) throw new Error('Index out of bounds');\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if (this._size === this._capacity) {\n            this.extendCapacity();\n        }\n        // Move all elements after index index forward by one position\n        for (let j = this._size - 1; j >= index; j--) {\n            this.arr[j + 1] = this.arr[j];\n        }\n        // Update the number of elements\n        this.arr[index] = num;\n        this._size++;\n    }\n\n    /* Remove element */\n    public remove(index: number): number {\n        if (index < 0 || index >= this._size) throw new Error('Index out of bounds');\n        let num = this.arr[index];\n        // Move all elements after index forward by one position\n        for (let j = index; j < this._size - 1; j++) {\n            this.arr[j] = this.arr[j + 1];\n        }\n        // Update the number of elements\n        this._size--;\n        // Return the removed element\n        return num;\n    }\n\n    /* Driver Code */\n    public extendCapacity(): void {\n        // Create new array of length size and copy original array to new array\n        this.arr = this.arr.concat(\n            new Array(this.capacity() * (this.extendRatio - 1))\n        );\n        // Add elements at the end\n        this._capacity = this.arr.length;\n    }\n\n    /* Convert list to array */\n    public toArray(): number[] {\n        let size = this.size();\n        // Elements enqueue\n        const arr = new Array(size);\n        for (let i = 0; i < size; i++) {\n            arr[i] = this.get(i);\n        }\n        return arr;\n    }\n}\n
      my_list.dart
      /* List class */\nclass MyList {\n  late List<int> _arr; // Array (stores list elements)\n  int _capacity = 10; // List capacity\n  int _size = 0; // List length (current number of elements)\n  int _extendRatio = 2; // Multiple by which the list capacity is extended each time\n\n  /* Constructor */\n  MyList() {\n    _arr = List.filled(_capacity, 0);\n  }\n\n  /* Get list length (current number of elements) */\n  int size() => _size;\n\n  /* Get list capacity */\n  int capacity() => _capacity;\n\n  /* Update element */\n  int get(int index) {\n    if (index >= _size) throw RangeError('Index out of bounds');\n    return _arr[index];\n  }\n\n  /* Add elements at the end */\n  void set(int index, int _num) {\n    if (index >= _size) throw RangeError('Index out of bounds');\n    _arr[index] = _num;\n  }\n\n  /* Direct traversal of list elements */\n  void add(int _num) {\n    // When the number of elements exceeds capacity, trigger the extension mechanism\n    if (_size == _capacity) extendCapacity();\n    _arr[_size] = _num;\n    // Update the number of elements\n    _size++;\n  }\n\n  /* Sort list */\n  void insert(int index, int _num) {\n    if (index >= _size) throw RangeError('Index out of bounds');\n    // When the number of elements exceeds capacity, trigger the extension mechanism\n    if (_size == _capacity) extendCapacity();\n    // Move all elements after index index forward by one position\n    for (var j = _size - 1; j >= index; j--) {\n      _arr[j + 1] = _arr[j];\n    }\n    _arr[index] = _num;\n    // Update the number of elements\n    _size++;\n  }\n\n  /* Remove element */\n  int remove(int index) {\n    if (index >= _size) throw RangeError('Index out of bounds');\n    int _num = _arr[index];\n    // Move all elements after index forward by one position\n    for (var j = index; j < _size - 1; j++) {\n      _arr[j] = _arr[j + 1];\n    }\n    // Update the number of elements\n    _size--;\n    // Return the removed element\n    return _num;\n  }\n\n  /* Driver Code */\n  void extendCapacity() {\n    // Create new array with length _extendRatio times original array\n    final _newNums = List.filled(_capacity * _extendRatio, 0);\n    // Copy original array to new array\n    List.copyRange(_newNums, 0, _arr);\n    // Update _arr reference\n    _arr = _newNums;\n    // Add elements at the end\n    _capacity = _arr.length;\n  }\n\n  /* Convert list to array */\n  List<int> toArray() {\n    List<int> arr = [];\n    for (var i = 0; i < _size; i++) {\n      arr.add(get(i));\n    }\n    return arr;\n  }\n}\n
      my_list.rs
      /* List class */\n#[allow(dead_code)]\nstruct MyList {\n    arr: Vec<i32>,       // Array (stores list elements)\n    capacity: usize,     // List capacity\n    size: usize,         // List length (current number of elements)\n    extend_ratio: usize, // Multiple by which the list capacity is extended each time\n}\n\n#[allow(unused, unused_comparisons)]\nimpl MyList {\n    /* Constructor */\n    pub fn new(capacity: usize) -> Self {\n        let mut vec = vec![0; capacity];\n        Self {\n            arr: vec,\n            capacity,\n            size: 0,\n            extend_ratio: 2,\n        }\n    }\n\n    /* Get list length (current number of elements) */\n    pub fn size(&self) -> usize {\n        return self.size;\n    }\n\n    /* Get list capacity */\n    pub fn capacity(&self) -> usize {\n        return self.capacity;\n    }\n\n    /* Update element */\n    pub fn get(&self, index: usize) -> i32 {\n        // If the index is out of bounds, throw an exception, as below\n        if index >= self.size {\n            panic!(\"Index out of bounds\")\n        };\n        return self.arr[index];\n    }\n\n    /* Add elements at the end */\n    pub fn set(&mut self, index: usize, num: i32) {\n        if index >= self.size {\n            panic!(\"Index out of bounds\")\n        };\n        self.arr[index] = num;\n    }\n\n    /* Direct traversal of list elements */\n    pub fn add(&mut self, num: i32) {\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if self.size == self.capacity() {\n            self.extend_capacity();\n        }\n        self.arr[self.size] = num;\n        // Update the number of elements\n        self.size += 1;\n    }\n\n    /* Sort list */\n    pub fn insert(&mut self, index: usize, num: i32) {\n        if index >= self.size() {\n            panic!(\"Index out of bounds\")\n        };\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if self.size == self.capacity() {\n            self.extend_capacity();\n        }\n        // Move all elements after index index forward by one position\n        for j in (index..self.size).rev() {\n            self.arr[j + 1] = self.arr[j];\n        }\n        self.arr[index] = num;\n        // Update the number of elements\n        self.size += 1;\n    }\n\n    /* Remove element */\n    pub fn remove(&mut self, index: usize) -> i32 {\n        if index >= self.size() {\n            panic!(\"Index out of bounds\")\n        };\n        let num = self.arr[index];\n        // Create a new array with length _extend_ratio times the original array, and copy the original array to the new array\n        for j in index..self.size - 1 {\n            self.arr[j] = self.arr[j + 1];\n        }\n        // Update the number of elements\n        self.size -= 1;\n        // Return the removed element\n        return num;\n    }\n\n    /* Driver Code */\n    pub fn extend_capacity(&mut self) {\n        // Create new array with length extend_ratio times original, copy original array to new array\n        let new_capacity = self.capacity * self.extend_ratio;\n        self.arr.resize(new_capacity, 0);\n        // Add elements at the end\n        self.capacity = new_capacity;\n    }\n\n    /* Convert list to array */\n    pub fn to_array(&self) -> Vec<i32> {\n        // Elements enqueue\n        let mut arr = Vec::new();\n        for i in 0..self.size {\n            arr.push(self.get(i));\n        }\n        arr\n    }\n}\n
      my_list.c
      /* List class */\ntypedef struct {\n    int *arr;        // Array (stores list elements)\n    int capacity;    // List capacity\n    int size;        // List size\n    int extendRatio; // List expansion multiplier\n} MyList;\n\n/* Constructor */\nMyList *newMyList() {\n    MyList *nums = malloc(sizeof(MyList));\n    nums->capacity = 10;\n    nums->arr = malloc(sizeof(int) * nums->capacity);\n    nums->size = 0;\n    nums->extendRatio = 2;\n    return nums;\n}\n\n/* Destructor */\nvoid delMyList(MyList *nums) {\n    free(nums->arr);\n    free(nums);\n}\n\n/* Get list length */\nint size(MyList *nums) {\n    return nums->size;\n}\n\n/* Get list capacity */\nint capacity(MyList *nums) {\n    return nums->capacity;\n}\n\n/* Update element */\nint get(MyList *nums, int index) {\n    assert(index >= 0 && index < nums->size);\n    return nums->arr[index];\n}\n\n/* Add elements at the end */\nvoid set(MyList *nums, int index, int num) {\n    assert(index >= 0 && index < nums->size);\n    nums->arr[index] = num;\n}\n\n/* Direct traversal of list elements */\nvoid add(MyList *nums, int num) {\n    if (size(nums) == capacity(nums)) {\n        extendCapacity(nums); // Expand capacity\n    }\n    nums->arr[size(nums)] = num;\n    nums->size++;\n}\n\n/* Sort list */\nvoid insert(MyList *nums, int index, int num) {\n    assert(index >= 0 && index < size(nums));\n    // When the number of elements exceeds capacity, trigger the extension mechanism\n    if (size(nums) == capacity(nums)) {\n        extendCapacity(nums); // Expand capacity\n    }\n    for (int i = size(nums); i > index; --i) {\n        nums->arr[i] = nums->arr[i - 1];\n    }\n    nums->arr[index] = num;\n    nums->size++;\n}\n\n/* Remove element */\n// Note: stdio.h occupies the remove keyword\nint removeItem(MyList *nums, int index) {\n    assert(index >= 0 && index < size(nums));\n    int num = nums->arr[index];\n    for (int i = index; i < size(nums) - 1; i++) {\n        nums->arr[i] = nums->arr[i + 1];\n    }\n    nums->size--;\n    return num;\n}\n\n/* Driver Code */\nvoid extendCapacity(MyList *nums) {\n    // Allocate space first\n    int newCapacity = capacity(nums) * nums->extendRatio;\n    int *extend = (int *)malloc(sizeof(int) * newCapacity);\n    int *temp = nums->arr;\n\n    // Copy old data to new data\n    for (int i = 0; i < size(nums); i++)\n        extend[i] = nums->arr[i];\n\n    // Free old data\n    free(temp);\n\n    // Update new data\n    nums->arr = extend;\n    nums->capacity = newCapacity;\n}\n\n/* Convert list to Array for printing */\nint *toArray(MyList *nums) {\n    return nums->arr;\n}\n
      my_list.kt
      /* List class */\nclass MyList {\n    private var arr: IntArray = intArrayOf() // Array (stores list elements)\n    private var capacity: Int = 10 // List capacity\n    private var size: Int = 0 // List length (current number of elements)\n    private var extendRatio: Int = 2 // Multiple by which the list capacity is extended each time\n\n    /* Constructor */\n    init {\n        arr = IntArray(capacity)\n    }\n\n    /* Get list length (current number of elements) */\n    fun size(): Int {\n        return size\n    }\n\n    /* Get list capacity */\n    fun capacity(): Int {\n        return capacity\n    }\n\n    /* Update element */\n    fun get(index: Int): Int {\n        // If the index is out of bounds, throw an exception, as below\n        if (index < 0 || index >= size)\n            throw IndexOutOfBoundsException(\"Index out of bounds\")\n        return arr[index]\n    }\n\n    /* Add elements at the end */\n    fun set(index: Int, num: Int) {\n        if (index < 0 || index >= size)\n            throw IndexOutOfBoundsException(\"Index out of bounds\")\n        arr[index] = num\n    }\n\n    /* Direct traversal of list elements */\n    fun add(num: Int) {\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if (size == capacity())\n            extendCapacity()\n        arr[size] = num\n        // Update the number of elements\n        size++\n    }\n\n    /* Sort list */\n    fun insert(index: Int, num: Int) {\n        if (index < 0 || index >= size)\n            throw IndexOutOfBoundsException(\"Index out of bounds\")\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if (size == capacity())\n            extendCapacity()\n        // Move all elements after index index forward by one position\n        for (j in size - 1 downTo index)\n            arr[j + 1] = arr[j]\n        arr[index] = num\n        // Update the number of elements\n        size++\n    }\n\n    /* Remove element */\n    fun remove(index: Int): Int {\n        if (index < 0 || index >= size)\n            throw IndexOutOfBoundsException(\"Index out of bounds\")\n        val num = arr[index]\n        // Move all elements after index forward by one position\n        for (j in index..<size - 1)\n            arr[j] = arr[j + 1]\n        // Update the number of elements\n        size--\n        // Return the removed element\n        return num\n    }\n\n    /* Driver Code */\n    fun extendCapacity() {\n        // Create a new array with length extendRatio times the original array and copy the original array to the new array\n        arr = arr.copyOf(capacity() * extendRatio)\n        // Add elements at the end\n        capacity = arr.size\n    }\n\n    /* Convert list to array */\n    fun toArray(): IntArray {\n        val size = size()\n        // Elements enqueue\n        val arr = IntArray(size)\n        for (i in 0..<size) {\n            arr[i] = get(i)\n        }\n        return arr\n    }\n}\n
      my_list.rb
      ### List class ###\nclass MyList\n  attr_reader :size       # Get list length (current number of elements)\n  attr_reader :capacity   # Get list capacity\n\n  ### Constructor ###\n  def initialize\n    @capacity = 10\n    @size = 0\n    @extend_ratio = 2\n    @arr = Array.new(capacity)\n  end\n\n  ### Access element ###\n  def get(index)\n    # If the index is out of bounds, throw an exception, as below\n    raise IndexError, \"Index out of bounds\" if index < 0 || index >= size\n    @arr[index]\n  end\n\n  ### Access element ###\n  def set(index, num)\n    raise IndexError, \"Index out of bounds\" if index < 0 || index >= size\n    @arr[index] = num\n  end\n\n  ### Add element at end ###\n  def add(num)\n    # When the number of elements exceeds capacity, trigger the extension mechanism\n    extend_capacity if size == capacity\n    @arr[size] = num\n\n    # Update the number of elements\n    @size += 1\n  end\n\n  ### Insert element in middle ###\n  def insert(index, num)\n    raise IndexError, \"Index out of bounds\" if index < 0 || index >= size\n\n    # When the number of elements exceeds capacity, trigger the extension mechanism\n    extend_capacity if size == capacity\n\n    # Move all elements after index index forward by one position\n    for j in (size - 1).downto(index)\n      @arr[j + 1] = @arr[j]\n    end\n    @arr[index] = num\n\n    # Update the number of elements\n    @size += 1\n  end\n\n  ### Delete element ###\n  def remove(index)\n    raise IndexError, \"Index out of bounds\" if index < 0 || index >= size\n    num = @arr[index]\n\n    # Move all elements after index forward by one position\n    for j in index...size\n      @arr[j] = @arr[j + 1]\n    end\n\n    # Update the number of elements\n    @size -= 1\n\n    # Return the removed element\n    num\n  end\n\n  ### Expand list capacity ###\n  def extend_capacity\n    # Create new array with length extend_ratio times original, copy original array to new array\n    arr = @arr.dup + Array.new(capacity * (@extend_ratio - 1))\n    # Add elements at the end\n    @capacity = arr.length\n  end\n\n  ### Convert list to array ###\n  def to_array\n    sz = size\n    # Elements enqueue\n    arr = Array.new(sz)\n    for i in 0...sz\n      arr[i] = get(i)\n    end\n    arr\n  end\nend\n
      "},{"location":"chapter_array_and_linkedlist/ram_and_cache/","title":"4.4 \u00a0 Random-Access Memory and Cache *","text":"

      In the first two sections of this chapter, we explored arrays and linked lists, two fundamental and important data structures that represent \"contiguous storage\" and \"distributed storage\" as two physical structures, respectively.

      In fact, physical structure largely determines the efficiency with which programs utilize memory and cache, which in turn affects the overall performance of algorithmic programs.

      "},{"location":"chapter_array_and_linkedlist/ram_and_cache/#441-computer-storage-devices","title":"4.4.1 \u00a0 Computer Storage Devices","text":"

      Computers include three types of storage devices: hard disk, random-access memory (RAM), and cache memory. The following table shows their different roles and performance characteristics in a computer system.

      Table 4-2 \u00a0 Computer Storage Devices

      Hard Disk RAM Cache Purpose Long-term storage of data, including operating systems, programs, and files Temporary storage of currently running programs and data being processed Storage of frequently accessed data and instructions to reduce CPU's accesses to memory Volatility Data is not lost after power-off Data is lost after power-off Data is lost after power-off Capacity Large, on the order of terabytes (TB) Small, on the order of gigabytes (GB) Very small, on the order of megabytes (MB) Speed Slow, hundreds to thousands of MB/s Fast, tens of GB/s Very fast, tens to hundreds of GB/s Cost (USD/GB) Inexpensive, fractions of a dollar to a few dollars per GB Expensive, tens to hundreds of dollars per GB Very expensive, priced as part of the CPU package

      We can imagine the computer storage system as a pyramid structure as shown in the diagram below. Storage devices closer to the top of the pyramid are faster, have smaller capacity, and are more expensive. This multi-layered design is not by accident, but rather the result of careful consideration by computer scientists and engineers.

      • Hard disk cannot be easily replaced by RAM. First, data in memory is lost after power-off, making it unsuitable for long-term data storage. Second, memory is tens of times more expensive than hard disk, which makes it difficult to popularize in the consumer market.
      • Cache cannot simultaneously achieve large capacity and high speed. As the capacity of L1, L2, and L3 caches increases, their physical size becomes larger, and the physical distance between them and the CPU core increases, resulting in longer data transmission time and higher element access latency. With current technology, the multi-layered cache structure represents the best balance point between capacity, speed, and cost.

      Figure 4-9 \u00a0 Computer Storage System

      Tip

      The storage hierarchy of computers embodies a delicate balance among speed, capacity, and cost. In fact, such trade-offs are common across all industrial fields, requiring us to find the optimal balance point between different advantages and constraints.

      In summary, hard disk is used for long-term storage of large amounts of data, RAM is used for temporary storage of data being processed during program execution, and cache is used for storage of frequently accessed data and instructions, to improve program execution efficiency. The three work together to ensure efficient operation of the computer system.

      As shown in the diagram below, during program execution, data is read from the hard disk into RAM for CPU computation. Cache can be viewed as part of the CPU, it intelligently loads data from RAM, providing the CPU with high-speed data reading, thereby significantly improving program execution efficiency and reducing reliance on slower RAM.

      Figure 4-10 \u00a0 Data Flow Among Hard Disk, RAM, and Cache

      "},{"location":"chapter_array_and_linkedlist/ram_and_cache/#442-memory-efficiency-of-data-structures","title":"4.4.2 \u00a0 Memory Efficiency of Data Structures","text":"

      In terms of memory space utilization, arrays and linked lists each have advantages and limitations.

      On one hand, memory is limited, and the same memory cannot be shared by multiple programs, so we hope data structures can utilize space as efficiently as possible. Array elements are tightly packed and do not require additional space to store references (pointers) between linked list nodes, thus having higher space efficiency. However, arrays need to allocate sufficient contiguous memory space at once, which may lead to memory waste, and array expansion requires additional time and space costs. In comparison, linked lists perform dynamic memory allocation and deallocation on a \"node\" basis, providing greater flexibility.

      On the other hand, during program execution, as memory is repeatedly allocated and freed, the degree of fragmentation of free memory becomes increasingly severe, leading to reduced memory utilization efficiency. Arrays, due to their contiguous storage approach, are relatively less prone to memory fragmentation. Conversely, linked list elements are distributed in storage, and frequent insertion and deletion operations are more likely to cause memory fragmentation.

      "},{"location":"chapter_array_and_linkedlist/ram_and_cache/#443-cache-efficiency-of-data-structures","title":"4.4.3 \u00a0 Cache Efficiency of Data Structures","text":"

      Although cache has much smaller space capacity than memory, it is much faster than memory and plays a crucial role in program execution speed. Since cache capacity is limited and can only store a small portion of frequently accessed data, when the CPU attempts to access data that is not in the cache, a cache miss occurs, and the CPU must load the required data from the slower memory.

      Clearly, the fewer \"cache misses,\" the higher the efficiency of CPU data reads and writes, and the better the program performance. We call the proportion of data that the CPU successfully obtains from the cache the cache hit rate, a metric typically used to measure cache efficiency.

      To achieve the highest efficiency possible, cache employs the following data loading mechanisms.

      • Cache lines: The cache does not store and load data on a byte-by-byte basis, but rather as cache lines. Compared to byte-by-byte transmission, cache line transmission is more efficient.
      • Prefetching mechanism: The processor attempts to predict data access patterns (e.g., sequential access, fixed-stride jumping access, etc.) and loads data into the cache according to specific patterns, thereby improving hit rate.
      • Spatial locality: If a piece of data is accessed, nearby data may also be accessed in the near future. Therefore, when the cache loads a particular piece of data, it also loads nearby data to improve hit rate.
      • Temporal locality: If a piece of data is accessed, it is likely to be accessed again in the near future. Cache leverages this principle by retaining recently accessed data to improve hit rate.

      In fact, arrays and linked lists have different efficiencies in utilizing cache, manifested in the following aspects.

      • Space occupied: Linked list elements occupy more space than array elements, resulting in fewer effective data in the cache.
      • Cache lines: Linked list data are scattered throughout memory, while cache loads \"by lines,\" so the proportion of invalid data loaded is higher.
      • Prefetching mechanism: Arrays have more \"predictable\" data access patterns than linked lists, making it easier for the system to guess which data will be loaded next.
      • Spatial locality: Arrays are stored in centralized memory space, so data near loaded data is more likely to be accessed soon.

      Overall, arrays have higher cache hit rates, thus they usually outperform linked lists in operation efficiency. This makes data structures implemented based on arrays more popular when solving algorithmic problems.

      It is important to note that high cache efficiency does not mean arrays are superior to linked lists in all cases. In practical applications, which data structure to choose should be determined based on specific requirements. For example, both arrays and linked lists can implement the \"stack\" data structure (which will be discussed in detail in the next chapter), but they are suitable for different scenarios.

      • When solving algorithm problems, we tend to prefer stack implementations based on arrays, because they provide higher operation efficiency and the ability of random access, at the cost of needing to pre-allocate a certain amount of memory space for the array.
      • If the data volume is very large, the dynamic nature is high, and the expected size of the stack is difficult to estimate, then a stack implementation based on linked lists is more suitable. Linked lists can distribute large amounts of data across different parts of memory and avoid the additional overhead produced by array expansion.
      "},{"location":"chapter_array_and_linkedlist/summary/","title":"4.5 \u00a0 Summary","text":""},{"location":"chapter_array_and_linkedlist/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • Arrays and linked lists are two fundamental data structures, representing two different ways data can be stored in computer memory: contiguous memory storage and scattered memory storage. The characteristics of the two complement each other.
      • Arrays support random access and use less memory; however, inserting and deleting elements is inefficient, and the length is immutable after initialization.
      • Linked lists achieve efficient insertion and deletion of nodes by modifying references (pointers), and can flexibly adjust length; however, node access is inefficient and memory consumption is higher. Common linked list types include singly linked lists, circular linked lists, and doubly linked lists.
      • A list is an ordered collection of elements that supports insertion, deletion, search, and modification, typically implemented based on dynamic arrays. It retains the advantages of arrays while allowing flexible adjustment of length.
      • The emergence of lists has greatly improved the practicality of arrays, but may result in some wasted memory space.
      • During program execution, data is primarily stored in memory. Arrays provide higher memory space efficiency, while linked lists offer greater flexibility in memory usage.
      • Caches provide fast data access to the CPU through mechanisms such as cache lines, prefetching, and spatial and temporal locality, significantly improving program execution efficiency.
      • Because arrays have higher cache hit rates, they are generally more efficient than linked lists. When choosing a data structure, appropriate selection should be made based on specific requirements and scenarios.
      "},{"location":"chapter_array_and_linkedlist/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

      Q: Does storing an array on the stack versus on the heap affect time efficiency and space efficiency?

      Arrays stored on the stack and on the heap are both stored in contiguous memory space, so data operation efficiency is basically the same. However, the stack and heap have their own characteristics, leading to the following differences.

      1. Allocation and deallocation efficiency: The stack is a relatively small piece of memory, with allocation automatically handled by the compiler; the heap is relatively larger and can be dynamically allocated in code, more prone to fragmentation. Therefore, allocation and deallocation operations on the heap are usually slower than on the stack.
      2. Size limitations: Stack memory is relatively small, and the heap size is generally limited by available memory. Therefore, the heap is more suitable for storing large arrays.
      3. Flexibility: The size of an array on the stack must be determined at compile time, while the size of an array on the heap can be determined dynamically at runtime.

      Q: Why do arrays require elements of the same type, while linked lists do not emphasize this requirement?

      Linked lists are composed of nodes, with nodes connected through references (pointers), and each node can store different types of data, such as int, double, string, object, etc.

      In contrast, array elements must be of the same type, so that the corresponding element position can be obtained by calculating the offset. For example, if an array contains both int and long types, with individual elements occupying 4 bytes and 8 bytes respectively, then the following formula cannot be used to calculate the offset, because the array contains two different \"element lengths\".

      # Element Memory Address = Array Memory Address (first Element Memory address) + Element Length * Element Index\n

      Q: After deleting node P, do we need to set P.next to None?

      It is not necessary to modify P.next. From the perspective of the linked list, traversing from the head node to the tail node will no longer encounter P. This means that node P has been removed from the linked list, and it doesn't matter where node P points to at this time\u2014it won't affect the linked list.

      From a data structures and algorithms perspective (problem-solving), not disconnecting the pointer doesn't matter as long as the program logic is correct. From the perspective of standard libraries, disconnecting is safer and the logic is clearer. If not disconnected, assuming the deleted node is not properly reclaimed, it may affect the memory reclamation of its successor nodes.

      Q: In a linked list, the time complexity of insertion and deletion operations is \\(O(1)\\). However, both insertion and deletion require \\(O(n)\\) time to find the element; why isn't the time complexity \\(O(n)\\)?

      If the element is first found and then deleted, the time complexity is indeed \\(O(n)\\). However, the advantage of \\(O(1)\\) insertion and deletion in linked lists can be demonstrated in other applications. For example, a deque is well-suited for linked list implementation, where we maintain pointer variables always pointing to the head and tail nodes, with each insertion and deletion operation being \\(O(1)\\).

      Q: In the diagram \"Linked List Definition and Storage Methods\", does the light blue pointer node occupy a single memory address, or does it share equally with the node value?

      This diagram is a qualitative representation; a quantitative representation requires analysis based on the specific situation.

      • Different types of node values occupy different amounts of space, such as int, long, double, and instance objects, etc.
      • The amount of memory space occupied by pointer variables depends on the operating system and compilation environment used, usually 8 bytes or 4 bytes.

      Q: Is appending an element at the end of a list always \\(O(1)\\)?

      If appending an element exceeds the list length, the list must first be expanded before adding. The system allocates a new block of memory and moves all elements from the original list to it, in which case the time complexity becomes \\(O(n)\\).

      Q: \"The emergence of lists has greatly improved the practicality of arrays, but may result in some wasted memory space\"\u2014does this space waste refer to the memory occupied by additional variables such as capacity, length, and expansion factor?

      This space waste mainly has two aspects: on one hand, lists typically set an initial length, which we may not need to fully utilize; on the other hand, to prevent frequent expansion, expansion generally multiplies by a coefficient, such as \\(\\times 1.5\\). As a result, there will be many empty positions that we typically cannot completely fill.

      Q: In Python, after initializing n = [1, 2, 3], the addresses of these 3 elements are contiguous, but initializing m = [2, 1, 3] reveals that each element's id is not continuous; rather, they are the same as those in n. Since the addresses of these elements are not contiguous, is m still an array?

      If we replace list elements with linked list nodes n = [n1, n2, n3, n4, n5], usually these 5 node objects are also scattered throughout memory. However, given a list index, we can still obtain the node memory address in \\(O(1)\\) time, thereby accessing the corresponding node. This is because the array stores references to nodes, not the nodes themselves.

      Unlike many languages, numbers in Python are wrapped as objects, and lists store not the numbers themselves, but references to the numbers. Therefore, we find that the same numbers in two arrays have the same id, and the memory addresses of these numbers need not be contiguous.

      Q: C++ STL has std::list which has already implemented a doubly linked list, but it seems that some algorithm books don't use it directly. Is there a limitation?

      On one hand, we often prefer to use arrays for implementing algorithms and only use linked lists when necessary, mainly for two reasons.

      • Space overhead: Since each element requires two additional pointers (one for the previous element and one for the next element), std::list typically consumes more space than std::vector.
      • Cache unfriendliness: Since data is not stored contiguously, std::list has lower cache utilization. In general, std::vector has better performance.

      On the other hand, cases where linked lists are necessary mainly involve binary trees and graphs. Stacks and queues usually use the stack and queue provided by the programming language, rather than linked lists.

      Q: Does the operation res = [[0]] * n create a 2D list where each [0] is independent?

      No, they are not independent. In this 2D list, all the [0] are actually references to the same object. If we modify one element, we will find that all corresponding elements change accordingly.

      If we want each [0] in the 2D list to be independent, we can use res = [[0] for _ in range(n)] to achieve this. The principle of this approach is to initialize \\(n\\) independent [0] list objects.

      Q: Does the operation res = [0] * n create a list where each integer 0 is independent?

      In this list, all integer 0s are references to the same object. This is because Python uses a caching mechanism for small integers (typically -5 to 256) to maximize object reuse and improve performance.

      Although they point to the same object, we can still independently modify each element in the list. This is because Python integers are \"immutable objects\". When we modify an element, we are actually switching to a reference of another object, rather than changing the original object itself.

      However, when list elements are \"mutable objects\" (such as lists, dictionaries, or class instances), modifying an element directly changes the object itself, and all elements referencing that object will have the same change.

      "},{"location":"chapter_backtracking/","title":"Chapter 13. \u00a0 Backtracking","text":"

      Abstract

      We are like explorers in a maze, and may encounter difficulties on the path forward.

      The power of backtracking allows us to start over, keep trying, and eventually find the exit leading to light.

      "},{"location":"chapter_backtracking/#chapter-contents","title":"Chapter contents","text":"
      • 13.1 \u00a0 Backtracking Algorithm
      • 13.2 \u00a0 Permutations Problem
      • 13.3 \u00a0 Subset-Sum Problem
      • 13.4 \u00a0 N-Queens Problem
      • 13.5 \u00a0 Summary
      "},{"location":"chapter_backtracking/backtracking_algorithm/","title":"13.1 \u00a0 Backtracking Algorithm","text":"

      The backtracking algorithm is a method for solving problems through exhaustive search. Its core idea is to start from an initial state and exhaustively search all possible solutions. When a correct solution is found, it is recorded. This process continues until a solution is found or all possible choices have been tried without finding a solution.

      The backtracking algorithm typically employs \"depth-first search\" to traverse the solution space. In the \"Binary Tree\" chapter, we mentioned that preorder, inorder, and postorder traversals all belong to depth-first search. Next, we will construct a backtracking problem using preorder traversal to progressively understand how the backtracking algorithm works.

      Example 1

      Given a binary tree, search and record all nodes with value \\(7\\), and return a list of these nodes.

      For this problem, we perform a preorder traversal of the tree and check whether the current node's value is \\(7\\). If it is, we add the node to the result list res. The relevant implementation is shown in the following figure and code:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_i_compact.py
      def pre_order(root: TreeNode):\n    \"\"\"Preorder traversal: Example 1\"\"\"\n    if root is None:\n        return\n    if root.val == 7:\n        # Record solution\n        res.append(root)\n    pre_order(root.left)\n    pre_order(root.right)\n
      preorder_traversal_i_compact.cpp
      /* Preorder traversal: Example 1 */\nvoid preOrder(TreeNode *root) {\n    if (root == nullptr) {\n        return;\n    }\n    if (root->val == 7) {\n        // Record solution\n        res.push_back(root);\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n}\n
      preorder_traversal_i_compact.java
      /* Preorder traversal: Example 1 */\nvoid preOrder(TreeNode root) {\n    if (root == null) {\n        return;\n    }\n    if (root.val == 7) {\n        // Record solution\n        res.add(root);\n    }\n    preOrder(root.left);\n    preOrder(root.right);\n}\n
      preorder_traversal_i_compact.cs
      /* Preorder traversal: Example 1 */\nvoid PreOrder(TreeNode? root) {\n    if (root == null) {\n        return;\n    }\n    if (root.val == 7) {\n        // Record solution\n        res.Add(root);\n    }\n    PreOrder(root.left);\n    PreOrder(root.right);\n}\n
      preorder_traversal_i_compact.go
      /* Preorder traversal: Example 1 */\nfunc preOrderI(root *TreeNode, res *[]*TreeNode) {\n    if root == nil {\n        return\n    }\n    if (root.Val).(int) == 7 {\n        // Record solution\n        *res = append(*res, root)\n    }\n    preOrderI(root.Left, res)\n    preOrderI(root.Right, res)\n}\n
      preorder_traversal_i_compact.swift
      /* Preorder traversal: Example 1 */\nfunc preOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    if root.val == 7 {\n        // Record solution\n        res.append(root)\n    }\n    preOrder(root: root.left)\n    preOrder(root: root.right)\n}\n
      preorder_traversal_i_compact.js
      /* Preorder traversal: Example 1 */\nfunction preOrder(root, res) {\n    if (root === null) {\n        return;\n    }\n    if (root.val === 7) {\n        // Record solution\n        res.push(root);\n    }\n    preOrder(root.left, res);\n    preOrder(root.right, res);\n}\n
      preorder_traversal_i_compact.ts
      /* Preorder traversal: Example 1 */\nfunction preOrder(root: TreeNode | null, res: TreeNode[]): void {\n    if (root === null) {\n        return;\n    }\n    if (root.val === 7) {\n        // Record solution\n        res.push(root);\n    }\n    preOrder(root.left, res);\n    preOrder(root.right, res);\n}\n
      preorder_traversal_i_compact.dart
      /* Preorder traversal: Example 1 */\nvoid preOrder(TreeNode? root, List<TreeNode> res) {\n  if (root == null) {\n    return;\n  }\n  if (root.val == 7) {\n    // Record solution\n    res.add(root);\n  }\n  preOrder(root.left, res);\n  preOrder(root.right, res);\n}\n
      preorder_traversal_i_compact.rs
      /* Preorder traversal: Example 1 */\nfn pre_order(res: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<&Rc<RefCell<TreeNode>>>) {\n    if root.is_none() {\n        return;\n    }\n    if let Some(node) = root {\n        if node.borrow().val == 7 {\n            // Record solution\n            res.push(node.clone());\n        }\n        pre_order(res, node.borrow().left.as_ref());\n        pre_order(res, node.borrow().right.as_ref());\n    }\n}\n
      preorder_traversal_i_compact.c
      /* Preorder traversal: Example 1 */\nvoid preOrder(TreeNode *root) {\n    if (root == NULL) {\n        return;\n    }\n    if (root->val == 7) {\n        // Record solution\n        res[resSize++] = root;\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n}\n
      preorder_traversal_i_compact.kt
      /* Preorder traversal: Example 1 */\nfun preOrder(root: TreeNode?) {\n    if (root == null) {\n        return\n    }\n    if (root._val == 7) {\n        // Record solution\n        res!!.add(root)\n    }\n    preOrder(root.left)\n    preOrder(root.right)\n}\n
      preorder_traversal_i_compact.rb
      ### Pre-order traversal: example 1 ###\ndef pre_order(root)\n  return unless root\n\n  # Record solution\n  $res << root if root.val == 7\n\n  pre_order(root.left)\n  pre_order(root.right)\nend\n

      Figure 13-1 \u00a0 Search for nodes in preorder traversal

      "},{"location":"chapter_backtracking/backtracking_algorithm/#1311-attempt-and-backtrack","title":"13.1.1 \u00a0 Attempt and Backtrack","text":"

      The reason it is called a backtracking algorithm is that it employs \"attempt\" and \"backtrack\" strategies when searching the solution space. When the algorithm encounters a state where it cannot continue forward or cannot find a solution that satisfies the constraints, it will undo the previous choice, return to a previous state, and try other possible choices.

      For Example 1, visiting each node represents an \"attempt\", while skipping over a leaf node or a function return from the parent node represents a \"backtrack\".

      It is worth noting that backtracking is not limited to function returns alone. To illustrate this, let's extend Example 1 slightly.

      Example 2

      In a binary tree, search all nodes with value \\(7\\), and return the paths from the root node to these nodes.

      Based on the code from Example 1, we need to use a list path to record the visited node path. When we reach a node with value \\(7\\), we copy path and add it to the result list res. After traversal is complete, res contains all the solutions. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_ii_compact.py
      def pre_order(root: TreeNode):\n    \"\"\"Preorder traversal: Example 2\"\"\"\n    if root is None:\n        return\n    # Attempt\n    path.append(root)\n    if root.val == 7:\n        # Record solution\n        res.append(list(path))\n    pre_order(root.left)\n    pre_order(root.right)\n    # Backtrack\n    path.pop()\n
      preorder_traversal_ii_compact.cpp
      /* Preorder traversal: Example 2 */\nvoid preOrder(TreeNode *root) {\n    if (root == nullptr) {\n        return;\n    }\n    // Attempt\n    path.push_back(root);\n    if (root->val == 7) {\n        // Record solution\n        res.push_back(path);\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // Backtrack\n    path.pop_back();\n}\n
      preorder_traversal_ii_compact.java
      /* Preorder traversal: Example 2 */\nvoid preOrder(TreeNode root) {\n    if (root == null) {\n        return;\n    }\n    // Attempt\n    path.add(root);\n    if (root.val == 7) {\n        // Record solution\n        res.add(new ArrayList<>(path));\n    }\n    preOrder(root.left);\n    preOrder(root.right);\n    // Backtrack\n    path.remove(path.size() - 1);\n}\n
      preorder_traversal_ii_compact.cs
      /* Preorder traversal: Example 2 */\nvoid PreOrder(TreeNode? root) {\n    if (root == null) {\n        return;\n    }\n    // Attempt\n    path.Add(root);\n    if (root.val == 7) {\n        // Record solution\n        res.Add(new List<TreeNode>(path));\n    }\n    PreOrder(root.left);\n    PreOrder(root.right);\n    // Backtrack\n    path.RemoveAt(path.Count - 1);\n}\n
      preorder_traversal_ii_compact.go
      /* Preorder traversal: Example 2 */\nfunc preOrderII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {\n    if root == nil {\n        return\n    }\n    // Attempt\n    *path = append(*path, root)\n    if root.Val.(int) == 7 {\n        // Record solution\n        *res = append(*res, append([]*TreeNode{}, *path...))\n    }\n    preOrderII(root.Left, res, path)\n    preOrderII(root.Right, res, path)\n    // Backtrack\n    *path = (*path)[:len(*path)-1]\n}\n
      preorder_traversal_ii_compact.swift
      /* Preorder traversal: Example 2 */\nfunc preOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    // Attempt\n    path.append(root)\n    if root.val == 7 {\n        // Record solution\n        res.append(path)\n    }\n    preOrder(root: root.left)\n    preOrder(root: root.right)\n    // Backtrack\n    path.removeLast()\n}\n
      preorder_traversal_ii_compact.js
      /* Preorder traversal: Example 2 */\nfunction preOrder(root, path, res) {\n    if (root === null) {\n        return;\n    }\n    // Attempt\n    path.push(root);\n    if (root.val === 7) {\n        // Record solution\n        res.push([...path]);\n    }\n    preOrder(root.left, path, res);\n    preOrder(root.right, path, res);\n    // Backtrack\n    path.pop();\n}\n
      preorder_traversal_ii_compact.ts
      /* Preorder traversal: Example 2 */\nfunction preOrder(\n    root: TreeNode | null,\n    path: TreeNode[],\n    res: TreeNode[][]\n): void {\n    if (root === null) {\n        return;\n    }\n    // Attempt\n    path.push(root);\n    if (root.val === 7) {\n        // Record solution\n        res.push([...path]);\n    }\n    preOrder(root.left, path, res);\n    preOrder(root.right, path, res);\n    // Backtrack\n    path.pop();\n}\n
      preorder_traversal_ii_compact.dart
      /* Preorder traversal: Example 2 */\nvoid preOrder(\n  TreeNode? root,\n  List<TreeNode> path,\n  List<List<TreeNode>> res,\n) {\n  if (root == null) {\n    return;\n  }\n\n  // Attempt\n  path.add(root);\n  if (root.val == 7) {\n    // Record solution\n    res.add(List.from(path));\n  }\n  preOrder(root.left, path, res);\n  preOrder(root.right, path, res);\n  // Backtrack\n  path.removeLast();\n}\n
      preorder_traversal_ii_compact.rs
      /* Preorder traversal: Example 2 */\nfn pre_order(\n    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,\n    path: &mut Vec<Rc<RefCell<TreeNode>>>,\n    root: Option<&Rc<RefCell<TreeNode>>>,\n) {\n    if root.is_none() {\n        return;\n    }\n    if let Some(node) = root {\n        // Attempt\n        path.push(node.clone());\n        if node.borrow().val == 7 {\n            // Record solution\n            res.push(path.clone());\n        }\n        pre_order(res, path, node.borrow().left.as_ref());\n        pre_order(res, path, node.borrow().right.as_ref());\n        // Backtrack\n        path.pop();\n    }\n}\n
      preorder_traversal_ii_compact.c
      /* Preorder traversal: Example 2 */\nvoid preOrder(TreeNode *root) {\n    if (root == NULL) {\n        return;\n    }\n    // Attempt\n    path[pathSize++] = root;\n    if (root->val == 7) {\n        // Record solution\n        for (int i = 0; i < pathSize; ++i) {\n            res[resSize][i] = path[i];\n        }\n        resSize++;\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // Backtrack\n    pathSize--;\n}\n
      preorder_traversal_ii_compact.kt
      /* Preorder traversal: Example 2 */\nfun preOrder(root: TreeNode?) {\n    if (root == null) {\n        return\n    }\n    // Attempt\n    path!!.add(root)\n    if (root._val == 7) {\n        // Record solution\n        res!!.add(path!!.toMutableList())\n    }\n    preOrder(root.left)\n    preOrder(root.right)\n    // Backtrack\n    path!!.removeAt(path!!.size - 1)\n}\n
      preorder_traversal_ii_compact.rb
      ### Pre-order traversal: example 2 ###\ndef pre_order(root)\n  return unless root\n\n  # Attempt\n  $path << root\n\n  # Record solution\n  $res << $path.dup if root.val == 7\n\n  pre_order(root.left)\n  pre_order(root.right)\n\n  # Backtrack\n  $path.pop\nend\n

      In each \"attempt\", we record the path by adding the current node to path; before \"backtracking\", we need to remove the node from path, to restore the state before this attempt.

      Observing the process shown in the following figure, we can understand attempt and backtrack as \"advance\" and \"undo\", two operations that are the reverse of each other.

      <1><2><3><4><5><6><7><8><9><10><11>

      Figure 13-2 \u00a0 Attempt and backtrack

      "},{"location":"chapter_backtracking/backtracking_algorithm/#1312-pruning","title":"13.1.2 \u00a0 Pruning","text":"

      Complex backtracking problems usually contain one or more constraints. Constraints can typically be used for \"pruning\".

      Example 3

      In a binary tree, search all nodes with value \\(7\\) and return the paths from the root node to these nodes, but require that the paths do not contain nodes with value \\(3\\).

      To satisfy the above constraints, we need to add pruning operations: during the search process, if we encounter a node with value \\(3\\), we return early and do not continue searching. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_iii_compact.py
      def pre_order(root: TreeNode):\n    \"\"\"Preorder traversal: Example 3\"\"\"\n    # Pruning\n    if root is None or root.val == 3:\n        return\n    # Attempt\n    path.append(root)\n    if root.val == 7:\n        # Record solution\n        res.append(list(path))\n    pre_order(root.left)\n    pre_order(root.right)\n    # Backtrack\n    path.pop()\n
      preorder_traversal_iii_compact.cpp
      /* Preorder traversal: Example 3 */\nvoid preOrder(TreeNode *root) {\n    // Pruning\n    if (root == nullptr || root->val == 3) {\n        return;\n    }\n    // Attempt\n    path.push_back(root);\n    if (root->val == 7) {\n        // Record solution\n        res.push_back(path);\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // Backtrack\n    path.pop_back();\n}\n
      preorder_traversal_iii_compact.java
      /* Preorder traversal: Example 3 */\nvoid preOrder(TreeNode root) {\n    // Pruning\n    if (root == null || root.val == 3) {\n        return;\n    }\n    // Attempt\n    path.add(root);\n    if (root.val == 7) {\n        // Record solution\n        res.add(new ArrayList<>(path));\n    }\n    preOrder(root.left);\n    preOrder(root.right);\n    // Backtrack\n    path.remove(path.size() - 1);\n}\n
      preorder_traversal_iii_compact.cs
      /* Preorder traversal: Example 3 */\nvoid PreOrder(TreeNode? root) {\n    // Pruning\n    if (root == null || root.val == 3) {\n        return;\n    }\n    // Attempt\n    path.Add(root);\n    if (root.val == 7) {\n        // Record solution\n        res.Add(new List<TreeNode>(path));\n    }\n    PreOrder(root.left);\n    PreOrder(root.right);\n    // Backtrack\n    path.RemoveAt(path.Count - 1);\n}\n
      preorder_traversal_iii_compact.go
      /* Preorder traversal: Example 3 */\nfunc preOrderIII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {\n    // Pruning\n    if root == nil || root.Val == 3 {\n        return\n    }\n    // Attempt\n    *path = append(*path, root)\n    if root.Val.(int) == 7 {\n        // Record solution\n        *res = append(*res, append([]*TreeNode{}, *path...))\n    }\n    preOrderIII(root.Left, res, path)\n    preOrderIII(root.Right, res, path)\n    // Backtrack\n    *path = (*path)[:len(*path)-1]\n}\n
      preorder_traversal_iii_compact.swift
      /* Preorder traversal: Example 3 */\nfunc preOrder(root: TreeNode?) {\n    // Pruning\n    guard let root = root, root.val != 3 else {\n        return\n    }\n    // Attempt\n    path.append(root)\n    if root.val == 7 {\n        // Record solution\n        res.append(path)\n    }\n    preOrder(root: root.left)\n    preOrder(root: root.right)\n    // Backtrack\n    path.removeLast()\n}\n
      preorder_traversal_iii_compact.js
      /* Preorder traversal: Example 3 */\nfunction preOrder(root, path, res) {\n    // Pruning\n    if (root === null || root.val === 3) {\n        return;\n    }\n    // Attempt\n    path.push(root);\n    if (root.val === 7) {\n        // Record solution\n        res.push([...path]);\n    }\n    preOrder(root.left, path, res);\n    preOrder(root.right, path, res);\n    // Backtrack\n    path.pop();\n}\n
      preorder_traversal_iii_compact.ts
      /* Preorder traversal: Example 3 */\nfunction preOrder(\n    root: TreeNode | null,\n    path: TreeNode[],\n    res: TreeNode[][]\n): void {\n    // Pruning\n    if (root === null || root.val === 3) {\n        return;\n    }\n    // Attempt\n    path.push(root);\n    if (root.val === 7) {\n        // Record solution\n        res.push([...path]);\n    }\n    preOrder(root.left, path, res);\n    preOrder(root.right, path, res);\n    // Backtrack\n    path.pop();\n}\n
      preorder_traversal_iii_compact.dart
      /* Preorder traversal: Example 3 */\nvoid preOrder(\n  TreeNode? root,\n  List<TreeNode> path,\n  List<List<TreeNode>> res,\n) {\n  if (root == null || root.val == 3) {\n    return;\n  }\n\n  // Attempt\n  path.add(root);\n  if (root.val == 7) {\n    // Record solution\n    res.add(List.from(path));\n  }\n  preOrder(root.left, path, res);\n  preOrder(root.right, path, res);\n  // Backtrack\n  path.removeLast();\n}\n
      preorder_traversal_iii_compact.rs
      /* Preorder traversal: Example 3 */\nfn pre_order(\n    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,\n    path: &mut Vec<Rc<RefCell<TreeNode>>>,\n    root: Option<&Rc<RefCell<TreeNode>>>,\n) {\n    // Pruning\n    if root.is_none() || root.as_ref().unwrap().borrow().val == 3 {\n        return;\n    }\n    if let Some(node) = root {\n        // Attempt\n        path.push(node.clone());\n        if node.borrow().val == 7 {\n            // Record solution\n            res.push(path.clone());\n        }\n        pre_order(res, path, node.borrow().left.as_ref());\n        pre_order(res, path, node.borrow().right.as_ref());\n        // Backtrack\n        path.pop();\n    }\n}\n
      preorder_traversal_iii_compact.c
      /* Preorder traversal: Example 3 */\nvoid preOrder(TreeNode *root) {\n    // Pruning\n    if (root == NULL || root->val == 3) {\n        return;\n    }\n    // Attempt\n    path[pathSize++] = root;\n    if (root->val == 7) {\n        // Record solution\n        for (int i = 0; i < pathSize; i++) {\n            res[resSize][i] = path[i];\n        }\n        resSize++;\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // Backtrack\n    pathSize--;\n}\n
      preorder_traversal_iii_compact.kt
      /* Preorder traversal: Example 3 */\nfun preOrder(root: TreeNode?) {\n    // Pruning\n    if (root == null || root._val == 3) {\n        return\n    }\n    // Attempt\n    path!!.add(root)\n    if (root._val == 7) {\n        // Record solution\n        res!!.add(path!!.toMutableList())\n    }\n    preOrder(root.left)\n    preOrder(root.right)\n    // Backtrack\n    path!!.removeAt(path!!.size - 1)\n}\n
      preorder_traversal_iii_compact.rb
      ### Pre-order traversal: example 3 ###\ndef pre_order(root)\n  # Pruning\n  return if !root || root.val == 3\n\n  # Attempt\n  $path.append(root)\n\n  # Record solution\n  $res << $path.dup if root.val == 7\n\n  pre_order(root.left)\n  pre_order(root.right)\n\n  # Backtrack\n  $path.pop\nend\n

      \"Pruning\" is a vivid term. As shown in the following figure, during the search process, we \"prune\" search branches that do not satisfy the constraints, avoiding many meaningless attempts and thus improving search efficiency.

      Figure 13-3 \u00a0 Pruning according to constraints

      "},{"location":"chapter_backtracking/backtracking_algorithm/#1313-framework-code","title":"13.1.3 \u00a0 Framework Code","text":"

      Next, we attempt to extract the main framework of backtracking's \"attempt, backtrack, and pruning\", to improve code generality.

      In the following framework code, state represents the current state of the problem, and choices represents the choices available in the current state:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      def backtrack(state: State, choices: list[choice], res: list[state]):\n    \"\"\"Backtracking algorithm framework\"\"\"\n    # Check if it is a solution\n    if is_solution(state):\n        # Record the solution\n        record_solution(state, res)\n        # Stop searching\n        return\n    # Traverse all choices\n    for choice in choices:\n        # Pruning: check if the choice is valid\n        if is_valid(state, choice):\n            # Attempt: make a choice and update the state\n            make_choice(state, choice)\n            backtrack(state, choices, res)\n            # Backtrack: undo the choice and restore to the previous state\n            undo_choice(state, choice)\n
      /* Backtracking algorithm framework */\nvoid backtrack(State *state, vector<Choice *> &choices, vector<State *> &res) {\n    // Check if it is a solution\n    if (isSolution(state)) {\n        // Record the solution\n        recordSolution(state, res);\n        // Stop searching\n        return;\n    }\n    // Traverse all choices\n    for (Choice choice : choices) {\n        // Pruning: check if the choice is valid\n        if (isValid(state, choice)) {\n            // Attempt: make a choice and update the state\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // Backtrack: undo the choice and restore to the previous state\n            undoChoice(state, choice);\n        }\n    }\n}\n
      /* Backtracking algorithm framework */\nvoid backtrack(State state, List<Choice> choices, List<State> res) {\n    // Check if it is a solution\n    if (isSolution(state)) {\n        // Record the solution\n        recordSolution(state, res);\n        // Stop searching\n        return;\n    }\n    // Traverse all choices\n    for (Choice choice : choices) {\n        // Pruning: check if the choice is valid\n        if (isValid(state, choice)) {\n            // Attempt: make a choice and update the state\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // Backtrack: undo the choice and restore to the previous state\n            undoChoice(state, choice);\n        }\n    }\n}\n
      /* Backtracking algorithm framework */\nvoid Backtrack(State state, List<Choice> choices, List<State> res) {\n    // Check if it is a solution\n    if (IsSolution(state)) {\n        // Record the solution\n        RecordSolution(state, res);\n        // Stop searching\n        return;\n    }\n    // Traverse all choices\n    foreach (Choice choice in choices) {\n        // Pruning: check if the choice is valid\n        if (IsValid(state, choice)) {\n            // Attempt: make a choice and update the state\n            MakeChoice(state, choice);\n            Backtrack(state, choices, res);\n            // Backtrack: undo the choice and restore to the previous state\n            UndoChoice(state, choice);\n        }\n    }\n}\n
      /* Backtracking algorithm framework */\nfunc backtrack(state *State, choices []Choice, res *[]State) {\n    // Check if it is a solution\n    if isSolution(state) {\n        // Record the solution\n        recordSolution(state, res)\n        // Stop searching\n        return\n    }\n    // Traverse all choices\n    for _, choice := range choices {\n        // Pruning: check if the choice is valid\n        if isValid(state, choice) {\n            // Attempt: make a choice and update the state\n            makeChoice(state, choice)\n            backtrack(state, choices, res)\n            // Backtrack: undo the choice and restore to the previous state\n            undoChoice(state, choice)\n        }\n    }\n}\n
      /* Backtracking algorithm framework */\nfunc backtrack(state: inout State, choices: [Choice], res: inout [State]) {\n    // Check if it is a solution\n    if isSolution(state: state) {\n        // Record the solution\n        recordSolution(state: state, res: &res)\n        // Stop searching\n        return\n    }\n    // Traverse all choices\n    for choice in choices {\n        // Pruning: check if the choice is valid\n        if isValid(state: state, choice: choice) {\n            // Attempt: make a choice and update the state\n            makeChoice(state: &state, choice: choice)\n            backtrack(state: &state, choices: choices, res: &res)\n            // Backtrack: undo the choice and restore to the previous state\n            undoChoice(state: &state, choice: choice)\n        }\n    }\n}\n
      /* Backtracking algorithm framework */\nfunction backtrack(state, choices, res) {\n    // Check if it is a solution\n    if (isSolution(state)) {\n        // Record the solution\n        recordSolution(state, res);\n        // Stop searching\n        return;\n    }\n    // Traverse all choices\n    for (let choice of choices) {\n        // Pruning: check if the choice is valid\n        if (isValid(state, choice)) {\n            // Attempt: make a choice and update the state\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // Backtrack: undo the choice and restore to the previous state\n            undoChoice(state, choice);\n        }\n    }\n}\n
      /* Backtracking algorithm framework */\nfunction backtrack(state: State, choices: Choice[], res: State[]): void {\n    // Check if it is a solution\n    if (isSolution(state)) {\n        // Record the solution\n        recordSolution(state, res);\n        // Stop searching\n        return;\n    }\n    // Traverse all choices\n    for (let choice of choices) {\n        // Pruning: check if the choice is valid\n        if (isValid(state, choice)) {\n            // Attempt: make a choice and update the state\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // Backtrack: undo the choice and restore to the previous state\n            undoChoice(state, choice);\n        }\n    }\n}\n
      /* Backtracking algorithm framework */\nvoid backtrack(State state, List<Choice>, List<State> res) {\n  // Check if it is a solution\n  if (isSolution(state)) {\n    // Record the solution\n    recordSolution(state, res);\n    // Stop searching\n    return;\n  }\n  // Traverse all choices\n  for (Choice choice in choices) {\n    // Pruning: check if the choice is valid\n    if (isValid(state, choice)) {\n      // Attempt: make a choice and update the state\n      makeChoice(state, choice);\n      backtrack(state, choices, res);\n      // Backtrack: undo the choice and restore to the previous state\n      undoChoice(state, choice);\n    }\n  }\n}\n
      /* Backtracking algorithm framework */\nfn backtrack(state: &mut State, choices: &Vec<Choice>, res: &mut Vec<State>) {\n    // Check if it is a solution\n    if is_solution(state) {\n        // Record the solution\n        record_solution(state, res);\n        // Stop searching\n        return;\n    }\n    // Traverse all choices\n    for choice in choices {\n        // Pruning: check if the choice is valid\n        if is_valid(state, choice) {\n            // Attempt: make a choice and update the state\n            make_choice(state, choice);\n            backtrack(state, choices, res);\n            // Backtrack: undo the choice and restore to the previous state\n            undo_choice(state, choice);\n        }\n    }\n}\n
      /* Backtracking algorithm framework */\nvoid backtrack(State *state, Choice *choices, int numChoices, State *res, int numRes) {\n    // Check if it is a solution\n    if (isSolution(state)) {\n        // Record the solution\n        recordSolution(state, res, numRes);\n        // Stop searching\n        return;\n    }\n    // Traverse all choices\n    for (int i = 0; i < numChoices; i++) {\n        // Pruning: check if the choice is valid\n        if (isValid(state, &choices[i])) {\n            // Attempt: make a choice and update the state\n            makeChoice(state, &choices[i]);\n            backtrack(state, choices, numChoices, res, numRes);\n            // Backtrack: undo the choice and restore to the previous state\n            undoChoice(state, &choices[i]);\n        }\n    }\n}\n
      /* Backtracking algorithm framework */\nfun backtrack(state: State?, choices: List<Choice?>, res: List<State?>?) {\n    // Check if it is a solution\n    if (isSolution(state)) {\n        // Record the solution\n        recordSolution(state, res)\n        // Stop searching\n        return\n    }\n    // Traverse all choices\n    for (choice in choices) {\n        // Pruning: check if the choice is valid\n        if (isValid(state, choice)) {\n            // Attempt: make a choice and update the state\n            makeChoice(state, choice)\n            backtrack(state, choices, res)\n            // Backtrack: undo the choice and restore to the previous state\n            undoChoice(state, choice)\n        }\n    }\n}\n
      ### Backtracking algorithm framework ###\ndef backtrack(state, choices, res)\n    # Check if it is a solution\n    if is_solution?(state)\n        # Record the solution\n        record_solution(state, res)\n        return\n    end\n\n    # Traverse all choices\n    for choice in choices\n        # Pruning: check if the choice is valid\n        if is_valid?(state, choice)\n            # Attempt: make a choice and update the state\n            make_choice(state, choice)\n            backtrack(state, choices, res)\n            # Backtrack: undo the choice and restore to the previous state\n            undo_choice(state, choice)\n        end\n    end\nend\n

      Next, we solve Example 3 based on the framework code. The state state is the node traversal path, the choices choices are the left and right child nodes of the current node, and the result res is a list of paths:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_iii_template.py
      def is_solution(state: list[TreeNode]) -> bool:\n    \"\"\"Check if the current state is a solution\"\"\"\n    return state and state[-1].val == 7\n\ndef record_solution(state: list[TreeNode], res: list[list[TreeNode]]):\n    \"\"\"Record solution\"\"\"\n    res.append(list(state))\n\ndef is_valid(state: list[TreeNode], choice: TreeNode) -> bool:\n    \"\"\"Check if the choice is valid under the current state\"\"\"\n    return choice is not None and choice.val != 3\n\ndef make_choice(state: list[TreeNode], choice: TreeNode):\n    \"\"\"Update state\"\"\"\n    state.append(choice)\n\ndef undo_choice(state: list[TreeNode], choice: TreeNode):\n    \"\"\"Restore state\"\"\"\n    state.pop()\n\ndef backtrack(\n    state: list[TreeNode], choices: list[TreeNode], res: list[list[TreeNode]]\n):\n    \"\"\"Backtracking algorithm: Example 3\"\"\"\n    # Check if it is a solution\n    if is_solution(state):\n        # Record solution\n        record_solution(state, res)\n    # Traverse all choices\n    for choice in choices:\n        # Pruning: check if the choice is valid\n        if is_valid(state, choice):\n            # Attempt: make choice, update state\n            make_choice(state, choice)\n            # Proceed to the next round of selection\n            backtrack(state, [choice.left, choice.right], res)\n            # Backtrack: undo choice, restore to previous state\n            undo_choice(state, choice)\n
      preorder_traversal_iii_template.cpp
      /* Check if the current state is a solution */\nbool isSolution(vector<TreeNode *> &state) {\n    return !state.empty() && state.back()->val == 7;\n}\n\n/* Record solution */\nvoid recordSolution(vector<TreeNode *> &state, vector<vector<TreeNode *>> &res) {\n    res.push_back(state);\n}\n\n/* Check if the choice is valid under the current state */\nbool isValid(vector<TreeNode *> &state, TreeNode *choice) {\n    return choice != nullptr && choice->val != 3;\n}\n\n/* Update state */\nvoid makeChoice(vector<TreeNode *> &state, TreeNode *choice) {\n    state.push_back(choice);\n}\n\n/* Restore state */\nvoid undoChoice(vector<TreeNode *> &state, TreeNode *choice) {\n    state.pop_back();\n}\n\n/* Backtracking algorithm: Example 3 */\nvoid backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<vector<TreeNode *>> &res) {\n    // Check if it is a solution\n    if (isSolution(state)) {\n        // Record solution\n        recordSolution(state, res);\n    }\n    // Traverse all choices\n    for (TreeNode *choice : choices) {\n        // Pruning: check if the choice is valid\n        if (isValid(state, choice)) {\n            // Attempt: make choice, update state\n            makeChoice(state, choice);\n            // Proceed to the next round of selection\n            vector<TreeNode *> nextChoices{choice->left, choice->right};\n            backtrack(state, nextChoices, res);\n            // Backtrack: undo choice, restore to previous state\n            undoChoice(state, choice);\n        }\n    }\n}\n
      preorder_traversal_iii_template.java
      /* Check if the current state is a solution */\nboolean isSolution(List<TreeNode> state) {\n    return !state.isEmpty() && state.get(state.size() - 1).val == 7;\n}\n\n/* Record solution */\nvoid recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n    res.add(new ArrayList<>(state));\n}\n\n/* Check if the choice is valid under the current state */\nboolean isValid(List<TreeNode> state, TreeNode choice) {\n    return choice != null && choice.val != 3;\n}\n\n/* Update state */\nvoid makeChoice(List<TreeNode> state, TreeNode choice) {\n    state.add(choice);\n}\n\n/* Restore state */\nvoid undoChoice(List<TreeNode> state, TreeNode choice) {\n    state.remove(state.size() - 1);\n}\n\n/* Backtracking algorithm: Example 3 */\nvoid backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {\n    // Check if it is a solution\n    if (isSolution(state)) {\n        // Record solution\n        recordSolution(state, res);\n    }\n    // Traverse all choices\n    for (TreeNode choice : choices) {\n        // Pruning: check if the choice is valid\n        if (isValid(state, choice)) {\n            // Attempt: make choice, update state\n            makeChoice(state, choice);\n            // Proceed to the next round of selection\n            backtrack(state, Arrays.asList(choice.left, choice.right), res);\n            // Backtrack: undo choice, restore to previous state\n            undoChoice(state, choice);\n        }\n    }\n}\n
      preorder_traversal_iii_template.cs
      /* Check if the current state is a solution */\nbool IsSolution(List<TreeNode> state) {\n    return state.Count != 0 && state[^1].val == 7;\n}\n\n/* Record solution */\nvoid RecordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n    res.Add(new List<TreeNode>(state));\n}\n\n/* Check if the choice is valid under the current state */\nbool IsValid(List<TreeNode> state, TreeNode choice) {\n    return choice != null && choice.val != 3;\n}\n\n/* Update state */\nvoid MakeChoice(List<TreeNode> state, TreeNode choice) {\n    state.Add(choice);\n}\n\n/* Restore state */\nvoid UndoChoice(List<TreeNode> state, TreeNode choice) {\n    state.RemoveAt(state.Count - 1);\n}\n\n/* Backtracking algorithm: Example 3 */\nvoid Backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {\n    // Check if it is a solution\n    if (IsSolution(state)) {\n        // Record solution\n        RecordSolution(state, res);\n    }\n    // Traverse all choices\n    foreach (TreeNode choice in choices) {\n        // Pruning: check if the choice is valid\n        if (IsValid(state, choice)) {\n            // Attempt: make choice, update state\n            MakeChoice(state, choice);\n            // Proceed to the next round of selection\n            Backtrack(state, [choice.left!, choice.right!], res);\n            // Backtrack: undo choice, restore to previous state\n            UndoChoice(state, choice);\n        }\n    }\n}\n
      preorder_traversal_iii_template.go
      /* Check if the current state is a solution */\nfunc isSolution(state *[]*TreeNode) bool {\n    return len(*state) != 0 && (*state)[len(*state)-1].Val == 7\n}\n\n/* Record solution */\nfunc recordSolution(state *[]*TreeNode, res *[][]*TreeNode) {\n    *res = append(*res, append([]*TreeNode{}, *state...))\n}\n\n/* Check if the choice is valid under the current state */\nfunc isValid(state *[]*TreeNode, choice *TreeNode) bool {\n    return choice != nil && choice.Val != 3\n}\n\n/* Update state */\nfunc makeChoice(state *[]*TreeNode, choice *TreeNode) {\n    *state = append(*state, choice)\n}\n\n/* Restore state */\nfunc undoChoice(state *[]*TreeNode, choice *TreeNode) {\n    *state = (*state)[:len(*state)-1]\n}\n\n/* Backtracking algorithm: Example 3 */\nfunc backtrackIII(state *[]*TreeNode, choices *[]*TreeNode, res *[][]*TreeNode) {\n    // Check if it is a solution\n    if isSolution(state) {\n        // Record solution\n        recordSolution(state, res)\n    }\n    // Traverse all choices\n    for _, choice := range *choices {\n        // Pruning: check if the choice is valid\n        if isValid(state, choice) {\n            // Attempt: make choice, update state\n            makeChoice(state, choice)\n            // Proceed to the next round of selection\n            temp := make([]*TreeNode, 0)\n            temp = append(temp, choice.Left, choice.Right)\n            backtrackIII(state, &temp, res)\n            // Backtrack: undo choice, restore to previous state\n            undoChoice(state, choice)\n        }\n    }\n}\n
      preorder_traversal_iii_template.swift
      /* Check if the current state is a solution */\nfunc isSolution(state: [TreeNode]) -> Bool {\n    !state.isEmpty && state.last!.val == 7\n}\n\n/* Record solution */\nfunc recordSolution(state: [TreeNode], res: inout [[TreeNode]]) {\n    res.append(state)\n}\n\n/* Check if the choice is valid under the current state */\nfunc isValid(state: [TreeNode], choice: TreeNode?) -> Bool {\n    choice != nil && choice!.val != 3\n}\n\n/* Update state */\nfunc makeChoice(state: inout [TreeNode], choice: TreeNode) {\n    state.append(choice)\n}\n\n/* Restore state */\nfunc undoChoice(state: inout [TreeNode], choice: TreeNode) {\n    state.removeLast()\n}\n\n/* Backtracking algorithm: Example 3 */\nfunc backtrack(state: inout [TreeNode], choices: [TreeNode], res: inout [[TreeNode]]) {\n    // Check if it is a solution\n    if isSolution(state: state) {\n        recordSolution(state: state, res: &res)\n    }\n    // Traverse all choices\n    for choice in choices {\n        // Pruning: check if the choice is valid\n        if isValid(state: state, choice: choice) {\n            // Attempt: make choice, update state\n            makeChoice(state: &state, choice: choice)\n            // Proceed to the next round of selection\n            backtrack(state: &state, choices: [choice.left, choice.right].compactMap { $0 }, res: &res)\n            // Backtrack: undo choice, restore to previous state\n            undoChoice(state: &state, choice: choice)\n        }\n    }\n}\n
      preorder_traversal_iii_template.js
      /* Check if the current state is a solution */\nfunction isSolution(state) {\n    return state && state[state.length - 1]?.val === 7;\n}\n\n/* Record solution */\nfunction recordSolution(state, res) {\n    res.push([...state]);\n}\n\n/* Check if the choice is valid under the current state */\nfunction isValid(state, choice) {\n    return choice !== null && choice.val !== 3;\n}\n\n/* Update state */\nfunction makeChoice(state, choice) {\n    state.push(choice);\n}\n\n/* Restore state */\nfunction undoChoice(state) {\n    state.pop();\n}\n\n/* Backtracking algorithm: Example 3 */\nfunction backtrack(state, choices, res) {\n    // Check if it is a solution\n    if (isSolution(state)) {\n        // Record solution\n        recordSolution(state, res);\n    }\n    // Traverse all choices\n    for (const choice of choices) {\n        // Pruning: check if the choice is valid\n        if (isValid(state, choice)) {\n            // Attempt: make choice, update state\n            makeChoice(state, choice);\n            // Proceed to the next round of selection\n            backtrack(state, [choice.left, choice.right], res);\n            // Backtrack: undo choice, restore to previous state\n            undoChoice(state);\n        }\n    }\n}\n
      preorder_traversal_iii_template.ts
      /* Check if the current state is a solution */\nfunction isSolution(state: TreeNode[]): boolean {\n    return state && state[state.length - 1]?.val === 7;\n}\n\n/* Record solution */\nfunction recordSolution(state: TreeNode[], res: TreeNode[][]): void {\n    res.push([...state]);\n}\n\n/* Check if the choice is valid under the current state */\nfunction isValid(state: TreeNode[], choice: TreeNode): boolean {\n    return choice !== null && choice.val !== 3;\n}\n\n/* Update state */\nfunction makeChoice(state: TreeNode[], choice: TreeNode): void {\n    state.push(choice);\n}\n\n/* Restore state */\nfunction undoChoice(state: TreeNode[]): void {\n    state.pop();\n}\n\n/* Backtracking algorithm: Example 3 */\nfunction backtrack(\n    state: TreeNode[],\n    choices: TreeNode[],\n    res: TreeNode[][]\n): void {\n    // Check if it is a solution\n    if (isSolution(state)) {\n        // Record solution\n        recordSolution(state, res);\n    }\n    // Traverse all choices\n    for (const choice of choices) {\n        // Pruning: check if the choice is valid\n        if (isValid(state, choice)) {\n            // Attempt: make choice, update state\n            makeChoice(state, choice);\n            // Proceed to the next round of selection\n            backtrack(state, [choice.left, choice.right], res);\n            // Backtrack: undo choice, restore to previous state\n            undoChoice(state);\n        }\n    }\n}\n
      preorder_traversal_iii_template.dart
      /* Check if the current state is a solution */\nbool isSolution(List<TreeNode> state) {\n  return state.isNotEmpty && state.last.val == 7;\n}\n\n/* Record solution */\nvoid recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n  res.add(List.from(state));\n}\n\n/* Check if the choice is valid under the current state */\nbool isValid(List<TreeNode> state, TreeNode? choice) {\n  return choice != null && choice.val != 3;\n}\n\n/* Update state */\nvoid makeChoice(List<TreeNode> state, TreeNode? choice) {\n  state.add(choice!);\n}\n\n/* Restore state */\nvoid undoChoice(List<TreeNode> state, TreeNode? choice) {\n  state.removeLast();\n}\n\n/* Backtracking algorithm: Example 3 */\nvoid backtrack(\n  List<TreeNode> state,\n  List<TreeNode?> choices,\n  List<List<TreeNode>> res,\n) {\n  // Check if it is a solution\n  if (isSolution(state)) {\n    // Record solution\n    recordSolution(state, res);\n  }\n  // Traverse all choices\n  for (TreeNode? choice in choices) {\n    // Pruning: check if the choice is valid\n    if (isValid(state, choice)) {\n      // Attempt: make choice, update state\n      makeChoice(state, choice);\n      // Proceed to the next round of selection\n      backtrack(state, [choice!.left, choice.right], res);\n      // Backtrack: undo choice, restore to previous state\n      undoChoice(state, choice);\n    }\n  }\n}\n
      preorder_traversal_iii_template.rs
      /* Check if the current state is a solution */\nfn is_solution(state: &mut Vec<Rc<RefCell<TreeNode>>>) -> bool {\n    return !state.is_empty() && state.last().unwrap().borrow().val == 7;\n}\n\n/* Record solution */\nfn record_solution(\n    state: &mut Vec<Rc<RefCell<TreeNode>>>,\n    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,\n) {\n    res.push(state.clone());\n}\n\n/* Check if the choice is valid under the current state */\nfn is_valid(_: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Option<&Rc<RefCell<TreeNode>>>) -> bool {\n    return choice.is_some() && choice.unwrap().borrow().val != 3;\n}\n\n/* Update state */\nfn make_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Rc<RefCell<TreeNode>>) {\n    state.push(choice);\n}\n\n/* Restore state */\nfn undo_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, _: Rc<RefCell<TreeNode>>) {\n    state.pop();\n}\n\n/* Backtracking algorithm: Example 3 */\nfn backtrack(\n    state: &mut Vec<Rc<RefCell<TreeNode>>>,\n    choices: &Vec<Option<&Rc<RefCell<TreeNode>>>>,\n    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,\n) {\n    // Check if it is a solution\n    if is_solution(state) {\n        // Record solution\n        record_solution(state, res);\n    }\n    // Traverse all choices\n    for &choice in choices.iter() {\n        // Pruning: check if the choice is valid\n        if is_valid(state, choice) {\n            // Attempt: make choice, update state\n            make_choice(state, choice.unwrap().clone());\n            // Proceed to the next round of selection\n            backtrack(\n                state,\n                &vec![\n                    choice.unwrap().borrow().left.as_ref(),\n                    choice.unwrap().borrow().right.as_ref(),\n                ],\n                res,\n            );\n            // Backtrack: undo choice, restore to previous state\n            undo_choice(state, choice.unwrap().clone());\n        }\n    }\n}\n
      preorder_traversal_iii_template.c
      /* Check if the current state is a solution */\nbool isSolution(void) {\n    return pathSize > 0 && path[pathSize - 1]->val == 7;\n}\n\n/* Record solution */\nvoid recordSolution(void) {\n    for (int i = 0; i < pathSize; i++) {\n        res[resSize][i] = path[i];\n    }\n    resSize++;\n}\n\n/* Check if the choice is valid under the current state */\nbool isValid(TreeNode *choice) {\n    return choice != NULL && choice->val != 3;\n}\n\n/* Update state */\nvoid makeChoice(TreeNode *choice) {\n    path[pathSize++] = choice;\n}\n\n/* Restore state */\nvoid undoChoice(void) {\n    pathSize--;\n}\n\n/* Backtracking algorithm: Example 3 */\nvoid backtrack(TreeNode *choices[2]) {\n    // Check if it is a solution\n    if (isSolution()) {\n        // Record solution\n        recordSolution();\n    }\n    // Traverse all choices\n    for (int i = 0; i < 2; i++) {\n        TreeNode *choice = choices[i];\n        // Pruning: check if the choice is valid\n        if (isValid(choice)) {\n            // Attempt: make choice, update state\n            makeChoice(choice);\n            // Proceed to the next round of selection\n            TreeNode *nextChoices[2] = {choice->left, choice->right};\n            backtrack(nextChoices);\n            // Backtrack: undo choice, restore to previous state\n            undoChoice();\n        }\n    }\n}\n
      preorder_traversal_iii_template.kt
      /* Check if the current state is a solution */\nfun isSolution(state: MutableList<TreeNode?>): Boolean {\n    return state.isNotEmpty() && state[state.size - 1]?._val == 7\n}\n\n/* Record solution */\nfun recordSolution(state: MutableList<TreeNode?>?, res: MutableList<MutableList<TreeNode?>?>) {\n    res.add(state!!.toMutableList())\n}\n\n/* Check if the choice is valid under the current state */\nfun isValid(state: MutableList<TreeNode?>?, choice: TreeNode?): Boolean {\n    return choice != null && choice._val != 3\n}\n\n/* Update state */\nfun makeChoice(state: MutableList<TreeNode?>, choice: TreeNode?) {\n    state.add(choice)\n}\n\n/* Restore state */\nfun undoChoice(state: MutableList<TreeNode?>, choice: TreeNode?) {\n    state.removeLast()\n}\n\n/* Backtracking algorithm: Example 3 */\nfun backtrack(\n    state: MutableList<TreeNode?>,\n    choices: MutableList<TreeNode?>,\n    res: MutableList<MutableList<TreeNode?>?>\n) {\n    // Check if it is a solution\n    if (isSolution(state)) {\n        // Record solution\n        recordSolution(state, res)\n    }\n    // Traverse all choices\n    for (choice in choices) {\n        // Pruning: check if the choice is valid\n        if (isValid(state, choice)) {\n            // Attempt: make choice, update state\n            makeChoice(state, choice)\n            // Proceed to the next round of selection\n            backtrack(state, mutableListOf(choice!!.left, choice.right), res)\n            // Backtrack: undo choice, restore to previous state\n            undoChoice(state, choice)\n        }\n    }\n}\n
      preorder_traversal_iii_template.rb
      ### Check if current state is solution ###\ndef is_solution?(state)\n  !state.empty? && state.last.val == 7\nend\n\n### Record solution ###\ndef record_solution(state, res)\n  res << state.dup\nend\n\n### Check if choice is valid in current state ###\ndef is_valid?(state, choice)\n  choice && choice.val != 3\nend\n\n### Update state ###\ndef make_choice(state, choice)\n  state << choice\nend\n\n### Restore state ###\ndef undo_choice(state, choice)\n  state.pop\nend\n\n### Backtracking: example 3 ###\ndef backtrack(state, choices, res)\n  # Check if it is a solution\n  record_solution(state, res) if is_solution?(state)\n\n  # Traverse all choices\n  for choice in choices\n    # Pruning: check if the choice is valid\n    if is_valid?(state, choice)\n      # Attempt: make choice, update state\n      make_choice(state, choice)\n      # Proceed to the next round of selection\n      backtrack(state, [choice.left, choice.right], res)\n      # Backtrack: undo choice, restore to previous state\n      undo_choice(state, choice)\n    end\n  end\nend\n

      As per the problem statement, we should continue searching after finding a node with value \\(7\\). Therefore, we need to remove the return statement after recording the solution. The following figure compares the search process with and without the return statement.

      Figure 13-4 \u00a0 Comparison of search process with and without return statement

      Compared to code based on preorder traversal, code based on the backtracking algorithm framework appears more verbose, but has better generality. In fact, many backtracking problems can be solved within this framework. We only need to define state and choices for the specific problem and implement each method in the framework.

      "},{"location":"chapter_backtracking/backtracking_algorithm/#1314-common-terminology","title":"13.1.4 \u00a0 Common Terminology","text":"

      To analyze algorithmic problems more clearly, we summarize the meanings of common terminology used in backtracking algorithms and provide corresponding examples from Example 3, as shown in the following table.

      Table 13-1 \u00a0 Common Backtracking Algorithm Terminology

      Term Definition Example 3 Solution (solution) A solution is an answer that satisfies the specific conditions of a problem; there may be one or more solutions All paths from root to nodes with value \\(7\\) that satisfy the constraint Constraint (constraint) A constraint is a condition in the problem that limits the feasibility of solutions, typically used for pruning Paths do not contain nodes with value \\(3\\) State (state) State represents the situation of a problem at a certain moment, including the choices already made The currently visited node path, i.e., the path list of nodes Attempt (attempt) An attempt is the process of exploring the solution space according to available choices, including making choices, updating state, and checking if it is a solution Recursively visit left (right) child nodes, add nodes to path, check if node value is \\(7\\) Backtrack (backtracking) Backtracking refers to undoing previous choices and returning to a previous state when encountering a state that does not satisfy constraints Stop searching when passing over leaf nodes, ending node visits, or encountering nodes with value \\(3\\); function returns Pruning (pruning) Pruning is a method of avoiding meaningless search paths according to problem characteristics and constraints, which can improve search efficiency When encountering a node with value \\(3\\), do not continue searching

      Tip

      The concepts of problem, solution, state, etc. are universal and are involved in divide-and-conquer, backtracking, dynamic programming, greedy and other algorithms.

      "},{"location":"chapter_backtracking/backtracking_algorithm/#1315-advantages-and-limitations","title":"13.1.5 \u00a0 Advantages and Limitations","text":"

      The backtracking algorithm is essentially a depth-first search algorithm that tries all possible solutions until it finds one that satisfies the conditions. The advantage of this approach is that it can find all possible solutions, and with reasonable pruning operations, it achieves high efficiency.

      However, when dealing with large-scale or complex problems, the running efficiency of the backtracking algorithm may be unacceptable.

      • Time: The backtracking algorithm usually needs to traverse all possibilities in the solution space, and the time complexity can reach exponential or factorial order.
      • Space: During recursive calls, the current state needs to be saved (such as paths, auxiliary variables used for pruning, etc.), and when the depth is large, the space requirement can become very large.

      Nevertheless, the backtracking algorithm is still the best solution for certain search problems and constraint satisfaction problems. For these problems, since we cannot predict which choices will generate valid solutions, we must traverse all possible choices. In this case, the key is how to optimize efficiency. There are two common efficiency optimization methods.

      • Pruning: Avoid searching paths that are guaranteed not to produce solutions, thereby saving time and space.
      • Heuristic search: Introduce certain strategies or estimation values during the search process to prioritize searching paths that are most likely to produce valid solutions.
      "},{"location":"chapter_backtracking/backtracking_algorithm/#1316-typical-backtracking-examples","title":"13.1.6 \u00a0 Typical Backtracking Examples","text":"

      The backtracking algorithm can be used to solve many search problems, constraint satisfaction problems, and combinatorial optimization problems.

      Search problems: The goal of these problems is to find solutions that satisfy specific conditions.

      • Permutation problem: Given a set, find all possible permutations and combinations.
      • Subset sum problem: Given a set and a target sum, find all subsets in the set whose elements sum to the target.
      • Tower of Hanoi: Given three pegs and a series of disks of different sizes, move all disks from one peg to another, moving only one disk at a time, and never placing a larger disk on a smaller disk.

      Constraint satisfaction problems: The goal of these problems is to find solutions that satisfy all constraints.

      • N-Queens: Place \\(n\\) queens on an \\(n \\times n\\) chessboard such that they do not attack each other.
      • Sudoku: Fill numbers \\(1\\) to \\(9\\) in a \\(9 \\times 9\\) grid such that each row, column, and \\(3 \\times 3\\) subgrid contains no repeated digits.
      • Graph coloring: Given an undirected graph, color each vertex with the minimum number of colors such that adjacent vertices have different colors.

      Combinatorial optimization problems: The goal of these problems is to find an optimal solution that satisfies certain conditions in a combinatorial space.

      • 0-1 Knapsack: Given a set of items and a knapsack, each item has a value and weight. Under the knapsack capacity constraint, select items to maximize total value.
      • Traveling Salesman Problem: Starting from a point in a graph, visit all other points exactly once and return to the starting point, finding the shortest path.
      • Maximum Clique: Given an undirected graph, find the largest complete subgraph, i.e., a subgraph where any two vertices are connected by an edge.

      Note that for many combinatorial optimization problems, backtracking is not the optimal solution.

      • The 0-1 Knapsack problem is usually solved using dynamic programming to achieve higher time efficiency.
      • The Traveling Salesman Problem is a famous NP-Hard problem; common solutions include genetic algorithms and ant colony algorithms.
      • The Maximum Clique problem is a classical problem in graph theory and can be solved using heuristic algorithms such as greedy algorithms.
      "},{"location":"chapter_backtracking/n_queens_problem/","title":"13.4 \u00a0 N-Queens Problem","text":"

      Question

      According to the rules of chess, a queen can attack pieces that share the same row, column, or diagonal line. Given \\(n\\) queens and an \\(n \\times n\\) chessboard, find a placement scheme such that no two queens can attack each other.

      As shown in Figure 13-15, when \\(n = 4\\), there are two solutions that can be found. From the perspective of the backtracking algorithm, an \\(n \\times n\\) chessboard has \\(n^2\\) squares, which provide all the choices choices. During the process of placing queens one by one, the chessboard state changes continuously, and the chessboard at each moment represents the state state.

      Figure 13-15 \u00a0 Solution to the 4-queens problem

      Figure 13-16 illustrates the three constraints of this problem: multiple queens cannot be in the same row, the same column, or on the same diagonal. It is worth noting that diagonals are divided into two types: the main diagonal \\ and the anti-diagonal /.

      Figure 13-16 \u00a0 Constraints of the n-queens problem

      "},{"location":"chapter_backtracking/n_queens_problem/#1-row-by-row-placement-strategy","title":"1. \u00a0 Row-By-Row Placement Strategy","text":"

      Since both the number of queens and the number of rows on the chessboard are \\(n\\), we can easily derive a conclusion: each row of the chessboard allows and only allows exactly one queen to be placed.

      This means we can adopt a row-by-row placement strategy: starting from the first row, place one queen in each row until the last row is completed.

      Figure 13-17 shows the row-by-row placement process for the 4-queens problem. Due to space limitations, the figure only expands one search branch of the first row, and all schemes that do not satisfy the column constraint and diagonal constraints are pruned.

      Figure 13-17 \u00a0 Row-by-row placement strategy

      Essentially, the row-by-row placement strategy serves a pruning function, as it avoids all search branches where multiple queens appear in the same row.

      "},{"location":"chapter_backtracking/n_queens_problem/#2-column-and-diagonal-pruning","title":"2. \u00a0 Column and Diagonal Pruning","text":"

      To satisfy the column constraint, we can use a boolean array cols of length \\(n\\) to record whether each column has a queen. Before each placement decision, we use cols to prune columns that already have queens, and dynamically update the state of cols during backtracking.

      Tip

      Please note that the origin of the matrix is located in the upper-left corner, where the row index increases from top to bottom, and the column index increases from left to right.

      So how do we handle diagonal constraints? Consider a square on the chessboard with row and column indices \\((row, col)\\). If we select a specific main diagonal in the matrix, we find that all squares on that diagonal have the same difference between their row and column indices, meaning that \\(row - col\\) is a constant value for all squares on the main diagonal.

      In other words, if two squares satisfy \\(row_1 - col_1 = row_2 - col_2\\), they must be on the same main diagonal. Using this pattern, we can use the array diags1 shown in Figure 13-18 to record whether there is a queen on each main diagonal.

      Similarly, for all squares on an anti-diagonal, the sum \\(row + col\\) is a constant value. We can likewise use the array diags2 to handle anti-diagonal constraints.

      Figure 13-18 \u00a0 Handling column and diagonal constraints

      "},{"location":"chapter_backtracking/n_queens_problem/#3-code-implementation","title":"3. \u00a0 Code Implementation","text":"

      Please note that in an \\(n\\)-dimensional square matrix, the range of \\(row - col\\) is \\([-n + 1, n - 1]\\), and the range of \\(row + col\\) is \\([0, 2n - 2]\\). Therefore, the number of both main diagonals and anti-diagonals is \\(2n - 1\\), meaning the length of both arrays diags1 and diags2 is \\(2n - 1\\).

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby n_queens.py
      def backtrack(\n    row: int,\n    n: int,\n    state: list[list[str]],\n    res: list[list[list[str]]],\n    cols: list[bool],\n    diags1: list[bool],\n    diags2: list[bool],\n):\n    \"\"\"Backtracking algorithm: N queens\"\"\"\n    # When all rows are placed, record the solution\n    if row == n:\n        res.append([list(row) for row in state])\n        return\n    # Traverse all columns\n    for col in range(n):\n        # Calculate the main diagonal and anti-diagonal corresponding to this cell\n        diag1 = row - col + n - 1\n        diag2 = row + col\n        # Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n        if not cols[col] and not diags1[diag1] and not diags2[diag2]:\n            # Attempt: place the queen in this cell\n            state[row][col] = \"Q\"\n            cols[col] = diags1[diag1] = diags2[diag2] = True\n            # Place the next row\n            backtrack(row + 1, n, state, res, cols, diags1, diags2)\n            # Backtrack: restore this cell to an empty cell\n            state[row][col] = \"#\"\n            cols[col] = diags1[diag1] = diags2[diag2] = False\n\ndef n_queens(n: int) -> list[list[list[str]]]:\n    \"\"\"Solve N queens\"\"\"\n    # Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n    state = [[\"#\" for _ in range(n)] for _ in range(n)]\n    cols = [False] * n  # Record whether there is a queen in the column\n    diags1 = [False] * (2 * n - 1)  # Record whether there is a queen on the main diagonal\n    diags2 = [False] * (2 * n - 1)  # Record whether there is a queen on the anti-diagonal\n    res = []\n    backtrack(0, n, state, res, cols, diags1, diags2)\n\n    return res\n
      n_queens.cpp
      /* Backtracking algorithm: N queens */\nvoid backtrack(int row, int n, vector<vector<string>> &state, vector<vector<vector<string>>> &res, vector<bool> &cols,\n               vector<bool> &diags1, vector<bool> &diags2) {\n    // When all rows are placed, record the solution\n    if (row == n) {\n        res.push_back(state);\n        return;\n    }\n    // Traverse all columns\n    for (int col = 0; col < n; col++) {\n        // Calculate the main diagonal and anti-diagonal corresponding to this cell\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // Attempt: place the queen in this cell\n            state[row][col] = \"Q\";\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // Place the next row\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // Backtrack: restore this cell to an empty cell\n            state[row][col] = \"#\";\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* Solve N queens */\nvector<vector<vector<string>>> nQueens(int n) {\n    // Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n    vector<vector<string>> state(n, vector<string>(n, \"#\"));\n    vector<bool> cols(n, false);           // Record whether there is a queen in the column\n    vector<bool> diags1(2 * n - 1, false); // Record whether there is a queen on the main diagonal\n    vector<bool> diags2(2 * n - 1, false); // Record whether there is a queen on the anti-diagonal\n    vector<vector<vector<string>>> res;\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n\n    return res;\n}\n
      n_queens.java
      /* Backtracking algorithm: N queens */\nvoid backtrack(int row, int n, List<List<String>> state, List<List<List<String>>> res,\n        boolean[] cols, boolean[] diags1, boolean[] diags2) {\n    // When all rows are placed, record the solution\n    if (row == n) {\n        List<List<String>> copyState = new ArrayList<>();\n        for (List<String> sRow : state) {\n            copyState.add(new ArrayList<>(sRow));\n        }\n        res.add(copyState);\n        return;\n    }\n    // Traverse all columns\n    for (int col = 0; col < n; col++) {\n        // Calculate the main diagonal and anti-diagonal corresponding to this cell\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // Attempt: place the queen in this cell\n            state.get(row).set(col, \"Q\");\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // Place the next row\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // Backtrack: restore this cell to an empty cell\n            state.get(row).set(col, \"#\");\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* Solve N queens */\nList<List<List<String>>> nQueens(int n) {\n    // Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n    List<List<String>> state = new ArrayList<>();\n    for (int i = 0; i < n; i++) {\n        List<String> row = new ArrayList<>();\n        for (int j = 0; j < n; j++) {\n            row.add(\"#\");\n        }\n        state.add(row);\n    }\n    boolean[] cols = new boolean[n]; // Record whether there is a queen in the column\n    boolean[] diags1 = new boolean[2 * n - 1]; // Record whether there is a queen on the main diagonal\n    boolean[] diags2 = new boolean[2 * n - 1]; // Record whether there is a queen on the anti-diagonal\n    List<List<List<String>>> res = new ArrayList<>();\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n\n    return res;\n}\n
      n_queens.cs
      /* Backtracking algorithm: N queens */\nvoid Backtrack(int row, int n, List<List<string>> state, List<List<List<string>>> res,\n        bool[] cols, bool[] diags1, bool[] diags2) {\n    // When all rows are placed, record the solution\n    if (row == n) {\n        List<List<string>> copyState = [];\n        foreach (List<string> sRow in state) {\n            copyState.Add(new List<string>(sRow));\n        }\n        res.Add(copyState);\n        return;\n    }\n    // Traverse all columns\n    for (int col = 0; col < n; col++) {\n        // Calculate the main diagonal and anti-diagonal corresponding to this cell\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // Attempt: place the queen in this cell\n            state[row][col] = \"Q\";\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // Place the next row\n            Backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // Backtrack: restore this cell to an empty cell\n            state[row][col] = \"#\";\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* Solve N queens */\nList<List<List<string>>> NQueens(int n) {\n    // Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n    List<List<string>> state = [];\n    for (int i = 0; i < n; i++) {\n        List<string> row = [];\n        for (int j = 0; j < n; j++) {\n            row.Add(\"#\");\n        }\n        state.Add(row);\n    }\n    bool[] cols = new bool[n]; // Record whether there is a queen in the column\n    bool[] diags1 = new bool[2 * n - 1]; // Record whether there is a queen on the main diagonal\n    bool[] diags2 = new bool[2 * n - 1]; // Record whether there is a queen on the anti-diagonal\n    List<List<List<string>>> res = [];\n\n    Backtrack(0, n, state, res, cols, diags1, diags2);\n\n    return res;\n}\n
      n_queens.go
      /* Backtracking algorithm: N queens */\nfunc backtrack(row, n int, state *[][]string, res *[][][]string, cols, diags1, diags2 *[]bool) {\n    // When all rows are placed, record the solution\n    if row == n {\n        newState := make([][]string, len(*state))\n        for i, _ := range newState {\n            newState[i] = make([]string, len((*state)[0]))\n            copy(newState[i], (*state)[i])\n\n        }\n        *res = append(*res, newState)\n        return\n    }\n    // Traverse all columns\n    for col := 0; col < n; col++ {\n        // Calculate the main diagonal and anti-diagonal corresponding to this cell\n        diag1 := row - col + n - 1\n        diag2 := row + col\n        // Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n        if !(*cols)[col] && !(*diags1)[diag1] && !(*diags2)[diag2] {\n            // Attempt: place the queen in this cell\n            (*state)[row][col] = \"Q\"\n            (*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = true, true, true\n            // Place the next row\n            backtrack(row+1, n, state, res, cols, diags1, diags2)\n            // Backtrack: restore this cell to an empty cell\n            (*state)[row][col] = \"#\"\n            (*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = false, false, false\n        }\n    }\n}\n\n/* Solve N queens */\nfunc nQueens(n int) [][][]string {\n    // Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n    state := make([][]string, n)\n    for i := 0; i < n; i++ {\n        row := make([]string, n)\n        for i := 0; i < n; i++ {\n            row[i] = \"#\"\n        }\n        state[i] = row\n    }\n    // Record whether there is a queen in the column\n    cols := make([]bool, n)\n    diags1 := make([]bool, 2*n-1)\n    diags2 := make([]bool, 2*n-1)\n    res := make([][][]string, 0)\n    backtrack(0, n, &state, &res, &cols, &diags1, &diags2)\n    return res\n}\n
      n_queens.swift
      /* Backtracking algorithm: N queens */\nfunc backtrack(row: Int, n: Int, state: inout [[String]], res: inout [[[String]]], cols: inout [Bool], diags1: inout [Bool], diags2: inout [Bool]) {\n    // When all rows are placed, record the solution\n    if row == n {\n        res.append(state)\n        return\n    }\n    // Traverse all columns\n    for col in 0 ..< n {\n        // Calculate the main diagonal and anti-diagonal corresponding to this cell\n        let diag1 = row - col + n - 1\n        let diag2 = row + col\n        // Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n        if !cols[col] && !diags1[diag1] && !diags2[diag2] {\n            // Attempt: place the queen in this cell\n            state[row][col] = \"Q\"\n            cols[col] = true\n            diags1[diag1] = true\n            diags2[diag2] = true\n            // Place the next row\n            backtrack(row: row + 1, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)\n            // Backtrack: restore this cell to an empty cell\n            state[row][col] = \"#\"\n            cols[col] = false\n            diags1[diag1] = false\n            diags2[diag2] = false\n        }\n    }\n}\n\n/* Solve N queens */\nfunc nQueens(n: Int) -> [[[String]]] {\n    // Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n    var state = Array(repeating: Array(repeating: \"#\", count: n), count: n)\n    var cols = Array(repeating: false, count: n) // Record whether there is a queen in the column\n    var diags1 = Array(repeating: false, count: 2 * n - 1) // Record whether there is a queen on the main diagonal\n    var diags2 = Array(repeating: false, count: 2 * n - 1) // Record whether there is a queen on the anti-diagonal\n    var res: [[[String]]] = []\n\n    backtrack(row: 0, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)\n\n    return res\n}\n
      n_queens.js
      /* Backtracking algorithm: N queens */\nfunction backtrack(row, n, state, res, cols, diags1, diags2) {\n    // When all rows are placed, record the solution\n    if (row === n) {\n        res.push(state.map((row) => row.slice()));\n        return;\n    }\n    // Traverse all columns\n    for (let col = 0; col < n; col++) {\n        // Calculate the main diagonal and anti-diagonal corresponding to this cell\n        const diag1 = row - col + n - 1;\n        const diag2 = row + col;\n        // Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // Attempt: place the queen in this cell\n            state[row][col] = 'Q';\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // Place the next row\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // Backtrack: restore this cell to an empty cell\n            state[row][col] = '#';\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* Solve N queens */\nfunction nQueens(n) {\n    // Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n    const state = Array.from({ length: n }, () => Array(n).fill('#'));\n    const cols = Array(n).fill(false); // Record whether there is a queen in the column\n    const diags1 = Array(2 * n - 1).fill(false); // Record whether there is a queen on the main diagonal\n    const diags2 = Array(2 * n - 1).fill(false); // Record whether there is a queen on the anti-diagonal\n    const res = [];\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n    return res;\n}\n
      n_queens.ts
      /* Backtracking algorithm: N queens */\nfunction backtrack(\n    row: number,\n    n: number,\n    state: string[][],\n    res: string[][][],\n    cols: boolean[],\n    diags1: boolean[],\n    diags2: boolean[]\n): void {\n    // When all rows are placed, record the solution\n    if (row === n) {\n        res.push(state.map((row) => row.slice()));\n        return;\n    }\n    // Traverse all columns\n    for (let col = 0; col < n; col++) {\n        // Calculate the main diagonal and anti-diagonal corresponding to this cell\n        const diag1 = row - col + n - 1;\n        const diag2 = row + col;\n        // Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // Attempt: place the queen in this cell\n            state[row][col] = 'Q';\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // Place the next row\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // Backtrack: restore this cell to an empty cell\n            state[row][col] = '#';\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* Solve N queens */\nfunction nQueens(n: number): string[][][] {\n    // Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n    const state = Array.from({ length: n }, () => Array(n).fill('#'));\n    const cols = Array(n).fill(false); // Record whether there is a queen in the column\n    const diags1 = Array(2 * n - 1).fill(false); // Record whether there is a queen on the main diagonal\n    const diags2 = Array(2 * n - 1).fill(false); // Record whether there is a queen on the anti-diagonal\n    const res: string[][][] = [];\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n    return res;\n}\n
      n_queens.dart
      /* Backtracking algorithm: N queens */\nvoid backtrack(\n  int row,\n  int n,\n  List<List<String>> state,\n  List<List<List<String>>> res,\n  List<bool> cols,\n  List<bool> diags1,\n  List<bool> diags2,\n) {\n  // When all rows are placed, record the solution\n  if (row == n) {\n    List<List<String>> copyState = [];\n    for (List<String> sRow in state) {\n      copyState.add(List.from(sRow));\n    }\n    res.add(copyState);\n    return;\n  }\n  // Traverse all columns\n  for (int col = 0; col < n; col++) {\n    // Calculate the main diagonal and anti-diagonal corresponding to this cell\n    int diag1 = row - col + n - 1;\n    int diag2 = row + col;\n    // Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n    if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n      // Attempt: place the queen in this cell\n      state[row][col] = \"Q\";\n      cols[col] = true;\n      diags1[diag1] = true;\n      diags2[diag2] = true;\n      // Place the next row\n      backtrack(row + 1, n, state, res, cols, diags1, diags2);\n      // Backtrack: restore this cell to an empty cell\n      state[row][col] = \"#\";\n      cols[col] = false;\n      diags1[diag1] = false;\n      diags2[diag2] = false;\n    }\n  }\n}\n\n/* Solve N queens */\nList<List<List<String>>> nQueens(int n) {\n  // Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n  List<List<String>> state = List.generate(n, (index) => List.filled(n, \"#\"));\n  List<bool> cols = List.filled(n, false); // Record whether there is a queen in the column\n  List<bool> diags1 = List.filled(2 * n - 1, false); // Record whether there is a queen on the main diagonal\n  List<bool> diags2 = List.filled(2 * n - 1, false); // Record whether there is a queen on the anti-diagonal\n  List<List<List<String>>> res = [];\n\n  backtrack(0, n, state, res, cols, diags1, diags2);\n\n  return res;\n}\n
      n_queens.rs
      /* Backtracking algorithm: N queens */\nfn backtrack(\n    row: usize,\n    n: usize,\n    state: &mut Vec<Vec<String>>,\n    res: &mut Vec<Vec<Vec<String>>>,\n    cols: &mut [bool],\n    diags1: &mut [bool],\n    diags2: &mut [bool],\n) {\n    // When all rows are placed, record the solution\n    if row == n {\n        res.push(state.clone());\n        return;\n    }\n    // Traverse all columns\n    for col in 0..n {\n        // Calculate the main diagonal and anti-diagonal corresponding to this cell\n        let diag1 = row + n - 1 - col;\n        let diag2 = row + col;\n        // Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n        if !cols[col] && !diags1[diag1] && !diags2[diag2] {\n            // Attempt: place the queen in this cell\n            state[row][col] = \"Q\".into();\n            (cols[col], diags1[diag1], diags2[diag2]) = (true, true, true);\n            // Place the next row\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // Backtrack: restore this cell to an empty cell\n            state[row][col] = \"#\".into();\n            (cols[col], diags1[diag1], diags2[diag2]) = (false, false, false);\n        }\n    }\n}\n\n/* Solve N queens */\nfn n_queens(n: usize) -> Vec<Vec<Vec<String>>> {\n    // Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n    let mut state: Vec<Vec<String>> = vec![vec![\"#\".to_string(); n]; n];\n    let mut cols = vec![false; n]; // Record whether there is a queen in the column\n    let mut diags1 = vec![false; 2 * n - 1]; // Record whether there is a queen on the main diagonal\n    let mut diags2 = vec![false; 2 * n - 1]; // Record whether there is a queen on the anti-diagonal\n    let mut res: Vec<Vec<Vec<String>>> = Vec::new();\n\n    backtrack(\n        0,\n        n,\n        &mut state,\n        &mut res,\n        &mut cols,\n        &mut diags1,\n        &mut diags2,\n    );\n\n    res\n}\n
      n_queens.c
      /* Backtracking algorithm: N queens */\nvoid backtrack(int row, int n, char state[MAX_SIZE][MAX_SIZE], char ***res, int *resSize, bool cols[MAX_SIZE],\n               bool diags1[2 * MAX_SIZE - 1], bool diags2[2 * MAX_SIZE - 1]) {\n    // When all rows are placed, record the solution\n    if (row == n) {\n        res[*resSize] = (char **)malloc(sizeof(char *) * n);\n        for (int i = 0; i < n; ++i) {\n            res[*resSize][i] = (char *)malloc(sizeof(char) * (n + 1));\n            strcpy(res[*resSize][i], state[i]);\n        }\n        (*resSize)++;\n        return;\n    }\n    // Traverse all columns\n    for (int col = 0; col < n; col++) {\n        // Calculate the main diagonal and anti-diagonal corresponding to this cell\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // Attempt: place the queen in this cell\n            state[row][col] = 'Q';\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // Place the next row\n            backtrack(row + 1, n, state, res, resSize, cols, diags1, diags2);\n            // Backtrack: restore this cell to an empty cell\n            state[row][col] = '#';\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* Solve N queens */\nchar ***nQueens(int n, int *returnSize) {\n    char state[MAX_SIZE][MAX_SIZE];\n    // Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n    for (int i = 0; i < n; ++i) {\n        for (int j = 0; j < n; ++j) {\n            state[i][j] = '#';\n        }\n        state[i][n] = '\\0';\n    }\n    bool cols[MAX_SIZE] = {false};           // Record whether there is a queen in the column\n    bool diags1[2 * MAX_SIZE - 1] = {false}; // Record whether there is a queen on the main diagonal\n    bool diags2[2 * MAX_SIZE - 1] = {false}; // Record whether there is a queen on the anti-diagonal\n\n    char ***res = (char ***)malloc(sizeof(char **) * MAX_SIZE);\n    *returnSize = 0;\n    backtrack(0, n, state, res, returnSize, cols, diags1, diags2);\n    return res;\n}\n
      n_queens.kt
      /* Backtracking algorithm: N queens */\nfun backtrack(\n    row: Int,\n    n: Int,\n    state: MutableList<MutableList<String>>,\n    res: MutableList<MutableList<MutableList<String>>?>,\n    cols: BooleanArray,\n    diags1: BooleanArray,\n    diags2: BooleanArray\n) {\n    // When all rows are placed, record the solution\n    if (row == n) {\n        val copyState = mutableListOf<MutableList<String>>()\n        for (sRow in state) {\n            copyState.add(sRow.toMutableList())\n        }\n        res.add(copyState)\n        return\n    }\n    // Traverse all columns\n    for (col in 0..<n) {\n        // Calculate the main diagonal and anti-diagonal corresponding to this cell\n        val diag1 = row - col + n - 1\n        val diag2 = row + col\n        // Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // Attempt: place the queen in this cell\n            state[row][col] = \"Q\"\n            diags2[diag2] = true\n            diags1[diag1] = diags2[diag2]\n            cols[col] = diags1[diag1]\n            // Place the next row\n            backtrack(row + 1, n, state, res, cols, diags1, diags2)\n            // Backtrack: restore this cell to an empty cell\n            state[row][col] = \"#\"\n            diags2[diag2] = false\n            diags1[diag1] = diags2[diag2]\n            cols[col] = diags1[diag1]\n        }\n    }\n}\n\n/* Solve N queens */\nfun nQueens(n: Int): MutableList<MutableList<MutableList<String>>?> {\n    // Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n    val state = mutableListOf<MutableList<String>>()\n    for (i in 0..<n) {\n        val row = mutableListOf<String>()\n        for (j in 0..<n) {\n            row.add(\"#\")\n        }\n        state.add(row)\n    }\n    val cols = BooleanArray(n) // Record whether there is a queen in the column\n    val diags1 = BooleanArray(2 * n - 1) // Record whether there is a queen on the main diagonal\n    val diags2 = BooleanArray(2 * n - 1) // Record whether there is a queen on the anti-diagonal\n    val res = mutableListOf<MutableList<MutableList<String>>?>()\n\n    backtrack(0, n, state, res, cols, diags1, diags2)\n\n    return res\n}\n
      n_queens.rb
      ### Backtracking: n queens ###\ndef backtrack(row, n, state, res, cols, diags1, diags2)\n  # When all rows are placed, record the solution\n  if row == n\n    res << state.map { |row| row.dup }\n    return\n  end\n\n  # Traverse all columns\n  for col in 0...n\n    # Calculate the main diagonal and anti-diagonal corresponding to this cell\n    diag1 = row - col + n - 1\n    diag2 = row + col\n    # Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n    if !cols[col] && !diags1[diag1] && !diags2[diag2]\n      # Attempt: place the queen in this cell\n      state[row][col] = \"Q\"\n      cols[col] = diags1[diag1] = diags2[diag2] = true\n      # Place the next row\n      backtrack(row + 1, n, state, res, cols, diags1, diags2)\n      # Backtrack: restore this cell to an empty cell\n      state[row][col] = \"#\"\n      cols[col] = diags1[diag1] = diags2[diag2] = false\n    end\n  end\nend\n\n### Solve n queens ###\ndef n_queens(n)\n  # Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n  state = Array.new(n) { Array.new(n, \"#\") }\n  cols = Array.new(n, false) # Record whether there is a queen in the column\n  diags1 = Array.new(2 * n - 1, false) # Record whether there is a queen on the main diagonal\n  diags2 = Array.new(2 * n - 1, false) # Record whether there is a queen on the anti-diagonal\n  res = []\n  backtrack(0, n, state, res, cols, diags1, diags2)\n\n  res\nend\n

      Placing \\(n\\) queens row by row, considering the column constraint, from the first row to the last row there are \\(n\\), \\(n-1\\), \\(\\dots\\), \\(2\\), \\(1\\) choices, using \\(O(n!)\\) time. When recording a solution, it is necessary to copy the matrix state and add it to res, and the copy operation uses \\(O(n^2)\\) time. Therefore, the overall time complexity is \\(O(n! \\cdot n^2)\\). In practice, pruning based on diagonal constraints can also significantly reduce the search space, so the search efficiency is often better than the time complexity mentioned above.

      The array state uses \\(O(n^2)\\) space, and the arrays cols, diags1, and diags2 each use \\(O(n)\\) space. The maximum recursion depth is \\(n\\), using \\(O(n)\\) stack frame space. Therefore, the space complexity is \\(O(n^2)\\).

      "},{"location":"chapter_backtracking/permutations_problem/","title":"13.2 \u00a0 Permutations Problem","text":"

      The permutations problem is a classic application of backtracking algorithms. It is defined as finding all possible arrangements of elements in a given collection (such as an array or string).

      Table 13-2 shows several example datasets, including input arrays and their corresponding permutations.

      Table 13-2 \u00a0 Permutations Examples

      Input Array All Permutations \\([1]\\) \\([1]\\) \\([1, 2]\\) \\([1, 2], [2, 1]\\) \\([1, 2, 3]\\) \\([1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]\\)"},{"location":"chapter_backtracking/permutations_problem/#1321-case-with-distinct-elements","title":"13.2.1 \u00a0 Case with Distinct Elements","text":"

      Question

      Given an integer array with no duplicate elements, return all possible permutations.

      From the perspective of backtracking algorithms, we can imagine the process of generating permutations as the result of a series of choices. Suppose the input array is \\([1, 2, 3]\\). If we first choose \\(1\\), then choose \\(3\\), and finally choose \\(2\\), we obtain the permutation \\([1, 3, 2]\\). Backtracking means undoing a choice and then trying other choices.

      From the perspective of backtracking code, the candidate set choices consists of all elements in the input array, and the state state is the elements that have been chosen so far. Note that each element can only be chosen once, therefore all elements in state should be unique.

      As shown in Figure 13-5, we can unfold the search process into a recursion tree, where each node in the tree represents the current state state. Starting from the root node, after three rounds of choices, we reach a leaf node, and each leaf node corresponds to a permutation.

      Figure 13-5 \u00a0 Recursion tree of permutations

      "},{"location":"chapter_backtracking/permutations_problem/#1-pruning-duplicate-choices","title":"1. \u00a0 Pruning Duplicate Choices","text":"

      To ensure that each element is chosen only once, we consider introducing a boolean array selected, where selected[i] indicates whether choices[i] has been chosen. We implement the following pruning operation based on it.

      • After making a choice choice[i], we set selected[i] to \\(\\text{True}\\), indicating that it has been chosen.
      • When traversing the candidate list choices, we skip all nodes that have been chosen, which is pruning.

      As shown in Figure 13-6, suppose we choose \\(1\\) in the first round, \\(3\\) in the second round, and \\(2\\) in the third round. Then we need to prune the branch of element \\(1\\) in the second round and prune the branches of elements \\(1\\) and \\(3\\) in the third round.

      Figure 13-6 \u00a0 Pruning example of permutations

      Observing the above figure, we find that this pruning operation reduces the search space size from \\(O(n^n)\\) to \\(O(n!)\\).

      "},{"location":"chapter_backtracking/permutations_problem/#2-code-implementation","title":"2. \u00a0 Code Implementation","text":"

      After understanding the above information, we can fill in the blanks in the template code. To shorten the overall code, we do not implement each function in the template separately, but instead unfold them in the backtrack() function:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby permutations_i.py
      def backtrack(\n    state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]\n):\n    \"\"\"Backtracking algorithm: Permutations I\"\"\"\n    # When the state length equals the number of elements, record the solution\n    if len(state) == len(choices):\n        res.append(list(state))\n        return\n    # Traverse all choices\n    for i, choice in enumerate(choices):\n        # Pruning: do not allow repeated selection of elements\n        if not selected[i]:\n            # Attempt: make choice, update state\n            selected[i] = True\n            state.append(choice)\n            # Proceed to the next round of selection\n            backtrack(state, choices, selected, res)\n            # Backtrack: undo choice, restore to previous state\n            selected[i] = False\n            state.pop()\n\ndef permutations_i(nums: list[int]) -> list[list[int]]:\n    \"\"\"Permutations I\"\"\"\n    res = []\n    backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\n    return res\n
      permutations_i.cpp
      /* Backtracking algorithm: Permutations I */\nvoid backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &selected, vector<vector<int>> &res) {\n    // When the state length equals the number of elements, record the solution\n    if (state.size() == choices.size()) {\n        res.push_back(state);\n        return;\n    }\n    // Traverse all choices\n    for (int i = 0; i < choices.size(); i++) {\n        int choice = choices[i];\n        // Pruning: do not allow repeated selection of elements\n        if (!selected[i]) {\n            // Attempt: make choice, update state\n            selected[i] = true;\n            state.push_back(choice);\n            // Proceed to the next round of selection\n            backtrack(state, choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.pop_back();\n        }\n    }\n}\n\n/* Permutations I */\nvector<vector<int>> permutationsI(vector<int> nums) {\n    vector<int> state;\n    vector<bool> selected(nums.size(), false);\n    vector<vector<int>> res;\n    backtrack(state, nums, selected, res);\n    return res;\n}\n
      permutations_i.java
      /* Backtracking algorithm: Permutations I */\nvoid backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {\n    // When the state length equals the number of elements, record the solution\n    if (state.size() == choices.length) {\n        res.add(new ArrayList<Integer>(state));\n        return;\n    }\n    // Traverse all choices\n    for (int i = 0; i < choices.length; i++) {\n        int choice = choices[i];\n        // Pruning: do not allow repeated selection of elements\n        if (!selected[i]) {\n            // Attempt: make choice, update state\n            selected[i] = true;\n            state.add(choice);\n            // Proceed to the next round of selection\n            backtrack(state, choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.remove(state.size() - 1);\n        }\n    }\n}\n\n/* Permutations I */\nList<List<Integer>> permutationsI(int[] nums) {\n    List<List<Integer>> res = new ArrayList<List<Integer>>();\n    backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);\n    return res;\n}\n
      permutations_i.cs
      /* Backtracking algorithm: Permutations I */\nvoid Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {\n    // When the state length equals the number of elements, record the solution\n    if (state.Count == choices.Length) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // Traverse all choices\n    for (int i = 0; i < choices.Length; i++) {\n        int choice = choices[i];\n        // Pruning: do not allow repeated selection of elements\n        if (!selected[i]) {\n            // Attempt: make choice, update state\n            selected[i] = true;\n            state.Add(choice);\n            // Proceed to the next round of selection\n            Backtrack(state, choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.RemoveAt(state.Count - 1);\n        }\n    }\n}\n\n/* Permutations I */\nList<List<int>> PermutationsI(int[] nums) {\n    List<List<int>> res = [];\n    Backtrack([], nums, new bool[nums.Length], res);\n    return res;\n}\n
      permutations_i.go
      /* Backtracking algorithm: Permutations I */\nfunc backtrackI(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {\n    // When the state length equals the number of elements, record the solution\n    if len(*state) == len(*choices) {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n    }\n    // Traverse all choices\n    for i := 0; i < len(*choices); i++ {\n        choice := (*choices)[i]\n        // Pruning: do not allow repeated selection of elements\n        if !(*selected)[i] {\n            // Attempt: make choice, update state\n            (*selected)[i] = true\n            *state = append(*state, choice)\n            // Proceed to the next round of selection\n            backtrackI(state, choices, selected, res)\n            // Backtrack: undo choice, restore to previous state\n            (*selected)[i] = false\n            *state = (*state)[:len(*state)-1]\n        }\n    }\n}\n\n/* Permutations I */\nfunc permutationsI(nums []int) [][]int {\n    res := make([][]int, 0)\n    state := make([]int, 0)\n    selected := make([]bool, len(nums))\n    backtrackI(&state, &nums, &selected, &res)\n    return res\n}\n
      permutations_i.swift
      /* Backtracking algorithm: Permutations I */\nfunc backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {\n    // When the state length equals the number of elements, record the solution\n    if state.count == choices.count {\n        res.append(state)\n        return\n    }\n    // Traverse all choices\n    for (i, choice) in choices.enumerated() {\n        // Pruning: do not allow repeated selection of elements\n        if !selected[i] {\n            // Attempt: make choice, update state\n            selected[i] = true\n            state.append(choice)\n            // Proceed to the next round of selection\n            backtrack(state: &state, choices: choices, selected: &selected, res: &res)\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false\n            state.removeLast()\n        }\n    }\n}\n\n/* Permutations I */\nfunc permutationsI(nums: [Int]) -> [[Int]] {\n    var state: [Int] = []\n    var selected = Array(repeating: false, count: nums.count)\n    var res: [[Int]] = []\n    backtrack(state: &state, choices: nums, selected: &selected, res: &res)\n    return res\n}\n
      permutations_i.js
      /* Backtracking algorithm: Permutations I */\nfunction backtrack(state, choices, selected, res) {\n    // When the state length equals the number of elements, record the solution\n    if (state.length === choices.length) {\n        res.push([...state]);\n        return;\n    }\n    // Traverse all choices\n    choices.forEach((choice, i) => {\n        // Pruning: do not allow repeated selection of elements\n        if (!selected[i]) {\n            // Attempt: make choice, update state\n            selected[i] = true;\n            state.push(choice);\n            // Proceed to the next round of selection\n            backtrack(state, choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.pop();\n        }\n    });\n}\n\n/* Permutations I */\nfunction permutationsI(nums) {\n    const res = [];\n    backtrack([], nums, Array(nums.length).fill(false), res);\n    return res;\n}\n
      permutations_i.ts
      /* Backtracking algorithm: Permutations I */\nfunction backtrack(\n    state: number[],\n    choices: number[],\n    selected: boolean[],\n    res: number[][]\n): void {\n    // When the state length equals the number of elements, record the solution\n    if (state.length === choices.length) {\n        res.push([...state]);\n        return;\n    }\n    // Traverse all choices\n    choices.forEach((choice, i) => {\n        // Pruning: do not allow repeated selection of elements\n        if (!selected[i]) {\n            // Attempt: make choice, update state\n            selected[i] = true;\n            state.push(choice);\n            // Proceed to the next round of selection\n            backtrack(state, choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.pop();\n        }\n    });\n}\n\n/* Permutations I */\nfunction permutationsI(nums: number[]): number[][] {\n    const res: number[][] = [];\n    backtrack([], nums, Array(nums.length).fill(false), res);\n    return res;\n}\n
      permutations_i.dart
      /* Backtracking algorithm: Permutations I */\nvoid backtrack(\n  List<int> state,\n  List<int> choices,\n  List<bool> selected,\n  List<List<int>> res,\n) {\n  // When the state length equals the number of elements, record the solution\n  if (state.length == choices.length) {\n    res.add(List.from(state));\n    return;\n  }\n  // Traverse all choices\n  for (int i = 0; i < choices.length; i++) {\n    int choice = choices[i];\n    // Pruning: do not allow repeated selection of elements\n    if (!selected[i]) {\n      // Attempt: make choice, update state\n      selected[i] = true;\n      state.add(choice);\n      // Proceed to the next round of selection\n      backtrack(state, choices, selected, res);\n      // Backtrack: undo choice, restore to previous state\n      selected[i] = false;\n      state.removeLast();\n    }\n  }\n}\n\n/* Permutations I */\nList<List<int>> permutationsI(List<int> nums) {\n  List<List<int>> res = [];\n  backtrack([], nums, List.filled(nums.length, false), res);\n  return res;\n}\n
      permutations_i.rs
      /* Backtracking algorithm: Permutations I */\nfn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {\n    // When the state length equals the number of elements, record the solution\n    if state.len() == choices.len() {\n        res.push(state);\n        return;\n    }\n    // Traverse all choices\n    for i in 0..choices.len() {\n        let choice = choices[i];\n        // Pruning: do not allow repeated selection of elements\n        if !selected[i] {\n            // Attempt: make choice, update state\n            selected[i] = true;\n            state.push(choice);\n            // Proceed to the next round of selection\n            backtrack(state.clone(), choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.pop();\n        }\n    }\n}\n\n/* Permutations I */\nfn permutations_i(nums: &mut [i32]) -> Vec<Vec<i32>> {\n    let mut res = Vec::new(); // State (subset)\n    backtrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);\n    res\n}\n
      permutations_i.c
      /* Backtracking algorithm: Permutations I */\nvoid backtrack(int *state, int stateSize, int *choices, int choicesSize, bool *selected, int **res, int *resSize) {\n    // When the state length equals the number of elements, record the solution\n    if (stateSize == choicesSize) {\n        res[*resSize] = (int *)malloc(choicesSize * sizeof(int));\n        for (int i = 0; i < choicesSize; i++) {\n            res[*resSize][i] = state[i];\n        }\n        (*resSize)++;\n        return;\n    }\n    // Traverse all choices\n    for (int i = 0; i < choicesSize; i++) {\n        int choice = choices[i];\n        // Pruning: do not allow repeated selection of elements\n        if (!selected[i]) {\n            // Attempt: make choice, update state\n            selected[i] = true;\n            state[stateSize] = choice;\n            // Proceed to the next round of selection\n            backtrack(state, stateSize + 1, choices, choicesSize, selected, res, resSize);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n        }\n    }\n}\n\n/* Permutations I */\nint **permutationsI(int *nums, int numsSize, int *returnSize) {\n    int *state = (int *)malloc(numsSize * sizeof(int));\n    bool *selected = (bool *)malloc(numsSize * sizeof(bool));\n    for (int i = 0; i < numsSize; i++) {\n        selected[i] = false;\n    }\n    int **res = (int **)malloc(MAX_SIZE * sizeof(int *));\n    *returnSize = 0;\n\n    backtrack(state, 0, nums, numsSize, selected, res, returnSize);\n\n    free(state);\n    free(selected);\n\n    return res;\n}\n
      permutations_i.kt
      /* Backtracking algorithm: Permutations I */\nfun backtrack(\n    state: MutableList<Int>,\n    choices: IntArray,\n    selected: BooleanArray,\n    res: MutableList<MutableList<Int>?>\n) {\n    // When the state length equals the number of elements, record the solution\n    if (state.size == choices.size) {\n        res.add(state.toMutableList())\n        return\n    }\n    // Traverse all choices\n    for (i in choices.indices) {\n        val choice = choices[i]\n        // Pruning: do not allow repeated selection of elements\n        if (!selected[i]) {\n            // Attempt: make choice, update state\n            selected[i] = true\n            state.add(choice)\n            // Proceed to the next round of selection\n            backtrack(state, choices, selected, res)\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false\n            state.removeAt(state.size - 1)\n        }\n    }\n}\n\n/* Permutations I */\nfun permutationsI(nums: IntArray): MutableList<MutableList<Int>?> {\n    val res = mutableListOf<MutableList<Int>?>()\n    backtrack(mutableListOf(), nums, BooleanArray(nums.size), res)\n    return res\n}\n
      permutations_i.rb
      ### Backtracking: permutations I ###\ndef backtrack(state, choices, selected, res)\n  # When the state length equals the number of elements, record the solution\n  if state.length == choices.length\n    res << state.dup\n    return\n  end\n\n  # Traverse all choices\n  choices.each_with_index do |choice, i|\n    # Pruning: do not allow repeated selection of elements\n    unless selected[i]\n      # Attempt: make choice, update state\n      selected[i] = true\n      state << choice\n      # Proceed to the next round of selection\n      backtrack(state, choices, selected, res)\n      # Backtrack: undo choice, restore to previous state\n      selected[i] = false\n      state.pop\n    end\n  end\nend\n\n### Permutations I ###\ndef permutations_i(nums)\n  res = []\n  backtrack([], nums, Array.new(nums.length, false), res)\n  res\nend\n
      "},{"location":"chapter_backtracking/permutations_problem/#1322-case-with-duplicate-elements","title":"13.2.2 \u00a0 Case with Duplicate Elements","text":"

      Question

      Given an integer array that may contain duplicate elements, return all unique permutations.

      Suppose the input array is \\([1, 1, 2]\\). To distinguish the two duplicate elements \\(1\\), we denote the second \\(1\\) as \\(\\hat{1}\\).

      As shown in Figure 13-7, the method described above generates permutations where half are duplicates.

      Figure 13-7 \u00a0 Duplicate permutations

      So how do we remove duplicate permutations? The most direct approach is to use a hash set to directly deduplicate the permutation results. However, this is not elegant because the search branches that generate duplicate permutations are unnecessary and should be identified and pruned early, which can further improve algorithm efficiency.

      "},{"location":"chapter_backtracking/permutations_problem/#1-pruning-duplicate-elements","title":"1. \u00a0 Pruning Duplicate Elements","text":"

      Observe Figure 13-8. In the first round, choosing \\(1\\) or choosing \\(\\hat{1}\\) is equivalent. All permutations generated under these two choices are duplicates. Therefore, we should prune \\(\\hat{1}\\).

      Similarly, after choosing \\(2\\) in the first round, the \\(1\\) and \\(\\hat{1}\\) in the second round also produce duplicate branches, so the second round's \\(\\hat{1}\\) should also be pruned.

      Essentially, our goal is to ensure that multiple equal elements are chosen only once in a certain round of choices.

      Figure 13-8 \u00a0 Pruning duplicate permutations

      "},{"location":"chapter_backtracking/permutations_problem/#2-code-implementation_1","title":"2. \u00a0 Code Implementation","text":"

      Building on the code from the previous problem, we consider opening a hash set duplicated in each round of choices to record which elements have been tried in this round, and prune duplicate elements:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby permutations_ii.py
      def backtrack(\n    state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]\n):\n    \"\"\"Backtracking algorithm: Permutations II\"\"\"\n    # When the state length equals the number of elements, record the solution\n    if len(state) == len(choices):\n        res.append(list(state))\n        return\n    # Traverse all choices\n    duplicated = set[int]()\n    for i, choice in enumerate(choices):\n        # Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n        if not selected[i] and choice not in duplicated:\n            # Attempt: make choice, update state\n            duplicated.add(choice)  # Record the selected element value\n            selected[i] = True\n            state.append(choice)\n            # Proceed to the next round of selection\n            backtrack(state, choices, selected, res)\n            # Backtrack: undo choice, restore to previous state\n            selected[i] = False\n            state.pop()\n\ndef permutations_ii(nums: list[int]) -> list[list[int]]:\n    \"\"\"Permutations II\"\"\"\n    res = []\n    backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\n    return res\n
      permutations_ii.cpp
      /* Backtracking algorithm: Permutations II */\nvoid backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &selected, vector<vector<int>> &res) {\n    // When the state length equals the number of elements, record the solution\n    if (state.size() == choices.size()) {\n        res.push_back(state);\n        return;\n    }\n    // Traverse all choices\n    unordered_set<int> duplicated;\n    for (int i = 0; i < choices.size(); i++) {\n        int choice = choices[i];\n        // Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n        if (!selected[i] && duplicated.find(choice) == duplicated.end()) {\n            // Attempt: make choice, update state\n            duplicated.emplace(choice); // Record the selected element value\n            selected[i] = true;\n            state.push_back(choice);\n            // Proceed to the next round of selection\n            backtrack(state, choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.pop_back();\n        }\n    }\n}\n\n/* Permutations II */\nvector<vector<int>> permutationsII(vector<int> nums) {\n    vector<int> state;\n    vector<bool> selected(nums.size(), false);\n    vector<vector<int>> res;\n    backtrack(state, nums, selected, res);\n    return res;\n}\n
      permutations_ii.java
      /* Backtracking algorithm: Permutations II */\nvoid backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {\n    // When the state length equals the number of elements, record the solution\n    if (state.size() == choices.length) {\n        res.add(new ArrayList<Integer>(state));\n        return;\n    }\n    // Traverse all choices\n    Set<Integer> duplicated = new HashSet<Integer>();\n    for (int i = 0; i < choices.length; i++) {\n        int choice = choices[i];\n        // Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n        if (!selected[i] && !duplicated.contains(choice)) {\n            // Attempt: make choice, update state\n            duplicated.add(choice); // Record the selected element value\n            selected[i] = true;\n            state.add(choice);\n            // Proceed to the next round of selection\n            backtrack(state, choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.remove(state.size() - 1);\n        }\n    }\n}\n\n/* Permutations II */\nList<List<Integer>> permutationsII(int[] nums) {\n    List<List<Integer>> res = new ArrayList<List<Integer>>();\n    backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);\n    return res;\n}\n
      permutations_ii.cs
      /* Backtracking algorithm: Permutations II */\nvoid Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {\n    // When the state length equals the number of elements, record the solution\n    if (state.Count == choices.Length) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // Traverse all choices\n    HashSet<int> duplicated = [];\n    for (int i = 0; i < choices.Length; i++) {\n        int choice = choices[i];\n        // Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n        if (!selected[i] && !duplicated.Contains(choice)) {\n            // Attempt: make choice, update state\n            duplicated.Add(choice); // Record the selected element value\n            selected[i] = true;\n            state.Add(choice);\n            // Proceed to the next round of selection\n            Backtrack(state, choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.RemoveAt(state.Count - 1);\n        }\n    }\n}\n\n/* Permutations II */\nList<List<int>> PermutationsII(int[] nums) {\n    List<List<int>> res = [];\n    Backtrack([], nums, new bool[nums.Length], res);\n    return res;\n}\n
      permutations_ii.go
      /* Backtracking algorithm: Permutations II */\nfunc backtrackII(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {\n    // When the state length equals the number of elements, record the solution\n    if len(*state) == len(*choices) {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n    }\n    // Traverse all choices\n    duplicated := make(map[int]struct{}, 0)\n    for i := 0; i < len(*choices); i++ {\n        choice := (*choices)[i]\n        // Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n        if _, ok := duplicated[choice]; !ok && !(*selected)[i] {\n            // Attempt: make choice, update state\n            // Record the selected element value\n            duplicated[choice] = struct{}{}\n            (*selected)[i] = true\n            *state = append(*state, choice)\n            // Proceed to the next round of selection\n            backtrackII(state, choices, selected, res)\n            // Backtrack: undo choice, restore to previous state\n            (*selected)[i] = false\n            *state = (*state)[:len(*state)-1]\n        }\n    }\n}\n\n/* Permutations II */\nfunc permutationsII(nums []int) [][]int {\n    res := make([][]int, 0)\n    state := make([]int, 0)\n    selected := make([]bool, len(nums))\n    backtrackII(&state, &nums, &selected, &res)\n    return res\n}\n
      permutations_ii.swift
      /* Backtracking algorithm: Permutations II */\nfunc backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {\n    // When the state length equals the number of elements, record the solution\n    if state.count == choices.count {\n        res.append(state)\n        return\n    }\n    // Traverse all choices\n    var duplicated: Set<Int> = []\n    for (i, choice) in choices.enumerated() {\n        // Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n        if !selected[i], !duplicated.contains(choice) {\n            // Attempt: make choice, update state\n            duplicated.insert(choice) // Record the selected element value\n            selected[i] = true\n            state.append(choice)\n            // Proceed to the next round of selection\n            backtrack(state: &state, choices: choices, selected: &selected, res: &res)\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false\n            state.removeLast()\n        }\n    }\n}\n\n/* Permutations II */\nfunc permutationsII(nums: [Int]) -> [[Int]] {\n    var state: [Int] = []\n    var selected = Array(repeating: false, count: nums.count)\n    var res: [[Int]] = []\n    backtrack(state: &state, choices: nums, selected: &selected, res: &res)\n    return res\n}\n
      permutations_ii.js
      /* Backtracking algorithm: Permutations II */\nfunction backtrack(state, choices, selected, res) {\n    // When the state length equals the number of elements, record the solution\n    if (state.length === choices.length) {\n        res.push([...state]);\n        return;\n    }\n    // Traverse all choices\n    const duplicated = new Set();\n    choices.forEach((choice, i) => {\n        // Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n        if (!selected[i] && !duplicated.has(choice)) {\n            // Attempt: make choice, update state\n            duplicated.add(choice); // Record the selected element value\n            selected[i] = true;\n            state.push(choice);\n            // Proceed to the next round of selection\n            backtrack(state, choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.pop();\n        }\n    });\n}\n\n/* Permutations II */\nfunction permutationsII(nums) {\n    const res = [];\n    backtrack([], nums, Array(nums.length).fill(false), res);\n    return res;\n}\n
      permutations_ii.ts
      /* Backtracking algorithm: Permutations II */\nfunction backtrack(\n    state: number[],\n    choices: number[],\n    selected: boolean[],\n    res: number[][]\n): void {\n    // When the state length equals the number of elements, record the solution\n    if (state.length === choices.length) {\n        res.push([...state]);\n        return;\n    }\n    // Traverse all choices\n    const duplicated = new Set();\n    choices.forEach((choice, i) => {\n        // Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n        if (!selected[i] && !duplicated.has(choice)) {\n            // Attempt: make choice, update state\n            duplicated.add(choice); // Record the selected element value\n            selected[i] = true;\n            state.push(choice);\n            // Proceed to the next round of selection\n            backtrack(state, choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.pop();\n        }\n    });\n}\n\n/* Permutations II */\nfunction permutationsII(nums: number[]): number[][] {\n    const res: number[][] = [];\n    backtrack([], nums, Array(nums.length).fill(false), res);\n    return res;\n}\n
      permutations_ii.dart
      /* Backtracking algorithm: Permutations II */\nvoid backtrack(\n  List<int> state,\n  List<int> choices,\n  List<bool> selected,\n  List<List<int>> res,\n) {\n  // When the state length equals the number of elements, record the solution\n  if (state.length == choices.length) {\n    res.add(List.from(state));\n    return;\n  }\n  // Traverse all choices\n  Set<int> duplicated = {};\n  for (int i = 0; i < choices.length; i++) {\n    int choice = choices[i];\n    // Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n    if (!selected[i] && !duplicated.contains(choice)) {\n      // Attempt: make choice, update state\n      duplicated.add(choice); // Record the selected element value\n      selected[i] = true;\n      state.add(choice);\n      // Proceed to the next round of selection\n      backtrack(state, choices, selected, res);\n      // Backtrack: undo choice, restore to previous state\n      selected[i] = false;\n      state.removeLast();\n    }\n  }\n}\n\n/* Permutations II */\nList<List<int>> permutationsII(List<int> nums) {\n  List<List<int>> res = [];\n  backtrack([], nums, List.filled(nums.length, false), res);\n  return res;\n}\n
      permutations_ii.rs
      /* Backtracking algorithm: Permutations II */\nfn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {\n    // When the state length equals the number of elements, record the solution\n    if state.len() == choices.len() {\n        res.push(state);\n        return;\n    }\n    // Traverse all choices\n    let mut duplicated = HashSet::<i32>::new();\n    for i in 0..choices.len() {\n        let choice = choices[i];\n        // Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n        if !selected[i] && !duplicated.contains(&choice) {\n            // Attempt: make choice, update state\n            duplicated.insert(choice); // Record the selected element value\n            selected[i] = true;\n            state.push(choice);\n            // Proceed to the next round of selection\n            backtrack(state.clone(), choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.pop();\n        }\n    }\n}\n\n/* Permutations II */\nfn permutations_ii(nums: &mut [i32]) -> Vec<Vec<i32>> {\n    let mut res = Vec::new();\n    backtrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);\n    res\n}\n
      permutations_ii.c
      /* Backtracking algorithm: Permutations II */\nvoid backtrack(int *state, int stateSize, int *choices, int choicesSize, bool *selected, int **res, int *resSize) {\n    // When the state length equals the number of elements, record the solution\n    if (stateSize == choicesSize) {\n        res[*resSize] = (int *)malloc(choicesSize * sizeof(int));\n        for (int i = 0; i < choicesSize; i++) {\n            res[*resSize][i] = state[i];\n        }\n        (*resSize)++;\n        return;\n    }\n    // Traverse all choices\n    bool duplicated[MAX_SIZE] = {false};\n    for (int i = 0; i < choicesSize; i++) {\n        int choice = choices[i];\n        // Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n        if (!selected[i] && !duplicated[choice]) {\n            // Attempt: make choice, update state\n            duplicated[choice] = true; // Record the selected element value\n            selected[i] = true;\n            state[stateSize] = choice;\n            // Proceed to the next round of selection\n            backtrack(state, stateSize + 1, choices, choicesSize, selected, res, resSize);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n        }\n    }\n}\n\n/* Permutations II */\nint **permutationsII(int *nums, int numsSize, int *returnSize) {\n    int *state = (int *)malloc(numsSize * sizeof(int));\n    bool *selected = (bool *)malloc(numsSize * sizeof(bool));\n    for (int i = 0; i < numsSize; i++) {\n        selected[i] = false;\n    }\n    int **res = (int **)malloc(MAX_SIZE * sizeof(int *));\n    *returnSize = 0;\n\n    backtrack(state, 0, nums, numsSize, selected, res, returnSize);\n\n    free(state);\n    free(selected);\n\n    return res;\n}\n
      permutations_ii.kt
      /* Backtracking algorithm: Permutations II */\nfun backtrack(\n    state: MutableList<Int>,\n    choices: IntArray,\n    selected: BooleanArray,\n    res: MutableList<MutableList<Int>?>\n) {\n    // When the state length equals the number of elements, record the solution\n    if (state.size == choices.size) {\n        res.add(state.toMutableList())\n        return\n    }\n    // Traverse all choices\n    val duplicated = HashSet<Int>()\n    for (i in choices.indices) {\n        val choice = choices[i]\n        // Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n        if (!selected[i] && !duplicated.contains(choice)) {\n            // Attempt: make choice, update state\n            duplicated.add(choice) // Record the selected element value\n            selected[i] = true\n            state.add(choice)\n            // Proceed to the next round of selection\n            backtrack(state, choices, selected, res)\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false\n            state.removeAt(state.size - 1)\n        }\n    }\n}\n\n/* Permutations II */\nfun permutationsII(nums: IntArray): MutableList<MutableList<Int>?> {\n    val res = mutableListOf<MutableList<Int>?>()\n    backtrack(mutableListOf(), nums, BooleanArray(nums.size), res)\n    return res\n}\n
      permutations_ii.rb
      ### Backtracking: permutations II ###\ndef backtrack(state, choices, selected, res)\n  # When the state length equals the number of elements, record the solution\n  if state.length == choices.length\n    res << state.dup\n    return\n  end\n\n  # Traverse all choices\n  duplicated = Set.new\n  choices.each_with_index do |choice, i|\n    # Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n    if !selected[i] && !duplicated.include?(choice)\n      # Attempt: make choice, update state\n      duplicated.add(choice)\n      selected[i] = true\n      state << choice\n      # Proceed to the next round of selection\n      backtrack(state, choices, selected, res)\n      # Backtrack: undo choice, restore to previous state\n      selected[i] = false\n      state.pop\n    end\n  end\nend\n\n### Permutations II ###\ndef permutations_ii(nums)\n  res = []\n  backtrack([], nums, Array.new(nums.length, false), res)\n  res\nend\n

      Assuming elements are pairwise distinct, there are \\(n!\\) (factorial) permutations of \\(n\\) elements. When recording results, we need to copy a list of length \\(n\\), using \\(O(n)\\) time. Therefore, the time complexity is \\(O(n! \\cdot n)\\).

      The maximum recursion depth is \\(n\\), using \\(O(n)\\) stack frame space. selected uses \\(O(n)\\) space. At most \\(n\\) duplicated sets exist simultaneously, using \\(O(n^2)\\) space. Therefore, the space complexity is \\(O(n^2)\\).

      "},{"location":"chapter_backtracking/permutations_problem/#3-comparison-of-two-pruning-methods","title":"3. \u00a0 Comparison of Two Pruning Methods","text":"

      Note that although both selected and duplicated are used for pruning, they have different objectives.

      • Pruning duplicate choices: There is only one selected throughout the entire search process. It records which elements are included in the current state, and its purpose is to prevent an element from appearing repeatedly in state.
      • Pruning duplicate elements: Each round of choices (each backtrack function call) contains a duplicated set. It records which elements have been chosen in this round's iteration (the for loop), and its purpose is to ensure that equal elements are chosen only once.

      Figure 13-9 shows the effective scope of the two pruning conditions. Note that each node in the tree represents a choice, and the nodes on the path from the root to a leaf node form a permutation.

      Figure 13-9 \u00a0 Effective scope of two pruning conditions

      "},{"location":"chapter_backtracking/subset_sum_problem/","title":"13.3 \u00a0 Subset-Sum Problem","text":""},{"location":"chapter_backtracking/subset_sum_problem/#1331-without-duplicate-elements","title":"13.3.1 \u00a0 Without Duplicate Elements","text":"

      Question

      Given a positive integer array nums and a target positive integer target, find all possible combinations where the sum of elements in the combination equals target. The given array has no duplicate elements, and each element can be selected multiple times. Return these combinations in list form, where the list should not contain duplicate combinations.

      For example, given the set \\(\\{3, 4, 5\\}\\) and target integer \\(9\\), the solutions are \\(\\{3, 3, 3\\}, \\{4, 5\\}\\). Note the following two points:

      • Elements in the input set can be selected repeatedly without limit.
      • Subsets do not distinguish element order; for example, \\(\\{4, 5\\}\\) and \\(\\{5, 4\\}\\) are the same subset.
      "},{"location":"chapter_backtracking/subset_sum_problem/#1-reference-to-full-permutation-solution","title":"1. \u00a0 Reference to Full Permutation Solution","text":"

      Similar to the full permutation problem, we can imagine the process of generating subsets as a series of choices, and update the \"sum of elements\" in real-time during the selection process. When the sum equals target, we record the subset to the result list.

      Unlike the full permutation problem, elements in this problem's set can be selected unlimited times, so we do not need to use a selected boolean list to track whether an element has been selected. We can make minor modifications to the full permutation code and initially obtain the solution:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby subset_sum_i_naive.py
      def backtrack(\n    state: list[int],\n    target: int,\n    total: int,\n    choices: list[int],\n    res: list[list[int]],\n):\n    \"\"\"Backtracking algorithm: Subset sum I\"\"\"\n    # When the subset sum equals target, record the solution\n    if total == target:\n        res.append(list(state))\n        return\n    # Traverse all choices\n    for i in range(len(choices)):\n        # Pruning: if the subset sum exceeds target, skip this choice\n        if total + choices[i] > target:\n            continue\n        # Attempt: make choice, update element sum total\n        state.append(choices[i])\n        # Proceed to the next round of selection\n        backtrack(state, target, total + choices[i], choices, res)\n        # Backtrack: undo choice, restore to previous state\n        state.pop()\n\ndef subset_sum_i_naive(nums: list[int], target: int) -> list[list[int]]:\n    \"\"\"Solve subset sum I (including duplicate subsets)\"\"\"\n    state = []  # State (subset)\n    total = 0  # Subset sum\n    res = []  # Result list (subset list)\n    backtrack(state, target, total, nums, res)\n    return res\n
      subset_sum_i_naive.cpp
      /* Backtracking algorithm: Subset sum I */\nvoid backtrack(vector<int> &state, int target, int total, vector<int> &choices, vector<vector<int>> &res) {\n    // When the subset sum equals target, record the solution\n    if (total == target) {\n        res.push_back(state);\n        return;\n    }\n    // Traverse all choices\n    for (size_t i = 0; i < choices.size(); i++) {\n        // Pruning: if the subset sum exceeds target, skip this choice\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // Attempt: make choice, update element sum total\n        state.push_back(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target, total + choices[i], choices, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop_back();\n    }\n}\n\n/* Solve subset sum I (including duplicate subsets) */\nvector<vector<int>> subsetSumINaive(vector<int> &nums, int target) {\n    vector<int> state;       // State (subset)\n    int total = 0;           // Subset sum\n    vector<vector<int>> res; // Result list (subset list)\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
      subset_sum_i_naive.java
      /* Backtracking algorithm: Subset sum I */\nvoid backtrack(List<Integer> state, int target, int total, int[] choices, List<List<Integer>> res) {\n    // When the subset sum equals target, record the solution\n    if (total == target) {\n        res.add(new ArrayList<>(state));\n        return;\n    }\n    // Traverse all choices\n    for (int i = 0; i < choices.length; i++) {\n        // Pruning: if the subset sum exceeds target, skip this choice\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // Attempt: make choice, update element sum total\n        state.add(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target, total + choices[i], choices, res);\n        // Backtrack: undo choice, restore to previous state\n        state.remove(state.size() - 1);\n    }\n}\n\n/* Solve subset sum I (including duplicate subsets) */\nList<List<Integer>> subsetSumINaive(int[] nums, int target) {\n    List<Integer> state = new ArrayList<>(); // State (subset)\n    int total = 0; // Subset sum\n    List<List<Integer>> res = new ArrayList<>(); // Result list (subset list)\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
      subset_sum_i_naive.cs
      /* Backtracking algorithm: Subset sum I */\nvoid Backtrack(List<int> state, int target, int total, int[] choices, List<List<int>> res) {\n    // When the subset sum equals target, record the solution\n    if (total == target) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // Traverse all choices\n    for (int i = 0; i < choices.Length; i++) {\n        // Pruning: if the subset sum exceeds target, skip this choice\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // Attempt: make choice, update element sum total\n        state.Add(choices[i]);\n        // Proceed to the next round of selection\n        Backtrack(state, target, total + choices[i], choices, res);\n        // Backtrack: undo choice, restore to previous state\n        state.RemoveAt(state.Count - 1);\n    }\n}\n\n/* Solve subset sum I (including duplicate subsets) */\nList<List<int>> SubsetSumINaive(int[] nums, int target) {\n    List<int> state = []; // State (subset)\n    int total = 0; // Subset sum\n    List<List<int>> res = []; // Result list (subset list)\n    Backtrack(state, target, total, nums, res);\n    return res;\n}\n
      subset_sum_i_naive.go
      /* Backtracking algorithm: Subset sum I */\nfunc backtrackSubsetSumINaive(total, target int, state, choices *[]int, res *[][]int) {\n    // When the subset sum equals target, record the solution\n    if target == total {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n        return\n    }\n    // Traverse all choices\n    for i := 0; i < len(*choices); i++ {\n        // Pruning: if the subset sum exceeds target, skip this choice\n        if total+(*choices)[i] > target {\n            continue\n        }\n        // Attempt: make choice, update element sum total\n        *state = append(*state, (*choices)[i])\n        // Proceed to the next round of selection\n        backtrackSubsetSumINaive(total+(*choices)[i], target, state, choices, res)\n        // Backtrack: undo choice, restore to previous state\n        *state = (*state)[:len(*state)-1]\n    }\n}\n\n/* Solve subset sum I (including duplicate subsets) */\nfunc subsetSumINaive(nums []int, target int) [][]int {\n    state := make([]int, 0) // State (subset)\n    total := 0              // Subset sum\n    res := make([][]int, 0) // Result list (subset list)\n    backtrackSubsetSumINaive(total, target, &state, &nums, &res)\n    return res\n}\n
      subset_sum_i_naive.swift
      /* Backtracking algorithm: Subset sum I */\nfunc backtrack(state: inout [Int], target: Int, total: Int, choices: [Int], res: inout [[Int]]) {\n    // When the subset sum equals target, record the solution\n    if total == target {\n        res.append(state)\n        return\n    }\n    // Traverse all choices\n    for i in choices.indices {\n        // Pruning: if the subset sum exceeds target, skip this choice\n        if total + choices[i] > target {\n            continue\n        }\n        // Attempt: make choice, update element sum total\n        state.append(choices[i])\n        // Proceed to the next round of selection\n        backtrack(state: &state, target: target, total: total + choices[i], choices: choices, res: &res)\n        // Backtrack: undo choice, restore to previous state\n        state.removeLast()\n    }\n}\n\n/* Solve subset sum I (including duplicate subsets) */\nfunc subsetSumINaive(nums: [Int], target: Int) -> [[Int]] {\n    var state: [Int] = [] // State (subset)\n    let total = 0 // Subset sum\n    var res: [[Int]] = [] // Result list (subset list)\n    backtrack(state: &state, target: target, total: total, choices: nums, res: &res)\n    return res\n}\n
      subset_sum_i_naive.js
      /* Backtracking algorithm: Subset sum I */\nfunction backtrack(state, target, total, choices, res) {\n    // When the subset sum equals target, record the solution\n    if (total === target) {\n        res.push([...state]);\n        return;\n    }\n    // Traverse all choices\n    for (let i = 0; i < choices.length; i++) {\n        // Pruning: if the subset sum exceeds target, skip this choice\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // Attempt: make choice, update element sum total\n        state.push(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target, total + choices[i], choices, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop();\n    }\n}\n\n/* Solve subset sum I (including duplicate subsets) */\nfunction subsetSumINaive(nums, target) {\n    const state = []; // State (subset)\n    const total = 0; // Subset sum\n    const res = []; // Result list (subset list)\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
      subset_sum_i_naive.ts
      /* Backtracking algorithm: Subset sum I */\nfunction backtrack(\n    state: number[],\n    target: number,\n    total: number,\n    choices: number[],\n    res: number[][]\n): void {\n    // When the subset sum equals target, record the solution\n    if (total === target) {\n        res.push([...state]);\n        return;\n    }\n    // Traverse all choices\n    for (let i = 0; i < choices.length; i++) {\n        // Pruning: if the subset sum exceeds target, skip this choice\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // Attempt: make choice, update element sum total\n        state.push(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target, total + choices[i], choices, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop();\n    }\n}\n\n/* Solve subset sum I (including duplicate subsets) */\nfunction subsetSumINaive(nums: number[], target: number): number[][] {\n    const state = []; // State (subset)\n    const total = 0; // Subset sum\n    const res = []; // Result list (subset list)\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
      subset_sum_i_naive.dart
      /* Backtracking algorithm: Subset sum I */\nvoid backtrack(\n  List<int> state,\n  int target,\n  int total,\n  List<int> choices,\n  List<List<int>> res,\n) {\n  // When the subset sum equals target, record the solution\n  if (total == target) {\n    res.add(List.from(state));\n    return;\n  }\n  // Traverse all choices\n  for (int i = 0; i < choices.length; i++) {\n    // Pruning: if the subset sum exceeds target, skip this choice\n    if (total + choices[i] > target) {\n      continue;\n    }\n    // Attempt: make choice, update element sum total\n    state.add(choices[i]);\n    // Proceed to the next round of selection\n    backtrack(state, target, total + choices[i], choices, res);\n    // Backtrack: undo choice, restore to previous state\n    state.removeLast();\n  }\n}\n\n/* Solve subset sum I (including duplicate subsets) */\nList<List<int>> subsetSumINaive(List<int> nums, int target) {\n  List<int> state = []; // State (subset)\n  int total = 0; // Sum of elements\n  List<List<int>> res = []; // Result list (subset list)\n  backtrack(state, target, total, nums, res);\n  return res;\n}\n
      subset_sum_i_naive.rs
      /* Backtracking algorithm: Subset sum I */\nfn backtrack(\n    state: &mut Vec<i32>,\n    target: i32,\n    total: i32,\n    choices: &[i32],\n    res: &mut Vec<Vec<i32>>,\n) {\n    // When the subset sum equals target, record the solution\n    if total == target {\n        res.push(state.clone());\n        return;\n    }\n    // Traverse all choices\n    for i in 0..choices.len() {\n        // Pruning: if the subset sum exceeds target, skip this choice\n        if total + choices[i] > target {\n            continue;\n        }\n        // Attempt: make choice, update element sum total\n        state.push(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target, total + choices[i], choices, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop();\n    }\n}\n\n/* Solve subset sum I (including duplicate subsets) */\nfn subset_sum_i_naive(nums: &[i32], target: i32) -> Vec<Vec<i32>> {\n    let mut state = Vec::new(); // State (subset)\n    let total = 0; // Subset sum\n    let mut res = Vec::new(); // Result list (subset list)\n    backtrack(&mut state, target, total, nums, &mut res);\n    res\n}\n
      subset_sum_i_naive.c
      /* Backtracking algorithm: Subset sum I */\nvoid backtrack(int target, int total, int *choices, int choicesSize) {\n    // When the subset sum equals target, record the solution\n    if (total == target) {\n        for (int i = 0; i < stateSize; i++) {\n            res[resSize][i] = state[i];\n        }\n        resColSizes[resSize++] = stateSize;\n        return;\n    }\n    // Traverse all choices\n    for (int i = 0; i < choicesSize; i++) {\n        // Pruning: if the subset sum exceeds target, skip this choice\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // Attempt: make choice, update element sum total\n        state[stateSize++] = choices[i];\n        // Proceed to the next round of selection\n        backtrack(target, total + choices[i], choices, choicesSize);\n        // Backtrack: undo choice, restore to previous state\n        stateSize--;\n    }\n}\n\n/* Solve subset sum I (including duplicate subsets) */\nvoid subsetSumINaive(int *nums, int numsSize, int target) {\n    resSize = 0; // Initialize solution count to 0\n    backtrack(target, 0, nums, numsSize);\n}\n
      subset_sum_i_naive.kt
      /* Backtracking algorithm: Subset sum I */\nfun backtrack(\n    state: MutableList<Int>,\n    target: Int,\n    total: Int,\n    choices: IntArray,\n    res: MutableList<MutableList<Int>?>\n) {\n    // When the subset sum equals target, record the solution\n    if (total == target) {\n        res.add(state.toMutableList())\n        return\n    }\n    // Traverse all choices\n    for (i in choices.indices) {\n        // Pruning: if the subset sum exceeds target, skip this choice\n        if (total + choices[i] > target) {\n            continue\n        }\n        // Attempt: make choice, update element sum total\n        state.add(choices[i])\n        // Proceed to the next round of selection\n        backtrack(state, target, total + choices[i], choices, res)\n        // Backtrack: undo choice, restore to previous state\n        state.removeAt(state.size - 1)\n    }\n}\n\n/* Solve subset sum I (including duplicate subsets) */\nfun subsetSumINaive(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {\n    val state = mutableListOf<Int>() // State (subset)\n    val total = 0 // Subset sum\n    val res = mutableListOf<MutableList<Int>?>() // Result list (subset list)\n    backtrack(state, target, total, nums, res)\n    return res\n}\n
      subset_sum_i_naive.rb
      ### Backtracking: subset sum I ###\ndef backtrack(state, target, total, choices, res)\n  # When the subset sum equals target, record the solution\n  if total == target\n    res << state.dup\n    return\n  end\n\n  # Traverse all choices\n  for i in 0...choices.length\n    # Pruning: if the subset sum exceeds target, skip this choice\n    next if total + choices[i] > target\n    # Attempt: make choice, update element sum total\n    state << choices[i]\n    # Proceed to the next round of selection\n    backtrack(state, target, total + choices[i], choices, res)\n    # Backtrack: undo choice, restore to previous state\n    state.pop\n  end\nend\n\n### Solve subset sum I (with duplicate subsets) ###\ndef subset_sum_i_naive(nums, target)\n  state = [] # State (subset)\n  total = 0 # Subset sum\n  res = [] # Result list (subset list)\n  backtrack(state, target, total, nums, res)\n  res\nend\n

      When we input array \\([3, 4, 5]\\) and target element \\(9\\) to the above code, the output is \\([3, 3, 3], [4, 5], [5, 4]\\). Although we successfully find all subsets that sum to \\(9\\), there are duplicate subsets \\([4, 5]\\) and \\([5, 4]\\).

      This is because the search process distinguishes the order of selections, but subsets do not distinguish selection order. As shown in Figure 13-10, selecting 4 first and then 5 versus selecting 5 first and then 4 are different branches, but they correspond to the same subset.

      Figure 13-10 \u00a0 Subset search and boundary pruning

      To eliminate duplicate subsets, one straightforward idea is to deduplicate the result list. However, this approach is very inefficient for two reasons:

      • When there are many array elements, especially when target is large, the search process generates many duplicate subsets.
      • Comparing subsets (arrays) is very time-consuming, requiring sorting the arrays first, then comparing each element in them.
      "},{"location":"chapter_backtracking/subset_sum_problem/#2-pruning-duplicate-subsets","title":"2. \u00a0 Pruning Duplicate Subsets","text":"

      We consider deduplication through pruning during the search process. Observing Figure 13-11, duplicate subsets occur when array elements are selected in different orders, as in the following cases:

      1. When the first and second rounds select \\(3\\) and \\(4\\) respectively, all subsets containing these two elements are generated, denoted as \\([3, 4, \\dots]\\).
      2. Afterward, when the first round selects \\(4\\), the second round should skip \\(3\\), because the subset \\([4, 3, \\dots]\\) generated by this choice is completely duplicate with the subset generated in step 1.

      In the search process, each level's choices are tried from left to right, so the rightmost branches are pruned more.

      1. The first two rounds select \\(3\\) and \\(5\\), generating subset \\([3, 5, \\dots]\\).
      2. The first two rounds select \\(4\\) and \\(5\\), generating subset \\([4, 5, \\dots]\\).
      3. If the first round selects \\(5\\), the second round should skip \\(3\\) and \\(4\\), because subsets \\([5, 3, \\dots]\\) and \\([5, 4, \\dots]\\) are completely duplicate with the subsets described in steps 1. and 2.

      Figure 13-11 \u00a0 Different selection orders leading to duplicate subsets

      In summary, given an input array \\([x_1, x_2, \\dots, x_n]\\), let the selection sequence in the search process be \\([x_{i_1}, x_{i_2}, \\dots, x_{i_m}]\\). This selection sequence must satisfy \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\); any selection sequence that does not satisfy this condition will cause duplicates and should be pruned.

      "},{"location":"chapter_backtracking/subset_sum_problem/#3-code-implementation","title":"3. \u00a0 Code Implementation","text":"

      To implement this pruning, we initialize a variable start to indicate the starting point of traversal. After making choice \\(x_{i}\\), set the next round to start traversal from index \\(i\\). This ensures that the selection sequence satisfies \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\), guaranteeing subset uniqueness.

      In addition, we have made the following two optimizations to the code:

      • Before starting the search, first sort the array nums. When traversing all choices, end the loop immediately when the subset sum exceeds target, because subsequent elements are larger, and their subset sums must exceed target.
      • Omit the element sum variable total and use subtraction on target to track the sum of elements. Record the solution when target equals \\(0\\).
      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby subset_sum_i.py
      def backtrack(\n    state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n    \"\"\"Backtracking algorithm: Subset sum I\"\"\"\n    # When the subset sum equals target, record the solution\n    if target == 0:\n        res.append(list(state))\n        return\n    # Traverse all choices\n    # Pruning 2: start traversing from start to avoid generating duplicate subsets\n    for i in range(start, len(choices)):\n        # Pruning 1: if the subset sum exceeds target, end the loop directly\n        # This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if target - choices[i] < 0:\n            break\n        # Attempt: make choice, update target, start\n        state.append(choices[i])\n        # Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i, res)\n        # Backtrack: undo choice, restore to previous state\n        state.pop()\n\ndef subset_sum_i(nums: list[int], target: int) -> list[list[int]]:\n    \"\"\"Solve subset sum I\"\"\"\n    state = []  # State (subset)\n    nums.sort()  # Sort nums\n    start = 0  # Start point for traversal\n    res = []  # Result list (subset list)\n    backtrack(state, target, nums, start, res)\n    return res\n
      subset_sum_i.cpp
      /* Backtracking algorithm: Subset sum I */\nvoid backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {\n    // When the subset sum equals target, record the solution\n    if (target == 0) {\n        res.push_back(state);\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    for (int i = start; i < choices.size(); i++) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // Attempt: make choice, update target, start\n        state.push_back(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop_back();\n    }\n}\n\n/* Solve subset sum I */\nvector<vector<int>> subsetSumI(vector<int> &nums, int target) {\n    vector<int> state;              // State (subset)\n    sort(nums.begin(), nums.end()); // Sort nums\n    int start = 0;                  // Start point for traversal\n    vector<vector<int>> res;        // Result list (subset list)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
      subset_sum_i.java
      /* Backtracking algorithm: Subset sum I */\nvoid backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {\n    // When the subset sum equals target, record the solution\n    if (target == 0) {\n        res.add(new ArrayList<>(state));\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    for (int i = start; i < choices.length; i++) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // Attempt: make choice, update target, start\n        state.add(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i, res);\n        // Backtrack: undo choice, restore to previous state\n        state.remove(state.size() - 1);\n    }\n}\n\n/* Solve subset sum I */\nList<List<Integer>> subsetSumI(int[] nums, int target) {\n    List<Integer> state = new ArrayList<>(); // State (subset)\n    Arrays.sort(nums); // Sort nums\n    int start = 0; // Start point for traversal\n    List<List<Integer>> res = new ArrayList<>(); // Result list (subset list)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
      subset_sum_i.cs
      /* Backtracking algorithm: Subset sum I */\nvoid Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {\n    // When the subset sum equals target, record the solution\n    if (target == 0) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    for (int i = start; i < choices.Length; i++) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // Attempt: make choice, update target, start\n        state.Add(choices[i]);\n        // Proceed to the next round of selection\n        Backtrack(state, target - choices[i], choices, i, res);\n        // Backtrack: undo choice, restore to previous state\n        state.RemoveAt(state.Count - 1);\n    }\n}\n\n/* Solve subset sum I */\nList<List<int>> SubsetSumI(int[] nums, int target) {\n    List<int> state = []; // State (subset)\n    Array.Sort(nums); // Sort nums\n    int start = 0; // Start point for traversal\n    List<List<int>> res = []; // Result list (subset list)\n    Backtrack(state, target, nums, start, res);\n    return res;\n}\n
      subset_sum_i.go
      /* Backtracking algorithm: Subset sum I */\nfunc backtrackSubsetSumI(start, target int, state, choices *[]int, res *[][]int) {\n    // When the subset sum equals target, record the solution\n    if target == 0 {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n        return\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    for i := start; i < len(*choices); i++ {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if target-(*choices)[i] < 0 {\n            break\n        }\n        // Attempt: make choice, update target, start\n        *state = append(*state, (*choices)[i])\n        // Proceed to the next round of selection\n        backtrackSubsetSumI(i, target-(*choices)[i], state, choices, res)\n        // Backtrack: undo choice, restore to previous state\n        *state = (*state)[:len(*state)-1]\n    }\n}\n\n/* Solve subset sum I */\nfunc subsetSumI(nums []int, target int) [][]int {\n    state := make([]int, 0) // State (subset)\n    sort.Ints(nums)         // Sort nums\n    start := 0              // Start point for traversal\n    res := make([][]int, 0) // Result list (subset list)\n    backtrackSubsetSumI(start, target, &state, &nums, &res)\n    return res\n}\n
      subset_sum_i.swift
      /* Backtracking algorithm: Subset sum I */\nfunc backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {\n    // When the subset sum equals target, record the solution\n    if target == 0 {\n        res.append(state)\n        return\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    for i in choices.indices.dropFirst(start) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if target - choices[i] < 0 {\n            break\n        }\n        // Attempt: make choice, update target, start\n        state.append(choices[i])\n        // Proceed to the next round of selection\n        backtrack(state: &state, target: target - choices[i], choices: choices, start: i, res: &res)\n        // Backtrack: undo choice, restore to previous state\n        state.removeLast()\n    }\n}\n\n/* Solve subset sum I */\nfunc subsetSumI(nums: [Int], target: Int) -> [[Int]] {\n    var state: [Int] = [] // State (subset)\n    let nums = nums.sorted() // Sort nums\n    let start = 0 // Start point for traversal\n    var res: [[Int]] = [] // Result list (subset list)\n    backtrack(state: &state, target: target, choices: nums, start: start, res: &res)\n    return res\n}\n
      subset_sum_i.js
      /* Backtracking algorithm: Subset sum I */\nfunction backtrack(state, target, choices, start, res) {\n    // When the subset sum equals target, record the solution\n    if (target === 0) {\n        res.push([...state]);\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    for (let i = start; i < choices.length; i++) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // Attempt: make choice, update target, start\n        state.push(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop();\n    }\n}\n\n/* Solve subset sum I */\nfunction subsetSumI(nums, target) {\n    const state = []; // State (subset)\n    nums.sort((a, b) => a - b); // Sort nums\n    const start = 0; // Start point for traversal\n    const res = []; // Result list (subset list)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
      subset_sum_i.ts
      /* Backtracking algorithm: Subset sum I */\nfunction backtrack(\n    state: number[],\n    target: number,\n    choices: number[],\n    start: number,\n    res: number[][]\n): void {\n    // When the subset sum equals target, record the solution\n    if (target === 0) {\n        res.push([...state]);\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    for (let i = start; i < choices.length; i++) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // Attempt: make choice, update target, start\n        state.push(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop();\n    }\n}\n\n/* Solve subset sum I */\nfunction subsetSumI(nums: number[], target: number): number[][] {\n    const state = []; // State (subset)\n    nums.sort((a, b) => a - b); // Sort nums\n    const start = 0; // Start point for traversal\n    const res = []; // Result list (subset list)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
      subset_sum_i.dart
      /* Backtracking algorithm: Subset sum I */\nvoid backtrack(\n  List<int> state,\n  int target,\n  List<int> choices,\n  int start,\n  List<List<int>> res,\n) {\n  // When the subset sum equals target, record the solution\n  if (target == 0) {\n    res.add(List.from(state));\n    return;\n  }\n  // Traverse all choices\n  // Pruning 2: start traversing from start to avoid generating duplicate subsets\n  for (int i = start; i < choices.length; i++) {\n    // Pruning 1: if the subset sum exceeds target, end the loop directly\n    // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n    if (target - choices[i] < 0) {\n      break;\n    }\n    // Attempt: make choice, update target, start\n    state.add(choices[i]);\n    // Proceed to the next round of selection\n    backtrack(state, target - choices[i], choices, i, res);\n    // Backtrack: undo choice, restore to previous state\n    state.removeLast();\n  }\n}\n\n/* Solve subset sum I */\nList<List<int>> subsetSumI(List<int> nums, int target) {\n  List<int> state = []; // State (subset)\n  nums.sort(); // Sort nums\n  int start = 0; // Start point for traversal\n  List<List<int>> res = []; // Result list (subset list)\n  backtrack(state, target, nums, start, res);\n  return res;\n}\n
      subset_sum_i.rs
      /* Backtracking algorithm: Subset sum I */\nfn backtrack(\n    state: &mut Vec<i32>,\n    target: i32,\n    choices: &[i32],\n    start: usize,\n    res: &mut Vec<Vec<i32>>,\n) {\n    // When the subset sum equals target, record the solution\n    if target == 0 {\n        res.push(state.clone());\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    for i in start..choices.len() {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if target - choices[i] < 0 {\n            break;\n        }\n        // Attempt: make choice, update target, start\n        state.push(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop();\n    }\n}\n\n/* Solve subset sum I */\nfn subset_sum_i(nums: &mut [i32], target: i32) -> Vec<Vec<i32>> {\n    let mut state = Vec::new(); // State (subset)\n    nums.sort(); // Sort nums\n    let start = 0; // Start point for traversal\n    let mut res = Vec::new(); // Result list (subset list)\n    backtrack(&mut state, target, nums, start, &mut res);\n    res\n}\n
      subset_sum_i.c
      /* Backtracking algorithm: Subset sum I */\nvoid backtrack(int target, int *choices, int choicesSize, int start) {\n    // When the subset sum equals target, record the solution\n    if (target == 0) {\n        for (int i = 0; i < stateSize; ++i) {\n            res[resSize][i] = state[i];\n        }\n        resColSizes[resSize++] = stateSize;\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    for (int i = start; i < choicesSize; i++) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // Attempt: make choice, update target, start\n        state[stateSize] = choices[i];\n        stateSize++;\n        // Proceed to the next round of selection\n        backtrack(target - choices[i], choices, choicesSize, i);\n        // Backtrack: undo choice, restore to previous state\n        stateSize--;\n    }\n}\n\n/* Solve subset sum I */\nvoid subsetSumI(int *nums, int numsSize, int target) {\n    qsort(nums, numsSize, sizeof(int), cmp); // Sort nums\n    int start = 0;                           // Start point for traversal\n    backtrack(target, nums, numsSize, start);\n}\n
      subset_sum_i.kt
      /* Backtracking algorithm: Subset sum I */\nfun backtrack(\n    state: MutableList<Int>,\n    target: Int,\n    choices: IntArray,\n    start: Int,\n    res: MutableList<MutableList<Int>?>\n) {\n    // When the subset sum equals target, record the solution\n    if (target == 0) {\n        res.add(state.toMutableList())\n        return\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    for (i in start..<choices.size) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break\n        }\n        // Attempt: make choice, update target, start\n        state.add(choices[i])\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i, res)\n        // Backtrack: undo choice, restore to previous state\n        state.removeAt(state.size - 1)\n    }\n}\n\n/* Solve subset sum I */\nfun subsetSumI(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {\n    val state = mutableListOf<Int>() // State (subset)\n    nums.sort() // Sort nums\n    val start = 0 // Start point for traversal\n    val res = mutableListOf<MutableList<Int>?>() // Result list (subset list)\n    backtrack(state, target, nums, start, res)\n    return res\n}\n
      subset_sum_i.rb
      ### Backtracking: subset sum I ###\ndef backtrack(state, target, choices, start, res)\n  # When the subset sum equals target, record the solution\n  if target.zero?\n    res << state.dup\n    return\n  end\n  # Traverse all choices\n  # Pruning 2: start traversing from start to avoid generating duplicate subsets\n  for i in start...choices.length\n    # Pruning 1: if the subset sum exceeds target, end the loop directly\n    # This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n    break if target - choices[i] < 0\n    # Attempt: make choice, update target, start\n    state << choices[i]\n    # Proceed to the next round of selection\n    backtrack(state, target - choices[i], choices, i, res)\n    # Backtrack: undo choice, restore to previous state\n    state.pop\n  end\nend\n\n### Solve subset sum I ###\ndef subset_sum_i(nums, target)\n  state = [] # State (subset)\n  nums.sort! # Sort nums\n  start = 0 # Start point for traversal\n  res = [] # Result list (subset list)\n  backtrack(state, target, nums, start, res)\n  res\nend\n

      Figure 13-12 shows the complete backtracking process when array \\([3, 4, 5]\\) and target element \\(9\\) are input to the above code.

      Figure 13-12 \u00a0 Subset-sum I backtracking process

      "},{"location":"chapter_backtracking/subset_sum_problem/#1332-with-duplicate-elements-in-array","title":"13.3.2 \u00a0 With Duplicate Elements in Array","text":"

      Question

      Given a positive integer array nums and a target positive integer target, find all possible combinations where the sum of elements in the combination equals target. The given array may contain duplicate elements, and each element can be selected at most once. Return these combinations in list form, where the list should not contain duplicate combinations.

      Compared to the previous problem, the input array in this problem may contain duplicate elements, which introduces new challenges. For example, given array \\([4, \\hat{4}, 5]\\) and target element \\(9\\), the output of the existing code is \\([4, 5], [\\hat{4}, 5]\\), which contains duplicate subsets.

      The reason for this duplication is that equal elements are selected multiple times in a certain round. In Figure 13-13, the first round has three choices, two of which are \\(4\\), creating two duplicate search branches that output duplicate subsets. Similarly, the two \\(4\\)'s in the second round also produce duplicate subsets.

      Figure 13-13 \u00a0 Duplicate subsets caused by equal elements

      "},{"location":"chapter_backtracking/subset_sum_problem/#1-pruning-equal-elements","title":"1. \u00a0 Pruning Equal Elements","text":"

      To solve this problem, we need to limit equal elements to be selected only once in each round. The implementation is quite clever: since the array is already sorted, equal elements are adjacent. This means that in a certain round of selection, if the current element equals the element to its left, it means this element has already been selected, so we skip the current element directly.

      At the same time, this problem specifies that each array element can only be selected once. Fortunately, we can also use the variable start to satisfy this constraint: after making choice \\(x_{i}\\), set the next round to start traversal from index \\(i + 1\\) onwards. This both eliminates duplicate subsets and avoids selecting elements multiple times.

      "},{"location":"chapter_backtracking/subset_sum_problem/#2-code-implementation","title":"2. \u00a0 Code Implementation","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby subset_sum_ii.py
      def backtrack(\n    state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n    \"\"\"Backtracking algorithm: Subset sum II\"\"\"\n    # When the subset sum equals target, record the solution\n    if target == 0:\n        res.append(list(state))\n        return\n    # Traverse all choices\n    # Pruning 2: start traversing from start to avoid generating duplicate subsets\n    # Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n    for i in range(start, len(choices)):\n        # Pruning 1: if the subset sum exceeds target, end the loop directly\n        # This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if target - choices[i] < 0:\n            break\n        # Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n        if i > start and choices[i] == choices[i - 1]:\n            continue\n        # Attempt: make choice, update target, start\n        state.append(choices[i])\n        # Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i + 1, res)\n        # Backtrack: undo choice, restore to previous state\n        state.pop()\n\ndef subset_sum_ii(nums: list[int], target: int) -> list[list[int]]:\n    \"\"\"Solve subset sum II\"\"\"\n    state = []  # State (subset)\n    nums.sort()  # Sort nums\n    start = 0  # Start point for traversal\n    res = []  # Result list (subset list)\n    backtrack(state, target, nums, start, res)\n    return res\n
      subset_sum_ii.cpp
      /* Backtracking algorithm: Subset sum II */\nvoid backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {\n    // When the subset sum equals target, record the solution\n    if (target == 0) {\n        res.push_back(state);\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    // Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n    for (int i = start; i < choices.size(); i++) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // Attempt: make choice, update target, start\n        state.push_back(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop_back();\n    }\n}\n\n/* Solve subset sum II */\nvector<vector<int>> subsetSumII(vector<int> &nums, int target) {\n    vector<int> state;              // State (subset)\n    sort(nums.begin(), nums.end()); // Sort nums\n    int start = 0;                  // Start point for traversal\n    vector<vector<int>> res;        // Result list (subset list)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
      subset_sum_ii.java
      /* Backtracking algorithm: Subset sum II */\nvoid backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {\n    // When the subset sum equals target, record the solution\n    if (target == 0) {\n        res.add(new ArrayList<>(state));\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    // Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n    for (int i = start; i < choices.length; i++) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // Attempt: make choice, update target, start\n        state.add(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // Backtrack: undo choice, restore to previous state\n        state.remove(state.size() - 1);\n    }\n}\n\n/* Solve subset sum II */\nList<List<Integer>> subsetSumII(int[] nums, int target) {\n    List<Integer> state = new ArrayList<>(); // State (subset)\n    Arrays.sort(nums); // Sort nums\n    int start = 0; // Start point for traversal\n    List<List<Integer>> res = new ArrayList<>(); // Result list (subset list)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
      subset_sum_ii.cs
      /* Backtracking algorithm: Subset sum II */\nvoid Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {\n    // When the subset sum equals target, record the solution\n    if (target == 0) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    // Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n    for (int i = start; i < choices.Length; i++) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // Attempt: make choice, update target, start\n        state.Add(choices[i]);\n        // Proceed to the next round of selection\n        Backtrack(state, target - choices[i], choices, i + 1, res);\n        // Backtrack: undo choice, restore to previous state\n        state.RemoveAt(state.Count - 1);\n    }\n}\n\n/* Solve subset sum II */\nList<List<int>> SubsetSumII(int[] nums, int target) {\n    List<int> state = []; // State (subset)\n    Array.Sort(nums); // Sort nums\n    int start = 0; // Start point for traversal\n    List<List<int>> res = []; // Result list (subset list)\n    Backtrack(state, target, nums, start, res);\n    return res;\n}\n
      subset_sum_ii.go
      /* Backtracking algorithm: Subset sum II */\nfunc backtrackSubsetSumII(start, target int, state, choices *[]int, res *[][]int) {\n    // When the subset sum equals target, record the solution\n    if target == 0 {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n        return\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    // Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n    for i := start; i < len(*choices); i++ {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if target-(*choices)[i] < 0 {\n            break\n        }\n        // Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n        if i > start && (*choices)[i] == (*choices)[i-1] {\n            continue\n        }\n        // Attempt: make choice, update target, start\n        *state = append(*state, (*choices)[i])\n        // Proceed to the next round of selection\n        backtrackSubsetSumII(i+1, target-(*choices)[i], state, choices, res)\n        // Backtrack: undo choice, restore to previous state\n        *state = (*state)[:len(*state)-1]\n    }\n}\n\n/* Solve subset sum II */\nfunc subsetSumII(nums []int, target int) [][]int {\n    state := make([]int, 0) // State (subset)\n    sort.Ints(nums)         // Sort nums\n    start := 0              // Start point for traversal\n    res := make([][]int, 0) // Result list (subset list)\n    backtrackSubsetSumII(start, target, &state, &nums, &res)\n    return res\n}\n
      subset_sum_ii.swift
      /* Backtracking algorithm: Subset sum II */\nfunc backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {\n    // When the subset sum equals target, record the solution\n    if target == 0 {\n        res.append(state)\n        return\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    // Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n    for i in choices.indices.dropFirst(start) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if target - choices[i] < 0 {\n            break\n        }\n        // Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n        if i > start, choices[i] == choices[i - 1] {\n            continue\n        }\n        // Attempt: make choice, update target, start\n        state.append(choices[i])\n        // Proceed to the next round of selection\n        backtrack(state: &state, target: target - choices[i], choices: choices, start: i + 1, res: &res)\n        // Backtrack: undo choice, restore to previous state\n        state.removeLast()\n    }\n}\n\n/* Solve subset sum II */\nfunc subsetSumII(nums: [Int], target: Int) -> [[Int]] {\n    var state: [Int] = [] // State (subset)\n    let nums = nums.sorted() // Sort nums\n    let start = 0 // Start point for traversal\n    var res: [[Int]] = [] // Result list (subset list)\n    backtrack(state: &state, target: target, choices: nums, start: start, res: &res)\n    return res\n}\n
      subset_sum_ii.js
      /* Backtracking algorithm: Subset sum II */\nfunction backtrack(state, target, choices, start, res) {\n    // When the subset sum equals target, record the solution\n    if (target === 0) {\n        res.push([...state]);\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    // Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n    for (let i = start; i < choices.length; i++) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n        if (i > start && choices[i] === choices[i - 1]) {\n            continue;\n        }\n        // Attempt: make choice, update target, start\n        state.push(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop();\n    }\n}\n\n/* Solve subset sum II */\nfunction subsetSumII(nums, target) {\n    const state = []; // State (subset)\n    nums.sort((a, b) => a - b); // Sort nums\n    const start = 0; // Start point for traversal\n    const res = []; // Result list (subset list)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
      subset_sum_ii.ts
      /* Backtracking algorithm: Subset sum II */\nfunction backtrack(\n    state: number[],\n    target: number,\n    choices: number[],\n    start: number,\n    res: number[][]\n): void {\n    // When the subset sum equals target, record the solution\n    if (target === 0) {\n        res.push([...state]);\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    // Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n    for (let i = start; i < choices.length; i++) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n        if (i > start && choices[i] === choices[i - 1]) {\n            continue;\n        }\n        // Attempt: make choice, update target, start\n        state.push(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop();\n    }\n}\n\n/* Solve subset sum II */\nfunction subsetSumII(nums: number[], target: number): number[][] {\n    const state = []; // State (subset)\n    nums.sort((a, b) => a - b); // Sort nums\n    const start = 0; // Start point for traversal\n    const res = []; // Result list (subset list)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
      subset_sum_ii.dart
      /* Backtracking algorithm: Subset sum II */\nvoid backtrack(\n  List<int> state,\n  int target,\n  List<int> choices,\n  int start,\n  List<List<int>> res,\n) {\n  // When the subset sum equals target, record the solution\n  if (target == 0) {\n    res.add(List.from(state));\n    return;\n  }\n  // Traverse all choices\n  // Pruning 2: start traversing from start to avoid generating duplicate subsets\n  // Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n  for (int i = start; i < choices.length; i++) {\n    // Pruning 1: if the subset sum exceeds target, end the loop directly\n    // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n    if (target - choices[i] < 0) {\n      break;\n    }\n    // Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n    if (i > start && choices[i] == choices[i - 1]) {\n      continue;\n    }\n    // Attempt: make choice, update target, start\n    state.add(choices[i]);\n    // Proceed to the next round of selection\n    backtrack(state, target - choices[i], choices, i + 1, res);\n    // Backtrack: undo choice, restore to previous state\n    state.removeLast();\n  }\n}\n\n/* Solve subset sum II */\nList<List<int>> subsetSumII(List<int> nums, int target) {\n  List<int> state = []; // State (subset)\n  nums.sort(); // Sort nums\n  int start = 0; // Start point for traversal\n  List<List<int>> res = []; // Result list (subset list)\n  backtrack(state, target, nums, start, res);\n  return res;\n}\n
      subset_sum_ii.rs
      /* Backtracking algorithm: Subset sum II */\nfn backtrack(\n    state: &mut Vec<i32>,\n    target: i32,\n    choices: &[i32],\n    start: usize,\n    res: &mut Vec<Vec<i32>>,\n) {\n    // When the subset sum equals target, record the solution\n    if target == 0 {\n        res.push(state.clone());\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    // Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n    for i in start..choices.len() {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if target - choices[i] < 0 {\n            break;\n        }\n        // Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n        if i > start && choices[i] == choices[i - 1] {\n            continue;\n        }\n        // Attempt: make choice, update target, start\n        state.push(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop();\n    }\n}\n\n/* Solve subset sum II */\nfn subset_sum_ii(nums: &mut [i32], target: i32) -> Vec<Vec<i32>> {\n    let mut state = Vec::new(); // State (subset)\n    nums.sort(); // Sort nums\n    let start = 0; // Start point for traversal\n    let mut res = Vec::new(); // Result list (subset list)\n    backtrack(&mut state, target, nums, start, &mut res);\n    res\n}\n
      subset_sum_ii.c
      /* Backtracking algorithm: Subset sum II */\nvoid backtrack(int target, int *choices, int choicesSize, int start) {\n    // When the subset sum equals target, record the solution\n    if (target == 0) {\n        for (int i = 0; i < stateSize; i++) {\n            res[resSize][i] = state[i];\n        }\n        resColSizes[resSize++] = stateSize;\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    // Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n    for (int i = start; i < choicesSize; i++) {\n        // Pruning 1: Skip if subset sum exceeds target\n        if (target - choices[i] < 0) {\n            continue;\n        }\n        // Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // Attempt: make choice, update target, start\n        state[stateSize] = choices[i];\n        stateSize++;\n        // Proceed to the next round of selection\n        backtrack(target - choices[i], choices, choicesSize, i + 1);\n        // Backtrack: undo choice, restore to previous state\n        stateSize--;\n    }\n}\n\n/* Solve subset sum II */\nvoid subsetSumII(int *nums, int numsSize, int target) {\n    // Sort nums\n    qsort(nums, numsSize, sizeof(int), cmp);\n    // Start backtracking\n    backtrack(target, nums, numsSize, 0);\n}\n
      subset_sum_ii.kt
      /* Backtracking algorithm: Subset sum II */\nfun backtrack(\n    state: MutableList<Int>,\n    target: Int,\n    choices: IntArray,\n    start: Int,\n    res: MutableList<MutableList<Int>?>\n) {\n    // When the subset sum equals target, record the solution\n    if (target == 0) {\n        res.add(state.toMutableList())\n        return\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    // Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n    for (i in start..<choices.size) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break\n        }\n        // Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue\n        }\n        // Attempt: make choice, update target, start\n        state.add(choices[i])\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i + 1, res)\n        // Backtrack: undo choice, restore to previous state\n        state.removeAt(state.size - 1)\n    }\n}\n\n/* Solve subset sum II */\nfun subsetSumII(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {\n    val state = mutableListOf<Int>() // State (subset)\n    nums.sort() // Sort nums\n    val start = 0 // Start point for traversal\n    val res = mutableListOf<MutableList<Int>?>() // Result list (subset list)\n    backtrack(state, target, nums, start, res)\n    return res\n}\n
      subset_sum_ii.rb
      ### Backtracking: subset sum II ###\ndef backtrack(state, target, choices, start, res)\n  # When the subset sum equals target, record the solution\n  if target.zero?\n    res << state.dup\n    return\n  end\n\n  # Traverse all choices\n  # Pruning 2: start traversing from start to avoid generating duplicate subsets\n  # Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n  for i in start...choices.length\n    # Pruning 1: if the subset sum exceeds target, end the loop directly\n    # This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n    break if target - choices[i] < 0\n    # Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n    next if i > start && choices[i] == choices[i - 1]\n    # Attempt: make choice, update target, start\n    state << choices[i]\n    # Proceed to the next round of selection\n    backtrack(state, target - choices[i], choices, i + 1, res)\n    # Backtrack: undo choice, restore to previous state\n    state.pop\n  end\nend\n\n### Solve subset sum II ###\ndef subset_sum_ii(nums, target)\n  state = [] # State (subset)\n  nums.sort! # Sort nums\n  start = 0 # Start point for traversal\n  res = [] # Result list (subset list)\n  backtrack(state, target, nums, start, res)\n  res\nend\n

      Figure 13-14 shows the backtracking process for array \\([4, 4, 5]\\) and target element \\(9\\), which includes four types of pruning operations. Combine the illustration with the code comments to understand the entire search process and how each pruning operation works.

      Figure 13-14 \u00a0 Subset-sum II backtracking process

      "},{"location":"chapter_backtracking/summary/","title":"13.5 \u00a0 Summary","text":""},{"location":"chapter_backtracking/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • The backtracking algorithm is fundamentally an exhaustive search method. It finds solutions that meet specified conditions by performing a depth-first traversal of the solution space. During the search process, when a solution satisfying the conditions is found, it is recorded. The search ends either after finding all solutions or when the traversal is complete.
      • The backtracking algorithm search process consists of two parts: attempting and backtracking. It tries various choices through depth-first search. When encountering situations that violate constraints, it reverts the previous choice, returns to the previous state, and continues exploring other options. Attempting and backtracking are operations in opposite directions.
      • Backtracking problems typically contain multiple constraints, which can be utilized to implement pruning operations. Pruning can terminate unnecessary search branches early, significantly improving search efficiency.
      • The backtracking algorithm is primarily used to solve search problems and constraint satisfaction problems. While combinatorial optimization problems can be solved with backtracking, there are often more efficient or better-performing solutions available.
      • The permutation problem aims to find all possible permutations of elements in a given set. We use an array to record whether each element has been selected, thereby pruning search branches that attempt to select the same element repeatedly, ensuring each element is selected exactly once.
      • In the permutation problem, if the set contains duplicate elements, the final result will contain duplicate permutations. We need to impose a constraint so that equal elements can only be selected once per round, which is typically achieved using a hash set.
      • The subset-sum problem aims to find all subsets of a given set that sum to a target value. Since the set is unordered but the search process outputs results in all orders, duplicate subsets are generated. We sort the data before backtracking and use a variable to indicate the starting point of each round's traversal, thereby pruning search branches that generate duplicate subsets.
      • For the subset-sum problem, equal elements in the array produce duplicate sets. We leverage the precondition that the array is sorted by checking whether adjacent elements are equal to implement pruning, ensuring that equal elements can only be selected once per round.
      • The \\(n\\) queens problem aims to find placements of \\(n\\) queens on an \\(n \\times n\\) chessboard such that no two queens can attack each other. The constraints of this problem include row constraints, column constraints, and main and anti-diagonal constraints. To satisfy row constraints, we adopt a row-by-row placement strategy, ensuring exactly one queen is placed in each row.
      • The handling of column constraints and diagonal constraints is similar. For column constraints, we use an array to record whether each column has a queen, thereby indicating whether a selected cell is valid. For diagonal constraints, we use two arrays to separately record whether queens exist on each main or anti-diagonal. The challenge lies in finding the row-column index pattern that characterizes cells on the same main (anti-)diagonal.
      "},{"location":"chapter_backtracking/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

      Q: How should we understand the relationship between backtracking and recursion?

      Overall, backtracking is an \"algorithm strategy\", while recursion is more like a \"tool\".

      • The backtracking algorithm is typically implemented based on recursion. However, backtracking is one application scenario of recursion and represents the application of recursion in search problems.
      • The structure of recursion embodies the \"subproblem decomposition\" problem-solving paradigm, commonly used to solve problems involving divide-and-conquer, backtracking, and dynamic programming (memoized recursion).
      "},{"location":"chapter_computational_complexity/","title":"Chapter 2. \u00a0 Complexity Analysis","text":"

      Abstract

      Complexity analysis is like a space-time guide in the vast universe of algorithms.

      It leads us to explore deeply within the two dimensions of time and space, seeking more elegant solutions.

      "},{"location":"chapter_computational_complexity/#chapter-contents","title":"Chapter contents","text":"
      • 2.1 \u00a0 Algorithm Efficiency Evaluation
      • 2.2 \u00a0 Iteration and Recursion
      • 2.3 \u00a0 Time Complexity
      • 2.4 \u00a0 Space Complexity
      • 2.5 \u00a0 Summary
      "},{"location":"chapter_computational_complexity/iteration_and_recursion/","title":"2.2 \u00a0 Iteration and Recursion","text":"

      In algorithms, repeatedly executing a task is very common and closely related to complexity analysis. Therefore, before introducing time complexity and space complexity, let's first understand how to implement repeated task execution in programs, namely the two basic program control structures: iteration and recursion.

      "},{"location":"chapter_computational_complexity/iteration_and_recursion/#221-iteration","title":"2.2.1 \u00a0 Iteration","text":"

      Iteration is a control structure for repeatedly executing a task. In iteration, a program repeatedly executes a segment of code under certain conditions until those conditions are no longer satisfied.

      "},{"location":"chapter_computational_complexity/iteration_and_recursion/#1-for-loop","title":"1. \u00a0 For Loop","text":"

      The for loop is one of the most common forms of iteration, suitable for use when the number of iterations is known in advance.

      The following function implements the summation \\(1 + 2 + \\dots + n\\) based on a for loop, with the sum result recorded using the variable res. Note that in Python, range(a, b) corresponds to a \"left-closed, right-open\" interval, with the traversal range being \\(a, a + 1, \\dots, b-1\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
      def for_loop(n: int) -> int:\n    \"\"\"for loop\"\"\"\n    res = 0\n    # Sum 1, 2, ..., n-1, n\n    for i in range(1, n + 1):\n        res += i\n    return res\n
      iteration.cpp
      /* for loop */\nint forLoop(int n) {\n    int res = 0;\n    // Sum 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; ++i) {\n        res += i;\n    }\n    return res;\n}\n
      iteration.java
      /* for loop */\nint forLoop(int n) {\n    int res = 0;\n    // Sum 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
      iteration.cs
      /* for loop */\nint ForLoop(int n) {\n    int res = 0;\n    // Sum 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
      iteration.go
      /* for loop */\nfunc forLoop(n int) int {\n    res := 0\n    // Sum 1, 2, ..., n-1, n\n    for i := 1; i <= n; i++ {\n        res += i\n    }\n    return res\n}\n
      iteration.swift
      /* for loop */\nfunc forLoop(n: Int) -> Int {\n    var res = 0\n    // Sum 1, 2, ..., n-1, n\n    for i in 1 ... n {\n        res += i\n    }\n    return res\n}\n
      iteration.js
      /* for loop */\nfunction forLoop(n) {\n    let res = 0;\n    // Sum 1, 2, ..., n-1, n\n    for (let i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
      iteration.ts
      /* for loop */\nfunction forLoop(n: number): number {\n    let res = 0;\n    // Sum 1, 2, ..., n-1, n\n    for (let i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
      iteration.dart
      /* for loop */\nint forLoop(int n) {\n  int res = 0;\n  // Sum 1, 2, ..., n-1, n\n  for (int i = 1; i <= n; i++) {\n    res += i;\n  }\n  return res;\n}\n
      iteration.rs
      /* for loop */\nfn for_loop(n: i32) -> i32 {\n    let mut res = 0;\n    // Sum 1, 2, ..., n-1, n\n    for i in 1..=n {\n        res += i;\n    }\n    res\n}\n
      iteration.c
      /* for loop */\nint forLoop(int n) {\n    int res = 0;\n    // Sum 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
      iteration.kt
      /* for loop */\nfun forLoop(n: Int): Int {\n    var res = 0\n    // Sum 1, 2, ..., n-1, n\n    for (i in 1..n) {\n        res += i\n    }\n    return res\n}\n
      iteration.rb
      ### for loop ###\ndef for_loop(n)\n  res = 0\n\n  # Sum 1, 2, ..., n-1, n\n  for i in 1..n\n    res += i\n  end\n\n  res\nend\n

      Figure 2-1 shows the flowchart of this summation function.

      Figure 2-1 \u00a0 Flowchart of the summation function

      The number of operations in this summation function is proportional to the input data size \\(n\\), or has a \"linear relationship\". In fact, time complexity describes precisely this \"linear relationship\". Related content will be introduced in detail in the next section.

      "},{"location":"chapter_computational_complexity/iteration_and_recursion/#2-while-loop","title":"2. \u00a0 While Loop","text":"

      Similar to the for loop, the while loop is also a method for implementing iteration. In a while loop, the program first checks the condition in each round; if the condition is true, it continues execution, otherwise it ends the loop.

      Below we use a while loop to implement the summation \\(1 + 2 + \\dots + n\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
      def while_loop(n: int) -> int:\n    \"\"\"while loop\"\"\"\n    res = 0\n    i = 1  # Initialize condition variable\n    # Sum 1, 2, ..., n-1, n\n    while i <= n:\n        res += i\n        i += 1  # Update condition variable\n    return res\n
      iteration.cpp
      /* while loop */\nint whileLoop(int n) {\n    int res = 0;\n    int i = 1; // Initialize condition variable\n    // Sum 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // Update condition variable\n    }\n    return res;\n}\n
      iteration.java
      /* while loop */\nint whileLoop(int n) {\n    int res = 0;\n    int i = 1; // Initialize condition variable\n    // Sum 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // Update condition variable\n    }\n    return res;\n}\n
      iteration.cs
      /* while loop */\nint WhileLoop(int n) {\n    int res = 0;\n    int i = 1; // Initialize condition variable\n    // Sum 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i += 1; // Update condition variable\n    }\n    return res;\n}\n
      iteration.go
      /* while loop */\nfunc whileLoop(n int) int {\n    res := 0\n    // Initialize condition variable\n    i := 1\n    // Sum 1, 2, ..., n-1, n\n    for i <= n {\n        res += i\n        // Update condition variable\n        i++\n    }\n    return res\n}\n
      iteration.swift
      /* while loop */\nfunc whileLoop(n: Int) -> Int {\n    var res = 0\n    var i = 1 // Initialize condition variable\n    // Sum 1, 2, ..., n-1, n\n    while i <= n {\n        res += i\n        i += 1 // Update condition variable\n    }\n    return res\n}\n
      iteration.js
      /* while loop */\nfunction whileLoop(n) {\n    let res = 0;\n    let i = 1; // Initialize condition variable\n    // Sum 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // Update condition variable\n    }\n    return res;\n}\n
      iteration.ts
      /* while loop */\nfunction whileLoop(n: number): number {\n    let res = 0;\n    let i = 1; // Initialize condition variable\n    // Sum 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // Update condition variable\n    }\n    return res;\n}\n
      iteration.dart
      /* while loop */\nint whileLoop(int n) {\n  int res = 0;\n  int i = 1; // Initialize condition variable\n  // Sum 1, 2, ..., n-1, n\n  while (i <= n) {\n    res += i;\n    i++; // Update condition variable\n  }\n  return res;\n}\n
      iteration.rs
      /* while loop */\nfn while_loop(n: i32) -> i32 {\n    let mut res = 0;\n    let mut i = 1; // Initialize condition variable\n\n    // Sum 1, 2, ..., n-1, n\n    while i <= n {\n        res += i;\n        i += 1; // Update condition variable\n    }\n    res\n}\n
      iteration.c
      /* while loop */\nint whileLoop(int n) {\n    int res = 0;\n    int i = 1; // Initialize condition variable\n    // Sum 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // Update condition variable\n    }\n    return res;\n}\n
      iteration.kt
      /* while loop */\nfun whileLoop(n: Int): Int {\n    var res = 0\n    var i = 1 // Initialize condition variable\n    // Sum 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i\n        i++ // Update condition variable\n    }\n    return res\n}\n
      iteration.rb
      ### while loop ###\ndef while_loop(n)\n  res = 0\n  i = 1 # Initialize condition variable\n\n  # Sum 1, 2, ..., n-1, n\n  while i <= n\n    res += i\n    i += 1 # Update condition variable\n  end\n\n  res\nend\n

      The while loop has greater flexibility than the for loop. In a while loop, we can freely design the initialization and update steps of the condition variable.

      For example, in the following code, the condition variable \\(i\\) is updated twice per round, which is not convenient to implement using a for loop:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
      def while_loop_ii(n: int) -> int:\n    \"\"\"while loop (two updates)\"\"\"\n    res = 0\n    i = 1  # Initialize condition variable\n    # Sum 1, 4, 10, ...\n    while i <= n:\n        res += i\n        # Update condition variable\n        i += 1\n        i *= 2\n    return res\n
      iteration.cpp
      /* while loop (two updates) */\nint whileLoopII(int n) {\n    int res = 0;\n    int i = 1; // Initialize condition variable\n    // Sum 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // Update condition variable\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
      iteration.java
      /* while loop (two updates) */\nint whileLoopII(int n) {\n    int res = 0;\n    int i = 1; // Initialize condition variable\n    // Sum 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // Update condition variable\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
      iteration.cs
      /* while loop (two updates) */\nint WhileLoopII(int n) {\n    int res = 0;\n    int i = 1; // Initialize condition variable\n    // Sum 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // Update condition variable\n        i += 1; \n        i *= 2;\n    }\n    return res;\n}\n
      iteration.go
      /* while loop (two updates) */\nfunc whileLoopII(n int) int {\n    res := 0\n    // Initialize condition variable\n    i := 1\n    // Sum 1, 4, 10, ...\n    for i <= n {\n        res += i\n        // Update condition variable\n        i++\n        i *= 2\n    }\n    return res\n}\n
      iteration.swift
      /* while loop (two updates) */\nfunc whileLoopII(n: Int) -> Int {\n    var res = 0\n    var i = 1 // Initialize condition variable\n    // Sum 1, 4, 10, ...\n    while i <= n {\n        res += i\n        // Update condition variable\n        i += 1\n        i *= 2\n    }\n    return res\n}\n
      iteration.js
      /* while loop (two updates) */\nfunction whileLoopII(n) {\n    let res = 0;\n    let i = 1; // Initialize condition variable\n    // Sum 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // Update condition variable\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
      iteration.ts
      /* while loop (two updates) */\nfunction whileLoopII(n: number): number {\n    let res = 0;\n    let i = 1; // Initialize condition variable\n    // Sum 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // Update condition variable\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
      iteration.dart
      /* while loop (two updates) */\nint whileLoopII(int n) {\n  int res = 0;\n  int i = 1; // Initialize condition variable\n  // Sum 1, 4, 10, ...\n  while (i <= n) {\n    res += i;\n    // Update condition variable\n    i++;\n    i *= 2;\n  }\n  return res;\n}\n
      iteration.rs
      /* while loop (two updates) */\nfn while_loop_ii(n: i32) -> i32 {\n    let mut res = 0;\n    let mut i = 1; // Initialize condition variable\n\n    // Sum 1, 4, 10, ...\n    while i <= n {\n        res += i;\n        // Update condition variable\n        i += 1;\n        i *= 2;\n    }\n    res\n}\n
      iteration.c
      /* while loop (two updates) */\nint whileLoopII(int n) {\n    int res = 0;\n    int i = 1; // Initialize condition variable\n    // Sum 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // Update condition variable\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
      iteration.kt
      /* while loop (two updates) */\nfun whileLoopII(n: Int): Int {\n    var res = 0\n    var i = 1 // Initialize condition variable\n    // Sum 1, 4, 10, ...\n    while (i <= n) {\n        res += i\n        // Update condition variable\n        i++\n        i *= 2\n    }\n    return res\n}\n
      iteration.rb
      ### while loop (two updates) ###\ndef while_loop_ii(n)\n  res = 0\n  i = 1 # Initialize condition variable\n\n  # Sum 1, 4, 10, ...\n  while i <= n\n    res += i\n    # Update condition variable\n    i += 1\n    i *= 2\n  end\n\n  res\nend\n

      Overall, for loops have more compact code, while while loops are more flexible; both can implement iterative structures. The choice of which to use should be determined based on the requirements of the specific problem.

      "},{"location":"chapter_computational_complexity/iteration_and_recursion/#3-nested-loops","title":"3. \u00a0 Nested Loops","text":"

      We can nest one loop structure inside another. Below is an example using for loops:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
      def nested_for_loop(n: int) -> str:\n    \"\"\"Nested for loop\"\"\"\n    res = \"\"\n    # Loop i = 1, 2, ..., n-1, n\n    for i in range(1, n + 1):\n        # Loop j = 1, 2, ..., n-1, n\n        for j in range(1, n + 1):\n            res += f\"({i}, {j}), \"\n    return res\n
      iteration.cpp
      /* Nested for loop */\nstring nestedForLoop(int n) {\n    ostringstream res;\n    // Loop i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; ++i) {\n        // Loop j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; ++j) {\n            res << \"(\" << i << \", \" << j << \"), \";\n        }\n    }\n    return res.str();\n}\n
      iteration.java
      /* Nested for loop */\nString nestedForLoop(int n) {\n    StringBuilder res = new StringBuilder();\n    // Loop i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        // Loop j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; j++) {\n            res.append(\"(\" + i + \", \" + j + \"), \");\n        }\n    }\n    return res.toString();\n}\n
      iteration.cs
      /* Nested for loop */\nstring NestedForLoop(int n) {\n    StringBuilder res = new();\n    // Loop i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        // Loop j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; j++) {\n            res.Append($\"({i}, {j}), \");\n        }\n    }\n    return res.ToString();\n}\n
      iteration.go
      /* Nested for loop */\nfunc nestedForLoop(n int) string {\n    res := \"\"\n    // Loop i = 1, 2, ..., n-1, n\n    for i := 1; i <= n; i++ {\n        for j := 1; j <= n; j++ {\n            // Loop j = 1, 2, ..., n-1, n\n            res += fmt.Sprintf(\"(%d, %d), \", i, j)\n        }\n    }\n    return res\n}\n
      iteration.swift
      /* Nested for loop */\nfunc nestedForLoop(n: Int) -> String {\n    var res = \"\"\n    // Loop i = 1, 2, ..., n-1, n\n    for i in 1 ... n {\n        // Loop j = 1, 2, ..., n-1, n\n        for j in 1 ... n {\n            res.append(\"(\\(i), \\(j)), \")\n        }\n    }\n    return res\n}\n
      iteration.js
      /* Nested for loop */\nfunction nestedForLoop(n) {\n    let res = '';\n    // Loop i = 1, 2, ..., n-1, n\n    for (let i = 1; i <= n; i++) {\n        // Loop j = 1, 2, ..., n-1, n\n        for (let j = 1; j <= n; j++) {\n            res += `(${i}, ${j}), `;\n        }\n    }\n    return res;\n}\n
      iteration.ts
      /* Nested for loop */\nfunction nestedForLoop(n: number): string {\n    let res = '';\n    // Loop i = 1, 2, ..., n-1, n\n    for (let i = 1; i <= n; i++) {\n        // Loop j = 1, 2, ..., n-1, n\n        for (let j = 1; j <= n; j++) {\n            res += `(${i}, ${j}), `;\n        }\n    }\n    return res;\n}\n
      iteration.dart
      /* Nested for loop */\nString nestedForLoop(int n) {\n  String res = \"\";\n  // Loop i = 1, 2, ..., n-1, n\n  for (int i = 1; i <= n; i++) {\n    // Loop j = 1, 2, ..., n-1, n\n    for (int j = 1; j <= n; j++) {\n      res += \"($i, $j), \";\n    }\n  }\n  return res;\n}\n
      iteration.rs
      /* Nested for loop */\nfn nested_for_loop(n: i32) -> String {\n    let mut res = vec![];\n    // Loop i = 1, 2, ..., n-1, n\n    for i in 1..=n {\n        // Loop j = 1, 2, ..., n-1, n\n        for j in 1..=n {\n            res.push(format!(\"({}, {}), \", i, j));\n        }\n    }\n    res.join(\"\")\n}\n
      iteration.c
      /* Nested for loop */\nchar *nestedForLoop(int n) {\n    // n * n is the number of points, \"(i, j), \" string max length is 6+10*2, plus extra space for null character \\0\n    int size = n * n * 26 + 1;\n    char *res = malloc(size * sizeof(char));\n    // Loop i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        // Loop j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; j++) {\n            char tmp[26];\n            snprintf(tmp, sizeof(tmp), \"(%d, %d), \", i, j);\n            strncat(res, tmp, size - strlen(res) - 1);\n        }\n    }\n    return res;\n}\n
      iteration.kt
      /* Nested for loop */\nfun nestedForLoop(n: Int): String {\n    val res = StringBuilder()\n    // Loop i = 1, 2, ..., n-1, n\n    for (i in 1..n) {\n        // Loop j = 1, 2, ..., n-1, n\n        for (j in 1..n) {\n            res.append(\" ($i, $j), \")\n        }\n    }\n    return res.toString()\n}\n
      iteration.rb
      ### Nested for loop ###\ndef nested_for_loop(n)\n  res = \"\"\n\n  # Loop i = 1, 2, ..., n-1, n\n  for i in 1..n\n    # Loop j = 1, 2, ..., n-1, n\n    for j in 1..n\n      res += \"(#{i}, #{j}), \"\n    end\n  end\n\n  res\nend\n

      Figure 2-2 shows the flowchart of this nested loop.

      Figure 2-2 \u00a0 Flowchart of nested loops

      In this case, the number of operations of the function is proportional to \\(n^2\\), or the algorithm's running time has a \"quadratic relationship\" with the input data size \\(n\\).

      We can continue adding nested loops, where each nesting is a \"dimension increase\", raising the time complexity to \"cubic relationship\", \"quartic relationship\", and so on.

      "},{"location":"chapter_computational_complexity/iteration_and_recursion/#222-recursion","title":"2.2.2 \u00a0 Recursion","text":"

      Recursion is an algorithmic strategy that solves problems by having a function call itself. It mainly consists of two phases.

      1. Descend: The program continuously calls itself deeper, usually passing in smaller or more simplified parameters, until reaching a \"termination condition\".
      2. Ascend: After triggering the \"termination condition\", the program returns layer by layer from the deepest recursive function, aggregating the result of each layer.

      From an implementation perspective, recursive code mainly consists of three elements.

      1. Termination condition: Used to determine when to switch from \"descending\" to \"ascending\".
      2. Recursive call: Corresponds to \"descending\", where the function calls itself, usually with smaller or more simplified parameters.
      3. Return result: Corresponds to \"ascending\", returning the result of the current recursion level to the previous layer.

      Observe the following code. We only need to call the function recur(n) to complete the calculation of \\(1 + 2 + \\dots + n\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
      def recur(n: int) -> int:\n    \"\"\"Recursion\"\"\"\n    # Termination condition\n    if n == 1:\n        return 1\n    # Recurse: recursive call\n    res = recur(n - 1)\n    # Return: return result\n    return n + res\n
      recursion.cpp
      /* Recursion */\nint recur(int n) {\n    // Termination condition\n    if (n == 1)\n        return 1;\n    // Recurse: recursive call\n    int res = recur(n - 1);\n    // Return: return result\n    return n + res;\n}\n
      recursion.java
      /* Recursion */\nint recur(int n) {\n    // Termination condition\n    if (n == 1)\n        return 1;\n    // Recurse: recursive call\n    int res = recur(n - 1);\n    // Return: return result\n    return n + res;\n}\n
      recursion.cs
      /* Recursion */\nint Recur(int n) {\n    // Termination condition\n    if (n == 1)\n        return 1;\n    // Recurse: recursive call\n    int res = Recur(n - 1);\n    // Return: return result\n    return n + res;\n}\n
      recursion.go
      /* Recursion */\nfunc recur(n int) int {\n    // Termination condition\n    if n == 1 {\n        return 1\n    }\n    // Recurse: recursive call\n    res := recur(n - 1)\n    // Return: return result\n    return n + res\n}\n
      recursion.swift
      /* Recursion */\nfunc recur(n: Int) -> Int {\n    // Termination condition\n    if n == 1 {\n        return 1\n    }\n    // Recurse: recursive call\n    let res = recur(n: n - 1)\n    // Return: return result\n    return n + res\n}\n
      recursion.js
      /* Recursion */\nfunction recur(n) {\n    // Termination condition\n    if (n === 1) return 1;\n    // Recurse: recursive call\n    const res = recur(n - 1);\n    // Return: return result\n    return n + res;\n}\n
      recursion.ts
      /* Recursion */\nfunction recur(n: number): number {\n    // Termination condition\n    if (n === 1) return 1;\n    // Recurse: recursive call\n    const res = recur(n - 1);\n    // Return: return result\n    return n + res;\n}\n
      recursion.dart
      /* Recursion */\nint recur(int n) {\n  // Termination condition\n  if (n == 1) return 1;\n  // Recurse: recursive call\n  int res = recur(n - 1);\n  // Return: return result\n  return n + res;\n}\n
      recursion.rs
      /* Recursion */\nfn recur(n: i32) -> i32 {\n    // Termination condition\n    if n == 1 {\n        return 1;\n    }\n    // Recurse: recursive call\n    let res = recur(n - 1);\n    // Return: return result\n    n + res\n}\n
      recursion.c
      /* Recursion */\nint recur(int n) {\n    // Termination condition\n    if (n == 1)\n        return 1;\n    // Recurse: recursive call\n    int res = recur(n - 1);\n    // Return: return result\n    return n + res;\n}\n
      recursion.kt
      /* Recursion */\nfun recur(n: Int): Int {\n    // Termination condition\n    if (n == 1)\n        return 1\n    // Descend: recursive call\n    val res = recur(n - 1)\n    // Return: return result\n    return n + res\n}\n
      recursion.rb
      ### Recursion ###\ndef recur(n)\n  # Termination condition\n  return 1 if n == 1\n  # Recurse: recursive call\n  res = recur(n - 1)\n  # Return: return result\n  n + res\nend\n

      Figure 2-3 shows the recursive process of this function.

      Figure 2-3 \u00a0 Recursive process of the summation function

      Although from a computational perspective, iteration and recursion can achieve the same results, they represent two completely different paradigms for thinking about and solving problems.

      • Iteration: Solves problems \"bottom-up\". Starting from the most basic steps, these steps are then repeatedly executed or accumulated until the task is complete.
      • Recursion: Solves problems \"top-down\". The original problem is decomposed into smaller subproblems that have the same form as the original problem. These subproblems continue to be decomposed into even smaller subproblems until reaching the base case (where the solution is known).

      Taking the above summation function as an example, let the problem be \\(f(n) = 1 + 2 + \\dots + n\\).

      • Iteration: Simulates the summation process in a loop, traversing from \\(1\\) to \\(n\\), performing the summation operation in each round to obtain \\(f(n)\\).
      • Recursion: Decomposes the problem into the subproblem \\(f(n) = n + f(n-1)\\), continuously decomposing (recursively) until terminating at the base case \\(f(1) = 1\\).
      "},{"location":"chapter_computational_complexity/iteration_and_recursion/#1-call-stack","title":"1. \u00a0 Call Stack","text":"

      Each time a recursive function calls itself, the system allocates memory for the newly opened function to store local variables, call addresses, and other information. This leads to two consequences.

      • The function's context data is stored in a memory area called \"stack frame space\", which is not released until the function returns. Therefore, recursion usually consumes more memory space than iteration.
      • Recursive function calls incur additional overhead. Therefore, recursion is usually less time-efficient than loops.

      As shown in Figure 2-4, before the termination condition is triggered, there are \\(n\\) unreturned recursive functions existing simultaneously, with a recursion depth of \\(n\\).

      Figure 2-4 \u00a0 Recursion call depth

      In practice, the recursion depth allowed by programming languages is usually limited, and excessively deep recursion may lead to stack overflow errors.

      "},{"location":"chapter_computational_complexity/iteration_and_recursion/#2-tail-recursion","title":"2. \u00a0 Tail Recursion","text":"

      Interestingly, if a function makes the recursive call as the very last step before returning, the function can be optimized by the compiler or interpreter to have space efficiency comparable to iteration. This case is called tail recursion.

      • Regular recursion: When a function returns to the previous level, it needs to continue executing code, so the system needs to save the context of the previous layer's call.
      • Tail recursion: The recursive call is the last operation before the function returns, meaning that after returning to the previous level, there is no need to continue executing other operations, so the system does not need to save the context of the previous layer's function.

      Taking the calculation of \\(1 + 2 + \\dots + n\\) as an example, we can set the result variable res as a function parameter to implement tail recursion:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
      def tail_recur(n, res):\n    \"\"\"Tail recursion\"\"\"\n    # Termination condition\n    if n == 0:\n        return res\n    # Tail recursive call\n    return tail_recur(n - 1, res + n)\n
      recursion.cpp
      /* Tail recursion */\nint tailRecur(int n, int res) {\n    // Termination condition\n    if (n == 0)\n        return res;\n    // Tail recursive call\n    return tailRecur(n - 1, res + n);\n}\n
      recursion.java
      /* Tail recursion */\nint tailRecur(int n, int res) {\n    // Termination condition\n    if (n == 0)\n        return res;\n    // Tail recursive call\n    return tailRecur(n - 1, res + n);\n}\n
      recursion.cs
      /* Tail recursion */\nint TailRecur(int n, int res) {\n    // Termination condition\n    if (n == 0)\n        return res;\n    // Tail recursive call\n    return TailRecur(n - 1, res + n);\n}\n
      recursion.go
      /* Tail recursion */\nfunc tailRecur(n int, res int) int {\n    // Termination condition\n    if n == 0 {\n        return res\n    }\n    // Tail recursive call\n    return tailRecur(n-1, res+n)\n}\n
      recursion.swift
      /* Tail recursion */\nfunc tailRecur(n: Int, res: Int) -> Int {\n    // Termination condition\n    if n == 0 {\n        return res\n    }\n    // Tail recursive call\n    return tailRecur(n: n - 1, res: res + n)\n}\n
      recursion.js
      /* Tail recursion */\nfunction tailRecur(n, res) {\n    // Termination condition\n    if (n === 0) return res;\n    // Tail recursive call\n    return tailRecur(n - 1, res + n);\n}\n
      recursion.ts
      /* Tail recursion */\nfunction tailRecur(n: number, res: number): number {\n    // Termination condition\n    if (n === 0) return res;\n    // Tail recursive call\n    return tailRecur(n - 1, res + n);\n}\n
      recursion.dart
      /* Tail recursion */\nint tailRecur(int n, int res) {\n  // Termination condition\n  if (n == 0) return res;\n  // Tail recursive call\n  return tailRecur(n - 1, res + n);\n}\n
      recursion.rs
      /* Tail recursion */\nfn tail_recur(n: i32, res: i32) -> i32 {\n    // Termination condition\n    if n == 0 {\n        return res;\n    }\n    // Tail recursive call\n    tail_recur(n - 1, res + n)\n}\n
      recursion.c
      /* Tail recursion */\nint tailRecur(int n, int res) {\n    // Termination condition\n    if (n == 0)\n        return res;\n    // Tail recursive call\n    return tailRecur(n - 1, res + n);\n}\n
      recursion.kt
      /* Tail recursion */\ntailrec fun tailRecur(n: Int, res: Int): Int {\n    // Add tailrec keyword to enable tail recursion optimization\n    // Termination condition\n    if (n == 0)\n        return res\n    // Tail recursive call\n    return tailRecur(n - 1, res + n)\n}\n
      recursion.rb
      ### Tail recursion ###\ndef tail_recur(n, res)\n  # Termination condition\n  return res if n == 0\n  # Tail recursive call\n  tail_recur(n - 1, res + n)\nend\n

      The execution process of tail recursion is shown in Figure 2-5. Comparing regular recursion and tail recursion, the execution point of the summation operation is different.

      • Regular recursion: The summation operation is performed during the \"ascending\" process, requiring an additional summation operation after each layer returns.
      • Tail recursion: The summation operation is performed during the \"descending\" process; the \"ascending\" process only needs to return layer by layer.

      Figure 2-5 \u00a0 Tail recursion process

      Tip

      Please note that many compilers or interpreters do not support tail recursion optimization. For example, Python does not support tail recursion optimization by default, so even if a function is in tail recursive form, it may still encounter stack overflow issues.

      "},{"location":"chapter_computational_complexity/iteration_and_recursion/#3-recursion-tree","title":"3. \u00a0 Recursion Tree","text":"

      When dealing with algorithmic problems related to \"divide and conquer\", recursion often provides a more intuitive approach and more readable code than iteration. Taking the \"Fibonacci sequence\" as an example.

      Question

      Given a Fibonacci sequence \\(0, 1, 1, 2, 3, 5, 8, 13, \\dots\\), find the \\(n\\)-th number in the sequence.

      Let the \\(n\\)-th number of the Fibonacci sequence be \\(f(n)\\). Two conclusions can be easily obtained.

      • The first two numbers of the sequence are \\(f(1) = 0\\) and \\(f(2) = 1\\).
      • Each number in the sequence is the sum of the previous two numbers, i.e., \\(f(n) = f(n - 1) + f(n - 2)\\).

      Following the recurrence relation to make recursive calls, with the first two numbers as termination conditions, we can write the recursive code. Calling fib(n) will give us the \\(n\\)-th number of the Fibonacci sequence:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
      def fib(n: int) -> int:\n    \"\"\"Fibonacci sequence: recursion\"\"\"\n    # Termination condition f(1) = 0, f(2) = 1\n    if n == 1 or n == 2:\n        return n - 1\n    # Recursive call f(n) = f(n-1) + f(n-2)\n    res = fib(n - 1) + fib(n - 2)\n    # Return result f(n)\n    return res\n
      recursion.cpp
      /* Fibonacci sequence: recursion */\nint fib(int n) {\n    // Termination condition f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // Recursive call f(n) = f(n-1) + f(n-2)\n    int res = fib(n - 1) + fib(n - 2);\n    // Return result f(n)\n    return res;\n}\n
      recursion.java
      /* Fibonacci sequence: recursion */\nint fib(int n) {\n    // Termination condition f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // Recursive call f(n) = f(n-1) + f(n-2)\n    int res = fib(n - 1) + fib(n - 2);\n    // Return result f(n)\n    return res;\n}\n
      recursion.cs
      /* Fibonacci sequence: recursion */\nint Fib(int n) {\n    // Termination condition f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // Recursive call f(n) = f(n-1) + f(n-2)\n    int res = Fib(n - 1) + Fib(n - 2);\n    // Return result f(n)\n    return res;\n}\n
      recursion.go
      /* Fibonacci sequence: recursion */\nfunc fib(n int) int {\n    // Termination condition f(1) = 0, f(2) = 1\n    if n == 1 || n == 2 {\n        return n - 1\n    }\n    // Recursive call f(n) = f(n-1) + f(n-2)\n    res := fib(n-1) + fib(n-2)\n    // Return result f(n)\n    return res\n}\n
      recursion.swift
      /* Fibonacci sequence: recursion */\nfunc fib(n: Int) -> Int {\n    // Termination condition f(1) = 0, f(2) = 1\n    if n == 1 || n == 2 {\n        return n - 1\n    }\n    // Recursive call f(n) = f(n-1) + f(n-2)\n    let res = fib(n: n - 1) + fib(n: n - 2)\n    // Return result f(n)\n    return res\n}\n
      recursion.js
      /* Fibonacci sequence: recursion */\nfunction fib(n) {\n    // Termination condition f(1) = 0, f(2) = 1\n    if (n === 1 || n === 2) return n - 1;\n    // Recursive call f(n) = f(n-1) + f(n-2)\n    const res = fib(n - 1) + fib(n - 2);\n    // Return result f(n)\n    return res;\n}\n
      recursion.ts
      /* Fibonacci sequence: recursion */\nfunction fib(n: number): number {\n    // Termination condition f(1) = 0, f(2) = 1\n    if (n === 1 || n === 2) return n - 1;\n    // Recursive call f(n) = f(n-1) + f(n-2)\n    const res = fib(n - 1) + fib(n - 2);\n    // Return result f(n)\n    return res;\n}\n
      recursion.dart
      /* Fibonacci sequence: recursion */\nint fib(int n) {\n  // Termination condition f(1) = 0, f(2) = 1\n  if (n == 1 || n == 2) return n - 1;\n  // Recursive call f(n) = f(n-1) + f(n-2)\n  int res = fib(n - 1) + fib(n - 2);\n  // Return result f(n)\n  return res;\n}\n
      recursion.rs
      /* Fibonacci sequence: recursion */\nfn fib(n: i32) -> i32 {\n    // Termination condition f(1) = 0, f(2) = 1\n    if n == 1 || n == 2 {\n        return n - 1;\n    }\n    // Recursive call f(n) = f(n-1) + f(n-2)\n    let res = fib(n - 1) + fib(n - 2);\n    // Return result\n    res\n}\n
      recursion.c
      /* Fibonacci sequence: recursion */\nint fib(int n) {\n    // Termination condition f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // Recursive call f(n) = f(n-1) + f(n-2)\n    int res = fib(n - 1) + fib(n - 2);\n    // Return result f(n)\n    return res;\n}\n
      recursion.kt
      /* Fibonacci sequence: recursion */\nfun fib(n: Int): Int {\n    // Termination condition f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1\n    // Recursive call f(n) = f(n-1) + f(n-2)\n    val res = fib(n - 1) + fib(n - 2)\n    // Return result f(n)\n    return res\n}\n
      recursion.rb
      ### Fibonacci sequence: recursion ###\ndef fib(n)\n  # Termination condition f(1) = 0, f(2) = 1\n  return n - 1 if n == 1 || n == 2\n  # Recursive call f(n) = f(n-1) + f(n-2)\n  res = fib(n - 1) + fib(n - 2)\n  # Return result f(n)\n  res\nend\n

      Observing the above code, we recursively call two functions within the function, meaning that one call produces two call branches. As shown in Figure 2-6, such continuous recursive calling will eventually produce a recursion tree with \\(n\\) levels.

      Figure 2-6 \u00a0 Recursion tree of the Fibonacci sequence

      Fundamentally, recursion embodies the paradigm of \"decomposing a problem into smaller subproblems\", and this divide-and-conquer strategy is crucial.

      • From an algorithmic perspective, many important algorithmic strategies such as searching, sorting, backtracking, divide and conquer, and dynamic programming directly or indirectly apply this way of thinking.
      • From a data structure perspective, recursion is naturally suited for handling problems related to linked lists, trees, and graphs, because they are well-suited for analysis using divide-and-conquer thinking.
      "},{"location":"chapter_computational_complexity/iteration_and_recursion/#223-comparison-of-the-two","title":"2.2.3 \u00a0 Comparison of the Two","text":"

      Summarizing the above content, as shown in Table 2-1, iteration and recursion differ in implementation, performance, and applicability.

      Table 2-1 \u00a0 Comparison of iteration and recursion characteristics

      Iteration Recursion Implementation Loop structure Function calls itself Time efficiency Generally more efficient, no function call overhead Each function call incurs overhead Memory usage Usually uses a fixed amount of memory space Accumulated function calls may use a large amount of stack frame space Suitable problems Suitable for simple loop tasks, with intuitive and readable code Suitable for subproblem decomposition, such as trees, graphs, divide and conquer, backtracking, etc., with concise and clear code structure

      Tip

      If you find the following content difficult to understand, you can review it after reading the \"Stack\" chapter.

      What is the intrinsic relationship between iteration and recursion? Taking the above recursive function as an example, the summation operation is performed during the \"ascending\" phase of recursion. This means that the function called first actually completes its summation operation last, and this working mechanism is similar to the \"last-in, first-out\" principle of stacks.

      In fact, recursive terminology such as \"call stack\" and \"stack frame space\" already hints at the close relationship between recursion and stacks.

      1. Descend: When a function is called, the system allocates a new stack frame on the \"call stack\" for that function to store the function's local variables, parameters, return address, and other data.
      2. Ascend: When the function completes execution and returns, the corresponding stack frame is removed from the \"call stack\", restoring the execution environment of the previous function.

      Therefore, we can use an explicit stack to simulate the behavior of the call stack, thus transforming recursion into iterative form:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
      def for_loop_recur(n: int) -> int:\n    \"\"\"Simulate recursion using iteration\"\"\"\n    # Use an explicit stack to simulate the system call stack\n    stack = []\n    res = 0\n    # Recurse: recursive call\n    for i in range(n, 0, -1):\n        # Simulate \"recurse\" with \"push\"\n        stack.append(i)\n    # Return: return result\n    while stack:\n        # Simulate \"return\" with \"pop\"\n        res += stack.pop()\n    # res = 1+2+3+...+n\n    return res\n
      recursion.cpp
      /* Simulate recursion using iteration */\nint forLoopRecur(int n) {\n    // Use an explicit stack to simulate the system call stack\n    stack<int> stack;\n    int res = 0;\n    // Recurse: recursive call\n    for (int i = n; i > 0; i--) {\n        // Simulate \"recurse\" with \"push\"\n        stack.push(i);\n    }\n    // Return: return result\n    while (!stack.empty()) {\n        // Simulate \"return\" with \"pop\"\n        res += stack.top();\n        stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
      recursion.java
      /* Simulate recursion using iteration */\nint forLoopRecur(int n) {\n    // Use an explicit stack to simulate the system call stack\n    Stack<Integer> stack = new Stack<>();\n    int res = 0;\n    // Recurse: recursive call\n    for (int i = n; i > 0; i--) {\n        // Simulate \"recurse\" with \"push\"\n        stack.push(i);\n    }\n    // Return: return result\n    while (!stack.isEmpty()) {\n        // Simulate \"return\" with \"pop\"\n        res += stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
      recursion.cs
      /* Simulate recursion using iteration */\nint ForLoopRecur(int n) {\n    // Use an explicit stack to simulate the system call stack\n    Stack<int> stack = new();\n    int res = 0;\n    // Recurse: recursive call\n    for (int i = n; i > 0; i--) {\n        // Simulate \"recurse\" with \"push\"\n        stack.Push(i);\n    }\n    // Return: return result\n    while (stack.Count > 0) {\n        // Simulate \"return\" with \"pop\"\n        res += stack.Pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
      recursion.go
      /* Simulate recursion using iteration */\nfunc forLoopRecur(n int) int {\n    // Use an explicit stack to simulate the system call stack\n    stack := list.New()\n    res := 0\n    // Recurse: recursive call\n    for i := n; i > 0; i-- {\n        // Simulate \"recurse\" with \"push\"\n        stack.PushBack(i)\n    }\n    // Return: return result\n    for stack.Len() != 0 {\n        // Simulate \"return\" with \"pop\"\n        res += stack.Back().Value.(int)\n        stack.Remove(stack.Back())\n    }\n    // res = 1+2+3+...+n\n    return res\n}\n
      recursion.swift
      /* Simulate recursion using iteration */\nfunc forLoopRecur(n: Int) -> Int {\n    // Use an explicit stack to simulate the system call stack\n    var stack: [Int] = []\n    var res = 0\n    // Recurse: recursive call\n    for i in (1 ... n).reversed() {\n        // Simulate \"recurse\" with \"push\"\n        stack.append(i)\n    }\n    // Return: return result\n    while !stack.isEmpty {\n        // Simulate \"return\" with \"pop\"\n        res += stack.removeLast()\n    }\n    // res = 1+2+3+...+n\n    return res\n}\n
      recursion.js
      /* Simulate recursion using iteration */\nfunction forLoopRecur(n) {\n    // Use an explicit stack to simulate the system call stack\n    const stack = [];\n    let res = 0;\n    // Recurse: recursive call\n    for (let i = n; i > 0; i--) {\n        // Simulate \"recurse\" with \"push\"\n        stack.push(i);\n    }\n    // Return: return result\n    while (stack.length) {\n        // Simulate \"return\" with \"pop\"\n        res += stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
      recursion.ts
      /* Simulate recursion using iteration */\nfunction forLoopRecur(n: number): number {\n    // Use an explicit stack to simulate the system call stack\n    const stack: number[] = [];\n    let res: number = 0;\n    // Recurse: recursive call\n    for (let i = n; i > 0; i--) {\n        // Simulate \"recurse\" with \"push\"\n        stack.push(i);\n    }\n    // Return: return result\n    while (stack.length) {\n        // Simulate \"return\" with \"pop\"\n        res += stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
      recursion.dart
      /* Simulate recursion using iteration */\nint forLoopRecur(int n) {\n  // Use an explicit stack to simulate the system call stack\n  List<int> stack = [];\n  int res = 0;\n  // Recurse: recursive call\n  for (int i = n; i > 0; i--) {\n    // Simulate \"recurse\" with \"push\"\n    stack.add(i);\n  }\n  // Return: return result\n  while (!stack.isEmpty) {\n    // Simulate \"return\" with \"pop\"\n    res += stack.removeLast();\n  }\n  // res = 1+2+3+...+n\n  return res;\n}\n
      recursion.rs
      /* Simulate recursion using iteration */\nfn for_loop_recur(n: i32) -> i32 {\n    // Use an explicit stack to simulate the system call stack\n    let mut stack = Vec::new();\n    let mut res = 0;\n    // Recurse: recursive call\n    for i in (1..=n).rev() {\n        // Simulate \"recurse\" with \"push\"\n        stack.push(i);\n    }\n    // Return: return result\n    while !stack.is_empty() {\n        // Simulate \"return\" with \"pop\"\n        res += stack.pop().unwrap();\n    }\n    // res = 1+2+3+...+n\n    res\n}\n
      recursion.c
      /* Simulate recursion using iteration */\nint forLoopRecur(int n) {\n    int stack[1000]; // Use a large array to simulate stack\n    int top = -1;    // Stack top index\n    int res = 0;\n    // Recurse: recursive call\n    for (int i = n; i > 0; i--) {\n        // Simulate \"recurse\" with \"push\"\n        stack[1 + top++] = i;\n    }\n    // Return: return result\n    while (top >= 0) {\n        // Simulate \"return\" with \"pop\"\n        res += stack[top--];\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
      recursion.kt
      /* Simulate recursion using iteration */\nfun forLoopRecur(n: Int): Int {\n    // Use an explicit stack to simulate the system call stack\n    val stack = Stack<Int>()\n    var res = 0\n    // Descend: recursive call\n    for (i in n downTo 0) {\n        // Simulate \"recurse\" with \"push\"\n        stack.push(i)\n    }\n    // Return: return result\n    while (stack.isNotEmpty()) {\n        // Simulate \"return\" with \"pop\"\n        res += stack.pop()\n    }\n    // res = 1+2+3+...+n\n    return res\n}\n
      recursion.rb
      ### Use iteration to simulate recursion ###\ndef for_loop_recur(n)\n  # Use an explicit stack to simulate the system call stack\n  stack = []\n  res = 0\n\n  # Recurse: recursive call\n  for i in n.downto(0)\n    # Simulate \"recurse\" with \"push\"\n    stack << i\n  end\n  # Return: return result\n  while !stack.empty?\n    res += stack.pop\n  end\n\n  # res = 1+2+3+...+n\n  res\nend\n

      Observing the above code, when recursion is transformed into iteration, the code becomes more complex. Although iteration and recursion can be converted into each other in many cases, it may not be worthwhile to do so for the following two reasons.

      • The transformed code may be more difficult to understand and less readable.
      • For some complex problems, simulating the behavior of the system call stack can be very difficult.

      In summary, choosing between iteration and recursion depends on the nature of the specific problem. In programming practice, it is crucial to weigh the pros and cons of both and choose the appropriate method based on the context.

      "},{"location":"chapter_computational_complexity/performance_evaluation/","title":"2.1 \u00a0 Algorithm Efficiency Evaluation","text":"

      In algorithm design, we pursue the following two levels of objectives sequentially.

      1. Finding a solution to the problem: The algorithm must reliably obtain the correct solution within the specified input range.
      2. Seeking the optimal solution: Multiple solutions may exist for the same problem, and we hope to find an algorithm that is as efficient as possible.

      In other words, under the premise of being able to solve the problem, algorithm efficiency has become the primary evaluation criterion for measuring the quality of algorithms. It includes the following two dimensions.

      • Time efficiency: The length of time the algorithm runs.
      • Space efficiency: The size of memory space the algorithm occupies.

      In short, our goal is to design data structures and algorithms that are \"both fast and memory-efficient\". Effectively evaluating algorithm efficiency is crucial, because only in this way can we compare various algorithms and guide the algorithm design and optimization process.

      Efficiency evaluation methods are mainly divided into two types: actual testing and theoretical estimation.

      "},{"location":"chapter_computational_complexity/performance_evaluation/#211-actual-testing","title":"2.1.1 \u00a0 Actual Testing","text":"

      Suppose we now have algorithm A and algorithm B, both of which can solve the same problem, and we need to compare the efficiency of these two algorithms. The most direct method is to find a computer, run these two algorithms, and monitor and record their running time and memory usage. This evaluation approach can reflect the real situation, but it also has considerable limitations.

      On one hand, it is difficult to eliminate interference factors from the testing environment. Hardware configuration affects the performance of algorithms. For example, if an algorithm has a high degree of parallelism, it is more suitable for running on multi-core CPUs; if an algorithm has intensive memory operations, it will perform better on high-performance memory. In other words, the test results of an algorithm on different machines may be inconsistent. This means we need to test on various machines and calculate average efficiency, which is impractical.

      On the other hand, conducting complete testing is very resource-intensive. As the input data volume changes, the algorithm will exhibit different efficiencies. For example, when the input data volume is small, the running time of algorithm A is shorter than algorithm B; but when the input data volume is large, the test results may be exactly the opposite. Therefore, to obtain convincing conclusions, we need to test input data of various scales, which requires a large amount of computational resources.

      "},{"location":"chapter_computational_complexity/performance_evaluation/#212-theoretical-estimation","title":"2.1.2 \u00a0 Theoretical Estimation","text":"

      Since actual testing has considerable limitations, we can consider evaluating algorithm efficiency through calculations alone. This estimation method is called asymptotic complexity analysis, or complexity analysis for short.

      Complexity analysis can reflect the relationship between the time and space resources required for algorithm execution and the input data scale. It describes the growth trend of the time and space required for algorithm execution as the input data scale increases. This definition is somewhat convoluted, so we can break it down into three key points to understand.

      • \"Time and space resources\" correspond to time complexity and space complexity, respectively.
      • \"As the input data scale increases\" means that complexity reflects the relationship between algorithm running efficiency and input data scale.
      • \"Growth trend of time and space\" indicates that complexity analysis focuses not on the specific values of running time or occupied space, but on how \"fast\" time or space grows.

      Complexity analysis overcomes the drawbacks of the actual testing method, reflected in the following aspects.

      • It does not need to actually run the code, making it more environmentally friendly and energy-efficient.
      • It is independent of the testing environment, and the analysis results are applicable to all running platforms.
      • It can reflect algorithm efficiency at different data volumes, especially algorithm performance at large data volumes.

      Tip

      If you are still confused about the concept of complexity, don't worry\u2014we will introduce it in detail in subsequent chapters.

      Complexity analysis provides us with a \"ruler\" for evaluating algorithm efficiency, allowing us to measure the time and space resources required to execute a certain algorithm and compare the efficiency between different algorithms.

      Complexity is a mathematical concept that may be relatively abstract for beginners, with a relatively high learning difficulty. From this perspective, complexity analysis may not be very suitable as the first content to be introduced. However, when we discuss the characteristics of a certain data structure or algorithm, it is difficult to avoid analyzing its running speed and space usage.

      In summary, it is recommended that before diving deep into data structures and algorithms, you first establish a preliminary understanding of complexity analysis so that you can complete complexity analysis of simple algorithms.

      "},{"location":"chapter_computational_complexity/space_complexity/","title":"2.4 \u00a0 Space Complexity","text":"

      Space complexity measures the growth trend of memory space occupied by an algorithm as the data size increases. This concept is very similar to time complexity, except that \"running time\" is replaced with \"occupied memory space\".

      "},{"location":"chapter_computational_complexity/space_complexity/#241-algorithm-related-space","title":"2.4.1 \u00a0 Algorithm-Related Space","text":"

      The memory space used by an algorithm during execution mainly includes the following types.

      • Input space: Used to store the input data of the algorithm.
      • Temporary space: Used to store variables, objects, function contexts, and other data during the algorithm's execution.
      • Output space: Used to store the output data of the algorithm.

      In general, the scope of space complexity statistics is \"temporary space\" plus \"output space\".

      Temporary space can be further divided into three parts.

      • Temporary data: Used to save various constants, variables, objects, etc., during the algorithm's execution.
      • Stack frame space: Used to save the context data of called functions. The system creates a stack frame at the top of the stack each time a function is called, and the stack frame space is released after the function returns.
      • Instruction space: Used to save compiled program instructions, which are usually ignored in actual statistics.

      When analyzing the space complexity of a program, we usually count three parts: temporary data, stack frame space, and output data, as shown in the following figure.

      Figure 2-15 \u00a0 Algorithm-related space

      The related code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      class Node:\n    \"\"\"Class\"\"\"\n    def __init__(self, x: int):\n        self.val: int = x              # Node value\n        self.next: Node | None = None  # Reference to the next node\n\ndef function() -> int:\n    \"\"\"Function\"\"\"\n    # Perform some operations...\n    return 0\n\ndef algorithm(n) -> int:  # Input data\n    A = 0                 # Temporary data (constant, usually represented by uppercase letters)\n    b = 0                 # Temporary data (variable)\n    node = Node(0)        # Temporary data (object)\n    c = function()        # Stack frame space (function call)\n    return A + b + c      # Output data\n
      /* Structure */\nstruct Node {\n    int val;\n    Node *next;\n    Node(int x) : val(x), next(nullptr) {}\n};\n\n/* Function */\nint func() {\n    // Perform some operations...\n    return 0;\n}\n\nint algorithm(int n) {        // Input data\n    const int a = 0;          // Temporary data (constant)\n    int b = 0;                // Temporary data (variable)\n    Node* node = new Node(0); // Temporary data (object)\n    int c = func();           // Stack frame space (function call)\n    return a + b + c;         // Output data\n}\n
      /* Class */\nclass Node {\n    int val;\n    Node next;\n    Node(int x) { val = x; }\n}\n\n/* Function */\nint function() {\n    // Perform some operations...\n    return 0;\n}\n\nint algorithm(int n) {        // Input data\n    final int a = 0;          // Temporary data (constant)\n    int b = 0;                // Temporary data (variable)\n    Node node = new Node(0);  // Temporary data (object)\n    int c = function();       // Stack frame space (function call)\n    return a + b + c;         // Output data\n}\n
      /* Class */\nclass Node(int x) {\n    int val = x;\n    Node next;\n}\n\n/* Function */\nint Function() {\n    // Perform some operations...\n    return 0;\n}\n\nint Algorithm(int n) {        // Input data\n    const int a = 0;          // Temporary data (constant)\n    int b = 0;                // Temporary data (variable)\n    Node node = new(0);       // Temporary data (object)\n    int c = Function();       // Stack frame space (function call)\n    return a + b + c;         // Output data\n}\n
      /* Structure */\ntype node struct {\n    val  int\n    next *node\n}\n\n/* Create node structure */\nfunc newNode(val int) *node {\n    return &node{val: val}\n}\n\n/* Function */\nfunc function() int {\n    // Perform some operations...\n    return 0\n}\n\nfunc algorithm(n int) int { // Input data\n    const a = 0             // Temporary data (constant)\n    b := 0                  // Temporary data (variable)\n    newNode(0)              // Temporary data (object)\n    c := function()         // Stack frame space (function call)\n    return a + b + c        // Output data\n}\n
      /* Class */\nclass Node {\n    var val: Int\n    var next: Node?\n\n    init(x: Int) {\n        val = x\n    }\n}\n\n/* Function */\nfunc function() -> Int {\n    // Perform some operations...\n    return 0\n}\n\nfunc algorithm(n: Int) -> Int { // Input data\n    let a = 0             // Temporary data (constant)\n    var b = 0             // Temporary data (variable)\n    let node = Node(x: 0) // Temporary data (object)\n    let c = function()    // Stack frame space (function call)\n    return a + b + c      // Output data\n}\n
      /* Class */\nclass Node {\n    val;\n    next;\n    constructor(val) {\n        this.val = val === undefined ? 0 : val; // Node value\n        this.next = null;                       // Reference to the next node\n    }\n}\n\n/* Function */\nfunction constFunc() {\n    // Perform some operations\n    return 0;\n}\n\nfunction algorithm(n) {       // Input data\n    const a = 0;              // Temporary data (constant)\n    let b = 0;                // Temporary data (variable)\n    const node = new Node(0); // Temporary data (object)\n    const c = constFunc();    // Stack frame space (function call)\n    return a + b + c;         // Output data\n}\n
      /* Class */\nclass Node {\n    val: number;\n    next: Node | null;\n    constructor(val?: number) {\n        this.val = val === undefined ? 0 : val; // Node value\n        this.next = null;                       // Reference to the next node\n    }\n}\n\n/* Function */\nfunction constFunc(): number {\n    // Perform some operations\n    return 0;\n}\n\nfunction algorithm(n: number): number { // Input data\n    const a = 0;                        // Temporary data (constant)\n    let b = 0;                          // Temporary data (variable)\n    const node = new Node(0);           // Temporary data (object)\n    const c = constFunc();              // Stack frame space (function call)\n    return a + b + c;                   // Output data\n}\n
      /* Class */\nclass Node {\n  int val;\n  Node next;\n  Node(this.val, [this.next]);\n}\n\n/* Function */\nint function() {\n  // Perform some operations...\n  return 0;\n}\n\nint algorithm(int n) {  // Input data\n  const int a = 0;      // Temporary data (constant)\n  int b = 0;            // Temporary data (variable)\n  Node node = Node(0);  // Temporary data (object)\n  int c = function();   // Stack frame space (function call)\n  return a + b + c;     // Output data\n}\n
      use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* Structure */\nstruct Node {\n    val: i32,\n    next: Option<Rc<RefCell<Node>>>,\n}\n\n/* Create Node structure */\nimpl Node {\n    fn new(val: i32) -> Self {\n        Self { val: val, next: None }\n    }\n}\n\n/* Function */\nfn function() -> i32 {\n    // Perform some operations...\n    return 0;\n}\n\nfn algorithm(n: i32) -> i32 {       // Input data\n    const a: i32 = 0;               // Temporary data (constant)\n    let mut b = 0;                  // Temporary data (variable)\n    let node = Node::new(0);        // Temporary data (object)\n    let c = function();             // Stack frame space (function call)\n    return a + b + c;               // Output data\n}\n
      /* Function */\nint func() {\n    // Perform some operations...\n    return 0;\n}\n\nint algorithm(int n) { // Input data\n    const int a = 0;   // Temporary data (constant)\n    int b = 0;         // Temporary data (variable)\n    int c = func();    // Stack frame space (function call)\n    return a + b + c;  // Output data\n}\n
      /* Class */\nclass Node(var _val: Int) {\n    var next: Node? = null\n}\n\n/* Function */\nfun function(): Int {\n    // Perform some operations...\n    return 0\n}\n\nfun algorithm(n: Int): Int { // Input data\n    val a = 0                // Temporary data (constant)\n    var b = 0                // Temporary data (variable)\n    val node = Node(0)       // Temporary data (object)\n    val c = function()       // Stack frame space (function call)\n    return a + b + c         // Output data\n}\n
      ### Class ###\nclass Node\n    attr_accessor :val      # Node value\n    attr_accessor :next     # Reference to the next node\n\n    def initialize(x)\n        @val = x\n    end\nend\n\n### Function ###\ndef function\n    # Perform some operations...\n    0\nend\n\n### Algorithm ###\ndef algorithm(n)        # Input data\n    a = 0               # Temporary data (constant)\n    b = 0               # Temporary data (variable)\n    node = Node.new(0)  # Temporary data (object)\n    c = function        # Stack frame space (function call)\n    a + b + c           # Output data\nend\n
      "},{"location":"chapter_computational_complexity/space_complexity/#242-calculation-method","title":"2.4.2 \u00a0 Calculation Method","text":"

      The calculation method for space complexity is roughly the same as for time complexity, except that the statistical object is changed from \"number of operations\" to \"size of space used\".

      Unlike time complexity, we usually only focus on the worst-case space complexity. This is because memory space is a hard requirement, and we must ensure that sufficient memory space is reserved for all input data.

      Observe the following code. The \"worst case\" in worst-case space complexity has two meanings.

      1. Based on the worst input data: When \\(n < 10\\), the space complexity is \\(O(1)\\); but when \\(n > 10\\), the initialized array nums occupies \\(O(n)\\) space, so the worst-case space complexity is \\(O(n)\\).
      2. Based on the peak memory during algorithm execution: For example, before executing the last line, the program occupies \\(O(1)\\) space; when initializing the array nums, the program occupies \\(O(n)\\) space, so the worst-case space complexity is \\(O(n)\\).
      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      def algorithm(n: int):\n    a = 0               # O(1)\n    b = [0] * 10000     # O(1)\n    if n > 10:\n        nums = [0] * n  # O(n)\n
      void algorithm(int n) {\n    int a = 0;               // O(1)\n    vector<int> b(10000);    // O(1)\n    if (n > 10)\n        vector<int> nums(n); // O(n)\n}\n
      void algorithm(int n) {\n    int a = 0;                   // O(1)\n    int[] b = new int[10000];    // O(1)\n    if (n > 10)\n        int[] nums = new int[n]; // O(n)\n}\n
      void Algorithm(int n) {\n    int a = 0;                   // O(1)\n    int[] b = new int[10000];    // O(1)\n    if (n > 10) {\n        int[] nums = new int[n]; // O(n)\n    }\n}\n
      func algorithm(n int) {\n    a := 0                      // O(1)\n    b := make([]int, 10000)     // O(1)\n    var nums []int\n    if n > 10 {\n        nums := make([]int, n)  // O(n)\n    }\n    fmt.Println(a, b, nums)\n}\n
      func algorithm(n: Int) {\n    let a = 0 // O(1)\n    let b = Array(repeating: 0, count: 10000) // O(1)\n    if n > 10 {\n        let nums = Array(repeating: 0, count: n) // O(n)\n    }\n}\n
      function algorithm(n) {\n    const a = 0;                   // O(1)\n    const b = new Array(10000);    // O(1)\n    if (n > 10) {\n        const nums = new Array(n); // O(n)\n    }\n}\n
      function algorithm(n: number): void {\n    const a = 0;                   // O(1)\n    const b = new Array(10000);    // O(1)\n    if (n > 10) {\n        const nums = new Array(n); // O(n)\n    }\n}\n
      void algorithm(int n) {\n  int a = 0;                            // O(1)\n  List<int> b = List.filled(10000, 0);  // O(1)\n  if (n > 10) {\n    List<int> nums = List.filled(n, 0); // O(n)\n  }\n}\n
      fn algorithm(n: i32) {\n    let a = 0;                              // O(1)\n    let b = [0; 10000];                     // O(1)\n    if n > 10 {\n        let nums = vec![0; n as usize];     // O(n)\n    }\n}\n
      void algorithm(int n) {\n    int a = 0;               // O(1)\n    int b[10000];            // O(1)\n    if (n > 10)\n        int nums[n] = {0};   // O(n)\n}\n
      fun algorithm(n: Int) {\n    val a = 0                    // O(1)\n    val b = IntArray(10000)      // O(1)\n    if (n > 10) {\n        val nums = IntArray(n)   // O(n)\n    }\n}\n
      def algorithm(n)\n    a = 0                           # O(1)\n    b = Array.new(10000)            # O(1)\n    nums = Array.new(n) if n > 10   # O(n)\nend\n

      In recursive functions, it is necessary to count the stack frame space. Observe the following code:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      def function() -> int:\n    # Perform some operations\n    return 0\n\ndef loop(n: int):\n    \"\"\"Loop has space complexity of O(1)\"\"\"\n    for _ in range(n):\n        function()\n\ndef recur(n: int):\n    \"\"\"Recursion has space complexity of O(n)\"\"\"\n    if n == 1:\n        return\n    return recur(n - 1)\n
      int func() {\n    // Perform some operations\n    return 0;\n}\n/* Loop has space complexity of O(1) */\nvoid loop(int n) {\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n/* Recursion has space complexity of O(n) */\nvoid recur(int n) {\n    if (n == 1) return;\n    recur(n - 1);\n}\n
      int function() {\n    // Perform some operations\n    return 0;\n}\n/* Loop has space complexity of O(1) */\nvoid loop(int n) {\n    for (int i = 0; i < n; i++) {\n        function();\n    }\n}\n/* Recursion has space complexity of O(n) */\nvoid recur(int n) {\n    if (n == 1) return;\n    recur(n - 1);\n}\n
      int Function() {\n    // Perform some operations\n    return 0;\n}\n/* Loop has space complexity of O(1) */\nvoid Loop(int n) {\n    for (int i = 0; i < n; i++) {\n        Function();\n    }\n}\n/* Recursion has space complexity of O(n) */\nint Recur(int n) {\n    if (n == 1) return 1;\n    return Recur(n - 1);\n}\n
      func function() int {\n    // Perform some operations\n    return 0\n}\n\n/* Loop has space complexity of O(1) */\nfunc loop(n int) {\n    for i := 0; i < n; i++ {\n        function()\n    }\n}\n\n/* Recursion has space complexity of O(n) */\nfunc recur(n int) {\n    if n == 1 {\n        return\n    }\n    recur(n - 1)\n}\n
      @discardableResult\nfunc function() -> Int {\n    // Perform some operations\n    return 0\n}\n\n/* Loop has space complexity of O(1) */\nfunc loop(n: Int) {\n    for _ in 0 ..< n {\n        function()\n    }\n}\n\n/* Recursion has space complexity of O(n) */\nfunc recur(n: Int) {\n    if n == 1 {\n        return\n    }\n    recur(n: n - 1)\n}\n
      function constFunc() {\n    // Perform some operations\n    return 0;\n}\n/* Loop has space complexity of O(1) */\nfunction loop(n) {\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n/* Recursion has space complexity of O(n) */\nfunction recur(n) {\n    if (n === 1) return;\n    return recur(n - 1);\n}\n
      function constFunc(): number {\n    // Perform some operations\n    return 0;\n}\n/* Loop has space complexity of O(1) */\nfunction loop(n: number): void {\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n/* Recursion has space complexity of O(n) */\nfunction recur(n: number): void {\n    if (n === 1) return;\n    return recur(n - 1);\n}\n
      int function() {\n  // Perform some operations\n  return 0;\n}\n/* Loop has space complexity of O(1) */\nvoid loop(int n) {\n  for (int i = 0; i < n; i++) {\n    function();\n  }\n}\n/* Recursion has space complexity of O(n) */\nvoid recur(int n) {\n  if (n == 1) return;\n  recur(n - 1);\n}\n
      fn function() -> i32 {\n    // Perform some operations\n    return 0;\n}\n/* Loop has space complexity of O(1) */\nfn loop(n: i32) {\n    for i in 0..n {\n        function();\n    }\n}\n/* Recursion has space complexity of O(n) */\nfn recur(n: i32) {\n    if n == 1 {\n        return;\n    }\n    recur(n - 1);\n}\n
      int func() {\n    // Perform some operations\n    return 0;\n}\n/* Loop has space complexity of O(1) */\nvoid loop(int n) {\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n/* Recursion has space complexity of O(n) */\nvoid recur(int n) {\n    if (n == 1) return;\n    recur(n - 1);\n}\n
      fun function(): Int {\n    // Perform some operations\n    return 0\n}\n/* Loop has space complexity of O(1) */\nfun loop(n: Int) {\n    for (i in 0..<n) {\n        function()\n    }\n}\n/* Recursion has space complexity of O(n) */\nfun recur(n: Int) {\n    if (n == 1) return\n    return recur(n - 1)\n}\n
      def function\n    # Perform some operations\n    0\nend\n\n### Loop has space complexity of O(1) ###\ndef loop(n)\n    (0...n).each { function }\nend\n\n### Recursion has space complexity of O(n) ###\ndef recur(n)\n    return if n == 1\n    recur(n - 1)\nend\n

      The time complexity of both functions loop() and recur() is \\(O(n)\\), but their space complexities are different.

      • The function loop() calls function() \\(n\\) times in a loop. In each iteration, function() returns and releases its stack frame space, so the space complexity remains \\(O(1)\\).
      • The recursive function recur() has \\(n\\) unreturned recur() instances existing simultaneously during execution, thus occupying \\(O(n)\\) stack frame space.
      "},{"location":"chapter_computational_complexity/space_complexity/#243-common-types","title":"2.4.3 \u00a0 Common Types","text":"

      Let the input data size be \\(n\\). The following figure shows common types of space complexity (arranged from low to high).

      \\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n^2) < O(2^n) \\newline \\text{Constant} < \\text{Logarithmic} < \\text{Linear} < \\text{Quadratic} < \\text{Exponential} \\end{aligned} \\]

      Figure 2-16 \u00a0 Common types of space complexity

      "},{"location":"chapter_computational_complexity/space_complexity/#1-constant-order-o1","title":"1. \u00a0 Constant Order \\(O(1)\\)","text":"

      Constant order is common in constants, variables, and objects whose quantity is independent of the input data size \\(n\\).

      It should be noted that memory occupied by initializing variables or calling functions in a loop is released when entering the next iteration, so it does not accumulate space, and the space complexity remains \\(O(1)\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
      def function() -> int:\n    \"\"\"Function\"\"\"\n    # Perform some operations\n    return 0\n\ndef constant(n: int):\n    \"\"\"Constant order\"\"\"\n    # Constants, variables, objects occupy O(1) space\n    a = 0\n    nums = [0] * 10000\n    node = ListNode(0)\n    # Variables in the loop occupy O(1) space\n    for _ in range(n):\n        c = 0\n    # Functions in the loop occupy O(1) space\n    for _ in range(n):\n        function()\n
      space_complexity.cpp
      /* Function */\nint func() {\n    // Perform some operations\n    return 0;\n}\n\n/* Constant order */\nvoid constant(int n) {\n    // Constants, variables, objects occupy O(1) space\n    const int a = 0;\n    int b = 0;\n    vector<int> nums(10000);\n    ListNode node(0);\n    // Variables in the loop occupy O(1) space\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // Functions in the loop occupy O(1) space\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n
      space_complexity.java
      /* Function */\nint function() {\n    // Perform some operations\n    return 0;\n}\n\n/* Constant order */\nvoid constant(int n) {\n    // Constants, variables, objects occupy O(1) space\n    final int a = 0;\n    int b = 0;\n    int[] nums = new int[10000];\n    ListNode node = new ListNode(0);\n    // Variables in the loop occupy O(1) space\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // Functions in the loop occupy O(1) space\n    for (int i = 0; i < n; i++) {\n        function();\n    }\n}\n
      space_complexity.cs
      /* Function */\nint Function() {\n    // Perform some operations\n    return 0;\n}\n\n/* Constant order */\nvoid Constant(int n) {\n    // Constants, variables, objects occupy O(1) space\n    int a = 0;\n    int b = 0;\n    int[] nums = new int[10000];\n    ListNode node = new(0);\n    // Variables in the loop occupy O(1) space\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // Functions in the loop occupy O(1) space\n    for (int i = 0; i < n; i++) {\n        Function();\n    }\n}\n
      space_complexity.go
      /* Function */\nfunc function() int {\n    // Perform some operations...\n    return 0\n}\n\n/* Constant order */\nfunc spaceConstant(n int) {\n    // Constants, variables, objects occupy O(1) space\n    const a = 0\n    b := 0\n    nums := make([]int, 10000)\n    node := newNode(0)\n    // Variables in the loop occupy O(1) space\n    var c int\n    for i := 0; i < n; i++ {\n        c = 0\n    }\n    // Functions in the loop occupy O(1) space\n    for i := 0; i < n; i++ {\n        function()\n    }\n    b += 0\n    c += 0\n    nums[0] = 0\n    node.val = 0\n}\n
      space_complexity.swift
      /* Function */\n@discardableResult\nfunc function() -> Int {\n    // Perform some operations\n    return 0\n}\n\n/* Constant order */\nfunc constant(n: Int) {\n    // Constants, variables, objects occupy O(1) space\n    let a = 0\n    var b = 0\n    let nums = Array(repeating: 0, count: 10000)\n    let node = ListNode(x: 0)\n    // Variables in the loop occupy O(1) space\n    for _ in 0 ..< n {\n        let c = 0\n    }\n    // Functions in the loop occupy O(1) space\n    for _ in 0 ..< n {\n        function()\n    }\n}\n
      space_complexity.js
      /* Function */\nfunction constFunc() {\n    // Perform some operations\n    return 0;\n}\n\n/* Constant order */\nfunction constant(n) {\n    // Constants, variables, objects occupy O(1) space\n    const a = 0;\n    const b = 0;\n    const nums = new Array(10000);\n    const node = new ListNode(0);\n    // Variables in the loop occupy O(1) space\n    for (let i = 0; i < n; i++) {\n        const c = 0;\n    }\n    // Functions in the loop occupy O(1) space\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n
      space_complexity.ts
      /* Function */\nfunction constFunc(): number {\n    // Perform some operations\n    return 0;\n}\n\n/* Constant order */\nfunction constant(n: number): void {\n    // Constants, variables, objects occupy O(1) space\n    const a = 0;\n    const b = 0;\n    const nums = new Array(10000);\n    const node = new ListNode(0);\n    // Variables in the loop occupy O(1) space\n    for (let i = 0; i < n; i++) {\n        const c = 0;\n    }\n    // Functions in the loop occupy O(1) space\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n
      space_complexity.dart
      /* Function */\nint function() {\n  // Perform some operations\n  return 0;\n}\n\n/* Constant order */\nvoid constant(int n) {\n  // Constants, variables, objects occupy O(1) space\n  final int a = 0;\n  int b = 0;\n  List<int> nums = List.filled(10000, 0);\n  ListNode node = ListNode(0);\n  // Variables in the loop occupy O(1) space\n  for (var i = 0; i < n; i++) {\n    int c = 0;\n  }\n  // Functions in the loop occupy O(1) space\n  for (var i = 0; i < n; i++) {\n    function();\n  }\n}\n
      space_complexity.rs
      /* Function */\nfn function() -> i32 {\n    // Perform some operations\n    return 0;\n}\n\n/* Constant order */\n#[allow(unused)]\nfn constant(n: i32) {\n    // Constants, variables, objects occupy O(1) space\n    const A: i32 = 0;\n    let b = 0;\n    let nums = vec![0; 10000];\n    let node = ListNode::new(0);\n    // Variables in the loop occupy O(1) space\n    for i in 0..n {\n        let c = 0;\n    }\n    // Functions in the loop occupy O(1) space\n    for i in 0..n {\n        function();\n    }\n}\n
      space_complexity.c
      /* Function */\nint func() {\n    // Perform some operations\n    return 0;\n}\n\n/* Constant order */\nvoid constant(int n) {\n    // Constants, variables, objects occupy O(1) space\n    const int a = 0;\n    int b = 0;\n    int nums[1000];\n    ListNode *node = newListNode(0);\n    free(node);\n    // Variables in the loop occupy O(1) space\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // Functions in the loop occupy O(1) space\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n
      space_complexity.kt
      /* Function */\nfun function(): Int {\n    // Perform some operations\n    return 0\n}\n\n/* Constant order */\nfun constant(n: Int) {\n    // Constants, variables, objects occupy O(1) space\n    val a = 0\n    var b = 0\n    val nums = Array(10000) { 0 }\n    val node = ListNode(0)\n    // Variables in the loop occupy O(1) space\n    for (i in 0..<n) {\n        val c = 0\n    }\n    // Functions in the loop occupy O(1) space\n    for (i in 0..<n) {\n        function()\n    }\n}\n
      space_complexity.rb
      ### Function ###\ndef function\n  # Perform some operations\n  0\nend\n\n### Constant time ###\ndef constant(n)\n  # Constants, variables, objects occupy O(1) space\n  a = 0\n  nums = [0] * 10000\n  node = ListNode.new\n\n  # Variables in the loop occupy O(1) space\n  (0...n).each { c = 0 }\n  # Functions in the loop occupy O(1) space\n  (0...n).each { function }\nend\n
      "},{"location":"chapter_computational_complexity/space_complexity/#2-linear-order-on","title":"2. \u00a0 Linear Order \\(O(n)\\)","text":"

      Linear order is common in arrays, linked lists, stacks, queues, etc., where the number of elements is proportional to \\(n\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
      def linear(n: int):\n    \"\"\"Linear order\"\"\"\n    # A list of length n occupies O(n) space\n    nums = [0] * n\n    # A hash table of length n occupies O(n) space\n    hmap = dict[int, str]()\n    for i in range(n):\n        hmap[i] = str(i)\n
      space_complexity.cpp
      /* Linear order */\nvoid linear(int n) {\n    // Array of length n uses O(n) space\n    vector<int> nums(n);\n    // A list of length n occupies O(n) space\n    vector<ListNode> nodes;\n    for (int i = 0; i < n; i++) {\n        nodes.push_back(ListNode(i));\n    }\n    // A hash table of length n occupies O(n) space\n    unordered_map<int, string> map;\n    for (int i = 0; i < n; i++) {\n        map[i] = to_string(i);\n    }\n}\n
      space_complexity.java
      /* Linear order */\nvoid linear(int n) {\n    // Array of length n uses O(n) space\n    int[] nums = new int[n];\n    // A list of length n occupies O(n) space\n    List<ListNode> nodes = new ArrayList<>();\n    for (int i = 0; i < n; i++) {\n        nodes.add(new ListNode(i));\n    }\n    // A hash table of length n occupies O(n) space\n    Map<Integer, String> map = new HashMap<>();\n    for (int i = 0; i < n; i++) {\n        map.put(i, String.valueOf(i));\n    }\n}\n
      space_complexity.cs
      /* Linear order */\nvoid Linear(int n) {\n    // Array of length n uses O(n) space\n    int[] nums = new int[n];\n    // A list of length n occupies O(n) space\n    List<ListNode> nodes = [];\n    for (int i = 0; i < n; i++) {\n        nodes.Add(new ListNode(i));\n    }\n    // A hash table of length n occupies O(n) space\n    Dictionary<int, string> map = [];\n    for (int i = 0; i < n; i++) {\n        map.Add(i, i.ToString());\n    }\n}\n
      space_complexity.go
      /* Linear order */\nfunc spaceLinear(n int) {\n    // Array of length n uses O(n) space\n    _ = make([]int, n)\n    // A list of length n occupies O(n) space\n    var nodes []*node\n    for i := 0; i < n; i++ {\n        nodes = append(nodes, newNode(i))\n    }\n    // A hash table of length n occupies O(n) space\n    m := make(map[int]string, n)\n    for i := 0; i < n; i++ {\n        m[i] = strconv.Itoa(i)\n    }\n}\n
      space_complexity.swift
      /* Linear order */\nfunc linear(n: Int) {\n    // Array of length n uses O(n) space\n    let nums = Array(repeating: 0, count: n)\n    // A list of length n occupies O(n) space\n    let nodes = (0 ..< n).map { ListNode(x: $0) }\n    // A hash table of length n occupies O(n) space\n    let map = Dictionary(uniqueKeysWithValues: (0 ..< n).map { ($0, \"\\($0)\") })\n}\n
      space_complexity.js
      /* Linear order */\nfunction linear(n) {\n    // Array of length n uses O(n) space\n    const nums = new Array(n);\n    // A list of length n occupies O(n) space\n    const nodes = [];\n    for (let i = 0; i < n; i++) {\n        nodes.push(new ListNode(i));\n    }\n    // A hash table of length n occupies O(n) space\n    const map = new Map();\n    for (let i = 0; i < n; i++) {\n        map.set(i, i.toString());\n    }\n}\n
      space_complexity.ts
      /* Linear order */\nfunction linear(n: number): void {\n    // Array of length n uses O(n) space\n    const nums = new Array(n);\n    // A list of length n occupies O(n) space\n    const nodes: ListNode[] = [];\n    for (let i = 0; i < n; i++) {\n        nodes.push(new ListNode(i));\n    }\n    // A hash table of length n occupies O(n) space\n    const map = new Map();\n    for (let i = 0; i < n; i++) {\n        map.set(i, i.toString());\n    }\n}\n
      space_complexity.dart
      /* Linear order */\nvoid linear(int n) {\n  // Array of length n uses O(n) space\n  List<int> nums = List.filled(n, 0);\n  // A list of length n occupies O(n) space\n  List<ListNode> nodes = [];\n  for (var i = 0; i < n; i++) {\n    nodes.add(ListNode(i));\n  }\n  // A hash table of length n occupies O(n) space\n  Map<int, String> map = HashMap();\n  for (var i = 0; i < n; i++) {\n    map.putIfAbsent(i, () => i.toString());\n  }\n}\n
      space_complexity.rs
      /* Linear order */\n#[allow(unused)]\nfn linear(n: i32) {\n    // Array of length n uses O(n) space\n    let mut nums = vec![0; n as usize];\n    // A list of length n occupies O(n) space\n    let mut nodes = Vec::new();\n    for i in 0..n {\n        nodes.push(ListNode::new(i))\n    }\n    // A hash table of length n occupies O(n) space\n    let mut map = HashMap::new();\n    for i in 0..n {\n        map.insert(i, i.to_string());\n    }\n}\n
      space_complexity.c
      /* Hash table */\ntypedef struct {\n    int key;\n    int val;\n    UT_hash_handle hh; // Implemented using uthash.h\n} HashTable;\n\n/* Linear order */\nvoid linear(int n) {\n    // Array of length n uses O(n) space\n    int *nums = malloc(sizeof(int) * n);\n    free(nums);\n\n    // A list of length n occupies O(n) space\n    ListNode **nodes = malloc(sizeof(ListNode *) * n);\n    for (int i = 0; i < n; i++) {\n        nodes[i] = newListNode(i);\n    }\n    // Memory release\n    for (int i = 0; i < n; i++) {\n        free(nodes[i]);\n    }\n    free(nodes);\n\n    // A hash table of length n occupies O(n) space\n    HashTable *h = NULL;\n    for (int i = 0; i < n; i++) {\n        HashTable *tmp = malloc(sizeof(HashTable));\n        tmp->key = i;\n        tmp->val = i;\n        HASH_ADD_INT(h, key, tmp);\n    }\n\n    // Memory release\n    HashTable *curr, *tmp;\n    HASH_ITER(hh, h, curr, tmp) {\n        HASH_DEL(h, curr);\n        free(curr);\n    }\n}\n
      space_complexity.kt
      /* Linear order */\nfun linear(n: Int) {\n    // Array of length n uses O(n) space\n    val nums = Array(n) { 0 }\n    // A list of length n occupies O(n) space\n    val nodes = mutableListOf<ListNode>()\n    for (i in 0..<n) {\n        nodes.add(ListNode(i))\n    }\n    // A hash table of length n occupies O(n) space\n    val map = mutableMapOf<Int, String>()\n    for (i in 0..<n) {\n        map[i] = i.toString()\n    }\n}\n
      space_complexity.rb
      ### Linear time ###\ndef linear(n)\n  # A list of length n occupies O(n) space\n  nums = Array.new(n, 0)\n\n  # A hash table of length n occupies O(n) space\n  hmap = {}\n  for i in 0...n\n    hmap[i] = i.to_s\n  end\nend\n

      As shown in the following figure, the recursion depth of this function is \\(n\\), meaning that there are \\(n\\) unreturned linear_recur() functions existing simultaneously, using \\(O(n)\\) stack frame space:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
      def linear_recur(n: int):\n    \"\"\"Linear order (recursive implementation)\"\"\"\n    print(\"Recursion n =\", n)\n    if n == 1:\n        return\n    linear_recur(n - 1)\n
      space_complexity.cpp
      /* Linear order (recursive implementation) */\nvoid linearRecur(int n) {\n    cout << \"Recursion n = \" << n << endl;\n    if (n == 1)\n        return;\n    linearRecur(n - 1);\n}\n
      space_complexity.java
      /* Linear order (recursive implementation) */\nvoid linearRecur(int n) {\n    System.out.println(\"Recursion n = \" + n);\n    if (n == 1)\n        return;\n    linearRecur(n - 1);\n}\n
      space_complexity.cs
      /* Linear order (recursive implementation) */\nvoid LinearRecur(int n) {\n    Console.WriteLine(\"Recursion n = \" + n);\n    if (n == 1) return;\n    LinearRecur(n - 1);\n}\n
      space_complexity.go
      /* Linear order (recursive implementation) */\nfunc spaceLinearRecur(n int) {\n    fmt.Println(\"Recursion n =\", n)\n    if n == 1 {\n        return\n    }\n    spaceLinearRecur(n - 1)\n}\n
      space_complexity.swift
      /* Linear order (recursive implementation) */\nfunc linearRecur(n: Int) {\n    print(\"Recursion n = \\(n)\")\n    if n == 1 {\n        return\n    }\n    linearRecur(n: n - 1)\n}\n
      space_complexity.js
      /* Linear order (recursive implementation) */\nfunction linearRecur(n) {\n    console.log(`Recursion n = ${n}`);\n    if (n === 1) return;\n    linearRecur(n - 1);\n}\n
      space_complexity.ts
      /* Linear order (recursive implementation) */\nfunction linearRecur(n: number): void {\n    console.log(`Recursion n = ${n}`);\n    if (n === 1) return;\n    linearRecur(n - 1);\n}\n
      space_complexity.dart
      /* Linear order (recursive implementation) */\nvoid linearRecur(int n) {\n  print('Recursion n = $n');\n  if (n == 1) return;\n  linearRecur(n - 1);\n}\n
      space_complexity.rs
      /* Linear order (recursive implementation) */\nfn linear_recur(n: i32) {\n    println!(\"Recursion n = {}\", n);\n    if n == 1 {\n        return;\n    };\n    linear_recur(n - 1);\n}\n
      space_complexity.c
      /* Linear order (recursive implementation) */\nvoid linearRecur(int n) {\n    printf(\"Recursion n = %d\\r\\n\", n);\n    if (n == 1)\n        return;\n    linearRecur(n - 1);\n}\n
      space_complexity.kt
      /* Linear order (recursive implementation) */\nfun linearRecur(n: Int) {\n    println(\"Recursion n = $n\")\n    if (n == 1)\n        return\n    linearRecur(n - 1)\n}\n
      space_complexity.rb
      ### Linear space (recursive) ###\ndef linear_recur(n)\n  puts \"Recursion n = #{n}\"\n  return if n == 1\n  linear_recur(n - 1)\nend\n

      Figure 2-17 \u00a0 Linear order space complexity generated by recursive function

      "},{"location":"chapter_computational_complexity/space_complexity/#3-quadratic-order-on2","title":"3. \u00a0 Quadratic Order \\(O(n^2)\\)","text":"

      Quadratic order is common in matrices and graphs, where the number of elements is quadratically related to \\(n\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
      def quadratic(n: int):\n    \"\"\"Quadratic order\"\"\"\n    # A 2D list occupies O(n^2) space\n    num_matrix = [[0] * n for _ in range(n)]\n
      space_complexity.cpp
      /* Exponential order */\nvoid quadratic(int n) {\n    // 2D list uses O(n^2) space\n    vector<vector<int>> numMatrix;\n    for (int i = 0; i < n; i++) {\n        vector<int> tmp;\n        for (int j = 0; j < n; j++) {\n            tmp.push_back(0);\n        }\n        numMatrix.push_back(tmp);\n    }\n}\n
      space_complexity.java
      /* Exponential order */\nvoid quadratic(int n) {\n    // Matrix uses O(n^2) space\n    int[][] numMatrix = new int[n][n];\n    // 2D list uses O(n^2) space\n    List<List<Integer>> numList = new ArrayList<>();\n    for (int i = 0; i < n; i++) {\n        List<Integer> tmp = new ArrayList<>();\n        for (int j = 0; j < n; j++) {\n            tmp.add(0);\n        }\n        numList.add(tmp);\n    }\n}\n
      space_complexity.cs
      /* Exponential order */\nvoid Quadratic(int n) {\n    // Matrix uses O(n^2) space\n    int[,] numMatrix = new int[n, n];\n    // 2D list uses O(n^2) space\n    List<List<int>> numList = [];\n    for (int i = 0; i < n; i++) {\n        List<int> tmp = [];\n        for (int j = 0; j < n; j++) {\n            tmp.Add(0);\n        }\n        numList.Add(tmp);\n    }\n}\n
      space_complexity.go
      /* Exponential order */\nfunc spaceQuadratic(n int) {\n    // Matrix uses O(n^2) space\n    numMatrix := make([][]int, n)\n    for i := 0; i < n; i++ {\n        numMatrix[i] = make([]int, n)\n    }\n}\n
      space_complexity.swift
      /* Exponential order */\nfunc quadratic(n: Int) {\n    // 2D list uses O(n^2) space\n    let numList = Array(repeating: Array(repeating: 0, count: n), count: n)\n}\n
      space_complexity.js
      /* Exponential order */\nfunction quadratic(n) {\n    // Matrix uses O(n^2) space\n    const numMatrix = Array(n)\n        .fill(null)\n        .map(() => Array(n).fill(null));\n    // 2D list uses O(n^2) space\n    const numList = [];\n    for (let i = 0; i < n; i++) {\n        const tmp = [];\n        for (let j = 0; j < n; j++) {\n            tmp.push(0);\n        }\n        numList.push(tmp);\n    }\n}\n
      space_complexity.ts
      /* Exponential order */\nfunction quadratic(n: number): void {\n    // Matrix uses O(n^2) space\n    const numMatrix = Array(n)\n        .fill(null)\n        .map(() => Array(n).fill(null));\n    // 2D list uses O(n^2) space\n    const numList = [];\n    for (let i = 0; i < n; i++) {\n        const tmp = [];\n        for (let j = 0; j < n; j++) {\n            tmp.push(0);\n        }\n        numList.push(tmp);\n    }\n}\n
      space_complexity.dart
      /* Exponential order */\nvoid quadratic(int n) {\n  // Matrix uses O(n^2) space\n  List<List<int>> numMatrix = List.generate(n, (_) => List.filled(n, 0));\n  // 2D list uses O(n^2) space\n  List<List<int>> numList = [];\n  for (var i = 0; i < n; i++) {\n    List<int> tmp = [];\n    for (int j = 0; j < n; j++) {\n      tmp.add(0);\n    }\n    numList.add(tmp);\n  }\n}\n
      space_complexity.rs
      /* Exponential order */\n#[allow(unused)]\nfn quadratic(n: i32) {\n    // Matrix uses O(n^2) space\n    let num_matrix = vec![vec![0; n as usize]; n as usize];\n    // 2D list uses O(n^2) space\n    let mut num_list = Vec::new();\n    for i in 0..n {\n        let mut tmp = Vec::new();\n        for j in 0..n {\n            tmp.push(0);\n        }\n        num_list.push(tmp);\n    }\n}\n
      space_complexity.c
      /* Exponential order */\nvoid quadratic(int n) {\n    // 2D list uses O(n^2) space\n    int **numMatrix = malloc(sizeof(int *) * n);\n    for (int i = 0; i < n; i++) {\n        int *tmp = malloc(sizeof(int) * n);\n        for (int j = 0; j < n; j++) {\n            tmp[j] = 0;\n        }\n        numMatrix[i] = tmp;\n    }\n\n    // Memory release\n    for (int i = 0; i < n; i++) {\n        free(numMatrix[i]);\n    }\n    free(numMatrix);\n}\n
      space_complexity.kt
      /* Exponential order */\nfun quadratic(n: Int) {\n    // Matrix uses O(n^2) space\n    val numMatrix = arrayOfNulls<Array<Int>?>(n)\n    // 2D list uses O(n^2) space\n    val numList = mutableListOf<MutableList<Int>>()\n    for (i in 0..<n) {\n        val tmp = mutableListOf<Int>()\n        for (j in 0..<n) {\n            tmp.add(0)\n        }\n        numList.add(tmp)\n    }\n}\n
      space_complexity.rb
      ### Quadratic time ###\ndef quadratic(n)\n  # 2D list uses O(n^2) space\n  Array.new(n) { Array.new(n, 0) }\nend\n

      As shown in the following figure, the recursion depth of this function is \\(n\\), and an array is initialized in each recursive function with lengths of \\(n\\), \\(n-1\\), \\(\\dots\\), \\(2\\), \\(1\\), with an average length of \\(n / 2\\), thus occupying \\(O(n^2)\\) space overall:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
      def quadratic_recur(n: int) -> int:\n    \"\"\"Quadratic order (recursive implementation)\"\"\"\n    if n <= 0:\n        return 0\n    # Array nums length is n, n-1, ..., 2, 1\n    nums = [0] * n\n    return quadratic_recur(n - 1)\n
      space_complexity.cpp
      /* Quadratic order (recursive implementation) */\nint quadraticRecur(int n) {\n    if (n <= 0)\n        return 0;\n    vector<int> nums(n);\n    cout << \"In recursion n = \" << n << \", nums length = \" << nums.size() << endl;\n    return quadraticRecur(n - 1);\n}\n
      space_complexity.java
      /* Quadratic order (recursive implementation) */\nint quadraticRecur(int n) {\n    if (n <= 0)\n        return 0;\n    // Array nums has length n, n-1, ..., 2, 1\n    int[] nums = new int[n];\n    System.out.println(\"In recursion n = \" + n + \", nums length = \" + nums.length);\n    return quadraticRecur(n - 1);\n}\n
      space_complexity.cs
      /* Quadratic order (recursive implementation) */\nint QuadraticRecur(int n) {\n    if (n <= 0) return 0;\n    int[] nums = new int[n];\n    Console.WriteLine(\"Recursion n = \" + n + \", nums length = \" + nums.Length);\n    return QuadraticRecur(n - 1);\n}\n
      space_complexity.go
      /* Quadratic order (recursive implementation) */\nfunc spaceQuadraticRecur(n int) int {\n    if n <= 0 {\n        return 0\n    }\n    nums := make([]int, n)\n    fmt.Printf(\"In recursion n = %d, nums length = %d \\n\", n, len(nums))\n    return spaceQuadraticRecur(n - 1)\n}\n
      space_complexity.swift
      /* Quadratic order (recursive implementation) */\n@discardableResult\nfunc quadraticRecur(n: Int) -> Int {\n    if n <= 0 {\n        return 0\n    }\n    // Array nums has length n, n-1, ..., 2, 1\n    let nums = Array(repeating: 0, count: n)\n    print(\"In recursion n = \\(n), nums length = \\(nums.count)\")\n    return quadraticRecur(n: n - 1)\n}\n
      space_complexity.js
      /* Quadratic order (recursive implementation) */\nfunction quadraticRecur(n) {\n    if (n <= 0) return 0;\n    const nums = new Array(n);\n    console.log(`In recursion n = ${n}, nums length = ${nums.length}`);\n    return quadraticRecur(n - 1);\n}\n
      space_complexity.ts
      /* Quadratic order (recursive implementation) */\nfunction quadraticRecur(n: number): number {\n    if (n <= 0) return 0;\n    const nums = new Array(n);\n    console.log(`In recursion n = ${n}, nums length = ${nums.length}`);\n    return quadraticRecur(n - 1);\n}\n
      space_complexity.dart
      /* Quadratic order (recursive implementation) */\nint quadraticRecur(int n) {\n  if (n <= 0) return 0;\n  List<int> nums = List.filled(n, 0);\n  print('In recursion n = $n, nums length = ${nums.length}');\n  return quadraticRecur(n - 1);\n}\n
      space_complexity.rs
      /* Quadratic order (recursive implementation) */\nfn quadratic_recur(n: i32) -> i32 {\n    if n <= 0 {\n        return 0;\n    };\n    // Array nums has length n, n-1, ..., 2, 1\n    let nums = vec![0; n as usize];\n    println!(\"In recursion n = {}, nums length = {}\", n, nums.len());\n    return quadratic_recur(n - 1);\n}\n
      space_complexity.c
      /* Quadratic order (recursive implementation) */\nint quadraticRecur(int n) {\n    if (n <= 0)\n        return 0;\n    int *nums = malloc(sizeof(int) * n);\n    printf(\"In recursion n = %d, nums length = %d\\r\\n\", n, n);\n    int res = quadraticRecur(n - 1);\n    free(nums);\n    return res;\n}\n
      space_complexity.kt
      /* Quadratic order (recursive implementation) */\ntailrec fun quadraticRecur(n: Int): Int {\n    if (n <= 0)\n        return 0\n    // Array nums has length n, n-1, ..., 2, 1\n    val nums = Array(n) { 0 }\n    println(\"In recursion n = $n, nums length = ${nums.size}\")\n    return quadraticRecur(n - 1)\n}\n
      space_complexity.rb
      ### Quadratic space (recursive) ###\ndef quadratic_recur(n)\n  return 0 unless n > 0\n\n  # Array nums has length n, n-1, ..., 2, 1\n  nums = Array.new(n, 0)\n  quadratic_recur(n - 1)\nend\n

      Figure 2-18 \u00a0 Quadratic order space complexity generated by recursive function

      "},{"location":"chapter_computational_complexity/space_complexity/#4-exponential-order-o2n","title":"4. \u00a0 Exponential Order \\(O(2^n)\\)","text":"

      Exponential order is common in binary trees. Observe the following figure: a \"full binary tree\" with \\(n\\) levels has \\(2^n - 1\\) nodes, occupying \\(O(2^n)\\) space:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
      def build_tree(n: int) -> TreeNode | None:\n    \"\"\"Exponential order (build full binary tree)\"\"\"\n    if n == 0:\n        return None\n    root = TreeNode(0)\n    root.left = build_tree(n - 1)\n    root.right = build_tree(n - 1)\n    return root\n
      space_complexity.cpp
      /* Driver Code */\nTreeNode *buildTree(int n) {\n    if (n == 0)\n        return nullptr;\n    TreeNode *root = new TreeNode(0);\n    root->left = buildTree(n - 1);\n    root->right = buildTree(n - 1);\n    return root;\n}\n
      space_complexity.java
      /* Driver Code */\nTreeNode buildTree(int n) {\n    if (n == 0)\n        return null;\n    TreeNode root = new TreeNode(0);\n    root.left = buildTree(n - 1);\n    root.right = buildTree(n - 1);\n    return root;\n}\n
      space_complexity.cs
      /* Driver Code */\nTreeNode? BuildTree(int n) {\n    if (n == 0) return null;\n    TreeNode root = new(0) {\n        left = BuildTree(n - 1),\n        right = BuildTree(n - 1)\n    };\n    return root;\n}\n
      space_complexity.go
      /* Driver Code */\nfunc buildTree(n int) *TreeNode {\n    if n == 0 {\n        return nil\n    }\n    root := NewTreeNode(0)\n    root.Left = buildTree(n - 1)\n    root.Right = buildTree(n - 1)\n    return root\n}\n
      space_complexity.swift
      /* Driver Code */\nfunc buildTree(n: Int) -> TreeNode? {\n    if n == 0 {\n        return nil\n    }\n    let root = TreeNode(x: 0)\n    root.left = buildTree(n: n - 1)\n    root.right = buildTree(n: n - 1)\n    return root\n}\n
      space_complexity.js
      /* Driver Code */\nfunction buildTree(n) {\n    if (n === 0) return null;\n    const root = new TreeNode(0);\n    root.left = buildTree(n - 1);\n    root.right = buildTree(n - 1);\n    return root;\n}\n
      space_complexity.ts
      /* Driver Code */\nfunction buildTree(n: number): TreeNode | null {\n    if (n === 0) return null;\n    const root = new TreeNode(0);\n    root.left = buildTree(n - 1);\n    root.right = buildTree(n - 1);\n    return root;\n}\n
      space_complexity.dart
      /* Driver Code */\nTreeNode? buildTree(int n) {\n  if (n == 0) return null;\n  TreeNode root = TreeNode(0);\n  root.left = buildTree(n - 1);\n  root.right = buildTree(n - 1);\n  return root;\n}\n
      space_complexity.rs
      /* Driver Code */\nfn build_tree(n: i32) -> Option<Rc<RefCell<TreeNode>>> {\n    if n == 0 {\n        return None;\n    };\n    let root = TreeNode::new(0);\n    root.borrow_mut().left = build_tree(n - 1);\n    root.borrow_mut().right = build_tree(n - 1);\n    return Some(root);\n}\n
      space_complexity.c
      /* Driver Code */\nTreeNode *buildTree(int n) {\n    if (n == 0)\n        return NULL;\n    TreeNode *root = newTreeNode(0);\n    root->left = buildTree(n - 1);\n    root->right = buildTree(n - 1);\n    return root;\n}\n
      space_complexity.kt
      /* Driver Code */\nfun buildTree(n: Int): TreeNode? {\n    if (n == 0)\n        return null\n    val root = TreeNode(0)\n    root.left = buildTree(n - 1)\n    root.right = buildTree(n - 1)\n    return root\n}\n
      space_complexity.rb
      ### Exponential space (build full binary tree) ###\ndef build_tree(n)\n  return if n == 0\n\n  TreeNode.new.tap do |root|\n    root.left = build_tree(n - 1)\n    root.right = build_tree(n - 1)\n  end\nend\n

      Figure 2-19 \u00a0 Exponential order space complexity generated by full binary tree

      "},{"location":"chapter_computational_complexity/space_complexity/#5-logarithmic-order-olog-n","title":"5. \u00a0 Logarithmic Order \\(O(\\log n)\\)","text":"

      Logarithmic order is common in divide-and-conquer algorithms. For example, merge sort: given an input array of length \\(n\\), each recursion divides the array in half from the midpoint, forming a recursion tree of height \\(\\log n\\), using \\(O(\\log n)\\) stack frame space.

      Another example is converting a number to a string. Given a positive integer \\(n\\), it has \\(\\lfloor \\log_{10} n \\rfloor + 1\\) digits, i.e., the corresponding string length is \\(\\lfloor \\log_{10} n \\rfloor + 1\\), so the space complexity is \\(O(\\log_{10} n + 1) = O(\\log n)\\).

      "},{"location":"chapter_computational_complexity/space_complexity/#244-trading-time-for-space","title":"2.4.4 \u00a0 Trading Time for Space","text":"

      Ideally, we hope that both the time complexity and space complexity of an algorithm can reach optimal. However, in practice, optimizing both time complexity and space complexity simultaneously is usually very difficult.

      Reducing time complexity usually comes at the cost of increasing space complexity, and vice versa. The approach of sacrificing memory space to improve algorithm execution speed is called \"trading space for time\"; conversely, it is called \"trading time for space\".

      The choice of which approach depends on which aspect we value more. In most cases, time is more precious than space, so \"trading space for time\" is usually the more common strategy. Of course, when the data volume is very large, controlling space complexity is also very important.

      "},{"location":"chapter_computational_complexity/summary/","title":"2.5 \u00a0 Summary","text":""},{"location":"chapter_computational_complexity/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"

      Algorithm Efficiency Assessment

      • Time efficiency and space efficiency are the two primary evaluation metrics for measuring algorithm performance.
      • We can evaluate algorithm efficiency through actual testing, but it is difficult to eliminate the influence of the testing environment, and it consumes substantial computational resources.
      • Complexity analysis can eliminate the drawbacks of actual testing, with results applicable to all running platforms, and it can reveal algorithm efficiency under different data scales.

      Time Complexity

      • Time complexity is used to measure the trend of algorithm runtime as data volume increases. It can effectively evaluate algorithm efficiency, but may fail in certain situations, such as when the input data volume is small or when time complexities are identical, making it impossible to precisely compare algorithm efficiency.
      • Worst-case time complexity is represented using Big \\(O\\) notation, corresponding to the asymptotic upper bound of a function, reflecting the growth level of the number of operations \\(T(n)\\) as \\(n\\) approaches positive infinity.
      • Deriving time complexity involves two steps: first, counting the number of operations, then determining the asymptotic upper bound.
      • Common time complexities arranged from low to high include \\(O(1)\\), \\(O(\\log n)\\), \\(O(n)\\), \\(O(n \\log n)\\), \\(O(n^2)\\), \\(O(2^n)\\), and \\(O(n!)\\).
      • The time complexity of some algorithms is not fixed, but rather depends on the distribution of input data. Time complexity is divided into worst-case, best-case, and average-case time complexity. Best-case time complexity is rarely used because input data generally needs to satisfy strict conditions to achieve the best case.
      • Average time complexity reflects the algorithm's runtime efficiency under random data input, and is closest to the algorithm's performance in practical applications. Calculating average time complexity requires statistical analysis of input data distribution and the combined mathematical expectation.

      Space Complexity

      • Space complexity serves a similar purpose to time complexity, used to measure the trend of algorithm memory usage as data volume increases.
      • The memory space related to algorithm execution can be divided into input space, temporary space, and output space. Typically, input space is not included in space complexity calculations. Temporary space can be divided into temporary data, stack frame space, and instruction space, where stack frame space usually affects space complexity only in recursive functions.
      • We typically only focus on worst-case space complexity, which is the space complexity of an algorithm under worst-case input data and worst-case runtime.
      • Common space complexities arranged from low to high include \\(O(1)\\), \\(O(\\log n)\\), \\(O(n)\\), \\(O(n^2)\\), and \\(O(2^n)\\).
      "},{"location":"chapter_computational_complexity/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

      Q: Is the space complexity of tail recursion \\(O(1)\\)?

      Theoretically, the space complexity of tail recursive functions can be optimized to \\(O(1)\\). However, most programming languages (such as Java, Python, C++, Go, C#, etc.) do not support automatic tail recursion optimization, so the space complexity is generally considered to be \\(O(n)\\).

      Q: What is the difference between the terms function and method?

      A function can be executed independently, with all parameters passed explicitly. A method is associated with an object, is implicitly passed to the object that invokes it, and can operate on data contained in class instances.

      The following examples use several common programming languages for illustration.

      • C is a procedural programming language without object-oriented concepts, so it only has functions. However, we can simulate object-oriented programming by creating structures (struct), and functions associated with structures are equivalent to methods in other programming languages.
      • Java and C# are object-oriented programming languages where code blocks (methods) are typically part of a class. Static methods behave like functions because they are bound to the class and cannot access specific instance variables.
      • C++ and Python support both procedural programming (functions) and object-oriented programming (methods).

      Q: Does the diagram for \"common space complexity types\" reflect the absolute size of occupied space?

      No, the diagram shows space complexity, which reflects growth trends rather than the absolute size of occupied space.

      Assuming \\(n = 8\\), you might find that the values of each curve do not correspond to the functions. This is because each curve contains a constant term used to compress the value range into a visually comfortable range.

      In practice, because we generally do not know what the \"constant term\" complexity of each method is, we usually cannot select the optimal solution for \\(n = 8\\) based on complexity alone. But for \\(n = 8^5\\), the choice is straightforward, as the growth trend already dominates.

      Q: Are there situations where algorithms are designed to sacrifice time (or space) based on actual use cases?

      In practical applications, most situations choose to sacrifice space for time. For example, with database indexes, we typically choose to build B+ trees or hash indexes, occupying substantial memory space in exchange for efficient queries of \\(O(\\log n)\\) or even \\(O(1)\\).

      In scenarios where space resources are precious, time may be sacrificed for space. For example, in embedded development, device memory is precious, and engineers may forgo using hash tables and choose to use array sequential search to save memory usage, at the cost of slower searches.

      "},{"location":"chapter_computational_complexity/time_complexity/","title":"2.3 \u00a0 Time Complexity","text":"

      Runtime can intuitively and accurately reflect the efficiency of an algorithm. If we want to accurately estimate the runtime of a piece of code, how should we proceed?

      1. Determine the running platform, including hardware configuration, programming language, system environment, etc., as these factors all affect code execution efficiency.
      2. Evaluate the runtime required for various computational operations, for example, an addition operation + requires 1 ns, a multiplication operation * requires 10 ns, a print operation print() requires 5 ns, etc.
      3. Count all computational operations in the code, and sum the execution times of all operations to obtain the runtime.

      For example, in the following code, the input data size is \\(n\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      # On a certain running platform\ndef algorithm(n: int):\n    a = 2      # 1 ns\n    a = a + 1  # 1 ns\n    a = a * 2  # 10 ns\n    # Loop n times\n    for _ in range(n):  # 1 ns\n        print(0)        # 5 ns\n
      // On a certain running platform\nvoid algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // Loop n times\n    for (int i = 0; i < n; i++) {  // 1 ns\n        cout << 0 << endl;         // 5 ns\n    }\n}\n
      // On a certain running platform\nvoid algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // Loop n times\n    for (int i = 0; i < n; i++) {  // 1 ns\n        System.out.println(0);     // 5 ns\n    }\n}\n
      // On a certain running platform\nvoid Algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // Loop n times\n    for (int i = 0; i < n; i++) {  // 1 ns\n        Console.WriteLine(0);      // 5 ns\n    }\n}\n
      // On a certain running platform\nfunc algorithm(n int) {\n    a := 2     // 1 ns\n    a = a + 1  // 1 ns\n    a = a * 2  // 10 ns\n    // Loop n times\n    for i := 0; i < n; i++ {  // 1 ns\n        fmt.Println(a)        // 5 ns\n    }\n}\n
      // On a certain running platform\nfunc algorithm(n: Int) {\n    var a = 2 // 1 ns\n    a = a + 1 // 1 ns\n    a = a * 2 // 10 ns\n    // Loop n times\n    for _ in 0 ..< n { // 1 ns\n        print(0) // 5 ns\n    }\n}\n
      // On a certain running platform\nfunction algorithm(n) {\n    var a = 2; // 1 ns\n    a = a + 1; // 1 ns\n    a = a * 2; // 10 ns\n    // Loop n times\n    for(let i = 0; i < n; i++) { // 1 ns\n        console.log(0); // 5 ns\n    }\n}\n
      // On a certain running platform\nfunction algorithm(n: number): void {\n    var a: number = 2; // 1 ns\n    a = a + 1; // 1 ns\n    a = a * 2; // 10 ns\n    // Loop n times\n    for(let i = 0; i < n; i++) { // 1 ns\n        console.log(0); // 5 ns\n    }\n}\n
      // On a certain running platform\nvoid algorithm(int n) {\n  int a = 2; // 1 ns\n  a = a + 1; // 1 ns\n  a = a * 2; // 10 ns\n  // Loop n times\n  for (int i = 0; i < n; i++) { // 1 ns\n    print(0); // 5 ns\n  }\n}\n
      // On a certain running platform\nfn algorithm(n: i32) {\n    let mut a = 2;      // 1 ns\n    a = a + 1;          // 1 ns\n    a = a * 2;          // 10 ns\n    // Loop n times\n    for _ in 0..n {     // 1 ns\n        println!(\"{}\", 0);  // 5 ns\n    }\n}\n
      // On a certain running platform\nvoid algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // Loop n times\n    for (int i = 0; i < n; i++) {   // 1 ns\n        printf(\"%d\", 0);            // 5 ns\n    }\n}\n
      // On a certain running platform\nfun algorithm(n: Int) {\n    var a = 2 // 1 ns\n    a = a + 1 // 1 ns\n    a = a * 2 // 10 ns\n    // Loop n times\n    for (i in 0..<n) {  // 1 ns\n        println(0)      // 5 ns\n    }\n}\n
      # On a certain running platform\ndef algorithm(n)\n    a = 2       # 1 ns\n    a = a + 1   # 1 ns\n    a = a * 2   # 10 ns\n    # Loop n times\n    (0...n).each do # 1 ns\n        puts 0      # 5 ns\n    end\nend\n

      According to the above method, the algorithm's runtime can be obtained as \\((6n + 12)\\) ns:

      \\[ 1 + 1 + 10 + (1 + 5) \\times n = 6n + 12 \\]

      In reality, however, counting an algorithm's runtime is neither reasonable nor realistic. First, we do not want to tie the estimated time to the running platform, because algorithms need to run on various different platforms. Second, it is difficult to know the runtime of each type of operation, which brings great difficulty to the estimation process.

      "},{"location":"chapter_computational_complexity/time_complexity/#231-counting-time-growth-trends","title":"2.3.1 \u00a0 Counting Time Growth Trends","text":"

      Time complexity analysis does not count the algorithm's runtime, but rather counts the growth trend of the algorithm's runtime as the data volume increases.

      The concept of \"time growth trend\" is rather abstract; let us understand it through an example. Suppose the input data size is \\(n\\), and given three algorithms A, B, and C:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      # Time complexity of algorithm A: constant order\ndef algorithm_A(n: int):\n    print(0)\n# Time complexity of algorithm B: linear order\ndef algorithm_B(n: int):\n    for _ in range(n):\n        print(0)\n# Time complexity of algorithm C: constant order\ndef algorithm_C(n: int):\n    for _ in range(1000000):\n        print(0)\n
      // Time complexity of algorithm A: constant order\nvoid algorithm_A(int n) {\n    cout << 0 << endl;\n}\n// Time complexity of algorithm B: linear order\nvoid algorithm_B(int n) {\n    for (int i = 0; i < n; i++) {\n        cout << 0 << endl;\n    }\n}\n// Time complexity of algorithm C: constant order\nvoid algorithm_C(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        cout << 0 << endl;\n    }\n}\n
      // Time complexity of algorithm A: constant order\nvoid algorithm_A(int n) {\n    System.out.println(0);\n}\n// Time complexity of algorithm B: linear order\nvoid algorithm_B(int n) {\n    for (int i = 0; i < n; i++) {\n        System.out.println(0);\n    }\n}\n// Time complexity of algorithm C: constant order\nvoid algorithm_C(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        System.out.println(0);\n    }\n}\n
      // Time complexity of algorithm A: constant order\nvoid AlgorithmA(int n) {\n    Console.WriteLine(0);\n}\n// Time complexity of algorithm B: linear order\nvoid AlgorithmB(int n) {\n    for (int i = 0; i < n; i++) {\n        Console.WriteLine(0);\n    }\n}\n// Time complexity of algorithm C: constant order\nvoid AlgorithmC(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        Console.WriteLine(0);\n    }\n}\n
      // Time complexity of algorithm A: constant order\nfunc algorithm_A(n int) {\n    fmt.Println(0)\n}\n// Time complexity of algorithm B: linear order\nfunc algorithm_B(n int) {\n    for i := 0; i < n; i++ {\n        fmt.Println(0)\n    }\n}\n// Time complexity of algorithm C: constant order\nfunc algorithm_C(n int) {\n    for i := 0; i < 1000000; i++ {\n        fmt.Println(0)\n    }\n}\n
      // Time complexity of algorithm A: constant order\nfunc algorithmA(n: Int) {\n    print(0)\n}\n\n// Time complexity of algorithm B: linear order\nfunc algorithmB(n: Int) {\n    for _ in 0 ..< n {\n        print(0)\n    }\n}\n\n// Time complexity of algorithm C: constant order\nfunc algorithmC(n: Int) {\n    for _ in 0 ..< 1_000_000 {\n        print(0)\n    }\n}\n
      // Time complexity of algorithm A: constant order\nfunction algorithm_A(n) {\n    console.log(0);\n}\n// Time complexity of algorithm B: linear order\nfunction algorithm_B(n) {\n    for (let i = 0; i < n; i++) {\n        console.log(0);\n    }\n}\n// Time complexity of algorithm C: constant order\nfunction algorithm_C(n) {\n    for (let i = 0; i < 1000000; i++) {\n        console.log(0);\n    }\n}\n
      // Time complexity of algorithm A: constant order\nfunction algorithm_A(n: number): void {\n    console.log(0);\n}\n// Time complexity of algorithm B: linear order\nfunction algorithm_B(n: number): void {\n    for (let i = 0; i < n; i++) {\n        console.log(0);\n    }\n}\n// Time complexity of algorithm C: constant order\nfunction algorithm_C(n: number): void {\n    for (let i = 0; i < 1000000; i++) {\n        console.log(0);\n    }\n}\n
      // Time complexity of algorithm A: constant order\nvoid algorithmA(int n) {\n  print(0);\n}\n// Time complexity of algorithm B: linear order\nvoid algorithmB(int n) {\n  for (int i = 0; i < n; i++) {\n    print(0);\n  }\n}\n// Time complexity of algorithm C: constant order\nvoid algorithmC(int n) {\n  for (int i = 0; i < 1000000; i++) {\n    print(0);\n  }\n}\n
      // Time complexity of algorithm A: constant order\nfn algorithm_A(n: i32) {\n    println!(\"{}\", 0);\n}\n// Time complexity of algorithm B: linear order\nfn algorithm_B(n: i32) {\n    for _ in 0..n {\n        println!(\"{}\", 0);\n    }\n}\n// Time complexity of algorithm C: constant order\nfn algorithm_C(n: i32) {\n    for _ in 0..1000000 {\n        println!(\"{}\", 0);\n    }\n}\n
      // Time complexity of algorithm A: constant order\nvoid algorithm_A(int n) {\n    printf(\"%d\", 0);\n}\n// Time complexity of algorithm B: linear order\nvoid algorithm_B(int n) {\n    for (int i = 0; i < n; i++) {\n        printf(\"%d\", 0);\n    }\n}\n// Time complexity of algorithm C: constant order\nvoid algorithm_C(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        printf(\"%d\", 0);\n    }\n}\n
      // Time complexity of algorithm A: constant order\nfun algoritm_A(n: Int) {\n    println(0)\n}\n// Time complexity of algorithm B: linear order\nfun algorithm_B(n: Int) {\n    for (i in 0..<n){\n        println(0)\n    }\n}\n// Time complexity of algorithm C: constant order\nfun algorithm_C(n: Int) {\n    for (i in 0..<1000000) {\n        println(0)\n    }\n}\n
      # Time complexity of algorithm A: constant order\ndef algorithm_A(n)\n    puts 0\nend\n\n# Time complexity of algorithm B: linear order\ndef algorithm_B(n)\n    (0...n).each { puts 0 }\nend\n\n# Time complexity of algorithm C: constant order\ndef algorithm_C(n)\n    (0...1_000_000).each { puts 0 }\nend\n

      Figure 2-7 shows the time complexity of the above three algorithm functions.

      • Algorithm A has only \\(1\\) print operation, and the algorithm's runtime does not grow as \\(n\\) increases. We call the time complexity of this algorithm \"constant order\".
      • In algorithm B, the print operation needs to loop \\(n\\) times, and the algorithm's runtime grows linearly as \\(n\\) increases. The time complexity of this algorithm is called \"linear order\".
      • In algorithm C, the print operation needs to loop \\(1000000\\) times. Although the runtime is very long, it is independent of the input data size \\(n\\). Therefore, the time complexity of C is the same as A, still \"constant order\".

      Figure 2-7 \u00a0 Time growth trends of algorithms A, B, and C

      Compared to directly counting the algorithm's runtime, what are the characteristics of time complexity analysis?

      • Time complexity can effectively evaluate algorithm efficiency. For example, the runtime of algorithm B grows linearly; when \\(n > 1\\) it is slower than algorithm A, and when \\(n > 1000000\\) it is slower than algorithm C. In fact, as long as the input data size \\(n\\) is sufficiently large, an algorithm with \"constant order\" complexity will always be superior to one with \"linear order\" complexity, which is precisely the meaning of time growth trend.
      • The derivation method for time complexity is simpler. Obviously, the running platform and the types of computational operations are both unrelated to the growth trend of the algorithm's runtime. Therefore, in time complexity analysis, we can simply treat the execution time of all computational operations as the same \"unit time\", thus simplifying \"counting computational operation runtime\" to \"counting the number of computational operations\", which greatly reduces the difficulty of estimation.
      • Time complexity also has certain limitations. For example, although algorithms A and C have the same time complexity, their actual runtimes differ significantly. Similarly, although algorithm B has a higher time complexity than C, when the input data size \\(n\\) is small, algorithm B is clearly superior to algorithm C. In such cases, it is often difficult to judge the efficiency of algorithms based solely on time complexity. Of course, despite the above issues, complexity analysis remains the most effective and commonly used method for evaluating algorithm efficiency.
      "},{"location":"chapter_computational_complexity/time_complexity/#232-asymptotic-upper-bound-of-functions","title":"2.3.2 \u00a0 Asymptotic Upper Bound of Functions","text":"

      Given a function with input size \\(n\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      def algorithm(n: int):\n    a = 1      # +1\n    a = a + 1  # +1\n    a = a * 2  # +1\n    # Loop n times\n    for i in range(n):  # +1\n        print(0)        # +1\n
      void algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // Loop n times\n    for (int i = 0; i < n; i++) { // +1 (i++ is executed each round)\n        cout << 0 << endl;    // +1\n    }\n}\n
      void algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // Loop n times\n    for (int i = 0; i < n; i++) { // +1 (i++ is executed each round)\n        System.out.println(0);    // +1\n    }\n}\n
      void Algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // Loop n times\n    for (int i = 0; i < n; i++) {   // +1 (i++ is executed each round)\n        Console.WriteLine(0);   // +1\n    }\n}\n
      func algorithm(n int) {\n    a := 1      // +1\n    a = a + 1   // +1\n    a = a * 2   // +1\n    // Loop n times\n    for i := 0; i < n; i++ {   // +1\n        fmt.Println(a)         // +1\n    }\n}\n
      func algorithm(n: Int) {\n    var a = 1 // +1\n    a = a + 1 // +1\n    a = a * 2 // +1\n    // Loop n times\n    for _ in 0 ..< n { // +1\n        print(0) // +1\n    }\n}\n
      function algorithm(n) {\n    var a = 1; // +1\n    a += 1; // +1\n    a *= 2; // +1\n    // Loop n times\n    for(let i = 0; i < n; i++){ // +1 (i++ is executed each round)\n        console.log(0); // +1\n    }\n}\n
      function algorithm(n: number): void{\n    var a: number = 1; // +1\n    a += 1; // +1\n    a *= 2; // +1\n    // Loop n times\n    for(let i = 0; i < n; i++){ // +1 (i++ is executed each round)\n        console.log(0); // +1\n    }\n}\n
      void algorithm(int n) {\n  int a = 1; // +1\n  a = a + 1; // +1\n  a = a * 2; // +1\n  // Loop n times\n  for (int i = 0; i < n; i++) { // +1 (i++ is executed each round)\n    print(0); // +1\n  }\n}\n
      fn algorithm(n: i32) {\n    let mut a = 1;   // +1\n    a = a + 1;      // +1\n    a = a * 2;      // +1\n\n    // Loop n times\n    for _ in 0..n { // +1 (i++ is executed each round)\n        println!(\"{}\", 0); // +1\n    }\n}\n
      void algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // Loop n times\n    for (int i = 0; i < n; i++) {   // +1 (i++ is executed each round)\n        printf(\"%d\", 0);            // +1\n    }\n}\n
      fun algorithm(n: Int) {\n    var a = 1 // +1\n    a = a + 1 // +1\n    a = a * 2 // +1\n    // Loop n times\n    for (i in 0..<n) { // +1 (i++ is executed each round)\n        println(0) // +1\n    }\n}\n
      def algorithm(n)\n    a = 1       # +1\n    a = a + 1   # +1\n    a = a * 2   # +1\n    # Loop n times\n    (0...n).each do # +1\n        puts 0      # +1\n    end\nend\n

      Let the number of operations of the algorithm be a function of the input data size \\(n\\), denoted as \\(T(n)\\). Then the number of operations of the above function is:

      \\[ T(n) = 3 + 2n \\]

      \\(T(n)\\) is a linear function, indicating that its runtime growth trend is linear, and therefore its time complexity is linear order.

      We denote the time complexity of linear order as \\(O(n)\\). This mathematical symbol is called big-\\(O\\) notation, representing the asymptotic upper bound of the function \\(T(n)\\).

      Time complexity analysis essentially calculates the asymptotic upper bound of \"the number of operations \\(T(n)\\)\", which has a clear mathematical definition.

      Asymptotic upper bound of functions

      If there exist positive real numbers \\(c\\) and \\(n_0\\) such that for all \\(n > n_0\\), we have \\(T(n) \\leq c \\cdot f(n)\\), then \\(f(n)\\) can be considered as an asymptotic upper bound of \\(T(n)\\), denoted as \\(T(n) = O(f(n))\\).

      As shown in Figure 2-8, calculating the asymptotic upper bound is to find a function \\(f(n)\\) such that when \\(n\\) tends to infinity, \\(T(n)\\) and \\(f(n)\\) are at the same growth level, differing only by a constant coefficient \\(c\\).

      Figure 2-8 \u00a0 Asymptotic upper bound of a function

      "},{"location":"chapter_computational_complexity/time_complexity/#233-derivation-method","title":"2.3.3 \u00a0 Derivation Method","text":"

      The asymptotic upper bound has a bit of mathematical flavor. If you feel you haven't fully understood it, don't worry. We can first master the derivation method, and gradually grasp its mathematical meaning through continuous practice.

      According to the definition, after determining \\(f(n)\\), we can obtain the time complexity \\(O(f(n))\\). So how do we determine the asymptotic upper bound \\(f(n)\\)? Overall, it is divided into two steps: first count the number of operations, then determine the asymptotic upper bound.

      "},{"location":"chapter_computational_complexity/time_complexity/#1-step-1-count-the-number-of-operations","title":"1. \u00a0 Step 1: Count the Number of Operations","text":"

      For code, count from top to bottom line by line. However, since the constant coefficient \\(c\\) in \\(c \\cdot f(n)\\) above can be of any size, coefficients and constant terms in the number of operations \\(T(n)\\) can all be ignored. According to this principle, the following counting simplification techniques can be summarized.

      1. Ignore constants in \\(T(n)\\). Because they are all independent of \\(n\\), they do not affect time complexity.
      2. Omit all coefficients. For example, looping \\(2n\\) times, \\(5n + 1\\) times, etc., can all be simplified as \\(n\\) times, because the coefficient before \\(n\\) does not affect time complexity.
      3. Use multiplication for nested loops. The total number of operations equals the product of the number of operations in the outer and inner loops, with each layer of loop still able to apply techniques 1. and 2. separately.

      Given a function, we can use the above techniques to count the number of operations:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      def algorithm(n: int):\n    a = 1      # +0 (Technique 1)\n    a = a + n  # +0 (Technique 1)\n    # +n (Technique 2)\n    for i in range(5 * n + 1):\n        print(0)\n    # +n*n (Technique 3)\n    for i in range(2 * n):\n        for j in range(n + 1):\n            print(0)\n
      void algorithm(int n) {\n    int a = 1;  // +0 (Technique 1)\n    a = a + n;  // +0 (Technique 1)\n    // +n (Technique 2)\n    for (int i = 0; i < 5 * n + 1; i++) {\n        cout << 0 << endl;\n    }\n    // +n*n (Technique 3)\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            cout << 0 << endl;\n        }\n    }\n}\n
      void algorithm(int n) {\n    int a = 1;  // +0 (Technique 1)\n    a = a + n;  // +0 (Technique 1)\n    // +n (Technique 2)\n    for (int i = 0; i < 5 * n + 1; i++) {\n        System.out.println(0);\n    }\n    // +n*n (Technique 3)\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            System.out.println(0);\n        }\n    }\n}\n
      void Algorithm(int n) {\n    int a = 1;  // +0 (Technique 1)\n    a = a + n;  // +0 (Technique 1)\n    // +n (Technique 2)\n    for (int i = 0; i < 5 * n + 1; i++) {\n        Console.WriteLine(0);\n    }\n    // +n*n (Technique 3)\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            Console.WriteLine(0);\n        }\n    }\n}\n
      func algorithm(n int) {\n    a := 1     // +0 (Technique 1)\n    a = a + n  // +0 (Technique 1)\n    // +n (Technique 2)\n    for i := 0; i < 5 * n + 1; i++ {\n        fmt.Println(0)\n    }\n    // +n*n (Technique 3)\n    for i := 0; i < 2 * n; i++ {\n        for j := 0; j < n + 1; j++ {\n            fmt.Println(0)\n        }\n    }\n}\n
      func algorithm(n: Int) {\n    var a = 1 // +0 (Technique 1)\n    a = a + n // +0 (Technique 1)\n    // +n (Technique 2)\n    for _ in 0 ..< (5 * n + 1) {\n        print(0)\n    }\n    // +n*n (Technique 3)\n    for _ in 0 ..< (2 * n) {\n        for _ in 0 ..< (n + 1) {\n            print(0)\n        }\n    }\n}\n
      function algorithm(n) {\n    let a = 1;  // +0 (Technique 1)\n    a = a + n;  // +0 (Technique 1)\n    // +n (Technique 2)\n    for (let i = 0; i < 5 * n + 1; i++) {\n        console.log(0);\n    }\n    // +n*n (Technique 3)\n    for (let i = 0; i < 2 * n; i++) {\n        for (let j = 0; j < n + 1; j++) {\n            console.log(0);\n        }\n    }\n}\n
      function algorithm(n: number): void {\n    let a = 1;  // +0 (Technique 1)\n    a = a + n;  // +0 (Technique 1)\n    // +n (Technique 2)\n    for (let i = 0; i < 5 * n + 1; i++) {\n        console.log(0);\n    }\n    // +n*n (Technique 3)\n    for (let i = 0; i < 2 * n; i++) {\n        for (let j = 0; j < n + 1; j++) {\n            console.log(0);\n        }\n    }\n}\n
      void algorithm(int n) {\n  int a = 1; // +0 (Technique 1)\n  a = a + n; // +0 (Technique 1)\n  // +n (Technique 2)\n  for (int i = 0; i < 5 * n + 1; i++) {\n    print(0);\n  }\n  // +n*n (Technique 3)\n  for (int i = 0; i < 2 * n; i++) {\n    for (int j = 0; j < n + 1; j++) {\n      print(0);\n    }\n  }\n}\n
      fn algorithm(n: i32) {\n    let mut a = 1;     // +0 (Technique 1)\n    a = a + n;        // +0 (Technique 1)\n\n    // +n (Technique 2)\n    for i in 0..(5 * n + 1) {\n        println!(\"{}\", 0);\n    }\n\n    // +n*n (Technique 3)\n    for i in 0..(2 * n) {\n        for j in 0..(n + 1) {\n            println!(\"{}\", 0);\n        }\n    }\n}\n
      void algorithm(int n) {\n    int a = 1;  // +0 (Technique 1)\n    a = a + n;  // +0 (Technique 1)\n    // +n (Technique 2)\n    for (int i = 0; i < 5 * n + 1; i++) {\n        printf(\"%d\", 0);\n    }\n    // +n*n (Technique 3)\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            printf(\"%d\", 0);\n        }\n    }\n}\n
      fun algorithm(n: Int) {\n    var a = 1   // +0 (Technique 1)\n    a = a + n   // +0 (Technique 1)\n    // +n (Technique 2)\n    for (i in 0..<5 * n + 1) {\n        println(0)\n    }\n    // +n*n (Technique 3)\n    for (i in 0..<2 * n) {\n        for (j in 0..<n + 1) {\n            println(0)\n        }\n    }\n}\n
      def algorithm(n)\n    a = 1       # +0 (Technique 1)\n    a = a + n   # +0 (Technique 1)\n    # +n (Technique 2)\n    (0...(5 * n + 1)).each do { puts 0 }\n    # +n*n (Technique 3)\n    (0...(2 * n)).each do\n        (0...(n + 1)).each do { puts 0 }\n    end\nend\n

      The following formula shows the counting results before and after using the above techniques; both derive a time complexity of \\(O(n^2)\\).

      \\[ \\begin{aligned} T(n) & = 2n(n + 1) + (5n + 1) + 2 & \\text{Complete count (-.-|||)} \\newline & = 2n^2 + 7n + 3 \\newline T(n) & = n^2 + n & \\text{Simplified count (o.O)} \\end{aligned} \\]"},{"location":"chapter_computational_complexity/time_complexity/#2-step-2-determine-the-asymptotic-upper-bound","title":"2. \u00a0 Step 2: Determine the Asymptotic Upper Bound","text":"

      Time complexity is determined by the highest-order term in \\(T(n)\\). This is because as \\(n\\) tends to infinity, the highest-order term will play a dominant role, and the influence of other terms can be ignored.

      Table 2-2 shows some examples, where some exaggerated values are used to emphasize the conclusion that \"coefficients cannot shake the order\". When \\(n\\) tends to infinity, these constants become insignificant.

      Table 2-2 \u00a0 Time complexities corresponding to different numbers of operations

      Number of Operations \\(T(n)\\) Time Complexity \\(O(f(n))\\) \\(100000\\) \\(O(1)\\) \\(3n + 2\\) \\(O(n)\\) \\(2n^2 + 3n + 2\\) \\(O(n^2)\\) \\(n^3 + 10000n^2\\) \\(O(n^3)\\) \\(2^n + 10000n^{10000}\\) \\(O(2^n)\\)"},{"location":"chapter_computational_complexity/time_complexity/#234-common-types","title":"2.3.4 \u00a0 Common Types","text":"

      Let the input data size be \\(n\\). Common time complexity types are shown in Figure 2-9 (arranged in order from low to high).

      \\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n \\log n) < O(n^2) < O(2^n) < O(n!) \\newline \\text{Constant order} < \\text{Logarithmic order} < \\text{Linear order} < \\text{Linearithmic order} < \\text{Quadratic order} < \\text{Exponential order} < \\text{Factorial order} \\end{aligned} \\]

      Figure 2-9 \u00a0 Common time complexity types

      "},{"location":"chapter_computational_complexity/time_complexity/#1-constant-order-o1","title":"1. \u00a0 Constant Order \\(O(1)\\)","text":"

      The number of operations in constant order is independent of the input data size \\(n\\), meaning it does not change as \\(n\\) changes.

      In the following function, although the number of operations size may be large, since it is independent of the input data size \\(n\\), the time complexity remains \\(O(1)\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
      def constant(n: int) -> int:\n    \"\"\"Constant order\"\"\"\n    count = 0\n    size = 100000\n    for _ in range(size):\n        count += 1\n    return count\n
      time_complexity.cpp
      /* Constant order */\nint constant(int n) {\n    int count = 0;\n    int size = 100000;\n    for (int i = 0; i < size; i++)\n        count++;\n    return count;\n}\n
      time_complexity.java
      /* Constant order */\nint constant(int n) {\n    int count = 0;\n    int size = 100000;\n    for (int i = 0; i < size; i++)\n        count++;\n    return count;\n}\n
      time_complexity.cs
      /* Constant order */\nint Constant(int n) {\n    int count = 0;\n    int size = 100000;\n    for (int i = 0; i < size; i++)\n        count++;\n    return count;\n}\n
      time_complexity.go
      /* Constant order */\nfunc constant(n int) int {\n    count := 0\n    size := 100000\n    for i := 0; i < size; i++ {\n        count++\n    }\n    return count\n}\n
      time_complexity.swift
      /* Constant order */\nfunc constant(n: Int) -> Int {\n    var count = 0\n    let size = 100_000\n    for _ in 0 ..< size {\n        count += 1\n    }\n    return count\n}\n
      time_complexity.js
      /* Constant order */\nfunction constant(n) {\n    let count = 0;\n    const size = 100000;\n    for (let i = 0; i < size; i++) count++;\n    return count;\n}\n
      time_complexity.ts
      /* Constant order */\nfunction constant(n: number): number {\n    let count = 0;\n    const size = 100000;\n    for (let i = 0; i < size; i++) count++;\n    return count;\n}\n
      time_complexity.dart
      /* Constant order */\nint constant(int n) {\n  int count = 0;\n  int size = 100000;\n  for (var i = 0; i < size; i++) {\n    count++;\n  }\n  return count;\n}\n
      time_complexity.rs
      /* Constant order */\nfn constant(n: i32) -> i32 {\n    _ = n;\n    let mut count = 0;\n    let size = 100_000;\n    for _ in 0..size {\n        count += 1;\n    }\n    count\n}\n
      time_complexity.c
      /* Constant order */\nint constant(int n) {\n    int count = 0;\n    int size = 100000;\n    int i = 0;\n    for (int i = 0; i < size; i++) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.kt
      /* Constant order */\nfun constant(n: Int): Int {\n    var count = 0\n    val size = 100000\n    for (i in 0..<size)\n        count++\n    return count\n}\n
      time_complexity.rb
      ### Constant time ###\ndef constant(n)\n  count = 0\n  size = 100000\n\n  (0...size).each { count += 1 }\n\n  count\nend\n
      "},{"location":"chapter_computational_complexity/time_complexity/#2-linear-order-on","title":"2. \u00a0 Linear Order \\(O(n)\\)","text":"

      The number of operations in linear order grows linearly relative to the input data size \\(n\\). Linear order typically appears in single-layer loops:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
      def linear(n: int) -> int:\n    \"\"\"Linear order\"\"\"\n    count = 0\n    for _ in range(n):\n        count += 1\n    return count\n
      time_complexity.cpp
      /* Linear order */\nint linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++)\n        count++;\n    return count;\n}\n
      time_complexity.java
      /* Linear order */\nint linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++)\n        count++;\n    return count;\n}\n
      time_complexity.cs
      /* Linear order */\nint Linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++)\n        count++;\n    return count;\n}\n
      time_complexity.go
      /* Linear order */\nfunc linear(n int) int {\n    count := 0\n    for i := 0; i < n; i++ {\n        count++\n    }\n    return count\n}\n
      time_complexity.swift
      /* Linear order */\nfunc linear(n: Int) -> Int {\n    var count = 0\n    for _ in 0 ..< n {\n        count += 1\n    }\n    return count\n}\n
      time_complexity.js
      /* Linear order */\nfunction linear(n) {\n    let count = 0;\n    for (let i = 0; i < n; i++) count++;\n    return count;\n}\n
      time_complexity.ts
      /* Linear order */\nfunction linear(n: number): number {\n    let count = 0;\n    for (let i = 0; i < n; i++) count++;\n    return count;\n}\n
      time_complexity.dart
      /* Linear order */\nint linear(int n) {\n  int count = 0;\n  for (var i = 0; i < n; i++) {\n    count++;\n  }\n  return count;\n}\n
      time_complexity.rs
      /* Linear order */\nfn linear(n: i32) -> i32 {\n    let mut count = 0;\n    for _ in 0..n {\n        count += 1;\n    }\n    count\n}\n
      time_complexity.c
      /* Linear order */\nint linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.kt
      /* Linear order */\nfun linear(n: Int): Int {\n    var count = 0\n    for (i in 0..<n)\n        count++\n    return count\n}\n
      time_complexity.rb
      ### Linear time ###\ndef linear(n)\n  count = 0\n  (0...n).each { count += 1 }\n  count\nend\n

      Operations such as traversing arrays and traversing linked lists have a time complexity of \\(O(n)\\), where \\(n\\) is the length of the array or linked list:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
      def array_traversal(nums: list[int]) -> int:\n    \"\"\"Linear order (traversing array)\"\"\"\n    count = 0\n    # Number of iterations is proportional to the array length\n    for num in nums:\n        count += 1\n    return count\n
      time_complexity.cpp
      /* Linear order (traversing array) */\nint arrayTraversal(vector<int> &nums) {\n    int count = 0;\n    // Number of iterations is proportional to the array length\n    for (int num : nums) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.java
      /* Linear order (traversing array) */\nint arrayTraversal(int[] nums) {\n    int count = 0;\n    // Number of iterations is proportional to the array length\n    for (int num : nums) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.cs
      /* Linear order (traversing array) */\nint ArrayTraversal(int[] nums) {\n    int count = 0;\n    // Number of iterations is proportional to the array length\n    foreach (int num in nums) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.go
      /* Linear order (traversing array) */\nfunc arrayTraversal(nums []int) int {\n    count := 0\n    // Number of iterations is proportional to the array length\n    for range nums {\n        count++\n    }\n    return count\n}\n
      time_complexity.swift
      /* Linear order (traversing array) */\nfunc arrayTraversal(nums: [Int]) -> Int {\n    var count = 0\n    // Number of iterations is proportional to the array length\n    for _ in nums {\n        count += 1\n    }\n    return count\n}\n
      time_complexity.js
      /* Linear order (traversing array) */\nfunction arrayTraversal(nums) {\n    let count = 0;\n    // Number of iterations is proportional to the array length\n    for (let i = 0; i < nums.length; i++) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.ts
      /* Linear order (traversing array) */\nfunction arrayTraversal(nums: number[]): number {\n    let count = 0;\n    // Number of iterations is proportional to the array length\n    for (let i = 0; i < nums.length; i++) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.dart
      /* Linear order (traversing array) */\nint arrayTraversal(List<int> nums) {\n  int count = 0;\n  // Number of iterations is proportional to the array length\n  for (var _num in nums) {\n    count++;\n  }\n  return count;\n}\n
      time_complexity.rs
      /* Linear order (traversing array) */\nfn array_traversal(nums: &[i32]) -> i32 {\n    let mut count = 0;\n    // Number of iterations is proportional to the array length\n    for _ in nums {\n        count += 1;\n    }\n    count\n}\n
      time_complexity.c
      /* Linear order (traversing array) */\nint arrayTraversal(int *nums, int n) {\n    int count = 0;\n    // Number of iterations is proportional to the array length\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.kt
      /* Linear order (traversing array) */\nfun arrayTraversal(nums: IntArray): Int {\n    var count = 0\n    // Number of iterations is proportional to the array length\n    for (num in nums) {\n        count++\n    }\n    return count\n}\n
      time_complexity.rb
      ### Linear time (array traversal) ###\ndef array_traversal(nums)\n  count = 0\n\n  # Number of iterations is proportional to the array length\n  for num in nums\n    count += 1\n  end\n\n  count\nend\n

      It is worth noting that the input data size \\(n\\) should be determined according to the type of input data. For example, in the first example, the variable \\(n\\) is the input data size; in the second example, the array length \\(n\\) is the data size.

      "},{"location":"chapter_computational_complexity/time_complexity/#3-quadratic-order-on2","title":"3. \u00a0 Quadratic Order \\(O(n^2)\\)","text":"

      The number of operations in quadratic order grows quadratically relative to the input data size \\(n\\). Quadratic order typically appears in nested loops, where both the outer and inner loops have a time complexity of \\(O(n)\\), resulting in an overall time complexity of \\(O(n^2)\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
      def quadratic(n: int) -> int:\n    \"\"\"Quadratic order\"\"\"\n    count = 0\n    # Number of iterations is quadratically related to the data size n\n    for i in range(n):\n        for j in range(n):\n            count += 1\n    return count\n
      time_complexity.cpp
      /* Exponential order */\nint quadratic(int n) {\n    int count = 0;\n    // Number of iterations is quadratically related to the data size n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
      time_complexity.java
      /* Exponential order */\nint quadratic(int n) {\n    int count = 0;\n    // Number of iterations is quadratically related to the data size n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
      time_complexity.cs
      /* Exponential order */\nint Quadratic(int n) {\n    int count = 0;\n    // Number of iterations is quadratically related to the data size n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
      time_complexity.go
      /* Exponential order */\nfunc quadratic(n int) int {\n    count := 0\n    // Number of iterations is quadratically related to the data size n\n    for i := 0; i < n; i++ {\n        for j := 0; j < n; j++ {\n            count++\n        }\n    }\n    return count\n}\n
      time_complexity.swift
      /* Exponential order */\nfunc quadratic(n: Int) -> Int {\n    var count = 0\n    // Number of iterations is quadratically related to the data size n\n    for _ in 0 ..< n {\n        for _ in 0 ..< n {\n            count += 1\n        }\n    }\n    return count\n}\n
      time_complexity.js
      /* Exponential order */\nfunction quadratic(n) {\n    let count = 0;\n    // Number of iterations is quadratically related to the data size n\n    for (let i = 0; i < n; i++) {\n        for (let j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
      time_complexity.ts
      /* Exponential order */\nfunction quadratic(n: number): number {\n    let count = 0;\n    // Number of iterations is quadratically related to the data size n\n    for (let i = 0; i < n; i++) {\n        for (let j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
      time_complexity.dart
      /* Exponential order */\nint quadratic(int n) {\n  int count = 0;\n  // Number of iterations is quadratically related to the data size n\n  for (int i = 0; i < n; i++) {\n    for (int j = 0; j < n; j++) {\n      count++;\n    }\n  }\n  return count;\n}\n
      time_complexity.rs
      /* Exponential order */\nfn quadratic(n: i32) -> i32 {\n    let mut count = 0;\n    // Number of iterations is quadratically related to the data size n\n    for _ in 0..n {\n        for _ in 0..n {\n            count += 1;\n        }\n    }\n    count\n}\n
      time_complexity.c
      /* Exponential order */\nint quadratic(int n) {\n    int count = 0;\n    // Number of iterations is quadratically related to the data size n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
      time_complexity.kt
      /* Exponential order */\nfun quadratic(n: Int): Int {\n    var count = 0\n    // Number of iterations is quadratically related to the data size n\n    for (i in 0..<n) {\n        for (j in 0..<n) {\n            count++\n        }\n    }\n    return count\n}\n
      time_complexity.rb
      ### Quadratic time ###\ndef quadratic(n)\n  count = 0\n\n  # Number of iterations is quadratically related to the data size n\n  for i in 0...n\n    for j in 0...n\n      count += 1\n    end\n  end\n\n  count\nend\n

      Figure 2-10 compares constant order, linear order, and quadratic order time complexities.

      Figure 2-10 \u00a0 Time complexities of constant, linear, and quadratic orders

      Taking bubble sort as an example, the outer loop executes \\(n - 1\\) times, and the inner loop executes \\(n-1\\), \\(n-2\\), \\(\\dots\\), \\(2\\), \\(1\\) times, averaging \\(n / 2\\) times, resulting in a time complexity of \\(O((n - 1) n / 2) = O(n^2)\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
      def bubble_sort(nums: list[int]) -> int:\n    \"\"\"Quadratic order (bubble sort)\"\"\"\n    count = 0  # Counter\n    # Outer loop: unsorted range is [0, i]\n    for i in range(len(nums) - 1, 0, -1):\n        # Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for j in range(i):\n            if nums[j] > nums[j + 1]:\n                # Swap nums[j] and nums[j + 1]\n                tmp: int = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = tmp\n                count += 3  # Element swap includes 3 unit operations\n    return count\n
      time_complexity.cpp
      /* Quadratic order (bubble sort) */\nint bubbleSort(vector<int> &nums) {\n    int count = 0; // Counter\n    // Outer loop: unsorted range is [0, i]\n    for (int i = nums.size() - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // Element swap includes 3 unit operations\n            }\n        }\n    }\n    return count;\n}\n
      time_complexity.java
      /* Quadratic order (bubble sort) */\nint bubbleSort(int[] nums) {\n    int count = 0; // Counter\n    // Outer loop: unsorted range is [0, i]\n    for (int i = nums.length - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // Element swap includes 3 unit operations\n            }\n        }\n    }\n    return count;\n}\n
      time_complexity.cs
      /* Quadratic order (bubble sort) */\nint BubbleSort(int[] nums) {\n    int count = 0;  // Counter\n    // Outer loop: unsorted range is [0, i]\n    for (int i = nums.Length - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n                count += 3;  // Element swap includes 3 unit operations\n            }\n        }\n    }\n    return count;\n}\n
      time_complexity.go
      /* Quadratic order (bubble sort) */\nfunc bubbleSort(nums []int) int {\n    count := 0 // Counter\n    // Outer loop: unsorted range is [0, i]\n    for i := len(nums) - 1; i > 0; i-- {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for j := 0; j < i; j++ {\n            if nums[j] > nums[j+1] {\n                // Swap nums[j] and nums[j + 1]\n                tmp := nums[j]\n                nums[j] = nums[j+1]\n                nums[j+1] = tmp\n                count += 3 // Element swap includes 3 unit operations\n            }\n        }\n    }\n    return count\n}\n
      time_complexity.swift
      /* Quadratic order (bubble sort) */\nfunc bubbleSort(nums: inout [Int]) -> Int {\n    var count = 0 // Counter\n    // Outer loop: unsorted range is [0, i]\n    for i in nums.indices.dropFirst().reversed() {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for j in 0 ..< i {\n            if nums[j] > nums[j + 1] {\n                // Swap nums[j] and nums[j + 1]\n                let tmp = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = tmp\n                count += 3 // Element swap includes 3 unit operations\n            }\n        }\n    }\n    return count\n}\n
      time_complexity.js
      /* Quadratic order (bubble sort) */\nfunction bubbleSort(nums) {\n    let count = 0; // Counter\n    // Outer loop: unsorted range is [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // Element swap includes 3 unit operations\n            }\n        }\n    }\n    return count;\n}\n
      time_complexity.ts
      /* Quadratic order (bubble sort) */\nfunction bubbleSort(nums: number[]): number {\n    let count = 0; // Counter\n    // Outer loop: unsorted range is [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // Element swap includes 3 unit operations\n            }\n        }\n    }\n    return count;\n}\n
      time_complexity.dart
      /* Quadratic order (bubble sort) */\nint bubbleSort(List<int> nums) {\n  int count = 0; // Counter\n  // Outer loop: unsorted range is [0, i]\n  for (var i = nums.length - 1; i > 0; i--) {\n    // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n    for (var j = 0; j < i; j++) {\n      if (nums[j] > nums[j + 1]) {\n        // Swap nums[j] and nums[j + 1]\n        int tmp = nums[j];\n        nums[j] = nums[j + 1];\n        nums[j + 1] = tmp;\n        count += 3; // Element swap includes 3 unit operations\n      }\n    }\n  }\n  return count;\n}\n
      time_complexity.rs
      /* Quadratic order (bubble sort) */\nfn bubble_sort(nums: &mut [i32]) -> i32 {\n    let mut count = 0; // Counter\n\n    // Outer loop: unsorted range is [0, i]\n    for i in (1..nums.len()).rev() {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for j in 0..i {\n            if nums[j] > nums[j + 1] {\n                // Swap nums[j] and nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // Element swap includes 3 unit operations\n            }\n        }\n    }\n    count\n}\n
      time_complexity.c
      /* Quadratic order (bubble sort) */\nint bubbleSort(int *nums, int n) {\n    int count = 0; // Counter\n    // Outer loop: unsorted range is [0, i]\n    for (int i = n - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // Element swap includes 3 unit operations\n            }\n        }\n    }\n    return count;\n}\n
      time_complexity.kt
      /* Quadratic order (bubble sort) */\nfun bubbleSort(nums: IntArray): Int {\n    var count = 0 // Counter\n    // Outer loop: unsorted range is [0, i]\n    for (i in nums.size - 1 downTo 1) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (j in 0..<i) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                val temp = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = temp\n                count += 3 // Element swap includes 3 unit operations\n            }\n        }\n    }\n    return count\n}\n
      time_complexity.rb
      ### Quadratic time (bubble sort) ###\ndef bubble_sort(nums)\n  count = 0  # Counter\n\n  # Outer loop: unsorted range is [0, i]\n  for i in (nums.length - 1).downto(0)\n    # Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n    for j in 0...i\n      if nums[j] > nums[j + 1]\n        # Swap nums[j] and nums[j + 1]\n        tmp = nums[j]\n        nums[j] = nums[j + 1]\n        nums[j + 1] = tmp\n        count += 3 # Element swap includes 3 unit operations\n      end\n    end\n  end\n\n  count\nend\n
      "},{"location":"chapter_computational_complexity/time_complexity/#4-exponential-order-o2n","title":"4. \u00a0 Exponential Order \\(O(2^n)\\)","text":"

      Biological \"cell division\" is a typical example of exponential order growth: the initial state is \\(1\\) cell, after one round of division it becomes \\(2\\), after two rounds it becomes \\(4\\), and so on; after \\(n\\) rounds of division there are \\(2^n\\) cells.

      Figure 2-11 and the following code simulate the cell division process, with a time complexity of \\(O(2^n)\\). Note that the input \\(n\\) represents the number of division rounds, and the return value count represents the total number of divisions.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
      def exponential(n: int) -> int:\n    \"\"\"Exponential order (loop implementation)\"\"\"\n    count = 0\n    base = 1\n    # Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n    for _ in range(n):\n        for _ in range(base):\n            count += 1\n        base *= 2\n    # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count\n
      time_complexity.cpp
      /* Exponential order (loop implementation) */\nint exponential(int n) {\n    int count = 0, base = 1;\n    // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
      time_complexity.java
      /* Exponential order (loop implementation) */\nint exponential(int n) {\n    int count = 0, base = 1;\n    // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
      time_complexity.cs
      /* Exponential order (loop implementation) */\nint Exponential(int n) {\n    int count = 0, bas = 1;\n    // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < bas; j++) {\n            count++;\n        }\n        bas *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
      time_complexity.go
      /* Exponential order (loop implementation) */\nfunc exponential(n int) int {\n    count, base := 0, 1\n    // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n    for i := 0; i < n; i++ {\n        for j := 0; j < base; j++ {\n            count++\n        }\n        base *= 2\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count\n}\n
      time_complexity.swift
      /* Exponential order (loop implementation) */\nfunc exponential(n: Int) -> Int {\n    var count = 0\n    var base = 1\n    // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n    for _ in 0 ..< n {\n        for _ in 0 ..< base {\n            count += 1\n        }\n        base *= 2\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count\n}\n
      time_complexity.js
      /* Exponential order (loop implementation) */\nfunction exponential(n) {\n    let count = 0,\n        base = 1;\n    // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n    for (let i = 0; i < n; i++) {\n        for (let j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
      time_complexity.ts
      /* Exponential order (loop implementation) */\nfunction exponential(n: number): number {\n    let count = 0,\n        base = 1;\n    // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n    for (let i = 0; i < n; i++) {\n        for (let j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
      time_complexity.dart
      /* Exponential order (loop implementation) */\nint exponential(int n) {\n  int count = 0, base = 1;\n  // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n  for (var i = 0; i < n; i++) {\n    for (var j = 0; j < base; j++) {\n      count++;\n    }\n    base *= 2;\n  }\n  // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n  return count;\n}\n
      time_complexity.rs
      /* Exponential order (loop implementation) */\nfn exponential(n: i32) -> i32 {\n    let mut count = 0;\n    let mut base = 1;\n    // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n    for _ in 0..n {\n        for _ in 0..base {\n            count += 1\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    count\n}\n
      time_complexity.c
      /* Exponential order (loop implementation) */\nint exponential(int n) {\n    int count = 0;\n    int bas = 1;\n    // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < bas; j++) {\n            count++;\n        }\n        bas *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
      time_complexity.kt
      /* Exponential order (loop implementation) */\nfun exponential(n: Int): Int {\n    var count = 0\n    var base = 1\n    // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n    for (i in 0..<n) {\n        for (j in 0..<base) {\n            count++\n        }\n        base *= 2\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count\n}\n
      time_complexity.rb
      ### Exponential time (iterative) ###\ndef exponential(n)\n  count, base = 0, 1\n\n  # Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n  (0...n).each do\n    (0...base).each { count += 1 }\n    base *= 2\n  end\n\n  # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n  count\nend\n

      Figure 2-11 \u00a0 Time complexity of exponential order

      In actual algorithms, exponential order often appears in recursive functions. For example, in the following code, it recursively splits in two, stopping after \\(n\\) splits:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
      def exp_recur(n: int) -> int:\n    \"\"\"Exponential order (recursive implementation)\"\"\"\n    if n == 1:\n        return 1\n    return exp_recur(n - 1) + exp_recur(n - 1) + 1\n
      time_complexity.cpp
      /* Exponential order (recursive implementation) */\nint expRecur(int n) {\n    if (n == 1)\n        return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
      time_complexity.java
      /* Exponential order (recursive implementation) */\nint expRecur(int n) {\n    if (n == 1)\n        return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
      time_complexity.cs
      /* Exponential order (recursive implementation) */\nint ExpRecur(int n) {\n    if (n == 1) return 1;\n    return ExpRecur(n - 1) + ExpRecur(n - 1) + 1;\n}\n
      time_complexity.go
      /* Exponential order (recursive implementation) */\nfunc expRecur(n int) int {\n    if n == 1 {\n        return 1\n    }\n    return expRecur(n-1) + expRecur(n-1) + 1\n}\n
      time_complexity.swift
      /* Exponential order (recursive implementation) */\nfunc expRecur(n: Int) -> Int {\n    if n == 1 {\n        return 1\n    }\n    return expRecur(n: n - 1) + expRecur(n: n - 1) + 1\n}\n
      time_complexity.js
      /* Exponential order (recursive implementation) */\nfunction expRecur(n) {\n    if (n === 1) return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
      time_complexity.ts
      /* Exponential order (recursive implementation) */\nfunction expRecur(n: number): number {\n    if (n === 1) return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
      time_complexity.dart
      /* Exponential order (recursive implementation) */\nint expRecur(int n) {\n  if (n == 1) return 1;\n  return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
      time_complexity.rs
      /* Exponential order (recursive implementation) */\nfn exp_recur(n: i32) -> i32 {\n    if n == 1 {\n        return 1;\n    }\n    exp_recur(n - 1) + exp_recur(n - 1) + 1\n}\n
      time_complexity.c
      /* Exponential order (recursive implementation) */\nint expRecur(int n) {\n    if (n == 1)\n        return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
      time_complexity.kt
      /* Exponential order (recursive implementation) */\nfun expRecur(n: Int): Int {\n    if (n == 1) {\n        return 1\n    }\n    return expRecur(n - 1) + expRecur(n - 1) + 1\n}\n
      time_complexity.rb
      ### Exponential time (recursive) ###\ndef exp_recur(n)\n  return 1 if n == 1\n  exp_recur(n - 1) + exp_recur(n - 1) + 1\nend\n

      Exponential order growth is very rapid and is common in exhaustive methods (brute force search, backtracking, etc.). For problems with large data scales, exponential order is unacceptable and typically requires dynamic programming or greedy algorithms to solve.

      "},{"location":"chapter_computational_complexity/time_complexity/#5-logarithmic-order-olog-n","title":"5. \u00a0 Logarithmic Order \\(O(\\log n)\\)","text":"

      In contrast to exponential order, logarithmic order reflects the situation of \"reducing to half each round\". Let the input data size be \\(n\\). Since it is reduced to half each round, the number of loops is \\(\\log_2 n\\), which is the inverse function of \\(2^n\\).

      Figure 2-12 and the following code simulate the process of \"reducing to half each round\", with a time complexity of \\(O(\\log_2 n)\\), abbreviated as \\(O(\\log n)\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
      def logarithmic(n: int) -> int:\n    \"\"\"Logarithmic order (loop implementation)\"\"\"\n    count = 0\n    while n > 1:\n        n = n / 2\n        count += 1\n    return count\n
      time_complexity.cpp
      /* Logarithmic order (loop implementation) */\nint logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
      time_complexity.java
      /* Logarithmic order (loop implementation) */\nint logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
      time_complexity.cs
      /* Logarithmic order (loop implementation) */\nint Logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n /= 2;\n        count++;\n    }\n    return count;\n}\n
      time_complexity.go
      /* Logarithmic order (loop implementation) */\nfunc logarithmic(n int) int {\n    count := 0\n    for n > 1 {\n        n = n / 2\n        count++\n    }\n    return count\n}\n
      time_complexity.swift
      /* Logarithmic order (loop implementation) */\nfunc logarithmic(n: Int) -> Int {\n    var count = 0\n    var n = n\n    while n > 1 {\n        n = n / 2\n        count += 1\n    }\n    return count\n}\n
      time_complexity.js
      /* Logarithmic order (loop implementation) */\nfunction logarithmic(n) {\n    let count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
      time_complexity.ts
      /* Logarithmic order (loop implementation) */\nfunction logarithmic(n: number): number {\n    let count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
      time_complexity.dart
      /* Logarithmic order (loop implementation) */\nint logarithmic(int n) {\n  int count = 0;\n  while (n > 1) {\n    n = n ~/ 2;\n    count++;\n  }\n  return count;\n}\n
      time_complexity.rs
      /* Logarithmic order (loop implementation) */\nfn logarithmic(mut n: i32) -> i32 {\n    let mut count = 0;\n    while n > 1 {\n        n = n / 2;\n        count += 1;\n    }\n    count\n}\n
      time_complexity.c
      /* Logarithmic order (loop implementation) */\nint logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
      time_complexity.kt
      /* Logarithmic order (loop implementation) */\nfun logarithmic(n: Int): Int {\n    var n1 = n\n    var count = 0\n    while (n1 > 1) {\n        n1 /= 2\n        count++\n    }\n    return count\n}\n
      time_complexity.rb
      ### Logarithmic time (iterative) ###\ndef logarithmic(n)\n  count = 0\n\n  while n > 1\n    n /= 2\n    count += 1\n  end\n\n  count\nend\n

      Figure 2-12 \u00a0 Time complexity of logarithmic order

      Like exponential order, logarithmic order also commonly appears in recursive functions. The following code forms a recursion tree of height \\(\\log_2 n\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
      def log_recur(n: int) -> int:\n    \"\"\"Logarithmic order (recursive implementation)\"\"\"\n    if n <= 1:\n        return 0\n    return log_recur(n / 2) + 1\n
      time_complexity.cpp
      /* Logarithmic order (recursive implementation) */\nint logRecur(int n) {\n    if (n <= 1)\n        return 0;\n    return logRecur(n / 2) + 1;\n}\n
      time_complexity.java
      /* Logarithmic order (recursive implementation) */\nint logRecur(int n) {\n    if (n <= 1)\n        return 0;\n    return logRecur(n / 2) + 1;\n}\n
      time_complexity.cs
      /* Logarithmic order (recursive implementation) */\nint LogRecur(int n) {\n    if (n <= 1) return 0;\n    return LogRecur(n / 2) + 1;\n}\n
      time_complexity.go
      /* Logarithmic order (recursive implementation) */\nfunc logRecur(n int) int {\n    if n <= 1 {\n        return 0\n    }\n    return logRecur(n/2) + 1\n}\n
      time_complexity.swift
      /* Logarithmic order (recursive implementation) */\nfunc logRecur(n: Int) -> Int {\n    if n <= 1 {\n        return 0\n    }\n    return logRecur(n: n / 2) + 1\n}\n
      time_complexity.js
      /* Logarithmic order (recursive implementation) */\nfunction logRecur(n) {\n    if (n <= 1) return 0;\n    return logRecur(n / 2) + 1;\n}\n
      time_complexity.ts
      /* Logarithmic order (recursive implementation) */\nfunction logRecur(n: number): number {\n    if (n <= 1) return 0;\n    return logRecur(n / 2) + 1;\n}\n
      time_complexity.dart
      /* Logarithmic order (recursive implementation) */\nint logRecur(int n) {\n  if (n <= 1) return 0;\n  return logRecur(n ~/ 2) + 1;\n}\n
      time_complexity.rs
      /* Logarithmic order (recursive implementation) */\nfn log_recur(n: i32) -> i32 {\n    if n <= 1 {\n        return 0;\n    }\n    log_recur(n / 2) + 1\n}\n
      time_complexity.c
      /* Logarithmic order (recursive implementation) */\nint logRecur(int n) {\n    if (n <= 1)\n        return 0;\n    return logRecur(n / 2) + 1;\n}\n
      time_complexity.kt
      /* Logarithmic order (recursive implementation) */\nfun logRecur(n: Int): Int {\n    if (n <= 1)\n        return 0\n    return logRecur(n / 2) + 1\n}\n
      time_complexity.rb
      ### Logarithmic time (recursive) ###\ndef log_recur(n)\n  return 0 unless n > 1\n  log_recur(n / 2) + 1\nend\n

      Logarithmic order commonly appears in algorithms based on the divide-and-conquer strategy, embodying the algorithmic thinking of \"dividing into many\" and \"simplifying complexity\". It grows slowly and is the ideal time complexity second only to constant order.

      What is the base of \\(O(\\log n)\\)?

      To be precise, \"dividing into \\(m\\)\" corresponds to a time complexity of \\(O(\\log_m n)\\). And through the logarithmic base change formula, we can obtain time complexities with different bases that are equal:

      \\[ O(\\log_m n) = O(\\log_k n / \\log_k m) = O(\\log_k n) \\]

      That is to say, the base \\(m\\) can be converted without affecting the complexity. Therefore, we usually omit the base \\(m\\) and denote logarithmic order simply as \\(O(\\log n)\\).

      "},{"location":"chapter_computational_complexity/time_complexity/#6-linearithmic-order-on-log-n","title":"6. \u00a0 Linearithmic Order \\(O(n \\log n)\\)","text":"

      Linearithmic order commonly appears in nested loops, where the time complexities of the two layers of loops are \\(O(\\log n)\\) and \\(O(n)\\) respectively. The relevant code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
      def linear_log_recur(n: int) -> int:\n    \"\"\"Linearithmic order\"\"\"\n    if n <= 1:\n        return 1\n    # Divide into two, the scale of subproblems is reduced by half\n    count = linear_log_recur(n // 2) + linear_log_recur(n // 2)\n    # Current subproblem contains n operations\n    for _ in range(n):\n        count += 1\n    return count\n
      time_complexity.cpp
      /* Linearithmic order */\nint linearLogRecur(int n) {\n    if (n <= 1)\n        return 1;\n    int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.java
      /* Linearithmic order */\nint linearLogRecur(int n) {\n    if (n <= 1)\n        return 1;\n    int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.cs
      /* Linearithmic order */\nint LinearLogRecur(int n) {\n    if (n <= 1) return 1;\n    int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.go
      /* Linearithmic order */\nfunc linearLogRecur(n int) int {\n    if n <= 1 {\n        return 1\n    }\n    count := linearLogRecur(n/2) + linearLogRecur(n/2)\n    for i := 0; i < n; i++ {\n        count++\n    }\n    return count\n}\n
      time_complexity.swift
      /* Linearithmic order */\nfunc linearLogRecur(n: Int) -> Int {\n    if n <= 1 {\n        return 1\n    }\n    var count = linearLogRecur(n: n / 2) + linearLogRecur(n: n / 2)\n    for _ in stride(from: 0, to: n, by: 1) {\n        count += 1\n    }\n    return count\n}\n
      time_complexity.js
      /* Linearithmic order */\nfunction linearLogRecur(n) {\n    if (n <= 1) return 1;\n    let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (let i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.ts
      /* Linearithmic order */\nfunction linearLogRecur(n: number): number {\n    if (n <= 1) return 1;\n    let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (let i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.dart
      /* Linearithmic order */\nint linearLogRecur(int n) {\n  if (n <= 1) return 1;\n  int count = linearLogRecur(n ~/ 2) + linearLogRecur(n ~/ 2);\n  for (var i = 0; i < n; i++) {\n    count++;\n  }\n  return count;\n}\n
      time_complexity.rs
      /* Linearithmic order */\nfn linear_log_recur(n: i32) -> i32 {\n    if n <= 1 {\n        return 1;\n    }\n    let mut count = linear_log_recur(n / 2) + linear_log_recur(n / 2);\n    for _ in 0..n {\n        count += 1;\n    }\n    return count;\n}\n
      time_complexity.c
      /* Linearithmic order */\nint linearLogRecur(int n) {\n    if (n <= 1)\n        return 1;\n    int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.kt
      /* Linearithmic order */\nfun linearLogRecur(n: Int): Int {\n    if (n <= 1)\n        return 1\n    var count = linearLogRecur(n / 2) + linearLogRecur(n / 2)\n    for (i in 0..<n) {\n        count++\n    }\n    return count\n}\n
      time_complexity.rb
      ### Linearithmic time ###\ndef linear_log_recur(n)\n  return 1 unless n > 1\n\n  count = linear_log_recur(n / 2) + linear_log_recur(n / 2)\n  (0...n).each { count += 1 }\n\n  count\nend\n

      Figure 2-13 shows how linearithmic order is generated. Each level of the binary tree has a total of \\(n\\) operations, and the tree has \\(\\log_2 n + 1\\) levels, resulting in a time complexity of \\(O(n \\log n)\\).

      Figure 2-13 \u00a0 Time complexity of linearithmic order

      Mainstream sorting algorithms typically have a time complexity of \\(O(n \\log n)\\), such as quicksort, merge sort, and heap sort.

      "},{"location":"chapter_computational_complexity/time_complexity/#7-factorial-order-on","title":"7. \u00a0 Factorial Order \\(O(n!)\\)","text":"

      Factorial order corresponds to the mathematical \"permutation\" problem. Given \\(n\\) distinct elements, find all possible permutation schemes; the number of schemes is:

      \\[ n! = n \\times (n - 1) \\times (n - 2) \\times \\dots \\times 2 \\times 1 \\]

      Factorials are typically implemented using recursion. As shown in Figure 2-14 and the following code, the first level splits into \\(n\\) branches, the second level splits into \\(n - 1\\) branches, and so on, until the \\(n\\)-th level when splitting stops:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
      def factorial_recur(n: int) -> int:\n    \"\"\"Factorial order (recursive implementation)\"\"\"\n    if n == 0:\n        return 1\n    count = 0\n    # Split from 1 into n\n    for _ in range(n):\n        count += factorial_recur(n - 1)\n    return count\n
      time_complexity.cpp
      /* Factorial order (recursive implementation) */\nint factorialRecur(int n) {\n    if (n == 0)\n        return 1;\n    int count = 0;\n    // Split from 1 into n\n    for (int i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
      time_complexity.java
      /* Factorial order (recursive implementation) */\nint factorialRecur(int n) {\n    if (n == 0)\n        return 1;\n    int count = 0;\n    // Split from 1 into n\n    for (int i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
      time_complexity.cs
      /* Factorial order (recursive implementation) */\nint FactorialRecur(int n) {\n    if (n == 0) return 1;\n    int count = 0;\n    // Split from 1 into n\n    for (int i = 0; i < n; i++) {\n        count += FactorialRecur(n - 1);\n    }\n    return count;\n}\n
      time_complexity.go
      /* Factorial order (recursive implementation) */\nfunc factorialRecur(n int) int {\n    if n == 0 {\n        return 1\n    }\n    count := 0\n    // Split from 1 into n\n    for i := 0; i < n; i++ {\n        count += factorialRecur(n - 1)\n    }\n    return count\n}\n
      time_complexity.swift
      /* Factorial order (recursive implementation) */\nfunc factorialRecur(n: Int) -> Int {\n    if n == 0 {\n        return 1\n    }\n    var count = 0\n    // Split from 1 into n\n    for _ in 0 ..< n {\n        count += factorialRecur(n: n - 1)\n    }\n    return count\n}\n
      time_complexity.js
      /* Factorial order (recursive implementation) */\nfunction factorialRecur(n) {\n    if (n === 0) return 1;\n    let count = 0;\n    // Split from 1 into n\n    for (let i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
      time_complexity.ts
      /* Factorial order (recursive implementation) */\nfunction factorialRecur(n: number): number {\n    if (n === 0) return 1;\n    let count = 0;\n    // Split from 1 into n\n    for (let i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
      time_complexity.dart
      /* Factorial order (recursive implementation) */\nint factorialRecur(int n) {\n  if (n == 0) return 1;\n  int count = 0;\n  // Split from 1 into n\n  for (var i = 0; i < n; i++) {\n    count += factorialRecur(n - 1);\n  }\n  return count;\n}\n
      time_complexity.rs
      /* Factorial order (recursive implementation) */\nfn factorial_recur(n: i32) -> i32 {\n    if n == 0 {\n        return 1;\n    }\n    let mut count = 0;\n    // Split from 1 into n\n    for _ in 0..n {\n        count += factorial_recur(n - 1);\n    }\n    count\n}\n
      time_complexity.c
      /* Factorial order (recursive implementation) */\nint factorialRecur(int n) {\n    if (n == 0)\n        return 1;\n    int count = 0;\n    for (int i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
      time_complexity.kt
      /* Factorial order (recursive implementation) */\nfun factorialRecur(n: Int): Int {\n    if (n == 0)\n        return 1\n    var count = 0\n    // Split from 1 into n\n    for (i in 0..<n) {\n        count += factorialRecur(n - 1)\n    }\n    return count\n}\n
      time_complexity.rb
      ### Factorial time (recursive) ###\ndef factorial_recur(n)\n  return 1 if n == 0\n\n  count = 0\n  # Split from 1 into n\n  (0...n).each { count += factorial_recur(n - 1) }\n\n  count\nend\n

      Figure 2-14 \u00a0 Time complexity of factorial order

      Note that because when \\(n \\geq 4\\) we always have \\(n! > 2^n\\), factorial order grows faster than exponential order, and is also unacceptable for large \\(n\\).

      "},{"location":"chapter_computational_complexity/time_complexity/#235-worst-best-and-average-time-complexities","title":"2.3.5 \u00a0 Worst, Best, and Average Time Complexities","text":"

      The time efficiency of an algorithm is often not fixed, but is related to the distribution of the input data. Suppose we input an array nums of length \\(n\\), where nums consists of numbers from \\(1\\) to \\(n\\), with each number appearing only once, but the element order is randomly shuffled. The task is to return the index of element \\(1\\). We can draw the following conclusions.

      • When nums = [?, ?, ..., 1], i.e., when the last element is \\(1\\), it requires a complete traversal of the array, reaching worst-case time complexity \\(O(n)\\).
      • When nums = [1, ?, ?, ...], i.e., when the first element is \\(1\\), no matter how long the array is, there is no need to continue traversing, reaching best-case time complexity \\(\\Omega(1)\\).

      The \"worst-case time complexity\" corresponds to the function's asymptotic upper bound, denoted using big-\\(O\\) notation. Correspondingly, the \"best-case time complexity\" corresponds to the function's asymptotic lower bound, denoted using \\(\\Omega\\) notation:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby worst_best_time_complexity.py
      def random_numbers(n: int) -> list[int]:\n    \"\"\"Generate an array with elements: 1, 2, ..., n, shuffled in order\"\"\"\n    # Generate array nums =: 1, 2, 3, ..., n\n    nums = [i for i in range(1, n + 1)]\n    # Randomly shuffle array elements\n    random.shuffle(nums)\n    return nums\n\ndef find_one(nums: list[int]) -> int:\n    \"\"\"Find the index of number 1 in array nums\"\"\"\n    for i in range(len(nums)):\n        # When element 1 is at the head of the array, best time complexity O(1) is achieved\n        # When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n        if nums[i] == 1:\n            return i\n    return -1\n
      worst_best_time_complexity.cpp
      /* Generate an array with elements { 1, 2, ..., n }, order shuffled */\nvector<int> randomNumbers(int n) {\n    vector<int> nums(n);\n    // Generate array nums = { 1, 2, 3, ..., n }\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // Use system time to generate random seed\n    unsigned seed = chrono::system_clock::now().time_since_epoch().count();\n    // Randomly shuffle array elements\n    shuffle(nums.begin(), nums.end(), default_random_engine(seed));\n    return nums;\n}\n\n/* Find the index of number 1 in array nums */\nint findOne(vector<int> &nums) {\n    for (int i = 0; i < nums.size(); i++) {\n        // When element 1 is at the head of the array, best time complexity O(1) is achieved\n        // When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
      worst_best_time_complexity.java
      /* Generate an array with elements { 1, 2, ..., n }, order shuffled */\nint[] randomNumbers(int n) {\n    Integer[] nums = new Integer[n];\n    // Generate array nums = { 1, 2, 3, ..., n }\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // Randomly shuffle array elements\n    Collections.shuffle(Arrays.asList(nums));\n    // Integer[] -> int[]\n    int[] res = new int[n];\n    for (int i = 0; i < n; i++) {\n        res[i] = nums[i];\n    }\n    return res;\n}\n\n/* Find the index of number 1 in array nums */\nint findOne(int[] nums) {\n    for (int i = 0; i < nums.length; i++) {\n        // When element 1 is at the head of the array, best time complexity O(1) is achieved\n        // When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
      worst_best_time_complexity.cs
      /* Generate an array with elements { 1, 2, ..., n }, order shuffled */\nint[] RandomNumbers(int n) {\n    int[] nums = new int[n];\n    // Generate array nums = { 1, 2, 3, ..., n }\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n\n    // Randomly shuffle array elements\n    for (int i = 0; i < nums.Length; i++) {\n        int index = new Random().Next(i, nums.Length);\n        (nums[i], nums[index]) = (nums[index], nums[i]);\n    }\n    return nums;\n}\n\n/* Find the index of number 1 in array nums */\nint FindOne(int[] nums) {\n    for (int i = 0; i < nums.Length; i++) {\n        // When element 1 is at the head of the array, best time complexity O(1) is achieved\n        // When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
      worst_best_time_complexity.go
      /* Generate an array with elements { 1, 2, ..., n }, order shuffled */\nfunc randomNumbers(n int) []int {\n    nums := make([]int, n)\n    // Generate array nums = { 1, 2, 3, ..., n }\n    for i := 0; i < n; i++ {\n        nums[i] = i + 1\n    }\n    // Randomly shuffle array elements\n    rand.Shuffle(len(nums), func(i, j int) {\n        nums[i], nums[j] = nums[j], nums[i]\n    })\n    return nums\n}\n\n/* Find the index of number 1 in array nums */\nfunc findOne(nums []int) int {\n    for i := 0; i < len(nums); i++ {\n        // When element 1 is at the head of the array, best time complexity O(1) is achieved\n        // When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n        if nums[i] == 1 {\n            return i\n        }\n    }\n    return -1\n}\n
      worst_best_time_complexity.swift
      /* Generate an array with elements { 1, 2, ..., n }, order shuffled */\nfunc randomNumbers(n: Int) -> [Int] {\n    // Generate array nums = { 1, 2, 3, ..., n }\n    var nums = Array(1 ... n)\n    // Randomly shuffle array elements\n    nums.shuffle()\n    return nums\n}\n\n/* Find the index of number 1 in array nums */\nfunc findOne(nums: [Int]) -> Int {\n    for i in nums.indices {\n        // When element 1 is at the head of the array, best time complexity O(1) is achieved\n        // When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n        if nums[i] == 1 {\n            return i\n        }\n    }\n    return -1\n}\n
      worst_best_time_complexity.js
      /* Generate an array with elements { 1, 2, ..., n }, order shuffled */\nfunction randomNumbers(n) {\n    const nums = Array(n);\n    // Generate array nums = { 1, 2, 3, ..., n }\n    for (let i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // Randomly shuffle array elements\n    for (let i = 0; i < n; i++) {\n        const r = Math.floor(Math.random() * (i + 1));\n        const temp = nums[i];\n        nums[i] = nums[r];\n        nums[r] = temp;\n    }\n    return nums;\n}\n\n/* Find the index of number 1 in array nums */\nfunction findOne(nums) {\n    for (let i = 0; i < nums.length; i++) {\n        // When element 1 is at the head of the array, best time complexity O(1) is achieved\n        // When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n        if (nums[i] === 1) {\n            return i;\n        }\n    }\n    return -1;\n}\n
      worst_best_time_complexity.ts
      /* Generate an array with elements { 1, 2, ..., n }, order shuffled */\nfunction randomNumbers(n: number): number[] {\n    const nums = Array(n);\n    // Generate array nums = { 1, 2, 3, ..., n }\n    for (let i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // Randomly shuffle array elements\n    for (let i = 0; i < n; i++) {\n        const r = Math.floor(Math.random() * (i + 1));\n        const temp = nums[i];\n        nums[i] = nums[r];\n        nums[r] = temp;\n    }\n    return nums;\n}\n\n/* Find the index of number 1 in array nums */\nfunction findOne(nums: number[]): number {\n    for (let i = 0; i < nums.length; i++) {\n        // When element 1 is at the head of the array, best time complexity O(1) is achieved\n        // When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n        if (nums[i] === 1) {\n            return i;\n        }\n    }\n    return -1;\n}\n
      worst_best_time_complexity.dart
      /* Generate an array with elements { 1, 2, ..., n }, order shuffled */\nList<int> randomNumbers(int n) {\n  final nums = List.filled(n, 0);\n  // Generate array nums = { 1, 2, 3, ..., n }\n  for (var i = 0; i < n; i++) {\n    nums[i] = i + 1;\n  }\n  // Randomly shuffle array elements\n  nums.shuffle();\n\n  return nums;\n}\n\n/* Find the index of number 1 in array nums */\nint findOne(List<int> nums) {\n  for (var i = 0; i < nums.length; i++) {\n    // When element 1 is at the head of the array, best time complexity O(1) is achieved\n    // When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n    if (nums[i] == 1) return i;\n  }\n\n  return -1;\n}\n
      worst_best_time_complexity.rs
      /* Generate an array with elements { 1, 2, ..., n }, order shuffled */\nfn random_numbers(n: i32) -> Vec<i32> {\n    // Generate array nums = { 1, 2, 3, ..., n }\n    let mut nums = (1..=n).collect::<Vec<i32>>();\n    // Randomly shuffle array elements\n    nums.shuffle(&mut thread_rng());\n    nums\n}\n\n/* Find the index of number 1 in array nums */\nfn find_one(nums: &[i32]) -> Option<usize> {\n    for i in 0..nums.len() {\n        // When element 1 is at the head of the array, best time complexity O(1) is achieved\n        // When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n        if nums[i] == 1 {\n            return Some(i);\n        }\n    }\n    None\n}\n
      worst_best_time_complexity.c
      /* Generate an array with elements { 1, 2, ..., n }, order shuffled */\nint *randomNumbers(int n) {\n    // Allocate heap memory (create 1D variable-length array: n elements of type int)\n    int *nums = (int *)malloc(n * sizeof(int));\n    // Generate array nums = { 1, 2, 3, ..., n }\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // Randomly shuffle array elements\n    for (int i = n - 1; i > 0; i--) {\n        int j = rand() % (i + 1);\n        int temp = nums[i];\n        nums[i] = nums[j];\n        nums[j] = temp;\n    }\n    return nums;\n}\n\n/* Find the index of number 1 in array nums */\nint findOne(int *nums, int n) {\n    for (int i = 0; i < n; i++) {\n        // When element 1 is at the head of the array, best time complexity O(1) is achieved\n        // When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
      worst_best_time_complexity.kt
      /* Generate an array with elements { 1, 2, ..., n }, order shuffled */\nfun randomNumbers(n: Int): Array<Int?> {\n    val nums = IntArray(n)\n    // Generate array nums = { 1, 2, 3, ..., n }\n    for (i in 0..<n) {\n        nums[i] = i + 1\n    }\n    // Randomly shuffle array elements\n    nums.shuffle()\n    val res = arrayOfNulls<Int>(n)\n    for (i in 0..<n) {\n        res[i] = nums[i]\n    }\n    return res\n}\n\n/* Find the index of number 1 in array nums */\nfun findOne(nums: Array<Int?>): Int {\n    for (i in nums.indices) {\n        // When element 1 is at the head of the array, best time complexity O(1) is achieved\n        // When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n        if (nums[i] == 1)\n            return i\n    }\n    return -1\n}\n
      worst_best_time_complexity.rb
      ### Generate array with elements: 1, 2, ..., n, shuffled ###\ndef random_numbers(n)\n  # Generate array nums =: 1, 2, 3, ..., n\n  nums = Array.new(n) { |i| i + 1 }\n  # Randomly shuffle array elements\n  nums.shuffle!\nend\n\n### Find index of number 1 in array nums ###\ndef find_one(nums)\n  for i in 0...nums.length\n    # When element 1 is at the head of the array, best time complexity O(1) is achieved\n    # When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n    return i if nums[i] == 1\n  end\n\n  -1\nend\n

      It is worth noting that we rarely use best-case time complexity in practice, because it can usually only be achieved with a very small probability and may be somewhat misleading. The worst-case time complexity is more practical because it gives a safety value for efficiency, allowing us to use the algorithm with confidence.

      From the above example, we can see that both worst-case and best-case time complexities only occur under \"special data distributions\", which may have a very small probability of occurrence and may not truly reflect the algorithm's running efficiency. In contrast, average time complexity can reflect the algorithm's running efficiency under random input data, denoted using the \\(\\Theta\\) notation.

      For some algorithms, we can simply derive the average case under random data distribution. For example, in the above example, since the input array is shuffled, the probability of element \\(1\\) appearing at any index is equal, so the algorithm's average number of loops is half the array length \\(n / 2\\), giving an average time complexity of \\(\\Theta(n / 2) = \\Theta(n)\\).

      But for more complex algorithms, calculating average time complexity is often quite difficult, because it is hard to analyze the overall mathematical expectation under data distribution. In this case, we usually use worst-case time complexity as the criterion for judging algorithm efficiency.

      Why is the \\(\\Theta\\) symbol rarely seen?

      This may be because the \\(O\\) symbol is too catchy, so we often use it to represent average time complexity. But strictly speaking, this practice is not standard. In this book and other materials, if you encounter expressions like \"average time complexity \\(O(n)\\)\", please understand it directly as \\(\\Theta(n)\\).

      "},{"location":"chapter_data_structure/","title":"Chapter 3. \u00a0 Data Structures","text":"

      Abstract

      Data structure is like a sturdy and diverse framework.

      It provides a blueprint for the orderly organization of data, upon which algorithms come to life.

      "},{"location":"chapter_data_structure/#chapter-contents","title":"Chapter contents","text":"
      • 3.1 \u00a0 Classification of Data Structures
      • 3.2 \u00a0 Basic Data Types
      • 3.3 \u00a0 Number Encoding *
      • 3.4 \u00a0 Character Encoding *
      • 3.5 \u00a0 Summary
      "},{"location":"chapter_data_structure/basic_data_types/","title":"3.2 \u00a0 Basic Data Types","text":"

      When we talk about data in computers, we think of various forms such as text, images, videos, audio, 3D models, and more. Although these data are organized in different ways, they are all composed of various basic data types.

      Basic data types are types that the CPU can directly operate on, and they are directly used in algorithms, mainly including the following.

      • Integer types byte, short, int, long.
      • Floating-point types float, double, used to represent decimal numbers.
      • Character type char, used to represent letters, punctuation marks, and even emojis in various languages.
      • Boolean type bool, used to represent \"yes\" and \"no\" judgments.

      Basic data types are stored in binary form in computers. One binary bit is \\(1\\) bit. In most modern operating systems, \\(1\\) byte consists of \\(8\\) bits.

      The range of values for basic data types depends on the size of the space they occupy. Below is an example using Java.

      • Integer type byte occupies \\(1\\) byte = \\(8\\) bits, and can represent \\(2^{8}\\) numbers.
      • Integer type int occupies \\(4\\) bytes = \\(32\\) bits, and can represent \\(2^{32}\\) numbers.

      The following table lists the space occupied, value ranges, and default values of various basic data types in Java. You don't need to memorize this table; a general understanding is sufficient, and you can refer to it when needed.

      Table 3-1 \u00a0 Space occupied and value ranges of basic data types

      Type Symbol Space Occupied Minimum Value Maximum Value Default Value Integer byte 1 byte \\(-2^7\\) (\\(-128\\)) \\(2^7 - 1\\) (\\(127\\)) \\(0\\) short 2 bytes \\(-2^{15}\\) \\(2^{15} - 1\\) \\(0\\) int 4 bytes \\(-2^{31}\\) \\(2^{31} - 1\\) \\(0\\) long 8 bytes \\(-2^{63}\\) \\(2^{63} - 1\\) \\(0\\) Float float 4 bytes \\(1.175 \\times 10^{-38}\\) \\(3.403 \\times 10^{38}\\) \\(0.0\\text{f}\\) double 8 bytes \\(2.225 \\times 10^{-308}\\) \\(1.798 \\times 10^{308}\\) \\(0.0\\) Character char 2 bytes \\(0\\) \\(2^{16} - 1\\) \\(0\\) Boolean bool 1 byte \\(\\text{false}\\) \\(\\text{true}\\) \\(\\text{false}\\)

      Please note that the above table is specific to Java's basic data types. Each programming language has its own data type definitions, and their space occupied, value ranges, and default values may vary.

      • In Python, the integer type int can be of any size, limited only by available memory; the floating-point type float is double-precision 64-bit; there is no char type, a single character is actually a string str of length 1.
      • C and C++ do not explicitly specify the size of basic data types, which varies by implementation and platform. The above table follows the LP64 data model, which is used in Unix 64-bit operating systems including Linux and macOS.
      • The size of character char is 1 byte in C and C++, and in most programming languages it depends on the specific character encoding method, as detailed in the \"Character Encoding\" section.
      • Even though representing a boolean value requires only 1 bit (\\(0\\) or \\(1\\)), it is usually stored as 1 byte in memory. This is because modern computer CPUs typically use 1 byte as the minimum addressable memory unit.

      So, what is the relationship between basic data types and data structures? We know that data structures are ways of organizing and storing data in computers. The subject of this statement is \"structure\", not \"data\".

      If we want to represent \"a row of numbers\", we naturally think of using an array. This is because the linear structure of an array can represent the adjacency and order relationships of numbers, but the content stored\u2014whether integer int, floating-point float, or character char\u2014is unrelated to the \"data structure\".

      In other words, basic data types provide the \"content type\" of data, while data structures provide the \"organization method\" of data. For example, in the following code, we use the same data structure (array) to store and represent different basic data types, including int, float, char, bool, etc.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      # Initialize arrays using various basic data types\nnumbers: list[int] = [0] * 5\ndecimals: list[float] = [0.0] * 5\n# In Python, characters are actually strings of length 1\ncharacters: list[str] = ['0'] * 5\nbools: list[bool] = [False] * 5\n# Python lists can freely store various basic data types and object references\ndata = [0, 0.0, 'a', False, ListNode(0)]\n
      // Initialize arrays using various basic data types\nint numbers[5];\nfloat decimals[5];\nchar characters[5];\nbool bools[5];\n
      // Initialize arrays using various basic data types\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nboolean[] bools = new boolean[5];\n
      // Initialize arrays using various basic data types\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nbool[] bools = new bool[5];\n
      // Initialize arrays using various basic data types\nvar numbers = [5]int{}\nvar decimals = [5]float64{}\nvar characters = [5]byte{}\nvar bools = [5]bool{}\n
      // Initialize arrays using various basic data types\nlet numbers = Array(repeating: 0, count: 5)\nlet decimals = Array(repeating: 0.0, count: 5)\nlet characters: [Character] = Array(repeating: \"a\", count: 5)\nlet bools = Array(repeating: false, count: 5)\n
      // JavaScript arrays can freely store various basic data types and objects\nconst array = [0, 0.0, 'a', false];\n
      // Initialize arrays using various basic data types\nconst numbers: number[] = [];\nconst characters: string[] = [];\nconst bools: boolean[] = [];\n
      // Initialize arrays using various basic data types\nList<int> numbers = List.filled(5, 0);\nList<double> decimals = List.filled(5, 0.0);\nList<String> characters = List.filled(5, 'a');\nList<bool> bools = List.filled(5, false);\n
      // Initialize arrays using various basic data types\nlet numbers: Vec<i32> = vec![0; 5];\nlet decimals: Vec<f32> = vec![0.0; 5];\nlet characters: Vec<char> = vec!['0'; 5];\nlet bools: Vec<bool> = vec![false; 5];\n
      // Initialize arrays using various basic data types\nint numbers[10];\nfloat decimals[10];\nchar characters[10];\nbool bools[10];\n
      // Initialize arrays using various basic data types\nval numbers = IntArray(5)\nval decinals = FloatArray(5)\nval characters = CharArray(5)\nval bools = BooleanArray(5)\n
      # Ruby lists can freely store various basic data types and object references\ndata = [0, 0.0, 'a', false, ListNode(0)]\n
      Visualized Execution

      https://pythontutor.com/render.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E4%BD%BF%E7%94%A8%E5%A4%9A%E7%A7%8D%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E6%9D%A5%E5%88%9D%E5%A7%8B%E5%8C%96%E6%95%B0%E7%BB%84%0A%20%20%20%20numbers%20%3D%20%5B0%5D%20*%205%0A%20%20%20%20decimals%20%3D%20%5B0.0%5D%20*%205%0A%20%20%20%20%23%20Python%20%E7%9A%84%E5%AD%97%E7%AC%A6%E5%AE%9E%E9%99%85%E4%B8%8A%E6%98%AF%E9%95%BF%E5%BA%A6%E4%B8%BA%201%20%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2%0A%20%20%20%20characters%20%3D%20%5B'0'%5D%20*%205%0A%20%20%20%20bools%20%3D%20%5BFalse%5D%20*%205%0A%20%20%20%20%23%20Python%20%E7%9A%84%E5%88%97%E8%A1%A8%E5%8F%AF%E4%BB%A5%E8%87%AA%E7%94%B1%E5%AD%98%E5%82%A8%E5%90%84%E7%A7%8D%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%92%8C%E5%AF%B9%E8%B1%A1%E5%BC%95%E7%94%A8%0A%20%20%20%20data%20%3D%20%5B0,%200.0,%20'a',%20False,%20ListNode%280%29%5D&cumulative=false&curInstr=12&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

      "},{"location":"chapter_data_structure/character_encoding/","title":"3.4 \u00a0 Character Encoding *","text":"

      In computers, all data is stored in binary form, and character char is no exception. To represent characters, we need to establish a \"character set\" that defines a one-to-one correspondence between each character and binary numbers. With a character set, computers can convert binary numbers to characters by looking up the table.

      "},{"location":"chapter_data_structure/character_encoding/#341-ascii-character-set","title":"3.4.1 \u00a0 Ascii Character Set","text":"

      ASCII code is the earliest character set, with the full name American Standard Code for Information Interchange. It uses 7 binary bits (the lower 7 bits of one byte) to represent a character, and can represent a maximum of 128 different characters. As shown in Figure 3-6, ASCII code includes uppercase and lowercase English letters, numbers 0 ~ 9, some punctuation marks, and some control characters (such as newline and tab).

      Figure 3-6 \u00a0 ASCII code

      However, ASCII code can only represent English. With the globalization of computers, a character set called EASCII that can represent more languages emerged. It expands from the 7-bit basis of ASCII to 8 bits, and can represent 256 different characters.

      Worldwide, a batch of EASCII character sets suitable for different regions have appeared successively. The first 128 characters of these character sets are unified as ASCII code, and the last 128 characters are defined differently to adapt to the needs of different languages.

      "},{"location":"chapter_data_structure/character_encoding/#342-gbk-character-set","title":"3.4.2 \u00a0 Gbk Character Set","text":"

      Later, people found that EASCII code still cannot meet the character quantity requirements of many languages. For example, there are nearly one hundred thousand Chinese characters, and several thousand are used daily. In 1980, the China National Standardization Administration released the GB2312 character set, which included 6,763 Chinese characters, basically meeting the needs for computer processing of Chinese characters.

      However, GB2312 cannot handle some rare characters and traditional Chinese characters. The GBK character set is an extension based on GB2312, which includes a total of 21,886 Chinese characters. In the GBK encoding scheme, ASCII characters are represented using one byte, and Chinese characters are represented using two bytes.

      "},{"location":"chapter_data_structure/character_encoding/#343-unicode-character-set","title":"3.4.3 \u00a0 Unicode Character Set","text":"

      With the vigorous development of computer technology, character sets and encoding standards flourished, which brought many problems. On the one hand, these character sets generally only define characters for specific languages and cannot work normally in multilingual environments. On the other hand, multiple character set standards exist for the same language, and if two computers use different encoding standards, garbled characters will appear during information transmission.

      Researchers of that era thought: If a sufficiently complete character set is released that includes all languages and symbols in the world, wouldn't it be possible to solve cross-language environment and garbled character problems? Driven by this idea, a large and comprehensive character set, Unicode, was born.

      Unicode is called \"\u7edf\u4e00\u7801\" (Unified Code) in Chinese and can theoretically accommodate over one million characters. It is committed to including characters from around the world into a unified character set, providing a universal character set to handle and display various language texts, reducing garbled character problems caused by different encoding standards.

      Since its release in 1991, Unicode has continuously expanded to include new languages and characters. As of September 2022, Unicode has included 149,186 characters, including characters, symbols, and even emojis from various languages. In the vast Unicode character set, commonly used characters occupy 2 bytes, and some rare characters occupy 3 bytes or even 4 bytes.

      Unicode is a universal character set that essentially assigns a number (called a \"code point\") to each character, but it does not specify how to store these character code points in computers. We can't help but ask: when Unicode code points of multiple lengths appear simultaneously in a text, how does the system parse the characters? For example, given an encoding with a length of 2 bytes, how does the system determine whether it is one 2-byte character or two 1-byte characters?

      For the above problem, a straightforward solution is to store all characters as equal-length encodings. As shown in Figure 3-7, each character in \"Hello\" occupies 1 byte, and each character in \"\u7b97\u6cd5\" (algorithm) occupies 2 bytes. We can encode all characters in \"Hello \u7b97\u6cd5\" as 2 bytes in length by padding the high bits with 0. In this way, the system can parse one character every 2 bytes and restore the content of this phrase.

      Figure 3-7 \u00a0 Unicode encoding example

      However, ASCII code has already proven to us that encoding English only requires 1 byte. If the above scheme is adopted, the size of English text will be twice that under ASCII encoding, which is very wasteful of memory space. Therefore, we need a more efficient Unicode encoding method.

      "},{"location":"chapter_data_structure/character_encoding/#344-utf-8-encoding","title":"3.4.4 \u00a0 Utf-8 Encoding","text":"

      Currently, UTF-8 has become the most widely used Unicode encoding method internationally. It is a variable-length encoding that uses 1 to 4 bytes to represent a character, depending on the complexity of the character. ASCII characters only require 1 byte, Latin and Greek letters require 2 bytes, commonly used Chinese characters require 3 bytes, and some other rare characters require 4 bytes.

      The encoding rules of UTF-8 are not complicated and can be divided into the following two cases.

      • For 1-byte characters, set the highest bit to \\(0\\), and set the remaining 7 bits to the Unicode code point. It is worth noting that ASCII characters occupy the first 128 code points in the Unicode character set. That is to say, UTF-8 encoding is backward compatible with ASCII code. This means we can use UTF-8 to parse very old ASCII code text.
      • For characters with a length of \\(n\\) bytes (where \\(n > 1\\)), set the highest \\(n\\) bits of the first byte to \\(1\\), and set the \\((n + 1)\\)-th bit to \\(0\\); starting from the second byte, set the highest 2 bits of each byte to \\(10\\); use all remaining bits to fill in the Unicode code point of the character.

      Figure 3-8 shows the UTF-8 encoding corresponding to \"Hello\u7b97\u6cd5\". It can be observed that since the highest \\(n\\) bits are all set to \\(1\\), the system can parse the length of the character as \\(n\\) by reading the number of highest bits that are \\(1\\).

      But why set the highest 2 bits of all other bytes to \\(10\\)? In fact, this \\(10\\) can serve as a check symbol. Assuming the system starts parsing text from an incorrect byte, the \\(10\\) at the beginning of the byte can help the system quickly determine an anomaly.

      The reason for using \\(10\\) as a check symbol is that under UTF-8 encoding rules, it is impossible for a character's highest two bits to be \\(10\\). This conclusion can be proven by contradiction: assuming the highest two bits of a character are \\(10\\), it means the length of the character is \\(1\\), corresponding to ASCII code. However, the highest bit of ASCII code should be \\(0\\), which contradicts the assumption.

      Figure 3-8 \u00a0 UTF-8 encoding example

      In addition to UTF-8, common encoding methods also include the following two.

      • UTF-16 encoding: Uses 2 or 4 bytes to represent a character. All ASCII characters and commonly used non-English characters are represented with 2 bytes; a few characters need to use 4 bytes. For 2-byte characters, UTF-16 encoding is equal to the Unicode code point.
      • UTF-32 encoding: Every character uses 4 bytes. This means that UTF-32 takes up more space than UTF-8 and UTF-16, especially for text with a high proportion of ASCII characters.

      From the perspective of storage space occupation, using UTF-8 to represent English characters is very efficient because it only requires 1 byte; using UTF-16 encoding for some non-English characters (such as Chinese) will be more efficient because it only requires 2 bytes, while UTF-8 may require 3 bytes.

      From a compatibility perspective, UTF-8 has the best universality, and many tools and libraries support UTF-8 first.

      "},{"location":"chapter_data_structure/character_encoding/#345-character-encoding-in-programming-languages","title":"3.4.5 \u00a0 Character Encoding in Programming Languages","text":"

      For most past programming languages, strings during program execution use fixed-length encodings such as UTF-16 or UTF-32. Under fixed-length encoding, we can treat strings as arrays for processing, and this approach has the following advantages.

      • Random access: UTF-16 encoded strings can be easily accessed randomly. UTF-8 is a variable-length encoding. To find the \\(i\\)-th character, we need to traverse from the beginning of the string to the \\(i\\)-th character, which requires \\(O(n)\\) time.
      • Character counting: Similar to random access, calculating the length of a UTF-16 encoded string is also an \\(O(1)\\) operation. However, calculating the length of a UTF-8 encoded string requires traversing the entire string.
      • String operations: Many string operations (such as splitting, joining, inserting, deleting, etc.) on UTF-16 encoded strings are easier to perform. Performing these operations on UTF-8 encoded strings usually requires additional calculations to ensure that invalid UTF-8 encoding is not generated.

      In fact, the design of character encoding schemes for programming languages is a very interesting topic involving many factors.

      • Java's String type uses UTF-16 encoding, with each character occupying 2 bytes. This is because at the beginning of Java language design, people believed that 16 bits were sufficient to represent all possible characters. However, this was an incorrect judgment. Later, the Unicode specification expanded beyond 16 bits, so characters in Java may now be represented by a pair of 16-bit values (called \"surrogate pairs\").
      • The strings of JavaScript and TypeScript use UTF-16 encoding for reasons similar to Java. When Netscape first introduced the JavaScript language in 1995, Unicode was still in its early stages of development, and at that time, using 16-bit encoding was sufficient to represent all Unicode characters.
      • C# uses UTF-16 encoding mainly because the .NET platform was designed by Microsoft, and many of Microsoft's technologies (including the Windows operating system) extensively use UTF-16 encoding.

      Due to the underestimation of character quantities by the above programming languages, they had to adopt the \"surrogate pair\" method to represent Unicode characters with lengths exceeding 16 bits. This is a reluctant compromise. On the one hand, in strings containing surrogate pairs, one character may occupy 2 bytes or 4 bytes, thus losing the advantage of fixed-length encoding. On the other hand, handling surrogate pairs requires additional code, which increases the complexity and difficulty of debugging in programming.

      For the above reasons, some programming languages have proposed different encoding schemes.

      • Python's str uses Unicode encoding and adopts a flexible string representation where the stored character length depends on the largest Unicode code point in the string. If all characters in the string are ASCII characters, each character occupies 1 byte; if there are characters exceeding the ASCII range but all within the Basic Multilingual Plane (BMP), each character occupies 2 bytes; if there are characters exceeding the BMP, each character occupies 4 bytes.
      • Go language's string type uses UTF-8 encoding internally. Go language also provides the rune type, which is used to represent a single Unicode code point.
      • Rust language's str and String types use UTF-8 encoding internally. Rust also provides the char type for representing a single Unicode code point.

      It should be noted that the above discussion is about how strings are stored in programming languages, which is different from how strings are stored in files or transmitted over networks. In file storage or network transmission, we usually encode strings into UTF-8 format to achieve optimal compatibility and space efficiency.

      "},{"location":"chapter_data_structure/classification_of_data_structure/","title":"3.1 \u00a0 Classification of Data Structures","text":"

      Common data structures include arrays, linked lists, stacks, queues, hash tables, trees, heaps, and graphs. They can be classified from two dimensions: \"logical structure\" and \"physical structure\".

      "},{"location":"chapter_data_structure/classification_of_data_structure/#311-logical-structure-linear-and-non-linear","title":"3.1.1 \u00a0 Logical Structure: Linear and Non-Linear","text":"

      Logical structure reveals the logical relationships between data elements. In arrays and linked lists, data is arranged in a certain order, embodying the linear relationship between data; while in trees, data is arranged hierarchically from top to bottom, showing the derived relationship between \"ancestors\" and \"descendants\"; graphs are composed of nodes and edges, reflecting complex network relationships.

      As shown in Figure 3-1, logical structures can be divided into two major categories: \"linear\" and \"non-linear\". Linear structures are more intuitive, indicating that data is linearly arranged in logical relationships; non-linear structures are the opposite, arranged non-linearly.

      • Linear data structures: Arrays, linked lists, stacks, queues, hash tables, where elements have a one-to-one sequential relationship.
      • Non-linear data structures: Trees, heaps, graphs, hash tables.

      Non-linear data structures can be further divided into tree structures and network structures.

      • Tree structures: Trees, heaps, hash tables, where elements have a one-to-many relationship.
      • Network structures: Graphs, where elements have a many-to-many relationship.

      Figure 3-1 \u00a0 Linear and non-linear data structures

      "},{"location":"chapter_data_structure/classification_of_data_structure/#312-physical-structure-contiguous-and-dispersed","title":"3.1.2 \u00a0 Physical Structure: Contiguous and Dispersed","text":"

      When an algorithm program runs, the data being processed is mainly stored in memory. Figure 3-2 shows a computer memory stick, where each black square contains a memory space. We can imagine memory as a huge Excel spreadsheet, where each cell can store a certain amount of data.

      The system accesses data at the target location through memory addresses. As shown in Figure 3-2, the computer assigns a number to each cell in the spreadsheet according to specific rules, ensuring that each memory space has a unique memory address. With these addresses, the program can access data in memory.

      Figure 3-2 \u00a0 Memory stick, memory space, memory address

      Tip

      It is worth noting that comparing memory to an Excel spreadsheet is a simplified analogy. The actual working mechanism of memory is quite complex, involving concepts such as address space, memory management, cache mechanisms, virtual memory, and physical memory.

      Memory is a shared resource for all programs. When a block of memory is occupied by a program, it usually cannot be used by other programs at the same time. Therefore, in the design of data structures and algorithms, memory resources are an important consideration. For example, the peak memory occupied by an algorithm should not exceed the remaining free memory of the system; if there is a lack of contiguous large memory blocks, then the data structure chosen must be able to be stored in dispersed memory spaces.

      As shown in Figure 3-3, physical structure reflects the way data is stored in computer memory, and can be divided into contiguous space storage (arrays) and dispersed space storage (linked lists). The two physical structures exhibit complementary characteristics in terms of time efficiency and space efficiency.

      Figure 3-3 \u00a0 Contiguous space storage and dispersed space storage

      It is worth noting that all data structures are implemented based on arrays, linked lists, or a combination of both. For example, stacks and queues can be implemented using either arrays or linked lists; while the implementation of hash tables may include both arrays and linked lists.

      • Can be implemented based on arrays: Stacks, queues, hash tables, trees, heaps, graphs, matrices, tensors (arrays with dimensions \\(\\geq 3\\)), etc.
      • Can be implemented based on linked lists: Stacks, queues, hash tables, trees, heaps, graphs, etc.

      After initialization, linked lists can still adjust their length during program execution, so they are also called \"dynamic data structures\". After initialization, the length of arrays cannot be changed, so they are also called \"static data structures\". It is worth noting that arrays can achieve length changes by reallocating memory, thus possessing a certain degree of \"dynamism\".

      Tip

      If you find it difficult to understand physical structure, it is recommended to read the next chapter first, and then review this section.

      "},{"location":"chapter_data_structure/number_encoding/","title":"3.3 \u00a0 Number Encoding *","text":"

      Tip

      In this book, chapters marked with an asterisk * are optional readings. If you are short on time or find them challenging, you may skip these initially and return to them after completing the essential chapters.

      "},{"location":"chapter_data_structure/number_encoding/#331-sign-magnitude-1s-complement-and-2s-complement","title":"3.3.1 \u00a0 Sign-Magnitude, 1's Complement, and 2's Complement","text":"

      In the table from the previous section, we found that all integer types can represent one more negative number than positive numbers. For example, the byte range is \\([-128, 127]\\). This phenomenon is counterintuitive, and its underlying reason involves knowledge of sign-magnitude, 1's complement, and 2's complement.

      First, it should be noted that numbers are stored in computers in the form of \"2's complement\". Before analyzing the reasons for this, let's first define these three concepts.

      • Sign-magnitude: We treat the highest bit of the binary representation of a number as the sign bit, where \\(0\\) represents a positive number and \\(1\\) represents a negative number, and the remaining bits represent the value of the number.
      • 1's complement: The 1's complement of a positive number is the same as its sign-magnitude. For a negative number, the 1's complement is obtained by inverting all bits except the sign bit of its sign-magnitude.
      • 2's complement: The 2's complement of a positive number is the same as its sign-magnitude. For a negative number, the 2's complement is obtained by adding \\(1\\) to its 1's complement.

      Figure 3-4 shows the conversion methods among sign-magnitude, 1's complement, and 2's complement.

      Figure 3-4 \u00a0 Conversions among sign-magnitude, 1's complement, and 2's complement

      Sign-magnitude, although the most intuitive, has some limitations. On one hand, the sign-magnitude of negative numbers cannot be directly used in operations. For example, calculating \\(1 + (-2)\\) in sign-magnitude yields \\(-3\\), which is clearly incorrect.

      \\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 + 1000 \\; 0010 \\newline & = 1000 \\; 0011 \\newline & \\rightarrow -3 \\end{aligned} \\]

      To solve this problem, computers introduced 1's complement. If we first convert sign-magnitude to 1's complement and calculate \\(1 + (-2)\\) in 1's complement, then convert the result back to sign-magnitude, we can obtain the correct result of \\(-1\\).

      \\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 \\; \\text{(Sign-magnitude)} + 1000 \\; 0010 \\; \\text{(Sign-magnitude)} \\newline & = 0000 \\; 0001 \\; \\text{(1's complement)} + 1111 \\; 1101 \\; \\text{(1's complement)} \\newline & = 1111 \\; 1110 \\; \\text{(1's complement)} \\newline & = 1000 \\; 0001 \\; \\text{(Sign-magnitude)} \\newline & \\rightarrow -1 \\end{aligned} \\]

      On the other hand, the sign-magnitude of the number zero has two representations, \\(+0\\) and \\(-0\\). This means that the number zero corresponds to two different binary encodings, which may cause ambiguity. For example, in conditional judgments, if we don't distinguish between positive zero and negative zero, it may lead to incorrect judgment results. If we want to handle the ambiguity of positive and negative zero, we need to introduce additional judgment operations, which may reduce the computational efficiency of the computer.

      \\[ \\begin{aligned} +0 & \\rightarrow 0000 \\; 0000 \\newline -0 & \\rightarrow 1000 \\; 0000 \\end{aligned} \\]

      Like sign-magnitude, 1's complement also has the problem of positive and negative zero ambiguity. Therefore, computers further introduced 2's complement. Let's first observe the conversion process of negative zero from sign-magnitude to 1's complement to 2's complement:

      \\[ \\begin{aligned} -0 \\rightarrow \\; & 1000 \\; 0000 \\; \\text{(Sign-magnitude)} \\newline = \\; & 1111 \\; 1111 \\; \\text{(1's complement)} \\newline = 1 \\; & 0000 \\; 0000 \\; \\text{(2's complement)} \\newline \\end{aligned} \\]

      Adding \\(1\\) to the 1's complement of negative zero produces a carry, but since the byte type has a length of only 8 bits, the \\(1\\) that overflows to the 9th bit is discarded. That is to say, the 2's complement of negative zero is \\(0000 \\; 0000\\), which is the same as the 2's complement of positive zero. This means that in 2's complement representation, there is only one zero, and the positive and negative zero ambiguity is thus resolved.

      One last question remains: the range of the byte type is \\([-128, 127]\\), and how is the extra negative number \\(-128\\) obtained? We notice that all integers in the interval \\([-127, +127]\\) have corresponding sign-magnitude, 1's complement, and 2's complement, and sign-magnitude and 2's complement can be converted to each other.

      However, the 2's complement \\(1000 \\; 0000\\) is an exception, and it does not have a corresponding sign-magnitude. According to the conversion method, we get that the sign-magnitude of this 2's complement is \\(0000 \\; 0000\\). This is clearly contradictory because this sign-magnitude represents the number \\(0\\), and its 2's complement should be itself. The computer specifies that this special 2's complement \\(1000 \\; 0000\\) represents \\(-128\\). In fact, the result of calculating \\((-1) + (-127)\\) in 2's complement is \\(-128\\).

      \\[ \\begin{aligned} & (-127) + (-1) \\newline & \\rightarrow 1111 \\; 1111 \\; \\text{(Sign-magnitude)} + 1000 \\; 0001 \\; \\text{(Sign-magnitude)} \\newline & = 1000 \\; 0000 \\; \\text{(1's complement)} + 1111 \\; 1110 \\; \\text{(1's complement)} \\newline & = 1000 \\; 0001 \\; \\text{(2's complement)} + 1111 \\; 1111 \\; \\text{(2's complement)} \\newline & = 1000 \\; 0000 \\; \\text{(2's complement)} \\newline & \\rightarrow -128 \\end{aligned} \\]

      You may have noticed that all the above calculations are addition operations. This hints at an important fact: the hardware circuits inside computers are mainly designed based on addition operations. This is because addition operations are simpler to implement in hardware compared to other operations (such as multiplication, division, and subtraction), easier to parallelize, and have faster operation speeds.

      Please note that this does not mean that computers can only perform addition. By combining addition with some basic logical operations, computers can implement various other mathematical operations. For example, calculating the subtraction \\(a - b\\) can be converted to calculating the addition \\(a + (-b)\\); calculating multiplication and division can be converted to calculating multiple additions or subtractions.

      Now we can summarize the reasons why computers use 2's complement: based on 2's complement representation, computers can use the same circuits and operations to handle the addition of positive and negative numbers, without the need to design special hardware circuits to handle subtraction, and without the need to specially handle the ambiguity problem of positive and negative zero. This greatly simplifies hardware design and improves operational efficiency.

      The design of 2's complement is very ingenious. Due to space limitations, we will stop here. Interested readers are encouraged to explore further.

      "},{"location":"chapter_data_structure/number_encoding/#332-floating-point-number-encoding","title":"3.3.2 \u00a0 Floating-Point Number Encoding","text":"

      Careful readers may have noticed: int and float have the same length, both are 4 bytes, but why does float have a much larger range than int? This is very counterintuitive because it stands to reason that float needs to represent decimals, so the range should be smaller.

      In fact, this is because floating-point number float uses a different representation method. Let's denote a 32-bit binary number as:

      \\[ b_{31} b_{30} b_{29} \\ldots b_2 b_1 b_0 \\]

      According to the IEEE 754 standard, a 32-bit float consists of the following three parts.

      • Sign bit \\(\\mathrm{S}\\): occupies 1 bit, corresponding to \\(b_{31}\\).
      • Exponent bit \\(\\mathrm{E}\\): occupies 8 bits, corresponding to \\(b_{30} b_{29} \\ldots b_{23}\\).
      • Fraction bit \\(\\mathrm{N}\\): occupies 23 bits, corresponding to \\(b_{22} b_{21} \\ldots b_0\\).

      The calculation method for the value corresponding to the binary float is:

      \\[ \\text {val} = (-1)^{b_{31}} \\times 2^{\\left(b_{30} b_{29} \\ldots b_{23}\\right)_2-127} \\times\\left(1 . b_{22} b_{21} \\ldots b_0\\right)_2 \\]

      Converted to decimal, the calculation formula is:

      \\[ \\text {val}=(-1)^{\\mathrm{S}} \\times 2^{\\mathrm{E} -127} \\times (1 + \\mathrm{N}) \\]

      The range of each component is:

      \\[ \\begin{aligned} \\mathrm{S} \\in & \\{ 0, 1\\}, \\quad \\mathrm{E} \\in \\{ 1, 2, \\dots, 254 \\} \\newline (1 + \\mathrm{N}) = & (1 + \\sum_{i=1}^{23} b_{23-i} 2^{-i}) \\subset [1, 2 - 2^{-23}] \\end{aligned} \\]

      Figure 3-5 \u00a0 Calculation example of float under IEEE 754 standard

      Observing Figure 3-5, given example data \\(\\mathrm{S} = 0\\), \\(\\mathrm{E} = 124\\), \\(\\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\\), we have:

      \\[ \\text { val } = (-1)^0 \\times 2^{124 - 127} \\times (1 + 0.375) = 0.171875 \\]

      Now we can answer the initial question: the representation of float includes an exponent bit, resulting in a range far greater than int. According to the above calculation, the maximum positive number that float can represent is \\(2^{254 - 127} \\times (2 - 2^{-23}) \\approx 3.4 \\times 10^{38}\\), and the minimum negative number can be obtained by switching the sign bit.

      Although floating-point number float expands the range, its side effect is sacrificing precision. The integer type int uses all 32 bits to represent numbers, and the numbers are evenly distributed; however, due to the existence of the exponent bit, the larger the value of floating-point number float, the larger the difference between two adjacent numbers tends to be.

      As shown in Table 3-2, exponent bits \\(\\mathrm{E} = 0\\) and \\(\\mathrm{E} = 255\\) have special meanings, used to represent zero, infinity, \\(\\mathrm{NaN}\\), etc.

      Table 3-2 \u00a0 Meaning of exponent bits

      Exponent Bit E Fraction Bit \\(\\mathrm{N} = 0\\) Fraction Bit \\(\\mathrm{N} \\ne 0\\) Calculation Formula \\(0\\) \\(\\pm 0\\) Subnormal Number \\((-1)^{\\mathrm{S}} \\times 2^{-126} \\times (0.\\mathrm{N})\\) \\(1, 2, \\dots, 254\\) Normal Number Normal Number \\((-1)^{\\mathrm{S}} \\times 2^{(\\mathrm{E} -127)} \\times (1.\\mathrm{N})\\) \\(255\\) \\(\\pm \\infty\\) \\(\\mathrm{NaN}\\)

      It is worth noting that subnormal numbers significantly improve the precision of floating-point numbers. The smallest positive normal number is \\(2^{-126}\\), and the smallest positive subnormal number is \\(2^{-126} \\times 2^{-23}\\).

      Double-precision double also uses a representation method similar to float, which will not be elaborated here.

      "},{"location":"chapter_data_structure/summary/","title":"3.5 \u00a0 Summary","text":""},{"location":"chapter_data_structure/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • Data structures can be classified from two perspectives: logical structure and physical structure. Logical structure describes the logical relationships between data elements, while physical structure describes how data is stored in computer memory.
      • Common logical structures include linear, tree, and network structures. We typically classify data structures as linear (arrays, linked lists, stacks, queues) and non-linear (trees, graphs, heaps) based on their logical structure. The implementation of hash tables may involve both linear and non-linear data structures.
      • When a program runs, data is stored in computer memory. Each memory space has a corresponding memory address, and the program accesses data through these memory addresses.
      • Physical structures are primarily divided into contiguous space storage (arrays) and dispersed space storage (linked lists). All data structures are implemented using arrays, linked lists, or a combination of both.
      • Basic data types in computers include integers byte, short, int, long, floating-point numbers float, double, characters char, and booleans bool. Their value ranges depend on the size of space they occupy and their representation method.
      • Sign-magnitude, 1's complement, and 2's complement are three methods for encoding numbers in computers, and they can be converted into each other. The most significant bit of sign-magnitude is the sign bit, and the remaining bits represent the value of the number.
      • Integers are stored in computers in 2's complement form. Under 2's complement representation, computers can treat the addition of positive and negative numbers uniformly, without needing to design special hardware circuits for subtraction, and there is no ambiguity of positive and negative zero.
      • The encoding of floating-point numbers consists of 1 sign bit, 8 exponent bits, and 23 fraction bits. Due to the exponent bits, the range of floating-point numbers is much larger than that of integers, at the cost of sacrificing precision.
      • ASCII is the earliest English character set, with a length of 1 byte, containing a total of 127 characters. GBK is a commonly used Chinese character set, containing over 20,000 Chinese characters. Unicode is committed to providing a complete character set standard, collecting characters from various languages around the world, thereby solving the garbled text problem caused by inconsistent character encoding methods.
      • UTF-8 is the most popular Unicode encoding method, with excellent universality. It is a variable-length encoding method with good scalability, effectively improving storage space efficiency. UTF-16 and UTF-32 are fixed-length encoding methods. When encoding Chinese characters, UTF-16 occupies less space than UTF-8. Programming languages such as Java and C# use UTF-16 encoding by default.
      "},{"location":"chapter_data_structure/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

      Q: Why do hash tables contain both linear and non-linear data structures?

      The underlying structure of a hash table is an array. To resolve hash collisions, we may use \"chaining\" (discussed in the subsequent \"Hash Collision\" section): each bucket in the array points to a linked list, which may be converted to a tree (usually a red-black tree) when the list length exceeds a certain threshold.

      From a storage perspective, the underlying structure of a hash table is an array, where each bucket slot may contain a value, a linked list, or a tree. Therefore, hash tables may contain both linear data structures (arrays, linked lists) and non-linear data structures (trees).

      Q: Is the length of the char type 1 byte?

      The length of the char type is determined by the encoding method used by the programming language. For example, Java, JavaScript, TypeScript, and C# all use UTF-16 encoding (to store Unicode code points), so the char type has a length of 2 bytes.

      Q: Is there ambiguity in referring to array-based data structures as \"static data structures\"? Stacks can also perform \"dynamic\" operations such as push and pop.

      Stacks can indeed implement dynamic data operations, but the data structure is still \"static\" (fixed length). Although array-based data structures can dynamically add or remove elements, their capacity is fixed. If the data volume exceeds the pre-allocated size, a new larger array needs to be created, and the contents of the old array must be copied to the new array.

      Q: When constructing a stack (queue), its size is not specified. Why are they \"static data structures\"?

      In high-level programming languages, we do not need to manually specify the initial capacity of a stack (queue); this work is automatically completed within the class. For example, the initial capacity of Java's ArrayList is typically 10. Additionally, the expansion operation is also automatically implemented. See the subsequent \"List\" section for details.

      Q: The method of converting sign-magnitude to 2's complement is \"first negate then add 1\". So converting 2's complement to sign-magnitude should be the inverse operation \"first subtract 1 then negate\". However, 2's complement can also be converted to sign-magnitude through \"first negate then add 1\". Why is this?

      This is because the mutual conversion between sign-magnitude and 2's complement is actually the process of computing the \"complement\". Let us first define the complement: assuming \\(a + b = c\\), then we say that \\(a\\) is the complement of \\(b\\) to \\(c\\), and conversely, \\(b\\) is the complement of \\(a\\) to \\(c\\).

      Given an \\(n = 4\\) bit binary number \\(0010\\), if we treat this number as sign-magnitude (ignoring the sign bit), then its 2's complement can be obtained through \"first negate then add 1\":

      \\[ 0010 \\rightarrow 1101 \\rightarrow 1110 \\]

      We find that the sum of sign-magnitude and 2's complement is \\(0010 + 1110 = 10000\\), which means the 2's complement \\(1110\\) is the \"complement\" of sign-magnitude \\(0010\\) to \\(10000\\). This means the above \"first negate then add 1\" is actually the process of computing the complement to \\(10000\\).

      So, what is the \"complement\" of 2's complement \\(1110\\) to \\(10000\\)? We can still use \"first negate then add 1\" to obtain it:

      \\[ 1110 \\rightarrow 0001 \\rightarrow 0010 \\]

      In other words, sign-magnitude and 2's complement are each other's \"complement\" to \\(10000\\), so \"sign-magnitude to 2's complement\" and \"2's complement to sign-magnitude\" can be implemented using the same operation (first negate then add 1).

      Of course, we can also use the inverse operation to find the sign-magnitude of 2's complement \\(1110\\), that is, \"first subtract 1 then negate\":

      \\[ 1110 \\rightarrow 1101 \\rightarrow 0010 \\]

      In summary, both \"first negate then add 1\" and \"first subtract 1 then negate\" are computing the complement to \\(10000\\), and they are equivalent.

      Essentially, the \"negate\" operation is actually finding the complement to \\(1111\\) (because sign-magnitude + 1's complement = 1111 always holds); and adding 1 to the 1's complement yields the 2's complement, which is the complement to \\(10000\\).

      The above uses \\(n = 4\\) as an example, and it can be generalized to binary numbers of any number of bits.

      "},{"location":"chapter_divide_and_conquer/","title":"Chapter 12. \u00a0 Divide and Conquer","text":"

      Abstract

      Difficult problems are decomposed layer by layer, with each decomposition making them simpler.

      Divide and conquer reveals an important truth: start with simplicity, and nothing remains complex.

      "},{"location":"chapter_divide_and_conquer/#chapter-contents","title":"Chapter contents","text":"
      • 12.1 \u00a0 Divide and Conquer Algorithms
      • 12.2 \u00a0 Divide and Conquer Search Strategy
      • 12.3 \u00a0 Building a Binary Tree Problem
      • 12.4 \u00a0 Hanoi Tower Problem
      • 12.5 \u00a0 Summary
      "},{"location":"chapter_divide_and_conquer/binary_search_recur/","title":"12.2 \u00a0 Divide and Conquer Search Strategy","text":"

      We have already learned that search algorithms are divided into two major categories.

      • Brute-force search: Implemented by traversing the data structure, with a time complexity of \\(O(n)\\).
      • Adaptive search: Utilizes unique data organization forms or prior information, with time complexity reaching \\(O(\\log n)\\) or even \\(O(1)\\).

      In fact, search algorithms with time complexity of \\(O(\\log n)\\) are typically implemented based on the divide and conquer strategy, such as binary search and trees.

      • Each step of binary search divides the problem (searching for a target element in an array) into a smaller problem (searching for the target element in half of the array), continuing until the array is empty or the target element is found.
      • Trees are representative of the divide and conquer idea. In data structures such as binary search trees, AVL trees, and heaps, the time complexity of various operations is \\(O(\\log n)\\).

      The divide and conquer strategy of binary search is as follows.

      • The problem can be decomposed: Binary search recursively decomposes the original problem (searching in an array) into subproblems (searching in half of the array), achieved by comparing the middle element with the target element.
      • Subproblems are independent: In binary search, each round only processes one subproblem, which is not affected by other subproblems.
      • Solutions of subproblems do not need to be merged: Binary search aims to find a specific element, so there is no need to merge the solutions of subproblems. When a subproblem is solved, the original problem is also solved.

      Divide and conquer can improve search efficiency because brute-force search can only eliminate one option per round, while divide and conquer search can eliminate half of the options per round.

      "},{"location":"chapter_divide_and_conquer/binary_search_recur/#1-implementing-binary-search-based-on-divide-and-conquer","title":"1. \u00a0 Implementing Binary Search Based on Divide and Conquer","text":"

      In previous sections, binary search was implemented based on iteration. Now we implement it based on divide and conquer (recursion).

      Question

      Given a sorted array nums of length \\(n\\), where all elements are unique, find the element target.

      From a divide and conquer perspective, we denote the subproblem corresponding to the search interval \\([i, j]\\) as \\(f(i, j)\\).

      Starting from the original problem \\(f(0, n-1)\\), perform binary search through the following steps.

      1. Calculate the midpoint \\(m\\) of the search interval \\([i, j]\\), and use it to eliminate half of the search interval.
      2. Recursively solve the subproblem reduced by half in size, which could be \\(f(i, m-1)\\) or \\(f(m+1, j)\\).
      3. Repeat steps 1. and 2. until target is found or the interval is empty and return.

      Figure 12-4 shows the divide and conquer process of binary search for element \\(6\\) in an array.

      Figure 12-4 \u00a0 Divide and conquer process of binary search

      In the implementation code, we declare a recursive function dfs() to solve the problem \\(f(i, j)\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_recur.py
      def dfs(nums: list[int], target: int, i: int, j: int) -> int:\n    \"\"\"Binary search: problem f(i, j)\"\"\"\n    # If the interval is empty, it means there is no target element, return -1\n    if i > j:\n        return -1\n    # Calculate the midpoint index m\n    m = (i + j) // 2\n    if nums[m] < target:\n        # Recursion subproblem f(m+1, j)\n        return dfs(nums, target, m + 1, j)\n    elif nums[m] > target:\n        # Recursion subproblem f(i, m-1)\n        return dfs(nums, target, i, m - 1)\n    else:\n        # Found the target element, return its index\n        return m\n\ndef binary_search(nums: list[int], target: int) -> int:\n    \"\"\"Binary search\"\"\"\n    n = len(nums)\n    # Solve the problem f(0, n-1)\n    return dfs(nums, target, 0, n - 1)\n
      binary_search_recur.cpp
      /* Binary search: problem f(i, j) */\nint dfs(vector<int> &nums, int target, int i, int j) {\n    // If the interval is empty, it means there is no target element, return -1\n    if (i > j) {\n        return -1;\n    }\n    // Calculate the midpoint index m\n    int m = (i + j) / 2;\n    if (nums[m] < target) {\n        // Recursion subproblem f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // Recursion subproblem f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // Found the target element, return its index\n        return m;\n    }\n}\n\n/* Binary search */\nint binarySearch(vector<int> &nums, int target) {\n    int n = nums.size();\n    // Solve the problem f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
      binary_search_recur.java
      /* Binary search: problem f(i, j) */\nint dfs(int[] nums, int target, int i, int j) {\n    // If the interval is empty, it means there is no target element, return -1\n    if (i > j) {\n        return -1;\n    }\n    // Calculate the midpoint index m\n    int m = (i + j) / 2;\n    if (nums[m] < target) {\n        // Recursion subproblem f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // Recursion subproblem f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // Found the target element, return its index\n        return m;\n    }\n}\n\n/* Binary search */\nint binarySearch(int[] nums, int target) {\n    int n = nums.length;\n    // Solve the problem f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
      binary_search_recur.cs
      /* Binary search: problem f(i, j) */\nint DFS(int[] nums, int target, int i, int j) {\n    // If the interval is empty, it means there is no target element, return -1\n    if (i > j) {\n        return -1;\n    }\n    // Calculate the midpoint index m\n    int m = (i + j) / 2;\n    if (nums[m] < target) {\n        // Recursion subproblem f(m+1, j)\n        return DFS(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // Recursion subproblem f(i, m-1)\n        return DFS(nums, target, i, m - 1);\n    } else {\n        // Found the target element, return its index\n        return m;\n    }\n}\n\n/* Binary search */\nint BinarySearch(int[] nums, int target) {\n    int n = nums.Length;\n    // Solve the problem f(0, n-1)\n    return DFS(nums, target, 0, n - 1);\n}\n
      binary_search_recur.go
      /* Binary search: problem f(i, j) */\nfunc dfs(nums []int, target, i, j int) int {\n    // If interval is empty, indicating no target element, return -1\n    if i > j {\n        return -1\n    }\n    // Calculate midpoint index\n    m := i + ((j - i) >> 1)\n    // Compare midpoint with target element\n    if nums[m] < target {\n        // If smaller, recurse on right half of array\n        // Recursion subproblem f(m+1, j)\n        return dfs(nums, target, m+1, j)\n    } else if nums[m] > target {\n        // If larger, recurse on left half of array\n        // Recursion subproblem f(i, m-1)\n        return dfs(nums, target, i, m-1)\n    } else {\n        // Found the target element, return its index\n        return m\n    }\n}\n\n/* Binary search */\nfunc binarySearch(nums []int, target int) int {\n    n := len(nums)\n    return dfs(nums, target, 0, n-1)\n}\n
      binary_search_recur.swift
      /* Binary search: problem f(i, j) */\nfunc dfs(nums: [Int], target: Int, i: Int, j: Int) -> Int {\n    // If the interval is empty, it means there is no target element, return -1\n    if i > j {\n        return -1\n    }\n    // Calculate the midpoint index m\n    let m = (i + j) / 2\n    if nums[m] < target {\n        // Recursion subproblem f(m+1, j)\n        return dfs(nums: nums, target: target, i: m + 1, j: j)\n    } else if nums[m] > target {\n        // Recursion subproblem f(i, m-1)\n        return dfs(nums: nums, target: target, i: i, j: m - 1)\n    } else {\n        // Found the target element, return its index\n        return m\n    }\n}\n\n/* Binary search */\nfunc binarySearch(nums: [Int], target: Int) -> Int {\n    // Solve the problem f(0, n-1)\n    dfs(nums: nums, target: target, i: nums.startIndex, j: nums.endIndex - 1)\n}\n
      binary_search_recur.js
      /* Binary search: problem f(i, j) */\nfunction dfs(nums, target, i, j) {\n    // If the interval is empty, it means there is no target element, return -1\n    if (i > j) {\n        return -1;\n    }\n    // Calculate the midpoint index m\n    const m = i + ((j - i) >> 1);\n    if (nums[m] < target) {\n        // Recursion subproblem f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // Recursion subproblem f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // Found the target element, return its index\n        return m;\n    }\n}\n\n/* Binary search */\nfunction binarySearch(nums, target) {\n    const n = nums.length;\n    // Solve the problem f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
      binary_search_recur.ts
      /* Binary search: problem f(i, j) */\nfunction dfs(nums: number[], target: number, i: number, j: number): number {\n    // If the interval is empty, it means there is no target element, return -1\n    if (i > j) {\n        return -1;\n    }\n    // Calculate the midpoint index m\n    const m = i + ((j - i) >> 1);\n    if (nums[m] < target) {\n        // Recursion subproblem f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // Recursion subproblem f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // Found the target element, return its index\n        return m;\n    }\n}\n\n/* Binary search */\nfunction binarySearch(nums: number[], target: number): number {\n    const n = nums.length;\n    // Solve the problem f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
      binary_search_recur.dart
      /* Binary search: problem f(i, j) */\nint dfs(List<int> nums, int target, int i, int j) {\n  // If the interval is empty, it means there is no target element, return -1\n  if (i > j) {\n    return -1;\n  }\n  // Calculate the midpoint index m\n  int m = (i + j) ~/ 2;\n  if (nums[m] < target) {\n    // Recursion subproblem f(m+1, j)\n    return dfs(nums, target, m + 1, j);\n  } else if (nums[m] > target) {\n    // Recursion subproblem f(i, m-1)\n    return dfs(nums, target, i, m - 1);\n  } else {\n    // Found the target element, return its index\n    return m;\n  }\n}\n\n/* Binary search */\nint binarySearch(List<int> nums, int target) {\n  int n = nums.length;\n  // Solve the problem f(0, n-1)\n  return dfs(nums, target, 0, n - 1);\n}\n
      binary_search_recur.rs
      /* Binary search: problem f(i, j) */\nfn dfs(nums: &[i32], target: i32, i: i32, j: i32) -> i32 {\n    // If the interval is empty, it means there is no target element, return -1\n    if i > j {\n        return -1;\n    }\n    let m: i32 = i + (j - i) / 2;\n    if nums[m as usize] < target {\n        // Recursion subproblem f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if nums[m as usize] > target {\n        // Recursion subproblem f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // Found the target element, return its index\n        return m;\n    }\n}\n\n/* Binary search */\nfn binary_search(nums: &[i32], target: i32) -> i32 {\n    let n = nums.len() as i32;\n    // Solve the problem f(0, n-1)\n    dfs(nums, target, 0, n - 1)\n}\n
      binary_search_recur.c
      /* Binary search: problem f(i, j) */\nint dfs(int nums[], int target, int i, int j) {\n    // If the interval is empty, it means there is no target element, return -1\n    if (i > j) {\n        return -1;\n    }\n    // Calculate the midpoint index m\n    int m = (i + j) / 2;\n    if (nums[m] < target) {\n        // Recursion subproblem f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // Recursion subproblem f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // Found the target element, return its index\n        return m;\n    }\n}\n\n/* Binary search */\nint binarySearch(int nums[], int target, int numsSize) {\n    int n = numsSize;\n    // Solve the problem f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
      binary_search_recur.kt
      /* Binary search: problem f(i, j) */\nfun dfs(\n    nums: IntArray,\n    target: Int,\n    i: Int,\n    j: Int\n): Int {\n    // If the interval is empty, it means there is no target element, return -1\n    if (i > j) {\n        return -1\n    }\n    // Calculate the midpoint index m\n    val m = (i + j) / 2\n    return if (nums[m] < target) {\n        // Recursion subproblem f(m+1, j)\n        dfs(nums, target, m + 1, j)\n    } else if (nums[m] > target) {\n        // Recursion subproblem f(i, m-1)\n        dfs(nums, target, i, m - 1)\n    } else {\n        // Found the target element, return its index\n        m\n    }\n}\n\n/* Binary search */\nfun binarySearch(nums: IntArray, target: Int): Int {\n    val n = nums.size\n    // Solve the problem f(0, n-1)\n    return dfs(nums, target, 0, n - 1)\n}\n
      binary_search_recur.rb
      ### Binary search: problem f(i, j) ###\ndef dfs(nums, target, i, j)\n  # If the interval is empty, it means there is no target element, return -1\n  return -1 if i > j\n\n  # Calculate the midpoint index m\n  m = (i + j) / 2\n\n  if nums[m] < target\n    # Recursion subproblem f(m+1, j)\n    return dfs(nums, target, m + 1, j)\n  elsif nums[m] > target\n    # Recursion subproblem f(i, m-1)\n    return dfs(nums, target, i, m - 1)\n  else\n    # Found the target element, return its index\n    return m\n  end\nend\n\n### Binary search ###\ndef binary_search(nums, target)\n  n = nums.length\n  # Solve the problem f(0, n-1)\n  dfs(nums, target, 0, n - 1)\nend\n
      "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/","title":"12.3 \u00a0 Building a Binary Tree Problem","text":"

      Question

      Given the preorder traversal preorder and inorder traversal inorder of a binary tree, construct the binary tree and return the root node of the binary tree. Assume there are no duplicate node values in the binary tree (as shown in Figure 12-5).

      Figure 12-5 \u00a0 Example data for building a binary tree

      "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#1-determining-if-it-is-a-divide-and-conquer-problem","title":"1. \u00a0 Determining If It Is a Divide and Conquer Problem","text":"

      The original problem is defined as constructing a binary tree from preorder and inorder, which is a typical divide and conquer problem.

      • The problem can be decomposed: From a divide and conquer perspective, we can divide the original problem into two subproblems: constructing the left subtree and constructing the right subtree, plus one operation: initializing the root node. For each subtree (subproblem), we can still reuse the above division method, dividing it into smaller subtrees (subproblems) until the smallest subproblem (empty subtree) is reached.
      • Subproblems are independent: The left and right subtrees are independent of each other; there is no overlap between them. When constructing the left subtree, we only need to focus on the parts of the inorder and preorder traversals corresponding to the left subtree. The same applies to the right subtree.
      • Solutions of subproblems can be merged: Once we have the left and right subtrees (solutions of subproblems), we can link them to the root node to obtain the solution to the original problem.
      "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#2-how-to-divide-subtrees","title":"2. \u00a0 How to Divide Subtrees","text":"

      Based on the above analysis, this problem can be solved using divide and conquer, but how do we divide the left and right subtrees through the preorder traversal preorder and inorder traversal inorder?

      According to the definition, both preorder and inorder can be divided into three parts.

      • Preorder traversal: [ Root Node | Left Subtree | Right Subtree ], for example, the tree in Figure 12-5 corresponds to [ 3 | 9 | 2 1 7 ].
      • Inorder traversal: [ Left Subtree | Root Node \uff5c Right Subtree ], for example, the tree in Figure 12-5 corresponds to [ 9 | 3 | 1 2 7 ].

      Using the data from the figure above as an example, we can obtain the division results through the steps shown in Figure 12-6.

      1. The first element 3 in the preorder traversal is the value of the root node.
      2. Find the index of root node 3 in inorder, and use this index to divide inorder into [ 9 | 3 \uff5c 1 2 7 ].
      3. Based on the division result of inorder, it is easy to determine that the left and right subtrees have 1 and 3 nodes respectively, allowing us to divide preorder into [ 3 | 9 | 2 1 7 ].

      Figure 12-6 \u00a0 Dividing subtrees in preorder and inorder traversals

      "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#3-describing-subtree-intervals-based-on-variables","title":"3. \u00a0 Describing Subtree Intervals Based on Variables","text":"

      Based on the above division method, we have obtained the index intervals of the root node, left subtree, and right subtree in preorder and inorder. To describe these index intervals, we need to use several pointer variables.

      • Denote the index of the current tree's root node in preorder as \\(i\\).
      • Denote the index of the current tree's root node in inorder as \\(m\\).
      • Denote the index interval of the current tree in inorder as \\([l, r]\\).

      As shown in Table 12-1, through these variables we can represent the index of the root node in preorder and the index intervals of the subtrees in inorder.

      Table 12-1 \u00a0 Indices of root node and subtrees in preorder and inorder traversals

      Root node index in preorder Subtree index interval in inorder Current tree \\(i\\) \\([l, r]\\) Left subtree \\(i + 1\\) \\([l, m-1]\\) Right subtree \\(i + 1 + (m - l)\\) \\([m+1, r]\\)

      Please note that \\((m-l)\\) in the right subtree root node index means \"the number of nodes in the left subtree\". It is recommended to understand this in conjunction with Figure 12-7.

      Figure 12-7 \u00a0 Index interval representation of root node and left and right subtrees

      "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#4-code-implementation","title":"4. \u00a0 Code Implementation","text":"

      To improve the efficiency of querying \\(m\\), we use a hash table hmap to store the mapping from elements in the inorder array to their indices:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby build_tree.py
      def dfs(\n    preorder: list[int],\n    inorder_map: dict[int, int],\n    i: int,\n    l: int,\n    r: int,\n) -> TreeNode | None:\n    \"\"\"Build binary tree: divide and conquer\"\"\"\n    # Terminate when the subtree interval is empty\n    if r - l < 0:\n        return None\n    # Initialize the root node\n    root = TreeNode(preorder[i])\n    # Query m to divide the left and right subtrees\n    m = inorder_map[preorder[i]]\n    # Subproblem: build the left subtree\n    root.left = dfs(preorder, inorder_map, i + 1, l, m - 1)\n    # Subproblem: build the right subtree\n    root.right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r)\n    # Return the root node\n    return root\n\ndef build_tree(preorder: list[int], inorder: list[int]) -> TreeNode | None:\n    \"\"\"Build binary tree\"\"\"\n    # Initialize hash map, storing the mapping from inorder elements to indices\n    inorder_map = {val: i for i, val in enumerate(inorder)}\n    root = dfs(preorder, inorder_map, 0, 0, len(inorder) - 1)\n    return root\n
      build_tree.cpp
      /* Build binary tree: divide and conquer */\nTreeNode *dfs(vector<int> &preorder, unordered_map<int, int> &inorderMap, int i, int l, int r) {\n    // Terminate when the subtree interval is empty\n    if (r - l < 0)\n        return NULL;\n    // Initialize the root node\n    TreeNode *root = new TreeNode(preorder[i]);\n    // Query m to divide the left and right subtrees\n    int m = inorderMap[preorder[i]];\n    // Subproblem: build the left subtree\n    root->left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // Subproblem: build the right subtree\n    root->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // Return the root node\n    return root;\n}\n\n/* Build binary tree */\nTreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {\n    // Initialize hash map, storing the mapping from inorder elements to indices\n    unordered_map<int, int> inorderMap;\n    for (int i = 0; i < inorder.size(); i++) {\n        inorderMap[inorder[i]] = i;\n    }\n    TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorder.size() - 1);\n    return root;\n}\n
      build_tree.java
      /* Build binary tree: divide and conquer */\nTreeNode dfs(int[] preorder, Map<Integer, Integer> inorderMap, int i, int l, int r) {\n    // Terminate when the subtree interval is empty\n    if (r - l < 0)\n        return null;\n    // Initialize the root node\n    TreeNode root = new TreeNode(preorder[i]);\n    // Query m to divide the left and right subtrees\n    int m = inorderMap.get(preorder[i]);\n    // Subproblem: build the left subtree\n    root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // Subproblem: build the right subtree\n    root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // Return the root node\n    return root;\n}\n\n/* Build binary tree */\nTreeNode buildTree(int[] preorder, int[] inorder) {\n    // Initialize hash map, storing the mapping from inorder elements to indices\n    Map<Integer, Integer> inorderMap = new HashMap<>();\n    for (int i = 0; i < inorder.length; i++) {\n        inorderMap.put(inorder[i], i);\n    }\n    TreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n    return root;\n}\n
      build_tree.cs
      /* Build binary tree: divide and conquer */\nTreeNode? DFS(int[] preorder, Dictionary<int, int> inorderMap, int i, int l, int r) {\n    // Terminate when the subtree interval is empty\n    if (r - l < 0)\n        return null;\n    // Initialize the root node\n    TreeNode root = new(preorder[i]);\n    // Query m to divide the left and right subtrees\n    int m = inorderMap[preorder[i]];\n    // Subproblem: build the left subtree\n    root.left = DFS(preorder, inorderMap, i + 1, l, m - 1);\n    // Subproblem: build the right subtree\n    root.right = DFS(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // Return the root node\n    return root;\n}\n\n/* Build binary tree */\nTreeNode? BuildTree(int[] preorder, int[] inorder) {\n    // Initialize hash map, storing the mapping from inorder elements to indices\n    Dictionary<int, int> inorderMap = [];\n    for (int i = 0; i < inorder.Length; i++) {\n        inorderMap.TryAdd(inorder[i], i);\n    }\n    TreeNode? root = DFS(preorder, inorderMap, 0, 0, inorder.Length - 1);\n    return root;\n}\n
      build_tree.go
      /* Build binary tree: divide and conquer */\nfunc dfsBuildTree(preorder []int, inorderMap map[int]int, i, l, r int) *TreeNode {\n    // Terminate when the subtree interval is empty\n    if r-l < 0 {\n        return nil\n    }\n    // Initialize the root node\n    root := NewTreeNode(preorder[i])\n    // Query m to divide the left and right subtrees\n    m := inorderMap[preorder[i]]\n    // Subproblem: build the left subtree\n    root.Left = dfsBuildTree(preorder, inorderMap, i+1, l, m-1)\n    // Subproblem: build the right subtree\n    root.Right = dfsBuildTree(preorder, inorderMap, i+1+m-l, m+1, r)\n    // Return the root node\n    return root\n}\n\n/* Build binary tree */\nfunc buildTree(preorder, inorder []int) *TreeNode {\n    // Initialize hash map, storing the mapping from inorder elements to indices\n    inorderMap := make(map[int]int, len(inorder))\n    for i := 0; i < len(inorder); i++ {\n        inorderMap[inorder[i]] = i\n    }\n\n    root := dfsBuildTree(preorder, inorderMap, 0, 0, len(inorder)-1)\n    return root\n}\n
      build_tree.swift
      /* Build binary tree: divide and conquer */\nfunc dfs(preorder: [Int], inorderMap: [Int: Int], i: Int, l: Int, r: Int) -> TreeNode? {\n    // Terminate when the subtree interval is empty\n    if r - l < 0 {\n        return nil\n    }\n    // Initialize the root node\n    let root = TreeNode(x: preorder[i])\n    // Query m to divide the left and right subtrees\n    let m = inorderMap[preorder[i]]!\n    // Subproblem: build the left subtree\n    root.left = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1, l: l, r: m - 1)\n    // Subproblem: build the right subtree\n    root.right = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1 + m - l, l: m + 1, r: r)\n    // Return the root node\n    return root\n}\n\n/* Build binary tree */\nfunc buildTree(preorder: [Int], inorder: [Int]) -> TreeNode? {\n    // Initialize hash map, storing the mapping from inorder elements to indices\n    let inorderMap = inorder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }\n    return dfs(preorder: preorder, inorderMap: inorderMap, i: inorder.startIndex, l: inorder.startIndex, r: inorder.endIndex - 1)\n}\n
      build_tree.js
      /* Build binary tree: divide and conquer */\nfunction dfs(preorder, inorderMap, i, l, r) {\n    // Terminate when the subtree interval is empty\n    if (r - l < 0) return null;\n    // Initialize the root node\n    const root = new TreeNode(preorder[i]);\n    // Query m to divide the left and right subtrees\n    const m = inorderMap.get(preorder[i]);\n    // Subproblem: build the left subtree\n    root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // Subproblem: build the right subtree\n    root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // Return the root node\n    return root;\n}\n\n/* Build binary tree */\nfunction buildTree(preorder, inorder) {\n    // Initialize hash map, storing the mapping from inorder elements to indices\n    let inorderMap = new Map();\n    for (let i = 0; i < inorder.length; i++) {\n        inorderMap.set(inorder[i], i);\n    }\n    const root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n    return root;\n}\n
      build_tree.ts
      /* Build binary tree: divide and conquer */\nfunction dfs(\n    preorder: number[],\n    inorderMap: Map<number, number>,\n    i: number,\n    l: number,\n    r: number\n): TreeNode | null {\n    // Terminate when the subtree interval is empty\n    if (r - l < 0) return null;\n    // Initialize the root node\n    const root: TreeNode = new TreeNode(preorder[i]);\n    // Query m to divide the left and right subtrees\n    const m = inorderMap.get(preorder[i]);\n    // Subproblem: build the left subtree\n    root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // Subproblem: build the right subtree\n    root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // Return the root node\n    return root;\n}\n\n/* Build binary tree */\nfunction buildTree(preorder: number[], inorder: number[]): TreeNode | null {\n    // Initialize hash map, storing the mapping from inorder elements to indices\n    let inorderMap = new Map<number, number>();\n    for (let i = 0; i < inorder.length; i++) {\n        inorderMap.set(inorder[i], i);\n    }\n    const root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n    return root;\n}\n
      build_tree.dart
      /* Build binary tree: divide and conquer */\nTreeNode? dfs(\n  List<int> preorder,\n  Map<int, int> inorderMap,\n  int i,\n  int l,\n  int r,\n) {\n  // Terminate when the subtree interval is empty\n  if (r - l < 0) {\n    return null;\n  }\n  // Initialize the root node\n  TreeNode? root = TreeNode(preorder[i]);\n  // Query m to divide the left and right subtrees\n  int m = inorderMap[preorder[i]]!;\n  // Subproblem: build the left subtree\n  root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n  // Subproblem: build the right subtree\n  root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n  // Return the root node\n  return root;\n}\n\n/* Build binary tree */\nTreeNode? buildTree(List<int> preorder, List<int> inorder) {\n  // Initialize hash map, storing the mapping from inorder elements to indices\n  Map<int, int> inorderMap = {};\n  for (int i = 0; i < inorder.length; i++) {\n    inorderMap[inorder[i]] = i;\n  }\n  TreeNode? root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n  return root;\n}\n
      build_tree.rs
      /* Build binary tree: divide and conquer */\nfn dfs(\n    preorder: &[i32],\n    inorder_map: &HashMap<i32, i32>,\n    i: i32,\n    l: i32,\n    r: i32,\n) -> Option<Rc<RefCell<TreeNode>>> {\n    // Terminate when the subtree interval is empty\n    if r - l < 0 {\n        return None;\n    }\n    // Initialize the root node\n    let root = TreeNode::new(preorder[i as usize]);\n    // Query m to divide the left and right subtrees\n    let m = inorder_map.get(&preorder[i as usize]).unwrap();\n    // Subproblem: build the left subtree\n    root.borrow_mut().left = dfs(preorder, inorder_map, i + 1, l, m - 1);\n    // Subproblem: build the right subtree\n    root.borrow_mut().right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r);\n    // Return the root node\n    Some(root)\n}\n\n/* Build binary tree */\nfn build_tree(preorder: &[i32], inorder: &[i32]) -> Option<Rc<RefCell<TreeNode>>> {\n    // Initialize hash map, storing the mapping from inorder elements to indices\n    let mut inorder_map: HashMap<i32, i32> = HashMap::new();\n    for i in 0..inorder.len() {\n        inorder_map.insert(inorder[i], i as i32);\n    }\n    let root = dfs(preorder, &inorder_map, 0, 0, inorder.len() as i32 - 1);\n    root\n}\n
      build_tree.c
      /* Build binary tree: divide and conquer */\nTreeNode *dfs(int *preorder, int *inorderMap, int i, int l, int r, int size) {\n    // Terminate when the subtree interval is empty\n    if (r - l < 0)\n        return NULL;\n    // Initialize the root node\n    TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));\n    root->val = preorder[i];\n    root->left = NULL;\n    root->right = NULL;\n    // Query m to divide the left and right subtrees\n    int m = inorderMap[preorder[i]];\n    // Subproblem: build the left subtree\n    root->left = dfs(preorder, inorderMap, i + 1, l, m - 1, size);\n    // Subproblem: build the right subtree\n    root->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r, size);\n    // Return the root node\n    return root;\n}\n\n/* Build binary tree */\nTreeNode *buildTree(int *preorder, int preorderSize, int *inorder, int inorderSize) {\n    // Initialize hash map, storing the mapping from inorder elements to indices\n    int *inorderMap = (int *)malloc(sizeof(int) * MAX_SIZE);\n    for (int i = 0; i < inorderSize; i++) {\n        inorderMap[inorder[i]] = i;\n    }\n    TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorderSize - 1, inorderSize);\n    free(inorderMap);\n    return root;\n}\n
      build_tree.kt
      /* Build binary tree: divide and conquer */\nfun dfs(\n    preorder: IntArray,\n    inorderMap: Map<Int?, Int?>,\n    i: Int,\n    l: Int,\n    r: Int\n): TreeNode? {\n    // Terminate when the subtree interval is empty\n    if (r - l < 0) return null\n    // Initialize the root node\n    val root = TreeNode(preorder[i])\n    // Query m to divide the left and right subtrees\n    val m = inorderMap[preorder[i]]!!\n    // Subproblem: build the left subtree\n    root.left = dfs(preorder, inorderMap, i + 1, l, m - 1)\n    // Subproblem: build the right subtree\n    root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r)\n    // Return the root node\n    return root\n}\n\n/* Build binary tree */\nfun buildTree(preorder: IntArray, inorder: IntArray): TreeNode? {\n    // Initialize hash map, storing the mapping from inorder elements to indices\n    val inorderMap = HashMap<Int?, Int?>()\n    for (i in inorder.indices) {\n        inorderMap[inorder[i]] = i\n    }\n    val root = dfs(preorder, inorderMap, 0, 0, inorder.size - 1)\n    return root\n}\n
      build_tree.rb
      ### Build binary tree: divide and conquer ###\ndef dfs(preorder, inorder_map, i, l, r)\n  # Terminate when the subtree interval is empty\n  return if r - l < 0\n\n  # Initialize the root node\n  root = TreeNode.new(preorder[i])\n  # Query m to divide the left and right subtrees\n  m = inorder_map[preorder[i]]\n  # Subproblem: build the left subtree\n  root.left = dfs(preorder, inorder_map, i + 1, l, m - 1)\n  # Subproblem: build the right subtree\n  root.right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r)\n\n  # Return the root node\n  root\nend\n\n### Build binary tree ###\ndef build_tree(preorder, inorder)\n  # Initialize hash map, storing the mapping from inorder elements to indices\n  inorder_map = {}\n  inorder.each_with_index { |val, i| inorder_map[val] = i }\n  dfs(preorder, inorder_map, 0, 0, inorder.length - 1)\nend\n

      Figure 12-8 shows the recursive process of building the binary tree. Each node is established during the downward \"recursion\" process, while each edge (reference) is established during the upward \"return\" process.

      <1><2><3><4><5><6><7><8><9>

      Figure 12-8 \u00a0 Recursive process of building a binary tree

      The division results of the preorder traversal preorder and inorder traversal inorder within each recursive function are shown in Figure 12-9.

      Figure 12-9 \u00a0 Division results in each recursive function

      Let the number of nodes in the tree be \\(n\\). Initializing each node (executing one recursive function dfs()) takes \\(O(1)\\) time. Therefore, the overall time complexity is \\(O(n)\\).

      The hash table stores the mapping from inorder elements to their indices, with a space complexity of \\(O(n)\\). In the worst case, when the binary tree degenerates into a linked list, the recursion depth reaches \\(n\\), using \\(O(n)\\) stack frame space. Therefore, the overall space complexity is \\(O(n)\\).

      "},{"location":"chapter_divide_and_conquer/divide_and_conquer/","title":"12.1 \u00a0 Divide and Conquer Algorithms","text":"

      Divide and conquer is a very important and common algorithm strategy. Divide and conquer is typically implemented based on recursion, consisting of two steps: \"divide\" and \"conquer\".

      1. Divide (partition phase): Recursively divide the original problem into two or more subproblems until the smallest subproblem is reached.
      2. Conquer (merge phase): Starting from the smallest subproblems with known solutions, merge the solutions of subproblems from bottom to top to construct the solution to the original problem.

      As shown in Figure 12-1, \"merge sort\" is one of the typical applications of the divide and conquer strategy.

      1. Divide: Recursively divide the original array (original problem) into two subarrays (subproblems) until the subarray has only one element (smallest subproblem).
      2. Conquer: Merge the sorted subarrays (solutions to subproblems) from bottom to top to obtain a sorted original array (solution to the original problem).

      Figure 12-1 \u00a0 Divide and conquer strategy of merge sort

      "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1211-how-to-determine-divide-and-conquer-problems","title":"12.1.1 \u00a0 How to Determine Divide and Conquer Problems","text":"

      Whether a problem is suitable for solving with divide and conquer can usually be determined based on the following criteria.

      1. The problem can be decomposed: The original problem can be divided into smaller, similar subproblems, and can be recursively divided in the same way.
      2. Subproblems are independent: There is no overlap between subproblems, they are independent of each other and can be solved independently.
      3. Solutions of subproblems can be merged: The solution to the original problem is obtained by merging the solutions of subproblems.

      Clearly, merge sort satisfies these three criteria.

      1. The problem can be decomposed: Recursively divide the array (original problem) into two subarrays (subproblems).
      2. Subproblems are independent: Each subarray can be sorted independently (subproblems can be solved independently).
      3. Solutions of subproblems can be merged: Two sorted subarrays (solutions of subproblems) can be merged into one sorted array (solution of the original problem).
      "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1212-improving-efficiency-through-divide-and-conquer","title":"12.1.2 \u00a0 Improving Efficiency Through Divide and Conquer","text":"

      Divide and conquer can not only effectively solve algorithmic problems but often also improve algorithm efficiency. In sorting algorithms, quick sort, merge sort, and heap sort are faster than selection, bubble, and insertion sort because they apply the divide and conquer strategy.

      This raises the question: Why can divide and conquer improve algorithm efficiency, and what is the underlying logic? In other words, why is dividing a large problem into multiple subproblems, solving the subproblems, and merging their solutions more efficient than directly solving the original problem? This question can be discussed from two aspects: operation count and parallel computation.

      "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1-operation-count-optimization","title":"1. \u00a0 Operation Count Optimization","text":"

      Taking \"bubble sort\" as an example, processing an array of length \\(n\\) requires \\(O(n^2)\\) time. Suppose we divide the array into two subarrays from the midpoint as shown in Figure 12-2, the division requires \\(O(n)\\) time, sorting each subarray requires \\(O((n / 2)^2)\\) time, and merging the two subarrays requires \\(O(n)\\) time, resulting in an overall time complexity of:

      \\[ O(n + (\\frac{n}{2})^2 \\times 2 + n) = O(\\frac{n^2}{2} + 2n) \\]

      Figure 12-2 \u00a0 Bubble sort before and after array division

      Next, we compute the following inequality, where the left and right sides represent the total number of operations before and after division, respectively:

      \\[ \\begin{aligned} n^2 & > \\frac{n^2}{2} + 2n \\newline n^2 - \\frac{n^2}{2} - 2n & > 0 \\newline n(n - 4) & > 0 \\end{aligned} \\]

      This means that when \\(n > 4\\), the number of operations after division is smaller, and sorting efficiency should be higher. Note that the time complexity after division is still quadratic \\(O(n^2)\\), but the constant term in the complexity has become smaller.

      Going further, what if we continuously divide the subarrays from their midpoints into two subarrays until the subarrays have only one element? This approach is actually \"merge sort\", with a time complexity of \\(O(n \\log n)\\).

      Thinking further, what if we set multiple division points and evenly divide the original array into \\(k\\) subarrays? This situation is very similar to \"bucket sort\", which is well-suited for sorting massive amounts of data, with a theoretical time complexity of \\(O(n + k)\\).

      "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#2-parallel-computation-optimization","title":"2. \u00a0 Parallel Computation Optimization","text":"

      We know that the subproblems generated by divide and conquer are independent of each other, so they can typically be solved in parallel. This means divide and conquer can not only reduce the time complexity of algorithms, but also benefits from parallel optimization by operating systems.

      Parallel optimization is particularly effective in multi-core or multi-processor environments, as the system can simultaneously handle multiple subproblems, making fuller use of computing resources and significantly reducing overall runtime.

      For example, in the \"bucket sort\" shown in Figure 12-3, we evenly distribute massive data into various buckets, and the sorting tasks for all buckets can be distributed to various computing units. After completion, the results are merged.

      Figure 12-3 \u00a0 Parallel computation in bucket sort

      "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1213-common-applications-of-divide-and-conquer","title":"12.1.3 \u00a0 Common Applications of Divide and Conquer","text":"

      On one hand, divide and conquer can be used to solve many classic algorithmic problems.

      • Finding the closest pair of points: This algorithm first divides the point set into two parts, then finds the closest pair of points in each part separately, and finally finds the closest pair of points that spans both parts.
      • Large integer multiplication: For example, the Karatsuba algorithm, which decomposes large integer multiplication into several smaller integer multiplications and additions.
      • Matrix multiplication: For example, the Strassen algorithm, which decomposes large matrix multiplication into multiple small matrix multiplications and additions.
      • Hanota problem: The hanota problem can be solved through recursion, which is a typical application of the divide and conquer strategy.
      • Solving inversion pairs: In a sequence, if a preceding number is greater than a following number, these two numbers form an inversion pair. Solving the inversion pair problem can utilize the divide and conquer approach with the help of merge sort.

      On the other hand, divide and conquer is widely applied in the design of algorithms and data structures.

      • Binary search: Binary search divides a sorted array into two parts from the midpoint index, then decides which half to eliminate based on the comparison result between the target value and the middle element value, and performs the same binary operation on the remaining interval.
      • Merge sort: Already introduced at the beginning of this section, no further elaboration needed.
      • Quick sort: Quick sort selects a pivot value, then divides the array into two subarrays, one with elements smaller than the pivot and the other with elements larger than the pivot, then performs the same division operation on these two parts until the subarrays have only one element.
      • Bucket sort: The basic idea of bucket sort is to scatter data into multiple buckets, then sort the elements within each bucket, and finally extract the elements from each bucket in sequence to obtain a sorted array.
      • Trees: For example, binary search trees, AVL trees, red-black trees, B-trees, B+ trees, etc. Their search, insertion, and deletion operations can all be viewed as applications of the divide and conquer strategy.
      • Heaps: A heap is a special complete binary tree, and its various operations, such as insertion, deletion, and heapify, actually imply the divide and conquer idea.
      • Hash tables: Although hash tables do not directly apply divide and conquer, some hash collision resolution solutions indirectly apply the divide and conquer strategy. For example, long linked lists in chaining may be converted to red-black trees to improve query efficiency.

      It can be seen that divide and conquer is a \"subtly pervasive\" algorithmic idea, embedded in various algorithms and data structures.

      "},{"location":"chapter_divide_and_conquer/hanota_problem/","title":"12.4 \u00a0 Hanota Problem","text":"

      In merge sort and building binary trees, we decompose the original problem into two subproblems, each half the size of the original problem. However, for the hanota problem, we adopt a different decomposition strategy.

      Question

      Given three pillars, denoted as A, B, and C. Initially, pillar A has \\(n\\) discs stacked on it, arranged from top to bottom in ascending order of size. Our task is to move these \\(n\\) discs to pillar C while maintaining their original order (as shown in Figure 12-10). The following rules must be followed when moving the discs.

      1. A disc can only be taken from the top of one pillar and placed on top of another pillar.
      2. Only one disc can be moved at a time.
      3. A smaller disc must always be on top of a larger disc.

      Figure 12-10 \u00a0 Example of the hanota problem

      We denote the hanota problem of size \\(i\\) as \\(f(i)\\). For example, \\(f(3)\\) represents moving \\(3\\) discs from A to C.

      "},{"location":"chapter_divide_and_conquer/hanota_problem/#1-considering-the-base-cases","title":"1. \u00a0 Considering the Base Cases","text":"

      As shown in Figure 12-11, for problem \\(f(1)\\), when there is only one disc, we can move it directly from A to C.

      <1><2>

      Figure 12-11 \u00a0 Solution for a problem of size 1

      As shown in Figure 12-12, for problem \\(f(2)\\), when there are two discs, since we must always keep the smaller disc on top of the larger disc, we need to use B to assist in the move.

      1. First, move the smaller disc from A to B.
      2. Then move the larger disc from A to C.
      3. Finally, move the smaller disc from B to C.
      <1><2><3><4>

      Figure 12-12 \u00a0 Solution for a problem of size 2

      The process of solving problem \\(f(2)\\) can be summarized as: moving two discs from A to C with the help of B. Here, C is called the target pillar, and B is called the buffer pillar.

      "},{"location":"chapter_divide_and_conquer/hanota_problem/#2-subproblem-decomposition","title":"2. \u00a0 Subproblem Decomposition","text":"

      For problem \\(f(3)\\), when there are three discs, the situation becomes slightly more complex.

      Since we already know the solutions to \\(f(1)\\) and \\(f(2)\\), we can think from a divide and conquer perspective, treating the top two discs on A as a whole, and execute the steps shown in Figure 12-13. This successfully moves the three discs from A to C.

      1. Let B be the target pillar and C be the buffer pillar, and move two discs from A to B.
      2. Move the remaining disc from A directly to C.
      3. Let C be the target pillar and A be the buffer pillar, and move two discs from B to C.
      <1><2><3><4>

      Figure 12-13 \u00a0 Solution for a problem of size 3

      Essentially, we divide problem \\(f(3)\\) into two subproblems \\(f(2)\\) and one subproblem \\(f(1)\\). By solving these three subproblems in order, the original problem is solved. This shows that the subproblems are independent and their solutions can be merged.

      From this, we can summarize the divide and conquer strategy for solving the hanota problem shown in Figure 12-14: divide the original problem \\(f(n)\\) into two subproblems \\(f(n-1)\\) and one subproblem \\(f(1)\\), and solve these three subproblems in the following order.

      1. Move \\(n-1\\) discs from A to B with the help of C.
      2. Move the remaining \\(1\\) disc directly from A to C.
      3. Move \\(n-1\\) discs from B to C with the help of A.

      For these two subproblems \\(f(n-1)\\), we can recursively divide them in the same way until reaching the smallest subproblem \\(f(1)\\). The solution to \\(f(1)\\) is known and requires only one move operation.

      Figure 12-14 \u00a0 Divide and conquer strategy for solving the hanota problem

      "},{"location":"chapter_divide_and_conquer/hanota_problem/#3-code-implementation","title":"3. \u00a0 Code Implementation","text":"

      In the code, we declare a recursive function dfs(i, src, buf, tar), whose purpose is to move the top \\(i\\) discs from pillar src to target pillar tar with the help of buffer pillar buf:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hanota.py
      def move(src: list[int], tar: list[int]):\n    \"\"\"Move a disk\"\"\"\n    # Take out a disk from the top of src\n    pan = src.pop()\n    # Place the disk on top of tar\n    tar.append(pan)\n\ndef dfs(i: int, src: list[int], buf: list[int], tar: list[int]):\n    \"\"\"Solve the Tower of Hanoi problem f(i)\"\"\"\n    # If there is only one disk left in src, move it directly to tar\n    if i == 1:\n        move(src, tar)\n        return\n    # Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n    dfs(i - 1, src, tar, buf)\n    # Subproblem f(1): move the remaining disk from src to tar\n    move(src, tar)\n    # Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n    dfs(i - 1, buf, src, tar)\n\ndef solve_hanota(A: list[int], B: list[int], C: list[int]):\n    \"\"\"Solve the Tower of Hanoi problem\"\"\"\n    n = len(A)\n    # Move the top n disks from A to C using B\n    dfs(n, A, B, C)\n
      hanota.cpp
      /* Move a disk */\nvoid move(vector<int> &src, vector<int> &tar) {\n    // Take out a disk from the top of src\n    int pan = src.back();\n    src.pop_back();\n    // Place the disk on top of tar\n    tar.push_back(pan);\n}\n\n/* Solve the Tower of Hanoi problem f(i) */\nvoid dfs(int i, vector<int> &src, vector<int> &buf, vector<int> &tar) {\n    // If there is only one disk left in src, move it directly to tar\n    if (i == 1) {\n        move(src, tar);\n        return;\n    }\n    // Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n    dfs(i - 1, src, tar, buf);\n    // Subproblem f(1): move the remaining disk from src to tar\n    move(src, tar);\n    // Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n    dfs(i - 1, buf, src, tar);\n}\n\n/* Solve the Tower of Hanoi problem */\nvoid solveHanota(vector<int> &A, vector<int> &B, vector<int> &C) {\n    int n = A.size();\n    // Move the top n disks from A to C using B\n    dfs(n, A, B, C);\n}\n
      hanota.java
      /* Move a disk */\nvoid move(List<Integer> src, List<Integer> tar) {\n    // Take out a disk from the top of src\n    Integer pan = src.remove(src.size() - 1);\n    // Place the disk on top of tar\n    tar.add(pan);\n}\n\n/* Solve the Tower of Hanoi problem f(i) */\nvoid dfs(int i, List<Integer> src, List<Integer> buf, List<Integer> tar) {\n    // If there is only one disk left in src, move it directly to tar\n    if (i == 1) {\n        move(src, tar);\n        return;\n    }\n    // Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n    dfs(i - 1, src, tar, buf);\n    // Subproblem f(1): move the remaining disk from src to tar\n    move(src, tar);\n    // Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n    dfs(i - 1, buf, src, tar);\n}\n\n/* Solve the Tower of Hanoi problem */\nvoid solveHanota(List<Integer> A, List<Integer> B, List<Integer> C) {\n    int n = A.size();\n    // Move the top n disks from A to C using B\n    dfs(n, A, B, C);\n}\n
      hanota.cs
      /* Move a disk */\nvoid Move(List<int> src, List<int> tar) {\n    // Take out a disk from the top of src\n    int pan = src[^1];\n    src.RemoveAt(src.Count - 1);\n    // Place the disk on top of tar\n    tar.Add(pan);\n}\n\n/* Solve the Tower of Hanoi problem f(i) */\nvoid DFS(int i, List<int> src, List<int> buf, List<int> tar) {\n    // If there is only one disk left in src, move it directly to tar\n    if (i == 1) {\n        Move(src, tar);\n        return;\n    }\n    // Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n    DFS(i - 1, src, tar, buf);\n    // Subproblem f(1): move the remaining disk from src to tar\n    Move(src, tar);\n    // Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n    DFS(i - 1, buf, src, tar);\n}\n\n/* Solve the Tower of Hanoi problem */\nvoid SolveHanota(List<int> A, List<int> B, List<int> C) {\n    int n = A.Count;\n    // Move the top n disks from A to C using B\n    DFS(n, A, B, C);\n}\n
      hanota.go
      /* Move a disk */\nfunc move(src, tar *list.List) {\n    // Take out a disk from the top of src\n    pan := src.Back()\n    // Place the disk on top of tar\n    tar.PushBack(pan.Value)\n    // Remove top disk from src\n    src.Remove(pan)\n}\n\n/* Solve the Tower of Hanoi problem f(i) */\nfunc dfsHanota(i int, src, buf, tar *list.List) {\n    // If there is only one disk left in src, move it directly to tar\n    if i == 1 {\n        move(src, tar)\n        return\n    }\n    // Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n    dfsHanota(i-1, src, tar, buf)\n    // Subproblem f(1): move the remaining disk from src to tar\n    move(src, tar)\n    // Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n    dfsHanota(i-1, buf, src, tar)\n}\n\n/* Solve the Tower of Hanoi problem */\nfunc solveHanota(A, B, C *list.List) {\n    n := A.Len()\n    // Move the top n disks from A to C using B\n    dfsHanota(n, A, B, C)\n}\n
      hanota.swift
      /* Move a disk */\nfunc move(src: inout [Int], tar: inout [Int]) {\n    // Take out a disk from the top of src\n    let pan = src.popLast()!\n    // Place the disk on top of tar\n    tar.append(pan)\n}\n\n/* Solve the Tower of Hanoi problem f(i) */\nfunc dfs(i: Int, src: inout [Int], buf: inout [Int], tar: inout [Int]) {\n    // If there is only one disk left in src, move it directly to tar\n    if i == 1 {\n        move(src: &src, tar: &tar)\n        return\n    }\n    // Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n    dfs(i: i - 1, src: &src, buf: &tar, tar: &buf)\n    // Subproblem f(1): move the remaining disk from src to tar\n    move(src: &src, tar: &tar)\n    // Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n    dfs(i: i - 1, src: &buf, buf: &src, tar: &tar)\n}\n\n/* Solve the Tower of Hanoi problem */\nfunc solveHanota(A: inout [Int], B: inout [Int], C: inout [Int]) {\n    let n = A.count\n    // The tail of the list is the top of the rod\n    // Move top n disks from src to C using B\n    dfs(i: n, src: &A, buf: &B, tar: &C)\n}\n
      hanota.js
      /* Move a disk */\nfunction move(src, tar) {\n    // Take out a disk from the top of src\n    const pan = src.pop();\n    // Place the disk on top of tar\n    tar.push(pan);\n}\n\n/* Solve the Tower of Hanoi problem f(i) */\nfunction dfs(i, src, buf, tar) {\n    // If there is only one disk left in src, move it directly to tar\n    if (i === 1) {\n        move(src, tar);\n        return;\n    }\n    // Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n    dfs(i - 1, src, tar, buf);\n    // Subproblem f(1): move the remaining disk from src to tar\n    move(src, tar);\n    // Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n    dfs(i - 1, buf, src, tar);\n}\n\n/* Solve the Tower of Hanoi problem */\nfunction solveHanota(A, B, C) {\n    const n = A.length;\n    // Move the top n disks from A to C using B\n    dfs(n, A, B, C);\n}\n
      hanota.ts
      /* Move a disk */\nfunction move(src: number[], tar: number[]): void {\n    // Take out a disk from the top of src\n    const pan = src.pop();\n    // Place the disk on top of tar\n    tar.push(pan);\n}\n\n/* Solve the Tower of Hanoi problem f(i) */\nfunction dfs(i: number, src: number[], buf: number[], tar: number[]): void {\n    // If there is only one disk left in src, move it directly to tar\n    if (i === 1) {\n        move(src, tar);\n        return;\n    }\n    // Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n    dfs(i - 1, src, tar, buf);\n    // Subproblem f(1): move the remaining disk from src to tar\n    move(src, tar);\n    // Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n    dfs(i - 1, buf, src, tar);\n}\n\n/* Solve the Tower of Hanoi problem */\nfunction solveHanota(A: number[], B: number[], C: number[]): void {\n    const n = A.length;\n    // Move the top n disks from A to C using B\n    dfs(n, A, B, C);\n}\n
      hanota.dart
      /* Move a disk */\nvoid move(List<int> src, List<int> tar) {\n  // Take out a disk from the top of src\n  int pan = src.removeLast();\n  // Place the disk on top of tar\n  tar.add(pan);\n}\n\n/* Solve the Tower of Hanoi problem f(i) */\nvoid dfs(int i, List<int> src, List<int> buf, List<int> tar) {\n  // If there is only one disk left in src, move it directly to tar\n  if (i == 1) {\n    move(src, tar);\n    return;\n  }\n  // Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n  dfs(i - 1, src, tar, buf);\n  // Subproblem f(1): move the remaining disk from src to tar\n  move(src, tar);\n  // Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n  dfs(i - 1, buf, src, tar);\n}\n\n/* Solve the Tower of Hanoi problem */\nvoid solveHanota(List<int> A, List<int> B, List<int> C) {\n  int n = A.length;\n  // Move the top n disks from A to C using B\n  dfs(n, A, B, C);\n}\n
      hanota.rs
      /* Move a disk */\nfn move_pan(src: &mut Vec<i32>, tar: &mut Vec<i32>) {\n    // Take out a disk from the top of src\n    let pan = src.pop().unwrap();\n    // Place the disk on top of tar\n    tar.push(pan);\n}\n\n/* Solve the Tower of Hanoi problem f(i) */\nfn dfs(i: i32, src: &mut Vec<i32>, buf: &mut Vec<i32>, tar: &mut Vec<i32>) {\n    // If there is only one disk left in src, move it directly to tar\n    if i == 1 {\n        move_pan(src, tar);\n        return;\n    }\n    // Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n    dfs(i - 1, src, tar, buf);\n    // Subproblem f(1): move the remaining disk from src to tar\n    move_pan(src, tar);\n    // Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n    dfs(i - 1, buf, src, tar);\n}\n\n/* Solve the Tower of Hanoi problem */\nfn solve_hanota(A: &mut Vec<i32>, B: &mut Vec<i32>, C: &mut Vec<i32>) {\n    let n = A.len() as i32;\n    // Move the top n disks from A to C using B\n    dfs(n, A, B, C);\n}\n
      hanota.c
      /* Move a disk */\nvoid move(int *src, int *srcSize, int *tar, int *tarSize) {\n    // Take out a disk from the top of src\n    int pan = src[*srcSize - 1];\n    src[*srcSize - 1] = 0;\n    (*srcSize)--;\n    // Place the disk on top of tar\n    tar[*tarSize] = pan;\n    (*tarSize)++;\n}\n\n/* Solve the Tower of Hanoi problem f(i) */\nvoid dfs(int i, int *src, int *srcSize, int *buf, int *bufSize, int *tar, int *tarSize) {\n    // If there is only one disk left in src, move it directly to tar\n    if (i == 1) {\n        move(src, srcSize, tar, tarSize);\n        return;\n    }\n    // Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n    dfs(i - 1, src, srcSize, tar, tarSize, buf, bufSize);\n    // Subproblem f(1): move the remaining disk from src to tar\n    move(src, srcSize, tar, tarSize);\n    // Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n    dfs(i - 1, buf, bufSize, src, srcSize, tar, tarSize);\n}\n\n/* Solve the Tower of Hanoi problem */\nvoid solveHanota(int *A, int *ASize, int *B, int *BSize, int *C, int *CSize) {\n    // Move the top n disks from A to C using B\n    dfs(*ASize, A, ASize, B, BSize, C, CSize);\n}\n
      hanota.kt
      /* Move a disk */\nfun move(src: MutableList<Int>, tar: MutableList<Int>) {\n    // Take out a disk from the top of src\n    val pan = src.removeAt(src.size - 1)\n    // Place the disk on top of tar\n    tar.add(pan)\n}\n\n/* Solve the Tower of Hanoi problem f(i) */\nfun dfs(i: Int, src: MutableList<Int>, buf: MutableList<Int>, tar: MutableList<Int>) {\n    // If there is only one disk left in src, move it directly to tar\n    if (i == 1) {\n        move(src, tar)\n        return\n    }\n    // Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n    dfs(i - 1, src, tar, buf)\n    // Subproblem f(1): move the remaining disk from src to tar\n    move(src, tar)\n    // Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n    dfs(i - 1, buf, src, tar)\n}\n\n/* Solve the Tower of Hanoi problem */\nfun solveHanota(A: MutableList<Int>, B: MutableList<Int>, C: MutableList<Int>) {\n    val n = A.size\n    // Move the top n disks from A to C using B\n    dfs(n, A, B, C)\n}\n
      hanota.rb
      ### Move one disk ###\ndef move(src, tar)\n  # Take out a disk from the top of src\n  pan = src.pop\n  # Place the disk on top of tar\n  tar << pan\nend\n\n### Solve Tower of Hanoi f(i) ###\ndef dfs(i, src, buf, tar)\n  # If there is only one disk left in src, move it directly to tar\n  if i == 1\n    move(src, tar)\n    return\n  end\n\n  # Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n  dfs(i - 1, src, tar, buf)\n  # Subproblem f(1): move the remaining disk from src to tar\n  move(src, tar)\n  # Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n  dfs(i - 1, buf, src, tar)\nend\n\n### Solve Tower of Hanoi ###\ndef solve_hanota(_A, _B, _C)\n  n = _A.length\n  # Move the top n disks from A to C using B\n  dfs(n, _A, _B, _C)\nend\n

      As shown in Figure 12-15, the hanota problem forms a recursion tree of height \\(n\\), where each node represents a subproblem corresponding to an invocation of the dfs() function, therefore the time complexity is \\(O(2^n)\\) and the space complexity is \\(O(n)\\).

      Figure 12-15 \u00a0 Recursion tree of the hanota problem

      Quote

      The hanota problem originates from an ancient legend. In a temple in ancient India, monks had three tall diamond pillars and \\(64\\) golden discs of different sizes. The monks continuously moved the discs, believing that when the last disc was correctly placed, the world would come to an end.

      However, even if the monks moved one disc per second, it would take approximately \\(2^{64} \\approx 1.84\u00d710^{19}\\) seconds, which is about \\(5850\\) billion years, far exceeding current estimates of the age of the universe. Therefore, if this legend is true, we should not need to worry about the end of the world.

      "},{"location":"chapter_divide_and_conquer/summary/","title":"12.5 \u00a0 Summary","text":""},{"location":"chapter_divide_and_conquer/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • Divide and conquer is a common algorithm design strategy, consisting of two phases: divide (partition) and conquer (merge), typically implemented based on recursion.
      • The criteria for determining whether a problem is a divide and conquer problem include: whether the problem can be decomposed, whether subproblems are independent, and whether subproblems can be merged.
      • Merge sort is a typical application of the divide and conquer strategy. It recursively divides an array into two equal-length subarrays until only one element remains, then merges them layer by layer to complete the sorting.
      • Introducing the divide and conquer strategy can often improve algorithm efficiency. On one hand, the divide and conquer strategy reduces the number of operations; on the other hand, it facilitates parallel optimization of the system after division.
      • Divide and conquer can both solve many algorithmic problems and is widely applied in data structure and algorithm design, appearing everywhere.
      • Compared to brute-force search, adaptive search is more efficient. Search algorithms with time complexity of \\(O(\\log n)\\) are typically implemented based on the divide and conquer strategy.
      • Binary search is another typical application of divide and conquer. It does not include the step of merging solutions of subproblems. We can implement binary search through recursive divide and conquer.
      • In the problem of building a binary tree, building the tree (original problem) can be divided into building the left subtree and right subtree (subproblems), which can be achieved by dividing the index intervals of the preorder and inorder traversals.
      • In the hanota problem, a problem of size \\(n\\) can be divided into two subproblems of size \\(n-1\\) and one subproblem of size \\(1\\). After solving these three subproblems in order, the original problem is solved.
      "},{"location":"chapter_dynamic_programming/","title":"Chapter 14. \u00a0 Dynamic Programming","text":"

      Abstract

      Streams converge into rivers, rivers converge into the sea.

      Dynamic programming gathers solutions to small problems into answers to large problems, step by step guiding us to the shore of problem-solving.

      "},{"location":"chapter_dynamic_programming/#chapter-contents","title":"Chapter contents","text":"
      • 14.1 \u00a0 Introduction to Dynamic Programming
      • 14.2 \u00a0 Characteristics of Dynamic Programming Problems
      • 14.3 \u00a0 Dynamic Programming Problem-Solving Approach
      • 14.4 \u00a0 0-1 Knapsack Problem
      • 14.5 \u00a0 Unbounded Knapsack Problem
      • 14.6 \u00a0 Edit Distance Problem
      • 14.7 \u00a0 Summary
      "},{"location":"chapter_dynamic_programming/dp_problem_features/","title":"14.2 \u00a0 Characteristics of Dynamic Programming Problems","text":"

      In the previous section, we learned how dynamic programming solves the original problem by decomposing it into subproblems. In fact, subproblem decomposition is a general algorithmic approach, with different emphases in divide and conquer, dynamic programming, and backtracking.

      • Divide and conquer algorithms recursively divide the original problem into multiple independent subproblems until the smallest subproblems are reached, and merge the solutions to the subproblems during backtracking to ultimately obtain the solution to the original problem.
      • Dynamic programming also recursively decomposes problems, but the main difference from divide and conquer algorithms is that subproblems in dynamic programming are interdependent, and many overlapping subproblems appear during the decomposition process.
      • Backtracking algorithms enumerate all possible solutions through trial and error, and avoid unnecessary search branches through pruning. The solution to the original problem consists of a series of decision steps, and we can regard the subsequence before each decision step as a subproblem.

      In fact, dynamic programming is commonly used to solve optimization problems, which not only contain overlapping subproblems but also have two other major characteristics: optimal substructure and no aftereffects.

      "},{"location":"chapter_dynamic_programming/dp_problem_features/#1421-optimal-substructure","title":"14.2.1 \u00a0 Optimal Substructure","text":"

      We make a slight modification to the stair climbing problem to make it more suitable for demonstrating the concept of optimal substructure.

      Climbing stairs with minimum cost

      Given a staircase, where you can climb \\(1\\) or \\(2\\) steps at a time, and each step has a non-negative integer representing the cost you need to pay at that step. Given a non-negative integer array \\(cost\\), where \\(cost[i]\\) represents the cost at the \\(i\\)-th step, and \\(cost[0]\\) is the ground (starting point). What is the minimum cost required to reach the top?

      As shown in Figure 14-6, if the costs of the \\(1\\)st, \\(2\\)nd, and \\(3\\)rd steps are \\(1\\), \\(10\\), and \\(1\\) respectively, then climbing from the ground to the \\(3\\)rd step requires a minimum cost of \\(2\\).

      Figure 14-6 \u00a0 Minimum cost to climb to the 3rd step

      Let \\(dp[i]\\) be the accumulated cost of climbing to the \\(i\\)-th step. Since the \\(i\\)-th step can only come from the \\(i-1\\)-th or \\(i-2\\)-th step, \\(dp[i]\\) can only equal \\(dp[i-1] + cost[i]\\) or \\(dp[i-2] + cost[i]\\). To minimize the cost, we should choose the smaller of the two:

      \\[ dp[i] = \\min(dp[i-1], dp[i-2]) + cost[i] \\]

      This leads us to the meaning of optimal substructure: the optimal solution to the original problem is constructed from the optimal solutions to the subproblems.

      This problem clearly has optimal substructure: we select the better one from the optimal solutions to the two subproblems \\(dp[i-1]\\) and \\(dp[i-2]\\), and use it to construct the optimal solution to the original problem \\(dp[i]\\).

      So, does the stair climbing problem from the previous section have optimal substructure? Its goal is to find the number of ways, which seems to be a counting problem, but if we change the question: \"Find the maximum number of ways\". We surprisingly discover that although the problem before and after modification are equivalent, the optimal substructure has emerged: the maximum number of ways for the \\(n\\)-th step equals the sum of the maximum number of ways for the \\(n-1\\)-th and \\(n-2\\)-th steps. Therefore, the interpretation of optimal substructure is quite flexible and will have different meanings in different problems.

      According to the state transition equation and the initial states \\(dp[1] = cost[1]\\) and \\(dp[2] = cost[2]\\), we can obtain the dynamic programming code:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_cost_climbing_stairs_dp.py
      def min_cost_climbing_stairs_dp(cost: list[int]) -> int:\n    \"\"\"Minimum cost climbing stairs: Dynamic programming\"\"\"\n    n = len(cost) - 1\n    if n == 1 or n == 2:\n        return cost[n]\n    # Initialize dp table, used to store solutions to subproblems\n    dp = [0] * (n + 1)\n    # Initial state: preset the solution to the smallest subproblem\n    dp[1], dp[2] = cost[1], cost[2]\n    # State transition: gradually solve larger subproblems from smaller ones\n    for i in range(3, n + 1):\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n    return dp[n]\n
      min_cost_climbing_stairs_dp.cpp
      /* Minimum cost climbing stairs: Dynamic programming */\nint minCostClimbingStairsDP(vector<int> &cost) {\n    int n = cost.size() - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // Initialize dp table, used to store solutions to subproblems\n    vector<int> dp(n + 1);\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
      min_cost_climbing_stairs_dp.java
      /* Minimum cost climbing stairs: Dynamic programming */\nint minCostClimbingStairsDP(int[] cost) {\n    int n = cost.length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // Initialize dp table, used to store solutions to subproblems\n    int[] dp = new int[n + 1];\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
      min_cost_climbing_stairs_dp.cs
      /* Minimum cost climbing stairs: Dynamic programming */\nint MinCostClimbingStairsDP(int[] cost) {\n    int n = cost.Length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // Initialize dp table, used to store solutions to subproblems\n    int[] dp = new int[n + 1];\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i] = Math.Min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
      min_cost_climbing_stairs_dp.go
      /* Minimum cost climbing stairs: Dynamic programming */\nfunc minCostClimbingStairsDP(cost []int) int {\n    n := len(cost) - 1\n    if n == 1 || n == 2 {\n        return cost[n]\n    }\n    min := func(a, b int) int {\n        if a < b {\n            return a\n        }\n        return b\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    dp := make([]int, n+1)\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = cost[1]\n    dp[2] = cost[2]\n    // State transition: gradually solve larger subproblems from smaller ones\n    for i := 3; i <= n; i++ {\n        dp[i] = min(dp[i-1], dp[i-2]) + cost[i]\n    }\n    return dp[n]\n}\n
      min_cost_climbing_stairs_dp.swift
      /* Minimum cost climbing stairs: Dynamic programming */\nfunc minCostClimbingStairsDP(cost: [Int]) -> Int {\n    let n = cost.count - 1\n    if n == 1 || n == 2 {\n        return cost[n]\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    var dp = Array(repeating: 0, count: n + 1)\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = cost[1]\n    dp[2] = cost[2]\n    // State transition: gradually solve larger subproblems from smaller ones\n    for i in 3 ... n {\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n    }\n    return dp[n]\n}\n
      min_cost_climbing_stairs_dp.js
      /* Minimum cost climbing stairs: Dynamic programming */\nfunction minCostClimbingStairsDP(cost) {\n    const n = cost.length - 1;\n    if (n === 1 || n === 2) {\n        return cost[n];\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    const dp = new Array(n + 1);\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (let i = 3; i <= n; i++) {\n        dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
      min_cost_climbing_stairs_dp.ts
      /* Minimum cost climbing stairs: Dynamic programming */\nfunction minCostClimbingStairsDP(cost: Array<number>): number {\n    const n = cost.length - 1;\n    if (n === 1 || n === 2) {\n        return cost[n];\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    const dp = new Array(n + 1);\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (let i = 3; i <= n; i++) {\n        dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
      min_cost_climbing_stairs_dp.dart
      /* Minimum cost climbing stairs: Dynamic programming */\nint minCostClimbingStairsDP(List<int> cost) {\n  int n = cost.length - 1;\n  if (n == 1 || n == 2) return cost[n];\n  // Initialize dp table, used to store solutions to subproblems\n  List<int> dp = List.filled(n + 1, 0);\n  // Initial state: preset the solution to the smallest subproblem\n  dp[1] = cost[1];\n  dp[2] = cost[2];\n  // State transition: gradually solve larger subproblems from smaller ones\n  for (int i = 3; i <= n; i++) {\n    dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n  }\n  return dp[n];\n}\n
      min_cost_climbing_stairs_dp.rs
      /* Minimum cost climbing stairs: Dynamic programming */\nfn min_cost_climbing_stairs_dp(cost: &[i32]) -> i32 {\n    let n = cost.len() - 1;\n    if n == 1 || n == 2 {\n        return cost[n];\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    let mut dp = vec![-1; n + 1];\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // State transition: gradually solve larger subproblems from smaller ones\n    for i in 3..=n {\n        dp[i] = cmp::min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    dp[n]\n}\n
      min_cost_climbing_stairs_dp.c
      /* Minimum cost climbing stairs: Dynamic programming */\nint minCostClimbingStairsDP(int cost[], int costSize) {\n    int n = costSize - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // Initialize dp table, used to store solutions to subproblems\n    int *dp = calloc(n + 1, sizeof(int));\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i] = myMin(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    int res = dp[n];\n    // Free memory\n    free(dp);\n    return res;\n}\n
      min_cost_climbing_stairs_dp.kt
      /* Minimum cost climbing stairs: Dynamic programming */\nfun minCostClimbingStairsDP(cost: IntArray): Int {\n    val n = cost.size - 1\n    if (n == 1 || n == 2) return cost[n]\n    // Initialize dp table, used to store solutions to subproblems\n    val dp = IntArray(n + 1)\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = cost[1]\n    dp[2] = cost[2]\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (i in 3..n) {\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n    }\n    return dp[n]\n}\n
      min_cost_climbing_stairs_dp.rb
      ### Minimum cost climbing stairs: DP ###\ndef min_cost_climbing_stairs_dp(cost)\n  n = cost.length - 1\n  return cost[n] if n == 1 || n == 2\n  # Initialize dp table, used to store solutions to subproblems\n  dp = Array.new(n + 1, 0)\n  # Initial state: preset the solution to the smallest subproblem\n  dp[1], dp[2] = cost[1], cost[2]\n  # State transition: gradually solve larger subproblems from smaller ones\n  (3...(n + 1)).each { |i| dp[i] = [dp[i - 1], dp[i - 2]].min + cost[i] }\n  dp[n]\nend\n

      Figure 14-7 shows the dynamic programming process for the above code.

      Figure 14-7 \u00a0 Dynamic programming process for climbing stairs with minimum cost

      This problem can also be space-optimized, compressing from one dimension to zero, reducing the space complexity from \\(O(n)\\) to \\(O(1)\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_cost_climbing_stairs_dp.py
      def min_cost_climbing_stairs_dp_comp(cost: list[int]) -> int:\n    \"\"\"Minimum cost climbing stairs: Space-optimized dynamic programming\"\"\"\n    n = len(cost) - 1\n    if n == 1 or n == 2:\n        return cost[n]\n    a, b = cost[1], cost[2]\n    for i in range(3, n + 1):\n        a, b = b, min(a, b) + cost[i]\n    return b\n
      min_cost_climbing_stairs_dp.cpp
      /* Minimum cost climbing stairs: Space-optimized dynamic programming */\nint minCostClimbingStairsDPComp(vector<int> &cost) {\n    int n = cost.size() - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
      min_cost_climbing_stairs_dp.java
      /* Minimum cost climbing stairs: Space-optimized dynamic programming */\nint minCostClimbingStairsDPComp(int[] cost) {\n    int n = cost.length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = Math.min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
      min_cost_climbing_stairs_dp.cs
      /* Minimum cost climbing stairs: Space-optimized dynamic programming */\nint MinCostClimbingStairsDPComp(int[] cost) {\n    int n = cost.Length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = Math.Min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
      min_cost_climbing_stairs_dp.go
      /* Minimum cost climbing stairs: Space-optimized dynamic programming */\nfunc minCostClimbingStairsDPComp(cost []int) int {\n    n := len(cost) - 1\n    if n == 1 || n == 2 {\n        return cost[n]\n    }\n    min := func(a, b int) int {\n        if a < b {\n            return a\n        }\n        return b\n    }\n    // Initial state: preset the solution to the smallest subproblem\n    a, b := cost[1], cost[2]\n    // State transition: gradually solve larger subproblems from smaller ones\n    for i := 3; i <= n; i++ {\n        tmp := b\n        b = min(a, tmp) + cost[i]\n        a = tmp\n    }\n    return b\n}\n
      min_cost_climbing_stairs_dp.swift
      /* Minimum cost climbing stairs: Space-optimized dynamic programming */\nfunc minCostClimbingStairsDPComp(cost: [Int]) -> Int {\n    let n = cost.count - 1\n    if n == 1 || n == 2 {\n        return cost[n]\n    }\n    var (a, b) = (cost[1], cost[2])\n    for i in 3 ... n {\n        (a, b) = (b, min(a, b) + cost[i])\n    }\n    return b\n}\n
      min_cost_climbing_stairs_dp.js
      /* Minimum cost climbing stairs: Space-optimized dynamic programming */\nfunction minCostClimbingStairsDPComp(cost) {\n    const n = cost.length - 1;\n    if (n === 1 || n === 2) {\n        return cost[n];\n    }\n    let a = cost[1],\n        b = cost[2];\n    for (let i = 3; i <= n; i++) {\n        const tmp = b;\n        b = Math.min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
      min_cost_climbing_stairs_dp.ts
      /* Minimum cost climbing stairs: Space-optimized dynamic programming */\nfunction minCostClimbingStairsDPComp(cost: Array<number>): number {\n    const n = cost.length - 1;\n    if (n === 1 || n === 2) {\n        return cost[n];\n    }\n    let a = cost[1],\n        b = cost[2];\n    for (let i = 3; i <= n; i++) {\n        const tmp = b;\n        b = Math.min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
      min_cost_climbing_stairs_dp.dart
      /* Minimum cost climbing stairs: Space-optimized dynamic programming */\nint minCostClimbingStairsDPComp(List<int> cost) {\n  int n = cost.length - 1;\n  if (n == 1 || n == 2) return cost[n];\n  int a = cost[1], b = cost[2];\n  for (int i = 3; i <= n; i++) {\n    int tmp = b;\n    b = min(a, tmp) + cost[i];\n    a = tmp;\n  }\n  return b;\n}\n
      min_cost_climbing_stairs_dp.rs
      /* Minimum cost climbing stairs: Space-optimized dynamic programming */\nfn min_cost_climbing_stairs_dp_comp(cost: &[i32]) -> i32 {\n    let n = cost.len() - 1;\n    if n == 1 || n == 2 {\n        return cost[n];\n    };\n    let (mut a, mut b) = (cost[1], cost[2]);\n    for i in 3..=n {\n        let tmp = b;\n        b = cmp::min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    b\n}\n
      min_cost_climbing_stairs_dp.c
      /* Minimum cost climbing stairs: Space-optimized dynamic programming */\nint minCostClimbingStairsDPComp(int cost[], int costSize) {\n    int n = costSize - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = myMin(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
      min_cost_climbing_stairs_dp.kt
      /* Minimum cost climbing stairs: Space-optimized dynamic programming */\nfun minCostClimbingStairsDPComp(cost: IntArray): Int {\n    val n = cost.size - 1\n    if (n == 1 || n == 2) return cost[n]\n    var a = cost[1]\n    var b = cost[2]\n    for (i in 3..n) {\n        val tmp = b\n        b = min(a, tmp) + cost[i]\n        a = tmp\n    }\n    return b\n}\n
      min_cost_climbing_stairs_dp.rb
      ### Minimum cost climbing stairs: DP ###\ndef min_cost_climbing_stairs_dp(cost)\n  n = cost.length - 1\n  return cost[n] if n == 1 || n == 2\n  # Initialize dp table, used to store solutions to subproblems\n  dp = Array.new(n + 1, 0)\n  # Initial state: preset the solution to the smallest subproblem\n  dp[1], dp[2] = cost[1], cost[2]\n  # State transition: gradually solve larger subproblems from smaller ones\n  (3...(n + 1)).each { |i| dp[i] = [dp[i - 1], dp[i - 2]].min + cost[i] }\n  dp[n]\nend\n\n# Minimum cost climbing stairs: Space-optimized dynamic programming\ndef min_cost_climbing_stairs_dp_comp(cost)\n  n = cost.length - 1\n  return cost[n] if n == 1 || n == 2\n  a, b = cost[1], cost[2]\n  (3...(n + 1)).each { |i| a, b = b, [a, b].min + cost[i] }\n  b\nend\n
      "},{"location":"chapter_dynamic_programming/dp_problem_features/#1422-no-aftereffects","title":"14.2.2 \u00a0 No Aftereffects","text":"

      No aftereffects is one of the important characteristics that enable dynamic programming to solve problems effectively. Its definition is: given a certain state, its future development is only related to the current state and has nothing to do with all past states.

      Taking the stair climbing problem as an example, given state \\(i\\), it will develop into states \\(i+1\\) and \\(i+2\\), corresponding to jumping \\(1\\) step and jumping \\(2\\) steps, respectively. When making these two choices, we do not need to consider the states before state \\(i\\), as they have no effect on the future of state \\(i\\).

      However, if we add a constraint to the stair climbing problem, the situation changes.

      Climbing stairs with constraint

      Given a staircase with \\(n\\) steps, where you can climb \\(1\\) or \\(2\\) steps at a time, but you cannot jump \\(1\\) step in two consecutive rounds. How many ways are there to climb to the top?

      As shown in Figure 14-8, there are only \\(2\\) feasible ways to climb to the \\(3\\)rd step. The way of jumping \\(1\\) step three consecutive times does not satisfy the constraint and is therefore discarded.

      Figure 14-8 \u00a0 Number of ways to climb to the 3rd step with constraint

      In this problem, if the previous round was a jump of \\(1\\) step, then the next round must jump \\(2\\) steps. This means that the next choice cannot be determined solely by the current state (current stair step number), but also depends on the previous state (the stair step number from the previous round).

      It is not difficult to see that this problem no longer satisfies no aftereffects, and the state transition equation \\(dp[i] = dp[i-1] + dp[i-2]\\) also fails, because \\(dp[i-1]\\) represents jumping \\(1\\) step in this round, but it includes many solutions where \"the previous round was a jump of \\(1\\) step\", which cannot be directly counted in \\(dp[i]\\) to satisfy the constraint.

      For this reason, we need to expand the state definition: state \\([i, j]\\) represents being on the \\(i\\)-th step with the previous round having jumped \\(j\\) steps, where \\(j \\in \\{1, 2\\}\\). This state definition effectively distinguishes whether the previous round was a jump of \\(1\\) step or \\(2\\) steps, allowing us to determine where the current state came from.

      • When the previous round jumped \\(1\\) step, the round before that could only choose to jump \\(2\\) steps, i.e., \\(dp[i, 1]\\) can only be transferred from \\(dp[i-1, 2]\\).
      • When the previous round jumped \\(2\\) steps, the round before that could choose to jump \\(1\\) step or \\(2\\) steps, i.e., \\(dp[i, 2]\\) can be transferred from \\(dp[i-2, 1]\\) or \\(dp[i-2, 2]\\).

      As shown in Figure 14-9, under this definition, \\(dp[i, j]\\) represents the number of ways for state \\([i, j]\\). The state transition equation is then:

      \\[ \\begin{cases} dp[i, 1] = dp[i-1, 2] \\\\ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2] \\end{cases} \\]

      Figure 14-9 \u00a0 Recurrence relation considering constraints

      Finally, return \\(dp[n, 1] + dp[n, 2]\\), where the sum of the two represents the total number of ways to climb to the \\(n\\)-th step:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_constraint_dp.py
      def climbing_stairs_constraint_dp(n: int) -> int:\n    \"\"\"Climbing stairs with constraint: Dynamic programming\"\"\"\n    if n == 1 or n == 2:\n        return 1\n    # Initialize dp table, used to store solutions to subproblems\n    dp = [[0] * 3 for _ in range(n + 1)]\n    # Initial state: preset the solution to the smallest subproblem\n    dp[1][1], dp[1][2] = 1, 0\n    dp[2][1], dp[2][2] = 0, 1\n    # State transition: gradually solve larger subproblems from smaller ones\n    for i in range(3, n + 1):\n        dp[i][1] = dp[i - 1][2]\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n    return dp[n][1] + dp[n][2]\n
      climbing_stairs_constraint_dp.cpp
      /* Climbing stairs with constraint: Dynamic programming */\nint climbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    vector<vector<int>> dp(n + 1, vector<int>(3, 0));\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
      climbing_stairs_constraint_dp.java
      /* Climbing stairs with constraint: Dynamic programming */\nint climbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    int[][] dp = new int[n + 1][3];\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
      climbing_stairs_constraint_dp.cs
      /* Climbing stairs with constraint: Dynamic programming */\nint ClimbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    int[,] dp = new int[n + 1, 3];\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1, 1] = 1;\n    dp[1, 2] = 0;\n    dp[2, 1] = 0;\n    dp[2, 2] = 1;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i, 1] = dp[i - 1, 2];\n        dp[i, 2] = dp[i - 2, 1] + dp[i - 2, 2];\n    }\n    return dp[n, 1] + dp[n, 2];\n}\n
      climbing_stairs_constraint_dp.go
      /* Climbing stairs with constraint: Dynamic programming */\nfunc climbingStairsConstraintDP(n int) int {\n    if n == 1 || n == 2 {\n        return 1\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    dp := make([][3]int, n+1)\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1][1] = 1\n    dp[1][2] = 0\n    dp[2][1] = 0\n    dp[2][2] = 1\n    // State transition: gradually solve larger subproblems from smaller ones\n    for i := 3; i <= n; i++ {\n        dp[i][1] = dp[i-1][2]\n        dp[i][2] = dp[i-2][1] + dp[i-2][2]\n    }\n    return dp[n][1] + dp[n][2]\n}\n
      climbing_stairs_constraint_dp.swift
      /* Climbing stairs with constraint: Dynamic programming */\nfunc climbingStairsConstraintDP(n: Int) -> Int {\n    if n == 1 || n == 2 {\n        return 1\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    var dp = Array(repeating: Array(repeating: 0, count: 3), count: n + 1)\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1][1] = 1\n    dp[1][2] = 0\n    dp[2][1] = 0\n    dp[2][2] = 1\n    // State transition: gradually solve larger subproblems from smaller ones\n    for i in 3 ... n {\n        dp[i][1] = dp[i - 1][2]\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n    }\n    return dp[n][1] + dp[n][2]\n}\n
      climbing_stairs_constraint_dp.js
      /* Climbing stairs with constraint: Dynamic programming */\nfunction climbingStairsConstraintDP(n) {\n    if (n === 1 || n === 2) {\n        return 1;\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    const dp = Array.from(new Array(n + 1), () => new Array(3));\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (let i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
      climbing_stairs_constraint_dp.ts
      /* Climbing stairs with constraint: Dynamic programming */\nfunction climbingStairsConstraintDP(n: number): number {\n    if (n === 1 || n === 2) {\n        return 1;\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    const dp = Array.from({ length: n + 1 }, () => new Array(3));\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (let i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
      climbing_stairs_constraint_dp.dart
      /* Climbing stairs with constraint: Dynamic programming */\nint climbingStairsConstraintDP(int n) {\n  if (n == 1 || n == 2) {\n    return 1;\n  }\n  // Initialize dp table, used to store solutions to subproblems\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(3, 0));\n  // Initial state: preset the solution to the smallest subproblem\n  dp[1][1] = 1;\n  dp[1][2] = 0;\n  dp[2][1] = 0;\n  dp[2][2] = 1;\n  // State transition: gradually solve larger subproblems from smaller ones\n  for (int i = 3; i <= n; i++) {\n    dp[i][1] = dp[i - 1][2];\n    dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n  }\n  return dp[n][1] + dp[n][2];\n}\n
      climbing_stairs_constraint_dp.rs
      /* Climbing stairs with constraint: Dynamic programming */\nfn climbing_stairs_constraint_dp(n: usize) -> i32 {\n    if n == 1 || n == 2 {\n        return 1;\n    };\n    // Initialize dp table, used to store solutions to subproblems\n    let mut dp = vec![vec![-1; 3]; n + 1];\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for i in 3..=n {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    dp[n][1] + dp[n][2]\n}\n
      climbing_stairs_constraint_dp.c
      /* Climbing stairs with constraint: Dynamic programming */\nint climbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(3, sizeof(int));\n    }\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    int res = dp[n][1] + dp[n][2];\n    // Free memory\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    free(dp);\n    return res;\n}\n
      climbing_stairs_constraint_dp.kt
      /* Climbing stairs with constraint: Dynamic programming */\nfun climbingStairsConstraintDP(n: Int): Int {\n    if (n == 1 || n == 2) {\n        return 1\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    val dp = Array(n + 1) { IntArray(3) }\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1][1] = 1\n    dp[1][2] = 0\n    dp[2][1] = 0\n    dp[2][2] = 1\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (i in 3..n) {\n        dp[i][1] = dp[i - 1][2]\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n    }\n    return dp[n][1] + dp[n][2]\n}\n
      climbing_stairs_constraint_dp.rb
      ### Climbing stairs with constraint: DP ###\ndef climbing_stairs_constraint_dp(n)\n  return 1 if n == 1 || n == 2\n\n  # Initialize dp table, used to store solutions to subproblems\n  dp = Array.new(n + 1) { Array.new(3, 0) }\n  # Initial state: preset the solution to the smallest subproblem\n  dp[1][1], dp[1][2] = 1, 0\n  dp[2][1], dp[2][2] = 0, 1\n  # State transition: gradually solve larger subproblems from smaller ones\n  for i in 3...(n + 1)\n    dp[i][1] = dp[i - 1][2]\n    dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n  end\n\n  dp[n][1] + dp[n][2]\nend\n

      In the above case, since we only need to consider one more preceding state, we can still make the problem satisfy no aftereffects by expanding the state definition. However, some problems have very severe \"aftereffects\".

      Climbing stairs with obstacle generation

      Given a staircase with \\(n\\) steps, where you can climb \\(1\\) or \\(2\\) steps at a time. It is stipulated that when climbing to the \\(i\\)-th step, the system will automatically place an obstacle on the \\(2i\\)-th step, and thereafter no round is allowed to jump to the \\(2i\\)-th step. For example, if the first two rounds jump to the \\(2\\)nd and \\(3\\)rd steps, then afterwards you cannot jump to the \\(4\\)th and \\(6\\)th steps. How many ways are there to climb to the top?

      In this problem, the next jump depends on all past states, because each jump places obstacles on higher steps, affecting future jumps. For such problems, dynamic programming is often difficult to solve.

      In fact, many complex combinatorial optimization problems (such as the traveling salesman problem) do not satisfy no aftereffects. For such problems, we usually choose to use other methods, such as heuristic search, genetic algorithms, reinforcement learning, etc., to obtain usable local optimal solutions within a limited time.

      "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/","title":"14.3 \u00a0 Dynamic Programming Problem-Solving Approach","text":"

      The previous two sections introduced the main characteristics of dynamic programming problems. Next, let us explore two more practical issues together.

      1. How to determine whether a problem is a dynamic programming problem?
      2. What is the complete process for solving a dynamic programming problem, and where should we start?
      "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1431-problem-determination","title":"14.3.1 \u00a0 Problem Determination","text":"

      Generally speaking, if a problem contains overlapping subproblems, optimal substructure, and satisfies no aftereffects, then it is usually suitable for solving with dynamic programming. However, it is difficult to directly extract these characteristics from the problem description. Therefore, we usually relax the conditions and first observe whether the problem is suitable for solving with backtracking (exhaustive search).

      Problems suitable for solving with backtracking usually satisfy the \"decision tree model\", which means the problem can be described using a tree structure, where each node represents a decision and each path represents a sequence of decisions.

      In other words, if a problem contains an explicit concept of decisions, and the solution is generated through a series of decisions, then it satisfies the decision tree model and can usually be solved using backtracking.

      On this basis, dynamic programming problems also have some \"bonus points\" for determination.

      • The problem contains descriptions such as maximum (minimum) or most (least), indicating optimization.
      • The problem's state can be represented using a list, multi-dimensional matrix, or tree, and a state has a recurrence relation with its surrounding states.

      Correspondingly, there are also some \"penalty points\".

      • The goal of the problem is to find all possible solutions, rather than finding the optimal solution.
      • The problem description has obvious permutation and combination characteristics, requiring the return of specific multiple solutions.

      If a problem satisfies the decision tree model and has relatively obvious \"bonus points\", we can assume it is a dynamic programming problem and verify it during the solving process.

      "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1432-problem-solving-steps","title":"14.3.2 \u00a0 Problem-Solving Steps","text":"

      The problem-solving process for dynamic programming varies depending on the nature and difficulty of the problem, but generally follows these steps: describe decisions, define states, establish the \\(dp\\) table, derive state transition equations, determine boundary conditions, etc.

      To illustrate the problem-solving steps more vividly, we use a classic problem \"minimum path sum\" as an example.

      Question

      Given an \\(n \\times m\\) two-dimensional grid grid, where each cell in the grid contains a non-negative integer representing the cost of that cell. A robot starts from the top-left cell and can only move down or right at each step until reaching the bottom-right cell. Return the minimum path sum from the top-left to the bottom-right.

      Figure 14-10 shows an example where the minimum path sum for the given grid is \\(13\\).

      Figure 14-10 \u00a0 Minimum path sum example data

      Step 1: Think about the decisions in each round, define the state, and thus obtain the \\(dp\\) table

      The decision in each round of this problem is to move one step down or right from the current cell. Let the row and column indices of the current cell be \\([i, j]\\). After moving down or right, the indices become \\([i+1, j]\\) or \\([i, j+1]\\). Therefore, the state should include two variables, the row index and column index, denoted as \\([i, j]\\).

      State \\([i, j]\\) corresponds to the subproblem: the minimum path sum from the starting point \\([0, 0]\\) to \\([i, j]\\), denoted as \\(dp[i, j]\\).

      From this, we obtain the two-dimensional \\(dp\\) matrix shown in Figure 14-11, whose size is the same as the input grid \\(grid\\).

      Figure 14-11 \u00a0 State definition and dp table

      Note

      The dynamic programming and backtracking processes can be described as a sequence of decisions, and the state consists of all decision variables. It should contain all variables describing the progress of problem-solving, and should contain sufficient information to derive the next state.

      Each state corresponds to a subproblem, and we define a \\(dp\\) table to store the solutions to all subproblems. Each independent variable of the state is a dimension of the \\(dp\\) table. Essentially, the \\(dp\\) table is a mapping between states and solutions to subproblems.

      Step 2: Identify the optimal substructure, and then derive the state transition equation

      For state \\([i, j]\\), it can only be transferred from the cell above \\([i-1, j]\\) or the cell to the left \\([i, j-1]\\). Therefore, the optimal substructure is: the minimum path sum to reach \\([i, j]\\) is determined by the smaller of the minimum path sums of \\([i, j-1]\\) and \\([i-1, j]\\).

      Based on the above analysis, the state transition equation shown in Figure 14-12 can be derived:

      \\[ dp[i, j] = \\min(dp[i-1, j], dp[i, j-1]) + grid[i, j] \\]

      Figure 14-12 \u00a0 Optimal substructure and state transition equation

      Note

      Based on the defined \\(dp\\) table, think about the relationship between the original problem and subproblems, and find the method to construct the optimal solution to the original problem from the optimal solutions to the subproblems, which is the optimal substructure.

      Once we identify the optimal substructure, we can use it to construct the state transition equation.

      Step 3: Determine boundary conditions and state transition order

      In this problem, states in the first row can only come from the state to their left, and states in the first column can only come from the state above them. Therefore, the first row \\(i = 0\\) and first column \\(j = 0\\) are boundary conditions.

      As shown in Figure 14-13, since each cell is transferred from the cell to its left and the cell above it, we use loops to traverse the matrix, with the outer loop traversing rows and the inner loop traversing columns.

      Figure 14-13 \u00a0 Boundary conditions and state transition order

      Note

      Boundary conditions in dynamic programming are used to initialize the \\(dp\\) table, and in search are used for pruning.

      The core of state transition order is to ensure that when computing the solution to the current problem, all the smaller subproblems it depends on have already been computed correctly.

      Based on the above analysis, we can directly write the dynamic programming code. However, subproblem decomposition is a top-down approach, so implementing in the order \"brute force search \\(\\rightarrow\\) memoization \\(\\rightarrow\\) dynamic programming\" is more aligned with thinking habits.

      "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1-method-1-brute-force-search","title":"1. \u00a0 Method 1: Brute Force Search","text":"

      Starting from state \\([i, j]\\), continuously decompose into smaller states \\([i-1, j]\\) and \\([i, j-1]\\). The recursive function includes the following elements.

      • Recursive parameters: state \\([i, j]\\).
      • Return value: minimum path sum from \\([0, 0]\\) to \\([i, j]\\), which is \\(dp[i, j]\\).
      • Termination condition: when \\(i = 0\\) and \\(j = 0\\), return cost \\(grid[0, 0]\\).
      • Pruning: when \\(i < 0\\) or \\(j < 0\\), the index is out of bounds, return cost \\(+\\infty\\), representing infeasibility.

      The implementation code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
      def min_path_sum_dfs(grid: list[list[int]], i: int, j: int) -> int:\n    \"\"\"Minimum path sum: Brute-force search\"\"\"\n    # If it's the top-left cell, terminate the search\n    if i == 0 and j == 0:\n        return grid[0][0]\n    # If row or column index is out of bounds, return +\u221e cost\n    if i < 0 or j < 0:\n        return inf\n    # Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n    up = min_path_sum_dfs(grid, i - 1, j)\n    left = min_path_sum_dfs(grid, i, j - 1)\n    # Return the minimum path cost from top-left to (i, j)\n    return min(left, up) + grid[i][j]\n
      min_path_sum.cpp
      /* Minimum path sum: Brute-force search */\nint minPathSumDFS(vector<vector<int>> &grid, int i, int j) {\n    // If it's the top-left cell, terminate the search\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return INT_MAX;\n    }\n    // Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n    int up = minPathSumDFS(grid, i - 1, j);\n    int left = minPathSumDFS(grid, i, j - 1);\n    // Return the minimum path cost from top-left to (i, j)\n    return min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\n}\n
      min_path_sum.java
      /* Minimum path sum: Brute-force search */\nint minPathSumDFS(int[][] grid, int i, int j) {\n    // If it's the top-left cell, terminate the search\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return Integer.MAX_VALUE;\n    }\n    // Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n    int up = minPathSumDFS(grid, i - 1, j);\n    int left = minPathSumDFS(grid, i, j - 1);\n    // Return the minimum path cost from top-left to (i, j)\n    return Math.min(left, up) + grid[i][j];\n}\n
      min_path_sum.cs
      /* Minimum path sum: Brute-force search */\nint MinPathSumDFS(int[][] grid, int i, int j) {\n    // If it's the top-left cell, terminate the search\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return int.MaxValue;\n    }\n    // Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n    int up = MinPathSumDFS(grid, i - 1, j);\n    int left = MinPathSumDFS(grid, i, j - 1);\n    // Return the minimum path cost from top-left to (i, j)\n    return Math.Min(left, up) + grid[i][j];\n}\n
      min_path_sum.go
      /* Minimum path sum: Brute-force search */\nfunc minPathSumDFS(grid [][]int, i, j int) int {\n    // If it's the top-left cell, terminate the search\n    if i == 0 && j == 0 {\n        return grid[0][0]\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if i < 0 || j < 0 {\n        return math.MaxInt\n    }\n    // Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n    up := minPathSumDFS(grid, i-1, j)\n    left := minPathSumDFS(grid, i, j-1)\n    // Return the minimum path cost from top-left to (i, j)\n    return int(math.Min(float64(left), float64(up))) + grid[i][j]\n}\n
      min_path_sum.swift
      /* Minimum path sum: Brute-force search */\nfunc minPathSumDFS(grid: [[Int]], i: Int, j: Int) -> Int {\n    // If it's the top-left cell, terminate the search\n    if i == 0, j == 0 {\n        return grid[0][0]\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if i < 0 || j < 0 {\n        return .max\n    }\n    // Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n    let up = minPathSumDFS(grid: grid, i: i - 1, j: j)\n    let left = minPathSumDFS(grid: grid, i: i, j: j - 1)\n    // Return the minimum path cost from top-left to (i, j)\n    return min(left, up) + grid[i][j]\n}\n
      min_path_sum.js
      /* Minimum path sum: Brute-force search */\nfunction minPathSumDFS(grid, i, j) {\n    // If it's the top-left cell, terminate the search\n    if (i === 0 && j === 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return Infinity;\n    }\n    // Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n    const up = minPathSumDFS(grid, i - 1, j);\n    const left = minPathSumDFS(grid, i, j - 1);\n    // Return the minimum path cost from top-left to (i, j)\n    return Math.min(left, up) + grid[i][j];\n}\n
      min_path_sum.ts
      /* Minimum path sum: Brute-force search */\nfunction minPathSumDFS(\n    grid: Array<Array<number>>,\n    i: number,\n    j: number\n): number {\n    // If it's the top-left cell, terminate the search\n    if (i === 0 && j == 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return Infinity;\n    }\n    // Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n    const up = minPathSumDFS(grid, i - 1, j);\n    const left = minPathSumDFS(grid, i, j - 1);\n    // Return the minimum path cost from top-left to (i, j)\n    return Math.min(left, up) + grid[i][j];\n}\n
      min_path_sum.dart
      /* Minimum path sum: Brute-force search */\nint minPathSumDFS(List<List<int>> grid, int i, int j) {\n  // If it's the top-left cell, terminate the search\n  if (i == 0 && j == 0) {\n    return grid[0][0];\n  }\n  // If row or column index is out of bounds, return +\u221e cost\n  if (i < 0 || j < 0) {\n    // In Dart, int type is fixed-range integer, no value representing \"infinity\"\n    return BigInt.from(2).pow(31).toInt();\n  }\n  // Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n  int up = minPathSumDFS(grid, i - 1, j);\n  int left = minPathSumDFS(grid, i, j - 1);\n  // Return the minimum path cost from top-left to (i, j)\n  return min(left, up) + grid[i][j];\n}\n
      min_path_sum.rs
      /* Minimum path sum: Brute-force search */\nfn min_path_sum_dfs(grid: &Vec<Vec<i32>>, i: i32, j: i32) -> i32 {\n    // If it's the top-left cell, terminate the search\n    if i == 0 && j == 0 {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if i < 0 || j < 0 {\n        return i32::MAX;\n    }\n    // Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n    let up = min_path_sum_dfs(grid, i - 1, j);\n    let left = min_path_sum_dfs(grid, i, j - 1);\n    // Return the minimum path cost from top-left to (i, j)\n    std::cmp::min(left, up) + grid[i as usize][j as usize]\n}\n
      min_path_sum.c
      /* Minimum path sum: Brute-force search */\nint minPathSumDFS(int grid[MAX_SIZE][MAX_SIZE], int i, int j) {\n    // If it's the top-left cell, terminate the search\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return INT_MAX;\n    }\n    // Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n    int up = minPathSumDFS(grid, i - 1, j);\n    int left = minPathSumDFS(grid, i, j - 1);\n    // Return the minimum path cost from top-left to (i, j)\n    return myMin(left, up) != INT_MAX ? myMin(left, up) + grid[i][j] : INT_MAX;\n}\n
      min_path_sum.kt
      /* Minimum path sum: Brute-force search */\nfun minPathSumDFS(grid: Array<IntArray>, i: Int, j: Int): Int {\n    // If it's the top-left cell, terminate the search\n    if (i == 0 && j == 0) {\n        return grid[0][0]\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return Int.MAX_VALUE\n    }\n    // Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n    val up = minPathSumDFS(grid, i - 1, j)\n    val left = minPathSumDFS(grid, i, j - 1)\n    // Return the minimum path cost from top-left to (i, j)\n    return min(left, up) + grid[i][j]\n}\n
      min_path_sum.rb
      ### Minimum path sum: brute force search ###\ndef min_path_sum_dfs(grid, i, j)\n  # If it's the top-left cell, terminate the search\n  return grid[i][j] if i == 0 && j == 0\n  # If row or column index is out of bounds, return +\u221e cost\n  return Float::INFINITY if i < 0 || j < 0\n  # Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n  up = min_path_sum_dfs(grid, i - 1, j)\n  left = min_path_sum_dfs(grid, i, j - 1)\n  # Return the minimum path cost from top-left to (i, j)\n  [left, up].min + grid[i][j]\nend\n

      Figure 14-14 shows the recursion tree rooted at \\(dp[2, 1]\\), which includes some overlapping subproblems whose number will increase sharply as the size of grid grid grows.

      Essentially, the reason for overlapping subproblems is: there are multiple paths from the top-left corner to reach a certain cell.

      Figure 14-14 \u00a0 Brute force search recursion tree

      Each state has two choices, down and right, so the total number of steps from the top-left corner to the bottom-right corner is \\(m + n - 2\\), giving a worst-case time complexity of \\(O(2^{m + n})\\), where \\(n\\) and \\(m\\) are the number of rows and columns of the grid, respectively. Note that this calculation does not account for situations near the grid boundaries, where only one choice remains when reaching the grid boundary, so the actual number of paths will be somewhat less.

      "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#2-method-2-memoization","title":"2. \u00a0 Method 2: Memoization","text":"

      We introduce a memo list mem of the same size as grid grid to record the solutions to subproblems and prune overlapping subproblems:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
      def min_path_sum_dfs_mem(\n    grid: list[list[int]], mem: list[list[int]], i: int, j: int\n) -> int:\n    \"\"\"Minimum path sum: Memoization search\"\"\"\n    # If it's the top-left cell, terminate the search\n    if i == 0 and j == 0:\n        return grid[0][0]\n    # If row or column index is out of bounds, return +\u221e cost\n    if i < 0 or j < 0:\n        return inf\n    # If there's a record, return it directly\n    if mem[i][j] != -1:\n        return mem[i][j]\n    # Minimum path cost for left and upper cells\n    up = min_path_sum_dfs_mem(grid, mem, i - 1, j)\n    left = min_path_sum_dfs_mem(grid, mem, i, j - 1)\n    # Record and return the minimum path cost from top-left to (i, j)\n    mem[i][j] = min(left, up) + grid[i][j]\n    return mem[i][j]\n
      min_path_sum.cpp
      /* Minimum path sum: Memoization search */\nint minPathSumDFSMem(vector<vector<int>> &grid, vector<vector<int>> &mem, int i, int j) {\n    // If it's the top-left cell, terminate the search\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return INT_MAX;\n    }\n    // If there's a record, return it directly\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // Minimum path cost for left and upper cells\n    int up = minPathSumDFSMem(grid, mem, i - 1, j);\n    int left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // Record and return the minimum path cost from top-left to (i, j)\n    mem[i][j] = min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\n    return mem[i][j];\n}\n
      min_path_sum.java
      /* Minimum path sum: Memoization search */\nint minPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n    // If it's the top-left cell, terminate the search\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return Integer.MAX_VALUE;\n    }\n    // If there's a record, return it directly\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // Minimum path cost for left and upper cells\n    int up = minPathSumDFSMem(grid, mem, i - 1, j);\n    int left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // Record and return the minimum path cost from top-left to (i, j)\n    mem[i][j] = Math.min(left, up) + grid[i][j];\n    return mem[i][j];\n}\n
      min_path_sum.cs
      /* Minimum path sum: Memoization search */\nint MinPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n    // If it's the top-left cell, terminate the search\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return int.MaxValue;\n    }\n    // If there's a record, return it directly\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // Minimum path cost for left and upper cells\n    int up = MinPathSumDFSMem(grid, mem, i - 1, j);\n    int left = MinPathSumDFSMem(grid, mem, i, j - 1);\n    // Record and return the minimum path cost from top-left to (i, j)\n    mem[i][j] = Math.Min(left, up) + grid[i][j];\n    return mem[i][j];\n}\n
      min_path_sum.go
      /* Minimum path sum: Memoization search */\nfunc minPathSumDFSMem(grid, mem [][]int, i, j int) int {\n    // If it's the top-left cell, terminate the search\n    if i == 0 && j == 0 {\n        return grid[0][0]\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if i < 0 || j < 0 {\n        return math.MaxInt\n    }\n    // If there's a record, return it directly\n    if mem[i][j] != -1 {\n        return mem[i][j]\n    }\n    // Minimum path cost for left and upper cells\n    up := minPathSumDFSMem(grid, mem, i-1, j)\n    left := minPathSumDFSMem(grid, mem, i, j-1)\n    // Record and return the minimum path cost from top-left to (i, j)\n    mem[i][j] = int(math.Min(float64(left), float64(up))) + grid[i][j]\n    return mem[i][j]\n}\n
      min_path_sum.swift
      /* Minimum path sum: Memoization search */\nfunc minPathSumDFSMem(grid: [[Int]], mem: inout [[Int]], i: Int, j: Int) -> Int {\n    // If it's the top-left cell, terminate the search\n    if i == 0, j == 0 {\n        return grid[0][0]\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if i < 0 || j < 0 {\n        return .max\n    }\n    // If there's a record, return it directly\n    if mem[i][j] != -1 {\n        return mem[i][j]\n    }\n    // Minimum path cost for left and upper cells\n    let up = minPathSumDFSMem(grid: grid, mem: &mem, i: i - 1, j: j)\n    let left = minPathSumDFSMem(grid: grid, mem: &mem, i: i, j: j - 1)\n    // Record and return the minimum path cost from top-left to (i, j)\n    mem[i][j] = min(left, up) + grid[i][j]\n    return mem[i][j]\n}\n
      min_path_sum.js
      /* Minimum path sum: Memoization search */\nfunction minPathSumDFSMem(grid, mem, i, j) {\n    // If it's the top-left cell, terminate the search\n    if (i === 0 && j === 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return Infinity;\n    }\n    // If there's a record, return it directly\n    if (mem[i][j] !== -1) {\n        return mem[i][j];\n    }\n    // Minimum path cost for left and upper cells\n    const up = minPathSumDFSMem(grid, mem, i - 1, j);\n    const left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // Record and return the minimum path cost from top-left to (i, j)\n    mem[i][j] = Math.min(left, up) + grid[i][j];\n    return mem[i][j];\n}\n
      min_path_sum.ts
      /* Minimum path sum: Memoization search */\nfunction minPathSumDFSMem(\n    grid: Array<Array<number>>,\n    mem: Array<Array<number>>,\n    i: number,\n    j: number\n): number {\n    // If it's the top-left cell, terminate the search\n    if (i === 0 && j === 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return Infinity;\n    }\n    // If there's a record, return it directly\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // Minimum path cost for left and upper cells\n    const up = minPathSumDFSMem(grid, mem, i - 1, j);\n    const left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // Record and return the minimum path cost from top-left to (i, j)\n    mem[i][j] = Math.min(left, up) + grid[i][j];\n    return mem[i][j];\n}\n
      min_path_sum.dart
      /* Minimum path sum: Memoization search */\nint minPathSumDFSMem(List<List<int>> grid, List<List<int>> mem, int i, int j) {\n  // If it's the top-left cell, terminate the search\n  if (i == 0 && j == 0) {\n    return grid[0][0];\n  }\n  // If row or column index is out of bounds, return +\u221e cost\n  if (i < 0 || j < 0) {\n    // In Dart, int type is fixed-range integer, no value representing \"infinity\"\n    return BigInt.from(2).pow(31).toInt();\n  }\n  // If there's a record, return it directly\n  if (mem[i][j] != -1) {\n    return mem[i][j];\n  }\n  // Minimum path cost for left and upper cells\n  int up = minPathSumDFSMem(grid, mem, i - 1, j);\n  int left = minPathSumDFSMem(grid, mem, i, j - 1);\n  // Record and return the minimum path cost from top-left to (i, j)\n  mem[i][j] = min(left, up) + grid[i][j];\n  return mem[i][j];\n}\n
      min_path_sum.rs
      /* Minimum path sum: Memoization search */\nfn min_path_sum_dfs_mem(grid: &Vec<Vec<i32>>, mem: &mut Vec<Vec<i32>>, i: i32, j: i32) -> i32 {\n    // If it's the top-left cell, terminate the search\n    if i == 0 && j == 0 {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if i < 0 || j < 0 {\n        return i32::MAX;\n    }\n    // If there's a record, return it directly\n    if mem[i as usize][j as usize] != -1 {\n        return mem[i as usize][j as usize];\n    }\n    // Minimum path cost for left and upper cells\n    let up = min_path_sum_dfs_mem(grid, mem, i - 1, j);\n    let left = min_path_sum_dfs_mem(grid, mem, i, j - 1);\n    // Record and return the minimum path cost from top-left to (i, j)\n    mem[i as usize][j as usize] = std::cmp::min(left, up) + grid[i as usize][j as usize];\n    mem[i as usize][j as usize]\n}\n
      min_path_sum.c
      /* Minimum path sum: Memoization search */\nint minPathSumDFSMem(int grid[MAX_SIZE][MAX_SIZE], int mem[MAX_SIZE][MAX_SIZE], int i, int j) {\n    // If it's the top-left cell, terminate the search\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return INT_MAX;\n    }\n    // If there's a record, return it directly\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // Minimum path cost for left and upper cells\n    int up = minPathSumDFSMem(grid, mem, i - 1, j);\n    int left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // Record and return the minimum path cost from top-left to (i, j)\n    mem[i][j] = myMin(left, up) != INT_MAX ? myMin(left, up) + grid[i][j] : INT_MAX;\n    return mem[i][j];\n}\n
      min_path_sum.kt
      /* Minimum path sum: Memoization search */\nfun minPathSumDFSMem(\n    grid: Array<IntArray>,\n    mem: Array<IntArray>,\n    i: Int,\n    j: Int\n): Int {\n    // If it's the top-left cell, terminate the search\n    if (i == 0 && j == 0) {\n        return grid[0][0]\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return Int.MAX_VALUE\n    }\n    // If there's a record, return it directly\n    if (mem[i][j] != -1) {\n        return mem[i][j]\n    }\n    // Minimum path cost for left and upper cells\n    val up = minPathSumDFSMem(grid, mem, i - 1, j)\n    val left = minPathSumDFSMem(grid, mem, i, j - 1)\n    // Record and return the minimum path cost from top-left to (i, j)\n    mem[i][j] = min(left, up) + grid[i][j]\n    return mem[i][j]\n}\n
      min_path_sum.rb
      ### Minimum path sum: memoization search ###\ndef min_path_sum_dfs_mem(grid, mem, i, j)\n  # If it's the top-left cell, terminate the search\n  return grid[0][0] if i == 0 && j == 0\n  # If row or column index is out of bounds, return +\u221e cost\n  return Float::INFINITY if i < 0 || j < 0\n  # If there's a record, return it directly\n  return mem[i][j] if mem[i][j] != -1\n  # Minimum path cost for left and upper cells\n  up = min_path_sum_dfs_mem(grid, mem, i - 1, j)\n  left = min_path_sum_dfs_mem(grid, mem, i, j - 1)\n  # Record and return the minimum path cost from top-left to (i, j)\n  mem[i][j] = [left, up].min + grid[i][j]\nend\n

      As shown in Figure 14-15, after introducing memoization, all subproblem solutions only need to be computed once, so the time complexity depends on the total number of states, which is the grid size \\(O(nm)\\).

      Figure 14-15 \u00a0 Memoization recursion tree

      "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#3-method-3-dynamic-programming","title":"3. \u00a0 Method 3: Dynamic Programming","text":"

      Implement the dynamic programming solution based on iteration, as shown in the code below:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
      def min_path_sum_dp(grid: list[list[int]]) -> int:\n    \"\"\"Minimum path sum: Dynamic programming\"\"\"\n    n, m = len(grid), len(grid[0])\n    # Initialize dp table\n    dp = [[0] * m for _ in range(n)]\n    dp[0][0] = grid[0][0]\n    # State transition: first row\n    for j in range(1, m):\n        dp[0][j] = dp[0][j - 1] + grid[0][j]\n    # State transition: first column\n    for i in range(1, n):\n        dp[i][0] = dp[i - 1][0] + grid[i][0]\n    # State transition: rest of the rows and columns\n    for i in range(1, n):\n        for j in range(1, m):\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n    return dp[n - 1][m - 1]\n
      min_path_sum.cpp
      /* Minimum path sum: Dynamic programming */\nint minPathSumDP(vector<vector<int>> &grid) {\n    int n = grid.size(), m = grid[0].size();\n    // Initialize dp table\n    vector<vector<int>> dp(n, vector<int>(m));\n    dp[0][0] = grid[0][0];\n    // State transition: first row\n    for (int j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // State transition: first column\n    for (int i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
      min_path_sum.java
      /* Minimum path sum: Dynamic programming */\nint minPathSumDP(int[][] grid) {\n    int n = grid.length, m = grid[0].length;\n    // Initialize dp table\n    int[][] dp = new int[n][m];\n    dp[0][0] = grid[0][0];\n    // State transition: first row\n    for (int j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // State transition: first column\n    for (int i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
      min_path_sum.cs
      /* Minimum path sum: Dynamic programming */\nint MinPathSumDP(int[][] grid) {\n    int n = grid.Length, m = grid[0].Length;\n    // Initialize dp table\n    int[,] dp = new int[n, m];\n    dp[0, 0] = grid[0][0];\n    // State transition: first row\n    for (int j = 1; j < m; j++) {\n        dp[0, j] = dp[0, j - 1] + grid[0][j];\n    }\n    // State transition: first column\n    for (int i = 1; i < n; i++) {\n        dp[i, 0] = dp[i - 1, 0] + grid[i][0];\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i, j] = Math.Min(dp[i, j - 1], dp[i - 1, j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1, m - 1];\n}\n
      min_path_sum.go
      /* Minimum path sum: Dynamic programming */\nfunc minPathSumDP(grid [][]int) int {\n    n, m := len(grid), len(grid[0])\n    // Initialize dp table\n    dp := make([][]int, n)\n    for i := 0; i < n; i++ {\n        dp[i] = make([]int, m)\n    }\n    dp[0][0] = grid[0][0]\n    // State transition: first row\n    for j := 1; j < m; j++ {\n        dp[0][j] = dp[0][j-1] + grid[0][j]\n    }\n    // State transition: first column\n    for i := 1; i < n; i++ {\n        dp[i][0] = dp[i-1][0] + grid[i][0]\n    }\n    // State transition: rest of the rows and columns\n    for i := 1; i < n; i++ {\n        for j := 1; j < m; j++ {\n            dp[i][j] = int(math.Min(float64(dp[i][j-1]), float64(dp[i-1][j]))) + grid[i][j]\n        }\n    }\n    return dp[n-1][m-1]\n}\n
      min_path_sum.swift
      /* Minimum path sum: Dynamic programming */\nfunc minPathSumDP(grid: [[Int]]) -> Int {\n    let n = grid.count\n    let m = grid[0].count\n    // Initialize dp table\n    var dp = Array(repeating: Array(repeating: 0, count: m), count: n)\n    dp[0][0] = grid[0][0]\n    // State transition: first row\n    for j in 1 ..< m {\n        dp[0][j] = dp[0][j - 1] + grid[0][j]\n    }\n    // State transition: first column\n    for i in 1 ..< n {\n        dp[i][0] = dp[i - 1][0] + grid[i][0]\n    }\n    // State transition: rest of the rows and columns\n    for i in 1 ..< n {\n        for j in 1 ..< m {\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n        }\n    }\n    return dp[n - 1][m - 1]\n}\n
      min_path_sum.js
      /* Minimum path sum: Dynamic programming */\nfunction minPathSumDP(grid) {\n    const n = grid.length,\n        m = grid[0].length;\n    // Initialize dp table\n    const dp = Array.from({ length: n }, () =>\n        Array.from({ length: m }, () => 0)\n    );\n    dp[0][0] = grid[0][0];\n    // State transition: first row\n    for (let j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // State transition: first column\n    for (let i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // State transition: rest of the rows and columns\n    for (let i = 1; i < n; i++) {\n        for (let j = 1; j < m; j++) {\n            dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
      min_path_sum.ts
      /* Minimum path sum: Dynamic programming */\nfunction minPathSumDP(grid: Array<Array<number>>): number {\n    const n = grid.length,\n        m = grid[0].length;\n    // Initialize dp table\n    const dp = Array.from({ length: n }, () =>\n        Array.from({ length: m }, () => 0)\n    );\n    dp[0][0] = grid[0][0];\n    // State transition: first row\n    for (let j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // State transition: first column\n    for (let i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // State transition: rest of the rows and columns\n    for (let i = 1; i < n; i++) {\n        for (let j: number = 1; j < m; j++) {\n            dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
      min_path_sum.dart
      /* Minimum path sum: Dynamic programming */\nint minPathSumDP(List<List<int>> grid) {\n  int n = grid.length, m = grid[0].length;\n  // Initialize dp table\n  List<List<int>> dp = List.generate(n, (i) => List.filled(m, 0));\n  dp[0][0] = grid[0][0];\n  // State transition: first row\n  for (int j = 1; j < m; j++) {\n    dp[0][j] = dp[0][j - 1] + grid[0][j];\n  }\n  // State transition: first column\n  for (int i = 1; i < n; i++) {\n    dp[i][0] = dp[i - 1][0] + grid[i][0];\n  }\n  // State transition: rest of the rows and columns\n  for (int i = 1; i < n; i++) {\n    for (int j = 1; j < m; j++) {\n      dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n    }\n  }\n  return dp[n - 1][m - 1];\n}\n
      min_path_sum.rs
      /* Minimum path sum: Dynamic programming */\nfn min_path_sum_dp(grid: &Vec<Vec<i32>>) -> i32 {\n    let (n, m) = (grid.len(), grid[0].len());\n    // Initialize dp table\n    let mut dp = vec![vec![0; m]; n];\n    dp[0][0] = grid[0][0];\n    // State transition: first row\n    for j in 1..m {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // State transition: first column\n    for i in 1..n {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // State transition: rest of the rows and columns\n    for i in 1..n {\n        for j in 1..m {\n            dp[i][j] = std::cmp::min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    dp[n - 1][m - 1]\n}\n
      min_path_sum.c
      /* Minimum path sum: Dynamic programming */\nint minPathSumDP(int grid[MAX_SIZE][MAX_SIZE], int n, int m) {\n    // Initialize dp table\n    int **dp = malloc(n * sizeof(int *));\n    for (int i = 0; i < n; i++) {\n        dp[i] = calloc(m, sizeof(int));\n    }\n    dp[0][0] = grid[0][0];\n    // State transition: first row\n    for (int j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // State transition: first column\n    for (int i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i][j] = myMin(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    int res = dp[n - 1][m - 1];\n    // Free memory\n    for (int i = 0; i < n; i++) {\n        free(dp[i]);\n    }\n    return res;\n}\n
      min_path_sum.kt
      /* Minimum path sum: Dynamic programming */\nfun minPathSumDP(grid: Array<IntArray>): Int {\n    val n = grid.size\n    val m = grid[0].size\n    // Initialize dp table\n    val dp = Array(n) { IntArray(m) }\n    dp[0][0] = grid[0][0]\n    // State transition: first row\n    for (j in 1..<m) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j]\n    }\n    // State transition: first column\n    for (i in 1..<n) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0]\n    }\n    // State transition: rest of the rows and columns\n    for (i in 1..<n) {\n        for (j in 1..<m) {\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n        }\n    }\n    return dp[n - 1][m - 1]\n}\n
      min_path_sum.rb
      ### Minimum path sum: dynamic programming ###\ndef min_path_sum_dp(grid)\n  n, m = grid.length, grid.first.length\n  # Initialize dp table\n  dp = Array.new(n) { Array.new(m, 0) }\n  dp[0][0] = grid[0][0]\n  # State transition: first row\n  (1...m).each { |j| dp[0][j] = dp[0][j - 1] + grid[0][j] }\n  # State transition: first column\n  (1...n).each { |i| dp[i][0] = dp[i - 1][0] + grid[i][0] }\n  # State transition: rest of the rows and columns\n  for i in 1...n\n    for j in 1...m\n      dp[i][j] = [dp[i][j - 1], dp[i - 1][j]].min + grid[i][j]\n    end\n  end\n  dp[n -1][m -1]\nend\n

      Figure 14-16 shows the state transition process for minimum path sum, which traverses the entire grid, thus the time complexity is \\(O(nm)\\).

      The array dp has size \\(n \\times m\\), thus the space complexity is \\(O(nm)\\).

      <1><2><3><4><5><6><7><8><9><10><11><12>

      Figure 14-16 \u00a0 Dynamic programming process for minimum path sum

      "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#4-space-optimization","title":"4. \u00a0 Space Optimization","text":"

      Since each cell is only related to the cell to its left and the cell above it, we can use a single-row array to implement the \\(dp\\) table.

      Note that since the array dp can only represent the state of one row, we cannot initialize the first column state in advance, but rather update it when traversing each row:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
      def min_path_sum_dp_comp(grid: list[list[int]]) -> int:\n    \"\"\"Minimum path sum: Space-optimized dynamic programming\"\"\"\n    n, m = len(grid), len(grid[0])\n    # Initialize dp table\n    dp = [0] * m\n    # State transition: first row\n    dp[0] = grid[0][0]\n    for j in range(1, m):\n        dp[j] = dp[j - 1] + grid[0][j]\n    # State transition: rest of the rows\n    for i in range(1, n):\n        # State transition: first column\n        dp[0] = dp[0] + grid[i][0]\n        # State transition: rest of the columns\n        for j in range(1, m):\n            dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n    return dp[m - 1]\n
      min_path_sum.cpp
      /* Minimum path sum: Space-optimized dynamic programming */\nint minPathSumDPComp(vector<vector<int>> &grid) {\n    int n = grid.size(), m = grid[0].size();\n    // Initialize dp table\n    vector<int> dp(m);\n    // State transition: first row\n    dp[0] = grid[0][0];\n    for (int j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // State transition: rest of the rows\n    for (int i = 1; i < n; i++) {\n        // State transition: first column\n        dp[0] = dp[0] + grid[i][0];\n        // State transition: rest of the columns\n        for (int j = 1; j < m; j++) {\n            dp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
      min_path_sum.java
      /* Minimum path sum: Space-optimized dynamic programming */\nint minPathSumDPComp(int[][] grid) {\n    int n = grid.length, m = grid[0].length;\n    // Initialize dp table\n    int[] dp = new int[m];\n    // State transition: first row\n    dp[0] = grid[0][0];\n    for (int j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // State transition: rest of the rows\n    for (int i = 1; i < n; i++) {\n        // State transition: first column\n        dp[0] = dp[0] + grid[i][0];\n        // State transition: rest of the columns\n        for (int j = 1; j < m; j++) {\n            dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
      min_path_sum.cs
      /* Minimum path sum: Space-optimized dynamic programming */\nint MinPathSumDPComp(int[][] grid) {\n    int n = grid.Length, m = grid[0].Length;\n    // Initialize dp table\n    int[] dp = new int[m];\n    dp[0] = grid[0][0];\n    // State transition: first row\n    for (int j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // State transition: rest of the rows\n    for (int i = 1; i < n; i++) {\n        // State transition: first column\n        dp[0] = dp[0] + grid[i][0];\n        // State transition: rest of the columns\n        for (int j = 1; j < m; j++) {\n            dp[j] = Math.Min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
      min_path_sum.go
      /* Minimum path sum: Space-optimized dynamic programming */\nfunc minPathSumDPComp(grid [][]int) int {\n    n, m := len(grid), len(grid[0])\n    // Initialize dp table\n    dp := make([]int, m)\n    // State transition: first row\n    dp[0] = grid[0][0]\n    for j := 1; j < m; j++ {\n        dp[j] = dp[j-1] + grid[0][j]\n    }\n    // State transition: rest of the rows and columns\n    for i := 1; i < n; i++ {\n        // State transition: first column\n        dp[0] = dp[0] + grid[i][0]\n        // State transition: rest of the columns\n        for j := 1; j < m; j++ {\n            dp[j] = int(math.Min(float64(dp[j-1]), float64(dp[j]))) + grid[i][j]\n        }\n    }\n    return dp[m-1]\n}\n
      min_path_sum.swift
      /* Minimum path sum: Space-optimized dynamic programming */\nfunc minPathSumDPComp(grid: [[Int]]) -> Int {\n    let n = grid.count\n    let m = grid[0].count\n    // Initialize dp table\n    var dp = Array(repeating: 0, count: m)\n    // State transition: first row\n    dp[0] = grid[0][0]\n    for j in 1 ..< m {\n        dp[j] = dp[j - 1] + grid[0][j]\n    }\n    // State transition: rest of the rows\n    for i in 1 ..< n {\n        // State transition: first column\n        dp[0] = dp[0] + grid[i][0]\n        // State transition: rest of the columns\n        for j in 1 ..< m {\n            dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n        }\n    }\n    return dp[m - 1]\n}\n
      min_path_sum.js
      /* Minimum path sum: Space-optimized dynamic programming */\nfunction minPathSumDPComp(grid) {\n    const n = grid.length,\n        m = grid[0].length;\n    // Initialize dp table\n    const dp = new Array(m);\n    // State transition: first row\n    dp[0] = grid[0][0];\n    for (let j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // State transition: rest of the rows\n    for (let i = 1; i < n; i++) {\n        // State transition: first column\n        dp[0] = dp[0] + grid[i][0];\n        // State transition: rest of the columns\n        for (let j = 1; j < m; j++) {\n            dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
      min_path_sum.ts
      /* Minimum path sum: Space-optimized dynamic programming */\nfunction minPathSumDPComp(grid: Array<Array<number>>): number {\n    const n = grid.length,\n        m = grid[0].length;\n    // Initialize dp table\n    const dp = new Array(m);\n    // State transition: first row\n    dp[0] = grid[0][0];\n    for (let j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // State transition: rest of the rows\n    for (let i = 1; i < n; i++) {\n        // State transition: first column\n        dp[0] = dp[0] + grid[i][0];\n        // State transition: rest of the columns\n        for (let j = 1; j < m; j++) {\n            dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
      min_path_sum.dart
      /* Minimum path sum: Space-optimized dynamic programming */\nint minPathSumDPComp(List<List<int>> grid) {\n  int n = grid.length, m = grid[0].length;\n  // Initialize dp table\n  List<int> dp = List.filled(m, 0);\n  dp[0] = grid[0][0];\n  for (int j = 1; j < m; j++) {\n    dp[j] = dp[j - 1] + grid[0][j];\n  }\n  // State transition: rest of the rows\n  for (int i = 1; i < n; i++) {\n    // State transition: first column\n    dp[0] = dp[0] + grid[i][0];\n    // State transition: rest of the columns\n    for (int j = 1; j < m; j++) {\n      dp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n    }\n  }\n  return dp[m - 1];\n}\n
      min_path_sum.rs
      /* Minimum path sum: Space-optimized dynamic programming */\nfn min_path_sum_dp_comp(grid: &Vec<Vec<i32>>) -> i32 {\n    let (n, m) = (grid.len(), grid[0].len());\n    // Initialize dp table\n    let mut dp = vec![0; m];\n    // State transition: first row\n    dp[0] = grid[0][0];\n    for j in 1..m {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // State transition: rest of the rows\n    for i in 1..n {\n        // State transition: first column\n        dp[0] = dp[0] + grid[i][0];\n        // State transition: rest of the columns\n        for j in 1..m {\n            dp[j] = std::cmp::min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    dp[m - 1]\n}\n
      min_path_sum.c
      /* Minimum path sum: Space-optimized dynamic programming */\nint minPathSumDPComp(int grid[MAX_SIZE][MAX_SIZE], int n, int m) {\n    // Initialize dp table\n    int *dp = calloc(m, sizeof(int));\n    // State transition: first row\n    dp[0] = grid[0][0];\n    for (int j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // State transition: rest of the rows\n    for (int i = 1; i < n; i++) {\n        // State transition: first column\n        dp[0] = dp[0] + grid[i][0];\n        // State transition: rest of the columns\n        for (int j = 1; j < m; j++) {\n            dp[j] = myMin(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    int res = dp[m - 1];\n    // Free memory\n    free(dp);\n    return res;\n}\n
      min_path_sum.kt
      /* Minimum path sum: Space-optimized dynamic programming */\nfun minPathSumDPComp(grid: Array<IntArray>): Int {\n    val n = grid.size\n    val m = grid[0].size\n    // Initialize dp table\n    val dp = IntArray(m)\n    // State transition: first row\n    dp[0] = grid[0][0]\n    for (j in 1..<m) {\n        dp[j] = dp[j - 1] + grid[0][j]\n    }\n    // State transition: rest of the rows\n    for (i in 1..<n) {\n        // State transition: first column\n        dp[0] = dp[0] + grid[i][0]\n        // State transition: rest of the columns\n        for (j in 1..<m) {\n            dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n        }\n    }\n    return dp[m - 1]\n}\n
      min_path_sum.rb
      ### Minimum path sum: space-optimized DP ###\ndef min_path_sum_dp_comp(grid)\n  n, m = grid.length, grid.first.length\n  # Initialize dp table\n  dp = Array.new(m, 0)\n  # State transition: first row\n  dp[0] = grid[0][0]\n  (1...m).each { |j| dp[j] = dp[j - 1] + grid[0][j] }\n  # State transition: rest of the rows\n  for i in 1...n\n    # State transition: first column\n    dp[0] = dp[0] + grid[i][0]\n    # State transition: rest of the columns\n    (1...m).each { |j| dp[j] = [dp[j - 1], dp[j]].min + grid[i][j] }\n  end\n  dp[m - 1]\nend\n
      "},{"location":"chapter_dynamic_programming/edit_distance_problem/","title":"14.6 \u00a0 Edit Distance Problem","text":"

      Edit distance, also known as Levenshtein distance, refers to the minimum number of edits required to transform one string into another, commonly used in information retrieval and natural language processing to measure the similarity between two sequences.

      Question

      Given two strings \\(s\\) and \\(t\\), return the minimum number of edits required to transform \\(s\\) into \\(t\\).

      You can perform three types of edit operations on a string: insert a character, delete a character, or replace a character with any other character.

      As shown in Figure 14-27, transforming kitten into sitting requires 3 edits, including 2 replacements and 1 insertion; transforming hello into algo requires 3 steps, including 2 replacements and 1 deletion.

      Figure 14-27 \u00a0 Example data for edit distance

      The edit distance problem can be naturally explained using the decision tree model. Strings correspond to tree nodes, and a round of decision (one edit operation) corresponds to an edge of the tree.

      As shown in Figure 14-28, without restricting operations, each node can branch into many edges, with each edge corresponding to one operation, meaning there are many possible paths to transform hello into algo.

      From the perspective of the decision tree, the goal of this problem is to find the shortest path between node hello and node algo.

      Figure 14-28 \u00a0 Representing edit distance problem based on decision tree model

      "},{"location":"chapter_dynamic_programming/edit_distance_problem/#1-dynamic-programming-approach","title":"1. \u00a0 Dynamic Programming Approach","text":"

      Step 1: Think about the decisions in each round, define the state, and thus obtain the \\(dp\\) table

      Each round of decision involves performing one edit operation on string \\(s\\).

      We want the problem scale to gradually decrease during the editing process, which allows us to construct subproblems. Let the lengths of strings \\(s\\) and \\(t\\) be \\(n\\) and \\(m\\) respectively. We first consider the tail characters of the two strings, \\(s[n-1]\\) and \\(t[m-1]\\).

      • If \\(s[n-1]\\) and \\(t[m-1]\\) are the same, we can skip them and directly consider \\(s[n-2]\\) and \\(t[m-2]\\).
      • If \\(s[n-1]\\) and \\(t[m-1]\\) are different, we need to perform one edit on \\(s\\) (insert, delete, or replace) to make the tail characters of the two strings the same, allowing us to skip them and consider a smaller-scale problem.

      In other words, each round of decision (edit operation) we make on string \\(s\\) will change the remaining characters to be matched in \\(s\\) and \\(t\\). Therefore, the state is the \\(i\\)-th and \\(j\\)-th characters currently being considered in \\(s\\) and \\(t\\), denoted as \\([i, j]\\).

      State \\([i, j]\\) corresponds to the subproblem: the minimum number of edits required to change the first \\(i\\) characters of \\(s\\) into the first \\(j\\) characters of \\(t\\).

      From this, we obtain a two-dimensional \\(dp\\) table of size \\((i+1) \\times (j+1)\\).

      Step 2: Identify the optimal substructure, and then derive the state transition equation

      Consider subproblem \\(dp[i, j]\\), where the tail characters of the corresponding two strings are \\(s[i-1]\\) and \\(t[j-1]\\), which can be divided into the three cases shown in Figure 14-29 based on different edit operations.

      1. Insert \\(t[j-1]\\) after \\(s[i-1]\\), then the remaining subproblem is \\(dp[i, j-1]\\).
      2. Delete \\(s[i-1]\\), then the remaining subproblem is \\(dp[i-1, j]\\).
      3. Replace \\(s[i-1]\\) with \\(t[j-1]\\), then the remaining subproblem is \\(dp[i-1, j-1]\\).

      Figure 14-29 \u00a0 State transition for edit distance

      Based on the above analysis, the optimal substructure can be obtained: the minimum number of edits for \\(dp[i, j]\\) equals the minimum among the minimum edit steps of \\(dp[i, j-1]\\), \\(dp[i-1, j]\\), and \\(dp[i-1, j-1]\\), plus the edit step \\(1\\) for this time. The corresponding state transition equation is:

      \\[ dp[i, j] = \\min(dp[i, j-1], dp[i-1, j], dp[i-1, j-1]) + 1 \\]

      Please note that when \\(s[i-1]\\) and \\(t[j-1]\\) are the same, no edit is required for the current character, in which case the state transition equation is:

      \\[ dp[i, j] = dp[i-1, j-1] \\]

      Step 3: Determine boundary conditions and state transition order

      When both strings are empty, the number of edit steps is \\(0\\), i.e., \\(dp[0, 0] = 0\\). When \\(s\\) is empty but \\(t\\) is not, the minimum number of edit steps equals the length of \\(t\\), i.e., the first row \\(dp[0, j] = j\\). When \\(s\\) is not empty but \\(t\\) is empty, the minimum number of edit steps equals the length of \\(s\\), i.e., the first column \\(dp[i, 0] = i\\).

      Observing the state transition equation, the solution \\(dp[i, j]\\) depends on solutions to the left, above, and upper-left, so the entire \\(dp\\) table can be traversed in order through two nested loops.

      "},{"location":"chapter_dynamic_programming/edit_distance_problem/#2-code-implementation","title":"2. \u00a0 Code Implementation","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby edit_distance.py
      def edit_distance_dp(s: str, t: str) -> int:\n    \"\"\"Edit distance: Dynamic programming\"\"\"\n    n, m = len(s), len(t)\n    dp = [[0] * (m + 1) for _ in range(n + 1)]\n    # State transition: first row and first column\n    for i in range(1, n + 1):\n        dp[i][0] = i\n    for j in range(1, m + 1):\n        dp[0][j] = j\n    # State transition: rest of the rows and columns\n    for i in range(1, n + 1):\n        for j in range(1, m + 1):\n            if s[i - 1] == t[j - 1]:\n                # If two characters are equal, skip both characters\n                dp[i][j] = dp[i - 1][j - 1]\n            else:\n                # Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1\n    return dp[n][m]\n
      edit_distance.cpp
      /* Edit distance: Dynamic programming */\nint editDistanceDP(string s, string t) {\n    int n = s.length(), m = t.length();\n    vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));\n    // State transition: first row and first column\n    for (int i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s[i - 1] == t[j - 1]) {\n                // If two characters are equal, skip both characters\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
      edit_distance.java
      /* Edit distance: Dynamic programming */\nint editDistanceDP(String s, String t) {\n    int n = s.length(), m = t.length();\n    int[][] dp = new int[n + 1][m + 1];\n    // State transition: first row and first column\n    for (int i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s.charAt(i - 1) == t.charAt(j - 1)) {\n                // If two characters are equal, skip both characters\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
      edit_distance.cs
      /* Edit distance: Dynamic programming */\nint EditDistanceDP(string s, string t) {\n    int n = s.Length, m = t.Length;\n    int[,] dp = new int[n + 1, m + 1];\n    // State transition: first row and first column\n    for (int i = 1; i <= n; i++) {\n        dp[i, 0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0, j] = j;\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s[i - 1] == t[j - 1]) {\n                // If two characters are equal, skip both characters\n                dp[i, j] = dp[i - 1, j - 1];\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[i, j] = Math.Min(Math.Min(dp[i, j - 1], dp[i - 1, j]), dp[i - 1, j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n, m];\n}\n
      edit_distance.go
      /* Edit distance: Dynamic programming */\nfunc editDistanceDP(s string, t string) int {\n    n := len(s)\n    m := len(t)\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, m+1)\n    }\n    // State transition: first row and first column\n    for i := 1; i <= n; i++ {\n        dp[i][0] = i\n    }\n    for j := 1; j <= m; j++ {\n        dp[0][j] = j\n    }\n    // State transition: rest of the rows and columns\n    for i := 1; i <= n; i++ {\n        for j := 1; j <= m; j++ {\n            if s[i-1] == t[j-1] {\n                // If two characters are equal, skip both characters\n                dp[i][j] = dp[i-1][j-1]\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[i][j] = MinInt(MinInt(dp[i][j-1], dp[i-1][j]), dp[i-1][j-1]) + 1\n            }\n        }\n    }\n    return dp[n][m]\n}\n
      edit_distance.swift
      /* Edit distance: Dynamic programming */\nfunc editDistanceDP(s: String, t: String) -> Int {\n    let n = s.utf8CString.count\n    let m = t.utf8CString.count\n    var dp = Array(repeating: Array(repeating: 0, count: m + 1), count: n + 1)\n    // State transition: first row and first column\n    for i in 1 ... n {\n        dp[i][0] = i\n    }\n    for j in 1 ... m {\n        dp[0][j] = j\n    }\n    // State transition: rest of the rows and columns\n    for i in 1 ... n {\n        for j in 1 ... m {\n            if s.utf8CString[i - 1] == t.utf8CString[j - 1] {\n                // If two characters are equal, skip both characters\n                dp[i][j] = dp[i - 1][j - 1]\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1\n            }\n        }\n    }\n    return dp[n][m]\n}\n
      edit_distance.js
      /* Edit distance: Dynamic programming */\nfunction editDistanceDP(s, t) {\n    const n = s.length,\n        m = t.length;\n    const dp = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));\n    // State transition: first row and first column\n    for (let i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (let j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // State transition: rest of the rows and columns\n    for (let i = 1; i <= n; i++) {\n        for (let j = 1; j <= m; j++) {\n            if (s.charAt(i - 1) === t.charAt(j - 1)) {\n                // If two characters are equal, skip both characters\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[i][j] =\n                    Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
      edit_distance.ts
      /* Edit distance: Dynamic programming */\nfunction editDistanceDP(s: string, t: string): number {\n    const n = s.length,\n        m = t.length;\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: m + 1 }, () => 0)\n    );\n    // State transition: first row and first column\n    for (let i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (let j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // State transition: rest of the rows and columns\n    for (let i = 1; i <= n; i++) {\n        for (let j = 1; j <= m; j++) {\n            if (s.charAt(i - 1) === t.charAt(j - 1)) {\n                // If two characters are equal, skip both characters\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[i][j] =\n                    Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
      edit_distance.dart
      /* Edit distance: Dynamic programming */\nint editDistanceDP(String s, String t) {\n  int n = s.length, m = t.length;\n  List<List<int>> dp = List.generate(n + 1, (_) => List.filled(m + 1, 0));\n  // State transition: first row and first column\n  for (int i = 1; i <= n; i++) {\n    dp[i][0] = i;\n  }\n  for (int j = 1; j <= m; j++) {\n    dp[0][j] = j;\n  }\n  // State transition: rest of the rows and columns\n  for (int i = 1; i <= n; i++) {\n    for (int j = 1; j <= m; j++) {\n      if (s[i - 1] == t[j - 1]) {\n        // If two characters are equal, skip both characters\n        dp[i][j] = dp[i - 1][j - 1];\n      } else {\n        // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n        dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n      }\n    }\n  }\n  return dp[n][m];\n}\n
      edit_distance.rs
      /* Edit distance: Dynamic programming */\nfn edit_distance_dp(s: &str, t: &str) -> i32 {\n    let (n, m) = (s.len(), t.len());\n    let mut dp = vec![vec![0; m + 1]; n + 1];\n    // State transition: first row and first column\n    for i in 1..=n {\n        dp[i][0] = i as i32;\n    }\n    for j in 1..m {\n        dp[0][j] = j as i32;\n    }\n    // State transition: rest of the rows and columns\n    for i in 1..=n {\n        for j in 1..=m {\n            if s.chars().nth(i - 1) == t.chars().nth(j - 1) {\n                // If two characters are equal, skip both characters\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[i][j] =\n                    std::cmp::min(std::cmp::min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    dp[n][m]\n}\n
      edit_distance.c
      /* Edit distance: Dynamic programming */\nint editDistanceDP(char *s, char *t, int n, int m) {\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(m + 1, sizeof(int));\n    }\n    // State transition: first row and first column\n    for (int i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s[i - 1] == t[j - 1]) {\n                // If two characters are equal, skip both characters\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[i][j] = myMin(myMin(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    int res = dp[n][m];\n    // Free memory\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    return res;\n}\n
      edit_distance.kt
      /* Edit distance: Dynamic programming */\nfun editDistanceDP(s: String, t: String): Int {\n    val n = s.length\n    val m = t.length\n    val dp = Array(n + 1) { IntArray(m + 1) }\n    // State transition: first row and first column\n    for (i in 1..n) {\n        dp[i][0] = i\n    }\n    for (j in 1..m) {\n        dp[0][j] = j\n    }\n    // State transition: rest of the rows and columns\n    for (i in 1..n) {\n        for (j in 1..m) {\n            if (s[i - 1] == t[j - 1]) {\n                // If two characters are equal, skip both characters\n                dp[i][j] = dp[i - 1][j - 1]\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1\n            }\n        }\n    }\n    return dp[n][m]\n}\n
      edit_distance.rb
      ### Edit distance: dynamic programming ###\ndef edit_distance_dp(s, t)\n  n, m = s.length, t.length\n  dp = Array.new(n + 1) { Array.new(m + 1, 0) }\n  # State transition: first row and first column\n  (1...(n + 1)).each { |i| dp[i][0] = i }\n  (1...(m + 1)).each { |j| dp[0][j] = j }\n  # State transition: rest of the rows and columns\n  for i in 1...(n + 1)\n    for j in 1...(m +1)\n      if s[i - 1] == t[j - 1]\n        # If two characters are equal, skip both characters\n        dp[i][j] = dp[i - 1][j - 1]\n      else\n        # Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n        dp[i][j] = [dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]].min + 1\n      end\n    end\n  end\n  dp[n][m]\nend\n

      As shown in Figure 14-30, the state transition process for the edit distance problem is very similar to the knapsack problem and can both be viewed as the process of filling a two-dimensional grid.

      <1><2><3><4><5><6><7><8><9><10><11><12><13><14><15>

      Figure 14-30 \u00a0 Dynamic programming process for edit distance

      "},{"location":"chapter_dynamic_programming/edit_distance_problem/#3-space-optimization","title":"3. \u00a0 Space Optimization","text":"

      Since \\(dp[i, j]\\) is transferred from the solutions above \\(dp[i-1, j]\\), to the left \\(dp[i, j-1]\\), and to the upper-left \\(dp[i-1, j-1]\\), forward traversal will lose the upper-left solution \\(dp[i-1, j-1]\\), and reverse traversal cannot build \\(dp[i, j-1]\\) in advance, so neither traversal order is feasible.

      For this reason, we can use a variable leftup to temporarily store the upper-left solution \\(dp[i-1, j-1]\\), so we only need to consider the solutions to the left and above. This situation is the same as the unbounded knapsack problem, allowing for forward traversal. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby edit_distance.py
      def edit_distance_dp_comp(s: str, t: str) -> int:\n    \"\"\"Edit distance: Space-optimized dynamic programming\"\"\"\n    n, m = len(s), len(t)\n    dp = [0] * (m + 1)\n    # State transition: first row\n    for j in range(1, m + 1):\n        dp[j] = j\n    # State transition: rest of the rows\n    for i in range(1, n + 1):\n        # State transition: first column\n        leftup = dp[0]  # Temporarily store dp[i-1, j-1]\n        dp[0] += 1\n        # State transition: rest of the columns\n        for j in range(1, m + 1):\n            temp = dp[j]\n            if s[i - 1] == t[j - 1]:\n                # If two characters are equal, skip both characters\n                dp[j] = leftup\n            else:\n                # Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[j] = min(dp[j - 1], dp[j], leftup) + 1\n            leftup = temp  # Update for next round's dp[i-1, j-1]\n    return dp[m]\n
      edit_distance.cpp
      /* Edit distance: Space-optimized dynamic programming */\nint editDistanceDPComp(string s, string t) {\n    int n = s.length(), m = t.length();\n    vector<int> dp(m + 1, 0);\n    // State transition: first row\n    for (int j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // State transition: rest of the rows\n    for (int i = 1; i <= n; i++) {\n        // State transition: first column\n        int leftup = dp[0]; // Temporarily store dp[i-1, j-1]\n        dp[0] = i;\n        // State transition: rest of the columns\n        for (int j = 1; j <= m; j++) {\n            int temp = dp[j];\n            if (s[i - 1] == t[j - 1]) {\n                // If two characters are equal, skip both characters\n                dp[j] = leftup;\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // Update for next round's dp[i-1, j-1]\n        }\n    }\n    return dp[m];\n}\n
      edit_distance.java
      /* Edit distance: Space-optimized dynamic programming */\nint editDistanceDPComp(String s, String t) {\n    int n = s.length(), m = t.length();\n    int[] dp = new int[m + 1];\n    // State transition: first row\n    for (int j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // State transition: rest of the rows\n    for (int i = 1; i <= n; i++) {\n        // State transition: first column\n        int leftup = dp[0]; // Temporarily store dp[i-1, j-1]\n        dp[0] = i;\n        // State transition: rest of the columns\n        for (int j = 1; j <= m; j++) {\n            int temp = dp[j];\n            if (s.charAt(i - 1) == t.charAt(j - 1)) {\n                // If two characters are equal, skip both characters\n                dp[j] = leftup;\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // Update for next round's dp[i-1, j-1]\n        }\n    }\n    return dp[m];\n}\n
      edit_distance.cs
      /* Edit distance: Space-optimized dynamic programming */\nint EditDistanceDPComp(string s, string t) {\n    int n = s.Length, m = t.Length;\n    int[] dp = new int[m + 1];\n    // State transition: first row\n    for (int j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // State transition: rest of the rows\n    for (int i = 1; i <= n; i++) {\n        // State transition: first column\n        int leftup = dp[0]; // Temporarily store dp[i-1, j-1]\n        dp[0] = i;\n        // State transition: rest of the columns\n        for (int j = 1; j <= m; j++) {\n            int temp = dp[j];\n            if (s[i - 1] == t[j - 1]) {\n                // If two characters are equal, skip both characters\n                dp[j] = leftup;\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[j] = Math.Min(Math.Min(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // Update for next round's dp[i-1, j-1]\n        }\n    }\n    return dp[m];\n}\n
      edit_distance.go
      /* Edit distance: Space-optimized dynamic programming */\nfunc editDistanceDPComp(s string, t string) int {\n    n := len(s)\n    m := len(t)\n    dp := make([]int, m+1)\n    // State transition: first row\n    for j := 1; j <= m; j++ {\n        dp[j] = j\n    }\n    // State transition: rest of the rows\n    for i := 1; i <= n; i++ {\n        // State transition: first column\n        leftUp := dp[0] // Temporarily store dp[i-1, j-1]\n        dp[0] = i\n        // State transition: rest of the columns\n        for j := 1; j <= m; j++ {\n            temp := dp[j]\n            if s[i-1] == t[j-1] {\n                // If two characters are equal, skip both characters\n                dp[j] = leftUp\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[j] = MinInt(MinInt(dp[j-1], dp[j]), leftUp) + 1\n            }\n            leftUp = temp // Update for next round's dp[i-1, j-1]\n        }\n    }\n    return dp[m]\n}\n
      edit_distance.swift
      /* Edit distance: Space-optimized dynamic programming */\nfunc editDistanceDPComp(s: String, t: String) -> Int {\n    let n = s.utf8CString.count\n    let m = t.utf8CString.count\n    var dp = Array(repeating: 0, count: m + 1)\n    // State transition: first row\n    for j in 1 ... m {\n        dp[j] = j\n    }\n    // State transition: rest of the rows\n    for i in 1 ... n {\n        // State transition: first column\n        var leftup = dp[0] // Temporarily store dp[i-1, j-1]\n        dp[0] = i\n        // State transition: rest of the columns\n        for j in 1 ... m {\n            let temp = dp[j]\n            if s.utf8CString[i - 1] == t.utf8CString[j - 1] {\n                // If two characters are equal, skip both characters\n                dp[j] = leftup\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1\n            }\n            leftup = temp // Update for next round's dp[i-1, j-1]\n        }\n    }\n    return dp[m]\n}\n
      edit_distance.js
      /* Edit distance: Space-optimized dynamic programming */\nfunction editDistanceDPComp(s, t) {\n    const n = s.length,\n        m = t.length;\n    const dp = new Array(m + 1).fill(0);\n    // State transition: first row\n    for (let j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // State transition: rest of the rows\n    for (let i = 1; i <= n; i++) {\n        // State transition: first column\n        let leftup = dp[0]; // Temporarily store dp[i-1, j-1]\n        dp[0] = i;\n        // State transition: rest of the columns\n        for (let j = 1; j <= m; j++) {\n            const temp = dp[j];\n            if (s.charAt(i - 1) === t.charAt(j - 1)) {\n                // If two characters are equal, skip both characters\n                dp[j] = leftup;\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[j] = Math.min(dp[j - 1], dp[j], leftup) + 1;\n            }\n            leftup = temp; // Update for next round's dp[i-1, j-1]\n        }\n    }\n    return dp[m];\n}\n
      edit_distance.ts
      /* Edit distance: Space-optimized dynamic programming */\nfunction editDistanceDPComp(s: string, t: string): number {\n    const n = s.length,\n        m = t.length;\n    const dp = new Array(m + 1).fill(0);\n    // State transition: first row\n    for (let j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // State transition: rest of the rows\n    for (let i = 1; i <= n; i++) {\n        // State transition: first column\n        let leftup = dp[0]; // Temporarily store dp[i-1, j-1]\n        dp[0] = i;\n        // State transition: rest of the columns\n        for (let j = 1; j <= m; j++) {\n            const temp = dp[j];\n            if (s.charAt(i - 1) === t.charAt(j - 1)) {\n                // If two characters are equal, skip both characters\n                dp[j] = leftup;\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[j] = Math.min(dp[j - 1], dp[j], leftup) + 1;\n            }\n            leftup = temp; // Update for next round's dp[i-1, j-1]\n        }\n    }\n    return dp[m];\n}\n
      edit_distance.dart
      /* Edit distance: Space-optimized dynamic programming */\nint editDistanceDPComp(String s, String t) {\n  int n = s.length, m = t.length;\n  List<int> dp = List.filled(m + 1, 0);\n  // State transition: first row\n  for (int j = 1; j <= m; j++) {\n    dp[j] = j;\n  }\n  // State transition: rest of the rows\n  for (int i = 1; i <= n; i++) {\n    // State transition: first column\n    int leftup = dp[0]; // Temporarily store dp[i-1, j-1]\n    dp[0] = i;\n    // State transition: rest of the columns\n    for (int j = 1; j <= m; j++) {\n      int temp = dp[j];\n      if (s[i - 1] == t[j - 1]) {\n        // If two characters are equal, skip both characters\n        dp[j] = leftup;\n      } else {\n        // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n        dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n      }\n      leftup = temp; // Update for next round's dp[i-1, j-1]\n    }\n  }\n  return dp[m];\n}\n
      edit_distance.rs
      /* Edit distance: Space-optimized dynamic programming */\nfn edit_distance_dp_comp(s: &str, t: &str) -> i32 {\n    let (n, m) = (s.len(), t.len());\n    let mut dp = vec![0; m + 1];\n    // State transition: first row\n    for j in 1..m {\n        dp[j] = j as i32;\n    }\n    // State transition: rest of the rows\n    for i in 1..=n {\n        // State transition: first column\n        let mut leftup = dp[0]; // Temporarily store dp[i-1, j-1]\n        dp[0] = i as i32;\n        // State transition: rest of the columns\n        for j in 1..=m {\n            let temp = dp[j];\n            if s.chars().nth(i - 1) == t.chars().nth(j - 1) {\n                // If two characters are equal, skip both characters\n                dp[j] = leftup;\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[j] = std::cmp::min(std::cmp::min(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // Update for next round's dp[i-1, j-1]\n        }\n    }\n    dp[m]\n}\n
      edit_distance.c
      /* Edit distance: Space-optimized dynamic programming */\nint editDistanceDPComp(char *s, char *t, int n, int m) {\n    int *dp = calloc(m + 1, sizeof(int));\n    // State transition: first row\n    for (int j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // State transition: rest of the rows\n    for (int i = 1; i <= n; i++) {\n        // State transition: first column\n        int leftup = dp[0]; // Temporarily store dp[i-1, j-1]\n        dp[0] = i;\n        // State transition: rest of the columns\n        for (int j = 1; j <= m; j++) {\n            int temp = dp[j];\n            if (s[i - 1] == t[j - 1]) {\n                // If two characters are equal, skip both characters\n                dp[j] = leftup;\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[j] = myMin(myMin(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // Update for next round's dp[i-1, j-1]\n        }\n    }\n    int res = dp[m];\n    // Free memory\n    free(dp);\n    return res;\n}\n
      edit_distance.kt
      /* Edit distance: Space-optimized dynamic programming */\nfun editDistanceDPComp(s: String, t: String): Int {\n    val n = s.length\n    val m = t.length\n    val dp = IntArray(m + 1)\n    // State transition: first row\n    for (j in 1..m) {\n        dp[j] = j\n    }\n    // State transition: rest of the rows\n    for (i in 1..n) {\n        // State transition: first column\n        var leftup = dp[0] // Temporarily store dp[i-1, j-1]\n        dp[0] = i\n        // State transition: rest of the columns\n        for (j in 1..m) {\n            val temp = dp[j]\n            if (s[i - 1] == t[j - 1]) {\n                // If two characters are equal, skip both characters\n                dp[j] = leftup\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1\n            }\n            leftup = temp // Update for next round's dp[i-1, j-1]\n        }\n    }\n    return dp[m]\n}\n
      edit_distance.rb
      ### Edit distance: space-optimized DP ###\ndef edit_distance_dp_comp(s, t)\n  n, m = s.length, t.length\n  dp = Array.new(m + 1, 0)\n  # State transition: first row\n  (1...(m + 1)).each { |j| dp[j] = j }\n  # State transition: rest of the rows\n  for i in 1...(n + 1)\n    # State transition: first column\n    leftup = dp.first # Temporarily store dp[i-1, j-1]\n    dp[0] += 1\n    # State transition: rest of the columns\n    for j in 1...(m + 1)\n      temp = dp[j]\n      if s[i - 1] == t[j - 1]\n        # If two characters are equal, skip both characters\n        dp[j] = leftup\n      else\n        # Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n        dp[j] = [dp[j - 1], dp[j], leftup].min + 1\n      end\n      leftup = temp # Update for next round's dp[i-1, j-1]\n    end\n  end\n  dp[m]\nend\n
      "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/","title":"14.1 \u00a0 Introduction to Dynamic Programming","text":"

      Dynamic programming is an important algorithmic paradigm that decomposes a problem into a series of smaller subproblems and avoids redundant computation by storing the solutions to subproblems, thereby significantly improving time efficiency.

      In this section, we start with a classic example, first presenting its brute force backtracking solution, observing the overlapping subproblems within it, and then gradually deriving a more efficient dynamic programming solution.

      Climbing stairs

      Given a staircase with \\(n\\) steps, where you can climb \\(1\\) or \\(2\\) steps at a time, how many different ways are there to reach the top?

      As shown in Figure 14-1, for a \\(3\\)-step staircase, there are \\(3\\) different ways to reach the top.

      Figure 14-1 \u00a0 Number of ways to reach the 3rd step

      The goal of this problem is to find the number of ways, we can consider using backtracking to enumerate all possibilities. Specifically, imagine climbing stairs as a multi-round selection process: starting from the ground, choosing to go up \\(1\\) or \\(2\\) steps in each round, incrementing the count by \\(1\\) whenever the top of the stairs is reached, and pruning when exceeding the top. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_backtrack.py
      def backtrack(choices: list[int], state: int, n: int, res: list[int]) -> int:\n    \"\"\"Backtracking\"\"\"\n    # When climbing to the n-th stair, add 1 to the solution count\n    if state == n:\n        res[0] += 1\n    # Traverse all choices\n    for choice in choices:\n        # Pruning: not allowed to go beyond the n-th stair\n        if state + choice > n:\n            continue\n        # Attempt: make a choice, update state\n        backtrack(choices, state + choice, n, res)\n        # Backtrack\n\ndef climbing_stairs_backtrack(n: int) -> int:\n    \"\"\"Climbing stairs: Backtracking\"\"\"\n    choices = [1, 2]  # Can choose to climb up 1 or 2 stairs\n    state = 0  # Start climbing from the 0-th stair\n    res = [0]  # Use res[0] to record the solution count\n    backtrack(choices, state, n, res)\n    return res[0]\n
      climbing_stairs_backtrack.cpp
      /* Backtracking */\nvoid backtrack(vector<int> &choices, int state, int n, vector<int> &res) {\n    // When climbing to the n-th stair, add 1 to the solution count\n    if (state == n)\n        res[0]++;\n    // Traverse all choices\n    for (auto &choice : choices) {\n        // Pruning: not allowed to go beyond the n-th stair\n        if (state + choice > n)\n            continue;\n        // Attempt: make choice, update state\n        backtrack(choices, state + choice, n, res);\n        // Backtrack\n    }\n}\n\n/* Climbing stairs: Backtracking */\nint climbingStairsBacktrack(int n) {\n    vector<int> choices = {1, 2}; // Can choose to climb up 1 or 2 stairs\n    int state = 0;                // Start climbing from the 0-th stair\n    vector<int> res = {0};        // Use res[0] to record the solution count\n    backtrack(choices, state, n, res);\n    return res[0];\n}\n
      climbing_stairs_backtrack.java
      /* Backtracking */\nvoid backtrack(List<Integer> choices, int state, int n, List<Integer> res) {\n    // When climbing to the n-th stair, add 1 to the solution count\n    if (state == n)\n        res.set(0, res.get(0) + 1);\n    // Traverse all choices\n    for (Integer choice : choices) {\n        // Pruning: not allowed to go beyond the n-th stair\n        if (state + choice > n)\n            continue;\n        // Attempt: make choice, update state\n        backtrack(choices, state + choice, n, res);\n        // Backtrack\n    }\n}\n\n/* Climbing stairs: Backtracking */\nint climbingStairsBacktrack(int n) {\n    List<Integer> choices = Arrays.asList(1, 2); // Can choose to climb up 1 or 2 stairs\n    int state = 0; // Start climbing from the 0-th stair\n    List<Integer> res = new ArrayList<>();\n    res.add(0); // Use res[0] to record the solution count\n    backtrack(choices, state, n, res);\n    return res.get(0);\n}\n
      climbing_stairs_backtrack.cs
      /* Backtracking */\nvoid Backtrack(List<int> choices, int state, int n, List<int> res) {\n    // When climbing to the n-th stair, add 1 to the solution count\n    if (state == n)\n        res[0]++;\n    // Traverse all choices\n    foreach (int choice in choices) {\n        // Pruning: not allowed to go beyond the n-th stair\n        if (state + choice > n)\n            continue;\n        // Attempt: make choice, update state\n        Backtrack(choices, state + choice, n, res);\n        // Backtrack\n    }\n}\n\n/* Climbing stairs: Backtracking */\nint ClimbingStairsBacktrack(int n) {\n    List<int> choices = [1, 2]; // Can choose to climb up 1 or 2 stairs\n    int state = 0; // Start climbing from the 0-th stair\n    List<int> res = [0]; // Use res[0] to record the solution count\n    Backtrack(choices, state, n, res);\n    return res[0];\n}\n
      climbing_stairs_backtrack.go
      /* Backtracking */\nfunc backtrack(choices []int, state, n int, res []int) {\n    // When climbing to the n-th stair, add 1 to the solution count\n    if state == n {\n        res[0] = res[0] + 1\n    }\n    // Traverse all choices\n    for _, choice := range choices {\n        // Pruning: not allowed to go beyond the n-th stair\n        if state+choice > n {\n            continue\n        }\n        // Attempt: make choice, update state\n        backtrack(choices, state+choice, n, res)\n        // Backtrack\n    }\n}\n\n/* Climbing stairs: Backtracking */\nfunc climbingStairsBacktrack(n int) int {\n    // Can choose to climb up 1 or 2 stairs\n    choices := []int{1, 2}\n    // Start climbing from the 0-th stair\n    state := 0\n    res := make([]int, 1)\n    // Use res[0] to record the solution count\n    res[0] = 0\n    backtrack(choices, state, n, res)\n    return res[0]\n}\n
      climbing_stairs_backtrack.swift
      /* Backtracking */\nfunc backtrack(choices: [Int], state: Int, n: Int, res: inout [Int]) {\n    // When climbing to the n-th stair, add 1 to the solution count\n    if state == n {\n        res[0] += 1\n    }\n    // Traverse all choices\n    for choice in choices {\n        // Pruning: not allowed to go beyond the n-th stair\n        if state + choice > n {\n            continue\n        }\n        // Attempt: make choice, update state\n        backtrack(choices: choices, state: state + choice, n: n, res: &res)\n        // Backtrack\n    }\n}\n\n/* Climbing stairs: Backtracking */\nfunc climbingStairsBacktrack(n: Int) -> Int {\n    let choices = [1, 2] // Can choose to climb up 1 or 2 stairs\n    let state = 0 // Start climbing from the 0-th stair\n    var res: [Int] = []\n    res.append(0) // Use res[0] to record the solution count\n    backtrack(choices: choices, state: state, n: n, res: &res)\n    return res[0]\n}\n
      climbing_stairs_backtrack.js
      /* Backtracking */\nfunction backtrack(choices, state, n, res) {\n    // When climbing to the n-th stair, add 1 to the solution count\n    if (state === n) res.set(0, res.get(0) + 1);\n    // Traverse all choices\n    for (const choice of choices) {\n        // Pruning: not allowed to go beyond the n-th stair\n        if (state + choice > n) continue;\n        // Attempt: make choice, update state\n        backtrack(choices, state + choice, n, res);\n        // Backtrack\n    }\n}\n\n/* Climbing stairs: Backtracking */\nfunction climbingStairsBacktrack(n) {\n    const choices = [1, 2]; // Can choose to climb up 1 or 2 stairs\n    const state = 0; // Start climbing from the 0-th stair\n    const res = new Map();\n    res.set(0, 0); // Use res[0] to record the solution count\n    backtrack(choices, state, n, res);\n    return res.get(0);\n}\n
      climbing_stairs_backtrack.ts
      /* Backtracking */\nfunction backtrack(\n    choices: number[],\n    state: number,\n    n: number,\n    res: Map<0, any>\n): void {\n    // When climbing to the n-th stair, add 1 to the solution count\n    if (state === n) res.set(0, res.get(0) + 1);\n    // Traverse all choices\n    for (const choice of choices) {\n        // Pruning: not allowed to go beyond the n-th stair\n        if (state + choice > n) continue;\n        // Attempt: make choice, update state\n        backtrack(choices, state + choice, n, res);\n        // Backtrack\n    }\n}\n\n/* Climbing stairs: Backtracking */\nfunction climbingStairsBacktrack(n: number): number {\n    const choices = [1, 2]; // Can choose to climb up 1 or 2 stairs\n    const state = 0; // Start climbing from the 0-th stair\n    const res = new Map();\n    res.set(0, 0); // Use res[0] to record the solution count\n    backtrack(choices, state, n, res);\n    return res.get(0);\n}\n
      climbing_stairs_backtrack.dart
      /* Backtracking */\nvoid backtrack(List<int> choices, int state, int n, List<int> res) {\n  // When climbing to the n-th stair, add 1 to the solution count\n  if (state == n) {\n    res[0]++;\n  }\n  // Traverse all choices\n  for (int choice in choices) {\n    // Pruning: not allowed to go beyond the n-th stair\n    if (state + choice > n) continue;\n    // Attempt: make choice, update state\n    backtrack(choices, state + choice, n, res);\n    // Backtrack\n  }\n}\n\n/* Climbing stairs: Backtracking */\nint climbingStairsBacktrack(int n) {\n  List<int> choices = [1, 2]; // Can choose to climb up 1 or 2 stairs\n  int state = 0; // Start climbing from the 0-th stair\n  List<int> res = [];\n  res.add(0); // Use res[0] to record the solution count\n  backtrack(choices, state, n, res);\n  return res[0];\n}\n
      climbing_stairs_backtrack.rs
      /* Backtracking */\nfn backtrack(choices: &[i32], state: i32, n: i32, res: &mut [i32]) {\n    // When climbing to the n-th stair, add 1 to the solution count\n    if state == n {\n        res[0] = res[0] + 1;\n    }\n    // Traverse all choices\n    for &choice in choices {\n        // Pruning: not allowed to go beyond the n-th stair\n        if state + choice > n {\n            continue;\n        }\n        // Attempt: make choice, update state\n        backtrack(choices, state + choice, n, res);\n        // Backtrack\n    }\n}\n\n/* Climbing stairs: Backtracking */\nfn climbing_stairs_backtrack(n: usize) -> i32 {\n    let choices = vec![1, 2]; // Can choose to climb up 1 or 2 stairs\n    let state = 0; // Start climbing from the 0-th stair\n    let mut res = Vec::new();\n    res.push(0); // Use res[0] to record the solution count\n    backtrack(&choices, state, n as i32, &mut res);\n    res[0]\n}\n
      climbing_stairs_backtrack.c
      /* Backtracking */\nvoid backtrack(int *choices, int state, int n, int *res, int len) {\n    // When climbing to the n-th stair, add 1 to the solution count\n    if (state == n)\n        res[0]++;\n    // Traverse all choices\n    for (int i = 0; i < len; i++) {\n        int choice = choices[i];\n        // Pruning: not allowed to go beyond the n-th stair\n        if (state + choice > n)\n            continue;\n        // Attempt: make choice, update state\n        backtrack(choices, state + choice, n, res, len);\n        // Backtrack\n    }\n}\n\n/* Climbing stairs: Backtracking */\nint climbingStairsBacktrack(int n) {\n    int choices[2] = {1, 2}; // Can choose to climb up 1 or 2 stairs\n    int state = 0;           // Start climbing from the 0-th stair\n    int *res = (int *)malloc(sizeof(int));\n    *res = 0; // Use res[0] to record the solution count\n    int len = sizeof(choices) / sizeof(int);\n    backtrack(choices, state, n, res, len);\n    int result = *res;\n    free(res);\n    return result;\n}\n
      climbing_stairs_backtrack.kt
      /* Backtracking */\nfun backtrack(\n    choices: MutableList<Int>,\n    state: Int,\n    n: Int,\n    res: MutableList<Int>\n) {\n    // When climbing to the n-th stair, add 1 to the solution count\n    if (state == n)\n        res[0] = res[0] + 1\n    // Traverse all choices\n    for (choice in choices) {\n        // Pruning: not allowed to go beyond the n-th stair\n        if (state + choice > n) continue\n        // Attempt: make choice, update state\n        backtrack(choices, state + choice, n, res)\n        // Backtrack\n    }\n}\n\n/* Climbing stairs: Backtracking */\nfun climbingStairsBacktrack(n: Int): Int {\n    val choices = mutableListOf(1, 2) // Can choose to climb up 1 or 2 stairs\n    val state = 0 // Start climbing from the 0-th stair\n    val res = mutableListOf<Int>()\n    res.add(0) // Use res[0] to record the solution count\n    backtrack(choices, state, n, res)\n    return res[0]\n}\n
      climbing_stairs_backtrack.rb
      ### Backtracking ###\ndef backtrack(choices, state, n, res)\n  # When climbing to the n-th stair, add 1 to the solution count\n  res[0] += 1 if state == n\n  # Traverse all choices\n  for choice in choices\n    # Pruning: not allowed to go beyond the n-th stair\n    next if state + choice > n\n\n    # Attempt: make choice, update state\n    backtrack(choices, state + choice, n, res)\n  end\n  # Backtrack\nend\n\n### Climbing stairs: backtracking ###\ndef climbing_stairs_backtrack(n)\n  choices = [1, 2] # Can choose to climb up 1 or 2 stairs\n  state = 0 # Start climbing from the 0-th stair\n  res = [0] # Use res[0] to record the solution count\n  backtrack(choices, state, n, res)\n  res.first\nend\n
      "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1411-method-1-brute-force-search","title":"14.1.1 \u00a0 Method 1: Brute Force Search","text":"

      Backtracking algorithms typically do not explicitly decompose problems, but rather treat solving the problem as a series of decision steps, searching for all possible solutions through trial and pruning.

      We can try to analyze this problem from the perspective of problem decomposition. Let the number of ways to climb to the \\(i\\)-th step be \\(dp[i]\\), then \\(dp[i]\\) is the original problem, and its subproblems include:

      \\[ dp[i-1], dp[i-2], \\dots, dp[2], dp[1] \\]

      Since we can only go up \\(1\\) or \\(2\\) steps in each round, when we stand on the \\(i\\)-th step, we could only have been on the \\(i-1\\)-th or \\(i-2\\)-th step in the previous round. In other words, we can only reach the \\(i\\)-th step from the \\(i-1\\)-th or \\(i-2\\)-th step.

      This leads to an important conclusion: the number of ways to climb to the \\(i-1\\)-th step plus the number of ways to climb to the \\(i-2\\)-th step equals the number of ways to climb to the \\(i\\)-th step. The formula is as follows:

      \\[ dp[i] = dp[i-1] + dp[i-2] \\]

      This means that in the stair climbing problem, there exists a recurrence relation among the subproblems, the solution to the original problem can be constructed from the solutions to the subproblems. Figure 14-2 illustrates this recurrence relation.

      Figure 14-2 \u00a0 Recurrence relation for the number of ways

      We can obtain a brute force search solution based on the recurrence formula. Starting from \\(dp[n]\\), recursively decompose a larger problem into the sum of two smaller problems, until reaching the smallest subproblems \\(dp[1]\\) and \\(dp[2]\\) and returning. Among them, the solutions to the smallest subproblems are known, namely \\(dp[1] = 1\\) and \\(dp[2] = 2\\), representing \\(1\\) and \\(2\\) ways to climb to the \\(1\\)st and \\(2\\)nd steps, respectively.

      Observe the following code, which, like standard backtracking code, belongs to depth-first search but is more concise:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dfs.py
      def dfs(i: int) -> int:\n    \"\"\"Search\"\"\"\n    # Known dp[1] and dp[2], return them\n    if i == 1 or i == 2:\n        return i\n    # dp[i] = dp[i-1] + dp[i-2]\n    count = dfs(i - 1) + dfs(i - 2)\n    return count\n\ndef climbing_stairs_dfs(n: int) -> int:\n    \"\"\"Climbing stairs: Search\"\"\"\n    return dfs(n)\n
      climbing_stairs_dfs.cpp
      /* Search */\nint dfs(int i) {\n    // Known dp[1] and dp[2], return them\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* Climbing stairs: Search */\nint climbingStairsDFS(int n) {\n    return dfs(n);\n}\n
      climbing_stairs_dfs.java
      /* Search */\nint dfs(int i) {\n    // Known dp[1] and dp[2], return them\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* Climbing stairs: Search */\nint climbingStairsDFS(int n) {\n    return dfs(n);\n}\n
      climbing_stairs_dfs.cs
      /* Search */\nint DFS(int i) {\n    // Known dp[1] and dp[2], return them\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = DFS(i - 1) + DFS(i - 2);\n    return count;\n}\n\n/* Climbing stairs: Search */\nint ClimbingStairsDFS(int n) {\n    return DFS(n);\n}\n
      climbing_stairs_dfs.go
      /* Search */\nfunc dfs(i int) int {\n    // Known dp[1] and dp[2], return them\n    if i == 1 || i == 2 {\n        return i\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    count := dfs(i-1) + dfs(i-2)\n    return count\n}\n\n/* Climbing stairs: Search */\nfunc climbingStairsDFS(n int) int {\n    return dfs(n)\n}\n
      climbing_stairs_dfs.swift
      /* Search */\nfunc dfs(i: Int) -> Int {\n    // Known dp[1] and dp[2], return them\n    if i == 1 || i == 2 {\n        return i\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    let count = dfs(i: i - 1) + dfs(i: i - 2)\n    return count\n}\n\n/* Climbing stairs: Search */\nfunc climbingStairsDFS(n: Int) -> Int {\n    dfs(i: n)\n}\n
      climbing_stairs_dfs.js
      /* Search */\nfunction dfs(i) {\n    // Known dp[1] and dp[2], return them\n    if (i === 1 || i === 2) return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    const count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* Climbing stairs: Search */\nfunction climbingStairsDFS(n) {\n    return dfs(n);\n}\n
      climbing_stairs_dfs.ts
      /* Search */\nfunction dfs(i: number): number {\n    // Known dp[1] and dp[2], return them\n    if (i === 1 || i === 2) return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    const count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* Climbing stairs: Search */\nfunction climbingStairsDFS(n: number): number {\n    return dfs(n);\n}\n
      climbing_stairs_dfs.dart
      /* Search */\nint dfs(int i) {\n  // Known dp[1] and dp[2], return them\n  if (i == 1 || i == 2) return i;\n  // dp[i] = dp[i-1] + dp[i-2]\n  int count = dfs(i - 1) + dfs(i - 2);\n  return count;\n}\n\n/* Climbing stairs: Search */\nint climbingStairsDFS(int n) {\n  return dfs(n);\n}\n
      climbing_stairs_dfs.rs
      /* Search */\nfn dfs(i: usize) -> i32 {\n    // Known dp[1] and dp[2], return them\n    if i == 1 || i == 2 {\n        return i as i32;\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    let count = dfs(i - 1) + dfs(i - 2);\n    count\n}\n\n/* Climbing stairs: Search */\nfn climbing_stairs_dfs(n: usize) -> i32 {\n    dfs(n)\n}\n
      climbing_stairs_dfs.c
      /* Search */\nint dfs(int i) {\n    // Known dp[1] and dp[2], return them\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* Climbing stairs: Search */\nint climbingStairsDFS(int n) {\n    return dfs(n);\n}\n
      climbing_stairs_dfs.kt
      /* Search */\nfun dfs(i: Int): Int {\n    // Known dp[1] and dp[2], return them\n    if (i == 1 || i == 2) return i\n    // dp[i] = dp[i-1] + dp[i-2]\n    val count = dfs(i - 1) + dfs(i - 2)\n    return count\n}\n\n/* Climbing stairs: Search */\nfun climbingStairsDFS(n: Int): Int {\n    return dfs(n)\n}\n
      climbing_stairs_dfs.rb
      ### Search ###\ndef dfs(i)\n  # Known dp[1] and dp[2], return them\n  return i if i == 1 || i == 2\n  # dp[i] = dp[i-1] + dp[i-2]\n  dfs(i - 1) + dfs(i - 2)\nend\n\n### Climbing stairs: search ###\ndef climbing_stairs_dfs(n)\n  dfs(n)\nend\n

      Figure 14-3 shows the recursion tree formed by brute force search. For the problem \\(dp[n]\\), the depth of its recursion tree is \\(n\\), with a time complexity of \\(O(2^n)\\). Exponential order represents explosive growth; if we input a relatively large \\(n\\), we will fall into a long wait.

      Figure 14-3 \u00a0 Recursion tree for climbing stairs

      Observing the above figure, the exponential time complexity is caused by \"overlapping subproblems\". For example, \\(dp[9]\\) is decomposed into \\(dp[8]\\) and \\(dp[7]\\), and \\(dp[8]\\) is decomposed into \\(dp[7]\\) and \\(dp[6]\\), both of which contain the subproblem \\(dp[7]\\).

      And so on, subproblems contain smaller overlapping subproblems, ad infinitum. The vast majority of computational resources are wasted on these overlapping subproblems.

      "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1412-method-2-memoization","title":"14.1.2 \u00a0 Method 2: Memoization","text":"

      To improve algorithm efficiency, we want all overlapping subproblems to be computed only once. For this purpose, we declare an array mem to record the solution to each subproblem and prune overlapping subproblems during the search process.

      1. When computing \\(dp[i]\\) for the first time, we record it in mem[i] for later use.
      2. When we need to compute \\(dp[i]\\) again, we can directly retrieve the result from mem[i], thereby avoiding redundant computation of that subproblem.

      The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dfs_mem.py
      def dfs(i: int, mem: list[int]) -> int:\n    \"\"\"Memoization search\"\"\"\n    # Known dp[1] and dp[2], return them\n    if i == 1 or i == 2:\n        return i\n    # If record dp[i] exists, return it directly\n    if mem[i] != -1:\n        return mem[i]\n    # dp[i] = dp[i-1] + dp[i-2]\n    count = dfs(i - 1, mem) + dfs(i - 2, mem)\n    # Record dp[i]\n    mem[i] = count\n    return count\n\ndef climbing_stairs_dfs_mem(n: int) -> int:\n    \"\"\"Climbing stairs: Memoization search\"\"\"\n    # mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n    mem = [-1] * (n + 1)\n    return dfs(n, mem)\n
      climbing_stairs_dfs_mem.cpp
      /* Memoization search */\nint dfs(int i, vector<int> &mem) {\n    // Known dp[1] and dp[2], return them\n    if (i == 1 || i == 2)\n        return i;\n    // If record dp[i] exists, return it directly\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // Record dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* Climbing stairs: Memoization search */\nint climbingStairsDFSMem(int n) {\n    // mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n    vector<int> mem(n + 1, -1);\n    return dfs(n, mem);\n}\n
      climbing_stairs_dfs_mem.java
      /* Memoization search */\nint dfs(int i, int[] mem) {\n    // Known dp[1] and dp[2], return them\n    if (i == 1 || i == 2)\n        return i;\n    // If record dp[i] exists, return it directly\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // Record dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* Climbing stairs: Memoization search */\nint climbingStairsDFSMem(int n) {\n    // mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n    int[] mem = new int[n + 1];\n    Arrays.fill(mem, -1);\n    return dfs(n, mem);\n}\n
      climbing_stairs_dfs_mem.cs
      /* Memoization search */\nint DFS(int i, int[] mem) {\n    // Known dp[1] and dp[2], return them\n    if (i == 1 || i == 2)\n        return i;\n    // If record dp[i] exists, return it directly\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = DFS(i - 1, mem) + DFS(i - 2, mem);\n    // Record dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* Climbing stairs: Memoization search */\nint ClimbingStairsDFSMem(int n) {\n    // mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n    int[] mem = new int[n + 1];\n    Array.Fill(mem, -1);\n    return DFS(n, mem);\n}\n
      climbing_stairs_dfs_mem.go
      /* Memoization search */\nfunc dfsMem(i int, mem []int) int {\n    // Known dp[1] and dp[2], return them\n    if i == 1 || i == 2 {\n        return i\n    }\n    // If record dp[i] exists, return it directly\n    if mem[i] != -1 {\n        return mem[i]\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    count := dfsMem(i-1, mem) + dfsMem(i-2, mem)\n    // Record dp[i]\n    mem[i] = count\n    return count\n}\n\n/* Climbing stairs: Memoization search */\nfunc climbingStairsDFSMem(n int) int {\n    // mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n    mem := make([]int, n+1)\n    for i := range mem {\n        mem[i] = -1\n    }\n    return dfsMem(n, mem)\n}\n
      climbing_stairs_dfs_mem.swift
      /* Memoization search */\nfunc dfs(i: Int, mem: inout [Int]) -> Int {\n    // Known dp[1] and dp[2], return them\n    if i == 1 || i == 2 {\n        return i\n    }\n    // If record dp[i] exists, return it directly\n    if mem[i] != -1 {\n        return mem[i]\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    let count = dfs(i: i - 1, mem: &mem) + dfs(i: i - 2, mem: &mem)\n    // Record dp[i]\n    mem[i] = count\n    return count\n}\n\n/* Climbing stairs: Memoization search */\nfunc climbingStairsDFSMem(n: Int) -> Int {\n    // mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n    var mem = Array(repeating: -1, count: n + 1)\n    return dfs(i: n, mem: &mem)\n}\n
      climbing_stairs_dfs_mem.js
      /* Memoization search */\nfunction dfs(i, mem) {\n    // Known dp[1] and dp[2], return them\n    if (i === 1 || i === 2) return i;\n    // If record dp[i] exists, return it directly\n    if (mem[i] != -1) return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    const count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // Record dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* Climbing stairs: Memoization search */\nfunction climbingStairsDFSMem(n) {\n    // mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n    const mem = new Array(n + 1).fill(-1);\n    return dfs(n, mem);\n}\n
      climbing_stairs_dfs_mem.ts
      /* Memoization search */\nfunction dfs(i: number, mem: number[]): number {\n    // Known dp[1] and dp[2], return them\n    if (i === 1 || i === 2) return i;\n    // If record dp[i] exists, return it directly\n    if (mem[i] != -1) return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    const count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // Record dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* Climbing stairs: Memoization search */\nfunction climbingStairsDFSMem(n: number): number {\n    // mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n    const mem = new Array(n + 1).fill(-1);\n    return dfs(n, mem);\n}\n
      climbing_stairs_dfs_mem.dart
      /* Memoization search */\nint dfs(int i, List<int> mem) {\n  // Known dp[1] and dp[2], return them\n  if (i == 1 || i == 2) return i;\n  // If record dp[i] exists, return it directly\n  if (mem[i] != -1) return mem[i];\n  // dp[i] = dp[i-1] + dp[i-2]\n  int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n  // Record dp[i]\n  mem[i] = count;\n  return count;\n}\n\n/* Climbing stairs: Memoization search */\nint climbingStairsDFSMem(int n) {\n  // mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n  List<int> mem = List.filled(n + 1, -1);\n  return dfs(n, mem);\n}\n
      climbing_stairs_dfs_mem.rs
      /* Memoization search */\nfn dfs(i: usize, mem: &mut [i32]) -> i32 {\n    // Known dp[1] and dp[2], return them\n    if i == 1 || i == 2 {\n        return i as i32;\n    }\n    // If record dp[i] exists, return it directly\n    if mem[i] != -1 {\n        return mem[i];\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    let count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // Record dp[i]\n    mem[i] = count;\n    count\n}\n\n/* Climbing stairs: Memoization search */\nfn climbing_stairs_dfs_mem(n: usize) -> i32 {\n    // mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n    let mut mem = vec![-1; n + 1];\n    dfs(n, &mut mem)\n}\n
      climbing_stairs_dfs_mem.c
      /* Memoization search */\nint dfs(int i, int *mem) {\n    // Known dp[1] and dp[2], return them\n    if (i == 1 || i == 2)\n        return i;\n    // If record dp[i] exists, return it directly\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // Record dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* Climbing stairs: Memoization search */\nint climbingStairsDFSMem(int n) {\n    // mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n    int *mem = (int *)malloc((n + 1) * sizeof(int));\n    for (int i = 0; i <= n; i++) {\n        mem[i] = -1;\n    }\n    int result = dfs(n, mem);\n    free(mem);\n    return result;\n}\n
      climbing_stairs_dfs_mem.kt
      /* Memoization search */\nfun dfs(i: Int, mem: IntArray): Int {\n    // Known dp[1] and dp[2], return them\n    if (i == 1 || i == 2) return i\n    // If record dp[i] exists, return it directly\n    if (mem[i] != -1) return mem[i]\n    // dp[i] = dp[i-1] + dp[i-2]\n    val count = dfs(i - 1, mem) + dfs(i - 2, mem)\n    // Record dp[i]\n    mem[i] = count\n    return count\n}\n\n/* Climbing stairs: Memoization search */\nfun climbingStairsDFSMem(n: Int): Int {\n    // mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n    val mem = IntArray(n + 1)\n    mem.fill(-1)\n    return dfs(n, mem)\n}\n
      climbing_stairs_dfs_mem.rb
      ### Memoization search ###\ndef dfs(i, mem)\n  # Known dp[1] and dp[2], return them\n  return i if i == 1 || i == 2\n  # If record dp[i] exists, return it directly\n  return mem[i] if mem[i] != -1\n\n  # dp[i] = dp[i-1] + dp[i-2]\n  count = dfs(i - 1, mem) + dfs(i - 2, mem)\n  # Record dp[i]\n  mem[i] = count\nend\n\n### Climbing stairs: memoization search ###\ndef climbing_stairs_dfs_mem(n)\n  # mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n  mem = Array.new(n + 1, -1)\n  dfs(n, mem)\nend\n

      Observe Figure 14-4, after memoization, all overlapping subproblems only need to be computed once, optimizing the time complexity to \\(O(n)\\), which is a tremendous leap.

      Figure 14-4 \u00a0 Recursion tree with memoization

      "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1413-method-3-dynamic-programming","title":"14.1.3 \u00a0 Method 3: Dynamic Programming","text":"

      Memoization is a \"top-down\" method: we start from the original problem (root node), recursively decompose larger subproblems into smaller ones, until reaching the smallest known subproblems (leaf nodes). Afterward, by backtracking, we collect the solutions to the subproblems layer by layer to construct the solution to the original problem.

      In contrast, dynamic programming is a \"bottom-up\" method: starting from the solutions to the smallest subproblems, iteratively constructing solutions to larger subproblems until obtaining the solution to the original problem.

      Since dynamic programming does not include a backtracking process, it only requires loop iteration for implementation and does not need recursion. In the following code, we initialize an array dp to store the solutions to subproblems, which serves the same recording function as the array mem in memoization:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dp.py
      def climbing_stairs_dp(n: int) -> int:\n    \"\"\"Climbing stairs: Dynamic programming\"\"\"\n    if n == 1 or n == 2:\n        return n\n    # Initialize dp table, used to store solutions to subproblems\n    dp = [0] * (n + 1)\n    # Initial state: preset the solution to the smallest subproblem\n    dp[1], dp[2] = 1, 2\n    # State transition: gradually solve larger subproblems from smaller ones\n    for i in range(3, n + 1):\n        dp[i] = dp[i - 1] + dp[i - 2]\n    return dp[n]\n
      climbing_stairs_dp.cpp
      /* Climbing stairs: Dynamic programming */\nint climbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // Initialize dp table, used to store solutions to subproblems\n    vector<int> dp(n + 1);\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = 1;\n    dp[2] = 2;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
      climbing_stairs_dp.java
      /* Climbing stairs: Dynamic programming */\nint climbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // Initialize dp table, used to store solutions to subproblems\n    int[] dp = new int[n + 1];\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = 1;\n    dp[2] = 2;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
      climbing_stairs_dp.cs
      /* Climbing stairs: Dynamic programming */\nint ClimbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // Initialize dp table, used to store solutions to subproblems\n    int[] dp = new int[n + 1];\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = 1;\n    dp[2] = 2;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
      climbing_stairs_dp.go
      /* Climbing stairs: Dynamic programming */\nfunc climbingStairsDP(n int) int {\n    if n == 1 || n == 2 {\n        return n\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    dp := make([]int, n+1)\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = 1\n    dp[2] = 2\n    // State transition: gradually solve larger subproblems from smaller ones\n    for i := 3; i <= n; i++ {\n        dp[i] = dp[i-1] + dp[i-2]\n    }\n    return dp[n]\n}\n
      climbing_stairs_dp.swift
      /* Climbing stairs: Dynamic programming */\nfunc climbingStairsDP(n: Int) -> Int {\n    if n == 1 || n == 2 {\n        return n\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    var dp = Array(repeating: 0, count: n + 1)\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = 1\n    dp[2] = 2\n    // State transition: gradually solve larger subproblems from smaller ones\n    for i in 3 ... n {\n        dp[i] = dp[i - 1] + dp[i - 2]\n    }\n    return dp[n]\n}\n
      climbing_stairs_dp.js
      /* Climbing stairs: Dynamic programming */\nfunction climbingStairsDP(n) {\n    if (n === 1 || n === 2) return n;\n    // Initialize dp table, used to store solutions to subproblems\n    const dp = new Array(n + 1).fill(-1);\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = 1;\n    dp[2] = 2;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (let i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
      climbing_stairs_dp.ts
      /* Climbing stairs: Dynamic programming */\nfunction climbingStairsDP(n: number): number {\n    if (n === 1 || n === 2) return n;\n    // Initialize dp table, used to store solutions to subproblems\n    const dp = new Array(n + 1).fill(-1);\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = 1;\n    dp[2] = 2;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (let i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
      climbing_stairs_dp.dart
      /* Climbing stairs: Dynamic programming */\nint climbingStairsDP(int n) {\n  if (n == 1 || n == 2) return n;\n  // Initialize dp table, used to store solutions to subproblems\n  List<int> dp = List.filled(n + 1, 0);\n  // Initial state: preset the solution to the smallest subproblem\n  dp[1] = 1;\n  dp[2] = 2;\n  // State transition: gradually solve larger subproblems from smaller ones\n  for (int i = 3; i <= n; i++) {\n    dp[i] = dp[i - 1] + dp[i - 2];\n  }\n  return dp[n];\n}\n
      climbing_stairs_dp.rs
      /* Climbing stairs: Dynamic programming */\nfn climbing_stairs_dp(n: usize) -> i32 {\n    // Known dp[1] and dp[2], return them\n    if n == 1 || n == 2 {\n        return n as i32;\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    let mut dp = vec![-1; n + 1];\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = 1;\n    dp[2] = 2;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for i in 3..=n {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    dp[n]\n}\n
      climbing_stairs_dp.c
      /* Climbing stairs: Dynamic programming */\nint climbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // Initialize dp table, used to store solutions to subproblems\n    int *dp = (int *)malloc((n + 1) * sizeof(int));\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = 1;\n    dp[2] = 2;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    int result = dp[n];\n    free(dp);\n    return result;\n}\n
      climbing_stairs_dp.kt
      /* Climbing stairs: Dynamic programming */\nfun climbingStairsDP(n: Int): Int {\n    if (n == 1 || n == 2) return n\n    // Initialize dp table, used to store solutions to subproblems\n    val dp = IntArray(n + 1)\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = 1\n    dp[2] = 2\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (i in 3..n) {\n        dp[i] = dp[i - 1] + dp[i - 2]\n    }\n    return dp[n]\n}\n
      climbing_stairs_dp.rb
      ### Climbing stairs: dynamic programming ###\ndef climbing_stairs_dp(n)\n  return n  if n == 1 || n == 2\n\n  # Initialize dp table, used to store solutions to subproblems\n  dp = Array.new(n + 1, 0)\n  # Initial state: preset the solution to the smallest subproblem\n  dp[1], dp[2] = 1, 2\n  # State transition: gradually solve larger subproblems from smaller ones\n  (3...(n + 1)).each { |i| dp[i] = dp[i - 1] + dp[i - 2] }\n\n  dp[n]\nend\n

      Figure 14-5 simulates the execution process of the above code.

      Figure 14-5 \u00a0 Dynamic programming process for climbing stairs

      Like backtracking algorithms, dynamic programming also uses the \"state\" concept to represent specific stages of problem solving, with each state corresponding to a subproblem and its corresponding local optimal solution. For example, the state in the stair climbing problem is defined as the current stair step number \\(i\\).

      Based on the above content, we can summarize the commonly used terminology in dynamic programming.

      • The array dp is called the dp table, where \\(dp[i]\\) represents the solution to the subproblem corresponding to state \\(i\\).
      • The states corresponding to the smallest subproblems (the \\(1\\)st and \\(2\\)nd steps) are called initial states.
      • The recurrence formula \\(dp[i] = dp[i-1] + dp[i-2]\\) is called the state transition equation.
      "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1414-space-optimization","title":"14.1.4 \u00a0 Space Optimization","text":"

      Observant readers may have noticed that since \\(dp[i]\\) is only related to \\(dp[i-1]\\) and \\(dp[i-2]\\), we do not need to use an array dp to store the solutions to all subproblems, but can simply use two variables to roll forward. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dp.py
      def climbing_stairs_dp_comp(n: int) -> int:\n    \"\"\"Climbing stairs: Space-optimized dynamic programming\"\"\"\n    if n == 1 or n == 2:\n        return n\n    a, b = 1, 2\n    for _ in range(3, n + 1):\n        a, b = b, a + b\n    return b\n
      climbing_stairs_dp.cpp
      /* Climbing stairs: Space-optimized dynamic programming */\nint climbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
      climbing_stairs_dp.java
      /* Climbing stairs: Space-optimized dynamic programming */\nint climbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
      climbing_stairs_dp.cs
      /* Climbing stairs: Space-optimized dynamic programming */\nint ClimbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
      climbing_stairs_dp.go
      /* Climbing stairs: Space-optimized dynamic programming */\nfunc climbingStairsDPComp(n int) int {\n    if n == 1 || n == 2 {\n        return n\n    }\n    a, b := 1, 2\n    // State transition: gradually solve larger subproblems from smaller ones\n    for i := 3; i <= n; i++ {\n        a, b = b, a+b\n    }\n    return b\n}\n
      climbing_stairs_dp.swift
      /* Climbing stairs: Space-optimized dynamic programming */\nfunc climbingStairsDPComp(n: Int) -> Int {\n    if n == 1 || n == 2 {\n        return n\n    }\n    var a = 1\n    var b = 2\n    for _ in 3 ... n {\n        (a, b) = (b, a + b)\n    }\n    return b\n}\n
      climbing_stairs_dp.js
      /* Climbing stairs: Space-optimized dynamic programming */\nfunction climbingStairsDPComp(n) {\n    if (n === 1 || n === 2) return n;\n    let a = 1,\n        b = 2;\n    for (let i = 3; i <= n; i++) {\n        const tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
      climbing_stairs_dp.ts
      /* Climbing stairs: Space-optimized dynamic programming */\nfunction climbingStairsDPComp(n: number): number {\n    if (n === 1 || n === 2) return n;\n    let a = 1,\n        b = 2;\n    for (let i = 3; i <= n; i++) {\n        const tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
      climbing_stairs_dp.dart
      /* Climbing stairs: Space-optimized dynamic programming */\nint climbingStairsDPComp(int n) {\n  if (n == 1 || n == 2) return n;\n  int a = 1, b = 2;\n  for (int i = 3; i <= n; i++) {\n    int tmp = b;\n    b = a + b;\n    a = tmp;\n  }\n  return b;\n}\n
      climbing_stairs_dp.rs
      /* Climbing stairs: Space-optimized dynamic programming */\nfn climbing_stairs_dp_comp(n: usize) -> i32 {\n    if n == 1 || n == 2 {\n        return n as i32;\n    }\n    let (mut a, mut b) = (1, 2);\n    for _ in 3..=n {\n        let tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    b\n}\n
      climbing_stairs_dp.c
      /* Climbing stairs: Space-optimized dynamic programming */\nint climbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
      climbing_stairs_dp.kt
      /* Climbing stairs: Space-optimized dynamic programming */\nfun climbingStairsDPComp(n: Int): Int {\n    if (n == 1 || n == 2) return n\n    var a = 1\n    var b = 2\n    for (i in 3..n) {\n        val temp = b\n        b += a\n        a = temp\n    }\n    return b\n}\n
      climbing_stairs_dp.rb
      ### Climbing stairs: space-optimized DP ###\ndef climbing_stairs_dp_comp(n)\n  return n if n == 1 || n == 2\n\n  a, b = 1, 2\n  (3...(n + 1)).each { a, b = b, a + b }\n\n  b\nend\n

      Observing the above code, since the space occupied by the array dp is saved, the space complexity is reduced from \\(O(n)\\) to \\(O(1)\\).

      In dynamic programming problems, the current state often depends only on a limited number of preceding states, allowing us to retain only the necessary states and save memory space through \"dimension reduction\". This space optimization technique is called \"rolling variable\" or \"rolling array\".

      "},{"location":"chapter_dynamic_programming/knapsack_problem/","title":"14.4 \u00a0 0-1 Knapsack Problem","text":"

      The knapsack problem is an excellent introductory problem for dynamic programming and is one of the most common problem forms in dynamic programming. It has many variants, such as the 0-1 knapsack problem, the unbounded knapsack problem, and the multiple knapsack problem.

      In this section, we will first solve the most common 0-1 knapsack problem.

      Question

      Given \\(n\\) items, where the weight of the \\(i\\)-th item is \\(wgt[i-1]\\) and its value is \\(val[i-1]\\), and a knapsack with capacity \\(cap\\). Each item can only be selected once. What is the maximum value that can be placed in the knapsack within the capacity limit?

      Observe Figure 14-17. Since item number \\(i\\) starts counting from \\(1\\) and array indices start from \\(0\\), item \\(i\\) corresponds to weight \\(wgt[i-1]\\) and value \\(val[i-1]\\).

      Figure 14-17 \u00a0 Example data for 0-1 knapsack

      We can view the 0-1 knapsack problem as a process consisting of \\(n\\) rounds of decisions, where for each item there are two decisions: not putting it in and putting it in, thus the problem satisfies the decision tree model.

      The goal of this problem is to find \"the maximum value that can be placed in the knapsack within the capacity limit\", so it is more likely to be a dynamic programming problem.

      Step 1: Think about the decisions in each round, define the state, and thus obtain the \\(dp\\) table

      For each item, if not placed in the knapsack, the knapsack capacity remains unchanged; if placed in, the knapsack capacity decreases. From this, we can derive the state definition: current item number \\(i\\) and knapsack capacity \\(c\\), denoted as \\([i, c]\\).

      State \\([i, c]\\) corresponds to the subproblem: the maximum value among the first \\(i\\) items in a knapsack of capacity \\(c\\), denoted as \\(dp[i, c]\\).

      What we need to find is \\(dp[n, cap]\\), so we need a two-dimensional \\(dp\\) table of size \\((n+1) \\times (cap+1)\\).

      Step 2: Identify the optimal substructure, and then derive the state transition equation

      After making the decision for item \\(i\\), what remains is the subproblem of the first \\(i-1\\) items, which can be divided into the following two cases.

      • Not putting item \\(i\\): The knapsack capacity remains unchanged, and the state changes to \\([i-1, c]\\).
      • Putting item \\(i\\): The knapsack capacity decreases by \\(wgt[i-1]\\), the value increases by \\(val[i-1]\\), and the state changes to \\([i-1, c-wgt[i-1]]\\).

      The above analysis reveals the optimal substructure of this problem: the maximum value \\(dp[i, c]\\) equals the larger value between not putting item \\(i\\) and putting item \\(i\\). From this, the state transition equation can be derived:

      \\[ dp[i, c] = \\max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1]) \\]

      Note that if the weight of the current item \\(wgt[i - 1]\\) exceeds the remaining knapsack capacity \\(c\\), then the only option is not to put it in the knapsack.

      Step 3: Determine boundary conditions and state transition order

      When there are no items or the knapsack capacity is \\(0\\), the maximum value is \\(0\\), i.e., the first column \\(dp[i, 0]\\) and the first row \\(dp[0, c]\\) are both equal to \\(0\\).

      The current state \\([i, c]\\) is transferred from the state above \\([i-1, c]\\) and the state in the upper-left \\([i-1, c-wgt[i-1]]\\), so the entire \\(dp\\) table is traversed in order through two nested loops.

      Based on the above analysis, we will next implement the brute force search, memoization, and dynamic programming solutions in order.

      "},{"location":"chapter_dynamic_programming/knapsack_problem/#1-method-1-brute-force-search","title":"1. \u00a0 Method 1: Brute Force Search","text":"

      The search code includes the following elements.

      • Recursive parameters: state \\([i, c]\\).
      • Return value: solution to the subproblem \\(dp[i, c]\\).
      • Termination condition: when the item number is out of bounds \\(i = 0\\) or the remaining knapsack capacity is \\(0\\), terminate recursion and return value \\(0\\).
      • Pruning: if the weight of the current item exceeds the remaining knapsack capacity, only the option of not putting it in is available.
      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
      def knapsack_dfs(wgt: list[int], val: list[int], i: int, c: int) -> int:\n    \"\"\"0-1 knapsack: Brute-force search\"\"\"\n    # If all items have been selected or knapsack has no remaining capacity, return value 0\n    if i == 0 or c == 0:\n        return 0\n    # If exceeds knapsack capacity, can only choose not to put it in\n    if wgt[i - 1] > c:\n        return knapsack_dfs(wgt, val, i - 1, c)\n    # Calculate the maximum value of not putting in and putting in item i\n    no = knapsack_dfs(wgt, val, i - 1, c)\n    yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]\n    # Return the larger value of the two options\n    return max(no, yes)\n
      knapsack.cpp
      /* 0-1 knapsack: Brute-force search */\nint knapsackDFS(vector<int> &wgt, vector<int> &val, int i, int c) {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    int no = knapsackDFS(wgt, val, i - 1, c);\n    int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // Return the larger value of the two options\n    return max(no, yes);\n}\n
      knapsack.java
      /* 0-1 knapsack: Brute-force search */\nint knapsackDFS(int[] wgt, int[] val, int i, int c) {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    int no = knapsackDFS(wgt, val, i - 1, c);\n    int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // Return the larger value of the two options\n    return Math.max(no, yes);\n}\n
      knapsack.cs
      /* 0-1 knapsack: Brute-force search */\nint KnapsackDFS(int[] weight, int[] val, int i, int c) {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (weight[i - 1] > c) {\n        return KnapsackDFS(weight, val, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    int no = KnapsackDFS(weight, val, i - 1, c);\n    int yes = KnapsackDFS(weight, val, i - 1, c - weight[i - 1]) + val[i - 1];\n    // Return the larger value of the two options\n    return Math.Max(no, yes);\n}\n
      knapsack.go
      /* 0-1 knapsack: Brute-force search */\nfunc knapsackDFS(wgt, val []int, i, c int) int {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if i == 0 || c == 0 {\n        return 0\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if wgt[i-1] > c {\n        return knapsackDFS(wgt, val, i-1, c)\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    no := knapsackDFS(wgt, val, i-1, c)\n    yes := knapsackDFS(wgt, val, i-1, c-wgt[i-1]) + val[i-1]\n    // Return the larger value of the two options\n    return int(math.Max(float64(no), float64(yes)))\n}\n
      knapsack.swift
      /* 0-1 knapsack: Brute-force search */\nfunc knapsackDFS(wgt: [Int], val: [Int], i: Int, c: Int) -> Int {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if i == 0 || c == 0 {\n        return 0\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if wgt[i - 1] > c {\n        return knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    let no = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)\n    let yes = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]\n    // Return the larger value of the two options\n    return max(no, yes)\n}\n
      knapsack.js
      /* 0-1 knapsack: Brute-force search */\nfunction knapsackDFS(wgt, val, i, c) {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i === 0 || c === 0) {\n        return 0;\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    const no = knapsackDFS(wgt, val, i - 1, c);\n    const yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // Return the larger value of the two options\n    return Math.max(no, yes);\n}\n
      knapsack.ts
      /* 0-1 knapsack: Brute-force search */\nfunction knapsackDFS(\n    wgt: Array<number>,\n    val: Array<number>,\n    i: number,\n    c: number\n): number {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i === 0 || c === 0) {\n        return 0;\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    const no = knapsackDFS(wgt, val, i - 1, c);\n    const yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // Return the larger value of the two options\n    return Math.max(no, yes);\n}\n
      knapsack.dart
      /* 0-1 knapsack: Brute-force search */\nint knapsackDFS(List<int> wgt, List<int> val, int i, int c) {\n  // If all items have been selected or knapsack has no remaining capacity, return value 0\n  if (i == 0 || c == 0) {\n    return 0;\n  }\n  // If exceeds knapsack capacity, can only choose not to put it in\n  if (wgt[i - 1] > c) {\n    return knapsackDFS(wgt, val, i - 1, c);\n  }\n  // Calculate the maximum value of not putting in and putting in item i\n  int no = knapsackDFS(wgt, val, i - 1, c);\n  int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n  // Return the larger value of the two options\n  return max(no, yes);\n}\n
      knapsack.rs
      /* 0-1 knapsack: Brute-force search */\nfn knapsack_dfs(wgt: &[i32], val: &[i32], i: usize, c: usize) -> i32 {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if i == 0 || c == 0 {\n        return 0;\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if wgt[i - 1] > c as i32 {\n        return knapsack_dfs(wgt, val, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    let no = knapsack_dfs(wgt, val, i - 1, c);\n    let yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1] as usize) + val[i - 1];\n    // Return the larger value of the two options\n    std::cmp::max(no, yes)\n}\n
      knapsack.c
      /* 0-1 knapsack: Brute-force search */\nint knapsackDFS(int wgt[], int val[], int i, int c) {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    int no = knapsackDFS(wgt, val, i - 1, c);\n    int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // Return the larger value of the two options\n    return myMax(no, yes);\n}\n
      knapsack.kt
      /* 0-1 knapsack: Brute-force search */\nfun knapsackDFS(\n    wgt: IntArray,\n    _val: IntArray,\n    i: Int,\n    c: Int\n): Int {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i == 0 || c == 0) {\n        return 0\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, _val, i - 1, c)\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    val no = knapsackDFS(wgt, _val, i - 1, c)\n    val yes = knapsackDFS(wgt, _val, i - 1, c - wgt[i - 1]) + _val[i - 1]\n    // Return the larger value of the two options\n    return max(no, yes)\n}\n
      knapsack.rb
      ### 0-1 knapsack: brute force search ###\ndef knapsack_dfs(wgt, val, i, c)\n  # If all items have been selected or knapsack has no remaining capacity, return value 0\n  return 0 if i == 0 || c == 0\n  # If exceeds knapsack capacity, can only choose not to put it in\n  return knapsack_dfs(wgt, val, i - 1, c) if wgt[i - 1] > c\n  # Calculate the maximum value of not putting in and putting in item i\n  no = knapsack_dfs(wgt, val, i - 1, c)\n  yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]\n  # Return the larger value of the two options\n  [no, yes].max\nend\n

      As shown in Figure 14-18, since each item generates two search branches of not selecting and selecting, the time complexity is \\(O(2^n)\\).

      Observing the recursion tree, it is easy to see overlapping subproblems, such as \\(dp[1, 10]\\). When there are many items, large knapsack capacity, and especially many items with the same weight, the number of overlapping subproblems will increase significantly.

      Figure 14-18 \u00a0 Brute force search recursion tree for 0-1 knapsack problem

      "},{"location":"chapter_dynamic_programming/knapsack_problem/#2-method-2-memoization","title":"2. \u00a0 Method 2: Memoization","text":"

      To ensure that overlapping subproblems are only computed once, we use a memo list mem to record the solutions to subproblems, where mem[i][c] corresponds to \\(dp[i, c]\\).

      After introducing memoization, the time complexity depends on the number of subproblems, which is \\(O(n \\times cap)\\). The implementation code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
      def knapsack_dfs_mem(\n    wgt: list[int], val: list[int], mem: list[list[int]], i: int, c: int\n) -> int:\n    \"\"\"0-1 knapsack: Memoization search\"\"\"\n    # If all items have been selected or knapsack has no remaining capacity, return value 0\n    if i == 0 or c == 0:\n        return 0\n    # If there's a record, return it directly\n    if mem[i][c] != -1:\n        return mem[i][c]\n    # If exceeds knapsack capacity, can only choose not to put it in\n    if wgt[i - 1] > c:\n        return knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n    # Calculate the maximum value of not putting in and putting in item i\n    no = knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n    yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1]\n    # Record and return the larger value of the two options\n    mem[i][c] = max(no, yes)\n    return mem[i][c]\n
      knapsack.cpp
      /* 0-1 knapsack: Memoization search */\nint knapsackDFSMem(vector<int> &wgt, vector<int> &val, vector<vector<int>> &mem, int i, int c) {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // If there's a record, return it directly\n    if (mem[i][c] != -1) {\n        return mem[i][c];\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, mem, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n    int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // Record and return the larger value of the two options\n    mem[i][c] = max(no, yes);\n    return mem[i][c];\n}\n
      knapsack.java
      /* 0-1 knapsack: Memoization search */\nint knapsackDFSMem(int[] wgt, int[] val, int[][] mem, int i, int c) {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // If there's a record, return it directly\n    if (mem[i][c] != -1) {\n        return mem[i][c];\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, mem, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n    int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // Record and return the larger value of the two options\n    mem[i][c] = Math.max(no, yes);\n    return mem[i][c];\n}\n
      knapsack.cs
      /* 0-1 knapsack: Memoization search */\nint KnapsackDFSMem(int[] weight, int[] val, int[][] mem, int i, int c) {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // If there's a record, return it directly\n    if (mem[i][c] != -1) {\n        return mem[i][c];\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (weight[i - 1] > c) {\n        return KnapsackDFSMem(weight, val, mem, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    int no = KnapsackDFSMem(weight, val, mem, i - 1, c);\n    int yes = KnapsackDFSMem(weight, val, mem, i - 1, c - weight[i - 1]) + val[i - 1];\n    // Record and return the larger value of the two options\n    mem[i][c] = Math.Max(no, yes);\n    return mem[i][c];\n}\n
      knapsack.go
      /* 0-1 knapsack: Memoization search */\nfunc knapsackDFSMem(wgt, val []int, mem [][]int, i, c int) int {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if i == 0 || c == 0 {\n        return 0\n    }\n    // If there's a record, return it directly\n    if mem[i][c] != -1 {\n        return mem[i][c]\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if wgt[i-1] > c {\n        return knapsackDFSMem(wgt, val, mem, i-1, c)\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    no := knapsackDFSMem(wgt, val, mem, i-1, c)\n    yes := knapsackDFSMem(wgt, val, mem, i-1, c-wgt[i-1]) + val[i-1]\n    // Return the larger value of the two options\n    mem[i][c] = int(math.Max(float64(no), float64(yes)))\n    return mem[i][c]\n}\n
      knapsack.swift
      /* 0-1 knapsack: Memoization search */\nfunc knapsackDFSMem(wgt: [Int], val: [Int], mem: inout [[Int]], i: Int, c: Int) -> Int {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if i == 0 || c == 0 {\n        return 0\n    }\n    // If there's a record, return it directly\n    if mem[i][c] != -1 {\n        return mem[i][c]\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if wgt[i - 1] > c {\n        return knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c)\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    let no = knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c)\n    let yes = knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]\n    // Record and return the larger value of the two options\n    mem[i][c] = max(no, yes)\n    return mem[i][c]\n}\n
      knapsack.js
      /* 0-1 knapsack: Memoization search */\nfunction knapsackDFSMem(wgt, val, mem, i, c) {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i === 0 || c === 0) {\n        return 0;\n    }\n    // If there's a record, return it directly\n    if (mem[i][c] !== -1) {\n        return mem[i][c];\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, mem, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    const no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n    const yes =\n        knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // Record and return the larger value of the two options\n    mem[i][c] = Math.max(no, yes);\n    return mem[i][c];\n}\n
      knapsack.ts
      /* 0-1 knapsack: Memoization search */\nfunction knapsackDFSMem(\n    wgt: Array<number>,\n    val: Array<number>,\n    mem: Array<Array<number>>,\n    i: number,\n    c: number\n): number {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i === 0 || c === 0) {\n        return 0;\n    }\n    // If there's a record, return it directly\n    if (mem[i][c] !== -1) {\n        return mem[i][c];\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, mem, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    const no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n    const yes =\n        knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // Record and return the larger value of the two options\n    mem[i][c] = Math.max(no, yes);\n    return mem[i][c];\n}\n
      knapsack.dart
      /* 0-1 knapsack: Memoization search */\nint knapsackDFSMem(\n  List<int> wgt,\n  List<int> val,\n  List<List<int>> mem,\n  int i,\n  int c,\n) {\n  // If all items have been selected or knapsack has no remaining capacity, return value 0\n  if (i == 0 || c == 0) {\n    return 0;\n  }\n  // If there's a record, return it directly\n  if (mem[i][c] != -1) {\n    return mem[i][c];\n  }\n  // If exceeds knapsack capacity, can only choose not to put it in\n  if (wgt[i - 1] > c) {\n    return knapsackDFSMem(wgt, val, mem, i - 1, c);\n  }\n  // Calculate the maximum value of not putting in and putting in item i\n  int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n  int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n  // Record and return the larger value of the two options\n  mem[i][c] = max(no, yes);\n  return mem[i][c];\n}\n
      knapsack.rs
      /* 0-1 knapsack: Memoization search */\nfn knapsack_dfs_mem(wgt: &[i32], val: &[i32], mem: &mut Vec<Vec<i32>>, i: usize, c: usize) -> i32 {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if i == 0 || c == 0 {\n        return 0;\n    }\n    // If there's a record, return it directly\n    if mem[i][c] != -1 {\n        return mem[i][c];\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if wgt[i - 1] > c as i32 {\n        return knapsack_dfs_mem(wgt, val, mem, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    let no = knapsack_dfs_mem(wgt, val, mem, i - 1, c);\n    let yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1] as usize) + val[i - 1];\n    // Record and return the larger value of the two options\n    mem[i][c] = std::cmp::max(no, yes);\n    mem[i][c]\n}\n
      knapsack.c
      /* 0-1 knapsack: Memoization search */\nint knapsackDFSMem(int wgt[], int val[], int memCols, int **mem, int i, int c) {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // If there's a record, return it directly\n    if (mem[i][c] != -1) {\n        return mem[i][c];\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, memCols, mem, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    int no = knapsackDFSMem(wgt, val, memCols, mem, i - 1, c);\n    int yes = knapsackDFSMem(wgt, val, memCols, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // Record and return the larger value of the two options\n    mem[i][c] = myMax(no, yes);\n    return mem[i][c];\n}\n
      knapsack.kt
      /* 0-1 knapsack: Memoization search */\nfun knapsackDFSMem(\n    wgt: IntArray,\n    _val: IntArray,\n    mem: Array<IntArray>,\n    i: Int,\n    c: Int\n): Int {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i == 0 || c == 0) {\n        return 0\n    }\n    // If there's a record, return it directly\n    if (mem[i][c] != -1) {\n        return mem[i][c]\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, _val, mem, i - 1, c)\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    val no = knapsackDFSMem(wgt, _val, mem, i - 1, c)\n    val yes = knapsackDFSMem(wgt, _val, mem, i - 1, c - wgt[i - 1]) + _val[i - 1]\n    // Record and return the larger value of the two options\n    mem[i][c] = max(no, yes)\n    return mem[i][c]\n}\n
      knapsack.rb
      ### 0-1 knapsack: memoization search ###\ndef knapsack_dfs_mem(wgt, val, mem, i, c)\n  # If all items have been selected or knapsack has no remaining capacity, return value 0\n  return 0 if i == 0 || c == 0\n  # If there's a record, return it directly\n  return mem[i][c] if mem[i][c] != -1\n  # If exceeds knapsack capacity, can only choose not to put it in\n  return knapsack_dfs_mem(wgt, val, mem, i - 1, c) if wgt[i - 1] > c\n  # Calculate the maximum value of not putting in and putting in item i\n  no = knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n  yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1]\n  # Record and return the larger value of the two options\n  mem[i][c] = [no, yes].max\nend\n

      Figure 14-19 shows the search branches pruned in memoization.

      Figure 14-19 \u00a0 Memoization recursion tree for 0-1 knapsack problem

      "},{"location":"chapter_dynamic_programming/knapsack_problem/#3-method-3-dynamic-programming","title":"3. \u00a0 Method 3: Dynamic Programming","text":"

      Dynamic programming is essentially the process of filling the \\(dp\\) table during state transitions. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
      def knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"0-1 knapsack: Dynamic programming\"\"\"\n    n = len(wgt)\n    # Initialize dp table\n    dp = [[0] * (cap + 1) for _ in range(n + 1)]\n    # State transition\n    for i in range(1, n + 1):\n        for c in range(1, cap + 1):\n            if wgt[i - 1] > c:\n                # If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c]\n            else:\n                # The larger value between not selecting and selecting item i\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\n    return dp[n][cap]\n
      knapsack.cpp
      /* 0-1 knapsack: Dynamic programming */\nint knapsackDP(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // Initialize dp table\n    vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
      knapsack.java
      /* 0-1 knapsack: Dynamic programming */\nint knapsackDP(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // Initialize dp table\n    int[][] dp = new int[n + 1][cap + 1];\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = Math.max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
      knapsack.cs
      /* 0-1 knapsack: Dynamic programming */\nint KnapsackDP(int[] weight, int[] val, int cap) {\n    int n = weight.Length;\n    // Initialize dp table\n    int[,] dp = new int[n + 1, cap + 1];\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (weight[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i, c] = dp[i - 1, c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i, c] = Math.Max(dp[i - 1, c - weight[i - 1]] + val[i - 1], dp[i - 1, c]);\n            }\n        }\n    }\n    return dp[n, cap];\n}\n
      knapsack.go
      /* 0-1 knapsack: Dynamic programming */\nfunc knapsackDP(wgt, val []int, cap int) int {\n    n := len(wgt)\n    // Initialize dp table\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, cap+1)\n    }\n    // State transition\n    for i := 1; i <= n; i++ {\n        for c := 1; c <= cap; c++ {\n            if wgt[i-1] > c {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i-1][c]\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i-1][c-wgt[i-1]]+val[i-1])))\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
      knapsack.swift
      /* 0-1 knapsack: Dynamic programming */\nfunc knapsackDP(wgt: [Int], val: [Int], cap: Int) -> Int {\n    let n = wgt.count\n    // Initialize dp table\n    var dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)\n    // State transition\n    for i in 1 ... n {\n        for c in 1 ... cap {\n            if wgt[i - 1] > c {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c]\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
      knapsack.js
      /* 0-1 knapsack: Dynamic programming */\nfunction knapsackDP(wgt, val, cap) {\n    const n = wgt.length;\n    // Initialize dp table\n    const dp = Array(n + 1)\n        .fill(0)\n        .map(() => Array(cap + 1).fill(0));\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = Math.max(\n                    dp[i - 1][c],\n                    dp[i - 1][c - wgt[i - 1]] + val[i - 1]\n                );\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
      knapsack.ts
      /* 0-1 knapsack: Dynamic programming */\nfunction knapsackDP(\n    wgt: Array<number>,\n    val: Array<number>,\n    cap: number\n): number {\n    const n = wgt.length;\n    // Initialize dp table\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: cap + 1 }, () => 0)\n    );\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = Math.max(\n                    dp[i - 1][c],\n                    dp[i - 1][c - wgt[i - 1]] + val[i - 1]\n                );\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
      knapsack.dart
      /* 0-1 knapsack: Dynamic programming */\nint knapsackDP(List<int> wgt, List<int> val, int cap) {\n  int n = wgt.length;\n  // Initialize dp table\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(cap + 1, 0));\n  // State transition\n  for (int i = 1; i <= n; i++) {\n    for (int c = 1; c <= cap; c++) {\n      if (wgt[i - 1] > c) {\n        // If exceeds knapsack capacity, don't select item i\n        dp[i][c] = dp[i - 1][c];\n      } else {\n        // The larger value between not selecting and selecting item i\n        dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n      }\n    }\n  }\n  return dp[n][cap];\n}\n
      knapsack.rs
      /* 0-1 knapsack: Dynamic programming */\nfn knapsack_dp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n    let n = wgt.len();\n    // Initialize dp table\n    let mut dp = vec![vec![0; cap + 1]; n + 1];\n    // State transition\n    for i in 1..=n {\n        for c in 1..=cap {\n            if wgt[i - 1] > c as i32 {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = std::cmp::max(\n                    dp[i - 1][c],\n                    dp[i - 1][c - wgt[i - 1] as usize] + val[i - 1],\n                );\n            }\n        }\n    }\n    dp[n][cap]\n}\n
      knapsack.c
      /* 0-1 knapsack: Dynamic programming */\nint knapsackDP(int wgt[], int val[], int cap, int wgtSize) {\n    int n = wgtSize;\n    // Initialize dp table\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(cap + 1, sizeof(int));\n    }\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = myMax(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    int res = dp[n][cap];\n    // Free memory\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    return res;\n}\n
      knapsack.kt
      /* 0-1 knapsack: Dynamic programming */\nfun knapsackDP(wgt: IntArray, _val: IntArray, cap: Int): Int {\n    val n = wgt.size\n    // Initialize dp table\n    val dp = Array(n + 1) { IntArray(cap + 1) }\n    // State transition\n    for (i in 1..n) {\n        for (c in 1..cap) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c]\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + _val[i - 1])\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
      knapsack.rb
      ### 0-1 knapsack: dynamic programming ###\ndef knapsack_dp(wgt, val, cap)\n  n = wgt.length\n  # Initialize dp table\n  dp = Array.new(n + 1) { Array.new(cap + 1, 0) }\n  # State transition\n  for i in 1...(n + 1)\n    for c in 1...(cap + 1)\n      if wgt[i - 1] > c\n        # If exceeds knapsack capacity, don't select item i\n        dp[i][c] = dp[i - 1][c]\n      else\n        # The larger value between not selecting and selecting item i\n        dp[i][c] = [dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[n][cap]\nend\n

      As shown in Figure 14-20, both time complexity and space complexity are determined by the size of the array dp, which is \\(O(n \\times cap)\\).

      <1><2><3><4><5><6><7><8><9><10><11><12><13><14>

      Figure 14-20 \u00a0 Dynamic programming process for 0-1 knapsack problem

      "},{"location":"chapter_dynamic_programming/knapsack_problem/#4-space-optimization","title":"4. \u00a0 Space Optimization","text":"

      Since each state is only related to the state in the row above it, we can use two arrays rolling forward to reduce the space complexity from \\(O(n^2)\\) to \\(O(n)\\).

      Further thinking, can we achieve space optimization using just one array? Observing, we can see that each state is transferred from the cell directly above or the cell in the upper-left. If there is only one array, when we start traversing row \\(i\\), that array still stores the state of row \\(i-1\\).

      • If using forward traversal, then when traversing to \\(dp[i, j]\\), the values in the upper-left \\(dp[i-1, 1]\\) ~ \\(dp[i-1, j-1]\\) may have already been overwritten, thus preventing correct state transition.
      • If using reverse traversal, there will be no overwriting issue, and state transition can proceed correctly.

      Figure 14-21 shows the transition process from row \\(i = 1\\) to row \\(i = 2\\) using a single array. Please consider the difference between forward and reverse traversal.

      <1><2><3><4><5><6>

      Figure 14-21 \u00a0 Space-optimized dynamic programming process for 0-1 knapsack

      In the code implementation, we simply need to delete the first dimension \\(i\\) of the array dp and change the inner loop to reverse traversal:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
      def knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"0-1 knapsack: Space-optimized dynamic programming\"\"\"\n    n = len(wgt)\n    # Initialize dp table\n    dp = [0] * (cap + 1)\n    # State transition\n    for i in range(1, n + 1):\n        # Traverse in reverse order\n        for c in range(cap, 0, -1):\n            if wgt[i - 1] > c:\n                # If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c]\n            else:\n                # The larger value between not selecting and selecting item i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n    return dp[cap]\n
      knapsack.cpp
      /* 0-1 knapsack: Space-optimized dynamic programming */\nint knapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // Initialize dp table\n    vector<int> dp(cap + 1, 0);\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        // Traverse in reverse order\n        for (int c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // The larger value between not selecting and selecting item i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
      knapsack.java
      /* 0-1 knapsack: Space-optimized dynamic programming */\nint knapsackDPComp(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // Initialize dp table\n    int[] dp = new int[cap + 1];\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        // Traverse in reverse order\n        for (int c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // The larger value between not selecting and selecting item i\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
      knapsack.cs
      /* 0-1 knapsack: Space-optimized dynamic programming */\nint KnapsackDPComp(int[] weight, int[] val, int cap) {\n    int n = weight.Length;\n    // Initialize dp table\n    int[] dp = new int[cap + 1];\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        // Traverse in reverse order\n        for (int c = cap; c > 0; c--) {\n            if (weight[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[c] = Math.Max(dp[c], dp[c - weight[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
      knapsack.go
      /* 0-1 knapsack: Space-optimized dynamic programming */\nfunc knapsackDPComp(wgt, val []int, cap int) int {\n    n := len(wgt)\n    // Initialize dp table\n    dp := make([]int, cap+1)\n    // State transition\n    for i := 1; i <= n; i++ {\n        // Traverse in reverse order\n        for c := cap; c >= 1; c-- {\n            if wgt[i-1] <= c {\n                // The larger value between not selecting and selecting item i\n                dp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))\n            }\n        }\n    }\n    return dp[cap]\n}\n
      knapsack.swift
      /* 0-1 knapsack: Space-optimized dynamic programming */\nfunc knapsackDPComp(wgt: [Int], val: [Int], cap: Int) -> Int {\n    let n = wgt.count\n    // Initialize dp table\n    var dp = Array(repeating: 0, count: cap + 1)\n    // State transition\n    for i in 1 ... n {\n        // Traverse in reverse order\n        for c in (1 ... cap).reversed() {\n            if wgt[i - 1] <= c {\n                // The larger value between not selecting and selecting item i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n            }\n        }\n    }\n    return dp[cap]\n}\n
      knapsack.js
      /* 0-1 knapsack: Space-optimized dynamic programming */\nfunction knapsackDPComp(wgt, val, cap) {\n    const n = wgt.length;\n    // Initialize dp table\n    const dp = Array(cap + 1).fill(0);\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        // Traverse in reverse order\n        for (let c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // The larger value between not selecting and selecting item i\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
      knapsack.ts
      /* 0-1 knapsack: Space-optimized dynamic programming */\nfunction knapsackDPComp(\n    wgt: Array<number>,\n    val: Array<number>,\n    cap: number\n): number {\n    const n = wgt.length;\n    // Initialize dp table\n    const dp = Array(cap + 1).fill(0);\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        // Traverse in reverse order\n        for (let c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // The larger value between not selecting and selecting item i\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
      knapsack.dart
      /* 0-1 knapsack: Space-optimized dynamic programming */\nint knapsackDPComp(List<int> wgt, List<int> val, int cap) {\n  int n = wgt.length;\n  // Initialize dp table\n  List<int> dp = List.filled(cap + 1, 0);\n  // State transition\n  for (int i = 1; i <= n; i++) {\n    // Traverse in reverse order\n    for (int c = cap; c >= 1; c--) {\n      if (wgt[i - 1] <= c) {\n        // The larger value between not selecting and selecting item i\n        dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n      }\n    }\n  }\n  return dp[cap];\n}\n
      knapsack.rs
      /* 0-1 knapsack: Space-optimized dynamic programming */\nfn knapsack_dp_comp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n    let n = wgt.len();\n    // Initialize dp table\n    let mut dp = vec![0; cap + 1];\n    // State transition\n    for i in 1..=n {\n        // Traverse in reverse order\n        for c in (1..=cap).rev() {\n            if wgt[i - 1] <= c as i32 {\n                // The larger value between not selecting and selecting item i\n                dp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);\n            }\n        }\n    }\n    dp[cap]\n}\n
      knapsack.c
      /* 0-1 knapsack: Space-optimized dynamic programming */\nint knapsackDPComp(int wgt[], int val[], int cap, int wgtSize) {\n    int n = wgtSize;\n    // Initialize dp table\n    int *dp = calloc(cap + 1, sizeof(int));\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        // Traverse in reverse order\n        for (int c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // The larger value between not selecting and selecting item i\n                dp[c] = myMax(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    int res = dp[cap];\n    // Free memory\n    free(dp);\n    return res;\n}\n
      knapsack.kt
      /* 0-1 knapsack: Space-optimized dynamic programming */\nfun knapsackDPComp(wgt: IntArray, _val: IntArray, cap: Int): Int {\n    val n = wgt.size\n    // Initialize dp table\n    val dp = IntArray(cap + 1)\n    // State transition\n    for (i in 1..n) {\n        // Traverse in reverse order\n        for (c in cap downTo 1) {\n            if (wgt[i - 1] <= c) {\n                // The larger value between not selecting and selecting item i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])\n            }\n        }\n    }\n    return dp[cap]\n}\n
      knapsack.rb
      ### 0-1 knapsack: space-optimized DP ###\ndef knapsack_dp_comp(wgt, val, cap)\n  n = wgt.length\n  # Initialize dp table\n  dp = Array.new(cap + 1, 0)\n  # State transition\n  for i in 1...(n + 1)\n    # Traverse in reverse order\n    for c in cap.downto(1)\n      if wgt[i - 1] > c\n        # If exceeds knapsack capacity, don't select item i\n        dp[c] = dp[c]\n      else\n        # The larger value between not selecting and selecting item i\n        dp[c] = [dp[c], dp[c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[cap]\nend\n
      "},{"location":"chapter_dynamic_programming/summary/","title":"14.7 \u00a0 Summary","text":""},{"location":"chapter_dynamic_programming/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • Dynamic programming decomposes problems and avoids redundant computation by storing the solutions to subproblems, thereby significantly improving computational efficiency.
      • Without considering time constraints, all dynamic programming problems can be solved using backtracking (brute force search), but the recursion tree contains a large number of overlapping subproblems, resulting in extremely low efficiency. By introducing a memo list, we can store the solutions to all computed subproblems, ensuring that overlapping subproblems are only computed once.
      • Memoization is a top-down recursive solution, while the corresponding dynamic programming is a bottom-up iterative solution, similar to \"filling in a table\". Since the current state only depends on certain local states, we can eliminate one dimension of the \\(dp\\) table to reduce space complexity.
      • Subproblem decomposition is a general algorithmic approach, with different properties in divide and conquer, dynamic programming, and backtracking.
      • Dynamic programming problems have three major characteristics: overlapping subproblems, optimal substructure, and no aftereffects.
      • If the optimal solution to the original problem can be constructed from the optimal solutions to the subproblems, then it has optimal substructure.
      • No aftereffects means that for a given state, its future development is only related to that state and has nothing to do with all past states. Many combinatorial optimization problems do not have no aftereffects and cannot be quickly solved using dynamic programming.

      Knapsack problem

      • The knapsack problem is one of the most typical dynamic programming problems, with variants such as the 0-1 knapsack, unbounded knapsack, and multiple knapsack.
      • The state definition for the 0-1 knapsack is the maximum value among the first \\(i\\) items in a knapsack of capacity \\(c\\). Based on the two decisions of not putting an item in the knapsack and putting it in, the optimal substructure can be identified and the state transition equation constructed. In space optimization, since each state depends on the state directly above and to the upper-left, the list needs to be traversed in reverse order to avoid overwriting the upper-left state.
      • The unbounded knapsack problem has no limit on the selection quantity of each type of item, so the state transition for choosing to put in an item differs from the 0-1 knapsack problem. Since the state depends on the state directly above and directly to the left, space optimization should use forward traversal.
      • The coin change problem is a variant of the unbounded knapsack problem. It changes from seeking the \"maximum\" value to seeking the \"minimum\" number of coins, so \\(\\max()\\) in the state transition equation should be changed to \\(\\min()\\). It changes from seeking \"not exceeding\" the knapsack capacity to seeking \"exactly\" making up the target amount, so \\(amt + 1\\) is used to represent the invalid solution of \"unable to make up the target amount\".
      • Coin change problem II changes from seeking the \"minimum number of coins\" to seeking the \"number of coin combinations\", so the state transition equation correspondingly changes from \\(\\min()\\) to a summation operator.

      Edit distance problem

      • Edit distance (Levenshtein distance) is used to measure the similarity between two strings, defined as the minimum number of edit steps from one string to another, with edit operations including insert, delete, and replace.
      • The state definition for the edit distance problem is the minimum number of edit steps required to change the first \\(i\\) characters of \\(s\\) into the first \\(j\\) characters of \\(t\\). When \\(s[i] \\ne t[j]\\), there are three decisions: insert, delete, replace, each with corresponding remaining subproblems. From this, the optimal substructure can be identified and the state transition equation constructed. When \\(s[i] = t[j]\\), no edit is required for the current character.
      • In edit distance, the state depends on the state directly above, directly to the left, and to the upper-left, so after space optimization, neither forward nor reverse traversal can correctly perform state transitions. For this reason, we use a variable to temporarily store the upper-left state, thus transforming to a situation equivalent to the unbounded knapsack problem, allowing for forward traversal after space optimization.
      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/","title":"14.5 \u00a0 Unbounded Knapsack Problem","text":"

      In this section, we first solve another common knapsack problem: the unbounded knapsack, and then explore a special case of it: the coin change problem.

      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1451-unbounded-knapsack-problem","title":"14.5.1 \u00a0 Unbounded Knapsack Problem","text":"

      Question

      Given \\(n\\) items, where the weight of the \\(i\\)-th item is \\(wgt[i-1]\\) and its value is \\(val[i-1]\\), and a knapsack with capacity \\(cap\\). Each item can be selected multiple times. What is the maximum value that can be placed in the knapsack within the capacity limit? An example is shown in Figure 14-22.

      Figure 14-22 \u00a0 Example data for unbounded knapsack problem

      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1-dynamic-programming-approach","title":"1. \u00a0 Dynamic Programming Approach","text":"

      The unbounded knapsack problem is very similar to the 0-1 knapsack problem, differing only in that there is no limit on the number of times an item can be selected.

      • In the 0-1 knapsack problem, there is only one of each type of item, so after placing item \\(i\\) in the knapsack, we can only choose from the first \\(i-1\\) items.
      • In the unbounded knapsack problem, the quantity of each type of item is unlimited, so after placing item \\(i\\) in the knapsack, we can still choose from the first \\(i\\) items.

      Under the rules of the unbounded knapsack problem, the changes in state \\([i, c]\\) are divided into two cases.

      • Not putting item \\(i\\): Same as the 0-1 knapsack problem, transfer to \\([i-1, c]\\).
      • Putting item \\(i\\): Different from the 0-1 knapsack problem, transfer to \\([i, c-wgt[i-1]]\\).

      Thus, the state transition equation becomes:

      \\[ dp[i, c] = \\max(dp[i-1, c], dp[i, c - wgt[i-1]] + val[i-1]) \\]"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2-code-implementation","title":"2. \u00a0 Code Implementation","text":"

      Comparing the code for the two problems, there is one change in state transition from \\(i-1\\) to \\(i\\), with everything else identical:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby unbounded_knapsack.py
      def unbounded_knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"Unbounded knapsack: Dynamic programming\"\"\"\n    n = len(wgt)\n    # Initialize dp table\n    dp = [[0] * (cap + 1) for _ in range(n + 1)]\n    # State transition\n    for i in range(1, n + 1):\n        for c in range(1, cap + 1):\n            if wgt[i - 1] > c:\n                # If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c]\n            else:\n                # The larger value between not selecting and selecting item i\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\n    return dp[n][cap]\n
      unbounded_knapsack.cpp
      /* Unbounded knapsack: Dynamic programming */\nint unboundedKnapsackDP(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // Initialize dp table\n    vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
      unbounded_knapsack.java
      /* Unbounded knapsack: Dynamic programming */\nint unboundedKnapsackDP(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // Initialize dp table\n    int[][] dp = new int[n + 1][cap + 1];\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = Math.max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
      unbounded_knapsack.cs
      /* Unbounded knapsack: Dynamic programming */\nint UnboundedKnapsackDP(int[] wgt, int[] val, int cap) {\n    int n = wgt.Length;\n    // Initialize dp table\n    int[,] dp = new int[n + 1, cap + 1];\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i, c] = dp[i - 1, c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i, c] = Math.Max(dp[i - 1, c], dp[i, c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n, cap];\n}\n
      unbounded_knapsack.go
      /* Unbounded knapsack: Dynamic programming */\nfunc unboundedKnapsackDP(wgt, val []int, cap int) int {\n    n := len(wgt)\n    // Initialize dp table\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, cap+1)\n    }\n    // State transition\n    for i := 1; i <= n; i++ {\n        for c := 1; c <= cap; c++ {\n            if wgt[i-1] > c {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i-1][c]\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i][c-wgt[i-1]]+val[i-1])))\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
      unbounded_knapsack.swift
      /* Unbounded knapsack: Dynamic programming */\nfunc unboundedKnapsackDP(wgt: [Int], val: [Int], cap: Int) -> Int {\n    let n = wgt.count\n    // Initialize dp table\n    var dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)\n    // State transition\n    for i in 1 ... n {\n        for c in 1 ... cap {\n            if wgt[i - 1] > c {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c]\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
      unbounded_knapsack.js
      /* Unbounded knapsack: Dynamic programming */\nfunction unboundedKnapsackDP(wgt, val, cap) {\n    const n = wgt.length;\n    // Initialize dp table\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: cap + 1 }, () => 0)\n    );\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = Math.max(\n                    dp[i - 1][c],\n                    dp[i][c - wgt[i - 1]] + val[i - 1]\n                );\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
      unbounded_knapsack.ts
      /* Unbounded knapsack: Dynamic programming */\nfunction unboundedKnapsackDP(\n    wgt: Array<number>,\n    val: Array<number>,\n    cap: number\n): number {\n    const n = wgt.length;\n    // Initialize dp table\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: cap + 1 }, () => 0)\n    );\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = Math.max(\n                    dp[i - 1][c],\n                    dp[i][c - wgt[i - 1]] + val[i - 1]\n                );\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
      unbounded_knapsack.dart
      /* Unbounded knapsack: Dynamic programming */\nint unboundedKnapsackDP(List<int> wgt, List<int> val, int cap) {\n  int n = wgt.length;\n  // Initialize dp table\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(cap + 1, 0));\n  // State transition\n  for (int i = 1; i <= n; i++) {\n    for (int c = 1; c <= cap; c++) {\n      if (wgt[i - 1] > c) {\n        // If exceeds knapsack capacity, don't select item i\n        dp[i][c] = dp[i - 1][c];\n      } else {\n        // The larger value between not selecting and selecting item i\n        dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n      }\n    }\n  }\n  return dp[n][cap];\n}\n
      unbounded_knapsack.rs
      /* Unbounded knapsack: Dynamic programming */\nfn unbounded_knapsack_dp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n    let n = wgt.len();\n    // Initialize dp table\n    let mut dp = vec![vec![0; cap + 1]; n + 1];\n    // State transition\n    for i in 1..=n {\n        for c in 1..=cap {\n            if wgt[i - 1] > c as i32 {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = std::cmp::max(dp[i - 1][c], dp[i][c - wgt[i - 1] as usize] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
      unbounded_knapsack.c
      /* Unbounded knapsack: Dynamic programming */\nint unboundedKnapsackDP(int wgt[], int val[], int cap, int wgtSize) {\n    int n = wgtSize;\n    // Initialize dp table\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(cap + 1, sizeof(int));\n    }\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = myMax(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    int res = dp[n][cap];\n    // Free memory\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    return res;\n}\n
      unbounded_knapsack.kt
      /* Unbounded knapsack: Dynamic programming */\nfun unboundedKnapsackDP(wgt: IntArray, _val: IntArray, cap: Int): Int {\n    val n = wgt.size\n    // Initialize dp table\n    val dp = Array(n + 1) { IntArray(cap + 1) }\n    // State transition\n    for (i in 1..n) {\n        for (c in 1..cap) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c]\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + _val[i - 1])\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
      unbounded_knapsack.rb
      ### Unbounded knapsack: dynamic programming ###\ndef unbounded_knapsack_dp(wgt, val, cap)\n  n = wgt.length\n  # Initialize dp table\n  dp = Array.new(n + 1) { Array.new(cap + 1, 0) }\n  # State transition\n  for i in 1...(n + 1)\n    for c in 1...(cap + 1)\n      if wgt[i - 1] > c\n        # If exceeds knapsack capacity, don't select item i\n        dp[i][c] = dp[i - 1][c]\n      else\n        # The larger value between not selecting and selecting item i\n        dp[i][c] = [dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[n][cap]\nend\n
      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3-space-optimization","title":"3. \u00a0 Space Optimization","text":"

      Since the current state is transferred from states on the left and above, after space optimization, each row in the \\(dp\\) table should be traversed in forward order.

      This traversal order is exactly opposite to the 0-1 knapsack. Please refer to Figure 14-23 to understand the difference between the two.

      <1><2><3><4><5><6>

      Figure 14-23 \u00a0 Space-optimized dynamic programming process for unbounded knapsack problem

      The code implementation is relatively simple, just delete the first dimension of the array dp:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby unbounded_knapsack.py
      def unbounded_knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"Unbounded knapsack: Space-optimized dynamic programming\"\"\"\n    n = len(wgt)\n    # Initialize dp table\n    dp = [0] * (cap + 1)\n    # State transition\n    for i in range(1, n + 1):\n        # Traverse in forward order\n        for c in range(1, cap + 1):\n            if wgt[i - 1] > c:\n                # If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c]\n            else:\n                # The larger value between not selecting and selecting item i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n    return dp[cap]\n
      unbounded_knapsack.cpp
      /* Unbounded knapsack: Space-optimized dynamic programming */\nint unboundedKnapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // Initialize dp table\n    vector<int> dp(cap + 1, 0);\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
      unbounded_knapsack.java
      /* Unbounded knapsack: Space-optimized dynamic programming */\nint unboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // Initialize dp table\n    int[] dp = new int[cap + 1];\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
      unbounded_knapsack.cs
      /* Unbounded knapsack: Space-optimized dynamic programming */\nint UnboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\n    int n = wgt.Length;\n    // Initialize dp table\n    int[] dp = new int[cap + 1];\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[c] = Math.Max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
      unbounded_knapsack.go
      /* Unbounded knapsack: Space-optimized dynamic programming */\nfunc unboundedKnapsackDPComp(wgt, val []int, cap int) int {\n    n := len(wgt)\n    // Initialize dp table\n    dp := make([]int, cap+1)\n    // State transition\n    for i := 1; i <= n; i++ {\n        for c := 1; c <= cap; c++ {\n            if wgt[i-1] > c {\n                // If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c]\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))\n            }\n        }\n    }\n    return dp[cap]\n}\n
      unbounded_knapsack.swift
      /* Unbounded knapsack: Space-optimized dynamic programming */\nfunc unboundedKnapsackDPComp(wgt: [Int], val: [Int], cap: Int) -> Int {\n    let n = wgt.count\n    // Initialize dp table\n    var dp = Array(repeating: 0, count: cap + 1)\n    // State transition\n    for i in 1 ... n {\n        for c in 1 ... cap {\n            if wgt[i - 1] > c {\n                // If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c]\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n            }\n        }\n    }\n    return dp[cap]\n}\n
      unbounded_knapsack.js
      /* Unbounded knapsack: Space-optimized dynamic programming */\nfunction unboundedKnapsackDPComp(wgt, val, cap) {\n    const n = wgt.length;\n    // Initialize dp table\n    const dp = Array.from({ length: cap + 1 }, () => 0);\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
      unbounded_knapsack.ts
      /* Unbounded knapsack: Space-optimized dynamic programming */\nfunction unboundedKnapsackDPComp(\n    wgt: Array<number>,\n    val: Array<number>,\n    cap: number\n): number {\n    const n = wgt.length;\n    // Initialize dp table\n    const dp = Array.from({ length: cap + 1 }, () => 0);\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
      unbounded_knapsack.dart
      /* Unbounded knapsack: Space-optimized dynamic programming */\nint unboundedKnapsackDPComp(List<int> wgt, List<int> val, int cap) {\n  int n = wgt.length;\n  // Initialize dp table\n  List<int> dp = List.filled(cap + 1, 0);\n  // State transition\n  for (int i = 1; i <= n; i++) {\n    for (int c = 1; c <= cap; c++) {\n      if (wgt[i - 1] > c) {\n        // If exceeds knapsack capacity, don't select item i\n        dp[c] = dp[c];\n      } else {\n        // The larger value between not selecting and selecting item i\n        dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n      }\n    }\n  }\n  return dp[cap];\n}\n
      unbounded_knapsack.rs
      /* Unbounded knapsack: Space-optimized dynamic programming */\nfn unbounded_knapsack_dp_comp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n    let n = wgt.len();\n    // Initialize dp table\n    let mut dp = vec![0; cap + 1];\n    // State transition\n    for i in 1..=n {\n        for c in 1..=cap {\n            if wgt[i - 1] > c as i32 {\n                // If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);\n            }\n        }\n    }\n    dp[cap]\n}\n
      unbounded_knapsack.c
      /* Unbounded knapsack: Space-optimized dynamic programming */\nint unboundedKnapsackDPComp(int wgt[], int val[], int cap, int wgtSize) {\n    int n = wgtSize;\n    // Initialize dp table\n    int *dp = calloc(cap + 1, sizeof(int));\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[c] = myMax(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    int res = dp[cap];\n    // Free memory\n    free(dp);\n    return res;\n}\n
      unbounded_knapsack.kt
      /* Unbounded knapsack: Space-optimized dynamic programming */\nfun unboundedKnapsackDPComp(\n    wgt: IntArray,\n    _val: IntArray,\n    cap: Int\n): Int {\n    val n = wgt.size\n    // Initialize dp table\n    val dp = IntArray(cap + 1)\n    // State transition\n    for (i in 1..n) {\n        for (c in 1..cap) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c]\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])\n            }\n        }\n    }\n    return dp[cap]\n}\n
      unbounded_knapsack.rb
      ### Unbounded knapsack: space-optimized DP ###\ndef unbounded_knapsack_dp_comp(wgt, val, cap)\n  n = wgt.length\n  # Initialize dp table\n  dp = Array.new(cap + 1, 0)\n  # State transition\n  for i in 1...(n + 1)\n    # Traverse in forward order\n    for c in 1...(cap + 1)\n      if wgt[i -1] > c\n        # If exceeds knapsack capacity, don't select item i\n        dp[c] = dp[c]\n      else\n        # The larger value between not selecting and selecting item i\n        dp[c] = [dp[c], dp[c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[cap]\nend\n
      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1452-coin-change-problem","title":"14.5.2 \u00a0 Coin Change Problem","text":"

      The knapsack problem represents a large class of dynamic programming problems and has many variants, such as the coin change problem.

      Question

      Given \\(n\\) types of coins, where the denomination of the \\(i\\)-th type of coin is \\(coins[i - 1]\\), and the target amount is \\(amt\\). Each type of coin can be selected multiple times. What is the minimum number of coins needed to make up the target amount? If it is impossible to make up the target amount, return \\(-1\\). An example is shown in Figure 14-24.

      Figure 14-24 \u00a0 Example data for coin change problem

      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1-dynamic-programming-approach_1","title":"1. \u00a0 Dynamic Programming Approach","text":"

      The coin change problem can be viewed as a special case of the unbounded knapsack problem, with the following connections and differences.

      • The two problems can be converted to each other: \"item\" corresponds to \"coin\", \"item weight\" corresponds to \"coin denomination\", and \"knapsack capacity\" corresponds to \"target amount\".
      • The optimization goals are opposite: the unbounded knapsack problem aims to maximize item value, while the coin change problem aims to minimize the number of coins.
      • The unbounded knapsack problem seeks solutions \"not exceeding\" the knapsack capacity, while the coin change problem seeks solutions that \"exactly\" make up the target amount.

      Step 1: Think about the decisions in each round, define the state, and thus obtain the \\(dp\\) table

      State \\([i, a]\\) corresponds to the subproblem: the minimum number of coins among the first \\(i\\) types of coins that can make up amount \\(a\\), denoted as \\(dp[i, a]\\).

      The two-dimensional \\(dp\\) table has size \\((n+1) \\times (amt+1)\\).

      Step 2: Identify the optimal substructure, and then derive the state transition equation

      This problem differs from the unbounded knapsack problem in the following two aspects regarding the state transition equation.

      • This problem seeks the minimum value, so the operator \\(\\max()\\) needs to be changed to \\(\\min()\\).
      • The optimization target is the number of coins rather than item value, so when a coin is selected, simply execute \\(+1\\).
      \\[ dp[i, a] = \\min(dp[i-1, a], dp[i, a - coins[i-1]] + 1) \\]

      Step 3: Determine boundary conditions and state transition order

      When the target amount is \\(0\\), the minimum number of coins needed to make it up is \\(0\\), so all \\(dp[i, 0]\\) in the first column equal \\(0\\).

      When there are no coins, it is impossible to make up any amount \\(> 0\\), which is an invalid solution. To enable the \\(\\min()\\) function in the state transition equation to identify and filter out invalid solutions, we consider using \\(+ \\infty\\) to represent them, i.e., set all \\(dp[0, a]\\) in the first row to \\(+ \\infty\\).

      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2-code-implementation_1","title":"2. \u00a0 Code Implementation","text":"

      Most programming languages do not provide a \\(+ \\infty\\) variable, and can only use the maximum value of integer type int as a substitute. However, this can lead to large number overflow: the \\(+ 1\\) operation in the state transition equation may cause overflow.

      For this reason, we use the number \\(amt + 1\\) to represent invalid solutions, because the maximum number of coins needed to make up \\(amt\\) is at most \\(amt\\). Before returning, check whether \\(dp[n, amt]\\) equals \\(amt + 1\\); if so, return \\(-1\\), indicating that the target amount cannot be made up. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change.py
      def coin_change_dp(coins: list[int], amt: int) -> int:\n    \"\"\"Coin change: Dynamic programming\"\"\"\n    n = len(coins)\n    MAX = amt + 1\n    # Initialize dp table\n    dp = [[0] * (amt + 1) for _ in range(n + 1)]\n    # State transition: first row and first column\n    for a in range(1, amt + 1):\n        dp[0][a] = MAX\n    # State transition: rest of the rows and columns\n    for i in range(1, n + 1):\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a]\n            else:\n                # The smaller value between not selecting and selecting coin i\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n    return dp[n][amt] if dp[n][amt] != MAX else -1\n
      coin_change.cpp
      /* Coin change: Dynamic programming */\nint coinChangeDP(vector<int> &coins, int amt) {\n    int n = coins.size();\n    int MAX = amt + 1;\n    // Initialize dp table\n    vector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n    // State transition: first row and first column\n    for (int a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
      coin_change.java
      /* Coin change: Dynamic programming */\nint coinChangeDP(int[] coins, int amt) {\n    int n = coins.length;\n    int MAX = amt + 1;\n    // Initialize dp table\n    int[][] dp = new int[n + 1][amt + 1];\n    // State transition: first row and first column\n    for (int a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
      coin_change.cs
      /* Coin change: Dynamic programming */\nint CoinChangeDP(int[] coins, int amt) {\n    int n = coins.Length;\n    int MAX = amt + 1;\n    // Initialize dp table\n    int[,] dp = new int[n + 1, amt + 1];\n    // State transition: first row and first column\n    for (int a = 1; a <= amt; a++) {\n        dp[0, a] = MAX;\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i, a] = dp[i - 1, a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[i, a] = Math.Min(dp[i - 1, a], dp[i, a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n, amt] != MAX ? dp[n, amt] : -1;\n}\n
      coin_change.go
      /* Coin change: Dynamic programming */\nfunc coinChangeDP(coins []int, amt int) int {\n    n := len(coins)\n    max := amt + 1\n    // Initialize dp table\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, amt+1)\n    }\n    // State transition: first row and first column\n    for a := 1; a <= amt; a++ {\n        dp[0][a] = max\n    }\n    // State transition: rest of the rows and columns\n    for i := 1; i <= n; i++ {\n        for a := 1; a <= amt; a++ {\n            if coins[i-1] > a {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i-1][a]\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[i][a] = int(math.Min(float64(dp[i-1][a]), float64(dp[i][a-coins[i-1]]+1)))\n            }\n        }\n    }\n    if dp[n][amt] != max {\n        return dp[n][amt]\n    }\n    return -1\n}\n
      coin_change.swift
      /* Coin change: Dynamic programming */\nfunc coinChangeDP(coins: [Int], amt: Int) -> Int {\n    let n = coins.count\n    let MAX = amt + 1\n    // Initialize dp table\n    var dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)\n    // State transition: first row and first column\n    for a in 1 ... amt {\n        dp[0][a] = MAX\n    }\n    // State transition: rest of the rows and columns\n    for i in 1 ... n {\n        for a in 1 ... amt {\n            if coins[i - 1] > a {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a]\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n            }\n        }\n    }\n    return dp[n][amt] != MAX ? dp[n][amt] : -1\n}\n
      coin_change.js
      /* Coin change: Dynamic programming */\nfunction coinChangeDP(coins, amt) {\n    const n = coins.length;\n    const MAX = amt + 1;\n    // Initialize dp table\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: amt + 1 }, () => 0)\n    );\n    // State transition: first row and first column\n    for (let a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // State transition: rest of the rows and columns\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] !== MAX ? dp[n][amt] : -1;\n}\n
      coin_change.ts
      /* Coin change: Dynamic programming */\nfunction coinChangeDP(coins: Array<number>, amt: number): number {\n    const n = coins.length;\n    const MAX = amt + 1;\n    // Initialize dp table\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: amt + 1 }, () => 0)\n    );\n    // State transition: first row and first column\n    for (let a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // State transition: rest of the rows and columns\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] !== MAX ? dp[n][amt] : -1;\n}\n
      coin_change.dart
      /* Coin change: Dynamic programming */\nint coinChangeDP(List<int> coins, int amt) {\n  int n = coins.length;\n  int MAX = amt + 1;\n  // Initialize dp table\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(amt + 1, 0));\n  // State transition: first row and first column\n  for (int a = 1; a <= amt; a++) {\n    dp[0][a] = MAX;\n  }\n  // State transition: rest of the rows and columns\n  for (int i = 1; i <= n; i++) {\n    for (int a = 1; a <= amt; a++) {\n      if (coins[i - 1] > a) {\n        // If exceeds target amount, don't select coin i\n        dp[i][a] = dp[i - 1][a];\n      } else {\n        // The smaller value between not selecting and selecting coin i\n        dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n      }\n    }\n  }\n  return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
      coin_change.rs
      /* Coin change: Dynamic programming */\nfn coin_change_dp(coins: &[i32], amt: usize) -> i32 {\n    let n = coins.len();\n    let max = amt + 1;\n    // Initialize dp table\n    let mut dp = vec![vec![0; amt + 1]; n + 1];\n    // State transition: first row and first column\n    for a in 1..=amt {\n        dp[0][a] = max;\n    }\n    // State transition: rest of the rows and columns\n    for i in 1..=n {\n        for a in 1..=amt {\n            if coins[i - 1] > a as i32 {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[i][a] = std::cmp::min(dp[i - 1][a], dp[i][a - coins[i - 1] as usize] + 1);\n            }\n        }\n    }\n    if dp[n][amt] != max {\n        return dp[n][amt] as i32;\n    } else {\n        -1\n    }\n}\n
      coin_change.c
      /* Coin change: Dynamic programming */\nint coinChangeDP(int coins[], int amt, int coinsSize) {\n    int n = coinsSize;\n    int MAX = amt + 1;\n    // Initialize dp table\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(amt + 1, sizeof(int));\n    }\n    // State transition: first row and first column\n    for (int a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[i][a] = myMin(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    int res = dp[n][amt] != MAX ? dp[n][amt] : -1;\n    // Free memory\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    free(dp);\n    return res;\n}\n
      coin_change.kt
      /* Coin change: Dynamic programming */\nfun coinChangeDP(coins: IntArray, amt: Int): Int {\n    val n = coins.size\n    val MAX = amt + 1\n    // Initialize dp table\n    val dp = Array(n + 1) { IntArray(amt + 1) }\n    // State transition: first row and first column\n    for (a in 1..amt) {\n        dp[0][a] = MAX\n    }\n    // State transition: rest of the rows and columns\n    for (i in 1..n) {\n        for (a in 1..amt) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a]\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n            }\n        }\n    }\n    return if (dp[n][amt] != MAX) dp[n][amt] else -1\n}\n
      coin_change.rb
      ### Coin change: dynamic programming ###\ndef coin_change_dp(coins, amt)\n  n = coins.length\n  _MAX = amt + 1\n  # Initialize dp table\n  dp = Array.new(n + 1) { Array.new(amt + 1, 0) }\n  # State transition: first row and first column\n  (1...(amt + 1)).each { |a| dp[0][a] = _MAX }\n  # State transition: rest of the rows and columns\n  for i in 1...(n + 1)\n    for a in 1...(amt + 1)\n      if coins[i - 1] > a\n        # If exceeds target amount, don't select coin i\n        dp[i][a] = dp[i - 1][a]\n      else\n        # The smaller value between not selecting and selecting coin i\n        dp[i][a] = [dp[i - 1][a], dp[i][a - coins[i - 1]] + 1].min\n      end\n    end\n  end\n  dp[n][amt] != _MAX ? dp[n][amt] : -1\nend\n

      Figure 14-25 shows the dynamic programming process for coin change, which is very similar to the unbounded knapsack problem.

      <1><2><3><4><5><6><7><8><9><10><11><12><13><14><15>

      Figure 14-25 \u00a0 Dynamic programming process for coin change problem

      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3-space-optimization_1","title":"3. \u00a0 Space Optimization","text":"

      The space optimization for the coin change problem is handled in the same way as the unbounded knapsack problem:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change.py
      def coin_change_dp_comp(coins: list[int], amt: int) -> int:\n    \"\"\"Coin change: Space-optimized dynamic programming\"\"\"\n    n = len(coins)\n    MAX = amt + 1\n    # Initialize dp table\n    dp = [MAX] * (amt + 1)\n    dp[0] = 0\n    # State transition\n    for i in range(1, n + 1):\n        # Traverse in forward order\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # If exceeds target amount, don't select coin i\n                dp[a] = dp[a]\n            else:\n                # The smaller value between not selecting and selecting coin i\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n    return dp[amt] if dp[amt] != MAX else -1\n
      coin_change.cpp
      /* Coin change: Space-optimized dynamic programming */\nint coinChangeDPComp(vector<int> &coins, int amt) {\n    int n = coins.size();\n    int MAX = amt + 1;\n    // Initialize dp table\n    vector<int> dp(amt + 1, MAX);\n    dp[0] = 0;\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1;\n}\n
      coin_change.java
      /* Coin change: Space-optimized dynamic programming */\nint coinChangeDPComp(int[] coins, int amt) {\n    int n = coins.length;\n    int MAX = amt + 1;\n    // Initialize dp table\n    int[] dp = new int[amt + 1];\n    Arrays.fill(dp, MAX);\n    dp[0] = 0;\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1;\n}\n
      coin_change.cs
      /* Coin change: Space-optimized dynamic programming */\nint CoinChangeDPComp(int[] coins, int amt) {\n    int n = coins.Length;\n    int MAX = amt + 1;\n    // Initialize dp table\n    int[] dp = new int[amt + 1];\n    Array.Fill(dp, MAX);\n    dp[0] = 0;\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[a] = Math.Min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1;\n}\n
      coin_change.go
      /* Coin change: Dynamic programming */\nfunc coinChangeDPComp(coins []int, amt int) int {\n    n := len(coins)\n    max := amt + 1\n    // Initialize dp table\n    dp := make([]int, amt+1)\n    for i := 1; i <= amt; i++ {\n        dp[i] = max\n    }\n    // State transition\n    for i := 1; i <= n; i++ {\n        // Traverse in forward order\n        for a := 1; a <= amt; a++ {\n            if coins[i-1] > a {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a]\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[a] = int(math.Min(float64(dp[a]), float64(dp[a-coins[i-1]]+1)))\n            }\n        }\n    }\n    if dp[amt] != max {\n        return dp[amt]\n    }\n    return -1\n}\n
      coin_change.swift
      /* Coin change: Space-optimized dynamic programming */\nfunc coinChangeDPComp(coins: [Int], amt: Int) -> Int {\n    let n = coins.count\n    let MAX = amt + 1\n    // Initialize dp table\n    var dp = Array(repeating: MAX, count: amt + 1)\n    dp[0] = 0\n    // State transition\n    for i in 1 ... n {\n        for a in 1 ... amt {\n            if coins[i - 1] > a {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a]\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1\n}\n
      coin_change.js
      /* Coin change: Space-optimized dynamic programming */\nfunction coinChangeDPComp(coins, amt) {\n    const n = coins.length;\n    const MAX = amt + 1;\n    // Initialize dp table\n    const dp = Array.from({ length: amt + 1 }, () => MAX);\n    dp[0] = 0;\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] !== MAX ? dp[amt] : -1;\n}\n
      coin_change.ts
      /* Coin change: Space-optimized dynamic programming */\nfunction coinChangeDPComp(coins: Array<number>, amt: number): number {\n    const n = coins.length;\n    const MAX = amt + 1;\n    // Initialize dp table\n    const dp = Array.from({ length: amt + 1 }, () => MAX);\n    dp[0] = 0;\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] !== MAX ? dp[amt] : -1;\n}\n
      coin_change.dart
      /* Coin change: Space-optimized dynamic programming */\nint coinChangeDPComp(List<int> coins, int amt) {\n  int n = coins.length;\n  int MAX = amt + 1;\n  // Initialize dp table\n  List<int> dp = List.filled(amt + 1, MAX);\n  dp[0] = 0;\n  // State transition\n  for (int i = 1; i <= n; i++) {\n    for (int a = 1; a <= amt; a++) {\n      if (coins[i - 1] > a) {\n        // If exceeds target amount, don't select coin i\n        dp[a] = dp[a];\n      } else {\n        // The smaller value between not selecting and selecting coin i\n        dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n      }\n    }\n  }\n  return dp[amt] != MAX ? dp[amt] : -1;\n}\n
      coin_change.rs
      /* Coin change: Space-optimized dynamic programming */\nfn coin_change_dp_comp(coins: &[i32], amt: usize) -> i32 {\n    let n = coins.len();\n    let max = amt + 1;\n    // Initialize dp table\n    let mut dp = vec![0; amt + 1];\n    dp.fill(max);\n    dp[0] = 0;\n    // State transition\n    for i in 1..=n {\n        for a in 1..=amt {\n            if coins[i - 1] > a as i32 {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[a] = std::cmp::min(dp[a], dp[a - coins[i - 1] as usize] + 1);\n            }\n        }\n    }\n    if dp[amt] != max {\n        return dp[amt] as i32;\n    } else {\n        -1\n    }\n}\n
      coin_change.c
      /* Coin change: Space-optimized dynamic programming */\nint coinChangeDPComp(int coins[], int amt, int coinsSize) {\n    int n = coinsSize;\n    int MAX = amt + 1;\n    // Initialize dp table\n    int *dp = malloc((amt + 1) * sizeof(int));\n    for (int j = 1; j <= amt; j++) {\n        dp[j] = MAX;\n    } \n    dp[0] = 0;\n\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[a] = myMin(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    int res = dp[amt] != MAX ? dp[amt] : -1;\n    // Free memory\n    free(dp);\n    return res;\n}\n
      coin_change.kt
      /* Coin change: Space-optimized dynamic programming */\nfun coinChangeDPComp(coins: IntArray, amt: Int): Int {\n    val n = coins.size\n    val MAX = amt + 1\n    // Initialize dp table\n    val dp = IntArray(amt + 1)\n    dp.fill(MAX)\n    dp[0] = 0\n    // State transition\n    for (i in 1..n) {\n        for (a in 1..amt) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a]\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n            }\n        }\n    }\n    return if (dp[amt] != MAX) dp[amt] else -1\n}\n
      coin_change.rb
      ### Coin change: space-optimized DP ###\ndef coin_change_dp_comp(coins, amt)\n  n = coins.length\n  _MAX = amt + 1\n  # Initialize dp table\n  dp = Array.new(amt + 1, _MAX)\n  dp[0] = 0\n  # State transition\n  for i in 1...(n + 1)\n    # Traverse in forward order\n    for a in 1...(amt + 1)\n      if coins[i - 1] > a\n        # If exceeds target amount, don't select coin i\n        dp[a] = dp[a]\n      else\n        # The smaller value between not selecting and selecting coin i\n        dp[a] = [dp[a], dp[a - coins[i - 1]] + 1].min\n      end\n    end\n  end\n  dp[amt] != _MAX ? dp[amt] : -1\nend\n
      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1453-coin-change-problem-ii","title":"14.5.3 \u00a0 Coin Change Problem Ii","text":"

      Question

      Given \\(n\\) types of coins, where the denomination of the \\(i\\)-th type of coin is \\(coins[i - 1]\\), and the target amount is \\(amt\\). Each type of coin can be selected multiple times. What is the number of coin combinations that can make up the target amount? An example is shown in Figure 14-26.

      Figure 14-26 \u00a0 Example data for coin change problem II

      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1-dynamic-programming-approach_2","title":"1. \u00a0 Dynamic Programming Approach","text":"

      Compared to the previous problem, this problem's goal is to find the number of combinations, so the subproblem becomes: the number of combinations among the first \\(i\\) types of coins that can make up amount \\(a\\). The \\(dp\\) table remains a two-dimensional matrix of size \\((n+1) \\times (amt + 1)\\).

      The number of combinations for the current state equals the sum of the combinations from not selecting the current coin and selecting the current coin. The state transition equation is:

      \\[ dp[i, a] = dp[i-1, a] + dp[i, a - coins[i-1]] \\]

      When the target amount is \\(0\\), no coins need to be selected to make up the target amount, so all \\(dp[i, 0]\\) in the first column should be initialized to \\(1\\). When there are no coins, it is impossible to make up any amount \\(>0\\), so all \\(dp[0, a]\\) in the first row equal \\(0\\).

      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2-code-implementation_2","title":"2. \u00a0 Code Implementation","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change_ii.py
      def coin_change_ii_dp(coins: list[int], amt: int) -> int:\n    \"\"\"Coin change II: Dynamic programming\"\"\"\n    n = len(coins)\n    # Initialize dp table\n    dp = [[0] * (amt + 1) for _ in range(n + 1)]\n    # Initialize first column\n    for i in range(n + 1):\n        dp[i][0] = 1\n    # State transition\n    for i in range(1, n + 1):\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a]\n            else:\n                # Sum of the two options: not selecting and selecting coin i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n    return dp[n][amt]\n
      coin_change_ii.cpp
      /* Coin change II: Dynamic programming */\nint coinChangeIIDP(vector<int> &coins, int amt) {\n    int n = coins.size();\n    // Initialize dp table\n    vector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n    // Initialize first column\n    for (int i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
      coin_change_ii.java
      /* Coin change II: Dynamic programming */\nint coinChangeIIDP(int[] coins, int amt) {\n    int n = coins.length;\n    // Initialize dp table\n    int[][] dp = new int[n + 1][amt + 1];\n    // Initialize first column\n    for (int i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
      coin_change_ii.cs
      /* Coin change II: Dynamic programming */\nint CoinChangeIIDP(int[] coins, int amt) {\n    int n = coins.Length;\n    // Initialize dp table\n    int[,] dp = new int[n + 1, amt + 1];\n    // Initialize first column\n    for (int i = 0; i <= n; i++) {\n        dp[i, 0] = 1;\n    }\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i, a] = dp[i - 1, a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[i, a] = dp[i - 1, a] + dp[i, a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n, amt];\n}\n
      coin_change_ii.go
      /* Coin change II: Dynamic programming */\nfunc coinChangeIIDP(coins []int, amt int) int {\n    n := len(coins)\n    // Initialize dp table\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, amt+1)\n    }\n    // Initialize first column\n    for i := 0; i <= n; i++ {\n        dp[i][0] = 1\n    }\n    // State transition: rest of the rows and columns\n    for i := 1; i <= n; i++ {\n        for a := 1; a <= amt; a++ {\n            if coins[i-1] > a {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i-1][a]\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[i][a] = dp[i-1][a] + dp[i][a-coins[i-1]]\n            }\n        }\n    }\n    return dp[n][amt]\n}\n
      coin_change_ii.swift
      /* Coin change II: Dynamic programming */\nfunc coinChangeIIDP(coins: [Int], amt: Int) -> Int {\n    let n = coins.count\n    // Initialize dp table\n    var dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)\n    // Initialize first column\n    for i in 0 ... n {\n        dp[i][0] = 1\n    }\n    // State transition\n    for i in 1 ... n {\n        for a in 1 ... amt {\n            if coins[i - 1] > a {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a]\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n            }\n        }\n    }\n    return dp[n][amt]\n}\n
      coin_change_ii.js
      /* Coin change II: Dynamic programming */\nfunction coinChangeIIDP(coins, amt) {\n    const n = coins.length;\n    // Initialize dp table\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: amt + 1 }, () => 0)\n    );\n    // Initialize first column\n    for (let i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
      coin_change_ii.ts
      /* Coin change II: Dynamic programming */\nfunction coinChangeIIDP(coins: Array<number>, amt: number): number {\n    const n = coins.length;\n    // Initialize dp table\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: amt + 1 }, () => 0)\n    );\n    // Initialize first column\n    for (let i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
      coin_change_ii.dart
      /* Coin change II: Dynamic programming */\nint coinChangeIIDP(List<int> coins, int amt) {\n  int n = coins.length;\n  // Initialize dp table\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(amt + 1, 0));\n  // Initialize first column\n  for (int i = 0; i <= n; i++) {\n    dp[i][0] = 1;\n  }\n  // State transition\n  for (int i = 1; i <= n; i++) {\n    for (int a = 1; a <= amt; a++) {\n      if (coins[i - 1] > a) {\n        // If exceeds target amount, don't select coin i\n        dp[i][a] = dp[i - 1][a];\n      } else {\n        // Sum of the two options: not selecting and selecting coin i\n        dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n      }\n    }\n  }\n  return dp[n][amt];\n}\n
      coin_change_ii.rs
      /* Coin change II: Dynamic programming */\nfn coin_change_ii_dp(coins: &[i32], amt: usize) -> i32 {\n    let n = coins.len();\n    // Initialize dp table\n    let mut dp = vec![vec![0; amt + 1]; n + 1];\n    // Initialize first column\n    for i in 0..=n {\n        dp[i][0] = 1;\n    }\n    // State transition\n    for i in 1..=n {\n        for a in 1..=amt {\n            if coins[i - 1] > a as i32 {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1] as usize];\n            }\n        }\n    }\n    dp[n][amt]\n}\n
      coin_change_ii.c
      /* Coin change II: Dynamic programming */\nint coinChangeIIDP(int coins[], int amt, int coinsSize) {\n    int n = coinsSize;\n    // Initialize dp table\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(amt + 1, sizeof(int));\n    }\n    // Initialize first column\n    for (int i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    int res = dp[n][amt];\n    // Free memory\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    free(dp);\n    return res;\n}\n
      coin_change_ii.kt
      /* Coin change II: Dynamic programming */\nfun coinChangeIIDP(coins: IntArray, amt: Int): Int {\n    val n = coins.size\n    // Initialize dp table\n    val dp = Array(n + 1) { IntArray(amt + 1) }\n    // Initialize first column\n    for (i in 0..n) {\n        dp[i][0] = 1\n    }\n    // State transition\n    for (i in 1..n) {\n        for (a in 1..amt) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a]\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n            }\n        }\n    }\n    return dp[n][amt]\n}\n
      coin_change_ii.rb
      ### Coin change II: dynamic programming ###\ndef coin_change_ii_dp(coins, amt)\n  n = coins.length\n  # Initialize dp table\n  dp = Array.new(n + 1) { Array.new(amt + 1, 0) }\n  # Initialize first column\n  (0...(n + 1)).each { |i| dp[i][0] = 1 }\n  # State transition\n  for i in 1...(n + 1)\n    for a in 1...(amt + 1)\n      if coins[i - 1] > a\n        # If exceeds target amount, don't select coin i\n        dp[i][a] = dp[i - 1][a]\n      else\n        # Sum of the two options: not selecting and selecting coin i\n        dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n      end\n    end\n  end\n  dp[n][amt]\nend\n
      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3-space-optimization_2","title":"3. \u00a0 Space Optimization","text":"

      The space optimization is handled in the same way, just delete the coin dimension:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change_ii.py
      def coin_change_ii_dp_comp(coins: list[int], amt: int) -> int:\n    \"\"\"Coin change II: Space-optimized dynamic programming\"\"\"\n    n = len(coins)\n    # Initialize dp table\n    dp = [0] * (amt + 1)\n    dp[0] = 1\n    # State transition\n    for i in range(1, n + 1):\n        # Traverse in forward order\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # If exceeds target amount, don't select coin i\n                dp[a] = dp[a]\n            else:\n                # Sum of the two options: not selecting and selecting coin i\n                dp[a] = dp[a] + dp[a - coins[i - 1]]\n    return dp[amt]\n
      coin_change_ii.cpp
      /* Coin change II: Space-optimized dynamic programming */\nint coinChangeIIDPComp(vector<int> &coins, int amt) {\n    int n = coins.size();\n    // Initialize dp table\n    vector<int> dp(amt + 1, 0);\n    dp[0] = 1;\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
      coin_change_ii.java
      /* Coin change II: Space-optimized dynamic programming */\nint coinChangeIIDPComp(int[] coins, int amt) {\n    int n = coins.length;\n    // Initialize dp table\n    int[] dp = new int[amt + 1];\n    dp[0] = 1;\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
      coin_change_ii.cs
      /* Coin change II: Space-optimized dynamic programming */\nint CoinChangeIIDPComp(int[] coins, int amt) {\n    int n = coins.Length;\n    // Initialize dp table\n    int[] dp = new int[amt + 1];\n    dp[0] = 1;\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
      coin_change_ii.go
      /* Coin change II: Space-optimized dynamic programming */\nfunc coinChangeIIDPComp(coins []int, amt int) int {\n    n := len(coins)\n    // Initialize dp table\n    dp := make([]int, amt+1)\n    dp[0] = 1\n    // State transition\n    for i := 1; i <= n; i++ {\n        // Traverse in forward order\n        for a := 1; a <= amt; a++ {\n            if coins[i-1] > a {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a]\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[a] = dp[a] + dp[a-coins[i-1]]\n            }\n        }\n    }\n    return dp[amt]\n}\n
      coin_change_ii.swift
      /* Coin change II: Space-optimized dynamic programming */\nfunc coinChangeIIDPComp(coins: [Int], amt: Int) -> Int {\n    let n = coins.count\n    // Initialize dp table\n    var dp = Array(repeating: 0, count: amt + 1)\n    dp[0] = 1\n    // State transition\n    for i in 1 ... n {\n        for a in 1 ... amt {\n            if coins[i - 1] > a {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a]\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[a] = dp[a] + dp[a - coins[i - 1]]\n            }\n        }\n    }\n    return dp[amt]\n}\n
      coin_change_ii.js
      /* Coin change II: Space-optimized dynamic programming */\nfunction coinChangeIIDPComp(coins, amt) {\n    const n = coins.length;\n    // Initialize dp table\n    const dp = Array.from({ length: amt + 1 }, () => 0);\n    dp[0] = 1;\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
      coin_change_ii.ts
      /* Coin change II: Space-optimized dynamic programming */\nfunction coinChangeIIDPComp(coins: Array<number>, amt: number): number {\n    const n = coins.length;\n    // Initialize dp table\n    const dp = Array.from({ length: amt + 1 }, () => 0);\n    dp[0] = 1;\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
      coin_change_ii.dart
      /* Coin change II: Space-optimized dynamic programming */\nint coinChangeIIDPComp(List<int> coins, int amt) {\n  int n = coins.length;\n  // Initialize dp table\n  List<int> dp = List.filled(amt + 1, 0);\n  dp[0] = 1;\n  // State transition\n  for (int i = 1; i <= n; i++) {\n    for (int a = 1; a <= amt; a++) {\n      if (coins[i - 1] > a) {\n        // If exceeds target amount, don't select coin i\n        dp[a] = dp[a];\n      } else {\n        // Sum of the two options: not selecting and selecting coin i\n        dp[a] = dp[a] + dp[a - coins[i - 1]];\n      }\n    }\n  }\n  return dp[amt];\n}\n
      coin_change_ii.rs
      /* Coin change II: Space-optimized dynamic programming */\nfn coin_change_ii_dp_comp(coins: &[i32], amt: usize) -> i32 {\n    let n = coins.len();\n    // Initialize dp table\n    let mut dp = vec![0; amt + 1];\n    dp[0] = 1;\n    // State transition\n    for i in 1..=n {\n        for a in 1..=amt {\n            if coins[i - 1] > a as i32 {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[a] = dp[a] + dp[a - coins[i - 1] as usize];\n            }\n        }\n    }\n    dp[amt]\n}\n
      coin_change_ii.c
      /* Coin change II: Space-optimized dynamic programming */\nint coinChangeIIDPComp(int coins[], int amt, int coinsSize) {\n    int n = coinsSize;\n    // Initialize dp table\n    int *dp = calloc(amt + 1, sizeof(int));\n    dp[0] = 1;\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    int res = dp[amt];\n    // Free memory\n    free(dp);\n    return res;\n}\n
      coin_change_ii.kt
      /* Coin change II: Space-optimized dynamic programming */\nfun coinChangeIIDPComp(coins: IntArray, amt: Int): Int {\n    val n = coins.size\n    // Initialize dp table\n    val dp = IntArray(amt + 1)\n    dp[0] = 1\n    // State transition\n    for (i in 1..n) {\n        for (a in 1..amt) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a]\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[a] = dp[a] + dp[a - coins[i - 1]]\n            }\n        }\n    }\n    return dp[amt]\n}\n
      coin_change_ii.rb
      ### Coin change II: space-optimized DP ###\ndef coin_change_ii_dp_comp(coins, amt)\n  n = coins.length\n  # Initialize dp table\n  dp = Array.new(amt + 1, 0)\n  dp[0] = 1\n  # State transition\n  for i in 1...(n + 1)\n    # Traverse in forward order\n    for a in 1...(amt + 1)\n      if coins[i - 1] > a\n        # If exceeds target amount, don't select coin i\n        dp[a] = dp[a]\n      else\n        # Sum of the two options: not selecting and selecting coin i\n        dp[a] = dp[a] + dp[a - coins[i - 1]]\n      end\n    end\n  end\n  dp[amt]\nend\n
      "},{"location":"chapter_graph/","title":"Chapter 9. \u00a0 Graph","text":"

      Abstract

      In the journey of life, we are like nodes, connected by countless invisible edges.

      Each encounter and parting leaves a unique mark on this vast network graph.

      "},{"location":"chapter_graph/#chapter-contents","title":"Chapter contents","text":"
      • 9.1 \u00a0 Graph
      • 9.2 \u00a0 Basic Operations on Graphs
      • 9.3 \u00a0 Graph Traversal
      • 9.4 \u00a0 Summary
      "},{"location":"chapter_graph/graph/","title":"9.1 \u00a0 Graph","text":"

      A graph is a nonlinear data structure consisting of vertices and edges. We can abstractly represent a graph \\(G\\) as a set of vertices \\(V\\) and a set of edges \\(E\\). The following example shows a graph containing 5 vertices and 7 edges.

      \\[ \\begin{aligned} V & = \\{ 1, 2, 3, 4, 5 \\} \\newline E & = \\{ (1,2), (1,3), (1,5), (2,3), (2,4), (2,5), (4,5) \\} \\newline G & = \\{ V, E \\} \\newline \\end{aligned} \\]

      If we view vertices as nodes and edges as references (pointers) connecting the nodes, we can see graphs as a data structure extended from linked lists. As shown in Figure 9-1, compared to linear relationships (linked lists) and divide-and-conquer relationships (trees), network relationships (graphs) have a higher degree of freedom and are therefore more complex.

      Figure 9-1 \u00a0 Relationships among linked lists, trees, and graphs

      "},{"location":"chapter_graph/graph/#911-common-types-and-terminology-of-graphs","title":"9.1.1 \u00a0 Common Types and Terminology of Graphs","text":"

      Graphs can be divided into undirected graphs and directed graphs based on whether edges have direction, as shown in Figure 9-2.

      • In undirected graphs, edges represent a \"bidirectional\" connection between two vertices, such as the \"friend relationship\" on WeChat or QQ.
      • In directed graphs, edges have directionality, meaning edges \\(A \\rightarrow B\\) and \\(A \\leftarrow B\\) are independent of each other, such as the \"follow\" and \"be followed\" relationships on Weibo or TikTok.

      Figure 9-2 \u00a0 Directed and undirected graphs

      Graphs can be divided into connected graphs and disconnected graphs based on whether all vertices are connected, as shown in Figure 9-3.

      • For connected graphs, starting from any vertex, all other vertices can be reached.
      • For disconnected graphs, starting from a certain vertex, at least one vertex cannot be reached.

      Figure 9-3 \u00a0 Connected and disconnected graphs

      We can also add a \"weight\" variable to edges, resulting in weighted graphs as shown in Figure 9-4. For example, in mobile games like \"Honor of Kings\", the system calculates the \"intimacy\" between players based on their shared game time, and such intimacy networks can be represented using weighted graphs.

      Figure 9-4 \u00a0 Weighted and unweighted graphs

      Graph data structures include the following commonly used terms.

      • Adjacency: When two vertices are connected by an edge, these two vertices are said to be \"adjacent\". In Figure 9-4, the adjacent vertices of vertex 1 are vertices 2, 3, and 5.
      • Path: The sequence of edges from vertex A to vertex B is called a \"path\" from A to B. In Figure 9-4, the edge sequence 1-5-2-4 is a path from vertex 1 to vertex 4.
      • Degree: The number of edges a vertex has. For directed graphs, in-degree indicates how many edges point to the vertex, and out-degree indicates how many edges point out from the vertex.
      "},{"location":"chapter_graph/graph/#912-representation-of-graphs","title":"9.1.2 \u00a0 Representation of Graphs","text":"

      Common representations of graphs include \"adjacency matrices\" and \"adjacency lists\". The following uses undirected graphs as examples.

      "},{"location":"chapter_graph/graph/#1-adjacency-matrix","title":"1. \u00a0 Adjacency Matrix","text":"

      Given a graph with \\(n\\) vertices, an adjacency matrix uses an \\(n \\times n\\) matrix to represent the graph, where each row (column) represents a vertex, and matrix elements represent edges, using \\(1\\) or \\(0\\) to indicate whether an edge exists between two vertices.

      As shown in Figure 9-5, let the adjacency matrix be \\(M\\) and the vertex list be \\(V\\). Then matrix element \\(M[i, j] = 1\\) indicates that an edge exists between vertex \\(V[i]\\) and vertex \\(V[j]\\), whereas \\(M[i, j] = 0\\) indicates no edge between the two vertices.

      Figure 9-5 \u00a0 Adjacency matrix representation of a graph

      Adjacency matrices have the following properties.

      • In simple graphs, vertices cannot connect to themselves, so the elements on the main diagonal of the adjacency matrix are meaningless.
      • For undirected graphs, edges in both directions are equivalent, so the adjacency matrix is symmetric about the main diagonal.
      • Replacing the elements of the adjacency matrix from \\(1\\) and \\(0\\) to weights allows representation of weighted graphs.

      When using adjacency matrices to represent graphs, we can directly access matrix elements to obtain edges, resulting in highly efficient addition, deletion, lookup, and modification operations, all with a time complexity of \\(O(1)\\). However, the space complexity of the matrix is \\(O(n^2)\\), which consumes significant memory.

      "},{"location":"chapter_graph/graph/#2-adjacency-list","title":"2. \u00a0 Adjacency List","text":"

      An adjacency list uses \\(n\\) linked lists to represent a graph, with linked list nodes representing vertices. The \\(i\\)-th linked list corresponds to vertex \\(i\\) and stores all adjacent vertices of that vertex (vertices connected to that vertex). Figure 9-6 shows an example of a graph stored using an adjacency list.

      Figure 9-6 \u00a0 Adjacency list representation of a graph

      Adjacency lists only store edges that actually exist, and the total number of edges is typically much less than \\(n^2\\), making them more space-efficient. However, finding edges in an adjacency list requires traversing the linked list, so its time efficiency is inferior to that of adjacency matrices.

      Observing Figure 9-6, the structure of adjacency lists is very similar to \"chaining\" in hash tables, so we can adopt similar methods to optimize efficiency. For example, when linked lists are long, they can be converted to AVL trees or red-black trees, thereby optimizing time efficiency from \\(O(n)\\) to \\(O(\\log n)\\); linked lists can also be converted to hash tables, thereby reducing time complexity to \\(O(1)\\).

      "},{"location":"chapter_graph/graph/#913-common-applications-of-graphs","title":"9.1.3 \u00a0 Common Applications of Graphs","text":"

      As shown in Table 9-1, many real-world systems can be modeled using graphs, and corresponding problems can be reduced to graph computation problems.

      Table 9-1 \u00a0 Common graphs in real life

      Vertices Edges Graph Computation Problem Social network Users Friend relationships Potential friend recommendation Subway lines Stations Connectivity between stations Shortest route recommendation Solar system Celestial bodies Gravitational forces between celestial bodies Planetary orbit calculation"},{"location":"chapter_graph/graph_operations/","title":"9.2 \u00a0 Basic Operations on Graphs","text":"

      Basic operations on graphs can be divided into operations on \"edges\" and operations on \"vertices\". Under the two representation methods of \"adjacency matrix\" and \"adjacency list\", the implementation methods differ.

      "},{"location":"chapter_graph/graph_operations/#921-implementation-based-on-adjacency-matrix","title":"9.2.1 \u00a0 Implementation Based on Adjacency Matrix","text":"

      Given an undirected graph with \\(n\\) vertices, the various operations are implemented as shown in Figure 9-7.

      • Adding or removing an edge: Directly modify the specified edge in the adjacency matrix, using \\(O(1)\\) time. Since it is an undirected graph, both directions of the edge need to be updated simultaneously.
      • Adding a vertex: Add a row and a column at the end of the adjacency matrix and fill them all with \\(0\\)s, using \\(O(n)\\) time.
      • Removing a vertex: Delete a row and a column in the adjacency matrix. The worst case occurs when removing the first row and column, requiring \\((n-1)^2\\) elements to be \"moved up and to the left\", thus using \\(O(n^2)\\) time.
      • Initialization: Pass in \\(n\\) vertices, initialize a vertex list vertices of length \\(n\\), using \\(O(n)\\) time; initialize an adjacency matrix adjMat of size \\(n \\times n\\), using \\(O(n^2)\\) time.
      Initialize adjacency matrixAdd an edgeRemove an edgeAdd a vertexRemove a vertex

      Figure 9-7 \u00a0 Initialization, adding and removing edges, adding and removing vertices in adjacency matrix

      The following is the implementation code for graphs represented using an adjacency matrix:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_adjacency_matrix.py
      class GraphAdjMat:\n    \"\"\"Undirected graph class based on adjacency matrix\"\"\"\n\n    def __init__(self, vertices: list[int], edges: list[list[int]]):\n        \"\"\"Constructor\"\"\"\n        # Vertex list, where the element represents the \"vertex value\" and the index represents the \"vertex index\"\n        self.vertices: list[int] = []\n        # Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n        self.adj_mat: list[list[int]] = []\n        # Add vertices\n        for val in vertices:\n            self.add_vertex(val)\n        # Add edges\n        # Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n        for e in edges:\n            self.add_edge(e[0], e[1])\n\n    def size(self) -> int:\n        \"\"\"Get the number of vertices\"\"\"\n        return len(self.vertices)\n\n    def add_vertex(self, val: int):\n        \"\"\"Add vertex\"\"\"\n        n = self.size()\n        # Add the value of the new vertex to the vertex list\n        self.vertices.append(val)\n        # Add a row to the adjacency matrix\n        new_row = [0] * n\n        self.adj_mat.append(new_row)\n        # Add a column to the adjacency matrix\n        for row in self.adj_mat:\n            row.append(0)\n\n    def remove_vertex(self, index: int):\n        \"\"\"Remove vertex\"\"\"\n        if index >= self.size():\n            raise IndexError()\n        # Remove the vertex at index from the vertex list\n        self.vertices.pop(index)\n        # Remove the row at index from the adjacency matrix\n        self.adj_mat.pop(index)\n        # Remove the column at index from the adjacency matrix\n        for row in self.adj_mat:\n            row.pop(index)\n\n    def add_edge(self, i: int, j: int):\n        \"\"\"Add edge\"\"\"\n        # Parameters i, j correspond to the vertices element indices\n        # Handle index out of bounds and equality\n        if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\n            raise IndexError()\n        # In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)\n        self.adj_mat[i][j] = 1\n        self.adj_mat[j][i] = 1\n\n    def remove_edge(self, i: int, j: int):\n        \"\"\"Remove edge\"\"\"\n        # Parameters i, j correspond to the vertices element indices\n        # Handle index out of bounds and equality\n        if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\n            raise IndexError()\n        self.adj_mat[i][j] = 0\n        self.adj_mat[j][i] = 0\n\n    def print(self):\n        \"\"\"Print adjacency matrix\"\"\"\n        print(\"Vertex list =\", self.vertices)\n        print(\"Adjacency matrix =\")\n        print_matrix(self.adj_mat)\n
      graph_adjacency_matrix.cpp
      /* Undirected graph class based on adjacency matrix */\nclass GraphAdjMat {\n    vector<int> vertices;       // Vertex list, where the element represents the \"vertex value\" and the index represents the \"vertex index\"\n    vector<vector<int>> adjMat; // Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n\n  public:\n    /* Constructor */\n    GraphAdjMat(const vector<int> &vertices, const vector<vector<int>> &edges) {\n        // Add vertex\n        for (int val : vertices) {\n            addVertex(val);\n        }\n        // Add edge\n        // Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n        for (const vector<int> &edge : edges) {\n            addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* Get the number of vertices */\n    int size() const {\n        return vertices.size();\n    }\n\n    /* Add vertex */\n    void addVertex(int val) {\n        int n = size();\n        // Add the value of the new vertex to the vertex list\n        vertices.push_back(val);\n        // Add a row to the adjacency matrix\n        adjMat.emplace_back(vector<int>(n, 0));\n        // Add a column to the adjacency matrix\n        for (vector<int> &row : adjMat) {\n            row.push_back(0);\n        }\n    }\n\n    /* Remove vertex */\n    void removeVertex(int index) {\n        if (index >= size()) {\n            throw out_of_range(\"Vertex does not exist\");\n        }\n        // Remove the vertex at index from the vertex list\n        vertices.erase(vertices.begin() + index);\n        // Remove the row at index from the adjacency matrix\n        adjMat.erase(adjMat.begin() + index);\n        // Remove the column at index from the adjacency matrix\n        for (vector<int> &row : adjMat) {\n            row.erase(row.begin() + index);\n        }\n    }\n\n    /* Add edge */\n    // Parameters i, j correspond to the vertices element indices\n    void addEdge(int i, int j) {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n            throw out_of_range(\"Vertex does not exist\");\n        }\n        // In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)\n        adjMat[i][j] = 1;\n        adjMat[j][i] = 1;\n    }\n\n    /* Remove edge */\n    // Parameters i, j correspond to the vertices element indices\n    void removeEdge(int i, int j) {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n            throw out_of_range(\"Vertex does not exist\");\n        }\n        adjMat[i][j] = 0;\n        adjMat[j][i] = 0;\n    }\n\n    /* Print adjacency matrix */\n    void print() {\n        cout << \"Vertex list = \";\n        printVector(vertices);\n        cout << \"Adjacency matrix =\" << endl;\n        printVectorMatrix(adjMat);\n    }\n};\n
      graph_adjacency_matrix.java
      /* Undirected graph class based on adjacency matrix */\nclass GraphAdjMat {\n    List<Integer> vertices; // Vertex list, where the element represents the \"vertex value\" and the index represents the \"vertex index\"\n    List<List<Integer>> adjMat; // Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n\n    /* Constructor */\n    public GraphAdjMat(int[] vertices, int[][] edges) {\n        this.vertices = new ArrayList<>();\n        this.adjMat = new ArrayList<>();\n        // Add vertex\n        for (int val : vertices) {\n            addVertex(val);\n        }\n        // Add edge\n        // Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n        for (int[] e : edges) {\n            addEdge(e[0], e[1]);\n        }\n    }\n\n    /* Get the number of vertices */\n    public int size() {\n        return vertices.size();\n    }\n\n    /* Add vertex */\n    public void addVertex(int val) {\n        int n = size();\n        // Add the value of the new vertex to the vertex list\n        vertices.add(val);\n        // Add a row to the adjacency matrix\n        List<Integer> newRow = new ArrayList<>(n);\n        for (int j = 0; j < n; j++) {\n            newRow.add(0);\n        }\n        adjMat.add(newRow);\n        // Add a column to the adjacency matrix\n        for (List<Integer> row : adjMat) {\n            row.add(0);\n        }\n    }\n\n    /* Remove vertex */\n    public void removeVertex(int index) {\n        if (index >= size())\n            throw new IndexOutOfBoundsException();\n        // Remove the vertex at index from the vertex list\n        vertices.remove(index);\n        // Remove the row at index from the adjacency matrix\n        adjMat.remove(index);\n        // Remove the column at index from the adjacency matrix\n        for (List<Integer> row : adjMat) {\n            row.remove(index);\n        }\n    }\n\n    /* Add edge */\n    // Parameters i, j correspond to the vertices element indices\n    public void addEdge(int i, int j) {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw new IndexOutOfBoundsException();\n        // In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)\n        adjMat.get(i).set(j, 1);\n        adjMat.get(j).set(i, 1);\n    }\n\n    /* Remove edge */\n    // Parameters i, j correspond to the vertices element indices\n    public void removeEdge(int i, int j) {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw new IndexOutOfBoundsException();\n        adjMat.get(i).set(j, 0);\n        adjMat.get(j).set(i, 0);\n    }\n\n    /* Print adjacency matrix */\n    public void print() {\n        System.out.print(\"Vertex list = \");\n        System.out.println(vertices);\n        System.out.println(\"Adjacency matrix =\");\n        PrintUtil.printMatrix(adjMat);\n    }\n}\n
      graph_adjacency_matrix.cs
      /* Undirected graph class based on adjacency matrix */\nclass GraphAdjMat {\n    List<int> vertices;     // Vertex list, where the element represents the \"vertex value\" and the index represents the \"vertex index\"\n    List<List<int>> adjMat; // Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n\n    /* Constructor */\n    public GraphAdjMat(int[] vertices, int[][] edges) {\n        this.vertices = [];\n        this.adjMat = [];\n        // Add vertex\n        foreach (int val in vertices) {\n            AddVertex(val);\n        }\n        // Add edge\n        // Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n        foreach (int[] e in edges) {\n            AddEdge(e[0], e[1]);\n        }\n    }\n\n    /* Get the number of vertices */\n    int Size() {\n        return vertices.Count;\n    }\n\n    /* Add vertex */\n    public void AddVertex(int val) {\n        int n = Size();\n        // Add the value of the new vertex to the vertex list\n        vertices.Add(val);\n        // Add a row to the adjacency matrix\n        List<int> newRow = new(n);\n        for (int j = 0; j < n; j++) {\n            newRow.Add(0);\n        }\n        adjMat.Add(newRow);\n        // Add a column to the adjacency matrix\n        foreach (List<int> row in adjMat) {\n            row.Add(0);\n        }\n    }\n\n    /* Remove vertex */\n    public void RemoveVertex(int index) {\n        if (index >= Size())\n            throw new IndexOutOfRangeException();\n        // Remove the vertex at index from the vertex list\n        vertices.RemoveAt(index);\n        // Remove the row at index from the adjacency matrix\n        adjMat.RemoveAt(index);\n        // Remove the column at index from the adjacency matrix\n        foreach (List<int> row in adjMat) {\n            row.RemoveAt(index);\n        }\n    }\n\n    /* Add edge */\n    // Parameters i, j correspond to the vertices element indices\n    public void AddEdge(int i, int j) {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)\n            throw new IndexOutOfRangeException();\n        // In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)\n        adjMat[i][j] = 1;\n        adjMat[j][i] = 1;\n    }\n\n    /* Remove edge */\n    // Parameters i, j correspond to the vertices element indices\n    public void RemoveEdge(int i, int j) {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)\n            throw new IndexOutOfRangeException();\n        adjMat[i][j] = 0;\n        adjMat[j][i] = 0;\n    }\n\n    /* Print adjacency matrix */\n    public void Print() {\n        Console.Write(\"Vertex list = \");\n        PrintUtil.PrintList(vertices);\n        Console.WriteLine(\"Adjacency matrix =\");\n        PrintUtil.PrintMatrix(adjMat);\n    }\n}\n
      graph_adjacency_matrix.go
      /* Undirected graph class based on adjacency matrix */\ntype graphAdjMat struct {\n    // Vertex list, where the element represents the \"vertex value\" and the index represents the \"vertex index\"\n    vertices []int\n    // Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n    adjMat [][]int\n}\n\n/* Constructor */\nfunc newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat {\n    // Add vertex\n    n := len(vertices)\n    adjMat := make([][]int, n)\n    for i := range adjMat {\n        adjMat[i] = make([]int, n)\n    }\n    // Initialize graph\n    g := &graphAdjMat{\n        vertices: vertices,\n        adjMat:   adjMat,\n    }\n    // Add edge\n    // Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n    for i := range edges {\n        g.addEdge(edges[i][0], edges[i][1])\n    }\n    return g\n}\n\n/* Get the number of vertices */\nfunc (g *graphAdjMat) size() int {\n    return len(g.vertices)\n}\n\n/* Add vertex */\nfunc (g *graphAdjMat) addVertex(val int) {\n    n := g.size()\n    // Add the value of the new vertex to the vertex list\n    g.vertices = append(g.vertices, val)\n    // Add a row to the adjacency matrix\n    newRow := make([]int, n)\n    g.adjMat = append(g.adjMat, newRow)\n    // Add a column to the adjacency matrix\n    for i := range g.adjMat {\n        g.adjMat[i] = append(g.adjMat[i], 0)\n    }\n}\n\n/* Remove vertex */\nfunc (g *graphAdjMat) removeVertex(index int) {\n    if index >= g.size() {\n        return\n    }\n    // Remove the vertex at index from the vertex list\n    g.vertices = append(g.vertices[:index], g.vertices[index+1:]...)\n    // Remove the row at index from the adjacency matrix\n    g.adjMat = append(g.adjMat[:index], g.adjMat[index+1:]...)\n    // Remove the column at index from the adjacency matrix\n    for i := range g.adjMat {\n        g.adjMat[i] = append(g.adjMat[i][:index], g.adjMat[i][index+1:]...)\n    }\n}\n\n/* Add edge */\n// Parameters i, j correspond to the vertices element indices\nfunc (g *graphAdjMat) addEdge(i, j int) {\n    // Handle index out of bounds and equality\n    if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\n        fmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n    }\n    // In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)\n    g.adjMat[i][j] = 1\n    g.adjMat[j][i] = 1\n}\n\n/* Remove edge */\n// Parameters i, j correspond to the vertices element indices\nfunc (g *graphAdjMat) removeEdge(i, j int) {\n    // Handle index out of bounds and equality\n    if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\n        fmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n    }\n    g.adjMat[i][j] = 0\n    g.adjMat[j][i] = 0\n}\n\n/* Print adjacency matrix */\nfunc (g *graphAdjMat) print() {\n    fmt.Printf(\"\\tVertex list = %v\\n\", g.vertices)\n    fmt.Printf(\"\\tAdjacency matrix = \\n\")\n    for i := range g.adjMat {\n        fmt.Printf(\"\\t\\t\\t%v\\n\", g.adjMat[i])\n    }\n}\n
      graph_adjacency_matrix.swift
      /* Undirected graph class based on adjacency matrix */\nclass GraphAdjMat {\n    private var vertices: [Int] // Vertex list, where the element represents the \"vertex value\" and the index represents the \"vertex index\"\n    private var adjMat: [[Int]] // Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n\n    /* Constructor */\n    init(vertices: [Int], edges: [[Int]]) {\n        self.vertices = []\n        adjMat = []\n        // Add vertex\n        for val in vertices {\n            addVertex(val: val)\n        }\n        // Add edge\n        // Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n        for e in edges {\n            addEdge(i: e[0], j: e[1])\n        }\n    }\n\n    /* Get the number of vertices */\n    func size() -> Int {\n        vertices.count\n    }\n\n    /* Add vertex */\n    func addVertex(val: Int) {\n        let n = size()\n        // Add the value of the new vertex to the vertex list\n        vertices.append(val)\n        // Add a row to the adjacency matrix\n        let newRow = Array(repeating: 0, count: n)\n        adjMat.append(newRow)\n        // Add a column to the adjacency matrix\n        for i in adjMat.indices {\n            adjMat[i].append(0)\n        }\n    }\n\n    /* Remove vertex */\n    func removeVertex(index: Int) {\n        if index >= size() {\n            fatalError(\"Out of bounds\")\n        }\n        // Remove the vertex at index from the vertex list\n        vertices.remove(at: index)\n        // Remove the row at index from the adjacency matrix\n        adjMat.remove(at: index)\n        // Remove the column at index from the adjacency matrix\n        for i in adjMat.indices {\n            adjMat[i].remove(at: index)\n        }\n    }\n\n    /* Add edge */\n    // Parameters i, j correspond to the vertices element indices\n    func addEdge(i: Int, j: Int) {\n        // Handle index out of bounds and equality\n        if i < 0 || j < 0 || i >= size() || j >= size() || i == j {\n            fatalError(\"Out of bounds\")\n        }\n        // In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)\n        adjMat[i][j] = 1\n        adjMat[j][i] = 1\n    }\n\n    /* Remove edge */\n    // Parameters i, j correspond to the vertices element indices\n    func removeEdge(i: Int, j: Int) {\n        // Handle index out of bounds and equality\n        if i < 0 || j < 0 || i >= size() || j >= size() || i == j {\n            fatalError(\"Out of bounds\")\n        }\n        adjMat[i][j] = 0\n        adjMat[j][i] = 0\n    }\n\n    /* Print adjacency matrix */\n    func print() {\n        Swift.print(\"Vertex list = \", terminator: \"\")\n        Swift.print(vertices)\n        Swift.print(\"Adjacency matrix =\")\n        PrintUtil.printMatrix(matrix: adjMat)\n    }\n}\n
      graph_adjacency_matrix.js
      /* Undirected graph class based on adjacency matrix */\nclass GraphAdjMat {\n    vertices; // Vertex list, where the element represents the \"vertex value\" and the index represents the \"vertex index\"\n    adjMat; // Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n\n    /* Constructor */\n    constructor(vertices, edges) {\n        this.vertices = [];\n        this.adjMat = [];\n        // Add vertex\n        for (const val of vertices) {\n            this.addVertex(val);\n        }\n        // Add edge\n        // Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n        for (const e of edges) {\n            this.addEdge(e[0], e[1]);\n        }\n    }\n\n    /* Get the number of vertices */\n    size() {\n        return this.vertices.length;\n    }\n\n    /* Add vertex */\n    addVertex(val) {\n        const n = this.size();\n        // Add the value of the new vertex to the vertex list\n        this.vertices.push(val);\n        // Add a row to the adjacency matrix\n        const newRow = [];\n        for (let j = 0; j < n; j++) {\n            newRow.push(0);\n        }\n        this.adjMat.push(newRow);\n        // Add a column to the adjacency matrix\n        for (const row of this.adjMat) {\n            row.push(0);\n        }\n    }\n\n    /* Remove vertex */\n    removeVertex(index) {\n        if (index >= this.size()) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // Remove the vertex at index from the vertex list\n        this.vertices.splice(index, 1);\n\n        // Remove the row at index from the adjacency matrix\n        this.adjMat.splice(index, 1);\n        // Remove the column at index from the adjacency matrix\n        for (const row of this.adjMat) {\n            row.splice(index, 1);\n        }\n    }\n\n    /* Add edge */\n    // Parameters i, j correspond to the vertices element indices\n    addEdge(i, j) {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // In undirected graph, adjacency matrix is symmetric about main diagonal, i.e., satisfies (i, j) === (j, i)\n        this.adjMat[i][j] = 1;\n        this.adjMat[j][i] = 1;\n    }\n\n    /* Remove edge */\n    // Parameters i, j correspond to the vertices element indices\n    removeEdge(i, j) {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        this.adjMat[i][j] = 0;\n        this.adjMat[j][i] = 0;\n    }\n\n    /* Print adjacency matrix */\n    print() {\n        console.log('Vertex list = ', this.vertices);\n        console.log('Adjacency matrix =', this.adjMat);\n    }\n}\n
      graph_adjacency_matrix.ts
      /* Undirected graph class based on adjacency matrix */\nclass GraphAdjMat {\n    vertices: number[]; // Vertex list, where the element represents the \"vertex value\" and the index represents the \"vertex index\"\n    adjMat: number[][]; // Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n\n    /* Constructor */\n    constructor(vertices: number[], edges: number[][]) {\n        this.vertices = [];\n        this.adjMat = [];\n        // Add vertex\n        for (const val of vertices) {\n            this.addVertex(val);\n        }\n        // Add edge\n        // Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n        for (const e of edges) {\n            this.addEdge(e[0], e[1]);\n        }\n    }\n\n    /* Get the number of vertices */\n    size(): number {\n        return this.vertices.length;\n    }\n\n    /* Add vertex */\n    addVertex(val: number): void {\n        const n: number = this.size();\n        // Add the value of the new vertex to the vertex list\n        this.vertices.push(val);\n        // Add a row to the adjacency matrix\n        const newRow: number[] = [];\n        for (let j: number = 0; j < n; j++) {\n            newRow.push(0);\n        }\n        this.adjMat.push(newRow);\n        // Add a column to the adjacency matrix\n        for (const row of this.adjMat) {\n            row.push(0);\n        }\n    }\n\n    /* Remove vertex */\n    removeVertex(index: number): void {\n        if (index >= this.size()) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // Remove the vertex at index from the vertex list\n        this.vertices.splice(index, 1);\n\n        // Remove the row at index from the adjacency matrix\n        this.adjMat.splice(index, 1);\n        // Remove the column at index from the adjacency matrix\n        for (const row of this.adjMat) {\n            row.splice(index, 1);\n        }\n    }\n\n    /* Add edge */\n    // Parameters i, j correspond to the vertices element indices\n    addEdge(i: number, j: number): void {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // In undirected graph, adjacency matrix is symmetric about main diagonal, i.e., satisfies (i, j) === (j, i)\n        this.adjMat[i][j] = 1;\n        this.adjMat[j][i] = 1;\n    }\n\n    /* Remove edge */\n    // Parameters i, j correspond to the vertices element indices\n    removeEdge(i: number, j: number): void {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        this.adjMat[i][j] = 0;\n        this.adjMat[j][i] = 0;\n    }\n\n    /* Print adjacency matrix */\n    print(): void {\n        console.log('Vertex list = ', this.vertices);\n        console.log('Adjacency matrix =', this.adjMat);\n    }\n}\n
      graph_adjacency_matrix.dart
      /* Undirected graph class based on adjacency matrix */\nclass GraphAdjMat {\n  List<int> vertices = []; // Vertex elements, elements represent \"vertex values\", indices represent \"vertex indices\"\n  List<List<int>> adjMat = []; // Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n\n  /* Constructor */\n  GraphAdjMat(List<int> vertices, List<List<int>> edges) {\n    this.vertices = [];\n    this.adjMat = [];\n    // Add vertex\n    for (int val in vertices) {\n      addVertex(val);\n    }\n    // Add edge\n    // Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n    for (List<int> e in edges) {\n      addEdge(e[0], e[1]);\n    }\n  }\n\n  /* Get the number of vertices */\n  int size() {\n    return vertices.length;\n  }\n\n  /* Add vertex */\n  void addVertex(int val) {\n    int n = size();\n    // Add the value of the new vertex to the vertex list\n    vertices.add(val);\n    // Add a row to the adjacency matrix\n    List<int> newRow = List.filled(n, 0, growable: true);\n    adjMat.add(newRow);\n    // Add a column to the adjacency matrix\n    for (List<int> row in adjMat) {\n      row.add(0);\n    }\n  }\n\n  /* Remove vertex */\n  void removeVertex(int index) {\n    if (index >= size()) {\n      throw IndexError;\n    }\n    // Remove the vertex at index from the vertex list\n    vertices.removeAt(index);\n    // Remove the row at index from the adjacency matrix\n    adjMat.removeAt(index);\n    // Remove the column at index from the adjacency matrix\n    for (List<int> row in adjMat) {\n      row.removeAt(index);\n    }\n  }\n\n  /* Add edge */\n  // Parameters i, j correspond to the vertices element indices\n  void addEdge(int i, int j) {\n    // Handle index out of bounds and equality\n    if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n      throw IndexError;\n    }\n    // In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)\n    adjMat[i][j] = 1;\n    adjMat[j][i] = 1;\n  }\n\n  /* Remove edge */\n  // Parameters i, j correspond to the vertices element indices\n  void removeEdge(int i, int j) {\n    // Handle index out of bounds and equality\n    if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n      throw IndexError;\n    }\n    adjMat[i][j] = 0;\n    adjMat[j][i] = 0;\n  }\n\n  /* Print adjacency matrix */\n  void printAdjMat() {\n    print(\"Vertex list = $vertices\");\n    print(\"Adjacency matrix = \");\n    printMatrix(adjMat);\n  }\n}\n
      graph_adjacency_matrix.rs
      /* Undirected graph type based on adjacency matrix */\npub struct GraphAdjMat {\n    // Vertex list, where the element represents the \"vertex value\" and the index represents the \"vertex index\"\n    pub vertices: Vec<i32>,\n    // Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n    pub adj_mat: Vec<Vec<i32>>,\n}\n\nimpl GraphAdjMat {\n    /* Constructor */\n    pub fn new(vertices: Vec<i32>, edges: Vec<[usize; 2]>) -> Self {\n        let mut graph = GraphAdjMat {\n            vertices: vec![],\n            adj_mat: vec![],\n        };\n        // Add vertex\n        for val in vertices {\n            graph.add_vertex(val);\n        }\n        // Add edge\n        // Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n        for edge in edges {\n            graph.add_edge(edge[0], edge[1])\n        }\n\n        graph\n    }\n\n    /* Get the number of vertices */\n    pub fn size(&self) -> usize {\n        self.vertices.len()\n    }\n\n    /* Add vertex */\n    pub fn add_vertex(&mut self, val: i32) {\n        let n = self.size();\n        // Add the value of the new vertex to the vertex list\n        self.vertices.push(val);\n        // Add a row to the adjacency matrix\n        self.adj_mat.push(vec![0; n]);\n        // Add a column to the adjacency matrix\n        for row in self.adj_mat.iter_mut() {\n            row.push(0);\n        }\n    }\n\n    /* Remove vertex */\n    pub fn remove_vertex(&mut self, index: usize) {\n        if index >= self.size() {\n            panic!(\"index error\")\n        }\n        // Remove the vertex at index from the vertex list\n        self.vertices.remove(index);\n        // Remove the row at index from the adjacency matrix\n        self.adj_mat.remove(index);\n        // Remove the column at index from the adjacency matrix\n        for row in self.adj_mat.iter_mut() {\n            row.remove(index);\n        }\n    }\n\n    /* Add edge */\n    pub fn add_edge(&mut self, i: usize, j: usize) {\n        // Parameters i, j correspond to the vertices element indices\n        // Handle index out of bounds and equality\n        if i >= self.size() || j >= self.size() || i == j {\n            panic!(\"index error\")\n        }\n        // In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)\n        self.adj_mat[i][j] = 1;\n        self.adj_mat[j][i] = 1;\n    }\n\n    /* Remove edge */\n    // Parameters i, j correspond to the vertices element indices\n    pub fn remove_edge(&mut self, i: usize, j: usize) {\n        // Parameters i, j correspond to the vertices element indices\n        // Handle index out of bounds and equality\n        if i >= self.size() || j >= self.size() || i == j {\n            panic!(\"index error\")\n        }\n        self.adj_mat[i][j] = 0;\n        self.adj_mat[j][i] = 0;\n    }\n\n    /* Print adjacency matrix */\n    pub fn print(&self) {\n        println!(\"Vertex list = {:?}\", self.vertices);\n        println!(\"Adjacency matrix =\");\n        println!(\"[\");\n        for row in &self.adj_mat {\n            println!(\"  {:?},\", row);\n        }\n        println!(\"]\")\n    }\n}\n
      graph_adjacency_matrix.c
      /* Undirected graph structure based on adjacency matrix */\ntypedef struct {\n    int vertices[MAX_SIZE];\n    int adjMat[MAX_SIZE][MAX_SIZE];\n    int size;\n} GraphAdjMat;\n\n/* Constructor */\nGraphAdjMat *newGraphAdjMat() {\n    GraphAdjMat *graph = (GraphAdjMat *)malloc(sizeof(GraphAdjMat));\n    graph->size = 0;\n    for (int i = 0; i < MAX_SIZE; i++) {\n        for (int j = 0; j < MAX_SIZE; j++) {\n            graph->adjMat[i][j] = 0;\n        }\n    }\n    return graph;\n}\n\n/* Destructor */\nvoid delGraphAdjMat(GraphAdjMat *graph) {\n    free(graph);\n}\n\n/* Add vertex */\nvoid addVertex(GraphAdjMat *graph, int val) {\n    if (graph->size == MAX_SIZE) {\n        fprintf(stderr, \"Graph vertex count has reached maximum\\n\");\n        return;\n    }\n    // Add nth vertex and zero nth row and column\n    int n = graph->size;\n    graph->vertices[n] = val;\n    for (int i = 0; i <= n; i++) {\n        graph->adjMat[n][i] = graph->adjMat[i][n] = 0;\n    }\n    graph->size++;\n}\n\n/* Remove vertex */\nvoid removeVertex(GraphAdjMat *graph, int index) {\n    if (index < 0 || index >= graph->size) {\n        fprintf(stderr, \"Vertex index out of bounds\\n\");\n        return;\n    }\n    // Remove the vertex at index from the vertex list\n    for (int i = index; i < graph->size - 1; i++) {\n        graph->vertices[i] = graph->vertices[i + 1];\n    }\n    // Remove the row at index from the adjacency matrix\n    for (int i = index; i < graph->size - 1; i++) {\n        for (int j = 0; j < graph->size; j++) {\n            graph->adjMat[i][j] = graph->adjMat[i + 1][j];\n        }\n    }\n    // Remove the column at index from the adjacency matrix\n    for (int i = 0; i < graph->size; i++) {\n        for (int j = index; j < graph->size - 1; j++) {\n            graph->adjMat[i][j] = graph->adjMat[i][j + 1];\n        }\n    }\n    graph->size--;\n}\n\n/* Add edge */\n// Parameters i, j correspond to the vertices element indices\nvoid addEdge(GraphAdjMat *graph, int i, int j) {\n    if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n        fprintf(stderr, \"Edge index out of bounds or equal\\n\");\n        return;\n    }\n    graph->adjMat[i][j] = 1;\n    graph->adjMat[j][i] = 1;\n}\n\n/* Remove edge */\n// Parameters i, j correspond to the vertices element indices\nvoid removeEdge(GraphAdjMat *graph, int i, int j) {\n    if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n        fprintf(stderr, \"Edge index out of bounds or equal\\n\");\n        return;\n    }\n    graph->adjMat[i][j] = 0;\n    graph->adjMat[j][i] = 0;\n}\n\n/* Print adjacency matrix */\nvoid printGraphAdjMat(GraphAdjMat *graph) {\n    printf(\"Vertex list = \");\n    printArray(graph->vertices, graph->size);\n    printf(\"Adjacency matrix =\\n\");\n    for (int i = 0; i < graph->size; i++) {\n        printArray(graph->adjMat[i], graph->size);\n    }\n}\n
      graph_adjacency_matrix.kt
      /* Undirected graph class based on adjacency matrix */\nclass GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {\n    val vertices = mutableListOf<Int>() // Vertex list, where the element represents the \"vertex value\" and the index represents the \"vertex index\"\n    val adjMat = mutableListOf<MutableList<Int>>() // Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n\n    /* Constructor */\n    init {\n        // Add vertex\n        for (vertex in vertices) {\n            addVertex(vertex)\n        }\n        // Add edge\n        // Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n        for (edge in edges) {\n            addEdge(edge[0], edge[1])\n        }\n    }\n\n    /* Get the number of vertices */\n    fun size(): Int {\n        return vertices.size\n    }\n\n    /* Add vertex */\n    fun addVertex(_val: Int) {\n        val n = size()\n        // Add the value of the new vertex to the vertex list\n        vertices.add(_val)\n        // Add a row to the adjacency matrix\n        val newRow = mutableListOf<Int>()\n        for (j in 0..<n) {\n            newRow.add(0)\n        }\n        adjMat.add(newRow)\n        // Add a column to the adjacency matrix\n        for (row in adjMat) {\n            row.add(0)\n        }\n    }\n\n    /* Remove vertex */\n    fun removeVertex(index: Int) {\n        if (index >= size())\n            throw IndexOutOfBoundsException()\n        // Remove the vertex at index from the vertex list\n        vertices.removeAt(index)\n        // Remove the row at index from the adjacency matrix\n        adjMat.removeAt(index)\n        // Remove the column at index from the adjacency matrix\n        for (row in adjMat) {\n            row.removeAt(index)\n        }\n    }\n\n    /* Add edge */\n    // Parameters i, j correspond to the vertices element indices\n    fun addEdge(i: Int, j: Int) {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw IndexOutOfBoundsException()\n        // In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)\n        adjMat[i][j] = 1\n        adjMat[j][i] = 1\n    }\n\n    /* Remove edge */\n    // Parameters i, j correspond to the vertices element indices\n    fun removeEdge(i: Int, j: Int) {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw IndexOutOfBoundsException()\n        adjMat[i][j] = 0\n        adjMat[j][i] = 0\n    }\n\n    /* Print adjacency matrix */\n    fun print() {\n        print(\"Vertex list = \")\n        println(vertices)\n        println(\"Adjacency matrix =\")\n        printMatrix(adjMat)\n    }\n}\n
      graph_adjacency_matrix.rb
      ### Undirected graph class based on adjacency matrix ###\nclass GraphAdjMat\n  def initialize(vertices, edges)\n    ### Constructor ###\n    # Vertex list, where the element represents the \"vertex value\" and the index represents the \"vertex index\"\n    @vertices = []\n    # Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n    @adj_mat = []\n    # Add vertex\n    vertices.each { |val| add_vertex(val) }\n    # Add edge\n    # Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n    edges.each { |e| add_edge(e[0], e[1]) }\n  end\n\n  ### Get number of vertices ###\n  def size\n    @vertices.length\n  end\n\n  ### Add vertex ###\n  def add_vertex(val)\n    n = size\n    # Add the value of the new vertex to the vertex list\n    @vertices << val\n    # Add a row to the adjacency matrix\n    new_row = Array.new(n, 0)\n    @adj_mat << new_row\n    # Add a column to the adjacency matrix\n    @adj_mat.each { |row| row << 0 }\n  end\n\n  ### Delete vertex ###\n  def remove_vertex(index)\n    raise IndexError if index >= size\n\n    # Remove the vertex at index from the vertex list\n    @vertices.delete_at(index)\n    # Remove the row at index from the adjacency matrix\n    @adj_mat.delete_at(index)\n    # Remove the column at index from the adjacency matrix\n    @adj_mat.each { |row| row.delete_at(index) }\n  end\n\n  ### Add edge ###\n  def add_edge(i, j)\n    # Parameters i, j correspond to the vertices element indices\n    # Handle index out of bounds and equality\n    if i < 0 || j < 0 || i >= size || j >= size || i == j\n      raise IndexError\n    end\n    # In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)\n    @adj_mat[i][j] = 1\n    @adj_mat[j][i] = 1\n  end\n\n  ### Delete edge ###\n  def remove_edge(i, j)\n    # Parameters i, j correspond to the vertices element indices\n    # Handle index out of bounds and equality\n    if i < 0 || j < 0 || i >= size || j >= size || i == j\n      raise IndexError\n    end\n    @adj_mat[i][j] = 0\n    @adj_mat[j][i] = 0\n  end\n\n  ### Print adjacency matrix ###\n  def __print__\n    puts \"Vertex list = #{@vertices}\"\n    puts 'Adjacency matrix ='\n    print_matrix(@adj_mat)\n  end\nend\n
      "},{"location":"chapter_graph/graph_operations/#922-implementation-based-on-adjacency-list","title":"9.2.2 \u00a0 Implementation Based on Adjacency List","text":"

      Given an undirected graph with a total of \\(n\\) vertices and \\(m\\) edges, the various operations can be implemented as shown in Figure 9-8.

      • Adding an edge: Add the edge at the end of the corresponding vertex's linked list, using \\(O(1)\\) time. Since it is an undirected graph, edges in both directions need to be added simultaneously.
      • Removing an edge: Find and remove the specified edge in the corresponding vertex's linked list, using \\(O(m)\\) time. In an undirected graph, edges in both directions need to be removed simultaneously.
      • Adding a vertex: Add a linked list in the adjacency list and set the new vertex as the head node of the list, using \\(O(1)\\) time.
      • Removing a vertex: Traverse the entire adjacency list and remove all edges containing the specified vertex, using \\(O(n + m)\\) time.
      • Initialization: Create \\(n\\) vertices and \\(2m\\) edges in the adjacency list, using \\(O(n + m)\\) time.
      Initialize adjacency listAdd an edgeRemove an edgeAdd a vertexRemove a vertex

      Figure 9-8 \u00a0 Initialization, adding and removing edges, adding and removing vertices in adjacency list

      The following is the adjacency list code implementation. Compared to Figure 9-8, the actual code has the following differences.

      • For convenience in adding and removing vertices, and to simplify the code, we use lists (dynamic arrays) instead of linked lists.
      • A hash table is used to store the adjacency list, where key is the vertex instance and value is the list (linked list) of adjacent vertices for that vertex.

      Additionally, we use the Vertex class to represent vertices in the adjacency list. The reason for this is: if we used list indices to distinguish different vertices as with adjacency matrices, then to delete the vertex at index \\(i\\), we would need to traverse the entire adjacency list and decrement all indices greater than \\(i\\) by \\(1\\), which is very inefficient. However, if each vertex is a unique Vertex instance, deleting a vertex does not require modifying other vertices.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_adjacency_list.py
      class GraphAdjList:\n    \"\"\"Undirected graph class based on adjacency list\"\"\"\n\n    def __init__(self, edges: list[list[Vertex]]):\n        \"\"\"Constructor\"\"\"\n        # Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n        self.adj_list = dict[Vertex, list[Vertex]]()\n        # Add all vertices and edges\n        for edge in edges:\n            self.add_vertex(edge[0])\n            self.add_vertex(edge[1])\n            self.add_edge(edge[0], edge[1])\n\n    def size(self) -> int:\n        \"\"\"Get the number of vertices\"\"\"\n        return len(self.adj_list)\n\n    def add_edge(self, vet1: Vertex, vet2: Vertex):\n        \"\"\"Add edge\"\"\"\n        if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n            raise ValueError()\n        # Add edge vet1 - vet2\n        self.adj_list[vet1].append(vet2)\n        self.adj_list[vet2].append(vet1)\n\n    def remove_edge(self, vet1: Vertex, vet2: Vertex):\n        \"\"\"Remove edge\"\"\"\n        if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n            raise ValueError()\n        # Remove edge vet1 - vet2\n        self.adj_list[vet1].remove(vet2)\n        self.adj_list[vet2].remove(vet1)\n\n    def add_vertex(self, vet: Vertex):\n        \"\"\"Add vertex\"\"\"\n        if vet in self.adj_list:\n            return\n        # Add a new linked list in the adjacency list\n        self.adj_list[vet] = []\n\n    def remove_vertex(self, vet: Vertex):\n        \"\"\"Remove vertex\"\"\"\n        if vet not in self.adj_list:\n            raise ValueError()\n        # Remove the linked list corresponding to vertex vet in the adjacency list\n        self.adj_list.pop(vet)\n        # Traverse the linked lists of other vertices and remove all edges containing vet\n        for vertex in self.adj_list:\n            if vet in self.adj_list[vertex]:\n                self.adj_list[vertex].remove(vet)\n\n    def print(self):\n        \"\"\"Print adjacency list\"\"\"\n        print(\"Adjacency list =\")\n        for vertex in self.adj_list:\n            tmp = [v.val for v in self.adj_list[vertex]]\n            print(f\"{vertex.val}: {tmp},\")\n
      graph_adjacency_list.cpp
      /* Undirected graph class based on adjacency list */\nclass GraphAdjList {\n  public:\n    // Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n    unordered_map<Vertex *, vector<Vertex *>> adjList;\n\n    /* Remove specified node from vector */\n    void remove(vector<Vertex *> &vec, Vertex *vet) {\n        for (int i = 0; i < vec.size(); i++) {\n            if (vec[i] == vet) {\n                vec.erase(vec.begin() + i);\n                break;\n            }\n        }\n    }\n\n    /* Constructor */\n    GraphAdjList(const vector<vector<Vertex *>> &edges) {\n        // Add all vertices and edges\n        for (const vector<Vertex *> &edge : edges) {\n            addVertex(edge[0]);\n            addVertex(edge[1]);\n            addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* Get the number of vertices */\n    int size() {\n        return adjList.size();\n    }\n\n    /* Add edge */\n    void addEdge(Vertex *vet1, Vertex *vet2) {\n        if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n            throw invalid_argument(\"Vertex does not exist\");\n        // Add edge vet1 - vet2\n        adjList[vet1].push_back(vet2);\n        adjList[vet2].push_back(vet1);\n    }\n\n    /* Remove edge */\n    void removeEdge(Vertex *vet1, Vertex *vet2) {\n        if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n            throw invalid_argument(\"Vertex does not exist\");\n        // Remove edge vet1 - vet2\n        remove(adjList[vet1], vet2);\n        remove(adjList[vet2], vet1);\n    }\n\n    /* Add vertex */\n    void addVertex(Vertex *vet) {\n        if (adjList.count(vet))\n            return;\n        // Add a new linked list in the adjacency list\n        adjList[vet] = vector<Vertex *>();\n    }\n\n    /* Remove vertex */\n    void removeVertex(Vertex *vet) {\n        if (!adjList.count(vet))\n            throw invalid_argument(\"Vertex does not exist\");\n        // Remove the linked list corresponding to vertex vet in the adjacency list\n        adjList.erase(vet);\n        // Traverse the linked lists of other vertices and remove all edges containing vet\n        for (auto &adj : adjList) {\n            remove(adj.second, vet);\n        }\n    }\n\n    /* Print adjacency list */\n    void print() {\n        cout << \"Adjacency list =\" << endl;\n        for (auto &adj : adjList) {\n            const auto &key = adj.first;\n            const auto &vec = adj.second;\n            cout << key->val << \": \";\n            printVector(vetsToVals(vec));\n        }\n    }\n};\n
      graph_adjacency_list.java
      /* Undirected graph class based on adjacency list */\nclass GraphAdjList {\n    // Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n    Map<Vertex, List<Vertex>> adjList;\n\n    /* Constructor */\n    public GraphAdjList(Vertex[][] edges) {\n        this.adjList = new HashMap<>();\n        // Add all vertices and edges\n        for (Vertex[] edge : edges) {\n            addVertex(edge[0]);\n            addVertex(edge[1]);\n            addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* Get the number of vertices */\n    public int size() {\n        return adjList.size();\n    }\n\n    /* Add edge */\n    public void addEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw new IllegalArgumentException();\n        // Add edge vet1 - vet2\n        adjList.get(vet1).add(vet2);\n        adjList.get(vet2).add(vet1);\n    }\n\n    /* Remove edge */\n    public void removeEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw new IllegalArgumentException();\n        // Remove edge vet1 - vet2\n        adjList.get(vet1).remove(vet2);\n        adjList.get(vet2).remove(vet1);\n    }\n\n    /* Add vertex */\n    public void addVertex(Vertex vet) {\n        if (adjList.containsKey(vet))\n            return;\n        // Add a new linked list in the adjacency list\n        adjList.put(vet, new ArrayList<>());\n    }\n\n    /* Remove vertex */\n    public void removeVertex(Vertex vet) {\n        if (!adjList.containsKey(vet))\n            throw new IllegalArgumentException();\n        // Remove the linked list corresponding to vertex vet in the adjacency list\n        adjList.remove(vet);\n        // Traverse the linked lists of other vertices and remove all edges containing vet\n        for (List<Vertex> list : adjList.values()) {\n            list.remove(vet);\n        }\n    }\n\n    /* Print adjacency list */\n    public void print() {\n        System.out.println(\"Adjacency list =\");\n        for (Map.Entry<Vertex, List<Vertex>> pair : adjList.entrySet()) {\n            List<Integer> tmp = new ArrayList<>();\n            for (Vertex vertex : pair.getValue())\n                tmp.add(vertex.val);\n            System.out.println(pair.getKey().val + \": \" + tmp + \",\");\n        }\n    }\n}\n
      graph_adjacency_list.cs
      /* Undirected graph class based on adjacency list */\nclass GraphAdjList {\n    // Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n    public Dictionary<Vertex, List<Vertex>> adjList;\n\n    /* Constructor */\n    public GraphAdjList(Vertex[][] edges) {\n        adjList = [];\n        // Add all vertices and edges\n        foreach (Vertex[] edge in edges) {\n            AddVertex(edge[0]);\n            AddVertex(edge[1]);\n            AddEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* Get the number of vertices */\n    int Size() {\n        return adjList.Count;\n    }\n\n    /* Add edge */\n    public void AddEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\n            throw new InvalidOperationException();\n        // Add edge vet1 - vet2\n        adjList[vet1].Add(vet2);\n        adjList[vet2].Add(vet1);\n    }\n\n    /* Remove edge */\n    public void RemoveEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\n            throw new InvalidOperationException();\n        // Remove edge vet1 - vet2\n        adjList[vet1].Remove(vet2);\n        adjList[vet2].Remove(vet1);\n    }\n\n    /* Add vertex */\n    public void AddVertex(Vertex vet) {\n        if (adjList.ContainsKey(vet))\n            return;\n        // Add a new linked list in the adjacency list\n        adjList.Add(vet, []);\n    }\n\n    /* Remove vertex */\n    public void RemoveVertex(Vertex vet) {\n        if (!adjList.ContainsKey(vet))\n            throw new InvalidOperationException();\n        // Remove the linked list corresponding to vertex vet in the adjacency list\n        adjList.Remove(vet);\n        // Traverse the linked lists of other vertices and remove all edges containing vet\n        foreach (List<Vertex> list in adjList.Values) {\n            list.Remove(vet);\n        }\n    }\n\n    /* Print adjacency list */\n    public void Print() {\n        Console.WriteLine(\"Adjacency list =\");\n        foreach (KeyValuePair<Vertex, List<Vertex>> pair in adjList) {\n            List<int> tmp = [];\n            foreach (Vertex vertex in pair.Value)\n                tmp.Add(vertex.val);\n            Console.WriteLine(pair.Key.val + \": [\" + string.Join(\", \", tmp) + \"],\");\n        }\n    }\n}\n
      graph_adjacency_list.go
      /* Undirected graph class based on adjacency list */\ntype graphAdjList struct {\n    // Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n    adjList map[Vertex][]Vertex\n}\n\n/* Constructor */\nfunc newGraphAdjList(edges [][]Vertex) *graphAdjList {\n    g := &graphAdjList{\n        adjList: make(map[Vertex][]Vertex),\n    }\n    // Add all vertices and edges\n    for _, edge := range edges {\n        g.addVertex(edge[0])\n        g.addVertex(edge[1])\n        g.addEdge(edge[0], edge[1])\n    }\n    return g\n}\n\n/* Get the number of vertices */\nfunc (g *graphAdjList) size() int {\n    return len(g.adjList)\n}\n\n/* Add edge */\nfunc (g *graphAdjList) addEdge(vet1 Vertex, vet2 Vertex) {\n    _, ok1 := g.adjList[vet1]\n    _, ok2 := g.adjList[vet2]\n    if !ok1 || !ok2 || vet1 == vet2 {\n        panic(\"error\")\n    }\n    // Add edge vet1 - vet2, add anonymous struct{},\n    g.adjList[vet1] = append(g.adjList[vet1], vet2)\n    g.adjList[vet2] = append(g.adjList[vet2], vet1)\n}\n\n/* Remove edge */\nfunc (g *graphAdjList) removeEdge(vet1 Vertex, vet2 Vertex) {\n    _, ok1 := g.adjList[vet1]\n    _, ok2 := g.adjList[vet2]\n    if !ok1 || !ok2 || vet1 == vet2 {\n        panic(\"error\")\n    }\n    // Remove edge vet1 - vet2\n    g.adjList[vet1] = DeleteSliceElms(g.adjList[vet1], vet2)\n    g.adjList[vet2] = DeleteSliceElms(g.adjList[vet2], vet1)\n}\n\n/* Add vertex */\nfunc (g *graphAdjList) addVertex(vet Vertex) {\n    _, ok := g.adjList[vet]\n    if ok {\n        return\n    }\n    // Add a new linked list in the adjacency list\n    g.adjList[vet] = make([]Vertex, 0)\n}\n\n/* Remove vertex */\nfunc (g *graphAdjList) removeVertex(vet Vertex) {\n    _, ok := g.adjList[vet]\n    if !ok {\n        panic(\"error\")\n    }\n    // Remove the linked list corresponding to vertex vet in the adjacency list\n    delete(g.adjList, vet)\n    // Traverse the linked lists of other vertices and remove all edges containing vet\n    for v, list := range g.adjList {\n        g.adjList[v] = DeleteSliceElms(list, vet)\n    }\n}\n\n/* Print adjacency list */\nfunc (g *graphAdjList) print() {\n    var builder strings.Builder\n    fmt.Printf(\"Adjacency list = \\n\")\n    for k, v := range g.adjList {\n        builder.WriteString(\"\\t\\t\" + strconv.Itoa(k.Val) + \": \")\n        for _, vet := range v {\n            builder.WriteString(strconv.Itoa(vet.Val) + \" \")\n        }\n        fmt.Println(builder.String())\n        builder.Reset()\n    }\n}\n
      graph_adjacency_list.swift
      /* Undirected graph class based on adjacency list */\nclass GraphAdjList {\n    // Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n    public private(set) var adjList: [Vertex: [Vertex]]\n\n    /* Constructor */\n    public init(edges: [[Vertex]]) {\n        adjList = [:]\n        // Add all vertices and edges\n        for edge in edges {\n            addVertex(vet: edge[0])\n            addVertex(vet: edge[1])\n            addEdge(vet1: edge[0], vet2: edge[1])\n        }\n    }\n\n    /* Get the number of vertices */\n    public func size() -> Int {\n        adjList.count\n    }\n\n    /* Add edge */\n    public func addEdge(vet1: Vertex, vet2: Vertex) {\n        if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\n            fatalError(\"Invalid parameter\")\n        }\n        // Add edge vet1 - vet2\n        adjList[vet1]?.append(vet2)\n        adjList[vet2]?.append(vet1)\n    }\n\n    /* Remove edge */\n    public func removeEdge(vet1: Vertex, vet2: Vertex) {\n        if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\n            fatalError(\"Invalid parameter\")\n        }\n        // Remove edge vet1 - vet2\n        adjList[vet1]?.removeAll { $0 == vet2 }\n        adjList[vet2]?.removeAll { $0 == vet1 }\n    }\n\n    /* Add vertex */\n    public func addVertex(vet: Vertex) {\n        if adjList[vet] != nil {\n            return\n        }\n        // Add a new linked list in the adjacency list\n        adjList[vet] = []\n    }\n\n    /* Remove vertex */\n    public func removeVertex(vet: Vertex) {\n        if adjList[vet] == nil {\n            fatalError(\"Invalid parameter\")\n        }\n        // Remove the linked list corresponding to vertex vet in the adjacency list\n        adjList.removeValue(forKey: vet)\n        // Traverse the linked lists of other vertices and remove all edges containing vet\n        for key in adjList.keys {\n            adjList[key]?.removeAll { $0 == vet }\n        }\n    }\n\n    /* Print adjacency list */\n    public func print() {\n        Swift.print(\"Adjacency list =\")\n        for (vertex, list) in adjList {\n            let list = list.map { $0.val }\n            Swift.print(\"\\(vertex.val): \\(list),\")\n        }\n    }\n}\n
      graph_adjacency_list.js
      /* Undirected graph class based on adjacency list */\nclass GraphAdjList {\n    // Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n    adjList;\n\n    /* Constructor */\n    constructor(edges) {\n        this.adjList = new Map();\n        // Add all vertices and edges\n        for (const edge of edges) {\n            this.addVertex(edge[0]);\n            this.addVertex(edge[1]);\n            this.addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* Get the number of vertices */\n    size() {\n        return this.adjList.size;\n    }\n\n    /* Add edge */\n    addEdge(vet1, vet2) {\n        if (\n            !this.adjList.has(vet1) ||\n            !this.adjList.has(vet2) ||\n            vet1 === vet2\n        ) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // Add edge vet1 - vet2\n        this.adjList.get(vet1).push(vet2);\n        this.adjList.get(vet2).push(vet1);\n    }\n\n    /* Remove edge */\n    removeEdge(vet1, vet2) {\n        if (\n            !this.adjList.has(vet1) ||\n            !this.adjList.has(vet2) ||\n            vet1 === vet2 ||\n            this.adjList.get(vet1).indexOf(vet2) === -1\n        ) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // Remove edge vet1 - vet2\n        this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\n        this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n    }\n\n    /* Add vertex */\n    addVertex(vet) {\n        if (this.adjList.has(vet)) return;\n        // Add a new linked list in the adjacency list\n        this.adjList.set(vet, []);\n    }\n\n    /* Remove vertex */\n    removeVertex(vet) {\n        if (!this.adjList.has(vet)) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // Remove the linked list corresponding to vertex vet in the adjacency list\n        this.adjList.delete(vet);\n        // Traverse the linked lists of other vertices and remove all edges containing vet\n        for (const set of this.adjList.values()) {\n            const index = set.indexOf(vet);\n            if (index > -1) {\n                set.splice(index, 1);\n            }\n        }\n    }\n\n    /* Print adjacency list */\n    print() {\n        console.log('Adjacency list =');\n        for (const [key, value] of this.adjList) {\n            const tmp = [];\n            for (const vertex of value) {\n                tmp.push(vertex.val);\n            }\n            console.log(key.val + ': ' + tmp.join());\n        }\n    }\n}\n
      graph_adjacency_list.ts
      /* Undirected graph class based on adjacency list */\nclass GraphAdjList {\n    // Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n    adjList: Map<Vertex, Vertex[]>;\n\n    /* Constructor */\n    constructor(edges: Vertex[][]) {\n        this.adjList = new Map();\n        // Add all vertices and edges\n        for (const edge of edges) {\n            this.addVertex(edge[0]);\n            this.addVertex(edge[1]);\n            this.addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* Get the number of vertices */\n    size(): number {\n        return this.adjList.size;\n    }\n\n    /* Add edge */\n    addEdge(vet1: Vertex, vet2: Vertex): void {\n        if (\n            !this.adjList.has(vet1) ||\n            !this.adjList.has(vet2) ||\n            vet1 === vet2\n        ) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // Add edge vet1 - vet2\n        this.adjList.get(vet1).push(vet2);\n        this.adjList.get(vet2).push(vet1);\n    }\n\n    /* Remove edge */\n    removeEdge(vet1: Vertex, vet2: Vertex): void {\n        if (\n            !this.adjList.has(vet1) ||\n            !this.adjList.has(vet2) ||\n            vet1 === vet2 ||\n            this.adjList.get(vet1).indexOf(vet2) === -1\n        ) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // Remove edge vet1 - vet2\n        this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\n        this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n    }\n\n    /* Add vertex */\n    addVertex(vet: Vertex): void {\n        if (this.adjList.has(vet)) return;\n        // Add a new linked list in the adjacency list\n        this.adjList.set(vet, []);\n    }\n\n    /* Remove vertex */\n    removeVertex(vet: Vertex): void {\n        if (!this.adjList.has(vet)) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // Remove the linked list corresponding to vertex vet in the adjacency list\n        this.adjList.delete(vet);\n        // Traverse the linked lists of other vertices and remove all edges containing vet\n        for (const set of this.adjList.values()) {\n            const index: number = set.indexOf(vet);\n            if (index > -1) {\n                set.splice(index, 1);\n            }\n        }\n    }\n\n    /* Print adjacency list */\n    print(): void {\n        console.log('Adjacency list =');\n        for (const [key, value] of this.adjList.entries()) {\n            const tmp = [];\n            for (const vertex of value) {\n                tmp.push(vertex.val);\n            }\n            console.log(key.val + ': ' + tmp.join());\n        }\n    }\n}\n
      graph_adjacency_list.dart
      /* Undirected graph class based on adjacency list */\nclass GraphAdjList {\n  // Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n  Map<Vertex, List<Vertex>> adjList = {};\n\n  /* Constructor */\n  GraphAdjList(List<List<Vertex>> edges) {\n    for (List<Vertex> edge in edges) {\n      addVertex(edge[0]);\n      addVertex(edge[1]);\n      addEdge(edge[0], edge[1]);\n    }\n  }\n\n  /* Get the number of vertices */\n  int size() {\n    return adjList.length;\n  }\n\n  /* Add edge */\n  void addEdge(Vertex vet1, Vertex vet2) {\n    if (!adjList.containsKey(vet1) ||\n        !adjList.containsKey(vet2) ||\n        vet1 == vet2) {\n      throw ArgumentError;\n    }\n    // Add edge vet1 - vet2\n    adjList[vet1]!.add(vet2);\n    adjList[vet2]!.add(vet1);\n  }\n\n  /* Remove edge */\n  void removeEdge(Vertex vet1, Vertex vet2) {\n    if (!adjList.containsKey(vet1) ||\n        !adjList.containsKey(vet2) ||\n        vet1 == vet2) {\n      throw ArgumentError;\n    }\n    // Remove edge vet1 - vet2\n    adjList[vet1]!.remove(vet2);\n    adjList[vet2]!.remove(vet1);\n  }\n\n  /* Add vertex */\n  void addVertex(Vertex vet) {\n    if (adjList.containsKey(vet)) return;\n    // Add a new linked list in the adjacency list\n    adjList[vet] = [];\n  }\n\n  /* Remove vertex */\n  void removeVertex(Vertex vet) {\n    if (!adjList.containsKey(vet)) {\n      throw ArgumentError;\n    }\n    // Remove the linked list corresponding to vertex vet in the adjacency list\n    adjList.remove(vet);\n    // Traverse the linked lists of other vertices and remove all edges containing vet\n    adjList.forEach((key, value) {\n      value.remove(vet);\n    });\n  }\n\n  /* Print adjacency list */\n  void printAdjList() {\n    print(\"Adjacency list =\");\n    adjList.forEach((key, value) {\n      List<int> tmp = [];\n      for (Vertex vertex in value) {\n        tmp.add(vertex.val);\n      }\n      print(\"${key.val}: $tmp,\");\n    });\n  }\n}\n
      graph_adjacency_list.rs
      /* Undirected graph type based on adjacency list */\npub struct GraphAdjList {\n    // Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n    pub adj_list: HashMap<Vertex, Vec<Vertex>>, // maybe HashSet<Vertex> for value part is better?\n}\n\nimpl GraphAdjList {\n    /* Constructor */\n    pub fn new(edges: Vec<[Vertex; 2]>) -> Self {\n        let mut graph = GraphAdjList {\n            adj_list: HashMap::new(),\n        };\n        // Add all vertices and edges\n        for edge in edges {\n            graph.add_vertex(edge[0]);\n            graph.add_vertex(edge[1]);\n            graph.add_edge(edge[0], edge[1]);\n        }\n\n        graph\n    }\n\n    /* Get the number of vertices */\n    #[allow(unused)]\n    pub fn size(&self) -> usize {\n        self.adj_list.len()\n    }\n\n    /* Add edge */\n    pub fn add_edge(&mut self, vet1: Vertex, vet2: Vertex) {\n        if vet1 == vet2 {\n            panic!(\"value error\");\n        }\n        // Add edge vet1 - vet2\n        self.adj_list.entry(vet1).or_default().push(vet2);\n        self.adj_list.entry(vet2).or_default().push(vet1);\n    }\n\n    /* Remove edge */\n    #[allow(unused)]\n    pub fn remove_edge(&mut self, vet1: Vertex, vet2: Vertex) {\n        if vet1 == vet2 {\n            panic!(\"value error\");\n        }\n        // Remove edge vet1 - vet2\n        self.adj_list\n            .entry(vet1)\n            .and_modify(|v| v.retain(|&e| e != vet2));\n        self.adj_list\n            .entry(vet2)\n            .and_modify(|v| v.retain(|&e| e != vet1));\n    }\n\n    /* Add vertex */\n    pub fn add_vertex(&mut self, vet: Vertex) {\n        if self.adj_list.contains_key(&vet) {\n            return;\n        }\n        // Add a new linked list in the adjacency list\n        self.adj_list.insert(vet, vec![]);\n    }\n\n    /* Remove vertex */\n    #[allow(unused)]\n    pub fn remove_vertex(&mut self, vet: Vertex) {\n        // Remove the linked list corresponding to vertex vet in the adjacency list\n        self.adj_list.remove(&vet);\n        // Traverse the linked lists of other vertices and remove all edges containing vet\n        for list in self.adj_list.values_mut() {\n            list.retain(|&v| v != vet);\n        }\n    }\n\n    /* Print adjacency list */\n    pub fn print(&self) {\n        println!(\"Adjacency list =\");\n        for (vertex, list) in &self.adj_list {\n            let list = list.iter().map(|vertex| vertex.val).collect::<Vec<i32>>();\n            println!(\"{}: {:?},\", vertex.val, list);\n        }\n    }\n}\n
      graph_adjacency_list.c
      /* Node structure */\ntypedef struct AdjListNode {\n    Vertex *vertex;           // Vertex\n    struct AdjListNode *next; // Successor node\n} AdjListNode;\n\n/* Find node corresponding to vertex */\nAdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {\n    for (int i = 0; i < graph->size; i++) {\n        if (graph->heads[i]->vertex == vet) {\n            return graph->heads[i];\n        }\n    }\n    return NULL;\n}\n\n/* Add edge helper function */\nvoid addEdgeHelper(AdjListNode *head, Vertex *vet) {\n    AdjListNode *node = (AdjListNode *)malloc(sizeof(AdjListNode));\n    node->vertex = vet;\n    // Head insertion\n    node->next = head->next;\n    head->next = node;\n}\n\n/* Remove edge helper function */\nvoid removeEdgeHelper(AdjListNode *head, Vertex *vet) {\n    AdjListNode *pre = head;\n    AdjListNode *cur = head->next;\n    // Search for node corresponding to vet in list\n    while (cur != NULL && cur->vertex != vet) {\n        pre = cur;\n        cur = cur->next;\n    }\n    if (cur == NULL)\n        return;\n    // Remove node corresponding to vet from list\n    pre->next = cur->next;\n    // Free memory\n    free(cur);\n}\n\n/* Undirected graph class based on adjacency list */\ntypedef struct {\n    AdjListNode *heads[MAX_SIZE]; // Node array\n    int size;                     // Node count\n} GraphAdjList;\n\n/* Constructor */\nGraphAdjList *newGraphAdjList() {\n    GraphAdjList *graph = (GraphAdjList *)malloc(sizeof(GraphAdjList));\n    if (!graph) {\n        return NULL;\n    }\n    graph->size = 0;\n    for (int i = 0; i < MAX_SIZE; i++) {\n        graph->heads[i] = NULL;\n    }\n    return graph;\n}\n\n/* Destructor */\nvoid delGraphAdjList(GraphAdjList *graph) {\n    for (int i = 0; i < graph->size; i++) {\n        AdjListNode *cur = graph->heads[i];\n        while (cur != NULL) {\n            AdjListNode *next = cur->next;\n            if (cur != graph->heads[i]) {\n                free(cur);\n            }\n            cur = next;\n        }\n        free(graph->heads[i]->vertex);\n        free(graph->heads[i]);\n    }\n    free(graph);\n}\n\n/* Find node corresponding to vertex */\nAdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {\n    for (int i = 0; i < graph->size; i++) {\n        if (graph->heads[i]->vertex == vet) {\n            return graph->heads[i];\n        }\n    }\n    return NULL;\n}\n\n/* Add edge */\nvoid addEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) {\n    AdjListNode *head1 = findNode(graph, vet1);\n    AdjListNode *head2 = findNode(graph, vet2);\n    assert(head1 != NULL && head2 != NULL && head1 != head2);\n    // Add edge vet1 - vet2\n    addEdgeHelper(head1, vet2);\n    addEdgeHelper(head2, vet1);\n}\n\n/* Remove edge */\nvoid removeEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) {\n    AdjListNode *head1 = findNode(graph, vet1);\n    AdjListNode *head2 = findNode(graph, vet2);\n    assert(head1 != NULL && head2 != NULL);\n    // Remove edge vet1 - vet2\n    removeEdgeHelper(head1, head2->vertex);\n    removeEdgeHelper(head2, head1->vertex);\n}\n\n/* Add vertex */\nvoid addVertex(GraphAdjList *graph, Vertex *vet) {\n    assert(graph != NULL && graph->size < MAX_SIZE);\n    AdjListNode *head = (AdjListNode *)malloc(sizeof(AdjListNode));\n    head->vertex = vet;\n    head->next = NULL;\n    // Add a new linked list in the adjacency list\n    graph->heads[graph->size++] = head;\n}\n\n/* Remove vertex */\nvoid removeVertex(GraphAdjList *graph, Vertex *vet) {\n    AdjListNode *node = findNode(graph, vet);\n    assert(node != NULL);\n    // Remove the linked list corresponding to vertex vet in the adjacency list\n    AdjListNode *cur = node, *pre = NULL;\n    while (cur) {\n        pre = cur;\n        cur = cur->next;\n        free(pre);\n    }\n    // Traverse the linked lists of other vertices and remove all edges containing vet\n    for (int i = 0; i < graph->size; i++) {\n        cur = graph->heads[i];\n        pre = NULL;\n        while (cur) {\n            pre = cur;\n            cur = cur->next;\n            if (cur && cur->vertex == vet) {\n                pre->next = cur->next;\n                free(cur);\n                break;\n            }\n        }\n    }\n    // Move vertices after this vertex forward to fill gap\n    int i;\n    for (i = 0; i < graph->size; i++) {\n        if (graph->heads[i] == node)\n            break;\n    }\n    for (int j = i; j < graph->size - 1; j++) {\n        graph->heads[j] = graph->heads[j + 1];\n    }\n    graph->size--;\n    free(vet);\n}\n
      graph_adjacency_list.kt
      /* Undirected graph class based on adjacency list */\nclass GraphAdjList(edges: Array<Array<Vertex?>>) {\n    // Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n    val adjList = HashMap<Vertex, MutableList<Vertex>>()\n\n    /* Constructor */\n    init {\n        // Add all vertices and edges\n        for (edge in edges) {\n            addVertex(edge[0]!!)\n            addVertex(edge[1]!!)\n            addEdge(edge[0]!!, edge[1]!!)\n        }\n    }\n\n    /* Get the number of vertices */\n    fun size(): Int {\n        return adjList.size\n    }\n\n    /* Add edge */\n    fun addEdge(vet1: Vertex, vet2: Vertex) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw IllegalArgumentException()\n        // Add edge vet1 - vet2\n        adjList[vet1]?.add(vet2)\n        adjList[vet2]?.add(vet1)\n    }\n\n    /* Remove edge */\n    fun removeEdge(vet1: Vertex, vet2: Vertex) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw IllegalArgumentException()\n        // Remove edge vet1 - vet2\n        adjList[vet1]?.remove(vet2)\n        adjList[vet2]?.remove(vet1)\n    }\n\n    /* Add vertex */\n    fun addVertex(vet: Vertex) {\n        if (adjList.containsKey(vet))\n            return\n        // Add a new linked list in the adjacency list\n        adjList[vet] = mutableListOf()\n    }\n\n    /* Remove vertex */\n    fun removeVertex(vet: Vertex) {\n        if (!adjList.containsKey(vet))\n            throw IllegalArgumentException()\n        // Remove the linked list corresponding to vertex vet in the adjacency list\n        adjList.remove(vet)\n        // Traverse the linked lists of other vertices and remove all edges containing vet\n        for (list in adjList.values) {\n            list.remove(vet)\n        }\n    }\n\n    /* Print adjacency list */\n    fun print() {\n        println(\"Adjacency list =\")\n        for (pair in adjList.entries) {\n            val tmp = mutableListOf<Int>()\n            for (vertex in pair.value) {\n                tmp.add(vertex._val)\n            }\n            println(\"${pair.key._val}: $tmp,\")\n        }\n    }\n}\n
      graph_adjacency_list.rb
      ### Undirected graph class based on adjacency list ###\nclass GraphAdjList\n  attr_reader :adj_list\n\n  ### Constructor ###\n  def initialize(edges)\n    # Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n    @adj_list = {}\n    # Add all vertices and edges\n    for edge in edges\n      add_vertex(edge[0])\n      add_vertex(edge[1])\n      add_edge(edge[0], edge[1])\n    end\n  end\n\n  ### Get number of vertices ###\n  def size\n    @adj_list.length\n  end\n\n  ### Add edge ###\n  def add_edge(vet1, vet2)\n    raise ArgumentError if !@adj_list.include?(vet1) || !@adj_list.include?(vet2)\n\n    @adj_list[vet1] << vet2\n    @adj_list[vet2] << vet1\n  end\n\n  ### Delete edge ###\n  def remove_edge(vet1, vet2)\n    raise ArgumentError if !@adj_list.include?(vet1) || !@adj_list.include?(vet2)\n\n    # Remove edge vet1 - vet2\n    @adj_list[vet1].delete(vet2)\n    @adj_list[vet2].delete(vet1)\n  end\n\n  ### Add vertex ###\n  def add_vertex(vet)\n    return if @adj_list.include?(vet)\n\n    # Add a new linked list in the adjacency list\n    @adj_list[vet] = []\n  end\n\n  ### Delete vertex ###\n  def remove_vertex(vet)\n    raise ArgumentError unless @adj_list.include?(vet)\n\n    # Remove the linked list corresponding to vertex vet in the adjacency list\n    @adj_list.delete(vet)\n    # Traverse the linked lists of other vertices and remove all edges containing vet\n    for vertex in @adj_list\n      @adj_list[vertex.first].delete(vet) if @adj_list[vertex.first].include?(vet)\n    end\n  end\n\n  ### Print adjacency list ###\n  def __print__\n    puts 'Adjacency list ='\n    for vertex in @adj_list\n      tmp = @adj_list[vertex.first].map { |v| v.val }\n      puts \"#{vertex.first.val}: #{tmp},\"\n    end\n  end\nend\n
      "},{"location":"chapter_graph/graph_operations/#923-efficiency-comparison","title":"9.2.3 \u00a0 Efficiency Comparison","text":"

      Assuming the graph has \\(n\\) vertices and \\(m\\) edges, Table 9-2 compares the time efficiency and space efficiency of adjacency matrices and adjacency lists. Note that the adjacency list (linked list) corresponds to the implementation in this text, while the adjacency list (hash table) refers specifically to the implementation where all linked lists are replaced with hash tables.

      Table 9-2 \u00a0 Comparison of adjacency matrix and adjacency list

      Adjacency matrix Adjacency list (linked list) Adjacency list (hash table) Determine adjacency \\(O(1)\\) \\(O(n)\\) \\(O(1)\\) Add an edge \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) Remove an edge \\(O(1)\\) \\(O(n)\\) \\(O(1)\\) Add a vertex \\(O(n)\\) \\(O(1)\\) \\(O(1)\\) Remove a vertex \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n)\\) Memory space usage \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n + m)\\)

      Observing Table 9-2, it appears that the adjacency list (hash table) has the best time efficiency and space efficiency. However, in practice, operating on edges in the adjacency matrix is more efficient, requiring only a single array access or assignment operation. Overall, adjacency matrices embody the principle of \"trading space for time\", while adjacency lists embody \"trading time for space\".

      "},{"location":"chapter_graph/graph_traversal/","title":"9.3 \u00a0 Graph Traversal","text":"

      Trees represent \"one-to-many\" relationships, while graphs have a higher degree of freedom and can represent any \"many-to-many\" relationships. Therefore, we can view trees as a special case of graphs. Clearly, tree traversal operations are also a special case of graph traversal operations.

      Both graphs and trees require the application of search algorithms to implement traversal operations. Graph traversal methods can also be divided into two types: breadth-first traversal and depth-first traversal.

      "},{"location":"chapter_graph/graph_traversal/#931-breadth-first-search","title":"9.3.1 \u00a0 Breadth-First Search","text":"

      Breadth-first search is a near-to-far traversal method that, starting from a certain node, always prioritizes visiting the nearest vertices and expands outward layer by layer. As shown in Figure 9-9, starting from the top-left vertex, first traverse all adjacent vertices of that vertex, then traverse all adjacent vertices of the next vertex, and so on, until all vertices have been visited.

      Figure 9-9 \u00a0 Breadth-first search of a graph

      "},{"location":"chapter_graph/graph_traversal/#1-algorithm-implementation","title":"1. \u00a0 Algorithm Implementation","text":"

      BFS is typically implemented with the help of a queue, as shown in the code below. The queue has a \"first in, first out\" property, which aligns with the BFS idea of \"near to far\".

      1. Add the starting vertex startVet to the queue and begin the loop.
      2. In each iteration of the loop, pop the vertex at the front of the queue and record it as visited, then add all adjacent vertices of that vertex to the back of the queue.
      3. Repeat step 2. until all vertices have been visited.

      To prevent revisiting vertices, we use a hash set visited to record which nodes have been visited.

      Tip

      A hash set can be viewed as a hash table that stores only key without storing value. It can perform addition, deletion, lookup, and modification operations on key in \\(O(1)\\) time complexity. Based on the uniqueness of key, hash sets are typically used for data deduplication and similar scenarios.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_bfs.py
      def graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n    \"\"\"Breadth-first traversal\"\"\"\n    # Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\n    # Vertex traversal sequence\n    res = []\n    # Hash set for recording vertices that have been visited\n    visited = set[Vertex]([start_vet])\n    # Queue used to implement BFS\n    que = deque[Vertex]([start_vet])\n    # Starting from vertex vet, loop until all vertices are visited\n    while len(que) > 0:\n        vet = que.popleft()  # Dequeue the front vertex\n        res.append(vet)  # Record visited vertex\n        # Traverse all adjacent vertices of this vertex\n        for adj_vet in graph.adj_list[vet]:\n            if adj_vet in visited:\n                continue  # Skip vertices that have been visited\n            que.append(adj_vet)  # Only enqueue unvisited vertices\n            visited.add(adj_vet)  # Mark this vertex as visited\n    # Return vertex traversal sequence\n    return res\n
      graph_bfs.cpp
      /* Breadth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nvector<Vertex *> graphBFS(GraphAdjList &graph, Vertex *startVet) {\n    // Vertex traversal sequence\n    vector<Vertex *> res;\n    // Hash set for recording vertices that have been visited\n    unordered_set<Vertex *> visited = {startVet};\n    // Queue used to implement BFS\n    queue<Vertex *> que;\n    que.push(startVet);\n    // Starting from vertex vet, loop until all vertices are visited\n    while (!que.empty()) {\n        Vertex *vet = que.front();\n        que.pop();          // Dequeue the front vertex\n        res.push_back(vet); // Record visited vertex\n        // Traverse all adjacent vertices of this vertex\n        for (auto adjVet : graph.adjList[vet]) {\n            if (visited.count(adjVet))\n                continue;            // Skip vertices that have been visited\n            que.push(adjVet);        // Only enqueue unvisited vertices\n            visited.emplace(adjVet); // Mark this vertex as visited\n        }\n    }\n    // Return vertex traversal sequence\n    return res;\n}\n
      graph_bfs.java
      /* Breadth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nList<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {\n    // Vertex traversal sequence\n    List<Vertex> res = new ArrayList<>();\n    // Hash set for recording vertices that have been visited\n    Set<Vertex> visited = new HashSet<>();\n    visited.add(startVet);\n    // Queue used to implement BFS\n    Queue<Vertex> que = new LinkedList<>();\n    que.offer(startVet);\n    // Starting from vertex vet, loop until all vertices are visited\n    while (!que.isEmpty()) {\n        Vertex vet = que.poll(); // Dequeue the front vertex\n        res.add(vet);            // Record visited vertex\n        // Traverse all adjacent vertices of this vertex\n        for (Vertex adjVet : graph.adjList.get(vet)) {\n            if (visited.contains(adjVet))\n                continue;        // Skip vertices that have been visited\n            que.offer(adjVet);   // Only enqueue unvisited vertices\n            visited.add(adjVet); // Mark this vertex as visited\n        }\n    }\n    // Return vertex traversal sequence\n    return res;\n}\n
      graph_bfs.cs
      /* Breadth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nList<Vertex> GraphBFS(GraphAdjList graph, Vertex startVet) {\n    // Vertex traversal sequence\n    List<Vertex> res = [];\n    // Hash set for recording vertices that have been visited\n    HashSet<Vertex> visited = [startVet];\n    // Queue used to implement BFS\n    Queue<Vertex> que = new();\n    que.Enqueue(startVet);\n    // Starting from vertex vet, loop until all vertices are visited\n    while (que.Count > 0) {\n        Vertex vet = que.Dequeue(); // Dequeue the front vertex\n        res.Add(vet);               // Record visited vertex\n        foreach (Vertex adjVet in graph.adjList[vet]) {\n            if (visited.Contains(adjVet)) {\n                continue;          // Skip vertices that have been visited\n            }\n            que.Enqueue(adjVet);   // Only enqueue unvisited vertices\n            visited.Add(adjVet);   // Mark this vertex as visited\n        }\n    }\n\n    // Return vertex traversal sequence\n    return res;\n}\n
      graph_bfs.go
      /* Breadth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfunc graphBFS(g *graphAdjList, startVet Vertex) []Vertex {\n    // Vertex traversal sequence\n    res := make([]Vertex, 0)\n    // Hash set for recording vertices that have been visited\n    visited := make(map[Vertex]struct{})\n    visited[startVet] = struct{}{}\n    // Queue used to implement BFS, using slice to simulate queue\n    queue := make([]Vertex, 0)\n    queue = append(queue, startVet)\n    // Starting from vertex vet, loop until all vertices are visited\n    for len(queue) > 0 {\n        // Dequeue the front vertex\n        vet := queue[0]\n        queue = queue[1:]\n        // Record visited vertex\n        res = append(res, vet)\n        // Traverse all adjacent vertices of this vertex\n        for _, adjVet := range g.adjList[vet] {\n            _, isExist := visited[adjVet]\n            // Only enqueue unvisited vertices\n            if !isExist {\n                queue = append(queue, adjVet)\n                visited[adjVet] = struct{}{}\n            }\n        }\n    }\n    // Return vertex traversal sequence\n    return res\n}\n
      graph_bfs.swift
      /* Breadth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfunc graphBFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {\n    // Vertex traversal sequence\n    var res: [Vertex] = []\n    // Hash set for recording vertices that have been visited\n    var visited: Set<Vertex> = [startVet]\n    // Queue used to implement BFS\n    var que: [Vertex] = [startVet]\n    // Starting from vertex vet, loop until all vertices are visited\n    while !que.isEmpty {\n        let vet = que.removeFirst() // Dequeue the front vertex\n        res.append(vet) // Record visited vertex\n        // Traverse all adjacent vertices of this vertex\n        for adjVet in graph.adjList[vet] ?? [] {\n            if visited.contains(adjVet) {\n                continue // Skip vertices that have been visited\n            }\n            que.append(adjVet) // Only enqueue unvisited vertices\n            visited.insert(adjVet) // Mark this vertex as visited\n        }\n    }\n    // Return vertex traversal sequence\n    return res\n}\n
      graph_bfs.js
      /* Breadth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfunction graphBFS(graph, startVet) {\n    // Vertex traversal sequence\n    const res = [];\n    // Hash set for recording vertices that have been visited\n    const visited = new Set();\n    visited.add(startVet);\n    // Queue used to implement BFS\n    const que = [startVet];\n    // Starting from vertex vet, loop until all vertices are visited\n    while (que.length) {\n        const vet = que.shift(); // Dequeue the front vertex\n        res.push(vet); // Record visited vertex\n        // Traverse all adjacent vertices of this vertex\n        for (const adjVet of graph.adjList.get(vet) ?? []) {\n            if (visited.has(adjVet)) {\n                continue; // Skip vertices that have been visited\n            }\n            que.push(adjVet); // Only enqueue unvisited vertices\n            visited.add(adjVet); // Mark this vertex as visited\n        }\n    }\n    // Return vertex traversal sequence\n    return res;\n}\n
      graph_bfs.ts
      /* Breadth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfunction graphBFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n    // Vertex traversal sequence\n    const res: Vertex[] = [];\n    // Hash set for recording vertices that have been visited\n    const visited: Set<Vertex> = new Set();\n    visited.add(startVet);\n    // Queue used to implement BFS\n    const que = [startVet];\n    // Starting from vertex vet, loop until all vertices are visited\n    while (que.length) {\n        const vet = que.shift(); // Dequeue the front vertex\n        res.push(vet); // Record visited vertex\n        // Traverse all adjacent vertices of this vertex\n        for (const adjVet of graph.adjList.get(vet) ?? []) {\n            if (visited.has(adjVet)) {\n                continue; // Skip vertices that have been visited\n            }\n            que.push(adjVet); // Only enqueue unvisited\n            visited.add(adjVet); // Mark this vertex as visited\n        }\n    }\n    // Return vertex traversal sequence\n    return res;\n}\n
      graph_bfs.dart
      /* Breadth-first traversal */\nList<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {\n  // Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\n  // Vertex traversal sequence\n  List<Vertex> res = [];\n  // Hash set for recording vertices that have been visited\n  Set<Vertex> visited = {};\n  visited.add(startVet);\n  // Queue used to implement BFS\n  Queue<Vertex> que = Queue();\n  que.add(startVet);\n  // Starting from vertex vet, loop until all vertices are visited\n  while (que.isNotEmpty) {\n    Vertex vet = que.removeFirst(); // Dequeue the front vertex\n    res.add(vet); // Record visited vertex\n    // Traverse all adjacent vertices of this vertex\n    for (Vertex adjVet in graph.adjList[vet]!) {\n      if (visited.contains(adjVet)) {\n        continue; // Skip vertices that have been visited\n      }\n      que.add(adjVet); // Only enqueue unvisited vertices\n      visited.add(adjVet); // Mark this vertex as visited\n    }\n  }\n  // Return vertex traversal sequence\n  return res;\n}\n
      graph_bfs.rs
      /* Breadth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfn graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {\n    // Vertex traversal sequence\n    let mut res = vec![];\n    // Hash set for recording vertices that have been visited\n    let mut visited = HashSet::new();\n    visited.insert(start_vet);\n    // Queue used to implement BFS\n    let mut que = VecDeque::new();\n    que.push_back(start_vet);\n    // Starting from vertex vet, loop until all vertices are visited\n    while let Some(vet) = que.pop_front() {\n        res.push(vet); // Record visited vertex\n\n        // Traverse all adjacent vertices of this vertex\n        if let Some(adj_vets) = graph.adj_list.get(&vet) {\n            for &adj_vet in adj_vets {\n                if visited.contains(&adj_vet) {\n                    continue; // Skip vertices that have been visited\n                }\n                que.push_back(adj_vet); // Only enqueue unvisited vertices\n                visited.insert(adj_vet); // Mark this vertex as visited\n            }\n        }\n    }\n    // Return vertex traversal sequence\n    res\n}\n
      graph_bfs.c
      /* Node queue structure */\ntypedef struct {\n    Vertex *vertices[MAX_SIZE];\n    int front, rear, size;\n} Queue;\n\n/* Constructor */\nQueue *newQueue() {\n    Queue *q = (Queue *)malloc(sizeof(Queue));\n    q->front = q->rear = q->size = 0;\n    return q;\n}\n\n/* Check if the queue is empty */\nint isEmpty(Queue *q) {\n    return q->size == 0;\n}\n\n/* Enqueue operation */\nvoid enqueue(Queue *q, Vertex *vet) {\n    q->vertices[q->rear] = vet;\n    q->rear = (q->rear + 1) % MAX_SIZE;\n    q->size++;\n}\n\n/* Dequeue operation */\nVertex *dequeue(Queue *q) {\n    Vertex *vet = q->vertices[q->front];\n    q->front = (q->front + 1) % MAX_SIZE;\n    q->size--;\n    return vet;\n}\n\n/* Check if vertex has been visited */\nint isVisited(Vertex **visited, int size, Vertex *vet) {\n    // Traverse to find node using O(n) time\n    for (int i = 0; i < size; i++) {\n        if (visited[i] == vet)\n            return 1;\n    }\n    return 0;\n}\n\n/* Breadth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nvoid graphBFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize, Vertex **visited, int *visitedSize) {\n    // Queue used to implement BFS\n    Queue *queue = newQueue();\n    enqueue(queue, startVet);\n    visited[(*visitedSize)++] = startVet;\n    // Starting from vertex vet, loop until all vertices are visited\n    while (!isEmpty(queue)) {\n        Vertex *vet = dequeue(queue); // Dequeue the front vertex\n        res[(*resSize)++] = vet;      // Record visited vertex\n        // Traverse all adjacent vertices of this vertex\n        AdjListNode *node = findNode(graph, vet);\n        while (node != NULL) {\n            // Skip vertices that have been visited\n            if (!isVisited(visited, *visitedSize, node->vertex)) {\n                enqueue(queue, node->vertex);             // Only enqueue unvisited vertices\n                visited[(*visitedSize)++] = node->vertex; // Mark this vertex as visited\n            }\n            node = node->next;\n        }\n    }\n    // Free memory\n    free(queue);\n}\n
      graph_bfs.kt
      /* Breadth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfun graphBFS(graph: GraphAdjList, startVet: Vertex): MutableList<Vertex?> {\n    // Vertex traversal sequence\n    val res = mutableListOf<Vertex?>()\n    // Hash set for recording vertices that have been visited\n    val visited = HashSet<Vertex>()\n    visited.add(startVet)\n    // Queue used to implement BFS\n    val que = LinkedList<Vertex>()\n    que.offer(startVet)\n    // Starting from vertex vet, loop until all vertices are visited\n    while (!que.isEmpty()) {\n        val vet = que.poll() // Dequeue the front vertex\n        res.add(vet)         // Record visited vertex\n        // Traverse all adjacent vertices of this vertex\n        for (adjVet in graph.adjList[vet]!!) {\n            if (visited.contains(adjVet))\n                continue        // Skip vertices that have been visited\n            que.offer(adjVet)   // Only enqueue unvisited vertices\n            visited.add(adjVet) // Mark this vertex as visited\n        }\n    }\n    // Return vertex traversal sequence\n    return res\n}\n
      graph_bfs.rb
      ### Breadth-first traversal ###\ndef graph_bfs(graph, start_vet)\n  # Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\n  # Vertex traversal sequence\n  res = []\n  # Hash set for recording vertices that have been visited\n  visited = Set.new([start_vet])\n  # Queue used to implement BFS\n  que = [start_vet]\n  # Starting from vertex vet, loop until all vertices are visited\n  while que.length > 0\n    vet = que.shift # Dequeue the front vertex\n    res << vet # Record visited vertex\n    # Traverse all adjacent vertices of this vertex\n    for adj_vet in graph.adj_list[vet]\n      next if visited.include?(adj_vet) # Skip vertices that have been visited\n      que << adj_vet # Only enqueue unvisited vertices\n      visited.add(adj_vet) # Mark this vertex as visited\n    end\n  end\n  # Return vertex traversal sequence\n  res\nend\n

      The code is relatively abstract; it is recommended to refer to Figure 9-10 to deepen understanding.

      <1><2><3><4><5><6><7><8><9><10><11>

      Figure 9-10 \u00a0 Steps of breadth-first search of a graph

      Is the breadth-first traversal sequence unique?

      Not unique. Breadth-first search only requires traversing in a \"near to far\" order, and the traversal order of vertices at the same distance can be arbitrarily shuffled. Taking Figure 9-10 as an example, the visit order of vertices \\(1\\) and \\(3\\) can be swapped, as can the visit order of vertices \\(2\\), \\(4\\), and \\(6\\).

      "},{"location":"chapter_graph/graph_traversal/#2-complexity-analysis","title":"2. \u00a0 Complexity Analysis","text":"

      Time complexity: All vertices will be enqueued and dequeued once, using \\(O(|V|)\\) time; in the process of traversing adjacent vertices, since it is an undirected graph, all edges will be visited \\(2\\) times, using \\(O(2|E|)\\) time; overall using \\(O(|V| + |E|)\\) time.

      Space complexity: The list res, hash set visited, and queue que can contain at most \\(|V|\\) vertices, using \\(O(|V|)\\) space.

      "},{"location":"chapter_graph/graph_traversal/#932-depth-first-search","title":"9.3.2 \u00a0 Depth-First Search","text":"

      Depth-first search is a traversal method that prioritizes going as far as possible, then backtracks when no path remains. As shown in Figure 9-11, starting from the top-left vertex, visit an adjacent vertex of the current vertex, continuing until reaching a dead end, then return and continue going as far as possible before returning again, and so on, until all vertices have been traversed.

      Figure 9-11 \u00a0 Depth-first search of a graph

      "},{"location":"chapter_graph/graph_traversal/#1-algorithm-implementation_1","title":"1. \u00a0 Algorithm Implementation","text":"

      This \"go as far as possible then return\" algorithm paradigm is typically implemented using recursion. Similar to breadth-first search, in depth-first search we also need a hash set visited to record visited vertices and avoid revisiting.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_dfs.py
      def dfs(graph: GraphAdjList, visited: set[Vertex], res: list[Vertex], vet: Vertex):\n    \"\"\"Depth-first traversal helper function\"\"\"\n    res.append(vet)  # Record visited vertex\n    visited.add(vet)  # Mark this vertex as visited\n    # Traverse all adjacent vertices of this vertex\n    for adjVet in graph.adj_list[vet]:\n        if adjVet in visited:\n            continue  # Skip vertices that have been visited\n        # Recursively visit adjacent vertices\n        dfs(graph, visited, res, adjVet)\n\ndef graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n    \"\"\"Depth-first traversal\"\"\"\n    # Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\n    # Vertex traversal sequence\n    res = []\n    # Hash set for recording vertices that have been visited\n    visited = set[Vertex]()\n    dfs(graph, visited, res, start_vet)\n    return res\n
      graph_dfs.cpp
      /* Depth-first traversal helper function */\nvoid dfs(GraphAdjList &graph, unordered_set<Vertex *> &visited, vector<Vertex *> &res, Vertex *vet) {\n    res.push_back(vet);   // Record visited vertex\n    visited.emplace(vet); // Mark this vertex as visited\n    // Traverse all adjacent vertices of this vertex\n    for (Vertex *adjVet : graph.adjList[vet]) {\n        if (visited.count(adjVet))\n            continue; // Skip vertices that have been visited\n        // Recursively visit adjacent vertices\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* Depth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nvector<Vertex *> graphDFS(GraphAdjList &graph, Vertex *startVet) {\n    // Vertex traversal sequence\n    vector<Vertex *> res;\n    // Hash set for recording vertices that have been visited\n    unordered_set<Vertex *> visited;\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
      graph_dfs.java
      /* Depth-first traversal helper function */\nvoid dfs(GraphAdjList graph, Set<Vertex> visited, List<Vertex> res, Vertex vet) {\n    res.add(vet);     // Record visited vertex\n    visited.add(vet); // Mark this vertex as visited\n    // Traverse all adjacent vertices of this vertex\n    for (Vertex adjVet : graph.adjList.get(vet)) {\n        if (visited.contains(adjVet))\n            continue; // Skip vertices that have been visited\n        // Recursively visit adjacent vertices\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* Depth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n    // Vertex traversal sequence\n    List<Vertex> res = new ArrayList<>();\n    // Hash set for recording vertices that have been visited\n    Set<Vertex> visited = new HashSet<>();\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
      graph_dfs.cs
      /* Depth-first traversal helper function */\nvoid DFS(GraphAdjList graph, HashSet<Vertex> visited, List<Vertex> res, Vertex vet) {\n    res.Add(vet);     // Record visited vertex\n    visited.Add(vet); // Mark this vertex as visited\n    // Traverse all adjacent vertices of this vertex\n    foreach (Vertex adjVet in graph.adjList[vet]) {\n        if (visited.Contains(adjVet)) {\n            continue; // Skip vertices that have been visited\n        }\n        // Recursively visit adjacent vertices\n        DFS(graph, visited, res, adjVet);\n    }\n}\n\n/* Depth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nList<Vertex> GraphDFS(GraphAdjList graph, Vertex startVet) {\n    // Vertex traversal sequence\n    List<Vertex> res = [];\n    // Hash set for recording vertices that have been visited\n    HashSet<Vertex> visited = [];\n    DFS(graph, visited, res, startVet);\n    return res;\n}\n
      graph_dfs.go
      /* Depth-first traversal helper function */\nfunc dfs(g *graphAdjList, visited map[Vertex]struct{}, res *[]Vertex, vet Vertex) {\n    // append operation returns a new reference, must reassign original reference to new slice's reference\n    *res = append(*res, vet)\n    visited[vet] = struct{}{}\n    // Traverse all adjacent vertices of this vertex\n    for _, adjVet := range g.adjList[vet] {\n        _, isExist := visited[adjVet]\n        // Recursively visit adjacent vertices\n        if !isExist {\n            dfs(g, visited, res, adjVet)\n        }\n    }\n}\n\n/* Depth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfunc graphDFS(g *graphAdjList, startVet Vertex) []Vertex {\n    // Vertex traversal sequence\n    res := make([]Vertex, 0)\n    // Hash set for recording vertices that have been visited\n    visited := make(map[Vertex]struct{})\n    dfs(g, visited, &res, startVet)\n    // Return vertex traversal sequence\n    return res\n}\n
      graph_dfs.swift
      /* Depth-first traversal helper function */\nfunc dfs(graph: GraphAdjList, visited: inout Set<Vertex>, res: inout [Vertex], vet: Vertex) {\n    res.append(vet) // Record visited vertex\n    visited.insert(vet) // Mark this vertex as visited\n    // Traverse all adjacent vertices of this vertex\n    for adjVet in graph.adjList[vet] ?? [] {\n        if visited.contains(adjVet) {\n            continue // Skip vertices that have been visited\n        }\n        // Recursively visit adjacent vertices\n        dfs(graph: graph, visited: &visited, res: &res, vet: adjVet)\n    }\n}\n\n/* Depth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfunc graphDFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {\n    // Vertex traversal sequence\n    var res: [Vertex] = []\n    // Hash set for recording vertices that have been visited\n    var visited: Set<Vertex> = []\n    dfs(graph: graph, visited: &visited, res: &res, vet: startVet)\n    return res\n}\n
      graph_dfs.js
      /* Depth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfunction dfs(graph, visited, res, vet) {\n    res.push(vet); // Record visited vertex\n    visited.add(vet); // Mark this vertex as visited\n    // Traverse all adjacent vertices of this vertex\n    for (const adjVet of graph.adjList.get(vet)) {\n        if (visited.has(adjVet)) {\n            continue; // Skip vertices that have been visited\n        }\n        // Recursively visit adjacent vertices\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* Depth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfunction graphDFS(graph, startVet) {\n    // Vertex traversal sequence\n    const res = [];\n    // Hash set for recording vertices that have been visited\n    const visited = new Set();\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
      graph_dfs.ts
      /* Depth-first traversal helper function */\nfunction dfs(\n    graph: GraphAdjList,\n    visited: Set<Vertex>,\n    res: Vertex[],\n    vet: Vertex\n): void {\n    res.push(vet); // Record visited vertex\n    visited.add(vet); // Mark this vertex as visited\n    // Traverse all adjacent vertices of this vertex\n    for (const adjVet of graph.adjList.get(vet)) {\n        if (visited.has(adjVet)) {\n            continue; // Skip vertices that have been visited\n        }\n        // Recursively visit adjacent vertices\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* Depth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfunction graphDFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n    // Vertex traversal sequence\n    const res: Vertex[] = [];\n    // Hash set for recording vertices that have been visited\n    const visited: Set<Vertex> = new Set();\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
      graph_dfs.dart
      /* Depth-first traversal helper function */\nvoid dfs(\n  GraphAdjList graph,\n  Set<Vertex> visited,\n  List<Vertex> res,\n  Vertex vet,\n) {\n  res.add(vet); // Record visited vertex\n  visited.add(vet); // Mark this vertex as visited\n  // Traverse all adjacent vertices of this vertex\n  for (Vertex adjVet in graph.adjList[vet]!) {\n    if (visited.contains(adjVet)) {\n      continue; // Skip vertices that have been visited\n    }\n    // Recursively visit adjacent vertices\n    dfs(graph, visited, res, adjVet);\n  }\n}\n\n/* Depth-first traversal */\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n  // Vertex traversal sequence\n  List<Vertex> res = [];\n  // Hash set for recording vertices that have been visited\n  Set<Vertex> visited = {};\n  dfs(graph, visited, res, startVet);\n  return res;\n}\n
      graph_dfs.rs
      /* Depth-first traversal helper function */\nfn dfs(graph: &GraphAdjList, visited: &mut HashSet<Vertex>, res: &mut Vec<Vertex>, vet: Vertex) {\n    res.push(vet); // Record visited vertex\n    visited.insert(vet); // Mark this vertex as visited\n                         // Traverse all adjacent vertices of this vertex\n    if let Some(adj_vets) = graph.adj_list.get(&vet) {\n        for &adj_vet in adj_vets {\n            if visited.contains(&adj_vet) {\n                continue; // Skip vertices that have been visited\n            }\n            // Recursively visit adjacent vertices\n            dfs(graph, visited, res, adj_vet);\n        }\n    }\n}\n\n/* Depth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfn graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {\n    // Vertex traversal sequence\n    let mut res = vec![];\n    // Hash set for recording vertices that have been visited\n    let mut visited = HashSet::new();\n    dfs(&graph, &mut visited, &mut res, start_vet);\n\n    res\n}\n
      graph_dfs.c
      /* Check if vertex has been visited */\nint isVisited(Vertex **res, int size, Vertex *vet) {\n    // Traverse to find node using O(n) time\n    for (int i = 0; i < size; i++) {\n        if (res[i] == vet) {\n            return 1;\n        }\n    }\n    return 0;\n}\n\n/* Depth-first traversal helper function */\nvoid dfs(GraphAdjList *graph, Vertex **res, int *resSize, Vertex *vet) {\n    // Record visited vertex\n    res[(*resSize)++] = vet;\n    // Traverse all adjacent vertices of this vertex\n    AdjListNode *node = findNode(graph, vet);\n    while (node != NULL) {\n        // Skip vertices that have been visited\n        if (!isVisited(res, *resSize, node->vertex)) {\n            // Recursively visit adjacent vertices\n            dfs(graph, res, resSize, node->vertex);\n        }\n        node = node->next;\n    }\n}\n\n/* Depth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nvoid graphDFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize) {\n    dfs(graph, res, resSize, startVet);\n}\n
      graph_dfs.kt
      /* Depth-first traversal helper function */\nfun dfs(\n    graph: GraphAdjList,\n    visited: MutableSet<Vertex?>,\n    res: MutableList<Vertex?>,\n    vet: Vertex?\n) {\n    res.add(vet)     // Record visited vertex\n    visited.add(vet) // Mark this vertex as visited\n    // Traverse all adjacent vertices of this vertex\n    for (adjVet in graph.adjList[vet]!!) {\n        if (visited.contains(adjVet))\n            continue  // Skip vertices that have been visited\n        // Recursively visit adjacent vertices\n        dfs(graph, visited, res, adjVet)\n    }\n}\n\n/* Depth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfun graphDFS(graph: GraphAdjList, startVet: Vertex?): MutableList<Vertex?> {\n    // Vertex traversal sequence\n    val res = mutableListOf<Vertex?>()\n    // Hash set for recording vertices that have been visited\n    val visited = HashSet<Vertex?>()\n    dfs(graph, visited, res, startVet)\n    return res\n}\n
      graph_dfs.rb
      ### Depth-first traversal helper function ###\ndef dfs(graph, visited, res, vet)\n  res << vet # Record visited vertex\n  visited.add(vet) # Mark this vertex as visited\n  # Traverse all adjacent vertices of this vertex\n  for adj_vet in graph.adj_list[vet]\n    next if visited.include?(adj_vet) # Skip vertices that have been visited\n    # Recursively visit adjacent vertices\n    dfs(graph, visited, res, adj_vet)\n  end\nend\n\n### Depth-first traversal ###\ndef graph_dfs(graph, start_vet)\n  # Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\n  # Vertex traversal sequence\n  res = []\n  # Hash set for recording vertices that have been visited\n  visited = Set.new\n  dfs(graph, visited, res, start_vet)\n  res\nend\n

      The algorithm flow of depth-first search is shown in Figure 9-12.

      • Straight dashed lines represent downward recursion, indicating that a new recursive method has been initiated to visit a new vertex.
      • Curved dashed lines represent upward backtracking, indicating that this recursive method has returned to the position where it was initiated.

      To deepen understanding, it is recommended to combine Figure 9-12 with the code to mentally simulate (or draw out) the entire DFS process, including when each recursive method is initiated and when it returns.

      <1><2><3><4><5><6><7><8><9><10><11>

      Figure 9-12 \u00a0 Steps of depth-first search of a graph

      Is the depth-first traversal sequence unique?

      Similar to breadth-first search, the order of depth-first traversal sequences is also not unique. Given a certain vertex, exploring in any direction first is valid, meaning the order of adjacent vertices can be arbitrarily shuffled, all being depth-first search.

      Taking tree traversal as an example, \"root \\(\\rightarrow\\) left \\(\\rightarrow\\) right\", \"left \\(\\rightarrow\\) root \\(\\rightarrow\\) right\", and \"left \\(\\rightarrow\\) right \\(\\rightarrow\\) root\" correspond to pre-order, in-order, and post-order traversals, respectively. They represent three different traversal priorities, yet all three belong to depth-first search.

      "},{"location":"chapter_graph/graph_traversal/#2-complexity-analysis_1","title":"2. \u00a0 Complexity Analysis","text":"

      Time complexity: All vertices will be visited \\(1\\) time, using \\(O(|V|)\\) time; all edges will be visited \\(2\\) times, using \\(O(2|E|)\\) time; overall using \\(O(|V| + |E|)\\) time.

      Space complexity: The list res and hash set visited can contain at most \\(|V|\\) vertices, and the maximum recursion depth is \\(|V|\\), therefore using \\(O(|V|)\\) space.

      "},{"location":"chapter_graph/summary/","title":"9.4 \u00a0 Summary","text":""},{"location":"chapter_graph/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • Graphs consist of vertices and edges and can be represented as a set of vertices and a set of edges.
      • Compared to linear relationships (linked lists) and divide-and-conquer relationships (trees), network relationships (graphs) have a higher degree of freedom and are therefore more complex.
      • Directed graphs have edges with directionality, connected graphs have all vertices reachable from any vertex, and weighted graphs have edges that each contain a weight variable.
      • Adjacency matrices use matrices to represent graphs, where each row (column) represents a vertex, and matrix elements represent edges, using \\(1\\) or \\(0\\) to indicate whether two vertices have an edge or not. Adjacency matrices are highly efficient for addition, deletion, lookup, and modification operations, but consume significant space.
      • Adjacency lists use multiple linked lists to represent graphs, where the \\(i\\)-th linked list corresponds to vertex \\(i\\) and stores all adjacent vertices of that vertex. Adjacency lists are more space-efficient than adjacency matrices, but have lower time efficiency because they require traversing linked lists to find edges.
      • When linked lists in adjacency lists become too long, they can be converted to red-black trees or hash tables, thereby improving lookup efficiency.
      • From an algorithmic perspective, adjacency matrices embody \"trading space for time\", while adjacency lists embody \"trading time for space\".
      • Graphs can be used to model various real-world systems, such as social networks and subway lines.
      • Trees are a special case of graphs, and tree traversal is a special case of graph traversal.
      • Breadth-first search of graphs is a near-to-far, layer-by-layer expansion search method, typically implemented using a queue.
      • Depth-first search of graphs is a search method that prioritizes going as far as possible and backtracks when no path remains, commonly implemented using recursion.
      "},{"location":"chapter_graph/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

      Q: Is a path defined as a sequence of vertices or a sequence of edges?

      The definitions in different language versions of Wikipedia are inconsistent: the English version states \"a path is a sequence of edges\", while the Chinese version states \"a path is a sequence of vertices\". The following is the original English text: In graph theory, a path in a graph is a finite or infinite sequence of edges which joins a sequence of vertices.

      In this text, a path is viewed as a sequence of edges, not a sequence of vertices. This is because there may be multiple edges connecting two vertices, in which case each edge corresponds to a path.

      Q: In a disconnected graph, will there be unreachable vertices?

      In a disconnected graph, starting from a certain vertex, at least one vertex cannot be reached. Traversing a disconnected graph requires setting multiple starting points to traverse all connected components of the graph.

      Q: In an adjacency list, is there a requirement for the order of \"all vertices connected to that vertex\"?

      It can be in any order. However, in practical applications, it may be necessary to sort according to specified rules, such as the order in which vertices were added, or the order of vertex values, which helps quickly find vertices \"with certain extreme values\".

      "},{"location":"chapter_greedy/","title":"Chapter 15. \u00a0 Greedy","text":"

      Abstract

      Sunflowers turn toward the sun, constantly pursuing the maximum potential for their own growth.

      Through rounds of simple choices, greedy strategies gradually lead to the best answer.

      "},{"location":"chapter_greedy/#chapter-contents","title":"Chapter contents","text":"
      • 15.1 \u00a0 Greedy Algorithm
      • 15.2 \u00a0 Fractional Knapsack Problem
      • 15.3 \u00a0 Maximum Capacity Problem
      • 15.4 \u00a0 Maximum Product Cutting Problem
      • 15.5 \u00a0 Summary
      "},{"location":"chapter_greedy/fractional_knapsack_problem/","title":"15.2 \u00a0 Fractional Knapsack Problem","text":"

      Question

      Given \\(n\\) items, where the weight of the \\(i\\)-th item is \\(wgt[i-1]\\) and its value is \\(val[i-1]\\), and a knapsack with capacity \\(cap\\). Each item can be selected only once, but a portion of an item can be selected, with the value calculated based on the proportion of weight selected, what is the maximum value of items in the knapsack under the limited capacity? An example is shown in Figure 15-3.

      Figure 15-3 \u00a0 Example data for the fractional knapsack problem

      The fractional knapsack problem is very similar overall to the 0-1 knapsack problem, with states including the current item \\(i\\) and capacity \\(c\\), and the goal being to maximize value under the limited knapsack capacity.

      The difference is that this problem allows selecting only a portion of an item. As shown in Figure 15-4, we can arbitrarily split items and calculate the corresponding value based on the weight proportion.

      1. For item \\(i\\), its value per unit weight is \\(val[i-1] / wgt[i-1]\\), referred to as unit value.
      2. Suppose we put a portion of item \\(i\\) with weight \\(w\\) into the knapsack, then the value added to the knapsack is \\(w \\times val[i-1] / wgt[i-1]\\).

      Figure 15-4 \u00a0 Value of items per unit weight

      "},{"location":"chapter_greedy/fractional_knapsack_problem/#1-greedy-strategy-determination","title":"1. \u00a0 Greedy Strategy Determination","text":"

      Maximizing the total value of items in the knapsack is essentially maximizing the value per unit weight of items. From this, we can derive the greedy strategy shown in Figure 15-5.

      1. Sort items by unit value from high to low.
      2. Iterate through all items, greedily selecting the item with the highest unit value in each round.
      3. If the remaining knapsack capacity is insufficient, use a portion of the current item to fill the knapsack.

      Figure 15-5 \u00a0 Greedy strategy for the fractional knapsack problem

      "},{"location":"chapter_greedy/fractional_knapsack_problem/#2-code-implementation","title":"2. \u00a0 Code Implementation","text":"

      We created an Item class to facilitate sorting items by unit value. We loop to make greedy selections, breaking when the knapsack is full and returning the solution:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby fractional_knapsack.py
      class Item:\n    \"\"\"Item\"\"\"\n\n    def __init__(self, w: int, v: int):\n        self.w = w  # Item weight\n        self.v = v  # Item value\n\ndef fractional_knapsack(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"Fractional knapsack: Greedy algorithm\"\"\"\n    # Create item list with two attributes: weight, value\n    items = [Item(w, v) for w, v in zip(wgt, val)]\n    # Sort by unit value item.v / item.w from high to low\n    items.sort(key=lambda item: item.v / item.w, reverse=True)\n    # Loop for greedy selection\n    res = 0\n    for item in items:\n        if item.w <= cap:\n            # If remaining capacity is sufficient, put the entire current item into the knapsack\n            res += item.v\n            cap -= item.w\n        else:\n            # If remaining capacity is insufficient, put part of the current item into the knapsack\n            res += (item.v / item.w) * cap\n            # No remaining capacity, so break out of the loop\n            break\n    return res\n
      fractional_knapsack.cpp
      /* Item */\nclass Item {\n  public:\n    int w; // Item weight\n    int v; // Item value\n\n    Item(int w, int v) : w(w), v(v) {\n    }\n};\n\n/* Fractional knapsack: Greedy algorithm */\ndouble fractionalKnapsack(vector<int> &wgt, vector<int> &val, int cap) {\n    // Create item list with two attributes: weight, value\n    vector<Item> items;\n    for (int i = 0; i < wgt.size(); i++) {\n        items.push_back(Item(wgt[i], val[i]));\n    }\n    // Sort by unit value item.v / item.w from high to low\n    sort(items.begin(), items.end(), [](Item &a, Item &b) { return (double)a.v / a.w > (double)b.v / b.w; });\n    // Loop for greedy selection\n    double res = 0;\n    for (auto &item : items) {\n        if (item.w <= cap) {\n            // If remaining capacity is sufficient, put the entire current item into the knapsack\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // If remaining capacity is insufficient, put part of the current item into the knapsack\n            res += (double)item.v / item.w * cap;\n            // No remaining capacity, so break out of the loop\n            break;\n        }\n    }\n    return res;\n}\n
      fractional_knapsack.java
      /* Item */\nclass Item {\n    int w; // Item weight\n    int v; // Item value\n\n    public Item(int w, int v) {\n        this.w = w;\n        this.v = v;\n    }\n}\n\n/* Fractional knapsack: Greedy algorithm */\ndouble fractionalKnapsack(int[] wgt, int[] val, int cap) {\n    // Create item list with two attributes: weight, value\n    Item[] items = new Item[wgt.length];\n    for (int i = 0; i < wgt.length; i++) {\n        items[i] = new Item(wgt[i], val[i]);\n    }\n    // Sort by unit value item.v / item.w from high to low\n    Arrays.sort(items, Comparator.comparingDouble(item -> -((double) item.v / item.w)));\n    // Loop for greedy selection\n    double res = 0;\n    for (Item item : items) {\n        if (item.w <= cap) {\n            // If remaining capacity is sufficient, put the entire current item into the knapsack\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // If remaining capacity is insufficient, put part of the current item into the knapsack\n            res += (double) item.v / item.w * cap;\n            // No remaining capacity, so break out of the loop\n            break;\n        }\n    }\n    return res;\n}\n
      fractional_knapsack.cs
      /* Item */\nclass Item(int w, int v) {\n    public int w = w; // Item weight\n    public int v = v; // Item value\n}\n\n/* Fractional knapsack: Greedy algorithm */\ndouble FractionalKnapsack(int[] wgt, int[] val, int cap) {\n    // Create item list with two attributes: weight, value\n    Item[] items = new Item[wgt.Length];\n    for (int i = 0; i < wgt.Length; i++) {\n        items[i] = new Item(wgt[i], val[i]);\n    }\n    // Sort by unit value item.v / item.w from high to low\n    Array.Sort(items, (x, y) => (y.v / y.w).CompareTo(x.v / x.w));\n    // Loop for greedy selection\n    double res = 0;\n    foreach (Item item in items) {\n        if (item.w <= cap) {\n            // If remaining capacity is sufficient, put the entire current item into the knapsack\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // If remaining capacity is insufficient, put part of the current item into the knapsack\n            res += (double)item.v / item.w * cap;\n            // No remaining capacity, so break out of the loop\n            break;\n        }\n    }\n    return res;\n}\n
      fractional_knapsack.go
      /* Item */\ntype Item struct {\n    w int // Item weight\n    v int // Item value\n}\n\n/* Fractional knapsack: Greedy algorithm */\nfunc fractionalKnapsack(wgt []int, val []int, cap int) float64 {\n    // Create item list with two attributes: weight, value\n    items := make([]Item, len(wgt))\n    for i := 0; i < len(wgt); i++ {\n        items[i] = Item{wgt[i], val[i]}\n    }\n    // Sort by unit value item.v / item.w from high to low\n    sort.Slice(items, func(i, j int) bool {\n        return float64(items[i].v)/float64(items[i].w) > float64(items[j].v)/float64(items[j].w)\n    })\n    // Loop for greedy selection\n    res := 0.0\n    for _, item := range items {\n        if item.w <= cap {\n            // If remaining capacity is sufficient, put the entire current item into the knapsack\n            res += float64(item.v)\n            cap -= item.w\n        } else {\n            // If remaining capacity is insufficient, put part of the current item into the knapsack\n            res += float64(item.v) / float64(item.w) * float64(cap)\n            // No remaining capacity, so break out of the loop\n            break\n        }\n    }\n    return res\n}\n
      fractional_knapsack.swift
      /* Item */\nclass Item {\n    var w: Int // Item weight\n    var v: Int // Item value\n\n    init(w: Int, v: Int) {\n        self.w = w\n        self.v = v\n    }\n}\n\n/* Fractional knapsack: Greedy algorithm */\nfunc fractionalKnapsack(wgt: [Int], val: [Int], cap: Int) -> Double {\n    // Create item list with two attributes: weight, value\n    var items = zip(wgt, val).map { Item(w: $0, v: $1) }\n    // Sort by unit value item.v / item.w from high to low\n    items.sort { -(Double($0.v) / Double($0.w)) < -(Double($1.v) / Double($1.w)) }\n    // Loop for greedy selection\n    var res = 0.0\n    var cap = cap\n    for item in items {\n        if item.w <= cap {\n            // If remaining capacity is sufficient, put the entire current item into the knapsack\n            res += Double(item.v)\n            cap -= item.w\n        } else {\n            // If remaining capacity is insufficient, put part of the current item into the knapsack\n            res += Double(item.v) / Double(item.w) * Double(cap)\n            // No remaining capacity, so break out of the loop\n            break\n        }\n    }\n    return res\n}\n
      fractional_knapsack.js
      /* Item */\nclass Item {\n    constructor(w, v) {\n        this.w = w; // Item weight\n        this.v = v; // Item value\n    }\n}\n\n/* Fractional knapsack: Greedy algorithm */\nfunction fractionalKnapsack(wgt, val, cap) {\n    // Create item list with two attributes: weight, value\n    const items = wgt.map((w, i) => new Item(w, val[i]));\n    // Sort by unit value item.v / item.w from high to low\n    items.sort((a, b) => b.v / b.w - a.v / a.w);\n    // Loop for greedy selection\n    let res = 0;\n    for (const item of items) {\n        if (item.w <= cap) {\n            // If remaining capacity is sufficient, put the entire current item into the knapsack\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // If remaining capacity is insufficient, put part of the current item into the knapsack\n            res += (item.v / item.w) * cap;\n            // No remaining capacity, so break out of the loop\n            break;\n        }\n    }\n    return res;\n}\n
      fractional_knapsack.ts
      /* Item */\nclass Item {\n    w: number; // Item weight\n    v: number; // Item value\n\n    constructor(w: number, v: number) {\n        this.w = w;\n        this.v = v;\n    }\n}\n\n/* Fractional knapsack: Greedy algorithm */\nfunction fractionalKnapsack(wgt: number[], val: number[], cap: number): number {\n    // Create item list with two attributes: weight, value\n    const items: Item[] = wgt.map((w, i) => new Item(w, val[i]));\n    // Sort by unit value item.v / item.w from high to low\n    items.sort((a, b) => b.v / b.w - a.v / a.w);\n    // Loop for greedy selection\n    let res = 0;\n    for (const item of items) {\n        if (item.w <= cap) {\n            // If remaining capacity is sufficient, put the entire current item into the knapsack\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // If remaining capacity is insufficient, put part of the current item into the knapsack\n            res += (item.v / item.w) * cap;\n            // No remaining capacity, so break out of the loop\n            break;\n        }\n    }\n    return res;\n}\n
      fractional_knapsack.dart
      /* Item */\nclass Item {\n  int w; // Item weight\n  int v; // Item value\n\n  Item(this.w, this.v);\n}\n\n/* Fractional knapsack: Greedy algorithm */\ndouble fractionalKnapsack(List<int> wgt, List<int> val, int cap) {\n  // Create item list with two attributes: weight, value\n  List<Item> items = List.generate(wgt.length, (i) => Item(wgt[i], val[i]));\n  // Sort by unit value item.v / item.w from high to low\n  items.sort((a, b) => (b.v / b.w).compareTo(a.v / a.w));\n  // Loop for greedy selection\n  double res = 0;\n  for (Item item in items) {\n    if (item.w <= cap) {\n      // If remaining capacity is sufficient, put the entire current item into the knapsack\n      res += item.v;\n      cap -= item.w;\n    } else {\n      // If remaining capacity is insufficient, put part of the current item into the knapsack\n      res += item.v / item.w * cap;\n      // No remaining capacity, so break out of the loop\n      break;\n    }\n  }\n  return res;\n}\n
      fractional_knapsack.rs
      /* Item */\nstruct Item {\n    w: i32, // Item weight\n    v: i32, // Item value\n}\n\nimpl Item {\n    fn new(w: i32, v: i32) -> Self {\n        Self { w, v }\n    }\n}\n\n/* Fractional knapsack: Greedy algorithm */\nfn fractional_knapsack(wgt: &[i32], val: &[i32], mut cap: i32) -> f64 {\n    // Create item list with two attributes: weight, value\n    let mut items = wgt\n        .iter()\n        .zip(val.iter())\n        .map(|(&w, &v)| Item::new(w, v))\n        .collect::<Vec<Item>>();\n    // Sort by unit value item.v / item.w from high to low\n    items.sort_by(|a, b| {\n        (b.v as f64 / b.w as f64)\n            .partial_cmp(&(a.v as f64 / a.w as f64))\n            .unwrap()\n    });\n    // Loop for greedy selection\n    let mut res = 0.0;\n    for item in &items {\n        if item.w <= cap {\n            // If remaining capacity is sufficient, put the entire current item into the knapsack\n            res += item.v as f64;\n            cap -= item.w;\n        } else {\n            // If remaining capacity is insufficient, put part of the current item into the knapsack\n            res += item.v as f64 / item.w as f64 * cap as f64;\n            // No remaining capacity, so break out of the loop\n            break;\n        }\n    }\n    res\n}\n
      fractional_knapsack.c
      /* Item */\ntypedef struct {\n    int w; // Item weight\n    int v; // Item value\n} Item;\n\n/* Fractional knapsack: Greedy algorithm */\nfloat fractionalKnapsack(int wgt[], int val[], int itemCount, int cap) {\n    // Create item list with two attributes: weight, value\n    Item *items = malloc(sizeof(Item) * itemCount);\n    for (int i = 0; i < itemCount; i++) {\n        items[i] = (Item){.w = wgt[i], .v = val[i]};\n    }\n    // Sort by unit value item.v / item.w from high to low\n    qsort(items, (size_t)itemCount, sizeof(Item), sortByValueDensity);\n    // Loop for greedy selection\n    float res = 0.0;\n    for (int i = 0; i < itemCount; i++) {\n        if (items[i].w <= cap) {\n            // If remaining capacity is sufficient, put the entire current item into the knapsack\n            res += items[i].v;\n            cap -= items[i].w;\n        } else {\n            // If remaining capacity is insufficient, put part of the current item into the knapsack\n            res += (float)cap / items[i].w * items[i].v;\n            cap = 0;\n            break;\n        }\n    }\n    free(items);\n    return res;\n}\n
      fractional_knapsack.kt
      /* Item */\nclass Item(\n    val w: Int, // Item\n    val v: Int  // Item value\n)\n\n/* Fractional knapsack: Greedy algorithm */\nfun fractionalKnapsack(wgt: IntArray, _val: IntArray, c: Int): Double {\n    // Create item list with two attributes: weight, value\n    var cap = c\n    val items = arrayOfNulls<Item>(wgt.size)\n    for (i in wgt.indices) {\n        items[i] = Item(wgt[i], _val[i])\n    }\n    // Sort by unit value item.v / item.w from high to low\n    items.sortBy { item: Item? -> -(item!!.v.toDouble() / item.w) }\n    // Loop for greedy selection\n    var res = 0.0\n    for (item in items) {\n        if (item!!.w <= cap) {\n            // If remaining capacity is sufficient, put the entire current item into the knapsack\n            res += item.v\n            cap -= item.w\n        } else {\n            // If remaining capacity is insufficient, put part of the current item into the knapsack\n            res += item.v.toDouble() / item.w * cap\n            // No remaining capacity, so break out of the loop\n            break\n        }\n    }\n    return res\n}\n
      fractional_knapsack.rb
      ### Item ###\nclass Item\n  attr_accessor :w # Item weight\n  attr_accessor :v # Item value\n\n  def initialize(w, v)\n    @w = w\n    @v = v\n  end\nend\n\n### Fractional knapsack: greedy ###\ndef fractional_knapsack(wgt, val, cap)\n  # Create item list with two attributes: weight, value\n  items = wgt.each_with_index.map { |w, i| Item.new(w, val[i]) }\n  # Sort by unit value item.v / item.w from high to low\n  items.sort! { |a, b| (b.v.to_f / b.w) <=> (a.v.to_f / a.w) }\n  # Loop for greedy selection\n  res = 0\n  for item in items\n    if item.w <= cap\n      # If remaining capacity is sufficient, put the entire current item into the knapsack\n      res += item.v\n      cap -= item.w\n    else\n      # If remaining capacity is insufficient, put part of the current item into the knapsack\n      res += (item.v.to_f / item.w) * cap\n      # No remaining capacity, so break out of the loop\n      break\n    end\n  end\n  res\nend\n

      The time complexity of built-in sorting algorithms is usually \\(O(\\log n)\\), and the space complexity is usually \\(O(\\log n)\\) or \\(O(n)\\), depending on the specific implementation of the programming language.

      Apart from sorting, in the worst case the entire item list needs to be traversed, therefore the time complexity is \\(O(n)\\), where \\(n\\) is the number of items.

      Since an Item object list is initialized, the space complexity is \\(O(n)\\).

      "},{"location":"chapter_greedy/fractional_knapsack_problem/#3-correctness-proof","title":"3. \u00a0 Correctness Proof","text":"

      Using proof by contradiction. Suppose item \\(x\\) has the highest unit value, and some algorithm yields a maximum value of res, but this solution does not include item \\(x\\).

      Now remove a unit weight of any item from the knapsack and replace it with a unit weight of item \\(x\\). Since item \\(x\\) has the highest unit value, the total value after replacement will definitely be greater than res. This contradicts the assumption that res is the optimal solution, proving that the optimal solution must include item \\(x\\).

      For other items in this solution, we can also construct the above contradiction. In summary, items with greater unit value are always better choices, which proves that the greedy strategy is effective.

      As shown in Figure 15-6, if we view item weight and item unit value as the horizontal and vertical axes of a two-dimensional chart respectively, then the fractional knapsack problem can be transformed into \"finding the maximum area enclosed within a limited horizontal axis range\". This analogy can help us understand the effectiveness of the greedy strategy from a geometric perspective.

      Figure 15-6 \u00a0 Geometric representation of the fractional knapsack problem

      "},{"location":"chapter_greedy/greedy_algorithm/","title":"15.1 \u00a0 Greedy Algorithm","text":"

      Greedy algorithm is a common algorithm for solving optimization problems. Its basic idea is to make the seemingly best choice at each decision stage of the problem, that is, to greedily make locally optimal decisions in hopes of obtaining a globally optimal solution. Greedy algorithms are simple and efficient, and are widely applied in many practical problems.

      Greedy algorithms and dynamic programming are both commonly used to solve optimization problems. They share some similarities, such as both relying on the optimal substructure property, but they work differently.

      • Dynamic programming considers all previous decisions when making the current decision, and uses solutions to past subproblems to construct the solution to the current subproblem.
      • Greedy algorithms do not consider past decisions, but instead make greedy choices moving forward, continually reducing the problem size until the problem is solved.

      We will first understand how greedy algorithms work through the example problem \"coin change\". This problem has already been introduced in the \"Complete Knapsack Problem\" chapter, so I believe you are not unfamiliar with it.

      Question

      Given \\(n\\) types of coins, where the denomination of the \\(i\\)-th type of coin is \\(coins[i - 1]\\), and the target amount is \\(amt\\), with each type of coin available for repeated selection, what is the minimum number of coins needed to make up the target amount? If it is impossible to make up the target amount, return \\(-1\\).

      The greedy strategy adopted for this problem is shown in Figure 15-1. Given a target amount, we greedily select the coin that is not greater than and closest to it, and continuously repeat this step until the target amount is reached.

      Figure 15-1 \u00a0 Greedy strategy for coin change

      The implementation code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change_greedy.py
      def coin_change_greedy(coins: list[int], amt: int) -> int:\n    \"\"\"Coin change: Greedy algorithm\"\"\"\n    # Assume coins list is sorted\n    i = len(coins) - 1\n    count = 0\n    # Loop to make greedy choices until no remaining amount\n    while amt > 0:\n        # Find the coin that is less than and closest to the remaining amount\n        while i > 0 and coins[i] > amt:\n            i -= 1\n        # Choose coins[i]\n        amt -= coins[i]\n        count += 1\n    # If no feasible solution is found, return -1\n    return count if amt == 0 else -1\n
      coin_change_greedy.cpp
      /* Coin change: Greedy algorithm */\nint coinChangeGreedy(vector<int> &coins, int amt) {\n    // Assume coins list is sorted\n    int i = coins.size() - 1;\n    int count = 0;\n    // Loop to make greedy choices until no remaining amount\n    while (amt > 0) {\n        // Find the coin that is less than and closest to the remaining amount\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // Choose coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // If no feasible solution is found, return -1\n    return amt == 0 ? count : -1;\n}\n
      coin_change_greedy.java
      /* Coin change: Greedy algorithm */\nint coinChangeGreedy(int[] coins, int amt) {\n    // Assume coins list is sorted\n    int i = coins.length - 1;\n    int count = 0;\n    // Loop to make greedy choices until no remaining amount\n    while (amt > 0) {\n        // Find the coin that is less than and closest to the remaining amount\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // Choose coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // If no feasible solution is found, return -1\n    return amt == 0 ? count : -1;\n}\n
      coin_change_greedy.cs
      /* Coin change: Greedy algorithm */\nint CoinChangeGreedy(int[] coins, int amt) {\n    // Assume coins list is sorted\n    int i = coins.Length - 1;\n    int count = 0;\n    // Loop to make greedy choices until no remaining amount\n    while (amt > 0) {\n        // Find the coin that is less than and closest to the remaining amount\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // Choose coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // If no feasible solution is found, return -1\n    return amt == 0 ? count : -1;\n}\n
      coin_change_greedy.go
      /* Coin change: Greedy algorithm */\nfunc coinChangeGreedy(coins []int, amt int) int {\n    // Assume coins list is sorted\n    i := len(coins) - 1\n    count := 0\n    // Loop to make greedy choices until no remaining amount\n    for amt > 0 {\n        // Find the coin that is less than and closest to the remaining amount\n        for i > 0 && coins[i] > amt {\n            i--\n        }\n        // Choose coins[i]\n        amt -= coins[i]\n        count++\n    }\n    // If no feasible solution is found, return -1\n    if amt != 0 {\n        return -1\n    }\n    return count\n}\n
      coin_change_greedy.swift
      /* Coin change: Greedy algorithm */\nfunc coinChangeGreedy(coins: [Int], amt: Int) -> Int {\n    // Assume coins list is sorted\n    var i = coins.count - 1\n    var count = 0\n    var amt = amt\n    // Loop to make greedy choices until no remaining amount\n    while amt > 0 {\n        // Find the coin that is less than and closest to the remaining amount\n        while i > 0 && coins[i] > amt {\n            i -= 1\n        }\n        // Choose coins[i]\n        amt -= coins[i]\n        count += 1\n    }\n    // If no feasible solution is found, return -1\n    return amt == 0 ? count : -1\n}\n
      coin_change_greedy.js
      /* Coin change: Greedy algorithm */\nfunction coinChangeGreedy(coins, amt) {\n    // Assume coins array is sorted\n    let i = coins.length - 1;\n    let count = 0;\n    // Loop to make greedy choices until no remaining amount\n    while (amt > 0) {\n        // Find the coin that is less than and closest to the remaining amount\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // Choose coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // If no feasible solution is found, return -1\n    return amt === 0 ? count : -1;\n}\n
      coin_change_greedy.ts
      /* Coin change: Greedy algorithm */\nfunction coinChangeGreedy(coins: number[], amt: number): number {\n    // Assume coins array is sorted\n    let i = coins.length - 1;\n    let count = 0;\n    // Loop to make greedy choices until no remaining amount\n    while (amt > 0) {\n        // Find the coin that is less than and closest to the remaining amount\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // Choose coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // If no feasible solution is found, return -1\n    return amt === 0 ? count : -1;\n}\n
      coin_change_greedy.dart
      /* Coin change: Greedy algorithm */\nint coinChangeGreedy(List<int> coins, int amt) {\n  // Assume coins list is sorted\n  int i = coins.length - 1;\n  int count = 0;\n  // Loop to make greedy choices until no remaining amount\n  while (amt > 0) {\n    // Find the coin that is less than and closest to the remaining amount\n    while (i > 0 && coins[i] > amt) {\n      i--;\n    }\n    // Choose coins[i]\n    amt -= coins[i];\n    count++;\n  }\n  // If no feasible solution is found, return -1\n  return amt == 0 ? count : -1;\n}\n
      coin_change_greedy.rs
      /* Coin change: Greedy algorithm */\nfn coin_change_greedy(coins: &[i32], mut amt: i32) -> i32 {\n    // Assume coins list is sorted\n    let mut i = coins.len() - 1;\n    let mut count = 0;\n    // Loop to make greedy choices until no remaining amount\n    while amt > 0 {\n        // Find the coin that is less than and closest to the remaining amount\n        while i > 0 && coins[i] > amt {\n            i -= 1;\n        }\n        // Choose coins[i]\n        amt -= coins[i];\n        count += 1;\n    }\n    // If no feasible solution is found, return -1\n    if amt == 0 {\n        count\n    } else {\n        -1\n    }\n}\n
      coin_change_greedy.c
      /* Coin change: Greedy algorithm */\nint coinChangeGreedy(int *coins, int size, int amt) {\n    // Assume coins list is sorted\n    int i = size - 1;\n    int count = 0;\n    // Loop to make greedy choices until no remaining amount\n    while (amt > 0) {\n        // Find the coin that is less than and closest to the remaining amount\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // Choose coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // If no feasible solution is found, return -1\n    return amt == 0 ? count : -1;\n}\n
      coin_change_greedy.kt
      /* Coin change: Greedy algorithm */\nfun coinChangeGreedy(coins: IntArray, amt: Int): Int {\n    // Assume coins list is sorted\n    var am = amt\n    var i = coins.size - 1\n    var count = 0\n    // Loop to make greedy choices until no remaining amount\n    while (am > 0) {\n        // Find the coin that is less than and closest to the remaining amount\n        while (i > 0 && coins[i] > am) {\n            i--\n        }\n        // Choose coins[i]\n        am -= coins[i]\n        count++\n    }\n    // If no feasible solution is found, return -1\n    return if (am == 0) count else -1\n}\n
      coin_change_greedy.rb
      ### Coin change: greedy ###\ndef coin_change_greedy(coins, amt)\n  # Assume coins list is sorted\n  i = coins.length - 1\n  count = 0\n  # Loop to make greedy choices until no remaining amount\n  while amt > 0\n    # Find the coin that is less than and closest to the remaining amount\n    while i > 0 && coins[i] > amt\n      i -= 1\n    end\n    # Choose coins[i]\n    amt -= coins[i]\n    count += 1\n  end\n  # Return -1 if no solution found\n  amt == 0 ? count : -1\nend\n

      You might exclaim: So clean! The greedy algorithm solves the coin change problem in about ten lines of code.

      "},{"location":"chapter_greedy/greedy_algorithm/#1511-advantages-and-limitations-of-greedy-algorithms","title":"15.1.1 \u00a0 Advantages and Limitations of Greedy Algorithms","text":"

      Greedy algorithms are not only straightforward and simple to implement, but are also usually very efficient. In the code above, if the smallest coin denomination is \\(\\min(coins)\\), the greedy choice loops at most \\(amt / \\min(coins)\\) times, giving a time complexity of \\(O(amt / \\min(coins))\\). This is an order of magnitude smaller than the time complexity of the dynamic programming solution \\(O(n \\times amt)\\).

      However, for certain coin denomination combinations, greedy algorithms cannot find the optimal solution. Figure 15-2 provides two examples.

      • Positive example \\(coins = [1, 5, 10, 20, 50, 100]\\): With this coin combination, given any \\(amt\\), the greedy algorithm can find the optimal solution.
      • Negative example \\(coins = [1, 20, 50]\\): Suppose \\(amt = 60\\), the greedy algorithm can only find the combination \\(50 + 1 \\times 10\\), totaling \\(11\\) coins, but dynamic programming can find the optimal solution \\(20 + 20 + 20\\), requiring only \\(3\\) coins.
      • Negative example \\(coins = [1, 49, 50]\\): Suppose \\(amt = 98\\), the greedy algorithm can only find the combination \\(50 + 1 \\times 48\\), totaling \\(49\\) coins, but dynamic programming can find the optimal solution \\(49 + 49\\), requiring only \\(2\\) coins.

      Figure 15-2 \u00a0 Examples where greedy algorithms cannot find the optimal solution

      In other words, for the coin change problem, greedy algorithms cannot guarantee finding the global optimal solution, and may even find very poor solutions. It is better suited for solving with dynamic programming.

      Generally, the applicability of greedy algorithms falls into the following two situations.

      1. Can guarantee finding the optimal solution: In this situation, greedy algorithms are often the best choice, because they tend to be more efficient than backtracking and dynamic programming.
      2. Can find an approximate optimal solution: Greedy algorithms are also applicable in this situation. For many complex problems, finding the global optimal solution is very difficult, and being able to find a suboptimal solution with high efficiency is also very good.
      "},{"location":"chapter_greedy/greedy_algorithm/#1512-characteristics-of-greedy-algorithms","title":"15.1.2 \u00a0 Characteristics of Greedy Algorithms","text":"

      So the question arises: what kind of problems are suitable for solving with greedy algorithms? Or in other words, under what conditions can greedy algorithms guarantee finding the optimal solution?

      Compared to dynamic programming, the conditions for using greedy algorithms are stricter, mainly focusing on two properties of the problem.

      • Greedy choice property: Only when locally optimal choices can always lead to a globally optimal solution can greedy algorithms guarantee obtaining the optimal solution.
      • Optimal substructure: The optimal solution to the original problem contains the optimal solutions to subproblems.

      Optimal substructure has already been introduced in the \"Dynamic Programming\" chapter, so we won't elaborate on it here. It's worth noting that the optimal substructure of some problems is not obvious, but they can still be solved using greedy algorithms.

      We mainly explore methods for determining the greedy choice property. Although its description seems relatively simple, in practice, for many problems, proving the greedy choice property is not easy.

      For example, in the coin change problem, although we can easily provide counterexamples to disprove the greedy choice property, proving it is quite difficult. If asked: what conditions must a coin combination satisfy to be solvable using a greedy algorithm? We often can only rely on intuition or examples to give an ambiguous answer, and find it difficult to provide a rigorous mathematical proof.

      Quote

      There is a paper that presents an algorithm with \\(O(n^3)\\) time complexity for determining whether a coin combination can use a greedy algorithm to find the optimal solution for any amount.

      Pearson, D. A polynomial-time algorithm for the change-making problem[J]. Operations Research Letters, 2005, 33(3): 231-234.

      "},{"location":"chapter_greedy/greedy_algorithm/#1513-steps-for-solving-problems-with-greedy-algorithms","title":"15.1.3 \u00a0 Steps for Solving Problems with Greedy Algorithms","text":"

      The problem-solving process for greedy problems can generally be divided into the following three steps.

      1. Problem analysis: Sort out and understand the problem characteristics, including state definition, optimization objectives, and constraints, etc. This step is also involved in backtracking and dynamic programming.
      2. Determine the greedy strategy: Determine how to make greedy choices at each step. This strategy should be able to reduce the problem size at each step, ultimately solving the entire problem.
      3. Correctness proof: It is usually necessary to prove that the problem has both greedy choice property and optimal substructure. This step may require mathematical proofs, such as mathematical induction or proof by contradiction.

      Determining the greedy strategy is the core step in solving the problem, but it may not be easy to implement, mainly for the following reasons.

      • Greedy strategies differ greatly between different problems. For many problems, the greedy strategy is relatively straightforward, and we can derive it through some general thinking and attempts. However, for some complex problems, the greedy strategy may be very elusive, which really tests one's problem-solving experience and algorithmic ability.
      • Some greedy strategies are highly misleading. When we confidently design a greedy strategy, write the solution code and submit it for testing, we may find that some test cases cannot pass. This is because the designed greedy strategy is only \"partially correct\", as exemplified by the coin change problem discussed above.

      To ensure correctness, we should rigorously mathematically prove the greedy strategy, usually using proof by contradiction or mathematical induction.

      However, correctness proofs may also not be easy. If we have no clue, we usually choose to debug the code based on test cases, step by step modifying and verifying the greedy strategy.

      "},{"location":"chapter_greedy/greedy_algorithm/#1514-typical-problems-solved-by-greedy-algorithms","title":"15.1.4 \u00a0 Typical Problems Solved by Greedy Algorithms","text":"

      Greedy algorithms are often applied to optimization problems that satisfy greedy choice property and optimal substructure. Below are some typical greedy algorithm problems.

      • Coin change problem: With certain coin combinations, greedy algorithms can always obtain the optimal solution.
      • Interval scheduling problem: Suppose you have some tasks, each taking place during a period of time, and your goal is to complete as many tasks as possible. If you always choose the task that ends earliest, then the greedy algorithm can obtain the optimal solution.
      • Fractional knapsack problem: Given a set of items and a carrying capacity, your goal is to select a set of items such that the total weight does not exceed the carrying capacity and the total value is maximized. If you always choose the item with the highest value-to-weight ratio (value / weight), then the greedy algorithm can obtain the optimal solution in some cases.
      • Stock trading problem: Given a set of historical stock prices, you can make multiple trades, but if you already hold stocks, you cannot buy again before selling, and the goal is to obtain the maximum profit.
      • Huffman coding: Huffman coding is a greedy algorithm used for lossless data compression. By constructing a Huffman tree and always merging the two nodes with the lowest frequency, the resulting Huffman tree has the minimum weighted path length (encoding length).
      • Dijkstra's algorithm: It is a greedy algorithm for solving the shortest path problem from a given source vertex to all other vertices.
      "},{"location":"chapter_greedy/max_capacity_problem/","title":"15.3 \u00a0 Max Capacity Problem","text":"

      Question

      Input an array \\(ht\\), where each element represents the height of a vertical partition. Any two partitions in the array, along with the space between them, can form a container.

      The capacity of the container equals the product of height and width (area), where the height is determined by the shorter partition, and the width is the difference in array indices between the two partitions.

      Please select two partitions in the array such that the capacity of the formed container is maximized, and return the maximum capacity. An example is shown in Figure 15-7.

      Figure 15-7 \u00a0 Example data for the max capacity problem

      The container is formed by any two partitions, therefore the state of this problem is the indices of two partitions, denoted as \\([i, j]\\).

      According to the problem description, capacity equals height multiplied by width, where height is determined by the shorter partition, and width is the difference in array indices between the two partitions. Let the capacity be \\(cap[i, j]\\), then the calculation formula is:

      \\[ cap[i, j] = \\min(ht[i], ht[j]) \\times (j - i) \\]

      Let the array length be \\(n\\), then the number of combinations of two partitions (total number of states) is \\(C_n^2 = \\frac{n(n - 1)}{2}\\). Most directly, we can exhaustively enumerate all states to find the maximum capacity, with time complexity \\(O(n^2)\\).

      "},{"location":"chapter_greedy/max_capacity_problem/#1-greedy-strategy-determination","title":"1. \u00a0 Greedy Strategy Determination","text":"

      This problem has a more efficient solution. As shown in Figure 15-8, select a state \\([i, j]\\) where index \\(i < j\\) and height \\(ht[i] < ht[j]\\), meaning \\(i\\) is the short partition and \\(j\\) is the long partition.

      Figure 15-8 \u00a0 Initial state

      As shown in Figure 15-9, if we now move the long partition \\(j\\) closer to the short partition \\(i\\), the capacity will definitely decrease.

      This is because after moving the long partition \\(j\\), the width \\(j-i\\) definitely decreases; and since height is determined by the short partition, the height can only remain unchanged (\\(i\\) is still the short partition) or decrease (the moved \\(j\\) becomes the short partition).

      Figure 15-9 \u00a0 State after moving the long partition inward

      Conversely, we can only possibly increase capacity by contracting the short partition \\(i\\) inward. Because although width will definitely decrease, height may increase (the moved short partition \\(i\\) may become taller). For example, in Figure 15-10, the area increases after moving the short partition.

      Figure 15-10 \u00a0 State after moving the short partition inward

      From this we can derive the greedy strategy for this problem: initialize two pointers at both ends of the container, and in each round contract the pointer corresponding to the short partition inward, until the two pointers meet.

      Figure 15-11 shows the execution process of the greedy strategy.

      1. In the initial state, pointers \\(i\\) and \\(j\\) are at both ends of the array.
      2. Calculate the capacity of the current state \\(cap[i, j]\\), and update the maximum capacity.
      3. Compare the heights of partition \\(i\\) and partition \\(j\\), and move the short partition inward by one position.
      4. Loop through steps 2. and 3. until \\(i\\) and \\(j\\) meet.
      <1><2><3><4><5><6><7><8><9>

      Figure 15-11 \u00a0 Greedy process for the max capacity problem

      "},{"location":"chapter_greedy/max_capacity_problem/#2-code-implementation","title":"2. \u00a0 Code Implementation","text":"

      The code loops at most \\(n\\) rounds, therefore the time complexity is \\(O(n)\\).

      Variables \\(i\\), \\(j\\), and \\(res\\) use a constant amount of extra space, therefore the space complexity is \\(O(1)\\).

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby max_capacity.py
      def max_capacity(ht: list[int]) -> int:\n    \"\"\"Max capacity: Greedy algorithm\"\"\"\n    # Initialize i, j to be at both ends of the array\n    i, j = 0, len(ht) - 1\n    # Initial max capacity is 0\n    res = 0\n    # Loop for greedy selection until the two boards meet\n    while i < j:\n        # Update max capacity\n        cap = min(ht[i], ht[j]) * (j - i)\n        res = max(res, cap)\n        # Move the shorter board inward\n        if ht[i] < ht[j]:\n            i += 1\n        else:\n            j -= 1\n    return res\n
      max_capacity.cpp
      /* Max capacity: Greedy algorithm */\nint maxCapacity(vector<int> &ht) {\n    // Initialize i, j to be at both ends of the array\n    int i = 0, j = ht.size() - 1;\n    // Initial max capacity is 0\n    int res = 0;\n    // Loop for greedy selection until the two boards meet\n    while (i < j) {\n        // Update max capacity\n        int cap = min(ht[i], ht[j]) * (j - i);\n        res = max(res, cap);\n        // Move the shorter board inward\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
      max_capacity.java
      /* Max capacity: Greedy algorithm */\nint maxCapacity(int[] ht) {\n    // Initialize i, j to be at both ends of the array\n    int i = 0, j = ht.length - 1;\n    // Initial max capacity is 0\n    int res = 0;\n    // Loop for greedy selection until the two boards meet\n    while (i < j) {\n        // Update max capacity\n        int cap = Math.min(ht[i], ht[j]) * (j - i);\n        res = Math.max(res, cap);\n        // Move the shorter board inward\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
      max_capacity.cs
      /* Max capacity: Greedy algorithm */\nint MaxCapacity(int[] ht) {\n    // Initialize i, j to be at both ends of the array\n    int i = 0, j = ht.Length - 1;\n    // Initial max capacity is 0\n    int res = 0;\n    // Loop for greedy selection until the two boards meet\n    while (i < j) {\n        // Update max capacity\n        int cap = Math.Min(ht[i], ht[j]) * (j - i);\n        res = Math.Max(res, cap);\n        // Move the shorter board inward\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
      max_capacity.go
      /* Max capacity: Greedy algorithm */\nfunc maxCapacity(ht []int) int {\n    // Initialize i, j to be at both ends of the array\n    i, j := 0, len(ht)-1\n    // Initial max capacity is 0\n    res := 0\n    // Loop for greedy selection until the two boards meet\n    for i < j {\n        // Update max capacity\n        capacity := int(math.Min(float64(ht[i]), float64(ht[j]))) * (j - i)\n        res = int(math.Max(float64(res), float64(capacity)))\n        // Move the shorter board inward\n        if ht[i] < ht[j] {\n            i++\n        } else {\n            j--\n        }\n    }\n    return res\n}\n
      max_capacity.swift
      /* Max capacity: Greedy algorithm */\nfunc maxCapacity(ht: [Int]) -> Int {\n    // Initialize i, j to be at both ends of the array\n    var i = ht.startIndex, j = ht.endIndex - 1\n    // Initial max capacity is 0\n    var res = 0\n    // Loop for greedy selection until the two boards meet\n    while i < j {\n        // Update max capacity\n        let cap = min(ht[i], ht[j]) * (j - i)\n        res = max(res, cap)\n        // Move the shorter board inward\n        if ht[i] < ht[j] {\n            i += 1\n        } else {\n            j -= 1\n        }\n    }\n    return res\n}\n
      max_capacity.js
      /* Max capacity: Greedy algorithm */\nfunction maxCapacity(ht) {\n    // Initialize i, j to be at both ends of the array\n    let i = 0,\n        j = ht.length - 1;\n    // Initial max capacity is 0\n    let res = 0;\n    // Loop for greedy selection until the two boards meet\n    while (i < j) {\n        // Update max capacity\n        const cap = Math.min(ht[i], ht[j]) * (j - i);\n        res = Math.max(res, cap);\n        // Move the shorter board inward\n        if (ht[i] < ht[j]) {\n            i += 1;\n        } else {\n            j -= 1;\n        }\n    }\n    return res;\n}\n
      max_capacity.ts
      /* Max capacity: Greedy algorithm */\nfunction maxCapacity(ht: number[]): number {\n    // Initialize i, j to be at both ends of the array\n    let i = 0,\n        j = ht.length - 1;\n    // Initial max capacity is 0\n    let res = 0;\n    // Loop for greedy selection until the two boards meet\n    while (i < j) {\n        // Update max capacity\n        const cap: number = Math.min(ht[i], ht[j]) * (j - i);\n        res = Math.max(res, cap);\n        // Move the shorter board inward\n        if (ht[i] < ht[j]) {\n            i += 1;\n        } else {\n            j -= 1;\n        }\n    }\n    return res;\n}\n
      max_capacity.dart
      /* Max capacity: Greedy algorithm */\nint maxCapacity(List<int> ht) {\n  // Initialize i, j to be at both ends of the array\n  int i = 0, j = ht.length - 1;\n  // Initial max capacity is 0\n  int res = 0;\n  // Loop for greedy selection until the two boards meet\n  while (i < j) {\n    // Update max capacity\n    int cap = min(ht[i], ht[j]) * (j - i);\n    res = max(res, cap);\n    // Move the shorter board inward\n    if (ht[i] < ht[j]) {\n      i++;\n    } else {\n      j--;\n    }\n  }\n  return res;\n}\n
      max_capacity.rs
      /* Max capacity: Greedy algorithm */\nfn max_capacity(ht: &[i32]) -> i32 {\n    // Initialize i, j to be at both ends of the array\n    let mut i = 0;\n    let mut j = ht.len() - 1;\n    // Initial max capacity is 0\n    let mut res = 0;\n    // Loop for greedy selection until the two boards meet\n    while i < j {\n        // Update max capacity\n        let cap = std::cmp::min(ht[i], ht[j]) * (j - i) as i32;\n        res = std::cmp::max(res, cap);\n        // Move the shorter board inward\n        if ht[i] < ht[j] {\n            i += 1;\n        } else {\n            j -= 1;\n        }\n    }\n    res\n}\n
      max_capacity.c
      /* Max capacity: Greedy algorithm */\nint maxCapacity(int ht[], int htLength) {\n    // Initialize i, j to be at both ends of the array\n    int i = 0;\n    int j = htLength - 1;\n    // Initial max capacity is 0\n    int res = 0;\n    // Loop for greedy selection until the two boards meet\n    while (i < j) {\n        // Update max capacity\n        int capacity = myMin(ht[i], ht[j]) * (j - i);\n        res = myMax(res, capacity);\n        // Move the shorter board inward\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
      max_capacity.kt
      /* Max capacity: Greedy algorithm */\nfun maxCapacity(ht: IntArray): Int {\n    // Initialize i, j to be at both ends of the array\n    var i = 0\n    var j = ht.size - 1\n    // Initial max capacity is 0\n    var res = 0\n    // Loop for greedy selection until the two boards meet\n    while (i < j) {\n        // Update max capacity\n        val cap = min(ht[i], ht[j]) * (j - i)\n        res = max(res, cap)\n        // Move the shorter board inward\n        if (ht[i] < ht[j]) {\n            i++\n        } else {\n            j--\n        }\n    }\n    return res\n}\n
      max_capacity.rb
      ### Maximum capacity: greedy ###\ndef max_capacity(ht)\n  # Initialize i, j to be at both ends of the array\n  i, j = 0, ht.length - 1\n  # Initial max capacity is 0\n  res = 0\n\n  # Loop for greedy selection until the two boards meet\n  while i < j\n    # Update max capacity\n    cap = [ht[i], ht[j]].min * (j - i)\n    res = [res, cap].max\n    # Move the shorter board inward\n    if ht[i] < ht[j]\n      i += 1\n    else\n      j -= 1\n    end\n  end\n\n  res\nend\n
      "},{"location":"chapter_greedy/max_capacity_problem/#3-correctness-proof","title":"3. \u00a0 Correctness Proof","text":"

      The reason greedy is faster than exhaustive enumeration is that each round of greedy selection \"skips\" some states.

      For example, in state \\(cap[i, j]\\) where \\(i\\) is the short partition and \\(j\\) is the long partition, if we greedily move the short partition \\(i\\) inward by one position, the states shown in Figure 15-12 will be \"skipped\". This means that the capacities of these states cannot be verified later.

      \\[ cap[i, i+1], cap[i, i+2], \\dots, cap[i, j-2], cap[i, j-1] \\]

      Figure 15-12 \u00a0 States skipped by moving the short partition

      Observing carefully, these skipped states are actually all the states obtained by moving the long partition \\(j\\) inward. We have already proven that moving the long partition inward will definitely decrease capacity. That is, the skipped states cannot possibly be the optimal solution, skipping them will not cause us to miss the optimal solution.

      The above analysis shows that the operation of moving the short partition is \"safe\", and the greedy strategy is effective.

      "},{"location":"chapter_greedy/max_product_cutting_problem/","title":"15.4 \u00a0 Max Product Cutting Problem","text":"

      Question

      Given a positive integer \\(n\\), split it into the sum of at least two positive integers, and find the maximum product of all integers after splitting, as shown in Figure 15-13.

      Figure 15-13 \u00a0 Problem definition of max product cutting

      Suppose we split \\(n\\) into \\(m\\) integer factors, where the \\(i\\)-th factor is denoted as \\(n_i\\), that is

      \\[ n = \\sum_{i=1}^{m}n_i \\]

      The goal of this problem is to find the maximum product of all integer factors, namely

      \\[ \\max(\\prod_{i=1}^{m}n_i) \\]

      We need to think about: how large should the splitting count \\(m\\) be, and what should each \\(n_i\\) be?

      "},{"location":"chapter_greedy/max_product_cutting_problem/#1-greedy-strategy-determination","title":"1. \u00a0 Greedy Strategy Determination","text":"

      Based on experience, the product of two integers is often greater than their sum. Suppose we split out a factor of \\(2\\) from \\(n\\), then their product is \\(2(n-2)\\). We compare this product with \\(n\\):

      \\[ \\begin{aligned} 2(n-2) & \\geq n \\newline 2n - n - 4 & \\geq 0 \\newline n & \\geq 4 \\end{aligned} \\]

      As shown in Figure 15-14, when \\(n \\geq 4\\), splitting out a \\(2\\) will increase the product, which indicates that integers greater than or equal to \\(4\\) should all be split.

      Greedy strategy one: If the splitting scheme includes factors \\(\\geq 4\\), then they should continue to be split. The final splitting scheme should only contain factors \\(1\\), \\(2\\), and \\(3\\).

      Figure 15-14 \u00a0 Splitting causes product to increase

      Next, consider which factor is optimal. Among the three factors \\(1\\), \\(2\\), and \\(3\\), clearly \\(1\\) is the worst, because \\(1 \\times (n-1) < n\\) always holds, meaning splitting out \\(1\\) will actually decrease the product.

      As shown in Figure 15-15, when \\(n = 6\\), we have \\(3 \\times 3 > 2 \\times 2 \\times 2\\). This means that splitting out \\(3\\) is better than splitting out \\(2\\).

      Greedy strategy two: In the splitting scheme, there should be at most two \\(2\\)s. Because three \\(2\\)s can always be replaced by two \\(3\\)s to obtain a larger product.

      Figure 15-15 \u00a0 Optimal splitting factor

      In summary, the following greedy strategies can be derived.

      1. Input integer \\(n\\), continuously split out factor \\(3\\) until the remainder is \\(0\\), \\(1\\), or \\(2\\).
      2. When the remainder is \\(0\\), it means \\(n\\) is a multiple of \\(3\\), so no further action is needed.
      3. When the remainder is \\(2\\), do not continue splitting, keep it.
      4. When the remainder is \\(1\\), since \\(2 \\times 2 > 1 \\times 3\\), the last \\(3\\) should be replaced with \\(2\\).
      "},{"location":"chapter_greedy/max_product_cutting_problem/#2-code-implementation","title":"2. \u00a0 Code Implementation","text":"

      As shown in Figure 15-16, we don't need to use loops to split the integer, but can use integer division to get the count of \\(3\\)s as \\(a\\), and modulo operation to get the remainder as \\(b\\), at which point we have:

      \\[ n = 3 a + b \\]

      Please note that for the edge case of \\(n \\leq 3\\), a \\(1\\) must be split out, with product \\(1 \\times (n - 1)\\).

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby max_product_cutting.py
      def max_product_cutting(n: int) -> int:\n    \"\"\"Max product cutting: Greedy algorithm\"\"\"\n    # When n <= 3, must cut out a 1\n    if n <= 3:\n        return 1 * (n - 1)\n    # Greedily cut out 3, a is the number of 3s, b is the remainder\n    a, b = n // 3, n % 3\n    if b == 1:\n        # When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n        return int(math.pow(3, a - 1)) * 2 * 2\n    if b == 2:\n        # When the remainder is 2, do nothing\n        return int(math.pow(3, a)) * 2\n    # When the remainder is 0, do nothing\n    return int(math.pow(3, a))\n
      max_product_cutting.cpp
      /* Max product cutting: Greedy algorithm */\nint maxProductCutting(int n) {\n    // When n <= 3, must cut out a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // Greedily cut out 3, a is the number of 3s, b is the remainder\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n        return (int)pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // When the remainder is 2, do nothing\n        return (int)pow(3, a) * 2;\n    }\n    // When the remainder is 0, do nothing\n    return (int)pow(3, a);\n}\n
      max_product_cutting.java
      /* Max product cutting: Greedy algorithm */\nint maxProductCutting(int n) {\n    // When n <= 3, must cut out a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // Greedily cut out 3, a is the number of 3s, b is the remainder\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n        return (int) Math.pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // When the remainder is 2, do nothing\n        return (int) Math.pow(3, a) * 2;\n    }\n    // When the remainder is 0, do nothing\n    return (int) Math.pow(3, a);\n}\n
      max_product_cutting.cs
      /* Max product cutting: Greedy algorithm */\nint MaxProductCutting(int n) {\n    // When n <= 3, must cut out a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // Greedily cut out 3, a is the number of 3s, b is the remainder\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n        return (int)Math.Pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // When the remainder is 2, do nothing\n        return (int)Math.Pow(3, a) * 2;\n    }\n    // When the remainder is 0, do nothing\n    return (int)Math.Pow(3, a);\n}\n
      max_product_cutting.go
      /* Max product cutting: Greedy algorithm */\nfunc maxProductCutting(n int) int {\n    // When n <= 3, must cut out a 1\n    if n <= 3 {\n        return 1 * (n - 1)\n    }\n    // Greedily cut out 3, a is the number of 3s, b is the remainder\n    a := n / 3\n    b := n % 3\n    if b == 1 {\n        // When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n        return int(math.Pow(3, float64(a-1))) * 2 * 2\n    }\n    if b == 2 {\n        // When the remainder is 2, do nothing\n        return int(math.Pow(3, float64(a))) * 2\n    }\n    // When the remainder is 0, do nothing\n    return int(math.Pow(3, float64(a)))\n}\n
      max_product_cutting.swift
      /* Max product cutting: Greedy algorithm */\nfunc maxProductCutting(n: Int) -> Int {\n    // When n <= 3, must cut out a 1\n    if n <= 3 {\n        return 1 * (n - 1)\n    }\n    // Greedily cut out 3, a is the number of 3s, b is the remainder\n    let a = n / 3\n    let b = n % 3\n    if b == 1 {\n        // When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n        return pow(3, a - 1) * 2 * 2\n    }\n    if b == 2 {\n        // When the remainder is 2, do nothing\n        return pow(3, a) * 2\n    }\n    // When the remainder is 0, do nothing\n    return pow(3, a)\n}\n
      max_product_cutting.js
      /* Max product cutting: Greedy algorithm */\nfunction maxProductCutting(n) {\n    // When n <= 3, must cut out a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // Greedily cut out 3, a is the number of 3s, b is the remainder\n    let a = Math.floor(n / 3);\n    let b = n % 3;\n    if (b === 1) {\n        // When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n        return Math.pow(3, a - 1) * 2 * 2;\n    }\n    if (b === 2) {\n        // When the remainder is 2, do nothing\n        return Math.pow(3, a) * 2;\n    }\n    // When the remainder is 0, do nothing\n    return Math.pow(3, a);\n}\n
      max_product_cutting.ts
      /* Max product cutting: Greedy algorithm */\nfunction maxProductCutting(n: number): number {\n    // When n <= 3, must cut out a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // Greedily cut out 3, a is the number of 3s, b is the remainder\n    let a: number = Math.floor(n / 3);\n    let b: number = n % 3;\n    if (b === 1) {\n        // When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n        return Math.pow(3, a - 1) * 2 * 2;\n    }\n    if (b === 2) {\n        // When the remainder is 2, do nothing\n        return Math.pow(3, a) * 2;\n    }\n    // When the remainder is 0, do nothing\n    return Math.pow(3, a);\n}\n
      max_product_cutting.dart
      /* Max product cutting: Greedy algorithm */\nint maxProductCutting(int n) {\n  // When n <= 3, must cut out a 1\n  if (n <= 3) {\n    return 1 * (n - 1);\n  }\n  // Greedily cut out 3, a is the number of 3s, b is the remainder\n  int a = n ~/ 3;\n  int b = n % 3;\n  if (b == 1) {\n    // When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n    return (pow(3, a - 1) * 2 * 2).toInt();\n  }\n  if (b == 2) {\n    // When the remainder is 2, do nothing\n    return (pow(3, a) * 2).toInt();\n  }\n  // When the remainder is 0, do nothing\n  return pow(3, a).toInt();\n}\n
      max_product_cutting.rs
      /* Max product cutting: Greedy algorithm */\nfn max_product_cutting(n: i32) -> i32 {\n    // When n <= 3, must cut out a 1\n    if n <= 3 {\n        return 1 * (n - 1);\n    }\n    // Greedily cut out 3, a is the number of 3s, b is the remainder\n    let a = n / 3;\n    let b = n % 3;\n    if b == 1 {\n        // When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n        3_i32.pow(a as u32 - 1) * 2 * 2\n    } else if b == 2 {\n        // When the remainder is 2, do nothing\n        3_i32.pow(a as u32) * 2\n    } else {\n        // When the remainder is 0, do nothing\n        3_i32.pow(a as u32)\n    }\n}\n
      max_product_cutting.c
      /* Max product cutting: Greedy algorithm */\nint maxProductCutting(int n) {\n    // When n <= 3, must cut out a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // Greedily cut out 3, a is the number of 3s, b is the remainder\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n        return pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // When the remainder is 2, do nothing\n        return pow(3, a) * 2;\n    }\n    // When the remainder is 0, do nothing\n    return pow(3, a);\n}\n
      max_product_cutting.kt
      /* Max product cutting: Greedy algorithm */\nfun maxProductCutting(n: Int): Int {\n    // When n <= 3, must cut out a 1\n    if (n <= 3) {\n        return 1 * (n - 1)\n    }\n    // Greedily cut out 3, a is the number of 3s, b is the remainder\n    val a = n / 3\n    val b = n % 3\n    if (b == 1) {\n        // When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n        return 3.0.pow((a - 1)).toInt() * 2 * 2\n    }\n    if (b == 2) {\n        // When the remainder is 2, do nothing\n        return 3.0.pow(a).toInt() * 2 * 2\n    }\n    // When the remainder is 0, do nothing\n    return 3.0.pow(a).toInt()\n}\n
      max_product_cutting.rb
      ### Maximum cutting product: greedy ###\ndef max_product_cutting(n)\n  # When n <= 3, must cut out a 1\n  return 1 * (n - 1) if n <= 3\n  # Greedily cut out 3, a is the number of 3s, b is the remainder\n  a, b = n / 3, n % 3\n  # When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n  return (3.pow(a - 1) * 2 * 2).to_i if b == 1\n  # When the remainder is 2, do nothing\n  return (3.pow(a) * 2).to_i if b == 2\n  # When the remainder is 0, do nothing\n  3.pow(a).to_i\nend\n

      Figure 15-16 \u00a0 Calculation method for max product cutting

      The time complexity depends on the implementation of the exponentiation operation in the programming language. Taking Python as an example, there are three commonly used power calculation functions.

      • Both the operator ** and the function pow() have time complexity \\(O(\\log\u2061 a)\\).
      • The function math.pow() internally calls the C library's pow() function, which performs floating-point exponentiation, with time complexity \\(O(1)\\).

      Variables \\(a\\) and \\(b\\) use a constant amount of extra space, therefore the space complexity is \\(O(1)\\).

      "},{"location":"chapter_greedy/max_product_cutting_problem/#3-correctness-proof","title":"3. \u00a0 Correctness Proof","text":"

      Using proof by contradiction, only analyzing the case where \\(n \\geq 4\\).

      1. All factors \\(\\leq 3\\): Suppose the optimal splitting scheme includes a factor \\(x \\geq 4\\), then it can definitely continue to be split into \\(2(x-2)\\) to obtain a larger (or equal) product. This contradicts the assumption.
      2. The splitting scheme does not contain \\(1\\): Suppose the optimal splitting scheme includes a factor of \\(1\\), then it can definitely be merged into another factor to obtain a larger product. This contradicts the assumption.
      3. The splitting scheme contains at most two \\(2\\)s: Suppose the optimal splitting scheme includes three \\(2\\)s, then they can definitely be replaced by two \\(3\\)s for a larger product. This contradicts the assumption.
      "},{"location":"chapter_greedy/summary/","title":"15.5 \u00a0 Summary","text":""},{"location":"chapter_greedy/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • Greedy algorithms are typically used to solve optimization problems. The principle is to make locally optimal decisions at each decision stage in hopes of obtaining a globally optimal solution.
      • Greedy algorithms iteratively make one greedy choice after another, transforming the problem into a smaller subproblem in each round, until the problem is solved.
      • Greedy algorithms are not only simple to implement, but also have high problem-solving efficiency. Compared to dynamic programming, greedy algorithms typically have lower time complexity.
      • In the coin change problem, for certain coin combinations, greedy algorithms can guarantee finding the optimal solution; for other coin combinations, however, greedy algorithms may find very poor solutions.
      • Problems suitable for solving with greedy algorithms have two major properties: greedy choice property and optimal substructure. The greedy choice property represents the effectiveness of the greedy strategy.
      • For some complex problems, proving the greedy choice property is not simple. Relatively speaking, disproving it is easier, such as in the coin change problem.
      • Solving greedy problems mainly consists of three steps: problem analysis, determining the greedy strategy, and correctness proof. Among these, determining the greedy strategy is the core step, and correctness proof is often the difficult point.
      • The fractional knapsack problem, based on the 0-1 knapsack problem, allows selecting a portion of items, and therefore can be solved using greedy algorithms. The correctness of the greedy strategy can be proven using proof by contradiction.
      • The max capacity problem can be solved using exhaustive enumeration with time complexity \\(O(n^2)\\). By designing a greedy strategy to move the short partition inward in each round, the time complexity can be optimized to \\(O(n)\\).
      • In the max product cutting problem, we successively derive two greedy strategies: integers \\(\\geq 4\\) should all continue to be split, and the optimal splitting factor is \\(3\\). The code includes exponentiation operations, and the time complexity depends on the implementation method of exponentiation, typically being \\(O(1)\\) or \\(O(\\log n)\\).
      "},{"location":"chapter_hashing/","title":"Chapter 6. \u00a0 Hashing","text":"

      Abstract

      In the world of computing, a hash table is like a clever librarian.

      They know how to calculate call numbers, enabling them to quickly locate the target book.

      "},{"location":"chapter_hashing/#chapter-contents","title":"Chapter contents","text":"
      • 6.1 \u00a0 Hash Table
      • 6.2 \u00a0 Hash Collision
      • 6.3 \u00a0 Hash Algorithm
      • 6.4 \u00a0 Summary
      "},{"location":"chapter_hashing/hash_algorithm/","title":"6.3 \u00a0 Hash Algorithm","text":"

      The previous two sections introduced the working principle of hash tables and the methods to handle hash collisions. However, both open addressing and separate chaining can only ensure that the hash table functions normally when hash collisions occur, but cannot reduce the frequency of hash collisions.

      If hash collisions occur too frequently, the performance of the hash table will deteriorate drastically. As shown in Figure 6-8, for a separate chaining hash table, in the ideal case, the key-value pairs are evenly distributed across the buckets, achieving optimal query efficiency; in the worst case, all key-value pairs are stored in the same bucket, degrading the time complexity to \\(O(n)\\).

      Figure 6-8 \u00a0 Ideal and worst cases of hash collisions

      The distribution of key-value pairs is determined by the hash function. Recalling the calculation steps of the hash function, first compute the hash value, then take the modulo by the array length:

      index = hash(key) % capacity\n

      Observing the above formula, when the hash table capacity capacity is fixed, the hash algorithm hash() determines the output value, thereby determining the distribution of key-value pairs in the hash table.

      This means that, to reduce the probability of hash collisions, we should focus on the design of the hash algorithm hash().

      "},{"location":"chapter_hashing/hash_algorithm/#631-goals-of-hash-algorithms","title":"6.3.1 \u00a0 Goals of Hash Algorithms","text":"

      To achieve a \"fast and stable\" hash table data structure, hash algorithms should have the following characteristics:

      • Determinism: For the same input, the hash algorithm should always produce the same output. Only then can the hash table be reliable.
      • High efficiency: The process of computing the hash value should be fast enough. The smaller the computational overhead, the more practical the hash table.
      • Uniform distribution: The hash algorithm should ensure that key-value pairs are evenly distributed in the hash table. The more uniform the distribution, the lower the probability of hash collisions.

      In fact, hash algorithms are not only used to implement hash tables but are also widely applied in other fields.

      • Password storage: To protect the security of user passwords, systems usually do not store the plaintext passwords but rather the hash values of the passwords. When a user enters a password, the system calculates the hash value of the input and compares it with the stored hash value. If they match, the password is considered correct.
      • Data integrity check: The data sender can calculate the hash value of the data and send it along; the receiver can recalculate the hash value of the received data and compare it with the received hash value. If they match, the data is considered intact.

      For cryptographic applications, to prevent reverse engineering such as deducing the original password from the hash value, hash algorithms need higher-level security features.

      • Unidirectionality: It should be impossible to deduce any information about the input data from the hash value.
      • Collision resistance: It should be extremely difficult to find two different inputs that produce the same hash value.
      • Avalanche effect: Minor changes in the input should lead to significant and unpredictable changes in the output.

      Note that \"uniform distribution\" and \"collision resistance\" are two independent concepts. Satisfying uniform distribution does not necessarily mean collision resistance. For example, under random input key, the hash function key % 100 can produce a uniformly distributed output. However, this hash algorithm is too simple, and all key with the same last two digits will have the same output, making it easy to deduce a usable key from the hash value, thereby cracking the password.

      "},{"location":"chapter_hashing/hash_algorithm/#632-design-of-hash-algorithms","title":"6.3.2 \u00a0 Design of Hash Algorithms","text":"

      The design of hash algorithms is a complex issue that requires consideration of many factors. However, for some less demanding scenarios, we can also design some simple hash algorithms.

      • Additive hash: Add up the ASCII codes of each character in the input and use the total sum as the hash value.
      • Multiplicative hash: Utilize the non-correlation of multiplication, multiplying each round by a constant, accumulating the ASCII codes of each character into the hash value.
      • XOR hash: Accumulate the hash value by XORing each element of the input data.
      • Rotating hash: Accumulate the ASCII code of each character into a hash value, performing a rotation operation on the hash value before each accumulation.
      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby simple_hash.py
      def add_hash(key: str) -> int:\n    \"\"\"Additive hash\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash += ord(c)\n    return hash % modulus\n\ndef mul_hash(key: str) -> int:\n    \"\"\"Multiplicative hash\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash = 31 * hash + ord(c)\n    return hash % modulus\n\ndef xor_hash(key: str) -> int:\n    \"\"\"XOR hash\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash ^= ord(c)\n    return hash % modulus\n\ndef rot_hash(key: str) -> int:\n    \"\"\"Rotational hash\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash = (hash << 4) ^ (hash >> 28) ^ ord(c)\n    return hash % modulus\n
      simple_hash.cpp
      /* Additive hash */\nint addHash(string key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash = (hash + (int)c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* Multiplicative hash */\nint mulHash(string key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash = (31 * hash + (int)c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* XOR hash */\nint xorHash(string key) {\n    int hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash ^= (int)c;\n    }\n    return hash & MODULUS;\n}\n\n/* Rotational hash */\nint rotHash(string key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ (int)c) % MODULUS;\n    }\n    return (int)hash;\n}\n
      simple_hash.java
      /* Additive hash */\nint addHash(String key) {\n    long hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash = (hash + (int) c) % MODULUS;\n    }\n    return (int) hash;\n}\n\n/* Multiplicative hash */\nint mulHash(String key) {\n    long hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash = (31 * hash + (int) c) % MODULUS;\n    }\n    return (int) hash;\n}\n\n/* XOR hash */\nint xorHash(String key) {\n    int hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash ^= (int) c;\n    }\n    return hash & MODULUS;\n}\n\n/* Rotational hash */\nint rotHash(String key) {\n    long hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ (int) c) % MODULUS;\n    }\n    return (int) hash;\n}\n
      simple_hash.cs
      /* Additive hash */\nint AddHash(string key) {\n    long hash = 0;\n    const int MODULUS = 1000000007;\n    foreach (char c in key) {\n        hash = (hash + c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* Multiplicative hash */\nint MulHash(string key) {\n    long hash = 0;\n    const int MODULUS = 1000000007;\n    foreach (char c in key) {\n        hash = (31 * hash + c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* XOR hash */\nint XorHash(string key) {\n    int hash = 0;\n    const int MODULUS = 1000000007;\n    foreach (char c in key) {\n        hash ^= c;\n    }\n    return hash & MODULUS;\n}\n\n/* Rotational hash */\nint RotHash(string key) {\n    long hash = 0;\n    const int MODULUS = 1000000007;\n    foreach (char c in key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ c) % MODULUS;\n    }\n    return (int)hash;\n}\n
      simple_hash.go
      /* Additive hash */\nfunc addHash(key string) int {\n    var hash int64\n    var modulus int64\n\n    modulus = 1000000007\n    for _, b := range []byte(key) {\n        hash = (hash + int64(b)) % modulus\n    }\n    return int(hash)\n}\n\n/* Multiplicative hash */\nfunc mulHash(key string) int {\n    var hash int64\n    var modulus int64\n\n    modulus = 1000000007\n    for _, b := range []byte(key) {\n        hash = (31*hash + int64(b)) % modulus\n    }\n    return int(hash)\n}\n\n/* XOR hash */\nfunc xorHash(key string) int {\n    hash := 0\n    modulus := 1000000007\n    for _, b := range []byte(key) {\n        fmt.Println(int(b))\n        hash ^= int(b)\n        hash = (31*hash + int(b)) % modulus\n    }\n    return hash & modulus\n}\n\n/* Rotational hash */\nfunc rotHash(key string) int {\n    var hash int64\n    var modulus int64\n\n    modulus = 1000000007\n    for _, b := range []byte(key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ int64(b)) % modulus\n    }\n    return int(hash)\n}\n
      simple_hash.swift
      /* Additive hash */\nfunc addHash(key: String) -> Int {\n    var hash = 0\n    let MODULUS = 1_000_000_007\n    for c in key {\n        for scalar in c.unicodeScalars {\n            hash = (hash + Int(scalar.value)) % MODULUS\n        }\n    }\n    return hash\n}\n\n/* Multiplicative hash */\nfunc mulHash(key: String) -> Int {\n    var hash = 0\n    let MODULUS = 1_000_000_007\n    for c in key {\n        for scalar in c.unicodeScalars {\n            hash = (31 * hash + Int(scalar.value)) % MODULUS\n        }\n    }\n    return hash\n}\n\n/* XOR hash */\nfunc xorHash(key: String) -> Int {\n    var hash = 0\n    let MODULUS = 1_000_000_007\n    for c in key {\n        for scalar in c.unicodeScalars {\n            hash ^= Int(scalar.value)\n        }\n    }\n    return hash & MODULUS\n}\n\n/* Rotational hash */\nfunc rotHash(key: String) -> Int {\n    var hash = 0\n    let MODULUS = 1_000_000_007\n    for c in key {\n        for scalar in c.unicodeScalars {\n            hash = ((hash << 4) ^ (hash >> 28) ^ Int(scalar.value)) % MODULUS\n        }\n    }\n    return hash\n}\n
      simple_hash.js
      /* Additive hash */\nfunction addHash(key) {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = (hash + c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n\n/* Multiplicative hash */\nfunction mulHash(key) {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n\n/* XOR hash */\nfunction xorHash(key) {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash ^= c.charCodeAt(0);\n    }\n    return hash % MODULUS;\n}\n\n/* Rotational hash */\nfunction rotHash(key) {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n
      simple_hash.ts
      /* Additive hash */\nfunction addHash(key: string): number {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = (hash + c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n\n/* Multiplicative hash */\nfunction mulHash(key: string): number {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n\n/* XOR hash */\nfunction xorHash(key: string): number {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash ^= c.charCodeAt(0);\n    }\n    return hash % MODULUS;\n}\n\n/* Rotational hash */\nfunction rotHash(key: string): number {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n
      simple_hash.dart
      /* Additive hash */\nint addHash(String key) {\n  int hash = 0;\n  final int MODULUS = 1000000007;\n  for (int i = 0; i < key.length; i++) {\n    hash = (hash + key.codeUnitAt(i)) % MODULUS;\n  }\n  return hash;\n}\n\n/* Multiplicative hash */\nint mulHash(String key) {\n  int hash = 0;\n  final int MODULUS = 1000000007;\n  for (int i = 0; i < key.length; i++) {\n    hash = (31 * hash + key.codeUnitAt(i)) % MODULUS;\n  }\n  return hash;\n}\n\n/* XOR hash */\nint xorHash(String key) {\n  int hash = 0;\n  final int MODULUS = 1000000007;\n  for (int i = 0; i < key.length; i++) {\n    hash ^= key.codeUnitAt(i);\n  }\n  return hash & MODULUS;\n}\n\n/* Rotational hash */\nint rotHash(String key) {\n  int hash = 0;\n  final int MODULUS = 1000000007;\n  for (int i = 0; i < key.length; i++) {\n    hash = ((hash << 4) ^ (hash >> 28) ^ key.codeUnitAt(i)) % MODULUS;\n  }\n  return hash;\n}\n
      simple_hash.rs
      /* Additive hash */\nfn add_hash(key: &str) -> i32 {\n    let mut hash = 0_i64;\n    const MODULUS: i64 = 1000000007;\n\n    for c in key.chars() {\n        hash = (hash + c as i64) % MODULUS;\n    }\n\n    hash as i32\n}\n\n/* Multiplicative hash */\nfn mul_hash(key: &str) -> i32 {\n    let mut hash = 0_i64;\n    const MODULUS: i64 = 1000000007;\n\n    for c in key.chars() {\n        hash = (31 * hash + c as i64) % MODULUS;\n    }\n\n    hash as i32\n}\n\n/* XOR hash */\nfn xor_hash(key: &str) -> i32 {\n    let mut hash = 0_i64;\n    const MODULUS: i64 = 1000000007;\n\n    for c in key.chars() {\n        hash ^= c as i64;\n    }\n\n    (hash & MODULUS) as i32\n}\n\n/* Rotational hash */\nfn rot_hash(key: &str) -> i32 {\n    let mut hash = 0_i64;\n    const MODULUS: i64 = 1000000007;\n\n    for c in key.chars() {\n        hash = ((hash << 4) ^ (hash >> 28) ^ c as i64) % MODULUS;\n    }\n\n    hash as i32\n}\n
      simple_hash.c
      /* Additive hash */\nint addHash(char *key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (int i = 0; i < strlen(key); i++) {\n        hash = (hash + (unsigned char)key[i]) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* Multiplicative hash */\nint mulHash(char *key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (int i = 0; i < strlen(key); i++) {\n        hash = (31 * hash + (unsigned char)key[i]) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* XOR hash */\nint xorHash(char *key) {\n    int hash = 0;\n    const int MODULUS = 1000000007;\n\n    for (int i = 0; i < strlen(key); i++) {\n        hash ^= (unsigned char)key[i];\n    }\n    return hash & MODULUS;\n}\n\n/* Rotational hash */\nint rotHash(char *key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (int i = 0; i < strlen(key); i++) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ (unsigned char)key[i]) % MODULUS;\n    }\n\n    return (int)hash;\n}\n
      simple_hash.kt
      /* Additive hash */\nfun addHash(key: String): Int {\n    var hash = 0L\n    val MODULUS = 1000000007\n    for (c in key.toCharArray()) {\n        hash = (hash + c.code) % MODULUS\n    }\n    return hash.toInt()\n}\n\n/* Multiplicative hash */\nfun mulHash(key: String): Int {\n    var hash = 0L\n    val MODULUS = 1000000007\n    for (c in key.toCharArray()) {\n        hash = (31 * hash + c.code) % MODULUS\n    }\n    return hash.toInt()\n}\n\n/* XOR hash */\nfun xorHash(key: String): Int {\n    var hash = 0\n    val MODULUS = 1000000007\n    for (c in key.toCharArray()) {\n        hash = hash xor c.code\n    }\n    return hash and MODULUS\n}\n\n/* Rotational hash */\nfun rotHash(key: String): Int {\n    var hash = 0L\n    val MODULUS = 1000000007\n    for (c in key.toCharArray()) {\n        hash = ((hash shl 4) xor (hash shr 28) xor c.code.toLong()) % MODULUS\n    }\n    return hash.toInt()\n}\n
      simple_hash.rb
      ### Additive hash ###\ndef add_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash += c.ord }\n\n  hash % modulus\nend\n\n### Multiplicative hash ###\ndef mul_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash = 31 * hash + c.ord }\n\n  hash % modulus\nend\n\n### XOR hash ###\ndef xor_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash ^= c.ord }\n\n  hash % modulus\nend\n\n### Rotational hash ###\ndef rot_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash = (hash << 4) ^ (hash >> 28) ^ c.ord }\n\n  hash % modulus\nend\n

      It is observed that the last step of each hash algorithm is to take the modulus of the large prime number \\(1000000007\\) to ensure that the hash value is within an appropriate range. It is worth pondering why emphasis is placed on modulo a prime number, or what are the disadvantages of modulo a composite number? This is an interesting question.

      To conclude: Using a large prime number as the modulus can maximize the uniform distribution of hash values. Since a prime number does not share common factors with other numbers, it can reduce the periodic patterns caused by the modulo operation, thus avoiding hash collisions.

      For example, suppose we choose the composite number \\(9\\) as the modulus, which can be divided by \\(3\\), then all key divisible by \\(3\\) will be mapped to hash values \\(0\\), \\(3\\), \\(6\\).

      \\[ \\begin{aligned} \\text{modulus} & = 9 \\newline \\text{key} & = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\dots \\} \\newline \\text{hash} & = \\{ 0, 3, 6, 0, 3, 6, 0, 3, 6, 0, 3, 6,\\dots \\} \\end{aligned} \\]

      If the input key happens to have this kind of arithmetic sequence distribution, then the hash values will cluster, thereby exacerbating hash collisions. Now, suppose we replace modulus with the prime number \\(13\\), since there are no common factors between key and modulus, the uniformity of the output hash values will be significantly improved.

      \\[ \\begin{aligned} \\text{modulus} & = 13 \\newline \\text{key} & = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\dots \\} \\newline \\text{hash} & = \\{ 0, 3, 6, 9, 12, 2, 5, 8, 11, 1, 4, 7, \\dots \\} \\end{aligned} \\]

      It is worth noting that if the key is guaranteed to be randomly and uniformly distributed, then choosing a prime number or a composite number as the modulus can both produce uniformly distributed hash values. However, when the distribution of key has some periodicity, modulo a composite number is more likely to result in clustering.

      In summary, we usually choose a prime number as the modulus, and this prime number should be large enough to eliminate periodic patterns as much as possible, enhancing the robustness of the hash algorithm.

      "},{"location":"chapter_hashing/hash_algorithm/#633-common-hash-algorithms","title":"6.3.3 \u00a0 Common Hash Algorithms","text":"

      It is not hard to see that the simple hash algorithms mentioned above are quite \"fragile\" and far from reaching the design goals of hash algorithms. For example, since addition and XOR obey the commutative law, additive hash and XOR hash cannot distinguish strings with the same content but in different order, which may exacerbate hash collisions and cause security issues.

      In practice, we usually use some standard hash algorithms, such as MD5, SHA-1, SHA-2, and SHA-3. They can map input data of any length to a fixed-length hash value.

      Over the past century, hash algorithms have been in a continuous process of upgrading and optimization. Some researchers strive to improve the performance of hash algorithms, while others, including hackers, are dedicated to finding security issues in hash algorithms. Table 6-2 shows hash algorithms commonly used in practical applications.

      • MD5 and SHA-1 have been successfully attacked multiple times and are thus abandoned in various security applications.
      • SHA-2 series, especially SHA-256, is one of the most secure hash algorithms to date, with no successful attacks reported, hence commonly used in various security applications and protocols.
      • SHA-3 has lower implementation costs and higher computational efficiency compared to SHA-2, but its current usage coverage is not as extensive as the SHA-2 series.

      Table 6-2 \u00a0 Common hash algorithms

      MD5 SHA-1 SHA-2 SHA-3 Release Year 1992 1995 2002 2008 Output Length 128 bit 160 bit 256/512 bit 224/256/384/512 bit Hash Collisions Frequent Frequent Rare Rare Security Level Low, has been successfully attacked Low, has been successfully attacked High High Applications Abandoned, still used for data integrity checks Abandoned Cryptocurrency transaction verification, digital signatures, etc. Can be used to replace SHA-2"},{"location":"chapter_hashing/hash_algorithm/#hash-values-in-data-structures","title":"Hash Values in Data Structures","text":"

      We know that the keys in a hash table can be of various data types such as integers, decimals, or strings. Programming languages usually provide built-in hash algorithms for these data types to calculate the bucket indices in the hash table. Taking Python as an example, we can use the hash() function to compute the hash values for various data types.

      • The hash values of integers and booleans are their own values.
      • The calculation of hash values for floating-point numbers and strings is more complex, and interested readers are encouraged to study this on their own.
      • The hash value of a tuple is a combination of the hash values of each of its elements, resulting in a single hash value.
      • The hash value of an object is generated based on its memory address. By overriding the hash method of an object, hash values can be generated based on content.

      Tip

      Be aware that the definition and methods of the built-in hash value calculation functions in different programming languages vary.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby built_in_hash.py
      num = 3\nhash_num = hash(num)\n# Hash value of integer 3 is 3\n\nbol = True\nhash_bol = hash(bol)\n# Hash value of boolean True is 1\n\ndec = 3.14159\nhash_dec = hash(dec)\n# Hash value of decimal 3.14159 is 326484311674566659\n\nstr = \"Hello \u7b97\u6cd5\"\nhash_str = hash(str)\n# Hash value of string \"Hello \u7b97\u6cd5\" is 4617003410720528961\n\ntup = (12836, \"\u5c0f\u54c8\")\nhash_tup = hash(tup)\n# Hash value of tuple (12836, '\u5c0f\u54c8') is 1029005403108185979\n\nobj = ListNode(0)\nhash_obj = hash(obj)\n# Hash value of ListNode object at 0x1058fd810 is 274267521\n
      built_in_hash.cpp
      int num = 3;\nsize_t hashNum = hash<int>()(num);\n// Hash value of integer 3 is 3\n\nbool bol = true;\nsize_t hashBol = hash<bool>()(bol);\n// Hash value of boolean 1 is 1\n\ndouble dec = 3.14159;\nsize_t hashDec = hash<double>()(dec);\n// Hash value of decimal 3.14159 is 4614256650576692846\n\nstring str = \"Hello \u7b97\u6cd5\";\nsize_t hashStr = hash<string>()(str);\n// Hash value of string \"Hello \u7b97\u6cd5\" is 15466937326284535026\n\n// In C++, built-in std::hash() only provides hash values for basic data types\n// Hash values for arrays and objects need to be implemented separately\n
      built_in_hash.java
      int num = 3;\nint hashNum = Integer.hashCode(num);\n// Hash value of integer 3 is 3\n\nboolean bol = true;\nint hashBol = Boolean.hashCode(bol);\n// Hash value of boolean true is 1231\n\ndouble dec = 3.14159;\nint hashDec = Double.hashCode(dec);\n// Hash value of decimal 3.14159 is -1340954729\n\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode();\n// Hash value of string \"Hello \u7b97\u6cd5\" is -727081396\n\nObject[] arr = { 12836, \"\u5c0f\u54c8\" };\nint hashTup = Arrays.hashCode(arr);\n// Hash value of array [12836, \u5c0f\u54c8] is 1151158\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode();\n// Hash value of ListNode object utils.ListNode@7dc5e7b4 is 2110121908\n
      built_in_hash.cs
      int num = 3;\nint hashNum = num.GetHashCode();\n// Hash value of integer 3 is 3;\n\nbool bol = true;\nint hashBol = bol.GetHashCode();\n// Hash value of boolean true is 1;\n\ndouble dec = 3.14159;\nint hashDec = dec.GetHashCode();\n// Hash value of decimal 3.14159 is -1340954729;\n\nstring str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.GetHashCode();\n// Hash value of string \"Hello \u7b97\u6cd5\" is -586107568;\n\nobject[] arr = [12836, \"\u5c0f\u54c8\"];\nint hashTup = arr.GetHashCode();\n// Hash value of array [12836, \u5c0f\u54c8] is 42931033;\n\nListNode obj = new(0);\nint hashObj = obj.GetHashCode();\n// Hash value of ListNode object 0 is 39053774;\n
      built_in_hash.go
      // Go does not provide built-in hash code functions\n
      built_in_hash.swift
      let num = 3\nlet hashNum = num.hashValue\n// Hash value of integer 3 is 9047044699613009734\n\nlet bol = true\nlet hashBol = bol.hashValue\n// Hash value of boolean true is -4431640247352757451\n\nlet dec = 3.14159\nlet hashDec = dec.hashValue\n// Hash value of decimal 3.14159 is -2465384235396674631\n\nlet str = \"Hello \u7b97\u6cd5\"\nlet hashStr = str.hashValue\n// Hash value of string \"Hello \u7b97\u6cd5\" is -7850626797806988787\n\nlet arr = [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")]\nlet hashTup = arr.hashValue\n// Hash value of array [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")] is -2308633508154532996\n\nlet obj = ListNode(x: 0)\nlet hashObj = obj.hashValue\n// Hash value of ListNode object utils.ListNode is -2434780518035996159\n
      built_in_hash.js
      // JavaScript does not provide built-in hash code functions\n
      built_in_hash.ts
      // TypeScript does not provide built-in hash code functions\n
      built_in_hash.dart
      int num = 3;\nint hashNum = num.hashCode;\n// Hash value of integer 3 is 34803\n\nbool bol = true;\nint hashBol = bol.hashCode;\n// Hash value of boolean true is 1231\n\ndouble dec = 3.14159;\nint hashDec = dec.hashCode;\n// Hash value of decimal 3.14159 is 2570631074981783\n\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode;\n// Hash value of string \"Hello \u7b97\u6cd5\" is 468167534\n\nList arr = [12836, \"\u5c0f\u54c8\"];\nint hashArr = arr.hashCode;\n// Hash value of array [12836, \u5c0f\u54c8] is 976512528\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode;\n// Hash value of ListNode object Instance of 'ListNode' is 1033450432\n
      built_in_hash.rs
      use std::collections::hash_map::DefaultHasher;\nuse std::hash::{Hash, Hasher};\n\nlet num = 3;\nlet mut num_hasher = DefaultHasher::new();\nnum.hash(&mut num_hasher);\nlet hash_num = num_hasher.finish();\n// Hash value of integer 3 is 568126464209439262\n\nlet bol = true;\nlet mut bol_hasher = DefaultHasher::new();\nbol.hash(&mut bol_hasher);\nlet hash_bol = bol_hasher.finish();\n// Hash value of boolean true is 4952851536318644461\n\nlet dec: f32 = 3.14159;\nlet mut dec_hasher = DefaultHasher::new();\ndec.to_bits().hash(&mut dec_hasher);\nlet hash_dec = dec_hasher.finish();\n// Hash value of decimal 3.14159 is 2566941990314602357\n\nlet str = \"Hello \u7b97\u6cd5\";\nlet mut str_hasher = DefaultHasher::new();\nstr.hash(&mut str_hasher);\nlet hash_str = str_hasher.finish();\n// Hash value of string \"Hello \u7b97\u6cd5\" is 16092673739211250988\n\nlet arr = (&12836, &\"\u5c0f\u54c8\");\nlet mut tup_hasher = DefaultHasher::new();\narr.hash(&mut tup_hasher);\nlet hash_tup = tup_hasher.finish();\n// Hash value of tuple (12836, \"\u5c0f\u54c8\") is 1885128010422702749\n\nlet node = ListNode::new(42);\nlet mut hasher = DefaultHasher::new();\nnode.borrow().val.hash(&mut hasher);\nlet hash = hasher.finish();\n// Hash value of ListNode object RefCell { value: ListNode { val: 42, next: None } } is 15387811073369036852\n
      built_in_hash.c
      // C does not provide built-in hash code functions\n
      built_in_hash.kt
      val num = 3\nval hashNum = num.hashCode()\n// Hash value of integer 3 is 3\n\nval bol = true\nval hashBol = bol.hashCode()\n// Hash value of boolean true is 1231\n\nval dec = 3.14159\nval hashDec = dec.hashCode()\n// Hash value of decimal 3.14159 is -1340954729\n\nval str = \"Hello \u7b97\u6cd5\"\nval hashStr = str.hashCode()\n// Hash value of string \"Hello \u7b97\u6cd5\" is -727081396\n\nval arr = arrayOf<Any>(12836, \"\u5c0f\u54c8\")\nval hashTup = arr.hashCode()\n// Hash value of array [12836, \u5c0f\u54c8] is 189568618\n\nval obj = ListNode(0)\nval hashObj = obj.hashCode()\n// Hash value of ListNode object utils.ListNode@1d81eb93 is 495053715\n
      built_in_hash.rb
      num = 3\nhash_num = num.hash\n# Hash value of integer 3 is -4385856518450339636\n\nbol = true\nhash_bol = bol.hash\n# Hash value of boolean true is -1617938112149317027\n\ndec = 3.14159\nhash_dec = dec.hash\n# Hash value of decimal 3.14159 is -1479186995943067893\n\nstr = \"Hello \u7b97\u6cd5\"\nhash_str = str.hash\n# Hash value of string \"Hello \u7b97\u6cd5\" is -4075943250025831763\n\ntup = [12836, '\u5c0f\u54c8']\nhash_tup = tup.hash\n# Hash value of tuple (12836, '\u5c0f\u54c8') is 1999544809202288822\n\nobj = ListNode.new(0)\nhash_obj = obj.hash\n# Hash value of ListNode object #<ListNode:0x000078133140ab70> is 4302940560806366381\n
      Visualized Execution

      https://pythontutor.com/render.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20num%20%3D%203%0A%20%20%20%20hash_num%20%3D%20hash%28num%29%0A%20%20%20%20%23%20%E6%95%B4%E6%95%B0%203%20%E7%9A%84%E5%93%88%E5%B8%8C%E5%80%BC%E4%B8%BA%203%0A%0A%20%20%20%20bol%20%3D%20True%0A%20%20%20%20hash_bol%20%3D%20hash%28bol%29%0A%20%20%20%20%23%20%E5%B8%83%E5%B0%94%E9%87%8F%20True%20%E7%9A%84%E5%93%88%E5%B8%8C%E5%80%BC%E4%B8%BA%201%0A%0A%20%20%20%20dec%20%3D%203.14159%0A%20%20%20%20hash_dec%20%3D%20hash%28dec%29%0A%20%20%20%20%23%20%E5%B0%8F%E6%95%B0%203.14159%20%E7%9A%84%E5%93%88%E5%B8%8C%E5%80%BC%E4%B8%BA%20326484311674566659%0A%0A%20%20%20%20str%20%3D%20%22Hello%20%E7%AE%97%E6%B3%95%22%0A%20%20%20%20hash_str%20%3D%20hash%28str%29%0A%20%20%20%20%23%20%E5%AD%97%E7%AC%A6%E4%B8%B2%E2%80%9CHello%20%E7%AE%97%E6%B3%95%E2%80%9D%E7%9A%84%E5%93%88%E5%B8%8C%E5%80%BC%E4%B8%BA%204617003410720528961%0A%0A%20%20%20%20tup%20%3D%20%2812836,%20%22%E5%B0%8F%E5%93%88%22%29%0A%20%20%20%20hash_tup%20%3D%20hash%28tup%29%0A%20%20%20%20%23%20%E5%85%83%E7%BB%84%20%2812836,%20'%E5%B0%8F%E5%93%88'%29%20%E7%9A%84%E5%93%88%E5%B8%8C%E5%80%BC%E4%B8%BA%201029005403108185979%0A%0A%20%20%20%20obj%20%3D%20ListNode%280%29%0A%20%20%20%20hash_obj%20%3D%20hash%28obj%29%0A%20%20%20%20%23%20%E8%8A%82%E7%82%B9%E5%AF%B9%E8%B1%A1%20%3CListNode%20object%20at%200x1058fd810%3E%20%E7%9A%84%E5%93%88%E5%B8%8C%E5%80%BC%E4%B8%BA%20274267521&cumulative=false&curInstr=19&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

      In many programming languages, only immutable objects can serve as the key in a hash table. If we use a list (dynamic array) as a key, when the contents of the list change, its hash value also changes, and we would no longer be able to find the original value in the hash table.

      Although the member variables of a custom object (such as a linked list node) are mutable, it is hashable. This is because the hash value of an object is usually generated based on its memory address, and even if the contents of the object change, the memory address remains the same, so the hash value remains unchanged.

      You might have noticed that the hash values output in different consoles are different. This is because the Python interpreter adds a random salt to the string hash function each time it starts up. This approach effectively prevents HashDoS attacks and enhances the security of the hash algorithm.

      "},{"location":"chapter_hashing/hash_collision/","title":"6.2 \u00a0 Hash Collision","text":"

      The previous section mentioned that, in most cases, the input space of a hash function is much larger than the output space, so theoretically, hash collisions are inevitable. For example, if the input space is all integers and the output space is the array capacity size, then multiple integers will inevitably be mapped to the same bucket index.

      Hash collisions can lead to incorrect query results, severely impacting the usability of the hash table. To address this issue, whenever a hash collision occurs, we can perform hash table expansion until the collision disappears. This approach is simple, straightforward, and effective, but it is very inefficient because hash table expansion involves a large amount of data migration and hash value recalculation. To improve efficiency, we can adopt the following strategies:

      1. Improve the hash table data structure so that the hash table can function normally when hash collisions occur.
      2. Only expand when necessary, that is, only when hash collisions are severe.

      The main methods for improving the structure of hash tables include \"separate chaining\" and \"open addressing\".

      "},{"location":"chapter_hashing/hash_collision/#621-separate-chaining","title":"6.2.1 \u00a0 Separate Chaining","text":"

      In the original hash table, each bucket can store only one key-value pair. Separate chaining converts a single element into a linked list, treating key-value pairs as linked list nodes and storing all colliding key-value pairs in the same linked list. Figure 6-5 shows an example of a separate chaining hash table.

      Figure 6-5 \u00a0 Separate chaining hash table

      The operations of a hash table implemented with separate chaining have changed as follows:

      • Querying elements: Input key, obtain the bucket index through the hash function, then access the head node of the linked list, then traverse the linked list and compare key to find the target key-value pair.
      • Adding elements: First access the linked list head node through the hash function, then append the node (key-value pair) to the linked list.
      • Deleting elements: Access the head of the linked list based on the result of the hash function, then traverse the linked list to find the target node and delete it.

      Separate chaining has the following limitations:

      • Increased Space Usage: The linked list contains node pointers, which consume more memory space than arrays.
      • Reduced Query Efficiency: This is because linear traversal of the linked list is required to find the corresponding element.

      The code below provides a simple implementation of a separate chaining hash table, with two things to note:

      • Lists (dynamic arrays) are used instead of linked lists to simplify the code. In this setup, the hash table (array) contains multiple buckets, each of which is a list.
      • This implementation includes a hash table expansion method. When the load factor exceeds \\(\\frac{2}{3}\\), we expand the hash table to \\(2\\) times its original size.
      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map_chaining.py
      class HashMapChaining:\n    \"\"\"Hash table with separate chaining\"\"\"\n\n    def __init__(self):\n        \"\"\"Constructor\"\"\"\n        self.size = 0  # Number of key-value pairs\n        self.capacity = 4  # Hash table capacity\n        self.load_thres = 2.0 / 3.0  # Load factor threshold for triggering expansion\n        self.extend_ratio = 2  # Expansion multiplier\n        self.buckets = [[] for _ in range(self.capacity)]  # Bucket array\n\n    def hash_func(self, key: int) -> int:\n        \"\"\"Hash function\"\"\"\n        return key % self.capacity\n\n    def load_factor(self) -> float:\n        \"\"\"Load factor\"\"\"\n        return self.size / self.capacity\n\n    def get(self, key: int) -> str | None:\n        \"\"\"Query operation\"\"\"\n        index = self.hash_func(key)\n        bucket = self.buckets[index]\n        # Traverse bucket, if key is found, return corresponding val\n        for pair in bucket:\n            if pair.key == key:\n                return pair.val\n        # If key is not found, return None\n        return None\n\n    def put(self, key: int, val: str):\n        \"\"\"Add operation\"\"\"\n        # When load factor exceeds threshold, perform expansion\n        if self.load_factor() > self.load_thres:\n            self.extend()\n        index = self.hash_func(key)\n        bucket = self.buckets[index]\n        # Traverse bucket, if specified key is encountered, update corresponding val and return\n        for pair in bucket:\n            if pair.key == key:\n                pair.val = val\n                return\n        # If key does not exist, append key-value pair to the end\n        pair = Pair(key, val)\n        bucket.append(pair)\n        self.size += 1\n\n    def remove(self, key: int):\n        \"\"\"Remove operation\"\"\"\n        index = self.hash_func(key)\n        bucket = self.buckets[index]\n        # Traverse bucket and remove key-value pair from it\n        for pair in bucket:\n            if pair.key == key:\n                bucket.remove(pair)\n                self.size -= 1\n                break\n\n    def extend(self):\n        \"\"\"Expand hash table\"\"\"\n        # Temporarily store the original hash table\n        buckets = self.buckets\n        # Initialize expanded new hash table\n        self.capacity *= self.extend_ratio\n        self.buckets = [[] for _ in range(self.capacity)]\n        self.size = 0\n        # Move key-value pairs from original hash table to new hash table\n        for bucket in buckets:\n            for pair in bucket:\n                self.put(pair.key, pair.val)\n\n    def print(self):\n        \"\"\"Print hash table\"\"\"\n        for bucket in self.buckets:\n            res = []\n            for pair in bucket:\n                res.append(str(pair.key) + \" -> \" + pair.val)\n            print(res)\n
      hash_map_chaining.cpp
      /* Hash table with separate chaining */\nclass HashMapChaining {\n  private:\n    int size;                       // Number of key-value pairs\n    int capacity;                   // Hash table capacity\n    double loadThres;               // Load factor threshold for triggering expansion\n    int extendRatio;                // Expansion multiplier\n    vector<vector<Pair *>> buckets; // Bucket array\n\n  public:\n    /* Constructor */\n    HashMapChaining() : size(0), capacity(4), loadThres(2.0 / 3.0), extendRatio(2) {\n        buckets.resize(capacity);\n    }\n\n    /* Destructor */\n    ~HashMapChaining() {\n        for (auto &bucket : buckets) {\n            for (Pair *pair : bucket) {\n                // Free memory\n                delete pair;\n            }\n        }\n    }\n\n    /* Hash function */\n    int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* Load factor */\n    double loadFactor() {\n        return (double)size / (double)capacity;\n    }\n\n    /* Query operation */\n    string get(int key) {\n        int index = hashFunc(key);\n        // Traverse bucket, if key is found, return corresponding val\n        for (Pair *pair : buckets[index]) {\n            if (pair->key == key) {\n                return pair->val;\n            }\n        }\n        // Return empty string if key not found\n        return \"\";\n    }\n\n    /* Add operation */\n    void put(int key, string val) {\n        // When load factor exceeds threshold, perform expansion\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        int index = hashFunc(key);\n        // Traverse bucket, if specified key is encountered, update corresponding val and return\n        for (Pair *pair : buckets[index]) {\n            if (pair->key == key) {\n                pair->val = val;\n                return;\n            }\n        }\n        // If key does not exist, append key-value pair to the end\n        buckets[index].push_back(new Pair(key, val));\n        size++;\n    }\n\n    /* Remove operation */\n    void remove(int key) {\n        int index = hashFunc(key);\n        auto &bucket = buckets[index];\n        // Traverse bucket and remove key-value pair from it\n        for (int i = 0; i < bucket.size(); i++) {\n            if (bucket[i]->key == key) {\n                Pair *tmp = bucket[i];\n                bucket.erase(bucket.begin() + i); // Remove key-value pair from it\n                delete tmp;                       // Free memory\n                size--;\n                return;\n            }\n        }\n    }\n\n    /* Expand hash table */\n    void extend() {\n        // Temporarily store the original hash table\n        vector<vector<Pair *>> bucketsTmp = buckets;\n        // Initialize expanded new hash table\n        capacity *= extendRatio;\n        buckets.clear();\n        buckets.resize(capacity);\n        size = 0;\n        // Move key-value pairs from original hash table to new hash table\n        for (auto &bucket : bucketsTmp) {\n            for (Pair *pair : bucket) {\n                put(pair->key, pair->val);\n                // Free memory\n                delete pair;\n            }\n        }\n    }\n\n    /* Print hash table */\n    void print() {\n        for (auto &bucket : buckets) {\n            cout << \"[\";\n            for (Pair *pair : bucket) {\n                cout << pair->key << \" -> \" << pair->val << \", \";\n            }\n            cout << \"]\\n\";\n        }\n    }\n};\n
      hash_map_chaining.java
      /* Hash table with separate chaining */\nclass HashMapChaining {\n    int size; // Number of key-value pairs\n    int capacity; // Hash table capacity\n    double loadThres; // Load factor threshold for triggering expansion\n    int extendRatio; // Expansion multiplier\n    List<List<Pair>> buckets; // Bucket array\n\n    /* Constructor */\n    public HashMapChaining() {\n        size = 0;\n        capacity = 4;\n        loadThres = 2.0 / 3.0;\n        extendRatio = 2;\n        buckets = new ArrayList<>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.add(new ArrayList<>());\n        }\n    }\n\n    /* Hash function */\n    int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* Load factor */\n    double loadFactor() {\n        return (double) size / capacity;\n    }\n\n    /* Query operation */\n    String get(int key) {\n        int index = hashFunc(key);\n        List<Pair> bucket = buckets.get(index);\n        // Traverse bucket, if key is found, return corresponding val\n        for (Pair pair : bucket) {\n            if (pair.key == key) {\n                return pair.val;\n            }\n        }\n        // If key is not found, return null\n        return null;\n    }\n\n    /* Add operation */\n    void put(int key, String val) {\n        // When load factor exceeds threshold, perform expansion\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        int index = hashFunc(key);\n        List<Pair> bucket = buckets.get(index);\n        // Traverse bucket, if specified key is encountered, update corresponding val and return\n        for (Pair pair : bucket) {\n            if (pair.key == key) {\n                pair.val = val;\n                return;\n            }\n        }\n        // If key does not exist, append key-value pair to the end\n        Pair pair = new Pair(key, val);\n        bucket.add(pair);\n        size++;\n    }\n\n    /* Remove operation */\n    void remove(int key) {\n        int index = hashFunc(key);\n        List<Pair> bucket = buckets.get(index);\n        // Traverse bucket and remove key-value pair from it\n        for (Pair pair : bucket) {\n            if (pair.key == key) {\n                bucket.remove(pair);\n                size--;\n                break;\n            }\n        }\n    }\n\n    /* Expand hash table */\n    void extend() {\n        // Temporarily store the original hash table\n        List<List<Pair>> bucketsTmp = buckets;\n        // Initialize expanded new hash table\n        capacity *= extendRatio;\n        buckets = new ArrayList<>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.add(new ArrayList<>());\n        }\n        size = 0;\n        // Move key-value pairs from original hash table to new hash table\n        for (List<Pair> bucket : bucketsTmp) {\n            for (Pair pair : bucket) {\n                put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* Print hash table */\n    void print() {\n        for (List<Pair> bucket : buckets) {\n            List<String> res = new ArrayList<>();\n            for (Pair pair : bucket) {\n                res.add(pair.key + \" -> \" + pair.val);\n            }\n            System.out.println(res);\n        }\n    }\n}\n
      hash_map_chaining.cs
      /* Hash table with separate chaining */\nclass HashMapChaining {\n    int size; // Number of key-value pairs\n    int capacity; // Hash table capacity\n    double loadThres; // Load factor threshold for triggering expansion\n    int extendRatio; // Expansion multiplier\n    List<List<Pair>> buckets; // Bucket array\n\n    /* Constructor */\n    public HashMapChaining() {\n        size = 0;\n        capacity = 4;\n        loadThres = 2.0 / 3.0;\n        extendRatio = 2;\n        buckets = new List<List<Pair>>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.Add([]);\n        }\n    }\n\n    /* Hash function */\n    int HashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* Load factor */\n    double LoadFactor() {\n        return (double)size / capacity;\n    }\n\n    /* Query operation */\n    public string? Get(int key) {\n        int index = HashFunc(key);\n        // Traverse bucket, if key is found, return corresponding val\n        foreach (Pair pair in buckets[index]) {\n            if (pair.key == key) {\n                return pair.val;\n            }\n        }\n        // If key is not found, return null\n        return null;\n    }\n\n    /* Add operation */\n    public void Put(int key, string val) {\n        // When load factor exceeds threshold, perform expansion\n        if (LoadFactor() > loadThres) {\n            Extend();\n        }\n        int index = HashFunc(key);\n        // Traverse bucket, if specified key is encountered, update corresponding val and return\n        foreach (Pair pair in buckets[index]) {\n            if (pair.key == key) {\n                pair.val = val;\n                return;\n            }\n        }\n        // If key does not exist, append key-value pair to the end\n        buckets[index].Add(new Pair(key, val));\n        size++;\n    }\n\n    /* Remove operation */\n    public void Remove(int key) {\n        int index = HashFunc(key);\n        // Traverse bucket and remove key-value pair from it\n        foreach (Pair pair in buckets[index].ToList()) {\n            if (pair.key == key) {\n                buckets[index].Remove(pair);\n                size--;\n                break;\n            }\n        }\n    }\n\n    /* Expand hash table */\n    void Extend() {\n        // Temporarily store the original hash table\n        List<List<Pair>> bucketsTmp = buckets;\n        // Initialize expanded new hash table\n        capacity *= extendRatio;\n        buckets = new List<List<Pair>>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.Add([]);\n        }\n        size = 0;\n        // Move key-value pairs from original hash table to new hash table\n        foreach (List<Pair> bucket in bucketsTmp) {\n            foreach (Pair pair in bucket) {\n                Put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* Print hash table */\n    public void Print() {\n        foreach (List<Pair> bucket in buckets) {\n            List<string> res = [];\n            foreach (Pair pair in bucket) {\n                res.Add(pair.key + \" -> \" + pair.val);\n            }\n            foreach (string kv in res) {\n                Console.WriteLine(kv);\n            }\n        }\n    }\n}\n
      hash_map_chaining.go
      /* Hash table with separate chaining */\ntype hashMapChaining struct {\n    size        int      // Number of key-value pairs\n    capacity    int      // Hash table capacity\n    loadThres   float64  // Load factor threshold for triggering expansion\n    extendRatio int      // Expansion multiplier\n    buckets     [][]pair // Bucket array\n}\n\n/* Constructor */\nfunc newHashMapChaining() *hashMapChaining {\n    buckets := make([][]pair, 4)\n    for i := 0; i < 4; i++ {\n        buckets[i] = make([]pair, 0)\n    }\n    return &hashMapChaining{\n        size:        0,\n        capacity:    4,\n        loadThres:   2.0 / 3.0,\n        extendRatio: 2,\n        buckets:     buckets,\n    }\n}\n\n/* Hash function */\nfunc (m *hashMapChaining) hashFunc(key int) int {\n    return key % m.capacity\n}\n\n/* Load factor */\nfunc (m *hashMapChaining) loadFactor() float64 {\n    return float64(m.size) / float64(m.capacity)\n}\n\n/* Query operation */\nfunc (m *hashMapChaining) get(key int) string {\n    idx := m.hashFunc(key)\n    bucket := m.buckets[idx]\n    // Traverse bucket, if key is found, return corresponding val\n    for _, p := range bucket {\n        if p.key == key {\n            return p.val\n        }\n    }\n    // Return empty string if key not found\n    return \"\"\n}\n\n/* Add operation */\nfunc (m *hashMapChaining) put(key int, val string) {\n    // When load factor exceeds threshold, perform expansion\n    if m.loadFactor() > m.loadThres {\n        m.extend()\n    }\n    idx := m.hashFunc(key)\n    // Traverse bucket, if specified key is encountered, update corresponding val and return\n    for i := range m.buckets[idx] {\n        if m.buckets[idx][i].key == key {\n            m.buckets[idx][i].val = val\n            return\n        }\n    }\n    // If key does not exist, append key-value pair to the end\n    p := pair{\n        key: key,\n        val: val,\n    }\n    m.buckets[idx] = append(m.buckets[idx], p)\n    m.size += 1\n}\n\n/* Remove operation */\nfunc (m *hashMapChaining) remove(key int) {\n    idx := m.hashFunc(key)\n    // Traverse bucket and remove key-value pair from it\n    for i, p := range m.buckets[idx] {\n        if p.key == key {\n            // Slice deletion\n            m.buckets[idx] = append(m.buckets[idx][:i], m.buckets[idx][i+1:]...)\n            m.size -= 1\n            break\n        }\n    }\n}\n\n/* Expand hash table */\nfunc (m *hashMapChaining) extend() {\n    // Temporarily store the original hash table\n    tmpBuckets := make([][]pair, len(m.buckets))\n    for i := 0; i < len(m.buckets); i++ {\n        tmpBuckets[i] = make([]pair, len(m.buckets[i]))\n        copy(tmpBuckets[i], m.buckets[i])\n    }\n    // Initialize expanded new hash table\n    m.capacity *= m.extendRatio\n    m.buckets = make([][]pair, m.capacity)\n    for i := 0; i < m.capacity; i++ {\n        m.buckets[i] = make([]pair, 0)\n    }\n    m.size = 0\n    // Move key-value pairs from original hash table to new hash table\n    for _, bucket := range tmpBuckets {\n        for _, p := range bucket {\n            m.put(p.key, p.val)\n        }\n    }\n}\n\n/* Print hash table */\nfunc (m *hashMapChaining) print() {\n    var builder strings.Builder\n\n    for _, bucket := range m.buckets {\n        builder.WriteString(\"[\")\n        for _, p := range bucket {\n            builder.WriteString(strconv.Itoa(p.key) + \" -> \" + p.val + \" \")\n        }\n        builder.WriteString(\"]\")\n        fmt.Println(builder.String())\n        builder.Reset()\n    }\n}\n
      hash_map_chaining.swift
      /* Hash table with separate chaining */\nclass HashMapChaining {\n    var size: Int // Number of key-value pairs\n    var capacity: Int // Hash table capacity\n    var loadThres: Double // Load factor threshold for triggering expansion\n    var extendRatio: Int // Expansion multiplier\n    var buckets: [[Pair]] // Bucket array\n\n    /* Constructor */\n    init() {\n        size = 0\n        capacity = 4\n        loadThres = 2.0 / 3.0\n        extendRatio = 2\n        buckets = Array(repeating: [], count: capacity)\n    }\n\n    /* Hash function */\n    func hashFunc(key: Int) -> Int {\n        key % capacity\n    }\n\n    /* Load factor */\n    func loadFactor() -> Double {\n        Double(size) / Double(capacity)\n    }\n\n    /* Query operation */\n    func get(key: Int) -> String? {\n        let index = hashFunc(key: key)\n        let bucket = buckets[index]\n        // Traverse bucket, if key is found, return corresponding val\n        for pair in bucket {\n            if pair.key == key {\n                return pair.val\n            }\n        }\n        // Return nil if key not found\n        return nil\n    }\n\n    /* Add operation */\n    func put(key: Int, val: String) {\n        // When load factor exceeds threshold, perform expansion\n        if loadFactor() > loadThres {\n            extend()\n        }\n        let index = hashFunc(key: key)\n        let bucket = buckets[index]\n        // Traverse bucket, if specified key is encountered, update corresponding val and return\n        for pair in bucket {\n            if pair.key == key {\n                pair.val = val\n                return\n            }\n        }\n        // If key does not exist, append key-value pair to the end\n        let pair = Pair(key: key, val: val)\n        buckets[index].append(pair)\n        size += 1\n    }\n\n    /* Remove operation */\n    func remove(key: Int) {\n        let index = hashFunc(key: key)\n        let bucket = buckets[index]\n        // Traverse bucket and remove key-value pair from it\n        for (pairIndex, pair) in bucket.enumerated() {\n            if pair.key == key {\n                buckets[index].remove(at: pairIndex)\n                size -= 1\n                break\n            }\n        }\n    }\n\n    /* Expand hash table */\n    func extend() {\n        // Temporarily store the original hash table\n        let bucketsTmp = buckets\n        // Initialize expanded new hash table\n        capacity *= extendRatio\n        buckets = Array(repeating: [], count: capacity)\n        size = 0\n        // Move key-value pairs from original hash table to new hash table\n        for bucket in bucketsTmp {\n            for pair in bucket {\n                put(key: pair.key, val: pair.val)\n            }\n        }\n    }\n\n    /* Print hash table */\n    func print() {\n        for bucket in buckets {\n            let res = bucket.map { \"\\($0.key) -> \\($0.val)\" }\n            Swift.print(res)\n        }\n    }\n}\n
      hash_map_chaining.js
      /* Hash table with separate chaining */\nclass HashMapChaining {\n    #size; // Number of key-value pairs\n    #capacity; // Hash table capacity\n    #loadThres; // Load factor threshold for triggering expansion\n    #extendRatio; // Expansion multiplier\n    #buckets; // Bucket array\n\n    /* Constructor */\n    constructor() {\n        this.#size = 0;\n        this.#capacity = 4;\n        this.#loadThres = 2.0 / 3.0;\n        this.#extendRatio = 2;\n        this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n    }\n\n    /* Hash function */\n    #hashFunc(key) {\n        return key % this.#capacity;\n    }\n\n    /* Load factor */\n    #loadFactor() {\n        return this.#size / this.#capacity;\n    }\n\n    /* Query operation */\n    get(key) {\n        const index = this.#hashFunc(key);\n        const bucket = this.#buckets[index];\n        // Traverse bucket, if key is found, return corresponding val\n        for (const pair of bucket) {\n            if (pair.key === key) {\n                return pair.val;\n            }\n        }\n        // If key is not found, return null\n        return null;\n    }\n\n    /* Add operation */\n    put(key, val) {\n        // When load factor exceeds threshold, perform expansion\n        if (this.#loadFactor() > this.#loadThres) {\n            this.#extend();\n        }\n        const index = this.#hashFunc(key);\n        const bucket = this.#buckets[index];\n        // Traverse bucket, if specified key is encountered, update corresponding val and return\n        for (const pair of bucket) {\n            if (pair.key === key) {\n                pair.val = val;\n                return;\n            }\n        }\n        // If key does not exist, append key-value pair to the end\n        const pair = new Pair(key, val);\n        bucket.push(pair);\n        this.#size++;\n    }\n\n    /* Remove operation */\n    remove(key) {\n        const index = this.#hashFunc(key);\n        let bucket = this.#buckets[index];\n        // Traverse bucket and remove key-value pair from it\n        for (let i = 0; i < bucket.length; i++) {\n            if (bucket[i].key === key) {\n                bucket.splice(i, 1);\n                this.#size--;\n                break;\n            }\n        }\n    }\n\n    /* Expand hash table */\n    #extend() {\n        // Temporarily store the original hash table\n        const bucketsTmp = this.#buckets;\n        // Initialize expanded new hash table\n        this.#capacity *= this.#extendRatio;\n        this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n        this.#size = 0;\n        // Move key-value pairs from original hash table to new hash table\n        for (const bucket of bucketsTmp) {\n            for (const pair of bucket) {\n                this.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* Print hash table */\n    print() {\n        for (const bucket of this.#buckets) {\n            let res = [];\n            for (const pair of bucket) {\n                res.push(pair.key + ' -> ' + pair.val);\n            }\n            console.log(res);\n        }\n    }\n}\n
      hash_map_chaining.ts
      /* Hash table with separate chaining */\nclass HashMapChaining {\n    #size: number; // Number of key-value pairs\n    #capacity: number; // Hash table capacity\n    #loadThres: number; // Load factor threshold for triggering expansion\n    #extendRatio: number; // Expansion multiplier\n    #buckets: Pair[][]; // Bucket array\n\n    /* Constructor */\n    constructor() {\n        this.#size = 0;\n        this.#capacity = 4;\n        this.#loadThres = 2.0 / 3.0;\n        this.#extendRatio = 2;\n        this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n    }\n\n    /* Hash function */\n    #hashFunc(key: number): number {\n        return key % this.#capacity;\n    }\n\n    /* Load factor */\n    #loadFactor(): number {\n        return this.#size / this.#capacity;\n    }\n\n    /* Query operation */\n    get(key: number): string | null {\n        const index = this.#hashFunc(key);\n        const bucket = this.#buckets[index];\n        // Traverse bucket, if key is found, return corresponding val\n        for (const pair of bucket) {\n            if (pair.key === key) {\n                return pair.val;\n            }\n        }\n        // If key is not found, return null\n        return null;\n    }\n\n    /* Add operation */\n    put(key: number, val: string): void {\n        // When load factor exceeds threshold, perform expansion\n        if (this.#loadFactor() > this.#loadThres) {\n            this.#extend();\n        }\n        const index = this.#hashFunc(key);\n        const bucket = this.#buckets[index];\n        // Traverse bucket, if specified key is encountered, update corresponding val and return\n        for (const pair of bucket) {\n            if (pair.key === key) {\n                pair.val = val;\n                return;\n            }\n        }\n        // If key does not exist, append key-value pair to the end\n        const pair = new Pair(key, val);\n        bucket.push(pair);\n        this.#size++;\n    }\n\n    /* Remove operation */\n    remove(key: number): void {\n        const index = this.#hashFunc(key);\n        let bucket = this.#buckets[index];\n        // Traverse bucket and remove key-value pair from it\n        for (let i = 0; i < bucket.length; i++) {\n            if (bucket[i].key === key) {\n                bucket.splice(i, 1);\n                this.#size--;\n                break;\n            }\n        }\n    }\n\n    /* Expand hash table */\n    #extend(): void {\n        // Temporarily store the original hash table\n        const bucketsTmp = this.#buckets;\n        // Initialize expanded new hash table\n        this.#capacity *= this.#extendRatio;\n        this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n        this.#size = 0;\n        // Move key-value pairs from original hash table to new hash table\n        for (const bucket of bucketsTmp) {\n            for (const pair of bucket) {\n                this.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* Print hash table */\n    print(): void {\n        for (const bucket of this.#buckets) {\n            let res = [];\n            for (const pair of bucket) {\n                res.push(pair.key + ' -> ' + pair.val);\n            }\n            console.log(res);\n        }\n    }\n}\n
      hash_map_chaining.dart
      /* Hash table with separate chaining */\nclass HashMapChaining {\n  late int size; // Number of key-value pairs\n  late int capacity; // Hash table capacity\n  late double loadThres; // Load factor threshold for triggering expansion\n  late int extendRatio; // Expansion multiplier\n  late List<List<Pair>> buckets; // Bucket array\n\n  /* Constructor */\n  HashMapChaining() {\n    size = 0;\n    capacity = 4;\n    loadThres = 2.0 / 3.0;\n    extendRatio = 2;\n    buckets = List.generate(capacity, (_) => []);\n  }\n\n  /* Hash function */\n  int hashFunc(int key) {\n    return key % capacity;\n  }\n\n  /* Load factor */\n  double loadFactor() {\n    return size / capacity;\n  }\n\n  /* Query operation */\n  String? get(int key) {\n    int index = hashFunc(key);\n    List<Pair> bucket = buckets[index];\n    // Traverse bucket, if key is found, return corresponding val\n    for (Pair pair in bucket) {\n      if (pair.key == key) {\n        return pair.val;\n      }\n    }\n    // If key is not found, return null\n    return null;\n  }\n\n  /* Add operation */\n  void put(int key, String val) {\n    // When load factor exceeds threshold, perform expansion\n    if (loadFactor() > loadThres) {\n      extend();\n    }\n    int index = hashFunc(key);\n    List<Pair> bucket = buckets[index];\n    // Traverse bucket, if specified key is encountered, update corresponding val and return\n    for (Pair pair in bucket) {\n      if (pair.key == key) {\n        pair.val = val;\n        return;\n      }\n    }\n    // If key does not exist, append key-value pair to the end\n    Pair pair = Pair(key, val);\n    bucket.add(pair);\n    size++;\n  }\n\n  /* Remove operation */\n  void remove(int key) {\n    int index = hashFunc(key);\n    List<Pair> bucket = buckets[index];\n    // Traverse bucket and remove key-value pair from it\n    for (Pair pair in bucket) {\n      if (pair.key == key) {\n        bucket.remove(pair);\n        size--;\n        break;\n      }\n    }\n  }\n\n  /* Expand hash table */\n  void extend() {\n    // Temporarily store the original hash table\n    List<List<Pair>> bucketsTmp = buckets;\n    // Initialize expanded new hash table\n    capacity *= extendRatio;\n    buckets = List.generate(capacity, (_) => []);\n    size = 0;\n    // Move key-value pairs from original hash table to new hash table\n    for (List<Pair> bucket in bucketsTmp) {\n      for (Pair pair in bucket) {\n        put(pair.key, pair.val);\n      }\n    }\n  }\n\n  /* Print hash table */\n  void printHashMap() {\n    for (List<Pair> bucket in buckets) {\n      List<String> res = [];\n      for (Pair pair in bucket) {\n        res.add(\"${pair.key} -> ${pair.val}\");\n      }\n      print(res);\n    }\n  }\n}\n
      hash_map_chaining.rs
      /* Hash table with separate chaining */\nstruct HashMapChaining {\n    size: usize,\n    capacity: usize,\n    load_thres: f32,\n    extend_ratio: usize,\n    buckets: Vec<Vec<Pair>>,\n}\n\nimpl HashMapChaining {\n    /* Constructor */\n    fn new() -> Self {\n        Self {\n            size: 0,\n            capacity: 4,\n            load_thres: 2.0 / 3.0,\n            extend_ratio: 2,\n            buckets: vec![vec![]; 4],\n        }\n    }\n\n    /* Hash function */\n    fn hash_func(&self, key: i32) -> usize {\n        key as usize % self.capacity\n    }\n\n    /* Load factor */\n    fn load_factor(&self) -> f32 {\n        self.size as f32 / self.capacity as f32\n    }\n\n    /* Remove operation */\n    fn remove(&mut self, key: i32) -> Option<String> {\n        let index = self.hash_func(key);\n\n        // Traverse bucket and remove key-value pair from it\n        for (i, p) in self.buckets[index].iter_mut().enumerate() {\n            if p.key == key {\n                let pair = self.buckets[index].remove(i);\n                self.size -= 1;\n                return Some(pair.val);\n            }\n        }\n\n        // If key is not found, return None\n        None\n    }\n\n    /* Expand hash table */\n    fn extend(&mut self) {\n        // Temporarily store the original hash table\n        let buckets_tmp = std::mem::take(&mut self.buckets);\n\n        // Initialize expanded new hash table\n        self.capacity *= self.extend_ratio;\n        self.buckets = vec![Vec::new(); self.capacity as usize];\n        self.size = 0;\n\n        // Move key-value pairs from original hash table to new hash table\n        for bucket in buckets_tmp {\n            for pair in bucket {\n                self.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* Print hash table */\n    fn print(&self) {\n        for bucket in &self.buckets {\n            let mut res = Vec::new();\n            for pair in bucket {\n                res.push(format!(\"{} -> {}\", pair.key, pair.val));\n            }\n            println!(\"{:?}\", res);\n        }\n    }\n\n    /* Add operation */\n    fn put(&mut self, key: i32, val: String) {\n        // When load factor exceeds threshold, perform expansion\n        if self.load_factor() > self.load_thres {\n            self.extend();\n        }\n\n        let index = self.hash_func(key);\n\n        // Traverse bucket, if specified key is encountered, update corresponding val and return\n        for pair in self.buckets[index].iter_mut() {\n            if pair.key == key {\n                pair.val = val;\n                return;\n            }\n        }\n\n        // If key does not exist, append key-value pair to the end\n        let pair = Pair { key, val };\n        self.buckets[index].push(pair);\n        self.size += 1;\n    }\n\n    /* Query operation */\n    fn get(&self, key: i32) -> Option<&str> {\n        let index = self.hash_func(key);\n\n        // Traverse bucket, if key is found, return corresponding val\n        for pair in self.buckets[index].iter() {\n            if pair.key == key {\n                return Some(&pair.val);\n            }\n        }\n\n        // If key is not found, return None\n        None\n    }\n}\n
      hash_map_chaining.c
      /* Linked list node */\ntypedef struct Node {\n    Pair *pair;\n    struct Node *next;\n} Node;\n\n/* Hash table with separate chaining */\ntypedef struct {\n    int size;         // Number of key-value pairs\n    int capacity;     // Hash table capacity\n    double loadThres; // Load factor threshold for triggering expansion\n    int extendRatio;  // Expansion multiplier\n    Node **buckets;   // Bucket array\n} HashMapChaining;\n\n/* Constructor */\nHashMapChaining *newHashMapChaining() {\n    HashMapChaining *hashMap = (HashMapChaining *)malloc(sizeof(HashMapChaining));\n    hashMap->size = 0;\n    hashMap->capacity = 4;\n    hashMap->loadThres = 2.0 / 3.0;\n    hashMap->extendRatio = 2;\n    hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));\n    for (int i = 0; i < hashMap->capacity; i++) {\n        hashMap->buckets[i] = NULL;\n    }\n    return hashMap;\n}\n\n/* Destructor */\nvoid delHashMapChaining(HashMapChaining *hashMap) {\n    for (int i = 0; i < hashMap->capacity; i++) {\n        Node *cur = hashMap->buckets[i];\n        while (cur) {\n            Node *tmp = cur;\n            cur = cur->next;\n            free(tmp->pair);\n            free(tmp);\n        }\n    }\n    free(hashMap->buckets);\n    free(hashMap);\n}\n\n/* Hash function */\nint hashFunc(HashMapChaining *hashMap, int key) {\n    return key % hashMap->capacity;\n}\n\n/* Load factor */\ndouble loadFactor(HashMapChaining *hashMap) {\n    return (double)hashMap->size / (double)hashMap->capacity;\n}\n\n/* Query operation */\nchar *get(HashMapChaining *hashMap, int key) {\n    int index = hashFunc(hashMap, key);\n    // Traverse bucket, if key is found, return corresponding val\n    Node *cur = hashMap->buckets[index];\n    while (cur) {\n        if (cur->pair->key == key) {\n            return cur->pair->val;\n        }\n        cur = cur->next;\n    }\n    return \"\"; // Return empty string if key not found\n}\n\n/* Add operation */\nvoid put(HashMapChaining *hashMap, int key, const char *val) {\n    // When load factor exceeds threshold, perform expansion\n    if (loadFactor(hashMap) > hashMap->loadThres) {\n        extend(hashMap);\n    }\n    int index = hashFunc(hashMap, key);\n    // Traverse bucket, if specified key is encountered, update corresponding val and return\n    Node *cur = hashMap->buckets[index];\n    while (cur) {\n        if (cur->pair->key == key) {\n            strcpy(cur->pair->val, val); // If specified key is found, update corresponding val and return\n            return;\n        }\n        cur = cur->next;\n    }\n    // If key not found, add key-value pair to list head\n    Pair *newPair = (Pair *)malloc(sizeof(Pair));\n    newPair->key = key;\n    strcpy(newPair->val, val);\n    Node *newNode = (Node *)malloc(sizeof(Node));\n    newNode->pair = newPair;\n    newNode->next = hashMap->buckets[index];\n    hashMap->buckets[index] = newNode;\n    hashMap->size++;\n}\n\n/* Expand hash table */\nvoid extend(HashMapChaining *hashMap) {\n    // Temporarily store the original hash table\n    int oldCapacity = hashMap->capacity;\n    Node **oldBuckets = hashMap->buckets;\n    // Initialize expanded new hash table\n    hashMap->capacity *= hashMap->extendRatio;\n    hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));\n    for (int i = 0; i < hashMap->capacity; i++) {\n        hashMap->buckets[i] = NULL;\n    }\n    hashMap->size = 0;\n    // Move key-value pairs from original hash table to new hash table\n    for (int i = 0; i < oldCapacity; i++) {\n        Node *cur = oldBuckets[i];\n        while (cur) {\n            put(hashMap, cur->pair->key, cur->pair->val);\n            Node *temp = cur;\n            cur = cur->next;\n            // Free memory\n            free(temp->pair);\n            free(temp);\n        }\n    }\n\n    free(oldBuckets);\n}\n\n/* Remove operation */\nvoid removeItem(HashMapChaining *hashMap, int key) {\n    int index = hashFunc(hashMap, key);\n    Node *cur = hashMap->buckets[index];\n    Node *pre = NULL;\n    while (cur) {\n        if (cur->pair->key == key) {\n            // Remove key-value pair from it\n            if (pre) {\n                pre->next = cur->next;\n            } else {\n                hashMap->buckets[index] = cur->next;\n            }\n            // Free memory\n            free(cur->pair);\n            free(cur);\n            hashMap->size--;\n            return;\n        }\n        pre = cur;\n        cur = cur->next;\n    }\n}\n\n/* Print hash table */\nvoid print(HashMapChaining *hashMap) {\n    for (int i = 0; i < hashMap->capacity; i++) {\n        Node *cur = hashMap->buckets[i];\n        printf(\"[\");\n        while (cur) {\n            printf(\"%d -> %s, \", cur->pair->key, cur->pair->val);\n            cur = cur->next;\n        }\n        printf(\"]\\n\");\n    }\n}\n
      hash_map_chaining.kt
      /* Hash table with separate chaining */\nclass HashMapChaining {\n    var size: Int // Number of key-value pairs\n    var capacity: Int // Hash table capacity\n    val loadThres: Double // Load factor threshold for triggering expansion\n    val extendRatio: Int // Expansion multiplier\n    var buckets: MutableList<MutableList<Pair>> // Bucket array\n\n    /* Constructor */\n    init {\n        size = 0\n        capacity = 4\n        loadThres = 2.0 / 3.0\n        extendRatio = 2\n        buckets = mutableListOf()\n        for (i in 0..<capacity) {\n            buckets.add(mutableListOf())\n        }\n    }\n\n    /* Hash function */\n    fun hashFunc(key: Int): Int {\n        return key % capacity\n    }\n\n    /* Load factor */\n    fun loadFactor(): Double {\n        return (size / capacity).toDouble()\n    }\n\n    /* Query operation */\n    fun get(key: Int): String? {\n        val index = hashFunc(key)\n        val bucket = buckets[index]\n        // Traverse bucket, if key is found, return corresponding val\n        for (pair in bucket) {\n            if (pair.key == key) return pair._val\n        }\n        // If key is not found, return null\n        return null\n    }\n\n    /* Add operation */\n    fun put(key: Int, _val: String) {\n        // When load factor exceeds threshold, perform expansion\n        if (loadFactor() > loadThres) {\n            extend()\n        }\n        val index = hashFunc(key)\n        val bucket = buckets[index]\n        // Traverse bucket, if specified key is encountered, update corresponding val and return\n        for (pair in bucket) {\n            if (pair.key == key) {\n                pair._val = _val\n                return\n            }\n        }\n        // If key does not exist, append key-value pair to the end\n        val pair = Pair(key, _val)\n        bucket.add(pair)\n        size++\n    }\n\n    /* Remove operation */\n    fun remove(key: Int) {\n        val index = hashFunc(key)\n        val bucket = buckets[index]\n        // Traverse bucket and remove key-value pair from it\n        for (pair in bucket) {\n            if (pair.key == key) {\n                bucket.remove(pair)\n                size--\n                break\n            }\n        }\n    }\n\n    /* Expand hash table */\n    fun extend() {\n        // Temporarily store the original hash table\n        val bucketsTmp = buckets\n        // Initialize expanded new hash table\n        capacity *= extendRatio\n        // mutablelist has no fixed size\n        buckets = mutableListOf()\n        for (i in 0..<capacity) {\n            buckets.add(mutableListOf())\n        }\n        size = 0\n        // Move key-value pairs from original hash table to new hash table\n        for (bucket in bucketsTmp) {\n            for (pair in bucket) {\n                put(pair.key, pair._val)\n            }\n        }\n    }\n\n    /* Print hash table */\n    fun print() {\n        for (bucket in buckets) {\n            val res = mutableListOf<String>()\n            for (pair in bucket) {\n                val k = pair.key\n                val v = pair._val\n                res.add(\"$k -> $v\")\n            }\n            println(res)\n        }\n    }\n}\n
      hash_map_chaining.rb
      ### Hash map with chaining ###\nclass HashMapChaining\n  ### Constructor ###\n  def initialize\n    @size = 0 # Number of key-value pairs\n    @capacity = 4 # Hash table capacity\n    @load_thres = 2.0 / 3.0 # Load factor threshold for triggering expansion\n    @extend_ratio = 2 # Expansion multiplier\n    @buckets = Array.new(@capacity) { [] } # Bucket array\n  end\n\n  ### Hash function ###\n  def hash_func(key)\n    key % @capacity\n  end\n\n  ### Load factor ###\n  def load_factor\n    @size / @capacity\n  end\n\n  ### Query operation ###\n  def get(key)\n    index = hash_func(key)\n    bucket = @buckets[index]\n    # Traverse bucket, if key is found, return corresponding val\n    for pair in bucket\n      return pair.val if pair.key == key\n    end\n    # Return nil if key not found\n    nil\n  end\n\n  ### Add operation ###\n  def put(key, val)\n    # When load factor exceeds threshold, perform expansion\n    extend if load_factor > @load_thres\n    index = hash_func(key)\n    bucket = @buckets[index]\n    # Traverse bucket, if specified key is encountered, update corresponding val and return\n    for pair in bucket\n      if pair.key == key\n        pair.val = val\n        return\n      end\n    end\n    # If key does not exist, append key-value pair to the end\n    pair = Pair.new(key, val)\n    bucket << pair\n    @size += 1\n  end\n\n  ### Delete operation ###\n  def remove(key)\n    index = hash_func(key)\n    bucket = @buckets[index]\n    # Traverse bucket and remove key-value pair from it\n    for pair in bucket\n      if pair.key == key\n        bucket.delete(pair)\n        @size -= 1\n        break\n      end\n    end\n  end\n\n  ### Expand hash table ###\n  def extend\n    # Temporarily store original hash table\n    buckets = @buckets\n    # Initialize expanded new hash table\n    @capacity *= @extend_ratio\n    @buckets = Array.new(@capacity) { [] }\n    @size = 0\n    # Move key-value pairs from original hash table to new hash table\n    for bucket in buckets\n      for pair in bucket\n        put(pair.key, pair.val)\n      end\n    end\n  end\n\n  ### Print hash table ###\n  def print\n    for bucket in @buckets\n      res = []\n      for pair in bucket\n        res << \"#{pair.key} -> #{pair.val}\"\n      end\n      pp res\n    end\n  end\nend\n

      It's worth noting that when the linked list is very long, the query efficiency \\(O(n)\\) is poor. In this case, the list can be converted to an \"AVL tree\" or \"Red-Black tree\" to optimize the time complexity of the query operation to \\(O(\\log n)\\).

      "},{"location":"chapter_hashing/hash_collision/#622-open-addressing","title":"6.2.2 \u00a0 Open Addressing","text":"

      Open addressing does not introduce additional data structures but instead handles hash collisions through \"multiple probes\". The probing methods mainly include linear probing, quadratic probing, and double hashing.

      Let's use linear probing as an example to introduce the mechanism of open addressing hash tables.

      "},{"location":"chapter_hashing/hash_collision/#1-linear-probing","title":"1. \u00a0 Linear Probing","text":"

      Linear probing uses a fixed-step linear search for probing, and its operation method differs from ordinary hash tables.

      • Inserting elements: Calculate the bucket index using the hash function. If the bucket already contains an element, linearly traverse forward from the conflict position (usually with a step size of \\(1\\)) until an empty bucket is found, then insert the element.
      • Searching for elements: If a hash collision is encountered, use the same step size to linearly traverse forward until the corresponding element is found and return value; if an empty bucket is encountered, it means the target element is not in the hash table, so return None.

      Figure 6-6 shows the distribution of key-value pairs in an open addressing (linear probing) hash table. According to this hash function, keys with the same last two digits will be mapped to the same bucket. Through linear probing, they are stored sequentially in that bucket and the buckets below it.

      Figure 6-6 \u00a0 Distribution of key-value pairs in open addressing (linear probing) hash table

      However, linear probing is prone to create \"clustering\". Specifically, the longer the continuously occupied positions in the array, the greater the probability of hash collisions occurring in these continuous positions, further promoting clustering growth at that position, forming a vicious cycle, and ultimately leading to degraded efficiency of insertion, deletion, query, and update operations.

      It's important to note that we cannot directly delete elements in an open addressing hash table. Deleting an element creates an empty bucket None in the array. When searching for elements, if linear probing encounters this empty bucket, it will return, making the elements below this empty bucket inaccessible. The program may incorrectly assume these elements do not exist, as shown in Figure 6-7.

      Figure 6-7 \u00a0 Query issues caused by deletion in open addressing

      To solve this problem, we can adopt the lazy deletion mechanism: instead of directly removing elements from the hash table, use a constant TOMBSTONE to mark the bucket. In this mechanism, both None and TOMBSTONE represent empty buckets and can hold key-value pairs. However, when linear probing encounters TOMBSTONE, it should continue traversing since there may still be key-value pairs below it.

      However, lazy deletion may accelerate the performance degradation of the hash table. Every deletion operation produces a deletion mark, and as TOMBSTONE increases, the search time will also increase because linear probing may need to skip multiple TOMBSTONE to find the target element.

      To address this, consider recording the index of the first encountered TOMBSTONE during linear probing and swapping the searched target element with that TOMBSTONE. The benefit of doing this is that each time an element is queried or added, the element will be moved to a bucket closer to its ideal position (the starting point of probing), thereby optimizing query efficiency.

      The code below implements an open addressing (linear probing) hash table with lazy deletion. To make better use of the hash table space, we treat the hash table as a \"circular array\". When going beyond the end of the array, we return to the beginning and continue traversing.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map_open_addressing.py
      class HashMapOpenAddressing:\n    \"\"\"Hash table with open addressing\"\"\"\n\n    def __init__(self):\n        \"\"\"Constructor\"\"\"\n        self.size = 0  # Number of key-value pairs\n        self.capacity = 4  # Hash table capacity\n        self.load_thres = 2.0 / 3.0  # Load factor threshold for triggering expansion\n        self.extend_ratio = 2  # Expansion multiplier\n        self.buckets: list[Pair | None] = [None] * self.capacity  # Bucket array\n        self.TOMBSTONE = Pair(-1, \"-1\")  # Removal marker\n\n    def hash_func(self, key: int) -> int:\n        \"\"\"Hash function\"\"\"\n        return key % self.capacity\n\n    def load_factor(self) -> float:\n        \"\"\"Load factor\"\"\"\n        return self.size / self.capacity\n\n    def find_bucket(self, key: int) -> int:\n        \"\"\"Search for bucket index corresponding to key\"\"\"\n        index = self.hash_func(key)\n        first_tombstone = -1\n        # Linear probing, break when encountering an empty bucket\n        while self.buckets[index] is not None:\n            # If key is encountered, return the corresponding bucket index\n            if self.buckets[index].key == key:\n                # If a removal marker was encountered before, move the key-value pair to that index\n                if first_tombstone != -1:\n                    self.buckets[first_tombstone] = self.buckets[index]\n                    self.buckets[index] = self.TOMBSTONE\n                    return first_tombstone  # Return the moved bucket index\n                return index  # Return bucket index\n            # Record the first removal marker encountered\n            if first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE:\n                first_tombstone = index\n            # Calculate bucket index, wrap around to the head if past the tail\n            index = (index + 1) % self.capacity\n        # If key does not exist, return the index for insertion\n        return index if first_tombstone == -1 else first_tombstone\n\n    def get(self, key: int) -> str:\n        \"\"\"Query operation\"\"\"\n        # Search for bucket index corresponding to key\n        index = self.find_bucket(key)\n        # If key-value pair is found, return corresponding val\n        if self.buckets[index] not in [None, self.TOMBSTONE]:\n            return self.buckets[index].val\n        # If key-value pair does not exist, return None\n        return None\n\n    def put(self, key: int, val: str):\n        \"\"\"Add operation\"\"\"\n        # When load factor exceeds threshold, perform expansion\n        if self.load_factor() > self.load_thres:\n            self.extend()\n        # Search for bucket index corresponding to key\n        index = self.find_bucket(key)\n        # If key-value pair is found, overwrite val and return\n        if self.buckets[index] not in [None, self.TOMBSTONE]:\n            self.buckets[index].val = val\n            return\n        # If key-value pair does not exist, add the key-value pair\n        self.buckets[index] = Pair(key, val)\n        self.size += 1\n\n    def remove(self, key: int):\n        \"\"\"Remove operation\"\"\"\n        # Search for bucket index corresponding to key\n        index = self.find_bucket(key)\n        # If key-value pair is found, overwrite it with removal marker\n        if self.buckets[index] not in [None, self.TOMBSTONE]:\n            self.buckets[index] = self.TOMBSTONE\n            self.size -= 1\n\n    def extend(self):\n        \"\"\"Expand hash table\"\"\"\n        # Temporarily store the original hash table\n        buckets_tmp = self.buckets\n        # Initialize expanded new hash table\n        self.capacity *= self.extend_ratio\n        self.buckets = [None] * self.capacity\n        self.size = 0\n        # Move key-value pairs from original hash table to new hash table\n        for pair in buckets_tmp:\n            if pair not in [None, self.TOMBSTONE]:\n                self.put(pair.key, pair.val)\n\n    def print(self):\n        \"\"\"Print hash table\"\"\"\n        for pair in self.buckets:\n            if pair is None:\n                print(\"None\")\n            elif pair is self.TOMBSTONE:\n                print(\"TOMBSTONE\")\n            else:\n                print(pair.key, \"->\", pair.val)\n
      hash_map_open_addressing.cpp
      /* Hash table with open addressing */\nclass HashMapOpenAddressing {\n  private:\n    int size;                             // Number of key-value pairs\n    int capacity = 4;                     // Hash table capacity\n    const double loadThres = 2.0 / 3.0;     // Load factor threshold for triggering expansion\n    const int extendRatio = 2;            // Expansion multiplier\n    vector<Pair *> buckets;               // Bucket array\n    Pair *TOMBSTONE = new Pair(-1, \"-1\"); // Removal marker\n\n  public:\n    /* Constructor */\n    HashMapOpenAddressing() : size(0), buckets(capacity, nullptr) {\n    }\n\n    /* Destructor */\n    ~HashMapOpenAddressing() {\n        for (Pair *pair : buckets) {\n            if (pair != nullptr && pair != TOMBSTONE) {\n                delete pair;\n            }\n        }\n        delete TOMBSTONE;\n    }\n\n    /* Hash function */\n    int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* Load factor */\n    double loadFactor() {\n        return (double)size / capacity;\n    }\n\n    /* Search for bucket index corresponding to key */\n    int findBucket(int key) {\n        int index = hashFunc(key);\n        int firstTombstone = -1;\n        // Linear probing, break when encountering an empty bucket\n        while (buckets[index] != nullptr) {\n            // If key is encountered, return the corresponding bucket index\n            if (buckets[index]->key == key) {\n                // If a removal marker was encountered before, move the key-value pair to that index\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index];\n                    buckets[index] = TOMBSTONE;\n                    return firstTombstone; // Return the moved bucket index\n                }\n                return index; // Return bucket index\n            }\n            // Record the first removal marker encountered\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index;\n            }\n            // Calculate bucket index, wrap around to the head if past the tail\n            index = (index + 1) % capacity;\n        }\n        // If key does not exist, return the index for insertion\n        return firstTombstone == -1 ? index : firstTombstone;\n    }\n\n    /* Query operation */\n    string get(int key) {\n        // Search for bucket index corresponding to key\n        int index = findBucket(key);\n        // If key-value pair is found, return corresponding val\n        if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n            return buckets[index]->val;\n        }\n        // Return empty string if key-value pair does not exist\n        return \"\";\n    }\n\n    /* Add operation */\n    void put(int key, string val) {\n        // When load factor exceeds threshold, perform expansion\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        // Search for bucket index corresponding to key\n        int index = findBucket(key);\n        // If key-value pair is found, overwrite val and return\n        if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n            buckets[index]->val = val;\n            return;\n        }\n        // If key-value pair does not exist, add the key-value pair\n        buckets[index] = new Pair(key, val);\n        size++;\n    }\n\n    /* Remove operation */\n    void remove(int key) {\n        // Search for bucket index corresponding to key\n        int index = findBucket(key);\n        // If key-value pair is found, overwrite it with removal marker\n        if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n            delete buckets[index];\n            buckets[index] = TOMBSTONE;\n            size--;\n        }\n    }\n\n    /* Expand hash table */\n    void extend() {\n        // Temporarily store the original hash table\n        vector<Pair *> bucketsTmp = buckets;\n        // Initialize expanded new hash table\n        capacity *= extendRatio;\n        buckets = vector<Pair *>(capacity, nullptr);\n        size = 0;\n        // Move key-value pairs from original hash table to new hash table\n        for (Pair *pair : bucketsTmp) {\n            if (pair != nullptr && pair != TOMBSTONE) {\n                put(pair->key, pair->val);\n                delete pair;\n            }\n        }\n    }\n\n    /* Print hash table */\n    void print() {\n        for (Pair *pair : buckets) {\n            if (pair == nullptr) {\n                cout << \"nullptr\" << endl;\n            } else if (pair == TOMBSTONE) {\n                cout << \"TOMBSTONE\" << endl;\n            } else {\n                cout << pair->key << \" -> \" << pair->val << endl;\n            }\n        }\n    }\n};\n
      hash_map_open_addressing.java
      /* Hash table with open addressing */\nclass HashMapOpenAddressing {\n    private int size; // Number of key-value pairs\n    private int capacity = 4; // Hash table capacity\n    private final double loadThres = 2.0 / 3.0; // Load factor threshold for triggering expansion\n    private final int extendRatio = 2; // Expansion multiplier\n    private Pair[] buckets; // Bucket array\n    private final Pair TOMBSTONE = new Pair(-1, \"-1\"); // Removal marker\n\n    /* Constructor */\n    public HashMapOpenAddressing() {\n        size = 0;\n        buckets = new Pair[capacity];\n    }\n\n    /* Hash function */\n    private int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* Load factor */\n    private double loadFactor() {\n        return (double) size / capacity;\n    }\n\n    /* Search for bucket index corresponding to key */\n    private int findBucket(int key) {\n        int index = hashFunc(key);\n        int firstTombstone = -1;\n        // Linear probing, break when encountering an empty bucket\n        while (buckets[index] != null) {\n            // If key is encountered, return the corresponding bucket index\n            if (buckets[index].key == key) {\n                // If a removal marker was encountered before, move the key-value pair to that index\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index];\n                    buckets[index] = TOMBSTONE;\n                    return firstTombstone; // Return the moved bucket index\n                }\n                return index; // Return bucket index\n            }\n            // Record the first removal marker encountered\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index;\n            }\n            // Calculate bucket index, wrap around to the head if past the tail\n            index = (index + 1) % capacity;\n        }\n        // If key does not exist, return the index for insertion\n        return firstTombstone == -1 ? index : firstTombstone;\n    }\n\n    /* Query operation */\n    public String get(int key) {\n        // Search for bucket index corresponding to key\n        int index = findBucket(key);\n        // If key-value pair is found, return corresponding val\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            return buckets[index].val;\n        }\n        // If key-value pair does not exist, return null\n        return null;\n    }\n\n    /* Add operation */\n    public void put(int key, String val) {\n        // When load factor exceeds threshold, perform expansion\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        // Search for bucket index corresponding to key\n        int index = findBucket(key);\n        // If key-value pair is found, overwrite val and return\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index].val = val;\n            return;\n        }\n        // If key-value pair does not exist, add the key-value pair\n        buckets[index] = new Pair(key, val);\n        size++;\n    }\n\n    /* Remove operation */\n    public void remove(int key) {\n        // Search for bucket index corresponding to key\n        int index = findBucket(key);\n        // If key-value pair is found, overwrite it with removal marker\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index] = TOMBSTONE;\n            size--;\n        }\n    }\n\n    /* Expand hash table */\n    private void extend() {\n        // Temporarily store the original hash table\n        Pair[] bucketsTmp = buckets;\n        // Initialize expanded new hash table\n        capacity *= extendRatio;\n        buckets = new Pair[capacity];\n        size = 0;\n        // Move key-value pairs from original hash table to new hash table\n        for (Pair pair : bucketsTmp) {\n            if (pair != null && pair != TOMBSTONE) {\n                put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* Print hash table */\n    public void print() {\n        for (Pair pair : buckets) {\n            if (pair == null) {\n                System.out.println(\"null\");\n            } else if (pair == TOMBSTONE) {\n                System.out.println(\"TOMBSTONE\");\n            } else {\n                System.out.println(pair.key + \" -> \" + pair.val);\n            }\n        }\n    }\n}\n
      hash_map_open_addressing.cs
      /* Hash table with open addressing */\nclass HashMapOpenAddressing {\n    int size; // Number of key-value pairs\n    int capacity = 4; // Hash table capacity\n    double loadThres = 2.0 / 3.0; // Load factor threshold for triggering expansion\n    int extendRatio = 2; // Expansion multiplier\n    Pair[] buckets; // Bucket array\n    Pair TOMBSTONE = new(-1, \"-1\"); // Removal marker\n\n    /* Constructor */\n    public HashMapOpenAddressing() {\n        size = 0;\n        buckets = new Pair[capacity];\n    }\n\n    /* Hash function */\n    int HashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* Load factor */\n    double LoadFactor() {\n        return (double)size / capacity;\n    }\n\n    /* Search for bucket index corresponding to key */\n    int FindBucket(int key) {\n        int index = HashFunc(key);\n        int firstTombstone = -1;\n        // Linear probing, break when encountering an empty bucket\n        while (buckets[index] != null) {\n            // If key is encountered, return the corresponding bucket index\n            if (buckets[index].key == key) {\n                // If a removal marker was encountered before, move the key-value pair to that index\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index];\n                    buckets[index] = TOMBSTONE;\n                    return firstTombstone; // Return the moved bucket index\n                }\n                return index; // Return bucket index\n            }\n            // Record the first removal marker encountered\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index;\n            }\n            // Calculate bucket index, wrap around to the head if past the tail\n            index = (index + 1) % capacity;\n        }\n        // If key does not exist, return the index for insertion\n        return firstTombstone == -1 ? index : firstTombstone;\n    }\n\n    /* Query operation */\n    public string? Get(int key) {\n        // Search for bucket index corresponding to key\n        int index = FindBucket(key);\n        // If key-value pair is found, return corresponding val\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            return buckets[index].val;\n        }\n        // If key-value pair does not exist, return null\n        return null;\n    }\n\n    /* Add operation */\n    public void Put(int key, string val) {\n        // When load factor exceeds threshold, perform expansion\n        if (LoadFactor() > loadThres) {\n            Extend();\n        }\n        // Search for bucket index corresponding to key\n        int index = FindBucket(key);\n        // If key-value pair is found, overwrite val and return\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index].val = val;\n            return;\n        }\n        // If key-value pair does not exist, add the key-value pair\n        buckets[index] = new Pair(key, val);\n        size++;\n    }\n\n    /* Remove operation */\n    public void Remove(int key) {\n        // Search for bucket index corresponding to key\n        int index = FindBucket(key);\n        // If key-value pair is found, overwrite it with removal marker\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index] = TOMBSTONE;\n            size--;\n        }\n    }\n\n    /* Expand hash table */\n    void Extend() {\n        // Temporarily store the original hash table\n        Pair[] bucketsTmp = buckets;\n        // Initialize expanded new hash table\n        capacity *= extendRatio;\n        buckets = new Pair[capacity];\n        size = 0;\n        // Move key-value pairs from original hash table to new hash table\n        foreach (Pair pair in bucketsTmp) {\n            if (pair != null && pair != TOMBSTONE) {\n                Put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* Print hash table */\n    public void Print() {\n        foreach (Pair pair in buckets) {\n            if (pair == null) {\n                Console.WriteLine(\"null\");\n            } else if (pair == TOMBSTONE) {\n                Console.WriteLine(\"TOMBSTONE\");\n            } else {\n                Console.WriteLine(pair.key + \" -> \" + pair.val);\n            }\n        }\n    }\n}\n
      hash_map_open_addressing.go
      /* Hash table with open addressing */\ntype hashMapOpenAddressing struct {\n    size        int     // Number of key-value pairs\n    capacity    int     // Hash table capacity\n    loadThres   float64 // Load factor threshold for triggering expansion\n    extendRatio int     // Expansion multiplier\n    buckets     []*pair // Bucket array\n    TOMBSTONE   *pair   // Removal marker\n}\n\n/* Constructor */\nfunc newHashMapOpenAddressing() *hashMapOpenAddressing {\n    return &hashMapOpenAddressing{\n        size:        0,\n        capacity:    4,\n        loadThres:   2.0 / 3.0,\n        extendRatio: 2,\n        buckets:     make([]*pair, 4),\n        TOMBSTONE:   &pair{-1, \"-1\"},\n    }\n}\n\n/* Hash function */\nfunc (h *hashMapOpenAddressing) hashFunc(key int) int {\n    return key % h.capacity // Calculate hash value based on key\n}\n\n/* Load factor */\nfunc (h *hashMapOpenAddressing) loadFactor() float64 {\n    return float64(h.size) / float64(h.capacity) // Calculate current load factor\n}\n\n/* Search for bucket index corresponding to key */\nfunc (h *hashMapOpenAddressing) findBucket(key int) int {\n    index := h.hashFunc(key) // Get initial index\n    firstTombstone := -1     // Record position of first TOMBSTONE encountered\n    for h.buckets[index] != nil {\n        if h.buckets[index].key == key {\n            if firstTombstone != -1 {\n                // If a removal marker was encountered before, move the key-value pair to that index\n                h.buckets[firstTombstone] = h.buckets[index]\n                h.buckets[index] = h.TOMBSTONE\n                return firstTombstone // Return the moved bucket index\n            }\n            return index // Return found index\n        }\n        if firstTombstone == -1 && h.buckets[index] == h.TOMBSTONE {\n            firstTombstone = index // Record position of first deletion marker encountered\n        }\n        index = (index + 1) % h.capacity // Linear probing, wrap around to head if past tail\n    }\n    // If key does not exist, return the index for insertion\n    if firstTombstone != -1 {\n        return firstTombstone\n    }\n    return index\n}\n\n/* Query operation */\nfunc (h *hashMapOpenAddressing) get(key int) string {\n    index := h.findBucket(key) // Search for bucket index corresponding to key\n    if h.buckets[index] != nil && h.buckets[index] != h.TOMBSTONE {\n        return h.buckets[index].val // If key-value pair is found, return corresponding val\n    }\n    return \"\" // Return \"\" if key-value pair does not exist\n}\n\n/* Add operation */\nfunc (h *hashMapOpenAddressing) put(key int, val string) {\n    if h.loadFactor() > h.loadThres {\n        h.extend() // When load factor exceeds threshold, perform expansion\n    }\n    index := h.findBucket(key) // Search for bucket index corresponding to key\n    if h.buckets[index] == nil || h.buckets[index] == h.TOMBSTONE {\n        h.buckets[index] = &pair{key, val} // If key-value pair does not exist, add the key-value pair\n        h.size++\n    } else {\n        h.buckets[index].val = val // If key-value pair found, overwrite val\n    }\n}\n\n/* Remove operation */\nfunc (h *hashMapOpenAddressing) remove(key int) {\n    index := h.findBucket(key) // Search for bucket index corresponding to key\n    if h.buckets[index] != nil && h.buckets[index] != h.TOMBSTONE {\n        h.buckets[index] = h.TOMBSTONE // If key-value pair is found, overwrite it with removal marker\n        h.size--\n    }\n}\n\n/* Expand hash table */\nfunc (h *hashMapOpenAddressing) extend() {\n    oldBuckets := h.buckets               // Temporarily store the original hash table\n    h.capacity *= h.extendRatio           // Update capacity\n    h.buckets = make([]*pair, h.capacity) // Initialize expanded new hash table\n    h.size = 0                            // Reset size\n    // Move key-value pairs from original hash table to new hash table\n    for _, pair := range oldBuckets {\n        if pair != nil && pair != h.TOMBSTONE {\n            h.put(pair.key, pair.val)\n        }\n    }\n}\n\n/* Print hash table */\nfunc (h *hashMapOpenAddressing) print() {\n    for _, pair := range h.buckets {\n        if pair == nil {\n            fmt.Println(\"nil\")\n        } else if pair == h.TOMBSTONE {\n            fmt.Println(\"TOMBSTONE\")\n        } else {\n            fmt.Printf(\"%d -> %s\\n\", pair.key, pair.val)\n        }\n    }\n}\n
      hash_map_open_addressing.swift
      /* Hash table with open addressing */\nclass HashMapOpenAddressing {\n    var size: Int // Number of key-value pairs\n    var capacity: Int // Hash table capacity\n    var loadThres: Double // Load factor threshold for triggering expansion\n    var extendRatio: Int // Expansion multiplier\n    var buckets: [Pair?] // Bucket array\n    var TOMBSTONE: Pair // Removal marker\n\n    /* Constructor */\n    init() {\n        size = 0\n        capacity = 4\n        loadThres = 2.0 / 3.0\n        extendRatio = 2\n        buckets = Array(repeating: nil, count: capacity)\n        TOMBSTONE = Pair(key: -1, val: \"-1\")\n    }\n\n    /* Hash function */\n    func hashFunc(key: Int) -> Int {\n        key % capacity\n    }\n\n    /* Load factor */\n    func loadFactor() -> Double {\n        Double(size) / Double(capacity)\n    }\n\n    /* Search for bucket index corresponding to key */\n    func findBucket(key: Int) -> Int {\n        var index = hashFunc(key: key)\n        var firstTombstone = -1\n        // Linear probing, break when encountering an empty bucket\n        while buckets[index] != nil {\n            // If key is encountered, return the corresponding bucket index\n            if buckets[index]!.key == key {\n                // If a removal marker was encountered before, move the key-value pair to that index\n                if firstTombstone != -1 {\n                    buckets[firstTombstone] = buckets[index]\n                    buckets[index] = TOMBSTONE\n                    return firstTombstone // Return the moved bucket index\n                }\n                return index // Return bucket index\n            }\n            // Record the first removal marker encountered\n            if firstTombstone == -1 && buckets[index] == TOMBSTONE {\n                firstTombstone = index\n            }\n            // Calculate bucket index, wrap around to the head if past the tail\n            index = (index + 1) % capacity\n        }\n        // If key does not exist, return the index for insertion\n        return firstTombstone == -1 ? index : firstTombstone\n    }\n\n    /* Query operation */\n    func get(key: Int) -> String? {\n        // Search for bucket index corresponding to key\n        let index = findBucket(key: key)\n        // If key-value pair is found, return corresponding val\n        if buckets[index] != nil, buckets[index] != TOMBSTONE {\n            return buckets[index]!.val\n        }\n        // If key-value pair does not exist, return null\n        return nil\n    }\n\n    /* Add operation */\n    func put(key: Int, val: String) {\n        // When load factor exceeds threshold, perform expansion\n        if loadFactor() > loadThres {\n            extend()\n        }\n        // Search for bucket index corresponding to key\n        let index = findBucket(key: key)\n        // If key-value pair is found, overwrite val and return\n        if buckets[index] != nil, buckets[index] != TOMBSTONE {\n            buckets[index]!.val = val\n            return\n        }\n        // If key-value pair does not exist, add the key-value pair\n        buckets[index] = Pair(key: key, val: val)\n        size += 1\n    }\n\n    /* Remove operation */\n    func remove(key: Int) {\n        // Search for bucket index corresponding to key\n        let index = findBucket(key: key)\n        // If key-value pair is found, overwrite it with removal marker\n        if buckets[index] != nil, buckets[index] != TOMBSTONE {\n            buckets[index] = TOMBSTONE\n            size -= 1\n        }\n    }\n\n    /* Expand hash table */\n    func extend() {\n        // Temporarily store the original hash table\n        let bucketsTmp = buckets\n        // Initialize expanded new hash table\n        capacity *= extendRatio\n        buckets = Array(repeating: nil, count: capacity)\n        size = 0\n        // Move key-value pairs from original hash table to new hash table\n        for pair in bucketsTmp {\n            if let pair, pair != TOMBSTONE {\n                put(key: pair.key, val: pair.val)\n            }\n        }\n    }\n\n    /* Print hash table */\n    func print() {\n        for pair in buckets {\n            if pair == nil {\n                Swift.print(\"null\")\n            } else if pair == TOMBSTONE {\n                Swift.print(\"TOMBSTONE\")\n            } else {\n                Swift.print(\"\\(pair!.key) -> \\(pair!.val)\")\n            }\n        }\n    }\n}\n
      hash_map_open_addressing.js
      /* Hash table with open addressing */\nclass HashMapOpenAddressing {\n    #size; // Number of key-value pairs\n    #capacity; // Hash table capacity\n    #loadThres; // Load factor threshold for triggering expansion\n    #extendRatio; // Expansion multiplier\n    #buckets; // Bucket array\n    #TOMBSTONE; // Removal marker\n\n    /* Constructor */\n    constructor() {\n        this.#size = 0; // Number of key-value pairs\n        this.#capacity = 4; // Hash table capacity\n        this.#loadThres = 2.0 / 3.0; // Load factor threshold for triggering expansion\n        this.#extendRatio = 2; // Expansion multiplier\n        this.#buckets = Array(this.#capacity).fill(null); // Bucket array\n        this.#TOMBSTONE = new Pair(-1, '-1'); // Removal marker\n    }\n\n    /* Hash function */\n    #hashFunc(key) {\n        return key % this.#capacity;\n    }\n\n    /* Load factor */\n    #loadFactor() {\n        return this.#size / this.#capacity;\n    }\n\n    /* Search for bucket index corresponding to key */\n    #findBucket(key) {\n        let index = this.#hashFunc(key);\n        let firstTombstone = -1;\n        // Linear probing, break when encountering an empty bucket\n        while (this.#buckets[index] !== null) {\n            // If key is encountered, return the corresponding bucket index\n            if (this.#buckets[index].key === key) {\n                // If a removal marker was encountered before, move the key-value pair to that index\n                if (firstTombstone !== -1) {\n                    this.#buckets[firstTombstone] = this.#buckets[index];\n                    this.#buckets[index] = this.#TOMBSTONE;\n                    return firstTombstone; // Return the moved bucket index\n                }\n                return index; // Return bucket index\n            }\n            // Record the first removal marker encountered\n            if (\n                firstTombstone === -1 &&\n                this.#buckets[index] === this.#TOMBSTONE\n            ) {\n                firstTombstone = index;\n            }\n            // Calculate bucket index, wrap around to the head if past the tail\n            index = (index + 1) % this.#capacity;\n        }\n        // If key does not exist, return the index for insertion\n        return firstTombstone === -1 ? index : firstTombstone;\n    }\n\n    /* Query operation */\n    get(key) {\n        // Search for bucket index corresponding to key\n        const index = this.#findBucket(key);\n        // If key-value pair is found, return corresponding val\n        if (\n            this.#buckets[index] !== null &&\n            this.#buckets[index] !== this.#TOMBSTONE\n        ) {\n            return this.#buckets[index].val;\n        }\n        // If key-value pair does not exist, return null\n        return null;\n    }\n\n    /* Add operation */\n    put(key, val) {\n        // When load factor exceeds threshold, perform expansion\n        if (this.#loadFactor() > this.#loadThres) {\n            this.#extend();\n        }\n        // Search for bucket index corresponding to key\n        const index = this.#findBucket(key);\n        // If key-value pair is found, overwrite val and return\n        if (\n            this.#buckets[index] !== null &&\n            this.#buckets[index] !== this.#TOMBSTONE\n        ) {\n            this.#buckets[index].val = val;\n            return;\n        }\n        // If key-value pair does not exist, add the key-value pair\n        this.#buckets[index] = new Pair(key, val);\n        this.#size++;\n    }\n\n    /* Remove operation */\n    remove(key) {\n        // Search for bucket index corresponding to key\n        const index = this.#findBucket(key);\n        // If key-value pair is found, overwrite it with removal marker\n        if (\n            this.#buckets[index] !== null &&\n            this.#buckets[index] !== this.#TOMBSTONE\n        ) {\n            this.#buckets[index] = this.#TOMBSTONE;\n            this.#size--;\n        }\n    }\n\n    /* Expand hash table */\n    #extend() {\n        // Temporarily store the original hash table\n        const bucketsTmp = this.#buckets;\n        // Initialize expanded new hash table\n        this.#capacity *= this.#extendRatio;\n        this.#buckets = Array(this.#capacity).fill(null);\n        this.#size = 0;\n        // Move key-value pairs from original hash table to new hash table\n        for (const pair of bucketsTmp) {\n            if (pair !== null && pair !== this.#TOMBSTONE) {\n                this.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* Print hash table */\n    print() {\n        for (const pair of this.#buckets) {\n            if (pair === null) {\n                console.log('null');\n            } else if (pair === this.#TOMBSTONE) {\n                console.log('TOMBSTONE');\n            } else {\n                console.log(pair.key + ' -> ' + pair.val);\n            }\n        }\n    }\n}\n
      hash_map_open_addressing.ts
      /* Hash table with open addressing */\nclass HashMapOpenAddressing {\n    private size: number; // Number of key-value pairs\n    private capacity: number; // Hash table capacity\n    private loadThres: number; // Load factor threshold for triggering expansion\n    private extendRatio: number; // Expansion multiplier\n    private buckets: Array<Pair | null>; // Bucket array\n    private TOMBSTONE: Pair; // Removal marker\n\n    /* Constructor */\n    constructor() {\n        this.size = 0; // Number of key-value pairs\n        this.capacity = 4; // Hash table capacity\n        this.loadThres = 2.0 / 3.0; // Load factor threshold for triggering expansion\n        this.extendRatio = 2; // Expansion multiplier\n        this.buckets = Array(this.capacity).fill(null); // Bucket array\n        this.TOMBSTONE = new Pair(-1, '-1'); // Removal marker\n    }\n\n    /* Hash function */\n    private hashFunc(key: number): number {\n        return key % this.capacity;\n    }\n\n    /* Load factor */\n    private loadFactor(): number {\n        return this.size / this.capacity;\n    }\n\n    /* Search for bucket index corresponding to key */\n    private findBucket(key: number): number {\n        let index = this.hashFunc(key);\n        let firstTombstone = -1;\n        // Linear probing, break when encountering an empty bucket\n        while (this.buckets[index] !== null) {\n            // If key is encountered, return the corresponding bucket index\n            if (this.buckets[index]!.key === key) {\n                // If a removal marker was encountered before, move the key-value pair to that index\n                if (firstTombstone !== -1) {\n                    this.buckets[firstTombstone] = this.buckets[index];\n                    this.buckets[index] = this.TOMBSTONE;\n                    return firstTombstone; // Return the moved bucket index\n                }\n                return index; // Return bucket index\n            }\n            // Record the first removal marker encountered\n            if (\n                firstTombstone === -1 &&\n                this.buckets[index] === this.TOMBSTONE\n            ) {\n                firstTombstone = index;\n            }\n            // Calculate bucket index, wrap around to the head if past the tail\n            index = (index + 1) % this.capacity;\n        }\n        // If key does not exist, return the index for insertion\n        return firstTombstone === -1 ? index : firstTombstone;\n    }\n\n    /* Query operation */\n    get(key: number): string | null {\n        // Search for bucket index corresponding to key\n        const index = this.findBucket(key);\n        // If key-value pair is found, return corresponding val\n        if (\n            this.buckets[index] !== null &&\n            this.buckets[index] !== this.TOMBSTONE\n        ) {\n            return this.buckets[index]!.val;\n        }\n        // If key-value pair does not exist, return null\n        return null;\n    }\n\n    /* Add operation */\n    put(key: number, val: string): void {\n        // When load factor exceeds threshold, perform expansion\n        if (this.loadFactor() > this.loadThres) {\n            this.extend();\n        }\n        // Search for bucket index corresponding to key\n        const index = this.findBucket(key);\n        // If key-value pair is found, overwrite val and return\n        if (\n            this.buckets[index] !== null &&\n            this.buckets[index] !== this.TOMBSTONE\n        ) {\n            this.buckets[index]!.val = val;\n            return;\n        }\n        // If key-value pair does not exist, add the key-value pair\n        this.buckets[index] = new Pair(key, val);\n        this.size++;\n    }\n\n    /* Remove operation */\n    remove(key: number): void {\n        // Search for bucket index corresponding to key\n        const index = this.findBucket(key);\n        // If key-value pair is found, overwrite it with removal marker\n        if (\n            this.buckets[index] !== null &&\n            this.buckets[index] !== this.TOMBSTONE\n        ) {\n            this.buckets[index] = this.TOMBSTONE;\n            this.size--;\n        }\n    }\n\n    /* Expand hash table */\n    private extend(): void {\n        // Temporarily store the original hash table\n        const bucketsTmp = this.buckets;\n        // Initialize expanded new hash table\n        this.capacity *= this.extendRatio;\n        this.buckets = Array(this.capacity).fill(null);\n        this.size = 0;\n        // Move key-value pairs from original hash table to new hash table\n        for (const pair of bucketsTmp) {\n            if (pair !== null && pair !== this.TOMBSTONE) {\n                this.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* Print hash table */\n    print(): void {\n        for (const pair of this.buckets) {\n            if (pair === null) {\n                console.log('null');\n            } else if (pair === this.TOMBSTONE) {\n                console.log('TOMBSTONE');\n            } else {\n                console.log(pair.key + ' -> ' + pair.val);\n            }\n        }\n    }\n}\n
      hash_map_open_addressing.dart
      /* Hash table with open addressing */\nclass HashMapOpenAddressing {\n  late int _size; // Number of key-value pairs\n  int _capacity = 4; // Hash table capacity\n  double _loadThres = 2.0 / 3.0; // Load factor threshold for triggering expansion\n  int _extendRatio = 2; // Expansion multiplier\n  late List<Pair?> _buckets; // Bucket array\n  Pair _TOMBSTONE = Pair(-1, \"-1\"); // Removal marker\n\n  /* Constructor */\n  HashMapOpenAddressing() {\n    _size = 0;\n    _buckets = List.generate(_capacity, (index) => null);\n  }\n\n  /* Hash function */\n  int hashFunc(int key) {\n    return key % _capacity;\n  }\n\n  /* Load factor */\n  double loadFactor() {\n    return _size / _capacity;\n  }\n\n  /* Search for bucket index corresponding to key */\n  int findBucket(int key) {\n    int index = hashFunc(key);\n    int firstTombstone = -1;\n    // Linear probing, break when encountering an empty bucket\n    while (_buckets[index] != null) {\n      // If key is encountered, return the corresponding bucket index\n      if (_buckets[index]!.key == key) {\n        // If a removal marker was encountered before, move the key-value pair to that index\n        if (firstTombstone != -1) {\n          _buckets[firstTombstone] = _buckets[index];\n          _buckets[index] = _TOMBSTONE;\n          return firstTombstone; // Return the moved bucket index\n        }\n        return index; // Return bucket index\n      }\n      // Record the first removal marker encountered\n      if (firstTombstone == -1 && _buckets[index] == _TOMBSTONE) {\n        firstTombstone = index;\n      }\n      // Calculate bucket index, wrap around to the head if past the tail\n      index = (index + 1) % _capacity;\n    }\n    // If key does not exist, return the index for insertion\n    return firstTombstone == -1 ? index : firstTombstone;\n  }\n\n  /* Query operation */\n  String? get(int key) {\n    // Search for bucket index corresponding to key\n    int index = findBucket(key);\n    // If key-value pair is found, return corresponding val\n    if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n      return _buckets[index]!.val;\n    }\n    // If key-value pair does not exist, return null\n    return null;\n  }\n\n  /* Add operation */\n  void put(int key, String val) {\n    // When load factor exceeds threshold, perform expansion\n    if (loadFactor() > _loadThres) {\n      extend();\n    }\n    // Search for bucket index corresponding to key\n    int index = findBucket(key);\n    // If key-value pair is found, overwrite val and return\n    if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n      _buckets[index]!.val = val;\n      return;\n    }\n    // If key-value pair does not exist, add the key-value pair\n    _buckets[index] = new Pair(key, val);\n    _size++;\n  }\n\n  /* Remove operation */\n  void remove(int key) {\n    // Search for bucket index corresponding to key\n    int index = findBucket(key);\n    // If key-value pair is found, overwrite it with removal marker\n    if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n      _buckets[index] = _TOMBSTONE;\n      _size--;\n    }\n  }\n\n  /* Expand hash table */\n  void extend() {\n    // Temporarily store the original hash table\n    List<Pair?> bucketsTmp = _buckets;\n    // Initialize expanded new hash table\n    _capacity *= _extendRatio;\n    _buckets = List.generate(_capacity, (index) => null);\n    _size = 0;\n    // Move key-value pairs from original hash table to new hash table\n    for (Pair? pair in bucketsTmp) {\n      if (pair != null && pair != _TOMBSTONE) {\n        put(pair.key, pair.val);\n      }\n    }\n  }\n\n  /* Print hash table */\n  void printHashMap() {\n    for (Pair? pair in _buckets) {\n      if (pair == null) {\n        print(\"null\");\n      } else if (pair == _TOMBSTONE) {\n        print(\"TOMBSTONE\");\n      } else {\n        print(\"${pair.key} -> ${pair.val}\");\n      }\n    }\n  }\n}\n
      hash_map_open_addressing.rs
      /* Hash table with open addressing */\nstruct HashMapOpenAddressing {\n    size: usize,                // Number of key-value pairs\n    capacity: usize,            // Hash table capacity\n    load_thres: f64,            // Load factor threshold for triggering expansion\n    extend_ratio: usize,        // Expansion multiplier\n    buckets: Vec<Option<Pair>>, // Bucket array\n    TOMBSTONE: Option<Pair>,    // Removal marker\n}\n\nimpl HashMapOpenAddressing {\n    /* Constructor */\n    fn new() -> Self {\n        Self {\n            size: 0,\n            capacity: 4,\n            load_thres: 2.0 / 3.0,\n            extend_ratio: 2,\n            buckets: vec![None; 4],\n            TOMBSTONE: Some(Pair {\n                key: -1,\n                val: \"-1\".to_string(),\n            }),\n        }\n    }\n\n    /* Hash function */\n    fn hash_func(&self, key: i32) -> usize {\n        (key % self.capacity as i32) as usize\n    }\n\n    /* Load factor */\n    fn load_factor(&self) -> f64 {\n        self.size as f64 / self.capacity as f64\n    }\n\n    /* Search for bucket index corresponding to key */\n    fn find_bucket(&mut self, key: i32) -> usize {\n        let mut index = self.hash_func(key);\n        let mut first_tombstone = -1;\n        // Linear probing, break when encountering an empty bucket\n        while self.buckets[index].is_some() {\n            // If key is found, return corresponding bucket index\n            if self.buckets[index].as_ref().unwrap().key == key {\n                // If deletion marker was encountered before, move key-value pair to that index\n                if first_tombstone != -1 {\n                    self.buckets[first_tombstone as usize] = self.buckets[index].take();\n                    self.buckets[index] = self.TOMBSTONE.clone();\n                    return first_tombstone as usize; // Return the moved bucket index\n                }\n                return index; // Return bucket index\n            }\n            // Record the first removal marker encountered\n            if first_tombstone == -1 && self.buckets[index] == self.TOMBSTONE {\n                first_tombstone = index as i32;\n            }\n            // Calculate bucket index, wrap around to the head if past the tail\n            index = (index + 1) % self.capacity;\n        }\n        // If key does not exist, return the index for insertion\n        if first_tombstone == -1 {\n            index\n        } else {\n            first_tombstone as usize\n        }\n    }\n\n    /* Query operation */\n    fn get(&mut self, key: i32) -> Option<&str> {\n        // Search for bucket index corresponding to key\n        let index = self.find_bucket(key);\n        // If key-value pair is found, return corresponding val\n        if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n            return self.buckets[index].as_ref().map(|pair| &pair.val as &str);\n        }\n        // If key-value pair does not exist, return null\n        None\n    }\n\n    /* Add operation */\n    fn put(&mut self, key: i32, val: String) {\n        // When load factor exceeds threshold, perform expansion\n        if self.load_factor() > self.load_thres {\n            self.extend();\n        }\n        // Search for bucket index corresponding to key\n        let index = self.find_bucket(key);\n        // If key-value pair is found, overwrite val and return\n        if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n            self.buckets[index].as_mut().unwrap().val = val;\n            return;\n        }\n        // If key-value pair does not exist, add the key-value pair\n        self.buckets[index] = Some(Pair { key, val });\n        self.size += 1;\n    }\n\n    /* Remove operation */\n    fn remove(&mut self, key: i32) {\n        // Search for bucket index corresponding to key\n        let index = self.find_bucket(key);\n        // If key-value pair is found, overwrite it with removal marker\n        if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n            self.buckets[index] = self.TOMBSTONE.clone();\n            self.size -= 1;\n        }\n    }\n\n    /* Expand hash table */\n    fn extend(&mut self) {\n        // Temporarily store the original hash table\n        let buckets_tmp = self.buckets.clone();\n        // Initialize expanded new hash table\n        self.capacity *= self.extend_ratio;\n        self.buckets = vec![None; self.capacity];\n        self.size = 0;\n\n        // Move key-value pairs from original hash table to new hash table\n        for pair in buckets_tmp {\n            if pair.is_none() || pair == self.TOMBSTONE {\n                continue;\n            }\n            let pair = pair.unwrap();\n\n            self.put(pair.key, pair.val);\n        }\n    }\n    /* Print hash table */\n    fn print(&self) {\n        for pair in &self.buckets {\n            if pair.is_none() {\n                println!(\"null\");\n            } else if pair == &self.TOMBSTONE {\n                println!(\"TOMBSTONE\");\n            } else {\n                let pair = pair.as_ref().unwrap();\n                println!(\"{} -> {}\", pair.key, pair.val);\n            }\n        }\n    }\n}\n
      hash_map_open_addressing.c
      /* Hash table with open addressing */\ntypedef struct {\n    int size;         // Number of key-value pairs\n    int capacity;     // Hash table capacity\n    double loadThres; // Load factor threshold for triggering expansion\n    int extendRatio;  // Expansion multiplier\n    Pair **buckets;   // Bucket array\n    Pair *TOMBSTONE;  // Removal marker\n} HashMapOpenAddressing;\n\n/* Constructor */\nHashMapOpenAddressing *newHashMapOpenAddressing() {\n    HashMapOpenAddressing *hashMap = (HashMapOpenAddressing *)malloc(sizeof(HashMapOpenAddressing));\n    hashMap->size = 0;\n    hashMap->capacity = 4;\n    hashMap->loadThres = 2.0 / 3.0;\n    hashMap->extendRatio = 2;\n    hashMap->buckets = (Pair **)calloc(hashMap->capacity, sizeof(Pair *));\n    hashMap->TOMBSTONE = (Pair *)malloc(sizeof(Pair));\n    hashMap->TOMBSTONE->key = -1;\n    hashMap->TOMBSTONE->val = \"-1\";\n\n    return hashMap;\n}\n\n/* Destructor */\nvoid delHashMapOpenAddressing(HashMapOpenAddressing *hashMap) {\n    for (int i = 0; i < hashMap->capacity; i++) {\n        Pair *pair = hashMap->buckets[i];\n        if (pair != NULL && pair != hashMap->TOMBSTONE) {\n            free(pair->val);\n            free(pair);\n        }\n    }\n    free(hashMap->buckets);\n    free(hashMap->TOMBSTONE);\n    free(hashMap);\n}\n\n/* Hash function */\nint hashFunc(HashMapOpenAddressing *hashMap, int key) {\n    return key % hashMap->capacity;\n}\n\n/* Load factor */\ndouble loadFactor(HashMapOpenAddressing *hashMap) {\n    return (double)hashMap->size / (double)hashMap->capacity;\n}\n\n/* Search for bucket index corresponding to key */\nint findBucket(HashMapOpenAddressing *hashMap, int key) {\n    int index = hashFunc(hashMap, key);\n    int firstTombstone = -1;\n    // Linear probing, break when encountering an empty bucket\n    while (hashMap->buckets[index] != NULL) {\n        // If key is encountered, return the corresponding bucket index\n        if (hashMap->buckets[index]->key == key) {\n            // If a removal marker was encountered before, move the key-value pair to that index\n            if (firstTombstone != -1) {\n                hashMap->buckets[firstTombstone] = hashMap->buckets[index];\n                hashMap->buckets[index] = hashMap->TOMBSTONE;\n                return firstTombstone; // Return the moved bucket index\n            }\n            return index; // Return bucket index\n        }\n        // Record the first removal marker encountered\n        if (firstTombstone == -1 && hashMap->buckets[index] == hashMap->TOMBSTONE) {\n            firstTombstone = index;\n        }\n        // Calculate bucket index, wrap around to the head if past the tail\n        index = (index + 1) % hashMap->capacity;\n    }\n    // If key does not exist, return the index for insertion\n    return firstTombstone == -1 ? index : firstTombstone;\n}\n\n/* Query operation */\nchar *get(HashMapOpenAddressing *hashMap, int key) {\n    // Search for bucket index corresponding to key\n    int index = findBucket(hashMap, key);\n    // If key-value pair is found, return corresponding val\n    if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n        return hashMap->buckets[index]->val;\n    }\n    // Return empty string if key-value pair does not exist\n    return \"\";\n}\n\n/* Add operation */\nvoid put(HashMapOpenAddressing *hashMap, int key, char *val) {\n    // When load factor exceeds threshold, perform expansion\n    if (loadFactor(hashMap) > hashMap->loadThres) {\n        extend(hashMap);\n    }\n    // Search for bucket index corresponding to key\n    int index = findBucket(hashMap, key);\n    // If key-value pair is found, overwrite val and return\n    if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n        free(hashMap->buckets[index]->val);\n        hashMap->buckets[index]->val = (char *)malloc(sizeof(strlen(val) + 1));\n        strcpy(hashMap->buckets[index]->val, val);\n        hashMap->buckets[index]->val[strlen(val)] = '\\0';\n        return;\n    }\n    // If key-value pair does not exist, add the key-value pair\n    Pair *pair = (Pair *)malloc(sizeof(Pair));\n    pair->key = key;\n    pair->val = (char *)malloc(sizeof(strlen(val) + 1));\n    strcpy(pair->val, val);\n    pair->val[strlen(val)] = '\\0';\n\n    hashMap->buckets[index] = pair;\n    hashMap->size++;\n}\n\n/* Remove operation */\nvoid removeItem(HashMapOpenAddressing *hashMap, int key) {\n    // Search for bucket index corresponding to key\n    int index = findBucket(hashMap, key);\n    // If key-value pair is found, overwrite it with removal marker\n    if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n        Pair *pair = hashMap->buckets[index];\n        free(pair->val);\n        free(pair);\n        hashMap->buckets[index] = hashMap->TOMBSTONE;\n        hashMap->size--;\n    }\n}\n\n/* Expand hash table */\nvoid extend(HashMapOpenAddressing *hashMap) {\n    // Temporarily store the original hash table\n    Pair **bucketsTmp = hashMap->buckets;\n    int oldCapacity = hashMap->capacity;\n    // Initialize expanded new hash table\n    hashMap->capacity *= hashMap->extendRatio;\n    hashMap->buckets = (Pair **)calloc(hashMap->capacity, sizeof(Pair *));\n    hashMap->size = 0;\n    // Move key-value pairs from original hash table to new hash table\n    for (int i = 0; i < oldCapacity; i++) {\n        Pair *pair = bucketsTmp[i];\n        if (pair != NULL && pair != hashMap->TOMBSTONE) {\n            put(hashMap, pair->key, pair->val);\n            free(pair->val);\n            free(pair);\n        }\n    }\n    free(bucketsTmp);\n}\n\n/* Print hash table */\nvoid print(HashMapOpenAddressing *hashMap) {\n    for (int i = 0; i < hashMap->capacity; i++) {\n        Pair *pair = hashMap->buckets[i];\n        if (pair == NULL) {\n            printf(\"NULL\\n\");\n        } else if (pair == hashMap->TOMBSTONE) {\n            printf(\"TOMBSTONE\\n\");\n        } else {\n            printf(\"%d -> %s\\n\", pair->key, pair->val);\n        }\n    }\n}\n
      hash_map_open_addressing.kt
      /* Hash table with open addressing */\nclass HashMapOpenAddressing {\n    private var size: Int               // Number of key-value pairs\n    private var capacity: Int           // Hash table capacity\n    private val loadThres: Double       // Load factor threshold for triggering expansion\n    private val extendRatio: Int        // Expansion multiplier\n    private var buckets: Array<Pair?>   // Bucket array\n    private val TOMBSTONE: Pair         // Removal marker\n\n    /* Constructor */\n    init {\n        size = 0\n        capacity = 4\n        loadThres = 2.0 / 3.0\n        extendRatio = 2\n        buckets = arrayOfNulls(capacity)\n        TOMBSTONE = Pair(-1, \"-1\")\n    }\n\n    /* Hash function */\n    fun hashFunc(key: Int): Int {\n        return key % capacity\n    }\n\n    /* Load factor */\n    fun loadFactor(): Double {\n        return (size / capacity).toDouble()\n    }\n\n    /* Search for bucket index corresponding to key */\n    fun findBucket(key: Int): Int {\n        var index = hashFunc(key)\n        var firstTombstone = -1\n        // Linear probing, break when encountering an empty bucket\n        while (buckets[index] != null) {\n            // If key is encountered, return the corresponding bucket index\n            if (buckets[index]?.key == key) {\n                // If a removal marker was encountered before, move the key-value pair to that index\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index]\n                    buckets[index] = TOMBSTONE\n                    return firstTombstone // Return the moved bucket index\n                }\n                return index // Return bucket index\n            }\n            // Record the first removal marker encountered\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index\n            }\n            // Calculate bucket index, wrap around to the head if past the tail\n            index = (index + 1) % capacity\n        }\n        // If key does not exist, return the index for insertion\n        return if (firstTombstone == -1) index else firstTombstone\n    }\n\n    /* Query operation */\n    fun get(key: Int): String? {\n        // Search for bucket index corresponding to key\n        val index = findBucket(key)\n        // If key-value pair is found, return corresponding val\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            return buckets[index]?._val\n        }\n        // If key-value pair does not exist, return null\n        return null\n    }\n\n    /* Add operation */\n    fun put(key: Int, _val: String) {\n        // When load factor exceeds threshold, perform expansion\n        if (loadFactor() > loadThres) {\n            extend()\n        }\n        // Search for bucket index corresponding to key\n        val index = findBucket(key)\n        // If key-value pair is found, overwrite val and return\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index]!!._val = _val\n            return\n        }\n        // If key-value pair does not exist, add the key-value pair\n        buckets[index] = Pair(key, _val)\n        size++\n    }\n\n    /* Remove operation */\n    fun remove(key: Int) {\n        // Search for bucket index corresponding to key\n        val index = findBucket(key)\n        // If key-value pair is found, overwrite it with removal marker\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index] = TOMBSTONE\n            size--\n        }\n    }\n\n    /* Expand hash table */\n    fun extend() {\n        // Temporarily store the original hash table\n        val bucketsTmp = buckets\n        // Initialize expanded new hash table\n        capacity *= extendRatio\n        buckets = arrayOfNulls(capacity)\n        size = 0\n        // Move key-value pairs from original hash table to new hash table\n        for (pair in bucketsTmp) {\n            if (pair != null && pair != TOMBSTONE) {\n                put(pair.key, pair._val)\n            }\n        }\n    }\n\n    /* Print hash table */\n    fun print() {\n        for (pair in buckets) {\n            if (pair == null) {\n                println(\"null\")\n            } else if (pair == TOMBSTONE) {\n                println(\"TOMESTOME\")\n            } else {\n                println(\"${pair.key} -> ${pair._val}\")\n            }\n        }\n    }\n}\n
      hash_map_open_addressing.rb
      ### Hash map with open addressing ###\nclass HashMapOpenAddressing\n  TOMBSTONE = Pair.new(-1, '-1') # Removal marker\n\n  ### Constructor ###\n  def initialize\n    @size = 0 # Number of key-value pairs\n    @capacity = 4 # Hash table capacity\n    @load_thres = 2.0 / 3.0 # Load factor threshold for triggering expansion\n    @extend_ratio = 2 # Expansion multiplier\n    @buckets = Array.new(@capacity) # Bucket array\n  end\n\n  ### Hash function ###\n  def hash_func(key)\n    key % @capacity\n  end\n\n  ### Load factor ###\n  def load_factor\n    @size / @capacity\n  end\n\n  ### Search bucket index for key ###\n  def find_bucket(key)\n    index = hash_func(key)\n    first_tombstone = -1\n    # Linear probing, break when encountering an empty bucket\n    while !@buckets[index].nil?\n      # If key is encountered, return the corresponding bucket index\n      if @buckets[index].key == key\n        # If a removal marker was encountered before, move the key-value pair to that index\n        if first_tombstone != -1\n          @buckets[first_tombstone] = @buckets[index]\n          @buckets[index] = TOMBSTONE\n          return first_tombstone # Return the moved bucket index\n        end\n        return index # Return bucket index\n      end\n      # Record the first removal marker encountered\n      first_tombstone = index if first_tombstone == -1 && @buckets[index] == TOMBSTONE\n      # Calculate bucket index, wrap around to the head if past the tail\n      index = (index + 1) % @capacity\n    end\n    # If key does not exist, return the index for insertion\n    first_tombstone == -1 ? index : first_tombstone\n  end\n\n  ### Query operation ###\n  def get(key)\n    # Search for bucket index corresponding to key\n    index = find_bucket(key)\n    # If key-value pair is found, return corresponding val\n    return @buckets[index].val unless [nil, TOMBSTONE].include?(@buckets[index])\n    # Return nil if key-value pair does not exist\n    nil\n  end\n\n  ### Add operation ###\n  def put(key, val)\n    # When load factor exceeds threshold, perform expansion\n    extend if load_factor > @load_thres\n    # Search for bucket index corresponding to key\n    index = find_bucket(key)\n    # If key-value pair found, overwrite val and return\n    unless [nil, TOMBSTONE].include?(@buckets[index])\n      @buckets[index].val = val\n      return\n    end\n    # If key-value pair does not exist, add the key-value pair\n    @buckets[index] = Pair.new(key, val)\n    @size += 1\n  end\n\n  ### Delete operation ###\n  def remove(key)\n    # Search for bucket index corresponding to key\n    index = find_bucket(key)\n    # If key-value pair is found, overwrite it with removal marker\n    unless [nil, TOMBSTONE].include?(@buckets[index])\n      @buckets[index] = TOMBSTONE\n      @size -= 1\n    end\n  end\n\n  ### Expand hash table ###\n  def extend\n    # Temporarily store the original hash table\n    buckets_tmp = @buckets\n    # Initialize expanded new hash table\n    @capacity *= @extend_ratio\n    @buckets = Array.new(@capacity)\n    @size = 0\n    # Move key-value pairs from original hash table to new hash table\n    for pair in buckets_tmp\n      put(pair.key, pair.val) unless [nil, TOMBSTONE].include?(pair)\n    end\n  end\n\n  ### Print hash table ###\n  def print\n    for pair in @buckets\n      if pair.nil?\n        puts \"Nil\"\n      elsif pair == TOMBSTONE\n        puts \"TOMBSTONE\"\n      else\n        puts \"#{pair.key} -> #{pair.val}\"\n      end\n    end\n  end\nend\n
      "},{"location":"chapter_hashing/hash_collision/#2-quadratic-probing","title":"2. \u00a0 Quadratic Probing","text":"

      Quadratic probing is similar to linear probing and is one of the common strategies for open addressing. When a collision occurs, quadratic probing does not simply skip a fixed number of steps but skips a number of steps equal to the \"square of the number of probes\", i.e., \\(1, 4, 9, \\dots\\) steps.

      Quadratic probing has the following advantages:

      • Quadratic probing attempts to alleviate the clustering effect of linear probing by skipping distances equal to the square of the probe count.
      • Quadratic probing skips larger distances to find empty positions, which helps to distribute data more evenly.

      However, quadratic probing is not perfect:

      • Clustering still exists, i.e., some positions are more likely to be occupied than others.
      • Due to the growth of squares, quadratic probing may not probe the entire hash table, meaning that even if there are empty buckets in the hash table, quadratic probing may not be able to access them.
      "},{"location":"chapter_hashing/hash_collision/#3-double-hashing","title":"3. \u00a0 Double Hashing","text":"

      As the name suggests, the double hashing method uses multiple hash functions \\(f_1(x)\\), \\(f_2(x)\\), \\(f_3(x)\\), \\(\\dots\\) for probing.

      • Inserting elements: If hash function \\(f_1(x)\\) encounters a conflict, try \\(f_2(x)\\), and so on, until an empty position is found and the element is inserted.
      • Searching for elements: Search in the same order of hash functions until the target element is found and return it; if an empty position is encountered or all hash functions have been tried, it indicates the element is not in the hash table, then return None.

      Compared to linear probing, the double hashing method is less prone to clustering, but multiple hash functions introduce additional computational overhead.

      Tip

      Please note that open addressing (linear probing, quadratic probing, and double hashing) hash tables all have the problem of \"cannot directly delete elements\".

      "},{"location":"chapter_hashing/hash_collision/#623-choice-of-programming-languages","title":"6.2.3 \u00a0 Choice of Programming Languages","text":"

      Different programming languages adopt different hash table implementation strategies. Here are a few examples:

      • Python uses open addressing. The dict dictionary uses pseudo-random numbers for probing.
      • Java uses separate chaining. Since JDK 1.8, when the array length in HashMap reaches 64 and the length of a linked list reaches 8, the linked list is converted to a red-black tree to improve search performance.
      • Go uses separate chaining. Go stipulates that each bucket can store up to 8 key-value pairs, and if the capacity is exceeded, an overflow bucket is linked; when there are too many overflow buckets, a special equal-capacity expansion operation is performed to ensure performance.
      "},{"location":"chapter_hashing/hash_map/","title":"6.1 \u00a0 Hash Table","text":"

      A hash table, also known as a hash map, establishes a mapping between keys key and values value, enabling efficient element retrieval. Specifically, when we input a key key into a hash table, we can retrieve the corresponding value value in \\(O(1)\\) time.

      As shown in Figure 6-1, given \\(n\\) students, each with two pieces of data: \"name\" and \"student ID\". If we want to implement a query function that \"inputs a student ID and returns the corresponding name\", we can use the hash table shown below.

      Figure 6-1 \u00a0 Abstract representation of a hash table

      In addition to hash tables, arrays and linked lists can also implement query functionality. Their efficiency comparison is shown in the following table.

      • Adding elements: Simply add elements to the end of the array (linked list), using \\(O(1)\\) time.
      • Querying elements: Since the array (linked list) is unordered, all elements need to be traversed, using \\(O(n)\\) time.
      • Deleting elements: The element must first be located, then deleted from the array (linked list), using \\(O(n)\\) time.

      Table 6-1 \u00a0 Comparison of element query efficiency

      Array Linked List Hash Table Find element \\(O(n)\\) \\(O(n)\\) \\(O(1)\\) Add element \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) Delete element \\(O(n)\\) \\(O(n)\\) \\(O(1)\\)

      As observed, the time complexity for insertion, deletion, search, and modification operations in a hash table is \\(O(1)\\), which is very efficient.

      "},{"location":"chapter_hashing/hash_map/#611-common-hash-table-operations","title":"6.1.1 \u00a0 Common Hash Table Operations","text":"

      Common operations on hash tables include: initialization, query operations, adding key-value pairs, and deleting key-value pairs. Example code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map.py
      # Initialize hash table\nhmap: dict = {}\n\n# Add operation\n# Add key-value pair (key, value) to hash table\nhmap[12836] = \"XiaoHa\"\nhmap[15937] = \"XiaoLuo\"\nhmap[16750] = \"XiaoSuan\"\nhmap[13276] = \"XiaoFa\"\nhmap[10583] = \"XiaoYa\"\n\n# Query operation\n# Input key into hash table to get value\nname: str = hmap[15937]\n\n# Delete operation\n# Delete key-value pair (key, value) from hash table\nhmap.pop(10583)\n
      hash_map.cpp
      /* Initialize hash table */\nunordered_map<int, string> map;\n\n/* Add operation */\n// Add key-value pair (key, value) to hash table\nmap[12836] = \"XiaoHa\";\nmap[15937] = \"XiaoLuo\";\nmap[16750] = \"XiaoSuan\";\nmap[13276] = \"XiaoFa\";\nmap[10583] = \"XiaoYa\";\n\n/* Query operation */\n// Input key into hash table to get value\nstring name = map[15937];\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.erase(10583);\n
      hash_map.java
      /* Initialize hash table */\nMap<Integer, String> map = new HashMap<>();\n\n/* Add operation */\n// Add key-value pair (key, value) to hash table\nmap.put(12836, \"XiaoHa\");\nmap.put(15937, \"XiaoLuo\");\nmap.put(16750, \"XiaoSuan\");\nmap.put(13276, \"XiaoFa\");\nmap.put(10583, \"XiaoYa\");\n\n/* Query operation */\n// Input key into hash table to get value\nString name = map.get(15937);\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.remove(10583);\n
      hash_map.cs
      /* Initialize hash table */\nDictionary<int, string> map = new() {\n    /* Add operation */\n    // Add key-value pair (key, value) to hash table\n    { 12836, \"XiaoHa\" },\n    { 15937, \"XiaoLuo\" },\n    { 16750, \"XiaoSuan\" },\n    { 13276, \"XiaoFa\" },\n    { 10583, \"XiaoYa\" }\n};\n\n/* Query operation */\n// Input key into hash table to get value\nstring name = map[15937];\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.Remove(10583);\n
      hash_map_test.go
      /* Initialize hash table */\nhmap := make(map[int]string)\n\n/* Add operation */\n// Add key-value pair (key, value) to hash table\nhmap[12836] = \"XiaoHa\"\nhmap[15937] = \"XiaoLuo\"\nhmap[16750] = \"XiaoSuan\"\nhmap[13276] = \"XiaoFa\"\nhmap[10583] = \"XiaoYa\"\n\n/* Query operation */\n// Input key into hash table to get value\nname := hmap[15937]\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\ndelete(hmap, 10583)\n
      hash_map.swift
      /* Initialize hash table */\nvar map: [Int: String] = [:]\n\n/* Add operation */\n// Add key-value pair (key, value) to hash table\nmap[12836] = \"XiaoHa\"\nmap[15937] = \"XiaoLuo\"\nmap[16750] = \"XiaoSuan\"\nmap[13276] = \"XiaoFa\"\nmap[10583] = \"XiaoYa\"\n\n/* Query operation */\n// Input key into hash table to get value\nlet name = map[15937]!\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.removeValue(forKey: 10583)\n
      hash_map.js
      /* Initialize hash table */\nconst map = new Map();\n/* Add operation */\n// Add key-value pair (key, value) to hash table\nmap.set(12836, 'XiaoHa');\nmap.set(15937, 'XiaoLuo');\nmap.set(16750, 'XiaoSuan');\nmap.set(13276, 'XiaoFa');\nmap.set(10583, 'XiaoYa');\n\n/* Query operation */\n// Input key into hash table to get value\nlet name = map.get(15937);\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.delete(10583);\n
      hash_map.ts
      /* Initialize hash table */\nconst map = new Map<number, string>();\n/* Add operation */\n// Add key-value pair (key, value) to hash table\nmap.set(12836, 'XiaoHa');\nmap.set(15937, 'XiaoLuo');\nmap.set(16750, 'XiaoSuan');\nmap.set(13276, 'XiaoFa');\nmap.set(10583, 'XiaoYa');\nconsole.info('\\nAfter adding, hash table is\\nKey -> Value');\nconsole.info(map);\n\n/* Query operation */\n// Input key into hash table to get value\nlet name = map.get(15937);\nconsole.info('\\nInput student ID 15937, queried name ' + name);\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.delete(10583);\nconsole.info('\\nAfter deleting 10583, hash table is\\nKey -> Value');\nconsole.info(map);\n
      hash_map.dart
      /* Initialize hash table */\nMap<int, String> map = {};\n\n/* Add operation */\n// Add key-value pair (key, value) to hash table\nmap[12836] = \"XiaoHa\";\nmap[15937] = \"XiaoLuo\";\nmap[16750] = \"XiaoSuan\";\nmap[13276] = \"XiaoFa\";\nmap[10583] = \"XiaoYa\";\n\n/* Query operation */\n// Input key into hash table to get value\nString name = map[15937];\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.remove(10583);\n
      hash_map.rs
      use std::collections::HashMap;\n\n/* Initialize hash table */\nlet mut map: HashMap<i32, String> = HashMap::new();\n\n/* Add operation */\n// Add key-value pair (key, value) to hash table\nmap.insert(12836, \"XiaoHa\".to_string());\nmap.insert(15937, \"XiaoLuo\".to_string());\nmap.insert(16750, \"XiaoSuan\".to_string());\nmap.insert(13279, \"XiaoFa\".to_string());\nmap.insert(10583, \"XiaoYa\".to_string());\n\n/* Query operation */\n// Input key into hash table to get value\nlet _name: Option<&String> = map.get(&15937);\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nlet _removed_value: Option<String> = map.remove(&10583);\n
      hash_map.c
      // C does not provide a built-in hash table\n
      hash_map.kt
      /* Initialize hash table */\nval map = HashMap<Int,String>()\n\n/* Add operation */\n// Add key-value pair (key, value) to hash table\nmap[12836] = \"XiaoHa\"\nmap[15937] = \"XiaoLuo\"\nmap[16750] = \"XiaoSuan\"\nmap[13276] = \"XiaoFa\"\nmap[10583] = \"XiaoYa\"\n\n/* Query operation */\n// Input key into hash table to get value\nval name = map[15937]\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.remove(10583)\n
      hash_map.rb
      # Initialize hash table\nhmap = {}\n\n# Add operation\n# Add key-value pair (key, value) to hash table\nhmap[12836] = \"XiaoHa\"\nhmap[15937] = \"XiaoLuo\"\nhmap[16750] = \"XiaoSuan\"\nhmap[13276] = \"XiaoFa\"\nhmap[10583] = \"XiaoYa\"\n\n# Query operation\n# Input key into hash table to get value\nname = hmap[15937]\n\n# Delete operation\n# Delete key-value pair (key, value) from hash table\nhmap.delete(10583)\n
      Visualized Execution

      https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%93%88%E5%B8%8C%E8%A1%A8%0A%20%20%20%20hmap%20%3D%20%7B%7D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%B7%BB%E5%8A%A0%E6%93%8D%E4%BD%9C%0A%20%20%20%20%23%20%E5%9C%A8%E5%93%88%E5%B8%8C%E8%A1%A8%E4%B8%AD%E6%B7%BB%E5%8A%A0%E9%94%AE%E5%80%BC%E5%AF%B9%20%28key,%20value%29%0A%20%20%20%20hmap%5B12836%5D%20%3D%20%22%E5%B0%8F%E5%93%88%22%0A%20%20%20%20hmap%5B15937%5D%20%3D%20%22%E5%B0%8F%E5%95%B0%22%0A%20%20%20%20hmap%5B16750%5D%20%3D%20%22%E5%B0%8F%E7%AE%97%22%0A%20%20%20%20hmap%5B13276%5D%20%3D%20%22%E5%B0%8F%E6%B3%95%22%0A%20%20%20%20hmap%5B10583%5D%20%3D%20%22%E5%B0%8F%E9%B8%AD%22%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C%0A%20%20%20%20%23%20%E5%90%91%E5%93%88%E5%B8%8C%E8%A1%A8%E4%B8%AD%E8%BE%93%E5%85%A5%E9%94%AE%20key%20%EF%BC%8C%E5%BE%97%E5%88%B0%E5%80%BC%20value%0A%20%20%20%20name%20%3D%20hmap%5B15937%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E6%93%8D%E4%BD%9C%0A%20%20%20%20%23%20%E5%9C%A8%E5%93%88%E5%B8%8C%E8%A1%A8%E4%B8%AD%E5%88%A0%E9%99%A4%E9%94%AE%E5%80%BC%E5%AF%B9%20%28key,%20value%29%0A%20%20%20%20hmap.pop%2810583%29&cumulative=false&curInstr=2&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

      There are three common ways to traverse a hash table: traversing key-value pairs, traversing keys, and traversing values. Example code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map.py
      # Traverse hash table\n# Traverse key-value pairs key->value\nfor key, value in hmap.items():\n    print(key, \"->\", value)\n# Traverse keys only\nfor key in hmap.keys():\n    print(key)\n# Traverse values only\nfor value in hmap.values():\n    print(value)\n
      hash_map.cpp
      /* Traverse hash table */\n// Traverse key-value pairs key->value\nfor (auto kv: map) {\n    cout << kv.first << \" -> \" << kv.second << endl;\n}\n// Traverse using iterator key->value\nfor (auto iter = map.begin(); iter != map.end(); iter++) {\n    cout << iter->first << \"->\" << iter->second << endl;\n}\n
      hash_map.java
      /* Traverse hash table */\n// Traverse key-value pairs key->value\nfor (Map.Entry<Integer, String> kv: map.entrySet()) {\n    System.out.println(kv.getKey() + \" -> \" + kv.getValue());\n}\n// Traverse keys only\nfor (int key: map.keySet()) {\n    System.out.println(key);\n}\n// Traverse values only\nfor (String val: map.values()) {\n    System.out.println(val);\n}\n
      hash_map.cs
      /* Traverse hash table */\n// Traverse key-value pairs Key->Value\nforeach (var kv in map) {\n    Console.WriteLine(kv.Key + \" -> \" + kv.Value);\n}\n// Traverse keys only\nforeach (int key in map.Keys) {\n    Console.WriteLine(key);\n}\n// Traverse values only\nforeach (string val in map.Values) {\n    Console.WriteLine(val);\n}\n
      hash_map_test.go
      /* Traverse hash table */\n// Traverse key-value pairs key->value\nfor key, value := range hmap {\n    fmt.Println(key, \"->\", value)\n}\n// Traverse keys only\nfor key := range hmap {\n    fmt.Println(key)\n}\n// Traverse values only\nfor _, value := range hmap {\n    fmt.Println(value)\n}\n
      hash_map.swift
      /* Traverse hash table */\n// Traverse key-value pairs Key->Value\nfor (key, value) in map {\n    print(\"\\(key) -> \\(value)\")\n}\n// Traverse keys only\nfor key in map.keys {\n    print(key)\n}\n// Traverse values only\nfor value in map.values {\n    print(value)\n}\n
      hash_map.js
      /* Traverse hash table */\nconsole.info('\\nTraverse key-value pairs Key->Value');\nfor (const [k, v] of map.entries()) {\n    console.info(k + ' -> ' + v);\n}\nconsole.info('\\nTraverse keys only Key');\nfor (const k of map.keys()) {\n    console.info(k);\n}\nconsole.info('\\nTraverse values only Value');\nfor (const v of map.values()) {\n    console.info(v);\n}\n
      hash_map.ts
      /* Traverse hash table */\nconsole.info('\\nTraverse key-value pairs Key->Value');\nfor (const [k, v] of map.entries()) {\n    console.info(k + ' -> ' + v);\n}\nconsole.info('\\nTraverse keys only Key');\nfor (const k of map.keys()) {\n    console.info(k);\n}\nconsole.info('\\nTraverse values only Value');\nfor (const v of map.values()) {\n    console.info(v);\n}\n
      hash_map.dart
      /* Traverse hash table */\n// Traverse key-value pairs Key->Value\nmap.forEach((key, value) {\n  print('$key -> $value');\n});\n\n// Traverse keys only\nmap.keys.forEach((key) {\n  print(key);\n});\n\n// Traverse values only\nmap.values.forEach((value) {\n  print(value);\n});\n
      hash_map.rs
      /* Traverse hash table */\n// Traverse key-value pairs Key->Value\nfor (key, value) in &map {\n    println!(\"{key} -> {value}\");\n}\n\n// Traverse keys only\nfor key in map.keys() {\n    println!(\"{key}\");\n}\n\n// Traverse values only\nfor value in map.values() {\n    println!(\"{value}\");\n}\n
      hash_map.c
      // C does not provide a built-in hash table\n
      hash_map.kt
      /* Traverse hash table */\n// Traverse key-value pairs key->value\nfor ((key, value) in map) {\n    println(\"$key -> $value\")\n}\n// Traverse keys only\nfor (key in map.keys) {\n    println(key)\n}\n// Traverse values only\nfor (_val in map.values) {\n    println(_val)\n}\n
      hash_map.rb
      # Traverse hash table\n# Traverse key-value pairs key->value\nhmap.entries.each { |key, value| puts \"#{key} -> #{value}\" }\n\n# Traverse keys only\nhmap.keys.each { |key| puts key }\n\n# Traverse values only\nhmap.values.each { |val| puts val }\n
      Visualized Execution

      https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%93%88%E5%B8%8C%E8%A1%A8%0A%20%20%20%20hmap%20%3D%20%7B%7D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%B7%BB%E5%8A%A0%E6%93%8D%E4%BD%9C%0A%20%20%20%20%23%20%E5%9C%A8%E5%93%88%E5%B8%8C%E8%A1%A8%E4%B8%AD%E6%B7%BB%E5%8A%A0%E9%94%AE%E5%80%BC%E5%AF%B9%20%28key,%20value%29%0A%20%20%20%20hmap%5B12836%5D%20%3D%20%22%E5%B0%8F%E5%93%88%22%0A%20%20%20%20hmap%5B15937%5D%20%3D%20%22%E5%B0%8F%E5%95%B0%22%0A%20%20%20%20hmap%5B16750%5D%20%3D%20%22%E5%B0%8F%E7%AE%97%22%0A%20%20%20%20hmap%5B13276%5D%20%3D%20%22%E5%B0%8F%E6%B3%95%22%0A%20%20%20%20hmap%5B10583%5D%20%3D%20%22%E5%B0%8F%E9%B8%AD%22%0A%20%20%20%20%0A%20%20%20%20%23%20%E9%81%8D%E5%8E%86%E5%93%88%E5%B8%8C%E8%A1%A8%0A%20%20%20%20%23%20%E9%81%8D%E5%8E%86%E9%94%AE%E5%80%BC%E5%AF%B9%20key-%3Evalue%0A%20%20%20%20for%20key,%20value%20in%20hmap.items%28%29%3A%0A%20%20%20%20%20%20%20%20print%28key,%20%22-%3E%22,%20value%29%0A%20%20%20%20%23%20%E5%8D%95%E7%8B%AC%E9%81%8D%E5%8E%86%E9%94%AE%20key%0A%20%20%20%20for%20key%20in%20hmap.keys%28%29%3A%0A%20%20%20%20%20%20%20%20print%28key%29%0A%20%20%20%20%23%20%E5%8D%95%E7%8B%AC%E9%81%8D%E5%8E%86%E5%80%BC%20value%0A%20%20%20%20for%20value%20in%20hmap.values%28%29%3A%0A%20%20%20%20%20%20%20%20print%28value%29&cumulative=false&curInstr=8&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

      "},{"location":"chapter_hashing/hash_map/#612-simple-hash-table-implementation","title":"6.1.2 \u00a0 Simple Hash Table Implementation","text":"

      Let's first consider the simplest case: implementing a hash table using only an array. In a hash table, each empty position in the array is called a bucket, and each bucket can store a key-value pair. Therefore, the query operation is to find the bucket corresponding to key and retrieve the value from the bucket.

      So how do we locate the corresponding bucket based on key? This is achieved through a hash function. The role of the hash function is to map a larger input space to a smaller output space. In a hash table, the input space is all keys, and the output space is all buckets (array indices). In other words, given a key, we can use the hash function to obtain the storage location of the key-value pair corresponding to that key in the array.

      When inputting a key, the hash function's calculation process consists of the following two steps:

      1. Calculate the hash value through a hash algorithm hash().
      2. Take the modulo of the hash value by the number of buckets (array length) capacity to obtain the bucket (array index) index corresponding to that key.
      index = hash(key) % capacity\n

      Subsequently, we can use index to access the corresponding bucket in the hash table and retrieve the value.

      Assuming the array length is capacity = 100 and the hash algorithm is hash(key) = key, the hash function becomes key % 100. Figure 6-2 shows the working principle of the hash function using key as student ID and value as name.

      Figure 6-2 \u00a0 Working principle of hash function

      The following code implements a simple hash table. Here, we encapsulate key and value into a class Pair to represent a key-value pair.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_hash_map.py
      class Pair:\n    \"\"\"Key-value pair\"\"\"\n\n    def __init__(self, key: int, val: str):\n        self.key = key\n        self.val = val\n\nclass ArrayHashMap:\n    \"\"\"Hash table based on array implementation\"\"\"\n\n    def __init__(self):\n        \"\"\"Constructor\"\"\"\n        # Initialize array with 100 buckets\n        self.buckets: list[Pair | None] = [None] * 100\n\n    def hash_func(self, key: int) -> int:\n        \"\"\"Hash function\"\"\"\n        index = key % 100\n        return index\n\n    def get(self, key: int) -> str | None:\n        \"\"\"Query operation\"\"\"\n        index: int = self.hash_func(key)\n        pair: Pair = self.buckets[index]\n        if pair is None:\n            return None\n        return pair.val\n\n    def put(self, key: int, val: str):\n        \"\"\"Add and update operation\"\"\"\n        pair = Pair(key, val)\n        index: int = self.hash_func(key)\n        self.buckets[index] = pair\n\n    def remove(self, key: int):\n        \"\"\"Remove operation\"\"\"\n        index: int = self.hash_func(key)\n        # Set to None to represent removal\n        self.buckets[index] = None\n\n    def entry_set(self) -> list[Pair]:\n        \"\"\"Get all key-value pairs\"\"\"\n        result: list[Pair] = []\n        for pair in self.buckets:\n            if pair is not None:\n                result.append(pair)\n        return result\n\n    def key_set(self) -> list[int]:\n        \"\"\"Get all keys\"\"\"\n        result = []\n        for pair in self.buckets:\n            if pair is not None:\n                result.append(pair.key)\n        return result\n\n    def value_set(self) -> list[str]:\n        \"\"\"Get all values\"\"\"\n        result = []\n        for pair in self.buckets:\n            if pair is not None:\n                result.append(pair.val)\n        return result\n\n    def print(self):\n        \"\"\"Print hash table\"\"\"\n        for pair in self.buckets:\n            if pair is not None:\n                print(pair.key, \"->\", pair.val)\n
      array_hash_map.cpp
      /* Key-value pair */\nstruct Pair {\n  public:\n    int key;\n    string val;\n    Pair(int key, string val) {\n        this->key = key;\n        this->val = val;\n    }\n};\n\n/* Hash table based on array implementation */\nclass ArrayHashMap {\n  private:\n    vector<Pair *> buckets;\n\n  public:\n    ArrayHashMap() {\n        // Initialize array with 100 buckets\n        buckets = vector<Pair *>(100);\n    }\n\n    ~ArrayHashMap() {\n        // Free memory\n        for (const auto &bucket : buckets) {\n            delete bucket;\n        }\n        buckets.clear();\n    }\n\n    /* Hash function */\n    int hashFunc(int key) {\n        int index = key % 100;\n        return index;\n    }\n\n    /* Query operation */\n    string get(int key) {\n        int index = hashFunc(key);\n        Pair *pair = buckets[index];\n        if (pair == nullptr)\n            return \"\";\n        return pair->val;\n    }\n\n    /* Add operation */\n    void put(int key, string val) {\n        Pair *pair = new Pair(key, val);\n        int index = hashFunc(key);\n        buckets[index] = pair;\n    }\n\n    /* Remove operation */\n    void remove(int key) {\n        int index = hashFunc(key);\n        // Free memory and set to nullptr\n        delete buckets[index];\n        buckets[index] = nullptr;\n    }\n\n    /* Get all key-value pairs */\n    vector<Pair *> pairSet() {\n        vector<Pair *> pairSet;\n        for (Pair *pair : buckets) {\n            if (pair != nullptr) {\n                pairSet.push_back(pair);\n            }\n        }\n        return pairSet;\n    }\n\n    /* Get all keys */\n    vector<int> keySet() {\n        vector<int> keySet;\n        for (Pair *pair : buckets) {\n            if (pair != nullptr) {\n                keySet.push_back(pair->key);\n            }\n        }\n        return keySet;\n    }\n\n    /* Get all values */\n    vector<string> valueSet() {\n        vector<string> valueSet;\n        for (Pair *pair : buckets) {\n            if (pair != nullptr) {\n                valueSet.push_back(pair->val);\n            }\n        }\n        return valueSet;\n    }\n\n    /* Print hash table */\n    void print() {\n        for (Pair *kv : pairSet()) {\n            cout << kv->key << \" -> \" << kv->val << endl;\n        }\n    }\n};\n
      array_hash_map.java
      /* Key-value pair */\nclass Pair {\n    public int key;\n    public String val;\n\n    public Pair(int key, String val) {\n        this.key = key;\n        this.val = val;\n    }\n}\n\n/* Hash table based on array implementation */\nclass ArrayHashMap {\n    private List<Pair> buckets;\n\n    public ArrayHashMap() {\n        // Initialize array with 100 buckets\n        buckets = new ArrayList<>();\n        for (int i = 0; i < 100; i++) {\n            buckets.add(null);\n        }\n    }\n\n    /* Hash function */\n    private int hashFunc(int key) {\n        int index = key % 100;\n        return index;\n    }\n\n    /* Query operation */\n    public String get(int key) {\n        int index = hashFunc(key);\n        Pair pair = buckets.get(index);\n        if (pair == null)\n            return null;\n        return pair.val;\n    }\n\n    /* Add operation */\n    public void put(int key, String val) {\n        Pair pair = new Pair(key, val);\n        int index = hashFunc(key);\n        buckets.set(index, pair);\n    }\n\n    /* Remove operation */\n    public void remove(int key) {\n        int index = hashFunc(key);\n        // Set to null to represent deletion\n        buckets.set(index, null);\n    }\n\n    /* Get all key-value pairs */\n    public List<Pair> pairSet() {\n        List<Pair> pairSet = new ArrayList<>();\n        for (Pair pair : buckets) {\n            if (pair != null)\n                pairSet.add(pair);\n        }\n        return pairSet;\n    }\n\n    /* Get all keys */\n    public List<Integer> keySet() {\n        List<Integer> keySet = new ArrayList<>();\n        for (Pair pair : buckets) {\n            if (pair != null)\n                keySet.add(pair.key);\n        }\n        return keySet;\n    }\n\n    /* Get all values */\n    public List<String> valueSet() {\n        List<String> valueSet = new ArrayList<>();\n        for (Pair pair : buckets) {\n            if (pair != null)\n                valueSet.add(pair.val);\n        }\n        return valueSet;\n    }\n\n    /* Print hash table */\n    public void print() {\n        for (Pair kv : pairSet()) {\n            System.out.println(kv.key + \" -> \" + kv.val);\n        }\n    }\n}\n
      array_hash_map.cs
      /* Key-value pair int->string */\nclass Pair(int key, string val) {\n    public int key = key;\n    public string val = val;\n}\n\n/* Hash table based on array implementation */\nclass ArrayHashMap {\n    List<Pair?> buckets;\n    public ArrayHashMap() {\n        // Initialize array with 100 buckets\n        buckets = [];\n        for (int i = 0; i < 100; i++) {\n            buckets.Add(null);\n        }\n    }\n\n    /* Hash function */\n    int HashFunc(int key) {\n        int index = key % 100;\n        return index;\n    }\n\n    /* Query operation */\n    public string? Get(int key) {\n        int index = HashFunc(key);\n        Pair? pair = buckets[index];\n        if (pair == null) return null;\n        return pair.val;\n    }\n\n    /* Add operation */\n    public void Put(int key, string val) {\n        Pair pair = new(key, val);\n        int index = HashFunc(key);\n        buckets[index] = pair;\n    }\n\n    /* Remove operation */\n    public void Remove(int key) {\n        int index = HashFunc(key);\n        // Set to null to represent deletion\n        buckets[index] = null;\n    }\n\n    /* Get all key-value pairs */\n    public List<Pair> PairSet() {\n        List<Pair> pairSet = [];\n        foreach (Pair? pair in buckets) {\n            if (pair != null)\n                pairSet.Add(pair);\n        }\n        return pairSet;\n    }\n\n    /* Get all keys */\n    public List<int> KeySet() {\n        List<int> keySet = [];\n        foreach (Pair? pair in buckets) {\n            if (pair != null)\n                keySet.Add(pair.key);\n        }\n        return keySet;\n    }\n\n    /* Get all values */\n    public List<string> ValueSet() {\n        List<string> valueSet = [];\n        foreach (Pair? pair in buckets) {\n            if (pair != null)\n                valueSet.Add(pair.val);\n        }\n        return valueSet;\n    }\n\n    /* Print hash table */\n    public void Print() {\n        foreach (Pair kv in PairSet()) {\n            Console.WriteLine(kv.key + \" -> \" + kv.val);\n        }\n    }\n}\n
      array_hash_map.go
      /* Key-value pair */\ntype pair struct {\n    key int\n    val string\n}\n\n/* Hash table based on array implementation */\ntype arrayHashMap struct {\n    buckets []*pair\n}\n\n/* Initialize hash table */\nfunc newArrayHashMap() *arrayHashMap {\n    // Initialize array with 100 buckets\n    buckets := make([]*pair, 100)\n    return &arrayHashMap{buckets: buckets}\n}\n\n/* Hash function */\nfunc (a *arrayHashMap) hashFunc(key int) int {\n    index := key % 100\n    return index\n}\n\n/* Query operation */\nfunc (a *arrayHashMap) get(key int) string {\n    index := a.hashFunc(key)\n    pair := a.buckets[index]\n    if pair == nil {\n        return \"Not Found\"\n    }\n    return pair.val\n}\n\n/* Add operation */\nfunc (a *arrayHashMap) put(key int, val string) {\n    pair := &pair{key: key, val: val}\n    index := a.hashFunc(key)\n    a.buckets[index] = pair\n}\n\n/* Remove operation */\nfunc (a *arrayHashMap) remove(key int) {\n    index := a.hashFunc(key)\n    // Set to nil to delete\n    a.buckets[index] = nil\n}\n\n/* Get all key pairs */\nfunc (a *arrayHashMap) pairSet() []*pair {\n    var pairs []*pair\n    for _, pair := range a.buckets {\n        if pair != nil {\n            pairs = append(pairs, pair)\n        }\n    }\n    return pairs\n}\n\n/* Get all keys */\nfunc (a *arrayHashMap) keySet() []int {\n    var keys []int\n    for _, pair := range a.buckets {\n        if pair != nil {\n            keys = append(keys, pair.key)\n        }\n    }\n    return keys\n}\n\n/* Get all values */\nfunc (a *arrayHashMap) valueSet() []string {\n    var values []string\n    for _, pair := range a.buckets {\n        if pair != nil {\n            values = append(values, pair.val)\n        }\n    }\n    return values\n}\n\n/* Print hash table */\nfunc (a *arrayHashMap) print() {\n    for _, pair := range a.buckets {\n        if pair != nil {\n            fmt.Println(pair.key, \"->\", pair.val)\n        }\n    }\n}\n
      array_hash_map.swift
      /* Key-value pair */\nclass Pair: Equatable {\n    public var key: Int\n    public var val: String\n\n    public init(key: Int, val: String) {\n        self.key = key\n        self.val = val\n    }\n\n    public static func == (lhs: Pair, rhs: Pair) -> Bool {\n        lhs.key == rhs.key && lhs.val == rhs.val\n    }\n}\n\n/* Hash table based on array implementation */\nclass ArrayHashMap {\n    private var buckets: [Pair?]\n\n    init() {\n        // Initialize array with 100 buckets\n        buckets = Array(repeating: nil, count: 100)\n    }\n\n    /* Hash function */\n    private func hashFunc(key: Int) -> Int {\n        let index = key % 100\n        return index\n    }\n\n    /* Query operation */\n    func get(key: Int) -> String? {\n        let index = hashFunc(key: key)\n        let pair = buckets[index]\n        return pair?.val\n    }\n\n    /* Add operation */\n    func put(key: Int, val: String) {\n        let pair = Pair(key: key, val: val)\n        let index = hashFunc(key: key)\n        buckets[index] = pair\n    }\n\n    /* Remove operation */\n    func remove(key: Int) {\n        let index = hashFunc(key: key)\n        // Set to nil to delete\n        buckets[index] = nil\n    }\n\n    /* Get all key-value pairs */\n    func pairSet() -> [Pair] {\n        buckets.compactMap { $0 }\n    }\n\n    /* Get all keys */\n    func keySet() -> [Int] {\n        buckets.compactMap { $0?.key }\n    }\n\n    /* Get all values */\n    func valueSet() -> [String] {\n        buckets.compactMap { $0?.val }\n    }\n\n    /* Print hash table */\n    func print() {\n        for pair in pairSet() {\n            Swift.print(\"\\(pair.key) -> \\(pair.val)\")\n        }\n    }\n}\n
      array_hash_map.js
      /* Key-value pair Number -> String */\nclass Pair {\n    constructor(key, val) {\n        this.key = key;\n        this.val = val;\n    }\n}\n\n/* Hash table based on array implementation */\nclass ArrayHashMap {\n    #buckets;\n    constructor() {\n        // Initialize array with 100 buckets\n        this.#buckets = new Array(100).fill(null);\n    }\n\n    /* Hash function */\n    #hashFunc(key) {\n        return key % 100;\n    }\n\n    /* Query operation */\n    get(key) {\n        let index = this.#hashFunc(key);\n        let pair = this.#buckets[index];\n        if (pair === null) return null;\n        return pair.val;\n    }\n\n    /* Add operation */\n    set(key, val) {\n        let index = this.#hashFunc(key);\n        this.#buckets[index] = new Pair(key, val);\n    }\n\n    /* Remove operation */\n    delete(key) {\n        let index = this.#hashFunc(key);\n        // Set to null to represent deletion\n        this.#buckets[index] = null;\n    }\n\n    /* Get all key-value pairs */\n    entries() {\n        let arr = [];\n        for (let i = 0; i < this.#buckets.length; i++) {\n            if (this.#buckets[i]) {\n                arr.push(this.#buckets[i]);\n            }\n        }\n        return arr;\n    }\n\n    /* Get all keys */\n    keys() {\n        let arr = [];\n        for (let i = 0; i < this.#buckets.length; i++) {\n            if (this.#buckets[i]) {\n                arr.push(this.#buckets[i].key);\n            }\n        }\n        return arr;\n    }\n\n    /* Get all values */\n    values() {\n        let arr = [];\n        for (let i = 0; i < this.#buckets.length; i++) {\n            if (this.#buckets[i]) {\n                arr.push(this.#buckets[i].val);\n            }\n        }\n        return arr;\n    }\n\n    /* Print hash table */\n    print() {\n        let pairSet = this.entries();\n        for (const pair of pairSet) {\n            console.info(`${pair.key} -> ${pair.val}`);\n        }\n    }\n}\n
      array_hash_map.ts
      /* Key-value pair Number -> String */\nclass Pair {\n    public key: number;\n    public val: string;\n\n    constructor(key: number, val: string) {\n        this.key = key;\n        this.val = val;\n    }\n}\n\n/* Hash table based on array implementation */\nclass ArrayHashMap {\n    private readonly buckets: (Pair | null)[];\n\n    constructor() {\n        // Initialize array with 100 buckets\n        this.buckets = new Array(100).fill(null);\n    }\n\n    /* Hash function */\n    private hashFunc(key: number): number {\n        return key % 100;\n    }\n\n    /* Query operation */\n    public get(key: number): string | null {\n        let index = this.hashFunc(key);\n        let pair = this.buckets[index];\n        if (pair === null) return null;\n        return pair.val;\n    }\n\n    /* Add operation */\n    public set(key: number, val: string) {\n        let index = this.hashFunc(key);\n        this.buckets[index] = new Pair(key, val);\n    }\n\n    /* Remove operation */\n    public delete(key: number) {\n        let index = this.hashFunc(key);\n        // Set to null to represent deletion\n        this.buckets[index] = null;\n    }\n\n    /* Get all key-value pairs */\n    public entries(): (Pair | null)[] {\n        let arr: (Pair | null)[] = [];\n        for (let i = 0; i < this.buckets.length; i++) {\n            if (this.buckets[i]) {\n                arr.push(this.buckets[i]);\n            }\n        }\n        return arr;\n    }\n\n    /* Get all keys */\n    public keys(): (number | undefined)[] {\n        let arr: (number | undefined)[] = [];\n        for (let i = 0; i < this.buckets.length; i++) {\n            if (this.buckets[i]) {\n                arr.push(this.buckets[i].key);\n            }\n        }\n        return arr;\n    }\n\n    /* Get all values */\n    public values(): (string | undefined)[] {\n        let arr: (string | undefined)[] = [];\n        for (let i = 0; i < this.buckets.length; i++) {\n            if (this.buckets[i]) {\n                arr.push(this.buckets[i].val);\n            }\n        }\n        return arr;\n    }\n\n    /* Print hash table */\n    public print() {\n        let pairSet = this.entries();\n        for (const pair of pairSet) {\n            console.info(`${pair.key} -> ${pair.val}`);\n        }\n    }\n}\n
      array_hash_map.dart
      /* Key-value pair */\nclass Pair {\n  int key;\n  String val;\n  Pair(this.key, this.val);\n}\n\n/* Hash table based on array implementation */\nclass ArrayHashMap {\n  late List<Pair?> _buckets;\n\n  ArrayHashMap() {\n    // Initialize array with 100 buckets\n    _buckets = List.filled(100, null);\n  }\n\n  /* Hash function */\n  int _hashFunc(int key) {\n    final int index = key % 100;\n    return index;\n  }\n\n  /* Query operation */\n  String? get(int key) {\n    final int index = _hashFunc(key);\n    final Pair? pair = _buckets[index];\n    if (pair == null) {\n      return null;\n    }\n    return pair.val;\n  }\n\n  /* Add operation */\n  void put(int key, String val) {\n    final Pair pair = Pair(key, val);\n    final int index = _hashFunc(key);\n    _buckets[index] = pair;\n  }\n\n  /* Remove operation */\n  void remove(int key) {\n    final int index = _hashFunc(key);\n    _buckets[index] = null;\n  }\n\n  /* Get all key-value pairs */\n  List<Pair> pairSet() {\n    List<Pair> pairSet = [];\n    for (final Pair? pair in _buckets) {\n      if (pair != null) {\n        pairSet.add(pair);\n      }\n    }\n    return pairSet;\n  }\n\n  /* Get all keys */\n  List<int> keySet() {\n    List<int> keySet = [];\n    for (final Pair? pair in _buckets) {\n      if (pair != null) {\n        keySet.add(pair.key);\n      }\n    }\n    return keySet;\n  }\n\n  /* Get all values */\n  List<String> values() {\n    List<String> valueSet = [];\n    for (final Pair? pair in _buckets) {\n      if (pair != null) {\n        valueSet.add(pair.val);\n      }\n    }\n    return valueSet;\n  }\n\n  /* Print hash table */\n  void printHashMap() {\n    for (final Pair kv in pairSet()) {\n      print(\"${kv.key} -> ${kv.val}\");\n    }\n  }\n}\n
      array_hash_map.rs
      /* Key-value pair */\n#[derive(Debug, Clone, PartialEq)]\npub struct Pair {\n    pub key: i32,\n    pub val: String,\n}\n\n/* Hash table based on array implementation */\npub struct ArrayHashMap {\n    buckets: Vec<Option<Pair>>,\n}\n\nimpl ArrayHashMap {\n    pub fn new() -> ArrayHashMap {\n        // Initialize array with 100 buckets\n        Self {\n            buckets: vec![None; 100],\n        }\n    }\n\n    /* Hash function */\n    fn hash_func(&self, key: i32) -> usize {\n        key as usize % 100\n    }\n\n    /* Query operation */\n    pub fn get(&self, key: i32) -> Option<&String> {\n        let index = self.hash_func(key);\n        self.buckets[index].as_ref().map(|pair| &pair.val)\n    }\n\n    /* Add operation */\n    pub fn put(&mut self, key: i32, val: &str) {\n        let index = self.hash_func(key);\n        self.buckets[index] = Some(Pair {\n            key,\n            val: val.to_string(),\n        });\n    }\n\n    /* Remove operation */\n    pub fn remove(&mut self, key: i32) {\n        let index = self.hash_func(key);\n        // Set to None to represent removal\n        self.buckets[index] = None;\n    }\n\n    /* Get all key-value pairs */\n    pub fn entry_set(&self) -> Vec<&Pair> {\n        self.buckets\n            .iter()\n            .filter_map(|pair| pair.as_ref())\n            .collect()\n    }\n\n    /* Get all keys */\n    pub fn key_set(&self) -> Vec<&i32> {\n        self.buckets\n            .iter()\n            .filter_map(|pair| pair.as_ref().map(|pair| &pair.key))\n            .collect()\n    }\n\n    /* Get all values */\n    pub fn value_set(&self) -> Vec<&String> {\n        self.buckets\n            .iter()\n            .filter_map(|pair| pair.as_ref().map(|pair| &pair.val))\n            .collect()\n    }\n\n    /* Print hash table */\n    pub fn print(&self) {\n        for pair in self.entry_set() {\n            println!(\"{} -> {}\", pair.key, pair.val);\n        }\n    }\n}\n
      array_hash_map.c
      /* Key-value pair int->string */\ntypedef struct {\n    int key;\n    char *val;\n} Pair;\n\n/* Hash table based on array implementation */\ntypedef struct {\n    Pair *buckets[MAX_SIZE];\n} ArrayHashMap;\n\n/* Constructor */\nArrayHashMap *newArrayHashMap() {\n    ArrayHashMap *hmap = malloc(sizeof(ArrayHashMap));\n    for (int i=0; i < MAX_SIZE; i++) {\n        hmap->buckets[i] = NULL;\n    }\n    return hmap;\n}\n\n/* Destructor */\nvoid delArrayHashMap(ArrayHashMap *hmap) {\n    for (int i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            free(hmap->buckets[i]->val);\n            free(hmap->buckets[i]);\n        }\n    }\n    free(hmap);\n}\n\n/* Add operation */\nvoid put(ArrayHashMap *hmap, const int key, const char *val) {\n    Pair *Pair = malloc(sizeof(Pair));\n    Pair->key = key;\n    Pair->val = malloc(strlen(val) + 1);\n    strcpy(Pair->val, val);\n\n    int index = hashFunc(key);\n    hmap->buckets[index] = Pair;\n}\n\n/* Remove operation */\nvoid removeItem(ArrayHashMap *hmap, const int key) {\n    int index = hashFunc(key);\n    free(hmap->buckets[index]->val);\n    free(hmap->buckets[index]);\n    hmap->buckets[index] = NULL;\n}\n\n/* Get all key-value pairs */\nvoid pairSet(ArrayHashMap *hmap, MapSet *set) {\n    Pair *entries;\n    int i = 0, index = 0;\n    int total = 0;\n    /* Count valid key-value pairs */\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            total++;\n        }\n    }\n    entries = malloc(sizeof(Pair) * total);\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            entries[index].key = hmap->buckets[i]->key;\n            entries[index].val = malloc(strlen(hmap->buckets[i]->val) + 1);\n            strcpy(entries[index].val, hmap->buckets[i]->val);\n            index++;\n        }\n    }\n    set->set = entries;\n    set->len = total;\n}\n\n/* Get all keys */\nvoid keySet(ArrayHashMap *hmap, MapSet *set) {\n    int *keys;\n    int i = 0, index = 0;\n    int total = 0;\n    /* Count valid key-value pairs */\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            total++;\n        }\n    }\n    keys = malloc(total * sizeof(int));\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            keys[index] = hmap->buckets[i]->key;\n            index++;\n        }\n    }\n    set->set = keys;\n    set->len = total;\n}\n\n/* Get all values */\nvoid valueSet(ArrayHashMap *hmap, MapSet *set) {\n    char **vals;\n    int i = 0, index = 0;\n    int total = 0;\n    /* Count valid key-value pairs */\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            total++;\n        }\n    }\n    vals = malloc(total * sizeof(char *));\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            vals[index] = hmap->buckets[i]->val;\n            index++;\n        }\n    }\n    set->set = vals;\n    set->len = total;\n}\n\n/* Print hash table */\nvoid print(ArrayHashMap *hmap) {\n    int i;\n    MapSet set;\n    pairSet(hmap, &set);\n    Pair *entries = (Pair *)set.set;\n    for (i = 0; i < set.len; i++) {\n        printf(\"%d -> %s\\n\", entries[i].key, entries[i].val);\n    }\n    free(set.set);\n}\n
      array_hash_map.kt
      /* Key-value pair */\nclass Pair(\n    var key: Int,\n    var _val: String\n)\n\n/* Hash table based on array implementation */\nclass ArrayHashMap {\n    // Initialize array with 100 buckets\n    private val buckets = arrayOfNulls<Pair>(100)\n\n    /* Hash function */\n    fun hashFunc(key: Int): Int {\n        val index = key % 100\n        return index\n    }\n\n    /* Query operation */\n    fun get(key: Int): String? {\n        val index = hashFunc(key)\n        val pair = buckets[index] ?: return null\n        return pair._val\n    }\n\n    /* Add operation */\n    fun put(key: Int, _val: String) {\n        val pair = Pair(key, _val)\n        val index = hashFunc(key)\n        buckets[index] = pair\n    }\n\n    /* Remove operation */\n    fun remove(key: Int) {\n        val index = hashFunc(key)\n        // Set to null to represent deletion\n        buckets[index] = null\n    }\n\n    /* Get all key-value pairs */\n    fun pairSet(): MutableList<Pair> {\n        val pairSet = mutableListOf<Pair>()\n        for (pair in buckets) {\n            if (pair != null)\n                pairSet.add(pair)\n        }\n        return pairSet\n    }\n\n    /* Get all keys */\n    fun keySet(): MutableList<Int> {\n        val keySet = mutableListOf<Int>()\n        for (pair in buckets) {\n            if (pair != null)\n                keySet.add(pair.key)\n        }\n        return keySet\n    }\n\n    /* Get all values */\n    fun valueSet(): MutableList<String> {\n        val valueSet = mutableListOf<String>()\n        for (pair in buckets) {\n            if (pair != null)\n                valueSet.add(pair._val)\n        }\n        return valueSet\n    }\n\n    /* Print hash table */\n    fun print() {\n        for (kv in pairSet()) {\n            val key = kv.key\n            val _val = kv._val\n            println(\"$key -> $_val\")\n        }\n    }\n}\n
      array_hash_map.rb
      ### Key-value pair ###\nclass Pair\n  attr_accessor :key, :val\n\n  def initialize(key, val)\n    @key = key\n    @val = val\n  end\nend\n\n### Hash map based on array ###\nclass ArrayHashMap\n  ### Constructor ###\n  def initialize\n    # Initialize array with 100 buckets\n    @buckets = Array.new(100)\n  end\n\n  ### Hash function ###\n  def hash_func(key)\n    index = key % 100\n  end\n\n  ### Query operation ###\n  def get(key)\n    index = hash_func(key)\n    pair = @buckets[index]\n\n    return if pair.nil?\n    pair.val\n  end\n\n  ### Add operation ###\n  def put(key, val)\n    pair = Pair.new(key, val)\n    index = hash_func(key)\n    @buckets[index] = pair\n  end\n\n  ### Delete operation ###\n  def remove(key)\n    index = hash_func(key)\n    # Set to nil to delete\n    @buckets[index] = nil\n  end\n\n  ### Get all key-value pairs ###\n  def entry_set\n    result = []\n    @buckets.each { |pair| result << pair unless pair.nil? }\n    result\n  end\n\n  ### Get all keys ###\n  def key_set\n    result = []\n    @buckets.each { |pair| result << pair.key unless pair.nil? }\n    result\n  end\n\n  ### Get all values ###\n  def value_set\n    result = []\n    @buckets.each { |pair| result << pair.val unless pair.nil? }\n    result\n  end\n\n  ### Print hash table ###\n  def print\n    @buckets.each { |pair| puts \"#{pair.key} -> #{pair.val}\" unless pair.nil? }\n  end\nend\n
      "},{"location":"chapter_hashing/hash_map/#613-hash-collision-and-resizing","title":"6.1.3 \u00a0 Hash Collision and Resizing","text":"

      Fundamentally, the role of a hash function is to map the input space consisting of all keys to the output space consisting of all array indices, and the input space is often much larger than the output space. Therefore, theoretically there must be cases where \"multiple inputs correspond to the same output\".

      For the hash function in the above example, when the input keys have the same last two digits, the hash function produces the same output. For example, when querying two students with IDs 12836 and 20336, we get:

      12836 % 100 = 36\n20336 % 100 = 36\n

      As shown in Figure 6-3, two student IDs point to the same name, which is obviously incorrect. We call this situation where multiple inputs correspond to the same output a hash collision.

      Figure 6-3 \u00a0 Hash collision example

      It's easy to see that the larger the hash table capacity \\(n\\), the lower the probability that multiple keys will be assigned to the same bucket, and the fewer collisions. Therefore, we can reduce hash collisions by expanding the hash table.

      As shown in Figure 6-4, before expansion, the key-value pairs (136, A) and (236, D) collided, but after expansion, the collision disappears.

      Figure 6-4 \u00a0 Hash table resizing

      Similar to array expansion, hash table expansion requires migrating all key-value pairs from the original hash table to the new hash table, which is very time-consuming. Moreover, since the hash table capacity capacity changes, we need to recalculate the storage locations of all key-value pairs through the hash function, further increasing the computational overhead of the expansion process. For this reason, programming languages typically reserve a sufficiently large hash table capacity to prevent frequent expansion.

      The load factor is an important concept for hash tables. It is defined as the number of elements in the hash table divided by the number of buckets, and is used to measure the severity of hash collisions. It is also commonly used as a trigger condition for hash table expansion. For example, in Java, when the load factor exceeds \\(0.75\\), the system will expand the hash table to \\(2\\) times its original size.

      "},{"location":"chapter_hashing/summary/","title":"6.4 \u00a0 Summary","text":""},{"location":"chapter_hashing/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • Given an input key, a hash table can retrieve the corresponding value in \\(O(1)\\) time, which is highly efficient.
      • Common hash table operations include querying, adding key-value pairs, deleting key-value pairs, and traversing the hash table.
      • The hash function maps a key to an array index, allowing access to the corresponding bucket and retrieval of the value.
      • Two different keys may end up with the same array index after hashing, leading to erroneous query results. This phenomenon is known as hash collision.
      • The larger the capacity of the hash table, the lower the probability of hash collisions. Therefore, hash table expansion can mitigate hash collisions. Similar to array expansion, hash table expansion is costly.
      • The load factor, defined as the number of elements divided by the number of buckets, reflects the severity of hash collisions and is often used as a condition to trigger hash table expansion.
      • Separate chaining addresses hash collisions by converting each element into a linked list, storing all colliding elements in the same linked list. However, excessively long linked lists can reduce query efficiency, which can be improved by converting the linked lists into red-black trees.
      • Open addressing handles hash collisions through multiple probing. Linear probing uses a fixed step size but cannot delete elements and is prone to clustering. Double hashing uses multiple hash functions for probing, which reduces clustering compared to linear probing but increases computational overhead.
      • Different programming languages adopt various hash table implementations. For example, Java's HashMap uses separate chaining, while Python's dict employs open addressing.
      • In hash tables, we desire hash algorithms with determinism, high efficiency, and uniform distribution. In cryptography, hash algorithms should also possess collision resistance and the avalanche effect.
      • Hash algorithms typically use large prime numbers as moduli to maximize the uniform distribution of hash values and reduce hash collisions.
      • Common hash algorithms include MD5, SHA-1, SHA-2, and SHA-3. MD5 is often used for file integrity checks, while SHA-2 is commonly used in secure applications and protocols.
      • Programming languages usually provide built-in hash algorithms for data types to calculate bucket indices in hash tables. Generally, only immutable objects are hashable.
      "},{"location":"chapter_hashing/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

      Q: When does the time complexity of a hash table degrade to \\(O(n)\\)?

      The time complexity of a hash table can degrade to \\(O(n)\\) when hash collisions are severe. When the hash function is well-designed, the capacity is set appropriately, and collisions are evenly distributed, the time complexity is \\(O(1)\\). We usually consider the time complexity to be \\(O(1)\\) when using built-in hash tables in programming languages.

      Q: Why not use the hash function \\(f(x) = x\\)? This would eliminate collisions.

      Under the hash function \\(f(x) = x\\), each element corresponds to a unique bucket index, which is equivalent to an array. However, the input space is usually much larger than the output space (array length), so the last step of a hash function is often to take the modulo of the array length. In other words, the goal of a hash table is to map a larger state space to a smaller one while providing \\(O(1)\\) query efficiency.

      Q: Why can hash tables be more efficient than arrays, linked lists, or binary trees, even though hash tables are implemented using these structures?

      Firstly, hash tables have higher time efficiency but lower space efficiency. A significant portion of memory in hash tables remains unused.

      Secondly, hash tables are only more time-efficient in specific use cases. If a feature can be implemented with the same time complexity using an array or a linked list, it's usually faster than using a hash table. This is because the computation of the hash function incurs overhead, making the constant factor in the time complexity larger.

      Lastly, the time complexity of hash tables can degrade. For example, in separate chaining, we perform search operations in a linked list or red-black tree, which still risks degrading to \\(O(n)\\) time.

      Q: Does double hashing also have the flaw of not being able to delete elements directly? Can space marked as deleted be reused?

      Double hashing is a form of open addressing, and all open addressing methods have the drawback of not being able to delete elements directly; they require marking elements as deleted. Marked spaces can be reused. When inserting new elements into the hash table, and the hash function points to a position marked as deleted, that position can be used by the new element. This maintains the probing sequence of the hash table while ensuring efficient use of space.

      Q: Why do hash collisions occur during the search process in linear probing?

      During the search process, the hash function points to the corresponding bucket and key-value pair. If the key doesn't match, it indicates a hash collision. Therefore, linear probing will search downward at a predetermined step size until the correct key-value pair is found or the search fails.

      Q: Why can expanding a hash table alleviate hash collisions?

      The last step of a hash function often involves taking the modulo of the array length \\(n\\), to keep the output within the array index range. When expanding, the array length \\(n\\) changes, and the indices corresponding to the keys may also change. Keys that were previously mapped to the same bucket might be distributed across multiple buckets after expansion, thereby mitigating hash collisions.

      "},{"location":"chapter_heap/","title":"Chapter 8. \u00a0 Heap","text":"

      Abstract

      Heaps are like mountain peaks, layered and undulating, each with its unique form.

      The peaks rise and fall at varying heights, yet the tallest peak always catches the eye first.

      "},{"location":"chapter_heap/#chapter-contents","title":"Chapter contents","text":"
      • 8.1 \u00a0 Heap
      • 8.2 \u00a0 Building a Heap
      • 8.3 \u00a0 Top-K Problem
      • 8.4 \u00a0 Summary
      "},{"location":"chapter_heap/build_heap/","title":"8.2 \u00a0 Heap Construction Operation","text":"

      In some cases, we want to build a heap using all elements of a list, and this process is called \"heap construction operation.\"

      "},{"location":"chapter_heap/build_heap/#821-implementing-with-element-insertion","title":"8.2.1 \u00a0 Implementing with Element Insertion","text":"

      We first create an empty heap, then iterate through the list, performing the \"element insertion operation\" on each element in sequence. This means adding the element to the bottom of the heap and then performing \"bottom-to-top\" heapify on that element.

      Each time an element is inserted into the heap, the heap's length increases by one. Since nodes are added to the binary tree sequentially from top to bottom, the heap is constructed \"from top to bottom.\"

      Given \\(n\\) elements, each element's insertion operation takes \\(O(\\log{n})\\) time, so the time complexity of this heap construction method is \\(O(n \\log n)\\).

      "},{"location":"chapter_heap/build_heap/#822-implementing-through-heapify-traversal","title":"8.2.2 \u00a0 Implementing Through Heapify Traversal","text":"

      In fact, we can implement a more efficient heap construction method in two steps.

      1. Add all elements of the list as-is to the heap, at which point the heap property is not yet satisfied.
      2. Traverse the heap in reverse order (reverse of level-order traversal), performing \"top-to-bottom heapify\" on each non-leaf node in sequence.

      After heapifying a node, the subtree rooted at that node becomes a valid sub-heap. Since we traverse in reverse order, the heap is constructed \"from bottom to top.\"

      The reason for choosing reverse order traversal is that it ensures the subtree below the current node is already a valid sub-heap, making the heapification of the current node effective.

      It's worth noting that since leaf nodes have no children, they are naturally valid sub-heaps and do not require heapification. As shown in the code below, the last non-leaf node is the parent of the last node; we start from it and traverse in reverse order to perform heapification:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
      def __init__(self, nums: list[int]):\n    \"\"\"Constructor, build heap based on input list\"\"\"\n    # Add list elements to heap as is\n    self.max_heap = nums\n    # Heapify all nodes except leaf nodes\n    for i in range(self.parent(self.size() - 1), -1, -1):\n        self.sift_down(i)\n
      my_heap.cpp
      /* Constructor, build heap based on input list */\nMaxHeap(vector<int> nums) {\n    // Add list elements to heap as is\n    maxHeap = nums;\n    // Heapify all nodes except leaf nodes\n    for (int i = parent(size() - 1); i >= 0; i--) {\n        siftDown(i);\n    }\n}\n
      my_heap.java
      /* Constructor, build heap based on input list */\nMaxHeap(List<Integer> nums) {\n    // Add list elements to heap as is\n    maxHeap = new ArrayList<>(nums);\n    // Heapify all nodes except leaf nodes\n    for (int i = parent(size() - 1); i >= 0; i--) {\n        siftDown(i);\n    }\n}\n
      my_heap.cs
      /* Constructor, build heap from input list */\nMaxHeap(IEnumerable<int> nums) {\n    // Add list elements to heap as is\n    maxHeap = new List<int>(nums);\n    // Heapify all nodes except leaf nodes\n    var size = Parent(this.Size() - 1);\n    for (int i = size; i >= 0; i--) {\n        SiftDown(i);\n    }\n}\n
      my_heap.go
      /* Constructor, build heap from slice */\nfunc newMaxHeap(nums []any) *maxHeap {\n    // Add list elements to heap as is\n    h := &maxHeap{data: nums}\n    for i := h.parent(len(h.data) - 1); i >= 0; i-- {\n        // Heapify all nodes except leaf nodes\n        h.siftDown(i)\n    }\n    return h\n}\n
      my_heap.swift
      /* Constructor, build heap based on input list */\ninit(nums: [Int]) {\n    // Add list elements to heap as is\n    maxHeap = nums\n    // Heapify all nodes except leaf nodes\n    for i in (0 ... parent(i: size() - 1)).reversed() {\n        siftDown(i: i)\n    }\n}\n
      my_heap.js
      /* Constructor, build empty heap or build heap from input list */\nconstructor(nums) {\n    // Add list elements to heap as is\n    this.#maxHeap = nums === undefined ? [] : [...nums];\n    // Heapify all nodes except leaf nodes\n    for (let i = this.#parent(this.size() - 1); i >= 0; i--) {\n        this.#siftDown(i);\n    }\n}\n
      my_heap.ts
      /* Constructor, build empty heap or build heap from input list */\nconstructor(nums?: number[]) {\n    // Add list elements to heap as is\n    this.maxHeap = nums === undefined ? [] : [...nums];\n    // Heapify all nodes except leaf nodes\n    for (let i = this.parent(this.size() - 1); i >= 0; i--) {\n        this.siftDown(i);\n    }\n}\n
      my_heap.dart
      /* Constructor, build heap based on input list */\nMaxHeap(List<int> nums) {\n  // Add list elements to heap as is\n  _maxHeap = nums;\n  // Heapify all nodes except leaf nodes\n  for (int i = _parent(size() - 1); i >= 0; i--) {\n    siftDown(i);\n  }\n}\n
      my_heap.rs
      /* Constructor, build heap based on input list */\nfn new(nums: Vec<i32>) -> Self {\n    // Add list elements to heap as is\n    let mut heap = MaxHeap { max_heap: nums };\n    // Heapify all nodes except leaf nodes\n    for i in (0..=Self::parent(heap.size() - 1)).rev() {\n        heap.sift_down(i);\n    }\n    heap\n}\n
      my_heap.c
      /* Constructor, build heap from slice */\nMaxHeap *newMaxHeap(int nums[], int size) {\n    // Push all elements to heap\n    MaxHeap *maxHeap = (MaxHeap *)malloc(sizeof(MaxHeap));\n    maxHeap->size = size;\n    memcpy(maxHeap->data, nums, size * sizeof(int));\n    for (int i = parent(maxHeap, size - 1); i >= 0; i--) {\n        // Heapify all nodes except leaf nodes\n        siftDown(maxHeap, i);\n    }\n    return maxHeap;\n}\n
      my_heap.kt
      /* Max heap */\nclass MaxHeap(nums: MutableList<Int>?) {\n    // Use list instead of array, no need to consider capacity expansion\n    private val maxHeap = mutableListOf<Int>()\n\n    /* Constructor, build heap based on input list */\n    init {\n        // Add list elements to heap as is\n        maxHeap.addAll(nums!!)\n        // Heapify all nodes except leaf nodes\n        for (i in parent(size() - 1) downTo 0) {\n            siftDown(i)\n        }\n    }\n\n    /* Get index of left child node */\n    private fun left(i: Int): Int {\n        return 2 * i + 1\n    }\n\n    /* Get index of right child node */\n    private fun right(i: Int): Int {\n        return 2 * i + 2\n    }\n\n    /* Get index of parent node */\n    private fun parent(i: Int): Int {\n        return (i - 1) / 2 // Floor division\n    }\n\n    /* Swap elements */\n    private fun swap(i: Int, j: Int) {\n        val temp = maxHeap[i]\n        maxHeap[i] = maxHeap[j]\n        maxHeap[j] = temp\n    }\n\n    /* Get heap size */\n    fun size(): Int {\n        return maxHeap.size\n    }\n\n    /* Check if heap is empty */\n    fun isEmpty(): Boolean {\n        /* Check if heap is empty */\n        return size() == 0\n    }\n\n    /* Access top element */\n    fun peek(): Int {\n        return maxHeap[0]\n    }\n\n    /* Element enters heap */\n    fun push(_val: Int) {\n        // Add node\n        maxHeap.add(_val)\n        // Heapify from bottom to top\n        siftUp(size() - 1)\n    }\n\n    /* Starting from node i, heapify from bottom to top */\n    private fun siftUp(it: Int) {\n        // Kotlin function parameters are immutable, so create temporary variable\n        var i = it\n        while (true) {\n            // Get parent node of node i\n            val p = parent(i)\n            // When \"crossing root node\" or \"node needs no repair\", end heapify\n            if (p < 0 || maxHeap[i] <= maxHeap[p]) break\n            // Swap two nodes\n            swap(i, p)\n            // Loop upward heapify\n            i = p\n        }\n    }\n\n    /* Element exits heap */\n    fun pop(): Int {\n        // Handle empty case\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        // Delete node\n        swap(0, size() - 1)\n        // Remove node\n        val _val = maxHeap.removeAt(size() - 1)\n        // Return top element\n        siftDown(0)\n        // Return heap top element\n        return _val\n    }\n\n    /* Starting from node i, heapify from top to bottom */\n    private fun siftDown(it: Int) {\n        // Kotlin function parameters are immutable, so create temporary variable\n        var i = it\n        while (true) {\n            // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n            val l = left(i)\n            val r = right(i)\n            var ma = i\n            if (l < size() && maxHeap[l] > maxHeap[ma]) ma = l\n            if (r < size() && maxHeap[r] > maxHeap[ma]) ma = r\n            // Swap two nodes\n            if (ma == i) break\n            // Swap two nodes\n            swap(i, ma)\n            // Loop downwards heapification\n            i = ma\n        }\n    }\n\n    /* Driver Code */\n    fun print() {\n        val queue = PriorityQueue { a: Int, b: Int -> b - a }\n        queue.addAll(maxHeap)\n        printHeap(queue)\n    }\n}\n
      my_heap.rb
      ### Constructor, build heap from input list ###\ndef initialize(nums)\n  # Add list elements to heap as is\n  @max_heap = nums\n  # Heapify all nodes except leaf nodes\n  parent(size - 1).downto(0) do |i|\n    sift_down(i)\n  end\nend\n
      "},{"location":"chapter_heap/build_heap/#823-complexity-analysis","title":"8.2.3 \u00a0 Complexity Analysis","text":"

      Next, let's attempt to derive the time complexity of this second heap construction method.

      • Assuming the complete binary tree has \\(n\\) nodes, then the number of leaf nodes is \\((n + 1) / 2\\), where \\(/\\) is floor division. Therefore, the number of nodes that need heapification is \\((n - 1) / 2\\).
      • In the top-to-bottom heapify process, each node is heapified at most to the leaf nodes, so the maximum number of iterations is the binary tree height \\(\\log n\\).

      Multiplying these two together, we get a time complexity of \\(O(n \\log n)\\) for the heap construction process. However, this estimate is not accurate because it doesn't account for the property that binary trees have far more nodes at lower levels than at upper levels.

      Let's perform a more accurate calculation. To reduce calculation difficulty, assume a \"perfect binary tree\" with \\(n\\) nodes and height \\(h\\); this assumption does not affect the correctness of the result.

      Figure 8-5 \u00a0 Node count at each level of a perfect binary tree

      As shown in Figure 8-5, the maximum number of iterations for a node's \"top-to-bottom heapify\" equals the distance from that node to the leaf nodes, which is precisely the \"node height.\" Therefore, we can sum the \"number of nodes \\(\\times\\) node height\" at each level to obtain the total number of heapify iterations for all nodes.

      \\[ T(h) = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{(h-1)}\\times1 \\]

      To simplify the above expression, we need to use sequence knowledge from high school. First, multiply \\(T(h)\\) by \\(2\\) to get:

      \\[ \\begin{aligned} T(h) & = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{h-1}\\times1 \\newline 2 T(h) & = 2^1h + 2^2(h-1) + 2^3(h-2) + \\dots + 2^{h}\\times1 \\newline \\end{aligned} \\]

      Using the method of differences, subtract the first equation \\(T(h)\\) from the second equation \\(2 T(h)\\) to get:

      \\[ 2T(h) - T(h) = T(h) = -2^0h + 2^1 + 2^2 + \\dots + 2^{h-1} + 2^h \\]

      Observing the above expression, we find that \\(T(h)\\) is a geometric series, which can be calculated directly using the sum formula, yielding a time complexity of:

      \\[ \\begin{aligned} T(h) & = 2 \\frac{1 - 2^h}{1 - 2} - h \\newline & = 2^{h+1} - h - 2 \\newline & = O(2^h) \\end{aligned} \\]

      Furthermore, a perfect binary tree with height \\(h\\) has \\(n = 2^{h+1} - 1\\) nodes, so the complexity is \\(O(2^h) = O(n)\\). This derivation shows that the time complexity of building a heap from an input list is \\(O(n)\\), which is highly efficient.

      "},{"location":"chapter_heap/heap/","title":"8.1 \u00a0 Heap","text":"

      A heap is a complete binary tree that satisfies specific conditions and can be mainly categorized into two types, as shown in Figure 8-1.

      • min heap: The value of any node \\(\\leq\\) the values of its child nodes.
      • max heap: The value of any node \\(\\geq\\) the values of its child nodes.

      Figure 8-1 \u00a0 Min heap and max heap

      As a special case of a complete binary tree, heaps have the following characteristics.

      • The bottom layer nodes are filled from left to right, and nodes in other layers are fully filled.
      • We call the root node of the binary tree the \"heap top\" and the bottom-rightmost node the \"heap bottom.\"
      • For max heaps (min heaps), the value of the heap top element (root node) is the largest (smallest).
      "},{"location":"chapter_heap/heap/#811-common-heap-operations","title":"8.1.1 \u00a0 Common Heap Operations","text":"

      It should be noted that many programming languages provide a priority queue, which is an abstract data structure defined as a queue with priority sorting.

      In fact, heaps are typically used to implement priority queues, with max heaps corresponding to priority queues where elements are dequeued in descending order. From a usage perspective, we can regard \"priority queue\" and \"heap\" as equivalent data structures. Therefore, this book does not make a special distinction between the two and uniformly refers to them as \"heap.\"

      Common heap operations are shown in Table 8-1, and method names need to be determined based on the programming language.

      Table 8-1 \u00a0 Efficiency of Heap Operations

      Method name Description Time complexity push() Insert an element into the heap \\(O(\\log n)\\) pop() Remove the heap top element \\(O(\\log n)\\) peek() Access the heap top element (max/min value for max/min heap) \\(O(1)\\) size() Get the number of elements in the heap \\(O(1)\\) isEmpty() Check if the heap is empty \\(O(1)\\)

      In practical applications, we can directly use the heap class (or priority queue class) provided by programming languages.

      Similar to \"ascending order\" and \"descending order\" in sorting algorithms, we can implement conversion between \"min heap\" and \"max heap\" by setting a flag or modifying the Comparator. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby heap.py
      # Initialize a min heap\nmin_heap, flag = [], 1\n# Initialize a max heap\nmax_heap, flag = [], -1\n\n# Python's heapq module implements a min heap by default\n# Consider negating elements before pushing them to the heap, which inverts the size relationship and thus implements a max heap\n# In this example, flag = 1 corresponds to a min heap, flag = -1 corresponds to a max heap\n\n# Push elements into the heap\nheapq.heappush(max_heap, flag * 1)\nheapq.heappush(max_heap, flag * 3)\nheapq.heappush(max_heap, flag * 2)\nheapq.heappush(max_heap, flag * 5)\nheapq.heappush(max_heap, flag * 4)\n\n# Get the heap top element\npeek: int = flag * max_heap[0] # 5\n\n# Remove the heap top element\n# The removed elements will form a descending sequence\nval = flag * heapq.heappop(max_heap) # 5\nval = flag * heapq.heappop(max_heap) # 4\nval = flag * heapq.heappop(max_heap) # 3\nval = flag * heapq.heappop(max_heap) # 2\nval = flag * heapq.heappop(max_heap) # 1\n\n# Get the heap size\nsize: int = len(max_heap)\n\n# Check if the heap is empty\nis_empty: bool = not max_heap\n\n# Build a heap from an input list\nmin_heap: list[int] = [1, 3, 2, 5, 4]\nheapq.heapify(min_heap)\n
      heap.cpp
      /* Initialize a heap */\n// Initialize a min heap\npriority_queue<int, vector<int>, greater<int>> minHeap;\n// Initialize a max heap\npriority_queue<int, vector<int>, less<int>> maxHeap;\n\n/* Push elements into the heap */\nmaxHeap.push(1);\nmaxHeap.push(3);\nmaxHeap.push(2);\nmaxHeap.push(5);\nmaxHeap.push(4);\n\n/* Get the heap top element */\nint peek = maxHeap.top(); // 5\n\n/* Remove the heap top element */\n// The removed elements will form a descending sequence\nmaxHeap.pop(); // 5\nmaxHeap.pop(); // 4\nmaxHeap.pop(); // 3\nmaxHeap.pop(); // 2\nmaxHeap.pop(); // 1\n\n/* Get the heap size */\nint size = maxHeap.size();\n\n/* Check if the heap is empty */\nbool isEmpty = maxHeap.empty();\n\n/* Build a heap from an input list */\nvector<int> input{1, 3, 2, 5, 4};\npriority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());\n
      heap.java
      /* Initialize a heap */\n// Initialize a min heap\nQueue<Integer> minHeap = new PriorityQueue<>();\n// Initialize a max heap (use lambda expression to modify Comparator)\nQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);\n\n/* Push elements into the heap */\nmaxHeap.offer(1);\nmaxHeap.offer(3);\nmaxHeap.offer(2);\nmaxHeap.offer(5);\nmaxHeap.offer(4);\n\n/* Get the heap top element */\nint peek = maxHeap.peek(); // 5\n\n/* Remove the heap top element */\n// The removed elements will form a descending sequence\npeek = maxHeap.poll(); // 5\npeek = maxHeap.poll(); // 4\npeek = maxHeap.poll(); // 3\npeek = maxHeap.poll(); // 2\npeek = maxHeap.poll(); // 1\n\n/* Get the heap size */\nint size = maxHeap.size();\n\n/* Check if the heap is empty */\nboolean isEmpty = maxHeap.isEmpty();\n\n/* Build a heap from an input list */\nminHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));\n
      heap.cs
      /* Initialize a heap */\n// Initialize a min heap\nPriorityQueue<int, int> minHeap = new();\n// Initialize a max heap (use lambda expression to modify Comparer)\nPriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y.CompareTo(x)));\n\n/* Push elements into the heap */\nmaxHeap.Enqueue(1, 1);\nmaxHeap.Enqueue(3, 3);\nmaxHeap.Enqueue(2, 2);\nmaxHeap.Enqueue(5, 5);\nmaxHeap.Enqueue(4, 4);\n\n/* Get the heap top element */\nint peek = maxHeap.Peek();//5\n\n/* Remove the heap top element */\n// The removed elements will form a descending sequence\npeek = maxHeap.Dequeue();  // 5\npeek = maxHeap.Dequeue();  // 4\npeek = maxHeap.Dequeue();  // 3\npeek = maxHeap.Dequeue();  // 2\npeek = maxHeap.Dequeue();  // 1\n\n/* Get the heap size */\nint size = maxHeap.Count;\n\n/* Check if the heap is empty */\nbool isEmpty = maxHeap.Count == 0;\n\n/* Build a heap from an input list */\nminHeap = new PriorityQueue<int, int>([(1, 1), (3, 3), (2, 2), (5, 5), (4, 4)]);\n
      heap.go
      // In Go, we can construct a max heap of integers by implementing heap.Interface\n// Implementing heap.Interface also requires implementing sort.Interface\ntype intHeap []any\n\n// Push implements the heap.Interface method for pushing an element into the heap\nfunc (h *intHeap) Push(x any) {\n    // Push and Pop use pointer receiver as parameters\n    // because they not only adjust the slice contents but also modify the slice length\n    *h = append(*h, x.(int))\n}\n\n// Pop implements the heap.Interface method for popping the heap top element\nfunc (h *intHeap) Pop() any {\n    // The element to be removed is stored at the end\n    last := (*h)[len(*h)-1]\n    *h = (*h)[:len(*h)-1]\n    return last\n}\n\n// Len is a sort.Interface method\nfunc (h *intHeap) Len() int {\n    return len(*h)\n}\n\n// Less is a sort.Interface method\nfunc (h *intHeap) Less(i, j int) bool {\n    // To implement a min heap, change this to a less-than sign\n    return (*h)[i].(int) > (*h)[j].(int)\n}\n\n// Swap is a sort.Interface method\nfunc (h *intHeap) Swap(i, j int) {\n    (*h)[i], (*h)[j] = (*h)[j], (*h)[i]\n}\n\n// Top gets the heap top element\nfunc (h *intHeap) Top() any {\n    return (*h)[0]\n}\n\n/* Driver Code */\nfunc TestHeap(t *testing.T) {\n    /* Initialize a heap */\n    // Initialize a max heap\n    maxHeap := &intHeap{}\n    heap.Init(maxHeap)\n    /* Push elements into the heap */\n    // Call heap.Interface methods to add elements\n    heap.Push(maxHeap, 1)\n    heap.Push(maxHeap, 3)\n    heap.Push(maxHeap, 2)\n    heap.Push(maxHeap, 4)\n    heap.Push(maxHeap, 5)\n\n    /* Get the heap top element */\n    top := maxHeap.Top()\n    fmt.Printf(\"Heap top element is %d\\n\", top)\n\n    /* Remove the heap top element */\n    // Call heap.Interface methods to remove elements\n    heap.Pop(maxHeap) // 5\n    heap.Pop(maxHeap) // 4\n    heap.Pop(maxHeap) // 3\n    heap.Pop(maxHeap) // 2\n    heap.Pop(maxHeap) // 1\n\n    /* Get the heap size */\n    size := len(*maxHeap)\n    fmt.Printf(\"Number of heap elements is %d\\n\", size)\n\n    /* Check if the heap is empty */\n    isEmpty := len(*maxHeap) == 0\n    fmt.Printf(\"Is the heap empty? %t\\n\", isEmpty)\n}\n
      heap.swift
      /* Initialize a heap */\n// Swift's Heap type supports both max heaps and min heaps, and requires importing swift-collections\nvar heap = Heap<Int>()\n\n/* Push elements into the heap */\nheap.insert(1)\nheap.insert(3)\nheap.insert(2)\nheap.insert(5)\nheap.insert(4)\n\n/* Get the heap top element */\nvar peek = heap.max()!\n\n/* Remove the heap top element */\npeek = heap.removeMax() // 5\npeek = heap.removeMax() // 4\npeek = heap.removeMax() // 3\npeek = heap.removeMax() // 2\npeek = heap.removeMax() // 1\n\n/* Get the heap size */\nlet size = heap.count\n\n/* Check if the heap is empty */\nlet isEmpty = heap.isEmpty\n\n/* Build a heap from an input list */\nlet heap2 = Heap([1, 3, 2, 5, 4])\n
      heap.js
      // JavaScript does not provide a built-in Heap class\n
      heap.ts
      // TypeScript does not provide a built-in Heap class\n
      heap.dart
      // Dart does not provide a built-in Heap class\n
      heap.rs
      use std::collections::BinaryHeap;\nuse std::cmp::Reverse;\n\n/* Initialize a heap */\n// Initialize a min heap\nlet mut min_heap = BinaryHeap::<Reverse<i32>>::new();\n// Initialize a max heap\nlet mut max_heap = BinaryHeap::new();\n\n/* Push elements into the heap */\nmax_heap.push(1);\nmax_heap.push(3);\nmax_heap.push(2);\nmax_heap.push(5);\nmax_heap.push(4);\n\n/* Get the heap top element */\nlet peek = max_heap.peek().unwrap();  // 5\n\n/* Remove the heap top element */\n// The removed elements will form a descending sequence\nlet peek = max_heap.pop().unwrap();   // 5\nlet peek = max_heap.pop().unwrap();   // 4\nlet peek = max_heap.pop().unwrap();   // 3\nlet peek = max_heap.pop().unwrap();   // 2\nlet peek = max_heap.pop().unwrap();   // 1\n\n/* Get the heap size */\nlet size = max_heap.len();\n\n/* Check if the heap is empty */\nlet is_empty = max_heap.is_empty();\n\n/* Build a heap from an input list */\nlet min_heap = BinaryHeap::from(vec![Reverse(1), Reverse(3), Reverse(2), Reverse(5), Reverse(4)]);\n
      heap.c
      // C does not provide a built-in Heap class\n
      heap.kt
      /* Initialize a heap */\n// Initialize a min heap\nvar minHeap = PriorityQueue<Int>()\n// Initialize a max heap (use lambda expression to modify Comparator)\nval maxHeap = PriorityQueue { a: Int, b: Int -> b - a }\n\n/* Push elements into the heap */\nmaxHeap.offer(1)\nmaxHeap.offer(3)\nmaxHeap.offer(2)\nmaxHeap.offer(5)\nmaxHeap.offer(4)\n\n/* Get the heap top element */\nvar peek = maxHeap.peek() // 5\n\n/* Remove the heap top element */\n// The removed elements will form a descending sequence\npeek = maxHeap.poll() // 5\npeek = maxHeap.poll() // 4\npeek = maxHeap.poll() // 3\npeek = maxHeap.poll() // 2\npeek = maxHeap.poll() // 1\n\n/* Get the heap size */\nval size = maxHeap.size\n\n/* Check if the heap is empty */\nval isEmpty = maxHeap.isEmpty()\n\n/* Build a heap from an input list */\nminHeap = PriorityQueue(mutableListOf(1, 3, 2, 5, 4))\n
      heap.rb
      # Ruby does not provide a built-in Heap class\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_heap/heap/#812-implementation-of-the-heap","title":"8.1.2 \u00a0 Implementation of the Heap","text":"

      The following implementation is of a max heap. To convert it to a min heap, simply invert all size logic comparisons (for example, replace \\(\\geq\\) with \\(\\leq\\)). Interested readers are encouraged to implement this on their own.

      "},{"location":"chapter_heap/heap/#1-heap-storage-and-representation","title":"1. \u00a0 Heap Storage and Representation","text":"

      As mentioned in the \"Binary Tree\" chapter, complete binary trees are well-suited for array representation. Since heaps are a type of complete binary tree, we will use arrays to store heaps.

      When representing a binary tree with an array, elements represent node values, and indexes represent node positions in the binary tree. Node pointers are implemented through index mapping formulas.

      As shown in Figure 8-2, given an index \\(i\\), the index of its left child is \\(2i + 1\\), the index of its right child is \\(2i + 2\\), and the index of its parent is \\((i - 1) / 2\\) (floor division). When an index is out of bounds, it indicates a null node or that the node does not exist.

      Figure 8-2 \u00a0 Representation and storage of heaps

      We can encapsulate the index mapping formula into functions for convenient subsequent use:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
      def left(self, i: int) -> int:\n    \"\"\"Get index of left child node\"\"\"\n    return 2 * i + 1\n\ndef right(self, i: int) -> int:\n    \"\"\"Get index of right child node\"\"\"\n    return 2 * i + 2\n\ndef parent(self, i: int) -> int:\n    \"\"\"Get index of parent node\"\"\"\n    return (i - 1) // 2  # Floor division\n
      my_heap.cpp
      /* Get index of left child node */\nint left(int i) {\n    return 2 * i + 1;\n}\n\n/* Get index of right child node */\nint right(int i) {\n    return 2 * i + 2;\n}\n\n/* Get index of parent node */\nint parent(int i) {\n    return (i - 1) / 2; // Floor division\n}\n
      my_heap.java
      /* Get index of left child node */\nint left(int i) {\n    return 2 * i + 1;\n}\n\n/* Get index of right child node */\nint right(int i) {\n    return 2 * i + 2;\n}\n\n/* Get index of parent node */\nint parent(int i) {\n    return (i - 1) / 2; // Floor division\n}\n
      my_heap.cs
      /* Get index of left child node */\nint Left(int i) {\n    return 2 * i + 1;\n}\n\n/* Get index of right child node */\nint Right(int i) {\n    return 2 * i + 2;\n}\n\n/* Get index of parent node */\nint Parent(int i) {\n    return (i - 1) / 2; // Floor division\n}\n
      my_heap.go
      /* Get index of left child node */\nfunc (h *maxHeap) left(i int) int {\n    return 2*i + 1\n}\n\n/* Get index of right child node */\nfunc (h *maxHeap) right(i int) int {\n    return 2*i + 2\n}\n\n/* Get index of parent node */\nfunc (h *maxHeap) parent(i int) int {\n    // Floor division\n    return (i - 1) / 2\n}\n
      my_heap.swift
      /* Get index of left child node */\nfunc left(i: Int) -> Int {\n    2 * i + 1\n}\n\n/* Get index of right child node */\nfunc right(i: Int) -> Int {\n    2 * i + 2\n}\n\n/* Get index of parent node */\nfunc parent(i: Int) -> Int {\n    (i - 1) / 2 // Floor division\n}\n
      my_heap.js
      /* Get index of left child node */\n#left(i) {\n    return 2 * i + 1;\n}\n\n/* Get index of right child node */\n#right(i) {\n    return 2 * i + 2;\n}\n\n/* Get index of parent node */\n#parent(i) {\n    return Math.floor((i - 1) / 2); // Floor division\n}\n
      my_heap.ts
      /* Get index of left child node */\nleft(i: number): number {\n    return 2 * i + 1;\n}\n\n/* Get index of right child node */\nright(i: number): number {\n    return 2 * i + 2;\n}\n\n/* Get index of parent node */\nparent(i: number): number {\n    return Math.floor((i - 1) / 2); // Floor division\n}\n
      my_heap.dart
      /* Get index of left child node */\nint _left(int i) {\n  return 2 * i + 1;\n}\n\n/* Get index of right child node */\nint _right(int i) {\n  return 2 * i + 2;\n}\n\n/* Get index of parent node */\nint _parent(int i) {\n  return (i - 1) ~/ 2; // Floor division\n}\n
      my_heap.rs
      /* Get index of left child node */\nfn left(i: usize) -> usize {\n    2 * i + 1\n}\n\n/* Get index of right child node */\nfn right(i: usize) -> usize {\n    2 * i + 2\n}\n\n/* Get index of parent node */\nfn parent(i: usize) -> usize {\n    (i - 1) / 2 // Floor division\n}\n
      my_heap.c
      /* Get index of left child node */\nint left(MaxHeap *maxHeap, int i) {\n    return 2 * i + 1;\n}\n\n/* Get index of right child node */\nint right(MaxHeap *maxHeap, int i) {\n    return 2 * i + 2;\n}\n\n/* Get index of parent node */\nint parent(MaxHeap *maxHeap, int i) {\n    return (i - 1) / 2; // Round down\n}\n
      my_heap.kt
      /* Get index of left child node */\nfun left(i: Int): Int {\n    return 2 * i + 1\n}\n\n/* Get index of right child node */\nfun right(i: Int): Int {\n    return 2 * i + 2\n}\n\n/* Get index of parent node */\nfun parent(i: Int): Int {\n    return (i - 1) / 2 // Floor division\n}\n
      my_heap.rb
      ### Get left child index ###\ndef left(i)\n  2 * i + 1\nend\n\n### Get right child index ###\ndef right(i)\n  2 * i + 2\nend\n\n### Get parent node index ###\ndef parent(i)\n  (i - 1) / 2     # Floor division\nend\n
      "},{"location":"chapter_heap/heap/#2-accessing-the-heap-top-element","title":"2. \u00a0 Accessing the Heap Top Element","text":"

      The heap top element is the root node of the binary tree, which is also the first element of the list:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
      def peek(self) -> int:\n    \"\"\"Access top element\"\"\"\n    return self.max_heap[0]\n
      my_heap.cpp
      /* Access top element */\nint peek() {\n    return maxHeap[0];\n}\n
      my_heap.java
      /* Access top element */\nint peek() {\n    return maxHeap.get(0);\n}\n
      my_heap.cs
      /* Access top element */\nint Peek() {\n    return maxHeap[0];\n}\n
      my_heap.go
      /* Access top element */\nfunc (h *maxHeap) peek() any {\n    return h.data[0]\n}\n
      my_heap.swift
      /* Access top element */\nfunc peek() -> Int {\n    maxHeap[0]\n}\n
      my_heap.js
      /* Access top element */\npeek() {\n    return this.#maxHeap[0];\n}\n
      my_heap.ts
      /* Access top element */\npeek(): number {\n    return this.maxHeap[0];\n}\n
      my_heap.dart
      /* Access top element */\nint peek() {\n  return _maxHeap[0];\n}\n
      my_heap.rs
      /* Access top element */\nfn peek(&self) -> Option<i32> {\n    self.max_heap.first().copied()\n}\n
      my_heap.c
      /* Access top element */\nint peek(MaxHeap *maxHeap) {\n    return maxHeap->data[0];\n}\n
      my_heap.kt
      /* Access top element */\nfun peek(): Int {\n    return maxHeap[0]\n}\n
      my_heap.rb
      ### Access heap top element ###\ndef peek\n  @max_heap[0]\nend\n
      "},{"location":"chapter_heap/heap/#3-inserting-an-element-into-the-heap","title":"3. \u00a0 Inserting an Element Into the Heap","text":"

      Given an element val, we first add it to the bottom of the heap. After addition, since val may be larger than other elements in the heap, the heap's property may be violated. Therefore, it's necessary to repair the path from the inserted node to the root node. This operation is called heapify.

      Starting from the inserted node, perform heapify from bottom to top. As shown in Figure 8-3, we compare the inserted node with its parent node, and if the inserted node is larger, swap them. Then continue this operation, repairing nodes in the heap from bottom to top until we pass the root node or encounter a node that does not need swapping.

      <1><2><3><4><5><6><7><8><9>

      Figure 8-3 \u00a0 Steps of inserting an element into the heap

      Given a total of \\(n\\) nodes, the tree height is \\(O(\\log n)\\). Thus, the number of loop iterations in the heapify operation is at most \\(O(\\log n)\\), making the time complexity of the element insertion operation \\(O(\\log n)\\). The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
      def push(self, val: int):\n    \"\"\"Element enters heap\"\"\"\n    # Add node\n    self.max_heap.append(val)\n    # Heapify from bottom to top\n    self.sift_up(self.size() - 1)\n\ndef sift_up(self, i: int):\n    \"\"\"Starting from node i, heapify from bottom to top\"\"\"\n    while True:\n        # Get parent node of node i\n        p = self.parent(i)\n        # When \"crossing root node\" or \"node needs no repair\", end heapify\n        if p < 0 or self.max_heap[i] <= self.max_heap[p]:\n            break\n        # Swap two nodes\n        self.swap(i, p)\n        # Loop upward heapify\n        i = p\n
      my_heap.cpp
      /* Element enters heap */\nvoid push(int val) {\n    // Add node\n    maxHeap.push_back(val);\n    // Heapify from bottom to top\n    siftUp(size() - 1);\n}\n\n/* Starting from node i, heapify from bottom to top */\nvoid siftUp(int i) {\n    while (true) {\n        // Get parent node of node i\n        int p = parent(i);\n        // When \"crossing root node\" or \"node needs no repair\", end heapify\n        if (p < 0 || maxHeap[i] <= maxHeap[p])\n            break;\n        // Swap two nodes\n        swap(maxHeap[i], maxHeap[p]);\n        // Loop upward heapify\n        i = p;\n    }\n}\n
      my_heap.java
      /* Element enters heap */\nvoid push(int val) {\n    // Add node\n    maxHeap.add(val);\n    // Heapify from bottom to top\n    siftUp(size() - 1);\n}\n\n/* Starting from node i, heapify from bottom to top */\nvoid siftUp(int i) {\n    while (true) {\n        // Get parent node of node i\n        int p = parent(i);\n        // When \"crossing root node\" or \"node needs no repair\", end heapify\n        if (p < 0 || maxHeap.get(i) <= maxHeap.get(p))\n            break;\n        // Swap two nodes\n        swap(i, p);\n        // Loop upward heapify\n        i = p;\n    }\n}\n
      my_heap.cs
      /* Element enters heap */\nvoid Push(int val) {\n    // Add node\n    maxHeap.Add(val);\n    // Heapify from bottom to top\n    SiftUp(Size() - 1);\n}\n\n/* Starting from node i, heapify from bottom to top */\nvoid SiftUp(int i) {\n    while (true) {\n        // Get parent node of node i\n        int p = Parent(i);\n        // If 'past root node' or 'node needs no repair', end heapify\n        if (p < 0 || maxHeap[i] <= maxHeap[p])\n            break;\n        // Swap two nodes\n        Swap(i, p);\n        // Loop upward heapify\n        i = p;\n    }\n}\n
      my_heap.go
      /* Element enters heap */\nfunc (h *maxHeap) push(val any) {\n    // Add node\n    h.data = append(h.data, val)\n    // Heapify from bottom to top\n    h.siftUp(len(h.data) - 1)\n}\n\n/* Starting from node i, heapify from bottom to top */\nfunc (h *maxHeap) siftUp(i int) {\n    for true {\n        // Get parent node of node i\n        p := h.parent(i)\n        // When \"crossing root node\" or \"node needs no repair\", end heapify\n        if p < 0 || h.data[i].(int) <= h.data[p].(int) {\n            break\n        }\n        // Swap two nodes\n        h.swap(i, p)\n        // Loop upward heapify\n        i = p\n    }\n}\n
      my_heap.swift
      /* Element enters heap */\nfunc push(val: Int) {\n    // Add node\n    maxHeap.append(val)\n    // Heapify from bottom to top\n    siftUp(i: size() - 1)\n}\n\n/* Starting from node i, heapify from bottom to top */\nfunc siftUp(i: Int) {\n    var i = i\n    while true {\n        // Get parent node of node i\n        let p = parent(i: i)\n        // When \"crossing root node\" or \"node needs no repair\", end heapify\n        if p < 0 || maxHeap[i] <= maxHeap[p] {\n            break\n        }\n        // Swap two nodes\n        swap(i: i, j: p)\n        // Loop upward heapify\n        i = p\n    }\n}\n
      my_heap.js
      /* Element enters heap */\npush(val) {\n    // Add node\n    this.#maxHeap.push(val);\n    // Heapify from bottom to top\n    this.#siftUp(this.size() - 1);\n}\n\n/* Starting from node i, heapify from bottom to top */\n#siftUp(i) {\n    while (true) {\n        // Get parent node of node i\n        const p = this.#parent(i);\n        // When \"crossing root node\" or \"node needs no repair\", end heapify\n        if (p < 0 || this.#maxHeap[i] <= this.#maxHeap[p]) break;\n        // Swap two nodes\n        this.#swap(i, p);\n        // Loop upward heapify\n        i = p;\n    }\n}\n
      my_heap.ts
      /* Element enters heap */\npush(val: number): void {\n    // Add node\n    this.maxHeap.push(val);\n    // Heapify from bottom to top\n    this.siftUp(this.size() - 1);\n}\n\n/* Starting from node i, heapify from bottom to top */\nsiftUp(i: number): void {\n    while (true) {\n        // Get parent node of node i\n        const p = this.parent(i);\n        // When \"crossing root node\" or \"node needs no repair\", end heapify\n        if (p < 0 || this.maxHeap[i] <= this.maxHeap[p]) break;\n        // Swap two nodes\n        this.swap(i, p);\n        // Loop upward heapify\n        i = p;\n    }\n}\n
      my_heap.dart
      /* Element enters heap */\nvoid push(int val) {\n  // Add node\n  _maxHeap.add(val);\n  // Heapify from bottom to top\n  siftUp(size() - 1);\n}\n\n/* Starting from node i, heapify from bottom to top */\nvoid siftUp(int i) {\n  while (true) {\n    // Get parent node of node i\n    int p = _parent(i);\n    // When \"crossing root node\" or \"node needs no repair\", end heapify\n    if (p < 0 || _maxHeap[i] <= _maxHeap[p]) {\n      break;\n    }\n    // Swap two nodes\n    _swap(i, p);\n    // Loop upward heapify\n    i = p;\n  }\n}\n
      my_heap.rs
      /* Element enters heap */\nfn push(&mut self, val: i32) {\n    // Add node\n    self.max_heap.push(val);\n    // Heapify from bottom to top\n    self.sift_up(self.size() - 1);\n}\n\n/* Starting from node i, heapify from bottom to top */\nfn sift_up(&mut self, mut i: usize) {\n    loop {\n        // Node i is already the heap root, end heapification\n        if i == 0 {\n            break;\n        }\n        // Get parent node of node i\n        let p = Self::parent(i);\n        // When \"node needs no repair\", end heapification\n        if self.max_heap[i] <= self.max_heap[p] {\n            break;\n        }\n        // Swap two nodes\n        self.swap(i, p);\n        // Loop upward heapify\n        i = p;\n    }\n}\n
      my_heap.c
      /* Element enters heap */\nvoid push(MaxHeap *maxHeap, int val) {\n    // By default, should not add this many nodes\n    if (maxHeap->size == MAX_SIZE) {\n        printf(\"heap is full!\");\n        return;\n    }\n    // Add node\n    maxHeap->data[maxHeap->size] = val;\n    maxHeap->size++;\n\n    // Heapify from bottom to top\n    siftUp(maxHeap, maxHeap->size - 1);\n}\n\n/* Starting from node i, heapify from bottom to top */\nvoid siftUp(MaxHeap *maxHeap, int i) {\n    while (true) {\n        // Get parent node of node i\n        int p = parent(maxHeap, i);\n        // When \"crossing root node\" or \"node needs no repair\", end heapify\n        if (p < 0 || maxHeap->data[i] <= maxHeap->data[p]) {\n            break;\n        }\n        // Swap two nodes\n        swap(maxHeap, i, p);\n        // Loop upward heapify\n        i = p;\n    }\n}\n
      my_heap.kt
      /* Element enters heap */\nfun push(_val: Int) {\n    // Add node\n    maxHeap.add(_val)\n    // Heapify from bottom to top\n    siftUp(size() - 1)\n}\n\n/* Starting from node i, heapify from bottom to top */\nfun siftUp(it: Int) {\n    // Kotlin function parameters are immutable, so create temporary variable\n    var i = it\n    while (true) {\n        // Get parent node of node i\n        val p = parent(i)\n        // When \"crossing root node\" or \"node needs no repair\", end heapify\n        if (p < 0 || maxHeap[i] <= maxHeap[p]) break\n        // Swap two nodes\n        swap(i, p)\n        // Loop upward heapify\n        i = p\n    }\n}\n
      my_heap.rb
      ### Push element to heap ###\ndef push(val)\n  # Add node\n  @max_heap << val\n  # Heapify from bottom to top\n  sift_up(size - 1)\nend\n\n### Heapify from node i, bottom to top ###\ndef sift_up(i)\n  loop do\n    # Get parent node of node i\n    p = parent(i)\n    # When \"crossing root node\" or \"node needs no repair\", end heapify\n    break if p < 0 || @max_heap[i] <= @max_heap[p]\n    # Swap two nodes\n    swap(i, p)\n    # Loop upward heapify\n    i = p\n  end\nend\n
      "},{"location":"chapter_heap/heap/#4-removing-the-heap-top-element","title":"4. \u00a0 Removing the Heap Top Element","text":"

      The heap top element is the root node of the binary tree, which is the first element of the list. If we directly remove the first element from the list, all node indexes in the binary tree would change, making subsequent repair with heapify difficult. To minimize changes in element indexes, we use the following steps.

      1. Swap the heap top element with the heap bottom element (swap the root node with the rightmost leaf node).
      2. After swapping, remove the heap bottom from the list (note that since we've swapped, we're actually removing the original heap top element).
      3. Starting from the root node, perform heapify from top to bottom.

      As shown in Figure 8-4, the direction of \"top-to-bottom heapify\" is opposite to \"bottom-to-top heapify\". We compare the root node's value with its two children and swap it with the largest child. Then loop this operation until we pass a leaf node or encounter a node that doesn't need swapping.

      <1><2><3><4><5><6><7><8><9><10>

      Figure 8-4 \u00a0 Steps of removing the heap top element

      Similar to the element insertion operation, the time complexity of the heap top element removal operation is also \\(O(\\log n)\\). The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
      def pop(self) -> int:\n    \"\"\"Element exits heap\"\"\"\n    # Handle empty case\n    if self.is_empty():\n        raise IndexError(\"Heap is empty\")\n    # Swap root node with rightmost leaf node (swap first element with last element)\n    self.swap(0, self.size() - 1)\n    # Delete node\n    val = self.max_heap.pop()\n    # Heapify from top to bottom\n    self.sift_down(0)\n    # Return top element\n    return val\n\ndef sift_down(self, i: int):\n    \"\"\"Starting from node i, heapify from top to bottom\"\"\"\n    while True:\n        # Find node with largest value among i, l, r, denoted as ma\n        l, r, ma = self.left(i), self.right(i), i\n        if l < self.size() and self.max_heap[l] > self.max_heap[ma]:\n            ma = l\n        if r < self.size() and self.max_heap[r] > self.max_heap[ma]:\n            ma = r\n        # If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        if ma == i:\n            break\n        # Swap two nodes\n        self.swap(i, ma)\n        # Loop downward heapify\n        i = ma\n
      my_heap.cpp
      /* Element exits heap */\nvoid pop() {\n    // Handle empty case\n    if (isEmpty()) {\n        throw out_of_range(\"Heap is empty\");\n    }\n    // Delete node\n    swap(maxHeap[0], maxHeap[size() - 1]);\n    // Remove node\n    maxHeap.pop_back();\n    // Return top element\n    siftDown(0);\n}\n\n/* Starting from node i, heapify from top to bottom */\nvoid siftDown(int i) {\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        int l = left(i), r = right(i), ma = i;\n        if (l < size() && maxHeap[l] > maxHeap[ma])\n            ma = l;\n        if (r < size() && maxHeap[r] > maxHeap[ma])\n            ma = r;\n        // Swap two nodes\n        if (ma == i)\n            break;\n        swap(maxHeap[i], maxHeap[ma]);\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n
      my_heap.java
      /* Element exits heap */\nint pop() {\n    // Handle empty case\n    if (isEmpty())\n        throw new IndexOutOfBoundsException();\n    // Delete node\n    swap(0, size() - 1);\n    // Remove node\n    int val = maxHeap.remove(size() - 1);\n    // Return top element\n    siftDown(0);\n    // Return heap top element\n    return val;\n}\n\n/* Starting from node i, heapify from top to bottom */\nvoid siftDown(int i) {\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        int l = left(i), r = right(i), ma = i;\n        if (l < size() && maxHeap.get(l) > maxHeap.get(ma))\n            ma = l;\n        if (r < size() && maxHeap.get(r) > maxHeap.get(ma))\n            ma = r;\n        // Swap two nodes\n        if (ma == i)\n            break;\n        // Swap two nodes\n        swap(i, ma);\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n
      my_heap.cs
      /* Element exits heap */\nint Pop() {\n    // Handle empty case\n    if (IsEmpty())\n        throw new IndexOutOfRangeException();\n    // Delete node\n    Swap(0, Size() - 1);\n    // Remove node\n    int val = maxHeap.Last();\n    maxHeap.RemoveAt(Size() - 1);\n    // Return top element\n    SiftDown(0);\n    // Return heap top element\n    return val;\n}\n\n/* Starting from node i, heapify from top to bottom */\nvoid SiftDown(int i) {\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        int l = Left(i), r = Right(i), ma = i;\n        if (l < Size() && maxHeap[l] > maxHeap[ma])\n            ma = l;\n        if (r < Size() && maxHeap[r] > maxHeap[ma])\n            ma = r;\n        // If 'node i is largest' or 'past leaf node', end heapify\n        if (ma == i) break;\n        // Swap two nodes\n        Swap(i, ma);\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n
      my_heap.go
      /* Element exits heap */\nfunc (h *maxHeap) pop() any {\n    // Handle empty case\n    if h.isEmpty() {\n        fmt.Println(\"error\")\n        return nil\n    }\n    // Delete node\n    h.swap(0, h.size()-1)\n    // Remove node\n    val := h.data[len(h.data)-1]\n    h.data = h.data[:len(h.data)-1]\n    // Return top element\n    h.siftDown(0)\n\n    // Return heap top element\n    return val\n}\n\n/* Starting from node i, heapify from top to bottom */\nfunc (h *maxHeap) siftDown(i int) {\n    for true {\n        // Find node with maximum value among nodes i, l, r, denoted as max\n        l, r, max := h.left(i), h.right(i), i\n        if l < h.size() && h.data[l].(int) > h.data[max].(int) {\n            max = l\n        }\n        if r < h.size() && h.data[r].(int) > h.data[max].(int) {\n            max = r\n        }\n        // Swap two nodes\n        if max == i {\n            break\n        }\n        // Swap two nodes\n        h.swap(i, max)\n        // Loop downwards heapification\n        i = max\n    }\n}\n
      my_heap.swift
      /* Element exits heap */\nfunc pop() -> Int {\n    // Handle empty case\n    if isEmpty() {\n        fatalError(\"Heap is empty\")\n    }\n    // Delete node\n    swap(i: 0, j: size() - 1)\n    // Remove node\n    let val = maxHeap.remove(at: size() - 1)\n    // Return top element\n    siftDown(i: 0)\n    // Return heap top element\n    return val\n}\n\n/* Starting from node i, heapify from top to bottom */\nfunc siftDown(i: Int) {\n    var i = i\n    while true {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        let l = left(i: i)\n        let r = right(i: i)\n        var ma = i\n        if l < size(), maxHeap[l] > maxHeap[ma] {\n            ma = l\n        }\n        if r < size(), maxHeap[r] > maxHeap[ma] {\n            ma = r\n        }\n        // Swap two nodes\n        if ma == i {\n            break\n        }\n        // Swap two nodes\n        swap(i: i, j: ma)\n        // Loop downwards heapification\n        i = ma\n    }\n}\n
      my_heap.js
      /* Element exits heap */\npop() {\n    // Handle empty case\n    if (this.isEmpty()) throw new Error('Heap is empty');\n    // Delete node\n    this.#swap(0, this.size() - 1);\n    // Remove node\n    const val = this.#maxHeap.pop();\n    // Return top element\n    this.#siftDown(0);\n    // Return heap top element\n    return val;\n}\n\n/* Starting from node i, heapify from top to bottom */\n#siftDown(i) {\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        const l = this.#left(i),\n            r = this.#right(i);\n        let ma = i;\n        if (l < this.size() && this.#maxHeap[l] > this.#maxHeap[ma]) ma = l;\n        if (r < this.size() && this.#maxHeap[r] > this.#maxHeap[ma]) ma = r;\n        // Swap two nodes\n        if (ma === i) break;\n        // Swap two nodes\n        this.#swap(i, ma);\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n
      my_heap.ts
      /* Element exits heap */\npop(): number {\n    // Handle empty case\n    if (this.isEmpty()) throw new RangeError('Heap is empty.');\n    // Delete node\n    this.swap(0, this.size() - 1);\n    // Remove node\n    const val = this.maxHeap.pop();\n    // Return top element\n    this.siftDown(0);\n    // Return heap top element\n    return val;\n}\n\n/* Starting from node i, heapify from top to bottom */\nsiftDown(i: number): void {\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        const l = this.left(i),\n            r = this.right(i);\n        let ma = i;\n        if (l < this.size() && this.maxHeap[l] > this.maxHeap[ma]) ma = l;\n        if (r < this.size() && this.maxHeap[r] > this.maxHeap[ma]) ma = r;\n        // Swap two nodes\n        if (ma === i) break;\n        // Swap two nodes\n        this.swap(i, ma);\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n
      my_heap.dart
      /* Element exits heap */\nint pop() {\n  // Handle empty case\n  if (isEmpty()) throw Exception('Heap is empty');\n  // Delete node\n  _swap(0, size() - 1);\n  // Remove node\n  int val = _maxHeap.removeLast();\n  // Return top element\n  siftDown(0);\n  // Return heap top element\n  return val;\n}\n\n/* Starting from node i, heapify from top to bottom */\nvoid siftDown(int i) {\n  while (true) {\n    // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n    int l = _left(i);\n    int r = _right(i);\n    int ma = i;\n    if (l < size() && _maxHeap[l] > _maxHeap[ma]) ma = l;\n    if (r < size() && _maxHeap[r] > _maxHeap[ma]) ma = r;\n    // Swap two nodes\n    if (ma == i) break;\n    // Swap two nodes\n    _swap(i, ma);\n    // Loop downwards heapification\n    i = ma;\n  }\n}\n
      my_heap.rs
      /* Element exits heap */\nfn pop(&mut self) -> i32 {\n    // Handle empty case\n    if self.is_empty() {\n        panic!(\"index out of bounds\");\n    }\n    // Delete node\n    self.swap(0, self.size() - 1);\n    // Remove node\n    let val = self.max_heap.pop().unwrap();\n    // Return top element\n    self.sift_down(0);\n    // Return heap top element\n    val\n}\n\n/* Starting from node i, heapify from top to bottom */\nfn sift_down(&mut self, mut i: usize) {\n    loop {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        let (l, r, mut ma) = (Self::left(i), Self::right(i), i);\n        if l < self.size() && self.max_heap[l] > self.max_heap[ma] {\n            ma = l;\n        }\n        if r < self.size() && self.max_heap[r] > self.max_heap[ma] {\n            ma = r;\n        }\n        // Swap two nodes\n        if ma == i {\n            break;\n        }\n        // Swap two nodes\n        self.swap(i, ma);\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n
      my_heap.c
      /* Element exits heap */\nint pop(MaxHeap *maxHeap) {\n    // Handle empty case\n    if (isEmpty(maxHeap)) {\n        printf(\"heap is empty!\");\n        return INT_MAX;\n    }\n    // Delete node\n    swap(maxHeap, 0, size(maxHeap) - 1);\n    // Remove node\n    int val = maxHeap->data[maxHeap->size - 1];\n    maxHeap->size--;\n    // Return top element\n    siftDown(maxHeap, 0);\n\n    // Return heap top element\n    return val;\n}\n\n/* Starting from node i, heapify from top to bottom */\nvoid siftDown(MaxHeap *maxHeap, int i) {\n    while (true) {\n        // Find node with maximum value among nodes i, l, r, denoted as max\n        int l = left(maxHeap, i);\n        int r = right(maxHeap, i);\n        int max = i;\n        if (l < size(maxHeap) && maxHeap->data[l] > maxHeap->data[max]) {\n            max = l;\n        }\n        if (r < size(maxHeap) && maxHeap->data[r] > maxHeap->data[max]) {\n            max = r;\n        }\n        // Swap two nodes\n        if (max == i) {\n            break;\n        }\n        // Swap two nodes\n        swap(maxHeap, i, max);\n        // Loop downwards heapification\n        i = max;\n    }\n}\n
      my_heap.kt
      /* Element exits heap */\nfun pop(): Int {\n    // Handle empty case\n    if (isEmpty()) throw IndexOutOfBoundsException()\n    // Delete node\n    swap(0, size() - 1)\n    // Remove node\n    val _val = maxHeap.removeAt(size() - 1)\n    // Return top element\n    siftDown(0)\n    // Return heap top element\n    return _val\n}\n\n/* Starting from node i, heapify from top to bottom */\nfun siftDown(it: Int) {\n    // Kotlin function parameters are immutable, so create temporary variable\n    var i = it\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        val l = left(i)\n        val r = right(i)\n        var ma = i\n        if (l < size() && maxHeap[l] > maxHeap[ma]) ma = l\n        if (r < size() && maxHeap[r] > maxHeap[ma]) ma = r\n        // Swap two nodes\n        if (ma == i) break\n        // Swap two nodes\n        swap(i, ma)\n        // Loop downwards heapification\n        i = ma\n    }\n}\n
      my_heap.rb
      ### Pop element from heap ###\ndef pop\n  # Handle empty case\n  raise IndexError, \"Heap is empty\" if is_empty?\n  # Delete node\n  swap(0, size - 1)\n  # Remove node\n  val = @max_heap.pop\n  # Return top element\n  sift_down(0)\n  # Return heap top element\n  val\nend\n\n### Heapify from node i, top to bottom ###\ndef sift_down(i)\n  loop do\n    # If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n    l, r, ma = left(i), right(i), i\n    ma = l if l < size && @max_heap[l] > @max_heap[ma]\n    ma = r if r < size && @max_heap[r] > @max_heap[ma]\n\n    # Swap two nodes\n    break if ma == i\n\n    # Swap two nodes\n    swap(i, ma)\n    # Loop downwards heapification\n    i = ma\n  end\nend\n
      "},{"location":"chapter_heap/heap/#813-common-applications-of-heaps","title":"8.1.3 \u00a0 Common Applications of Heaps","text":"
      • Priority queue: Heaps are typically the preferred data structure for implementing priority queues, with both enqueue and dequeue operations having a time complexity of \\(O(\\log n)\\), and the heap construction operation having \\(O(n)\\), all of which are highly efficient.
      • Heap sort: Given a set of data, we can build a heap with them and then continuously perform element removal operations to obtain sorted data. However, we usually use a more elegant approach to implement heap sort, as detailed in the \"Heap Sort\" chapter.
      • Getting the largest \\(k\\) elements: This is a classic algorithm problem and also a typical application, such as selecting the top 10 trending news for Weibo hot search, selecting the top 10 best-selling products, etc.
      "},{"location":"chapter_heap/summary/","title":"8.4 \u00a0 Summary","text":""},{"location":"chapter_heap/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • A heap is a complete binary tree that can be categorized as a max heap or min heap based on its property. The heap top element of a max heap (min heap) is the largest (smallest).
      • A priority queue is defined as a queue with priority sorting, typically implemented using heaps.
      • Common heap operations and their corresponding time complexities include: element insertion \\(O(\\log n)\\), heap top element removal \\(O(\\log n)\\), and accessing the heap top element \\(O(1)\\).
      • Complete binary trees are well-suited for array representation, so we typically use arrays to store heaps.
      • Heapify operations are used to maintain the heap property and are employed in both element insertion and removal operations.
      • The time complexity of building a heap with \\(n\\) input elements can be optimized to \\(O(n)\\), which is highly efficient.
      • Top-k is a classic algorithm problem that can be efficiently solved using the heap data structure, with a time complexity of \\(O(n \\log k)\\).
      "},{"location":"chapter_heap/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

      Q: Are the \"heap\" in data structures and the \"heap\" in memory management the same concept?

      The two are not the same concept; they just happen to share the name \"heap.\" The heap in computer system memory is part of dynamic memory allocation, where programs can use it to store data during runtime. Programs can request a certain amount of heap memory to store complex structures such as objects and arrays. When this data is no longer needed, the program needs to release this memory to prevent memory leaks. Compared to stack memory, heap memory management and usage require more caution, as improper use can lead to issues such as memory leaks and dangling pointers.

      "},{"location":"chapter_heap/top_k/","title":"8.3 \u00a0 Top-K Problem","text":"

      Question

      Given an unordered array nums of length \\(n\\), return the largest \\(k\\) elements in the array.

      For this problem, we'll first introduce two solutions with relatively straightforward approaches, then introduce a more efficient heap-based solution.

      "},{"location":"chapter_heap/top_k/#831-method-1-iterative-selection","title":"8.3.1 \u00a0 Method 1: Iterative Selection","text":"

      We can perform \\(k\\) rounds of traversal as shown in Figure 8-6, extracting the \\(1^{st}\\), \\(2^{nd}\\), \\(\\dots\\), \\(k^{th}\\) largest elements in each round, with a time complexity of \\(O(nk)\\).

      This method is only suitable when \\(k \\ll n\\), because when \\(k\\) is close to \\(n\\), the time complexity approaches \\(O(n^2)\\), which is very time-consuming.

      Figure 8-6 \u00a0 Traversing to find the largest k elements

      Tip

      When \\(k = n\\), we can obtain a complete sorted sequence, which is equivalent to the \"selection sort\" algorithm.

      "},{"location":"chapter_heap/top_k/#832-method-2-sorting","title":"8.3.2 \u00a0 Method 2: Sorting","text":"

      As shown in Figure 8-7, we can first sort the array nums, then return the rightmost \\(k\\) elements, with a time complexity of \\(O(n \\log n)\\).

      Clearly, this method \"overachieves\" the task, as we only need to find the largest \\(k\\) elements, without needing to sort the other elements.

      Figure 8-7 \u00a0 Sorting to find the largest k elements

      "},{"location":"chapter_heap/top_k/#833-method-3-heap","title":"8.3.3 \u00a0 Method 3: Heap","text":"

      We can solve the Top-k problem more efficiently using heaps, with the process shown in Figure 8-8.

      1. Initialize a min heap, where the heap top element is the smallest.
      2. First, insert the first \\(k\\) elements of the array into the heap in sequence.
      3. Starting from the \\((k + 1)^{th}\\) element, if the current element is greater than the heap top element, remove the heap top element and insert the current element into the heap.
      4. After traversal is complete, the heap contains the largest \\(k\\) elements.
      <1><2><3><4><5><6><7><8><9>

      Figure 8-8 \u00a0 Finding the largest k elements using a heap

      Example code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby top_k.py
      def top_k_heap(nums: list[int], k: int) -> list[int]:\n    \"\"\"Find the largest k elements in array based on heap\"\"\"\n    # Initialize min heap\n    heap = []\n    # Enter the first k elements of array into heap\n    for i in range(k):\n        heapq.heappush(heap, nums[i])\n    # Starting from the (k+1)th element, maintain heap length as k\n    for i in range(k, len(nums)):\n        # If current element is greater than top element, top element exits heap, current element enters heap\n        if nums[i] > heap[0]:\n            heapq.heappop(heap)\n            heapq.heappush(heap, nums[i])\n    return heap\n
      top_k.cpp
      /* Find the largest k elements in array based on heap */\npriority_queue<int, vector<int>, greater<int>> topKHeap(vector<int> &nums, int k) {\n    // Python's heapq module implements min heap by default\n    priority_queue<int, vector<int>, greater<int>> heap;\n    // Enter the first k elements of array into heap\n    for (int i = 0; i < k; i++) {\n        heap.push(nums[i]);\n    }\n    // Starting from the (k+1)th element, maintain heap length as k\n    for (int i = k; i < nums.size(); i++) {\n        // If current element is greater than top element, top element exits heap, current element enters heap\n        if (nums[i] > heap.top()) {\n            heap.pop();\n            heap.push(nums[i]);\n        }\n    }\n    return heap;\n}\n
      top_k.java
      /* Find the largest k elements in array based on heap */\nQueue<Integer> topKHeap(int[] nums, int k) {\n    // Python's heapq module implements min heap by default\n    Queue<Integer> heap = new PriorityQueue<Integer>();\n    // Enter the first k elements of array into heap\n    for (int i = 0; i < k; i++) {\n        heap.offer(nums[i]);\n    }\n    // Starting from the (k+1)th element, maintain heap length as k\n    for (int i = k; i < nums.length; i++) {\n        // If current element is greater than top element, top element exits heap, current element enters heap\n        if (nums[i] > heap.peek()) {\n            heap.poll();\n            heap.offer(nums[i]);\n        }\n    }\n    return heap;\n}\n
      top_k.cs
      /* Find the largest k elements in array based on heap */\nPriorityQueue<int, int> TopKHeap(int[] nums, int k) {\n    // Python's heapq module implements min heap by default\n    PriorityQueue<int, int> heap = new();\n    // Enter the first k elements of array into heap\n    for (int i = 0; i < k; i++) {\n        heap.Enqueue(nums[i], nums[i]);\n    }\n    // Starting from the (k+1)th element, maintain heap length as k\n    for (int i = k; i < nums.Length; i++) {\n        // If current element is greater than top element, top element exits heap, current element enters heap\n        if (nums[i] > heap.Peek()) {\n            heap.Dequeue();\n            heap.Enqueue(nums[i], nums[i]);\n        }\n    }\n    return heap;\n}\n
      top_k.go
      /* Find the largest k elements in array based on heap */\nfunc topKHeap(nums []int, k int) *minHeap {\n    // Python's heapq module implements min heap by default\n    h := &minHeap{}\n    heap.Init(h)\n    // Enter the first k elements of array into heap\n    for i := 0; i < k; i++ {\n        heap.Push(h, nums[i])\n    }\n    // Starting from the (k+1)th element, maintain heap length as k\n    for i := k; i < len(nums); i++ {\n        // If current element is greater than top element, top element exits heap, current element enters heap\n        if nums[i] > h.Top().(int) {\n            heap.Pop(h)\n            heap.Push(h, nums[i])\n        }\n    }\n    return h\n}\n
      top_k.swift
      /* Find the largest k elements in array based on heap */\nfunc topKHeap(nums: [Int], k: Int) -> [Int] {\n    // Initialize min heap and build heap with first k elements\n    var heap = Heap(nums.prefix(k))\n    // Starting from the (k+1)th element, maintain heap length as k\n    for i in nums.indices.dropFirst(k) {\n        // If current element is greater than top element, top element exits heap, current element enters heap\n        if nums[i] > heap.min()! {\n            _ = heap.removeMin()\n            heap.insert(nums[i])\n        }\n    }\n    return heap.unordered\n}\n
      top_k.js
      /* Element enters heap */\nfunction pushMinHeap(maxHeap, val) {\n    // Negate element\n    maxHeap.push(-val);\n}\n\n/* Element exits heap */\nfunction popMinHeap(maxHeap) {\n    // Negate element\n    return -maxHeap.pop();\n}\n\n/* Access top element */\nfunction peekMinHeap(maxHeap) {\n    // Negate element\n    return -maxHeap.peek();\n}\n\n/* Extract elements from heap */\nfunction getMinHeap(maxHeap) {\n    // Negate element\n    return maxHeap.getMaxHeap().map((num) => -num);\n}\n\n/* Find the largest k elements in array based on heap */\nfunction topKHeap(nums, k) {\n    // Python's heapq module implements min heap by default\n    // Note: We negate all heap elements to simulate min heap using max heap\n    const maxHeap = new MaxHeap([]);\n    // Enter the first k elements of array into heap\n    for (let i = 0; i < k; i++) {\n        pushMinHeap(maxHeap, nums[i]);\n    }\n    // Starting from the (k+1)th element, maintain heap length as k\n    for (let i = k; i < nums.length; i++) {\n        // If current element is greater than top element, top element exits heap, current element enters heap\n        if (nums[i] > peekMinHeap(maxHeap)) {\n            popMinHeap(maxHeap);\n            pushMinHeap(maxHeap, nums[i]);\n        }\n    }\n    // Return elements in heap\n    return getMinHeap(maxHeap);\n}\n
      top_k.ts
      /* Element enters heap */\nfunction pushMinHeap(maxHeap: MaxHeap, val: number): void {\n    // Negate element\n    maxHeap.push(-val);\n}\n\n/* Element exits heap */\nfunction popMinHeap(maxHeap: MaxHeap): number {\n    // Negate element\n    return -maxHeap.pop();\n}\n\n/* Access top element */\nfunction peekMinHeap(maxHeap: MaxHeap): number {\n    // Negate element\n    return -maxHeap.peek();\n}\n\n/* Extract elements from heap */\nfunction getMinHeap(maxHeap: MaxHeap): number[] {\n    // Negate element\n    return maxHeap.getMaxHeap().map((num: number) => -num);\n}\n\n/* Find the largest k elements in array based on heap */\nfunction topKHeap(nums: number[], k: number): number[] {\n    // Python's heapq module implements min heap by default\n    // Note: We negate all heap elements to simulate min heap using max heap\n    const maxHeap = new MaxHeap([]);\n    // Enter the first k elements of array into heap\n    for (let i = 0; i < k; i++) {\n        pushMinHeap(maxHeap, nums[i]);\n    }\n    // Starting from the (k+1)th element, maintain heap length as k\n    for (let i = k; i < nums.length; i++) {\n        // If current element is greater than top element, top element exits heap, current element enters heap\n        if (nums[i] > peekMinHeap(maxHeap)) {\n            popMinHeap(maxHeap);\n            pushMinHeap(maxHeap, nums[i]);\n        }\n    }\n    // Return elements in heap\n    return getMinHeap(maxHeap);\n}\n
      top_k.dart
      /* Find the largest k elements in array based on heap */\nMinHeap topKHeap(List<int> nums, int k) {\n  // Initialize min heap, push first k elements of array to heap\n  MinHeap heap = MinHeap(nums.sublist(0, k));\n  // Starting from the (k+1)th element, maintain heap length as k\n  for (int i = k; i < nums.length; i++) {\n    // If current element is greater than top element, top element exits heap, current element enters heap\n    if (nums[i] > heap.peek()) {\n      heap.pop();\n      heap.push(nums[i]);\n    }\n  }\n  return heap;\n}\n
      top_k.rs
      /* Find the largest k elements in array based on heap */\nfn top_k_heap(nums: Vec<i32>, k: usize) -> BinaryHeap<Reverse<i32>> {\n    // BinaryHeap is a max heap, use Reverse to negate elements to implement min heap\n    let mut heap = BinaryHeap::<Reverse<i32>>::new();\n    // Enter the first k elements of array into heap\n    for &num in nums.iter().take(k) {\n        heap.push(Reverse(num));\n    }\n    // Starting from the (k+1)th element, maintain heap length as k\n    for &num in nums.iter().skip(k) {\n        // If current element is greater than top element, top element exits heap, current element enters heap\n        if num > heap.peek().unwrap().0 {\n            heap.pop();\n            heap.push(Reverse(num));\n        }\n    }\n    heap\n}\n
      top_k.c
      /* Element enters heap */\nvoid pushMinHeap(MaxHeap *maxHeap, int val) {\n    // Negate element\n    push(maxHeap, -val);\n}\n\n/* Element exits heap */\nint popMinHeap(MaxHeap *maxHeap) {\n    // Negate element\n    return -pop(maxHeap);\n}\n\n/* Access top element */\nint peekMinHeap(MaxHeap *maxHeap) {\n    // Negate element\n    return -peek(maxHeap);\n}\n\n/* Extract elements from heap */\nint *getMinHeap(MaxHeap *maxHeap) {\n    // Negate all heap elements and store in res array\n    int *res = (int *)malloc(maxHeap->size * sizeof(int));\n    for (int i = 0; i < maxHeap->size; i++) {\n        res[i] = -maxHeap->data[i];\n    }\n    return res;\n}\n\n/* Extract elements from heap */\nint *getMinHeap(MaxHeap *maxHeap) {\n    // Negate all heap elements and store in res array\n    int *res = (int *)malloc(maxHeap->size * sizeof(int));\n    for (int i = 0; i < maxHeap->size; i++) {\n        res[i] = -maxHeap->data[i];\n    }\n    return res;\n}\n\n// Function to find k largest elements in array using heap\nint *topKHeap(int *nums, int sizeNums, int k) {\n    // Python's heapq module implements min heap by default\n    // Note: We negate all heap elements to simulate min heap using max heap\n    int *empty = (int *)malloc(0);\n    MaxHeap *maxHeap = newMaxHeap(empty, 0);\n    // Enter the first k elements of array into heap\n    for (int i = 0; i < k; i++) {\n        pushMinHeap(maxHeap, nums[i]);\n    }\n    // Starting from the (k+1)th element, maintain heap length as k\n    for (int i = k; i < sizeNums; i++) {\n        // If current element is greater than top element, top element exits heap, current element enters heap\n        if (nums[i] > peekMinHeap(maxHeap)) {\n            popMinHeap(maxHeap);\n            pushMinHeap(maxHeap, nums[i]);\n        }\n    }\n    int *res = getMinHeap(maxHeap);\n    // Free memory\n    delMaxHeap(maxHeap);\n    return res;\n}\n
      top_k.kt
      /* Find the largest k elements in array based on heap */\nfun topKHeap(nums: IntArray, k: Int): Queue<Int> {\n    // Python's heapq module implements min heap by default\n    val heap = PriorityQueue<Int>()\n    // Enter the first k elements of array into heap\n    for (i in 0..<k) {\n        heap.offer(nums[i])\n    }\n    // Starting from the (k+1)th element, maintain heap length as k\n    for (i in k..<nums.size) {\n        // If current element is greater than top element, top element exits heap, current element enters heap\n        if (nums[i] > heap.peek()) {\n            heap.poll()\n            heap.offer(nums[i])\n        }\n    }\n    return heap\n}\n
      top_k.rb
      ### Find largest k elements in array using heap ###\ndef top_k_heap(nums, k)\n  # Python's heapq module implements min heap by default\n  # Note: We negate all heap elements to simulate min heap using max heap\n  max_heap = MaxHeap.new([])\n\n  # Enter the first k elements of array into heap\n  for i in 0...k\n    push_min_heap(max_heap, nums[i])\n  end\n\n  # Starting from the (k+1)th element, maintain heap length as k\n  for i in k...nums.length\n    # If current element is greater than top element, top element exits heap, current element enters heap\n    if nums[i] > peek_min_heap(max_heap)\n      pop_min_heap(max_heap)\n      push_min_heap(max_heap, nums[i])\n    end\n  end\n\n  get_min_heap(max_heap)\nend\n

      A total of \\(n\\) rounds of heap insertions and removals are performed, with the heap's maximum length being \\(k\\), so the time complexity is \\(O(n \\log k)\\). This method is very efficient; when \\(k\\) is small, the time complexity approaches \\(O(n)\\); when \\(k\\) is large, the time complexity does not exceed \\(O(n \\log n)\\).

      Additionally, this method is suitable for dynamic data stream scenarios. By continuously adding data, we can maintain the elements in the heap, thus achieving dynamic updates of the largest \\(k\\) elements.

      "},{"location":"chapter_hello_algo/","title":"Before Starting","text":"

      A few years ago, I shared the \"Sword for Offer\" problem solutions on LeetCode, receiving encouragement and support from many readers. During interactions with readers, the most frequently asked question I encountered was \"how to get started with algorithms.\" Gradually, I developed a keen interest in this question.

      Diving straight into problem-solving seems to be the most popular approach\u2014it's simple, direct, and effective. However, problem-solving is like playing Minesweeper: those with strong self-learning abilities can successfully defuse the mines one by one, while those with insufficient foundations may end up bruised and battered, retreating step by step in frustration. Reading through textbooks is also a common practice, but for job seekers, graduation theses, resume submissions, and preparations for written tests and interviews have already consumed most of their energy, making working through thick books an arduous challenge.

      If you're facing similar struggles, then it's fortunate that this book has \"found\" you. This book is my answer to this question\u2014even if it may not be the optimal solution, it is at least a positive attempt. While this book alone won't directly land you a job offer, it will guide you to explore the \"knowledge map\" of data structures and algorithms, help you understand the shapes, sizes, and distributions of different \"mines,\" and enable you to master various \"mine-clearing methods.\" With these skills, I believe you can tackle problems and read technical literature more confidently, gradually building a complete knowledge system.

      I deeply agree with Professor Feynman's words: \"Knowledge isn't free. You have to pay attention.\" In this sense, this book is not entirely \"free.\" In order to live up to the precious \"attention\" you invest in this book, I will do my utmost and devote my greatest \"attention\" to completing this work.

      I'm acutely aware of my limited knowledge and shallow expertise. Although the content of this book has been refined over a period of time, there are certainly still many errors, and I sincerely welcome critiques and corrections from teachers and fellow students.

      Hello, Algorithms!

      The advent of computers has brought tremendous changes to the world. With their high-speed computing capabilities and excellent programmability, they have become the ideal medium for executing algorithms and processing data. Whether it's the realistic graphics in video games, the intelligent decision-making in autonomous driving, AlphaGo's brilliant Go matches, or ChatGPT's natural interactions, these applications are all exquisite interpretations of algorithms on computers.

      In fact, before the advent of computers, algorithms and data structures already existed in every corner of the world. Early algorithms were relatively simple, such as ancient counting methods and tool-making procedures. As civilization progressed, algorithms gradually became more refined and complex. From the ingenious craftsmanship of master artisans, to industrial products that liberate productive forces, to the scientific laws governing the operation of the universe, behind almost every ordinary or astonishing thing lies ingenious algorithmic thinking.

      Similarly, data structures are everywhere: from large-scale social networks to small subway systems, many systems can be modeled as \"graphs\"; from a nation to a family, the primary organizational forms of society exhibit characteristics of \"trees\"; winter clothing is like a \"stack,\" where the first item put on is the last to be taken off; a badminton tube is like a \"queue,\" with items inserted at one end and retrieved from the other; a dictionary is like a \"hash table,\" enabling quick lookup of target entries.

      This book aims to help readers understand the core concepts of algorithms and data structures through clear and accessible animated illustrations and runnable code examples, and to implement them through programming. Building on this foundation, the book endeavors to reveal the vivid manifestations of algorithms in the complex world and showcase the beauty of algorithms. I hope this book can be of help to you!

      "},{"location":"chapter_introduction/","title":"Chapter 1. \u00a0 Encounter with Algorithms","text":"

      Abstract

      A young girl dances gracefully, intertwined with data, her skirt flowing with the melody of algorithms.

      She invites you to dance with her. Follow her steps closely and enter the world of algorithms, full of logic and beauty.

      "},{"location":"chapter_introduction/#chapter-contents","title":"Chapter contents","text":"
      • 1.1 \u00a0 Algorithms Are Everywhere
      • 1.2 \u00a0 What Is an Algorithm
      • 1.3 \u00a0 Summary
      "},{"location":"chapter_introduction/algorithms_are_everywhere/","title":"1.1 \u00a0 Algorithms Are Everywhere","text":"

      When we hear the term \"algorithm,\" we naturally think of mathematics. However, many algorithms do not involve complex mathematics but rely more on basic logic, which can be seen everywhere in our daily lives.

      Before we start discussing about algorithms officially, there's an interesting fact worth sharing: you've learned many algorithms unconsciously and are used to applying them in your daily life. Here, I will give a few specific examples to prove this point.

      Example 1: Looking Up a Dictionary. In an English dictionary, words are listed alphabetically. Assuming we're searching for a word that starts with the letter \\(r\\), this is typically done in the following way:

      1. Open the dictionary to about halfway and check the first vocabulary of the page, let's say the letter starts with \\(m\\).
      2. Since \\(r\\) comes after \\(m\\) in the alphabet, the first half can be ignored and the search space is narrowed down to the second half.
      3. Repeat steps 1. and 2. until you find the page where the word starts with \\(r\\).
      <1><2><3><4><5>

      Figure 1-1 \u00a0 Process of looking up a dictionary

      Looking up a dictionary, an essential skill for elementary school students is actually the famous \"Binary Search\" algorithm. From a data structure perspective, we can consider the dictionary as a sorted \"array\"; from an algorithmic perspective, the series of actions taken to look up a word in the dictionary can be viewed as the algorithm \"Binary Search.\"

      Example 2: Organizing Card Deck. When playing cards, we need to arrange the cards in our hands in ascending order, as shown in the following process.

      1. Divide the playing cards into \"ordered\" and \"unordered\" sections, assuming initially the leftmost card is already in order.
      2. Take out a card from the unordered section and insert it into the correct position in the ordered section; after this, the leftmost two cards are in order.
      3. Repeat step 2 until all cards are in order.

      Figure 1-2 \u00a0 Process of sorting a deck of cards

      The above method of organizing playing cards is practically the \"Insertion Sort\" algorithm, which is very efficient for small datasets. Many programming languages' sorting functions include the insertion sort.

      Example 3: Making Change. Assume making a purchase of \\(69\\) at a supermarket. If you give the cashier \\(100\\), they will need to provide you with \\(31\\) in change. This process can be clearly understood as illustrated in Figure 1-3.

      1. The options are currencies valued below \\(31\\), including \\(1\\), \\(5\\), \\(10\\), and \\(20\\).
      2. Take out the largest \\(20\\) from the options, leaving \\(31 - 20 = 11\\).
      3. Take out the largest \\(10\\) from the remaining options, leaving \\(11 - 10 = 1\\).
      4. Take out the largest \\(1\\) from the remaining options, leaving \\(1 - 1 = 0\\).
      5. Complete change-making, the solution is \\(20 + 10 + 1 = 31\\).

      Figure 1-3 \u00a0 Process of making change

      In the steps described, we choose the best option at each stage by utilizing the largest denomination available, which leads to an effective change-making strategy. From a data structures and algorithms perspective, this approach is known as a \"Greedy\" algorithm.

      From cooking a meal to interstellar travel, almost all problem-solving involves algorithms. The advent of computers allows us to store data structures in memory and write code to call the CPU and GPU to execute algorithms. In this way, we can transfer real-life problems to computers and solve various complex issues in a more efficient way.

      Tip

      If you are still confused about concepts like data structures, algorithms, arrays, and binary searches, I encourage you to keep reading. This book will gently guide you into the realm of understanding data structures and algorithms.

      "},{"location":"chapter_introduction/summary/","title":"1.3 \u00a0 Summary","text":""},{"location":"chapter_introduction/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • Algorithms are ubiquitous in daily life and are not distant, esoteric knowledge. In fact, we have already learned many algorithms unconsciously and use them to solve problems big and small in life.
      • The principle of looking up a dictionary is consistent with the binary search algorithm. Binary search embodies the important algorithmic idea of divide and conquer.
      • The process of organizing playing cards is very similar to the insertion sort algorithm. Insertion sort is suitable for sorting small datasets.
      • The steps of making change are essentially a greedy algorithm, where the best choice is made at each step based on the current situation.
      • An algorithm is a set of instructions or operational steps that solves a specific problem within a finite amount of time, while a data structure is the way computers organize and store data.
      • Data structures and algorithms are closely connected. Data structures are the foundation of algorithms, and algorithms breathe life into data structures.
      • We can compare data structures and algorithms to assembling building blocks. The blocks represent data, the shape and connection method of the blocks represent the data structure, and the steps to assemble the blocks correspond to the algorithm.
      "},{"location":"chapter_introduction/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

      Q: As a programmer, I have never used algorithms to solve problems in my daily work. Common algorithms are already encapsulated by programming languages and can be used directly. Does this mean that the problems in our work have not yet reached the level where algorithms are needed?

      If we compare specific work skills to \"techniques\" in martial arts, then fundamental subjects should be more like \"internal skills\".

      I believe the significance of learning algorithms (and other fundamental subjects) is not to implement them from scratch at work, but rather to be able to make professional reactions and judgments when solving problems based on the knowledge learned, thereby improving the overall quality of work. Here is a simple example. Every programming language has a built-in sorting function:

      • If we have not studied data structures and algorithms, we might simply feed any given data to this sorting function. It runs smoothly with good performance, and there doesn't seem to be any problem.
      • But if we have studied algorithms, we would know that the time complexity of the built-in sorting function is \\(O(n \\log n)\\). However, if the given data consists of integers with a fixed number of digits (such as student IDs), we can use the more efficient \"radix sort\", reducing the time complexity to \\(O(nk)\\), where \\(k\\) is the number of digits. When the data volume is very large, the saved running time can create significant value (reduced costs, improved experience, etc.).

      In the field of engineering, a large number of problems are difficult to reach optimal solutions, and many problems are only solved \"approximately\". The difficulty of a problem depends on one hand on the nature of the problem itself, and on the other hand on the knowledge reserve of the person observing the problem. The more complete a person's knowledge and the more experience they have, the deeper their analysis of the problem will be, and the more elegantly the problem can be solved.

      "},{"location":"chapter_introduction/what_is_dsa/","title":"1.2 \u00a0 What Is an Algorithm","text":""},{"location":"chapter_introduction/what_is_dsa/#121-algorithm-definition","title":"1.2.1 \u00a0 Algorithm Definition","text":"

      An algorithm is a set of instructions or operational steps that solves a specific problem within a finite amount of time. It has the following characteristics.

      • The problem is well-defined, with clear input and output definitions.
      • It is feasible and can be completed within a finite number of steps, time, and memory space.
      • Each step has a definite meaning, and under the same input and operating conditions, the output is always the same.
      "},{"location":"chapter_introduction/what_is_dsa/#122-data-structure-definition","title":"1.2.2 \u00a0 Data Structure Definition","text":"

      A data structure is a way of organizing and storing data, covering the data content, relationships between data, and methods for data operations. It has the following design objectives.

      • Occupy as little space as possible to save computer memory.
      • Data operations should be as fast as possible, covering data access, addition, deletion, update, etc.
      • Provide a concise data representation and logical information so that algorithms can run efficiently.

      Data structure design is a process full of trade-offs. If we want to achieve improvements in one aspect, we often need to make compromises in another aspect. Here are two examples.

      • Compared to arrays, linked lists are more convenient for data addition and deletion operations but sacrifice data access speed.
      • Compared to linked lists, graphs provide richer logical information but require larger memory space.
      "},{"location":"chapter_introduction/what_is_dsa/#123-the-relationship-between-data-structures-and-algorithms","title":"1.2.3 \u00a0 The Relationship Between Data Structures and Algorithms","text":"

      As shown in Figure 1-4, data structures and algorithms are highly related and tightly coupled, specifically manifested in the following three aspects.

      • Data structures are the foundation of algorithms. Data structures provide algorithms with structured storage of data and methods for operating on data.
      • Algorithms breathe life into data structures. Data structures themselves only store data information; combined with algorithms, they can solve specific problems.
      • Algorithms can usually be implemented based on different data structures, but execution efficiency may vary greatly. Choosing the appropriate data structure is key.

      Figure 1-4 \u00a0 The relationship between data structures and algorithms

      Data structures and algorithms are like assembling building blocks as shown in Figure 1-5. A set of building blocks, in addition to containing many parts, also comes with detailed assembly instructions. By following the instructions step by step, we can assemble an exquisite building block model.

      Figure 1-5 \u00a0 Assembling blocks

      The detailed correspondence between the two is shown in Table 1-1.

      Table 1-1 \u00a0 Comparing data structures and algorithms to assembling building blocks

      Data structures and algorithms Assembling building blocks Input data Unassembled building blocks Data structure Organization form of building blocks, including shape, size, connection method, etc. Algorithm A series of operational steps to assemble the blocks into the target form Output data Building block model

      It is worth noting that data structures and algorithms are independent of programming languages. For this reason, this book is able to provide implementations based on multiple programming languages.

      Conventional abbreviation

      In actual discussions, we usually abbreviate \"data structures and algorithms\" as \"algorithms\". For example, the well-known LeetCode algorithm problems actually examine knowledge of both data structures and algorithms.

      "},{"location":"chapter_preface/","title":"Chapter 0. \u00a0 Preface","text":"

      Abstract

      Algorithms are like a beautiful symphony, each line of code flows like a melody.

      May this book gently resonate in your mind, leaving a unique and profound melody.

      "},{"location":"chapter_preface/#chapter-contents","title":"Chapter contents","text":"
      • 0.1 \u00a0 About This Book
      • 0.2 \u00a0 How to Use This Book
      • 0.3 \u00a0 Summary
      "},{"location":"chapter_preface/about_the_book/","title":"0.1 \u00a0 About This Book","text":"

      This project aims to create an open-source, free, beginner-friendly introductory tutorial on data structures and algorithms.

      • The entire book uses animated illustrations, with clear and easy-to-understand content and a smooth learning curve, guiding beginners to explore the knowledge map of data structures and algorithms.
      • The source code can be run with one click, helping readers improve their programming skills through practice and understand how algorithms work and the underlying implementation of data structures.
      • We encourage readers to learn from each other, and everyone is welcome to ask questions and share insights in the comments section, making progress together through discussion and exchange.
      "},{"location":"chapter_preface/about_the_book/#011-target-audience","title":"0.1.1 \u00a0 Target Audience","text":"

      If you are an algorithm beginner who has never been exposed to algorithms, or if you already have some problem-solving experience and have a vague understanding of data structures and algorithms, oscillating between knowing and not knowing, then this book is tailor-made for you!

      If you have already accumulated a certain amount of problem-solving experience and are familiar with most question types, this book can help you review and organize your algorithm knowledge system, and the repository's source code can be used as a \"problem-solving toolkit\" or \"algorithm dictionary.\"

      If you are an algorithm \"expert,\" we look forward to receiving your valuable suggestions, or participating in creation together.

      Prerequisites

      You need to have at least a programming foundation in any language, and be able to read and write simple code.

      "},{"location":"chapter_preface/about_the_book/#012-content-structure","title":"0.1.2 \u00a0 Content Structure","text":"

      The main content of this book is shown in Figure 0-1.

      • Complexity analysis: Evaluation dimensions and methods for data structures and algorithms. Methods for calculating time complexity and space complexity, common types, examples, etc.
      • Data structures: Classification methods for basic data types and data structures. The definition, advantages and disadvantages, common operations, common types, typical applications, implementation methods, etc. of data structures such as arrays, linked lists, stacks, queues, hash tables, trees, heaps, and graphs.
      • Algorithms: The definition, advantages and disadvantages, efficiency, application scenarios, problem-solving steps, and example problems of algorithms such as searching, sorting, divide and conquer, backtracking, dynamic programming, and greedy algorithms.

      Figure 0-1 \u00a0 Main content of this book

      "},{"location":"chapter_preface/about_the_book/#013-acknowledgements","title":"0.1.3 \u00a0 Acknowledgements","text":"

      This book has been continuously improved through the joint efforts of many contributors in the open-source community. Thanks to every contributor who invested time and effort, they are (in the order automatically generated by GitHub): krahets, coderonion, Gonglja, nuomi1, Reanon, justin-tse, hpstory, danielsss, curtishd, night-cruise, S-N-O-R-L-A-X, rongyi, msk397, gvenusleo, khoaxuantu, rivertwilight, K3v123, gyt95, zhuoqinyue, yuelinxin, Zuoxun, mingXta, Phoenix0415, FangYuan33, GN-Yu, longsizhuo, IsChristina, xBLACKICEx, guowei-gong, Cathay-Chen, pengchzn, QiLOL, magentaqin, hello-ikun, JoseHung, qualifier1024, thomasq0, sunshinesDL, L-Super, Guanngxu, Transmigration-zhou, WSL0809, Slone123c, lhxsm, yuan0221, what-is-me, Shyam-Chen, theNefelibatas, longranger2, codeberg-user, xiongsp, JeffersonHuang, prinpal, seven1240, Wonderdch, malone6, xiaomiusa87, gaofer, bluebean-cloud, a16su, SamJin98, hongyun-robot, nanlei, XiaChuerwu, yd-j, iron-irax, mgisr, steventimes, junminhong, heshuyue, danny900714, MolDuM, Nigh, Dr-XYZ, XC-Zero, reeswell, PXG-XPG, NI-SW, Horbin-Magician, Enlightenus, YangXuanyi, beatrix-chan, DullSword, xjr7670, jiaxianhua, qq909244296, iStig, boloboloda, hts0000, gledfish, wenjianmin, keshida, kilikilikid, lclc6, lwbaptx, linyejoe2, liuxjerry, llql1211, fbigm, echo1937, szu17dmy, dshlstarr, Yucao-cy, coderlef, czruby, bongbongbakudan, beintentional, ZongYangL, ZhongYuuu, ZhongGuanbin, hezhizhen, linzeyan, ZJKung, luluxia, xb534, ztkuaikuai, yw-1021, ElaBosak233, baagod, zhouLion, yishangzhang, yi427, yanedie, yabo083, weibk, wangwang105, th1nk3r-ing, tao363, 4yDX3906, syd168, sslmj2020, smilelsb, siqyka, selear, sdshaoda, Xi-Row, popozhu, nuquist19, noobcodemaker, XiaoK29, chadyi, lyl625760, lucaswangdev, 0130w, shanghai-Jerry, EJackYang, Javesun99, eltociear, lipusheng, KNChiu, BlindTerran, ShiMaRing, lovelock, FreddieLi, FloranceYeh, fanchenggang, gltianwen, goerll, nedchu, curly210102, CuB3y0nd, KraHsu, CarrotDLaw, youshaoXG, bubble9um, Asashishi, Asa0oo0o0o, fanenr, eagleanurag, akshiterate, 52coder, foursevenlove, KorsChen, GaochaoZhu, hopkings2008, yang-le, realwujing, Evilrabbit520, Umer-Jahangir, Turing-1024-Lee, Suremotoo, paoxiaomooo, Chieko-Seren, Allen-Scai, ymmmas, Risuntsy, Richard-Zhang1019, RafaelCaso, qingpeng9802, primexiao, Urbaner3, zhongfq, nidhoggfgg, MwumLi, CreatorMetaSky, martinx, ZnYang2018, hugtyftg, logan-qiu, psychelzh, Keynman, KeiichiKasai, and KawaiiAsh.

      The code review work for this book was completed by coderonion, curtishd, Gonglja, gvenusleo, hpstory, justin-tse, khoaxuantu, krahets, night-cruise, nuomi1, Reanon and rongyi (in alphabetical order). Thanks to them for the time and effort they put in, it is they who ensure the standardization and unity of code in various languages.

      The Traditional Chinese version of this book was reviewed by Shyam-Chen and Dr-XYZ, and the English version was reviewed by yuelinxin, K3v123, QiLOL, Phoenix0415, SamJin98, yanedie, RafaelCaso, pengchzn, thomasq0 and magentaqin. It is because of their continuous contributions that this book can serve a wider readership, and we thank them.

      The ePub ebook generation tool for this book was developed by zhongfq. We thank him for his contribution, which provides readers with a more flexible way to read.

      During the creation of this book, I received help from many people.

      • Thanks to my mentor at the company, Dr. Li Xi, who encouraged me to \"take action quickly\" during a conversation, strengthening my determination to write this book;
      • Thanks to my girlfriend Bubble as the first reader of this book, who provided many valuable suggestions from the perspective of an algorithm beginner, making this book more suitable for novices to read;
      • Thanks to Tengbao, Qibao, and Feibao for coming up with a creative name for this book, evoking everyone's fond memories of writing their first line of code \"Hello World!\";
      • Thanks to Xiaoquan for providing professional help in intellectual property rights, which played an important role in the improvement of this open-source book;
      • Thanks to Sutong for designing the beautiful cover and logo for this book, and for patiently making revisions multiple times driven by my obsessive-compulsive disorder;
      • Thanks to @squidfunk for the typesetting suggestions, as well as for developing the open-source documentation theme Material-for-MkDocs.

      During the writing process, I read many textbooks and articles on data structures and algorithms. These works provided excellent examples for this book and ensured the accuracy and quality of the book's content. I would like to thank all the teachers and predecessors for their outstanding contributions!

      This book advocates a learning method that combines hands and brain, and in this regard I was deeply inspired by Dive into Deep Learning. I highly recommend this excellent work to all readers.

      Heartfelt thanks to my parents, it is your support and encouragement that has given me the opportunity to do this interesting thing.

      "},{"location":"chapter_preface/suggestions/","title":"0.2 \u00a0 How to Use This Book","text":"

      Tip

      For the best reading experience, it is recommended that you read through this section.

      "},{"location":"chapter_preface/suggestions/#021-writing-style-conventions","title":"0.2.1 \u00a0 Writing Style Conventions","text":"
      • Titles marked with * are optional sections with relatively difficult content. If you have limited time, you can skip them first.
      • Technical terms will be in bold (in paper and PDF versions) or underlined (in web versions), such as array. It is recommended to memorize them for reading literature.
      • Key content and summary statements will be bolded, and such text deserves special attention.
      • Words and phrases with specific meanings will be marked with \"quotation marks\" to avoid ambiguity.
      • When it comes to nouns that are inconsistent between programming languages, this book uses Python as the standard, for example, using None to represent \"null\".
      • This book partially abandons the comment conventions of programming languages in favor of more compact content layout. Comments are mainly divided into three types: title comments, content comments, and multi-line comments.
      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      \"\"\"Title comment, used to label functions, classes, test cases, etc.\"\"\"\n\n# Content comment, used to explain code in detail\n\n\"\"\"\nMulti-line\ncomment\n\"\"\"\n
      /* Title comment, used to label functions, classes, test cases, etc. */\n\n// Content comment, used to explain code in detail\n\n/**\n * Multi-line\n * comment\n */\n
      /* Title comment, used to label functions, classes, test cases, etc. */\n\n// Content comment, used to explain code in detail\n\n/**\n * Multi-line\n * comment\n */\n
      /* Title comment, used to label functions, classes, test cases, etc. */\n\n// Content comment, used to explain code in detail\n\n/**\n * Multi-line\n * comment\n */\n
      /* Title comment, used to label functions, classes, test cases, etc. */\n\n// Content comment, used to explain code in detail\n\n/**\n * Multi-line\n * comment\n */\n
      /* Title comment, used to label functions, classes, test cases, etc. */\n\n// Content comment, used to explain code in detail\n\n/**\n * Multi-line\n * comment\n */\n
      /* Title comment, used to label functions, classes, test cases, etc. */\n\n// Content comment, used to explain code in detail\n\n/**\n * Multi-line\n * comment\n */\n
      /* Title comment, used to label functions, classes, test cases, etc. */\n\n// Content comment, used to explain code in detail\n\n/**\n * Multi-line\n * comment\n */\n
      /* Title comment, used to label functions, classes, test cases, etc. */\n\n// Content comment, used to explain code in detail\n\n/**\n * Multi-line\n * comment\n */\n
      /* Title comment, used to label functions, classes, test cases, etc. */\n\n// Content comment, used to explain code in detail\n\n/**\n * Multi-line\n * comment\n */\n
      /* Title comment, used to label functions, classes, test cases, etc. */\n\n// Content comment, used to explain code in detail\n\n/**\n * Multi-line\n * comment\n */\n
      /* Title comment, used to label functions, classes, test cases, etc. */\n\n// Content comment, used to explain code in detail\n\n/**\n * Multi-line\n * comment\n */\n
      ### Title comment, used to label functions, classes, test cases, etc. ###\n\n# Content comment, used to explain code in detail\n\n# Multi-line\n# comment\n
      "},{"location":"chapter_preface/suggestions/#022-learning-efficiently-with-animated-illustrations","title":"0.2.2 \u00a0 Learning Efficiently with Animated Illustrations","text":"

      Compared to text, videos and images have higher information density and structural organization, making them easier to understand. In this book, key and difficult knowledge will mainly be presented in the form of animated illustrations, with text serving as explanation and supplement.

      If you find that a section of content provides animated illustrations as shown in Figure 0-2 while reading this book, please focus on the illustrations first, with text as a supplement, and combine the two to understand the content.

      Figure 0-2 \u00a0 Example of animated illustrations

      "},{"location":"chapter_preface/suggestions/#023-deepening-understanding-through-code-practice","title":"0.2.3 \u00a0 Deepening Understanding Through Code Practice","text":"

      The accompanying code for this book is hosted in the GitHub repository. As shown in Figure 0-3, the source code comes with test cases and can be run with one click.

      If time permits, it is recommended that you type out the code yourself. If you have limited study time, please at least read through and run all the code.

      Compared to reading code, the process of writing code often brings more rewards. Learning by doing is the real learning.

      Figure 0-3 \u00a0 Example of running code

      The preliminary work for running code is mainly divided into three steps.

      Step 1: Install the local programming environment. Please follow the tutorial shown in the appendix for installation. If already installed, you can skip this step.

      Step 2: Clone or download the code repository. Visit the GitHub repository. If you have already installed Git, you can clone this repository with the following command:

      git clone https://github.com/krahets/hello-algo.git\n

      Of course, you can also click the \"Download ZIP\" button at the location shown in Figure 0-4 to directly download the code compressed package, and then extract it locally.

      Figure 0-4 \u00a0 Clone repository and download code

      Step 3: Run the source code. As shown in Figure 0-5, for code blocks with file names at the top, we can find the corresponding source code files in the codes folder of the repository. The source code files can be run with one click, which will help you save unnecessary debugging time and allow you to focus on learning content.

      Figure 0-5 \u00a0 Code blocks and corresponding source code files

      In addition to running code locally, the web version also supports visual running of Python code (implemented based on pythontutor). As shown in Figure 0-6, you can click \"Visual Run\" below the code block to expand the view and observe the execution process of the algorithm code; you can also click \"Full Screen View\" for a better viewing experience.

      Figure 0-6 \u00a0 Visual running of Python code

      "},{"location":"chapter_preface/suggestions/#024-growing-together-through-questions-and-discussions","title":"0.2.4 \u00a0 Growing Together Through Questions and Discussions","text":"

      When reading this book, please do not easily skip knowledge points that you have not learned well. Feel free to ask your questions in the comments section, and my friends and I will do our best to answer you, and generally reply within two days.

      As shown in Figure 0-7, the web version has a comments section at the bottom of each chapter. I hope you will pay more attention to the content of the comments section. On the one hand, you can learn about the problems that everyone encounters, thus checking for omissions and stimulating deeper thinking. On the other hand, I hope you can generously answer other friends' questions, share your insights, and help others progress.

      Figure 0-7 \u00a0 Example of comments section

      "},{"location":"chapter_preface/suggestions/#025-algorithm-learning-roadmap","title":"0.2.5 \u00a0 Algorithm Learning Roadmap","text":"

      From an overall perspective, we can divide the process of learning data structures and algorithms into three stages.

      1. Stage 1: Algorithm introduction. We need to familiarize ourselves with the characteristics and usage of various data structures, and learn the principles, processes, uses, and efficiency of different algorithms.
      2. Stage 2: Practice algorithm problems. It is recommended to start with popular problems, and accumulate at least 100 problems first, to familiarize yourself with mainstream algorithm problems. When first practicing problems, \"knowledge forgetting\" may be a challenge, but rest assured, this is very normal. We can review problems according to the \"Ebbinghaus forgetting curve\", and usually after 3-5 rounds of repetition, we can firmly remember them. For recommended problem lists and practice plans, please see this GitHub repository.
      3. Stage 3: Building a knowledge system. In terms of learning, we can read algorithm column articles, problem-solving frameworks, and algorithm textbooks to continuously enrich our knowledge system. In terms of practicing problems, we can try advanced problem-solving strategies, such as categorization by topic, one problem multiple solutions, one solution multiple problems, etc. Related problem-solving insights can be found in various communities.

      As shown in Figure 0-8, the content of this book mainly covers \"Stage 1\", aiming to help you more efficiently carry out Stage 2 and Stage 3 learning.

      Figure 0-8 \u00a0 Algorithm learning roadmap

      "},{"location":"chapter_preface/summary/","title":"0.3 \u00a0 Summary","text":""},{"location":"chapter_preface/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • The main audience of this book is algorithm beginners. If you already have a certain foundation, this book can help you systematically review algorithm knowledge, and the source code in the book can also be used as a \"problem-solving toolkit.\"
      • The content of the book mainly includes three parts: complexity analysis, data structures, and algorithms, covering most topics in this field.
      • For algorithm novices, reading an introductory book during the initial learning stage is crucial, as it can help you avoid many detours.
      • The animated illustrations in the book are usually used to introduce key and difficult knowledge. When reading this book, you should pay more attention to these contents.
      • Practice is the best way to learn programming. It is strongly recommended to run the source code and type the code yourself.
      • The web version of this book has a comments section for each chapter, where you are welcome to share your questions and insights at any time.
      "},{"location":"chapter_reference/","title":"References","text":"

      [1] Thomas H. Cormen, et al. Introduction to Algorithms (3rd Edition).

      [2] Aditya Bhargava. Grokking Algorithms: An Illustrated Guide for Programmers and Other Curious People (1st Edition).

      [3] Robert Sedgewick, et al. Algorithms (4th Edition).

      [4] Yan Weimin. Data Structures (C Language Version).

      [5] Deng Junhui. Data Structures (C++ Language Version, Third Edition).

      [6] Mark Allen Weiss, translated by Chen Yue. Data Structures and Algorithm Analysis in Java (Third Edition).

      [7] Cheng Jie. Conversational Data Structures.

      [8] Wang Zheng. The Beauty of Data Structures and Algorithms.

      [9] Gayle Laakmann McDowell. Cracking the Coding Interview: 189 Programming Questions and Solutions (6th Edition).

      [10] Aston Zhang, et al. Dive into Deep Learning.

      "},{"location":"chapter_searching/","title":"Chapter 10. \u00a0 Searching","text":"

      Abstract

      Searching is an adventure into the unknown, where we may need to traverse every corner of the mysterious space, or we may be able to quickly lock onto the target.

      In this journey of discovery, each exploration may yield an unexpected answer.

      "},{"location":"chapter_searching/#chapter-contents","title":"Chapter contents","text":"
      • 10.1 \u00a0 Binary Search
      • 10.2 \u00a0 Binary Search Insertion
      • 10.3 \u00a0 Binary Search Edge Cases
      • 10.4 \u00a0 Hash Optimization Strategy
      • 10.5 \u00a0 Search Algorithms Revisited
      • 10.6 \u00a0 Summary
      "},{"location":"chapter_searching/binary_search/","title":"10.1 \u00a0 Binary Search","text":"

      Binary search is an efficient searching algorithm based on the divide-and-conquer strategy. It leverages the orderliness of data to reduce the search range by half in each round until the target element is found or the search interval becomes empty.

      Question

      Given an array nums of length \\(n\\) with elements arranged in ascending order and no duplicates, search for and return the index of element target in the array. If the array does not contain the element, return \\(-1\\). An example is shown in Figure 10-1.

      Figure 10-1 \u00a0 Binary search example data

      As shown in Figure 10-2, we first initialize pointers \\(i = 0\\) and \\(j = n - 1\\), pointing to the first and last elements of the array respectively, representing the search interval \\([0, n - 1]\\). Note that square brackets denote a closed interval, which includes the boundary values themselves.

      Next, perform the following two steps in a loop:

      1. Calculate the midpoint index \\(m = \\lfloor {(i + j) / 2} \\rfloor\\), where \\(\\lfloor \\: \\rfloor\\) denotes the floor operation.
      2. Compare nums[m] and target, which results in three cases:
        1. When nums[m] < target, it indicates that target is in the interval \\([m + 1, j]\\), so execute \\(i = m + 1\\).
        2. When nums[m] > target, it indicates that target is in the interval \\([i, m - 1]\\), so execute \\(j = m - 1\\).
        3. When nums[m] = target, it indicates that target has been found, so return index \\(m\\).

      If the array does not contain the target element, the search interval will eventually shrink to empty. In this case, return \\(-1\\).

      <1><2><3><4><5><6><7>

      Figure 10-2 \u00a0 Binary search process

      It's worth noting that since both \\(i\\) and \\(j\\) are of int type, \\(i + j\\) may exceed the range of the int type. To avoid large number overflow, we typically use the formula \\(m = \\lfloor {i + (j - i) / 2} \\rfloor\\) to calculate the midpoint.

      The code is shown below:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search.py
      def binary_search(nums: list[int], target: int) -> int:\n    \"\"\"Binary search (closed interval)\"\"\"\n    # Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n    i, j = 0, len(nums) - 1\n    # Loop, exit when the search interval is empty (empty when i > j)\n    while i <= j:\n        # In theory, Python numbers can be infinitely large (depending on memory size), no need to consider large number overflow\n        m = (i + j) // 2  # Calculate midpoint index m\n        if nums[m] < target:\n            i = m + 1  # This means target is in the interval [m+1, j]\n        elif nums[m] > target:\n            j = m - 1  # This means target is in the interval [i, m-1]\n        else:\n            return m  # Found the target element, return its index\n    return -1  # Target element not found, return -1\n
      binary_search.cpp
      /* Binary search (closed interval on both sides) */\nint binarySearch(vector<int> &nums, int target) {\n    // Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n    int i = 0, j = nums.size() - 1;\n    // Loop, exit when the search interval is empty (empty when i > j)\n    while (i <= j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target)    // This means target is in the interval [m+1, j]\n            i = m + 1;\n        else if (nums[m] > target) // This means target is in the interval [i, m-1]\n            j = m - 1;\n        else // Found the target element, return its index\n            return m;\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.java
      /* Binary search (closed interval on both sides) */\nint binarySearch(int[] nums, int target) {\n    // Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n    int i = 0, j = nums.length - 1;\n    // Loop, exit when the search interval is empty (empty when i > j)\n    while (i <= j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target) // This means target is in the interval [m+1, j]\n            i = m + 1;\n        else if (nums[m] > target) // This means target is in the interval [i, m-1]\n            j = m - 1;\n        else // Found the target element, return its index\n            return m;\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.cs
      /* Binary search (closed interval on both sides) */\nint BinarySearch(int[] nums, int target) {\n    // Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n    int i = 0, j = nums.Length - 1;\n    // Loop, exit when the search interval is empty (empty when i > j)\n    while (i <= j) {\n        int m = i + (j - i) / 2;   // Calculate the midpoint index m\n        if (nums[m] < target)      // This means target is in the interval [m+1, j]\n            i = m + 1;\n        else if (nums[m] > target) // This means target is in the interval [i, m-1]\n            j = m - 1;\n        else                       // Found the target element, return its index\n            return m;\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.go
      /* Binary search (closed interval on both sides) */\nfunc binarySearch(nums []int, target int) int {\n    // Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n    i, j := 0, len(nums)-1\n    // Loop, exit when the search interval is empty (empty when i > j)\n    for i <= j {\n        m := i + (j-i)/2      // Calculate the midpoint index m\n        if nums[m] < target { // This means target is in the interval [m+1, j]\n            i = m + 1\n        } else if nums[m] > target { // This means target is in the interval [i, m-1]\n            j = m - 1\n        } else { // Found the target element, return its index\n            return m\n        }\n    }\n    // Target element not found, return -1\n    return -1\n}\n
      binary_search.swift
      /* Binary search (closed interval on both sides) */\nfunc binarySearch(nums: [Int], target: Int) -> Int {\n    // Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n    var i = nums.startIndex\n    var j = nums.endIndex - 1\n    // Loop, exit when the search interval is empty (empty when i > j)\n    while i <= j {\n        let m = i + (j - i) / 2 // Calculate the midpoint index m\n        if nums[m] < target { // This means target is in the interval [m+1, j]\n            i = m + 1\n        } else if nums[m] > target { // This means target is in the interval [i, m-1]\n            j = m - 1\n        } else { // Found the target element, return its index\n            return m\n        }\n    }\n    // Target element not found, return -1\n    return -1\n}\n
      binary_search.js
      /* Binary search (closed interval on both sides) */\nfunction binarySearch(nums, target) {\n    // Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n    let i = 0,\n        j = nums.length - 1;\n    // Loop, exit when the search interval is empty (empty when i > j)\n    while (i <= j) {\n        // Calculate midpoint index m, use parseInt() to round down\n        const m = parseInt(i + (j - i) / 2);\n        if (nums[m] < target)\n            // This means target is in the interval [m+1, j]\n            i = m + 1;\n        else if (nums[m] > target)\n            // This means target is in the interval [i, m-1]\n            j = m - 1;\n        else return m; // Found the target element, return its index\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.ts
      /* Binary search (closed interval on both sides) */\nfunction binarySearch(nums: number[], target: number): number {\n    // Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n    let i = 0,\n        j = nums.length - 1;\n    // Loop, exit when the search interval is empty (empty when i > j)\n    while (i <= j) {\n        // Calculate the midpoint index m\n        const m = Math.floor(i + (j - i) / 2);\n        if (nums[m] < target) {\n            // This means target is in the interval [m+1, j]\n            i = m + 1;\n        } else if (nums[m] > target) {\n            // This means target is in the interval [i, m-1]\n            j = m - 1;\n        } else {\n            // Found the target element, return its index\n            return m;\n        }\n    }\n    return -1; // Target element not found, return -1\n}\n
      binary_search.dart
      /* Binary search (closed interval on both sides) */\nint binarySearch(List<int> nums, int target) {\n  // Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n  int i = 0, j = nums.length - 1;\n  // Loop, exit when the search interval is empty (empty when i > j)\n  while (i <= j) {\n    int m = i + (j - i) ~/ 2; // Calculate the midpoint index m\n    if (nums[m] < target) {\n      // This means target is in the interval [m+1, j]\n      i = m + 1;\n    } else if (nums[m] > target) {\n      // This means target is in the interval [i, m-1]\n      j = m - 1;\n    } else {\n      // Found the target element, return its index\n      return m;\n    }\n  }\n  // Target element not found, return -1\n  return -1;\n}\n
      binary_search.rs
      /* Binary search (closed interval on both sides) */\nfn binary_search(nums: &[i32], target: i32) -> i32 {\n    // Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n    let mut i = 0;\n    let mut j = nums.len() as i32 - 1;\n    // Loop, exit when the search interval is empty (empty when i > j)\n    while i <= j {\n        let m = i + (j - i) / 2; // Calculate the midpoint index m\n        if nums[m as usize] < target {\n            // This means target is in the interval [m+1, j]\n            i = m + 1;\n        } else if nums[m as usize] > target {\n            // This means target is in the interval [i, m-1]\n            j = m - 1;\n        } else {\n            // Found the target element, return its index\n            return m;\n        }\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.c
      /* Binary search (closed interval on both sides) */\nint binarySearch(int *nums, int len, int target) {\n    // Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n    int i = 0, j = len - 1;\n    // Loop, exit when the search interval is empty (empty when i > j)\n    while (i <= j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target)    // This means target is in the interval [m+1, j]\n            i = m + 1;\n        else if (nums[m] > target) // This means target is in the interval [i, m-1]\n            j = m - 1;\n        else // Found the target element, return its index\n            return m;\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.kt
      /* Binary search (closed interval on both sides) */\nfun binarySearch(nums: IntArray, target: Int): Int {\n    // Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n    var i = 0\n    var j = nums.size - 1\n    // Loop, exit when the search interval is empty (empty when i > j)\n    while (i <= j) {\n        val m = i + (j - i) / 2 // Calculate the midpoint index m\n        if (nums[m] < target) // This means target is in the interval [m+1, j]\n            i = m + 1\n        else if (nums[m] > target) // This means target is in the interval [i, m-1]\n            j = m - 1\n        else  // Found the target element, return its index\n            return m\n    }\n    // Target element not found, return -1\n    return -1\n}\n
      binary_search.rb
      ### Binary search (closed interval) ###\ndef binary_search(nums, target)\n  # Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n  i, j = 0, nums.length - 1\n\n  # Loop, exit when the search interval is empty (empty when i > j)\n  while i <= j\n    # In theory, Ruby numbers can be infinitely large (limited by memory), no need to consider overflow\n    m = (i + j) / 2   # Calculate the midpoint index m\n\n    if nums[m] < target\n      i = m + 1 # This means target is in the interval [m+1, j]\n    elsif nums[m] > target\n      j = m - 1 # This means target is in the interval [i, m-1]\n    else\n      return m  # Found the target element, return its index\n    end\n  end\n\n  -1  # Target element not found, return -1\nend\n

      Time complexity is \\(O(\\log n)\\): In the binary loop, the interval is reduced by half each round, so the number of loops is \\(\\log_2 n\\).

      Space complexity is \\(O(1)\\): Pointers \\(i\\) and \\(j\\) use constant-size space.

      "},{"location":"chapter_searching/binary_search/#1011-interval-representation-methods","title":"10.1.1 \u00a0 Interval Representation Methods","text":"

      In addition to the closed interval mentioned above, another common interval representation is the \"left-closed right-open\" interval, defined as \\([0, n)\\), meaning the left boundary includes itself while the right boundary does not. Under this representation, the interval \\([i, j)\\) is empty when \\(i = j\\).

      We can implement a binary search algorithm with the same functionality based on this representation:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search.py
      def binary_search_lcro(nums: list[int], target: int) -> int:\n    \"\"\"Binary search (left-closed right-open interval)\"\"\"\n    # Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n    i, j = 0, len(nums)\n    # Loop, exit when the search interval is empty (empty when i = j)\n    while i < j:\n        m = (i + j) // 2  # Calculate midpoint index m\n        if nums[m] < target:\n            i = m + 1  # This means target is in the interval [m+1, j)\n        elif nums[m] > target:\n            j = m  # This means target is in the interval [i, m)\n        else:\n            return m  # Found the target element, return its index\n    return -1  # Target element not found, return -1\n
      binary_search.cpp
      /* Binary search (left-closed right-open interval) */\nint binarySearchLCRO(vector<int> &nums, int target) {\n    // Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n    int i = 0, j = nums.size();\n    // Loop, exit when the search interval is empty (empty when i = j)\n    while (i < j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target)    // This means target is in the interval [m+1, j)\n            i = m + 1;\n        else if (nums[m] > target) // This means target is in the interval [i, m)\n            j = m;\n        else // Found the target element, return its index\n            return m;\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.java
      /* Binary search (left-closed right-open interval) */\nint binarySearchLCRO(int[] nums, int target) {\n    // Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n    int i = 0, j = nums.length;\n    // Loop, exit when the search interval is empty (empty when i = j)\n    while (i < j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target) // This means target is in the interval [m+1, j)\n            i = m + 1;\n        else if (nums[m] > target) // This means target is in the interval [i, m)\n            j = m;\n        else // Found the target element, return its index\n            return m;\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.cs
      /* Binary search (left-closed right-open interval) */\nint BinarySearchLCRO(int[] nums, int target) {\n    // Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n    int i = 0, j = nums.Length;\n    // Loop, exit when the search interval is empty (empty when i = j)\n    while (i < j) {\n        int m = i + (j - i) / 2;   // Calculate the midpoint index m\n        if (nums[m] < target)      // This means target is in the interval [m+1, j)\n            i = m + 1;\n        else if (nums[m] > target) // This means target is in the interval [i, m)\n            j = m;\n        else                       // Found the target element, return its index\n            return m;\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.go
      /* Binary search (left-closed right-open interval) */\nfunc binarySearchLCRO(nums []int, target int) int {\n    // Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n    i, j := 0, len(nums)\n    // Loop, exit when the search interval is empty (empty when i = j)\n    for i < j {\n        m := i + (j-i)/2      // Calculate the midpoint index m\n        if nums[m] < target { // This means target is in the interval [m+1, j)\n            i = m + 1\n        } else if nums[m] > target { // This means target is in the interval [i, m)\n            j = m\n        } else { // Found the target element, return its index\n            return m\n        }\n    }\n    // Target element not found, return -1\n    return -1\n}\n
      binary_search.swift
      /* Binary search (left-closed right-open interval) */\nfunc binarySearchLCRO(nums: [Int], target: Int) -> Int {\n    // Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n    var i = nums.startIndex\n    var j = nums.endIndex\n    // Loop, exit when the search interval is empty (empty when i = j)\n    while i < j {\n        let m = i + (j - i) / 2 // Calculate the midpoint index m\n        if nums[m] < target { // This means target is in the interval [m+1, j)\n            i = m + 1\n        } else if nums[m] > target { // This means target is in the interval [i, m)\n            j = m\n        } else { // Found the target element, return its index\n            return m\n        }\n    }\n    // Target element not found, return -1\n    return -1\n}\n
      binary_search.js
      /* Binary search (left-closed right-open interval) */\nfunction binarySearchLCRO(nums, target) {\n    // Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n    let i = 0,\n        j = nums.length;\n    // Loop, exit when the search interval is empty (empty when i = j)\n    while (i < j) {\n        // Calculate midpoint index m, use parseInt() to round down\n        const m = parseInt(i + (j - i) / 2);\n        if (nums[m] < target)\n            // This means target is in the interval [m+1, j)\n            i = m + 1;\n        else if (nums[m] > target)\n            // This means target is in the interval [i, m)\n            j = m;\n        // Found the target element, return its index\n        else return m;\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.ts
      /* Binary search (left-closed right-open interval) */\nfunction binarySearchLCRO(nums: number[], target: number): number {\n    // Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n    let i = 0,\n        j = nums.length;\n    // Loop, exit when the search interval is empty (empty when i = j)\n    while (i < j) {\n        // Calculate the midpoint index m\n        const m = Math.floor(i + (j - i) / 2);\n        if (nums[m] < target) {\n            // This means target is in the interval [m+1, j)\n            i = m + 1;\n        } else if (nums[m] > target) {\n            // This means target is in the interval [i, m)\n            j = m;\n        } else {\n            // Found the target element, return its index\n            return m;\n        }\n    }\n    return -1; // Target element not found, return -1\n}\n
      binary_search.dart
      /* Binary search (left-closed right-open interval) */\nint binarySearchLCRO(List<int> nums, int target) {\n  // Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n  int i = 0, j = nums.length;\n  // Loop, exit when the search interval is empty (empty when i = j)\n  while (i < j) {\n    int m = i + (j - i) ~/ 2; // Calculate the midpoint index m\n    if (nums[m] < target) {\n      // This means target is in the interval [m+1, j)\n      i = m + 1;\n    } else if (nums[m] > target) {\n      // This means target is in the interval [i, m)\n      j = m;\n    } else {\n      // Found the target element, return its index\n      return m;\n    }\n  }\n  // Target element not found, return -1\n  return -1;\n}\n
      binary_search.rs
      /* Binary search (left-closed right-open interval) */\nfn binary_search_lcro(nums: &[i32], target: i32) -> i32 {\n    // Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n    let mut i = 0;\n    let mut j = nums.len() as i32;\n    // Loop, exit when the search interval is empty (empty when i = j)\n    while i < j {\n        let m = i + (j - i) / 2; // Calculate the midpoint index m\n        if nums[m as usize] < target {\n            // This means target is in the interval [m+1, j)\n            i = m + 1;\n        } else if nums[m as usize] > target {\n            // This means target is in the interval [i, m)\n            j = m;\n        } else {\n            // Found the target element, return its index\n            return m;\n        }\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.c
      /* Binary search (left-closed right-open interval) */\nint binarySearchLCRO(int *nums, int len, int target) {\n    // Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n    int i = 0, j = len;\n    // Loop, exit when the search interval is empty (empty when i = j)\n    while (i < j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target)    // This means target is in the interval [m+1, j)\n            i = m + 1;\n        else if (nums[m] > target) // This means target is in the interval [i, m)\n            j = m;\n        else // Found the target element, return its index\n            return m;\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.kt
      /* Binary search (left-closed right-open interval) */\nfun binarySearchLCRO(nums: IntArray, target: Int): Int {\n    // Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n    var i = 0\n    var j = nums.size\n    // Loop, exit when the search interval is empty (empty when i = j)\n    while (i < j) {\n        val m = i + (j - i) / 2 // Calculate the midpoint index m\n        if (nums[m] < target) // This means target is in the interval [m+1, j)\n            i = m + 1\n        else if (nums[m] > target) // This means target is in the interval [i, m)\n            j = m\n        else  // Found the target element, return its index\n            return m\n    }\n    // Target element not found, return -1\n    return -1\n}\n
      binary_search.rb
      ### Binary search (left-closed right-open interval) ###\ndef binary_search_lcro(nums, target)\n  # Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n  i, j = 0, nums.length\n\n  # Loop, exit when the search interval is empty (empty when i = j)\n  while i < j\n    # Calculate the midpoint index m\n    m = (i + j) / 2\n\n    if nums[m] < target\n      i = m + 1 # This means target is in the interval [m+1, j)\n    elsif nums[m] > target\n      j = m - 1 # This means target is in the interval [i, m)\n    else\n      return m  # Found the target element, return its index\n    end\n  end\n\n  -1  # Target element not found, return -1\nend\n

      As shown in Figure 10-3, under the two interval representations, the initialization, loop condition, and interval narrowing operations of the binary search algorithm are all different.

      Since both the left and right boundaries in the \"closed interval\" representation are defined as closed, the operations to narrow the interval through pointers \\(i\\) and \\(j\\) are also symmetric. This makes it less error-prone, so the \"closed interval\" approach is generally recommended.

      Figure 10-3 \u00a0 Two interval definitions

      "},{"location":"chapter_searching/binary_search/#1012-advantages-and-limitations","title":"10.1.2 \u00a0 Advantages and Limitations","text":"

      Binary search performs well in both time and space aspects.

      • Binary search has high time efficiency. With large data volumes, the logarithmic time complexity has significant advantages. For example, when the data size \\(n = 2^{20}\\), linear search requires \\(2^{20} = 1048576\\) loop rounds, while binary search only needs \\(\\log_2 2^{20} = 20\\) rounds.
      • Binary search requires no extra space. Compared to searching algorithms that require additional space (such as hash-based search), binary search is more space-efficient.

      However, binary search is not suitable for all situations, mainly for the following reasons:

      • Binary search is only applicable to sorted data. If the input data is unsorted, sorting specifically to use binary search would be counterproductive, as sorting algorithms typically have a time complexity of \\(O(n \\log n)\\), which is higher than both linear search and binary search. For scenarios with frequent element insertions, maintaining array orderliness requires inserting elements at specific positions with a time complexity of \\(O(n)\\), which is also very expensive.
      • Binary search is only applicable to arrays. Binary search requires jump-style (non-contiguous) element access, and jump-style access has low efficiency in linked lists, making it unsuitable for linked lists or data structures based on linked list implementations.
      • For small data volumes, linear search performs better. In linear search, each round requires only 1 comparison operation; while in binary search, it requires 1 addition, 1 division, 1-3 comparison operations, and 1 addition (subtraction), totaling 4-6 unit operations. Therefore, when the data volume \\(n\\) is small, linear search is actually faster than binary search.
      "},{"location":"chapter_searching/binary_search_edge/","title":"10.3 \u00a0 Binary Search Edge Cases","text":""},{"location":"chapter_searching/binary_search_edge/#1031-finding-the-left-boundary","title":"10.3.1 \u00a0 Finding the Left Boundary","text":"

      Question

      Given a sorted array nums of length \\(n\\) that may contain duplicate elements, return the index of the leftmost element target in the array. If the array does not contain the element, return \\(-1\\).

      Recall the method for finding the insertion point with binary search. After the search completes, \\(i\\) points to the leftmost target, so finding the insertion point is essentially finding the index of the leftmost target.

      Consider implementing the left boundary search using the insertion point finding function. Note that the array may not contain target, which could result in the following two cases:

      • The insertion point index \\(i\\) is out of bounds.
      • The element nums[i] is not equal to target.

      When either of these situations occurs, simply return \\(-1\\). The code is shown below:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_edge.py
      def binary_search_left_edge(nums: list[int], target: int) -> int:\n    \"\"\"Binary search for the leftmost target\"\"\"\n    # Equivalent to finding the insertion point of target\n    i = binary_search_insertion(nums, target)\n    # Target not found, return -1\n    if i == len(nums) or nums[i] != target:\n        return -1\n    # Found target, return index i\n    return i\n
      binary_search_edge.cpp
      /* Binary search for the leftmost target */\nint binarySearchLeftEdge(vector<int> &nums, int target) {\n    // Equivalent to finding the insertion point of target\n    int i = binarySearchInsertion(nums, target);\n    // Target not found, return -1\n    if (i == nums.size() || nums[i] != target) {\n        return -1;\n    }\n    // Found target, return index i\n    return i;\n}\n
      binary_search_edge.java
      /* Binary search for the leftmost target */\nint binarySearchLeftEdge(int[] nums, int target) {\n    // Equivalent to finding the insertion point of target\n    int i = binary_search_insertion.binarySearchInsertion(nums, target);\n    // Target not found, return -1\n    if (i == nums.length || nums[i] != target) {\n        return -1;\n    }\n    // Found target, return index i\n    return i;\n}\n
      binary_search_edge.cs
      /* Binary search for the leftmost target */\nint BinarySearchLeftEdge(int[] nums, int target) {\n    // Equivalent to finding the insertion point of target\n    int i = binary_search_insertion.BinarySearchInsertion(nums, target);\n    // Target not found, return -1\n    if (i == nums.Length || nums[i] != target) {\n        return -1;\n    }\n    // Found target, return index i\n    return i;\n}\n
      binary_search_edge.go
      /* Binary search for the leftmost target */\nfunc binarySearchLeftEdge(nums []int, target int) int {\n    // Equivalent to finding the insertion point of target\n    i := binarySearchInsertion(nums, target)\n    // Target not found, return -1\n    if i == len(nums) || nums[i] != target {\n        return -1\n    }\n    // Found target, return index i\n    return i\n}\n
      binary_search_edge.swift
      /* Binary search for the leftmost target */\nfunc binarySearchLeftEdge(nums: [Int], target: Int) -> Int {\n    // Equivalent to finding the insertion point of target\n    let i = binarySearchInsertion(nums: nums, target: target)\n    // Target not found, return -1\n    if i == nums.endIndex || nums[i] != target {\n        return -1\n    }\n    // Found target, return index i\n    return i\n}\n
      binary_search_edge.js
      /* Binary search for the leftmost target */\nfunction binarySearchLeftEdge(nums, target) {\n    // Equivalent to finding the insertion point of target\n    const i = binarySearchInsertion(nums, target);\n    // Target not found, return -1\n    if (i === nums.length || nums[i] !== target) {\n        return -1;\n    }\n    // Found target, return index i\n    return i;\n}\n
      binary_search_edge.ts
      /* Binary search for the leftmost target */\nfunction binarySearchLeftEdge(nums: Array<number>, target: number): number {\n    // Equivalent to finding the insertion point of target\n    const i = binarySearchInsertion(nums, target);\n    // Target not found, return -1\n    if (i === nums.length || nums[i] !== target) {\n        return -1;\n    }\n    // Found target, return index i\n    return i;\n}\n
      binary_search_edge.dart
      /* Binary search for the leftmost target */\nint binarySearchLeftEdge(List<int> nums, int target) {\n  // Equivalent to finding the insertion point of target\n  int i = binarySearchInsertion(nums, target);\n  // Target not found, return -1\n  if (i == nums.length || nums[i] != target) {\n    return -1;\n  }\n  // Found target, return index i\n  return i;\n}\n
      binary_search_edge.rs
      /* Binary search for the leftmost target */\nfn binary_search_left_edge(nums: &[i32], target: i32) -> i32 {\n    // Equivalent to finding the insertion point of target\n    let i = binary_search_insertion(nums, target);\n    // Target not found, return -1\n    if i == nums.len() as i32 || nums[i as usize] != target {\n        return -1;\n    }\n    // Found target, return index i\n    i\n}\n
      binary_search_edge.c
      /* Binary search for the leftmost target */\nint binarySearchLeftEdge(int *nums, int numSize, int target) {\n    // Equivalent to finding the insertion point of target\n    int i = binarySearchInsertion(nums, numSize, target);\n    // Target not found, return -1\n    if (i == numSize || nums[i] != target) {\n        return -1;\n    }\n    // Found target, return index i\n    return i;\n}\n
      binary_search_edge.kt
      /* Binary search for the leftmost target */\nfun binarySearchLeftEdge(nums: IntArray, target: Int): Int {\n    // Equivalent to finding the insertion point of target\n    val i = binarySearchInsertion(nums, target)\n    // Target not found, return -1\n    if (i == nums.size || nums[i] != target) {\n        return -1\n    }\n    // Found target, return index i\n    return i\n}\n
      binary_search_edge.rb
      ### Binary search leftmost target ###\ndef binary_search_left_edge(nums, target)\n  # Equivalent to finding the insertion point of target\n  i = binary_search_insertion(nums, target)\n\n  # Target not found, return -1\n  return -1 if i == nums.length || nums[i] != target\n\n  i # Found target, return index i\nend\n
      "},{"location":"chapter_searching/binary_search_edge/#1032-finding-the-right-boundary","title":"10.3.2 \u00a0 Finding the Right Boundary","text":"

      So how do we find the rightmost target? The most direct approach is to modify the code and replace the pointer shrinking operation in the nums[m] == target case. The code is omitted here; interested readers can implement it themselves.

      Below we introduce two more clever methods.

      "},{"location":"chapter_searching/binary_search_edge/#1-reusing-left-boundary-search","title":"1. \u00a0 Reusing Left Boundary Search","text":"

      In fact, we can use the function for finding the leftmost element to find the rightmost element. The specific method is: Convert finding the rightmost target into finding the leftmost target + 1.

      As shown in Figure 10-7, after the search completes, pointer \\(i\\) points to the leftmost target + 1 (if it exists), while \\(j\\) points to the rightmost target, so we can simply return \\(j\\).

      Figure 10-7 \u00a0 Converting right boundary search to left boundary search

      Note that the returned insertion point is \\(i\\), so we need to subtract \\(1\\) from it to obtain \\(j\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_edge.py
      def binary_search_right_edge(nums: list[int], target: int) -> int:\n    \"\"\"Binary search for the rightmost target\"\"\"\n    # Convert to finding the leftmost target + 1\n    i = binary_search_insertion(nums, target + 1)\n    # j points to the rightmost target, i points to the first element greater than target\n    j = i - 1\n    # Target not found, return -1\n    if j == -1 or nums[j] != target:\n        return -1\n    # Found target, return index j\n    return j\n
      binary_search_edge.cpp
      /* Binary search for the rightmost target */\nint binarySearchRightEdge(vector<int> &nums, int target) {\n    // Convert to finding the leftmost target + 1\n    int i = binarySearchInsertion(nums, target + 1);\n    // j points to the rightmost target, i points to the first element greater than target\n    int j = i - 1;\n    // Target not found, return -1\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // Found target, return index j\n    return j;\n}\n
      binary_search_edge.java
      /* Binary search for the rightmost target */\nint binarySearchRightEdge(int[] nums, int target) {\n    // Convert to finding the leftmost target + 1\n    int i = binary_search_insertion.binarySearchInsertion(nums, target + 1);\n    // j points to the rightmost target, i points to the first element greater than target\n    int j = i - 1;\n    // Target not found, return -1\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // Found target, return index j\n    return j;\n}\n
      binary_search_edge.cs
      /* Binary search for the rightmost target */\nint BinarySearchRightEdge(int[] nums, int target) {\n    // Convert to finding the leftmost target + 1\n    int i = binary_search_insertion.BinarySearchInsertion(nums, target + 1);\n    // j points to the rightmost target, i points to the first element greater than target\n    int j = i - 1;\n    // Target not found, return -1\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // Found target, return index j\n    return j;\n}\n
      binary_search_edge.go
      /* Binary search for the rightmost target */\nfunc binarySearchRightEdge(nums []int, target int) int {\n    // Convert to finding the leftmost target + 1\n    i := binarySearchInsertion(nums, target+1)\n    // j points to the rightmost target, i points to the first element greater than target\n    j := i - 1\n    // Target not found, return -1\n    if j == -1 || nums[j] != target {\n        return -1\n    }\n    // Found target, return index j\n    return j\n}\n
      binary_search_edge.swift
      /* Binary search for the rightmost target */\nfunc binarySearchRightEdge(nums: [Int], target: Int) -> Int {\n    // Convert to finding the leftmost target + 1\n    let i = binarySearchInsertion(nums: nums, target: target + 1)\n    // j points to the rightmost target, i points to the first element greater than target\n    let j = i - 1\n    // Target not found, return -1\n    if j == -1 || nums[j] != target {\n        return -1\n    }\n    // Found target, return index j\n    return j\n}\n
      binary_search_edge.js
      /* Binary search for the rightmost target */\nfunction binarySearchRightEdge(nums, target) {\n    // Convert to finding the leftmost target + 1\n    const i = binarySearchInsertion(nums, target + 1);\n    // j points to the rightmost target, i points to the first element greater than target\n    const j = i - 1;\n    // Target not found, return -1\n    if (j === -1 || nums[j] !== target) {\n        return -1;\n    }\n    // Found target, return index j\n    return j;\n}\n
      binary_search_edge.ts
      /* Binary search for the rightmost target */\nfunction binarySearchRightEdge(nums: Array<number>, target: number): number {\n    // Convert to finding the leftmost target + 1\n    const i = binarySearchInsertion(nums, target + 1);\n    // j points to the rightmost target, i points to the first element greater than target\n    const j = i - 1;\n    // Target not found, return -1\n    if (j === -1 || nums[j] !== target) {\n        return -1;\n    }\n    // Found target, return index j\n    return j;\n}\n
      binary_search_edge.dart
      /* Binary search for the rightmost target */\nint binarySearchRightEdge(List<int> nums, int target) {\n  // Convert to finding the leftmost target + 1\n  int i = binarySearchInsertion(nums, target + 1);\n  // j points to the rightmost target, i points to the first element greater than target\n  int j = i - 1;\n  // Target not found, return -1\n  if (j == -1 || nums[j] != target) {\n    return -1;\n  }\n  // Found target, return index j\n  return j;\n}\n
      binary_search_edge.rs
      /* Binary search for the rightmost target */\nfn binary_search_right_edge(nums: &[i32], target: i32) -> i32 {\n    // Convert to finding the leftmost target + 1\n    let i = binary_search_insertion(nums, target + 1);\n    // j points to the rightmost target, i points to the first element greater than target\n    let j = i - 1;\n    // Target not found, return -1\n    if j == -1 || nums[j as usize] != target {\n        return -1;\n    }\n    // Found target, return index j\n    j\n}\n
      binary_search_edge.c
      /* Binary search for the rightmost target */\nint binarySearchRightEdge(int *nums, int numSize, int target) {\n    // Convert to finding the leftmost target + 1\n    int i = binarySearchInsertion(nums, numSize, target + 1);\n    // j points to the rightmost target, i points to the first element greater than target\n    int j = i - 1;\n    // Target not found, return -1\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // Found target, return index j\n    return j;\n}\n
      binary_search_edge.kt
      /* Binary search for the rightmost target */\nfun binarySearchRightEdge(nums: IntArray, target: Int): Int {\n    // Convert to finding the leftmost target + 1\n    val i = binarySearchInsertion(nums, target + 1)\n    // j points to the rightmost target, i points to the first element greater than target\n    val j = i - 1\n    // Target not found, return -1\n    if (j == -1 || nums[j] != target) {\n        return -1\n    }\n    // Found target, return index j\n    return j\n}\n
      binary_search_edge.rb
      ### Binary search rightmost target ###\ndef binary_search_right_edge(nums, target)\n  # Convert to finding the leftmost target + 1\n  i = binary_search_insertion(nums, target + 1)\n\n  # j points to the rightmost target, i points to the first element greater than target\n  j = i - 1\n\n  # Target not found, return -1\n  return -1 if j == -1 || nums[j] != target\n\n  j # Found target, return index j\nend\n
      "},{"location":"chapter_searching/binary_search_edge/#2-converting-to-element-search","title":"2. \u00a0 Converting to Element Search","text":"

      We know that when the array does not contain target, \\(i\\) and \\(j\\) will eventually point to the first elements greater than and less than target, respectively.

      Therefore, as shown in Figure 10-8, we can construct an element that does not exist in the array to find the left and right boundaries.

      • Finding the leftmost target: Can be converted to finding target - 0.5 and returning pointer \\(i\\).
      • Finding the rightmost target: Can be converted to finding target + 0.5 and returning pointer \\(j\\).

      Figure 10-8 \u00a0 Converting boundary search to element search

      The code is omitted here, but the following two points are worth noting:

      • Since the given array does not contain decimals, we don't need to worry about how to handle equal cases.
      • Because this method introduces decimals, the variable target in the function needs to be changed to a floating-point type (Python does not require this change).
      "},{"location":"chapter_searching/binary_search_insertion/","title":"10.2 \u00a0 Binary Search Insertion Point","text":"

      Binary search can not only be used to search for target elements but also to solve many variant problems, such as searching for the insertion position of a target element.

      "},{"location":"chapter_searching/binary_search_insertion/#1021-case-without-duplicate-elements","title":"10.2.1 \u00a0 Case Without Duplicate Elements","text":"

      Question

      Given a sorted array nums of length \\(n\\) and an element target, where the array contains no duplicate elements. Insert target into the array nums while maintaining its sorted order. If the array already contains the element target, insert it to its left. Return the index of target in the array after insertion. An example is shown in Figure 10-4.

      Figure 10-4 \u00a0 Binary search insertion point example data

      If we want to reuse the binary search code from the previous section, we need to answer the following two questions.

      Question 1: When the array contains target, is the insertion point index the same as that element's index?

      The problem requires inserting target to the left of equal elements, which means the newly inserted target replaces the position of the original target. In other words, when the array contains target, the insertion point index is the index of that target.

      Question 2: When the array does not contain target, what is the insertion point index?

      Further consider the binary search process: When nums[m] < target, \\(i\\) moves, which means pointer \\(i\\) is approaching elements greater than or equal to target. Similarly, pointer \\(j\\) is always approaching elements less than or equal to target.

      Therefore, when the binary search ends, we must have: \\(i\\) points to the first element greater than target, and \\(j\\) points to the first element less than target. It's easy to see that when the array does not contain target, the insertion index is \\(i\\). The code is shown below:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_insertion.py
      def binary_search_insertion_simple(nums: list[int], target: int) -> int:\n    \"\"\"Binary search for insertion point (no duplicate elements)\"\"\"\n    i, j = 0, len(nums) - 1  # Initialize closed interval [0, n-1]\n    while i <= j:\n        m = (i + j) // 2  # Calculate midpoint index m\n        if nums[m] < target:\n            i = m + 1  # target is in the interval [m+1, j]\n        elif nums[m] > target:\n            j = m - 1  # target is in the interval [i, m-1]\n        else:\n            return m  # Found target, return insertion point m\n    # Target not found, return insertion point i\n    return i\n
      binary_search_insertion.cpp
      /* Binary search for insertion point (no duplicate elements) */\nint binarySearchInsertionSimple(vector<int> &nums, int target) {\n    int i = 0, j = nums.size() - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            return m; // Found target, return insertion point m\n        }\n    }\n    // Target not found, return insertion point i\n    return i;\n}\n
      binary_search_insertion.java
      /* Binary search for insertion point (no duplicate elements) */\nint binarySearchInsertionSimple(int[] nums, int target) {\n    int i = 0, j = nums.length - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            return m; // Found target, return insertion point m\n        }\n    }\n    // Target not found, return insertion point i\n    return i;\n}\n
      binary_search_insertion.cs
      /* Binary search for insertion point (no duplicate elements) */\nint BinarySearchInsertionSimple(int[] nums, int target) {\n    int i = 0, j = nums.Length - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            return m; // Found target, return insertion point m\n        }\n    }\n    // Target not found, return insertion point i\n    return i;\n}\n
      binary_search_insertion.go
      /* Binary search for insertion point (no duplicate elements) */\nfunc binarySearchInsertionSimple(nums []int, target int) int {\n    // Initialize closed interval [0, n-1]\n    i, j := 0, len(nums)-1\n    for i <= j {\n        // Calculate the midpoint index m\n        m := i + (j-i)/2\n        if nums[m] < target {\n            // target is in the interval [m+1, j]\n            i = m + 1\n        } else if nums[m] > target {\n            // target is in the interval [i, m-1]\n            j = m - 1\n        } else {\n            // Found target, return insertion point m\n            return m\n        }\n    }\n    // Target not found, return insertion point i\n    return i\n}\n
      binary_search_insertion.swift
      /* Binary search for insertion point (no duplicate elements) */\nfunc binarySearchInsertionSimple(nums: [Int], target: Int) -> Int {\n    // Initialize closed interval [0, n-1]\n    var i = nums.startIndex\n    var j = nums.endIndex - 1\n    while i <= j {\n        let m = i + (j - i) / 2 // Calculate the midpoint index m\n        if nums[m] < target {\n            i = m + 1 // target is in the interval [m+1, j]\n        } else if nums[m] > target {\n            j = m - 1 // target is in the interval [i, m-1]\n        } else {\n            return m // Found target, return insertion point m\n        }\n    }\n    // Target not found, return insertion point i\n    return i\n}\n
      binary_search_insertion.js
      /* Binary search for insertion point (no duplicate elements) */\nfunction binarySearchInsertionSimple(nums, target) {\n    let i = 0,\n        j = nums.length - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        const m = Math.floor(i + (j - i) / 2); // Calculate midpoint index m, use Math.floor() to round down\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            return m; // Found target, return insertion point m\n        }\n    }\n    // Target not found, return insertion point i\n    return i;\n}\n
      binary_search_insertion.ts
      /* Binary search for insertion point (no duplicate elements) */\nfunction binarySearchInsertionSimple(\n    nums: Array<number>,\n    target: number\n): number {\n    let i = 0,\n        j = nums.length - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        const m = Math.floor(i + (j - i) / 2); // Calculate midpoint index m, use Math.floor() to round down\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            return m; // Found target, return insertion point m\n        }\n    }\n    // Target not found, return insertion point i\n    return i;\n}\n
      binary_search_insertion.dart
      /* Binary search for insertion point (no duplicate elements) */\nint binarySearchInsertionSimple(List<int> nums, int target) {\n  int i = 0, j = nums.length - 1; // Initialize closed interval [0, n-1]\n  while (i <= j) {\n    int m = i + (j - i) ~/ 2; // Calculate the midpoint index m\n    if (nums[m] < target) {\n      i = m + 1; // target is in the interval [m+1, j]\n    } else if (nums[m] > target) {\n      j = m - 1; // target is in the interval [i, m-1]\n    } else {\n      return m; // Found target, return insertion point m\n    }\n  }\n  // Target not found, return insertion point i\n  return i;\n}\n
      binary_search_insertion.rs
      /* Binary search for insertion point (no duplicate elements) */\nfn binary_search_insertion_simple(nums: &[i32], target: i32) -> i32 {\n    let (mut i, mut j) = (0, nums.len() as i32 - 1); // Initialize closed interval [0, n-1]\n    while i <= j {\n        let m = i + (j - i) / 2; // Calculate the midpoint index m\n        if nums[m as usize] < target {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if nums[m as usize] > target {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            return m;\n        }\n    }\n    // Target not found, return insertion point i\n    i\n}\n
      binary_search_insertion.c
      /* Binary search for insertion point (no duplicate elements) */\nint binarySearchInsertionSimple(int *nums, int numSize, int target) {\n    int i = 0, j = numSize - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            return m; // Found target, return insertion point m\n        }\n    }\n    // Target not found, return insertion point i\n    return i;\n}\n
      binary_search_insertion.kt
      /* Binary search for insertion point (no duplicate elements) */\nfun binarySearchInsertionSimple(nums: IntArray, target: Int): Int {\n    var i = 0\n    var j = nums.size - 1 // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        val m = i + (j - i) / 2 // Calculate the midpoint index m\n        if (nums[m] < target) {\n            i = m + 1 // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1 // target is in the interval [i, m-1]\n        } else {\n            return m // Found target, return insertion point m\n        }\n    }\n    // Target not found, return insertion point i\n    return i\n}\n
      binary_search_insertion.rb
      ### Binary search insertion point (no duplicates) ###\ndef binary_search_insertion_simple(nums, target)\n  # Initialize closed interval [0, n-1]\n  i, j = 0, nums.length - 1\n\n  while i <= j\n    # Calculate the midpoint index m\n    m = (i + j) / 2\n\n    if nums[m] < target\n      i = m + 1 # target is in the interval [m+1, j]\n    elsif nums[m] > target\n      j = m - 1 # target is in the interval [i, m-1]\n    else\n      return m  # Found target, return insertion point m\n    end\n  end\n\n  i # Target not found, return insertion point i\nend\n
      "},{"location":"chapter_searching/binary_search_insertion/#1022-case-with-duplicate-elements","title":"10.2.2 \u00a0 Case with Duplicate Elements","text":"

      Question

      Based on the previous problem, assume the array may contain duplicate elements, with everything else remaining the same.

      Suppose there are multiple target elements in the array. Ordinary binary search can only return the index of one target, and cannot determine how many target elements are to the left and right of that element.

      The problem requires inserting the target element at the leftmost position, so we need to find the index of the leftmost target in the array. Initially, consider implementing this through the steps shown in Figure 10-5:

      1. Perform binary search to obtain the index of any target, denoted as \\(k\\).
      2. Starting from index \\(k\\), perform linear traversal to the left, and return when the leftmost target is found.

      Figure 10-5 \u00a0 Linear search for insertion point of duplicate elements

      Although this method works, it includes linear search, resulting in a time complexity of \\(O(n)\\). When the array contains many duplicate target elements, this method is very inefficient.

      Now consider extending the binary search code. As shown in Figure 10-6, the overall process remains unchanged: calculate the midpoint index \\(m\\) in each round, then compare target with nums[m], divided into the following cases:

      • When nums[m] < target or nums[m] > target, it means target has not been found yet, so use the ordinary binary search interval narrowing operation to make pointers \\(i\\) and \\(j\\) approach target.
      • When nums[m] == target, it means elements less than target are in the interval \\([i, m - 1]\\), so use \\(j = m - 1\\) to narrow the interval, thereby making pointer \\(j\\) approach elements less than target.

      After the loop completes, \\(i\\) points to the leftmost target, and \\(j\\) points to the first element less than target, so index \\(i\\) is the insertion point.

      <1><2><3><4><5><6><7><8>

      Figure 10-6 \u00a0 Steps for binary search insertion point of duplicate elements

      Observe the following code: the operations for branches nums[m] > target and nums[m] == target are the same, so the two can be merged.

      Even so, we can still keep the conditional branches expanded, as the logic is clearer and more readable.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_insertion.py
      def binary_search_insertion(nums: list[int], target: int) -> int:\n    \"\"\"Binary search for insertion point (with duplicate elements)\"\"\"\n    i, j = 0, len(nums) - 1  # Initialize closed interval [0, n-1]\n    while i <= j:\n        m = (i + j) // 2  # Calculate midpoint index m\n        if nums[m] < target:\n            i = m + 1  # target is in the interval [m+1, j]\n        elif nums[m] > target:\n            j = m - 1  # target is in the interval [i, m-1]\n        else:\n            j = m - 1  # The first element less than target is in the interval [i, m-1]\n    # Return insertion point i\n    return i\n
      binary_search_insertion.cpp
      /* Binary search for insertion point (with duplicate elements) */\nint binarySearchInsertion(vector<int> &nums, int target) {\n    int i = 0, j = nums.size() - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            j = m - 1; // The first element less than target is in the interval [i, m-1]\n        }\n    }\n    // Return insertion point i\n    return i;\n}\n
      binary_search_insertion.java
      /* Binary search for insertion point (with duplicate elements) */\nint binarySearchInsertion(int[] nums, int target) {\n    int i = 0, j = nums.length - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            j = m - 1; // The first element less than target is in the interval [i, m-1]\n        }\n    }\n    // Return insertion point i\n    return i;\n}\n
      binary_search_insertion.cs
      /* Binary search for insertion point (with duplicate elements) */\nint BinarySearchInsertion(int[] nums, int target) {\n    int i = 0, j = nums.Length - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            j = m - 1; // The first element less than target is in the interval [i, m-1]\n        }\n    }\n    // Return insertion point i\n    return i;\n}\n
      binary_search_insertion.go
      /* Binary search for insertion point (with duplicate elements) */\nfunc binarySearchInsertion(nums []int, target int) int {\n    // Initialize closed interval [0, n-1]\n    i, j := 0, len(nums)-1\n    for i <= j {\n        // Calculate the midpoint index m\n        m := i + (j-i)/2\n        if nums[m] < target {\n            // target is in the interval [m+1, j]\n            i = m + 1\n        } else if nums[m] > target {\n            // target is in the interval [i, m-1]\n            j = m - 1\n        } else {\n            // The first element less than target is in the interval [i, m-1]\n            j = m - 1\n        }\n    }\n    // Return insertion point i\n    return i\n}\n
      binary_search_insertion.swift
      /* Binary search for insertion point (with duplicate elements) */\nfunc binarySearchInsertion(nums: [Int], target: Int) -> Int {\n    // Initialize closed interval [0, n-1]\n    var i = nums.startIndex\n    var j = nums.endIndex - 1\n    while i <= j {\n        let m = i + (j - i) / 2 // Calculate the midpoint index m\n        if nums[m] < target {\n            i = m + 1 // target is in the interval [m+1, j]\n        } else if nums[m] > target {\n            j = m - 1 // target is in the interval [i, m-1]\n        } else {\n            j = m - 1 // The first element less than target is in the interval [i, m-1]\n        }\n    }\n    // Return insertion point i\n    return i\n}\n
      binary_search_insertion.js
      /* Binary search for insertion point (with duplicate elements) */\nfunction binarySearchInsertion(nums, target) {\n    let i = 0,\n        j = nums.length - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        const m = Math.floor(i + (j - i) / 2); // Calculate midpoint index m, use Math.floor() to round down\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            j = m - 1; // The first element less than target is in the interval [i, m-1]\n        }\n    }\n    // Return insertion point i\n    return i;\n}\n
      binary_search_insertion.ts
      /* Binary search for insertion point (with duplicate elements) */\nfunction binarySearchInsertion(nums: Array<number>, target: number): number {\n    let i = 0,\n        j = nums.length - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        const m = Math.floor(i + (j - i) / 2); // Calculate midpoint index m, use Math.floor() to round down\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            j = m - 1; // The first element less than target is in the interval [i, m-1]\n        }\n    }\n    // Return insertion point i\n    return i;\n}\n
      binary_search_insertion.dart
      /* Binary search for insertion point (with duplicate elements) */\nint binarySearchInsertion(List<int> nums, int target) {\n  int i = 0, j = nums.length - 1; // Initialize closed interval [0, n-1]\n  while (i <= j) {\n    int m = i + (j - i) ~/ 2; // Calculate the midpoint index m\n    if (nums[m] < target) {\n      i = m + 1; // target is in the interval [m+1, j]\n    } else if (nums[m] > target) {\n      j = m - 1; // target is in the interval [i, m-1]\n    } else {\n      j = m - 1; // The first element less than target is in the interval [i, m-1]\n    }\n  }\n  // Return insertion point i\n  return i;\n}\n
      binary_search_insertion.rs
      /* Binary search for insertion point (with duplicate elements) */\npub fn binary_search_insertion(nums: &[i32], target: i32) -> i32 {\n    let (mut i, mut j) = (0, nums.len() as i32 - 1); // Initialize closed interval [0, n-1]\n    while i <= j {\n        let m = i + (j - i) / 2; // Calculate the midpoint index m\n        if nums[m as usize] < target {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if nums[m as usize] > target {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            j = m - 1; // The first element less than target is in the interval [i, m-1]\n        }\n    }\n    // Return insertion point i\n    i\n}\n
      binary_search_insertion.c
      /* Binary search for insertion point (with duplicate elements) */\nint binarySearchInsertion(int *nums, int numSize, int target) {\n    int i = 0, j = numSize - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            j = m - 1; // The first element less than target is in the interval [i, m-1]\n        }\n    }\n    // Return insertion point i\n    return i;\n}\n
      binary_search_insertion.kt
      /* Binary search for insertion point (with duplicate elements) */\nfun binarySearchInsertion(nums: IntArray, target: Int): Int {\n    var i = 0\n    var j = nums.size - 1 // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        val m = i + (j - i) / 2 // Calculate the midpoint index m\n        if (nums[m] < target) {\n            i = m + 1 // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1 // target is in the interval [i, m-1]\n        } else {\n            j = m - 1 // The first element less than target is in the interval [i, m-1]\n        }\n    }\n    // Return insertion point i\n    return i\n}\n
      binary_search_insertion.rb
      ### Binary search insertion point (with duplicates) ###\ndef binary_search_insertion(nums, target)\n  # Initialize closed interval [0, n-1]\n  i, j = 0, nums.length - 1\n\n  while i <= j\n    # Calculate the midpoint index m\n    m = (i + j) / 2\n\n    if nums[m] < target\n      i = m + 1 # target is in the interval [m+1, j]\n    elsif nums[m] > target\n      j = m - 1 # target is in the interval [i, m-1]\n    else\n      j = m - 1 # The first element less than target is in the interval [i, m-1]\n    end\n  end\n\n  i # Return insertion point i\nend\n

      Tip

      The code in this section all uses the \"closed interval\" approach. Interested readers can implement the \"left-closed right-open\" approach themselves.

      Overall, binary search is simply about setting search targets for pointers \\(i\\) and \\(j\\) separately. The target could be a specific element (such as target) or a range of elements (such as elements less than target).

      Through continuous binary iterations, both pointers \\(i\\) and \\(j\\) gradually approach their preset targets. Ultimately, they either successfully find the answer or stop after crossing the boundaries.

      "},{"location":"chapter_searching/replace_linear_by_hashing/","title":"10.4 \u00a0 Hash Optimization Strategy","text":"

      In algorithm problems, we often reduce the time complexity of algorithms by replacing linear search with hash-based search. Let's use an algorithm problem to deepen our understanding.

      Question

      Given an integer array nums and a target element target, search for two elements in the array whose \"sum\" equals target, and return their array indices. Any solution will do.

      "},{"location":"chapter_searching/replace_linear_by_hashing/#1041-linear-search-trading-time-for-space","title":"10.4.1 \u00a0 Linear Search: Trading Time for Space","text":"

      Consider directly traversing all possible combinations. As shown in Figure 10-9, we open a two-layer loop and judge in each round whether the sum of two integers equals target. If so, return their indices.

      Figure 10-9 \u00a0 Linear search solution for two sum

      The code is shown below:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby two_sum.py
      def two_sum_brute_force(nums: list[int], target: int) -> list[int]:\n    \"\"\"Method 1: Brute force enumeration\"\"\"\n    # Two nested loops, time complexity is O(n^2)\n    for i in range(len(nums) - 1):\n        for j in range(i + 1, len(nums)):\n            if nums[i] + nums[j] == target:\n                return [i, j]\n    return []\n
      two_sum.cpp
      /* Method 1: Brute force enumeration */\nvector<int> twoSumBruteForce(vector<int> &nums, int target) {\n    int size = nums.size();\n    // Two nested loops, time complexity is O(n^2)\n    for (int i = 0; i < size - 1; i++) {\n        for (int j = i + 1; j < size; j++) {\n            if (nums[i] + nums[j] == target)\n                return {i, j};\n        }\n    }\n    return {};\n}\n
      two_sum.java
      /* Method 1: Brute force enumeration */\nint[] twoSumBruteForce(int[] nums, int target) {\n    int size = nums.length;\n    // Two nested loops, time complexity is O(n^2)\n    for (int i = 0; i < size - 1; i++) {\n        for (int j = i + 1; j < size; j++) {\n            if (nums[i] + nums[j] == target)\n                return new int[] { i, j };\n        }\n    }\n    return new int[0];\n}\n
      two_sum.cs
      /* Method 1: Brute force enumeration */\nint[] TwoSumBruteForce(int[] nums, int target) {\n    int size = nums.Length;\n    // Two nested loops, time complexity is O(n^2)\n    for (int i = 0; i < size - 1; i++) {\n        for (int j = i + 1; j < size; j++) {\n            if (nums[i] + nums[j] == target)\n                return [i, j];\n        }\n    }\n    return [];\n}\n
      two_sum.go
      /* Method 1: Brute force enumeration */\nfunc twoSumBruteForce(nums []int, target int) []int {\n    size := len(nums)\n    // Two nested loops, time complexity is O(n^2)\n    for i := 0; i < size-1; i++ {\n        for j := i + 1; j < size; j++ {\n            if nums[i]+nums[j] == target {\n                return []int{i, j}\n            }\n        }\n    }\n    return nil\n}\n
      two_sum.swift
      /* Method 1: Brute force enumeration */\nfunc twoSumBruteForce(nums: [Int], target: Int) -> [Int] {\n    // Two nested loops, time complexity is O(n^2)\n    for i in nums.indices.dropLast() {\n        for j in nums.indices.dropFirst(i + 1) {\n            if nums[i] + nums[j] == target {\n                return [i, j]\n            }\n        }\n    }\n    return [0]\n}\n
      two_sum.js
      /* Method 1: Brute force enumeration */\nfunction twoSumBruteForce(nums, target) {\n    const n = nums.length;\n    // Two nested loops, time complexity is O(n^2)\n    for (let i = 0; i < n; i++) {\n        for (let j = i + 1; j < n; j++) {\n            if (nums[i] + nums[j] === target) {\n                return [i, j];\n            }\n        }\n    }\n    return [];\n}\n
      two_sum.ts
      /* Method 1: Brute force enumeration */\nfunction twoSumBruteForce(nums: number[], target: number): number[] {\n    const n = nums.length;\n    // Two nested loops, time complexity is O(n^2)\n    for (let i = 0; i < n; i++) {\n        for (let j = i + 1; j < n; j++) {\n            if (nums[i] + nums[j] === target) {\n                return [i, j];\n            }\n        }\n    }\n    return [];\n}\n
      two_sum.dart
      /* Method 1: Brute force enumeration */\nList<int> twoSumBruteForce(List<int> nums, int target) {\n  int size = nums.length;\n  // Two nested loops, time complexity is O(n^2)\n  for (var i = 0; i < size - 1; i++) {\n    for (var j = i + 1; j < size; j++) {\n      if (nums[i] + nums[j] == target) return [i, j];\n    }\n  }\n  return [0];\n}\n
      two_sum.rs
      /* Method 1: Brute force enumeration */\npub fn two_sum_brute_force(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {\n    let size = nums.len();\n    // Two nested loops, time complexity is O(n^2)\n    for i in 0..size - 1 {\n        for j in i + 1..size {\n            if nums[i] + nums[j] == target {\n                return Some(vec![i as i32, j as i32]);\n            }\n        }\n    }\n    None\n}\n
      two_sum.c
      /* Method 1: Brute force enumeration */\nint *twoSumBruteForce(int *nums, int numsSize, int target, int *returnSize) {\n    for (int i = 0; i < numsSize; ++i) {\n        for (int j = i + 1; j < numsSize; ++j) {\n            if (nums[i] + nums[j] == target) {\n                int *res = malloc(sizeof(int) * 2);\n                res[0] = i, res[1] = j;\n                *returnSize = 2;\n                return res;\n            }\n        }\n    }\n    *returnSize = 0;\n    return NULL;\n}\n
      two_sum.kt
      /* Method 1: Brute force enumeration */\nfun twoSumBruteForce(nums: IntArray, target: Int): IntArray {\n    val size = nums.size\n    // Two nested loops, time complexity is O(n^2)\n    for (i in 0..<size - 1) {\n        for (j in i + 1..<size) {\n            if (nums[i] + nums[j] == target) return intArrayOf(i, j)\n        }\n    }\n    return IntArray(0)\n}\n
      two_sum.rb
      ### Method 1: Brute force enumeration ###\ndef two_sum_brute_force(nums, target)\n  # Two nested loops, time complexity is O(n^2)\n  for i in 0...(nums.length - 1)\n    for j in (i + 1)...nums.length\n      return [i, j] if nums[i] + nums[j] == target\n    end\n  end\n\n  []\nend\n

      This method has a time complexity of \\(O(n^2)\\) and a space complexity of \\(O(1)\\), which is very time-consuming with large data volumes.

      "},{"location":"chapter_searching/replace_linear_by_hashing/#1042-hash-based-search-trading-space-for-time","title":"10.4.2 \u00a0 Hash-Based Search: Trading Space for Time","text":"

      Consider using a hash table where key-value pairs are array elements and element indices respectively. Loop through the array, performing the steps shown in Figure 10-10 in each round:

      1. Check if the number target - nums[i] is in the hash table. If so, directly return the indices of these two elements.
      2. Add the key-value pair nums[i] and index i to the hash table.
      <1><2><3>

      Figure 10-10 \u00a0 Hash table solution for two sum

      The implementation code is shown below, requiring only a single loop:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby two_sum.py
      def two_sum_hash_table(nums: list[int], target: int) -> list[int]:\n    \"\"\"Method 2: Auxiliary hash table\"\"\"\n    # Auxiliary hash table, space complexity is O(n)\n    dic = {}\n    # Single loop, time complexity is O(n)\n    for i in range(len(nums)):\n        if target - nums[i] in dic:\n            return [dic[target - nums[i]], i]\n        dic[nums[i]] = i\n    return []\n
      two_sum.cpp
      /* Method 2: Auxiliary hash table */\nvector<int> twoSumHashTable(vector<int> &nums, int target) {\n    int size = nums.size();\n    // Auxiliary hash table, space complexity is O(n)\n    unordered_map<int, int> dic;\n    // Single loop, time complexity is O(n)\n    for (int i = 0; i < size; i++) {\n        if (dic.find(target - nums[i]) != dic.end()) {\n            return {dic[target - nums[i]], i};\n        }\n        dic.emplace(nums[i], i);\n    }\n    return {};\n}\n
      two_sum.java
      /* Method 2: Auxiliary hash table */\nint[] twoSumHashTable(int[] nums, int target) {\n    int size = nums.length;\n    // Auxiliary hash table, space complexity is O(n)\n    Map<Integer, Integer> dic = new HashMap<>();\n    // Single loop, time complexity is O(n)\n    for (int i = 0; i < size; i++) {\n        if (dic.containsKey(target - nums[i])) {\n            return new int[] { dic.get(target - nums[i]), i };\n        }\n        dic.put(nums[i], i);\n    }\n    return new int[0];\n}\n
      two_sum.cs
      /* Method 2: Auxiliary hash table */\nint[] TwoSumHashTable(int[] nums, int target) {\n    int size = nums.Length;\n    // Auxiliary hash table, space complexity is O(n)\n    Dictionary<int, int> dic = [];\n    // Single loop, time complexity is O(n)\n    for (int i = 0; i < size; i++) {\n        if (dic.ContainsKey(target - nums[i])) {\n            return [dic[target - nums[i]], i];\n        }\n        dic.Add(nums[i], i);\n    }\n    return [];\n}\n
      two_sum.go
      /* Method 2: Auxiliary hash table */\nfunc twoSumHashTable(nums []int, target int) []int {\n    // Auxiliary hash table, space complexity is O(n)\n    hashTable := map[int]int{}\n    // Single loop, time complexity is O(n)\n    for idx, val := range nums {\n        if preIdx, ok := hashTable[target-val]; ok {\n            return []int{preIdx, idx}\n        }\n        hashTable[val] = idx\n    }\n    return nil\n}\n
      two_sum.swift
      /* Method 2: Auxiliary hash table */\nfunc twoSumHashTable(nums: [Int], target: Int) -> [Int] {\n    // Auxiliary hash table, space complexity is O(n)\n    var dic: [Int: Int] = [:]\n    // Single loop, time complexity is O(n)\n    for i in nums.indices {\n        if let j = dic[target - nums[i]] {\n            return [j, i]\n        }\n        dic[nums[i]] = i\n    }\n    return [0]\n}\n
      two_sum.js
      /* Method 2: Auxiliary hash table */\nfunction twoSumHashTable(nums, target) {\n    // Auxiliary hash table, space complexity is O(n)\n    let m = {};\n    // Single loop, time complexity is O(n)\n    for (let i = 0; i < nums.length; i++) {\n        if (m[target - nums[i]] !== undefined) {\n            return [m[target - nums[i]], i];\n        } else {\n            m[nums[i]] = i;\n        }\n    }\n    return [];\n}\n
      two_sum.ts
      /* Method 2: Auxiliary hash table */\nfunction twoSumHashTable(nums: number[], target: number): number[] {\n    // Auxiliary hash table, space complexity is O(n)\n    let m: Map<number, number> = new Map();\n    // Single loop, time complexity is O(n)\n    for (let i = 0; i < nums.length; i++) {\n        let index = m.get(target - nums[i]);\n        if (index !== undefined) {\n            return [index, i];\n        } else {\n            m.set(nums[i], i);\n        }\n    }\n    return [];\n}\n
      two_sum.dart
      /* Method 2: Auxiliary hash table */\nList<int> twoSumHashTable(List<int> nums, int target) {\n  int size = nums.length;\n  // Auxiliary hash table, space complexity is O(n)\n  Map<int, int> dic = HashMap();\n  // Single loop, time complexity is O(n)\n  for (var i = 0; i < size; i++) {\n    if (dic.containsKey(target - nums[i])) {\n      return [dic[target - nums[i]]!, i];\n    }\n    dic.putIfAbsent(nums[i], () => i);\n  }\n  return [0];\n}\n
      two_sum.rs
      /* Method 2: Auxiliary hash table */\npub fn two_sum_hash_table(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {\n    // Auxiliary hash table, space complexity is O(n)\n    let mut dic = HashMap::new();\n    // Single loop, time complexity is O(n)\n    for (i, num) in nums.iter().enumerate() {\n        match dic.get(&(target - num)) {\n            Some(v) => return Some(vec![*v as i32, i as i32]),\n            None => dic.insert(num, i as i32),\n        };\n    }\n    None\n}\n
      two_sum.c
      /* Hash table */\ntypedef struct {\n    int key;\n    int val;\n    UT_hash_handle hh; // Implemented using uthash.h\n} HashTable;\n\n/* Hash table lookup */\nHashTable *find(HashTable *h, int key) {\n    HashTable *tmp;\n    HASH_FIND_INT(h, &key, tmp);\n    return tmp;\n}\n\n/* Hash table element insertion */\nvoid insert(HashTable **h, int key, int val) {\n    HashTable *t = find(*h, key);\n    if (t == NULL) {\n        HashTable *tmp = malloc(sizeof(HashTable));\n        tmp->key = key, tmp->val = val;\n        HASH_ADD_INT(*h, key, tmp);\n    } else {\n        t->val = val;\n    }\n}\n\n/* Method 2: Auxiliary hash table */\nint *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {\n    HashTable *hashtable = NULL;\n    for (int i = 0; i < numsSize; i++) {\n        HashTable *t = find(hashtable, target - nums[i]);\n        if (t != NULL) {\n            int *res = malloc(sizeof(int) * 2);\n            res[0] = t->val, res[1] = i;\n            *returnSize = 2;\n            return res;\n        }\n        insert(&hashtable, nums[i], i);\n    }\n    *returnSize = 0;\n    return NULL;\n}\n
      two_sum.kt
      /* Method 2: Auxiliary hash table */\nfun twoSumHashTable(nums: IntArray, target: Int): IntArray {\n    val size = nums.size\n    // Auxiliary hash table, space complexity is O(n)\n    val dic = HashMap<Int, Int>()\n    // Single loop, time complexity is O(n)\n    for (i in 0..<size) {\n        if (dic.containsKey(target - nums[i])) {\n            return intArrayOf(dic[target - nums[i]]!!, i)\n        }\n        dic[nums[i]] = i\n    }\n    return IntArray(0)\n}\n
      two_sum.rb
      ### Method 2: Auxiliary hash table ###\ndef two_sum_hash_table(nums, target)\n  # Auxiliary hash table, space complexity is O(n)\n  dic = {}\n  # Single loop, time complexity is O(n)\n  for i in 0...nums.length\n    return [dic[target - nums[i]], i] if dic.has_key?(target - nums[i])\n\n    dic[nums[i]] = i\n  end\n\n  []\nend\n

      This method reduces the time complexity from \\(O(n^2)\\) to \\(O(n)\\) through hash-based search, greatly improving runtime efficiency.

      Since an additional hash table needs to be maintained, the space complexity is \\(O(n)\\). Nevertheless, this method achieves a more balanced overall time-space efficiency, making it the optimal solution for this problem.

      "},{"location":"chapter_searching/searching_algorithm_revisited/","title":"10.5 \u00a0 Searching Algorithms Revisited","text":"

      Searching algorithms are used to search for one or a group of elements that meet specific conditions in data structures (such as arrays, linked lists, trees, or graphs).

      Searching algorithms can be divided into the following two categories based on their implementation approach:

      • Locating target elements by traversing the data structure, such as traversing arrays, linked lists, trees, and graphs.
      • Achieving efficient element search by utilizing data organization structure or prior information contained in the data, such as binary search, hash-based search, and binary search tree search.

      It's not hard to see that these topics have all been covered in previous chapters, so searching algorithms are not unfamiliar to us. In this section, we will approach from a more systematic perspective and re-examine searching algorithms.

      "},{"location":"chapter_searching/searching_algorithm_revisited/#1051-brute-force-search","title":"10.5.1 \u00a0 Brute-Force Search","text":"

      Brute-force search locates target elements by traversing each element of the data structure.

      • \"Linear search\" is applicable to linear data structures such as arrays and linked lists. It starts from one end of the data structure and accesses elements one by one until the target element is found or the other end is reached without finding the target element.
      • \"Breadth-first search\" and \"depth-first search\" are two traversal strategies for graphs and trees. Breadth-first search starts from the initial node and searches layer by layer, visiting nodes from near to far. Depth-first search starts from the initial node, follows a path to the end, then backtracks and tries other paths until the entire data structure is traversed.

      The advantage of brute-force search is that it is simple and has good generality, requiring no data preprocessing or additional data structures.

      However, the time complexity of such algorithms is \\(O(n)\\), where \\(n\\) is the number of elements, so performance is poor when dealing with large amounts of data.

      "},{"location":"chapter_searching/searching_algorithm_revisited/#1052-adaptive-search","title":"10.5.2 \u00a0 Adaptive Search","text":"

      Adaptive search utilizes the unique properties of data (such as orderliness) to optimize the search process, thereby locating target elements more efficiently.

      • \"Binary search\" uses the orderliness of data to achieve efficient searching, applicable only to arrays.
      • \"Hash-based search\" uses hash tables to establish key-value pair mappings between search data and target data, thereby achieving query operations.
      • \"Tree search\" in specific tree structures (such as binary search trees), quickly eliminates nodes based on comparing node values to locate target elements.

      The advantage of such algorithms is high efficiency, with time complexity reaching \\(O(\\log n)\\) or even \\(O(1)\\).

      However, using these algorithms often requires data preprocessing. For example, binary search requires pre-sorting the array, while hash-based search and tree search both require additional data structures, and maintaining these data structures also requires extra time and space overhead.

      Tip

      Adaptive search algorithms are often called lookup algorithms, mainly used to quickly retrieve target elements in specific data structures.

      "},{"location":"chapter_searching/searching_algorithm_revisited/#1053-search-method-selection","title":"10.5.3 \u00a0 Search Method Selection","text":"

      Given a dataset of size \\(n\\), we can use linear search, binary search, tree search, hash-based search, and other methods to search for the target element. The working principles of each method are shown in Figure 10-11.

      Figure 10-11 \u00a0 Multiple search strategies

      The operational efficiency and characteristics of the above methods are as follows:

      Table 10-1 \u00a0 Comparison of search algorithm efficiency

      Linear search Binary search Tree search Hash-based search Search element \\(O(n)\\) \\(O(\\log n)\\) \\(O(\\log n)\\) \\(O(1)\\) Insert element \\(O(1)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) Delete element \\(O(n)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) Extra space \\(O(1)\\) \\(O(1)\\) \\(O(n)\\) \\(O(n)\\) Data preprocessing / Sorting \\(O(n \\log n)\\) Tree building \\(O(n \\log n)\\) Hash table building \\(O(n)\\) Data ordered Unordered Ordered Ordered Unordered

      The choice of search algorithm also depends on data volume, search performance requirements, data query and update frequency, etc.

      Linear search

      • Good generality, requiring no data preprocessing operations. If we only need to query the data once, the data preprocessing time for the other three methods would be longer than linear search.
      • Suitable for small data volumes, where time complexity has less impact on efficiency.
      • Suitable for scenarios with high data update frequency, as this method does not require any additional data maintenance.

      Binary search

      • Suitable for large data volumes with stable efficiency performance, worst-case time complexity of \\(O(\\log n)\\).
      • Data volume cannot be too large, as storing arrays requires contiguous memory space.
      • Not suitable for scenarios with frequent data insertion and deletion, as maintaining a sorted array has high overhead.

      Hash-based search

      • Suitable for scenarios with high query performance requirements, with an average time complexity of \\(O(1)\\).
      • Not suitable for scenarios requiring ordered data or range searches, as hash tables cannot maintain data orderliness.
      • High dependence on hash functions and hash collision handling strategies, with significant risk of performance degradation.
      • Not suitable for excessively large data volumes, as hash tables require extra space to minimize collisions and thus provide good query performance.

      Tree search

      • Suitable for massive data, as tree nodes are stored dispersedly in memory.
      • Suitable for scenarios requiring maintained ordered data or range searches.
      • During continuous node insertion and deletion, binary search trees may become skewed, degrading time complexity to \\(O(n)\\).
      • If using AVL trees or red-black trees, all operations can run stably at \\(O(\\log n)\\) efficiency, but operations to maintain tree balance add extra overhead.
      "},{"location":"chapter_searching/summary/","title":"10.6 \u00a0 Summary","text":""},{"location":"chapter_searching/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • Binary search relies on data orderliness and progressively reduces the search interval by half through loops. It requires input data to be sorted and is only applicable to arrays or data structures based on array implementations.
      • Brute-force search locates data by traversing the data structure. Linear search is applicable to arrays and linked lists, while breadth-first search and depth-first search are applicable to graphs and trees. Such algorithms have good generality and require no data preprocessing, but have a relatively high time complexity of \\(O(n)\\).
      • Hash-based search, tree search, and binary search are efficient search methods that can quickly locate target elements in specific data structures. Such algorithms are highly efficient with time complexity reaching \\(O(\\log n)\\) or even \\(O(1)\\), but typically require additional data structures.
      • In practice, we need to analyze factors such as data scale, search performance requirements, and data query and update frequency to choose the appropriate search method.
      • Linear search is suitable for small-scale or frequently updated data; binary search is suitable for large-scale, sorted data; hash-based search is suitable for data with high query efficiency requirements and no need for range queries; tree search is suitable for large-scale dynamic data that needs to maintain order and support range queries.
      • Replacing linear search with hash-based search is a commonly used strategy to optimize runtime, reducing time complexity from \\(O(n)\\) to \\(O(1)\\).
      "},{"location":"chapter_sorting/","title":"Chapter 11. \u00a0 Sorting","text":"

      Abstract

      Sorting is like a magic key that transforms chaos into order, enabling us to understand and process data more efficiently.

      Whether it's simple ascending order or complex categorized arrangements, sorting demonstrates the harmonious beauty of data.

      "},{"location":"chapter_sorting/#chapter-contents","title":"Chapter contents","text":"
      • 11.1 \u00a0 Sorting Algorithms
      • 11.2 \u00a0 Selection Sort
      • 11.3 \u00a0 Bubble Sort
      • 11.4 \u00a0 Insertion Sort
      • 11.5 \u00a0 Quick Sort
      • 11.6 \u00a0 Merge Sort
      • 11.7 \u00a0 Heap Sort
      • 11.8 \u00a0 Bucket Sort
      • 11.9 \u00a0 Counting Sort
      • 11.10 \u00a0 Radix Sort
      • 11.11 \u00a0 Summary
      "},{"location":"chapter_sorting/bubble_sort/","title":"11.3 \u00a0 Bubble Sort","text":"

      Bubble sort (bubble sort) achieves sorting by continuously comparing and swapping adjacent elements. This process is like bubbles rising from the bottom to the top, hence the name bubble sort.

      As shown in Figure 11-4, the bubbling process can be simulated using element swap operations: starting from the leftmost end of the array and traversing to the right, compare the size of adjacent elements, and if \"left element > right element\", swap them. After completing the traversal, the largest element will be moved to the rightmost end of the array.

      <1><2><3><4><5><6><7>

      Figure 11-4 \u00a0 Simulating bubble using element swap operation

      "},{"location":"chapter_sorting/bubble_sort/#1131-algorithm-flow","title":"11.3.1 \u00a0 Algorithm Flow","text":"

      Assume the array has length \\(n\\). The steps of bubble sort are shown in Figure 11-5.

      1. First, perform \"bubbling\" on \\(n\\) elements, swapping the largest element of the array to its correct position.
      2. Next, perform \"bubbling\" on the remaining \\(n - 1\\) elements, swapping the second largest element to its correct position.
      3. And so on. After \\(n - 1\\) rounds of \"bubbling\", the largest \\(n - 1\\) elements have all been swapped to their correct positions.
      4. The only remaining element must be the smallest element, requiring no sorting, so the array sorting is complete.

      Figure 11-5 \u00a0 Bubble sort flow

      Example code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby bubble_sort.py
      def bubble_sort(nums: list[int]):\n    \"\"\"Bubble sort\"\"\"\n    n = len(nums)\n    # Outer loop: unsorted interval is [0, i]\n    for i in range(n - 1, 0, -1):\n        # Inner loop: swap the largest element in the unsorted interval [0, i] to the rightmost end of the interval\n        for j in range(i):\n            if nums[j] > nums[j + 1]:\n                # Swap nums[j] and nums[j + 1]\n                nums[j], nums[j + 1] = nums[j + 1], nums[j]\n
      bubble_sort.cpp
      /* Bubble sort */\nvoid bubbleSort(vector<int> &nums) {\n    // Outer loop: unsorted range is [0, i]\n    for (int i = nums.size() - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                // Using std::swap() function here\n                swap(nums[j], nums[j + 1]);\n            }\n        }\n    }\n}\n
      bubble_sort.java
      /* Bubble sort */\nvoid bubbleSort(int[] nums) {\n    // Outer loop: unsorted range is [0, i]\n    for (int i = nums.length - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n            }\n        }\n    }\n}\n
      bubble_sort.cs
      /* Bubble sort */\nvoid BubbleSort(int[] nums) {\n    // Outer loop: unsorted range is [0, i]\n    for (int i = nums.Length - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n            }\n        }\n    }\n}\n
      bubble_sort.go
      /* Bubble sort */\nfunc bubbleSort(nums []int) {\n    // Outer loop: unsorted range is [0, i]\n    for i := len(nums) - 1; i > 0; i-- {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for j := 0; j < i; j++ {\n            if nums[j] > nums[j+1] {\n                // Swap nums[j] and nums[j + 1]\n                nums[j], nums[j+1] = nums[j+1], nums[j]\n            }\n        }\n    }\n}\n
      bubble_sort.swift
      /* Bubble sort */\nfunc bubbleSort(nums: inout [Int]) {\n    // Outer loop: unsorted range is [0, i]\n    for i in nums.indices.dropFirst().reversed() {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for j in 0 ..< i {\n            if nums[j] > nums[j + 1] {\n                // Swap nums[j] and nums[j + 1]\n                nums.swapAt(j, j + 1)\n            }\n        }\n    }\n}\n
      bubble_sort.js
      /* Bubble sort */\nfunction bubbleSort(nums) {\n    // Outer loop: unsorted range is [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n            }\n        }\n    }\n}\n
      bubble_sort.ts
      /* Bubble sort */\nfunction bubbleSort(nums: number[]): void {\n    // Outer loop: unsorted range is [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n            }\n        }\n    }\n}\n
      bubble_sort.dart
      /* Bubble sort */\nvoid bubbleSort(List<int> nums) {\n  // Outer loop: unsorted range is [0, i]\n  for (int i = nums.length - 1; i > 0; i--) {\n    // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n    for (int j = 0; j < i; j++) {\n      if (nums[j] > nums[j + 1]) {\n        // Swap nums[j] and nums[j + 1]\n        int tmp = nums[j];\n        nums[j] = nums[j + 1];\n        nums[j + 1] = tmp;\n      }\n    }\n  }\n}\n
      bubble_sort.rs
      /* Bubble sort */\nfn bubble_sort(nums: &mut [i32]) {\n    // Outer loop: unsorted range is [0, i]\n    for i in (1..nums.len()).rev() {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for j in 0..i {\n            if nums[j] > nums[j + 1] {\n                // Swap nums[j] and nums[j + 1]\n                nums.swap(j, j + 1);\n            }\n        }\n    }\n}\n
      bubble_sort.c
      /* Bubble sort */\nvoid bubbleSort(int nums[], int size) {\n    // Outer loop: unsorted range is [0, i]\n    for (int i = size - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                int temp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = temp;\n            }\n        }\n    }\n}\n
      bubble_sort.kt
      /* Bubble sort */\nfun bubbleSort(nums: IntArray) {\n    // Outer loop: unsorted range is [0, i]\n    for (i in nums.size - 1 downTo 1) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (j in 0..<i) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                val temp = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = temp\n            }\n        }\n    }\n}\n
      bubble_sort.rb
      ### Bubble sort ###\ndef bubble_sort(nums)\n  n = nums.length\n  # Outer loop: unsorted range is [0, i]\n  for i in (n - 1).downto(1)\n    # Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n    for j in 0...i\n      if nums[j] > nums[j + 1]\n        # Swap nums[j] and nums[j + 1]\n        nums[j], nums[j + 1] = nums[j + 1], nums[j]\n      end\n    end\n  end\nend\n
      "},{"location":"chapter_sorting/bubble_sort/#1132-efficiency-optimization","title":"11.3.2 \u00a0 Efficiency Optimization","text":"

      We notice that if no swap operations are performed during a certain round of \"bubbling\", it means the array has already completed sorting and can directly return the result. Therefore, we can add a flag flag to monitor this situation and return immediately once it occurs.

      After optimization, the worst-case time complexity and average time complexity of bubble sort remain \\(O(n^2)\\); but when the input array is completely ordered, the best-case time complexity can reach \\(O(n)\\).

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby bubble_sort.py
      def bubble_sort_with_flag(nums: list[int]):\n    \"\"\"Bubble sort (flag optimization)\"\"\"\n    n = len(nums)\n    # Outer loop: unsorted interval is [0, i]\n    for i in range(n - 1, 0, -1):\n        flag = False  # Initialize flag\n        # Inner loop: swap the largest element in the unsorted interval [0, i] to the rightmost end of the interval\n        for j in range(i):\n            if nums[j] > nums[j + 1]:\n                # Swap nums[j] and nums[j + 1]\n                nums[j], nums[j + 1] = nums[j + 1], nums[j]\n                flag = True  # Record element swap\n        if not flag:\n            break  # No elements were swapped in this round of \"bubbling\", exit directly\n
      bubble_sort.cpp
      /* Bubble sort (flag optimization)*/\nvoid bubbleSortWithFlag(vector<int> &nums) {\n    // Outer loop: unsorted range is [0, i]\n    for (int i = nums.size() - 1; i > 0; i--) {\n        bool flag = false; // Initialize flag\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                // Using std::swap() function here\n                swap(nums[j], nums[j + 1]);\n                flag = true; // Record element swap\n            }\n        }\n        if (!flag)\n            break; // No elements were swapped in this round of \"bubbling\", exit directly\n    }\n}\n
      bubble_sort.java
      /* Bubble sort (flag optimization) */\nvoid bubbleSortWithFlag(int[] nums) {\n    // Outer loop: unsorted range is [0, i]\n    for (int i = nums.length - 1; i > 0; i--) {\n        boolean flag = false; // Initialize flag\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                flag = true; // Record element swap\n            }\n        }\n        if (!flag)\n            break; // No elements were swapped in this round of \"bubbling\", exit directly\n    }\n}\n
      bubble_sort.cs
      /* Bubble sort (flag optimization) */\nvoid BubbleSortWithFlag(int[] nums) {\n    // Outer loop: unsorted range is [0, i]\n    for (int i = nums.Length - 1; i > 0; i--) {\n        bool flag = false; // Initialize flag\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n                flag = true;  // Record element swap\n            }\n        }\n        if (!flag) break;     // No elements were swapped in this round of \"bubbling\", exit directly\n    }\n}\n
      bubble_sort.go
      /* Bubble sort (flag optimization) */\nfunc bubbleSortWithFlag(nums []int) {\n    // Outer loop: unsorted range is [0, i]\n    for i := len(nums) - 1; i > 0; i-- {\n        flag := false // Initialize flag\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for j := 0; j < i; j++ {\n            if nums[j] > nums[j+1] {\n                // Swap nums[j] and nums[j + 1]\n                nums[j], nums[j+1] = nums[j+1], nums[j]\n                flag = true // Record element swap\n            }\n        }\n        if flag == false { // No elements were swapped in this round of \"bubbling\", exit directly\n            break\n        }\n    }\n}\n
      bubble_sort.swift
      /* Bubble sort (flag optimization) */\nfunc bubbleSortWithFlag(nums: inout [Int]) {\n    // Outer loop: unsorted range is [0, i]\n    for i in nums.indices.dropFirst().reversed() {\n        var flag = false // Initialize flag\n        for j in 0 ..< i {\n            if nums[j] > nums[j + 1] {\n                // Swap nums[j] and nums[j + 1]\n                nums.swapAt(j, j + 1)\n                flag = true // Record element swap\n            }\n        }\n        if !flag { // No elements were swapped in this round of \"bubbling\", exit directly\n            break\n        }\n    }\n}\n
      bubble_sort.js
      /* Bubble sort (flag optimization) */\nfunction bubbleSortWithFlag(nums) {\n    // Outer loop: unsorted range is [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        let flag = false; // Initialize flag\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                flag = true; // Record element swap\n            }\n        }\n        if (!flag) break; // No elements were swapped in this round of \"bubbling\", exit directly\n    }\n}\n
      bubble_sort.ts
      /* Bubble sort (flag optimization) */\nfunction bubbleSortWithFlag(nums: number[]): void {\n    // Outer loop: unsorted range is [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        let flag = false; // Initialize flag\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                flag = true; // Record element swap\n            }\n        }\n        if (!flag) break; // No elements were swapped in this round of \"bubbling\", exit directly\n    }\n}\n
      bubble_sort.dart
      /* Bubble sort (flag optimization) */\nvoid bubbleSortWithFlag(List<int> nums) {\n  // Outer loop: unsorted range is [0, i]\n  for (int i = nums.length - 1; i > 0; i--) {\n    bool flag = false; // Initialize flag\n    // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n    for (int j = 0; j < i; j++) {\n      if (nums[j] > nums[j + 1]) {\n        // Swap nums[j] and nums[j + 1]\n        int tmp = nums[j];\n        nums[j] = nums[j + 1];\n        nums[j + 1] = tmp;\n        flag = true; // Record element swap\n      }\n    }\n    if (!flag) break; // No elements were swapped in this round of \"bubbling\", exit directly\n  }\n}\n
      bubble_sort.rs
      /* Bubble sort (flag optimization) */\nfn bubble_sort_with_flag(nums: &mut [i32]) {\n    // Outer loop: unsorted range is [0, i]\n    for i in (1..nums.len()).rev() {\n        let mut flag = false; // Initialize flag\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for j in 0..i {\n            if nums[j] > nums[j + 1] {\n                // Swap nums[j] and nums[j + 1]\n                nums.swap(j, j + 1);\n                flag = true; // Record element swap\n            }\n        }\n        if !flag {\n            break; // No elements were swapped in this round of \"bubbling\", exit directly\n        };\n    }\n}\n
      bubble_sort.c
      /* Bubble sort (flag optimization) */\nvoid bubbleSortWithFlag(int nums[], int size) {\n    // Outer loop: unsorted range is [0, i]\n    for (int i = size - 1; i > 0; i--) {\n        bool flag = false;\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                int temp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = temp;\n                flag = true;\n            }\n        }\n        if (!flag)\n            break;\n    }\n}\n
      bubble_sort.kt
      /* Bubble sort (flag optimization) */\nfun bubbleSortWithFlag(nums: IntArray) {\n    // Outer loop: unsorted range is [0, i]\n    for (i in nums.size - 1 downTo 1) {\n        var flag = false // Initialize flag\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (j in 0..<i) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                val temp = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = temp\n                flag = true // Record element swap\n            }\n        }\n        if (!flag) break // No elements were swapped in this round of \"bubbling\", exit directly\n    }\n}\n
      bubble_sort.rb
      ### Bubble sort (flag optimization) ###\ndef bubble_sort_with_flag(nums)\n  n = nums.length\n  # Outer loop: unsorted range is [0, i]\n  for i in (n - 1).downto(1)\n    flag = false # Initialize flag\n\n    # Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n    for j in 0...i\n      if nums[j] > nums[j + 1]\n        # Swap nums[j] and nums[j + 1]\n        nums[j], nums[j + 1] = nums[j + 1], nums[j]\n        flag = true # Record element swap\n      end\n    end\n\n    break unless flag # No elements were swapped in this round of \"bubbling\", exit directly\n  end\nend\n
      "},{"location":"chapter_sorting/bubble_sort/#1133-algorithm-characteristics","title":"11.3.3 \u00a0 Algorithm Characteristics","text":"
      • Time complexity of \\(O(n^2)\\), adaptive sorting: The array lengths traversed in each round of \"bubbling\" are \\(n - 1\\), \\(n - 2\\), \\(\\dots\\), \\(2\\), \\(1\\), totaling \\((n - 1) n / 2\\). After introducing the flag optimization, the best-case time complexity can reach \\(O(n)\\).
      • Space complexity of \\(O(1)\\), in-place sorting: Pointers \\(i\\) and \\(j\\) use a constant amount of extra space.
      • Stable sorting: Since equal elements are not swapped during \"bubbling\".
      "},{"location":"chapter_sorting/bucket_sort/","title":"11.8 \u00a0 Bucket Sort","text":"

      The several sorting algorithms mentioned earlier all belong to \"comparison-based sorting algorithms\", which achieve sorting by comparing the size of elements. The time complexity of such sorting algorithms cannot exceed \\(O(n \\log n)\\). Next, we will explore several \"non-comparison sorting algorithms\", whose time complexity can reach linear order.

      Bucket sort (bucket sort) is a typical application of the divide-and-conquer strategy. It works by setting up buckets with size order, each bucket corresponding to a data range, evenly distributing data to each bucket; then, sorting within each bucket separately; finally, merging all data in the order of the buckets.

      "},{"location":"chapter_sorting/bucket_sort/#1181-algorithm-flow","title":"11.8.1 \u00a0 Algorithm Flow","text":"

      Consider an array of length \\(n\\), whose elements are floating-point numbers in the range \\([0, 1)\\). The flow of bucket sort is shown in Figure 11-13.

      1. Initialize \\(k\\) buckets and distribute the \\(n\\) elements into the \\(k\\) buckets.
      2. Sort each bucket separately (here we use the built-in sorting function of the programming language).
      3. Merge the results in order from smallest to largest bucket.

      Figure 11-13 \u00a0 Bucket sort algorithm flow

      The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby bucket_sort.py
      def bucket_sort(nums: list[float]):\n    \"\"\"Bucket sort\"\"\"\n    # Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n    k = len(nums) // 2\n    buckets = [[] for _ in range(k)]\n    # 1. Distribute array elements into various buckets\n    for num in nums:\n        # Input data range is [0, 1), use num * k to map to index range [0, k-1]\n        i = int(num * k)\n        # Add num to bucket i\n        buckets[i].append(num)\n    # 2. Sort each bucket\n    for bucket in buckets:\n        # Use built-in sorting function, can also replace with other sorting algorithms\n        bucket.sort()\n    # 3. Traverse buckets to merge results\n    i = 0\n    for bucket in buckets:\n        for num in bucket:\n            nums[i] = num\n            i += 1\n
      bucket_sort.cpp
      /* Bucket sort */\nvoid bucketSort(vector<float> &nums) {\n    // Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n    int k = nums.size() / 2;\n    vector<vector<float>> buckets(k);\n    // 1. Distribute array elements into various buckets\n    for (float num : nums) {\n        // Input data range is [0, 1), use num * k to map to index range [0, k-1]\n        int i = num * k;\n        // Add num to bucket bucket_idx\n        buckets[i].push_back(num);\n    }\n    // 2. Sort each bucket\n    for (vector<float> &bucket : buckets) {\n        // Use built-in sorting function, can also replace with other sorting algorithms\n        sort(bucket.begin(), bucket.end());\n    }\n    // 3. Traverse buckets to merge results\n    int i = 0;\n    for (vector<float> &bucket : buckets) {\n        for (float num : bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
      bucket_sort.java
      /* Bucket sort */\nvoid bucketSort(float[] nums) {\n    // Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n    int k = nums.length / 2;\n    List<List<Float>> buckets = new ArrayList<>();\n    for (int i = 0; i < k; i++) {\n        buckets.add(new ArrayList<>());\n    }\n    // 1. Distribute array elements into various buckets\n    for (float num : nums) {\n        // Input data range is [0, 1), use num * k to map to index range [0, k-1]\n        int i = (int) (num * k);\n        // Add num to bucket i\n        buckets.get(i).add(num);\n    }\n    // 2. Sort each bucket\n    for (List<Float> bucket : buckets) {\n        // Use built-in sorting function, can also replace with other sorting algorithms\n        Collections.sort(bucket);\n    }\n    // 3. Traverse buckets to merge results\n    int i = 0;\n    for (List<Float> bucket : buckets) {\n        for (float num : bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
      bucket_sort.cs
      /* Bucket sort */\nvoid BucketSort(float[] nums) {\n    // Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n    int k = nums.Length / 2;\n    List<List<float>> buckets = [];\n    for (int i = 0; i < k; i++) {\n        buckets.Add([]);\n    }\n    // 1. Distribute array elements into various buckets\n    foreach (float num in nums) {\n        // Input data range is [0, 1), use num * k to map to index range [0, k-1]\n        int i = (int)(num * k);\n        // Add num to bucket i\n        buckets[i].Add(num);\n    }\n    // 2. Sort each bucket\n    foreach (List<float> bucket in buckets) {\n        // Use built-in sorting function, can also replace with other sorting algorithms\n        bucket.Sort();\n    }\n    // 3. Traverse buckets to merge results\n    int j = 0;\n    foreach (List<float> bucket in buckets) {\n        foreach (float num in bucket) {\n            nums[j++] = num;\n        }\n    }\n}\n
      bucket_sort.go
      /* Bucket sort */\nfunc bucketSort(nums []float64) {\n    // Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n    k := len(nums) / 2\n    buckets := make([][]float64, k)\n    for i := 0; i < k; i++ {\n        buckets[i] = make([]float64, 0)\n    }\n    // 1. Distribute array elements into various buckets\n    for _, num := range nums {\n        // Input data range is [0, 1), use num * k to map to index range [0, k-1]\n        i := int(num * float64(k))\n        // Add num to bucket i\n        buckets[i] = append(buckets[i], num)\n    }\n    // 2. Sort each bucket\n    for i := 0; i < k; i++ {\n        // Use built-in slice sorting function, can also be replaced with other sorting algorithms\n        sort.Float64s(buckets[i])\n    }\n    // 3. Traverse buckets to merge results\n    i := 0\n    for _, bucket := range buckets {\n        for _, num := range bucket {\n            nums[i] = num\n            i++\n        }\n    }\n}\n
      bucket_sort.swift
      /* Bucket sort */\nfunc bucketSort(nums: inout [Double]) {\n    // Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n    let k = nums.count / 2\n    var buckets = (0 ..< k).map { _ in [Double]() }\n    // 1. Distribute array elements into various buckets\n    for num in nums {\n        // Input data range is [0, 1), use num * k to map to index range [0, k-1]\n        let i = Int(num * Double(k))\n        // Add num to bucket i\n        buckets[i].append(num)\n    }\n    // 2. Sort each bucket\n    for i in buckets.indices {\n        // Use built-in sorting function, can also replace with other sorting algorithms\n        buckets[i].sort()\n    }\n    // 3. Traverse buckets to merge results\n    var i = nums.startIndex\n    for bucket in buckets {\n        for num in bucket {\n            nums[i] = num\n            i += 1\n        }\n    }\n}\n
      bucket_sort.js
      /* Bucket sort */\nfunction bucketSort(nums) {\n    // Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n    const k = nums.length / 2;\n    const buckets = [];\n    for (let i = 0; i < k; i++) {\n        buckets.push([]);\n    }\n    // 1. Distribute array elements into various buckets\n    for (const num of nums) {\n        // Input data range is [0, 1), use num * k to map to index range [0, k-1]\n        const i = Math.floor(num * k);\n        // Add num to bucket i\n        buckets[i].push(num);\n    }\n    // 2. Sort each bucket\n    for (const bucket of buckets) {\n        // Use built-in sorting function, can also replace with other sorting algorithms\n        bucket.sort((a, b) => a - b);\n    }\n    // 3. Traverse buckets to merge results\n    let i = 0;\n    for (const bucket of buckets) {\n        for (const num of bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
      bucket_sort.ts
      /* Bucket sort */\nfunction bucketSort(nums: number[]): void {\n    // Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n    const k = nums.length / 2;\n    const buckets: number[][] = [];\n    for (let i = 0; i < k; i++) {\n        buckets.push([]);\n    }\n    // 1. Distribute array elements into various buckets\n    for (const num of nums) {\n        // Input data range is [0, 1), use num * k to map to index range [0, k-1]\n        const i = Math.floor(num * k);\n        // Add num to bucket i\n        buckets[i].push(num);\n    }\n    // 2. Sort each bucket\n    for (const bucket of buckets) {\n        // Use built-in sorting function, can also replace with other sorting algorithms\n        bucket.sort((a, b) => a - b);\n    }\n    // 3. Traverse buckets to merge results\n    let i = 0;\n    for (const bucket of buckets) {\n        for (const num of bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
      bucket_sort.dart
      /* Bucket sort */\nvoid bucketSort(List<double> nums) {\n  // Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n  int k = nums.length ~/ 2;\n  List<List<double>> buckets = List.generate(k, (index) => []);\n\n  // 1. Distribute array elements into various buckets\n  for (double _num in nums) {\n    // Input data range is [0, 1), use _num * k to map to index range [0, k-1]\n    int i = (_num * k).toInt();\n    // Add _num to bucket bucket_idx\n    buckets[i].add(_num);\n  }\n  // 2. Sort each bucket\n  for (List<double> bucket in buckets) {\n    bucket.sort();\n  }\n  // 3. Traverse buckets to merge results\n  int i = 0;\n  for (List<double> bucket in buckets) {\n    for (double _num in bucket) {\n      nums[i++] = _num;\n    }\n  }\n}\n
      bucket_sort.rs
      /* Bucket sort */\nfn bucket_sort(nums: &mut [f64]) {\n    // Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n    let k = nums.len() / 2;\n    let mut buckets = vec![vec![]; k];\n    // 1. Distribute array elements into various buckets\n    for &num in nums.iter() {\n        // Input data range is [0, 1), use num * k to map to index range [0, k-1]\n        let i = (num * k as f64) as usize;\n        // Add num to bucket i\n        buckets[i].push(num);\n    }\n    // 2. Sort each bucket\n    for bucket in &mut buckets {\n        // Use built-in sorting function, can also replace with other sorting algorithms\n        bucket.sort_by(|a, b| a.partial_cmp(b).unwrap());\n    }\n    // 3. Traverse buckets to merge results\n    let mut i = 0;\n    for bucket in buckets.iter() {\n        for &num in bucket.iter() {\n            nums[i] = num;\n            i += 1;\n        }\n    }\n}\n
      bucket_sort.c
      /* Bucket sort */\nvoid bucketSort(float nums[], int n) {\n    int k = n / 2;                                 // Initialize k = n/2 buckets\n    int *sizes = malloc(k * sizeof(int));          // Record each bucket's size\n    float **buckets = malloc(k * sizeof(float *)); // Array of dynamic arrays (buckets)\n    // Pre-allocate sufficient space for each bucket\n    for (int i = 0; i < k; ++i) {\n        buckets[i] = (float *)malloc(n * sizeof(float));\n        sizes[i] = 0;\n    }\n    // 1. Distribute array elements into various buckets\n    for (int i = 0; i < n; ++i) {\n        int idx = (int)(nums[i] * k);\n        buckets[idx][sizes[idx]++] = nums[i];\n    }\n    // 2. Sort each bucket\n    for (int i = 0; i < k; ++i) {\n        qsort(buckets[i], sizes[i], sizeof(float), compare);\n    }\n    // 3. Merge sorted buckets\n    int idx = 0;\n    for (int i = 0; i < k; ++i) {\n        for (int j = 0; j < sizes[i]; ++j) {\n            nums[idx++] = buckets[i][j];\n        }\n        // Free memory\n        free(buckets[i]);\n    }\n}\n
      bucket_sort.kt
      /* Bucket sort */\nfun bucketSort(nums: FloatArray) {\n    // Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n    val k = nums.size / 2\n    val buckets = mutableListOf<MutableList<Float>>()\n    for (i in 0..<k) {\n        buckets.add(mutableListOf())\n    }\n    // 1. Distribute array elements into various buckets\n    for (num in nums) {\n        // Input data range is [0, 1), use num * k to map to index range [0, k-1]\n        val i = (num * k).toInt()\n        // Add num to bucket i\n        buckets[i].add(num)\n    }\n    // 2. Sort each bucket\n    for (bucket in buckets) {\n        // Use built-in sorting function, can also replace with other sorting algorithms\n        bucket.sort()\n    }\n    // 3. Traverse buckets to merge results\n    var i = 0\n    for (bucket in buckets) {\n        for (num in bucket) {\n            nums[i++] = num\n        }\n    }\n}\n
      bucket_sort.rb
      ### Bucket sort ###\ndef bucket_sort(nums)\n  # Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n  k = nums.length / 2\n  buckets = Array.new(k) { [] }\n\n  # 1. Distribute array elements into various buckets\n  nums.each do |num|\n    # Input data range is [0, 1), use num * k to map to index range [0, k-1]\n    i = (num * k).to_i\n    # Add num to bucket i\n    buckets[i] << num\n  end\n\n  # 2. Sort each bucket\n  buckets.each do |bucket|\n    # Use built-in sorting function, can also replace with other sorting algorithms\n    bucket.sort!\n  end\n\n  # 3. Traverse buckets to merge results\n  i = 0\n  buckets.each do |bucket|\n    bucket.each do |num|\n      nums[i] = num\n      i += 1\n    end\n  end\nend\n
      "},{"location":"chapter_sorting/bucket_sort/#1182-algorithm-characteristics","title":"11.8.2 \u00a0 Algorithm Characteristics","text":"

      Bucket sort is suitable for processing very large data volumes. For example, if the input data contains 1 million elements and system memory cannot load all the data at once, the data can be divided into 1000 buckets, each bucket sorted separately, and then the results merged.

      • Time complexity of \\(O(n + k)\\): Assuming the elements are evenly distributed among the buckets, then the number of elements in each bucket is \\(\\frac{n}{k}\\). Assuming sorting a single bucket uses \\(O(\\frac{n}{k} \\log\\frac{n}{k})\\) time, then sorting all buckets uses \\(O(n \\log\\frac{n}{k})\\) time. When the number of buckets \\(k\\) is relatively large, the time complexity approaches \\(O(n)\\). Merging results requires traversing all buckets and elements, taking \\(O(n + k)\\) time. In the worst case, all data is distributed into one bucket, and sorting that bucket uses \\(O(n^2)\\) time.
      • Space complexity of \\(O(n + k)\\), non-in-place sorting: Additional space is required for \\(k\\) buckets and a total of \\(n\\) elements.
      • Whether bucket sort is stable depends on whether the algorithm for sorting elements within buckets is stable.
      "},{"location":"chapter_sorting/bucket_sort/#1183-how-to-achieve-even-distribution","title":"11.8.3 \u00a0 How to Achieve Even Distribution","text":"

      Theoretically, bucket sort can achieve \\(O(n)\\) time complexity. The key is to evenly distribute elements to each bucket, because real data is often not evenly distributed. For example, if we want to evenly distribute all products on Taobao into 10 buckets by price range, there may be very many products below 100 yuan and very few above 1000 yuan. If the price intervals are evenly divided into 10, the difference in the number of products in each bucket will be very large.

      To achieve even distribution, we can first set an approximate dividing line to roughly divide the data into 3 buckets. After distribution is complete, continue dividing buckets with more products into 3 buckets until the number of elements in all buckets is roughly equal.

      As shown in Figure 11-14, this method essentially creates a recursion tree, with the goal of making the values of leaf nodes as even as possible. Of course, it is not necessary to divide the data into 3 buckets every round; the specific division method can be flexibly chosen according to data characteristics.

      Figure 11-14 \u00a0 Recursively dividing buckets

      If we know the probability distribution of product prices in advance, we can set the price dividing line for each bucket based on the data probability distribution. It is worth noting that the data distribution does not necessarily need to be specifically calculated, but can also be approximated using a certain probability model based on data characteristics.

      As shown in Figure 11-15, we assume that product prices follow a normal distribution, which allows us to reasonably set price intervals to evenly distribute products to each bucket.

      Figure 11-15 \u00a0 Dividing buckets based on probability distribution

      "},{"location":"chapter_sorting/counting_sort/","title":"11.9 \u00a0 Counting Sort","text":"

      Counting sort (counting sort) achieves sorting by counting the number of elements, typically applied to integer arrays.

      "},{"location":"chapter_sorting/counting_sort/#1191-simple-implementation","title":"11.9.1 \u00a0 Simple Implementation","text":"

      Let's start with a simple example. Given an array nums of length \\(n\\), where the elements are all \"non-negative integers\", the overall flow of counting sort is shown in Figure 11-16.

      1. Traverse the array to find the largest number, denoted as \\(m\\), and then create an auxiliary array counter of length \\(m + 1\\).
      2. Use counter to count the number of occurrences of each number in nums, where counter[num] corresponds to the number of occurrences of the number num. The counting method is simple: just traverse nums (let the current number be num), and increase counter[num] by \\(1\\) in each round.
      3. Since each index of counter is naturally ordered, this is equivalent to all numbers being sorted. Next, we traverse counter and fill in nums in ascending order based on the number of occurrences of each number.

      Figure 11-16 \u00a0 Counting sort flow

      The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby counting_sort.py
      def counting_sort_naive(nums: list[int]):\n    \"\"\"Counting sort\"\"\"\n    # Simple implementation, cannot be used for sorting objects\n    # 1. Count the maximum element m in the array\n    m = 0\n    for num in nums:\n        m = max(m, num)\n    # 2. Count the occurrence of each number\n    # counter[num] represents the occurrence of num\n    counter = [0] * (m + 1)\n    for num in nums:\n        counter[num] += 1\n    # 3. Traverse counter, filling each element back into the original array nums\n    i = 0\n    for num in range(m + 1):\n        for _ in range(counter[num]):\n            nums[i] = num\n            i += 1\n
      counting_sort.cpp
      /* Counting sort */\n// Simple implementation, cannot be used for sorting objects\nvoid countingSortNaive(vector<int> &nums) {\n    // 1. Count the maximum element m in the array\n    int m = 0;\n    for (int num : nums) {\n        m = max(m, num);\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    vector<int> counter(m + 1, 0);\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. Traverse counter, filling each element back into the original array nums\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
      counting_sort.java
      /* Counting sort */\n// Simple implementation, cannot be used for sorting objects\nvoid countingSortNaive(int[] nums) {\n    // 1. Count the maximum element m in the array\n    int m = 0;\n    for (int num : nums) {\n        m = Math.max(m, num);\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    int[] counter = new int[m + 1];\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. Traverse counter, filling each element back into the original array nums\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
      counting_sort.cs
      /* Counting sort */\n// Simple implementation, cannot be used for sorting objects\nvoid CountingSortNaive(int[] nums) {\n    // 1. Count the maximum element m in the array\n    int m = 0;\n    foreach (int num in nums) {\n        m = Math.Max(m, num);\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    int[] counter = new int[m + 1];\n    foreach (int num in nums) {\n        counter[num]++;\n    }\n    // 3. Traverse counter, filling each element back into the original array nums\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
      counting_sort.go
      /* Counting sort */\n// Simple implementation, cannot be used for sorting objects\nfunc countingSortNaive(nums []int) {\n    // 1. Count the maximum element m in the array\n    m := 0\n    for _, num := range nums {\n        if num > m {\n            m = num\n        }\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    counter := make([]int, m+1)\n    for _, num := range nums {\n        counter[num]++\n    }\n    // 3. Traverse counter, filling each element back into the original array nums\n    for i, num := 0, 0; num < m+1; num++ {\n        for j := 0; j < counter[num]; j++ {\n            nums[i] = num\n            i++\n        }\n    }\n}\n
      counting_sort.swift
      /* Counting sort */\n// Simple implementation, cannot be used for sorting objects\nfunc countingSortNaive(nums: inout [Int]) {\n    // 1. Count the maximum element m in the array\n    let m = nums.max()!\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    var counter = Array(repeating: 0, count: m + 1)\n    for num in nums {\n        counter[num] += 1\n    }\n    // 3. Traverse counter, filling each element back into the original array nums\n    var i = 0\n    for num in 0 ..< m + 1 {\n        for _ in 0 ..< counter[num] {\n            nums[i] = num\n            i += 1\n        }\n    }\n}\n
      counting_sort.js
      /* Counting sort */\n// Simple implementation, cannot be used for sorting objects\nfunction countingSortNaive(nums) {\n    // 1. Count the maximum element m in the array\n    let m = Math.max(...nums);\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    const counter = new Array(m + 1).fill(0);\n    for (const num of nums) {\n        counter[num]++;\n    }\n    // 3. Traverse counter, filling each element back into the original array nums\n    let i = 0;\n    for (let num = 0; num < m + 1; num++) {\n        for (let j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
      counting_sort.ts
      /* Counting sort */\n// Simple implementation, cannot be used for sorting objects\nfunction countingSortNaive(nums: number[]): void {\n    // 1. Count the maximum element m in the array\n    let m: number = Math.max(...nums);\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    const counter: number[] = new Array<number>(m + 1).fill(0);\n    for (const num of nums) {\n        counter[num]++;\n    }\n    // 3. Traverse counter, filling each element back into the original array nums\n    let i = 0;\n    for (let num = 0; num < m + 1; num++) {\n        for (let j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
      counting_sort.dart
      /* Counting sort */\n// Simple implementation, cannot be used for sorting objects\nvoid countingSortNaive(List<int> nums) {\n  // 1. Count the maximum element m in the array\n  int m = 0;\n  for (int _num in nums) {\n    m = max(m, _num);\n  }\n  // 2. Count the occurrence of each number\n  // counter[_num] represents occurrence count of _num\n  List<int> counter = List.filled(m + 1, 0);\n  for (int _num in nums) {\n    counter[_num]++;\n  }\n  // 3. Traverse counter, filling each element back into the original array nums\n  int i = 0;\n  for (int _num = 0; _num < m + 1; _num++) {\n    for (int j = 0; j < counter[_num]; j++, i++) {\n      nums[i] = _num;\n    }\n  }\n}\n
      counting_sort.rs
      /* Counting sort */\n// Simple implementation, cannot be used for sorting objects\nfn counting_sort_naive(nums: &mut [i32]) {\n    // 1. Count the maximum element m in the array\n    let m = *nums.iter().max().unwrap();\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    let mut counter = vec![0; m as usize + 1];\n    for &num in nums.iter() {\n        counter[num as usize] += 1;\n    }\n    // 3. Traverse counter, filling each element back into the original array nums\n    let mut i = 0;\n    for num in 0..m + 1 {\n        for _ in 0..counter[num as usize] {\n            nums[i] = num;\n            i += 1;\n        }\n    }\n}\n
      counting_sort.c
      /* Counting sort */\n// Simple implementation, cannot be used for sorting objects\nvoid countingSortNaive(int nums[], int size) {\n    // 1. Count the maximum element m in the array\n    int m = 0;\n    for (int i = 0; i < size; i++) {\n        if (nums[i] > m) {\n            m = nums[i];\n        }\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    int *counter = calloc(m + 1, sizeof(int));\n    for (int i = 0; i < size; i++) {\n        counter[nums[i]]++;\n    }\n    // 3. Traverse counter, filling each element back into the original array nums\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n    // 4. Free memory\n    free(counter);\n}\n
      counting_sort.kt
      /* Counting sort */\n// Simple implementation, cannot be used for sorting objects\nfun countingSortNaive(nums: IntArray) {\n    // 1. Count the maximum element m in the array\n    var m = 0\n    for (num in nums) {\n        m = max(m, num)\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    val counter = IntArray(m + 1)\n    for (num in nums) {\n        counter[num]++\n    }\n    // 3. Traverse counter, filling each element back into the original array nums\n    var i = 0\n    for (num in 0..<m + 1) {\n        var j = 0\n        while (j < counter[num]) {\n            nums[i] = num\n            j++\n            i++\n        }\n    }\n}\n
      counting_sort.rb
      ### Counting sort ###\ndef counting_sort_naive(nums)\n  # Simple implementation, cannot be used for sorting objects\n  # 1. Count the maximum element m in the array\n  m = 0\n  nums.each { |num| m = [m, num].max }\n  # 2. Count the occurrence of each number\n  # counter[num] represents the occurrence of num\n  counter = Array.new(m + 1, 0)\n  nums.each { |num| counter[num] += 1 }\n  # 3. Traverse counter, filling each element back into the original array nums\n  i = 0\n  for num in 0...(m + 1)\n    (0...counter[num]).each do\n      nums[i] = num\n      i += 1\n    end\n  end\nend\n

      Connection between counting sort and bucket sort

      From the perspective of bucket sort, we can regard each index of the counting array counter in counting sort as a bucket, and the process of counting quantities as distributing each element to the corresponding bucket. Essentially, counting sort is a special case of bucket sort for integer data.

      "},{"location":"chapter_sorting/counting_sort/#1192-complete-implementation","title":"11.9.2 \u00a0 Complete Implementation","text":"

      Observant readers may have noticed that if the input data is objects, step 3. above becomes invalid. Suppose the input data is product objects, and we want to sort the products by price (a member variable of the class), but the above algorithm can only give the sorting result of prices.

      So how can we obtain the sorting result of the original data? We first calculate the \"prefix sum\" of counter. As the name suggests, the prefix sum at index i, prefix[i], equals the sum of the first i elements of the array:

      \\[ \\text{prefix}[i] = \\sum_{j=0}^i \\text{counter[j]} \\]

      The prefix sum has a clear meaning: prefix[num] - 1 represents the index of the last occurrence of element num in the result array res. This information is very critical because it tells us where each element should appear in the result array. Next, we traverse each element num of the original array nums in reverse order, performing the following two steps in each iteration.

      1. Fill num into the array res at index prefix[num] - 1.
      2. Decrease the prefix sum prefix[num] by \\(1\\) to get the index for the next placement of num.

      After the traversal is complete, the array res contains the sorted result, and finally res is used to overwrite the original array nums. The complete counting sort flow is shown in Figure 11-17.

      <1><2><3><4><5><6><7><8>

      Figure 11-17 \u00a0 Counting sort steps

      The implementation code of counting sort is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby counting_sort.py
      def counting_sort(nums: list[int]):\n    \"\"\"Counting sort\"\"\"\n    # Complete implementation, can sort objects and is a stable sort\n    # 1. Count the maximum element m in the array\n    m = max(nums)\n    # 2. Count the occurrence of each number\n    # counter[num] represents the occurrence of num\n    counter = [0] * (m + 1)\n    for num in nums:\n        counter[num] += 1\n    # 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n    # counter[num]-1 is the last index where num appears in res\n    for i in range(m):\n        counter[i + 1] += counter[i]\n    # 4. Traverse nums in reverse order, placing each element into the result array res\n    # Initialize the array res to record results\n    n = len(nums)\n    res = [0] * n\n    for i in range(n - 1, -1, -1):\n        num = nums[i]\n        res[counter[num] - 1] = num  # Place num at the corresponding index\n        counter[num] -= 1  # Decrement the prefix sum by 1, getting the next index to place num\n    # Use result array res to overwrite the original array nums\n    for i in range(n):\n        nums[i] = res[i]\n
      counting_sort.cpp
      /* Counting sort */\n// Complete implementation, can sort objects and is a stable sort\nvoid countingSort(vector<int> &nums) {\n    // 1. Count the maximum element m in the array\n    int m = 0;\n    for (int num : nums) {\n        m = max(m, num);\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    vector<int> counter(m + 1, 0);\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n    // counter[num]-1 is the last index where num appears in res\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. Traverse nums in reverse order, placing each element into the result array res\n    // Initialize the array res to record results\n    int n = nums.size();\n    vector<int> res(n);\n    for (int i = n - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // Place num at the corresponding index\n        counter[num]--;              // Decrement the prefix sum by 1, getting the next index to place num\n    }\n    // Use result array res to overwrite the original array nums\n    nums = res;\n}\n
      counting_sort.java
      /* Counting sort */\n// Complete implementation, can sort objects and is a stable sort\nvoid countingSort(int[] nums) {\n    // 1. Count the maximum element m in the array\n    int m = 0;\n    for (int num : nums) {\n        m = Math.max(m, num);\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    int[] counter = new int[m + 1];\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n    // counter[num]-1 is the last index where num appears in res\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. Traverse nums in reverse order, placing each element into the result array res\n    // Initialize the array res to record results\n    int n = nums.length;\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // Place num at the corresponding index\n        counter[num]--; // Decrement the prefix sum by 1, getting the next index to place num\n    }\n    // Use result array res to overwrite the original array nums\n    for (int i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n
      counting_sort.cs
      /* Counting sort */\n// Complete implementation, can sort objects and is a stable sort\nvoid CountingSort(int[] nums) {\n    // 1. Count the maximum element m in the array\n    int m = 0;\n    foreach (int num in nums) {\n        m = Math.Max(m, num);\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    int[] counter = new int[m + 1];\n    foreach (int num in nums) {\n        counter[num]++;\n    }\n    // 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n    // counter[num]-1 is the last index where num appears in res\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. Traverse nums in reverse order, placing each element into the result array res\n    // Initialize the array res to record results\n    int n = nums.Length;\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // Place num at the corresponding index\n        counter[num]--; // Decrement the prefix sum by 1, getting the next index to place num\n    }\n    // Use result array res to overwrite the original array nums\n    for (int i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n
      counting_sort.go
      /* Counting sort */\n// Complete implementation, can sort objects and is a stable sort\nfunc countingSort(nums []int) {\n    // 1. Count the maximum element m in the array\n    m := 0\n    for _, num := range nums {\n        if num > m {\n            m = num\n        }\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    counter := make([]int, m+1)\n    for _, num := range nums {\n        counter[num]++\n    }\n    // 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n    // counter[num]-1 is the last index where num appears in res\n    for i := 0; i < m; i++ {\n        counter[i+1] += counter[i]\n    }\n    // 4. Traverse nums in reverse order, placing each element into the result array res\n    // Initialize the array res to record results\n    n := len(nums)\n    res := make([]int, n)\n    for i := n - 1; i >= 0; i-- {\n        num := nums[i]\n        // Place num at the corresponding index\n        res[counter[num]-1] = num\n        // Decrement the prefix sum by 1, getting the next index to place num\n        counter[num]--\n    }\n    // Use result array res to overwrite the original array nums\n    copy(nums, res)\n}\n
      counting_sort.swift
      /* Counting sort */\n// Complete implementation, can sort objects and is a stable sort\nfunc countingSort(nums: inout [Int]) {\n    // 1. Count the maximum element m in the array\n    let m = nums.max()!\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    var counter = Array(repeating: 0, count: m + 1)\n    for num in nums {\n        counter[num] += 1\n    }\n    // 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n    // counter[num]-1 is the last index where num appears in res\n    for i in 0 ..< m {\n        counter[i + 1] += counter[i]\n    }\n    // 4. Traverse nums in reverse order, placing each element into the result array res\n    // Initialize the array res to record results\n    var res = Array(repeating: 0, count: nums.count)\n    for i in nums.indices.reversed() {\n        let num = nums[i]\n        res[counter[num] - 1] = num // Place num at the corresponding index\n        counter[num] -= 1 // Decrement the prefix sum by 1, getting the next index to place num\n    }\n    // Use result array res to overwrite the original array nums\n    for i in nums.indices {\n        nums[i] = res[i]\n    }\n}\n
      counting_sort.js
      /* Counting sort */\n// Complete implementation, can sort objects and is a stable sort\nfunction countingSort(nums) {\n    // 1. Count the maximum element m in the array\n    let m = Math.max(...nums);\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    const counter = new Array(m + 1).fill(0);\n    for (const num of nums) {\n        counter[num]++;\n    }\n    // 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n    // counter[num]-1 is the last index where num appears in res\n    for (let i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. Traverse nums in reverse order, placing each element into the result array res\n    // Initialize the array res to record results\n    const n = nums.length;\n    const res = new Array(n);\n    for (let i = n - 1; i >= 0; i--) {\n        const num = nums[i];\n        res[counter[num] - 1] = num; // Place num at the corresponding index\n        counter[num]--; // Decrement the prefix sum by 1, getting the next index to place num\n    }\n    // Use result array res to overwrite the original array nums\n    for (let i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n
      counting_sort.ts
      /* Counting sort */\n// Complete implementation, can sort objects and is a stable sort\nfunction countingSort(nums: number[]): void {\n    // 1. Count the maximum element m in the array\n    let m: number = Math.max(...nums);\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    const counter: number[] = new Array<number>(m + 1).fill(0);\n    for (const num of nums) {\n        counter[num]++;\n    }\n    // 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n    // counter[num]-1 is the last index where num appears in res\n    for (let i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. Traverse nums in reverse order, placing each element into the result array res\n    // Initialize the array res to record results\n    const n = nums.length;\n    const res: number[] = new Array<number>(n);\n    for (let i = n - 1; i >= 0; i--) {\n        const num = nums[i];\n        res[counter[num] - 1] = num; // Place num at the corresponding index\n        counter[num]--; // Decrement the prefix sum by 1, getting the next index to place num\n    }\n    // Use result array res to overwrite the original array nums\n    for (let i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n
      counting_sort.dart
      /* Counting sort */\n// Complete implementation, can sort objects and is a stable sort\nvoid countingSort(List<int> nums) {\n  // 1. Count the maximum element m in the array\n  int m = 0;\n  for (int _num in nums) {\n    m = max(m, _num);\n  }\n  // 2. Count the occurrence of each number\n  // counter[_num] represents occurrence count of _num\n  List<int> counter = List.filled(m + 1, 0);\n  for (int _num in nums) {\n    counter[_num]++;\n  }\n  // 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n  // That is, counter[_num]-1 is the last occurrence index of _num in res\n  for (int i = 0; i < m; i++) {\n    counter[i + 1] += counter[i];\n  }\n  // 4. Traverse nums in reverse order, placing each element into the result array res\n  // Initialize the array res to record results\n  int n = nums.length;\n  List<int> res = List.filled(n, 0);\n  for (int i = n - 1; i >= 0; i--) {\n    int _num = nums[i];\n    res[counter[_num] - 1] = _num; // Place _num at corresponding index\n    counter[_num]--; // Decrement prefix sum by 1 to get next placement index for _num\n  }\n  // Use result array res to overwrite the original array nums\n  nums.setAll(0, res);\n}\n
      counting_sort.rs
      /* Counting sort */\n// Complete implementation, can sort objects and is a stable sort\nfn counting_sort(nums: &mut [i32]) {\n    // 1. Count the maximum element m in the array\n    let m = *nums.iter().max().unwrap() as usize;\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    let mut counter = vec![0; m + 1];\n    for &num in nums.iter() {\n        counter[num as usize] += 1;\n    }\n    // 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n    // counter[num]-1 is the last index where num appears in res\n    for i in 0..m {\n        counter[i + 1] += counter[i];\n    }\n    // 4. Traverse nums in reverse order, placing each element into the result array res\n    // Initialize the array res to record results\n    let n = nums.len();\n    let mut res = vec![0; n];\n    for i in (0..n).rev() {\n        let num = nums[i];\n        res[counter[num as usize] - 1] = num; // Place num at the corresponding index\n        counter[num as usize] -= 1; // Decrement the prefix sum by 1, getting the next index to place num\n    }\n    // Use result array res to overwrite the original array nums\n    nums.copy_from_slice(&res)\n}\n
      counting_sort.c
      /* Counting sort */\n// Complete implementation, can sort objects and is a stable sort\nvoid countingSort(int nums[], int size) {\n    // 1. Count the maximum element m in the array\n    int m = 0;\n    for (int i = 0; i < size; i++) {\n        if (nums[i] > m) {\n            m = nums[i];\n        }\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    int *counter = calloc(m, sizeof(int));\n    for (int i = 0; i < size; i++) {\n        counter[nums[i]]++;\n    }\n    // 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n    // counter[num]-1 is the last index where num appears in res\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. Traverse nums in reverse order, placing each element into the result array res\n    // Initialize the array res to record results\n    int *res = malloc(sizeof(int) * size);\n    for (int i = size - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // Place num at the corresponding index\n        counter[num]--;              // Decrement the prefix sum by 1, getting the next index to place num\n    }\n    // Use result array res to overwrite the original array nums\n    memcpy(nums, res, size * sizeof(int));\n    // 5. Free memory\n    free(res);\n    free(counter);\n}\n
      counting_sort.kt
      /* Counting sort */\n// Complete implementation, can sort objects and is a stable sort\nfun countingSort(nums: IntArray) {\n    // 1. Count the maximum element m in the array\n    var m = 0\n    for (num in nums) {\n        m = max(m, num)\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    val counter = IntArray(m + 1)\n    for (num in nums) {\n        counter[num]++\n    }\n    // 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n    // counter[num]-1 is the last index where num appears in res\n    for (i in 0..<m) {\n        counter[i + 1] += counter[i]\n    }\n    // 4. Traverse nums in reverse order, placing each element into the result array res\n    // Initialize the array res to record results\n    val n = nums.size\n    val res = IntArray(n)\n    for (i in n - 1 downTo 0) {\n        val num = nums[i]\n        res[counter[num] - 1] = num // Place num at the corresponding index\n        counter[num]-- // Decrement the prefix sum by 1, getting the next index to place num\n    }\n    // Use result array res to overwrite the original array nums\n    for (i in 0..<n) {\n        nums[i] = res[i]\n    }\n}\n
      counting_sort.rb
      ### Counting sort ###\ndef counting_sort(nums)\n  # Complete implementation, can sort objects and is a stable sort\n  # 1. Count the maximum element m in the array\n  m = nums.max\n  # 2. Count the occurrence of each number\n  # counter[num] represents the occurrence of num\n  counter = Array.new(m + 1, 0)\n  nums.each { |num| counter[num] += 1 }\n  # 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n  # counter[num]-1 is the last index where num appears in res\n  (0...m).each { |i| counter[i + 1] += counter[i] }\n  # 4. Traverse nums in reverse, fill elements into result array res\n  # Initialize the array res to record results\n  n = nums.length\n  res = Array.new(n, 0)\n  (n - 1).downto(0).each do |i|\n    num = nums[i]\n    res[counter[num] - 1] = num # Place num at the corresponding index\n    counter[num] -= 1 # Decrement the prefix sum by 1, getting the next index to place num\n  end\n  # Use result array res to overwrite the original array nums\n  (0...n).each { |i| nums[i] = res[i] }\nend\n
      "},{"location":"chapter_sorting/counting_sort/#1193-algorithm-characteristics","title":"11.9.3 \u00a0 Algorithm Characteristics","text":"
      • Time complexity of \\(O(n + m)\\), non-adaptive sorting: Involves traversing nums and traversing counter, both using linear time. Generally, \\(n \\gg m\\), and time complexity tends toward \\(O(n)\\).
      • Space complexity of \\(O(n + m)\\), non-in-place sorting: Uses arrays res and counter of lengths \\(n\\) and \\(m\\) respectively.
      • Stable sorting: Since elements are filled into res in a \"right-to-left\" order, traversing nums in reverse can avoid changing the relative positions of equal elements, thereby achieving stable sorting. In fact, traversing nums in forward order can also yield correct sorting results, but the result would be unstable.
      "},{"location":"chapter_sorting/counting_sort/#1194-limitations","title":"11.9.4 \u00a0 Limitations","text":"

      By this point, you might think counting sort is very clever, as it can achieve efficient sorting just by counting quantities. However, the prerequisites for using counting sort are relatively strict.

      Counting sort is only suitable for non-negative integers. If you want to apply it to other types of data, you need to ensure that the data can be converted to non-negative integers without changing the relative size relationships between elements. For example, for an integer array containing negative numbers, you can first add a constant to all numbers to convert them all to positive numbers, and then convert them back after sorting is complete.

      Counting sort is suitable for situations where the data volume is large but the data range is small. For example, in the above example, \\(m\\) cannot be too large, otherwise it will occupy too much space. And when \\(n \\ll m\\), counting sort uses \\(O(m)\\) time, which may be slower than \\(O(n \\log n)\\) sorting algorithms.

      "},{"location":"chapter_sorting/heap_sort/","title":"11.7 \u00a0 Heap Sort","text":"

      Tip

      Before reading this section, please ensure you have completed the \"Heap\" chapter.

      Heap sort (heap sort) is an efficient sorting algorithm based on the heap data structure. We can use the \"build heap operation\" and \"element out-heap operation\" that we have already learned to implement heap sort.

      1. Input the array and build a min-heap, at which point the smallest element is at the heap top.
      2. Continuously perform the out-heap operation, record the out-heap elements in sequence, and an ascending sorted sequence can be obtained.

      Although the above method is feasible, it requires an additional array to save the popped elements, which is quite wasteful of space. In practice, we usually use a more elegant implementation method.

      "},{"location":"chapter_sorting/heap_sort/#1171-algorithm-flow","title":"11.7.1 \u00a0 Algorithm Flow","text":"

      Assume the array length is \\(n\\). The flow of heap sort is shown in Figure 11-12.

      1. Input the array and build a max-heap. After completion, the largest element is at the heap top.
      2. Swap the heap top element (first element) with the heap bottom element (last element). After the swap is complete, reduce the heap length by \\(1\\) and increase the count of sorted elements by \\(1\\).
      3. Starting from the heap top element, perform top-to-bottom heapify operation (sift down). After heapify is complete, the heap property is restored.
      4. Loop through steps 2. and 3. After looping \\(n - 1\\) rounds, the array sorting can be completed.

      Tip

      In fact, the element out-heap operation also includes steps 2. and 3., with just an additional step to pop the element.

      <1><2><3><4><5><6><7><8><9><10><11><12>

      Figure 11-12 \u00a0 Heap sort steps

      In the code implementation, we use the same top-to-bottom heapify function sift_down() from the \"Heap\" chapter. It is worth noting that since the heap length will decrease as the largest element is extracted, we need to add a length parameter \\(n\\) to the sift_down() function to specify the current effective length of the heap. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby heap_sort.py
      def sift_down(nums: list[int], n: int, i: int):\n    \"\"\"Heap length is n, start heapifying node i, from top to bottom\"\"\"\n    while True:\n        # Determine the largest node among i, l, r, noted as ma\n        l = 2 * i + 1\n        r = 2 * i + 2\n        ma = i\n        if l < n and nums[l] > nums[ma]:\n            ma = l\n        if r < n and nums[r] > nums[ma]:\n            ma = r\n        # If node i is the largest or indices l, r are out of bounds, no further heapification needed, break\n        if ma == i:\n            break\n        # Swap two nodes\n        nums[i], nums[ma] = nums[ma], nums[i]\n        # Loop downwards heapification\n        i = ma\n\ndef heap_sort(nums: list[int]):\n    \"\"\"Heap sort\"\"\"\n    # Build heap operation: heapify all nodes except leaves\n    for i in range(len(nums) // 2 - 1, -1, -1):\n        sift_down(nums, len(nums), i)\n    # Extract the largest element from the heap and repeat for n-1 rounds\n    for i in range(len(nums) - 1, 0, -1):\n        # Swap the root node with the rightmost leaf node (swap the first element with the last element)\n        nums[0], nums[i] = nums[i], nums[0]\n        # Start heapifying the root node, from top to bottom\n        sift_down(nums, i, 0)\n
      heap_sort.cpp
      /* Heap length is n, start heapifying node i, from top to bottom */\nvoid siftDown(vector<int> &nums, int n, int i) {\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // Swap two nodes\n        if (ma == i) {\n            break;\n        }\n        // Swap two nodes\n        swap(nums[i], nums[ma]);\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n\n/* Heap sort */\nvoid heapSort(vector<int> &nums) {\n    // Build heap operation: heapify all nodes except leaves\n    for (int i = nums.size() / 2 - 1; i >= 0; --i) {\n        siftDown(nums, nums.size(), i);\n    }\n    // Extract the largest element from the heap and repeat for n-1 rounds\n    for (int i = nums.size() - 1; i > 0; --i) {\n        // Delete node\n        swap(nums[0], nums[i]);\n        // Start heapifying the root node, from top to bottom\n        siftDown(nums, i, 0);\n    }\n}\n
      heap_sort.java
      /* Heap length is n, start heapifying node i, from top to bottom */\nvoid siftDown(int[] nums, int n, int i) {\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // Swap two nodes\n        if (ma == i)\n            break;\n        // Swap two nodes\n        int temp = nums[i];\n        nums[i] = nums[ma];\n        nums[ma] = temp;\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n\n/* Heap sort */\nvoid heapSort(int[] nums) {\n    // Build heap operation: heapify all nodes except leaves\n    for (int i = nums.length / 2 - 1; i >= 0; i--) {\n        siftDown(nums, nums.length, i);\n    }\n    // Extract the largest element from the heap and repeat for n-1 rounds\n    for (int i = nums.length - 1; i > 0; i--) {\n        // Delete node\n        int tmp = nums[0];\n        nums[0] = nums[i];\n        nums[i] = tmp;\n        // Start heapifying the root node, from top to bottom\n        siftDown(nums, i, 0);\n    }\n}\n
      heap_sort.cs
      /* Heap length is n, start heapifying node i, from top to bottom */\nvoid SiftDown(int[] nums, int n, int i) {\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // Swap two nodes\n        if (ma == i)\n            break;\n        // Swap two nodes\n        (nums[ma], nums[i]) = (nums[i], nums[ma]);\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n\n/* Heap sort */\nvoid HeapSort(int[] nums) {\n    // Build heap operation: heapify all nodes except leaves\n    for (int i = nums.Length / 2 - 1; i >= 0; i--) {\n        SiftDown(nums, nums.Length, i);\n    }\n    // Extract the largest element from the heap and repeat for n-1 rounds\n    for (int i = nums.Length - 1; i > 0; i--) {\n        // Delete node\n        (nums[i], nums[0]) = (nums[0], nums[i]);\n        // Start heapifying the root node, from top to bottom\n        SiftDown(nums, i, 0);\n    }\n}\n
      heap_sort.go
      /* Heap length is n, start heapifying node i, from top to bottom */\nfunc siftDown(nums *[]int, n, i int) {\n    for true {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        l := 2*i + 1\n        r := 2*i + 2\n        ma := i\n        if l < n && (*nums)[l] > (*nums)[ma] {\n            ma = l\n        }\n        if r < n && (*nums)[r] > (*nums)[ma] {\n            ma = r\n        }\n        // Swap two nodes\n        if ma == i {\n            break\n        }\n        // Swap two nodes\n        (*nums)[i], (*nums)[ma] = (*nums)[ma], (*nums)[i]\n        // Loop downwards heapification\n        i = ma\n    }\n}\n\n/* Heap sort */\nfunc heapSort(nums *[]int) {\n    // Build heap operation: heapify all nodes except leaves\n    for i := len(*nums)/2 - 1; i >= 0; i-- {\n        siftDown(nums, len(*nums), i)\n    }\n    // Extract the largest element from the heap and repeat for n-1 rounds\n    for i := len(*nums) - 1; i > 0; i-- {\n        // Delete node\n        (*nums)[0], (*nums)[i] = (*nums)[i], (*nums)[0]\n        // Start heapifying the root node, from top to bottom\n        siftDown(nums, i, 0)\n    }\n}\n
      heap_sort.swift
      /* Heap length is n, start heapifying node i, from top to bottom */\nfunc siftDown(nums: inout [Int], n: Int, i: Int) {\n    var i = i\n    while true {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        let l = 2 * i + 1\n        let r = 2 * i + 2\n        var ma = i\n        if l < n, nums[l] > nums[ma] {\n            ma = l\n        }\n        if r < n, nums[r] > nums[ma] {\n            ma = r\n        }\n        // Swap two nodes\n        if ma == i {\n            break\n        }\n        // Swap two nodes\n        nums.swapAt(i, ma)\n        // Loop downwards heapification\n        i = ma\n    }\n}\n\n/* Heap sort */\nfunc heapSort(nums: inout [Int]) {\n    // Build heap operation: heapify all nodes except leaves\n    for i in stride(from: nums.count / 2 - 1, through: 0, by: -1) {\n        siftDown(nums: &nums, n: nums.count, i: i)\n    }\n    // Extract the largest element from the heap and repeat for n-1 rounds\n    for i in nums.indices.dropFirst().reversed() {\n        // Delete node\n        nums.swapAt(0, i)\n        // Start heapifying the root node, from top to bottom\n        siftDown(nums: &nums, n: i, i: 0)\n    }\n}\n
      heap_sort.js
      /* Heap length is n, start heapifying node i, from top to bottom */\nfunction siftDown(nums, n, i) {\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        let l = 2 * i + 1;\n        let r = 2 * i + 2;\n        let ma = i;\n        if (l < n && nums[l] > nums[ma]) {\n            ma = l;\n        }\n        if (r < n && nums[r] > nums[ma]) {\n            ma = r;\n        }\n        // Swap two nodes\n        if (ma === i) {\n            break;\n        }\n        // Swap two nodes\n        [nums[i], nums[ma]] = [nums[ma], nums[i]];\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n\n/* Heap sort */\nfunction heapSort(nums) {\n    // Build heap operation: heapify all nodes except leaves\n    for (let i = Math.floor(nums.length / 2) - 1; i >= 0; i--) {\n        siftDown(nums, nums.length, i);\n    }\n    // Extract the largest element from the heap and repeat for n-1 rounds\n    for (let i = nums.length - 1; i > 0; i--) {\n        // Delete node\n        [nums[0], nums[i]] = [nums[i], nums[0]];\n        // Start heapifying the root node, from top to bottom\n        siftDown(nums, i, 0);\n    }\n}\n
      heap_sort.ts
      /* Heap length is n, start heapifying node i, from top to bottom */\nfunction siftDown(nums: number[], n: number, i: number): void {\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        let l = 2 * i + 1;\n        let r = 2 * i + 2;\n        let ma = i;\n        if (l < n && nums[l] > nums[ma]) {\n            ma = l;\n        }\n        if (r < n && nums[r] > nums[ma]) {\n            ma = r;\n        }\n        // Swap two nodes\n        if (ma === i) {\n            break;\n        }\n        // Swap two nodes\n        [nums[i], nums[ma]] = [nums[ma], nums[i]];\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n\n/* Heap sort */\nfunction heapSort(nums: number[]): void {\n    // Build heap operation: heapify all nodes except leaves\n    for (let i = Math.floor(nums.length / 2) - 1; i >= 0; i--) {\n        siftDown(nums, nums.length, i);\n    }\n    // Extract the largest element from the heap and repeat for n-1 rounds\n    for (let i = nums.length - 1; i > 0; i--) {\n        // Delete node\n        [nums[0], nums[i]] = [nums[i], nums[0]];\n        // Start heapifying the root node, from top to bottom\n        siftDown(nums, i, 0);\n    }\n}\n
      heap_sort.dart
      /* Heap length is n, start heapifying node i, from top to bottom */\nvoid siftDown(List<int> nums, int n, int i) {\n  while (true) {\n    // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n    int l = 2 * i + 1;\n    int r = 2 * i + 2;\n    int ma = i;\n    if (l < n && nums[l] > nums[ma]) ma = l;\n    if (r < n && nums[r] > nums[ma]) ma = r;\n    // Swap two nodes\n    if (ma == i) break;\n    // Swap two nodes\n    int temp = nums[i];\n    nums[i] = nums[ma];\n    nums[ma] = temp;\n    // Loop downwards heapification\n    i = ma;\n  }\n}\n\n/* Heap sort */\nvoid heapSort(List<int> nums) {\n  // Build heap operation: heapify all nodes except leaves\n  for (int i = nums.length ~/ 2 - 1; i >= 0; i--) {\n    siftDown(nums, nums.length, i);\n  }\n  // Extract the largest element from the heap and repeat for n-1 rounds\n  for (int i = nums.length - 1; i > 0; i--) {\n    // Delete node\n    int tmp = nums[0];\n    nums[0] = nums[i];\n    nums[i] = tmp;\n    // Start heapifying the root node, from top to bottom\n    siftDown(nums, i, 0);\n  }\n}\n
      heap_sort.rs
      /* Heap length is n, start heapifying node i, from top to bottom */\nfn sift_down(nums: &mut [i32], n: usize, mut i: usize) {\n    loop {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        let l = 2 * i + 1;\n        let r = 2 * i + 2;\n        let mut ma = i;\n        if l < n && nums[l] > nums[ma] {\n            ma = l;\n        }\n        if r < n && nums[r] > nums[ma] {\n            ma = r;\n        }\n        // Swap two nodes\n        if ma == i {\n            break;\n        }\n        // Swap two nodes\n        nums.swap(i, ma);\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n\n/* Heap sort */\nfn heap_sort(nums: &mut [i32]) {\n    // Build heap operation: heapify all nodes except leaves\n    for i in (0..nums.len() / 2).rev() {\n        sift_down(nums, nums.len(), i);\n    }\n    // Extract the largest element from the heap and repeat for n-1 rounds\n    for i in (1..nums.len()).rev() {\n        // Delete node\n        nums.swap(0, i);\n        // Start heapifying the root node, from top to bottom\n        sift_down(nums, i, 0);\n    }\n}\n
      heap_sort.c
      /* Heap length is n, start heapifying node i, from top to bottom */\nvoid siftDown(int nums[], int n, int i) {\n    while (1) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // Swap two nodes\n        if (ma == i) {\n            break;\n        }\n        // Swap two nodes\n        int temp = nums[i];\n        nums[i] = nums[ma];\n        nums[ma] = temp;\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n\n/* Heap sort */\nvoid heapSort(int nums[], int n) {\n    // Build heap operation: heapify all nodes except leaves\n    for (int i = n / 2 - 1; i >= 0; --i) {\n        siftDown(nums, n, i);\n    }\n    // Extract the largest element from the heap and repeat for n-1 rounds\n    for (int i = n - 1; i > 0; --i) {\n        // Delete node\n        int tmp = nums[0];\n        nums[0] = nums[i];\n        nums[i] = tmp;\n        // Start heapifying the root node, from top to bottom\n        siftDown(nums, i, 0);\n    }\n}\n
      heap_sort.kt
      /* Heap length is n, start heapifying node i, from top to bottom */\nfun siftDown(nums: IntArray, n: Int, li: Int) {\n    var i = li\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        val l = 2 * i + 1\n        val r = 2 * i + 2\n        var ma = i\n        if (l < n && nums[l] > nums[ma]) \n            ma = l\n        if (r < n && nums[r] > nums[ma]) \n            ma = r\n        // Swap two nodes\n        if (ma == i) \n            break\n        // Swap two nodes\n        val temp = nums[i]\n        nums[i] = nums[ma]\n        nums[ma] = temp\n        // Loop downwards heapification\n        i = ma\n    }\n}\n\n/* Heap sort */\nfun heapSort(nums: IntArray) {\n    // Build heap operation: heapify all nodes except leaves\n    for (i in nums.size / 2 - 1 downTo 0) {\n        siftDown(nums, nums.size, i)\n    }\n    // Extract the largest element from the heap and repeat for n-1 rounds\n    for (i in nums.size - 1 downTo 1) {\n        // Delete node\n        val temp = nums[0]\n        nums[0] = nums[i]\n        nums[i] = temp\n        // Start heapifying the root node, from top to bottom\n        siftDown(nums, i, 0)\n    }\n}\n
      heap_sort.rb
      ### Heap length is n, heapify from node i, top to bottom ###\ndef sift_down(nums, n, i)\n  while true\n    # If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n    l = 2 * i + 1\n    r = 2 * i + 2\n    ma = i\n    ma = l if l < n && nums[l] > nums[ma]\n    ma = r if r < n && nums[r] > nums[ma]\n    # Swap two nodes\n    break if ma == i\n    # Swap two nodes\n    nums[i], nums[ma] = nums[ma], nums[i]\n    # Loop downwards heapification\n    i = ma\n  end\nend\n\n### Heap sort ###\ndef heap_sort(nums)\n  # Build heap operation: heapify all nodes except leaves\n  (nums.length / 2 - 1).downto(0) do |i|\n    sift_down(nums, nums.length, i)\n  end\n  # Extract the largest element from the heap and repeat for n-1 rounds\n  (nums.length - 1).downto(1) do |i|\n    # Delete node\n    nums[0], nums[i] = nums[i], nums[0]\n    # Start heapifying the root node, from top to bottom\n    sift_down(nums, i, 0)\n  end\nend\n
      "},{"location":"chapter_sorting/heap_sort/#1172-algorithm-characteristics","title":"11.7.2 \u00a0 Algorithm Characteristics","text":"
      • Time complexity of \\(O(n \\log n)\\), non-adaptive sorting: The build heap operation uses \\(O(n)\\) time. Extracting the largest element from the heap has a time complexity of \\(O(\\log n)\\), looping a total of \\(n - 1\\) rounds.
      • Space complexity of \\(O(1)\\), in-place sorting: A few pointer variables use \\(O(1)\\) space. Element swapping and heapify operations are both performed on the original array.
      • Non-stable sorting: When swapping the heap top element and heap bottom element, the relative positions of equal elements may change.
      "},{"location":"chapter_sorting/insertion_sort/","title":"11.4 \u00a0 Insertion Sort","text":"

      Insertion sort (insertion sort) is a simple sorting algorithm that works very similarly to the process of manually organizing a deck of cards.

      Specifically, we select a base element from the unsorted interval, compare the element with elements in the sorted interval to its left one by one, and insert the element into the correct position.

      Figure 11-6 shows the operation flow of inserting an element into the array. Let the base element be base. We need to move all elements from the target index to base one position to the right, and then assign base to the target index.

      Figure 11-6 \u00a0 Single insertion operation

      "},{"location":"chapter_sorting/insertion_sort/#1141-algorithm-flow","title":"11.4.1 \u00a0 Algorithm Flow","text":"

      The overall flow of insertion sort is shown in Figure 11-7.

      1. Initially, the first element of the array has completed sorting.
      2. Select the second element of the array as base, and after inserting it into the correct position, the first 2 elements of the array are sorted.
      3. Select the third element as base, and after inserting it into the correct position, the first 3 elements of the array are sorted.
      4. And so on. In the last round, select the last element as base, and after inserting it into the correct position, all elements are sorted.

      Figure 11-7 \u00a0 Insertion sort flow

      Example code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby insertion_sort.py
      def insertion_sort(nums: list[int]):\n    \"\"\"Insertion sort\"\"\"\n    # Outer loop: sorted interval is [0, i-1]\n    for i in range(1, len(nums)):\n        base = nums[i]\n        j = i - 1\n        # Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n        while j >= 0 and nums[j] > base:\n            nums[j + 1] = nums[j]  # Move nums[j] to the right by one position\n            j -= 1\n        nums[j + 1] = base  # Assign base to the correct position\n
      insertion_sort.cpp
      /* Insertion sort */\nvoid insertionSort(vector<int> &nums) {\n    // Outer loop: sorted interval is [0, i-1]\n    for (int i = 1; i < nums.size(); i++) {\n        int base = nums[i], j = i - 1;\n        // Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // Move nums[j] to the right by one position\n            j--;\n        }\n        nums[j + 1] = base; // Assign base to the correct position\n    }\n}\n
      insertion_sort.java
      /* Insertion sort */\nvoid insertionSort(int[] nums) {\n    // Outer loop: sorted interval is [0, i-1]\n    for (int i = 1; i < nums.length; i++) {\n        int base = nums[i], j = i - 1;\n        // Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // Move nums[j] to the right by one position\n            j--;\n        }\n        nums[j + 1] = base;        // Assign base to the correct position\n    }\n}\n
      insertion_sort.cs
      /* Insertion sort */\nvoid InsertionSort(int[] nums) {\n    // Outer loop: sorted interval is [0, i-1]\n    for (int i = 1; i < nums.Length; i++) {\n        int bas = nums[i], j = i - 1;\n        // Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n        while (j >= 0 && nums[j] > bas) {\n            nums[j + 1] = nums[j]; // Move nums[j] to the right by one position\n            j--;\n        }\n        nums[j + 1] = bas;         // Assign base to the correct position\n    }\n}\n
      insertion_sort.go
      /* Insertion sort */\nfunc insertionSort(nums []int) {\n    // Outer loop: sorted interval is [0, i-1]\n    for i := 1; i < len(nums); i++ {\n        base := nums[i]\n        j := i - 1\n        // Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n        for j >= 0 && nums[j] > base {\n            nums[j+1] = nums[j] // Move nums[j] to the right by one position\n            j--\n        }\n        nums[j+1] = base // Assign base to the correct position\n    }\n}\n
      insertion_sort.swift
      /* Insertion sort */\nfunc insertionSort(nums: inout [Int]) {\n    // Outer loop: sorted interval is [0, i-1]\n    for i in nums.indices.dropFirst() {\n        let base = nums[i]\n        var j = i - 1\n        // Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n        while j >= 0, nums[j] > base {\n            nums[j + 1] = nums[j] // Move nums[j] to the right by one position\n            j -= 1\n        }\n        nums[j + 1] = base // Assign base to the correct position\n    }\n}\n
      insertion_sort.js
      /* Insertion sort */\nfunction insertionSort(nums) {\n    // Outer loop: sorted interval is [0, i-1]\n    for (let i = 1; i < nums.length; i++) {\n        let base = nums[i],\n            j = i - 1;\n        // Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // Move nums[j] to the right by one position\n            j--;\n        }\n        nums[j + 1] = base; // Assign base to the correct position\n    }\n}\n
      insertion_sort.ts
      /* Insertion sort */\nfunction insertionSort(nums: number[]): void {\n    // Outer loop: sorted interval is [0, i-1]\n    for (let i = 1; i < nums.length; i++) {\n        const base = nums[i];\n        let j = i - 1;\n        // Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // Move nums[j] to the right by one position\n            j--;\n        }\n        nums[j + 1] = base; // Assign base to the correct position\n    }\n}\n
      insertion_sort.dart
      /* Insertion sort */\nvoid insertionSort(List<int> nums) {\n  // Outer loop: sorted interval is [0, i-1]\n  for (int i = 1; i < nums.length; i++) {\n    int base = nums[i], j = i - 1;\n    // Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n    while (j >= 0 && nums[j] > base) {\n      nums[j + 1] = nums[j]; // Move nums[j] to the right by one position\n      j--;\n    }\n    nums[j + 1] = base; // Assign base to the correct position\n  }\n}\n
      insertion_sort.rs
      /* Insertion sort */\nfn insertion_sort(nums: &mut [i32]) {\n    // Outer loop: sorted interval is [0, i-1]\n    for i in 1..nums.len() {\n        let (base, mut j) = (nums[i], (i - 1) as i32);\n        // Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n        while j >= 0 && nums[j as usize] > base {\n            nums[(j + 1) as usize] = nums[j as usize]; // Move nums[j] to the right by one position\n            j -= 1;\n        }\n        nums[(j + 1) as usize] = base; // Assign base to the correct position\n    }\n}\n
      insertion_sort.c
      /* Insertion sort */\nvoid insertionSort(int nums[], int size) {\n    // Outer loop: sorted interval is [0, i-1]\n    for (int i = 1; i < size; i++) {\n        int base = nums[i], j = i - 1;\n        // Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n        while (j >= 0 && nums[j] > base) {\n            // Move nums[j] to the right by one position\n            nums[j + 1] = nums[j];\n            j--;\n        }\n        // Assign base to the correct position\n        nums[j + 1] = base;\n    }\n}\n
      insertion_sort.kt
      /* Insertion sort */\nfun insertionSort(nums: IntArray) {\n    // Outer loop: sorted elements are 1, 2, ..., n\n    for (i in nums.indices) {\n        val base = nums[i]\n        var j = i - 1\n        // Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j] // Move nums[j] to the right by one position\n            j--\n        }\n        nums[j + 1] = base        // Assign base to the correct position\n    }\n}\n
      insertion_sort.rb
      ### Insertion sort ###\ndef insertion_sort(nums)\n  n = nums.length\n  # Outer loop: sorted interval is [0, i-1]\n  for i in 1...n\n    base = nums[i]\n    j = i - 1\n    # Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n    while j >= 0 && nums[j] > base\n      nums[j + 1] = nums[j] # Move nums[j] to the right by one position\n      j -= 1\n    end\n    nums[j + 1] = base # Assign base to the correct position\n  end\nend\n
      "},{"location":"chapter_sorting/insertion_sort/#1142-algorithm-characteristics","title":"11.4.2 \u00a0 Algorithm Characteristics","text":"
      • Time complexity of \\(O(n^2)\\), adaptive sorting: In the worst case, each insertion operation requires loops of \\(n - 1\\), \\(n-2\\), \\(\\dots\\), \\(2\\), \\(1\\), summing to \\((n - 1) n / 2\\), so the time complexity is \\(O(n^2)\\). When encountering ordered data, the insertion operation will terminate early. When the input array is completely ordered, insertion sort achieves the best-case time complexity of \\(O(n)\\).
      • Space complexity of \\(O(1)\\), in-place sorting: Pointers \\(i\\) and \\(j\\) use a constant amount of extra space.
      • Stable sorting: During the insertion operation process, we insert elements to the right of equal elements, without changing their order.
      "},{"location":"chapter_sorting/insertion_sort/#1143-advantages-of-insertion-sort","title":"11.4.3 \u00a0 Advantages of Insertion Sort","text":"

      The time complexity of insertion sort is \\(O(n^2)\\), while the time complexity of quick sort, which we will learn about next, is \\(O(n \\log n)\\). Although insertion sort has a higher time complexity, insertion sort is usually faster for smaller data volumes.

      This conclusion is similar to the applicable situations of linear search and binary search. Algorithms like quick sort with \\(O(n \\log n)\\) complexity are sorting algorithms based on divide-and-conquer strategy and often contain more unit computation operations. When the data volume is small, \\(n^2\\) and \\(n \\log n\\) are numerically close, and complexity does not dominate; the number of unit operations per round plays a decisive role.

      In fact, the built-in sorting functions in many programming languages (such as Java) adopt insertion sort. The general approach is: for long arrays, use sorting algorithms based on divide-and-conquer strategy, such as quick sort; for short arrays, directly use insertion sort.

      Although bubble sort, selection sort, and insertion sort all have a time complexity of \\(O(n^2)\\), in actual situations, insertion sort is used significantly more frequently than bubble sort and selection sort, mainly for the following reasons.

      • Bubble sort is based on element swapping, requiring the use of a temporary variable, involving 3 unit operations; insertion sort is based on element assignment, requiring only 1 unit operation. Therefore, the computational overhead of bubble sort is usually higher than that of insertion sort.
      • Selection sort has a time complexity of \\(O(n^2)\\) in any case. If given a set of partially ordered data, insertion sort is usually more efficient than selection sort.
      • Selection sort is unstable and cannot be applied to multi-level sorting.
      "},{"location":"chapter_sorting/merge_sort/","title":"11.6 \u00a0 Merge Sort","text":"

      Merge sort (merge sort) is a sorting algorithm based on the divide-and-conquer strategy, which includes the \"divide\" and \"merge\" phases shown in Figure 11-10.

      1. Divide phase: Recursively split the array from the midpoint, transforming the sorting problem of a long array into the sorting problems of shorter arrays.
      2. Merge phase: When the sub-array length is 1, terminate the division and start merging, continuously merging two shorter sorted arrays into one longer sorted array until the process is complete.

      Figure 11-10 \u00a0 Divide and merge phases of merge sort

      "},{"location":"chapter_sorting/merge_sort/#1161-algorithm-flow","title":"11.6.1 \u00a0 Algorithm Flow","text":"

      As shown in Figure 11-11, the \"divide phase\" recursively splits the array from the midpoint into two sub-arrays from top to bottom.

      1. Calculate the array midpoint mid, recursively divide the left sub-array (interval [left, mid]) and right sub-array (interval [mid + 1, right]).
      2. Recursively execute step 1. until the sub-array interval length is 1, then terminate.

      The \"merge phase\" merges the left sub-array and right sub-array into a sorted array from bottom to top. Note that merging starts from sub-arrays of length 1, and each sub-array in the merge phase is sorted.

      <1><2><3><4><5><6><7><8><9><10>

      Figure 11-11 \u00a0 Merge sort steps

      It can be observed that the recursive order of merge sort is consistent with the post-order traversal of a binary tree.

      • Post-order traversal: First recursively traverse the left subtree, then recursively traverse the right subtree, and finally process the root node.
      • Merge sort: First recursively process the left sub-array, then recursively process the right sub-array, and finally perform the merge.

      The implementation of merge sort is shown in the code below. Note that the interval to be merged in nums is [left, right], while the corresponding interval in tmp is [0, right - left].

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby merge_sort.py
      def merge(nums: list[int], left: int, mid: int, right: int):\n    \"\"\"Merge left subarray and right subarray\"\"\"\n    # Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n    # Create a temporary array tmp to store the merged results\n    tmp = [0] * (right - left + 1)\n    # Initialize the start indices of the left and right subarrays\n    i, j, k = left, mid + 1, 0\n    # While both subarrays still have elements, compare and copy the smaller element into the temporary array\n    while i <= mid and j <= right:\n        if nums[i] <= nums[j]:\n            tmp[k] = nums[i]\n            i += 1\n        else:\n            tmp[k] = nums[j]\n            j += 1\n        k += 1\n    # Copy the remaining elements of the left and right subarrays into the temporary array\n    while i <= mid:\n        tmp[k] = nums[i]\n        i += 1\n        k += 1\n    while j <= right:\n        tmp[k] = nums[j]\n        j += 1\n        k += 1\n    # Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n    for k in range(0, len(tmp)):\n        nums[left + k] = tmp[k]\n\ndef merge_sort(nums: list[int], left: int, right: int):\n    \"\"\"Merge sort\"\"\"\n    # Termination condition\n    if left >= right:\n        return  # Terminate recursion when subarray length is 1\n    # Divide and conquer stage\n    mid = (left + right) // 2  # Calculate midpoint\n    merge_sort(nums, left, mid)  # Recursively process the left subarray\n    merge_sort(nums, mid + 1, right)  # Recursively process the right subarray\n    # Merge stage\n    merge(nums, left, mid, right)\n
      merge_sort.cpp
      /* Merge left subarray and right subarray */\nvoid merge(vector<int> &nums, int left, int mid, int right) {\n    // Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n    // Create a temporary array tmp to store the merged results\n    vector<int> tmp(right - left + 1);\n    // Initialize the start indices of the left and right subarrays\n    int i = left, j = mid + 1, k = 0;\n    // While both subarrays still have elements, compare and copy the smaller element into the temporary array\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++];\n        else\n            tmp[k++] = nums[j++];\n    }\n    // Copy the remaining elements of the left and right subarrays into the temporary array\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n    for (k = 0; k < tmp.size(); k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* Merge sort */\nvoid mergeSort(vector<int> &nums, int left, int right) {\n    // Termination condition\n    if (left >= right)\n        return; // Terminate recursion when subarray length is 1\n    // Divide and conquer stage\n    int mid = left + (right - left) / 2;    // Calculate midpoint\n    mergeSort(nums, left, mid);      // Recursively process the left subarray\n    mergeSort(nums, mid + 1, right); // Recursively process the right subarray\n    // Merge stage\n    merge(nums, left, mid, right);\n}\n
      merge_sort.java
      /* Merge left subarray and right subarray */\nvoid merge(int[] nums, int left, int mid, int right) {\n    // Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n    // Create a temporary array tmp to store the merged results\n    int[] tmp = new int[right - left + 1];\n    // Initialize the start indices of the left and right subarrays\n    int i = left, j = mid + 1, k = 0;\n    // While both subarrays still have elements, compare and copy the smaller element into the temporary array\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++];\n        else\n            tmp[k++] = nums[j++];\n    }\n    // Copy the remaining elements of the left and right subarrays into the temporary array\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n    for (k = 0; k < tmp.length; k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* Merge sort */\nvoid mergeSort(int[] nums, int left, int right) {\n    // Termination condition\n    if (left >= right)\n        return; // Terminate recursion when subarray length is 1\n    // Divide and conquer stage\n    int mid = left + (right - left) / 2; // Calculate midpoint\n    mergeSort(nums, left, mid); // Recursively process the left subarray\n    mergeSort(nums, mid + 1, right); // Recursively process the right subarray\n    // Merge stage\n    merge(nums, left, mid, right);\n}\n
      merge_sort.cs
      /* Merge left subarray and right subarray */\nvoid Merge(int[] nums, int left, int mid, int right) {\n    // Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n    // Create a temporary array tmp to store the merged results\n    int[] tmp = new int[right - left + 1];\n    // Initialize the start indices of the left and right subarrays\n    int i = left, j = mid + 1, k = 0;\n    // While both subarrays still have elements, compare and copy the smaller element into the temporary array\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++];\n        else\n            tmp[k++] = nums[j++];\n    }\n    // Copy the remaining elements of the left and right subarrays into the temporary array\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n    for (k = 0; k < tmp.Length; ++k) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* Merge sort */\nvoid MergeSort(int[] nums, int left, int right) {\n    // Termination condition\n    if (left >= right) return;       // Terminate recursion when subarray length is 1\n    // Divide and conquer stage\n    int mid = left + (right - left) / 2;    // Calculate midpoint\n    MergeSort(nums, left, mid);      // Recursively process the left subarray\n    MergeSort(nums, mid + 1, right); // Recursively process the right subarray\n    // Merge stage\n    Merge(nums, left, mid, right);\n}\n
      merge_sort.go
      /* Merge left subarray and right subarray */\nfunc merge(nums []int, left, mid, right int) {\n    // Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n    // Create a temporary array tmp to store the merged results\n    tmp := make([]int, right-left+1)\n    // Initialize the start indices of the left and right subarrays\n    i, j, k := left, mid+1, 0\n    // While both subarrays still have elements, compare and copy the smaller element into the temporary array\n    for i <= mid && j <= right {\n        if nums[i] <= nums[j] {\n            tmp[k] = nums[i]\n            i++\n        } else {\n            tmp[k] = nums[j]\n            j++\n        }\n        k++\n    }\n    // Copy the remaining elements of the left and right subarrays into the temporary array\n    for i <= mid {\n        tmp[k] = nums[i]\n        i++\n        k++\n    }\n    for j <= right {\n        tmp[k] = nums[j]\n        j++\n        k++\n    }\n    // Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n    for k := 0; k < len(tmp); k++ {\n        nums[left+k] = tmp[k]\n    }\n}\n\n/* Merge sort */\nfunc mergeSort(nums []int, left, right int) {\n    // Termination condition\n    if left >= right {\n        return\n    }\n    // Divide and conquer stage\n    mid := left + (right - left) / 2\n    mergeSort(nums, left, mid)\n    mergeSort(nums, mid+1, right)\n    // Merge stage\n    merge(nums, left, mid, right)\n}\n
      merge_sort.swift
      /* Merge left subarray and right subarray */\nfunc merge(nums: inout [Int], left: Int, mid: Int, right: Int) {\n    // Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n    // Create a temporary array tmp to store the merged results\n    var tmp = Array(repeating: 0, count: right - left + 1)\n    // Initialize the start indices of the left and right subarrays\n    var i = left, j = mid + 1, k = 0\n    // While both subarrays still have elements, compare and copy the smaller element into the temporary array\n    while i <= mid, j <= right {\n        if nums[i] <= nums[j] {\n            tmp[k] = nums[i]\n            i += 1\n        } else {\n            tmp[k] = nums[j]\n            j += 1\n        }\n        k += 1\n    }\n    // Copy the remaining elements of the left and right subarrays into the temporary array\n    while i <= mid {\n        tmp[k] = nums[i]\n        i += 1\n        k += 1\n    }\n    while j <= right {\n        tmp[k] = nums[j]\n        j += 1\n        k += 1\n    }\n    // Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n    for k in tmp.indices {\n        nums[left + k] = tmp[k]\n    }\n}\n\n/* Merge sort */\nfunc mergeSort(nums: inout [Int], left: Int, right: Int) {\n    // Termination condition\n    if left >= right { // Terminate recursion when subarray length is 1\n        return\n    }\n    // Divide and conquer stage\n    let mid = left + (right - left) / 2 // Calculate midpoint\n    mergeSort(nums: &nums, left: left, right: mid) // Recursively process the left subarray\n    mergeSort(nums: &nums, left: mid + 1, right: right) // Recursively process the right subarray\n    // Merge stage\n    merge(nums: &nums, left: left, mid: mid, right: right)\n}\n
      merge_sort.js
      /* Merge left subarray and right subarray */\nfunction merge(nums, left, mid, right) {\n    // Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n    // Create a temporary array tmp to store the merged results\n    const tmp = new Array(right - left + 1);\n    // Initialize the start indices of the left and right subarrays\n    let i = left,\n        j = mid + 1,\n        k = 0;\n    // While both subarrays still have elements, compare and copy the smaller element into the temporary array\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j]) {\n            tmp[k++] = nums[i++];\n        } else {\n            tmp[k++] = nums[j++];\n        }\n    }\n    // Copy the remaining elements of the left and right subarrays into the temporary array\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n    for (k = 0; k < tmp.length; k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* Merge sort */\nfunction mergeSort(nums, left, right) {\n    // Termination condition\n    if (left >= right) return; // Terminate recursion when subarray length is 1\n    // Divide and conquer stage\n    let mid = Math.floor(left + (right - left) / 2); // Calculate midpoint\n    mergeSort(nums, left, mid); // Recursively process the left subarray\n    mergeSort(nums, mid + 1, right); // Recursively process the right subarray\n    // Merge stage\n    merge(nums, left, mid, right);\n}\n
      merge_sort.ts
      /* Merge left subarray and right subarray */\nfunction merge(nums: number[], left: number, mid: number, right: number): void {\n    // Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n    // Create a temporary array tmp to store the merged results\n    const tmp = new Array(right - left + 1);\n    // Initialize the start indices of the left and right subarrays\n    let i = left,\n        j = mid + 1,\n        k = 0;\n    // While both subarrays still have elements, compare and copy the smaller element into the temporary array\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j]) {\n            tmp[k++] = nums[i++];\n        } else {\n            tmp[k++] = nums[j++];\n        }\n    }\n    // Copy the remaining elements of the left and right subarrays into the temporary array\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n    for (k = 0; k < tmp.length; k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* Merge sort */\nfunction mergeSort(nums: number[], left: number, right: number): void {\n    // Termination condition\n    if (left >= right) return; // Terminate recursion when subarray length is 1\n    // Divide and conquer stage\n    let mid = Math.floor(left + (right - left) / 2); // Calculate midpoint\n    mergeSort(nums, left, mid); // Recursively process the left subarray\n    mergeSort(nums, mid + 1, right); // Recursively process the right subarray\n    // Merge stage\n    merge(nums, left, mid, right);\n}\n
      merge_sort.dart
      /* Merge left subarray and right subarray */\nvoid merge(List<int> nums, int left, int mid, int right) {\n  // Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n  // Create a temporary array tmp to store the merged results\n  List<int> tmp = List.filled(right - left + 1, 0);\n  // Initialize the start indices of the left and right subarrays\n  int i = left, j = mid + 1, k = 0;\n  // While both subarrays still have elements, compare and copy the smaller element into the temporary array\n  while (i <= mid && j <= right) {\n    if (nums[i] <= nums[j])\n      tmp[k++] = nums[i++];\n    else\n      tmp[k++] = nums[j++];\n  }\n  // Copy the remaining elements of the left and right subarrays into the temporary array\n  while (i <= mid) {\n    tmp[k++] = nums[i++];\n  }\n  while (j <= right) {\n    tmp[k++] = nums[j++];\n  }\n  // Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n  for (k = 0; k < tmp.length; k++) {\n    nums[left + k] = tmp[k];\n  }\n}\n\n/* Merge sort */\nvoid mergeSort(List<int> nums, int left, int right) {\n  // Termination condition\n  if (left >= right) return; // Terminate recursion when subarray length is 1\n  // Divide and conquer stage\n  int mid = left + (right - left) ~/ 2; // Calculate midpoint\n  mergeSort(nums, left, mid); // Recursively process the left subarray\n  mergeSort(nums, mid + 1, right); // Recursively process the right subarray\n  // Merge stage\n  merge(nums, left, mid, right);\n}\n
      merge_sort.rs
      /* Merge left subarray and right subarray */\nfn merge(nums: &mut [i32], left: usize, mid: usize, right: usize) {\n    // Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n    // Create a temporary array tmp to store the merged results\n    let tmp_size = right - left + 1;\n    let mut tmp = vec![0; tmp_size];\n    // Initialize the start indices of the left and right subarrays\n    let (mut i, mut j, mut k) = (left, mid + 1, 0);\n    // While both subarrays still have elements, compare and copy the smaller element into the temporary array\n    while i <= mid && j <= right {\n        if nums[i] <= nums[j] {\n            tmp[k] = nums[i];\n            i += 1;\n        } else {\n            tmp[k] = nums[j];\n            j += 1;\n        }\n        k += 1;\n    }\n    // Copy the remaining elements of the left and right subarrays into the temporary array\n    while i <= mid {\n        tmp[k] = nums[i];\n        k += 1;\n        i += 1;\n    }\n    while j <= right {\n        tmp[k] = nums[j];\n        k += 1;\n        j += 1;\n    }\n    // Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n    for k in 0..tmp_size {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* Merge sort */\nfn merge_sort(nums: &mut [i32], left: usize, right: usize) {\n    // Termination condition\n    if left >= right {\n        return; // Terminate recursion when subarray length is 1\n    }\n\n    // Divide and conquer stage\n    let mid = left + (right - left) / 2; // Calculate midpoint\n    merge_sort(nums, left, mid); // Recursively process the left subarray\n    merge_sort(nums, mid + 1, right); // Recursively process the right subarray\n\n    // Merge stage\n    merge(nums, left, mid, right);\n}\n
      merge_sort.c
      /* Merge left subarray and right subarray */\nvoid merge(int *nums, int left, int mid, int right) {\n    // Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n    // Create a temporary array tmp to store the merged results\n    int tmpSize = right - left + 1;\n    int *tmp = (int *)malloc(tmpSize * sizeof(int));\n    // Initialize the start indices of the left and right subarrays\n    int i = left, j = mid + 1, k = 0;\n    // While both subarrays still have elements, compare and copy the smaller element into the temporary array\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j]) {\n            tmp[k++] = nums[i++];\n        } else {\n            tmp[k++] = nums[j++];\n        }\n    }\n    // Copy the remaining elements of the left and right subarrays into the temporary array\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n    for (k = 0; k < tmpSize; ++k) {\n        nums[left + k] = tmp[k];\n    }\n    // Free memory\n    free(tmp);\n}\n\n/* Merge sort */\nvoid mergeSort(int *nums, int left, int right) {\n    // Termination condition\n    if (left >= right)\n        return; // Terminate recursion when subarray length is 1\n    // Divide and conquer stage\n    int mid = left + (right - left) / 2;    // Calculate midpoint\n    mergeSort(nums, left, mid);      // Recursively process the left subarray\n    mergeSort(nums, mid + 1, right); // Recursively process the right subarray\n    // Merge stage\n    merge(nums, left, mid, right);\n}\n
      merge_sort.kt
      /* Merge left subarray and right subarray */\nfun merge(nums: IntArray, left: Int, mid: Int, right: Int) {\n    // Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n    // Create a temporary array tmp to store the merged results\n    val tmp = IntArray(right - left + 1)\n    // Initialize the start indices of the left and right subarrays\n    var i = left\n    var j = mid + 1\n    var k = 0\n    // While both subarrays still have elements, compare and copy the smaller element into the temporary array\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++]\n        else\n            tmp[k++] = nums[j++]\n    }\n    // Copy the remaining elements of the left and right subarrays into the temporary array\n    while (i <= mid) {\n        tmp[k++] = nums[i++]\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++]\n    }\n    // Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n    for (l in tmp.indices) {\n        nums[left + l] = tmp[l]\n    }\n}\n\n/* Merge sort */\nfun mergeSort(nums: IntArray, left: Int, right: Int) {\n    // Termination condition\n    if (left >= right) return  // Terminate recursion when subarray length is 1\n    // Divide and conquer stage\n    val mid = left + (right - left) / 2 // Calculate midpoint\n    mergeSort(nums, left, mid) // Recursively process the left subarray\n    mergeSort(nums, mid + 1, right) // Recursively process the right subarray\n    // Merge stage\n    merge(nums, left, mid, right)\n}\n
      merge_sort.rb
      ### Merge left and right subarrays ###\ndef merge(nums, left, mid, right)\n  # Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n  # Create temporary array tmp to store merged result\n  tmp = Array.new(right - left + 1, 0)\n  # Initialize the start indices of the left and right subarrays\n  i, j, k = left, mid + 1, 0\n  # While both subarrays still have elements, compare and copy the smaller element into the temporary array\n  while i <= mid && j <= right\n    if nums[i] <= nums[j]\n      tmp[k] = nums[i]\n      i += 1\n    else\n      tmp[k] = nums[j]\n      j += 1\n    end\n    k += 1\n  end\n  # Copy the remaining elements of the left and right subarrays into the temporary array\n  while i <= mid\n    tmp[k] = nums[i]\n    i += 1\n    k += 1\n  end\n  while j <= right\n    tmp[k] = nums[j]\n    j += 1\n    k += 1\n  end\n  # Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n  (0...tmp.length).each do |k|\n    nums[left + k] = tmp[k]\n  end\nend\n\n### Merge sort ###\ndef merge_sort(nums, left, right)\n  # Termination condition\n  # Terminate recursion when subarray length is 1\n  return if left >= right\n  # Divide and conquer stage\n  mid = left + (right - left) / 2 # Calculate midpoint\n  merge_sort(nums, left, mid) # Recursively process the left subarray\n  merge_sort(nums, mid + 1, right) # Recursively process the right subarray\n  # Merge stage\n  merge(nums, left, mid, right)\nend\n
      "},{"location":"chapter_sorting/merge_sort/#1162-algorithm-characteristics","title":"11.6.2 \u00a0 Algorithm Characteristics","text":"
      • Time complexity of \\(O(n \\log n)\\), non-adaptive sorting: The division produces a recursion tree of height \\(\\log n\\), and the total number of merge operations at each level is \\(n\\), so the overall time complexity is \\(O(n \\log n)\\).
      • Space complexity of \\(O(n)\\), non-in-place sorting: The recursion depth is \\(\\log n\\), using \\(O(\\log n)\\) size of stack frame space. The merge operation requires the aid of an auxiliary array, using \\(O(n)\\) size of additional space.
      • Stable sorting: In the merge process, the order of equal elements remains unchanged.
      "},{"location":"chapter_sorting/merge_sort/#1163-linked-list-sorting","title":"11.6.3 \u00a0 Linked List Sorting","text":"

      For linked lists, merge sort has significant advantages over other sorting algorithms, and can optimize the space complexity of linked list sorting tasks to \\(O(1)\\).

      • Divide phase: \"Iteration\" can be used instead of \"recursion\" to implement linked list division work, thus saving the stack frame space used by recursion.
      • Merge phase: In linked lists, node insertion and deletion operations can be achieved by just changing references (pointers), so there is no need to create additional linked lists during the merge phase (merging two short ordered linked lists into one long ordered linked list).

      The specific implementation details are quite complex, and interested readers can consult related materials for learning.

      "},{"location":"chapter_sorting/quick_sort/","title":"11.5 \u00a0 Quick Sort","text":"

      Quick sort (quick sort) is a sorting algorithm based on the divide-and-conquer strategy, which operates efficiently and is widely applied.

      The core operation of quick sort is \"sentinel partitioning\", which aims to: select a certain element in the array as the \"pivot\", move all elements smaller than the pivot to its left, and move elements larger than the pivot to its right. Specifically, the flow of sentinel partitioning is shown in Figure 11-8.

      1. Select the leftmost element of the array as the pivot, and initialize two pointers i and j pointing to the two ends of the array.
      2. Set up a loop in which i (j) is used in each round to find the first element larger (smaller) than the pivot, and then swap these two elements.
      3. Loop through step 2. until i and j meet, and finally swap the pivot to the boundary line of the two sub-arrays.
      <1><2><3><4><5><6><7><8><9>

      Figure 11-8 \u00a0 Sentinel partitioning steps

      After sentinel partitioning is complete, the original array is divided into three parts: left sub-array, pivot, right sub-array, satisfying \"any element in left sub-array \\(\\leq\\) pivot \\(\\leq\\) any element in right sub-array\". Therefore, we next only need to sort these two sub-arrays.

      Divide-and-conquer strategy of quick sort

      The essence of sentinel partitioning is to simplify the sorting problem of a longer array into the sorting problems of two shorter arrays.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
      def partition(self, nums: list[int], left: int, right: int) -> int:\n    \"\"\"Sentinel partition\"\"\"\n    # Use nums[left] as the pivot\n    i, j = left, right\n    while i < j:\n        while i < j and nums[j] >= nums[left]:\n            j -= 1  # Search from right to left for the first element smaller than the pivot\n        while i < j and nums[i] <= nums[left]:\n            i += 1  # Search from left to right for the first element greater than the pivot\n        # Swap elements\n        nums[i], nums[j] = nums[j], nums[i]\n    # Swap the pivot to the boundary between the two subarrays\n    nums[i], nums[left] = nums[left], nums[i]\n    return i  # Return the index of the pivot\n
      quick_sort.cpp
      /* Sentinel partition */\nint partition(vector<int> &nums, int left, int right) {\n    // Use nums[left] as the pivot\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;                // Search from right to left for the first element smaller than the pivot\n        while (i < j && nums[i] <= nums[left])\n            i++;                // Search from left to right for the first element greater than the pivot\n        swap(nums[i], nums[j]); // Swap these two elements\n    }\n    swap(nums[i], nums[left]);  // Swap the pivot to the boundary between the two subarrays\n    return i;                   // Return the index of the pivot\n}\n
      quick_sort.java
      /* Swap elements */\nvoid swap(int[] nums, int i, int j) {\n    int tmp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = tmp;\n}\n\n/* Sentinel partition */\nint partition(int[] nums, int left, int right) {\n    // Use nums[left] as the pivot\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // Search from right to left for the first element smaller than the pivot\n        while (i < j && nums[i] <= nums[left])\n            i++;          // Search from left to right for the first element greater than the pivot\n        swap(nums, i, j); // Swap these two elements\n    }\n    swap(nums, i, left);  // Swap the pivot to the boundary between the two subarrays\n    return i;             // Return the index of the pivot\n}\n
      quick_sort.cs
      /* Swap elements */\nvoid Swap(int[] nums, int i, int j) {\n    (nums[j], nums[i]) = (nums[i], nums[j]);\n}\n\n/* Sentinel partition */\nint Partition(int[] nums, int left, int right) {\n    // Use nums[left] as the pivot\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // Search from right to left for the first element smaller than the pivot\n        while (i < j && nums[i] <= nums[left])\n            i++;          // Search from left to right for the first element greater than the pivot\n        Swap(nums, i, j); // Swap these two elements\n    }\n    Swap(nums, i, left);  // Swap the pivot to the boundary between the two subarrays\n    return i;             // Return the index of the pivot\n}\n
      quick_sort.go
      /* Sentinel partition */\nfunc (q *quickSort) partition(nums []int, left, right int) int {\n    // Use nums[left] as the pivot\n    i, j := left, right\n    for i < j {\n        for i < j && nums[j] >= nums[left] {\n            j-- // Search from right to left for the first element smaller than the pivot\n        }\n        for i < j && nums[i] <= nums[left] {\n            i++ // Search from left to right for the first element greater than the pivot\n        }\n        // Swap elements\n        nums[i], nums[j] = nums[j], nums[i]\n    }\n    // Swap the pivot to the boundary between the two subarrays\n    nums[i], nums[left] = nums[left], nums[i]\n    return i // Return the index of the pivot\n}\n
      quick_sort.swift
      /* Sentinel partition */\nfunc partition(nums: inout [Int], left: Int, right: Int) -> Int {\n    // Use nums[left] as the pivot\n    var i = left\n    var j = right\n    while i < j {\n        while i < j, nums[j] >= nums[left] {\n            j -= 1 // Search from right to left for the first element smaller than the pivot\n        }\n        while i < j, nums[i] <= nums[left] {\n            i += 1 // Search from left to right for the first element greater than the pivot\n        }\n        nums.swapAt(i, j) // Swap these two elements\n    }\n    nums.swapAt(i, left) // Swap the pivot to the boundary between the two subarrays\n    return i // Return the index of the pivot\n}\n
      quick_sort.js
      /* Swap elements */\nswap(nums, i, j) {\n    let tmp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = tmp;\n}\n\n/* Sentinel partition */\npartition(nums, left, right) {\n    // Use nums[left] as the pivot\n    let i = left,\n        j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) {\n            j -= 1; // Search from right to left for the first element smaller than the pivot\n        }\n        while (i < j && nums[i] <= nums[left]) {\n            i += 1; // Search from left to right for the first element greater than the pivot\n        }\n        // Swap elements\n        this.swap(nums, i, j); // Swap these two elements\n    }\n    this.swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays\n    return i; // Return the index of the pivot\n}\n
      quick_sort.ts
      /* Swap elements */\nswap(nums: number[], i: number, j: number): void {\n    let tmp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = tmp;\n}\n\n/* Sentinel partition */\npartition(nums: number[], left: number, right: number): number {\n    // Use nums[left] as the pivot\n    let i = left,\n        j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) {\n            j -= 1; // Search from right to left for the first element smaller than the pivot\n        }\n        while (i < j && nums[i] <= nums[left]) {\n            i += 1; // Search from left to right for the first element greater than the pivot\n        }\n        // Swap elements\n        this.swap(nums, i, j); // Swap these two elements\n    }\n    this.swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays\n    return i; // Return the index of the pivot\n}\n
      quick_sort.dart
      /* Swap elements */\nvoid _swap(List<int> nums, int i, int j) {\n  int tmp = nums[i];\n  nums[i] = nums[j];\n  nums[j] = tmp;\n}\n\n/* Sentinel partition */\nint _partition(List<int> nums, int left, int right) {\n  // Use nums[left] as the pivot\n  int i = left, j = right;\n  while (i < j) {\n    while (i < j && nums[j] >= nums[left]) j--; // Search from right to left for the first element smaller than the pivot\n    while (i < j && nums[i] <= nums[left]) i++; // Search from left to right for the first element greater than the pivot\n    _swap(nums, i, j); // Swap these two elements\n  }\n  _swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays\n  return i; // Return the index of the pivot\n}\n
      quick_sort.rs
      /* Sentinel partition */\nfn partition(nums: &mut [i32], left: usize, right: usize) -> usize {\n    // Use nums[left] as the pivot\n    let (mut i, mut j) = (left, right);\n    while i < j {\n        while i < j && nums[j] >= nums[left] {\n            j -= 1; // Search from right to left for the first element smaller than the pivot\n        }\n        while i < j && nums[i] <= nums[left] {\n            i += 1; // Search from left to right for the first element greater than the pivot\n        }\n        nums.swap(i, j); // Swap these two elements\n    }\n    nums.swap(i, left); // Swap the pivot to the boundary between the two subarrays\n    i // Return the index of the pivot\n}\n
      quick_sort.c
      /* Swap elements */\nvoid swap(int nums[], int i, int j) {\n    int tmp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = tmp;\n}\n\n/* Sentinel partition */\nint partition(int nums[], int left, int right) {\n    // Use nums[left] as the pivot\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) {\n            j--; // Search from right to left for the first element smaller than the pivot\n        }\n        while (i < j && nums[i] <= nums[left]) {\n            i++; // Search from left to right for the first element greater than the pivot\n        }\n        // Swap these two elements\n        swap(nums, i, j);\n    }\n    // Swap the pivot to the boundary between the two subarrays\n    swap(nums, i, left);\n    // Return the index of the pivot\n    return i;\n}\n
      quick_sort.kt
      /* Swap elements */\nfun swap(nums: IntArray, i: Int, j: Int) {\n    val temp = nums[i]\n    nums[i] = nums[j]\n    nums[j] = temp\n}\n\n/* Sentinel partition */\nfun partition(nums: IntArray, left: Int, right: Int): Int {\n    // Use nums[left] as the pivot\n    var i = left\n    var j = right\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--           // Search from right to left for the first element smaller than the pivot\n        while (i < j && nums[i] <= nums[left])\n            i++           // Search from left to right for the first element greater than the pivot\n        swap(nums, i, j)  // Swap these two elements\n    }\n    swap(nums, i, left)   // Swap the pivot to the boundary between the two subarrays\n    return i              // Return the index of the pivot\n}\n
      quick_sort.rb
      ### Sentinel partition ###\ndef partition(nums, left, right)\n  # Use nums[left] as the pivot\n  i, j = left, right\n  while i < j\n    while i < j && nums[j] >= nums[left]\n      j -= 1 # Search from right to left for the first element smaller than the pivot\n    end\n    while i < j && nums[i] <= nums[left]\n      i += 1 # Search from left to right for the first element greater than the pivot\n    end\n    # Swap elements\n    nums[i], nums[j] = nums[j], nums[i]\n  end\n  # Swap the pivot to the boundary between the two subarrays\n  nums[i], nums[left] = nums[left], nums[i]\n  i # Return the index of the pivot\nend\n
      "},{"location":"chapter_sorting/quick_sort/#1151-algorithm-flow","title":"11.5.1 \u00a0 Algorithm Flow","text":"

      The overall flow of quick sort is shown in Figure 11-9.

      1. First, perform one \"sentinel partitioning\" on the original array to obtain the unsorted left sub-array and right sub-array.
      2. Then, recursively perform \"sentinel partitioning\" on the left sub-array and right sub-array respectively.
      3. Continue recursively until the sub-array length is 1, at which point sorting of the entire array is complete.

      Figure 11-9 \u00a0 Quick sort flow

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
      def quick_sort(self, nums: list[int], left: int, right: int):\n    \"\"\"Quick sort\"\"\"\n    # Terminate recursion when subarray length is 1\n    if left >= right:\n        return\n    # Sentinel partition\n    pivot = self.partition(nums, left, right)\n    # Recursively process the left subarray and right subarray\n    self.quick_sort(nums, left, pivot - 1)\n    self.quick_sort(nums, pivot + 1, right)\n
      quick_sort.cpp
      /* Quick sort */\nvoid quickSort(vector<int> &nums, int left, int right) {\n    // Terminate recursion when subarray length is 1\n    if (left >= right)\n        return;\n    // Sentinel partition\n    int pivot = partition(nums, left, right);\n    // Recursively process the left subarray and right subarray\n    quickSort(nums, left, pivot - 1);\n    quickSort(nums, pivot + 1, right);\n}\n
      quick_sort.java
      /* Quick sort */\nvoid quickSort(int[] nums, int left, int right) {\n    // Terminate recursion when subarray length is 1\n    if (left >= right)\n        return;\n    // Sentinel partition\n    int pivot = partition(nums, left, right);\n    // Recursively process the left subarray and right subarray\n    quickSort(nums, left, pivot - 1);\n    quickSort(nums, pivot + 1, right);\n}\n
      quick_sort.cs
      /* Quick sort */\nvoid QuickSort(int[] nums, int left, int right) {\n    // Terminate recursion when subarray length is 1\n    if (left >= right)\n        return;\n    // Sentinel partition\n    int pivot = Partition(nums, left, right);\n    // Recursively process the left subarray and right subarray\n    QuickSort(nums, left, pivot - 1);\n    QuickSort(nums, pivot + 1, right);\n}\n
      quick_sort.go
      /* Quick sort */\nfunc (q *quickSort) quickSort(nums []int, left, right int) {\n    // Terminate recursion when subarray length is 1\n    if left >= right {\n        return\n    }\n    // Sentinel partition\n    pivot := q.partition(nums, left, right)\n    // Recursively process the left subarray and right subarray\n    q.quickSort(nums, left, pivot-1)\n    q.quickSort(nums, pivot+1, right)\n}\n
      quick_sort.swift
      /* Quick sort */\nfunc quickSort(nums: inout [Int], left: Int, right: Int) {\n    // Terminate recursion when subarray length is 1\n    if left >= right {\n        return\n    }\n    // Sentinel partition\n    let pivot = partition(nums: &nums, left: left, right: right)\n    // Recursively process the left subarray and right subarray\n    quickSort(nums: &nums, left: left, right: pivot - 1)\n    quickSort(nums: &nums, left: pivot + 1, right: right)\n}\n
      quick_sort.js
      /* Quick sort */\nquickSort(nums, left, right) {\n    // Terminate recursion when subarray length is 1\n    if (left >= right) return;\n    // Sentinel partition\n    const pivot = this.partition(nums, left, right);\n    // Recursively process the left subarray and right subarray\n    this.quickSort(nums, left, pivot - 1);\n    this.quickSort(nums, pivot + 1, right);\n}\n
      quick_sort.ts
      /* Quick sort */\nquickSort(nums: number[], left: number, right: number): void {\n    // Terminate recursion when subarray length is 1\n    if (left >= right) {\n        return;\n    }\n    // Sentinel partition\n    const pivot = this.partition(nums, left, right);\n    // Recursively process the left subarray and right subarray\n    this.quickSort(nums, left, pivot - 1);\n    this.quickSort(nums, pivot + 1, right);\n}\n
      quick_sort.dart
      /* Quick sort */\nvoid quickSort(List<int> nums, int left, int right) {\n  // Terminate recursion when subarray length is 1\n  if (left >= right) return;\n  // Sentinel partition\n  int pivot = _partition(nums, left, right);\n  // Recursively process the left subarray and right subarray\n  quickSort(nums, left, pivot - 1);\n  quickSort(nums, pivot + 1, right);\n}\n
      quick_sort.rs
      /* Quick sort */\npub fn quick_sort(left: i32, right: i32, nums: &mut [i32]) {\n    // Terminate recursion when subarray length is 1\n    if left >= right {\n        return;\n    }\n    // Sentinel partition\n    let pivot = Self::partition(nums, left as usize, right as usize) as i32;\n    // Recursively process the left subarray and right subarray\n    Self::quick_sort(left, pivot - 1, nums);\n    Self::quick_sort(pivot + 1, right, nums);\n}\n
      quick_sort.c
      /* Quick sort */\nvoid quickSort(int nums[], int left, int right) {\n    // Terminate recursion when subarray length is 1\n    if (left >= right) {\n        return;\n    }\n    // Sentinel partition\n    int pivot = partition(nums, left, right);\n    // Recursively process the left subarray and right subarray\n    quickSort(nums, left, pivot - 1);\n    quickSort(nums, pivot + 1, right);\n}\n
      quick_sort.kt
      /* Quick sort */\nfun quickSort(nums: IntArray, left: Int, right: Int) {\n    // Terminate recursion when subarray length is 1\n    if (left >= right) return\n    // Sentinel partition\n    val pivot = partition(nums, left, right)\n    // Recursively process the left subarray and right subarray\n    quickSort(nums, left, pivot - 1)\n    quickSort(nums, pivot + 1, right)\n}\n
      quick_sort.rb
      ### Quick sort class ###\ndef quick_sort(nums, left, right)\n  # Recurse when subarray length is not 1\n  if left < right\n    # Sentinel partition\n    pivot = partition(nums, left, right)\n    # Recursively process the left subarray and right subarray\n    quick_sort(nums, left, pivot - 1)\n    quick_sort(nums, pivot + 1, right)\n  end\n  nums\nend\n
      "},{"location":"chapter_sorting/quick_sort/#1152-algorithm-characteristics","title":"11.5.2 \u00a0 Algorithm Characteristics","text":"
      • Time complexity of \\(O(n \\log n)\\), non-adaptive sorting: In the average case, the number of recursive levels of sentinel partitioning is \\(\\log n\\), and the total number of loops at each level is \\(n\\), using \\(O(n \\log n)\\) time overall. In the worst case, each round of sentinel partitioning divides an array of length \\(n\\) into two sub-arrays of length \\(0\\) and \\(n - 1\\), at which point the number of recursive levels reaches \\(n\\), the number of loops at each level is \\(n\\), and the total time used is \\(O(n^2)\\).
      • Space complexity of \\(O(n)\\), in-place sorting: In the case where the input array is completely reversed, the worst recursive depth reaches \\(n\\), using \\(O(n)\\) stack frame space. The sorting operation is performed on the original array without the aid of an additional array.
      • Non-stable sorting: In the last step of sentinel partitioning, the pivot may be swapped to the right of equal elements.
      "},{"location":"chapter_sorting/quick_sort/#1153-why-is-quick-sort-fast","title":"11.5.3 \u00a0 Why Is Quick Sort Fast","text":"

      From the name, we can see that quick sort should have certain advantages in terms of efficiency. Although the average time complexity of quick sort is the same as \"merge sort\" and \"heap sort\", quick sort is usually more efficient, mainly for the following reasons.

      • The probability of the worst case occurring is very low: Although the worst-case time complexity of quick sort is \\(O(n^2)\\), which is not as stable as merge sort, in the vast majority of cases, quick sort can run with a time complexity of \\(O(n \\log n)\\).
      • High cache utilization: When performing sentinel partitioning operations, the system can load the entire sub-array into the cache, so element access efficiency is relatively high. Algorithms like \"heap sort\" require jump-style access to elements, thus lacking this characteristic.
      • Small constant coefficient of complexity: Among the three algorithms mentioned above, quick sort has the smallest total number of operations such as comparisons, assignments, and swaps. This is similar to the reason why \"insertion sort\" is faster than \"bubble sort\".
      "},{"location":"chapter_sorting/quick_sort/#1154-pivot-optimization","title":"11.5.4 \u00a0 Pivot Optimization","text":"

      Quick sort may have reduced time efficiency for certain inputs. Take an extreme example: suppose the input array is completely reversed. Since we select the leftmost element as the pivot, after sentinel partitioning is complete, the pivot is swapped to the rightmost end of the array, causing the left sub-array length to be \\(n - 1\\) and the right sub-array length to be \\(0\\). If we recurse down like this, each round of sentinel partitioning will have a sub-array length of \\(0\\), the divide-and-conquer strategy fails, and quick sort degrades to a form approximate to \"bubble sort\".

      To avoid this situation as much as possible, we can optimize the pivot selection strategy in sentinel partitioning. For example, we can randomly select an element as the pivot. However, if luck is not good and we select a non-ideal pivot every time, efficiency is still not satisfactory.

      It should be noted that programming languages usually generate \"pseudo-random numbers\". If we construct a specific test case for a pseudo-random number sequence, the efficiency of quick sort may still degrade.

      For further improvement, we can select three candidate elements in the array (usually the first, last, and middle elements of the array), and use the median of these three candidate elements as the pivot. In this way, the probability that the pivot is \"neither too small nor too large\" will be greatly increased. Of course, we can also select more candidate elements to further improve the robustness of the algorithm. After adopting this method, the probability of time complexity degrading to \\(O(n^2)\\) is greatly reduced.

      Example code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
      def median_three(self, nums: list[int], left: int, mid: int, right: int) -> int:\n    \"\"\"Select the median of three candidate elements\"\"\"\n    l, m, r = nums[left], nums[mid], nums[right]\n    if (l <= m <= r) or (r <= m <= l):\n        return mid  # m is between l and r\n    if (m <= l <= r) or (r <= l <= m):\n        return left  # l is between m and r\n    return right\n\ndef partition(self, nums: list[int], left: int, right: int) -> int:\n    \"\"\"Sentinel partition (median of three)\"\"\"\n    # Use nums[left] as the pivot\n    med = self.median_three(nums, left, (left + right) // 2, right)\n    # Swap the median to the array's leftmost position\n    nums[left], nums[med] = nums[med], nums[left]\n    # Use nums[left] as the pivot\n    i, j = left, right\n    while i < j:\n        while i < j and nums[j] >= nums[left]:\n            j -= 1  # Search from right to left for the first element smaller than the pivot\n        while i < j and nums[i] <= nums[left]:\n            i += 1  # Search from left to right for the first element greater than the pivot\n        # Swap elements\n        nums[i], nums[j] = nums[j], nums[i]\n    # Swap the pivot to the boundary between the two subarrays\n    nums[i], nums[left] = nums[left], nums[i]\n    return i  # Return the index of the pivot\n
      quick_sort.cpp
      /* Select the median of three candidate elements */\nint medianThree(vector<int> &nums, int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m is between l and r\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l is between m and r\n    return right;\n}\n\n/* Sentinel partition (median of three) */\nint partition(vector<int> &nums, int left, int right) {\n    // Select the median of three candidate elements\n    int med = medianThree(nums, left, (left + right) / 2, right);\n    // Swap the median to the array's leftmost position\n    swap(nums[left], nums[med]);\n    // Use nums[left] as the pivot\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;                // Search from right to left for the first element smaller than the pivot\n        while (i < j && nums[i] <= nums[left])\n            i++;                // Search from left to right for the first element greater than the pivot\n        swap(nums[i], nums[j]); // Swap these two elements\n    }\n    swap(nums[i], nums[left]);  // Swap the pivot to the boundary between the two subarrays\n    return i;                   // Return the index of the pivot\n}\n
      quick_sort.java
      /* Select the median of three candidate elements */\nint medianThree(int[] nums, int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m is between l and r\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l is between m and r\n    return right;\n}\n\n/* Sentinel partition (median of three) */\nint partition(int[] nums, int left, int right) {\n    // Select the median of three candidate elements\n    int med = medianThree(nums, left, (left + right) / 2, right);\n    // Swap the median to the array's leftmost position\n    swap(nums, left, med);\n    // Use nums[left] as the pivot\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // Search from right to left for the first element smaller than the pivot\n        while (i < j && nums[i] <= nums[left])\n            i++;          // Search from left to right for the first element greater than the pivot\n        swap(nums, i, j); // Swap these two elements\n    }\n    swap(nums, i, left);  // Swap the pivot to the boundary between the two subarrays\n    return i;             // Return the index of the pivot\n}\n
      quick_sort.cs
      /* Select the median of three candidate elements */\nint MedianThree(int[] nums, int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m is between l and r\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l is between m and r\n    return right;\n}\n\n/* Sentinel partition (median of three) */\nint Partition(int[] nums, int left, int right) {\n    // Select the median of three candidate elements\n    int med = MedianThree(nums, left, (left + right) / 2, right);\n    // Swap the median to the array's leftmost position\n    Swap(nums, left, med);\n    // Use nums[left] as the pivot\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // Search from right to left for the first element smaller than the pivot\n        while (i < j && nums[i] <= nums[left])\n            i++;          // Search from left to right for the first element greater than the pivot\n        Swap(nums, i, j); // Swap these two elements\n    }\n    Swap(nums, i, left);  // Swap the pivot to the boundary between the two subarrays\n    return i;             // Return the index of the pivot\n}\n
      quick_sort.go
      /* Select the median of three candidate elements */\nfunc (q *quickSortMedian) medianThree(nums []int, left, mid, right int) int {\n    l, m, r := nums[left], nums[mid], nums[right]\n    if (l <= m && m <= r) || (r <= m && m <= l) {\n        return mid // m is between l and r\n    }\n    if (m <= l && l <= r) || (r <= l && l <= m) {\n        return left // l is between m and r\n    }\n    return right\n}\n\n/* Sentinel partition (median of three) */\nfunc (q *quickSortMedian) partition(nums []int, left, right int) int {\n    // Use nums[left] as the pivot\n    med := q.medianThree(nums, left, (left+right)/2, right)\n    // Swap the median to the array's leftmost position\n    nums[left], nums[med] = nums[med], nums[left]\n    // Use nums[left] as the pivot\n    i, j := left, right\n    for i < j {\n        for i < j && nums[j] >= nums[left] {\n            j-- // Search from right to left for the first element smaller than the pivot\n        }\n        for i < j && nums[i] <= nums[left] {\n            i++ // Search from left to right for the first element greater than the pivot\n        }\n        // Swap elements\n        nums[i], nums[j] = nums[j], nums[i]\n    }\n    // Swap the pivot to the boundary between the two subarrays\n    nums[i], nums[left] = nums[left], nums[i]\n    return i // Return the index of the pivot\n}\n
      quick_sort.swift
      /* Select the median of three candidate elements */\nfunc medianThree(nums: [Int], left: Int, mid: Int, right: Int) -> Int {\n    let l = nums[left]\n    let m = nums[mid]\n    let r = nums[right]\n    if (l <= m && m <= r) || (r <= m && m <= l) {\n        return mid // m is between l and r\n    }\n    if (m <= l && l <= r) || (r <= l && l <= m) {\n        return left // l is between m and r\n    }\n    return right\n}\n\n/* Sentinel partition (median of three) */\nfunc partitionMedian(nums: inout [Int], left: Int, right: Int) -> Int {\n    // Select the median of three candidate elements\n    let med = medianThree(nums: nums, left: left, mid: left + (right - left) / 2, right: right)\n    // Swap the median to the array's leftmost position\n    nums.swapAt(left, med)\n    return partition(nums: &nums, left: left, right: right)\n}\n
      quick_sort.js
      /* Select the median of three candidate elements */\nmedianThree(nums, left, mid, right) {\n    let l = nums[left],\n        m = nums[mid],\n        r = nums[right];\n    // m is between l and r\n    if ((l <= m && m <= r) || (r <= m && m <= l)) return mid;\n    // l is between m and r\n    if ((m <= l && l <= r) || (r <= l && l <= m)) return left;\n    return right;\n}\n\n/* Sentinel partition (median of three) */\npartition(nums, left, right) {\n    // Select the median of three candidate elements\n    let med = this.medianThree(\n        nums,\n        left,\n        Math.floor((left + right) / 2),\n        right\n    );\n    // Swap the median to the array's leftmost position\n    this.swap(nums, left, med);\n    // Use nums[left] as the pivot\n    let i = left,\n        j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) j--; // Search from right to left for the first element smaller than the pivot\n        while (i < j && nums[i] <= nums[left]) i++; // Search from left to right for the first element greater than the pivot\n        this.swap(nums, i, j); // Swap these two elements\n    }\n    this.swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays\n    return i; // Return the index of the pivot\n}\n
      quick_sort.ts
      /* Select the median of three candidate elements */\nmedianThree(\n    nums: number[],\n    left: number,\n    mid: number,\n    right: number\n): number {\n    let l = nums[left],\n        m = nums[mid],\n        r = nums[right];\n    // m is between l and r\n    if ((l <= m && m <= r) || (r <= m && m <= l)) return mid;\n    // l is between m and r\n    if ((m <= l && l <= r) || (r <= l && l <= m)) return left;\n    return right;\n}\n\n/* Sentinel partition (median of three) */\npartition(nums: number[], left: number, right: number): number {\n    // Select the median of three candidate elements\n    let med = this.medianThree(\n        nums,\n        left,\n        Math.floor((left + right) / 2),\n        right\n    );\n    // Swap the median to the array's leftmost position\n    this.swap(nums, left, med);\n    // Use nums[left] as the pivot\n    let i = left,\n        j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) {\n            j--; // Search from right to left for the first element smaller than the pivot\n        }\n        while (i < j && nums[i] <= nums[left]) {\n            i++; // Search from left to right for the first element greater than the pivot\n        }\n        this.swap(nums, i, j); // Swap these two elements\n    }\n    this.swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays\n    return i; // Return the index of the pivot\n}\n
      quick_sort.dart
      /* Select the median of three candidate elements */\nint _medianThree(List<int> nums, int left, int mid, int right) {\n  int l = nums[left], m = nums[mid], r = nums[right];\n  if ((l <= m && m <= r) || (r <= m && m <= l))\n    return mid; // m is between l and r\n  if ((m <= l && l <= r) || (r <= l && l <= m))\n    return left; // l is between m and r\n  return right;\n}\n\n/* Sentinel partition (median of three) */\nint _partition(List<int> nums, int left, int right) {\n  // Select the median of three candidate elements\n  int med = _medianThree(nums, left, (left + right) ~/ 2, right);\n  // Swap the median to the array's leftmost position\n  _swap(nums, left, med);\n  // Use nums[left] as the pivot\n  int i = left, j = right;\n  while (i < j) {\n    while (i < j && nums[j] >= nums[left]) j--; // Search from right to left for the first element smaller than the pivot\n    while (i < j && nums[i] <= nums[left]) i++; // Search from left to right for the first element greater than the pivot\n    _swap(nums, i, j); // Swap these two elements\n  }\n  _swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays\n  return i; // Return the index of the pivot\n}\n
      quick_sort.rs
      /* Select the median of three candidate elements */\nfn median_three(nums: &mut [i32], left: usize, mid: usize, right: usize) -> usize {\n    let (l, m, r) = (nums[left], nums[mid], nums[right]);\n    if (l <= m && m <= r) || (r <= m && m <= l) {\n        return mid; // m is between l and r\n    }\n    if (m <= l && l <= r) || (r <= l && l <= m) {\n        return left; // l is between m and r\n    }\n    right\n}\n\n/* Sentinel partition (median of three) */\nfn partition(nums: &mut [i32], left: usize, right: usize) -> usize {\n    // Select the median of three candidate elements\n    let med = Self::median_three(nums, left, (left + right) / 2, right);\n    // Swap the median to the array's leftmost position\n    nums.swap(left, med);\n    // Use nums[left] as the pivot\n    let (mut i, mut j) = (left, right);\n    while i < j {\n        while i < j && nums[j] >= nums[left] {\n            j -= 1; // Search from right to left for the first element smaller than the pivot\n        }\n        while i < j && nums[i] <= nums[left] {\n            i += 1; // Search from left to right for the first element greater than the pivot\n        }\n        nums.swap(i, j); // Swap these two elements\n    }\n    nums.swap(i, left); // Swap the pivot to the boundary between the two subarrays\n    i // Return the index of the pivot\n}\n
      quick_sort.c
      /* Select the median of three candidate elements */\nint medianThree(int nums[], int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m is between l and r\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l is between m and r\n    return right;\n}\n\n/* Sentinel partition (median of three) */\nint partitionMedian(int nums[], int left, int right) {\n    // Select the median of three candidate elements\n    int med = medianThree(nums, left, (left + right) / 2, right);\n    // Swap the median to the array's leftmost position\n    swap(nums, left, med);\n    // Use nums[left] as the pivot\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--; // Search from right to left for the first element smaller than the pivot\n        while (i < j && nums[i] <= nums[left])\n            i++;          // Search from left to right for the first element greater than the pivot\n        swap(nums, i, j); // Swap these two elements\n    }\n    swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays\n    return i;            // Return the index of the pivot\n}\n
      quick_sort.kt
      /* Select the median of three candidate elements */\nfun medianThree(nums: IntArray, left: Int, mid: Int, right: Int): Int {\n    val l = nums[left]\n    val m = nums[mid]\n    val r = nums[right]\n    if ((m in l..r) || (m in r..l))\n        return mid  // m is between l and r\n    if ((l in m..r) || (l in r..m))\n        return left // l is between m and r\n    return right\n}\n\n/* Sentinel partition (median of three) */\nfun partitionMedian(nums: IntArray, left: Int, right: Int): Int {\n    // Select the median of three candidate elements\n    val med = medianThree(nums, left, (left + right) / 2, right)\n    // Swap the median to the array's leftmost position\n    swap(nums, left, med)\n    // Use nums[left] as the pivot\n    var i = left\n    var j = right\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--                      // Search from right to left for the first element smaller than the pivot\n        while (i < j && nums[i] <= nums[left])\n            i++                      // Search from left to right for the first element greater than the pivot\n        swap(nums, i, j)             // Swap these two elements\n    }\n    swap(nums, i, left)              // Swap the pivot to the boundary between the two subarrays\n    return i                         // Return the index of the pivot\n}\n
      quick_sort.rb
      ### Select median of three candidate elements ###\ndef median_three(nums, left, mid, right)\n  # Select the median of three candidate elements\n  _l, _m, _r = nums[left], nums[mid], nums[right]\n  # m is between l and r\n  return mid if (_l <= _m && _m <= _r) || (_r <= _m && _m <= _l)\n  # l is between m and r\n  return left if (_m <= _l && _l <= _r) || (_r <= _l && _l <= _m)\n  return right\nend\n\n### Sentinel partition (median of three) ###\ndef partition(nums, left, right)\n  ### Use nums[left] as pivot\n  med = median_three(nums, left, (left + right) / 2, right)\n  # Swap median to leftmost position of array\n  nums[left], nums[med] = nums[med], nums[left]\n  i, j = left, right\n  while i < j\n    while i < j && nums[j] >= nums[left]\n      j -= 1 # Search from right to left for the first element smaller than the pivot\n    end\n    while i < j && nums[i] <= nums[left]\n      i += 1 # Search from left to right for the first element greater than the pivot\n    end\n    # Swap elements\n    nums[i], nums[j] = nums[j], nums[i]\n  end\n  # Swap the pivot to the boundary between the two subarrays\n  nums[i], nums[left] = nums[left], nums[i]\n  i # Return the index of the pivot\nend\n
      "},{"location":"chapter_sorting/quick_sort/#1155-recursive-depth-optimization","title":"11.5.5 \u00a0 Recursive Depth Optimization","text":"

      For certain inputs, quick sort may occupy more space. Taking a completely ordered input array as an example, let the length of the sub-array in recursion be \\(m\\). Each round of sentinel partitioning will produce a left sub-array of length \\(0\\) and a right sub-array of length \\(m - 1\\), which means that the problem scale reduced per recursive call is very small (only one element is reduced), and the height of the recursion tree will reach \\(n - 1\\), at which point \\(O(n)\\) size of stack frame space is required.

      To prevent the accumulation of stack frame space, we can compare the lengths of the two sub-arrays after each round of sentinel sorting is complete, and only recurse on the shorter sub-array. Since the length of the shorter sub-array will not exceed \\(n / 2\\), this method can ensure that the recursion depth does not exceed \\(\\log n\\), thus optimizing the worst-case space complexity to \\(O(\\log n)\\). The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
      def quick_sort(self, nums: list[int], left: int, right: int):\n    \"\"\"Quick sort (recursion depth optimization)\"\"\"\n    # Terminate when subarray length is 1\n    while left < right:\n        # Sentinel partition operation\n        pivot = self.partition(nums, left, right)\n        # Perform quick sort on the shorter of the two subarrays\n        if pivot - left < right - pivot:\n            self.quick_sort(nums, left, pivot - 1)  # Recursively sort the left subarray\n            left = pivot + 1  # Remaining unsorted interval is [pivot + 1, right]\n        else:\n            self.quick_sort(nums, pivot + 1, right)  # Recursively sort the right subarray\n            right = pivot - 1  # Remaining unsorted interval is [left, pivot - 1]\n
      quick_sort.cpp
      /* Quick sort (recursion depth optimization) */\nvoid quickSort(vector<int> &nums, int left, int right) {\n    // Terminate when subarray length is 1\n    while (left < right) {\n        // Sentinel partition operation\n        int pivot = partition(nums, left, right);\n        // Perform quick sort on the shorter of the two subarrays\n        if (pivot - left < right - pivot) {\n            quickSort(nums, left, pivot - 1); // Recursively sort the left subarray\n            left = pivot + 1;                 // Remaining unsorted interval is [pivot + 1, right]\n        } else {\n            quickSort(nums, pivot + 1, right); // Recursively sort the right subarray\n            right = pivot - 1;                 // Remaining unsorted interval is [left, pivot - 1]\n        }\n    }\n}\n
      quick_sort.java
      /* Quick sort (recursion depth optimization) */\nvoid quickSort(int[] nums, int left, int right) {\n    // Terminate when subarray length is 1\n    while (left < right) {\n        // Sentinel partition operation\n        int pivot = partition(nums, left, right);\n        // Perform quick sort on the shorter of the two subarrays\n        if (pivot - left < right - pivot) {\n            quickSort(nums, left, pivot - 1); // Recursively sort the left subarray\n            left = pivot + 1; // Remaining unsorted interval is [pivot + 1, right]\n        } else {\n            quickSort(nums, pivot + 1, right); // Recursively sort the right subarray\n            right = pivot - 1; // Remaining unsorted interval is [left, pivot - 1]\n        }\n    }\n}\n
      quick_sort.cs
      /* Quick sort (recursion depth optimization) */\nvoid QuickSort(int[] nums, int left, int right) {\n    // Terminate when subarray length is 1\n    while (left < right) {\n        // Sentinel partition operation\n        int pivot = Partition(nums, left, right);\n        // Perform quick sort on the shorter of the two subarrays\n        if (pivot - left < right - pivot) {\n            QuickSort(nums, left, pivot - 1);  // Recursively sort the left subarray\n            left = pivot + 1;  // Remaining unsorted interval is [pivot + 1, right]\n        } else {\n            QuickSort(nums, pivot + 1, right); // Recursively sort the right subarray\n            right = pivot - 1; // Remaining unsorted interval is [left, pivot - 1]\n        }\n    }\n}\n
      quick_sort.go
      /* Quick sort (recursion depth optimization) */\nfunc (q *quickSortTailCall) quickSort(nums []int, left, right int) {\n    // Terminate when subarray length is 1\n    for left < right {\n        // Sentinel partition operation\n        pivot := q.partition(nums, left, right)\n        // Perform quick sort on the shorter of the two subarrays\n        if pivot-left < right-pivot {\n            q.quickSort(nums, left, pivot-1) // Recursively sort the left subarray\n            left = pivot + 1                 // Remaining unsorted interval is [pivot + 1, right]\n        } else {\n            q.quickSort(nums, pivot+1, right) // Recursively sort the right subarray\n            right = pivot - 1                 // Remaining unsorted interval is [left, pivot - 1]\n        }\n    }\n}\n
      quick_sort.swift
      /* Quick sort (recursion depth optimization) */\nfunc quickSortTailCall(nums: inout [Int], left: Int, right: Int) {\n    var left = left\n    var right = right\n    // Terminate when subarray length is 1\n    while left < right {\n        // Sentinel partition operation\n        let pivot = partition(nums: &nums, left: left, right: right)\n        // Perform quick sort on the shorter of the two subarrays\n        if (pivot - left) < (right - pivot) {\n            quickSortTailCall(nums: &nums, left: left, right: pivot - 1) // Recursively sort the left subarray\n            left = pivot + 1 // Remaining unsorted interval is [pivot + 1, right]\n        } else {\n            quickSortTailCall(nums: &nums, left: pivot + 1, right: right) // Recursively sort the right subarray\n            right = pivot - 1 // Remaining unsorted interval is [left, pivot - 1]\n        }\n    }\n}\n
      quick_sort.js
      /* Quick sort (recursion depth optimization) */\nquickSort(nums, left, right) {\n    // Terminate when subarray length is 1\n    while (left < right) {\n        // Sentinel partition operation\n        let pivot = this.partition(nums, left, right);\n        // Perform quick sort on the shorter of the two subarrays\n        if (pivot - left < right - pivot) {\n            this.quickSort(nums, left, pivot - 1); // Recursively sort the left subarray\n            left = pivot + 1; // Remaining unsorted interval is [pivot + 1, right]\n        } else {\n            this.quickSort(nums, pivot + 1, right); // Recursively sort the right subarray\n            right = pivot - 1; // Remaining unsorted interval is [left, pivot - 1]\n        }\n    }\n}\n
      quick_sort.ts
      /* Quick sort (recursion depth optimization) */\nquickSort(nums: number[], left: number, right: number): void {\n    // Terminate when subarray length is 1\n    while (left < right) {\n        // Sentinel partition operation\n        let pivot = this.partition(nums, left, right);\n        // Perform quick sort on the shorter of the two subarrays\n        if (pivot - left < right - pivot) {\n            this.quickSort(nums, left, pivot - 1); // Recursively sort the left subarray\n            left = pivot + 1; // Remaining unsorted interval is [pivot + 1, right]\n        } else {\n            this.quickSort(nums, pivot + 1, right); // Recursively sort the right subarray\n            right = pivot - 1; // Remaining unsorted interval is [left, pivot - 1]\n        }\n    }\n}\n
      quick_sort.dart
      /* Quick sort (recursion depth optimization) */\nvoid quickSort(List<int> nums, int left, int right) {\n  // Terminate when subarray length is 1\n  while (left < right) {\n    // Sentinel partition operation\n    int pivot = _partition(nums, left, right);\n    // Perform quick sort on the shorter of the two subarrays\n    if (pivot - left < right - pivot) {\n      quickSort(nums, left, pivot - 1); // Recursively sort the left subarray\n      left = pivot + 1; // Remaining unsorted interval is [pivot + 1, right]\n    } else {\n      quickSort(nums, pivot + 1, right); // Recursively sort the right subarray\n      right = pivot - 1; // Remaining unsorted interval is [left, pivot - 1]\n    }\n  }\n}\n
      quick_sort.rs
      /* Quick sort (recursion depth optimization) */\npub fn quick_sort(mut left: i32, mut right: i32, nums: &mut [i32]) {\n    // Terminate when subarray length is 1\n    while left < right {\n        // Sentinel partition operation\n        let pivot = Self::partition(nums, left as usize, right as usize) as i32;\n        // Perform quick sort on the shorter of the two subarrays\n        if pivot - left < right - pivot {\n            Self::quick_sort(left, pivot - 1, nums); // Recursively sort the left subarray\n            left = pivot + 1; // Remaining unsorted interval is [pivot + 1, right]\n        } else {\n            Self::quick_sort(pivot + 1, right, nums); // Recursively sort the right subarray\n            right = pivot - 1; // Remaining unsorted interval is [left, pivot - 1]\n        }\n    }\n}\n
      quick_sort.c
      /* Quick sort (recursion depth optimization) */\nvoid quickSortTailCall(int nums[], int left, int right) {\n    // Terminate when subarray length is 1\n    while (left < right) {\n        // Sentinel partition operation\n        int pivot = partition(nums, left, right);\n        // Perform quick sort on the shorter of the two subarrays\n        if (pivot - left < right - pivot) {\n            // Recursively sort the left subarray\n            quickSortTailCall(nums, left, pivot - 1);\n            // Remaining unsorted interval is [pivot + 1, right]\n            left = pivot + 1;\n        } else {\n            // Recursively sort the right subarray\n            quickSortTailCall(nums, pivot + 1, right);\n            // Remaining unsorted interval is [left, pivot - 1]\n            right = pivot - 1;\n        }\n    }\n}\n
      quick_sort.kt
      /* Quick sort (recursion depth optimization) */\nfun quickSortTailCall(nums: IntArray, left: Int, right: Int) {\n    // Terminate when subarray length is 1\n    var l = left\n    var r = right\n    while (l < r) {\n        // Sentinel partition operation\n        val pivot = partition(nums, l, r)\n        // Perform quick sort on the shorter of the two subarrays\n        if (pivot - l < r - pivot) {\n            quickSort(nums, l, pivot - 1) // Recursively sort the left subarray\n            l = pivot + 1 // Remaining unsorted interval is [pivot + 1, right]\n        } else {\n            quickSort(nums, pivot + 1, r) // Recursively sort the right subarray\n            r = pivot - 1 // Remaining unsorted interval is [left, pivot - 1]\n        }\n    }\n}\n
      quick_sort.rb
      ### Quick sort (recursion depth optimization) ###\ndef quick_sort(nums, left, right)\n  # Recurse when subarray length is not 1\n  while left < right\n    # Sentinel partition\n    pivot = partition(nums, left, right)\n    # Perform quick sort on the shorter of the two subarrays\n    if pivot - left < right - pivot\n      quick_sort(nums, left, pivot - 1)\n      left = pivot + 1 # Remaining unsorted interval is [pivot + 1, right]\n    else\n      quick_sort(nums, pivot + 1, right)\n      right = pivot - 1 # Remaining unsorted interval is [left, pivot - 1]\n    end\n  end\nend\n
      "},{"location":"chapter_sorting/radix_sort/","title":"11.10 \u00a0 Radix Sort","text":"

      The previous section introduced counting sort, which is suitable for situations where the data volume \\(n\\) is large but the data range \\(m\\) is small. Suppose we need to sort \\(n = 10^6\\) student IDs, and the student ID is an 8-digit number, which means the data range \\(m = 10^8\\) is very large. Using counting sort would require allocating a large amount of memory space, whereas radix sort can avoid this situation.

      Radix sort (radix sort) has a core idea consistent with counting sort, which also achieves sorting by counting quantities. Building on this, radix sort utilizes the progressive relationship between the digits of numbers, sorting each digit in turn to obtain the final sorting result.

      "},{"location":"chapter_sorting/radix_sort/#11101-algorithm-flow","title":"11.10.1 \u00a0 Algorithm Flow","text":"

      Taking student ID data as an example, assume the lowest digit is the \\(1\\)st digit and the highest digit is the \\(8\\)th digit. The flow of radix sort is shown in Figure 11-18.

      1. Initialize the digit \\(k = 1\\).
      2. Perform \"counting sort\" on the \\(k\\)th digit of the student IDs. After completion, the data will be sorted from smallest to largest according to the \\(k\\)th digit.
      3. Increase \\(k\\) by \\(1\\), then return to step 2. and continue iterating until all digits are sorted, at which point the process ends.

      Figure 11-18 \u00a0 Radix sort algorithm flow

      Below we analyze the code implementation. For a \\(d\\)-base number \\(x\\), to get its \\(k\\)th digit \\(x_k\\), the following calculation formula can be used:

      \\[ x_k = \\lfloor\\frac{x}{d^{k-1}}\\rfloor \\bmod d \\]

      Where \\(\\lfloor a \\rfloor\\) denotes rounding down the floating-point number \\(a\\), and \\(\\bmod \\: d\\) denotes taking the modulo (remainder) with respect to \\(d\\). For student ID data, \\(d = 10\\) and \\(k \\in [1, 8]\\).

      Additionally, we need to slightly modify the counting sort code to make it sort based on the \\(k\\)th digit of the number:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby radix_sort.py
      def digit(num: int, exp: int) -> int:\n    \"\"\"Get the k-th digit of element num, where exp = 10^(k-1)\"\"\"\n    # Passing exp instead of k can avoid repeated expensive exponentiation here\n    return (num // exp) % 10\n\ndef counting_sort_digit(nums: list[int], exp: int):\n    \"\"\"Counting sort (based on nums k-th digit)\"\"\"\n    # Decimal digit range is 0~9, therefore need a bucket array of length 10\n    counter = [0] * 10\n    n = len(nums)\n    # Count the occurrence of digits 0~9\n    for i in range(n):\n        d = digit(nums[i], exp)  # Get the k-th digit of nums[i], noted as d\n        counter[d] += 1  # Count the occurrence of digit d\n    # Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n    for i in range(1, 10):\n        counter[i] += counter[i - 1]\n    # Traverse in reverse, based on bucket statistics, place each element into res\n    res = [0] * n\n    for i in range(n - 1, -1, -1):\n        d = digit(nums[i], exp)\n        j = counter[d] - 1  # Get the index j for d in the array\n        res[j] = nums[i]  # Place the current element at index j\n        counter[d] -= 1  # Decrease the count of d by 1\n    # Use result to overwrite the original array nums\n    for i in range(n):\n        nums[i] = res[i]\n\ndef radix_sort(nums: list[int]):\n    \"\"\"Radix sort\"\"\"\n    # Get the maximum element of the array, used to determine the maximum number of digits\n    m = max(nums)\n    # Traverse from the lowest to the highest digit\n    exp = 1\n    while exp <= m:\n        # Perform counting sort on the k-th digit of array elements\n        # k = 1 -> exp = 1\n        # k = 2 -> exp = 10\n        # i.e., exp = 10^(k-1)\n        counting_sort_digit(nums, exp)\n        exp *= 10\n
      radix_sort.cpp
      /* Get the k-th digit of element num, where exp = 10^(k-1) */\nint digit(int num, int exp) {\n    // Passing exp instead of k can avoid repeated expensive exponentiation here\n    return (num / exp) % 10;\n}\n\n/* Counting sort (based on nums k-th digit) */\nvoid countingSortDigit(vector<int> &nums, int exp) {\n    // Decimal digit range is 0~9, therefore need a bucket array of length 10\n    vector<int> counter(10, 0);\n    int n = nums.size();\n    // Count the occurrence of digits 0~9\n    for (int i = 0; i < n; i++) {\n        int d = digit(nums[i], exp); // Get the k-th digit of nums[i], noted as d\n        counter[d]++;                // Count the occurrence of digit d\n    }\n    // Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // Traverse in reverse, based on bucket statistics, place each element into res\n    vector<int> res(n, 0);\n    for (int i = n - 1; i >= 0; i--) {\n        int d = digit(nums[i], exp);\n        int j = counter[d] - 1; // Get the index j for d in the array\n        res[j] = nums[i];       // Place the current element at index j\n        counter[d]--;           // Decrease the count of d by 1\n    }\n    // Use result to overwrite the original array nums\n    for (int i = 0; i < n; i++)\n        nums[i] = res[i];\n}\n\n/* Radix sort */\nvoid radixSort(vector<int> &nums) {\n    // Get the maximum element of the array, used to determine the maximum number of digits\n    int m = *max_element(nums.begin(), nums.end());\n    // Traverse from the lowest to the highest digit\n    for (int exp = 1; exp <= m; exp *= 10)\n        // Perform counting sort on the k-th digit of array elements\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // i.e., exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n}\n
      radix_sort.java
      /* Get the k-th digit of element num, where exp = 10^(k-1) */\nint digit(int num, int exp) {\n    // Passing exp instead of k can avoid repeated expensive exponentiation here\n    return (num / exp) % 10;\n}\n\n/* Counting sort (based on nums k-th digit) */\nvoid countingSortDigit(int[] nums, int exp) {\n    // Decimal digit range is 0~9, therefore need a bucket array of length 10\n    int[] counter = new int[10];\n    int n = nums.length;\n    // Count the occurrence of digits 0~9\n    for (int i = 0; i < n; i++) {\n        int d = digit(nums[i], exp); // Get the k-th digit of nums[i], noted as d\n        counter[d]++;                // Count the occurrence of digit d\n    }\n    // Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // Traverse in reverse, based on bucket statistics, place each element into res\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int d = digit(nums[i], exp);\n        int j = counter[d] - 1; // Get the index j for d in the array\n        res[j] = nums[i];       // Place the current element at index j\n        counter[d]--;           // Decrease the count of d by 1\n    }\n    // Use result to overwrite the original array nums\n    for (int i = 0; i < n; i++)\n        nums[i] = res[i];\n}\n\n/* Radix sort */\nvoid radixSort(int[] nums) {\n    // Get the maximum element of the array, used to determine the maximum number of digits\n    int m = Integer.MIN_VALUE;\n    for (int num : nums)\n        if (num > m)\n            m = num;\n    // Traverse from the lowest to the highest digit\n    for (int exp = 1; exp <= m; exp *= 10) {\n        // Perform counting sort on the k-th digit of array elements\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // i.e., exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n    }\n}\n
      radix_sort.cs
      /* Get the k-th digit of element num, where exp = 10^(k-1) */\nint Digit(int num, int exp) {\n    // Passing exp instead of k can avoid repeated expensive exponentiation here\n    return (num / exp) % 10;\n}\n\n/* Counting sort (based on nums k-th digit) */\nvoid CountingSortDigit(int[] nums, int exp) {\n    // Decimal digit range is 0~9, therefore need a bucket array of length 10\n    int[] counter = new int[10];\n    int n = nums.Length;\n    // Count the occurrence of digits 0~9\n    for (int i = 0; i < n; i++) {\n        int d = Digit(nums[i], exp); // Get the k-th digit of nums[i], noted as d\n        counter[d]++;                // Count the occurrence of digit d\n    }\n    // Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // Traverse in reverse, based on bucket statistics, place each element into res\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int d = Digit(nums[i], exp);\n        int j = counter[d] - 1; // Get the index j for d in the array\n        res[j] = nums[i];       // Place the current element at index j\n        counter[d]--;           // Decrease the count of d by 1\n    }\n    // Use result to overwrite the original array nums\n    for (int i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n\n/* Radix sort */\nvoid RadixSort(int[] nums) {\n    // Get the maximum element of the array, used to determine the maximum number of digits\n    int m = int.MinValue;\n    foreach (int num in nums) {\n        if (num > m) m = num;\n    }\n    // Traverse from the lowest to the highest digit\n    for (int exp = 1; exp <= m; exp *= 10) {\n        // Perform counting sort on the k-th digit of array elements\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // i.e., exp = 10^(k-1)\n        CountingSortDigit(nums, exp);\n    }\n}\n
      radix_sort.go
      /* Get the k-th digit of element num, where exp = 10^(k-1) */\nfunc digit(num, exp int) int {\n    // Passing exp instead of k can avoid repeated expensive exponentiation here\n    return (num / exp) % 10\n}\n\n/* Counting sort (based on nums k-th digit) */\nfunc countingSortDigit(nums []int, exp int) {\n    // Decimal digit range is 0~9, therefore need a bucket array of length 10\n    counter := make([]int, 10)\n    n := len(nums)\n    // Count the occurrence of digits 0~9\n    for i := 0; i < n; i++ {\n        d := digit(nums[i], exp) // Get the k-th digit of nums[i], noted as d\n        counter[d]++             // Count the occurrence of digit d\n    }\n    // Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n    for i := 1; i < 10; i++ {\n        counter[i] += counter[i-1]\n    }\n    // Traverse in reverse, based on bucket statistics, place each element into res\n    res := make([]int, n)\n    for i := n - 1; i >= 0; i-- {\n        d := digit(nums[i], exp)\n        j := counter[d] - 1 // Get the index j for d in the array\n        res[j] = nums[i]    // Place the current element at index j\n        counter[d]--        // Decrease the count of d by 1\n    }\n    // Use result to overwrite the original array nums\n    for i := 0; i < n; i++ {\n        nums[i] = res[i]\n    }\n}\n\n/* Radix sort */\nfunc radixSort(nums []int) {\n    // Get the maximum element of the array, used to determine the maximum number of digits\n    max := math.MinInt\n    for _, num := range nums {\n        if num > max {\n            max = num\n        }\n    }\n    // Traverse from the lowest to the highest digit\n    for exp := 1; max >= exp; exp *= 10 {\n        // Perform counting sort on the k-th digit of array elements\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // i.e., exp = 10^(k-1)\n        countingSortDigit(nums, exp)\n    }\n}\n
      radix_sort.swift
      /* Get the k-th digit of element num, where exp = 10^(k-1) */\nfunc digit(num: Int, exp: Int) -> Int {\n    // Passing exp instead of k can avoid repeated expensive exponentiation here\n    (num / exp) % 10\n}\n\n/* Counting sort (based on nums k-th digit) */\nfunc countingSortDigit(nums: inout [Int], exp: Int) {\n    // Decimal digit range is 0~9, therefore need a bucket array of length 10\n    var counter = Array(repeating: 0, count: 10)\n    // Count the occurrence of digits 0~9\n    for i in nums.indices {\n        let d = digit(num: nums[i], exp: exp) // Get the k-th digit of nums[i], noted as d\n        counter[d] += 1 // Count the occurrence of digit d\n    }\n    // Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n    for i in 1 ..< 10 {\n        counter[i] += counter[i - 1]\n    }\n    // Traverse in reverse, based on bucket statistics, place each element into res\n    var res = Array(repeating: 0, count: nums.count)\n    for i in nums.indices.reversed() {\n        let d = digit(num: nums[i], exp: exp)\n        let j = counter[d] - 1 // Get the index j for d in the array\n        res[j] = nums[i] // Place the current element at index j\n        counter[d] -= 1 // Decrease the count of d by 1\n    }\n    // Use result to overwrite the original array nums\n    for i in nums.indices {\n        nums[i] = res[i]\n    }\n}\n\n/* Radix sort */\nfunc radixSort(nums: inout [Int]) {\n    // Get the maximum element of the array, used to determine the maximum number of digits\n    var m = Int.min\n    for num in nums {\n        if num > m {\n            m = num\n        }\n    }\n    // Traverse from the lowest to the highest digit\n    for exp in sequence(first: 1, next: { m >= ($0 * 10) ? $0 * 10 : nil }) {\n        // Perform counting sort on the k-th digit of array elements\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // i.e., exp = 10^(k-1)\n        countingSortDigit(nums: &nums, exp: exp)\n    }\n}\n
      radix_sort.js
      /* Get the k-th digit of element num, where exp = 10^(k-1) */\nfunction digit(num, exp) {\n    // Passing exp instead of k can avoid repeated expensive exponentiation here\n    return Math.floor(num / exp) % 10;\n}\n\n/* Counting sort (based on nums k-th digit) */\nfunction countingSortDigit(nums, exp) {\n    // Decimal digit range is 0~9, therefore need a bucket array of length 10\n    const counter = new Array(10).fill(0);\n    const n = nums.length;\n    // Count the occurrence of digits 0~9\n    for (let i = 0; i < n; i++) {\n        const d = digit(nums[i], exp); // Get the k-th digit of nums[i], noted as d\n        counter[d]++; // Count the occurrence of digit d\n    }\n    // Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n    for (let i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // Traverse in reverse, based on bucket statistics, place each element into res\n    const res = new Array(n).fill(0);\n    for (let i = n - 1; i >= 0; i--) {\n        const d = digit(nums[i], exp);\n        const j = counter[d] - 1; // Get the index j for d in the array\n        res[j] = nums[i]; // Place the current element at index j\n        counter[d]--; // Decrease the count of d by 1\n    }\n    // Use result to overwrite the original array nums\n    for (let i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n\n/* Radix sort */\nfunction radixSort(nums) {\n    // Get the maximum element of the array, used to determine the maximum number of digits\n    let m = Math.max(... nums);\n    // Traverse from the lowest to the highest digit\n    for (let exp = 1; exp <= m; exp *= 10) {\n        // Perform counting sort on the k-th digit of array elements\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // i.e., exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n    }\n}\n
      radix_sort.ts
      /* Get the k-th digit of element num, where exp = 10^(k-1) */\nfunction digit(num: number, exp: number): number {\n    // Passing exp instead of k can avoid repeated expensive exponentiation here\n    return Math.floor(num / exp) % 10;\n}\n\n/* Counting sort (based on nums k-th digit) */\nfunction countingSortDigit(nums: number[], exp: number): void {\n    // Decimal digit range is 0~9, therefore need a bucket array of length 10\n    const counter = new Array(10).fill(0);\n    const n = nums.length;\n    // Count the occurrence of digits 0~9\n    for (let i = 0; i < n; i++) {\n        const d = digit(nums[i], exp); // Get the k-th digit of nums[i], noted as d\n        counter[d]++; // Count the occurrence of digit d\n    }\n    // Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n    for (let i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // Traverse in reverse, based on bucket statistics, place each element into res\n    const res = new Array(n).fill(0);\n    for (let i = n - 1; i >= 0; i--) {\n        const d = digit(nums[i], exp);\n        const j = counter[d] - 1; // Get the index j for d in the array\n        res[j] = nums[i]; // Place the current element at index j\n        counter[d]--; // Decrease the count of d by 1\n    }\n    // Use result to overwrite the original array nums\n    for (let i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n\n/* Radix sort */\nfunction radixSort(nums: number[]): void {\n    // Get the maximum element of the array, used to determine the maximum number of digits\n    let m: number = Math.max(... nums);\n    // Traverse from the lowest to the highest digit\n    for (let exp = 1; exp <= m; exp *= 10) {\n        // Perform counting sort on the k-th digit of array elements\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // i.e., exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n    }\n}\n
      radix_sort.dart
      /* Get k-th digit of element _num, where exp = 10^(k-1) */\nint digit(int _num, int exp) {\n  // Passing exp instead of k can avoid repeated expensive exponentiation here\n  return (_num ~/ exp) % 10;\n}\n\n/* Counting sort (based on nums k-th digit) */\nvoid countingSortDigit(List<int> nums, int exp) {\n  // Decimal digit range is 0~9, therefore need a bucket array of length 10\n  List<int> counter = List<int>.filled(10, 0);\n  int n = nums.length;\n  // Count the occurrence of digits 0~9\n  for (int i = 0; i < n; i++) {\n    int d = digit(nums[i], exp); // Get the k-th digit of nums[i], noted as d\n    counter[d]++; // Count the occurrence of digit d\n  }\n  // Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n  for (int i = 1; i < 10; i++) {\n    counter[i] += counter[i - 1];\n  }\n  // Traverse in reverse, based on bucket statistics, place each element into res\n  List<int> res = List<int>.filled(n, 0);\n  for (int i = n - 1; i >= 0; i--) {\n    int d = digit(nums[i], exp);\n    int j = counter[d] - 1; // Get the index j for d in the array\n    res[j] = nums[i]; // Place the current element at index j\n    counter[d]--; // Decrease the count of d by 1\n  }\n  // Use result to overwrite the original array nums\n  for (int i = 0; i < n; i++) nums[i] = res[i];\n}\n\n/* Radix sort */\nvoid radixSort(List<int> nums) {\n  // Get the maximum element of the array, used to determine the maximum number of digits\n  // In Dart, int length is 64 bits\n  int m = -1 << 63;\n  for (int _num in nums) if (_num > m) m = _num;\n  // Traverse from the lowest to the highest digit\n  for (int exp = 1; exp <= m; exp *= 10)\n    // Perform counting sort on the k-th digit of array elements\n    // k = 1 -> exp = 1\n    // k = 2 -> exp = 10\n    // i.e., exp = 10^(k-1)\n    countingSortDigit(nums, exp);\n}\n
      radix_sort.rs
      /* Get the k-th digit of element num, where exp = 10^(k-1) */\nfn digit(num: i32, exp: i32) -> usize {\n    // Passing exp instead of k can avoid repeated expensive exponentiation here\n    return ((num / exp) % 10) as usize;\n}\n\n/* Counting sort (based on nums k-th digit) */\nfn counting_sort_digit(nums: &mut [i32], exp: i32) {\n    // Decimal digit range is 0~9, therefore need a bucket array of length 10\n    let mut counter = [0; 10];\n    let n = nums.len();\n    // Count the occurrence of digits 0~9\n    for i in 0..n {\n        let d = digit(nums[i], exp); // Get the k-th digit of nums[i], noted as d\n        counter[d] += 1; // Count the occurrence of digit d\n    }\n    // Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n    for i in 1..10 {\n        counter[i] += counter[i - 1];\n    }\n    // Traverse in reverse, based on bucket statistics, place each element into res\n    let mut res = vec![0; n];\n    for i in (0..n).rev() {\n        let d = digit(nums[i], exp);\n        let j = counter[d] - 1; // Get the index j for d in the array\n        res[j] = nums[i]; // Place the current element at index j\n        counter[d] -= 1; // Decrease the count of d by 1\n    }\n    // Use result to overwrite the original array nums\n    nums.copy_from_slice(&res);\n}\n\n/* Radix sort */\nfn radix_sort(nums: &mut [i32]) {\n    // Get the maximum element of the array, used to determine the maximum number of digits\n    let m = *nums.into_iter().max().unwrap();\n    // Traverse from the lowest to the highest digit\n    let mut exp = 1;\n    while exp <= m {\n        counting_sort_digit(nums, exp);\n        exp *= 10;\n    }\n}\n
      radix_sort.c
      /* Get the k-th digit of element num, where exp = 10^(k-1) */\nint digit(int num, int exp) {\n    // Passing exp instead of k can avoid repeated expensive exponentiation here\n    return (num / exp) % 10;\n}\n\n/* Counting sort (based on nums k-th digit) */\nvoid countingSortDigit(int nums[], int size, int exp) {\n    // Decimal digit range is 0~9, therefore need a bucket array of length 10\n    int *counter = (int *)malloc((sizeof(int) * 10));\n    memset(counter, 0, sizeof(int) * 10); // Initialize to 0 to support subsequent memory release\n    // Count the occurrence of digits 0~9\n    for (int i = 0; i < size; i++) {\n        // Get the k-th digit of nums[i], noted as d\n        int d = digit(nums[i], exp);\n        // Count the occurrence of digit d\n        counter[d]++;\n    }\n    // Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // Traverse in reverse, based on bucket statistics, place each element into res\n    int *res = (int *)malloc(sizeof(int) * size);\n    for (int i = size - 1; i >= 0; i--) {\n        int d = digit(nums[i], exp);\n        int j = counter[d] - 1; // Get the index j for d in the array\n        res[j] = nums[i];       // Place the current element at index j\n        counter[d]--;           // Decrease the count of d by 1\n    }\n    // Use result to overwrite the original array nums\n    for (int i = 0; i < size; i++) {\n        nums[i] = res[i];\n    }\n    // Free memory\n    free(res);\n    free(counter);\n}\n\n/* Radix sort */\nvoid radixSort(int nums[], int size) {\n    // Get the maximum element of the array, used to determine the maximum number of digits\n    int max = INT32_MIN;\n    for (int i = 0; i < size; i++) {\n        if (nums[i] > max) {\n            max = nums[i];\n        }\n    }\n    // Traverse from the lowest to the highest digit\n    for (int exp = 1; max >= exp; exp *= 10)\n        // Perform counting sort on the k-th digit of array elements\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // i.e., exp = 10^(k-1)\n        countingSortDigit(nums, size, exp);\n}\n
      radix_sort.kt
      /* Get the k-th digit of element num, where exp = 10^(k-1) */\nfun digit(num: Int, exp: Int): Int {\n    // Passing exp instead of k can avoid repeated expensive exponentiation here\n    return (num / exp) % 10\n}\n\n/* Counting sort (based on nums k-th digit) */\nfun countingSortDigit(nums: IntArray, exp: Int) {\n    // Decimal digit range is 0~9, therefore need a bucket array of length 10\n    val counter = IntArray(10)\n    val n = nums.size\n    // Count the occurrence of digits 0~9\n    for (i in 0..<n) {\n        val d = digit(nums[i], exp) // Get the k-th digit of nums[i], noted as d\n        counter[d]++                // Count the occurrence of digit d\n    }\n    // Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n    for (i in 1..9) {\n        counter[i] += counter[i - 1]\n    }\n    // Traverse in reverse, based on bucket statistics, place each element into res\n    val res = IntArray(n)\n    for (i in n - 1 downTo 0) {\n        val d = digit(nums[i], exp)\n        val j = counter[d] - 1 // Get the index j for d in the array\n        res[j] = nums[i]       // Place the current element at index j\n        counter[d]--           // Decrease the count of d by 1\n    }\n    // Use result to overwrite the original array nums\n    for (i in 0..<n)\n        nums[i] = res[i]\n}\n\n/* Radix sort */\nfun radixSort(nums: IntArray) {\n    // Get the maximum element of the array, used to determine the maximum number of digits\n    var m = Int.MIN_VALUE\n    for (num in nums) if (num > m) m = num\n    var exp = 1\n    // Traverse from the lowest to the highest digit\n    while (exp <= m) {\n        // Perform counting sort on the k-th digit of array elements\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // i.e., exp = 10^(k-1)\n        countingSortDigit(nums, exp)\n        exp *= 10\n    }\n}\n
      radix_sort.rb
      ### Get k-th digit of element num, where exp = 10^(k-1) ###\ndef digit(num, exp)\n  # Passing exp instead of k avoids expensive exponentiation calculations\n  (num / exp) % 10\nend\n\n### Counting sort (sort by k-th digit of nums) ###\ndef counting_sort_digit(nums, exp)\n  # Decimal digit range is 0~9, therefore need a bucket array of length 10\n  counter = Array.new(10, 0)\n  n = nums.length\n  # Count the occurrence of digits 0~9\n  for i in 0...n\n    d = digit(nums[i], exp) # Get the k-th digit of nums[i], noted as d\n    counter[d] += 1 # Count the occurrence of digit d\n  end\n  # Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n  (1...10).each { |i| counter[i] += counter[i - 1] }\n  # Traverse in reverse, based on bucket statistics, place each element into res\n  res = Array.new(n, 0)\n  for i in (n - 1).downto(0)\n    d = digit(nums[i], exp)\n    j = counter[d] - 1 # Get the index j for d in the array\n    res[j] = nums[i] # Place the current element at index j\n    counter[d] -= 1 # Decrease the count of d by 1\n  end\n  # Use result to overwrite the original array nums\n  (0...n).each { |i| nums[i] = res[i] }\nend\n\n### Radix sort ###\ndef radix_sort(nums)\n  # Get the maximum element of the array, used to determine the maximum number of digits\n  m = nums.max\n  # Traverse from the lowest to the highest digit\n  exp = 1\n  while exp <= m\n    # Perform counting sort on the k-th digit of array elements\n    # k = 1 -> exp = 1\n    # k = 2 -> exp = 10\n    # i.e., exp = 10^(k-1)\n    counting_sort_digit(nums, exp)\n    exp *= 10\n  end\nend\n

      Why start sorting from the lowest digit?

      In successive sorting rounds, the result of a later round will override the result of an earlier round. For example, if the first round result is \\(a < b\\), while the second round result is \\(a > b\\), then the second round's result will replace the first round's result. Since higher-order digits have higher priority than lower-order digits, we should sort the lower digits first and then sort the higher digits.

      "},{"location":"chapter_sorting/radix_sort/#11102-algorithm-characteristics","title":"11.10.2 \u00a0 Algorithm Characteristics","text":"

      Compared to counting sort, radix sort is suitable for larger numerical ranges, but the prerequisite is that the data must be representable in a fixed number of digits, and the number of digits should not be too large. For example, floating-point numbers are not suitable for radix sort because their number of digits \\(k\\) may be too large, potentially leading to time complexity \\(O(nk) \\gg O(n^2)\\).

      • Time complexity of \\(O(nk)\\), non-adaptive sorting: Let the data volume be \\(n\\), the data be in base \\(d\\), and the maximum number of digits be \\(k\\). Then performing counting sort on a certain digit uses \\(O(n + d)\\) time, and sorting all \\(k\\) digits uses \\(O((n + d)k)\\) time. Typically, both \\(d\\) and \\(k\\) are relatively small, and the time complexity approaches \\(O(n)\\).
      • Space complexity of \\(O(n + d)\\), non-in-place sorting: Same as counting sort, radix sort requires auxiliary arrays res and counter of lengths \\(n\\) and \\(d\\).
      • Stable sorting: When counting sort is stable, radix sort is also stable; when counting sort is unstable, radix sort cannot guarantee obtaining correct sorting results.
      "},{"location":"chapter_sorting/selection_sort/","title":"11.2 \u00a0 Selection Sort","text":"

      Selection sort (selection sort) works very simply: it opens a loop, and in each round, selects the smallest element from the unsorted interval and places it at the end of the sorted interval.

      Assume the array has length \\(n\\). The algorithm flow of selection sort is shown in Figure 11-2.

      1. Initially, all elements are unsorted, i.e., the unsorted (index) interval is \\([0, n-1]\\).
      2. Select the smallest element in the interval \\([0, n-1]\\) and swap it with the element at index \\(0\\). After completion, the first element of the array is sorted.
      3. Select the smallest element in the interval \\([1, n-1]\\) and swap it with the element at index \\(1\\). After completion, the first 2 elements of the array are sorted.
      4. And so on. After \\(n - 1\\) rounds of selection and swapping, the first \\(n - 1\\) elements of the array are sorted.
      5. The only remaining element must be the largest element, requiring no sorting, so the array sorting is complete.
      <1><2><3><4><5><6><7><8><9><10><11>

      Figure 11-2 \u00a0 Selection sort steps

      In the code, we use \\(k\\) to record the smallest element within the unsorted interval:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby selection_sort.py
      def selection_sort(nums: list[int]):\n    \"\"\"Selection sort\"\"\"\n    n = len(nums)\n    # Outer loop: unsorted interval is [i, n-1]\n    for i in range(n - 1):\n        # Inner loop: find the smallest element within the unsorted interval\n        k = i\n        for j in range(i + 1, n):\n            if nums[j] < nums[k]:\n                k = j  # Record the index of the smallest element\n        # Swap the smallest element with the first element of the unsorted interval\n        nums[i], nums[k] = nums[k], nums[i]\n
      selection_sort.cpp
      /* Selection sort */\nvoid selectionSort(vector<int> &nums) {\n    int n = nums.size();\n    // Outer loop: unsorted interval is [i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // Inner loop: find the smallest element within the unsorted interval\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // Record the index of the smallest element\n        }\n        // Swap the smallest element with the first element of the unsorted interval\n        swap(nums[i], nums[k]);\n    }\n}\n
      selection_sort.java
      /* Selection sort */\nvoid selectionSort(int[] nums) {\n    int n = nums.length;\n    // Outer loop: unsorted interval is [i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // Inner loop: find the smallest element within the unsorted interval\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // Record the index of the smallest element\n        }\n        // Swap the smallest element with the first element of the unsorted interval\n        int temp = nums[i];\n        nums[i] = nums[k];\n        nums[k] = temp;\n    }\n}\n
      selection_sort.cs
      /* Selection sort */\nvoid SelectionSort(int[] nums) {\n    int n = nums.Length;\n    // Outer loop: unsorted interval is [i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // Inner loop: find the smallest element within the unsorted interval\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // Record the index of the smallest element\n        }\n        // Swap the smallest element with the first element of the unsorted interval\n        (nums[k], nums[i]) = (nums[i], nums[k]);\n    }\n}\n
      selection_sort.go
      /* Selection sort */\nfunc selectionSort(nums []int) {\n    n := len(nums)\n    // Outer loop: unsorted interval is [i, n-1]\n    for i := 0; i < n-1; i++ {\n        // Inner loop: find the smallest element within the unsorted interval\n        k := i\n        for j := i + 1; j < n; j++ {\n            if nums[j] < nums[k] {\n                // Record the index of the smallest element\n                k = j\n            }\n        }\n        // Swap the smallest element with the first element of the unsorted interval\n        nums[i], nums[k] = nums[k], nums[i]\n\n    }\n}\n
      selection_sort.swift
      /* Selection sort */\nfunc selectionSort(nums: inout [Int]) {\n    // Outer loop: unsorted interval is [i, n-1]\n    for i in nums.indices.dropLast() {\n        // Inner loop: find the smallest element within the unsorted interval\n        var k = i\n        for j in nums.indices.dropFirst(i + 1) {\n            if nums[j] < nums[k] {\n                k = j // Record the index of the smallest element\n            }\n        }\n        // Swap the smallest element with the first element of the unsorted interval\n        nums.swapAt(i, k)\n    }\n}\n
      selection_sort.js
      /* Selection sort */\nfunction selectionSort(nums) {\n    let n = nums.length;\n    // Outer loop: unsorted interval is [i, n-1]\n    for (let i = 0; i < n - 1; i++) {\n        // Inner loop: find the smallest element within the unsorted interval\n        let k = i;\n        for (let j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k]) {\n                k = j; // Record the index of the smallest element\n            }\n        }\n        // Swap the smallest element with the first element of the unsorted interval\n        [nums[i], nums[k]] = [nums[k], nums[i]];\n    }\n}\n
      selection_sort.ts
      /* Selection sort */\nfunction selectionSort(nums: number[]): void {\n    let n = nums.length;\n    // Outer loop: unsorted interval is [i, n-1]\n    for (let i = 0; i < n - 1; i++) {\n        // Inner loop: find the smallest element within the unsorted interval\n        let k = i;\n        for (let j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k]) {\n                k = j; // Record the index of the smallest element\n            }\n        }\n        // Swap the smallest element with the first element of the unsorted interval\n        [nums[i], nums[k]] = [nums[k], nums[i]];\n    }\n}\n
      selection_sort.dart
      /* Selection sort */\nvoid selectionSort(List<int> nums) {\n  int n = nums.length;\n  // Outer loop: unsorted interval is [i, n-1]\n  for (int i = 0; i < n - 1; i++) {\n    // Inner loop: find the smallest element within the unsorted interval\n    int k = i;\n    for (int j = i + 1; j < n; j++) {\n      if (nums[j] < nums[k]) k = j; // Record the index of the smallest element\n    }\n    // Swap the smallest element with the first element of the unsorted interval\n    int temp = nums[i];\n    nums[i] = nums[k];\n    nums[k] = temp;\n  }\n}\n
      selection_sort.rs
      /* Selection sort */\nfn selection_sort(nums: &mut [i32]) {\n    if nums.is_empty() {\n        return;\n    }\n    let n = nums.len();\n    // Outer loop: unsorted interval is [i, n-1]\n    for i in 0..n - 1 {\n        // Inner loop: find the smallest element within the unsorted interval\n        let mut k = i;\n        for j in i + 1..n {\n            if nums[j] < nums[k] {\n                k = j; // Record the index of the smallest element\n            }\n        }\n        // Swap the smallest element with the first element of the unsorted interval\n        nums.swap(i, k);\n    }\n}\n
      selection_sort.c
      /* Selection sort */\nvoid selectionSort(int nums[], int n) {\n    // Outer loop: unsorted interval is [i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // Inner loop: find the smallest element within the unsorted interval\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // Record the index of the smallest element\n        }\n        // Swap the smallest element with the first element of the unsorted interval\n        int temp = nums[i];\n        nums[i] = nums[k];\n        nums[k] = temp;\n    }\n}\n
      selection_sort.kt
      /* Selection sort */\nfun selectionSort(nums: IntArray) {\n    val n = nums.size\n    // Outer loop: unsorted interval is [i, n-1]\n    for (i in 0..<n - 1) {\n        var k = i\n        // Inner loop: find the smallest element within the unsorted interval\n        for (j in i + 1..<n) {\n            if (nums[j] < nums[k])\n                k = j // Record the index of the smallest element\n        }\n        // Swap the smallest element with the first element of the unsorted interval\n        val temp = nums[i]\n        nums[i] = nums[k]\n        nums[k] = temp\n    }\n}\n
      selection_sort.rb
      ### Selection sort ###\ndef selection_sort(nums)\n  n = nums.length\n  # Outer loop: unsorted interval is [i, n-1]\n  for i in 0...(n - 1)\n    # Inner loop: find the smallest element within the unsorted interval\n    k = i\n    for j in (i + 1)...n\n      if nums[j] < nums[k]\n        k = j # Record the index of the smallest element\n      end\n    end\n    # Swap the smallest element with the first element of the unsorted interval\n    nums[i], nums[k] = nums[k], nums[i]\n  end\nend\n
      "},{"location":"chapter_sorting/selection_sort/#1121-algorithm-characteristics","title":"11.2.1 \u00a0 Algorithm Characteristics","text":"
      • Time complexity of \\(O(n^2)\\), non-adaptive sorting: The outer loop has \\(n - 1\\) rounds in total. The length of the unsorted interval in the first round is \\(n\\), and the length of the unsorted interval in the last round is \\(2\\). That is, each round of the outer loop contains \\(n\\), \\(n - 1\\), \\(\\dots\\), \\(3\\), \\(2\\) inner loop iterations, summing to \\(\\frac{(n - 1)(n + 2)}{2}\\).
      • Space complexity of \\(O(1)\\), in-place sorting: Pointers \\(i\\) and \\(j\\) use a constant amount of extra space.
      • Non-stable sorting: As shown in Figure 11-3, element nums[i] may be swapped to the right of an element equal to it, causing a change in their relative order.

      Figure 11-3 \u00a0 Selection sort non-stability example

      "},{"location":"chapter_sorting/sorting_algorithm/","title":"11.1 \u00a0 Sorting Algorithm","text":"

      Sorting algorithm (sorting algorithm) is used to arrange a group of data in a specific order. Sorting algorithms have extensive applications because ordered data can usually be searched, analyzed, and processed more efficiently.

      As shown in Figure 11-1, data types in sorting algorithms can be integers, floating-point numbers, characters, or strings, etc. The sorting criterion can be set according to requirements, such as numerical size, character ASCII code order, or custom rules.

      Figure 11-1 \u00a0 Data type and criterion examples

      "},{"location":"chapter_sorting/sorting_algorithm/#1111-evaluation-dimensions","title":"11.1.1 \u00a0 Evaluation Dimensions","text":"

      Execution efficiency: We expect the time complexity of sorting algorithms to be as low as possible, with a smaller total number of operations (reducing the constant factor in time complexity). For large data volumes, execution efficiency is particularly important.

      In-place property: As the name implies, in-place sorting achieves sorting by operating directly on the original array without requiring additional auxiliary arrays, thus saving memory. Typically, in-place sorting involves fewer data movement operations and runs faster.

      Stability: Stable sorting ensures that the relative order of equal elements in the array does not change after sorting is completed.

      Stable sorting is a necessary condition for multi-level sorting scenarios. Suppose we have a table storing student information, where column 1 and column 2 are name and age, respectively. In this case, unstable sorting may cause the ordered nature of the input data to be lost:

      # Input Data Is Sorted by Name\n# (name, age)\n  ('A', 19)\n  ('B', 18)\n  ('C', 21)\n  ('D', 19)\n  ('E', 23)\n\n# Assuming We Use an Unstable Sorting Algorithm to Sort the List by Age,\n# In the Result, the Relative Positions of ('D', 19) and ('A', 19) Are Changed,\n# And the Property That the Input Data Is Sorted by Name Is Lost\n  ('B', 18)\n  ('D', 19)\n  ('A', 19)\n  ('C', 21)\n  ('E', 23)\n

      Adaptability: Adaptive sorting can utilize the existing order information in the input data to reduce the amount of computation, achieving better time efficiency. The best-case time complexity of adaptive sorting algorithms is typically better than the average time complexity.

      Comparison-based or not: Comparison-based sorting relies on comparison operators (\\(<\\), \\(=\\), \\(>\\)) to determine the relative order of elements, thereby sorting the entire array, with a theoretical optimal time complexity of \\(O(n \\log n)\\). Non-comparison sorting does not use comparison operators and can achieve a time complexity of \\(O(n)\\), but its versatility is relatively limited.

      "},{"location":"chapter_sorting/sorting_algorithm/#1112-ideal-sorting-algorithm","title":"11.1.2 \u00a0 Ideal Sorting Algorithm","text":"

      Fast execution, in-place, stable, adaptive, good versatility. Clearly, no sorting algorithm has been discovered to date that combines all of these characteristics. Therefore, when selecting a sorting algorithm, it is necessary to decide based on the specific characteristics of the data and the requirements of the problem.

      Next, we will learn about various sorting algorithms together and analyze the advantages and disadvantages of each sorting algorithm based on the above evaluation dimensions.

      "},{"location":"chapter_sorting/summary/","title":"11.11 \u00a0 Summary","text":""},{"location":"chapter_sorting/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • Bubble sort achieves sorting by swapping adjacent elements. By adding a flag to enable early return, we can optimize the best-case time complexity of bubble sort to \\(O(n)\\).
      • Insertion sort completes sorting by inserting elements from the unsorted interval into the correct position in the sorted interval each round. Although the time complexity of insertion sort is \\(O(n^2)\\), it is very popular in small data volume sorting tasks because it involves relatively few unit operations.
      • Quick sort is implemented based on sentinel partitioning operations. In sentinel partitioning, it is possible to select the worst pivot every time, causing the time complexity to degrade to \\(O(n^2)\\). Introducing median pivot or random pivot can reduce the probability of such degradation. By preferentially recursing on the shorter sub-interval, the recursion depth can be effectively reduced, optimizing the space complexity to \\(O(\\log n)\\).
      • Merge sort includes two phases: divide and merge, which typically embody the divide-and-conquer strategy. In merge sort, sorting an array requires creating auxiliary arrays, with a space complexity of \\(O(n)\\); however, the space complexity of sorting a linked list can be optimized to \\(O(1)\\).
      • Bucket sort consists of three steps: distributing data into buckets, sorting within buckets, and merging results. It also embodies the divide-and-conquer strategy and is suitable for very large data volumes. The key to bucket sort is distributing data evenly.
      • Counting sort is a special case of bucket sort, which achieves sorting by counting the number of occurrences of data. Counting sort is suitable for situations where the data volume is large but the data range is limited, and requires that data can be converted to positive integers.
      • Radix sort achieves data sorting by sorting digit by digit, requiring that data can be represented as fixed-digit numbers.
      • Overall, we hope to find a sorting algorithm that is efficient, stable, in-place, and adaptive, with good versatility. However, just like other data structures and algorithms, no sorting algorithm has been found so far that simultaneously possesses all these characteristics. In practical applications, we need to select the appropriate sorting algorithm based on the specific characteristics of the data.
      • Figure 11-19 compares mainstream sorting algorithms in terms of efficiency, stability, in-place property, and adaptability.

      Figure 11-19 \u00a0 Sorting algorithm comparison

      "},{"location":"chapter_sorting/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

      Q: In what situations is the stability of sorting algorithms necessary?

      In reality, we may sort based on a certain attribute of objects. For example, students have two attributes: name and height. We want to implement multi-level sorting: first sort by name to get (A, 180) (B, 185) (C, 170) (D, 170); then sort by height. Because the sorting algorithm is unstable, we may get (D, 170) (C, 170) (A, 180) (B, 185).

      It can be seen that the positions of students D and C have been swapped, and the orderliness of names has been disrupted, which is something we don't want to see.

      Q: Can the order of \"searching from right to left\" and \"searching from left to right\" in sentinel partitioning be swapped?

      No. When we use the leftmost element as the pivot, we must first \"search from right to left\" and then \"search from left to right\". This conclusion is somewhat counterintuitive; let's analyze the reason.

      The last step of sentinel partitioning partition() is to swap nums[left] and nums[i]. After the swap is complete, the elements to the left of the pivot are all <= the pivot, which requires that nums[left] >= nums[i] must hold before the last swap. Suppose we first \"search from left to right\", then if we cannot find an element larger than the pivot, we will exit the loop when i == j, at which point it may be that nums[j] == nums[i] > nums[left]. In other words, the last swap operation will swap an element larger than the pivot to the leftmost end of the array, causing sentinel partitioning to fail.

      For example, given the array [0, 0, 0, 0, 1], if we first \"search from left to right\", the array after sentinel partitioning is [1, 0, 0, 0, 0], which is incorrect.

      Thinking deeper, if we select nums[right] as the pivot, then it's exactly the opposite - we must first \"search from left to right\".

      Q: Regarding the optimization of recursion depth in quick sort, why can selecting the shorter array ensure that the recursion depth does not exceed \\(\\log n\\)?

      The recursion depth is the number of currently unreturned recursive methods. Each round of sentinel partitioning divides the original array into two sub-arrays. After recursion depth optimization, the length of the sub-array to be recursively processed is at most half of the original array length. Assuming the worst case is always half the length, the final recursion depth will be \\(\\log n\\).

      Reviewing the original quick sort, we may continuously recurse on the longer array. In the worst case, it would be \\(n\\), \\(n - 1\\), \\(\\dots\\), \\(2\\), \\(1\\), with a recursion depth of \\(n\\). Recursion depth optimization can avoid this situation.

      Q: When all elements in the array are equal, is the time complexity of quick sort \\(O(n^2)\\)? How should this degenerate case be handled?

      Yes. For this situation, consider partitioning the array into three parts through sentinel partitioning: less than, equal to, and greater than the pivot. Only recursively process the less than and greater than parts. Under this method, an array where all input elements are equal can complete sorting in just one round of sentinel partitioning.

      Q: Why is the worst-case time complexity of bucket sort \\(O(n^2)\\)?

      In the worst case, all elements are distributed into the same bucket. If we use an \\(O(n^2)\\) algorithm to sort these elements, the time complexity will be \\(O(n^2)\\).

      "},{"location":"chapter_stack_and_queue/","title":"Chapter 5. \u00a0 Stack and Queue","text":"

      Abstract

      Stacks are like stacking cats, while queues are like cats lining up.

      They represent LIFO (Last In First Out) and FIFO (First In First Out) logic, respectively.

      "},{"location":"chapter_stack_and_queue/#chapter-contents","title":"Chapter contents","text":"
      • 5.1 \u00a0 Stack
      • 5.2 \u00a0 Queue
      • 5.3 \u00a0 Double-Ended Queue
      • 5.4 \u00a0 Summary
      "},{"location":"chapter_stack_and_queue/deque/","title":"5.3 \u00a0 Deque","text":"

      In a queue, we can only remove elements from the front or add elements at the rear. As shown in Figure 5-7, a double-ended queue (deque) provides greater flexibility, allowing the addition or removal of elements at both the front and rear.

      Figure 5-7 \u00a0 Operations of deque

      "},{"location":"chapter_stack_and_queue/deque/#531-common-deque-operations","title":"5.3.1 \u00a0 Common Deque Operations","text":"

      The common operations on a deque are shown in Table 5-3. The specific method names depend on the programming language used.

      Table 5-3 \u00a0 Efficiency of Deque Operations

      Method Description Time Complexity push_first() Add element to front \\(O(1)\\) push_last() Add element to rear \\(O(1)\\) pop_first() Remove front element \\(O(1)\\) pop_last() Remove rear element \\(O(1)\\) peek_first() Access front element \\(O(1)\\) peek_last() Access rear element \\(O(1)\\)

      Similarly, we can directly use the deque classes already implemented in programming languages:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby deque.py
      from collections import deque\n\n# Initialize deque\ndeq: deque[int] = deque()\n\n# Enqueue elements\ndeq.append(2)      # Add to rear\ndeq.append(5)\ndeq.append(4)\ndeq.appendleft(3)  # Add to front\ndeq.appendleft(1)\n\n# Access elements\nfront: int = deq[0]  # Front element\nrear: int = deq[-1]  # Rear element\n\n# Dequeue elements\npop_front: int = deq.popleft()  # Front element dequeue\npop_rear: int = deq.pop()       # Rear element dequeue\n\n# Get deque length\nsize: int = len(deq)\n\n# Check if deque is empty\nis_empty: bool = len(deq) == 0\n
      deque.cpp
      /* Initialize deque */\ndeque<int> deque;\n\n/* Enqueue elements */\ndeque.push_back(2);   // Add to rear\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3);  // Add to front\ndeque.push_front(1);\n\n/* Access elements */\nint front = deque.front(); // Front element\nint back = deque.back();   // Rear element\n\n/* Dequeue elements */\ndeque.pop_front();  // Front element dequeue\ndeque.pop_back();   // Rear element dequeue\n\n/* Get deque length */\nint size = deque.size();\n\n/* Check if deque is empty */\nbool empty = deque.empty();\n
      deque.java
      /* Initialize deque */\nDeque<Integer> deque = new LinkedList<>();\n\n/* Enqueue elements */\ndeque.offerLast(2);   // Add to rear\ndeque.offerLast(5);\ndeque.offerLast(4);\ndeque.offerFirst(3);  // Add to front\ndeque.offerFirst(1);\n\n/* Access elements */\nint peekFirst = deque.peekFirst();  // Front element\nint peekLast = deque.peekLast();    // Rear element\n\n/* Dequeue elements */\nint popFirst = deque.pollFirst();  // Front element dequeue\nint popLast = deque.pollLast();    // Rear element dequeue\n\n/* Get deque length */\nint size = deque.size();\n\n/* Check if deque is empty */\nboolean isEmpty = deque.isEmpty();\n
      deque.cs
      /* Initialize deque */\n// In C#, use LinkedList as a deque\nLinkedList<int> deque = new();\n\n/* Enqueue elements */\ndeque.AddLast(2);   // Add to rear\ndeque.AddLast(5);\ndeque.AddLast(4);\ndeque.AddFirst(3);  // Add to front\ndeque.AddFirst(1);\n\n/* Access elements */\nint peekFirst = deque.First.Value;  // Front element\nint peekLast = deque.Last.Value;    // Rear element\n\n/* Dequeue elements */\ndeque.RemoveFirst();  // Front element dequeue\ndeque.RemoveLast();   // Rear element dequeue\n\n/* Get deque length */\nint size = deque.Count;\n\n/* Check if deque is empty */\nbool isEmpty = deque.Count == 0;\n
      deque_test.go
      /* Initialize deque */\n// In Go, use list as a deque\ndeque := list.New()\n\n/* Enqueue elements */\ndeque.PushBack(2)      // Add to rear\ndeque.PushBack(5)\ndeque.PushBack(4)\ndeque.PushFront(3)     // Add to front\ndeque.PushFront(1)\n\n/* Access elements */\nfront := deque.Front() // Front element\nrear := deque.Back()   // Rear element\n\n/* Dequeue elements */\ndeque.Remove(front)    // Front element dequeue\ndeque.Remove(rear)     // Rear element dequeue\n\n/* Get deque length */\nsize := deque.Len()\n\n/* Check if deque is empty */\nisEmpty := deque.Len() == 0\n
      deque.swift
      /* Initialize deque */\n// Swift does not have a built-in deque class, can use Array as a deque\nvar deque: [Int] = []\n\n/* Enqueue elements */\ndeque.append(2) // Add to rear\ndeque.append(5)\ndeque.append(4)\ndeque.insert(3, at: 0) // Add to front\ndeque.insert(1, at: 0)\n\n/* Access elements */\nlet peekFirst = deque.first! // Front element\nlet peekLast = deque.last! // Rear element\n\n/* Dequeue elements */\n// When using Array simulation, popFirst has O(n) complexity\nlet popFirst = deque.removeFirst() // Front element dequeue\nlet popLast = deque.removeLast() // Rear element dequeue\n\n/* Get deque length */\nlet size = deque.count\n\n/* Check if deque is empty */\nlet isEmpty = deque.isEmpty\n
      deque.js
      /* Initialize deque */\n// JavaScript does not have a built-in deque, can only use Array as a deque\nconst deque = [];\n\n/* Enqueue elements */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// Please note that since it's an array, unshift() has O(n) time complexity\ndeque.unshift(3);\ndeque.unshift(1);\n\n/* Access elements */\nconst peekFirst = deque[0];\nconst peekLast = deque[deque.length - 1];\n\n/* Dequeue elements */\n// Please note that since it's an array, shift() has O(n) time complexity\nconst popFront = deque.shift();\nconst popBack = deque.pop();\n\n/* Get deque length */\nconst size = deque.length;\n\n/* Check if deque is empty */\nconst isEmpty = size === 0;\n
      deque.ts
      /* Initialize deque */\n// TypeScript does not have a built-in deque, can only use Array as a deque\nconst deque: number[] = [];\n\n/* Enqueue elements */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// Please note that since it's an array, unshift() has O(n) time complexity\ndeque.unshift(3);\ndeque.unshift(1);\n\n/* Access elements */\nconst peekFirst: number = deque[0];\nconst peekLast: number = deque[deque.length - 1];\n\n/* Dequeue elements */\n// Please note that since it's an array, shift() has O(n) time complexity\nconst popFront: number = deque.shift() as number;\nconst popBack: number = deque.pop() as number;\n\n/* Get deque length */\nconst size: number = deque.length;\n\n/* Check if deque is empty */\nconst isEmpty: boolean = size === 0;\n
      deque.dart
      /* Initialize deque */\n// In Dart, Queue is defined as a deque\nQueue<int> deque = Queue<int>();\n\n/* Enqueue elements */\ndeque.addLast(2);  // Add to rear\ndeque.addLast(5);\ndeque.addLast(4);\ndeque.addFirst(3); // Add to front\ndeque.addFirst(1);\n\n/* Access elements */\nint peekFirst = deque.first; // Front element\nint peekLast = deque.last;   // Rear element\n\n/* Dequeue elements */\nint popFirst = deque.removeFirst(); // Front element dequeue\nint popLast = deque.removeLast();   // Rear element dequeue\n\n/* Get deque length */\nint size = deque.length;\n\n/* Check if deque is empty */\nbool isEmpty = deque.isEmpty;\n
      deque.rs
      /* Initialize deque */\nlet mut deque: VecDeque<u32> = VecDeque::new();\n\n/* Enqueue elements */\ndeque.push_back(2);  // Add to rear\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3); // Add to front\ndeque.push_front(1);\n\n/* Access elements */\nif let Some(front) = deque.front() { // Front element\n}\nif let Some(rear) = deque.back() {   // Rear element\n}\n\n/* Dequeue elements */\nif let Some(pop_front) = deque.pop_front() { // Front element dequeue\n}\nif let Some(pop_rear) = deque.pop_back() {   // Rear element dequeue\n}\n\n/* Get deque length */\nlet size = deque.len();\n\n/* Check if deque is empty */\nlet is_empty = deque.is_empty();\n
      deque.c
      // C does not provide a built-in deque\n
      deque.kt
      /* Initialize deque */\nval deque = LinkedList<Int>()\n\n/* Enqueue elements */\ndeque.offerLast(2)  // Add to rear\ndeque.offerLast(5)\ndeque.offerLast(4)\ndeque.offerFirst(3) // Add to front\ndeque.offerFirst(1)\n\n/* Access elements */\nval peekFirst = deque.peekFirst() // Front element\nval peekLast = deque.peekLast()   // Rear element\n\n/* Dequeue elements */\nval popFirst = deque.pollFirst() // Front element dequeue\nval popLast = deque.pollLast()   // Rear element dequeue\n\n/* Get deque length */\nval size = deque.size\n\n/* Check if deque is empty */\nval isEmpty = deque.isEmpty()\n
      deque.rb
      # Initialize deque\n# Ruby does not have a built-in deque, can only use Array as a deque\ndeque = []\n\n# Enqueue elements\ndeque << 2\ndeque << 5\ndeque << 4\n# Please note that since it's an array, Array#unshift has O(n) time complexity\ndeque.unshift(3)\ndeque.unshift(1)\n\n# Access elements\npeek_first = deque.first\npeek_last = deque.last\n\n# Dequeue elements\n# Please note that since it's an array, Array#shift has O(n) time complexity\npop_front = deque.shift\npop_back = deque.pop\n\n# Get deque length\nsize = deque.length\n\n# Check if deque is empty\nis_empty = size.zero?\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_stack_and_queue/deque/#532-deque-implementation","title":"5.3.2 \u00a0 Deque Implementation *","text":"

      The implementation of a deque is similar to that of a queue. You can choose either a linked list or an array as the underlying data structure.

      "},{"location":"chapter_stack_and_queue/deque/#1-doubly-linked-list-implementation","title":"1. \u00a0 Doubly Linked List Implementation","text":"

      Reviewing the previous section, we used a regular singly linked list to implement a queue because it conveniently allows deleting the head node (corresponding to dequeue) and adding new nodes after the tail node (corresponding to enqueue).

      For a deque, both the front and rear can perform enqueue and dequeue operations. In other words, a deque needs to implement operations in the opposite direction as well. For this reason, we use a \"doubly linked list\" as the underlying data structure for the deque.

      As shown in Figure 5-8, we treat the head and tail nodes of the doubly linked list as the front and rear of the deque, implementing functionality to add and remove nodes at both ends.

      LinkedListDequepush_last()push_first()pop_last()pop_first()

      Figure 5-8 \u00a0 Enqueue and dequeue operations in linked list implementation of deque

      The implementation code is shown below:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linkedlist_deque.py
      class ListNode:\n    \"\"\"Doubly linked list node\"\"\"\n\n    def __init__(self, val: int):\n        \"\"\"Constructor\"\"\"\n        self.val: int = val\n        self.next: ListNode | None = None  # Successor node reference\n        self.prev: ListNode | None = None  # Predecessor node reference\n\nclass LinkedListDeque:\n    \"\"\"Double-ended queue based on doubly linked list implementation\"\"\"\n\n    def __init__(self):\n        \"\"\"Constructor\"\"\"\n        self._front: ListNode | None = None  # Head node front\n        self._rear: ListNode | None = None  # Tail node rear\n        self._size: int = 0  # Length of the double-ended queue\n\n    def size(self) -> int:\n        \"\"\"Get the length of the double-ended queue\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"Check if the double-ended queue is empty\"\"\"\n        return self._size == 0\n\n    def push(self, num: int, is_front: bool):\n        \"\"\"Enqueue operation\"\"\"\n        node = ListNode(num)\n        # If the linked list is empty, make both front and rear point to node\n        if self.is_empty():\n            self._front = self._rear = node\n        # Front of the queue enqueue operation\n        elif is_front:\n            # Add node to the head of the linked list\n            self._front.prev = node\n            node.next = self._front\n            self._front = node  # Update head node\n        # Rear of the queue enqueue operation\n        else:\n            # Add node to the tail of the linked list\n            self._rear.next = node\n            node.prev = self._rear\n            self._rear = node  # Update tail node\n        self._size += 1  # Update queue length\n\n    def push_first(self, num: int):\n        \"\"\"Front of the queue enqueue\"\"\"\n        self.push(num, True)\n\n    def push_last(self, num: int):\n        \"\"\"Rear of the queue enqueue\"\"\"\n        self.push(num, False)\n\n    def pop(self, is_front: bool) -> int:\n        \"\"\"Dequeue operation\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Double-ended queue is empty\")\n        # Front of the queue dequeue operation\n        if is_front:\n            val: int = self._front.val  # Temporarily store head node value\n            # Delete head node\n            fnext: ListNode | None = self._front.next\n            if fnext is not None:\n                fnext.prev = None\n                self._front.next = None\n            self._front = fnext  # Update head node\n        # Rear of the queue dequeue operation\n        else:\n            val: int = self._rear.val  # Temporarily store tail node value\n            # Delete tail node\n            rprev: ListNode | None = self._rear.prev\n            if rprev is not None:\n                rprev.next = None\n                self._rear.prev = None\n            self._rear = rprev  # Update tail node\n        self._size -= 1  # Update queue length\n        return val\n\n    def pop_first(self) -> int:\n        \"\"\"Front of the queue dequeue\"\"\"\n        return self.pop(True)\n\n    def pop_last(self) -> int:\n        \"\"\"Rear of the queue dequeue\"\"\"\n        return self.pop(False)\n\n    def peek_first(self) -> int:\n        \"\"\"Access front of the queue element\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Double-ended queue is empty\")\n        return self._front.val\n\n    def peek_last(self) -> int:\n        \"\"\"Access rear of the queue element\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Double-ended queue is empty\")\n        return self._rear.val\n\n    def to_array(self) -> list[int]:\n        \"\"\"Return array for printing\"\"\"\n        node = self._front\n        res = [0] * self.size()\n        for i in range(self.size()):\n            res[i] = node.val\n            node = node.next\n        return res\n
      linkedlist_deque.cpp
      /* Doubly linked list node */\nstruct DoublyListNode {\n    int val;              // Node value\n    DoublyListNode *next; // Successor node pointer\n    DoublyListNode *prev; // Predecessor node pointer\n    DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {\n    }\n};\n\n/* Double-ended queue based on doubly linked list implementation */\nclass LinkedListDeque {\n  private:\n    DoublyListNode *front, *rear; // Head node front, tail node rear\n    int queSize = 0;              // Length of the double-ended queue\n\n  public:\n    /* Constructor */\n    LinkedListDeque() : front(nullptr), rear(nullptr) {\n    }\n\n    /* Destructor */\n    ~LinkedListDeque() {\n        // Traverse linked list to delete nodes and free memory\n        DoublyListNode *pre, *cur = front;\n        while (cur != nullptr) {\n            pre = cur;\n            cur = cur->next;\n            delete pre;\n        }\n    }\n\n    /* Get the length of the double-ended queue */\n    int size() {\n        return queSize;\n    }\n\n    /* Check if the double-ended queue is empty */\n    bool isEmpty() {\n        return size() == 0;\n    }\n\n    /* Enqueue operation */\n    void push(int num, bool isFront) {\n        DoublyListNode *node = new DoublyListNode(num);\n        // If the linked list is empty, make both front and rear point to node\n        if (isEmpty())\n            front = rear = node;\n        // Front of the queue enqueue operation\n        else if (isFront) {\n            // Add node to the head of the linked list\n            front->prev = node;\n            node->next = front;\n            front = node; // Update head node\n        // Rear of the queue enqueue operation\n        } else {\n            // Add node to the tail of the linked list\n            rear->next = node;\n            node->prev = rear;\n            rear = node; // Update tail node\n        }\n        queSize++; // Update queue length\n    }\n\n    /* Front of the queue enqueue */\n    void pushFirst(int num) {\n        push(num, true);\n    }\n\n    /* Rear of the queue enqueue */\n    void pushLast(int num) {\n        push(num, false);\n    }\n\n    /* Dequeue operation */\n    int pop(bool isFront) {\n        if (isEmpty())\n            throw out_of_range(\"Queue is empty\");\n        int val;\n        // Temporarily store head node value\n        if (isFront) {\n            val = front->val; // Delete head node\n            // Delete head node\n            DoublyListNode *fNext = front->next;\n            if (fNext != nullptr) {\n                fNext->prev = nullptr;\n                front->next = nullptr;\n            }\n            delete front;\n            front = fNext; // Update head node\n        // Temporarily store tail node value\n        } else {\n            val = rear->val; // Delete tail node\n            // Update tail node\n            DoublyListNode *rPrev = rear->prev;\n            if (rPrev != nullptr) {\n                rPrev->next = nullptr;\n                rear->prev = nullptr;\n            }\n            delete rear;\n            rear = rPrev; // Update tail node\n        }\n        queSize--; // Update queue length\n        return val;\n    }\n\n    /* Rear of the queue dequeue */\n    int popFirst() {\n        return pop(true);\n    }\n\n    /* Access rear of the queue element */\n    int popLast() {\n        return pop(false);\n    }\n\n    /* Return list for printing */\n    int peekFirst() {\n        if (isEmpty())\n            throw out_of_range(\"Deque is empty\");\n        return front->val;\n    }\n\n    /* Driver Code */\n    int peekLast() {\n        if (isEmpty())\n            throw out_of_range(\"Deque is empty\");\n        return rear->val;\n    }\n\n    /* Return array for printing */\n    vector<int> toVector() {\n        DoublyListNode *node = front;\n        vector<int> res(size());\n        for (int i = 0; i < res.size(); i++) {\n            res[i] = node->val;\n            node = node->next;\n        }\n        return res;\n    }\n};\n
      linkedlist_deque.java
      /* Doubly linked list node */\nclass ListNode {\n    int val; // Node value\n    ListNode next; // Successor node reference\n    ListNode prev; // Predecessor node reference\n\n    ListNode(int val) {\n        this.val = val;\n        prev = next = null;\n    }\n}\n\n/* Double-ended queue based on doubly linked list implementation */\nclass LinkedListDeque {\n    private ListNode front, rear; // Head node front, tail node rear\n    private int queSize = 0; // Length of the double-ended queue\n\n    public LinkedListDeque() {\n        front = rear = null;\n    }\n\n    /* Get the length of the double-ended queue */\n    public int size() {\n        return queSize;\n    }\n\n    /* Check if the double-ended queue is empty */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* Enqueue operation */\n    private void push(int num, boolean isFront) {\n        ListNode node = new ListNode(num);\n        // If the linked list is empty, make both front and rear point to node\n        if (isEmpty())\n            front = rear = node;\n        // Front of the queue enqueue operation\n        else if (isFront) {\n            // Add node to the head of the linked list\n            front.prev = node;\n            node.next = front;\n            front = node; // Update head node\n        // Rear of the queue enqueue operation\n        } else {\n            // Add node to the tail of the linked list\n            rear.next = node;\n            node.prev = rear;\n            rear = node; // Update tail node\n        }\n        queSize++; // Update queue length\n    }\n\n    /* Front of the queue enqueue */\n    public void pushFirst(int num) {\n        push(num, true);\n    }\n\n    /* Rear of the queue enqueue */\n    public void pushLast(int num) {\n        push(num, false);\n    }\n\n    /* Dequeue operation */\n    private int pop(boolean isFront) {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        int val;\n        // Temporarily store head node value\n        if (isFront) {\n            val = front.val; // Delete head node\n            // Delete head node\n            ListNode fNext = front.next;\n            if (fNext != null) {\n                fNext.prev = null;\n                front.next = null;\n            }\n            front = fNext; // Update head node\n        // Temporarily store tail node value\n        } else {\n            val = rear.val; // Delete tail node\n            // Update tail node\n            ListNode rPrev = rear.prev;\n            if (rPrev != null) {\n                rPrev.next = null;\n                rear.prev = null;\n            }\n            rear = rPrev; // Update tail node\n        }\n        queSize--; // Update queue length\n        return val;\n    }\n\n    /* Rear of the queue dequeue */\n    public int popFirst() {\n        return pop(true);\n    }\n\n    /* Access rear of the queue element */\n    public int popLast() {\n        return pop(false);\n    }\n\n    /* Return list for printing */\n    public int peekFirst() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return front.val;\n    }\n\n    /* Driver Code */\n    public int peekLast() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return rear.val;\n    }\n\n    /* Return array for printing */\n    public int[] toArray() {\n        ListNode node = front;\n        int[] res = new int[size()];\n        for (int i = 0; i < res.length; i++) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
      linkedlist_deque.cs
      /* Doubly linked list node */\nclass ListNode(int val) {\n    public int val = val;       // Node value\n    public ListNode? next = null; // Successor node reference\n    public ListNode? prev = null; // Predecessor node reference\n}\n\n/* Double-ended queue based on doubly linked list implementation */\nclass LinkedListDeque {\n    ListNode? front, rear; // Head node front, tail node rear\n    int queSize = 0;      // Length of the double-ended queue\n\n    public LinkedListDeque() {\n        front = null;\n        rear = null;\n    }\n\n    /* Get the length of the double-ended queue */\n    public int Size() {\n        return queSize;\n    }\n\n    /* Check if the double-ended queue is empty */\n    public bool IsEmpty() {\n        return Size() == 0;\n    }\n\n    /* Enqueue operation */\n    void Push(int num, bool isFront) {\n        ListNode node = new(num);\n        // If the linked list is empty, make both front and rear point to node\n        if (IsEmpty()) {\n            front = node;\n            rear = node;\n        }\n        // Front of the queue enqueue operation\n        else if (isFront) {\n            // Add node to the head of the linked list\n            front!.prev = node;\n            node.next = front;\n            front = node; // Update head node\n        }\n        // Rear of the queue enqueue operation\n        else {\n            // Add node to the tail of the linked list\n            rear!.next = node;\n            node.prev = rear;\n            rear = node;  // Update tail node\n        }\n\n        queSize++; // Update queue length\n    }\n\n    /* Front of the queue enqueue */\n    public void PushFirst(int num) {\n        Push(num, true);\n    }\n\n    /* Rear of the queue enqueue */\n    public void PushLast(int num) {\n        Push(num, false);\n    }\n\n    /* Dequeue operation */\n    int? Pop(bool isFront) {\n        if (IsEmpty())\n            throw new Exception();\n        int? val;\n        // Temporarily store head node value\n        if (isFront) {\n            val = front?.val; // Delete head node\n            // Delete head node\n            ListNode? fNext = front?.next;\n            if (fNext != null) {\n                fNext.prev = null;\n                front!.next = null;\n            }\n            front = fNext;   // Update head node\n        }\n        // Temporarily store tail node value\n        else {\n            val = rear?.val;  // Delete tail node\n            // Update tail node\n            ListNode? rPrev = rear?.prev;\n            if (rPrev != null) {\n                rPrev.next = null;\n                rear!.prev = null;\n            }\n            rear = rPrev;    // Update tail node\n        }\n\n        queSize--; // Update queue length\n        return val;\n    }\n\n    /* Rear of the queue dequeue */\n    public int? PopFirst() {\n        return Pop(true);\n    }\n\n    /* Access rear of the queue element */\n    public int? PopLast() {\n        return Pop(false);\n    }\n\n    /* Return list for printing */\n    public int? PeekFirst() {\n        if (IsEmpty())\n            throw new Exception();\n        return front?.val;\n    }\n\n    /* Driver Code */\n    public int? PeekLast() {\n        if (IsEmpty())\n            throw new Exception();\n        return rear?.val;\n    }\n\n    /* Return array for printing */\n    public int?[] ToArray() {\n        ListNode? node = front;\n        int?[] res = new int?[Size()];\n        for (int i = 0; i < res.Length; i++) {\n            res[i] = node?.val;\n            node = node?.next;\n        }\n\n        return res;\n    }\n}\n
      linkedlist_deque.go
      /* Double-ended queue based on doubly linked list implementation */\ntype linkedListDeque struct {\n    // Use built-in package list\n    data *list.List\n}\n\n/* Initialize deque */\nfunc newLinkedListDeque() *linkedListDeque {\n    return &linkedListDeque{\n        data: list.New(),\n    }\n}\n\n/* Front element enqueue */\nfunc (s *linkedListDeque) pushFirst(value any) {\n    s.data.PushFront(value)\n}\n\n/* Rear element enqueue */\nfunc (s *linkedListDeque) pushLast(value any) {\n    s.data.PushBack(value)\n}\n\n/* Check if the double-ended queue is empty */\nfunc (s *linkedListDeque) popFirst() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Front()\n    s.data.Remove(e)\n    return e.Value\n}\n\n/* Rear element dequeue */\nfunc (s *linkedListDeque) popLast() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Back()\n    s.data.Remove(e)\n    return e.Value\n}\n\n/* Return list for printing */\nfunc (s *linkedListDeque) peekFirst() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Front()\n    return e.Value\n}\n\n/* Driver Code */\nfunc (s *linkedListDeque) peekLast() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Back()\n    return e.Value\n}\n\n/* Get the length of the queue */\nfunc (s *linkedListDeque) size() int {\n    return s.data.Len()\n}\n\n/* Check if the queue is empty */\nfunc (s *linkedListDeque) isEmpty() bool {\n    return s.data.Len() == 0\n}\n\n/* Get List for printing */\nfunc (s *linkedListDeque) toList() *list.List {\n    return s.data\n}\n
      linkedlist_deque.swift
      /* Doubly linked list node */\nclass ListNode {\n    var val: Int // Node value\n    var next: ListNode? // Successor node reference\n    weak var prev: ListNode? // Predecessor node reference\n\n    init(val: Int) {\n        self.val = val\n    }\n}\n\n/* Double-ended queue based on doubly linked list implementation */\nclass LinkedListDeque {\n    private var front: ListNode? // Head node front\n    private var rear: ListNode? // Tail node rear\n    private var _size: Int // Length of the double-ended queue\n\n    init() {\n        _size = 0\n    }\n\n    /* Get the length of the double-ended queue */\n    func size() -> Int {\n        _size\n    }\n\n    /* Check if the double-ended queue is empty */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* Enqueue operation */\n    private func push(num: Int, isFront: Bool) {\n        let node = ListNode(val: num)\n        // If the linked list is empty, make both front and rear point to node\n        if isEmpty() {\n            front = node\n            rear = node\n        }\n        // Front of the queue enqueue operation\n        else if isFront {\n            // Add node to the head of the linked list\n            front?.prev = node\n            node.next = front\n            front = node // Update head node\n        }\n        // Rear of the queue enqueue operation\n        else {\n            // Add node to the tail of the linked list\n            rear?.next = node\n            node.prev = rear\n            rear = node // Update tail node\n        }\n        _size += 1 // Update queue length\n    }\n\n    /* Front of the queue enqueue */\n    func pushFirst(num: Int) {\n        push(num: num, isFront: true)\n    }\n\n    /* Rear of the queue enqueue */\n    func pushLast(num: Int) {\n        push(num: num, isFront: false)\n    }\n\n    /* Dequeue operation */\n    private func pop(isFront: Bool) -> Int {\n        if isEmpty() {\n            fatalError(\"Deque is empty\")\n        }\n        let val: Int\n        // Temporarily store head node value\n        if isFront {\n            val = front!.val // Delete head node\n            // Delete head node\n            let fNext = front?.next\n            if fNext != nil {\n                fNext?.prev = nil\n                front?.next = nil\n            }\n            front = fNext // Update head node\n        }\n        // Temporarily store tail node value\n        else {\n            val = rear!.val // Delete tail node\n            // Update tail node\n            let rPrev = rear?.prev\n            if rPrev != nil {\n                rPrev?.next = nil\n                rear?.prev = nil\n            }\n            rear = rPrev // Update tail node\n        }\n        _size -= 1 // Update queue length\n        return val\n    }\n\n    /* Rear of the queue dequeue */\n    func popFirst() -> Int {\n        pop(isFront: true)\n    }\n\n    /* Access rear of the queue element */\n    func popLast() -> Int {\n        pop(isFront: false)\n    }\n\n    /* Return list for printing */\n    func peekFirst() -> Int {\n        if isEmpty() {\n            fatalError(\"Deque is empty\")\n        }\n        return front!.val\n    }\n\n    /* Driver Code */\n    func peekLast() -> Int {\n        if isEmpty() {\n            fatalError(\"Deque is empty\")\n        }\n        return rear!.val\n    }\n\n    /* Return array for printing */\n    func toArray() -> [Int] {\n        var node = front\n        var res = Array(repeating: 0, count: size())\n        for i in res.indices {\n            res[i] = node!.val\n            node = node?.next\n        }\n        return res\n    }\n}\n
      linkedlist_deque.js
      /* Doubly linked list node */\nclass ListNode {\n    prev; // Predecessor node reference (pointer)\n    next; // Successor node reference (pointer)\n    val; // Node value\n\n    constructor(val) {\n        this.val = val;\n        this.next = null;\n        this.prev = null;\n    }\n}\n\n/* Double-ended queue based on doubly linked list implementation */\nclass LinkedListDeque {\n    #front; // Head node front\n    #rear; // Tail node rear\n    #queSize; // Length of the double-ended queue\n\n    constructor() {\n        this.#front = null;\n        this.#rear = null;\n        this.#queSize = 0;\n    }\n\n    /* Rear of the queue enqueue operation */\n    pushLast(val) {\n        const node = new ListNode(val);\n        // If the linked list is empty, make both front and rear point to node\n        if (this.#queSize === 0) {\n            this.#front = node;\n            this.#rear = node;\n        } else {\n            // Add node to the tail of the linked list\n            this.#rear.next = node;\n            node.prev = this.#rear;\n            this.#rear = node; // Update tail node\n        }\n        this.#queSize++;\n    }\n\n    /* Front of the queue enqueue operation */\n    pushFirst(val) {\n        const node = new ListNode(val);\n        // If the linked list is empty, make both front and rear point to node\n        if (this.#queSize === 0) {\n            this.#front = node;\n            this.#rear = node;\n        } else {\n            // Add node to the head of the linked list\n            this.#front.prev = node;\n            node.next = this.#front;\n            this.#front = node; // Update head node\n        }\n        this.#queSize++;\n    }\n\n    /* Temporarily store tail node value */\n    popLast() {\n        if (this.#queSize === 0) {\n            return null;\n        }\n        const value = this.#rear.val; // Store tail node value\n        // Update tail node\n        let temp = this.#rear.prev;\n        if (temp !== null) {\n            temp.next = null;\n            this.#rear.prev = null;\n        }\n        this.#rear = temp; // Update tail node\n        this.#queSize--;\n        return value;\n    }\n\n    /* Temporarily store head node value */\n    popFirst() {\n        if (this.#queSize === 0) {\n            return null;\n        }\n        const value = this.#front.val; // Store tail node value\n        // Delete head node\n        let temp = this.#front.next;\n        if (temp !== null) {\n            temp.prev = null;\n            this.#front.next = null;\n        }\n        this.#front = temp; // Update head node\n        this.#queSize--;\n        return value;\n    }\n\n    /* Driver Code */\n    peekLast() {\n        return this.#queSize === 0 ? null : this.#rear.val;\n    }\n\n    /* Return list for printing */\n    peekFirst() {\n        return this.#queSize === 0 ? null : this.#front.val;\n    }\n\n    /* Get the length of the double-ended queue */\n    size() {\n        return this.#queSize;\n    }\n\n    /* Check if the double-ended queue is empty */\n    isEmpty() {\n        return this.#queSize === 0;\n    }\n\n    /* Print deque */\n    print() {\n        const arr = [];\n        let temp = this.#front;\n        while (temp !== null) {\n            arr.push(temp.val);\n            temp = temp.next;\n        }\n        console.log('[' + arr.join(', ') + ']');\n    }\n}\n
      linkedlist_deque.ts
      /* Doubly linked list node */\nclass ListNode {\n    prev: ListNode; // Predecessor node reference (pointer)\n    next: ListNode; // Successor node reference (pointer)\n    val: number; // Node value\n\n    constructor(val: number) {\n        this.val = val;\n        this.next = null;\n        this.prev = null;\n    }\n}\n\n/* Double-ended queue based on doubly linked list implementation */\nclass LinkedListDeque {\n    private front: ListNode; // Head node front\n    private rear: ListNode; // Tail node rear\n    private queSize: number; // Length of the double-ended queue\n\n    constructor() {\n        this.front = null;\n        this.rear = null;\n        this.queSize = 0;\n    }\n\n    /* Rear of the queue enqueue operation */\n    pushLast(val: number): void {\n        const node: ListNode = new ListNode(val);\n        // If the linked list is empty, make both front and rear point to node\n        if (this.queSize === 0) {\n            this.front = node;\n            this.rear = node;\n        } else {\n            // Add node to the tail of the linked list\n            this.rear.next = node;\n            node.prev = this.rear;\n            this.rear = node; // Update tail node\n        }\n        this.queSize++;\n    }\n\n    /* Front of the queue enqueue operation */\n    pushFirst(val: number): void {\n        const node: ListNode = new ListNode(val);\n        // If the linked list is empty, make both front and rear point to node\n        if (this.queSize === 0) {\n            this.front = node;\n            this.rear = node;\n        } else {\n            // Add node to the head of the linked list\n            this.front.prev = node;\n            node.next = this.front;\n            this.front = node; // Update head node\n        }\n        this.queSize++;\n    }\n\n    /* Temporarily store tail node value */\n    popLast(): number {\n        if (this.queSize === 0) {\n            return null;\n        }\n        const value: number = this.rear.val; // Store tail node value\n        // Update tail node\n        let temp: ListNode = this.rear.prev;\n        if (temp !== null) {\n            temp.next = null;\n            this.rear.prev = null;\n        }\n        this.rear = temp; // Update tail node\n        this.queSize--;\n        return value;\n    }\n\n    /* Temporarily store head node value */\n    popFirst(): number {\n        if (this.queSize === 0) {\n            return null;\n        }\n        const value: number = this.front.val; // Store tail node value\n        // Delete head node\n        let temp: ListNode = this.front.next;\n        if (temp !== null) {\n            temp.prev = null;\n            this.front.next = null;\n        }\n        this.front = temp; // Update head node\n        this.queSize--;\n        return value;\n    }\n\n    /* Driver Code */\n    peekLast(): number {\n        return this.queSize === 0 ? null : this.rear.val;\n    }\n\n    /* Return list for printing */\n    peekFirst(): number {\n        return this.queSize === 0 ? null : this.front.val;\n    }\n\n    /* Get the length of the double-ended queue */\n    size(): number {\n        return this.queSize;\n    }\n\n    /* Check if the double-ended queue is empty */\n    isEmpty(): boolean {\n        return this.queSize === 0;\n    }\n\n    /* Print deque */\n    print(): void {\n        const arr: number[] = [];\n        let temp: ListNode = this.front;\n        while (temp !== null) {\n            arr.push(temp.val);\n            temp = temp.next;\n        }\n        console.log('[' + arr.join(', ') + ']');\n    }\n}\n
      linkedlist_deque.dart
      /* Doubly linked list node */\nclass ListNode {\n  int val; // Node value\n  ListNode? next; // Successor node reference\n  ListNode? prev; // Predecessor node reference\n\n  ListNode(this.val, {this.next, this.prev});\n}\n\n/* Deque implemented based on doubly linked list */\nclass LinkedListDeque {\n  late ListNode? _front; // Head node _front\n  late ListNode? _rear; // Tail node _rear\n  int _queSize = 0; // Length of the double-ended queue\n\n  LinkedListDeque() {\n    this._front = null;\n    this._rear = null;\n  }\n\n  /* Get deque length */\n  int size() {\n    return this._queSize;\n  }\n\n  /* Check if the double-ended queue is empty */\n  bool isEmpty() {\n    return size() == 0;\n  }\n\n  /* Enqueue operation */\n  void push(int _num, bool isFront) {\n    final ListNode node = ListNode(_num);\n    if (isEmpty()) {\n      // If list is empty, let both _front and _rear point to node\n      _front = _rear = node;\n    } else if (isFront) {\n      // Front of the queue enqueue operation\n      // Add node to the head of the linked list\n      _front!.prev = node;\n      node.next = _front;\n      _front = node; // Update head node\n    } else {\n      // Rear of the queue enqueue operation\n      // Add node to the tail of the linked list\n      _rear!.next = node;\n      node.prev = _rear;\n      _rear = node; // Update tail node\n    }\n    _queSize++; // Update queue length\n  }\n\n  /* Front of the queue enqueue */\n  void pushFirst(int _num) {\n    push(_num, true);\n  }\n\n  /* Rear of the queue enqueue */\n  void pushLast(int _num) {\n    push(_num, false);\n  }\n\n  /* Dequeue operation */\n  int? pop(bool isFront) {\n    // If queue is empty, return null directly\n    if (isEmpty()) {\n      return null;\n    }\n    final int val;\n    if (isFront) {\n      // Temporarily store head node value\n      val = _front!.val; // Delete head node\n      // Delete head node\n      ListNode? fNext = _front!.next;\n      if (fNext != null) {\n        fNext.prev = null;\n        _front!.next = null;\n      }\n      _front = fNext; // Update head node\n    } else {\n      // Temporarily store tail node value\n      val = _rear!.val; // Delete tail node\n      // Update tail node\n      ListNode? rPrev = _rear!.prev;\n      if (rPrev != null) {\n        rPrev.next = null;\n        _rear!.prev = null;\n      }\n      _rear = rPrev; // Update tail node\n    }\n    _queSize--; // Update queue length\n    return val;\n  }\n\n  /* Rear of the queue dequeue */\n  int? popFirst() {\n    return pop(true);\n  }\n\n  /* Access rear of the queue element */\n  int? popLast() {\n    return pop(false);\n  }\n\n  /* Return list for printing */\n  int? peekFirst() {\n    return _front?.val;\n  }\n\n  /* Driver Code */\n  int? peekLast() {\n    return _rear?.val;\n  }\n\n  /* Return array for printing */\n  List<int> toArray() {\n    ListNode? node = _front;\n    final List<int> res = [];\n    for (int i = 0; i < _queSize; i++) {\n      res.add(node!.val);\n      node = node.next;\n    }\n    return res;\n  }\n}\n
      linkedlist_deque.rs
      /* Doubly linked list node */\npub struct ListNode<T> {\n    pub val: T,                                 // Node value\n    pub next: Option<Rc<RefCell<ListNode<T>>>>, // Successor node pointer\n    pub prev: Option<Rc<RefCell<ListNode<T>>>>, // Predecessor node pointer\n}\n\nimpl<T> ListNode<T> {\n    pub fn new(val: T) -> Rc<RefCell<ListNode<T>>> {\n        Rc::new(RefCell::new(ListNode {\n            val,\n            next: None,\n            prev: None,\n        }))\n    }\n}\n\n/* Double-ended queue based on doubly linked list implementation */\n#[allow(dead_code)]\npub struct LinkedListDeque<T> {\n    front: Option<Rc<RefCell<ListNode<T>>>>, // Head node front\n    rear: Option<Rc<RefCell<ListNode<T>>>>,  // Tail node rear\n    que_size: usize,                         // Length of the double-ended queue\n}\n\nimpl<T: Copy> LinkedListDeque<T> {\n    pub fn new() -> Self {\n        Self {\n            front: None,\n            rear: None,\n            que_size: 0,\n        }\n    }\n\n    /* Get the length of the double-ended queue */\n    pub fn size(&self) -> usize {\n        return self.que_size;\n    }\n\n    /* Check if the double-ended queue is empty */\n    pub fn is_empty(&self) -> bool {\n        return self.que_size == 0;\n    }\n\n    /* Enqueue operation */\n    fn push(&mut self, num: T, is_front: bool) {\n        let node = ListNode::new(num);\n        // Front of the queue enqueue operation\n        if is_front {\n            match self.front.take() {\n                // If the linked list is empty, make both front and rear point to node\n                None => {\n                    self.rear = Some(node.clone());\n                    self.front = Some(node);\n                }\n                // Add node to the head of the linked list\n                Some(old_front) => {\n                    old_front.borrow_mut().prev = Some(node.clone());\n                    node.borrow_mut().next = Some(old_front);\n                    self.front = Some(node); // Update head node\n                }\n            }\n        }\n        // Rear of the queue enqueue operation\n        else {\n            match self.rear.take() {\n                // If the linked list is empty, make both front and rear point to node\n                None => {\n                    self.front = Some(node.clone());\n                    self.rear = Some(node);\n                }\n                // Add node to the tail of the linked list\n                Some(old_rear) => {\n                    old_rear.borrow_mut().next = Some(node.clone());\n                    node.borrow_mut().prev = Some(old_rear);\n                    self.rear = Some(node); // Update tail node\n                }\n            }\n        }\n        self.que_size += 1; // Update queue length\n    }\n\n    /* Front of the queue enqueue */\n    pub fn push_first(&mut self, num: T) {\n        self.push(num, true);\n    }\n\n    /* Rear of the queue enqueue */\n    pub fn push_last(&mut self, num: T) {\n        self.push(num, false);\n    }\n\n    /* Dequeue operation */\n    fn pop(&mut self, is_front: bool) -> Option<T> {\n        // If queue is empty, return None directly\n        if self.is_empty() {\n            return None;\n        };\n        // Temporarily store head node value\n        if is_front {\n            self.front.take().map(|old_front| {\n                match old_front.borrow_mut().next.take() {\n                    Some(new_front) => {\n                        new_front.borrow_mut().prev.take();\n                        self.front = Some(new_front); // Update head node\n                    }\n                    None => {\n                        self.rear.take();\n                    }\n                }\n                self.que_size -= 1; // Update queue length\n                old_front.borrow().val\n            })\n        }\n        // Temporarily store tail node value\n        else {\n            self.rear.take().map(|old_rear| {\n                match old_rear.borrow_mut().prev.take() {\n                    Some(new_rear) => {\n                        new_rear.borrow_mut().next.take();\n                        self.rear = Some(new_rear); // Update tail node\n                    }\n                    None => {\n                        self.front.take();\n                    }\n                }\n                self.que_size -= 1; // Update queue length\n                old_rear.borrow().val\n            })\n        }\n    }\n\n    /* Rear of the queue dequeue */\n    pub fn pop_first(&mut self) -> Option<T> {\n        return self.pop(true);\n    }\n\n    /* Access rear of the queue element */\n    pub fn pop_last(&mut self) -> Option<T> {\n        return self.pop(false);\n    }\n\n    /* Return list for printing */\n    pub fn peek_first(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n        self.front.as_ref()\n    }\n\n    /* Driver Code */\n    pub fn peek_last(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n        self.rear.as_ref()\n    }\n\n    /* Return array for printing */\n    pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n        let mut res: Vec<T> = Vec::new();\n        fn recur<T: Copy>(cur: Option<&Rc<RefCell<ListNode<T>>>>, res: &mut Vec<T>) {\n            if let Some(cur) = cur {\n                res.push(cur.borrow().val);\n                recur(cur.borrow().next.as_ref(), res);\n            }\n        }\n\n        recur(head, &mut res);\n        res\n    }\n}\n
      linkedlist_deque.c
      /* Doubly linked list node */\ntypedef struct DoublyListNode {\n    int val;                     // Node value\n    struct DoublyListNode *next; // Successor node\n    struct DoublyListNode *prev; // Predecessor node\n} DoublyListNode;\n\n/* Constructor */\nDoublyListNode *newDoublyListNode(int num) {\n    DoublyListNode *new = (DoublyListNode *)malloc(sizeof(DoublyListNode));\n    new->val = num;\n    new->next = NULL;\n    new->prev = NULL;\n    return new;\n}\n\n/* Destructor */\nvoid delDoublyListNode(DoublyListNode *node) {\n    free(node);\n}\n\n/* Double-ended queue based on doubly linked list implementation */\ntypedef struct {\n    DoublyListNode *front, *rear; // Head node front, tail node rear\n    int queSize;                  // Length of the double-ended queue\n} LinkedListDeque;\n\n/* Constructor */\nLinkedListDeque *newLinkedListDeque() {\n    LinkedListDeque *deque = (LinkedListDeque *)malloc(sizeof(LinkedListDeque));\n    deque->front = NULL;\n    deque->rear = NULL;\n    deque->queSize = 0;\n    return deque;\n}\n\n/* Destructor */\nvoid delLinkedListdeque(LinkedListDeque *deque) {\n    // Free all nodes\n    for (int i = 0; i < deque->queSize && deque->front != NULL; i++) {\n        DoublyListNode *tmp = deque->front;\n        deque->front = deque->front->next;\n        free(tmp);\n    }\n    // Free deque structure\n    free(deque);\n}\n\n/* Get the length of the queue */\nint size(LinkedListDeque *deque) {\n    return deque->queSize;\n}\n\n/* Check if the queue is empty */\nbool empty(LinkedListDeque *deque) {\n    return (size(deque) == 0);\n}\n\n/* Enqueue */\nvoid push(LinkedListDeque *deque, int num, bool isFront) {\n    DoublyListNode *node = newDoublyListNode(num);\n    // If list is empty, set both front and rear to node\n    if (empty(deque)) {\n        deque->front = deque->rear = node;\n    }\n    // Front of the queue enqueue operation\n    else if (isFront) {\n        // Add node to the head of the linked list\n        deque->front->prev = node;\n        node->next = deque->front;\n        deque->front = node; // Update head node\n    }\n    // Rear of the queue enqueue operation\n    else {\n        // Add node to the tail of the linked list\n        deque->rear->next = node;\n        node->prev = deque->rear;\n        deque->rear = node;\n    }\n    deque->queSize++; // Update queue length\n}\n\n/* Front of the queue enqueue */\nvoid pushFirst(LinkedListDeque *deque, int num) {\n    push(deque, num, true);\n}\n\n/* Rear of the queue enqueue */\nvoid pushLast(LinkedListDeque *deque, int num) {\n    push(deque, num, false);\n}\n\n/* Return list for printing */\nint peekFirst(LinkedListDeque *deque) {\n    assert(size(deque) && deque->front);\n    return deque->front->val;\n}\n\n/* Driver Code */\nint peekLast(LinkedListDeque *deque) {\n    assert(size(deque) && deque->rear);\n    return deque->rear->val;\n}\n\n/* Dequeue */\nint pop(LinkedListDeque *deque, bool isFront) {\n    if (empty(deque))\n        return -1;\n    int val;\n    // Temporarily store head node value\n    if (isFront) {\n        val = peekFirst(deque); // Delete head node\n        DoublyListNode *fNext = deque->front->next;\n        if (fNext) {\n            fNext->prev = NULL;\n            deque->front->next = NULL;\n        }\n        delDoublyListNode(deque->front);\n        deque->front = fNext; // Update head node\n    }\n    // Temporarily store tail node value\n    else {\n        val = peekLast(deque); // Delete tail node\n        DoublyListNode *rPrev = deque->rear->prev;\n        if (rPrev) {\n            rPrev->next = NULL;\n            deque->rear->prev = NULL;\n        }\n        delDoublyListNode(deque->rear);\n        deque->rear = rPrev; // Update tail node\n    }\n    deque->queSize--; // Update queue length\n    return val;\n}\n\n/* Rear of the queue dequeue */\nint popFirst(LinkedListDeque *deque) {\n    return pop(deque, true);\n}\n\n/* Access rear of the queue element */\nint popLast(LinkedListDeque *deque) {\n    return pop(deque, false);\n}\n\n/* Print queue */\nvoid printLinkedListDeque(LinkedListDeque *deque) {\n    int *arr = malloc(sizeof(int) * deque->queSize);\n    // Copy data from list to array\n    int i;\n    DoublyListNode *node;\n    for (i = 0, node = deque->front; i < deque->queSize; i++) {\n        arr[i] = node->val;\n        node = node->next;\n    }\n    printArray(arr, deque->queSize);\n    free(arr);\n}\n
      linkedlist_deque.kt
      /* Doubly linked list node */\nclass ListNode(var _val: Int) {\n    // Node value\n    var next: ListNode? = null // Successor node reference\n    var prev: ListNode? = null // Predecessor node reference\n}\n\n/* Double-ended queue based on doubly linked list implementation */\nclass LinkedListDeque {\n    private var front: ListNode? = null // Head node front\n    private var rear: ListNode? = null // Tail node rear\n    private var queSize: Int = 0 // Length of the double-ended queue\n\n    /* Get the length of the double-ended queue */\n    fun size(): Int {\n        return queSize\n    }\n\n    /* Check if the double-ended queue is empty */\n    fun isEmpty(): Boolean {\n        return size() == 0\n    }\n\n    /* Enqueue operation */\n    fun push(num: Int, isFront: Boolean) {\n        val node = ListNode(num)\n        // If the linked list is empty, make both front and rear point to node\n        if (isEmpty()) {\n            rear = node\n            front = rear\n            // Front of the queue enqueue operation\n        } else if (isFront) {\n            // Add node to the head of the linked list\n            front?.prev = node\n            node.next = front\n            front = node // Update head node\n            // Rear of the queue enqueue operation\n        } else {\n            // Add node to the tail of the linked list\n            rear?.next = node\n            node.prev = rear\n            rear = node // Update tail node\n        }\n        queSize++ // Update queue length\n    }\n\n    /* Front of the queue enqueue */\n    fun pushFirst(num: Int) {\n        push(num, true)\n    }\n\n    /* Rear of the queue enqueue */\n    fun pushLast(num: Int) {\n        push(num, false)\n    }\n\n    /* Dequeue operation */\n    fun pop(isFront: Boolean): Int {\n        if (isEmpty()) \n            throw IndexOutOfBoundsException()\n        val _val: Int\n        // Temporarily store head node value\n        if (isFront) {\n            _val = front!!._val // Delete head node\n            // Delete head node\n            val fNext = front!!.next\n            if (fNext != null) {\n                fNext.prev = null\n                front!!.next = null\n            }\n            front = fNext // Update head node\n            // Temporarily store tail node value\n        } else {\n            _val = rear!!._val // Delete tail node\n            // Update tail node\n            val rPrev = rear!!.prev\n            if (rPrev != null) {\n                rPrev.next = null\n                rear!!.prev = null\n            }\n            rear = rPrev // Update tail node\n        }\n        queSize-- // Update queue length\n        return _val\n    }\n\n    /* Rear of the queue dequeue */\n    fun popFirst(): Int {\n        return pop(true)\n    }\n\n    /* Access rear of the queue element */\n    fun popLast(): Int {\n        return pop(false)\n    }\n\n    /* Return list for printing */\n    fun peekFirst(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return front!!._val\n    }\n\n    /* Driver Code */\n    fun peekLast(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return rear!!._val\n    }\n\n    /* Return array for printing */\n    fun toArray(): IntArray {\n        var node = front\n        val res = IntArray(size())\n        for (i in res.indices) {\n            res[i] = node!!._val\n            node = node.next\n        }\n        return res\n    }\n}\n
      linkedlist_deque.rb
      =begin\nFile: linkedlist_deque.rb\nCreated Time: 2024-04-06\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n### Doubly linked list node\nclass ListNode\n  attr_accessor :val\n  attr_accessor :next # Successor node reference\n  attr_accessor :prev # Predecessor node reference\n\n  ### Constructor ###\n  def initialize(val)\n    @val = val\n  end\nend\n\n### Deque based on doubly linked list ###\nclass LinkedListDeque\n  ### Get deque length ###\n  attr_reader :size\n\n  ### Constructor ###\n  def initialize\n    @front = nil  # Head node front\n    @rear = nil   # Tail node rear\n    @size = 0     # Length of the double-ended queue\n  end\n\n  ### Check if deque is empty ###\n  def is_empty?\n    size.zero?\n  end\n\n  ### Enqueue operation ###\n  def push(num, is_front)\n    node = ListNode.new(num)\n    # If list is empty, set both front and rear to node\n    if is_empty?\n      @front = @rear = node\n    # Front of the queue enqueue operation\n    elsif is_front\n      # Add node to the head of the linked list\n      @front.prev = node\n      node.next = @front\n      @front = node # Update head node\n    # Rear of the queue enqueue operation\n    else\n      # Add node to the tail of the linked list\n      @rear.next = node\n      node.prev = @rear\n      @rear = node # Update tail node\n    end\n    @size += 1 # Update queue length\n  end\n\n  ### Enqueue at front ###\n  def push_first(num)\n    push(num, true)\n  end\n\n  ### Enqueue at rear ###\n  def push_last(num)\n    push(num, false)\n  end\n\n  ### Dequeue operation ###\n  def pop(is_front)\n    raise IndexError, 'Deque is empty' if is_empty?\n\n    # Temporarily store head node value\n    if is_front\n      val = @front.val # Delete head node\n      # Delete head node\n      fnext = @front.next\n      unless fnext.nil?\n        fnext.prev = nil\n        @front.next = nil\n      end\n      @front = fnext # Update head node\n    # Temporarily store tail node value\n    else\n      val = @rear.val # Delete tail node\n      # Update tail node\n      rprev = @rear.prev\n      unless rprev.nil?\n        rprev.next = nil\n        @rear.prev = nil\n      end\n      @rear = rprev # Update tail node\n    end\n    @size -= 1 # Update queue length\n\n    val\n  end\n\n  ### Dequeue from front ###\n  def pop_first\n    pop(true)\n  end\n\n  ### Dequeue from front ###\n  def pop_last\n    pop(false)\n  end\n\n  ### Access front element ###\n  def peek_first\n    raise IndexError, 'Deque is empty' if is_empty?\n\n    @front.val\n  end\n\n  ### Access rear element ###\n  def peek_last\n    raise IndexError, 'Deque is empty' if is_empty?\n\n    @rear.val\n  end\n\n  ### Return array for printing ###\n  def to_array\n    node = @front\n    res = Array.new(size, 0)\n    for i in 0...size\n      res[i] = node.val\n      node = node.next\n    end\n    res\n  end\nend\n
      "},{"location":"chapter_stack_and_queue/deque/#2-array-implementation","title":"2. \u00a0 Array Implementation","text":"

      As shown in Figure 5-9, similar to implementing a queue based on an array, we can also use a circular array to implement a deque.

      ArrayDequepush_last()push_first()pop_last()pop_first()

      Figure 5-9 \u00a0 Enqueue and dequeue operations in array implementation of deque

      Based on the queue implementation, we only need to add methods for \"enqueue at front\" and \"dequeue from rear\":

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_deque.py
      class ArrayDeque:\n    \"\"\"Double-ended queue based on circular array implementation\"\"\"\n\n    def __init__(self, capacity: int):\n        \"\"\"Constructor\"\"\"\n        self._nums: list[int] = [0] * capacity\n        self._front: int = 0\n        self._size: int = 0\n\n    def capacity(self) -> int:\n        \"\"\"Get the capacity of the double-ended queue\"\"\"\n        return len(self._nums)\n\n    def size(self) -> int:\n        \"\"\"Get the length of the double-ended queue\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"Check if the double-ended queue is empty\"\"\"\n        return self._size == 0\n\n    def index(self, i: int) -> int:\n        \"\"\"Calculate circular array index\"\"\"\n        # Use modulo operation to wrap the array head and tail together\n        # When i passes the tail of the array, return to the head\n        # When i passes the head of the array, return to the tail\n        return (i + self.capacity()) % self.capacity()\n\n    def push_first(self, num: int):\n        \"\"\"Front of the queue enqueue\"\"\"\n        if self._size == self.capacity():\n            print(\"Double-ended queue is full\")\n            return\n        # Front pointer moves one position to the left\n        # Use modulo operation to wrap front around to the tail after passing the head of the array\n        self._front = self.index(self._front - 1)\n        # Add num to the front of the queue\n        self._nums[self._front] = num\n        self._size += 1\n\n    def push_last(self, num: int):\n        \"\"\"Rear of the queue enqueue\"\"\"\n        if self._size == self.capacity():\n            print(\"Double-ended queue is full\")\n            return\n        # Calculate rear pointer, points to rear index + 1\n        rear = self.index(self._front + self._size)\n        # Add num to the rear of the queue\n        self._nums[rear] = num\n        self._size += 1\n\n    def pop_first(self) -> int:\n        \"\"\"Front of the queue dequeue\"\"\"\n        num = self.peek_first()\n        # Front pointer moves one position backward\n        self._front = self.index(self._front + 1)\n        self._size -= 1\n        return num\n\n    def pop_last(self) -> int:\n        \"\"\"Rear of the queue dequeue\"\"\"\n        num = self.peek_last()\n        self._size -= 1\n        return num\n\n    def peek_first(self) -> int:\n        \"\"\"Access front of the queue element\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Double-ended queue is empty\")\n        return self._nums[self._front]\n\n    def peek_last(self) -> int:\n        \"\"\"Access rear of the queue element\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Double-ended queue is empty\")\n        # Calculate tail element index\n        last = self.index(self._front + self._size - 1)\n        return self._nums[last]\n\n    def to_array(self) -> list[int]:\n        \"\"\"Return array for printing\"\"\"\n        # Only convert list elements within the valid length range\n        res = []\n        for i in range(self._size):\n            res.append(self._nums[self.index(self._front + i)])\n        return res\n
      array_deque.cpp
      /* Double-ended queue based on circular array implementation */\nclass ArrayDeque {\n  private:\n    vector<int> nums; // Array for storing double-ended queue elements\n    int front;        // Front pointer, points to the front of the queue element\n    int queSize;      // Double-ended queue length\n\n  public:\n    /* Constructor */\n    ArrayDeque(int capacity) {\n        nums.resize(capacity);\n        front = queSize = 0;\n    }\n\n    /* Get the capacity of the double-ended queue */\n    int capacity() {\n        return nums.size();\n    }\n\n    /* Get the length of the double-ended queue */\n    int size() {\n        return queSize;\n    }\n\n    /* Check if the double-ended queue is empty */\n    bool isEmpty() {\n        return queSize == 0;\n    }\n\n    /* Calculate circular array index */\n    int index(int i) {\n        // Use modulo operation to wrap the array head and tail together\n        // When i passes the tail of the array, return to the head\n        // When i passes the head of the array, return to the tail\n        return (i + capacity()) % capacity();\n    }\n\n    /* Front of the queue enqueue */\n    void pushFirst(int num) {\n        if (queSize == capacity()) {\n            cout << \"Double-ended queue is full\" << endl;\n            return;\n        }\n        // Use modulo operation to wrap front around to the tail after passing the head of the array\n        // Add num to the front of the queue\n        front = index(front - 1);\n        // Add num to front of queue\n        nums[front] = num;\n        queSize++;\n    }\n\n    /* Rear of the queue enqueue */\n    void pushLast(int num) {\n        if (queSize == capacity()) {\n            cout << \"Double-ended queue is full\" << endl;\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        int rear = index(front + queSize);\n        // Front pointer moves one position backward\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* Rear of the queue dequeue */\n    int popFirst() {\n        int num = peekFirst();\n        // Move front pointer backward by one position\n        front = index(front + 1);\n        queSize--;\n        return num;\n    }\n\n    /* Access rear of the queue element */\n    int popLast() {\n        int num = peekLast();\n        queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    int peekFirst() {\n        if (isEmpty())\n            throw out_of_range(\"Deque is empty\");\n        return nums[front];\n    }\n\n    /* Driver Code */\n    int peekLast() {\n        if (isEmpty())\n            throw out_of_range(\"Deque is empty\");\n        // Initialize double-ended queue\n        int last = index(front + queSize - 1);\n        return nums[last];\n    }\n\n    /* Return array for printing */\n    vector<int> toVector() {\n        // Elements enqueue\n        vector<int> res(queSize);\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[index(j)];\n        }\n        return res;\n    }\n};\n
      array_deque.java
      /* Double-ended queue based on circular array implementation */\nclass ArrayDeque {\n    private int[] nums; // Array for storing double-ended queue elements\n    private int front; // Front pointer, points to the front of the queue element\n    private int queSize; // Double-ended queue length\n\n    /* Constructor */\n    public ArrayDeque(int capacity) {\n        this.nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* Get the capacity of the double-ended queue */\n    public int capacity() {\n        return nums.length;\n    }\n\n    /* Get the length of the double-ended queue */\n    public int size() {\n        return queSize;\n    }\n\n    /* Check if the double-ended queue is empty */\n    public boolean isEmpty() {\n        return queSize == 0;\n    }\n\n    /* Calculate circular array index */\n    private int index(int i) {\n        // Use modulo operation to wrap the array head and tail together\n        // When i passes the tail of the array, return to the head\n        // When i passes the head of the array, return to the tail\n        return (i + capacity()) % capacity();\n    }\n\n    /* Front of the queue enqueue */\n    public void pushFirst(int num) {\n        if (queSize == capacity()) {\n            System.out.println(\"Double-ended queue is full\");\n            return;\n        }\n        // Use modulo operation to wrap front around to the tail after passing the head of the array\n        // Add num to the front of the queue\n        front = index(front - 1);\n        // Add num to front of queue\n        nums[front] = num;\n        queSize++;\n    }\n\n    /* Rear of the queue enqueue */\n    public void pushLast(int num) {\n        if (queSize == capacity()) {\n            System.out.println(\"Double-ended queue is full\");\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        int rear = index(front + queSize);\n        // Front pointer moves one position backward\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* Rear of the queue dequeue */\n    public int popFirst() {\n        int num = peekFirst();\n        // Move front pointer backward by one position\n        front = index(front + 1);\n        queSize--;\n        return num;\n    }\n\n    /* Access rear of the queue element */\n    public int popLast() {\n        int num = peekLast();\n        queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    public int peekFirst() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return nums[front];\n    }\n\n    /* Driver Code */\n    public int peekLast() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        // Initialize double-ended queue\n        int last = index(front + queSize - 1);\n        return nums[last];\n    }\n\n    /* Return array for printing */\n    public int[] toArray() {\n        // Elements enqueue\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[index(j)];\n        }\n        return res;\n    }\n}\n
      array_deque.cs
      /* Double-ended queue based on circular array implementation */\nclass ArrayDeque {\n    int[] nums;  // Array for storing double-ended queue elements\n    int front;   // Front pointer, points to the front of the queue element\n    int queSize; // Double-ended queue length\n\n    /* Constructor */\n    public ArrayDeque(int capacity) {\n        nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* Get the capacity of the double-ended queue */\n    int Capacity() {\n        return nums.Length;\n    }\n\n    /* Get the length of the double-ended queue */\n    public int Size() {\n        return queSize;\n    }\n\n    /* Check if the double-ended queue is empty */\n    public bool IsEmpty() {\n        return queSize == 0;\n    }\n\n    /* Calculate circular array index */\n    int Index(int i) {\n        // Use modulo operation to wrap the array head and tail together\n        // When i passes the tail of the array, return to the head\n        // When i passes the head of the array, return to the tail\n        return (i + Capacity()) % Capacity();\n    }\n\n    /* Front of the queue enqueue */\n    public void PushFirst(int num) {\n        if (queSize == Capacity()) {\n            Console.WriteLine(\"Double-ended queue is full\");\n            return;\n        }\n        // Use modulo operation to wrap front around to the tail after passing the head of the array\n        // Add num to the front of the queue\n        front = Index(front - 1);\n        // Add num to front of queue\n        nums[front] = num;\n        queSize++;\n    }\n\n    /* Rear of the queue enqueue */\n    public void PushLast(int num) {\n        if (queSize == Capacity()) {\n            Console.WriteLine(\"Double-ended queue is full\");\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        int rear = Index(front + queSize);\n        // Front pointer moves one position backward\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* Rear of the queue dequeue */\n    public int PopFirst() {\n        int num = PeekFirst();\n        // Move front pointer backward by one position\n        front = Index(front + 1);\n        queSize--;\n        return num;\n    }\n\n    /* Access rear of the queue element */\n    public int PopLast() {\n        int num = PeekLast();\n        queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    public int PeekFirst() {\n        if (IsEmpty()) {\n            throw new InvalidOperationException();\n        }\n        return nums[front];\n    }\n\n    /* Driver Code */\n    public int PeekLast() {\n        if (IsEmpty()) {\n            throw new InvalidOperationException();\n        }\n        // Initialize double-ended queue\n        int last = Index(front + queSize - 1);\n        return nums[last];\n    }\n\n    /* Return array for printing */\n    public int[] ToArray() {\n        // Elements enqueue\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[Index(j)];\n        }\n        return res;\n    }\n}\n
      array_deque.go
      /* Double-ended queue based on circular array implementation */\ntype arrayDeque struct {\n    nums        []int // Array for storing double-ended queue elements\n    front       int   // Front pointer, points to the front of the queue element\n    queSize     int   // Double-ended queue length\n    queCapacity int   // Queue capacity (maximum number of elements)\n}\n\n/* Access front of the queue element */\nfunc newArrayDeque(queCapacity int) *arrayDeque {\n    return &arrayDeque{\n        nums:        make([]int, queCapacity),\n        queCapacity: queCapacity,\n        front:       0,\n        queSize:     0,\n    }\n}\n\n/* Get the length of the double-ended queue */\nfunc (q *arrayDeque) size() int {\n    return q.queSize\n}\n\n/* Check if the double-ended queue is empty */\nfunc (q *arrayDeque) isEmpty() bool {\n    return q.queSize == 0\n}\n\n/* Calculate circular array index */\nfunc (q *arrayDeque) index(i int) int {\n    // Use modulo operation to wrap the array head and tail together\n    // When i passes the tail of the array, return to the head\n    // When i passes the head of the array, return to the tail\n    return (i + q.queCapacity) % q.queCapacity\n}\n\n/* Front of the queue enqueue */\nfunc (q *arrayDeque) pushFirst(num int) {\n    if q.queSize == q.queCapacity {\n        fmt.Println(\"Double-ended queue is full\")\n        return\n    }\n    // Use modulo operation to wrap front around to the tail after passing the head of the array\n    // Add num to the front of the queue\n    q.front = q.index(q.front - 1)\n    // Add num to front of queue\n    q.nums[q.front] = num\n    q.queSize++\n}\n\n/* Rear of the queue enqueue */\nfunc (q *arrayDeque) pushLast(num int) {\n    if q.queSize == q.queCapacity {\n        fmt.Println(\"Double-ended queue is full\")\n        return\n    }\n    // Use modulo operation to wrap rear around to the head after passing the tail of the array\n    rear := q.index(q.front + q.queSize)\n    // Front pointer moves one position backward\n    q.nums[rear] = num\n    q.queSize++\n}\n\n/* Rear of the queue dequeue */\nfunc (q *arrayDeque) popFirst() any {\n    num := q.peekFirst()\n    if num == nil {\n        return nil\n    }\n    // Move front pointer backward by one position\n    q.front = q.index(q.front + 1)\n    q.queSize--\n    return num\n}\n\n/* Access rear of the queue element */\nfunc (q *arrayDeque) popLast() any {\n    num := q.peekLast()\n    if num == nil {\n        return nil\n    }\n    q.queSize--\n    return num\n}\n\n/* Return list for printing */\nfunc (q *arrayDeque) peekFirst() any {\n    if q.isEmpty() {\n        return nil\n    }\n    return q.nums[q.front]\n}\n\n/* Driver Code */\nfunc (q *arrayDeque) peekLast() any {\n    if q.isEmpty() {\n        return nil\n    }\n    // Initialize double-ended queue\n    last := q.index(q.front + q.queSize - 1)\n    return q.nums[last]\n}\n\n/* Get Slice for printing */\nfunc (q *arrayDeque) toSlice() []int {\n    // Elements enqueue\n    res := make([]int, q.queSize)\n    for i, j := 0, q.front; i < q.queSize; i++ {\n        res[i] = q.nums[q.index(j)]\n        j++\n    }\n    return res\n}\n
      array_deque.swift
      /* Double-ended queue based on circular array implementation */\nclass ArrayDeque {\n    private var nums: [Int] // Array for storing double-ended queue elements\n    private var front: Int // Front pointer, points to the front of the queue element\n    private var _size: Int // Double-ended queue length\n\n    /* Constructor */\n    init(capacity: Int) {\n        nums = Array(repeating: 0, count: capacity)\n        front = 0\n        _size = 0\n    }\n\n    /* Get the capacity of the double-ended queue */\n    func capacity() -> Int {\n        nums.count\n    }\n\n    /* Get the length of the double-ended queue */\n    func size() -> Int {\n        _size\n    }\n\n    /* Check if the double-ended queue is empty */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* Calculate circular array index */\n    private func index(i: Int) -> Int {\n        // Use modulo operation to wrap the array head and tail together\n        // When i passes the tail of the array, return to the head\n        // When i passes the head of the array, return to the tail\n        (i + capacity()) % capacity()\n    }\n\n    /* Front of the queue enqueue */\n    func pushFirst(num: Int) {\n        if size() == capacity() {\n            print(\"Double-ended queue is full\")\n            return\n        }\n        // Use modulo operation to wrap front around to the tail after passing the head of the array\n        // Add num to the front of the queue\n        front = index(i: front - 1)\n        // Add num to front of queue\n        nums[front] = num\n        _size += 1\n    }\n\n    /* Rear of the queue enqueue */\n    func pushLast(num: Int) {\n        if size() == capacity() {\n            print(\"Double-ended queue is full\")\n            return\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        let rear = index(i: front + size())\n        // Front pointer moves one position backward\n        nums[rear] = num\n        _size += 1\n    }\n\n    /* Rear of the queue dequeue */\n    func popFirst() -> Int {\n        let num = peekFirst()\n        // Move front pointer backward by one position\n        front = index(i: front + 1)\n        _size -= 1\n        return num\n    }\n\n    /* Access rear of the queue element */\n    func popLast() -> Int {\n        let num = peekLast()\n        _size -= 1\n        return num\n    }\n\n    /* Return list for printing */\n    func peekFirst() -> Int {\n        if isEmpty() {\n            fatalError(\"Deque is empty\")\n        }\n        return nums[front]\n    }\n\n    /* Driver Code */\n    func peekLast() -> Int {\n        if isEmpty() {\n            fatalError(\"Deque is empty\")\n        }\n        // Initialize double-ended queue\n        let last = index(i: front + size() - 1)\n        return nums[last]\n    }\n\n    /* Return array for printing */\n    func toArray() -> [Int] {\n        // Elements enqueue\n        (front ..< front + size()).map { nums[index(i: $0)] }\n    }\n}\n
      array_deque.js
      /* Double-ended queue based on circular array implementation */\nclass ArrayDeque {\n    #nums; // Array for storing double-ended queue elements\n    #front; // Front pointer, points to the front of the queue element\n    #queSize; // Double-ended queue length\n\n    /* Constructor */\n    constructor(capacity) {\n        this.#nums = new Array(capacity);\n        this.#front = 0;\n        this.#queSize = 0;\n    }\n\n    /* Get the capacity of the double-ended queue */\n    capacity() {\n        return this.#nums.length;\n    }\n\n    /* Get the length of the double-ended queue */\n    size() {\n        return this.#queSize;\n    }\n\n    /* Check if the double-ended queue is empty */\n    isEmpty() {\n        return this.#queSize === 0;\n    }\n\n    /* Calculate circular array index */\n    index(i) {\n        // Use modulo operation to wrap the array head and tail together\n        // When i passes the tail of the array, return to the head\n        // When i passes the head of the array, return to the tail\n        return (i + this.capacity()) % this.capacity();\n    }\n\n    /* Front of the queue enqueue */\n    pushFirst(num) {\n        if (this.#queSize === this.capacity()) {\n            console.log('Double-ended queue is full');\n            return;\n        }\n        // Use modulo operation to wrap front around to the tail after passing the head of the array\n        // Add num to the front of the queue\n        this.#front = this.index(this.#front - 1);\n        // Add num to front of queue\n        this.#nums[this.#front] = num;\n        this.#queSize++;\n    }\n\n    /* Rear of the queue enqueue */\n    pushLast(num) {\n        if (this.#queSize === this.capacity()) {\n            console.log('Double-ended queue is full');\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        const rear = this.index(this.#front + this.#queSize);\n        // Front pointer moves one position backward\n        this.#nums[rear] = num;\n        this.#queSize++;\n    }\n\n    /* Rear of the queue dequeue */\n    popFirst() {\n        const num = this.peekFirst();\n        // Move front pointer backward by one position\n        this.#front = this.index(this.#front + 1);\n        this.#queSize--;\n        return num;\n    }\n\n    /* Access rear of the queue element */\n    popLast() {\n        const num = this.peekLast();\n        this.#queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peekFirst() {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        return this.#nums[this.#front];\n    }\n\n    /* Driver Code */\n    peekLast() {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        // Initialize double-ended queue\n        const last = this.index(this.#front + this.#queSize - 1);\n        return this.#nums[last];\n    }\n\n    /* Return array for printing */\n    toArray() {\n        // Elements enqueue\n        const res = [];\n        for (let i = 0, j = this.#front; i < this.#queSize; i++, j++) {\n            res[i] = this.#nums[this.index(j)];\n        }\n        return res;\n    }\n}\n
      array_deque.ts
      /* Double-ended queue based on circular array implementation */\nclass ArrayDeque {\n    private nums: number[]; // Array for storing double-ended queue elements\n    private front: number; // Front pointer, points to the front of the queue element\n    private queSize: number; // Double-ended queue length\n\n    /* Constructor */\n    constructor(capacity: number) {\n        this.nums = new Array(capacity);\n        this.front = 0;\n        this.queSize = 0;\n    }\n\n    /* Get the capacity of the double-ended queue */\n    capacity(): number {\n        return this.nums.length;\n    }\n\n    /* Get the length of the double-ended queue */\n    size(): number {\n        return this.queSize;\n    }\n\n    /* Check if the double-ended queue is empty */\n    isEmpty(): boolean {\n        return this.queSize === 0;\n    }\n\n    /* Calculate circular array index */\n    index(i: number): number {\n        // Use modulo operation to wrap the array head and tail together\n        // When i passes the tail of the array, return to the head\n        // When i passes the head of the array, return to the tail\n        return (i + this.capacity()) % this.capacity();\n    }\n\n    /* Front of the queue enqueue */\n    pushFirst(num: number): void {\n        if (this.queSize === this.capacity()) {\n            console.log('Double-ended queue is full');\n            return;\n        }\n        // Use modulo operation to wrap front around to the tail after passing the head of the array\n        // Add num to the front of the queue\n        this.front = this.index(this.front - 1);\n        // Add num to front of queue\n        this.nums[this.front] = num;\n        this.queSize++;\n    }\n\n    /* Rear of the queue enqueue */\n    pushLast(num: number): void {\n        if (this.queSize === this.capacity()) {\n            console.log('Double-ended queue is full');\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        const rear: number = this.index(this.front + this.queSize);\n        // Front pointer moves one position backward\n        this.nums[rear] = num;\n        this.queSize++;\n    }\n\n    /* Rear of the queue dequeue */\n    popFirst(): number {\n        const num: number = this.peekFirst();\n        // Move front pointer backward by one position\n        this.front = this.index(this.front + 1);\n        this.queSize--;\n        return num;\n    }\n\n    /* Access rear of the queue element */\n    popLast(): number {\n        const num: number = this.peekLast();\n        this.queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peekFirst(): number {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        return this.nums[this.front];\n    }\n\n    /* Driver Code */\n    peekLast(): number {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        // Initialize double-ended queue\n        const last = this.index(this.front + this.queSize - 1);\n        return this.nums[last];\n    }\n\n    /* Return array for printing */\n    toArray(): number[] {\n        // Elements enqueue\n        const res: number[] = [];\n        for (let i = 0, j = this.front; i < this.queSize; i++, j++) {\n            res[i] = this.nums[this.index(j)];\n        }\n        return res;\n    }\n}\n
      array_deque.dart
      /* Double-ended queue based on circular array implementation */\nclass ArrayDeque {\n  late List<int> _nums; // Array for storing double-ended queue elements\n  late int _front; // Front pointer, points to the front of the queue element\n  late int _queSize; // Double-ended queue length\n\n  /* Constructor */\n  ArrayDeque(int capacity) {\n    this._nums = List.filled(capacity, 0);\n    this._front = this._queSize = 0;\n  }\n\n  /* Get the capacity of the double-ended queue */\n  int capacity() {\n    return _nums.length;\n  }\n\n  /* Get the length of the double-ended queue */\n  int size() {\n    return _queSize;\n  }\n\n  /* Check if the double-ended queue is empty */\n  bool isEmpty() {\n    return _queSize == 0;\n  }\n\n  /* Calculate circular array index */\n  int index(int i) {\n    // Use modulo operation to wrap the array head and tail together\n    // When i passes the tail of the array, return to the head\n    // When i passes the head of the array, return to the tail\n    return (i + capacity()) % capacity();\n  }\n\n  /* Front of the queue enqueue */\n  void pushFirst(int _num) {\n    if (_queSize == capacity()) {\n      throw Exception(\"Double-ended queue is full\");\n    }\n    // Use modulo operation to wrap front around to the tail after passing the head of the array\n    // Use modulo operation to wrap _front from array head back to tail\n    _front = index(_front - 1);\n    // Add _num to queue front\n    _nums[_front] = _num;\n    _queSize++;\n  }\n\n  /* Rear of the queue enqueue */\n  void pushLast(int _num) {\n    if (_queSize == capacity()) {\n      throw Exception(\"Double-ended queue is full\");\n    }\n    // Use modulo operation to wrap rear around to the head after passing the tail of the array\n    int rear = index(_front + _queSize);\n    // Add _num to queue rear\n    _nums[rear] = _num;\n    _queSize++;\n  }\n\n  /* Rear of the queue dequeue */\n  int popFirst() {\n    int _num = peekFirst();\n    // Move front pointer right by one\n    _front = index(_front + 1);\n    _queSize--;\n    return _num;\n  }\n\n  /* Access rear of the queue element */\n  int popLast() {\n    int _num = peekLast();\n    _queSize--;\n    return _num;\n  }\n\n  /* Return list for printing */\n  int peekFirst() {\n    if (isEmpty()) {\n      throw Exception(\"Deque is empty\");\n    }\n    return _nums[_front];\n  }\n\n  /* Driver Code */\n  int peekLast() {\n    if (isEmpty()) {\n      throw Exception(\"Deque is empty\");\n    }\n    // Initialize double-ended queue\n    int last = index(_front + _queSize - 1);\n    return _nums[last];\n  }\n\n  /* Return array for printing */\n  List<int> toArray() {\n    // Elements enqueue\n    List<int> res = List.filled(_queSize, 0);\n    for (int i = 0, j = _front; i < _queSize; i++, j++) {\n      res[i] = _nums[index(j)];\n    }\n    return res;\n  }\n}\n
      array_deque.rs
      /* Double-ended queue based on circular array implementation */\nstruct ArrayDeque<T> {\n    nums: Vec<T>,    // Array for storing double-ended queue elements\n    front: usize,    // Front pointer, points to the front of the queue element\n    que_size: usize, // Double-ended queue length\n}\n\nimpl<T: Copy + Default> ArrayDeque<T> {\n    /* Constructor */\n    pub fn new(capacity: usize) -> Self {\n        Self {\n            nums: vec![T::default(); capacity],\n            front: 0,\n            que_size: 0,\n        }\n    }\n\n    /* Get the capacity of the double-ended queue */\n    pub fn capacity(&self) -> usize {\n        self.nums.len()\n    }\n\n    /* Get the length of the double-ended queue */\n    pub fn size(&self) -> usize {\n        self.que_size\n    }\n\n    /* Check if the double-ended queue is empty */\n    pub fn is_empty(&self) -> bool {\n        self.que_size == 0\n    }\n\n    /* Calculate circular array index */\n    fn index(&self, i: i32) -> usize {\n        // Use modulo operation to wrap the array head and tail together\n        // When i passes the tail of the array, return to the head\n        // When i passes the head of the array, return to the tail\n        ((i + self.capacity() as i32) % self.capacity() as i32) as usize\n    }\n\n    /* Front of the queue enqueue */\n    pub fn push_first(&mut self, num: T) {\n        if self.que_size == self.capacity() {\n            println!(\"Double-ended queue is full\");\n            return;\n        }\n        // Use modulo operation to wrap front around to the tail after passing the head of the array\n        // Add num to the front of the queue\n        self.front = self.index(self.front as i32 - 1);\n        // Add num to front of queue\n        self.nums[self.front] = num;\n        self.que_size += 1;\n    }\n\n    /* Rear of the queue enqueue */\n    pub fn push_last(&mut self, num: T) {\n        if self.que_size == self.capacity() {\n            println!(\"Double-ended queue is full\");\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        let rear = self.index(self.front as i32 + self.que_size as i32);\n        // Front pointer moves one position backward\n        self.nums[rear] = num;\n        self.que_size += 1;\n    }\n\n    /* Rear of the queue dequeue */\n    fn pop_first(&mut self) -> T {\n        let num = self.peek_first();\n        // Move front pointer backward by one position\n        self.front = self.index(self.front as i32 + 1);\n        self.que_size -= 1;\n        num\n    }\n\n    /* Access rear of the queue element */\n    fn pop_last(&mut self) -> T {\n        let num = self.peek_last();\n        self.que_size -= 1;\n        num\n    }\n\n    /* Return list for printing */\n    fn peek_first(&self) -> T {\n        if self.is_empty() {\n            panic!(\"Deque is empty\")\n        };\n        self.nums[self.front]\n    }\n\n    /* Driver Code */\n    fn peek_last(&self) -> T {\n        if self.is_empty() {\n            panic!(\"Deque is empty\")\n        };\n        // Initialize double-ended queue\n        let last = self.index(self.front as i32 + self.que_size as i32 - 1);\n        self.nums[last]\n    }\n\n    /* Return array for printing */\n    fn to_array(&self) -> Vec<T> {\n        // Elements enqueue\n        let mut res = vec![T::default(); self.que_size];\n        let mut j = self.front;\n        for i in 0..self.que_size {\n            res[i] = self.nums[self.index(j as i32)];\n            j += 1;\n        }\n        res\n    }\n}\n
      array_deque.c
      /* Double-ended queue based on circular array implementation */\ntypedef struct {\n    int *nums;       // Array for storing queue elements\n    int front;       // Front pointer, points to the front of the queue element\n    int queSize;     // Rear pointer, points to rear + 1\n    int queCapacity; // Queue capacity\n} ArrayDeque;\n\n/* Constructor */\nArrayDeque *newArrayDeque(int capacity) {\n    ArrayDeque *deque = (ArrayDeque *)malloc(sizeof(ArrayDeque));\n    // Initialize array\n    deque->queCapacity = capacity;\n    deque->nums = (int *)malloc(sizeof(int) * deque->queCapacity);\n    deque->front = deque->queSize = 0;\n    return deque;\n}\n\n/* Destructor */\nvoid delArrayDeque(ArrayDeque *deque) {\n    free(deque->nums);\n    free(deque);\n}\n\n/* Get the capacity of the double-ended queue */\nint capacity(ArrayDeque *deque) {\n    return deque->queCapacity;\n}\n\n/* Get the length of the double-ended queue */\nint size(ArrayDeque *deque) {\n    return deque->queSize;\n}\n\n/* Check if the double-ended queue is empty */\nbool empty(ArrayDeque *deque) {\n    return deque->queSize == 0;\n}\n\n/* Calculate circular array index */\nint dequeIndex(ArrayDeque *deque, int i) {\n    // Use modulo operation to wrap the array head and tail together\n    // When i exceeds array end, wrap to head\n    // When i passes the head of the array, return to the tail\n    return ((i + capacity(deque)) % capacity(deque));\n}\n\n/* Front of the queue enqueue */\nvoid pushFirst(ArrayDeque *deque, int num) {\n    if (deque->queSize == capacity(deque)) {\n        printf(\"Deque is full\\r\\n\");\n        return;\n    }\n    // Use modulo operation to wrap front around to the tail after passing the head of the array\n    // Use modulo to wrap front from array head to rear\n    deque->front = dequeIndex(deque, deque->front - 1);\n    // Add num to queue front\n    deque->nums[deque->front] = num;\n    deque->queSize++;\n}\n\n/* Rear of the queue enqueue */\nvoid pushLast(ArrayDeque *deque, int num) {\n    if (deque->queSize == capacity(deque)) {\n        printf(\"Deque is full\\r\\n\");\n        return;\n    }\n    // Use modulo operation to wrap rear around to the head after passing the tail of the array\n    int rear = dequeIndex(deque, deque->front + deque->queSize);\n    // Front pointer moves one position backward\n    deque->nums[rear] = num;\n    deque->queSize++;\n}\n\n/* Return list for printing */\nint peekFirst(ArrayDeque *deque) {\n    // Access error: Deque is empty\n    assert(empty(deque) == 0);\n    return deque->nums[deque->front];\n}\n\n/* Driver Code */\nint peekLast(ArrayDeque *deque) {\n    // Access error: Deque is empty\n    assert(empty(deque) == 0);\n    int last = dequeIndex(deque, deque->front + deque->queSize - 1);\n    return deque->nums[last];\n}\n\n/* Rear of the queue dequeue */\nint popFirst(ArrayDeque *deque) {\n    int num = peekFirst(deque);\n    // Move front pointer backward by one position\n    deque->front = dequeIndex(deque, deque->front + 1);\n    deque->queSize--;\n    return num;\n}\n\n/* Access rear of the queue element */\nint popLast(ArrayDeque *deque) {\n    int num = peekLast(deque);\n    deque->queSize--;\n    return num;\n}\n\n/* Return array for printing */\nint *toArray(ArrayDeque *deque, int *queSize) {\n    *queSize = deque->queSize;\n    int *res = (int *)calloc(deque->queSize, sizeof(int));\n    int j = deque->front;\n    for (int i = 0; i < deque->queSize; i++) {\n        res[i] = deque->nums[j % deque->queCapacity];\n        j++;\n    }\n    return res;\n}\n
      array_deque.kt
      /* Constructor */\nclass ArrayDeque(capacity: Int) {\n    private var nums: IntArray = IntArray(capacity) // Array for storing double-ended queue elements\n    private var front: Int = 0 // Front pointer, points to the front of the queue element\n    private var queSize: Int = 0 // Double-ended queue length\n\n    /* Get the capacity of the double-ended queue */\n    fun capacity(): Int {\n        return nums.size\n    }\n\n    /* Get the length of the double-ended queue */\n    fun size(): Int {\n        return queSize\n    }\n\n    /* Check if the double-ended queue is empty */\n    fun isEmpty(): Boolean {\n        return queSize == 0\n    }\n\n    /* Calculate circular array index */\n    private fun index(i: Int): Int {\n        // Use modulo operation to wrap the array head and tail together\n        // When i passes the tail of the array, return to the head\n        // When i passes the head of the array, return to the tail\n        return (i + capacity()) % capacity()\n    }\n\n    /* Front of the queue enqueue */\n    fun pushFirst(num: Int) {\n        if (queSize == capacity()) {\n            println(\"Double-ended queue is full\")\n            return\n        }\n        // Use modulo operation to wrap front around to the tail after passing the head of the array\n        // Add num to the front of the queue\n        front = index(front - 1)\n        // Add num to front of queue\n        nums[front] = num\n        queSize++\n    }\n\n    /* Rear of the queue enqueue */\n    fun pushLast(num: Int) {\n        if (queSize == capacity()) {\n            println(\"Double-ended queue is full\")\n            return\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        val rear = index(front + queSize)\n        // Front pointer moves one position backward\n        nums[rear] = num\n        queSize++\n    }\n\n    /* Rear of the queue dequeue */\n    fun popFirst(): Int {\n        val num = peekFirst()\n        // Move front pointer backward by one position\n        front = index(front + 1)\n        queSize--\n        return num\n    }\n\n    /* Access rear of the queue element */\n    fun popLast(): Int {\n        val num = peekLast()\n        queSize--\n        return num\n    }\n\n    /* Return list for printing */\n    fun peekFirst(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return nums[front]\n    }\n\n    /* Driver Code */\n    fun peekLast(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        // Initialize double-ended queue\n        val last = index(front + queSize - 1)\n        return nums[last]\n    }\n\n    /* Return array for printing */\n    fun toArray(): IntArray {\n        // Elements enqueue\n        val res = IntArray(queSize)\n        var i = 0\n        var j = front\n        while (i < queSize) {\n            res[i] = nums[index(j)]\n            i++\n            j++\n        }\n        return res\n    }\n}\n
      array_deque.rb
      ### Deque based on circular array ###\nclass ArrayDeque\n  ### Get deque length ###\n  attr_reader :size\n\n  ### Constructor ###\n  def initialize(capacity)\n    @nums = Array.new(capacity, 0)\n    @front = 0\n    @size = 0\n  end\n\n  ### Get deque capacity ###\n  def capacity\n    @nums.length\n  end\n\n  ### Check if deque is empty ###\n  def is_empty?\n    size.zero?\n  end\n\n  ### Enqueue at front ###\n  def push_first(num)\n    if size == capacity\n      puts 'Double-ended queue is full'\n      return\n    end\n\n    # Use modulo operation to wrap front around to the tail after passing the head of the array\n    # Add num to the front of the queue\n    @front = index(@front - 1)\n    # Add num to front of queue\n    @nums[@front] = num\n    @size += 1\n  end\n\n  ### Enqueue at rear ###\n  def push_last(num)\n    if size == capacity\n      puts 'Double-ended queue is full'\n      return\n    end\n\n    # Use modulo operation to wrap rear around to the head after passing the tail of the array\n    rear = index(@front + size)\n    # Front pointer moves one position backward\n    @nums[rear] = num\n    @size += 1\n  end\n\n  ### Dequeue from front ###\n  def pop_first\n    num = peek_first\n    # Move front pointer backward by one position\n    @front = index(@front + 1)\n    @size -= 1\n    num\n  end\n\n  ### Dequeue from rear ###\n  def pop_last\n    num = peek_last\n    @size -= 1\n    num\n  end\n\n  ### Access front element ###\n  def peek_first\n    raise IndexError, 'Deque is empty' if is_empty?\n\n    @nums[@front]\n  end\n\n  ### Access rear element ###\n  def peek_last\n    raise IndexError, 'Deque is empty' if is_empty?\n\n    # Initialize double-ended queue\n    last = index(@front + size - 1)\n    @nums[last]\n  end\n\n  ### Return array for printing ###\n  def to_array\n    # Elements enqueue\n    res = []\n    for i in 0...size\n      res << @nums[index(@front + i)]\n    end\n    res\n  end\n\n  private\n\n  ### Calculate circular array index ###\n  def index(i)\n    # Use modulo operation to wrap the array head and tail together\n    # When i passes the tail of the array, return to the head\n    # When i passes the head of the array, return to the tail\n    (i + capacity) % capacity\n  end\nend\n
      "},{"location":"chapter_stack_and_queue/deque/#533-deque-applications","title":"5.3.3 \u00a0 Deque Applications","text":"

      A deque combines the logic of both stacks and queues. Therefore, it can implement all application scenarios of both, while providing greater flexibility.

      We know that the \"undo\" function in software is typically implemented using a stack: the system pushes each change operation onto the stack and then implements undo through pop. However, considering system resource limitations, software usually limits the number of undo steps (for example, only allowing 50 steps to be saved). When the stack length exceeds 50, the software needs to perform a deletion operation at the bottom of the stack (front of the queue). But a stack cannot implement this functionality, so a deque is needed to replace the stack. Note that the core logic of \"undo\" still follows the LIFO principle of a stack; it's just that the deque can more flexibly implement some additional logic.

      "},{"location":"chapter_stack_and_queue/queue/","title":"5.2 \u00a0 Queue","text":"

      A queue is a linear data structure that follows the First In First Out (FIFO) rule. As the name suggests, a queue simulates the phenomenon of lining up, where newcomers continuously join the end of the queue, while people at the front of the queue leave one by one.

      As shown in Figure 5-4, we call the front of the queue the \"front\" and the end the \"rear.\" The operation of adding an element to the rear is called \"enqueue,\" and the operation of removing the front element is called \"dequeue.\"

      Figure 5-4 \u00a0 FIFO rule of queue

      "},{"location":"chapter_stack_and_queue/queue/#521-common-queue-operations","title":"5.2.1 \u00a0 Common Queue Operations","text":"

      The common operations on a queue are shown in Table 5-2. Note that method names may vary across different programming languages. We adopt the same naming convention as for stacks here.

      Table 5-2 \u00a0 Efficiency of Queue Operations

      Method Description Time Complexity push() Enqueue element, add element to rear \\(O(1)\\) pop() Dequeue front element \\(O(1)\\) peek() Access front element \\(O(1)\\)

      We can directly use the ready-made queue classes in programming languages:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby queue.py
      from collections import deque\n\n# Initialize queue\n# In Python, we generally use the deque class as a queue\n# Although queue.Queue() is a pure queue class, it is not very user-friendly, so it is not recommended\nque: deque[int] = deque()\n\n# Enqueue elements\nque.append(1)\nque.append(3)\nque.append(2)\nque.append(5)\nque.append(4)\n\n# Access front element\nfront: int = que[0]\n\n# Dequeue element\npop: int = que.popleft()\n\n# Get queue length\nsize: int = len(que)\n\n# Check if queue is empty\nis_empty: bool = len(que) == 0\n
      queue.cpp
      /* Initialize queue */\nqueue<int> queue;\n\n/* Enqueue elements */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* Access front element */\nint front = queue.front();\n\n/* Dequeue element */\nqueue.pop();\n\n/* Get queue length */\nint size = queue.size();\n\n/* Check if queue is empty */\nbool empty = queue.empty();\n
      queue.java
      /* Initialize queue */\nQueue<Integer> queue = new LinkedList<>();\n\n/* Enqueue elements */\nqueue.offer(1);\nqueue.offer(3);\nqueue.offer(2);\nqueue.offer(5);\nqueue.offer(4);\n\n/* Access front element */\nint peek = queue.peek();\n\n/* Dequeue element */\nint pop = queue.poll();\n\n/* Get queue length */\nint size = queue.size();\n\n/* Check if queue is empty */\nboolean isEmpty = queue.isEmpty();\n
      queue.cs
      /* Initialize queue */\nQueue<int> queue = new();\n\n/* Enqueue elements */\nqueue.Enqueue(1);\nqueue.Enqueue(3);\nqueue.Enqueue(2);\nqueue.Enqueue(5);\nqueue.Enqueue(4);\n\n/* Access front element */\nint peek = queue.Peek();\n\n/* Dequeue element */\nint pop = queue.Dequeue();\n\n/* Get queue length */\nint size = queue.Count;\n\n/* Check if queue is empty */\nbool isEmpty = queue.Count == 0;\n
      queue_test.go
      /* Initialize queue */\n// In Go, use list as a queue\nqueue := list.New()\n\n/* Enqueue elements */\nqueue.PushBack(1)\nqueue.PushBack(3)\nqueue.PushBack(2)\nqueue.PushBack(5)\nqueue.PushBack(4)\n\n/* Access front element */\npeek := queue.Front()\n\n/* Dequeue element */\npop := queue.Front()\nqueue.Remove(pop)\n\n/* Get queue length */\nsize := queue.Len()\n\n/* Check if queue is empty */\nisEmpty := queue.Len() == 0\n
      queue.swift
      /* Initialize queue */\n// Swift does not have a built-in queue class, can use Array as a queue\nvar queue: [Int] = []\n\n/* Enqueue elements */\nqueue.append(1)\nqueue.append(3)\nqueue.append(2)\nqueue.append(5)\nqueue.append(4)\n\n/* Access front element */\nlet peek = queue.first!\n\n/* Dequeue element */\n// Since it's an array, removeFirst has O(n) complexity\nlet pool = queue.removeFirst()\n\n/* Get queue length */\nlet size = queue.count\n\n/* Check if queue is empty */\nlet isEmpty = queue.isEmpty\n
      queue.js
      /* Initialize queue */\n// JavaScript does not have a built-in queue, can use Array as a queue\nconst queue = [];\n\n/* Enqueue elements */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* Access front element */\nconst peek = queue[0];\n\n/* Dequeue element */\n// The underlying structure is an array, so shift() has O(n) time complexity\nconst pop = queue.shift();\n\n/* Get queue length */\nconst size = queue.length;\n\n/* Check if queue is empty */\nconst empty = queue.length === 0;\n
      queue.ts
      /* Initialize queue */\n// TypeScript does not have a built-in queue, can use Array as a queue\nconst queue: number[] = [];\n\n/* Enqueue elements */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* Access front element */\nconst peek = queue[0];\n\n/* Dequeue element */\n// The underlying structure is an array, so shift() has O(n) time complexity\nconst pop = queue.shift();\n\n/* Get queue length */\nconst size = queue.length;\n\n/* Check if queue is empty */\nconst empty = queue.length === 0;\n
      queue.dart
      /* Initialize queue */\n// In Dart, the Queue class is a deque and can also be used as a queue\nQueue<int> queue = Queue();\n\n/* Enqueue elements */\nqueue.add(1);\nqueue.add(3);\nqueue.add(2);\nqueue.add(5);\nqueue.add(4);\n\n/* Access front element */\nint peek = queue.first;\n\n/* Dequeue element */\nint pop = queue.removeFirst();\n\n/* Get queue length */\nint size = queue.length;\n\n/* Check if queue is empty */\nbool isEmpty = queue.isEmpty;\n
      queue.rs
      /* Initialize deque */\n// In Rust, use deque as a regular queue\nlet mut deque: VecDeque<u32> = VecDeque::new();\n\n/* Enqueue elements */\ndeque.push_back(1);\ndeque.push_back(3);\ndeque.push_back(2);\ndeque.push_back(5);\ndeque.push_back(4);\n\n/* Access front element */\nif let Some(front) = deque.front() {\n}\n\n/* Dequeue element */\nif let Some(pop) = deque.pop_front() {\n}\n\n/* Get queue length */\nlet size = deque.len();\n\n/* Check if queue is empty */\nlet is_empty = deque.is_empty();\n
      queue.c
      // C does not provide a built-in queue\n
      queue.kt
      /* Initialize queue */\nval queue = LinkedList<Int>()\n\n/* Enqueue elements */\nqueue.offer(1)\nqueue.offer(3)\nqueue.offer(2)\nqueue.offer(5)\nqueue.offer(4)\n\n/* Access front element */\nval peek = queue.peek()\n\n/* Dequeue element */\nval pop = queue.poll()\n\n/* Get queue length */\nval size = queue.size\n\n/* Check if queue is empty */\nval isEmpty = queue.isEmpty()\n
      queue.rb
      # Initialize queue\n# Ruby's built-in queue (Thread::Queue) does not have peek and traversal methods, can use Array as a queue\nqueue = []\n\n# Enqueue elements\nqueue.push(1)\nqueue.push(3)\nqueue.push(2)\nqueue.push(5)\nqueue.push(4)\n\n# Access front element\npeek = queue.first\n\n# Dequeue element\n# Please note that since it's an array, Array#shift has O(n) time complexity\npop = queue.shift\n\n# Get queue length\nsize = queue.length\n\n# Check if queue is empty\nis_empty = queue.empty?\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_stack_and_queue/queue/#522-queue-implementation","title":"5.2.2 \u00a0 Queue Implementation","text":"

      To implement a queue, we need a data structure that allows adding elements at one end and removing elements at the other end. Both linked lists and arrays meet this requirement.

      "},{"location":"chapter_stack_and_queue/queue/#1-linked-list-implementation","title":"1. \u00a0 Linked List Implementation","text":"

      As shown in Figure 5-5, we can treat the \"head node\" and \"tail node\" of a linked list as the \"front\" and \"rear\" of the queue, respectively, with the rule that nodes can only be added at the rear and removed from the front.

      LinkedListQueuepush()pop()

      Figure 5-5 \u00a0 Enqueue and dequeue operations in linked list implementation of queue

      Below is the code for implementing a queue using a linked list:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linkedlist_queue.py
      class LinkedListQueue:\n    \"\"\"Queue based on linked list implementation\"\"\"\n\n    def __init__(self):\n        \"\"\"Constructor\"\"\"\n        self._front: ListNode | None = None  # Head node front\n        self._rear: ListNode | None = None  # Tail node rear\n        self._size: int = 0\n\n    def size(self) -> int:\n        \"\"\"Get the length of the queue\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"Check if the queue is empty\"\"\"\n        return self._size == 0\n\n    def push(self, num: int):\n        \"\"\"Enqueue\"\"\"\n        # Add num after the tail node\n        node = ListNode(num)\n        # If the queue is empty, make both front and rear point to the node\n        if self._front is None:\n            self._front = node\n            self._rear = node\n        # If the queue is not empty, add the node after the tail node\n        else:\n            self._rear.next = node\n            self._rear = node\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"Dequeue\"\"\"\n        num = self.peek()\n        # Delete head node\n        self._front = self._front.next\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"Access front of the queue element\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Queue is empty\")\n        return self._front.val\n\n    def to_list(self) -> list[int]:\n        \"\"\"Convert to list for printing\"\"\"\n        queue = []\n        temp = self._front\n        while temp:\n            queue.append(temp.val)\n            temp = temp.next\n        return queue\n
      linkedlist_queue.cpp
      /* Queue based on linked list implementation */\nclass LinkedListQueue {\n  private:\n    ListNode *front, *rear; // Head node front, tail node rear\n    int queSize;\n\n  public:\n    LinkedListQueue() {\n        front = nullptr;\n        rear = nullptr;\n        queSize = 0;\n    }\n\n    ~LinkedListQueue() {\n        // Traverse linked list to delete nodes and free memory\n        freeMemoryLinkedList(front);\n    }\n\n    /* Get the length of the queue */\n    int size() {\n        return queSize;\n    }\n\n    /* Check if the queue is empty */\n    bool isEmpty() {\n        return queSize == 0;\n    }\n\n    /* Enqueue */\n    void push(int num) {\n        // Add num after the tail node\n        ListNode *node = new ListNode(num);\n        // If the queue is empty, make both front and rear point to the node\n        if (front == nullptr) {\n            front = node;\n            rear = node;\n        }\n        // If the queue is not empty, add the node after the tail node\n        else {\n            rear->next = node;\n            rear = node;\n        }\n        queSize++;\n    }\n\n    /* Dequeue */\n    int pop() {\n        int num = peek();\n        // Delete head node\n        ListNode *tmp = front;\n        front = front->next;\n        // Free memory\n        delete tmp;\n        queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    int peek() {\n        if (size() == 0)\n            throw out_of_range(\"Queue is empty\");\n        return front->val;\n    }\n\n    /* Convert linked list to Vector and return */\n    vector<int> toVector() {\n        ListNode *node = front;\n        vector<int> res(size());\n        for (int i = 0; i < res.size(); i++) {\n            res[i] = node->val;\n            node = node->next;\n        }\n        return res;\n    }\n};\n
      linkedlist_queue.java
      /* Queue based on linked list implementation */\nclass LinkedListQueue {\n    private ListNode front, rear; // Head node front, tail node rear\n    private int queSize = 0;\n\n    public LinkedListQueue() {\n        front = null;\n        rear = null;\n    }\n\n    /* Get the length of the queue */\n    public int size() {\n        return queSize;\n    }\n\n    /* Check if the queue is empty */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* Enqueue */\n    public void push(int num) {\n        // Add num after the tail node\n        ListNode node = new ListNode(num);\n        // If the queue is empty, make both front and rear point to the node\n        if (front == null) {\n            front = node;\n            rear = node;\n        // If the queue is not empty, add the node after the tail node\n        } else {\n            rear.next = node;\n            rear = node;\n        }\n        queSize++;\n    }\n\n    /* Dequeue */\n    public int pop() {\n        int num = peek();\n        // Delete head node\n        front = front.next;\n        queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return front.val;\n    }\n\n    /* Convert linked list to Array and return */\n    public int[] toArray() {\n        ListNode node = front;\n        int[] res = new int[size()];\n        for (int i = 0; i < res.length; i++) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
      linkedlist_queue.cs
      /* Queue based on linked list implementation */\nclass LinkedListQueue {\n    ListNode? front, rear;  // Head node front, tail node rear\n    int queSize = 0;\n\n    public LinkedListQueue() {\n        front = null;\n        rear = null;\n    }\n\n    /* Get the length of the queue */\n    public int Size() {\n        return queSize;\n    }\n\n    /* Check if the queue is empty */\n    public bool IsEmpty() {\n        return Size() == 0;\n    }\n\n    /* Enqueue */\n    public void Push(int num) {\n        // Add num after the tail node\n        ListNode node = new(num);\n        // If the queue is empty, make both front and rear point to the node\n        if (front == null) {\n            front = node;\n            rear = node;\n            // If the queue is not empty, add the node after the tail node\n        } else if (rear != null) {\n            rear.next = node;\n            rear = node;\n        }\n        queSize++;\n    }\n\n    /* Dequeue */\n    public int Pop() {\n        int num = Peek();\n        // Delete head node\n        front = front?.next;\n        queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    public int Peek() {\n        if (IsEmpty())\n            throw new Exception();\n        return front!.val;\n    }\n\n    /* Convert linked list to Array and return */\n    public int[] ToArray() {\n        if (front == null)\n            return [];\n\n        ListNode? node = front;\n        int[] res = new int[Size()];\n        for (int i = 0; i < res.Length; i++) {\n            res[i] = node!.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
      linkedlist_queue.go
      /* Queue based on linked list implementation */\ntype linkedListQueue struct {\n    // Use built-in package list to implement queue\n    data *list.List\n}\n\n/* Access front of the queue element */\nfunc newLinkedListQueue() *linkedListQueue {\n    return &linkedListQueue{\n        data: list.New(),\n    }\n}\n\n/* Enqueue */\nfunc (s *linkedListQueue) push(value any) {\n    s.data.PushBack(value)\n}\n\n/* Dequeue */\nfunc (s *linkedListQueue) pop() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Front()\n    s.data.Remove(e)\n    return e.Value\n}\n\n/* Return list for printing */\nfunc (s *linkedListQueue) peek() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Front()\n    return e.Value\n}\n\n/* Get the length of the queue */\nfunc (s *linkedListQueue) size() int {\n    return s.data.Len()\n}\n\n/* Check if the queue is empty */\nfunc (s *linkedListQueue) isEmpty() bool {\n    return s.data.Len() == 0\n}\n\n/* Get List for printing */\nfunc (s *linkedListQueue) toList() *list.List {\n    return s.data\n}\n
      linkedlist_queue.swift
      /* Queue based on linked list implementation */\nclass LinkedListQueue {\n    private var front: ListNode? // Head node\n    private var rear: ListNode? // Tail node\n    private var _size: Int\n\n    init() {\n        _size = 0\n    }\n\n    /* Get the length of the queue */\n    func size() -> Int {\n        _size\n    }\n\n    /* Check if the queue is empty */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* Enqueue */\n    func push(num: Int) {\n        // Add num after the tail node\n        let node = ListNode(x: num)\n        // If the queue is empty, make both front and rear point to the node\n        if front == nil {\n            front = node\n            rear = node\n        }\n        // If the queue is not empty, add the node after the tail node\n        else {\n            rear?.next = node\n            rear = node\n        }\n        _size += 1\n    }\n\n    /* Dequeue */\n    @discardableResult\n    func pop() -> Int {\n        let num = peek()\n        // Delete head node\n        front = front?.next\n        _size -= 1\n        return num\n    }\n\n    /* Return list for printing */\n    func peek() -> Int {\n        if isEmpty() {\n            fatalError(\"Queue is empty\")\n        }\n        return front!.val\n    }\n\n    /* Convert linked list to Array and return */\n    func toArray() -> [Int] {\n        var node = front\n        var res = Array(repeating: 0, count: size())\n        for i in res.indices {\n            res[i] = node!.val\n            node = node?.next\n        }\n        return res\n    }\n}\n
      linkedlist_queue.js
      /* Queue based on linked list implementation */\nclass LinkedListQueue {\n    #front; // Front node #front\n    #rear; // Rear node #rear\n    #queSize = 0;\n\n    constructor() {\n        this.#front = null;\n        this.#rear = null;\n    }\n\n    /* Get the length of the queue */\n    get size() {\n        return this.#queSize;\n    }\n\n    /* Check if the queue is empty */\n    isEmpty() {\n        return this.size === 0;\n    }\n\n    /* Enqueue */\n    push(num) {\n        // Add num after the tail node\n        const node = new ListNode(num);\n        // If the queue is empty, make both front and rear point to the node\n        if (!this.#front) {\n            this.#front = node;\n            this.#rear = node;\n            // If the queue is not empty, add the node after the tail node\n        } else {\n            this.#rear.next = node;\n            this.#rear = node;\n        }\n        this.#queSize++;\n    }\n\n    /* Dequeue */\n    pop() {\n        const num = this.peek();\n        // Delete head node\n        this.#front = this.#front.next;\n        this.#queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peek() {\n        if (this.size === 0) throw new Error('Queue is empty');\n        return this.#front.val;\n    }\n\n    /* Convert linked list to Array and return */\n    toArray() {\n        let node = this.#front;\n        const res = new Array(this.size);\n        for (let i = 0; i < res.length; i++) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
      linkedlist_queue.ts
      /* Queue based on linked list implementation */\nclass LinkedListQueue {\n    private front: ListNode | null; // Head node front\n    private rear: ListNode | null; // Tail node rear\n    private queSize: number = 0;\n\n    constructor() {\n        this.front = null;\n        this.rear = null;\n    }\n\n    /* Get the length of the queue */\n    get size(): number {\n        return this.queSize;\n    }\n\n    /* Check if the queue is empty */\n    isEmpty(): boolean {\n        return this.size === 0;\n    }\n\n    /* Enqueue */\n    push(num: number): void {\n        // Add num after the tail node\n        const node = new ListNode(num);\n        // If the queue is empty, make both front and rear point to the node\n        if (!this.front) {\n            this.front = node;\n            this.rear = node;\n            // If the queue is not empty, add the node after the tail node\n        } else {\n            this.rear!.next = node;\n            this.rear = node;\n        }\n        this.queSize++;\n    }\n\n    /* Dequeue */\n    pop(): number {\n        const num = this.peek();\n        if (!this.front) throw new Error('Queue is empty');\n        // Delete head node\n        this.front = this.front.next;\n        this.queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peek(): number {\n        if (this.size === 0) throw new Error('Queue is empty');\n        return this.front!.val;\n    }\n\n    /* Convert linked list to Array and return */\n    toArray(): number[] {\n        let node = this.front;\n        const res = new Array<number>(this.size);\n        for (let i = 0; i < res.length; i++) {\n            res[i] = node!.val;\n            node = node!.next;\n        }\n        return res;\n    }\n}\n
      linkedlist_queue.dart
      /* Queue based on linked list implementation */\nclass LinkedListQueue {\n  ListNode? _front; // Head node _front\n  ListNode? _rear; // Tail node _rear\n  int _queSize = 0; // Queue length\n\n  LinkedListQueue() {\n    _front = null;\n    _rear = null;\n  }\n\n  /* Get the length of the queue */\n  int size() {\n    return _queSize;\n  }\n\n  /* Check if the queue is empty */\n  bool isEmpty() {\n    return _queSize == 0;\n  }\n\n  /* Enqueue */\n  void push(int _num) {\n    // Add _num after tail node\n    final node = ListNode(_num);\n    // If the queue is empty, make both front and rear point to the node\n    if (_front == null) {\n      _front = node;\n      _rear = node;\n    } else {\n      // If the queue is not empty, add the node after the tail node\n      _rear!.next = node;\n      _rear = node;\n    }\n    _queSize++;\n  }\n\n  /* Dequeue */\n  int pop() {\n    final int _num = peek();\n    // Delete head node\n    _front = _front!.next;\n    _queSize--;\n    return _num;\n  }\n\n  /* Return list for printing */\n  int peek() {\n    if (_queSize == 0) {\n      throw Exception('Queue is empty');\n    }\n    return _front!.val;\n  }\n\n  /* Convert linked list to Array and return */\n  List<int> toArray() {\n    ListNode? node = _front;\n    final List<int> queue = [];\n    while (node != null) {\n      queue.add(node.val);\n      node = node.next;\n    }\n    return queue;\n  }\n}\n
      linkedlist_queue.rs
      /* Queue based on linked list implementation */\n#[allow(dead_code)]\npub struct LinkedListQueue<T> {\n    front: Option<Rc<RefCell<ListNode<T>>>>, // Head node front\n    rear: Option<Rc<RefCell<ListNode<T>>>>,  // Tail node rear\n    que_size: usize,                         // Queue length\n}\n\nimpl<T: Copy> LinkedListQueue<T> {\n    pub fn new() -> Self {\n        Self {\n            front: None,\n            rear: None,\n            que_size: 0,\n        }\n    }\n\n    /* Get the length of the queue */\n    pub fn size(&self) -> usize {\n        return self.que_size;\n    }\n\n    /* Check if the queue is empty */\n    pub fn is_empty(&self) -> bool {\n        return self.que_size == 0;\n    }\n\n    /* Enqueue */\n    pub fn push(&mut self, num: T) {\n        // Add num after the tail node\n        let new_rear = ListNode::new(num);\n        match self.rear.take() {\n            // If the queue is not empty, add the node after the tail node\n            Some(old_rear) => {\n                old_rear.borrow_mut().next = Some(new_rear.clone());\n                self.rear = Some(new_rear);\n            }\n            // If the queue is empty, make both front and rear point to the node\n            None => {\n                self.front = Some(new_rear.clone());\n                self.rear = Some(new_rear);\n            }\n        }\n        self.que_size += 1;\n    }\n\n    /* Dequeue */\n    pub fn pop(&mut self) -> Option<T> {\n        self.front.take().map(|old_front| {\n            match old_front.borrow_mut().next.take() {\n                Some(new_front) => {\n                    self.front = Some(new_front);\n                }\n                None => {\n                    self.rear.take();\n                }\n            }\n            self.que_size -= 1;\n            old_front.borrow().val\n        })\n    }\n\n    /* Return list for printing */\n    pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n        self.front.as_ref()\n    }\n\n    /* Convert linked list to Array and return */\n    pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n        let mut res: Vec<T> = Vec::new();\n\n        fn recur<T: Copy>(cur: Option<&Rc<RefCell<ListNode<T>>>>, res: &mut Vec<T>) {\n            if let Some(cur) = cur {\n                res.push(cur.borrow().val);\n                recur(cur.borrow().next.as_ref(), res);\n            }\n        }\n\n        recur(head, &mut res);\n\n        res\n    }\n}\n
      linkedlist_queue.c
      /* Queue based on linked list implementation */\ntypedef struct {\n    ListNode *front, *rear;\n    int queSize;\n} LinkedListQueue;\n\n/* Constructor */\nLinkedListQueue *newLinkedListQueue() {\n    LinkedListQueue *queue = (LinkedListQueue *)malloc(sizeof(LinkedListQueue));\n    queue->front = NULL;\n    queue->rear = NULL;\n    queue->queSize = 0;\n    return queue;\n}\n\n/* Destructor */\nvoid delLinkedListQueue(LinkedListQueue *queue) {\n    // Free all nodes\n    while (queue->front != NULL) {\n        ListNode *tmp = queue->front;\n        queue->front = queue->front->next;\n        free(tmp);\n    }\n    // Free queue structure\n    free(queue);\n}\n\n/* Get the length of the queue */\nint size(LinkedListQueue *queue) {\n    return queue->queSize;\n}\n\n/* Check if the queue is empty */\nbool empty(LinkedListQueue *queue) {\n    return (size(queue) == 0);\n}\n\n/* Enqueue */\nvoid push(LinkedListQueue *queue, int num) {\n    // Add node at tail\n    ListNode *node = newListNode(num);\n    // If the queue is empty, make both front and rear point to the node\n    if (queue->front == NULL) {\n        queue->front = node;\n        queue->rear = node;\n    }\n    // If the queue is not empty, add the node after the tail node\n    else {\n        queue->rear->next = node;\n        queue->rear = node;\n    }\n    queue->queSize++;\n}\n\n/* Return list for printing */\nint peek(LinkedListQueue *queue) {\n    assert(size(queue) && queue->front);\n    return queue->front->val;\n}\n\n/* Dequeue */\nint pop(LinkedListQueue *queue) {\n    int num = peek(queue);\n    ListNode *tmp = queue->front;\n    queue->front = queue->front->next;\n    free(tmp);\n    queue->queSize--;\n    return num;\n}\n\n/* Print queue */\nvoid printLinkedListQueue(LinkedListQueue *queue) {\n    int *arr = malloc(sizeof(int) * queue->queSize);\n    // Copy data from list to array\n    int i;\n    ListNode *node;\n    for (i = 0, node = queue->front; i < queue->queSize; i++) {\n        arr[i] = node->val;\n        node = node->next;\n    }\n    printArray(arr, queue->queSize);\n    free(arr);\n}\n
      linkedlist_queue.kt
      /* Queue based on linked list implementation */\nclass LinkedListQueue(\n    // Head node front, tail node rear\n    private var front: ListNode? = null,\n    private var rear: ListNode? = null,\n    private var queSize: Int = 0\n) {\n\n    /* Get the length of the queue */\n    fun size(): Int {\n        return queSize\n    }\n\n    /* Check if the queue is empty */\n    fun isEmpty(): Boolean {\n        return size() == 0\n    }\n\n    /* Enqueue */\n    fun push(num: Int) {\n        // Add num after the tail node\n        val node = ListNode(num)\n        // If the queue is empty, make both front and rear point to the node\n        if (front == null) {\n            front = node\n            rear = node\n            // If the queue is not empty, add the node after the tail node\n        } else {\n            rear?.next = node\n            rear = node\n        }\n        queSize++\n    }\n\n    /* Dequeue */\n    fun pop(): Int {\n        val num = peek()\n        // Delete head node\n        front = front?.next\n        queSize--\n        return num\n    }\n\n    /* Return list for printing */\n    fun peek(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return front!!._val\n    }\n\n    /* Convert linked list to Array and return */\n    fun toArray(): IntArray {\n        var node = front\n        val res = IntArray(size())\n        for (i in res.indices) {\n            res[i] = node!!._val\n            node = node.next\n        }\n        return res\n    }\n}\n
      linkedlist_queue.rb
      ### Queue based on linked list ###\nclass LinkedListQueue\n  ### Get queue length ###\n  attr_reader :size\n\n  ### Constructor ###\n  def initialize\n    @front = nil  # Head node front\n    @rear = nil   # Tail node rear\n    @size = 0\n  end\n\n  ### Check if queue is empty ###\n  def is_empty?\n    @front.nil?\n  end\n\n  ### Enqueue ###\n  def push(num)\n    # Add num after the tail node\n    node = ListNode.new(num)\n\n    # If queue is empty, set both front and rear to this node\n    if @front.nil?\n      @front = node\n      @rear = node\n    # If queue is not empty, add this node after rear\n    else\n      @rear.next = node\n      @rear = node\n    end\n\n    @size += 1\n  end\n\n  ### Dequeue ###\n  def pop\n    num = peek\n    # Delete head node\n    @front = @front.next\n    @size -= 1\n    num\n  end\n\n  ### Access front element ###\n  def peek\n    raise IndexError, 'Queue is empty' if is_empty?\n\n    @front.val\n  end\n\n  ### Convert linked list to Array and return ###\n  def to_array\n    queue = []\n    temp = @front\n    while temp\n      queue << temp.val\n      temp = temp.next\n    end\n    queue\n  end\nend\n
      "},{"location":"chapter_stack_and_queue/queue/#2-array-implementation","title":"2. \u00a0 Array Implementation","text":"

      Deleting the first element in an array has a time complexity of \\(O(n)\\), which would make the dequeue operation inefficient. However, we can use the following clever method to avoid this problem.

      We can use a variable front to point to the index of the front element and maintain a variable size to record the queue length. We define rear = front + size, which calculates the position right after the rear element.

      Based on this design, the valid interval containing elements in the array is [front, rear - 1]. The implementation methods for various operations are shown in Figure 5-6:

      • Enqueue operation: Assign the input element to the rear index and increase size by 1.
      • Dequeue operation: Simply increase front by 1 and decrease size by 1.

      As you can see, both enqueue and dequeue operations require only one operation, with a time complexity of \\(O(1)\\).

      ArrayQueuepush()pop()

      Figure 5-6 \u00a0 Enqueue and dequeue operations in array implementation of queue

      You may notice a problem: as we continuously enqueue and dequeue, both front and rear move to the right. When they reach the end of the array, they cannot continue moving. To solve this problem, we can treat the array as a \"circular array\" with head and tail connected.

      For a circular array, we need to let front or rear wrap around to the beginning of the array when they cross the end. This periodic pattern can be implemented using the \"modulo operation,\" as shown in the code below:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_queue.py
      class ArrayQueue:\n    \"\"\"Queue based on circular array implementation\"\"\"\n\n    def __init__(self, size: int):\n        \"\"\"Constructor\"\"\"\n        self._nums: list[int] = [0] * size  # Array for storing queue elements\n        self._front: int = 0  # Front pointer, points to the front of the queue element\n        self._size: int = 0  # Queue length\n\n    def capacity(self) -> int:\n        \"\"\"Get the capacity of the queue\"\"\"\n        return len(self._nums)\n\n    def size(self) -> int:\n        \"\"\"Get the length of the queue\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"Check if the queue is empty\"\"\"\n        return self._size == 0\n\n    def push(self, num: int):\n        \"\"\"Enqueue\"\"\"\n        if self._size == self.capacity():\n            raise IndexError(\"Queue is full\")\n        # Calculate rear pointer, points to rear index + 1\n        # Use modulo operation to wrap rear around to the head after passing the tail of the array\n        rear: int = (self._front + self._size) % self.capacity()\n        # Add num to the rear of the queue\n        self._nums[rear] = num\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"Dequeue\"\"\"\n        num: int = self.peek()\n        # Front pointer moves one position backward, if it passes the tail, return to the head of the array\n        self._front = (self._front + 1) % self.capacity()\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"Access front of the queue element\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Queue is empty\")\n        return self._nums[self._front]\n\n    def to_list(self) -> list[int]:\n        \"\"\"Return list for printing\"\"\"\n        res = [0] * self.size()\n        j: int = self._front\n        for i in range(self.size()):\n            res[i] = self._nums[(j % self.capacity())]\n            j += 1\n        return res\n
      array_queue.cpp
      /* Queue based on circular array implementation */\nclass ArrayQueue {\n  private:\n    int *nums;       // Array for storing queue elements\n    int front;       // Front pointer, points to the front of the queue element\n    int queSize;     // Queue length\n    int queCapacity; // Queue capacity\n\n  public:\n    ArrayQueue(int capacity) {\n        // Initialize array\n        nums = new int[capacity];\n        queCapacity = capacity;\n        front = queSize = 0;\n    }\n\n    ~ArrayQueue() {\n        delete[] nums;\n    }\n\n    /* Get the capacity of the queue */\n    int capacity() {\n        return queCapacity;\n    }\n\n    /* Get the length of the queue */\n    int size() {\n        return queSize;\n    }\n\n    /* Check if the queue is empty */\n    bool isEmpty() {\n        return size() == 0;\n    }\n\n    /* Enqueue */\n    void push(int num) {\n        if (queSize == queCapacity) {\n            cout << \"Queue is full\" << endl;\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        // Add num to the rear of the queue\n        int rear = (front + queSize) % queCapacity;\n        // Front pointer moves one position backward\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* Dequeue */\n    int pop() {\n        int num = peek();\n        // Move front pointer backward by one position, if it passes the tail, return to array head\n        front = (front + 1) % queCapacity;\n        queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    int peek() {\n        if (isEmpty())\n            throw out_of_range(\"Queue is empty\");\n        return nums[front];\n    }\n\n    /* Convert array to Vector and return */\n    vector<int> toVector() {\n        // Elements enqueue\n        vector<int> arr(queSize);\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            arr[i] = nums[j % queCapacity];\n        }\n        return arr;\n    }\n};\n
      array_queue.java
      /* Queue based on circular array implementation */\nclass ArrayQueue {\n    private int[] nums; // Array for storing queue elements\n    private int front; // Front pointer, points to the front of the queue element\n    private int queSize; // Queue length\n\n    public ArrayQueue(int capacity) {\n        nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* Get the capacity of the queue */\n    public int capacity() {\n        return nums.length;\n    }\n\n    /* Get the length of the queue */\n    public int size() {\n        return queSize;\n    }\n\n    /* Check if the queue is empty */\n    public boolean isEmpty() {\n        return queSize == 0;\n    }\n\n    /* Enqueue */\n    public void push(int num) {\n        if (queSize == capacity()) {\n            System.out.println(\"Queue is full\");\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        // Add num to the rear of the queue\n        int rear = (front + queSize) % capacity();\n        // Front pointer moves one position backward\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* Dequeue */\n    public int pop() {\n        int num = peek();\n        // Move front pointer backward by one position, if it passes the tail, return to array head\n        front = (front + 1) % capacity();\n        queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return nums[front];\n    }\n\n    /* Return array */\n    public int[] toArray() {\n        // Elements enqueue\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[j % capacity()];\n        }\n        return res;\n    }\n}\n
      array_queue.cs
      /* Queue based on circular array implementation */\nclass ArrayQueue {\n    int[] nums;  // Array for storing queue elements\n    int front;   // Front pointer, points to the front of the queue element\n    int queSize; // Queue length\n\n    public ArrayQueue(int capacity) {\n        nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* Get the capacity of the queue */\n    int Capacity() {\n        return nums.Length;\n    }\n\n    /* Get the length of the queue */\n    public int Size() {\n        return queSize;\n    }\n\n    /* Check if the queue is empty */\n    public bool IsEmpty() {\n        return queSize == 0;\n    }\n\n    /* Enqueue */\n    public void Push(int num) {\n        if (queSize == Capacity()) {\n            Console.WriteLine(\"Queue is full\");\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        // Add num to the rear of the queue\n        int rear = (front + queSize) % Capacity();\n        // Front pointer moves one position backward\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* Dequeue */\n    public int Pop() {\n        int num = Peek();\n        // Move front pointer backward by one position, if it passes the tail, return to array head\n        front = (front + 1) % Capacity();\n        queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    public int Peek() {\n        if (IsEmpty())\n            throw new Exception();\n        return nums[front];\n    }\n\n    /* Return array */\n    public int[] ToArray() {\n        // Elements enqueue\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[j % this.Capacity()];\n        }\n        return res;\n    }\n}\n
      array_queue.go
      /* Queue based on circular array implementation */\ntype arrayQueue struct {\n    nums        []int // Array for storing queue elements\n    front       int   // Front pointer, points to the front of the queue element\n    queSize     int   // Queue length\n    queCapacity int   // Queue capacity (maximum number of elements)\n}\n\n/* Access front of the queue element */\nfunc newArrayQueue(queCapacity int) *arrayQueue {\n    return &arrayQueue{\n        nums:        make([]int, queCapacity),\n        queCapacity: queCapacity,\n        front:       0,\n        queSize:     0,\n    }\n}\n\n/* Get the length of the queue */\nfunc (q *arrayQueue) size() int {\n    return q.queSize\n}\n\n/* Check if the queue is empty */\nfunc (q *arrayQueue) isEmpty() bool {\n    return q.queSize == 0\n}\n\n/* Enqueue */\nfunc (q *arrayQueue) push(num int) {\n    // When rear == queCapacity, queue is full\n    if q.queSize == q.queCapacity {\n        return\n    }\n    // Use modulo operation to wrap rear around to the head after passing the tail of the array\n    // Add num to the rear of the queue\n    rear := (q.front + q.queSize) % q.queCapacity\n    // Front pointer moves one position backward\n    q.nums[rear] = num\n    q.queSize++\n}\n\n/* Dequeue */\nfunc (q *arrayQueue) pop() any {\n    num := q.peek()\n    if num == nil {\n        return nil\n    }\n\n    // Move front pointer backward by one position, if it passes the tail, return to array head\n    q.front = (q.front + 1) % q.queCapacity\n    q.queSize--\n    return num\n}\n\n/* Return list for printing */\nfunc (q *arrayQueue) peek() any {\n    if q.isEmpty() {\n        return nil\n    }\n    return q.nums[q.front]\n}\n\n/* Get Slice for printing */\nfunc (q *arrayQueue) toSlice() []int {\n    rear := (q.front + q.queSize)\n    if rear >= q.queCapacity {\n        rear %= q.queCapacity\n        return append(q.nums[q.front:], q.nums[:rear]...)\n    }\n    return q.nums[q.front:rear]\n}\n
      array_queue.swift
      /* Queue based on circular array implementation */\nclass ArrayQueue {\n    private var nums: [Int] // Array for storing queue elements\n    private var front: Int // Front pointer, points to the front of the queue element\n    private var _size: Int // Queue length\n\n    init(capacity: Int) {\n        // Initialize array\n        nums = Array(repeating: 0, count: capacity)\n        front = 0\n        _size = 0\n    }\n\n    /* Get the capacity of the queue */\n    func capacity() -> Int {\n        nums.count\n    }\n\n    /* Get the length of the queue */\n    func size() -> Int {\n        _size\n    }\n\n    /* Check if the queue is empty */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* Enqueue */\n    func push(num: Int) {\n        if size() == capacity() {\n            print(\"Queue is full\")\n            return\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        // Add num to the rear of the queue\n        let rear = (front + size()) % capacity()\n        // Front pointer moves one position backward\n        nums[rear] = num\n        _size += 1\n    }\n\n    /* Dequeue */\n    @discardableResult\n    func pop() -> Int {\n        let num = peek()\n        // Move front pointer backward by one position, if it passes the tail, return to array head\n        front = (front + 1) % capacity()\n        _size -= 1\n        return num\n    }\n\n    /* Return list for printing */\n    func peek() -> Int {\n        if isEmpty() {\n            fatalError(\"Queue is empty\")\n        }\n        return nums[front]\n    }\n\n    /* Return array */\n    func toArray() -> [Int] {\n        // Elements enqueue\n        (front ..< front + size()).map { nums[$0 % capacity()] }\n    }\n}\n
      array_queue.js
      /* Queue based on circular array implementation */\nclass ArrayQueue {\n    #nums; // Array for storing queue elements\n    #front = 0; // Front pointer, points to the front of the queue element\n    #queSize = 0; // Queue length\n\n    constructor(capacity) {\n        this.#nums = new Array(capacity);\n    }\n\n    /* Get the capacity of the queue */\n    get capacity() {\n        return this.#nums.length;\n    }\n\n    /* Get the length of the queue */\n    get size() {\n        return this.#queSize;\n    }\n\n    /* Check if the queue is empty */\n    isEmpty() {\n        return this.#queSize === 0;\n    }\n\n    /* Enqueue */\n    push(num) {\n        if (this.size === this.capacity) {\n            console.log('Queue is full');\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        // Add num to the rear of the queue\n        const rear = (this.#front + this.size) % this.capacity;\n        // Front pointer moves one position backward\n        this.#nums[rear] = num;\n        this.#queSize++;\n    }\n\n    /* Dequeue */\n    pop() {\n        const num = this.peek();\n        // Move front pointer backward by one position, if it passes the tail, return to array head\n        this.#front = (this.#front + 1) % this.capacity;\n        this.#queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peek() {\n        if (this.isEmpty()) throw new Error('Queue is empty');\n        return this.#nums[this.#front];\n    }\n\n    /* Return Array */\n    toArray() {\n        // Elements enqueue\n        const arr = new Array(this.size);\n        for (let i = 0, j = this.#front; i < this.size; i++, j++) {\n            arr[i] = this.#nums[j % this.capacity];\n        }\n        return arr;\n    }\n}\n
      array_queue.ts
      /* Queue based on circular array implementation */\nclass ArrayQueue {\n    private nums: number[]; // Array for storing queue elements\n    private front: number; // Front pointer, points to the front of the queue element\n    private queSize: number; // Queue length\n\n    constructor(capacity: number) {\n        this.nums = new Array(capacity);\n        this.front = this.queSize = 0;\n    }\n\n    /* Get the capacity of the queue */\n    get capacity(): number {\n        return this.nums.length;\n    }\n\n    /* Get the length of the queue */\n    get size(): number {\n        return this.queSize;\n    }\n\n    /* Check if the queue is empty */\n    isEmpty(): boolean {\n        return this.queSize === 0;\n    }\n\n    /* Enqueue */\n    push(num: number): void {\n        if (this.size === this.capacity) {\n            console.log('Queue is full');\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        // Add num to the rear of the queue\n        const rear = (this.front + this.queSize) % this.capacity;\n        // Front pointer moves one position backward\n        this.nums[rear] = num;\n        this.queSize++;\n    }\n\n    /* Dequeue */\n    pop(): number {\n        const num = this.peek();\n        // Move front pointer backward by one position, if it passes the tail, return to array head\n        this.front = (this.front + 1) % this.capacity;\n        this.queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peek(): number {\n        if (this.isEmpty()) throw new Error('Queue is empty');\n        return this.nums[this.front];\n    }\n\n    /* Return Array */\n    toArray(): number[] {\n        // Elements enqueue\n        const arr = new Array(this.size);\n        for (let i = 0, j = this.front; i < this.size; i++, j++) {\n            arr[i] = this.nums[j % this.capacity];\n        }\n        return arr;\n    }\n}\n
      array_queue.dart
      /* Queue based on circular array implementation */\nclass ArrayQueue {\n  late List<int> _nums; // Array for storing queue elements\n  late int _front; // Front pointer, points to the front of the queue element\n  late int _queSize; // Queue length\n\n  ArrayQueue(int capacity) {\n    _nums = List.filled(capacity, 0);\n    _front = _queSize = 0;\n  }\n\n  /* Get the capacity of the queue */\n  int capaCity() {\n    return _nums.length;\n  }\n\n  /* Get the length of the queue */\n  int size() {\n    return _queSize;\n  }\n\n  /* Check if the queue is empty */\n  bool isEmpty() {\n    return _queSize == 0;\n  }\n\n  /* Enqueue */\n  void push(int _num) {\n    if (_queSize == capaCity()) {\n      throw Exception(\"Queue is full\");\n    }\n    // Use modulo operation to wrap rear around to the head after passing the tail of the array\n    // Add num to the rear of the queue\n    int rear = (_front + _queSize) % capaCity();\n    // Add _num to queue rear\n    _nums[rear] = _num;\n    _queSize++;\n  }\n\n  /* Dequeue */\n  int pop() {\n    int _num = peek();\n    // Move front pointer backward by one position, if it passes the tail, return to array head\n    _front = (_front + 1) % capaCity();\n    _queSize--;\n    return _num;\n  }\n\n  /* Return list for printing */\n  int peek() {\n    if (isEmpty()) {\n      throw Exception(\"Queue is empty\");\n    }\n    return _nums[_front];\n  }\n\n  /* Return Array */\n  List<int> toArray() {\n    // Elements enqueue\n    final List<int> res = List.filled(_queSize, 0);\n    for (int i = 0, j = _front; i < _queSize; i++, j++) {\n      res[i] = _nums[j % capaCity()];\n    }\n    return res;\n  }\n}\n
      array_queue.rs
      /* Queue based on circular array implementation */\nstruct ArrayQueue<T> {\n    nums: Vec<T>,      // Array for storing queue elements\n    front: i32,        // Front pointer, points to the front of the queue element\n    que_size: i32,     // Queue length\n    que_capacity: i32, // Queue capacity\n}\n\nimpl<T: Copy + Default> ArrayQueue<T> {\n    /* Constructor */\n    fn new(capacity: i32) -> ArrayQueue<T> {\n        ArrayQueue {\n            nums: vec![T::default(); capacity as usize],\n            front: 0,\n            que_size: 0,\n            que_capacity: capacity,\n        }\n    }\n\n    /* Get the capacity of the queue */\n    fn capacity(&self) -> i32 {\n        self.que_capacity\n    }\n\n    /* Get the length of the queue */\n    fn size(&self) -> i32 {\n        self.que_size\n    }\n\n    /* Check if the queue is empty */\n    fn is_empty(&self) -> bool {\n        self.que_size == 0\n    }\n\n    /* Enqueue */\n    fn push(&mut self, num: T) {\n        if self.que_size == self.capacity() {\n            println!(\"Queue is full\");\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        // Add num to the rear of the queue\n        let rear = (self.front + self.que_size) % self.que_capacity;\n        // Front pointer moves one position backward\n        self.nums[rear as usize] = num;\n        self.que_size += 1;\n    }\n\n    /* Dequeue */\n    fn pop(&mut self) -> T {\n        let num = self.peek();\n        // Move front pointer backward by one position, if it passes the tail, return to array head\n        self.front = (self.front + 1) % self.que_capacity;\n        self.que_size -= 1;\n        num\n    }\n\n    /* Return list for printing */\n    fn peek(&self) -> T {\n        if self.is_empty() {\n            panic!(\"index out of bounds\");\n        }\n        self.nums[self.front as usize]\n    }\n\n    /* Return array */\n    fn to_vector(&self) -> Vec<T> {\n        let cap = self.que_capacity;\n        let mut j = self.front;\n        let mut arr = vec![T::default(); cap as usize];\n        for i in 0..self.que_size {\n            arr[i as usize] = self.nums[(j % cap) as usize];\n            j += 1;\n        }\n        arr\n    }\n}\n
      array_queue.c
      /* Queue based on circular array implementation */\ntypedef struct {\n    int *nums;       // Array for storing queue elements\n    int front;       // Front pointer, points to the front of the queue element\n    int queSize;     // Rear pointer, points to rear + 1\n    int queCapacity; // Queue capacity\n} ArrayQueue;\n\n/* Constructor */\nArrayQueue *newArrayQueue(int capacity) {\n    ArrayQueue *queue = (ArrayQueue *)malloc(sizeof(ArrayQueue));\n    // Initialize array\n    queue->queCapacity = capacity;\n    queue->nums = (int *)malloc(sizeof(int) * queue->queCapacity);\n    queue->front = queue->queSize = 0;\n    return queue;\n}\n\n/* Destructor */\nvoid delArrayQueue(ArrayQueue *queue) {\n    free(queue->nums);\n    free(queue);\n}\n\n/* Get the capacity of the queue */\nint capacity(ArrayQueue *queue) {\n    return queue->queCapacity;\n}\n\n/* Get the length of the queue */\nint size(ArrayQueue *queue) {\n    return queue->queSize;\n}\n\n/* Check if the queue is empty */\nbool empty(ArrayQueue *queue) {\n    return queue->queSize == 0;\n}\n\n/* Return list for printing */\nint peek(ArrayQueue *queue) {\n    assert(size(queue) != 0);\n    return queue->nums[queue->front];\n}\n\n/* Enqueue */\nvoid push(ArrayQueue *queue, int num) {\n    if (size(queue) == capacity(queue)) {\n        printf(\"Queue is full\\r\\n\");\n        return;\n    }\n    // Use modulo operation to wrap rear around to the head after passing the tail of the array\n    // Add num to the rear of the queue\n    int rear = (queue->front + queue->queSize) % queue->queCapacity;\n    // Front pointer moves one position backward\n    queue->nums[rear] = num;\n    queue->queSize++;\n}\n\n/* Dequeue */\nint pop(ArrayQueue *queue) {\n    int num = peek(queue);\n    // Move front pointer backward by one position, if it passes the tail, return to array head\n    queue->front = (queue->front + 1) % queue->queCapacity;\n    queue->queSize--;\n    return num;\n}\n\n/* Return array for printing */\nint *toArray(ArrayQueue *queue, int *queSize) {\n    *queSize = queue->queSize;\n    int *res = (int *)calloc(queue->queSize, sizeof(int));\n    int j = queue->front;\n    for (int i = 0; i < queue->queSize; i++) {\n        res[i] = queue->nums[j % queue->queCapacity];\n        j++;\n    }\n    return res;\n}\n
      array_queue.kt
      /* Queue based on circular array implementation */\nclass ArrayQueue(capacity: Int) {\n    private val nums: IntArray = IntArray(capacity) // Array for storing queue elements\n    private var front: Int = 0 // Front pointer, points to the front of the queue element\n    private var queSize: Int = 0 // Queue length\n\n    /* Get the capacity of the queue */\n    fun capacity(): Int {\n        return nums.size\n    }\n\n    /* Get the length of the queue */\n    fun size(): Int {\n        return queSize\n    }\n\n    /* Check if the queue is empty */\n    fun isEmpty(): Boolean {\n        return queSize == 0\n    }\n\n    /* Enqueue */\n    fun push(num: Int) {\n        if (queSize == capacity()) {\n            println(\"Queue is full\")\n            return\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        // Add num to the rear of the queue\n        val rear = (front + queSize) % capacity()\n        // Front pointer moves one position backward\n        nums[rear] = num\n        queSize++\n    }\n\n    /* Dequeue */\n    fun pop(): Int {\n        val num = peek()\n        // Move front pointer backward by one position, if it passes the tail, return to array head\n        front = (front + 1) % capacity()\n        queSize--\n        return num\n    }\n\n    /* Return list for printing */\n    fun peek(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return nums[front]\n    }\n\n    /* Return array */\n    fun toArray(): IntArray {\n        // Elements enqueue\n        val res = IntArray(queSize)\n        var i = 0\n        var j = front\n        while (i < queSize) {\n            res[i] = nums[j % capacity()]\n            i++\n            j++\n        }\n        return res\n    }\n}\n
      array_queue.rb
      ### Queue based on circular array ###\nclass ArrayQueue\n  ### Get queue length ###\n  attr_reader :size\n\n  ### Constructor ###\n  def initialize(size)\n    @nums = Array.new(size, 0) # Array for storing queue elements\n    @front = 0 # Front pointer, points to the front of the queue element\n    @size = 0 # Queue length\n  end\n\n  ### Get queue capacity ###\n  def capacity\n    @nums.length\n  end\n\n  ### Check if queue is empty ###\n  def is_empty?\n    size.zero?\n  end\n\n  ### Enqueue ###\n  def push(num)\n    raise IndexError, 'Queue is full' if size == capacity\n\n    # Use modulo operation to wrap rear around to the head after passing the tail of the array\n    # Add num to the rear of the queue\n    rear = (@front + size) % capacity\n    # Front pointer moves one position backward\n    @nums[rear] = num\n    @size += 1\n  end\n\n  ### Dequeue ###\n  def pop\n    num = peek\n    # Move front pointer backward by one position, if it passes the tail, return to array head\n    @front = (@front + 1) % capacity\n    @size -= 1\n    num\n  end\n\n  ### Access front element ###\n  def peek\n    raise IndexError, 'Queue is empty' if is_empty?\n\n    @nums[@front]\n  end\n\n  ### Return list for printing ###\n  def to_array\n    res = Array.new(size, 0)\n    j = @front\n\n    for i in 0...size\n      res[i] = @nums[j % capacity]\n      j += 1\n    end\n\n    res\n  end\nend\n

      The queue implemented above still has limitations: its length is immutable. However, this problem is not difficult to solve. We can replace the array with a dynamic array to introduce an expansion mechanism. Interested readers can try to implement this themselves.

      The comparison conclusions for the two implementations are consistent with those for stacks and will not be repeated here.

      "},{"location":"chapter_stack_and_queue/queue/#523-typical-applications-of-queue","title":"5.2.3 \u00a0 Typical Applications of Queue","text":"
      • Taobao orders. After shoppers place orders, the orders are added to a queue, and the system subsequently processes the orders in the queue according to their sequence. During Double Eleven, massive orders are generated in a short time, and high concurrency becomes a key challenge that engineers need to tackle.
      • Various to-do tasks. Any scenario that needs to implement \"first come, first served\" functionality, such as a printer's task queue or a restaurant's order queue, can effectively maintain the processing order using queues.
      "},{"location":"chapter_stack_and_queue/stack/","title":"5.1 \u00a0 Stack","text":"

      A stack is a linear data structure that follows the Last In First Out (LIFO) logic.

      We can compare a stack to a pile of plates on a table. If we specify that only one plate can be moved at a time, then to get the bottom plate, we must first remove the plates above it one by one. If we replace the plates with various types of elements (such as integers, characters, objects, etc.), we get the stack data structure.

      As shown in Figure 5-1, we call the top of the stacked elements the \"top\" and the bottom the \"base.\" The operation of adding an element to the top is called \"push,\" and the operation of removing the top element is called \"pop.\"

      Figure 5-1 \u00a0 LIFO rule of stack

      "},{"location":"chapter_stack_and_queue/stack/#511-common-stack-operations","title":"5.1.1 \u00a0 Common Stack Operations","text":"

      The common operations on a stack are shown in Table 5-1. The specific method names depend on the programming language used. Here, we use the common naming convention of push(), pop(), and peek().

      Table 5-1 \u00a0 Efficiency of Stack Operations

      Method Description Time Complexity push() Push element onto stack (add to top) \\(O(1)\\) pop() Pop top element from stack \\(O(1)\\) peek() Access top element \\(O(1)\\)

      Typically, we can directly use the built-in stack class provided by the programming language. However, some languages may not provide a dedicated stack class. In these cases, we can use the language's \"array\" or \"linked list\" as a stack and ignore operations unrelated to the stack in the program logic.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby stack.py
      # Initialize stack\n# Python does not have a built-in stack class, can use list as a stack\nstack: list[int] = []\n\n# Push elements\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n# Access top element\npeek: int = stack[-1]\n\n# Pop element\npop: int = stack.pop()\n\n# Get stack length\nsize: int = len(stack)\n\n# Check if empty\nis_empty: bool = len(stack) == 0\n
      stack.cpp
      /* Initialize stack */\nstack<int> stack;\n\n/* Push elements */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* Access top element */\nint top = stack.top();\n\n/* Pop element */\nstack.pop(); // No return value\n\n/* Get stack length */\nint size = stack.size();\n\n/* Check if empty */\nbool empty = stack.empty();\n
      stack.java
      /* Initialize stack */\nStack<Integer> stack = new Stack<>();\n\n/* Push elements */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* Access top element */\nint peek = stack.peek();\n\n/* Pop element */\nint pop = stack.pop();\n\n/* Get stack length */\nint size = stack.size();\n\n/* Check if empty */\nboolean isEmpty = stack.isEmpty();\n
      stack.cs
      /* Initialize stack */\nStack<int> stack = new();\n\n/* Push elements */\nstack.Push(1);\nstack.Push(3);\nstack.Push(2);\nstack.Push(5);\nstack.Push(4);\n\n/* Access top element */\nint peek = stack.Peek();\n\n/* Pop element */\nint pop = stack.Pop();\n\n/* Get stack length */\nint size = stack.Count;\n\n/* Check if empty */\nbool isEmpty = stack.Count == 0;\n
      stack_test.go
      /* Initialize stack */\n// In Go, it is recommended to use Slice as a stack\nvar stack []int\n\n/* Push elements */\nstack = append(stack, 1)\nstack = append(stack, 3)\nstack = append(stack, 2)\nstack = append(stack, 5)\nstack = append(stack, 4)\n\n/* Access top element */\npeek := stack[len(stack)-1]\n\n/* Pop element */\npop := stack[len(stack)-1]\nstack = stack[:len(stack)-1]\n\n/* Get stack length */\nsize := len(stack)\n\n/* Check if empty */\nisEmpty := len(stack) == 0\n
      stack.swift
      /* Initialize stack */\n// Swift does not have a built-in stack class, can use Array as a stack\nvar stack: [Int] = []\n\n/* Push elements */\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n/* Access top element */\nlet peek = stack.last!\n\n/* Pop element */\nlet pop = stack.removeLast()\n\n/* Get stack length */\nlet size = stack.count\n\n/* Check if empty */\nlet isEmpty = stack.isEmpty\n
      stack.js
      /* Initialize stack */\n// JavaScript does not have a built-in stack class, can use Array as a stack\nconst stack = [];\n\n/* Push elements */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* Access top element */\nconst peek = stack[stack.length-1];\n\n/* Pop element */\nconst pop = stack.pop();\n\n/* Get stack length */\nconst size = stack.length;\n\n/* Check if empty */\nconst is_empty = stack.length === 0;\n
      stack.ts
      /* Initialize stack */\n// TypeScript does not have a built-in stack class, can use Array as a stack\nconst stack: number[] = [];\n\n/* Push elements */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* Access top element */\nconst peek = stack[stack.length - 1];\n\n/* Pop element */\nconst pop = stack.pop();\n\n/* Get stack length */\nconst size = stack.length;\n\n/* Check if empty */\nconst is_empty = stack.length === 0;\n
      stack.dart
      /* Initialize stack */\n// Dart does not have a built-in stack class, can use List as a stack\nList<int> stack = [];\n\n/* Push elements */\nstack.add(1);\nstack.add(3);\nstack.add(2);\nstack.add(5);\nstack.add(4);\n\n/* Access top element */\nint peek = stack.last;\n\n/* Pop element */\nint pop = stack.removeLast();\n\n/* Get stack length */\nint size = stack.length;\n\n/* Check if empty */\nbool isEmpty = stack.isEmpty;\n
      stack.rs
      /* Initialize stack */\n// Use Vec as a stack\nlet mut stack: Vec<i32> = Vec::new();\n\n/* Push elements */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* Access top element */\nlet top = stack.last().unwrap();\n\n/* Pop element */\nlet pop = stack.pop().unwrap();\n\n/* Get stack length */\nlet size = stack.len();\n\n/* Check if empty */\nlet is_empty = stack.is_empty();\n
      stack.c
      // C does not provide a built-in stack\n
      stack.kt
      /* Initialize stack */\nval stack = Stack<Int>()\n\n/* Push elements */\nstack.push(1)\nstack.push(3)\nstack.push(2)\nstack.push(5)\nstack.push(4)\n\n/* Access top element */\nval peek = stack.peek()\n\n/* Pop element */\nval pop = stack.pop()\n\n/* Get stack length */\nval size = stack.size\n\n/* Check if empty */\nval isEmpty = stack.isEmpty()\n
      stack.rb
      # Initialize stack\n# Ruby does not have a built-in stack class, can use Array as a stack\nstack = []\n\n# Push elements\nstack << 1\nstack << 3\nstack << 2\nstack << 5\nstack << 4\n\n# Access top element\npeek = stack.last\n\n# Pop element\npop = stack.pop\n\n# Get stack length\nsize = stack.length\n\n# Check if empty\nis_empty = stack.empty?\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_stack_and_queue/stack/#512-stack-implementation","title":"5.1.2 \u00a0 Stack Implementation","text":"

      To gain a deeper understanding of how a stack operates, let's try implementing a stack class ourselves.

      A stack follows the LIFO principle, so we can only add or remove elements at the top. However, both arrays and linked lists allow adding and removing elements at any position. Therefore, a stack can be viewed as a restricted array or linked list. In other words, we can \"shield\" some irrelevant operations of arrays or linked lists so that their external logic conforms to the characteristics of a stack.

      "},{"location":"chapter_stack_and_queue/stack/#1-linked-list-implementation","title":"1. \u00a0 Linked List Implementation","text":"

      When implementing a stack using a linked list, we can treat the head node of the linked list as the top of the stack and the tail node as the base.

      As shown in Figure 5-2, for the push operation, we simply insert an element at the head of the linked list. This node insertion method is called the \"head insertion method.\" For the pop operation, we just need to remove the head node from the linked list.

      LinkedListStackpush()pop()

      Figure 5-2 \u00a0 Push and pop operations in linked list implementation of stack

      Below is sample code for implementing a stack based on a linked list:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linkedlist_stack.py
      class LinkedListStack:\n    \"\"\"Stack based on linked list implementation\"\"\"\n\n    def __init__(self):\n        \"\"\"Constructor\"\"\"\n        self._peek: ListNode | None = None\n        self._size: int = 0\n\n    def size(self) -> int:\n        \"\"\"Get the length of the stack\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"Check if the stack is empty\"\"\"\n        return self._size == 0\n\n    def push(self, val: int):\n        \"\"\"Push\"\"\"\n        node = ListNode(val)\n        node.next = self._peek\n        self._peek = node\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"Pop\"\"\"\n        num = self.peek()\n        self._peek = self._peek.next\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"Access top of the stack element\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Stack is empty\")\n        return self._peek.val\n\n    def to_list(self) -> list[int]:\n        \"\"\"Convert to list for printing\"\"\"\n        arr = []\n        node = self._peek\n        while node:\n            arr.append(node.val)\n            node = node.next\n        arr.reverse()\n        return arr\n
      linkedlist_stack.cpp
      /* Stack based on linked list implementation */\nclass LinkedListStack {\n  private:\n    ListNode *stackTop; // Use head node as stack top\n    int stkSize;        // Stack length\n\n  public:\n    LinkedListStack() {\n        stackTop = nullptr;\n        stkSize = 0;\n    }\n\n    ~LinkedListStack() {\n        // Traverse linked list to delete nodes and free memory\n        freeMemoryLinkedList(stackTop);\n    }\n\n    /* Get the length of the stack */\n    int size() {\n        return stkSize;\n    }\n\n    /* Check if the stack is empty */\n    bool isEmpty() {\n        return size() == 0;\n    }\n\n    /* Push */\n    void push(int num) {\n        ListNode *node = new ListNode(num);\n        node->next = stackTop;\n        stackTop = node;\n        stkSize++;\n    }\n\n    /* Pop */\n    int pop() {\n        int num = top();\n        ListNode *tmp = stackTop;\n        stackTop = stackTop->next;\n        // Free memory\n        delete tmp;\n        stkSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    int top() {\n        if (isEmpty())\n            throw out_of_range(\"Stack is empty\");\n        return stackTop->val;\n    }\n\n    /* Convert List to Array and return */\n    vector<int> toVector() {\n        ListNode *node = stackTop;\n        vector<int> res(size());\n        for (int i = res.size() - 1; i >= 0; i--) {\n            res[i] = node->val;\n            node = node->next;\n        }\n        return res;\n    }\n};\n
      linkedlist_stack.java
      /* Stack based on linked list implementation */\nclass LinkedListStack {\n    private ListNode stackPeek; // Use head node as stack top\n    private int stkSize = 0; // Stack length\n\n    public LinkedListStack() {\n        stackPeek = null;\n    }\n\n    /* Get the length of the stack */\n    public int size() {\n        return stkSize;\n    }\n\n    /* Check if the stack is empty */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* Push */\n    public void push(int num) {\n        ListNode node = new ListNode(num);\n        node.next = stackPeek;\n        stackPeek = node;\n        stkSize++;\n    }\n\n    /* Pop */\n    public int pop() {\n        int num = peek();\n        stackPeek = stackPeek.next;\n        stkSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return stackPeek.val;\n    }\n\n    /* Convert List to Array and return */\n    public int[] toArray() {\n        ListNode node = stackPeek;\n        int[] res = new int[size()];\n        for (int i = res.length - 1; i >= 0; i--) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
      linkedlist_stack.cs
      /* Stack based on linked list implementation */\nclass LinkedListStack {\n    ListNode? stackPeek;  // Use head node as stack top\n    int stkSize = 0;   // Stack length\n\n    public LinkedListStack() {\n        stackPeek = null;\n    }\n\n    /* Get the length of the stack */\n    public int Size() {\n        return stkSize;\n    }\n\n    /* Check if the stack is empty */\n    public bool IsEmpty() {\n        return Size() == 0;\n    }\n\n    /* Push */\n    public void Push(int num) {\n        ListNode node = new(num) {\n            next = stackPeek\n        };\n        stackPeek = node;\n        stkSize++;\n    }\n\n    /* Pop */\n    public int Pop() {\n        int num = Peek();\n        stackPeek = stackPeek!.next;\n        stkSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    public int Peek() {\n        if (IsEmpty())\n            throw new Exception();\n        return stackPeek!.val;\n    }\n\n    /* Convert List to Array and return */\n    public int[] ToArray() {\n        if (stackPeek == null)\n            return [];\n\n        ListNode? node = stackPeek;\n        int[] res = new int[Size()];\n        for (int i = res.Length - 1; i >= 0; i--) {\n            res[i] = node!.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
      linkedlist_stack.go
      /* Stack based on linked list implementation */\ntype linkedListStack struct {\n    // Use built-in package list to implement stack\n    data *list.List\n}\n\n/* Access top of the stack element */\nfunc newLinkedListStack() *linkedListStack {\n    return &linkedListStack{\n        data: list.New(),\n    }\n}\n\n/* Push */\nfunc (s *linkedListStack) push(value int) {\n    s.data.PushBack(value)\n}\n\n/* Pop */\nfunc (s *linkedListStack) pop() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Back()\n    s.data.Remove(e)\n    return e.Value\n}\n\n/* Return list for printing */\nfunc (s *linkedListStack) peek() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Back()\n    return e.Value\n}\n\n/* Get the length of the stack */\nfunc (s *linkedListStack) size() int {\n    return s.data.Len()\n}\n\n/* Check if the stack is empty */\nfunc (s *linkedListStack) isEmpty() bool {\n    return s.data.Len() == 0\n}\n\n/* Get List for printing */\nfunc (s *linkedListStack) toList() *list.List {\n    return s.data\n}\n
      linkedlist_stack.swift
      /* Stack based on linked list implementation */\nclass LinkedListStack {\n    private var _peek: ListNode? // Use head node as stack top\n    private var _size: Int // Stack length\n\n    init() {\n        _size = 0\n    }\n\n    /* Get the length of the stack */\n    func size() -> Int {\n        _size\n    }\n\n    /* Check if the stack is empty */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* Push */\n    func push(num: Int) {\n        let node = ListNode(x: num)\n        node.next = _peek\n        _peek = node\n        _size += 1\n    }\n\n    /* Pop */\n    @discardableResult\n    func pop() -> Int {\n        let num = peek()\n        _peek = _peek?.next\n        _size -= 1\n        return num\n    }\n\n    /* Return list for printing */\n    func peek() -> Int {\n        if isEmpty() {\n            fatalError(\"Stack is empty\")\n        }\n        return _peek!.val\n    }\n\n    /* Convert List to Array and return */\n    func toArray() -> [Int] {\n        var node = _peek\n        var res = Array(repeating: 0, count: size())\n        for i in res.indices.reversed() {\n            res[i] = node!.val\n            node = node?.next\n        }\n        return res\n    }\n}\n
      linkedlist_stack.js
      /* Stack based on linked list implementation */\nclass LinkedListStack {\n    #stackPeek; // Use head node as stack top\n    #stkSize = 0; // Stack length\n\n    constructor() {\n        this.#stackPeek = null;\n    }\n\n    /* Get the length of the stack */\n    get size() {\n        return this.#stkSize;\n    }\n\n    /* Check if the stack is empty */\n    isEmpty() {\n        return this.size === 0;\n    }\n\n    /* Push */\n    push(num) {\n        const node = new ListNode(num);\n        node.next = this.#stackPeek;\n        this.#stackPeek = node;\n        this.#stkSize++;\n    }\n\n    /* Pop */\n    pop() {\n        const num = this.peek();\n        this.#stackPeek = this.#stackPeek.next;\n        this.#stkSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peek() {\n        if (!this.#stackPeek) throw new Error('Stack is empty');\n        return this.#stackPeek.val;\n    }\n\n    /* Convert linked list to Array and return */\n    toArray() {\n        let node = this.#stackPeek;\n        const res = new Array(this.size);\n        for (let i = res.length - 1; i >= 0; i--) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
      linkedlist_stack.ts
      /* Stack based on linked list implementation */\nclass LinkedListStack {\n    private stackPeek: ListNode | null; // Use head node as stack top\n    private stkSize: number = 0; // Stack length\n\n    constructor() {\n        this.stackPeek = null;\n    }\n\n    /* Get the length of the stack */\n    get size(): number {\n        return this.stkSize;\n    }\n\n    /* Check if the stack is empty */\n    isEmpty(): boolean {\n        return this.size === 0;\n    }\n\n    /* Push */\n    push(num: number): void {\n        const node = new ListNode(num);\n        node.next = this.stackPeek;\n        this.stackPeek = node;\n        this.stkSize++;\n    }\n\n    /* Pop */\n    pop(): number {\n        const num = this.peek();\n        if (!this.stackPeek) throw new Error('Stack is empty');\n        this.stackPeek = this.stackPeek.next;\n        this.stkSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peek(): number {\n        if (!this.stackPeek) throw new Error('Stack is empty');\n        return this.stackPeek.val;\n    }\n\n    /* Convert linked list to Array and return */\n    toArray(): number[] {\n        let node = this.stackPeek;\n        const res = new Array<number>(this.size);\n        for (let i = res.length - 1; i >= 0; i--) {\n            res[i] = node!.val;\n            node = node!.next;\n        }\n        return res;\n    }\n}\n
      linkedlist_stack.dart
      /* Stack implemented based on linked list class */\nclass LinkedListStack {\n  ListNode? _stackPeek; // Use head node as stack top\n  int _stkSize = 0; // Stack length\n\n  LinkedListStack() {\n    _stackPeek = null;\n  }\n\n  /* Get the length of the stack */\n  int size() {\n    return _stkSize;\n  }\n\n  /* Check if the stack is empty */\n  bool isEmpty() {\n    return _stkSize == 0;\n  }\n\n  /* Push */\n  void push(int _num) {\n    final ListNode node = ListNode(_num);\n    node.next = _stackPeek;\n    _stackPeek = node;\n    _stkSize++;\n  }\n\n  /* Pop */\n  int pop() {\n    final int _num = peek();\n    _stackPeek = _stackPeek!.next;\n    _stkSize--;\n    return _num;\n  }\n\n  /* Return list for printing */\n  int peek() {\n    if (_stackPeek == null) {\n      throw Exception(\"Stack is empty\");\n    }\n    return _stackPeek!.val;\n  }\n\n  /* Convert linked list to List and return */\n  List<int> toList() {\n    ListNode? node = _stackPeek;\n    List<int> list = [];\n    while (node != null) {\n      list.add(node.val);\n      node = node.next;\n    }\n    list = list.reversed.toList();\n    return list;\n  }\n}\n
      linkedlist_stack.rs
      /* Stack based on linked list implementation */\n#[allow(dead_code)]\npub struct LinkedListStack<T> {\n    stack_peek: Option<Rc<RefCell<ListNode<T>>>>, // Use head node as stack top\n    stk_size: usize,                              // Stack length\n}\n\nimpl<T: Copy> LinkedListStack<T> {\n    pub fn new() -> Self {\n        Self {\n            stack_peek: None,\n            stk_size: 0,\n        }\n    }\n\n    /* Get the length of the stack */\n    pub fn size(&self) -> usize {\n        return self.stk_size;\n    }\n\n    /* Check if the stack is empty */\n    pub fn is_empty(&self) -> bool {\n        return self.size() == 0;\n    }\n\n    /* Push */\n    pub fn push(&mut self, num: T) {\n        let node = ListNode::new(num);\n        node.borrow_mut().next = self.stack_peek.take();\n        self.stack_peek = Some(node);\n        self.stk_size += 1;\n    }\n\n    /* Pop */\n    pub fn pop(&mut self) -> Option<T> {\n        self.stack_peek.take().map(|old_head| {\n            self.stack_peek = old_head.borrow_mut().next.take();\n            self.stk_size -= 1;\n\n            old_head.borrow().val\n        })\n    }\n\n    /* Return list for printing */\n    pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n        self.stack_peek.as_ref()\n    }\n\n    /* Convert List to Array and return */\n    pub fn to_array(&self) -> Vec<T> {\n        fn _to_array<T: Sized + Copy>(head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n            if let Some(node) = head {\n                let mut nums = _to_array(node.borrow().next.as_ref());\n                nums.push(node.borrow().val);\n                return nums;\n            }\n            return Vec::new();\n        }\n\n        _to_array(self.peek())\n    }\n}\n
      linkedlist_stack.c
      /* Stack based on linked list implementation */\ntypedef struct {\n    ListNode *top; // Use head node as stack top\n    int size;      // Stack length\n} LinkedListStack;\n\n/* Constructor */\nLinkedListStack *newLinkedListStack() {\n    LinkedListStack *s = malloc(sizeof(LinkedListStack));\n    s->top = NULL;\n    s->size = 0;\n    return s;\n}\n\n/* Destructor */\nvoid delLinkedListStack(LinkedListStack *s) {\n    while (s->top) {\n        ListNode *n = s->top->next;\n        free(s->top);\n        s->top = n;\n    }\n    free(s);\n}\n\n/* Get the length of the stack */\nint size(LinkedListStack *s) {\n    return s->size;\n}\n\n/* Check if the stack is empty */\nbool isEmpty(LinkedListStack *s) {\n    return size(s) == 0;\n}\n\n/* Push */\nvoid push(LinkedListStack *s, int num) {\n    ListNode *node = (ListNode *)malloc(sizeof(ListNode));\n    node->next = s->top; // Update new node's pointer field\n    node->val = num;     // Update new node's data field\n    s->top = node;       // Update stack top\n    s->size++;           // Update stack size\n}\n\n/* Return list for printing */\nint peek(LinkedListStack *s) {\n    if (s->size == 0) {\n        printf(\"Stack is empty\\n\");\n        return INT_MAX;\n    }\n    return s->top->val;\n}\n\n/* Pop */\nint pop(LinkedListStack *s) {\n    int val = peek(s);\n    ListNode *tmp = s->top;\n    s->top = s->top->next;\n    // Free memory\n    free(tmp);\n    s->size--;\n    return val;\n}\n
      linkedlist_stack.kt
      /* Stack based on linked list implementation */\nclass LinkedListStack(\n    private var stackPeek: ListNode? = null, // Use head node as stack top\n    private var stkSize: Int = 0 // Stack length\n) {\n\n    /* Get the length of the stack */\n    fun size(): Int {\n        return stkSize\n    }\n\n    /* Check if the stack is empty */\n    fun isEmpty(): Boolean {\n        return size() == 0\n    }\n\n    /* Push */\n    fun push(num: Int) {\n        val node = ListNode(num)\n        node.next = stackPeek\n        stackPeek = node\n        stkSize++\n    }\n\n    /* Pop */\n    fun pop(): Int? {\n        val num = peek()\n        stackPeek = stackPeek?.next\n        stkSize--\n        return num\n    }\n\n    /* Return list for printing */\n    fun peek(): Int? {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return stackPeek?._val\n    }\n\n    /* Convert List to Array and return */\n    fun toArray(): IntArray {\n        var node = stackPeek\n        val res = IntArray(size())\n        for (i in res.size - 1 downTo 0) {\n            res[i] = node?._val!!\n            node = node.next\n        }\n        return res\n    }\n}\n
      linkedlist_stack.rb
      ### Stack based on linked list ###\nclass LinkedListStack\n  attr_reader :size\n\n  ### Constructor ###\n  def initialize\n    @size = 0\n  end\n\n  ### Check if stack is empty ###\n  def is_empty?\n    @peek.nil?\n  end\n\n  ### Push ###\n  def push(val)\n    node = ListNode.new(val)\n    node.next = @peek\n    @peek = node\n    @size += 1\n  end\n\n  ### Pop ###\n  def pop\n    num = peek\n    @peek = @peek.next\n    @size -= 1\n    num\n  end\n\n  ### Access top element ###\n  def peek\n    raise IndexError, 'Stack is empty' if is_empty?\n\n    @peek.val\n  end\n\n  ### Convert linked list to Array and return ###\n  def to_array\n    arr = []\n    node = @peek\n    while node\n      arr << node.val\n      node = node.next\n    end\n    arr.reverse\n  end\nend\n
      "},{"location":"chapter_stack_and_queue/stack/#2-array-implementation","title":"2. \u00a0 Array Implementation","text":"

      When implementing a stack using an array, we can treat the end of the array as the top of the stack. As shown in Figure 5-3, push and pop operations correspond to adding and removing elements at the end of the array, both with a time complexity of \\(O(1)\\).

      ArrayStackpush()pop()

      Figure 5-3 \u00a0 Push and pop operations in array implementation of stack

      Since elements pushed onto the stack may increase continuously, we can use a dynamic array, which eliminates the need to handle array expansion ourselves. Here is the sample code:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_stack.py
      class ArrayStack:\n    \"\"\"Stack based on array implementation\"\"\"\n\n    def __init__(self):\n        \"\"\"Constructor\"\"\"\n        self._stack: list[int] = []\n\n    def size(self) -> int:\n        \"\"\"Get the length of the stack\"\"\"\n        return len(self._stack)\n\n    def is_empty(self) -> bool:\n        \"\"\"Check if the stack is empty\"\"\"\n        return self.size() == 0\n\n    def push(self, item: int):\n        \"\"\"Push\"\"\"\n        self._stack.append(item)\n\n    def pop(self) -> int:\n        \"\"\"Pop\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Stack is empty\")\n        return self._stack.pop()\n\n    def peek(self) -> int:\n        \"\"\"Access top of the stack element\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Stack is empty\")\n        return self._stack[-1]\n\n    def to_list(self) -> list[int]:\n        \"\"\"Return list for printing\"\"\"\n        return self._stack\n
      array_stack.cpp
      /* Stack based on array implementation */\nclass ArrayStack {\n  private:\n    vector<int> stack;\n\n  public:\n    /* Get the length of the stack */\n    int size() {\n        return stack.size();\n    }\n\n    /* Check if the stack is empty */\n    bool isEmpty() {\n        return stack.size() == 0;\n    }\n\n    /* Push */\n    void push(int num) {\n        stack.push_back(num);\n    }\n\n    /* Pop */\n    int pop() {\n        int num = top();\n        stack.pop_back();\n        return num;\n    }\n\n    /* Return list for printing */\n    int top() {\n        if (isEmpty())\n            throw out_of_range(\"Stack is empty\");\n        return stack.back();\n    }\n\n    /* Return Vector */\n    vector<int> toVector() {\n        return stack;\n    }\n};\n
      array_stack.java
      /* Stack based on array implementation */\nclass ArrayStack {\n    private ArrayList<Integer> stack;\n\n    public ArrayStack() {\n        // Initialize list (dynamic array)\n        stack = new ArrayList<>();\n    }\n\n    /* Get the length of the stack */\n    public int size() {\n        return stack.size();\n    }\n\n    /* Check if the stack is empty */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* Push */\n    public void push(int num) {\n        stack.add(num);\n    }\n\n    /* Pop */\n    public int pop() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return stack.remove(size() - 1);\n    }\n\n    /* Return list for printing */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return stack.get(size() - 1);\n    }\n\n    /* Convert List to Array and return */\n    public Object[] toArray() {\n        return stack.toArray();\n    }\n}\n
      array_stack.cs
      /* Stack based on array implementation */\nclass ArrayStack {\n    List<int> stack;\n    public ArrayStack() {\n        // Initialize list (dynamic array)\n        stack = [];\n    }\n\n    /* Get the length of the stack */\n    public int Size() {\n        return stack.Count;\n    }\n\n    /* Check if the stack is empty */\n    public bool IsEmpty() {\n        return Size() == 0;\n    }\n\n    /* Push */\n    public void Push(int num) {\n        stack.Add(num);\n    }\n\n    /* Pop */\n    public int Pop() {\n        if (IsEmpty())\n            throw new Exception();\n        var val = Peek();\n        stack.RemoveAt(Size() - 1);\n        return val;\n    }\n\n    /* Return list for printing */\n    public int Peek() {\n        if (IsEmpty())\n            throw new Exception();\n        return stack[Size() - 1];\n    }\n\n    /* Convert List to Array and return */\n    public int[] ToArray() {\n        return [.. stack];\n    }\n}\n
      array_stack.go
      /* Stack based on array implementation */\ntype arrayStack struct {\n    data []int // Data\n}\n\n/* Access top of the stack element */\nfunc newArrayStack() *arrayStack {\n    return &arrayStack{\n        // Set stack length to 0, capacity to 16\n        data: make([]int, 0, 16),\n    }\n}\n\n/* Stack length */\nfunc (s *arrayStack) size() int {\n    return len(s.data)\n}\n\n/* Is stack empty */\nfunc (s *arrayStack) isEmpty() bool {\n    return s.size() == 0\n}\n\n/* Push */\nfunc (s *arrayStack) push(v int) {\n    // Slice will automatically expand\n    s.data = append(s.data, v)\n}\n\n/* Pop */\nfunc (s *arrayStack) pop() any {\n    val := s.peek()\n    s.data = s.data[:len(s.data)-1]\n    return val\n}\n\n/* Get stack top element */\nfunc (s *arrayStack) peek() any {\n    if s.isEmpty() {\n        return nil\n    }\n    val := s.data[len(s.data)-1]\n    return val\n}\n\n/* Get Slice for printing */\nfunc (s *arrayStack) toSlice() []int {\n    return s.data\n}\n
      array_stack.swift
      /* Stack based on array implementation */\nclass ArrayStack {\n    private var stack: [Int]\n\n    init() {\n        // Initialize list (dynamic array)\n        stack = []\n    }\n\n    /* Get the length of the stack */\n    func size() -> Int {\n        stack.count\n    }\n\n    /* Check if the stack is empty */\n    func isEmpty() -> Bool {\n        stack.isEmpty\n    }\n\n    /* Push */\n    func push(num: Int) {\n        stack.append(num)\n    }\n\n    /* Pop */\n    @discardableResult\n    func pop() -> Int {\n        if isEmpty() {\n            fatalError(\"Stack is empty\")\n        }\n        return stack.removeLast()\n    }\n\n    /* Return list for printing */\n    func peek() -> Int {\n        if isEmpty() {\n            fatalError(\"Stack is empty\")\n        }\n        return stack.last!\n    }\n\n    /* Convert List to Array and return */\n    func toArray() -> [Int] {\n        stack\n    }\n}\n
      array_stack.js
      /* Stack based on array implementation */\nclass ArrayStack {\n    #stack;\n    constructor() {\n        this.#stack = [];\n    }\n\n    /* Get the length of the stack */\n    get size() {\n        return this.#stack.length;\n    }\n\n    /* Check if the stack is empty */\n    isEmpty() {\n        return this.#stack.length === 0;\n    }\n\n    /* Push */\n    push(num) {\n        this.#stack.push(num);\n    }\n\n    /* Pop */\n    pop() {\n        if (this.isEmpty()) throw new Error('Stack is empty');\n        return this.#stack.pop();\n    }\n\n    /* Return list for printing */\n    top() {\n        if (this.isEmpty()) throw new Error('Stack is empty');\n        return this.#stack[this.#stack.length - 1];\n    }\n\n    /* Return Array */\n    toArray() {\n        return this.#stack;\n    }\n}\n
      array_stack.ts
      /* Stack based on array implementation */\nclass ArrayStack {\n    private stack: number[];\n    constructor() {\n        this.stack = [];\n    }\n\n    /* Get the length of the stack */\n    get size(): number {\n        return this.stack.length;\n    }\n\n    /* Check if the stack is empty */\n    isEmpty(): boolean {\n        return this.stack.length === 0;\n    }\n\n    /* Push */\n    push(num: number): void {\n        this.stack.push(num);\n    }\n\n    /* Pop */\n    pop(): number | undefined {\n        if (this.isEmpty()) throw new Error('Stack is empty');\n        return this.stack.pop();\n    }\n\n    /* Return list for printing */\n    top(): number | undefined {\n        if (this.isEmpty()) throw new Error('Stack is empty');\n        return this.stack[this.stack.length - 1];\n    }\n\n    /* Return Array */\n    toArray() {\n        return this.stack;\n    }\n}\n
      array_stack.dart
      /* Stack based on array implementation */\nclass ArrayStack {\n  late List<int> _stack;\n  ArrayStack() {\n    _stack = [];\n  }\n\n  /* Get the length of the stack */\n  int size() {\n    return _stack.length;\n  }\n\n  /* Check if the stack is empty */\n  bool isEmpty() {\n    return _stack.isEmpty;\n  }\n\n  /* Push */\n  void push(int _num) {\n    _stack.add(_num);\n  }\n\n  /* Pop */\n  int pop() {\n    if (isEmpty()) {\n      throw Exception(\"Stack is empty\");\n    }\n    return _stack.removeLast();\n  }\n\n  /* Return list for printing */\n  int peek() {\n    if (isEmpty()) {\n      throw Exception(\"Stack is empty\");\n    }\n    return _stack.last;\n  }\n\n  /* Convert stack to Array and return */\n  List<int> toArray() => _stack;\n}\n
      array_stack.rs
      /* Stack based on array implementation */\nstruct ArrayStack<T> {\n    stack: Vec<T>,\n}\n\nimpl<T> ArrayStack<T> {\n    /* Access top of the stack element */\n    fn new() -> ArrayStack<T> {\n        ArrayStack::<T> {\n            stack: Vec::<T>::new(),\n        }\n    }\n\n    /* Get the length of the stack */\n    fn size(&self) -> usize {\n        self.stack.len()\n    }\n\n    /* Check if the stack is empty */\n    fn is_empty(&self) -> bool {\n        self.size() == 0\n    }\n\n    /* Push */\n    fn push(&mut self, num: T) {\n        self.stack.push(num);\n    }\n\n    /* Pop */\n    fn pop(&mut self) -> Option<T> {\n        self.stack.pop()\n    }\n\n    /* Return list for printing */\n    fn peek(&self) -> Option<&T> {\n        if self.is_empty() {\n            panic!(\"Stack is empty\")\n        };\n        self.stack.last()\n    }\n\n    /* Return &Vec */\n    fn to_array(&self) -> &Vec<T> {\n        &self.stack\n    }\n}\n
      array_stack.c
      /* Stack based on array implementation */\ntypedef struct {\n    int *data;\n    int size;\n} ArrayStack;\n\n/* Constructor */\nArrayStack *newArrayStack() {\n    ArrayStack *stack = malloc(sizeof(ArrayStack));\n    // Initialize with large capacity to avoid expansion\n    stack->data = malloc(sizeof(int) * MAX_SIZE);\n    stack->size = 0;\n    return stack;\n}\n\n/* Destructor */\nvoid delArrayStack(ArrayStack *stack) {\n    free(stack->data);\n    free(stack);\n}\n\n/* Get the length of the stack */\nint size(ArrayStack *stack) {\n    return stack->size;\n}\n\n/* Check if the stack is empty */\nbool isEmpty(ArrayStack *stack) {\n    return stack->size == 0;\n}\n\n/* Push */\nvoid push(ArrayStack *stack, int num) {\n    if (stack->size == MAX_SIZE) {\n        printf(\"Stack is full\\n\");\n        return;\n    }\n    stack->data[stack->size] = num;\n    stack->size++;\n}\n\n/* Return list for printing */\nint peek(ArrayStack *stack) {\n    if (stack->size == 0) {\n        printf(\"Stack is empty\\n\");\n        return INT_MAX;\n    }\n    return stack->data[stack->size - 1];\n}\n\n/* Pop */\nint pop(ArrayStack *stack) {\n    int val = peek(stack);\n    stack->size--;\n    return val;\n}\n
      array_stack.kt
      /* Stack based on array implementation */\nclass ArrayStack {\n    // Initialize list (dynamic array)\n    private val stack = mutableListOf<Int>()\n\n    /* Get the length of the stack */\n    fun size(): Int {\n        return stack.size\n    }\n\n    /* Check if the stack is empty */\n    fun isEmpty(): Boolean {\n        return size() == 0\n    }\n\n    /* Push */\n    fun push(num: Int) {\n        stack.add(num)\n    }\n\n    /* Pop */\n    fun pop(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return stack.removeAt(size() - 1)\n    }\n\n    /* Return list for printing */\n    fun peek(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return stack[size() - 1]\n    }\n\n    /* Convert List to Array and return */\n    fun toArray(): Array<Any> {\n        return stack.toTypedArray()\n    }\n}\n
      array_stack.rb
      ### Stack based on array ###\nclass ArrayStack\n  ### Constructor ###\n  def initialize\n    @stack = []\n  end\n\n  ### Get stack length ###\n  def size\n    @stack.length\n  end\n\n  ### Check if stack is empty ###\n  def is_empty?\n    @stack.empty?\n  end\n\n  ### Push ###\n  def push(item)\n    @stack << item\n  end\n\n  ### Pop ###\n  def pop\n    raise IndexError, 'Stack is empty' if is_empty?\n\n    @stack.pop\n  end\n\n  ### Access top element ###\n  def peek\n    raise IndexError, 'Stack is empty' if is_empty?\n\n    @stack.last\n  end\n\n  ### Return list for printing ###\n  def to_array\n    @stack\n  end\nend\n
      "},{"location":"chapter_stack_and_queue/stack/#513-comparison-of-the-two-implementations","title":"5.1.3 \u00a0 Comparison of the Two Implementations","text":"

      Supported Operations

      Both implementations support all operations defined by the stack. The array implementation additionally supports random access, but this goes beyond the stack definition and is generally not used.

      Time Efficiency

      In the array-based implementation, both push and pop operations occur in pre-allocated contiguous memory, which has good cache locality and is therefore more efficient. However, if pushing exceeds the array capacity, it triggers an expansion mechanism, causing the time complexity of that particular push operation to become \\(O(n)\\).

      In the linked list-based implementation, list expansion is very flexible, and there is no issue of reduced efficiency due to array expansion. However, the push operation requires initializing a node object and modifying pointers, so it is relatively less efficient. Nevertheless, if the pushed elements are already node objects, the initialization step can be omitted, thereby improving efficiency.

      In summary, when the elements pushed and popped are basic data types such as int or double, we can draw the following conclusions:

      • The array-based stack implementation has reduced efficiency when expansion is triggered, but since expansion is an infrequent operation, the average efficiency is higher.
      • The linked list-based stack implementation can provide more stable efficiency performance.

      Space Efficiency

      When initializing a list, the system allocates an \"initial capacity\" that may exceed the actual need. Additionally, the expansion mechanism typically expands at a specific ratio (e.g., 2x), and the capacity after expansion may also exceed actual needs. Therefore, the array-based stack implementation may cause some space wastage.

      However, since linked list nodes need to store additional pointers, the space occupied by linked list nodes is relatively large.

      In summary, we cannot simply determine which implementation is more memory-efficient and need to analyze the specific situation.

      "},{"location":"chapter_stack_and_queue/stack/#514-typical-applications-of-stack","title":"5.1.4 \u00a0 Typical Applications of Stack","text":"
      • Back and forward in browsers, undo and redo in software. Every time we open a new webpage, the browser pushes the previous page onto the stack, allowing us to return to the previous page via the back operation. The back operation is essentially performing a pop. To support both back and forward, two stacks are needed to work together.
      • Program memory management. Each time a function is called, the system adds a stack frame to the top of the stack to record the function's context information. During recursion, the downward recursive phase continuously performs push operations, while the upward backtracking phase continuously performs pop operations.
      "},{"location":"chapter_stack_and_queue/summary/","title":"5.4 \u00a0 Summary","text":""},{"location":"chapter_stack_and_queue/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • A stack is a data structure that follows the LIFO principle and can be implemented using arrays or linked lists.
      • In terms of time efficiency, the array implementation of a stack has higher average efficiency, but during expansion, the time complexity of a single push operation degrades to \\(O(n)\\). In contrast, the linked list implementation of a stack provides more stable efficiency performance.
      • In terms of space efficiency, the array implementation of a stack may lead to some degree of space wastage. However, it should be noted that the memory space occupied by linked list nodes is larger than that of array elements.
      • A queue is a data structure that follows the FIFO principle and can also be implemented using arrays or linked lists. The conclusions regarding time efficiency and space efficiency comparisons for queues are similar to those for stacks mentioned above.
      • A deque is a queue with greater flexibility that allows adding and removing elements at both ends.
      "},{"location":"chapter_stack_and_queue/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

      Q: Is the browser's forward and backward functionality implemented with a doubly linked list?

      The forward and backward functionality of a browser is essentially a manifestation of a \"stack.\" When a user visits a new page, that page is added to the top of the stack; when the user clicks the back button, that page is popped from the top of the stack. Using a deque can conveniently implement some additional operations, as mentioned in the \"Deque\" section.

      Q: After popping from the stack, do we need to free the memory of the popped node?

      If the popped node will still be needed later, then memory does not need to be freed. If it won't be used afterward, languages like Java and Python have automatic garbage collection, so manual memory deallocation is not required; in C and C++, manual memory deallocation is necessary.

      Q: A deque seems like two stacks joined together. What is its purpose?

      A deque is like a combination of a stack and a queue, or two stacks joined together. It exhibits the logic of both stack and queue, so it can implement all applications of stacks and queues, and is more flexible.

      Q: How are undo and redo specifically implemented?

      Use two stacks: stack A for undo and stack B for redo.

      1. Whenever the user performs an operation, push this operation onto stack A and clear stack B.
      2. When the user performs \"undo,\" pop the most recent operation from stack A and push it onto stack B.
      3. When the user performs \"redo,\" pop the most recent operation from stack B and push it onto stack A.
      "},{"location":"chapter_tree/","title":"Chapter 7. \u00a0 Tree","text":"

      Abstract

      Towering trees are full of vitality, with deep roots and lush leaves, spreading branches and flourishing.

      They show us the vivid form of divide and conquer in data.

      "},{"location":"chapter_tree/#chapter-contents","title":"Chapter contents","text":"
      • 7.1 \u00a0 Binary Tree
      • 7.2 \u00a0 Binary Tree Traversal
      • 7.3 \u00a0 Array Representation of Tree
      • 7.4 \u00a0 Binary Search Tree
      • 7.5 \u00a0 AVL Tree *
      • 7.6 \u00a0 Summary
      "},{"location":"chapter_tree/array_representation_of_tree/","title":"7.3 \u00a0 Array Representation of Binary Trees","text":"

      Under the linked list representation, the storage unit of a binary tree is a node TreeNode, and nodes are connected by pointers. The previous section introduced the basic operations of binary trees under the linked list representation.

      So, can we use an array to represent a binary tree? The answer is yes.

      "},{"location":"chapter_tree/array_representation_of_tree/#731-representing-perfect-binary-trees","title":"7.3.1 \u00a0 Representing Perfect Binary Trees","text":"

      Let's analyze a simple case first. Given a perfect binary tree, we store all nodes in an array according to the order of level-order traversal, where each node corresponds to a unique array index.

      Based on the characteristics of level-order traversal, we can derive a \"mapping formula\" between parent node index and child node indices: If a node's index is \\(i\\), then its left child index is \\(2i + 1\\) and its right child index is \\(2i + 2\\). Figure 7-12 shows the mapping relationships between various node indices.

      Figure 7-12 \u00a0 Array representation of a perfect binary tree

      The mapping formula plays a role similar to the node references (pointers) in linked lists. Given any node in the array, we can access its left (right) child node using the mapping formula.

      "},{"location":"chapter_tree/array_representation_of_tree/#732-representing-any-binary-tree","title":"7.3.2 \u00a0 Representing Any Binary Tree","text":"

      Perfect binary trees are a special case; in the middle levels of a binary tree, there are typically many None values. Since the level-order traversal sequence does not include these None values, we cannot infer the number and distribution of None values based on this sequence alone. This means multiple binary tree structures can correspond to the same level-order traversal sequence.

      As shown in Figure 7-13, given a non-perfect binary tree, the above method of array representation fails.

      Figure 7-13 \u00a0 Level-order traversal sequence corresponds to multiple binary tree possibilities

      To solve this problem, we can consider explicitly writing out all None values in the level-order traversal sequence. As shown in Figure 7-14, after this treatment, the level-order traversal sequence can uniquely represent a binary tree. Example code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      # Array representation of a binary tree\n# Using None to represent empty slots\ntree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]\n
      /* Array representation of a binary tree */\n// Using the maximum integer value INT_MAX to mark empty slots\nvector<int> tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
      /* Array representation of a binary tree */\n// Using the Integer wrapper class allows for using null to mark empty slots\nInteger[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };\n
      /* Array representation of a binary tree */\n// Using nullable int (int?) allows for using null to mark empty slots\nint?[] tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
      /* Array representation of a binary tree */\n// Using an any type slice, allowing for nil to mark empty slots\ntree := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15}\n
      /* Array representation of a binary tree */\n// Using optional Int (Int?) allows for using nil to mark empty slots\nlet tree: [Int?] = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]\n
      /* Array representation of a binary tree */\n// Using null to represent empty slots\nlet tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
      /* Array representation of a binary tree */\n// Using null to represent empty slots\nlet tree: (number | null)[] = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
      /* Array representation of a binary tree */\n// Using nullable int (int?) allows for using null to mark empty slots\nList<int?> tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
      /* Array representation of a binary tree */\n// Using None to mark empty slots\nlet tree = [Some(1), Some(2), Some(3), Some(4), None, Some(6), Some(7), Some(8), Some(9), None, None, Some(12), None, None, Some(15)];\n
      /* Array representation of a binary tree */\n// Using the maximum int value to mark empty slots, therefore, node values must not be INT_MAX\nint tree[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
      /* Array representation of a binary tree */\n// Using null to represent empty slots\nval tree = arrayOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 )\n
      ### Array representation of a binary tree ###\n# Using nil to represent empty slots\ntree = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]\n

      Figure 7-14 \u00a0 Array representation of any type of binary tree

      It's worth noting that complete binary trees are very well-suited for array representation. Recalling the definition of a complete binary tree, None only appears at the bottom level and towards the right, meaning all None values must appear at the end of the level-order traversal sequence.

      This means that when using an array to represent a complete binary tree, it's possible to omit storing all None values, which is very convenient. Figure 7-15 gives an example.

      Figure 7-15 \u00a0 Array representation of a complete binary tree

      The following code implements a binary tree based on array representation, including the following operations:

      • Given a certain node, obtain its value, left (right) child node, and parent node.
      • Obtain the preorder, inorder, postorder, and level-order traversal sequences.
      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_binary_tree.py
      class ArrayBinaryTree:\n    \"\"\"Binary tree class represented by array\"\"\"\n\n    def __init__(self, arr: list[int | None]):\n        \"\"\"Constructor\"\"\"\n        self._tree = list(arr)\n\n    def size(self):\n        \"\"\"List capacity\"\"\"\n        return len(self._tree)\n\n    def val(self, i: int) -> int | None:\n        \"\"\"Get value of node at index i\"\"\"\n        # If index is out of bounds, return None, representing empty position\n        if i < 0 or i >= self.size():\n            return None\n        return self._tree[i]\n\n    def left(self, i: int) -> int | None:\n        \"\"\"Get index of left child node of node at index i\"\"\"\n        return 2 * i + 1\n\n    def right(self, i: int) -> int | None:\n        \"\"\"Get index of right child node of node at index i\"\"\"\n        return 2 * i + 2\n\n    def parent(self, i: int) -> int | None:\n        \"\"\"Get index of parent node of node at index i\"\"\"\n        return (i - 1) // 2\n\n    def level_order(self) -> list[int]:\n        \"\"\"Level-order traversal\"\"\"\n        self.res = []\n        # Traverse array directly\n        for i in range(self.size()):\n            if self.val(i) is not None:\n                self.res.append(self.val(i))\n        return self.res\n\n    def dfs(self, i: int, order: str):\n        \"\"\"Depth-first traversal\"\"\"\n        if self.val(i) is None:\n            return\n        # Preorder traversal\n        if order == \"pre\":\n            self.res.append(self.val(i))\n        self.dfs(self.left(i), order)\n        # Inorder traversal\n        if order == \"in\":\n            self.res.append(self.val(i))\n        self.dfs(self.right(i), order)\n        # Postorder traversal\n        if order == \"post\":\n            self.res.append(self.val(i))\n\n    def pre_order(self) -> list[int]:\n        \"\"\"Preorder traversal\"\"\"\n        self.res = []\n        self.dfs(0, order=\"pre\")\n        return self.res\n\n    def in_order(self) -> list[int]:\n        \"\"\"Inorder traversal\"\"\"\n        self.res = []\n        self.dfs(0, order=\"in\")\n        return self.res\n\n    def post_order(self) -> list[int]:\n        \"\"\"Postorder traversal\"\"\"\n        self.res = []\n        self.dfs(0, order=\"post\")\n        return self.res\n
      array_binary_tree.cpp
      /* Binary tree class represented by array */\nclass ArrayBinaryTree {\n  public:\n    /* Constructor */\n    ArrayBinaryTree(vector<int> arr) {\n        tree = arr;\n    }\n\n    /* List capacity */\n    int size() {\n        return tree.size();\n    }\n\n    /* Get value of node at index i */\n    int val(int i) {\n        // Return INT_MAX if index out of bounds, representing empty position\n        if (i < 0 || i >= size())\n            return INT_MAX;\n        return tree[i];\n    }\n\n    /* Get index of left child node of node at index i */\n    int left(int i) {\n        return 2 * i + 1;\n    }\n\n    /* Get index of right child node of node at index i */\n    int right(int i) {\n        return 2 * i + 2;\n    }\n\n    /* Get index of parent node of node at index i */\n    int parent(int i) {\n        return (i - 1) / 2;\n    }\n\n    /* Level-order traversal */\n    vector<int> levelOrder() {\n        vector<int> res;\n        // Traverse array directly\n        for (int i = 0; i < size(); i++) {\n            if (val(i) != INT_MAX)\n                res.push_back(val(i));\n        }\n        return res;\n    }\n\n    /* Preorder traversal */\n    vector<int> preOrder() {\n        vector<int> res;\n        dfs(0, \"pre\", res);\n        return res;\n    }\n\n    /* Inorder traversal */\n    vector<int> inOrder() {\n        vector<int> res;\n        dfs(0, \"in\", res);\n        return res;\n    }\n\n    /* Postorder traversal */\n    vector<int> postOrder() {\n        vector<int> res;\n        dfs(0, \"post\", res);\n        return res;\n    }\n\n  private:\n    vector<int> tree;\n\n    /* Depth-first traversal */\n    void dfs(int i, string order, vector<int> &res) {\n        // If empty position, return\n        if (val(i) == INT_MAX)\n            return;\n        // Preorder traversal\n        if (order == \"pre\")\n            res.push_back(val(i));\n        dfs(left(i), order, res);\n        // Inorder traversal\n        if (order == \"in\")\n            res.push_back(val(i));\n        dfs(right(i), order, res);\n        // Postorder traversal\n        if (order == \"post\")\n            res.push_back(val(i));\n    }\n};\n
      array_binary_tree.java
      /* Binary tree class represented by array */\nclass ArrayBinaryTree {\n    private List<Integer> tree;\n\n    /* Constructor */\n    public ArrayBinaryTree(List<Integer> arr) {\n        tree = new ArrayList<>(arr);\n    }\n\n    /* List capacity */\n    public int size() {\n        return tree.size();\n    }\n\n    /* Get value of node at index i */\n    public Integer val(int i) {\n        // If index out of bounds, return null to represent empty position\n        if (i < 0 || i >= size())\n            return null;\n        return tree.get(i);\n    }\n\n    /* Get index of left child node of node at index i */\n    public Integer left(int i) {\n        return 2 * i + 1;\n    }\n\n    /* Get index of right child node of node at index i */\n    public Integer right(int i) {\n        return 2 * i + 2;\n    }\n\n    /* Get index of parent node of node at index i */\n    public Integer parent(int i) {\n        return (i - 1) / 2;\n    }\n\n    /* Level-order traversal */\n    public List<Integer> levelOrder() {\n        List<Integer> res = new ArrayList<>();\n        // Traverse array directly\n        for (int i = 0; i < size(); i++) {\n            if (val(i) != null)\n                res.add(val(i));\n        }\n        return res;\n    }\n\n    /* Depth-first traversal */\n    private void dfs(Integer i, String order, List<Integer> res) {\n        // If empty position, return\n        if (val(i) == null)\n            return;\n        // Preorder traversal\n        if (\"pre\".equals(order))\n            res.add(val(i));\n        dfs(left(i), order, res);\n        // Inorder traversal\n        if (\"in\".equals(order))\n            res.add(val(i));\n        dfs(right(i), order, res);\n        // Postorder traversal\n        if (\"post\".equals(order))\n            res.add(val(i));\n    }\n\n    /* Preorder traversal */\n    public List<Integer> preOrder() {\n        List<Integer> res = new ArrayList<>();\n        dfs(0, \"pre\", res);\n        return res;\n    }\n\n    /* Inorder traversal */\n    public List<Integer> inOrder() {\n        List<Integer> res = new ArrayList<>();\n        dfs(0, \"in\", res);\n        return res;\n    }\n\n    /* Postorder traversal */\n    public List<Integer> postOrder() {\n        List<Integer> res = new ArrayList<>();\n        dfs(0, \"post\", res);\n        return res;\n    }\n}\n
      array_binary_tree.cs
      /* Binary tree class represented by array */\nclass ArrayBinaryTree(List<int?> arr) {\n    List<int?> tree = new(arr);\n\n    /* List capacity */\n    public int Size() {\n        return tree.Count;\n    }\n\n    /* Get value of node at index i */\n    public int? Val(int i) {\n        // If index out of bounds, return null to represent empty position\n        if (i < 0 || i >= Size())\n            return null;\n        return tree[i];\n    }\n\n    /* Get index of left child node of node at index i */\n    public int Left(int i) {\n        return 2 * i + 1;\n    }\n\n    /* Get index of right child node of node at index i */\n    public int Right(int i) {\n        return 2 * i + 2;\n    }\n\n    /* Get index of parent node of node at index i */\n    public int Parent(int i) {\n        return (i - 1) / 2;\n    }\n\n    /* Level-order traversal */\n    public List<int> LevelOrder() {\n        List<int> res = [];\n        // Traverse array directly\n        for (int i = 0; i < Size(); i++) {\n            if (Val(i).HasValue)\n                res.Add(Val(i)!.Value);\n        }\n        return res;\n    }\n\n    /* Depth-first traversal */\n    void DFS(int i, string order, List<int> res) {\n        // If empty position, return\n        if (!Val(i).HasValue)\n            return;\n        // Preorder traversal\n        if (order == \"pre\")\n            res.Add(Val(i)!.Value);\n        DFS(Left(i), order, res);\n        // Inorder traversal\n        if (order == \"in\")\n            res.Add(Val(i)!.Value);\n        DFS(Right(i), order, res);\n        // Postorder traversal\n        if (order == \"post\")\n            res.Add(Val(i)!.Value);\n    }\n\n    /* Preorder traversal */\n    public List<int> PreOrder() {\n        List<int> res = [];\n        DFS(0, \"pre\", res);\n        return res;\n    }\n\n    /* Inorder traversal */\n    public List<int> InOrder() {\n        List<int> res = [];\n        DFS(0, \"in\", res);\n        return res;\n    }\n\n    /* Postorder traversal */\n    public List<int> PostOrder() {\n        List<int> res = [];\n        DFS(0, \"post\", res);\n        return res;\n    }\n}\n
      array_binary_tree.go
      /* Binary tree class represented by array */\ntype arrayBinaryTree struct {\n    tree []any\n}\n\n/* Constructor */\nfunc newArrayBinaryTree(arr []any) *arrayBinaryTree {\n    return &arrayBinaryTree{\n        tree: arr,\n    }\n}\n\n/* List capacity */\nfunc (abt *arrayBinaryTree) size() int {\n    return len(abt.tree)\n}\n\n/* Get value of node at index i */\nfunc (abt *arrayBinaryTree) val(i int) any {\n    // If index out of bounds, return null to represent empty position\n    if i < 0 || i >= abt.size() {\n        return nil\n    }\n    return abt.tree[i]\n}\n\n/* Get index of left child node of node at index i */\nfunc (abt *arrayBinaryTree) left(i int) int {\n    return 2*i + 1\n}\n\n/* Get index of right child node of node at index i */\nfunc (abt *arrayBinaryTree) right(i int) int {\n    return 2*i + 2\n}\n\n/* Get index of parent node of node at index i */\nfunc (abt *arrayBinaryTree) parent(i int) int {\n    return (i - 1) / 2\n}\n\n/* Level-order traversal */\nfunc (abt *arrayBinaryTree) levelOrder() []any {\n    var res []any\n    // Traverse array directly\n    for i := 0; i < abt.size(); i++ {\n        if abt.val(i) != nil {\n            res = append(res, abt.val(i))\n        }\n    }\n    return res\n}\n\n/* Depth-first traversal */\nfunc (abt *arrayBinaryTree) dfs(i int, order string, res *[]any) {\n    // If empty position, return\n    if abt.val(i) == nil {\n        return\n    }\n    // Preorder traversal\n    if order == \"pre\" {\n        *res = append(*res, abt.val(i))\n    }\n    abt.dfs(abt.left(i), order, res)\n    // Inorder traversal\n    if order == \"in\" {\n        *res = append(*res, abt.val(i))\n    }\n    abt.dfs(abt.right(i), order, res)\n    // Postorder traversal\n    if order == \"post\" {\n        *res = append(*res, abt.val(i))\n    }\n}\n\n/* Preorder traversal */\nfunc (abt *arrayBinaryTree) preOrder() []any {\n    var res []any\n    abt.dfs(0, \"pre\", &res)\n    return res\n}\n\n/* Inorder traversal */\nfunc (abt *arrayBinaryTree) inOrder() []any {\n    var res []any\n    abt.dfs(0, \"in\", &res)\n    return res\n}\n\n/* Postorder traversal */\nfunc (abt *arrayBinaryTree) postOrder() []any {\n    var res []any\n    abt.dfs(0, \"post\", &res)\n    return res\n}\n
      array_binary_tree.swift
      /* Binary tree class represented by array */\nclass ArrayBinaryTree {\n    private var tree: [Int?]\n\n    /* Constructor */\n    init(arr: [Int?]) {\n        tree = arr\n    }\n\n    /* List capacity */\n    func size() -> Int {\n        tree.count\n    }\n\n    /* Get value of node at index i */\n    func val(i: Int) -> Int? {\n        // If index out of bounds, return null to represent empty position\n        if i < 0 || i >= size() {\n            return nil\n        }\n        return tree[i]\n    }\n\n    /* Get index of left child node of node at index i */\n    func left(i: Int) -> Int {\n        2 * i + 1\n    }\n\n    /* Get index of right child node of node at index i */\n    func right(i: Int) -> Int {\n        2 * i + 2\n    }\n\n    /* Get index of parent node of node at index i */\n    func parent(i: Int) -> Int {\n        (i - 1) / 2\n    }\n\n    /* Level-order traversal */\n    func levelOrder() -> [Int] {\n        var res: [Int] = []\n        // Traverse array directly\n        for i in 0 ..< size() {\n            if let val = val(i: i) {\n                res.append(val)\n            }\n        }\n        return res\n    }\n\n    /* Depth-first traversal */\n    private func dfs(i: Int, order: String, res: inout [Int]) {\n        // If empty position, return\n        guard let val = val(i: i) else {\n            return\n        }\n        // Preorder traversal\n        if order == \"pre\" {\n            res.append(val)\n        }\n        dfs(i: left(i: i), order: order, res: &res)\n        // Inorder traversal\n        if order == \"in\" {\n            res.append(val)\n        }\n        dfs(i: right(i: i), order: order, res: &res)\n        // Postorder traversal\n        if order == \"post\" {\n            res.append(val)\n        }\n    }\n\n    /* Preorder traversal */\n    func preOrder() -> [Int] {\n        var res: [Int] = []\n        dfs(i: 0, order: \"pre\", res: &res)\n        return res\n    }\n\n    /* Inorder traversal */\n    func inOrder() -> [Int] {\n        var res: [Int] = []\n        dfs(i: 0, order: \"in\", res: &res)\n        return res\n    }\n\n    /* Postorder traversal */\n    func postOrder() -> [Int] {\n        var res: [Int] = []\n        dfs(i: 0, order: \"post\", res: &res)\n        return res\n    }\n}\n
      array_binary_tree.js
      /* Binary tree class represented by array */\nclass ArrayBinaryTree {\n    #tree;\n\n    /* Constructor */\n    constructor(arr) {\n        this.#tree = arr;\n    }\n\n    /* List capacity */\n    size() {\n        return this.#tree.length;\n    }\n\n    /* Get value of node at index i */\n    val(i) {\n        // If index out of bounds, return null to represent empty position\n        if (i < 0 || i >= this.size()) return null;\n        return this.#tree[i];\n    }\n\n    /* Get index of left child node of node at index i */\n    left(i) {\n        return 2 * i + 1;\n    }\n\n    /* Get index of right child node of node at index i */\n    right(i) {\n        return 2 * i + 2;\n    }\n\n    /* Get index of parent node of node at index i */\n    parent(i) {\n        return Math.floor((i - 1) / 2); // Floor division\n    }\n\n    /* Level-order traversal */\n    levelOrder() {\n        let res = [];\n        // Traverse array directly\n        for (let i = 0; i < this.size(); i++) {\n            if (this.val(i) !== null) res.push(this.val(i));\n        }\n        return res;\n    }\n\n    /* Depth-first traversal */\n    #dfs(i, order, res) {\n        // If empty position, return\n        if (this.val(i) === null) return;\n        // Preorder traversal\n        if (order === 'pre') res.push(this.val(i));\n        this.#dfs(this.left(i), order, res);\n        // Inorder traversal\n        if (order === 'in') res.push(this.val(i));\n        this.#dfs(this.right(i), order, res);\n        // Postorder traversal\n        if (order === 'post') res.push(this.val(i));\n    }\n\n    /* Preorder traversal */\n    preOrder() {\n        const res = [];\n        this.#dfs(0, 'pre', res);\n        return res;\n    }\n\n    /* Inorder traversal */\n    inOrder() {\n        const res = [];\n        this.#dfs(0, 'in', res);\n        return res;\n    }\n\n    /* Postorder traversal */\n    postOrder() {\n        const res = [];\n        this.#dfs(0, 'post', res);\n        return res;\n    }\n}\n
      array_binary_tree.ts
      /* Binary tree class represented by array */\nclass ArrayBinaryTree {\n    #tree: (number | null)[];\n\n    /* Constructor */\n    constructor(arr: (number | null)[]) {\n        this.#tree = arr;\n    }\n\n    /* List capacity */\n    size(): number {\n        return this.#tree.length;\n    }\n\n    /* Get value of node at index i */\n    val(i: number): number | null {\n        // If index out of bounds, return null to represent empty position\n        if (i < 0 || i >= this.size()) return null;\n        return this.#tree[i];\n    }\n\n    /* Get index of left child node of node at index i */\n    left(i: number): number {\n        return 2 * i + 1;\n    }\n\n    /* Get index of right child node of node at index i */\n    right(i: number): number {\n        return 2 * i + 2;\n    }\n\n    /* Get index of parent node of node at index i */\n    parent(i: number): number {\n        return Math.floor((i - 1) / 2); // Floor division\n    }\n\n    /* Level-order traversal */\n    levelOrder(): number[] {\n        let res = [];\n        // Traverse array directly\n        for (let i = 0; i < this.size(); i++) {\n            if (this.val(i) !== null) res.push(this.val(i));\n        }\n        return res;\n    }\n\n    /* Depth-first traversal */\n    #dfs(i: number, order: Order, res: (number | null)[]): void {\n        // If empty position, return\n        if (this.val(i) === null) return;\n        // Preorder traversal\n        if (order === 'pre') res.push(this.val(i));\n        this.#dfs(this.left(i), order, res);\n        // Inorder traversal\n        if (order === 'in') res.push(this.val(i));\n        this.#dfs(this.right(i), order, res);\n        // Postorder traversal\n        if (order === 'post') res.push(this.val(i));\n    }\n\n    /* Preorder traversal */\n    preOrder(): (number | null)[] {\n        const res = [];\n        this.#dfs(0, 'pre', res);\n        return res;\n    }\n\n    /* Inorder traversal */\n    inOrder(): (number | null)[] {\n        const res = [];\n        this.#dfs(0, 'in', res);\n        return res;\n    }\n\n    /* Postorder traversal */\n    postOrder(): (number | null)[] {\n        const res = [];\n        this.#dfs(0, 'post', res);\n        return res;\n    }\n}\n
      array_binary_tree.dart
      /* Binary tree class represented by array */\nclass ArrayBinaryTree {\n  late List<int?> _tree;\n\n  /* Constructor */\n  ArrayBinaryTree(this._tree);\n\n  /* List capacity */\n  int size() {\n    return _tree.length;\n  }\n\n  /* Get value of node at index i */\n  int? val(int i) {\n    // If index out of bounds, return null to represent empty position\n    if (i < 0 || i >= size()) {\n      return null;\n    }\n    return _tree[i];\n  }\n\n  /* Get index of left child node of node at index i */\n  int? left(int i) {\n    return 2 * i + 1;\n  }\n\n  /* Get index of right child node of node at index i */\n  int? right(int i) {\n    return 2 * i + 2;\n  }\n\n  /* Get index of parent node of node at index i */\n  int? parent(int i) {\n    return (i - 1) ~/ 2;\n  }\n\n  /* Level-order traversal */\n  List<int> levelOrder() {\n    List<int> res = [];\n    for (int i = 0; i < size(); i++) {\n      if (val(i) != null) {\n        res.add(val(i)!);\n      }\n    }\n    return res;\n  }\n\n  /* Depth-first traversal */\n  void dfs(int i, String order, List<int?> res) {\n    // If empty position, return\n    if (val(i) == null) {\n      return;\n    }\n    // Preorder traversal\n    if (order == 'pre') {\n      res.add(val(i));\n    }\n    dfs(left(i)!, order, res);\n    // Inorder traversal\n    if (order == 'in') {\n      res.add(val(i));\n    }\n    dfs(right(i)!, order, res);\n    // Postorder traversal\n    if (order == 'post') {\n      res.add(val(i));\n    }\n  }\n\n  /* Preorder traversal */\n  List<int?> preOrder() {\n    List<int?> res = [];\n    dfs(0, 'pre', res);\n    return res;\n  }\n\n  /* Inorder traversal */\n  List<int?> inOrder() {\n    List<int?> res = [];\n    dfs(0, 'in', res);\n    return res;\n  }\n\n  /* Postorder traversal */\n  List<int?> postOrder() {\n    List<int?> res = [];\n    dfs(0, 'post', res);\n    return res;\n  }\n}\n
      array_binary_tree.rs
      /* Binary tree class represented by array */\nstruct ArrayBinaryTree {\n    tree: Vec<Option<i32>>,\n}\n\nimpl ArrayBinaryTree {\n    /* Constructor */\n    fn new(arr: Vec<Option<i32>>) -> Self {\n        Self { tree: arr }\n    }\n\n    /* List capacity */\n    fn size(&self) -> i32 {\n        self.tree.len() as i32\n    }\n\n    /* Get value of node at index i */\n    fn val(&self, i: i32) -> Option<i32> {\n        // If index is out of bounds, return None, representing empty position\n        if i < 0 || i >= self.size() {\n            None\n        } else {\n            self.tree[i as usize]\n        }\n    }\n\n    /* Get index of left child node of node at index i */\n    fn left(&self, i: i32) -> i32 {\n        2 * i + 1\n    }\n\n    /* Get index of right child node of node at index i */\n    fn right(&self, i: i32) -> i32 {\n        2 * i + 2\n    }\n\n    /* Get index of parent node of node at index i */\n    fn parent(&self, i: i32) -> i32 {\n        (i - 1) / 2\n    }\n\n    /* Level-order traversal */\n    fn level_order(&self) -> Vec<i32> {\n        self.tree.iter().filter_map(|&x| x).collect()\n    }\n\n    /* Depth-first traversal */\n    fn dfs(&self, i: i32, order: &'static str, res: &mut Vec<i32>) {\n        if self.val(i).is_none() {\n            return;\n        }\n        let val = self.val(i).unwrap();\n        // Preorder traversal\n        if order == \"pre\" {\n            res.push(val);\n        }\n        self.dfs(self.left(i), order, res);\n        // Inorder traversal\n        if order == \"in\" {\n            res.push(val);\n        }\n        self.dfs(self.right(i), order, res);\n        // Postorder traversal\n        if order == \"post\" {\n            res.push(val);\n        }\n    }\n\n    /* Preorder traversal */\n    fn pre_order(&self) -> Vec<i32> {\n        let mut res = vec![];\n        self.dfs(0, \"pre\", &mut res);\n        res\n    }\n\n    /* Inorder traversal */\n    fn in_order(&self) -> Vec<i32> {\n        let mut res = vec![];\n        self.dfs(0, \"in\", &mut res);\n        res\n    }\n\n    /* Postorder traversal */\n    fn post_order(&self) -> Vec<i32> {\n        let mut res = vec![];\n        self.dfs(0, \"post\", &mut res);\n        res\n    }\n}\n
      array_binary_tree.c
      /* Binary tree structure in array representation */\ntypedef struct {\n    int *tree;\n    int size;\n} ArrayBinaryTree;\n\n/* Constructor */\nArrayBinaryTree *newArrayBinaryTree(int *arr, int arrSize) {\n    ArrayBinaryTree *abt = (ArrayBinaryTree *)malloc(sizeof(ArrayBinaryTree));\n    abt->tree = malloc(sizeof(int) * arrSize);\n    memcpy(abt->tree, arr, sizeof(int) * arrSize);\n    abt->size = arrSize;\n    return abt;\n}\n\n/* Destructor */\nvoid delArrayBinaryTree(ArrayBinaryTree *abt) {\n    free(abt->tree);\n    free(abt);\n}\n\n/* List capacity */\nint size(ArrayBinaryTree *abt) {\n    return abt->size;\n}\n\n/* Get value of node at index i */\nint val(ArrayBinaryTree *abt, int i) {\n    // Return INT_MAX if index out of bounds, representing empty position\n    if (i < 0 || i >= size(abt))\n        return INT_MAX;\n    return abt->tree[i];\n}\n\n/* Level-order traversal */\nint *levelOrder(ArrayBinaryTree *abt, int *returnSize) {\n    int *res = (int *)malloc(sizeof(int) * size(abt));\n    int index = 0;\n    // Traverse array directly\n    for (int i = 0; i < size(abt); i++) {\n        if (val(abt, i) != INT_MAX)\n            res[index++] = val(abt, i);\n    }\n    *returnSize = index;\n    return res;\n}\n\n/* Depth-first traversal */\nvoid dfs(ArrayBinaryTree *abt, int i, char *order, int *res, int *index) {\n    // If empty position, return\n    if (val(abt, i) == INT_MAX)\n        return;\n    // Preorder traversal\n    if (strcmp(order, \"pre\") == 0)\n        res[(*index)++] = val(abt, i);\n    dfs(abt, left(i), order, res, index);\n    // Inorder traversal\n    if (strcmp(order, \"in\") == 0)\n        res[(*index)++] = val(abt, i);\n    dfs(abt, right(i), order, res, index);\n    // Postorder traversal\n    if (strcmp(order, \"post\") == 0)\n        res[(*index)++] = val(abt, i);\n}\n\n/* Preorder traversal */\nint *preOrder(ArrayBinaryTree *abt, int *returnSize) {\n    int *res = (int *)malloc(sizeof(int) * size(abt));\n    int index = 0;\n    dfs(abt, 0, \"pre\", res, &index);\n    *returnSize = index;\n    return res;\n}\n\n/* Inorder traversal */\nint *inOrder(ArrayBinaryTree *abt, int *returnSize) {\n    int *res = (int *)malloc(sizeof(int) * size(abt));\n    int index = 0;\n    dfs(abt, 0, \"in\", res, &index);\n    *returnSize = index;\n    return res;\n}\n\n/* Postorder traversal */\nint *postOrder(ArrayBinaryTree *abt, int *returnSize) {\n    int *res = (int *)malloc(sizeof(int) * size(abt));\n    int index = 0;\n    dfs(abt, 0, \"post\", res, &index);\n    *returnSize = index;\n    return res;\n}\n
      array_binary_tree.kt
      /* Binary tree class represented by array */\nclass ArrayBinaryTree(val tree: MutableList<Int?>) {\n    /* List capacity */\n    fun size(): Int {\n        return tree.size\n    }\n\n    /* Get value of node at index i */\n    fun _val(i: Int): Int? {\n        // If index out of bounds, return null to represent empty position\n        if (i < 0 || i >= size()) return null\n        return tree[i]\n    }\n\n    /* Get index of left child node of node at index i */\n    fun left(i: Int): Int {\n        return 2 * i + 1\n    }\n\n    /* Get index of right child node of node at index i */\n    fun right(i: Int): Int {\n        return 2 * i + 2\n    }\n\n    /* Get index of parent node of node at index i */\n    fun parent(i: Int): Int {\n        return (i - 1) / 2\n    }\n\n    /* Level-order traversal */\n    fun levelOrder(): MutableList<Int?> {\n        val res = mutableListOf<Int?>()\n        // Traverse array directly\n        for (i in 0..<size()) {\n            if (_val(i) != null)\n                res.add(_val(i))\n        }\n        return res\n    }\n\n    /* Depth-first traversal */\n    fun dfs(i: Int, order: String, res: MutableList<Int?>) {\n        // If empty position, return\n        if (_val(i) == null)\n            return\n        // Preorder traversal\n        if (\"pre\" == order)\n            res.add(_val(i))\n        dfs(left(i), order, res)\n        // Inorder traversal\n        if (\"in\" == order)\n            res.add(_val(i))\n        dfs(right(i), order, res)\n        // Postorder traversal\n        if (\"post\" == order)\n            res.add(_val(i))\n    }\n\n    /* Preorder traversal */\n    fun preOrder(): MutableList<Int?> {\n        val res = mutableListOf<Int?>()\n        dfs(0, \"pre\", res)\n        return res\n    }\n\n    /* Inorder traversal */\n    fun inOrder(): MutableList<Int?> {\n        val res = mutableListOf<Int?>()\n        dfs(0, \"in\", res)\n        return res\n    }\n\n    /* Postorder traversal */\n    fun postOrder(): MutableList<Int?> {\n        val res = mutableListOf<Int?>()\n        dfs(0, \"post\", res)\n        return res\n    }\n}\n
      array_binary_tree.rb
      ### Array representation of binary tree class ###\nclass ArrayBinaryTree\n  ### Constructor ###\n  def initialize(arr)\n    @tree = arr.to_a\n  end\n\n  ### List capacity ###\n  def size\n    @tree.length\n  end\n\n  ### Get value of node at index i ###\n  def val(i)\n    # Return nil if index out of bounds, representing empty position\n    return if i < 0 || i >= size\n\n    @tree[i]\n  end\n\n  ### Get left child index of node at index i ###\n  def left(i)\n    2 * i + 1\n  end\n\n  ### Get right child index of node at index i ###\n  def right(i)\n    2 * i + 2\n  end\n\n  ### Get parent node index of node at index i ###\n  def parent(i)\n    (i - 1) / 2\n  end\n\n  ### Level-order traversal ###\n  def level_order\n    @res = []\n\n    # Traverse array directly\n    for i in 0...size\n      @res << val(i) unless val(i).nil?\n    end\n\n    @res\n  end\n\n  ### Depth-first traversal ###\n  def dfs(i, order)\n    return if val(i).nil?\n    # Preorder traversal\n    @res << val(i) if order == :pre\n    dfs(left(i), order)\n    # Inorder traversal\n    @res << val(i) if order == :in\n    dfs(right(i), order)\n    # Postorder traversal\n    @res << val(i) if order == :post\n  end\n\n  ### Pre-order traversal ###\n  def pre_order\n    @res = []\n    dfs(0, :pre)\n    @res\n  end\n\n  ### In-order traversal ###\n  def in_order\n    @res = []\n    dfs(0, :in)\n    @res\n  end\n\n  ### Post-order traversal ###\n  def post_order\n    @res = []\n    dfs(0, :post)\n    @res\n  end\nend\n
      "},{"location":"chapter_tree/array_representation_of_tree/#733-advantages-and-limitations","title":"7.3.3 \u00a0 Advantages and Limitations","text":"

      The array representation of binary trees has the following advantages:

      • Arrays are stored in contiguous memory space, which is cache-friendly, allowing faster access and traversal.
      • It does not require storing pointers, which saves space.
      • It allows random access to nodes.

      However, the array representation also has some limitations:

      • Array storage requires contiguous memory space, so it is not suitable for storing trees with a large amount of data.
      • Adding or removing nodes requires array insertion and deletion operations, which have lower efficiency.
      • When there are many None values in the binary tree, the proportion of node data contained in the array is low, leading to lower space utilization.
      "},{"location":"chapter_tree/avl_tree/","title":"7.5 \u00a0 Avl Tree *","text":"

      In the \"Binary Search Tree\" section, we mentioned that after multiple insertion and removal operations, a binary search tree may degenerate into a linked list. In this case, the time complexity of all operations degrades from \\(O(\\log n)\\) to \\(O(n)\\).

      As shown in Figure 7-24, after two node removal operations, this binary search tree will degrade into a linked list.

      Figure 7-24 \u00a0 Degradation of an AVL tree after removing nodes

      For example, in the perfect binary tree shown in Figure 7-25, after inserting two nodes, the tree will lean heavily to the left, and the time complexity of search operations will also degrade.

      Figure 7-25 \u00a0 Degradation of an AVL tree after inserting nodes

      In 1962, G. M. Adelson-Velsky and E. M. Landis proposed the AVL tree in their paper \"An algorithm for the organization of information\". The paper described in detail a series of operations ensuring that after continuously adding and removing nodes, the AVL tree does not degenerate, thus keeping the time complexity of various operations at the \\(O(\\log n)\\) level. In other words, in scenarios requiring frequent insertions, deletions, searches, and modifications, the AVL tree can always maintain efficient data operation performance, making it very valuable in applications.

      "},{"location":"chapter_tree/avl_tree/#751-common-terminology-in-avl-trees","title":"7.5.1 \u00a0 Common Terminology in Avl Trees","text":"

      An AVL tree is both a binary search tree and a balanced binary tree, simultaneously satisfying all the properties of these two types of binary trees, hence it is a balanced binary search tree.

      "},{"location":"chapter_tree/avl_tree/#1-node-height","title":"1. \u00a0 Node Height","text":"

      Since the operations related to AVL trees require obtaining node heights, we need to add a height variable to the node class:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      class TreeNode:\n    \"\"\"AVL tree node\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val                 # Node value\n        self.height: int = 0                # Node height\n        self.left: TreeNode | None = None   # Left child reference\n        self.right: TreeNode | None = None  # Right child reference\n
      /* AVL tree node */\nstruct TreeNode {\n    int val{};          // Node value\n    int height = 0;     // Node height\n    TreeNode *left{};   // Left child\n    TreeNode *right{};  // Right child\n    TreeNode() = default;\n    explicit TreeNode(int x) : val(x){}\n};\n
      /* AVL tree node */\nclass TreeNode {\n    public int val;        // Node value\n    public int height;     // Node height\n    public TreeNode left;  // Left child\n    public TreeNode right; // Right child\n    public TreeNode(int x) { val = x; }\n}\n
      /* AVL tree node */\nclass TreeNode(int? x) {\n    public int? val = x;    // Node value\n    public int height;      // Node height\n    public TreeNode? left;  // Left child reference\n    public TreeNode? right; // Right child reference\n}\n
      /* AVL tree node */\ntype TreeNode struct {\n    Val    int       // Node value\n    Height int       // Node height\n    Left   *TreeNode // Left child reference\n    Right  *TreeNode // Right child reference\n}\n
      /* AVL tree node */\nclass TreeNode {\n    var val: Int // Node value\n    var height: Int // Node height\n    var left: TreeNode? // Left child\n    var right: TreeNode? // Right child\n\n    init(x: Int) {\n        val = x\n        height = 0\n    }\n}\n
      /* AVL tree node */\nclass TreeNode {\n    val; // Node value\n    height; // Node height\n    left; // Left child pointer\n    right; // Right child pointer\n    constructor(val, left, right, height) {\n        this.val = val === undefined ? 0 : val;\n        this.height = height === undefined ? 0 : height;\n        this.left = left === undefined ? null : left;\n        this.right = right === undefined ? null : right;\n    }\n}\n
      /* AVL tree node */\nclass TreeNode {\n    val: number;            // Node value\n    height: number;         // Node height\n    left: TreeNode | null;  // Left child pointer\n    right: TreeNode | null; // Right child pointer\n    constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {\n        this.val = val === undefined ? 0 : val;\n        this.height = height === undefined ? 0 : height; \n        this.left = left === undefined ? null : left; \n        this.right = right === undefined ? null : right; \n    }\n}\n
      /* AVL tree node */\nclass TreeNode {\n  int val;         // Node value\n  int height;      // Node height\n  TreeNode? left;  // Left child\n  TreeNode? right; // Right child\n  TreeNode(this.val, [this.height = 0, this.left, this.right]);\n}\n
      use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* AVL tree node */\nstruct TreeNode {\n    val: i32,                               // Node value\n    height: i32,                            // Node height\n    left: Option<Rc<RefCell<TreeNode>>>,    // Left child\n    right: Option<Rc<RefCell<TreeNode>>>,   // Right child\n}\n\nimpl TreeNode {\n    /* Constructor */\n    fn new(val: i32) -> Rc<RefCell<Self>> {\n        Rc::new(RefCell::new(Self {\n            val,\n            height: 0,\n            left: None,\n            right: None\n        }))\n    }\n}\n
      /* AVL tree node */\ntypedef struct TreeNode {\n    int val;\n    int height;\n    struct TreeNode *left;\n    struct TreeNode *right;\n} TreeNode;\n\n/* Constructor */\nTreeNode *newTreeNode(int val) {\n    TreeNode *node;\n\n    node = (TreeNode *)malloc(sizeof(TreeNode));\n    node->val = val;\n    node->height = 0;\n    node->left = NULL;\n    node->right = NULL;\n    return node;\n}\n
      /* AVL tree node */\nclass TreeNode(val _val: Int) {  // Node value\n    val height: Int = 0          // Node height\n    val left: TreeNode? = null   // Left child\n    val right: TreeNode? = null  // Right child\n}\n
      ### AVL tree node class ###\nclass TreeNode\n  attr_accessor :val    # Node value\n  attr_accessor :height # Node height\n  attr_accessor :left   # Left child reference\n  attr_accessor :right  # Right child reference\n\n  def initialize(val)\n    @val = val\n    @height = 0\n  end\nend\n

      The \"node height\" refers to the distance from that node to its farthest leaf node, i.e., the number of \"edges\" passed. It is important to note that the height of a leaf node is \\(0\\), and the height of a null node is \\(-1\\). We will create two utility functions for getting and updating the height of a node:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
      def height(self, node: TreeNode | None) -> int:\n    \"\"\"Get node height\"\"\"\n    # Empty node height is -1, leaf node height is 0\n    if node is not None:\n        return node.height\n    return -1\n\ndef update_height(self, node: TreeNode | None):\n    \"\"\"Update node height\"\"\"\n    # Node height equals the height of the tallest subtree + 1\n    node.height = max([self.height(node.left), self.height(node.right)]) + 1\n
      avl_tree.cpp
      /* Get node height */\nint height(TreeNode *node) {\n    // Empty node height is -1, leaf node height is 0\n    return node == nullptr ? -1 : node->height;\n}\n\n/* Update node height */\nvoid updateHeight(TreeNode *node) {\n    // Node height equals the height of the tallest subtree + 1\n    node->height = max(height(node->left), height(node->right)) + 1;\n}\n
      avl_tree.java
      /* Get node height */\nint height(TreeNode node) {\n    // Empty node height is -1, leaf node height is 0\n    return node == null ? -1 : node.height;\n}\n\n/* Update node height */\nvoid updateHeight(TreeNode node) {\n    // Node height equals the height of the tallest subtree + 1\n    node.height = Math.max(height(node.left), height(node.right)) + 1;\n}\n
      avl_tree.cs
      /* Get node height */\nint Height(TreeNode? node) {\n    // Empty node height is -1, leaf node height is 0\n    return node == null ? -1 : node.height;\n}\n\n/* Update node height */\nvoid UpdateHeight(TreeNode node) {\n    // Node height equals the height of the tallest subtree + 1\n    node.height = Math.Max(Height(node.left), Height(node.right)) + 1;\n}\n
      avl_tree.go
      /* Get node height */\nfunc (t *aVLTree) height(node *TreeNode) int {\n    // Empty node height is -1, leaf node height is 0\n    if node != nil {\n        return node.Height\n    }\n    return -1\n}\n\n/* Update node height */\nfunc (t *aVLTree) updateHeight(node *TreeNode) {\n    lh := t.height(node.Left)\n    rh := t.height(node.Right)\n    // Node height equals the height of the tallest subtree + 1\n    if lh > rh {\n        node.Height = lh + 1\n    } else {\n        node.Height = rh + 1\n    }\n}\n
      avl_tree.swift
      /* Get node height */\nfunc height(node: TreeNode?) -> Int {\n    // Empty node height is -1, leaf node height is 0\n    node?.height ?? -1\n}\n\n/* Update node height */\nfunc updateHeight(node: TreeNode?) {\n    // Node height equals the height of the tallest subtree + 1\n    node?.height = max(height(node: node?.left), height(node: node?.right)) + 1\n}\n
      avl_tree.js
      /* Get node height */\nheight(node) {\n    // Empty node height is -1, leaf node height is 0\n    return node === null ? -1 : node.height;\n}\n\n/* Update node height */\n#updateHeight(node) {\n    // Node height equals the height of the tallest subtree + 1\n    node.height =\n        Math.max(this.height(node.left), this.height(node.right)) + 1;\n}\n
      avl_tree.ts
      /* Get node height */\nheight(node: TreeNode): number {\n    // Empty node height is -1, leaf node height is 0\n    return node === null ? -1 : node.height;\n}\n\n/* Update node height */\nupdateHeight(node: TreeNode): void {\n    // Node height equals the height of the tallest subtree + 1\n    node.height =\n        Math.max(this.height(node.left), this.height(node.right)) + 1;\n}\n
      avl_tree.dart
      /* Get node height */\nint height(TreeNode? node) {\n  // Empty node height is -1, leaf node height is 0\n  return node == null ? -1 : node.height;\n}\n\n/* Update node height */\nvoid updateHeight(TreeNode? node) {\n  // Node height equals the height of the tallest subtree + 1\n  node!.height = max(height(node.left), height(node.right)) + 1;\n}\n
      avl_tree.rs
      /* Get node height */\nfn height(node: OptionTreeNodeRc) -> i32 {\n    // Empty node height is -1, leaf node height is 0\n    match node {\n        Some(node) => node.borrow().height,\n        None => -1,\n    }\n}\n\n/* Update node height */\nfn update_height(node: OptionTreeNodeRc) {\n    if let Some(node) = node {\n        let left = node.borrow().left.clone();\n        let right = node.borrow().right.clone();\n        // Node height equals the height of the tallest subtree + 1\n        node.borrow_mut().height = std::cmp::max(Self::height(left), Self::height(right)) + 1;\n    }\n}\n
      avl_tree.c
      /* Get node height */\nint height(TreeNode *node) {\n    // Empty node height is -1, leaf node height is 0\n    if (node != NULL) {\n        return node->height;\n    }\n    return -1;\n}\n\n/* Update node height */\nvoid updateHeight(TreeNode *node) {\n    int lh = height(node->left);\n    int rh = height(node->right);\n    // Node height equals the height of the tallest subtree + 1\n    if (lh > rh) {\n        node->height = lh + 1;\n    } else {\n        node->height = rh + 1;\n    }\n}\n
      avl_tree.kt
      /* Get node height */\nfun height(node: TreeNode?): Int {\n    // Empty node height is -1, leaf node height is 0\n    return node?.height ?: -1\n}\n\n/* Update node height */\nfun updateHeight(node: TreeNode?) {\n    // Node height equals the height of the tallest subtree + 1\n    node?.height = max(height(node?.left), height(node?.right)) + 1\n}\n
      avl_tree.rb
      ### Get node height ###\ndef height(node)\n  # Empty node height is -1, leaf node height is 0\n  return node.height unless node.nil?\n\n  -1\nend\n\n### Update node height ###\ndef update_height(node)\n  # Node height equals the height of the tallest subtree + 1\n  node.height = [height(node.left), height(node.right)].max + 1\nend\n
      "},{"location":"chapter_tree/avl_tree/#2-node-balance-factor","title":"2. \u00a0 Node Balance Factor","text":"

      The balance factor of a node is defined as the height of the node's left subtree minus the height of its right subtree, and the balance factor of a null node is defined as \\(0\\). We also encapsulate the function to obtain the node's balance factor for convenient subsequent use:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
      def balance_factor(self, node: TreeNode | None) -> int:\n    \"\"\"Get balance factor\"\"\"\n    # Empty node balance factor is 0\n    if node is None:\n        return 0\n    # Node balance factor = left subtree height - right subtree height\n    return self.height(node.left) - self.height(node.right)\n
      avl_tree.cpp
      /* Get balance factor */\nint balanceFactor(TreeNode *node) {\n    // Empty node balance factor is 0\n    if (node == nullptr)\n        return 0;\n    // Node balance factor = left subtree height - right subtree height\n    return height(node->left) - height(node->right);\n}\n
      avl_tree.java
      /* Get balance factor */\nint balanceFactor(TreeNode node) {\n    // Empty node balance factor is 0\n    if (node == null)\n        return 0;\n    // Node balance factor = left subtree height - right subtree height\n    return height(node.left) - height(node.right);\n}\n
      avl_tree.cs
      /* Get balance factor */\nint BalanceFactor(TreeNode? node) {\n    // Empty node balance factor is 0\n    if (node == null) return 0;\n    // Node balance factor = left subtree height - right subtree height\n    return Height(node.left) - Height(node.right);\n}\n
      avl_tree.go
      /* Get balance factor */\nfunc (t *aVLTree) balanceFactor(node *TreeNode) int {\n    // Empty node balance factor is 0\n    if node == nil {\n        return 0\n    }\n    // Node balance factor = left subtree height - right subtree height\n    return t.height(node.Left) - t.height(node.Right)\n}\n
      avl_tree.swift
      /* Get balance factor */\nfunc balanceFactor(node: TreeNode?) -> Int {\n    // Empty node balance factor is 0\n    guard let node = node else { return 0 }\n    // Node balance factor = left subtree height - right subtree height\n    return height(node: node.left) - height(node: node.right)\n}\n
      avl_tree.js
      /* Get balance factor */\nbalanceFactor(node) {\n    // Empty node balance factor is 0\n    if (node === null) return 0;\n    // Node balance factor = left subtree height - right subtree height\n    return this.height(node.left) - this.height(node.right);\n}\n
      avl_tree.ts
      /* Get balance factor */\nbalanceFactor(node: TreeNode): number {\n    // Empty node balance factor is 0\n    if (node === null) return 0;\n    // Node balance factor = left subtree height - right subtree height\n    return this.height(node.left) - this.height(node.right);\n}\n
      avl_tree.dart
      /* Get balance factor */\nint balanceFactor(TreeNode? node) {\n  // Empty node balance factor is 0\n  if (node == null) return 0;\n  // Node balance factor = left subtree height - right subtree height\n  return height(node.left) - height(node.right);\n}\n
      avl_tree.rs
      /* Get balance factor */\nfn balance_factor(node: OptionTreeNodeRc) -> i32 {\n    match node {\n        // Empty node balance factor is 0\n        None => 0,\n        // Node balance factor = left subtree height - right subtree height\n        Some(node) => {\n            Self::height(node.borrow().left.clone()) - Self::height(node.borrow().right.clone())\n        }\n    }\n}\n
      avl_tree.c
      /* Get balance factor */\nint balanceFactor(TreeNode *node) {\n    // Empty node balance factor is 0\n    if (node == NULL) {\n        return 0;\n    }\n    // Node balance factor = left subtree height - right subtree height\n    return height(node->left) - height(node->right);\n}\n
      avl_tree.kt
      /* Get balance factor */\nfun balanceFactor(node: TreeNode?): Int {\n    // Empty node balance factor is 0\n    if (node == null) return 0\n    // Node balance factor = left subtree height - right subtree height\n    return height(node.left) - height(node.right)\n}\n
      avl_tree.rb
      ### Get balance factor ###\ndef balance_factor(node)\n  # Empty node balance factor is 0\n  return 0 if node.nil?\n\n  # Node balance factor = left subtree height - right subtree height\n  height(node.left) - height(node.right)\nend\n

      Tip

      Let the balance factor be \\(f\\), then the balance factor of any node in an AVL tree satisfies \\(-1 \\le f \\le 1\\).

      "},{"location":"chapter_tree/avl_tree/#752-rotations-in-avl-trees","title":"7.5.2 \u00a0 Rotations in Avl Trees","text":"

      The characteristic of AVL trees lies in the \"rotation\" operation, which can restore balance to unbalanced nodes without affecting the inorder traversal sequence of the binary tree. In other words, rotation operations can both maintain the property of a \"binary search tree\" and make the tree return to a \"balanced binary tree\".

      We call nodes with a balance factor absolute value \\(> 1\\) \"unbalanced nodes\". Depending on the imbalance situation, rotation operations are divided into four types: right rotation, left rotation, left rotation then right rotation, and right rotation then left rotation. Below we describe these rotation operations in detail.

      "},{"location":"chapter_tree/avl_tree/#1-right-rotation","title":"1. \u00a0 Right Rotation","text":"

      As shown in Figure 7-26, the value below the node is the balance factor. From bottom to top, the first unbalanced node in the binary tree is \"node 3\". We focus on the subtree with this unbalanced node as the root, denoting the node as node and its left child as child, and perform a \"right rotation\" operation. After the right rotation is completed, the subtree regains balance and still maintains the properties of a binary search tree.

      <1><2><3><4>

      Figure 7-26 \u00a0 Steps of right rotation

      As shown in Figure 7-27, when the child node has a right child (denoted as grand_child), a step needs to be added in the right rotation: set grand_child as the left child of node.

      Figure 7-27 \u00a0 Right rotation with grand_child

      \"Right rotation\" is a figurative term; in practice, it is achieved by modifying node pointers, as shown in the following code:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
      def right_rotate(self, node: TreeNode | None) -> TreeNode | None:\n    \"\"\"Right rotation operation\"\"\"\n    child = node.left\n    grand_child = child.right\n    # Using child as pivot, rotate node to the right\n    child.right = node\n    node.left = grand_child\n    # Update node height\n    self.update_height(node)\n    self.update_height(child)\n    # Return root node of subtree after rotation\n    return child\n
      avl_tree.cpp
      /* Right rotation operation */\nTreeNode *rightRotate(TreeNode *node) {\n    TreeNode *child = node->left;\n    TreeNode *grandChild = child->right;\n    // Using child as pivot, rotate node to the right\n    child->right = node;\n    node->left = grandChild;\n    // Update node height\n    updateHeight(node);\n    updateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.java
      /* Right rotation operation */\nTreeNode rightRotate(TreeNode node) {\n    TreeNode child = node.left;\n    TreeNode grandChild = child.right;\n    // Using child as pivot, rotate node to the right\n    child.right = node;\n    node.left = grandChild;\n    // Update node height\n    updateHeight(node);\n    updateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.cs
      /* Right rotation operation */\nTreeNode? RightRotate(TreeNode? node) {\n    TreeNode? child = node?.left;\n    TreeNode? grandChild = child?.right;\n    // Using child as pivot, rotate node to the right\n    child.right = node;\n    node.left = grandChild;\n    // Update node height\n    UpdateHeight(node);\n    UpdateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.go
      /* Right rotation operation */\nfunc (t *aVLTree) rightRotate(node *TreeNode) *TreeNode {\n    child := node.Left\n    grandChild := child.Right\n    // Using child as pivot, rotate node to the right\n    child.Right = node\n    node.Left = grandChild\n    // Update node height\n    t.updateHeight(node)\n    t.updateHeight(child)\n    // Return root node of subtree after rotation\n    return child\n}\n
      avl_tree.swift
      /* Right rotation operation */\nfunc rightRotate(node: TreeNode?) -> TreeNode? {\n    let child = node?.left\n    let grandChild = child?.right\n    // Using child as pivot, rotate node to the right\n    child?.right = node\n    node?.left = grandChild\n    // Update node height\n    updateHeight(node: node)\n    updateHeight(node: child)\n    // Return root node of subtree after rotation\n    return child\n}\n
      avl_tree.js
      /* Right rotation operation */\n#rightRotate(node) {\n    const child = node.left;\n    const grandChild = child.right;\n    // Using child as pivot, rotate node to the right\n    child.right = node;\n    node.left = grandChild;\n    // Update node height\n    this.#updateHeight(node);\n    this.#updateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.ts
      /* Right rotation operation */\nrightRotate(node: TreeNode): TreeNode {\n    const child = node.left;\n    const grandChild = child.right;\n    // Using child as pivot, rotate node to the right\n    child.right = node;\n    node.left = grandChild;\n    // Update node height\n    this.updateHeight(node);\n    this.updateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.dart
      /* Right rotation operation */\nTreeNode? rightRotate(TreeNode? node) {\n  TreeNode? child = node!.left;\n  TreeNode? grandChild = child!.right;\n  // Using child as pivot, rotate node to the right\n  child.right = node;\n  node.left = grandChild;\n  // Update node height\n  updateHeight(node);\n  updateHeight(child);\n  // Return root node of subtree after rotation\n  return child;\n}\n
      avl_tree.rs
      /* Right rotation operation */\nfn right_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n    match node {\n        Some(node) => {\n            let child = node.borrow().left.clone().unwrap();\n            let grand_child = child.borrow().right.clone();\n            // Using child as pivot, rotate node to the right\n            child.borrow_mut().right = Some(node.clone());\n            node.borrow_mut().left = grand_child;\n            // Update node height\n            Self::update_height(Some(node));\n            Self::update_height(Some(child.clone()));\n            // Return root node of subtree after rotation\n            Some(child)\n        }\n        None => None,\n    }\n}\n
      avl_tree.c
      /* Right rotation operation */\nTreeNode *rightRotate(TreeNode *node) {\n    TreeNode *child, *grandChild;\n    child = node->left;\n    grandChild = child->right;\n    // Using child as pivot, rotate node to the right\n    child->right = node;\n    node->left = grandChild;\n    // Update node height\n    updateHeight(node);\n    updateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.kt
      /* Right rotation operation */\nfun rightRotate(node: TreeNode?): TreeNode {\n    val child = node!!.left\n    val grandChild = child!!.right\n    // Using child as pivot, rotate node to the right\n    child.right = node\n    node.left = grandChild\n    // Update node height\n    updateHeight(node)\n    updateHeight(child)\n    // Return root node of subtree after rotation\n    return child\n}\n
      avl_tree.rb
      ### Right rotation ###\ndef right_rotate(node)\n  child = node.left\n  grand_child = child.right\n  # Using child as pivot, rotate node to the right\n  child.right = node\n  node.left = grand_child\n  # Update node height\n  update_height(node)\n  update_height(child)\n  # Return root node of subtree after rotation\n  child\nend\n
      "},{"location":"chapter_tree/avl_tree/#2-left-rotation","title":"2. \u00a0 Left Rotation","text":"

      Correspondingly, if considering the \"mirror\" of the above unbalanced binary tree, the \"left rotation\" operation shown in Figure 7-28 needs to be performed.

      Figure 7-28 \u00a0 Left rotation operation

      Similarly, as shown in Figure 7-29, when the child node has a left child (denoted as grand_child), a step needs to be added in the left rotation: set grand_child as the right child of node.

      Figure 7-29 \u00a0 Left rotation with grand_child

      It can be observed that right rotation and left rotation operations are mirror symmetric in logic, and the two imbalance cases they solve are also symmetric. Based on symmetry, we only need to replace all left in the right rotation implementation code with right, and all right with left, to obtain the left rotation implementation code:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
      def left_rotate(self, node: TreeNode | None) -> TreeNode | None:\n    \"\"\"Left rotation operation\"\"\"\n    child = node.right\n    grand_child = child.left\n    # Using child as pivot, rotate node to the left\n    child.left = node\n    node.right = grand_child\n    # Update node height\n    self.update_height(node)\n    self.update_height(child)\n    # Return root node of subtree after rotation\n    return child\n
      avl_tree.cpp
      /* Left rotation operation */\nTreeNode *leftRotate(TreeNode *node) {\n    TreeNode *child = node->right;\n    TreeNode *grandChild = child->left;\n    // Using child as pivot, rotate node to the left\n    child->left = node;\n    node->right = grandChild;\n    // Update node height\n    updateHeight(node);\n    updateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.java
      /* Left rotation operation */\nTreeNode leftRotate(TreeNode node) {\n    TreeNode child = node.right;\n    TreeNode grandChild = child.left;\n    // Using child as pivot, rotate node to the left\n    child.left = node;\n    node.right = grandChild;\n    // Update node height\n    updateHeight(node);\n    updateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.cs
      /* Left rotation operation */\nTreeNode? LeftRotate(TreeNode? node) {\n    TreeNode? child = node?.right;\n    TreeNode? grandChild = child?.left;\n    // Using child as pivot, rotate node to the left\n    child.left = node;\n    node.right = grandChild;\n    // Update node height\n    UpdateHeight(node);\n    UpdateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.go
      /* Left rotation operation */\nfunc (t *aVLTree) leftRotate(node *TreeNode) *TreeNode {\n    child := node.Right\n    grandChild := child.Left\n    // Using child as pivot, rotate node to the left\n    child.Left = node\n    node.Right = grandChild\n    // Update node height\n    t.updateHeight(node)\n    t.updateHeight(child)\n    // Return root node of subtree after rotation\n    return child\n}\n
      avl_tree.swift
      /* Left rotation operation */\nfunc leftRotate(node: TreeNode?) -> TreeNode? {\n    let child = node?.right\n    let grandChild = child?.left\n    // Using child as pivot, rotate node to the left\n    child?.left = node\n    node?.right = grandChild\n    // Update node height\n    updateHeight(node: node)\n    updateHeight(node: child)\n    // Return root node of subtree after rotation\n    return child\n}\n
      avl_tree.js
      /* Left rotation operation */\n#leftRotate(node) {\n    const child = node.right;\n    const grandChild = child.left;\n    // Using child as pivot, rotate node to the left\n    child.left = node;\n    node.right = grandChild;\n    // Update node height\n    this.#updateHeight(node);\n    this.#updateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.ts
      /* Left rotation operation */\nleftRotate(node: TreeNode): TreeNode {\n    const child = node.right;\n    const grandChild = child.left;\n    // Using child as pivot, rotate node to the left\n    child.left = node;\n    node.right = grandChild;\n    // Update node height\n    this.updateHeight(node);\n    this.updateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.dart
      /* Left rotation operation */\nTreeNode? leftRotate(TreeNode? node) {\n  TreeNode? child = node!.right;\n  TreeNode? grandChild = child!.left;\n  // Using child as pivot, rotate node to the left\n  child.left = node;\n  node.right = grandChild;\n  // Update node height\n  updateHeight(node);\n  updateHeight(child);\n  // Return root node of subtree after rotation\n  return child;\n}\n
      avl_tree.rs
      /* Left rotation operation */\nfn left_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n    match node {\n        Some(node) => {\n            let child = node.borrow().right.clone().unwrap();\n            let grand_child = child.borrow().left.clone();\n            // Using child as pivot, rotate node to the left\n            child.borrow_mut().left = Some(node.clone());\n            node.borrow_mut().right = grand_child;\n            // Update node height\n            Self::update_height(Some(node));\n            Self::update_height(Some(child.clone()));\n            // Return root node of subtree after rotation\n            Some(child)\n        }\n        None => None,\n    }\n}\n
      avl_tree.c
      /* Left rotation operation */\nTreeNode *leftRotate(TreeNode *node) {\n    TreeNode *child, *grandChild;\n    child = node->right;\n    grandChild = child->left;\n    // Using child as pivot, rotate node to the left\n    child->left = node;\n    node->right = grandChild;\n    // Update node height\n    updateHeight(node);\n    updateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.kt
      /* Left rotation operation */\nfun leftRotate(node: TreeNode?): TreeNode {\n    val child = node!!.right\n    val grandChild = child!!.left\n    // Using child as pivot, rotate node to the left\n    child.left = node\n    node.right = grandChild\n    // Update node height\n    updateHeight(node)\n    updateHeight(child)\n    // Return root node of subtree after rotation\n    return child\n}\n
      avl_tree.rb
      ### Left rotation ###\ndef left_rotate(node)\n  child = node.right\n  grand_child = child.left\n  # Using child as pivot, rotate node to the left\n  child.left = node\n  node.right = grand_child\n  # Update node height\n  update_height(node)\n  update_height(child)\n  # Return root node of subtree after rotation\n  child\nend\n
      "},{"location":"chapter_tree/avl_tree/#3-left-rotation-then-right-rotation","title":"3. \u00a0 Left Rotation Then Right Rotation","text":"

      For the unbalanced node 3 in Figure 7-30, using either left rotation or right rotation alone cannot restore the subtree to balance. In this case, a \"left rotation\" needs to be performed on child first, followed by a \"right rotation\" on node.

      Figure 7-30 \u00a0 Left-right rotation

      "},{"location":"chapter_tree/avl_tree/#4-right-rotation-then-left-rotation","title":"4. \u00a0 Right Rotation Then Left Rotation","text":"

      As shown in Figure 7-31, for the mirror case of the above unbalanced binary tree, a \"right rotation\" needs to be performed on child first, then a \"left rotation\" on node.

      Figure 7-31 \u00a0 Right-left rotation

      "},{"location":"chapter_tree/avl_tree/#5-choice-of-rotation","title":"5. \u00a0 Choice of Rotation","text":"

      The four imbalances shown in Figure 7-32 correspond one-to-one with the above cases, requiring right rotation, left rotation then right rotation, right rotation then left rotation, and left rotation operations respectively.

      Figure 7-32 \u00a0 The four rotation cases of AVL tree

      As shown in Table 7-3, we determine which case the unbalanced node belongs to by judging the signs of the balance factor of the unbalanced node and the balance factor of its taller-side child node.

      Table 7-3 \u00a0 Conditions for Choosing Among the Four Rotation Cases

      Balance factor of the unbalanced node Balance factor of the child node Rotation method to apply \\(> 1\\) (left-leaning tree) \\(\\geq 0\\) Right rotation \\(> 1\\) (left-leaning tree) \\(<0\\) Left rotation then right rotation \\(< -1\\) (right-leaning tree) \\(\\leq 0\\) Left rotation \\(< -1\\) (right-leaning tree) \\(>0\\) Right rotation then left rotation

      For ease of use, we encapsulate the rotation operations into a function. With this function, we can perform rotations for various imbalance situations, restoring balance to unbalanced nodes. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
      def rotate(self, node: TreeNode | None) -> TreeNode | None:\n    \"\"\"Perform rotation operation to restore balance to this subtree\"\"\"\n    # Get balance factor of node\n    balance_factor = self.balance_factor(node)\n    # Left-leaning tree\n    if balance_factor > 1:\n        if self.balance_factor(node.left) >= 0:\n            # Right rotation\n            return self.right_rotate(node)\n        else:\n            # First left rotation then right rotation\n            node.left = self.left_rotate(node.left)\n            return self.right_rotate(node)\n    # Right-leaning tree\n    elif balance_factor < -1:\n        if self.balance_factor(node.right) <= 0:\n            # Left rotation\n            return self.left_rotate(node)\n        else:\n            # First right rotation then left rotation\n            node.right = self.right_rotate(node.right)\n            return self.left_rotate(node)\n    # Balanced tree, no rotation needed, return directly\n    return node\n
      avl_tree.cpp
      /* Perform rotation operation to restore balance to this subtree */\nTreeNode *rotate(TreeNode *node) {\n    // Get balance factor of node\n    int _balanceFactor = balanceFactor(node);\n    // Left-leaning tree\n    if (_balanceFactor > 1) {\n        if (balanceFactor(node->left) >= 0) {\n            // Right rotation\n            return rightRotate(node);\n        } else {\n            // First left rotation then right rotation\n            node->left = leftRotate(node->left);\n            return rightRotate(node);\n        }\n    }\n    // Right-leaning tree\n    if (_balanceFactor < -1) {\n        if (balanceFactor(node->right) <= 0) {\n            // Left rotation\n            return leftRotate(node);\n        } else {\n            // First right rotation then left rotation\n            node->right = rightRotate(node->right);\n            return leftRotate(node);\n        }\n    }\n    // Balanced tree, no rotation needed, return directly\n    return node;\n}\n
      avl_tree.java
      /* Perform rotation operation to restore balance to this subtree */\nTreeNode rotate(TreeNode node) {\n    // Get balance factor of node\n    int balanceFactor = balanceFactor(node);\n    // Left-leaning tree\n    if (balanceFactor > 1) {\n        if (balanceFactor(node.left) >= 0) {\n            // Right rotation\n            return rightRotate(node);\n        } else {\n            // First left rotation then right rotation\n            node.left = leftRotate(node.left);\n            return rightRotate(node);\n        }\n    }\n    // Right-leaning tree\n    if (balanceFactor < -1) {\n        if (balanceFactor(node.right) <= 0) {\n            // Left rotation\n            return leftRotate(node);\n        } else {\n            // First right rotation then left rotation\n            node.right = rightRotate(node.right);\n            return leftRotate(node);\n        }\n    }\n    // Balanced tree, no rotation needed, return directly\n    return node;\n}\n
      avl_tree.cs
      /* Perform rotation operation to restore balance to this subtree */\nTreeNode? Rotate(TreeNode? node) {\n    // Get balance factor of node\n    int balanceFactorInt = BalanceFactor(node);\n    // Left-leaning tree\n    if (balanceFactorInt > 1) {\n        if (BalanceFactor(node?.left) >= 0) {\n            // Right rotation\n            return RightRotate(node);\n        } else {\n            // First left rotation then right rotation\n            node!.left = LeftRotate(node!.left);\n            return RightRotate(node);\n        }\n    }\n    // Right-leaning tree\n    if (balanceFactorInt < -1) {\n        if (BalanceFactor(node?.right) <= 0) {\n            // Left rotation\n            return LeftRotate(node);\n        } else {\n            // First right rotation then left rotation\n            node!.right = RightRotate(node!.right);\n            return LeftRotate(node);\n        }\n    }\n    // Balanced tree, no rotation needed, return directly\n    return node;\n}\n
      avl_tree.go
      /* Perform rotation operation to restore balance to this subtree */\nfunc (t *aVLTree) rotate(node *TreeNode) *TreeNode {\n    // Get balance factor of node\n    // Go recommends short variables, here bf refers to t.balanceFactor\n    bf := t.balanceFactor(node)\n    // Left-leaning tree\n    if bf > 1 {\n        if t.balanceFactor(node.Left) >= 0 {\n            // Right rotation\n            return t.rightRotate(node)\n        } else {\n            // First left rotation then right rotation\n            node.Left = t.leftRotate(node.Left)\n            return t.rightRotate(node)\n        }\n    }\n    // Right-leaning tree\n    if bf < -1 {\n        if t.balanceFactor(node.Right) <= 0 {\n            // Left rotation\n            return t.leftRotate(node)\n        } else {\n            // First right rotation then left rotation\n            node.Right = t.rightRotate(node.Right)\n            return t.leftRotate(node)\n        }\n    }\n    // Balanced tree, no rotation needed, return directly\n    return node\n}\n
      avl_tree.swift
      /* Perform rotation operation to restore balance to this subtree */\nfunc rotate(node: TreeNode?) -> TreeNode? {\n    // Get balance factor of node\n    let balanceFactor = balanceFactor(node: node)\n    // Left-leaning tree\n    if balanceFactor > 1 {\n        if self.balanceFactor(node: node?.left) >= 0 {\n            // Right rotation\n            return rightRotate(node: node)\n        } else {\n            // First left rotation then right rotation\n            node?.left = leftRotate(node: node?.left)\n            return rightRotate(node: node)\n        }\n    }\n    // Right-leaning tree\n    if balanceFactor < -1 {\n        if self.balanceFactor(node: node?.right) <= 0 {\n            // Left rotation\n            return leftRotate(node: node)\n        } else {\n            // First right rotation then left rotation\n            node?.right = rightRotate(node: node?.right)\n            return leftRotate(node: node)\n        }\n    }\n    // Balanced tree, no rotation needed, return directly\n    return node\n}\n
      avl_tree.js
      /* Perform rotation operation to restore balance to this subtree */\n#rotate(node) {\n    // Get balance factor of node\n    const balanceFactor = this.balanceFactor(node);\n    // Left-leaning tree\n    if (balanceFactor > 1) {\n        if (this.balanceFactor(node.left) >= 0) {\n            // Right rotation\n            return this.#rightRotate(node);\n        } else {\n            // First left rotation then right rotation\n            node.left = this.#leftRotate(node.left);\n            return this.#rightRotate(node);\n        }\n    }\n    // Right-leaning tree\n    if (balanceFactor < -1) {\n        if (this.balanceFactor(node.right) <= 0) {\n            // Left rotation\n            return this.#leftRotate(node);\n        } else {\n            // First right rotation then left rotation\n            node.right = this.#rightRotate(node.right);\n            return this.#leftRotate(node);\n        }\n    }\n    // Balanced tree, no rotation needed, return directly\n    return node;\n}\n
      avl_tree.ts
      /* Perform rotation operation to restore balance to this subtree */\nrotate(node: TreeNode): TreeNode {\n    // Get balance factor of node\n    const balanceFactor = this.balanceFactor(node);\n    // Left-leaning tree\n    if (balanceFactor > 1) {\n        if (this.balanceFactor(node.left) >= 0) {\n            // Right rotation\n            return this.rightRotate(node);\n        } else {\n            // First left rotation then right rotation\n            node.left = this.leftRotate(node.left);\n            return this.rightRotate(node);\n        }\n    }\n    // Right-leaning tree\n    if (balanceFactor < -1) {\n        if (this.balanceFactor(node.right) <= 0) {\n            // Left rotation\n            return this.leftRotate(node);\n        } else {\n            // First right rotation then left rotation\n            node.right = this.rightRotate(node.right);\n            return this.leftRotate(node);\n        }\n    }\n    // Balanced tree, no rotation needed, return directly\n    return node;\n}\n
      avl_tree.dart
      /* Perform rotation operation to restore balance to this subtree */\nTreeNode? rotate(TreeNode? node) {\n  // Get balance factor of node\n  int factor = balanceFactor(node);\n  // Left-leaning tree\n  if (factor > 1) {\n    if (balanceFactor(node!.left) >= 0) {\n      // Right rotation\n      return rightRotate(node);\n    } else {\n      // First left rotation then right rotation\n      node.left = leftRotate(node.left);\n      return rightRotate(node);\n    }\n  }\n  // Right-leaning tree\n  if (factor < -1) {\n    if (balanceFactor(node!.right) <= 0) {\n      // Left rotation\n      return leftRotate(node);\n    } else {\n      // First right rotation then left rotation\n      node.right = rightRotate(node.right);\n      return leftRotate(node);\n    }\n  }\n  // Balanced tree, no rotation needed, return directly\n  return node;\n}\n
      avl_tree.rs
      /* Perform rotation operation to restore balance to this subtree */\nfn rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n    // Get balance factor of node\n    let balance_factor = Self::balance_factor(node.clone());\n    // Left-leaning tree\n    if balance_factor > 1 {\n        let node = node.unwrap();\n        if Self::balance_factor(node.borrow().left.clone()) >= 0 {\n            // Right rotation\n            Self::right_rotate(Some(node))\n        } else {\n            // First left rotation then right rotation\n            let left = node.borrow().left.clone();\n            node.borrow_mut().left = Self::left_rotate(left);\n            Self::right_rotate(Some(node))\n        }\n    }\n    // Right-leaning tree\n    else if balance_factor < -1 {\n        let node = node.unwrap();\n        if Self::balance_factor(node.borrow().right.clone()) <= 0 {\n            // Left rotation\n            Self::left_rotate(Some(node))\n        } else {\n            // First right rotation then left rotation\n            let right = node.borrow().right.clone();\n            node.borrow_mut().right = Self::right_rotate(right);\n            Self::left_rotate(Some(node))\n        }\n    } else {\n        // Balanced tree, no rotation needed, return directly\n        node\n    }\n}\n
      avl_tree.c
      /* Perform rotation operation to restore balance to this subtree */\nTreeNode *rotate(TreeNode *node) {\n    // Get balance factor of node\n    int bf = balanceFactor(node);\n    // Left-leaning tree\n    if (bf > 1) {\n        if (balanceFactor(node->left) >= 0) {\n            // Right rotation\n            return rightRotate(node);\n        } else {\n            // First left rotation then right rotation\n            node->left = leftRotate(node->left);\n            return rightRotate(node);\n        }\n    }\n    // Right-leaning tree\n    if (bf < -1) {\n        if (balanceFactor(node->right) <= 0) {\n            // Left rotation\n            return leftRotate(node);\n        } else {\n            // First right rotation then left rotation\n            node->right = rightRotate(node->right);\n            return leftRotate(node);\n        }\n    }\n    // Balanced tree, no rotation needed, return directly\n    return node;\n}\n
      avl_tree.kt
      /* Perform rotation operation to restore balance to this subtree */\nfun rotate(node: TreeNode): TreeNode {\n    // Get balance factor of node\n    val balanceFactor = balanceFactor(node)\n    // Left-leaning tree\n    if (balanceFactor > 1) {\n        if (balanceFactor(node.left) >= 0) {\n            // Right rotation\n            return rightRotate(node)\n        } else {\n            // First left rotation then right rotation\n            node.left = leftRotate(node.left)\n            return rightRotate(node)\n        }\n    }\n    // Right-leaning tree\n    if (balanceFactor < -1) {\n        if (balanceFactor(node.right) <= 0) {\n            // Left rotation\n            return leftRotate(node)\n        } else {\n            // First right rotation then left rotation\n            node.right = rightRotate(node.right)\n            return leftRotate(node)\n        }\n    }\n    // Balanced tree, no rotation needed, return directly\n    return node\n}\n
      avl_tree.rb
      ### Perform rotation to rebalance subtree ###\ndef rotate(node)\n  # Get balance factor of node\n  balance_factor = balance_factor(node)\n  # Left-heavy tree\n  if balance_factor > 1\n    if balance_factor(node.left) >= 0\n      # Right rotation\n      return right_rotate(node)\n    else\n      # First left rotation then right rotation\n      node.left = left_rotate(node.left)\n      return right_rotate(node)\n    end\n  # Right-heavy tree\n  elsif balance_factor < -1\n    if balance_factor(node.right) <= 0\n      # Left rotation\n      return left_rotate(node)\n    else\n      # First right rotation then left rotation\n      node.right = right_rotate(node.right)\n      return left_rotate(node)\n    end\n  end\n  # Balanced tree, no rotation needed, return directly\n  node\nend\n
      "},{"location":"chapter_tree/avl_tree/#753-common-operations-in-avl-trees","title":"7.5.3 \u00a0 Common Operations in Avl Trees","text":""},{"location":"chapter_tree/avl_tree/#1-node-insertion","title":"1. \u00a0 Node Insertion","text":"

      The node insertion operation in AVL trees is similar in principle to that in binary search trees. The only difference is that after inserting a node in an AVL tree, a series of unbalanced nodes may appear on the path from that node to the root. Therefore, we need to start from this node and perform rotation operations from bottom to top, restoring balance to all unbalanced nodes. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
      def insert(self, val):\n    \"\"\"Insert node\"\"\"\n    self._root = self.insert_helper(self._root, val)\n\ndef insert_helper(self, node: TreeNode | None, val: int) -> TreeNode:\n    \"\"\"Recursively insert node (helper method)\"\"\"\n    if node is None:\n        return TreeNode(val)\n    # 1. Find insertion position and insert node\n    if val < node.val:\n        node.left = self.insert_helper(node.left, val)\n    elif val > node.val:\n        node.right = self.insert_helper(node.right, val)\n    else:\n        # Duplicate node not inserted, return directly\n        return node\n    # Update node height\n    self.update_height(node)\n    # 2. Perform rotation operation to restore balance to this subtree\n    return self.rotate(node)\n
      avl_tree.cpp
      /* Insert node */\nvoid insert(int val) {\n    root = insertHelper(root, val);\n}\n\n/* Recursively insert node (helper method) */\nTreeNode *insertHelper(TreeNode *node, int val) {\n    if (node == nullptr)\n        return new TreeNode(val);\n    /* 1. Find insertion position and insert node */\n    if (val < node->val)\n        node->left = insertHelper(node->left, val);\n    else if (val > node->val)\n        node->right = insertHelper(node->right, val);\n    else\n        return node;    // Duplicate node not inserted, return directly\n    updateHeight(node); // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.java
      /* Insert node */\nvoid insert(int val) {\n    root = insertHelper(root, val);\n}\n\n/* Recursively insert node (helper method) */\nTreeNode insertHelper(TreeNode node, int val) {\n    if (node == null)\n        return new TreeNode(val);\n    /* 1. Find insertion position and insert node */\n    if (val < node.val)\n        node.left = insertHelper(node.left, val);\n    else if (val > node.val)\n        node.right = insertHelper(node.right, val);\n    else\n        return node; // Duplicate node not inserted, return directly\n    updateHeight(node); // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.cs
      /* Insert node */\nvoid Insert(int val) {\n    root = InsertHelper(root, val);\n}\n\n/* Recursively insert node (helper method) */\nTreeNode? InsertHelper(TreeNode? node, int val) {\n    if (node == null) return new TreeNode(val);\n    /* 1. Find insertion position and insert node */\n    if (val < node.val)\n        node.left = InsertHelper(node.left, val);\n    else if (val > node.val)\n        node.right = InsertHelper(node.right, val);\n    else\n        return node;     // Duplicate node not inserted, return directly\n    UpdateHeight(node);  // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = Rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.go
      /* Insert node */\nfunc (t *aVLTree) insert(val int) {\n    t.root = t.insertHelper(t.root, val)\n}\n\n/* Recursively insert node (helper function) */\nfunc (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode {\n    if node == nil {\n        return NewTreeNode(val)\n    }\n    /* 1. Find insertion position and insert node */\n    if val < node.Val.(int) {\n        node.Left = t.insertHelper(node.Left, val)\n    } else if val > node.Val.(int) {\n        node.Right = t.insertHelper(node.Right, val)\n    } else {\n        // Duplicate node not inserted, return directly\n        return node\n    }\n    // Update node height\n    t.updateHeight(node)\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = t.rotate(node)\n    // Return root node of subtree\n    return node\n}\n
      avl_tree.swift
      /* Insert node */\nfunc insert(val: Int) {\n    root = insertHelper(node: root, val: val)\n}\n\n/* Recursively insert node (helper method) */\nfunc insertHelper(node: TreeNode?, val: Int) -> TreeNode? {\n    var node = node\n    if node == nil {\n        return TreeNode(x: val)\n    }\n    /* 1. Find insertion position and insert node */\n    if val < node!.val {\n        node?.left = insertHelper(node: node?.left, val: val)\n    } else if val > node!.val {\n        node?.right = insertHelper(node: node?.right, val: val)\n    } else {\n        return node // Duplicate node not inserted, return directly\n    }\n    updateHeight(node: node) // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = rotate(node: node)\n    // Return root node of subtree\n    return node\n}\n
      avl_tree.js
      /* Insert node */\ninsert(val) {\n    this.root = this.#insertHelper(this.root, val);\n}\n\n/* Recursively insert node (helper method) */\n#insertHelper(node, val) {\n    if (node === null) return new TreeNode(val);\n    /* 1. Find insertion position and insert node */\n    if (val < node.val) node.left = this.#insertHelper(node.left, val);\n    else if (val > node.val)\n        node.right = this.#insertHelper(node.right, val);\n    else return node; // Duplicate node not inserted, return directly\n    this.#updateHeight(node); // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = this.#rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.ts
      /* Insert node */\ninsert(val: number): void {\n    this.root = this.insertHelper(this.root, val);\n}\n\n/* Recursively insert node (helper method) */\ninsertHelper(node: TreeNode, val: number): TreeNode {\n    if (node === null) return new TreeNode(val);\n    /* 1. Find insertion position and insert node */\n    if (val < node.val) {\n        node.left = this.insertHelper(node.left, val);\n    } else if (val > node.val) {\n        node.right = this.insertHelper(node.right, val);\n    } else {\n        return node; // Duplicate node not inserted, return directly\n    }\n    this.updateHeight(node); // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = this.rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.dart
      /* Insert node */\nvoid insert(int val) {\n  root = insertHelper(root, val);\n}\n\n/* Recursively insert node (helper method) */\nTreeNode? insertHelper(TreeNode? node, int val) {\n  if (node == null) return TreeNode(val);\n  /* 1. Find insertion position and insert node */\n  if (val < node.val)\n    node.left = insertHelper(node.left, val);\n  else if (val > node.val)\n    node.right = insertHelper(node.right, val);\n  else\n    return node; // Duplicate node not inserted, return directly\n  updateHeight(node); // Update node height\n  /* 2. Perform rotation operation to restore balance to this subtree */\n  node = rotate(node);\n  // Return root node of subtree\n  return node;\n}\n
      avl_tree.rs
      /* Insert node */\nfn insert(&mut self, val: i32) {\n    self.root = Self::insert_helper(self.root.clone(), val);\n}\n\n/* Recursively insert node (helper method) */\nfn insert_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {\n    match node {\n        Some(mut node) => {\n            /* 1. Find insertion position and insert node */\n            match {\n                let node_val = node.borrow().val;\n                node_val\n            }\n            .cmp(&val)\n            {\n                Ordering::Greater => {\n                    let left = node.borrow().left.clone();\n                    node.borrow_mut().left = Self::insert_helper(left, val);\n                }\n                Ordering::Less => {\n                    let right = node.borrow().right.clone();\n                    node.borrow_mut().right = Self::insert_helper(right, val);\n                }\n                Ordering::Equal => {\n                    return Some(node); // Duplicate node not inserted, return directly\n                }\n            }\n            Self::update_height(Some(node.clone())); // Update node height\n\n            /* 2. Perform rotation operation to restore balance to this subtree */\n            node = Self::rotate(Some(node)).unwrap();\n            // Return root node of subtree\n            Some(node)\n        }\n        None => Some(TreeNode::new(val)),\n    }\n}\n
      avl_tree.c
      /* Insert node */\nvoid insert(AVLTree *tree, int val) {\n    tree->root = insertHelper(tree->root, val);\n}\n\n/* Recursively insert node (helper function) */\nTreeNode *insertHelper(TreeNode *node, int val) {\n    if (node == NULL) {\n        return newTreeNode(val);\n    }\n    /* 1. Find insertion position and insert node */\n    if (val < node->val) {\n        node->left = insertHelper(node->left, val);\n    } else if (val > node->val) {\n        node->right = insertHelper(node->right, val);\n    } else {\n        // Duplicate node not inserted, return directly\n        return node;\n    }\n    // Update node height\n    updateHeight(node);\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.kt
      /* Insert node */\nfun insert(_val: Int) {\n    root = insertHelper(root, _val)\n}\n\n/* Recursively insert node (helper method) */\nfun insertHelper(n: TreeNode?, _val: Int): TreeNode {\n    if (n == null)\n        return TreeNode(_val)\n    var node = n\n    /* 1. Find insertion position and insert node */\n    if (_val < node._val)\n        node.left = insertHelper(node.left, _val)\n    else if (_val > node._val)\n        node.right = insertHelper(node.right, _val)\n    else\n        return node // Duplicate node not inserted, return directly\n    updateHeight(node) // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = rotate(node)\n    // Return root node of subtree\n    return node\n}\n
      avl_tree.rb
      ### Insert node ###\ndef insert(val)\n  @root = insert_helper(@root, val)\nend\n\n### Recursively insert node (helper method) ###\ndef insert_helper(node, val)\n  return TreeNode.new(val) if node.nil?\n  # 1. Find insertion position and insert node\n  if val < node.val\n    node.left = insert_helper(node.left, val)\n  elsif val > node.val\n    node.right = insert_helper(node.right, val)\n  else\n    # Duplicate node not inserted, return directly\n    return node\n  end\n  # Update node height\n  update_height(node)\n  # 2. Perform rotation operation to restore balance to this subtree\n  rotate(node)\nend\n
      "},{"location":"chapter_tree/avl_tree/#2-node-removal","title":"2. \u00a0 Node Removal","text":"

      Similarly, on the basis of the binary search tree's node removal method, rotation operations need to be performed from bottom to top to restore balance to all unbalanced nodes. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
      def remove(self, val: int):\n    \"\"\"Delete node\"\"\"\n    self._root = self.remove_helper(self._root, val)\n\ndef remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None:\n    \"\"\"Recursively delete node (helper method)\"\"\"\n    if node is None:\n        return None\n    # 1. Find node and delete\n    if val < node.val:\n        node.left = self.remove_helper(node.left, val)\n    elif val > node.val:\n        node.right = self.remove_helper(node.right, val)\n    else:\n        if node.left is None or node.right is None:\n            child = node.left or node.right\n            # Number of child nodes = 0, delete node directly and return\n            if child is None:\n                return None\n            # Number of child nodes = 1, delete node directly\n            else:\n                node = child\n        else:\n            # Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n            temp = node.right\n            while temp.left is not None:\n                temp = temp.left\n            node.right = self.remove_helper(node.right, temp.val)\n            node.val = temp.val\n    # Update node height\n    self.update_height(node)\n    # 2. Perform rotation operation to restore balance to this subtree\n    return self.rotate(node)\n
      avl_tree.cpp
      /* Remove node */\nvoid remove(int val) {\n    root = removeHelper(root, val);\n}\n\n/* Recursively delete node (helper method) */\nTreeNode *removeHelper(TreeNode *node, int val) {\n    if (node == nullptr)\n        return nullptr;\n    /* 1. Find node and delete */\n    if (val < node->val)\n        node->left = removeHelper(node->left, val);\n    else if (val > node->val)\n        node->right = removeHelper(node->right, val);\n    else {\n        if (node->left == nullptr || node->right == nullptr) {\n            TreeNode *child = node->left != nullptr ? node->left : node->right;\n            // Number of child nodes = 0, delete node directly and return\n            if (child == nullptr) {\n                delete node;\n                return nullptr;\n            }\n            // Number of child nodes = 1, delete node directly\n            else {\n                delete node;\n                node = child;\n            }\n        } else {\n            // Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n            TreeNode *temp = node->right;\n            while (temp->left != nullptr) {\n                temp = temp->left;\n            }\n            int tempVal = temp->val;\n            node->right = removeHelper(node->right, temp->val);\n            node->val = tempVal;\n        }\n    }\n    updateHeight(node); // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.java
      /* Remove node */\nvoid remove(int val) {\n    root = removeHelper(root, val);\n}\n\n/* Recursively delete node (helper method) */\nTreeNode removeHelper(TreeNode node, int val) {\n    if (node == null)\n        return null;\n    /* 1. Find node and delete */\n    if (val < node.val)\n        node.left = removeHelper(node.left, val);\n    else if (val > node.val)\n        node.right = removeHelper(node.right, val);\n    else {\n        if (node.left == null || node.right == null) {\n            TreeNode child = node.left != null ? node.left : node.right;\n            // Number of child nodes = 0, delete node directly and return\n            if (child == null)\n                return null;\n            // Number of child nodes = 1, delete node directly\n            else\n                node = child;\n        } else {\n            // Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n            TreeNode temp = node.right;\n            while (temp.left != null) {\n                temp = temp.left;\n            }\n            node.right = removeHelper(node.right, temp.val);\n            node.val = temp.val;\n        }\n    }\n    updateHeight(node); // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.cs
      /* Remove node */\nvoid Remove(int val) {\n    root = RemoveHelper(root, val);\n}\n\n/* Recursively delete node (helper method) */\nTreeNode? RemoveHelper(TreeNode? node, int val) {\n    if (node == null) return null;\n    /* 1. Find node and delete */\n    if (val < node.val)\n        node.left = RemoveHelper(node.left, val);\n    else if (val > node.val)\n        node.right = RemoveHelper(node.right, val);\n    else {\n        if (node.left == null || node.right == null) {\n            TreeNode? child = node.left ?? node.right;\n            // Number of child nodes = 0, delete node directly and return\n            if (child == null)\n                return null;\n            // Number of child nodes = 1, delete node directly\n            else\n                node = child;\n        } else {\n            // Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n            TreeNode? temp = node.right;\n            while (temp.left != null) {\n                temp = temp.left;\n            }\n            node.right = RemoveHelper(node.right, temp.val!.Value);\n            node.val = temp.val;\n        }\n    }\n    UpdateHeight(node);  // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = Rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.go
      /* Remove node */\nfunc (t *aVLTree) remove(val int) {\n    t.root = t.removeHelper(t.root, val)\n}\n\n/* Recursively remove node (helper function) */\nfunc (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {\n    if node == nil {\n        return nil\n    }\n    /* 1. Find node and delete */\n    if val < node.Val.(int) {\n        node.Left = t.removeHelper(node.Left, val)\n    } else if val > node.Val.(int) {\n        node.Right = t.removeHelper(node.Right, val)\n    } else {\n        if node.Left == nil || node.Right == nil {\n            child := node.Left\n            if node.Right != nil {\n                child = node.Right\n            }\n            if child == nil {\n                // Number of child nodes = 0, delete node directly and return\n                return nil\n            } else {\n                // Number of child nodes = 1, delete node directly\n                node = child\n            }\n        } else {\n            // Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n            temp := node.Right\n            for temp.Left != nil {\n                temp = temp.Left\n            }\n            node.Right = t.removeHelper(node.Right, temp.Val.(int))\n            node.Val = temp.Val\n        }\n    }\n    // Update node height\n    t.updateHeight(node)\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = t.rotate(node)\n    // Return root node of subtree\n    return node\n}\n
      avl_tree.swift
      /* Remove node */\nfunc remove(val: Int) {\n    root = removeHelper(node: root, val: val)\n}\n\n/* Recursively delete node (helper method) */\nfunc removeHelper(node: TreeNode?, val: Int) -> TreeNode? {\n    var node = node\n    if node == nil {\n        return nil\n    }\n    /* 1. Find node and delete */\n    if val < node!.val {\n        node?.left = removeHelper(node: node?.left, val: val)\n    } else if val > node!.val {\n        node?.right = removeHelper(node: node?.right, val: val)\n    } else {\n        if node?.left == nil || node?.right == nil {\n            let child = node?.left ?? node?.right\n            // Number of child nodes = 0, delete node directly and return\n            if child == nil {\n                return nil\n            }\n            // Number of child nodes = 1, delete node directly\n            else {\n                node = child\n            }\n        } else {\n            // Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n            var temp = node?.right\n            while temp?.left != nil {\n                temp = temp?.left\n            }\n            node?.right = removeHelper(node: node?.right, val: temp!.val)\n            node?.val = temp!.val\n        }\n    }\n    updateHeight(node: node) // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = rotate(node: node)\n    // Return root node of subtree\n    return node\n}\n
      avl_tree.js
      /* Remove node */\nremove(val) {\n    this.root = this.#removeHelper(this.root, val);\n}\n\n/* Recursively delete node (helper method) */\n#removeHelper(node, val) {\n    if (node === null) return null;\n    /* 1. Find node and delete */\n    if (val < node.val) node.left = this.#removeHelper(node.left, val);\n    else if (val > node.val)\n        node.right = this.#removeHelper(node.right, val);\n    else {\n        if (node.left === null || node.right === null) {\n            const child = node.left !== null ? node.left : node.right;\n            // Number of child nodes = 0, delete node directly and return\n            if (child === null) return null;\n            // Number of child nodes = 1, delete node directly\n            else node = child;\n        } else {\n            // Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n            let temp = node.right;\n            while (temp.left !== null) {\n                temp = temp.left;\n            }\n            node.right = this.#removeHelper(node.right, temp.val);\n            node.val = temp.val;\n        }\n    }\n    this.#updateHeight(node); // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = this.#rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.ts
      /* Remove node */\nremove(val: number): void {\n    this.root = this.removeHelper(this.root, val);\n}\n\n/* Recursively delete node (helper method) */\nremoveHelper(node: TreeNode, val: number): TreeNode {\n    if (node === null) return null;\n    /* 1. Find node and delete */\n    if (val < node.val) {\n        node.left = this.removeHelper(node.left, val);\n    } else if (val > node.val) {\n        node.right = this.removeHelper(node.right, val);\n    } else {\n        if (node.left === null || node.right === null) {\n            const child = node.left !== null ? node.left : node.right;\n            // Number of child nodes = 0, delete node directly and return\n            if (child === null) {\n                return null;\n            } else {\n                // Number of child nodes = 1, delete node directly\n                node = child;\n            }\n        } else {\n            // Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n            let temp = node.right;\n            while (temp.left !== null) {\n                temp = temp.left;\n            }\n            node.right = this.removeHelper(node.right, temp.val);\n            node.val = temp.val;\n        }\n    }\n    this.updateHeight(node); // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = this.rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.dart
      /* Remove node */\nvoid remove(int val) {\n  root = removeHelper(root, val);\n}\n\n/* Recursively delete node (helper method) */\nTreeNode? removeHelper(TreeNode? node, int val) {\n  if (node == null) return null;\n  /* 1. Find node and delete */\n  if (val < node.val)\n    node.left = removeHelper(node.left, val);\n  else if (val > node.val)\n    node.right = removeHelper(node.right, val);\n  else {\n    if (node.left == null || node.right == null) {\n      TreeNode? child = node.left ?? node.right;\n      // Number of child nodes = 0, delete node directly and return\n      if (child == null)\n        return null;\n      // Number of child nodes = 1, delete node directly\n      else\n        node = child;\n    } else {\n      // Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n      TreeNode? temp = node.right;\n      while (temp!.left != null) {\n        temp = temp.left;\n      }\n      node.right = removeHelper(node.right, temp.val);\n      node.val = temp.val;\n    }\n  }\n  updateHeight(node); // Update node height\n  /* 2. Perform rotation operation to restore balance to this subtree */\n  node = rotate(node);\n  // Return root node of subtree\n  return node;\n}\n
      avl_tree.rs
      /* Remove node */\nfn remove(&self, val: i32) {\n    Self::remove_helper(self.root.clone(), val);\n}\n\n/* Recursively delete node (helper method) */\nfn remove_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {\n    match node {\n        Some(mut node) => {\n            /* 1. Find node and delete */\n            if val < node.borrow().val {\n                let left = node.borrow().left.clone();\n                node.borrow_mut().left = Self::remove_helper(left, val);\n            } else if val > node.borrow().val {\n                let right = node.borrow().right.clone();\n                node.borrow_mut().right = Self::remove_helper(right, val);\n            } else if node.borrow().left.is_none() || node.borrow().right.is_none() {\n                let child = if node.borrow().left.is_some() {\n                    node.borrow().left.clone()\n                } else {\n                    node.borrow().right.clone()\n                };\n                match child {\n                    // Number of child nodes = 0, delete node directly and return\n                    None => {\n                        return None;\n                    }\n                    // Number of child nodes = 1, delete node directly\n                    Some(child) => node = child,\n                }\n            } else {\n                // Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n                let mut temp = node.borrow().right.clone().unwrap();\n                loop {\n                    let temp_left = temp.borrow().left.clone();\n                    if temp_left.is_none() {\n                        break;\n                    }\n                    temp = temp_left.unwrap();\n                }\n                let right = node.borrow().right.clone();\n                node.borrow_mut().right = Self::remove_helper(right, temp.borrow().val);\n                node.borrow_mut().val = temp.borrow().val;\n            }\n            Self::update_height(Some(node.clone())); // Update node height\n\n            /* 2. Perform rotation operation to restore balance to this subtree */\n            node = Self::rotate(Some(node)).unwrap();\n            // Return root node of subtree\n            Some(node)\n        }\n        None => None,\n    }\n}\n
      avl_tree.c
      /* Remove node */\n// Cannot use remove keyword here due to stdio.h inclusion\nvoid removeItem(AVLTree *tree, int val) {\n    TreeNode *root = removeHelper(tree->root, val);\n}\n\n/* Recursively remove node (helper function) */\nTreeNode *removeHelper(TreeNode *node, int val) {\n    TreeNode *child, *grandChild;\n    if (node == NULL) {\n        return NULL;\n    }\n    /* 1. Find node and delete */\n    if (val < node->val) {\n        node->left = removeHelper(node->left, val);\n    } else if (val > node->val) {\n        node->right = removeHelper(node->right, val);\n    } else {\n        if (node->left == NULL || node->right == NULL) {\n            child = node->left;\n            if (node->right != NULL) {\n                child = node->right;\n            }\n            // Number of child nodes = 0, delete node directly and return\n            if (child == NULL) {\n                return NULL;\n            } else {\n                // Number of child nodes = 1, delete node directly\n                node = child;\n            }\n        } else {\n            // Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n            TreeNode *temp = node->right;\n            while (temp->left != NULL) {\n                temp = temp->left;\n            }\n            int tempVal = temp->val;\n            node->right = removeHelper(node->right, temp->val);\n            node->val = tempVal;\n        }\n    }\n    // Update node height\n    updateHeight(node);\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.kt
      /* Remove node */\nfun remove(_val: Int) {\n    root = removeHelper(root, _val)\n}\n\n/* Recursively delete node (helper method) */\nfun removeHelper(n: TreeNode?, _val: Int): TreeNode? {\n    var node = n ?: return null\n    /* 1. Find node and delete */\n    if (_val < node._val)\n        node.left = removeHelper(node.left, _val)\n    else if (_val > node._val)\n        node.right = removeHelper(node.right, _val)\n    else {\n        if (node.left == null || node.right == null) {\n            val child = if (node.left != null)\n                node.left\n            else\n                node.right\n            // Number of child nodes = 0, delete node directly and return\n            if (child == null)\n                return null\n            // Number of child nodes = 1, delete node directly\n            else\n                node = child\n        } else {\n            // Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n            var temp = node.right\n            while (temp!!.left != null) {\n                temp = temp.left\n            }\n            node.right = removeHelper(node.right, temp._val)\n            node._val = temp._val\n        }\n    }\n    updateHeight(node) // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = rotate(node)\n    // Return root node of subtree\n    return node\n}\n
      avl_tree.rb
      ### Delete node ###\ndef remove(val)\n  @root = remove_helper(@root, val)\nend\n\n### Recursively delete node (helper method) ###\ndef remove_helper(node, val)\n  return if node.nil?\n  # 1. Find node and delete\n  if val < node.val\n    node.left = remove_helper(node.left, val)\n  elsif val > node.val\n    node.right = remove_helper(node.right, val)\n  else\n    if node.left.nil? || node.right.nil?\n      child = node.left || node.right\n      # Number of child nodes = 0, delete node directly and return\n      return if child.nil?\n      # Number of child nodes = 1, delete node directly\n      node = child\n    else\n      # Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n      temp = node.right\n      while !temp.left.nil?\n        temp = temp.left\n      end\n      node.right = remove_helper(node.right, temp.val)\n      node.val = temp.val\n    end\n  end\n  # Update node height\n  update_height(node)\n  # 2. Perform rotation operation to restore balance to this subtree\n  rotate(node)\nend\n
      "},{"location":"chapter_tree/avl_tree/#3-node-search","title":"3. \u00a0 Node Search","text":"

      The node search operation in AVL trees is consistent with that in binary search trees, and will not be elaborated here.

      "},{"location":"chapter_tree/avl_tree/#754-typical-applications-of-avl-trees","title":"7.5.4 \u00a0 Typical Applications of Avl Trees","text":"
      • Organizing and storing large-scale data, suitable for scenarios with high-frequency searches and low-frequency insertions and deletions.
      • Used to build index systems in databases.
      • Red-black trees are also a common type of balanced binary search tree. Compared to AVL trees, red-black trees have more relaxed balance conditions, require fewer rotation operations for node insertion and deletion, and have higher average efficiency for node addition and deletion operations.
      "},{"location":"chapter_tree/binary_search_tree/","title":"7.4 \u00a0 Binary Search Tree","text":"

      As shown in Figure 7-16, a binary search tree satisfies the following conditions.

      1. For the root node, the value of all nodes in the left subtree \\(<\\) the value of the root node \\(<\\) the value of all nodes in the right subtree.
      2. The left and right subtrees of any node are also binary search trees, i.e., they satisfy condition 1. as well.

      Figure 7-16 \u00a0 Binary search tree

      "},{"location":"chapter_tree/binary_search_tree/#741-operations-on-a-binary-search-tree","title":"7.4.1 \u00a0 Operations on a Binary Search Tree","text":"

      We encapsulate the binary search tree as a class BinarySearchTree and declare a member variable root pointing to the tree's root node.

      "},{"location":"chapter_tree/binary_search_tree/#1-searching-for-a-node","title":"1. \u00a0 Searching for a Node","text":"

      Given a target node value num, we can search according to the properties of the binary search tree. As shown in Figure 7-17, we declare a node cur and start from the binary tree's root node root, looping to compare the node value cur.val with num.

      • If cur.val < num, it means the target node is in cur's right subtree, thus execute cur = cur.right.
      • If cur.val > num, it means the target node is in cur's left subtree, thus execute cur = cur.left.
      • If cur.val = num, it means the target node is found, exit the loop, and return the node.
      <1><2><3><4>

      Figure 7-17 \u00a0 Example of searching for a node in a binary search tree

      The search operation in a binary search tree works on the same principle as the binary search algorithm, both eliminating half of the cases in each round. The number of loop iterations is at most the height of the binary tree. When the binary tree is balanced, it uses \\(O(\\log n)\\) time. The example code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_tree.py
      def search(self, num: int) -> TreeNode | None:\n    \"\"\"Search node\"\"\"\n    cur = self._root\n    # Loop search, exit after passing leaf node\n    while cur is not None:\n        # Target node is in cur's right subtree\n        if cur.val < num:\n            cur = cur.right\n        # Target node is in cur's left subtree\n        elif cur.val > num:\n            cur = cur.left\n        # Found target node, exit loop\n        else:\n            break\n    return cur\n
      binary_search_tree.cpp
      /* Search node */\nTreeNode *search(int num) {\n    TreeNode *cur = root;\n    // Loop search, exit after passing leaf node\n    while (cur != nullptr) {\n        // Target node is in cur's right subtree\n        if (cur->val < num)\n            cur = cur->right;\n        // Target node is in cur's left subtree\n        else if (cur->val > num)\n            cur = cur->left;\n        // Found target node, exit loop\n        else\n            break;\n    }\n    // Return target node\n    return cur;\n}\n
      binary_search_tree.java
      /* Search node */\nTreeNode search(int num) {\n    TreeNode cur = root;\n    // Loop search, exit after passing leaf node\n    while (cur != null) {\n        // Target node is in cur's right subtree\n        if (cur.val < num)\n            cur = cur.right;\n        // Target node is in cur's left subtree\n        else if (cur.val > num)\n            cur = cur.left;\n        // Found target node, exit loop\n        else\n            break;\n    }\n    // Return target node\n    return cur;\n}\n
      binary_search_tree.cs
      /* Search node */\nTreeNode? Search(int num) {\n    TreeNode? cur = root;\n    // Loop search, exit after passing leaf node\n    while (cur != null) {\n        // Target node is in cur's right subtree\n        if (cur.val < num) cur =\n            cur.right;\n        // Target node is in cur's left subtree\n        else if (cur.val > num)\n            cur = cur.left;\n        // Found target node, exit loop\n        else\n            break;\n    }\n    // Return target node\n    return cur;\n}\n
      binary_search_tree.go
      /* Search node */\nfunc (bst *binarySearchTree) search(num int) *TreeNode {\n    node := bst.root\n    // Loop search, exit after passing leaf node\n    for node != nil {\n        if node.Val.(int) < num {\n            // Target node is in cur's right subtree\n            node = node.Right\n        } else if node.Val.(int) > num {\n            // Target node is in cur's left subtree\n            node = node.Left\n        } else {\n            // Found target node, exit loop\n            break\n        }\n    }\n    // Return target node\n    return node\n}\n
      binary_search_tree.swift
      /* Search node */\nfunc search(num: Int) -> TreeNode? {\n    var cur = root\n    // Loop search, exit after passing leaf node\n    while cur != nil {\n        // Target node is in cur's right subtree\n        if cur!.val < num {\n            cur = cur?.right\n        }\n        // Target node is in cur's left subtree\n        else if cur!.val > num {\n            cur = cur?.left\n        }\n        // Found target node, exit loop\n        else {\n            break\n        }\n    }\n    // Return target node\n    return cur\n}\n
      binary_search_tree.js
      /* Search node */\nsearch(num) {\n    let cur = this.root;\n    // Loop search, exit after passing leaf node\n    while (cur !== null) {\n        // Target node is in cur's right subtree\n        if (cur.val < num) cur = cur.right;\n        // Target node is in cur's left subtree\n        else if (cur.val > num) cur = cur.left;\n        // Found target node, exit loop\n        else break;\n    }\n    // Return target node\n    return cur;\n}\n
      binary_search_tree.ts
      /* Search node */\nsearch(num: number): TreeNode | null {\n    let cur = this.root;\n    // Loop search, exit after passing leaf node\n    while (cur !== null) {\n        // Target node is in cur's right subtree\n        if (cur.val < num) cur = cur.right;\n        // Target node is in cur's left subtree\n        else if (cur.val > num) cur = cur.left;\n        // Found target node, exit loop\n        else break;\n    }\n    // Return target node\n    return cur;\n}\n
      binary_search_tree.dart
      /* Search node */\nTreeNode? search(int _num) {\n  TreeNode? cur = _root;\n  // Loop search, exit after passing leaf node\n  while (cur != null) {\n    // Target node is in cur's right subtree\n    if (cur.val < _num)\n      cur = cur.right;\n    // Target node is in cur's left subtree\n    else if (cur.val > _num)\n      cur = cur.left;\n    // Found target node, exit loop\n    else\n      break;\n  }\n  // Return target node\n  return cur;\n}\n
      binary_search_tree.rs
      /* Search node */\npub fn search(&self, num: i32) -> OptionTreeNodeRc {\n    let mut cur = self.root.clone();\n    // Loop search, exit after passing leaf node\n    while let Some(node) = cur.clone() {\n        match num.cmp(&node.borrow().val) {\n            // Target node is in cur's right subtree\n            Ordering::Greater => cur = node.borrow().right.clone(),\n            // Target node is in cur's left subtree\n            Ordering::Less => cur = node.borrow().left.clone(),\n            // Found target node, exit loop\n            Ordering::Equal => break,\n        }\n    }\n\n    // Return target node\n    cur\n}\n
      binary_search_tree.c
      /* Search node */\nTreeNode *search(BinarySearchTree *bst, int num) {\n    TreeNode *cur = bst->root;\n    // Loop search, exit after passing leaf node\n    while (cur != NULL) {\n        if (cur->val < num) {\n            // Target node is in cur's right subtree\n            cur = cur->right;\n        } else if (cur->val > num) {\n            // Target node is in cur's left subtree\n            cur = cur->left;\n        } else {\n            // Found target node, exit loop\n            break;\n        }\n    }\n    // Return target node\n    return cur;\n}\n
      binary_search_tree.kt
      /* Search node */\nfun search(num: Int): TreeNode? {\n    var cur = root\n    // Loop search, exit after passing leaf node\n    while (cur != null) {\n        // Target node is in cur's right subtree\n        cur = if (cur._val < num)\n            cur.right\n        // Target node is in cur's left subtree\n        else if (cur._val > num)\n            cur.left\n        // Found target node, exit loop\n        else\n            break\n    }\n    // Return target node\n    return cur\n}\n
      binary_search_tree.rb
      ### Search node ###\ndef search(num)\n  cur = @root\n\n  # Loop search, exit after passing leaf node\n  while !cur.nil?\n    # Target node is in cur's right subtree\n    if cur.val < num\n      cur = cur.right\n    # Target node is in cur's left subtree\n    elsif cur.val > num\n      cur = cur.left\n    # Found target node, exit loop\n    else\n      break\n    end\n  end\n\n  cur\nend\n
      "},{"location":"chapter_tree/binary_search_tree/#2-inserting-a-node","title":"2. \u00a0 Inserting a Node","text":"

      Given an element num to be inserted, in order to maintain the property of the binary search tree \"left subtree < root node < right subtree,\" the insertion process is as shown in Figure 7-18.

      1. Finding the insertion position: Similar to the search operation, start from the root node and loop downward searching according to the size relationship between the current node value and num, until passing the leaf node (traversing to None) and then exit the loop.
      2. Insert the node at that position: Initialize node num and place it at the None position.

      Figure 7-18 \u00a0 Inserting a node into a binary search tree

      In the code implementation, note the following two points:

      • Binary search trees do not allow duplicate nodes; otherwise, it would violate its definition. Therefore, if the node to be inserted already exists in the tree, the insertion is not performed and it returns directly.
      • To implement the node insertion, we need to use node pre to save the node from the previous loop iteration. This way, when traversing to None, we can obtain its parent node, thereby completing the node insertion operation.
      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_tree.py
      def insert(self, num: int):\n    \"\"\"Insert node\"\"\"\n    # If tree is empty, initialize root node\n    if self._root is None:\n        self._root = TreeNode(num)\n        return\n    # Loop search, exit after passing leaf node\n    cur, pre = self._root, None\n    while cur is not None:\n        # Found duplicate node, return directly\n        if cur.val == num:\n            return\n        pre = cur\n        # Insertion position is in cur's right subtree\n        if cur.val < num:\n            cur = cur.right\n        # Insertion position is in cur's left subtree\n        else:\n            cur = cur.left\n    # Insert node\n    node = TreeNode(num)\n    if pre.val < num:\n        pre.right = node\n    else:\n        pre.left = node\n
      binary_search_tree.cpp
      /* Insert node */\nvoid insert(int num) {\n    // If tree is empty, initialize root node\n    if (root == nullptr) {\n        root = new TreeNode(num);\n        return;\n    }\n    TreeNode *cur = root, *pre = nullptr;\n    // Loop search, exit after passing leaf node\n    while (cur != nullptr) {\n        // Found duplicate node, return directly\n        if (cur->val == num)\n            return;\n        pre = cur;\n        // Insertion position is in cur's right subtree\n        if (cur->val < num)\n            cur = cur->right;\n        // Insertion position is in cur's left subtree\n        else\n            cur = cur->left;\n    }\n    // Insert node\n    TreeNode *node = new TreeNode(num);\n    if (pre->val < num)\n        pre->right = node;\n    else\n        pre->left = node;\n}\n
      binary_search_tree.java
      /* Insert node */\nvoid insert(int num) {\n    // If tree is empty, initialize root node\n    if (root == null) {\n        root = new TreeNode(num);\n        return;\n    }\n    TreeNode cur = root, pre = null;\n    // Loop search, exit after passing leaf node\n    while (cur != null) {\n        // Found duplicate node, return directly\n        if (cur.val == num)\n            return;\n        pre = cur;\n        // Insertion position is in cur's right subtree\n        if (cur.val < num)\n            cur = cur.right;\n        // Insertion position is in cur's left subtree\n        else\n            cur = cur.left;\n    }\n    // Insert node\n    TreeNode node = new TreeNode(num);\n    if (pre.val < num)\n        pre.right = node;\n    else\n        pre.left = node;\n}\n
      binary_search_tree.cs
      /* Insert node */\nvoid Insert(int num) {\n    // If tree is empty, initialize root node\n    if (root == null) {\n        root = new TreeNode(num);\n        return;\n    }\n    TreeNode? cur = root, pre = null;\n    // Loop search, exit after passing leaf node\n    while (cur != null) {\n        // Found duplicate node, return directly\n        if (cur.val == num)\n            return;\n        pre = cur;\n        // Insertion position is in cur's right subtree\n        if (cur.val < num)\n            cur = cur.right;\n        // Insertion position is in cur's left subtree\n        else\n            cur = cur.left;\n    }\n\n    // Insert node\n    TreeNode node = new(num);\n    if (pre != null) {\n        if (pre.val < num)\n            pre.right = node;\n        else\n            pre.left = node;\n    }\n}\n
      binary_search_tree.go
      /* Insert node */\nfunc (bst *binarySearchTree) insert(num int) {\n    cur := bst.root\n    // If tree is empty, initialize root node\n    if cur == nil {\n        bst.root = NewTreeNode(num)\n        return\n    }\n    // Node position before the node to be inserted\n    var pre *TreeNode = nil\n    // Loop search, exit after passing leaf node\n    for cur != nil {\n        if cur.Val == num {\n            return\n        }\n        pre = cur\n        if cur.Val.(int) < num {\n            cur = cur.Right\n        } else {\n            cur = cur.Left\n        }\n    }\n    // Insert node\n    node := NewTreeNode(num)\n    if pre.Val.(int) < num {\n        pre.Right = node\n    } else {\n        pre.Left = node\n    }\n}\n
      binary_search_tree.swift
      /* Insert node */\nfunc insert(num: Int) {\n    // If tree is empty, initialize root node\n    if root == nil {\n        root = TreeNode(x: num)\n        return\n    }\n    var cur = root\n    var pre: TreeNode?\n    // Loop search, exit after passing leaf node\n    while cur != nil {\n        // Found duplicate node, return directly\n        if cur!.val == num {\n            return\n        }\n        pre = cur\n        // Insertion position is in cur's right subtree\n        if cur!.val < num {\n            cur = cur?.right\n        }\n        // Insertion position is in cur's left subtree\n        else {\n            cur = cur?.left\n        }\n    }\n    // Insert node\n    let node = TreeNode(x: num)\n    if pre!.val < num {\n        pre?.right = node\n    } else {\n        pre?.left = node\n    }\n}\n
      binary_search_tree.js
      /* Insert node */\ninsert(num) {\n    // If tree is empty, initialize root node\n    if (this.root === null) {\n        this.root = new TreeNode(num);\n        return;\n    }\n    let cur = this.root,\n        pre = null;\n    // Loop search, exit after passing leaf node\n    while (cur !== null) {\n        // Found duplicate node, return directly\n        if (cur.val === num) return;\n        pre = cur;\n        // Insertion position is in cur's right subtree\n        if (cur.val < num) cur = cur.right;\n        // Insertion position is in cur's left subtree\n        else cur = cur.left;\n    }\n    // Insert node\n    const node = new TreeNode(num);\n    if (pre.val < num) pre.right = node;\n    else pre.left = node;\n}\n
      binary_search_tree.ts
      /* Insert node */\ninsert(num: number): void {\n    // If tree is empty, initialize root node\n    if (this.root === null) {\n        this.root = new TreeNode(num);\n        return;\n    }\n    let cur: TreeNode | null = this.root,\n        pre: TreeNode | null = null;\n    // Loop search, exit after passing leaf node\n    while (cur !== null) {\n        // Found duplicate node, return directly\n        if (cur.val === num) return;\n        pre = cur;\n        // Insertion position is in cur's right subtree\n        if (cur.val < num) cur = cur.right;\n        // Insertion position is in cur's left subtree\n        else cur = cur.left;\n    }\n    // Insert node\n    const node = new TreeNode(num);\n    if (pre!.val < num) pre!.right = node;\n    else pre!.left = node;\n}\n
      binary_search_tree.dart
      /* Insert node */\nvoid insert(int _num) {\n  // If tree is empty, initialize root node\n  if (_root == null) {\n    _root = TreeNode(_num);\n    return;\n  }\n  TreeNode? cur = _root;\n  TreeNode? pre = null;\n  // Loop search, exit after passing leaf node\n  while (cur != null) {\n    // Found duplicate node, return directly\n    if (cur.val == _num) return;\n    pre = cur;\n    // Insertion position is in cur's right subtree\n    if (cur.val < _num)\n      cur = cur.right;\n    // Insertion position is in cur's left subtree\n    else\n      cur = cur.left;\n  }\n  // Insert node\n  TreeNode? node = TreeNode(_num);\n  if (pre!.val < _num)\n    pre.right = node;\n  else\n    pre.left = node;\n}\n
      binary_search_tree.rs
      /* Insert node */\npub fn insert(&mut self, num: i32) {\n    // If tree is empty, initialize root node\n    if self.root.is_none() {\n        self.root = Some(TreeNode::new(num));\n        return;\n    }\n    let mut cur = self.root.clone();\n    let mut pre = None;\n    // Loop search, exit after passing leaf node\n    while let Some(node) = cur.clone() {\n        match num.cmp(&node.borrow().val) {\n            // Found duplicate node, return directly\n            Ordering::Equal => return,\n            // Insertion position is in cur's right subtree\n            Ordering::Greater => {\n                pre = cur.clone();\n                cur = node.borrow().right.clone();\n            }\n            // Insertion position is in cur's left subtree\n            Ordering::Less => {\n                pre = cur.clone();\n                cur = node.borrow().left.clone();\n            }\n        }\n    }\n    // Insert node\n    let pre = pre.unwrap();\n    let node = Some(TreeNode::new(num));\n    if num > pre.borrow().val {\n        pre.borrow_mut().right = node;\n    } else {\n        pre.borrow_mut().left = node;\n    }\n}\n
      binary_search_tree.c
      /* Insert node */\nvoid insert(BinarySearchTree *bst, int num) {\n    // If tree is empty, initialize root node\n    if (bst->root == NULL) {\n        bst->root = newTreeNode(num);\n        return;\n    }\n    TreeNode *cur = bst->root, *pre = NULL;\n    // Loop search, exit after passing leaf node\n    while (cur != NULL) {\n        // Found duplicate node, return directly\n        if (cur->val == num) {\n            return;\n        }\n        pre = cur;\n        if (cur->val < num) {\n            // Insertion position is in cur's right subtree\n            cur = cur->right;\n        } else {\n            // Insertion position is in cur's left subtree\n            cur = cur->left;\n        }\n    }\n    // Insert node\n    TreeNode *node = newTreeNode(num);\n    if (pre->val < num) {\n        pre->right = node;\n    } else {\n        pre->left = node;\n    }\n}\n
      binary_search_tree.kt
      /* Insert node */\nfun insert(num: Int) {\n    // If tree is empty, initialize root node\n    if (root == null) {\n        root = TreeNode(num)\n        return\n    }\n    var cur = root\n    var pre: TreeNode? = null\n    // Loop search, exit after passing leaf node\n    while (cur != null) {\n        // Found duplicate node, return directly\n        if (cur._val == num)\n            return\n        pre = cur\n        // Insertion position is in cur's right subtree\n        cur = if (cur._val < num)\n            cur.right\n        // Insertion position is in cur's left subtree\n        else\n            cur.left\n    }\n    // Insert node\n    val node = TreeNode(num)\n    if (pre?._val!! < num)\n        pre.right = node\n    else\n        pre.left = node\n}\n
      binary_search_tree.rb
      ### Insert node ###\ndef insert(num)\n  # If tree is empty, initialize root node\n  if @root.nil?\n    @root = TreeNode.new(num)\n    return\n  end\n\n  # Loop search, exit after passing leaf node\n  cur, pre = @root, nil\n  while !cur.nil?\n    # Found duplicate node, return directly\n    return if cur.val == num\n\n    pre = cur\n    # Insertion position is in cur's right subtree\n    if cur.val < num\n      cur = cur.right\n    # Insertion position is in cur's left subtree\n    else\n      cur = cur.left\n    end\n  end\n\n  # Insert node\n  node = TreeNode.new(num)\n  if pre.val < num\n    pre.right = node\n  else\n    pre.left = node\n  end\nend\n

      Similar to searching for a node, inserting a node uses \\(O(\\log n)\\) time.

      "},{"location":"chapter_tree/binary_search_tree/#3-removing-a-node","title":"3. \u00a0 Removing a Node","text":"

      First, find the target node in the binary tree, then remove it. Similar to node insertion, we need to ensure that after the removal operation is completed, the binary search tree's property of \"left subtree \\(<\\) root node \\(<\\) right subtree\" is still maintained. Therefore, depending on the number of child nodes the target node has, we divide it into 0, 1, and 2 three cases, and execute the corresponding node removal operations.

      As shown in Figure 7-19, when the degree of the node to be removed is \\(0\\), it means the node is a leaf node and can be directly removed.

      Figure 7-19 \u00a0 Removing a node in a binary search tree (degree 0)

      As shown in Figure 7-20, when the degree of the node to be removed is \\(1\\), replacing the node to be removed with its child node is sufficient.

      Figure 7-20 \u00a0 Removing a node in a binary search tree (degree 1)

      When the degree of the node to be removed is \\(2\\), we cannot directly remove it; instead, we need to use a node to replace it. To maintain the binary search tree's property of \"left subtree \\(<\\) root node \\(<\\) right subtree,\" this node can be either the smallest node in the right subtree or the largest node in the left subtree.

      Assuming we choose the smallest node in the right subtree (the next node in the inorder traversal), the removal process is as shown in Figure 7-21.

      1. Find the next node of the node to be removed in the \"inorder traversal sequence,\" denoted as tmp.
      2. Replace the value of the node to be removed with the value of tmp, and recursively remove node tmp in the tree.
      <1><2><3><4>

      Figure 7-21 \u00a0 Removing a node in a binary search tree (degree 2)

      The node removal operation also uses \\(O(\\log n)\\) time, where finding the node to be removed requires \\(O(\\log n)\\) time, and obtaining the inorder successor node requires \\(O(\\log n)\\) time. Example code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_tree.py
      def remove(self, num: int):\n    \"\"\"Delete node\"\"\"\n    # If tree is empty, return directly\n    if self._root is None:\n        return\n    # Loop search, exit after passing leaf node\n    cur, pre = self._root, None\n    while cur is not None:\n        # Found node to delete, exit loop\n        if cur.val == num:\n            break\n        pre = cur\n        # Node to delete is in cur's right subtree\n        if cur.val < num:\n            cur = cur.right\n        # Node to delete is in cur's left subtree\n        else:\n            cur = cur.left\n    # If no node to delete, return directly\n    if cur is None:\n        return\n\n    # Number of child nodes = 0 or 1\n    if cur.left is None or cur.right is None:\n        # When number of child nodes = 0 / 1, child = null / that child node\n        child = cur.left or cur.right\n        # Delete node cur\n        if cur != self._root:\n            if pre.left == cur:\n                pre.left = child\n            else:\n                pre.right = child\n        else:\n            # If deleted node is root node, reassign root node\n            self._root = child\n    # Number of child nodes = 2\n    else:\n        # Get next node of cur in inorder traversal\n        tmp: TreeNode = cur.right\n        while tmp.left is not None:\n            tmp = tmp.left\n        # Recursively delete node tmp\n        self.remove(tmp.val)\n        # Replace cur with tmp\n        cur.val = tmp.val\n
      binary_search_tree.cpp
      /* Remove node */\nvoid remove(int num) {\n    // If tree is empty, return directly\n    if (root == nullptr)\n        return;\n    TreeNode *cur = root, *pre = nullptr;\n    // Loop search, exit after passing leaf node\n    while (cur != nullptr) {\n        // Found node to delete, exit loop\n        if (cur->val == num)\n            break;\n        pre = cur;\n        // Node to delete is in cur's right subtree\n        if (cur->val < num)\n            cur = cur->right;\n        // Node to delete is in cur's left subtree\n        else\n            cur = cur->left;\n    }\n    // If no node to delete, return directly\n    if (cur == nullptr)\n        return;\n    // Number of child nodes = 0 or 1\n    if (cur->left == nullptr || cur->right == nullptr) {\n        // When number of child nodes = 0 / 1, child = nullptr / that child node\n        TreeNode *child = cur->left != nullptr ? cur->left : cur->right;\n        // Delete node cur\n        if (cur != root) {\n            if (pre->left == cur)\n                pre->left = child;\n            else\n                pre->right = child;\n        } else {\n            // If deleted node is root node, reassign root node\n            root = child;\n        }\n        // Free memory\n        delete cur;\n    }\n    // Number of child nodes = 2\n    else {\n        // Get next node of cur in inorder traversal\n        TreeNode *tmp = cur->right;\n        while (tmp->left != nullptr) {\n            tmp = tmp->left;\n        }\n        int tmpVal = tmp->val;\n        // Recursively delete node tmp\n        remove(tmp->val);\n        // Replace cur with tmp\n        cur->val = tmpVal;\n    }\n}\n
      binary_search_tree.java
      /* Remove node */\nvoid remove(int num) {\n    // If tree is empty, return directly\n    if (root == null)\n        return;\n    TreeNode cur = root, pre = null;\n    // Loop search, exit after passing leaf node\n    while (cur != null) {\n        // Found node to delete, exit loop\n        if (cur.val == num)\n            break;\n        pre = cur;\n        // Node to delete is in cur's right subtree\n        if (cur.val < num)\n            cur = cur.right;\n        // Node to delete is in cur's left subtree\n        else\n            cur = cur.left;\n    }\n    // If no node to delete, return directly\n    if (cur == null)\n        return;\n    // Number of child nodes = 0 or 1\n    if (cur.left == null || cur.right == null) {\n        // When number of child nodes = 0 / 1, child = null / that child node\n        TreeNode child = cur.left != null ? cur.left : cur.right;\n        // Delete node cur\n        if (cur != root) {\n            if (pre.left == cur)\n                pre.left = child;\n            else\n                pre.right = child;\n        } else {\n            // If deleted node is root node, reassign root node\n            root = child;\n        }\n    }\n    // Number of child nodes = 2\n    else {\n        // Get next node of cur in inorder traversal\n        TreeNode tmp = cur.right;\n        while (tmp.left != null) {\n            tmp = tmp.left;\n        }\n        // Recursively delete node tmp\n        remove(tmp.val);\n        // Replace cur with tmp\n        cur.val = tmp.val;\n    }\n}\n
      binary_search_tree.cs
      /* Remove node */\nvoid Remove(int num) {\n    // If tree is empty, return directly\n    if (root == null)\n        return;\n    TreeNode? cur = root, pre = null;\n    // Loop search, exit after passing leaf node\n    while (cur != null) {\n        // Found node to delete, exit loop\n        if (cur.val == num)\n            break;\n        pre = cur;\n        // Node to delete is in cur's right subtree\n        if (cur.val < num)\n            cur = cur.right;\n        // Node to delete is in cur's left subtree\n        else\n            cur = cur.left;\n    }\n    // If no node to delete, return directly\n    if (cur == null)\n        return;\n    // Number of child nodes = 0 or 1\n    if (cur.left == null || cur.right == null) {\n        // When number of child nodes = 0 / 1, child = null / that child node\n        TreeNode? child = cur.left ?? cur.right;\n        // Delete node cur\n        if (cur != root) {\n            if (pre!.left == cur)\n                pre.left = child;\n            else\n                pre.right = child;\n        } else {\n            // If deleted node is root node, reassign root node\n            root = child;\n        }\n    }\n    // Number of child nodes = 2\n    else {\n        // Get next node of cur in inorder traversal\n        TreeNode? tmp = cur.right;\n        while (tmp.left != null) {\n            tmp = tmp.left;\n        }\n        // Recursively delete node tmp\n        Remove(tmp.val!.Value);\n        // Replace cur with tmp\n        cur.val = tmp.val;\n    }\n}\n
      binary_search_tree.go
      /* Remove node */\nfunc (bst *binarySearchTree) remove(num int) {\n    cur := bst.root\n    // If tree is empty, return directly\n    if cur == nil {\n        return\n    }\n    // Node position before the node to be removed\n    var pre *TreeNode = nil\n    // Loop search, exit after passing leaf node\n    for cur != nil {\n        if cur.Val == num {\n            break\n        }\n        pre = cur\n        if cur.Val.(int) < num {\n            // Node to be removed is in right subtree\n            cur = cur.Right\n        } else {\n            // Node to be removed is in left subtree\n            cur = cur.Left\n        }\n    }\n    // If no node to delete, return directly\n    if cur == nil {\n        return\n    }\n    // Number of child nodes is 0 or 1\n    if cur.Left == nil || cur.Right == nil {\n        var child *TreeNode = nil\n        // Get child node of node to be removed\n        if cur.Left != nil {\n            child = cur.Left\n        } else {\n            child = cur.Right\n        }\n        // Delete node cur\n        if cur != bst.root {\n            if pre.Left == cur {\n                pre.Left = child\n            } else {\n                pre.Right = child\n            }\n        } else {\n            // If deleted node is root node, reassign root node\n            bst.root = child\n        }\n        // Number of child nodes is 2\n    } else {\n        // Get next node of node cur to be removed in in-order traversal\n        tmp := cur.Right\n        for tmp.Left != nil {\n            tmp = tmp.Left\n        }\n        // Recursively delete node tmp\n        bst.remove(tmp.Val.(int))\n        // Replace cur with tmp\n        cur.Val = tmp.Val\n    }\n}\n
      binary_search_tree.swift
      /* Remove node */\nfunc remove(num: Int) {\n    // If tree is empty, return directly\n    if root == nil {\n        return\n    }\n    var cur = root\n    var pre: TreeNode?\n    // Loop search, exit after passing leaf node\n    while cur != nil {\n        // Found node to delete, exit loop\n        if cur!.val == num {\n            break\n        }\n        pre = cur\n        // Node to delete is in cur's right subtree\n        if cur!.val < num {\n            cur = cur?.right\n        }\n        // Node to delete is in cur's left subtree\n        else {\n            cur = cur?.left\n        }\n    }\n    // If no node to delete, return directly\n    if cur == nil {\n        return\n    }\n    // Number of child nodes = 0 or 1\n    if cur?.left == nil || cur?.right == nil {\n        // When number of child nodes = 0 / 1, child = null / that child node\n        let child = cur?.left ?? cur?.right\n        // Delete node cur\n        if cur !== root {\n            if pre?.left === cur {\n                pre?.left = child\n            } else {\n                pre?.right = child\n            }\n        } else {\n            // If deleted node is root node, reassign root node\n            root = child\n        }\n    }\n    // Number of child nodes = 2\n    else {\n        // Get next node of cur in inorder traversal\n        var tmp = cur?.right\n        while tmp?.left != nil {\n            tmp = tmp?.left\n        }\n        // Recursively delete node tmp\n        remove(num: tmp!.val)\n        // Replace cur with tmp\n        cur?.val = tmp!.val\n    }\n}\n
      binary_search_tree.js
      /* Remove node */\nremove(num) {\n    // If tree is empty, return directly\n    if (this.root === null) return;\n    let cur = this.root,\n        pre = null;\n    // Loop search, exit after passing leaf node\n    while (cur !== null) {\n        // Found node to delete, exit loop\n        if (cur.val === num) break;\n        pre = cur;\n        // Node to delete is in cur's right subtree\n        if (cur.val < num) cur = cur.right;\n        // Node to delete is in cur's left subtree\n        else cur = cur.left;\n    }\n    // If no node to delete, return directly\n    if (cur === null) return;\n    // Number of child nodes = 0 or 1\n    if (cur.left === null || cur.right === null) {\n        // When number of child nodes = 0 / 1, child = null / that child node\n        const child = cur.left !== null ? cur.left : cur.right;\n        // Delete node cur\n        if (cur !== this.root) {\n            if (pre.left === cur) pre.left = child;\n            else pre.right = child;\n        } else {\n            // If deleted node is root node, reassign root node\n            this.root = child;\n        }\n    }\n    // Number of child nodes = 2\n    else {\n        // Get next node of cur in inorder traversal\n        let tmp = cur.right;\n        while (tmp.left !== null) {\n            tmp = tmp.left;\n        }\n        // Recursively delete node tmp\n        this.remove(tmp.val);\n        // Replace cur with tmp\n        cur.val = tmp.val;\n    }\n}\n
      binary_search_tree.ts
      /* Remove node */\nremove(num: number): void {\n    // If tree is empty, return directly\n    if (this.root === null) return;\n    let cur: TreeNode | null = this.root,\n        pre: TreeNode | null = null;\n    // Loop search, exit after passing leaf node\n    while (cur !== null) {\n        // Found node to delete, exit loop\n        if (cur.val === num) break;\n        pre = cur;\n        // Node to delete is in cur's right subtree\n        if (cur.val < num) cur = cur.right;\n        // Node to delete is in cur's left subtree\n        else cur = cur.left;\n    }\n    // If no node to delete, return directly\n    if (cur === null) return;\n    // Number of child nodes = 0 or 1\n    if (cur.left === null || cur.right === null) {\n        // When number of child nodes = 0 / 1, child = null / that child node\n        const child: TreeNode | null =\n            cur.left !== null ? cur.left : cur.right;\n        // Delete node cur\n        if (cur !== this.root) {\n            if (pre!.left === cur) pre!.left = child;\n            else pre!.right = child;\n        } else {\n            // If deleted node is root node, reassign root node\n            this.root = child;\n        }\n    }\n    // Number of child nodes = 2\n    else {\n        // Get next node of cur in inorder traversal\n        let tmp: TreeNode | null = cur.right;\n        while (tmp!.left !== null) {\n            tmp = tmp!.left;\n        }\n        // Recursively delete node tmp\n        this.remove(tmp!.val);\n        // Replace cur with tmp\n        cur.val = tmp!.val;\n    }\n}\n
      binary_search_tree.dart
      /* Remove node */\nvoid remove(int _num) {\n  // If tree is empty, return directly\n  if (_root == null) return;\n  TreeNode? cur = _root;\n  TreeNode? pre = null;\n  // Loop search, exit after passing leaf node\n  while (cur != null) {\n    // Found node to delete, exit loop\n    if (cur.val == _num) break;\n    pre = cur;\n    // Node to delete is in cur's right subtree\n    if (cur.val < _num)\n      cur = cur.right;\n    // Node to delete is in cur's left subtree\n    else\n      cur = cur.left;\n  }\n  // If no node to delete, return directly\n  if (cur == null) return;\n  // Number of child nodes = 0 or 1\n  if (cur.left == null || cur.right == null) {\n    // When number of child nodes = 0 / 1, child = null / that child node\n    TreeNode? child = cur.left ?? cur.right;\n    // Delete node cur\n    if (cur != _root) {\n      if (pre!.left == cur)\n        pre.left = child;\n      else\n        pre.right = child;\n    } else {\n      // If deleted node is root node, reassign root node\n      _root = child;\n    }\n  } else {\n    // Number of child nodes = 2\n    // Get next node of cur in inorder traversal\n    TreeNode? tmp = cur.right;\n    while (tmp!.left != null) {\n      tmp = tmp.left;\n    }\n    // Recursively delete node tmp\n    remove(tmp.val);\n    // Replace cur with tmp\n    cur.val = tmp.val;\n  }\n}\n
      binary_search_tree.rs
      /* Remove node */\npub fn remove(&mut self, num: i32) {\n    // If tree is empty, return directly\n    if self.root.is_none() {\n        return;\n    }\n    let mut cur = self.root.clone();\n    let mut pre = None;\n    // Loop search, exit after passing leaf node\n    while let Some(node) = cur.clone() {\n        match num.cmp(&node.borrow().val) {\n            // Found node to delete, exit loop\n            Ordering::Equal => break,\n            // Node to delete is in cur's right subtree\n            Ordering::Greater => {\n                pre = cur.clone();\n                cur = node.borrow().right.clone();\n            }\n            // Node to delete is in cur's left subtree\n            Ordering::Less => {\n                pre = cur.clone();\n                cur = node.borrow().left.clone();\n            }\n        }\n    }\n    // If no node to delete, return directly\n    if cur.is_none() {\n        return;\n    }\n    let cur = cur.unwrap();\n    let (left_child, right_child) = (cur.borrow().left.clone(), cur.borrow().right.clone());\n    match (left_child.clone(), right_child.clone()) {\n        // Number of child nodes = 0 or 1\n        (None, None) | (Some(_), None) | (None, Some(_)) => {\n            // When number of child nodes = 0 / 1, child = nullptr / that child node\n            let child = left_child.or(right_child);\n            let pre = pre.unwrap();\n            // Delete node cur\n            if !Rc::ptr_eq(&cur, self.root.as_ref().unwrap()) {\n                let left = pre.borrow().left.clone();\n                if left.is_some() && Rc::ptr_eq(left.as_ref().unwrap(), &cur) {\n                    pre.borrow_mut().left = child;\n                } else {\n                    pre.borrow_mut().right = child;\n                }\n            } else {\n                // If deleted node is root node, reassign root node\n                self.root = child;\n            }\n        }\n        // Number of child nodes = 2\n        (Some(_), Some(_)) => {\n            // Get next node of cur in inorder traversal\n            let mut tmp = cur.borrow().right.clone();\n            while let Some(node) = tmp.clone() {\n                if node.borrow().left.is_some() {\n                    tmp = node.borrow().left.clone();\n                } else {\n                    break;\n                }\n            }\n            let tmp_val = tmp.unwrap().borrow().val;\n            // Recursively delete node tmp\n            self.remove(tmp_val);\n            // Replace cur with tmp\n            cur.borrow_mut().val = tmp_val;\n        }\n    }\n}\n
      binary_search_tree.c
      /* Remove node */\n// Cannot use remove keyword here due to stdio.h inclusion\nvoid removeItem(BinarySearchTree *bst, int num) {\n    // If tree is empty, return directly\n    if (bst->root == NULL)\n        return;\n    TreeNode *cur = bst->root, *pre = NULL;\n    // Loop search, exit after passing leaf node\n    while (cur != NULL) {\n        // Found node to delete, exit loop\n        if (cur->val == num)\n            break;\n        pre = cur;\n        if (cur->val < num) {\n            // Node to delete is in right subtree of root\n            cur = cur->right;\n        } else {\n            // Node to delete is in left subtree of root\n            cur = cur->left;\n        }\n    }\n    // If no node to delete, return directly\n    if (cur == NULL)\n        return;\n    // Check if node to delete has children\n    if (cur->left == NULL || cur->right == NULL) {\n        /* Number of child nodes = 0 or 1 */\n        // When number of child nodes = 0 / 1, child = nullptr / that child node\n        TreeNode *child = cur->left != NULL ? cur->left : cur->right;\n        // Delete node cur\n        if (pre->left == cur) {\n            pre->left = child;\n        } else {\n            pre->right = child;\n        }\n        // Free memory\n        free(cur);\n    } else {\n        /* Number of child nodes = 2 */\n        // Get next node of cur in inorder traversal\n        TreeNode *tmp = cur->right;\n        while (tmp->left != NULL) {\n            tmp = tmp->left;\n        }\n        int tmpVal = tmp->val;\n        // Recursively delete node tmp\n        removeItem(bst, tmp->val);\n        // Replace cur with tmp\n        cur->val = tmpVal;\n    }\n}\n
      binary_search_tree.kt
      /* Remove node */\nfun remove(num: Int) {\n    // If tree is empty, return directly\n    if (root == null)\n        return\n    var cur = root\n    var pre: TreeNode? = null\n    // Loop search, exit after passing leaf node\n    while (cur != null) {\n        // Found node to delete, exit loop\n        if (cur._val == num)\n            break\n        pre = cur\n        // Node to delete is in cur's right subtree\n        cur = if (cur._val < num)\n            cur.right\n        // Node to delete is in cur's left subtree\n        else\n            cur.left\n    }\n    // If no node to delete, return directly\n    if (cur == null)\n        return\n    // Number of child nodes = 0 or 1\n    if (cur.left == null || cur.right == null) {\n        // When number of child nodes = 0 / 1, child = null / that child node\n        val child = if (cur.left != null)\n            cur.left\n        else\n            cur.right\n        // Delete node cur\n        if (cur != root) {\n            if (pre!!.left == cur)\n                pre.left = child\n            else\n                pre.right = child\n        } else {\n            // If deleted node is root node, reassign root node\n            root = child\n        }\n        // Number of child nodes = 2\n    } else {\n        // Get next node of cur in inorder traversal\n        var tmp = cur.right\n        while (tmp!!.left != null) {\n            tmp = tmp.left\n        }\n        // Recursively delete node tmp\n        remove(tmp._val)\n        // Replace cur with tmp\n        cur._val = tmp._val\n    }\n}\n
      binary_search_tree.rb
      ### Delete node ###\ndef remove(num)\n  # If tree is empty, return directly\n  return if @root.nil?\n\n  # Loop search, exit after passing leaf node\n  cur, pre = @root, nil\n  while !cur.nil?\n    # Found node to delete, exit loop\n    break if cur.val == num\n\n    pre = cur\n    # Node to delete is in cur's right subtree\n    if cur.val < num\n      cur = cur.right\n    # Node to delete is in cur's left subtree\n    else\n      cur = cur.left\n    end\n  end\n  # If no node to delete, return directly\n  return if cur.nil?\n\n  # Number of child nodes = 0 or 1\n  if cur.left.nil? || cur.right.nil?\n    # When number of child nodes = 0 / 1, child = null / that child node\n    child = cur.left || cur.right\n    # Delete node cur\n    if cur != @root\n      if pre.left == cur\n        pre.left = child\n      else\n        pre.right = child\n      end\n    else\n      # If deleted node is root node, reassign root node\n      @root = child\n    end\n  # Number of child nodes = 2\n  else\n    # Get next node of cur in inorder traversal\n    tmp = cur.right\n    while !tmp.left.nil?\n      tmp = tmp.left\n    end\n    # Recursively delete node tmp\n    remove(tmp.val)\n    # Replace cur with tmp\n    cur.val = tmp.val\n  end\nend\n
      "},{"location":"chapter_tree/binary_search_tree/#4-inorder-traversal-is-ordered","title":"4. \u00a0 Inorder Traversal Is Ordered","text":"

      As shown in Figure 7-22, the inorder traversal of a binary tree follows the \"left \\(\\rightarrow\\) root \\(\\rightarrow\\) right\" traversal order, while the binary search tree satisfies the \"left child node \\(<\\) root node \\(<\\) right child node\" size relationship.

      This means that when performing an inorder traversal in a binary search tree, the next smallest node is always traversed first, thus yielding an important property: The inorder traversal sequence of a binary search tree is ascending.

      Using the property of inorder traversal being ascending, we can obtain ordered data in a binary search tree in only \\(O(n)\\) time, without the need for additional sorting operations, which is very efficient.

      Figure 7-22 \u00a0 Inorder traversal sequence of a binary search tree

      "},{"location":"chapter_tree/binary_search_tree/#742-efficiency-of-binary-search-trees","title":"7.4.2 \u00a0 Efficiency of Binary Search Trees","text":"

      Given a set of data, we consider using an array or a binary search tree for storage. Observing Table 7-2, all operations in a binary search tree have logarithmic time complexity, providing stable and efficient performance. Arrays are more efficient than binary search trees only in scenarios with high-frequency additions and low-frequency searches and deletions.

      Table 7-2 \u00a0 Efficiency comparison between arrays and search trees

      Unsorted array Binary search tree Search element \\(O(n)\\) \\(O(\\log n)\\) Insert element \\(O(1)\\) \\(O(\\log n)\\) Remove element \\(O(n)\\) \\(O(\\log n)\\)

      In the ideal case, a binary search tree is \"balanced,\" such that any node can be found within \\(\\log n\\) loop iterations.

      However, if we continuously insert and remove nodes in a binary search tree, it may degenerate into a linked list as shown in Figure 7-23, where the time complexity of various operations also degrades to \\(O(n)\\).

      Figure 7-23 \u00a0 Degradation of a binary search tree

      "},{"location":"chapter_tree/binary_search_tree/#743-common-applications-of-binary-search-trees","title":"7.4.3 \u00a0 Common Applications of Binary Search Trees","text":"
      • Used as multi-level indexes in systems to implement efficient search, insertion, and removal operations.
      • Serves as the underlying data structure for certain search algorithms.
      • Used to store data streams to maintain their ordered state.
      "},{"location":"chapter_tree/binary_tree/","title":"7.1 \u00a0 Binary Tree","text":"

      A binary tree is a non-linear data structure that represents the derivation relationship between \"ancestors\" and \"descendants\" and embodies the divide-and-conquer logic of \"one divides into two\". Similar to a linked list, the basic unit of a binary tree is a node, and each node contains a value, a reference to its left child node, and a reference to its right child node.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      class TreeNode:\n    \"\"\"Binary tree node\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val                # Node value\n        self.left: TreeNode | None = None  # Reference to left child node\n        self.right: TreeNode | None = None # Reference to right child node\n
      /* Binary tree node */\nstruct TreeNode {\n    int val;          // Node value\n    TreeNode *left;   // Pointer to left child node\n    TreeNode *right;  // Pointer to right child node\n    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n};\n
      /* Binary tree node */\nclass TreeNode {\n    int val;         // Node value\n    TreeNode left;   // Reference to left child node\n    TreeNode right;  // Reference to right child node\n    TreeNode(int x) { val = x; }\n}\n
      /* Binary tree node */\nclass TreeNode(int? x) {\n    public int? val = x;    // Node value\n    public TreeNode? left;  // Reference to left child node\n    public TreeNode? right; // Reference to right child node\n}\n
      /* Binary tree node */\ntype TreeNode struct {\n    Val   int\n    Left  *TreeNode\n    Right *TreeNode\n}\n/* Constructor */\nfunc NewTreeNode(v int) *TreeNode {\n    return &TreeNode{\n        Left:  nil, // Pointer to left child node\n        Right: nil, // Pointer to right child node\n        Val:   v,   // Node value\n    }\n}\n
      /* Binary tree node */\nclass TreeNode {\n    var val: Int // Node value\n    var left: TreeNode? // Reference to left child node\n    var right: TreeNode? // Reference to right child node\n\n    init(x: Int) {\n        val = x\n    }\n}\n
      /* Binary tree node */\nclass TreeNode {\n    val; // Node value\n    left; // Pointer to left child node\n    right; // Pointer to right child node\n    constructor(val, left, right) {\n        this.val = val === undefined ? 0 : val;\n        this.left = left === undefined ? null : left;\n        this.right = right === undefined ? null : right;\n    }\n}\n
      /* Binary tree node */\nclass TreeNode {\n    val: number;\n    left: TreeNode | null;\n    right: TreeNode | null;\n\n    constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {\n        this.val = val === undefined ? 0 : val; // Node value\n        this.left = left === undefined ? null : left; // Reference to left child node\n        this.right = right === undefined ? null : right; // Reference to right child node\n    }\n}\n
      /* Binary tree node */\nclass TreeNode {\n  int val;         // Node value\n  TreeNode? left;  // Reference to left child node\n  TreeNode? right; // Reference to right child node\n  TreeNode(this.val, [this.left, this.right]);\n}\n
      use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* Binary tree node */\nstruct TreeNode {\n    val: i32,                               // Node value\n    left: Option<Rc<RefCell<TreeNode>>>,    // Reference to left child node\n    right: Option<Rc<RefCell<TreeNode>>>,   // Reference to right child node\n}\n\nimpl TreeNode {\n    /* Constructor */\n    fn new(val: i32) -> Rc<RefCell<Self>> {\n        Rc::new(RefCell::new(Self {\n            val,\n            left: None,\n            right: None\n        }))\n    }\n}\n
      /* Binary tree node */\ntypedef struct TreeNode {\n    int val;                // Node value\n    int height;             // Node height\n    struct TreeNode *left;  // Pointer to left child node\n    struct TreeNode *right; // Pointer to right child node\n} TreeNode;\n\n/* Constructor */\nTreeNode *newTreeNode(int val) {\n    TreeNode *node;\n\n    node = (TreeNode *)malloc(sizeof(TreeNode));\n    node->val = val;\n    node->height = 0;\n    node->left = NULL;\n    node->right = NULL;\n    return node;\n}\n
      /* Binary tree node */\nclass TreeNode(val _val: Int) {  // Node value\n    val left: TreeNode? = null   // Reference to left child node\n    val right: TreeNode? = null  // Reference to right child node\n}\n
      ### Binary tree node class ###\nclass TreeNode\n  attr_accessor :val    # Node value\n  attr_accessor :left   # Reference to left child node\n  attr_accessor :right  # Reference to right child node\n\n  def initialize(val)\n    @val = val\n  end\nend\n

      Each node has two references (pointers), pointing respectively to the left-child node and right-child node. This node is called the parent node of these two child nodes. When given a node of a binary tree, we call the tree formed by this node's left child and all nodes below it the left subtree of this node. Similarly, the right subtree can be defined.

      In a binary tree, except leaf nodes, all other nodes contain child nodes and non-empty subtrees. As shown in Figure 7-1, if \"Node 2\" is regarded as a parent node, its left and right child nodes are \"Node 4\" and \"Node 5\" respectively. The left subtree is formed by \"Node 4\" and all nodes beneath it, while the right subtree is formed by \"Node 5\" and all nodes beneath it.

      Figure 7-1 \u00a0 Parent Node, child Node, subtree

      "},{"location":"chapter_tree/binary_tree/#711-common-terminology-of-binary-trees","title":"7.1.1 \u00a0 Common Terminology of Binary Trees","text":"

      The commonly used terminology of binary trees is shown in Figure 7-2.

      • Root node: The node at the top level of a binary tree, which does not have a parent node.
      • Leaf node: A node that does not have any child nodes, with both of its pointers pointing to None.
      • Edge: A line segment that connects two nodes, representing a reference (pointer) between the nodes.
      • The level of a node: It increases from top to bottom, with the root node being at level 1.
      • The degree of a node: The number of child nodes that a node has. In a binary tree, the degree can be 0, 1, or 2.
      • The height of a binary tree: The number of edges from the root node to the farthest leaf node.
      • The depth of a node: The number of edges from the root node to the node.
      • The height of a node: The number of edges from the farthest leaf node to the node.

      Figure 7-2 \u00a0 Common Terminology of Binary Trees

      Tip

      Please note that we usually define \"height\" and \"depth\" as \"the number of edges traversed\", but some questions or textbooks may define them as \"the number of nodes traversed\". In this case, both height and depth need to be incremented by 1.

      "},{"location":"chapter_tree/binary_tree/#712-basic-operations-of-binary-trees","title":"7.1.2 \u00a0 Basic Operations of Binary Trees","text":""},{"location":"chapter_tree/binary_tree/#1-initializing-a-binary-tree","title":"1. \u00a0 Initializing a Binary Tree","text":"

      Similar to a linked list, the initialization of a binary tree involves first creating the nodes and then establishing the references (pointers) between them.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree.py
      # Initializing a binary tree\n# Initializing nodes\nn1 = TreeNode(val=1)\nn2 = TreeNode(val=2)\nn3 = TreeNode(val=3)\nn4 = TreeNode(val=4)\nn5 = TreeNode(val=5)\n# Linking references (pointers) between nodes\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
      binary_tree.cpp
      /* Initializing a binary tree */\n// Initializing nodes\nTreeNode* n1 = new TreeNode(1);\nTreeNode* n2 = new TreeNode(2);\nTreeNode* n3 = new TreeNode(3);\nTreeNode* n4 = new TreeNode(4);\nTreeNode* n5 = new TreeNode(5);\n// Linking references (pointers) between nodes\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
      binary_tree.java
      // Initializing nodes\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// Linking references (pointers) between nodes\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
      binary_tree.cs
      /* Initializing a binary tree */\n// Initializing nodes\nTreeNode n1 = new(1);\nTreeNode n2 = new(2);\nTreeNode n3 = new(3);\nTreeNode n4 = new(4);\nTreeNode n5 = new(5);\n// Linking references (pointers) between nodes\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
      binary_tree.go
      /* Initializing a binary tree */\n// Initializing nodes\nn1 := NewTreeNode(1)\nn2 := NewTreeNode(2)\nn3 := NewTreeNode(3)\nn4 := NewTreeNode(4)\nn5 := NewTreeNode(5)\n// Linking references (pointers) between nodes\nn1.Left = n2\nn1.Right = n3\nn2.Left = n4\nn2.Right = n5\n
      binary_tree.swift
      // Initializing nodes\nlet n1 = TreeNode(x: 1)\nlet n2 = TreeNode(x: 2)\nlet n3 = TreeNode(x: 3)\nlet n4 = TreeNode(x: 4)\nlet n5 = TreeNode(x: 5)\n// Linking references (pointers) between nodes\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
      binary_tree.js
      /* Initializing a binary tree */\n// Initializing nodes\nlet n1 = new TreeNode(1),\n    n2 = new TreeNode(2),\n    n3 = new TreeNode(3),\n    n4 = new TreeNode(4),\n    n5 = new TreeNode(5);\n// Linking references (pointers) between nodes\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
      binary_tree.ts
      /* Initializing a binary tree */\n// Initializing nodes\nlet n1 = new TreeNode(1),\n    n2 = new TreeNode(2),\n    n3 = new TreeNode(3),\n    n4 = new TreeNode(4),\n    n5 = new TreeNode(5);\n// Linking references (pointers) between nodes\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
      binary_tree.dart
      /* Initializing a binary tree */\n// Initializing nodes\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// Linking references (pointers) between nodes\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
      binary_tree.rs
      // Initializing nodes\nlet n1 = TreeNode::new(1);\nlet n2 = TreeNode::new(2);\nlet n3 = TreeNode::new(3);\nlet n4 = TreeNode::new(4);\nlet n5 = TreeNode::new(5);\n// Linking references (pointers) between nodes\nn1.borrow_mut().left = Some(n2.clone());\nn1.borrow_mut().right = Some(n3);\nn2.borrow_mut().left = Some(n4);\nn2.borrow_mut().right = Some(n5);\n
      binary_tree.c
      /* Initializing a binary tree */\n// Initializing nodes\nTreeNode *n1 = newTreeNode(1);\nTreeNode *n2 = newTreeNode(2);\nTreeNode *n3 = newTreeNode(3);\nTreeNode *n4 = newTreeNode(4);\nTreeNode *n5 = newTreeNode(5);\n// Linking references (pointers) between nodes\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
      binary_tree.kt
      // Initializing nodes\nval n1 = TreeNode(1)\nval n2 = TreeNode(2)\nval n3 = TreeNode(3)\nval n4 = TreeNode(4)\nval n5 = TreeNode(5)\n// Linking references (pointers) between nodes\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
      binary_tree.rb
      # Initializing a binary tree\n# Initializing nodes\nn1 = TreeNode.new(1)\nn2 = TreeNode.new(2)\nn3 = TreeNode.new(3)\nn4 = TreeNode.new(4)\nn5 = TreeNode.new(5)\n# Linking references (pointers) between nodes\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_tree/binary_tree/#2-inserting-and-removing-nodes","title":"2. \u00a0 Inserting and Removing Nodes","text":"

      Similar to a linked list, inserting and removing nodes in a binary tree can be achieved by modifying pointers. Figure 7-3 provides an example.

      Figure 7-3 \u00a0 Inserting and removing nodes in a binary tree

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree.py
      # Inserting and removing nodes\np = TreeNode(0)\n# Inserting node P between n1 -> n2\nn1.left = p\np.left = n2\n# Removing node P\nn1.left = n2\n
      binary_tree.cpp
      /* Inserting and removing nodes */\nTreeNode* P = new TreeNode(0);\n// Inserting node P between n1 and n2\nn1->left = P;\nP->left = n2;\n// Removing node P\nn1->left = n2;\n
      binary_tree.java
      TreeNode P = new TreeNode(0);\n// Inserting node P between n1 and n2\nn1.left = P;\nP.left = n2;\n// Removing node P\nn1.left = n2;\n
      binary_tree.cs
      /* Inserting and removing nodes */\nTreeNode P = new(0);\n// Inserting node P between n1 and n2\nn1.left = P;\nP.left = n2;\n// Removing node P\nn1.left = n2;\n
      binary_tree.go
      /* Inserting and removing nodes */\n// Inserting node P between n1 and n2\np := NewTreeNode(0)\nn1.Left = p\np.Left = n2\n// Removing node P\nn1.Left = n2\n
      binary_tree.swift
      let P = TreeNode(x: 0)\n// Inserting node P between n1 and n2\nn1.left = P\nP.left = n2\n// Removing node P\nn1.left = n2\n
      binary_tree.js
      /* Inserting and removing nodes */\nlet P = new TreeNode(0);\n// Inserting node P between n1 and n2\nn1.left = P;\nP.left = n2;\n// Removing node P\nn1.left = n2;\n
      binary_tree.ts
      /* Inserting and removing nodes */\nconst P = new TreeNode(0);\n// Inserting node P between n1 and n2\nn1.left = P;\nP.left = n2;\n// Removing node P\nn1.left = n2;\n
      binary_tree.dart
      /* Inserting and removing nodes */\nTreeNode P = new TreeNode(0);\n// Inserting node P between n1 and n2\nn1.left = P;\nP.left = n2;\n// Removing node P\nn1.left = n2;\n
      binary_tree.rs
      let p = TreeNode::new(0);\n// Inserting node P between n1 and n2\nn1.borrow_mut().left = Some(p.clone());\np.borrow_mut().left = Some(n2.clone());\n// Removing node P\nn1.borrow_mut().left = Some(n2);\n
      binary_tree.c
      /* Inserting and removing nodes */\nTreeNode *P = newTreeNode(0);\n// Inserting node P between n1 and n2\nn1->left = P;\nP->left = n2;\n// Removing node P\nn1->left = n2;\n
      binary_tree.kt
      val P = TreeNode(0)\n// Inserting node P between n1 and n2\nn1.left = P\nP.left = n2\n// Removing node P\nn1.left = n2\n
      binary_tree.rb
      # Inserting and removing nodes\n_p = TreeNode.new(0)\n# Inserting node _p between n1 and n2\nn1.left = _p\n_p.left = n2\n# Removing node _p\nn1.left = n2\n
      Code Visualization

      Full Screen >

      Tip

      It should be noted that inserting nodes may change the original logical structure of the binary tree, while removing nodes typically involves removing the node and all its subtrees. Therefore, in a binary tree, insertion and removal are usually performed through a set of operations to achieve meaningful outcomes.

      "},{"location":"chapter_tree/binary_tree/#713-common-types-of-binary-trees","title":"7.1.3 \u00a0 Common Types of Binary Trees","text":""},{"location":"chapter_tree/binary_tree/#1-perfect-binary-tree","title":"1. \u00a0 Perfect Binary Tree","text":"

      As shown in Figure 7-4, a perfect binary tree has all levels completely filled with nodes. In a perfect binary tree, leaf nodes have a degree of \\(0\\), while all other nodes have a degree of \\(2\\). If the tree height is \\(h\\), the total number of nodes is \\(2^{h+1} - 1\\), exhibiting a standard exponential relationship that reflects the common phenomenon of cell division in nature.

      Tip

      Please note that in the Chinese community, a perfect binary tree is often referred to as a full binary tree.

      Figure 7-4 \u00a0 Perfect binary tree

      "},{"location":"chapter_tree/binary_tree/#2-complete-binary-tree","title":"2. \u00a0 Complete Binary Tree","text":"

      As shown in Figure 7-5, a complete binary tree only allows the bottom level to be incompletely filled, and the nodes at the bottom level must be filled continuously from left to right. Note that a perfect binary tree is also a complete binary tree.

      Figure 7-5 \u00a0 Complete binary tree

      "},{"location":"chapter_tree/binary_tree/#3-full-binary-tree","title":"3. \u00a0 Full Binary Tree","text":"

      As shown in Figure 7-6, in a full binary tree, all nodes except leaf nodes have two child nodes.

      Figure 7-6 \u00a0 Full binary tree

      "},{"location":"chapter_tree/binary_tree/#4-balanced-binary-tree","title":"4. \u00a0 Balanced Binary Tree","text":"

      As shown in Figure 7-7, in a balanced binary tree, the absolute difference between the height of the left and right subtrees of any node does not exceed 1.

      Figure 7-7 \u00a0 Balanced binary tree

      "},{"location":"chapter_tree/binary_tree/#714-degeneration-of-binary-trees","title":"7.1.4 \u00a0 Degeneration of Binary Trees","text":"

      Figure 7-8 shows the ideal and degenerate structures of binary trees. When every level of a binary tree is filled, it reaches the \"perfect binary tree\" state; when all nodes are biased toward one side, the binary tree degenerates into a \"linked list\".

      • A perfect binary tree is the ideal case, fully leveraging the \"divide and conquer\" advantage of binary trees.
      • A linked list represents the other extreme, where all operations become linear operations with time complexity degrading to \\(O(n)\\).

      Figure 7-8 \u00a0 The Best and Worst Structures of Binary Trees

      As shown in Table 7-1, in the best and worst structures, the binary tree achieves either maximum or minimum values for leaf node count, total number of nodes, and height.

      Table 7-1 \u00a0 The Best and Worst Structures of Binary Trees

      Perfect binary tree Linked list Number of nodes at level \\(i\\) \\(2^{i-1}\\) \\(1\\) Number of leaf nodes in a tree with height \\(h\\) \\(2^h\\) \\(1\\) Total number of nodes in a tree with height \\(h\\) \\(2^{h+1} - 1\\) \\(h + 1\\) Height of a tree with \\(n\\) total nodes \\(\\log_2 (n+1) - 1\\) \\(n - 1\\)"},{"location":"chapter_tree/binary_tree_traversal/","title":"7.2 \u00a0 Binary Tree Traversal","text":"

      From a physical structure perspective, a tree is a data structure based on linked lists. Hence, its traversal method involves accessing nodes one by one through pointers. However, a tree is a non-linear data structure, which makes traversing a tree more complex than traversing a linked list, requiring the assistance of search algorithms.

      The common traversal methods for binary trees include level-order traversal, pre-order traversal, in-order traversal, and post-order traversal.

      "},{"location":"chapter_tree/binary_tree_traversal/#721-level-order-traversal","title":"7.2.1 \u00a0 Level-Order Traversal","text":"

      As shown in Figure 7-9, level-order traversal traverses the binary tree from top to bottom, layer by layer. Within each level, it visits nodes from left to right.

      Level-order traversal is essentially breadth-first traversal, also known as breadth-first search (BFS), which embodies a \"expanding outward circle by circle\" layer-by-layer traversal method.

      Figure 7-9 \u00a0 Level-order traversal of a binary tree

      "},{"location":"chapter_tree/binary_tree_traversal/#1-code-implementation","title":"1. \u00a0 Code Implementation","text":"

      Breadth-first traversal is typically implemented with the help of a \"queue\". The queue follows the \"first in, first out\" rule, while breadth-first traversal follows the \"layer-by-layer progression\" rule; the underlying ideas of the two are consistent. The implementation code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree_bfs.py
      def level_order(root: TreeNode | None) -> list[int]:\n    \"\"\"Level-order traversal\"\"\"\n    # Initialize queue, add root node\n    queue: deque[TreeNode] = deque()\n    queue.append(root)\n    # Initialize a list to save the traversal sequence\n    res = []\n    while queue:\n        node: TreeNode = queue.popleft()  # Dequeue\n        res.append(node.val)  # Save node value\n        if node.left is not None:\n            queue.append(node.left)  # Left child node enqueue\n        if node.right is not None:\n            queue.append(node.right)  # Right child node enqueue\n    return res\n
      binary_tree_bfs.cpp
      /* Level-order traversal */\nvector<int> levelOrder(TreeNode *root) {\n    // Initialize queue, add root node\n    queue<TreeNode *> queue;\n    queue.push(root);\n    // Initialize a list to save the traversal sequence\n    vector<int> vec;\n    while (!queue.empty()) {\n        TreeNode *node = queue.front();\n        queue.pop();              // Dequeue\n        vec.push_back(node->val); // Save node value\n        if (node->left != nullptr)\n            queue.push(node->left); // Left child node enqueue\n        if (node->right != nullptr)\n            queue.push(node->right); // Right child node enqueue\n    }\n    return vec;\n}\n
      binary_tree_bfs.java
      /* Level-order traversal */\nList<Integer> levelOrder(TreeNode root) {\n    // Initialize queue, add root node\n    Queue<TreeNode> queue = new LinkedList<>();\n    queue.add(root);\n    // Initialize a list to save the traversal sequence\n    List<Integer> list = new ArrayList<>();\n    while (!queue.isEmpty()) {\n        TreeNode node = queue.poll(); // Dequeue\n        list.add(node.val);           // Save node value\n        if (node.left != null)\n            queue.offer(node.left);   // Left child node enqueue\n        if (node.right != null)\n            queue.offer(node.right);  // Right child node enqueue\n    }\n    return list;\n}\n
      binary_tree_bfs.cs
      /* Level-order traversal */\nList<int> LevelOrder(TreeNode root) {\n    // Initialize queue, add root node\n    Queue<TreeNode> queue = new();\n    queue.Enqueue(root);\n    // Initialize a list to save the traversal sequence\n    List<int> list = [];\n    while (queue.Count != 0) {\n        TreeNode node = queue.Dequeue(); // Dequeue\n        list.Add(node.val!.Value);       // Save node value\n        if (node.left != null)\n            queue.Enqueue(node.left);    // Left child node enqueue\n        if (node.right != null)\n            queue.Enqueue(node.right);   // Right child node enqueue\n    }\n    return list;\n}\n
      binary_tree_bfs.go
      /* Level-order traversal */\nfunc levelOrder(root *TreeNode) []any {\n    // Initialize queue, add root node\n    queue := list.New()\n    queue.PushBack(root)\n    // Initialize a slice to save traversal sequence\n    nums := make([]any, 0)\n    for queue.Len() > 0 {\n        // Dequeue\n        node := queue.Remove(queue.Front()).(*TreeNode)\n        // Save node value\n        nums = append(nums, node.Val)\n        if node.Left != nil {\n            // Left child node enqueue\n            queue.PushBack(node.Left)\n        }\n        if node.Right != nil {\n            // Right child node enqueue\n            queue.PushBack(node.Right)\n        }\n    }\n    return nums\n}\n
      binary_tree_bfs.swift
      /* Level-order traversal */\nfunc levelOrder(root: TreeNode) -> [Int] {\n    // Initialize queue, add root node\n    var queue: [TreeNode] = [root]\n    // Initialize a list to save the traversal sequence\n    var list: [Int] = []\n    while !queue.isEmpty {\n        let node = queue.removeFirst() // Dequeue\n        list.append(node.val) // Save node value\n        if let left = node.left {\n            queue.append(left) // Left child node enqueue\n        }\n        if let right = node.right {\n            queue.append(right) // Right child node enqueue\n        }\n    }\n    return list\n}\n
      binary_tree_bfs.js
      /* Level-order traversal */\nfunction levelOrder(root) {\n    // Initialize queue, add root node\n    const queue = [root];\n    // Initialize a list to save the traversal sequence\n    const list = [];\n    while (queue.length) {\n        let node = queue.shift(); // Dequeue\n        list.push(node.val); // Save node value\n        if (node.left) queue.push(node.left); // Left child node enqueue\n        if (node.right) queue.push(node.right); // Right child node enqueue\n    }\n    return list;\n}\n
      binary_tree_bfs.ts
      /* Level-order traversal */\nfunction levelOrder(root: TreeNode | null): number[] {\n    // Initialize queue, add root node\n    const queue = [root];\n    // Initialize a list to save the traversal sequence\n    const list: number[] = [];\n    while (queue.length) {\n        let node = queue.shift() as TreeNode; // Dequeue\n        list.push(node.val); // Save node value\n        if (node.left) {\n            queue.push(node.left); // Left child node enqueue\n        }\n        if (node.right) {\n            queue.push(node.right); // Right child node enqueue\n        }\n    }\n    return list;\n}\n
      binary_tree_bfs.dart
      /* Level-order traversal */\nList<int> levelOrder(TreeNode? root) {\n  // Initialize queue, add root node\n  Queue<TreeNode?> queue = Queue();\n  queue.add(root);\n  // Initialize a list to save the traversal sequence\n  List<int> res = [];\n  while (queue.isNotEmpty) {\n    TreeNode? node = queue.removeFirst(); // Dequeue\n    res.add(node!.val); // Save node value\n    if (node.left != null) queue.add(node.left); // Left child node enqueue\n    if (node.right != null) queue.add(node.right); // Right child node enqueue\n  }\n  return res;\n}\n
      binary_tree_bfs.rs
      /* Level-order traversal */\nfn level_order(root: &Rc<RefCell<TreeNode>>) -> Vec<i32> {\n    // Initialize queue, add root node\n    let mut que = VecDeque::new();\n    que.push_back(root.clone());\n    // Initialize a list to save the traversal sequence\n    let mut vec = Vec::new();\n\n    while let Some(node) = que.pop_front() {\n        // Dequeue\n        vec.push(node.borrow().val); // Save node value\n        if let Some(left) = node.borrow().left.as_ref() {\n            que.push_back(left.clone()); // Left child node enqueue\n        }\n        if let Some(right) = node.borrow().right.as_ref() {\n            que.push_back(right.clone()); // Right child node enqueue\n        };\n    }\n    vec\n}\n
      binary_tree_bfs.c
      /* Level-order traversal */\nint *levelOrder(TreeNode *root, int *size) {\n    /* Auxiliary queue */\n    int front, rear;\n    int index, *arr;\n    TreeNode *node;\n    TreeNode **queue;\n\n    /* Auxiliary queue */\n    queue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_SIZE);\n    // Queue pointer\n    front = 0, rear = 0;\n    // Add root node\n    queue[rear++] = root;\n    // Initialize a list to save the traversal sequence\n    /* Auxiliary array */\n    arr = (int *)malloc(sizeof(int) * MAX_SIZE);\n    // Array pointer\n    index = 0;\n    while (front < rear) {\n        // Dequeue\n        node = queue[front++];\n        // Save node value\n        arr[index++] = node->val;\n        if (node->left != NULL) {\n            // Left child node enqueue\n            queue[rear++] = node->left;\n        }\n        if (node->right != NULL) {\n            // Right child node enqueue\n            queue[rear++] = node->right;\n        }\n    }\n    // Update array length value\n    *size = index;\n    arr = realloc(arr, sizeof(int) * (*size));\n\n    // Free auxiliary array space\n    free(queue);\n    return arr;\n}\n
      binary_tree_bfs.kt
      /* Level-order traversal */\nfun levelOrder(root: TreeNode?): MutableList<Int> {\n    // Initialize queue, add root node\n    val queue = LinkedList<TreeNode?>()\n    queue.add(root)\n    // Initialize a list to save the traversal sequence\n    val list = mutableListOf<Int>()\n    while (queue.isNotEmpty()) {\n        val node = queue.poll()      // Dequeue\n        list.add(node?._val!!)       // Save node value\n        if (node.left != null)\n            queue.offer(node.left)   // Left child node enqueue\n        if (node.right != null)\n            queue.offer(node.right)  // Right child node enqueue\n    }\n    return list\n}\n
      binary_tree_bfs.rb
      ### Level-order traversal ###\ndef level_order(root)\n  # Initialize queue, add root node\n  queue = [root]\n  # Initialize a list to save the traversal sequence\n  res = []\n  while !queue.empty?\n    node = queue.shift # Dequeue\n    res << node.val # Save node value\n    queue << node.left unless node.left.nil? # Left child node enqueue\n    queue << node.right unless node.right.nil? # Right child node enqueue\n  end\n  res\nend\n
      "},{"location":"chapter_tree/binary_tree_traversal/#2-complexity-analysis","title":"2. \u00a0 Complexity Analysis","text":"
      • Time complexity is \\(O(n)\\): All nodes are visited once, using \\(O(n)\\) time, where \\(n\\) is the number of nodes.
      • Space complexity is \\(O(n)\\): In the worst case, i.e., a full binary tree, before traversing to the bottom level, the queue contains at most \\((n + 1) / 2\\) nodes simultaneously, occupying \\(O(n)\\) space.
      "},{"location":"chapter_tree/binary_tree_traversal/#722-preorder-inorder-and-postorder-traversal","title":"7.2.2 \u00a0 Preorder, Inorder, and Postorder Traversal","text":"

      Correspondingly, preorder, inorder, and postorder traversals all belong to depth-first traversal, also known as depth-first search (DFS), which embodies a \"first go to the end, then backtrack and continue\" traversal method.

      Figure 7-10 shows how depth-first traversal works on a binary tree. Depth-first traversal is like \"walking\" around the perimeter of the entire binary tree, encountering three positions at each node, corresponding to preorder, inorder, and postorder traversal.

      Figure 7-10 \u00a0 Preorder, inorder, and postorder traversal of a binary tree

      "},{"location":"chapter_tree/binary_tree_traversal/#1-code-implementation_1","title":"1. \u00a0 Code Implementation","text":"

      Depth-first search is usually implemented based on recursion:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree_dfs.py
      def pre_order(root: TreeNode | None):\n    \"\"\"Preorder traversal\"\"\"\n    if root is None:\n        return\n    # Visit priority: root node -> left subtree -> right subtree\n    res.append(root.val)\n    pre_order(root=root.left)\n    pre_order(root=root.right)\n\ndef in_order(root: TreeNode | None):\n    \"\"\"Inorder traversal\"\"\"\n    if root is None:\n        return\n    # Visit priority: left subtree -> root node -> right subtree\n    in_order(root=root.left)\n    res.append(root.val)\n    in_order(root=root.right)\n\ndef post_order(root: TreeNode | None):\n    \"\"\"Postorder traversal\"\"\"\n    if root is None:\n        return\n    # Visit priority: left subtree -> right subtree -> root node\n    post_order(root=root.left)\n    post_order(root=root.right)\n    res.append(root.val)\n
      binary_tree_dfs.cpp
      /* Preorder traversal */\nvoid preOrder(TreeNode *root) {\n    if (root == nullptr)\n        return;\n    // Visit priority: root node -> left subtree -> right subtree\n    vec.push_back(root->val);\n    preOrder(root->left);\n    preOrder(root->right);\n}\n\n/* Inorder traversal */\nvoid inOrder(TreeNode *root) {\n    if (root == nullptr)\n        return;\n    // Visit priority: left subtree -> root node -> right subtree\n    inOrder(root->left);\n    vec.push_back(root->val);\n    inOrder(root->right);\n}\n\n/* Postorder traversal */\nvoid postOrder(TreeNode *root) {\n    if (root == nullptr)\n        return;\n    // Visit priority: left subtree -> right subtree -> root node\n    postOrder(root->left);\n    postOrder(root->right);\n    vec.push_back(root->val);\n}\n
      binary_tree_dfs.java
      /* Preorder traversal */\nvoid preOrder(TreeNode root) {\n    if (root == null)\n        return;\n    // Visit priority: root node -> left subtree -> right subtree\n    list.add(root.val);\n    preOrder(root.left);\n    preOrder(root.right);\n}\n\n/* Inorder traversal */\nvoid inOrder(TreeNode root) {\n    if (root == null)\n        return;\n    // Visit priority: left subtree -> root node -> right subtree\n    inOrder(root.left);\n    list.add(root.val);\n    inOrder(root.right);\n}\n\n/* Postorder traversal */\nvoid postOrder(TreeNode root) {\n    if (root == null)\n        return;\n    // Visit priority: left subtree -> right subtree -> root node\n    postOrder(root.left);\n    postOrder(root.right);\n    list.add(root.val);\n}\n
      binary_tree_dfs.cs
      /* Preorder traversal */\nvoid PreOrder(TreeNode? root) {\n    if (root == null) return;\n    // Visit priority: root node -> left subtree -> right subtree\n    list.Add(root.val!.Value);\n    PreOrder(root.left);\n    PreOrder(root.right);\n}\n\n/* Inorder traversal */\nvoid InOrder(TreeNode? root) {\n    if (root == null) return;\n    // Visit priority: left subtree -> root node -> right subtree\n    InOrder(root.left);\n    list.Add(root.val!.Value);\n    InOrder(root.right);\n}\n\n/* Postorder traversal */\nvoid PostOrder(TreeNode? root) {\n    if (root == null) return;\n    // Visit priority: left subtree -> right subtree -> root node\n    PostOrder(root.left);\n    PostOrder(root.right);\n    list.Add(root.val!.Value);\n}\n
      binary_tree_dfs.go
      /* Preorder traversal */\nfunc preOrder(node *TreeNode) {\n    if node == nil {\n        return\n    }\n    // Visit priority: root node -> left subtree -> right subtree\n    nums = append(nums, node.Val)\n    preOrder(node.Left)\n    preOrder(node.Right)\n}\n\n/* Inorder traversal */\nfunc inOrder(node *TreeNode) {\n    if node == nil {\n        return\n    }\n    // Visit priority: left subtree -> root node -> right subtree\n    inOrder(node.Left)\n    nums = append(nums, node.Val)\n    inOrder(node.Right)\n}\n\n/* Postorder traversal */\nfunc postOrder(node *TreeNode) {\n    if node == nil {\n        return\n    }\n    // Visit priority: left subtree -> right subtree -> root node\n    postOrder(node.Left)\n    postOrder(node.Right)\n    nums = append(nums, node.Val)\n}\n
      binary_tree_dfs.swift
      /* Preorder traversal */\nfunc preOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    // Visit priority: root node -> left subtree -> right subtree\n    list.append(root.val)\n    preOrder(root: root.left)\n    preOrder(root: root.right)\n}\n\n/* Inorder traversal */\nfunc inOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    // Visit priority: left subtree -> root node -> right subtree\n    inOrder(root: root.left)\n    list.append(root.val)\n    inOrder(root: root.right)\n}\n\n/* Postorder traversal */\nfunc postOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    // Visit priority: left subtree -> right subtree -> root node\n    postOrder(root: root.left)\n    postOrder(root: root.right)\n    list.append(root.val)\n}\n
      binary_tree_dfs.js
      /* Preorder traversal */\nfunction preOrder(root) {\n    if (root === null) return;\n    // Visit priority: root node -> left subtree -> right subtree\n    list.push(root.val);\n    preOrder(root.left);\n    preOrder(root.right);\n}\n\n/* Inorder traversal */\nfunction inOrder(root) {\n    if (root === null) return;\n    // Visit priority: left subtree -> root node -> right subtree\n    inOrder(root.left);\n    list.push(root.val);\n    inOrder(root.right);\n}\n\n/* Postorder traversal */\nfunction postOrder(root) {\n    if (root === null) return;\n    // Visit priority: left subtree -> right subtree -> root node\n    postOrder(root.left);\n    postOrder(root.right);\n    list.push(root.val);\n}\n
      binary_tree_dfs.ts
      /* Preorder traversal */\nfunction preOrder(root: TreeNode | null): void {\n    if (root === null) {\n        return;\n    }\n    // Visit priority: root node -> left subtree -> right subtree\n    list.push(root.val);\n    preOrder(root.left);\n    preOrder(root.right);\n}\n\n/* Inorder traversal */\nfunction inOrder(root: TreeNode | null): void {\n    if (root === null) {\n        return;\n    }\n    // Visit priority: left subtree -> root node -> right subtree\n    inOrder(root.left);\n    list.push(root.val);\n    inOrder(root.right);\n}\n\n/* Postorder traversal */\nfunction postOrder(root: TreeNode | null): void {\n    if (root === null) {\n        return;\n    }\n    // Visit priority: left subtree -> right subtree -> root node\n    postOrder(root.left);\n    postOrder(root.right);\n    list.push(root.val);\n}\n
      binary_tree_dfs.dart
      /* Preorder traversal */\nvoid preOrder(TreeNode? node) {\n  if (node == null) return;\n  // Visit priority: root node -> left subtree -> right subtree\n  list.add(node.val);\n  preOrder(node.left);\n  preOrder(node.right);\n}\n\n/* Inorder traversal */\nvoid inOrder(TreeNode? node) {\n  if (node == null) return;\n  // Visit priority: left subtree -> root node -> right subtree\n  inOrder(node.left);\n  list.add(node.val);\n  inOrder(node.right);\n}\n\n/* Postorder traversal */\nvoid postOrder(TreeNode? node) {\n  if (node == null) return;\n  // Visit priority: left subtree -> right subtree -> root node\n  postOrder(node.left);\n  postOrder(node.right);\n  list.add(node.val);\n}\n
      binary_tree_dfs.rs
      /* Preorder traversal */\nfn pre_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n    let mut result = vec![];\n\n    fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {\n        if let Some(node) = root {\n            // Visit priority: root node -> left subtree -> right subtree\n            let node = node.borrow();\n            res.push(node.val);\n            dfs(node.left.as_ref(), res);\n            dfs(node.right.as_ref(), res);\n        }\n    }\n    dfs(root, &mut result);\n\n    result\n}\n\n/* Inorder traversal */\nfn in_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n    let mut result = vec![];\n\n    fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {\n        if let Some(node) = root {\n            // Visit priority: left subtree -> root node -> right subtree\n            let node = node.borrow();\n            dfs(node.left.as_ref(), res);\n            res.push(node.val);\n            dfs(node.right.as_ref(), res);\n        }\n    }\n    dfs(root, &mut result);\n\n    result\n}\n\n/* Postorder traversal */\nfn post_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n    let mut result = vec![];\n\n    fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {\n        if let Some(node) = root {\n            // Visit priority: left subtree -> right subtree -> root node\n            let node = node.borrow();\n            dfs(node.left.as_ref(), res);\n            dfs(node.right.as_ref(), res);\n            res.push(node.val);\n        }\n    }\n\n    dfs(root, &mut result);\n\n    result\n}\n
      binary_tree_dfs.c
      /* Preorder traversal */\nvoid preOrder(TreeNode *root, int *size) {\n    if (root == NULL)\n        return;\n    // Visit priority: root node -> left subtree -> right subtree\n    arr[(*size)++] = root->val;\n    preOrder(root->left, size);\n    preOrder(root->right, size);\n}\n\n/* Inorder traversal */\nvoid inOrder(TreeNode *root, int *size) {\n    if (root == NULL)\n        return;\n    // Visit priority: left subtree -> root node -> right subtree\n    inOrder(root->left, size);\n    arr[(*size)++] = root->val;\n    inOrder(root->right, size);\n}\n\n/* Postorder traversal */\nvoid postOrder(TreeNode *root, int *size) {\n    if (root == NULL)\n        return;\n    // Visit priority: left subtree -> right subtree -> root node\n    postOrder(root->left, size);\n    postOrder(root->right, size);\n    arr[(*size)++] = root->val;\n}\n
      binary_tree_dfs.kt
      /* Preorder traversal */\nfun preOrder(root: TreeNode?) {\n    if (root == null) return\n    // Visit priority: root node -> left subtree -> right subtree\n    list.add(root._val)\n    preOrder(root.left)\n    preOrder(root.right)\n}\n\n/* Inorder traversal */\nfun inOrder(root: TreeNode?) {\n    if (root == null) return\n    // Visit priority: left subtree -> root node -> right subtree\n    inOrder(root.left)\n    list.add(root._val)\n    inOrder(root.right)\n}\n\n/* Postorder traversal */\nfun postOrder(root: TreeNode?) {\n    if (root == null) return\n    // Visit priority: left subtree -> right subtree -> root node\n    postOrder(root.left)\n    postOrder(root.right)\n    list.add(root._val)\n}\n
      binary_tree_dfs.rb
      ### Pre-order traversal ###\ndef pre_order(root)\n  return if root.nil?\n\n  # Visit priority: root node -> left subtree -> right subtree\n  $res << root.val\n  pre_order(root.left)\n  pre_order(root.right)\nend\n\n### In-order traversal ###\ndef in_order(root)\n  return if root.nil?\n\n  # Visit priority: left subtree -> root node -> right subtree\n  in_order(root.left)\n  $res << root.val\n  in_order(root.right)\nend\n\n### Post-order traversal ###\ndef post_order(root)\n  return if root.nil?\n\n  # Visit priority: left subtree -> right subtree -> root node\n  post_order(root.left)\n  post_order(root.right)\n  $res << root.val\nend\n

      Tip

      Depth-first search can also be implemented based on iteration, interested readers can study this on their own.

      Figure 7-11 shows the recursive process of preorder traversal of a binary tree, which can be divided into two opposite parts: \"recursion\" and \"return\".

      1. \"Recursion\" means opening a new method, where the program accesses the next node in this process.
      2. \"Return\" means the function returns, indicating that the current node has been fully visited.
      <1><2><3><4><5><6><7><8><9><10><11>

      Figure 7-11 \u00a0 The recursive process of preorder traversal

      "},{"location":"chapter_tree/binary_tree_traversal/#2-complexity-analysis_1","title":"2. \u00a0 Complexity Analysis","text":"
      • Time complexity is \\(O(n)\\): All nodes are visited once, using \\(O(n)\\) time.
      • Space complexity is \\(O(n)\\): In the worst case, i.e., the tree degenerates into a linked list, the recursion depth reaches \\(n\\), and the system occupies \\(O(n)\\) stack frame space.
      "},{"location":"chapter_tree/summary/","title":"7.6 \u00a0 Summary","text":""},{"location":"chapter_tree/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • A binary tree is a non-linear data structure that embodies the divide-and-conquer logic of \"one divides into two\". Each binary tree node contains a value and two pointers, which respectively point to its left and right child nodes.
      • For a certain node in a binary tree, the tree formed by its left (right) child node and all nodes below is called the left (right) subtree of that node.
      • Related terminology of binary trees includes root node, leaf node, level, degree, edge, height, and depth.
      • The initialization, node insertion, and node removal operations of binary trees are similar to those of linked lists.
      • Common types of binary trees include perfect binary trees, complete binary trees, full binary trees, and balanced binary trees. The perfect binary tree is the ideal state, while the linked list is the worst state after degradation.
      • A binary tree can be represented using an array by arranging node values and empty slots in level-order traversal sequence, and implementing pointers based on the index mapping relationship between parent and child nodes.
      • Level-order traversal of a binary tree is a breadth-first search method, embodying a layer-by-layer traversal approach of \"expanding outward circle by circle\", typically implemented using a queue.
      • Preorder, inorder, and postorder traversals all belong to depth-first search, embodying a traversal approach of \"first go to the end, then backtrack and continue\", typically implemented using recursion.
      • A binary search tree is an efficient data structure for element searching, with search, insertion, and removal operations all having time complexity of \\(O(\\log n)\\). When a binary search tree degenerates into a linked list, all time complexities degrade to \\(O(n)\\).
      • An AVL tree, also known as a balanced binary search tree, ensures the tree remains balanced after continuous node insertions and removals through rotation operations.
      • Rotation operations in AVL trees include right rotation, left rotation, left rotation then right rotation, and right rotation then left rotation. After inserting or removing nodes, AVL trees perform rotation operations from bottom to top to restore the tree to balance.
      "},{"location":"chapter_tree/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

      Q: For a binary tree with only one node, are both the height of the tree and the depth of the root node \\(0\\)?

      Yes, because height and depth are typically defined as \"the number of edges passed.\"

      Q: The insertion and removal in a binary tree are generally accomplished by a set of operations. What does \"a set of operations\" refer to here? Does it imply releasing the resources of the child nodes?

      Taking the binary search tree as an example, the operation of removing a node needs to be handled in three different scenarios, each requiring multiple steps of node operations.

      Q: Why does DFS traversal of binary trees have three orders: preorder, inorder, and postorder, and what are their uses?

      Similar to forward and reverse traversal of arrays, preorder, inorder, and postorder traversals are three methods of binary tree traversal that allow us to obtain a traversal result in a specific order. For example, in a binary search tree, since nodes satisfy the relationship left child node value < root node value < right child node value, we only need to traverse the tree with the priority of \"left \\(\\rightarrow\\) root \\(\\rightarrow\\) right\" to obtain an ordered node sequence.

      Q: In a right rotation operation handling the relationship between unbalanced nodes node, child, and grand_child, doesn't the connection between node and its parent node get lost after the right rotation?

      We need to view this problem from a recursive perspective. The right rotation operation right_rotate(root) passes in the root node of the subtree and eventually returns the root node of the subtree after rotation with return child. The connection between the subtree's root node and its parent node is completed after the function returns, which is not within the maintenance scope of the right rotation operation.

      Q: In C++, functions are divided into private and public sections. What considerations are there for this? Why are the height() function and the updateHeight() function placed in public and private, respectively?

      It mainly depends on the method's usage scope. If a method is only used within the class, then it is designed as private. For example, calling updateHeight() alone by the user makes no sense, as it is only a step in insertion or removal operations. However, height() is used to access node height, similar to vector.size(), so it is set to public for ease of use.

      Q: How do you build a binary search tree from a set of input data? Is the choice of root node very important?

      Yes, the method for building a tree is provided in the build_tree() method in the binary search tree code. As for the choice of root node, we typically sort the input data, then select the middle element as the root node, and recursively build the left and right subtrees. This approach maximizes the tree's balance.

      Q: In Java, do you always have to use the equals() method for string comparison?

      In Java, for primitive data types, == is used to compare whether the values of two variables are equal. For reference types, the working principles of the two symbols are different.

      • ==: Used to compare whether two variables point to the same object, i.e., whether their positions in memory are the same.
      • equals(): Used to compare whether the values of two objects are equal.

      Therefore, if we want to compare values, we should use equals(). However, strings initialized via String a = \"hi\"; String b = \"hi\"; are stored in the string constant pool and point to the same object, so a == b can also be used to compare the contents of the two strings.

      Q: Before reaching the bottom level, is the number of nodes in the queue \\(2^h\\) in breadth-first traversal?

      Yes, for example, a full binary tree with height \\(h = 2\\) has a total of \\(n = 7\\) nodes, then the bottom level has \\(4 = 2^h = (n + 1) / 2\\) nodes.

      "}]} \ No newline at end of file +{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"],"fields":{"title":{"boost":1000.0},"text":{"boost":1.0},"tags":{"boost":1000000.0}}},"docs":[{"location":"chapter_appendix/","title":"Chapter 16. \u00a0 Appendix","text":""},{"location":"chapter_appendix/#chapter-contents","title":"Chapter contents","text":"
      • 16.1 \u00a0 Programming Environment Installation
      • 16.2 \u00a0 Contributing Together
      • 16.3 \u00a0 Terminology Table
      "},{"location":"chapter_appendix/contribution/","title":"16.2 \u00a0 Contributing Together","text":"

      Due to limited capacity, there may be inevitable omissions and errors in this book. We appreciate your understanding and are grateful for your help in correcting them. If you discover typos, broken links, missing content, ambiguous wording, unclear explanations, or structural issues, please help us make corrections to provide readers with higher-quality learning resources.

      The GitHub IDs of all contributors will be displayed on the homepage of the book repository, the web version, and the PDF version to acknowledge their selfless contributions to the open source community.

      The Charm of Open Source

      The interval between two printings of a physical book is often quite long, making content updates very inconvenient.

      In this open source book, the time for content updates has been shortened to just days or even hours.

      "},{"location":"chapter_appendix/contribution/#1-minor-content-adjustments","title":"1. \u00a0 Minor Content Adjustments","text":"

      As shown in Figure 16-3, there is an \"edit icon\" in the top-right corner of each page. You can modify text or code by following these steps.

      1. Click the \"edit icon\". If you encounter a prompt asking you to \"Fork this repository\", please approve the operation.
      2. Modify the content of the Markdown source file, verify the correctness of the content, and maintain consistent formatting as much as possible.
      3. Fill in a description of your changes at the bottom of the page, then click the \"Propose file change\" button. After the page transitions, click the \"Create pull request\" button to submit your pull request.

      Figure 16-3 \u00a0 Page edit button

      Images cannot be directly modified. Please describe the issue by creating a new Issue or leaving a comment. We will promptly redraw and replace the images.

      "},{"location":"chapter_appendix/contribution/#2-content-creation","title":"2. \u00a0 Content Creation","text":"

      If you are interested in contributing to this open source project, including translating code into other programming languages or expanding article content, you will need to follow the Pull Request workflow below.

      1. Log in to GitHub and Fork the book's code repository to your personal account.
      2. Enter your forked repository webpage and use the git clone command to clone the repository to your local machine.
      3. Create content locally and conduct comprehensive tests to verify code correctness.
      4. Commit your local changes and push them to the remote repository.
      5. Refresh the repository webpage and click the \"Create pull request\" button to submit your pull request.
      "},{"location":"chapter_appendix/contribution/#3-docker-deployment","title":"3. \u00a0 Docker Deployment","text":"

      From the root directory of hello-algo, run the following Docker script to access the project at http://localhost:8000:

      docker-compose up -d\n

      Use the following command to remove the deployment:

      docker-compose down\n
      "},{"location":"chapter_appendix/installation/","title":"16.1 \u00a0 Programming Environment Installation","text":""},{"location":"chapter_appendix/installation/#1611-installing-ide","title":"16.1.1 \u00a0 Installing Ide","text":"

      We recommend using the open-source and lightweight VS Code as the local integrated development environment (IDE). Visit the VS Code official website, and download and install the appropriate version of VS Code according to your operating system.

      Figure 16-1 \u00a0 Download VS Code from the Official Website

      VS Code has a powerful ecosystem of extensions that supports running and debugging most programming languages. For example, after installing the \"Python Extension Pack\" extension, you can debug Python code. The installation steps are shown in the following figure.

      Figure 16-2 \u00a0 Install VS Code Extensions

      "},{"location":"chapter_appendix/installation/#1612-installing-language-environments","title":"16.1.2 \u00a0 Installing Language Environments","text":""},{"location":"chapter_appendix/installation/#1-python-environment","title":"1. \u00a0 Python Environment","text":"
      1. Download and install Miniconda3, which requires Python 3.10 or newer.
      2. Search for python in the VS Code extension marketplace and install the Python Extension Pack.
      3. (Optional) Enter pip install black on the command line to install the code formatter.
      "},{"location":"chapter_appendix/installation/#2-cc-environment","title":"2. \u00a0 C/c++ Environment","text":"
      1. Windows systems need to install MinGW (configuration tutorial); macOS comes with Clang built-in and does not require installation.
      2. Search for c++ in the VS Code extension marketplace and install the C/C++ Extension Pack.
      3. (Optional) Open the Settings page, search for the Clang_format_fallback Style code formatting option, and set it to { BasedOnStyle: Microsoft, BreakBeforeBraces: Attach }.
      "},{"location":"chapter_appendix/installation/#3-java-environment","title":"3. \u00a0 Java Environment","text":"
      1. Download and install OpenJDK (version must be > JDK 9).
      2. Search for java in the VS Code extension marketplace and install the Extension Pack for Java.
      "},{"location":"chapter_appendix/installation/#4-c-environment","title":"4. \u00a0 C# Environment","text":"
      1. Download and install .Net 8.0.
      2. Search for C# Dev Kit in the VS Code extension marketplace and install C# Dev Kit (configuration tutorial).
      3. You can also use Visual Studio (installation tutorial).
      "},{"location":"chapter_appendix/installation/#5-go-environment","title":"5. \u00a0 Go Environment","text":"
      1. Download and install Go.
      2. Search for go in the VS Code extension marketplace and install Go.
      3. Press Ctrl + Shift + P to open the command palette, type go, select Go: Install/Update Tools, check all options and install.
      "},{"location":"chapter_appendix/installation/#6-swift-environment","title":"6. \u00a0 Swift Environment","text":"
      1. Download and install Swift.
      2. Search for swift in the VS Code extension marketplace and install Swift for Visual Studio Code.
      "},{"location":"chapter_appendix/installation/#7-javascript-environment","title":"7. \u00a0 Javascript Environment","text":"
      1. Download and install Node.js.
      2. (Optional) Search for Prettier in the VS Code extension marketplace and install the code formatter.
      "},{"location":"chapter_appendix/installation/#8-typescript-environment","title":"8. \u00a0 Typescript Environment","text":"
      1. Follow the same installation steps as the JavaScript environment.
      2. Install TypeScript Execute (tsx).
      3. Search for typescript in the VS Code extension marketplace and install Pretty TypeScript Errors.
      "},{"location":"chapter_appendix/installation/#9-dart-environment","title":"9. \u00a0 Dart Environment","text":"
      1. Download and install Dart.
      2. Search for dart in the VS Code extension marketplace and install Dart.
      "},{"location":"chapter_appendix/installation/#10-rust-environment","title":"10. \u00a0 Rust Environment","text":"
      1. Download and install Rust.
      2. Search for rust in the VS Code extension marketplace and install rust-analyzer.
      "},{"location":"chapter_appendix/terminology/","title":"16.3 \u00a0 Terminology Table","text":"

      The following table lists important terms that appear in this book. It is worth noting the following points:

      • We recommend remembering the English names of terms to help with reading English literature.
      • Some terms have different names in Simplified Chinese and Traditional Chinese.

      Table 16-1 \u00a0 Important Terms in Data Structures and Algorithms

      English Simplified Chinese Traditional Chinese algorithm \u7b97\u6cd5 \u6f14\u7b97\u6cd5 data structure \u6570\u636e\u7ed3\u6784 \u8cc7\u6599\u7d50\u69cb code \u4ee3\u7801 \u7a0b\u5f0f\u78bc file \u6587\u4ef6 \u6a94\u6848 function \u51fd\u6570 \u51fd\u5f0f method \u65b9\u6cd5 \u65b9\u6cd5 variable \u53d8\u91cf \u8b8a\u6578 asymptotic complexity analysis \u6e10\u8fd1\u590d\u6742\u5ea6\u5206\u6790 \u6f38\u8fd1\u8907\u96dc\u5ea6\u5206\u6790 time complexity \u65f6\u95f4\u590d\u6742\u5ea6 \u6642\u9593\u8907\u96dc\u5ea6 space complexity \u7a7a\u95f4\u590d\u6742\u5ea6 \u7a7a\u9593\u8907\u96dc\u5ea6 loop \u5faa\u73af \u8ff4\u5708 iteration \u8fed\u4ee3 \u8fed\u4ee3 recursion \u9012\u5f52 \u905e\u8ff4 tail recursion \u5c3e\u9012\u5f52 \u5c3e\u905e\u8ff4 recursion tree \u9012\u5f52\u6811 \u905e\u8ff4\u6a39 big-\\(O\\) notation \u5927 \\(O\\) \u8bb0\u53f7 \u5927 \\(O\\) \u8a18\u865f asymptotic upper bound \u6e10\u8fd1\u4e0a\u754c \u6f38\u8fd1\u4e0a\u754c sign-magnitude \u539f\u7801 \u539f\u78bc 1\u2019s complement \u53cd\u7801 \u4e00\u88dc\u6578 2\u2019s complement \u8865\u7801 \u4e8c\u88dc\u6578 array \u6570\u7ec4 \u9663\u5217 index \u7d22\u5f15 \u7d22\u5f15 linked list \u94fe\u8868 \u93c8\u7d50\u4e32\u5217 linked list node, list node \u94fe\u8868\u8282\u70b9 \u93c8\u7d50\u4e32\u5217\u7bc0\u9ede head node \u5934\u8282\u70b9 \u982d\u7bc0\u9ede tail node \u5c3e\u8282\u70b9 \u5c3e\u7bc0\u9ede list \u5217\u8868 \u4e32\u5217 dynamic array \u52a8\u6001\u6570\u7ec4 \u52d5\u614b\u9663\u5217 hard disk \u786c\u76d8 \u786c\u789f random-access memory (RAM) \u5185\u5b58 \u8a18\u61b6\u9ad4 cache memory \u7f13\u5b58 \u5feb\u53d6 cache miss \u7f13\u5b58\u672a\u547d\u4e2d \u5feb\u53d6\u672a\u547d\u4e2d cache hit rate \u7f13\u5b58\u547d\u4e2d\u7387 \u5feb\u53d6\u547d\u4e2d\u7387 stack \u6808 \u5806\u758a top of the stack \u6808\u9876 \u5806\u758a\u9802 bottom of the stack \u6808\u5e95 \u5806\u758a\u5e95 queue \u961f\u5217 \u4f47\u5217 double-ended queue \u53cc\u5411\u961f\u5217 \u96d9\u5411\u4f47\u5217 front of the queue \u961f\u9996 \u4f47\u5217\u9996 rear of the queue \u961f\u5c3e \u4f47\u5217\u5c3e hash table \u54c8\u5e0c\u8868 \u96dc\u6e4a\u8868 hash set \u54c8\u5e0c\u96c6\u5408 \u96dc\u6e4a\u96c6\u5408 bucket \u6876 \u6876 hash function \u54c8\u5e0c\u51fd\u6570 \u96dc\u6e4a\u51fd\u5f0f hash collision \u54c8\u5e0c\u51b2\u7a81 \u96dc\u6e4a\u885d\u7a81 load factor \u8d1f\u8f7d\u56e0\u5b50 \u8ca0\u8f09\u56e0\u5b50 separate chaining \u94fe\u5f0f\u5730\u5740 \u93c8\u7d50\u4f4d\u5740 open addressing \u5f00\u653e\u5bfb\u5740 \u958b\u653e\u5b9a\u5740 linear probing \u7ebf\u6027\u63a2\u6d4b \u7dda\u6027\u63a2\u67e5 lazy deletion \u61d2\u5220\u9664 \u61f6\u522a\u9664 binary tree \u4e8c\u53c9\u6811 \u4e8c\u5143\u6a39 tree node \u6811\u8282\u70b9 \u6a39\u7bc0\u9ede left-child node \u5de6\u5b50\u8282\u70b9 \u5de6\u5b50\u7bc0\u9ede right-child node \u53f3\u5b50\u8282\u70b9 \u53f3\u5b50\u7bc0\u9ede parent node \u7236\u8282\u70b9 \u7236\u7bc0\u9ede left subtree \u5de6\u5b50\u6811 \u5de6\u5b50\u6a39 right subtree \u53f3\u5b50\u6811 \u53f3\u5b50\u6a39 root node \u6839\u8282\u70b9 \u6839\u7bc0\u9ede leaf node \u53f6\u8282\u70b9 \u8449\u7bc0\u9ede edge \u8fb9 \u908a level \u5c42 \u5c64 degree \u5ea6 \u5ea6 height \u9ad8\u5ea6 \u9ad8\u5ea6 depth \u6df1\u5ea6 \u6df1\u5ea6 perfect binary tree \u5b8c\u7f8e\u4e8c\u53c9\u6811 \u5b8c\u7f8e\u4e8c\u5143\u6a39 complete binary tree \u5b8c\u5168\u4e8c\u53c9\u6811 \u5b8c\u5168\u4e8c\u5143\u6a39 full binary tree \u5b8c\u6ee1\u4e8c\u53c9\u6811 \u5b8c\u6eff\u4e8c\u5143\u6a39 balanced binary tree \u5e73\u8861\u4e8c\u53c9\u6811 \u5e73\u8861\u4e8c\u5143\u6a39 binary search tree \u4e8c\u53c9\u641c\u7d22\u6811 \u4e8c\u5143\u641c\u5c0b\u6a39 AVL tree AVL \u6811 AVL \u6a39 red-black tree \u7ea2\u9ed1\u6811 \u7d05\u9ed1\u6a39 level-order traversal \u5c42\u5e8f\u904d\u5386 \u5c64\u5e8f\u8d70\u8a2a breadth-first traversal \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 \u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a depth-first traversal \u6df1\u5ea6\u4f18\u5148\u904d\u5386 \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a binary search tree \u4e8c\u53c9\u641c\u7d22\u6811 \u4e8c\u5143\u641c\u5c0b\u6a39 balanced binary search tree \u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811 \u5e73\u8861\u4e8c\u5143\u641c\u5c0b\u6a39 balance factor \u5e73\u8861\u56e0\u5b50 \u5e73\u8861\u56e0\u5b50 heap \u5806 \u5806\u7a4d max heap \u5927\u9876\u5806 \u5927\u9802\u5806\u7a4d min heap \u5c0f\u9876\u5806 \u5c0f\u9802\u5806\u7a4d priority queue \u4f18\u5148\u961f\u5217 \u512a\u5148\u4f47\u5217 heapify \u5806\u5316 \u5806\u7a4d\u5316 top-\\(k\\) problem Top-\\(k\\) \u95ee\u9898 Top-\\(k\\) \u554f\u984c graph \u56fe \u5716 vertex \u9876\u70b9 \u9802\u9ede undirected graph \u65e0\u5411\u56fe \u7121\u5411\u5716 directed graph \u6709\u5411\u56fe \u6709\u5411\u5716 connected graph \u8fde\u901a\u56fe \u9023\u901a\u5716 disconnected graph \u975e\u8fde\u901a\u56fe \u975e\u9023\u901a\u5716 weighted graph \u6709\u6743\u56fe \u6709\u6b0a\u5716 adjacency \u90bb\u63a5 \u9130\u63a5 path \u8def\u5f84 \u8def\u5f91 in-degree \u5165\u5ea6 \u5165\u5ea6 out-degree \u51fa\u5ea6 \u51fa\u5ea6 adjacency matrix \u90bb\u63a5\u77e9\u9635 \u9130\u63a5\u77e9\u9663 adjacency list \u90bb\u63a5\u8868 \u9130\u63a5\u8868 breadth-first search \u5e7f\u5ea6\u4f18\u5148\u641c\u7d22 \u5ee3\u5ea6\u512a\u5148\u641c\u5c0b depth-first search \u6df1\u5ea6\u4f18\u5148\u641c\u7d22 \u6df1\u5ea6\u512a\u5148\u641c\u5c0b binary search \u4e8c\u5206\u67e5\u627e \u4e8c\u5206\u641c\u5c0b searching algorithm \u641c\u7d22\u7b97\u6cd5 \u641c\u5c0b\u6f14\u7b97\u6cd5 sorting algorithm \u6392\u5e8f\u7b97\u6cd5 \u6392\u5e8f\u6f14\u7b97\u6cd5 selection sort \u9009\u62e9\u6392\u5e8f \u9078\u64c7\u6392\u5e8f bubble sort \u5192\u6ce1\u6392\u5e8f \u6ce1\u6cab\u6392\u5e8f insertion sort \u63d2\u5165\u6392\u5e8f \u63d2\u5165\u6392\u5e8f quick sort \u5feb\u901f\u6392\u5e8f \u5feb\u901f\u6392\u5e8f merge sort \u5f52\u5e76\u6392\u5e8f \u5408\u4f75\u6392\u5e8f heap sort \u5806\u6392\u5e8f \u5806\u7a4d\u6392\u5e8f bucket sort \u6876\u6392\u5e8f \u6876\u6392\u5e8f counting sort \u8ba1\u6570\u6392\u5e8f \u8a08\u6578\u6392\u5e8f radix sort \u57fa\u6570\u6392\u5e8f \u57fa\u6578\u6392\u5e8f divide and conquer \u5206\u6cbb \u5206\u6cbb hanota problem \u6c49\u8bfa\u5854\u95ee\u9898 \u6cb3\u5167\u5854\u554f\u984c backtracking algorithm \u56de\u6eaf\u7b97\u6cd5 \u56de\u6eaf\u6f14\u7b97\u6cd5 constraint \u7ea6\u675f \u7d04\u675f solution \u89e3 \u89e3 state \u72b6\u6001 \u72c0\u614b pruning \u526a\u679d \u526a\u679d permutations problem \u5168\u6392\u5217\u95ee\u9898 \u5168\u6392\u5217\u554f\u984c subset-sum problem \u5b50\u96c6\u548c\u95ee\u9898 \u5b50\u96c6\u5408\u554f\u984c \\(n\\)-queens problem \\(n\\) \u7687\u540e\u95ee\u9898 \\(n\\) \u7687\u540e\u554f\u984c dynamic programming \u52a8\u6001\u89c4\u5212 \u52d5\u614b\u898f\u5283 initial state \u521d\u59cb\u72b6\u6001 \u521d\u59cb\u72c0\u614b state-transition equation \u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b \u72c0\u614b\u8f49\u79fb\u65b9\u7a0b knapsack problem \u80cc\u5305\u95ee\u9898 \u80cc\u5305\u554f\u984c edit distance problem \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898 \u7de8\u8f2f\u8ddd\u96e2\u554f\u984c greedy algorithm \u8d2a\u5fc3\u7b97\u6cd5 \u8caa\u5a6a\u6f14\u7b97\u6cd5"},{"location":"chapter_array_and_linkedlist/","title":"Chapter 4. \u00a0 Array and Linked List","text":"

      Abstract

      The world of data structures is like a solid brick wall.

      Array bricks are neatly arranged, tightly packed one by one. Linked list bricks are scattered everywhere, with connecting vines freely weaving through the gaps between bricks.

      "},{"location":"chapter_array_and_linkedlist/#chapter-contents","title":"Chapter contents","text":"
      • 4.1 \u00a0 Array
      • 4.2 \u00a0 Linked List
      • 4.3 \u00a0 List
      • 4.4 \u00a0 Memory and Cache *
      • 4.5 \u00a0 Summary
      "},{"location":"chapter_array_and_linkedlist/array/","title":"4.1 \u00a0 Array","text":"

      An array is a linear data structure that stores elements of the same type in contiguous memory space. The position of an element in the array is called the element's index. Figure 4-1 illustrates the main concepts and storage method of arrays.

      Figure 4-1 \u00a0 Array definition and storage method

      "},{"location":"chapter_array_and_linkedlist/array/#411-common-array-operations","title":"4.1.1 \u00a0 Common Array Operations","text":""},{"location":"chapter_array_and_linkedlist/array/#1-initializing-arrays","title":"1. \u00a0 Initializing Arrays","text":"

      We can choose between two array initialization methods based on our needs: without initial values or with given initial values. When no initial values are specified, most programming languages will initialize array elements to \\(0\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
      # Initialize array\narr: list[int] = [0] * 5  # [ 0, 0, 0, 0, 0 ]\nnums: list[int] = [1, 3, 2, 5, 4]\n
      array.cpp
      /* Initialize array */\n// Stored on stack\nint arr[5];\nint nums[5] = { 1, 3, 2, 5, 4 };\n// Stored on heap (requires manual memory release)\nint* arr1 = new int[5];\nint* nums1 = new int[5] { 1, 3, 2, 5, 4 };\n
      array.java
      /* Initialize array */\nint[] arr = new int[5]; // { 0, 0, 0, 0, 0 }\nint[] nums = { 1, 3, 2, 5, 4 };\n
      array.cs
      /* Initialize array */\nint[] arr = new int[5]; // [ 0, 0, 0, 0, 0 ]\nint[] nums = [1, 3, 2, 5, 4];\n
      array.go
      /* Initialize array */\nvar arr [5]int\n// In Go, specifying length ([5]int) creates an array; not specifying length ([]int) creates a slice\n// Since Go's arrays are designed to have their length determined at compile time, only constants can be used to specify the length\n// For convenience in implementing the extend() method, slices are treated as arrays below\nnums := []int{1, 3, 2, 5, 4}\n
      array.swift
      /* Initialize array */\nlet arr = Array(repeating: 0, count: 5) // [0, 0, 0, 0, 0]\nlet nums = [1, 3, 2, 5, 4]\n
      array.js
      /* Initialize array */\nvar arr = new Array(5).fill(0);\nvar nums = [1, 3, 2, 5, 4];\n
      array.ts
      /* Initialize array */\nlet arr: number[] = new Array(5).fill(0);\nlet nums: number[] = [1, 3, 2, 5, 4];\n
      array.dart
      /* Initialize array */\nList<int> arr = List.filled(5, 0); // [0, 0, 0, 0, 0]\nList<int> nums = [1, 3, 2, 5, 4];\n
      array.rs
      /* Initialize array */\nlet arr: [i32; 5] = [0; 5]; // [0, 0, 0, 0, 0]\nlet slice: &[i32] = &[0; 5];\n// In Rust, specifying length ([i32; 5]) creates an array; not specifying length (&[i32]) creates a slice\n// Since Rust's arrays are designed to have their length determined at compile time, only constants can be used to specify the length\n// Vector is the type generally used as a dynamic array in Rust\n// For convenience in implementing the extend() method, vectors are treated as arrays below\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
      array.c
      /* Initialize array */\nint arr[5] = { 0 }; // { 0, 0, 0, 0, 0 }\nint nums[5] = { 1, 3, 2, 5, 4 };\n
      array.kt
      /* Initialize array */\nvar arr = IntArray(5) // { 0, 0, 0, 0, 0 }\nvar nums = intArrayOf(1, 3, 2, 5, 4)\n
      array.rb
      # Initialize array\narr = Array.new(5, 0)\nnums = [1, 3, 2, 5, 4]\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_array_and_linkedlist/array/#2-accessing-elements","title":"2. \u00a0 Accessing Elements","text":"

      Array elements are stored in contiguous memory space, which means calculating the memory address of array elements is very easy. Given the array's memory address (the memory address of the first element) and an element's index, we can use the formula shown in Figure 4-2 to calculate the element's memory address and directly access that element.

      Figure 4-2 \u00a0 Memory address calculation for array elements

      Observing Figure 4-2, we find that the first element of an array has an index of \\(0\\), which may seem counterintuitive since counting from \\(1\\) would be more natural. However, from the perspective of the address calculation formula, an index is essentially an offset from the memory address. The address offset of the first element is \\(0\\), so it is reasonable for its index to be \\(0\\).

      Accessing elements in an array is highly efficient; we can randomly access any element in the array in \\(O(1)\\) time.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
      def random_access(nums: list[int]) -> int:\n    \"\"\"Random access to element\"\"\"\n    # Randomly select a number from the interval [0, len(nums)-1]\n    random_index = random.randint(0, len(nums) - 1)\n    # Retrieve and return the random element\n    random_num = nums[random_index]\n    return random_num\n
      array.cpp
      /* Random access to element */\nint randomAccess(int *nums, int size) {\n    // Randomly select a number from interval [0, size)\n    int randomIndex = rand() % size;\n    // Retrieve and return the random element\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
      array.java
      /* Random access to element */\nint randomAccess(int[] nums) {\n    // Randomly select a number in the interval [0, nums.length)\n    int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);\n    // Retrieve and return the random element\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
      array.cs
      /* Random access to element */\nint RandomAccess(int[] nums) {\n    Random random = new();\n    // Randomly select a number in interval [0, nums.Length)\n    int randomIndex = random.Next(nums.Length);\n    // Retrieve and return the random element\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
      array.go
      /* Random access to element */\nfunc randomAccess(nums []int) (randomNum int) {\n    // Randomly select a number in the interval [0, nums.length)\n    randomIndex := rand.Intn(len(nums))\n    // Retrieve and return the random element\n    randomNum = nums[randomIndex]\n    return\n}\n
      array.swift
      /* Random access to element */\nfunc randomAccess(nums: [Int]) -> Int {\n    // Randomly select a number in interval [0, nums.count)\n    let randomIndex = nums.indices.randomElement()!\n    // Retrieve and return the random element\n    let randomNum = nums[randomIndex]\n    return randomNum\n}\n
      array.js
      /* Random access to element */\nfunction randomAccess(nums) {\n    // Randomly select a number in the interval [0, nums.length)\n    const random_index = Math.floor(Math.random() * nums.length);\n    // Retrieve and return the random element\n    const random_num = nums[random_index];\n    return random_num;\n}\n
      array.ts
      /* Random access to element */\nfunction randomAccess(nums: number[]): number {\n    // Randomly select a number in the interval [0, nums.length)\n    const random_index = Math.floor(Math.random() * nums.length);\n    // Retrieve and return the random element\n    const random_num = nums[random_index];\n    return random_num;\n}\n
      array.dart
      /* Random access to element */\nint randomAccess(List<int> nums) {\n  // Randomly select a number in the interval [0, nums.length)\n  int randomIndex = Random().nextInt(nums.length);\n  // Retrieve and return the random element\n  int randomNum = nums[randomIndex];\n  return randomNum;\n}\n
      array.rs
      /* Random access to element */\nfn random_access(nums: &[i32]) -> i32 {\n    // Randomly select a number in interval [0, nums.len())\n    let random_index = rand::thread_rng().gen_range(0..nums.len());\n    // Retrieve and return the random element\n    let random_num = nums[random_index];\n    random_num\n}\n
      array.c
      /* Random access to element */\nint randomAccess(int *nums, int size) {\n    // Randomly select a number from interval [0, size)\n    int randomIndex = rand() % size;\n    // Retrieve and return the random element\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
      array.kt
      /* Random access to element */\nfun randomAccess(nums: IntArray): Int {\n    // Randomly select a number in interval [0, nums.size)\n    val randomIndex = ThreadLocalRandom.current().nextInt(0, nums.size)\n    // Retrieve and return the random element\n    val randomNum = nums[randomIndex]\n    return randomNum\n}\n
      array.rb
      ### Random access element ###\ndef random_access(nums)\n  # Randomly select a number in the interval [0, nums.length)\n  random_index = Random.rand(0...nums.length)\n\n  # Retrieve and return the random element\n  nums[random_index]\nend\n
      "},{"location":"chapter_array_and_linkedlist/array/#3-inserting-elements","title":"3. \u00a0 Inserting Elements","text":"

      Array elements are stored \"tightly adjacent\" in memory, with no space between them to store any additional data. As shown in Figure 4-3, if we want to insert an element in the middle of an array, we need to shift all elements after that position backward by one position, and then assign the value to that index.

      Figure 4-3 \u00a0 Example of inserting an element into an array

      It is worth noting that since the length of an array is fixed, inserting an element will inevitably cause the element at the end of the array to be \"lost\". We will leave the solution to this problem for discussion in the \"List\" chapter.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
      def insert(nums: list[int], num: int, index: int):\n    \"\"\"Insert element num at index index in the array\"\"\"\n    # Move all elements at and after index index backward by one position\n    for i in range(len(nums) - 1, index, -1):\n        nums[i] = nums[i - 1]\n    # Assign num to the element at index index\n    nums[index] = num\n
      array.cpp
      /* Insert element num at index index in the array */\nvoid insert(int *nums, int size, int num, int index) {\n    // Move all elements at and after index index backward by one position\n    for (int i = size - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // Assign num to the element at index index\n    nums[index] = num;\n}\n
      array.java
      /* Insert element num at index index in the array */\nvoid insert(int[] nums, int num, int index) {\n    // Move all elements at and after index index backward by one position\n    for (int i = nums.length - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // Assign num to the element at index index\n    nums[index] = num;\n}\n
      array.cs
      /* Insert element num at index index in the array */\nvoid Insert(int[] nums, int num, int index) {\n    // Move all elements at and after index index backward by one position\n    for (int i = nums.Length - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // Assign num to the element at index index\n    nums[index] = num;\n}\n
      array.go
      /* Insert element num at index index in the array */\nfunc insert(nums []int, num int, index int) {\n    // Move all elements at and after index index backward by one position\n    for i := len(nums) - 1; i > index; i-- {\n        nums[i] = nums[i-1]\n    }\n    // Assign num to the element at index index\n    nums[index] = num\n}\n
      array.swift
      /* Insert element num at index index in the array */\nfunc insert(nums: inout [Int], num: Int, index: Int) {\n    // Move all elements at and after index index backward by one position\n    for i in nums.indices.dropFirst(index).reversed() {\n        nums[i] = nums[i - 1]\n    }\n    // Assign num to the element at index index\n    nums[index] = num\n}\n
      array.js
      /* Insert element num at index index in the array */\nfunction insert(nums, num, index) {\n    // Move all elements at and after index index backward by one position\n    for (let i = nums.length - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // Assign num to the element at index index\n    nums[index] = num;\n}\n
      array.ts
      /* Insert element num at index index in the array */\nfunction insert(nums: number[], num: number, index: number): void {\n    // Move all elements at and after index index backward by one position\n    for (let i = nums.length - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // Assign num to the element at index index\n    nums[index] = num;\n}\n
      array.dart
      /* Insert element _num at array index index */\nvoid insert(List<int> nums, int _num, int index) {\n  // Move all elements at and after index index backward by one position\n  for (var i = nums.length - 1; i > index; i--) {\n    nums[i] = nums[i - 1];\n  }\n  // Assign _num to element at index\n  nums[index] = _num;\n}\n
      array.rs
      /* Insert element num at index index in the array */\nfn insert(nums: &mut [i32], num: i32, index: usize) {\n    // Move all elements at and after index index backward by one position\n    for i in (index + 1..nums.len()).rev() {\n        nums[i] = nums[i - 1];\n    }\n    // Assign num to the element at index index\n    nums[index] = num;\n}\n
      array.c
      /* Insert element num at index index in the array */\nvoid insert(int *nums, int size, int num, int index) {\n    // Move all elements at and after index index backward by one position\n    for (int i = size - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // Assign num to the element at index index\n    nums[index] = num;\n}\n
      array.kt
      /* Insert element num at index index in the array */\nfun insert(nums: IntArray, num: Int, index: Int) {\n    // Move all elements at and after index index backward by one position\n    for (i in nums.size - 1 downTo index + 1) {\n        nums[i] = nums[i - 1]\n    }\n    // Assign num to the element at index index\n    nums[index] = num\n}\n
      array.rb
      ### Insert element num at index in array ###\ndef insert(nums, num, index)\n  # Move all elements at and after index index backward by one position\n  for i in (nums.length - 1).downto(index + 1)\n    nums[i] = nums[i - 1]\n  end\n\n  # Assign num to the element at index index\n  nums[index] = num\nend\n
      "},{"location":"chapter_array_and_linkedlist/array/#4-removing-elements","title":"4. \u00a0 Removing Elements","text":"

      Similarly, as shown in Figure 4-4, to delete the element at index \\(i\\), we need to shift all elements after index \\(i\\) forward by one position.

      Figure 4-4 \u00a0 Example of removing an element from an array

      Note that after the deletion is complete, the original last element becomes \"meaningless\", so we do not need to specifically modify it.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
      def remove(nums: list[int], index: int):\n    \"\"\"Remove the element at index index\"\"\"\n    # Move all elements after index index forward by one position\n    for i in range(index, len(nums) - 1):\n        nums[i] = nums[i + 1]\n
      array.cpp
      /* Remove the element at index index */\nvoid remove(int *nums, int size, int index) {\n    // Move all elements after index index forward by one position\n    for (int i = index; i < size - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
      array.java
      /* Remove the element at index index */\nvoid remove(int[] nums, int index) {\n    // Move all elements after index index forward by one position\n    for (int i = index; i < nums.length - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
      array.cs
      /* Remove the element at index index */\nvoid Remove(int[] nums, int index) {\n    // Move all elements after index index forward by one position\n    for (int i = index; i < nums.Length - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
      array.go
      /* Remove the element at index index */\nfunc remove(nums []int, index int) {\n    // Move all elements after index index forward by one position\n    for i := index; i < len(nums)-1; i++ {\n        nums[i] = nums[i+1]\n    }\n}\n
      array.swift
      /* Remove the element at index index */\nfunc remove(nums: inout [Int], index: Int) {\n    // Move all elements after index index forward by one position\n    for i in nums.indices.dropFirst(index).dropLast() {\n        nums[i] = nums[i + 1]\n    }\n}\n
      array.js
      /* Remove the element at index index */\nfunction remove(nums, index) {\n    // Move all elements after index index forward by one position\n    for (let i = index; i < nums.length - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
      array.ts
      /* Remove the element at index index */\nfunction remove(nums: number[], index: number): void {\n    // Move all elements after index index forward by one position\n    for (let i = index; i < nums.length - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
      array.dart
      /* Remove the element at index index */\nvoid remove(List<int> nums, int index) {\n  // Move all elements after index index forward by one position\n  for (var i = index; i < nums.length - 1; i++) {\n    nums[i] = nums[i + 1];\n  }\n}\n
      array.rs
      /* Remove the element at index index */\nfn remove(nums: &mut [i32], index: usize) {\n    // Move all elements after index index forward by one position\n    for i in index..nums.len() - 1 {\n        nums[i] = nums[i + 1];\n    }\n}\n
      array.c
      /* Remove the element at index index */\n// Note: stdio.h occupies the remove keyword\nvoid removeItem(int *nums, int size, int index) {\n    // Move all elements after index index forward by one position\n    for (int i = index; i < size - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
      array.kt
      /* Remove the element at index index */\nfun remove(nums: IntArray, index: Int) {\n    // Move all elements after index index forward by one position\n    for (i in index..<nums.size - 1) {\n        nums[i] = nums[i + 1]\n    }\n}\n
      array.rb
      ### Delete element at index ###\ndef remove(nums, index)\n  # Move all elements after index index forward by one position\n  for i in index...(nums.length - 1)\n    nums[i] = nums[i + 1]\n  end\nend\n

      Overall, array insertion and deletion operations have the following drawbacks:

      • High time complexity: The average time complexity for both insertion and deletion in arrays is \\(O(n)\\), where \\(n\\) is the length of the array.
      • Loss of elements: Since the length of an array is immutable, after inserting an element, elements that exceed the array's length will be lost.
      • Memory waste: We can initialize a relatively long array and only use the front portion, so that when inserting data, the lost elements at the end are \"meaningless\", but this causes some memory space to be wasted.
      "},{"location":"chapter_array_and_linkedlist/array/#5-traversing-arrays","title":"5. \u00a0 Traversing Arrays","text":"

      In most programming languages, we can traverse an array either by index or by directly iterating through each element in the array:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
      def traverse(nums: list[int]):\n    \"\"\"Traverse array\"\"\"\n    count = 0\n    # Traverse array by index\n    for i in range(len(nums)):\n        count += nums[i]\n    # Direct traversal of array elements\n    for num in nums:\n        count += num\n    # Traverse simultaneously data index and elements\n    for i, num in enumerate(nums):\n        count += nums[i]\n        count += num\n
      array.cpp
      /* Traverse array */\nvoid traverse(int *nums, int size) {\n    int count = 0;\n    // Traverse array by index\n    for (int i = 0; i < size; i++) {\n        count += nums[i];\n    }\n}\n
      array.java
      /* Traverse array */\nvoid traverse(int[] nums) {\n    int count = 0;\n    // Traverse array by index\n    for (int i = 0; i < nums.length; i++) {\n        count += nums[i];\n    }\n    // Direct traversal of array elements\n    for (int num : nums) {\n        count += num;\n    }\n}\n
      array.cs
      /* Traverse array */\nvoid Traverse(int[] nums) {\n    int count = 0;\n    // Traverse array by index\n    for (int i = 0; i < nums.Length; i++) {\n        count += nums[i];\n    }\n    // Direct traversal of array elements\n    foreach (int num in nums) {\n        count += num;\n    }\n}\n
      array.go
      /* Traverse array */\nfunc traverse(nums []int) {\n    count := 0\n    // Traverse array by index\n    for i := 0; i < len(nums); i++ {\n        count += nums[i]\n    }\n    count = 0\n    // Direct traversal of array elements\n    for _, num := range nums {\n        count += num\n    }\n    // Traverse simultaneously data index and elements\n    for i, num := range nums {\n        count += nums[i]\n        count += num\n    }\n}\n
      array.swift
      /* Traverse array */\nfunc traverse(nums: [Int]) {\n    var count = 0\n    // Traverse array by index\n    for i in nums.indices {\n        count += nums[i]\n    }\n    // Direct traversal of array elements\n    for num in nums {\n        count += num\n    }\n    // Traverse simultaneously data index and elements\n    for (i, num) in nums.enumerated() {\n        count += nums[i]\n        count += num\n    }\n}\n
      array.js
      /* Traverse array */\nfunction traverse(nums) {\n    let count = 0;\n    // Traverse array by index\n    for (let i = 0; i < nums.length; i++) {\n        count += nums[i];\n    }\n    // Direct traversal of array elements\n    for (const num of nums) {\n        count += num;\n    }\n}\n
      array.ts
      /* Traverse array */\nfunction traverse(nums: number[]): void {\n    let count = 0;\n    // Traverse array by index\n    for (let i = 0; i < nums.length; i++) {\n        count += nums[i];\n    }\n    // Direct traversal of array elements\n    for (const num of nums) {\n        count += num;\n    }\n}\n
      array.dart
      /* Traverse array elements */\nvoid traverse(List<int> nums) {\n  int count = 0;\n  // Traverse array by index\n  for (var i = 0; i < nums.length; i++) {\n    count += nums[i];\n  }\n  // Direct traversal of array elements\n  for (int _num in nums) {\n    count += _num;\n  }\n  // Traverse array using forEach method\n  nums.forEach((_num) {\n    count += _num;\n  });\n}\n
      array.rs
      /* Traverse array */\nfn traverse(nums: &[i32]) {\n    let mut _count = 0;\n    // Traverse array by index\n    for i in 0..nums.len() {\n        _count += nums[i];\n    }\n    // Direct traversal of array elements\n    _count = 0;\n    for &num in nums {\n        _count += num;\n    }\n}\n
      array.c
      /* Traverse array */\nvoid traverse(int *nums, int size) {\n    int count = 0;\n    // Traverse array by index\n    for (int i = 0; i < size; i++) {\n        count += nums[i];\n    }\n}\n
      array.kt
      /* Traverse array */\nfun traverse(nums: IntArray) {\n    var count = 0\n    // Traverse array by index\n    for (i in nums.indices) {\n        count += nums[i]\n    }\n    // Direct traversal of array elements\n    for (j in nums) {\n        count += j\n    }\n}\n
      array.rb
      ### Traverse array ###\ndef traverse(nums)\n  count = 0\n\n  # Traverse array by index\n  for i in 0...nums.length\n    count += nums[i]\n  end\n\n  # Direct traversal of array elements\n  for num in nums\n    count += num\n  end\nend\n
      "},{"location":"chapter_array_and_linkedlist/array/#6-finding-elements","title":"6. \u00a0 Finding Elements","text":"

      Finding a specified element in an array requires traversing the array and checking whether the element value matches in each iteration; if it matches, output the corresponding index.

      Since an array is a linear data structure, the above search operation is called a \"linear search\".

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
      def find(nums: list[int], target: int) -> int:\n    \"\"\"Find the specified element in the array\"\"\"\n    for i in range(len(nums)):\n        if nums[i] == target:\n            return i\n    return -1\n
      array.cpp
      /* Find the specified element in the array */\nint find(int *nums, int size, int target) {\n    for (int i = 0; i < size; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
      array.java
      /* Find the specified element in the array */\nint find(int[] nums, int target) {\n    for (int i = 0; i < nums.length; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
      array.cs
      /* Find the specified element in the array */\nint Find(int[] nums, int target) {\n    for (int i = 0; i < nums.Length; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
      array.go
      /* Find the specified element in the array */\nfunc find(nums []int, target int) (index int) {\n    index = -1\n    for i := 0; i < len(nums); i++ {\n        if nums[i] == target {\n            index = i\n            break\n        }\n    }\n    return\n}\n
      array.swift
      /* Find the specified element in the array */\nfunc find(nums: [Int], target: Int) -> Int {\n    for i in nums.indices {\n        if nums[i] == target {\n            return i\n        }\n    }\n    return -1\n}\n
      array.js
      /* Find the specified element in the array */\nfunction find(nums, target) {\n    for (let i = 0; i < nums.length; i++) {\n        if (nums[i] === target) return i;\n    }\n    return -1;\n}\n
      array.ts
      /* Find the specified element in the array */\nfunction find(nums: number[], target: number): number {\n    for (let i = 0; i < nums.length; i++) {\n        if (nums[i] === target) {\n            return i;\n        }\n    }\n    return -1;\n}\n
      array.dart
      /* Find the specified element in the array */\nint find(List<int> nums, int target) {\n  for (var i = 0; i < nums.length; i++) {\n    if (nums[i] == target) return i;\n  }\n  return -1;\n}\n
      array.rs
      /* Find the specified element in the array */\nfn find(nums: &[i32], target: i32) -> Option<usize> {\n    for i in 0..nums.len() {\n        if nums[i] == target {\n            return Some(i);\n        }\n    }\n    None\n}\n
      array.c
      /* Find the specified element in the array */\nint find(int *nums, int size, int target) {\n    for (int i = 0; i < size; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
      array.kt
      /* Find the specified element in the array */\nfun find(nums: IntArray, target: Int): Int {\n    for (i in nums.indices) {\n        if (nums[i] == target)\n            return i\n    }\n    return -1\n}\n
      array.rb
      ### Find specified element in array ###\ndef find(nums, target)\n  for i in 0...nums.length\n    return i if nums[i] == target\n  end\n\n  -1\nend\n
      "},{"location":"chapter_array_and_linkedlist/array/#7-expanding-arrays","title":"7. \u00a0 Expanding Arrays","text":"

      In complex system environments, programs cannot guarantee that the memory space after an array is available, making it unsafe to expand the array's capacity. Therefore, in most programming languages, the length of an array is immutable.

      If we want to expand an array, we need to create a new, larger array and then copy the original array elements to the new array one by one. This is an \\(O(n)\\) operation, which is very time-consuming when the array is large. The code is shown below:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
      def extend(nums: list[int], enlarge: int) -> list[int]:\n    \"\"\"Extend array length\"\"\"\n    # Initialize an array with extended length\n    res = [0] * (len(nums) + enlarge)\n    # Copy all elements from the original array to the new array\n    for i in range(len(nums)):\n        res[i] = nums[i]\n    # Return the extended new array\n    return res\n
      array.cpp
      /* Extend array length */\nint *extend(int *nums, int size, int enlarge) {\n    // Initialize an array with extended length\n    int *res = new int[size + enlarge];\n    // Copy all elements from the original array to the new array\n    for (int i = 0; i < size; i++) {\n        res[i] = nums[i];\n    }\n    // Free memory\n    delete[] nums;\n    // Return the extended new array\n    return res;\n}\n
      array.java
      /* Extend array length */\nint[] extend(int[] nums, int enlarge) {\n    // Initialize an array with extended length\n    int[] res = new int[nums.length + enlarge];\n    // Copy all elements from the original array to the new array\n    for (int i = 0; i < nums.length; i++) {\n        res[i] = nums[i];\n    }\n    // Return the extended new array\n    return res;\n}\n
      array.cs
      /* Extend array length */\nint[] Extend(int[] nums, int enlarge) {\n    // Initialize an array with extended length\n    int[] res = new int[nums.Length + enlarge];\n    // Copy all elements from the original array to the new array\n    for (int i = 0; i < nums.Length; i++) {\n        res[i] = nums[i];\n    }\n    // Return the extended new array\n    return res;\n}\n
      array.go
      /* Extend array length */\nfunc extend(nums []int, enlarge int) []int {\n    // Initialize an array with extended length\n    res := make([]int, len(nums)+enlarge)\n    // Copy all elements from the original array to the new array\n    for i, num := range nums {\n        res[i] = num\n    }\n    // Return the extended new array\n    return res\n}\n
      array.swift
      /* Extend array length */\nfunc extend(nums: [Int], enlarge: Int) -> [Int] {\n    // Initialize an array with extended length\n    var res = Array(repeating: 0, count: nums.count + enlarge)\n    // Copy all elements from the original array to the new array\n    for i in nums.indices {\n        res[i] = nums[i]\n    }\n    // Return the extended new array\n    return res\n}\n
      array.js
      /* Extend array length */\n// Note: JavaScript's Array is dynamic array, can be directly expanded\n// For learning purposes, this function treats Array as fixed-length array\nfunction extend(nums, enlarge) {\n    // Initialize an array with extended length\n    const res = new Array(nums.length + enlarge).fill(0);\n    // Copy all elements from the original array to the new array\n    for (let i = 0; i < nums.length; i++) {\n        res[i] = nums[i];\n    }\n    // Return the extended new array\n    return res;\n}\n
      array.ts
      /* Extend array length */\n// Note: TypeScript's Array is dynamic array, can be directly expanded\n// For learning purposes, this function treats Array as fixed-length array\nfunction extend(nums: number[], enlarge: number): number[] {\n    // Initialize an array with extended length\n    const res = new Array(nums.length + enlarge).fill(0);\n    // Copy all elements from the original array to the new array\n    for (let i = 0; i < nums.length; i++) {\n        res[i] = nums[i];\n    }\n    // Return the extended new array\n    return res;\n}\n
      array.dart
      /* Extend array length */\nList<int> extend(List<int> nums, int enlarge) {\n  // Initialize an array with extended length\n  List<int> res = List.filled(nums.length + enlarge, 0);\n  // Copy all elements from the original array to the new array\n  for (var i = 0; i < nums.length; i++) {\n    res[i] = nums[i];\n  }\n  // Return the extended new array\n  return res;\n}\n
      array.rs
      /* Extend array length */\nfn extend(nums: &[i32], enlarge: usize) -> Vec<i32> {\n    // Initialize an array with extended length\n    let mut res: Vec<i32> = vec![0; nums.len() + enlarge];\n    // Copy all elements from original array to new\n    res[0..nums.len()].copy_from_slice(nums);\n\n    // Return the extended new array\n    res\n}\n
      array.c
      /* Extend array length */\nint *extend(int *nums, int size, int enlarge) {\n    // Initialize an array with extended length\n    int *res = (int *)malloc(sizeof(int) * (size + enlarge));\n    // Copy all elements from the original array to the new array\n    for (int i = 0; i < size; i++) {\n        res[i] = nums[i];\n    }\n    // Initialize expanded space\n    for (int i = size; i < size + enlarge; i++) {\n        res[i] = 0;\n    }\n    // Return the extended new array\n    return res;\n}\n
      array.kt
      /* Extend array length */\nfun extend(nums: IntArray, enlarge: Int): IntArray {\n    // Initialize an array with extended length\n    val res = IntArray(nums.size + enlarge)\n    // Copy all elements from the original array to the new array\n    for (i in nums.indices) {\n        res[i] = nums[i]\n    }\n    // Return the extended new array\n    return res\n}\n
      array.rb
      ### Extend array length ###\n# Note: Ruby's Array is dynamic array, can be directly expanded\n# For learning purposes, this function treats Array as fixed-length array\ndef extend(nums, enlarge)\n  # Initialize an array with extended length\n  res = Array.new(nums.length + enlarge, 0)\n\n  # Copy all elements from the original array to the new array\n  for i in 0...nums.length\n    res[i] = nums[i]\n  end\n\n  # Return the extended new array\n  res\nend\n
      "},{"location":"chapter_array_and_linkedlist/array/#412-advantages-and-limitations-of-arrays","title":"4.1.2 \u00a0 Advantages and Limitations of Arrays","text":"

      Arrays are stored in contiguous memory space with elements of the same type. This approach contains rich prior information that the system can use to optimize the efficiency of data structure operations.

      • High space efficiency: Arrays allocate contiguous memory blocks for data without additional structural overhead.
      • Support for random access: Arrays allow accessing any element in \\(O(1)\\) time.
      • Cache locality: When accessing array elements, the computer not only loads the element but also caches the surrounding data, thereby leveraging the cache to improve the execution speed of subsequent operations.

      Contiguous space storage is a double-edged sword with the following limitations:

      • Low insertion and deletion efficiency: When an array has many elements, insertion and deletion operations require shifting a large number of elements.
      • Immutable length: After an array is initialized, its length is fixed. Expanding the array requires copying all data to a new array, which is very costly.
      • Space waste: If the allocated size of an array exceeds what is actually needed, the extra space is wasted.
      "},{"location":"chapter_array_and_linkedlist/array/#413-typical-applications-of-arrays","title":"4.1.3 \u00a0 Typical Applications of Arrays","text":"

      Arrays are a fundamental and common data structure, frequently used in various algorithms and for implementing various complex data structures.

      • Random access: If we want to randomly sample some items, we can use an array to store them and generate a random sequence to implement random sampling based on indices.
      • Sorting and searching: Arrays are the most commonly used data structure for sorting and searching algorithms. Quick sort, merge sort, binary search, and others are primarily performed on arrays.
      • Lookup tables: When we need to quickly find an element or its corresponding relationship, we can use an array as a lookup table. For example, if we want to implement a mapping from characters to ASCII codes, we can use the ASCII code value of a character as an index, with the corresponding element stored at that position in the array.
      • Machine learning: Neural networks make extensive use of linear algebra operations between vectors, matrices, and tensors, all of which are constructed in the form of arrays. Arrays are the most commonly used data structure in neural network programming.
      • Data structure implementation: Arrays can be used to implement stacks, queues, hash tables, heaps, graphs, and other data structures. For example, the adjacency matrix representation of a graph is essentially a two-dimensional array.
      "},{"location":"chapter_array_and_linkedlist/linked_list/","title":"4.2 \u00a0 Linked List","text":"

      Memory space is a shared resource for all programs. In a complex system runtime environment, available memory space may be scattered throughout the memory. We know that the memory space for storing an array must be contiguous, and when the array is very large, the memory may not be able to provide such a large contiguous space. This is where the flexibility advantage of linked lists becomes apparent.

      A linked list is a linear data structure in which each element is a node object, and the nodes are connected through \"references\". A reference records the memory address of the next node, through which the next node can be accessed from the current node.

      The design of linked lists allows nodes to be stored scattered throughout the memory, and their memory addresses do not need to be contiguous.

      Figure 4-5 \u00a0 Linked list definition and storage method

      Observing Figure 4-5, the basic unit of a linked list is a node object. Each node contains two pieces of data: the node's \"value\" and a \"reference\" to the next node.

      • The first node of a linked list is called the \"head node\", and the last node is called the \"tail node\".
      • The tail node points to \"null\", which is denoted as null, nullptr, and None in Java, C++, and Python, respectively.
      • In languages that support pointers, such as C, C++, Go, and Rust, the aforementioned \"reference\" should be replaced with \"pointer\".

      As shown in the following code, a linked list node ListNode contains not only a value but also an additional reference (pointer). Therefore, linked lists occupy more memory space than arrays when storing the same amount of data.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      class ListNode:\n    \"\"\"Linked list node class\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val               # Node value\n        self.next: ListNode | None = None # Reference to the next node\n
      /* Linked list node structure */\nstruct ListNode {\n    int val;         // Node value\n    ListNode *next;  // Pointer to the next node\n    ListNode(int x) : val(x), next(nullptr) {}  // Constructor\n};\n
      /* Linked list node class */\nclass ListNode {\n    int val;        // Node value\n    ListNode next;  // Reference to the next node\n    ListNode(int x) { val = x; }  // Constructor\n}\n
      /* Linked list node class */\nclass ListNode(int x) {  // Constructor\n    int val = x;         // Node value\n    ListNode? next;      // Reference to the next node\n}\n
      /* Linked list node structure */\ntype ListNode struct {\n    Val  int       // Node value\n    Next *ListNode // Pointer to the next node\n}\n\n// NewListNode Constructor, creates a new linked list\nfunc NewListNode(val int) *ListNode {\n    return &ListNode{\n        Val:  val,\n        Next: nil,\n    }\n}\n
      /* Linked list node class */\nclass ListNode {\n    var val: Int // Node value\n    var next: ListNode? // Reference to the next node\n\n    init(x: Int) { // Constructor\n        val = x\n    }\n}\n
      /* Linked list node class */\nclass ListNode {\n    constructor(val, next) {\n        this.val = (val === undefined ? 0 : val);       // Node value\n        this.next = (next === undefined ? null : next); // Reference to the next node\n    }\n}\n
      /* Linked list node class */\nclass ListNode {\n    val: number;\n    next: ListNode | null;\n    constructor(val?: number, next?: ListNode | null) {\n        this.val = val === undefined ? 0 : val;        // Node value\n        this.next = next === undefined ? null : next;  // Reference to the next node\n    }\n}\n
      /* Linked list node class */\nclass ListNode {\n  int val; // Node value\n  ListNode? next; // Reference to the next node\n  ListNode(this.val, [this.next]); // Constructor\n}\n
      use std::rc::Rc;\nuse std::cell::RefCell;\n/* Linked list node class */\n#[derive(Debug)]\nstruct ListNode {\n    val: i32, // Node value\n    next: Option<Rc<RefCell<ListNode>>>, // Pointer to the next node\n}\n
      /* Linked list node structure */\ntypedef struct ListNode {\n    int val;               // Node value\n    struct ListNode *next; // Pointer to the next node\n} ListNode;\n\n/* Constructor */\nListNode *newListNode(int val) {\n    ListNode *node;\n    node = (ListNode *) malloc(sizeof(ListNode));\n    node->val = val;\n    node->next = NULL;\n    return node;\n}\n
      /* Linked list node class */\n// Constructor\nclass ListNode(x: Int) {\n    val _val: Int = x          // Node value\n    val next: ListNode? = null // Reference to the next node\n}\n
      # Linked list node class\nclass ListNode\n  attr_accessor :val  # Node value\n  attr_accessor :next # Reference to the next node\n\n  def initialize(val=0, next_node=nil)\n    @val = val\n    @next = next_node\n  end\nend\n
      "},{"location":"chapter_array_and_linkedlist/linked_list/#421-common-linked-list-operations","title":"4.2.1 \u00a0 Common Linked List Operations","text":""},{"location":"chapter_array_and_linkedlist/linked_list/#1-initializing-a-linked-list","title":"1. \u00a0 Initializing a Linked List","text":"

      Building a linked list involves two steps: first, initializing each node object; second, constructing the reference relationships between nodes. Once initialization is complete, we can traverse all nodes starting from the head node of the linked list through the reference next.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
      # Initialize linked list 1 -> 3 -> 2 -> 5 -> 4\n# Initialize each node\nn0 = ListNode(1)\nn1 = ListNode(3)\nn2 = ListNode(2)\nn3 = ListNode(5)\nn4 = ListNode(4)\n# Build references between nodes\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
      linked_list.cpp
      /* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nListNode* n0 = new ListNode(1);\nListNode* n1 = new ListNode(3);\nListNode* n2 = new ListNode(2);\nListNode* n3 = new ListNode(5);\nListNode* n4 = new ListNode(4);\n// Build references between nodes\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
      linked_list.java
      /* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nListNode n0 = new ListNode(1);\nListNode n1 = new ListNode(3);\nListNode n2 = new ListNode(2);\nListNode n3 = new ListNode(5);\nListNode n4 = new ListNode(4);\n// Build references between nodes\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
      linked_list.cs
      /* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nListNode n0 = new(1);\nListNode n1 = new(3);\nListNode n2 = new(2);\nListNode n3 = new(5);\nListNode n4 = new(4);\n// Build references between nodes\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
      linked_list.go
      /* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nn0 := NewListNode(1)\nn1 := NewListNode(3)\nn2 := NewListNode(2)\nn3 := NewListNode(5)\nn4 := NewListNode(4)\n// Build references between nodes\nn0.Next = n1\nn1.Next = n2\nn2.Next = n3\nn3.Next = n4\n
      linked_list.swift
      /* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nlet n0 = ListNode(x: 1)\nlet n1 = ListNode(x: 3)\nlet n2 = ListNode(x: 2)\nlet n3 = ListNode(x: 5)\nlet n4 = ListNode(x: 4)\n// Build references between nodes\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
      linked_list.js
      /* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// Build references between nodes\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
      linked_list.ts
      /* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// Build references between nodes\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
      linked_list.dart
      /* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */\\\n// Initialize each node\nListNode n0 = ListNode(1);\nListNode n1 = ListNode(3);\nListNode n2 = ListNode(2);\nListNode n3 = ListNode(5);\nListNode n4 = ListNode(4);\n// Build references between nodes\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
      linked_list.rs
      /* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nlet n0 = Rc::new(RefCell::new(ListNode { val: 1, next: None }));\nlet n1 = Rc::new(RefCell::new(ListNode { val: 3, next: None }));\nlet n2 = Rc::new(RefCell::new(ListNode { val: 2, next: None }));\nlet n3 = Rc::new(RefCell::new(ListNode { val: 5, next: None }));\nlet n4 = Rc::new(RefCell::new(ListNode { val: 4, next: None }));\n\n// Build references between nodes\nn0.borrow_mut().next = Some(n1.clone());\nn1.borrow_mut().next = Some(n2.clone());\nn2.borrow_mut().next = Some(n3.clone());\nn3.borrow_mut().next = Some(n4.clone());\n
      linked_list.c
      /* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nListNode* n0 = newListNode(1);\nListNode* n1 = newListNode(3);\nListNode* n2 = newListNode(2);\nListNode* n3 = newListNode(5);\nListNode* n4 = newListNode(4);\n// Build references between nodes\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
      linked_list.kt
      /* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nval n0 = ListNode(1)\nval n1 = ListNode(3)\nval n2 = ListNode(2)\nval n3 = ListNode(5)\nval n4 = ListNode(4)\n// Build references between nodes\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
      linked_list.rb
      # Initialize linked list 1 -> 3 -> 2 -> 5 -> 4\n# Initialize each node\nn0 = ListNode.new(1)\nn1 = ListNode.new(3)\nn2 = ListNode.new(2)\nn3 = ListNode.new(5)\nn4 = ListNode.new(4)\n# Build references between nodes\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
      Code Visualization

      Full Screen >

      An array is a single variable; for example, an array nums contains elements nums[0], nums[1], etc. A linked list, however, is composed of multiple independent node objects. We typically use the head node as the reference to the linked list; for example, the linked list in the above code can be referred to as linked list n0.

      "},{"location":"chapter_array_and_linkedlist/linked_list/#2-inserting-a-node","title":"2. \u00a0 Inserting a Node","text":"

      Inserting a node in a linked list is very easy. As shown in Figure 4-6, suppose we want to insert a new node P between two adjacent nodes n0 and n1. We only need to change two node references (pointers), with a time complexity of \\(O(1)\\).

      In contrast, the time complexity of inserting an element in an array is \\(O(n)\\), which is inefficient when dealing with large amounts of data.

      Figure 4-6 \u00a0 Example of inserting a node into a linked list

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
      def insert(n0: ListNode, P: ListNode):\n    \"\"\"Insert node P after node n0 in the linked list\"\"\"\n    n1 = n0.next\n    P.next = n1\n    n0.next = P\n
      linked_list.cpp
      /* Insert node P after node n0 in the linked list */\nvoid insert(ListNode *n0, ListNode *P) {\n    ListNode *n1 = n0->next;\n    P->next = n1;\n    n0->next = P;\n}\n
      linked_list.java
      /* Insert node P after node n0 in the linked list */\nvoid insert(ListNode n0, ListNode P) {\n    ListNode n1 = n0.next;\n    P.next = n1;\n    n0.next = P;\n}\n
      linked_list.cs
      /* Insert node P after node n0 in the linked list */\nvoid Insert(ListNode n0, ListNode P) {\n    ListNode? n1 = n0.next;\n    P.next = n1;\n    n0.next = P;\n}\n
      linked_list.go
      /* Insert node P after node n0 in the linked list */\nfunc insertNode(n0 *ListNode, P *ListNode) {\n    n1 := n0.Next\n    P.Next = n1\n    n0.Next = P\n}\n
      linked_list.swift
      /* Insert node P after node n0 in the linked list */\nfunc insert(n0: ListNode, P: ListNode) {\n    let n1 = n0.next\n    P.next = n1\n    n0.next = P\n}\n
      linked_list.js
      /* Insert node P after node n0 in the linked list */\nfunction insert(n0, P) {\n    const n1 = n0.next;\n    P.next = n1;\n    n0.next = P;\n}\n
      linked_list.ts
      /* Insert node P after node n0 in the linked list */\nfunction insert(n0: ListNode, P: ListNode): void {\n    const n1 = n0.next;\n    P.next = n1;\n    n0.next = P;\n}\n
      linked_list.dart
      /* Insert node P after node n0 in the linked list */\nvoid insert(ListNode n0, ListNode P) {\n  ListNode? n1 = n0.next;\n  P.next = n1;\n  n0.next = P;\n}\n
      linked_list.rs
      /* Insert node P after node n0 in the linked list */\n#[allow(non_snake_case)]\npub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) {\n    let n1 = n0.borrow_mut().next.take();\n    P.borrow_mut().next = n1;\n    n0.borrow_mut().next = Some(P);\n}\n
      linked_list.c
      /* Insert node P after node n0 in the linked list */\nvoid insert(ListNode *n0, ListNode *P) {\n    ListNode *n1 = n0->next;\n    P->next = n1;\n    n0->next = P;\n}\n
      linked_list.kt
      /* Insert node P after node n0 in the linked list */\nfun insert(n0: ListNode?, p: ListNode?) {\n    val n1 = n0?.next\n    p?.next = n1\n    n0?.next = p\n}\n
      linked_list.rb
      ### Insert node _p after node n0 in linked list ###\n# Ruby's `p` is a built-in function, `P` is a constant, so use `_p` instead\ndef insert(n0, _p)\n  n1 = n0.next\n  _p.next = n1\n  n0.next = _p\nend\n
      "},{"location":"chapter_array_and_linkedlist/linked_list/#3-removing-a-node","title":"3. \u00a0 Removing a Node","text":"

      As shown in Figure 4-7, removing a node in a linked list is also very convenient. We only need to change one node's reference (pointer).

      Note that although node P still points to n1 after the deletion operation is complete, the linked list can no longer access P when traversing, which means P no longer belongs to this linked list.

      Figure 4-7 \u00a0 Removing a node from a linked list

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
      def remove(n0: ListNode):\n    \"\"\"Remove the first node after node n0 in the linked list\"\"\"\n    if not n0.next:\n        return\n    # n0 -> P -> n1\n    P = n0.next\n    n1 = P.next\n    n0.next = n1\n
      linked_list.cpp
      /* Remove the first node after node n0 in the linked list */\nvoid remove(ListNode *n0) {\n    if (n0->next == nullptr)\n        return;\n    // n0 -> P -> n1\n    ListNode *P = n0->next;\n    ListNode *n1 = P->next;\n    n0->next = n1;\n    // Free memory\n    delete P;\n}\n
      linked_list.java
      /* Remove the first node after node n0 in the linked list */\nvoid remove(ListNode n0) {\n    if (n0.next == null)\n        return;\n    // n0 -> P -> n1\n    ListNode P = n0.next;\n    ListNode n1 = P.next;\n    n0.next = n1;\n}\n
      linked_list.cs
      /* Remove the first node after node n0 in the linked list */\nvoid Remove(ListNode n0) {\n    if (n0.next == null)\n        return;\n    // n0 -> P -> n1\n    ListNode P = n0.next;\n    ListNode? n1 = P.next;\n    n0.next = n1;\n}\n
      linked_list.go
      /* Remove the first node after node n0 in the linked list */\nfunc removeItem(n0 *ListNode) {\n    if n0.Next == nil {\n        return\n    }\n    // n0 -> P -> n1\n    P := n0.Next\n    n1 := P.Next\n    n0.Next = n1\n}\n
      linked_list.swift
      /* Remove the first node after node n0 in the linked list */\nfunc remove(n0: ListNode) {\n    if n0.next == nil {\n        return\n    }\n    // n0 -> P -> n1\n    let P = n0.next\n    let n1 = P?.next\n    n0.next = n1\n}\n
      linked_list.js
      /* Remove the first node after node n0 in the linked list */\nfunction remove(n0) {\n    if (!n0.next) return;\n    // n0 -> P -> n1\n    const P = n0.next;\n    const n1 = P.next;\n    n0.next = n1;\n}\n
      linked_list.ts
      /* Remove the first node after node n0 in the linked list */\nfunction remove(n0: ListNode): void {\n    if (!n0.next) {\n        return;\n    }\n    // n0 -> P -> n1\n    const P = n0.next;\n    const n1 = P.next;\n    n0.next = n1;\n}\n
      linked_list.dart
      /* Remove the first node after node n0 in the linked list */\nvoid remove(ListNode n0) {\n  if (n0.next == null) return;\n  // n0 -> P -> n1\n  ListNode P = n0.next!;\n  ListNode? n1 = P.next;\n  n0.next = n1;\n}\n
      linked_list.rs
      /* Remove the first node after node n0 in the linked list */\n#[allow(non_snake_case)]\npub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {\n    // n0 -> P -> n1\n    let P = n0.borrow_mut().next.take();\n    if let Some(node) = P {\n        let n1 = node.borrow_mut().next.take();\n        n0.borrow_mut().next = n1;\n    }\n}\n
      linked_list.c
      /* Remove the first node after node n0 in the linked list */\n// Note: stdio.h occupies the remove keyword\nvoid removeItem(ListNode *n0) {\n    if (!n0->next)\n        return;\n    // n0 -> P -> n1\n    ListNode *P = n0->next;\n    ListNode *n1 = P->next;\n    n0->next = n1;\n    // Free memory\n    free(P);\n}\n
      linked_list.kt
      /* Remove the first node after node n0 in the linked list */\nfun remove(n0: ListNode?) {\n    if (n0?.next == null)\n        return\n    // n0 -> P -> n1\n    val p = n0.next\n    val n1 = p?.next\n    n0.next = n1\n}\n
      linked_list.rb
      ### Delete first node after node n0 in linked list ###\ndef remove(n0)\n  return if n0.next.nil?\n\n  # n0 -> remove_node -> n1\n  remove_node = n0.next\n  n1 = remove_node.next\n  n0.next = n1\nend\n
      "},{"location":"chapter_array_and_linkedlist/linked_list/#4-accessing-a-node","title":"4. \u00a0 Accessing a Node","text":"

      Accessing nodes in a linked list is less efficient. As mentioned in the previous section, we can access any element in an array in \\(O(1)\\) time. This is not the case with linked lists. The program needs to start from the head node and traverse backward one by one until the target node is found. That is, accessing the \\(i\\)-th node in a linked list requires \\(i - 1\\) iterations, with a time complexity of \\(O(n)\\).

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
      def access(head: ListNode, index: int) -> ListNode | None:\n    \"\"\"Access the node at index index in the linked list\"\"\"\n    for _ in range(index):\n        if not head:\n            return None\n        head = head.next\n    return head\n
      linked_list.cpp
      /* Access the node at index index in the linked list */\nListNode *access(ListNode *head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == nullptr)\n            return nullptr;\n        head = head->next;\n    }\n    return head;\n}\n
      linked_list.java
      /* Access the node at index index in the linked list */\nListNode access(ListNode head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == null)\n            return null;\n        head = head.next;\n    }\n    return head;\n}\n
      linked_list.cs
      /* Access the node at index index in the linked list */\nListNode? Access(ListNode? head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == null)\n            return null;\n        head = head.next;\n    }\n    return head;\n}\n
      linked_list.go
      /* Access the node at index index in the linked list */\nfunc access(head *ListNode, index int) *ListNode {\n    for i := 0; i < index; i++ {\n        if head == nil {\n            return nil\n        }\n        head = head.Next\n    }\n    return head\n}\n
      linked_list.swift
      /* Access the node at index index in the linked list */\nfunc access(head: ListNode, index: Int) -> ListNode? {\n    var head: ListNode? = head\n    for _ in 0 ..< index {\n        if head == nil {\n            return nil\n        }\n        head = head?.next\n    }\n    return head\n}\n
      linked_list.js
      /* Access the node at index index in the linked list */\nfunction access(head, index) {\n    for (let i = 0; i < index; i++) {\n        if (!head) {\n            return null;\n        }\n        head = head.next;\n    }\n    return head;\n}\n
      linked_list.ts
      /* Access the node at index index in the linked list */\nfunction access(head: ListNode | null, index: number): ListNode | null {\n    for (let i = 0; i < index; i++) {\n        if (!head) {\n            return null;\n        }\n        head = head.next;\n    }\n    return head;\n}\n
      linked_list.dart
      /* Access the node at index index in the linked list */\nListNode? access(ListNode? head, int index) {\n  for (var i = 0; i < index; i++) {\n    if (head == null) return null;\n    head = head.next;\n  }\n  return head;\n}\n
      linked_list.rs
      /* Access the node at index index in the linked list */\npub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Option<Rc<RefCell<ListNode<T>>>> {\n    fn dfs<T>(\n        head: Option<&Rc<RefCell<ListNode<T>>>>,\n        index: i32,\n    ) -> Option<Rc<RefCell<ListNode<T>>>> {\n        if index <= 0 {\n            return head.cloned();\n        }\n\n        if let Some(node) = head {\n            dfs(node.borrow().next.as_ref(), index - 1)\n        } else {\n            None\n        }\n    }\n\n    dfs(Some(head).as_ref(), index)\n}\n
      linked_list.c
      /* Access the node at index index in the linked list */\nListNode *access(ListNode *head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == NULL)\n            return NULL;\n        head = head->next;\n    }\n    return head;\n}\n
      linked_list.kt
      /* Access the node at index index in the linked list */\nfun access(head: ListNode?, index: Int): ListNode? {\n    var h = head\n    for (i in 0..<index) {\n        if (h == null)\n            return null\n        h = h.next\n    }\n    return h\n}\n
      linked_list.rb
      ### Access node at index in linked list ###\ndef access(head, index)\n  for i in 0...index\n    return nil if head.nil?\n    head = head.next\n  end\n\n  head\nend\n
      "},{"location":"chapter_array_and_linkedlist/linked_list/#5-finding-a-node","title":"5. \u00a0 Finding a Node","text":"

      Traverse the linked list to find a node with value target, and output the index of that node in the linked list. This process is also a linear search. The code is shown below:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
      def find(head: ListNode, target: int) -> int:\n    \"\"\"Find the first node with value target in the linked list\"\"\"\n    index = 0\n    while head:\n        if head.val == target:\n            return index\n        head = head.next\n        index += 1\n    return -1\n
      linked_list.cpp
      /* Find the first node with value target in the linked list */\nint find(ListNode *head, int target) {\n    int index = 0;\n    while (head != nullptr) {\n        if (head->val == target)\n            return index;\n        head = head->next;\n        index++;\n    }\n    return -1;\n}\n
      linked_list.java
      /* Find the first node with value target in the linked list */\nint find(ListNode head, int target) {\n    int index = 0;\n    while (head != null) {\n        if (head.val == target)\n            return index;\n        head = head.next;\n        index++;\n    }\n    return -1;\n}\n
      linked_list.cs
      /* Find the first node with value target in the linked list */\nint Find(ListNode? head, int target) {\n    int index = 0;\n    while (head != null) {\n        if (head.val == target)\n            return index;\n        head = head.next;\n        index++;\n    }\n    return -1;\n}\n
      linked_list.go
      /* Find the first node with value target in the linked list */\nfunc findNode(head *ListNode, target int) int {\n    index := 0\n    for head != nil {\n        if head.Val == target {\n            return index\n        }\n        head = head.Next\n        index++\n    }\n    return -1\n}\n
      linked_list.swift
      /* Find the first node with value target in the linked list */\nfunc find(head: ListNode, target: Int) -> Int {\n    var head: ListNode? = head\n    var index = 0\n    while head != nil {\n        if head?.val == target {\n            return index\n        }\n        head = head?.next\n        index += 1\n    }\n    return -1\n}\n
      linked_list.js
      /* Find the first node with value target in the linked list */\nfunction find(head, target) {\n    let index = 0;\n    while (head !== null) {\n        if (head.val === target) {\n            return index;\n        }\n        head = head.next;\n        index += 1;\n    }\n    return -1;\n}\n
      linked_list.ts
      /* Find the first node with value target in the linked list */\nfunction find(head: ListNode | null, target: number): number {\n    let index = 0;\n    while (head !== null) {\n        if (head.val === target) {\n            return index;\n        }\n        head = head.next;\n        index += 1;\n    }\n    return -1;\n}\n
      linked_list.dart
      /* Find the first node with value target in the linked list */\nint find(ListNode? head, int target) {\n  int index = 0;\n  while (head != null) {\n    if (head.val == target) {\n      return index;\n    }\n    head = head.next;\n    index++;\n  }\n  return -1;\n}\n
      linked_list.rs
      /* Find the first node with value target in the linked list */\npub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T) -> i32 {\n    fn find<T: PartialEq>(head: Option<&Rc<RefCell<ListNode<T>>>>, target: T, idx: i32) -> i32 {\n        if let Some(node) = head {\n            if node.borrow().val == target {\n                return idx;\n            }\n            return find(node.borrow().next.as_ref(), target, idx + 1);\n        } else {\n            -1\n        }\n    }\n\n    find(Some(head).as_ref(), target, 0)\n}\n
      linked_list.c
      /* Find the first node with value target in the linked list */\nint find(ListNode *head, int target) {\n    int index = 0;\n    while (head) {\n        if (head->val == target)\n            return index;\n        head = head->next;\n        index++;\n    }\n    return -1;\n}\n
      linked_list.kt
      /* Find the first node with value target in the linked list */\nfun find(head: ListNode?, target: Int): Int {\n    var index = 0\n    var h = head\n    while (h != null) {\n        if (h._val == target)\n            return index\n        h = h.next\n        index++\n    }\n    return -1\n}\n
      linked_list.rb
      ### Find first node with value target in linked list ###\ndef find(head, target)\n  index = 0\n  while head\n    return index if head.val == target\n    head = head.next\n    index += 1\n  end\n\n  -1\nend\n
      "},{"location":"chapter_array_and_linkedlist/linked_list/#422-arrays-vs-linked-lists","title":"4.2.2 \u00a0 Arrays vs. Linked Lists","text":"

      Table 4-1 summarizes the characteristics of arrays and linked lists and compares their operational efficiencies. Since they employ two opposite storage strategies, their various properties and operational efficiencies also exhibit contrasting characteristics.

      Table 4-1 \u00a0 Comparison of array and linked list efficiencies

      Array Linked List Storage method Contiguous memory space Scattered memory space Capacity expansion Immutable length Flexible expansion Memory efficiency Elements occupy less memory, but space may be wasted Elements occupy more memory Accessing an element \\(O(1)\\) \\(O(n)\\) Adding an element \\(O(n)\\) \\(O(1)\\) Removing an element \\(O(n)\\) \\(O(1)\\)"},{"location":"chapter_array_and_linkedlist/linked_list/#423-common-types-of-linked-lists","title":"4.2.3 \u00a0 Common Types of Linked Lists","text":"

      As shown in Figure 4-8, there are three common types of linked lists:

      • Singly linked list: This is the ordinary linked list introduced earlier. The nodes of a singly linked list contain a value and a reference to the next node. We call the first node the head node and the last node the tail node, which points to null None.
      • Circular linked list: If we make the tail node of a singly linked list point to the head node (connecting the tail to the head), we get a circular linked list. In a circular linked list, any node can be viewed as the head node.
      • Doubly linked list: Compared to a singly linked list, a doubly linked list records references in both directions. The node definition of a doubly linked list includes references to both the successor node (next node) and the predecessor node (previous node). Compared to a singly linked list, a doubly linked list is more flexible and can traverse the linked list in both directions, but it also requires more memory space.
      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      class ListNode:\n    \"\"\"Doubly linked list node class\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val                # Node value\n        self.next: ListNode | None = None  # Reference to the successor node\n        self.prev: ListNode | None = None  # Reference to the predecessor node\n
      /* Doubly linked list node structure */\nstruct ListNode {\n    int val;         // Node value\n    ListNode *next;  // Pointer to the successor node\n    ListNode *prev;  // Pointer to the predecessor node\n    ListNode(int x) : val(x), next(nullptr), prev(nullptr) {}  // Constructor\n};\n
      /* Doubly linked list node class */\nclass ListNode {\n    int val;        // Node value\n    ListNode next;  // Reference to the successor node\n    ListNode prev;  // Reference to the predecessor node\n    ListNode(int x) { val = x; }  // Constructor\n}\n
      /* Doubly linked list node class */\nclass ListNode(int x) {  // Constructor\n    int val = x;    // Node value\n    ListNode next;  // Reference to the successor node\n    ListNode prev;  // Reference to the predecessor node\n}\n
      /* Doubly linked list node structure */\ntype DoublyListNode struct {\n    Val  int             // Node value\n    Next *DoublyListNode // Pointer to the successor node\n    Prev *DoublyListNode // Pointer to the predecessor node\n}\n\n// NewDoublyListNode Initialization\nfunc NewDoublyListNode(val int) *DoublyListNode {\n    return &DoublyListNode{\n        Val:  val,\n        Next: nil,\n        Prev: nil,\n    }\n}\n
      /* Doubly linked list node class */\nclass ListNode {\n    var val: Int // Node value\n    var next: ListNode? // Reference to the successor node\n    var prev: ListNode? // Reference to the predecessor node\n\n    init(x: Int) { // Constructor\n        val = x\n    }\n}\n
      /* Doubly linked list node class */\nclass ListNode {\n    constructor(val, next, prev) {\n        this.val = val  ===  undefined ? 0 : val;        // Node value\n        this.next = next  ===  undefined ? null : next;  // Reference to the successor node\n        this.prev = prev  ===  undefined ? null : prev;  // Reference to the predecessor node\n    }\n}\n
      /* Doubly linked list node class */\nclass ListNode {\n    val: number;\n    next: ListNode | null;\n    prev: ListNode | null;\n    constructor(val?: number, next?: ListNode | null, prev?: ListNode | null) {\n        this.val = val  ===  undefined ? 0 : val;        // Node value\n        this.next = next  ===  undefined ? null : next;  // Reference to the successor node\n        this.prev = prev  ===  undefined ? null : prev;  // Reference to the predecessor node\n    }\n}\n
      /* Doubly linked list node class */\nclass ListNode {\n    int val;        // Node value\n    ListNode? next;  // Reference to the successor node\n    ListNode? prev;  // Reference to the predecessor node\n    ListNode(this.val, [this.next, this.prev]);  // Constructor\n}\n
      use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* Doubly linked list node type */\n#[derive(Debug)]\nstruct ListNode {\n    val: i32, // Node value\n    next: Option<Rc<RefCell<ListNode>>>, // Pointer to the successor node\n    prev: Option<Rc<RefCell<ListNode>>>, // Pointer to the predecessor node\n}\n\n/* Constructor */\nimpl ListNode {\n    fn new(val: i32) -> Self {\n        ListNode {\n            val,\n            next: None,\n            prev: None,\n        }\n    }\n}\n
      /* Doubly linked list node structure */\ntypedef struct ListNode {\n    int val;               // Node value\n    struct ListNode *next; // Pointer to the successor node\n    struct ListNode *prev; // Pointer to the predecessor node\n} ListNode;\n\n/* Constructor */\nListNode *newListNode(int val) {\n    ListNode *node;\n    node = (ListNode *) malloc(sizeof(ListNode));\n    node->val = val;\n    node->next = NULL;\n    node->prev = NULL;\n    return node;\n}\n
      /* Doubly linked list node class */\n// Constructor\nclass ListNode(x: Int) {\n    val _val: Int = x           // Node value\n    val next: ListNode? = null  // Reference to the successor node\n    val prev: ListNode? = null  // Reference to the predecessor node\n}\n
      # Doubly linked list node class\nclass ListNode\n  attr_accessor :val    # Node value\n  attr_accessor :next   # Reference to the successor node\n  attr_accessor :prev   # Reference to the predecessor node\n\n  def initialize(val=0, next_node=nil, prev_node=nil)\n    @val = val\n    @next = next_node\n    @prev = prev_node\n  end\nend\n

      Figure 4-8 \u00a0 Common types of linked lists

      "},{"location":"chapter_array_and_linkedlist/linked_list/#424-typical-applications-of-linked-lists","title":"4.2.4 \u00a0 Typical Applications of Linked Lists","text":"

      Singly linked lists are commonly used to implement stacks, queues, hash tables, and graphs.

      • Stacks and queues: When insertion and deletion operations both occur at one end of the linked list, it exhibits last-in-first-out characteristics, corresponding to a stack. When insertion operations occur at one end of the linked list and deletion operations occur at the other end, it exhibits first-in-first-out characteristics, corresponding to a queue.
      • Hash tables: Separate chaining is one of the mainstream solutions for resolving hash collisions. In this approach, all colliding elements are placed in a linked list.
      • Graphs: An adjacency list is a common way to represent a graph, where each vertex in the graph is associated with a linked list, and each element in the linked list represents another vertex connected to that vertex.

      Doubly linked lists are commonly used in scenarios where quick access to the previous and next elements is needed.

      • Advanced data structures: For example, in red-black trees and B-trees, we need to access the parent node of a node, which can be achieved by saving a reference to the parent node in the node, similar to a doubly linked list.
      • Browser history: In web browsers, when a user clicks the forward or backward button, the browser needs to know the previous and next web pages the user visited. The characteristics of doubly linked lists make this operation simple.
      • LRU algorithm: In cache eviction (LRU) algorithms, we need to quickly find the least recently used data and support quick addition and deletion of nodes. Using a doubly linked list is very suitable for this.

      Circular linked lists are commonly used in scenarios that require periodic operations, such as operating system resource scheduling.

      • Round-robin scheduling algorithm: In operating systems, round-robin scheduling is a common CPU scheduling algorithm that needs to cycle through a set of processes. Each process is assigned a time slice, and when the time slice expires, the CPU switches to the next process. This cyclic operation can be implemented using a circular linked list.
      • Data buffers: In some data buffer implementations, circular linked lists may also be used. For example, in audio and video players, the data stream may be divided into multiple buffer blocks and placed in a circular linked list to achieve seamless playback.
      "},{"location":"chapter_array_and_linkedlist/list/","title":"4.3 \u00a0 List","text":"

      A list is an abstract data structure concept that represents an ordered collection of elements, supporting operations such as element access, modification, insertion, deletion, and traversal, without requiring users to consider capacity limitations. Lists can be implemented based on linked lists or arrays.

      • A linked list can naturally be viewed as a list, supporting element insertion, deletion, search, and modification operations, and can flexibly expand dynamically.
      • An array also supports element insertion, deletion, search, and modification, but since its length is immutable, it can only be viewed as a list with length limitations.

      When implementing lists using arrays, the immutable length property reduces the practicality of the list. This is because we usually cannot determine in advance how much data we need to store, making it difficult to choose an appropriate list length. If the length is too small, it may fail to meet usage requirements; if the length is too large, it will waste memory space.

      To solve this problem, we can use a dynamic array to implement a list. It inherits all the advantages of arrays and can dynamically expand during program execution.

      In fact, the lists provided in the standard libraries of many programming languages are implemented based on dynamic arrays, such as list in Python, ArrayList in Java, vector in C++, and List in C#. In the following discussion, we will treat \"list\" and \"dynamic array\" as equivalent concepts.

      "},{"location":"chapter_array_and_linkedlist/list/#431-common-list-operations","title":"4.3.1 \u00a0 Common List Operations","text":""},{"location":"chapter_array_and_linkedlist/list/#1-initialize-a-list","title":"1. \u00a0 Initialize a List","text":"

      We typically use two initialization methods: \"without initial values\" and \"with initial values\":

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
      # Initialize a list\n# Without initial values\nnums1: list[int] = []\n# With initial values\nnums: list[int] = [1, 3, 2, 5, 4]\n
      list.cpp
      /* Initialize a list */\n// Note that vector in C++ is equivalent to nums as described in this article\n// Without initial values\nvector<int> nums1;\n// With initial values\nvector<int> nums = { 1, 3, 2, 5, 4 };\n
      list.java
      /* Initialize a list */\n// Without initial values\nList<Integer> nums1 = new ArrayList<>();\n// With initial values (note that array elements should use the wrapper class Integer[] instead of int[])\nInteger[] numbers = new Integer[] { 1, 3, 2, 5, 4 };\nList<Integer> nums = new ArrayList<>(Arrays.asList(numbers));\n
      list.cs
      /* Initialize a list */\n// Without initial values\nList<int> nums1 = [];\n// With initial values\nint[] numbers = [1, 3, 2, 5, 4];\nList<int> nums = [.. numbers];\n
      list_test.go
      /* Initialize a list */\n// Without initial values\nnums1 := []int{}\n// With initial values\nnums := []int{1, 3, 2, 5, 4}\n
      list.swift
      /* Initialize a list */\n// Without initial values\nlet nums1: [Int] = []\n// With initial values\nvar nums = [1, 3, 2, 5, 4]\n
      list.js
      /* Initialize a list */\n// Without initial values\nconst nums1 = [];\n// With initial values\nconst nums = [1, 3, 2, 5, 4];\n
      list.ts
      /* Initialize a list */\n// Without initial values\nconst nums1: number[] = [];\n// With initial values\nconst nums: number[] = [1, 3, 2, 5, 4];\n
      list.dart
      /* Initialize a list */\n// Without initial values\nList<int> nums1 = [];\n// With initial values\nList<int> nums = [1, 3, 2, 5, 4];\n
      list.rs
      /* Initialize a list */\n// Without initial values\nlet nums1: Vec<i32> = Vec::new();\n// With initial values\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
      list.c
      // C does not provide built-in dynamic arrays\n
      list.kt
      /* Initialize a list */\n// Without initial values\nvar nums1 = listOf<Int>()\n// With initial values\nvar numbers = arrayOf(1, 3, 2, 5, 4)\nvar nums = numbers.toMutableList()\n
      list.rb
      # Initialize a list\n# Without initial values\nnums1 = []\n# With initial values\nnums = [1, 3, 2, 5, 4]\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_array_and_linkedlist/list/#2-access-elements","title":"2. \u00a0 Access Elements","text":"

      Since a list is essentially an array, we can access and update elements in \\(O(1)\\) time complexity, which is very efficient.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
      # Access an element\nnum: int = nums[1]  # Access element at index 1\n\n# Update an element\nnums[1] = 0    # Update element at index 1 to 0\n
      list.cpp
      /* Access an element */\nint num = nums[1];  // Access element at index 1\n\n/* Update an element */\nnums[1] = 0;  // Update element at index 1 to 0\n
      list.java
      /* Access an element */\nint num = nums.get(1);  // Access element at index 1\n\n/* Update an element */\nnums.set(1, 0);  // Update element at index 1 to 0\n
      list.cs
      /* Access an element */\nint num = nums[1];  // Access element at index 1\n\n/* Update an element */\nnums[1] = 0;  // Update element at index 1 to 0\n
      list_test.go
      /* Access an element */\nnum := nums[1]  // Access element at index 1\n\n/* Update an element */\nnums[1] = 0     // Update element at index 1 to 0\n
      list.swift
      /* Access an element */\nlet num = nums[1] // Access element at index 1\n\n/* Update an element */\nnums[1] = 0 // Update element at index 1 to 0\n
      list.js
      /* Access an element */\nconst num = nums[1];  // Access element at index 1\n\n/* Update an element */\nnums[1] = 0;  // Update element at index 1 to 0\n
      list.ts
      /* Access an element */\nconst num: number = nums[1];  // Access element at index 1\n\n/* Update an element */\nnums[1] = 0;  // Update element at index 1 to 0\n
      list.dart
      /* Access an element */\nint num = nums[1];  // Access element at index 1\n\n/* Update an element */\nnums[1] = 0;  // Update element at index 1 to 0\n
      list.rs
      /* Access an element */\nlet num: i32 = nums[1];  // Access element at index 1\n/* Update an element */\nnums[1] = 0;             // Update element at index 1 to 0\n
      list.c
      // C does not provide built-in dynamic arrays\n
      list.kt
      /* Access an element */\nval num = nums[1]       // Access element at index 1\n/* Update an element */\nnums[1] = 0             // Update element at index 1 to 0\n
      list.rb
      # Access an element\nnum = nums[1] # Access element at index 1\n# Update an element\nnums[1] = 0 # Update element at index 1 to 0\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_array_and_linkedlist/list/#3-insert-and-delete-elements","title":"3. \u00a0 Insert and Delete Elements","text":"

      Compared to arrays, lists can freely add and delete elements. Adding an element at the end of a list has a time complexity of \\(O(1)\\), but inserting and deleting elements still have the same efficiency as arrays, with a time complexity of \\(O(n)\\).

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
      # Clear the list\nnums.clear()\n\n# Add elements at the end\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n# Insert an element in the middle\nnums.insert(3, 6)  # Insert number 6 at index 3\n\n# Delete an element\nnums.pop(3)        # Delete element at index 3\n
      list.cpp
      /* Clear the list */\nnums.clear();\n\n/* Add elements at the end */\nnums.push_back(1);\nnums.push_back(3);\nnums.push_back(2);\nnums.push_back(5);\nnums.push_back(4);\n\n/* Insert an element in the middle */\nnums.insert(nums.begin() + 3, 6);  // Insert number 6 at index 3\n\n/* Delete an element */\nnums.erase(nums.begin() + 3);      // Delete element at index 3\n
      list.java
      /* Clear the list */\nnums.clear();\n\n/* Add elements at the end */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* Insert an element in the middle */\nnums.add(3, 6);  // Insert number 6 at index 3\n\n/* Delete an element */\nnums.remove(3);  // Delete element at index 3\n
      list.cs
      /* Clear the list */\nnums.Clear();\n\n/* Add elements at the end */\nnums.Add(1);\nnums.Add(3);\nnums.Add(2);\nnums.Add(5);\nnums.Add(4);\n\n/* Insert an element in the middle */\nnums.Insert(3, 6);  // Insert number 6 at index 3\n\n/* Delete an element */\nnums.RemoveAt(3);  // Delete element at index 3\n
      list_test.go
      /* Clear the list */\nnums = nil\n\n/* Add elements at the end */\nnums = append(nums, 1)\nnums = append(nums, 3)\nnums = append(nums, 2)\nnums = append(nums, 5)\nnums = append(nums, 4)\n\n/* Insert an element in the middle */\nnums = append(nums[:3], append([]int{6}, nums[3:]...)...) // Insert number 6 at index 3\n\n/* Delete an element */\nnums = append(nums[:3], nums[4:]...) // Delete element at index 3\n
      list.swift
      /* Clear the list */\nnums.removeAll()\n\n/* Add elements at the end */\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n/* Insert an element in the middle */\nnums.insert(6, at: 3) // Insert number 6 at index 3\n\n/* Delete an element */\nnums.remove(at: 3) // Delete element at index 3\n
      list.js
      /* Clear the list */\nnums.length = 0;\n\n/* Add elements at the end */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* Insert an element in the middle */\nnums.splice(3, 0, 6); // Insert number 6 at index 3\n\n/* Delete an element */\nnums.splice(3, 1);  // Delete element at index 3\n
      list.ts
      /* Clear the list */\nnums.length = 0;\n\n/* Add elements at the end */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* Insert an element in the middle */\nnums.splice(3, 0, 6); // Insert number 6 at index 3\n\n/* Delete an element */\nnums.splice(3, 1);  // Delete element at index 3\n
      list.dart
      /* Clear the list */\nnums.clear();\n\n/* Add elements at the end */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* Insert an element in the middle */\nnums.insert(3, 6); // Insert number 6 at index 3\n\n/* Delete an element */\nnums.removeAt(3); // Delete element at index 3\n
      list.rs
      /* Clear the list */\nnums.clear();\n\n/* Add elements at the end */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* Insert an element in the middle */\nnums.insert(3, 6);  // Insert number 6 at index 3\n\n/* Delete an element */\nnums.remove(3);    // Delete element at index 3\n
      list.c
      // C does not provide built-in dynamic arrays\n
      list.kt
      /* Clear the list */\nnums.clear();\n\n/* Add elements at the end */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* Insert an element in the middle */\nnums.add(3, 6);  // Insert number 6 at index 3\n\n/* Delete an element */\nnums.remove(3);  // Delete element at index 3\n
      list.rb
      # Clear the list\nnums.clear\n\n# Add elements at the end\nnums << 1\nnums << 3\nnums << 2\nnums << 5\nnums << 4\n\n# Insert an element in the middle\nnums.insert(3, 6) # Insert number 6 at index 3\n\n# Delete an element\nnums.delete_at(3) # Delete element at index 3\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_array_and_linkedlist/list/#4-traverse-a-list","title":"4. \u00a0 Traverse a List","text":"

      Like arrays, lists can be traversed by index or by directly iterating through elements.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
      # Traverse the list by index\ncount = 0\nfor i in range(len(nums)):\n    count += nums[i]\n\n# Traverse list elements directly\nfor num in nums:\n    count += num\n
      list.cpp
      /* Traverse the list by index */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\n    count += nums[i];\n}\n\n/* Traverse list elements directly */\ncount = 0;\nfor (int num : nums) {\n    count += num;\n}\n
      list.java
      /* Traverse the list by index */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\n    count += nums.get(i);\n}\n\n/* Traverse list elements directly */\nfor (int num : nums) {\n    count += num;\n}\n
      list.cs
      /* Traverse the list by index */\nint count = 0;\nfor (int i = 0; i < nums.Count; i++) {\n    count += nums[i];\n}\n\n/* Traverse list elements directly */\ncount = 0;\nforeach (int num in nums) {\n    count += num;\n}\n
      list_test.go
      /* Traverse the list by index */\ncount := 0\nfor i := 0; i < len(nums); i++ {\n    count += nums[i]\n}\n\n/* Traverse list elements directly */\ncount = 0\nfor _, num := range nums {\n    count += num\n}\n
      list.swift
      /* Traverse the list by index */\nvar count = 0\nfor i in nums.indices {\n    count += nums[i]\n}\n\n/* Traverse list elements directly */\ncount = 0\nfor num in nums {\n    count += num\n}\n
      list.js
      /* Traverse the list by index */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\n    count += nums[i];\n}\n\n/* Traverse list elements directly */\ncount = 0;\nfor (const num of nums) {\n    count += num;\n}\n
      list.ts
      /* Traverse the list by index */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\n    count += nums[i];\n}\n\n/* Traverse list elements directly */\ncount = 0;\nfor (const num of nums) {\n    count += num;\n}\n
      list.dart
      /* Traverse the list by index */\nint count = 0;\nfor (var i = 0; i < nums.length; i++) {\n    count += nums[i];\n}\n\n/* Traverse list elements directly */\ncount = 0;\nfor (var num in nums) {\n    count += num;\n}\n
      list.rs
      // Traverse the list by index\nlet mut _count = 0;\nfor i in 0..nums.len() {\n    _count += nums[i];\n}\n\n// Traverse list elements directly\n_count = 0;\nfor num in &nums {\n    _count += num;\n}\n
      list.c
      // C does not provide built-in dynamic arrays\n
      list.kt
      /* Traverse the list by index */\nvar count = 0\nfor (i in nums.indices) {\n    count += nums[i]\n}\n\n/* Traverse list elements directly */\nfor (num in nums) {\n    count += num\n}\n
      list.rb
      # Traverse the list by index\ncount = 0\nfor i in 0...nums.length\n    count += nums[i]\nend\n\n# Traverse list elements directly\ncount = 0\nfor num in nums\n    count += num\nend\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_array_and_linkedlist/list/#5-concatenate-lists","title":"5. \u00a0 Concatenate Lists","text":"

      Given a new list nums1, we can concatenate it to the end of the original list.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
      # Concatenate two lists\nnums1: list[int] = [6, 8, 7, 10, 9]\nnums += nums1  # Concatenate list nums1 to the end of nums\n
      list.cpp
      /* Concatenate two lists */\nvector<int> nums1 = { 6, 8, 7, 10, 9 };\n// Concatenate list nums1 to the end of nums\nnums.insert(nums.end(), nums1.begin(), nums1.end());\n
      list.java
      /* Concatenate two lists */\nList<Integer> nums1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));\nnums.addAll(nums1);  // Concatenate list nums1 to the end of nums\n
      list.cs
      /* Concatenate two lists */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.AddRange(nums1);  // Concatenate list nums1 to the end of nums\n
      list_test.go
      /* Concatenate two lists */\nnums1 := []int{6, 8, 7, 10, 9}\nnums = append(nums, nums1...)  // Concatenate list nums1 to the end of nums\n
      list.swift
      /* Concatenate two lists */\nlet nums1 = [6, 8, 7, 10, 9]\nnums.append(contentsOf: nums1) // Concatenate list nums1 to the end of nums\n
      list.js
      /* Concatenate two lists */\nconst nums1 = [6, 8, 7, 10, 9];\nnums.push(...nums1);  // Concatenate list nums1 to the end of nums\n
      list.ts
      /* Concatenate two lists */\nconst nums1: number[] = [6, 8, 7, 10, 9];\nnums.push(...nums1);  // Concatenate list nums1 to the end of nums\n
      list.dart
      /* Concatenate two lists */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.addAll(nums1);  // Concatenate list nums1 to the end of nums\n
      list.rs
      /* Concatenate two lists */\nlet nums1: Vec<i32> = vec![6, 8, 7, 10, 9];\nnums.extend(nums1);\n
      list.c
      // C does not provide built-in dynamic arrays\n
      list.kt
      /* Concatenate two lists */\nval nums1 = intArrayOf(6, 8, 7, 10, 9).toMutableList()\nnums.addAll(nums1)  // Concatenate list nums1 to the end of nums\n
      list.rb
      # Concatenate two lists\nnums1 = [6, 8, 7, 10, 9]\nnums += nums1\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_array_and_linkedlist/list/#6-sort-a-list","title":"6. \u00a0 Sort a List","text":"

      After sorting a list, we can use \"binary search\" and \"two-pointer\" algorithms, which are frequently tested in array algorithm problems.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
      # Sort a list\nnums.sort()  # After sorting, list elements are arranged from smallest to largest\n
      list.cpp
      /* Sort a list */\nsort(nums.begin(), nums.end());  // After sorting, list elements are arranged from smallest to largest\n
      list.java
      /* Sort a list */\nCollections.sort(nums);  // After sorting, list elements are arranged from smallest to largest\n
      list.cs
      /* Sort a list */\nnums.Sort(); // After sorting, list elements are arranged from smallest to largest\n
      list_test.go
      /* Sort a list */\nsort.Ints(nums)  // After sorting, list elements are arranged from smallest to largest\n
      list.swift
      /* Sort a list */\nnums.sort() // After sorting, list elements are arranged from smallest to largest\n
      list.js
      /* Sort a list */\nnums.sort((a, b) => a - b);  // After sorting, list elements are arranged from smallest to largest\n
      list.ts
      /* Sort a list */\nnums.sort((a, b) => a - b);  // After sorting, list elements are arranged from smallest to largest\n
      list.dart
      /* Sort a list */\nnums.sort(); // After sorting, list elements are arranged from smallest to largest\n
      list.rs
      /* Sort a list */\nnums.sort(); // After sorting, list elements are arranged from smallest to largest\n
      list.c
      // C does not provide built-in dynamic arrays\n
      list.kt
      /* Sort a list */\nnums.sort() // After sorting, list elements are arranged from smallest to largest\n
      list.rb
      # Sort a list\nnums = nums.sort { |a, b| a <=> b } # After sorting, list elements are arranged from smallest to largest\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_array_and_linkedlist/list/#432-list-implementation","title":"4.3.2 \u00a0 List Implementation","text":"

      Many programming languages have built-in lists, such as Java, C++, and Python. Their implementations are quite complex, and the parameters are carefully considered, such as initial capacity, expansion multiples, and so on. Interested readers can consult the source code to learn more.

      To deepen our understanding of how lists work, we attempt to implement a simple list with three key design considerations:

      • Initial capacity: Select a reasonable initial capacity for the underlying array. In this example, we choose 10 as the initial capacity.
      • Size tracking: Declare a variable size to record the current number of elements in the list and update it in real-time as elements are inserted and deleted. Based on this variable, we can locate the end of the list and determine whether expansion is needed.
      • Expansion mechanism: When the list capacity is full upon inserting an element, we need to expand. We create a larger array based on the expansion multiple and then move all elements from the current array to the new array in order. In this example, we specify that the array should be expanded to 2 times its previous size each time.
      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_list.py
      class MyList:\n    \"\"\"List class\"\"\"\n\n    def __init__(self):\n        \"\"\"Constructor\"\"\"\n        self._capacity: int = 10  # List capacity\n        self._arr: list[int] = [0] * self._capacity  # Array (stores list elements)\n        self._size: int = 0  # List length (current number of elements)\n        self._extend_ratio: int = 2  # Multiple by which the list capacity is extended each time\n\n    def size(self) -> int:\n        \"\"\"Get list length (current number of elements)\"\"\"\n        return self._size\n\n    def capacity(self) -> int:\n        \"\"\"Get list capacity\"\"\"\n        return self._capacity\n\n    def get(self, index: int) -> int:\n        \"\"\"Access element\"\"\"\n        # If the index is out of bounds, throw an exception, as below\n        if index < 0 or index >= self._size:\n            raise IndexError(\"Index out of bounds\")\n        return self._arr[index]\n\n    def set(self, num: int, index: int):\n        \"\"\"Update element\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"Index out of bounds\")\n        self._arr[index] = num\n\n    def add(self, num: int):\n        \"\"\"Add element at the end\"\"\"\n        # When the number of elements exceeds capacity, trigger the extension mechanism\n        if self.size() == self.capacity():\n            self.extend_capacity()\n        self._arr[self._size] = num\n        self._size += 1\n\n    def insert(self, num: int, index: int):\n        \"\"\"Insert element in the middle\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"Index out of bounds\")\n        # When the number of elements exceeds capacity, trigger the extension mechanism\n        if self._size == self.capacity():\n            self.extend_capacity()\n        # Move all elements at and after index index backward by one position\n        for j in range(self._size - 1, index - 1, -1):\n            self._arr[j + 1] = self._arr[j]\n        self._arr[index] = num\n        # Update the number of elements\n        self._size += 1\n\n    def remove(self, index: int) -> int:\n        \"\"\"Remove element\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"Index out of bounds\")\n        num = self._arr[index]\n        # Move all elements after index index forward by one position\n        for j in range(index, self._size - 1):\n            self._arr[j] = self._arr[j + 1]\n        # Update the number of elements\n        self._size -= 1\n        # Return the removed element\n        return num\n\n    def extend_capacity(self):\n        \"\"\"Extend list capacity\"\"\"\n        # Create a new array with length _extend_ratio times the original array, and copy the original array to the new array\n        self._arr = self._arr + [0] * self.capacity() * (self._extend_ratio - 1)\n        # Update list capacity\n        self._capacity = len(self._arr)\n\n    def to_array(self) -> list[int]:\n        \"\"\"Return list with valid length\"\"\"\n        return self._arr[: self._size]\n
      my_list.cpp
      /* List class */\nclass MyList {\n  private:\n    int *arr;             // Array (stores list elements)\n    int arrCapacity = 10; // List capacity\n    int arrSize = 0;      // List length (current number of elements)\n    int extendRatio = 2;   // Multiple by which the list capacity is extended each time\n\n  public:\n    /* Constructor */\n    MyList() {\n        arr = new int[arrCapacity];\n    }\n\n    /* Destructor */\n    ~MyList() {\n        delete[] arr;\n    }\n\n    /* Get list length (current number of elements)*/\n    int size() {\n        return arrSize;\n    }\n\n    /* Get list capacity */\n    int capacity() {\n        return arrCapacity;\n    }\n\n    /* Update element */\n    int get(int index) {\n        // If the index is out of bounds, throw an exception, as below\n        if (index < 0 || index >= size())\n            throw out_of_range(\"Index out of bounds\");\n        return arr[index];\n    }\n\n    /* Add elements at the end */\n    void set(int index, int num) {\n        if (index < 0 || index >= size())\n            throw out_of_range(\"Index out of bounds\");\n        arr[index] = num;\n    }\n\n    /* Direct traversal of list elements */\n    void add(int num) {\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if (size() == capacity())\n            extendCapacity();\n        arr[size()] = num;\n        // Update the number of elements\n        arrSize++;\n    }\n\n    /* Sort list */\n    void insert(int index, int num) {\n        if (index < 0 || index >= size())\n            throw out_of_range(\"Index out of bounds\");\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if (size() == capacity())\n            extendCapacity();\n        // Move all elements after index index forward by one position\n        for (int j = size() - 1; j >= index; j--) {\n            arr[j + 1] = arr[j];\n        }\n        arr[index] = num;\n        // Update the number of elements\n        arrSize++;\n    }\n\n    /* Remove element */\n    int remove(int index) {\n        if (index < 0 || index >= size())\n            throw out_of_range(\"Index out of bounds\");\n        int num = arr[index];\n        // Create a new array with length _extend_ratio times the original array, and copy the original array to the new array\n        for (int j = index; j < size() - 1; j++) {\n            arr[j] = arr[j + 1];\n        }\n        // Update the number of elements\n        arrSize--;\n        // Return the removed element\n        return num;\n    }\n\n    /* Driver Code */\n    void extendCapacity() {\n        // Create a new array with length extendRatio times the original array\n        int newCapacity = capacity() * extendRatio;\n        int *tmp = arr;\n        arr = new int[newCapacity];\n        // Copy all elements from the original array to the new array\n        for (int i = 0; i < size(); i++) {\n            arr[i] = tmp[i];\n        }\n        // Free memory\n        delete[] tmp;\n        arrCapacity = newCapacity;\n    }\n\n    /* Convert list to Vector for printing */\n    vector<int> toVector() {\n        // Elements enqueue\n        vector<int> vec(size());\n        for (int i = 0; i < size(); i++) {\n            vec[i] = arr[i];\n        }\n        return vec;\n    }\n};\n
      my_list.java
      /* List class */\nclass MyList {\n    private int[] arr; // Array (stores list elements)\n    private int capacity = 10; // List capacity\n    private int size = 0; // List length (current number of elements)\n    private int extendRatio = 2; // Multiple by which the list capacity is extended each time\n\n    /* Constructor */\n    public MyList() {\n        arr = new int[capacity];\n    }\n\n    /* Get list length (current number of elements) */\n    public int size() {\n        return size;\n    }\n\n    /* Get list capacity */\n    public int capacity() {\n        return capacity;\n    }\n\n    /* Update element */\n    public int get(int index) {\n        // If the index is out of bounds, throw an exception, as below\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"Index out of bounds\");\n        return arr[index];\n    }\n\n    /* Add elements at the end */\n    public void set(int index, int num) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"Index out of bounds\");\n        arr[index] = num;\n    }\n\n    /* Direct traversal of list elements */\n    public void add(int num) {\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if (size == capacity())\n            extendCapacity();\n        arr[size] = num;\n        // Update the number of elements\n        size++;\n    }\n\n    /* Sort list */\n    public void insert(int index, int num) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"Index out of bounds\");\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if (size == capacity())\n            extendCapacity();\n        // Move all elements after index index forward by one position\n        for (int j = size - 1; j >= index; j--) {\n            arr[j + 1] = arr[j];\n        }\n        arr[index] = num;\n        // Update the number of elements\n        size++;\n    }\n\n    /* Remove element */\n    public int remove(int index) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"Index out of bounds\");\n        int num = arr[index];\n        // Move all elements after index forward by one position\n        for (int j = index; j < size - 1; j++) {\n            arr[j] = arr[j + 1];\n        }\n        // Update the number of elements\n        size--;\n        // Return the removed element\n        return num;\n    }\n\n    /* Driver Code */\n    public void extendCapacity() {\n        // Create a new array with length extendRatio times the original array and copy the original array to the new array\n        arr = Arrays.copyOf(arr, capacity() * extendRatio);\n        // Add elements at the end\n        capacity = arr.length;\n    }\n\n    /* Convert list to array */\n    public int[] toArray() {\n        int size = size();\n        // Elements enqueue\n        int[] arr = new int[size];\n        for (int i = 0; i < size; i++) {\n            arr[i] = get(i);\n        }\n        return arr;\n    }\n}\n
      my_list.cs
      /* List class */\nclass MyList {\n    private int[] arr;           // Array (stores list elements)\n    private int arrCapacity = 10;    // List capacity\n    private int arrSize = 0;         // List length (current number of elements)\n    private readonly int extendRatio = 2;  // Multiple by which the list capacity is extended each time\n\n    /* Constructor */\n    public MyList() {\n        arr = new int[arrCapacity];\n    }\n\n    /* Get list length (current number of elements) */\n    public int Size() {\n        return arrSize;\n    }\n\n    /* Get list capacity */\n    public int Capacity() {\n        return arrCapacity;\n    }\n\n    /* Update element */\n    public int Get(int index) {\n        // If the index is out of bounds, throw an exception, as below\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"Index out of bounds\");\n        return arr[index];\n    }\n\n    /* Add elements at the end */\n    public void Set(int index, int num) {\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"Index out of bounds\");\n        arr[index] = num;\n    }\n\n    /* Direct traversal of list elements */\n    public void Add(int num) {\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if (arrSize == arrCapacity)\n            ExtendCapacity();\n        arr[arrSize] = num;\n        // Update the number of elements\n        arrSize++;\n    }\n\n    /* Sort list */\n    public void Insert(int index, int num) {\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"Index out of bounds\");\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if (arrSize == arrCapacity)\n            ExtendCapacity();\n        // Move all elements after index index forward by one position\n        for (int j = arrSize - 1; j >= index; j--) {\n            arr[j + 1] = arr[j];\n        }\n        arr[index] = num;\n        // Update the number of elements\n        arrSize++;\n    }\n\n    /* Remove element */\n    public int Remove(int index) {\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"Index out of bounds\");\n        int num = arr[index];\n        // Move all elements after index forward by one position\n        for (int j = index; j < arrSize - 1; j++) {\n            arr[j] = arr[j + 1];\n        }\n        // Update the number of elements\n        arrSize--;\n        // Return the removed element\n        return num;\n    }\n\n    /* Driver Code */\n    public void ExtendCapacity() {\n        // Create new array of length arrCapacity * extendRatio and copy original array to new array\n        Array.Resize(ref arr, arrCapacity * extendRatio);\n        // Add elements at the end\n        arrCapacity = arr.Length;\n    }\n\n    /* Convert list to array */\n    public int[] ToArray() {\n        // Elements enqueue\n        int[] arr = new int[arrSize];\n        for (int i = 0; i < arrSize; i++) {\n            arr[i] = Get(i);\n        }\n        return arr;\n    }\n}\n
      my_list.go
      /* List class */\ntype myList struct {\n    arrCapacity int\n    arr         []int\n    arrSize     int\n    extendRatio int\n}\n\n/* Constructor */\nfunc newMyList() *myList {\n    return &myList{\n        arrCapacity: 10,              // List capacity\n        arr:         make([]int, 10), // Array (stores list elements)\n        arrSize:     0,               // List length (current number of elements)\n        extendRatio: 2,               // Multiple by which the list capacity is extended each time\n    }\n}\n\n/* Get list length (current number of elements) */\nfunc (l *myList) size() int {\n    return l.arrSize\n}\n\n/* Get list capacity */\nfunc (l *myList) capacity() int {\n    return l.arrCapacity\n}\n\n/* Update element */\nfunc (l *myList) get(index int) int {\n    // If the index is out of bounds, throw an exception, as below\n    if index < 0 || index >= l.arrSize {\n        panic(\"Index out of bounds\")\n    }\n    return l.arr[index]\n}\n\n/* Add elements at the end */\nfunc (l *myList) set(num, index int) {\n    if index < 0 || index >= l.arrSize {\n        panic(\"Index out of bounds\")\n    }\n    l.arr[index] = num\n}\n\n/* Direct traversal of list elements */\nfunc (l *myList) add(num int) {\n    // When the number of elements exceeds capacity, trigger the extension mechanism\n    if l.arrSize == l.arrCapacity {\n        l.extendCapacity()\n    }\n    l.arr[l.arrSize] = num\n    // Update the number of elements\n    l.arrSize++\n}\n\n/* Sort list */\nfunc (l *myList) insert(num, index int) {\n    if index < 0 || index >= l.arrSize {\n        panic(\"Index out of bounds\")\n    }\n    // When the number of elements exceeds capacity, trigger the extension mechanism\n    if l.arrSize == l.arrCapacity {\n        l.extendCapacity()\n    }\n    // Move all elements after index index forward by one position\n    for j := l.arrSize - 1; j >= index; j-- {\n        l.arr[j+1] = l.arr[j]\n    }\n    l.arr[index] = num\n    // Update the number of elements\n    l.arrSize++\n}\n\n/* Remove element */\nfunc (l *myList) remove(index int) int {\n    if index < 0 || index >= l.arrSize {\n        panic(\"Index out of bounds\")\n    }\n    num := l.arr[index]\n    // Create a new array with length _extend_ratio times the original array, and copy the original array to the new array\n    for j := index; j < l.arrSize-1; j++ {\n        l.arr[j] = l.arr[j+1]\n    }\n    // Update the number of elements\n    l.arrSize--\n    // Return the removed element\n    return num\n}\n\n/* Driver Code */\nfunc (l *myList) extendCapacity() {\n    // Create a new array with length extendRatio times the original array and copy the original array to the new array\n    l.arr = append(l.arr, make([]int, l.arrCapacity*(l.extendRatio-1))...)\n    // Add elements at the end\n    l.arrCapacity = len(l.arr)\n}\n\n/* Return list with valid length */\nfunc (l *myList) toArray() []int {\n    // Elements enqueue\n    return l.arr[:l.arrSize]\n}\n
      my_list.swift
      /* List class */\nclass MyList {\n    private var arr: [Int] // Array (stores list elements)\n    private var _capacity: Int // List capacity\n    private var _size: Int // List length (current number of elements)\n    private let extendRatio: Int // Multiple by which the list capacity is extended each time\n\n    /* Constructor */\n    init() {\n        _capacity = 10\n        _size = 0\n        extendRatio = 2\n        arr = Array(repeating: 0, count: _capacity)\n    }\n\n    /* Get list length (current number of elements) */\n    func size() -> Int {\n        _size\n    }\n\n    /* Get list capacity */\n    func capacity() -> Int {\n        _capacity\n    }\n\n    /* Update element */\n    func get(index: Int) -> Int {\n        // Throw error if index out of bounds, same below\n        if index < 0 || index >= size() {\n            fatalError(\"Index out of bounds\")\n        }\n        return arr[index]\n    }\n\n    /* Add elements at the end */\n    func set(index: Int, num: Int) {\n        if index < 0 || index >= size() {\n            fatalError(\"Index out of bounds\")\n        }\n        arr[index] = num\n    }\n\n    /* Direct traversal of list elements */\n    func add(num: Int) {\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if size() == capacity() {\n            extendCapacity()\n        }\n        arr[size()] = num\n        // Update the number of elements\n        _size += 1\n    }\n\n    /* Sort list */\n    func insert(index: Int, num: Int) {\n        if index < 0 || index >= size() {\n            fatalError(\"Index out of bounds\")\n        }\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if size() == capacity() {\n            extendCapacity()\n        }\n        // Move all elements after index index forward by one position\n        for j in (index ..< size()).reversed() {\n            arr[j + 1] = arr[j]\n        }\n        arr[index] = num\n        // Update the number of elements\n        _size += 1\n    }\n\n    /* Remove element */\n    @discardableResult\n    func remove(index: Int) -> Int {\n        if index < 0 || index >= size() {\n            fatalError(\"Index out of bounds\")\n        }\n        let num = arr[index]\n        // Move all elements after index forward by one position\n        for j in index ..< (size() - 1) {\n            arr[j] = arr[j + 1]\n        }\n        // Update the number of elements\n        _size -= 1\n        // Return the removed element\n        return num\n    }\n\n    /* Driver Code */\n    func extendCapacity() {\n        // Create a new array with length extendRatio times the original array and copy the original array to the new array\n        arr = arr + Array(repeating: 0, count: capacity() * (extendRatio - 1))\n        // Add elements at the end\n        _capacity = arr.count\n    }\n\n    /* Convert list to array */\n    func toArray() -> [Int] {\n        Array(arr.prefix(size()))\n    }\n}\n
      my_list.js
      /* List class */\nclass MyList {\n    #arr = new Array(); // Array (stores list elements)\n    #capacity = 10; // List capacity\n    #size = 0; // List length (current number of elements)\n    #extendRatio = 2; // Multiple by which the list capacity is extended each time\n\n    /* Constructor */\n    constructor() {\n        this.#arr = new Array(this.#capacity);\n    }\n\n    /* Get list length (current number of elements) */\n    size() {\n        return this.#size;\n    }\n\n    /* Get list capacity */\n    capacity() {\n        return this.#capacity;\n    }\n\n    /* Update element */\n    get(index) {\n        // If the index is out of bounds, throw an exception, as below\n        if (index < 0 || index >= this.#size) throw new Error('Index out of bounds');\n        return this.#arr[index];\n    }\n\n    /* Add elements at the end */\n    set(index, num) {\n        if (index < 0 || index >= this.#size) throw new Error('Index out of bounds');\n        this.#arr[index] = num;\n    }\n\n    /* Direct traversal of list elements */\n    add(num) {\n        // If length equals capacity, need to expand\n        if (this.#size === this.#capacity) {\n            this.extendCapacity();\n        }\n        // Add new element to end of list\n        this.#arr[this.#size] = num;\n        this.#size++;\n    }\n\n    /* Sort list */\n    insert(index, num) {\n        if (index < 0 || index >= this.#size) throw new Error('Index out of bounds');\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if (this.#size === this.#capacity) {\n            this.extendCapacity();\n        }\n        // Move all elements after index index forward by one position\n        for (let j = this.#size - 1; j >= index; j--) {\n            this.#arr[j + 1] = this.#arr[j];\n        }\n        // Update the number of elements\n        this.#arr[index] = num;\n        this.#size++;\n    }\n\n    /* Remove element */\n    remove(index) {\n        if (index < 0 || index >= this.#size) throw new Error('Index out of bounds');\n        let num = this.#arr[index];\n        // Create a new array with length _extend_ratio times the original array, and copy the original array to the new array\n        for (let j = index; j < this.#size - 1; j++) {\n            this.#arr[j] = this.#arr[j + 1];\n        }\n        // Update the number of elements\n        this.#size--;\n        // Return the removed element\n        return num;\n    }\n\n    /* Driver Code */\n    extendCapacity() {\n        // Create a new array with length extendRatio times the original array and copy the original array to the new array\n        this.#arr = this.#arr.concat(\n            new Array(this.capacity() * (this.#extendRatio - 1))\n        );\n        // Add elements at the end\n        this.#capacity = this.#arr.length;\n    }\n\n    /* Convert list to array */\n    toArray() {\n        let size = this.size();\n        // Elements enqueue\n        const arr = new Array(size);\n        for (let i = 0; i < size; i++) {\n            arr[i] = this.get(i);\n        }\n        return arr;\n    }\n}\n
      my_list.ts
      /* List class */\nclass MyList {\n    private arr: Array<number>; // Array (stores list elements)\n    private _capacity: number = 10; // List capacity\n    private _size: number = 0; // List length (current number of elements)\n    private extendRatio: number = 2; // Multiple by which the list capacity is extended each time\n\n    /* Constructor */\n    constructor() {\n        this.arr = new Array(this._capacity);\n    }\n\n    /* Get list length (current number of elements) */\n    public size(): number {\n        return this._size;\n    }\n\n    /* Get list capacity */\n    public capacity(): number {\n        return this._capacity;\n    }\n\n    /* Update element */\n    public get(index: number): number {\n        // If the index is out of bounds, throw an exception, as below\n        if (index < 0 || index >= this._size) throw new Error('Index out of bounds');\n        return this.arr[index];\n    }\n\n    /* Add elements at the end */\n    public set(index: number, num: number): void {\n        if (index < 0 || index >= this._size) throw new Error('Index out of bounds');\n        this.arr[index] = num;\n    }\n\n    /* Direct traversal of list elements */\n    public add(num: number): void {\n        // If length equals capacity, need to expand\n        if (this._size === this._capacity) this.extendCapacity();\n        // Add new element to end of list\n        this.arr[this._size] = num;\n        this._size++;\n    }\n\n    /* Sort list */\n    public insert(index: number, num: number): void {\n        if (index < 0 || index >= this._size) throw new Error('Index out of bounds');\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if (this._size === this._capacity) {\n            this.extendCapacity();\n        }\n        // Move all elements after index index forward by one position\n        for (let j = this._size - 1; j >= index; j--) {\n            this.arr[j + 1] = this.arr[j];\n        }\n        // Update the number of elements\n        this.arr[index] = num;\n        this._size++;\n    }\n\n    /* Remove element */\n    public remove(index: number): number {\n        if (index < 0 || index >= this._size) throw new Error('Index out of bounds');\n        let num = this.arr[index];\n        // Move all elements after index forward by one position\n        for (let j = index; j < this._size - 1; j++) {\n            this.arr[j] = this.arr[j + 1];\n        }\n        // Update the number of elements\n        this._size--;\n        // Return the removed element\n        return num;\n    }\n\n    /* Driver Code */\n    public extendCapacity(): void {\n        // Create new array of length size and copy original array to new array\n        this.arr = this.arr.concat(\n            new Array(this.capacity() * (this.extendRatio - 1))\n        );\n        // Add elements at the end\n        this._capacity = this.arr.length;\n    }\n\n    /* Convert list to array */\n    public toArray(): number[] {\n        let size = this.size();\n        // Elements enqueue\n        const arr = new Array(size);\n        for (let i = 0; i < size; i++) {\n            arr[i] = this.get(i);\n        }\n        return arr;\n    }\n}\n
      my_list.dart
      /* List class */\nclass MyList {\n  late List<int> _arr; // Array (stores list elements)\n  int _capacity = 10; // List capacity\n  int _size = 0; // List length (current number of elements)\n  int _extendRatio = 2; // Multiple by which the list capacity is extended each time\n\n  /* Constructor */\n  MyList() {\n    _arr = List.filled(_capacity, 0);\n  }\n\n  /* Get list length (current number of elements) */\n  int size() => _size;\n\n  /* Get list capacity */\n  int capacity() => _capacity;\n\n  /* Update element */\n  int get(int index) {\n    if (index >= _size) throw RangeError('Index out of bounds');\n    return _arr[index];\n  }\n\n  /* Add elements at the end */\n  void set(int index, int _num) {\n    if (index >= _size) throw RangeError('Index out of bounds');\n    _arr[index] = _num;\n  }\n\n  /* Direct traversal of list elements */\n  void add(int _num) {\n    // When the number of elements exceeds capacity, trigger the extension mechanism\n    if (_size == _capacity) extendCapacity();\n    _arr[_size] = _num;\n    // Update the number of elements\n    _size++;\n  }\n\n  /* Sort list */\n  void insert(int index, int _num) {\n    if (index >= _size) throw RangeError('Index out of bounds');\n    // When the number of elements exceeds capacity, trigger the extension mechanism\n    if (_size == _capacity) extendCapacity();\n    // Move all elements after index index forward by one position\n    for (var j = _size - 1; j >= index; j--) {\n      _arr[j + 1] = _arr[j];\n    }\n    _arr[index] = _num;\n    // Update the number of elements\n    _size++;\n  }\n\n  /* Remove element */\n  int remove(int index) {\n    if (index >= _size) throw RangeError('Index out of bounds');\n    int _num = _arr[index];\n    // Move all elements after index forward by one position\n    for (var j = index; j < _size - 1; j++) {\n      _arr[j] = _arr[j + 1];\n    }\n    // Update the number of elements\n    _size--;\n    // Return the removed element\n    return _num;\n  }\n\n  /* Driver Code */\n  void extendCapacity() {\n    // Create new array with length _extendRatio times original array\n    final _newNums = List.filled(_capacity * _extendRatio, 0);\n    // Copy original array to new array\n    List.copyRange(_newNums, 0, _arr);\n    // Update _arr reference\n    _arr = _newNums;\n    // Add elements at the end\n    _capacity = _arr.length;\n  }\n\n  /* Convert list to array */\n  List<int> toArray() {\n    List<int> arr = [];\n    for (var i = 0; i < _size; i++) {\n      arr.add(get(i));\n    }\n    return arr;\n  }\n}\n
      my_list.rs
      /* List class */\n#[allow(dead_code)]\nstruct MyList {\n    arr: Vec<i32>,       // Array (stores list elements)\n    capacity: usize,     // List capacity\n    size: usize,         // List length (current number of elements)\n    extend_ratio: usize, // Multiple by which the list capacity is extended each time\n}\n\n#[allow(unused, unused_comparisons)]\nimpl MyList {\n    /* Constructor */\n    pub fn new(capacity: usize) -> Self {\n        let mut vec = vec![0; capacity];\n        Self {\n            arr: vec,\n            capacity,\n            size: 0,\n            extend_ratio: 2,\n        }\n    }\n\n    /* Get list length (current number of elements) */\n    pub fn size(&self) -> usize {\n        return self.size;\n    }\n\n    /* Get list capacity */\n    pub fn capacity(&self) -> usize {\n        return self.capacity;\n    }\n\n    /* Update element */\n    pub fn get(&self, index: usize) -> i32 {\n        // If the index is out of bounds, throw an exception, as below\n        if index >= self.size {\n            panic!(\"Index out of bounds\")\n        };\n        return self.arr[index];\n    }\n\n    /* Add elements at the end */\n    pub fn set(&mut self, index: usize, num: i32) {\n        if index >= self.size {\n            panic!(\"Index out of bounds\")\n        };\n        self.arr[index] = num;\n    }\n\n    /* Direct traversal of list elements */\n    pub fn add(&mut self, num: i32) {\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if self.size == self.capacity() {\n            self.extend_capacity();\n        }\n        self.arr[self.size] = num;\n        // Update the number of elements\n        self.size += 1;\n    }\n\n    /* Sort list */\n    pub fn insert(&mut self, index: usize, num: i32) {\n        if index >= self.size() {\n            panic!(\"Index out of bounds\")\n        };\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if self.size == self.capacity() {\n            self.extend_capacity();\n        }\n        // Move all elements after index index forward by one position\n        for j in (index..self.size).rev() {\n            self.arr[j + 1] = self.arr[j];\n        }\n        self.arr[index] = num;\n        // Update the number of elements\n        self.size += 1;\n    }\n\n    /* Remove element */\n    pub fn remove(&mut self, index: usize) -> i32 {\n        if index >= self.size() {\n            panic!(\"Index out of bounds\")\n        };\n        let num = self.arr[index];\n        // Create a new array with length _extend_ratio times the original array, and copy the original array to the new array\n        for j in index..self.size - 1 {\n            self.arr[j] = self.arr[j + 1];\n        }\n        // Update the number of elements\n        self.size -= 1;\n        // Return the removed element\n        return num;\n    }\n\n    /* Driver Code */\n    pub fn extend_capacity(&mut self) {\n        // Create new array with length extend_ratio times original, copy original array to new array\n        let new_capacity = self.capacity * self.extend_ratio;\n        self.arr.resize(new_capacity, 0);\n        // Add elements at the end\n        self.capacity = new_capacity;\n    }\n\n    /* Convert list to array */\n    pub fn to_array(&self) -> Vec<i32> {\n        // Elements enqueue\n        let mut arr = Vec::new();\n        for i in 0..self.size {\n            arr.push(self.get(i));\n        }\n        arr\n    }\n}\n
      my_list.c
      /* List class */\ntypedef struct {\n    int *arr;        // Array (stores list elements)\n    int capacity;    // List capacity\n    int size;        // List size\n    int extendRatio; // List expansion multiplier\n} MyList;\n\n/* Constructor */\nMyList *newMyList() {\n    MyList *nums = malloc(sizeof(MyList));\n    nums->capacity = 10;\n    nums->arr = malloc(sizeof(int) * nums->capacity);\n    nums->size = 0;\n    nums->extendRatio = 2;\n    return nums;\n}\n\n/* Destructor */\nvoid delMyList(MyList *nums) {\n    free(nums->arr);\n    free(nums);\n}\n\n/* Get list length */\nint size(MyList *nums) {\n    return nums->size;\n}\n\n/* Get list capacity */\nint capacity(MyList *nums) {\n    return nums->capacity;\n}\n\n/* Update element */\nint get(MyList *nums, int index) {\n    assert(index >= 0 && index < nums->size);\n    return nums->arr[index];\n}\n\n/* Add elements at the end */\nvoid set(MyList *nums, int index, int num) {\n    assert(index >= 0 && index < nums->size);\n    nums->arr[index] = num;\n}\n\n/* Direct traversal of list elements */\nvoid add(MyList *nums, int num) {\n    if (size(nums) == capacity(nums)) {\n        extendCapacity(nums); // Expand capacity\n    }\n    nums->arr[size(nums)] = num;\n    nums->size++;\n}\n\n/* Sort list */\nvoid insert(MyList *nums, int index, int num) {\n    assert(index >= 0 && index < size(nums));\n    // When the number of elements exceeds capacity, trigger the extension mechanism\n    if (size(nums) == capacity(nums)) {\n        extendCapacity(nums); // Expand capacity\n    }\n    for (int i = size(nums); i > index; --i) {\n        nums->arr[i] = nums->arr[i - 1];\n    }\n    nums->arr[index] = num;\n    nums->size++;\n}\n\n/* Remove element */\n// Note: stdio.h occupies the remove keyword\nint removeItem(MyList *nums, int index) {\n    assert(index >= 0 && index < size(nums));\n    int num = nums->arr[index];\n    for (int i = index; i < size(nums) - 1; i++) {\n        nums->arr[i] = nums->arr[i + 1];\n    }\n    nums->size--;\n    return num;\n}\n\n/* Driver Code */\nvoid extendCapacity(MyList *nums) {\n    // Allocate space first\n    int newCapacity = capacity(nums) * nums->extendRatio;\n    int *extend = (int *)malloc(sizeof(int) * newCapacity);\n    int *temp = nums->arr;\n\n    // Copy old data to new data\n    for (int i = 0; i < size(nums); i++)\n        extend[i] = nums->arr[i];\n\n    // Free old data\n    free(temp);\n\n    // Update new data\n    nums->arr = extend;\n    nums->capacity = newCapacity;\n}\n\n/* Convert list to Array for printing */\nint *toArray(MyList *nums) {\n    return nums->arr;\n}\n
      my_list.kt
      /* List class */\nclass MyList {\n    private var arr: IntArray = intArrayOf() // Array (stores list elements)\n    private var capacity: Int = 10 // List capacity\n    private var size: Int = 0 // List length (current number of elements)\n    private var extendRatio: Int = 2 // Multiple by which the list capacity is extended each time\n\n    /* Constructor */\n    init {\n        arr = IntArray(capacity)\n    }\n\n    /* Get list length (current number of elements) */\n    fun size(): Int {\n        return size\n    }\n\n    /* Get list capacity */\n    fun capacity(): Int {\n        return capacity\n    }\n\n    /* Update element */\n    fun get(index: Int): Int {\n        // If the index is out of bounds, throw an exception, as below\n        if (index < 0 || index >= size)\n            throw IndexOutOfBoundsException(\"Index out of bounds\")\n        return arr[index]\n    }\n\n    /* Add elements at the end */\n    fun set(index: Int, num: Int) {\n        if (index < 0 || index >= size)\n            throw IndexOutOfBoundsException(\"Index out of bounds\")\n        arr[index] = num\n    }\n\n    /* Direct traversal of list elements */\n    fun add(num: Int) {\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if (size == capacity())\n            extendCapacity()\n        arr[size] = num\n        // Update the number of elements\n        size++\n    }\n\n    /* Sort list */\n    fun insert(index: Int, num: Int) {\n        if (index < 0 || index >= size)\n            throw IndexOutOfBoundsException(\"Index out of bounds\")\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if (size == capacity())\n            extendCapacity()\n        // Move all elements after index index forward by one position\n        for (j in size - 1 downTo index)\n            arr[j + 1] = arr[j]\n        arr[index] = num\n        // Update the number of elements\n        size++\n    }\n\n    /* Remove element */\n    fun remove(index: Int): Int {\n        if (index < 0 || index >= size)\n            throw IndexOutOfBoundsException(\"Index out of bounds\")\n        val num = arr[index]\n        // Move all elements after index forward by one position\n        for (j in index..<size - 1)\n            arr[j] = arr[j + 1]\n        // Update the number of elements\n        size--\n        // Return the removed element\n        return num\n    }\n\n    /* Driver Code */\n    fun extendCapacity() {\n        // Create a new array with length extendRatio times the original array and copy the original array to the new array\n        arr = arr.copyOf(capacity() * extendRatio)\n        // Add elements at the end\n        capacity = arr.size\n    }\n\n    /* Convert list to array */\n    fun toArray(): IntArray {\n        val size = size()\n        // Elements enqueue\n        val arr = IntArray(size)\n        for (i in 0..<size) {\n            arr[i] = get(i)\n        }\n        return arr\n    }\n}\n
      my_list.rb
      ### List class ###\nclass MyList\n  attr_reader :size       # Get list length (current number of elements)\n  attr_reader :capacity   # Get list capacity\n\n  ### Constructor ###\n  def initialize\n    @capacity = 10\n    @size = 0\n    @extend_ratio = 2\n    @arr = Array.new(capacity)\n  end\n\n  ### Access element ###\n  def get(index)\n    # If the index is out of bounds, throw an exception, as below\n    raise IndexError, \"Index out of bounds\" if index < 0 || index >= size\n    @arr[index]\n  end\n\n  ### Access element ###\n  def set(index, num)\n    raise IndexError, \"Index out of bounds\" if index < 0 || index >= size\n    @arr[index] = num\n  end\n\n  ### Add element at end ###\n  def add(num)\n    # When the number of elements exceeds capacity, trigger the extension mechanism\n    extend_capacity if size == capacity\n    @arr[size] = num\n\n    # Update the number of elements\n    @size += 1\n  end\n\n  ### Insert element in middle ###\n  def insert(index, num)\n    raise IndexError, \"Index out of bounds\" if index < 0 || index >= size\n\n    # When the number of elements exceeds capacity, trigger the extension mechanism\n    extend_capacity if size == capacity\n\n    # Move all elements after index index forward by one position\n    for j in (size - 1).downto(index)\n      @arr[j + 1] = @arr[j]\n    end\n    @arr[index] = num\n\n    # Update the number of elements\n    @size += 1\n  end\n\n  ### Delete element ###\n  def remove(index)\n    raise IndexError, \"Index out of bounds\" if index < 0 || index >= size\n    num = @arr[index]\n\n    # Move all elements after index forward by one position\n    for j in index...size\n      @arr[j] = @arr[j + 1]\n    end\n\n    # Update the number of elements\n    @size -= 1\n\n    # Return the removed element\n    num\n  end\n\n  ### Expand list capacity ###\n  def extend_capacity\n    # Create new array with length extend_ratio times original, copy original array to new array\n    arr = @arr.dup + Array.new(capacity * (@extend_ratio - 1))\n    # Add elements at the end\n    @capacity = arr.length\n  end\n\n  ### Convert list to array ###\n  def to_array\n    sz = size\n    # Elements enqueue\n    arr = Array.new(sz)\n    for i in 0...sz\n      arr[i] = get(i)\n    end\n    arr\n  end\nend\n
      "},{"location":"chapter_array_and_linkedlist/ram_and_cache/","title":"4.4 \u00a0 Random-Access Memory and Cache *","text":"

      In the first two sections of this chapter, we explored arrays and linked lists, two fundamental and important data structures that represent \"contiguous storage\" and \"distributed storage\" as two physical structures, respectively.

      In fact, physical structure largely determines the efficiency with which programs utilize memory and cache, which in turn affects the overall performance of algorithmic programs.

      "},{"location":"chapter_array_and_linkedlist/ram_and_cache/#441-computer-storage-devices","title":"4.4.1 \u00a0 Computer Storage Devices","text":"

      Computers include three types of storage devices: hard disk, random-access memory (RAM), and cache memory. The following table shows their different roles and performance characteristics in a computer system.

      Table 4-2 \u00a0 Computer Storage Devices

      Hard Disk RAM Cache Purpose Long-term storage of data, including operating systems, programs, and files Temporary storage of currently running programs and data being processed Storage of frequently accessed data and instructions to reduce CPU's accesses to memory Volatility Data is not lost after power-off Data is lost after power-off Data is lost after power-off Capacity Large, on the order of terabytes (TB) Small, on the order of gigabytes (GB) Very small, on the order of megabytes (MB) Speed Slow, hundreds to thousands of MB/s Fast, tens of GB/s Very fast, tens to hundreds of GB/s Cost (USD/GB) Inexpensive, fractions of a dollar to a few dollars per GB Expensive, tens to hundreds of dollars per GB Very expensive, priced as part of the CPU package

      We can imagine the computer storage system as a pyramid structure as shown in the diagram below. Storage devices closer to the top of the pyramid are faster, have smaller capacity, and are more expensive. This multi-layered design is not by accident, but rather the result of careful consideration by computer scientists and engineers.

      • Hard disk cannot be easily replaced by RAM. First, data in memory is lost after power-off, making it unsuitable for long-term data storage. Second, memory is tens of times more expensive than hard disk, which makes it difficult to popularize in the consumer market.
      • Cache cannot simultaneously achieve large capacity and high speed. As the capacity of L1, L2, and L3 caches increases, their physical size becomes larger, and the physical distance between them and the CPU core increases, resulting in longer data transmission time and higher element access latency. With current technology, the multi-layered cache structure represents the best balance point between capacity, speed, and cost.

      Figure 4-9 \u00a0 Computer Storage System

      Tip

      The storage hierarchy of computers embodies a delicate balance among speed, capacity, and cost. In fact, such trade-offs are common across all industrial fields, requiring us to find the optimal balance point between different advantages and constraints.

      In summary, hard disk is used for long-term storage of large amounts of data, RAM is used for temporary storage of data being processed during program execution, and cache is used for storage of frequently accessed data and instructions, to improve program execution efficiency. The three work together to ensure efficient operation of the computer system.

      As shown in the diagram below, during program execution, data is read from the hard disk into RAM for CPU computation. Cache can be viewed as part of the CPU, it intelligently loads data from RAM, providing the CPU with high-speed data reading, thereby significantly improving program execution efficiency and reducing reliance on slower RAM.

      Figure 4-10 \u00a0 Data Flow Among Hard Disk, RAM, and Cache

      "},{"location":"chapter_array_and_linkedlist/ram_and_cache/#442-memory-efficiency-of-data-structures","title":"4.4.2 \u00a0 Memory Efficiency of Data Structures","text":"

      In terms of memory space utilization, arrays and linked lists each have advantages and limitations.

      On one hand, memory is limited, and the same memory cannot be shared by multiple programs, so we hope data structures can utilize space as efficiently as possible. Array elements are tightly packed and do not require additional space to store references (pointers) between linked list nodes, thus having higher space efficiency. However, arrays need to allocate sufficient contiguous memory space at once, which may lead to memory waste, and array expansion requires additional time and space costs. In comparison, linked lists perform dynamic memory allocation and deallocation on a \"node\" basis, providing greater flexibility.

      On the other hand, during program execution, as memory is repeatedly allocated and freed, the degree of fragmentation of free memory becomes increasingly severe, leading to reduced memory utilization efficiency. Arrays, due to their contiguous storage approach, are relatively less prone to memory fragmentation. Conversely, linked list elements are distributed in storage, and frequent insertion and deletion operations are more likely to cause memory fragmentation.

      "},{"location":"chapter_array_and_linkedlist/ram_and_cache/#443-cache-efficiency-of-data-structures","title":"4.4.3 \u00a0 Cache Efficiency of Data Structures","text":"

      Although cache has much smaller space capacity than memory, it is much faster than memory and plays a crucial role in program execution speed. Since cache capacity is limited and can only store a small portion of frequently accessed data, when the CPU attempts to access data that is not in the cache, a cache miss occurs, and the CPU must load the required data from the slower memory.

      Clearly, the fewer \"cache misses,\" the higher the efficiency of CPU data reads and writes, and the better the program performance. We call the proportion of data that the CPU successfully obtains from the cache the cache hit rate, a metric typically used to measure cache efficiency.

      To achieve the highest efficiency possible, cache employs the following data loading mechanisms.

      • Cache lines: The cache does not store and load data on a byte-by-byte basis, but rather as cache lines. Compared to byte-by-byte transmission, cache line transmission is more efficient.
      • Prefetching mechanism: The processor attempts to predict data access patterns (e.g., sequential access, fixed-stride jumping access, etc.) and loads data into the cache according to specific patterns, thereby improving hit rate.
      • Spatial locality: If a piece of data is accessed, nearby data may also be accessed in the near future. Therefore, when the cache loads a particular piece of data, it also loads nearby data to improve hit rate.
      • Temporal locality: If a piece of data is accessed, it is likely to be accessed again in the near future. Cache leverages this principle by retaining recently accessed data to improve hit rate.

      In fact, arrays and linked lists have different efficiencies in utilizing cache, manifested in the following aspects.

      • Space occupied: Linked list elements occupy more space than array elements, resulting in fewer effective data in the cache.
      • Cache lines: Linked list data are scattered throughout memory, while cache loads \"by lines,\" so the proportion of invalid data loaded is higher.
      • Prefetching mechanism: Arrays have more \"predictable\" data access patterns than linked lists, making it easier for the system to guess which data will be loaded next.
      • Spatial locality: Arrays are stored in centralized memory space, so data near loaded data is more likely to be accessed soon.

      Overall, arrays have higher cache hit rates, thus they usually outperform linked lists in operation efficiency. This makes data structures implemented based on arrays more popular when solving algorithmic problems.

      It is important to note that high cache efficiency does not mean arrays are superior to linked lists in all cases. In practical applications, which data structure to choose should be determined based on specific requirements. For example, both arrays and linked lists can implement the \"stack\" data structure (which will be discussed in detail in the next chapter), but they are suitable for different scenarios.

      • When solving algorithm problems, we tend to prefer stack implementations based on arrays, because they provide higher operation efficiency and the ability of random access, at the cost of needing to pre-allocate a certain amount of memory space for the array.
      • If the data volume is very large, the dynamic nature is high, and the expected size of the stack is difficult to estimate, then a stack implementation based on linked lists is more suitable. Linked lists can distribute large amounts of data across different parts of memory and avoid the additional overhead produced by array expansion.
      "},{"location":"chapter_array_and_linkedlist/summary/","title":"4.5 \u00a0 Summary","text":""},{"location":"chapter_array_and_linkedlist/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • Arrays and linked lists are two fundamental data structures, representing two different ways data can be stored in computer memory: contiguous memory storage and scattered memory storage. The characteristics of the two complement each other.
      • Arrays support random access and use less memory; however, inserting and deleting elements is inefficient, and the length is immutable after initialization.
      • Linked lists achieve efficient insertion and deletion of nodes by modifying references (pointers), and can flexibly adjust length; however, node access is inefficient and memory consumption is higher. Common linked list types include singly linked lists, circular linked lists, and doubly linked lists.
      • A list is an ordered collection of elements that supports insertion, deletion, search, and modification, typically implemented based on dynamic arrays. It retains the advantages of arrays while allowing flexible adjustment of length.
      • The emergence of lists has greatly improved the practicality of arrays, but may result in some wasted memory space.
      • During program execution, data is primarily stored in memory. Arrays provide higher memory space efficiency, while linked lists offer greater flexibility in memory usage.
      • Caches provide fast data access to the CPU through mechanisms such as cache lines, prefetching, and spatial and temporal locality, significantly improving program execution efficiency.
      • Because arrays have higher cache hit rates, they are generally more efficient than linked lists. When choosing a data structure, appropriate selection should be made based on specific requirements and scenarios.
      "},{"location":"chapter_array_and_linkedlist/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

      Q: Does storing an array on the stack versus on the heap affect time efficiency and space efficiency?

      Arrays stored on the stack and on the heap are both stored in contiguous memory space, so data operation efficiency is basically the same. However, the stack and heap have their own characteristics, leading to the following differences.

      1. Allocation and deallocation efficiency: The stack is a relatively small piece of memory, with allocation automatically handled by the compiler; the heap is relatively larger and can be dynamically allocated in code, more prone to fragmentation. Therefore, allocation and deallocation operations on the heap are usually slower than on the stack.
      2. Size limitations: Stack memory is relatively small, and the heap size is generally limited by available memory. Therefore, the heap is more suitable for storing large arrays.
      3. Flexibility: The size of an array on the stack must be determined at compile time, while the size of an array on the heap can be determined dynamically at runtime.

      Q: Why do arrays require elements of the same type, while linked lists do not emphasize this requirement?

      Linked lists are composed of nodes, with nodes connected through references (pointers), and each node can store different types of data, such as int, double, string, object, etc.

      In contrast, array elements must be of the same type, so that the corresponding element position can be obtained by calculating the offset. For example, if an array contains both int and long types, with individual elements occupying 4 bytes and 8 bytes respectively, then the following formula cannot be used to calculate the offset, because the array contains two different \"element lengths\".

      # Element Memory Address = Array Memory Address (first Element Memory address) + Element Length * Element Index\n

      Q: After deleting node P, do we need to set P.next to None?

      It is not necessary to modify P.next. From the perspective of the linked list, traversing from the head node to the tail node will no longer encounter P. This means that node P has been removed from the linked list, and it doesn't matter where node P points to at this time\u2014it won't affect the linked list.

      From a data structures and algorithms perspective (problem-solving), not disconnecting the pointer doesn't matter as long as the program logic is correct. From the perspective of standard libraries, disconnecting is safer and the logic is clearer. If not disconnected, assuming the deleted node is not properly reclaimed, it may affect the memory reclamation of its successor nodes.

      Q: In a linked list, the time complexity of insertion and deletion operations is \\(O(1)\\). However, both insertion and deletion require \\(O(n)\\) time to find the element; why isn't the time complexity \\(O(n)\\)?

      If the element is first found and then deleted, the time complexity is indeed \\(O(n)\\). However, the advantage of \\(O(1)\\) insertion and deletion in linked lists can be demonstrated in other applications. For example, a deque is well-suited for linked list implementation, where we maintain pointer variables always pointing to the head and tail nodes, with each insertion and deletion operation being \\(O(1)\\).

      Q: In the diagram \"Linked List Definition and Storage Methods\", does the light blue pointer node occupy a single memory address, or does it share equally with the node value?

      This diagram is a qualitative representation; a quantitative representation requires analysis based on the specific situation.

      • Different types of node values occupy different amounts of space, such as int, long, double, and instance objects, etc.
      • The amount of memory space occupied by pointer variables depends on the operating system and compilation environment used, usually 8 bytes or 4 bytes.

      Q: Is appending an element at the end of a list always \\(O(1)\\)?

      If appending an element exceeds the list length, the list must first be expanded before adding. The system allocates a new block of memory and moves all elements from the original list to it, in which case the time complexity becomes \\(O(n)\\).

      Q: \"The emergence of lists has greatly improved the practicality of arrays, but may result in some wasted memory space\"\u2014does this space waste refer to the memory occupied by additional variables such as capacity, length, and expansion factor?

      This space waste mainly has two aspects: on one hand, lists typically set an initial length, which we may not need to fully utilize; on the other hand, to prevent frequent expansion, expansion generally multiplies by a coefficient, such as \\(\\times 1.5\\). As a result, there will be many empty positions that we typically cannot completely fill.

      Q: In Python, after initializing n = [1, 2, 3], the addresses of these 3 elements are contiguous, but initializing m = [2, 1, 3] reveals that each element's id is not continuous; rather, they are the same as those in n. Since the addresses of these elements are not contiguous, is m still an array?

      If we replace list elements with linked list nodes n = [n1, n2, n3, n4, n5], usually these 5 node objects are also scattered throughout memory. However, given a list index, we can still obtain the node memory address in \\(O(1)\\) time, thereby accessing the corresponding node. This is because the array stores references to nodes, not the nodes themselves.

      Unlike many languages, numbers in Python are wrapped as objects, and lists store not the numbers themselves, but references to the numbers. Therefore, we find that the same numbers in two arrays have the same id, and the memory addresses of these numbers need not be contiguous.

      Q: C++ STL has std::list which has already implemented a doubly linked list, but it seems that some algorithm books don't use it directly. Is there a limitation?

      On one hand, we often prefer to use arrays for implementing algorithms and only use linked lists when necessary, mainly for two reasons.

      • Space overhead: Since each element requires two additional pointers (one for the previous element and one for the next element), std::list typically consumes more space than std::vector.
      • Cache unfriendliness: Since data is not stored contiguously, std::list has lower cache utilization. In general, std::vector has better performance.

      On the other hand, cases where linked lists are necessary mainly involve binary trees and graphs. Stacks and queues usually use the stack and queue provided by the programming language, rather than linked lists.

      Q: Does the operation res = [[0]] * n create a 2D list where each [0] is independent?

      No, they are not independent. In this 2D list, all the [0] are actually references to the same object. If we modify one element, we will find that all corresponding elements change accordingly.

      If we want each [0] in the 2D list to be independent, we can use res = [[0] for _ in range(n)] to achieve this. The principle of this approach is to initialize \\(n\\) independent [0] list objects.

      Q: Does the operation res = [0] * n create a list where each integer 0 is independent?

      In this list, all integer 0s are references to the same object. This is because Python uses a caching mechanism for small integers (typically -5 to 256) to maximize object reuse and improve performance.

      Although they point to the same object, we can still independently modify each element in the list. This is because Python integers are \"immutable objects\". When we modify an element, we are actually switching to a reference of another object, rather than changing the original object itself.

      However, when list elements are \"mutable objects\" (such as lists, dictionaries, or class instances), modifying an element directly changes the object itself, and all elements referencing that object will have the same change.

      "},{"location":"chapter_backtracking/","title":"Chapter 13. \u00a0 Backtracking","text":"

      Abstract

      We are like explorers in a maze, and may encounter difficulties on the path forward.

      The power of backtracking allows us to start over, keep trying, and eventually find the exit leading to light.

      "},{"location":"chapter_backtracking/#chapter-contents","title":"Chapter contents","text":"
      • 13.1 \u00a0 Backtracking Algorithm
      • 13.2 \u00a0 Permutations Problem
      • 13.3 \u00a0 Subset-Sum Problem
      • 13.4 \u00a0 N-Queens Problem
      • 13.5 \u00a0 Summary
      "},{"location":"chapter_backtracking/backtracking_algorithm/","title":"13.1 \u00a0 Backtracking Algorithm","text":"

      The backtracking algorithm is a method for solving problems through exhaustive search. Its core idea is to start from an initial state and exhaustively search all possible solutions. When a correct solution is found, it is recorded. This process continues until a solution is found or all possible choices have been tried without finding a solution.

      The backtracking algorithm typically employs \"depth-first search\" to traverse the solution space. In the \"Binary Tree\" chapter, we mentioned that preorder, inorder, and postorder traversals all belong to depth-first search. Next, we will construct a backtracking problem using preorder traversal to progressively understand how the backtracking algorithm works.

      Example 1

      Given a binary tree, search and record all nodes with value \\(7\\), and return a list of these nodes.

      For this problem, we perform a preorder traversal of the tree and check whether the current node's value is \\(7\\). If it is, we add the node to the result list res. The relevant implementation is shown in the following figure and code:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_i_compact.py
      def pre_order(root: TreeNode):\n    \"\"\"Preorder traversal: Example 1\"\"\"\n    if root is None:\n        return\n    if root.val == 7:\n        # Record solution\n        res.append(root)\n    pre_order(root.left)\n    pre_order(root.right)\n
      preorder_traversal_i_compact.cpp
      /* Preorder traversal: Example 1 */\nvoid preOrder(TreeNode *root) {\n    if (root == nullptr) {\n        return;\n    }\n    if (root->val == 7) {\n        // Record solution\n        res.push_back(root);\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n}\n
      preorder_traversal_i_compact.java
      /* Preorder traversal: Example 1 */\nvoid preOrder(TreeNode root) {\n    if (root == null) {\n        return;\n    }\n    if (root.val == 7) {\n        // Record solution\n        res.add(root);\n    }\n    preOrder(root.left);\n    preOrder(root.right);\n}\n
      preorder_traversal_i_compact.cs
      /* Preorder traversal: Example 1 */\nvoid PreOrder(TreeNode? root) {\n    if (root == null) {\n        return;\n    }\n    if (root.val == 7) {\n        // Record solution\n        res.Add(root);\n    }\n    PreOrder(root.left);\n    PreOrder(root.right);\n}\n
      preorder_traversal_i_compact.go
      /* Preorder traversal: Example 1 */\nfunc preOrderI(root *TreeNode, res *[]*TreeNode) {\n    if root == nil {\n        return\n    }\n    if (root.Val).(int) == 7 {\n        // Record solution\n        *res = append(*res, root)\n    }\n    preOrderI(root.Left, res)\n    preOrderI(root.Right, res)\n}\n
      preorder_traversal_i_compact.swift
      /* Preorder traversal: Example 1 */\nfunc preOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    if root.val == 7 {\n        // Record solution\n        res.append(root)\n    }\n    preOrder(root: root.left)\n    preOrder(root: root.right)\n}\n
      preorder_traversal_i_compact.js
      /* Preorder traversal: Example 1 */\nfunction preOrder(root, res) {\n    if (root === null) {\n        return;\n    }\n    if (root.val === 7) {\n        // Record solution\n        res.push(root);\n    }\n    preOrder(root.left, res);\n    preOrder(root.right, res);\n}\n
      preorder_traversal_i_compact.ts
      /* Preorder traversal: Example 1 */\nfunction preOrder(root: TreeNode | null, res: TreeNode[]): void {\n    if (root === null) {\n        return;\n    }\n    if (root.val === 7) {\n        // Record solution\n        res.push(root);\n    }\n    preOrder(root.left, res);\n    preOrder(root.right, res);\n}\n
      preorder_traversal_i_compact.dart
      /* Preorder traversal: Example 1 */\nvoid preOrder(TreeNode? root, List<TreeNode> res) {\n  if (root == null) {\n    return;\n  }\n  if (root.val == 7) {\n    // Record solution\n    res.add(root);\n  }\n  preOrder(root.left, res);\n  preOrder(root.right, res);\n}\n
      preorder_traversal_i_compact.rs
      /* Preorder traversal: Example 1 */\nfn pre_order(res: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<&Rc<RefCell<TreeNode>>>) {\n    if root.is_none() {\n        return;\n    }\n    if let Some(node) = root {\n        if node.borrow().val == 7 {\n            // Record solution\n            res.push(node.clone());\n        }\n        pre_order(res, node.borrow().left.as_ref());\n        pre_order(res, node.borrow().right.as_ref());\n    }\n}\n
      preorder_traversal_i_compact.c
      /* Preorder traversal: Example 1 */\nvoid preOrder(TreeNode *root) {\n    if (root == NULL) {\n        return;\n    }\n    if (root->val == 7) {\n        // Record solution\n        res[resSize++] = root;\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n}\n
      preorder_traversal_i_compact.kt
      /* Preorder traversal: Example 1 */\nfun preOrder(root: TreeNode?) {\n    if (root == null) {\n        return\n    }\n    if (root._val == 7) {\n        // Record solution\n        res!!.add(root)\n    }\n    preOrder(root.left)\n    preOrder(root.right)\n}\n
      preorder_traversal_i_compact.rb
      ### Pre-order traversal: example 1 ###\ndef pre_order(root)\n  return unless root\n\n  # Record solution\n  $res << root if root.val == 7\n\n  pre_order(root.left)\n  pre_order(root.right)\nend\n

      Figure 13-1 \u00a0 Search for nodes in preorder traversal

      "},{"location":"chapter_backtracking/backtracking_algorithm/#1311-attempt-and-backtrack","title":"13.1.1 \u00a0 Attempt and Backtrack","text":"

      The reason it is called a backtracking algorithm is that it employs \"attempt\" and \"backtrack\" strategies when searching the solution space. When the algorithm encounters a state where it cannot continue forward or cannot find a solution that satisfies the constraints, it will undo the previous choice, return to a previous state, and try other possible choices.

      For Example 1, visiting each node represents an \"attempt\", while skipping over a leaf node or a function return from the parent node represents a \"backtrack\".

      It is worth noting that backtracking is not limited to function returns alone. To illustrate this, let's extend Example 1 slightly.

      Example 2

      In a binary tree, search all nodes with value \\(7\\), and return the paths from the root node to these nodes.

      Based on the code from Example 1, we need to use a list path to record the visited node path. When we reach a node with value \\(7\\), we copy path and add it to the result list res. After traversal is complete, res contains all the solutions. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_ii_compact.py
      def pre_order(root: TreeNode):\n    \"\"\"Preorder traversal: Example 2\"\"\"\n    if root is None:\n        return\n    # Attempt\n    path.append(root)\n    if root.val == 7:\n        # Record solution\n        res.append(list(path))\n    pre_order(root.left)\n    pre_order(root.right)\n    # Backtrack\n    path.pop()\n
      preorder_traversal_ii_compact.cpp
      /* Preorder traversal: Example 2 */\nvoid preOrder(TreeNode *root) {\n    if (root == nullptr) {\n        return;\n    }\n    // Attempt\n    path.push_back(root);\n    if (root->val == 7) {\n        // Record solution\n        res.push_back(path);\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // Backtrack\n    path.pop_back();\n}\n
      preorder_traversal_ii_compact.java
      /* Preorder traversal: Example 2 */\nvoid preOrder(TreeNode root) {\n    if (root == null) {\n        return;\n    }\n    // Attempt\n    path.add(root);\n    if (root.val == 7) {\n        // Record solution\n        res.add(new ArrayList<>(path));\n    }\n    preOrder(root.left);\n    preOrder(root.right);\n    // Backtrack\n    path.remove(path.size() - 1);\n}\n
      preorder_traversal_ii_compact.cs
      /* Preorder traversal: Example 2 */\nvoid PreOrder(TreeNode? root) {\n    if (root == null) {\n        return;\n    }\n    // Attempt\n    path.Add(root);\n    if (root.val == 7) {\n        // Record solution\n        res.Add(new List<TreeNode>(path));\n    }\n    PreOrder(root.left);\n    PreOrder(root.right);\n    // Backtrack\n    path.RemoveAt(path.Count - 1);\n}\n
      preorder_traversal_ii_compact.go
      /* Preorder traversal: Example 2 */\nfunc preOrderII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {\n    if root == nil {\n        return\n    }\n    // Attempt\n    *path = append(*path, root)\n    if root.Val.(int) == 7 {\n        // Record solution\n        *res = append(*res, append([]*TreeNode{}, *path...))\n    }\n    preOrderII(root.Left, res, path)\n    preOrderII(root.Right, res, path)\n    // Backtrack\n    *path = (*path)[:len(*path)-1]\n}\n
      preorder_traversal_ii_compact.swift
      /* Preorder traversal: Example 2 */\nfunc preOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    // Attempt\n    path.append(root)\n    if root.val == 7 {\n        // Record solution\n        res.append(path)\n    }\n    preOrder(root: root.left)\n    preOrder(root: root.right)\n    // Backtrack\n    path.removeLast()\n}\n
      preorder_traversal_ii_compact.js
      /* Preorder traversal: Example 2 */\nfunction preOrder(root, path, res) {\n    if (root === null) {\n        return;\n    }\n    // Attempt\n    path.push(root);\n    if (root.val === 7) {\n        // Record solution\n        res.push([...path]);\n    }\n    preOrder(root.left, path, res);\n    preOrder(root.right, path, res);\n    // Backtrack\n    path.pop();\n}\n
      preorder_traversal_ii_compact.ts
      /* Preorder traversal: Example 2 */\nfunction preOrder(\n    root: TreeNode | null,\n    path: TreeNode[],\n    res: TreeNode[][]\n): void {\n    if (root === null) {\n        return;\n    }\n    // Attempt\n    path.push(root);\n    if (root.val === 7) {\n        // Record solution\n        res.push([...path]);\n    }\n    preOrder(root.left, path, res);\n    preOrder(root.right, path, res);\n    // Backtrack\n    path.pop();\n}\n
      preorder_traversal_ii_compact.dart
      /* Preorder traversal: Example 2 */\nvoid preOrder(\n  TreeNode? root,\n  List<TreeNode> path,\n  List<List<TreeNode>> res,\n) {\n  if (root == null) {\n    return;\n  }\n\n  // Attempt\n  path.add(root);\n  if (root.val == 7) {\n    // Record solution\n    res.add(List.from(path));\n  }\n  preOrder(root.left, path, res);\n  preOrder(root.right, path, res);\n  // Backtrack\n  path.removeLast();\n}\n
      preorder_traversal_ii_compact.rs
      /* Preorder traversal: Example 2 */\nfn pre_order(\n    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,\n    path: &mut Vec<Rc<RefCell<TreeNode>>>,\n    root: Option<&Rc<RefCell<TreeNode>>>,\n) {\n    if root.is_none() {\n        return;\n    }\n    if let Some(node) = root {\n        // Attempt\n        path.push(node.clone());\n        if node.borrow().val == 7 {\n            // Record solution\n            res.push(path.clone());\n        }\n        pre_order(res, path, node.borrow().left.as_ref());\n        pre_order(res, path, node.borrow().right.as_ref());\n        // Backtrack\n        path.pop();\n    }\n}\n
      preorder_traversal_ii_compact.c
      /* Preorder traversal: Example 2 */\nvoid preOrder(TreeNode *root) {\n    if (root == NULL) {\n        return;\n    }\n    // Attempt\n    path[pathSize++] = root;\n    if (root->val == 7) {\n        // Record solution\n        for (int i = 0; i < pathSize; ++i) {\n            res[resSize][i] = path[i];\n        }\n        resSize++;\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // Backtrack\n    pathSize--;\n}\n
      preorder_traversal_ii_compact.kt
      /* Preorder traversal: Example 2 */\nfun preOrder(root: TreeNode?) {\n    if (root == null) {\n        return\n    }\n    // Attempt\n    path!!.add(root)\n    if (root._val == 7) {\n        // Record solution\n        res!!.add(path!!.toMutableList())\n    }\n    preOrder(root.left)\n    preOrder(root.right)\n    // Backtrack\n    path!!.removeAt(path!!.size - 1)\n}\n
      preorder_traversal_ii_compact.rb
      ### Pre-order traversal: example 2 ###\ndef pre_order(root)\n  return unless root\n\n  # Attempt\n  $path << root\n\n  # Record solution\n  $res << $path.dup if root.val == 7\n\n  pre_order(root.left)\n  pre_order(root.right)\n\n  # Backtrack\n  $path.pop\nend\n

      In each \"attempt\", we record the path by adding the current node to path; before \"backtracking\", we need to remove the node from path, to restore the state before this attempt.

      Observing the process shown in the following figure, we can understand attempt and backtrack as \"advance\" and \"undo\", two operations that are the reverse of each other.

      <1><2><3><4><5><6><7><8><9><10><11>

      Figure 13-2 \u00a0 Attempt and backtrack

      "},{"location":"chapter_backtracking/backtracking_algorithm/#1312-pruning","title":"13.1.2 \u00a0 Pruning","text":"

      Complex backtracking problems usually contain one or more constraints. Constraints can typically be used for \"pruning\".

      Example 3

      In a binary tree, search all nodes with value \\(7\\) and return the paths from the root node to these nodes, but require that the paths do not contain nodes with value \\(3\\).

      To satisfy the above constraints, we need to add pruning operations: during the search process, if we encounter a node with value \\(3\\), we return early and do not continue searching. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_iii_compact.py
      def pre_order(root: TreeNode):\n    \"\"\"Preorder traversal: Example 3\"\"\"\n    # Pruning\n    if root is None or root.val == 3:\n        return\n    # Attempt\n    path.append(root)\n    if root.val == 7:\n        # Record solution\n        res.append(list(path))\n    pre_order(root.left)\n    pre_order(root.right)\n    # Backtrack\n    path.pop()\n
      preorder_traversal_iii_compact.cpp
      /* Preorder traversal: Example 3 */\nvoid preOrder(TreeNode *root) {\n    // Pruning\n    if (root == nullptr || root->val == 3) {\n        return;\n    }\n    // Attempt\n    path.push_back(root);\n    if (root->val == 7) {\n        // Record solution\n        res.push_back(path);\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // Backtrack\n    path.pop_back();\n}\n
      preorder_traversal_iii_compact.java
      /* Preorder traversal: Example 3 */\nvoid preOrder(TreeNode root) {\n    // Pruning\n    if (root == null || root.val == 3) {\n        return;\n    }\n    // Attempt\n    path.add(root);\n    if (root.val == 7) {\n        // Record solution\n        res.add(new ArrayList<>(path));\n    }\n    preOrder(root.left);\n    preOrder(root.right);\n    // Backtrack\n    path.remove(path.size() - 1);\n}\n
      preorder_traversal_iii_compact.cs
      /* Preorder traversal: Example 3 */\nvoid PreOrder(TreeNode? root) {\n    // Pruning\n    if (root == null || root.val == 3) {\n        return;\n    }\n    // Attempt\n    path.Add(root);\n    if (root.val == 7) {\n        // Record solution\n        res.Add(new List<TreeNode>(path));\n    }\n    PreOrder(root.left);\n    PreOrder(root.right);\n    // Backtrack\n    path.RemoveAt(path.Count - 1);\n}\n
      preorder_traversal_iii_compact.go
      /* Preorder traversal: Example 3 */\nfunc preOrderIII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {\n    // Pruning\n    if root == nil || root.Val == 3 {\n        return\n    }\n    // Attempt\n    *path = append(*path, root)\n    if root.Val.(int) == 7 {\n        // Record solution\n        *res = append(*res, append([]*TreeNode{}, *path...))\n    }\n    preOrderIII(root.Left, res, path)\n    preOrderIII(root.Right, res, path)\n    // Backtrack\n    *path = (*path)[:len(*path)-1]\n}\n
      preorder_traversal_iii_compact.swift
      /* Preorder traversal: Example 3 */\nfunc preOrder(root: TreeNode?) {\n    // Pruning\n    guard let root = root, root.val != 3 else {\n        return\n    }\n    // Attempt\n    path.append(root)\n    if root.val == 7 {\n        // Record solution\n        res.append(path)\n    }\n    preOrder(root: root.left)\n    preOrder(root: root.right)\n    // Backtrack\n    path.removeLast()\n}\n
      preorder_traversal_iii_compact.js
      /* Preorder traversal: Example 3 */\nfunction preOrder(root, path, res) {\n    // Pruning\n    if (root === null || root.val === 3) {\n        return;\n    }\n    // Attempt\n    path.push(root);\n    if (root.val === 7) {\n        // Record solution\n        res.push([...path]);\n    }\n    preOrder(root.left, path, res);\n    preOrder(root.right, path, res);\n    // Backtrack\n    path.pop();\n}\n
      preorder_traversal_iii_compact.ts
      /* Preorder traversal: Example 3 */\nfunction preOrder(\n    root: TreeNode | null,\n    path: TreeNode[],\n    res: TreeNode[][]\n): void {\n    // Pruning\n    if (root === null || root.val === 3) {\n        return;\n    }\n    // Attempt\n    path.push(root);\n    if (root.val === 7) {\n        // Record solution\n        res.push([...path]);\n    }\n    preOrder(root.left, path, res);\n    preOrder(root.right, path, res);\n    // Backtrack\n    path.pop();\n}\n
      preorder_traversal_iii_compact.dart
      /* Preorder traversal: Example 3 */\nvoid preOrder(\n  TreeNode? root,\n  List<TreeNode> path,\n  List<List<TreeNode>> res,\n) {\n  if (root == null || root.val == 3) {\n    return;\n  }\n\n  // Attempt\n  path.add(root);\n  if (root.val == 7) {\n    // Record solution\n    res.add(List.from(path));\n  }\n  preOrder(root.left, path, res);\n  preOrder(root.right, path, res);\n  // Backtrack\n  path.removeLast();\n}\n
      preorder_traversal_iii_compact.rs
      /* Preorder traversal: Example 3 */\nfn pre_order(\n    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,\n    path: &mut Vec<Rc<RefCell<TreeNode>>>,\n    root: Option<&Rc<RefCell<TreeNode>>>,\n) {\n    // Pruning\n    if root.is_none() || root.as_ref().unwrap().borrow().val == 3 {\n        return;\n    }\n    if let Some(node) = root {\n        // Attempt\n        path.push(node.clone());\n        if node.borrow().val == 7 {\n            // Record solution\n            res.push(path.clone());\n        }\n        pre_order(res, path, node.borrow().left.as_ref());\n        pre_order(res, path, node.borrow().right.as_ref());\n        // Backtrack\n        path.pop();\n    }\n}\n
      preorder_traversal_iii_compact.c
      /* Preorder traversal: Example 3 */\nvoid preOrder(TreeNode *root) {\n    // Pruning\n    if (root == NULL || root->val == 3) {\n        return;\n    }\n    // Attempt\n    path[pathSize++] = root;\n    if (root->val == 7) {\n        // Record solution\n        for (int i = 0; i < pathSize; i++) {\n            res[resSize][i] = path[i];\n        }\n        resSize++;\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // Backtrack\n    pathSize--;\n}\n
      preorder_traversal_iii_compact.kt
      /* Preorder traversal: Example 3 */\nfun preOrder(root: TreeNode?) {\n    // Pruning\n    if (root == null || root._val == 3) {\n        return\n    }\n    // Attempt\n    path!!.add(root)\n    if (root._val == 7) {\n        // Record solution\n        res!!.add(path!!.toMutableList())\n    }\n    preOrder(root.left)\n    preOrder(root.right)\n    // Backtrack\n    path!!.removeAt(path!!.size - 1)\n}\n
      preorder_traversal_iii_compact.rb
      ### Pre-order traversal: example 3 ###\ndef pre_order(root)\n  # Pruning\n  return if !root || root.val == 3\n\n  # Attempt\n  $path.append(root)\n\n  # Record solution\n  $res << $path.dup if root.val == 7\n\n  pre_order(root.left)\n  pre_order(root.right)\n\n  # Backtrack\n  $path.pop\nend\n

      \"Pruning\" is a vivid term. As shown in the following figure, during the search process, we \"prune\" search branches that do not satisfy the constraints, avoiding many meaningless attempts and thus improving search efficiency.

      Figure 13-3 \u00a0 Pruning according to constraints

      "},{"location":"chapter_backtracking/backtracking_algorithm/#1313-framework-code","title":"13.1.3 \u00a0 Framework Code","text":"

      Next, we attempt to extract the main framework of backtracking's \"attempt, backtrack, and pruning\", to improve code generality.

      In the following framework code, state represents the current state of the problem, and choices represents the choices available in the current state:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      def backtrack(state: State, choices: list[choice], res: list[state]):\n    \"\"\"Backtracking algorithm framework\"\"\"\n    # Check if it is a solution\n    if is_solution(state):\n        # Record the solution\n        record_solution(state, res)\n        # Stop searching\n        return\n    # Traverse all choices\n    for choice in choices:\n        # Pruning: check if the choice is valid\n        if is_valid(state, choice):\n            # Attempt: make a choice and update the state\n            make_choice(state, choice)\n            backtrack(state, choices, res)\n            # Backtrack: undo the choice and restore to the previous state\n            undo_choice(state, choice)\n
      /* Backtracking algorithm framework */\nvoid backtrack(State *state, vector<Choice *> &choices, vector<State *> &res) {\n    // Check if it is a solution\n    if (isSolution(state)) {\n        // Record the solution\n        recordSolution(state, res);\n        // Stop searching\n        return;\n    }\n    // Traverse all choices\n    for (Choice choice : choices) {\n        // Pruning: check if the choice is valid\n        if (isValid(state, choice)) {\n            // Attempt: make a choice and update the state\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // Backtrack: undo the choice and restore to the previous state\n            undoChoice(state, choice);\n        }\n    }\n}\n
      /* Backtracking algorithm framework */\nvoid backtrack(State state, List<Choice> choices, List<State> res) {\n    // Check if it is a solution\n    if (isSolution(state)) {\n        // Record the solution\n        recordSolution(state, res);\n        // Stop searching\n        return;\n    }\n    // Traverse all choices\n    for (Choice choice : choices) {\n        // Pruning: check if the choice is valid\n        if (isValid(state, choice)) {\n            // Attempt: make a choice and update the state\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // Backtrack: undo the choice and restore to the previous state\n            undoChoice(state, choice);\n        }\n    }\n}\n
      /* Backtracking algorithm framework */\nvoid Backtrack(State state, List<Choice> choices, List<State> res) {\n    // Check if it is a solution\n    if (IsSolution(state)) {\n        // Record the solution\n        RecordSolution(state, res);\n        // Stop searching\n        return;\n    }\n    // Traverse all choices\n    foreach (Choice choice in choices) {\n        // Pruning: check if the choice is valid\n        if (IsValid(state, choice)) {\n            // Attempt: make a choice and update the state\n            MakeChoice(state, choice);\n            Backtrack(state, choices, res);\n            // Backtrack: undo the choice and restore to the previous state\n            UndoChoice(state, choice);\n        }\n    }\n}\n
      /* Backtracking algorithm framework */\nfunc backtrack(state *State, choices []Choice, res *[]State) {\n    // Check if it is a solution\n    if isSolution(state) {\n        // Record the solution\n        recordSolution(state, res)\n        // Stop searching\n        return\n    }\n    // Traverse all choices\n    for _, choice := range choices {\n        // Pruning: check if the choice is valid\n        if isValid(state, choice) {\n            // Attempt: make a choice and update the state\n            makeChoice(state, choice)\n            backtrack(state, choices, res)\n            // Backtrack: undo the choice and restore to the previous state\n            undoChoice(state, choice)\n        }\n    }\n}\n
      /* Backtracking algorithm framework */\nfunc backtrack(state: inout State, choices: [Choice], res: inout [State]) {\n    // Check if it is a solution\n    if isSolution(state: state) {\n        // Record the solution\n        recordSolution(state: state, res: &res)\n        // Stop searching\n        return\n    }\n    // Traverse all choices\n    for choice in choices {\n        // Pruning: check if the choice is valid\n        if isValid(state: state, choice: choice) {\n            // Attempt: make a choice and update the state\n            makeChoice(state: &state, choice: choice)\n            backtrack(state: &state, choices: choices, res: &res)\n            // Backtrack: undo the choice and restore to the previous state\n            undoChoice(state: &state, choice: choice)\n        }\n    }\n}\n
      /* Backtracking algorithm framework */\nfunction backtrack(state, choices, res) {\n    // Check if it is a solution\n    if (isSolution(state)) {\n        // Record the solution\n        recordSolution(state, res);\n        // Stop searching\n        return;\n    }\n    // Traverse all choices\n    for (let choice of choices) {\n        // Pruning: check if the choice is valid\n        if (isValid(state, choice)) {\n            // Attempt: make a choice and update the state\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // Backtrack: undo the choice and restore to the previous state\n            undoChoice(state, choice);\n        }\n    }\n}\n
      /* Backtracking algorithm framework */\nfunction backtrack(state: State, choices: Choice[], res: State[]): void {\n    // Check if it is a solution\n    if (isSolution(state)) {\n        // Record the solution\n        recordSolution(state, res);\n        // Stop searching\n        return;\n    }\n    // Traverse all choices\n    for (let choice of choices) {\n        // Pruning: check if the choice is valid\n        if (isValid(state, choice)) {\n            // Attempt: make a choice and update the state\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // Backtrack: undo the choice and restore to the previous state\n            undoChoice(state, choice);\n        }\n    }\n}\n
      /* Backtracking algorithm framework */\nvoid backtrack(State state, List<Choice>, List<State> res) {\n  // Check if it is a solution\n  if (isSolution(state)) {\n    // Record the solution\n    recordSolution(state, res);\n    // Stop searching\n    return;\n  }\n  // Traverse all choices\n  for (Choice choice in choices) {\n    // Pruning: check if the choice is valid\n    if (isValid(state, choice)) {\n      // Attempt: make a choice and update the state\n      makeChoice(state, choice);\n      backtrack(state, choices, res);\n      // Backtrack: undo the choice and restore to the previous state\n      undoChoice(state, choice);\n    }\n  }\n}\n
      /* Backtracking algorithm framework */\nfn backtrack(state: &mut State, choices: &Vec<Choice>, res: &mut Vec<State>) {\n    // Check if it is a solution\n    if is_solution(state) {\n        // Record the solution\n        record_solution(state, res);\n        // Stop searching\n        return;\n    }\n    // Traverse all choices\n    for choice in choices {\n        // Pruning: check if the choice is valid\n        if is_valid(state, choice) {\n            // Attempt: make a choice and update the state\n            make_choice(state, choice);\n            backtrack(state, choices, res);\n            // Backtrack: undo the choice and restore to the previous state\n            undo_choice(state, choice);\n        }\n    }\n}\n
      /* Backtracking algorithm framework */\nvoid backtrack(State *state, Choice *choices, int numChoices, State *res, int numRes) {\n    // Check if it is a solution\n    if (isSolution(state)) {\n        // Record the solution\n        recordSolution(state, res, numRes);\n        // Stop searching\n        return;\n    }\n    // Traverse all choices\n    for (int i = 0; i < numChoices; i++) {\n        // Pruning: check if the choice is valid\n        if (isValid(state, &choices[i])) {\n            // Attempt: make a choice and update the state\n            makeChoice(state, &choices[i]);\n            backtrack(state, choices, numChoices, res, numRes);\n            // Backtrack: undo the choice and restore to the previous state\n            undoChoice(state, &choices[i]);\n        }\n    }\n}\n
      /* Backtracking algorithm framework */\nfun backtrack(state: State?, choices: List<Choice?>, res: List<State?>?) {\n    // Check if it is a solution\n    if (isSolution(state)) {\n        // Record the solution\n        recordSolution(state, res)\n        // Stop searching\n        return\n    }\n    // Traverse all choices\n    for (choice in choices) {\n        // Pruning: check if the choice is valid\n        if (isValid(state, choice)) {\n            // Attempt: make a choice and update the state\n            makeChoice(state, choice)\n            backtrack(state, choices, res)\n            // Backtrack: undo the choice and restore to the previous state\n            undoChoice(state, choice)\n        }\n    }\n}\n
      ### Backtracking algorithm framework ###\ndef backtrack(state, choices, res)\n    # Check if it is a solution\n    if is_solution?(state)\n        # Record the solution\n        record_solution(state, res)\n        return\n    end\n\n    # Traverse all choices\n    for choice in choices\n        # Pruning: check if the choice is valid\n        if is_valid?(state, choice)\n            # Attempt: make a choice and update the state\n            make_choice(state, choice)\n            backtrack(state, choices, res)\n            # Backtrack: undo the choice and restore to the previous state\n            undo_choice(state, choice)\n        end\n    end\nend\n

      Next, we solve Example 3 based on the framework code. The state state is the node traversal path, the choices choices are the left and right child nodes of the current node, and the result res is a list of paths:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_iii_template.py
      def is_solution(state: list[TreeNode]) -> bool:\n    \"\"\"Check if the current state is a solution\"\"\"\n    return state and state[-1].val == 7\n\ndef record_solution(state: list[TreeNode], res: list[list[TreeNode]]):\n    \"\"\"Record solution\"\"\"\n    res.append(list(state))\n\ndef is_valid(state: list[TreeNode], choice: TreeNode) -> bool:\n    \"\"\"Check if the choice is valid under the current state\"\"\"\n    return choice is not None and choice.val != 3\n\ndef make_choice(state: list[TreeNode], choice: TreeNode):\n    \"\"\"Update state\"\"\"\n    state.append(choice)\n\ndef undo_choice(state: list[TreeNode], choice: TreeNode):\n    \"\"\"Restore state\"\"\"\n    state.pop()\n\ndef backtrack(\n    state: list[TreeNode], choices: list[TreeNode], res: list[list[TreeNode]]\n):\n    \"\"\"Backtracking algorithm: Example 3\"\"\"\n    # Check if it is a solution\n    if is_solution(state):\n        # Record solution\n        record_solution(state, res)\n    # Traverse all choices\n    for choice in choices:\n        # Pruning: check if the choice is valid\n        if is_valid(state, choice):\n            # Attempt: make choice, update state\n            make_choice(state, choice)\n            # Proceed to the next round of selection\n            backtrack(state, [choice.left, choice.right], res)\n            # Backtrack: undo choice, restore to previous state\n            undo_choice(state, choice)\n
      preorder_traversal_iii_template.cpp
      /* Check if the current state is a solution */\nbool isSolution(vector<TreeNode *> &state) {\n    return !state.empty() && state.back()->val == 7;\n}\n\n/* Record solution */\nvoid recordSolution(vector<TreeNode *> &state, vector<vector<TreeNode *>> &res) {\n    res.push_back(state);\n}\n\n/* Check if the choice is valid under the current state */\nbool isValid(vector<TreeNode *> &state, TreeNode *choice) {\n    return choice != nullptr && choice->val != 3;\n}\n\n/* Update state */\nvoid makeChoice(vector<TreeNode *> &state, TreeNode *choice) {\n    state.push_back(choice);\n}\n\n/* Restore state */\nvoid undoChoice(vector<TreeNode *> &state, TreeNode *choice) {\n    state.pop_back();\n}\n\n/* Backtracking algorithm: Example 3 */\nvoid backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<vector<TreeNode *>> &res) {\n    // Check if it is a solution\n    if (isSolution(state)) {\n        // Record solution\n        recordSolution(state, res);\n    }\n    // Traverse all choices\n    for (TreeNode *choice : choices) {\n        // Pruning: check if the choice is valid\n        if (isValid(state, choice)) {\n            // Attempt: make choice, update state\n            makeChoice(state, choice);\n            // Proceed to the next round of selection\n            vector<TreeNode *> nextChoices{choice->left, choice->right};\n            backtrack(state, nextChoices, res);\n            // Backtrack: undo choice, restore to previous state\n            undoChoice(state, choice);\n        }\n    }\n}\n
      preorder_traversal_iii_template.java
      /* Check if the current state is a solution */\nboolean isSolution(List<TreeNode> state) {\n    return !state.isEmpty() && state.get(state.size() - 1).val == 7;\n}\n\n/* Record solution */\nvoid recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n    res.add(new ArrayList<>(state));\n}\n\n/* Check if the choice is valid under the current state */\nboolean isValid(List<TreeNode> state, TreeNode choice) {\n    return choice != null && choice.val != 3;\n}\n\n/* Update state */\nvoid makeChoice(List<TreeNode> state, TreeNode choice) {\n    state.add(choice);\n}\n\n/* Restore state */\nvoid undoChoice(List<TreeNode> state, TreeNode choice) {\n    state.remove(state.size() - 1);\n}\n\n/* Backtracking algorithm: Example 3 */\nvoid backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {\n    // Check if it is a solution\n    if (isSolution(state)) {\n        // Record solution\n        recordSolution(state, res);\n    }\n    // Traverse all choices\n    for (TreeNode choice : choices) {\n        // Pruning: check if the choice is valid\n        if (isValid(state, choice)) {\n            // Attempt: make choice, update state\n            makeChoice(state, choice);\n            // Proceed to the next round of selection\n            backtrack(state, Arrays.asList(choice.left, choice.right), res);\n            // Backtrack: undo choice, restore to previous state\n            undoChoice(state, choice);\n        }\n    }\n}\n
      preorder_traversal_iii_template.cs
      /* Check if the current state is a solution */\nbool IsSolution(List<TreeNode> state) {\n    return state.Count != 0 && state[^1].val == 7;\n}\n\n/* Record solution */\nvoid RecordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n    res.Add(new List<TreeNode>(state));\n}\n\n/* Check if the choice is valid under the current state */\nbool IsValid(List<TreeNode> state, TreeNode choice) {\n    return choice != null && choice.val != 3;\n}\n\n/* Update state */\nvoid MakeChoice(List<TreeNode> state, TreeNode choice) {\n    state.Add(choice);\n}\n\n/* Restore state */\nvoid UndoChoice(List<TreeNode> state, TreeNode choice) {\n    state.RemoveAt(state.Count - 1);\n}\n\n/* Backtracking algorithm: Example 3 */\nvoid Backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {\n    // Check if it is a solution\n    if (IsSolution(state)) {\n        // Record solution\n        RecordSolution(state, res);\n    }\n    // Traverse all choices\n    foreach (TreeNode choice in choices) {\n        // Pruning: check if the choice is valid\n        if (IsValid(state, choice)) {\n            // Attempt: make choice, update state\n            MakeChoice(state, choice);\n            // Proceed to the next round of selection\n            Backtrack(state, [choice.left!, choice.right!], res);\n            // Backtrack: undo choice, restore to previous state\n            UndoChoice(state, choice);\n        }\n    }\n}\n
      preorder_traversal_iii_template.go
      /* Check if the current state is a solution */\nfunc isSolution(state *[]*TreeNode) bool {\n    return len(*state) != 0 && (*state)[len(*state)-1].Val == 7\n}\n\n/* Record solution */\nfunc recordSolution(state *[]*TreeNode, res *[][]*TreeNode) {\n    *res = append(*res, append([]*TreeNode{}, *state...))\n}\n\n/* Check if the choice is valid under the current state */\nfunc isValid(state *[]*TreeNode, choice *TreeNode) bool {\n    return choice != nil && choice.Val != 3\n}\n\n/* Update state */\nfunc makeChoice(state *[]*TreeNode, choice *TreeNode) {\n    *state = append(*state, choice)\n}\n\n/* Restore state */\nfunc undoChoice(state *[]*TreeNode, choice *TreeNode) {\n    *state = (*state)[:len(*state)-1]\n}\n\n/* Backtracking algorithm: Example 3 */\nfunc backtrackIII(state *[]*TreeNode, choices *[]*TreeNode, res *[][]*TreeNode) {\n    // Check if it is a solution\n    if isSolution(state) {\n        // Record solution\n        recordSolution(state, res)\n    }\n    // Traverse all choices\n    for _, choice := range *choices {\n        // Pruning: check if the choice is valid\n        if isValid(state, choice) {\n            // Attempt: make choice, update state\n            makeChoice(state, choice)\n            // Proceed to the next round of selection\n            temp := make([]*TreeNode, 0)\n            temp = append(temp, choice.Left, choice.Right)\n            backtrackIII(state, &temp, res)\n            // Backtrack: undo choice, restore to previous state\n            undoChoice(state, choice)\n        }\n    }\n}\n
      preorder_traversal_iii_template.swift
      /* Check if the current state is a solution */\nfunc isSolution(state: [TreeNode]) -> Bool {\n    !state.isEmpty && state.last!.val == 7\n}\n\n/* Record solution */\nfunc recordSolution(state: [TreeNode], res: inout [[TreeNode]]) {\n    res.append(state)\n}\n\n/* Check if the choice is valid under the current state */\nfunc isValid(state: [TreeNode], choice: TreeNode?) -> Bool {\n    choice != nil && choice!.val != 3\n}\n\n/* Update state */\nfunc makeChoice(state: inout [TreeNode], choice: TreeNode) {\n    state.append(choice)\n}\n\n/* Restore state */\nfunc undoChoice(state: inout [TreeNode], choice: TreeNode) {\n    state.removeLast()\n}\n\n/* Backtracking algorithm: Example 3 */\nfunc backtrack(state: inout [TreeNode], choices: [TreeNode], res: inout [[TreeNode]]) {\n    // Check if it is a solution\n    if isSolution(state: state) {\n        recordSolution(state: state, res: &res)\n    }\n    // Traverse all choices\n    for choice in choices {\n        // Pruning: check if the choice is valid\n        if isValid(state: state, choice: choice) {\n            // Attempt: make choice, update state\n            makeChoice(state: &state, choice: choice)\n            // Proceed to the next round of selection\n            backtrack(state: &state, choices: [choice.left, choice.right].compactMap { $0 }, res: &res)\n            // Backtrack: undo choice, restore to previous state\n            undoChoice(state: &state, choice: choice)\n        }\n    }\n}\n
      preorder_traversal_iii_template.js
      /* Check if the current state is a solution */\nfunction isSolution(state) {\n    return state && state[state.length - 1]?.val === 7;\n}\n\n/* Record solution */\nfunction recordSolution(state, res) {\n    res.push([...state]);\n}\n\n/* Check if the choice is valid under the current state */\nfunction isValid(state, choice) {\n    return choice !== null && choice.val !== 3;\n}\n\n/* Update state */\nfunction makeChoice(state, choice) {\n    state.push(choice);\n}\n\n/* Restore state */\nfunction undoChoice(state) {\n    state.pop();\n}\n\n/* Backtracking algorithm: Example 3 */\nfunction backtrack(state, choices, res) {\n    // Check if it is a solution\n    if (isSolution(state)) {\n        // Record solution\n        recordSolution(state, res);\n    }\n    // Traverse all choices\n    for (const choice of choices) {\n        // Pruning: check if the choice is valid\n        if (isValid(state, choice)) {\n            // Attempt: make choice, update state\n            makeChoice(state, choice);\n            // Proceed to the next round of selection\n            backtrack(state, [choice.left, choice.right], res);\n            // Backtrack: undo choice, restore to previous state\n            undoChoice(state);\n        }\n    }\n}\n
      preorder_traversal_iii_template.ts
      /* Check if the current state is a solution */\nfunction isSolution(state: TreeNode[]): boolean {\n    return state && state[state.length - 1]?.val === 7;\n}\n\n/* Record solution */\nfunction recordSolution(state: TreeNode[], res: TreeNode[][]): void {\n    res.push([...state]);\n}\n\n/* Check if the choice is valid under the current state */\nfunction isValid(state: TreeNode[], choice: TreeNode): boolean {\n    return choice !== null && choice.val !== 3;\n}\n\n/* Update state */\nfunction makeChoice(state: TreeNode[], choice: TreeNode): void {\n    state.push(choice);\n}\n\n/* Restore state */\nfunction undoChoice(state: TreeNode[]): void {\n    state.pop();\n}\n\n/* Backtracking algorithm: Example 3 */\nfunction backtrack(\n    state: TreeNode[],\n    choices: TreeNode[],\n    res: TreeNode[][]\n): void {\n    // Check if it is a solution\n    if (isSolution(state)) {\n        // Record solution\n        recordSolution(state, res);\n    }\n    // Traverse all choices\n    for (const choice of choices) {\n        // Pruning: check if the choice is valid\n        if (isValid(state, choice)) {\n            // Attempt: make choice, update state\n            makeChoice(state, choice);\n            // Proceed to the next round of selection\n            backtrack(state, [choice.left, choice.right], res);\n            // Backtrack: undo choice, restore to previous state\n            undoChoice(state);\n        }\n    }\n}\n
      preorder_traversal_iii_template.dart
      /* Check if the current state is a solution */\nbool isSolution(List<TreeNode> state) {\n  return state.isNotEmpty && state.last.val == 7;\n}\n\n/* Record solution */\nvoid recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n  res.add(List.from(state));\n}\n\n/* Check if the choice is valid under the current state */\nbool isValid(List<TreeNode> state, TreeNode? choice) {\n  return choice != null && choice.val != 3;\n}\n\n/* Update state */\nvoid makeChoice(List<TreeNode> state, TreeNode? choice) {\n  state.add(choice!);\n}\n\n/* Restore state */\nvoid undoChoice(List<TreeNode> state, TreeNode? choice) {\n  state.removeLast();\n}\n\n/* Backtracking algorithm: Example 3 */\nvoid backtrack(\n  List<TreeNode> state,\n  List<TreeNode?> choices,\n  List<List<TreeNode>> res,\n) {\n  // Check if it is a solution\n  if (isSolution(state)) {\n    // Record solution\n    recordSolution(state, res);\n  }\n  // Traverse all choices\n  for (TreeNode? choice in choices) {\n    // Pruning: check if the choice is valid\n    if (isValid(state, choice)) {\n      // Attempt: make choice, update state\n      makeChoice(state, choice);\n      // Proceed to the next round of selection\n      backtrack(state, [choice!.left, choice.right], res);\n      // Backtrack: undo choice, restore to previous state\n      undoChoice(state, choice);\n    }\n  }\n}\n
      preorder_traversal_iii_template.rs
      /* Check if the current state is a solution */\nfn is_solution(state: &mut Vec<Rc<RefCell<TreeNode>>>) -> bool {\n    return !state.is_empty() && state.last().unwrap().borrow().val == 7;\n}\n\n/* Record solution */\nfn record_solution(\n    state: &mut Vec<Rc<RefCell<TreeNode>>>,\n    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,\n) {\n    res.push(state.clone());\n}\n\n/* Check if the choice is valid under the current state */\nfn is_valid(_: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Option<&Rc<RefCell<TreeNode>>>) -> bool {\n    return choice.is_some() && choice.unwrap().borrow().val != 3;\n}\n\n/* Update state */\nfn make_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Rc<RefCell<TreeNode>>) {\n    state.push(choice);\n}\n\n/* Restore state */\nfn undo_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, _: Rc<RefCell<TreeNode>>) {\n    state.pop();\n}\n\n/* Backtracking algorithm: Example 3 */\nfn backtrack(\n    state: &mut Vec<Rc<RefCell<TreeNode>>>,\n    choices: &Vec<Option<&Rc<RefCell<TreeNode>>>>,\n    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,\n) {\n    // Check if it is a solution\n    if is_solution(state) {\n        // Record solution\n        record_solution(state, res);\n    }\n    // Traverse all choices\n    for &choice in choices.iter() {\n        // Pruning: check if the choice is valid\n        if is_valid(state, choice) {\n            // Attempt: make choice, update state\n            make_choice(state, choice.unwrap().clone());\n            // Proceed to the next round of selection\n            backtrack(\n                state,\n                &vec![\n                    choice.unwrap().borrow().left.as_ref(),\n                    choice.unwrap().borrow().right.as_ref(),\n                ],\n                res,\n            );\n            // Backtrack: undo choice, restore to previous state\n            undo_choice(state, choice.unwrap().clone());\n        }\n    }\n}\n
      preorder_traversal_iii_template.c
      /* Check if the current state is a solution */\nbool isSolution(void) {\n    return pathSize > 0 && path[pathSize - 1]->val == 7;\n}\n\n/* Record solution */\nvoid recordSolution(void) {\n    for (int i = 0; i < pathSize; i++) {\n        res[resSize][i] = path[i];\n    }\n    resSize++;\n}\n\n/* Check if the choice is valid under the current state */\nbool isValid(TreeNode *choice) {\n    return choice != NULL && choice->val != 3;\n}\n\n/* Update state */\nvoid makeChoice(TreeNode *choice) {\n    path[pathSize++] = choice;\n}\n\n/* Restore state */\nvoid undoChoice(void) {\n    pathSize--;\n}\n\n/* Backtracking algorithm: Example 3 */\nvoid backtrack(TreeNode *choices[2]) {\n    // Check if it is a solution\n    if (isSolution()) {\n        // Record solution\n        recordSolution();\n    }\n    // Traverse all choices\n    for (int i = 0; i < 2; i++) {\n        TreeNode *choice = choices[i];\n        // Pruning: check if the choice is valid\n        if (isValid(choice)) {\n            // Attempt: make choice, update state\n            makeChoice(choice);\n            // Proceed to the next round of selection\n            TreeNode *nextChoices[2] = {choice->left, choice->right};\n            backtrack(nextChoices);\n            // Backtrack: undo choice, restore to previous state\n            undoChoice();\n        }\n    }\n}\n
      preorder_traversal_iii_template.kt
      /* Check if the current state is a solution */\nfun isSolution(state: MutableList<TreeNode?>): Boolean {\n    return state.isNotEmpty() && state[state.size - 1]?._val == 7\n}\n\n/* Record solution */\nfun recordSolution(state: MutableList<TreeNode?>?, res: MutableList<MutableList<TreeNode?>?>) {\n    res.add(state!!.toMutableList())\n}\n\n/* Check if the choice is valid under the current state */\nfun isValid(state: MutableList<TreeNode?>?, choice: TreeNode?): Boolean {\n    return choice != null && choice._val != 3\n}\n\n/* Update state */\nfun makeChoice(state: MutableList<TreeNode?>, choice: TreeNode?) {\n    state.add(choice)\n}\n\n/* Restore state */\nfun undoChoice(state: MutableList<TreeNode?>, choice: TreeNode?) {\n    state.removeLast()\n}\n\n/* Backtracking algorithm: Example 3 */\nfun backtrack(\n    state: MutableList<TreeNode?>,\n    choices: MutableList<TreeNode?>,\n    res: MutableList<MutableList<TreeNode?>?>\n) {\n    // Check if it is a solution\n    if (isSolution(state)) {\n        // Record solution\n        recordSolution(state, res)\n    }\n    // Traverse all choices\n    for (choice in choices) {\n        // Pruning: check if the choice is valid\n        if (isValid(state, choice)) {\n            // Attempt: make choice, update state\n            makeChoice(state, choice)\n            // Proceed to the next round of selection\n            backtrack(state, mutableListOf(choice!!.left, choice.right), res)\n            // Backtrack: undo choice, restore to previous state\n            undoChoice(state, choice)\n        }\n    }\n}\n
      preorder_traversal_iii_template.rb
      ### Check if current state is solution ###\ndef is_solution?(state)\n  !state.empty? && state.last.val == 7\nend\n\n### Record solution ###\ndef record_solution(state, res)\n  res << state.dup\nend\n\n### Check if choice is valid in current state ###\ndef is_valid?(state, choice)\n  choice && choice.val != 3\nend\n\n### Update state ###\ndef make_choice(state, choice)\n  state << choice\nend\n\n### Restore state ###\ndef undo_choice(state, choice)\n  state.pop\nend\n\n### Backtracking: example 3 ###\ndef backtrack(state, choices, res)\n  # Check if it is a solution\n  record_solution(state, res) if is_solution?(state)\n\n  # Traverse all choices\n  for choice in choices\n    # Pruning: check if the choice is valid\n    if is_valid?(state, choice)\n      # Attempt: make choice, update state\n      make_choice(state, choice)\n      # Proceed to the next round of selection\n      backtrack(state, [choice.left, choice.right], res)\n      # Backtrack: undo choice, restore to previous state\n      undo_choice(state, choice)\n    end\n  end\nend\n

      As per the problem statement, we should continue searching after finding a node with value \\(7\\). Therefore, we need to remove the return statement after recording the solution. The following figure compares the search process with and without the return statement.

      Figure 13-4 \u00a0 Comparison of search process with and without return statement

      Compared to code based on preorder traversal, code based on the backtracking algorithm framework appears more verbose, but has better generality. In fact, many backtracking problems can be solved within this framework. We only need to define state and choices for the specific problem and implement each method in the framework.

      "},{"location":"chapter_backtracking/backtracking_algorithm/#1314-common-terminology","title":"13.1.4 \u00a0 Common Terminology","text":"

      To analyze algorithmic problems more clearly, we summarize the meanings of common terminology used in backtracking algorithms and provide corresponding examples from Example 3, as shown in the following table.

      Table 13-1 \u00a0 Common Backtracking Algorithm Terminology

      Term Definition Example 3 Solution (solution) A solution is an answer that satisfies the specific conditions of a problem; there may be one or more solutions All paths from root to nodes with value \\(7\\) that satisfy the constraint Constraint (constraint) A constraint is a condition in the problem that limits the feasibility of solutions, typically used for pruning Paths do not contain nodes with value \\(3\\) State (state) State represents the situation of a problem at a certain moment, including the choices already made The currently visited node path, i.e., the path list of nodes Attempt (attempt) An attempt is the process of exploring the solution space according to available choices, including making choices, updating state, and checking if it is a solution Recursively visit left (right) child nodes, add nodes to path, check if node value is \\(7\\) Backtrack (backtracking) Backtracking refers to undoing previous choices and returning to a previous state when encountering a state that does not satisfy constraints Stop searching when passing over leaf nodes, ending node visits, or encountering nodes with value \\(3\\); function returns Pruning (pruning) Pruning is a method of avoiding meaningless search paths according to problem characteristics and constraints, which can improve search efficiency When encountering a node with value \\(3\\), do not continue searching

      Tip

      The concepts of problem, solution, state, etc. are universal and are involved in divide-and-conquer, backtracking, dynamic programming, greedy and other algorithms.

      "},{"location":"chapter_backtracking/backtracking_algorithm/#1315-advantages-and-limitations","title":"13.1.5 \u00a0 Advantages and Limitations","text":"

      The backtracking algorithm is essentially a depth-first search algorithm that tries all possible solutions until it finds one that satisfies the conditions. The advantage of this approach is that it can find all possible solutions, and with reasonable pruning operations, it achieves high efficiency.

      However, when dealing with large-scale or complex problems, the running efficiency of the backtracking algorithm may be unacceptable.

      • Time: The backtracking algorithm usually needs to traverse all possibilities in the solution space, and the time complexity can reach exponential or factorial order.
      • Space: During recursive calls, the current state needs to be saved (such as paths, auxiliary variables used for pruning, etc.), and when the depth is large, the space requirement can become very large.

      Nevertheless, the backtracking algorithm is still the best solution for certain search problems and constraint satisfaction problems. For these problems, since we cannot predict which choices will generate valid solutions, we must traverse all possible choices. In this case, the key is how to optimize efficiency. There are two common efficiency optimization methods.

      • Pruning: Avoid searching paths that are guaranteed not to produce solutions, thereby saving time and space.
      • Heuristic search: Introduce certain strategies or estimation values during the search process to prioritize searching paths that are most likely to produce valid solutions.
      "},{"location":"chapter_backtracking/backtracking_algorithm/#1316-typical-backtracking-examples","title":"13.1.6 \u00a0 Typical Backtracking Examples","text":"

      The backtracking algorithm can be used to solve many search problems, constraint satisfaction problems, and combinatorial optimization problems.

      Search problems: The goal of these problems is to find solutions that satisfy specific conditions.

      • Permutation problem: Given a set, find all possible permutations and combinations.
      • Subset sum problem: Given a set and a target sum, find all subsets in the set whose elements sum to the target.
      • Tower of Hanoi: Given three pegs and a series of disks of different sizes, move all disks from one peg to another, moving only one disk at a time, and never placing a larger disk on a smaller disk.

      Constraint satisfaction problems: The goal of these problems is to find solutions that satisfy all constraints.

      • N-Queens: Place \\(n\\) queens on an \\(n \\times n\\) chessboard such that they do not attack each other.
      • Sudoku: Fill numbers \\(1\\) to \\(9\\) in a \\(9 \\times 9\\) grid such that each row, column, and \\(3 \\times 3\\) subgrid contains no repeated digits.
      • Graph coloring: Given an undirected graph, color each vertex with the minimum number of colors such that adjacent vertices have different colors.

      Combinatorial optimization problems: The goal of these problems is to find an optimal solution that satisfies certain conditions in a combinatorial space.

      • 0-1 Knapsack: Given a set of items and a knapsack, each item has a value and weight. Under the knapsack capacity constraint, select items to maximize total value.
      • Traveling Salesman Problem: Starting from a point in a graph, visit all other points exactly once and return to the starting point, finding the shortest path.
      • Maximum Clique: Given an undirected graph, find the largest complete subgraph, i.e., a subgraph where any two vertices are connected by an edge.

      Note that for many combinatorial optimization problems, backtracking is not the optimal solution.

      • The 0-1 Knapsack problem is usually solved using dynamic programming to achieve higher time efficiency.
      • The Traveling Salesman Problem is a famous NP-Hard problem; common solutions include genetic algorithms and ant colony algorithms.
      • The Maximum Clique problem is a classical problem in graph theory and can be solved using heuristic algorithms such as greedy algorithms.
      "},{"location":"chapter_backtracking/n_queens_problem/","title":"13.4 \u00a0 N-Queens Problem","text":"

      Question

      According to the rules of chess, a queen can attack pieces that share the same row, column, or diagonal line. Given \\(n\\) queens and an \\(n \\times n\\) chessboard, find a placement scheme such that no two queens can attack each other.

      As shown in Figure 13-15, when \\(n = 4\\), there are two solutions that can be found. From the perspective of the backtracking algorithm, an \\(n \\times n\\) chessboard has \\(n^2\\) squares, which provide all the choices choices. During the process of placing queens one by one, the chessboard state changes continuously, and the chessboard at each moment represents the state state.

      Figure 13-15 \u00a0 Solution to the 4-queens problem

      Figure 13-16 illustrates the three constraints of this problem: multiple queens cannot be in the same row, the same column, or on the same diagonal. It is worth noting that diagonals are divided into two types: the main diagonal \\ and the anti-diagonal /.

      Figure 13-16 \u00a0 Constraints of the n-queens problem

      "},{"location":"chapter_backtracking/n_queens_problem/#1-row-by-row-placement-strategy","title":"1. \u00a0 Row-By-Row Placement Strategy","text":"

      Since both the number of queens and the number of rows on the chessboard are \\(n\\), we can easily derive a conclusion: each row of the chessboard allows and only allows exactly one queen to be placed.

      This means we can adopt a row-by-row placement strategy: starting from the first row, place one queen in each row until the last row is completed.

      Figure 13-17 shows the row-by-row placement process for the 4-queens problem. Due to space limitations, the figure only expands one search branch of the first row, and all schemes that do not satisfy the column constraint and diagonal constraints are pruned.

      Figure 13-17 \u00a0 Row-by-row placement strategy

      Essentially, the row-by-row placement strategy serves a pruning function, as it avoids all search branches where multiple queens appear in the same row.

      "},{"location":"chapter_backtracking/n_queens_problem/#2-column-and-diagonal-pruning","title":"2. \u00a0 Column and Diagonal Pruning","text":"

      To satisfy the column constraint, we can use a boolean array cols of length \\(n\\) to record whether each column has a queen. Before each placement decision, we use cols to prune columns that already have queens, and dynamically update the state of cols during backtracking.

      Tip

      Please note that the origin of the matrix is located in the upper-left corner, where the row index increases from top to bottom, and the column index increases from left to right.

      So how do we handle diagonal constraints? Consider a square on the chessboard with row and column indices \\((row, col)\\). If we select a specific main diagonal in the matrix, we find that all squares on that diagonal have the same difference between their row and column indices, meaning that \\(row - col\\) is a constant value for all squares on the main diagonal.

      In other words, if two squares satisfy \\(row_1 - col_1 = row_2 - col_2\\), they must be on the same main diagonal. Using this pattern, we can use the array diags1 shown in Figure 13-18 to record whether there is a queen on each main diagonal.

      Similarly, for all squares on an anti-diagonal, the sum \\(row + col\\) is a constant value. We can likewise use the array diags2 to handle anti-diagonal constraints.

      Figure 13-18 \u00a0 Handling column and diagonal constraints

      "},{"location":"chapter_backtracking/n_queens_problem/#3-code-implementation","title":"3. \u00a0 Code Implementation","text":"

      Please note that in an \\(n\\)-dimensional square matrix, the range of \\(row - col\\) is \\([-n + 1, n - 1]\\), and the range of \\(row + col\\) is \\([0, 2n - 2]\\). Therefore, the number of both main diagonals and anti-diagonals is \\(2n - 1\\), meaning the length of both arrays diags1 and diags2 is \\(2n - 1\\).

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby n_queens.py
      def backtrack(\n    row: int,\n    n: int,\n    state: list[list[str]],\n    res: list[list[list[str]]],\n    cols: list[bool],\n    diags1: list[bool],\n    diags2: list[bool],\n):\n    \"\"\"Backtracking algorithm: N queens\"\"\"\n    # When all rows are placed, record the solution\n    if row == n:\n        res.append([list(row) for row in state])\n        return\n    # Traverse all columns\n    for col in range(n):\n        # Calculate the main diagonal and anti-diagonal corresponding to this cell\n        diag1 = row - col + n - 1\n        diag2 = row + col\n        # Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n        if not cols[col] and not diags1[diag1] and not diags2[diag2]:\n            # Attempt: place the queen in this cell\n            state[row][col] = \"Q\"\n            cols[col] = diags1[diag1] = diags2[diag2] = True\n            # Place the next row\n            backtrack(row + 1, n, state, res, cols, diags1, diags2)\n            # Backtrack: restore this cell to an empty cell\n            state[row][col] = \"#\"\n            cols[col] = diags1[diag1] = diags2[diag2] = False\n\ndef n_queens(n: int) -> list[list[list[str]]]:\n    \"\"\"Solve N queens\"\"\"\n    # Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n    state = [[\"#\" for _ in range(n)] for _ in range(n)]\n    cols = [False] * n  # Record whether there is a queen in the column\n    diags1 = [False] * (2 * n - 1)  # Record whether there is a queen on the main diagonal\n    diags2 = [False] * (2 * n - 1)  # Record whether there is a queen on the anti-diagonal\n    res = []\n    backtrack(0, n, state, res, cols, diags1, diags2)\n\n    return res\n
      n_queens.cpp
      /* Backtracking algorithm: N queens */\nvoid backtrack(int row, int n, vector<vector<string>> &state, vector<vector<vector<string>>> &res, vector<bool> &cols,\n               vector<bool> &diags1, vector<bool> &diags2) {\n    // When all rows are placed, record the solution\n    if (row == n) {\n        res.push_back(state);\n        return;\n    }\n    // Traverse all columns\n    for (int col = 0; col < n; col++) {\n        // Calculate the main diagonal and anti-diagonal corresponding to this cell\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // Attempt: place the queen in this cell\n            state[row][col] = \"Q\";\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // Place the next row\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // Backtrack: restore this cell to an empty cell\n            state[row][col] = \"#\";\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* Solve N queens */\nvector<vector<vector<string>>> nQueens(int n) {\n    // Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n    vector<vector<string>> state(n, vector<string>(n, \"#\"));\n    vector<bool> cols(n, false);           // Record whether there is a queen in the column\n    vector<bool> diags1(2 * n - 1, false); // Record whether there is a queen on the main diagonal\n    vector<bool> diags2(2 * n - 1, false); // Record whether there is a queen on the anti-diagonal\n    vector<vector<vector<string>>> res;\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n\n    return res;\n}\n
      n_queens.java
      /* Backtracking algorithm: N queens */\nvoid backtrack(int row, int n, List<List<String>> state, List<List<List<String>>> res,\n        boolean[] cols, boolean[] diags1, boolean[] diags2) {\n    // When all rows are placed, record the solution\n    if (row == n) {\n        List<List<String>> copyState = new ArrayList<>();\n        for (List<String> sRow : state) {\n            copyState.add(new ArrayList<>(sRow));\n        }\n        res.add(copyState);\n        return;\n    }\n    // Traverse all columns\n    for (int col = 0; col < n; col++) {\n        // Calculate the main diagonal and anti-diagonal corresponding to this cell\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // Attempt: place the queen in this cell\n            state.get(row).set(col, \"Q\");\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // Place the next row\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // Backtrack: restore this cell to an empty cell\n            state.get(row).set(col, \"#\");\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* Solve N queens */\nList<List<List<String>>> nQueens(int n) {\n    // Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n    List<List<String>> state = new ArrayList<>();\n    for (int i = 0; i < n; i++) {\n        List<String> row = new ArrayList<>();\n        for (int j = 0; j < n; j++) {\n            row.add(\"#\");\n        }\n        state.add(row);\n    }\n    boolean[] cols = new boolean[n]; // Record whether there is a queen in the column\n    boolean[] diags1 = new boolean[2 * n - 1]; // Record whether there is a queen on the main diagonal\n    boolean[] diags2 = new boolean[2 * n - 1]; // Record whether there is a queen on the anti-diagonal\n    List<List<List<String>>> res = new ArrayList<>();\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n\n    return res;\n}\n
      n_queens.cs
      /* Backtracking algorithm: N queens */\nvoid Backtrack(int row, int n, List<List<string>> state, List<List<List<string>>> res,\n        bool[] cols, bool[] diags1, bool[] diags2) {\n    // When all rows are placed, record the solution\n    if (row == n) {\n        List<List<string>> copyState = [];\n        foreach (List<string> sRow in state) {\n            copyState.Add(new List<string>(sRow));\n        }\n        res.Add(copyState);\n        return;\n    }\n    // Traverse all columns\n    for (int col = 0; col < n; col++) {\n        // Calculate the main diagonal and anti-diagonal corresponding to this cell\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // Attempt: place the queen in this cell\n            state[row][col] = \"Q\";\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // Place the next row\n            Backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // Backtrack: restore this cell to an empty cell\n            state[row][col] = \"#\";\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* Solve N queens */\nList<List<List<string>>> NQueens(int n) {\n    // Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n    List<List<string>> state = [];\n    for (int i = 0; i < n; i++) {\n        List<string> row = [];\n        for (int j = 0; j < n; j++) {\n            row.Add(\"#\");\n        }\n        state.Add(row);\n    }\n    bool[] cols = new bool[n]; // Record whether there is a queen in the column\n    bool[] diags1 = new bool[2 * n - 1]; // Record whether there is a queen on the main diagonal\n    bool[] diags2 = new bool[2 * n - 1]; // Record whether there is a queen on the anti-diagonal\n    List<List<List<string>>> res = [];\n\n    Backtrack(0, n, state, res, cols, diags1, diags2);\n\n    return res;\n}\n
      n_queens.go
      /* Backtracking algorithm: N queens */\nfunc backtrack(row, n int, state *[][]string, res *[][][]string, cols, diags1, diags2 *[]bool) {\n    // When all rows are placed, record the solution\n    if row == n {\n        newState := make([][]string, len(*state))\n        for i, _ := range newState {\n            newState[i] = make([]string, len((*state)[0]))\n            copy(newState[i], (*state)[i])\n\n        }\n        *res = append(*res, newState)\n        return\n    }\n    // Traverse all columns\n    for col := 0; col < n; col++ {\n        // Calculate the main diagonal and anti-diagonal corresponding to this cell\n        diag1 := row - col + n - 1\n        diag2 := row + col\n        // Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n        if !(*cols)[col] && !(*diags1)[diag1] && !(*diags2)[diag2] {\n            // Attempt: place the queen in this cell\n            (*state)[row][col] = \"Q\"\n            (*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = true, true, true\n            // Place the next row\n            backtrack(row+1, n, state, res, cols, diags1, diags2)\n            // Backtrack: restore this cell to an empty cell\n            (*state)[row][col] = \"#\"\n            (*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = false, false, false\n        }\n    }\n}\n\n/* Solve N queens */\nfunc nQueens(n int) [][][]string {\n    // Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n    state := make([][]string, n)\n    for i := 0; i < n; i++ {\n        row := make([]string, n)\n        for i := 0; i < n; i++ {\n            row[i] = \"#\"\n        }\n        state[i] = row\n    }\n    // Record whether there is a queen in the column\n    cols := make([]bool, n)\n    diags1 := make([]bool, 2*n-1)\n    diags2 := make([]bool, 2*n-1)\n    res := make([][][]string, 0)\n    backtrack(0, n, &state, &res, &cols, &diags1, &diags2)\n    return res\n}\n
      n_queens.swift
      /* Backtracking algorithm: N queens */\nfunc backtrack(row: Int, n: Int, state: inout [[String]], res: inout [[[String]]], cols: inout [Bool], diags1: inout [Bool], diags2: inout [Bool]) {\n    // When all rows are placed, record the solution\n    if row == n {\n        res.append(state)\n        return\n    }\n    // Traverse all columns\n    for col in 0 ..< n {\n        // Calculate the main diagonal and anti-diagonal corresponding to this cell\n        let diag1 = row - col + n - 1\n        let diag2 = row + col\n        // Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n        if !cols[col] && !diags1[diag1] && !diags2[diag2] {\n            // Attempt: place the queen in this cell\n            state[row][col] = \"Q\"\n            cols[col] = true\n            diags1[diag1] = true\n            diags2[diag2] = true\n            // Place the next row\n            backtrack(row: row + 1, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)\n            // Backtrack: restore this cell to an empty cell\n            state[row][col] = \"#\"\n            cols[col] = false\n            diags1[diag1] = false\n            diags2[diag2] = false\n        }\n    }\n}\n\n/* Solve N queens */\nfunc nQueens(n: Int) -> [[[String]]] {\n    // Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n    var state = Array(repeating: Array(repeating: \"#\", count: n), count: n)\n    var cols = Array(repeating: false, count: n) // Record whether there is a queen in the column\n    var diags1 = Array(repeating: false, count: 2 * n - 1) // Record whether there is a queen on the main diagonal\n    var diags2 = Array(repeating: false, count: 2 * n - 1) // Record whether there is a queen on the anti-diagonal\n    var res: [[[String]]] = []\n\n    backtrack(row: 0, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)\n\n    return res\n}\n
      n_queens.js
      /* Backtracking algorithm: N queens */\nfunction backtrack(row, n, state, res, cols, diags1, diags2) {\n    // When all rows are placed, record the solution\n    if (row === n) {\n        res.push(state.map((row) => row.slice()));\n        return;\n    }\n    // Traverse all columns\n    for (let col = 0; col < n; col++) {\n        // Calculate the main diagonal and anti-diagonal corresponding to this cell\n        const diag1 = row - col + n - 1;\n        const diag2 = row + col;\n        // Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // Attempt: place the queen in this cell\n            state[row][col] = 'Q';\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // Place the next row\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // Backtrack: restore this cell to an empty cell\n            state[row][col] = '#';\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* Solve N queens */\nfunction nQueens(n) {\n    // Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n    const state = Array.from({ length: n }, () => Array(n).fill('#'));\n    const cols = Array(n).fill(false); // Record whether there is a queen in the column\n    const diags1 = Array(2 * n - 1).fill(false); // Record whether there is a queen on the main diagonal\n    const diags2 = Array(2 * n - 1).fill(false); // Record whether there is a queen on the anti-diagonal\n    const res = [];\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n    return res;\n}\n
      n_queens.ts
      /* Backtracking algorithm: N queens */\nfunction backtrack(\n    row: number,\n    n: number,\n    state: string[][],\n    res: string[][][],\n    cols: boolean[],\n    diags1: boolean[],\n    diags2: boolean[]\n): void {\n    // When all rows are placed, record the solution\n    if (row === n) {\n        res.push(state.map((row) => row.slice()));\n        return;\n    }\n    // Traverse all columns\n    for (let col = 0; col < n; col++) {\n        // Calculate the main diagonal and anti-diagonal corresponding to this cell\n        const diag1 = row - col + n - 1;\n        const diag2 = row + col;\n        // Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // Attempt: place the queen in this cell\n            state[row][col] = 'Q';\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // Place the next row\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // Backtrack: restore this cell to an empty cell\n            state[row][col] = '#';\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* Solve N queens */\nfunction nQueens(n: number): string[][][] {\n    // Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n    const state = Array.from({ length: n }, () => Array(n).fill('#'));\n    const cols = Array(n).fill(false); // Record whether there is a queen in the column\n    const diags1 = Array(2 * n - 1).fill(false); // Record whether there is a queen on the main diagonal\n    const diags2 = Array(2 * n - 1).fill(false); // Record whether there is a queen on the anti-diagonal\n    const res: string[][][] = [];\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n    return res;\n}\n
      n_queens.dart
      /* Backtracking algorithm: N queens */\nvoid backtrack(\n  int row,\n  int n,\n  List<List<String>> state,\n  List<List<List<String>>> res,\n  List<bool> cols,\n  List<bool> diags1,\n  List<bool> diags2,\n) {\n  // When all rows are placed, record the solution\n  if (row == n) {\n    List<List<String>> copyState = [];\n    for (List<String> sRow in state) {\n      copyState.add(List.from(sRow));\n    }\n    res.add(copyState);\n    return;\n  }\n  // Traverse all columns\n  for (int col = 0; col < n; col++) {\n    // Calculate the main diagonal and anti-diagonal corresponding to this cell\n    int diag1 = row - col + n - 1;\n    int diag2 = row + col;\n    // Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n    if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n      // Attempt: place the queen in this cell\n      state[row][col] = \"Q\";\n      cols[col] = true;\n      diags1[diag1] = true;\n      diags2[diag2] = true;\n      // Place the next row\n      backtrack(row + 1, n, state, res, cols, diags1, diags2);\n      // Backtrack: restore this cell to an empty cell\n      state[row][col] = \"#\";\n      cols[col] = false;\n      diags1[diag1] = false;\n      diags2[diag2] = false;\n    }\n  }\n}\n\n/* Solve N queens */\nList<List<List<String>>> nQueens(int n) {\n  // Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n  List<List<String>> state = List.generate(n, (index) => List.filled(n, \"#\"));\n  List<bool> cols = List.filled(n, false); // Record whether there is a queen in the column\n  List<bool> diags1 = List.filled(2 * n - 1, false); // Record whether there is a queen on the main diagonal\n  List<bool> diags2 = List.filled(2 * n - 1, false); // Record whether there is a queen on the anti-diagonal\n  List<List<List<String>>> res = [];\n\n  backtrack(0, n, state, res, cols, diags1, diags2);\n\n  return res;\n}\n
      n_queens.rs
      /* Backtracking algorithm: N queens */\nfn backtrack(\n    row: usize,\n    n: usize,\n    state: &mut Vec<Vec<String>>,\n    res: &mut Vec<Vec<Vec<String>>>,\n    cols: &mut [bool],\n    diags1: &mut [bool],\n    diags2: &mut [bool],\n) {\n    // When all rows are placed, record the solution\n    if row == n {\n        res.push(state.clone());\n        return;\n    }\n    // Traverse all columns\n    for col in 0..n {\n        // Calculate the main diagonal and anti-diagonal corresponding to this cell\n        let diag1 = row + n - 1 - col;\n        let diag2 = row + col;\n        // Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n        if !cols[col] && !diags1[diag1] && !diags2[diag2] {\n            // Attempt: place the queen in this cell\n            state[row][col] = \"Q\".into();\n            (cols[col], diags1[diag1], diags2[diag2]) = (true, true, true);\n            // Place the next row\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // Backtrack: restore this cell to an empty cell\n            state[row][col] = \"#\".into();\n            (cols[col], diags1[diag1], diags2[diag2]) = (false, false, false);\n        }\n    }\n}\n\n/* Solve N queens */\nfn n_queens(n: usize) -> Vec<Vec<Vec<String>>> {\n    // Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n    let mut state: Vec<Vec<String>> = vec![vec![\"#\".to_string(); n]; n];\n    let mut cols = vec![false; n]; // Record whether there is a queen in the column\n    let mut diags1 = vec![false; 2 * n - 1]; // Record whether there is a queen on the main diagonal\n    let mut diags2 = vec![false; 2 * n - 1]; // Record whether there is a queen on the anti-diagonal\n    let mut res: Vec<Vec<Vec<String>>> = Vec::new();\n\n    backtrack(\n        0,\n        n,\n        &mut state,\n        &mut res,\n        &mut cols,\n        &mut diags1,\n        &mut diags2,\n    );\n\n    res\n}\n
      n_queens.c
      /* Backtracking algorithm: N queens */\nvoid backtrack(int row, int n, char state[MAX_SIZE][MAX_SIZE], char ***res, int *resSize, bool cols[MAX_SIZE],\n               bool diags1[2 * MAX_SIZE - 1], bool diags2[2 * MAX_SIZE - 1]) {\n    // When all rows are placed, record the solution\n    if (row == n) {\n        res[*resSize] = (char **)malloc(sizeof(char *) * n);\n        for (int i = 0; i < n; ++i) {\n            res[*resSize][i] = (char *)malloc(sizeof(char) * (n + 1));\n            strcpy(res[*resSize][i], state[i]);\n        }\n        (*resSize)++;\n        return;\n    }\n    // Traverse all columns\n    for (int col = 0; col < n; col++) {\n        // Calculate the main diagonal and anti-diagonal corresponding to this cell\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // Attempt: place the queen in this cell\n            state[row][col] = 'Q';\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // Place the next row\n            backtrack(row + 1, n, state, res, resSize, cols, diags1, diags2);\n            // Backtrack: restore this cell to an empty cell\n            state[row][col] = '#';\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* Solve N queens */\nchar ***nQueens(int n, int *returnSize) {\n    char state[MAX_SIZE][MAX_SIZE];\n    // Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n    for (int i = 0; i < n; ++i) {\n        for (int j = 0; j < n; ++j) {\n            state[i][j] = '#';\n        }\n        state[i][n] = '\\0';\n    }\n    bool cols[MAX_SIZE] = {false};           // Record whether there is a queen in the column\n    bool diags1[2 * MAX_SIZE - 1] = {false}; // Record whether there is a queen on the main diagonal\n    bool diags2[2 * MAX_SIZE - 1] = {false}; // Record whether there is a queen on the anti-diagonal\n\n    char ***res = (char ***)malloc(sizeof(char **) * MAX_SIZE);\n    *returnSize = 0;\n    backtrack(0, n, state, res, returnSize, cols, diags1, diags2);\n    return res;\n}\n
      n_queens.kt
      /* Backtracking algorithm: N queens */\nfun backtrack(\n    row: Int,\n    n: Int,\n    state: MutableList<MutableList<String>>,\n    res: MutableList<MutableList<MutableList<String>>?>,\n    cols: BooleanArray,\n    diags1: BooleanArray,\n    diags2: BooleanArray\n) {\n    // When all rows are placed, record the solution\n    if (row == n) {\n        val copyState = mutableListOf<MutableList<String>>()\n        for (sRow in state) {\n            copyState.add(sRow.toMutableList())\n        }\n        res.add(copyState)\n        return\n    }\n    // Traverse all columns\n    for (col in 0..<n) {\n        // Calculate the main diagonal and anti-diagonal corresponding to this cell\n        val diag1 = row - col + n - 1\n        val diag2 = row + col\n        // Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // Attempt: place the queen in this cell\n            state[row][col] = \"Q\"\n            diags2[diag2] = true\n            diags1[diag1] = diags2[diag2]\n            cols[col] = diags1[diag1]\n            // Place the next row\n            backtrack(row + 1, n, state, res, cols, diags1, diags2)\n            // Backtrack: restore this cell to an empty cell\n            state[row][col] = \"#\"\n            diags2[diag2] = false\n            diags1[diag1] = diags2[diag2]\n            cols[col] = diags1[diag1]\n        }\n    }\n}\n\n/* Solve N queens */\nfun nQueens(n: Int): MutableList<MutableList<MutableList<String>>?> {\n    // Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n    val state = mutableListOf<MutableList<String>>()\n    for (i in 0..<n) {\n        val row = mutableListOf<String>()\n        for (j in 0..<n) {\n            row.add(\"#\")\n        }\n        state.add(row)\n    }\n    val cols = BooleanArray(n) // Record whether there is a queen in the column\n    val diags1 = BooleanArray(2 * n - 1) // Record whether there is a queen on the main diagonal\n    val diags2 = BooleanArray(2 * n - 1) // Record whether there is a queen on the anti-diagonal\n    val res = mutableListOf<MutableList<MutableList<String>>?>()\n\n    backtrack(0, n, state, res, cols, diags1, diags2)\n\n    return res\n}\n
      n_queens.rb
      ### Backtracking: n queens ###\ndef backtrack(row, n, state, res, cols, diags1, diags2)\n  # When all rows are placed, record the solution\n  if row == n\n    res << state.map { |row| row.dup }\n    return\n  end\n\n  # Traverse all columns\n  for col in 0...n\n    # Calculate the main diagonal and anti-diagonal corresponding to this cell\n    diag1 = row - col + n - 1\n    diag2 = row + col\n    # Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell\n    if !cols[col] && !diags1[diag1] && !diags2[diag2]\n      # Attempt: place the queen in this cell\n      state[row][col] = \"Q\"\n      cols[col] = diags1[diag1] = diags2[diag2] = true\n      # Place the next row\n      backtrack(row + 1, n, state, res, cols, diags1, diags2)\n      # Backtrack: restore this cell to an empty cell\n      state[row][col] = \"#\"\n      cols[col] = diags1[diag1] = diags2[diag2] = false\n    end\n  end\nend\n\n### Solve n queens ###\ndef n_queens(n)\n  # Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell\n  state = Array.new(n) { Array.new(n, \"#\") }\n  cols = Array.new(n, false) # Record whether there is a queen in the column\n  diags1 = Array.new(2 * n - 1, false) # Record whether there is a queen on the main diagonal\n  diags2 = Array.new(2 * n - 1, false) # Record whether there is a queen on the anti-diagonal\n  res = []\n  backtrack(0, n, state, res, cols, diags1, diags2)\n\n  res\nend\n

      Placing \\(n\\) queens row by row, considering the column constraint, from the first row to the last row there are \\(n\\), \\(n-1\\), \\(\\dots\\), \\(2\\), \\(1\\) choices, using \\(O(n!)\\) time. When recording a solution, it is necessary to copy the matrix state and add it to res, and the copy operation uses \\(O(n^2)\\) time. Therefore, the overall time complexity is \\(O(n! \\cdot n^2)\\). In practice, pruning based on diagonal constraints can also significantly reduce the search space, so the search efficiency is often better than the time complexity mentioned above.

      The array state uses \\(O(n^2)\\) space, and the arrays cols, diags1, and diags2 each use \\(O(n)\\) space. The maximum recursion depth is \\(n\\), using \\(O(n)\\) stack frame space. Therefore, the space complexity is \\(O(n^2)\\).

      "},{"location":"chapter_backtracking/permutations_problem/","title":"13.2 \u00a0 Permutations Problem","text":"

      The permutations problem is a classic application of backtracking algorithms. It is defined as finding all possible arrangements of elements in a given collection (such as an array or string).

      Table 13-2 shows several example datasets, including input arrays and their corresponding permutations.

      Table 13-2 \u00a0 Permutations Examples

      Input Array All Permutations \\([1]\\) \\([1]\\) \\([1, 2]\\) \\([1, 2], [2, 1]\\) \\([1, 2, 3]\\) \\([1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]\\)"},{"location":"chapter_backtracking/permutations_problem/#1321-case-with-distinct-elements","title":"13.2.1 \u00a0 Case with Distinct Elements","text":"

      Question

      Given an integer array with no duplicate elements, return all possible permutations.

      From the perspective of backtracking algorithms, we can imagine the process of generating permutations as the result of a series of choices. Suppose the input array is \\([1, 2, 3]\\). If we first choose \\(1\\), then choose \\(3\\), and finally choose \\(2\\), we obtain the permutation \\([1, 3, 2]\\). Backtracking means undoing a choice and then trying other choices.

      From the perspective of backtracking code, the candidate set choices consists of all elements in the input array, and the state state is the elements that have been chosen so far. Note that each element can only be chosen once, therefore all elements in state should be unique.

      As shown in Figure 13-5, we can unfold the search process into a recursion tree, where each node in the tree represents the current state state. Starting from the root node, after three rounds of choices, we reach a leaf node, and each leaf node corresponds to a permutation.

      Figure 13-5 \u00a0 Recursion tree of permutations

      "},{"location":"chapter_backtracking/permutations_problem/#1-pruning-duplicate-choices","title":"1. \u00a0 Pruning Duplicate Choices","text":"

      To ensure that each element is chosen only once, we consider introducing a boolean array selected, where selected[i] indicates whether choices[i] has been chosen. We implement the following pruning operation based on it.

      • After making a choice choice[i], we set selected[i] to \\(\\text{True}\\), indicating that it has been chosen.
      • When traversing the candidate list choices, we skip all nodes that have been chosen, which is pruning.

      As shown in Figure 13-6, suppose we choose \\(1\\) in the first round, \\(3\\) in the second round, and \\(2\\) in the third round. Then we need to prune the branch of element \\(1\\) in the second round and prune the branches of elements \\(1\\) and \\(3\\) in the third round.

      Figure 13-6 \u00a0 Pruning example of permutations

      Observing the above figure, we find that this pruning operation reduces the search space size from \\(O(n^n)\\) to \\(O(n!)\\).

      "},{"location":"chapter_backtracking/permutations_problem/#2-code-implementation","title":"2. \u00a0 Code Implementation","text":"

      After understanding the above information, we can fill in the blanks in the template code. To shorten the overall code, we do not implement each function in the template separately, but instead unfold them in the backtrack() function:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby permutations_i.py
      def backtrack(\n    state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]\n):\n    \"\"\"Backtracking algorithm: Permutations I\"\"\"\n    # When the state length equals the number of elements, record the solution\n    if len(state) == len(choices):\n        res.append(list(state))\n        return\n    # Traverse all choices\n    for i, choice in enumerate(choices):\n        # Pruning: do not allow repeated selection of elements\n        if not selected[i]:\n            # Attempt: make choice, update state\n            selected[i] = True\n            state.append(choice)\n            # Proceed to the next round of selection\n            backtrack(state, choices, selected, res)\n            # Backtrack: undo choice, restore to previous state\n            selected[i] = False\n            state.pop()\n\ndef permutations_i(nums: list[int]) -> list[list[int]]:\n    \"\"\"Permutations I\"\"\"\n    res = []\n    backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\n    return res\n
      permutations_i.cpp
      /* Backtracking algorithm: Permutations I */\nvoid backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &selected, vector<vector<int>> &res) {\n    // When the state length equals the number of elements, record the solution\n    if (state.size() == choices.size()) {\n        res.push_back(state);\n        return;\n    }\n    // Traverse all choices\n    for (int i = 0; i < choices.size(); i++) {\n        int choice = choices[i];\n        // Pruning: do not allow repeated selection of elements\n        if (!selected[i]) {\n            // Attempt: make choice, update state\n            selected[i] = true;\n            state.push_back(choice);\n            // Proceed to the next round of selection\n            backtrack(state, choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.pop_back();\n        }\n    }\n}\n\n/* Permutations I */\nvector<vector<int>> permutationsI(vector<int> nums) {\n    vector<int> state;\n    vector<bool> selected(nums.size(), false);\n    vector<vector<int>> res;\n    backtrack(state, nums, selected, res);\n    return res;\n}\n
      permutations_i.java
      /* Backtracking algorithm: Permutations I */\nvoid backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {\n    // When the state length equals the number of elements, record the solution\n    if (state.size() == choices.length) {\n        res.add(new ArrayList<Integer>(state));\n        return;\n    }\n    // Traverse all choices\n    for (int i = 0; i < choices.length; i++) {\n        int choice = choices[i];\n        // Pruning: do not allow repeated selection of elements\n        if (!selected[i]) {\n            // Attempt: make choice, update state\n            selected[i] = true;\n            state.add(choice);\n            // Proceed to the next round of selection\n            backtrack(state, choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.remove(state.size() - 1);\n        }\n    }\n}\n\n/* Permutations I */\nList<List<Integer>> permutationsI(int[] nums) {\n    List<List<Integer>> res = new ArrayList<List<Integer>>();\n    backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);\n    return res;\n}\n
      permutations_i.cs
      /* Backtracking algorithm: Permutations I */\nvoid Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {\n    // When the state length equals the number of elements, record the solution\n    if (state.Count == choices.Length) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // Traverse all choices\n    for (int i = 0; i < choices.Length; i++) {\n        int choice = choices[i];\n        // Pruning: do not allow repeated selection of elements\n        if (!selected[i]) {\n            // Attempt: make choice, update state\n            selected[i] = true;\n            state.Add(choice);\n            // Proceed to the next round of selection\n            Backtrack(state, choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.RemoveAt(state.Count - 1);\n        }\n    }\n}\n\n/* Permutations I */\nList<List<int>> PermutationsI(int[] nums) {\n    List<List<int>> res = [];\n    Backtrack([], nums, new bool[nums.Length], res);\n    return res;\n}\n
      permutations_i.go
      /* Backtracking algorithm: Permutations I */\nfunc backtrackI(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {\n    // When the state length equals the number of elements, record the solution\n    if len(*state) == len(*choices) {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n    }\n    // Traverse all choices\n    for i := 0; i < len(*choices); i++ {\n        choice := (*choices)[i]\n        // Pruning: do not allow repeated selection of elements\n        if !(*selected)[i] {\n            // Attempt: make choice, update state\n            (*selected)[i] = true\n            *state = append(*state, choice)\n            // Proceed to the next round of selection\n            backtrackI(state, choices, selected, res)\n            // Backtrack: undo choice, restore to previous state\n            (*selected)[i] = false\n            *state = (*state)[:len(*state)-1]\n        }\n    }\n}\n\n/* Permutations I */\nfunc permutationsI(nums []int) [][]int {\n    res := make([][]int, 0)\n    state := make([]int, 0)\n    selected := make([]bool, len(nums))\n    backtrackI(&state, &nums, &selected, &res)\n    return res\n}\n
      permutations_i.swift
      /* Backtracking algorithm: Permutations I */\nfunc backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {\n    // When the state length equals the number of elements, record the solution\n    if state.count == choices.count {\n        res.append(state)\n        return\n    }\n    // Traverse all choices\n    for (i, choice) in choices.enumerated() {\n        // Pruning: do not allow repeated selection of elements\n        if !selected[i] {\n            // Attempt: make choice, update state\n            selected[i] = true\n            state.append(choice)\n            // Proceed to the next round of selection\n            backtrack(state: &state, choices: choices, selected: &selected, res: &res)\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false\n            state.removeLast()\n        }\n    }\n}\n\n/* Permutations I */\nfunc permutationsI(nums: [Int]) -> [[Int]] {\n    var state: [Int] = []\n    var selected = Array(repeating: false, count: nums.count)\n    var res: [[Int]] = []\n    backtrack(state: &state, choices: nums, selected: &selected, res: &res)\n    return res\n}\n
      permutations_i.js
      /* Backtracking algorithm: Permutations I */\nfunction backtrack(state, choices, selected, res) {\n    // When the state length equals the number of elements, record the solution\n    if (state.length === choices.length) {\n        res.push([...state]);\n        return;\n    }\n    // Traverse all choices\n    choices.forEach((choice, i) => {\n        // Pruning: do not allow repeated selection of elements\n        if (!selected[i]) {\n            // Attempt: make choice, update state\n            selected[i] = true;\n            state.push(choice);\n            // Proceed to the next round of selection\n            backtrack(state, choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.pop();\n        }\n    });\n}\n\n/* Permutations I */\nfunction permutationsI(nums) {\n    const res = [];\n    backtrack([], nums, Array(nums.length).fill(false), res);\n    return res;\n}\n
      permutations_i.ts
      /* Backtracking algorithm: Permutations I */\nfunction backtrack(\n    state: number[],\n    choices: number[],\n    selected: boolean[],\n    res: number[][]\n): void {\n    // When the state length equals the number of elements, record the solution\n    if (state.length === choices.length) {\n        res.push([...state]);\n        return;\n    }\n    // Traverse all choices\n    choices.forEach((choice, i) => {\n        // Pruning: do not allow repeated selection of elements\n        if (!selected[i]) {\n            // Attempt: make choice, update state\n            selected[i] = true;\n            state.push(choice);\n            // Proceed to the next round of selection\n            backtrack(state, choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.pop();\n        }\n    });\n}\n\n/* Permutations I */\nfunction permutationsI(nums: number[]): number[][] {\n    const res: number[][] = [];\n    backtrack([], nums, Array(nums.length).fill(false), res);\n    return res;\n}\n
      permutations_i.dart
      /* Backtracking algorithm: Permutations I */\nvoid backtrack(\n  List<int> state,\n  List<int> choices,\n  List<bool> selected,\n  List<List<int>> res,\n) {\n  // When the state length equals the number of elements, record the solution\n  if (state.length == choices.length) {\n    res.add(List.from(state));\n    return;\n  }\n  // Traverse all choices\n  for (int i = 0; i < choices.length; i++) {\n    int choice = choices[i];\n    // Pruning: do not allow repeated selection of elements\n    if (!selected[i]) {\n      // Attempt: make choice, update state\n      selected[i] = true;\n      state.add(choice);\n      // Proceed to the next round of selection\n      backtrack(state, choices, selected, res);\n      // Backtrack: undo choice, restore to previous state\n      selected[i] = false;\n      state.removeLast();\n    }\n  }\n}\n\n/* Permutations I */\nList<List<int>> permutationsI(List<int> nums) {\n  List<List<int>> res = [];\n  backtrack([], nums, List.filled(nums.length, false), res);\n  return res;\n}\n
      permutations_i.rs
      /* Backtracking algorithm: Permutations I */\nfn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {\n    // When the state length equals the number of elements, record the solution\n    if state.len() == choices.len() {\n        res.push(state);\n        return;\n    }\n    // Traverse all choices\n    for i in 0..choices.len() {\n        let choice = choices[i];\n        // Pruning: do not allow repeated selection of elements\n        if !selected[i] {\n            // Attempt: make choice, update state\n            selected[i] = true;\n            state.push(choice);\n            // Proceed to the next round of selection\n            backtrack(state.clone(), choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.pop();\n        }\n    }\n}\n\n/* Permutations I */\nfn permutations_i(nums: &mut [i32]) -> Vec<Vec<i32>> {\n    let mut res = Vec::new(); // State (subset)\n    backtrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);\n    res\n}\n
      permutations_i.c
      /* Backtracking algorithm: Permutations I */\nvoid backtrack(int *state, int stateSize, int *choices, int choicesSize, bool *selected, int **res, int *resSize) {\n    // When the state length equals the number of elements, record the solution\n    if (stateSize == choicesSize) {\n        res[*resSize] = (int *)malloc(choicesSize * sizeof(int));\n        for (int i = 0; i < choicesSize; i++) {\n            res[*resSize][i] = state[i];\n        }\n        (*resSize)++;\n        return;\n    }\n    // Traverse all choices\n    for (int i = 0; i < choicesSize; i++) {\n        int choice = choices[i];\n        // Pruning: do not allow repeated selection of elements\n        if (!selected[i]) {\n            // Attempt: make choice, update state\n            selected[i] = true;\n            state[stateSize] = choice;\n            // Proceed to the next round of selection\n            backtrack(state, stateSize + 1, choices, choicesSize, selected, res, resSize);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n        }\n    }\n}\n\n/* Permutations I */\nint **permutationsI(int *nums, int numsSize, int *returnSize) {\n    int *state = (int *)malloc(numsSize * sizeof(int));\n    bool *selected = (bool *)malloc(numsSize * sizeof(bool));\n    for (int i = 0; i < numsSize; i++) {\n        selected[i] = false;\n    }\n    int **res = (int **)malloc(MAX_SIZE * sizeof(int *));\n    *returnSize = 0;\n\n    backtrack(state, 0, nums, numsSize, selected, res, returnSize);\n\n    free(state);\n    free(selected);\n\n    return res;\n}\n
      permutations_i.kt
      /* Backtracking algorithm: Permutations I */\nfun backtrack(\n    state: MutableList<Int>,\n    choices: IntArray,\n    selected: BooleanArray,\n    res: MutableList<MutableList<Int>?>\n) {\n    // When the state length equals the number of elements, record the solution\n    if (state.size == choices.size) {\n        res.add(state.toMutableList())\n        return\n    }\n    // Traverse all choices\n    for (i in choices.indices) {\n        val choice = choices[i]\n        // Pruning: do not allow repeated selection of elements\n        if (!selected[i]) {\n            // Attempt: make choice, update state\n            selected[i] = true\n            state.add(choice)\n            // Proceed to the next round of selection\n            backtrack(state, choices, selected, res)\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false\n            state.removeAt(state.size - 1)\n        }\n    }\n}\n\n/* Permutations I */\nfun permutationsI(nums: IntArray): MutableList<MutableList<Int>?> {\n    val res = mutableListOf<MutableList<Int>?>()\n    backtrack(mutableListOf(), nums, BooleanArray(nums.size), res)\n    return res\n}\n
      permutations_i.rb
      ### Backtracking: permutations I ###\ndef backtrack(state, choices, selected, res)\n  # When the state length equals the number of elements, record the solution\n  if state.length == choices.length\n    res << state.dup\n    return\n  end\n\n  # Traverse all choices\n  choices.each_with_index do |choice, i|\n    # Pruning: do not allow repeated selection of elements\n    unless selected[i]\n      # Attempt: make choice, update state\n      selected[i] = true\n      state << choice\n      # Proceed to the next round of selection\n      backtrack(state, choices, selected, res)\n      # Backtrack: undo choice, restore to previous state\n      selected[i] = false\n      state.pop\n    end\n  end\nend\n\n### Permutations I ###\ndef permutations_i(nums)\n  res = []\n  backtrack([], nums, Array.new(nums.length, false), res)\n  res\nend\n
      "},{"location":"chapter_backtracking/permutations_problem/#1322-case-with-duplicate-elements","title":"13.2.2 \u00a0 Case with Duplicate Elements","text":"

      Question

      Given an integer array that may contain duplicate elements, return all unique permutations.

      Suppose the input array is \\([1, 1, 2]\\). To distinguish the two duplicate elements \\(1\\), we denote the second \\(1\\) as \\(\\hat{1}\\).

      As shown in Figure 13-7, the method described above generates permutations where half are duplicates.

      Figure 13-7 \u00a0 Duplicate permutations

      So how do we remove duplicate permutations? The most direct approach is to use a hash set to directly deduplicate the permutation results. However, this is not elegant because the search branches that generate duplicate permutations are unnecessary and should be identified and pruned early, which can further improve algorithm efficiency.

      "},{"location":"chapter_backtracking/permutations_problem/#1-pruning-duplicate-elements","title":"1. \u00a0 Pruning Duplicate Elements","text":"

      Observe Figure 13-8. In the first round, choosing \\(1\\) or choosing \\(\\hat{1}\\) is equivalent. All permutations generated under these two choices are duplicates. Therefore, we should prune \\(\\hat{1}\\).

      Similarly, after choosing \\(2\\) in the first round, the \\(1\\) and \\(\\hat{1}\\) in the second round also produce duplicate branches, so the second round's \\(\\hat{1}\\) should also be pruned.

      Essentially, our goal is to ensure that multiple equal elements are chosen only once in a certain round of choices.

      Figure 13-8 \u00a0 Pruning duplicate permutations

      "},{"location":"chapter_backtracking/permutations_problem/#2-code-implementation_1","title":"2. \u00a0 Code Implementation","text":"

      Building on the code from the previous problem, we consider opening a hash set duplicated in each round of choices to record which elements have been tried in this round, and prune duplicate elements:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby permutations_ii.py
      def backtrack(\n    state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]\n):\n    \"\"\"Backtracking algorithm: Permutations II\"\"\"\n    # When the state length equals the number of elements, record the solution\n    if len(state) == len(choices):\n        res.append(list(state))\n        return\n    # Traverse all choices\n    duplicated = set[int]()\n    for i, choice in enumerate(choices):\n        # Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n        if not selected[i] and choice not in duplicated:\n            # Attempt: make choice, update state\n            duplicated.add(choice)  # Record the selected element value\n            selected[i] = True\n            state.append(choice)\n            # Proceed to the next round of selection\n            backtrack(state, choices, selected, res)\n            # Backtrack: undo choice, restore to previous state\n            selected[i] = False\n            state.pop()\n\ndef permutations_ii(nums: list[int]) -> list[list[int]]:\n    \"\"\"Permutations II\"\"\"\n    res = []\n    backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\n    return res\n
      permutations_ii.cpp
      /* Backtracking algorithm: Permutations II */\nvoid backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &selected, vector<vector<int>> &res) {\n    // When the state length equals the number of elements, record the solution\n    if (state.size() == choices.size()) {\n        res.push_back(state);\n        return;\n    }\n    // Traverse all choices\n    unordered_set<int> duplicated;\n    for (int i = 0; i < choices.size(); i++) {\n        int choice = choices[i];\n        // Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n        if (!selected[i] && duplicated.find(choice) == duplicated.end()) {\n            // Attempt: make choice, update state\n            duplicated.emplace(choice); // Record the selected element value\n            selected[i] = true;\n            state.push_back(choice);\n            // Proceed to the next round of selection\n            backtrack(state, choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.pop_back();\n        }\n    }\n}\n\n/* Permutations II */\nvector<vector<int>> permutationsII(vector<int> nums) {\n    vector<int> state;\n    vector<bool> selected(nums.size(), false);\n    vector<vector<int>> res;\n    backtrack(state, nums, selected, res);\n    return res;\n}\n
      permutations_ii.java
      /* Backtracking algorithm: Permutations II */\nvoid backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {\n    // When the state length equals the number of elements, record the solution\n    if (state.size() == choices.length) {\n        res.add(new ArrayList<Integer>(state));\n        return;\n    }\n    // Traverse all choices\n    Set<Integer> duplicated = new HashSet<Integer>();\n    for (int i = 0; i < choices.length; i++) {\n        int choice = choices[i];\n        // Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n        if (!selected[i] && !duplicated.contains(choice)) {\n            // Attempt: make choice, update state\n            duplicated.add(choice); // Record the selected element value\n            selected[i] = true;\n            state.add(choice);\n            // Proceed to the next round of selection\n            backtrack(state, choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.remove(state.size() - 1);\n        }\n    }\n}\n\n/* Permutations II */\nList<List<Integer>> permutationsII(int[] nums) {\n    List<List<Integer>> res = new ArrayList<List<Integer>>();\n    backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);\n    return res;\n}\n
      permutations_ii.cs
      /* Backtracking algorithm: Permutations II */\nvoid Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {\n    // When the state length equals the number of elements, record the solution\n    if (state.Count == choices.Length) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // Traverse all choices\n    HashSet<int> duplicated = [];\n    for (int i = 0; i < choices.Length; i++) {\n        int choice = choices[i];\n        // Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n        if (!selected[i] && !duplicated.Contains(choice)) {\n            // Attempt: make choice, update state\n            duplicated.Add(choice); // Record the selected element value\n            selected[i] = true;\n            state.Add(choice);\n            // Proceed to the next round of selection\n            Backtrack(state, choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.RemoveAt(state.Count - 1);\n        }\n    }\n}\n\n/* Permutations II */\nList<List<int>> PermutationsII(int[] nums) {\n    List<List<int>> res = [];\n    Backtrack([], nums, new bool[nums.Length], res);\n    return res;\n}\n
      permutations_ii.go
      /* Backtracking algorithm: Permutations II */\nfunc backtrackII(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {\n    // When the state length equals the number of elements, record the solution\n    if len(*state) == len(*choices) {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n    }\n    // Traverse all choices\n    duplicated := make(map[int]struct{}, 0)\n    for i := 0; i < len(*choices); i++ {\n        choice := (*choices)[i]\n        // Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n        if _, ok := duplicated[choice]; !ok && !(*selected)[i] {\n            // Attempt: make choice, update state\n            // Record the selected element value\n            duplicated[choice] = struct{}{}\n            (*selected)[i] = true\n            *state = append(*state, choice)\n            // Proceed to the next round of selection\n            backtrackII(state, choices, selected, res)\n            // Backtrack: undo choice, restore to previous state\n            (*selected)[i] = false\n            *state = (*state)[:len(*state)-1]\n        }\n    }\n}\n\n/* Permutations II */\nfunc permutationsII(nums []int) [][]int {\n    res := make([][]int, 0)\n    state := make([]int, 0)\n    selected := make([]bool, len(nums))\n    backtrackII(&state, &nums, &selected, &res)\n    return res\n}\n
      permutations_ii.swift
      /* Backtracking algorithm: Permutations II */\nfunc backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {\n    // When the state length equals the number of elements, record the solution\n    if state.count == choices.count {\n        res.append(state)\n        return\n    }\n    // Traverse all choices\n    var duplicated: Set<Int> = []\n    for (i, choice) in choices.enumerated() {\n        // Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n        if !selected[i], !duplicated.contains(choice) {\n            // Attempt: make choice, update state\n            duplicated.insert(choice) // Record the selected element value\n            selected[i] = true\n            state.append(choice)\n            // Proceed to the next round of selection\n            backtrack(state: &state, choices: choices, selected: &selected, res: &res)\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false\n            state.removeLast()\n        }\n    }\n}\n\n/* Permutations II */\nfunc permutationsII(nums: [Int]) -> [[Int]] {\n    var state: [Int] = []\n    var selected = Array(repeating: false, count: nums.count)\n    var res: [[Int]] = []\n    backtrack(state: &state, choices: nums, selected: &selected, res: &res)\n    return res\n}\n
      permutations_ii.js
      /* Backtracking algorithm: Permutations II */\nfunction backtrack(state, choices, selected, res) {\n    // When the state length equals the number of elements, record the solution\n    if (state.length === choices.length) {\n        res.push([...state]);\n        return;\n    }\n    // Traverse all choices\n    const duplicated = new Set();\n    choices.forEach((choice, i) => {\n        // Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n        if (!selected[i] && !duplicated.has(choice)) {\n            // Attempt: make choice, update state\n            duplicated.add(choice); // Record the selected element value\n            selected[i] = true;\n            state.push(choice);\n            // Proceed to the next round of selection\n            backtrack(state, choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.pop();\n        }\n    });\n}\n\n/* Permutations II */\nfunction permutationsII(nums) {\n    const res = [];\n    backtrack([], nums, Array(nums.length).fill(false), res);\n    return res;\n}\n
      permutations_ii.ts
      /* Backtracking algorithm: Permutations II */\nfunction backtrack(\n    state: number[],\n    choices: number[],\n    selected: boolean[],\n    res: number[][]\n): void {\n    // When the state length equals the number of elements, record the solution\n    if (state.length === choices.length) {\n        res.push([...state]);\n        return;\n    }\n    // Traverse all choices\n    const duplicated = new Set();\n    choices.forEach((choice, i) => {\n        // Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n        if (!selected[i] && !duplicated.has(choice)) {\n            // Attempt: make choice, update state\n            duplicated.add(choice); // Record the selected element value\n            selected[i] = true;\n            state.push(choice);\n            // Proceed to the next round of selection\n            backtrack(state, choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.pop();\n        }\n    });\n}\n\n/* Permutations II */\nfunction permutationsII(nums: number[]): number[][] {\n    const res: number[][] = [];\n    backtrack([], nums, Array(nums.length).fill(false), res);\n    return res;\n}\n
      permutations_ii.dart
      /* Backtracking algorithm: Permutations II */\nvoid backtrack(\n  List<int> state,\n  List<int> choices,\n  List<bool> selected,\n  List<List<int>> res,\n) {\n  // When the state length equals the number of elements, record the solution\n  if (state.length == choices.length) {\n    res.add(List.from(state));\n    return;\n  }\n  // Traverse all choices\n  Set<int> duplicated = {};\n  for (int i = 0; i < choices.length; i++) {\n    int choice = choices[i];\n    // Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n    if (!selected[i] && !duplicated.contains(choice)) {\n      // Attempt: make choice, update state\n      duplicated.add(choice); // Record the selected element value\n      selected[i] = true;\n      state.add(choice);\n      // Proceed to the next round of selection\n      backtrack(state, choices, selected, res);\n      // Backtrack: undo choice, restore to previous state\n      selected[i] = false;\n      state.removeLast();\n    }\n  }\n}\n\n/* Permutations II */\nList<List<int>> permutationsII(List<int> nums) {\n  List<List<int>> res = [];\n  backtrack([], nums, List.filled(nums.length, false), res);\n  return res;\n}\n
      permutations_ii.rs
      /* Backtracking algorithm: Permutations II */\nfn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {\n    // When the state length equals the number of elements, record the solution\n    if state.len() == choices.len() {\n        res.push(state);\n        return;\n    }\n    // Traverse all choices\n    let mut duplicated = HashSet::<i32>::new();\n    for i in 0..choices.len() {\n        let choice = choices[i];\n        // Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n        if !selected[i] && !duplicated.contains(&choice) {\n            // Attempt: make choice, update state\n            duplicated.insert(choice); // Record the selected element value\n            selected[i] = true;\n            state.push(choice);\n            // Proceed to the next round of selection\n            backtrack(state.clone(), choices, selected, res);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n            state.pop();\n        }\n    }\n}\n\n/* Permutations II */\nfn permutations_ii(nums: &mut [i32]) -> Vec<Vec<i32>> {\n    let mut res = Vec::new();\n    backtrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);\n    res\n}\n
      permutations_ii.c
      /* Backtracking algorithm: Permutations II */\nvoid backtrack(int *state, int stateSize, int *choices, int choicesSize, bool *selected, int **res, int *resSize) {\n    // When the state length equals the number of elements, record the solution\n    if (stateSize == choicesSize) {\n        res[*resSize] = (int *)malloc(choicesSize * sizeof(int));\n        for (int i = 0; i < choicesSize; i++) {\n            res[*resSize][i] = state[i];\n        }\n        (*resSize)++;\n        return;\n    }\n    // Traverse all choices\n    bool duplicated[MAX_SIZE] = {false};\n    for (int i = 0; i < choicesSize; i++) {\n        int choice = choices[i];\n        // Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n        if (!selected[i] && !duplicated[choice]) {\n            // Attempt: make choice, update state\n            duplicated[choice] = true; // Record the selected element value\n            selected[i] = true;\n            state[stateSize] = choice;\n            // Proceed to the next round of selection\n            backtrack(state, stateSize + 1, choices, choicesSize, selected, res, resSize);\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false;\n        }\n    }\n}\n\n/* Permutations II */\nint **permutationsII(int *nums, int numsSize, int *returnSize) {\n    int *state = (int *)malloc(numsSize * sizeof(int));\n    bool *selected = (bool *)malloc(numsSize * sizeof(bool));\n    for (int i = 0; i < numsSize; i++) {\n        selected[i] = false;\n    }\n    int **res = (int **)malloc(MAX_SIZE * sizeof(int *));\n    *returnSize = 0;\n\n    backtrack(state, 0, nums, numsSize, selected, res, returnSize);\n\n    free(state);\n    free(selected);\n\n    return res;\n}\n
      permutations_ii.kt
      /* Backtracking algorithm: Permutations II */\nfun backtrack(\n    state: MutableList<Int>,\n    choices: IntArray,\n    selected: BooleanArray,\n    res: MutableList<MutableList<Int>?>\n) {\n    // When the state length equals the number of elements, record the solution\n    if (state.size == choices.size) {\n        res.add(state.toMutableList())\n        return\n    }\n    // Traverse all choices\n    val duplicated = HashSet<Int>()\n    for (i in choices.indices) {\n        val choice = choices[i]\n        // Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n        if (!selected[i] && !duplicated.contains(choice)) {\n            // Attempt: make choice, update state\n            duplicated.add(choice) // Record the selected element value\n            selected[i] = true\n            state.add(choice)\n            // Proceed to the next round of selection\n            backtrack(state, choices, selected, res)\n            // Backtrack: undo choice, restore to previous state\n            selected[i] = false\n            state.removeAt(state.size - 1)\n        }\n    }\n}\n\n/* Permutations II */\nfun permutationsII(nums: IntArray): MutableList<MutableList<Int>?> {\n    val res = mutableListOf<MutableList<Int>?>()\n    backtrack(mutableListOf(), nums, BooleanArray(nums.size), res)\n    return res\n}\n
      permutations_ii.rb
      ### Backtracking: permutations II ###\ndef backtrack(state, choices, selected, res)\n  # When the state length equals the number of elements, record the solution\n  if state.length == choices.length\n    res << state.dup\n    return\n  end\n\n  # Traverse all choices\n  duplicated = Set.new\n  choices.each_with_index do |choice, i|\n    # Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements\n    if !selected[i] && !duplicated.include?(choice)\n      # Attempt: make choice, update state\n      duplicated.add(choice)\n      selected[i] = true\n      state << choice\n      # Proceed to the next round of selection\n      backtrack(state, choices, selected, res)\n      # Backtrack: undo choice, restore to previous state\n      selected[i] = false\n      state.pop\n    end\n  end\nend\n\n### Permutations II ###\ndef permutations_ii(nums)\n  res = []\n  backtrack([], nums, Array.new(nums.length, false), res)\n  res\nend\n

      Assuming elements are pairwise distinct, there are \\(n!\\) (factorial) permutations of \\(n\\) elements. When recording results, we need to copy a list of length \\(n\\), using \\(O(n)\\) time. Therefore, the time complexity is \\(O(n! \\cdot n)\\).

      The maximum recursion depth is \\(n\\), using \\(O(n)\\) stack frame space. selected uses \\(O(n)\\) space. At most \\(n\\) duplicated sets exist simultaneously, using \\(O(n^2)\\) space. Therefore, the space complexity is \\(O(n^2)\\).

      "},{"location":"chapter_backtracking/permutations_problem/#3-comparison-of-two-pruning-methods","title":"3. \u00a0 Comparison of Two Pruning Methods","text":"

      Note that although both selected and duplicated are used for pruning, they have different objectives.

      • Pruning duplicate choices: There is only one selected throughout the entire search process. It records which elements are included in the current state, and its purpose is to prevent an element from appearing repeatedly in state.
      • Pruning duplicate elements: Each round of choices (each backtrack function call) contains a duplicated set. It records which elements have been chosen in this round's iteration (the for loop), and its purpose is to ensure that equal elements are chosen only once.

      Figure 13-9 shows the effective scope of the two pruning conditions. Note that each node in the tree represents a choice, and the nodes on the path from the root to a leaf node form a permutation.

      Figure 13-9 \u00a0 Effective scope of two pruning conditions

      "},{"location":"chapter_backtracking/subset_sum_problem/","title":"13.3 \u00a0 Subset-Sum Problem","text":""},{"location":"chapter_backtracking/subset_sum_problem/#1331-without-duplicate-elements","title":"13.3.1 \u00a0 Without Duplicate Elements","text":"

      Question

      Given a positive integer array nums and a target positive integer target, find all possible combinations where the sum of elements in the combination equals target. The given array has no duplicate elements, and each element can be selected multiple times. Return these combinations in list form, where the list should not contain duplicate combinations.

      For example, given the set \\(\\{3, 4, 5\\}\\) and target integer \\(9\\), the solutions are \\(\\{3, 3, 3\\}, \\{4, 5\\}\\). Note the following two points:

      • Elements in the input set can be selected repeatedly without limit.
      • Subsets do not distinguish element order; for example, \\(\\{4, 5\\}\\) and \\(\\{5, 4\\}\\) are the same subset.
      "},{"location":"chapter_backtracking/subset_sum_problem/#1-reference-to-full-permutation-solution","title":"1. \u00a0 Reference to Full Permutation Solution","text":"

      Similar to the full permutation problem, we can imagine the process of generating subsets as a series of choices, and update the \"sum of elements\" in real-time during the selection process. When the sum equals target, we record the subset to the result list.

      Unlike the full permutation problem, elements in this problem's set can be selected unlimited times, so we do not need to use a selected boolean list to track whether an element has been selected. We can make minor modifications to the full permutation code and initially obtain the solution:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby subset_sum_i_naive.py
      def backtrack(\n    state: list[int],\n    target: int,\n    total: int,\n    choices: list[int],\n    res: list[list[int]],\n):\n    \"\"\"Backtracking algorithm: Subset sum I\"\"\"\n    # When the subset sum equals target, record the solution\n    if total == target:\n        res.append(list(state))\n        return\n    # Traverse all choices\n    for i in range(len(choices)):\n        # Pruning: if the subset sum exceeds target, skip this choice\n        if total + choices[i] > target:\n            continue\n        # Attempt: make choice, update element sum total\n        state.append(choices[i])\n        # Proceed to the next round of selection\n        backtrack(state, target, total + choices[i], choices, res)\n        # Backtrack: undo choice, restore to previous state\n        state.pop()\n\ndef subset_sum_i_naive(nums: list[int], target: int) -> list[list[int]]:\n    \"\"\"Solve subset sum I (including duplicate subsets)\"\"\"\n    state = []  # State (subset)\n    total = 0  # Subset sum\n    res = []  # Result list (subset list)\n    backtrack(state, target, total, nums, res)\n    return res\n
      subset_sum_i_naive.cpp
      /* Backtracking algorithm: Subset sum I */\nvoid backtrack(vector<int> &state, int target, int total, vector<int> &choices, vector<vector<int>> &res) {\n    // When the subset sum equals target, record the solution\n    if (total == target) {\n        res.push_back(state);\n        return;\n    }\n    // Traverse all choices\n    for (size_t i = 0; i < choices.size(); i++) {\n        // Pruning: if the subset sum exceeds target, skip this choice\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // Attempt: make choice, update element sum total\n        state.push_back(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target, total + choices[i], choices, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop_back();\n    }\n}\n\n/* Solve subset sum I (including duplicate subsets) */\nvector<vector<int>> subsetSumINaive(vector<int> &nums, int target) {\n    vector<int> state;       // State (subset)\n    int total = 0;           // Subset sum\n    vector<vector<int>> res; // Result list (subset list)\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
      subset_sum_i_naive.java
      /* Backtracking algorithm: Subset sum I */\nvoid backtrack(List<Integer> state, int target, int total, int[] choices, List<List<Integer>> res) {\n    // When the subset sum equals target, record the solution\n    if (total == target) {\n        res.add(new ArrayList<>(state));\n        return;\n    }\n    // Traverse all choices\n    for (int i = 0; i < choices.length; i++) {\n        // Pruning: if the subset sum exceeds target, skip this choice\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // Attempt: make choice, update element sum total\n        state.add(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target, total + choices[i], choices, res);\n        // Backtrack: undo choice, restore to previous state\n        state.remove(state.size() - 1);\n    }\n}\n\n/* Solve subset sum I (including duplicate subsets) */\nList<List<Integer>> subsetSumINaive(int[] nums, int target) {\n    List<Integer> state = new ArrayList<>(); // State (subset)\n    int total = 0; // Subset sum\n    List<List<Integer>> res = new ArrayList<>(); // Result list (subset list)\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
      subset_sum_i_naive.cs
      /* Backtracking algorithm: Subset sum I */\nvoid Backtrack(List<int> state, int target, int total, int[] choices, List<List<int>> res) {\n    // When the subset sum equals target, record the solution\n    if (total == target) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // Traverse all choices\n    for (int i = 0; i < choices.Length; i++) {\n        // Pruning: if the subset sum exceeds target, skip this choice\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // Attempt: make choice, update element sum total\n        state.Add(choices[i]);\n        // Proceed to the next round of selection\n        Backtrack(state, target, total + choices[i], choices, res);\n        // Backtrack: undo choice, restore to previous state\n        state.RemoveAt(state.Count - 1);\n    }\n}\n\n/* Solve subset sum I (including duplicate subsets) */\nList<List<int>> SubsetSumINaive(int[] nums, int target) {\n    List<int> state = []; // State (subset)\n    int total = 0; // Subset sum\n    List<List<int>> res = []; // Result list (subset list)\n    Backtrack(state, target, total, nums, res);\n    return res;\n}\n
      subset_sum_i_naive.go
      /* Backtracking algorithm: Subset sum I */\nfunc backtrackSubsetSumINaive(total, target int, state, choices *[]int, res *[][]int) {\n    // When the subset sum equals target, record the solution\n    if target == total {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n        return\n    }\n    // Traverse all choices\n    for i := 0; i < len(*choices); i++ {\n        // Pruning: if the subset sum exceeds target, skip this choice\n        if total+(*choices)[i] > target {\n            continue\n        }\n        // Attempt: make choice, update element sum total\n        *state = append(*state, (*choices)[i])\n        // Proceed to the next round of selection\n        backtrackSubsetSumINaive(total+(*choices)[i], target, state, choices, res)\n        // Backtrack: undo choice, restore to previous state\n        *state = (*state)[:len(*state)-1]\n    }\n}\n\n/* Solve subset sum I (including duplicate subsets) */\nfunc subsetSumINaive(nums []int, target int) [][]int {\n    state := make([]int, 0) // State (subset)\n    total := 0              // Subset sum\n    res := make([][]int, 0) // Result list (subset list)\n    backtrackSubsetSumINaive(total, target, &state, &nums, &res)\n    return res\n}\n
      subset_sum_i_naive.swift
      /* Backtracking algorithm: Subset sum I */\nfunc backtrack(state: inout [Int], target: Int, total: Int, choices: [Int], res: inout [[Int]]) {\n    // When the subset sum equals target, record the solution\n    if total == target {\n        res.append(state)\n        return\n    }\n    // Traverse all choices\n    for i in choices.indices {\n        // Pruning: if the subset sum exceeds target, skip this choice\n        if total + choices[i] > target {\n            continue\n        }\n        // Attempt: make choice, update element sum total\n        state.append(choices[i])\n        // Proceed to the next round of selection\n        backtrack(state: &state, target: target, total: total + choices[i], choices: choices, res: &res)\n        // Backtrack: undo choice, restore to previous state\n        state.removeLast()\n    }\n}\n\n/* Solve subset sum I (including duplicate subsets) */\nfunc subsetSumINaive(nums: [Int], target: Int) -> [[Int]] {\n    var state: [Int] = [] // State (subset)\n    let total = 0 // Subset sum\n    var res: [[Int]] = [] // Result list (subset list)\n    backtrack(state: &state, target: target, total: total, choices: nums, res: &res)\n    return res\n}\n
      subset_sum_i_naive.js
      /* Backtracking algorithm: Subset sum I */\nfunction backtrack(state, target, total, choices, res) {\n    // When the subset sum equals target, record the solution\n    if (total === target) {\n        res.push([...state]);\n        return;\n    }\n    // Traverse all choices\n    for (let i = 0; i < choices.length; i++) {\n        // Pruning: if the subset sum exceeds target, skip this choice\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // Attempt: make choice, update element sum total\n        state.push(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target, total + choices[i], choices, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop();\n    }\n}\n\n/* Solve subset sum I (including duplicate subsets) */\nfunction subsetSumINaive(nums, target) {\n    const state = []; // State (subset)\n    const total = 0; // Subset sum\n    const res = []; // Result list (subset list)\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
      subset_sum_i_naive.ts
      /* Backtracking algorithm: Subset sum I */\nfunction backtrack(\n    state: number[],\n    target: number,\n    total: number,\n    choices: number[],\n    res: number[][]\n): void {\n    // When the subset sum equals target, record the solution\n    if (total === target) {\n        res.push([...state]);\n        return;\n    }\n    // Traverse all choices\n    for (let i = 0; i < choices.length; i++) {\n        // Pruning: if the subset sum exceeds target, skip this choice\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // Attempt: make choice, update element sum total\n        state.push(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target, total + choices[i], choices, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop();\n    }\n}\n\n/* Solve subset sum I (including duplicate subsets) */\nfunction subsetSumINaive(nums: number[], target: number): number[][] {\n    const state = []; // State (subset)\n    const total = 0; // Subset sum\n    const res = []; // Result list (subset list)\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
      subset_sum_i_naive.dart
      /* Backtracking algorithm: Subset sum I */\nvoid backtrack(\n  List<int> state,\n  int target,\n  int total,\n  List<int> choices,\n  List<List<int>> res,\n) {\n  // When the subset sum equals target, record the solution\n  if (total == target) {\n    res.add(List.from(state));\n    return;\n  }\n  // Traverse all choices\n  for (int i = 0; i < choices.length; i++) {\n    // Pruning: if the subset sum exceeds target, skip this choice\n    if (total + choices[i] > target) {\n      continue;\n    }\n    // Attempt: make choice, update element sum total\n    state.add(choices[i]);\n    // Proceed to the next round of selection\n    backtrack(state, target, total + choices[i], choices, res);\n    // Backtrack: undo choice, restore to previous state\n    state.removeLast();\n  }\n}\n\n/* Solve subset sum I (including duplicate subsets) */\nList<List<int>> subsetSumINaive(List<int> nums, int target) {\n  List<int> state = []; // State (subset)\n  int total = 0; // Sum of elements\n  List<List<int>> res = []; // Result list (subset list)\n  backtrack(state, target, total, nums, res);\n  return res;\n}\n
      subset_sum_i_naive.rs
      /* Backtracking algorithm: Subset sum I */\nfn backtrack(\n    state: &mut Vec<i32>,\n    target: i32,\n    total: i32,\n    choices: &[i32],\n    res: &mut Vec<Vec<i32>>,\n) {\n    // When the subset sum equals target, record the solution\n    if total == target {\n        res.push(state.clone());\n        return;\n    }\n    // Traverse all choices\n    for i in 0..choices.len() {\n        // Pruning: if the subset sum exceeds target, skip this choice\n        if total + choices[i] > target {\n            continue;\n        }\n        // Attempt: make choice, update element sum total\n        state.push(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target, total + choices[i], choices, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop();\n    }\n}\n\n/* Solve subset sum I (including duplicate subsets) */\nfn subset_sum_i_naive(nums: &[i32], target: i32) -> Vec<Vec<i32>> {\n    let mut state = Vec::new(); // State (subset)\n    let total = 0; // Subset sum\n    let mut res = Vec::new(); // Result list (subset list)\n    backtrack(&mut state, target, total, nums, &mut res);\n    res\n}\n
      subset_sum_i_naive.c
      /* Backtracking algorithm: Subset sum I */\nvoid backtrack(int target, int total, int *choices, int choicesSize) {\n    // When the subset sum equals target, record the solution\n    if (total == target) {\n        for (int i = 0; i < stateSize; i++) {\n            res[resSize][i] = state[i];\n        }\n        resColSizes[resSize++] = stateSize;\n        return;\n    }\n    // Traverse all choices\n    for (int i = 0; i < choicesSize; i++) {\n        // Pruning: if the subset sum exceeds target, skip this choice\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // Attempt: make choice, update element sum total\n        state[stateSize++] = choices[i];\n        // Proceed to the next round of selection\n        backtrack(target, total + choices[i], choices, choicesSize);\n        // Backtrack: undo choice, restore to previous state\n        stateSize--;\n    }\n}\n\n/* Solve subset sum I (including duplicate subsets) */\nvoid subsetSumINaive(int *nums, int numsSize, int target) {\n    resSize = 0; // Initialize solution count to 0\n    backtrack(target, 0, nums, numsSize);\n}\n
      subset_sum_i_naive.kt
      /* Backtracking algorithm: Subset sum I */\nfun backtrack(\n    state: MutableList<Int>,\n    target: Int,\n    total: Int,\n    choices: IntArray,\n    res: MutableList<MutableList<Int>?>\n) {\n    // When the subset sum equals target, record the solution\n    if (total == target) {\n        res.add(state.toMutableList())\n        return\n    }\n    // Traverse all choices\n    for (i in choices.indices) {\n        // Pruning: if the subset sum exceeds target, skip this choice\n        if (total + choices[i] > target) {\n            continue\n        }\n        // Attempt: make choice, update element sum total\n        state.add(choices[i])\n        // Proceed to the next round of selection\n        backtrack(state, target, total + choices[i], choices, res)\n        // Backtrack: undo choice, restore to previous state\n        state.removeAt(state.size - 1)\n    }\n}\n\n/* Solve subset sum I (including duplicate subsets) */\nfun subsetSumINaive(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {\n    val state = mutableListOf<Int>() // State (subset)\n    val total = 0 // Subset sum\n    val res = mutableListOf<MutableList<Int>?>() // Result list (subset list)\n    backtrack(state, target, total, nums, res)\n    return res\n}\n
      subset_sum_i_naive.rb
      ### Backtracking: subset sum I ###\ndef backtrack(state, target, total, choices, res)\n  # When the subset sum equals target, record the solution\n  if total == target\n    res << state.dup\n    return\n  end\n\n  # Traverse all choices\n  for i in 0...choices.length\n    # Pruning: if the subset sum exceeds target, skip this choice\n    next if total + choices[i] > target\n    # Attempt: make choice, update element sum total\n    state << choices[i]\n    # Proceed to the next round of selection\n    backtrack(state, target, total + choices[i], choices, res)\n    # Backtrack: undo choice, restore to previous state\n    state.pop\n  end\nend\n\n### Solve subset sum I (with duplicate subsets) ###\ndef subset_sum_i_naive(nums, target)\n  state = [] # State (subset)\n  total = 0 # Subset sum\n  res = [] # Result list (subset list)\n  backtrack(state, target, total, nums, res)\n  res\nend\n

      When we input array \\([3, 4, 5]\\) and target element \\(9\\) to the above code, the output is \\([3, 3, 3], [4, 5], [5, 4]\\). Although we successfully find all subsets that sum to \\(9\\), there are duplicate subsets \\([4, 5]\\) and \\([5, 4]\\).

      This is because the search process distinguishes the order of selections, but subsets do not distinguish selection order. As shown in Figure 13-10, selecting 4 first and then 5 versus selecting 5 first and then 4 are different branches, but they correspond to the same subset.

      Figure 13-10 \u00a0 Subset search and boundary pruning

      To eliminate duplicate subsets, one straightforward idea is to deduplicate the result list. However, this approach is very inefficient for two reasons:

      • When there are many array elements, especially when target is large, the search process generates many duplicate subsets.
      • Comparing subsets (arrays) is very time-consuming, requiring sorting the arrays first, then comparing each element in them.
      "},{"location":"chapter_backtracking/subset_sum_problem/#2-pruning-duplicate-subsets","title":"2. \u00a0 Pruning Duplicate Subsets","text":"

      We consider deduplication through pruning during the search process. Observing Figure 13-11, duplicate subsets occur when array elements are selected in different orders, as in the following cases:

      1. When the first and second rounds select \\(3\\) and \\(4\\) respectively, all subsets containing these two elements are generated, denoted as \\([3, 4, \\dots]\\).
      2. Afterward, when the first round selects \\(4\\), the second round should skip \\(3\\), because the subset \\([4, 3, \\dots]\\) generated by this choice is completely duplicate with the subset generated in step 1.

      In the search process, each level's choices are tried from left to right, so the rightmost branches are pruned more.

      1. The first two rounds select \\(3\\) and \\(5\\), generating subset \\([3, 5, \\dots]\\).
      2. The first two rounds select \\(4\\) and \\(5\\), generating subset \\([4, 5, \\dots]\\).
      3. If the first round selects \\(5\\), the second round should skip \\(3\\) and \\(4\\), because subsets \\([5, 3, \\dots]\\) and \\([5, 4, \\dots]\\) are completely duplicate with the subsets described in steps 1. and 2.

      Figure 13-11 \u00a0 Different selection orders leading to duplicate subsets

      In summary, given an input array \\([x_1, x_2, \\dots, x_n]\\), let the selection sequence in the search process be \\([x_{i_1}, x_{i_2}, \\dots, x_{i_m}]\\). This selection sequence must satisfy \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\); any selection sequence that does not satisfy this condition will cause duplicates and should be pruned.

      "},{"location":"chapter_backtracking/subset_sum_problem/#3-code-implementation","title":"3. \u00a0 Code Implementation","text":"

      To implement this pruning, we initialize a variable start to indicate the starting point of traversal. After making choice \\(x_{i}\\), set the next round to start traversal from index \\(i\\). This ensures that the selection sequence satisfies \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\), guaranteeing subset uniqueness.

      In addition, we have made the following two optimizations to the code:

      • Before starting the search, first sort the array nums. When traversing all choices, end the loop immediately when the subset sum exceeds target, because subsequent elements are larger, and their subset sums must exceed target.
      • Omit the element sum variable total and use subtraction on target to track the sum of elements. Record the solution when target equals \\(0\\).
      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby subset_sum_i.py
      def backtrack(\n    state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n    \"\"\"Backtracking algorithm: Subset sum I\"\"\"\n    # When the subset sum equals target, record the solution\n    if target == 0:\n        res.append(list(state))\n        return\n    # Traverse all choices\n    # Pruning 2: start traversing from start to avoid generating duplicate subsets\n    for i in range(start, len(choices)):\n        # Pruning 1: if the subset sum exceeds target, end the loop directly\n        # This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if target - choices[i] < 0:\n            break\n        # Attempt: make choice, update target, start\n        state.append(choices[i])\n        # Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i, res)\n        # Backtrack: undo choice, restore to previous state\n        state.pop()\n\ndef subset_sum_i(nums: list[int], target: int) -> list[list[int]]:\n    \"\"\"Solve subset sum I\"\"\"\n    state = []  # State (subset)\n    nums.sort()  # Sort nums\n    start = 0  # Start point for traversal\n    res = []  # Result list (subset list)\n    backtrack(state, target, nums, start, res)\n    return res\n
      subset_sum_i.cpp
      /* Backtracking algorithm: Subset sum I */\nvoid backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {\n    // When the subset sum equals target, record the solution\n    if (target == 0) {\n        res.push_back(state);\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    for (int i = start; i < choices.size(); i++) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // Attempt: make choice, update target, start\n        state.push_back(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop_back();\n    }\n}\n\n/* Solve subset sum I */\nvector<vector<int>> subsetSumI(vector<int> &nums, int target) {\n    vector<int> state;              // State (subset)\n    sort(nums.begin(), nums.end()); // Sort nums\n    int start = 0;                  // Start point for traversal\n    vector<vector<int>> res;        // Result list (subset list)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
      subset_sum_i.java
      /* Backtracking algorithm: Subset sum I */\nvoid backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {\n    // When the subset sum equals target, record the solution\n    if (target == 0) {\n        res.add(new ArrayList<>(state));\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    for (int i = start; i < choices.length; i++) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // Attempt: make choice, update target, start\n        state.add(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i, res);\n        // Backtrack: undo choice, restore to previous state\n        state.remove(state.size() - 1);\n    }\n}\n\n/* Solve subset sum I */\nList<List<Integer>> subsetSumI(int[] nums, int target) {\n    List<Integer> state = new ArrayList<>(); // State (subset)\n    Arrays.sort(nums); // Sort nums\n    int start = 0; // Start point for traversal\n    List<List<Integer>> res = new ArrayList<>(); // Result list (subset list)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
      subset_sum_i.cs
      /* Backtracking algorithm: Subset sum I */\nvoid Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {\n    // When the subset sum equals target, record the solution\n    if (target == 0) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    for (int i = start; i < choices.Length; i++) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // Attempt: make choice, update target, start\n        state.Add(choices[i]);\n        // Proceed to the next round of selection\n        Backtrack(state, target - choices[i], choices, i, res);\n        // Backtrack: undo choice, restore to previous state\n        state.RemoveAt(state.Count - 1);\n    }\n}\n\n/* Solve subset sum I */\nList<List<int>> SubsetSumI(int[] nums, int target) {\n    List<int> state = []; // State (subset)\n    Array.Sort(nums); // Sort nums\n    int start = 0; // Start point for traversal\n    List<List<int>> res = []; // Result list (subset list)\n    Backtrack(state, target, nums, start, res);\n    return res;\n}\n
      subset_sum_i.go
      /* Backtracking algorithm: Subset sum I */\nfunc backtrackSubsetSumI(start, target int, state, choices *[]int, res *[][]int) {\n    // When the subset sum equals target, record the solution\n    if target == 0 {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n        return\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    for i := start; i < len(*choices); i++ {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if target-(*choices)[i] < 0 {\n            break\n        }\n        // Attempt: make choice, update target, start\n        *state = append(*state, (*choices)[i])\n        // Proceed to the next round of selection\n        backtrackSubsetSumI(i, target-(*choices)[i], state, choices, res)\n        // Backtrack: undo choice, restore to previous state\n        *state = (*state)[:len(*state)-1]\n    }\n}\n\n/* Solve subset sum I */\nfunc subsetSumI(nums []int, target int) [][]int {\n    state := make([]int, 0) // State (subset)\n    sort.Ints(nums)         // Sort nums\n    start := 0              // Start point for traversal\n    res := make([][]int, 0) // Result list (subset list)\n    backtrackSubsetSumI(start, target, &state, &nums, &res)\n    return res\n}\n
      subset_sum_i.swift
      /* Backtracking algorithm: Subset sum I */\nfunc backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {\n    // When the subset sum equals target, record the solution\n    if target == 0 {\n        res.append(state)\n        return\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    for i in choices.indices.dropFirst(start) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if target - choices[i] < 0 {\n            break\n        }\n        // Attempt: make choice, update target, start\n        state.append(choices[i])\n        // Proceed to the next round of selection\n        backtrack(state: &state, target: target - choices[i], choices: choices, start: i, res: &res)\n        // Backtrack: undo choice, restore to previous state\n        state.removeLast()\n    }\n}\n\n/* Solve subset sum I */\nfunc subsetSumI(nums: [Int], target: Int) -> [[Int]] {\n    var state: [Int] = [] // State (subset)\n    let nums = nums.sorted() // Sort nums\n    let start = 0 // Start point for traversal\n    var res: [[Int]] = [] // Result list (subset list)\n    backtrack(state: &state, target: target, choices: nums, start: start, res: &res)\n    return res\n}\n
      subset_sum_i.js
      /* Backtracking algorithm: Subset sum I */\nfunction backtrack(state, target, choices, start, res) {\n    // When the subset sum equals target, record the solution\n    if (target === 0) {\n        res.push([...state]);\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    for (let i = start; i < choices.length; i++) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // Attempt: make choice, update target, start\n        state.push(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop();\n    }\n}\n\n/* Solve subset sum I */\nfunction subsetSumI(nums, target) {\n    const state = []; // State (subset)\n    nums.sort((a, b) => a - b); // Sort nums\n    const start = 0; // Start point for traversal\n    const res = []; // Result list (subset list)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
      subset_sum_i.ts
      /* Backtracking algorithm: Subset sum I */\nfunction backtrack(\n    state: number[],\n    target: number,\n    choices: number[],\n    start: number,\n    res: number[][]\n): void {\n    // When the subset sum equals target, record the solution\n    if (target === 0) {\n        res.push([...state]);\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    for (let i = start; i < choices.length; i++) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // Attempt: make choice, update target, start\n        state.push(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop();\n    }\n}\n\n/* Solve subset sum I */\nfunction subsetSumI(nums: number[], target: number): number[][] {\n    const state = []; // State (subset)\n    nums.sort((a, b) => a - b); // Sort nums\n    const start = 0; // Start point for traversal\n    const res = []; // Result list (subset list)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
      subset_sum_i.dart
      /* Backtracking algorithm: Subset sum I */\nvoid backtrack(\n  List<int> state,\n  int target,\n  List<int> choices,\n  int start,\n  List<List<int>> res,\n) {\n  // When the subset sum equals target, record the solution\n  if (target == 0) {\n    res.add(List.from(state));\n    return;\n  }\n  // Traverse all choices\n  // Pruning 2: start traversing from start to avoid generating duplicate subsets\n  for (int i = start; i < choices.length; i++) {\n    // Pruning 1: if the subset sum exceeds target, end the loop directly\n    // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n    if (target - choices[i] < 0) {\n      break;\n    }\n    // Attempt: make choice, update target, start\n    state.add(choices[i]);\n    // Proceed to the next round of selection\n    backtrack(state, target - choices[i], choices, i, res);\n    // Backtrack: undo choice, restore to previous state\n    state.removeLast();\n  }\n}\n\n/* Solve subset sum I */\nList<List<int>> subsetSumI(List<int> nums, int target) {\n  List<int> state = []; // State (subset)\n  nums.sort(); // Sort nums\n  int start = 0; // Start point for traversal\n  List<List<int>> res = []; // Result list (subset list)\n  backtrack(state, target, nums, start, res);\n  return res;\n}\n
      subset_sum_i.rs
      /* Backtracking algorithm: Subset sum I */\nfn backtrack(\n    state: &mut Vec<i32>,\n    target: i32,\n    choices: &[i32],\n    start: usize,\n    res: &mut Vec<Vec<i32>>,\n) {\n    // When the subset sum equals target, record the solution\n    if target == 0 {\n        res.push(state.clone());\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    for i in start..choices.len() {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if target - choices[i] < 0 {\n            break;\n        }\n        // Attempt: make choice, update target, start\n        state.push(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop();\n    }\n}\n\n/* Solve subset sum I */\nfn subset_sum_i(nums: &mut [i32], target: i32) -> Vec<Vec<i32>> {\n    let mut state = Vec::new(); // State (subset)\n    nums.sort(); // Sort nums\n    let start = 0; // Start point for traversal\n    let mut res = Vec::new(); // Result list (subset list)\n    backtrack(&mut state, target, nums, start, &mut res);\n    res\n}\n
      subset_sum_i.c
      /* Backtracking algorithm: Subset sum I */\nvoid backtrack(int target, int *choices, int choicesSize, int start) {\n    // When the subset sum equals target, record the solution\n    if (target == 0) {\n        for (int i = 0; i < stateSize; ++i) {\n            res[resSize][i] = state[i];\n        }\n        resColSizes[resSize++] = stateSize;\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    for (int i = start; i < choicesSize; i++) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // Attempt: make choice, update target, start\n        state[stateSize] = choices[i];\n        stateSize++;\n        // Proceed to the next round of selection\n        backtrack(target - choices[i], choices, choicesSize, i);\n        // Backtrack: undo choice, restore to previous state\n        stateSize--;\n    }\n}\n\n/* Solve subset sum I */\nvoid subsetSumI(int *nums, int numsSize, int target) {\n    qsort(nums, numsSize, sizeof(int), cmp); // Sort nums\n    int start = 0;                           // Start point for traversal\n    backtrack(target, nums, numsSize, start);\n}\n
      subset_sum_i.kt
      /* Backtracking algorithm: Subset sum I */\nfun backtrack(\n    state: MutableList<Int>,\n    target: Int,\n    choices: IntArray,\n    start: Int,\n    res: MutableList<MutableList<Int>?>\n) {\n    // When the subset sum equals target, record the solution\n    if (target == 0) {\n        res.add(state.toMutableList())\n        return\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    for (i in start..<choices.size) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break\n        }\n        // Attempt: make choice, update target, start\n        state.add(choices[i])\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i, res)\n        // Backtrack: undo choice, restore to previous state\n        state.removeAt(state.size - 1)\n    }\n}\n\n/* Solve subset sum I */\nfun subsetSumI(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {\n    val state = mutableListOf<Int>() // State (subset)\n    nums.sort() // Sort nums\n    val start = 0 // Start point for traversal\n    val res = mutableListOf<MutableList<Int>?>() // Result list (subset list)\n    backtrack(state, target, nums, start, res)\n    return res\n}\n
      subset_sum_i.rb
      ### Backtracking: subset sum I ###\ndef backtrack(state, target, choices, start, res)\n  # When the subset sum equals target, record the solution\n  if target.zero?\n    res << state.dup\n    return\n  end\n  # Traverse all choices\n  # Pruning 2: start traversing from start to avoid generating duplicate subsets\n  for i in start...choices.length\n    # Pruning 1: if the subset sum exceeds target, end the loop directly\n    # This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n    break if target - choices[i] < 0\n    # Attempt: make choice, update target, start\n    state << choices[i]\n    # Proceed to the next round of selection\n    backtrack(state, target - choices[i], choices, i, res)\n    # Backtrack: undo choice, restore to previous state\n    state.pop\n  end\nend\n\n### Solve subset sum I ###\ndef subset_sum_i(nums, target)\n  state = [] # State (subset)\n  nums.sort! # Sort nums\n  start = 0 # Start point for traversal\n  res = [] # Result list (subset list)\n  backtrack(state, target, nums, start, res)\n  res\nend\n

      Figure 13-12 shows the complete backtracking process when array \\([3, 4, 5]\\) and target element \\(9\\) are input to the above code.

      Figure 13-12 \u00a0 Subset-sum I backtracking process

      "},{"location":"chapter_backtracking/subset_sum_problem/#1332-with-duplicate-elements-in-array","title":"13.3.2 \u00a0 With Duplicate Elements in Array","text":"

      Question

      Given a positive integer array nums and a target positive integer target, find all possible combinations where the sum of elements in the combination equals target. The given array may contain duplicate elements, and each element can be selected at most once. Return these combinations in list form, where the list should not contain duplicate combinations.

      Compared to the previous problem, the input array in this problem may contain duplicate elements, which introduces new challenges. For example, given array \\([4, \\hat{4}, 5]\\) and target element \\(9\\), the output of the existing code is \\([4, 5], [\\hat{4}, 5]\\), which contains duplicate subsets.

      The reason for this duplication is that equal elements are selected multiple times in a certain round. In Figure 13-13, the first round has three choices, two of which are \\(4\\), creating two duplicate search branches that output duplicate subsets. Similarly, the two \\(4\\)'s in the second round also produce duplicate subsets.

      Figure 13-13 \u00a0 Duplicate subsets caused by equal elements

      "},{"location":"chapter_backtracking/subset_sum_problem/#1-pruning-equal-elements","title":"1. \u00a0 Pruning Equal Elements","text":"

      To solve this problem, we need to limit equal elements to be selected only once in each round. The implementation is quite clever: since the array is already sorted, equal elements are adjacent. This means that in a certain round of selection, if the current element equals the element to its left, it means this element has already been selected, so we skip the current element directly.

      At the same time, this problem specifies that each array element can only be selected once. Fortunately, we can also use the variable start to satisfy this constraint: after making choice \\(x_{i}\\), set the next round to start traversal from index \\(i + 1\\) onwards. This both eliminates duplicate subsets and avoids selecting elements multiple times.

      "},{"location":"chapter_backtracking/subset_sum_problem/#2-code-implementation","title":"2. \u00a0 Code Implementation","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby subset_sum_ii.py
      def backtrack(\n    state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n    \"\"\"Backtracking algorithm: Subset sum II\"\"\"\n    # When the subset sum equals target, record the solution\n    if target == 0:\n        res.append(list(state))\n        return\n    # Traverse all choices\n    # Pruning 2: start traversing from start to avoid generating duplicate subsets\n    # Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n    for i in range(start, len(choices)):\n        # Pruning 1: if the subset sum exceeds target, end the loop directly\n        # This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if target - choices[i] < 0:\n            break\n        # Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n        if i > start and choices[i] == choices[i - 1]:\n            continue\n        # Attempt: make choice, update target, start\n        state.append(choices[i])\n        # Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i + 1, res)\n        # Backtrack: undo choice, restore to previous state\n        state.pop()\n\ndef subset_sum_ii(nums: list[int], target: int) -> list[list[int]]:\n    \"\"\"Solve subset sum II\"\"\"\n    state = []  # State (subset)\n    nums.sort()  # Sort nums\n    start = 0  # Start point for traversal\n    res = []  # Result list (subset list)\n    backtrack(state, target, nums, start, res)\n    return res\n
      subset_sum_ii.cpp
      /* Backtracking algorithm: Subset sum II */\nvoid backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {\n    // When the subset sum equals target, record the solution\n    if (target == 0) {\n        res.push_back(state);\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    // Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n    for (int i = start; i < choices.size(); i++) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // Attempt: make choice, update target, start\n        state.push_back(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop_back();\n    }\n}\n\n/* Solve subset sum II */\nvector<vector<int>> subsetSumII(vector<int> &nums, int target) {\n    vector<int> state;              // State (subset)\n    sort(nums.begin(), nums.end()); // Sort nums\n    int start = 0;                  // Start point for traversal\n    vector<vector<int>> res;        // Result list (subset list)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
      subset_sum_ii.java
      /* Backtracking algorithm: Subset sum II */\nvoid backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {\n    // When the subset sum equals target, record the solution\n    if (target == 0) {\n        res.add(new ArrayList<>(state));\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    // Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n    for (int i = start; i < choices.length; i++) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // Attempt: make choice, update target, start\n        state.add(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // Backtrack: undo choice, restore to previous state\n        state.remove(state.size() - 1);\n    }\n}\n\n/* Solve subset sum II */\nList<List<Integer>> subsetSumII(int[] nums, int target) {\n    List<Integer> state = new ArrayList<>(); // State (subset)\n    Arrays.sort(nums); // Sort nums\n    int start = 0; // Start point for traversal\n    List<List<Integer>> res = new ArrayList<>(); // Result list (subset list)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
      subset_sum_ii.cs
      /* Backtracking algorithm: Subset sum II */\nvoid Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {\n    // When the subset sum equals target, record the solution\n    if (target == 0) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    // Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n    for (int i = start; i < choices.Length; i++) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // Attempt: make choice, update target, start\n        state.Add(choices[i]);\n        // Proceed to the next round of selection\n        Backtrack(state, target - choices[i], choices, i + 1, res);\n        // Backtrack: undo choice, restore to previous state\n        state.RemoveAt(state.Count - 1);\n    }\n}\n\n/* Solve subset sum II */\nList<List<int>> SubsetSumII(int[] nums, int target) {\n    List<int> state = []; // State (subset)\n    Array.Sort(nums); // Sort nums\n    int start = 0; // Start point for traversal\n    List<List<int>> res = []; // Result list (subset list)\n    Backtrack(state, target, nums, start, res);\n    return res;\n}\n
      subset_sum_ii.go
      /* Backtracking algorithm: Subset sum II */\nfunc backtrackSubsetSumII(start, target int, state, choices *[]int, res *[][]int) {\n    // When the subset sum equals target, record the solution\n    if target == 0 {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n        return\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    // Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n    for i := start; i < len(*choices); i++ {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if target-(*choices)[i] < 0 {\n            break\n        }\n        // Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n        if i > start && (*choices)[i] == (*choices)[i-1] {\n            continue\n        }\n        // Attempt: make choice, update target, start\n        *state = append(*state, (*choices)[i])\n        // Proceed to the next round of selection\n        backtrackSubsetSumII(i+1, target-(*choices)[i], state, choices, res)\n        // Backtrack: undo choice, restore to previous state\n        *state = (*state)[:len(*state)-1]\n    }\n}\n\n/* Solve subset sum II */\nfunc subsetSumII(nums []int, target int) [][]int {\n    state := make([]int, 0) // State (subset)\n    sort.Ints(nums)         // Sort nums\n    start := 0              // Start point for traversal\n    res := make([][]int, 0) // Result list (subset list)\n    backtrackSubsetSumII(start, target, &state, &nums, &res)\n    return res\n}\n
      subset_sum_ii.swift
      /* Backtracking algorithm: Subset sum II */\nfunc backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {\n    // When the subset sum equals target, record the solution\n    if target == 0 {\n        res.append(state)\n        return\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    // Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n    for i in choices.indices.dropFirst(start) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if target - choices[i] < 0 {\n            break\n        }\n        // Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n        if i > start, choices[i] == choices[i - 1] {\n            continue\n        }\n        // Attempt: make choice, update target, start\n        state.append(choices[i])\n        // Proceed to the next round of selection\n        backtrack(state: &state, target: target - choices[i], choices: choices, start: i + 1, res: &res)\n        // Backtrack: undo choice, restore to previous state\n        state.removeLast()\n    }\n}\n\n/* Solve subset sum II */\nfunc subsetSumII(nums: [Int], target: Int) -> [[Int]] {\n    var state: [Int] = [] // State (subset)\n    let nums = nums.sorted() // Sort nums\n    let start = 0 // Start point for traversal\n    var res: [[Int]] = [] // Result list (subset list)\n    backtrack(state: &state, target: target, choices: nums, start: start, res: &res)\n    return res\n}\n
      subset_sum_ii.js
      /* Backtracking algorithm: Subset sum II */\nfunction backtrack(state, target, choices, start, res) {\n    // When the subset sum equals target, record the solution\n    if (target === 0) {\n        res.push([...state]);\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    // Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n    for (let i = start; i < choices.length; i++) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n        if (i > start && choices[i] === choices[i - 1]) {\n            continue;\n        }\n        // Attempt: make choice, update target, start\n        state.push(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop();\n    }\n}\n\n/* Solve subset sum II */\nfunction subsetSumII(nums, target) {\n    const state = []; // State (subset)\n    nums.sort((a, b) => a - b); // Sort nums\n    const start = 0; // Start point for traversal\n    const res = []; // Result list (subset list)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
      subset_sum_ii.ts
      /* Backtracking algorithm: Subset sum II */\nfunction backtrack(\n    state: number[],\n    target: number,\n    choices: number[],\n    start: number,\n    res: number[][]\n): void {\n    // When the subset sum equals target, record the solution\n    if (target === 0) {\n        res.push([...state]);\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    // Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n    for (let i = start; i < choices.length; i++) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n        if (i > start && choices[i] === choices[i - 1]) {\n            continue;\n        }\n        // Attempt: make choice, update target, start\n        state.push(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop();\n    }\n}\n\n/* Solve subset sum II */\nfunction subsetSumII(nums: number[], target: number): number[][] {\n    const state = []; // State (subset)\n    nums.sort((a, b) => a - b); // Sort nums\n    const start = 0; // Start point for traversal\n    const res = []; // Result list (subset list)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
      subset_sum_ii.dart
      /* Backtracking algorithm: Subset sum II */\nvoid backtrack(\n  List<int> state,\n  int target,\n  List<int> choices,\n  int start,\n  List<List<int>> res,\n) {\n  // When the subset sum equals target, record the solution\n  if (target == 0) {\n    res.add(List.from(state));\n    return;\n  }\n  // Traverse all choices\n  // Pruning 2: start traversing from start to avoid generating duplicate subsets\n  // Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n  for (int i = start; i < choices.length; i++) {\n    // Pruning 1: if the subset sum exceeds target, end the loop directly\n    // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n    if (target - choices[i] < 0) {\n      break;\n    }\n    // Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n    if (i > start && choices[i] == choices[i - 1]) {\n      continue;\n    }\n    // Attempt: make choice, update target, start\n    state.add(choices[i]);\n    // Proceed to the next round of selection\n    backtrack(state, target - choices[i], choices, i + 1, res);\n    // Backtrack: undo choice, restore to previous state\n    state.removeLast();\n  }\n}\n\n/* Solve subset sum II */\nList<List<int>> subsetSumII(List<int> nums, int target) {\n  List<int> state = []; // State (subset)\n  nums.sort(); // Sort nums\n  int start = 0; // Start point for traversal\n  List<List<int>> res = []; // Result list (subset list)\n  backtrack(state, target, nums, start, res);\n  return res;\n}\n
      subset_sum_ii.rs
      /* Backtracking algorithm: Subset sum II */\nfn backtrack(\n    state: &mut Vec<i32>,\n    target: i32,\n    choices: &[i32],\n    start: usize,\n    res: &mut Vec<Vec<i32>>,\n) {\n    // When the subset sum equals target, record the solution\n    if target == 0 {\n        res.push(state.clone());\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    // Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n    for i in start..choices.len() {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if target - choices[i] < 0 {\n            break;\n        }\n        // Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n        if i > start && choices[i] == choices[i - 1] {\n            continue;\n        }\n        // Attempt: make choice, update target, start\n        state.push(choices[i]);\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // Backtrack: undo choice, restore to previous state\n        state.pop();\n    }\n}\n\n/* Solve subset sum II */\nfn subset_sum_ii(nums: &mut [i32], target: i32) -> Vec<Vec<i32>> {\n    let mut state = Vec::new(); // State (subset)\n    nums.sort(); // Sort nums\n    let start = 0; // Start point for traversal\n    let mut res = Vec::new(); // Result list (subset list)\n    backtrack(&mut state, target, nums, start, &mut res);\n    res\n}\n
      subset_sum_ii.c
      /* Backtracking algorithm: Subset sum II */\nvoid backtrack(int target, int *choices, int choicesSize, int start) {\n    // When the subset sum equals target, record the solution\n    if (target == 0) {\n        for (int i = 0; i < stateSize; i++) {\n            res[resSize][i] = state[i];\n        }\n        resColSizes[resSize++] = stateSize;\n        return;\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    // Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n    for (int i = start; i < choicesSize; i++) {\n        // Pruning 1: Skip if subset sum exceeds target\n        if (target - choices[i] < 0) {\n            continue;\n        }\n        // Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // Attempt: make choice, update target, start\n        state[stateSize] = choices[i];\n        stateSize++;\n        // Proceed to the next round of selection\n        backtrack(target - choices[i], choices, choicesSize, i + 1);\n        // Backtrack: undo choice, restore to previous state\n        stateSize--;\n    }\n}\n\n/* Solve subset sum II */\nvoid subsetSumII(int *nums, int numsSize, int target) {\n    // Sort nums\n    qsort(nums, numsSize, sizeof(int), cmp);\n    // Start backtracking\n    backtrack(target, nums, numsSize, 0);\n}\n
      subset_sum_ii.kt
      /* Backtracking algorithm: Subset sum II */\nfun backtrack(\n    state: MutableList<Int>,\n    target: Int,\n    choices: IntArray,\n    start: Int,\n    res: MutableList<MutableList<Int>?>\n) {\n    // When the subset sum equals target, record the solution\n    if (target == 0) {\n        res.add(state.toMutableList())\n        return\n    }\n    // Traverse all choices\n    // Pruning 2: start traversing from start to avoid generating duplicate subsets\n    // Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n    for (i in start..<choices.size) {\n        // Pruning 1: if the subset sum exceeds target, end the loop directly\n        // This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n        if (target - choices[i] < 0) {\n            break\n        }\n        // Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue\n        }\n        // Attempt: make choice, update target, start\n        state.add(choices[i])\n        // Proceed to the next round of selection\n        backtrack(state, target - choices[i], choices, i + 1, res)\n        // Backtrack: undo choice, restore to previous state\n        state.removeAt(state.size - 1)\n    }\n}\n\n/* Solve subset sum II */\nfun subsetSumII(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {\n    val state = mutableListOf<Int>() // State (subset)\n    nums.sort() // Sort nums\n    val start = 0 // Start point for traversal\n    val res = mutableListOf<MutableList<Int>?>() // Result list (subset list)\n    backtrack(state, target, nums, start, res)\n    return res\n}\n
      subset_sum_ii.rb
      ### Backtracking: subset sum II ###\ndef backtrack(state, target, choices, start, res)\n  # When the subset sum equals target, record the solution\n  if target.zero?\n    res << state.dup\n    return\n  end\n\n  # Traverse all choices\n  # Pruning 2: start traversing from start to avoid generating duplicate subsets\n  # Pruning 3: start traversing from start to avoid repeatedly selecting the same element\n  for i in start...choices.length\n    # Pruning 1: if the subset sum exceeds target, end the loop directly\n    # This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target\n    break if target - choices[i] < 0\n    # Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly\n    next if i > start && choices[i] == choices[i - 1]\n    # Attempt: make choice, update target, start\n    state << choices[i]\n    # Proceed to the next round of selection\n    backtrack(state, target - choices[i], choices, i + 1, res)\n    # Backtrack: undo choice, restore to previous state\n    state.pop\n  end\nend\n\n### Solve subset sum II ###\ndef subset_sum_ii(nums, target)\n  state = [] # State (subset)\n  nums.sort! # Sort nums\n  start = 0 # Start point for traversal\n  res = [] # Result list (subset list)\n  backtrack(state, target, nums, start, res)\n  res\nend\n

      Figure 13-14 shows the backtracking process for array \\([4, 4, 5]\\) and target element \\(9\\), which includes four types of pruning operations. Combine the illustration with the code comments to understand the entire search process and how each pruning operation works.

      Figure 13-14 \u00a0 Subset-sum II backtracking process

      "},{"location":"chapter_backtracking/summary/","title":"13.5 \u00a0 Summary","text":""},{"location":"chapter_backtracking/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • The backtracking algorithm is fundamentally an exhaustive search method. It finds solutions that meet specified conditions by performing a depth-first traversal of the solution space. During the search process, when a solution satisfying the conditions is found, it is recorded. The search ends either after finding all solutions or when the traversal is complete.
      • The backtracking algorithm search process consists of two parts: attempting and backtracking. It tries various choices through depth-first search. When encountering situations that violate constraints, it reverts the previous choice, returns to the previous state, and continues exploring other options. Attempting and backtracking are operations in opposite directions.
      • Backtracking problems typically contain multiple constraints, which can be utilized to implement pruning operations. Pruning can terminate unnecessary search branches early, significantly improving search efficiency.
      • The backtracking algorithm is primarily used to solve search problems and constraint satisfaction problems. While combinatorial optimization problems can be solved with backtracking, there are often more efficient or better-performing solutions available.
      • The permutation problem aims to find all possible permutations of elements in a given set. We use an array to record whether each element has been selected, thereby pruning search branches that attempt to select the same element repeatedly, ensuring each element is selected exactly once.
      • In the permutation problem, if the set contains duplicate elements, the final result will contain duplicate permutations. We need to impose a constraint so that equal elements can only be selected once per round, which is typically achieved using a hash set.
      • The subset-sum problem aims to find all subsets of a given set that sum to a target value. Since the set is unordered but the search process outputs results in all orders, duplicate subsets are generated. We sort the data before backtracking and use a variable to indicate the starting point of each round's traversal, thereby pruning search branches that generate duplicate subsets.
      • For the subset-sum problem, equal elements in the array produce duplicate sets. We leverage the precondition that the array is sorted by checking whether adjacent elements are equal to implement pruning, ensuring that equal elements can only be selected once per round.
      • The \\(n\\) queens problem aims to find placements of \\(n\\) queens on an \\(n \\times n\\) chessboard such that no two queens can attack each other. The constraints of this problem include row constraints, column constraints, and main and anti-diagonal constraints. To satisfy row constraints, we adopt a row-by-row placement strategy, ensuring exactly one queen is placed in each row.
      • The handling of column constraints and diagonal constraints is similar. For column constraints, we use an array to record whether each column has a queen, thereby indicating whether a selected cell is valid. For diagonal constraints, we use two arrays to separately record whether queens exist on each main or anti-diagonal. The challenge lies in finding the row-column index pattern that characterizes cells on the same main (anti-)diagonal.
      "},{"location":"chapter_backtracking/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

      Q: How should we understand the relationship between backtracking and recursion?

      Overall, backtracking is an \"algorithm strategy\", while recursion is more like a \"tool\".

      • The backtracking algorithm is typically implemented based on recursion. However, backtracking is one application scenario of recursion and represents the application of recursion in search problems.
      • The structure of recursion embodies the \"subproblem decomposition\" problem-solving paradigm, commonly used to solve problems involving divide-and-conquer, backtracking, and dynamic programming (memoized recursion).
      "},{"location":"chapter_computational_complexity/","title":"Chapter 2. \u00a0 Complexity Analysis","text":"

      Abstract

      Complexity analysis is like a space-time guide in the vast universe of algorithms.

      It leads us to explore deeply within the two dimensions of time and space, seeking more elegant solutions.

      "},{"location":"chapter_computational_complexity/#chapter-contents","title":"Chapter contents","text":"
      • 2.1 \u00a0 Algorithm Efficiency Evaluation
      • 2.2 \u00a0 Iteration and Recursion
      • 2.3 \u00a0 Time Complexity
      • 2.4 \u00a0 Space Complexity
      • 2.5 \u00a0 Summary
      "},{"location":"chapter_computational_complexity/iteration_and_recursion/","title":"2.2 \u00a0 Iteration and Recursion","text":"

      In algorithms, repeatedly executing a task is very common and closely related to complexity analysis. Therefore, before introducing time complexity and space complexity, let's first understand how to implement repeated task execution in programs, namely the two basic program control structures: iteration and recursion.

      "},{"location":"chapter_computational_complexity/iteration_and_recursion/#221-iteration","title":"2.2.1 \u00a0 Iteration","text":"

      Iteration is a control structure for repeatedly executing a task. In iteration, a program repeatedly executes a segment of code under certain conditions until those conditions are no longer satisfied.

      "},{"location":"chapter_computational_complexity/iteration_and_recursion/#1-for-loop","title":"1. \u00a0 For Loop","text":"

      The for loop is one of the most common forms of iteration, suitable for use when the number of iterations is known in advance.

      The following function implements the summation \\(1 + 2 + \\dots + n\\) based on a for loop, with the sum result recorded using the variable res. Note that in Python, range(a, b) corresponds to a \"left-closed, right-open\" interval, with the traversal range being \\(a, a + 1, \\dots, b-1\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
      def for_loop(n: int) -> int:\n    \"\"\"for loop\"\"\"\n    res = 0\n    # Sum 1, 2, ..., n-1, n\n    for i in range(1, n + 1):\n        res += i\n    return res\n
      iteration.cpp
      /* for loop */\nint forLoop(int n) {\n    int res = 0;\n    // Sum 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; ++i) {\n        res += i;\n    }\n    return res;\n}\n
      iteration.java
      /* for loop */\nint forLoop(int n) {\n    int res = 0;\n    // Sum 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
      iteration.cs
      /* for loop */\nint ForLoop(int n) {\n    int res = 0;\n    // Sum 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
      iteration.go
      /* for loop */\nfunc forLoop(n int) int {\n    res := 0\n    // Sum 1, 2, ..., n-1, n\n    for i := 1; i <= n; i++ {\n        res += i\n    }\n    return res\n}\n
      iteration.swift
      /* for loop */\nfunc forLoop(n: Int) -> Int {\n    var res = 0\n    // Sum 1, 2, ..., n-1, n\n    for i in 1 ... n {\n        res += i\n    }\n    return res\n}\n
      iteration.js
      /* for loop */\nfunction forLoop(n) {\n    let res = 0;\n    // Sum 1, 2, ..., n-1, n\n    for (let i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
      iteration.ts
      /* for loop */\nfunction forLoop(n: number): number {\n    let res = 0;\n    // Sum 1, 2, ..., n-1, n\n    for (let i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
      iteration.dart
      /* for loop */\nint forLoop(int n) {\n  int res = 0;\n  // Sum 1, 2, ..., n-1, n\n  for (int i = 1; i <= n; i++) {\n    res += i;\n  }\n  return res;\n}\n
      iteration.rs
      /* for loop */\nfn for_loop(n: i32) -> i32 {\n    let mut res = 0;\n    // Sum 1, 2, ..., n-1, n\n    for i in 1..=n {\n        res += i;\n    }\n    res\n}\n
      iteration.c
      /* for loop */\nint forLoop(int n) {\n    int res = 0;\n    // Sum 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
      iteration.kt
      /* for loop */\nfun forLoop(n: Int): Int {\n    var res = 0\n    // Sum 1, 2, ..., n-1, n\n    for (i in 1..n) {\n        res += i\n    }\n    return res\n}\n
      iteration.rb
      ### for loop ###\ndef for_loop(n)\n  res = 0\n\n  # Sum 1, 2, ..., n-1, n\n  for i in 1..n\n    res += i\n  end\n\n  res\nend\n

      Figure 2-1 shows the flowchart of this summation function.

      Figure 2-1 \u00a0 Flowchart of the summation function

      The number of operations in this summation function is proportional to the input data size \\(n\\), or has a \"linear relationship\". In fact, time complexity describes precisely this \"linear relationship\". Related content will be introduced in detail in the next section.

      "},{"location":"chapter_computational_complexity/iteration_and_recursion/#2-while-loop","title":"2. \u00a0 While Loop","text":"

      Similar to the for loop, the while loop is also a method for implementing iteration. In a while loop, the program first checks the condition in each round; if the condition is true, it continues execution, otherwise it ends the loop.

      Below we use a while loop to implement the summation \\(1 + 2 + \\dots + n\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
      def while_loop(n: int) -> int:\n    \"\"\"while loop\"\"\"\n    res = 0\n    i = 1  # Initialize condition variable\n    # Sum 1, 2, ..., n-1, n\n    while i <= n:\n        res += i\n        i += 1  # Update condition variable\n    return res\n
      iteration.cpp
      /* while loop */\nint whileLoop(int n) {\n    int res = 0;\n    int i = 1; // Initialize condition variable\n    // Sum 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // Update condition variable\n    }\n    return res;\n}\n
      iteration.java
      /* while loop */\nint whileLoop(int n) {\n    int res = 0;\n    int i = 1; // Initialize condition variable\n    // Sum 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // Update condition variable\n    }\n    return res;\n}\n
      iteration.cs
      /* while loop */\nint WhileLoop(int n) {\n    int res = 0;\n    int i = 1; // Initialize condition variable\n    // Sum 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i += 1; // Update condition variable\n    }\n    return res;\n}\n
      iteration.go
      /* while loop */\nfunc whileLoop(n int) int {\n    res := 0\n    // Initialize condition variable\n    i := 1\n    // Sum 1, 2, ..., n-1, n\n    for i <= n {\n        res += i\n        // Update condition variable\n        i++\n    }\n    return res\n}\n
      iteration.swift
      /* while loop */\nfunc whileLoop(n: Int) -> Int {\n    var res = 0\n    var i = 1 // Initialize condition variable\n    // Sum 1, 2, ..., n-1, n\n    while i <= n {\n        res += i\n        i += 1 // Update condition variable\n    }\n    return res\n}\n
      iteration.js
      /* while loop */\nfunction whileLoop(n) {\n    let res = 0;\n    let i = 1; // Initialize condition variable\n    // Sum 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // Update condition variable\n    }\n    return res;\n}\n
      iteration.ts
      /* while loop */\nfunction whileLoop(n: number): number {\n    let res = 0;\n    let i = 1; // Initialize condition variable\n    // Sum 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // Update condition variable\n    }\n    return res;\n}\n
      iteration.dart
      /* while loop */\nint whileLoop(int n) {\n  int res = 0;\n  int i = 1; // Initialize condition variable\n  // Sum 1, 2, ..., n-1, n\n  while (i <= n) {\n    res += i;\n    i++; // Update condition variable\n  }\n  return res;\n}\n
      iteration.rs
      /* while loop */\nfn while_loop(n: i32) -> i32 {\n    let mut res = 0;\n    let mut i = 1; // Initialize condition variable\n\n    // Sum 1, 2, ..., n-1, n\n    while i <= n {\n        res += i;\n        i += 1; // Update condition variable\n    }\n    res\n}\n
      iteration.c
      /* while loop */\nint whileLoop(int n) {\n    int res = 0;\n    int i = 1; // Initialize condition variable\n    // Sum 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // Update condition variable\n    }\n    return res;\n}\n
      iteration.kt
      /* while loop */\nfun whileLoop(n: Int): Int {\n    var res = 0\n    var i = 1 // Initialize condition variable\n    // Sum 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i\n        i++ // Update condition variable\n    }\n    return res\n}\n
      iteration.rb
      ### while loop ###\ndef while_loop(n)\n  res = 0\n  i = 1 # Initialize condition variable\n\n  # Sum 1, 2, ..., n-1, n\n  while i <= n\n    res += i\n    i += 1 # Update condition variable\n  end\n\n  res\nend\n

      The while loop has greater flexibility than the for loop. In a while loop, we can freely design the initialization and update steps of the condition variable.

      For example, in the following code, the condition variable \\(i\\) is updated twice per round, which is not convenient to implement using a for loop:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
      def while_loop_ii(n: int) -> int:\n    \"\"\"while loop (two updates)\"\"\"\n    res = 0\n    i = 1  # Initialize condition variable\n    # Sum 1, 4, 10, ...\n    while i <= n:\n        res += i\n        # Update condition variable\n        i += 1\n        i *= 2\n    return res\n
      iteration.cpp
      /* while loop (two updates) */\nint whileLoopII(int n) {\n    int res = 0;\n    int i = 1; // Initialize condition variable\n    // Sum 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // Update condition variable\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
      iteration.java
      /* while loop (two updates) */\nint whileLoopII(int n) {\n    int res = 0;\n    int i = 1; // Initialize condition variable\n    // Sum 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // Update condition variable\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
      iteration.cs
      /* while loop (two updates) */\nint WhileLoopII(int n) {\n    int res = 0;\n    int i = 1; // Initialize condition variable\n    // Sum 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // Update condition variable\n        i += 1; \n        i *= 2;\n    }\n    return res;\n}\n
      iteration.go
      /* while loop (two updates) */\nfunc whileLoopII(n int) int {\n    res := 0\n    // Initialize condition variable\n    i := 1\n    // Sum 1, 4, 10, ...\n    for i <= n {\n        res += i\n        // Update condition variable\n        i++\n        i *= 2\n    }\n    return res\n}\n
      iteration.swift
      /* while loop (two updates) */\nfunc whileLoopII(n: Int) -> Int {\n    var res = 0\n    var i = 1 // Initialize condition variable\n    // Sum 1, 4, 10, ...\n    while i <= n {\n        res += i\n        // Update condition variable\n        i += 1\n        i *= 2\n    }\n    return res\n}\n
      iteration.js
      /* while loop (two updates) */\nfunction whileLoopII(n) {\n    let res = 0;\n    let i = 1; // Initialize condition variable\n    // Sum 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // Update condition variable\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
      iteration.ts
      /* while loop (two updates) */\nfunction whileLoopII(n: number): number {\n    let res = 0;\n    let i = 1; // Initialize condition variable\n    // Sum 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // Update condition variable\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
      iteration.dart
      /* while loop (two updates) */\nint whileLoopII(int n) {\n  int res = 0;\n  int i = 1; // Initialize condition variable\n  // Sum 1, 4, 10, ...\n  while (i <= n) {\n    res += i;\n    // Update condition variable\n    i++;\n    i *= 2;\n  }\n  return res;\n}\n
      iteration.rs
      /* while loop (two updates) */\nfn while_loop_ii(n: i32) -> i32 {\n    let mut res = 0;\n    let mut i = 1; // Initialize condition variable\n\n    // Sum 1, 4, 10, ...\n    while i <= n {\n        res += i;\n        // Update condition variable\n        i += 1;\n        i *= 2;\n    }\n    res\n}\n
      iteration.c
      /* while loop (two updates) */\nint whileLoopII(int n) {\n    int res = 0;\n    int i = 1; // Initialize condition variable\n    // Sum 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // Update condition variable\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
      iteration.kt
      /* while loop (two updates) */\nfun whileLoopII(n: Int): Int {\n    var res = 0\n    var i = 1 // Initialize condition variable\n    // Sum 1, 4, 10, ...\n    while (i <= n) {\n        res += i\n        // Update condition variable\n        i++\n        i *= 2\n    }\n    return res\n}\n
      iteration.rb
      ### while loop (two updates) ###\ndef while_loop_ii(n)\n  res = 0\n  i = 1 # Initialize condition variable\n\n  # Sum 1, 4, 10, ...\n  while i <= n\n    res += i\n    # Update condition variable\n    i += 1\n    i *= 2\n  end\n\n  res\nend\n

      Overall, for loops have more compact code, while while loops are more flexible; both can implement iterative structures. The choice of which to use should be determined based on the requirements of the specific problem.

      "},{"location":"chapter_computational_complexity/iteration_and_recursion/#3-nested-loops","title":"3. \u00a0 Nested Loops","text":"

      We can nest one loop structure inside another. Below is an example using for loops:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
      def nested_for_loop(n: int) -> str:\n    \"\"\"Nested for loop\"\"\"\n    res = \"\"\n    # Loop i = 1, 2, ..., n-1, n\n    for i in range(1, n + 1):\n        # Loop j = 1, 2, ..., n-1, n\n        for j in range(1, n + 1):\n            res += f\"({i}, {j}), \"\n    return res\n
      iteration.cpp
      /* Nested for loop */\nstring nestedForLoop(int n) {\n    ostringstream res;\n    // Loop i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; ++i) {\n        // Loop j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; ++j) {\n            res << \"(\" << i << \", \" << j << \"), \";\n        }\n    }\n    return res.str();\n}\n
      iteration.java
      /* Nested for loop */\nString nestedForLoop(int n) {\n    StringBuilder res = new StringBuilder();\n    // Loop i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        // Loop j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; j++) {\n            res.append(\"(\" + i + \", \" + j + \"), \");\n        }\n    }\n    return res.toString();\n}\n
      iteration.cs
      /* Nested for loop */\nstring NestedForLoop(int n) {\n    StringBuilder res = new();\n    // Loop i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        // Loop j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; j++) {\n            res.Append($\"({i}, {j}), \");\n        }\n    }\n    return res.ToString();\n}\n
      iteration.go
      /* Nested for loop */\nfunc nestedForLoop(n int) string {\n    res := \"\"\n    // Loop i = 1, 2, ..., n-1, n\n    for i := 1; i <= n; i++ {\n        for j := 1; j <= n; j++ {\n            // Loop j = 1, 2, ..., n-1, n\n            res += fmt.Sprintf(\"(%d, %d), \", i, j)\n        }\n    }\n    return res\n}\n
      iteration.swift
      /* Nested for loop */\nfunc nestedForLoop(n: Int) -> String {\n    var res = \"\"\n    // Loop i = 1, 2, ..., n-1, n\n    for i in 1 ... n {\n        // Loop j = 1, 2, ..., n-1, n\n        for j in 1 ... n {\n            res.append(\"(\\(i), \\(j)), \")\n        }\n    }\n    return res\n}\n
      iteration.js
      /* Nested for loop */\nfunction nestedForLoop(n) {\n    let res = '';\n    // Loop i = 1, 2, ..., n-1, n\n    for (let i = 1; i <= n; i++) {\n        // Loop j = 1, 2, ..., n-1, n\n        for (let j = 1; j <= n; j++) {\n            res += `(${i}, ${j}), `;\n        }\n    }\n    return res;\n}\n
      iteration.ts
      /* Nested for loop */\nfunction nestedForLoop(n: number): string {\n    let res = '';\n    // Loop i = 1, 2, ..., n-1, n\n    for (let i = 1; i <= n; i++) {\n        // Loop j = 1, 2, ..., n-1, n\n        for (let j = 1; j <= n; j++) {\n            res += `(${i}, ${j}), `;\n        }\n    }\n    return res;\n}\n
      iteration.dart
      /* Nested for loop */\nString nestedForLoop(int n) {\n  String res = \"\";\n  // Loop i = 1, 2, ..., n-1, n\n  for (int i = 1; i <= n; i++) {\n    // Loop j = 1, 2, ..., n-1, n\n    for (int j = 1; j <= n; j++) {\n      res += \"($i, $j), \";\n    }\n  }\n  return res;\n}\n
      iteration.rs
      /* Nested for loop */\nfn nested_for_loop(n: i32) -> String {\n    let mut res = vec![];\n    // Loop i = 1, 2, ..., n-1, n\n    for i in 1..=n {\n        // Loop j = 1, 2, ..., n-1, n\n        for j in 1..=n {\n            res.push(format!(\"({}, {}), \", i, j));\n        }\n    }\n    res.join(\"\")\n}\n
      iteration.c
      /* Nested for loop */\nchar *nestedForLoop(int n) {\n    // n * n is the number of points, \"(i, j), \" string max length is 6+10*2, plus extra space for null character \\0\n    int size = n * n * 26 + 1;\n    char *res = malloc(size * sizeof(char));\n    // Loop i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        // Loop j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; j++) {\n            char tmp[26];\n            snprintf(tmp, sizeof(tmp), \"(%d, %d), \", i, j);\n            strncat(res, tmp, size - strlen(res) - 1);\n        }\n    }\n    return res;\n}\n
      iteration.kt
      /* Nested for loop */\nfun nestedForLoop(n: Int): String {\n    val res = StringBuilder()\n    // Loop i = 1, 2, ..., n-1, n\n    for (i in 1..n) {\n        // Loop j = 1, 2, ..., n-1, n\n        for (j in 1..n) {\n            res.append(\" ($i, $j), \")\n        }\n    }\n    return res.toString()\n}\n
      iteration.rb
      ### Nested for loop ###\ndef nested_for_loop(n)\n  res = \"\"\n\n  # Loop i = 1, 2, ..., n-1, n\n  for i in 1..n\n    # Loop j = 1, 2, ..., n-1, n\n    for j in 1..n\n      res += \"(#{i}, #{j}), \"\n    end\n  end\n\n  res\nend\n

      Figure 2-2 shows the flowchart of this nested loop.

      Figure 2-2 \u00a0 Flowchart of nested loops

      In this case, the number of operations of the function is proportional to \\(n^2\\), or the algorithm's running time has a \"quadratic relationship\" with the input data size \\(n\\).

      We can continue adding nested loops, where each nesting is a \"dimension increase\", raising the time complexity to \"cubic relationship\", \"quartic relationship\", and so on.

      "},{"location":"chapter_computational_complexity/iteration_and_recursion/#222-recursion","title":"2.2.2 \u00a0 Recursion","text":"

      Recursion is an algorithmic strategy that solves problems by having a function call itself. It mainly consists of two phases.

      1. Descend: The program continuously calls itself deeper, usually passing in smaller or more simplified parameters, until reaching a \"termination condition\".
      2. Ascend: After triggering the \"termination condition\", the program returns layer by layer from the deepest recursive function, aggregating the result of each layer.

      From an implementation perspective, recursive code mainly consists of three elements.

      1. Termination condition: Used to determine when to switch from \"descending\" to \"ascending\".
      2. Recursive call: Corresponds to \"descending\", where the function calls itself, usually with smaller or more simplified parameters.
      3. Return result: Corresponds to \"ascending\", returning the result of the current recursion level to the previous layer.

      Observe the following code. We only need to call the function recur(n) to complete the calculation of \\(1 + 2 + \\dots + n\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
      def recur(n: int) -> int:\n    \"\"\"Recursion\"\"\"\n    # Termination condition\n    if n == 1:\n        return 1\n    # Recurse: recursive call\n    res = recur(n - 1)\n    # Return: return result\n    return n + res\n
      recursion.cpp
      /* Recursion */\nint recur(int n) {\n    // Termination condition\n    if (n == 1)\n        return 1;\n    // Recurse: recursive call\n    int res = recur(n - 1);\n    // Return: return result\n    return n + res;\n}\n
      recursion.java
      /* Recursion */\nint recur(int n) {\n    // Termination condition\n    if (n == 1)\n        return 1;\n    // Recurse: recursive call\n    int res = recur(n - 1);\n    // Return: return result\n    return n + res;\n}\n
      recursion.cs
      /* Recursion */\nint Recur(int n) {\n    // Termination condition\n    if (n == 1)\n        return 1;\n    // Recurse: recursive call\n    int res = Recur(n - 1);\n    // Return: return result\n    return n + res;\n}\n
      recursion.go
      /* Recursion */\nfunc recur(n int) int {\n    // Termination condition\n    if n == 1 {\n        return 1\n    }\n    // Recurse: recursive call\n    res := recur(n - 1)\n    // Return: return result\n    return n + res\n}\n
      recursion.swift
      /* Recursion */\nfunc recur(n: Int) -> Int {\n    // Termination condition\n    if n == 1 {\n        return 1\n    }\n    // Recurse: recursive call\n    let res = recur(n: n - 1)\n    // Return: return result\n    return n + res\n}\n
      recursion.js
      /* Recursion */\nfunction recur(n) {\n    // Termination condition\n    if (n === 1) return 1;\n    // Recurse: recursive call\n    const res = recur(n - 1);\n    // Return: return result\n    return n + res;\n}\n
      recursion.ts
      /* Recursion */\nfunction recur(n: number): number {\n    // Termination condition\n    if (n === 1) return 1;\n    // Recurse: recursive call\n    const res = recur(n - 1);\n    // Return: return result\n    return n + res;\n}\n
      recursion.dart
      /* Recursion */\nint recur(int n) {\n  // Termination condition\n  if (n == 1) return 1;\n  // Recurse: recursive call\n  int res = recur(n - 1);\n  // Return: return result\n  return n + res;\n}\n
      recursion.rs
      /* Recursion */\nfn recur(n: i32) -> i32 {\n    // Termination condition\n    if n == 1 {\n        return 1;\n    }\n    // Recurse: recursive call\n    let res = recur(n - 1);\n    // Return: return result\n    n + res\n}\n
      recursion.c
      /* Recursion */\nint recur(int n) {\n    // Termination condition\n    if (n == 1)\n        return 1;\n    // Recurse: recursive call\n    int res = recur(n - 1);\n    // Return: return result\n    return n + res;\n}\n
      recursion.kt
      /* Recursion */\nfun recur(n: Int): Int {\n    // Termination condition\n    if (n == 1)\n        return 1\n    // Descend: recursive call\n    val res = recur(n - 1)\n    // Return: return result\n    return n + res\n}\n
      recursion.rb
      ### Recursion ###\ndef recur(n)\n  # Termination condition\n  return 1 if n == 1\n  # Recurse: recursive call\n  res = recur(n - 1)\n  # Return: return result\n  n + res\nend\n

      Figure 2-3 shows the recursive process of this function.

      Figure 2-3 \u00a0 Recursive process of the summation function

      Although from a computational perspective, iteration and recursion can achieve the same results, they represent two completely different paradigms for thinking about and solving problems.

      • Iteration: Solves problems \"bottom-up\". Starting from the most basic steps, these steps are then repeatedly executed or accumulated until the task is complete.
      • Recursion: Solves problems \"top-down\". The original problem is decomposed into smaller subproblems that have the same form as the original problem. These subproblems continue to be decomposed into even smaller subproblems until reaching the base case (where the solution is known).

      Taking the above summation function as an example, let the problem be \\(f(n) = 1 + 2 + \\dots + n\\).

      • Iteration: Simulates the summation process in a loop, traversing from \\(1\\) to \\(n\\), performing the summation operation in each round to obtain \\(f(n)\\).
      • Recursion: Decomposes the problem into the subproblem \\(f(n) = n + f(n-1)\\), continuously decomposing (recursively) until terminating at the base case \\(f(1) = 1\\).
      "},{"location":"chapter_computational_complexity/iteration_and_recursion/#1-call-stack","title":"1. \u00a0 Call Stack","text":"

      Each time a recursive function calls itself, the system allocates memory for the newly opened function to store local variables, call addresses, and other information. This leads to two consequences.

      • The function's context data is stored in a memory area called \"stack frame space\", which is not released until the function returns. Therefore, recursion usually consumes more memory space than iteration.
      • Recursive function calls incur additional overhead. Therefore, recursion is usually less time-efficient than loops.

      As shown in Figure 2-4, before the termination condition is triggered, there are \\(n\\) unreturned recursive functions existing simultaneously, with a recursion depth of \\(n\\).

      Figure 2-4 \u00a0 Recursion call depth

      In practice, the recursion depth allowed by programming languages is usually limited, and excessively deep recursion may lead to stack overflow errors.

      "},{"location":"chapter_computational_complexity/iteration_and_recursion/#2-tail-recursion","title":"2. \u00a0 Tail Recursion","text":"

      Interestingly, if a function makes the recursive call as the very last step before returning, the function can be optimized by the compiler or interpreter to have space efficiency comparable to iteration. This case is called tail recursion.

      • Regular recursion: When a function returns to the previous level, it needs to continue executing code, so the system needs to save the context of the previous layer's call.
      • Tail recursion: The recursive call is the last operation before the function returns, meaning that after returning to the previous level, there is no need to continue executing other operations, so the system does not need to save the context of the previous layer's function.

      Taking the calculation of \\(1 + 2 + \\dots + n\\) as an example, we can set the result variable res as a function parameter to implement tail recursion:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
      def tail_recur(n, res):\n    \"\"\"Tail recursion\"\"\"\n    # Termination condition\n    if n == 0:\n        return res\n    # Tail recursive call\n    return tail_recur(n - 1, res + n)\n
      recursion.cpp
      /* Tail recursion */\nint tailRecur(int n, int res) {\n    // Termination condition\n    if (n == 0)\n        return res;\n    // Tail recursive call\n    return tailRecur(n - 1, res + n);\n}\n
      recursion.java
      /* Tail recursion */\nint tailRecur(int n, int res) {\n    // Termination condition\n    if (n == 0)\n        return res;\n    // Tail recursive call\n    return tailRecur(n - 1, res + n);\n}\n
      recursion.cs
      /* Tail recursion */\nint TailRecur(int n, int res) {\n    // Termination condition\n    if (n == 0)\n        return res;\n    // Tail recursive call\n    return TailRecur(n - 1, res + n);\n}\n
      recursion.go
      /* Tail recursion */\nfunc tailRecur(n int, res int) int {\n    // Termination condition\n    if n == 0 {\n        return res\n    }\n    // Tail recursive call\n    return tailRecur(n-1, res+n)\n}\n
      recursion.swift
      /* Tail recursion */\nfunc tailRecur(n: Int, res: Int) -> Int {\n    // Termination condition\n    if n == 0 {\n        return res\n    }\n    // Tail recursive call\n    return tailRecur(n: n - 1, res: res + n)\n}\n
      recursion.js
      /* Tail recursion */\nfunction tailRecur(n, res) {\n    // Termination condition\n    if (n === 0) return res;\n    // Tail recursive call\n    return tailRecur(n - 1, res + n);\n}\n
      recursion.ts
      /* Tail recursion */\nfunction tailRecur(n: number, res: number): number {\n    // Termination condition\n    if (n === 0) return res;\n    // Tail recursive call\n    return tailRecur(n - 1, res + n);\n}\n
      recursion.dart
      /* Tail recursion */\nint tailRecur(int n, int res) {\n  // Termination condition\n  if (n == 0) return res;\n  // Tail recursive call\n  return tailRecur(n - 1, res + n);\n}\n
      recursion.rs
      /* Tail recursion */\nfn tail_recur(n: i32, res: i32) -> i32 {\n    // Termination condition\n    if n == 0 {\n        return res;\n    }\n    // Tail recursive call\n    tail_recur(n - 1, res + n)\n}\n
      recursion.c
      /* Tail recursion */\nint tailRecur(int n, int res) {\n    // Termination condition\n    if (n == 0)\n        return res;\n    // Tail recursive call\n    return tailRecur(n - 1, res + n);\n}\n
      recursion.kt
      /* Tail recursion */\ntailrec fun tailRecur(n: Int, res: Int): Int {\n    // Add tailrec keyword to enable tail recursion optimization\n    // Termination condition\n    if (n == 0)\n        return res\n    // Tail recursive call\n    return tailRecur(n - 1, res + n)\n}\n
      recursion.rb
      ### Tail recursion ###\ndef tail_recur(n, res)\n  # Termination condition\n  return res if n == 0\n  # Tail recursive call\n  tail_recur(n - 1, res + n)\nend\n

      The execution process of tail recursion is shown in Figure 2-5. Comparing regular recursion and tail recursion, the execution point of the summation operation is different.

      • Regular recursion: The summation operation is performed during the \"ascending\" process, requiring an additional summation operation after each layer returns.
      • Tail recursion: The summation operation is performed during the \"descending\" process; the \"ascending\" process only needs to return layer by layer.

      Figure 2-5 \u00a0 Tail recursion process

      Tip

      Please note that many compilers or interpreters do not support tail recursion optimization. For example, Python does not support tail recursion optimization by default, so even if a function is in tail recursive form, it may still encounter stack overflow issues.

      "},{"location":"chapter_computational_complexity/iteration_and_recursion/#3-recursion-tree","title":"3. \u00a0 Recursion Tree","text":"

      When dealing with algorithmic problems related to \"divide and conquer\", recursion often provides a more intuitive approach and more readable code than iteration. Taking the \"Fibonacci sequence\" as an example.

      Question

      Given a Fibonacci sequence \\(0, 1, 1, 2, 3, 5, 8, 13, \\dots\\), find the \\(n\\)-th number in the sequence.

      Let the \\(n\\)-th number of the Fibonacci sequence be \\(f(n)\\). Two conclusions can be easily obtained.

      • The first two numbers of the sequence are \\(f(1) = 0\\) and \\(f(2) = 1\\).
      • Each number in the sequence is the sum of the previous two numbers, i.e., \\(f(n) = f(n - 1) + f(n - 2)\\).

      Following the recurrence relation to make recursive calls, with the first two numbers as termination conditions, we can write the recursive code. Calling fib(n) will give us the \\(n\\)-th number of the Fibonacci sequence:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
      def fib(n: int) -> int:\n    \"\"\"Fibonacci sequence: recursion\"\"\"\n    # Termination condition f(1) = 0, f(2) = 1\n    if n == 1 or n == 2:\n        return n - 1\n    # Recursive call f(n) = f(n-1) + f(n-2)\n    res = fib(n - 1) + fib(n - 2)\n    # Return result f(n)\n    return res\n
      recursion.cpp
      /* Fibonacci sequence: recursion */\nint fib(int n) {\n    // Termination condition f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // Recursive call f(n) = f(n-1) + f(n-2)\n    int res = fib(n - 1) + fib(n - 2);\n    // Return result f(n)\n    return res;\n}\n
      recursion.java
      /* Fibonacci sequence: recursion */\nint fib(int n) {\n    // Termination condition f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // Recursive call f(n) = f(n-1) + f(n-2)\n    int res = fib(n - 1) + fib(n - 2);\n    // Return result f(n)\n    return res;\n}\n
      recursion.cs
      /* Fibonacci sequence: recursion */\nint Fib(int n) {\n    // Termination condition f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // Recursive call f(n) = f(n-1) + f(n-2)\n    int res = Fib(n - 1) + Fib(n - 2);\n    // Return result f(n)\n    return res;\n}\n
      recursion.go
      /* Fibonacci sequence: recursion */\nfunc fib(n int) int {\n    // Termination condition f(1) = 0, f(2) = 1\n    if n == 1 || n == 2 {\n        return n - 1\n    }\n    // Recursive call f(n) = f(n-1) + f(n-2)\n    res := fib(n-1) + fib(n-2)\n    // Return result f(n)\n    return res\n}\n
      recursion.swift
      /* Fibonacci sequence: recursion */\nfunc fib(n: Int) -> Int {\n    // Termination condition f(1) = 0, f(2) = 1\n    if n == 1 || n == 2 {\n        return n - 1\n    }\n    // Recursive call f(n) = f(n-1) + f(n-2)\n    let res = fib(n: n - 1) + fib(n: n - 2)\n    // Return result f(n)\n    return res\n}\n
      recursion.js
      /* Fibonacci sequence: recursion */\nfunction fib(n) {\n    // Termination condition f(1) = 0, f(2) = 1\n    if (n === 1 || n === 2) return n - 1;\n    // Recursive call f(n) = f(n-1) + f(n-2)\n    const res = fib(n - 1) + fib(n - 2);\n    // Return result f(n)\n    return res;\n}\n
      recursion.ts
      /* Fibonacci sequence: recursion */\nfunction fib(n: number): number {\n    // Termination condition f(1) = 0, f(2) = 1\n    if (n === 1 || n === 2) return n - 1;\n    // Recursive call f(n) = f(n-1) + f(n-2)\n    const res = fib(n - 1) + fib(n - 2);\n    // Return result f(n)\n    return res;\n}\n
      recursion.dart
      /* Fibonacci sequence: recursion */\nint fib(int n) {\n  // Termination condition f(1) = 0, f(2) = 1\n  if (n == 1 || n == 2) return n - 1;\n  // Recursive call f(n) = f(n-1) + f(n-2)\n  int res = fib(n - 1) + fib(n - 2);\n  // Return result f(n)\n  return res;\n}\n
      recursion.rs
      /* Fibonacci sequence: recursion */\nfn fib(n: i32) -> i32 {\n    // Termination condition f(1) = 0, f(2) = 1\n    if n == 1 || n == 2 {\n        return n - 1;\n    }\n    // Recursive call f(n) = f(n-1) + f(n-2)\n    let res = fib(n - 1) + fib(n - 2);\n    // Return result\n    res\n}\n
      recursion.c
      /* Fibonacci sequence: recursion */\nint fib(int n) {\n    // Termination condition f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // Recursive call f(n) = f(n-1) + f(n-2)\n    int res = fib(n - 1) + fib(n - 2);\n    // Return result f(n)\n    return res;\n}\n
      recursion.kt
      /* Fibonacci sequence: recursion */\nfun fib(n: Int): Int {\n    // Termination condition f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1\n    // Recursive call f(n) = f(n-1) + f(n-2)\n    val res = fib(n - 1) + fib(n - 2)\n    // Return result f(n)\n    return res\n}\n
      recursion.rb
      ### Fibonacci sequence: recursion ###\ndef fib(n)\n  # Termination condition f(1) = 0, f(2) = 1\n  return n - 1 if n == 1 || n == 2\n  # Recursive call f(n) = f(n-1) + f(n-2)\n  res = fib(n - 1) + fib(n - 2)\n  # Return result f(n)\n  res\nend\n

      Observing the above code, we recursively call two functions within the function, meaning that one call produces two call branches. As shown in Figure 2-6, such continuous recursive calling will eventually produce a recursion tree with \\(n\\) levels.

      Figure 2-6 \u00a0 Recursion tree of the Fibonacci sequence

      Fundamentally, recursion embodies the paradigm of \"decomposing a problem into smaller subproblems\", and this divide-and-conquer strategy is crucial.

      • From an algorithmic perspective, many important algorithmic strategies such as searching, sorting, backtracking, divide and conquer, and dynamic programming directly or indirectly apply this way of thinking.
      • From a data structure perspective, recursion is naturally suited for handling problems related to linked lists, trees, and graphs, because they are well-suited for analysis using divide-and-conquer thinking.
      "},{"location":"chapter_computational_complexity/iteration_and_recursion/#223-comparison-of-the-two","title":"2.2.3 \u00a0 Comparison of the Two","text":"

      Summarizing the above content, as shown in Table 2-1, iteration and recursion differ in implementation, performance, and applicability.

      Table 2-1 \u00a0 Comparison of iteration and recursion characteristics

      Iteration Recursion Implementation Loop structure Function calls itself Time efficiency Generally more efficient, no function call overhead Each function call incurs overhead Memory usage Usually uses a fixed amount of memory space Accumulated function calls may use a large amount of stack frame space Suitable problems Suitable for simple loop tasks, with intuitive and readable code Suitable for subproblem decomposition, such as trees, graphs, divide and conquer, backtracking, etc., with concise and clear code structure

      Tip

      If you find the following content difficult to understand, you can review it after reading the \"Stack\" chapter.

      What is the intrinsic relationship between iteration and recursion? Taking the above recursive function as an example, the summation operation is performed during the \"ascending\" phase of recursion. This means that the function called first actually completes its summation operation last, and this working mechanism is similar to the \"last-in, first-out\" principle of stacks.

      In fact, recursive terminology such as \"call stack\" and \"stack frame space\" already hints at the close relationship between recursion and stacks.

      1. Descend: When a function is called, the system allocates a new stack frame on the \"call stack\" for that function to store the function's local variables, parameters, return address, and other data.
      2. Ascend: When the function completes execution and returns, the corresponding stack frame is removed from the \"call stack\", restoring the execution environment of the previous function.

      Therefore, we can use an explicit stack to simulate the behavior of the call stack, thus transforming recursion into iterative form:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
      def for_loop_recur(n: int) -> int:\n    \"\"\"Simulate recursion using iteration\"\"\"\n    # Use an explicit stack to simulate the system call stack\n    stack = []\n    res = 0\n    # Recurse: recursive call\n    for i in range(n, 0, -1):\n        # Simulate \"recurse\" with \"push\"\n        stack.append(i)\n    # Return: return result\n    while stack:\n        # Simulate \"return\" with \"pop\"\n        res += stack.pop()\n    # res = 1+2+3+...+n\n    return res\n
      recursion.cpp
      /* Simulate recursion using iteration */\nint forLoopRecur(int n) {\n    // Use an explicit stack to simulate the system call stack\n    stack<int> stack;\n    int res = 0;\n    // Recurse: recursive call\n    for (int i = n; i > 0; i--) {\n        // Simulate \"recurse\" with \"push\"\n        stack.push(i);\n    }\n    // Return: return result\n    while (!stack.empty()) {\n        // Simulate \"return\" with \"pop\"\n        res += stack.top();\n        stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
      recursion.java
      /* Simulate recursion using iteration */\nint forLoopRecur(int n) {\n    // Use an explicit stack to simulate the system call stack\n    Stack<Integer> stack = new Stack<>();\n    int res = 0;\n    // Recurse: recursive call\n    for (int i = n; i > 0; i--) {\n        // Simulate \"recurse\" with \"push\"\n        stack.push(i);\n    }\n    // Return: return result\n    while (!stack.isEmpty()) {\n        // Simulate \"return\" with \"pop\"\n        res += stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
      recursion.cs
      /* Simulate recursion using iteration */\nint ForLoopRecur(int n) {\n    // Use an explicit stack to simulate the system call stack\n    Stack<int> stack = new();\n    int res = 0;\n    // Recurse: recursive call\n    for (int i = n; i > 0; i--) {\n        // Simulate \"recurse\" with \"push\"\n        stack.Push(i);\n    }\n    // Return: return result\n    while (stack.Count > 0) {\n        // Simulate \"return\" with \"pop\"\n        res += stack.Pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
      recursion.go
      /* Simulate recursion using iteration */\nfunc forLoopRecur(n int) int {\n    // Use an explicit stack to simulate the system call stack\n    stack := list.New()\n    res := 0\n    // Recurse: recursive call\n    for i := n; i > 0; i-- {\n        // Simulate \"recurse\" with \"push\"\n        stack.PushBack(i)\n    }\n    // Return: return result\n    for stack.Len() != 0 {\n        // Simulate \"return\" with \"pop\"\n        res += stack.Back().Value.(int)\n        stack.Remove(stack.Back())\n    }\n    // res = 1+2+3+...+n\n    return res\n}\n
      recursion.swift
      /* Simulate recursion using iteration */\nfunc forLoopRecur(n: Int) -> Int {\n    // Use an explicit stack to simulate the system call stack\n    var stack: [Int] = []\n    var res = 0\n    // Recurse: recursive call\n    for i in (1 ... n).reversed() {\n        // Simulate \"recurse\" with \"push\"\n        stack.append(i)\n    }\n    // Return: return result\n    while !stack.isEmpty {\n        // Simulate \"return\" with \"pop\"\n        res += stack.removeLast()\n    }\n    // res = 1+2+3+...+n\n    return res\n}\n
      recursion.js
      /* Simulate recursion using iteration */\nfunction forLoopRecur(n) {\n    // Use an explicit stack to simulate the system call stack\n    const stack = [];\n    let res = 0;\n    // Recurse: recursive call\n    for (let i = n; i > 0; i--) {\n        // Simulate \"recurse\" with \"push\"\n        stack.push(i);\n    }\n    // Return: return result\n    while (stack.length) {\n        // Simulate \"return\" with \"pop\"\n        res += stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
      recursion.ts
      /* Simulate recursion using iteration */\nfunction forLoopRecur(n: number): number {\n    // Use an explicit stack to simulate the system call stack\n    const stack: number[] = [];\n    let res: number = 0;\n    // Recurse: recursive call\n    for (let i = n; i > 0; i--) {\n        // Simulate \"recurse\" with \"push\"\n        stack.push(i);\n    }\n    // Return: return result\n    while (stack.length) {\n        // Simulate \"return\" with \"pop\"\n        res += stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
      recursion.dart
      /* Simulate recursion using iteration */\nint forLoopRecur(int n) {\n  // Use an explicit stack to simulate the system call stack\n  List<int> stack = [];\n  int res = 0;\n  // Recurse: recursive call\n  for (int i = n; i > 0; i--) {\n    // Simulate \"recurse\" with \"push\"\n    stack.add(i);\n  }\n  // Return: return result\n  while (!stack.isEmpty) {\n    // Simulate \"return\" with \"pop\"\n    res += stack.removeLast();\n  }\n  // res = 1+2+3+...+n\n  return res;\n}\n
      recursion.rs
      /* Simulate recursion using iteration */\nfn for_loop_recur(n: i32) -> i32 {\n    // Use an explicit stack to simulate the system call stack\n    let mut stack = Vec::new();\n    let mut res = 0;\n    // Recurse: recursive call\n    for i in (1..=n).rev() {\n        // Simulate \"recurse\" with \"push\"\n        stack.push(i);\n    }\n    // Return: return result\n    while !stack.is_empty() {\n        // Simulate \"return\" with \"pop\"\n        res += stack.pop().unwrap();\n    }\n    // res = 1+2+3+...+n\n    res\n}\n
      recursion.c
      /* Simulate recursion using iteration */\nint forLoopRecur(int n) {\n    int stack[1000]; // Use a large array to simulate stack\n    int top = -1;    // Stack top index\n    int res = 0;\n    // Recurse: recursive call\n    for (int i = n; i > 0; i--) {\n        // Simulate \"recurse\" with \"push\"\n        stack[1 + top++] = i;\n    }\n    // Return: return result\n    while (top >= 0) {\n        // Simulate \"return\" with \"pop\"\n        res += stack[top--];\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
      recursion.kt
      /* Simulate recursion using iteration */\nfun forLoopRecur(n: Int): Int {\n    // Use an explicit stack to simulate the system call stack\n    val stack = Stack<Int>()\n    var res = 0\n    // Descend: recursive call\n    for (i in n downTo 0) {\n        // Simulate \"recurse\" with \"push\"\n        stack.push(i)\n    }\n    // Return: return result\n    while (stack.isNotEmpty()) {\n        // Simulate \"return\" with \"pop\"\n        res += stack.pop()\n    }\n    // res = 1+2+3+...+n\n    return res\n}\n
      recursion.rb
      ### Use iteration to simulate recursion ###\ndef for_loop_recur(n)\n  # Use an explicit stack to simulate the system call stack\n  stack = []\n  res = 0\n\n  # Recurse: recursive call\n  for i in n.downto(0)\n    # Simulate \"recurse\" with \"push\"\n    stack << i\n  end\n  # Return: return result\n  while !stack.empty?\n    res += stack.pop\n  end\n\n  # res = 1+2+3+...+n\n  res\nend\n

      Observing the above code, when recursion is transformed into iteration, the code becomes more complex. Although iteration and recursion can be converted into each other in many cases, it may not be worthwhile to do so for the following two reasons.

      • The transformed code may be more difficult to understand and less readable.
      • For some complex problems, simulating the behavior of the system call stack can be very difficult.

      In summary, choosing between iteration and recursion depends on the nature of the specific problem. In programming practice, it is crucial to weigh the pros and cons of both and choose the appropriate method based on the context.

      "},{"location":"chapter_computational_complexity/performance_evaluation/","title":"2.1 \u00a0 Algorithm Efficiency Evaluation","text":"

      In algorithm design, we pursue the following two levels of objectives sequentially.

      1. Finding a solution to the problem: The algorithm must reliably obtain the correct solution within the specified input range.
      2. Seeking the optimal solution: Multiple solutions may exist for the same problem, and we hope to find an algorithm that is as efficient as possible.

      In other words, under the premise of being able to solve the problem, algorithm efficiency has become the primary evaluation criterion for measuring the quality of algorithms. It includes the following two dimensions.

      • Time efficiency: The length of time the algorithm runs.
      • Space efficiency: The size of memory space the algorithm occupies.

      In short, our goal is to design data structures and algorithms that are \"both fast and memory-efficient\". Effectively evaluating algorithm efficiency is crucial, because only in this way can we compare various algorithms and guide the algorithm design and optimization process.

      Efficiency evaluation methods are mainly divided into two types: actual testing and theoretical estimation.

      "},{"location":"chapter_computational_complexity/performance_evaluation/#211-actual-testing","title":"2.1.1 \u00a0 Actual Testing","text":"

      Suppose we now have algorithm A and algorithm B, both of which can solve the same problem, and we need to compare the efficiency of these two algorithms. The most direct method is to find a computer, run these two algorithms, and monitor and record their running time and memory usage. This evaluation approach can reflect the real situation, but it also has considerable limitations.

      On one hand, it is difficult to eliminate interference factors from the testing environment. Hardware configuration affects the performance of algorithms. For example, if an algorithm has a high degree of parallelism, it is more suitable for running on multi-core CPUs; if an algorithm has intensive memory operations, it will perform better on high-performance memory. In other words, the test results of an algorithm on different machines may be inconsistent. This means we need to test on various machines and calculate average efficiency, which is impractical.

      On the other hand, conducting complete testing is very resource-intensive. As the input data volume changes, the algorithm will exhibit different efficiencies. For example, when the input data volume is small, the running time of algorithm A is shorter than algorithm B; but when the input data volume is large, the test results may be exactly the opposite. Therefore, to obtain convincing conclusions, we need to test input data of various scales, which requires a large amount of computational resources.

      "},{"location":"chapter_computational_complexity/performance_evaluation/#212-theoretical-estimation","title":"2.1.2 \u00a0 Theoretical Estimation","text":"

      Since actual testing has considerable limitations, we can consider evaluating algorithm efficiency through calculations alone. This estimation method is called asymptotic complexity analysis, or complexity analysis for short.

      Complexity analysis can reflect the relationship between the time and space resources required for algorithm execution and the input data scale. It describes the growth trend of the time and space required for algorithm execution as the input data scale increases. This definition is somewhat convoluted, so we can break it down into three key points to understand.

      • \"Time and space resources\" correspond to time complexity and space complexity, respectively.
      • \"As the input data scale increases\" means that complexity reflects the relationship between algorithm running efficiency and input data scale.
      • \"Growth trend of time and space\" indicates that complexity analysis focuses not on the specific values of running time or occupied space, but on how \"fast\" time or space grows.

      Complexity analysis overcomes the drawbacks of the actual testing method, reflected in the following aspects.

      • It does not need to actually run the code, making it more environmentally friendly and energy-efficient.
      • It is independent of the testing environment, and the analysis results are applicable to all running platforms.
      • It can reflect algorithm efficiency at different data volumes, especially algorithm performance at large data volumes.

      Tip

      If you are still confused about the concept of complexity, don't worry\u2014we will introduce it in detail in subsequent chapters.

      Complexity analysis provides us with a \"ruler\" for evaluating algorithm efficiency, allowing us to measure the time and space resources required to execute a certain algorithm and compare the efficiency between different algorithms.

      Complexity is a mathematical concept that may be relatively abstract for beginners, with a relatively high learning difficulty. From this perspective, complexity analysis may not be very suitable as the first content to be introduced. However, when we discuss the characteristics of a certain data structure or algorithm, it is difficult to avoid analyzing its running speed and space usage.

      In summary, it is recommended that before diving deep into data structures and algorithms, you first establish a preliminary understanding of complexity analysis so that you can complete complexity analysis of simple algorithms.

      "},{"location":"chapter_computational_complexity/space_complexity/","title":"2.4 \u00a0 Space Complexity","text":"

      Space complexity measures the growth trend of memory space occupied by an algorithm as the data size increases. This concept is very similar to time complexity, except that \"running time\" is replaced with \"occupied memory space\".

      "},{"location":"chapter_computational_complexity/space_complexity/#241-algorithm-related-space","title":"2.4.1 \u00a0 Algorithm-Related Space","text":"

      The memory space used by an algorithm during execution mainly includes the following types.

      • Input space: Used to store the input data of the algorithm.
      • Temporary space: Used to store variables, objects, function contexts, and other data during the algorithm's execution.
      • Output space: Used to store the output data of the algorithm.

      In general, the scope of space complexity statistics is \"temporary space\" plus \"output space\".

      Temporary space can be further divided into three parts.

      • Temporary data: Used to save various constants, variables, objects, etc., during the algorithm's execution.
      • Stack frame space: Used to save the context data of called functions. The system creates a stack frame at the top of the stack each time a function is called, and the stack frame space is released after the function returns.
      • Instruction space: Used to save compiled program instructions, which are usually ignored in actual statistics.

      When analyzing the space complexity of a program, we usually count three parts: temporary data, stack frame space, and output data, as shown in the following figure.

      Figure 2-15 \u00a0 Algorithm-related space

      The related code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      class Node:\n    \"\"\"Class\"\"\"\n    def __init__(self, x: int):\n        self.val: int = x              # Node value\n        self.next: Node | None = None  # Reference to the next node\n\ndef function() -> int:\n    \"\"\"Function\"\"\"\n    # Perform some operations...\n    return 0\n\ndef algorithm(n) -> int:  # Input data\n    A = 0                 # Temporary data (constant, usually represented by uppercase letters)\n    b = 0                 # Temporary data (variable)\n    node = Node(0)        # Temporary data (object)\n    c = function()        # Stack frame space (function call)\n    return A + b + c      # Output data\n
      /* Structure */\nstruct Node {\n    int val;\n    Node *next;\n    Node(int x) : val(x), next(nullptr) {}\n};\n\n/* Function */\nint func() {\n    // Perform some operations...\n    return 0;\n}\n\nint algorithm(int n) {        // Input data\n    const int a = 0;          // Temporary data (constant)\n    int b = 0;                // Temporary data (variable)\n    Node* node = new Node(0); // Temporary data (object)\n    int c = func();           // Stack frame space (function call)\n    return a + b + c;         // Output data\n}\n
      /* Class */\nclass Node {\n    int val;\n    Node next;\n    Node(int x) { val = x; }\n}\n\n/* Function */\nint function() {\n    // Perform some operations...\n    return 0;\n}\n\nint algorithm(int n) {        // Input data\n    final int a = 0;          // Temporary data (constant)\n    int b = 0;                // Temporary data (variable)\n    Node node = new Node(0);  // Temporary data (object)\n    int c = function();       // Stack frame space (function call)\n    return a + b + c;         // Output data\n}\n
      /* Class */\nclass Node(int x) {\n    int val = x;\n    Node next;\n}\n\n/* Function */\nint Function() {\n    // Perform some operations...\n    return 0;\n}\n\nint Algorithm(int n) {        // Input data\n    const int a = 0;          // Temporary data (constant)\n    int b = 0;                // Temporary data (variable)\n    Node node = new(0);       // Temporary data (object)\n    int c = Function();       // Stack frame space (function call)\n    return a + b + c;         // Output data\n}\n
      /* Structure */\ntype node struct {\n    val  int\n    next *node\n}\n\n/* Create node structure */\nfunc newNode(val int) *node {\n    return &node{val: val}\n}\n\n/* Function */\nfunc function() int {\n    // Perform some operations...\n    return 0\n}\n\nfunc algorithm(n int) int { // Input data\n    const a = 0             // Temporary data (constant)\n    b := 0                  // Temporary data (variable)\n    newNode(0)              // Temporary data (object)\n    c := function()         // Stack frame space (function call)\n    return a + b + c        // Output data\n}\n
      /* Class */\nclass Node {\n    var val: Int\n    var next: Node?\n\n    init(x: Int) {\n        val = x\n    }\n}\n\n/* Function */\nfunc function() -> Int {\n    // Perform some operations...\n    return 0\n}\n\nfunc algorithm(n: Int) -> Int { // Input data\n    let a = 0             // Temporary data (constant)\n    var b = 0             // Temporary data (variable)\n    let node = Node(x: 0) // Temporary data (object)\n    let c = function()    // Stack frame space (function call)\n    return a + b + c      // Output data\n}\n
      /* Class */\nclass Node {\n    val;\n    next;\n    constructor(val) {\n        this.val = val === undefined ? 0 : val; // Node value\n        this.next = null;                       // Reference to the next node\n    }\n}\n\n/* Function */\nfunction constFunc() {\n    // Perform some operations\n    return 0;\n}\n\nfunction algorithm(n) {       // Input data\n    const a = 0;              // Temporary data (constant)\n    let b = 0;                // Temporary data (variable)\n    const node = new Node(0); // Temporary data (object)\n    const c = constFunc();    // Stack frame space (function call)\n    return a + b + c;         // Output data\n}\n
      /* Class */\nclass Node {\n    val: number;\n    next: Node | null;\n    constructor(val?: number) {\n        this.val = val === undefined ? 0 : val; // Node value\n        this.next = null;                       // Reference to the next node\n    }\n}\n\n/* Function */\nfunction constFunc(): number {\n    // Perform some operations\n    return 0;\n}\n\nfunction algorithm(n: number): number { // Input data\n    const a = 0;                        // Temporary data (constant)\n    let b = 0;                          // Temporary data (variable)\n    const node = new Node(0);           // Temporary data (object)\n    const c = constFunc();              // Stack frame space (function call)\n    return a + b + c;                   // Output data\n}\n
      /* Class */\nclass Node {\n  int val;\n  Node next;\n  Node(this.val, [this.next]);\n}\n\n/* Function */\nint function() {\n  // Perform some operations...\n  return 0;\n}\n\nint algorithm(int n) {  // Input data\n  const int a = 0;      // Temporary data (constant)\n  int b = 0;            // Temporary data (variable)\n  Node node = Node(0);  // Temporary data (object)\n  int c = function();   // Stack frame space (function call)\n  return a + b + c;     // Output data\n}\n
      use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* Structure */\nstruct Node {\n    val: i32,\n    next: Option<Rc<RefCell<Node>>>,\n}\n\n/* Create Node structure */\nimpl Node {\n    fn new(val: i32) -> Self {\n        Self { val: val, next: None }\n    }\n}\n\n/* Function */\nfn function() -> i32 {\n    // Perform some operations...\n    return 0;\n}\n\nfn algorithm(n: i32) -> i32 {       // Input data\n    const a: i32 = 0;               // Temporary data (constant)\n    let mut b = 0;                  // Temporary data (variable)\n    let node = Node::new(0);        // Temporary data (object)\n    let c = function();             // Stack frame space (function call)\n    return a + b + c;               // Output data\n}\n
      /* Function */\nint func() {\n    // Perform some operations...\n    return 0;\n}\n\nint algorithm(int n) { // Input data\n    const int a = 0;   // Temporary data (constant)\n    int b = 0;         // Temporary data (variable)\n    int c = func();    // Stack frame space (function call)\n    return a + b + c;  // Output data\n}\n
      /* Class */\nclass Node(var _val: Int) {\n    var next: Node? = null\n}\n\n/* Function */\nfun function(): Int {\n    // Perform some operations...\n    return 0\n}\n\nfun algorithm(n: Int): Int { // Input data\n    val a = 0                // Temporary data (constant)\n    var b = 0                // Temporary data (variable)\n    val node = Node(0)       // Temporary data (object)\n    val c = function()       // Stack frame space (function call)\n    return a + b + c         // Output data\n}\n
      ### Class ###\nclass Node\n    attr_accessor :val      # Node value\n    attr_accessor :next     # Reference to the next node\n\n    def initialize(x)\n        @val = x\n    end\nend\n\n### Function ###\ndef function\n    # Perform some operations...\n    0\nend\n\n### Algorithm ###\ndef algorithm(n)        # Input data\n    a = 0               # Temporary data (constant)\n    b = 0               # Temporary data (variable)\n    node = Node.new(0)  # Temporary data (object)\n    c = function        # Stack frame space (function call)\n    a + b + c           # Output data\nend\n
      "},{"location":"chapter_computational_complexity/space_complexity/#242-calculation-method","title":"2.4.2 \u00a0 Calculation Method","text":"

      The calculation method for space complexity is roughly the same as for time complexity, except that the statistical object is changed from \"number of operations\" to \"size of space used\".

      Unlike time complexity, we usually only focus on the worst-case space complexity. This is because memory space is a hard requirement, and we must ensure that sufficient memory space is reserved for all input data.

      Observe the following code. The \"worst case\" in worst-case space complexity has two meanings.

      1. Based on the worst input data: When \\(n < 10\\), the space complexity is \\(O(1)\\); but when \\(n > 10\\), the initialized array nums occupies \\(O(n)\\) space, so the worst-case space complexity is \\(O(n)\\).
      2. Based on the peak memory during algorithm execution: For example, before executing the last line, the program occupies \\(O(1)\\) space; when initializing the array nums, the program occupies \\(O(n)\\) space, so the worst-case space complexity is \\(O(n)\\).
      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      def algorithm(n: int):\n    a = 0               # O(1)\n    b = [0] * 10000     # O(1)\n    if n > 10:\n        nums = [0] * n  # O(n)\n
      void algorithm(int n) {\n    int a = 0;               // O(1)\n    vector<int> b(10000);    // O(1)\n    if (n > 10)\n        vector<int> nums(n); // O(n)\n}\n
      void algorithm(int n) {\n    int a = 0;                   // O(1)\n    int[] b = new int[10000];    // O(1)\n    if (n > 10)\n        int[] nums = new int[n]; // O(n)\n}\n
      void Algorithm(int n) {\n    int a = 0;                   // O(1)\n    int[] b = new int[10000];    // O(1)\n    if (n > 10) {\n        int[] nums = new int[n]; // O(n)\n    }\n}\n
      func algorithm(n int) {\n    a := 0                      // O(1)\n    b := make([]int, 10000)     // O(1)\n    var nums []int\n    if n > 10 {\n        nums := make([]int, n)  // O(n)\n    }\n    fmt.Println(a, b, nums)\n}\n
      func algorithm(n: Int) {\n    let a = 0 // O(1)\n    let b = Array(repeating: 0, count: 10000) // O(1)\n    if n > 10 {\n        let nums = Array(repeating: 0, count: n) // O(n)\n    }\n}\n
      function algorithm(n) {\n    const a = 0;                   // O(1)\n    const b = new Array(10000);    // O(1)\n    if (n > 10) {\n        const nums = new Array(n); // O(n)\n    }\n}\n
      function algorithm(n: number): void {\n    const a = 0;                   // O(1)\n    const b = new Array(10000);    // O(1)\n    if (n > 10) {\n        const nums = new Array(n); // O(n)\n    }\n}\n
      void algorithm(int n) {\n  int a = 0;                            // O(1)\n  List<int> b = List.filled(10000, 0);  // O(1)\n  if (n > 10) {\n    List<int> nums = List.filled(n, 0); // O(n)\n  }\n}\n
      fn algorithm(n: i32) {\n    let a = 0;                              // O(1)\n    let b = [0; 10000];                     // O(1)\n    if n > 10 {\n        let nums = vec![0; n as usize];     // O(n)\n    }\n}\n
      void algorithm(int n) {\n    int a = 0;               // O(1)\n    int b[10000];            // O(1)\n    if (n > 10)\n        int nums[n] = {0};   // O(n)\n}\n
      fun algorithm(n: Int) {\n    val a = 0                    // O(1)\n    val b = IntArray(10000)      // O(1)\n    if (n > 10) {\n        val nums = IntArray(n)   // O(n)\n    }\n}\n
      def algorithm(n)\n    a = 0                           # O(1)\n    b = Array.new(10000)            # O(1)\n    nums = Array.new(n) if n > 10   # O(n)\nend\n

      In recursive functions, it is necessary to count the stack frame space. Observe the following code:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      def function() -> int:\n    # Perform some operations\n    return 0\n\ndef loop(n: int):\n    \"\"\"Loop has space complexity of O(1)\"\"\"\n    for _ in range(n):\n        function()\n\ndef recur(n: int):\n    \"\"\"Recursion has space complexity of O(n)\"\"\"\n    if n == 1:\n        return\n    return recur(n - 1)\n
      int func() {\n    // Perform some operations\n    return 0;\n}\n/* Loop has space complexity of O(1) */\nvoid loop(int n) {\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n/* Recursion has space complexity of O(n) */\nvoid recur(int n) {\n    if (n == 1) return;\n    recur(n - 1);\n}\n
      int function() {\n    // Perform some operations\n    return 0;\n}\n/* Loop has space complexity of O(1) */\nvoid loop(int n) {\n    for (int i = 0; i < n; i++) {\n        function();\n    }\n}\n/* Recursion has space complexity of O(n) */\nvoid recur(int n) {\n    if (n == 1) return;\n    recur(n - 1);\n}\n
      int Function() {\n    // Perform some operations\n    return 0;\n}\n/* Loop has space complexity of O(1) */\nvoid Loop(int n) {\n    for (int i = 0; i < n; i++) {\n        Function();\n    }\n}\n/* Recursion has space complexity of O(n) */\nint Recur(int n) {\n    if (n == 1) return 1;\n    return Recur(n - 1);\n}\n
      func function() int {\n    // Perform some operations\n    return 0\n}\n\n/* Loop has space complexity of O(1) */\nfunc loop(n int) {\n    for i := 0; i < n; i++ {\n        function()\n    }\n}\n\n/* Recursion has space complexity of O(n) */\nfunc recur(n int) {\n    if n == 1 {\n        return\n    }\n    recur(n - 1)\n}\n
      @discardableResult\nfunc function() -> Int {\n    // Perform some operations\n    return 0\n}\n\n/* Loop has space complexity of O(1) */\nfunc loop(n: Int) {\n    for _ in 0 ..< n {\n        function()\n    }\n}\n\n/* Recursion has space complexity of O(n) */\nfunc recur(n: Int) {\n    if n == 1 {\n        return\n    }\n    recur(n: n - 1)\n}\n
      function constFunc() {\n    // Perform some operations\n    return 0;\n}\n/* Loop has space complexity of O(1) */\nfunction loop(n) {\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n/* Recursion has space complexity of O(n) */\nfunction recur(n) {\n    if (n === 1) return;\n    return recur(n - 1);\n}\n
      function constFunc(): number {\n    // Perform some operations\n    return 0;\n}\n/* Loop has space complexity of O(1) */\nfunction loop(n: number): void {\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n/* Recursion has space complexity of O(n) */\nfunction recur(n: number): void {\n    if (n === 1) return;\n    return recur(n - 1);\n}\n
      int function() {\n  // Perform some operations\n  return 0;\n}\n/* Loop has space complexity of O(1) */\nvoid loop(int n) {\n  for (int i = 0; i < n; i++) {\n    function();\n  }\n}\n/* Recursion has space complexity of O(n) */\nvoid recur(int n) {\n  if (n == 1) return;\n  recur(n - 1);\n}\n
      fn function() -> i32 {\n    // Perform some operations\n    return 0;\n}\n/* Loop has space complexity of O(1) */\nfn loop(n: i32) {\n    for i in 0..n {\n        function();\n    }\n}\n/* Recursion has space complexity of O(n) */\nfn recur(n: i32) {\n    if n == 1 {\n        return;\n    }\n    recur(n - 1);\n}\n
      int func() {\n    // Perform some operations\n    return 0;\n}\n/* Loop has space complexity of O(1) */\nvoid loop(int n) {\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n/* Recursion has space complexity of O(n) */\nvoid recur(int n) {\n    if (n == 1) return;\n    recur(n - 1);\n}\n
      fun function(): Int {\n    // Perform some operations\n    return 0\n}\n/* Loop has space complexity of O(1) */\nfun loop(n: Int) {\n    for (i in 0..<n) {\n        function()\n    }\n}\n/* Recursion has space complexity of O(n) */\nfun recur(n: Int) {\n    if (n == 1) return\n    return recur(n - 1)\n}\n
      def function\n    # Perform some operations\n    0\nend\n\n### Loop has space complexity of O(1) ###\ndef loop(n)\n    (0...n).each { function }\nend\n\n### Recursion has space complexity of O(n) ###\ndef recur(n)\n    return if n == 1\n    recur(n - 1)\nend\n

      The time complexity of both functions loop() and recur() is \\(O(n)\\), but their space complexities are different.

      • The function loop() calls function() \\(n\\) times in a loop. In each iteration, function() returns and releases its stack frame space, so the space complexity remains \\(O(1)\\).
      • The recursive function recur() has \\(n\\) unreturned recur() instances existing simultaneously during execution, thus occupying \\(O(n)\\) stack frame space.
      "},{"location":"chapter_computational_complexity/space_complexity/#243-common-types","title":"2.4.3 \u00a0 Common Types","text":"

      Let the input data size be \\(n\\). The following figure shows common types of space complexity (arranged from low to high).

      \\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n^2) < O(2^n) \\newline \\text{Constant} < \\text{Logarithmic} < \\text{Linear} < \\text{Quadratic} < \\text{Exponential} \\end{aligned} \\]

      Figure 2-16 \u00a0 Common types of space complexity

      "},{"location":"chapter_computational_complexity/space_complexity/#1-constant-order-o1","title":"1. \u00a0 Constant Order \\(O(1)\\)","text":"

      Constant order is common in constants, variables, and objects whose quantity is independent of the input data size \\(n\\).

      It should be noted that memory occupied by initializing variables or calling functions in a loop is released when entering the next iteration, so it does not accumulate space, and the space complexity remains \\(O(1)\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
      def function() -> int:\n    \"\"\"Function\"\"\"\n    # Perform some operations\n    return 0\n\ndef constant(n: int):\n    \"\"\"Constant order\"\"\"\n    # Constants, variables, objects occupy O(1) space\n    a = 0\n    nums = [0] * 10000\n    node = ListNode(0)\n    # Variables in the loop occupy O(1) space\n    for _ in range(n):\n        c = 0\n    # Functions in the loop occupy O(1) space\n    for _ in range(n):\n        function()\n
      space_complexity.cpp
      /* Function */\nint func() {\n    // Perform some operations\n    return 0;\n}\n\n/* Constant order */\nvoid constant(int n) {\n    // Constants, variables, objects occupy O(1) space\n    const int a = 0;\n    int b = 0;\n    vector<int> nums(10000);\n    ListNode node(0);\n    // Variables in the loop occupy O(1) space\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // Functions in the loop occupy O(1) space\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n
      space_complexity.java
      /* Function */\nint function() {\n    // Perform some operations\n    return 0;\n}\n\n/* Constant order */\nvoid constant(int n) {\n    // Constants, variables, objects occupy O(1) space\n    final int a = 0;\n    int b = 0;\n    int[] nums = new int[10000];\n    ListNode node = new ListNode(0);\n    // Variables in the loop occupy O(1) space\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // Functions in the loop occupy O(1) space\n    for (int i = 0; i < n; i++) {\n        function();\n    }\n}\n
      space_complexity.cs
      /* Function */\nint Function() {\n    // Perform some operations\n    return 0;\n}\n\n/* Constant order */\nvoid Constant(int n) {\n    // Constants, variables, objects occupy O(1) space\n    int a = 0;\n    int b = 0;\n    int[] nums = new int[10000];\n    ListNode node = new(0);\n    // Variables in the loop occupy O(1) space\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // Functions in the loop occupy O(1) space\n    for (int i = 0; i < n; i++) {\n        Function();\n    }\n}\n
      space_complexity.go
      /* Function */\nfunc function() int {\n    // Perform some operations...\n    return 0\n}\n\n/* Constant order */\nfunc spaceConstant(n int) {\n    // Constants, variables, objects occupy O(1) space\n    const a = 0\n    b := 0\n    nums := make([]int, 10000)\n    node := newNode(0)\n    // Variables in the loop occupy O(1) space\n    var c int\n    for i := 0; i < n; i++ {\n        c = 0\n    }\n    // Functions in the loop occupy O(1) space\n    for i := 0; i < n; i++ {\n        function()\n    }\n    b += 0\n    c += 0\n    nums[0] = 0\n    node.val = 0\n}\n
      space_complexity.swift
      /* Function */\n@discardableResult\nfunc function() -> Int {\n    // Perform some operations\n    return 0\n}\n\n/* Constant order */\nfunc constant(n: Int) {\n    // Constants, variables, objects occupy O(1) space\n    let a = 0\n    var b = 0\n    let nums = Array(repeating: 0, count: 10000)\n    let node = ListNode(x: 0)\n    // Variables in the loop occupy O(1) space\n    for _ in 0 ..< n {\n        let c = 0\n    }\n    // Functions in the loop occupy O(1) space\n    for _ in 0 ..< n {\n        function()\n    }\n}\n
      space_complexity.js
      /* Function */\nfunction constFunc() {\n    // Perform some operations\n    return 0;\n}\n\n/* Constant order */\nfunction constant(n) {\n    // Constants, variables, objects occupy O(1) space\n    const a = 0;\n    const b = 0;\n    const nums = new Array(10000);\n    const node = new ListNode(0);\n    // Variables in the loop occupy O(1) space\n    for (let i = 0; i < n; i++) {\n        const c = 0;\n    }\n    // Functions in the loop occupy O(1) space\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n
      space_complexity.ts
      /* Function */\nfunction constFunc(): number {\n    // Perform some operations\n    return 0;\n}\n\n/* Constant order */\nfunction constant(n: number): void {\n    // Constants, variables, objects occupy O(1) space\n    const a = 0;\n    const b = 0;\n    const nums = new Array(10000);\n    const node = new ListNode(0);\n    // Variables in the loop occupy O(1) space\n    for (let i = 0; i < n; i++) {\n        const c = 0;\n    }\n    // Functions in the loop occupy O(1) space\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n
      space_complexity.dart
      /* Function */\nint function() {\n  // Perform some operations\n  return 0;\n}\n\n/* Constant order */\nvoid constant(int n) {\n  // Constants, variables, objects occupy O(1) space\n  final int a = 0;\n  int b = 0;\n  List<int> nums = List.filled(10000, 0);\n  ListNode node = ListNode(0);\n  // Variables in the loop occupy O(1) space\n  for (var i = 0; i < n; i++) {\n    int c = 0;\n  }\n  // Functions in the loop occupy O(1) space\n  for (var i = 0; i < n; i++) {\n    function();\n  }\n}\n
      space_complexity.rs
      /* Function */\nfn function() -> i32 {\n    // Perform some operations\n    return 0;\n}\n\n/* Constant order */\n#[allow(unused)]\nfn constant(n: i32) {\n    // Constants, variables, objects occupy O(1) space\n    const A: i32 = 0;\n    let b = 0;\n    let nums = vec![0; 10000];\n    let node = ListNode::new(0);\n    // Variables in the loop occupy O(1) space\n    for i in 0..n {\n        let c = 0;\n    }\n    // Functions in the loop occupy O(1) space\n    for i in 0..n {\n        function();\n    }\n}\n
      space_complexity.c
      /* Function */\nint func() {\n    // Perform some operations\n    return 0;\n}\n\n/* Constant order */\nvoid constant(int n) {\n    // Constants, variables, objects occupy O(1) space\n    const int a = 0;\n    int b = 0;\n    int nums[1000];\n    ListNode *node = newListNode(0);\n    free(node);\n    // Variables in the loop occupy O(1) space\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // Functions in the loop occupy O(1) space\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n
      space_complexity.kt
      /* Function */\nfun function(): Int {\n    // Perform some operations\n    return 0\n}\n\n/* Constant order */\nfun constant(n: Int) {\n    // Constants, variables, objects occupy O(1) space\n    val a = 0\n    var b = 0\n    val nums = Array(10000) { 0 }\n    val node = ListNode(0)\n    // Variables in the loop occupy O(1) space\n    for (i in 0..<n) {\n        val c = 0\n    }\n    // Functions in the loop occupy O(1) space\n    for (i in 0..<n) {\n        function()\n    }\n}\n
      space_complexity.rb
      ### Function ###\ndef function\n  # Perform some operations\n  0\nend\n\n### Constant time ###\ndef constant(n)\n  # Constants, variables, objects occupy O(1) space\n  a = 0\n  nums = [0] * 10000\n  node = ListNode.new\n\n  # Variables in the loop occupy O(1) space\n  (0...n).each { c = 0 }\n  # Functions in the loop occupy O(1) space\n  (0...n).each { function }\nend\n
      "},{"location":"chapter_computational_complexity/space_complexity/#2-linear-order-on","title":"2. \u00a0 Linear Order \\(O(n)\\)","text":"

      Linear order is common in arrays, linked lists, stacks, queues, etc., where the number of elements is proportional to \\(n\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
      def linear(n: int):\n    \"\"\"Linear order\"\"\"\n    # A list of length n occupies O(n) space\n    nums = [0] * n\n    # A hash table of length n occupies O(n) space\n    hmap = dict[int, str]()\n    for i in range(n):\n        hmap[i] = str(i)\n
      space_complexity.cpp
      /* Linear order */\nvoid linear(int n) {\n    // Array of length n uses O(n) space\n    vector<int> nums(n);\n    // A list of length n occupies O(n) space\n    vector<ListNode> nodes;\n    for (int i = 0; i < n; i++) {\n        nodes.push_back(ListNode(i));\n    }\n    // A hash table of length n occupies O(n) space\n    unordered_map<int, string> map;\n    for (int i = 0; i < n; i++) {\n        map[i] = to_string(i);\n    }\n}\n
      space_complexity.java
      /* Linear order */\nvoid linear(int n) {\n    // Array of length n uses O(n) space\n    int[] nums = new int[n];\n    // A list of length n occupies O(n) space\n    List<ListNode> nodes = new ArrayList<>();\n    for (int i = 0; i < n; i++) {\n        nodes.add(new ListNode(i));\n    }\n    // A hash table of length n occupies O(n) space\n    Map<Integer, String> map = new HashMap<>();\n    for (int i = 0; i < n; i++) {\n        map.put(i, String.valueOf(i));\n    }\n}\n
      space_complexity.cs
      /* Linear order */\nvoid Linear(int n) {\n    // Array of length n uses O(n) space\n    int[] nums = new int[n];\n    // A list of length n occupies O(n) space\n    List<ListNode> nodes = [];\n    for (int i = 0; i < n; i++) {\n        nodes.Add(new ListNode(i));\n    }\n    // A hash table of length n occupies O(n) space\n    Dictionary<int, string> map = [];\n    for (int i = 0; i < n; i++) {\n        map.Add(i, i.ToString());\n    }\n}\n
      space_complexity.go
      /* Linear order */\nfunc spaceLinear(n int) {\n    // Array of length n uses O(n) space\n    _ = make([]int, n)\n    // A list of length n occupies O(n) space\n    var nodes []*node\n    for i := 0; i < n; i++ {\n        nodes = append(nodes, newNode(i))\n    }\n    // A hash table of length n occupies O(n) space\n    m := make(map[int]string, n)\n    for i := 0; i < n; i++ {\n        m[i] = strconv.Itoa(i)\n    }\n}\n
      space_complexity.swift
      /* Linear order */\nfunc linear(n: Int) {\n    // Array of length n uses O(n) space\n    let nums = Array(repeating: 0, count: n)\n    // A list of length n occupies O(n) space\n    let nodes = (0 ..< n).map { ListNode(x: $0) }\n    // A hash table of length n occupies O(n) space\n    let map = Dictionary(uniqueKeysWithValues: (0 ..< n).map { ($0, \"\\($0)\") })\n}\n
      space_complexity.js
      /* Linear order */\nfunction linear(n) {\n    // Array of length n uses O(n) space\n    const nums = new Array(n);\n    // A list of length n occupies O(n) space\n    const nodes = [];\n    for (let i = 0; i < n; i++) {\n        nodes.push(new ListNode(i));\n    }\n    // A hash table of length n occupies O(n) space\n    const map = new Map();\n    for (let i = 0; i < n; i++) {\n        map.set(i, i.toString());\n    }\n}\n
      space_complexity.ts
      /* Linear order */\nfunction linear(n: number): void {\n    // Array of length n uses O(n) space\n    const nums = new Array(n);\n    // A list of length n occupies O(n) space\n    const nodes: ListNode[] = [];\n    for (let i = 0; i < n; i++) {\n        nodes.push(new ListNode(i));\n    }\n    // A hash table of length n occupies O(n) space\n    const map = new Map();\n    for (let i = 0; i < n; i++) {\n        map.set(i, i.toString());\n    }\n}\n
      space_complexity.dart
      /* Linear order */\nvoid linear(int n) {\n  // Array of length n uses O(n) space\n  List<int> nums = List.filled(n, 0);\n  // A list of length n occupies O(n) space\n  List<ListNode> nodes = [];\n  for (var i = 0; i < n; i++) {\n    nodes.add(ListNode(i));\n  }\n  // A hash table of length n occupies O(n) space\n  Map<int, String> map = HashMap();\n  for (var i = 0; i < n; i++) {\n    map.putIfAbsent(i, () => i.toString());\n  }\n}\n
      space_complexity.rs
      /* Linear order */\n#[allow(unused)]\nfn linear(n: i32) {\n    // Array of length n uses O(n) space\n    let mut nums = vec![0; n as usize];\n    // A list of length n occupies O(n) space\n    let mut nodes = Vec::new();\n    for i in 0..n {\n        nodes.push(ListNode::new(i))\n    }\n    // A hash table of length n occupies O(n) space\n    let mut map = HashMap::new();\n    for i in 0..n {\n        map.insert(i, i.to_string());\n    }\n}\n
      space_complexity.c
      /* Hash table */\ntypedef struct {\n    int key;\n    int val;\n    UT_hash_handle hh; // Implemented using uthash.h\n} HashTable;\n\n/* Linear order */\nvoid linear(int n) {\n    // Array of length n uses O(n) space\n    int *nums = malloc(sizeof(int) * n);\n    free(nums);\n\n    // A list of length n occupies O(n) space\n    ListNode **nodes = malloc(sizeof(ListNode *) * n);\n    for (int i = 0; i < n; i++) {\n        nodes[i] = newListNode(i);\n    }\n    // Memory release\n    for (int i = 0; i < n; i++) {\n        free(nodes[i]);\n    }\n    free(nodes);\n\n    // A hash table of length n occupies O(n) space\n    HashTable *h = NULL;\n    for (int i = 0; i < n; i++) {\n        HashTable *tmp = malloc(sizeof(HashTable));\n        tmp->key = i;\n        tmp->val = i;\n        HASH_ADD_INT(h, key, tmp);\n    }\n\n    // Memory release\n    HashTable *curr, *tmp;\n    HASH_ITER(hh, h, curr, tmp) {\n        HASH_DEL(h, curr);\n        free(curr);\n    }\n}\n
      space_complexity.kt
      /* Linear order */\nfun linear(n: Int) {\n    // Array of length n uses O(n) space\n    val nums = Array(n) { 0 }\n    // A list of length n occupies O(n) space\n    val nodes = mutableListOf<ListNode>()\n    for (i in 0..<n) {\n        nodes.add(ListNode(i))\n    }\n    // A hash table of length n occupies O(n) space\n    val map = mutableMapOf<Int, String>()\n    for (i in 0..<n) {\n        map[i] = i.toString()\n    }\n}\n
      space_complexity.rb
      ### Linear time ###\ndef linear(n)\n  # A list of length n occupies O(n) space\n  nums = Array.new(n, 0)\n\n  # A hash table of length n occupies O(n) space\n  hmap = {}\n  for i in 0...n\n    hmap[i] = i.to_s\n  end\nend\n

      As shown in the following figure, the recursion depth of this function is \\(n\\), meaning that there are \\(n\\) unreturned linear_recur() functions existing simultaneously, using \\(O(n)\\) stack frame space:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
      def linear_recur(n: int):\n    \"\"\"Linear order (recursive implementation)\"\"\"\n    print(\"Recursion n =\", n)\n    if n == 1:\n        return\n    linear_recur(n - 1)\n
      space_complexity.cpp
      /* Linear order (recursive implementation) */\nvoid linearRecur(int n) {\n    cout << \"Recursion n = \" << n << endl;\n    if (n == 1)\n        return;\n    linearRecur(n - 1);\n}\n
      space_complexity.java
      /* Linear order (recursive implementation) */\nvoid linearRecur(int n) {\n    System.out.println(\"Recursion n = \" + n);\n    if (n == 1)\n        return;\n    linearRecur(n - 1);\n}\n
      space_complexity.cs
      /* Linear order (recursive implementation) */\nvoid LinearRecur(int n) {\n    Console.WriteLine(\"Recursion n = \" + n);\n    if (n == 1) return;\n    LinearRecur(n - 1);\n}\n
      space_complexity.go
      /* Linear order (recursive implementation) */\nfunc spaceLinearRecur(n int) {\n    fmt.Println(\"Recursion n =\", n)\n    if n == 1 {\n        return\n    }\n    spaceLinearRecur(n - 1)\n}\n
      space_complexity.swift
      /* Linear order (recursive implementation) */\nfunc linearRecur(n: Int) {\n    print(\"Recursion n = \\(n)\")\n    if n == 1 {\n        return\n    }\n    linearRecur(n: n - 1)\n}\n
      space_complexity.js
      /* Linear order (recursive implementation) */\nfunction linearRecur(n) {\n    console.log(`Recursion n = ${n}`);\n    if (n === 1) return;\n    linearRecur(n - 1);\n}\n
      space_complexity.ts
      /* Linear order (recursive implementation) */\nfunction linearRecur(n: number): void {\n    console.log(`Recursion n = ${n}`);\n    if (n === 1) return;\n    linearRecur(n - 1);\n}\n
      space_complexity.dart
      /* Linear order (recursive implementation) */\nvoid linearRecur(int n) {\n  print('Recursion n = $n');\n  if (n == 1) return;\n  linearRecur(n - 1);\n}\n
      space_complexity.rs
      /* Linear order (recursive implementation) */\nfn linear_recur(n: i32) {\n    println!(\"Recursion n = {}\", n);\n    if n == 1 {\n        return;\n    };\n    linear_recur(n - 1);\n}\n
      space_complexity.c
      /* Linear order (recursive implementation) */\nvoid linearRecur(int n) {\n    printf(\"Recursion n = %d\\r\\n\", n);\n    if (n == 1)\n        return;\n    linearRecur(n - 1);\n}\n
      space_complexity.kt
      /* Linear order (recursive implementation) */\nfun linearRecur(n: Int) {\n    println(\"Recursion n = $n\")\n    if (n == 1)\n        return\n    linearRecur(n - 1)\n}\n
      space_complexity.rb
      ### Linear space (recursive) ###\ndef linear_recur(n)\n  puts \"Recursion n = #{n}\"\n  return if n == 1\n  linear_recur(n - 1)\nend\n

      Figure 2-17 \u00a0 Linear order space complexity generated by recursive function

      "},{"location":"chapter_computational_complexity/space_complexity/#3-quadratic-order-on2","title":"3. \u00a0 Quadratic Order \\(O(n^2)\\)","text":"

      Quadratic order is common in matrices and graphs, where the number of elements is quadratically related to \\(n\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
      def quadratic(n: int):\n    \"\"\"Quadratic order\"\"\"\n    # A 2D list occupies O(n^2) space\n    num_matrix = [[0] * n for _ in range(n)]\n
      space_complexity.cpp
      /* Exponential order */\nvoid quadratic(int n) {\n    // 2D list uses O(n^2) space\n    vector<vector<int>> numMatrix;\n    for (int i = 0; i < n; i++) {\n        vector<int> tmp;\n        for (int j = 0; j < n; j++) {\n            tmp.push_back(0);\n        }\n        numMatrix.push_back(tmp);\n    }\n}\n
      space_complexity.java
      /* Exponential order */\nvoid quadratic(int n) {\n    // Matrix uses O(n^2) space\n    int[][] numMatrix = new int[n][n];\n    // 2D list uses O(n^2) space\n    List<List<Integer>> numList = new ArrayList<>();\n    for (int i = 0; i < n; i++) {\n        List<Integer> tmp = new ArrayList<>();\n        for (int j = 0; j < n; j++) {\n            tmp.add(0);\n        }\n        numList.add(tmp);\n    }\n}\n
      space_complexity.cs
      /* Exponential order */\nvoid Quadratic(int n) {\n    // Matrix uses O(n^2) space\n    int[,] numMatrix = new int[n, n];\n    // 2D list uses O(n^2) space\n    List<List<int>> numList = [];\n    for (int i = 0; i < n; i++) {\n        List<int> tmp = [];\n        for (int j = 0; j < n; j++) {\n            tmp.Add(0);\n        }\n        numList.Add(tmp);\n    }\n}\n
      space_complexity.go
      /* Exponential order */\nfunc spaceQuadratic(n int) {\n    // Matrix uses O(n^2) space\n    numMatrix := make([][]int, n)\n    for i := 0; i < n; i++ {\n        numMatrix[i] = make([]int, n)\n    }\n}\n
      space_complexity.swift
      /* Exponential order */\nfunc quadratic(n: Int) {\n    // 2D list uses O(n^2) space\n    let numList = Array(repeating: Array(repeating: 0, count: n), count: n)\n}\n
      space_complexity.js
      /* Exponential order */\nfunction quadratic(n) {\n    // Matrix uses O(n^2) space\n    const numMatrix = Array(n)\n        .fill(null)\n        .map(() => Array(n).fill(null));\n    // 2D list uses O(n^2) space\n    const numList = [];\n    for (let i = 0; i < n; i++) {\n        const tmp = [];\n        for (let j = 0; j < n; j++) {\n            tmp.push(0);\n        }\n        numList.push(tmp);\n    }\n}\n
      space_complexity.ts
      /* Exponential order */\nfunction quadratic(n: number): void {\n    // Matrix uses O(n^2) space\n    const numMatrix = Array(n)\n        .fill(null)\n        .map(() => Array(n).fill(null));\n    // 2D list uses O(n^2) space\n    const numList = [];\n    for (let i = 0; i < n; i++) {\n        const tmp = [];\n        for (let j = 0; j < n; j++) {\n            tmp.push(0);\n        }\n        numList.push(tmp);\n    }\n}\n
      space_complexity.dart
      /* Exponential order */\nvoid quadratic(int n) {\n  // Matrix uses O(n^2) space\n  List<List<int>> numMatrix = List.generate(n, (_) => List.filled(n, 0));\n  // 2D list uses O(n^2) space\n  List<List<int>> numList = [];\n  for (var i = 0; i < n; i++) {\n    List<int> tmp = [];\n    for (int j = 0; j < n; j++) {\n      tmp.add(0);\n    }\n    numList.add(tmp);\n  }\n}\n
      space_complexity.rs
      /* Exponential order */\n#[allow(unused)]\nfn quadratic(n: i32) {\n    // Matrix uses O(n^2) space\n    let num_matrix = vec![vec![0; n as usize]; n as usize];\n    // 2D list uses O(n^2) space\n    let mut num_list = Vec::new();\n    for i in 0..n {\n        let mut tmp = Vec::new();\n        for j in 0..n {\n            tmp.push(0);\n        }\n        num_list.push(tmp);\n    }\n}\n
      space_complexity.c
      /* Exponential order */\nvoid quadratic(int n) {\n    // 2D list uses O(n^2) space\n    int **numMatrix = malloc(sizeof(int *) * n);\n    for (int i = 0; i < n; i++) {\n        int *tmp = malloc(sizeof(int) * n);\n        for (int j = 0; j < n; j++) {\n            tmp[j] = 0;\n        }\n        numMatrix[i] = tmp;\n    }\n\n    // Memory release\n    for (int i = 0; i < n; i++) {\n        free(numMatrix[i]);\n    }\n    free(numMatrix);\n}\n
      space_complexity.kt
      /* Exponential order */\nfun quadratic(n: Int) {\n    // Matrix uses O(n^2) space\n    val numMatrix = arrayOfNulls<Array<Int>?>(n)\n    // 2D list uses O(n^2) space\n    val numList = mutableListOf<MutableList<Int>>()\n    for (i in 0..<n) {\n        val tmp = mutableListOf<Int>()\n        for (j in 0..<n) {\n            tmp.add(0)\n        }\n        numList.add(tmp)\n    }\n}\n
      space_complexity.rb
      ### Quadratic time ###\ndef quadratic(n)\n  # 2D list uses O(n^2) space\n  Array.new(n) { Array.new(n, 0) }\nend\n

      As shown in the following figure, the recursion depth of this function is \\(n\\), and an array is initialized in each recursive function with lengths of \\(n\\), \\(n-1\\), \\(\\dots\\), \\(2\\), \\(1\\), with an average length of \\(n / 2\\), thus occupying \\(O(n^2)\\) space overall:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
      def quadratic_recur(n: int) -> int:\n    \"\"\"Quadratic order (recursive implementation)\"\"\"\n    if n <= 0:\n        return 0\n    # Array nums length is n, n-1, ..., 2, 1\n    nums = [0] * n\n    return quadratic_recur(n - 1)\n
      space_complexity.cpp
      /* Quadratic order (recursive implementation) */\nint quadraticRecur(int n) {\n    if (n <= 0)\n        return 0;\n    vector<int> nums(n);\n    cout << \"In recursion n = \" << n << \", nums length = \" << nums.size() << endl;\n    return quadraticRecur(n - 1);\n}\n
      space_complexity.java
      /* Quadratic order (recursive implementation) */\nint quadraticRecur(int n) {\n    if (n <= 0)\n        return 0;\n    // Array nums has length n, n-1, ..., 2, 1\n    int[] nums = new int[n];\n    System.out.println(\"In recursion n = \" + n + \", nums length = \" + nums.length);\n    return quadraticRecur(n - 1);\n}\n
      space_complexity.cs
      /* Quadratic order (recursive implementation) */\nint QuadraticRecur(int n) {\n    if (n <= 0) return 0;\n    int[] nums = new int[n];\n    Console.WriteLine(\"Recursion n = \" + n + \", nums length = \" + nums.Length);\n    return QuadraticRecur(n - 1);\n}\n
      space_complexity.go
      /* Quadratic order (recursive implementation) */\nfunc spaceQuadraticRecur(n int) int {\n    if n <= 0 {\n        return 0\n    }\n    nums := make([]int, n)\n    fmt.Printf(\"In recursion n = %d, nums length = %d \\n\", n, len(nums))\n    return spaceQuadraticRecur(n - 1)\n}\n
      space_complexity.swift
      /* Quadratic order (recursive implementation) */\n@discardableResult\nfunc quadraticRecur(n: Int) -> Int {\n    if n <= 0 {\n        return 0\n    }\n    // Array nums has length n, n-1, ..., 2, 1\n    let nums = Array(repeating: 0, count: n)\n    print(\"In recursion n = \\(n), nums length = \\(nums.count)\")\n    return quadraticRecur(n: n - 1)\n}\n
      space_complexity.js
      /* Quadratic order (recursive implementation) */\nfunction quadraticRecur(n) {\n    if (n <= 0) return 0;\n    const nums = new Array(n);\n    console.log(`In recursion n = ${n}, nums length = ${nums.length}`);\n    return quadraticRecur(n - 1);\n}\n
      space_complexity.ts
      /* Quadratic order (recursive implementation) */\nfunction quadraticRecur(n: number): number {\n    if (n <= 0) return 0;\n    const nums = new Array(n);\n    console.log(`In recursion n = ${n}, nums length = ${nums.length}`);\n    return quadraticRecur(n - 1);\n}\n
      space_complexity.dart
      /* Quadratic order (recursive implementation) */\nint quadraticRecur(int n) {\n  if (n <= 0) return 0;\n  List<int> nums = List.filled(n, 0);\n  print('In recursion n = $n, nums length = ${nums.length}');\n  return quadraticRecur(n - 1);\n}\n
      space_complexity.rs
      /* Quadratic order (recursive implementation) */\nfn quadratic_recur(n: i32) -> i32 {\n    if n <= 0 {\n        return 0;\n    };\n    // Array nums has length n, n-1, ..., 2, 1\n    let nums = vec![0; n as usize];\n    println!(\"In recursion n = {}, nums length = {}\", n, nums.len());\n    return quadratic_recur(n - 1);\n}\n
      space_complexity.c
      /* Quadratic order (recursive implementation) */\nint quadraticRecur(int n) {\n    if (n <= 0)\n        return 0;\n    int *nums = malloc(sizeof(int) * n);\n    printf(\"In recursion n = %d, nums length = %d\\r\\n\", n, n);\n    int res = quadraticRecur(n - 1);\n    free(nums);\n    return res;\n}\n
      space_complexity.kt
      /* Quadratic order (recursive implementation) */\ntailrec fun quadraticRecur(n: Int): Int {\n    if (n <= 0)\n        return 0\n    // Array nums has length n, n-1, ..., 2, 1\n    val nums = Array(n) { 0 }\n    println(\"In recursion n = $n, nums length = ${nums.size}\")\n    return quadraticRecur(n - 1)\n}\n
      space_complexity.rb
      ### Quadratic space (recursive) ###\ndef quadratic_recur(n)\n  return 0 unless n > 0\n\n  # Array nums has length n, n-1, ..., 2, 1\n  nums = Array.new(n, 0)\n  quadratic_recur(n - 1)\nend\n

      Figure 2-18 \u00a0 Quadratic order space complexity generated by recursive function

      "},{"location":"chapter_computational_complexity/space_complexity/#4-exponential-order-o2n","title":"4. \u00a0 Exponential Order \\(O(2^n)\\)","text":"

      Exponential order is common in binary trees. Observe the following figure: a \"full binary tree\" with \\(n\\) levels has \\(2^n - 1\\) nodes, occupying \\(O(2^n)\\) space:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
      def build_tree(n: int) -> TreeNode | None:\n    \"\"\"Exponential order (build full binary tree)\"\"\"\n    if n == 0:\n        return None\n    root = TreeNode(0)\n    root.left = build_tree(n - 1)\n    root.right = build_tree(n - 1)\n    return root\n
      space_complexity.cpp
      /* Driver Code */\nTreeNode *buildTree(int n) {\n    if (n == 0)\n        return nullptr;\n    TreeNode *root = new TreeNode(0);\n    root->left = buildTree(n - 1);\n    root->right = buildTree(n - 1);\n    return root;\n}\n
      space_complexity.java
      /* Driver Code */\nTreeNode buildTree(int n) {\n    if (n == 0)\n        return null;\n    TreeNode root = new TreeNode(0);\n    root.left = buildTree(n - 1);\n    root.right = buildTree(n - 1);\n    return root;\n}\n
      space_complexity.cs
      /* Driver Code */\nTreeNode? BuildTree(int n) {\n    if (n == 0) return null;\n    TreeNode root = new(0) {\n        left = BuildTree(n - 1),\n        right = BuildTree(n - 1)\n    };\n    return root;\n}\n
      space_complexity.go
      /* Driver Code */\nfunc buildTree(n int) *TreeNode {\n    if n == 0 {\n        return nil\n    }\n    root := NewTreeNode(0)\n    root.Left = buildTree(n - 1)\n    root.Right = buildTree(n - 1)\n    return root\n}\n
      space_complexity.swift
      /* Driver Code */\nfunc buildTree(n: Int) -> TreeNode? {\n    if n == 0 {\n        return nil\n    }\n    let root = TreeNode(x: 0)\n    root.left = buildTree(n: n - 1)\n    root.right = buildTree(n: n - 1)\n    return root\n}\n
      space_complexity.js
      /* Driver Code */\nfunction buildTree(n) {\n    if (n === 0) return null;\n    const root = new TreeNode(0);\n    root.left = buildTree(n - 1);\n    root.right = buildTree(n - 1);\n    return root;\n}\n
      space_complexity.ts
      /* Driver Code */\nfunction buildTree(n: number): TreeNode | null {\n    if (n === 0) return null;\n    const root = new TreeNode(0);\n    root.left = buildTree(n - 1);\n    root.right = buildTree(n - 1);\n    return root;\n}\n
      space_complexity.dart
      /* Driver Code */\nTreeNode? buildTree(int n) {\n  if (n == 0) return null;\n  TreeNode root = TreeNode(0);\n  root.left = buildTree(n - 1);\n  root.right = buildTree(n - 1);\n  return root;\n}\n
      space_complexity.rs
      /* Driver Code */\nfn build_tree(n: i32) -> Option<Rc<RefCell<TreeNode>>> {\n    if n == 0 {\n        return None;\n    };\n    let root = TreeNode::new(0);\n    root.borrow_mut().left = build_tree(n - 1);\n    root.borrow_mut().right = build_tree(n - 1);\n    return Some(root);\n}\n
      space_complexity.c
      /* Driver Code */\nTreeNode *buildTree(int n) {\n    if (n == 0)\n        return NULL;\n    TreeNode *root = newTreeNode(0);\n    root->left = buildTree(n - 1);\n    root->right = buildTree(n - 1);\n    return root;\n}\n
      space_complexity.kt
      /* Driver Code */\nfun buildTree(n: Int): TreeNode? {\n    if (n == 0)\n        return null\n    val root = TreeNode(0)\n    root.left = buildTree(n - 1)\n    root.right = buildTree(n - 1)\n    return root\n}\n
      space_complexity.rb
      ### Exponential space (build full binary tree) ###\ndef build_tree(n)\n  return if n == 0\n\n  TreeNode.new.tap do |root|\n    root.left = build_tree(n - 1)\n    root.right = build_tree(n - 1)\n  end\nend\n

      Figure 2-19 \u00a0 Exponential order space complexity generated by full binary tree

      "},{"location":"chapter_computational_complexity/space_complexity/#5-logarithmic-order-olog-n","title":"5. \u00a0 Logarithmic Order \\(O(\\log n)\\)","text":"

      Logarithmic order is common in divide-and-conquer algorithms. For example, merge sort: given an input array of length \\(n\\), each recursion divides the array in half from the midpoint, forming a recursion tree of height \\(\\log n\\), using \\(O(\\log n)\\) stack frame space.

      Another example is converting a number to a string. Given a positive integer \\(n\\), it has \\(\\lfloor \\log_{10} n \\rfloor + 1\\) digits, i.e., the corresponding string length is \\(\\lfloor \\log_{10} n \\rfloor + 1\\), so the space complexity is \\(O(\\log_{10} n + 1) = O(\\log n)\\).

      "},{"location":"chapter_computational_complexity/space_complexity/#244-trading-time-for-space","title":"2.4.4 \u00a0 Trading Time for Space","text":"

      Ideally, we hope that both the time complexity and space complexity of an algorithm can reach optimal. However, in practice, optimizing both time complexity and space complexity simultaneously is usually very difficult.

      Reducing time complexity usually comes at the cost of increasing space complexity, and vice versa. The approach of sacrificing memory space to improve algorithm execution speed is called \"trading space for time\"; conversely, it is called \"trading time for space\".

      The choice of which approach depends on which aspect we value more. In most cases, time is more precious than space, so \"trading space for time\" is usually the more common strategy. Of course, when the data volume is very large, controlling space complexity is also very important.

      "},{"location":"chapter_computational_complexity/summary/","title":"2.5 \u00a0 Summary","text":""},{"location":"chapter_computational_complexity/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"

      Algorithm Efficiency Assessment

      • Time efficiency and space efficiency are the two primary evaluation metrics for measuring algorithm performance.
      • We can evaluate algorithm efficiency through actual testing, but it is difficult to eliminate the influence of the testing environment, and it consumes substantial computational resources.
      • Complexity analysis can eliminate the drawbacks of actual testing, with results applicable to all running platforms, and it can reveal algorithm efficiency under different data scales.

      Time Complexity

      • Time complexity is used to measure the trend of algorithm runtime as data volume increases. It can effectively evaluate algorithm efficiency, but may fail in certain situations, such as when the input data volume is small or when time complexities are identical, making it impossible to precisely compare algorithm efficiency.
      • Worst-case time complexity is represented using Big \\(O\\) notation, corresponding to the asymptotic upper bound of a function, reflecting the growth level of the number of operations \\(T(n)\\) as \\(n\\) approaches positive infinity.
      • Deriving time complexity involves two steps: first, counting the number of operations, then determining the asymptotic upper bound.
      • Common time complexities arranged from low to high include \\(O(1)\\), \\(O(\\log n)\\), \\(O(n)\\), \\(O(n \\log n)\\), \\(O(n^2)\\), \\(O(2^n)\\), and \\(O(n!)\\).
      • The time complexity of some algorithms is not fixed, but rather depends on the distribution of input data. Time complexity is divided into worst-case, best-case, and average-case time complexity. Best-case time complexity is rarely used because input data generally needs to satisfy strict conditions to achieve the best case.
      • Average time complexity reflects the algorithm's runtime efficiency under random data input, and is closest to the algorithm's performance in practical applications. Calculating average time complexity requires statistical analysis of input data distribution and the combined mathematical expectation.

      Space Complexity

      • Space complexity serves a similar purpose to time complexity, used to measure the trend of algorithm memory usage as data volume increases.
      • The memory space related to algorithm execution can be divided into input space, temporary space, and output space. Typically, input space is not included in space complexity calculations. Temporary space can be divided into temporary data, stack frame space, and instruction space, where stack frame space usually affects space complexity only in recursive functions.
      • We typically only focus on worst-case space complexity, which is the space complexity of an algorithm under worst-case input data and worst-case runtime.
      • Common space complexities arranged from low to high include \\(O(1)\\), \\(O(\\log n)\\), \\(O(n)\\), \\(O(n^2)\\), and \\(O(2^n)\\).
      "},{"location":"chapter_computational_complexity/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

      Q: Is the space complexity of tail recursion \\(O(1)\\)?

      Theoretically, the space complexity of tail recursive functions can be optimized to \\(O(1)\\). However, most programming languages (such as Java, Python, C++, Go, C#, etc.) do not support automatic tail recursion optimization, so the space complexity is generally considered to be \\(O(n)\\).

      Q: What is the difference between the terms function and method?

      A function can be executed independently, with all parameters passed explicitly. A method is associated with an object, is implicitly passed to the object that invokes it, and can operate on data contained in class instances.

      The following examples use several common programming languages for illustration.

      • C is a procedural programming language without object-oriented concepts, so it only has functions. However, we can simulate object-oriented programming by creating structures (struct), and functions associated with structures are equivalent to methods in other programming languages.
      • Java and C# are object-oriented programming languages where code blocks (methods) are typically part of a class. Static methods behave like functions because they are bound to the class and cannot access specific instance variables.
      • C++ and Python support both procedural programming (functions) and object-oriented programming (methods).

      Q: Does the diagram for \"common space complexity types\" reflect the absolute size of occupied space?

      No, the diagram shows space complexity, which reflects growth trends rather than the absolute size of occupied space.

      Assuming \\(n = 8\\), you might find that the values of each curve do not correspond to the functions. This is because each curve contains a constant term used to compress the value range into a visually comfortable range.

      In practice, because we generally do not know what the \"constant term\" complexity of each method is, we usually cannot select the optimal solution for \\(n = 8\\) based on complexity alone. But for \\(n = 8^5\\), the choice is straightforward, as the growth trend already dominates.

      Q: Are there situations where algorithms are designed to sacrifice time (or space) based on actual use cases?

      In practical applications, most situations choose to sacrifice space for time. For example, with database indexes, we typically choose to build B+ trees or hash indexes, occupying substantial memory space in exchange for efficient queries of \\(O(\\log n)\\) or even \\(O(1)\\).

      In scenarios where space resources are precious, time may be sacrificed for space. For example, in embedded development, device memory is precious, and engineers may forgo using hash tables and choose to use array sequential search to save memory usage, at the cost of slower searches.

      "},{"location":"chapter_computational_complexity/time_complexity/","title":"2.3 \u00a0 Time Complexity","text":"

      Runtime can intuitively and accurately reflect the efficiency of an algorithm. If we want to accurately estimate the runtime of a piece of code, how should we proceed?

      1. Determine the running platform, including hardware configuration, programming language, system environment, etc., as these factors all affect code execution efficiency.
      2. Evaluate the runtime required for various computational operations, for example, an addition operation + requires 1 ns, a multiplication operation * requires 10 ns, a print operation print() requires 5 ns, etc.
      3. Count all computational operations in the code, and sum the execution times of all operations to obtain the runtime.

      For example, in the following code, the input data size is \\(n\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      # On a certain running platform\ndef algorithm(n: int):\n    a = 2      # 1 ns\n    a = a + 1  # 1 ns\n    a = a * 2  # 10 ns\n    # Loop n times\n    for _ in range(n):  # 1 ns\n        print(0)        # 5 ns\n
      // On a certain running platform\nvoid algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // Loop n times\n    for (int i = 0; i < n; i++) {  // 1 ns\n        cout << 0 << endl;         // 5 ns\n    }\n}\n
      // On a certain running platform\nvoid algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // Loop n times\n    for (int i = 0; i < n; i++) {  // 1 ns\n        System.out.println(0);     // 5 ns\n    }\n}\n
      // On a certain running platform\nvoid Algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // Loop n times\n    for (int i = 0; i < n; i++) {  // 1 ns\n        Console.WriteLine(0);      // 5 ns\n    }\n}\n
      // On a certain running platform\nfunc algorithm(n int) {\n    a := 2     // 1 ns\n    a = a + 1  // 1 ns\n    a = a * 2  // 10 ns\n    // Loop n times\n    for i := 0; i < n; i++ {  // 1 ns\n        fmt.Println(a)        // 5 ns\n    }\n}\n
      // On a certain running platform\nfunc algorithm(n: Int) {\n    var a = 2 // 1 ns\n    a = a + 1 // 1 ns\n    a = a * 2 // 10 ns\n    // Loop n times\n    for _ in 0 ..< n { // 1 ns\n        print(0) // 5 ns\n    }\n}\n
      // On a certain running platform\nfunction algorithm(n) {\n    var a = 2; // 1 ns\n    a = a + 1; // 1 ns\n    a = a * 2; // 10 ns\n    // Loop n times\n    for(let i = 0; i < n; i++) { // 1 ns\n        console.log(0); // 5 ns\n    }\n}\n
      // On a certain running platform\nfunction algorithm(n: number): void {\n    var a: number = 2; // 1 ns\n    a = a + 1; // 1 ns\n    a = a * 2; // 10 ns\n    // Loop n times\n    for(let i = 0; i < n; i++) { // 1 ns\n        console.log(0); // 5 ns\n    }\n}\n
      // On a certain running platform\nvoid algorithm(int n) {\n  int a = 2; // 1 ns\n  a = a + 1; // 1 ns\n  a = a * 2; // 10 ns\n  // Loop n times\n  for (int i = 0; i < n; i++) { // 1 ns\n    print(0); // 5 ns\n  }\n}\n
      // On a certain running platform\nfn algorithm(n: i32) {\n    let mut a = 2;      // 1 ns\n    a = a + 1;          // 1 ns\n    a = a * 2;          // 10 ns\n    // Loop n times\n    for _ in 0..n {     // 1 ns\n        println!(\"{}\", 0);  // 5 ns\n    }\n}\n
      // On a certain running platform\nvoid algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // Loop n times\n    for (int i = 0; i < n; i++) {   // 1 ns\n        printf(\"%d\", 0);            // 5 ns\n    }\n}\n
      // On a certain running platform\nfun algorithm(n: Int) {\n    var a = 2 // 1 ns\n    a = a + 1 // 1 ns\n    a = a * 2 // 10 ns\n    // Loop n times\n    for (i in 0..<n) {  // 1 ns\n        println(0)      // 5 ns\n    }\n}\n
      # On a certain running platform\ndef algorithm(n)\n    a = 2       # 1 ns\n    a = a + 1   # 1 ns\n    a = a * 2   # 10 ns\n    # Loop n times\n    (0...n).each do # 1 ns\n        puts 0      # 5 ns\n    end\nend\n

      According to the above method, the algorithm's runtime can be obtained as \\((6n + 12)\\) ns:

      \\[ 1 + 1 + 10 + (1 + 5) \\times n = 6n + 12 \\]

      In reality, however, counting an algorithm's runtime is neither reasonable nor realistic. First, we do not want to tie the estimated time to the running platform, because algorithms need to run on various different platforms. Second, it is difficult to know the runtime of each type of operation, which brings great difficulty to the estimation process.

      "},{"location":"chapter_computational_complexity/time_complexity/#231-counting-time-growth-trends","title":"2.3.1 \u00a0 Counting Time Growth Trends","text":"

      Time complexity analysis does not count the algorithm's runtime, but rather counts the growth trend of the algorithm's runtime as the data volume increases.

      The concept of \"time growth trend\" is rather abstract; let us understand it through an example. Suppose the input data size is \\(n\\), and given three algorithms A, B, and C:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      # Time complexity of algorithm A: constant order\ndef algorithm_A(n: int):\n    print(0)\n# Time complexity of algorithm B: linear order\ndef algorithm_B(n: int):\n    for _ in range(n):\n        print(0)\n# Time complexity of algorithm C: constant order\ndef algorithm_C(n: int):\n    for _ in range(1000000):\n        print(0)\n
      // Time complexity of algorithm A: constant order\nvoid algorithm_A(int n) {\n    cout << 0 << endl;\n}\n// Time complexity of algorithm B: linear order\nvoid algorithm_B(int n) {\n    for (int i = 0; i < n; i++) {\n        cout << 0 << endl;\n    }\n}\n// Time complexity of algorithm C: constant order\nvoid algorithm_C(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        cout << 0 << endl;\n    }\n}\n
      // Time complexity of algorithm A: constant order\nvoid algorithm_A(int n) {\n    System.out.println(0);\n}\n// Time complexity of algorithm B: linear order\nvoid algorithm_B(int n) {\n    for (int i = 0; i < n; i++) {\n        System.out.println(0);\n    }\n}\n// Time complexity of algorithm C: constant order\nvoid algorithm_C(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        System.out.println(0);\n    }\n}\n
      // Time complexity of algorithm A: constant order\nvoid AlgorithmA(int n) {\n    Console.WriteLine(0);\n}\n// Time complexity of algorithm B: linear order\nvoid AlgorithmB(int n) {\n    for (int i = 0; i < n; i++) {\n        Console.WriteLine(0);\n    }\n}\n// Time complexity of algorithm C: constant order\nvoid AlgorithmC(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        Console.WriteLine(0);\n    }\n}\n
      // Time complexity of algorithm A: constant order\nfunc algorithm_A(n int) {\n    fmt.Println(0)\n}\n// Time complexity of algorithm B: linear order\nfunc algorithm_B(n int) {\n    for i := 0; i < n; i++ {\n        fmt.Println(0)\n    }\n}\n// Time complexity of algorithm C: constant order\nfunc algorithm_C(n int) {\n    for i := 0; i < 1000000; i++ {\n        fmt.Println(0)\n    }\n}\n
      // Time complexity of algorithm A: constant order\nfunc algorithmA(n: Int) {\n    print(0)\n}\n\n// Time complexity of algorithm B: linear order\nfunc algorithmB(n: Int) {\n    for _ in 0 ..< n {\n        print(0)\n    }\n}\n\n// Time complexity of algorithm C: constant order\nfunc algorithmC(n: Int) {\n    for _ in 0 ..< 1_000_000 {\n        print(0)\n    }\n}\n
      // Time complexity of algorithm A: constant order\nfunction algorithm_A(n) {\n    console.log(0);\n}\n// Time complexity of algorithm B: linear order\nfunction algorithm_B(n) {\n    for (let i = 0; i < n; i++) {\n        console.log(0);\n    }\n}\n// Time complexity of algorithm C: constant order\nfunction algorithm_C(n) {\n    for (let i = 0; i < 1000000; i++) {\n        console.log(0);\n    }\n}\n
      // Time complexity of algorithm A: constant order\nfunction algorithm_A(n: number): void {\n    console.log(0);\n}\n// Time complexity of algorithm B: linear order\nfunction algorithm_B(n: number): void {\n    for (let i = 0; i < n; i++) {\n        console.log(0);\n    }\n}\n// Time complexity of algorithm C: constant order\nfunction algorithm_C(n: number): void {\n    for (let i = 0; i < 1000000; i++) {\n        console.log(0);\n    }\n}\n
      // Time complexity of algorithm A: constant order\nvoid algorithmA(int n) {\n  print(0);\n}\n// Time complexity of algorithm B: linear order\nvoid algorithmB(int n) {\n  for (int i = 0; i < n; i++) {\n    print(0);\n  }\n}\n// Time complexity of algorithm C: constant order\nvoid algorithmC(int n) {\n  for (int i = 0; i < 1000000; i++) {\n    print(0);\n  }\n}\n
      // Time complexity of algorithm A: constant order\nfn algorithm_A(n: i32) {\n    println!(\"{}\", 0);\n}\n// Time complexity of algorithm B: linear order\nfn algorithm_B(n: i32) {\n    for _ in 0..n {\n        println!(\"{}\", 0);\n    }\n}\n// Time complexity of algorithm C: constant order\nfn algorithm_C(n: i32) {\n    for _ in 0..1000000 {\n        println!(\"{}\", 0);\n    }\n}\n
      // Time complexity of algorithm A: constant order\nvoid algorithm_A(int n) {\n    printf(\"%d\", 0);\n}\n// Time complexity of algorithm B: linear order\nvoid algorithm_B(int n) {\n    for (int i = 0; i < n; i++) {\n        printf(\"%d\", 0);\n    }\n}\n// Time complexity of algorithm C: constant order\nvoid algorithm_C(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        printf(\"%d\", 0);\n    }\n}\n
      // Time complexity of algorithm A: constant order\nfun algoritm_A(n: Int) {\n    println(0)\n}\n// Time complexity of algorithm B: linear order\nfun algorithm_B(n: Int) {\n    for (i in 0..<n){\n        println(0)\n    }\n}\n// Time complexity of algorithm C: constant order\nfun algorithm_C(n: Int) {\n    for (i in 0..<1000000) {\n        println(0)\n    }\n}\n
      # Time complexity of algorithm A: constant order\ndef algorithm_A(n)\n    puts 0\nend\n\n# Time complexity of algorithm B: linear order\ndef algorithm_B(n)\n    (0...n).each { puts 0 }\nend\n\n# Time complexity of algorithm C: constant order\ndef algorithm_C(n)\n    (0...1_000_000).each { puts 0 }\nend\n

      Figure 2-7 shows the time complexity of the above three algorithm functions.

      • Algorithm A has only \\(1\\) print operation, and the algorithm's runtime does not grow as \\(n\\) increases. We call the time complexity of this algorithm \"constant order\".
      • In algorithm B, the print operation needs to loop \\(n\\) times, and the algorithm's runtime grows linearly as \\(n\\) increases. The time complexity of this algorithm is called \"linear order\".
      • In algorithm C, the print operation needs to loop \\(1000000\\) times. Although the runtime is very long, it is independent of the input data size \\(n\\). Therefore, the time complexity of C is the same as A, still \"constant order\".

      Figure 2-7 \u00a0 Time growth trends of algorithms A, B, and C

      Compared to directly counting the algorithm's runtime, what are the characteristics of time complexity analysis?

      • Time complexity can effectively evaluate algorithm efficiency. For example, the runtime of algorithm B grows linearly; when \\(n > 1\\) it is slower than algorithm A, and when \\(n > 1000000\\) it is slower than algorithm C. In fact, as long as the input data size \\(n\\) is sufficiently large, an algorithm with \"constant order\" complexity will always be superior to one with \"linear order\" complexity, which is precisely the meaning of time growth trend.
      • The derivation method for time complexity is simpler. Obviously, the running platform and the types of computational operations are both unrelated to the growth trend of the algorithm's runtime. Therefore, in time complexity analysis, we can simply treat the execution time of all computational operations as the same \"unit time\", thus simplifying \"counting computational operation runtime\" to \"counting the number of computational operations\", which greatly reduces the difficulty of estimation.
      • Time complexity also has certain limitations. For example, although algorithms A and C have the same time complexity, their actual runtimes differ significantly. Similarly, although algorithm B has a higher time complexity than C, when the input data size \\(n\\) is small, algorithm B is clearly superior to algorithm C. In such cases, it is often difficult to judge the efficiency of algorithms based solely on time complexity. Of course, despite the above issues, complexity analysis remains the most effective and commonly used method for evaluating algorithm efficiency.
      "},{"location":"chapter_computational_complexity/time_complexity/#232-asymptotic-upper-bound-of-functions","title":"2.3.2 \u00a0 Asymptotic Upper Bound of Functions","text":"

      Given a function with input size \\(n\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      def algorithm(n: int):\n    a = 1      # +1\n    a = a + 1  # +1\n    a = a * 2  # +1\n    # Loop n times\n    for i in range(n):  # +1\n        print(0)        # +1\n
      void algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // Loop n times\n    for (int i = 0; i < n; i++) { // +1 (i++ is executed each round)\n        cout << 0 << endl;    // +1\n    }\n}\n
      void algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // Loop n times\n    for (int i = 0; i < n; i++) { // +1 (i++ is executed each round)\n        System.out.println(0);    // +1\n    }\n}\n
      void Algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // Loop n times\n    for (int i = 0; i < n; i++) {   // +1 (i++ is executed each round)\n        Console.WriteLine(0);   // +1\n    }\n}\n
      func algorithm(n int) {\n    a := 1      // +1\n    a = a + 1   // +1\n    a = a * 2   // +1\n    // Loop n times\n    for i := 0; i < n; i++ {   // +1\n        fmt.Println(a)         // +1\n    }\n}\n
      func algorithm(n: Int) {\n    var a = 1 // +1\n    a = a + 1 // +1\n    a = a * 2 // +1\n    // Loop n times\n    for _ in 0 ..< n { // +1\n        print(0) // +1\n    }\n}\n
      function algorithm(n) {\n    var a = 1; // +1\n    a += 1; // +1\n    a *= 2; // +1\n    // Loop n times\n    for(let i = 0; i < n; i++){ // +1 (i++ is executed each round)\n        console.log(0); // +1\n    }\n}\n
      function algorithm(n: number): void{\n    var a: number = 1; // +1\n    a += 1; // +1\n    a *= 2; // +1\n    // Loop n times\n    for(let i = 0; i < n; i++){ // +1 (i++ is executed each round)\n        console.log(0); // +1\n    }\n}\n
      void algorithm(int n) {\n  int a = 1; // +1\n  a = a + 1; // +1\n  a = a * 2; // +1\n  // Loop n times\n  for (int i = 0; i < n; i++) { // +1 (i++ is executed each round)\n    print(0); // +1\n  }\n}\n
      fn algorithm(n: i32) {\n    let mut a = 1;   // +1\n    a = a + 1;      // +1\n    a = a * 2;      // +1\n\n    // Loop n times\n    for _ in 0..n { // +1 (i++ is executed each round)\n        println!(\"{}\", 0); // +1\n    }\n}\n
      void algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // Loop n times\n    for (int i = 0; i < n; i++) {   // +1 (i++ is executed each round)\n        printf(\"%d\", 0);            // +1\n    }\n}\n
      fun algorithm(n: Int) {\n    var a = 1 // +1\n    a = a + 1 // +1\n    a = a * 2 // +1\n    // Loop n times\n    for (i in 0..<n) { // +1 (i++ is executed each round)\n        println(0) // +1\n    }\n}\n
      def algorithm(n)\n    a = 1       # +1\n    a = a + 1   # +1\n    a = a * 2   # +1\n    # Loop n times\n    (0...n).each do # +1\n        puts 0      # +1\n    end\nend\n

      Let the number of operations of the algorithm be a function of the input data size \\(n\\), denoted as \\(T(n)\\). Then the number of operations of the above function is:

      \\[ T(n) = 3 + 2n \\]

      \\(T(n)\\) is a linear function, indicating that its runtime growth trend is linear, and therefore its time complexity is linear order.

      We denote the time complexity of linear order as \\(O(n)\\). This mathematical symbol is called big-\\(O\\) notation, representing the asymptotic upper bound of the function \\(T(n)\\).

      Time complexity analysis essentially calculates the asymptotic upper bound of \"the number of operations \\(T(n)\\)\", which has a clear mathematical definition.

      Asymptotic upper bound of functions

      If there exist positive real numbers \\(c\\) and \\(n_0\\) such that for all \\(n > n_0\\), we have \\(T(n) \\leq c \\cdot f(n)\\), then \\(f(n)\\) can be considered as an asymptotic upper bound of \\(T(n)\\), denoted as \\(T(n) = O(f(n))\\).

      As shown in Figure 2-8, calculating the asymptotic upper bound is to find a function \\(f(n)\\) such that when \\(n\\) tends to infinity, \\(T(n)\\) and \\(f(n)\\) are at the same growth level, differing only by a constant coefficient \\(c\\).

      Figure 2-8 \u00a0 Asymptotic upper bound of a function

      "},{"location":"chapter_computational_complexity/time_complexity/#233-derivation-method","title":"2.3.3 \u00a0 Derivation Method","text":"

      The asymptotic upper bound has a bit of mathematical flavor. If you feel you haven't fully understood it, don't worry. We can first master the derivation method, and gradually grasp its mathematical meaning through continuous practice.

      According to the definition, after determining \\(f(n)\\), we can obtain the time complexity \\(O(f(n))\\). So how do we determine the asymptotic upper bound \\(f(n)\\)? Overall, it is divided into two steps: first count the number of operations, then determine the asymptotic upper bound.

      "},{"location":"chapter_computational_complexity/time_complexity/#1-step-1-count-the-number-of-operations","title":"1. \u00a0 Step 1: Count the Number of Operations","text":"

      For code, count from top to bottom line by line. However, since the constant coefficient \\(c\\) in \\(c \\cdot f(n)\\) above can be of any size, coefficients and constant terms in the number of operations \\(T(n)\\) can all be ignored. According to this principle, the following counting simplification techniques can be summarized.

      1. Ignore constants in \\(T(n)\\). Because they are all independent of \\(n\\), they do not affect time complexity.
      2. Omit all coefficients. For example, looping \\(2n\\) times, \\(5n + 1\\) times, etc., can all be simplified as \\(n\\) times, because the coefficient before \\(n\\) does not affect time complexity.
      3. Use multiplication for nested loops. The total number of operations equals the product of the number of operations in the outer and inner loops, with each layer of loop still able to apply techniques 1. and 2. separately.

      Given a function, we can use the above techniques to count the number of operations:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      def algorithm(n: int):\n    a = 1      # +0 (Technique 1)\n    a = a + n  # +0 (Technique 1)\n    # +n (Technique 2)\n    for i in range(5 * n + 1):\n        print(0)\n    # +n*n (Technique 3)\n    for i in range(2 * n):\n        for j in range(n + 1):\n            print(0)\n
      void algorithm(int n) {\n    int a = 1;  // +0 (Technique 1)\n    a = a + n;  // +0 (Technique 1)\n    // +n (Technique 2)\n    for (int i = 0; i < 5 * n + 1; i++) {\n        cout << 0 << endl;\n    }\n    // +n*n (Technique 3)\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            cout << 0 << endl;\n        }\n    }\n}\n
      void algorithm(int n) {\n    int a = 1;  // +0 (Technique 1)\n    a = a + n;  // +0 (Technique 1)\n    // +n (Technique 2)\n    for (int i = 0; i < 5 * n + 1; i++) {\n        System.out.println(0);\n    }\n    // +n*n (Technique 3)\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            System.out.println(0);\n        }\n    }\n}\n
      void Algorithm(int n) {\n    int a = 1;  // +0 (Technique 1)\n    a = a + n;  // +0 (Technique 1)\n    // +n (Technique 2)\n    for (int i = 0; i < 5 * n + 1; i++) {\n        Console.WriteLine(0);\n    }\n    // +n*n (Technique 3)\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            Console.WriteLine(0);\n        }\n    }\n}\n
      func algorithm(n int) {\n    a := 1     // +0 (Technique 1)\n    a = a + n  // +0 (Technique 1)\n    // +n (Technique 2)\n    for i := 0; i < 5 * n + 1; i++ {\n        fmt.Println(0)\n    }\n    // +n*n (Technique 3)\n    for i := 0; i < 2 * n; i++ {\n        for j := 0; j < n + 1; j++ {\n            fmt.Println(0)\n        }\n    }\n}\n
      func algorithm(n: Int) {\n    var a = 1 // +0 (Technique 1)\n    a = a + n // +0 (Technique 1)\n    // +n (Technique 2)\n    for _ in 0 ..< (5 * n + 1) {\n        print(0)\n    }\n    // +n*n (Technique 3)\n    for _ in 0 ..< (2 * n) {\n        for _ in 0 ..< (n + 1) {\n            print(0)\n        }\n    }\n}\n
      function algorithm(n) {\n    let a = 1;  // +0 (Technique 1)\n    a = a + n;  // +0 (Technique 1)\n    // +n (Technique 2)\n    for (let i = 0; i < 5 * n + 1; i++) {\n        console.log(0);\n    }\n    // +n*n (Technique 3)\n    for (let i = 0; i < 2 * n; i++) {\n        for (let j = 0; j < n + 1; j++) {\n            console.log(0);\n        }\n    }\n}\n
      function algorithm(n: number): void {\n    let a = 1;  // +0 (Technique 1)\n    a = a + n;  // +0 (Technique 1)\n    // +n (Technique 2)\n    for (let i = 0; i < 5 * n + 1; i++) {\n        console.log(0);\n    }\n    // +n*n (Technique 3)\n    for (let i = 0; i < 2 * n; i++) {\n        for (let j = 0; j < n + 1; j++) {\n            console.log(0);\n        }\n    }\n}\n
      void algorithm(int n) {\n  int a = 1; // +0 (Technique 1)\n  a = a + n; // +0 (Technique 1)\n  // +n (Technique 2)\n  for (int i = 0; i < 5 * n + 1; i++) {\n    print(0);\n  }\n  // +n*n (Technique 3)\n  for (int i = 0; i < 2 * n; i++) {\n    for (int j = 0; j < n + 1; j++) {\n      print(0);\n    }\n  }\n}\n
      fn algorithm(n: i32) {\n    let mut a = 1;     // +0 (Technique 1)\n    a = a + n;        // +0 (Technique 1)\n\n    // +n (Technique 2)\n    for i in 0..(5 * n + 1) {\n        println!(\"{}\", 0);\n    }\n\n    // +n*n (Technique 3)\n    for i in 0..(2 * n) {\n        for j in 0..(n + 1) {\n            println!(\"{}\", 0);\n        }\n    }\n}\n
      void algorithm(int n) {\n    int a = 1;  // +0 (Technique 1)\n    a = a + n;  // +0 (Technique 1)\n    // +n (Technique 2)\n    for (int i = 0; i < 5 * n + 1; i++) {\n        printf(\"%d\", 0);\n    }\n    // +n*n (Technique 3)\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            printf(\"%d\", 0);\n        }\n    }\n}\n
      fun algorithm(n: Int) {\n    var a = 1   // +0 (Technique 1)\n    a = a + n   // +0 (Technique 1)\n    // +n (Technique 2)\n    for (i in 0..<5 * n + 1) {\n        println(0)\n    }\n    // +n*n (Technique 3)\n    for (i in 0..<2 * n) {\n        for (j in 0..<n + 1) {\n            println(0)\n        }\n    }\n}\n
      def algorithm(n)\n    a = 1       # +0 (Technique 1)\n    a = a + n   # +0 (Technique 1)\n    # +n (Technique 2)\n    (0...(5 * n + 1)).each do { puts 0 }\n    # +n*n (Technique 3)\n    (0...(2 * n)).each do\n        (0...(n + 1)).each do { puts 0 }\n    end\nend\n

      The following formula shows the counting results before and after using the above techniques; both derive a time complexity of \\(O(n^2)\\).

      \\[ \\begin{aligned} T(n) & = 2n(n + 1) + (5n + 1) + 2 & \\text{Complete count (-.-|||)} \\newline & = 2n^2 + 7n + 3 \\newline T(n) & = n^2 + n & \\text{Simplified count (o.O)} \\end{aligned} \\]"},{"location":"chapter_computational_complexity/time_complexity/#2-step-2-determine-the-asymptotic-upper-bound","title":"2. \u00a0 Step 2: Determine the Asymptotic Upper Bound","text":"

      Time complexity is determined by the highest-order term in \\(T(n)\\). This is because as \\(n\\) tends to infinity, the highest-order term will play a dominant role, and the influence of other terms can be ignored.

      Table 2-2 shows some examples, where some exaggerated values are used to emphasize the conclusion that \"coefficients cannot shake the order\". When \\(n\\) tends to infinity, these constants become insignificant.

      Table 2-2 \u00a0 Time complexities corresponding to different numbers of operations

      Number of Operations \\(T(n)\\) Time Complexity \\(O(f(n))\\) \\(100000\\) \\(O(1)\\) \\(3n + 2\\) \\(O(n)\\) \\(2n^2 + 3n + 2\\) \\(O(n^2)\\) \\(n^3 + 10000n^2\\) \\(O(n^3)\\) \\(2^n + 10000n^{10000}\\) \\(O(2^n)\\)"},{"location":"chapter_computational_complexity/time_complexity/#234-common-types","title":"2.3.4 \u00a0 Common Types","text":"

      Let the input data size be \\(n\\). Common time complexity types are shown in Figure 2-9 (arranged in order from low to high).

      \\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n \\log n) < O(n^2) < O(2^n) < O(n!) \\newline \\text{Constant order} < \\text{Logarithmic order} < \\text{Linear order} < \\text{Linearithmic order} < \\text{Quadratic order} < \\text{Exponential order} < \\text{Factorial order} \\end{aligned} \\]

      Figure 2-9 \u00a0 Common time complexity types

      "},{"location":"chapter_computational_complexity/time_complexity/#1-constant-order-o1","title":"1. \u00a0 Constant Order \\(O(1)\\)","text":"

      The number of operations in constant order is independent of the input data size \\(n\\), meaning it does not change as \\(n\\) changes.

      In the following function, although the number of operations size may be large, since it is independent of the input data size \\(n\\), the time complexity remains \\(O(1)\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
      def constant(n: int) -> int:\n    \"\"\"Constant order\"\"\"\n    count = 0\n    size = 100000\n    for _ in range(size):\n        count += 1\n    return count\n
      time_complexity.cpp
      /* Constant order */\nint constant(int n) {\n    int count = 0;\n    int size = 100000;\n    for (int i = 0; i < size; i++)\n        count++;\n    return count;\n}\n
      time_complexity.java
      /* Constant order */\nint constant(int n) {\n    int count = 0;\n    int size = 100000;\n    for (int i = 0; i < size; i++)\n        count++;\n    return count;\n}\n
      time_complexity.cs
      /* Constant order */\nint Constant(int n) {\n    int count = 0;\n    int size = 100000;\n    for (int i = 0; i < size; i++)\n        count++;\n    return count;\n}\n
      time_complexity.go
      /* Constant order */\nfunc constant(n int) int {\n    count := 0\n    size := 100000\n    for i := 0; i < size; i++ {\n        count++\n    }\n    return count\n}\n
      time_complexity.swift
      /* Constant order */\nfunc constant(n: Int) -> Int {\n    var count = 0\n    let size = 100_000\n    for _ in 0 ..< size {\n        count += 1\n    }\n    return count\n}\n
      time_complexity.js
      /* Constant order */\nfunction constant(n) {\n    let count = 0;\n    const size = 100000;\n    for (let i = 0; i < size; i++) count++;\n    return count;\n}\n
      time_complexity.ts
      /* Constant order */\nfunction constant(n: number): number {\n    let count = 0;\n    const size = 100000;\n    for (let i = 0; i < size; i++) count++;\n    return count;\n}\n
      time_complexity.dart
      /* Constant order */\nint constant(int n) {\n  int count = 0;\n  int size = 100000;\n  for (var i = 0; i < size; i++) {\n    count++;\n  }\n  return count;\n}\n
      time_complexity.rs
      /* Constant order */\nfn constant(n: i32) -> i32 {\n    _ = n;\n    let mut count = 0;\n    let size = 100_000;\n    for _ in 0..size {\n        count += 1;\n    }\n    count\n}\n
      time_complexity.c
      /* Constant order */\nint constant(int n) {\n    int count = 0;\n    int size = 100000;\n    int i = 0;\n    for (int i = 0; i < size; i++) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.kt
      /* Constant order */\nfun constant(n: Int): Int {\n    var count = 0\n    val size = 100000\n    for (i in 0..<size)\n        count++\n    return count\n}\n
      time_complexity.rb
      ### Constant time ###\ndef constant(n)\n  count = 0\n  size = 100000\n\n  (0...size).each { count += 1 }\n\n  count\nend\n
      "},{"location":"chapter_computational_complexity/time_complexity/#2-linear-order-on","title":"2. \u00a0 Linear Order \\(O(n)\\)","text":"

      The number of operations in linear order grows linearly relative to the input data size \\(n\\). Linear order typically appears in single-layer loops:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
      def linear(n: int) -> int:\n    \"\"\"Linear order\"\"\"\n    count = 0\n    for _ in range(n):\n        count += 1\n    return count\n
      time_complexity.cpp
      /* Linear order */\nint linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++)\n        count++;\n    return count;\n}\n
      time_complexity.java
      /* Linear order */\nint linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++)\n        count++;\n    return count;\n}\n
      time_complexity.cs
      /* Linear order */\nint Linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++)\n        count++;\n    return count;\n}\n
      time_complexity.go
      /* Linear order */\nfunc linear(n int) int {\n    count := 0\n    for i := 0; i < n; i++ {\n        count++\n    }\n    return count\n}\n
      time_complexity.swift
      /* Linear order */\nfunc linear(n: Int) -> Int {\n    var count = 0\n    for _ in 0 ..< n {\n        count += 1\n    }\n    return count\n}\n
      time_complexity.js
      /* Linear order */\nfunction linear(n) {\n    let count = 0;\n    for (let i = 0; i < n; i++) count++;\n    return count;\n}\n
      time_complexity.ts
      /* Linear order */\nfunction linear(n: number): number {\n    let count = 0;\n    for (let i = 0; i < n; i++) count++;\n    return count;\n}\n
      time_complexity.dart
      /* Linear order */\nint linear(int n) {\n  int count = 0;\n  for (var i = 0; i < n; i++) {\n    count++;\n  }\n  return count;\n}\n
      time_complexity.rs
      /* Linear order */\nfn linear(n: i32) -> i32 {\n    let mut count = 0;\n    for _ in 0..n {\n        count += 1;\n    }\n    count\n}\n
      time_complexity.c
      /* Linear order */\nint linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.kt
      /* Linear order */\nfun linear(n: Int): Int {\n    var count = 0\n    for (i in 0..<n)\n        count++\n    return count\n}\n
      time_complexity.rb
      ### Linear time ###\ndef linear(n)\n  count = 0\n  (0...n).each { count += 1 }\n  count\nend\n

      Operations such as traversing arrays and traversing linked lists have a time complexity of \\(O(n)\\), where \\(n\\) is the length of the array or linked list:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
      def array_traversal(nums: list[int]) -> int:\n    \"\"\"Linear order (traversing array)\"\"\"\n    count = 0\n    # Number of iterations is proportional to the array length\n    for num in nums:\n        count += 1\n    return count\n
      time_complexity.cpp
      /* Linear order (traversing array) */\nint arrayTraversal(vector<int> &nums) {\n    int count = 0;\n    // Number of iterations is proportional to the array length\n    for (int num : nums) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.java
      /* Linear order (traversing array) */\nint arrayTraversal(int[] nums) {\n    int count = 0;\n    // Number of iterations is proportional to the array length\n    for (int num : nums) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.cs
      /* Linear order (traversing array) */\nint ArrayTraversal(int[] nums) {\n    int count = 0;\n    // Number of iterations is proportional to the array length\n    foreach (int num in nums) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.go
      /* Linear order (traversing array) */\nfunc arrayTraversal(nums []int) int {\n    count := 0\n    // Number of iterations is proportional to the array length\n    for range nums {\n        count++\n    }\n    return count\n}\n
      time_complexity.swift
      /* Linear order (traversing array) */\nfunc arrayTraversal(nums: [Int]) -> Int {\n    var count = 0\n    // Number of iterations is proportional to the array length\n    for _ in nums {\n        count += 1\n    }\n    return count\n}\n
      time_complexity.js
      /* Linear order (traversing array) */\nfunction arrayTraversal(nums) {\n    let count = 0;\n    // Number of iterations is proportional to the array length\n    for (let i = 0; i < nums.length; i++) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.ts
      /* Linear order (traversing array) */\nfunction arrayTraversal(nums: number[]): number {\n    let count = 0;\n    // Number of iterations is proportional to the array length\n    for (let i = 0; i < nums.length; i++) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.dart
      /* Linear order (traversing array) */\nint arrayTraversal(List<int> nums) {\n  int count = 0;\n  // Number of iterations is proportional to the array length\n  for (var _num in nums) {\n    count++;\n  }\n  return count;\n}\n
      time_complexity.rs
      /* Linear order (traversing array) */\nfn array_traversal(nums: &[i32]) -> i32 {\n    let mut count = 0;\n    // Number of iterations is proportional to the array length\n    for _ in nums {\n        count += 1;\n    }\n    count\n}\n
      time_complexity.c
      /* Linear order (traversing array) */\nint arrayTraversal(int *nums, int n) {\n    int count = 0;\n    // Number of iterations is proportional to the array length\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.kt
      /* Linear order (traversing array) */\nfun arrayTraversal(nums: IntArray): Int {\n    var count = 0\n    // Number of iterations is proportional to the array length\n    for (num in nums) {\n        count++\n    }\n    return count\n}\n
      time_complexity.rb
      ### Linear time (array traversal) ###\ndef array_traversal(nums)\n  count = 0\n\n  # Number of iterations is proportional to the array length\n  for num in nums\n    count += 1\n  end\n\n  count\nend\n

      It is worth noting that the input data size \\(n\\) should be determined according to the type of input data. For example, in the first example, the variable \\(n\\) is the input data size; in the second example, the array length \\(n\\) is the data size.

      "},{"location":"chapter_computational_complexity/time_complexity/#3-quadratic-order-on2","title":"3. \u00a0 Quadratic Order \\(O(n^2)\\)","text":"

      The number of operations in quadratic order grows quadratically relative to the input data size \\(n\\). Quadratic order typically appears in nested loops, where both the outer and inner loops have a time complexity of \\(O(n)\\), resulting in an overall time complexity of \\(O(n^2)\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
      def quadratic(n: int) -> int:\n    \"\"\"Quadratic order\"\"\"\n    count = 0\n    # Number of iterations is quadratically related to the data size n\n    for i in range(n):\n        for j in range(n):\n            count += 1\n    return count\n
      time_complexity.cpp
      /* Exponential order */\nint quadratic(int n) {\n    int count = 0;\n    // Number of iterations is quadratically related to the data size n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
      time_complexity.java
      /* Exponential order */\nint quadratic(int n) {\n    int count = 0;\n    // Number of iterations is quadratically related to the data size n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
      time_complexity.cs
      /* Exponential order */\nint Quadratic(int n) {\n    int count = 0;\n    // Number of iterations is quadratically related to the data size n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
      time_complexity.go
      /* Exponential order */\nfunc quadratic(n int) int {\n    count := 0\n    // Number of iterations is quadratically related to the data size n\n    for i := 0; i < n; i++ {\n        for j := 0; j < n; j++ {\n            count++\n        }\n    }\n    return count\n}\n
      time_complexity.swift
      /* Exponential order */\nfunc quadratic(n: Int) -> Int {\n    var count = 0\n    // Number of iterations is quadratically related to the data size n\n    for _ in 0 ..< n {\n        for _ in 0 ..< n {\n            count += 1\n        }\n    }\n    return count\n}\n
      time_complexity.js
      /* Exponential order */\nfunction quadratic(n) {\n    let count = 0;\n    // Number of iterations is quadratically related to the data size n\n    for (let i = 0; i < n; i++) {\n        for (let j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
      time_complexity.ts
      /* Exponential order */\nfunction quadratic(n: number): number {\n    let count = 0;\n    // Number of iterations is quadratically related to the data size n\n    for (let i = 0; i < n; i++) {\n        for (let j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
      time_complexity.dart
      /* Exponential order */\nint quadratic(int n) {\n  int count = 0;\n  // Number of iterations is quadratically related to the data size n\n  for (int i = 0; i < n; i++) {\n    for (int j = 0; j < n; j++) {\n      count++;\n    }\n  }\n  return count;\n}\n
      time_complexity.rs
      /* Exponential order */\nfn quadratic(n: i32) -> i32 {\n    let mut count = 0;\n    // Number of iterations is quadratically related to the data size n\n    for _ in 0..n {\n        for _ in 0..n {\n            count += 1;\n        }\n    }\n    count\n}\n
      time_complexity.c
      /* Exponential order */\nint quadratic(int n) {\n    int count = 0;\n    // Number of iterations is quadratically related to the data size n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
      time_complexity.kt
      /* Exponential order */\nfun quadratic(n: Int): Int {\n    var count = 0\n    // Number of iterations is quadratically related to the data size n\n    for (i in 0..<n) {\n        for (j in 0..<n) {\n            count++\n        }\n    }\n    return count\n}\n
      time_complexity.rb
      ### Quadratic time ###\ndef quadratic(n)\n  count = 0\n\n  # Number of iterations is quadratically related to the data size n\n  for i in 0...n\n    for j in 0...n\n      count += 1\n    end\n  end\n\n  count\nend\n

      Figure 2-10 compares constant order, linear order, and quadratic order time complexities.

      Figure 2-10 \u00a0 Time complexities of constant, linear, and quadratic orders

      Taking bubble sort as an example, the outer loop executes \\(n - 1\\) times, and the inner loop executes \\(n-1\\), \\(n-2\\), \\(\\dots\\), \\(2\\), \\(1\\) times, averaging \\(n / 2\\) times, resulting in a time complexity of \\(O((n - 1) n / 2) = O(n^2)\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
      def bubble_sort(nums: list[int]) -> int:\n    \"\"\"Quadratic order (bubble sort)\"\"\"\n    count = 0  # Counter\n    # Outer loop: unsorted range is [0, i]\n    for i in range(len(nums) - 1, 0, -1):\n        # Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for j in range(i):\n            if nums[j] > nums[j + 1]:\n                # Swap nums[j] and nums[j + 1]\n                tmp: int = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = tmp\n                count += 3  # Element swap includes 3 unit operations\n    return count\n
      time_complexity.cpp
      /* Quadratic order (bubble sort) */\nint bubbleSort(vector<int> &nums) {\n    int count = 0; // Counter\n    // Outer loop: unsorted range is [0, i]\n    for (int i = nums.size() - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // Element swap includes 3 unit operations\n            }\n        }\n    }\n    return count;\n}\n
      time_complexity.java
      /* Quadratic order (bubble sort) */\nint bubbleSort(int[] nums) {\n    int count = 0; // Counter\n    // Outer loop: unsorted range is [0, i]\n    for (int i = nums.length - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // Element swap includes 3 unit operations\n            }\n        }\n    }\n    return count;\n}\n
      time_complexity.cs
      /* Quadratic order (bubble sort) */\nint BubbleSort(int[] nums) {\n    int count = 0;  // Counter\n    // Outer loop: unsorted range is [0, i]\n    for (int i = nums.Length - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n                count += 3;  // Element swap includes 3 unit operations\n            }\n        }\n    }\n    return count;\n}\n
      time_complexity.go
      /* Quadratic order (bubble sort) */\nfunc bubbleSort(nums []int) int {\n    count := 0 // Counter\n    // Outer loop: unsorted range is [0, i]\n    for i := len(nums) - 1; i > 0; i-- {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for j := 0; j < i; j++ {\n            if nums[j] > nums[j+1] {\n                // Swap nums[j] and nums[j + 1]\n                tmp := nums[j]\n                nums[j] = nums[j+1]\n                nums[j+1] = tmp\n                count += 3 // Element swap includes 3 unit operations\n            }\n        }\n    }\n    return count\n}\n
      time_complexity.swift
      /* Quadratic order (bubble sort) */\nfunc bubbleSort(nums: inout [Int]) -> Int {\n    var count = 0 // Counter\n    // Outer loop: unsorted range is [0, i]\n    for i in nums.indices.dropFirst().reversed() {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for j in 0 ..< i {\n            if nums[j] > nums[j + 1] {\n                // Swap nums[j] and nums[j + 1]\n                let tmp = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = tmp\n                count += 3 // Element swap includes 3 unit operations\n            }\n        }\n    }\n    return count\n}\n
      time_complexity.js
      /* Quadratic order (bubble sort) */\nfunction bubbleSort(nums) {\n    let count = 0; // Counter\n    // Outer loop: unsorted range is [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // Element swap includes 3 unit operations\n            }\n        }\n    }\n    return count;\n}\n
      time_complexity.ts
      /* Quadratic order (bubble sort) */\nfunction bubbleSort(nums: number[]): number {\n    let count = 0; // Counter\n    // Outer loop: unsorted range is [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // Element swap includes 3 unit operations\n            }\n        }\n    }\n    return count;\n}\n
      time_complexity.dart
      /* Quadratic order (bubble sort) */\nint bubbleSort(List<int> nums) {\n  int count = 0; // Counter\n  // Outer loop: unsorted range is [0, i]\n  for (var i = nums.length - 1; i > 0; i--) {\n    // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n    for (var j = 0; j < i; j++) {\n      if (nums[j] > nums[j + 1]) {\n        // Swap nums[j] and nums[j + 1]\n        int tmp = nums[j];\n        nums[j] = nums[j + 1];\n        nums[j + 1] = tmp;\n        count += 3; // Element swap includes 3 unit operations\n      }\n    }\n  }\n  return count;\n}\n
      time_complexity.rs
      /* Quadratic order (bubble sort) */\nfn bubble_sort(nums: &mut [i32]) -> i32 {\n    let mut count = 0; // Counter\n\n    // Outer loop: unsorted range is [0, i]\n    for i in (1..nums.len()).rev() {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for j in 0..i {\n            if nums[j] > nums[j + 1] {\n                // Swap nums[j] and nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // Element swap includes 3 unit operations\n            }\n        }\n    }\n    count\n}\n
      time_complexity.c
      /* Quadratic order (bubble sort) */\nint bubbleSort(int *nums, int n) {\n    int count = 0; // Counter\n    // Outer loop: unsorted range is [0, i]\n    for (int i = n - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // Element swap includes 3 unit operations\n            }\n        }\n    }\n    return count;\n}\n
      time_complexity.kt
      /* Quadratic order (bubble sort) */\nfun bubbleSort(nums: IntArray): Int {\n    var count = 0 // Counter\n    // Outer loop: unsorted range is [0, i]\n    for (i in nums.size - 1 downTo 1) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (j in 0..<i) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                val temp = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = temp\n                count += 3 // Element swap includes 3 unit operations\n            }\n        }\n    }\n    return count\n}\n
      time_complexity.rb
      ### Quadratic time (bubble sort) ###\ndef bubble_sort(nums)\n  count = 0  # Counter\n\n  # Outer loop: unsorted range is [0, i]\n  for i in (nums.length - 1).downto(0)\n    # Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n    for j in 0...i\n      if nums[j] > nums[j + 1]\n        # Swap nums[j] and nums[j + 1]\n        tmp = nums[j]\n        nums[j] = nums[j + 1]\n        nums[j + 1] = tmp\n        count += 3 # Element swap includes 3 unit operations\n      end\n    end\n  end\n\n  count\nend\n
      "},{"location":"chapter_computational_complexity/time_complexity/#4-exponential-order-o2n","title":"4. \u00a0 Exponential Order \\(O(2^n)\\)","text":"

      Biological \"cell division\" is a typical example of exponential order growth: the initial state is \\(1\\) cell, after one round of division it becomes \\(2\\), after two rounds it becomes \\(4\\), and so on; after \\(n\\) rounds of division there are \\(2^n\\) cells.

      Figure 2-11 and the following code simulate the cell division process, with a time complexity of \\(O(2^n)\\). Note that the input \\(n\\) represents the number of division rounds, and the return value count represents the total number of divisions.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
      def exponential(n: int) -> int:\n    \"\"\"Exponential order (loop implementation)\"\"\"\n    count = 0\n    base = 1\n    # Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n    for _ in range(n):\n        for _ in range(base):\n            count += 1\n        base *= 2\n    # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count\n
      time_complexity.cpp
      /* Exponential order (loop implementation) */\nint exponential(int n) {\n    int count = 0, base = 1;\n    // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
      time_complexity.java
      /* Exponential order (loop implementation) */\nint exponential(int n) {\n    int count = 0, base = 1;\n    // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
      time_complexity.cs
      /* Exponential order (loop implementation) */\nint Exponential(int n) {\n    int count = 0, bas = 1;\n    // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < bas; j++) {\n            count++;\n        }\n        bas *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
      time_complexity.go
      /* Exponential order (loop implementation) */\nfunc exponential(n int) int {\n    count, base := 0, 1\n    // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n    for i := 0; i < n; i++ {\n        for j := 0; j < base; j++ {\n            count++\n        }\n        base *= 2\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count\n}\n
      time_complexity.swift
      /* Exponential order (loop implementation) */\nfunc exponential(n: Int) -> Int {\n    var count = 0\n    var base = 1\n    // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n    for _ in 0 ..< n {\n        for _ in 0 ..< base {\n            count += 1\n        }\n        base *= 2\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count\n}\n
      time_complexity.js
      /* Exponential order (loop implementation) */\nfunction exponential(n) {\n    let count = 0,\n        base = 1;\n    // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n    for (let i = 0; i < n; i++) {\n        for (let j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
      time_complexity.ts
      /* Exponential order (loop implementation) */\nfunction exponential(n: number): number {\n    let count = 0,\n        base = 1;\n    // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n    for (let i = 0; i < n; i++) {\n        for (let j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
      time_complexity.dart
      /* Exponential order (loop implementation) */\nint exponential(int n) {\n  int count = 0, base = 1;\n  // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n  for (var i = 0; i < n; i++) {\n    for (var j = 0; j < base; j++) {\n      count++;\n    }\n    base *= 2;\n  }\n  // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n  return count;\n}\n
      time_complexity.rs
      /* Exponential order (loop implementation) */\nfn exponential(n: i32) -> i32 {\n    let mut count = 0;\n    let mut base = 1;\n    // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n    for _ in 0..n {\n        for _ in 0..base {\n            count += 1\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    count\n}\n
      time_complexity.c
      /* Exponential order (loop implementation) */\nint exponential(int n) {\n    int count = 0;\n    int bas = 1;\n    // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < bas; j++) {\n            count++;\n        }\n        bas *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
      time_complexity.kt
      /* Exponential order (loop implementation) */\nfun exponential(n: Int): Int {\n    var count = 0\n    var base = 1\n    // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n    for (i in 0..<n) {\n        for (j in 0..<base) {\n            count++\n        }\n        base *= 2\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count\n}\n
      time_complexity.rb
      ### Exponential time (iterative) ###\ndef exponential(n)\n  count, base = 0, 1\n\n  # Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)\n  (0...n).each do\n    (0...base).each { count += 1 }\n    base *= 2\n  end\n\n  # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n  count\nend\n

      Figure 2-11 \u00a0 Time complexity of exponential order

      In actual algorithms, exponential order often appears in recursive functions. For example, in the following code, it recursively splits in two, stopping after \\(n\\) splits:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
      def exp_recur(n: int) -> int:\n    \"\"\"Exponential order (recursive implementation)\"\"\"\n    if n == 1:\n        return 1\n    return exp_recur(n - 1) + exp_recur(n - 1) + 1\n
      time_complexity.cpp
      /* Exponential order (recursive implementation) */\nint expRecur(int n) {\n    if (n == 1)\n        return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
      time_complexity.java
      /* Exponential order (recursive implementation) */\nint expRecur(int n) {\n    if (n == 1)\n        return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
      time_complexity.cs
      /* Exponential order (recursive implementation) */\nint ExpRecur(int n) {\n    if (n == 1) return 1;\n    return ExpRecur(n - 1) + ExpRecur(n - 1) + 1;\n}\n
      time_complexity.go
      /* Exponential order (recursive implementation) */\nfunc expRecur(n int) int {\n    if n == 1 {\n        return 1\n    }\n    return expRecur(n-1) + expRecur(n-1) + 1\n}\n
      time_complexity.swift
      /* Exponential order (recursive implementation) */\nfunc expRecur(n: Int) -> Int {\n    if n == 1 {\n        return 1\n    }\n    return expRecur(n: n - 1) + expRecur(n: n - 1) + 1\n}\n
      time_complexity.js
      /* Exponential order (recursive implementation) */\nfunction expRecur(n) {\n    if (n === 1) return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
      time_complexity.ts
      /* Exponential order (recursive implementation) */\nfunction expRecur(n: number): number {\n    if (n === 1) return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
      time_complexity.dart
      /* Exponential order (recursive implementation) */\nint expRecur(int n) {\n  if (n == 1) return 1;\n  return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
      time_complexity.rs
      /* Exponential order (recursive implementation) */\nfn exp_recur(n: i32) -> i32 {\n    if n == 1 {\n        return 1;\n    }\n    exp_recur(n - 1) + exp_recur(n - 1) + 1\n}\n
      time_complexity.c
      /* Exponential order (recursive implementation) */\nint expRecur(int n) {\n    if (n == 1)\n        return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
      time_complexity.kt
      /* Exponential order (recursive implementation) */\nfun expRecur(n: Int): Int {\n    if (n == 1) {\n        return 1\n    }\n    return expRecur(n - 1) + expRecur(n - 1) + 1\n}\n
      time_complexity.rb
      ### Exponential time (recursive) ###\ndef exp_recur(n)\n  return 1 if n == 1\n  exp_recur(n - 1) + exp_recur(n - 1) + 1\nend\n

      Exponential order growth is very rapid and is common in exhaustive methods (brute force search, backtracking, etc.). For problems with large data scales, exponential order is unacceptable and typically requires dynamic programming or greedy algorithms to solve.

      "},{"location":"chapter_computational_complexity/time_complexity/#5-logarithmic-order-olog-n","title":"5. \u00a0 Logarithmic Order \\(O(\\log n)\\)","text":"

      In contrast to exponential order, logarithmic order reflects the situation of \"reducing to half each round\". Let the input data size be \\(n\\). Since it is reduced to half each round, the number of loops is \\(\\log_2 n\\), which is the inverse function of \\(2^n\\).

      Figure 2-12 and the following code simulate the process of \"reducing to half each round\", with a time complexity of \\(O(\\log_2 n)\\), abbreviated as \\(O(\\log n)\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
      def logarithmic(n: int) -> int:\n    \"\"\"Logarithmic order (loop implementation)\"\"\"\n    count = 0\n    while n > 1:\n        n = n / 2\n        count += 1\n    return count\n
      time_complexity.cpp
      /* Logarithmic order (loop implementation) */\nint logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
      time_complexity.java
      /* Logarithmic order (loop implementation) */\nint logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
      time_complexity.cs
      /* Logarithmic order (loop implementation) */\nint Logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n /= 2;\n        count++;\n    }\n    return count;\n}\n
      time_complexity.go
      /* Logarithmic order (loop implementation) */\nfunc logarithmic(n int) int {\n    count := 0\n    for n > 1 {\n        n = n / 2\n        count++\n    }\n    return count\n}\n
      time_complexity.swift
      /* Logarithmic order (loop implementation) */\nfunc logarithmic(n: Int) -> Int {\n    var count = 0\n    var n = n\n    while n > 1 {\n        n = n / 2\n        count += 1\n    }\n    return count\n}\n
      time_complexity.js
      /* Logarithmic order (loop implementation) */\nfunction logarithmic(n) {\n    let count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
      time_complexity.ts
      /* Logarithmic order (loop implementation) */\nfunction logarithmic(n: number): number {\n    let count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
      time_complexity.dart
      /* Logarithmic order (loop implementation) */\nint logarithmic(int n) {\n  int count = 0;\n  while (n > 1) {\n    n = n ~/ 2;\n    count++;\n  }\n  return count;\n}\n
      time_complexity.rs
      /* Logarithmic order (loop implementation) */\nfn logarithmic(mut n: i32) -> i32 {\n    let mut count = 0;\n    while n > 1 {\n        n = n / 2;\n        count += 1;\n    }\n    count\n}\n
      time_complexity.c
      /* Logarithmic order (loop implementation) */\nint logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
      time_complexity.kt
      /* Logarithmic order (loop implementation) */\nfun logarithmic(n: Int): Int {\n    var n1 = n\n    var count = 0\n    while (n1 > 1) {\n        n1 /= 2\n        count++\n    }\n    return count\n}\n
      time_complexity.rb
      ### Logarithmic time (iterative) ###\ndef logarithmic(n)\n  count = 0\n\n  while n > 1\n    n /= 2\n    count += 1\n  end\n\n  count\nend\n

      Figure 2-12 \u00a0 Time complexity of logarithmic order

      Like exponential order, logarithmic order also commonly appears in recursive functions. The following code forms a recursion tree of height \\(\\log_2 n\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
      def log_recur(n: int) -> int:\n    \"\"\"Logarithmic order (recursive implementation)\"\"\"\n    if n <= 1:\n        return 0\n    return log_recur(n / 2) + 1\n
      time_complexity.cpp
      /* Logarithmic order (recursive implementation) */\nint logRecur(int n) {\n    if (n <= 1)\n        return 0;\n    return logRecur(n / 2) + 1;\n}\n
      time_complexity.java
      /* Logarithmic order (recursive implementation) */\nint logRecur(int n) {\n    if (n <= 1)\n        return 0;\n    return logRecur(n / 2) + 1;\n}\n
      time_complexity.cs
      /* Logarithmic order (recursive implementation) */\nint LogRecur(int n) {\n    if (n <= 1) return 0;\n    return LogRecur(n / 2) + 1;\n}\n
      time_complexity.go
      /* Logarithmic order (recursive implementation) */\nfunc logRecur(n int) int {\n    if n <= 1 {\n        return 0\n    }\n    return logRecur(n/2) + 1\n}\n
      time_complexity.swift
      /* Logarithmic order (recursive implementation) */\nfunc logRecur(n: Int) -> Int {\n    if n <= 1 {\n        return 0\n    }\n    return logRecur(n: n / 2) + 1\n}\n
      time_complexity.js
      /* Logarithmic order (recursive implementation) */\nfunction logRecur(n) {\n    if (n <= 1) return 0;\n    return logRecur(n / 2) + 1;\n}\n
      time_complexity.ts
      /* Logarithmic order (recursive implementation) */\nfunction logRecur(n: number): number {\n    if (n <= 1) return 0;\n    return logRecur(n / 2) + 1;\n}\n
      time_complexity.dart
      /* Logarithmic order (recursive implementation) */\nint logRecur(int n) {\n  if (n <= 1) return 0;\n  return logRecur(n ~/ 2) + 1;\n}\n
      time_complexity.rs
      /* Logarithmic order (recursive implementation) */\nfn log_recur(n: i32) -> i32 {\n    if n <= 1 {\n        return 0;\n    }\n    log_recur(n / 2) + 1\n}\n
      time_complexity.c
      /* Logarithmic order (recursive implementation) */\nint logRecur(int n) {\n    if (n <= 1)\n        return 0;\n    return logRecur(n / 2) + 1;\n}\n
      time_complexity.kt
      /* Logarithmic order (recursive implementation) */\nfun logRecur(n: Int): Int {\n    if (n <= 1)\n        return 0\n    return logRecur(n / 2) + 1\n}\n
      time_complexity.rb
      ### Logarithmic time (recursive) ###\ndef log_recur(n)\n  return 0 unless n > 1\n  log_recur(n / 2) + 1\nend\n

      Logarithmic order commonly appears in algorithms based on the divide-and-conquer strategy, embodying the algorithmic thinking of \"dividing into many\" and \"simplifying complexity\". It grows slowly and is the ideal time complexity second only to constant order.

      What is the base of \\(O(\\log n)\\)?

      To be precise, \"dividing into \\(m\\)\" corresponds to a time complexity of \\(O(\\log_m n)\\). And through the logarithmic base change formula, we can obtain time complexities with different bases that are equal:

      \\[ O(\\log_m n) = O(\\log_k n / \\log_k m) = O(\\log_k n) \\]

      That is to say, the base \\(m\\) can be converted without affecting the complexity. Therefore, we usually omit the base \\(m\\) and denote logarithmic order simply as \\(O(\\log n)\\).

      "},{"location":"chapter_computational_complexity/time_complexity/#6-linearithmic-order-on-log-n","title":"6. \u00a0 Linearithmic Order \\(O(n \\log n)\\)","text":"

      Linearithmic order commonly appears in nested loops, where the time complexities of the two layers of loops are \\(O(\\log n)\\) and \\(O(n)\\) respectively. The relevant code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
      def linear_log_recur(n: int) -> int:\n    \"\"\"Linearithmic order\"\"\"\n    if n <= 1:\n        return 1\n    # Divide into two, the scale of subproblems is reduced by half\n    count = linear_log_recur(n // 2) + linear_log_recur(n // 2)\n    # Current subproblem contains n operations\n    for _ in range(n):\n        count += 1\n    return count\n
      time_complexity.cpp
      /* Linearithmic order */\nint linearLogRecur(int n) {\n    if (n <= 1)\n        return 1;\n    int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.java
      /* Linearithmic order */\nint linearLogRecur(int n) {\n    if (n <= 1)\n        return 1;\n    int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.cs
      /* Linearithmic order */\nint LinearLogRecur(int n) {\n    if (n <= 1) return 1;\n    int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.go
      /* Linearithmic order */\nfunc linearLogRecur(n int) int {\n    if n <= 1 {\n        return 1\n    }\n    count := linearLogRecur(n/2) + linearLogRecur(n/2)\n    for i := 0; i < n; i++ {\n        count++\n    }\n    return count\n}\n
      time_complexity.swift
      /* Linearithmic order */\nfunc linearLogRecur(n: Int) -> Int {\n    if n <= 1 {\n        return 1\n    }\n    var count = linearLogRecur(n: n / 2) + linearLogRecur(n: n / 2)\n    for _ in stride(from: 0, to: n, by: 1) {\n        count += 1\n    }\n    return count\n}\n
      time_complexity.js
      /* Linearithmic order */\nfunction linearLogRecur(n) {\n    if (n <= 1) return 1;\n    let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (let i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.ts
      /* Linearithmic order */\nfunction linearLogRecur(n: number): number {\n    if (n <= 1) return 1;\n    let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (let i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.dart
      /* Linearithmic order */\nint linearLogRecur(int n) {\n  if (n <= 1) return 1;\n  int count = linearLogRecur(n ~/ 2) + linearLogRecur(n ~/ 2);\n  for (var i = 0; i < n; i++) {\n    count++;\n  }\n  return count;\n}\n
      time_complexity.rs
      /* Linearithmic order */\nfn linear_log_recur(n: i32) -> i32 {\n    if n <= 1 {\n        return 1;\n    }\n    let mut count = linear_log_recur(n / 2) + linear_log_recur(n / 2);\n    for _ in 0..n {\n        count += 1;\n    }\n    return count;\n}\n
      time_complexity.c
      /* Linearithmic order */\nint linearLogRecur(int n) {\n    if (n <= 1)\n        return 1;\n    int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
      time_complexity.kt
      /* Linearithmic order */\nfun linearLogRecur(n: Int): Int {\n    if (n <= 1)\n        return 1\n    var count = linearLogRecur(n / 2) + linearLogRecur(n / 2)\n    for (i in 0..<n) {\n        count++\n    }\n    return count\n}\n
      time_complexity.rb
      ### Linearithmic time ###\ndef linear_log_recur(n)\n  return 1 unless n > 1\n\n  count = linear_log_recur(n / 2) + linear_log_recur(n / 2)\n  (0...n).each { count += 1 }\n\n  count\nend\n

      Figure 2-13 shows how linearithmic order is generated. Each level of the binary tree has a total of \\(n\\) operations, and the tree has \\(\\log_2 n + 1\\) levels, resulting in a time complexity of \\(O(n \\log n)\\).

      Figure 2-13 \u00a0 Time complexity of linearithmic order

      Mainstream sorting algorithms typically have a time complexity of \\(O(n \\log n)\\), such as quicksort, merge sort, and heap sort.

      "},{"location":"chapter_computational_complexity/time_complexity/#7-factorial-order-on","title":"7. \u00a0 Factorial Order \\(O(n!)\\)","text":"

      Factorial order corresponds to the mathematical \"permutation\" problem. Given \\(n\\) distinct elements, find all possible permutation schemes; the number of schemes is:

      \\[ n! = n \\times (n - 1) \\times (n - 2) \\times \\dots \\times 2 \\times 1 \\]

      Factorials are typically implemented using recursion. As shown in Figure 2-14 and the following code, the first level splits into \\(n\\) branches, the second level splits into \\(n - 1\\) branches, and so on, until the \\(n\\)-th level when splitting stops:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
      def factorial_recur(n: int) -> int:\n    \"\"\"Factorial order (recursive implementation)\"\"\"\n    if n == 0:\n        return 1\n    count = 0\n    # Split from 1 into n\n    for _ in range(n):\n        count += factorial_recur(n - 1)\n    return count\n
      time_complexity.cpp
      /* Factorial order (recursive implementation) */\nint factorialRecur(int n) {\n    if (n == 0)\n        return 1;\n    int count = 0;\n    // Split from 1 into n\n    for (int i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
      time_complexity.java
      /* Factorial order (recursive implementation) */\nint factorialRecur(int n) {\n    if (n == 0)\n        return 1;\n    int count = 0;\n    // Split from 1 into n\n    for (int i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
      time_complexity.cs
      /* Factorial order (recursive implementation) */\nint FactorialRecur(int n) {\n    if (n == 0) return 1;\n    int count = 0;\n    // Split from 1 into n\n    for (int i = 0; i < n; i++) {\n        count += FactorialRecur(n - 1);\n    }\n    return count;\n}\n
      time_complexity.go
      /* Factorial order (recursive implementation) */\nfunc factorialRecur(n int) int {\n    if n == 0 {\n        return 1\n    }\n    count := 0\n    // Split from 1 into n\n    for i := 0; i < n; i++ {\n        count += factorialRecur(n - 1)\n    }\n    return count\n}\n
      time_complexity.swift
      /* Factorial order (recursive implementation) */\nfunc factorialRecur(n: Int) -> Int {\n    if n == 0 {\n        return 1\n    }\n    var count = 0\n    // Split from 1 into n\n    for _ in 0 ..< n {\n        count += factorialRecur(n: n - 1)\n    }\n    return count\n}\n
      time_complexity.js
      /* Factorial order (recursive implementation) */\nfunction factorialRecur(n) {\n    if (n === 0) return 1;\n    let count = 0;\n    // Split from 1 into n\n    for (let i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
      time_complexity.ts
      /* Factorial order (recursive implementation) */\nfunction factorialRecur(n: number): number {\n    if (n === 0) return 1;\n    let count = 0;\n    // Split from 1 into n\n    for (let i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
      time_complexity.dart
      /* Factorial order (recursive implementation) */\nint factorialRecur(int n) {\n  if (n == 0) return 1;\n  int count = 0;\n  // Split from 1 into n\n  for (var i = 0; i < n; i++) {\n    count += factorialRecur(n - 1);\n  }\n  return count;\n}\n
      time_complexity.rs
      /* Factorial order (recursive implementation) */\nfn factorial_recur(n: i32) -> i32 {\n    if n == 0 {\n        return 1;\n    }\n    let mut count = 0;\n    // Split from 1 into n\n    for _ in 0..n {\n        count += factorial_recur(n - 1);\n    }\n    count\n}\n
      time_complexity.c
      /* Factorial order (recursive implementation) */\nint factorialRecur(int n) {\n    if (n == 0)\n        return 1;\n    int count = 0;\n    for (int i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
      time_complexity.kt
      /* Factorial order (recursive implementation) */\nfun factorialRecur(n: Int): Int {\n    if (n == 0)\n        return 1\n    var count = 0\n    // Split from 1 into n\n    for (i in 0..<n) {\n        count += factorialRecur(n - 1)\n    }\n    return count\n}\n
      time_complexity.rb
      ### Factorial time (recursive) ###\ndef factorial_recur(n)\n  return 1 if n == 0\n\n  count = 0\n  # Split from 1 into n\n  (0...n).each { count += factorial_recur(n - 1) }\n\n  count\nend\n

      Figure 2-14 \u00a0 Time complexity of factorial order

      Note that because when \\(n \\geq 4\\) we always have \\(n! > 2^n\\), factorial order grows faster than exponential order, and is also unacceptable for large \\(n\\).

      "},{"location":"chapter_computational_complexity/time_complexity/#235-worst-best-and-average-time-complexities","title":"2.3.5 \u00a0 Worst, Best, and Average Time Complexities","text":"

      The time efficiency of an algorithm is often not fixed, but is related to the distribution of the input data. Suppose we input an array nums of length \\(n\\), where nums consists of numbers from \\(1\\) to \\(n\\), with each number appearing only once, but the element order is randomly shuffled. The task is to return the index of element \\(1\\). We can draw the following conclusions.

      • When nums = [?, ?, ..., 1], i.e., when the last element is \\(1\\), it requires a complete traversal of the array, reaching worst-case time complexity \\(O(n)\\).
      • When nums = [1, ?, ?, ...], i.e., when the first element is \\(1\\), no matter how long the array is, there is no need to continue traversing, reaching best-case time complexity \\(\\Omega(1)\\).

      The \"worst-case time complexity\" corresponds to the function's asymptotic upper bound, denoted using big-\\(O\\) notation. Correspondingly, the \"best-case time complexity\" corresponds to the function's asymptotic lower bound, denoted using \\(\\Omega\\) notation:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby worst_best_time_complexity.py
      def random_numbers(n: int) -> list[int]:\n    \"\"\"Generate an array with elements: 1, 2, ..., n, shuffled in order\"\"\"\n    # Generate array nums =: 1, 2, 3, ..., n\n    nums = [i for i in range(1, n + 1)]\n    # Randomly shuffle array elements\n    random.shuffle(nums)\n    return nums\n\ndef find_one(nums: list[int]) -> int:\n    \"\"\"Find the index of number 1 in array nums\"\"\"\n    for i in range(len(nums)):\n        # When element 1 is at the head of the array, best time complexity O(1) is achieved\n        # When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n        if nums[i] == 1:\n            return i\n    return -1\n
      worst_best_time_complexity.cpp
      /* Generate an array with elements { 1, 2, ..., n }, order shuffled */\nvector<int> randomNumbers(int n) {\n    vector<int> nums(n);\n    // Generate array nums = { 1, 2, 3, ..., n }\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // Use system time to generate random seed\n    unsigned seed = chrono::system_clock::now().time_since_epoch().count();\n    // Randomly shuffle array elements\n    shuffle(nums.begin(), nums.end(), default_random_engine(seed));\n    return nums;\n}\n\n/* Find the index of number 1 in array nums */\nint findOne(vector<int> &nums) {\n    for (int i = 0; i < nums.size(); i++) {\n        // When element 1 is at the head of the array, best time complexity O(1) is achieved\n        // When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
      worst_best_time_complexity.java
      /* Generate an array with elements { 1, 2, ..., n }, order shuffled */\nint[] randomNumbers(int n) {\n    Integer[] nums = new Integer[n];\n    // Generate array nums = { 1, 2, 3, ..., n }\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // Randomly shuffle array elements\n    Collections.shuffle(Arrays.asList(nums));\n    // Integer[] -> int[]\n    int[] res = new int[n];\n    for (int i = 0; i < n; i++) {\n        res[i] = nums[i];\n    }\n    return res;\n}\n\n/* Find the index of number 1 in array nums */\nint findOne(int[] nums) {\n    for (int i = 0; i < nums.length; i++) {\n        // When element 1 is at the head of the array, best time complexity O(1) is achieved\n        // When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
      worst_best_time_complexity.cs
      /* Generate an array with elements { 1, 2, ..., n }, order shuffled */\nint[] RandomNumbers(int n) {\n    int[] nums = new int[n];\n    // Generate array nums = { 1, 2, 3, ..., n }\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n\n    // Randomly shuffle array elements\n    for (int i = 0; i < nums.Length; i++) {\n        int index = new Random().Next(i, nums.Length);\n        (nums[i], nums[index]) = (nums[index], nums[i]);\n    }\n    return nums;\n}\n\n/* Find the index of number 1 in array nums */\nint FindOne(int[] nums) {\n    for (int i = 0; i < nums.Length; i++) {\n        // When element 1 is at the head of the array, best time complexity O(1) is achieved\n        // When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
      worst_best_time_complexity.go
      /* Generate an array with elements { 1, 2, ..., n }, order shuffled */\nfunc randomNumbers(n int) []int {\n    nums := make([]int, n)\n    // Generate array nums = { 1, 2, 3, ..., n }\n    for i := 0; i < n; i++ {\n        nums[i] = i + 1\n    }\n    // Randomly shuffle array elements\n    rand.Shuffle(len(nums), func(i, j int) {\n        nums[i], nums[j] = nums[j], nums[i]\n    })\n    return nums\n}\n\n/* Find the index of number 1 in array nums */\nfunc findOne(nums []int) int {\n    for i := 0; i < len(nums); i++ {\n        // When element 1 is at the head of the array, best time complexity O(1) is achieved\n        // When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n        if nums[i] == 1 {\n            return i\n        }\n    }\n    return -1\n}\n
      worst_best_time_complexity.swift
      /* Generate an array with elements { 1, 2, ..., n }, order shuffled */\nfunc randomNumbers(n: Int) -> [Int] {\n    // Generate array nums = { 1, 2, 3, ..., n }\n    var nums = Array(1 ... n)\n    // Randomly shuffle array elements\n    nums.shuffle()\n    return nums\n}\n\n/* Find the index of number 1 in array nums */\nfunc findOne(nums: [Int]) -> Int {\n    for i in nums.indices {\n        // When element 1 is at the head of the array, best time complexity O(1) is achieved\n        // When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n        if nums[i] == 1 {\n            return i\n        }\n    }\n    return -1\n}\n
      worst_best_time_complexity.js
      /* Generate an array with elements { 1, 2, ..., n }, order shuffled */\nfunction randomNumbers(n) {\n    const nums = Array(n);\n    // Generate array nums = { 1, 2, 3, ..., n }\n    for (let i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // Randomly shuffle array elements\n    for (let i = 0; i < n; i++) {\n        const r = Math.floor(Math.random() * (i + 1));\n        const temp = nums[i];\n        nums[i] = nums[r];\n        nums[r] = temp;\n    }\n    return nums;\n}\n\n/* Find the index of number 1 in array nums */\nfunction findOne(nums) {\n    for (let i = 0; i < nums.length; i++) {\n        // When element 1 is at the head of the array, best time complexity O(1) is achieved\n        // When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n        if (nums[i] === 1) {\n            return i;\n        }\n    }\n    return -1;\n}\n
      worst_best_time_complexity.ts
      /* Generate an array with elements { 1, 2, ..., n }, order shuffled */\nfunction randomNumbers(n: number): number[] {\n    const nums = Array(n);\n    // Generate array nums = { 1, 2, 3, ..., n }\n    for (let i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // Randomly shuffle array elements\n    for (let i = 0; i < n; i++) {\n        const r = Math.floor(Math.random() * (i + 1));\n        const temp = nums[i];\n        nums[i] = nums[r];\n        nums[r] = temp;\n    }\n    return nums;\n}\n\n/* Find the index of number 1 in array nums */\nfunction findOne(nums: number[]): number {\n    for (let i = 0; i < nums.length; i++) {\n        // When element 1 is at the head of the array, best time complexity O(1) is achieved\n        // When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n        if (nums[i] === 1) {\n            return i;\n        }\n    }\n    return -1;\n}\n
      worst_best_time_complexity.dart
      /* Generate an array with elements { 1, 2, ..., n }, order shuffled */\nList<int> randomNumbers(int n) {\n  final nums = List.filled(n, 0);\n  // Generate array nums = { 1, 2, 3, ..., n }\n  for (var i = 0; i < n; i++) {\n    nums[i] = i + 1;\n  }\n  // Randomly shuffle array elements\n  nums.shuffle();\n\n  return nums;\n}\n\n/* Find the index of number 1 in array nums */\nint findOne(List<int> nums) {\n  for (var i = 0; i < nums.length; i++) {\n    // When element 1 is at the head of the array, best time complexity O(1) is achieved\n    // When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n    if (nums[i] == 1) return i;\n  }\n\n  return -1;\n}\n
      worst_best_time_complexity.rs
      /* Generate an array with elements { 1, 2, ..., n }, order shuffled */\nfn random_numbers(n: i32) -> Vec<i32> {\n    // Generate array nums = { 1, 2, 3, ..., n }\n    let mut nums = (1..=n).collect::<Vec<i32>>();\n    // Randomly shuffle array elements\n    nums.shuffle(&mut thread_rng());\n    nums\n}\n\n/* Find the index of number 1 in array nums */\nfn find_one(nums: &[i32]) -> Option<usize> {\n    for i in 0..nums.len() {\n        // When element 1 is at the head of the array, best time complexity O(1) is achieved\n        // When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n        if nums[i] == 1 {\n            return Some(i);\n        }\n    }\n    None\n}\n
      worst_best_time_complexity.c
      /* Generate an array with elements { 1, 2, ..., n }, order shuffled */\nint *randomNumbers(int n) {\n    // Allocate heap memory (create 1D variable-length array: n elements of type int)\n    int *nums = (int *)malloc(n * sizeof(int));\n    // Generate array nums = { 1, 2, 3, ..., n }\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // Randomly shuffle array elements\n    for (int i = n - 1; i > 0; i--) {\n        int j = rand() % (i + 1);\n        int temp = nums[i];\n        nums[i] = nums[j];\n        nums[j] = temp;\n    }\n    return nums;\n}\n\n/* Find the index of number 1 in array nums */\nint findOne(int *nums, int n) {\n    for (int i = 0; i < n; i++) {\n        // When element 1 is at the head of the array, best time complexity O(1) is achieved\n        // When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
      worst_best_time_complexity.kt
      /* Generate an array with elements { 1, 2, ..., n }, order shuffled */\nfun randomNumbers(n: Int): Array<Int?> {\n    val nums = IntArray(n)\n    // Generate array nums = { 1, 2, 3, ..., n }\n    for (i in 0..<n) {\n        nums[i] = i + 1\n    }\n    // Randomly shuffle array elements\n    nums.shuffle()\n    val res = arrayOfNulls<Int>(n)\n    for (i in 0..<n) {\n        res[i] = nums[i]\n    }\n    return res\n}\n\n/* Find the index of number 1 in array nums */\nfun findOne(nums: Array<Int?>): Int {\n    for (i in nums.indices) {\n        // When element 1 is at the head of the array, best time complexity O(1) is achieved\n        // When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n        if (nums[i] == 1)\n            return i\n    }\n    return -1\n}\n
      worst_best_time_complexity.rb
      ### Generate array with elements: 1, 2, ..., n, shuffled ###\ndef random_numbers(n)\n  # Generate array nums =: 1, 2, 3, ..., n\n  nums = Array.new(n) { |i| i + 1 }\n  # Randomly shuffle array elements\n  nums.shuffle!\nend\n\n### Find index of number 1 in array nums ###\ndef find_one(nums)\n  for i in 0...nums.length\n    # When element 1 is at the head of the array, best time complexity O(1) is achieved\n    # When element 1 is at the tail of the array, worst time complexity O(n) is achieved\n    return i if nums[i] == 1\n  end\n\n  -1\nend\n

      It is worth noting that we rarely use best-case time complexity in practice, because it can usually only be achieved with a very small probability and may be somewhat misleading. The worst-case time complexity is more practical because it gives a safety value for efficiency, allowing us to use the algorithm with confidence.

      From the above example, we can see that both worst-case and best-case time complexities only occur under \"special data distributions\", which may have a very small probability of occurrence and may not truly reflect the algorithm's running efficiency. In contrast, average time complexity can reflect the algorithm's running efficiency under random input data, denoted using the \\(\\Theta\\) notation.

      For some algorithms, we can simply derive the average case under random data distribution. For example, in the above example, since the input array is shuffled, the probability of element \\(1\\) appearing at any index is equal, so the algorithm's average number of loops is half the array length \\(n / 2\\), giving an average time complexity of \\(\\Theta(n / 2) = \\Theta(n)\\).

      But for more complex algorithms, calculating average time complexity is often quite difficult, because it is hard to analyze the overall mathematical expectation under data distribution. In this case, we usually use worst-case time complexity as the criterion for judging algorithm efficiency.

      Why is the \\(\\Theta\\) symbol rarely seen?

      This may be because the \\(O\\) symbol is too catchy, so we often use it to represent average time complexity. But strictly speaking, this practice is not standard. In this book and other materials, if you encounter expressions like \"average time complexity \\(O(n)\\)\", please understand it directly as \\(\\Theta(n)\\).

      "},{"location":"chapter_data_structure/","title":"Chapter 3. \u00a0 Data Structures","text":"

      Abstract

      Data structure is like a sturdy and diverse framework.

      It provides a blueprint for the orderly organization of data, upon which algorithms come to life.

      "},{"location":"chapter_data_structure/#chapter-contents","title":"Chapter contents","text":"
      • 3.1 \u00a0 Classification of Data Structures
      • 3.2 \u00a0 Basic Data Types
      • 3.3 \u00a0 Number Encoding *
      • 3.4 \u00a0 Character Encoding *
      • 3.5 \u00a0 Summary
      "},{"location":"chapter_data_structure/basic_data_types/","title":"3.2 \u00a0 Basic Data Types","text":"

      When we talk about data in computers, we think of various forms such as text, images, videos, audio, 3D models, and more. Although these data are organized in different ways, they are all composed of various basic data types.

      Basic data types are types that the CPU can directly operate on, and they are directly used in algorithms, mainly including the following.

      • Integer types byte, short, int, long.
      • Floating-point types float, double, used to represent decimal numbers.
      • Character type char, used to represent letters, punctuation marks, and even emojis in various languages.
      • Boolean type bool, used to represent \"yes\" and \"no\" judgments.

      Basic data types are stored in binary form in computers. One binary bit is \\(1\\) bit. In most modern operating systems, \\(1\\) byte consists of \\(8\\) bits.

      The range of values for basic data types depends on the size of the space they occupy. Below is an example using Java.

      • Integer type byte occupies \\(1\\) byte = \\(8\\) bits, and can represent \\(2^{8}\\) numbers.
      • Integer type int occupies \\(4\\) bytes = \\(32\\) bits, and can represent \\(2^{32}\\) numbers.

      The following table lists the space occupied, value ranges, and default values of various basic data types in Java. You don't need to memorize this table; a general understanding is sufficient, and you can refer to it when needed.

      Table 3-1 \u00a0 Space occupied and value ranges of basic data types

      Type Symbol Space Occupied Minimum Value Maximum Value Default Value Integer byte 1 byte \\(-2^7\\) (\\(-128\\)) \\(2^7 - 1\\) (\\(127\\)) \\(0\\) short 2 bytes \\(-2^{15}\\) \\(2^{15} - 1\\) \\(0\\) int 4 bytes \\(-2^{31}\\) \\(2^{31} - 1\\) \\(0\\) long 8 bytes \\(-2^{63}\\) \\(2^{63} - 1\\) \\(0\\) Float float 4 bytes \\(1.175 \\times 10^{-38}\\) \\(3.403 \\times 10^{38}\\) \\(0.0\\text{f}\\) double 8 bytes \\(2.225 \\times 10^{-308}\\) \\(1.798 \\times 10^{308}\\) \\(0.0\\) Character char 2 bytes \\(0\\) \\(2^{16} - 1\\) \\(0\\) Boolean bool 1 byte \\(\\text{false}\\) \\(\\text{true}\\) \\(\\text{false}\\)

      Please note that the above table is specific to Java's basic data types. Each programming language has its own data type definitions, and their space occupied, value ranges, and default values may vary.

      • In Python, the integer type int can be of any size, limited only by available memory; the floating-point type float is double-precision 64-bit; there is no char type, a single character is actually a string str of length 1.
      • C and C++ do not explicitly specify the size of basic data types, which varies by implementation and platform. The above table follows the LP64 data model, which is used in Unix 64-bit operating systems including Linux and macOS.
      • The size of character char is 1 byte in C and C++, and in most programming languages it depends on the specific character encoding method, as detailed in the \"Character Encoding\" section.
      • Even though representing a boolean value requires only 1 bit (\\(0\\) or \\(1\\)), it is usually stored as 1 byte in memory. This is because modern computer CPUs typically use 1 byte as the minimum addressable memory unit.

      So, what is the relationship between basic data types and data structures? We know that data structures are ways of organizing and storing data in computers. The subject of this statement is \"structure\", not \"data\".

      If we want to represent \"a row of numbers\", we naturally think of using an array. This is because the linear structure of an array can represent the adjacency and order relationships of numbers, but the content stored\u2014whether integer int, floating-point float, or character char\u2014is unrelated to the \"data structure\".

      In other words, basic data types provide the \"content type\" of data, while data structures provide the \"organization method\" of data. For example, in the following code, we use the same data structure (array) to store and represent different basic data types, including int, float, char, bool, etc.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      # Initialize arrays using various basic data types\nnumbers: list[int] = [0] * 5\ndecimals: list[float] = [0.0] * 5\n# In Python, characters are actually strings of length 1\ncharacters: list[str] = ['0'] * 5\nbools: list[bool] = [False] * 5\n# Python lists can freely store various basic data types and object references\ndata = [0, 0.0, 'a', False, ListNode(0)]\n
      // Initialize arrays using various basic data types\nint numbers[5];\nfloat decimals[5];\nchar characters[5];\nbool bools[5];\n
      // Initialize arrays using various basic data types\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nboolean[] bools = new boolean[5];\n
      // Initialize arrays using various basic data types\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nbool[] bools = new bool[5];\n
      // Initialize arrays using various basic data types\nvar numbers = [5]int{}\nvar decimals = [5]float64{}\nvar characters = [5]byte{}\nvar bools = [5]bool{}\n
      // Initialize arrays using various basic data types\nlet numbers = Array(repeating: 0, count: 5)\nlet decimals = Array(repeating: 0.0, count: 5)\nlet characters: [Character] = Array(repeating: \"a\", count: 5)\nlet bools = Array(repeating: false, count: 5)\n
      // JavaScript arrays can freely store various basic data types and objects\nconst array = [0, 0.0, 'a', false];\n
      // Initialize arrays using various basic data types\nconst numbers: number[] = [];\nconst characters: string[] = [];\nconst bools: boolean[] = [];\n
      // Initialize arrays using various basic data types\nList<int> numbers = List.filled(5, 0);\nList<double> decimals = List.filled(5, 0.0);\nList<String> characters = List.filled(5, 'a');\nList<bool> bools = List.filled(5, false);\n
      // Initialize arrays using various basic data types\nlet numbers: Vec<i32> = vec![0; 5];\nlet decimals: Vec<f32> = vec![0.0; 5];\nlet characters: Vec<char> = vec!['0'; 5];\nlet bools: Vec<bool> = vec![false; 5];\n
      // Initialize arrays using various basic data types\nint numbers[10];\nfloat decimals[10];\nchar characters[10];\nbool bools[10];\n
      // Initialize arrays using various basic data types\nval numbers = IntArray(5)\nval decinals = FloatArray(5)\nval characters = CharArray(5)\nval bools = BooleanArray(5)\n
      # Ruby lists can freely store various basic data types and object references\ndata = [0, 0.0, 'a', false, ListNode(0)]\n
      Visualized Execution

      https://pythontutor.com/render.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E4%BD%BF%E7%94%A8%E5%A4%9A%E7%A7%8D%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E6%9D%A5%E5%88%9D%E5%A7%8B%E5%8C%96%E6%95%B0%E7%BB%84%0A%20%20%20%20numbers%20%3D%20%5B0%5D%20*%205%0A%20%20%20%20decimals%20%3D%20%5B0.0%5D%20*%205%0A%20%20%20%20%23%20Python%20%E7%9A%84%E5%AD%97%E7%AC%A6%E5%AE%9E%E9%99%85%E4%B8%8A%E6%98%AF%E9%95%BF%E5%BA%A6%E4%B8%BA%201%20%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2%0A%20%20%20%20characters%20%3D%20%5B'0'%5D%20*%205%0A%20%20%20%20bools%20%3D%20%5BFalse%5D%20*%205%0A%20%20%20%20%23%20Python%20%E7%9A%84%E5%88%97%E8%A1%A8%E5%8F%AF%E4%BB%A5%E8%87%AA%E7%94%B1%E5%AD%98%E5%82%A8%E5%90%84%E7%A7%8D%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%92%8C%E5%AF%B9%E8%B1%A1%E5%BC%95%E7%94%A8%0A%20%20%20%20data%20%3D%20%5B0,%200.0,%20'a',%20False,%20ListNode%280%29%5D&cumulative=false&curInstr=12&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

      "},{"location":"chapter_data_structure/character_encoding/","title":"3.4 \u00a0 Character Encoding *","text":"

      In computers, all data is stored in binary form, and character char is no exception. To represent characters, we need to establish a \"character set\" that defines a one-to-one correspondence between each character and binary numbers. With a character set, computers can convert binary numbers to characters by looking up the table.

      "},{"location":"chapter_data_structure/character_encoding/#341-ascii-character-set","title":"3.4.1 \u00a0 Ascii Character Set","text":"

      ASCII code is the earliest character set, with the full name American Standard Code for Information Interchange. It uses 7 binary bits (the lower 7 bits of one byte) to represent a character, and can represent a maximum of 128 different characters. As shown in Figure 3-6, ASCII code includes uppercase and lowercase English letters, numbers 0 ~ 9, some punctuation marks, and some control characters (such as newline and tab).

      Figure 3-6 \u00a0 ASCII code

      However, ASCII code can only represent English. With the globalization of computers, a character set called EASCII that can represent more languages emerged. It expands from the 7-bit basis of ASCII to 8 bits, and can represent 256 different characters.

      Worldwide, a batch of EASCII character sets suitable for different regions have appeared successively. The first 128 characters of these character sets are unified as ASCII code, and the last 128 characters are defined differently to adapt to the needs of different languages.

      "},{"location":"chapter_data_structure/character_encoding/#342-gbk-character-set","title":"3.4.2 \u00a0 Gbk Character Set","text":"

      Later, people found that EASCII code still cannot meet the character quantity requirements of many languages. For example, there are nearly one hundred thousand Chinese characters, and several thousand are used daily. In 1980, the China National Standardization Administration released the GB2312 character set, which included 6,763 Chinese characters, basically meeting the needs for computer processing of Chinese characters.

      However, GB2312 cannot handle some rare characters and traditional Chinese characters. The GBK character set is an extension based on GB2312, which includes a total of 21,886 Chinese characters. In the GBK encoding scheme, ASCII characters are represented using one byte, and Chinese characters are represented using two bytes.

      "},{"location":"chapter_data_structure/character_encoding/#343-unicode-character-set","title":"3.4.3 \u00a0 Unicode Character Set","text":"

      With the vigorous development of computer technology, character sets and encoding standards flourished, which brought many problems. On the one hand, these character sets generally only define characters for specific languages and cannot work normally in multilingual environments. On the other hand, multiple character set standards exist for the same language, and if two computers use different encoding standards, garbled characters will appear during information transmission.

      Researchers of that era thought: If a sufficiently complete character set is released that includes all languages and symbols in the world, wouldn't it be possible to solve cross-language environment and garbled character problems? Driven by this idea, a large and comprehensive character set, Unicode, was born.

      Unicode is called \"\u7edf\u4e00\u7801\" (Unified Code) in Chinese and can theoretically accommodate over one million characters. It is committed to including characters from around the world into a unified character set, providing a universal character set to handle and display various language texts, reducing garbled character problems caused by different encoding standards.

      Since its release in 1991, Unicode has continuously expanded to include new languages and characters. As of September 2022, Unicode has included 149,186 characters, including characters, symbols, and even emojis from various languages. In the vast Unicode character set, commonly used characters occupy 2 bytes, and some rare characters occupy 3 bytes or even 4 bytes.

      Unicode is a universal character set that essentially assigns a number (called a \"code point\") to each character, but it does not specify how to store these character code points in computers. We can't help but ask: when Unicode code points of multiple lengths appear simultaneously in a text, how does the system parse the characters? For example, given an encoding with a length of 2 bytes, how does the system determine whether it is one 2-byte character or two 1-byte characters?

      For the above problem, a straightforward solution is to store all characters as equal-length encodings. As shown in Figure 3-7, each character in \"Hello\" occupies 1 byte, and each character in \"\u7b97\u6cd5\" (algorithm) occupies 2 bytes. We can encode all characters in \"Hello \u7b97\u6cd5\" as 2 bytes in length by padding the high bits with 0. In this way, the system can parse one character every 2 bytes and restore the content of this phrase.

      Figure 3-7 \u00a0 Unicode encoding example

      However, ASCII code has already proven to us that encoding English only requires 1 byte. If the above scheme is adopted, the size of English text will be twice that under ASCII encoding, which is very wasteful of memory space. Therefore, we need a more efficient Unicode encoding method.

      "},{"location":"chapter_data_structure/character_encoding/#344-utf-8-encoding","title":"3.4.4 \u00a0 Utf-8 Encoding","text":"

      Currently, UTF-8 has become the most widely used Unicode encoding method internationally. It is a variable-length encoding that uses 1 to 4 bytes to represent a character, depending on the complexity of the character. ASCII characters only require 1 byte, Latin and Greek letters require 2 bytes, commonly used Chinese characters require 3 bytes, and some other rare characters require 4 bytes.

      The encoding rules of UTF-8 are not complicated and can be divided into the following two cases.

      • For 1-byte characters, set the highest bit to \\(0\\), and set the remaining 7 bits to the Unicode code point. It is worth noting that ASCII characters occupy the first 128 code points in the Unicode character set. That is to say, UTF-8 encoding is backward compatible with ASCII code. This means we can use UTF-8 to parse very old ASCII code text.
      • For characters with a length of \\(n\\) bytes (where \\(n > 1\\)), set the highest \\(n\\) bits of the first byte to \\(1\\), and set the \\((n + 1)\\)-th bit to \\(0\\); starting from the second byte, set the highest 2 bits of each byte to \\(10\\); use all remaining bits to fill in the Unicode code point of the character.

      Figure 3-8 shows the UTF-8 encoding corresponding to \"Hello\u7b97\u6cd5\". It can be observed that since the highest \\(n\\) bits are all set to \\(1\\), the system can parse the length of the character as \\(n\\) by reading the number of highest bits that are \\(1\\).

      But why set the highest 2 bits of all other bytes to \\(10\\)? In fact, this \\(10\\) can serve as a check symbol. Assuming the system starts parsing text from an incorrect byte, the \\(10\\) at the beginning of the byte can help the system quickly determine an anomaly.

      The reason for using \\(10\\) as a check symbol is that under UTF-8 encoding rules, it is impossible for a character's highest two bits to be \\(10\\). This conclusion can be proven by contradiction: assuming the highest two bits of a character are \\(10\\), it means the length of the character is \\(1\\), corresponding to ASCII code. However, the highest bit of ASCII code should be \\(0\\), which contradicts the assumption.

      Figure 3-8 \u00a0 UTF-8 encoding example

      In addition to UTF-8, common encoding methods also include the following two.

      • UTF-16 encoding: Uses 2 or 4 bytes to represent a character. All ASCII characters and commonly used non-English characters are represented with 2 bytes; a few characters need to use 4 bytes. For 2-byte characters, UTF-16 encoding is equal to the Unicode code point.
      • UTF-32 encoding: Every character uses 4 bytes. This means that UTF-32 takes up more space than UTF-8 and UTF-16, especially for text with a high proportion of ASCII characters.

      From the perspective of storage space occupation, using UTF-8 to represent English characters is very efficient because it only requires 1 byte; using UTF-16 encoding for some non-English characters (such as Chinese) will be more efficient because it only requires 2 bytes, while UTF-8 may require 3 bytes.

      From a compatibility perspective, UTF-8 has the best universality, and many tools and libraries support UTF-8 first.

      "},{"location":"chapter_data_structure/character_encoding/#345-character-encoding-in-programming-languages","title":"3.4.5 \u00a0 Character Encoding in Programming Languages","text":"

      For most past programming languages, strings during program execution use fixed-length encodings such as UTF-16 or UTF-32. Under fixed-length encoding, we can treat strings as arrays for processing, and this approach has the following advantages.

      • Random access: UTF-16 encoded strings can be easily accessed randomly. UTF-8 is a variable-length encoding. To find the \\(i\\)-th character, we need to traverse from the beginning of the string to the \\(i\\)-th character, which requires \\(O(n)\\) time.
      • Character counting: Similar to random access, calculating the length of a UTF-16 encoded string is also an \\(O(1)\\) operation. However, calculating the length of a UTF-8 encoded string requires traversing the entire string.
      • String operations: Many string operations (such as splitting, joining, inserting, deleting, etc.) on UTF-16 encoded strings are easier to perform. Performing these operations on UTF-8 encoded strings usually requires additional calculations to ensure that invalid UTF-8 encoding is not generated.

      In fact, the design of character encoding schemes for programming languages is a very interesting topic involving many factors.

      • Java's String type uses UTF-16 encoding, with each character occupying 2 bytes. This is because at the beginning of Java language design, people believed that 16 bits were sufficient to represent all possible characters. However, this was an incorrect judgment. Later, the Unicode specification expanded beyond 16 bits, so characters in Java may now be represented by a pair of 16-bit values (called \"surrogate pairs\").
      • The strings of JavaScript and TypeScript use UTF-16 encoding for reasons similar to Java. When Netscape first introduced the JavaScript language in 1995, Unicode was still in its early stages of development, and at that time, using 16-bit encoding was sufficient to represent all Unicode characters.
      • C# uses UTF-16 encoding mainly because the .NET platform was designed by Microsoft, and many of Microsoft's technologies (including the Windows operating system) extensively use UTF-16 encoding.

      Due to the underestimation of character quantities by the above programming languages, they had to adopt the \"surrogate pair\" method to represent Unicode characters with lengths exceeding 16 bits. This is a reluctant compromise. On the one hand, in strings containing surrogate pairs, one character may occupy 2 bytes or 4 bytes, thus losing the advantage of fixed-length encoding. On the other hand, handling surrogate pairs requires additional code, which increases the complexity and difficulty of debugging in programming.

      For the above reasons, some programming languages have proposed different encoding schemes.

      • Python's str uses Unicode encoding and adopts a flexible string representation where the stored character length depends on the largest Unicode code point in the string. If all characters in the string are ASCII characters, each character occupies 1 byte; if there are characters exceeding the ASCII range but all within the Basic Multilingual Plane (BMP), each character occupies 2 bytes; if there are characters exceeding the BMP, each character occupies 4 bytes.
      • Go language's string type uses UTF-8 encoding internally. Go language also provides the rune type, which is used to represent a single Unicode code point.
      • Rust language's str and String types use UTF-8 encoding internally. Rust also provides the char type for representing a single Unicode code point.

      It should be noted that the above discussion is about how strings are stored in programming languages, which is different from how strings are stored in files or transmitted over networks. In file storage or network transmission, we usually encode strings into UTF-8 format to achieve optimal compatibility and space efficiency.

      "},{"location":"chapter_data_structure/classification_of_data_structure/","title":"3.1 \u00a0 Classification of Data Structures","text":"

      Common data structures include arrays, linked lists, stacks, queues, hash tables, trees, heaps, and graphs. They can be classified from two dimensions: \"logical structure\" and \"physical structure\".

      "},{"location":"chapter_data_structure/classification_of_data_structure/#311-logical-structure-linear-and-non-linear","title":"3.1.1 \u00a0 Logical Structure: Linear and Non-Linear","text":"

      Logical structure reveals the logical relationships between data elements. In arrays and linked lists, data is arranged in a certain order, embodying the linear relationship between data; while in trees, data is arranged hierarchically from top to bottom, showing the derived relationship between \"ancestors\" and \"descendants\"; graphs are composed of nodes and edges, reflecting complex network relationships.

      As shown in Figure 3-1, logical structures can be divided into two major categories: \"linear\" and \"non-linear\". Linear structures are more intuitive, indicating that data is linearly arranged in logical relationships; non-linear structures are the opposite, arranged non-linearly.

      • Linear data structures: Arrays, linked lists, stacks, queues, hash tables, where elements have a one-to-one sequential relationship.
      • Non-linear data structures: Trees, heaps, graphs, hash tables.

      Non-linear data structures can be further divided into tree structures and network structures.

      • Tree structures: Trees, heaps, hash tables, where elements have a one-to-many relationship.
      • Network structures: Graphs, where elements have a many-to-many relationship.

      Figure 3-1 \u00a0 Linear and non-linear data structures

      "},{"location":"chapter_data_structure/classification_of_data_structure/#312-physical-structure-contiguous-and-dispersed","title":"3.1.2 \u00a0 Physical Structure: Contiguous and Dispersed","text":"

      When an algorithm program runs, the data being processed is mainly stored in memory. Figure 3-2 shows a computer memory stick, where each black square contains a memory space. We can imagine memory as a huge Excel spreadsheet, where each cell can store a certain amount of data.

      The system accesses data at the target location through memory addresses. As shown in Figure 3-2, the computer assigns a number to each cell in the spreadsheet according to specific rules, ensuring that each memory space has a unique memory address. With these addresses, the program can access data in memory.

      Figure 3-2 \u00a0 Memory stick, memory space, memory address

      Tip

      It is worth noting that comparing memory to an Excel spreadsheet is a simplified analogy. The actual working mechanism of memory is quite complex, involving concepts such as address space, memory management, cache mechanisms, virtual memory, and physical memory.

      Memory is a shared resource for all programs. When a block of memory is occupied by a program, it usually cannot be used by other programs at the same time. Therefore, in the design of data structures and algorithms, memory resources are an important consideration. For example, the peak memory occupied by an algorithm should not exceed the remaining free memory of the system; if there is a lack of contiguous large memory blocks, then the data structure chosen must be able to be stored in dispersed memory spaces.

      As shown in Figure 3-3, physical structure reflects the way data is stored in computer memory, and can be divided into contiguous space storage (arrays) and dispersed space storage (linked lists). The two physical structures exhibit complementary characteristics in terms of time efficiency and space efficiency.

      Figure 3-3 \u00a0 Contiguous space storage and dispersed space storage

      It is worth noting that all data structures are implemented based on arrays, linked lists, or a combination of both. For example, stacks and queues can be implemented using either arrays or linked lists; while the implementation of hash tables may include both arrays and linked lists.

      • Can be implemented based on arrays: Stacks, queues, hash tables, trees, heaps, graphs, matrices, tensors (arrays with dimensions \\(\\geq 3\\)), etc.
      • Can be implemented based on linked lists: Stacks, queues, hash tables, trees, heaps, graphs, etc.

      After initialization, linked lists can still adjust their length during program execution, so they are also called \"dynamic data structures\". After initialization, the length of arrays cannot be changed, so they are also called \"static data structures\". It is worth noting that arrays can achieve length changes by reallocating memory, thus possessing a certain degree of \"dynamism\".

      Tip

      If you find it difficult to understand physical structure, it is recommended to read the next chapter first, and then review this section.

      "},{"location":"chapter_data_structure/number_encoding/","title":"3.3 \u00a0 Number Encoding *","text":"

      Tip

      In this book, chapters marked with an asterisk * are optional readings. If you are short on time or find them challenging, you may skip these initially and return to them after completing the essential chapters.

      "},{"location":"chapter_data_structure/number_encoding/#331-sign-magnitude-1s-complement-and-2s-complement","title":"3.3.1 \u00a0 Sign-Magnitude, 1's Complement, and 2's Complement","text":"

      In the table from the previous section, we found that all integer types can represent one more negative number than positive numbers. For example, the byte range is \\([-128, 127]\\). This phenomenon is counterintuitive, and its underlying reason involves knowledge of sign-magnitude, 1's complement, and 2's complement.

      First, it should be noted that numbers are stored in computers in the form of \"2's complement\". Before analyzing the reasons for this, let's first define these three concepts.

      • Sign-magnitude: We treat the highest bit of the binary representation of a number as the sign bit, where \\(0\\) represents a positive number and \\(1\\) represents a negative number, and the remaining bits represent the value of the number.
      • 1's complement: The 1's complement of a positive number is the same as its sign-magnitude. For a negative number, the 1's complement is obtained by inverting all bits except the sign bit of its sign-magnitude.
      • 2's complement: The 2's complement of a positive number is the same as its sign-magnitude. For a negative number, the 2's complement is obtained by adding \\(1\\) to its 1's complement.

      Figure 3-4 shows the conversion methods among sign-magnitude, 1's complement, and 2's complement.

      Figure 3-4 \u00a0 Conversions among sign-magnitude, 1's complement, and 2's complement

      Sign-magnitude, although the most intuitive, has some limitations. On one hand, the sign-magnitude of negative numbers cannot be directly used in operations. For example, calculating \\(1 + (-2)\\) in sign-magnitude yields \\(-3\\), which is clearly incorrect.

      \\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 + 1000 \\; 0010 \\newline & = 1000 \\; 0011 \\newline & \\rightarrow -3 \\end{aligned} \\]

      To solve this problem, computers introduced 1's complement. If we first convert sign-magnitude to 1's complement and calculate \\(1 + (-2)\\) in 1's complement, then convert the result back to sign-magnitude, we can obtain the correct result of \\(-1\\).

      \\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 \\; \\text{(Sign-magnitude)} + 1000 \\; 0010 \\; \\text{(Sign-magnitude)} \\newline & = 0000 \\; 0001 \\; \\text{(1's complement)} + 1111 \\; 1101 \\; \\text{(1's complement)} \\newline & = 1111 \\; 1110 \\; \\text{(1's complement)} \\newline & = 1000 \\; 0001 \\; \\text{(Sign-magnitude)} \\newline & \\rightarrow -1 \\end{aligned} \\]

      On the other hand, the sign-magnitude of the number zero has two representations, \\(+0\\) and \\(-0\\). This means that the number zero corresponds to two different binary encodings, which may cause ambiguity. For example, in conditional judgments, if we don't distinguish between positive zero and negative zero, it may lead to incorrect judgment results. If we want to handle the ambiguity of positive and negative zero, we need to introduce additional judgment operations, which may reduce the computational efficiency of the computer.

      \\[ \\begin{aligned} +0 & \\rightarrow 0000 \\; 0000 \\newline -0 & \\rightarrow 1000 \\; 0000 \\end{aligned} \\]

      Like sign-magnitude, 1's complement also has the problem of positive and negative zero ambiguity. Therefore, computers further introduced 2's complement. Let's first observe the conversion process of negative zero from sign-magnitude to 1's complement to 2's complement:

      \\[ \\begin{aligned} -0 \\rightarrow \\; & 1000 \\; 0000 \\; \\text{(Sign-magnitude)} \\newline = \\; & 1111 \\; 1111 \\; \\text{(1's complement)} \\newline = 1 \\; & 0000 \\; 0000 \\; \\text{(2's complement)} \\newline \\end{aligned} \\]

      Adding \\(1\\) to the 1's complement of negative zero produces a carry, but since the byte type has a length of only 8 bits, the \\(1\\) that overflows to the 9th bit is discarded. That is to say, the 2's complement of negative zero is \\(0000 \\; 0000\\), which is the same as the 2's complement of positive zero. This means that in 2's complement representation, there is only one zero, and the positive and negative zero ambiguity is thus resolved.

      One last question remains: the range of the byte type is \\([-128, 127]\\), and how is the extra negative number \\(-128\\) obtained? We notice that all integers in the interval \\([-127, +127]\\) have corresponding sign-magnitude, 1's complement, and 2's complement, and sign-magnitude and 2's complement can be converted to each other.

      However, the 2's complement \\(1000 \\; 0000\\) is an exception, and it does not have a corresponding sign-magnitude. According to the conversion method, we get that the sign-magnitude of this 2's complement is \\(0000 \\; 0000\\). This is clearly contradictory because this sign-magnitude represents the number \\(0\\), and its 2's complement should be itself. The computer specifies that this special 2's complement \\(1000 \\; 0000\\) represents \\(-128\\). In fact, the result of calculating \\((-1) + (-127)\\) in 2's complement is \\(-128\\).

      \\[ \\begin{aligned} & (-127) + (-1) \\newline & \\rightarrow 1111 \\; 1111 \\; \\text{(Sign-magnitude)} + 1000 \\; 0001 \\; \\text{(Sign-magnitude)} \\newline & = 1000 \\; 0000 \\; \\text{(1's complement)} + 1111 \\; 1110 \\; \\text{(1's complement)} \\newline & = 1000 \\; 0001 \\; \\text{(2's complement)} + 1111 \\; 1111 \\; \\text{(2's complement)} \\newline & = 1000 \\; 0000 \\; \\text{(2's complement)} \\newline & \\rightarrow -128 \\end{aligned} \\]

      You may have noticed that all the above calculations are addition operations. This hints at an important fact: the hardware circuits inside computers are mainly designed based on addition operations. This is because addition operations are simpler to implement in hardware compared to other operations (such as multiplication, division, and subtraction), easier to parallelize, and have faster operation speeds.

      Please note that this does not mean that computers can only perform addition. By combining addition with some basic logical operations, computers can implement various other mathematical operations. For example, calculating the subtraction \\(a - b\\) can be converted to calculating the addition \\(a + (-b)\\); calculating multiplication and division can be converted to calculating multiple additions or subtractions.

      Now we can summarize the reasons why computers use 2's complement: based on 2's complement representation, computers can use the same circuits and operations to handle the addition of positive and negative numbers, without the need to design special hardware circuits to handle subtraction, and without the need to specially handle the ambiguity problem of positive and negative zero. This greatly simplifies hardware design and improves operational efficiency.

      The design of 2's complement is very ingenious. Due to space limitations, we will stop here. Interested readers are encouraged to explore further.

      "},{"location":"chapter_data_structure/number_encoding/#332-floating-point-number-encoding","title":"3.3.2 \u00a0 Floating-Point Number Encoding","text":"

      Careful readers may have noticed: int and float have the same length, both are 4 bytes, but why does float have a much larger range than int? This is very counterintuitive because it stands to reason that float needs to represent decimals, so the range should be smaller.

      In fact, this is because floating-point number float uses a different representation method. Let's denote a 32-bit binary number as:

      \\[ b_{31} b_{30} b_{29} \\ldots b_2 b_1 b_0 \\]

      According to the IEEE 754 standard, a 32-bit float consists of the following three parts.

      • Sign bit \\(\\mathrm{S}\\): occupies 1 bit, corresponding to \\(b_{31}\\).
      • Exponent bit \\(\\mathrm{E}\\): occupies 8 bits, corresponding to \\(b_{30} b_{29} \\ldots b_{23}\\).
      • Fraction bit \\(\\mathrm{N}\\): occupies 23 bits, corresponding to \\(b_{22} b_{21} \\ldots b_0\\).

      The calculation method for the value corresponding to the binary float is:

      \\[ \\text {val} = (-1)^{b_{31}} \\times 2^{\\left(b_{30} b_{29} \\ldots b_{23}\\right)_2-127} \\times\\left(1 . b_{22} b_{21} \\ldots b_0\\right)_2 \\]

      Converted to decimal, the calculation formula is:

      \\[ \\text {val}=(-1)^{\\mathrm{S}} \\times 2^{\\mathrm{E} -127} \\times (1 + \\mathrm{N}) \\]

      The range of each component is:

      \\[ \\begin{aligned} \\mathrm{S} \\in & \\{ 0, 1\\}, \\quad \\mathrm{E} \\in \\{ 1, 2, \\dots, 254 \\} \\newline (1 + \\mathrm{N}) = & (1 + \\sum_{i=1}^{23} b_{23-i} 2^{-i}) \\subset [1, 2 - 2^{-23}] \\end{aligned} \\]

      Figure 3-5 \u00a0 Calculation example of float under IEEE 754 standard

      Observing Figure 3-5, given example data \\(\\mathrm{S} = 0\\), \\(\\mathrm{E} = 124\\), \\(\\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\\), we have:

      \\[ \\text { val } = (-1)^0 \\times 2^{124 - 127} \\times (1 + 0.375) = 0.171875 \\]

      Now we can answer the initial question: the representation of float includes an exponent bit, resulting in a range far greater than int. According to the above calculation, the maximum positive number that float can represent is \\(2^{254 - 127} \\times (2 - 2^{-23}) \\approx 3.4 \\times 10^{38}\\), and the minimum negative number can be obtained by switching the sign bit.

      Although floating-point number float expands the range, its side effect is sacrificing precision. The integer type int uses all 32 bits to represent numbers, and the numbers are evenly distributed; however, due to the existence of the exponent bit, the larger the value of floating-point number float, the larger the difference between two adjacent numbers tends to be.

      As shown in Table 3-2, exponent bits \\(\\mathrm{E} = 0\\) and \\(\\mathrm{E} = 255\\) have special meanings, used to represent zero, infinity, \\(\\mathrm{NaN}\\), etc.

      Table 3-2 \u00a0 Meaning of exponent bits

      Exponent Bit E Fraction Bit \\(\\mathrm{N} = 0\\) Fraction Bit \\(\\mathrm{N} \\ne 0\\) Calculation Formula \\(0\\) \\(\\pm 0\\) Subnormal Number \\((-1)^{\\mathrm{S}} \\times 2^{-126} \\times (0.\\mathrm{N})\\) \\(1, 2, \\dots, 254\\) Normal Number Normal Number \\((-1)^{\\mathrm{S}} \\times 2^{(\\mathrm{E} -127)} \\times (1.\\mathrm{N})\\) \\(255\\) \\(\\pm \\infty\\) \\(\\mathrm{NaN}\\)

      It is worth noting that subnormal numbers significantly improve the precision of floating-point numbers. The smallest positive normal number is \\(2^{-126}\\), and the smallest positive subnormal number is \\(2^{-126} \\times 2^{-23}\\).

      Double-precision double also uses a representation method similar to float, which will not be elaborated here.

      "},{"location":"chapter_data_structure/summary/","title":"3.5 \u00a0 Summary","text":""},{"location":"chapter_data_structure/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • Data structures can be classified from two perspectives: logical structure and physical structure. Logical structure describes the logical relationships between data elements, while physical structure describes how data is stored in computer memory.
      • Common logical structures include linear, tree, and network structures. We typically classify data structures as linear (arrays, linked lists, stacks, queues) and non-linear (trees, graphs, heaps) based on their logical structure. The implementation of hash tables may involve both linear and non-linear data structures.
      • When a program runs, data is stored in computer memory. Each memory space has a corresponding memory address, and the program accesses data through these memory addresses.
      • Physical structures are primarily divided into contiguous space storage (arrays) and dispersed space storage (linked lists). All data structures are implemented using arrays, linked lists, or a combination of both.
      • Basic data types in computers include integers byte, short, int, long, floating-point numbers float, double, characters char, and booleans bool. Their value ranges depend on the size of space they occupy and their representation method.
      • Sign-magnitude, 1's complement, and 2's complement are three methods for encoding numbers in computers, and they can be converted into each other. The most significant bit of sign-magnitude is the sign bit, and the remaining bits represent the value of the number.
      • Integers are stored in computers in 2's complement form. Under 2's complement representation, computers can treat the addition of positive and negative numbers uniformly, without needing to design special hardware circuits for subtraction, and there is no ambiguity of positive and negative zero.
      • The encoding of floating-point numbers consists of 1 sign bit, 8 exponent bits, and 23 fraction bits. Due to the exponent bits, the range of floating-point numbers is much larger than that of integers, at the cost of sacrificing precision.
      • ASCII is the earliest English character set, with a length of 1 byte, containing a total of 127 characters. GBK is a commonly used Chinese character set, containing over 20,000 Chinese characters. Unicode is committed to providing a complete character set standard, collecting characters from various languages around the world, thereby solving the garbled text problem caused by inconsistent character encoding methods.
      • UTF-8 is the most popular Unicode encoding method, with excellent universality. It is a variable-length encoding method with good scalability, effectively improving storage space efficiency. UTF-16 and UTF-32 are fixed-length encoding methods. When encoding Chinese characters, UTF-16 occupies less space than UTF-8. Programming languages such as Java and C# use UTF-16 encoding by default.
      "},{"location":"chapter_data_structure/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

      Q: Why do hash tables contain both linear and non-linear data structures?

      The underlying structure of a hash table is an array. To resolve hash collisions, we may use \"chaining\" (discussed in the subsequent \"Hash Collision\" section): each bucket in the array points to a linked list, which may be converted to a tree (usually a red-black tree) when the list length exceeds a certain threshold.

      From a storage perspective, the underlying structure of a hash table is an array, where each bucket slot may contain a value, a linked list, or a tree. Therefore, hash tables may contain both linear data structures (arrays, linked lists) and non-linear data structures (trees).

      Q: Is the length of the char type 1 byte?

      The length of the char type is determined by the encoding method used by the programming language. For example, Java, JavaScript, TypeScript, and C# all use UTF-16 encoding (to store Unicode code points), so the char type has a length of 2 bytes.

      Q: Is there ambiguity in referring to array-based data structures as \"static data structures\"? Stacks can also perform \"dynamic\" operations such as push and pop.

      Stacks can indeed implement dynamic data operations, but the data structure is still \"static\" (fixed length). Although array-based data structures can dynamically add or remove elements, their capacity is fixed. If the data volume exceeds the pre-allocated size, a new larger array needs to be created, and the contents of the old array must be copied to the new array.

      Q: When constructing a stack (queue), its size is not specified. Why are they \"static data structures\"?

      In high-level programming languages, we do not need to manually specify the initial capacity of a stack (queue); this work is automatically completed within the class. For example, the initial capacity of Java's ArrayList is typically 10. Additionally, the expansion operation is also automatically implemented. See the subsequent \"List\" section for details.

      Q: The method of converting sign-magnitude to 2's complement is \"first negate then add 1\". So converting 2's complement to sign-magnitude should be the inverse operation \"first subtract 1 then negate\". However, 2's complement can also be converted to sign-magnitude through \"first negate then add 1\". Why is this?

      This is because the mutual conversion between sign-magnitude and 2's complement is actually the process of computing the \"complement\". Let us first define the complement: assuming \\(a + b = c\\), then we say that \\(a\\) is the complement of \\(b\\) to \\(c\\), and conversely, \\(b\\) is the complement of \\(a\\) to \\(c\\).

      Given an \\(n = 4\\) bit binary number \\(0010\\), if we treat this number as sign-magnitude (ignoring the sign bit), then its 2's complement can be obtained through \"first negate then add 1\":

      \\[ 0010 \\rightarrow 1101 \\rightarrow 1110 \\]

      We find that the sum of sign-magnitude and 2's complement is \\(0010 + 1110 = 10000\\), which means the 2's complement \\(1110\\) is the \"complement\" of sign-magnitude \\(0010\\) to \\(10000\\). This means the above \"first negate then add 1\" is actually the process of computing the complement to \\(10000\\).

      So, what is the \"complement\" of 2's complement \\(1110\\) to \\(10000\\)? We can still use \"first negate then add 1\" to obtain it:

      \\[ 1110 \\rightarrow 0001 \\rightarrow 0010 \\]

      In other words, sign-magnitude and 2's complement are each other's \"complement\" to \\(10000\\), so \"sign-magnitude to 2's complement\" and \"2's complement to sign-magnitude\" can be implemented using the same operation (first negate then add 1).

      Of course, we can also use the inverse operation to find the sign-magnitude of 2's complement \\(1110\\), that is, \"first subtract 1 then negate\":

      \\[ 1110 \\rightarrow 1101 \\rightarrow 0010 \\]

      In summary, both \"first negate then add 1\" and \"first subtract 1 then negate\" are computing the complement to \\(10000\\), and they are equivalent.

      Essentially, the \"negate\" operation is actually finding the complement to \\(1111\\) (because sign-magnitude + 1's complement = 1111 always holds); and adding 1 to the 1's complement yields the 2's complement, which is the complement to \\(10000\\).

      The above uses \\(n = 4\\) as an example, and it can be generalized to binary numbers of any number of bits.

      "},{"location":"chapter_divide_and_conquer/","title":"Chapter 12. \u00a0 Divide and Conquer","text":"

      Abstract

      Difficult problems are decomposed layer by layer, with each decomposition making them simpler.

      Divide and conquer reveals an important truth: start with simplicity, and nothing remains complex.

      "},{"location":"chapter_divide_and_conquer/#chapter-contents","title":"Chapter contents","text":"
      • 12.1 \u00a0 Divide and Conquer Algorithms
      • 12.2 \u00a0 Divide and Conquer Search Strategy
      • 12.3 \u00a0 Building a Binary Tree Problem
      • 12.4 \u00a0 Hanoi Tower Problem
      • 12.5 \u00a0 Summary
      "},{"location":"chapter_divide_and_conquer/binary_search_recur/","title":"12.2 \u00a0 Divide and Conquer Search Strategy","text":"

      We have already learned that search algorithms are divided into two major categories.

      • Brute-force search: Implemented by traversing the data structure, with a time complexity of \\(O(n)\\).
      • Adaptive search: Utilizes unique data organization forms or prior information, with time complexity reaching \\(O(\\log n)\\) or even \\(O(1)\\).

      In fact, search algorithms with time complexity of \\(O(\\log n)\\) are typically implemented based on the divide and conquer strategy, such as binary search and trees.

      • Each step of binary search divides the problem (searching for a target element in an array) into a smaller problem (searching for the target element in half of the array), continuing until the array is empty or the target element is found.
      • Trees are representative of the divide and conquer idea. In data structures such as binary search trees, AVL trees, and heaps, the time complexity of various operations is \\(O(\\log n)\\).

      The divide and conquer strategy of binary search is as follows.

      • The problem can be decomposed: Binary search recursively decomposes the original problem (searching in an array) into subproblems (searching in half of the array), achieved by comparing the middle element with the target element.
      • Subproblems are independent: In binary search, each round only processes one subproblem, which is not affected by other subproblems.
      • Solutions of subproblems do not need to be merged: Binary search aims to find a specific element, so there is no need to merge the solutions of subproblems. When a subproblem is solved, the original problem is also solved.

      Divide and conquer can improve search efficiency because brute-force search can only eliminate one option per round, while divide and conquer search can eliminate half of the options per round.

      "},{"location":"chapter_divide_and_conquer/binary_search_recur/#1-implementing-binary-search-based-on-divide-and-conquer","title":"1. \u00a0 Implementing Binary Search Based on Divide and Conquer","text":"

      In previous sections, binary search was implemented based on iteration. Now we implement it based on divide and conquer (recursion).

      Question

      Given a sorted array nums of length \\(n\\), where all elements are unique, find the element target.

      From a divide and conquer perspective, we denote the subproblem corresponding to the search interval \\([i, j]\\) as \\(f(i, j)\\).

      Starting from the original problem \\(f(0, n-1)\\), perform binary search through the following steps.

      1. Calculate the midpoint \\(m\\) of the search interval \\([i, j]\\), and use it to eliminate half of the search interval.
      2. Recursively solve the subproblem reduced by half in size, which could be \\(f(i, m-1)\\) or \\(f(m+1, j)\\).
      3. Repeat steps 1. and 2. until target is found or the interval is empty and return.

      Figure 12-4 shows the divide and conquer process of binary search for element \\(6\\) in an array.

      Figure 12-4 \u00a0 Divide and conquer process of binary search

      In the implementation code, we declare a recursive function dfs() to solve the problem \\(f(i, j)\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_recur.py
      def dfs(nums: list[int], target: int, i: int, j: int) -> int:\n    \"\"\"Binary search: problem f(i, j)\"\"\"\n    # If the interval is empty, it means there is no target element, return -1\n    if i > j:\n        return -1\n    # Calculate the midpoint index m\n    m = (i + j) // 2\n    if nums[m] < target:\n        # Recursion subproblem f(m+1, j)\n        return dfs(nums, target, m + 1, j)\n    elif nums[m] > target:\n        # Recursion subproblem f(i, m-1)\n        return dfs(nums, target, i, m - 1)\n    else:\n        # Found the target element, return its index\n        return m\n\ndef binary_search(nums: list[int], target: int) -> int:\n    \"\"\"Binary search\"\"\"\n    n = len(nums)\n    # Solve the problem f(0, n-1)\n    return dfs(nums, target, 0, n - 1)\n
      binary_search_recur.cpp
      /* Binary search: problem f(i, j) */\nint dfs(vector<int> &nums, int target, int i, int j) {\n    // If the interval is empty, it means there is no target element, return -1\n    if (i > j) {\n        return -1;\n    }\n    // Calculate the midpoint index m\n    int m = (i + j) / 2;\n    if (nums[m] < target) {\n        // Recursion subproblem f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // Recursion subproblem f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // Found the target element, return its index\n        return m;\n    }\n}\n\n/* Binary search */\nint binarySearch(vector<int> &nums, int target) {\n    int n = nums.size();\n    // Solve the problem f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
      binary_search_recur.java
      /* Binary search: problem f(i, j) */\nint dfs(int[] nums, int target, int i, int j) {\n    // If the interval is empty, it means there is no target element, return -1\n    if (i > j) {\n        return -1;\n    }\n    // Calculate the midpoint index m\n    int m = (i + j) / 2;\n    if (nums[m] < target) {\n        // Recursion subproblem f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // Recursion subproblem f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // Found the target element, return its index\n        return m;\n    }\n}\n\n/* Binary search */\nint binarySearch(int[] nums, int target) {\n    int n = nums.length;\n    // Solve the problem f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
      binary_search_recur.cs
      /* Binary search: problem f(i, j) */\nint DFS(int[] nums, int target, int i, int j) {\n    // If the interval is empty, it means there is no target element, return -1\n    if (i > j) {\n        return -1;\n    }\n    // Calculate the midpoint index m\n    int m = (i + j) / 2;\n    if (nums[m] < target) {\n        // Recursion subproblem f(m+1, j)\n        return DFS(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // Recursion subproblem f(i, m-1)\n        return DFS(nums, target, i, m - 1);\n    } else {\n        // Found the target element, return its index\n        return m;\n    }\n}\n\n/* Binary search */\nint BinarySearch(int[] nums, int target) {\n    int n = nums.Length;\n    // Solve the problem f(0, n-1)\n    return DFS(nums, target, 0, n - 1);\n}\n
      binary_search_recur.go
      /* Binary search: problem f(i, j) */\nfunc dfs(nums []int, target, i, j int) int {\n    // If interval is empty, indicating no target element, return -1\n    if i > j {\n        return -1\n    }\n    // Calculate midpoint index\n    m := i + ((j - i) >> 1)\n    // Compare midpoint with target element\n    if nums[m] < target {\n        // If smaller, recurse on right half of array\n        // Recursion subproblem f(m+1, j)\n        return dfs(nums, target, m+1, j)\n    } else if nums[m] > target {\n        // If larger, recurse on left half of array\n        // Recursion subproblem f(i, m-1)\n        return dfs(nums, target, i, m-1)\n    } else {\n        // Found the target element, return its index\n        return m\n    }\n}\n\n/* Binary search */\nfunc binarySearch(nums []int, target int) int {\n    n := len(nums)\n    return dfs(nums, target, 0, n-1)\n}\n
      binary_search_recur.swift
      /* Binary search: problem f(i, j) */\nfunc dfs(nums: [Int], target: Int, i: Int, j: Int) -> Int {\n    // If the interval is empty, it means there is no target element, return -1\n    if i > j {\n        return -1\n    }\n    // Calculate the midpoint index m\n    let m = (i + j) / 2\n    if nums[m] < target {\n        // Recursion subproblem f(m+1, j)\n        return dfs(nums: nums, target: target, i: m + 1, j: j)\n    } else if nums[m] > target {\n        // Recursion subproblem f(i, m-1)\n        return dfs(nums: nums, target: target, i: i, j: m - 1)\n    } else {\n        // Found the target element, return its index\n        return m\n    }\n}\n\n/* Binary search */\nfunc binarySearch(nums: [Int], target: Int) -> Int {\n    // Solve the problem f(0, n-1)\n    dfs(nums: nums, target: target, i: nums.startIndex, j: nums.endIndex - 1)\n}\n
      binary_search_recur.js
      /* Binary search: problem f(i, j) */\nfunction dfs(nums, target, i, j) {\n    // If the interval is empty, it means there is no target element, return -1\n    if (i > j) {\n        return -1;\n    }\n    // Calculate the midpoint index m\n    const m = i + ((j - i) >> 1);\n    if (nums[m] < target) {\n        // Recursion subproblem f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // Recursion subproblem f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // Found the target element, return its index\n        return m;\n    }\n}\n\n/* Binary search */\nfunction binarySearch(nums, target) {\n    const n = nums.length;\n    // Solve the problem f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
      binary_search_recur.ts
      /* Binary search: problem f(i, j) */\nfunction dfs(nums: number[], target: number, i: number, j: number): number {\n    // If the interval is empty, it means there is no target element, return -1\n    if (i > j) {\n        return -1;\n    }\n    // Calculate the midpoint index m\n    const m = i + ((j - i) >> 1);\n    if (nums[m] < target) {\n        // Recursion subproblem f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // Recursion subproblem f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // Found the target element, return its index\n        return m;\n    }\n}\n\n/* Binary search */\nfunction binarySearch(nums: number[], target: number): number {\n    const n = nums.length;\n    // Solve the problem f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
      binary_search_recur.dart
      /* Binary search: problem f(i, j) */\nint dfs(List<int> nums, int target, int i, int j) {\n  // If the interval is empty, it means there is no target element, return -1\n  if (i > j) {\n    return -1;\n  }\n  // Calculate the midpoint index m\n  int m = (i + j) ~/ 2;\n  if (nums[m] < target) {\n    // Recursion subproblem f(m+1, j)\n    return dfs(nums, target, m + 1, j);\n  } else if (nums[m] > target) {\n    // Recursion subproblem f(i, m-1)\n    return dfs(nums, target, i, m - 1);\n  } else {\n    // Found the target element, return its index\n    return m;\n  }\n}\n\n/* Binary search */\nint binarySearch(List<int> nums, int target) {\n  int n = nums.length;\n  // Solve the problem f(0, n-1)\n  return dfs(nums, target, 0, n - 1);\n}\n
      binary_search_recur.rs
      /* Binary search: problem f(i, j) */\nfn dfs(nums: &[i32], target: i32, i: i32, j: i32) -> i32 {\n    // If the interval is empty, it means there is no target element, return -1\n    if i > j {\n        return -1;\n    }\n    let m: i32 = i + (j - i) / 2;\n    if nums[m as usize] < target {\n        // Recursion subproblem f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if nums[m as usize] > target {\n        // Recursion subproblem f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // Found the target element, return its index\n        return m;\n    }\n}\n\n/* Binary search */\nfn binary_search(nums: &[i32], target: i32) -> i32 {\n    let n = nums.len() as i32;\n    // Solve the problem f(0, n-1)\n    dfs(nums, target, 0, n - 1)\n}\n
      binary_search_recur.c
      /* Binary search: problem f(i, j) */\nint dfs(int nums[], int target, int i, int j) {\n    // If the interval is empty, it means there is no target element, return -1\n    if (i > j) {\n        return -1;\n    }\n    // Calculate the midpoint index m\n    int m = (i + j) / 2;\n    if (nums[m] < target) {\n        // Recursion subproblem f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // Recursion subproblem f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // Found the target element, return its index\n        return m;\n    }\n}\n\n/* Binary search */\nint binarySearch(int nums[], int target, int numsSize) {\n    int n = numsSize;\n    // Solve the problem f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
      binary_search_recur.kt
      /* Binary search: problem f(i, j) */\nfun dfs(\n    nums: IntArray,\n    target: Int,\n    i: Int,\n    j: Int\n): Int {\n    // If the interval is empty, it means there is no target element, return -1\n    if (i > j) {\n        return -1\n    }\n    // Calculate the midpoint index m\n    val m = (i + j) / 2\n    return if (nums[m] < target) {\n        // Recursion subproblem f(m+1, j)\n        dfs(nums, target, m + 1, j)\n    } else if (nums[m] > target) {\n        // Recursion subproblem f(i, m-1)\n        dfs(nums, target, i, m - 1)\n    } else {\n        // Found the target element, return its index\n        m\n    }\n}\n\n/* Binary search */\nfun binarySearch(nums: IntArray, target: Int): Int {\n    val n = nums.size\n    // Solve the problem f(0, n-1)\n    return dfs(nums, target, 0, n - 1)\n}\n
      binary_search_recur.rb
      ### Binary search: problem f(i, j) ###\ndef dfs(nums, target, i, j)\n  # If the interval is empty, it means there is no target element, return -1\n  return -1 if i > j\n\n  # Calculate the midpoint index m\n  m = (i + j) / 2\n\n  if nums[m] < target\n    # Recursion subproblem f(m+1, j)\n    return dfs(nums, target, m + 1, j)\n  elsif nums[m] > target\n    # Recursion subproblem f(i, m-1)\n    return dfs(nums, target, i, m - 1)\n  else\n    # Found the target element, return its index\n    return m\n  end\nend\n\n### Binary search ###\ndef binary_search(nums, target)\n  n = nums.length\n  # Solve the problem f(0, n-1)\n  dfs(nums, target, 0, n - 1)\nend\n
      "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/","title":"12.3 \u00a0 Building a Binary Tree Problem","text":"

      Question

      Given the preorder traversal preorder and inorder traversal inorder of a binary tree, construct the binary tree and return the root node of the binary tree. Assume there are no duplicate node values in the binary tree (as shown in Figure 12-5).

      Figure 12-5 \u00a0 Example data for building a binary tree

      "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#1-determining-if-it-is-a-divide-and-conquer-problem","title":"1. \u00a0 Determining If It Is a Divide and Conquer Problem","text":"

      The original problem is defined as constructing a binary tree from preorder and inorder, which is a typical divide and conquer problem.

      • The problem can be decomposed: From a divide and conquer perspective, we can divide the original problem into two subproblems: constructing the left subtree and constructing the right subtree, plus one operation: initializing the root node. For each subtree (subproblem), we can still reuse the above division method, dividing it into smaller subtrees (subproblems) until the smallest subproblem (empty subtree) is reached.
      • Subproblems are independent: The left and right subtrees are independent of each other; there is no overlap between them. When constructing the left subtree, we only need to focus on the parts of the inorder and preorder traversals corresponding to the left subtree. The same applies to the right subtree.
      • Solutions of subproblems can be merged: Once we have the left and right subtrees (solutions of subproblems), we can link them to the root node to obtain the solution to the original problem.
      "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#2-how-to-divide-subtrees","title":"2. \u00a0 How to Divide Subtrees","text":"

      Based on the above analysis, this problem can be solved using divide and conquer, but how do we divide the left and right subtrees through the preorder traversal preorder and inorder traversal inorder?

      According to the definition, both preorder and inorder can be divided into three parts.

      • Preorder traversal: [ Root Node | Left Subtree | Right Subtree ], for example, the tree in Figure 12-5 corresponds to [ 3 | 9 | 2 1 7 ].
      • Inorder traversal: [ Left Subtree | Root Node \uff5c Right Subtree ], for example, the tree in Figure 12-5 corresponds to [ 9 | 3 | 1 2 7 ].

      Using the data from the figure above as an example, we can obtain the division results through the steps shown in Figure 12-6.

      1. The first element 3 in the preorder traversal is the value of the root node.
      2. Find the index of root node 3 in inorder, and use this index to divide inorder into [ 9 | 3 \uff5c 1 2 7 ].
      3. Based on the division result of inorder, it is easy to determine that the left and right subtrees have 1 and 3 nodes respectively, allowing us to divide preorder into [ 3 | 9 | 2 1 7 ].

      Figure 12-6 \u00a0 Dividing subtrees in preorder and inorder traversals

      "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#3-describing-subtree-intervals-based-on-variables","title":"3. \u00a0 Describing Subtree Intervals Based on Variables","text":"

      Based on the above division method, we have obtained the index intervals of the root node, left subtree, and right subtree in preorder and inorder. To describe these index intervals, we need to use several pointer variables.

      • Denote the index of the current tree's root node in preorder as \\(i\\).
      • Denote the index of the current tree's root node in inorder as \\(m\\).
      • Denote the index interval of the current tree in inorder as \\([l, r]\\).

      As shown in Table 12-1, through these variables we can represent the index of the root node in preorder and the index intervals of the subtrees in inorder.

      Table 12-1 \u00a0 Indices of root node and subtrees in preorder and inorder traversals

      Root node index in preorder Subtree index interval in inorder Current tree \\(i\\) \\([l, r]\\) Left subtree \\(i + 1\\) \\([l, m-1]\\) Right subtree \\(i + 1 + (m - l)\\) \\([m+1, r]\\)

      Please note that \\((m-l)\\) in the right subtree root node index means \"the number of nodes in the left subtree\". It is recommended to understand this in conjunction with Figure 12-7.

      Figure 12-7 \u00a0 Index interval representation of root node and left and right subtrees

      "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#4-code-implementation","title":"4. \u00a0 Code Implementation","text":"

      To improve the efficiency of querying \\(m\\), we use a hash table hmap to store the mapping from elements in the inorder array to their indices:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby build_tree.py
      def dfs(\n    preorder: list[int],\n    inorder_map: dict[int, int],\n    i: int,\n    l: int,\n    r: int,\n) -> TreeNode | None:\n    \"\"\"Build binary tree: divide and conquer\"\"\"\n    # Terminate when the subtree interval is empty\n    if r - l < 0:\n        return None\n    # Initialize the root node\n    root = TreeNode(preorder[i])\n    # Query m to divide the left and right subtrees\n    m = inorder_map[preorder[i]]\n    # Subproblem: build the left subtree\n    root.left = dfs(preorder, inorder_map, i + 1, l, m - 1)\n    # Subproblem: build the right subtree\n    root.right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r)\n    # Return the root node\n    return root\n\ndef build_tree(preorder: list[int], inorder: list[int]) -> TreeNode | None:\n    \"\"\"Build binary tree\"\"\"\n    # Initialize hash map, storing the mapping from inorder elements to indices\n    inorder_map = {val: i for i, val in enumerate(inorder)}\n    root = dfs(preorder, inorder_map, 0, 0, len(inorder) - 1)\n    return root\n
      build_tree.cpp
      /* Build binary tree: divide and conquer */\nTreeNode *dfs(vector<int> &preorder, unordered_map<int, int> &inorderMap, int i, int l, int r) {\n    // Terminate when the subtree interval is empty\n    if (r - l < 0)\n        return NULL;\n    // Initialize the root node\n    TreeNode *root = new TreeNode(preorder[i]);\n    // Query m to divide the left and right subtrees\n    int m = inorderMap[preorder[i]];\n    // Subproblem: build the left subtree\n    root->left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // Subproblem: build the right subtree\n    root->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // Return the root node\n    return root;\n}\n\n/* Build binary tree */\nTreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {\n    // Initialize hash map, storing the mapping from inorder elements to indices\n    unordered_map<int, int> inorderMap;\n    for (int i = 0; i < inorder.size(); i++) {\n        inorderMap[inorder[i]] = i;\n    }\n    TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorder.size() - 1);\n    return root;\n}\n
      build_tree.java
      /* Build binary tree: divide and conquer */\nTreeNode dfs(int[] preorder, Map<Integer, Integer> inorderMap, int i, int l, int r) {\n    // Terminate when the subtree interval is empty\n    if (r - l < 0)\n        return null;\n    // Initialize the root node\n    TreeNode root = new TreeNode(preorder[i]);\n    // Query m to divide the left and right subtrees\n    int m = inorderMap.get(preorder[i]);\n    // Subproblem: build the left subtree\n    root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // Subproblem: build the right subtree\n    root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // Return the root node\n    return root;\n}\n\n/* Build binary tree */\nTreeNode buildTree(int[] preorder, int[] inorder) {\n    // Initialize hash map, storing the mapping from inorder elements to indices\n    Map<Integer, Integer> inorderMap = new HashMap<>();\n    for (int i = 0; i < inorder.length; i++) {\n        inorderMap.put(inorder[i], i);\n    }\n    TreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n    return root;\n}\n
      build_tree.cs
      /* Build binary tree: divide and conquer */\nTreeNode? DFS(int[] preorder, Dictionary<int, int> inorderMap, int i, int l, int r) {\n    // Terminate when the subtree interval is empty\n    if (r - l < 0)\n        return null;\n    // Initialize the root node\n    TreeNode root = new(preorder[i]);\n    // Query m to divide the left and right subtrees\n    int m = inorderMap[preorder[i]];\n    // Subproblem: build the left subtree\n    root.left = DFS(preorder, inorderMap, i + 1, l, m - 1);\n    // Subproblem: build the right subtree\n    root.right = DFS(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // Return the root node\n    return root;\n}\n\n/* Build binary tree */\nTreeNode? BuildTree(int[] preorder, int[] inorder) {\n    // Initialize hash map, storing the mapping from inorder elements to indices\n    Dictionary<int, int> inorderMap = [];\n    for (int i = 0; i < inorder.Length; i++) {\n        inorderMap.TryAdd(inorder[i], i);\n    }\n    TreeNode? root = DFS(preorder, inorderMap, 0, 0, inorder.Length - 1);\n    return root;\n}\n
      build_tree.go
      /* Build binary tree: divide and conquer */\nfunc dfsBuildTree(preorder []int, inorderMap map[int]int, i, l, r int) *TreeNode {\n    // Terminate when the subtree interval is empty\n    if r-l < 0 {\n        return nil\n    }\n    // Initialize the root node\n    root := NewTreeNode(preorder[i])\n    // Query m to divide the left and right subtrees\n    m := inorderMap[preorder[i]]\n    // Subproblem: build the left subtree\n    root.Left = dfsBuildTree(preorder, inorderMap, i+1, l, m-1)\n    // Subproblem: build the right subtree\n    root.Right = dfsBuildTree(preorder, inorderMap, i+1+m-l, m+1, r)\n    // Return the root node\n    return root\n}\n\n/* Build binary tree */\nfunc buildTree(preorder, inorder []int) *TreeNode {\n    // Initialize hash map, storing the mapping from inorder elements to indices\n    inorderMap := make(map[int]int, len(inorder))\n    for i := 0; i < len(inorder); i++ {\n        inorderMap[inorder[i]] = i\n    }\n\n    root := dfsBuildTree(preorder, inorderMap, 0, 0, len(inorder)-1)\n    return root\n}\n
      build_tree.swift
      /* Build binary tree: divide and conquer */\nfunc dfs(preorder: [Int], inorderMap: [Int: Int], i: Int, l: Int, r: Int) -> TreeNode? {\n    // Terminate when the subtree interval is empty\n    if r - l < 0 {\n        return nil\n    }\n    // Initialize the root node\n    let root = TreeNode(x: preorder[i])\n    // Query m to divide the left and right subtrees\n    let m = inorderMap[preorder[i]]!\n    // Subproblem: build the left subtree\n    root.left = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1, l: l, r: m - 1)\n    // Subproblem: build the right subtree\n    root.right = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1 + m - l, l: m + 1, r: r)\n    // Return the root node\n    return root\n}\n\n/* Build binary tree */\nfunc buildTree(preorder: [Int], inorder: [Int]) -> TreeNode? {\n    // Initialize hash map, storing the mapping from inorder elements to indices\n    let inorderMap = inorder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }\n    return dfs(preorder: preorder, inorderMap: inorderMap, i: inorder.startIndex, l: inorder.startIndex, r: inorder.endIndex - 1)\n}\n
      build_tree.js
      /* Build binary tree: divide and conquer */\nfunction dfs(preorder, inorderMap, i, l, r) {\n    // Terminate when the subtree interval is empty\n    if (r - l < 0) return null;\n    // Initialize the root node\n    const root = new TreeNode(preorder[i]);\n    // Query m to divide the left and right subtrees\n    const m = inorderMap.get(preorder[i]);\n    // Subproblem: build the left subtree\n    root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // Subproblem: build the right subtree\n    root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // Return the root node\n    return root;\n}\n\n/* Build binary tree */\nfunction buildTree(preorder, inorder) {\n    // Initialize hash map, storing the mapping from inorder elements to indices\n    let inorderMap = new Map();\n    for (let i = 0; i < inorder.length; i++) {\n        inorderMap.set(inorder[i], i);\n    }\n    const root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n    return root;\n}\n
      build_tree.ts
      /* Build binary tree: divide and conquer */\nfunction dfs(\n    preorder: number[],\n    inorderMap: Map<number, number>,\n    i: number,\n    l: number,\n    r: number\n): TreeNode | null {\n    // Terminate when the subtree interval is empty\n    if (r - l < 0) return null;\n    // Initialize the root node\n    const root: TreeNode = new TreeNode(preorder[i]);\n    // Query m to divide the left and right subtrees\n    const m = inorderMap.get(preorder[i]);\n    // Subproblem: build the left subtree\n    root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // Subproblem: build the right subtree\n    root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // Return the root node\n    return root;\n}\n\n/* Build binary tree */\nfunction buildTree(preorder: number[], inorder: number[]): TreeNode | null {\n    // Initialize hash map, storing the mapping from inorder elements to indices\n    let inorderMap = new Map<number, number>();\n    for (let i = 0; i < inorder.length; i++) {\n        inorderMap.set(inorder[i], i);\n    }\n    const root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n    return root;\n}\n
      build_tree.dart
      /* Build binary tree: divide and conquer */\nTreeNode? dfs(\n  List<int> preorder,\n  Map<int, int> inorderMap,\n  int i,\n  int l,\n  int r,\n) {\n  // Terminate when the subtree interval is empty\n  if (r - l < 0) {\n    return null;\n  }\n  // Initialize the root node\n  TreeNode? root = TreeNode(preorder[i]);\n  // Query m to divide the left and right subtrees\n  int m = inorderMap[preorder[i]]!;\n  // Subproblem: build the left subtree\n  root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n  // Subproblem: build the right subtree\n  root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n  // Return the root node\n  return root;\n}\n\n/* Build binary tree */\nTreeNode? buildTree(List<int> preorder, List<int> inorder) {\n  // Initialize hash map, storing the mapping from inorder elements to indices\n  Map<int, int> inorderMap = {};\n  for (int i = 0; i < inorder.length; i++) {\n    inorderMap[inorder[i]] = i;\n  }\n  TreeNode? root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n  return root;\n}\n
      build_tree.rs
      /* Build binary tree: divide and conquer */\nfn dfs(\n    preorder: &[i32],\n    inorder_map: &HashMap<i32, i32>,\n    i: i32,\n    l: i32,\n    r: i32,\n) -> Option<Rc<RefCell<TreeNode>>> {\n    // Terminate when the subtree interval is empty\n    if r - l < 0 {\n        return None;\n    }\n    // Initialize the root node\n    let root = TreeNode::new(preorder[i as usize]);\n    // Query m to divide the left and right subtrees\n    let m = inorder_map.get(&preorder[i as usize]).unwrap();\n    // Subproblem: build the left subtree\n    root.borrow_mut().left = dfs(preorder, inorder_map, i + 1, l, m - 1);\n    // Subproblem: build the right subtree\n    root.borrow_mut().right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r);\n    // Return the root node\n    Some(root)\n}\n\n/* Build binary tree */\nfn build_tree(preorder: &[i32], inorder: &[i32]) -> Option<Rc<RefCell<TreeNode>>> {\n    // Initialize hash map, storing the mapping from inorder elements to indices\n    let mut inorder_map: HashMap<i32, i32> = HashMap::new();\n    for i in 0..inorder.len() {\n        inorder_map.insert(inorder[i], i as i32);\n    }\n    let root = dfs(preorder, &inorder_map, 0, 0, inorder.len() as i32 - 1);\n    root\n}\n
      build_tree.c
      /* Build binary tree: divide and conquer */\nTreeNode *dfs(int *preorder, int *inorderMap, int i, int l, int r, int size) {\n    // Terminate when the subtree interval is empty\n    if (r - l < 0)\n        return NULL;\n    // Initialize the root node\n    TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));\n    root->val = preorder[i];\n    root->left = NULL;\n    root->right = NULL;\n    // Query m to divide the left and right subtrees\n    int m = inorderMap[preorder[i]];\n    // Subproblem: build the left subtree\n    root->left = dfs(preorder, inorderMap, i + 1, l, m - 1, size);\n    // Subproblem: build the right subtree\n    root->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r, size);\n    // Return the root node\n    return root;\n}\n\n/* Build binary tree */\nTreeNode *buildTree(int *preorder, int preorderSize, int *inorder, int inorderSize) {\n    // Initialize hash map, storing the mapping from inorder elements to indices\n    int *inorderMap = (int *)malloc(sizeof(int) * MAX_SIZE);\n    for (int i = 0; i < inorderSize; i++) {\n        inorderMap[inorder[i]] = i;\n    }\n    TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorderSize - 1, inorderSize);\n    free(inorderMap);\n    return root;\n}\n
      build_tree.kt
      /* Build binary tree: divide and conquer */\nfun dfs(\n    preorder: IntArray,\n    inorderMap: Map<Int?, Int?>,\n    i: Int,\n    l: Int,\n    r: Int\n): TreeNode? {\n    // Terminate when the subtree interval is empty\n    if (r - l < 0) return null\n    // Initialize the root node\n    val root = TreeNode(preorder[i])\n    // Query m to divide the left and right subtrees\n    val m = inorderMap[preorder[i]]!!\n    // Subproblem: build the left subtree\n    root.left = dfs(preorder, inorderMap, i + 1, l, m - 1)\n    // Subproblem: build the right subtree\n    root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r)\n    // Return the root node\n    return root\n}\n\n/* Build binary tree */\nfun buildTree(preorder: IntArray, inorder: IntArray): TreeNode? {\n    // Initialize hash map, storing the mapping from inorder elements to indices\n    val inorderMap = HashMap<Int?, Int?>()\n    for (i in inorder.indices) {\n        inorderMap[inorder[i]] = i\n    }\n    val root = dfs(preorder, inorderMap, 0, 0, inorder.size - 1)\n    return root\n}\n
      build_tree.rb
      ### Build binary tree: divide and conquer ###\ndef dfs(preorder, inorder_map, i, l, r)\n  # Terminate when the subtree interval is empty\n  return if r - l < 0\n\n  # Initialize the root node\n  root = TreeNode.new(preorder[i])\n  # Query m to divide the left and right subtrees\n  m = inorder_map[preorder[i]]\n  # Subproblem: build the left subtree\n  root.left = dfs(preorder, inorder_map, i + 1, l, m - 1)\n  # Subproblem: build the right subtree\n  root.right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r)\n\n  # Return the root node\n  root\nend\n\n### Build binary tree ###\ndef build_tree(preorder, inorder)\n  # Initialize hash map, storing the mapping from inorder elements to indices\n  inorder_map = {}\n  inorder.each_with_index { |val, i| inorder_map[val] = i }\n  dfs(preorder, inorder_map, 0, 0, inorder.length - 1)\nend\n

      Figure 12-8 shows the recursive process of building the binary tree. Each node is established during the downward \"recursion\" process, while each edge (reference) is established during the upward \"return\" process.

      <1><2><3><4><5><6><7><8><9>

      Figure 12-8 \u00a0 Recursive process of building a binary tree

      The division results of the preorder traversal preorder and inorder traversal inorder within each recursive function are shown in Figure 12-9.

      Figure 12-9 \u00a0 Division results in each recursive function

      Let the number of nodes in the tree be \\(n\\). Initializing each node (executing one recursive function dfs()) takes \\(O(1)\\) time. Therefore, the overall time complexity is \\(O(n)\\).

      The hash table stores the mapping from inorder elements to their indices, with a space complexity of \\(O(n)\\). In the worst case, when the binary tree degenerates into a linked list, the recursion depth reaches \\(n\\), using \\(O(n)\\) stack frame space. Therefore, the overall space complexity is \\(O(n)\\).

      "},{"location":"chapter_divide_and_conquer/divide_and_conquer/","title":"12.1 \u00a0 Divide and Conquer Algorithms","text":"

      Divide and conquer is a very important and common algorithm strategy. Divide and conquer is typically implemented based on recursion, consisting of two steps: \"divide\" and \"conquer\".

      1. Divide (partition phase): Recursively divide the original problem into two or more subproblems until the smallest subproblem is reached.
      2. Conquer (merge phase): Starting from the smallest subproblems with known solutions, merge the solutions of subproblems from bottom to top to construct the solution to the original problem.

      As shown in Figure 12-1, \"merge sort\" is one of the typical applications of the divide and conquer strategy.

      1. Divide: Recursively divide the original array (original problem) into two subarrays (subproblems) until the subarray has only one element (smallest subproblem).
      2. Conquer: Merge the sorted subarrays (solutions to subproblems) from bottom to top to obtain a sorted original array (solution to the original problem).

      Figure 12-1 \u00a0 Divide and conquer strategy of merge sort

      "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1211-how-to-determine-divide-and-conquer-problems","title":"12.1.1 \u00a0 How to Determine Divide and Conquer Problems","text":"

      Whether a problem is suitable for solving with divide and conquer can usually be determined based on the following criteria.

      1. The problem can be decomposed: The original problem can be divided into smaller, similar subproblems, and can be recursively divided in the same way.
      2. Subproblems are independent: There is no overlap between subproblems, they are independent of each other and can be solved independently.
      3. Solutions of subproblems can be merged: The solution to the original problem is obtained by merging the solutions of subproblems.

      Clearly, merge sort satisfies these three criteria.

      1. The problem can be decomposed: Recursively divide the array (original problem) into two subarrays (subproblems).
      2. Subproblems are independent: Each subarray can be sorted independently (subproblems can be solved independently).
      3. Solutions of subproblems can be merged: Two sorted subarrays (solutions of subproblems) can be merged into one sorted array (solution of the original problem).
      "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1212-improving-efficiency-through-divide-and-conquer","title":"12.1.2 \u00a0 Improving Efficiency Through Divide and Conquer","text":"

      Divide and conquer can not only effectively solve algorithmic problems but often also improve algorithm efficiency. In sorting algorithms, quick sort, merge sort, and heap sort are faster than selection, bubble, and insertion sort because they apply the divide and conquer strategy.

      This raises the question: Why can divide and conquer improve algorithm efficiency, and what is the underlying logic? In other words, why is dividing a large problem into multiple subproblems, solving the subproblems, and merging their solutions more efficient than directly solving the original problem? This question can be discussed from two aspects: operation count and parallel computation.

      "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1-operation-count-optimization","title":"1. \u00a0 Operation Count Optimization","text":"

      Taking \"bubble sort\" as an example, processing an array of length \\(n\\) requires \\(O(n^2)\\) time. Suppose we divide the array into two subarrays from the midpoint as shown in Figure 12-2, the division requires \\(O(n)\\) time, sorting each subarray requires \\(O((n / 2)^2)\\) time, and merging the two subarrays requires \\(O(n)\\) time, resulting in an overall time complexity of:

      \\[ O(n + (\\frac{n}{2})^2 \\times 2 + n) = O(\\frac{n^2}{2} + 2n) \\]

      Figure 12-2 \u00a0 Bubble sort before and after array division

      Next, we compute the following inequality, where the left and right sides represent the total number of operations before and after division, respectively:

      \\[ \\begin{aligned} n^2 & > \\frac{n^2}{2} + 2n \\newline n^2 - \\frac{n^2}{2} - 2n & > 0 \\newline n(n - 4) & > 0 \\end{aligned} \\]

      This means that when \\(n > 4\\), the number of operations after division is smaller, and sorting efficiency should be higher. Note that the time complexity after division is still quadratic \\(O(n^2)\\), but the constant term in the complexity has become smaller.

      Going further, what if we continuously divide the subarrays from their midpoints into two subarrays until the subarrays have only one element? This approach is actually \"merge sort\", with a time complexity of \\(O(n \\log n)\\).

      Thinking further, what if we set multiple division points and evenly divide the original array into \\(k\\) subarrays? This situation is very similar to \"bucket sort\", which is well-suited for sorting massive amounts of data, with a theoretical time complexity of \\(O(n + k)\\).

      "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#2-parallel-computation-optimization","title":"2. \u00a0 Parallel Computation Optimization","text":"

      We know that the subproblems generated by divide and conquer are independent of each other, so they can typically be solved in parallel. This means divide and conquer can not only reduce the time complexity of algorithms, but also benefits from parallel optimization by operating systems.

      Parallel optimization is particularly effective in multi-core or multi-processor environments, as the system can simultaneously handle multiple subproblems, making fuller use of computing resources and significantly reducing overall runtime.

      For example, in the \"bucket sort\" shown in Figure 12-3, we evenly distribute massive data into various buckets, and the sorting tasks for all buckets can be distributed to various computing units. After completion, the results are merged.

      Figure 12-3 \u00a0 Parallel computation in bucket sort

      "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1213-common-applications-of-divide-and-conquer","title":"12.1.3 \u00a0 Common Applications of Divide and Conquer","text":"

      On one hand, divide and conquer can be used to solve many classic algorithmic problems.

      • Finding the closest pair of points: This algorithm first divides the point set into two parts, then finds the closest pair of points in each part separately, and finally finds the closest pair of points that spans both parts.
      • Large integer multiplication: For example, the Karatsuba algorithm, which decomposes large integer multiplication into several smaller integer multiplications and additions.
      • Matrix multiplication: For example, the Strassen algorithm, which decomposes large matrix multiplication into multiple small matrix multiplications and additions.
      • Hanota problem: The hanota problem can be solved through recursion, which is a typical application of the divide and conquer strategy.
      • Solving inversion pairs: In a sequence, if a preceding number is greater than a following number, these two numbers form an inversion pair. Solving the inversion pair problem can utilize the divide and conquer approach with the help of merge sort.

      On the other hand, divide and conquer is widely applied in the design of algorithms and data structures.

      • Binary search: Binary search divides a sorted array into two parts from the midpoint index, then decides which half to eliminate based on the comparison result between the target value and the middle element value, and performs the same binary operation on the remaining interval.
      • Merge sort: Already introduced at the beginning of this section, no further elaboration needed.
      • Quick sort: Quick sort selects a pivot value, then divides the array into two subarrays, one with elements smaller than the pivot and the other with elements larger than the pivot, then performs the same division operation on these two parts until the subarrays have only one element.
      • Bucket sort: The basic idea of bucket sort is to scatter data into multiple buckets, then sort the elements within each bucket, and finally extract the elements from each bucket in sequence to obtain a sorted array.
      • Trees: For example, binary search trees, AVL trees, red-black trees, B-trees, B+ trees, etc. Their search, insertion, and deletion operations can all be viewed as applications of the divide and conquer strategy.
      • Heaps: A heap is a special complete binary tree, and its various operations, such as insertion, deletion, and heapify, actually imply the divide and conquer idea.
      • Hash tables: Although hash tables do not directly apply divide and conquer, some hash collision resolution solutions indirectly apply the divide and conquer strategy. For example, long linked lists in chaining may be converted to red-black trees to improve query efficiency.

      It can be seen that divide and conquer is a \"subtly pervasive\" algorithmic idea, embedded in various algorithms and data structures.

      "},{"location":"chapter_divide_and_conquer/hanota_problem/","title":"12.4 \u00a0 Hanota Problem","text":"

      In merge sort and building binary trees, we decompose the original problem into two subproblems, each half the size of the original problem. However, for the hanota problem, we adopt a different decomposition strategy.

      Question

      Given three pillars, denoted as A, B, and C. Initially, pillar A has \\(n\\) discs stacked on it, arranged from top to bottom in ascending order of size. Our task is to move these \\(n\\) discs to pillar C while maintaining their original order (as shown in Figure 12-10). The following rules must be followed when moving the discs.

      1. A disc can only be taken from the top of one pillar and placed on top of another pillar.
      2. Only one disc can be moved at a time.
      3. A smaller disc must always be on top of a larger disc.

      Figure 12-10 \u00a0 Example of the hanota problem

      We denote the hanota problem of size \\(i\\) as \\(f(i)\\). For example, \\(f(3)\\) represents moving \\(3\\) discs from A to C.

      "},{"location":"chapter_divide_and_conquer/hanota_problem/#1-considering-the-base-cases","title":"1. \u00a0 Considering the Base Cases","text":"

      As shown in Figure 12-11, for problem \\(f(1)\\), when there is only one disc, we can move it directly from A to C.

      <1><2>

      Figure 12-11 \u00a0 Solution for a problem of size 1

      As shown in Figure 12-12, for problem \\(f(2)\\), when there are two discs, since we must always keep the smaller disc on top of the larger disc, we need to use B to assist in the move.

      1. First, move the smaller disc from A to B.
      2. Then move the larger disc from A to C.
      3. Finally, move the smaller disc from B to C.
      <1><2><3><4>

      Figure 12-12 \u00a0 Solution for a problem of size 2

      The process of solving problem \\(f(2)\\) can be summarized as: moving two discs from A to C with the help of B. Here, C is called the target pillar, and B is called the buffer pillar.

      "},{"location":"chapter_divide_and_conquer/hanota_problem/#2-subproblem-decomposition","title":"2. \u00a0 Subproblem Decomposition","text":"

      For problem \\(f(3)\\), when there are three discs, the situation becomes slightly more complex.

      Since we already know the solutions to \\(f(1)\\) and \\(f(2)\\), we can think from a divide and conquer perspective, treating the top two discs on A as a whole, and execute the steps shown in Figure 12-13. This successfully moves the three discs from A to C.

      1. Let B be the target pillar and C be the buffer pillar, and move two discs from A to B.
      2. Move the remaining disc from A directly to C.
      3. Let C be the target pillar and A be the buffer pillar, and move two discs from B to C.
      <1><2><3><4>

      Figure 12-13 \u00a0 Solution for a problem of size 3

      Essentially, we divide problem \\(f(3)\\) into two subproblems \\(f(2)\\) and one subproblem \\(f(1)\\). By solving these three subproblems in order, the original problem is solved. This shows that the subproblems are independent and their solutions can be merged.

      From this, we can summarize the divide and conquer strategy for solving the hanota problem shown in Figure 12-14: divide the original problem \\(f(n)\\) into two subproblems \\(f(n-1)\\) and one subproblem \\(f(1)\\), and solve these three subproblems in the following order.

      1. Move \\(n-1\\) discs from A to B with the help of C.
      2. Move the remaining \\(1\\) disc directly from A to C.
      3. Move \\(n-1\\) discs from B to C with the help of A.

      For these two subproblems \\(f(n-1)\\), we can recursively divide them in the same way until reaching the smallest subproblem \\(f(1)\\). The solution to \\(f(1)\\) is known and requires only one move operation.

      Figure 12-14 \u00a0 Divide and conquer strategy for solving the hanota problem

      "},{"location":"chapter_divide_and_conquer/hanota_problem/#3-code-implementation","title":"3. \u00a0 Code Implementation","text":"

      In the code, we declare a recursive function dfs(i, src, buf, tar), whose purpose is to move the top \\(i\\) discs from pillar src to target pillar tar with the help of buffer pillar buf:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hanota.py
      def move(src: list[int], tar: list[int]):\n    \"\"\"Move a disk\"\"\"\n    # Take out a disk from the top of src\n    pan = src.pop()\n    # Place the disk on top of tar\n    tar.append(pan)\n\ndef dfs(i: int, src: list[int], buf: list[int], tar: list[int]):\n    \"\"\"Solve the Tower of Hanoi problem f(i)\"\"\"\n    # If there is only one disk left in src, move it directly to tar\n    if i == 1:\n        move(src, tar)\n        return\n    # Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n    dfs(i - 1, src, tar, buf)\n    # Subproblem f(1): move the remaining disk from src to tar\n    move(src, tar)\n    # Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n    dfs(i - 1, buf, src, tar)\n\ndef solve_hanota(A: list[int], B: list[int], C: list[int]):\n    \"\"\"Solve the Tower of Hanoi problem\"\"\"\n    n = len(A)\n    # Move the top n disks from A to C using B\n    dfs(n, A, B, C)\n
      hanota.cpp
      /* Move a disk */\nvoid move(vector<int> &src, vector<int> &tar) {\n    // Take out a disk from the top of src\n    int pan = src.back();\n    src.pop_back();\n    // Place the disk on top of tar\n    tar.push_back(pan);\n}\n\n/* Solve the Tower of Hanoi problem f(i) */\nvoid dfs(int i, vector<int> &src, vector<int> &buf, vector<int> &tar) {\n    // If there is only one disk left in src, move it directly to tar\n    if (i == 1) {\n        move(src, tar);\n        return;\n    }\n    // Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n    dfs(i - 1, src, tar, buf);\n    // Subproblem f(1): move the remaining disk from src to tar\n    move(src, tar);\n    // Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n    dfs(i - 1, buf, src, tar);\n}\n\n/* Solve the Tower of Hanoi problem */\nvoid solveHanota(vector<int> &A, vector<int> &B, vector<int> &C) {\n    int n = A.size();\n    // Move the top n disks from A to C using B\n    dfs(n, A, B, C);\n}\n
      hanota.java
      /* Move a disk */\nvoid move(List<Integer> src, List<Integer> tar) {\n    // Take out a disk from the top of src\n    Integer pan = src.remove(src.size() - 1);\n    // Place the disk on top of tar\n    tar.add(pan);\n}\n\n/* Solve the Tower of Hanoi problem f(i) */\nvoid dfs(int i, List<Integer> src, List<Integer> buf, List<Integer> tar) {\n    // If there is only one disk left in src, move it directly to tar\n    if (i == 1) {\n        move(src, tar);\n        return;\n    }\n    // Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n    dfs(i - 1, src, tar, buf);\n    // Subproblem f(1): move the remaining disk from src to tar\n    move(src, tar);\n    // Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n    dfs(i - 1, buf, src, tar);\n}\n\n/* Solve the Tower of Hanoi problem */\nvoid solveHanota(List<Integer> A, List<Integer> B, List<Integer> C) {\n    int n = A.size();\n    // Move the top n disks from A to C using B\n    dfs(n, A, B, C);\n}\n
      hanota.cs
      /* Move a disk */\nvoid Move(List<int> src, List<int> tar) {\n    // Take out a disk from the top of src\n    int pan = src[^1];\n    src.RemoveAt(src.Count - 1);\n    // Place the disk on top of tar\n    tar.Add(pan);\n}\n\n/* Solve the Tower of Hanoi problem f(i) */\nvoid DFS(int i, List<int> src, List<int> buf, List<int> tar) {\n    // If there is only one disk left in src, move it directly to tar\n    if (i == 1) {\n        Move(src, tar);\n        return;\n    }\n    // Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n    DFS(i - 1, src, tar, buf);\n    // Subproblem f(1): move the remaining disk from src to tar\n    Move(src, tar);\n    // Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n    DFS(i - 1, buf, src, tar);\n}\n\n/* Solve the Tower of Hanoi problem */\nvoid SolveHanota(List<int> A, List<int> B, List<int> C) {\n    int n = A.Count;\n    // Move the top n disks from A to C using B\n    DFS(n, A, B, C);\n}\n
      hanota.go
      /* Move a disk */\nfunc move(src, tar *list.List) {\n    // Take out a disk from the top of src\n    pan := src.Back()\n    // Place the disk on top of tar\n    tar.PushBack(pan.Value)\n    // Remove top disk from src\n    src.Remove(pan)\n}\n\n/* Solve the Tower of Hanoi problem f(i) */\nfunc dfsHanota(i int, src, buf, tar *list.List) {\n    // If there is only one disk left in src, move it directly to tar\n    if i == 1 {\n        move(src, tar)\n        return\n    }\n    // Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n    dfsHanota(i-1, src, tar, buf)\n    // Subproblem f(1): move the remaining disk from src to tar\n    move(src, tar)\n    // Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n    dfsHanota(i-1, buf, src, tar)\n}\n\n/* Solve the Tower of Hanoi problem */\nfunc solveHanota(A, B, C *list.List) {\n    n := A.Len()\n    // Move the top n disks from A to C using B\n    dfsHanota(n, A, B, C)\n}\n
      hanota.swift
      /* Move a disk */\nfunc move(src: inout [Int], tar: inout [Int]) {\n    // Take out a disk from the top of src\n    let pan = src.popLast()!\n    // Place the disk on top of tar\n    tar.append(pan)\n}\n\n/* Solve the Tower of Hanoi problem f(i) */\nfunc dfs(i: Int, src: inout [Int], buf: inout [Int], tar: inout [Int]) {\n    // If there is only one disk left in src, move it directly to tar\n    if i == 1 {\n        move(src: &src, tar: &tar)\n        return\n    }\n    // Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n    dfs(i: i - 1, src: &src, buf: &tar, tar: &buf)\n    // Subproblem f(1): move the remaining disk from src to tar\n    move(src: &src, tar: &tar)\n    // Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n    dfs(i: i - 1, src: &buf, buf: &src, tar: &tar)\n}\n\n/* Solve the Tower of Hanoi problem */\nfunc solveHanota(A: inout [Int], B: inout [Int], C: inout [Int]) {\n    let n = A.count\n    // The tail of the list is the top of the rod\n    // Move top n disks from src to C using B\n    dfs(i: n, src: &A, buf: &B, tar: &C)\n}\n
      hanota.js
      /* Move a disk */\nfunction move(src, tar) {\n    // Take out a disk from the top of src\n    const pan = src.pop();\n    // Place the disk on top of tar\n    tar.push(pan);\n}\n\n/* Solve the Tower of Hanoi problem f(i) */\nfunction dfs(i, src, buf, tar) {\n    // If there is only one disk left in src, move it directly to tar\n    if (i === 1) {\n        move(src, tar);\n        return;\n    }\n    // Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n    dfs(i - 1, src, tar, buf);\n    // Subproblem f(1): move the remaining disk from src to tar\n    move(src, tar);\n    // Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n    dfs(i - 1, buf, src, tar);\n}\n\n/* Solve the Tower of Hanoi problem */\nfunction solveHanota(A, B, C) {\n    const n = A.length;\n    // Move the top n disks from A to C using B\n    dfs(n, A, B, C);\n}\n
      hanota.ts
      /* Move a disk */\nfunction move(src: number[], tar: number[]): void {\n    // Take out a disk from the top of src\n    const pan = src.pop();\n    // Place the disk on top of tar\n    tar.push(pan);\n}\n\n/* Solve the Tower of Hanoi problem f(i) */\nfunction dfs(i: number, src: number[], buf: number[], tar: number[]): void {\n    // If there is only one disk left in src, move it directly to tar\n    if (i === 1) {\n        move(src, tar);\n        return;\n    }\n    // Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n    dfs(i - 1, src, tar, buf);\n    // Subproblem f(1): move the remaining disk from src to tar\n    move(src, tar);\n    // Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n    dfs(i - 1, buf, src, tar);\n}\n\n/* Solve the Tower of Hanoi problem */\nfunction solveHanota(A: number[], B: number[], C: number[]): void {\n    const n = A.length;\n    // Move the top n disks from A to C using B\n    dfs(n, A, B, C);\n}\n
      hanota.dart
      /* Move a disk */\nvoid move(List<int> src, List<int> tar) {\n  // Take out a disk from the top of src\n  int pan = src.removeLast();\n  // Place the disk on top of tar\n  tar.add(pan);\n}\n\n/* Solve the Tower of Hanoi problem f(i) */\nvoid dfs(int i, List<int> src, List<int> buf, List<int> tar) {\n  // If there is only one disk left in src, move it directly to tar\n  if (i == 1) {\n    move(src, tar);\n    return;\n  }\n  // Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n  dfs(i - 1, src, tar, buf);\n  // Subproblem f(1): move the remaining disk from src to tar\n  move(src, tar);\n  // Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n  dfs(i - 1, buf, src, tar);\n}\n\n/* Solve the Tower of Hanoi problem */\nvoid solveHanota(List<int> A, List<int> B, List<int> C) {\n  int n = A.length;\n  // Move the top n disks from A to C using B\n  dfs(n, A, B, C);\n}\n
      hanota.rs
      /* Move a disk */\nfn move_pan(src: &mut Vec<i32>, tar: &mut Vec<i32>) {\n    // Take out a disk from the top of src\n    let pan = src.pop().unwrap();\n    // Place the disk on top of tar\n    tar.push(pan);\n}\n\n/* Solve the Tower of Hanoi problem f(i) */\nfn dfs(i: i32, src: &mut Vec<i32>, buf: &mut Vec<i32>, tar: &mut Vec<i32>) {\n    // If there is only one disk left in src, move it directly to tar\n    if i == 1 {\n        move_pan(src, tar);\n        return;\n    }\n    // Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n    dfs(i - 1, src, tar, buf);\n    // Subproblem f(1): move the remaining disk from src to tar\n    move_pan(src, tar);\n    // Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n    dfs(i - 1, buf, src, tar);\n}\n\n/* Solve the Tower of Hanoi problem */\nfn solve_hanota(A: &mut Vec<i32>, B: &mut Vec<i32>, C: &mut Vec<i32>) {\n    let n = A.len() as i32;\n    // Move the top n disks from A to C using B\n    dfs(n, A, B, C);\n}\n
      hanota.c
      /* Move a disk */\nvoid move(int *src, int *srcSize, int *tar, int *tarSize) {\n    // Take out a disk from the top of src\n    int pan = src[*srcSize - 1];\n    src[*srcSize - 1] = 0;\n    (*srcSize)--;\n    // Place the disk on top of tar\n    tar[*tarSize] = pan;\n    (*tarSize)++;\n}\n\n/* Solve the Tower of Hanoi problem f(i) */\nvoid dfs(int i, int *src, int *srcSize, int *buf, int *bufSize, int *tar, int *tarSize) {\n    // If there is only one disk left in src, move it directly to tar\n    if (i == 1) {\n        move(src, srcSize, tar, tarSize);\n        return;\n    }\n    // Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n    dfs(i - 1, src, srcSize, tar, tarSize, buf, bufSize);\n    // Subproblem f(1): move the remaining disk from src to tar\n    move(src, srcSize, tar, tarSize);\n    // Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n    dfs(i - 1, buf, bufSize, src, srcSize, tar, tarSize);\n}\n\n/* Solve the Tower of Hanoi problem */\nvoid solveHanota(int *A, int *ASize, int *B, int *BSize, int *C, int *CSize) {\n    // Move the top n disks from A to C using B\n    dfs(*ASize, A, ASize, B, BSize, C, CSize);\n}\n
      hanota.kt
      /* Move a disk */\nfun move(src: MutableList<Int>, tar: MutableList<Int>) {\n    // Take out a disk from the top of src\n    val pan = src.removeAt(src.size - 1)\n    // Place the disk on top of tar\n    tar.add(pan)\n}\n\n/* Solve the Tower of Hanoi problem f(i) */\nfun dfs(i: Int, src: MutableList<Int>, buf: MutableList<Int>, tar: MutableList<Int>) {\n    // If there is only one disk left in src, move it directly to tar\n    if (i == 1) {\n        move(src, tar)\n        return\n    }\n    // Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n    dfs(i - 1, src, tar, buf)\n    // Subproblem f(1): move the remaining disk from src to tar\n    move(src, tar)\n    // Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n    dfs(i - 1, buf, src, tar)\n}\n\n/* Solve the Tower of Hanoi problem */\nfun solveHanota(A: MutableList<Int>, B: MutableList<Int>, C: MutableList<Int>) {\n    val n = A.size\n    // Move the top n disks from A to C using B\n    dfs(n, A, B, C)\n}\n
      hanota.rb
      ### Move one disk ###\ndef move(src, tar)\n  # Take out a disk from the top of src\n  pan = src.pop\n  # Place the disk on top of tar\n  tar << pan\nend\n\n### Solve Tower of Hanoi f(i) ###\ndef dfs(i, src, buf, tar)\n  # If there is only one disk left in src, move it directly to tar\n  if i == 1\n    move(src, tar)\n    return\n  end\n\n  # Subproblem f(i-1): move the top i-1 disks from src to buf using tar\n  dfs(i - 1, src, tar, buf)\n  # Subproblem f(1): move the remaining disk from src to tar\n  move(src, tar)\n  # Subproblem f(i-1): move the top i-1 disks from buf to tar using src\n  dfs(i - 1, buf, src, tar)\nend\n\n### Solve Tower of Hanoi ###\ndef solve_hanota(_A, _B, _C)\n  n = _A.length\n  # Move the top n disks from A to C using B\n  dfs(n, _A, _B, _C)\nend\n

      As shown in Figure 12-15, the hanota problem forms a recursion tree of height \\(n\\), where each node represents a subproblem corresponding to an invocation of the dfs() function, therefore the time complexity is \\(O(2^n)\\) and the space complexity is \\(O(n)\\).

      Figure 12-15 \u00a0 Recursion tree of the hanota problem

      Quote

      The hanota problem originates from an ancient legend. In a temple in ancient India, monks had three tall diamond pillars and \\(64\\) golden discs of different sizes. The monks continuously moved the discs, believing that when the last disc was correctly placed, the world would come to an end.

      However, even if the monks moved one disc per second, it would take approximately \\(2^{64} \\approx 1.84\u00d710^{19}\\) seconds, which is about \\(5850\\) billion years, far exceeding current estimates of the age of the universe. Therefore, if this legend is true, we should not need to worry about the end of the world.

      "},{"location":"chapter_divide_and_conquer/summary/","title":"12.5 \u00a0 Summary","text":""},{"location":"chapter_divide_and_conquer/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • Divide and conquer is a common algorithm design strategy, consisting of two phases: divide (partition) and conquer (merge), typically implemented based on recursion.
      • The criteria for determining whether a problem is a divide and conquer problem include: whether the problem can be decomposed, whether subproblems are independent, and whether subproblems can be merged.
      • Merge sort is a typical application of the divide and conquer strategy. It recursively divides an array into two equal-length subarrays until only one element remains, then merges them layer by layer to complete the sorting.
      • Introducing the divide and conquer strategy can often improve algorithm efficiency. On one hand, the divide and conquer strategy reduces the number of operations; on the other hand, it facilitates parallel optimization of the system after division.
      • Divide and conquer can both solve many algorithmic problems and is widely applied in data structure and algorithm design, appearing everywhere.
      • Compared to brute-force search, adaptive search is more efficient. Search algorithms with time complexity of \\(O(\\log n)\\) are typically implemented based on the divide and conquer strategy.
      • Binary search is another typical application of divide and conquer. It does not include the step of merging solutions of subproblems. We can implement binary search through recursive divide and conquer.
      • In the problem of building a binary tree, building the tree (original problem) can be divided into building the left subtree and right subtree (subproblems), which can be achieved by dividing the index intervals of the preorder and inorder traversals.
      • In the hanota problem, a problem of size \\(n\\) can be divided into two subproblems of size \\(n-1\\) and one subproblem of size \\(1\\). After solving these three subproblems in order, the original problem is solved.
      "},{"location":"chapter_dynamic_programming/","title":"Chapter 14. \u00a0 Dynamic Programming","text":"

      Abstract

      Streams converge into rivers, rivers converge into the sea.

      Dynamic programming gathers solutions to small problems into answers to large problems, step by step guiding us to the shore of problem-solving.

      "},{"location":"chapter_dynamic_programming/#chapter-contents","title":"Chapter contents","text":"
      • 14.1 \u00a0 Introduction to Dynamic Programming
      • 14.2 \u00a0 Characteristics of Dynamic Programming Problems
      • 14.3 \u00a0 Dynamic Programming Problem-Solving Approach
      • 14.4 \u00a0 0-1 Knapsack Problem
      • 14.5 \u00a0 Unbounded Knapsack Problem
      • 14.6 \u00a0 Edit Distance Problem
      • 14.7 \u00a0 Summary
      "},{"location":"chapter_dynamic_programming/dp_problem_features/","title":"14.2 \u00a0 Characteristics of Dynamic Programming Problems","text":"

      In the previous section, we learned how dynamic programming solves the original problem by decomposing it into subproblems. In fact, subproblem decomposition is a general algorithmic approach, with different emphases in divide and conquer, dynamic programming, and backtracking.

      • Divide and conquer algorithms recursively divide the original problem into multiple independent subproblems until the smallest subproblems are reached, and merge the solutions to the subproblems during backtracking to ultimately obtain the solution to the original problem.
      • Dynamic programming also recursively decomposes problems, but the main difference from divide and conquer algorithms is that subproblems in dynamic programming are interdependent, and many overlapping subproblems appear during the decomposition process.
      • Backtracking algorithms enumerate all possible solutions through trial and error, and avoid unnecessary search branches through pruning. The solution to the original problem consists of a series of decision steps, and we can regard the subsequence before each decision step as a subproblem.

      In fact, dynamic programming is commonly used to solve optimization problems, which not only contain overlapping subproblems but also have two other major characteristics: optimal substructure and no aftereffects.

      "},{"location":"chapter_dynamic_programming/dp_problem_features/#1421-optimal-substructure","title":"14.2.1 \u00a0 Optimal Substructure","text":"

      We make a slight modification to the stair climbing problem to make it more suitable for demonstrating the concept of optimal substructure.

      Climbing stairs with minimum cost

      Given a staircase, where you can climb \\(1\\) or \\(2\\) steps at a time, and each step has a non-negative integer representing the cost you need to pay at that step. Given a non-negative integer array \\(cost\\), where \\(cost[i]\\) represents the cost at the \\(i\\)-th step, and \\(cost[0]\\) is the ground (starting point). What is the minimum cost required to reach the top?

      As shown in Figure 14-6, if the costs of the \\(1\\)st, \\(2\\)nd, and \\(3\\)rd steps are \\(1\\), \\(10\\), and \\(1\\) respectively, then climbing from the ground to the \\(3\\)rd step requires a minimum cost of \\(2\\).

      Figure 14-6 \u00a0 Minimum cost to climb to the 3rd step

      Let \\(dp[i]\\) be the accumulated cost of climbing to the \\(i\\)-th step. Since the \\(i\\)-th step can only come from the \\(i-1\\)-th or \\(i-2\\)-th step, \\(dp[i]\\) can only equal \\(dp[i-1] + cost[i]\\) or \\(dp[i-2] + cost[i]\\). To minimize the cost, we should choose the smaller of the two:

      \\[ dp[i] = \\min(dp[i-1], dp[i-2]) + cost[i] \\]

      This leads us to the meaning of optimal substructure: the optimal solution to the original problem is constructed from the optimal solutions to the subproblems.

      This problem clearly has optimal substructure: we select the better one from the optimal solutions to the two subproblems \\(dp[i-1]\\) and \\(dp[i-2]\\), and use it to construct the optimal solution to the original problem \\(dp[i]\\).

      So, does the stair climbing problem from the previous section have optimal substructure? Its goal is to find the number of ways, which seems to be a counting problem, but if we change the question: \"Find the maximum number of ways\". We surprisingly discover that although the problem before and after modification are equivalent, the optimal substructure has emerged: the maximum number of ways for the \\(n\\)-th step equals the sum of the maximum number of ways for the \\(n-1\\)-th and \\(n-2\\)-th steps. Therefore, the interpretation of optimal substructure is quite flexible and will have different meanings in different problems.

      According to the state transition equation and the initial states \\(dp[1] = cost[1]\\) and \\(dp[2] = cost[2]\\), we can obtain the dynamic programming code:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_cost_climbing_stairs_dp.py
      def min_cost_climbing_stairs_dp(cost: list[int]) -> int:\n    \"\"\"Minimum cost climbing stairs: Dynamic programming\"\"\"\n    n = len(cost) - 1\n    if n == 1 or n == 2:\n        return cost[n]\n    # Initialize dp table, used to store solutions to subproblems\n    dp = [0] * (n + 1)\n    # Initial state: preset the solution to the smallest subproblem\n    dp[1], dp[2] = cost[1], cost[2]\n    # State transition: gradually solve larger subproblems from smaller ones\n    for i in range(3, n + 1):\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n    return dp[n]\n
      min_cost_climbing_stairs_dp.cpp
      /* Minimum cost climbing stairs: Dynamic programming */\nint minCostClimbingStairsDP(vector<int> &cost) {\n    int n = cost.size() - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // Initialize dp table, used to store solutions to subproblems\n    vector<int> dp(n + 1);\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
      min_cost_climbing_stairs_dp.java
      /* Minimum cost climbing stairs: Dynamic programming */\nint minCostClimbingStairsDP(int[] cost) {\n    int n = cost.length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // Initialize dp table, used to store solutions to subproblems\n    int[] dp = new int[n + 1];\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
      min_cost_climbing_stairs_dp.cs
      /* Minimum cost climbing stairs: Dynamic programming */\nint MinCostClimbingStairsDP(int[] cost) {\n    int n = cost.Length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // Initialize dp table, used to store solutions to subproblems\n    int[] dp = new int[n + 1];\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i] = Math.Min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
      min_cost_climbing_stairs_dp.go
      /* Minimum cost climbing stairs: Dynamic programming */\nfunc minCostClimbingStairsDP(cost []int) int {\n    n := len(cost) - 1\n    if n == 1 || n == 2 {\n        return cost[n]\n    }\n    min := func(a, b int) int {\n        if a < b {\n            return a\n        }\n        return b\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    dp := make([]int, n+1)\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = cost[1]\n    dp[2] = cost[2]\n    // State transition: gradually solve larger subproblems from smaller ones\n    for i := 3; i <= n; i++ {\n        dp[i] = min(dp[i-1], dp[i-2]) + cost[i]\n    }\n    return dp[n]\n}\n
      min_cost_climbing_stairs_dp.swift
      /* Minimum cost climbing stairs: Dynamic programming */\nfunc minCostClimbingStairsDP(cost: [Int]) -> Int {\n    let n = cost.count - 1\n    if n == 1 || n == 2 {\n        return cost[n]\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    var dp = Array(repeating: 0, count: n + 1)\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = cost[1]\n    dp[2] = cost[2]\n    // State transition: gradually solve larger subproblems from smaller ones\n    for i in 3 ... n {\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n    }\n    return dp[n]\n}\n
      min_cost_climbing_stairs_dp.js
      /* Minimum cost climbing stairs: Dynamic programming */\nfunction minCostClimbingStairsDP(cost) {\n    const n = cost.length - 1;\n    if (n === 1 || n === 2) {\n        return cost[n];\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    const dp = new Array(n + 1);\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (let i = 3; i <= n; i++) {\n        dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
      min_cost_climbing_stairs_dp.ts
      /* Minimum cost climbing stairs: Dynamic programming */\nfunction minCostClimbingStairsDP(cost: Array<number>): number {\n    const n = cost.length - 1;\n    if (n === 1 || n === 2) {\n        return cost[n];\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    const dp = new Array(n + 1);\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (let i = 3; i <= n; i++) {\n        dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
      min_cost_climbing_stairs_dp.dart
      /* Minimum cost climbing stairs: Dynamic programming */\nint minCostClimbingStairsDP(List<int> cost) {\n  int n = cost.length - 1;\n  if (n == 1 || n == 2) return cost[n];\n  // Initialize dp table, used to store solutions to subproblems\n  List<int> dp = List.filled(n + 1, 0);\n  // Initial state: preset the solution to the smallest subproblem\n  dp[1] = cost[1];\n  dp[2] = cost[2];\n  // State transition: gradually solve larger subproblems from smaller ones\n  for (int i = 3; i <= n; i++) {\n    dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n  }\n  return dp[n];\n}\n
      min_cost_climbing_stairs_dp.rs
      /* Minimum cost climbing stairs: Dynamic programming */\nfn min_cost_climbing_stairs_dp(cost: &[i32]) -> i32 {\n    let n = cost.len() - 1;\n    if n == 1 || n == 2 {\n        return cost[n];\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    let mut dp = vec![-1; n + 1];\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // State transition: gradually solve larger subproblems from smaller ones\n    for i in 3..=n {\n        dp[i] = cmp::min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    dp[n]\n}\n
      min_cost_climbing_stairs_dp.c
      /* Minimum cost climbing stairs: Dynamic programming */\nint minCostClimbingStairsDP(int cost[], int costSize) {\n    int n = costSize - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // Initialize dp table, used to store solutions to subproblems\n    int *dp = calloc(n + 1, sizeof(int));\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i] = myMin(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    int res = dp[n];\n    // Free memory\n    free(dp);\n    return res;\n}\n
      min_cost_climbing_stairs_dp.kt
      /* Minimum cost climbing stairs: Dynamic programming */\nfun minCostClimbingStairsDP(cost: IntArray): Int {\n    val n = cost.size - 1\n    if (n == 1 || n == 2) return cost[n]\n    // Initialize dp table, used to store solutions to subproblems\n    val dp = IntArray(n + 1)\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = cost[1]\n    dp[2] = cost[2]\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (i in 3..n) {\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n    }\n    return dp[n]\n}\n
      min_cost_climbing_stairs_dp.rb
      ### Minimum cost climbing stairs: DP ###\ndef min_cost_climbing_stairs_dp(cost)\n  n = cost.length - 1\n  return cost[n] if n == 1 || n == 2\n  # Initialize dp table, used to store solutions to subproblems\n  dp = Array.new(n + 1, 0)\n  # Initial state: preset the solution to the smallest subproblem\n  dp[1], dp[2] = cost[1], cost[2]\n  # State transition: gradually solve larger subproblems from smaller ones\n  (3...(n + 1)).each { |i| dp[i] = [dp[i - 1], dp[i - 2]].min + cost[i] }\n  dp[n]\nend\n

      Figure 14-7 shows the dynamic programming process for the above code.

      Figure 14-7 \u00a0 Dynamic programming process for climbing stairs with minimum cost

      This problem can also be space-optimized, compressing from one dimension to zero, reducing the space complexity from \\(O(n)\\) to \\(O(1)\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_cost_climbing_stairs_dp.py
      def min_cost_climbing_stairs_dp_comp(cost: list[int]) -> int:\n    \"\"\"Minimum cost climbing stairs: Space-optimized dynamic programming\"\"\"\n    n = len(cost) - 1\n    if n == 1 or n == 2:\n        return cost[n]\n    a, b = cost[1], cost[2]\n    for i in range(3, n + 1):\n        a, b = b, min(a, b) + cost[i]\n    return b\n
      min_cost_climbing_stairs_dp.cpp
      /* Minimum cost climbing stairs: Space-optimized dynamic programming */\nint minCostClimbingStairsDPComp(vector<int> &cost) {\n    int n = cost.size() - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
      min_cost_climbing_stairs_dp.java
      /* Minimum cost climbing stairs: Space-optimized dynamic programming */\nint minCostClimbingStairsDPComp(int[] cost) {\n    int n = cost.length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = Math.min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
      min_cost_climbing_stairs_dp.cs
      /* Minimum cost climbing stairs: Space-optimized dynamic programming */\nint MinCostClimbingStairsDPComp(int[] cost) {\n    int n = cost.Length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = Math.Min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
      min_cost_climbing_stairs_dp.go
      /* Minimum cost climbing stairs: Space-optimized dynamic programming */\nfunc minCostClimbingStairsDPComp(cost []int) int {\n    n := len(cost) - 1\n    if n == 1 || n == 2 {\n        return cost[n]\n    }\n    min := func(a, b int) int {\n        if a < b {\n            return a\n        }\n        return b\n    }\n    // Initial state: preset the solution to the smallest subproblem\n    a, b := cost[1], cost[2]\n    // State transition: gradually solve larger subproblems from smaller ones\n    for i := 3; i <= n; i++ {\n        tmp := b\n        b = min(a, tmp) + cost[i]\n        a = tmp\n    }\n    return b\n}\n
      min_cost_climbing_stairs_dp.swift
      /* Minimum cost climbing stairs: Space-optimized dynamic programming */\nfunc minCostClimbingStairsDPComp(cost: [Int]) -> Int {\n    let n = cost.count - 1\n    if n == 1 || n == 2 {\n        return cost[n]\n    }\n    var (a, b) = (cost[1], cost[2])\n    for i in 3 ... n {\n        (a, b) = (b, min(a, b) + cost[i])\n    }\n    return b\n}\n
      min_cost_climbing_stairs_dp.js
      /* Minimum cost climbing stairs: Space-optimized dynamic programming */\nfunction minCostClimbingStairsDPComp(cost) {\n    const n = cost.length - 1;\n    if (n === 1 || n === 2) {\n        return cost[n];\n    }\n    let a = cost[1],\n        b = cost[2];\n    for (let i = 3; i <= n; i++) {\n        const tmp = b;\n        b = Math.min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
      min_cost_climbing_stairs_dp.ts
      /* Minimum cost climbing stairs: Space-optimized dynamic programming */\nfunction minCostClimbingStairsDPComp(cost: Array<number>): number {\n    const n = cost.length - 1;\n    if (n === 1 || n === 2) {\n        return cost[n];\n    }\n    let a = cost[1],\n        b = cost[2];\n    for (let i = 3; i <= n; i++) {\n        const tmp = b;\n        b = Math.min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
      min_cost_climbing_stairs_dp.dart
      /* Minimum cost climbing stairs: Space-optimized dynamic programming */\nint minCostClimbingStairsDPComp(List<int> cost) {\n  int n = cost.length - 1;\n  if (n == 1 || n == 2) return cost[n];\n  int a = cost[1], b = cost[2];\n  for (int i = 3; i <= n; i++) {\n    int tmp = b;\n    b = min(a, tmp) + cost[i];\n    a = tmp;\n  }\n  return b;\n}\n
      min_cost_climbing_stairs_dp.rs
      /* Minimum cost climbing stairs: Space-optimized dynamic programming */\nfn min_cost_climbing_stairs_dp_comp(cost: &[i32]) -> i32 {\n    let n = cost.len() - 1;\n    if n == 1 || n == 2 {\n        return cost[n];\n    };\n    let (mut a, mut b) = (cost[1], cost[2]);\n    for i in 3..=n {\n        let tmp = b;\n        b = cmp::min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    b\n}\n
      min_cost_climbing_stairs_dp.c
      /* Minimum cost climbing stairs: Space-optimized dynamic programming */\nint minCostClimbingStairsDPComp(int cost[], int costSize) {\n    int n = costSize - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = myMin(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
      min_cost_climbing_stairs_dp.kt
      /* Minimum cost climbing stairs: Space-optimized dynamic programming */\nfun minCostClimbingStairsDPComp(cost: IntArray): Int {\n    val n = cost.size - 1\n    if (n == 1 || n == 2) return cost[n]\n    var a = cost[1]\n    var b = cost[2]\n    for (i in 3..n) {\n        val tmp = b\n        b = min(a, tmp) + cost[i]\n        a = tmp\n    }\n    return b\n}\n
      min_cost_climbing_stairs_dp.rb
      ### Minimum cost climbing stairs: DP ###\ndef min_cost_climbing_stairs_dp(cost)\n  n = cost.length - 1\n  return cost[n] if n == 1 || n == 2\n  # Initialize dp table, used to store solutions to subproblems\n  dp = Array.new(n + 1, 0)\n  # Initial state: preset the solution to the smallest subproblem\n  dp[1], dp[2] = cost[1], cost[2]\n  # State transition: gradually solve larger subproblems from smaller ones\n  (3...(n + 1)).each { |i| dp[i] = [dp[i - 1], dp[i - 2]].min + cost[i] }\n  dp[n]\nend\n\n# Minimum cost climbing stairs: Space-optimized dynamic programming\ndef min_cost_climbing_stairs_dp_comp(cost)\n  n = cost.length - 1\n  return cost[n] if n == 1 || n == 2\n  a, b = cost[1], cost[2]\n  (3...(n + 1)).each { |i| a, b = b, [a, b].min + cost[i] }\n  b\nend\n
      "},{"location":"chapter_dynamic_programming/dp_problem_features/#1422-no-aftereffects","title":"14.2.2 \u00a0 No Aftereffects","text":"

      No aftereffects is one of the important characteristics that enable dynamic programming to solve problems effectively. Its definition is: given a certain state, its future development is only related to the current state and has nothing to do with all past states.

      Taking the stair climbing problem as an example, given state \\(i\\), it will develop into states \\(i+1\\) and \\(i+2\\), corresponding to jumping \\(1\\) step and jumping \\(2\\) steps, respectively. When making these two choices, we do not need to consider the states before state \\(i\\), as they have no effect on the future of state \\(i\\).

      However, if we add a constraint to the stair climbing problem, the situation changes.

      Climbing stairs with constraint

      Given a staircase with \\(n\\) steps, where you can climb \\(1\\) or \\(2\\) steps at a time, but you cannot jump \\(1\\) step in two consecutive rounds. How many ways are there to climb to the top?

      As shown in Figure 14-8, there are only \\(2\\) feasible ways to climb to the \\(3\\)rd step. The way of jumping \\(1\\) step three consecutive times does not satisfy the constraint and is therefore discarded.

      Figure 14-8 \u00a0 Number of ways to climb to the 3rd step with constraint

      In this problem, if the previous round was a jump of \\(1\\) step, then the next round must jump \\(2\\) steps. This means that the next choice cannot be determined solely by the current state (current stair step number), but also depends on the previous state (the stair step number from the previous round).

      It is not difficult to see that this problem no longer satisfies no aftereffects, and the state transition equation \\(dp[i] = dp[i-1] + dp[i-2]\\) also fails, because \\(dp[i-1]\\) represents jumping \\(1\\) step in this round, but it includes many solutions where \"the previous round was a jump of \\(1\\) step\", which cannot be directly counted in \\(dp[i]\\) to satisfy the constraint.

      For this reason, we need to expand the state definition: state \\([i, j]\\) represents being on the \\(i\\)-th step with the previous round having jumped \\(j\\) steps, where \\(j \\in \\{1, 2\\}\\). This state definition effectively distinguishes whether the previous round was a jump of \\(1\\) step or \\(2\\) steps, allowing us to determine where the current state came from.

      • When the previous round jumped \\(1\\) step, the round before that could only choose to jump \\(2\\) steps, i.e., \\(dp[i, 1]\\) can only be transferred from \\(dp[i-1, 2]\\).
      • When the previous round jumped \\(2\\) steps, the round before that could choose to jump \\(1\\) step or \\(2\\) steps, i.e., \\(dp[i, 2]\\) can be transferred from \\(dp[i-2, 1]\\) or \\(dp[i-2, 2]\\).

      As shown in Figure 14-9, under this definition, \\(dp[i, j]\\) represents the number of ways for state \\([i, j]\\). The state transition equation is then:

      \\[ \\begin{cases} dp[i, 1] = dp[i-1, 2] \\\\ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2] \\end{cases} \\]

      Figure 14-9 \u00a0 Recurrence relation considering constraints

      Finally, return \\(dp[n, 1] + dp[n, 2]\\), where the sum of the two represents the total number of ways to climb to the \\(n\\)-th step:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_constraint_dp.py
      def climbing_stairs_constraint_dp(n: int) -> int:\n    \"\"\"Climbing stairs with constraint: Dynamic programming\"\"\"\n    if n == 1 or n == 2:\n        return 1\n    # Initialize dp table, used to store solutions to subproblems\n    dp = [[0] * 3 for _ in range(n + 1)]\n    # Initial state: preset the solution to the smallest subproblem\n    dp[1][1], dp[1][2] = 1, 0\n    dp[2][1], dp[2][2] = 0, 1\n    # State transition: gradually solve larger subproblems from smaller ones\n    for i in range(3, n + 1):\n        dp[i][1] = dp[i - 1][2]\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n    return dp[n][1] + dp[n][2]\n
      climbing_stairs_constraint_dp.cpp
      /* Climbing stairs with constraint: Dynamic programming */\nint climbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    vector<vector<int>> dp(n + 1, vector<int>(3, 0));\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
      climbing_stairs_constraint_dp.java
      /* Climbing stairs with constraint: Dynamic programming */\nint climbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    int[][] dp = new int[n + 1][3];\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
      climbing_stairs_constraint_dp.cs
      /* Climbing stairs with constraint: Dynamic programming */\nint ClimbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    int[,] dp = new int[n + 1, 3];\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1, 1] = 1;\n    dp[1, 2] = 0;\n    dp[2, 1] = 0;\n    dp[2, 2] = 1;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i, 1] = dp[i - 1, 2];\n        dp[i, 2] = dp[i - 2, 1] + dp[i - 2, 2];\n    }\n    return dp[n, 1] + dp[n, 2];\n}\n
      climbing_stairs_constraint_dp.go
      /* Climbing stairs with constraint: Dynamic programming */\nfunc climbingStairsConstraintDP(n int) int {\n    if n == 1 || n == 2 {\n        return 1\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    dp := make([][3]int, n+1)\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1][1] = 1\n    dp[1][2] = 0\n    dp[2][1] = 0\n    dp[2][2] = 1\n    // State transition: gradually solve larger subproblems from smaller ones\n    for i := 3; i <= n; i++ {\n        dp[i][1] = dp[i-1][2]\n        dp[i][2] = dp[i-2][1] + dp[i-2][2]\n    }\n    return dp[n][1] + dp[n][2]\n}\n
      climbing_stairs_constraint_dp.swift
      /* Climbing stairs with constraint: Dynamic programming */\nfunc climbingStairsConstraintDP(n: Int) -> Int {\n    if n == 1 || n == 2 {\n        return 1\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    var dp = Array(repeating: Array(repeating: 0, count: 3), count: n + 1)\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1][1] = 1\n    dp[1][2] = 0\n    dp[2][1] = 0\n    dp[2][2] = 1\n    // State transition: gradually solve larger subproblems from smaller ones\n    for i in 3 ... n {\n        dp[i][1] = dp[i - 1][2]\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n    }\n    return dp[n][1] + dp[n][2]\n}\n
      climbing_stairs_constraint_dp.js
      /* Climbing stairs with constraint: Dynamic programming */\nfunction climbingStairsConstraintDP(n) {\n    if (n === 1 || n === 2) {\n        return 1;\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    const dp = Array.from(new Array(n + 1), () => new Array(3));\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (let i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
      climbing_stairs_constraint_dp.ts
      /* Climbing stairs with constraint: Dynamic programming */\nfunction climbingStairsConstraintDP(n: number): number {\n    if (n === 1 || n === 2) {\n        return 1;\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    const dp = Array.from({ length: n + 1 }, () => new Array(3));\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (let i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
      climbing_stairs_constraint_dp.dart
      /* Climbing stairs with constraint: Dynamic programming */\nint climbingStairsConstraintDP(int n) {\n  if (n == 1 || n == 2) {\n    return 1;\n  }\n  // Initialize dp table, used to store solutions to subproblems\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(3, 0));\n  // Initial state: preset the solution to the smallest subproblem\n  dp[1][1] = 1;\n  dp[1][2] = 0;\n  dp[2][1] = 0;\n  dp[2][2] = 1;\n  // State transition: gradually solve larger subproblems from smaller ones\n  for (int i = 3; i <= n; i++) {\n    dp[i][1] = dp[i - 1][2];\n    dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n  }\n  return dp[n][1] + dp[n][2];\n}\n
      climbing_stairs_constraint_dp.rs
      /* Climbing stairs with constraint: Dynamic programming */\nfn climbing_stairs_constraint_dp(n: usize) -> i32 {\n    if n == 1 || n == 2 {\n        return 1;\n    };\n    // Initialize dp table, used to store solutions to subproblems\n    let mut dp = vec![vec![-1; 3]; n + 1];\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for i in 3..=n {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    dp[n][1] + dp[n][2]\n}\n
      climbing_stairs_constraint_dp.c
      /* Climbing stairs with constraint: Dynamic programming */\nint climbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(3, sizeof(int));\n    }\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    int res = dp[n][1] + dp[n][2];\n    // Free memory\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    free(dp);\n    return res;\n}\n
      climbing_stairs_constraint_dp.kt
      /* Climbing stairs with constraint: Dynamic programming */\nfun climbingStairsConstraintDP(n: Int): Int {\n    if (n == 1 || n == 2) {\n        return 1\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    val dp = Array(n + 1) { IntArray(3) }\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1][1] = 1\n    dp[1][2] = 0\n    dp[2][1] = 0\n    dp[2][2] = 1\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (i in 3..n) {\n        dp[i][1] = dp[i - 1][2]\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n    }\n    return dp[n][1] + dp[n][2]\n}\n
      climbing_stairs_constraint_dp.rb
      ### Climbing stairs with constraint: DP ###\ndef climbing_stairs_constraint_dp(n)\n  return 1 if n == 1 || n == 2\n\n  # Initialize dp table, used to store solutions to subproblems\n  dp = Array.new(n + 1) { Array.new(3, 0) }\n  # Initial state: preset the solution to the smallest subproblem\n  dp[1][1], dp[1][2] = 1, 0\n  dp[2][1], dp[2][2] = 0, 1\n  # State transition: gradually solve larger subproblems from smaller ones\n  for i in 3...(n + 1)\n    dp[i][1] = dp[i - 1][2]\n    dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n  end\n\n  dp[n][1] + dp[n][2]\nend\n

      In the above case, since we only need to consider one more preceding state, we can still make the problem satisfy no aftereffects by expanding the state definition. However, some problems have very severe \"aftereffects\".

      Climbing stairs with obstacle generation

      Given a staircase with \\(n\\) steps, where you can climb \\(1\\) or \\(2\\) steps at a time. It is stipulated that when climbing to the \\(i\\)-th step, the system will automatically place an obstacle on the \\(2i\\)-th step, and thereafter no round is allowed to jump to the \\(2i\\)-th step. For example, if the first two rounds jump to the \\(2\\)nd and \\(3\\)rd steps, then afterwards you cannot jump to the \\(4\\)th and \\(6\\)th steps. How many ways are there to climb to the top?

      In this problem, the next jump depends on all past states, because each jump places obstacles on higher steps, affecting future jumps. For such problems, dynamic programming is often difficult to solve.

      In fact, many complex combinatorial optimization problems (such as the traveling salesman problem) do not satisfy no aftereffects. For such problems, we usually choose to use other methods, such as heuristic search, genetic algorithms, reinforcement learning, etc., to obtain usable local optimal solutions within a limited time.

      "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/","title":"14.3 \u00a0 Dynamic Programming Problem-Solving Approach","text":"

      The previous two sections introduced the main characteristics of dynamic programming problems. Next, let us explore two more practical issues together.

      1. How to determine whether a problem is a dynamic programming problem?
      2. What is the complete process for solving a dynamic programming problem, and where should we start?
      "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1431-problem-determination","title":"14.3.1 \u00a0 Problem Determination","text":"

      Generally speaking, if a problem contains overlapping subproblems, optimal substructure, and satisfies no aftereffects, then it is usually suitable for solving with dynamic programming. However, it is difficult to directly extract these characteristics from the problem description. Therefore, we usually relax the conditions and first observe whether the problem is suitable for solving with backtracking (exhaustive search).

      Problems suitable for solving with backtracking usually satisfy the \"decision tree model\", which means the problem can be described using a tree structure, where each node represents a decision and each path represents a sequence of decisions.

      In other words, if a problem contains an explicit concept of decisions, and the solution is generated through a series of decisions, then it satisfies the decision tree model and can usually be solved using backtracking.

      On this basis, dynamic programming problems also have some \"bonus points\" for determination.

      • The problem contains descriptions such as maximum (minimum) or most (least), indicating optimization.
      • The problem's state can be represented using a list, multi-dimensional matrix, or tree, and a state has a recurrence relation with its surrounding states.

      Correspondingly, there are also some \"penalty points\".

      • The goal of the problem is to find all possible solutions, rather than finding the optimal solution.
      • The problem description has obvious permutation and combination characteristics, requiring the return of specific multiple solutions.

      If a problem satisfies the decision tree model and has relatively obvious \"bonus points\", we can assume it is a dynamic programming problem and verify it during the solving process.

      "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1432-problem-solving-steps","title":"14.3.2 \u00a0 Problem-Solving Steps","text":"

      The problem-solving process for dynamic programming varies depending on the nature and difficulty of the problem, but generally follows these steps: describe decisions, define states, establish the \\(dp\\) table, derive state transition equations, determine boundary conditions, etc.

      To illustrate the problem-solving steps more vividly, we use a classic problem \"minimum path sum\" as an example.

      Question

      Given an \\(n \\times m\\) two-dimensional grid grid, where each cell in the grid contains a non-negative integer representing the cost of that cell. A robot starts from the top-left cell and can only move down or right at each step until reaching the bottom-right cell. Return the minimum path sum from the top-left to the bottom-right.

      Figure 14-10 shows an example where the minimum path sum for the given grid is \\(13\\).

      Figure 14-10 \u00a0 Minimum path sum example data

      Step 1: Think about the decisions in each round, define the state, and thus obtain the \\(dp\\) table

      The decision in each round of this problem is to move one step down or right from the current cell. Let the row and column indices of the current cell be \\([i, j]\\). After moving down or right, the indices become \\([i+1, j]\\) or \\([i, j+1]\\). Therefore, the state should include two variables, the row index and column index, denoted as \\([i, j]\\).

      State \\([i, j]\\) corresponds to the subproblem: the minimum path sum from the starting point \\([0, 0]\\) to \\([i, j]\\), denoted as \\(dp[i, j]\\).

      From this, we obtain the two-dimensional \\(dp\\) matrix shown in Figure 14-11, whose size is the same as the input grid \\(grid\\).

      Figure 14-11 \u00a0 State definition and dp table

      Note

      The dynamic programming and backtracking processes can be described as a sequence of decisions, and the state consists of all decision variables. It should contain all variables describing the progress of problem-solving, and should contain sufficient information to derive the next state.

      Each state corresponds to a subproblem, and we define a \\(dp\\) table to store the solutions to all subproblems. Each independent variable of the state is a dimension of the \\(dp\\) table. Essentially, the \\(dp\\) table is a mapping between states and solutions to subproblems.

      Step 2: Identify the optimal substructure, and then derive the state transition equation

      For state \\([i, j]\\), it can only be transferred from the cell above \\([i-1, j]\\) or the cell to the left \\([i, j-1]\\). Therefore, the optimal substructure is: the minimum path sum to reach \\([i, j]\\) is determined by the smaller of the minimum path sums of \\([i, j-1]\\) and \\([i-1, j]\\).

      Based on the above analysis, the state transition equation shown in Figure 14-12 can be derived:

      \\[ dp[i, j] = \\min(dp[i-1, j], dp[i, j-1]) + grid[i, j] \\]

      Figure 14-12 \u00a0 Optimal substructure and state transition equation

      Note

      Based on the defined \\(dp\\) table, think about the relationship between the original problem and subproblems, and find the method to construct the optimal solution to the original problem from the optimal solutions to the subproblems, which is the optimal substructure.

      Once we identify the optimal substructure, we can use it to construct the state transition equation.

      Step 3: Determine boundary conditions and state transition order

      In this problem, states in the first row can only come from the state to their left, and states in the first column can only come from the state above them. Therefore, the first row \\(i = 0\\) and first column \\(j = 0\\) are boundary conditions.

      As shown in Figure 14-13, since each cell is transferred from the cell to its left and the cell above it, we use loops to traverse the matrix, with the outer loop traversing rows and the inner loop traversing columns.

      Figure 14-13 \u00a0 Boundary conditions and state transition order

      Note

      Boundary conditions in dynamic programming are used to initialize the \\(dp\\) table, and in search are used for pruning.

      The core of state transition order is to ensure that when computing the solution to the current problem, all the smaller subproblems it depends on have already been computed correctly.

      Based on the above analysis, we can directly write the dynamic programming code. However, subproblem decomposition is a top-down approach, so implementing in the order \"brute force search \\(\\rightarrow\\) memoization \\(\\rightarrow\\) dynamic programming\" is more aligned with thinking habits.

      "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1-method-1-brute-force-search","title":"1. \u00a0 Method 1: Brute Force Search","text":"

      Starting from state \\([i, j]\\), continuously decompose into smaller states \\([i-1, j]\\) and \\([i, j-1]\\). The recursive function includes the following elements.

      • Recursive parameters: state \\([i, j]\\).
      • Return value: minimum path sum from \\([0, 0]\\) to \\([i, j]\\), which is \\(dp[i, j]\\).
      • Termination condition: when \\(i = 0\\) and \\(j = 0\\), return cost \\(grid[0, 0]\\).
      • Pruning: when \\(i < 0\\) or \\(j < 0\\), the index is out of bounds, return cost \\(+\\infty\\), representing infeasibility.

      The implementation code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
      def min_path_sum_dfs(grid: list[list[int]], i: int, j: int) -> int:\n    \"\"\"Minimum path sum: Brute-force search\"\"\"\n    # If it's the top-left cell, terminate the search\n    if i == 0 and j == 0:\n        return grid[0][0]\n    # If row or column index is out of bounds, return +\u221e cost\n    if i < 0 or j < 0:\n        return inf\n    # Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n    up = min_path_sum_dfs(grid, i - 1, j)\n    left = min_path_sum_dfs(grid, i, j - 1)\n    # Return the minimum path cost from top-left to (i, j)\n    return min(left, up) + grid[i][j]\n
      min_path_sum.cpp
      /* Minimum path sum: Brute-force search */\nint minPathSumDFS(vector<vector<int>> &grid, int i, int j) {\n    // If it's the top-left cell, terminate the search\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return INT_MAX;\n    }\n    // Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n    int up = minPathSumDFS(grid, i - 1, j);\n    int left = minPathSumDFS(grid, i, j - 1);\n    // Return the minimum path cost from top-left to (i, j)\n    return min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\n}\n
      min_path_sum.java
      /* Minimum path sum: Brute-force search */\nint minPathSumDFS(int[][] grid, int i, int j) {\n    // If it's the top-left cell, terminate the search\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return Integer.MAX_VALUE;\n    }\n    // Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n    int up = minPathSumDFS(grid, i - 1, j);\n    int left = minPathSumDFS(grid, i, j - 1);\n    // Return the minimum path cost from top-left to (i, j)\n    return Math.min(left, up) + grid[i][j];\n}\n
      min_path_sum.cs
      /* Minimum path sum: Brute-force search */\nint MinPathSumDFS(int[][] grid, int i, int j) {\n    // If it's the top-left cell, terminate the search\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return int.MaxValue;\n    }\n    // Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n    int up = MinPathSumDFS(grid, i - 1, j);\n    int left = MinPathSumDFS(grid, i, j - 1);\n    // Return the minimum path cost from top-left to (i, j)\n    return Math.Min(left, up) + grid[i][j];\n}\n
      min_path_sum.go
      /* Minimum path sum: Brute-force search */\nfunc minPathSumDFS(grid [][]int, i, j int) int {\n    // If it's the top-left cell, terminate the search\n    if i == 0 && j == 0 {\n        return grid[0][0]\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if i < 0 || j < 0 {\n        return math.MaxInt\n    }\n    // Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n    up := minPathSumDFS(grid, i-1, j)\n    left := minPathSumDFS(grid, i, j-1)\n    // Return the minimum path cost from top-left to (i, j)\n    return int(math.Min(float64(left), float64(up))) + grid[i][j]\n}\n
      min_path_sum.swift
      /* Minimum path sum: Brute-force search */\nfunc minPathSumDFS(grid: [[Int]], i: Int, j: Int) -> Int {\n    // If it's the top-left cell, terminate the search\n    if i == 0, j == 0 {\n        return grid[0][0]\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if i < 0 || j < 0 {\n        return .max\n    }\n    // Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n    let up = minPathSumDFS(grid: grid, i: i - 1, j: j)\n    let left = minPathSumDFS(grid: grid, i: i, j: j - 1)\n    // Return the minimum path cost from top-left to (i, j)\n    return min(left, up) + grid[i][j]\n}\n
      min_path_sum.js
      /* Minimum path sum: Brute-force search */\nfunction minPathSumDFS(grid, i, j) {\n    // If it's the top-left cell, terminate the search\n    if (i === 0 && j === 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return Infinity;\n    }\n    // Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n    const up = minPathSumDFS(grid, i - 1, j);\n    const left = minPathSumDFS(grid, i, j - 1);\n    // Return the minimum path cost from top-left to (i, j)\n    return Math.min(left, up) + grid[i][j];\n}\n
      min_path_sum.ts
      /* Minimum path sum: Brute-force search */\nfunction minPathSumDFS(\n    grid: Array<Array<number>>,\n    i: number,\n    j: number\n): number {\n    // If it's the top-left cell, terminate the search\n    if (i === 0 && j == 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return Infinity;\n    }\n    // Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n    const up = minPathSumDFS(grid, i - 1, j);\n    const left = minPathSumDFS(grid, i, j - 1);\n    // Return the minimum path cost from top-left to (i, j)\n    return Math.min(left, up) + grid[i][j];\n}\n
      min_path_sum.dart
      /* Minimum path sum: Brute-force search */\nint minPathSumDFS(List<List<int>> grid, int i, int j) {\n  // If it's the top-left cell, terminate the search\n  if (i == 0 && j == 0) {\n    return grid[0][0];\n  }\n  // If row or column index is out of bounds, return +\u221e cost\n  if (i < 0 || j < 0) {\n    // In Dart, int type is fixed-range integer, no value representing \"infinity\"\n    return BigInt.from(2).pow(31).toInt();\n  }\n  // Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n  int up = minPathSumDFS(grid, i - 1, j);\n  int left = minPathSumDFS(grid, i, j - 1);\n  // Return the minimum path cost from top-left to (i, j)\n  return min(left, up) + grid[i][j];\n}\n
      min_path_sum.rs
      /* Minimum path sum: Brute-force search */\nfn min_path_sum_dfs(grid: &Vec<Vec<i32>>, i: i32, j: i32) -> i32 {\n    // If it's the top-left cell, terminate the search\n    if i == 0 && j == 0 {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if i < 0 || j < 0 {\n        return i32::MAX;\n    }\n    // Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n    let up = min_path_sum_dfs(grid, i - 1, j);\n    let left = min_path_sum_dfs(grid, i, j - 1);\n    // Return the minimum path cost from top-left to (i, j)\n    std::cmp::min(left, up) + grid[i as usize][j as usize]\n}\n
      min_path_sum.c
      /* Minimum path sum: Brute-force search */\nint minPathSumDFS(int grid[MAX_SIZE][MAX_SIZE], int i, int j) {\n    // If it's the top-left cell, terminate the search\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return INT_MAX;\n    }\n    // Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n    int up = minPathSumDFS(grid, i - 1, j);\n    int left = minPathSumDFS(grid, i, j - 1);\n    // Return the minimum path cost from top-left to (i, j)\n    return myMin(left, up) != INT_MAX ? myMin(left, up) + grid[i][j] : INT_MAX;\n}\n
      min_path_sum.kt
      /* Minimum path sum: Brute-force search */\nfun minPathSumDFS(grid: Array<IntArray>, i: Int, j: Int): Int {\n    // If it's the top-left cell, terminate the search\n    if (i == 0 && j == 0) {\n        return grid[0][0]\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return Int.MAX_VALUE\n    }\n    // Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n    val up = minPathSumDFS(grid, i - 1, j)\n    val left = minPathSumDFS(grid, i, j - 1)\n    // Return the minimum path cost from top-left to (i, j)\n    return min(left, up) + grid[i][j]\n}\n
      min_path_sum.rb
      ### Minimum path sum: brute force search ###\ndef min_path_sum_dfs(grid, i, j)\n  # If it's the top-left cell, terminate the search\n  return grid[i][j] if i == 0 && j == 0\n  # If row or column index is out of bounds, return +\u221e cost\n  return Float::INFINITY if i < 0 || j < 0\n  # Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)\n  up = min_path_sum_dfs(grid, i - 1, j)\n  left = min_path_sum_dfs(grid, i, j - 1)\n  # Return the minimum path cost from top-left to (i, j)\n  [left, up].min + grid[i][j]\nend\n

      Figure 14-14 shows the recursion tree rooted at \\(dp[2, 1]\\), which includes some overlapping subproblems whose number will increase sharply as the size of grid grid grows.

      Essentially, the reason for overlapping subproblems is: there are multiple paths from the top-left corner to reach a certain cell.

      Figure 14-14 \u00a0 Brute force search recursion tree

      Each state has two choices, down and right, so the total number of steps from the top-left corner to the bottom-right corner is \\(m + n - 2\\), giving a worst-case time complexity of \\(O(2^{m + n})\\), where \\(n\\) and \\(m\\) are the number of rows and columns of the grid, respectively. Note that this calculation does not account for situations near the grid boundaries, where only one choice remains when reaching the grid boundary, so the actual number of paths will be somewhat less.

      "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#2-method-2-memoization","title":"2. \u00a0 Method 2: Memoization","text":"

      We introduce a memo list mem of the same size as grid grid to record the solutions to subproblems and prune overlapping subproblems:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
      def min_path_sum_dfs_mem(\n    grid: list[list[int]], mem: list[list[int]], i: int, j: int\n) -> int:\n    \"\"\"Minimum path sum: Memoization search\"\"\"\n    # If it's the top-left cell, terminate the search\n    if i == 0 and j == 0:\n        return grid[0][0]\n    # If row or column index is out of bounds, return +\u221e cost\n    if i < 0 or j < 0:\n        return inf\n    # If there's a record, return it directly\n    if mem[i][j] != -1:\n        return mem[i][j]\n    # Minimum path cost for left and upper cells\n    up = min_path_sum_dfs_mem(grid, mem, i - 1, j)\n    left = min_path_sum_dfs_mem(grid, mem, i, j - 1)\n    # Record and return the minimum path cost from top-left to (i, j)\n    mem[i][j] = min(left, up) + grid[i][j]\n    return mem[i][j]\n
      min_path_sum.cpp
      /* Minimum path sum: Memoization search */\nint minPathSumDFSMem(vector<vector<int>> &grid, vector<vector<int>> &mem, int i, int j) {\n    // If it's the top-left cell, terminate the search\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return INT_MAX;\n    }\n    // If there's a record, return it directly\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // Minimum path cost for left and upper cells\n    int up = minPathSumDFSMem(grid, mem, i - 1, j);\n    int left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // Record and return the minimum path cost from top-left to (i, j)\n    mem[i][j] = min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\n    return mem[i][j];\n}\n
      min_path_sum.java
      /* Minimum path sum: Memoization search */\nint minPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n    // If it's the top-left cell, terminate the search\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return Integer.MAX_VALUE;\n    }\n    // If there's a record, return it directly\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // Minimum path cost for left and upper cells\n    int up = minPathSumDFSMem(grid, mem, i - 1, j);\n    int left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // Record and return the minimum path cost from top-left to (i, j)\n    mem[i][j] = Math.min(left, up) + grid[i][j];\n    return mem[i][j];\n}\n
      min_path_sum.cs
      /* Minimum path sum: Memoization search */\nint MinPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n    // If it's the top-left cell, terminate the search\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return int.MaxValue;\n    }\n    // If there's a record, return it directly\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // Minimum path cost for left and upper cells\n    int up = MinPathSumDFSMem(grid, mem, i - 1, j);\n    int left = MinPathSumDFSMem(grid, mem, i, j - 1);\n    // Record and return the minimum path cost from top-left to (i, j)\n    mem[i][j] = Math.Min(left, up) + grid[i][j];\n    return mem[i][j];\n}\n
      min_path_sum.go
      /* Minimum path sum: Memoization search */\nfunc minPathSumDFSMem(grid, mem [][]int, i, j int) int {\n    // If it's the top-left cell, terminate the search\n    if i == 0 && j == 0 {\n        return grid[0][0]\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if i < 0 || j < 0 {\n        return math.MaxInt\n    }\n    // If there's a record, return it directly\n    if mem[i][j] != -1 {\n        return mem[i][j]\n    }\n    // Minimum path cost for left and upper cells\n    up := minPathSumDFSMem(grid, mem, i-1, j)\n    left := minPathSumDFSMem(grid, mem, i, j-1)\n    // Record and return the minimum path cost from top-left to (i, j)\n    mem[i][j] = int(math.Min(float64(left), float64(up))) + grid[i][j]\n    return mem[i][j]\n}\n
      min_path_sum.swift
      /* Minimum path sum: Memoization search */\nfunc minPathSumDFSMem(grid: [[Int]], mem: inout [[Int]], i: Int, j: Int) -> Int {\n    // If it's the top-left cell, terminate the search\n    if i == 0, j == 0 {\n        return grid[0][0]\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if i < 0 || j < 0 {\n        return .max\n    }\n    // If there's a record, return it directly\n    if mem[i][j] != -1 {\n        return mem[i][j]\n    }\n    // Minimum path cost for left and upper cells\n    let up = minPathSumDFSMem(grid: grid, mem: &mem, i: i - 1, j: j)\n    let left = minPathSumDFSMem(grid: grid, mem: &mem, i: i, j: j - 1)\n    // Record and return the minimum path cost from top-left to (i, j)\n    mem[i][j] = min(left, up) + grid[i][j]\n    return mem[i][j]\n}\n
      min_path_sum.js
      /* Minimum path sum: Memoization search */\nfunction minPathSumDFSMem(grid, mem, i, j) {\n    // If it's the top-left cell, terminate the search\n    if (i === 0 && j === 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return Infinity;\n    }\n    // If there's a record, return it directly\n    if (mem[i][j] !== -1) {\n        return mem[i][j];\n    }\n    // Minimum path cost for left and upper cells\n    const up = minPathSumDFSMem(grid, mem, i - 1, j);\n    const left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // Record and return the minimum path cost from top-left to (i, j)\n    mem[i][j] = Math.min(left, up) + grid[i][j];\n    return mem[i][j];\n}\n
      min_path_sum.ts
      /* Minimum path sum: Memoization search */\nfunction minPathSumDFSMem(\n    grid: Array<Array<number>>,\n    mem: Array<Array<number>>,\n    i: number,\n    j: number\n): number {\n    // If it's the top-left cell, terminate the search\n    if (i === 0 && j === 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return Infinity;\n    }\n    // If there's a record, return it directly\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // Minimum path cost for left and upper cells\n    const up = minPathSumDFSMem(grid, mem, i - 1, j);\n    const left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // Record and return the minimum path cost from top-left to (i, j)\n    mem[i][j] = Math.min(left, up) + grid[i][j];\n    return mem[i][j];\n}\n
      min_path_sum.dart
      /* Minimum path sum: Memoization search */\nint minPathSumDFSMem(List<List<int>> grid, List<List<int>> mem, int i, int j) {\n  // If it's the top-left cell, terminate the search\n  if (i == 0 && j == 0) {\n    return grid[0][0];\n  }\n  // If row or column index is out of bounds, return +\u221e cost\n  if (i < 0 || j < 0) {\n    // In Dart, int type is fixed-range integer, no value representing \"infinity\"\n    return BigInt.from(2).pow(31).toInt();\n  }\n  // If there's a record, return it directly\n  if (mem[i][j] != -1) {\n    return mem[i][j];\n  }\n  // Minimum path cost for left and upper cells\n  int up = minPathSumDFSMem(grid, mem, i - 1, j);\n  int left = minPathSumDFSMem(grid, mem, i, j - 1);\n  // Record and return the minimum path cost from top-left to (i, j)\n  mem[i][j] = min(left, up) + grid[i][j];\n  return mem[i][j];\n}\n
      min_path_sum.rs
      /* Minimum path sum: Memoization search */\nfn min_path_sum_dfs_mem(grid: &Vec<Vec<i32>>, mem: &mut Vec<Vec<i32>>, i: i32, j: i32) -> i32 {\n    // If it's the top-left cell, terminate the search\n    if i == 0 && j == 0 {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if i < 0 || j < 0 {\n        return i32::MAX;\n    }\n    // If there's a record, return it directly\n    if mem[i as usize][j as usize] != -1 {\n        return mem[i as usize][j as usize];\n    }\n    // Minimum path cost for left and upper cells\n    let up = min_path_sum_dfs_mem(grid, mem, i - 1, j);\n    let left = min_path_sum_dfs_mem(grid, mem, i, j - 1);\n    // Record and return the minimum path cost from top-left to (i, j)\n    mem[i as usize][j as usize] = std::cmp::min(left, up) + grid[i as usize][j as usize];\n    mem[i as usize][j as usize]\n}\n
      min_path_sum.c
      /* Minimum path sum: Memoization search */\nint minPathSumDFSMem(int grid[MAX_SIZE][MAX_SIZE], int mem[MAX_SIZE][MAX_SIZE], int i, int j) {\n    // If it's the top-left cell, terminate the search\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return INT_MAX;\n    }\n    // If there's a record, return it directly\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // Minimum path cost for left and upper cells\n    int up = minPathSumDFSMem(grid, mem, i - 1, j);\n    int left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // Record and return the minimum path cost from top-left to (i, j)\n    mem[i][j] = myMin(left, up) != INT_MAX ? myMin(left, up) + grid[i][j] : INT_MAX;\n    return mem[i][j];\n}\n
      min_path_sum.kt
      /* Minimum path sum: Memoization search */\nfun minPathSumDFSMem(\n    grid: Array<IntArray>,\n    mem: Array<IntArray>,\n    i: Int,\n    j: Int\n): Int {\n    // If it's the top-left cell, terminate the search\n    if (i == 0 && j == 0) {\n        return grid[0][0]\n    }\n    // If row or column index is out of bounds, return +\u221e cost\n    if (i < 0 || j < 0) {\n        return Int.MAX_VALUE\n    }\n    // If there's a record, return it directly\n    if (mem[i][j] != -1) {\n        return mem[i][j]\n    }\n    // Minimum path cost for left and upper cells\n    val up = minPathSumDFSMem(grid, mem, i - 1, j)\n    val left = minPathSumDFSMem(grid, mem, i, j - 1)\n    // Record and return the minimum path cost from top-left to (i, j)\n    mem[i][j] = min(left, up) + grid[i][j]\n    return mem[i][j]\n}\n
      min_path_sum.rb
      ### Minimum path sum: memoization search ###\ndef min_path_sum_dfs_mem(grid, mem, i, j)\n  # If it's the top-left cell, terminate the search\n  return grid[0][0] if i == 0 && j == 0\n  # If row or column index is out of bounds, return +\u221e cost\n  return Float::INFINITY if i < 0 || j < 0\n  # If there's a record, return it directly\n  return mem[i][j] if mem[i][j] != -1\n  # Minimum path cost for left and upper cells\n  up = min_path_sum_dfs_mem(grid, mem, i - 1, j)\n  left = min_path_sum_dfs_mem(grid, mem, i, j - 1)\n  # Record and return the minimum path cost from top-left to (i, j)\n  mem[i][j] = [left, up].min + grid[i][j]\nend\n

      As shown in Figure 14-15, after introducing memoization, all subproblem solutions only need to be computed once, so the time complexity depends on the total number of states, which is the grid size \\(O(nm)\\).

      Figure 14-15 \u00a0 Memoization recursion tree

      "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#3-method-3-dynamic-programming","title":"3. \u00a0 Method 3: Dynamic Programming","text":"

      Implement the dynamic programming solution based on iteration, as shown in the code below:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
      def min_path_sum_dp(grid: list[list[int]]) -> int:\n    \"\"\"Minimum path sum: Dynamic programming\"\"\"\n    n, m = len(grid), len(grid[0])\n    # Initialize dp table\n    dp = [[0] * m for _ in range(n)]\n    dp[0][0] = grid[0][0]\n    # State transition: first row\n    for j in range(1, m):\n        dp[0][j] = dp[0][j - 1] + grid[0][j]\n    # State transition: first column\n    for i in range(1, n):\n        dp[i][0] = dp[i - 1][0] + grid[i][0]\n    # State transition: rest of the rows and columns\n    for i in range(1, n):\n        for j in range(1, m):\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n    return dp[n - 1][m - 1]\n
      min_path_sum.cpp
      /* Minimum path sum: Dynamic programming */\nint minPathSumDP(vector<vector<int>> &grid) {\n    int n = grid.size(), m = grid[0].size();\n    // Initialize dp table\n    vector<vector<int>> dp(n, vector<int>(m));\n    dp[0][0] = grid[0][0];\n    // State transition: first row\n    for (int j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // State transition: first column\n    for (int i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
      min_path_sum.java
      /* Minimum path sum: Dynamic programming */\nint minPathSumDP(int[][] grid) {\n    int n = grid.length, m = grid[0].length;\n    // Initialize dp table\n    int[][] dp = new int[n][m];\n    dp[0][0] = grid[0][0];\n    // State transition: first row\n    for (int j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // State transition: first column\n    for (int i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
      min_path_sum.cs
      /* Minimum path sum: Dynamic programming */\nint MinPathSumDP(int[][] grid) {\n    int n = grid.Length, m = grid[0].Length;\n    // Initialize dp table\n    int[,] dp = new int[n, m];\n    dp[0, 0] = grid[0][0];\n    // State transition: first row\n    for (int j = 1; j < m; j++) {\n        dp[0, j] = dp[0, j - 1] + grid[0][j];\n    }\n    // State transition: first column\n    for (int i = 1; i < n; i++) {\n        dp[i, 0] = dp[i - 1, 0] + grid[i][0];\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i, j] = Math.Min(dp[i, j - 1], dp[i - 1, j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1, m - 1];\n}\n
      min_path_sum.go
      /* Minimum path sum: Dynamic programming */\nfunc minPathSumDP(grid [][]int) int {\n    n, m := len(grid), len(grid[0])\n    // Initialize dp table\n    dp := make([][]int, n)\n    for i := 0; i < n; i++ {\n        dp[i] = make([]int, m)\n    }\n    dp[0][0] = grid[0][0]\n    // State transition: first row\n    for j := 1; j < m; j++ {\n        dp[0][j] = dp[0][j-1] + grid[0][j]\n    }\n    // State transition: first column\n    for i := 1; i < n; i++ {\n        dp[i][0] = dp[i-1][0] + grid[i][0]\n    }\n    // State transition: rest of the rows and columns\n    for i := 1; i < n; i++ {\n        for j := 1; j < m; j++ {\n            dp[i][j] = int(math.Min(float64(dp[i][j-1]), float64(dp[i-1][j]))) + grid[i][j]\n        }\n    }\n    return dp[n-1][m-1]\n}\n
      min_path_sum.swift
      /* Minimum path sum: Dynamic programming */\nfunc minPathSumDP(grid: [[Int]]) -> Int {\n    let n = grid.count\n    let m = grid[0].count\n    // Initialize dp table\n    var dp = Array(repeating: Array(repeating: 0, count: m), count: n)\n    dp[0][0] = grid[0][0]\n    // State transition: first row\n    for j in 1 ..< m {\n        dp[0][j] = dp[0][j - 1] + grid[0][j]\n    }\n    // State transition: first column\n    for i in 1 ..< n {\n        dp[i][0] = dp[i - 1][0] + grid[i][0]\n    }\n    // State transition: rest of the rows and columns\n    for i in 1 ..< n {\n        for j in 1 ..< m {\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n        }\n    }\n    return dp[n - 1][m - 1]\n}\n
      min_path_sum.js
      /* Minimum path sum: Dynamic programming */\nfunction minPathSumDP(grid) {\n    const n = grid.length,\n        m = grid[0].length;\n    // Initialize dp table\n    const dp = Array.from({ length: n }, () =>\n        Array.from({ length: m }, () => 0)\n    );\n    dp[0][0] = grid[0][0];\n    // State transition: first row\n    for (let j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // State transition: first column\n    for (let i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // State transition: rest of the rows and columns\n    for (let i = 1; i < n; i++) {\n        for (let j = 1; j < m; j++) {\n            dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
      min_path_sum.ts
      /* Minimum path sum: Dynamic programming */\nfunction minPathSumDP(grid: Array<Array<number>>): number {\n    const n = grid.length,\n        m = grid[0].length;\n    // Initialize dp table\n    const dp = Array.from({ length: n }, () =>\n        Array.from({ length: m }, () => 0)\n    );\n    dp[0][0] = grid[0][0];\n    // State transition: first row\n    for (let j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // State transition: first column\n    for (let i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // State transition: rest of the rows and columns\n    for (let i = 1; i < n; i++) {\n        for (let j: number = 1; j < m; j++) {\n            dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
      min_path_sum.dart
      /* Minimum path sum: Dynamic programming */\nint minPathSumDP(List<List<int>> grid) {\n  int n = grid.length, m = grid[0].length;\n  // Initialize dp table\n  List<List<int>> dp = List.generate(n, (i) => List.filled(m, 0));\n  dp[0][0] = grid[0][0];\n  // State transition: first row\n  for (int j = 1; j < m; j++) {\n    dp[0][j] = dp[0][j - 1] + grid[0][j];\n  }\n  // State transition: first column\n  for (int i = 1; i < n; i++) {\n    dp[i][0] = dp[i - 1][0] + grid[i][0];\n  }\n  // State transition: rest of the rows and columns\n  for (int i = 1; i < n; i++) {\n    for (int j = 1; j < m; j++) {\n      dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n    }\n  }\n  return dp[n - 1][m - 1];\n}\n
      min_path_sum.rs
      /* Minimum path sum: Dynamic programming */\nfn min_path_sum_dp(grid: &Vec<Vec<i32>>) -> i32 {\n    let (n, m) = (grid.len(), grid[0].len());\n    // Initialize dp table\n    let mut dp = vec![vec![0; m]; n];\n    dp[0][0] = grid[0][0];\n    // State transition: first row\n    for j in 1..m {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // State transition: first column\n    for i in 1..n {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // State transition: rest of the rows and columns\n    for i in 1..n {\n        for j in 1..m {\n            dp[i][j] = std::cmp::min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    dp[n - 1][m - 1]\n}\n
      min_path_sum.c
      /* Minimum path sum: Dynamic programming */\nint minPathSumDP(int grid[MAX_SIZE][MAX_SIZE], int n, int m) {\n    // Initialize dp table\n    int **dp = malloc(n * sizeof(int *));\n    for (int i = 0; i < n; i++) {\n        dp[i] = calloc(m, sizeof(int));\n    }\n    dp[0][0] = grid[0][0];\n    // State transition: first row\n    for (int j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // State transition: first column\n    for (int i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i][j] = myMin(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    int res = dp[n - 1][m - 1];\n    // Free memory\n    for (int i = 0; i < n; i++) {\n        free(dp[i]);\n    }\n    return res;\n}\n
      min_path_sum.kt
      /* Minimum path sum: Dynamic programming */\nfun minPathSumDP(grid: Array<IntArray>): Int {\n    val n = grid.size\n    val m = grid[0].size\n    // Initialize dp table\n    val dp = Array(n) { IntArray(m) }\n    dp[0][0] = grid[0][0]\n    // State transition: first row\n    for (j in 1..<m) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j]\n    }\n    // State transition: first column\n    for (i in 1..<n) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0]\n    }\n    // State transition: rest of the rows and columns\n    for (i in 1..<n) {\n        for (j in 1..<m) {\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n        }\n    }\n    return dp[n - 1][m - 1]\n}\n
      min_path_sum.rb
      ### Minimum path sum: dynamic programming ###\ndef min_path_sum_dp(grid)\n  n, m = grid.length, grid.first.length\n  # Initialize dp table\n  dp = Array.new(n) { Array.new(m, 0) }\n  dp[0][0] = grid[0][0]\n  # State transition: first row\n  (1...m).each { |j| dp[0][j] = dp[0][j - 1] + grid[0][j] }\n  # State transition: first column\n  (1...n).each { |i| dp[i][0] = dp[i - 1][0] + grid[i][0] }\n  # State transition: rest of the rows and columns\n  for i in 1...n\n    for j in 1...m\n      dp[i][j] = [dp[i][j - 1], dp[i - 1][j]].min + grid[i][j]\n    end\n  end\n  dp[n -1][m -1]\nend\n

      Figure 14-16 shows the state transition process for minimum path sum, which traverses the entire grid, thus the time complexity is \\(O(nm)\\).

      The array dp has size \\(n \\times m\\), thus the space complexity is \\(O(nm)\\).

      <1><2><3><4><5><6><7><8><9><10><11><12>

      Figure 14-16 \u00a0 Dynamic programming process for minimum path sum

      "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#4-space-optimization","title":"4. \u00a0 Space Optimization","text":"

      Since each cell is only related to the cell to its left and the cell above it, we can use a single-row array to implement the \\(dp\\) table.

      Note that since the array dp can only represent the state of one row, we cannot initialize the first column state in advance, but rather update it when traversing each row:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
      def min_path_sum_dp_comp(grid: list[list[int]]) -> int:\n    \"\"\"Minimum path sum: Space-optimized dynamic programming\"\"\"\n    n, m = len(grid), len(grid[0])\n    # Initialize dp table\n    dp = [0] * m\n    # State transition: first row\n    dp[0] = grid[0][0]\n    for j in range(1, m):\n        dp[j] = dp[j - 1] + grid[0][j]\n    # State transition: rest of the rows\n    for i in range(1, n):\n        # State transition: first column\n        dp[0] = dp[0] + grid[i][0]\n        # State transition: rest of the columns\n        for j in range(1, m):\n            dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n    return dp[m - 1]\n
      min_path_sum.cpp
      /* Minimum path sum: Space-optimized dynamic programming */\nint minPathSumDPComp(vector<vector<int>> &grid) {\n    int n = grid.size(), m = grid[0].size();\n    // Initialize dp table\n    vector<int> dp(m);\n    // State transition: first row\n    dp[0] = grid[0][0];\n    for (int j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // State transition: rest of the rows\n    for (int i = 1; i < n; i++) {\n        // State transition: first column\n        dp[0] = dp[0] + grid[i][0];\n        // State transition: rest of the columns\n        for (int j = 1; j < m; j++) {\n            dp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
      min_path_sum.java
      /* Minimum path sum: Space-optimized dynamic programming */\nint minPathSumDPComp(int[][] grid) {\n    int n = grid.length, m = grid[0].length;\n    // Initialize dp table\n    int[] dp = new int[m];\n    // State transition: first row\n    dp[0] = grid[0][0];\n    for (int j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // State transition: rest of the rows\n    for (int i = 1; i < n; i++) {\n        // State transition: first column\n        dp[0] = dp[0] + grid[i][0];\n        // State transition: rest of the columns\n        for (int j = 1; j < m; j++) {\n            dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
      min_path_sum.cs
      /* Minimum path sum: Space-optimized dynamic programming */\nint MinPathSumDPComp(int[][] grid) {\n    int n = grid.Length, m = grid[0].Length;\n    // Initialize dp table\n    int[] dp = new int[m];\n    dp[0] = grid[0][0];\n    // State transition: first row\n    for (int j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // State transition: rest of the rows\n    for (int i = 1; i < n; i++) {\n        // State transition: first column\n        dp[0] = dp[0] + grid[i][0];\n        // State transition: rest of the columns\n        for (int j = 1; j < m; j++) {\n            dp[j] = Math.Min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
      min_path_sum.go
      /* Minimum path sum: Space-optimized dynamic programming */\nfunc minPathSumDPComp(grid [][]int) int {\n    n, m := len(grid), len(grid[0])\n    // Initialize dp table\n    dp := make([]int, m)\n    // State transition: first row\n    dp[0] = grid[0][0]\n    for j := 1; j < m; j++ {\n        dp[j] = dp[j-1] + grid[0][j]\n    }\n    // State transition: rest of the rows and columns\n    for i := 1; i < n; i++ {\n        // State transition: first column\n        dp[0] = dp[0] + grid[i][0]\n        // State transition: rest of the columns\n        for j := 1; j < m; j++ {\n            dp[j] = int(math.Min(float64(dp[j-1]), float64(dp[j]))) + grid[i][j]\n        }\n    }\n    return dp[m-1]\n}\n
      min_path_sum.swift
      /* Minimum path sum: Space-optimized dynamic programming */\nfunc minPathSumDPComp(grid: [[Int]]) -> Int {\n    let n = grid.count\n    let m = grid[0].count\n    // Initialize dp table\n    var dp = Array(repeating: 0, count: m)\n    // State transition: first row\n    dp[0] = grid[0][0]\n    for j in 1 ..< m {\n        dp[j] = dp[j - 1] + grid[0][j]\n    }\n    // State transition: rest of the rows\n    for i in 1 ..< n {\n        // State transition: first column\n        dp[0] = dp[0] + grid[i][0]\n        // State transition: rest of the columns\n        for j in 1 ..< m {\n            dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n        }\n    }\n    return dp[m - 1]\n}\n
      min_path_sum.js
      /* Minimum path sum: Space-optimized dynamic programming */\nfunction minPathSumDPComp(grid) {\n    const n = grid.length,\n        m = grid[0].length;\n    // Initialize dp table\n    const dp = new Array(m);\n    // State transition: first row\n    dp[0] = grid[0][0];\n    for (let j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // State transition: rest of the rows\n    for (let i = 1; i < n; i++) {\n        // State transition: first column\n        dp[0] = dp[0] + grid[i][0];\n        // State transition: rest of the columns\n        for (let j = 1; j < m; j++) {\n            dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
      min_path_sum.ts
      /* Minimum path sum: Space-optimized dynamic programming */\nfunction minPathSumDPComp(grid: Array<Array<number>>): number {\n    const n = grid.length,\n        m = grid[0].length;\n    // Initialize dp table\n    const dp = new Array(m);\n    // State transition: first row\n    dp[0] = grid[0][0];\n    for (let j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // State transition: rest of the rows\n    for (let i = 1; i < n; i++) {\n        // State transition: first column\n        dp[0] = dp[0] + grid[i][0];\n        // State transition: rest of the columns\n        for (let j = 1; j < m; j++) {\n            dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
      min_path_sum.dart
      /* Minimum path sum: Space-optimized dynamic programming */\nint minPathSumDPComp(List<List<int>> grid) {\n  int n = grid.length, m = grid[0].length;\n  // Initialize dp table\n  List<int> dp = List.filled(m, 0);\n  dp[0] = grid[0][0];\n  for (int j = 1; j < m; j++) {\n    dp[j] = dp[j - 1] + grid[0][j];\n  }\n  // State transition: rest of the rows\n  for (int i = 1; i < n; i++) {\n    // State transition: first column\n    dp[0] = dp[0] + grid[i][0];\n    // State transition: rest of the columns\n    for (int j = 1; j < m; j++) {\n      dp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n    }\n  }\n  return dp[m - 1];\n}\n
      min_path_sum.rs
      /* Minimum path sum: Space-optimized dynamic programming */\nfn min_path_sum_dp_comp(grid: &Vec<Vec<i32>>) -> i32 {\n    let (n, m) = (grid.len(), grid[0].len());\n    // Initialize dp table\n    let mut dp = vec![0; m];\n    // State transition: first row\n    dp[0] = grid[0][0];\n    for j in 1..m {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // State transition: rest of the rows\n    for i in 1..n {\n        // State transition: first column\n        dp[0] = dp[0] + grid[i][0];\n        // State transition: rest of the columns\n        for j in 1..m {\n            dp[j] = std::cmp::min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    dp[m - 1]\n}\n
      min_path_sum.c
      /* Minimum path sum: Space-optimized dynamic programming */\nint minPathSumDPComp(int grid[MAX_SIZE][MAX_SIZE], int n, int m) {\n    // Initialize dp table\n    int *dp = calloc(m, sizeof(int));\n    // State transition: first row\n    dp[0] = grid[0][0];\n    for (int j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // State transition: rest of the rows\n    for (int i = 1; i < n; i++) {\n        // State transition: first column\n        dp[0] = dp[0] + grid[i][0];\n        // State transition: rest of the columns\n        for (int j = 1; j < m; j++) {\n            dp[j] = myMin(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    int res = dp[m - 1];\n    // Free memory\n    free(dp);\n    return res;\n}\n
      min_path_sum.kt
      /* Minimum path sum: Space-optimized dynamic programming */\nfun minPathSumDPComp(grid: Array<IntArray>): Int {\n    val n = grid.size\n    val m = grid[0].size\n    // Initialize dp table\n    val dp = IntArray(m)\n    // State transition: first row\n    dp[0] = grid[0][0]\n    for (j in 1..<m) {\n        dp[j] = dp[j - 1] + grid[0][j]\n    }\n    // State transition: rest of the rows\n    for (i in 1..<n) {\n        // State transition: first column\n        dp[0] = dp[0] + grid[i][0]\n        // State transition: rest of the columns\n        for (j in 1..<m) {\n            dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n        }\n    }\n    return dp[m - 1]\n}\n
      min_path_sum.rb
      ### Minimum path sum: space-optimized DP ###\ndef min_path_sum_dp_comp(grid)\n  n, m = grid.length, grid.first.length\n  # Initialize dp table\n  dp = Array.new(m, 0)\n  # State transition: first row\n  dp[0] = grid[0][0]\n  (1...m).each { |j| dp[j] = dp[j - 1] + grid[0][j] }\n  # State transition: rest of the rows\n  for i in 1...n\n    # State transition: first column\n    dp[0] = dp[0] + grid[i][0]\n    # State transition: rest of the columns\n    (1...m).each { |j| dp[j] = [dp[j - 1], dp[j]].min + grid[i][j] }\n  end\n  dp[m - 1]\nend\n
      "},{"location":"chapter_dynamic_programming/edit_distance_problem/","title":"14.6 \u00a0 Edit Distance Problem","text":"

      Edit distance, also known as Levenshtein distance, refers to the minimum number of edits required to transform one string into another, commonly used in information retrieval and natural language processing to measure the similarity between two sequences.

      Question

      Given two strings \\(s\\) and \\(t\\), return the minimum number of edits required to transform \\(s\\) into \\(t\\).

      You can perform three types of edit operations on a string: insert a character, delete a character, or replace a character with any other character.

      As shown in Figure 14-27, transforming kitten into sitting requires 3 edits, including 2 replacements and 1 insertion; transforming hello into algo requires 3 steps, including 2 replacements and 1 deletion.

      Figure 14-27 \u00a0 Example data for edit distance

      The edit distance problem can be naturally explained using the decision tree model. Strings correspond to tree nodes, and a round of decision (one edit operation) corresponds to an edge of the tree.

      As shown in Figure 14-28, without restricting operations, each node can branch into many edges, with each edge corresponding to one operation, meaning there are many possible paths to transform hello into algo.

      From the perspective of the decision tree, the goal of this problem is to find the shortest path between node hello and node algo.

      Figure 14-28 \u00a0 Representing edit distance problem based on decision tree model

      "},{"location":"chapter_dynamic_programming/edit_distance_problem/#1-dynamic-programming-approach","title":"1. \u00a0 Dynamic Programming Approach","text":"

      Step 1: Think about the decisions in each round, define the state, and thus obtain the \\(dp\\) table

      Each round of decision involves performing one edit operation on string \\(s\\).

      We want the problem scale to gradually decrease during the editing process, which allows us to construct subproblems. Let the lengths of strings \\(s\\) and \\(t\\) be \\(n\\) and \\(m\\) respectively. We first consider the tail characters of the two strings, \\(s[n-1]\\) and \\(t[m-1]\\).

      • If \\(s[n-1]\\) and \\(t[m-1]\\) are the same, we can skip them and directly consider \\(s[n-2]\\) and \\(t[m-2]\\).
      • If \\(s[n-1]\\) and \\(t[m-1]\\) are different, we need to perform one edit on \\(s\\) (insert, delete, or replace) to make the tail characters of the two strings the same, allowing us to skip them and consider a smaller-scale problem.

      In other words, each round of decision (edit operation) we make on string \\(s\\) will change the remaining characters to be matched in \\(s\\) and \\(t\\). Therefore, the state is the \\(i\\)-th and \\(j\\)-th characters currently being considered in \\(s\\) and \\(t\\), denoted as \\([i, j]\\).

      State \\([i, j]\\) corresponds to the subproblem: the minimum number of edits required to change the first \\(i\\) characters of \\(s\\) into the first \\(j\\) characters of \\(t\\).

      From this, we obtain a two-dimensional \\(dp\\) table of size \\((i+1) \\times (j+1)\\).

      Step 2: Identify the optimal substructure, and then derive the state transition equation

      Consider subproblem \\(dp[i, j]\\), where the tail characters of the corresponding two strings are \\(s[i-1]\\) and \\(t[j-1]\\), which can be divided into the three cases shown in Figure 14-29 based on different edit operations.

      1. Insert \\(t[j-1]\\) after \\(s[i-1]\\), then the remaining subproblem is \\(dp[i, j-1]\\).
      2. Delete \\(s[i-1]\\), then the remaining subproblem is \\(dp[i-1, j]\\).
      3. Replace \\(s[i-1]\\) with \\(t[j-1]\\), then the remaining subproblem is \\(dp[i-1, j-1]\\).

      Figure 14-29 \u00a0 State transition for edit distance

      Based on the above analysis, the optimal substructure can be obtained: the minimum number of edits for \\(dp[i, j]\\) equals the minimum among the minimum edit steps of \\(dp[i, j-1]\\), \\(dp[i-1, j]\\), and \\(dp[i-1, j-1]\\), plus the edit step \\(1\\) for this time. The corresponding state transition equation is:

      \\[ dp[i, j] = \\min(dp[i, j-1], dp[i-1, j], dp[i-1, j-1]) + 1 \\]

      Please note that when \\(s[i-1]\\) and \\(t[j-1]\\) are the same, no edit is required for the current character, in which case the state transition equation is:

      \\[ dp[i, j] = dp[i-1, j-1] \\]

      Step 3: Determine boundary conditions and state transition order

      When both strings are empty, the number of edit steps is \\(0\\), i.e., \\(dp[0, 0] = 0\\). When \\(s\\) is empty but \\(t\\) is not, the minimum number of edit steps equals the length of \\(t\\), i.e., the first row \\(dp[0, j] = j\\). When \\(s\\) is not empty but \\(t\\) is empty, the minimum number of edit steps equals the length of \\(s\\), i.e., the first column \\(dp[i, 0] = i\\).

      Observing the state transition equation, the solution \\(dp[i, j]\\) depends on solutions to the left, above, and upper-left, so the entire \\(dp\\) table can be traversed in order through two nested loops.

      "},{"location":"chapter_dynamic_programming/edit_distance_problem/#2-code-implementation","title":"2. \u00a0 Code Implementation","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby edit_distance.py
      def edit_distance_dp(s: str, t: str) -> int:\n    \"\"\"Edit distance: Dynamic programming\"\"\"\n    n, m = len(s), len(t)\n    dp = [[0] * (m + 1) for _ in range(n + 1)]\n    # State transition: first row and first column\n    for i in range(1, n + 1):\n        dp[i][0] = i\n    for j in range(1, m + 1):\n        dp[0][j] = j\n    # State transition: rest of the rows and columns\n    for i in range(1, n + 1):\n        for j in range(1, m + 1):\n            if s[i - 1] == t[j - 1]:\n                # If two characters are equal, skip both characters\n                dp[i][j] = dp[i - 1][j - 1]\n            else:\n                # Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1\n    return dp[n][m]\n
      edit_distance.cpp
      /* Edit distance: Dynamic programming */\nint editDistanceDP(string s, string t) {\n    int n = s.length(), m = t.length();\n    vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));\n    // State transition: first row and first column\n    for (int i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s[i - 1] == t[j - 1]) {\n                // If two characters are equal, skip both characters\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
      edit_distance.java
      /* Edit distance: Dynamic programming */\nint editDistanceDP(String s, String t) {\n    int n = s.length(), m = t.length();\n    int[][] dp = new int[n + 1][m + 1];\n    // State transition: first row and first column\n    for (int i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s.charAt(i - 1) == t.charAt(j - 1)) {\n                // If two characters are equal, skip both characters\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
      edit_distance.cs
      /* Edit distance: Dynamic programming */\nint EditDistanceDP(string s, string t) {\n    int n = s.Length, m = t.Length;\n    int[,] dp = new int[n + 1, m + 1];\n    // State transition: first row and first column\n    for (int i = 1; i <= n; i++) {\n        dp[i, 0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0, j] = j;\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s[i - 1] == t[j - 1]) {\n                // If two characters are equal, skip both characters\n                dp[i, j] = dp[i - 1, j - 1];\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[i, j] = Math.Min(Math.Min(dp[i, j - 1], dp[i - 1, j]), dp[i - 1, j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n, m];\n}\n
      edit_distance.go
      /* Edit distance: Dynamic programming */\nfunc editDistanceDP(s string, t string) int {\n    n := len(s)\n    m := len(t)\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, m+1)\n    }\n    // State transition: first row and first column\n    for i := 1; i <= n; i++ {\n        dp[i][0] = i\n    }\n    for j := 1; j <= m; j++ {\n        dp[0][j] = j\n    }\n    // State transition: rest of the rows and columns\n    for i := 1; i <= n; i++ {\n        for j := 1; j <= m; j++ {\n            if s[i-1] == t[j-1] {\n                // If two characters are equal, skip both characters\n                dp[i][j] = dp[i-1][j-1]\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[i][j] = MinInt(MinInt(dp[i][j-1], dp[i-1][j]), dp[i-1][j-1]) + 1\n            }\n        }\n    }\n    return dp[n][m]\n}\n
      edit_distance.swift
      /* Edit distance: Dynamic programming */\nfunc editDistanceDP(s: String, t: String) -> Int {\n    let n = s.utf8CString.count\n    let m = t.utf8CString.count\n    var dp = Array(repeating: Array(repeating: 0, count: m + 1), count: n + 1)\n    // State transition: first row and first column\n    for i in 1 ... n {\n        dp[i][0] = i\n    }\n    for j in 1 ... m {\n        dp[0][j] = j\n    }\n    // State transition: rest of the rows and columns\n    for i in 1 ... n {\n        for j in 1 ... m {\n            if s.utf8CString[i - 1] == t.utf8CString[j - 1] {\n                // If two characters are equal, skip both characters\n                dp[i][j] = dp[i - 1][j - 1]\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1\n            }\n        }\n    }\n    return dp[n][m]\n}\n
      edit_distance.js
      /* Edit distance: Dynamic programming */\nfunction editDistanceDP(s, t) {\n    const n = s.length,\n        m = t.length;\n    const dp = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));\n    // State transition: first row and first column\n    for (let i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (let j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // State transition: rest of the rows and columns\n    for (let i = 1; i <= n; i++) {\n        for (let j = 1; j <= m; j++) {\n            if (s.charAt(i - 1) === t.charAt(j - 1)) {\n                // If two characters are equal, skip both characters\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[i][j] =\n                    Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
      edit_distance.ts
      /* Edit distance: Dynamic programming */\nfunction editDistanceDP(s: string, t: string): number {\n    const n = s.length,\n        m = t.length;\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: m + 1 }, () => 0)\n    );\n    // State transition: first row and first column\n    for (let i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (let j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // State transition: rest of the rows and columns\n    for (let i = 1; i <= n; i++) {\n        for (let j = 1; j <= m; j++) {\n            if (s.charAt(i - 1) === t.charAt(j - 1)) {\n                // If two characters are equal, skip both characters\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[i][j] =\n                    Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
      edit_distance.dart
      /* Edit distance: Dynamic programming */\nint editDistanceDP(String s, String t) {\n  int n = s.length, m = t.length;\n  List<List<int>> dp = List.generate(n + 1, (_) => List.filled(m + 1, 0));\n  // State transition: first row and first column\n  for (int i = 1; i <= n; i++) {\n    dp[i][0] = i;\n  }\n  for (int j = 1; j <= m; j++) {\n    dp[0][j] = j;\n  }\n  // State transition: rest of the rows and columns\n  for (int i = 1; i <= n; i++) {\n    for (int j = 1; j <= m; j++) {\n      if (s[i - 1] == t[j - 1]) {\n        // If two characters are equal, skip both characters\n        dp[i][j] = dp[i - 1][j - 1];\n      } else {\n        // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n        dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n      }\n    }\n  }\n  return dp[n][m];\n}\n
      edit_distance.rs
      /* Edit distance: Dynamic programming */\nfn edit_distance_dp(s: &str, t: &str) -> i32 {\n    let (n, m) = (s.len(), t.len());\n    let mut dp = vec![vec![0; m + 1]; n + 1];\n    // State transition: first row and first column\n    for i in 1..=n {\n        dp[i][0] = i as i32;\n    }\n    for j in 1..m {\n        dp[0][j] = j as i32;\n    }\n    // State transition: rest of the rows and columns\n    for i in 1..=n {\n        for j in 1..=m {\n            if s.chars().nth(i - 1) == t.chars().nth(j - 1) {\n                // If two characters are equal, skip both characters\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[i][j] =\n                    std::cmp::min(std::cmp::min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    dp[n][m]\n}\n
      edit_distance.c
      /* Edit distance: Dynamic programming */\nint editDistanceDP(char *s, char *t, int n, int m) {\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(m + 1, sizeof(int));\n    }\n    // State transition: first row and first column\n    for (int i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s[i - 1] == t[j - 1]) {\n                // If two characters are equal, skip both characters\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[i][j] = myMin(myMin(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    int res = dp[n][m];\n    // Free memory\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    return res;\n}\n
      edit_distance.kt
      /* Edit distance: Dynamic programming */\nfun editDistanceDP(s: String, t: String): Int {\n    val n = s.length\n    val m = t.length\n    val dp = Array(n + 1) { IntArray(m + 1) }\n    // State transition: first row and first column\n    for (i in 1..n) {\n        dp[i][0] = i\n    }\n    for (j in 1..m) {\n        dp[0][j] = j\n    }\n    // State transition: rest of the rows and columns\n    for (i in 1..n) {\n        for (j in 1..m) {\n            if (s[i - 1] == t[j - 1]) {\n                // If two characters are equal, skip both characters\n                dp[i][j] = dp[i - 1][j - 1]\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1\n            }\n        }\n    }\n    return dp[n][m]\n}\n
      edit_distance.rb
      ### Edit distance: dynamic programming ###\ndef edit_distance_dp(s, t)\n  n, m = s.length, t.length\n  dp = Array.new(n + 1) { Array.new(m + 1, 0) }\n  # State transition: first row and first column\n  (1...(n + 1)).each { |i| dp[i][0] = i }\n  (1...(m + 1)).each { |j| dp[0][j] = j }\n  # State transition: rest of the rows and columns\n  for i in 1...(n + 1)\n    for j in 1...(m +1)\n      if s[i - 1] == t[j - 1]\n        # If two characters are equal, skip both characters\n        dp[i][j] = dp[i - 1][j - 1]\n      else\n        # Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n        dp[i][j] = [dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]].min + 1\n      end\n    end\n  end\n  dp[n][m]\nend\n

      As shown in Figure 14-30, the state transition process for the edit distance problem is very similar to the knapsack problem and can both be viewed as the process of filling a two-dimensional grid.

      <1><2><3><4><5><6><7><8><9><10><11><12><13><14><15>

      Figure 14-30 \u00a0 Dynamic programming process for edit distance

      "},{"location":"chapter_dynamic_programming/edit_distance_problem/#3-space-optimization","title":"3. \u00a0 Space Optimization","text":"

      Since \\(dp[i, j]\\) is transferred from the solutions above \\(dp[i-1, j]\\), to the left \\(dp[i, j-1]\\), and to the upper-left \\(dp[i-1, j-1]\\), forward traversal will lose the upper-left solution \\(dp[i-1, j-1]\\), and reverse traversal cannot build \\(dp[i, j-1]\\) in advance, so neither traversal order is feasible.

      For this reason, we can use a variable leftup to temporarily store the upper-left solution \\(dp[i-1, j-1]\\), so we only need to consider the solutions to the left and above. This situation is the same as the unbounded knapsack problem, allowing for forward traversal. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby edit_distance.py
      def edit_distance_dp_comp(s: str, t: str) -> int:\n    \"\"\"Edit distance: Space-optimized dynamic programming\"\"\"\n    n, m = len(s), len(t)\n    dp = [0] * (m + 1)\n    # State transition: first row\n    for j in range(1, m + 1):\n        dp[j] = j\n    # State transition: rest of the rows\n    for i in range(1, n + 1):\n        # State transition: first column\n        leftup = dp[0]  # Temporarily store dp[i-1, j-1]\n        dp[0] += 1\n        # State transition: rest of the columns\n        for j in range(1, m + 1):\n            temp = dp[j]\n            if s[i - 1] == t[j - 1]:\n                # If two characters are equal, skip both characters\n                dp[j] = leftup\n            else:\n                # Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[j] = min(dp[j - 1], dp[j], leftup) + 1\n            leftup = temp  # Update for next round's dp[i-1, j-1]\n    return dp[m]\n
      edit_distance.cpp
      /* Edit distance: Space-optimized dynamic programming */\nint editDistanceDPComp(string s, string t) {\n    int n = s.length(), m = t.length();\n    vector<int> dp(m + 1, 0);\n    // State transition: first row\n    for (int j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // State transition: rest of the rows\n    for (int i = 1; i <= n; i++) {\n        // State transition: first column\n        int leftup = dp[0]; // Temporarily store dp[i-1, j-1]\n        dp[0] = i;\n        // State transition: rest of the columns\n        for (int j = 1; j <= m; j++) {\n            int temp = dp[j];\n            if (s[i - 1] == t[j - 1]) {\n                // If two characters are equal, skip both characters\n                dp[j] = leftup;\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // Update for next round's dp[i-1, j-1]\n        }\n    }\n    return dp[m];\n}\n
      edit_distance.java
      /* Edit distance: Space-optimized dynamic programming */\nint editDistanceDPComp(String s, String t) {\n    int n = s.length(), m = t.length();\n    int[] dp = new int[m + 1];\n    // State transition: first row\n    for (int j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // State transition: rest of the rows\n    for (int i = 1; i <= n; i++) {\n        // State transition: first column\n        int leftup = dp[0]; // Temporarily store dp[i-1, j-1]\n        dp[0] = i;\n        // State transition: rest of the columns\n        for (int j = 1; j <= m; j++) {\n            int temp = dp[j];\n            if (s.charAt(i - 1) == t.charAt(j - 1)) {\n                // If two characters are equal, skip both characters\n                dp[j] = leftup;\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // Update for next round's dp[i-1, j-1]\n        }\n    }\n    return dp[m];\n}\n
      edit_distance.cs
      /* Edit distance: Space-optimized dynamic programming */\nint EditDistanceDPComp(string s, string t) {\n    int n = s.Length, m = t.Length;\n    int[] dp = new int[m + 1];\n    // State transition: first row\n    for (int j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // State transition: rest of the rows\n    for (int i = 1; i <= n; i++) {\n        // State transition: first column\n        int leftup = dp[0]; // Temporarily store dp[i-1, j-1]\n        dp[0] = i;\n        // State transition: rest of the columns\n        for (int j = 1; j <= m; j++) {\n            int temp = dp[j];\n            if (s[i - 1] == t[j - 1]) {\n                // If two characters are equal, skip both characters\n                dp[j] = leftup;\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[j] = Math.Min(Math.Min(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // Update for next round's dp[i-1, j-1]\n        }\n    }\n    return dp[m];\n}\n
      edit_distance.go
      /* Edit distance: Space-optimized dynamic programming */\nfunc editDistanceDPComp(s string, t string) int {\n    n := len(s)\n    m := len(t)\n    dp := make([]int, m+1)\n    // State transition: first row\n    for j := 1; j <= m; j++ {\n        dp[j] = j\n    }\n    // State transition: rest of the rows\n    for i := 1; i <= n; i++ {\n        // State transition: first column\n        leftUp := dp[0] // Temporarily store dp[i-1, j-1]\n        dp[0] = i\n        // State transition: rest of the columns\n        for j := 1; j <= m; j++ {\n            temp := dp[j]\n            if s[i-1] == t[j-1] {\n                // If two characters are equal, skip both characters\n                dp[j] = leftUp\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[j] = MinInt(MinInt(dp[j-1], dp[j]), leftUp) + 1\n            }\n            leftUp = temp // Update for next round's dp[i-1, j-1]\n        }\n    }\n    return dp[m]\n}\n
      edit_distance.swift
      /* Edit distance: Space-optimized dynamic programming */\nfunc editDistanceDPComp(s: String, t: String) -> Int {\n    let n = s.utf8CString.count\n    let m = t.utf8CString.count\n    var dp = Array(repeating: 0, count: m + 1)\n    // State transition: first row\n    for j in 1 ... m {\n        dp[j] = j\n    }\n    // State transition: rest of the rows\n    for i in 1 ... n {\n        // State transition: first column\n        var leftup = dp[0] // Temporarily store dp[i-1, j-1]\n        dp[0] = i\n        // State transition: rest of the columns\n        for j in 1 ... m {\n            let temp = dp[j]\n            if s.utf8CString[i - 1] == t.utf8CString[j - 1] {\n                // If two characters are equal, skip both characters\n                dp[j] = leftup\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1\n            }\n            leftup = temp // Update for next round's dp[i-1, j-1]\n        }\n    }\n    return dp[m]\n}\n
      edit_distance.js
      /* Edit distance: Space-optimized dynamic programming */\nfunction editDistanceDPComp(s, t) {\n    const n = s.length,\n        m = t.length;\n    const dp = new Array(m + 1).fill(0);\n    // State transition: first row\n    for (let j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // State transition: rest of the rows\n    for (let i = 1; i <= n; i++) {\n        // State transition: first column\n        let leftup = dp[0]; // Temporarily store dp[i-1, j-1]\n        dp[0] = i;\n        // State transition: rest of the columns\n        for (let j = 1; j <= m; j++) {\n            const temp = dp[j];\n            if (s.charAt(i - 1) === t.charAt(j - 1)) {\n                // If two characters are equal, skip both characters\n                dp[j] = leftup;\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[j] = Math.min(dp[j - 1], dp[j], leftup) + 1;\n            }\n            leftup = temp; // Update for next round's dp[i-1, j-1]\n        }\n    }\n    return dp[m];\n}\n
      edit_distance.ts
      /* Edit distance: Space-optimized dynamic programming */\nfunction editDistanceDPComp(s: string, t: string): number {\n    const n = s.length,\n        m = t.length;\n    const dp = new Array(m + 1).fill(0);\n    // State transition: first row\n    for (let j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // State transition: rest of the rows\n    for (let i = 1; i <= n; i++) {\n        // State transition: first column\n        let leftup = dp[0]; // Temporarily store dp[i-1, j-1]\n        dp[0] = i;\n        // State transition: rest of the columns\n        for (let j = 1; j <= m; j++) {\n            const temp = dp[j];\n            if (s.charAt(i - 1) === t.charAt(j - 1)) {\n                // If two characters are equal, skip both characters\n                dp[j] = leftup;\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[j] = Math.min(dp[j - 1], dp[j], leftup) + 1;\n            }\n            leftup = temp; // Update for next round's dp[i-1, j-1]\n        }\n    }\n    return dp[m];\n}\n
      edit_distance.dart
      /* Edit distance: Space-optimized dynamic programming */\nint editDistanceDPComp(String s, String t) {\n  int n = s.length, m = t.length;\n  List<int> dp = List.filled(m + 1, 0);\n  // State transition: first row\n  for (int j = 1; j <= m; j++) {\n    dp[j] = j;\n  }\n  // State transition: rest of the rows\n  for (int i = 1; i <= n; i++) {\n    // State transition: first column\n    int leftup = dp[0]; // Temporarily store dp[i-1, j-1]\n    dp[0] = i;\n    // State transition: rest of the columns\n    for (int j = 1; j <= m; j++) {\n      int temp = dp[j];\n      if (s[i - 1] == t[j - 1]) {\n        // If two characters are equal, skip both characters\n        dp[j] = leftup;\n      } else {\n        // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n        dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n      }\n      leftup = temp; // Update for next round's dp[i-1, j-1]\n    }\n  }\n  return dp[m];\n}\n
      edit_distance.rs
      /* Edit distance: Space-optimized dynamic programming */\nfn edit_distance_dp_comp(s: &str, t: &str) -> i32 {\n    let (n, m) = (s.len(), t.len());\n    let mut dp = vec![0; m + 1];\n    // State transition: first row\n    for j in 1..m {\n        dp[j] = j as i32;\n    }\n    // State transition: rest of the rows\n    for i in 1..=n {\n        // State transition: first column\n        let mut leftup = dp[0]; // Temporarily store dp[i-1, j-1]\n        dp[0] = i as i32;\n        // State transition: rest of the columns\n        for j in 1..=m {\n            let temp = dp[j];\n            if s.chars().nth(i - 1) == t.chars().nth(j - 1) {\n                // If two characters are equal, skip both characters\n                dp[j] = leftup;\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[j] = std::cmp::min(std::cmp::min(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // Update for next round's dp[i-1, j-1]\n        }\n    }\n    dp[m]\n}\n
      edit_distance.c
      /* Edit distance: Space-optimized dynamic programming */\nint editDistanceDPComp(char *s, char *t, int n, int m) {\n    int *dp = calloc(m + 1, sizeof(int));\n    // State transition: first row\n    for (int j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // State transition: rest of the rows\n    for (int i = 1; i <= n; i++) {\n        // State transition: first column\n        int leftup = dp[0]; // Temporarily store dp[i-1, j-1]\n        dp[0] = i;\n        // State transition: rest of the columns\n        for (int j = 1; j <= m; j++) {\n            int temp = dp[j];\n            if (s[i - 1] == t[j - 1]) {\n                // If two characters are equal, skip both characters\n                dp[j] = leftup;\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[j] = myMin(myMin(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // Update for next round's dp[i-1, j-1]\n        }\n    }\n    int res = dp[m];\n    // Free memory\n    free(dp);\n    return res;\n}\n
      edit_distance.kt
      /* Edit distance: Space-optimized dynamic programming */\nfun editDistanceDPComp(s: String, t: String): Int {\n    val n = s.length\n    val m = t.length\n    val dp = IntArray(m + 1)\n    // State transition: first row\n    for (j in 1..m) {\n        dp[j] = j\n    }\n    // State transition: rest of the rows\n    for (i in 1..n) {\n        // State transition: first column\n        var leftup = dp[0] // Temporarily store dp[i-1, j-1]\n        dp[0] = i\n        // State transition: rest of the columns\n        for (j in 1..m) {\n            val temp = dp[j]\n            if (s[i - 1] == t[j - 1]) {\n                // If two characters are equal, skip both characters\n                dp[j] = leftup\n            } else {\n                // Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n                dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1\n            }\n            leftup = temp // Update for next round's dp[i-1, j-1]\n        }\n    }\n    return dp[m]\n}\n
      edit_distance.rb
      ### Edit distance: space-optimized DP ###\ndef edit_distance_dp_comp(s, t)\n  n, m = s.length, t.length\n  dp = Array.new(m + 1, 0)\n  # State transition: first row\n  (1...(m + 1)).each { |j| dp[j] = j }\n  # State transition: rest of the rows\n  for i in 1...(n + 1)\n    # State transition: first column\n    leftup = dp.first # Temporarily store dp[i-1, j-1]\n    dp[0] += 1\n    # State transition: rest of the columns\n    for j in 1...(m + 1)\n      temp = dp[j]\n      if s[i - 1] == t[j - 1]\n        # If two characters are equal, skip both characters\n        dp[j] = leftup\n      else\n        # Minimum edit steps = minimum edit steps of insert, delete, replace + 1\n        dp[j] = [dp[j - 1], dp[j], leftup].min + 1\n      end\n      leftup = temp # Update for next round's dp[i-1, j-1]\n    end\n  end\n  dp[m]\nend\n
      "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/","title":"14.1 \u00a0 Introduction to Dynamic Programming","text":"

      Dynamic programming is an important algorithmic paradigm that decomposes a problem into a series of smaller subproblems and avoids redundant computation by storing the solutions to subproblems, thereby significantly improving time efficiency.

      In this section, we start with a classic example, first presenting its brute force backtracking solution, observing the overlapping subproblems within it, and then gradually deriving a more efficient dynamic programming solution.

      Climbing stairs

      Given a staircase with \\(n\\) steps, where you can climb \\(1\\) or \\(2\\) steps at a time, how many different ways are there to reach the top?

      As shown in Figure 14-1, for a \\(3\\)-step staircase, there are \\(3\\) different ways to reach the top.

      Figure 14-1 \u00a0 Number of ways to reach the 3rd step

      The goal of this problem is to find the number of ways, we can consider using backtracking to enumerate all possibilities. Specifically, imagine climbing stairs as a multi-round selection process: starting from the ground, choosing to go up \\(1\\) or \\(2\\) steps in each round, incrementing the count by \\(1\\) whenever the top of the stairs is reached, and pruning when exceeding the top. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_backtrack.py
      def backtrack(choices: list[int], state: int, n: int, res: list[int]) -> int:\n    \"\"\"Backtracking\"\"\"\n    # When climbing to the n-th stair, add 1 to the solution count\n    if state == n:\n        res[0] += 1\n    # Traverse all choices\n    for choice in choices:\n        # Pruning: not allowed to go beyond the n-th stair\n        if state + choice > n:\n            continue\n        # Attempt: make a choice, update state\n        backtrack(choices, state + choice, n, res)\n        # Backtrack\n\ndef climbing_stairs_backtrack(n: int) -> int:\n    \"\"\"Climbing stairs: Backtracking\"\"\"\n    choices = [1, 2]  # Can choose to climb up 1 or 2 stairs\n    state = 0  # Start climbing from the 0-th stair\n    res = [0]  # Use res[0] to record the solution count\n    backtrack(choices, state, n, res)\n    return res[0]\n
      climbing_stairs_backtrack.cpp
      /* Backtracking */\nvoid backtrack(vector<int> &choices, int state, int n, vector<int> &res) {\n    // When climbing to the n-th stair, add 1 to the solution count\n    if (state == n)\n        res[0]++;\n    // Traverse all choices\n    for (auto &choice : choices) {\n        // Pruning: not allowed to go beyond the n-th stair\n        if (state + choice > n)\n            continue;\n        // Attempt: make choice, update state\n        backtrack(choices, state + choice, n, res);\n        // Backtrack\n    }\n}\n\n/* Climbing stairs: Backtracking */\nint climbingStairsBacktrack(int n) {\n    vector<int> choices = {1, 2}; // Can choose to climb up 1 or 2 stairs\n    int state = 0;                // Start climbing from the 0-th stair\n    vector<int> res = {0};        // Use res[0] to record the solution count\n    backtrack(choices, state, n, res);\n    return res[0];\n}\n
      climbing_stairs_backtrack.java
      /* Backtracking */\nvoid backtrack(List<Integer> choices, int state, int n, List<Integer> res) {\n    // When climbing to the n-th stair, add 1 to the solution count\n    if (state == n)\n        res.set(0, res.get(0) + 1);\n    // Traverse all choices\n    for (Integer choice : choices) {\n        // Pruning: not allowed to go beyond the n-th stair\n        if (state + choice > n)\n            continue;\n        // Attempt: make choice, update state\n        backtrack(choices, state + choice, n, res);\n        // Backtrack\n    }\n}\n\n/* Climbing stairs: Backtracking */\nint climbingStairsBacktrack(int n) {\n    List<Integer> choices = Arrays.asList(1, 2); // Can choose to climb up 1 or 2 stairs\n    int state = 0; // Start climbing from the 0-th stair\n    List<Integer> res = new ArrayList<>();\n    res.add(0); // Use res[0] to record the solution count\n    backtrack(choices, state, n, res);\n    return res.get(0);\n}\n
      climbing_stairs_backtrack.cs
      /* Backtracking */\nvoid Backtrack(List<int> choices, int state, int n, List<int> res) {\n    // When climbing to the n-th stair, add 1 to the solution count\n    if (state == n)\n        res[0]++;\n    // Traverse all choices\n    foreach (int choice in choices) {\n        // Pruning: not allowed to go beyond the n-th stair\n        if (state + choice > n)\n            continue;\n        // Attempt: make choice, update state\n        Backtrack(choices, state + choice, n, res);\n        // Backtrack\n    }\n}\n\n/* Climbing stairs: Backtracking */\nint ClimbingStairsBacktrack(int n) {\n    List<int> choices = [1, 2]; // Can choose to climb up 1 or 2 stairs\n    int state = 0; // Start climbing from the 0-th stair\n    List<int> res = [0]; // Use res[0] to record the solution count\n    Backtrack(choices, state, n, res);\n    return res[0];\n}\n
      climbing_stairs_backtrack.go
      /* Backtracking */\nfunc backtrack(choices []int, state, n int, res []int) {\n    // When climbing to the n-th stair, add 1 to the solution count\n    if state == n {\n        res[0] = res[0] + 1\n    }\n    // Traverse all choices\n    for _, choice := range choices {\n        // Pruning: not allowed to go beyond the n-th stair\n        if state+choice > n {\n            continue\n        }\n        // Attempt: make choice, update state\n        backtrack(choices, state+choice, n, res)\n        // Backtrack\n    }\n}\n\n/* Climbing stairs: Backtracking */\nfunc climbingStairsBacktrack(n int) int {\n    // Can choose to climb up 1 or 2 stairs\n    choices := []int{1, 2}\n    // Start climbing from the 0-th stair\n    state := 0\n    res := make([]int, 1)\n    // Use res[0] to record the solution count\n    res[0] = 0\n    backtrack(choices, state, n, res)\n    return res[0]\n}\n
      climbing_stairs_backtrack.swift
      /* Backtracking */\nfunc backtrack(choices: [Int], state: Int, n: Int, res: inout [Int]) {\n    // When climbing to the n-th stair, add 1 to the solution count\n    if state == n {\n        res[0] += 1\n    }\n    // Traverse all choices\n    for choice in choices {\n        // Pruning: not allowed to go beyond the n-th stair\n        if state + choice > n {\n            continue\n        }\n        // Attempt: make choice, update state\n        backtrack(choices: choices, state: state + choice, n: n, res: &res)\n        // Backtrack\n    }\n}\n\n/* Climbing stairs: Backtracking */\nfunc climbingStairsBacktrack(n: Int) -> Int {\n    let choices = [1, 2] // Can choose to climb up 1 or 2 stairs\n    let state = 0 // Start climbing from the 0-th stair\n    var res: [Int] = []\n    res.append(0) // Use res[0] to record the solution count\n    backtrack(choices: choices, state: state, n: n, res: &res)\n    return res[0]\n}\n
      climbing_stairs_backtrack.js
      /* Backtracking */\nfunction backtrack(choices, state, n, res) {\n    // When climbing to the n-th stair, add 1 to the solution count\n    if (state === n) res.set(0, res.get(0) + 1);\n    // Traverse all choices\n    for (const choice of choices) {\n        // Pruning: not allowed to go beyond the n-th stair\n        if (state + choice > n) continue;\n        // Attempt: make choice, update state\n        backtrack(choices, state + choice, n, res);\n        // Backtrack\n    }\n}\n\n/* Climbing stairs: Backtracking */\nfunction climbingStairsBacktrack(n) {\n    const choices = [1, 2]; // Can choose to climb up 1 or 2 stairs\n    const state = 0; // Start climbing from the 0-th stair\n    const res = new Map();\n    res.set(0, 0); // Use res[0] to record the solution count\n    backtrack(choices, state, n, res);\n    return res.get(0);\n}\n
      climbing_stairs_backtrack.ts
      /* Backtracking */\nfunction backtrack(\n    choices: number[],\n    state: number,\n    n: number,\n    res: Map<0, any>\n): void {\n    // When climbing to the n-th stair, add 1 to the solution count\n    if (state === n) res.set(0, res.get(0) + 1);\n    // Traverse all choices\n    for (const choice of choices) {\n        // Pruning: not allowed to go beyond the n-th stair\n        if (state + choice > n) continue;\n        // Attempt: make choice, update state\n        backtrack(choices, state + choice, n, res);\n        // Backtrack\n    }\n}\n\n/* Climbing stairs: Backtracking */\nfunction climbingStairsBacktrack(n: number): number {\n    const choices = [1, 2]; // Can choose to climb up 1 or 2 stairs\n    const state = 0; // Start climbing from the 0-th stair\n    const res = new Map();\n    res.set(0, 0); // Use res[0] to record the solution count\n    backtrack(choices, state, n, res);\n    return res.get(0);\n}\n
      climbing_stairs_backtrack.dart
      /* Backtracking */\nvoid backtrack(List<int> choices, int state, int n, List<int> res) {\n  // When climbing to the n-th stair, add 1 to the solution count\n  if (state == n) {\n    res[0]++;\n  }\n  // Traverse all choices\n  for (int choice in choices) {\n    // Pruning: not allowed to go beyond the n-th stair\n    if (state + choice > n) continue;\n    // Attempt: make choice, update state\n    backtrack(choices, state + choice, n, res);\n    // Backtrack\n  }\n}\n\n/* Climbing stairs: Backtracking */\nint climbingStairsBacktrack(int n) {\n  List<int> choices = [1, 2]; // Can choose to climb up 1 or 2 stairs\n  int state = 0; // Start climbing from the 0-th stair\n  List<int> res = [];\n  res.add(0); // Use res[0] to record the solution count\n  backtrack(choices, state, n, res);\n  return res[0];\n}\n
      climbing_stairs_backtrack.rs
      /* Backtracking */\nfn backtrack(choices: &[i32], state: i32, n: i32, res: &mut [i32]) {\n    // When climbing to the n-th stair, add 1 to the solution count\n    if state == n {\n        res[0] = res[0] + 1;\n    }\n    // Traverse all choices\n    for &choice in choices {\n        // Pruning: not allowed to go beyond the n-th stair\n        if state + choice > n {\n            continue;\n        }\n        // Attempt: make choice, update state\n        backtrack(choices, state + choice, n, res);\n        // Backtrack\n    }\n}\n\n/* Climbing stairs: Backtracking */\nfn climbing_stairs_backtrack(n: usize) -> i32 {\n    let choices = vec![1, 2]; // Can choose to climb up 1 or 2 stairs\n    let state = 0; // Start climbing from the 0-th stair\n    let mut res = Vec::new();\n    res.push(0); // Use res[0] to record the solution count\n    backtrack(&choices, state, n as i32, &mut res);\n    res[0]\n}\n
      climbing_stairs_backtrack.c
      /* Backtracking */\nvoid backtrack(int *choices, int state, int n, int *res, int len) {\n    // When climbing to the n-th stair, add 1 to the solution count\n    if (state == n)\n        res[0]++;\n    // Traverse all choices\n    for (int i = 0; i < len; i++) {\n        int choice = choices[i];\n        // Pruning: not allowed to go beyond the n-th stair\n        if (state + choice > n)\n            continue;\n        // Attempt: make choice, update state\n        backtrack(choices, state + choice, n, res, len);\n        // Backtrack\n    }\n}\n\n/* Climbing stairs: Backtracking */\nint climbingStairsBacktrack(int n) {\n    int choices[2] = {1, 2}; // Can choose to climb up 1 or 2 stairs\n    int state = 0;           // Start climbing from the 0-th stair\n    int *res = (int *)malloc(sizeof(int));\n    *res = 0; // Use res[0] to record the solution count\n    int len = sizeof(choices) / sizeof(int);\n    backtrack(choices, state, n, res, len);\n    int result = *res;\n    free(res);\n    return result;\n}\n
      climbing_stairs_backtrack.kt
      /* Backtracking */\nfun backtrack(\n    choices: MutableList<Int>,\n    state: Int,\n    n: Int,\n    res: MutableList<Int>\n) {\n    // When climbing to the n-th stair, add 1 to the solution count\n    if (state == n)\n        res[0] = res[0] + 1\n    // Traverse all choices\n    for (choice in choices) {\n        // Pruning: not allowed to go beyond the n-th stair\n        if (state + choice > n) continue\n        // Attempt: make choice, update state\n        backtrack(choices, state + choice, n, res)\n        // Backtrack\n    }\n}\n\n/* Climbing stairs: Backtracking */\nfun climbingStairsBacktrack(n: Int): Int {\n    val choices = mutableListOf(1, 2) // Can choose to climb up 1 or 2 stairs\n    val state = 0 // Start climbing from the 0-th stair\n    val res = mutableListOf<Int>()\n    res.add(0) // Use res[0] to record the solution count\n    backtrack(choices, state, n, res)\n    return res[0]\n}\n
      climbing_stairs_backtrack.rb
      ### Backtracking ###\ndef backtrack(choices, state, n, res)\n  # When climbing to the n-th stair, add 1 to the solution count\n  res[0] += 1 if state == n\n  # Traverse all choices\n  for choice in choices\n    # Pruning: not allowed to go beyond the n-th stair\n    next if state + choice > n\n\n    # Attempt: make choice, update state\n    backtrack(choices, state + choice, n, res)\n  end\n  # Backtrack\nend\n\n### Climbing stairs: backtracking ###\ndef climbing_stairs_backtrack(n)\n  choices = [1, 2] # Can choose to climb up 1 or 2 stairs\n  state = 0 # Start climbing from the 0-th stair\n  res = [0] # Use res[0] to record the solution count\n  backtrack(choices, state, n, res)\n  res.first\nend\n
      "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1411-method-1-brute-force-search","title":"14.1.1 \u00a0 Method 1: Brute Force Search","text":"

      Backtracking algorithms typically do not explicitly decompose problems, but rather treat solving the problem as a series of decision steps, searching for all possible solutions through trial and pruning.

      We can try to analyze this problem from the perspective of problem decomposition. Let the number of ways to climb to the \\(i\\)-th step be \\(dp[i]\\), then \\(dp[i]\\) is the original problem, and its subproblems include:

      \\[ dp[i-1], dp[i-2], \\dots, dp[2], dp[1] \\]

      Since we can only go up \\(1\\) or \\(2\\) steps in each round, when we stand on the \\(i\\)-th step, we could only have been on the \\(i-1\\)-th or \\(i-2\\)-th step in the previous round. In other words, we can only reach the \\(i\\)-th step from the \\(i-1\\)-th or \\(i-2\\)-th step.

      This leads to an important conclusion: the number of ways to climb to the \\(i-1\\)-th step plus the number of ways to climb to the \\(i-2\\)-th step equals the number of ways to climb to the \\(i\\)-th step. The formula is as follows:

      \\[ dp[i] = dp[i-1] + dp[i-2] \\]

      This means that in the stair climbing problem, there exists a recurrence relation among the subproblems, the solution to the original problem can be constructed from the solutions to the subproblems. Figure 14-2 illustrates this recurrence relation.

      Figure 14-2 \u00a0 Recurrence relation for the number of ways

      We can obtain a brute force search solution based on the recurrence formula. Starting from \\(dp[n]\\), recursively decompose a larger problem into the sum of two smaller problems, until reaching the smallest subproblems \\(dp[1]\\) and \\(dp[2]\\) and returning. Among them, the solutions to the smallest subproblems are known, namely \\(dp[1] = 1\\) and \\(dp[2] = 2\\), representing \\(1\\) and \\(2\\) ways to climb to the \\(1\\)st and \\(2\\)nd steps, respectively.

      Observe the following code, which, like standard backtracking code, belongs to depth-first search but is more concise:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dfs.py
      def dfs(i: int) -> int:\n    \"\"\"Search\"\"\"\n    # Known dp[1] and dp[2], return them\n    if i == 1 or i == 2:\n        return i\n    # dp[i] = dp[i-1] + dp[i-2]\n    count = dfs(i - 1) + dfs(i - 2)\n    return count\n\ndef climbing_stairs_dfs(n: int) -> int:\n    \"\"\"Climbing stairs: Search\"\"\"\n    return dfs(n)\n
      climbing_stairs_dfs.cpp
      /* Search */\nint dfs(int i) {\n    // Known dp[1] and dp[2], return them\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* Climbing stairs: Search */\nint climbingStairsDFS(int n) {\n    return dfs(n);\n}\n
      climbing_stairs_dfs.java
      /* Search */\nint dfs(int i) {\n    // Known dp[1] and dp[2], return them\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* Climbing stairs: Search */\nint climbingStairsDFS(int n) {\n    return dfs(n);\n}\n
      climbing_stairs_dfs.cs
      /* Search */\nint DFS(int i) {\n    // Known dp[1] and dp[2], return them\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = DFS(i - 1) + DFS(i - 2);\n    return count;\n}\n\n/* Climbing stairs: Search */\nint ClimbingStairsDFS(int n) {\n    return DFS(n);\n}\n
      climbing_stairs_dfs.go
      /* Search */\nfunc dfs(i int) int {\n    // Known dp[1] and dp[2], return them\n    if i == 1 || i == 2 {\n        return i\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    count := dfs(i-1) + dfs(i-2)\n    return count\n}\n\n/* Climbing stairs: Search */\nfunc climbingStairsDFS(n int) int {\n    return dfs(n)\n}\n
      climbing_stairs_dfs.swift
      /* Search */\nfunc dfs(i: Int) -> Int {\n    // Known dp[1] and dp[2], return them\n    if i == 1 || i == 2 {\n        return i\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    let count = dfs(i: i - 1) + dfs(i: i - 2)\n    return count\n}\n\n/* Climbing stairs: Search */\nfunc climbingStairsDFS(n: Int) -> Int {\n    dfs(i: n)\n}\n
      climbing_stairs_dfs.js
      /* Search */\nfunction dfs(i) {\n    // Known dp[1] and dp[2], return them\n    if (i === 1 || i === 2) return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    const count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* Climbing stairs: Search */\nfunction climbingStairsDFS(n) {\n    return dfs(n);\n}\n
      climbing_stairs_dfs.ts
      /* Search */\nfunction dfs(i: number): number {\n    // Known dp[1] and dp[2], return them\n    if (i === 1 || i === 2) return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    const count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* Climbing stairs: Search */\nfunction climbingStairsDFS(n: number): number {\n    return dfs(n);\n}\n
      climbing_stairs_dfs.dart
      /* Search */\nint dfs(int i) {\n  // Known dp[1] and dp[2], return them\n  if (i == 1 || i == 2) return i;\n  // dp[i] = dp[i-1] + dp[i-2]\n  int count = dfs(i - 1) + dfs(i - 2);\n  return count;\n}\n\n/* Climbing stairs: Search */\nint climbingStairsDFS(int n) {\n  return dfs(n);\n}\n
      climbing_stairs_dfs.rs
      /* Search */\nfn dfs(i: usize) -> i32 {\n    // Known dp[1] and dp[2], return them\n    if i == 1 || i == 2 {\n        return i as i32;\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    let count = dfs(i - 1) + dfs(i - 2);\n    count\n}\n\n/* Climbing stairs: Search */\nfn climbing_stairs_dfs(n: usize) -> i32 {\n    dfs(n)\n}\n
      climbing_stairs_dfs.c
      /* Search */\nint dfs(int i) {\n    // Known dp[1] and dp[2], return them\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* Climbing stairs: Search */\nint climbingStairsDFS(int n) {\n    return dfs(n);\n}\n
      climbing_stairs_dfs.kt
      /* Search */\nfun dfs(i: Int): Int {\n    // Known dp[1] and dp[2], return them\n    if (i == 1 || i == 2) return i\n    // dp[i] = dp[i-1] + dp[i-2]\n    val count = dfs(i - 1) + dfs(i - 2)\n    return count\n}\n\n/* Climbing stairs: Search */\nfun climbingStairsDFS(n: Int): Int {\n    return dfs(n)\n}\n
      climbing_stairs_dfs.rb
      ### Search ###\ndef dfs(i)\n  # Known dp[1] and dp[2], return them\n  return i if i == 1 || i == 2\n  # dp[i] = dp[i-1] + dp[i-2]\n  dfs(i - 1) + dfs(i - 2)\nend\n\n### Climbing stairs: search ###\ndef climbing_stairs_dfs(n)\n  dfs(n)\nend\n

      Figure 14-3 shows the recursion tree formed by brute force search. For the problem \\(dp[n]\\), the depth of its recursion tree is \\(n\\), with a time complexity of \\(O(2^n)\\). Exponential order represents explosive growth; if we input a relatively large \\(n\\), we will fall into a long wait.

      Figure 14-3 \u00a0 Recursion tree for climbing stairs

      Observing the above figure, the exponential time complexity is caused by \"overlapping subproblems\". For example, \\(dp[9]\\) is decomposed into \\(dp[8]\\) and \\(dp[7]\\), and \\(dp[8]\\) is decomposed into \\(dp[7]\\) and \\(dp[6]\\), both of which contain the subproblem \\(dp[7]\\).

      And so on, subproblems contain smaller overlapping subproblems, ad infinitum. The vast majority of computational resources are wasted on these overlapping subproblems.

      "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1412-method-2-memoization","title":"14.1.2 \u00a0 Method 2: Memoization","text":"

      To improve algorithm efficiency, we want all overlapping subproblems to be computed only once. For this purpose, we declare an array mem to record the solution to each subproblem and prune overlapping subproblems during the search process.

      1. When computing \\(dp[i]\\) for the first time, we record it in mem[i] for later use.
      2. When we need to compute \\(dp[i]\\) again, we can directly retrieve the result from mem[i], thereby avoiding redundant computation of that subproblem.

      The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dfs_mem.py
      def dfs(i: int, mem: list[int]) -> int:\n    \"\"\"Memoization search\"\"\"\n    # Known dp[1] and dp[2], return them\n    if i == 1 or i == 2:\n        return i\n    # If record dp[i] exists, return it directly\n    if mem[i] != -1:\n        return mem[i]\n    # dp[i] = dp[i-1] + dp[i-2]\n    count = dfs(i - 1, mem) + dfs(i - 2, mem)\n    # Record dp[i]\n    mem[i] = count\n    return count\n\ndef climbing_stairs_dfs_mem(n: int) -> int:\n    \"\"\"Climbing stairs: Memoization search\"\"\"\n    # mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n    mem = [-1] * (n + 1)\n    return dfs(n, mem)\n
      climbing_stairs_dfs_mem.cpp
      /* Memoization search */\nint dfs(int i, vector<int> &mem) {\n    // Known dp[1] and dp[2], return them\n    if (i == 1 || i == 2)\n        return i;\n    // If record dp[i] exists, return it directly\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // Record dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* Climbing stairs: Memoization search */\nint climbingStairsDFSMem(int n) {\n    // mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n    vector<int> mem(n + 1, -1);\n    return dfs(n, mem);\n}\n
      climbing_stairs_dfs_mem.java
      /* Memoization search */\nint dfs(int i, int[] mem) {\n    // Known dp[1] and dp[2], return them\n    if (i == 1 || i == 2)\n        return i;\n    // If record dp[i] exists, return it directly\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // Record dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* Climbing stairs: Memoization search */\nint climbingStairsDFSMem(int n) {\n    // mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n    int[] mem = new int[n + 1];\n    Arrays.fill(mem, -1);\n    return dfs(n, mem);\n}\n
      climbing_stairs_dfs_mem.cs
      /* Memoization search */\nint DFS(int i, int[] mem) {\n    // Known dp[1] and dp[2], return them\n    if (i == 1 || i == 2)\n        return i;\n    // If record dp[i] exists, return it directly\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = DFS(i - 1, mem) + DFS(i - 2, mem);\n    // Record dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* Climbing stairs: Memoization search */\nint ClimbingStairsDFSMem(int n) {\n    // mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n    int[] mem = new int[n + 1];\n    Array.Fill(mem, -1);\n    return DFS(n, mem);\n}\n
      climbing_stairs_dfs_mem.go
      /* Memoization search */\nfunc dfsMem(i int, mem []int) int {\n    // Known dp[1] and dp[2], return them\n    if i == 1 || i == 2 {\n        return i\n    }\n    // If record dp[i] exists, return it directly\n    if mem[i] != -1 {\n        return mem[i]\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    count := dfsMem(i-1, mem) + dfsMem(i-2, mem)\n    // Record dp[i]\n    mem[i] = count\n    return count\n}\n\n/* Climbing stairs: Memoization search */\nfunc climbingStairsDFSMem(n int) int {\n    // mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n    mem := make([]int, n+1)\n    for i := range mem {\n        mem[i] = -1\n    }\n    return dfsMem(n, mem)\n}\n
      climbing_stairs_dfs_mem.swift
      /* Memoization search */\nfunc dfs(i: Int, mem: inout [Int]) -> Int {\n    // Known dp[1] and dp[2], return them\n    if i == 1 || i == 2 {\n        return i\n    }\n    // If record dp[i] exists, return it directly\n    if mem[i] != -1 {\n        return mem[i]\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    let count = dfs(i: i - 1, mem: &mem) + dfs(i: i - 2, mem: &mem)\n    // Record dp[i]\n    mem[i] = count\n    return count\n}\n\n/* Climbing stairs: Memoization search */\nfunc climbingStairsDFSMem(n: Int) -> Int {\n    // mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n    var mem = Array(repeating: -1, count: n + 1)\n    return dfs(i: n, mem: &mem)\n}\n
      climbing_stairs_dfs_mem.js
      /* Memoization search */\nfunction dfs(i, mem) {\n    // Known dp[1] and dp[2], return them\n    if (i === 1 || i === 2) return i;\n    // If record dp[i] exists, return it directly\n    if (mem[i] != -1) return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    const count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // Record dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* Climbing stairs: Memoization search */\nfunction climbingStairsDFSMem(n) {\n    // mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n    const mem = new Array(n + 1).fill(-1);\n    return dfs(n, mem);\n}\n
      climbing_stairs_dfs_mem.ts
      /* Memoization search */\nfunction dfs(i: number, mem: number[]): number {\n    // Known dp[1] and dp[2], return them\n    if (i === 1 || i === 2) return i;\n    // If record dp[i] exists, return it directly\n    if (mem[i] != -1) return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    const count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // Record dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* Climbing stairs: Memoization search */\nfunction climbingStairsDFSMem(n: number): number {\n    // mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n    const mem = new Array(n + 1).fill(-1);\n    return dfs(n, mem);\n}\n
      climbing_stairs_dfs_mem.dart
      /* Memoization search */\nint dfs(int i, List<int> mem) {\n  // Known dp[1] and dp[2], return them\n  if (i == 1 || i == 2) return i;\n  // If record dp[i] exists, return it directly\n  if (mem[i] != -1) return mem[i];\n  // dp[i] = dp[i-1] + dp[i-2]\n  int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n  // Record dp[i]\n  mem[i] = count;\n  return count;\n}\n\n/* Climbing stairs: Memoization search */\nint climbingStairsDFSMem(int n) {\n  // mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n  List<int> mem = List.filled(n + 1, -1);\n  return dfs(n, mem);\n}\n
      climbing_stairs_dfs_mem.rs
      /* Memoization search */\nfn dfs(i: usize, mem: &mut [i32]) -> i32 {\n    // Known dp[1] and dp[2], return them\n    if i == 1 || i == 2 {\n        return i as i32;\n    }\n    // If record dp[i] exists, return it directly\n    if mem[i] != -1 {\n        return mem[i];\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    let count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // Record dp[i]\n    mem[i] = count;\n    count\n}\n\n/* Climbing stairs: Memoization search */\nfn climbing_stairs_dfs_mem(n: usize) -> i32 {\n    // mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n    let mut mem = vec![-1; n + 1];\n    dfs(n, &mut mem)\n}\n
      climbing_stairs_dfs_mem.c
      /* Memoization search */\nint dfs(int i, int *mem) {\n    // Known dp[1] and dp[2], return them\n    if (i == 1 || i == 2)\n        return i;\n    // If record dp[i] exists, return it directly\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // Record dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* Climbing stairs: Memoization search */\nint climbingStairsDFSMem(int n) {\n    // mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n    int *mem = (int *)malloc((n + 1) * sizeof(int));\n    for (int i = 0; i <= n; i++) {\n        mem[i] = -1;\n    }\n    int result = dfs(n, mem);\n    free(mem);\n    return result;\n}\n
      climbing_stairs_dfs_mem.kt
      /* Memoization search */\nfun dfs(i: Int, mem: IntArray): Int {\n    // Known dp[1] and dp[2], return them\n    if (i == 1 || i == 2) return i\n    // If record dp[i] exists, return it directly\n    if (mem[i] != -1) return mem[i]\n    // dp[i] = dp[i-1] + dp[i-2]\n    val count = dfs(i - 1, mem) + dfs(i - 2, mem)\n    // Record dp[i]\n    mem[i] = count\n    return count\n}\n\n/* Climbing stairs: Memoization search */\nfun climbingStairsDFSMem(n: Int): Int {\n    // mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n    val mem = IntArray(n + 1)\n    mem.fill(-1)\n    return dfs(n, mem)\n}\n
      climbing_stairs_dfs_mem.rb
      ### Memoization search ###\ndef dfs(i, mem)\n  # Known dp[1] and dp[2], return them\n  return i if i == 1 || i == 2\n  # If record dp[i] exists, return it directly\n  return mem[i] if mem[i] != -1\n\n  # dp[i] = dp[i-1] + dp[i-2]\n  count = dfs(i - 1, mem) + dfs(i - 2, mem)\n  # Record dp[i]\n  mem[i] = count\nend\n\n### Climbing stairs: memoization search ###\ndef climbing_stairs_dfs_mem(n)\n  # mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record\n  mem = Array.new(n + 1, -1)\n  dfs(n, mem)\nend\n

      Observe Figure 14-4, after memoization, all overlapping subproblems only need to be computed once, optimizing the time complexity to \\(O(n)\\), which is a tremendous leap.

      Figure 14-4 \u00a0 Recursion tree with memoization

      "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1413-method-3-dynamic-programming","title":"14.1.3 \u00a0 Method 3: Dynamic Programming","text":"

      Memoization is a \"top-down\" method: we start from the original problem (root node), recursively decompose larger subproblems into smaller ones, until reaching the smallest known subproblems (leaf nodes). Afterward, by backtracking, we collect the solutions to the subproblems layer by layer to construct the solution to the original problem.

      In contrast, dynamic programming is a \"bottom-up\" method: starting from the solutions to the smallest subproblems, iteratively constructing solutions to larger subproblems until obtaining the solution to the original problem.

      Since dynamic programming does not include a backtracking process, it only requires loop iteration for implementation and does not need recursion. In the following code, we initialize an array dp to store the solutions to subproblems, which serves the same recording function as the array mem in memoization:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dp.py
      def climbing_stairs_dp(n: int) -> int:\n    \"\"\"Climbing stairs: Dynamic programming\"\"\"\n    if n == 1 or n == 2:\n        return n\n    # Initialize dp table, used to store solutions to subproblems\n    dp = [0] * (n + 1)\n    # Initial state: preset the solution to the smallest subproblem\n    dp[1], dp[2] = 1, 2\n    # State transition: gradually solve larger subproblems from smaller ones\n    for i in range(3, n + 1):\n        dp[i] = dp[i - 1] + dp[i - 2]\n    return dp[n]\n
      climbing_stairs_dp.cpp
      /* Climbing stairs: Dynamic programming */\nint climbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // Initialize dp table, used to store solutions to subproblems\n    vector<int> dp(n + 1);\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = 1;\n    dp[2] = 2;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
      climbing_stairs_dp.java
      /* Climbing stairs: Dynamic programming */\nint climbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // Initialize dp table, used to store solutions to subproblems\n    int[] dp = new int[n + 1];\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = 1;\n    dp[2] = 2;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
      climbing_stairs_dp.cs
      /* Climbing stairs: Dynamic programming */\nint ClimbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // Initialize dp table, used to store solutions to subproblems\n    int[] dp = new int[n + 1];\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = 1;\n    dp[2] = 2;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
      climbing_stairs_dp.go
      /* Climbing stairs: Dynamic programming */\nfunc climbingStairsDP(n int) int {\n    if n == 1 || n == 2 {\n        return n\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    dp := make([]int, n+1)\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = 1\n    dp[2] = 2\n    // State transition: gradually solve larger subproblems from smaller ones\n    for i := 3; i <= n; i++ {\n        dp[i] = dp[i-1] + dp[i-2]\n    }\n    return dp[n]\n}\n
      climbing_stairs_dp.swift
      /* Climbing stairs: Dynamic programming */\nfunc climbingStairsDP(n: Int) -> Int {\n    if n == 1 || n == 2 {\n        return n\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    var dp = Array(repeating: 0, count: n + 1)\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = 1\n    dp[2] = 2\n    // State transition: gradually solve larger subproblems from smaller ones\n    for i in 3 ... n {\n        dp[i] = dp[i - 1] + dp[i - 2]\n    }\n    return dp[n]\n}\n
      climbing_stairs_dp.js
      /* Climbing stairs: Dynamic programming */\nfunction climbingStairsDP(n) {\n    if (n === 1 || n === 2) return n;\n    // Initialize dp table, used to store solutions to subproblems\n    const dp = new Array(n + 1).fill(-1);\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = 1;\n    dp[2] = 2;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (let i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
      climbing_stairs_dp.ts
      /* Climbing stairs: Dynamic programming */\nfunction climbingStairsDP(n: number): number {\n    if (n === 1 || n === 2) return n;\n    // Initialize dp table, used to store solutions to subproblems\n    const dp = new Array(n + 1).fill(-1);\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = 1;\n    dp[2] = 2;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (let i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
      climbing_stairs_dp.dart
      /* Climbing stairs: Dynamic programming */\nint climbingStairsDP(int n) {\n  if (n == 1 || n == 2) return n;\n  // Initialize dp table, used to store solutions to subproblems\n  List<int> dp = List.filled(n + 1, 0);\n  // Initial state: preset the solution to the smallest subproblem\n  dp[1] = 1;\n  dp[2] = 2;\n  // State transition: gradually solve larger subproblems from smaller ones\n  for (int i = 3; i <= n; i++) {\n    dp[i] = dp[i - 1] + dp[i - 2];\n  }\n  return dp[n];\n}\n
      climbing_stairs_dp.rs
      /* Climbing stairs: Dynamic programming */\nfn climbing_stairs_dp(n: usize) -> i32 {\n    // Known dp[1] and dp[2], return them\n    if n == 1 || n == 2 {\n        return n as i32;\n    }\n    // Initialize dp table, used to store solutions to subproblems\n    let mut dp = vec![-1; n + 1];\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = 1;\n    dp[2] = 2;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for i in 3..=n {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    dp[n]\n}\n
      climbing_stairs_dp.c
      /* Climbing stairs: Dynamic programming */\nint climbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // Initialize dp table, used to store solutions to subproblems\n    int *dp = (int *)malloc((n + 1) * sizeof(int));\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = 1;\n    dp[2] = 2;\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    int result = dp[n];\n    free(dp);\n    return result;\n}\n
      climbing_stairs_dp.kt
      /* Climbing stairs: Dynamic programming */\nfun climbingStairsDP(n: Int): Int {\n    if (n == 1 || n == 2) return n\n    // Initialize dp table, used to store solutions to subproblems\n    val dp = IntArray(n + 1)\n    // Initial state: preset the solution to the smallest subproblem\n    dp[1] = 1\n    dp[2] = 2\n    // State transition: gradually solve larger subproblems from smaller ones\n    for (i in 3..n) {\n        dp[i] = dp[i - 1] + dp[i - 2]\n    }\n    return dp[n]\n}\n
      climbing_stairs_dp.rb
      ### Climbing stairs: dynamic programming ###\ndef climbing_stairs_dp(n)\n  return n  if n == 1 || n == 2\n\n  # Initialize dp table, used to store solutions to subproblems\n  dp = Array.new(n + 1, 0)\n  # Initial state: preset the solution to the smallest subproblem\n  dp[1], dp[2] = 1, 2\n  # State transition: gradually solve larger subproblems from smaller ones\n  (3...(n + 1)).each { |i| dp[i] = dp[i - 1] + dp[i - 2] }\n\n  dp[n]\nend\n

      Figure 14-5 simulates the execution process of the above code.

      Figure 14-5 \u00a0 Dynamic programming process for climbing stairs

      Like backtracking algorithms, dynamic programming also uses the \"state\" concept to represent specific stages of problem solving, with each state corresponding to a subproblem and its corresponding local optimal solution. For example, the state in the stair climbing problem is defined as the current stair step number \\(i\\).

      Based on the above content, we can summarize the commonly used terminology in dynamic programming.

      • The array dp is called the dp table, where \\(dp[i]\\) represents the solution to the subproblem corresponding to state \\(i\\).
      • The states corresponding to the smallest subproblems (the \\(1\\)st and \\(2\\)nd steps) are called initial states.
      • The recurrence formula \\(dp[i] = dp[i-1] + dp[i-2]\\) is called the state transition equation.
      "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1414-space-optimization","title":"14.1.4 \u00a0 Space Optimization","text":"

      Observant readers may have noticed that since \\(dp[i]\\) is only related to \\(dp[i-1]\\) and \\(dp[i-2]\\), we do not need to use an array dp to store the solutions to all subproblems, but can simply use two variables to roll forward. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dp.py
      def climbing_stairs_dp_comp(n: int) -> int:\n    \"\"\"Climbing stairs: Space-optimized dynamic programming\"\"\"\n    if n == 1 or n == 2:\n        return n\n    a, b = 1, 2\n    for _ in range(3, n + 1):\n        a, b = b, a + b\n    return b\n
      climbing_stairs_dp.cpp
      /* Climbing stairs: Space-optimized dynamic programming */\nint climbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
      climbing_stairs_dp.java
      /* Climbing stairs: Space-optimized dynamic programming */\nint climbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
      climbing_stairs_dp.cs
      /* Climbing stairs: Space-optimized dynamic programming */\nint ClimbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
      climbing_stairs_dp.go
      /* Climbing stairs: Space-optimized dynamic programming */\nfunc climbingStairsDPComp(n int) int {\n    if n == 1 || n == 2 {\n        return n\n    }\n    a, b := 1, 2\n    // State transition: gradually solve larger subproblems from smaller ones\n    for i := 3; i <= n; i++ {\n        a, b = b, a+b\n    }\n    return b\n}\n
      climbing_stairs_dp.swift
      /* Climbing stairs: Space-optimized dynamic programming */\nfunc climbingStairsDPComp(n: Int) -> Int {\n    if n == 1 || n == 2 {\n        return n\n    }\n    var a = 1\n    var b = 2\n    for _ in 3 ... n {\n        (a, b) = (b, a + b)\n    }\n    return b\n}\n
      climbing_stairs_dp.js
      /* Climbing stairs: Space-optimized dynamic programming */\nfunction climbingStairsDPComp(n) {\n    if (n === 1 || n === 2) return n;\n    let a = 1,\n        b = 2;\n    for (let i = 3; i <= n; i++) {\n        const tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
      climbing_stairs_dp.ts
      /* Climbing stairs: Space-optimized dynamic programming */\nfunction climbingStairsDPComp(n: number): number {\n    if (n === 1 || n === 2) return n;\n    let a = 1,\n        b = 2;\n    for (let i = 3; i <= n; i++) {\n        const tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
      climbing_stairs_dp.dart
      /* Climbing stairs: Space-optimized dynamic programming */\nint climbingStairsDPComp(int n) {\n  if (n == 1 || n == 2) return n;\n  int a = 1, b = 2;\n  for (int i = 3; i <= n; i++) {\n    int tmp = b;\n    b = a + b;\n    a = tmp;\n  }\n  return b;\n}\n
      climbing_stairs_dp.rs
      /* Climbing stairs: Space-optimized dynamic programming */\nfn climbing_stairs_dp_comp(n: usize) -> i32 {\n    if n == 1 || n == 2 {\n        return n as i32;\n    }\n    let (mut a, mut b) = (1, 2);\n    for _ in 3..=n {\n        let tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    b\n}\n
      climbing_stairs_dp.c
      /* Climbing stairs: Space-optimized dynamic programming */\nint climbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
      climbing_stairs_dp.kt
      /* Climbing stairs: Space-optimized dynamic programming */\nfun climbingStairsDPComp(n: Int): Int {\n    if (n == 1 || n == 2) return n\n    var a = 1\n    var b = 2\n    for (i in 3..n) {\n        val temp = b\n        b += a\n        a = temp\n    }\n    return b\n}\n
      climbing_stairs_dp.rb
      ### Climbing stairs: space-optimized DP ###\ndef climbing_stairs_dp_comp(n)\n  return n if n == 1 || n == 2\n\n  a, b = 1, 2\n  (3...(n + 1)).each { a, b = b, a + b }\n\n  b\nend\n

      Observing the above code, since the space occupied by the array dp is saved, the space complexity is reduced from \\(O(n)\\) to \\(O(1)\\).

      In dynamic programming problems, the current state often depends only on a limited number of preceding states, allowing us to retain only the necessary states and save memory space through \"dimension reduction\". This space optimization technique is called \"rolling variable\" or \"rolling array\".

      "},{"location":"chapter_dynamic_programming/knapsack_problem/","title":"14.4 \u00a0 0-1 Knapsack Problem","text":"

      The knapsack problem is an excellent introductory problem for dynamic programming and is one of the most common problem forms in dynamic programming. It has many variants, such as the 0-1 knapsack problem, the unbounded knapsack problem, and the multiple knapsack problem.

      In this section, we will first solve the most common 0-1 knapsack problem.

      Question

      Given \\(n\\) items, where the weight of the \\(i\\)-th item is \\(wgt[i-1]\\) and its value is \\(val[i-1]\\), and a knapsack with capacity \\(cap\\). Each item can only be selected once. What is the maximum value that can be placed in the knapsack within the capacity limit?

      Observe Figure 14-17. Since item number \\(i\\) starts counting from \\(1\\) and array indices start from \\(0\\), item \\(i\\) corresponds to weight \\(wgt[i-1]\\) and value \\(val[i-1]\\).

      Figure 14-17 \u00a0 Example data for 0-1 knapsack

      We can view the 0-1 knapsack problem as a process consisting of \\(n\\) rounds of decisions, where for each item there are two decisions: not putting it in and putting it in, thus the problem satisfies the decision tree model.

      The goal of this problem is to find \"the maximum value that can be placed in the knapsack within the capacity limit\", so it is more likely to be a dynamic programming problem.

      Step 1: Think about the decisions in each round, define the state, and thus obtain the \\(dp\\) table

      For each item, if not placed in the knapsack, the knapsack capacity remains unchanged; if placed in, the knapsack capacity decreases. From this, we can derive the state definition: current item number \\(i\\) and knapsack capacity \\(c\\), denoted as \\([i, c]\\).

      State \\([i, c]\\) corresponds to the subproblem: the maximum value among the first \\(i\\) items in a knapsack of capacity \\(c\\), denoted as \\(dp[i, c]\\).

      What we need to find is \\(dp[n, cap]\\), so we need a two-dimensional \\(dp\\) table of size \\((n+1) \\times (cap+1)\\).

      Step 2: Identify the optimal substructure, and then derive the state transition equation

      After making the decision for item \\(i\\), what remains is the subproblem of the first \\(i-1\\) items, which can be divided into the following two cases.

      • Not putting item \\(i\\): The knapsack capacity remains unchanged, and the state changes to \\([i-1, c]\\).
      • Putting item \\(i\\): The knapsack capacity decreases by \\(wgt[i-1]\\), the value increases by \\(val[i-1]\\), and the state changes to \\([i-1, c-wgt[i-1]]\\).

      The above analysis reveals the optimal substructure of this problem: the maximum value \\(dp[i, c]\\) equals the larger value between not putting item \\(i\\) and putting item \\(i\\). From this, the state transition equation can be derived:

      \\[ dp[i, c] = \\max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1]) \\]

      Note that if the weight of the current item \\(wgt[i - 1]\\) exceeds the remaining knapsack capacity \\(c\\), then the only option is not to put it in the knapsack.

      Step 3: Determine boundary conditions and state transition order

      When there are no items or the knapsack capacity is \\(0\\), the maximum value is \\(0\\), i.e., the first column \\(dp[i, 0]\\) and the first row \\(dp[0, c]\\) are both equal to \\(0\\).

      The current state \\([i, c]\\) is transferred from the state above \\([i-1, c]\\) and the state in the upper-left \\([i-1, c-wgt[i-1]]\\), so the entire \\(dp\\) table is traversed in order through two nested loops.

      Based on the above analysis, we will next implement the brute force search, memoization, and dynamic programming solutions in order.

      "},{"location":"chapter_dynamic_programming/knapsack_problem/#1-method-1-brute-force-search","title":"1. \u00a0 Method 1: Brute Force Search","text":"

      The search code includes the following elements.

      • Recursive parameters: state \\([i, c]\\).
      • Return value: solution to the subproblem \\(dp[i, c]\\).
      • Termination condition: when the item number is out of bounds \\(i = 0\\) or the remaining knapsack capacity is \\(0\\), terminate recursion and return value \\(0\\).
      • Pruning: if the weight of the current item exceeds the remaining knapsack capacity, only the option of not putting it in is available.
      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
      def knapsack_dfs(wgt: list[int], val: list[int], i: int, c: int) -> int:\n    \"\"\"0-1 knapsack: Brute-force search\"\"\"\n    # If all items have been selected or knapsack has no remaining capacity, return value 0\n    if i == 0 or c == 0:\n        return 0\n    # If exceeds knapsack capacity, can only choose not to put it in\n    if wgt[i - 1] > c:\n        return knapsack_dfs(wgt, val, i - 1, c)\n    # Calculate the maximum value of not putting in and putting in item i\n    no = knapsack_dfs(wgt, val, i - 1, c)\n    yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]\n    # Return the larger value of the two options\n    return max(no, yes)\n
      knapsack.cpp
      /* 0-1 knapsack: Brute-force search */\nint knapsackDFS(vector<int> &wgt, vector<int> &val, int i, int c) {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    int no = knapsackDFS(wgt, val, i - 1, c);\n    int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // Return the larger value of the two options\n    return max(no, yes);\n}\n
      knapsack.java
      /* 0-1 knapsack: Brute-force search */\nint knapsackDFS(int[] wgt, int[] val, int i, int c) {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    int no = knapsackDFS(wgt, val, i - 1, c);\n    int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // Return the larger value of the two options\n    return Math.max(no, yes);\n}\n
      knapsack.cs
      /* 0-1 knapsack: Brute-force search */\nint KnapsackDFS(int[] weight, int[] val, int i, int c) {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (weight[i - 1] > c) {\n        return KnapsackDFS(weight, val, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    int no = KnapsackDFS(weight, val, i - 1, c);\n    int yes = KnapsackDFS(weight, val, i - 1, c - weight[i - 1]) + val[i - 1];\n    // Return the larger value of the two options\n    return Math.Max(no, yes);\n}\n
      knapsack.go
      /* 0-1 knapsack: Brute-force search */\nfunc knapsackDFS(wgt, val []int, i, c int) int {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if i == 0 || c == 0 {\n        return 0\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if wgt[i-1] > c {\n        return knapsackDFS(wgt, val, i-1, c)\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    no := knapsackDFS(wgt, val, i-1, c)\n    yes := knapsackDFS(wgt, val, i-1, c-wgt[i-1]) + val[i-1]\n    // Return the larger value of the two options\n    return int(math.Max(float64(no), float64(yes)))\n}\n
      knapsack.swift
      /* 0-1 knapsack: Brute-force search */\nfunc knapsackDFS(wgt: [Int], val: [Int], i: Int, c: Int) -> Int {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if i == 0 || c == 0 {\n        return 0\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if wgt[i - 1] > c {\n        return knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    let no = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)\n    let yes = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]\n    // Return the larger value of the two options\n    return max(no, yes)\n}\n
      knapsack.js
      /* 0-1 knapsack: Brute-force search */\nfunction knapsackDFS(wgt, val, i, c) {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i === 0 || c === 0) {\n        return 0;\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    const no = knapsackDFS(wgt, val, i - 1, c);\n    const yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // Return the larger value of the two options\n    return Math.max(no, yes);\n}\n
      knapsack.ts
      /* 0-1 knapsack: Brute-force search */\nfunction knapsackDFS(\n    wgt: Array<number>,\n    val: Array<number>,\n    i: number,\n    c: number\n): number {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i === 0 || c === 0) {\n        return 0;\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    const no = knapsackDFS(wgt, val, i - 1, c);\n    const yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // Return the larger value of the two options\n    return Math.max(no, yes);\n}\n
      knapsack.dart
      /* 0-1 knapsack: Brute-force search */\nint knapsackDFS(List<int> wgt, List<int> val, int i, int c) {\n  // If all items have been selected or knapsack has no remaining capacity, return value 0\n  if (i == 0 || c == 0) {\n    return 0;\n  }\n  // If exceeds knapsack capacity, can only choose not to put it in\n  if (wgt[i - 1] > c) {\n    return knapsackDFS(wgt, val, i - 1, c);\n  }\n  // Calculate the maximum value of not putting in and putting in item i\n  int no = knapsackDFS(wgt, val, i - 1, c);\n  int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n  // Return the larger value of the two options\n  return max(no, yes);\n}\n
      knapsack.rs
      /* 0-1 knapsack: Brute-force search */\nfn knapsack_dfs(wgt: &[i32], val: &[i32], i: usize, c: usize) -> i32 {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if i == 0 || c == 0 {\n        return 0;\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if wgt[i - 1] > c as i32 {\n        return knapsack_dfs(wgt, val, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    let no = knapsack_dfs(wgt, val, i - 1, c);\n    let yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1] as usize) + val[i - 1];\n    // Return the larger value of the two options\n    std::cmp::max(no, yes)\n}\n
      knapsack.c
      /* 0-1 knapsack: Brute-force search */\nint knapsackDFS(int wgt[], int val[], int i, int c) {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    int no = knapsackDFS(wgt, val, i - 1, c);\n    int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // Return the larger value of the two options\n    return myMax(no, yes);\n}\n
      knapsack.kt
      /* 0-1 knapsack: Brute-force search */\nfun knapsackDFS(\n    wgt: IntArray,\n    _val: IntArray,\n    i: Int,\n    c: Int\n): Int {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i == 0 || c == 0) {\n        return 0\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, _val, i - 1, c)\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    val no = knapsackDFS(wgt, _val, i - 1, c)\n    val yes = knapsackDFS(wgt, _val, i - 1, c - wgt[i - 1]) + _val[i - 1]\n    // Return the larger value of the two options\n    return max(no, yes)\n}\n
      knapsack.rb
      ### 0-1 knapsack: brute force search ###\ndef knapsack_dfs(wgt, val, i, c)\n  # If all items have been selected or knapsack has no remaining capacity, return value 0\n  return 0 if i == 0 || c == 0\n  # If exceeds knapsack capacity, can only choose not to put it in\n  return knapsack_dfs(wgt, val, i - 1, c) if wgt[i - 1] > c\n  # Calculate the maximum value of not putting in and putting in item i\n  no = knapsack_dfs(wgt, val, i - 1, c)\n  yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]\n  # Return the larger value of the two options\n  [no, yes].max\nend\n

      As shown in Figure 14-18, since each item generates two search branches of not selecting and selecting, the time complexity is \\(O(2^n)\\).

      Observing the recursion tree, it is easy to see overlapping subproblems, such as \\(dp[1, 10]\\). When there are many items, large knapsack capacity, and especially many items with the same weight, the number of overlapping subproblems will increase significantly.

      Figure 14-18 \u00a0 Brute force search recursion tree for 0-1 knapsack problem

      "},{"location":"chapter_dynamic_programming/knapsack_problem/#2-method-2-memoization","title":"2. \u00a0 Method 2: Memoization","text":"

      To ensure that overlapping subproblems are only computed once, we use a memo list mem to record the solutions to subproblems, where mem[i][c] corresponds to \\(dp[i, c]\\).

      After introducing memoization, the time complexity depends on the number of subproblems, which is \\(O(n \\times cap)\\). The implementation code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
      def knapsack_dfs_mem(\n    wgt: list[int], val: list[int], mem: list[list[int]], i: int, c: int\n) -> int:\n    \"\"\"0-1 knapsack: Memoization search\"\"\"\n    # If all items have been selected or knapsack has no remaining capacity, return value 0\n    if i == 0 or c == 0:\n        return 0\n    # If there's a record, return it directly\n    if mem[i][c] != -1:\n        return mem[i][c]\n    # If exceeds knapsack capacity, can only choose not to put it in\n    if wgt[i - 1] > c:\n        return knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n    # Calculate the maximum value of not putting in and putting in item i\n    no = knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n    yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1]\n    # Record and return the larger value of the two options\n    mem[i][c] = max(no, yes)\n    return mem[i][c]\n
      knapsack.cpp
      /* 0-1 knapsack: Memoization search */\nint knapsackDFSMem(vector<int> &wgt, vector<int> &val, vector<vector<int>> &mem, int i, int c) {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // If there's a record, return it directly\n    if (mem[i][c] != -1) {\n        return mem[i][c];\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, mem, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n    int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // Record and return the larger value of the two options\n    mem[i][c] = max(no, yes);\n    return mem[i][c];\n}\n
      knapsack.java
      /* 0-1 knapsack: Memoization search */\nint knapsackDFSMem(int[] wgt, int[] val, int[][] mem, int i, int c) {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // If there's a record, return it directly\n    if (mem[i][c] != -1) {\n        return mem[i][c];\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, mem, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n    int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // Record and return the larger value of the two options\n    mem[i][c] = Math.max(no, yes);\n    return mem[i][c];\n}\n
      knapsack.cs
      /* 0-1 knapsack: Memoization search */\nint KnapsackDFSMem(int[] weight, int[] val, int[][] mem, int i, int c) {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // If there's a record, return it directly\n    if (mem[i][c] != -1) {\n        return mem[i][c];\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (weight[i - 1] > c) {\n        return KnapsackDFSMem(weight, val, mem, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    int no = KnapsackDFSMem(weight, val, mem, i - 1, c);\n    int yes = KnapsackDFSMem(weight, val, mem, i - 1, c - weight[i - 1]) + val[i - 1];\n    // Record and return the larger value of the two options\n    mem[i][c] = Math.Max(no, yes);\n    return mem[i][c];\n}\n
      knapsack.go
      /* 0-1 knapsack: Memoization search */\nfunc knapsackDFSMem(wgt, val []int, mem [][]int, i, c int) int {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if i == 0 || c == 0 {\n        return 0\n    }\n    // If there's a record, return it directly\n    if mem[i][c] != -1 {\n        return mem[i][c]\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if wgt[i-1] > c {\n        return knapsackDFSMem(wgt, val, mem, i-1, c)\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    no := knapsackDFSMem(wgt, val, mem, i-1, c)\n    yes := knapsackDFSMem(wgt, val, mem, i-1, c-wgt[i-1]) + val[i-1]\n    // Return the larger value of the two options\n    mem[i][c] = int(math.Max(float64(no), float64(yes)))\n    return mem[i][c]\n}\n
      knapsack.swift
      /* 0-1 knapsack: Memoization search */\nfunc knapsackDFSMem(wgt: [Int], val: [Int], mem: inout [[Int]], i: Int, c: Int) -> Int {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if i == 0 || c == 0 {\n        return 0\n    }\n    // If there's a record, return it directly\n    if mem[i][c] != -1 {\n        return mem[i][c]\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if wgt[i - 1] > c {\n        return knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c)\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    let no = knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c)\n    let yes = knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]\n    // Record and return the larger value of the two options\n    mem[i][c] = max(no, yes)\n    return mem[i][c]\n}\n
      knapsack.js
      /* 0-1 knapsack: Memoization search */\nfunction knapsackDFSMem(wgt, val, mem, i, c) {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i === 0 || c === 0) {\n        return 0;\n    }\n    // If there's a record, return it directly\n    if (mem[i][c] !== -1) {\n        return mem[i][c];\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, mem, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    const no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n    const yes =\n        knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // Record and return the larger value of the two options\n    mem[i][c] = Math.max(no, yes);\n    return mem[i][c];\n}\n
      knapsack.ts
      /* 0-1 knapsack: Memoization search */\nfunction knapsackDFSMem(\n    wgt: Array<number>,\n    val: Array<number>,\n    mem: Array<Array<number>>,\n    i: number,\n    c: number\n): number {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i === 0 || c === 0) {\n        return 0;\n    }\n    // If there's a record, return it directly\n    if (mem[i][c] !== -1) {\n        return mem[i][c];\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, mem, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    const no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n    const yes =\n        knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // Record and return the larger value of the two options\n    mem[i][c] = Math.max(no, yes);\n    return mem[i][c];\n}\n
      knapsack.dart
      /* 0-1 knapsack: Memoization search */\nint knapsackDFSMem(\n  List<int> wgt,\n  List<int> val,\n  List<List<int>> mem,\n  int i,\n  int c,\n) {\n  // If all items have been selected or knapsack has no remaining capacity, return value 0\n  if (i == 0 || c == 0) {\n    return 0;\n  }\n  // If there's a record, return it directly\n  if (mem[i][c] != -1) {\n    return mem[i][c];\n  }\n  // If exceeds knapsack capacity, can only choose not to put it in\n  if (wgt[i - 1] > c) {\n    return knapsackDFSMem(wgt, val, mem, i - 1, c);\n  }\n  // Calculate the maximum value of not putting in and putting in item i\n  int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n  int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n  // Record and return the larger value of the two options\n  mem[i][c] = max(no, yes);\n  return mem[i][c];\n}\n
      knapsack.rs
      /* 0-1 knapsack: Memoization search */\nfn knapsack_dfs_mem(wgt: &[i32], val: &[i32], mem: &mut Vec<Vec<i32>>, i: usize, c: usize) -> i32 {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if i == 0 || c == 0 {\n        return 0;\n    }\n    // If there's a record, return it directly\n    if mem[i][c] != -1 {\n        return mem[i][c];\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if wgt[i - 1] > c as i32 {\n        return knapsack_dfs_mem(wgt, val, mem, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    let no = knapsack_dfs_mem(wgt, val, mem, i - 1, c);\n    let yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1] as usize) + val[i - 1];\n    // Record and return the larger value of the two options\n    mem[i][c] = std::cmp::max(no, yes);\n    mem[i][c]\n}\n
      knapsack.c
      /* 0-1 knapsack: Memoization search */\nint knapsackDFSMem(int wgt[], int val[], int memCols, int **mem, int i, int c) {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // If there's a record, return it directly\n    if (mem[i][c] != -1) {\n        return mem[i][c];\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, memCols, mem, i - 1, c);\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    int no = knapsackDFSMem(wgt, val, memCols, mem, i - 1, c);\n    int yes = knapsackDFSMem(wgt, val, memCols, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // Record and return the larger value of the two options\n    mem[i][c] = myMax(no, yes);\n    return mem[i][c];\n}\n
      knapsack.kt
      /* 0-1 knapsack: Memoization search */\nfun knapsackDFSMem(\n    wgt: IntArray,\n    _val: IntArray,\n    mem: Array<IntArray>,\n    i: Int,\n    c: Int\n): Int {\n    // If all items have been selected or knapsack has no remaining capacity, return value 0\n    if (i == 0 || c == 0) {\n        return 0\n    }\n    // If there's a record, return it directly\n    if (mem[i][c] != -1) {\n        return mem[i][c]\n    }\n    // If exceeds knapsack capacity, can only choose not to put it in\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, _val, mem, i - 1, c)\n    }\n    // Calculate the maximum value of not putting in and putting in item i\n    val no = knapsackDFSMem(wgt, _val, mem, i - 1, c)\n    val yes = knapsackDFSMem(wgt, _val, mem, i - 1, c - wgt[i - 1]) + _val[i - 1]\n    // Record and return the larger value of the two options\n    mem[i][c] = max(no, yes)\n    return mem[i][c]\n}\n
      knapsack.rb
      ### 0-1 knapsack: memoization search ###\ndef knapsack_dfs_mem(wgt, val, mem, i, c)\n  # If all items have been selected or knapsack has no remaining capacity, return value 0\n  return 0 if i == 0 || c == 0\n  # If there's a record, return it directly\n  return mem[i][c] if mem[i][c] != -1\n  # If exceeds knapsack capacity, can only choose not to put it in\n  return knapsack_dfs_mem(wgt, val, mem, i - 1, c) if wgt[i - 1] > c\n  # Calculate the maximum value of not putting in and putting in item i\n  no = knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n  yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1]\n  # Record and return the larger value of the two options\n  mem[i][c] = [no, yes].max\nend\n

      Figure 14-19 shows the search branches pruned in memoization.

      Figure 14-19 \u00a0 Memoization recursion tree for 0-1 knapsack problem

      "},{"location":"chapter_dynamic_programming/knapsack_problem/#3-method-3-dynamic-programming","title":"3. \u00a0 Method 3: Dynamic Programming","text":"

      Dynamic programming is essentially the process of filling the \\(dp\\) table during state transitions. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
      def knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"0-1 knapsack: Dynamic programming\"\"\"\n    n = len(wgt)\n    # Initialize dp table\n    dp = [[0] * (cap + 1) for _ in range(n + 1)]\n    # State transition\n    for i in range(1, n + 1):\n        for c in range(1, cap + 1):\n            if wgt[i - 1] > c:\n                # If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c]\n            else:\n                # The larger value between not selecting and selecting item i\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\n    return dp[n][cap]\n
      knapsack.cpp
      /* 0-1 knapsack: Dynamic programming */\nint knapsackDP(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // Initialize dp table\n    vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
      knapsack.java
      /* 0-1 knapsack: Dynamic programming */\nint knapsackDP(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // Initialize dp table\n    int[][] dp = new int[n + 1][cap + 1];\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = Math.max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
      knapsack.cs
      /* 0-1 knapsack: Dynamic programming */\nint KnapsackDP(int[] weight, int[] val, int cap) {\n    int n = weight.Length;\n    // Initialize dp table\n    int[,] dp = new int[n + 1, cap + 1];\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (weight[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i, c] = dp[i - 1, c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i, c] = Math.Max(dp[i - 1, c - weight[i - 1]] + val[i - 1], dp[i - 1, c]);\n            }\n        }\n    }\n    return dp[n, cap];\n}\n
      knapsack.go
      /* 0-1 knapsack: Dynamic programming */\nfunc knapsackDP(wgt, val []int, cap int) int {\n    n := len(wgt)\n    // Initialize dp table\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, cap+1)\n    }\n    // State transition\n    for i := 1; i <= n; i++ {\n        for c := 1; c <= cap; c++ {\n            if wgt[i-1] > c {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i-1][c]\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i-1][c-wgt[i-1]]+val[i-1])))\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
      knapsack.swift
      /* 0-1 knapsack: Dynamic programming */\nfunc knapsackDP(wgt: [Int], val: [Int], cap: Int) -> Int {\n    let n = wgt.count\n    // Initialize dp table\n    var dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)\n    // State transition\n    for i in 1 ... n {\n        for c in 1 ... cap {\n            if wgt[i - 1] > c {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c]\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
      knapsack.js
      /* 0-1 knapsack: Dynamic programming */\nfunction knapsackDP(wgt, val, cap) {\n    const n = wgt.length;\n    // Initialize dp table\n    const dp = Array(n + 1)\n        .fill(0)\n        .map(() => Array(cap + 1).fill(0));\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = Math.max(\n                    dp[i - 1][c],\n                    dp[i - 1][c - wgt[i - 1]] + val[i - 1]\n                );\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
      knapsack.ts
      /* 0-1 knapsack: Dynamic programming */\nfunction knapsackDP(\n    wgt: Array<number>,\n    val: Array<number>,\n    cap: number\n): number {\n    const n = wgt.length;\n    // Initialize dp table\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: cap + 1 }, () => 0)\n    );\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = Math.max(\n                    dp[i - 1][c],\n                    dp[i - 1][c - wgt[i - 1]] + val[i - 1]\n                );\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
      knapsack.dart
      /* 0-1 knapsack: Dynamic programming */\nint knapsackDP(List<int> wgt, List<int> val, int cap) {\n  int n = wgt.length;\n  // Initialize dp table\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(cap + 1, 0));\n  // State transition\n  for (int i = 1; i <= n; i++) {\n    for (int c = 1; c <= cap; c++) {\n      if (wgt[i - 1] > c) {\n        // If exceeds knapsack capacity, don't select item i\n        dp[i][c] = dp[i - 1][c];\n      } else {\n        // The larger value between not selecting and selecting item i\n        dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n      }\n    }\n  }\n  return dp[n][cap];\n}\n
      knapsack.rs
      /* 0-1 knapsack: Dynamic programming */\nfn knapsack_dp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n    let n = wgt.len();\n    // Initialize dp table\n    let mut dp = vec![vec![0; cap + 1]; n + 1];\n    // State transition\n    for i in 1..=n {\n        for c in 1..=cap {\n            if wgt[i - 1] > c as i32 {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = std::cmp::max(\n                    dp[i - 1][c],\n                    dp[i - 1][c - wgt[i - 1] as usize] + val[i - 1],\n                );\n            }\n        }\n    }\n    dp[n][cap]\n}\n
      knapsack.c
      /* 0-1 knapsack: Dynamic programming */\nint knapsackDP(int wgt[], int val[], int cap, int wgtSize) {\n    int n = wgtSize;\n    // Initialize dp table\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(cap + 1, sizeof(int));\n    }\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = myMax(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    int res = dp[n][cap];\n    // Free memory\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    return res;\n}\n
      knapsack.kt
      /* 0-1 knapsack: Dynamic programming */\nfun knapsackDP(wgt: IntArray, _val: IntArray, cap: Int): Int {\n    val n = wgt.size\n    // Initialize dp table\n    val dp = Array(n + 1) { IntArray(cap + 1) }\n    // State transition\n    for (i in 1..n) {\n        for (c in 1..cap) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c]\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + _val[i - 1])\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
      knapsack.rb
      ### 0-1 knapsack: dynamic programming ###\ndef knapsack_dp(wgt, val, cap)\n  n = wgt.length\n  # Initialize dp table\n  dp = Array.new(n + 1) { Array.new(cap + 1, 0) }\n  # State transition\n  for i in 1...(n + 1)\n    for c in 1...(cap + 1)\n      if wgt[i - 1] > c\n        # If exceeds knapsack capacity, don't select item i\n        dp[i][c] = dp[i - 1][c]\n      else\n        # The larger value between not selecting and selecting item i\n        dp[i][c] = [dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[n][cap]\nend\n

      As shown in Figure 14-20, both time complexity and space complexity are determined by the size of the array dp, which is \\(O(n \\times cap)\\).

      <1><2><3><4><5><6><7><8><9><10><11><12><13><14>

      Figure 14-20 \u00a0 Dynamic programming process for 0-1 knapsack problem

      "},{"location":"chapter_dynamic_programming/knapsack_problem/#4-space-optimization","title":"4. \u00a0 Space Optimization","text":"

      Since each state is only related to the state in the row above it, we can use two arrays rolling forward to reduce the space complexity from \\(O(n^2)\\) to \\(O(n)\\).

      Further thinking, can we achieve space optimization using just one array? Observing, we can see that each state is transferred from the cell directly above or the cell in the upper-left. If there is only one array, when we start traversing row \\(i\\), that array still stores the state of row \\(i-1\\).

      • If using forward traversal, then when traversing to \\(dp[i, j]\\), the values in the upper-left \\(dp[i-1, 1]\\) ~ \\(dp[i-1, j-1]\\) may have already been overwritten, thus preventing correct state transition.
      • If using reverse traversal, there will be no overwriting issue, and state transition can proceed correctly.

      Figure 14-21 shows the transition process from row \\(i = 1\\) to row \\(i = 2\\) using a single array. Please consider the difference between forward and reverse traversal.

      <1><2><3><4><5><6>

      Figure 14-21 \u00a0 Space-optimized dynamic programming process for 0-1 knapsack

      In the code implementation, we simply need to delete the first dimension \\(i\\) of the array dp and change the inner loop to reverse traversal:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
      def knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"0-1 knapsack: Space-optimized dynamic programming\"\"\"\n    n = len(wgt)\n    # Initialize dp table\n    dp = [0] * (cap + 1)\n    # State transition\n    for i in range(1, n + 1):\n        # Traverse in reverse order\n        for c in range(cap, 0, -1):\n            if wgt[i - 1] > c:\n                # If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c]\n            else:\n                # The larger value between not selecting and selecting item i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n    return dp[cap]\n
      knapsack.cpp
      /* 0-1 knapsack: Space-optimized dynamic programming */\nint knapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // Initialize dp table\n    vector<int> dp(cap + 1, 0);\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        // Traverse in reverse order\n        for (int c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // The larger value between not selecting and selecting item i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
      knapsack.java
      /* 0-1 knapsack: Space-optimized dynamic programming */\nint knapsackDPComp(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // Initialize dp table\n    int[] dp = new int[cap + 1];\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        // Traverse in reverse order\n        for (int c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // The larger value between not selecting and selecting item i\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
      knapsack.cs
      /* 0-1 knapsack: Space-optimized dynamic programming */\nint KnapsackDPComp(int[] weight, int[] val, int cap) {\n    int n = weight.Length;\n    // Initialize dp table\n    int[] dp = new int[cap + 1];\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        // Traverse in reverse order\n        for (int c = cap; c > 0; c--) {\n            if (weight[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[c] = Math.Max(dp[c], dp[c - weight[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
      knapsack.go
      /* 0-1 knapsack: Space-optimized dynamic programming */\nfunc knapsackDPComp(wgt, val []int, cap int) int {\n    n := len(wgt)\n    // Initialize dp table\n    dp := make([]int, cap+1)\n    // State transition\n    for i := 1; i <= n; i++ {\n        // Traverse in reverse order\n        for c := cap; c >= 1; c-- {\n            if wgt[i-1] <= c {\n                // The larger value between not selecting and selecting item i\n                dp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))\n            }\n        }\n    }\n    return dp[cap]\n}\n
      knapsack.swift
      /* 0-1 knapsack: Space-optimized dynamic programming */\nfunc knapsackDPComp(wgt: [Int], val: [Int], cap: Int) -> Int {\n    let n = wgt.count\n    // Initialize dp table\n    var dp = Array(repeating: 0, count: cap + 1)\n    // State transition\n    for i in 1 ... n {\n        // Traverse in reverse order\n        for c in (1 ... cap).reversed() {\n            if wgt[i - 1] <= c {\n                // The larger value between not selecting and selecting item i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n            }\n        }\n    }\n    return dp[cap]\n}\n
      knapsack.js
      /* 0-1 knapsack: Space-optimized dynamic programming */\nfunction knapsackDPComp(wgt, val, cap) {\n    const n = wgt.length;\n    // Initialize dp table\n    const dp = Array(cap + 1).fill(0);\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        // Traverse in reverse order\n        for (let c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // The larger value between not selecting and selecting item i\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
      knapsack.ts
      /* 0-1 knapsack: Space-optimized dynamic programming */\nfunction knapsackDPComp(\n    wgt: Array<number>,\n    val: Array<number>,\n    cap: number\n): number {\n    const n = wgt.length;\n    // Initialize dp table\n    const dp = Array(cap + 1).fill(0);\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        // Traverse in reverse order\n        for (let c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // The larger value between not selecting and selecting item i\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
      knapsack.dart
      /* 0-1 knapsack: Space-optimized dynamic programming */\nint knapsackDPComp(List<int> wgt, List<int> val, int cap) {\n  int n = wgt.length;\n  // Initialize dp table\n  List<int> dp = List.filled(cap + 1, 0);\n  // State transition\n  for (int i = 1; i <= n; i++) {\n    // Traverse in reverse order\n    for (int c = cap; c >= 1; c--) {\n      if (wgt[i - 1] <= c) {\n        // The larger value between not selecting and selecting item i\n        dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n      }\n    }\n  }\n  return dp[cap];\n}\n
      knapsack.rs
      /* 0-1 knapsack: Space-optimized dynamic programming */\nfn knapsack_dp_comp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n    let n = wgt.len();\n    // Initialize dp table\n    let mut dp = vec![0; cap + 1];\n    // State transition\n    for i in 1..=n {\n        // Traverse in reverse order\n        for c in (1..=cap).rev() {\n            if wgt[i - 1] <= c as i32 {\n                // The larger value between not selecting and selecting item i\n                dp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);\n            }\n        }\n    }\n    dp[cap]\n}\n
      knapsack.c
      /* 0-1 knapsack: Space-optimized dynamic programming */\nint knapsackDPComp(int wgt[], int val[], int cap, int wgtSize) {\n    int n = wgtSize;\n    // Initialize dp table\n    int *dp = calloc(cap + 1, sizeof(int));\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        // Traverse in reverse order\n        for (int c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // The larger value between not selecting and selecting item i\n                dp[c] = myMax(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    int res = dp[cap];\n    // Free memory\n    free(dp);\n    return res;\n}\n
      knapsack.kt
      /* 0-1 knapsack: Space-optimized dynamic programming */\nfun knapsackDPComp(wgt: IntArray, _val: IntArray, cap: Int): Int {\n    val n = wgt.size\n    // Initialize dp table\n    val dp = IntArray(cap + 1)\n    // State transition\n    for (i in 1..n) {\n        // Traverse in reverse order\n        for (c in cap downTo 1) {\n            if (wgt[i - 1] <= c) {\n                // The larger value between not selecting and selecting item i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])\n            }\n        }\n    }\n    return dp[cap]\n}\n
      knapsack.rb
      ### 0-1 knapsack: space-optimized DP ###\ndef knapsack_dp_comp(wgt, val, cap)\n  n = wgt.length\n  # Initialize dp table\n  dp = Array.new(cap + 1, 0)\n  # State transition\n  for i in 1...(n + 1)\n    # Traverse in reverse order\n    for c in cap.downto(1)\n      if wgt[i - 1] > c\n        # If exceeds knapsack capacity, don't select item i\n        dp[c] = dp[c]\n      else\n        # The larger value between not selecting and selecting item i\n        dp[c] = [dp[c], dp[c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[cap]\nend\n
      "},{"location":"chapter_dynamic_programming/summary/","title":"14.7 \u00a0 Summary","text":""},{"location":"chapter_dynamic_programming/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • Dynamic programming decomposes problems and avoids redundant computation by storing the solutions to subproblems, thereby significantly improving computational efficiency.
      • Without considering time constraints, all dynamic programming problems can be solved using backtracking (brute force search), but the recursion tree contains a large number of overlapping subproblems, resulting in extremely low efficiency. By introducing a memo list, we can store the solutions to all computed subproblems, ensuring that overlapping subproblems are only computed once.
      • Memoization is a top-down recursive solution, while the corresponding dynamic programming is a bottom-up iterative solution, similar to \"filling in a table\". Since the current state only depends on certain local states, we can eliminate one dimension of the \\(dp\\) table to reduce space complexity.
      • Subproblem decomposition is a general algorithmic approach, with different properties in divide and conquer, dynamic programming, and backtracking.
      • Dynamic programming problems have three major characteristics: overlapping subproblems, optimal substructure, and no aftereffects.
      • If the optimal solution to the original problem can be constructed from the optimal solutions to the subproblems, then it has optimal substructure.
      • No aftereffects means that for a given state, its future development is only related to that state and has nothing to do with all past states. Many combinatorial optimization problems do not have no aftereffects and cannot be quickly solved using dynamic programming.

      Knapsack problem

      • The knapsack problem is one of the most typical dynamic programming problems, with variants such as the 0-1 knapsack, unbounded knapsack, and multiple knapsack.
      • The state definition for the 0-1 knapsack is the maximum value among the first \\(i\\) items in a knapsack of capacity \\(c\\). Based on the two decisions of not putting an item in the knapsack and putting it in, the optimal substructure can be identified and the state transition equation constructed. In space optimization, since each state depends on the state directly above and to the upper-left, the list needs to be traversed in reverse order to avoid overwriting the upper-left state.
      • The unbounded knapsack problem has no limit on the selection quantity of each type of item, so the state transition for choosing to put in an item differs from the 0-1 knapsack problem. Since the state depends on the state directly above and directly to the left, space optimization should use forward traversal.
      • The coin change problem is a variant of the unbounded knapsack problem. It changes from seeking the \"maximum\" value to seeking the \"minimum\" number of coins, so \\(\\max()\\) in the state transition equation should be changed to \\(\\min()\\). It changes from seeking \"not exceeding\" the knapsack capacity to seeking \"exactly\" making up the target amount, so \\(amt + 1\\) is used to represent the invalid solution of \"unable to make up the target amount\".
      • Coin change problem II changes from seeking the \"minimum number of coins\" to seeking the \"number of coin combinations\", so the state transition equation correspondingly changes from \\(\\min()\\) to a summation operator.

      Edit distance problem

      • Edit distance (Levenshtein distance) is used to measure the similarity between two strings, defined as the minimum number of edit steps from one string to another, with edit operations including insert, delete, and replace.
      • The state definition for the edit distance problem is the minimum number of edit steps required to change the first \\(i\\) characters of \\(s\\) into the first \\(j\\) characters of \\(t\\). When \\(s[i] \\ne t[j]\\), there are three decisions: insert, delete, replace, each with corresponding remaining subproblems. From this, the optimal substructure can be identified and the state transition equation constructed. When \\(s[i] = t[j]\\), no edit is required for the current character.
      • In edit distance, the state depends on the state directly above, directly to the left, and to the upper-left, so after space optimization, neither forward nor reverse traversal can correctly perform state transitions. For this reason, we use a variable to temporarily store the upper-left state, thus transforming to a situation equivalent to the unbounded knapsack problem, allowing for forward traversal after space optimization.
      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/","title":"14.5 \u00a0 Unbounded Knapsack Problem","text":"

      In this section, we first solve another common knapsack problem: the unbounded knapsack, and then explore a special case of it: the coin change problem.

      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1451-unbounded-knapsack-problem","title":"14.5.1 \u00a0 Unbounded Knapsack Problem","text":"

      Question

      Given \\(n\\) items, where the weight of the \\(i\\)-th item is \\(wgt[i-1]\\) and its value is \\(val[i-1]\\), and a knapsack with capacity \\(cap\\). Each item can be selected multiple times. What is the maximum value that can be placed in the knapsack within the capacity limit? An example is shown in Figure 14-22.

      Figure 14-22 \u00a0 Example data for unbounded knapsack problem

      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1-dynamic-programming-approach","title":"1. \u00a0 Dynamic Programming Approach","text":"

      The unbounded knapsack problem is very similar to the 0-1 knapsack problem, differing only in that there is no limit on the number of times an item can be selected.

      • In the 0-1 knapsack problem, there is only one of each type of item, so after placing item \\(i\\) in the knapsack, we can only choose from the first \\(i-1\\) items.
      • In the unbounded knapsack problem, the quantity of each type of item is unlimited, so after placing item \\(i\\) in the knapsack, we can still choose from the first \\(i\\) items.

      Under the rules of the unbounded knapsack problem, the changes in state \\([i, c]\\) are divided into two cases.

      • Not putting item \\(i\\): Same as the 0-1 knapsack problem, transfer to \\([i-1, c]\\).
      • Putting item \\(i\\): Different from the 0-1 knapsack problem, transfer to \\([i, c-wgt[i-1]]\\).

      Thus, the state transition equation becomes:

      \\[ dp[i, c] = \\max(dp[i-1, c], dp[i, c - wgt[i-1]] + val[i-1]) \\]"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2-code-implementation","title":"2. \u00a0 Code Implementation","text":"

      Comparing the code for the two problems, there is one change in state transition from \\(i-1\\) to \\(i\\), with everything else identical:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby unbounded_knapsack.py
      def unbounded_knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"Unbounded knapsack: Dynamic programming\"\"\"\n    n = len(wgt)\n    # Initialize dp table\n    dp = [[0] * (cap + 1) for _ in range(n + 1)]\n    # State transition\n    for i in range(1, n + 1):\n        for c in range(1, cap + 1):\n            if wgt[i - 1] > c:\n                # If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c]\n            else:\n                # The larger value between not selecting and selecting item i\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\n    return dp[n][cap]\n
      unbounded_knapsack.cpp
      /* Unbounded knapsack: Dynamic programming */\nint unboundedKnapsackDP(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // Initialize dp table\n    vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
      unbounded_knapsack.java
      /* Unbounded knapsack: Dynamic programming */\nint unboundedKnapsackDP(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // Initialize dp table\n    int[][] dp = new int[n + 1][cap + 1];\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = Math.max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
      unbounded_knapsack.cs
      /* Unbounded knapsack: Dynamic programming */\nint UnboundedKnapsackDP(int[] wgt, int[] val, int cap) {\n    int n = wgt.Length;\n    // Initialize dp table\n    int[,] dp = new int[n + 1, cap + 1];\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i, c] = dp[i - 1, c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i, c] = Math.Max(dp[i - 1, c], dp[i, c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n, cap];\n}\n
      unbounded_knapsack.go
      /* Unbounded knapsack: Dynamic programming */\nfunc unboundedKnapsackDP(wgt, val []int, cap int) int {\n    n := len(wgt)\n    // Initialize dp table\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, cap+1)\n    }\n    // State transition\n    for i := 1; i <= n; i++ {\n        for c := 1; c <= cap; c++ {\n            if wgt[i-1] > c {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i-1][c]\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i][c-wgt[i-1]]+val[i-1])))\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
      unbounded_knapsack.swift
      /* Unbounded knapsack: Dynamic programming */\nfunc unboundedKnapsackDP(wgt: [Int], val: [Int], cap: Int) -> Int {\n    let n = wgt.count\n    // Initialize dp table\n    var dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)\n    // State transition\n    for i in 1 ... n {\n        for c in 1 ... cap {\n            if wgt[i - 1] > c {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c]\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
      unbounded_knapsack.js
      /* Unbounded knapsack: Dynamic programming */\nfunction unboundedKnapsackDP(wgt, val, cap) {\n    const n = wgt.length;\n    // Initialize dp table\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: cap + 1 }, () => 0)\n    );\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = Math.max(\n                    dp[i - 1][c],\n                    dp[i][c - wgt[i - 1]] + val[i - 1]\n                );\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
      unbounded_knapsack.ts
      /* Unbounded knapsack: Dynamic programming */\nfunction unboundedKnapsackDP(\n    wgt: Array<number>,\n    val: Array<number>,\n    cap: number\n): number {\n    const n = wgt.length;\n    // Initialize dp table\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: cap + 1 }, () => 0)\n    );\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = Math.max(\n                    dp[i - 1][c],\n                    dp[i][c - wgt[i - 1]] + val[i - 1]\n                );\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
      unbounded_knapsack.dart
      /* Unbounded knapsack: Dynamic programming */\nint unboundedKnapsackDP(List<int> wgt, List<int> val, int cap) {\n  int n = wgt.length;\n  // Initialize dp table\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(cap + 1, 0));\n  // State transition\n  for (int i = 1; i <= n; i++) {\n    for (int c = 1; c <= cap; c++) {\n      if (wgt[i - 1] > c) {\n        // If exceeds knapsack capacity, don't select item i\n        dp[i][c] = dp[i - 1][c];\n      } else {\n        // The larger value between not selecting and selecting item i\n        dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n      }\n    }\n  }\n  return dp[n][cap];\n}\n
      unbounded_knapsack.rs
      /* Unbounded knapsack: Dynamic programming */\nfn unbounded_knapsack_dp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n    let n = wgt.len();\n    // Initialize dp table\n    let mut dp = vec![vec![0; cap + 1]; n + 1];\n    // State transition\n    for i in 1..=n {\n        for c in 1..=cap {\n            if wgt[i - 1] > c as i32 {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = std::cmp::max(dp[i - 1][c], dp[i][c - wgt[i - 1] as usize] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
      unbounded_knapsack.c
      /* Unbounded knapsack: Dynamic programming */\nint unboundedKnapsackDP(int wgt[], int val[], int cap, int wgtSize) {\n    int n = wgtSize;\n    // Initialize dp table\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(cap + 1, sizeof(int));\n    }\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = myMax(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    int res = dp[n][cap];\n    // Free memory\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    return res;\n}\n
      unbounded_knapsack.kt
      /* Unbounded knapsack: Dynamic programming */\nfun unboundedKnapsackDP(wgt: IntArray, _val: IntArray, cap: Int): Int {\n    val n = wgt.size\n    // Initialize dp table\n    val dp = Array(n + 1) { IntArray(cap + 1) }\n    // State transition\n    for (i in 1..n) {\n        for (c in 1..cap) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[i][c] = dp[i - 1][c]\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + _val[i - 1])\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
      unbounded_knapsack.rb
      ### Unbounded knapsack: dynamic programming ###\ndef unbounded_knapsack_dp(wgt, val, cap)\n  n = wgt.length\n  # Initialize dp table\n  dp = Array.new(n + 1) { Array.new(cap + 1, 0) }\n  # State transition\n  for i in 1...(n + 1)\n    for c in 1...(cap + 1)\n      if wgt[i - 1] > c\n        # If exceeds knapsack capacity, don't select item i\n        dp[i][c] = dp[i - 1][c]\n      else\n        # The larger value between not selecting and selecting item i\n        dp[i][c] = [dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[n][cap]\nend\n
      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3-space-optimization","title":"3. \u00a0 Space Optimization","text":"

      Since the current state is transferred from states on the left and above, after space optimization, each row in the \\(dp\\) table should be traversed in forward order.

      This traversal order is exactly opposite to the 0-1 knapsack. Please refer to Figure 14-23 to understand the difference between the two.

      <1><2><3><4><5><6>

      Figure 14-23 \u00a0 Space-optimized dynamic programming process for unbounded knapsack problem

      The code implementation is relatively simple, just delete the first dimension of the array dp:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby unbounded_knapsack.py
      def unbounded_knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"Unbounded knapsack: Space-optimized dynamic programming\"\"\"\n    n = len(wgt)\n    # Initialize dp table\n    dp = [0] * (cap + 1)\n    # State transition\n    for i in range(1, n + 1):\n        # Traverse in forward order\n        for c in range(1, cap + 1):\n            if wgt[i - 1] > c:\n                # If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c]\n            else:\n                # The larger value between not selecting and selecting item i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n    return dp[cap]\n
      unbounded_knapsack.cpp
      /* Unbounded knapsack: Space-optimized dynamic programming */\nint unboundedKnapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // Initialize dp table\n    vector<int> dp(cap + 1, 0);\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
      unbounded_knapsack.java
      /* Unbounded knapsack: Space-optimized dynamic programming */\nint unboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // Initialize dp table\n    int[] dp = new int[cap + 1];\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
      unbounded_knapsack.cs
      /* Unbounded knapsack: Space-optimized dynamic programming */\nint UnboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\n    int n = wgt.Length;\n    // Initialize dp table\n    int[] dp = new int[cap + 1];\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[c] = Math.Max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
      unbounded_knapsack.go
      /* Unbounded knapsack: Space-optimized dynamic programming */\nfunc unboundedKnapsackDPComp(wgt, val []int, cap int) int {\n    n := len(wgt)\n    // Initialize dp table\n    dp := make([]int, cap+1)\n    // State transition\n    for i := 1; i <= n; i++ {\n        for c := 1; c <= cap; c++ {\n            if wgt[i-1] > c {\n                // If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c]\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))\n            }\n        }\n    }\n    return dp[cap]\n}\n
      unbounded_knapsack.swift
      /* Unbounded knapsack: Space-optimized dynamic programming */\nfunc unboundedKnapsackDPComp(wgt: [Int], val: [Int], cap: Int) -> Int {\n    let n = wgt.count\n    // Initialize dp table\n    var dp = Array(repeating: 0, count: cap + 1)\n    // State transition\n    for i in 1 ... n {\n        for c in 1 ... cap {\n            if wgt[i - 1] > c {\n                // If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c]\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n            }\n        }\n    }\n    return dp[cap]\n}\n
      unbounded_knapsack.js
      /* Unbounded knapsack: Space-optimized dynamic programming */\nfunction unboundedKnapsackDPComp(wgt, val, cap) {\n    const n = wgt.length;\n    // Initialize dp table\n    const dp = Array.from({ length: cap + 1 }, () => 0);\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
      unbounded_knapsack.ts
      /* Unbounded knapsack: Space-optimized dynamic programming */\nfunction unboundedKnapsackDPComp(\n    wgt: Array<number>,\n    val: Array<number>,\n    cap: number\n): number {\n    const n = wgt.length;\n    // Initialize dp table\n    const dp = Array.from({ length: cap + 1 }, () => 0);\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
      unbounded_knapsack.dart
      /* Unbounded knapsack: Space-optimized dynamic programming */\nint unboundedKnapsackDPComp(List<int> wgt, List<int> val, int cap) {\n  int n = wgt.length;\n  // Initialize dp table\n  List<int> dp = List.filled(cap + 1, 0);\n  // State transition\n  for (int i = 1; i <= n; i++) {\n    for (int c = 1; c <= cap; c++) {\n      if (wgt[i - 1] > c) {\n        // If exceeds knapsack capacity, don't select item i\n        dp[c] = dp[c];\n      } else {\n        // The larger value between not selecting and selecting item i\n        dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n      }\n    }\n  }\n  return dp[cap];\n}\n
      unbounded_knapsack.rs
      /* Unbounded knapsack: Space-optimized dynamic programming */\nfn unbounded_knapsack_dp_comp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n    let n = wgt.len();\n    // Initialize dp table\n    let mut dp = vec![0; cap + 1];\n    // State transition\n    for i in 1..=n {\n        for c in 1..=cap {\n            if wgt[i - 1] > c as i32 {\n                // If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);\n            }\n        }\n    }\n    dp[cap]\n}\n
      unbounded_knapsack.c
      /* Unbounded knapsack: Space-optimized dynamic programming */\nint unboundedKnapsackDPComp(int wgt[], int val[], int cap, int wgtSize) {\n    int n = wgtSize;\n    // Initialize dp table\n    int *dp = calloc(cap + 1, sizeof(int));\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c];\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[c] = myMax(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    int res = dp[cap];\n    // Free memory\n    free(dp);\n    return res;\n}\n
      unbounded_knapsack.kt
      /* Unbounded knapsack: Space-optimized dynamic programming */\nfun unboundedKnapsackDPComp(\n    wgt: IntArray,\n    _val: IntArray,\n    cap: Int\n): Int {\n    val n = wgt.size\n    // Initialize dp table\n    val dp = IntArray(cap + 1)\n    // State transition\n    for (i in 1..n) {\n        for (c in 1..cap) {\n            if (wgt[i - 1] > c) {\n                // If exceeds knapsack capacity, don't select item i\n                dp[c] = dp[c]\n            } else {\n                // The larger value between not selecting and selecting item i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])\n            }\n        }\n    }\n    return dp[cap]\n}\n
      unbounded_knapsack.rb
      ### Unbounded knapsack: space-optimized DP ###\ndef unbounded_knapsack_dp_comp(wgt, val, cap)\n  n = wgt.length\n  # Initialize dp table\n  dp = Array.new(cap + 1, 0)\n  # State transition\n  for i in 1...(n + 1)\n    # Traverse in forward order\n    for c in 1...(cap + 1)\n      if wgt[i -1] > c\n        # If exceeds knapsack capacity, don't select item i\n        dp[c] = dp[c]\n      else\n        # The larger value between not selecting and selecting item i\n        dp[c] = [dp[c], dp[c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[cap]\nend\n
      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1452-coin-change-problem","title":"14.5.2 \u00a0 Coin Change Problem","text":"

      The knapsack problem represents a large class of dynamic programming problems and has many variants, such as the coin change problem.

      Question

      Given \\(n\\) types of coins, where the denomination of the \\(i\\)-th type of coin is \\(coins[i - 1]\\), and the target amount is \\(amt\\). Each type of coin can be selected multiple times. What is the minimum number of coins needed to make up the target amount? If it is impossible to make up the target amount, return \\(-1\\). An example is shown in Figure 14-24.

      Figure 14-24 \u00a0 Example data for coin change problem

      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1-dynamic-programming-approach_1","title":"1. \u00a0 Dynamic Programming Approach","text":"

      The coin change problem can be viewed as a special case of the unbounded knapsack problem, with the following connections and differences.

      • The two problems can be converted to each other: \"item\" corresponds to \"coin\", \"item weight\" corresponds to \"coin denomination\", and \"knapsack capacity\" corresponds to \"target amount\".
      • The optimization goals are opposite: the unbounded knapsack problem aims to maximize item value, while the coin change problem aims to minimize the number of coins.
      • The unbounded knapsack problem seeks solutions \"not exceeding\" the knapsack capacity, while the coin change problem seeks solutions that \"exactly\" make up the target amount.

      Step 1: Think about the decisions in each round, define the state, and thus obtain the \\(dp\\) table

      State \\([i, a]\\) corresponds to the subproblem: the minimum number of coins among the first \\(i\\) types of coins that can make up amount \\(a\\), denoted as \\(dp[i, a]\\).

      The two-dimensional \\(dp\\) table has size \\((n+1) \\times (amt+1)\\).

      Step 2: Identify the optimal substructure, and then derive the state transition equation

      This problem differs from the unbounded knapsack problem in the following two aspects regarding the state transition equation.

      • This problem seeks the minimum value, so the operator \\(\\max()\\) needs to be changed to \\(\\min()\\).
      • The optimization target is the number of coins rather than item value, so when a coin is selected, simply execute \\(+1\\).
      \\[ dp[i, a] = \\min(dp[i-1, a], dp[i, a - coins[i-1]] + 1) \\]

      Step 3: Determine boundary conditions and state transition order

      When the target amount is \\(0\\), the minimum number of coins needed to make it up is \\(0\\), so all \\(dp[i, 0]\\) in the first column equal \\(0\\).

      When there are no coins, it is impossible to make up any amount \\(> 0\\), which is an invalid solution. To enable the \\(\\min()\\) function in the state transition equation to identify and filter out invalid solutions, we consider using \\(+ \\infty\\) to represent them, i.e., set all \\(dp[0, a]\\) in the first row to \\(+ \\infty\\).

      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2-code-implementation_1","title":"2. \u00a0 Code Implementation","text":"

      Most programming languages do not provide a \\(+ \\infty\\) variable, and can only use the maximum value of integer type int as a substitute. However, this can lead to large number overflow: the \\(+ 1\\) operation in the state transition equation may cause overflow.

      For this reason, we use the number \\(amt + 1\\) to represent invalid solutions, because the maximum number of coins needed to make up \\(amt\\) is at most \\(amt\\). Before returning, check whether \\(dp[n, amt]\\) equals \\(amt + 1\\); if so, return \\(-1\\), indicating that the target amount cannot be made up. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change.py
      def coin_change_dp(coins: list[int], amt: int) -> int:\n    \"\"\"Coin change: Dynamic programming\"\"\"\n    n = len(coins)\n    MAX = amt + 1\n    # Initialize dp table\n    dp = [[0] * (amt + 1) for _ in range(n + 1)]\n    # State transition: first row and first column\n    for a in range(1, amt + 1):\n        dp[0][a] = MAX\n    # State transition: rest of the rows and columns\n    for i in range(1, n + 1):\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a]\n            else:\n                # The smaller value between not selecting and selecting coin i\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n    return dp[n][amt] if dp[n][amt] != MAX else -1\n
      coin_change.cpp
      /* Coin change: Dynamic programming */\nint coinChangeDP(vector<int> &coins, int amt) {\n    int n = coins.size();\n    int MAX = amt + 1;\n    // Initialize dp table\n    vector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n    // State transition: first row and first column\n    for (int a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
      coin_change.java
      /* Coin change: Dynamic programming */\nint coinChangeDP(int[] coins, int amt) {\n    int n = coins.length;\n    int MAX = amt + 1;\n    // Initialize dp table\n    int[][] dp = new int[n + 1][amt + 1];\n    // State transition: first row and first column\n    for (int a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
      coin_change.cs
      /* Coin change: Dynamic programming */\nint CoinChangeDP(int[] coins, int amt) {\n    int n = coins.Length;\n    int MAX = amt + 1;\n    // Initialize dp table\n    int[,] dp = new int[n + 1, amt + 1];\n    // State transition: first row and first column\n    for (int a = 1; a <= amt; a++) {\n        dp[0, a] = MAX;\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i, a] = dp[i - 1, a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[i, a] = Math.Min(dp[i - 1, a], dp[i, a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n, amt] != MAX ? dp[n, amt] : -1;\n}\n
      coin_change.go
      /* Coin change: Dynamic programming */\nfunc coinChangeDP(coins []int, amt int) int {\n    n := len(coins)\n    max := amt + 1\n    // Initialize dp table\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, amt+1)\n    }\n    // State transition: first row and first column\n    for a := 1; a <= amt; a++ {\n        dp[0][a] = max\n    }\n    // State transition: rest of the rows and columns\n    for i := 1; i <= n; i++ {\n        for a := 1; a <= amt; a++ {\n            if coins[i-1] > a {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i-1][a]\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[i][a] = int(math.Min(float64(dp[i-1][a]), float64(dp[i][a-coins[i-1]]+1)))\n            }\n        }\n    }\n    if dp[n][amt] != max {\n        return dp[n][amt]\n    }\n    return -1\n}\n
      coin_change.swift
      /* Coin change: Dynamic programming */\nfunc coinChangeDP(coins: [Int], amt: Int) -> Int {\n    let n = coins.count\n    let MAX = amt + 1\n    // Initialize dp table\n    var dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)\n    // State transition: first row and first column\n    for a in 1 ... amt {\n        dp[0][a] = MAX\n    }\n    // State transition: rest of the rows and columns\n    for i in 1 ... n {\n        for a in 1 ... amt {\n            if coins[i - 1] > a {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a]\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n            }\n        }\n    }\n    return dp[n][amt] != MAX ? dp[n][amt] : -1\n}\n
      coin_change.js
      /* Coin change: Dynamic programming */\nfunction coinChangeDP(coins, amt) {\n    const n = coins.length;\n    const MAX = amt + 1;\n    // Initialize dp table\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: amt + 1 }, () => 0)\n    );\n    // State transition: first row and first column\n    for (let a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // State transition: rest of the rows and columns\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] !== MAX ? dp[n][amt] : -1;\n}\n
      coin_change.ts
      /* Coin change: Dynamic programming */\nfunction coinChangeDP(coins: Array<number>, amt: number): number {\n    const n = coins.length;\n    const MAX = amt + 1;\n    // Initialize dp table\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: amt + 1 }, () => 0)\n    );\n    // State transition: first row and first column\n    for (let a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // State transition: rest of the rows and columns\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] !== MAX ? dp[n][amt] : -1;\n}\n
      coin_change.dart
      /* Coin change: Dynamic programming */\nint coinChangeDP(List<int> coins, int amt) {\n  int n = coins.length;\n  int MAX = amt + 1;\n  // Initialize dp table\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(amt + 1, 0));\n  // State transition: first row and first column\n  for (int a = 1; a <= amt; a++) {\n    dp[0][a] = MAX;\n  }\n  // State transition: rest of the rows and columns\n  for (int i = 1; i <= n; i++) {\n    for (int a = 1; a <= amt; a++) {\n      if (coins[i - 1] > a) {\n        // If exceeds target amount, don't select coin i\n        dp[i][a] = dp[i - 1][a];\n      } else {\n        // The smaller value between not selecting and selecting coin i\n        dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n      }\n    }\n  }\n  return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
      coin_change.rs
      /* Coin change: Dynamic programming */\nfn coin_change_dp(coins: &[i32], amt: usize) -> i32 {\n    let n = coins.len();\n    let max = amt + 1;\n    // Initialize dp table\n    let mut dp = vec![vec![0; amt + 1]; n + 1];\n    // State transition: first row and first column\n    for a in 1..=amt {\n        dp[0][a] = max;\n    }\n    // State transition: rest of the rows and columns\n    for i in 1..=n {\n        for a in 1..=amt {\n            if coins[i - 1] > a as i32 {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[i][a] = std::cmp::min(dp[i - 1][a], dp[i][a - coins[i - 1] as usize] + 1);\n            }\n        }\n    }\n    if dp[n][amt] != max {\n        return dp[n][amt] as i32;\n    } else {\n        -1\n    }\n}\n
      coin_change.c
      /* Coin change: Dynamic programming */\nint coinChangeDP(int coins[], int amt, int coinsSize) {\n    int n = coinsSize;\n    int MAX = amt + 1;\n    // Initialize dp table\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(amt + 1, sizeof(int));\n    }\n    // State transition: first row and first column\n    for (int a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // State transition: rest of the rows and columns\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[i][a] = myMin(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    int res = dp[n][amt] != MAX ? dp[n][amt] : -1;\n    // Free memory\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    free(dp);\n    return res;\n}\n
      coin_change.kt
      /* Coin change: Dynamic programming */\nfun coinChangeDP(coins: IntArray, amt: Int): Int {\n    val n = coins.size\n    val MAX = amt + 1\n    // Initialize dp table\n    val dp = Array(n + 1) { IntArray(amt + 1) }\n    // State transition: first row and first column\n    for (a in 1..amt) {\n        dp[0][a] = MAX\n    }\n    // State transition: rest of the rows and columns\n    for (i in 1..n) {\n        for (a in 1..amt) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a]\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n            }\n        }\n    }\n    return if (dp[n][amt] != MAX) dp[n][amt] else -1\n}\n
      coin_change.rb
      ### Coin change: dynamic programming ###\ndef coin_change_dp(coins, amt)\n  n = coins.length\n  _MAX = amt + 1\n  # Initialize dp table\n  dp = Array.new(n + 1) { Array.new(amt + 1, 0) }\n  # State transition: first row and first column\n  (1...(amt + 1)).each { |a| dp[0][a] = _MAX }\n  # State transition: rest of the rows and columns\n  for i in 1...(n + 1)\n    for a in 1...(amt + 1)\n      if coins[i - 1] > a\n        # If exceeds target amount, don't select coin i\n        dp[i][a] = dp[i - 1][a]\n      else\n        # The smaller value between not selecting and selecting coin i\n        dp[i][a] = [dp[i - 1][a], dp[i][a - coins[i - 1]] + 1].min\n      end\n    end\n  end\n  dp[n][amt] != _MAX ? dp[n][amt] : -1\nend\n

      Figure 14-25 shows the dynamic programming process for coin change, which is very similar to the unbounded knapsack problem.

      <1><2><3><4><5><6><7><8><9><10><11><12><13><14><15>

      Figure 14-25 \u00a0 Dynamic programming process for coin change problem

      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3-space-optimization_1","title":"3. \u00a0 Space Optimization","text":"

      The space optimization for the coin change problem is handled in the same way as the unbounded knapsack problem:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change.py
      def coin_change_dp_comp(coins: list[int], amt: int) -> int:\n    \"\"\"Coin change: Space-optimized dynamic programming\"\"\"\n    n = len(coins)\n    MAX = amt + 1\n    # Initialize dp table\n    dp = [MAX] * (amt + 1)\n    dp[0] = 0\n    # State transition\n    for i in range(1, n + 1):\n        # Traverse in forward order\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # If exceeds target amount, don't select coin i\n                dp[a] = dp[a]\n            else:\n                # The smaller value between not selecting and selecting coin i\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n    return dp[amt] if dp[amt] != MAX else -1\n
      coin_change.cpp
      /* Coin change: Space-optimized dynamic programming */\nint coinChangeDPComp(vector<int> &coins, int amt) {\n    int n = coins.size();\n    int MAX = amt + 1;\n    // Initialize dp table\n    vector<int> dp(amt + 1, MAX);\n    dp[0] = 0;\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1;\n}\n
      coin_change.java
      /* Coin change: Space-optimized dynamic programming */\nint coinChangeDPComp(int[] coins, int amt) {\n    int n = coins.length;\n    int MAX = amt + 1;\n    // Initialize dp table\n    int[] dp = new int[amt + 1];\n    Arrays.fill(dp, MAX);\n    dp[0] = 0;\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1;\n}\n
      coin_change.cs
      /* Coin change: Space-optimized dynamic programming */\nint CoinChangeDPComp(int[] coins, int amt) {\n    int n = coins.Length;\n    int MAX = amt + 1;\n    // Initialize dp table\n    int[] dp = new int[amt + 1];\n    Array.Fill(dp, MAX);\n    dp[0] = 0;\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[a] = Math.Min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1;\n}\n
      coin_change.go
      /* Coin change: Dynamic programming */\nfunc coinChangeDPComp(coins []int, amt int) int {\n    n := len(coins)\n    max := amt + 1\n    // Initialize dp table\n    dp := make([]int, amt+1)\n    for i := 1; i <= amt; i++ {\n        dp[i] = max\n    }\n    // State transition\n    for i := 1; i <= n; i++ {\n        // Traverse in forward order\n        for a := 1; a <= amt; a++ {\n            if coins[i-1] > a {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a]\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[a] = int(math.Min(float64(dp[a]), float64(dp[a-coins[i-1]]+1)))\n            }\n        }\n    }\n    if dp[amt] != max {\n        return dp[amt]\n    }\n    return -1\n}\n
      coin_change.swift
      /* Coin change: Space-optimized dynamic programming */\nfunc coinChangeDPComp(coins: [Int], amt: Int) -> Int {\n    let n = coins.count\n    let MAX = amt + 1\n    // Initialize dp table\n    var dp = Array(repeating: MAX, count: amt + 1)\n    dp[0] = 0\n    // State transition\n    for i in 1 ... n {\n        for a in 1 ... amt {\n            if coins[i - 1] > a {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a]\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1\n}\n
      coin_change.js
      /* Coin change: Space-optimized dynamic programming */\nfunction coinChangeDPComp(coins, amt) {\n    const n = coins.length;\n    const MAX = amt + 1;\n    // Initialize dp table\n    const dp = Array.from({ length: amt + 1 }, () => MAX);\n    dp[0] = 0;\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] !== MAX ? dp[amt] : -1;\n}\n
      coin_change.ts
      /* Coin change: Space-optimized dynamic programming */\nfunction coinChangeDPComp(coins: Array<number>, amt: number): number {\n    const n = coins.length;\n    const MAX = amt + 1;\n    // Initialize dp table\n    const dp = Array.from({ length: amt + 1 }, () => MAX);\n    dp[0] = 0;\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] !== MAX ? dp[amt] : -1;\n}\n
      coin_change.dart
      /* Coin change: Space-optimized dynamic programming */\nint coinChangeDPComp(List<int> coins, int amt) {\n  int n = coins.length;\n  int MAX = amt + 1;\n  // Initialize dp table\n  List<int> dp = List.filled(amt + 1, MAX);\n  dp[0] = 0;\n  // State transition\n  for (int i = 1; i <= n; i++) {\n    for (int a = 1; a <= amt; a++) {\n      if (coins[i - 1] > a) {\n        // If exceeds target amount, don't select coin i\n        dp[a] = dp[a];\n      } else {\n        // The smaller value between not selecting and selecting coin i\n        dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n      }\n    }\n  }\n  return dp[amt] != MAX ? dp[amt] : -1;\n}\n
      coin_change.rs
      /* Coin change: Space-optimized dynamic programming */\nfn coin_change_dp_comp(coins: &[i32], amt: usize) -> i32 {\n    let n = coins.len();\n    let max = amt + 1;\n    // Initialize dp table\n    let mut dp = vec![0; amt + 1];\n    dp.fill(max);\n    dp[0] = 0;\n    // State transition\n    for i in 1..=n {\n        for a in 1..=amt {\n            if coins[i - 1] > a as i32 {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[a] = std::cmp::min(dp[a], dp[a - coins[i - 1] as usize] + 1);\n            }\n        }\n    }\n    if dp[amt] != max {\n        return dp[amt] as i32;\n    } else {\n        -1\n    }\n}\n
      coin_change.c
      /* Coin change: Space-optimized dynamic programming */\nint coinChangeDPComp(int coins[], int amt, int coinsSize) {\n    int n = coinsSize;\n    int MAX = amt + 1;\n    // Initialize dp table\n    int *dp = malloc((amt + 1) * sizeof(int));\n    for (int j = 1; j <= amt; j++) {\n        dp[j] = MAX;\n    } \n    dp[0] = 0;\n\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[a] = myMin(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    int res = dp[amt] != MAX ? dp[amt] : -1;\n    // Free memory\n    free(dp);\n    return res;\n}\n
      coin_change.kt
      /* Coin change: Space-optimized dynamic programming */\nfun coinChangeDPComp(coins: IntArray, amt: Int): Int {\n    val n = coins.size\n    val MAX = amt + 1\n    // Initialize dp table\n    val dp = IntArray(amt + 1)\n    dp.fill(MAX)\n    dp[0] = 0\n    // State transition\n    for (i in 1..n) {\n        for (a in 1..amt) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a]\n            } else {\n                // The smaller value between not selecting and selecting coin i\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n            }\n        }\n    }\n    return if (dp[amt] != MAX) dp[amt] else -1\n}\n
      coin_change.rb
      ### Coin change: space-optimized DP ###\ndef coin_change_dp_comp(coins, amt)\n  n = coins.length\n  _MAX = amt + 1\n  # Initialize dp table\n  dp = Array.new(amt + 1, _MAX)\n  dp[0] = 0\n  # State transition\n  for i in 1...(n + 1)\n    # Traverse in forward order\n    for a in 1...(amt + 1)\n      if coins[i - 1] > a\n        # If exceeds target amount, don't select coin i\n        dp[a] = dp[a]\n      else\n        # The smaller value between not selecting and selecting coin i\n        dp[a] = [dp[a], dp[a - coins[i - 1]] + 1].min\n      end\n    end\n  end\n  dp[amt] != _MAX ? dp[amt] : -1\nend\n
      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1453-coin-change-problem-ii","title":"14.5.3 \u00a0 Coin Change Problem Ii","text":"

      Question

      Given \\(n\\) types of coins, where the denomination of the \\(i\\)-th type of coin is \\(coins[i - 1]\\), and the target amount is \\(amt\\). Each type of coin can be selected multiple times. What is the number of coin combinations that can make up the target amount? An example is shown in Figure 14-26.

      Figure 14-26 \u00a0 Example data for coin change problem II

      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1-dynamic-programming-approach_2","title":"1. \u00a0 Dynamic Programming Approach","text":"

      Compared to the previous problem, this problem's goal is to find the number of combinations, so the subproblem becomes: the number of combinations among the first \\(i\\) types of coins that can make up amount \\(a\\). The \\(dp\\) table remains a two-dimensional matrix of size \\((n+1) \\times (amt + 1)\\).

      The number of combinations for the current state equals the sum of the combinations from not selecting the current coin and selecting the current coin. The state transition equation is:

      \\[ dp[i, a] = dp[i-1, a] + dp[i, a - coins[i-1]] \\]

      When the target amount is \\(0\\), no coins need to be selected to make up the target amount, so all \\(dp[i, 0]\\) in the first column should be initialized to \\(1\\). When there are no coins, it is impossible to make up any amount \\(>0\\), so all \\(dp[0, a]\\) in the first row equal \\(0\\).

      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2-code-implementation_2","title":"2. \u00a0 Code Implementation","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change_ii.py
      def coin_change_ii_dp(coins: list[int], amt: int) -> int:\n    \"\"\"Coin change II: Dynamic programming\"\"\"\n    n = len(coins)\n    # Initialize dp table\n    dp = [[0] * (amt + 1) for _ in range(n + 1)]\n    # Initialize first column\n    for i in range(n + 1):\n        dp[i][0] = 1\n    # State transition\n    for i in range(1, n + 1):\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a]\n            else:\n                # Sum of the two options: not selecting and selecting coin i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n    return dp[n][amt]\n
      coin_change_ii.cpp
      /* Coin change II: Dynamic programming */\nint coinChangeIIDP(vector<int> &coins, int amt) {\n    int n = coins.size();\n    // Initialize dp table\n    vector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n    // Initialize first column\n    for (int i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
      coin_change_ii.java
      /* Coin change II: Dynamic programming */\nint coinChangeIIDP(int[] coins, int amt) {\n    int n = coins.length;\n    // Initialize dp table\n    int[][] dp = new int[n + 1][amt + 1];\n    // Initialize first column\n    for (int i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
      coin_change_ii.cs
      /* Coin change II: Dynamic programming */\nint CoinChangeIIDP(int[] coins, int amt) {\n    int n = coins.Length;\n    // Initialize dp table\n    int[,] dp = new int[n + 1, amt + 1];\n    // Initialize first column\n    for (int i = 0; i <= n; i++) {\n        dp[i, 0] = 1;\n    }\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i, a] = dp[i - 1, a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[i, a] = dp[i - 1, a] + dp[i, a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n, amt];\n}\n
      coin_change_ii.go
      /* Coin change II: Dynamic programming */\nfunc coinChangeIIDP(coins []int, amt int) int {\n    n := len(coins)\n    // Initialize dp table\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, amt+1)\n    }\n    // Initialize first column\n    for i := 0; i <= n; i++ {\n        dp[i][0] = 1\n    }\n    // State transition: rest of the rows and columns\n    for i := 1; i <= n; i++ {\n        for a := 1; a <= amt; a++ {\n            if coins[i-1] > a {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i-1][a]\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[i][a] = dp[i-1][a] + dp[i][a-coins[i-1]]\n            }\n        }\n    }\n    return dp[n][amt]\n}\n
      coin_change_ii.swift
      /* Coin change II: Dynamic programming */\nfunc coinChangeIIDP(coins: [Int], amt: Int) -> Int {\n    let n = coins.count\n    // Initialize dp table\n    var dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)\n    // Initialize first column\n    for i in 0 ... n {\n        dp[i][0] = 1\n    }\n    // State transition\n    for i in 1 ... n {\n        for a in 1 ... amt {\n            if coins[i - 1] > a {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a]\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n            }\n        }\n    }\n    return dp[n][amt]\n}\n
      coin_change_ii.js
      /* Coin change II: Dynamic programming */\nfunction coinChangeIIDP(coins, amt) {\n    const n = coins.length;\n    // Initialize dp table\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: amt + 1 }, () => 0)\n    );\n    // Initialize first column\n    for (let i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
      coin_change_ii.ts
      /* Coin change II: Dynamic programming */\nfunction coinChangeIIDP(coins: Array<number>, amt: number): number {\n    const n = coins.length;\n    // Initialize dp table\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: amt + 1 }, () => 0)\n    );\n    // Initialize first column\n    for (let i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
      coin_change_ii.dart
      /* Coin change II: Dynamic programming */\nint coinChangeIIDP(List<int> coins, int amt) {\n  int n = coins.length;\n  // Initialize dp table\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(amt + 1, 0));\n  // Initialize first column\n  for (int i = 0; i <= n; i++) {\n    dp[i][0] = 1;\n  }\n  // State transition\n  for (int i = 1; i <= n; i++) {\n    for (int a = 1; a <= amt; a++) {\n      if (coins[i - 1] > a) {\n        // If exceeds target amount, don't select coin i\n        dp[i][a] = dp[i - 1][a];\n      } else {\n        // Sum of the two options: not selecting and selecting coin i\n        dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n      }\n    }\n  }\n  return dp[n][amt];\n}\n
      coin_change_ii.rs
      /* Coin change II: Dynamic programming */\nfn coin_change_ii_dp(coins: &[i32], amt: usize) -> i32 {\n    let n = coins.len();\n    // Initialize dp table\n    let mut dp = vec![vec![0; amt + 1]; n + 1];\n    // Initialize first column\n    for i in 0..=n {\n        dp[i][0] = 1;\n    }\n    // State transition\n    for i in 1..=n {\n        for a in 1..=amt {\n            if coins[i - 1] > a as i32 {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1] as usize];\n            }\n        }\n    }\n    dp[n][amt]\n}\n
      coin_change_ii.c
      /* Coin change II: Dynamic programming */\nint coinChangeIIDP(int coins[], int amt, int coinsSize) {\n    int n = coinsSize;\n    // Initialize dp table\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(amt + 1, sizeof(int));\n    }\n    // Initialize first column\n    for (int i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    int res = dp[n][amt];\n    // Free memory\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    free(dp);\n    return res;\n}\n
      coin_change_ii.kt
      /* Coin change II: Dynamic programming */\nfun coinChangeIIDP(coins: IntArray, amt: Int): Int {\n    val n = coins.size\n    // Initialize dp table\n    val dp = Array(n + 1) { IntArray(amt + 1) }\n    // Initialize first column\n    for (i in 0..n) {\n        dp[i][0] = 1\n    }\n    // State transition\n    for (i in 1..n) {\n        for (a in 1..amt) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[i][a] = dp[i - 1][a]\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n            }\n        }\n    }\n    return dp[n][amt]\n}\n
      coin_change_ii.rb
      ### Coin change II: dynamic programming ###\ndef coin_change_ii_dp(coins, amt)\n  n = coins.length\n  # Initialize dp table\n  dp = Array.new(n + 1) { Array.new(amt + 1, 0) }\n  # Initialize first column\n  (0...(n + 1)).each { |i| dp[i][0] = 1 }\n  # State transition\n  for i in 1...(n + 1)\n    for a in 1...(amt + 1)\n      if coins[i - 1] > a\n        # If exceeds target amount, don't select coin i\n        dp[i][a] = dp[i - 1][a]\n      else\n        # Sum of the two options: not selecting and selecting coin i\n        dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n      end\n    end\n  end\n  dp[n][amt]\nend\n
      "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3-space-optimization_2","title":"3. \u00a0 Space Optimization","text":"

      The space optimization is handled in the same way, just delete the coin dimension:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change_ii.py
      def coin_change_ii_dp_comp(coins: list[int], amt: int) -> int:\n    \"\"\"Coin change II: Space-optimized dynamic programming\"\"\"\n    n = len(coins)\n    # Initialize dp table\n    dp = [0] * (amt + 1)\n    dp[0] = 1\n    # State transition\n    for i in range(1, n + 1):\n        # Traverse in forward order\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # If exceeds target amount, don't select coin i\n                dp[a] = dp[a]\n            else:\n                # Sum of the two options: not selecting and selecting coin i\n                dp[a] = dp[a] + dp[a - coins[i - 1]]\n    return dp[amt]\n
      coin_change_ii.cpp
      /* Coin change II: Space-optimized dynamic programming */\nint coinChangeIIDPComp(vector<int> &coins, int amt) {\n    int n = coins.size();\n    // Initialize dp table\n    vector<int> dp(amt + 1, 0);\n    dp[0] = 1;\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
      coin_change_ii.java
      /* Coin change II: Space-optimized dynamic programming */\nint coinChangeIIDPComp(int[] coins, int amt) {\n    int n = coins.length;\n    // Initialize dp table\n    int[] dp = new int[amt + 1];\n    dp[0] = 1;\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
      coin_change_ii.cs
      /* Coin change II: Space-optimized dynamic programming */\nint CoinChangeIIDPComp(int[] coins, int amt) {\n    int n = coins.Length;\n    // Initialize dp table\n    int[] dp = new int[amt + 1];\n    dp[0] = 1;\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
      coin_change_ii.go
      /* Coin change II: Space-optimized dynamic programming */\nfunc coinChangeIIDPComp(coins []int, amt int) int {\n    n := len(coins)\n    // Initialize dp table\n    dp := make([]int, amt+1)\n    dp[0] = 1\n    // State transition\n    for i := 1; i <= n; i++ {\n        // Traverse in forward order\n        for a := 1; a <= amt; a++ {\n            if coins[i-1] > a {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a]\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[a] = dp[a] + dp[a-coins[i-1]]\n            }\n        }\n    }\n    return dp[amt]\n}\n
      coin_change_ii.swift
      /* Coin change II: Space-optimized dynamic programming */\nfunc coinChangeIIDPComp(coins: [Int], amt: Int) -> Int {\n    let n = coins.count\n    // Initialize dp table\n    var dp = Array(repeating: 0, count: amt + 1)\n    dp[0] = 1\n    // State transition\n    for i in 1 ... n {\n        for a in 1 ... amt {\n            if coins[i - 1] > a {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a]\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[a] = dp[a] + dp[a - coins[i - 1]]\n            }\n        }\n    }\n    return dp[amt]\n}\n
      coin_change_ii.js
      /* Coin change II: Space-optimized dynamic programming */\nfunction coinChangeIIDPComp(coins, amt) {\n    const n = coins.length;\n    // Initialize dp table\n    const dp = Array.from({ length: amt + 1 }, () => 0);\n    dp[0] = 1;\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
      coin_change_ii.ts
      /* Coin change II: Space-optimized dynamic programming */\nfunction coinChangeIIDPComp(coins: Array<number>, amt: number): number {\n    const n = coins.length;\n    // Initialize dp table\n    const dp = Array.from({ length: amt + 1 }, () => 0);\n    dp[0] = 1;\n    // State transition\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
      coin_change_ii.dart
      /* Coin change II: Space-optimized dynamic programming */\nint coinChangeIIDPComp(List<int> coins, int amt) {\n  int n = coins.length;\n  // Initialize dp table\n  List<int> dp = List.filled(amt + 1, 0);\n  dp[0] = 1;\n  // State transition\n  for (int i = 1; i <= n; i++) {\n    for (int a = 1; a <= amt; a++) {\n      if (coins[i - 1] > a) {\n        // If exceeds target amount, don't select coin i\n        dp[a] = dp[a];\n      } else {\n        // Sum of the two options: not selecting and selecting coin i\n        dp[a] = dp[a] + dp[a - coins[i - 1]];\n      }\n    }\n  }\n  return dp[amt];\n}\n
      coin_change_ii.rs
      /* Coin change II: Space-optimized dynamic programming */\nfn coin_change_ii_dp_comp(coins: &[i32], amt: usize) -> i32 {\n    let n = coins.len();\n    // Initialize dp table\n    let mut dp = vec![0; amt + 1];\n    dp[0] = 1;\n    // State transition\n    for i in 1..=n {\n        for a in 1..=amt {\n            if coins[i - 1] > a as i32 {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[a] = dp[a] + dp[a - coins[i - 1] as usize];\n            }\n        }\n    }\n    dp[amt]\n}\n
      coin_change_ii.c
      /* Coin change II: Space-optimized dynamic programming */\nint coinChangeIIDPComp(int coins[], int amt, int coinsSize) {\n    int n = coinsSize;\n    // Initialize dp table\n    int *dp = calloc(amt + 1, sizeof(int));\n    dp[0] = 1;\n    // State transition\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a];\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    int res = dp[amt];\n    // Free memory\n    free(dp);\n    return res;\n}\n
      coin_change_ii.kt
      /* Coin change II: Space-optimized dynamic programming */\nfun coinChangeIIDPComp(coins: IntArray, amt: Int): Int {\n    val n = coins.size\n    // Initialize dp table\n    val dp = IntArray(amt + 1)\n    dp[0] = 1\n    // State transition\n    for (i in 1..n) {\n        for (a in 1..amt) {\n            if (coins[i - 1] > a) {\n                // If exceeds target amount, don't select coin i\n                dp[a] = dp[a]\n            } else {\n                // Sum of the two options: not selecting and selecting coin i\n                dp[a] = dp[a] + dp[a - coins[i - 1]]\n            }\n        }\n    }\n    return dp[amt]\n}\n
      coin_change_ii.rb
      ### Coin change II: space-optimized DP ###\ndef coin_change_ii_dp_comp(coins, amt)\n  n = coins.length\n  # Initialize dp table\n  dp = Array.new(amt + 1, 0)\n  dp[0] = 1\n  # State transition\n  for i in 1...(n + 1)\n    # Traverse in forward order\n    for a in 1...(amt + 1)\n      if coins[i - 1] > a\n        # If exceeds target amount, don't select coin i\n        dp[a] = dp[a]\n      else\n        # Sum of the two options: not selecting and selecting coin i\n        dp[a] = dp[a] + dp[a - coins[i - 1]]\n      end\n    end\n  end\n  dp[amt]\nend\n
      "},{"location":"chapter_graph/","title":"Chapter 9. \u00a0 Graph","text":"

      Abstract

      In the journey of life, we are like nodes, connected by countless invisible edges.

      Each encounter and parting leaves a unique mark on this vast network graph.

      "},{"location":"chapter_graph/#chapter-contents","title":"Chapter contents","text":"
      • 9.1 \u00a0 Graph
      • 9.2 \u00a0 Basic Operations on Graphs
      • 9.3 \u00a0 Graph Traversal
      • 9.4 \u00a0 Summary
      "},{"location":"chapter_graph/graph/","title":"9.1 \u00a0 Graph","text":"

      A graph is a nonlinear data structure consisting of vertices and edges. We can abstractly represent a graph \\(G\\) as a set of vertices \\(V\\) and a set of edges \\(E\\). The following example shows a graph containing 5 vertices and 7 edges.

      \\[ \\begin{aligned} V & = \\{ 1, 2, 3, 4, 5 \\} \\newline E & = \\{ (1,2), (1,3), (1,5), (2,3), (2,4), (2,5), (4,5) \\} \\newline G & = \\{ V, E \\} \\newline \\end{aligned} \\]

      If we view vertices as nodes and edges as references (pointers) connecting the nodes, we can see graphs as a data structure extended from linked lists. As shown in Figure 9-1, compared to linear relationships (linked lists) and divide-and-conquer relationships (trees), network relationships (graphs) have a higher degree of freedom and are therefore more complex.

      Figure 9-1 \u00a0 Relationships among linked lists, trees, and graphs

      "},{"location":"chapter_graph/graph/#911-common-types-and-terminology-of-graphs","title":"9.1.1 \u00a0 Common Types and Terminology of Graphs","text":"

      Graphs can be divided into undirected graphs and directed graphs based on whether edges have direction, as shown in Figure 9-2.

      • In undirected graphs, edges represent a \"bidirectional\" connection between two vertices, such as the \"friend relationship\" on WeChat or QQ.
      • In directed graphs, edges have directionality, meaning edges \\(A \\rightarrow B\\) and \\(A \\leftarrow B\\) are independent of each other, such as the \"follow\" and \"be followed\" relationships on Weibo or TikTok.

      Figure 9-2 \u00a0 Directed and undirected graphs

      Graphs can be divided into connected graphs and disconnected graphs based on whether all vertices are connected, as shown in Figure 9-3.

      • For connected graphs, starting from any vertex, all other vertices can be reached.
      • For disconnected graphs, starting from a certain vertex, at least one vertex cannot be reached.

      Figure 9-3 \u00a0 Connected and disconnected graphs

      We can also add a \"weight\" variable to edges, resulting in weighted graphs as shown in Figure 9-4. For example, in mobile games like \"Honor of Kings\", the system calculates the \"intimacy\" between players based on their shared game time, and such intimacy networks can be represented using weighted graphs.

      Figure 9-4 \u00a0 Weighted and unweighted graphs

      Graph data structures include the following commonly used terms.

      • Adjacency: When two vertices are connected by an edge, these two vertices are said to be \"adjacent\". In Figure 9-4, the adjacent vertices of vertex 1 are vertices 2, 3, and 5.
      • Path: The sequence of edges from vertex A to vertex B is called a \"path\" from A to B. In Figure 9-4, the edge sequence 1-5-2-4 is a path from vertex 1 to vertex 4.
      • Degree: The number of edges a vertex has. For directed graphs, in-degree indicates how many edges point to the vertex, and out-degree indicates how many edges point out from the vertex.
      "},{"location":"chapter_graph/graph/#912-representation-of-graphs","title":"9.1.2 \u00a0 Representation of Graphs","text":"

      Common representations of graphs include \"adjacency matrices\" and \"adjacency lists\". The following uses undirected graphs as examples.

      "},{"location":"chapter_graph/graph/#1-adjacency-matrix","title":"1. \u00a0 Adjacency Matrix","text":"

      Given a graph with \\(n\\) vertices, an adjacency matrix uses an \\(n \\times n\\) matrix to represent the graph, where each row (column) represents a vertex, and matrix elements represent edges, using \\(1\\) or \\(0\\) to indicate whether an edge exists between two vertices.

      As shown in Figure 9-5, let the adjacency matrix be \\(M\\) and the vertex list be \\(V\\). Then matrix element \\(M[i, j] = 1\\) indicates that an edge exists between vertex \\(V[i]\\) and vertex \\(V[j]\\), whereas \\(M[i, j] = 0\\) indicates no edge between the two vertices.

      Figure 9-5 \u00a0 Adjacency matrix representation of a graph

      Adjacency matrices have the following properties.

      • In simple graphs, vertices cannot connect to themselves, so the elements on the main diagonal of the adjacency matrix are meaningless.
      • For undirected graphs, edges in both directions are equivalent, so the adjacency matrix is symmetric about the main diagonal.
      • Replacing the elements of the adjacency matrix from \\(1\\) and \\(0\\) to weights allows representation of weighted graphs.

      When using adjacency matrices to represent graphs, we can directly access matrix elements to obtain edges, resulting in highly efficient addition, deletion, lookup, and modification operations, all with a time complexity of \\(O(1)\\). However, the space complexity of the matrix is \\(O(n^2)\\), which consumes significant memory.

      "},{"location":"chapter_graph/graph/#2-adjacency-list","title":"2. \u00a0 Adjacency List","text":"

      An adjacency list uses \\(n\\) linked lists to represent a graph, with linked list nodes representing vertices. The \\(i\\)-th linked list corresponds to vertex \\(i\\) and stores all adjacent vertices of that vertex (vertices connected to that vertex). Figure 9-6 shows an example of a graph stored using an adjacency list.

      Figure 9-6 \u00a0 Adjacency list representation of a graph

      Adjacency lists only store edges that actually exist, and the total number of edges is typically much less than \\(n^2\\), making them more space-efficient. However, finding edges in an adjacency list requires traversing the linked list, so its time efficiency is inferior to that of adjacency matrices.

      Observing Figure 9-6, the structure of adjacency lists is very similar to \"chaining\" in hash tables, so we can adopt similar methods to optimize efficiency. For example, when linked lists are long, they can be converted to AVL trees or red-black trees, thereby optimizing time efficiency from \\(O(n)\\) to \\(O(\\log n)\\); linked lists can also be converted to hash tables, thereby reducing time complexity to \\(O(1)\\).

      "},{"location":"chapter_graph/graph/#913-common-applications-of-graphs","title":"9.1.3 \u00a0 Common Applications of Graphs","text":"

      As shown in Table 9-1, many real-world systems can be modeled using graphs, and corresponding problems can be reduced to graph computation problems.

      Table 9-1 \u00a0 Common graphs in real life

      Vertices Edges Graph Computation Problem Social network Users Friend relationships Potential friend recommendation Subway lines Stations Connectivity between stations Shortest route recommendation Solar system Celestial bodies Gravitational forces between celestial bodies Planetary orbit calculation"},{"location":"chapter_graph/graph_operations/","title":"9.2 \u00a0 Basic Operations on Graphs","text":"

      Basic operations on graphs can be divided into operations on \"edges\" and operations on \"vertices\". Under the two representation methods of \"adjacency matrix\" and \"adjacency list\", the implementation methods differ.

      "},{"location":"chapter_graph/graph_operations/#921-implementation-based-on-adjacency-matrix","title":"9.2.1 \u00a0 Implementation Based on Adjacency Matrix","text":"

      Given an undirected graph with \\(n\\) vertices, the various operations are implemented as shown in Figure 9-7.

      • Adding or removing an edge: Directly modify the specified edge in the adjacency matrix, using \\(O(1)\\) time. Since it is an undirected graph, both directions of the edge need to be updated simultaneously.
      • Adding a vertex: Add a row and a column at the end of the adjacency matrix and fill them all with \\(0\\)s, using \\(O(n)\\) time.
      • Removing a vertex: Delete a row and a column in the adjacency matrix. The worst case occurs when removing the first row and column, requiring \\((n-1)^2\\) elements to be \"moved up and to the left\", thus using \\(O(n^2)\\) time.
      • Initialization: Pass in \\(n\\) vertices, initialize a vertex list vertices of length \\(n\\), using \\(O(n)\\) time; initialize an adjacency matrix adjMat of size \\(n \\times n\\), using \\(O(n^2)\\) time.
      Initialize adjacency matrixAdd an edgeRemove an edgeAdd a vertexRemove a vertex

      Figure 9-7 \u00a0 Initialization, adding and removing edges, adding and removing vertices in adjacency matrix

      The following is the implementation code for graphs represented using an adjacency matrix:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_adjacency_matrix.py
      class GraphAdjMat:\n    \"\"\"Undirected graph class based on adjacency matrix\"\"\"\n\n    def __init__(self, vertices: list[int], edges: list[list[int]]):\n        \"\"\"Constructor\"\"\"\n        # Vertex list, where the element represents the \"vertex value\" and the index represents the \"vertex index\"\n        self.vertices: list[int] = []\n        # Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n        self.adj_mat: list[list[int]] = []\n        # Add vertices\n        for val in vertices:\n            self.add_vertex(val)\n        # Add edges\n        # Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n        for e in edges:\n            self.add_edge(e[0], e[1])\n\n    def size(self) -> int:\n        \"\"\"Get the number of vertices\"\"\"\n        return len(self.vertices)\n\n    def add_vertex(self, val: int):\n        \"\"\"Add vertex\"\"\"\n        n = self.size()\n        # Add the value of the new vertex to the vertex list\n        self.vertices.append(val)\n        # Add a row to the adjacency matrix\n        new_row = [0] * n\n        self.adj_mat.append(new_row)\n        # Add a column to the adjacency matrix\n        for row in self.adj_mat:\n            row.append(0)\n\n    def remove_vertex(self, index: int):\n        \"\"\"Remove vertex\"\"\"\n        if index >= self.size():\n            raise IndexError()\n        # Remove the vertex at index from the vertex list\n        self.vertices.pop(index)\n        # Remove the row at index from the adjacency matrix\n        self.adj_mat.pop(index)\n        # Remove the column at index from the adjacency matrix\n        for row in self.adj_mat:\n            row.pop(index)\n\n    def add_edge(self, i: int, j: int):\n        \"\"\"Add edge\"\"\"\n        # Parameters i, j correspond to the vertices element indices\n        # Handle index out of bounds and equality\n        if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\n            raise IndexError()\n        # In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)\n        self.adj_mat[i][j] = 1\n        self.adj_mat[j][i] = 1\n\n    def remove_edge(self, i: int, j: int):\n        \"\"\"Remove edge\"\"\"\n        # Parameters i, j correspond to the vertices element indices\n        # Handle index out of bounds and equality\n        if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\n            raise IndexError()\n        self.adj_mat[i][j] = 0\n        self.adj_mat[j][i] = 0\n\n    def print(self):\n        \"\"\"Print adjacency matrix\"\"\"\n        print(\"Vertex list =\", self.vertices)\n        print(\"Adjacency matrix =\")\n        print_matrix(self.adj_mat)\n
      graph_adjacency_matrix.cpp
      /* Undirected graph class based on adjacency matrix */\nclass GraphAdjMat {\n    vector<int> vertices;       // Vertex list, where the element represents the \"vertex value\" and the index represents the \"vertex index\"\n    vector<vector<int>> adjMat; // Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n\n  public:\n    /* Constructor */\n    GraphAdjMat(const vector<int> &vertices, const vector<vector<int>> &edges) {\n        // Add vertex\n        for (int val : vertices) {\n            addVertex(val);\n        }\n        // Add edge\n        // Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n        for (const vector<int> &edge : edges) {\n            addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* Get the number of vertices */\n    int size() const {\n        return vertices.size();\n    }\n\n    /* Add vertex */\n    void addVertex(int val) {\n        int n = size();\n        // Add the value of the new vertex to the vertex list\n        vertices.push_back(val);\n        // Add a row to the adjacency matrix\n        adjMat.emplace_back(vector<int>(n, 0));\n        // Add a column to the adjacency matrix\n        for (vector<int> &row : adjMat) {\n            row.push_back(0);\n        }\n    }\n\n    /* Remove vertex */\n    void removeVertex(int index) {\n        if (index >= size()) {\n            throw out_of_range(\"Vertex does not exist\");\n        }\n        // Remove the vertex at index from the vertex list\n        vertices.erase(vertices.begin() + index);\n        // Remove the row at index from the adjacency matrix\n        adjMat.erase(adjMat.begin() + index);\n        // Remove the column at index from the adjacency matrix\n        for (vector<int> &row : adjMat) {\n            row.erase(row.begin() + index);\n        }\n    }\n\n    /* Add edge */\n    // Parameters i, j correspond to the vertices element indices\n    void addEdge(int i, int j) {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n            throw out_of_range(\"Vertex does not exist\");\n        }\n        // In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)\n        adjMat[i][j] = 1;\n        adjMat[j][i] = 1;\n    }\n\n    /* Remove edge */\n    // Parameters i, j correspond to the vertices element indices\n    void removeEdge(int i, int j) {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n            throw out_of_range(\"Vertex does not exist\");\n        }\n        adjMat[i][j] = 0;\n        adjMat[j][i] = 0;\n    }\n\n    /* Print adjacency matrix */\n    void print() {\n        cout << \"Vertex list = \";\n        printVector(vertices);\n        cout << \"Adjacency matrix =\" << endl;\n        printVectorMatrix(adjMat);\n    }\n};\n
      graph_adjacency_matrix.java
      /* Undirected graph class based on adjacency matrix */\nclass GraphAdjMat {\n    List<Integer> vertices; // Vertex list, where the element represents the \"vertex value\" and the index represents the \"vertex index\"\n    List<List<Integer>> adjMat; // Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n\n    /* Constructor */\n    public GraphAdjMat(int[] vertices, int[][] edges) {\n        this.vertices = new ArrayList<>();\n        this.adjMat = new ArrayList<>();\n        // Add vertex\n        for (int val : vertices) {\n            addVertex(val);\n        }\n        // Add edge\n        // Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n        for (int[] e : edges) {\n            addEdge(e[0], e[1]);\n        }\n    }\n\n    /* Get the number of vertices */\n    public int size() {\n        return vertices.size();\n    }\n\n    /* Add vertex */\n    public void addVertex(int val) {\n        int n = size();\n        // Add the value of the new vertex to the vertex list\n        vertices.add(val);\n        // Add a row to the adjacency matrix\n        List<Integer> newRow = new ArrayList<>(n);\n        for (int j = 0; j < n; j++) {\n            newRow.add(0);\n        }\n        adjMat.add(newRow);\n        // Add a column to the adjacency matrix\n        for (List<Integer> row : adjMat) {\n            row.add(0);\n        }\n    }\n\n    /* Remove vertex */\n    public void removeVertex(int index) {\n        if (index >= size())\n            throw new IndexOutOfBoundsException();\n        // Remove the vertex at index from the vertex list\n        vertices.remove(index);\n        // Remove the row at index from the adjacency matrix\n        adjMat.remove(index);\n        // Remove the column at index from the adjacency matrix\n        for (List<Integer> row : adjMat) {\n            row.remove(index);\n        }\n    }\n\n    /* Add edge */\n    // Parameters i, j correspond to the vertices element indices\n    public void addEdge(int i, int j) {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw new IndexOutOfBoundsException();\n        // In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)\n        adjMat.get(i).set(j, 1);\n        adjMat.get(j).set(i, 1);\n    }\n\n    /* Remove edge */\n    // Parameters i, j correspond to the vertices element indices\n    public void removeEdge(int i, int j) {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw new IndexOutOfBoundsException();\n        adjMat.get(i).set(j, 0);\n        adjMat.get(j).set(i, 0);\n    }\n\n    /* Print adjacency matrix */\n    public void print() {\n        System.out.print(\"Vertex list = \");\n        System.out.println(vertices);\n        System.out.println(\"Adjacency matrix =\");\n        PrintUtil.printMatrix(adjMat);\n    }\n}\n
      graph_adjacency_matrix.cs
      /* Undirected graph class based on adjacency matrix */\nclass GraphAdjMat {\n    List<int> vertices;     // Vertex list, where the element represents the \"vertex value\" and the index represents the \"vertex index\"\n    List<List<int>> adjMat; // Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n\n    /* Constructor */\n    public GraphAdjMat(int[] vertices, int[][] edges) {\n        this.vertices = [];\n        this.adjMat = [];\n        // Add vertex\n        foreach (int val in vertices) {\n            AddVertex(val);\n        }\n        // Add edge\n        // Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n        foreach (int[] e in edges) {\n            AddEdge(e[0], e[1]);\n        }\n    }\n\n    /* Get the number of vertices */\n    int Size() {\n        return vertices.Count;\n    }\n\n    /* Add vertex */\n    public void AddVertex(int val) {\n        int n = Size();\n        // Add the value of the new vertex to the vertex list\n        vertices.Add(val);\n        // Add a row to the adjacency matrix\n        List<int> newRow = new(n);\n        for (int j = 0; j < n; j++) {\n            newRow.Add(0);\n        }\n        adjMat.Add(newRow);\n        // Add a column to the adjacency matrix\n        foreach (List<int> row in adjMat) {\n            row.Add(0);\n        }\n    }\n\n    /* Remove vertex */\n    public void RemoveVertex(int index) {\n        if (index >= Size())\n            throw new IndexOutOfRangeException();\n        // Remove the vertex at index from the vertex list\n        vertices.RemoveAt(index);\n        // Remove the row at index from the adjacency matrix\n        adjMat.RemoveAt(index);\n        // Remove the column at index from the adjacency matrix\n        foreach (List<int> row in adjMat) {\n            row.RemoveAt(index);\n        }\n    }\n\n    /* Add edge */\n    // Parameters i, j correspond to the vertices element indices\n    public void AddEdge(int i, int j) {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)\n            throw new IndexOutOfRangeException();\n        // In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)\n        adjMat[i][j] = 1;\n        adjMat[j][i] = 1;\n    }\n\n    /* Remove edge */\n    // Parameters i, j correspond to the vertices element indices\n    public void RemoveEdge(int i, int j) {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)\n            throw new IndexOutOfRangeException();\n        adjMat[i][j] = 0;\n        adjMat[j][i] = 0;\n    }\n\n    /* Print adjacency matrix */\n    public void Print() {\n        Console.Write(\"Vertex list = \");\n        PrintUtil.PrintList(vertices);\n        Console.WriteLine(\"Adjacency matrix =\");\n        PrintUtil.PrintMatrix(adjMat);\n    }\n}\n
      graph_adjacency_matrix.go
      /* Undirected graph class based on adjacency matrix */\ntype graphAdjMat struct {\n    // Vertex list, where the element represents the \"vertex value\" and the index represents the \"vertex index\"\n    vertices []int\n    // Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n    adjMat [][]int\n}\n\n/* Constructor */\nfunc newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat {\n    // Add vertex\n    n := len(vertices)\n    adjMat := make([][]int, n)\n    for i := range adjMat {\n        adjMat[i] = make([]int, n)\n    }\n    // Initialize graph\n    g := &graphAdjMat{\n        vertices: vertices,\n        adjMat:   adjMat,\n    }\n    // Add edge\n    // Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n    for i := range edges {\n        g.addEdge(edges[i][0], edges[i][1])\n    }\n    return g\n}\n\n/* Get the number of vertices */\nfunc (g *graphAdjMat) size() int {\n    return len(g.vertices)\n}\n\n/* Add vertex */\nfunc (g *graphAdjMat) addVertex(val int) {\n    n := g.size()\n    // Add the value of the new vertex to the vertex list\n    g.vertices = append(g.vertices, val)\n    // Add a row to the adjacency matrix\n    newRow := make([]int, n)\n    g.adjMat = append(g.adjMat, newRow)\n    // Add a column to the adjacency matrix\n    for i := range g.adjMat {\n        g.adjMat[i] = append(g.adjMat[i], 0)\n    }\n}\n\n/* Remove vertex */\nfunc (g *graphAdjMat) removeVertex(index int) {\n    if index >= g.size() {\n        return\n    }\n    // Remove the vertex at index from the vertex list\n    g.vertices = append(g.vertices[:index], g.vertices[index+1:]...)\n    // Remove the row at index from the adjacency matrix\n    g.adjMat = append(g.adjMat[:index], g.adjMat[index+1:]...)\n    // Remove the column at index from the adjacency matrix\n    for i := range g.adjMat {\n        g.adjMat[i] = append(g.adjMat[i][:index], g.adjMat[i][index+1:]...)\n    }\n}\n\n/* Add edge */\n// Parameters i, j correspond to the vertices element indices\nfunc (g *graphAdjMat) addEdge(i, j int) {\n    // Handle index out of bounds and equality\n    if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\n        fmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n    }\n    // In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)\n    g.adjMat[i][j] = 1\n    g.adjMat[j][i] = 1\n}\n\n/* Remove edge */\n// Parameters i, j correspond to the vertices element indices\nfunc (g *graphAdjMat) removeEdge(i, j int) {\n    // Handle index out of bounds and equality\n    if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\n        fmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n    }\n    g.adjMat[i][j] = 0\n    g.adjMat[j][i] = 0\n}\n\n/* Print adjacency matrix */\nfunc (g *graphAdjMat) print() {\n    fmt.Printf(\"\\tVertex list = %v\\n\", g.vertices)\n    fmt.Printf(\"\\tAdjacency matrix = \\n\")\n    for i := range g.adjMat {\n        fmt.Printf(\"\\t\\t\\t%v\\n\", g.adjMat[i])\n    }\n}\n
      graph_adjacency_matrix.swift
      /* Undirected graph class based on adjacency matrix */\nclass GraphAdjMat {\n    private var vertices: [Int] // Vertex list, where the element represents the \"vertex value\" and the index represents the \"vertex index\"\n    private var adjMat: [[Int]] // Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n\n    /* Constructor */\n    init(vertices: [Int], edges: [[Int]]) {\n        self.vertices = []\n        adjMat = []\n        // Add vertex\n        for val in vertices {\n            addVertex(val: val)\n        }\n        // Add edge\n        // Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n        for e in edges {\n            addEdge(i: e[0], j: e[1])\n        }\n    }\n\n    /* Get the number of vertices */\n    func size() -> Int {\n        vertices.count\n    }\n\n    /* Add vertex */\n    func addVertex(val: Int) {\n        let n = size()\n        // Add the value of the new vertex to the vertex list\n        vertices.append(val)\n        // Add a row to the adjacency matrix\n        let newRow = Array(repeating: 0, count: n)\n        adjMat.append(newRow)\n        // Add a column to the adjacency matrix\n        for i in adjMat.indices {\n            adjMat[i].append(0)\n        }\n    }\n\n    /* Remove vertex */\n    func removeVertex(index: Int) {\n        if index >= size() {\n            fatalError(\"Out of bounds\")\n        }\n        // Remove the vertex at index from the vertex list\n        vertices.remove(at: index)\n        // Remove the row at index from the adjacency matrix\n        adjMat.remove(at: index)\n        // Remove the column at index from the adjacency matrix\n        for i in adjMat.indices {\n            adjMat[i].remove(at: index)\n        }\n    }\n\n    /* Add edge */\n    // Parameters i, j correspond to the vertices element indices\n    func addEdge(i: Int, j: Int) {\n        // Handle index out of bounds and equality\n        if i < 0 || j < 0 || i >= size() || j >= size() || i == j {\n            fatalError(\"Out of bounds\")\n        }\n        // In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)\n        adjMat[i][j] = 1\n        adjMat[j][i] = 1\n    }\n\n    /* Remove edge */\n    // Parameters i, j correspond to the vertices element indices\n    func removeEdge(i: Int, j: Int) {\n        // Handle index out of bounds and equality\n        if i < 0 || j < 0 || i >= size() || j >= size() || i == j {\n            fatalError(\"Out of bounds\")\n        }\n        adjMat[i][j] = 0\n        adjMat[j][i] = 0\n    }\n\n    /* Print adjacency matrix */\n    func print() {\n        Swift.print(\"Vertex list = \", terminator: \"\")\n        Swift.print(vertices)\n        Swift.print(\"Adjacency matrix =\")\n        PrintUtil.printMatrix(matrix: adjMat)\n    }\n}\n
      graph_adjacency_matrix.js
      /* Undirected graph class based on adjacency matrix */\nclass GraphAdjMat {\n    vertices; // Vertex list, where the element represents the \"vertex value\" and the index represents the \"vertex index\"\n    adjMat; // Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n\n    /* Constructor */\n    constructor(vertices, edges) {\n        this.vertices = [];\n        this.adjMat = [];\n        // Add vertex\n        for (const val of vertices) {\n            this.addVertex(val);\n        }\n        // Add edge\n        // Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n        for (const e of edges) {\n            this.addEdge(e[0], e[1]);\n        }\n    }\n\n    /* Get the number of vertices */\n    size() {\n        return this.vertices.length;\n    }\n\n    /* Add vertex */\n    addVertex(val) {\n        const n = this.size();\n        // Add the value of the new vertex to the vertex list\n        this.vertices.push(val);\n        // Add a row to the adjacency matrix\n        const newRow = [];\n        for (let j = 0; j < n; j++) {\n            newRow.push(0);\n        }\n        this.adjMat.push(newRow);\n        // Add a column to the adjacency matrix\n        for (const row of this.adjMat) {\n            row.push(0);\n        }\n    }\n\n    /* Remove vertex */\n    removeVertex(index) {\n        if (index >= this.size()) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // Remove the vertex at index from the vertex list\n        this.vertices.splice(index, 1);\n\n        // Remove the row at index from the adjacency matrix\n        this.adjMat.splice(index, 1);\n        // Remove the column at index from the adjacency matrix\n        for (const row of this.adjMat) {\n            row.splice(index, 1);\n        }\n    }\n\n    /* Add edge */\n    // Parameters i, j correspond to the vertices element indices\n    addEdge(i, j) {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // In undirected graph, adjacency matrix is symmetric about main diagonal, i.e., satisfies (i, j) === (j, i)\n        this.adjMat[i][j] = 1;\n        this.adjMat[j][i] = 1;\n    }\n\n    /* Remove edge */\n    // Parameters i, j correspond to the vertices element indices\n    removeEdge(i, j) {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        this.adjMat[i][j] = 0;\n        this.adjMat[j][i] = 0;\n    }\n\n    /* Print adjacency matrix */\n    print() {\n        console.log('Vertex list = ', this.vertices);\n        console.log('Adjacency matrix =', this.adjMat);\n    }\n}\n
      graph_adjacency_matrix.ts
      /* Undirected graph class based on adjacency matrix */\nclass GraphAdjMat {\n    vertices: number[]; // Vertex list, where the element represents the \"vertex value\" and the index represents the \"vertex index\"\n    adjMat: number[][]; // Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n\n    /* Constructor */\n    constructor(vertices: number[], edges: number[][]) {\n        this.vertices = [];\n        this.adjMat = [];\n        // Add vertex\n        for (const val of vertices) {\n            this.addVertex(val);\n        }\n        // Add edge\n        // Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n        for (const e of edges) {\n            this.addEdge(e[0], e[1]);\n        }\n    }\n\n    /* Get the number of vertices */\n    size(): number {\n        return this.vertices.length;\n    }\n\n    /* Add vertex */\n    addVertex(val: number): void {\n        const n: number = this.size();\n        // Add the value of the new vertex to the vertex list\n        this.vertices.push(val);\n        // Add a row to the adjacency matrix\n        const newRow: number[] = [];\n        for (let j: number = 0; j < n; j++) {\n            newRow.push(0);\n        }\n        this.adjMat.push(newRow);\n        // Add a column to the adjacency matrix\n        for (const row of this.adjMat) {\n            row.push(0);\n        }\n    }\n\n    /* Remove vertex */\n    removeVertex(index: number): void {\n        if (index >= this.size()) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // Remove the vertex at index from the vertex list\n        this.vertices.splice(index, 1);\n\n        // Remove the row at index from the adjacency matrix\n        this.adjMat.splice(index, 1);\n        // Remove the column at index from the adjacency matrix\n        for (const row of this.adjMat) {\n            row.splice(index, 1);\n        }\n    }\n\n    /* Add edge */\n    // Parameters i, j correspond to the vertices element indices\n    addEdge(i: number, j: number): void {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // In undirected graph, adjacency matrix is symmetric about main diagonal, i.e., satisfies (i, j) === (j, i)\n        this.adjMat[i][j] = 1;\n        this.adjMat[j][i] = 1;\n    }\n\n    /* Remove edge */\n    // Parameters i, j correspond to the vertices element indices\n    removeEdge(i: number, j: number): void {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        this.adjMat[i][j] = 0;\n        this.adjMat[j][i] = 0;\n    }\n\n    /* Print adjacency matrix */\n    print(): void {\n        console.log('Vertex list = ', this.vertices);\n        console.log('Adjacency matrix =', this.adjMat);\n    }\n}\n
      graph_adjacency_matrix.dart
      /* Undirected graph class based on adjacency matrix */\nclass GraphAdjMat {\n  List<int> vertices = []; // Vertex elements, elements represent \"vertex values\", indices represent \"vertex indices\"\n  List<List<int>> adjMat = []; // Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n\n  /* Constructor */\n  GraphAdjMat(List<int> vertices, List<List<int>> edges) {\n    this.vertices = [];\n    this.adjMat = [];\n    // Add vertex\n    for (int val in vertices) {\n      addVertex(val);\n    }\n    // Add edge\n    // Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n    for (List<int> e in edges) {\n      addEdge(e[0], e[1]);\n    }\n  }\n\n  /* Get the number of vertices */\n  int size() {\n    return vertices.length;\n  }\n\n  /* Add vertex */\n  void addVertex(int val) {\n    int n = size();\n    // Add the value of the new vertex to the vertex list\n    vertices.add(val);\n    // Add a row to the adjacency matrix\n    List<int> newRow = List.filled(n, 0, growable: true);\n    adjMat.add(newRow);\n    // Add a column to the adjacency matrix\n    for (List<int> row in adjMat) {\n      row.add(0);\n    }\n  }\n\n  /* Remove vertex */\n  void removeVertex(int index) {\n    if (index >= size()) {\n      throw IndexError;\n    }\n    // Remove the vertex at index from the vertex list\n    vertices.removeAt(index);\n    // Remove the row at index from the adjacency matrix\n    adjMat.removeAt(index);\n    // Remove the column at index from the adjacency matrix\n    for (List<int> row in adjMat) {\n      row.removeAt(index);\n    }\n  }\n\n  /* Add edge */\n  // Parameters i, j correspond to the vertices element indices\n  void addEdge(int i, int j) {\n    // Handle index out of bounds and equality\n    if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n      throw IndexError;\n    }\n    // In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)\n    adjMat[i][j] = 1;\n    adjMat[j][i] = 1;\n  }\n\n  /* Remove edge */\n  // Parameters i, j correspond to the vertices element indices\n  void removeEdge(int i, int j) {\n    // Handle index out of bounds and equality\n    if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n      throw IndexError;\n    }\n    adjMat[i][j] = 0;\n    adjMat[j][i] = 0;\n  }\n\n  /* Print adjacency matrix */\n  void printAdjMat() {\n    print(\"Vertex list = $vertices\");\n    print(\"Adjacency matrix = \");\n    printMatrix(adjMat);\n  }\n}\n
      graph_adjacency_matrix.rs
      /* Undirected graph type based on adjacency matrix */\npub struct GraphAdjMat {\n    // Vertex list, where the element represents the \"vertex value\" and the index represents the \"vertex index\"\n    pub vertices: Vec<i32>,\n    // Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n    pub adj_mat: Vec<Vec<i32>>,\n}\n\nimpl GraphAdjMat {\n    /* Constructor */\n    pub fn new(vertices: Vec<i32>, edges: Vec<[usize; 2]>) -> Self {\n        let mut graph = GraphAdjMat {\n            vertices: vec![],\n            adj_mat: vec![],\n        };\n        // Add vertex\n        for val in vertices {\n            graph.add_vertex(val);\n        }\n        // Add edge\n        // Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n        for edge in edges {\n            graph.add_edge(edge[0], edge[1])\n        }\n\n        graph\n    }\n\n    /* Get the number of vertices */\n    pub fn size(&self) -> usize {\n        self.vertices.len()\n    }\n\n    /* Add vertex */\n    pub fn add_vertex(&mut self, val: i32) {\n        let n = self.size();\n        // Add the value of the new vertex to the vertex list\n        self.vertices.push(val);\n        // Add a row to the adjacency matrix\n        self.adj_mat.push(vec![0; n]);\n        // Add a column to the adjacency matrix\n        for row in self.adj_mat.iter_mut() {\n            row.push(0);\n        }\n    }\n\n    /* Remove vertex */\n    pub fn remove_vertex(&mut self, index: usize) {\n        if index >= self.size() {\n            panic!(\"index error\")\n        }\n        // Remove the vertex at index from the vertex list\n        self.vertices.remove(index);\n        // Remove the row at index from the adjacency matrix\n        self.adj_mat.remove(index);\n        // Remove the column at index from the adjacency matrix\n        for row in self.adj_mat.iter_mut() {\n            row.remove(index);\n        }\n    }\n\n    /* Add edge */\n    pub fn add_edge(&mut self, i: usize, j: usize) {\n        // Parameters i, j correspond to the vertices element indices\n        // Handle index out of bounds and equality\n        if i >= self.size() || j >= self.size() || i == j {\n            panic!(\"index error\")\n        }\n        // In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)\n        self.adj_mat[i][j] = 1;\n        self.adj_mat[j][i] = 1;\n    }\n\n    /* Remove edge */\n    // Parameters i, j correspond to the vertices element indices\n    pub fn remove_edge(&mut self, i: usize, j: usize) {\n        // Parameters i, j correspond to the vertices element indices\n        // Handle index out of bounds and equality\n        if i >= self.size() || j >= self.size() || i == j {\n            panic!(\"index error\")\n        }\n        self.adj_mat[i][j] = 0;\n        self.adj_mat[j][i] = 0;\n    }\n\n    /* Print adjacency matrix */\n    pub fn print(&self) {\n        println!(\"Vertex list = {:?}\", self.vertices);\n        println!(\"Adjacency matrix =\");\n        println!(\"[\");\n        for row in &self.adj_mat {\n            println!(\"  {:?},\", row);\n        }\n        println!(\"]\")\n    }\n}\n
      graph_adjacency_matrix.c
      /* Undirected graph structure based on adjacency matrix */\ntypedef struct {\n    int vertices[MAX_SIZE];\n    int adjMat[MAX_SIZE][MAX_SIZE];\n    int size;\n} GraphAdjMat;\n\n/* Constructor */\nGraphAdjMat *newGraphAdjMat() {\n    GraphAdjMat *graph = (GraphAdjMat *)malloc(sizeof(GraphAdjMat));\n    graph->size = 0;\n    for (int i = 0; i < MAX_SIZE; i++) {\n        for (int j = 0; j < MAX_SIZE; j++) {\n            graph->adjMat[i][j] = 0;\n        }\n    }\n    return graph;\n}\n\n/* Destructor */\nvoid delGraphAdjMat(GraphAdjMat *graph) {\n    free(graph);\n}\n\n/* Add vertex */\nvoid addVertex(GraphAdjMat *graph, int val) {\n    if (graph->size == MAX_SIZE) {\n        fprintf(stderr, \"Graph vertex count has reached maximum\\n\");\n        return;\n    }\n    // Add nth vertex and zero nth row and column\n    int n = graph->size;\n    graph->vertices[n] = val;\n    for (int i = 0; i <= n; i++) {\n        graph->adjMat[n][i] = graph->adjMat[i][n] = 0;\n    }\n    graph->size++;\n}\n\n/* Remove vertex */\nvoid removeVertex(GraphAdjMat *graph, int index) {\n    if (index < 0 || index >= graph->size) {\n        fprintf(stderr, \"Vertex index out of bounds\\n\");\n        return;\n    }\n    // Remove the vertex at index from the vertex list\n    for (int i = index; i < graph->size - 1; i++) {\n        graph->vertices[i] = graph->vertices[i + 1];\n    }\n    // Remove the row at index from the adjacency matrix\n    for (int i = index; i < graph->size - 1; i++) {\n        for (int j = 0; j < graph->size; j++) {\n            graph->adjMat[i][j] = graph->adjMat[i + 1][j];\n        }\n    }\n    // Remove the column at index from the adjacency matrix\n    for (int i = 0; i < graph->size; i++) {\n        for (int j = index; j < graph->size - 1; j++) {\n            graph->adjMat[i][j] = graph->adjMat[i][j + 1];\n        }\n    }\n    graph->size--;\n}\n\n/* Add edge */\n// Parameters i, j correspond to the vertices element indices\nvoid addEdge(GraphAdjMat *graph, int i, int j) {\n    if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n        fprintf(stderr, \"Edge index out of bounds or equal\\n\");\n        return;\n    }\n    graph->adjMat[i][j] = 1;\n    graph->adjMat[j][i] = 1;\n}\n\n/* Remove edge */\n// Parameters i, j correspond to the vertices element indices\nvoid removeEdge(GraphAdjMat *graph, int i, int j) {\n    if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n        fprintf(stderr, \"Edge index out of bounds or equal\\n\");\n        return;\n    }\n    graph->adjMat[i][j] = 0;\n    graph->adjMat[j][i] = 0;\n}\n\n/* Print adjacency matrix */\nvoid printGraphAdjMat(GraphAdjMat *graph) {\n    printf(\"Vertex list = \");\n    printArray(graph->vertices, graph->size);\n    printf(\"Adjacency matrix =\\n\");\n    for (int i = 0; i < graph->size; i++) {\n        printArray(graph->adjMat[i], graph->size);\n    }\n}\n
      graph_adjacency_matrix.kt
      /* Undirected graph class based on adjacency matrix */\nclass GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {\n    val vertices = mutableListOf<Int>() // Vertex list, where the element represents the \"vertex value\" and the index represents the \"vertex index\"\n    val adjMat = mutableListOf<MutableList<Int>>() // Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n\n    /* Constructor */\n    init {\n        // Add vertex\n        for (vertex in vertices) {\n            addVertex(vertex)\n        }\n        // Add edge\n        // Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n        for (edge in edges) {\n            addEdge(edge[0], edge[1])\n        }\n    }\n\n    /* Get the number of vertices */\n    fun size(): Int {\n        return vertices.size\n    }\n\n    /* Add vertex */\n    fun addVertex(_val: Int) {\n        val n = size()\n        // Add the value of the new vertex to the vertex list\n        vertices.add(_val)\n        // Add a row to the adjacency matrix\n        val newRow = mutableListOf<Int>()\n        for (j in 0..<n) {\n            newRow.add(0)\n        }\n        adjMat.add(newRow)\n        // Add a column to the adjacency matrix\n        for (row in adjMat) {\n            row.add(0)\n        }\n    }\n\n    /* Remove vertex */\n    fun removeVertex(index: Int) {\n        if (index >= size())\n            throw IndexOutOfBoundsException()\n        // Remove the vertex at index from the vertex list\n        vertices.removeAt(index)\n        // Remove the row at index from the adjacency matrix\n        adjMat.removeAt(index)\n        // Remove the column at index from the adjacency matrix\n        for (row in adjMat) {\n            row.removeAt(index)\n        }\n    }\n\n    /* Add edge */\n    // Parameters i, j correspond to the vertices element indices\n    fun addEdge(i: Int, j: Int) {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw IndexOutOfBoundsException()\n        // In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)\n        adjMat[i][j] = 1\n        adjMat[j][i] = 1\n    }\n\n    /* Remove edge */\n    // Parameters i, j correspond to the vertices element indices\n    fun removeEdge(i: Int, j: Int) {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw IndexOutOfBoundsException()\n        adjMat[i][j] = 0\n        adjMat[j][i] = 0\n    }\n\n    /* Print adjacency matrix */\n    fun print() {\n        print(\"Vertex list = \")\n        println(vertices)\n        println(\"Adjacency matrix =\")\n        printMatrix(adjMat)\n    }\n}\n
      graph_adjacency_matrix.rb
      ### Undirected graph class based on adjacency matrix ###\nclass GraphAdjMat\n  def initialize(vertices, edges)\n    ### Constructor ###\n    # Vertex list, where the element represents the \"vertex value\" and the index represents the \"vertex index\"\n    @vertices = []\n    # Adjacency matrix, where the row and column indices correspond to the \"vertex index\"\n    @adj_mat = []\n    # Add vertex\n    vertices.each { |val| add_vertex(val) }\n    # Add edge\n    # Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices\n    edges.each { |e| add_edge(e[0], e[1]) }\n  end\n\n  ### Get number of vertices ###\n  def size\n    @vertices.length\n  end\n\n  ### Add vertex ###\n  def add_vertex(val)\n    n = size\n    # Add the value of the new vertex to the vertex list\n    @vertices << val\n    # Add a row to the adjacency matrix\n    new_row = Array.new(n, 0)\n    @adj_mat << new_row\n    # Add a column to the adjacency matrix\n    @adj_mat.each { |row| row << 0 }\n  end\n\n  ### Delete vertex ###\n  def remove_vertex(index)\n    raise IndexError if index >= size\n\n    # Remove the vertex at index from the vertex list\n    @vertices.delete_at(index)\n    # Remove the row at index from the adjacency matrix\n    @adj_mat.delete_at(index)\n    # Remove the column at index from the adjacency matrix\n    @adj_mat.each { |row| row.delete_at(index) }\n  end\n\n  ### Add edge ###\n  def add_edge(i, j)\n    # Parameters i, j correspond to the vertices element indices\n    # Handle index out of bounds and equality\n    if i < 0 || j < 0 || i >= size || j >= size || i == j\n      raise IndexError\n    end\n    # In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)\n    @adj_mat[i][j] = 1\n    @adj_mat[j][i] = 1\n  end\n\n  ### Delete edge ###\n  def remove_edge(i, j)\n    # Parameters i, j correspond to the vertices element indices\n    # Handle index out of bounds and equality\n    if i < 0 || j < 0 || i >= size || j >= size || i == j\n      raise IndexError\n    end\n    @adj_mat[i][j] = 0\n    @adj_mat[j][i] = 0\n  end\n\n  ### Print adjacency matrix ###\n  def __print__\n    puts \"Vertex list = #{@vertices}\"\n    puts 'Adjacency matrix ='\n    print_matrix(@adj_mat)\n  end\nend\n
      "},{"location":"chapter_graph/graph_operations/#922-implementation-based-on-adjacency-list","title":"9.2.2 \u00a0 Implementation Based on Adjacency List","text":"

      Given an undirected graph with a total of \\(n\\) vertices and \\(m\\) edges, the various operations can be implemented as shown in Figure 9-8.

      • Adding an edge: Add the edge at the end of the corresponding vertex's linked list, using \\(O(1)\\) time. Since it is an undirected graph, edges in both directions need to be added simultaneously.
      • Removing an edge: Find and remove the specified edge in the corresponding vertex's linked list, using \\(O(m)\\) time. In an undirected graph, edges in both directions need to be removed simultaneously.
      • Adding a vertex: Add a linked list in the adjacency list and set the new vertex as the head node of the list, using \\(O(1)\\) time.
      • Removing a vertex: Traverse the entire adjacency list and remove all edges containing the specified vertex, using \\(O(n + m)\\) time.
      • Initialization: Create \\(n\\) vertices and \\(2m\\) edges in the adjacency list, using \\(O(n + m)\\) time.
      Initialize adjacency listAdd an edgeRemove an edgeAdd a vertexRemove a vertex

      Figure 9-8 \u00a0 Initialization, adding and removing edges, adding and removing vertices in adjacency list

      The following is the adjacency list code implementation. Compared to Figure 9-8, the actual code has the following differences.

      • For convenience in adding and removing vertices, and to simplify the code, we use lists (dynamic arrays) instead of linked lists.
      • A hash table is used to store the adjacency list, where key is the vertex instance and value is the list (linked list) of adjacent vertices for that vertex.

      Additionally, we use the Vertex class to represent vertices in the adjacency list. The reason for this is: if we used list indices to distinguish different vertices as with adjacency matrices, then to delete the vertex at index \\(i\\), we would need to traverse the entire adjacency list and decrement all indices greater than \\(i\\) by \\(1\\), which is very inefficient. However, if each vertex is a unique Vertex instance, deleting a vertex does not require modifying other vertices.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_adjacency_list.py
      class GraphAdjList:\n    \"\"\"Undirected graph class based on adjacency list\"\"\"\n\n    def __init__(self, edges: list[list[Vertex]]):\n        \"\"\"Constructor\"\"\"\n        # Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n        self.adj_list = dict[Vertex, list[Vertex]]()\n        # Add all vertices and edges\n        for edge in edges:\n            self.add_vertex(edge[0])\n            self.add_vertex(edge[1])\n            self.add_edge(edge[0], edge[1])\n\n    def size(self) -> int:\n        \"\"\"Get the number of vertices\"\"\"\n        return len(self.adj_list)\n\n    def add_edge(self, vet1: Vertex, vet2: Vertex):\n        \"\"\"Add edge\"\"\"\n        if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n            raise ValueError()\n        # Add edge vet1 - vet2\n        self.adj_list[vet1].append(vet2)\n        self.adj_list[vet2].append(vet1)\n\n    def remove_edge(self, vet1: Vertex, vet2: Vertex):\n        \"\"\"Remove edge\"\"\"\n        if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n            raise ValueError()\n        # Remove edge vet1 - vet2\n        self.adj_list[vet1].remove(vet2)\n        self.adj_list[vet2].remove(vet1)\n\n    def add_vertex(self, vet: Vertex):\n        \"\"\"Add vertex\"\"\"\n        if vet in self.adj_list:\n            return\n        # Add a new linked list in the adjacency list\n        self.adj_list[vet] = []\n\n    def remove_vertex(self, vet: Vertex):\n        \"\"\"Remove vertex\"\"\"\n        if vet not in self.adj_list:\n            raise ValueError()\n        # Remove the linked list corresponding to vertex vet in the adjacency list\n        self.adj_list.pop(vet)\n        # Traverse the linked lists of other vertices and remove all edges containing vet\n        for vertex in self.adj_list:\n            if vet in self.adj_list[vertex]:\n                self.adj_list[vertex].remove(vet)\n\n    def print(self):\n        \"\"\"Print adjacency list\"\"\"\n        print(\"Adjacency list =\")\n        for vertex in self.adj_list:\n            tmp = [v.val for v in self.adj_list[vertex]]\n            print(f\"{vertex.val}: {tmp},\")\n
      graph_adjacency_list.cpp
      /* Undirected graph class based on adjacency list */\nclass GraphAdjList {\n  public:\n    // Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n    unordered_map<Vertex *, vector<Vertex *>> adjList;\n\n    /* Remove specified node from vector */\n    void remove(vector<Vertex *> &vec, Vertex *vet) {\n        for (int i = 0; i < vec.size(); i++) {\n            if (vec[i] == vet) {\n                vec.erase(vec.begin() + i);\n                break;\n            }\n        }\n    }\n\n    /* Constructor */\n    GraphAdjList(const vector<vector<Vertex *>> &edges) {\n        // Add all vertices and edges\n        for (const vector<Vertex *> &edge : edges) {\n            addVertex(edge[0]);\n            addVertex(edge[1]);\n            addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* Get the number of vertices */\n    int size() {\n        return adjList.size();\n    }\n\n    /* Add edge */\n    void addEdge(Vertex *vet1, Vertex *vet2) {\n        if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n            throw invalid_argument(\"Vertex does not exist\");\n        // Add edge vet1 - vet2\n        adjList[vet1].push_back(vet2);\n        adjList[vet2].push_back(vet1);\n    }\n\n    /* Remove edge */\n    void removeEdge(Vertex *vet1, Vertex *vet2) {\n        if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n            throw invalid_argument(\"Vertex does not exist\");\n        // Remove edge vet1 - vet2\n        remove(adjList[vet1], vet2);\n        remove(adjList[vet2], vet1);\n    }\n\n    /* Add vertex */\n    void addVertex(Vertex *vet) {\n        if (adjList.count(vet))\n            return;\n        // Add a new linked list in the adjacency list\n        adjList[vet] = vector<Vertex *>();\n    }\n\n    /* Remove vertex */\n    void removeVertex(Vertex *vet) {\n        if (!adjList.count(vet))\n            throw invalid_argument(\"Vertex does not exist\");\n        // Remove the linked list corresponding to vertex vet in the adjacency list\n        adjList.erase(vet);\n        // Traverse the linked lists of other vertices and remove all edges containing vet\n        for (auto &adj : adjList) {\n            remove(adj.second, vet);\n        }\n    }\n\n    /* Print adjacency list */\n    void print() {\n        cout << \"Adjacency list =\" << endl;\n        for (auto &adj : adjList) {\n            const auto &key = adj.first;\n            const auto &vec = adj.second;\n            cout << key->val << \": \";\n            printVector(vetsToVals(vec));\n        }\n    }\n};\n
      graph_adjacency_list.java
      /* Undirected graph class based on adjacency list */\nclass GraphAdjList {\n    // Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n    Map<Vertex, List<Vertex>> adjList;\n\n    /* Constructor */\n    public GraphAdjList(Vertex[][] edges) {\n        this.adjList = new HashMap<>();\n        // Add all vertices and edges\n        for (Vertex[] edge : edges) {\n            addVertex(edge[0]);\n            addVertex(edge[1]);\n            addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* Get the number of vertices */\n    public int size() {\n        return adjList.size();\n    }\n\n    /* Add edge */\n    public void addEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw new IllegalArgumentException();\n        // Add edge vet1 - vet2\n        adjList.get(vet1).add(vet2);\n        adjList.get(vet2).add(vet1);\n    }\n\n    /* Remove edge */\n    public void removeEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw new IllegalArgumentException();\n        // Remove edge vet1 - vet2\n        adjList.get(vet1).remove(vet2);\n        adjList.get(vet2).remove(vet1);\n    }\n\n    /* Add vertex */\n    public void addVertex(Vertex vet) {\n        if (adjList.containsKey(vet))\n            return;\n        // Add a new linked list in the adjacency list\n        adjList.put(vet, new ArrayList<>());\n    }\n\n    /* Remove vertex */\n    public void removeVertex(Vertex vet) {\n        if (!adjList.containsKey(vet))\n            throw new IllegalArgumentException();\n        // Remove the linked list corresponding to vertex vet in the adjacency list\n        adjList.remove(vet);\n        // Traverse the linked lists of other vertices and remove all edges containing vet\n        for (List<Vertex> list : adjList.values()) {\n            list.remove(vet);\n        }\n    }\n\n    /* Print adjacency list */\n    public void print() {\n        System.out.println(\"Adjacency list =\");\n        for (Map.Entry<Vertex, List<Vertex>> pair : adjList.entrySet()) {\n            List<Integer> tmp = new ArrayList<>();\n            for (Vertex vertex : pair.getValue())\n                tmp.add(vertex.val);\n            System.out.println(pair.getKey().val + \": \" + tmp + \",\");\n        }\n    }\n}\n
      graph_adjacency_list.cs
      /* Undirected graph class based on adjacency list */\nclass GraphAdjList {\n    // Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n    public Dictionary<Vertex, List<Vertex>> adjList;\n\n    /* Constructor */\n    public GraphAdjList(Vertex[][] edges) {\n        adjList = [];\n        // Add all vertices and edges\n        foreach (Vertex[] edge in edges) {\n            AddVertex(edge[0]);\n            AddVertex(edge[1]);\n            AddEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* Get the number of vertices */\n    int Size() {\n        return adjList.Count;\n    }\n\n    /* Add edge */\n    public void AddEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\n            throw new InvalidOperationException();\n        // Add edge vet1 - vet2\n        adjList[vet1].Add(vet2);\n        adjList[vet2].Add(vet1);\n    }\n\n    /* Remove edge */\n    public void RemoveEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\n            throw new InvalidOperationException();\n        // Remove edge vet1 - vet2\n        adjList[vet1].Remove(vet2);\n        adjList[vet2].Remove(vet1);\n    }\n\n    /* Add vertex */\n    public void AddVertex(Vertex vet) {\n        if (adjList.ContainsKey(vet))\n            return;\n        // Add a new linked list in the adjacency list\n        adjList.Add(vet, []);\n    }\n\n    /* Remove vertex */\n    public void RemoveVertex(Vertex vet) {\n        if (!adjList.ContainsKey(vet))\n            throw new InvalidOperationException();\n        // Remove the linked list corresponding to vertex vet in the adjacency list\n        adjList.Remove(vet);\n        // Traverse the linked lists of other vertices and remove all edges containing vet\n        foreach (List<Vertex> list in adjList.Values) {\n            list.Remove(vet);\n        }\n    }\n\n    /* Print adjacency list */\n    public void Print() {\n        Console.WriteLine(\"Adjacency list =\");\n        foreach (KeyValuePair<Vertex, List<Vertex>> pair in adjList) {\n            List<int> tmp = [];\n            foreach (Vertex vertex in pair.Value)\n                tmp.Add(vertex.val);\n            Console.WriteLine(pair.Key.val + \": [\" + string.Join(\", \", tmp) + \"],\");\n        }\n    }\n}\n
      graph_adjacency_list.go
      /* Undirected graph class based on adjacency list */\ntype graphAdjList struct {\n    // Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n    adjList map[Vertex][]Vertex\n}\n\n/* Constructor */\nfunc newGraphAdjList(edges [][]Vertex) *graphAdjList {\n    g := &graphAdjList{\n        adjList: make(map[Vertex][]Vertex),\n    }\n    // Add all vertices and edges\n    for _, edge := range edges {\n        g.addVertex(edge[0])\n        g.addVertex(edge[1])\n        g.addEdge(edge[0], edge[1])\n    }\n    return g\n}\n\n/* Get the number of vertices */\nfunc (g *graphAdjList) size() int {\n    return len(g.adjList)\n}\n\n/* Add edge */\nfunc (g *graphAdjList) addEdge(vet1 Vertex, vet2 Vertex) {\n    _, ok1 := g.adjList[vet1]\n    _, ok2 := g.adjList[vet2]\n    if !ok1 || !ok2 || vet1 == vet2 {\n        panic(\"error\")\n    }\n    // Add edge vet1 - vet2, add anonymous struct{},\n    g.adjList[vet1] = append(g.adjList[vet1], vet2)\n    g.adjList[vet2] = append(g.adjList[vet2], vet1)\n}\n\n/* Remove edge */\nfunc (g *graphAdjList) removeEdge(vet1 Vertex, vet2 Vertex) {\n    _, ok1 := g.adjList[vet1]\n    _, ok2 := g.adjList[vet2]\n    if !ok1 || !ok2 || vet1 == vet2 {\n        panic(\"error\")\n    }\n    // Remove edge vet1 - vet2\n    g.adjList[vet1] = DeleteSliceElms(g.adjList[vet1], vet2)\n    g.adjList[vet2] = DeleteSliceElms(g.adjList[vet2], vet1)\n}\n\n/* Add vertex */\nfunc (g *graphAdjList) addVertex(vet Vertex) {\n    _, ok := g.adjList[vet]\n    if ok {\n        return\n    }\n    // Add a new linked list in the adjacency list\n    g.adjList[vet] = make([]Vertex, 0)\n}\n\n/* Remove vertex */\nfunc (g *graphAdjList) removeVertex(vet Vertex) {\n    _, ok := g.adjList[vet]\n    if !ok {\n        panic(\"error\")\n    }\n    // Remove the linked list corresponding to vertex vet in the adjacency list\n    delete(g.adjList, vet)\n    // Traverse the linked lists of other vertices and remove all edges containing vet\n    for v, list := range g.adjList {\n        g.adjList[v] = DeleteSliceElms(list, vet)\n    }\n}\n\n/* Print adjacency list */\nfunc (g *graphAdjList) print() {\n    var builder strings.Builder\n    fmt.Printf(\"Adjacency list = \\n\")\n    for k, v := range g.adjList {\n        builder.WriteString(\"\\t\\t\" + strconv.Itoa(k.Val) + \": \")\n        for _, vet := range v {\n            builder.WriteString(strconv.Itoa(vet.Val) + \" \")\n        }\n        fmt.Println(builder.String())\n        builder.Reset()\n    }\n}\n
      graph_adjacency_list.swift
      /* Undirected graph class based on adjacency list */\nclass GraphAdjList {\n    // Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n    public private(set) var adjList: [Vertex: [Vertex]]\n\n    /* Constructor */\n    public init(edges: [[Vertex]]) {\n        adjList = [:]\n        // Add all vertices and edges\n        for edge in edges {\n            addVertex(vet: edge[0])\n            addVertex(vet: edge[1])\n            addEdge(vet1: edge[0], vet2: edge[1])\n        }\n    }\n\n    /* Get the number of vertices */\n    public func size() -> Int {\n        adjList.count\n    }\n\n    /* Add edge */\n    public func addEdge(vet1: Vertex, vet2: Vertex) {\n        if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\n            fatalError(\"Invalid parameter\")\n        }\n        // Add edge vet1 - vet2\n        adjList[vet1]?.append(vet2)\n        adjList[vet2]?.append(vet1)\n    }\n\n    /* Remove edge */\n    public func removeEdge(vet1: Vertex, vet2: Vertex) {\n        if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\n            fatalError(\"Invalid parameter\")\n        }\n        // Remove edge vet1 - vet2\n        adjList[vet1]?.removeAll { $0 == vet2 }\n        adjList[vet2]?.removeAll { $0 == vet1 }\n    }\n\n    /* Add vertex */\n    public func addVertex(vet: Vertex) {\n        if adjList[vet] != nil {\n            return\n        }\n        // Add a new linked list in the adjacency list\n        adjList[vet] = []\n    }\n\n    /* Remove vertex */\n    public func removeVertex(vet: Vertex) {\n        if adjList[vet] == nil {\n            fatalError(\"Invalid parameter\")\n        }\n        // Remove the linked list corresponding to vertex vet in the adjacency list\n        adjList.removeValue(forKey: vet)\n        // Traverse the linked lists of other vertices and remove all edges containing vet\n        for key in adjList.keys {\n            adjList[key]?.removeAll { $0 == vet }\n        }\n    }\n\n    /* Print adjacency list */\n    public func print() {\n        Swift.print(\"Adjacency list =\")\n        for (vertex, list) in adjList {\n            let list = list.map { $0.val }\n            Swift.print(\"\\(vertex.val): \\(list),\")\n        }\n    }\n}\n
      graph_adjacency_list.js
      /* Undirected graph class based on adjacency list */\nclass GraphAdjList {\n    // Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n    adjList;\n\n    /* Constructor */\n    constructor(edges) {\n        this.adjList = new Map();\n        // Add all vertices and edges\n        for (const edge of edges) {\n            this.addVertex(edge[0]);\n            this.addVertex(edge[1]);\n            this.addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* Get the number of vertices */\n    size() {\n        return this.adjList.size;\n    }\n\n    /* Add edge */\n    addEdge(vet1, vet2) {\n        if (\n            !this.adjList.has(vet1) ||\n            !this.adjList.has(vet2) ||\n            vet1 === vet2\n        ) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // Add edge vet1 - vet2\n        this.adjList.get(vet1).push(vet2);\n        this.adjList.get(vet2).push(vet1);\n    }\n\n    /* Remove edge */\n    removeEdge(vet1, vet2) {\n        if (\n            !this.adjList.has(vet1) ||\n            !this.adjList.has(vet2) ||\n            vet1 === vet2 ||\n            this.adjList.get(vet1).indexOf(vet2) === -1\n        ) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // Remove edge vet1 - vet2\n        this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\n        this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n    }\n\n    /* Add vertex */\n    addVertex(vet) {\n        if (this.adjList.has(vet)) return;\n        // Add a new linked list in the adjacency list\n        this.adjList.set(vet, []);\n    }\n\n    /* Remove vertex */\n    removeVertex(vet) {\n        if (!this.adjList.has(vet)) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // Remove the linked list corresponding to vertex vet in the adjacency list\n        this.adjList.delete(vet);\n        // Traverse the linked lists of other vertices and remove all edges containing vet\n        for (const set of this.adjList.values()) {\n            const index = set.indexOf(vet);\n            if (index > -1) {\n                set.splice(index, 1);\n            }\n        }\n    }\n\n    /* Print adjacency list */\n    print() {\n        console.log('Adjacency list =');\n        for (const [key, value] of this.adjList) {\n            const tmp = [];\n            for (const vertex of value) {\n                tmp.push(vertex.val);\n            }\n            console.log(key.val + ': ' + tmp.join());\n        }\n    }\n}\n
      graph_adjacency_list.ts
      /* Undirected graph class based on adjacency list */\nclass GraphAdjList {\n    // Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n    adjList: Map<Vertex, Vertex[]>;\n\n    /* Constructor */\n    constructor(edges: Vertex[][]) {\n        this.adjList = new Map();\n        // Add all vertices and edges\n        for (const edge of edges) {\n            this.addVertex(edge[0]);\n            this.addVertex(edge[1]);\n            this.addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* Get the number of vertices */\n    size(): number {\n        return this.adjList.size;\n    }\n\n    /* Add edge */\n    addEdge(vet1: Vertex, vet2: Vertex): void {\n        if (\n            !this.adjList.has(vet1) ||\n            !this.adjList.has(vet2) ||\n            vet1 === vet2\n        ) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // Add edge vet1 - vet2\n        this.adjList.get(vet1).push(vet2);\n        this.adjList.get(vet2).push(vet1);\n    }\n\n    /* Remove edge */\n    removeEdge(vet1: Vertex, vet2: Vertex): void {\n        if (\n            !this.adjList.has(vet1) ||\n            !this.adjList.has(vet2) ||\n            vet1 === vet2 ||\n            this.adjList.get(vet1).indexOf(vet2) === -1\n        ) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // Remove edge vet1 - vet2\n        this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\n        this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n    }\n\n    /* Add vertex */\n    addVertex(vet: Vertex): void {\n        if (this.adjList.has(vet)) return;\n        // Add a new linked list in the adjacency list\n        this.adjList.set(vet, []);\n    }\n\n    /* Remove vertex */\n    removeVertex(vet: Vertex): void {\n        if (!this.adjList.has(vet)) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // Remove the linked list corresponding to vertex vet in the adjacency list\n        this.adjList.delete(vet);\n        // Traverse the linked lists of other vertices and remove all edges containing vet\n        for (const set of this.adjList.values()) {\n            const index: number = set.indexOf(vet);\n            if (index > -1) {\n                set.splice(index, 1);\n            }\n        }\n    }\n\n    /* Print adjacency list */\n    print(): void {\n        console.log('Adjacency list =');\n        for (const [key, value] of this.adjList.entries()) {\n            const tmp = [];\n            for (const vertex of value) {\n                tmp.push(vertex.val);\n            }\n            console.log(key.val + ': ' + tmp.join());\n        }\n    }\n}\n
      graph_adjacency_list.dart
      /* Undirected graph class based on adjacency list */\nclass GraphAdjList {\n  // Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n  Map<Vertex, List<Vertex>> adjList = {};\n\n  /* Constructor */\n  GraphAdjList(List<List<Vertex>> edges) {\n    for (List<Vertex> edge in edges) {\n      addVertex(edge[0]);\n      addVertex(edge[1]);\n      addEdge(edge[0], edge[1]);\n    }\n  }\n\n  /* Get the number of vertices */\n  int size() {\n    return adjList.length;\n  }\n\n  /* Add edge */\n  void addEdge(Vertex vet1, Vertex vet2) {\n    if (!adjList.containsKey(vet1) ||\n        !adjList.containsKey(vet2) ||\n        vet1 == vet2) {\n      throw ArgumentError;\n    }\n    // Add edge vet1 - vet2\n    adjList[vet1]!.add(vet2);\n    adjList[vet2]!.add(vet1);\n  }\n\n  /* Remove edge */\n  void removeEdge(Vertex vet1, Vertex vet2) {\n    if (!adjList.containsKey(vet1) ||\n        !adjList.containsKey(vet2) ||\n        vet1 == vet2) {\n      throw ArgumentError;\n    }\n    // Remove edge vet1 - vet2\n    adjList[vet1]!.remove(vet2);\n    adjList[vet2]!.remove(vet1);\n  }\n\n  /* Add vertex */\n  void addVertex(Vertex vet) {\n    if (adjList.containsKey(vet)) return;\n    // Add a new linked list in the adjacency list\n    adjList[vet] = [];\n  }\n\n  /* Remove vertex */\n  void removeVertex(Vertex vet) {\n    if (!adjList.containsKey(vet)) {\n      throw ArgumentError;\n    }\n    // Remove the linked list corresponding to vertex vet in the adjacency list\n    adjList.remove(vet);\n    // Traverse the linked lists of other vertices and remove all edges containing vet\n    adjList.forEach((key, value) {\n      value.remove(vet);\n    });\n  }\n\n  /* Print adjacency list */\n  void printAdjList() {\n    print(\"Adjacency list =\");\n    adjList.forEach((key, value) {\n      List<int> tmp = [];\n      for (Vertex vertex in value) {\n        tmp.add(vertex.val);\n      }\n      print(\"${key.val}: $tmp,\");\n    });\n  }\n}\n
      graph_adjacency_list.rs
      /* Undirected graph type based on adjacency list */\npub struct GraphAdjList {\n    // Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n    pub adj_list: HashMap<Vertex, Vec<Vertex>>, // maybe HashSet<Vertex> for value part is better?\n}\n\nimpl GraphAdjList {\n    /* Constructor */\n    pub fn new(edges: Vec<[Vertex; 2]>) -> Self {\n        let mut graph = GraphAdjList {\n            adj_list: HashMap::new(),\n        };\n        // Add all vertices and edges\n        for edge in edges {\n            graph.add_vertex(edge[0]);\n            graph.add_vertex(edge[1]);\n            graph.add_edge(edge[0], edge[1]);\n        }\n\n        graph\n    }\n\n    /* Get the number of vertices */\n    #[allow(unused)]\n    pub fn size(&self) -> usize {\n        self.adj_list.len()\n    }\n\n    /* Add edge */\n    pub fn add_edge(&mut self, vet1: Vertex, vet2: Vertex) {\n        if vet1 == vet2 {\n            panic!(\"value error\");\n        }\n        // Add edge vet1 - vet2\n        self.adj_list.entry(vet1).or_default().push(vet2);\n        self.adj_list.entry(vet2).or_default().push(vet1);\n    }\n\n    /* Remove edge */\n    #[allow(unused)]\n    pub fn remove_edge(&mut self, vet1: Vertex, vet2: Vertex) {\n        if vet1 == vet2 {\n            panic!(\"value error\");\n        }\n        // Remove edge vet1 - vet2\n        self.adj_list\n            .entry(vet1)\n            .and_modify(|v| v.retain(|&e| e != vet2));\n        self.adj_list\n            .entry(vet2)\n            .and_modify(|v| v.retain(|&e| e != vet1));\n    }\n\n    /* Add vertex */\n    pub fn add_vertex(&mut self, vet: Vertex) {\n        if self.adj_list.contains_key(&vet) {\n            return;\n        }\n        // Add a new linked list in the adjacency list\n        self.adj_list.insert(vet, vec![]);\n    }\n\n    /* Remove vertex */\n    #[allow(unused)]\n    pub fn remove_vertex(&mut self, vet: Vertex) {\n        // Remove the linked list corresponding to vertex vet in the adjacency list\n        self.adj_list.remove(&vet);\n        // Traverse the linked lists of other vertices and remove all edges containing vet\n        for list in self.adj_list.values_mut() {\n            list.retain(|&v| v != vet);\n        }\n    }\n\n    /* Print adjacency list */\n    pub fn print(&self) {\n        println!(\"Adjacency list =\");\n        for (vertex, list) in &self.adj_list {\n            let list = list.iter().map(|vertex| vertex.val).collect::<Vec<i32>>();\n            println!(\"{}: {:?},\", vertex.val, list);\n        }\n    }\n}\n
      graph_adjacency_list.c
      /* Node structure */\ntypedef struct AdjListNode {\n    Vertex *vertex;           // Vertex\n    struct AdjListNode *next; // Successor node\n} AdjListNode;\n\n/* Find node corresponding to vertex */\nAdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {\n    for (int i = 0; i < graph->size; i++) {\n        if (graph->heads[i]->vertex == vet) {\n            return graph->heads[i];\n        }\n    }\n    return NULL;\n}\n\n/* Add edge helper function */\nvoid addEdgeHelper(AdjListNode *head, Vertex *vet) {\n    AdjListNode *node = (AdjListNode *)malloc(sizeof(AdjListNode));\n    node->vertex = vet;\n    // Head insertion\n    node->next = head->next;\n    head->next = node;\n}\n\n/* Remove edge helper function */\nvoid removeEdgeHelper(AdjListNode *head, Vertex *vet) {\n    AdjListNode *pre = head;\n    AdjListNode *cur = head->next;\n    // Search for node corresponding to vet in list\n    while (cur != NULL && cur->vertex != vet) {\n        pre = cur;\n        cur = cur->next;\n    }\n    if (cur == NULL)\n        return;\n    // Remove node corresponding to vet from list\n    pre->next = cur->next;\n    // Free memory\n    free(cur);\n}\n\n/* Undirected graph class based on adjacency list */\ntypedef struct {\n    AdjListNode *heads[MAX_SIZE]; // Node array\n    int size;                     // Node count\n} GraphAdjList;\n\n/* Constructor */\nGraphAdjList *newGraphAdjList() {\n    GraphAdjList *graph = (GraphAdjList *)malloc(sizeof(GraphAdjList));\n    if (!graph) {\n        return NULL;\n    }\n    graph->size = 0;\n    for (int i = 0; i < MAX_SIZE; i++) {\n        graph->heads[i] = NULL;\n    }\n    return graph;\n}\n\n/* Destructor */\nvoid delGraphAdjList(GraphAdjList *graph) {\n    for (int i = 0; i < graph->size; i++) {\n        AdjListNode *cur = graph->heads[i];\n        while (cur != NULL) {\n            AdjListNode *next = cur->next;\n            if (cur != graph->heads[i]) {\n                free(cur);\n            }\n            cur = next;\n        }\n        free(graph->heads[i]->vertex);\n        free(graph->heads[i]);\n    }\n    free(graph);\n}\n\n/* Find node corresponding to vertex */\nAdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {\n    for (int i = 0; i < graph->size; i++) {\n        if (graph->heads[i]->vertex == vet) {\n            return graph->heads[i];\n        }\n    }\n    return NULL;\n}\n\n/* Add edge */\nvoid addEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) {\n    AdjListNode *head1 = findNode(graph, vet1);\n    AdjListNode *head2 = findNode(graph, vet2);\n    assert(head1 != NULL && head2 != NULL && head1 != head2);\n    // Add edge vet1 - vet2\n    addEdgeHelper(head1, vet2);\n    addEdgeHelper(head2, vet1);\n}\n\n/* Remove edge */\nvoid removeEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) {\n    AdjListNode *head1 = findNode(graph, vet1);\n    AdjListNode *head2 = findNode(graph, vet2);\n    assert(head1 != NULL && head2 != NULL);\n    // Remove edge vet1 - vet2\n    removeEdgeHelper(head1, head2->vertex);\n    removeEdgeHelper(head2, head1->vertex);\n}\n\n/* Add vertex */\nvoid addVertex(GraphAdjList *graph, Vertex *vet) {\n    assert(graph != NULL && graph->size < MAX_SIZE);\n    AdjListNode *head = (AdjListNode *)malloc(sizeof(AdjListNode));\n    head->vertex = vet;\n    head->next = NULL;\n    // Add a new linked list in the adjacency list\n    graph->heads[graph->size++] = head;\n}\n\n/* Remove vertex */\nvoid removeVertex(GraphAdjList *graph, Vertex *vet) {\n    AdjListNode *node = findNode(graph, vet);\n    assert(node != NULL);\n    // Remove the linked list corresponding to vertex vet in the adjacency list\n    AdjListNode *cur = node, *pre = NULL;\n    while (cur) {\n        pre = cur;\n        cur = cur->next;\n        free(pre);\n    }\n    // Traverse the linked lists of other vertices and remove all edges containing vet\n    for (int i = 0; i < graph->size; i++) {\n        cur = graph->heads[i];\n        pre = NULL;\n        while (cur) {\n            pre = cur;\n            cur = cur->next;\n            if (cur && cur->vertex == vet) {\n                pre->next = cur->next;\n                free(cur);\n                break;\n            }\n        }\n    }\n    // Move vertices after this vertex forward to fill gap\n    int i;\n    for (i = 0; i < graph->size; i++) {\n        if (graph->heads[i] == node)\n            break;\n    }\n    for (int j = i; j < graph->size - 1; j++) {\n        graph->heads[j] = graph->heads[j + 1];\n    }\n    graph->size--;\n    free(vet);\n}\n
      graph_adjacency_list.kt
      /* Undirected graph class based on adjacency list */\nclass GraphAdjList(edges: Array<Array<Vertex?>>) {\n    // Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n    val adjList = HashMap<Vertex, MutableList<Vertex>>()\n\n    /* Constructor */\n    init {\n        // Add all vertices and edges\n        for (edge in edges) {\n            addVertex(edge[0]!!)\n            addVertex(edge[1]!!)\n            addEdge(edge[0]!!, edge[1]!!)\n        }\n    }\n\n    /* Get the number of vertices */\n    fun size(): Int {\n        return adjList.size\n    }\n\n    /* Add edge */\n    fun addEdge(vet1: Vertex, vet2: Vertex) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw IllegalArgumentException()\n        // Add edge vet1 - vet2\n        adjList[vet1]?.add(vet2)\n        adjList[vet2]?.add(vet1)\n    }\n\n    /* Remove edge */\n    fun removeEdge(vet1: Vertex, vet2: Vertex) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw IllegalArgumentException()\n        // Remove edge vet1 - vet2\n        adjList[vet1]?.remove(vet2)\n        adjList[vet2]?.remove(vet1)\n    }\n\n    /* Add vertex */\n    fun addVertex(vet: Vertex) {\n        if (adjList.containsKey(vet))\n            return\n        // Add a new linked list in the adjacency list\n        adjList[vet] = mutableListOf()\n    }\n\n    /* Remove vertex */\n    fun removeVertex(vet: Vertex) {\n        if (!adjList.containsKey(vet))\n            throw IllegalArgumentException()\n        // Remove the linked list corresponding to vertex vet in the adjacency list\n        adjList.remove(vet)\n        // Traverse the linked lists of other vertices and remove all edges containing vet\n        for (list in adjList.values) {\n            list.remove(vet)\n        }\n    }\n\n    /* Print adjacency list */\n    fun print() {\n        println(\"Adjacency list =\")\n        for (pair in adjList.entries) {\n            val tmp = mutableListOf<Int>()\n            for (vertex in pair.value) {\n                tmp.add(vertex._val)\n            }\n            println(\"${pair.key._val}: $tmp,\")\n        }\n    }\n}\n
      graph_adjacency_list.rb
      ### Undirected graph class based on adjacency list ###\nclass GraphAdjList\n  attr_reader :adj_list\n\n  ### Constructor ###\n  def initialize(edges)\n    # Adjacency list, key: vertex, value: all adjacent vertices of that vertex\n    @adj_list = {}\n    # Add all vertices and edges\n    for edge in edges\n      add_vertex(edge[0])\n      add_vertex(edge[1])\n      add_edge(edge[0], edge[1])\n    end\n  end\n\n  ### Get number of vertices ###\n  def size\n    @adj_list.length\n  end\n\n  ### Add edge ###\n  def add_edge(vet1, vet2)\n    raise ArgumentError if !@adj_list.include?(vet1) || !@adj_list.include?(vet2)\n\n    @adj_list[vet1] << vet2\n    @adj_list[vet2] << vet1\n  end\n\n  ### Delete edge ###\n  def remove_edge(vet1, vet2)\n    raise ArgumentError if !@adj_list.include?(vet1) || !@adj_list.include?(vet2)\n\n    # Remove edge vet1 - vet2\n    @adj_list[vet1].delete(vet2)\n    @adj_list[vet2].delete(vet1)\n  end\n\n  ### Add vertex ###\n  def add_vertex(vet)\n    return if @adj_list.include?(vet)\n\n    # Add a new linked list in the adjacency list\n    @adj_list[vet] = []\n  end\n\n  ### Delete vertex ###\n  def remove_vertex(vet)\n    raise ArgumentError unless @adj_list.include?(vet)\n\n    # Remove the linked list corresponding to vertex vet in the adjacency list\n    @adj_list.delete(vet)\n    # Traverse the linked lists of other vertices and remove all edges containing vet\n    for vertex in @adj_list\n      @adj_list[vertex.first].delete(vet) if @adj_list[vertex.first].include?(vet)\n    end\n  end\n\n  ### Print adjacency list ###\n  def __print__\n    puts 'Adjacency list ='\n    for vertex in @adj_list\n      tmp = @adj_list[vertex.first].map { |v| v.val }\n      puts \"#{vertex.first.val}: #{tmp},\"\n    end\n  end\nend\n
      "},{"location":"chapter_graph/graph_operations/#923-efficiency-comparison","title":"9.2.3 \u00a0 Efficiency Comparison","text":"

      Assuming the graph has \\(n\\) vertices and \\(m\\) edges, Table 9-2 compares the time efficiency and space efficiency of adjacency matrices and adjacency lists. Note that the adjacency list (linked list) corresponds to the implementation in this text, while the adjacency list (hash table) refers specifically to the implementation where all linked lists are replaced with hash tables.

      Table 9-2 \u00a0 Comparison of adjacency matrix and adjacency list

      Adjacency matrix Adjacency list (linked list) Adjacency list (hash table) Determine adjacency \\(O(1)\\) \\(O(n)\\) \\(O(1)\\) Add an edge \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) Remove an edge \\(O(1)\\) \\(O(n)\\) \\(O(1)\\) Add a vertex \\(O(n)\\) \\(O(1)\\) \\(O(1)\\) Remove a vertex \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n)\\) Memory space usage \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n + m)\\)

      Observing Table 9-2, it appears that the adjacency list (hash table) has the best time efficiency and space efficiency. However, in practice, operating on edges in the adjacency matrix is more efficient, requiring only a single array access or assignment operation. Overall, adjacency matrices embody the principle of \"trading space for time\", while adjacency lists embody \"trading time for space\".

      "},{"location":"chapter_graph/graph_traversal/","title":"9.3 \u00a0 Graph Traversal","text":"

      Trees represent \"one-to-many\" relationships, while graphs have a higher degree of freedom and can represent any \"many-to-many\" relationships. Therefore, we can view trees as a special case of graphs. Clearly, tree traversal operations are also a special case of graph traversal operations.

      Both graphs and trees require the application of search algorithms to implement traversal operations. Graph traversal methods can also be divided into two types: breadth-first traversal and depth-first traversal.

      "},{"location":"chapter_graph/graph_traversal/#931-breadth-first-search","title":"9.3.1 \u00a0 Breadth-First Search","text":"

      Breadth-first search is a near-to-far traversal method that, starting from a certain node, always prioritizes visiting the nearest vertices and expands outward layer by layer. As shown in Figure 9-9, starting from the top-left vertex, first traverse all adjacent vertices of that vertex, then traverse all adjacent vertices of the next vertex, and so on, until all vertices have been visited.

      Figure 9-9 \u00a0 Breadth-first search of a graph

      "},{"location":"chapter_graph/graph_traversal/#1-algorithm-implementation","title":"1. \u00a0 Algorithm Implementation","text":"

      BFS is typically implemented with the help of a queue, as shown in the code below. The queue has a \"first in, first out\" property, which aligns with the BFS idea of \"near to far\".

      1. Add the starting vertex startVet to the queue and begin the loop.
      2. In each iteration of the loop, pop the vertex at the front of the queue and record it as visited, then add all adjacent vertices of that vertex to the back of the queue.
      3. Repeat step 2. until all vertices have been visited.

      To prevent revisiting vertices, we use a hash set visited to record which nodes have been visited.

      Tip

      A hash set can be viewed as a hash table that stores only key without storing value. It can perform addition, deletion, lookup, and modification operations on key in \\(O(1)\\) time complexity. Based on the uniqueness of key, hash sets are typically used for data deduplication and similar scenarios.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_bfs.py
      def graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n    \"\"\"Breadth-first traversal\"\"\"\n    # Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\n    # Vertex traversal sequence\n    res = []\n    # Hash set for recording vertices that have been visited\n    visited = set[Vertex]([start_vet])\n    # Queue used to implement BFS\n    que = deque[Vertex]([start_vet])\n    # Starting from vertex vet, loop until all vertices are visited\n    while len(que) > 0:\n        vet = que.popleft()  # Dequeue the front vertex\n        res.append(vet)  # Record visited vertex\n        # Traverse all adjacent vertices of this vertex\n        for adj_vet in graph.adj_list[vet]:\n            if adj_vet in visited:\n                continue  # Skip vertices that have been visited\n            que.append(adj_vet)  # Only enqueue unvisited vertices\n            visited.add(adj_vet)  # Mark this vertex as visited\n    # Return vertex traversal sequence\n    return res\n
      graph_bfs.cpp
      /* Breadth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nvector<Vertex *> graphBFS(GraphAdjList &graph, Vertex *startVet) {\n    // Vertex traversal sequence\n    vector<Vertex *> res;\n    // Hash set for recording vertices that have been visited\n    unordered_set<Vertex *> visited = {startVet};\n    // Queue used to implement BFS\n    queue<Vertex *> que;\n    que.push(startVet);\n    // Starting from vertex vet, loop until all vertices are visited\n    while (!que.empty()) {\n        Vertex *vet = que.front();\n        que.pop();          // Dequeue the front vertex\n        res.push_back(vet); // Record visited vertex\n        // Traverse all adjacent vertices of this vertex\n        for (auto adjVet : graph.adjList[vet]) {\n            if (visited.count(adjVet))\n                continue;            // Skip vertices that have been visited\n            que.push(adjVet);        // Only enqueue unvisited vertices\n            visited.emplace(adjVet); // Mark this vertex as visited\n        }\n    }\n    // Return vertex traversal sequence\n    return res;\n}\n
      graph_bfs.java
      /* Breadth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nList<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {\n    // Vertex traversal sequence\n    List<Vertex> res = new ArrayList<>();\n    // Hash set for recording vertices that have been visited\n    Set<Vertex> visited = new HashSet<>();\n    visited.add(startVet);\n    // Queue used to implement BFS\n    Queue<Vertex> que = new LinkedList<>();\n    que.offer(startVet);\n    // Starting from vertex vet, loop until all vertices are visited\n    while (!que.isEmpty()) {\n        Vertex vet = que.poll(); // Dequeue the front vertex\n        res.add(vet);            // Record visited vertex\n        // Traverse all adjacent vertices of this vertex\n        for (Vertex adjVet : graph.adjList.get(vet)) {\n            if (visited.contains(adjVet))\n                continue;        // Skip vertices that have been visited\n            que.offer(adjVet);   // Only enqueue unvisited vertices\n            visited.add(adjVet); // Mark this vertex as visited\n        }\n    }\n    // Return vertex traversal sequence\n    return res;\n}\n
      graph_bfs.cs
      /* Breadth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nList<Vertex> GraphBFS(GraphAdjList graph, Vertex startVet) {\n    // Vertex traversal sequence\n    List<Vertex> res = [];\n    // Hash set for recording vertices that have been visited\n    HashSet<Vertex> visited = [startVet];\n    // Queue used to implement BFS\n    Queue<Vertex> que = new();\n    que.Enqueue(startVet);\n    // Starting from vertex vet, loop until all vertices are visited\n    while (que.Count > 0) {\n        Vertex vet = que.Dequeue(); // Dequeue the front vertex\n        res.Add(vet);               // Record visited vertex\n        foreach (Vertex adjVet in graph.adjList[vet]) {\n            if (visited.Contains(adjVet)) {\n                continue;          // Skip vertices that have been visited\n            }\n            que.Enqueue(adjVet);   // Only enqueue unvisited vertices\n            visited.Add(adjVet);   // Mark this vertex as visited\n        }\n    }\n\n    // Return vertex traversal sequence\n    return res;\n}\n
      graph_bfs.go
      /* Breadth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfunc graphBFS(g *graphAdjList, startVet Vertex) []Vertex {\n    // Vertex traversal sequence\n    res := make([]Vertex, 0)\n    // Hash set for recording vertices that have been visited\n    visited := make(map[Vertex]struct{})\n    visited[startVet] = struct{}{}\n    // Queue used to implement BFS, using slice to simulate queue\n    queue := make([]Vertex, 0)\n    queue = append(queue, startVet)\n    // Starting from vertex vet, loop until all vertices are visited\n    for len(queue) > 0 {\n        // Dequeue the front vertex\n        vet := queue[0]\n        queue = queue[1:]\n        // Record visited vertex\n        res = append(res, vet)\n        // Traverse all adjacent vertices of this vertex\n        for _, adjVet := range g.adjList[vet] {\n            _, isExist := visited[adjVet]\n            // Only enqueue unvisited vertices\n            if !isExist {\n                queue = append(queue, adjVet)\n                visited[adjVet] = struct{}{}\n            }\n        }\n    }\n    // Return vertex traversal sequence\n    return res\n}\n
      graph_bfs.swift
      /* Breadth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfunc graphBFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {\n    // Vertex traversal sequence\n    var res: [Vertex] = []\n    // Hash set for recording vertices that have been visited\n    var visited: Set<Vertex> = [startVet]\n    // Queue used to implement BFS\n    var que: [Vertex] = [startVet]\n    // Starting from vertex vet, loop until all vertices are visited\n    while !que.isEmpty {\n        let vet = que.removeFirst() // Dequeue the front vertex\n        res.append(vet) // Record visited vertex\n        // Traverse all adjacent vertices of this vertex\n        for adjVet in graph.adjList[vet] ?? [] {\n            if visited.contains(adjVet) {\n                continue // Skip vertices that have been visited\n            }\n            que.append(adjVet) // Only enqueue unvisited vertices\n            visited.insert(adjVet) // Mark this vertex as visited\n        }\n    }\n    // Return vertex traversal sequence\n    return res\n}\n
      graph_bfs.js
      /* Breadth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfunction graphBFS(graph, startVet) {\n    // Vertex traversal sequence\n    const res = [];\n    // Hash set for recording vertices that have been visited\n    const visited = new Set();\n    visited.add(startVet);\n    // Queue used to implement BFS\n    const que = [startVet];\n    // Starting from vertex vet, loop until all vertices are visited\n    while (que.length) {\n        const vet = que.shift(); // Dequeue the front vertex\n        res.push(vet); // Record visited vertex\n        // Traverse all adjacent vertices of this vertex\n        for (const adjVet of graph.adjList.get(vet) ?? []) {\n            if (visited.has(adjVet)) {\n                continue; // Skip vertices that have been visited\n            }\n            que.push(adjVet); // Only enqueue unvisited vertices\n            visited.add(adjVet); // Mark this vertex as visited\n        }\n    }\n    // Return vertex traversal sequence\n    return res;\n}\n
      graph_bfs.ts
      /* Breadth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfunction graphBFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n    // Vertex traversal sequence\n    const res: Vertex[] = [];\n    // Hash set for recording vertices that have been visited\n    const visited: Set<Vertex> = new Set();\n    visited.add(startVet);\n    // Queue used to implement BFS\n    const que = [startVet];\n    // Starting from vertex vet, loop until all vertices are visited\n    while (que.length) {\n        const vet = que.shift(); // Dequeue the front vertex\n        res.push(vet); // Record visited vertex\n        // Traverse all adjacent vertices of this vertex\n        for (const adjVet of graph.adjList.get(vet) ?? []) {\n            if (visited.has(adjVet)) {\n                continue; // Skip vertices that have been visited\n            }\n            que.push(adjVet); // Only enqueue unvisited\n            visited.add(adjVet); // Mark this vertex as visited\n        }\n    }\n    // Return vertex traversal sequence\n    return res;\n}\n
      graph_bfs.dart
      /* Breadth-first traversal */\nList<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {\n  // Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\n  // Vertex traversal sequence\n  List<Vertex> res = [];\n  // Hash set for recording vertices that have been visited\n  Set<Vertex> visited = {};\n  visited.add(startVet);\n  // Queue used to implement BFS\n  Queue<Vertex> que = Queue();\n  que.add(startVet);\n  // Starting from vertex vet, loop until all vertices are visited\n  while (que.isNotEmpty) {\n    Vertex vet = que.removeFirst(); // Dequeue the front vertex\n    res.add(vet); // Record visited vertex\n    // Traverse all adjacent vertices of this vertex\n    for (Vertex adjVet in graph.adjList[vet]!) {\n      if (visited.contains(adjVet)) {\n        continue; // Skip vertices that have been visited\n      }\n      que.add(adjVet); // Only enqueue unvisited vertices\n      visited.add(adjVet); // Mark this vertex as visited\n    }\n  }\n  // Return vertex traversal sequence\n  return res;\n}\n
      graph_bfs.rs
      /* Breadth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfn graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {\n    // Vertex traversal sequence\n    let mut res = vec![];\n    // Hash set for recording vertices that have been visited\n    let mut visited = HashSet::new();\n    visited.insert(start_vet);\n    // Queue used to implement BFS\n    let mut que = VecDeque::new();\n    que.push_back(start_vet);\n    // Starting from vertex vet, loop until all vertices are visited\n    while let Some(vet) = que.pop_front() {\n        res.push(vet); // Record visited vertex\n\n        // Traverse all adjacent vertices of this vertex\n        if let Some(adj_vets) = graph.adj_list.get(&vet) {\n            for &adj_vet in adj_vets {\n                if visited.contains(&adj_vet) {\n                    continue; // Skip vertices that have been visited\n                }\n                que.push_back(adj_vet); // Only enqueue unvisited vertices\n                visited.insert(adj_vet); // Mark this vertex as visited\n            }\n        }\n    }\n    // Return vertex traversal sequence\n    res\n}\n
      graph_bfs.c
      /* Node queue structure */\ntypedef struct {\n    Vertex *vertices[MAX_SIZE];\n    int front, rear, size;\n} Queue;\n\n/* Constructor */\nQueue *newQueue() {\n    Queue *q = (Queue *)malloc(sizeof(Queue));\n    q->front = q->rear = q->size = 0;\n    return q;\n}\n\n/* Check if the queue is empty */\nint isEmpty(Queue *q) {\n    return q->size == 0;\n}\n\n/* Enqueue operation */\nvoid enqueue(Queue *q, Vertex *vet) {\n    q->vertices[q->rear] = vet;\n    q->rear = (q->rear + 1) % MAX_SIZE;\n    q->size++;\n}\n\n/* Dequeue operation */\nVertex *dequeue(Queue *q) {\n    Vertex *vet = q->vertices[q->front];\n    q->front = (q->front + 1) % MAX_SIZE;\n    q->size--;\n    return vet;\n}\n\n/* Check if vertex has been visited */\nint isVisited(Vertex **visited, int size, Vertex *vet) {\n    // Traverse to find node using O(n) time\n    for (int i = 0; i < size; i++) {\n        if (visited[i] == vet)\n            return 1;\n    }\n    return 0;\n}\n\n/* Breadth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nvoid graphBFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize, Vertex **visited, int *visitedSize) {\n    // Queue used to implement BFS\n    Queue *queue = newQueue();\n    enqueue(queue, startVet);\n    visited[(*visitedSize)++] = startVet;\n    // Starting from vertex vet, loop until all vertices are visited\n    while (!isEmpty(queue)) {\n        Vertex *vet = dequeue(queue); // Dequeue the front vertex\n        res[(*resSize)++] = vet;      // Record visited vertex\n        // Traverse all adjacent vertices of this vertex\n        AdjListNode *node = findNode(graph, vet);\n        while (node != NULL) {\n            // Skip vertices that have been visited\n            if (!isVisited(visited, *visitedSize, node->vertex)) {\n                enqueue(queue, node->vertex);             // Only enqueue unvisited vertices\n                visited[(*visitedSize)++] = node->vertex; // Mark this vertex as visited\n            }\n            node = node->next;\n        }\n    }\n    // Free memory\n    free(queue);\n}\n
      graph_bfs.kt
      /* Breadth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfun graphBFS(graph: GraphAdjList, startVet: Vertex): MutableList<Vertex?> {\n    // Vertex traversal sequence\n    val res = mutableListOf<Vertex?>()\n    // Hash set for recording vertices that have been visited\n    val visited = HashSet<Vertex>()\n    visited.add(startVet)\n    // Queue used to implement BFS\n    val que = LinkedList<Vertex>()\n    que.offer(startVet)\n    // Starting from vertex vet, loop until all vertices are visited\n    while (!que.isEmpty()) {\n        val vet = que.poll() // Dequeue the front vertex\n        res.add(vet)         // Record visited vertex\n        // Traverse all adjacent vertices of this vertex\n        for (adjVet in graph.adjList[vet]!!) {\n            if (visited.contains(adjVet))\n                continue        // Skip vertices that have been visited\n            que.offer(adjVet)   // Only enqueue unvisited vertices\n            visited.add(adjVet) // Mark this vertex as visited\n        }\n    }\n    // Return vertex traversal sequence\n    return res\n}\n
      graph_bfs.rb
      ### Breadth-first traversal ###\ndef graph_bfs(graph, start_vet)\n  # Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\n  # Vertex traversal sequence\n  res = []\n  # Hash set for recording vertices that have been visited\n  visited = Set.new([start_vet])\n  # Queue used to implement BFS\n  que = [start_vet]\n  # Starting from vertex vet, loop until all vertices are visited\n  while que.length > 0\n    vet = que.shift # Dequeue the front vertex\n    res << vet # Record visited vertex\n    # Traverse all adjacent vertices of this vertex\n    for adj_vet in graph.adj_list[vet]\n      next if visited.include?(adj_vet) # Skip vertices that have been visited\n      que << adj_vet # Only enqueue unvisited vertices\n      visited.add(adj_vet) # Mark this vertex as visited\n    end\n  end\n  # Return vertex traversal sequence\n  res\nend\n

      The code is relatively abstract; it is recommended to refer to Figure 9-10 to deepen understanding.

      <1><2><3><4><5><6><7><8><9><10><11>

      Figure 9-10 \u00a0 Steps of breadth-first search of a graph

      Is the breadth-first traversal sequence unique?

      Not unique. Breadth-first search only requires traversing in a \"near to far\" order, and the traversal order of vertices at the same distance can be arbitrarily shuffled. Taking Figure 9-10 as an example, the visit order of vertices \\(1\\) and \\(3\\) can be swapped, as can the visit order of vertices \\(2\\), \\(4\\), and \\(6\\).

      "},{"location":"chapter_graph/graph_traversal/#2-complexity-analysis","title":"2. \u00a0 Complexity Analysis","text":"

      Time complexity: All vertices will be enqueued and dequeued once, using \\(O(|V|)\\) time; in the process of traversing adjacent vertices, since it is an undirected graph, all edges will be visited \\(2\\) times, using \\(O(2|E|)\\) time; overall using \\(O(|V| + |E|)\\) time.

      Space complexity: The list res, hash set visited, and queue que can contain at most \\(|V|\\) vertices, using \\(O(|V|)\\) space.

      "},{"location":"chapter_graph/graph_traversal/#932-depth-first-search","title":"9.3.2 \u00a0 Depth-First Search","text":"

      Depth-first search is a traversal method that prioritizes going as far as possible, then backtracks when no path remains. As shown in Figure 9-11, starting from the top-left vertex, visit an adjacent vertex of the current vertex, continuing until reaching a dead end, then return and continue going as far as possible before returning again, and so on, until all vertices have been traversed.

      Figure 9-11 \u00a0 Depth-first search of a graph

      "},{"location":"chapter_graph/graph_traversal/#1-algorithm-implementation_1","title":"1. \u00a0 Algorithm Implementation","text":"

      This \"go as far as possible then return\" algorithm paradigm is typically implemented using recursion. Similar to breadth-first search, in depth-first search we also need a hash set visited to record visited vertices and avoid revisiting.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_dfs.py
      def dfs(graph: GraphAdjList, visited: set[Vertex], res: list[Vertex], vet: Vertex):\n    \"\"\"Depth-first traversal helper function\"\"\"\n    res.append(vet)  # Record visited vertex\n    visited.add(vet)  # Mark this vertex as visited\n    # Traverse all adjacent vertices of this vertex\n    for adjVet in graph.adj_list[vet]:\n        if adjVet in visited:\n            continue  # Skip vertices that have been visited\n        # Recursively visit adjacent vertices\n        dfs(graph, visited, res, adjVet)\n\ndef graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n    \"\"\"Depth-first traversal\"\"\"\n    # Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\n    # Vertex traversal sequence\n    res = []\n    # Hash set for recording vertices that have been visited\n    visited = set[Vertex]()\n    dfs(graph, visited, res, start_vet)\n    return res\n
      graph_dfs.cpp
      /* Depth-first traversal helper function */\nvoid dfs(GraphAdjList &graph, unordered_set<Vertex *> &visited, vector<Vertex *> &res, Vertex *vet) {\n    res.push_back(vet);   // Record visited vertex\n    visited.emplace(vet); // Mark this vertex as visited\n    // Traverse all adjacent vertices of this vertex\n    for (Vertex *adjVet : graph.adjList[vet]) {\n        if (visited.count(adjVet))\n            continue; // Skip vertices that have been visited\n        // Recursively visit adjacent vertices\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* Depth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nvector<Vertex *> graphDFS(GraphAdjList &graph, Vertex *startVet) {\n    // Vertex traversal sequence\n    vector<Vertex *> res;\n    // Hash set for recording vertices that have been visited\n    unordered_set<Vertex *> visited;\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
      graph_dfs.java
      /* Depth-first traversal helper function */\nvoid dfs(GraphAdjList graph, Set<Vertex> visited, List<Vertex> res, Vertex vet) {\n    res.add(vet);     // Record visited vertex\n    visited.add(vet); // Mark this vertex as visited\n    // Traverse all adjacent vertices of this vertex\n    for (Vertex adjVet : graph.adjList.get(vet)) {\n        if (visited.contains(adjVet))\n            continue; // Skip vertices that have been visited\n        // Recursively visit adjacent vertices\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* Depth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n    // Vertex traversal sequence\n    List<Vertex> res = new ArrayList<>();\n    // Hash set for recording vertices that have been visited\n    Set<Vertex> visited = new HashSet<>();\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
      graph_dfs.cs
      /* Depth-first traversal helper function */\nvoid DFS(GraphAdjList graph, HashSet<Vertex> visited, List<Vertex> res, Vertex vet) {\n    res.Add(vet);     // Record visited vertex\n    visited.Add(vet); // Mark this vertex as visited\n    // Traverse all adjacent vertices of this vertex\n    foreach (Vertex adjVet in graph.adjList[vet]) {\n        if (visited.Contains(adjVet)) {\n            continue; // Skip vertices that have been visited\n        }\n        // Recursively visit adjacent vertices\n        DFS(graph, visited, res, adjVet);\n    }\n}\n\n/* Depth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nList<Vertex> GraphDFS(GraphAdjList graph, Vertex startVet) {\n    // Vertex traversal sequence\n    List<Vertex> res = [];\n    // Hash set for recording vertices that have been visited\n    HashSet<Vertex> visited = [];\n    DFS(graph, visited, res, startVet);\n    return res;\n}\n
      graph_dfs.go
      /* Depth-first traversal helper function */\nfunc dfs(g *graphAdjList, visited map[Vertex]struct{}, res *[]Vertex, vet Vertex) {\n    // append operation returns a new reference, must reassign original reference to new slice's reference\n    *res = append(*res, vet)\n    visited[vet] = struct{}{}\n    // Traverse all adjacent vertices of this vertex\n    for _, adjVet := range g.adjList[vet] {\n        _, isExist := visited[adjVet]\n        // Recursively visit adjacent vertices\n        if !isExist {\n            dfs(g, visited, res, adjVet)\n        }\n    }\n}\n\n/* Depth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfunc graphDFS(g *graphAdjList, startVet Vertex) []Vertex {\n    // Vertex traversal sequence\n    res := make([]Vertex, 0)\n    // Hash set for recording vertices that have been visited\n    visited := make(map[Vertex]struct{})\n    dfs(g, visited, &res, startVet)\n    // Return vertex traversal sequence\n    return res\n}\n
      graph_dfs.swift
      /* Depth-first traversal helper function */\nfunc dfs(graph: GraphAdjList, visited: inout Set<Vertex>, res: inout [Vertex], vet: Vertex) {\n    res.append(vet) // Record visited vertex\n    visited.insert(vet) // Mark this vertex as visited\n    // Traverse all adjacent vertices of this vertex\n    for adjVet in graph.adjList[vet] ?? [] {\n        if visited.contains(adjVet) {\n            continue // Skip vertices that have been visited\n        }\n        // Recursively visit adjacent vertices\n        dfs(graph: graph, visited: &visited, res: &res, vet: adjVet)\n    }\n}\n\n/* Depth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfunc graphDFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {\n    // Vertex traversal sequence\n    var res: [Vertex] = []\n    // Hash set for recording vertices that have been visited\n    var visited: Set<Vertex> = []\n    dfs(graph: graph, visited: &visited, res: &res, vet: startVet)\n    return res\n}\n
      graph_dfs.js
      /* Depth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfunction dfs(graph, visited, res, vet) {\n    res.push(vet); // Record visited vertex\n    visited.add(vet); // Mark this vertex as visited\n    // Traverse all adjacent vertices of this vertex\n    for (const adjVet of graph.adjList.get(vet)) {\n        if (visited.has(adjVet)) {\n            continue; // Skip vertices that have been visited\n        }\n        // Recursively visit adjacent vertices\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* Depth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfunction graphDFS(graph, startVet) {\n    // Vertex traversal sequence\n    const res = [];\n    // Hash set for recording vertices that have been visited\n    const visited = new Set();\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
      graph_dfs.ts
      /* Depth-first traversal helper function */\nfunction dfs(\n    graph: GraphAdjList,\n    visited: Set<Vertex>,\n    res: Vertex[],\n    vet: Vertex\n): void {\n    res.push(vet); // Record visited vertex\n    visited.add(vet); // Mark this vertex as visited\n    // Traverse all adjacent vertices of this vertex\n    for (const adjVet of graph.adjList.get(vet)) {\n        if (visited.has(adjVet)) {\n            continue; // Skip vertices that have been visited\n        }\n        // Recursively visit adjacent vertices\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* Depth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfunction graphDFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n    // Vertex traversal sequence\n    const res: Vertex[] = [];\n    // Hash set for recording vertices that have been visited\n    const visited: Set<Vertex> = new Set();\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
      graph_dfs.dart
      /* Depth-first traversal helper function */\nvoid dfs(\n  GraphAdjList graph,\n  Set<Vertex> visited,\n  List<Vertex> res,\n  Vertex vet,\n) {\n  res.add(vet); // Record visited vertex\n  visited.add(vet); // Mark this vertex as visited\n  // Traverse all adjacent vertices of this vertex\n  for (Vertex adjVet in graph.adjList[vet]!) {\n    if (visited.contains(adjVet)) {\n      continue; // Skip vertices that have been visited\n    }\n    // Recursively visit adjacent vertices\n    dfs(graph, visited, res, adjVet);\n  }\n}\n\n/* Depth-first traversal */\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n  // Vertex traversal sequence\n  List<Vertex> res = [];\n  // Hash set for recording vertices that have been visited\n  Set<Vertex> visited = {};\n  dfs(graph, visited, res, startVet);\n  return res;\n}\n
      graph_dfs.rs
      /* Depth-first traversal helper function */\nfn dfs(graph: &GraphAdjList, visited: &mut HashSet<Vertex>, res: &mut Vec<Vertex>, vet: Vertex) {\n    res.push(vet); // Record visited vertex\n    visited.insert(vet); // Mark this vertex as visited\n                         // Traverse all adjacent vertices of this vertex\n    if let Some(adj_vets) = graph.adj_list.get(&vet) {\n        for &adj_vet in adj_vets {\n            if visited.contains(&adj_vet) {\n                continue; // Skip vertices that have been visited\n            }\n            // Recursively visit adjacent vertices\n            dfs(graph, visited, res, adj_vet);\n        }\n    }\n}\n\n/* Depth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfn graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {\n    // Vertex traversal sequence\n    let mut res = vec![];\n    // Hash set for recording vertices that have been visited\n    let mut visited = HashSet::new();\n    dfs(&graph, &mut visited, &mut res, start_vet);\n\n    res\n}\n
      graph_dfs.c
      /* Check if vertex has been visited */\nint isVisited(Vertex **res, int size, Vertex *vet) {\n    // Traverse to find node using O(n) time\n    for (int i = 0; i < size; i++) {\n        if (res[i] == vet) {\n            return 1;\n        }\n    }\n    return 0;\n}\n\n/* Depth-first traversal helper function */\nvoid dfs(GraphAdjList *graph, Vertex **res, int *resSize, Vertex *vet) {\n    // Record visited vertex\n    res[(*resSize)++] = vet;\n    // Traverse all adjacent vertices of this vertex\n    AdjListNode *node = findNode(graph, vet);\n    while (node != NULL) {\n        // Skip vertices that have been visited\n        if (!isVisited(res, *resSize, node->vertex)) {\n            // Recursively visit adjacent vertices\n            dfs(graph, res, resSize, node->vertex);\n        }\n        node = node->next;\n    }\n}\n\n/* Depth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nvoid graphDFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize) {\n    dfs(graph, res, resSize, startVet);\n}\n
      graph_dfs.kt
      /* Depth-first traversal helper function */\nfun dfs(\n    graph: GraphAdjList,\n    visited: MutableSet<Vertex?>,\n    res: MutableList<Vertex?>,\n    vet: Vertex?\n) {\n    res.add(vet)     // Record visited vertex\n    visited.add(vet) // Mark this vertex as visited\n    // Traverse all adjacent vertices of this vertex\n    for (adjVet in graph.adjList[vet]!!) {\n        if (visited.contains(adjVet))\n            continue  // Skip vertices that have been visited\n        // Recursively visit adjacent vertices\n        dfs(graph, visited, res, adjVet)\n    }\n}\n\n/* Depth-first traversal */\n// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\nfun graphDFS(graph: GraphAdjList, startVet: Vertex?): MutableList<Vertex?> {\n    // Vertex traversal sequence\n    val res = mutableListOf<Vertex?>()\n    // Hash set for recording vertices that have been visited\n    val visited = HashSet<Vertex?>()\n    dfs(graph, visited, res, startVet)\n    return res\n}\n
      graph_dfs.rb
      ### Depth-first traversal helper function ###\ndef dfs(graph, visited, res, vet)\n  res << vet # Record visited vertex\n  visited.add(vet) # Mark this vertex as visited\n  # Traverse all adjacent vertices of this vertex\n  for adj_vet in graph.adj_list[vet]\n    next if visited.include?(adj_vet) # Skip vertices that have been visited\n    # Recursively visit adjacent vertices\n    dfs(graph, visited, res, adj_vet)\n  end\nend\n\n### Depth-first traversal ###\ndef graph_dfs(graph, start_vet)\n  # Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex\n  # Vertex traversal sequence\n  res = []\n  # Hash set for recording vertices that have been visited\n  visited = Set.new\n  dfs(graph, visited, res, start_vet)\n  res\nend\n

      The algorithm flow of depth-first search is shown in Figure 9-12.

      • Straight dashed lines represent downward recursion, indicating that a new recursive method has been initiated to visit a new vertex.
      • Curved dashed lines represent upward backtracking, indicating that this recursive method has returned to the position where it was initiated.

      To deepen understanding, it is recommended to combine Figure 9-12 with the code to mentally simulate (or draw out) the entire DFS process, including when each recursive method is initiated and when it returns.

      <1><2><3><4><5><6><7><8><9><10><11>

      Figure 9-12 \u00a0 Steps of depth-first search of a graph

      Is the depth-first traversal sequence unique?

      Similar to breadth-first search, the order of depth-first traversal sequences is also not unique. Given a certain vertex, exploring in any direction first is valid, meaning the order of adjacent vertices can be arbitrarily shuffled, all being depth-first search.

      Taking tree traversal as an example, \"root \\(\\rightarrow\\) left \\(\\rightarrow\\) right\", \"left \\(\\rightarrow\\) root \\(\\rightarrow\\) right\", and \"left \\(\\rightarrow\\) right \\(\\rightarrow\\) root\" correspond to pre-order, in-order, and post-order traversals, respectively. They represent three different traversal priorities, yet all three belong to depth-first search.

      "},{"location":"chapter_graph/graph_traversal/#2-complexity-analysis_1","title":"2. \u00a0 Complexity Analysis","text":"

      Time complexity: All vertices will be visited \\(1\\) time, using \\(O(|V|)\\) time; all edges will be visited \\(2\\) times, using \\(O(2|E|)\\) time; overall using \\(O(|V| + |E|)\\) time.

      Space complexity: The list res and hash set visited can contain at most \\(|V|\\) vertices, and the maximum recursion depth is \\(|V|\\), therefore using \\(O(|V|)\\) space.

      "},{"location":"chapter_graph/summary/","title":"9.4 \u00a0 Summary","text":""},{"location":"chapter_graph/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • Graphs consist of vertices and edges and can be represented as a set of vertices and a set of edges.
      • Compared to linear relationships (linked lists) and divide-and-conquer relationships (trees), network relationships (graphs) have a higher degree of freedom and are therefore more complex.
      • Directed graphs have edges with directionality, connected graphs have all vertices reachable from any vertex, and weighted graphs have edges that each contain a weight variable.
      • Adjacency matrices use matrices to represent graphs, where each row (column) represents a vertex, and matrix elements represent edges, using \\(1\\) or \\(0\\) to indicate whether two vertices have an edge or not. Adjacency matrices are highly efficient for addition, deletion, lookup, and modification operations, but consume significant space.
      • Adjacency lists use multiple linked lists to represent graphs, where the \\(i\\)-th linked list corresponds to vertex \\(i\\) and stores all adjacent vertices of that vertex. Adjacency lists are more space-efficient than adjacency matrices, but have lower time efficiency because they require traversing linked lists to find edges.
      • When linked lists in adjacency lists become too long, they can be converted to red-black trees or hash tables, thereby improving lookup efficiency.
      • From an algorithmic perspective, adjacency matrices embody \"trading space for time\", while adjacency lists embody \"trading time for space\".
      • Graphs can be used to model various real-world systems, such as social networks and subway lines.
      • Trees are a special case of graphs, and tree traversal is a special case of graph traversal.
      • Breadth-first search of graphs is a near-to-far, layer-by-layer expansion search method, typically implemented using a queue.
      • Depth-first search of graphs is a search method that prioritizes going as far as possible and backtracks when no path remains, commonly implemented using recursion.
      "},{"location":"chapter_graph/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

      Q: Is a path defined as a sequence of vertices or a sequence of edges?

      The definitions in different language versions of Wikipedia are inconsistent: the English version states \"a path is a sequence of edges\", while the Chinese version states \"a path is a sequence of vertices\". The following is the original English text: In graph theory, a path in a graph is a finite or infinite sequence of edges which joins a sequence of vertices.

      In this text, a path is viewed as a sequence of edges, not a sequence of vertices. This is because there may be multiple edges connecting two vertices, in which case each edge corresponds to a path.

      Q: In a disconnected graph, will there be unreachable vertices?

      In a disconnected graph, starting from a certain vertex, at least one vertex cannot be reached. Traversing a disconnected graph requires setting multiple starting points to traverse all connected components of the graph.

      Q: In an adjacency list, is there a requirement for the order of \"all vertices connected to that vertex\"?

      It can be in any order. However, in practical applications, it may be necessary to sort according to specified rules, such as the order in which vertices were added, or the order of vertex values, which helps quickly find vertices \"with certain extreme values\".

      "},{"location":"chapter_greedy/","title":"Chapter 15. \u00a0 Greedy","text":"

      Abstract

      Sunflowers turn toward the sun, constantly pursuing the maximum potential for their own growth.

      Through rounds of simple choices, greedy strategies gradually lead to the best answer.

      "},{"location":"chapter_greedy/#chapter-contents","title":"Chapter contents","text":"
      • 15.1 \u00a0 Greedy Algorithm
      • 15.2 \u00a0 Fractional Knapsack Problem
      • 15.3 \u00a0 Maximum Capacity Problem
      • 15.4 \u00a0 Maximum Product Cutting Problem
      • 15.5 \u00a0 Summary
      "},{"location":"chapter_greedy/fractional_knapsack_problem/","title":"15.2 \u00a0 Fractional Knapsack Problem","text":"

      Question

      Given \\(n\\) items, where the weight of the \\(i\\)-th item is \\(wgt[i-1]\\) and its value is \\(val[i-1]\\), and a knapsack with capacity \\(cap\\). Each item can be selected only once, but a portion of an item can be selected, with the value calculated based on the proportion of weight selected, what is the maximum value of items in the knapsack under the limited capacity? An example is shown in Figure 15-3.

      Figure 15-3 \u00a0 Example data for the fractional knapsack problem

      The fractional knapsack problem is very similar overall to the 0-1 knapsack problem, with states including the current item \\(i\\) and capacity \\(c\\), and the goal being to maximize value under the limited knapsack capacity.

      The difference is that this problem allows selecting only a portion of an item. As shown in Figure 15-4, we can arbitrarily split items and calculate the corresponding value based on the weight proportion.

      1. For item \\(i\\), its value per unit weight is \\(val[i-1] / wgt[i-1]\\), referred to as unit value.
      2. Suppose we put a portion of item \\(i\\) with weight \\(w\\) into the knapsack, then the value added to the knapsack is \\(w \\times val[i-1] / wgt[i-1]\\).

      Figure 15-4 \u00a0 Value of items per unit weight

      "},{"location":"chapter_greedy/fractional_knapsack_problem/#1-greedy-strategy-determination","title":"1. \u00a0 Greedy Strategy Determination","text":"

      Maximizing the total value of items in the knapsack is essentially maximizing the value per unit weight of items. From this, we can derive the greedy strategy shown in Figure 15-5.

      1. Sort items by unit value from high to low.
      2. Iterate through all items, greedily selecting the item with the highest unit value in each round.
      3. If the remaining knapsack capacity is insufficient, use a portion of the current item to fill the knapsack.

      Figure 15-5 \u00a0 Greedy strategy for the fractional knapsack problem

      "},{"location":"chapter_greedy/fractional_knapsack_problem/#2-code-implementation","title":"2. \u00a0 Code Implementation","text":"

      We created an Item class to facilitate sorting items by unit value. We loop to make greedy selections, breaking when the knapsack is full and returning the solution:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby fractional_knapsack.py
      class Item:\n    \"\"\"Item\"\"\"\n\n    def __init__(self, w: int, v: int):\n        self.w = w  # Item weight\n        self.v = v  # Item value\n\ndef fractional_knapsack(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"Fractional knapsack: Greedy algorithm\"\"\"\n    # Create item list with two attributes: weight, value\n    items = [Item(w, v) for w, v in zip(wgt, val)]\n    # Sort by unit value item.v / item.w from high to low\n    items.sort(key=lambda item: item.v / item.w, reverse=True)\n    # Loop for greedy selection\n    res = 0\n    for item in items:\n        if item.w <= cap:\n            # If remaining capacity is sufficient, put the entire current item into the knapsack\n            res += item.v\n            cap -= item.w\n        else:\n            # If remaining capacity is insufficient, put part of the current item into the knapsack\n            res += (item.v / item.w) * cap\n            # No remaining capacity, so break out of the loop\n            break\n    return res\n
      fractional_knapsack.cpp
      /* Item */\nclass Item {\n  public:\n    int w; // Item weight\n    int v; // Item value\n\n    Item(int w, int v) : w(w), v(v) {\n    }\n};\n\n/* Fractional knapsack: Greedy algorithm */\ndouble fractionalKnapsack(vector<int> &wgt, vector<int> &val, int cap) {\n    // Create item list with two attributes: weight, value\n    vector<Item> items;\n    for (int i = 0; i < wgt.size(); i++) {\n        items.push_back(Item(wgt[i], val[i]));\n    }\n    // Sort by unit value item.v / item.w from high to low\n    sort(items.begin(), items.end(), [](Item &a, Item &b) { return (double)a.v / a.w > (double)b.v / b.w; });\n    // Loop for greedy selection\n    double res = 0;\n    for (auto &item : items) {\n        if (item.w <= cap) {\n            // If remaining capacity is sufficient, put the entire current item into the knapsack\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // If remaining capacity is insufficient, put part of the current item into the knapsack\n            res += (double)item.v / item.w * cap;\n            // No remaining capacity, so break out of the loop\n            break;\n        }\n    }\n    return res;\n}\n
      fractional_knapsack.java
      /* Item */\nclass Item {\n    int w; // Item weight\n    int v; // Item value\n\n    public Item(int w, int v) {\n        this.w = w;\n        this.v = v;\n    }\n}\n\n/* Fractional knapsack: Greedy algorithm */\ndouble fractionalKnapsack(int[] wgt, int[] val, int cap) {\n    // Create item list with two attributes: weight, value\n    Item[] items = new Item[wgt.length];\n    for (int i = 0; i < wgt.length; i++) {\n        items[i] = new Item(wgt[i], val[i]);\n    }\n    // Sort by unit value item.v / item.w from high to low\n    Arrays.sort(items, Comparator.comparingDouble(item -> -((double) item.v / item.w)));\n    // Loop for greedy selection\n    double res = 0;\n    for (Item item : items) {\n        if (item.w <= cap) {\n            // If remaining capacity is sufficient, put the entire current item into the knapsack\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // If remaining capacity is insufficient, put part of the current item into the knapsack\n            res += (double) item.v / item.w * cap;\n            // No remaining capacity, so break out of the loop\n            break;\n        }\n    }\n    return res;\n}\n
      fractional_knapsack.cs
      /* Item */\nclass Item(int w, int v) {\n    public int w = w; // Item weight\n    public int v = v; // Item value\n}\n\n/* Fractional knapsack: Greedy algorithm */\ndouble FractionalKnapsack(int[] wgt, int[] val, int cap) {\n    // Create item list with two attributes: weight, value\n    Item[] items = new Item[wgt.Length];\n    for (int i = 0; i < wgt.Length; i++) {\n        items[i] = new Item(wgt[i], val[i]);\n    }\n    // Sort by unit value item.v / item.w from high to low\n    Array.Sort(items, (x, y) => (y.v / y.w).CompareTo(x.v / x.w));\n    // Loop for greedy selection\n    double res = 0;\n    foreach (Item item in items) {\n        if (item.w <= cap) {\n            // If remaining capacity is sufficient, put the entire current item into the knapsack\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // If remaining capacity is insufficient, put part of the current item into the knapsack\n            res += (double)item.v / item.w * cap;\n            // No remaining capacity, so break out of the loop\n            break;\n        }\n    }\n    return res;\n}\n
      fractional_knapsack.go
      /* Item */\ntype Item struct {\n    w int // Item weight\n    v int // Item value\n}\n\n/* Fractional knapsack: Greedy algorithm */\nfunc fractionalKnapsack(wgt []int, val []int, cap int) float64 {\n    // Create item list with two attributes: weight, value\n    items := make([]Item, len(wgt))\n    for i := 0; i < len(wgt); i++ {\n        items[i] = Item{wgt[i], val[i]}\n    }\n    // Sort by unit value item.v / item.w from high to low\n    sort.Slice(items, func(i, j int) bool {\n        return float64(items[i].v)/float64(items[i].w) > float64(items[j].v)/float64(items[j].w)\n    })\n    // Loop for greedy selection\n    res := 0.0\n    for _, item := range items {\n        if item.w <= cap {\n            // If remaining capacity is sufficient, put the entire current item into the knapsack\n            res += float64(item.v)\n            cap -= item.w\n        } else {\n            // If remaining capacity is insufficient, put part of the current item into the knapsack\n            res += float64(item.v) / float64(item.w) * float64(cap)\n            // No remaining capacity, so break out of the loop\n            break\n        }\n    }\n    return res\n}\n
      fractional_knapsack.swift
      /* Item */\nclass Item {\n    var w: Int // Item weight\n    var v: Int // Item value\n\n    init(w: Int, v: Int) {\n        self.w = w\n        self.v = v\n    }\n}\n\n/* Fractional knapsack: Greedy algorithm */\nfunc fractionalKnapsack(wgt: [Int], val: [Int], cap: Int) -> Double {\n    // Create item list with two attributes: weight, value\n    var items = zip(wgt, val).map { Item(w: $0, v: $1) }\n    // Sort by unit value item.v / item.w from high to low\n    items.sort { -(Double($0.v) / Double($0.w)) < -(Double($1.v) / Double($1.w)) }\n    // Loop for greedy selection\n    var res = 0.0\n    var cap = cap\n    for item in items {\n        if item.w <= cap {\n            // If remaining capacity is sufficient, put the entire current item into the knapsack\n            res += Double(item.v)\n            cap -= item.w\n        } else {\n            // If remaining capacity is insufficient, put part of the current item into the knapsack\n            res += Double(item.v) / Double(item.w) * Double(cap)\n            // No remaining capacity, so break out of the loop\n            break\n        }\n    }\n    return res\n}\n
      fractional_knapsack.js
      /* Item */\nclass Item {\n    constructor(w, v) {\n        this.w = w; // Item weight\n        this.v = v; // Item value\n    }\n}\n\n/* Fractional knapsack: Greedy algorithm */\nfunction fractionalKnapsack(wgt, val, cap) {\n    // Create item list with two attributes: weight, value\n    const items = wgt.map((w, i) => new Item(w, val[i]));\n    // Sort by unit value item.v / item.w from high to low\n    items.sort((a, b) => b.v / b.w - a.v / a.w);\n    // Loop for greedy selection\n    let res = 0;\n    for (const item of items) {\n        if (item.w <= cap) {\n            // If remaining capacity is sufficient, put the entire current item into the knapsack\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // If remaining capacity is insufficient, put part of the current item into the knapsack\n            res += (item.v / item.w) * cap;\n            // No remaining capacity, so break out of the loop\n            break;\n        }\n    }\n    return res;\n}\n
      fractional_knapsack.ts
      /* Item */\nclass Item {\n    w: number; // Item weight\n    v: number; // Item value\n\n    constructor(w: number, v: number) {\n        this.w = w;\n        this.v = v;\n    }\n}\n\n/* Fractional knapsack: Greedy algorithm */\nfunction fractionalKnapsack(wgt: number[], val: number[], cap: number): number {\n    // Create item list with two attributes: weight, value\n    const items: Item[] = wgt.map((w, i) => new Item(w, val[i]));\n    // Sort by unit value item.v / item.w from high to low\n    items.sort((a, b) => b.v / b.w - a.v / a.w);\n    // Loop for greedy selection\n    let res = 0;\n    for (const item of items) {\n        if (item.w <= cap) {\n            // If remaining capacity is sufficient, put the entire current item into the knapsack\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // If remaining capacity is insufficient, put part of the current item into the knapsack\n            res += (item.v / item.w) * cap;\n            // No remaining capacity, so break out of the loop\n            break;\n        }\n    }\n    return res;\n}\n
      fractional_knapsack.dart
      /* Item */\nclass Item {\n  int w; // Item weight\n  int v; // Item value\n\n  Item(this.w, this.v);\n}\n\n/* Fractional knapsack: Greedy algorithm */\ndouble fractionalKnapsack(List<int> wgt, List<int> val, int cap) {\n  // Create item list with two attributes: weight, value\n  List<Item> items = List.generate(wgt.length, (i) => Item(wgt[i], val[i]));\n  // Sort by unit value item.v / item.w from high to low\n  items.sort((a, b) => (b.v / b.w).compareTo(a.v / a.w));\n  // Loop for greedy selection\n  double res = 0;\n  for (Item item in items) {\n    if (item.w <= cap) {\n      // If remaining capacity is sufficient, put the entire current item into the knapsack\n      res += item.v;\n      cap -= item.w;\n    } else {\n      // If remaining capacity is insufficient, put part of the current item into the knapsack\n      res += item.v / item.w * cap;\n      // No remaining capacity, so break out of the loop\n      break;\n    }\n  }\n  return res;\n}\n
      fractional_knapsack.rs
      /* Item */\nstruct Item {\n    w: i32, // Item weight\n    v: i32, // Item value\n}\n\nimpl Item {\n    fn new(w: i32, v: i32) -> Self {\n        Self { w, v }\n    }\n}\n\n/* Fractional knapsack: Greedy algorithm */\nfn fractional_knapsack(wgt: &[i32], val: &[i32], mut cap: i32) -> f64 {\n    // Create item list with two attributes: weight, value\n    let mut items = wgt\n        .iter()\n        .zip(val.iter())\n        .map(|(&w, &v)| Item::new(w, v))\n        .collect::<Vec<Item>>();\n    // Sort by unit value item.v / item.w from high to low\n    items.sort_by(|a, b| {\n        (b.v as f64 / b.w as f64)\n            .partial_cmp(&(a.v as f64 / a.w as f64))\n            .unwrap()\n    });\n    // Loop for greedy selection\n    let mut res = 0.0;\n    for item in &items {\n        if item.w <= cap {\n            // If remaining capacity is sufficient, put the entire current item into the knapsack\n            res += item.v as f64;\n            cap -= item.w;\n        } else {\n            // If remaining capacity is insufficient, put part of the current item into the knapsack\n            res += item.v as f64 / item.w as f64 * cap as f64;\n            // No remaining capacity, so break out of the loop\n            break;\n        }\n    }\n    res\n}\n
      fractional_knapsack.c
      /* Item */\ntypedef struct {\n    int w; // Item weight\n    int v; // Item value\n} Item;\n\n/* Fractional knapsack: Greedy algorithm */\nfloat fractionalKnapsack(int wgt[], int val[], int itemCount, int cap) {\n    // Create item list with two attributes: weight, value\n    Item *items = malloc(sizeof(Item) * itemCount);\n    for (int i = 0; i < itemCount; i++) {\n        items[i] = (Item){.w = wgt[i], .v = val[i]};\n    }\n    // Sort by unit value item.v / item.w from high to low\n    qsort(items, (size_t)itemCount, sizeof(Item), sortByValueDensity);\n    // Loop for greedy selection\n    float res = 0.0;\n    for (int i = 0; i < itemCount; i++) {\n        if (items[i].w <= cap) {\n            // If remaining capacity is sufficient, put the entire current item into the knapsack\n            res += items[i].v;\n            cap -= items[i].w;\n        } else {\n            // If remaining capacity is insufficient, put part of the current item into the knapsack\n            res += (float)cap / items[i].w * items[i].v;\n            cap = 0;\n            break;\n        }\n    }\n    free(items);\n    return res;\n}\n
      fractional_knapsack.kt
      /* Item */\nclass Item(\n    val w: Int, // Item\n    val v: Int  // Item value\n)\n\n/* Fractional knapsack: Greedy algorithm */\nfun fractionalKnapsack(wgt: IntArray, _val: IntArray, c: Int): Double {\n    // Create item list with two attributes: weight, value\n    var cap = c\n    val items = arrayOfNulls<Item>(wgt.size)\n    for (i in wgt.indices) {\n        items[i] = Item(wgt[i], _val[i])\n    }\n    // Sort by unit value item.v / item.w from high to low\n    items.sortBy { item: Item? -> -(item!!.v.toDouble() / item.w) }\n    // Loop for greedy selection\n    var res = 0.0\n    for (item in items) {\n        if (item!!.w <= cap) {\n            // If remaining capacity is sufficient, put the entire current item into the knapsack\n            res += item.v\n            cap -= item.w\n        } else {\n            // If remaining capacity is insufficient, put part of the current item into the knapsack\n            res += item.v.toDouble() / item.w * cap\n            // No remaining capacity, so break out of the loop\n            break\n        }\n    }\n    return res\n}\n
      fractional_knapsack.rb
      ### Item ###\nclass Item\n  attr_accessor :w # Item weight\n  attr_accessor :v # Item value\n\n  def initialize(w, v)\n    @w = w\n    @v = v\n  end\nend\n\n### Fractional knapsack: greedy ###\ndef fractional_knapsack(wgt, val, cap)\n  # Create item list with two attributes: weight, value\n  items = wgt.each_with_index.map { |w, i| Item.new(w, val[i]) }\n  # Sort by unit value item.v / item.w from high to low\n  items.sort! { |a, b| (b.v.to_f / b.w) <=> (a.v.to_f / a.w) }\n  # Loop for greedy selection\n  res = 0\n  for item in items\n    if item.w <= cap\n      # If remaining capacity is sufficient, put the entire current item into the knapsack\n      res += item.v\n      cap -= item.w\n    else\n      # If remaining capacity is insufficient, put part of the current item into the knapsack\n      res += (item.v.to_f / item.w) * cap\n      # No remaining capacity, so break out of the loop\n      break\n    end\n  end\n  res\nend\n

      The time complexity of built-in sorting algorithms is usually \\(O(\\log n)\\), and the space complexity is usually \\(O(\\log n)\\) or \\(O(n)\\), depending on the specific implementation of the programming language.

      Apart from sorting, in the worst case the entire item list needs to be traversed, therefore the time complexity is \\(O(n)\\), where \\(n\\) is the number of items.

      Since an Item object list is initialized, the space complexity is \\(O(n)\\).

      "},{"location":"chapter_greedy/fractional_knapsack_problem/#3-correctness-proof","title":"3. \u00a0 Correctness Proof","text":"

      Using proof by contradiction. Suppose item \\(x\\) has the highest unit value, and some algorithm yields a maximum value of res, but this solution does not include item \\(x\\).

      Now remove a unit weight of any item from the knapsack and replace it with a unit weight of item \\(x\\). Since item \\(x\\) has the highest unit value, the total value after replacement will definitely be greater than res. This contradicts the assumption that res is the optimal solution, proving that the optimal solution must include item \\(x\\).

      For other items in this solution, we can also construct the above contradiction. In summary, items with greater unit value are always better choices, which proves that the greedy strategy is effective.

      As shown in Figure 15-6, if we view item weight and item unit value as the horizontal and vertical axes of a two-dimensional chart respectively, then the fractional knapsack problem can be transformed into \"finding the maximum area enclosed within a limited horizontal axis range\". This analogy can help us understand the effectiveness of the greedy strategy from a geometric perspective.

      Figure 15-6 \u00a0 Geometric representation of the fractional knapsack problem

      "},{"location":"chapter_greedy/greedy_algorithm/","title":"15.1 \u00a0 Greedy Algorithm","text":"

      Greedy algorithm is a common algorithm for solving optimization problems. Its basic idea is to make the seemingly best choice at each decision stage of the problem, that is, to greedily make locally optimal decisions in hopes of obtaining a globally optimal solution. Greedy algorithms are simple and efficient, and are widely applied in many practical problems.

      Greedy algorithms and dynamic programming are both commonly used to solve optimization problems. They share some similarities, such as both relying on the optimal substructure property, but they work differently.

      • Dynamic programming considers all previous decisions when making the current decision, and uses solutions to past subproblems to construct the solution to the current subproblem.
      • Greedy algorithms do not consider past decisions, but instead make greedy choices moving forward, continually reducing the problem size until the problem is solved.

      We will first understand how greedy algorithms work through the example problem \"coin change\". This problem has already been introduced in the \"Complete Knapsack Problem\" chapter, so I believe you are not unfamiliar with it.

      Question

      Given \\(n\\) types of coins, where the denomination of the \\(i\\)-th type of coin is \\(coins[i - 1]\\), and the target amount is \\(amt\\), with each type of coin available for repeated selection, what is the minimum number of coins needed to make up the target amount? If it is impossible to make up the target amount, return \\(-1\\).

      The greedy strategy adopted for this problem is shown in Figure 15-1. Given a target amount, we greedily select the coin that is not greater than and closest to it, and continuously repeat this step until the target amount is reached.

      Figure 15-1 \u00a0 Greedy strategy for coin change

      The implementation code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change_greedy.py
      def coin_change_greedy(coins: list[int], amt: int) -> int:\n    \"\"\"Coin change: Greedy algorithm\"\"\"\n    # Assume coins list is sorted\n    i = len(coins) - 1\n    count = 0\n    # Loop to make greedy choices until no remaining amount\n    while amt > 0:\n        # Find the coin that is less than and closest to the remaining amount\n        while i > 0 and coins[i] > amt:\n            i -= 1\n        # Choose coins[i]\n        amt -= coins[i]\n        count += 1\n    # If no feasible solution is found, return -1\n    return count if amt == 0 else -1\n
      coin_change_greedy.cpp
      /* Coin change: Greedy algorithm */\nint coinChangeGreedy(vector<int> &coins, int amt) {\n    // Assume coins list is sorted\n    int i = coins.size() - 1;\n    int count = 0;\n    // Loop to make greedy choices until no remaining amount\n    while (amt > 0) {\n        // Find the coin that is less than and closest to the remaining amount\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // Choose coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // If no feasible solution is found, return -1\n    return amt == 0 ? count : -1;\n}\n
      coin_change_greedy.java
      /* Coin change: Greedy algorithm */\nint coinChangeGreedy(int[] coins, int amt) {\n    // Assume coins list is sorted\n    int i = coins.length - 1;\n    int count = 0;\n    // Loop to make greedy choices until no remaining amount\n    while (amt > 0) {\n        // Find the coin that is less than and closest to the remaining amount\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // Choose coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // If no feasible solution is found, return -1\n    return amt == 0 ? count : -1;\n}\n
      coin_change_greedy.cs
      /* Coin change: Greedy algorithm */\nint CoinChangeGreedy(int[] coins, int amt) {\n    // Assume coins list is sorted\n    int i = coins.Length - 1;\n    int count = 0;\n    // Loop to make greedy choices until no remaining amount\n    while (amt > 0) {\n        // Find the coin that is less than and closest to the remaining amount\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // Choose coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // If no feasible solution is found, return -1\n    return amt == 0 ? count : -1;\n}\n
      coin_change_greedy.go
      /* Coin change: Greedy algorithm */\nfunc coinChangeGreedy(coins []int, amt int) int {\n    // Assume coins list is sorted\n    i := len(coins) - 1\n    count := 0\n    // Loop to make greedy choices until no remaining amount\n    for amt > 0 {\n        // Find the coin that is less than and closest to the remaining amount\n        for i > 0 && coins[i] > amt {\n            i--\n        }\n        // Choose coins[i]\n        amt -= coins[i]\n        count++\n    }\n    // If no feasible solution is found, return -1\n    if amt != 0 {\n        return -1\n    }\n    return count\n}\n
      coin_change_greedy.swift
      /* Coin change: Greedy algorithm */\nfunc coinChangeGreedy(coins: [Int], amt: Int) -> Int {\n    // Assume coins list is sorted\n    var i = coins.count - 1\n    var count = 0\n    var amt = amt\n    // Loop to make greedy choices until no remaining amount\n    while amt > 0 {\n        // Find the coin that is less than and closest to the remaining amount\n        while i > 0 && coins[i] > amt {\n            i -= 1\n        }\n        // Choose coins[i]\n        amt -= coins[i]\n        count += 1\n    }\n    // If no feasible solution is found, return -1\n    return amt == 0 ? count : -1\n}\n
      coin_change_greedy.js
      /* Coin change: Greedy algorithm */\nfunction coinChangeGreedy(coins, amt) {\n    // Assume coins array is sorted\n    let i = coins.length - 1;\n    let count = 0;\n    // Loop to make greedy choices until no remaining amount\n    while (amt > 0) {\n        // Find the coin that is less than and closest to the remaining amount\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // Choose coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // If no feasible solution is found, return -1\n    return amt === 0 ? count : -1;\n}\n
      coin_change_greedy.ts
      /* Coin change: Greedy algorithm */\nfunction coinChangeGreedy(coins: number[], amt: number): number {\n    // Assume coins array is sorted\n    let i = coins.length - 1;\n    let count = 0;\n    // Loop to make greedy choices until no remaining amount\n    while (amt > 0) {\n        // Find the coin that is less than and closest to the remaining amount\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // Choose coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // If no feasible solution is found, return -1\n    return amt === 0 ? count : -1;\n}\n
      coin_change_greedy.dart
      /* Coin change: Greedy algorithm */\nint coinChangeGreedy(List<int> coins, int amt) {\n  // Assume coins list is sorted\n  int i = coins.length - 1;\n  int count = 0;\n  // Loop to make greedy choices until no remaining amount\n  while (amt > 0) {\n    // Find the coin that is less than and closest to the remaining amount\n    while (i > 0 && coins[i] > amt) {\n      i--;\n    }\n    // Choose coins[i]\n    amt -= coins[i];\n    count++;\n  }\n  // If no feasible solution is found, return -1\n  return amt == 0 ? count : -1;\n}\n
      coin_change_greedy.rs
      /* Coin change: Greedy algorithm */\nfn coin_change_greedy(coins: &[i32], mut amt: i32) -> i32 {\n    // Assume coins list is sorted\n    let mut i = coins.len() - 1;\n    let mut count = 0;\n    // Loop to make greedy choices until no remaining amount\n    while amt > 0 {\n        // Find the coin that is less than and closest to the remaining amount\n        while i > 0 && coins[i] > amt {\n            i -= 1;\n        }\n        // Choose coins[i]\n        amt -= coins[i];\n        count += 1;\n    }\n    // If no feasible solution is found, return -1\n    if amt == 0 {\n        count\n    } else {\n        -1\n    }\n}\n
      coin_change_greedy.c
      /* Coin change: Greedy algorithm */\nint coinChangeGreedy(int *coins, int size, int amt) {\n    // Assume coins list is sorted\n    int i = size - 1;\n    int count = 0;\n    // Loop to make greedy choices until no remaining amount\n    while (amt > 0) {\n        // Find the coin that is less than and closest to the remaining amount\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // Choose coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // If no feasible solution is found, return -1\n    return amt == 0 ? count : -1;\n}\n
      coin_change_greedy.kt
      /* Coin change: Greedy algorithm */\nfun coinChangeGreedy(coins: IntArray, amt: Int): Int {\n    // Assume coins list is sorted\n    var am = amt\n    var i = coins.size - 1\n    var count = 0\n    // Loop to make greedy choices until no remaining amount\n    while (am > 0) {\n        // Find the coin that is less than and closest to the remaining amount\n        while (i > 0 && coins[i] > am) {\n            i--\n        }\n        // Choose coins[i]\n        am -= coins[i]\n        count++\n    }\n    // If no feasible solution is found, return -1\n    return if (am == 0) count else -1\n}\n
      coin_change_greedy.rb
      ### Coin change: greedy ###\ndef coin_change_greedy(coins, amt)\n  # Assume coins list is sorted\n  i = coins.length - 1\n  count = 0\n  # Loop to make greedy choices until no remaining amount\n  while amt > 0\n    # Find the coin that is less than and closest to the remaining amount\n    while i > 0 && coins[i] > amt\n      i -= 1\n    end\n    # Choose coins[i]\n    amt -= coins[i]\n    count += 1\n  end\n  # Return -1 if no solution found\n  amt == 0 ? count : -1\nend\n

      You might exclaim: So clean! The greedy algorithm solves the coin change problem in about ten lines of code.

      "},{"location":"chapter_greedy/greedy_algorithm/#1511-advantages-and-limitations-of-greedy-algorithms","title":"15.1.1 \u00a0 Advantages and Limitations of Greedy Algorithms","text":"

      Greedy algorithms are not only straightforward and simple to implement, but are also usually very efficient. In the code above, if the smallest coin denomination is \\(\\min(coins)\\), the greedy choice loops at most \\(amt / \\min(coins)\\) times, giving a time complexity of \\(O(amt / \\min(coins))\\). This is an order of magnitude smaller than the time complexity of the dynamic programming solution \\(O(n \\times amt)\\).

      However, for certain coin denomination combinations, greedy algorithms cannot find the optimal solution. Figure 15-2 provides two examples.

      • Positive example \\(coins = [1, 5, 10, 20, 50, 100]\\): With this coin combination, given any \\(amt\\), the greedy algorithm can find the optimal solution.
      • Negative example \\(coins = [1, 20, 50]\\): Suppose \\(amt = 60\\), the greedy algorithm can only find the combination \\(50 + 1 \\times 10\\), totaling \\(11\\) coins, but dynamic programming can find the optimal solution \\(20 + 20 + 20\\), requiring only \\(3\\) coins.
      • Negative example \\(coins = [1, 49, 50]\\): Suppose \\(amt = 98\\), the greedy algorithm can only find the combination \\(50 + 1 \\times 48\\), totaling \\(49\\) coins, but dynamic programming can find the optimal solution \\(49 + 49\\), requiring only \\(2\\) coins.

      Figure 15-2 \u00a0 Examples where greedy algorithms cannot find the optimal solution

      In other words, for the coin change problem, greedy algorithms cannot guarantee finding the global optimal solution, and may even find very poor solutions. It is better suited for solving with dynamic programming.

      Generally, the applicability of greedy algorithms falls into the following two situations.

      1. Can guarantee finding the optimal solution: In this situation, greedy algorithms are often the best choice, because they tend to be more efficient than backtracking and dynamic programming.
      2. Can find an approximate optimal solution: Greedy algorithms are also applicable in this situation. For many complex problems, finding the global optimal solution is very difficult, and being able to find a suboptimal solution with high efficiency is also very good.
      "},{"location":"chapter_greedy/greedy_algorithm/#1512-characteristics-of-greedy-algorithms","title":"15.1.2 \u00a0 Characteristics of Greedy Algorithms","text":"

      So the question arises: what kind of problems are suitable for solving with greedy algorithms? Or in other words, under what conditions can greedy algorithms guarantee finding the optimal solution?

      Compared to dynamic programming, the conditions for using greedy algorithms are stricter, mainly focusing on two properties of the problem.

      • Greedy choice property: Only when locally optimal choices can always lead to a globally optimal solution can greedy algorithms guarantee obtaining the optimal solution.
      • Optimal substructure: The optimal solution to the original problem contains the optimal solutions to subproblems.

      Optimal substructure has already been introduced in the \"Dynamic Programming\" chapter, so we won't elaborate on it here. It's worth noting that the optimal substructure of some problems is not obvious, but they can still be solved using greedy algorithms.

      We mainly explore methods for determining the greedy choice property. Although its description seems relatively simple, in practice, for many problems, proving the greedy choice property is not easy.

      For example, in the coin change problem, although we can easily provide counterexamples to disprove the greedy choice property, proving it is quite difficult. If asked: what conditions must a coin combination satisfy to be solvable using a greedy algorithm? We often can only rely on intuition or examples to give an ambiguous answer, and find it difficult to provide a rigorous mathematical proof.

      Quote

      There is a paper that presents an algorithm with \\(O(n^3)\\) time complexity for determining whether a coin combination can use a greedy algorithm to find the optimal solution for any amount.

      Pearson, D. A polynomial-time algorithm for the change-making problem[J]. Operations Research Letters, 2005, 33(3): 231-234.

      "},{"location":"chapter_greedy/greedy_algorithm/#1513-steps-for-solving-problems-with-greedy-algorithms","title":"15.1.3 \u00a0 Steps for Solving Problems with Greedy Algorithms","text":"

      The problem-solving process for greedy problems can generally be divided into the following three steps.

      1. Problem analysis: Sort out and understand the problem characteristics, including state definition, optimization objectives, and constraints, etc. This step is also involved in backtracking and dynamic programming.
      2. Determine the greedy strategy: Determine how to make greedy choices at each step. This strategy should be able to reduce the problem size at each step, ultimately solving the entire problem.
      3. Correctness proof: It is usually necessary to prove that the problem has both greedy choice property and optimal substructure. This step may require mathematical proofs, such as mathematical induction or proof by contradiction.

      Determining the greedy strategy is the core step in solving the problem, but it may not be easy to implement, mainly for the following reasons.

      • Greedy strategies differ greatly between different problems. For many problems, the greedy strategy is relatively straightforward, and we can derive it through some general thinking and attempts. However, for some complex problems, the greedy strategy may be very elusive, which really tests one's problem-solving experience and algorithmic ability.
      • Some greedy strategies are highly misleading. When we confidently design a greedy strategy, write the solution code and submit it for testing, we may find that some test cases cannot pass. This is because the designed greedy strategy is only \"partially correct\", as exemplified by the coin change problem discussed above.

      To ensure correctness, we should rigorously mathematically prove the greedy strategy, usually using proof by contradiction or mathematical induction.

      However, correctness proofs may also not be easy. If we have no clue, we usually choose to debug the code based on test cases, step by step modifying and verifying the greedy strategy.

      "},{"location":"chapter_greedy/greedy_algorithm/#1514-typical-problems-solved-by-greedy-algorithms","title":"15.1.4 \u00a0 Typical Problems Solved by Greedy Algorithms","text":"

      Greedy algorithms are often applied to optimization problems that satisfy greedy choice property and optimal substructure. Below are some typical greedy algorithm problems.

      • Coin change problem: With certain coin combinations, greedy algorithms can always obtain the optimal solution.
      • Interval scheduling problem: Suppose you have some tasks, each taking place during a period of time, and your goal is to complete as many tasks as possible. If you always choose the task that ends earliest, then the greedy algorithm can obtain the optimal solution.
      • Fractional knapsack problem: Given a set of items and a carrying capacity, your goal is to select a set of items such that the total weight does not exceed the carrying capacity and the total value is maximized. If you always choose the item with the highest value-to-weight ratio (value / weight), then the greedy algorithm can obtain the optimal solution in some cases.
      • Stock trading problem: Given a set of historical stock prices, you can make multiple trades, but if you already hold stocks, you cannot buy again before selling, and the goal is to obtain the maximum profit.
      • Huffman coding: Huffman coding is a greedy algorithm used for lossless data compression. By constructing a Huffman tree and always merging the two nodes with the lowest frequency, the resulting Huffman tree has the minimum weighted path length (encoding length).
      • Dijkstra's algorithm: It is a greedy algorithm for solving the shortest path problem from a given source vertex to all other vertices.
      "},{"location":"chapter_greedy/max_capacity_problem/","title":"15.3 \u00a0 Max Capacity Problem","text":"

      Question

      Input an array \\(ht\\), where each element represents the height of a vertical partition. Any two partitions in the array, along with the space between them, can form a container.

      The capacity of the container equals the product of height and width (area), where the height is determined by the shorter partition, and the width is the difference in array indices between the two partitions.

      Please select two partitions in the array such that the capacity of the formed container is maximized, and return the maximum capacity. An example is shown in Figure 15-7.

      Figure 15-7 \u00a0 Example data for the max capacity problem

      The container is formed by any two partitions, therefore the state of this problem is the indices of two partitions, denoted as \\([i, j]\\).

      According to the problem description, capacity equals height multiplied by width, where height is determined by the shorter partition, and width is the difference in array indices between the two partitions. Let the capacity be \\(cap[i, j]\\), then the calculation formula is:

      \\[ cap[i, j] = \\min(ht[i], ht[j]) \\times (j - i) \\]

      Let the array length be \\(n\\), then the number of combinations of two partitions (total number of states) is \\(C_n^2 = \\frac{n(n - 1)}{2}\\). Most directly, we can exhaustively enumerate all states to find the maximum capacity, with time complexity \\(O(n^2)\\).

      "},{"location":"chapter_greedy/max_capacity_problem/#1-greedy-strategy-determination","title":"1. \u00a0 Greedy Strategy Determination","text":"

      This problem has a more efficient solution. As shown in Figure 15-8, select a state \\([i, j]\\) where index \\(i < j\\) and height \\(ht[i] < ht[j]\\), meaning \\(i\\) is the short partition and \\(j\\) is the long partition.

      Figure 15-8 \u00a0 Initial state

      As shown in Figure 15-9, if we now move the long partition \\(j\\) closer to the short partition \\(i\\), the capacity will definitely decrease.

      This is because after moving the long partition \\(j\\), the width \\(j-i\\) definitely decreases; and since height is determined by the short partition, the height can only remain unchanged (\\(i\\) is still the short partition) or decrease (the moved \\(j\\) becomes the short partition).

      Figure 15-9 \u00a0 State after moving the long partition inward

      Conversely, we can only possibly increase capacity by contracting the short partition \\(i\\) inward. Because although width will definitely decrease, height may increase (the moved short partition \\(i\\) may become taller). For example, in Figure 15-10, the area increases after moving the short partition.

      Figure 15-10 \u00a0 State after moving the short partition inward

      From this we can derive the greedy strategy for this problem: initialize two pointers at both ends of the container, and in each round contract the pointer corresponding to the short partition inward, until the two pointers meet.

      Figure 15-11 shows the execution process of the greedy strategy.

      1. In the initial state, pointers \\(i\\) and \\(j\\) are at both ends of the array.
      2. Calculate the capacity of the current state \\(cap[i, j]\\), and update the maximum capacity.
      3. Compare the heights of partition \\(i\\) and partition \\(j\\), and move the short partition inward by one position.
      4. Loop through steps 2. and 3. until \\(i\\) and \\(j\\) meet.
      <1><2><3><4><5><6><7><8><9>

      Figure 15-11 \u00a0 Greedy process for the max capacity problem

      "},{"location":"chapter_greedy/max_capacity_problem/#2-code-implementation","title":"2. \u00a0 Code Implementation","text":"

      The code loops at most \\(n\\) rounds, therefore the time complexity is \\(O(n)\\).

      Variables \\(i\\), \\(j\\), and \\(res\\) use a constant amount of extra space, therefore the space complexity is \\(O(1)\\).

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby max_capacity.py
      def max_capacity(ht: list[int]) -> int:\n    \"\"\"Max capacity: Greedy algorithm\"\"\"\n    # Initialize i, j to be at both ends of the array\n    i, j = 0, len(ht) - 1\n    # Initial max capacity is 0\n    res = 0\n    # Loop for greedy selection until the two boards meet\n    while i < j:\n        # Update max capacity\n        cap = min(ht[i], ht[j]) * (j - i)\n        res = max(res, cap)\n        # Move the shorter board inward\n        if ht[i] < ht[j]:\n            i += 1\n        else:\n            j -= 1\n    return res\n
      max_capacity.cpp
      /* Max capacity: Greedy algorithm */\nint maxCapacity(vector<int> &ht) {\n    // Initialize i, j to be at both ends of the array\n    int i = 0, j = ht.size() - 1;\n    // Initial max capacity is 0\n    int res = 0;\n    // Loop for greedy selection until the two boards meet\n    while (i < j) {\n        // Update max capacity\n        int cap = min(ht[i], ht[j]) * (j - i);\n        res = max(res, cap);\n        // Move the shorter board inward\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
      max_capacity.java
      /* Max capacity: Greedy algorithm */\nint maxCapacity(int[] ht) {\n    // Initialize i, j to be at both ends of the array\n    int i = 0, j = ht.length - 1;\n    // Initial max capacity is 0\n    int res = 0;\n    // Loop for greedy selection until the two boards meet\n    while (i < j) {\n        // Update max capacity\n        int cap = Math.min(ht[i], ht[j]) * (j - i);\n        res = Math.max(res, cap);\n        // Move the shorter board inward\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
      max_capacity.cs
      /* Max capacity: Greedy algorithm */\nint MaxCapacity(int[] ht) {\n    // Initialize i, j to be at both ends of the array\n    int i = 0, j = ht.Length - 1;\n    // Initial max capacity is 0\n    int res = 0;\n    // Loop for greedy selection until the two boards meet\n    while (i < j) {\n        // Update max capacity\n        int cap = Math.Min(ht[i], ht[j]) * (j - i);\n        res = Math.Max(res, cap);\n        // Move the shorter board inward\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
      max_capacity.go
      /* Max capacity: Greedy algorithm */\nfunc maxCapacity(ht []int) int {\n    // Initialize i, j to be at both ends of the array\n    i, j := 0, len(ht)-1\n    // Initial max capacity is 0\n    res := 0\n    // Loop for greedy selection until the two boards meet\n    for i < j {\n        // Update max capacity\n        capacity := int(math.Min(float64(ht[i]), float64(ht[j]))) * (j - i)\n        res = int(math.Max(float64(res), float64(capacity)))\n        // Move the shorter board inward\n        if ht[i] < ht[j] {\n            i++\n        } else {\n            j--\n        }\n    }\n    return res\n}\n
      max_capacity.swift
      /* Max capacity: Greedy algorithm */\nfunc maxCapacity(ht: [Int]) -> Int {\n    // Initialize i, j to be at both ends of the array\n    var i = ht.startIndex, j = ht.endIndex - 1\n    // Initial max capacity is 0\n    var res = 0\n    // Loop for greedy selection until the two boards meet\n    while i < j {\n        // Update max capacity\n        let cap = min(ht[i], ht[j]) * (j - i)\n        res = max(res, cap)\n        // Move the shorter board inward\n        if ht[i] < ht[j] {\n            i += 1\n        } else {\n            j -= 1\n        }\n    }\n    return res\n}\n
      max_capacity.js
      /* Max capacity: Greedy algorithm */\nfunction maxCapacity(ht) {\n    // Initialize i, j to be at both ends of the array\n    let i = 0,\n        j = ht.length - 1;\n    // Initial max capacity is 0\n    let res = 0;\n    // Loop for greedy selection until the two boards meet\n    while (i < j) {\n        // Update max capacity\n        const cap = Math.min(ht[i], ht[j]) * (j - i);\n        res = Math.max(res, cap);\n        // Move the shorter board inward\n        if (ht[i] < ht[j]) {\n            i += 1;\n        } else {\n            j -= 1;\n        }\n    }\n    return res;\n}\n
      max_capacity.ts
      /* Max capacity: Greedy algorithm */\nfunction maxCapacity(ht: number[]): number {\n    // Initialize i, j to be at both ends of the array\n    let i = 0,\n        j = ht.length - 1;\n    // Initial max capacity is 0\n    let res = 0;\n    // Loop for greedy selection until the two boards meet\n    while (i < j) {\n        // Update max capacity\n        const cap: number = Math.min(ht[i], ht[j]) * (j - i);\n        res = Math.max(res, cap);\n        // Move the shorter board inward\n        if (ht[i] < ht[j]) {\n            i += 1;\n        } else {\n            j -= 1;\n        }\n    }\n    return res;\n}\n
      max_capacity.dart
      /* Max capacity: Greedy algorithm */\nint maxCapacity(List<int> ht) {\n  // Initialize i, j to be at both ends of the array\n  int i = 0, j = ht.length - 1;\n  // Initial max capacity is 0\n  int res = 0;\n  // Loop for greedy selection until the two boards meet\n  while (i < j) {\n    // Update max capacity\n    int cap = min(ht[i], ht[j]) * (j - i);\n    res = max(res, cap);\n    // Move the shorter board inward\n    if (ht[i] < ht[j]) {\n      i++;\n    } else {\n      j--;\n    }\n  }\n  return res;\n}\n
      max_capacity.rs
      /* Max capacity: Greedy algorithm */\nfn max_capacity(ht: &[i32]) -> i32 {\n    // Initialize i, j to be at both ends of the array\n    let mut i = 0;\n    let mut j = ht.len() - 1;\n    // Initial max capacity is 0\n    let mut res = 0;\n    // Loop for greedy selection until the two boards meet\n    while i < j {\n        // Update max capacity\n        let cap = std::cmp::min(ht[i], ht[j]) * (j - i) as i32;\n        res = std::cmp::max(res, cap);\n        // Move the shorter board inward\n        if ht[i] < ht[j] {\n            i += 1;\n        } else {\n            j -= 1;\n        }\n    }\n    res\n}\n
      max_capacity.c
      /* Max capacity: Greedy algorithm */\nint maxCapacity(int ht[], int htLength) {\n    // Initialize i, j to be at both ends of the array\n    int i = 0;\n    int j = htLength - 1;\n    // Initial max capacity is 0\n    int res = 0;\n    // Loop for greedy selection until the two boards meet\n    while (i < j) {\n        // Update max capacity\n        int capacity = myMin(ht[i], ht[j]) * (j - i);\n        res = myMax(res, capacity);\n        // Move the shorter board inward\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
      max_capacity.kt
      /* Max capacity: Greedy algorithm */\nfun maxCapacity(ht: IntArray): Int {\n    // Initialize i, j to be at both ends of the array\n    var i = 0\n    var j = ht.size - 1\n    // Initial max capacity is 0\n    var res = 0\n    // Loop for greedy selection until the two boards meet\n    while (i < j) {\n        // Update max capacity\n        val cap = min(ht[i], ht[j]) * (j - i)\n        res = max(res, cap)\n        // Move the shorter board inward\n        if (ht[i] < ht[j]) {\n            i++\n        } else {\n            j--\n        }\n    }\n    return res\n}\n
      max_capacity.rb
      ### Maximum capacity: greedy ###\ndef max_capacity(ht)\n  # Initialize i, j to be at both ends of the array\n  i, j = 0, ht.length - 1\n  # Initial max capacity is 0\n  res = 0\n\n  # Loop for greedy selection until the two boards meet\n  while i < j\n    # Update max capacity\n    cap = [ht[i], ht[j]].min * (j - i)\n    res = [res, cap].max\n    # Move the shorter board inward\n    if ht[i] < ht[j]\n      i += 1\n    else\n      j -= 1\n    end\n  end\n\n  res\nend\n
      "},{"location":"chapter_greedy/max_capacity_problem/#3-correctness-proof","title":"3. \u00a0 Correctness Proof","text":"

      The reason greedy is faster than exhaustive enumeration is that each round of greedy selection \"skips\" some states.

      For example, in state \\(cap[i, j]\\) where \\(i\\) is the short partition and \\(j\\) is the long partition, if we greedily move the short partition \\(i\\) inward by one position, the states shown in Figure 15-12 will be \"skipped\". This means that the capacities of these states cannot be verified later.

      \\[ cap[i, i+1], cap[i, i+2], \\dots, cap[i, j-2], cap[i, j-1] \\]

      Figure 15-12 \u00a0 States skipped by moving the short partition

      Observing carefully, these skipped states are actually all the states obtained by moving the long partition \\(j\\) inward. We have already proven that moving the long partition inward will definitely decrease capacity. That is, the skipped states cannot possibly be the optimal solution, skipping them will not cause us to miss the optimal solution.

      The above analysis shows that the operation of moving the short partition is \"safe\", and the greedy strategy is effective.

      "},{"location":"chapter_greedy/max_product_cutting_problem/","title":"15.4 \u00a0 Max Product Cutting Problem","text":"

      Question

      Given a positive integer \\(n\\), split it into the sum of at least two positive integers, and find the maximum product of all integers after splitting, as shown in Figure 15-13.

      Figure 15-13 \u00a0 Problem definition of max product cutting

      Suppose we split \\(n\\) into \\(m\\) integer factors, where the \\(i\\)-th factor is denoted as \\(n_i\\), that is

      \\[ n = \\sum_{i=1}^{m}n_i \\]

      The goal of this problem is to find the maximum product of all integer factors, namely

      \\[ \\max(\\prod_{i=1}^{m}n_i) \\]

      We need to think about: how large should the splitting count \\(m\\) be, and what should each \\(n_i\\) be?

      "},{"location":"chapter_greedy/max_product_cutting_problem/#1-greedy-strategy-determination","title":"1. \u00a0 Greedy Strategy Determination","text":"

      Based on experience, the product of two integers is often greater than their sum. Suppose we split out a factor of \\(2\\) from \\(n\\), then their product is \\(2(n-2)\\). We compare this product with \\(n\\):

      \\[ \\begin{aligned} 2(n-2) & \\geq n \\newline 2n - n - 4 & \\geq 0 \\newline n & \\geq 4 \\end{aligned} \\]

      As shown in Figure 15-14, when \\(n \\geq 4\\), splitting out a \\(2\\) will increase the product, which indicates that integers greater than or equal to \\(4\\) should all be split.

      Greedy strategy one: If the splitting scheme includes factors \\(\\geq 4\\), then they should continue to be split. The final splitting scheme should only contain factors \\(1\\), \\(2\\), and \\(3\\).

      Figure 15-14 \u00a0 Splitting causes product to increase

      Next, consider which factor is optimal. Among the three factors \\(1\\), \\(2\\), and \\(3\\), clearly \\(1\\) is the worst, because \\(1 \\times (n-1) < n\\) always holds, meaning splitting out \\(1\\) will actually decrease the product.

      As shown in Figure 15-15, when \\(n = 6\\), we have \\(3 \\times 3 > 2 \\times 2 \\times 2\\). This means that splitting out \\(3\\) is better than splitting out \\(2\\).

      Greedy strategy two: In the splitting scheme, there should be at most two \\(2\\)s. Because three \\(2\\)s can always be replaced by two \\(3\\)s to obtain a larger product.

      Figure 15-15 \u00a0 Optimal splitting factor

      In summary, the following greedy strategies can be derived.

      1. Input integer \\(n\\), continuously split out factor \\(3\\) until the remainder is \\(0\\), \\(1\\), or \\(2\\).
      2. When the remainder is \\(0\\), it means \\(n\\) is a multiple of \\(3\\), so no further action is needed.
      3. When the remainder is \\(2\\), do not continue splitting, keep it.
      4. When the remainder is \\(1\\), since \\(2 \\times 2 > 1 \\times 3\\), the last \\(3\\) should be replaced with \\(2\\).
      "},{"location":"chapter_greedy/max_product_cutting_problem/#2-code-implementation","title":"2. \u00a0 Code Implementation","text":"

      As shown in Figure 15-16, we don't need to use loops to split the integer, but can use integer division to get the count of \\(3\\)s as \\(a\\), and modulo operation to get the remainder as \\(b\\), at which point we have:

      \\[ n = 3 a + b \\]

      Please note that for the edge case of \\(n \\leq 3\\), a \\(1\\) must be split out, with product \\(1 \\times (n - 1)\\).

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby max_product_cutting.py
      def max_product_cutting(n: int) -> int:\n    \"\"\"Max product cutting: Greedy algorithm\"\"\"\n    # When n <= 3, must cut out a 1\n    if n <= 3:\n        return 1 * (n - 1)\n    # Greedily cut out 3, a is the number of 3s, b is the remainder\n    a, b = n // 3, n % 3\n    if b == 1:\n        # When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n        return int(math.pow(3, a - 1)) * 2 * 2\n    if b == 2:\n        # When the remainder is 2, do nothing\n        return int(math.pow(3, a)) * 2\n    # When the remainder is 0, do nothing\n    return int(math.pow(3, a))\n
      max_product_cutting.cpp
      /* Max product cutting: Greedy algorithm */\nint maxProductCutting(int n) {\n    // When n <= 3, must cut out a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // Greedily cut out 3, a is the number of 3s, b is the remainder\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n        return (int)pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // When the remainder is 2, do nothing\n        return (int)pow(3, a) * 2;\n    }\n    // When the remainder is 0, do nothing\n    return (int)pow(3, a);\n}\n
      max_product_cutting.java
      /* Max product cutting: Greedy algorithm */\nint maxProductCutting(int n) {\n    // When n <= 3, must cut out a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // Greedily cut out 3, a is the number of 3s, b is the remainder\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n        return (int) Math.pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // When the remainder is 2, do nothing\n        return (int) Math.pow(3, a) * 2;\n    }\n    // When the remainder is 0, do nothing\n    return (int) Math.pow(3, a);\n}\n
      max_product_cutting.cs
      /* Max product cutting: Greedy algorithm */\nint MaxProductCutting(int n) {\n    // When n <= 3, must cut out a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // Greedily cut out 3, a is the number of 3s, b is the remainder\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n        return (int)Math.Pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // When the remainder is 2, do nothing\n        return (int)Math.Pow(3, a) * 2;\n    }\n    // When the remainder is 0, do nothing\n    return (int)Math.Pow(3, a);\n}\n
      max_product_cutting.go
      /* Max product cutting: Greedy algorithm */\nfunc maxProductCutting(n int) int {\n    // When n <= 3, must cut out a 1\n    if n <= 3 {\n        return 1 * (n - 1)\n    }\n    // Greedily cut out 3, a is the number of 3s, b is the remainder\n    a := n / 3\n    b := n % 3\n    if b == 1 {\n        // When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n        return int(math.Pow(3, float64(a-1))) * 2 * 2\n    }\n    if b == 2 {\n        // When the remainder is 2, do nothing\n        return int(math.Pow(3, float64(a))) * 2\n    }\n    // When the remainder is 0, do nothing\n    return int(math.Pow(3, float64(a)))\n}\n
      max_product_cutting.swift
      /* Max product cutting: Greedy algorithm */\nfunc maxProductCutting(n: Int) -> Int {\n    // When n <= 3, must cut out a 1\n    if n <= 3 {\n        return 1 * (n - 1)\n    }\n    // Greedily cut out 3, a is the number of 3s, b is the remainder\n    let a = n / 3\n    let b = n % 3\n    if b == 1 {\n        // When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n        return pow(3, a - 1) * 2 * 2\n    }\n    if b == 2 {\n        // When the remainder is 2, do nothing\n        return pow(3, a) * 2\n    }\n    // When the remainder is 0, do nothing\n    return pow(3, a)\n}\n
      max_product_cutting.js
      /* Max product cutting: Greedy algorithm */\nfunction maxProductCutting(n) {\n    // When n <= 3, must cut out a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // Greedily cut out 3, a is the number of 3s, b is the remainder\n    let a = Math.floor(n / 3);\n    let b = n % 3;\n    if (b === 1) {\n        // When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n        return Math.pow(3, a - 1) * 2 * 2;\n    }\n    if (b === 2) {\n        // When the remainder is 2, do nothing\n        return Math.pow(3, a) * 2;\n    }\n    // When the remainder is 0, do nothing\n    return Math.pow(3, a);\n}\n
      max_product_cutting.ts
      /* Max product cutting: Greedy algorithm */\nfunction maxProductCutting(n: number): number {\n    // When n <= 3, must cut out a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // Greedily cut out 3, a is the number of 3s, b is the remainder\n    let a: number = Math.floor(n / 3);\n    let b: number = n % 3;\n    if (b === 1) {\n        // When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n        return Math.pow(3, a - 1) * 2 * 2;\n    }\n    if (b === 2) {\n        // When the remainder is 2, do nothing\n        return Math.pow(3, a) * 2;\n    }\n    // When the remainder is 0, do nothing\n    return Math.pow(3, a);\n}\n
      max_product_cutting.dart
      /* Max product cutting: Greedy algorithm */\nint maxProductCutting(int n) {\n  // When n <= 3, must cut out a 1\n  if (n <= 3) {\n    return 1 * (n - 1);\n  }\n  // Greedily cut out 3, a is the number of 3s, b is the remainder\n  int a = n ~/ 3;\n  int b = n % 3;\n  if (b == 1) {\n    // When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n    return (pow(3, a - 1) * 2 * 2).toInt();\n  }\n  if (b == 2) {\n    // When the remainder is 2, do nothing\n    return (pow(3, a) * 2).toInt();\n  }\n  // When the remainder is 0, do nothing\n  return pow(3, a).toInt();\n}\n
      max_product_cutting.rs
      /* Max product cutting: Greedy algorithm */\nfn max_product_cutting(n: i32) -> i32 {\n    // When n <= 3, must cut out a 1\n    if n <= 3 {\n        return 1 * (n - 1);\n    }\n    // Greedily cut out 3, a is the number of 3s, b is the remainder\n    let a = n / 3;\n    let b = n % 3;\n    if b == 1 {\n        // When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n        3_i32.pow(a as u32 - 1) * 2 * 2\n    } else if b == 2 {\n        // When the remainder is 2, do nothing\n        3_i32.pow(a as u32) * 2\n    } else {\n        // When the remainder is 0, do nothing\n        3_i32.pow(a as u32)\n    }\n}\n
      max_product_cutting.c
      /* Max product cutting: Greedy algorithm */\nint maxProductCutting(int n) {\n    // When n <= 3, must cut out a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // Greedily cut out 3, a is the number of 3s, b is the remainder\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n        return pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // When the remainder is 2, do nothing\n        return pow(3, a) * 2;\n    }\n    // When the remainder is 0, do nothing\n    return pow(3, a);\n}\n
      max_product_cutting.kt
      /* Max product cutting: Greedy algorithm */\nfun maxProductCutting(n: Int): Int {\n    // When n <= 3, must cut out a 1\n    if (n <= 3) {\n        return 1 * (n - 1)\n    }\n    // Greedily cut out 3, a is the number of 3s, b is the remainder\n    val a = n / 3\n    val b = n % 3\n    if (b == 1) {\n        // When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n        return 3.0.pow((a - 1)).toInt() * 2 * 2\n    }\n    if (b == 2) {\n        // When the remainder is 2, do nothing\n        return 3.0.pow(a).toInt() * 2 * 2\n    }\n    // When the remainder is 0, do nothing\n    return 3.0.pow(a).toInt()\n}\n
      max_product_cutting.rb
      ### Maximum cutting product: greedy ###\ndef max_product_cutting(n)\n  # When n <= 3, must cut out a 1\n  return 1 * (n - 1) if n <= 3\n  # Greedily cut out 3, a is the number of 3s, b is the remainder\n  a, b = n / 3, n % 3\n  # When the remainder is 1, convert a pair of 1 * 3 to 2 * 2\n  return (3.pow(a - 1) * 2 * 2).to_i if b == 1\n  # When the remainder is 2, do nothing\n  return (3.pow(a) * 2).to_i if b == 2\n  # When the remainder is 0, do nothing\n  3.pow(a).to_i\nend\n

      Figure 15-16 \u00a0 Calculation method for max product cutting

      The time complexity depends on the implementation of the exponentiation operation in the programming language. Taking Python as an example, there are three commonly used power calculation functions.

      • Both the operator ** and the function pow() have time complexity \\(O(\\log\u2061 a)\\).
      • The function math.pow() internally calls the C library's pow() function, which performs floating-point exponentiation, with time complexity \\(O(1)\\).

      Variables \\(a\\) and \\(b\\) use a constant amount of extra space, therefore the space complexity is \\(O(1)\\).

      "},{"location":"chapter_greedy/max_product_cutting_problem/#3-correctness-proof","title":"3. \u00a0 Correctness Proof","text":"

      Using proof by contradiction, only analyzing the case where \\(n \\geq 4\\).

      1. All factors \\(\\leq 3\\): Suppose the optimal splitting scheme includes a factor \\(x \\geq 4\\), then it can definitely continue to be split into \\(2(x-2)\\) to obtain a larger (or equal) product. This contradicts the assumption.
      2. The splitting scheme does not contain \\(1\\): Suppose the optimal splitting scheme includes a factor of \\(1\\), then it can definitely be merged into another factor to obtain a larger product. This contradicts the assumption.
      3. The splitting scheme contains at most two \\(2\\)s: Suppose the optimal splitting scheme includes three \\(2\\)s, then they can definitely be replaced by two \\(3\\)s for a larger product. This contradicts the assumption.
      "},{"location":"chapter_greedy/summary/","title":"15.5 \u00a0 Summary","text":""},{"location":"chapter_greedy/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • Greedy algorithms are typically used to solve optimization problems. The principle is to make locally optimal decisions at each decision stage in hopes of obtaining a globally optimal solution.
      • Greedy algorithms iteratively make one greedy choice after another, transforming the problem into a smaller subproblem in each round, until the problem is solved.
      • Greedy algorithms are not only simple to implement, but also have high problem-solving efficiency. Compared to dynamic programming, greedy algorithms typically have lower time complexity.
      • In the coin change problem, for certain coin combinations, greedy algorithms can guarantee finding the optimal solution; for other coin combinations, however, greedy algorithms may find very poor solutions.
      • Problems suitable for solving with greedy algorithms have two major properties: greedy choice property and optimal substructure. The greedy choice property represents the effectiveness of the greedy strategy.
      • For some complex problems, proving the greedy choice property is not simple. Relatively speaking, disproving it is easier, such as in the coin change problem.
      • Solving greedy problems mainly consists of three steps: problem analysis, determining the greedy strategy, and correctness proof. Among these, determining the greedy strategy is the core step, and correctness proof is often the difficult point.
      • The fractional knapsack problem, based on the 0-1 knapsack problem, allows selecting a portion of items, and therefore can be solved using greedy algorithms. The correctness of the greedy strategy can be proven using proof by contradiction.
      • The max capacity problem can be solved using exhaustive enumeration with time complexity \\(O(n^2)\\). By designing a greedy strategy to move the short partition inward in each round, the time complexity can be optimized to \\(O(n)\\).
      • In the max product cutting problem, we successively derive two greedy strategies: integers \\(\\geq 4\\) should all continue to be split, and the optimal splitting factor is \\(3\\). The code includes exponentiation operations, and the time complexity depends on the implementation method of exponentiation, typically being \\(O(1)\\) or \\(O(\\log n)\\).
      "},{"location":"chapter_hashing/","title":"Chapter 6. \u00a0 Hashing","text":"

      Abstract

      In the world of computing, a hash table is like a clever librarian.

      They know how to calculate call numbers, enabling them to quickly locate the target book.

      "},{"location":"chapter_hashing/#chapter-contents","title":"Chapter contents","text":"
      • 6.1 \u00a0 Hash Table
      • 6.2 \u00a0 Hash Collision
      • 6.3 \u00a0 Hash Algorithm
      • 6.4 \u00a0 Summary
      "},{"location":"chapter_hashing/hash_algorithm/","title":"6.3 \u00a0 Hash Algorithm","text":"

      The previous two sections introduced the working principle of hash tables and the methods to handle hash collisions. However, both open addressing and separate chaining can only ensure that the hash table functions normally when hash collisions occur, but cannot reduce the frequency of hash collisions.

      If hash collisions occur too frequently, the performance of the hash table will deteriorate drastically. As shown in Figure 6-8, for a separate chaining hash table, in the ideal case, the key-value pairs are evenly distributed across the buckets, achieving optimal query efficiency; in the worst case, all key-value pairs are stored in the same bucket, degrading the time complexity to \\(O(n)\\).

      Figure 6-8 \u00a0 Ideal and worst cases of hash collisions

      The distribution of key-value pairs is determined by the hash function. Recalling the calculation steps of the hash function, first compute the hash value, then take the modulo by the array length:

      index = hash(key) % capacity\n

      Observing the above formula, when the hash table capacity capacity is fixed, the hash algorithm hash() determines the output value, thereby determining the distribution of key-value pairs in the hash table.

      This means that, to reduce the probability of hash collisions, we should focus on the design of the hash algorithm hash().

      "},{"location":"chapter_hashing/hash_algorithm/#631-goals-of-hash-algorithms","title":"6.3.1 \u00a0 Goals of Hash Algorithms","text":"

      To achieve a \"fast and stable\" hash table data structure, hash algorithms should have the following characteristics:

      • Determinism: For the same input, the hash algorithm should always produce the same output. Only then can the hash table be reliable.
      • High efficiency: The process of computing the hash value should be fast enough. The smaller the computational overhead, the more practical the hash table.
      • Uniform distribution: The hash algorithm should ensure that key-value pairs are evenly distributed in the hash table. The more uniform the distribution, the lower the probability of hash collisions.

      In fact, hash algorithms are not only used to implement hash tables but are also widely applied in other fields.

      • Password storage: To protect the security of user passwords, systems usually do not store the plaintext passwords but rather the hash values of the passwords. When a user enters a password, the system calculates the hash value of the input and compares it with the stored hash value. If they match, the password is considered correct.
      • Data integrity check: The data sender can calculate the hash value of the data and send it along; the receiver can recalculate the hash value of the received data and compare it with the received hash value. If they match, the data is considered intact.

      For cryptographic applications, to prevent reverse engineering such as deducing the original password from the hash value, hash algorithms need higher-level security features.

      • Unidirectionality: It should be impossible to deduce any information about the input data from the hash value.
      • Collision resistance: It should be extremely difficult to find two different inputs that produce the same hash value.
      • Avalanche effect: Minor changes in the input should lead to significant and unpredictable changes in the output.

      Note that \"uniform distribution\" and \"collision resistance\" are two independent concepts. Satisfying uniform distribution does not necessarily mean collision resistance. For example, under random input key, the hash function key % 100 can produce a uniformly distributed output. However, this hash algorithm is too simple, and all key with the same last two digits will have the same output, making it easy to deduce a usable key from the hash value, thereby cracking the password.

      "},{"location":"chapter_hashing/hash_algorithm/#632-design-of-hash-algorithms","title":"6.3.2 \u00a0 Design of Hash Algorithms","text":"

      The design of hash algorithms is a complex issue that requires consideration of many factors. However, for some less demanding scenarios, we can also design some simple hash algorithms.

      • Additive hash: Add up the ASCII codes of each character in the input and use the total sum as the hash value.
      • Multiplicative hash: Utilize the non-correlation of multiplication, multiplying each round by a constant, accumulating the ASCII codes of each character into the hash value.
      • XOR hash: Accumulate the hash value by XORing each element of the input data.
      • Rotating hash: Accumulate the ASCII code of each character into a hash value, performing a rotation operation on the hash value before each accumulation.
      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby simple_hash.py
      def add_hash(key: str) -> int:\n    \"\"\"Additive hash\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash += ord(c)\n    return hash % modulus\n\ndef mul_hash(key: str) -> int:\n    \"\"\"Multiplicative hash\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash = 31 * hash + ord(c)\n    return hash % modulus\n\ndef xor_hash(key: str) -> int:\n    \"\"\"XOR hash\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash ^= ord(c)\n    return hash % modulus\n\ndef rot_hash(key: str) -> int:\n    \"\"\"Rotational hash\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash = (hash << 4) ^ (hash >> 28) ^ ord(c)\n    return hash % modulus\n
      simple_hash.cpp
      /* Additive hash */\nint addHash(string key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash = (hash + (int)c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* Multiplicative hash */\nint mulHash(string key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash = (31 * hash + (int)c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* XOR hash */\nint xorHash(string key) {\n    int hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash ^= (int)c;\n    }\n    return hash & MODULUS;\n}\n\n/* Rotational hash */\nint rotHash(string key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ (int)c) % MODULUS;\n    }\n    return (int)hash;\n}\n
      simple_hash.java
      /* Additive hash */\nint addHash(String key) {\n    long hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash = (hash + (int) c) % MODULUS;\n    }\n    return (int) hash;\n}\n\n/* Multiplicative hash */\nint mulHash(String key) {\n    long hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash = (31 * hash + (int) c) % MODULUS;\n    }\n    return (int) hash;\n}\n\n/* XOR hash */\nint xorHash(String key) {\n    int hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash ^= (int) c;\n    }\n    return hash & MODULUS;\n}\n\n/* Rotational hash */\nint rotHash(String key) {\n    long hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ (int) c) % MODULUS;\n    }\n    return (int) hash;\n}\n
      simple_hash.cs
      /* Additive hash */\nint AddHash(string key) {\n    long hash = 0;\n    const int MODULUS = 1000000007;\n    foreach (char c in key) {\n        hash = (hash + c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* Multiplicative hash */\nint MulHash(string key) {\n    long hash = 0;\n    const int MODULUS = 1000000007;\n    foreach (char c in key) {\n        hash = (31 * hash + c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* XOR hash */\nint XorHash(string key) {\n    int hash = 0;\n    const int MODULUS = 1000000007;\n    foreach (char c in key) {\n        hash ^= c;\n    }\n    return hash & MODULUS;\n}\n\n/* Rotational hash */\nint RotHash(string key) {\n    long hash = 0;\n    const int MODULUS = 1000000007;\n    foreach (char c in key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ c) % MODULUS;\n    }\n    return (int)hash;\n}\n
      simple_hash.go
      /* Additive hash */\nfunc addHash(key string) int {\n    var hash int64\n    var modulus int64\n\n    modulus = 1000000007\n    for _, b := range []byte(key) {\n        hash = (hash + int64(b)) % modulus\n    }\n    return int(hash)\n}\n\n/* Multiplicative hash */\nfunc mulHash(key string) int {\n    var hash int64\n    var modulus int64\n\n    modulus = 1000000007\n    for _, b := range []byte(key) {\n        hash = (31*hash + int64(b)) % modulus\n    }\n    return int(hash)\n}\n\n/* XOR hash */\nfunc xorHash(key string) int {\n    hash := 0\n    modulus := 1000000007\n    for _, b := range []byte(key) {\n        fmt.Println(int(b))\n        hash ^= int(b)\n        hash = (31*hash + int(b)) % modulus\n    }\n    return hash & modulus\n}\n\n/* Rotational hash */\nfunc rotHash(key string) int {\n    var hash int64\n    var modulus int64\n\n    modulus = 1000000007\n    for _, b := range []byte(key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ int64(b)) % modulus\n    }\n    return int(hash)\n}\n
      simple_hash.swift
      /* Additive hash */\nfunc addHash(key: String) -> Int {\n    var hash = 0\n    let MODULUS = 1_000_000_007\n    for c in key {\n        for scalar in c.unicodeScalars {\n            hash = (hash + Int(scalar.value)) % MODULUS\n        }\n    }\n    return hash\n}\n\n/* Multiplicative hash */\nfunc mulHash(key: String) -> Int {\n    var hash = 0\n    let MODULUS = 1_000_000_007\n    for c in key {\n        for scalar in c.unicodeScalars {\n            hash = (31 * hash + Int(scalar.value)) % MODULUS\n        }\n    }\n    return hash\n}\n\n/* XOR hash */\nfunc xorHash(key: String) -> Int {\n    var hash = 0\n    let MODULUS = 1_000_000_007\n    for c in key {\n        for scalar in c.unicodeScalars {\n            hash ^= Int(scalar.value)\n        }\n    }\n    return hash & MODULUS\n}\n\n/* Rotational hash */\nfunc rotHash(key: String) -> Int {\n    var hash = 0\n    let MODULUS = 1_000_000_007\n    for c in key {\n        for scalar in c.unicodeScalars {\n            hash = ((hash << 4) ^ (hash >> 28) ^ Int(scalar.value)) % MODULUS\n        }\n    }\n    return hash\n}\n
      simple_hash.js
      /* Additive hash */\nfunction addHash(key) {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = (hash + c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n\n/* Multiplicative hash */\nfunction mulHash(key) {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n\n/* XOR hash */\nfunction xorHash(key) {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash ^= c.charCodeAt(0);\n    }\n    return hash % MODULUS;\n}\n\n/* Rotational hash */\nfunction rotHash(key) {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n
      simple_hash.ts
      /* Additive hash */\nfunction addHash(key: string): number {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = (hash + c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n\n/* Multiplicative hash */\nfunction mulHash(key: string): number {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n\n/* XOR hash */\nfunction xorHash(key: string): number {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash ^= c.charCodeAt(0);\n    }\n    return hash % MODULUS;\n}\n\n/* Rotational hash */\nfunction rotHash(key: string): number {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n
      simple_hash.dart
      /* Additive hash */\nint addHash(String key) {\n  int hash = 0;\n  final int MODULUS = 1000000007;\n  for (int i = 0; i < key.length; i++) {\n    hash = (hash + key.codeUnitAt(i)) % MODULUS;\n  }\n  return hash;\n}\n\n/* Multiplicative hash */\nint mulHash(String key) {\n  int hash = 0;\n  final int MODULUS = 1000000007;\n  for (int i = 0; i < key.length; i++) {\n    hash = (31 * hash + key.codeUnitAt(i)) % MODULUS;\n  }\n  return hash;\n}\n\n/* XOR hash */\nint xorHash(String key) {\n  int hash = 0;\n  final int MODULUS = 1000000007;\n  for (int i = 0; i < key.length; i++) {\n    hash ^= key.codeUnitAt(i);\n  }\n  return hash & MODULUS;\n}\n\n/* Rotational hash */\nint rotHash(String key) {\n  int hash = 0;\n  final int MODULUS = 1000000007;\n  for (int i = 0; i < key.length; i++) {\n    hash = ((hash << 4) ^ (hash >> 28) ^ key.codeUnitAt(i)) % MODULUS;\n  }\n  return hash;\n}\n
      simple_hash.rs
      /* Additive hash */\nfn add_hash(key: &str) -> i32 {\n    let mut hash = 0_i64;\n    const MODULUS: i64 = 1000000007;\n\n    for c in key.chars() {\n        hash = (hash + c as i64) % MODULUS;\n    }\n\n    hash as i32\n}\n\n/* Multiplicative hash */\nfn mul_hash(key: &str) -> i32 {\n    let mut hash = 0_i64;\n    const MODULUS: i64 = 1000000007;\n\n    for c in key.chars() {\n        hash = (31 * hash + c as i64) % MODULUS;\n    }\n\n    hash as i32\n}\n\n/* XOR hash */\nfn xor_hash(key: &str) -> i32 {\n    let mut hash = 0_i64;\n    const MODULUS: i64 = 1000000007;\n\n    for c in key.chars() {\n        hash ^= c as i64;\n    }\n\n    (hash & MODULUS) as i32\n}\n\n/* Rotational hash */\nfn rot_hash(key: &str) -> i32 {\n    let mut hash = 0_i64;\n    const MODULUS: i64 = 1000000007;\n\n    for c in key.chars() {\n        hash = ((hash << 4) ^ (hash >> 28) ^ c as i64) % MODULUS;\n    }\n\n    hash as i32\n}\n
      simple_hash.c
      /* Additive hash */\nint addHash(char *key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (int i = 0; i < strlen(key); i++) {\n        hash = (hash + (unsigned char)key[i]) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* Multiplicative hash */\nint mulHash(char *key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (int i = 0; i < strlen(key); i++) {\n        hash = (31 * hash + (unsigned char)key[i]) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* XOR hash */\nint xorHash(char *key) {\n    int hash = 0;\n    const int MODULUS = 1000000007;\n\n    for (int i = 0; i < strlen(key); i++) {\n        hash ^= (unsigned char)key[i];\n    }\n    return hash & MODULUS;\n}\n\n/* Rotational hash */\nint rotHash(char *key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (int i = 0; i < strlen(key); i++) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ (unsigned char)key[i]) % MODULUS;\n    }\n\n    return (int)hash;\n}\n
      simple_hash.kt
      /* Additive hash */\nfun addHash(key: String): Int {\n    var hash = 0L\n    val MODULUS = 1000000007\n    for (c in key.toCharArray()) {\n        hash = (hash + c.code) % MODULUS\n    }\n    return hash.toInt()\n}\n\n/* Multiplicative hash */\nfun mulHash(key: String): Int {\n    var hash = 0L\n    val MODULUS = 1000000007\n    for (c in key.toCharArray()) {\n        hash = (31 * hash + c.code) % MODULUS\n    }\n    return hash.toInt()\n}\n\n/* XOR hash */\nfun xorHash(key: String): Int {\n    var hash = 0\n    val MODULUS = 1000000007\n    for (c in key.toCharArray()) {\n        hash = hash xor c.code\n    }\n    return hash and MODULUS\n}\n\n/* Rotational hash */\nfun rotHash(key: String): Int {\n    var hash = 0L\n    val MODULUS = 1000000007\n    for (c in key.toCharArray()) {\n        hash = ((hash shl 4) xor (hash shr 28) xor c.code.toLong()) % MODULUS\n    }\n    return hash.toInt()\n}\n
      simple_hash.rb
      ### Additive hash ###\ndef add_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash += c.ord }\n\n  hash % modulus\nend\n\n### Multiplicative hash ###\ndef mul_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash = 31 * hash + c.ord }\n\n  hash % modulus\nend\n\n### XOR hash ###\ndef xor_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash ^= c.ord }\n\n  hash % modulus\nend\n\n### Rotational hash ###\ndef rot_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash = (hash << 4) ^ (hash >> 28) ^ c.ord }\n\n  hash % modulus\nend\n

      It is observed that the last step of each hash algorithm is to take the modulus of the large prime number \\(1000000007\\) to ensure that the hash value is within an appropriate range. It is worth pondering why emphasis is placed on modulo a prime number, or what are the disadvantages of modulo a composite number? This is an interesting question.

      To conclude: Using a large prime number as the modulus can maximize the uniform distribution of hash values. Since a prime number does not share common factors with other numbers, it can reduce the periodic patterns caused by the modulo operation, thus avoiding hash collisions.

      For example, suppose we choose the composite number \\(9\\) as the modulus, which can be divided by \\(3\\), then all key divisible by \\(3\\) will be mapped to hash values \\(0\\), \\(3\\), \\(6\\).

      \\[ \\begin{aligned} \\text{modulus} & = 9 \\newline \\text{key} & = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\dots \\} \\newline \\text{hash} & = \\{ 0, 3, 6, 0, 3, 6, 0, 3, 6, 0, 3, 6,\\dots \\} \\end{aligned} \\]

      If the input key happens to have this kind of arithmetic sequence distribution, then the hash values will cluster, thereby exacerbating hash collisions. Now, suppose we replace modulus with the prime number \\(13\\), since there are no common factors between key and modulus, the uniformity of the output hash values will be significantly improved.

      \\[ \\begin{aligned} \\text{modulus} & = 13 \\newline \\text{key} & = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\dots \\} \\newline \\text{hash} & = \\{ 0, 3, 6, 9, 12, 2, 5, 8, 11, 1, 4, 7, \\dots \\} \\end{aligned} \\]

      It is worth noting that if the key is guaranteed to be randomly and uniformly distributed, then choosing a prime number or a composite number as the modulus can both produce uniformly distributed hash values. However, when the distribution of key has some periodicity, modulo a composite number is more likely to result in clustering.

      In summary, we usually choose a prime number as the modulus, and this prime number should be large enough to eliminate periodic patterns as much as possible, enhancing the robustness of the hash algorithm.

      "},{"location":"chapter_hashing/hash_algorithm/#633-common-hash-algorithms","title":"6.3.3 \u00a0 Common Hash Algorithms","text":"

      It is not hard to see that the simple hash algorithms mentioned above are quite \"fragile\" and far from reaching the design goals of hash algorithms. For example, since addition and XOR obey the commutative law, additive hash and XOR hash cannot distinguish strings with the same content but in different order, which may exacerbate hash collisions and cause security issues.

      In practice, we usually use some standard hash algorithms, such as MD5, SHA-1, SHA-2, and SHA-3. They can map input data of any length to a fixed-length hash value.

      Over the past century, hash algorithms have been in a continuous process of upgrading and optimization. Some researchers strive to improve the performance of hash algorithms, while others, including hackers, are dedicated to finding security issues in hash algorithms. Table 6-2 shows hash algorithms commonly used in practical applications.

      • MD5 and SHA-1 have been successfully attacked multiple times and are thus abandoned in various security applications.
      • SHA-2 series, especially SHA-256, is one of the most secure hash algorithms to date, with no successful attacks reported, hence commonly used in various security applications and protocols.
      • SHA-3 has lower implementation costs and higher computational efficiency compared to SHA-2, but its current usage coverage is not as extensive as the SHA-2 series.

      Table 6-2 \u00a0 Common hash algorithms

      MD5 SHA-1 SHA-2 SHA-3 Release Year 1992 1995 2002 2008 Output Length 128 bit 160 bit 256/512 bit 224/256/384/512 bit Hash Collisions Frequent Frequent Rare Rare Security Level Low, has been successfully attacked Low, has been successfully attacked High High Applications Abandoned, still used for data integrity checks Abandoned Cryptocurrency transaction verification, digital signatures, etc. Can be used to replace SHA-2"},{"location":"chapter_hashing/hash_algorithm/#hash-values-in-data-structures","title":"Hash Values in Data Structures","text":"

      We know that the keys in a hash table can be of various data types such as integers, decimals, or strings. Programming languages usually provide built-in hash algorithms for these data types to calculate the bucket indices in the hash table. Taking Python as an example, we can use the hash() function to compute the hash values for various data types.

      • The hash values of integers and booleans are their own values.
      • The calculation of hash values for floating-point numbers and strings is more complex, and interested readers are encouraged to study this on their own.
      • The hash value of a tuple is a combination of the hash values of each of its elements, resulting in a single hash value.
      • The hash value of an object is generated based on its memory address. By overriding the hash method of an object, hash values can be generated based on content.

      Tip

      Be aware that the definition and methods of the built-in hash value calculation functions in different programming languages vary.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby built_in_hash.py
      num = 3\nhash_num = hash(num)\n# Hash value of integer 3 is 3\n\nbol = True\nhash_bol = hash(bol)\n# Hash value of boolean True is 1\n\ndec = 3.14159\nhash_dec = hash(dec)\n# Hash value of decimal 3.14159 is 326484311674566659\n\nstr = \"Hello \u7b97\u6cd5\"\nhash_str = hash(str)\n# Hash value of string \"Hello \u7b97\u6cd5\" is 4617003410720528961\n\ntup = (12836, \"\u5c0f\u54c8\")\nhash_tup = hash(tup)\n# Hash value of tuple (12836, '\u5c0f\u54c8') is 1029005403108185979\n\nobj = ListNode(0)\nhash_obj = hash(obj)\n# Hash value of ListNode object at 0x1058fd810 is 274267521\n
      built_in_hash.cpp
      int num = 3;\nsize_t hashNum = hash<int>()(num);\n// Hash value of integer 3 is 3\n\nbool bol = true;\nsize_t hashBol = hash<bool>()(bol);\n// Hash value of boolean 1 is 1\n\ndouble dec = 3.14159;\nsize_t hashDec = hash<double>()(dec);\n// Hash value of decimal 3.14159 is 4614256650576692846\n\nstring str = \"Hello \u7b97\u6cd5\";\nsize_t hashStr = hash<string>()(str);\n// Hash value of string \"Hello \u7b97\u6cd5\" is 15466937326284535026\n\n// In C++, built-in std::hash() only provides hash values for basic data types\n// Hash values for arrays and objects need to be implemented separately\n
      built_in_hash.java
      int num = 3;\nint hashNum = Integer.hashCode(num);\n// Hash value of integer 3 is 3\n\nboolean bol = true;\nint hashBol = Boolean.hashCode(bol);\n// Hash value of boolean true is 1231\n\ndouble dec = 3.14159;\nint hashDec = Double.hashCode(dec);\n// Hash value of decimal 3.14159 is -1340954729\n\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode();\n// Hash value of string \"Hello \u7b97\u6cd5\" is -727081396\n\nObject[] arr = { 12836, \"\u5c0f\u54c8\" };\nint hashTup = Arrays.hashCode(arr);\n// Hash value of array [12836, \u5c0f\u54c8] is 1151158\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode();\n// Hash value of ListNode object utils.ListNode@7dc5e7b4 is 2110121908\n
      built_in_hash.cs
      int num = 3;\nint hashNum = num.GetHashCode();\n// Hash value of integer 3 is 3;\n\nbool bol = true;\nint hashBol = bol.GetHashCode();\n// Hash value of boolean true is 1;\n\ndouble dec = 3.14159;\nint hashDec = dec.GetHashCode();\n// Hash value of decimal 3.14159 is -1340954729;\n\nstring str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.GetHashCode();\n// Hash value of string \"Hello \u7b97\u6cd5\" is -586107568;\n\nobject[] arr = [12836, \"\u5c0f\u54c8\"];\nint hashTup = arr.GetHashCode();\n// Hash value of array [12836, \u5c0f\u54c8] is 42931033;\n\nListNode obj = new(0);\nint hashObj = obj.GetHashCode();\n// Hash value of ListNode object 0 is 39053774;\n
      built_in_hash.go
      // Go does not provide built-in hash code functions\n
      built_in_hash.swift
      let num = 3\nlet hashNum = num.hashValue\n// Hash value of integer 3 is 9047044699613009734\n\nlet bol = true\nlet hashBol = bol.hashValue\n// Hash value of boolean true is -4431640247352757451\n\nlet dec = 3.14159\nlet hashDec = dec.hashValue\n// Hash value of decimal 3.14159 is -2465384235396674631\n\nlet str = \"Hello \u7b97\u6cd5\"\nlet hashStr = str.hashValue\n// Hash value of string \"Hello \u7b97\u6cd5\" is -7850626797806988787\n\nlet arr = [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")]\nlet hashTup = arr.hashValue\n// Hash value of array [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")] is -2308633508154532996\n\nlet obj = ListNode(x: 0)\nlet hashObj = obj.hashValue\n// Hash value of ListNode object utils.ListNode is -2434780518035996159\n
      built_in_hash.js
      // JavaScript does not provide built-in hash code functions\n
      built_in_hash.ts
      // TypeScript does not provide built-in hash code functions\n
      built_in_hash.dart
      int num = 3;\nint hashNum = num.hashCode;\n// Hash value of integer 3 is 34803\n\nbool bol = true;\nint hashBol = bol.hashCode;\n// Hash value of boolean true is 1231\n\ndouble dec = 3.14159;\nint hashDec = dec.hashCode;\n// Hash value of decimal 3.14159 is 2570631074981783\n\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode;\n// Hash value of string \"Hello \u7b97\u6cd5\" is 468167534\n\nList arr = [12836, \"\u5c0f\u54c8\"];\nint hashArr = arr.hashCode;\n// Hash value of array [12836, \u5c0f\u54c8] is 976512528\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode;\n// Hash value of ListNode object Instance of 'ListNode' is 1033450432\n
      built_in_hash.rs
      use std::collections::hash_map::DefaultHasher;\nuse std::hash::{Hash, Hasher};\n\nlet num = 3;\nlet mut num_hasher = DefaultHasher::new();\nnum.hash(&mut num_hasher);\nlet hash_num = num_hasher.finish();\n// Hash value of integer 3 is 568126464209439262\n\nlet bol = true;\nlet mut bol_hasher = DefaultHasher::new();\nbol.hash(&mut bol_hasher);\nlet hash_bol = bol_hasher.finish();\n// Hash value of boolean true is 4952851536318644461\n\nlet dec: f32 = 3.14159;\nlet mut dec_hasher = DefaultHasher::new();\ndec.to_bits().hash(&mut dec_hasher);\nlet hash_dec = dec_hasher.finish();\n// Hash value of decimal 3.14159 is 2566941990314602357\n\nlet str = \"Hello \u7b97\u6cd5\";\nlet mut str_hasher = DefaultHasher::new();\nstr.hash(&mut str_hasher);\nlet hash_str = str_hasher.finish();\n// Hash value of string \"Hello \u7b97\u6cd5\" is 16092673739211250988\n\nlet arr = (&12836, &\"\u5c0f\u54c8\");\nlet mut tup_hasher = DefaultHasher::new();\narr.hash(&mut tup_hasher);\nlet hash_tup = tup_hasher.finish();\n// Hash value of tuple (12836, \"\u5c0f\u54c8\") is 1885128010422702749\n\nlet node = ListNode::new(42);\nlet mut hasher = DefaultHasher::new();\nnode.borrow().val.hash(&mut hasher);\nlet hash = hasher.finish();\n// Hash value of ListNode object RefCell { value: ListNode { val: 42, next: None } } is 15387811073369036852\n
      built_in_hash.c
      // C does not provide built-in hash code functions\n
      built_in_hash.kt
      val num = 3\nval hashNum = num.hashCode()\n// Hash value of integer 3 is 3\n\nval bol = true\nval hashBol = bol.hashCode()\n// Hash value of boolean true is 1231\n\nval dec = 3.14159\nval hashDec = dec.hashCode()\n// Hash value of decimal 3.14159 is -1340954729\n\nval str = \"Hello \u7b97\u6cd5\"\nval hashStr = str.hashCode()\n// Hash value of string \"Hello \u7b97\u6cd5\" is -727081396\n\nval arr = arrayOf<Any>(12836, \"\u5c0f\u54c8\")\nval hashTup = arr.hashCode()\n// Hash value of array [12836, \u5c0f\u54c8] is 189568618\n\nval obj = ListNode(0)\nval hashObj = obj.hashCode()\n// Hash value of ListNode object utils.ListNode@1d81eb93 is 495053715\n
      built_in_hash.rb
      num = 3\nhash_num = num.hash\n# Hash value of integer 3 is -4385856518450339636\n\nbol = true\nhash_bol = bol.hash\n# Hash value of boolean true is -1617938112149317027\n\ndec = 3.14159\nhash_dec = dec.hash\n# Hash value of decimal 3.14159 is -1479186995943067893\n\nstr = \"Hello \u7b97\u6cd5\"\nhash_str = str.hash\n# Hash value of string \"Hello \u7b97\u6cd5\" is -4075943250025831763\n\ntup = [12836, '\u5c0f\u54c8']\nhash_tup = tup.hash\n# Hash value of tuple (12836, '\u5c0f\u54c8') is 1999544809202288822\n\nobj = ListNode.new(0)\nhash_obj = obj.hash\n# Hash value of ListNode object #<ListNode:0x000078133140ab70> is 4302940560806366381\n
      Visualized Execution

      https://pythontutor.com/render.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20num%20%3D%203%0A%20%20%20%20hash_num%20%3D%20hash%28num%29%0A%20%20%20%20%23%20%E6%95%B4%E6%95%B0%203%20%E7%9A%84%E5%93%88%E5%B8%8C%E5%80%BC%E4%B8%BA%203%0A%0A%20%20%20%20bol%20%3D%20True%0A%20%20%20%20hash_bol%20%3D%20hash%28bol%29%0A%20%20%20%20%23%20%E5%B8%83%E5%B0%94%E9%87%8F%20True%20%E7%9A%84%E5%93%88%E5%B8%8C%E5%80%BC%E4%B8%BA%201%0A%0A%20%20%20%20dec%20%3D%203.14159%0A%20%20%20%20hash_dec%20%3D%20hash%28dec%29%0A%20%20%20%20%23%20%E5%B0%8F%E6%95%B0%203.14159%20%E7%9A%84%E5%93%88%E5%B8%8C%E5%80%BC%E4%B8%BA%20326484311674566659%0A%0A%20%20%20%20str%20%3D%20%22Hello%20%E7%AE%97%E6%B3%95%22%0A%20%20%20%20hash_str%20%3D%20hash%28str%29%0A%20%20%20%20%23%20%E5%AD%97%E7%AC%A6%E4%B8%B2%E2%80%9CHello%20%E7%AE%97%E6%B3%95%E2%80%9D%E7%9A%84%E5%93%88%E5%B8%8C%E5%80%BC%E4%B8%BA%204617003410720528961%0A%0A%20%20%20%20tup%20%3D%20%2812836,%20%22%E5%B0%8F%E5%93%88%22%29%0A%20%20%20%20hash_tup%20%3D%20hash%28tup%29%0A%20%20%20%20%23%20%E5%85%83%E7%BB%84%20%2812836,%20'%E5%B0%8F%E5%93%88'%29%20%E7%9A%84%E5%93%88%E5%B8%8C%E5%80%BC%E4%B8%BA%201029005403108185979%0A%0A%20%20%20%20obj%20%3D%20ListNode%280%29%0A%20%20%20%20hash_obj%20%3D%20hash%28obj%29%0A%20%20%20%20%23%20%E8%8A%82%E7%82%B9%E5%AF%B9%E8%B1%A1%20%3CListNode%20object%20at%200x1058fd810%3E%20%E7%9A%84%E5%93%88%E5%B8%8C%E5%80%BC%E4%B8%BA%20274267521&cumulative=false&curInstr=19&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

      In many programming languages, only immutable objects can serve as the key in a hash table. If we use a list (dynamic array) as a key, when the contents of the list change, its hash value also changes, and we would no longer be able to find the original value in the hash table.

      Although the member variables of a custom object (such as a linked list node) are mutable, it is hashable. This is because the hash value of an object is usually generated based on its memory address, and even if the contents of the object change, the memory address remains the same, so the hash value remains unchanged.

      You might have noticed that the hash values output in different consoles are different. This is because the Python interpreter adds a random salt to the string hash function each time it starts up. This approach effectively prevents HashDoS attacks and enhances the security of the hash algorithm.

      "},{"location":"chapter_hashing/hash_collision/","title":"6.2 \u00a0 Hash Collision","text":"

      The previous section mentioned that, in most cases, the input space of a hash function is much larger than the output space, so theoretically, hash collisions are inevitable. For example, if the input space is all integers and the output space is the array capacity size, then multiple integers will inevitably be mapped to the same bucket index.

      Hash collisions can lead to incorrect query results, severely impacting the usability of the hash table. To address this issue, whenever a hash collision occurs, we can perform hash table expansion until the collision disappears. This approach is simple, straightforward, and effective, but it is very inefficient because hash table expansion involves a large amount of data migration and hash value recalculation. To improve efficiency, we can adopt the following strategies:

      1. Improve the hash table data structure so that the hash table can function normally when hash collisions occur.
      2. Only expand when necessary, that is, only when hash collisions are severe.

      The main methods for improving the structure of hash tables include \"separate chaining\" and \"open addressing\".

      "},{"location":"chapter_hashing/hash_collision/#621-separate-chaining","title":"6.2.1 \u00a0 Separate Chaining","text":"

      In the original hash table, each bucket can store only one key-value pair. Separate chaining converts a single element into a linked list, treating key-value pairs as linked list nodes and storing all colliding key-value pairs in the same linked list. Figure 6-5 shows an example of a separate chaining hash table.

      Figure 6-5 \u00a0 Separate chaining hash table

      The operations of a hash table implemented with separate chaining have changed as follows:

      • Querying elements: Input key, obtain the bucket index through the hash function, then access the head node of the linked list, then traverse the linked list and compare key to find the target key-value pair.
      • Adding elements: First access the linked list head node through the hash function, then append the node (key-value pair) to the linked list.
      • Deleting elements: Access the head of the linked list based on the result of the hash function, then traverse the linked list to find the target node and delete it.

      Separate chaining has the following limitations:

      • Increased Space Usage: The linked list contains node pointers, which consume more memory space than arrays.
      • Reduced Query Efficiency: This is because linear traversal of the linked list is required to find the corresponding element.

      The code below provides a simple implementation of a separate chaining hash table, with two things to note:

      • Lists (dynamic arrays) are used instead of linked lists to simplify the code. In this setup, the hash table (array) contains multiple buckets, each of which is a list.
      • This implementation includes a hash table expansion method. When the load factor exceeds \\(\\frac{2}{3}\\), we expand the hash table to \\(2\\) times its original size.
      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map_chaining.py
      class HashMapChaining:\n    \"\"\"Hash table with separate chaining\"\"\"\n\n    def __init__(self):\n        \"\"\"Constructor\"\"\"\n        self.size = 0  # Number of key-value pairs\n        self.capacity = 4  # Hash table capacity\n        self.load_thres = 2.0 / 3.0  # Load factor threshold for triggering expansion\n        self.extend_ratio = 2  # Expansion multiplier\n        self.buckets = [[] for _ in range(self.capacity)]  # Bucket array\n\n    def hash_func(self, key: int) -> int:\n        \"\"\"Hash function\"\"\"\n        return key % self.capacity\n\n    def load_factor(self) -> float:\n        \"\"\"Load factor\"\"\"\n        return self.size / self.capacity\n\n    def get(self, key: int) -> str | None:\n        \"\"\"Query operation\"\"\"\n        index = self.hash_func(key)\n        bucket = self.buckets[index]\n        # Traverse bucket, if key is found, return corresponding val\n        for pair in bucket:\n            if pair.key == key:\n                return pair.val\n        # If key is not found, return None\n        return None\n\n    def put(self, key: int, val: str):\n        \"\"\"Add operation\"\"\"\n        # When load factor exceeds threshold, perform expansion\n        if self.load_factor() > self.load_thres:\n            self.extend()\n        index = self.hash_func(key)\n        bucket = self.buckets[index]\n        # Traverse bucket, if specified key is encountered, update corresponding val and return\n        for pair in bucket:\n            if pair.key == key:\n                pair.val = val\n                return\n        # If key does not exist, append key-value pair to the end\n        pair = Pair(key, val)\n        bucket.append(pair)\n        self.size += 1\n\n    def remove(self, key: int):\n        \"\"\"Remove operation\"\"\"\n        index = self.hash_func(key)\n        bucket = self.buckets[index]\n        # Traverse bucket and remove key-value pair from it\n        for pair in bucket:\n            if pair.key == key:\n                bucket.remove(pair)\n                self.size -= 1\n                break\n\n    def extend(self):\n        \"\"\"Expand hash table\"\"\"\n        # Temporarily store the original hash table\n        buckets = self.buckets\n        # Initialize expanded new hash table\n        self.capacity *= self.extend_ratio\n        self.buckets = [[] for _ in range(self.capacity)]\n        self.size = 0\n        # Move key-value pairs from original hash table to new hash table\n        for bucket in buckets:\n            for pair in bucket:\n                self.put(pair.key, pair.val)\n\n    def print(self):\n        \"\"\"Print hash table\"\"\"\n        for bucket in self.buckets:\n            res = []\n            for pair in bucket:\n                res.append(str(pair.key) + \" -> \" + pair.val)\n            print(res)\n
      hash_map_chaining.cpp
      /* Hash table with separate chaining */\nclass HashMapChaining {\n  private:\n    int size;                       // Number of key-value pairs\n    int capacity;                   // Hash table capacity\n    double loadThres;               // Load factor threshold for triggering expansion\n    int extendRatio;                // Expansion multiplier\n    vector<vector<Pair *>> buckets; // Bucket array\n\n  public:\n    /* Constructor */\n    HashMapChaining() : size(0), capacity(4), loadThres(2.0 / 3.0), extendRatio(2) {\n        buckets.resize(capacity);\n    }\n\n    /* Destructor */\n    ~HashMapChaining() {\n        for (auto &bucket : buckets) {\n            for (Pair *pair : bucket) {\n                // Free memory\n                delete pair;\n            }\n        }\n    }\n\n    /* Hash function */\n    int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* Load factor */\n    double loadFactor() {\n        return (double)size / (double)capacity;\n    }\n\n    /* Query operation */\n    string get(int key) {\n        int index = hashFunc(key);\n        // Traverse bucket, if key is found, return corresponding val\n        for (Pair *pair : buckets[index]) {\n            if (pair->key == key) {\n                return pair->val;\n            }\n        }\n        // Return empty string if key not found\n        return \"\";\n    }\n\n    /* Add operation */\n    void put(int key, string val) {\n        // When load factor exceeds threshold, perform expansion\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        int index = hashFunc(key);\n        // Traverse bucket, if specified key is encountered, update corresponding val and return\n        for (Pair *pair : buckets[index]) {\n            if (pair->key == key) {\n                pair->val = val;\n                return;\n            }\n        }\n        // If key does not exist, append key-value pair to the end\n        buckets[index].push_back(new Pair(key, val));\n        size++;\n    }\n\n    /* Remove operation */\n    void remove(int key) {\n        int index = hashFunc(key);\n        auto &bucket = buckets[index];\n        // Traverse bucket and remove key-value pair from it\n        for (int i = 0; i < bucket.size(); i++) {\n            if (bucket[i]->key == key) {\n                Pair *tmp = bucket[i];\n                bucket.erase(bucket.begin() + i); // Remove key-value pair from it\n                delete tmp;                       // Free memory\n                size--;\n                return;\n            }\n        }\n    }\n\n    /* Expand hash table */\n    void extend() {\n        // Temporarily store the original hash table\n        vector<vector<Pair *>> bucketsTmp = buckets;\n        // Initialize expanded new hash table\n        capacity *= extendRatio;\n        buckets.clear();\n        buckets.resize(capacity);\n        size = 0;\n        // Move key-value pairs from original hash table to new hash table\n        for (auto &bucket : bucketsTmp) {\n            for (Pair *pair : bucket) {\n                put(pair->key, pair->val);\n                // Free memory\n                delete pair;\n            }\n        }\n    }\n\n    /* Print hash table */\n    void print() {\n        for (auto &bucket : buckets) {\n            cout << \"[\";\n            for (Pair *pair : bucket) {\n                cout << pair->key << \" -> \" << pair->val << \", \";\n            }\n            cout << \"]\\n\";\n        }\n    }\n};\n
      hash_map_chaining.java
      /* Hash table with separate chaining */\nclass HashMapChaining {\n    int size; // Number of key-value pairs\n    int capacity; // Hash table capacity\n    double loadThres; // Load factor threshold for triggering expansion\n    int extendRatio; // Expansion multiplier\n    List<List<Pair>> buckets; // Bucket array\n\n    /* Constructor */\n    public HashMapChaining() {\n        size = 0;\n        capacity = 4;\n        loadThres = 2.0 / 3.0;\n        extendRatio = 2;\n        buckets = new ArrayList<>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.add(new ArrayList<>());\n        }\n    }\n\n    /* Hash function */\n    int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* Load factor */\n    double loadFactor() {\n        return (double) size / capacity;\n    }\n\n    /* Query operation */\n    String get(int key) {\n        int index = hashFunc(key);\n        List<Pair> bucket = buckets.get(index);\n        // Traverse bucket, if key is found, return corresponding val\n        for (Pair pair : bucket) {\n            if (pair.key == key) {\n                return pair.val;\n            }\n        }\n        // If key is not found, return null\n        return null;\n    }\n\n    /* Add operation */\n    void put(int key, String val) {\n        // When load factor exceeds threshold, perform expansion\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        int index = hashFunc(key);\n        List<Pair> bucket = buckets.get(index);\n        // Traverse bucket, if specified key is encountered, update corresponding val and return\n        for (Pair pair : bucket) {\n            if (pair.key == key) {\n                pair.val = val;\n                return;\n            }\n        }\n        // If key does not exist, append key-value pair to the end\n        Pair pair = new Pair(key, val);\n        bucket.add(pair);\n        size++;\n    }\n\n    /* Remove operation */\n    void remove(int key) {\n        int index = hashFunc(key);\n        List<Pair> bucket = buckets.get(index);\n        // Traverse bucket and remove key-value pair from it\n        for (Pair pair : bucket) {\n            if (pair.key == key) {\n                bucket.remove(pair);\n                size--;\n                break;\n            }\n        }\n    }\n\n    /* Expand hash table */\n    void extend() {\n        // Temporarily store the original hash table\n        List<List<Pair>> bucketsTmp = buckets;\n        // Initialize expanded new hash table\n        capacity *= extendRatio;\n        buckets = new ArrayList<>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.add(new ArrayList<>());\n        }\n        size = 0;\n        // Move key-value pairs from original hash table to new hash table\n        for (List<Pair> bucket : bucketsTmp) {\n            for (Pair pair : bucket) {\n                put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* Print hash table */\n    void print() {\n        for (List<Pair> bucket : buckets) {\n            List<String> res = new ArrayList<>();\n            for (Pair pair : bucket) {\n                res.add(pair.key + \" -> \" + pair.val);\n            }\n            System.out.println(res);\n        }\n    }\n}\n
      hash_map_chaining.cs
      /* Hash table with separate chaining */\nclass HashMapChaining {\n    int size; // Number of key-value pairs\n    int capacity; // Hash table capacity\n    double loadThres; // Load factor threshold for triggering expansion\n    int extendRatio; // Expansion multiplier\n    List<List<Pair>> buckets; // Bucket array\n\n    /* Constructor */\n    public HashMapChaining() {\n        size = 0;\n        capacity = 4;\n        loadThres = 2.0 / 3.0;\n        extendRatio = 2;\n        buckets = new List<List<Pair>>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.Add([]);\n        }\n    }\n\n    /* Hash function */\n    int HashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* Load factor */\n    double LoadFactor() {\n        return (double)size / capacity;\n    }\n\n    /* Query operation */\n    public string? Get(int key) {\n        int index = HashFunc(key);\n        // Traverse bucket, if key is found, return corresponding val\n        foreach (Pair pair in buckets[index]) {\n            if (pair.key == key) {\n                return pair.val;\n            }\n        }\n        // If key is not found, return null\n        return null;\n    }\n\n    /* Add operation */\n    public void Put(int key, string val) {\n        // When load factor exceeds threshold, perform expansion\n        if (LoadFactor() > loadThres) {\n            Extend();\n        }\n        int index = HashFunc(key);\n        // Traverse bucket, if specified key is encountered, update corresponding val and return\n        foreach (Pair pair in buckets[index]) {\n            if (pair.key == key) {\n                pair.val = val;\n                return;\n            }\n        }\n        // If key does not exist, append key-value pair to the end\n        buckets[index].Add(new Pair(key, val));\n        size++;\n    }\n\n    /* Remove operation */\n    public void Remove(int key) {\n        int index = HashFunc(key);\n        // Traverse bucket and remove key-value pair from it\n        foreach (Pair pair in buckets[index].ToList()) {\n            if (pair.key == key) {\n                buckets[index].Remove(pair);\n                size--;\n                break;\n            }\n        }\n    }\n\n    /* Expand hash table */\n    void Extend() {\n        // Temporarily store the original hash table\n        List<List<Pair>> bucketsTmp = buckets;\n        // Initialize expanded new hash table\n        capacity *= extendRatio;\n        buckets = new List<List<Pair>>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.Add([]);\n        }\n        size = 0;\n        // Move key-value pairs from original hash table to new hash table\n        foreach (List<Pair> bucket in bucketsTmp) {\n            foreach (Pair pair in bucket) {\n                Put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* Print hash table */\n    public void Print() {\n        foreach (List<Pair> bucket in buckets) {\n            List<string> res = [];\n            foreach (Pair pair in bucket) {\n                res.Add(pair.key + \" -> \" + pair.val);\n            }\n            foreach (string kv in res) {\n                Console.WriteLine(kv);\n            }\n        }\n    }\n}\n
      hash_map_chaining.go
      /* Hash table with separate chaining */\ntype hashMapChaining struct {\n    size        int      // Number of key-value pairs\n    capacity    int      // Hash table capacity\n    loadThres   float64  // Load factor threshold for triggering expansion\n    extendRatio int      // Expansion multiplier\n    buckets     [][]pair // Bucket array\n}\n\n/* Constructor */\nfunc newHashMapChaining() *hashMapChaining {\n    buckets := make([][]pair, 4)\n    for i := 0; i < 4; i++ {\n        buckets[i] = make([]pair, 0)\n    }\n    return &hashMapChaining{\n        size:        0,\n        capacity:    4,\n        loadThres:   2.0 / 3.0,\n        extendRatio: 2,\n        buckets:     buckets,\n    }\n}\n\n/* Hash function */\nfunc (m *hashMapChaining) hashFunc(key int) int {\n    return key % m.capacity\n}\n\n/* Load factor */\nfunc (m *hashMapChaining) loadFactor() float64 {\n    return float64(m.size) / float64(m.capacity)\n}\n\n/* Query operation */\nfunc (m *hashMapChaining) get(key int) string {\n    idx := m.hashFunc(key)\n    bucket := m.buckets[idx]\n    // Traverse bucket, if key is found, return corresponding val\n    for _, p := range bucket {\n        if p.key == key {\n            return p.val\n        }\n    }\n    // Return empty string if key not found\n    return \"\"\n}\n\n/* Add operation */\nfunc (m *hashMapChaining) put(key int, val string) {\n    // When load factor exceeds threshold, perform expansion\n    if m.loadFactor() > m.loadThres {\n        m.extend()\n    }\n    idx := m.hashFunc(key)\n    // Traverse bucket, if specified key is encountered, update corresponding val and return\n    for i := range m.buckets[idx] {\n        if m.buckets[idx][i].key == key {\n            m.buckets[idx][i].val = val\n            return\n        }\n    }\n    // If key does not exist, append key-value pair to the end\n    p := pair{\n        key: key,\n        val: val,\n    }\n    m.buckets[idx] = append(m.buckets[idx], p)\n    m.size += 1\n}\n\n/* Remove operation */\nfunc (m *hashMapChaining) remove(key int) {\n    idx := m.hashFunc(key)\n    // Traverse bucket and remove key-value pair from it\n    for i, p := range m.buckets[idx] {\n        if p.key == key {\n            // Slice deletion\n            m.buckets[idx] = append(m.buckets[idx][:i], m.buckets[idx][i+1:]...)\n            m.size -= 1\n            break\n        }\n    }\n}\n\n/* Expand hash table */\nfunc (m *hashMapChaining) extend() {\n    // Temporarily store the original hash table\n    tmpBuckets := make([][]pair, len(m.buckets))\n    for i := 0; i < len(m.buckets); i++ {\n        tmpBuckets[i] = make([]pair, len(m.buckets[i]))\n        copy(tmpBuckets[i], m.buckets[i])\n    }\n    // Initialize expanded new hash table\n    m.capacity *= m.extendRatio\n    m.buckets = make([][]pair, m.capacity)\n    for i := 0; i < m.capacity; i++ {\n        m.buckets[i] = make([]pair, 0)\n    }\n    m.size = 0\n    // Move key-value pairs from original hash table to new hash table\n    for _, bucket := range tmpBuckets {\n        for _, p := range bucket {\n            m.put(p.key, p.val)\n        }\n    }\n}\n\n/* Print hash table */\nfunc (m *hashMapChaining) print() {\n    var builder strings.Builder\n\n    for _, bucket := range m.buckets {\n        builder.WriteString(\"[\")\n        for _, p := range bucket {\n            builder.WriteString(strconv.Itoa(p.key) + \" -> \" + p.val + \" \")\n        }\n        builder.WriteString(\"]\")\n        fmt.Println(builder.String())\n        builder.Reset()\n    }\n}\n
      hash_map_chaining.swift
      /* Hash table with separate chaining */\nclass HashMapChaining {\n    var size: Int // Number of key-value pairs\n    var capacity: Int // Hash table capacity\n    var loadThres: Double // Load factor threshold for triggering expansion\n    var extendRatio: Int // Expansion multiplier\n    var buckets: [[Pair]] // Bucket array\n\n    /* Constructor */\n    init() {\n        size = 0\n        capacity = 4\n        loadThres = 2.0 / 3.0\n        extendRatio = 2\n        buckets = Array(repeating: [], count: capacity)\n    }\n\n    /* Hash function */\n    func hashFunc(key: Int) -> Int {\n        key % capacity\n    }\n\n    /* Load factor */\n    func loadFactor() -> Double {\n        Double(size) / Double(capacity)\n    }\n\n    /* Query operation */\n    func get(key: Int) -> String? {\n        let index = hashFunc(key: key)\n        let bucket = buckets[index]\n        // Traverse bucket, if key is found, return corresponding val\n        for pair in bucket {\n            if pair.key == key {\n                return pair.val\n            }\n        }\n        // Return nil if key not found\n        return nil\n    }\n\n    /* Add operation */\n    func put(key: Int, val: String) {\n        // When load factor exceeds threshold, perform expansion\n        if loadFactor() > loadThres {\n            extend()\n        }\n        let index = hashFunc(key: key)\n        let bucket = buckets[index]\n        // Traverse bucket, if specified key is encountered, update corresponding val and return\n        for pair in bucket {\n            if pair.key == key {\n                pair.val = val\n                return\n            }\n        }\n        // If key does not exist, append key-value pair to the end\n        let pair = Pair(key: key, val: val)\n        buckets[index].append(pair)\n        size += 1\n    }\n\n    /* Remove operation */\n    func remove(key: Int) {\n        let index = hashFunc(key: key)\n        let bucket = buckets[index]\n        // Traverse bucket and remove key-value pair from it\n        for (pairIndex, pair) in bucket.enumerated() {\n            if pair.key == key {\n                buckets[index].remove(at: pairIndex)\n                size -= 1\n                break\n            }\n        }\n    }\n\n    /* Expand hash table */\n    func extend() {\n        // Temporarily store the original hash table\n        let bucketsTmp = buckets\n        // Initialize expanded new hash table\n        capacity *= extendRatio\n        buckets = Array(repeating: [], count: capacity)\n        size = 0\n        // Move key-value pairs from original hash table to new hash table\n        for bucket in bucketsTmp {\n            for pair in bucket {\n                put(key: pair.key, val: pair.val)\n            }\n        }\n    }\n\n    /* Print hash table */\n    func print() {\n        for bucket in buckets {\n            let res = bucket.map { \"\\($0.key) -> \\($0.val)\" }\n            Swift.print(res)\n        }\n    }\n}\n
      hash_map_chaining.js
      /* Hash table with separate chaining */\nclass HashMapChaining {\n    #size; // Number of key-value pairs\n    #capacity; // Hash table capacity\n    #loadThres; // Load factor threshold for triggering expansion\n    #extendRatio; // Expansion multiplier\n    #buckets; // Bucket array\n\n    /* Constructor */\n    constructor() {\n        this.#size = 0;\n        this.#capacity = 4;\n        this.#loadThres = 2.0 / 3.0;\n        this.#extendRatio = 2;\n        this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n    }\n\n    /* Hash function */\n    #hashFunc(key) {\n        return key % this.#capacity;\n    }\n\n    /* Load factor */\n    #loadFactor() {\n        return this.#size / this.#capacity;\n    }\n\n    /* Query operation */\n    get(key) {\n        const index = this.#hashFunc(key);\n        const bucket = this.#buckets[index];\n        // Traverse bucket, if key is found, return corresponding val\n        for (const pair of bucket) {\n            if (pair.key === key) {\n                return pair.val;\n            }\n        }\n        // If key is not found, return null\n        return null;\n    }\n\n    /* Add operation */\n    put(key, val) {\n        // When load factor exceeds threshold, perform expansion\n        if (this.#loadFactor() > this.#loadThres) {\n            this.#extend();\n        }\n        const index = this.#hashFunc(key);\n        const bucket = this.#buckets[index];\n        // Traverse bucket, if specified key is encountered, update corresponding val and return\n        for (const pair of bucket) {\n            if (pair.key === key) {\n                pair.val = val;\n                return;\n            }\n        }\n        // If key does not exist, append key-value pair to the end\n        const pair = new Pair(key, val);\n        bucket.push(pair);\n        this.#size++;\n    }\n\n    /* Remove operation */\n    remove(key) {\n        const index = this.#hashFunc(key);\n        let bucket = this.#buckets[index];\n        // Traverse bucket and remove key-value pair from it\n        for (let i = 0; i < bucket.length; i++) {\n            if (bucket[i].key === key) {\n                bucket.splice(i, 1);\n                this.#size--;\n                break;\n            }\n        }\n    }\n\n    /* Expand hash table */\n    #extend() {\n        // Temporarily store the original hash table\n        const bucketsTmp = this.#buckets;\n        // Initialize expanded new hash table\n        this.#capacity *= this.#extendRatio;\n        this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n        this.#size = 0;\n        // Move key-value pairs from original hash table to new hash table\n        for (const bucket of bucketsTmp) {\n            for (const pair of bucket) {\n                this.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* Print hash table */\n    print() {\n        for (const bucket of this.#buckets) {\n            let res = [];\n            for (const pair of bucket) {\n                res.push(pair.key + ' -> ' + pair.val);\n            }\n            console.log(res);\n        }\n    }\n}\n
      hash_map_chaining.ts
      /* Hash table with separate chaining */\nclass HashMapChaining {\n    #size: number; // Number of key-value pairs\n    #capacity: number; // Hash table capacity\n    #loadThres: number; // Load factor threshold for triggering expansion\n    #extendRatio: number; // Expansion multiplier\n    #buckets: Pair[][]; // Bucket array\n\n    /* Constructor */\n    constructor() {\n        this.#size = 0;\n        this.#capacity = 4;\n        this.#loadThres = 2.0 / 3.0;\n        this.#extendRatio = 2;\n        this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n    }\n\n    /* Hash function */\n    #hashFunc(key: number): number {\n        return key % this.#capacity;\n    }\n\n    /* Load factor */\n    #loadFactor(): number {\n        return this.#size / this.#capacity;\n    }\n\n    /* Query operation */\n    get(key: number): string | null {\n        const index = this.#hashFunc(key);\n        const bucket = this.#buckets[index];\n        // Traverse bucket, if key is found, return corresponding val\n        for (const pair of bucket) {\n            if (pair.key === key) {\n                return pair.val;\n            }\n        }\n        // If key is not found, return null\n        return null;\n    }\n\n    /* Add operation */\n    put(key: number, val: string): void {\n        // When load factor exceeds threshold, perform expansion\n        if (this.#loadFactor() > this.#loadThres) {\n            this.#extend();\n        }\n        const index = this.#hashFunc(key);\n        const bucket = this.#buckets[index];\n        // Traverse bucket, if specified key is encountered, update corresponding val and return\n        for (const pair of bucket) {\n            if (pair.key === key) {\n                pair.val = val;\n                return;\n            }\n        }\n        // If key does not exist, append key-value pair to the end\n        const pair = new Pair(key, val);\n        bucket.push(pair);\n        this.#size++;\n    }\n\n    /* Remove operation */\n    remove(key: number): void {\n        const index = this.#hashFunc(key);\n        let bucket = this.#buckets[index];\n        // Traverse bucket and remove key-value pair from it\n        for (let i = 0; i < bucket.length; i++) {\n            if (bucket[i].key === key) {\n                bucket.splice(i, 1);\n                this.#size--;\n                break;\n            }\n        }\n    }\n\n    /* Expand hash table */\n    #extend(): void {\n        // Temporarily store the original hash table\n        const bucketsTmp = this.#buckets;\n        // Initialize expanded new hash table\n        this.#capacity *= this.#extendRatio;\n        this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n        this.#size = 0;\n        // Move key-value pairs from original hash table to new hash table\n        for (const bucket of bucketsTmp) {\n            for (const pair of bucket) {\n                this.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* Print hash table */\n    print(): void {\n        for (const bucket of this.#buckets) {\n            let res = [];\n            for (const pair of bucket) {\n                res.push(pair.key + ' -> ' + pair.val);\n            }\n            console.log(res);\n        }\n    }\n}\n
      hash_map_chaining.dart
      /* Hash table with separate chaining */\nclass HashMapChaining {\n  late int size; // Number of key-value pairs\n  late int capacity; // Hash table capacity\n  late double loadThres; // Load factor threshold for triggering expansion\n  late int extendRatio; // Expansion multiplier\n  late List<List<Pair>> buckets; // Bucket array\n\n  /* Constructor */\n  HashMapChaining() {\n    size = 0;\n    capacity = 4;\n    loadThres = 2.0 / 3.0;\n    extendRatio = 2;\n    buckets = List.generate(capacity, (_) => []);\n  }\n\n  /* Hash function */\n  int hashFunc(int key) {\n    return key % capacity;\n  }\n\n  /* Load factor */\n  double loadFactor() {\n    return size / capacity;\n  }\n\n  /* Query operation */\n  String? get(int key) {\n    int index = hashFunc(key);\n    List<Pair> bucket = buckets[index];\n    // Traverse bucket, if key is found, return corresponding val\n    for (Pair pair in bucket) {\n      if (pair.key == key) {\n        return pair.val;\n      }\n    }\n    // If key is not found, return null\n    return null;\n  }\n\n  /* Add operation */\n  void put(int key, String val) {\n    // When load factor exceeds threshold, perform expansion\n    if (loadFactor() > loadThres) {\n      extend();\n    }\n    int index = hashFunc(key);\n    List<Pair> bucket = buckets[index];\n    // Traverse bucket, if specified key is encountered, update corresponding val and return\n    for (Pair pair in bucket) {\n      if (pair.key == key) {\n        pair.val = val;\n        return;\n      }\n    }\n    // If key does not exist, append key-value pair to the end\n    Pair pair = Pair(key, val);\n    bucket.add(pair);\n    size++;\n  }\n\n  /* Remove operation */\n  void remove(int key) {\n    int index = hashFunc(key);\n    List<Pair> bucket = buckets[index];\n    // Traverse bucket and remove key-value pair from it\n    for (Pair pair in bucket) {\n      if (pair.key == key) {\n        bucket.remove(pair);\n        size--;\n        break;\n      }\n    }\n  }\n\n  /* Expand hash table */\n  void extend() {\n    // Temporarily store the original hash table\n    List<List<Pair>> bucketsTmp = buckets;\n    // Initialize expanded new hash table\n    capacity *= extendRatio;\n    buckets = List.generate(capacity, (_) => []);\n    size = 0;\n    // Move key-value pairs from original hash table to new hash table\n    for (List<Pair> bucket in bucketsTmp) {\n      for (Pair pair in bucket) {\n        put(pair.key, pair.val);\n      }\n    }\n  }\n\n  /* Print hash table */\n  void printHashMap() {\n    for (List<Pair> bucket in buckets) {\n      List<String> res = [];\n      for (Pair pair in bucket) {\n        res.add(\"${pair.key} -> ${pair.val}\");\n      }\n      print(res);\n    }\n  }\n}\n
      hash_map_chaining.rs
      /* Hash table with separate chaining */\nstruct HashMapChaining {\n    size: usize,\n    capacity: usize,\n    load_thres: f32,\n    extend_ratio: usize,\n    buckets: Vec<Vec<Pair>>,\n}\n\nimpl HashMapChaining {\n    /* Constructor */\n    fn new() -> Self {\n        Self {\n            size: 0,\n            capacity: 4,\n            load_thres: 2.0 / 3.0,\n            extend_ratio: 2,\n            buckets: vec![vec![]; 4],\n        }\n    }\n\n    /* Hash function */\n    fn hash_func(&self, key: i32) -> usize {\n        key as usize % self.capacity\n    }\n\n    /* Load factor */\n    fn load_factor(&self) -> f32 {\n        self.size as f32 / self.capacity as f32\n    }\n\n    /* Remove operation */\n    fn remove(&mut self, key: i32) -> Option<String> {\n        let index = self.hash_func(key);\n\n        // Traverse bucket and remove key-value pair from it\n        for (i, p) in self.buckets[index].iter_mut().enumerate() {\n            if p.key == key {\n                let pair = self.buckets[index].remove(i);\n                self.size -= 1;\n                return Some(pair.val);\n            }\n        }\n\n        // If key is not found, return None\n        None\n    }\n\n    /* Expand hash table */\n    fn extend(&mut self) {\n        // Temporarily store the original hash table\n        let buckets_tmp = std::mem::take(&mut self.buckets);\n\n        // Initialize expanded new hash table\n        self.capacity *= self.extend_ratio;\n        self.buckets = vec![Vec::new(); self.capacity as usize];\n        self.size = 0;\n\n        // Move key-value pairs from original hash table to new hash table\n        for bucket in buckets_tmp {\n            for pair in bucket {\n                self.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* Print hash table */\n    fn print(&self) {\n        for bucket in &self.buckets {\n            let mut res = Vec::new();\n            for pair in bucket {\n                res.push(format!(\"{} -> {}\", pair.key, pair.val));\n            }\n            println!(\"{:?}\", res);\n        }\n    }\n\n    /* Add operation */\n    fn put(&mut self, key: i32, val: String) {\n        // When load factor exceeds threshold, perform expansion\n        if self.load_factor() > self.load_thres {\n            self.extend();\n        }\n\n        let index = self.hash_func(key);\n\n        // Traverse bucket, if specified key is encountered, update corresponding val and return\n        for pair in self.buckets[index].iter_mut() {\n            if pair.key == key {\n                pair.val = val;\n                return;\n            }\n        }\n\n        // If key does not exist, append key-value pair to the end\n        let pair = Pair { key, val };\n        self.buckets[index].push(pair);\n        self.size += 1;\n    }\n\n    /* Query operation */\n    fn get(&self, key: i32) -> Option<&str> {\n        let index = self.hash_func(key);\n\n        // Traverse bucket, if key is found, return corresponding val\n        for pair in self.buckets[index].iter() {\n            if pair.key == key {\n                return Some(&pair.val);\n            }\n        }\n\n        // If key is not found, return None\n        None\n    }\n}\n
      hash_map_chaining.c
      /* Linked list node */\ntypedef struct Node {\n    Pair *pair;\n    struct Node *next;\n} Node;\n\n/* Hash table with separate chaining */\ntypedef struct {\n    int size;         // Number of key-value pairs\n    int capacity;     // Hash table capacity\n    double loadThres; // Load factor threshold for triggering expansion\n    int extendRatio;  // Expansion multiplier\n    Node **buckets;   // Bucket array\n} HashMapChaining;\n\n/* Constructor */\nHashMapChaining *newHashMapChaining() {\n    HashMapChaining *hashMap = (HashMapChaining *)malloc(sizeof(HashMapChaining));\n    hashMap->size = 0;\n    hashMap->capacity = 4;\n    hashMap->loadThres = 2.0 / 3.0;\n    hashMap->extendRatio = 2;\n    hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));\n    for (int i = 0; i < hashMap->capacity; i++) {\n        hashMap->buckets[i] = NULL;\n    }\n    return hashMap;\n}\n\n/* Destructor */\nvoid delHashMapChaining(HashMapChaining *hashMap) {\n    for (int i = 0; i < hashMap->capacity; i++) {\n        Node *cur = hashMap->buckets[i];\n        while (cur) {\n            Node *tmp = cur;\n            cur = cur->next;\n            free(tmp->pair);\n            free(tmp);\n        }\n    }\n    free(hashMap->buckets);\n    free(hashMap);\n}\n\n/* Hash function */\nint hashFunc(HashMapChaining *hashMap, int key) {\n    return key % hashMap->capacity;\n}\n\n/* Load factor */\ndouble loadFactor(HashMapChaining *hashMap) {\n    return (double)hashMap->size / (double)hashMap->capacity;\n}\n\n/* Query operation */\nchar *get(HashMapChaining *hashMap, int key) {\n    int index = hashFunc(hashMap, key);\n    // Traverse bucket, if key is found, return corresponding val\n    Node *cur = hashMap->buckets[index];\n    while (cur) {\n        if (cur->pair->key == key) {\n            return cur->pair->val;\n        }\n        cur = cur->next;\n    }\n    return \"\"; // Return empty string if key not found\n}\n\n/* Add operation */\nvoid put(HashMapChaining *hashMap, int key, const char *val) {\n    // When load factor exceeds threshold, perform expansion\n    if (loadFactor(hashMap) > hashMap->loadThres) {\n        extend(hashMap);\n    }\n    int index = hashFunc(hashMap, key);\n    // Traverse bucket, if specified key is encountered, update corresponding val and return\n    Node *cur = hashMap->buckets[index];\n    while (cur) {\n        if (cur->pair->key == key) {\n            strcpy(cur->pair->val, val); // If specified key is found, update corresponding val and return\n            return;\n        }\n        cur = cur->next;\n    }\n    // If key not found, add key-value pair to list head\n    Pair *newPair = (Pair *)malloc(sizeof(Pair));\n    newPair->key = key;\n    strcpy(newPair->val, val);\n    Node *newNode = (Node *)malloc(sizeof(Node));\n    newNode->pair = newPair;\n    newNode->next = hashMap->buckets[index];\n    hashMap->buckets[index] = newNode;\n    hashMap->size++;\n}\n\n/* Expand hash table */\nvoid extend(HashMapChaining *hashMap) {\n    // Temporarily store the original hash table\n    int oldCapacity = hashMap->capacity;\n    Node **oldBuckets = hashMap->buckets;\n    // Initialize expanded new hash table\n    hashMap->capacity *= hashMap->extendRatio;\n    hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));\n    for (int i = 0; i < hashMap->capacity; i++) {\n        hashMap->buckets[i] = NULL;\n    }\n    hashMap->size = 0;\n    // Move key-value pairs from original hash table to new hash table\n    for (int i = 0; i < oldCapacity; i++) {\n        Node *cur = oldBuckets[i];\n        while (cur) {\n            put(hashMap, cur->pair->key, cur->pair->val);\n            Node *temp = cur;\n            cur = cur->next;\n            // Free memory\n            free(temp->pair);\n            free(temp);\n        }\n    }\n\n    free(oldBuckets);\n}\n\n/* Remove operation */\nvoid removeItem(HashMapChaining *hashMap, int key) {\n    int index = hashFunc(hashMap, key);\n    Node *cur = hashMap->buckets[index];\n    Node *pre = NULL;\n    while (cur) {\n        if (cur->pair->key == key) {\n            // Remove key-value pair from it\n            if (pre) {\n                pre->next = cur->next;\n            } else {\n                hashMap->buckets[index] = cur->next;\n            }\n            // Free memory\n            free(cur->pair);\n            free(cur);\n            hashMap->size--;\n            return;\n        }\n        pre = cur;\n        cur = cur->next;\n    }\n}\n\n/* Print hash table */\nvoid print(HashMapChaining *hashMap) {\n    for (int i = 0; i < hashMap->capacity; i++) {\n        Node *cur = hashMap->buckets[i];\n        printf(\"[\");\n        while (cur) {\n            printf(\"%d -> %s, \", cur->pair->key, cur->pair->val);\n            cur = cur->next;\n        }\n        printf(\"]\\n\");\n    }\n}\n
      hash_map_chaining.kt
      /* Hash table with separate chaining */\nclass HashMapChaining {\n    var size: Int // Number of key-value pairs\n    var capacity: Int // Hash table capacity\n    val loadThres: Double // Load factor threshold for triggering expansion\n    val extendRatio: Int // Expansion multiplier\n    var buckets: MutableList<MutableList<Pair>> // Bucket array\n\n    /* Constructor */\n    init {\n        size = 0\n        capacity = 4\n        loadThres = 2.0 / 3.0\n        extendRatio = 2\n        buckets = mutableListOf()\n        for (i in 0..<capacity) {\n            buckets.add(mutableListOf())\n        }\n    }\n\n    /* Hash function */\n    fun hashFunc(key: Int): Int {\n        return key % capacity\n    }\n\n    /* Load factor */\n    fun loadFactor(): Double {\n        return (size / capacity).toDouble()\n    }\n\n    /* Query operation */\n    fun get(key: Int): String? {\n        val index = hashFunc(key)\n        val bucket = buckets[index]\n        // Traverse bucket, if key is found, return corresponding val\n        for (pair in bucket) {\n            if (pair.key == key) return pair._val\n        }\n        // If key is not found, return null\n        return null\n    }\n\n    /* Add operation */\n    fun put(key: Int, _val: String) {\n        // When load factor exceeds threshold, perform expansion\n        if (loadFactor() > loadThres) {\n            extend()\n        }\n        val index = hashFunc(key)\n        val bucket = buckets[index]\n        // Traverse bucket, if specified key is encountered, update corresponding val and return\n        for (pair in bucket) {\n            if (pair.key == key) {\n                pair._val = _val\n                return\n            }\n        }\n        // If key does not exist, append key-value pair to the end\n        val pair = Pair(key, _val)\n        bucket.add(pair)\n        size++\n    }\n\n    /* Remove operation */\n    fun remove(key: Int) {\n        val index = hashFunc(key)\n        val bucket = buckets[index]\n        // Traverse bucket and remove key-value pair from it\n        for (pair in bucket) {\n            if (pair.key == key) {\n                bucket.remove(pair)\n                size--\n                break\n            }\n        }\n    }\n\n    /* Expand hash table */\n    fun extend() {\n        // Temporarily store the original hash table\n        val bucketsTmp = buckets\n        // Initialize expanded new hash table\n        capacity *= extendRatio\n        // mutablelist has no fixed size\n        buckets = mutableListOf()\n        for (i in 0..<capacity) {\n            buckets.add(mutableListOf())\n        }\n        size = 0\n        // Move key-value pairs from original hash table to new hash table\n        for (bucket in bucketsTmp) {\n            for (pair in bucket) {\n                put(pair.key, pair._val)\n            }\n        }\n    }\n\n    /* Print hash table */\n    fun print() {\n        for (bucket in buckets) {\n            val res = mutableListOf<String>()\n            for (pair in bucket) {\n                val k = pair.key\n                val v = pair._val\n                res.add(\"$k -> $v\")\n            }\n            println(res)\n        }\n    }\n}\n
      hash_map_chaining.rb
      ### Hash map with chaining ###\nclass HashMapChaining\n  ### Constructor ###\n  def initialize\n    @size = 0 # Number of key-value pairs\n    @capacity = 4 # Hash table capacity\n    @load_thres = 2.0 / 3.0 # Load factor threshold for triggering expansion\n    @extend_ratio = 2 # Expansion multiplier\n    @buckets = Array.new(@capacity) { [] } # Bucket array\n  end\n\n  ### Hash function ###\n  def hash_func(key)\n    key % @capacity\n  end\n\n  ### Load factor ###\n  def load_factor\n    @size / @capacity\n  end\n\n  ### Query operation ###\n  def get(key)\n    index = hash_func(key)\n    bucket = @buckets[index]\n    # Traverse bucket, if key is found, return corresponding val\n    for pair in bucket\n      return pair.val if pair.key == key\n    end\n    # Return nil if key not found\n    nil\n  end\n\n  ### Add operation ###\n  def put(key, val)\n    # When load factor exceeds threshold, perform expansion\n    extend if load_factor > @load_thres\n    index = hash_func(key)\n    bucket = @buckets[index]\n    # Traverse bucket, if specified key is encountered, update corresponding val and return\n    for pair in bucket\n      if pair.key == key\n        pair.val = val\n        return\n      end\n    end\n    # If key does not exist, append key-value pair to the end\n    pair = Pair.new(key, val)\n    bucket << pair\n    @size += 1\n  end\n\n  ### Delete operation ###\n  def remove(key)\n    index = hash_func(key)\n    bucket = @buckets[index]\n    # Traverse bucket and remove key-value pair from it\n    for pair in bucket\n      if pair.key == key\n        bucket.delete(pair)\n        @size -= 1\n        break\n      end\n    end\n  end\n\n  ### Expand hash table ###\n  def extend\n    # Temporarily store original hash table\n    buckets = @buckets\n    # Initialize expanded new hash table\n    @capacity *= @extend_ratio\n    @buckets = Array.new(@capacity) { [] }\n    @size = 0\n    # Move key-value pairs from original hash table to new hash table\n    for bucket in buckets\n      for pair in bucket\n        put(pair.key, pair.val)\n      end\n    end\n  end\n\n  ### Print hash table ###\n  def print\n    for bucket in @buckets\n      res = []\n      for pair in bucket\n        res << \"#{pair.key} -> #{pair.val}\"\n      end\n      pp res\n    end\n  end\nend\n

      It's worth noting that when the linked list is very long, the query efficiency \\(O(n)\\) is poor. In this case, the list can be converted to an \"AVL tree\" or \"Red-Black tree\" to optimize the time complexity of the query operation to \\(O(\\log n)\\).

      "},{"location":"chapter_hashing/hash_collision/#622-open-addressing","title":"6.2.2 \u00a0 Open Addressing","text":"

      Open addressing does not introduce additional data structures but instead handles hash collisions through \"multiple probes\". The probing methods mainly include linear probing, quadratic probing, and double hashing.

      Let's use linear probing as an example to introduce the mechanism of open addressing hash tables.

      "},{"location":"chapter_hashing/hash_collision/#1-linear-probing","title":"1. \u00a0 Linear Probing","text":"

      Linear probing uses a fixed-step linear search for probing, and its operation method differs from ordinary hash tables.

      • Inserting elements: Calculate the bucket index using the hash function. If the bucket already contains an element, linearly traverse forward from the conflict position (usually with a step size of \\(1\\)) until an empty bucket is found, then insert the element.
      • Searching for elements: If a hash collision is encountered, use the same step size to linearly traverse forward until the corresponding element is found and return value; if an empty bucket is encountered, it means the target element is not in the hash table, so return None.

      Figure 6-6 shows the distribution of key-value pairs in an open addressing (linear probing) hash table. According to this hash function, keys with the same last two digits will be mapped to the same bucket. Through linear probing, they are stored sequentially in that bucket and the buckets below it.

      Figure 6-6 \u00a0 Distribution of key-value pairs in open addressing (linear probing) hash table

      However, linear probing is prone to create \"clustering\". Specifically, the longer the continuously occupied positions in the array, the greater the probability of hash collisions occurring in these continuous positions, further promoting clustering growth at that position, forming a vicious cycle, and ultimately leading to degraded efficiency of insertion, deletion, query, and update operations.

      It's important to note that we cannot directly delete elements in an open addressing hash table. Deleting an element creates an empty bucket None in the array. When searching for elements, if linear probing encounters this empty bucket, it will return, making the elements below this empty bucket inaccessible. The program may incorrectly assume these elements do not exist, as shown in Figure 6-7.

      Figure 6-7 \u00a0 Query issues caused by deletion in open addressing

      To solve this problem, we can adopt the lazy deletion mechanism: instead of directly removing elements from the hash table, use a constant TOMBSTONE to mark the bucket. In this mechanism, both None and TOMBSTONE represent empty buckets and can hold key-value pairs. However, when linear probing encounters TOMBSTONE, it should continue traversing since there may still be key-value pairs below it.

      However, lazy deletion may accelerate the performance degradation of the hash table. Every deletion operation produces a deletion mark, and as TOMBSTONE increases, the search time will also increase because linear probing may need to skip multiple TOMBSTONE to find the target element.

      To address this, consider recording the index of the first encountered TOMBSTONE during linear probing and swapping the searched target element with that TOMBSTONE. The benefit of doing this is that each time an element is queried or added, the element will be moved to a bucket closer to its ideal position (the starting point of probing), thereby optimizing query efficiency.

      The code below implements an open addressing (linear probing) hash table with lazy deletion. To make better use of the hash table space, we treat the hash table as a \"circular array\". When going beyond the end of the array, we return to the beginning and continue traversing.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map_open_addressing.py
      class HashMapOpenAddressing:\n    \"\"\"Hash table with open addressing\"\"\"\n\n    def __init__(self):\n        \"\"\"Constructor\"\"\"\n        self.size = 0  # Number of key-value pairs\n        self.capacity = 4  # Hash table capacity\n        self.load_thres = 2.0 / 3.0  # Load factor threshold for triggering expansion\n        self.extend_ratio = 2  # Expansion multiplier\n        self.buckets: list[Pair | None] = [None] * self.capacity  # Bucket array\n        self.TOMBSTONE = Pair(-1, \"-1\")  # Removal marker\n\n    def hash_func(self, key: int) -> int:\n        \"\"\"Hash function\"\"\"\n        return key % self.capacity\n\n    def load_factor(self) -> float:\n        \"\"\"Load factor\"\"\"\n        return self.size / self.capacity\n\n    def find_bucket(self, key: int) -> int:\n        \"\"\"Search for bucket index corresponding to key\"\"\"\n        index = self.hash_func(key)\n        first_tombstone = -1\n        # Linear probing, break when encountering an empty bucket\n        while self.buckets[index] is not None:\n            # If key is encountered, return the corresponding bucket index\n            if self.buckets[index].key == key:\n                # If a removal marker was encountered before, move the key-value pair to that index\n                if first_tombstone != -1:\n                    self.buckets[first_tombstone] = self.buckets[index]\n                    self.buckets[index] = self.TOMBSTONE\n                    return first_tombstone  # Return the moved bucket index\n                return index  # Return bucket index\n            # Record the first removal marker encountered\n            if first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE:\n                first_tombstone = index\n            # Calculate bucket index, wrap around to the head if past the tail\n            index = (index + 1) % self.capacity\n        # If key does not exist, return the index for insertion\n        return index if first_tombstone == -1 else first_tombstone\n\n    def get(self, key: int) -> str:\n        \"\"\"Query operation\"\"\"\n        # Search for bucket index corresponding to key\n        index = self.find_bucket(key)\n        # If key-value pair is found, return corresponding val\n        if self.buckets[index] not in [None, self.TOMBSTONE]:\n            return self.buckets[index].val\n        # If key-value pair does not exist, return None\n        return None\n\n    def put(self, key: int, val: str):\n        \"\"\"Add operation\"\"\"\n        # When load factor exceeds threshold, perform expansion\n        if self.load_factor() > self.load_thres:\n            self.extend()\n        # Search for bucket index corresponding to key\n        index = self.find_bucket(key)\n        # If key-value pair is found, overwrite val and return\n        if self.buckets[index] not in [None, self.TOMBSTONE]:\n            self.buckets[index].val = val\n            return\n        # If key-value pair does not exist, add the key-value pair\n        self.buckets[index] = Pair(key, val)\n        self.size += 1\n\n    def remove(self, key: int):\n        \"\"\"Remove operation\"\"\"\n        # Search for bucket index corresponding to key\n        index = self.find_bucket(key)\n        # If key-value pair is found, overwrite it with removal marker\n        if self.buckets[index] not in [None, self.TOMBSTONE]:\n            self.buckets[index] = self.TOMBSTONE\n            self.size -= 1\n\n    def extend(self):\n        \"\"\"Expand hash table\"\"\"\n        # Temporarily store the original hash table\n        buckets_tmp = self.buckets\n        # Initialize expanded new hash table\n        self.capacity *= self.extend_ratio\n        self.buckets = [None] * self.capacity\n        self.size = 0\n        # Move key-value pairs from original hash table to new hash table\n        for pair in buckets_tmp:\n            if pair not in [None, self.TOMBSTONE]:\n                self.put(pair.key, pair.val)\n\n    def print(self):\n        \"\"\"Print hash table\"\"\"\n        for pair in self.buckets:\n            if pair is None:\n                print(\"None\")\n            elif pair is self.TOMBSTONE:\n                print(\"TOMBSTONE\")\n            else:\n                print(pair.key, \"->\", pair.val)\n
      hash_map_open_addressing.cpp
      /* Hash table with open addressing */\nclass HashMapOpenAddressing {\n  private:\n    int size;                             // Number of key-value pairs\n    int capacity = 4;                     // Hash table capacity\n    const double loadThres = 2.0 / 3.0;     // Load factor threshold for triggering expansion\n    const int extendRatio = 2;            // Expansion multiplier\n    vector<Pair *> buckets;               // Bucket array\n    Pair *TOMBSTONE = new Pair(-1, \"-1\"); // Removal marker\n\n  public:\n    /* Constructor */\n    HashMapOpenAddressing() : size(0), buckets(capacity, nullptr) {\n    }\n\n    /* Destructor */\n    ~HashMapOpenAddressing() {\n        for (Pair *pair : buckets) {\n            if (pair != nullptr && pair != TOMBSTONE) {\n                delete pair;\n            }\n        }\n        delete TOMBSTONE;\n    }\n\n    /* Hash function */\n    int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* Load factor */\n    double loadFactor() {\n        return (double)size / capacity;\n    }\n\n    /* Search for bucket index corresponding to key */\n    int findBucket(int key) {\n        int index = hashFunc(key);\n        int firstTombstone = -1;\n        // Linear probing, break when encountering an empty bucket\n        while (buckets[index] != nullptr) {\n            // If key is encountered, return the corresponding bucket index\n            if (buckets[index]->key == key) {\n                // If a removal marker was encountered before, move the key-value pair to that index\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index];\n                    buckets[index] = TOMBSTONE;\n                    return firstTombstone; // Return the moved bucket index\n                }\n                return index; // Return bucket index\n            }\n            // Record the first removal marker encountered\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index;\n            }\n            // Calculate bucket index, wrap around to the head if past the tail\n            index = (index + 1) % capacity;\n        }\n        // If key does not exist, return the index for insertion\n        return firstTombstone == -1 ? index : firstTombstone;\n    }\n\n    /* Query operation */\n    string get(int key) {\n        // Search for bucket index corresponding to key\n        int index = findBucket(key);\n        // If key-value pair is found, return corresponding val\n        if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n            return buckets[index]->val;\n        }\n        // Return empty string if key-value pair does not exist\n        return \"\";\n    }\n\n    /* Add operation */\n    void put(int key, string val) {\n        // When load factor exceeds threshold, perform expansion\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        // Search for bucket index corresponding to key\n        int index = findBucket(key);\n        // If key-value pair is found, overwrite val and return\n        if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n            buckets[index]->val = val;\n            return;\n        }\n        // If key-value pair does not exist, add the key-value pair\n        buckets[index] = new Pair(key, val);\n        size++;\n    }\n\n    /* Remove operation */\n    void remove(int key) {\n        // Search for bucket index corresponding to key\n        int index = findBucket(key);\n        // If key-value pair is found, overwrite it with removal marker\n        if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n            delete buckets[index];\n            buckets[index] = TOMBSTONE;\n            size--;\n        }\n    }\n\n    /* Expand hash table */\n    void extend() {\n        // Temporarily store the original hash table\n        vector<Pair *> bucketsTmp = buckets;\n        // Initialize expanded new hash table\n        capacity *= extendRatio;\n        buckets = vector<Pair *>(capacity, nullptr);\n        size = 0;\n        // Move key-value pairs from original hash table to new hash table\n        for (Pair *pair : bucketsTmp) {\n            if (pair != nullptr && pair != TOMBSTONE) {\n                put(pair->key, pair->val);\n                delete pair;\n            }\n        }\n    }\n\n    /* Print hash table */\n    void print() {\n        for (Pair *pair : buckets) {\n            if (pair == nullptr) {\n                cout << \"nullptr\" << endl;\n            } else if (pair == TOMBSTONE) {\n                cout << \"TOMBSTONE\" << endl;\n            } else {\n                cout << pair->key << \" -> \" << pair->val << endl;\n            }\n        }\n    }\n};\n
      hash_map_open_addressing.java
      /* Hash table with open addressing */\nclass HashMapOpenAddressing {\n    private int size; // Number of key-value pairs\n    private int capacity = 4; // Hash table capacity\n    private final double loadThres = 2.0 / 3.0; // Load factor threshold for triggering expansion\n    private final int extendRatio = 2; // Expansion multiplier\n    private Pair[] buckets; // Bucket array\n    private final Pair TOMBSTONE = new Pair(-1, \"-1\"); // Removal marker\n\n    /* Constructor */\n    public HashMapOpenAddressing() {\n        size = 0;\n        buckets = new Pair[capacity];\n    }\n\n    /* Hash function */\n    private int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* Load factor */\n    private double loadFactor() {\n        return (double) size / capacity;\n    }\n\n    /* Search for bucket index corresponding to key */\n    private int findBucket(int key) {\n        int index = hashFunc(key);\n        int firstTombstone = -1;\n        // Linear probing, break when encountering an empty bucket\n        while (buckets[index] != null) {\n            // If key is encountered, return the corresponding bucket index\n            if (buckets[index].key == key) {\n                // If a removal marker was encountered before, move the key-value pair to that index\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index];\n                    buckets[index] = TOMBSTONE;\n                    return firstTombstone; // Return the moved bucket index\n                }\n                return index; // Return bucket index\n            }\n            // Record the first removal marker encountered\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index;\n            }\n            // Calculate bucket index, wrap around to the head if past the tail\n            index = (index + 1) % capacity;\n        }\n        // If key does not exist, return the index for insertion\n        return firstTombstone == -1 ? index : firstTombstone;\n    }\n\n    /* Query operation */\n    public String get(int key) {\n        // Search for bucket index corresponding to key\n        int index = findBucket(key);\n        // If key-value pair is found, return corresponding val\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            return buckets[index].val;\n        }\n        // If key-value pair does not exist, return null\n        return null;\n    }\n\n    /* Add operation */\n    public void put(int key, String val) {\n        // When load factor exceeds threshold, perform expansion\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        // Search for bucket index corresponding to key\n        int index = findBucket(key);\n        // If key-value pair is found, overwrite val and return\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index].val = val;\n            return;\n        }\n        // If key-value pair does not exist, add the key-value pair\n        buckets[index] = new Pair(key, val);\n        size++;\n    }\n\n    /* Remove operation */\n    public void remove(int key) {\n        // Search for bucket index corresponding to key\n        int index = findBucket(key);\n        // If key-value pair is found, overwrite it with removal marker\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index] = TOMBSTONE;\n            size--;\n        }\n    }\n\n    /* Expand hash table */\n    private void extend() {\n        // Temporarily store the original hash table\n        Pair[] bucketsTmp = buckets;\n        // Initialize expanded new hash table\n        capacity *= extendRatio;\n        buckets = new Pair[capacity];\n        size = 0;\n        // Move key-value pairs from original hash table to new hash table\n        for (Pair pair : bucketsTmp) {\n            if (pair != null && pair != TOMBSTONE) {\n                put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* Print hash table */\n    public void print() {\n        for (Pair pair : buckets) {\n            if (pair == null) {\n                System.out.println(\"null\");\n            } else if (pair == TOMBSTONE) {\n                System.out.println(\"TOMBSTONE\");\n            } else {\n                System.out.println(pair.key + \" -> \" + pair.val);\n            }\n        }\n    }\n}\n
      hash_map_open_addressing.cs
      /* Hash table with open addressing */\nclass HashMapOpenAddressing {\n    int size; // Number of key-value pairs\n    int capacity = 4; // Hash table capacity\n    double loadThres = 2.0 / 3.0; // Load factor threshold for triggering expansion\n    int extendRatio = 2; // Expansion multiplier\n    Pair[] buckets; // Bucket array\n    Pair TOMBSTONE = new(-1, \"-1\"); // Removal marker\n\n    /* Constructor */\n    public HashMapOpenAddressing() {\n        size = 0;\n        buckets = new Pair[capacity];\n    }\n\n    /* Hash function */\n    int HashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* Load factor */\n    double LoadFactor() {\n        return (double)size / capacity;\n    }\n\n    /* Search for bucket index corresponding to key */\n    int FindBucket(int key) {\n        int index = HashFunc(key);\n        int firstTombstone = -1;\n        // Linear probing, break when encountering an empty bucket\n        while (buckets[index] != null) {\n            // If key is encountered, return the corresponding bucket index\n            if (buckets[index].key == key) {\n                // If a removal marker was encountered before, move the key-value pair to that index\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index];\n                    buckets[index] = TOMBSTONE;\n                    return firstTombstone; // Return the moved bucket index\n                }\n                return index; // Return bucket index\n            }\n            // Record the first removal marker encountered\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index;\n            }\n            // Calculate bucket index, wrap around to the head if past the tail\n            index = (index + 1) % capacity;\n        }\n        // If key does not exist, return the index for insertion\n        return firstTombstone == -1 ? index : firstTombstone;\n    }\n\n    /* Query operation */\n    public string? Get(int key) {\n        // Search for bucket index corresponding to key\n        int index = FindBucket(key);\n        // If key-value pair is found, return corresponding val\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            return buckets[index].val;\n        }\n        // If key-value pair does not exist, return null\n        return null;\n    }\n\n    /* Add operation */\n    public void Put(int key, string val) {\n        // When load factor exceeds threshold, perform expansion\n        if (LoadFactor() > loadThres) {\n            Extend();\n        }\n        // Search for bucket index corresponding to key\n        int index = FindBucket(key);\n        // If key-value pair is found, overwrite val and return\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index].val = val;\n            return;\n        }\n        // If key-value pair does not exist, add the key-value pair\n        buckets[index] = new Pair(key, val);\n        size++;\n    }\n\n    /* Remove operation */\n    public void Remove(int key) {\n        // Search for bucket index corresponding to key\n        int index = FindBucket(key);\n        // If key-value pair is found, overwrite it with removal marker\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index] = TOMBSTONE;\n            size--;\n        }\n    }\n\n    /* Expand hash table */\n    void Extend() {\n        // Temporarily store the original hash table\n        Pair[] bucketsTmp = buckets;\n        // Initialize expanded new hash table\n        capacity *= extendRatio;\n        buckets = new Pair[capacity];\n        size = 0;\n        // Move key-value pairs from original hash table to new hash table\n        foreach (Pair pair in bucketsTmp) {\n            if (pair != null && pair != TOMBSTONE) {\n                Put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* Print hash table */\n    public void Print() {\n        foreach (Pair pair in buckets) {\n            if (pair == null) {\n                Console.WriteLine(\"null\");\n            } else if (pair == TOMBSTONE) {\n                Console.WriteLine(\"TOMBSTONE\");\n            } else {\n                Console.WriteLine(pair.key + \" -> \" + pair.val);\n            }\n        }\n    }\n}\n
      hash_map_open_addressing.go
      /* Hash table with open addressing */\ntype hashMapOpenAddressing struct {\n    size        int     // Number of key-value pairs\n    capacity    int     // Hash table capacity\n    loadThres   float64 // Load factor threshold for triggering expansion\n    extendRatio int     // Expansion multiplier\n    buckets     []*pair // Bucket array\n    TOMBSTONE   *pair   // Removal marker\n}\n\n/* Constructor */\nfunc newHashMapOpenAddressing() *hashMapOpenAddressing {\n    return &hashMapOpenAddressing{\n        size:        0,\n        capacity:    4,\n        loadThres:   2.0 / 3.0,\n        extendRatio: 2,\n        buckets:     make([]*pair, 4),\n        TOMBSTONE:   &pair{-1, \"-1\"},\n    }\n}\n\n/* Hash function */\nfunc (h *hashMapOpenAddressing) hashFunc(key int) int {\n    return key % h.capacity // Calculate hash value based on key\n}\n\n/* Load factor */\nfunc (h *hashMapOpenAddressing) loadFactor() float64 {\n    return float64(h.size) / float64(h.capacity) // Calculate current load factor\n}\n\n/* Search for bucket index corresponding to key */\nfunc (h *hashMapOpenAddressing) findBucket(key int) int {\n    index := h.hashFunc(key) // Get initial index\n    firstTombstone := -1     // Record position of first TOMBSTONE encountered\n    for h.buckets[index] != nil {\n        if h.buckets[index].key == key {\n            if firstTombstone != -1 {\n                // If a removal marker was encountered before, move the key-value pair to that index\n                h.buckets[firstTombstone] = h.buckets[index]\n                h.buckets[index] = h.TOMBSTONE\n                return firstTombstone // Return the moved bucket index\n            }\n            return index // Return found index\n        }\n        if firstTombstone == -1 && h.buckets[index] == h.TOMBSTONE {\n            firstTombstone = index // Record position of first deletion marker encountered\n        }\n        index = (index + 1) % h.capacity // Linear probing, wrap around to head if past tail\n    }\n    // If key does not exist, return the index for insertion\n    if firstTombstone != -1 {\n        return firstTombstone\n    }\n    return index\n}\n\n/* Query operation */\nfunc (h *hashMapOpenAddressing) get(key int) string {\n    index := h.findBucket(key) // Search for bucket index corresponding to key\n    if h.buckets[index] != nil && h.buckets[index] != h.TOMBSTONE {\n        return h.buckets[index].val // If key-value pair is found, return corresponding val\n    }\n    return \"\" // Return \"\" if key-value pair does not exist\n}\n\n/* Add operation */\nfunc (h *hashMapOpenAddressing) put(key int, val string) {\n    if h.loadFactor() > h.loadThres {\n        h.extend() // When load factor exceeds threshold, perform expansion\n    }\n    index := h.findBucket(key) // Search for bucket index corresponding to key\n    if h.buckets[index] == nil || h.buckets[index] == h.TOMBSTONE {\n        h.buckets[index] = &pair{key, val} // If key-value pair does not exist, add the key-value pair\n        h.size++\n    } else {\n        h.buckets[index].val = val // If key-value pair found, overwrite val\n    }\n}\n\n/* Remove operation */\nfunc (h *hashMapOpenAddressing) remove(key int) {\n    index := h.findBucket(key) // Search for bucket index corresponding to key\n    if h.buckets[index] != nil && h.buckets[index] != h.TOMBSTONE {\n        h.buckets[index] = h.TOMBSTONE // If key-value pair is found, overwrite it with removal marker\n        h.size--\n    }\n}\n\n/* Expand hash table */\nfunc (h *hashMapOpenAddressing) extend() {\n    oldBuckets := h.buckets               // Temporarily store the original hash table\n    h.capacity *= h.extendRatio           // Update capacity\n    h.buckets = make([]*pair, h.capacity) // Initialize expanded new hash table\n    h.size = 0                            // Reset size\n    // Move key-value pairs from original hash table to new hash table\n    for _, pair := range oldBuckets {\n        if pair != nil && pair != h.TOMBSTONE {\n            h.put(pair.key, pair.val)\n        }\n    }\n}\n\n/* Print hash table */\nfunc (h *hashMapOpenAddressing) print() {\n    for _, pair := range h.buckets {\n        if pair == nil {\n            fmt.Println(\"nil\")\n        } else if pair == h.TOMBSTONE {\n            fmt.Println(\"TOMBSTONE\")\n        } else {\n            fmt.Printf(\"%d -> %s\\n\", pair.key, pair.val)\n        }\n    }\n}\n
      hash_map_open_addressing.swift
      /* Hash table with open addressing */\nclass HashMapOpenAddressing {\n    var size: Int // Number of key-value pairs\n    var capacity: Int // Hash table capacity\n    var loadThres: Double // Load factor threshold for triggering expansion\n    var extendRatio: Int // Expansion multiplier\n    var buckets: [Pair?] // Bucket array\n    var TOMBSTONE: Pair // Removal marker\n\n    /* Constructor */\n    init() {\n        size = 0\n        capacity = 4\n        loadThres = 2.0 / 3.0\n        extendRatio = 2\n        buckets = Array(repeating: nil, count: capacity)\n        TOMBSTONE = Pair(key: -1, val: \"-1\")\n    }\n\n    /* Hash function */\n    func hashFunc(key: Int) -> Int {\n        key % capacity\n    }\n\n    /* Load factor */\n    func loadFactor() -> Double {\n        Double(size) / Double(capacity)\n    }\n\n    /* Search for bucket index corresponding to key */\n    func findBucket(key: Int) -> Int {\n        var index = hashFunc(key: key)\n        var firstTombstone = -1\n        // Linear probing, break when encountering an empty bucket\n        while buckets[index] != nil {\n            // If key is encountered, return the corresponding bucket index\n            if buckets[index]!.key == key {\n                // If a removal marker was encountered before, move the key-value pair to that index\n                if firstTombstone != -1 {\n                    buckets[firstTombstone] = buckets[index]\n                    buckets[index] = TOMBSTONE\n                    return firstTombstone // Return the moved bucket index\n                }\n                return index // Return bucket index\n            }\n            // Record the first removal marker encountered\n            if firstTombstone == -1 && buckets[index] == TOMBSTONE {\n                firstTombstone = index\n            }\n            // Calculate bucket index, wrap around to the head if past the tail\n            index = (index + 1) % capacity\n        }\n        // If key does not exist, return the index for insertion\n        return firstTombstone == -1 ? index : firstTombstone\n    }\n\n    /* Query operation */\n    func get(key: Int) -> String? {\n        // Search for bucket index corresponding to key\n        let index = findBucket(key: key)\n        // If key-value pair is found, return corresponding val\n        if buckets[index] != nil, buckets[index] != TOMBSTONE {\n            return buckets[index]!.val\n        }\n        // If key-value pair does not exist, return null\n        return nil\n    }\n\n    /* Add operation */\n    func put(key: Int, val: String) {\n        // When load factor exceeds threshold, perform expansion\n        if loadFactor() > loadThres {\n            extend()\n        }\n        // Search for bucket index corresponding to key\n        let index = findBucket(key: key)\n        // If key-value pair is found, overwrite val and return\n        if buckets[index] != nil, buckets[index] != TOMBSTONE {\n            buckets[index]!.val = val\n            return\n        }\n        // If key-value pair does not exist, add the key-value pair\n        buckets[index] = Pair(key: key, val: val)\n        size += 1\n    }\n\n    /* Remove operation */\n    func remove(key: Int) {\n        // Search for bucket index corresponding to key\n        let index = findBucket(key: key)\n        // If key-value pair is found, overwrite it with removal marker\n        if buckets[index] != nil, buckets[index] != TOMBSTONE {\n            buckets[index] = TOMBSTONE\n            size -= 1\n        }\n    }\n\n    /* Expand hash table */\n    func extend() {\n        // Temporarily store the original hash table\n        let bucketsTmp = buckets\n        // Initialize expanded new hash table\n        capacity *= extendRatio\n        buckets = Array(repeating: nil, count: capacity)\n        size = 0\n        // Move key-value pairs from original hash table to new hash table\n        for pair in bucketsTmp {\n            if let pair, pair != TOMBSTONE {\n                put(key: pair.key, val: pair.val)\n            }\n        }\n    }\n\n    /* Print hash table */\n    func print() {\n        for pair in buckets {\n            if pair == nil {\n                Swift.print(\"null\")\n            } else if pair == TOMBSTONE {\n                Swift.print(\"TOMBSTONE\")\n            } else {\n                Swift.print(\"\\(pair!.key) -> \\(pair!.val)\")\n            }\n        }\n    }\n}\n
      hash_map_open_addressing.js
      /* Hash table with open addressing */\nclass HashMapOpenAddressing {\n    #size; // Number of key-value pairs\n    #capacity; // Hash table capacity\n    #loadThres; // Load factor threshold for triggering expansion\n    #extendRatio; // Expansion multiplier\n    #buckets; // Bucket array\n    #TOMBSTONE; // Removal marker\n\n    /* Constructor */\n    constructor() {\n        this.#size = 0; // Number of key-value pairs\n        this.#capacity = 4; // Hash table capacity\n        this.#loadThres = 2.0 / 3.0; // Load factor threshold for triggering expansion\n        this.#extendRatio = 2; // Expansion multiplier\n        this.#buckets = Array(this.#capacity).fill(null); // Bucket array\n        this.#TOMBSTONE = new Pair(-1, '-1'); // Removal marker\n    }\n\n    /* Hash function */\n    #hashFunc(key) {\n        return key % this.#capacity;\n    }\n\n    /* Load factor */\n    #loadFactor() {\n        return this.#size / this.#capacity;\n    }\n\n    /* Search for bucket index corresponding to key */\n    #findBucket(key) {\n        let index = this.#hashFunc(key);\n        let firstTombstone = -1;\n        // Linear probing, break when encountering an empty bucket\n        while (this.#buckets[index] !== null) {\n            // If key is encountered, return the corresponding bucket index\n            if (this.#buckets[index].key === key) {\n                // If a removal marker was encountered before, move the key-value pair to that index\n                if (firstTombstone !== -1) {\n                    this.#buckets[firstTombstone] = this.#buckets[index];\n                    this.#buckets[index] = this.#TOMBSTONE;\n                    return firstTombstone; // Return the moved bucket index\n                }\n                return index; // Return bucket index\n            }\n            // Record the first removal marker encountered\n            if (\n                firstTombstone === -1 &&\n                this.#buckets[index] === this.#TOMBSTONE\n            ) {\n                firstTombstone = index;\n            }\n            // Calculate bucket index, wrap around to the head if past the tail\n            index = (index + 1) % this.#capacity;\n        }\n        // If key does not exist, return the index for insertion\n        return firstTombstone === -1 ? index : firstTombstone;\n    }\n\n    /* Query operation */\n    get(key) {\n        // Search for bucket index corresponding to key\n        const index = this.#findBucket(key);\n        // If key-value pair is found, return corresponding val\n        if (\n            this.#buckets[index] !== null &&\n            this.#buckets[index] !== this.#TOMBSTONE\n        ) {\n            return this.#buckets[index].val;\n        }\n        // If key-value pair does not exist, return null\n        return null;\n    }\n\n    /* Add operation */\n    put(key, val) {\n        // When load factor exceeds threshold, perform expansion\n        if (this.#loadFactor() > this.#loadThres) {\n            this.#extend();\n        }\n        // Search for bucket index corresponding to key\n        const index = this.#findBucket(key);\n        // If key-value pair is found, overwrite val and return\n        if (\n            this.#buckets[index] !== null &&\n            this.#buckets[index] !== this.#TOMBSTONE\n        ) {\n            this.#buckets[index].val = val;\n            return;\n        }\n        // If key-value pair does not exist, add the key-value pair\n        this.#buckets[index] = new Pair(key, val);\n        this.#size++;\n    }\n\n    /* Remove operation */\n    remove(key) {\n        // Search for bucket index corresponding to key\n        const index = this.#findBucket(key);\n        // If key-value pair is found, overwrite it with removal marker\n        if (\n            this.#buckets[index] !== null &&\n            this.#buckets[index] !== this.#TOMBSTONE\n        ) {\n            this.#buckets[index] = this.#TOMBSTONE;\n            this.#size--;\n        }\n    }\n\n    /* Expand hash table */\n    #extend() {\n        // Temporarily store the original hash table\n        const bucketsTmp = this.#buckets;\n        // Initialize expanded new hash table\n        this.#capacity *= this.#extendRatio;\n        this.#buckets = Array(this.#capacity).fill(null);\n        this.#size = 0;\n        // Move key-value pairs from original hash table to new hash table\n        for (const pair of bucketsTmp) {\n            if (pair !== null && pair !== this.#TOMBSTONE) {\n                this.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* Print hash table */\n    print() {\n        for (const pair of this.#buckets) {\n            if (pair === null) {\n                console.log('null');\n            } else if (pair === this.#TOMBSTONE) {\n                console.log('TOMBSTONE');\n            } else {\n                console.log(pair.key + ' -> ' + pair.val);\n            }\n        }\n    }\n}\n
      hash_map_open_addressing.ts
      /* Hash table with open addressing */\nclass HashMapOpenAddressing {\n    private size: number; // Number of key-value pairs\n    private capacity: number; // Hash table capacity\n    private loadThres: number; // Load factor threshold for triggering expansion\n    private extendRatio: number; // Expansion multiplier\n    private buckets: Array<Pair | null>; // Bucket array\n    private TOMBSTONE: Pair; // Removal marker\n\n    /* Constructor */\n    constructor() {\n        this.size = 0; // Number of key-value pairs\n        this.capacity = 4; // Hash table capacity\n        this.loadThres = 2.0 / 3.0; // Load factor threshold for triggering expansion\n        this.extendRatio = 2; // Expansion multiplier\n        this.buckets = Array(this.capacity).fill(null); // Bucket array\n        this.TOMBSTONE = new Pair(-1, '-1'); // Removal marker\n    }\n\n    /* Hash function */\n    private hashFunc(key: number): number {\n        return key % this.capacity;\n    }\n\n    /* Load factor */\n    private loadFactor(): number {\n        return this.size / this.capacity;\n    }\n\n    /* Search for bucket index corresponding to key */\n    private findBucket(key: number): number {\n        let index = this.hashFunc(key);\n        let firstTombstone = -1;\n        // Linear probing, break when encountering an empty bucket\n        while (this.buckets[index] !== null) {\n            // If key is encountered, return the corresponding bucket index\n            if (this.buckets[index]!.key === key) {\n                // If a removal marker was encountered before, move the key-value pair to that index\n                if (firstTombstone !== -1) {\n                    this.buckets[firstTombstone] = this.buckets[index];\n                    this.buckets[index] = this.TOMBSTONE;\n                    return firstTombstone; // Return the moved bucket index\n                }\n                return index; // Return bucket index\n            }\n            // Record the first removal marker encountered\n            if (\n                firstTombstone === -1 &&\n                this.buckets[index] === this.TOMBSTONE\n            ) {\n                firstTombstone = index;\n            }\n            // Calculate bucket index, wrap around to the head if past the tail\n            index = (index + 1) % this.capacity;\n        }\n        // If key does not exist, return the index for insertion\n        return firstTombstone === -1 ? index : firstTombstone;\n    }\n\n    /* Query operation */\n    get(key: number): string | null {\n        // Search for bucket index corresponding to key\n        const index = this.findBucket(key);\n        // If key-value pair is found, return corresponding val\n        if (\n            this.buckets[index] !== null &&\n            this.buckets[index] !== this.TOMBSTONE\n        ) {\n            return this.buckets[index]!.val;\n        }\n        // If key-value pair does not exist, return null\n        return null;\n    }\n\n    /* Add operation */\n    put(key: number, val: string): void {\n        // When load factor exceeds threshold, perform expansion\n        if (this.loadFactor() > this.loadThres) {\n            this.extend();\n        }\n        // Search for bucket index corresponding to key\n        const index = this.findBucket(key);\n        // If key-value pair is found, overwrite val and return\n        if (\n            this.buckets[index] !== null &&\n            this.buckets[index] !== this.TOMBSTONE\n        ) {\n            this.buckets[index]!.val = val;\n            return;\n        }\n        // If key-value pair does not exist, add the key-value pair\n        this.buckets[index] = new Pair(key, val);\n        this.size++;\n    }\n\n    /* Remove operation */\n    remove(key: number): void {\n        // Search for bucket index corresponding to key\n        const index = this.findBucket(key);\n        // If key-value pair is found, overwrite it with removal marker\n        if (\n            this.buckets[index] !== null &&\n            this.buckets[index] !== this.TOMBSTONE\n        ) {\n            this.buckets[index] = this.TOMBSTONE;\n            this.size--;\n        }\n    }\n\n    /* Expand hash table */\n    private extend(): void {\n        // Temporarily store the original hash table\n        const bucketsTmp = this.buckets;\n        // Initialize expanded new hash table\n        this.capacity *= this.extendRatio;\n        this.buckets = Array(this.capacity).fill(null);\n        this.size = 0;\n        // Move key-value pairs from original hash table to new hash table\n        for (const pair of bucketsTmp) {\n            if (pair !== null && pair !== this.TOMBSTONE) {\n                this.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* Print hash table */\n    print(): void {\n        for (const pair of this.buckets) {\n            if (pair === null) {\n                console.log('null');\n            } else if (pair === this.TOMBSTONE) {\n                console.log('TOMBSTONE');\n            } else {\n                console.log(pair.key + ' -> ' + pair.val);\n            }\n        }\n    }\n}\n
      hash_map_open_addressing.dart
      /* Hash table with open addressing */\nclass HashMapOpenAddressing {\n  late int _size; // Number of key-value pairs\n  int _capacity = 4; // Hash table capacity\n  double _loadThres = 2.0 / 3.0; // Load factor threshold for triggering expansion\n  int _extendRatio = 2; // Expansion multiplier\n  late List<Pair?> _buckets; // Bucket array\n  Pair _TOMBSTONE = Pair(-1, \"-1\"); // Removal marker\n\n  /* Constructor */\n  HashMapOpenAddressing() {\n    _size = 0;\n    _buckets = List.generate(_capacity, (index) => null);\n  }\n\n  /* Hash function */\n  int hashFunc(int key) {\n    return key % _capacity;\n  }\n\n  /* Load factor */\n  double loadFactor() {\n    return _size / _capacity;\n  }\n\n  /* Search for bucket index corresponding to key */\n  int findBucket(int key) {\n    int index = hashFunc(key);\n    int firstTombstone = -1;\n    // Linear probing, break when encountering an empty bucket\n    while (_buckets[index] != null) {\n      // If key is encountered, return the corresponding bucket index\n      if (_buckets[index]!.key == key) {\n        // If a removal marker was encountered before, move the key-value pair to that index\n        if (firstTombstone != -1) {\n          _buckets[firstTombstone] = _buckets[index];\n          _buckets[index] = _TOMBSTONE;\n          return firstTombstone; // Return the moved bucket index\n        }\n        return index; // Return bucket index\n      }\n      // Record the first removal marker encountered\n      if (firstTombstone == -1 && _buckets[index] == _TOMBSTONE) {\n        firstTombstone = index;\n      }\n      // Calculate bucket index, wrap around to the head if past the tail\n      index = (index + 1) % _capacity;\n    }\n    // If key does not exist, return the index for insertion\n    return firstTombstone == -1 ? index : firstTombstone;\n  }\n\n  /* Query operation */\n  String? get(int key) {\n    // Search for bucket index corresponding to key\n    int index = findBucket(key);\n    // If key-value pair is found, return corresponding val\n    if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n      return _buckets[index]!.val;\n    }\n    // If key-value pair does not exist, return null\n    return null;\n  }\n\n  /* Add operation */\n  void put(int key, String val) {\n    // When load factor exceeds threshold, perform expansion\n    if (loadFactor() > _loadThres) {\n      extend();\n    }\n    // Search for bucket index corresponding to key\n    int index = findBucket(key);\n    // If key-value pair is found, overwrite val and return\n    if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n      _buckets[index]!.val = val;\n      return;\n    }\n    // If key-value pair does not exist, add the key-value pair\n    _buckets[index] = new Pair(key, val);\n    _size++;\n  }\n\n  /* Remove operation */\n  void remove(int key) {\n    // Search for bucket index corresponding to key\n    int index = findBucket(key);\n    // If key-value pair is found, overwrite it with removal marker\n    if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n      _buckets[index] = _TOMBSTONE;\n      _size--;\n    }\n  }\n\n  /* Expand hash table */\n  void extend() {\n    // Temporarily store the original hash table\n    List<Pair?> bucketsTmp = _buckets;\n    // Initialize expanded new hash table\n    _capacity *= _extendRatio;\n    _buckets = List.generate(_capacity, (index) => null);\n    _size = 0;\n    // Move key-value pairs from original hash table to new hash table\n    for (Pair? pair in bucketsTmp) {\n      if (pair != null && pair != _TOMBSTONE) {\n        put(pair.key, pair.val);\n      }\n    }\n  }\n\n  /* Print hash table */\n  void printHashMap() {\n    for (Pair? pair in _buckets) {\n      if (pair == null) {\n        print(\"null\");\n      } else if (pair == _TOMBSTONE) {\n        print(\"TOMBSTONE\");\n      } else {\n        print(\"${pair.key} -> ${pair.val}\");\n      }\n    }\n  }\n}\n
      hash_map_open_addressing.rs
      /* Hash table with open addressing */\nstruct HashMapOpenAddressing {\n    size: usize,                // Number of key-value pairs\n    capacity: usize,            // Hash table capacity\n    load_thres: f64,            // Load factor threshold for triggering expansion\n    extend_ratio: usize,        // Expansion multiplier\n    buckets: Vec<Option<Pair>>, // Bucket array\n    TOMBSTONE: Option<Pair>,    // Removal marker\n}\n\nimpl HashMapOpenAddressing {\n    /* Constructor */\n    fn new() -> Self {\n        Self {\n            size: 0,\n            capacity: 4,\n            load_thres: 2.0 / 3.0,\n            extend_ratio: 2,\n            buckets: vec![None; 4],\n            TOMBSTONE: Some(Pair {\n                key: -1,\n                val: \"-1\".to_string(),\n            }),\n        }\n    }\n\n    /* Hash function */\n    fn hash_func(&self, key: i32) -> usize {\n        (key % self.capacity as i32) as usize\n    }\n\n    /* Load factor */\n    fn load_factor(&self) -> f64 {\n        self.size as f64 / self.capacity as f64\n    }\n\n    /* Search for bucket index corresponding to key */\n    fn find_bucket(&mut self, key: i32) -> usize {\n        let mut index = self.hash_func(key);\n        let mut first_tombstone = -1;\n        // Linear probing, break when encountering an empty bucket\n        while self.buckets[index].is_some() {\n            // If key is found, return corresponding bucket index\n            if self.buckets[index].as_ref().unwrap().key == key {\n                // If deletion marker was encountered before, move key-value pair to that index\n                if first_tombstone != -1 {\n                    self.buckets[first_tombstone as usize] = self.buckets[index].take();\n                    self.buckets[index] = self.TOMBSTONE.clone();\n                    return first_tombstone as usize; // Return the moved bucket index\n                }\n                return index; // Return bucket index\n            }\n            // Record the first removal marker encountered\n            if first_tombstone == -1 && self.buckets[index] == self.TOMBSTONE {\n                first_tombstone = index as i32;\n            }\n            // Calculate bucket index, wrap around to the head if past the tail\n            index = (index + 1) % self.capacity;\n        }\n        // If key does not exist, return the index for insertion\n        if first_tombstone == -1 {\n            index\n        } else {\n            first_tombstone as usize\n        }\n    }\n\n    /* Query operation */\n    fn get(&mut self, key: i32) -> Option<&str> {\n        // Search for bucket index corresponding to key\n        let index = self.find_bucket(key);\n        // If key-value pair is found, return corresponding val\n        if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n            return self.buckets[index].as_ref().map(|pair| &pair.val as &str);\n        }\n        // If key-value pair does not exist, return null\n        None\n    }\n\n    /* Add operation */\n    fn put(&mut self, key: i32, val: String) {\n        // When load factor exceeds threshold, perform expansion\n        if self.load_factor() > self.load_thres {\n            self.extend();\n        }\n        // Search for bucket index corresponding to key\n        let index = self.find_bucket(key);\n        // If key-value pair is found, overwrite val and return\n        if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n            self.buckets[index].as_mut().unwrap().val = val;\n            return;\n        }\n        // If key-value pair does not exist, add the key-value pair\n        self.buckets[index] = Some(Pair { key, val });\n        self.size += 1;\n    }\n\n    /* Remove operation */\n    fn remove(&mut self, key: i32) {\n        // Search for bucket index corresponding to key\n        let index = self.find_bucket(key);\n        // If key-value pair is found, overwrite it with removal marker\n        if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n            self.buckets[index] = self.TOMBSTONE.clone();\n            self.size -= 1;\n        }\n    }\n\n    /* Expand hash table */\n    fn extend(&mut self) {\n        // Temporarily store the original hash table\n        let buckets_tmp = self.buckets.clone();\n        // Initialize expanded new hash table\n        self.capacity *= self.extend_ratio;\n        self.buckets = vec![None; self.capacity];\n        self.size = 0;\n\n        // Move key-value pairs from original hash table to new hash table\n        for pair in buckets_tmp {\n            if pair.is_none() || pair == self.TOMBSTONE {\n                continue;\n            }\n            let pair = pair.unwrap();\n\n            self.put(pair.key, pair.val);\n        }\n    }\n    /* Print hash table */\n    fn print(&self) {\n        for pair in &self.buckets {\n            if pair.is_none() {\n                println!(\"null\");\n            } else if pair == &self.TOMBSTONE {\n                println!(\"TOMBSTONE\");\n            } else {\n                let pair = pair.as_ref().unwrap();\n                println!(\"{} -> {}\", pair.key, pair.val);\n            }\n        }\n    }\n}\n
      hash_map_open_addressing.c
      /* Hash table with open addressing */\ntypedef struct {\n    int size;         // Number of key-value pairs\n    int capacity;     // Hash table capacity\n    double loadThres; // Load factor threshold for triggering expansion\n    int extendRatio;  // Expansion multiplier\n    Pair **buckets;   // Bucket array\n    Pair *TOMBSTONE;  // Removal marker\n} HashMapOpenAddressing;\n\n/* Constructor */\nHashMapOpenAddressing *newHashMapOpenAddressing() {\n    HashMapOpenAddressing *hashMap = (HashMapOpenAddressing *)malloc(sizeof(HashMapOpenAddressing));\n    hashMap->size = 0;\n    hashMap->capacity = 4;\n    hashMap->loadThres = 2.0 / 3.0;\n    hashMap->extendRatio = 2;\n    hashMap->buckets = (Pair **)calloc(hashMap->capacity, sizeof(Pair *));\n    hashMap->TOMBSTONE = (Pair *)malloc(sizeof(Pair));\n    hashMap->TOMBSTONE->key = -1;\n    hashMap->TOMBSTONE->val = \"-1\";\n\n    return hashMap;\n}\n\n/* Destructor */\nvoid delHashMapOpenAddressing(HashMapOpenAddressing *hashMap) {\n    for (int i = 0; i < hashMap->capacity; i++) {\n        Pair *pair = hashMap->buckets[i];\n        if (pair != NULL && pair != hashMap->TOMBSTONE) {\n            free(pair->val);\n            free(pair);\n        }\n    }\n    free(hashMap->buckets);\n    free(hashMap->TOMBSTONE);\n    free(hashMap);\n}\n\n/* Hash function */\nint hashFunc(HashMapOpenAddressing *hashMap, int key) {\n    return key % hashMap->capacity;\n}\n\n/* Load factor */\ndouble loadFactor(HashMapOpenAddressing *hashMap) {\n    return (double)hashMap->size / (double)hashMap->capacity;\n}\n\n/* Search for bucket index corresponding to key */\nint findBucket(HashMapOpenAddressing *hashMap, int key) {\n    int index = hashFunc(hashMap, key);\n    int firstTombstone = -1;\n    // Linear probing, break when encountering an empty bucket\n    while (hashMap->buckets[index] != NULL) {\n        // If key is encountered, return the corresponding bucket index\n        if (hashMap->buckets[index]->key == key) {\n            // If a removal marker was encountered before, move the key-value pair to that index\n            if (firstTombstone != -1) {\n                hashMap->buckets[firstTombstone] = hashMap->buckets[index];\n                hashMap->buckets[index] = hashMap->TOMBSTONE;\n                return firstTombstone; // Return the moved bucket index\n            }\n            return index; // Return bucket index\n        }\n        // Record the first removal marker encountered\n        if (firstTombstone == -1 && hashMap->buckets[index] == hashMap->TOMBSTONE) {\n            firstTombstone = index;\n        }\n        // Calculate bucket index, wrap around to the head if past the tail\n        index = (index + 1) % hashMap->capacity;\n    }\n    // If key does not exist, return the index for insertion\n    return firstTombstone == -1 ? index : firstTombstone;\n}\n\n/* Query operation */\nchar *get(HashMapOpenAddressing *hashMap, int key) {\n    // Search for bucket index corresponding to key\n    int index = findBucket(hashMap, key);\n    // If key-value pair is found, return corresponding val\n    if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n        return hashMap->buckets[index]->val;\n    }\n    // Return empty string if key-value pair does not exist\n    return \"\";\n}\n\n/* Add operation */\nvoid put(HashMapOpenAddressing *hashMap, int key, char *val) {\n    // When load factor exceeds threshold, perform expansion\n    if (loadFactor(hashMap) > hashMap->loadThres) {\n        extend(hashMap);\n    }\n    // Search for bucket index corresponding to key\n    int index = findBucket(hashMap, key);\n    // If key-value pair is found, overwrite val and return\n    if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n        free(hashMap->buckets[index]->val);\n        hashMap->buckets[index]->val = (char *)malloc(sizeof(strlen(val) + 1));\n        strcpy(hashMap->buckets[index]->val, val);\n        hashMap->buckets[index]->val[strlen(val)] = '\\0';\n        return;\n    }\n    // If key-value pair does not exist, add the key-value pair\n    Pair *pair = (Pair *)malloc(sizeof(Pair));\n    pair->key = key;\n    pair->val = (char *)malloc(sizeof(strlen(val) + 1));\n    strcpy(pair->val, val);\n    pair->val[strlen(val)] = '\\0';\n\n    hashMap->buckets[index] = pair;\n    hashMap->size++;\n}\n\n/* Remove operation */\nvoid removeItem(HashMapOpenAddressing *hashMap, int key) {\n    // Search for bucket index corresponding to key\n    int index = findBucket(hashMap, key);\n    // If key-value pair is found, overwrite it with removal marker\n    if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n        Pair *pair = hashMap->buckets[index];\n        free(pair->val);\n        free(pair);\n        hashMap->buckets[index] = hashMap->TOMBSTONE;\n        hashMap->size--;\n    }\n}\n\n/* Expand hash table */\nvoid extend(HashMapOpenAddressing *hashMap) {\n    // Temporarily store the original hash table\n    Pair **bucketsTmp = hashMap->buckets;\n    int oldCapacity = hashMap->capacity;\n    // Initialize expanded new hash table\n    hashMap->capacity *= hashMap->extendRatio;\n    hashMap->buckets = (Pair **)calloc(hashMap->capacity, sizeof(Pair *));\n    hashMap->size = 0;\n    // Move key-value pairs from original hash table to new hash table\n    for (int i = 0; i < oldCapacity; i++) {\n        Pair *pair = bucketsTmp[i];\n        if (pair != NULL && pair != hashMap->TOMBSTONE) {\n            put(hashMap, pair->key, pair->val);\n            free(pair->val);\n            free(pair);\n        }\n    }\n    free(bucketsTmp);\n}\n\n/* Print hash table */\nvoid print(HashMapOpenAddressing *hashMap) {\n    for (int i = 0; i < hashMap->capacity; i++) {\n        Pair *pair = hashMap->buckets[i];\n        if (pair == NULL) {\n            printf(\"NULL\\n\");\n        } else if (pair == hashMap->TOMBSTONE) {\n            printf(\"TOMBSTONE\\n\");\n        } else {\n            printf(\"%d -> %s\\n\", pair->key, pair->val);\n        }\n    }\n}\n
      hash_map_open_addressing.kt
      /* Hash table with open addressing */\nclass HashMapOpenAddressing {\n    private var size: Int               // Number of key-value pairs\n    private var capacity: Int           // Hash table capacity\n    private val loadThres: Double       // Load factor threshold for triggering expansion\n    private val extendRatio: Int        // Expansion multiplier\n    private var buckets: Array<Pair?>   // Bucket array\n    private val TOMBSTONE: Pair         // Removal marker\n\n    /* Constructor */\n    init {\n        size = 0\n        capacity = 4\n        loadThres = 2.0 / 3.0\n        extendRatio = 2\n        buckets = arrayOfNulls(capacity)\n        TOMBSTONE = Pair(-1, \"-1\")\n    }\n\n    /* Hash function */\n    fun hashFunc(key: Int): Int {\n        return key % capacity\n    }\n\n    /* Load factor */\n    fun loadFactor(): Double {\n        return (size / capacity).toDouble()\n    }\n\n    /* Search for bucket index corresponding to key */\n    fun findBucket(key: Int): Int {\n        var index = hashFunc(key)\n        var firstTombstone = -1\n        // Linear probing, break when encountering an empty bucket\n        while (buckets[index] != null) {\n            // If key is encountered, return the corresponding bucket index\n            if (buckets[index]?.key == key) {\n                // If a removal marker was encountered before, move the key-value pair to that index\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index]\n                    buckets[index] = TOMBSTONE\n                    return firstTombstone // Return the moved bucket index\n                }\n                return index // Return bucket index\n            }\n            // Record the first removal marker encountered\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index\n            }\n            // Calculate bucket index, wrap around to the head if past the tail\n            index = (index + 1) % capacity\n        }\n        // If key does not exist, return the index for insertion\n        return if (firstTombstone == -1) index else firstTombstone\n    }\n\n    /* Query operation */\n    fun get(key: Int): String? {\n        // Search for bucket index corresponding to key\n        val index = findBucket(key)\n        // If key-value pair is found, return corresponding val\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            return buckets[index]?._val\n        }\n        // If key-value pair does not exist, return null\n        return null\n    }\n\n    /* Add operation */\n    fun put(key: Int, _val: String) {\n        // When load factor exceeds threshold, perform expansion\n        if (loadFactor() > loadThres) {\n            extend()\n        }\n        // Search for bucket index corresponding to key\n        val index = findBucket(key)\n        // If key-value pair is found, overwrite val and return\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index]!!._val = _val\n            return\n        }\n        // If key-value pair does not exist, add the key-value pair\n        buckets[index] = Pair(key, _val)\n        size++\n    }\n\n    /* Remove operation */\n    fun remove(key: Int) {\n        // Search for bucket index corresponding to key\n        val index = findBucket(key)\n        // If key-value pair is found, overwrite it with removal marker\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index] = TOMBSTONE\n            size--\n        }\n    }\n\n    /* Expand hash table */\n    fun extend() {\n        // Temporarily store the original hash table\n        val bucketsTmp = buckets\n        // Initialize expanded new hash table\n        capacity *= extendRatio\n        buckets = arrayOfNulls(capacity)\n        size = 0\n        // Move key-value pairs from original hash table to new hash table\n        for (pair in bucketsTmp) {\n            if (pair != null && pair != TOMBSTONE) {\n                put(pair.key, pair._val)\n            }\n        }\n    }\n\n    /* Print hash table */\n    fun print() {\n        for (pair in buckets) {\n            if (pair == null) {\n                println(\"null\")\n            } else if (pair == TOMBSTONE) {\n                println(\"TOMESTOME\")\n            } else {\n                println(\"${pair.key} -> ${pair._val}\")\n            }\n        }\n    }\n}\n
      hash_map_open_addressing.rb
      ### Hash map with open addressing ###\nclass HashMapOpenAddressing\n  TOMBSTONE = Pair.new(-1, '-1') # Removal marker\n\n  ### Constructor ###\n  def initialize\n    @size = 0 # Number of key-value pairs\n    @capacity = 4 # Hash table capacity\n    @load_thres = 2.0 / 3.0 # Load factor threshold for triggering expansion\n    @extend_ratio = 2 # Expansion multiplier\n    @buckets = Array.new(@capacity) # Bucket array\n  end\n\n  ### Hash function ###\n  def hash_func(key)\n    key % @capacity\n  end\n\n  ### Load factor ###\n  def load_factor\n    @size / @capacity\n  end\n\n  ### Search bucket index for key ###\n  def find_bucket(key)\n    index = hash_func(key)\n    first_tombstone = -1\n    # Linear probing, break when encountering an empty bucket\n    while !@buckets[index].nil?\n      # If key is encountered, return the corresponding bucket index\n      if @buckets[index].key == key\n        # If a removal marker was encountered before, move the key-value pair to that index\n        if first_tombstone != -1\n          @buckets[first_tombstone] = @buckets[index]\n          @buckets[index] = TOMBSTONE\n          return first_tombstone # Return the moved bucket index\n        end\n        return index # Return bucket index\n      end\n      # Record the first removal marker encountered\n      first_tombstone = index if first_tombstone == -1 && @buckets[index] == TOMBSTONE\n      # Calculate bucket index, wrap around to the head if past the tail\n      index = (index + 1) % @capacity\n    end\n    # If key does not exist, return the index for insertion\n    first_tombstone == -1 ? index : first_tombstone\n  end\n\n  ### Query operation ###\n  def get(key)\n    # Search for bucket index corresponding to key\n    index = find_bucket(key)\n    # If key-value pair is found, return corresponding val\n    return @buckets[index].val unless [nil, TOMBSTONE].include?(@buckets[index])\n    # Return nil if key-value pair does not exist\n    nil\n  end\n\n  ### Add operation ###\n  def put(key, val)\n    # When load factor exceeds threshold, perform expansion\n    extend if load_factor > @load_thres\n    # Search for bucket index corresponding to key\n    index = find_bucket(key)\n    # If key-value pair found, overwrite val and return\n    unless [nil, TOMBSTONE].include?(@buckets[index])\n      @buckets[index].val = val\n      return\n    end\n    # If key-value pair does not exist, add the key-value pair\n    @buckets[index] = Pair.new(key, val)\n    @size += 1\n  end\n\n  ### Delete operation ###\n  def remove(key)\n    # Search for bucket index corresponding to key\n    index = find_bucket(key)\n    # If key-value pair is found, overwrite it with removal marker\n    unless [nil, TOMBSTONE].include?(@buckets[index])\n      @buckets[index] = TOMBSTONE\n      @size -= 1\n    end\n  end\n\n  ### Expand hash table ###\n  def extend\n    # Temporarily store the original hash table\n    buckets_tmp = @buckets\n    # Initialize expanded new hash table\n    @capacity *= @extend_ratio\n    @buckets = Array.new(@capacity)\n    @size = 0\n    # Move key-value pairs from original hash table to new hash table\n    for pair in buckets_tmp\n      put(pair.key, pair.val) unless [nil, TOMBSTONE].include?(pair)\n    end\n  end\n\n  ### Print hash table ###\n  def print\n    for pair in @buckets\n      if pair.nil?\n        puts \"Nil\"\n      elsif pair == TOMBSTONE\n        puts \"TOMBSTONE\"\n      else\n        puts \"#{pair.key} -> #{pair.val}\"\n      end\n    end\n  end\nend\n
      "},{"location":"chapter_hashing/hash_collision/#2-quadratic-probing","title":"2. \u00a0 Quadratic Probing","text":"

      Quadratic probing is similar to linear probing and is one of the common strategies for open addressing. When a collision occurs, quadratic probing does not simply skip a fixed number of steps but skips a number of steps equal to the \"square of the number of probes\", i.e., \\(1, 4, 9, \\dots\\) steps.

      Quadratic probing has the following advantages:

      • Quadratic probing attempts to alleviate the clustering effect of linear probing by skipping distances equal to the square of the probe count.
      • Quadratic probing skips larger distances to find empty positions, which helps to distribute data more evenly.

      However, quadratic probing is not perfect:

      • Clustering still exists, i.e., some positions are more likely to be occupied than others.
      • Due to the growth of squares, quadratic probing may not probe the entire hash table, meaning that even if there are empty buckets in the hash table, quadratic probing may not be able to access them.
      "},{"location":"chapter_hashing/hash_collision/#3-double-hashing","title":"3. \u00a0 Double Hashing","text":"

      As the name suggests, the double hashing method uses multiple hash functions \\(f_1(x)\\), \\(f_2(x)\\), \\(f_3(x)\\), \\(\\dots\\) for probing.

      • Inserting elements: If hash function \\(f_1(x)\\) encounters a conflict, try \\(f_2(x)\\), and so on, until an empty position is found and the element is inserted.
      • Searching for elements: Search in the same order of hash functions until the target element is found and return it; if an empty position is encountered or all hash functions have been tried, it indicates the element is not in the hash table, then return None.

      Compared to linear probing, the double hashing method is less prone to clustering, but multiple hash functions introduce additional computational overhead.

      Tip

      Please note that open addressing (linear probing, quadratic probing, and double hashing) hash tables all have the problem of \"cannot directly delete elements\".

      "},{"location":"chapter_hashing/hash_collision/#623-choice-of-programming-languages","title":"6.2.3 \u00a0 Choice of Programming Languages","text":"

      Different programming languages adopt different hash table implementation strategies. Here are a few examples:

      • Python uses open addressing. The dict dictionary uses pseudo-random numbers for probing.
      • Java uses separate chaining. Since JDK 1.8, when the array length in HashMap reaches 64 and the length of a linked list reaches 8, the linked list is converted to a red-black tree to improve search performance.
      • Go uses separate chaining. Go stipulates that each bucket can store up to 8 key-value pairs, and if the capacity is exceeded, an overflow bucket is linked; when there are too many overflow buckets, a special equal-capacity expansion operation is performed to ensure performance.
      "},{"location":"chapter_hashing/hash_map/","title":"6.1 \u00a0 Hash Table","text":"

      A hash table, also known as a hash map, establishes a mapping between keys key and values value, enabling efficient element retrieval. Specifically, when we input a key key into a hash table, we can retrieve the corresponding value value in \\(O(1)\\) time.

      As shown in Figure 6-1, given \\(n\\) students, each with two pieces of data: \"name\" and \"student ID\". If we want to implement a query function that \"inputs a student ID and returns the corresponding name\", we can use the hash table shown below.

      Figure 6-1 \u00a0 Abstract representation of a hash table

      In addition to hash tables, arrays and linked lists can also implement query functionality. Their efficiency comparison is shown in the following table.

      • Adding elements: Simply add elements to the end of the array (linked list), using \\(O(1)\\) time.
      • Querying elements: Since the array (linked list) is unordered, all elements need to be traversed, using \\(O(n)\\) time.
      • Deleting elements: The element must first be located, then deleted from the array (linked list), using \\(O(n)\\) time.

      Table 6-1 \u00a0 Comparison of element query efficiency

      Array Linked List Hash Table Find element \\(O(n)\\) \\(O(n)\\) \\(O(1)\\) Add element \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) Delete element \\(O(n)\\) \\(O(n)\\) \\(O(1)\\)

      As observed, the time complexity for insertion, deletion, search, and modification operations in a hash table is \\(O(1)\\), which is very efficient.

      "},{"location":"chapter_hashing/hash_map/#611-common-hash-table-operations","title":"6.1.1 \u00a0 Common Hash Table Operations","text":"

      Common operations on hash tables include: initialization, query operations, adding key-value pairs, and deleting key-value pairs. Example code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map.py
      # Initialize hash table\nhmap: dict = {}\n\n# Add operation\n# Add key-value pair (key, value) to hash table\nhmap[12836] = \"XiaoHa\"\nhmap[15937] = \"XiaoLuo\"\nhmap[16750] = \"XiaoSuan\"\nhmap[13276] = \"XiaoFa\"\nhmap[10583] = \"XiaoYa\"\n\n# Query operation\n# Input key into hash table to get value\nname: str = hmap[15937]\n\n# Delete operation\n# Delete key-value pair (key, value) from hash table\nhmap.pop(10583)\n
      hash_map.cpp
      /* Initialize hash table */\nunordered_map<int, string> map;\n\n/* Add operation */\n// Add key-value pair (key, value) to hash table\nmap[12836] = \"XiaoHa\";\nmap[15937] = \"XiaoLuo\";\nmap[16750] = \"XiaoSuan\";\nmap[13276] = \"XiaoFa\";\nmap[10583] = \"XiaoYa\";\n\n/* Query operation */\n// Input key into hash table to get value\nstring name = map[15937];\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.erase(10583);\n
      hash_map.java
      /* Initialize hash table */\nMap<Integer, String> map = new HashMap<>();\n\n/* Add operation */\n// Add key-value pair (key, value) to hash table\nmap.put(12836, \"XiaoHa\");\nmap.put(15937, \"XiaoLuo\");\nmap.put(16750, \"XiaoSuan\");\nmap.put(13276, \"XiaoFa\");\nmap.put(10583, \"XiaoYa\");\n\n/* Query operation */\n// Input key into hash table to get value\nString name = map.get(15937);\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.remove(10583);\n
      hash_map.cs
      /* Initialize hash table */\nDictionary<int, string> map = new() {\n    /* Add operation */\n    // Add key-value pair (key, value) to hash table\n    { 12836, \"XiaoHa\" },\n    { 15937, \"XiaoLuo\" },\n    { 16750, \"XiaoSuan\" },\n    { 13276, \"XiaoFa\" },\n    { 10583, \"XiaoYa\" }\n};\n\n/* Query operation */\n// Input key into hash table to get value\nstring name = map[15937];\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.Remove(10583);\n
      hash_map_test.go
      /* Initialize hash table */\nhmap := make(map[int]string)\n\n/* Add operation */\n// Add key-value pair (key, value) to hash table\nhmap[12836] = \"XiaoHa\"\nhmap[15937] = \"XiaoLuo\"\nhmap[16750] = \"XiaoSuan\"\nhmap[13276] = \"XiaoFa\"\nhmap[10583] = \"XiaoYa\"\n\n/* Query operation */\n// Input key into hash table to get value\nname := hmap[15937]\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\ndelete(hmap, 10583)\n
      hash_map.swift
      /* Initialize hash table */\nvar map: [Int: String] = [:]\n\n/* Add operation */\n// Add key-value pair (key, value) to hash table\nmap[12836] = \"XiaoHa\"\nmap[15937] = \"XiaoLuo\"\nmap[16750] = \"XiaoSuan\"\nmap[13276] = \"XiaoFa\"\nmap[10583] = \"XiaoYa\"\n\n/* Query operation */\n// Input key into hash table to get value\nlet name = map[15937]!\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.removeValue(forKey: 10583)\n
      hash_map.js
      /* Initialize hash table */\nconst map = new Map();\n/* Add operation */\n// Add key-value pair (key, value) to hash table\nmap.set(12836, 'XiaoHa');\nmap.set(15937, 'XiaoLuo');\nmap.set(16750, 'XiaoSuan');\nmap.set(13276, 'XiaoFa');\nmap.set(10583, 'XiaoYa');\n\n/* Query operation */\n// Input key into hash table to get value\nlet name = map.get(15937);\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.delete(10583);\n
      hash_map.ts
      /* Initialize hash table */\nconst map = new Map<number, string>();\n/* Add operation */\n// Add key-value pair (key, value) to hash table\nmap.set(12836, 'XiaoHa');\nmap.set(15937, 'XiaoLuo');\nmap.set(16750, 'XiaoSuan');\nmap.set(13276, 'XiaoFa');\nmap.set(10583, 'XiaoYa');\nconsole.info('\\nAfter adding, hash table is\\nKey -> Value');\nconsole.info(map);\n\n/* Query operation */\n// Input key into hash table to get value\nlet name = map.get(15937);\nconsole.info('\\nInput student ID 15937, queried name ' + name);\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.delete(10583);\nconsole.info('\\nAfter deleting 10583, hash table is\\nKey -> Value');\nconsole.info(map);\n
      hash_map.dart
      /* Initialize hash table */\nMap<int, String> map = {};\n\n/* Add operation */\n// Add key-value pair (key, value) to hash table\nmap[12836] = \"XiaoHa\";\nmap[15937] = \"XiaoLuo\";\nmap[16750] = \"XiaoSuan\";\nmap[13276] = \"XiaoFa\";\nmap[10583] = \"XiaoYa\";\n\n/* Query operation */\n// Input key into hash table to get value\nString name = map[15937];\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.remove(10583);\n
      hash_map.rs
      use std::collections::HashMap;\n\n/* Initialize hash table */\nlet mut map: HashMap<i32, String> = HashMap::new();\n\n/* Add operation */\n// Add key-value pair (key, value) to hash table\nmap.insert(12836, \"XiaoHa\".to_string());\nmap.insert(15937, \"XiaoLuo\".to_string());\nmap.insert(16750, \"XiaoSuan\".to_string());\nmap.insert(13279, \"XiaoFa\".to_string());\nmap.insert(10583, \"XiaoYa\".to_string());\n\n/* Query operation */\n// Input key into hash table to get value\nlet _name: Option<&String> = map.get(&15937);\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nlet _removed_value: Option<String> = map.remove(&10583);\n
      hash_map.c
      // C does not provide a built-in hash table\n
      hash_map.kt
      /* Initialize hash table */\nval map = HashMap<Int,String>()\n\n/* Add operation */\n// Add key-value pair (key, value) to hash table\nmap[12836] = \"XiaoHa\"\nmap[15937] = \"XiaoLuo\"\nmap[16750] = \"XiaoSuan\"\nmap[13276] = \"XiaoFa\"\nmap[10583] = \"XiaoYa\"\n\n/* Query operation */\n// Input key into hash table to get value\nval name = map[15937]\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.remove(10583)\n
      hash_map.rb
      # Initialize hash table\nhmap = {}\n\n# Add operation\n# Add key-value pair (key, value) to hash table\nhmap[12836] = \"XiaoHa\"\nhmap[15937] = \"XiaoLuo\"\nhmap[16750] = \"XiaoSuan\"\nhmap[13276] = \"XiaoFa\"\nhmap[10583] = \"XiaoYa\"\n\n# Query operation\n# Input key into hash table to get value\nname = hmap[15937]\n\n# Delete operation\n# Delete key-value pair (key, value) from hash table\nhmap.delete(10583)\n
      Visualized Execution

      https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%93%88%E5%B8%8C%E8%A1%A8%0A%20%20%20%20hmap%20%3D%20%7B%7D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%B7%BB%E5%8A%A0%E6%93%8D%E4%BD%9C%0A%20%20%20%20%23%20%E5%9C%A8%E5%93%88%E5%B8%8C%E8%A1%A8%E4%B8%AD%E6%B7%BB%E5%8A%A0%E9%94%AE%E5%80%BC%E5%AF%B9%20%28key,%20value%29%0A%20%20%20%20hmap%5B12836%5D%20%3D%20%22%E5%B0%8F%E5%93%88%22%0A%20%20%20%20hmap%5B15937%5D%20%3D%20%22%E5%B0%8F%E5%95%B0%22%0A%20%20%20%20hmap%5B16750%5D%20%3D%20%22%E5%B0%8F%E7%AE%97%22%0A%20%20%20%20hmap%5B13276%5D%20%3D%20%22%E5%B0%8F%E6%B3%95%22%0A%20%20%20%20hmap%5B10583%5D%20%3D%20%22%E5%B0%8F%E9%B8%AD%22%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C%0A%20%20%20%20%23%20%E5%90%91%E5%93%88%E5%B8%8C%E8%A1%A8%E4%B8%AD%E8%BE%93%E5%85%A5%E9%94%AE%20key%20%EF%BC%8C%E5%BE%97%E5%88%B0%E5%80%BC%20value%0A%20%20%20%20name%20%3D%20hmap%5B15937%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E6%93%8D%E4%BD%9C%0A%20%20%20%20%23%20%E5%9C%A8%E5%93%88%E5%B8%8C%E8%A1%A8%E4%B8%AD%E5%88%A0%E9%99%A4%E9%94%AE%E5%80%BC%E5%AF%B9%20%28key,%20value%29%0A%20%20%20%20hmap.pop%2810583%29&cumulative=false&curInstr=2&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

      There are three common ways to traverse a hash table: traversing key-value pairs, traversing keys, and traversing values. Example code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map.py
      # Traverse hash table\n# Traverse key-value pairs key->value\nfor key, value in hmap.items():\n    print(key, \"->\", value)\n# Traverse keys only\nfor key in hmap.keys():\n    print(key)\n# Traverse values only\nfor value in hmap.values():\n    print(value)\n
      hash_map.cpp
      /* Traverse hash table */\n// Traverse key-value pairs key->value\nfor (auto kv: map) {\n    cout << kv.first << \" -> \" << kv.second << endl;\n}\n// Traverse using iterator key->value\nfor (auto iter = map.begin(); iter != map.end(); iter++) {\n    cout << iter->first << \"->\" << iter->second << endl;\n}\n
      hash_map.java
      /* Traverse hash table */\n// Traverse key-value pairs key->value\nfor (Map.Entry<Integer, String> kv: map.entrySet()) {\n    System.out.println(kv.getKey() + \" -> \" + kv.getValue());\n}\n// Traverse keys only\nfor (int key: map.keySet()) {\n    System.out.println(key);\n}\n// Traverse values only\nfor (String val: map.values()) {\n    System.out.println(val);\n}\n
      hash_map.cs
      /* Traverse hash table */\n// Traverse key-value pairs Key->Value\nforeach (var kv in map) {\n    Console.WriteLine(kv.Key + \" -> \" + kv.Value);\n}\n// Traverse keys only\nforeach (int key in map.Keys) {\n    Console.WriteLine(key);\n}\n// Traverse values only\nforeach (string val in map.Values) {\n    Console.WriteLine(val);\n}\n
      hash_map_test.go
      /* Traverse hash table */\n// Traverse key-value pairs key->value\nfor key, value := range hmap {\n    fmt.Println(key, \"->\", value)\n}\n// Traverse keys only\nfor key := range hmap {\n    fmt.Println(key)\n}\n// Traverse values only\nfor _, value := range hmap {\n    fmt.Println(value)\n}\n
      hash_map.swift
      /* Traverse hash table */\n// Traverse key-value pairs Key->Value\nfor (key, value) in map {\n    print(\"\\(key) -> \\(value)\")\n}\n// Traverse keys only\nfor key in map.keys {\n    print(key)\n}\n// Traverse values only\nfor value in map.values {\n    print(value)\n}\n
      hash_map.js
      /* Traverse hash table */\nconsole.info('\\nTraverse key-value pairs Key->Value');\nfor (const [k, v] of map.entries()) {\n    console.info(k + ' -> ' + v);\n}\nconsole.info('\\nTraverse keys only Key');\nfor (const k of map.keys()) {\n    console.info(k);\n}\nconsole.info('\\nTraverse values only Value');\nfor (const v of map.values()) {\n    console.info(v);\n}\n
      hash_map.ts
      /* Traverse hash table */\nconsole.info('\\nTraverse key-value pairs Key->Value');\nfor (const [k, v] of map.entries()) {\n    console.info(k + ' -> ' + v);\n}\nconsole.info('\\nTraverse keys only Key');\nfor (const k of map.keys()) {\n    console.info(k);\n}\nconsole.info('\\nTraverse values only Value');\nfor (const v of map.values()) {\n    console.info(v);\n}\n
      hash_map.dart
      /* Traverse hash table */\n// Traverse key-value pairs Key->Value\nmap.forEach((key, value) {\n  print('$key -> $value');\n});\n\n// Traverse keys only\nmap.keys.forEach((key) {\n  print(key);\n});\n\n// Traverse values only\nmap.values.forEach((value) {\n  print(value);\n});\n
      hash_map.rs
      /* Traverse hash table */\n// Traverse key-value pairs Key->Value\nfor (key, value) in &map {\n    println!(\"{key} -> {value}\");\n}\n\n// Traverse keys only\nfor key in map.keys() {\n    println!(\"{key}\");\n}\n\n// Traverse values only\nfor value in map.values() {\n    println!(\"{value}\");\n}\n
      hash_map.c
      // C does not provide a built-in hash table\n
      hash_map.kt
      /* Traverse hash table */\n// Traverse key-value pairs key->value\nfor ((key, value) in map) {\n    println(\"$key -> $value\")\n}\n// Traverse keys only\nfor (key in map.keys) {\n    println(key)\n}\n// Traverse values only\nfor (_val in map.values) {\n    println(_val)\n}\n
      hash_map.rb
      # Traverse hash table\n# Traverse key-value pairs key->value\nhmap.entries.each { |key, value| puts \"#{key} -> #{value}\" }\n\n# Traverse keys only\nhmap.keys.each { |key| puts key }\n\n# Traverse values only\nhmap.values.each { |val| puts val }\n
      Visualized Execution

      https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%93%88%E5%B8%8C%E8%A1%A8%0A%20%20%20%20hmap%20%3D%20%7B%7D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%B7%BB%E5%8A%A0%E6%93%8D%E4%BD%9C%0A%20%20%20%20%23%20%E5%9C%A8%E5%93%88%E5%B8%8C%E8%A1%A8%E4%B8%AD%E6%B7%BB%E5%8A%A0%E9%94%AE%E5%80%BC%E5%AF%B9%20%28key,%20value%29%0A%20%20%20%20hmap%5B12836%5D%20%3D%20%22%E5%B0%8F%E5%93%88%22%0A%20%20%20%20hmap%5B15937%5D%20%3D%20%22%E5%B0%8F%E5%95%B0%22%0A%20%20%20%20hmap%5B16750%5D%20%3D%20%22%E5%B0%8F%E7%AE%97%22%0A%20%20%20%20hmap%5B13276%5D%20%3D%20%22%E5%B0%8F%E6%B3%95%22%0A%20%20%20%20hmap%5B10583%5D%20%3D%20%22%E5%B0%8F%E9%B8%AD%22%0A%20%20%20%20%0A%20%20%20%20%23%20%E9%81%8D%E5%8E%86%E5%93%88%E5%B8%8C%E8%A1%A8%0A%20%20%20%20%23%20%E9%81%8D%E5%8E%86%E9%94%AE%E5%80%BC%E5%AF%B9%20key-%3Evalue%0A%20%20%20%20for%20key,%20value%20in%20hmap.items%28%29%3A%0A%20%20%20%20%20%20%20%20print%28key,%20%22-%3E%22,%20value%29%0A%20%20%20%20%23%20%E5%8D%95%E7%8B%AC%E9%81%8D%E5%8E%86%E9%94%AE%20key%0A%20%20%20%20for%20key%20in%20hmap.keys%28%29%3A%0A%20%20%20%20%20%20%20%20print%28key%29%0A%20%20%20%20%23%20%E5%8D%95%E7%8B%AC%E9%81%8D%E5%8E%86%E5%80%BC%20value%0A%20%20%20%20for%20value%20in%20hmap.values%28%29%3A%0A%20%20%20%20%20%20%20%20print%28value%29&cumulative=false&curInstr=8&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

      "},{"location":"chapter_hashing/hash_map/#612-simple-hash-table-implementation","title":"6.1.2 \u00a0 Simple Hash Table Implementation","text":"

      Let's first consider the simplest case: implementing a hash table using only an array. In a hash table, each empty position in the array is called a bucket, and each bucket can store a key-value pair. Therefore, the query operation is to find the bucket corresponding to key and retrieve the value from the bucket.

      So how do we locate the corresponding bucket based on key? This is achieved through a hash function. The role of the hash function is to map a larger input space to a smaller output space. In a hash table, the input space is all keys, and the output space is all buckets (array indices). In other words, given a key, we can use the hash function to obtain the storage location of the key-value pair corresponding to that key in the array.

      When inputting a key, the hash function's calculation process consists of the following two steps:

      1. Calculate the hash value through a hash algorithm hash().
      2. Take the modulo of the hash value by the number of buckets (array length) capacity to obtain the bucket (array index) index corresponding to that key.
      index = hash(key) % capacity\n

      Subsequently, we can use index to access the corresponding bucket in the hash table and retrieve the value.

      Assuming the array length is capacity = 100 and the hash algorithm is hash(key) = key, the hash function becomes key % 100. Figure 6-2 shows the working principle of the hash function using key as student ID and value as name.

      Figure 6-2 \u00a0 Working principle of hash function

      The following code implements a simple hash table. Here, we encapsulate key and value into a class Pair to represent a key-value pair.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_hash_map.py
      class Pair:\n    \"\"\"Key-value pair\"\"\"\n\n    def __init__(self, key: int, val: str):\n        self.key = key\n        self.val = val\n\nclass ArrayHashMap:\n    \"\"\"Hash table based on array implementation\"\"\"\n\n    def __init__(self):\n        \"\"\"Constructor\"\"\"\n        # Initialize array with 100 buckets\n        self.buckets: list[Pair | None] = [None] * 100\n\n    def hash_func(self, key: int) -> int:\n        \"\"\"Hash function\"\"\"\n        index = key % 100\n        return index\n\n    def get(self, key: int) -> str | None:\n        \"\"\"Query operation\"\"\"\n        index: int = self.hash_func(key)\n        pair: Pair = self.buckets[index]\n        if pair is None:\n            return None\n        return pair.val\n\n    def put(self, key: int, val: str):\n        \"\"\"Add and update operation\"\"\"\n        pair = Pair(key, val)\n        index: int = self.hash_func(key)\n        self.buckets[index] = pair\n\n    def remove(self, key: int):\n        \"\"\"Remove operation\"\"\"\n        index: int = self.hash_func(key)\n        # Set to None to represent removal\n        self.buckets[index] = None\n\n    def entry_set(self) -> list[Pair]:\n        \"\"\"Get all key-value pairs\"\"\"\n        result: list[Pair] = []\n        for pair in self.buckets:\n            if pair is not None:\n                result.append(pair)\n        return result\n\n    def key_set(self) -> list[int]:\n        \"\"\"Get all keys\"\"\"\n        result = []\n        for pair in self.buckets:\n            if pair is not None:\n                result.append(pair.key)\n        return result\n\n    def value_set(self) -> list[str]:\n        \"\"\"Get all values\"\"\"\n        result = []\n        for pair in self.buckets:\n            if pair is not None:\n                result.append(pair.val)\n        return result\n\n    def print(self):\n        \"\"\"Print hash table\"\"\"\n        for pair in self.buckets:\n            if pair is not None:\n                print(pair.key, \"->\", pair.val)\n
      array_hash_map.cpp
      /* Key-value pair */\nstruct Pair {\n  public:\n    int key;\n    string val;\n    Pair(int key, string val) {\n        this->key = key;\n        this->val = val;\n    }\n};\n\n/* Hash table based on array implementation */\nclass ArrayHashMap {\n  private:\n    vector<Pair *> buckets;\n\n  public:\n    ArrayHashMap() {\n        // Initialize array with 100 buckets\n        buckets = vector<Pair *>(100);\n    }\n\n    ~ArrayHashMap() {\n        // Free memory\n        for (const auto &bucket : buckets) {\n            delete bucket;\n        }\n        buckets.clear();\n    }\n\n    /* Hash function */\n    int hashFunc(int key) {\n        int index = key % 100;\n        return index;\n    }\n\n    /* Query operation */\n    string get(int key) {\n        int index = hashFunc(key);\n        Pair *pair = buckets[index];\n        if (pair == nullptr)\n            return \"\";\n        return pair->val;\n    }\n\n    /* Add operation */\n    void put(int key, string val) {\n        Pair *pair = new Pair(key, val);\n        int index = hashFunc(key);\n        buckets[index] = pair;\n    }\n\n    /* Remove operation */\n    void remove(int key) {\n        int index = hashFunc(key);\n        // Free memory and set to nullptr\n        delete buckets[index];\n        buckets[index] = nullptr;\n    }\n\n    /* Get all key-value pairs */\n    vector<Pair *> pairSet() {\n        vector<Pair *> pairSet;\n        for (Pair *pair : buckets) {\n            if (pair != nullptr) {\n                pairSet.push_back(pair);\n            }\n        }\n        return pairSet;\n    }\n\n    /* Get all keys */\n    vector<int> keySet() {\n        vector<int> keySet;\n        for (Pair *pair : buckets) {\n            if (pair != nullptr) {\n                keySet.push_back(pair->key);\n            }\n        }\n        return keySet;\n    }\n\n    /* Get all values */\n    vector<string> valueSet() {\n        vector<string> valueSet;\n        for (Pair *pair : buckets) {\n            if (pair != nullptr) {\n                valueSet.push_back(pair->val);\n            }\n        }\n        return valueSet;\n    }\n\n    /* Print hash table */\n    void print() {\n        for (Pair *kv : pairSet()) {\n            cout << kv->key << \" -> \" << kv->val << endl;\n        }\n    }\n};\n
      array_hash_map.java
      /* Key-value pair */\nclass Pair {\n    public int key;\n    public String val;\n\n    public Pair(int key, String val) {\n        this.key = key;\n        this.val = val;\n    }\n}\n\n/* Hash table based on array implementation */\nclass ArrayHashMap {\n    private List<Pair> buckets;\n\n    public ArrayHashMap() {\n        // Initialize array with 100 buckets\n        buckets = new ArrayList<>();\n        for (int i = 0; i < 100; i++) {\n            buckets.add(null);\n        }\n    }\n\n    /* Hash function */\n    private int hashFunc(int key) {\n        int index = key % 100;\n        return index;\n    }\n\n    /* Query operation */\n    public String get(int key) {\n        int index = hashFunc(key);\n        Pair pair = buckets.get(index);\n        if (pair == null)\n            return null;\n        return pair.val;\n    }\n\n    /* Add operation */\n    public void put(int key, String val) {\n        Pair pair = new Pair(key, val);\n        int index = hashFunc(key);\n        buckets.set(index, pair);\n    }\n\n    /* Remove operation */\n    public void remove(int key) {\n        int index = hashFunc(key);\n        // Set to null to represent deletion\n        buckets.set(index, null);\n    }\n\n    /* Get all key-value pairs */\n    public List<Pair> pairSet() {\n        List<Pair> pairSet = new ArrayList<>();\n        for (Pair pair : buckets) {\n            if (pair != null)\n                pairSet.add(pair);\n        }\n        return pairSet;\n    }\n\n    /* Get all keys */\n    public List<Integer> keySet() {\n        List<Integer> keySet = new ArrayList<>();\n        for (Pair pair : buckets) {\n            if (pair != null)\n                keySet.add(pair.key);\n        }\n        return keySet;\n    }\n\n    /* Get all values */\n    public List<String> valueSet() {\n        List<String> valueSet = new ArrayList<>();\n        for (Pair pair : buckets) {\n            if (pair != null)\n                valueSet.add(pair.val);\n        }\n        return valueSet;\n    }\n\n    /* Print hash table */\n    public void print() {\n        for (Pair kv : pairSet()) {\n            System.out.println(kv.key + \" -> \" + kv.val);\n        }\n    }\n}\n
      array_hash_map.cs
      /* Key-value pair int->string */\nclass Pair(int key, string val) {\n    public int key = key;\n    public string val = val;\n}\n\n/* Hash table based on array implementation */\nclass ArrayHashMap {\n    List<Pair?> buckets;\n    public ArrayHashMap() {\n        // Initialize array with 100 buckets\n        buckets = [];\n        for (int i = 0; i < 100; i++) {\n            buckets.Add(null);\n        }\n    }\n\n    /* Hash function */\n    int HashFunc(int key) {\n        int index = key % 100;\n        return index;\n    }\n\n    /* Query operation */\n    public string? Get(int key) {\n        int index = HashFunc(key);\n        Pair? pair = buckets[index];\n        if (pair == null) return null;\n        return pair.val;\n    }\n\n    /* Add operation */\n    public void Put(int key, string val) {\n        Pair pair = new(key, val);\n        int index = HashFunc(key);\n        buckets[index] = pair;\n    }\n\n    /* Remove operation */\n    public void Remove(int key) {\n        int index = HashFunc(key);\n        // Set to null to represent deletion\n        buckets[index] = null;\n    }\n\n    /* Get all key-value pairs */\n    public List<Pair> PairSet() {\n        List<Pair> pairSet = [];\n        foreach (Pair? pair in buckets) {\n            if (pair != null)\n                pairSet.Add(pair);\n        }\n        return pairSet;\n    }\n\n    /* Get all keys */\n    public List<int> KeySet() {\n        List<int> keySet = [];\n        foreach (Pair? pair in buckets) {\n            if (pair != null)\n                keySet.Add(pair.key);\n        }\n        return keySet;\n    }\n\n    /* Get all values */\n    public List<string> ValueSet() {\n        List<string> valueSet = [];\n        foreach (Pair? pair in buckets) {\n            if (pair != null)\n                valueSet.Add(pair.val);\n        }\n        return valueSet;\n    }\n\n    /* Print hash table */\n    public void Print() {\n        foreach (Pair kv in PairSet()) {\n            Console.WriteLine(kv.key + \" -> \" + kv.val);\n        }\n    }\n}\n
      array_hash_map.go
      /* Key-value pair */\ntype pair struct {\n    key int\n    val string\n}\n\n/* Hash table based on array implementation */\ntype arrayHashMap struct {\n    buckets []*pair\n}\n\n/* Initialize hash table */\nfunc newArrayHashMap() *arrayHashMap {\n    // Initialize array with 100 buckets\n    buckets := make([]*pair, 100)\n    return &arrayHashMap{buckets: buckets}\n}\n\n/* Hash function */\nfunc (a *arrayHashMap) hashFunc(key int) int {\n    index := key % 100\n    return index\n}\n\n/* Query operation */\nfunc (a *arrayHashMap) get(key int) string {\n    index := a.hashFunc(key)\n    pair := a.buckets[index]\n    if pair == nil {\n        return \"Not Found\"\n    }\n    return pair.val\n}\n\n/* Add operation */\nfunc (a *arrayHashMap) put(key int, val string) {\n    pair := &pair{key: key, val: val}\n    index := a.hashFunc(key)\n    a.buckets[index] = pair\n}\n\n/* Remove operation */\nfunc (a *arrayHashMap) remove(key int) {\n    index := a.hashFunc(key)\n    // Set to nil to delete\n    a.buckets[index] = nil\n}\n\n/* Get all key pairs */\nfunc (a *arrayHashMap) pairSet() []*pair {\n    var pairs []*pair\n    for _, pair := range a.buckets {\n        if pair != nil {\n            pairs = append(pairs, pair)\n        }\n    }\n    return pairs\n}\n\n/* Get all keys */\nfunc (a *arrayHashMap) keySet() []int {\n    var keys []int\n    for _, pair := range a.buckets {\n        if pair != nil {\n            keys = append(keys, pair.key)\n        }\n    }\n    return keys\n}\n\n/* Get all values */\nfunc (a *arrayHashMap) valueSet() []string {\n    var values []string\n    for _, pair := range a.buckets {\n        if pair != nil {\n            values = append(values, pair.val)\n        }\n    }\n    return values\n}\n\n/* Print hash table */\nfunc (a *arrayHashMap) print() {\n    for _, pair := range a.buckets {\n        if pair != nil {\n            fmt.Println(pair.key, \"->\", pair.val)\n        }\n    }\n}\n
      array_hash_map.swift
      /* Key-value pair */\nclass Pair: Equatable {\n    public var key: Int\n    public var val: String\n\n    public init(key: Int, val: String) {\n        self.key = key\n        self.val = val\n    }\n\n    public static func == (lhs: Pair, rhs: Pair) -> Bool {\n        lhs.key == rhs.key && lhs.val == rhs.val\n    }\n}\n\n/* Hash table based on array implementation */\nclass ArrayHashMap {\n    private var buckets: [Pair?]\n\n    init() {\n        // Initialize array with 100 buckets\n        buckets = Array(repeating: nil, count: 100)\n    }\n\n    /* Hash function */\n    private func hashFunc(key: Int) -> Int {\n        let index = key % 100\n        return index\n    }\n\n    /* Query operation */\n    func get(key: Int) -> String? {\n        let index = hashFunc(key: key)\n        let pair = buckets[index]\n        return pair?.val\n    }\n\n    /* Add operation */\n    func put(key: Int, val: String) {\n        let pair = Pair(key: key, val: val)\n        let index = hashFunc(key: key)\n        buckets[index] = pair\n    }\n\n    /* Remove operation */\n    func remove(key: Int) {\n        let index = hashFunc(key: key)\n        // Set to nil to delete\n        buckets[index] = nil\n    }\n\n    /* Get all key-value pairs */\n    func pairSet() -> [Pair] {\n        buckets.compactMap { $0 }\n    }\n\n    /* Get all keys */\n    func keySet() -> [Int] {\n        buckets.compactMap { $0?.key }\n    }\n\n    /* Get all values */\n    func valueSet() -> [String] {\n        buckets.compactMap { $0?.val }\n    }\n\n    /* Print hash table */\n    func print() {\n        for pair in pairSet() {\n            Swift.print(\"\\(pair.key) -> \\(pair.val)\")\n        }\n    }\n}\n
      array_hash_map.js
      /* Key-value pair Number -> String */\nclass Pair {\n    constructor(key, val) {\n        this.key = key;\n        this.val = val;\n    }\n}\n\n/* Hash table based on array implementation */\nclass ArrayHashMap {\n    #buckets;\n    constructor() {\n        // Initialize array with 100 buckets\n        this.#buckets = new Array(100).fill(null);\n    }\n\n    /* Hash function */\n    #hashFunc(key) {\n        return key % 100;\n    }\n\n    /* Query operation */\n    get(key) {\n        let index = this.#hashFunc(key);\n        let pair = this.#buckets[index];\n        if (pair === null) return null;\n        return pair.val;\n    }\n\n    /* Add operation */\n    set(key, val) {\n        let index = this.#hashFunc(key);\n        this.#buckets[index] = new Pair(key, val);\n    }\n\n    /* Remove operation */\n    delete(key) {\n        let index = this.#hashFunc(key);\n        // Set to null to represent deletion\n        this.#buckets[index] = null;\n    }\n\n    /* Get all key-value pairs */\n    entries() {\n        let arr = [];\n        for (let i = 0; i < this.#buckets.length; i++) {\n            if (this.#buckets[i]) {\n                arr.push(this.#buckets[i]);\n            }\n        }\n        return arr;\n    }\n\n    /* Get all keys */\n    keys() {\n        let arr = [];\n        for (let i = 0; i < this.#buckets.length; i++) {\n            if (this.#buckets[i]) {\n                arr.push(this.#buckets[i].key);\n            }\n        }\n        return arr;\n    }\n\n    /* Get all values */\n    values() {\n        let arr = [];\n        for (let i = 0; i < this.#buckets.length; i++) {\n            if (this.#buckets[i]) {\n                arr.push(this.#buckets[i].val);\n            }\n        }\n        return arr;\n    }\n\n    /* Print hash table */\n    print() {\n        let pairSet = this.entries();\n        for (const pair of pairSet) {\n            console.info(`${pair.key} -> ${pair.val}`);\n        }\n    }\n}\n
      array_hash_map.ts
      /* Key-value pair Number -> String */\nclass Pair {\n    public key: number;\n    public val: string;\n\n    constructor(key: number, val: string) {\n        this.key = key;\n        this.val = val;\n    }\n}\n\n/* Hash table based on array implementation */\nclass ArrayHashMap {\n    private readonly buckets: (Pair | null)[];\n\n    constructor() {\n        // Initialize array with 100 buckets\n        this.buckets = new Array(100).fill(null);\n    }\n\n    /* Hash function */\n    private hashFunc(key: number): number {\n        return key % 100;\n    }\n\n    /* Query operation */\n    public get(key: number): string | null {\n        let index = this.hashFunc(key);\n        let pair = this.buckets[index];\n        if (pair === null) return null;\n        return pair.val;\n    }\n\n    /* Add operation */\n    public set(key: number, val: string) {\n        let index = this.hashFunc(key);\n        this.buckets[index] = new Pair(key, val);\n    }\n\n    /* Remove operation */\n    public delete(key: number) {\n        let index = this.hashFunc(key);\n        // Set to null to represent deletion\n        this.buckets[index] = null;\n    }\n\n    /* Get all key-value pairs */\n    public entries(): (Pair | null)[] {\n        let arr: (Pair | null)[] = [];\n        for (let i = 0; i < this.buckets.length; i++) {\n            if (this.buckets[i]) {\n                arr.push(this.buckets[i]);\n            }\n        }\n        return arr;\n    }\n\n    /* Get all keys */\n    public keys(): (number | undefined)[] {\n        let arr: (number | undefined)[] = [];\n        for (let i = 0; i < this.buckets.length; i++) {\n            if (this.buckets[i]) {\n                arr.push(this.buckets[i].key);\n            }\n        }\n        return arr;\n    }\n\n    /* Get all values */\n    public values(): (string | undefined)[] {\n        let arr: (string | undefined)[] = [];\n        for (let i = 0; i < this.buckets.length; i++) {\n            if (this.buckets[i]) {\n                arr.push(this.buckets[i].val);\n            }\n        }\n        return arr;\n    }\n\n    /* Print hash table */\n    public print() {\n        let pairSet = this.entries();\n        for (const pair of pairSet) {\n            console.info(`${pair.key} -> ${pair.val}`);\n        }\n    }\n}\n
      array_hash_map.dart
      /* Key-value pair */\nclass Pair {\n  int key;\n  String val;\n  Pair(this.key, this.val);\n}\n\n/* Hash table based on array implementation */\nclass ArrayHashMap {\n  late List<Pair?> _buckets;\n\n  ArrayHashMap() {\n    // Initialize array with 100 buckets\n    _buckets = List.filled(100, null);\n  }\n\n  /* Hash function */\n  int _hashFunc(int key) {\n    final int index = key % 100;\n    return index;\n  }\n\n  /* Query operation */\n  String? get(int key) {\n    final int index = _hashFunc(key);\n    final Pair? pair = _buckets[index];\n    if (pair == null) {\n      return null;\n    }\n    return pair.val;\n  }\n\n  /* Add operation */\n  void put(int key, String val) {\n    final Pair pair = Pair(key, val);\n    final int index = _hashFunc(key);\n    _buckets[index] = pair;\n  }\n\n  /* Remove operation */\n  void remove(int key) {\n    final int index = _hashFunc(key);\n    _buckets[index] = null;\n  }\n\n  /* Get all key-value pairs */\n  List<Pair> pairSet() {\n    List<Pair> pairSet = [];\n    for (final Pair? pair in _buckets) {\n      if (pair != null) {\n        pairSet.add(pair);\n      }\n    }\n    return pairSet;\n  }\n\n  /* Get all keys */\n  List<int> keySet() {\n    List<int> keySet = [];\n    for (final Pair? pair in _buckets) {\n      if (pair != null) {\n        keySet.add(pair.key);\n      }\n    }\n    return keySet;\n  }\n\n  /* Get all values */\n  List<String> values() {\n    List<String> valueSet = [];\n    for (final Pair? pair in _buckets) {\n      if (pair != null) {\n        valueSet.add(pair.val);\n      }\n    }\n    return valueSet;\n  }\n\n  /* Print hash table */\n  void printHashMap() {\n    for (final Pair kv in pairSet()) {\n      print(\"${kv.key} -> ${kv.val}\");\n    }\n  }\n}\n
      array_hash_map.rs
      /* Key-value pair */\n#[derive(Debug, Clone, PartialEq)]\npub struct Pair {\n    pub key: i32,\n    pub val: String,\n}\n\n/* Hash table based on array implementation */\npub struct ArrayHashMap {\n    buckets: Vec<Option<Pair>>,\n}\n\nimpl ArrayHashMap {\n    pub fn new() -> ArrayHashMap {\n        // Initialize array with 100 buckets\n        Self {\n            buckets: vec![None; 100],\n        }\n    }\n\n    /* Hash function */\n    fn hash_func(&self, key: i32) -> usize {\n        key as usize % 100\n    }\n\n    /* Query operation */\n    pub fn get(&self, key: i32) -> Option<&String> {\n        let index = self.hash_func(key);\n        self.buckets[index].as_ref().map(|pair| &pair.val)\n    }\n\n    /* Add operation */\n    pub fn put(&mut self, key: i32, val: &str) {\n        let index = self.hash_func(key);\n        self.buckets[index] = Some(Pair {\n            key,\n            val: val.to_string(),\n        });\n    }\n\n    /* Remove operation */\n    pub fn remove(&mut self, key: i32) {\n        let index = self.hash_func(key);\n        // Set to None to represent removal\n        self.buckets[index] = None;\n    }\n\n    /* Get all key-value pairs */\n    pub fn entry_set(&self) -> Vec<&Pair> {\n        self.buckets\n            .iter()\n            .filter_map(|pair| pair.as_ref())\n            .collect()\n    }\n\n    /* Get all keys */\n    pub fn key_set(&self) -> Vec<&i32> {\n        self.buckets\n            .iter()\n            .filter_map(|pair| pair.as_ref().map(|pair| &pair.key))\n            .collect()\n    }\n\n    /* Get all values */\n    pub fn value_set(&self) -> Vec<&String> {\n        self.buckets\n            .iter()\n            .filter_map(|pair| pair.as_ref().map(|pair| &pair.val))\n            .collect()\n    }\n\n    /* Print hash table */\n    pub fn print(&self) {\n        for pair in self.entry_set() {\n            println!(\"{} -> {}\", pair.key, pair.val);\n        }\n    }\n}\n
      array_hash_map.c
      /* Key-value pair int->string */\ntypedef struct {\n    int key;\n    char *val;\n} Pair;\n\n/* Hash table based on array implementation */\ntypedef struct {\n    Pair *buckets[MAX_SIZE];\n} ArrayHashMap;\n\n/* Constructor */\nArrayHashMap *newArrayHashMap() {\n    ArrayHashMap *hmap = malloc(sizeof(ArrayHashMap));\n    for (int i=0; i < MAX_SIZE; i++) {\n        hmap->buckets[i] = NULL;\n    }\n    return hmap;\n}\n\n/* Destructor */\nvoid delArrayHashMap(ArrayHashMap *hmap) {\n    for (int i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            free(hmap->buckets[i]->val);\n            free(hmap->buckets[i]);\n        }\n    }\n    free(hmap);\n}\n\n/* Add operation */\nvoid put(ArrayHashMap *hmap, const int key, const char *val) {\n    Pair *Pair = malloc(sizeof(Pair));\n    Pair->key = key;\n    Pair->val = malloc(strlen(val) + 1);\n    strcpy(Pair->val, val);\n\n    int index = hashFunc(key);\n    hmap->buckets[index] = Pair;\n}\n\n/* Remove operation */\nvoid removeItem(ArrayHashMap *hmap, const int key) {\n    int index = hashFunc(key);\n    free(hmap->buckets[index]->val);\n    free(hmap->buckets[index]);\n    hmap->buckets[index] = NULL;\n}\n\n/* Get all key-value pairs */\nvoid pairSet(ArrayHashMap *hmap, MapSet *set) {\n    Pair *entries;\n    int i = 0, index = 0;\n    int total = 0;\n    /* Count valid key-value pairs */\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            total++;\n        }\n    }\n    entries = malloc(sizeof(Pair) * total);\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            entries[index].key = hmap->buckets[i]->key;\n            entries[index].val = malloc(strlen(hmap->buckets[i]->val) + 1);\n            strcpy(entries[index].val, hmap->buckets[i]->val);\n            index++;\n        }\n    }\n    set->set = entries;\n    set->len = total;\n}\n\n/* Get all keys */\nvoid keySet(ArrayHashMap *hmap, MapSet *set) {\n    int *keys;\n    int i = 0, index = 0;\n    int total = 0;\n    /* Count valid key-value pairs */\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            total++;\n        }\n    }\n    keys = malloc(total * sizeof(int));\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            keys[index] = hmap->buckets[i]->key;\n            index++;\n        }\n    }\n    set->set = keys;\n    set->len = total;\n}\n\n/* Get all values */\nvoid valueSet(ArrayHashMap *hmap, MapSet *set) {\n    char **vals;\n    int i = 0, index = 0;\n    int total = 0;\n    /* Count valid key-value pairs */\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            total++;\n        }\n    }\n    vals = malloc(total * sizeof(char *));\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            vals[index] = hmap->buckets[i]->val;\n            index++;\n        }\n    }\n    set->set = vals;\n    set->len = total;\n}\n\n/* Print hash table */\nvoid print(ArrayHashMap *hmap) {\n    int i;\n    MapSet set;\n    pairSet(hmap, &set);\n    Pair *entries = (Pair *)set.set;\n    for (i = 0; i < set.len; i++) {\n        printf(\"%d -> %s\\n\", entries[i].key, entries[i].val);\n    }\n    free(set.set);\n}\n
      array_hash_map.kt
      /* Key-value pair */\nclass Pair(\n    var key: Int,\n    var _val: String\n)\n\n/* Hash table based on array implementation */\nclass ArrayHashMap {\n    // Initialize array with 100 buckets\n    private val buckets = arrayOfNulls<Pair>(100)\n\n    /* Hash function */\n    fun hashFunc(key: Int): Int {\n        val index = key % 100\n        return index\n    }\n\n    /* Query operation */\n    fun get(key: Int): String? {\n        val index = hashFunc(key)\n        val pair = buckets[index] ?: return null\n        return pair._val\n    }\n\n    /* Add operation */\n    fun put(key: Int, _val: String) {\n        val pair = Pair(key, _val)\n        val index = hashFunc(key)\n        buckets[index] = pair\n    }\n\n    /* Remove operation */\n    fun remove(key: Int) {\n        val index = hashFunc(key)\n        // Set to null to represent deletion\n        buckets[index] = null\n    }\n\n    /* Get all key-value pairs */\n    fun pairSet(): MutableList<Pair> {\n        val pairSet = mutableListOf<Pair>()\n        for (pair in buckets) {\n            if (pair != null)\n                pairSet.add(pair)\n        }\n        return pairSet\n    }\n\n    /* Get all keys */\n    fun keySet(): MutableList<Int> {\n        val keySet = mutableListOf<Int>()\n        for (pair in buckets) {\n            if (pair != null)\n                keySet.add(pair.key)\n        }\n        return keySet\n    }\n\n    /* Get all values */\n    fun valueSet(): MutableList<String> {\n        val valueSet = mutableListOf<String>()\n        for (pair in buckets) {\n            if (pair != null)\n                valueSet.add(pair._val)\n        }\n        return valueSet\n    }\n\n    /* Print hash table */\n    fun print() {\n        for (kv in pairSet()) {\n            val key = kv.key\n            val _val = kv._val\n            println(\"$key -> $_val\")\n        }\n    }\n}\n
      array_hash_map.rb
      ### Key-value pair ###\nclass Pair\n  attr_accessor :key, :val\n\n  def initialize(key, val)\n    @key = key\n    @val = val\n  end\nend\n\n### Hash map based on array ###\nclass ArrayHashMap\n  ### Constructor ###\n  def initialize\n    # Initialize array with 100 buckets\n    @buckets = Array.new(100)\n  end\n\n  ### Hash function ###\n  def hash_func(key)\n    index = key % 100\n  end\n\n  ### Query operation ###\n  def get(key)\n    index = hash_func(key)\n    pair = @buckets[index]\n\n    return if pair.nil?\n    pair.val\n  end\n\n  ### Add operation ###\n  def put(key, val)\n    pair = Pair.new(key, val)\n    index = hash_func(key)\n    @buckets[index] = pair\n  end\n\n  ### Delete operation ###\n  def remove(key)\n    index = hash_func(key)\n    # Set to nil to delete\n    @buckets[index] = nil\n  end\n\n  ### Get all key-value pairs ###\n  def entry_set\n    result = []\n    @buckets.each { |pair| result << pair unless pair.nil? }\n    result\n  end\n\n  ### Get all keys ###\n  def key_set\n    result = []\n    @buckets.each { |pair| result << pair.key unless pair.nil? }\n    result\n  end\n\n  ### Get all values ###\n  def value_set\n    result = []\n    @buckets.each { |pair| result << pair.val unless pair.nil? }\n    result\n  end\n\n  ### Print hash table ###\n  def print\n    @buckets.each { |pair| puts \"#{pair.key} -> #{pair.val}\" unless pair.nil? }\n  end\nend\n
      "},{"location":"chapter_hashing/hash_map/#613-hash-collision-and-resizing","title":"6.1.3 \u00a0 Hash Collision and Resizing","text":"

      Fundamentally, the role of a hash function is to map the input space consisting of all keys to the output space consisting of all array indices, and the input space is often much larger than the output space. Therefore, theoretically there must be cases where \"multiple inputs correspond to the same output\".

      For the hash function in the above example, when the input keys have the same last two digits, the hash function produces the same output. For example, when querying two students with IDs 12836 and 20336, we get:

      12836 % 100 = 36\n20336 % 100 = 36\n

      As shown in Figure 6-3, two student IDs point to the same name, which is obviously incorrect. We call this situation where multiple inputs correspond to the same output a hash collision.

      Figure 6-3 \u00a0 Hash collision example

      It's easy to see that the larger the hash table capacity \\(n\\), the lower the probability that multiple keys will be assigned to the same bucket, and the fewer collisions. Therefore, we can reduce hash collisions by expanding the hash table.

      As shown in Figure 6-4, before expansion, the key-value pairs (136, A) and (236, D) collided, but after expansion, the collision disappears.

      Figure 6-4 \u00a0 Hash table resizing

      Similar to array expansion, hash table expansion requires migrating all key-value pairs from the original hash table to the new hash table, which is very time-consuming. Moreover, since the hash table capacity capacity changes, we need to recalculate the storage locations of all key-value pairs through the hash function, further increasing the computational overhead of the expansion process. For this reason, programming languages typically reserve a sufficiently large hash table capacity to prevent frequent expansion.

      The load factor is an important concept for hash tables. It is defined as the number of elements in the hash table divided by the number of buckets, and is used to measure the severity of hash collisions. It is also commonly used as a trigger condition for hash table expansion. For example, in Java, when the load factor exceeds \\(0.75\\), the system will expand the hash table to \\(2\\) times its original size.

      "},{"location":"chapter_hashing/summary/","title":"6.4 \u00a0 Summary","text":""},{"location":"chapter_hashing/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • Given an input key, a hash table can retrieve the corresponding value in \\(O(1)\\) time, which is highly efficient.
      • Common hash table operations include querying, adding key-value pairs, deleting key-value pairs, and traversing the hash table.
      • The hash function maps a key to an array index, allowing access to the corresponding bucket and retrieval of the value.
      • Two different keys may end up with the same array index after hashing, leading to erroneous query results. This phenomenon is known as hash collision.
      • The larger the capacity of the hash table, the lower the probability of hash collisions. Therefore, hash table expansion can mitigate hash collisions. Similar to array expansion, hash table expansion is costly.
      • The load factor, defined as the number of elements divided by the number of buckets, reflects the severity of hash collisions and is often used as a condition to trigger hash table expansion.
      • Separate chaining addresses hash collisions by converting each element into a linked list, storing all colliding elements in the same linked list. However, excessively long linked lists can reduce query efficiency, which can be improved by converting the linked lists into red-black trees.
      • Open addressing handles hash collisions through multiple probing. Linear probing uses a fixed step size but cannot delete elements and is prone to clustering. Double hashing uses multiple hash functions for probing, which reduces clustering compared to linear probing but increases computational overhead.
      • Different programming languages adopt various hash table implementations. For example, Java's HashMap uses separate chaining, while Python's dict employs open addressing.
      • In hash tables, we desire hash algorithms with determinism, high efficiency, and uniform distribution. In cryptography, hash algorithms should also possess collision resistance and the avalanche effect.
      • Hash algorithms typically use large prime numbers as moduli to maximize the uniform distribution of hash values and reduce hash collisions.
      • Common hash algorithms include MD5, SHA-1, SHA-2, and SHA-3. MD5 is often used for file integrity checks, while SHA-2 is commonly used in secure applications and protocols.
      • Programming languages usually provide built-in hash algorithms for data types to calculate bucket indices in hash tables. Generally, only immutable objects are hashable.
      "},{"location":"chapter_hashing/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

      Q: When does the time complexity of a hash table degrade to \\(O(n)\\)?

      The time complexity of a hash table can degrade to \\(O(n)\\) when hash collisions are severe. When the hash function is well-designed, the capacity is set appropriately, and collisions are evenly distributed, the time complexity is \\(O(1)\\). We usually consider the time complexity to be \\(O(1)\\) when using built-in hash tables in programming languages.

      Q: Why not use the hash function \\(f(x) = x\\)? This would eliminate collisions.

      Under the hash function \\(f(x) = x\\), each element corresponds to a unique bucket index, which is equivalent to an array. However, the input space is usually much larger than the output space (array length), so the last step of a hash function is often to take the modulo of the array length. In other words, the goal of a hash table is to map a larger state space to a smaller one while providing \\(O(1)\\) query efficiency.

      Q: Why can hash tables be more efficient than arrays, linked lists, or binary trees, even though hash tables are implemented using these structures?

      Firstly, hash tables have higher time efficiency but lower space efficiency. A significant portion of memory in hash tables remains unused.

      Secondly, hash tables are only more time-efficient in specific use cases. If a feature can be implemented with the same time complexity using an array or a linked list, it's usually faster than using a hash table. This is because the computation of the hash function incurs overhead, making the constant factor in the time complexity larger.

      Lastly, the time complexity of hash tables can degrade. For example, in separate chaining, we perform search operations in a linked list or red-black tree, which still risks degrading to \\(O(n)\\) time.

      Q: Does double hashing also have the flaw of not being able to delete elements directly? Can space marked as deleted be reused?

      Double hashing is a form of open addressing, and all open addressing methods have the drawback of not being able to delete elements directly; they require marking elements as deleted. Marked spaces can be reused. When inserting new elements into the hash table, and the hash function points to a position marked as deleted, that position can be used by the new element. This maintains the probing sequence of the hash table while ensuring efficient use of space.

      Q: Why do hash collisions occur during the search process in linear probing?

      During the search process, the hash function points to the corresponding bucket and key-value pair. If the key doesn't match, it indicates a hash collision. Therefore, linear probing will search downward at a predetermined step size until the correct key-value pair is found or the search fails.

      Q: Why can expanding a hash table alleviate hash collisions?

      The last step of a hash function often involves taking the modulo of the array length \\(n\\), to keep the output within the array index range. When expanding, the array length \\(n\\) changes, and the indices corresponding to the keys may also change. Keys that were previously mapped to the same bucket might be distributed across multiple buckets after expansion, thereby mitigating hash collisions.

      "},{"location":"chapter_heap/","title":"Chapter 8. \u00a0 Heap","text":"

      Abstract

      Heaps are like mountain peaks, layered and undulating, each with its unique form.

      The peaks rise and fall at varying heights, yet the tallest peak always catches the eye first.

      "},{"location":"chapter_heap/#chapter-contents","title":"Chapter contents","text":"
      • 8.1 \u00a0 Heap
      • 8.2 \u00a0 Building a Heap
      • 8.3 \u00a0 Top-K Problem
      • 8.4 \u00a0 Summary
      "},{"location":"chapter_heap/build_heap/","title":"8.2 \u00a0 Heap Construction Operation","text":"

      In some cases, we want to build a heap using all elements of a list, and this process is called \"heap construction operation.\"

      "},{"location":"chapter_heap/build_heap/#821-implementing-with-element-insertion","title":"8.2.1 \u00a0 Implementing with Element Insertion","text":"

      We first create an empty heap, then iterate through the list, performing the \"element insertion operation\" on each element in sequence. This means adding the element to the bottom of the heap and then performing \"bottom-to-top\" heapify on that element.

      Each time an element is inserted into the heap, the heap's length increases by one. Since nodes are added to the binary tree sequentially from top to bottom, the heap is constructed \"from top to bottom.\"

      Given \\(n\\) elements, each element's insertion operation takes \\(O(\\log{n})\\) time, so the time complexity of this heap construction method is \\(O(n \\log n)\\).

      "},{"location":"chapter_heap/build_heap/#822-implementing-through-heapify-traversal","title":"8.2.2 \u00a0 Implementing Through Heapify Traversal","text":"

      In fact, we can implement a more efficient heap construction method in two steps.

      1. Add all elements of the list as-is to the heap, at which point the heap property is not yet satisfied.
      2. Traverse the heap in reverse order (reverse of level-order traversal), performing \"top-to-bottom heapify\" on each non-leaf node in sequence.

      After heapifying a node, the subtree rooted at that node becomes a valid sub-heap. Since we traverse in reverse order, the heap is constructed \"from bottom to top.\"

      The reason for choosing reverse order traversal is that it ensures the subtree below the current node is already a valid sub-heap, making the heapification of the current node effective.

      It's worth noting that since leaf nodes have no children, they are naturally valid sub-heaps and do not require heapification. As shown in the code below, the last non-leaf node is the parent of the last node; we start from it and traverse in reverse order to perform heapification:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
      def __init__(self, nums: list[int]):\n    \"\"\"Constructor, build heap based on input list\"\"\"\n    # Add list elements to heap as is\n    self.max_heap = nums\n    # Heapify all nodes except leaf nodes\n    for i in range(self.parent(self.size() - 1), -1, -1):\n        self.sift_down(i)\n
      my_heap.cpp
      /* Constructor, build heap based on input list */\nMaxHeap(vector<int> nums) {\n    // Add list elements to heap as is\n    maxHeap = nums;\n    // Heapify all nodes except leaf nodes\n    for (int i = parent(size() - 1); i >= 0; i--) {\n        siftDown(i);\n    }\n}\n
      my_heap.java
      /* Constructor, build heap based on input list */\nMaxHeap(List<Integer> nums) {\n    // Add list elements to heap as is\n    maxHeap = new ArrayList<>(nums);\n    // Heapify all nodes except leaf nodes\n    for (int i = parent(size() - 1); i >= 0; i--) {\n        siftDown(i);\n    }\n}\n
      my_heap.cs
      /* Constructor, build heap from input list */\nMaxHeap(IEnumerable<int> nums) {\n    // Add list elements to heap as is\n    maxHeap = new List<int>(nums);\n    // Heapify all nodes except leaf nodes\n    var size = Parent(this.Size() - 1);\n    for (int i = size; i >= 0; i--) {\n        SiftDown(i);\n    }\n}\n
      my_heap.go
      /* Constructor, build heap from slice */\nfunc newMaxHeap(nums []any) *maxHeap {\n    // Add list elements to heap as is\n    h := &maxHeap{data: nums}\n    for i := h.parent(len(h.data) - 1); i >= 0; i-- {\n        // Heapify all nodes except leaf nodes\n        h.siftDown(i)\n    }\n    return h\n}\n
      my_heap.swift
      /* Constructor, build heap based on input list */\ninit(nums: [Int]) {\n    // Add list elements to heap as is\n    maxHeap = nums\n    // Heapify all nodes except leaf nodes\n    for i in (0 ... parent(i: size() - 1)).reversed() {\n        siftDown(i: i)\n    }\n}\n
      my_heap.js
      /* Constructor, build empty heap or build heap from input list */\nconstructor(nums) {\n    // Add list elements to heap as is\n    this.#maxHeap = nums === undefined ? [] : [...nums];\n    // Heapify all nodes except leaf nodes\n    for (let i = this.#parent(this.size() - 1); i >= 0; i--) {\n        this.#siftDown(i);\n    }\n}\n
      my_heap.ts
      /* Constructor, build empty heap or build heap from input list */\nconstructor(nums?: number[]) {\n    // Add list elements to heap as is\n    this.maxHeap = nums === undefined ? [] : [...nums];\n    // Heapify all nodes except leaf nodes\n    for (let i = this.parent(this.size() - 1); i >= 0; i--) {\n        this.siftDown(i);\n    }\n}\n
      my_heap.dart
      /* Constructor, build heap based on input list */\nMaxHeap(List<int> nums) {\n  // Add list elements to heap as is\n  _maxHeap = nums;\n  // Heapify all nodes except leaf nodes\n  for (int i = _parent(size() - 1); i >= 0; i--) {\n    siftDown(i);\n  }\n}\n
      my_heap.rs
      /* Constructor, build heap based on input list */\nfn new(nums: Vec<i32>) -> Self {\n    // Add list elements to heap as is\n    let mut heap = MaxHeap { max_heap: nums };\n    // Heapify all nodes except leaf nodes\n    for i in (0..=Self::parent(heap.size() - 1)).rev() {\n        heap.sift_down(i);\n    }\n    heap\n}\n
      my_heap.c
      /* Constructor, build heap from slice */\nMaxHeap *newMaxHeap(int nums[], int size) {\n    // Push all elements to heap\n    MaxHeap *maxHeap = (MaxHeap *)malloc(sizeof(MaxHeap));\n    maxHeap->size = size;\n    memcpy(maxHeap->data, nums, size * sizeof(int));\n    for (int i = parent(maxHeap, size - 1); i >= 0; i--) {\n        // Heapify all nodes except leaf nodes\n        siftDown(maxHeap, i);\n    }\n    return maxHeap;\n}\n
      my_heap.kt
      /* Max heap */\nclass MaxHeap(nums: MutableList<Int>?) {\n    // Use list instead of array, no need to consider capacity expansion\n    private val maxHeap = mutableListOf<Int>()\n\n    /* Constructor, build heap based on input list */\n    init {\n        // Add list elements to heap as is\n        maxHeap.addAll(nums!!)\n        // Heapify all nodes except leaf nodes\n        for (i in parent(size() - 1) downTo 0) {\n            siftDown(i)\n        }\n    }\n\n    /* Get index of left child node */\n    private fun left(i: Int): Int {\n        return 2 * i + 1\n    }\n\n    /* Get index of right child node */\n    private fun right(i: Int): Int {\n        return 2 * i + 2\n    }\n\n    /* Get index of parent node */\n    private fun parent(i: Int): Int {\n        return (i - 1) / 2 // Floor division\n    }\n\n    /* Swap elements */\n    private fun swap(i: Int, j: Int) {\n        val temp = maxHeap[i]\n        maxHeap[i] = maxHeap[j]\n        maxHeap[j] = temp\n    }\n\n    /* Get heap size */\n    fun size(): Int {\n        return maxHeap.size\n    }\n\n    /* Check if heap is empty */\n    fun isEmpty(): Boolean {\n        /* Check if heap is empty */\n        return size() == 0\n    }\n\n    /* Access top element */\n    fun peek(): Int {\n        return maxHeap[0]\n    }\n\n    /* Element enters heap */\n    fun push(_val: Int) {\n        // Add node\n        maxHeap.add(_val)\n        // Heapify from bottom to top\n        siftUp(size() - 1)\n    }\n\n    /* Starting from node i, heapify from bottom to top */\n    private fun siftUp(it: Int) {\n        // Kotlin function parameters are immutable, so create temporary variable\n        var i = it\n        while (true) {\n            // Get parent node of node i\n            val p = parent(i)\n            // When \"crossing root node\" or \"node needs no repair\", end heapify\n            if (p < 0 || maxHeap[i] <= maxHeap[p]) break\n            // Swap two nodes\n            swap(i, p)\n            // Loop upward heapify\n            i = p\n        }\n    }\n\n    /* Element exits heap */\n    fun pop(): Int {\n        // Handle empty case\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        // Delete node\n        swap(0, size() - 1)\n        // Remove node\n        val _val = maxHeap.removeAt(size() - 1)\n        // Return top element\n        siftDown(0)\n        // Return heap top element\n        return _val\n    }\n\n    /* Starting from node i, heapify from top to bottom */\n    private fun siftDown(it: Int) {\n        // Kotlin function parameters are immutable, so create temporary variable\n        var i = it\n        while (true) {\n            // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n            val l = left(i)\n            val r = right(i)\n            var ma = i\n            if (l < size() && maxHeap[l] > maxHeap[ma]) ma = l\n            if (r < size() && maxHeap[r] > maxHeap[ma]) ma = r\n            // Swap two nodes\n            if (ma == i) break\n            // Swap two nodes\n            swap(i, ma)\n            // Loop downwards heapification\n            i = ma\n        }\n    }\n\n    /* Driver Code */\n    fun print() {\n        val queue = PriorityQueue { a: Int, b: Int -> b - a }\n        queue.addAll(maxHeap)\n        printHeap(queue)\n    }\n}\n
      my_heap.rb
      ### Constructor, build heap from input list ###\ndef initialize(nums)\n  # Add list elements to heap as is\n  @max_heap = nums\n  # Heapify all nodes except leaf nodes\n  parent(size - 1).downto(0) do |i|\n    sift_down(i)\n  end\nend\n
      "},{"location":"chapter_heap/build_heap/#823-complexity-analysis","title":"8.2.3 \u00a0 Complexity Analysis","text":"

      Next, let's attempt to derive the time complexity of this second heap construction method.

      • Assuming the complete binary tree has \\(n\\) nodes, then the number of leaf nodes is \\((n + 1) / 2\\), where \\(/\\) is floor division. Therefore, the number of nodes that need heapification is \\((n - 1) / 2\\).
      • In the top-to-bottom heapify process, each node is heapified at most to the leaf nodes, so the maximum number of iterations is the binary tree height \\(\\log n\\).

      Multiplying these two together, we get a time complexity of \\(O(n \\log n)\\) for the heap construction process. However, this estimate is not accurate because it doesn't account for the property that binary trees have far more nodes at lower levels than at upper levels.

      Let's perform a more accurate calculation. To reduce calculation difficulty, assume a \"perfect binary tree\" with \\(n\\) nodes and height \\(h\\); this assumption does not affect the correctness of the result.

      Figure 8-5 \u00a0 Node count at each level of a perfect binary tree

      As shown in Figure 8-5, the maximum number of iterations for a node's \"top-to-bottom heapify\" equals the distance from that node to the leaf nodes, which is precisely the \"node height.\" Therefore, we can sum the \"number of nodes \\(\\times\\) node height\" at each level to obtain the total number of heapify iterations for all nodes.

      \\[ T(h) = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{(h-1)}\\times1 \\]

      To simplify the above expression, we need to use sequence knowledge from high school. First, multiply \\(T(h)\\) by \\(2\\) to get:

      \\[ \\begin{aligned} T(h) & = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{h-1}\\times1 \\newline 2 T(h) & = 2^1h + 2^2(h-1) + 2^3(h-2) + \\dots + 2^{h}\\times1 \\newline \\end{aligned} \\]

      Using the method of differences, subtract the first equation \\(T(h)\\) from the second equation \\(2 T(h)\\) to get:

      \\[ 2T(h) - T(h) = T(h) = -2^0h + 2^1 + 2^2 + \\dots + 2^{h-1} + 2^h \\]

      Observing the above expression, we find that \\(T(h)\\) is a geometric series, which can be calculated directly using the sum formula, yielding a time complexity of:

      \\[ \\begin{aligned} T(h) & = 2 \\frac{1 - 2^h}{1 - 2} - h \\newline & = 2^{h+1} - h - 2 \\newline & = O(2^h) \\end{aligned} \\]

      Furthermore, a perfect binary tree with height \\(h\\) has \\(n = 2^{h+1} - 1\\) nodes, so the complexity is \\(O(2^h) = O(n)\\). This derivation shows that the time complexity of building a heap from an input list is \\(O(n)\\), which is highly efficient.

      "},{"location":"chapter_heap/heap/","title":"8.1 \u00a0 Heap","text":"

      A heap is a complete binary tree that satisfies specific conditions and can be mainly categorized into two types, as shown in Figure 8-1.

      • min heap: The value of any node \\(\\leq\\) the values of its child nodes.
      • max heap: The value of any node \\(\\geq\\) the values of its child nodes.

      Figure 8-1 \u00a0 Min heap and max heap

      As a special case of a complete binary tree, heaps have the following characteristics.

      • The bottom layer nodes are filled from left to right, and nodes in other layers are fully filled.
      • We call the root node of the binary tree the \"heap top\" and the bottom-rightmost node the \"heap bottom.\"
      • For max heaps (min heaps), the value of the heap top element (root node) is the largest (smallest).
      "},{"location":"chapter_heap/heap/#811-common-heap-operations","title":"8.1.1 \u00a0 Common Heap Operations","text":"

      It should be noted that many programming languages provide a priority queue, which is an abstract data structure defined as a queue with priority sorting.

      In fact, heaps are typically used to implement priority queues, with max heaps corresponding to priority queues where elements are dequeued in descending order. From a usage perspective, we can regard \"priority queue\" and \"heap\" as equivalent data structures. Therefore, this book does not make a special distinction between the two and uniformly refers to them as \"heap.\"

      Common heap operations are shown in Table 8-1, and method names need to be determined based on the programming language.

      Table 8-1 \u00a0 Efficiency of Heap Operations

      Method name Description Time complexity push() Insert an element into the heap \\(O(\\log n)\\) pop() Remove the heap top element \\(O(\\log n)\\) peek() Access the heap top element (max/min value for max/min heap) \\(O(1)\\) size() Get the number of elements in the heap \\(O(1)\\) isEmpty() Check if the heap is empty \\(O(1)\\)

      In practical applications, we can directly use the heap class (or priority queue class) provided by programming languages.

      Similar to \"ascending order\" and \"descending order\" in sorting algorithms, we can implement conversion between \"min heap\" and \"max heap\" by setting a flag or modifying the Comparator. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby heap.py
      # Initialize a min heap\nmin_heap, flag = [], 1\n# Initialize a max heap\nmax_heap, flag = [], -1\n\n# Python's heapq module implements a min heap by default\n# Consider negating elements before pushing them to the heap, which inverts the size relationship and thus implements a max heap\n# In this example, flag = 1 corresponds to a min heap, flag = -1 corresponds to a max heap\n\n# Push elements into the heap\nheapq.heappush(max_heap, flag * 1)\nheapq.heappush(max_heap, flag * 3)\nheapq.heappush(max_heap, flag * 2)\nheapq.heappush(max_heap, flag * 5)\nheapq.heappush(max_heap, flag * 4)\n\n# Get the heap top element\npeek: int = flag * max_heap[0] # 5\n\n# Remove the heap top element\n# The removed elements will form a descending sequence\nval = flag * heapq.heappop(max_heap) # 5\nval = flag * heapq.heappop(max_heap) # 4\nval = flag * heapq.heappop(max_heap) # 3\nval = flag * heapq.heappop(max_heap) # 2\nval = flag * heapq.heappop(max_heap) # 1\n\n# Get the heap size\nsize: int = len(max_heap)\n\n# Check if the heap is empty\nis_empty: bool = not max_heap\n\n# Build a heap from an input list\nmin_heap: list[int] = [1, 3, 2, 5, 4]\nheapq.heapify(min_heap)\n
      heap.cpp
      /* Initialize a heap */\n// Initialize a min heap\npriority_queue<int, vector<int>, greater<int>> minHeap;\n// Initialize a max heap\npriority_queue<int, vector<int>, less<int>> maxHeap;\n\n/* Push elements into the heap */\nmaxHeap.push(1);\nmaxHeap.push(3);\nmaxHeap.push(2);\nmaxHeap.push(5);\nmaxHeap.push(4);\n\n/* Get the heap top element */\nint peek = maxHeap.top(); // 5\n\n/* Remove the heap top element */\n// The removed elements will form a descending sequence\nmaxHeap.pop(); // 5\nmaxHeap.pop(); // 4\nmaxHeap.pop(); // 3\nmaxHeap.pop(); // 2\nmaxHeap.pop(); // 1\n\n/* Get the heap size */\nint size = maxHeap.size();\n\n/* Check if the heap is empty */\nbool isEmpty = maxHeap.empty();\n\n/* Build a heap from an input list */\nvector<int> input{1, 3, 2, 5, 4};\npriority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());\n
      heap.java
      /* Initialize a heap */\n// Initialize a min heap\nQueue<Integer> minHeap = new PriorityQueue<>();\n// Initialize a max heap (use lambda expression to modify Comparator)\nQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);\n\n/* Push elements into the heap */\nmaxHeap.offer(1);\nmaxHeap.offer(3);\nmaxHeap.offer(2);\nmaxHeap.offer(5);\nmaxHeap.offer(4);\n\n/* Get the heap top element */\nint peek = maxHeap.peek(); // 5\n\n/* Remove the heap top element */\n// The removed elements will form a descending sequence\npeek = maxHeap.poll(); // 5\npeek = maxHeap.poll(); // 4\npeek = maxHeap.poll(); // 3\npeek = maxHeap.poll(); // 2\npeek = maxHeap.poll(); // 1\n\n/* Get the heap size */\nint size = maxHeap.size();\n\n/* Check if the heap is empty */\nboolean isEmpty = maxHeap.isEmpty();\n\n/* Build a heap from an input list */\nminHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));\n
      heap.cs
      /* Initialize a heap */\n// Initialize a min heap\nPriorityQueue<int, int> minHeap = new();\n// Initialize a max heap (use lambda expression to modify Comparer)\nPriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y.CompareTo(x)));\n\n/* Push elements into the heap */\nmaxHeap.Enqueue(1, 1);\nmaxHeap.Enqueue(3, 3);\nmaxHeap.Enqueue(2, 2);\nmaxHeap.Enqueue(5, 5);\nmaxHeap.Enqueue(4, 4);\n\n/* Get the heap top element */\nint peek = maxHeap.Peek();//5\n\n/* Remove the heap top element */\n// The removed elements will form a descending sequence\npeek = maxHeap.Dequeue();  // 5\npeek = maxHeap.Dequeue();  // 4\npeek = maxHeap.Dequeue();  // 3\npeek = maxHeap.Dequeue();  // 2\npeek = maxHeap.Dequeue();  // 1\n\n/* Get the heap size */\nint size = maxHeap.Count;\n\n/* Check if the heap is empty */\nbool isEmpty = maxHeap.Count == 0;\n\n/* Build a heap from an input list */\nminHeap = new PriorityQueue<int, int>([(1, 1), (3, 3), (2, 2), (5, 5), (4, 4)]);\n
      heap.go
      // In Go, we can construct a max heap of integers by implementing heap.Interface\n// Implementing heap.Interface also requires implementing sort.Interface\ntype intHeap []any\n\n// Push implements the heap.Interface method for pushing an element into the heap\nfunc (h *intHeap) Push(x any) {\n    // Push and Pop use pointer receiver as parameters\n    // because they not only adjust the slice contents but also modify the slice length\n    *h = append(*h, x.(int))\n}\n\n// Pop implements the heap.Interface method for popping the heap top element\nfunc (h *intHeap) Pop() any {\n    // The element to be removed is stored at the end\n    last := (*h)[len(*h)-1]\n    *h = (*h)[:len(*h)-1]\n    return last\n}\n\n// Len is a sort.Interface method\nfunc (h *intHeap) Len() int {\n    return len(*h)\n}\n\n// Less is a sort.Interface method\nfunc (h *intHeap) Less(i, j int) bool {\n    // To implement a min heap, change this to a less-than sign\n    return (*h)[i].(int) > (*h)[j].(int)\n}\n\n// Swap is a sort.Interface method\nfunc (h *intHeap) Swap(i, j int) {\n    (*h)[i], (*h)[j] = (*h)[j], (*h)[i]\n}\n\n// Top gets the heap top element\nfunc (h *intHeap) Top() any {\n    return (*h)[0]\n}\n\n/* Driver Code */\nfunc TestHeap(t *testing.T) {\n    /* Initialize a heap */\n    // Initialize a max heap\n    maxHeap := &intHeap{}\n    heap.Init(maxHeap)\n    /* Push elements into the heap */\n    // Call heap.Interface methods to add elements\n    heap.Push(maxHeap, 1)\n    heap.Push(maxHeap, 3)\n    heap.Push(maxHeap, 2)\n    heap.Push(maxHeap, 4)\n    heap.Push(maxHeap, 5)\n\n    /* Get the heap top element */\n    top := maxHeap.Top()\n    fmt.Printf(\"Heap top element is %d\\n\", top)\n\n    /* Remove the heap top element */\n    // Call heap.Interface methods to remove elements\n    heap.Pop(maxHeap) // 5\n    heap.Pop(maxHeap) // 4\n    heap.Pop(maxHeap) // 3\n    heap.Pop(maxHeap) // 2\n    heap.Pop(maxHeap) // 1\n\n    /* Get the heap size */\n    size := len(*maxHeap)\n    fmt.Printf(\"Number of heap elements is %d\\n\", size)\n\n    /* Check if the heap is empty */\n    isEmpty := len(*maxHeap) == 0\n    fmt.Printf(\"Is the heap empty? %t\\n\", isEmpty)\n}\n
      heap.swift
      /* Initialize a heap */\n// Swift's Heap type supports both max heaps and min heaps, and requires importing swift-collections\nvar heap = Heap<Int>()\n\n/* Push elements into the heap */\nheap.insert(1)\nheap.insert(3)\nheap.insert(2)\nheap.insert(5)\nheap.insert(4)\n\n/* Get the heap top element */\nvar peek = heap.max()!\n\n/* Remove the heap top element */\npeek = heap.removeMax() // 5\npeek = heap.removeMax() // 4\npeek = heap.removeMax() // 3\npeek = heap.removeMax() // 2\npeek = heap.removeMax() // 1\n\n/* Get the heap size */\nlet size = heap.count\n\n/* Check if the heap is empty */\nlet isEmpty = heap.isEmpty\n\n/* Build a heap from an input list */\nlet heap2 = Heap([1, 3, 2, 5, 4])\n
      heap.js
      // JavaScript does not provide a built-in Heap class\n
      heap.ts
      // TypeScript does not provide a built-in Heap class\n
      heap.dart
      // Dart does not provide a built-in Heap class\n
      heap.rs
      use std::collections::BinaryHeap;\nuse std::cmp::Reverse;\n\n/* Initialize a heap */\n// Initialize a min heap\nlet mut min_heap = BinaryHeap::<Reverse<i32>>::new();\n// Initialize a max heap\nlet mut max_heap = BinaryHeap::new();\n\n/* Push elements into the heap */\nmax_heap.push(1);\nmax_heap.push(3);\nmax_heap.push(2);\nmax_heap.push(5);\nmax_heap.push(4);\n\n/* Get the heap top element */\nlet peek = max_heap.peek().unwrap();  // 5\n\n/* Remove the heap top element */\n// The removed elements will form a descending sequence\nlet peek = max_heap.pop().unwrap();   // 5\nlet peek = max_heap.pop().unwrap();   // 4\nlet peek = max_heap.pop().unwrap();   // 3\nlet peek = max_heap.pop().unwrap();   // 2\nlet peek = max_heap.pop().unwrap();   // 1\n\n/* Get the heap size */\nlet size = max_heap.len();\n\n/* Check if the heap is empty */\nlet is_empty = max_heap.is_empty();\n\n/* Build a heap from an input list */\nlet min_heap = BinaryHeap::from(vec![Reverse(1), Reverse(3), Reverse(2), Reverse(5), Reverse(4)]);\n
      heap.c
      // C does not provide a built-in Heap class\n
      heap.kt
      /* Initialize a heap */\n// Initialize a min heap\nvar minHeap = PriorityQueue<Int>()\n// Initialize a max heap (use lambda expression to modify Comparator)\nval maxHeap = PriorityQueue { a: Int, b: Int -> b - a }\n\n/* Push elements into the heap */\nmaxHeap.offer(1)\nmaxHeap.offer(3)\nmaxHeap.offer(2)\nmaxHeap.offer(5)\nmaxHeap.offer(4)\n\n/* Get the heap top element */\nvar peek = maxHeap.peek() // 5\n\n/* Remove the heap top element */\n// The removed elements will form a descending sequence\npeek = maxHeap.poll() // 5\npeek = maxHeap.poll() // 4\npeek = maxHeap.poll() // 3\npeek = maxHeap.poll() // 2\npeek = maxHeap.poll() // 1\n\n/* Get the heap size */\nval size = maxHeap.size\n\n/* Check if the heap is empty */\nval isEmpty = maxHeap.isEmpty()\n\n/* Build a heap from an input list */\nminHeap = PriorityQueue(mutableListOf(1, 3, 2, 5, 4))\n
      heap.rb
      # Ruby does not provide a built-in Heap class\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_heap/heap/#812-implementation-of-the-heap","title":"8.1.2 \u00a0 Implementation of the Heap","text":"

      The following implementation is of a max heap. To convert it to a min heap, simply invert all size logic comparisons (for example, replace \\(\\geq\\) with \\(\\leq\\)). Interested readers are encouraged to implement this on their own.

      "},{"location":"chapter_heap/heap/#1-heap-storage-and-representation","title":"1. \u00a0 Heap Storage and Representation","text":"

      As mentioned in the \"Binary Tree\" chapter, complete binary trees are well-suited for array representation. Since heaps are a type of complete binary tree, we will use arrays to store heaps.

      When representing a binary tree with an array, elements represent node values, and indexes represent node positions in the binary tree. Node pointers are implemented through index mapping formulas.

      As shown in Figure 8-2, given an index \\(i\\), the index of its left child is \\(2i + 1\\), the index of its right child is \\(2i + 2\\), and the index of its parent is \\((i - 1) / 2\\) (floor division). When an index is out of bounds, it indicates a null node or that the node does not exist.

      Figure 8-2 \u00a0 Representation and storage of heaps

      We can encapsulate the index mapping formula into functions for convenient subsequent use:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
      def left(self, i: int) -> int:\n    \"\"\"Get index of left child node\"\"\"\n    return 2 * i + 1\n\ndef right(self, i: int) -> int:\n    \"\"\"Get index of right child node\"\"\"\n    return 2 * i + 2\n\ndef parent(self, i: int) -> int:\n    \"\"\"Get index of parent node\"\"\"\n    return (i - 1) // 2  # Floor division\n
      my_heap.cpp
      /* Get index of left child node */\nint left(int i) {\n    return 2 * i + 1;\n}\n\n/* Get index of right child node */\nint right(int i) {\n    return 2 * i + 2;\n}\n\n/* Get index of parent node */\nint parent(int i) {\n    return (i - 1) / 2; // Floor division\n}\n
      my_heap.java
      /* Get index of left child node */\nint left(int i) {\n    return 2 * i + 1;\n}\n\n/* Get index of right child node */\nint right(int i) {\n    return 2 * i + 2;\n}\n\n/* Get index of parent node */\nint parent(int i) {\n    return (i - 1) / 2; // Floor division\n}\n
      my_heap.cs
      /* Get index of left child node */\nint Left(int i) {\n    return 2 * i + 1;\n}\n\n/* Get index of right child node */\nint Right(int i) {\n    return 2 * i + 2;\n}\n\n/* Get index of parent node */\nint Parent(int i) {\n    return (i - 1) / 2; // Floor division\n}\n
      my_heap.go
      /* Get index of left child node */\nfunc (h *maxHeap) left(i int) int {\n    return 2*i + 1\n}\n\n/* Get index of right child node */\nfunc (h *maxHeap) right(i int) int {\n    return 2*i + 2\n}\n\n/* Get index of parent node */\nfunc (h *maxHeap) parent(i int) int {\n    // Floor division\n    return (i - 1) / 2\n}\n
      my_heap.swift
      /* Get index of left child node */\nfunc left(i: Int) -> Int {\n    2 * i + 1\n}\n\n/* Get index of right child node */\nfunc right(i: Int) -> Int {\n    2 * i + 2\n}\n\n/* Get index of parent node */\nfunc parent(i: Int) -> Int {\n    (i - 1) / 2 // Floor division\n}\n
      my_heap.js
      /* Get index of left child node */\n#left(i) {\n    return 2 * i + 1;\n}\n\n/* Get index of right child node */\n#right(i) {\n    return 2 * i + 2;\n}\n\n/* Get index of parent node */\n#parent(i) {\n    return Math.floor((i - 1) / 2); // Floor division\n}\n
      my_heap.ts
      /* Get index of left child node */\nleft(i: number): number {\n    return 2 * i + 1;\n}\n\n/* Get index of right child node */\nright(i: number): number {\n    return 2 * i + 2;\n}\n\n/* Get index of parent node */\nparent(i: number): number {\n    return Math.floor((i - 1) / 2); // Floor division\n}\n
      my_heap.dart
      /* Get index of left child node */\nint _left(int i) {\n  return 2 * i + 1;\n}\n\n/* Get index of right child node */\nint _right(int i) {\n  return 2 * i + 2;\n}\n\n/* Get index of parent node */\nint _parent(int i) {\n  return (i - 1) ~/ 2; // Floor division\n}\n
      my_heap.rs
      /* Get index of left child node */\nfn left(i: usize) -> usize {\n    2 * i + 1\n}\n\n/* Get index of right child node */\nfn right(i: usize) -> usize {\n    2 * i + 2\n}\n\n/* Get index of parent node */\nfn parent(i: usize) -> usize {\n    (i - 1) / 2 // Floor division\n}\n
      my_heap.c
      /* Get index of left child node */\nint left(MaxHeap *maxHeap, int i) {\n    return 2 * i + 1;\n}\n\n/* Get index of right child node */\nint right(MaxHeap *maxHeap, int i) {\n    return 2 * i + 2;\n}\n\n/* Get index of parent node */\nint parent(MaxHeap *maxHeap, int i) {\n    return (i - 1) / 2; // Round down\n}\n
      my_heap.kt
      /* Get index of left child node */\nfun left(i: Int): Int {\n    return 2 * i + 1\n}\n\n/* Get index of right child node */\nfun right(i: Int): Int {\n    return 2 * i + 2\n}\n\n/* Get index of parent node */\nfun parent(i: Int): Int {\n    return (i - 1) / 2 // Floor division\n}\n
      my_heap.rb
      ### Get left child index ###\ndef left(i)\n  2 * i + 1\nend\n\n### Get right child index ###\ndef right(i)\n  2 * i + 2\nend\n\n### Get parent node index ###\ndef parent(i)\n  (i - 1) / 2     # Floor division\nend\n
      "},{"location":"chapter_heap/heap/#2-accessing-the-heap-top-element","title":"2. \u00a0 Accessing the Heap Top Element","text":"

      The heap top element is the root node of the binary tree, which is also the first element of the list:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
      def peek(self) -> int:\n    \"\"\"Access top element\"\"\"\n    return self.max_heap[0]\n
      my_heap.cpp
      /* Access top element */\nint peek() {\n    return maxHeap[0];\n}\n
      my_heap.java
      /* Access top element */\nint peek() {\n    return maxHeap.get(0);\n}\n
      my_heap.cs
      /* Access top element */\nint Peek() {\n    return maxHeap[0];\n}\n
      my_heap.go
      /* Access top element */\nfunc (h *maxHeap) peek() any {\n    return h.data[0]\n}\n
      my_heap.swift
      /* Access top element */\nfunc peek() -> Int {\n    maxHeap[0]\n}\n
      my_heap.js
      /* Access top element */\npeek() {\n    return this.#maxHeap[0];\n}\n
      my_heap.ts
      /* Access top element */\npeek(): number {\n    return this.maxHeap[0];\n}\n
      my_heap.dart
      /* Access top element */\nint peek() {\n  return _maxHeap[0];\n}\n
      my_heap.rs
      /* Access top element */\nfn peek(&self) -> Option<i32> {\n    self.max_heap.first().copied()\n}\n
      my_heap.c
      /* Access top element */\nint peek(MaxHeap *maxHeap) {\n    return maxHeap->data[0];\n}\n
      my_heap.kt
      /* Access top element */\nfun peek(): Int {\n    return maxHeap[0]\n}\n
      my_heap.rb
      ### Access heap top element ###\ndef peek\n  @max_heap[0]\nend\n
      "},{"location":"chapter_heap/heap/#3-inserting-an-element-into-the-heap","title":"3. \u00a0 Inserting an Element Into the Heap","text":"

      Given an element val, we first add it to the bottom of the heap. After addition, since val may be larger than other elements in the heap, the heap's property may be violated. Therefore, it's necessary to repair the path from the inserted node to the root node. This operation is called heapify.

      Starting from the inserted node, perform heapify from bottom to top. As shown in Figure 8-3, we compare the inserted node with its parent node, and if the inserted node is larger, swap them. Then continue this operation, repairing nodes in the heap from bottom to top until we pass the root node or encounter a node that does not need swapping.

      <1><2><3><4><5><6><7><8><9>

      Figure 8-3 \u00a0 Steps of inserting an element into the heap

      Given a total of \\(n\\) nodes, the tree height is \\(O(\\log n)\\). Thus, the number of loop iterations in the heapify operation is at most \\(O(\\log n)\\), making the time complexity of the element insertion operation \\(O(\\log n)\\). The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
      def push(self, val: int):\n    \"\"\"Element enters heap\"\"\"\n    # Add node\n    self.max_heap.append(val)\n    # Heapify from bottom to top\n    self.sift_up(self.size() - 1)\n\ndef sift_up(self, i: int):\n    \"\"\"Starting from node i, heapify from bottom to top\"\"\"\n    while True:\n        # Get parent node of node i\n        p = self.parent(i)\n        # When \"crossing root node\" or \"node needs no repair\", end heapify\n        if p < 0 or self.max_heap[i] <= self.max_heap[p]:\n            break\n        # Swap two nodes\n        self.swap(i, p)\n        # Loop upward heapify\n        i = p\n
      my_heap.cpp
      /* Element enters heap */\nvoid push(int val) {\n    // Add node\n    maxHeap.push_back(val);\n    // Heapify from bottom to top\n    siftUp(size() - 1);\n}\n\n/* Starting from node i, heapify from bottom to top */\nvoid siftUp(int i) {\n    while (true) {\n        // Get parent node of node i\n        int p = parent(i);\n        // When \"crossing root node\" or \"node needs no repair\", end heapify\n        if (p < 0 || maxHeap[i] <= maxHeap[p])\n            break;\n        // Swap two nodes\n        swap(maxHeap[i], maxHeap[p]);\n        // Loop upward heapify\n        i = p;\n    }\n}\n
      my_heap.java
      /* Element enters heap */\nvoid push(int val) {\n    // Add node\n    maxHeap.add(val);\n    // Heapify from bottom to top\n    siftUp(size() - 1);\n}\n\n/* Starting from node i, heapify from bottom to top */\nvoid siftUp(int i) {\n    while (true) {\n        // Get parent node of node i\n        int p = parent(i);\n        // When \"crossing root node\" or \"node needs no repair\", end heapify\n        if (p < 0 || maxHeap.get(i) <= maxHeap.get(p))\n            break;\n        // Swap two nodes\n        swap(i, p);\n        // Loop upward heapify\n        i = p;\n    }\n}\n
      my_heap.cs
      /* Element enters heap */\nvoid Push(int val) {\n    // Add node\n    maxHeap.Add(val);\n    // Heapify from bottom to top\n    SiftUp(Size() - 1);\n}\n\n/* Starting from node i, heapify from bottom to top */\nvoid SiftUp(int i) {\n    while (true) {\n        // Get parent node of node i\n        int p = Parent(i);\n        // If 'past root node' or 'node needs no repair', end heapify\n        if (p < 0 || maxHeap[i] <= maxHeap[p])\n            break;\n        // Swap two nodes\n        Swap(i, p);\n        // Loop upward heapify\n        i = p;\n    }\n}\n
      my_heap.go
      /* Element enters heap */\nfunc (h *maxHeap) push(val any) {\n    // Add node\n    h.data = append(h.data, val)\n    // Heapify from bottom to top\n    h.siftUp(len(h.data) - 1)\n}\n\n/* Starting from node i, heapify from bottom to top */\nfunc (h *maxHeap) siftUp(i int) {\n    for true {\n        // Get parent node of node i\n        p := h.parent(i)\n        // When \"crossing root node\" or \"node needs no repair\", end heapify\n        if p < 0 || h.data[i].(int) <= h.data[p].(int) {\n            break\n        }\n        // Swap two nodes\n        h.swap(i, p)\n        // Loop upward heapify\n        i = p\n    }\n}\n
      my_heap.swift
      /* Element enters heap */\nfunc push(val: Int) {\n    // Add node\n    maxHeap.append(val)\n    // Heapify from bottom to top\n    siftUp(i: size() - 1)\n}\n\n/* Starting from node i, heapify from bottom to top */\nfunc siftUp(i: Int) {\n    var i = i\n    while true {\n        // Get parent node of node i\n        let p = parent(i: i)\n        // When \"crossing root node\" or \"node needs no repair\", end heapify\n        if p < 0 || maxHeap[i] <= maxHeap[p] {\n            break\n        }\n        // Swap two nodes\n        swap(i: i, j: p)\n        // Loop upward heapify\n        i = p\n    }\n}\n
      my_heap.js
      /* Element enters heap */\npush(val) {\n    // Add node\n    this.#maxHeap.push(val);\n    // Heapify from bottom to top\n    this.#siftUp(this.size() - 1);\n}\n\n/* Starting from node i, heapify from bottom to top */\n#siftUp(i) {\n    while (true) {\n        // Get parent node of node i\n        const p = this.#parent(i);\n        // When \"crossing root node\" or \"node needs no repair\", end heapify\n        if (p < 0 || this.#maxHeap[i] <= this.#maxHeap[p]) break;\n        // Swap two nodes\n        this.#swap(i, p);\n        // Loop upward heapify\n        i = p;\n    }\n}\n
      my_heap.ts
      /* Element enters heap */\npush(val: number): void {\n    // Add node\n    this.maxHeap.push(val);\n    // Heapify from bottom to top\n    this.siftUp(this.size() - 1);\n}\n\n/* Starting from node i, heapify from bottom to top */\nsiftUp(i: number): void {\n    while (true) {\n        // Get parent node of node i\n        const p = this.parent(i);\n        // When \"crossing root node\" or \"node needs no repair\", end heapify\n        if (p < 0 || this.maxHeap[i] <= this.maxHeap[p]) break;\n        // Swap two nodes\n        this.swap(i, p);\n        // Loop upward heapify\n        i = p;\n    }\n}\n
      my_heap.dart
      /* Element enters heap */\nvoid push(int val) {\n  // Add node\n  _maxHeap.add(val);\n  // Heapify from bottom to top\n  siftUp(size() - 1);\n}\n\n/* Starting from node i, heapify from bottom to top */\nvoid siftUp(int i) {\n  while (true) {\n    // Get parent node of node i\n    int p = _parent(i);\n    // When \"crossing root node\" or \"node needs no repair\", end heapify\n    if (p < 0 || _maxHeap[i] <= _maxHeap[p]) {\n      break;\n    }\n    // Swap two nodes\n    _swap(i, p);\n    // Loop upward heapify\n    i = p;\n  }\n}\n
      my_heap.rs
      /* Element enters heap */\nfn push(&mut self, val: i32) {\n    // Add node\n    self.max_heap.push(val);\n    // Heapify from bottom to top\n    self.sift_up(self.size() - 1);\n}\n\n/* Starting from node i, heapify from bottom to top */\nfn sift_up(&mut self, mut i: usize) {\n    loop {\n        // Node i is already the heap root, end heapification\n        if i == 0 {\n            break;\n        }\n        // Get parent node of node i\n        let p = Self::parent(i);\n        // When \"node needs no repair\", end heapification\n        if self.max_heap[i] <= self.max_heap[p] {\n            break;\n        }\n        // Swap two nodes\n        self.swap(i, p);\n        // Loop upward heapify\n        i = p;\n    }\n}\n
      my_heap.c
      /* Element enters heap */\nvoid push(MaxHeap *maxHeap, int val) {\n    // By default, should not add this many nodes\n    if (maxHeap->size == MAX_SIZE) {\n        printf(\"heap is full!\");\n        return;\n    }\n    // Add node\n    maxHeap->data[maxHeap->size] = val;\n    maxHeap->size++;\n\n    // Heapify from bottom to top\n    siftUp(maxHeap, maxHeap->size - 1);\n}\n\n/* Starting from node i, heapify from bottom to top */\nvoid siftUp(MaxHeap *maxHeap, int i) {\n    while (true) {\n        // Get parent node of node i\n        int p = parent(maxHeap, i);\n        // When \"crossing root node\" or \"node needs no repair\", end heapify\n        if (p < 0 || maxHeap->data[i] <= maxHeap->data[p]) {\n            break;\n        }\n        // Swap two nodes\n        swap(maxHeap, i, p);\n        // Loop upward heapify\n        i = p;\n    }\n}\n
      my_heap.kt
      /* Element enters heap */\nfun push(_val: Int) {\n    // Add node\n    maxHeap.add(_val)\n    // Heapify from bottom to top\n    siftUp(size() - 1)\n}\n\n/* Starting from node i, heapify from bottom to top */\nfun siftUp(it: Int) {\n    // Kotlin function parameters are immutable, so create temporary variable\n    var i = it\n    while (true) {\n        // Get parent node of node i\n        val p = parent(i)\n        // When \"crossing root node\" or \"node needs no repair\", end heapify\n        if (p < 0 || maxHeap[i] <= maxHeap[p]) break\n        // Swap two nodes\n        swap(i, p)\n        // Loop upward heapify\n        i = p\n    }\n}\n
      my_heap.rb
      ### Push element to heap ###\ndef push(val)\n  # Add node\n  @max_heap << val\n  # Heapify from bottom to top\n  sift_up(size - 1)\nend\n\n### Heapify from node i, bottom to top ###\ndef sift_up(i)\n  loop do\n    # Get parent node of node i\n    p = parent(i)\n    # When \"crossing root node\" or \"node needs no repair\", end heapify\n    break if p < 0 || @max_heap[i] <= @max_heap[p]\n    # Swap two nodes\n    swap(i, p)\n    # Loop upward heapify\n    i = p\n  end\nend\n
      "},{"location":"chapter_heap/heap/#4-removing-the-heap-top-element","title":"4. \u00a0 Removing the Heap Top Element","text":"

      The heap top element is the root node of the binary tree, which is the first element of the list. If we directly remove the first element from the list, all node indexes in the binary tree would change, making subsequent repair with heapify difficult. To minimize changes in element indexes, we use the following steps.

      1. Swap the heap top element with the heap bottom element (swap the root node with the rightmost leaf node).
      2. After swapping, remove the heap bottom from the list (note that since we've swapped, we're actually removing the original heap top element).
      3. Starting from the root node, perform heapify from top to bottom.

      As shown in Figure 8-4, the direction of \"top-to-bottom heapify\" is opposite to \"bottom-to-top heapify\". We compare the root node's value with its two children and swap it with the largest child. Then loop this operation until we pass a leaf node or encounter a node that doesn't need swapping.

      <1><2><3><4><5><6><7><8><9><10>

      Figure 8-4 \u00a0 Steps of removing the heap top element

      Similar to the element insertion operation, the time complexity of the heap top element removal operation is also \\(O(\\log n)\\). The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
      def pop(self) -> int:\n    \"\"\"Element exits heap\"\"\"\n    # Handle empty case\n    if self.is_empty():\n        raise IndexError(\"Heap is empty\")\n    # Swap root node with rightmost leaf node (swap first element with last element)\n    self.swap(0, self.size() - 1)\n    # Delete node\n    val = self.max_heap.pop()\n    # Heapify from top to bottom\n    self.sift_down(0)\n    # Return top element\n    return val\n\ndef sift_down(self, i: int):\n    \"\"\"Starting from node i, heapify from top to bottom\"\"\"\n    while True:\n        # Find node with largest value among i, l, r, denoted as ma\n        l, r, ma = self.left(i), self.right(i), i\n        if l < self.size() and self.max_heap[l] > self.max_heap[ma]:\n            ma = l\n        if r < self.size() and self.max_heap[r] > self.max_heap[ma]:\n            ma = r\n        # If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        if ma == i:\n            break\n        # Swap two nodes\n        self.swap(i, ma)\n        # Loop downward heapify\n        i = ma\n
      my_heap.cpp
      /* Element exits heap */\nvoid pop() {\n    // Handle empty case\n    if (isEmpty()) {\n        throw out_of_range(\"Heap is empty\");\n    }\n    // Delete node\n    swap(maxHeap[0], maxHeap[size() - 1]);\n    // Remove node\n    maxHeap.pop_back();\n    // Return top element\n    siftDown(0);\n}\n\n/* Starting from node i, heapify from top to bottom */\nvoid siftDown(int i) {\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        int l = left(i), r = right(i), ma = i;\n        if (l < size() && maxHeap[l] > maxHeap[ma])\n            ma = l;\n        if (r < size() && maxHeap[r] > maxHeap[ma])\n            ma = r;\n        // Swap two nodes\n        if (ma == i)\n            break;\n        swap(maxHeap[i], maxHeap[ma]);\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n
      my_heap.java
      /* Element exits heap */\nint pop() {\n    // Handle empty case\n    if (isEmpty())\n        throw new IndexOutOfBoundsException();\n    // Delete node\n    swap(0, size() - 1);\n    // Remove node\n    int val = maxHeap.remove(size() - 1);\n    // Return top element\n    siftDown(0);\n    // Return heap top element\n    return val;\n}\n\n/* Starting from node i, heapify from top to bottom */\nvoid siftDown(int i) {\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        int l = left(i), r = right(i), ma = i;\n        if (l < size() && maxHeap.get(l) > maxHeap.get(ma))\n            ma = l;\n        if (r < size() && maxHeap.get(r) > maxHeap.get(ma))\n            ma = r;\n        // Swap two nodes\n        if (ma == i)\n            break;\n        // Swap two nodes\n        swap(i, ma);\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n
      my_heap.cs
      /* Element exits heap */\nint Pop() {\n    // Handle empty case\n    if (IsEmpty())\n        throw new IndexOutOfRangeException();\n    // Delete node\n    Swap(0, Size() - 1);\n    // Remove node\n    int val = maxHeap.Last();\n    maxHeap.RemoveAt(Size() - 1);\n    // Return top element\n    SiftDown(0);\n    // Return heap top element\n    return val;\n}\n\n/* Starting from node i, heapify from top to bottom */\nvoid SiftDown(int i) {\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        int l = Left(i), r = Right(i), ma = i;\n        if (l < Size() && maxHeap[l] > maxHeap[ma])\n            ma = l;\n        if (r < Size() && maxHeap[r] > maxHeap[ma])\n            ma = r;\n        // If 'node i is largest' or 'past leaf node', end heapify\n        if (ma == i) break;\n        // Swap two nodes\n        Swap(i, ma);\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n
      my_heap.go
      /* Element exits heap */\nfunc (h *maxHeap) pop() any {\n    // Handle empty case\n    if h.isEmpty() {\n        fmt.Println(\"error\")\n        return nil\n    }\n    // Delete node\n    h.swap(0, h.size()-1)\n    // Remove node\n    val := h.data[len(h.data)-1]\n    h.data = h.data[:len(h.data)-1]\n    // Return top element\n    h.siftDown(0)\n\n    // Return heap top element\n    return val\n}\n\n/* Starting from node i, heapify from top to bottom */\nfunc (h *maxHeap) siftDown(i int) {\n    for true {\n        // Find node with maximum value among nodes i, l, r, denoted as max\n        l, r, max := h.left(i), h.right(i), i\n        if l < h.size() && h.data[l].(int) > h.data[max].(int) {\n            max = l\n        }\n        if r < h.size() && h.data[r].(int) > h.data[max].(int) {\n            max = r\n        }\n        // Swap two nodes\n        if max == i {\n            break\n        }\n        // Swap two nodes\n        h.swap(i, max)\n        // Loop downwards heapification\n        i = max\n    }\n}\n
      my_heap.swift
      /* Element exits heap */\nfunc pop() -> Int {\n    // Handle empty case\n    if isEmpty() {\n        fatalError(\"Heap is empty\")\n    }\n    // Delete node\n    swap(i: 0, j: size() - 1)\n    // Remove node\n    let val = maxHeap.remove(at: size() - 1)\n    // Return top element\n    siftDown(i: 0)\n    // Return heap top element\n    return val\n}\n\n/* Starting from node i, heapify from top to bottom */\nfunc siftDown(i: Int) {\n    var i = i\n    while true {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        let l = left(i: i)\n        let r = right(i: i)\n        var ma = i\n        if l < size(), maxHeap[l] > maxHeap[ma] {\n            ma = l\n        }\n        if r < size(), maxHeap[r] > maxHeap[ma] {\n            ma = r\n        }\n        // Swap two nodes\n        if ma == i {\n            break\n        }\n        // Swap two nodes\n        swap(i: i, j: ma)\n        // Loop downwards heapification\n        i = ma\n    }\n}\n
      my_heap.js
      /* Element exits heap */\npop() {\n    // Handle empty case\n    if (this.isEmpty()) throw new Error('Heap is empty');\n    // Delete node\n    this.#swap(0, this.size() - 1);\n    // Remove node\n    const val = this.#maxHeap.pop();\n    // Return top element\n    this.#siftDown(0);\n    // Return heap top element\n    return val;\n}\n\n/* Starting from node i, heapify from top to bottom */\n#siftDown(i) {\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        const l = this.#left(i),\n            r = this.#right(i);\n        let ma = i;\n        if (l < this.size() && this.#maxHeap[l] > this.#maxHeap[ma]) ma = l;\n        if (r < this.size() && this.#maxHeap[r] > this.#maxHeap[ma]) ma = r;\n        // Swap two nodes\n        if (ma === i) break;\n        // Swap two nodes\n        this.#swap(i, ma);\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n
      my_heap.ts
      /* Element exits heap */\npop(): number {\n    // Handle empty case\n    if (this.isEmpty()) throw new RangeError('Heap is empty.');\n    // Delete node\n    this.swap(0, this.size() - 1);\n    // Remove node\n    const val = this.maxHeap.pop();\n    // Return top element\n    this.siftDown(0);\n    // Return heap top element\n    return val;\n}\n\n/* Starting from node i, heapify from top to bottom */\nsiftDown(i: number): void {\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        const l = this.left(i),\n            r = this.right(i);\n        let ma = i;\n        if (l < this.size() && this.maxHeap[l] > this.maxHeap[ma]) ma = l;\n        if (r < this.size() && this.maxHeap[r] > this.maxHeap[ma]) ma = r;\n        // Swap two nodes\n        if (ma === i) break;\n        // Swap two nodes\n        this.swap(i, ma);\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n
      my_heap.dart
      /* Element exits heap */\nint pop() {\n  // Handle empty case\n  if (isEmpty()) throw Exception('Heap is empty');\n  // Delete node\n  _swap(0, size() - 1);\n  // Remove node\n  int val = _maxHeap.removeLast();\n  // Return top element\n  siftDown(0);\n  // Return heap top element\n  return val;\n}\n\n/* Starting from node i, heapify from top to bottom */\nvoid siftDown(int i) {\n  while (true) {\n    // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n    int l = _left(i);\n    int r = _right(i);\n    int ma = i;\n    if (l < size() && _maxHeap[l] > _maxHeap[ma]) ma = l;\n    if (r < size() && _maxHeap[r] > _maxHeap[ma]) ma = r;\n    // Swap two nodes\n    if (ma == i) break;\n    // Swap two nodes\n    _swap(i, ma);\n    // Loop downwards heapification\n    i = ma;\n  }\n}\n
      my_heap.rs
      /* Element exits heap */\nfn pop(&mut self) -> i32 {\n    // Handle empty case\n    if self.is_empty() {\n        panic!(\"index out of bounds\");\n    }\n    // Delete node\n    self.swap(0, self.size() - 1);\n    // Remove node\n    let val = self.max_heap.pop().unwrap();\n    // Return top element\n    self.sift_down(0);\n    // Return heap top element\n    val\n}\n\n/* Starting from node i, heapify from top to bottom */\nfn sift_down(&mut self, mut i: usize) {\n    loop {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        let (l, r, mut ma) = (Self::left(i), Self::right(i), i);\n        if l < self.size() && self.max_heap[l] > self.max_heap[ma] {\n            ma = l;\n        }\n        if r < self.size() && self.max_heap[r] > self.max_heap[ma] {\n            ma = r;\n        }\n        // Swap two nodes\n        if ma == i {\n            break;\n        }\n        // Swap two nodes\n        self.swap(i, ma);\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n
      my_heap.c
      /* Element exits heap */\nint pop(MaxHeap *maxHeap) {\n    // Handle empty case\n    if (isEmpty(maxHeap)) {\n        printf(\"heap is empty!\");\n        return INT_MAX;\n    }\n    // Delete node\n    swap(maxHeap, 0, size(maxHeap) - 1);\n    // Remove node\n    int val = maxHeap->data[maxHeap->size - 1];\n    maxHeap->size--;\n    // Return top element\n    siftDown(maxHeap, 0);\n\n    // Return heap top element\n    return val;\n}\n\n/* Starting from node i, heapify from top to bottom */\nvoid siftDown(MaxHeap *maxHeap, int i) {\n    while (true) {\n        // Find node with maximum value among nodes i, l, r, denoted as max\n        int l = left(maxHeap, i);\n        int r = right(maxHeap, i);\n        int max = i;\n        if (l < size(maxHeap) && maxHeap->data[l] > maxHeap->data[max]) {\n            max = l;\n        }\n        if (r < size(maxHeap) && maxHeap->data[r] > maxHeap->data[max]) {\n            max = r;\n        }\n        // Swap two nodes\n        if (max == i) {\n            break;\n        }\n        // Swap two nodes\n        swap(maxHeap, i, max);\n        // Loop downwards heapification\n        i = max;\n    }\n}\n
      my_heap.kt
      /* Element exits heap */\nfun pop(): Int {\n    // Handle empty case\n    if (isEmpty()) throw IndexOutOfBoundsException()\n    // Delete node\n    swap(0, size() - 1)\n    // Remove node\n    val _val = maxHeap.removeAt(size() - 1)\n    // Return top element\n    siftDown(0)\n    // Return heap top element\n    return _val\n}\n\n/* Starting from node i, heapify from top to bottom */\nfun siftDown(it: Int) {\n    // Kotlin function parameters are immutable, so create temporary variable\n    var i = it\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        val l = left(i)\n        val r = right(i)\n        var ma = i\n        if (l < size() && maxHeap[l] > maxHeap[ma]) ma = l\n        if (r < size() && maxHeap[r] > maxHeap[ma]) ma = r\n        // Swap two nodes\n        if (ma == i) break\n        // Swap two nodes\n        swap(i, ma)\n        // Loop downwards heapification\n        i = ma\n    }\n}\n
      my_heap.rb
      ### Pop element from heap ###\ndef pop\n  # Handle empty case\n  raise IndexError, \"Heap is empty\" if is_empty?\n  # Delete node\n  swap(0, size - 1)\n  # Remove node\n  val = @max_heap.pop\n  # Return top element\n  sift_down(0)\n  # Return heap top element\n  val\nend\n\n### Heapify from node i, top to bottom ###\ndef sift_down(i)\n  loop do\n    # If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n    l, r, ma = left(i), right(i), i\n    ma = l if l < size && @max_heap[l] > @max_heap[ma]\n    ma = r if r < size && @max_heap[r] > @max_heap[ma]\n\n    # Swap two nodes\n    break if ma == i\n\n    # Swap two nodes\n    swap(i, ma)\n    # Loop downwards heapification\n    i = ma\n  end\nend\n
      "},{"location":"chapter_heap/heap/#813-common-applications-of-heaps","title":"8.1.3 \u00a0 Common Applications of Heaps","text":"
      • Priority queue: Heaps are typically the preferred data structure for implementing priority queues, with both enqueue and dequeue operations having a time complexity of \\(O(\\log n)\\), and the heap construction operation having \\(O(n)\\), all of which are highly efficient.
      • Heap sort: Given a set of data, we can build a heap with them and then continuously perform element removal operations to obtain sorted data. However, we usually use a more elegant approach to implement heap sort, as detailed in the \"Heap Sort\" chapter.
      • Getting the largest \\(k\\) elements: This is a classic algorithm problem and also a typical application, such as selecting the top 10 trending news for Weibo hot search, selecting the top 10 best-selling products, etc.
      "},{"location":"chapter_heap/summary/","title":"8.4 \u00a0 Summary","text":""},{"location":"chapter_heap/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • A heap is a complete binary tree that can be categorized as a max heap or min heap based on its property. The heap top element of a max heap (min heap) is the largest (smallest).
      • A priority queue is defined as a queue with priority sorting, typically implemented using heaps.
      • Common heap operations and their corresponding time complexities include: element insertion \\(O(\\log n)\\), heap top element removal \\(O(\\log n)\\), and accessing the heap top element \\(O(1)\\).
      • Complete binary trees are well-suited for array representation, so we typically use arrays to store heaps.
      • Heapify operations are used to maintain the heap property and are employed in both element insertion and removal operations.
      • The time complexity of building a heap with \\(n\\) input elements can be optimized to \\(O(n)\\), which is highly efficient.
      • Top-k is a classic algorithm problem that can be efficiently solved using the heap data structure, with a time complexity of \\(O(n \\log k)\\).
      "},{"location":"chapter_heap/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

      Q: Are the \"heap\" in data structures and the \"heap\" in memory management the same concept?

      The two are not the same concept; they just happen to share the name \"heap.\" The heap in computer system memory is part of dynamic memory allocation, where programs can use it to store data during runtime. Programs can request a certain amount of heap memory to store complex structures such as objects and arrays. When this data is no longer needed, the program needs to release this memory to prevent memory leaks. Compared to stack memory, heap memory management and usage require more caution, as improper use can lead to issues such as memory leaks and dangling pointers.

      "},{"location":"chapter_heap/top_k/","title":"8.3 \u00a0 Top-K Problem","text":"

      Question

      Given an unordered array nums of length \\(n\\), return the largest \\(k\\) elements in the array.

      For this problem, we'll first introduce two solutions with relatively straightforward approaches, then introduce a more efficient heap-based solution.

      "},{"location":"chapter_heap/top_k/#831-method-1-iterative-selection","title":"8.3.1 \u00a0 Method 1: Iterative Selection","text":"

      We can perform \\(k\\) rounds of traversal as shown in Figure 8-6, extracting the \\(1^{st}\\), \\(2^{nd}\\), \\(\\dots\\), \\(k^{th}\\) largest elements in each round, with a time complexity of \\(O(nk)\\).

      This method is only suitable when \\(k \\ll n\\), because when \\(k\\) is close to \\(n\\), the time complexity approaches \\(O(n^2)\\), which is very time-consuming.

      Figure 8-6 \u00a0 Traversing to find the largest k elements

      Tip

      When \\(k = n\\), we can obtain a complete sorted sequence, which is equivalent to the \"selection sort\" algorithm.

      "},{"location":"chapter_heap/top_k/#832-method-2-sorting","title":"8.3.2 \u00a0 Method 2: Sorting","text":"

      As shown in Figure 8-7, we can first sort the array nums, then return the rightmost \\(k\\) elements, with a time complexity of \\(O(n \\log n)\\).

      Clearly, this method \"overachieves\" the task, as we only need to find the largest \\(k\\) elements, without needing to sort the other elements.

      Figure 8-7 \u00a0 Sorting to find the largest k elements

      "},{"location":"chapter_heap/top_k/#833-method-3-heap","title":"8.3.3 \u00a0 Method 3: Heap","text":"

      We can solve the Top-k problem more efficiently using heaps, with the process shown in Figure 8-8.

      1. Initialize a min heap, where the heap top element is the smallest.
      2. First, insert the first \\(k\\) elements of the array into the heap in sequence.
      3. Starting from the \\((k + 1)^{th}\\) element, if the current element is greater than the heap top element, remove the heap top element and insert the current element into the heap.
      4. After traversal is complete, the heap contains the largest \\(k\\) elements.
      <1><2><3><4><5><6><7><8><9>

      Figure 8-8 \u00a0 Finding the largest k elements using a heap

      Example code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby top_k.py
      def top_k_heap(nums: list[int], k: int) -> list[int]:\n    \"\"\"Find the largest k elements in array based on heap\"\"\"\n    # Initialize min heap\n    heap = []\n    # Enter the first k elements of array into heap\n    for i in range(k):\n        heapq.heappush(heap, nums[i])\n    # Starting from the (k+1)th element, maintain heap length as k\n    for i in range(k, len(nums)):\n        # If current element is greater than top element, top element exits heap, current element enters heap\n        if nums[i] > heap[0]:\n            heapq.heappop(heap)\n            heapq.heappush(heap, nums[i])\n    return heap\n
      top_k.cpp
      /* Find the largest k elements in array based on heap */\npriority_queue<int, vector<int>, greater<int>> topKHeap(vector<int> &nums, int k) {\n    // Python's heapq module implements min heap by default\n    priority_queue<int, vector<int>, greater<int>> heap;\n    // Enter the first k elements of array into heap\n    for (int i = 0; i < k; i++) {\n        heap.push(nums[i]);\n    }\n    // Starting from the (k+1)th element, maintain heap length as k\n    for (int i = k; i < nums.size(); i++) {\n        // If current element is greater than top element, top element exits heap, current element enters heap\n        if (nums[i] > heap.top()) {\n            heap.pop();\n            heap.push(nums[i]);\n        }\n    }\n    return heap;\n}\n
      top_k.java
      /* Find the largest k elements in array based on heap */\nQueue<Integer> topKHeap(int[] nums, int k) {\n    // Python's heapq module implements min heap by default\n    Queue<Integer> heap = new PriorityQueue<Integer>();\n    // Enter the first k elements of array into heap\n    for (int i = 0; i < k; i++) {\n        heap.offer(nums[i]);\n    }\n    // Starting from the (k+1)th element, maintain heap length as k\n    for (int i = k; i < nums.length; i++) {\n        // If current element is greater than top element, top element exits heap, current element enters heap\n        if (nums[i] > heap.peek()) {\n            heap.poll();\n            heap.offer(nums[i]);\n        }\n    }\n    return heap;\n}\n
      top_k.cs
      /* Find the largest k elements in array based on heap */\nPriorityQueue<int, int> TopKHeap(int[] nums, int k) {\n    // Python's heapq module implements min heap by default\n    PriorityQueue<int, int> heap = new();\n    // Enter the first k elements of array into heap\n    for (int i = 0; i < k; i++) {\n        heap.Enqueue(nums[i], nums[i]);\n    }\n    // Starting from the (k+1)th element, maintain heap length as k\n    for (int i = k; i < nums.Length; i++) {\n        // If current element is greater than top element, top element exits heap, current element enters heap\n        if (nums[i] > heap.Peek()) {\n            heap.Dequeue();\n            heap.Enqueue(nums[i], nums[i]);\n        }\n    }\n    return heap;\n}\n
      top_k.go
      /* Find the largest k elements in array based on heap */\nfunc topKHeap(nums []int, k int) *minHeap {\n    // Python's heapq module implements min heap by default\n    h := &minHeap{}\n    heap.Init(h)\n    // Enter the first k elements of array into heap\n    for i := 0; i < k; i++ {\n        heap.Push(h, nums[i])\n    }\n    // Starting from the (k+1)th element, maintain heap length as k\n    for i := k; i < len(nums); i++ {\n        // If current element is greater than top element, top element exits heap, current element enters heap\n        if nums[i] > h.Top().(int) {\n            heap.Pop(h)\n            heap.Push(h, nums[i])\n        }\n    }\n    return h\n}\n
      top_k.swift
      /* Find the largest k elements in array based on heap */\nfunc topKHeap(nums: [Int], k: Int) -> [Int] {\n    // Initialize min heap and build heap with first k elements\n    var heap = Heap(nums.prefix(k))\n    // Starting from the (k+1)th element, maintain heap length as k\n    for i in nums.indices.dropFirst(k) {\n        // If current element is greater than top element, top element exits heap, current element enters heap\n        if nums[i] > heap.min()! {\n            _ = heap.removeMin()\n            heap.insert(nums[i])\n        }\n    }\n    return heap.unordered\n}\n
      top_k.js
      /* Element enters heap */\nfunction pushMinHeap(maxHeap, val) {\n    // Negate element\n    maxHeap.push(-val);\n}\n\n/* Element exits heap */\nfunction popMinHeap(maxHeap) {\n    // Negate element\n    return -maxHeap.pop();\n}\n\n/* Access top element */\nfunction peekMinHeap(maxHeap) {\n    // Negate element\n    return -maxHeap.peek();\n}\n\n/* Extract elements from heap */\nfunction getMinHeap(maxHeap) {\n    // Negate element\n    return maxHeap.getMaxHeap().map((num) => -num);\n}\n\n/* Find the largest k elements in array based on heap */\nfunction topKHeap(nums, k) {\n    // Python's heapq module implements min heap by default\n    // Note: We negate all heap elements to simulate min heap using max heap\n    const maxHeap = new MaxHeap([]);\n    // Enter the first k elements of array into heap\n    for (let i = 0; i < k; i++) {\n        pushMinHeap(maxHeap, nums[i]);\n    }\n    // Starting from the (k+1)th element, maintain heap length as k\n    for (let i = k; i < nums.length; i++) {\n        // If current element is greater than top element, top element exits heap, current element enters heap\n        if (nums[i] > peekMinHeap(maxHeap)) {\n            popMinHeap(maxHeap);\n            pushMinHeap(maxHeap, nums[i]);\n        }\n    }\n    // Return elements in heap\n    return getMinHeap(maxHeap);\n}\n
      top_k.ts
      /* Element enters heap */\nfunction pushMinHeap(maxHeap: MaxHeap, val: number): void {\n    // Negate element\n    maxHeap.push(-val);\n}\n\n/* Element exits heap */\nfunction popMinHeap(maxHeap: MaxHeap): number {\n    // Negate element\n    return -maxHeap.pop();\n}\n\n/* Access top element */\nfunction peekMinHeap(maxHeap: MaxHeap): number {\n    // Negate element\n    return -maxHeap.peek();\n}\n\n/* Extract elements from heap */\nfunction getMinHeap(maxHeap: MaxHeap): number[] {\n    // Negate element\n    return maxHeap.getMaxHeap().map((num: number) => -num);\n}\n\n/* Find the largest k elements in array based on heap */\nfunction topKHeap(nums: number[], k: number): number[] {\n    // Python's heapq module implements min heap by default\n    // Note: We negate all heap elements to simulate min heap using max heap\n    const maxHeap = new MaxHeap([]);\n    // Enter the first k elements of array into heap\n    for (let i = 0; i < k; i++) {\n        pushMinHeap(maxHeap, nums[i]);\n    }\n    // Starting from the (k+1)th element, maintain heap length as k\n    for (let i = k; i < nums.length; i++) {\n        // If current element is greater than top element, top element exits heap, current element enters heap\n        if (nums[i] > peekMinHeap(maxHeap)) {\n            popMinHeap(maxHeap);\n            pushMinHeap(maxHeap, nums[i]);\n        }\n    }\n    // Return elements in heap\n    return getMinHeap(maxHeap);\n}\n
      top_k.dart
      /* Find the largest k elements in array based on heap */\nMinHeap topKHeap(List<int> nums, int k) {\n  // Initialize min heap, push first k elements of array to heap\n  MinHeap heap = MinHeap(nums.sublist(0, k));\n  // Starting from the (k+1)th element, maintain heap length as k\n  for (int i = k; i < nums.length; i++) {\n    // If current element is greater than top element, top element exits heap, current element enters heap\n    if (nums[i] > heap.peek()) {\n      heap.pop();\n      heap.push(nums[i]);\n    }\n  }\n  return heap;\n}\n
      top_k.rs
      /* Find the largest k elements in array based on heap */\nfn top_k_heap(nums: Vec<i32>, k: usize) -> BinaryHeap<Reverse<i32>> {\n    // BinaryHeap is a max heap, use Reverse to negate elements to implement min heap\n    let mut heap = BinaryHeap::<Reverse<i32>>::new();\n    // Enter the first k elements of array into heap\n    for &num in nums.iter().take(k) {\n        heap.push(Reverse(num));\n    }\n    // Starting from the (k+1)th element, maintain heap length as k\n    for &num in nums.iter().skip(k) {\n        // If current element is greater than top element, top element exits heap, current element enters heap\n        if num > heap.peek().unwrap().0 {\n            heap.pop();\n            heap.push(Reverse(num));\n        }\n    }\n    heap\n}\n
      top_k.c
      /* Element enters heap */\nvoid pushMinHeap(MaxHeap *maxHeap, int val) {\n    // Negate element\n    push(maxHeap, -val);\n}\n\n/* Element exits heap */\nint popMinHeap(MaxHeap *maxHeap) {\n    // Negate element\n    return -pop(maxHeap);\n}\n\n/* Access top element */\nint peekMinHeap(MaxHeap *maxHeap) {\n    // Negate element\n    return -peek(maxHeap);\n}\n\n/* Extract elements from heap */\nint *getMinHeap(MaxHeap *maxHeap) {\n    // Negate all heap elements and store in res array\n    int *res = (int *)malloc(maxHeap->size * sizeof(int));\n    for (int i = 0; i < maxHeap->size; i++) {\n        res[i] = -maxHeap->data[i];\n    }\n    return res;\n}\n\n/* Extract elements from heap */\nint *getMinHeap(MaxHeap *maxHeap) {\n    // Negate all heap elements and store in res array\n    int *res = (int *)malloc(maxHeap->size * sizeof(int));\n    for (int i = 0; i < maxHeap->size; i++) {\n        res[i] = -maxHeap->data[i];\n    }\n    return res;\n}\n\n// Function to find k largest elements in array using heap\nint *topKHeap(int *nums, int sizeNums, int k) {\n    // Python's heapq module implements min heap by default\n    // Note: We negate all heap elements to simulate min heap using max heap\n    int *empty = (int *)malloc(0);\n    MaxHeap *maxHeap = newMaxHeap(empty, 0);\n    // Enter the first k elements of array into heap\n    for (int i = 0; i < k; i++) {\n        pushMinHeap(maxHeap, nums[i]);\n    }\n    // Starting from the (k+1)th element, maintain heap length as k\n    for (int i = k; i < sizeNums; i++) {\n        // If current element is greater than top element, top element exits heap, current element enters heap\n        if (nums[i] > peekMinHeap(maxHeap)) {\n            popMinHeap(maxHeap);\n            pushMinHeap(maxHeap, nums[i]);\n        }\n    }\n    int *res = getMinHeap(maxHeap);\n    // Free memory\n    delMaxHeap(maxHeap);\n    return res;\n}\n
      top_k.kt
      /* Find the largest k elements in array based on heap */\nfun topKHeap(nums: IntArray, k: Int): Queue<Int> {\n    // Python's heapq module implements min heap by default\n    val heap = PriorityQueue<Int>()\n    // Enter the first k elements of array into heap\n    for (i in 0..<k) {\n        heap.offer(nums[i])\n    }\n    // Starting from the (k+1)th element, maintain heap length as k\n    for (i in k..<nums.size) {\n        // If current element is greater than top element, top element exits heap, current element enters heap\n        if (nums[i] > heap.peek()) {\n            heap.poll()\n            heap.offer(nums[i])\n        }\n    }\n    return heap\n}\n
      top_k.rb
      ### Find largest k elements in array using heap ###\ndef top_k_heap(nums, k)\n  # Python's heapq module implements min heap by default\n  # Note: We negate all heap elements to simulate min heap using max heap\n  max_heap = MaxHeap.new([])\n\n  # Enter the first k elements of array into heap\n  for i in 0...k\n    push_min_heap(max_heap, nums[i])\n  end\n\n  # Starting from the (k+1)th element, maintain heap length as k\n  for i in k...nums.length\n    # If current element is greater than top element, top element exits heap, current element enters heap\n    if nums[i] > peek_min_heap(max_heap)\n      pop_min_heap(max_heap)\n      push_min_heap(max_heap, nums[i])\n    end\n  end\n\n  get_min_heap(max_heap)\nend\n

      A total of \\(n\\) rounds of heap insertions and removals are performed, with the heap's maximum length being \\(k\\), so the time complexity is \\(O(n \\log k)\\). This method is very efficient; when \\(k\\) is small, the time complexity approaches \\(O(n)\\); when \\(k\\) is large, the time complexity does not exceed \\(O(n \\log n)\\).

      Additionally, this method is suitable for dynamic data stream scenarios. By continuously adding data, we can maintain the elements in the heap, thus achieving dynamic updates of the largest \\(k\\) elements.

      "},{"location":"chapter_hello_algo/","title":"Before Starting","text":"

      A few years ago, I shared the \"Sword for Offer\" problem solutions on LeetCode, receiving encouragement and support from many readers. During interactions with readers, the most frequently asked question I encountered was \"how to get started with algorithms.\" Gradually, I developed a keen interest in this question.

      Diving straight into problem-solving seems to be the most popular approach\u2014it's simple, direct, and effective. However, problem-solving is like playing Minesweeper: those with strong self-learning abilities can successfully defuse the mines one by one, while those with insufficient foundations may end up bruised and battered, retreating step by step in frustration. Reading through textbooks is also a common practice, but for job seekers, graduation theses, resume submissions, and preparations for written tests and interviews have already consumed most of their energy, making working through thick books an arduous challenge.

      If you're facing similar struggles, then it's fortunate that this book has \"found\" you. This book is my answer to this question\u2014even if it may not be the optimal solution, it is at least a positive attempt. While this book alone won't directly land you a job offer, it will guide you to explore the \"knowledge map\" of data structures and algorithms, help you understand the shapes, sizes, and distributions of different \"mines,\" and enable you to master various \"mine-clearing methods.\" With these skills, I believe you can tackle problems and read technical literature more confidently, gradually building a complete knowledge system.

      I deeply agree with Professor Feynman's words: \"Knowledge isn't free. You have to pay attention.\" In this sense, this book is not entirely \"free.\" In order to live up to the precious \"attention\" you invest in this book, I will do my utmost and devote my greatest \"attention\" to completing this work.

      I'm acutely aware of my limited knowledge and shallow expertise. Although the content of this book has been refined over a period of time, there are certainly still many errors, and I sincerely welcome critiques and corrections from teachers and fellow students.

      Hello, Algorithms!

      The advent of computers has brought tremendous changes to the world. With their high-speed computing capabilities and excellent programmability, they have become the ideal medium for executing algorithms and processing data. Whether it's the realistic graphics in video games, the intelligent decision-making in autonomous driving, AlphaGo's brilliant Go matches, or ChatGPT's natural interactions, these applications are all exquisite interpretations of algorithms on computers.

      In fact, before the advent of computers, algorithms and data structures already existed in every corner of the world. Early algorithms were relatively simple, such as ancient counting methods and tool-making procedures. As civilization progressed, algorithms gradually became more refined and complex. From the ingenious craftsmanship of master artisans, to industrial products that liberate productive forces, to the scientific laws governing the operation of the universe, behind almost every ordinary or astonishing thing lies ingenious algorithmic thinking.

      Similarly, data structures are everywhere: from large-scale social networks to small subway systems, many systems can be modeled as \"graphs\"; from a nation to a family, the primary organizational forms of society exhibit characteristics of \"trees\"; winter clothing is like a \"stack,\" where the first item put on is the last to be taken off; a badminton tube is like a \"queue,\" with items inserted at one end and retrieved from the other; a dictionary is like a \"hash table,\" enabling quick lookup of target entries.

      This book aims to help readers understand the core concepts of algorithms and data structures through clear and accessible animated illustrations and runnable code examples, and to implement them through programming. Building on this foundation, the book endeavors to reveal the vivid manifestations of algorithms in the complex world and showcase the beauty of algorithms. I hope this book can be of help to you!

      "},{"location":"chapter_introduction/","title":"Chapter 1. \u00a0 Encounter with Algorithms","text":"

      Abstract

      A young girl dances gracefully, intertwined with data, her skirt flowing with the melody of algorithms.

      She invites you to dance with her. Follow her steps closely and enter the world of algorithms, full of logic and beauty.

      "},{"location":"chapter_introduction/#chapter-contents","title":"Chapter contents","text":"
      • 1.1 \u00a0 Algorithms Are Everywhere
      • 1.2 \u00a0 What Is an Algorithm
      • 1.3 \u00a0 Summary
      "},{"location":"chapter_introduction/algorithms_are_everywhere/","title":"1.1 \u00a0 Algorithms Are Everywhere","text":"

      When we hear the term \"algorithm,\" we naturally think of mathematics. However, many algorithms do not involve complex mathematics but rely more on basic logic, which can be seen everywhere in our daily lives.

      Before we start discussing about algorithms officially, there's an interesting fact worth sharing: you've learned many algorithms unconsciously and are used to applying them in your daily life. Here, I will give a few specific examples to prove this point.

      Example 1: Looking Up a Dictionary. In an English dictionary, words are listed alphabetically. Assuming we're searching for a word that starts with the letter \\(r\\), this is typically done in the following way:

      1. Open the dictionary to about halfway and check the first vocabulary of the page, let's say the letter starts with \\(m\\).
      2. Since \\(r\\) comes after \\(m\\) in the alphabet, the first half can be ignored and the search space is narrowed down to the second half.
      3. Repeat steps 1. and 2. until you find the page where the word starts with \\(r\\).
      <1><2><3><4><5>

      Figure 1-1 \u00a0 Process of looking up a dictionary

      Looking up a dictionary, an essential skill for elementary school students is actually the famous \"Binary Search\" algorithm. From a data structure perspective, we can consider the dictionary as a sorted \"array\"; from an algorithmic perspective, the series of actions taken to look up a word in the dictionary can be viewed as the algorithm \"Binary Search.\"

      Example 2: Organizing Card Deck. When playing cards, we need to arrange the cards in our hands in ascending order, as shown in the following process.

      1. Divide the playing cards into \"ordered\" and \"unordered\" sections, assuming initially the leftmost card is already in order.
      2. Take out a card from the unordered section and insert it into the correct position in the ordered section; after this, the leftmost two cards are in order.
      3. Repeat step 2 until all cards are in order.

      Figure 1-2 \u00a0 Process of sorting a deck of cards

      The above method of organizing playing cards is practically the \"Insertion Sort\" algorithm, which is very efficient for small datasets. Many programming languages' sorting functions include the insertion sort.

      Example 3: Making Change. Assume making a purchase of \\(69\\) at a supermarket. If you give the cashier \\(100\\), they will need to provide you with \\(31\\) in change. This process can be clearly understood as illustrated in Figure 1-3.

      1. The options are currencies valued below \\(31\\), including \\(1\\), \\(5\\), \\(10\\), and \\(20\\).
      2. Take out the largest \\(20\\) from the options, leaving \\(31 - 20 = 11\\).
      3. Take out the largest \\(10\\) from the remaining options, leaving \\(11 - 10 = 1\\).
      4. Take out the largest \\(1\\) from the remaining options, leaving \\(1 - 1 = 0\\).
      5. Complete change-making, the solution is \\(20 + 10 + 1 = 31\\).

      Figure 1-3 \u00a0 Process of making change

      In the steps described, we choose the best option at each stage by utilizing the largest denomination available, which leads to an effective change-making strategy. From a data structures and algorithms perspective, this approach is known as a \"Greedy\" algorithm.

      From cooking a meal to interstellar travel, almost all problem-solving involves algorithms. The advent of computers allows us to store data structures in memory and write code to call the CPU and GPU to execute algorithms. In this way, we can transfer real-life problems to computers and solve various complex issues in a more efficient way.

      Tip

      If you are still confused about concepts like data structures, algorithms, arrays, and binary searches, I encourage you to keep reading. This book will gently guide you into the realm of understanding data structures and algorithms.

      "},{"location":"chapter_introduction/summary/","title":"1.3 \u00a0 Summary","text":""},{"location":"chapter_introduction/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • Algorithms are ubiquitous in daily life and are not distant, esoteric knowledge. In fact, we have already learned many algorithms unconsciously and use them to solve problems big and small in life.
      • The principle of looking up a dictionary is consistent with the binary search algorithm. Binary search embodies the important algorithmic idea of divide and conquer.
      • The process of organizing playing cards is very similar to the insertion sort algorithm. Insertion sort is suitable for sorting small datasets.
      • The steps of making change are essentially a greedy algorithm, where the best choice is made at each step based on the current situation.
      • An algorithm is a set of instructions or operational steps that solves a specific problem within a finite amount of time, while a data structure is the way computers organize and store data.
      • Data structures and algorithms are closely connected. Data structures are the foundation of algorithms, and algorithms breathe life into data structures.
      • We can compare data structures and algorithms to assembling building blocks. The blocks represent data, the shape and connection method of the blocks represent the data structure, and the steps to assemble the blocks correspond to the algorithm.
      "},{"location":"chapter_introduction/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

      Q: As a programmer, I have never used algorithms to solve problems in my daily work. Common algorithms are already encapsulated by programming languages and can be used directly. Does this mean that the problems in our work have not yet reached the level where algorithms are needed?

      If we compare specific work skills to \"techniques\" in martial arts, then fundamental subjects should be more like \"internal skills\".

      I believe the significance of learning algorithms (and other fundamental subjects) is not to implement them from scratch at work, but rather to be able to make professional reactions and judgments when solving problems based on the knowledge learned, thereby improving the overall quality of work. Here is a simple example. Every programming language has a built-in sorting function:

      • If we have not studied data structures and algorithms, we might simply feed any given data to this sorting function. It runs smoothly with good performance, and there doesn't seem to be any problem.
      • But if we have studied algorithms, we would know that the time complexity of the built-in sorting function is \\(O(n \\log n)\\). However, if the given data consists of integers with a fixed number of digits (such as student IDs), we can use the more efficient \"radix sort\", reducing the time complexity to \\(O(nk)\\), where \\(k\\) is the number of digits. When the data volume is very large, the saved running time can create significant value (reduced costs, improved experience, etc.).

      In the field of engineering, a large number of problems are difficult to reach optimal solutions, and many problems are only solved \"approximately\". The difficulty of a problem depends on one hand on the nature of the problem itself, and on the other hand on the knowledge reserve of the person observing the problem. The more complete a person's knowledge and the more experience they have, the deeper their analysis of the problem will be, and the more elegantly the problem can be solved.

      "},{"location":"chapter_introduction/what_is_dsa/","title":"1.2 \u00a0 What Is an Algorithm","text":""},{"location":"chapter_introduction/what_is_dsa/#121-algorithm-definition","title":"1.2.1 \u00a0 Algorithm Definition","text":"

      An algorithm is a set of instructions or operational steps that solves a specific problem within a finite amount of time. It has the following characteristics.

      • The problem is well-defined, with clear input and output definitions.
      • It is feasible and can be completed within a finite number of steps, time, and memory space.
      • Each step has a definite meaning, and under the same input and operating conditions, the output is always the same.
      "},{"location":"chapter_introduction/what_is_dsa/#122-data-structure-definition","title":"1.2.2 \u00a0 Data Structure Definition","text":"

      A data structure is a way of organizing and storing data, covering the data content, relationships between data, and methods for data operations. It has the following design objectives.

      • Occupy as little space as possible to save computer memory.
      • Data operations should be as fast as possible, covering data access, addition, deletion, update, etc.
      • Provide a concise data representation and logical information so that algorithms can run efficiently.

      Data structure design is a process full of trade-offs. If we want to achieve improvements in one aspect, we often need to make compromises in another aspect. Here are two examples.

      • Compared to arrays, linked lists are more convenient for data addition and deletion operations but sacrifice data access speed.
      • Compared to linked lists, graphs provide richer logical information but require larger memory space.
      "},{"location":"chapter_introduction/what_is_dsa/#123-the-relationship-between-data-structures-and-algorithms","title":"1.2.3 \u00a0 The Relationship Between Data Structures and Algorithms","text":"

      As shown in Figure 1-4, data structures and algorithms are highly related and tightly coupled, specifically manifested in the following three aspects.

      • Data structures are the foundation of algorithms. Data structures provide algorithms with structured storage of data and methods for operating on data.
      • Algorithms breathe life into data structures. Data structures themselves only store data information; combined with algorithms, they can solve specific problems.
      • Algorithms can usually be implemented based on different data structures, but execution efficiency may vary greatly. Choosing the appropriate data structure is key.

      Figure 1-4 \u00a0 The relationship between data structures and algorithms

      Data structures and algorithms are like assembling building blocks as shown in Figure 1-5. A set of building blocks, in addition to containing many parts, also comes with detailed assembly instructions. By following the instructions step by step, we can assemble an exquisite building block model.

      Figure 1-5 \u00a0 Assembling blocks

      The detailed correspondence between the two is shown in Table 1-1.

      Table 1-1 \u00a0 Comparing data structures and algorithms to assembling building blocks

      Data structures and algorithms Assembling building blocks Input data Unassembled building blocks Data structure Organization form of building blocks, including shape, size, connection method, etc. Algorithm A series of operational steps to assemble the blocks into the target form Output data Building block model

      It is worth noting that data structures and algorithms are independent of programming languages. For this reason, this book is able to provide implementations based on multiple programming languages.

      Conventional abbreviation

      In actual discussions, we usually abbreviate \"data structures and algorithms\" as \"algorithms\". For example, the well-known LeetCode algorithm problems actually examine knowledge of both data structures and algorithms.

      "},{"location":"chapter_preface/","title":"Chapter 0. \u00a0 Preface","text":"

      Abstract

      Algorithms are like a beautiful symphony, each line of code flows like a melody.

      May this book gently resonate in your mind, leaving a unique and profound melody.

      "},{"location":"chapter_preface/#chapter-contents","title":"Chapter contents","text":"
      • 0.1 \u00a0 About This Book
      • 0.2 \u00a0 How to Use This Book
      • 0.3 \u00a0 Summary
      "},{"location":"chapter_preface/about_the_book/","title":"0.1 \u00a0 About This Book","text":"

      This project aims to create an open-source, free, beginner-friendly introductory tutorial on data structures and algorithms.

      • The entire book uses animated illustrations, with clear and easy-to-understand content and a smooth learning curve, guiding beginners to explore the knowledge map of data structures and algorithms.
      • The source code can be run with one click, helping readers improve their programming skills through practice and understand how algorithms work and the underlying implementation of data structures.
      • We encourage readers to learn from each other, and everyone is welcome to ask questions and share insights in the comments section, making progress together through discussion and exchange.
      "},{"location":"chapter_preface/about_the_book/#011-target-audience","title":"0.1.1 \u00a0 Target Audience","text":"

      If you are an algorithm beginner who has never been exposed to algorithms, or if you already have some problem-solving experience and have a vague understanding of data structures and algorithms, oscillating between knowing and not knowing, then this book is tailor-made for you!

      If you have already accumulated a certain amount of problem-solving experience and are familiar with most question types, this book can help you review and organize your algorithm knowledge system, and the repository's source code can be used as a \"problem-solving toolkit\" or \"algorithm dictionary.\"

      If you are an algorithm \"expert,\" we look forward to receiving your valuable suggestions, or participating in creation together.

      Prerequisites

      You need to have at least a programming foundation in any language, and be able to read and write simple code.

      "},{"location":"chapter_preface/about_the_book/#012-content-structure","title":"0.1.2 \u00a0 Content Structure","text":"

      The main content of this book is shown in Figure 0-1.

      • Complexity analysis: Evaluation dimensions and methods for data structures and algorithms. Methods for calculating time complexity and space complexity, common types, examples, etc.
      • Data structures: Classification methods for basic data types and data structures. The definition, advantages and disadvantages, common operations, common types, typical applications, implementation methods, etc. of data structures such as arrays, linked lists, stacks, queues, hash tables, trees, heaps, and graphs.
      • Algorithms: The definition, advantages and disadvantages, efficiency, application scenarios, problem-solving steps, and example problems of algorithms such as searching, sorting, divide and conquer, backtracking, dynamic programming, and greedy algorithms.

      Figure 0-1 \u00a0 Main content of this book

      "},{"location":"chapter_preface/about_the_book/#013-acknowledgements","title":"0.1.3 \u00a0 Acknowledgements","text":"

      This book has been continuously improved through the joint efforts of many contributors in the open-source community. Thanks to every contributor who invested time and effort, they are (in the order automatically generated by GitHub): krahets, coderonion, Gonglja, nuomi1, Reanon, justin-tse, hpstory, danielsss, curtishd, night-cruise, S-N-O-R-L-A-X, rongyi, msk397, gvenusleo, khoaxuantu, rivertwilight, K3v123, gyt95, zhuoqinyue, yuelinxin, Zuoxun, mingXta, Phoenix0415, FangYuan33, GN-Yu, longsizhuo, IsChristina, xBLACKICEx, guowei-gong, Cathay-Chen, pengchzn, QiLOL, magentaqin, hello-ikun, JoseHung, qualifier1024, thomasq0, sunshinesDL, L-Super, Guanngxu, Transmigration-zhou, WSL0809, Slone123c, lhxsm, yuan0221, what-is-me, Shyam-Chen, theNefelibatas, longranger2, codeberg-user, xiongsp, JeffersonHuang, prinpal, seven1240, Wonderdch, malone6, xiaomiusa87, gaofer, bluebean-cloud, a16su, SamJin98, hongyun-robot, nanlei, XiaChuerwu, yd-j, iron-irax, mgisr, steventimes, junminhong, heshuyue, danny900714, MolDuM, Nigh, Dr-XYZ, XC-Zero, reeswell, PXG-XPG, NI-SW, Horbin-Magician, Enlightenus, YangXuanyi, beatrix-chan, DullSword, xjr7670, jiaxianhua, qq909244296, iStig, boloboloda, hts0000, gledfish, wenjianmin, keshida, kilikilikid, lclc6, lwbaptx, linyejoe2, liuxjerry, llql1211, fbigm, echo1937, szu17dmy, dshlstarr, Yucao-cy, coderlef, czruby, bongbongbakudan, beintentional, ZongYangL, ZhongYuuu, ZhongGuanbin, hezhizhen, linzeyan, ZJKung, luluxia, xb534, ztkuaikuai, yw-1021, ElaBosak233, baagod, zhouLion, yishangzhang, yi427, yanedie, yabo083, weibk, wangwang105, th1nk3r-ing, tao363, 4yDX3906, syd168, sslmj2020, smilelsb, siqyka, selear, sdshaoda, Xi-Row, popozhu, nuquist19, noobcodemaker, XiaoK29, chadyi, lyl625760, lucaswangdev, 0130w, shanghai-Jerry, EJackYang, Javesun99, eltociear, lipusheng, KNChiu, BlindTerran, ShiMaRing, lovelock, FreddieLi, FloranceYeh, fanchenggang, gltianwen, goerll, nedchu, curly210102, CuB3y0nd, KraHsu, CarrotDLaw, youshaoXG, bubble9um, Asashishi, Asa0oo0o0o, fanenr, eagleanurag, akshiterate, 52coder, foursevenlove, KorsChen, GaochaoZhu, hopkings2008, yang-le, realwujing, Evilrabbit520, Umer-Jahangir, Turing-1024-Lee, Suremotoo, paoxiaomooo, Chieko-Seren, Allen-Scai, ymmmas, Risuntsy, Richard-Zhang1019, RafaelCaso, qingpeng9802, primexiao, Urbaner3, zhongfq, nidhoggfgg, MwumLi, CreatorMetaSky, martinx, ZnYang2018, hugtyftg, logan-qiu, psychelzh, Keynman, KeiichiKasai, and KawaiiAsh.

      The code review work for this book was completed by coderonion, curtishd, Gonglja, gvenusleo, hpstory, justin-tse, khoaxuantu, krahets, night-cruise, nuomi1, Reanon and rongyi (in alphabetical order). Thanks to them for the time and effort they put in, it is they who ensure the standardization and unity of code in various languages.

      The Traditional Chinese version of this book was reviewed by Shyam-Chen and Dr-XYZ, the English version was reviewed by yuelinxin, K3v123, QiLOL, Phoenix0415, SamJin98, yanedie, RafaelCaso, pengchzn, thomasq0 and magentaqin, and the Japanese edition was reviewed by eltociear. It is because of their continuous contributions that this book can serve a wider readership, and we thank them.

      The ePub ebook generation tool for this book was developed by zhongfq. We thank him for his contribution, which provides readers with a more flexible way to read.

      During the creation of this book, I received help from many people.

      • Thanks to my mentor at the company, Dr. Li Xi, who encouraged me to \"take action quickly\" during a conversation, strengthening my determination to write this book;
      • Thanks to my girlfriend Bubble as the first reader of this book, who provided many valuable suggestions from the perspective of an algorithm beginner, making this book more suitable for novices to read;
      • Thanks to Tengbao, Qibao, and Feibao for coming up with a creative name for this book, evoking everyone's fond memories of writing their first line of code \"Hello World!\";
      • Thanks to Xiaoquan for providing professional help in intellectual property rights, which played an important role in the improvement of this open-source book;
      • Thanks to Sutong for designing the beautiful cover and logo for this book, and for patiently making revisions multiple times driven by my obsessive-compulsive disorder;
      • Thanks to @squidfunk for the typesetting suggestions, as well as for developing the open-source documentation theme Material-for-MkDocs.

      During the writing process, I read many textbooks and articles on data structures and algorithms. These works provided excellent examples for this book and ensured the accuracy and quality of the book's content. I would like to thank all the teachers and predecessors for their outstanding contributions!

      This book advocates a learning method that combines hands and brain, and in this regard I was deeply inspired by Dive into Deep Learning. I highly recommend this excellent work to all readers.

      Heartfelt thanks to my parents, it is your support and encouragement that has given me the opportunity to do this interesting thing.

      "},{"location":"chapter_preface/suggestions/","title":"0.2 \u00a0 How to Use This Book","text":"

      Tip

      For the best reading experience, it is recommended that you read through this section.

      "},{"location":"chapter_preface/suggestions/#021-writing-style-conventions","title":"0.2.1 \u00a0 Writing Style Conventions","text":"
      • Titles marked with * are optional sections with relatively difficult content. If you have limited time, you can skip them first.
      • Technical terms will be in bold (in paper and PDF versions) or underlined (in web versions), such as array. It is recommended to memorize them for reading literature.
      • Key content and summary statements will be bolded, and such text deserves special attention.
      • Words and phrases with specific meanings will be marked with \"quotation marks\" to avoid ambiguity.
      • When it comes to nouns that are inconsistent between programming languages, this book uses Python as the standard, for example, using None to represent \"null\".
      • This book partially abandons the comment conventions of programming languages in favor of more compact content layout. Comments are mainly divided into three types: title comments, content comments, and multi-line comments.
      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      \"\"\"Title comment, used to label functions, classes, test cases, etc.\"\"\"\n\n# Content comment, used to explain code in detail\n\n\"\"\"\nMulti-line\ncomment\n\"\"\"\n
      /* Title comment, used to label functions, classes, test cases, etc. */\n\n// Content comment, used to explain code in detail\n\n/**\n * Multi-line\n * comment\n */\n
      /* Title comment, used to label functions, classes, test cases, etc. */\n\n// Content comment, used to explain code in detail\n\n/**\n * Multi-line\n * comment\n */\n
      /* Title comment, used to label functions, classes, test cases, etc. */\n\n// Content comment, used to explain code in detail\n\n/**\n * Multi-line\n * comment\n */\n
      /* Title comment, used to label functions, classes, test cases, etc. */\n\n// Content comment, used to explain code in detail\n\n/**\n * Multi-line\n * comment\n */\n
      /* Title comment, used to label functions, classes, test cases, etc. */\n\n// Content comment, used to explain code in detail\n\n/**\n * Multi-line\n * comment\n */\n
      /* Title comment, used to label functions, classes, test cases, etc. */\n\n// Content comment, used to explain code in detail\n\n/**\n * Multi-line\n * comment\n */\n
      /* Title comment, used to label functions, classes, test cases, etc. */\n\n// Content comment, used to explain code in detail\n\n/**\n * Multi-line\n * comment\n */\n
      /* Title comment, used to label functions, classes, test cases, etc. */\n\n// Content comment, used to explain code in detail\n\n/**\n * Multi-line\n * comment\n */\n
      /* Title comment, used to label functions, classes, test cases, etc. */\n\n// Content comment, used to explain code in detail\n\n/**\n * Multi-line\n * comment\n */\n
      /* Title comment, used to label functions, classes, test cases, etc. */\n\n// Content comment, used to explain code in detail\n\n/**\n * Multi-line\n * comment\n */\n
      /* Title comment, used to label functions, classes, test cases, etc. */\n\n// Content comment, used to explain code in detail\n\n/**\n * Multi-line\n * comment\n */\n
      ### Title comment, used to label functions, classes, test cases, etc. ###\n\n# Content comment, used to explain code in detail\n\n# Multi-line\n# comment\n
      "},{"location":"chapter_preface/suggestions/#022-learning-efficiently-with-animated-illustrations","title":"0.2.2 \u00a0 Learning Efficiently with Animated Illustrations","text":"

      Compared to text, videos and images have higher information density and structural organization, making them easier to understand. In this book, key and difficult knowledge will mainly be presented in the form of animated illustrations, with text serving as explanation and supplement.

      If you find that a section of content provides animated illustrations as shown in Figure 0-2 while reading this book, please focus on the illustrations first, with text as a supplement, and combine the two to understand the content.

      Figure 0-2 \u00a0 Example of animated illustrations

      "},{"location":"chapter_preface/suggestions/#023-deepening-understanding-through-code-practice","title":"0.2.3 \u00a0 Deepening Understanding Through Code Practice","text":"

      The accompanying code for this book is hosted in the GitHub repository. As shown in Figure 0-3, the source code comes with test cases and can be run with one click.

      If time permits, it is recommended that you type out the code yourself. If you have limited study time, please at least read through and run all the code.

      Compared to reading code, the process of writing code often brings more rewards. Learning by doing is the real learning.

      Figure 0-3 \u00a0 Example of running code

      The preliminary work for running code is mainly divided into three steps.

      Step 1: Install the local programming environment. Please follow the tutorial shown in the appendix for installation. If already installed, you can skip this step.

      Step 2: Clone or download the code repository. Visit the GitHub repository. If you have already installed Git, you can clone this repository with the following command:

      git clone https://github.com/krahets/hello-algo.git\n

      Of course, you can also click the \"Download ZIP\" button at the location shown in Figure 0-4 to directly download the code compressed package, and then extract it locally.

      Figure 0-4 \u00a0 Clone repository and download code

      Step 3: Run the source code. As shown in Figure 0-5, for code blocks with file names at the top, we can find the corresponding source code files in the codes folder of the repository. The source code files can be run with one click, which will help you save unnecessary debugging time and allow you to focus on learning content.

      Figure 0-5 \u00a0 Code blocks and corresponding source code files

      In addition to running code locally, the web version also supports visual running of Python code (implemented based on pythontutor). As shown in Figure 0-6, you can click \"Visual Run\" below the code block to expand the view and observe the execution process of the algorithm code; you can also click \"Full Screen View\" for a better viewing experience.

      Figure 0-6 \u00a0 Visual running of Python code

      "},{"location":"chapter_preface/suggestions/#024-growing-together-through-questions-and-discussions","title":"0.2.4 \u00a0 Growing Together Through Questions and Discussions","text":"

      When reading this book, please do not easily skip knowledge points that you have not learned well. Feel free to ask your questions in the comments section, and my friends and I will do our best to answer you, and generally reply within two days.

      As shown in Figure 0-7, the web version has a comments section at the bottom of each chapter. I hope you will pay more attention to the content of the comments section. On the one hand, you can learn about the problems that everyone encounters, thus checking for omissions and stimulating deeper thinking. On the other hand, I hope you can generously answer other friends' questions, share your insights, and help others progress.

      Figure 0-7 \u00a0 Example of comments section

      "},{"location":"chapter_preface/suggestions/#025-algorithm-learning-roadmap","title":"0.2.5 \u00a0 Algorithm Learning Roadmap","text":"

      From an overall perspective, we can divide the process of learning data structures and algorithms into three stages.

      1. Stage 1: Algorithm introduction. We need to familiarize ourselves with the characteristics and usage of various data structures, and learn the principles, processes, uses, and efficiency of different algorithms.
      2. Stage 2: Practice algorithm problems. It is recommended to start with popular problems, and accumulate at least 100 problems first, to familiarize yourself with mainstream algorithm problems. When first practicing problems, \"knowledge forgetting\" may be a challenge, but rest assured, this is very normal. We can review problems according to the \"Ebbinghaus forgetting curve\", and usually after 3-5 rounds of repetition, we can firmly remember them. For recommended problem lists and practice plans, please see this GitHub repository.
      3. Stage 3: Building a knowledge system. In terms of learning, we can read algorithm column articles, problem-solving frameworks, and algorithm textbooks to continuously enrich our knowledge system. In terms of practicing problems, we can try advanced problem-solving strategies, such as categorization by topic, one problem multiple solutions, one solution multiple problems, etc. Related problem-solving insights can be found in various communities.

      As shown in Figure 0-8, the content of this book mainly covers \"Stage 1\", aiming to help you more efficiently carry out Stage 2 and Stage 3 learning.

      Figure 0-8 \u00a0 Algorithm learning roadmap

      "},{"location":"chapter_preface/summary/","title":"0.3 \u00a0 Summary","text":""},{"location":"chapter_preface/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • The main audience of this book is algorithm beginners. If you already have a certain foundation, this book can help you systematically review algorithm knowledge, and the source code in the book can also be used as a \"problem-solving toolkit.\"
      • The content of the book mainly includes three parts: complexity analysis, data structures, and algorithms, covering most topics in this field.
      • For algorithm novices, reading an introductory book during the initial learning stage is crucial, as it can help you avoid many detours.
      • The animated illustrations in the book are usually used to introduce key and difficult knowledge. When reading this book, you should pay more attention to these contents.
      • Practice is the best way to learn programming. It is strongly recommended to run the source code and type the code yourself.
      • The web version of this book has a comments section for each chapter, where you are welcome to share your questions and insights at any time.
      "},{"location":"chapter_reference/","title":"References","text":"

      [1] Thomas H. Cormen, et al. Introduction to Algorithms (3rd Edition).

      [2] Aditya Bhargava. Grokking Algorithms: An Illustrated Guide for Programmers and Other Curious People (1st Edition).

      [3] Robert Sedgewick, et al. Algorithms (4th Edition).

      [4] Yan Weimin. Data Structures (C Language Version).

      [5] Deng Junhui. Data Structures (C++ Language Version, Third Edition).

      [6] Mark Allen Weiss, translated by Chen Yue. Data Structures and Algorithm Analysis in Java (Third Edition).

      [7] Cheng Jie. Conversational Data Structures.

      [8] Wang Zheng. The Beauty of Data Structures and Algorithms.

      [9] Gayle Laakmann McDowell. Cracking the Coding Interview: 189 Programming Questions and Solutions (6th Edition).

      [10] Aston Zhang, et al. Dive into Deep Learning.

      "},{"location":"chapter_searching/","title":"Chapter 10. \u00a0 Searching","text":"

      Abstract

      Searching is an adventure into the unknown, where we may need to traverse every corner of the mysterious space, or we may be able to quickly lock onto the target.

      In this journey of discovery, each exploration may yield an unexpected answer.

      "},{"location":"chapter_searching/#chapter-contents","title":"Chapter contents","text":"
      • 10.1 \u00a0 Binary Search
      • 10.2 \u00a0 Binary Search Insertion
      • 10.3 \u00a0 Binary Search Edge Cases
      • 10.4 \u00a0 Hash Optimization Strategy
      • 10.5 \u00a0 Search Algorithms Revisited
      • 10.6 \u00a0 Summary
      "},{"location":"chapter_searching/binary_search/","title":"10.1 \u00a0 Binary Search","text":"

      Binary search is an efficient searching algorithm based on the divide-and-conquer strategy. It leverages the orderliness of data to reduce the search range by half in each round until the target element is found or the search interval becomes empty.

      Question

      Given an array nums of length \\(n\\) with elements arranged in ascending order and no duplicates, search for and return the index of element target in the array. If the array does not contain the element, return \\(-1\\). An example is shown in Figure 10-1.

      Figure 10-1 \u00a0 Binary search example data

      As shown in Figure 10-2, we first initialize pointers \\(i = 0\\) and \\(j = n - 1\\), pointing to the first and last elements of the array respectively, representing the search interval \\([0, n - 1]\\). Note that square brackets denote a closed interval, which includes the boundary values themselves.

      Next, perform the following two steps in a loop:

      1. Calculate the midpoint index \\(m = \\lfloor {(i + j) / 2} \\rfloor\\), where \\(\\lfloor \\: \\rfloor\\) denotes the floor operation.
      2. Compare nums[m] and target, which results in three cases:
        1. When nums[m] < target, it indicates that target is in the interval \\([m + 1, j]\\), so execute \\(i = m + 1\\).
        2. When nums[m] > target, it indicates that target is in the interval \\([i, m - 1]\\), so execute \\(j = m - 1\\).
        3. When nums[m] = target, it indicates that target has been found, so return index \\(m\\).

      If the array does not contain the target element, the search interval will eventually shrink to empty. In this case, return \\(-1\\).

      <1><2><3><4><5><6><7>

      Figure 10-2 \u00a0 Binary search process

      It's worth noting that since both \\(i\\) and \\(j\\) are of int type, \\(i + j\\) may exceed the range of the int type. To avoid large number overflow, we typically use the formula \\(m = \\lfloor {i + (j - i) / 2} \\rfloor\\) to calculate the midpoint.

      The code is shown below:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search.py
      def binary_search(nums: list[int], target: int) -> int:\n    \"\"\"Binary search (closed interval)\"\"\"\n    # Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n    i, j = 0, len(nums) - 1\n    # Loop, exit when the search interval is empty (empty when i > j)\n    while i <= j:\n        # In theory, Python numbers can be infinitely large (depending on memory size), no need to consider large number overflow\n        m = (i + j) // 2  # Calculate midpoint index m\n        if nums[m] < target:\n            i = m + 1  # This means target is in the interval [m+1, j]\n        elif nums[m] > target:\n            j = m - 1  # This means target is in the interval [i, m-1]\n        else:\n            return m  # Found the target element, return its index\n    return -1  # Target element not found, return -1\n
      binary_search.cpp
      /* Binary search (closed interval on both sides) */\nint binarySearch(vector<int> &nums, int target) {\n    // Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n    int i = 0, j = nums.size() - 1;\n    // Loop, exit when the search interval is empty (empty when i > j)\n    while (i <= j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target)    // This means target is in the interval [m+1, j]\n            i = m + 1;\n        else if (nums[m] > target) // This means target is in the interval [i, m-1]\n            j = m - 1;\n        else // Found the target element, return its index\n            return m;\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.java
      /* Binary search (closed interval on both sides) */\nint binarySearch(int[] nums, int target) {\n    // Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n    int i = 0, j = nums.length - 1;\n    // Loop, exit when the search interval is empty (empty when i > j)\n    while (i <= j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target) // This means target is in the interval [m+1, j]\n            i = m + 1;\n        else if (nums[m] > target) // This means target is in the interval [i, m-1]\n            j = m - 1;\n        else // Found the target element, return its index\n            return m;\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.cs
      /* Binary search (closed interval on both sides) */\nint BinarySearch(int[] nums, int target) {\n    // Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n    int i = 0, j = nums.Length - 1;\n    // Loop, exit when the search interval is empty (empty when i > j)\n    while (i <= j) {\n        int m = i + (j - i) / 2;   // Calculate the midpoint index m\n        if (nums[m] < target)      // This means target is in the interval [m+1, j]\n            i = m + 1;\n        else if (nums[m] > target) // This means target is in the interval [i, m-1]\n            j = m - 1;\n        else                       // Found the target element, return its index\n            return m;\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.go
      /* Binary search (closed interval on both sides) */\nfunc binarySearch(nums []int, target int) int {\n    // Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n    i, j := 0, len(nums)-1\n    // Loop, exit when the search interval is empty (empty when i > j)\n    for i <= j {\n        m := i + (j-i)/2      // Calculate the midpoint index m\n        if nums[m] < target { // This means target is in the interval [m+1, j]\n            i = m + 1\n        } else if nums[m] > target { // This means target is in the interval [i, m-1]\n            j = m - 1\n        } else { // Found the target element, return its index\n            return m\n        }\n    }\n    // Target element not found, return -1\n    return -1\n}\n
      binary_search.swift
      /* Binary search (closed interval on both sides) */\nfunc binarySearch(nums: [Int], target: Int) -> Int {\n    // Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n    var i = nums.startIndex\n    var j = nums.endIndex - 1\n    // Loop, exit when the search interval is empty (empty when i > j)\n    while i <= j {\n        let m = i + (j - i) / 2 // Calculate the midpoint index m\n        if nums[m] < target { // This means target is in the interval [m+1, j]\n            i = m + 1\n        } else if nums[m] > target { // This means target is in the interval [i, m-1]\n            j = m - 1\n        } else { // Found the target element, return its index\n            return m\n        }\n    }\n    // Target element not found, return -1\n    return -1\n}\n
      binary_search.js
      /* Binary search (closed interval on both sides) */\nfunction binarySearch(nums, target) {\n    // Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n    let i = 0,\n        j = nums.length - 1;\n    // Loop, exit when the search interval is empty (empty when i > j)\n    while (i <= j) {\n        // Calculate midpoint index m, use parseInt() to round down\n        const m = parseInt(i + (j - i) / 2);\n        if (nums[m] < target)\n            // This means target is in the interval [m+1, j]\n            i = m + 1;\n        else if (nums[m] > target)\n            // This means target is in the interval [i, m-1]\n            j = m - 1;\n        else return m; // Found the target element, return its index\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.ts
      /* Binary search (closed interval on both sides) */\nfunction binarySearch(nums: number[], target: number): number {\n    // Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n    let i = 0,\n        j = nums.length - 1;\n    // Loop, exit when the search interval is empty (empty when i > j)\n    while (i <= j) {\n        // Calculate the midpoint index m\n        const m = Math.floor(i + (j - i) / 2);\n        if (nums[m] < target) {\n            // This means target is in the interval [m+1, j]\n            i = m + 1;\n        } else if (nums[m] > target) {\n            // This means target is in the interval [i, m-1]\n            j = m - 1;\n        } else {\n            // Found the target element, return its index\n            return m;\n        }\n    }\n    return -1; // Target element not found, return -1\n}\n
      binary_search.dart
      /* Binary search (closed interval on both sides) */\nint binarySearch(List<int> nums, int target) {\n  // Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n  int i = 0, j = nums.length - 1;\n  // Loop, exit when the search interval is empty (empty when i > j)\n  while (i <= j) {\n    int m = i + (j - i) ~/ 2; // Calculate the midpoint index m\n    if (nums[m] < target) {\n      // This means target is in the interval [m+1, j]\n      i = m + 1;\n    } else if (nums[m] > target) {\n      // This means target is in the interval [i, m-1]\n      j = m - 1;\n    } else {\n      // Found the target element, return its index\n      return m;\n    }\n  }\n  // Target element not found, return -1\n  return -1;\n}\n
      binary_search.rs
      /* Binary search (closed interval on both sides) */\nfn binary_search(nums: &[i32], target: i32) -> i32 {\n    // Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n    let mut i = 0;\n    let mut j = nums.len() as i32 - 1;\n    // Loop, exit when the search interval is empty (empty when i > j)\n    while i <= j {\n        let m = i + (j - i) / 2; // Calculate the midpoint index m\n        if nums[m as usize] < target {\n            // This means target is in the interval [m+1, j]\n            i = m + 1;\n        } else if nums[m as usize] > target {\n            // This means target is in the interval [i, m-1]\n            j = m - 1;\n        } else {\n            // Found the target element, return its index\n            return m;\n        }\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.c
      /* Binary search (closed interval on both sides) */\nint binarySearch(int *nums, int len, int target) {\n    // Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n    int i = 0, j = len - 1;\n    // Loop, exit when the search interval is empty (empty when i > j)\n    while (i <= j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target)    // This means target is in the interval [m+1, j]\n            i = m + 1;\n        else if (nums[m] > target) // This means target is in the interval [i, m-1]\n            j = m - 1;\n        else // Found the target element, return its index\n            return m;\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.kt
      /* Binary search (closed interval on both sides) */\nfun binarySearch(nums: IntArray, target: Int): Int {\n    // Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n    var i = 0\n    var j = nums.size - 1\n    // Loop, exit when the search interval is empty (empty when i > j)\n    while (i <= j) {\n        val m = i + (j - i) / 2 // Calculate the midpoint index m\n        if (nums[m] < target) // This means target is in the interval [m+1, j]\n            i = m + 1\n        else if (nums[m] > target) // This means target is in the interval [i, m-1]\n            j = m - 1\n        else  // Found the target element, return its index\n            return m\n    }\n    // Target element not found, return -1\n    return -1\n}\n
      binary_search.rb
      ### Binary search (closed interval) ###\ndef binary_search(nums, target)\n  # Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array\n  i, j = 0, nums.length - 1\n\n  # Loop, exit when the search interval is empty (empty when i > j)\n  while i <= j\n    # In theory, Ruby numbers can be infinitely large (limited by memory), no need to consider overflow\n    m = (i + j) / 2   # Calculate the midpoint index m\n\n    if nums[m] < target\n      i = m + 1 # This means target is in the interval [m+1, j]\n    elsif nums[m] > target\n      j = m - 1 # This means target is in the interval [i, m-1]\n    else\n      return m  # Found the target element, return its index\n    end\n  end\n\n  -1  # Target element not found, return -1\nend\n

      Time complexity is \\(O(\\log n)\\): In the binary loop, the interval is reduced by half each round, so the number of loops is \\(\\log_2 n\\).

      Space complexity is \\(O(1)\\): Pointers \\(i\\) and \\(j\\) use constant-size space.

      "},{"location":"chapter_searching/binary_search/#1011-interval-representation-methods","title":"10.1.1 \u00a0 Interval Representation Methods","text":"

      In addition to the closed interval mentioned above, another common interval representation is the \"left-closed right-open\" interval, defined as \\([0, n)\\), meaning the left boundary includes itself while the right boundary does not. Under this representation, the interval \\([i, j)\\) is empty when \\(i = j\\).

      We can implement a binary search algorithm with the same functionality based on this representation:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search.py
      def binary_search_lcro(nums: list[int], target: int) -> int:\n    \"\"\"Binary search (left-closed right-open interval)\"\"\"\n    # Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n    i, j = 0, len(nums)\n    # Loop, exit when the search interval is empty (empty when i = j)\n    while i < j:\n        m = (i + j) // 2  # Calculate midpoint index m\n        if nums[m] < target:\n            i = m + 1  # This means target is in the interval [m+1, j)\n        elif nums[m] > target:\n            j = m  # This means target is in the interval [i, m)\n        else:\n            return m  # Found the target element, return its index\n    return -1  # Target element not found, return -1\n
      binary_search.cpp
      /* Binary search (left-closed right-open interval) */\nint binarySearchLCRO(vector<int> &nums, int target) {\n    // Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n    int i = 0, j = nums.size();\n    // Loop, exit when the search interval is empty (empty when i = j)\n    while (i < j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target)    // This means target is in the interval [m+1, j)\n            i = m + 1;\n        else if (nums[m] > target) // This means target is in the interval [i, m)\n            j = m;\n        else // Found the target element, return its index\n            return m;\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.java
      /* Binary search (left-closed right-open interval) */\nint binarySearchLCRO(int[] nums, int target) {\n    // Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n    int i = 0, j = nums.length;\n    // Loop, exit when the search interval is empty (empty when i = j)\n    while (i < j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target) // This means target is in the interval [m+1, j)\n            i = m + 1;\n        else if (nums[m] > target) // This means target is in the interval [i, m)\n            j = m;\n        else // Found the target element, return its index\n            return m;\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.cs
      /* Binary search (left-closed right-open interval) */\nint BinarySearchLCRO(int[] nums, int target) {\n    // Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n    int i = 0, j = nums.Length;\n    // Loop, exit when the search interval is empty (empty when i = j)\n    while (i < j) {\n        int m = i + (j - i) / 2;   // Calculate the midpoint index m\n        if (nums[m] < target)      // This means target is in the interval [m+1, j)\n            i = m + 1;\n        else if (nums[m] > target) // This means target is in the interval [i, m)\n            j = m;\n        else                       // Found the target element, return its index\n            return m;\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.go
      /* Binary search (left-closed right-open interval) */\nfunc binarySearchLCRO(nums []int, target int) int {\n    // Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n    i, j := 0, len(nums)\n    // Loop, exit when the search interval is empty (empty when i = j)\n    for i < j {\n        m := i + (j-i)/2      // Calculate the midpoint index m\n        if nums[m] < target { // This means target is in the interval [m+1, j)\n            i = m + 1\n        } else if nums[m] > target { // This means target is in the interval [i, m)\n            j = m\n        } else { // Found the target element, return its index\n            return m\n        }\n    }\n    // Target element not found, return -1\n    return -1\n}\n
      binary_search.swift
      /* Binary search (left-closed right-open interval) */\nfunc binarySearchLCRO(nums: [Int], target: Int) -> Int {\n    // Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n    var i = nums.startIndex\n    var j = nums.endIndex\n    // Loop, exit when the search interval is empty (empty when i = j)\n    while i < j {\n        let m = i + (j - i) / 2 // Calculate the midpoint index m\n        if nums[m] < target { // This means target is in the interval [m+1, j)\n            i = m + 1\n        } else if nums[m] > target { // This means target is in the interval [i, m)\n            j = m\n        } else { // Found the target element, return its index\n            return m\n        }\n    }\n    // Target element not found, return -1\n    return -1\n}\n
      binary_search.js
      /* Binary search (left-closed right-open interval) */\nfunction binarySearchLCRO(nums, target) {\n    // Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n    let i = 0,\n        j = nums.length;\n    // Loop, exit when the search interval is empty (empty when i = j)\n    while (i < j) {\n        // Calculate midpoint index m, use parseInt() to round down\n        const m = parseInt(i + (j - i) / 2);\n        if (nums[m] < target)\n            // This means target is in the interval [m+1, j)\n            i = m + 1;\n        else if (nums[m] > target)\n            // This means target is in the interval [i, m)\n            j = m;\n        // Found the target element, return its index\n        else return m;\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.ts
      /* Binary search (left-closed right-open interval) */\nfunction binarySearchLCRO(nums: number[], target: number): number {\n    // Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n    let i = 0,\n        j = nums.length;\n    // Loop, exit when the search interval is empty (empty when i = j)\n    while (i < j) {\n        // Calculate the midpoint index m\n        const m = Math.floor(i + (j - i) / 2);\n        if (nums[m] < target) {\n            // This means target is in the interval [m+1, j)\n            i = m + 1;\n        } else if (nums[m] > target) {\n            // This means target is in the interval [i, m)\n            j = m;\n        } else {\n            // Found the target element, return its index\n            return m;\n        }\n    }\n    return -1; // Target element not found, return -1\n}\n
      binary_search.dart
      /* Binary search (left-closed right-open interval) */\nint binarySearchLCRO(List<int> nums, int target) {\n  // Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n  int i = 0, j = nums.length;\n  // Loop, exit when the search interval is empty (empty when i = j)\n  while (i < j) {\n    int m = i + (j - i) ~/ 2; // Calculate the midpoint index m\n    if (nums[m] < target) {\n      // This means target is in the interval [m+1, j)\n      i = m + 1;\n    } else if (nums[m] > target) {\n      // This means target is in the interval [i, m)\n      j = m;\n    } else {\n      // Found the target element, return its index\n      return m;\n    }\n  }\n  // Target element not found, return -1\n  return -1;\n}\n
      binary_search.rs
      /* Binary search (left-closed right-open interval) */\nfn binary_search_lcro(nums: &[i32], target: i32) -> i32 {\n    // Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n    let mut i = 0;\n    let mut j = nums.len() as i32;\n    // Loop, exit when the search interval is empty (empty when i = j)\n    while i < j {\n        let m = i + (j - i) / 2; // Calculate the midpoint index m\n        if nums[m as usize] < target {\n            // This means target is in the interval [m+1, j)\n            i = m + 1;\n        } else if nums[m as usize] > target {\n            // This means target is in the interval [i, m)\n            j = m;\n        } else {\n            // Found the target element, return its index\n            return m;\n        }\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.c
      /* Binary search (left-closed right-open interval) */\nint binarySearchLCRO(int *nums, int len, int target) {\n    // Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n    int i = 0, j = len;\n    // Loop, exit when the search interval is empty (empty when i = j)\n    while (i < j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target)    // This means target is in the interval [m+1, j)\n            i = m + 1;\n        else if (nums[m] > target) // This means target is in the interval [i, m)\n            j = m;\n        else // Found the target element, return its index\n            return m;\n    }\n    // Target element not found, return -1\n    return -1;\n}\n
      binary_search.kt
      /* Binary search (left-closed right-open interval) */\nfun binarySearchLCRO(nums: IntArray, target: Int): Int {\n    // Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n    var i = 0\n    var j = nums.size\n    // Loop, exit when the search interval is empty (empty when i = j)\n    while (i < j) {\n        val m = i + (j - i) / 2 // Calculate the midpoint index m\n        if (nums[m] < target) // This means target is in the interval [m+1, j)\n            i = m + 1\n        else if (nums[m] > target) // This means target is in the interval [i, m)\n            j = m\n        else  // Found the target element, return its index\n            return m\n    }\n    // Target element not found, return -1\n    return -1\n}\n
      binary_search.rb
      ### Binary search (left-closed right-open interval) ###\ndef binary_search_lcro(nums, target)\n  # Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1\n  i, j = 0, nums.length\n\n  # Loop, exit when the search interval is empty (empty when i = j)\n  while i < j\n    # Calculate the midpoint index m\n    m = (i + j) / 2\n\n    if nums[m] < target\n      i = m + 1 # This means target is in the interval [m+1, j)\n    elsif nums[m] > target\n      j = m - 1 # This means target is in the interval [i, m)\n    else\n      return m  # Found the target element, return its index\n    end\n  end\n\n  -1  # Target element not found, return -1\nend\n

      As shown in Figure 10-3, under the two interval representations, the initialization, loop condition, and interval narrowing operations of the binary search algorithm are all different.

      Since both the left and right boundaries in the \"closed interval\" representation are defined as closed, the operations to narrow the interval through pointers \\(i\\) and \\(j\\) are also symmetric. This makes it less error-prone, so the \"closed interval\" approach is generally recommended.

      Figure 10-3 \u00a0 Two interval definitions

      "},{"location":"chapter_searching/binary_search/#1012-advantages-and-limitations","title":"10.1.2 \u00a0 Advantages and Limitations","text":"

      Binary search performs well in both time and space aspects.

      • Binary search has high time efficiency. With large data volumes, the logarithmic time complexity has significant advantages. For example, when the data size \\(n = 2^{20}\\), linear search requires \\(2^{20} = 1048576\\) loop rounds, while binary search only needs \\(\\log_2 2^{20} = 20\\) rounds.
      • Binary search requires no extra space. Compared to searching algorithms that require additional space (such as hash-based search), binary search is more space-efficient.

      However, binary search is not suitable for all situations, mainly for the following reasons:

      • Binary search is only applicable to sorted data. If the input data is unsorted, sorting specifically to use binary search would be counterproductive, as sorting algorithms typically have a time complexity of \\(O(n \\log n)\\), which is higher than both linear search and binary search. For scenarios with frequent element insertions, maintaining array orderliness requires inserting elements at specific positions with a time complexity of \\(O(n)\\), which is also very expensive.
      • Binary search is only applicable to arrays. Binary search requires jump-style (non-contiguous) element access, and jump-style access has low efficiency in linked lists, making it unsuitable for linked lists or data structures based on linked list implementations.
      • For small data volumes, linear search performs better. In linear search, each round requires only 1 comparison operation; while in binary search, it requires 1 addition, 1 division, 1-3 comparison operations, and 1 addition (subtraction), totaling 4-6 unit operations. Therefore, when the data volume \\(n\\) is small, linear search is actually faster than binary search.
      "},{"location":"chapter_searching/binary_search_edge/","title":"10.3 \u00a0 Binary Search Edge Cases","text":""},{"location":"chapter_searching/binary_search_edge/#1031-finding-the-left-boundary","title":"10.3.1 \u00a0 Finding the Left Boundary","text":"

      Question

      Given a sorted array nums of length \\(n\\) that may contain duplicate elements, return the index of the leftmost element target in the array. If the array does not contain the element, return \\(-1\\).

      Recall the method for finding the insertion point with binary search. After the search completes, \\(i\\) points to the leftmost target, so finding the insertion point is essentially finding the index of the leftmost target.

      Consider implementing the left boundary search using the insertion point finding function. Note that the array may not contain target, which could result in the following two cases:

      • The insertion point index \\(i\\) is out of bounds.
      • The element nums[i] is not equal to target.

      When either of these situations occurs, simply return \\(-1\\). The code is shown below:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_edge.py
      def binary_search_left_edge(nums: list[int], target: int) -> int:\n    \"\"\"Binary search for the leftmost target\"\"\"\n    # Equivalent to finding the insertion point of target\n    i = binary_search_insertion(nums, target)\n    # Target not found, return -1\n    if i == len(nums) or nums[i] != target:\n        return -1\n    # Found target, return index i\n    return i\n
      binary_search_edge.cpp
      /* Binary search for the leftmost target */\nint binarySearchLeftEdge(vector<int> &nums, int target) {\n    // Equivalent to finding the insertion point of target\n    int i = binarySearchInsertion(nums, target);\n    // Target not found, return -1\n    if (i == nums.size() || nums[i] != target) {\n        return -1;\n    }\n    // Found target, return index i\n    return i;\n}\n
      binary_search_edge.java
      /* Binary search for the leftmost target */\nint binarySearchLeftEdge(int[] nums, int target) {\n    // Equivalent to finding the insertion point of target\n    int i = binary_search_insertion.binarySearchInsertion(nums, target);\n    // Target not found, return -1\n    if (i == nums.length || nums[i] != target) {\n        return -1;\n    }\n    // Found target, return index i\n    return i;\n}\n
      binary_search_edge.cs
      /* Binary search for the leftmost target */\nint BinarySearchLeftEdge(int[] nums, int target) {\n    // Equivalent to finding the insertion point of target\n    int i = binary_search_insertion.BinarySearchInsertion(nums, target);\n    // Target not found, return -1\n    if (i == nums.Length || nums[i] != target) {\n        return -1;\n    }\n    // Found target, return index i\n    return i;\n}\n
      binary_search_edge.go
      /* Binary search for the leftmost target */\nfunc binarySearchLeftEdge(nums []int, target int) int {\n    // Equivalent to finding the insertion point of target\n    i := binarySearchInsertion(nums, target)\n    // Target not found, return -1\n    if i == len(nums) || nums[i] != target {\n        return -1\n    }\n    // Found target, return index i\n    return i\n}\n
      binary_search_edge.swift
      /* Binary search for the leftmost target */\nfunc binarySearchLeftEdge(nums: [Int], target: Int) -> Int {\n    // Equivalent to finding the insertion point of target\n    let i = binarySearchInsertion(nums: nums, target: target)\n    // Target not found, return -1\n    if i == nums.endIndex || nums[i] != target {\n        return -1\n    }\n    // Found target, return index i\n    return i\n}\n
      binary_search_edge.js
      /* Binary search for the leftmost target */\nfunction binarySearchLeftEdge(nums, target) {\n    // Equivalent to finding the insertion point of target\n    const i = binarySearchInsertion(nums, target);\n    // Target not found, return -1\n    if (i === nums.length || nums[i] !== target) {\n        return -1;\n    }\n    // Found target, return index i\n    return i;\n}\n
      binary_search_edge.ts
      /* Binary search for the leftmost target */\nfunction binarySearchLeftEdge(nums: Array<number>, target: number): number {\n    // Equivalent to finding the insertion point of target\n    const i = binarySearchInsertion(nums, target);\n    // Target not found, return -1\n    if (i === nums.length || nums[i] !== target) {\n        return -1;\n    }\n    // Found target, return index i\n    return i;\n}\n
      binary_search_edge.dart
      /* Binary search for the leftmost target */\nint binarySearchLeftEdge(List<int> nums, int target) {\n  // Equivalent to finding the insertion point of target\n  int i = binarySearchInsertion(nums, target);\n  // Target not found, return -1\n  if (i == nums.length || nums[i] != target) {\n    return -1;\n  }\n  // Found target, return index i\n  return i;\n}\n
      binary_search_edge.rs
      /* Binary search for the leftmost target */\nfn binary_search_left_edge(nums: &[i32], target: i32) -> i32 {\n    // Equivalent to finding the insertion point of target\n    let i = binary_search_insertion(nums, target);\n    // Target not found, return -1\n    if i == nums.len() as i32 || nums[i as usize] != target {\n        return -1;\n    }\n    // Found target, return index i\n    i\n}\n
      binary_search_edge.c
      /* Binary search for the leftmost target */\nint binarySearchLeftEdge(int *nums, int numSize, int target) {\n    // Equivalent to finding the insertion point of target\n    int i = binarySearchInsertion(nums, numSize, target);\n    // Target not found, return -1\n    if (i == numSize || nums[i] != target) {\n        return -1;\n    }\n    // Found target, return index i\n    return i;\n}\n
      binary_search_edge.kt
      /* Binary search for the leftmost target */\nfun binarySearchLeftEdge(nums: IntArray, target: Int): Int {\n    // Equivalent to finding the insertion point of target\n    val i = binarySearchInsertion(nums, target)\n    // Target not found, return -1\n    if (i == nums.size || nums[i] != target) {\n        return -1\n    }\n    // Found target, return index i\n    return i\n}\n
      binary_search_edge.rb
      ### Binary search leftmost target ###\ndef binary_search_left_edge(nums, target)\n  # Equivalent to finding the insertion point of target\n  i = binary_search_insertion(nums, target)\n\n  # Target not found, return -1\n  return -1 if i == nums.length || nums[i] != target\n\n  i # Found target, return index i\nend\n
      "},{"location":"chapter_searching/binary_search_edge/#1032-finding-the-right-boundary","title":"10.3.2 \u00a0 Finding the Right Boundary","text":"

      So how do we find the rightmost target? The most direct approach is to modify the code and replace the pointer shrinking operation in the nums[m] == target case. The code is omitted here; interested readers can implement it themselves.

      Below we introduce two more clever methods.

      "},{"location":"chapter_searching/binary_search_edge/#1-reusing-left-boundary-search","title":"1. \u00a0 Reusing Left Boundary Search","text":"

      In fact, we can use the function for finding the leftmost element to find the rightmost element. The specific method is: Convert finding the rightmost target into finding the leftmost target + 1.

      As shown in Figure 10-7, after the search completes, pointer \\(i\\) points to the leftmost target + 1 (if it exists), while \\(j\\) points to the rightmost target, so we can simply return \\(j\\).

      Figure 10-7 \u00a0 Converting right boundary search to left boundary search

      Note that the returned insertion point is \\(i\\), so we need to subtract \\(1\\) from it to obtain \\(j\\):

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_edge.py
      def binary_search_right_edge(nums: list[int], target: int) -> int:\n    \"\"\"Binary search for the rightmost target\"\"\"\n    # Convert to finding the leftmost target + 1\n    i = binary_search_insertion(nums, target + 1)\n    # j points to the rightmost target, i points to the first element greater than target\n    j = i - 1\n    # Target not found, return -1\n    if j == -1 or nums[j] != target:\n        return -1\n    # Found target, return index j\n    return j\n
      binary_search_edge.cpp
      /* Binary search for the rightmost target */\nint binarySearchRightEdge(vector<int> &nums, int target) {\n    // Convert to finding the leftmost target + 1\n    int i = binarySearchInsertion(nums, target + 1);\n    // j points to the rightmost target, i points to the first element greater than target\n    int j = i - 1;\n    // Target not found, return -1\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // Found target, return index j\n    return j;\n}\n
      binary_search_edge.java
      /* Binary search for the rightmost target */\nint binarySearchRightEdge(int[] nums, int target) {\n    // Convert to finding the leftmost target + 1\n    int i = binary_search_insertion.binarySearchInsertion(nums, target + 1);\n    // j points to the rightmost target, i points to the first element greater than target\n    int j = i - 1;\n    // Target not found, return -1\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // Found target, return index j\n    return j;\n}\n
      binary_search_edge.cs
      /* Binary search for the rightmost target */\nint BinarySearchRightEdge(int[] nums, int target) {\n    // Convert to finding the leftmost target + 1\n    int i = binary_search_insertion.BinarySearchInsertion(nums, target + 1);\n    // j points to the rightmost target, i points to the first element greater than target\n    int j = i - 1;\n    // Target not found, return -1\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // Found target, return index j\n    return j;\n}\n
      binary_search_edge.go
      /* Binary search for the rightmost target */\nfunc binarySearchRightEdge(nums []int, target int) int {\n    // Convert to finding the leftmost target + 1\n    i := binarySearchInsertion(nums, target+1)\n    // j points to the rightmost target, i points to the first element greater than target\n    j := i - 1\n    // Target not found, return -1\n    if j == -1 || nums[j] != target {\n        return -1\n    }\n    // Found target, return index j\n    return j\n}\n
      binary_search_edge.swift
      /* Binary search for the rightmost target */\nfunc binarySearchRightEdge(nums: [Int], target: Int) -> Int {\n    // Convert to finding the leftmost target + 1\n    let i = binarySearchInsertion(nums: nums, target: target + 1)\n    // j points to the rightmost target, i points to the first element greater than target\n    let j = i - 1\n    // Target not found, return -1\n    if j == -1 || nums[j] != target {\n        return -1\n    }\n    // Found target, return index j\n    return j\n}\n
      binary_search_edge.js
      /* Binary search for the rightmost target */\nfunction binarySearchRightEdge(nums, target) {\n    // Convert to finding the leftmost target + 1\n    const i = binarySearchInsertion(nums, target + 1);\n    // j points to the rightmost target, i points to the first element greater than target\n    const j = i - 1;\n    // Target not found, return -1\n    if (j === -1 || nums[j] !== target) {\n        return -1;\n    }\n    // Found target, return index j\n    return j;\n}\n
      binary_search_edge.ts
      /* Binary search for the rightmost target */\nfunction binarySearchRightEdge(nums: Array<number>, target: number): number {\n    // Convert to finding the leftmost target + 1\n    const i = binarySearchInsertion(nums, target + 1);\n    // j points to the rightmost target, i points to the first element greater than target\n    const j = i - 1;\n    // Target not found, return -1\n    if (j === -1 || nums[j] !== target) {\n        return -1;\n    }\n    // Found target, return index j\n    return j;\n}\n
      binary_search_edge.dart
      /* Binary search for the rightmost target */\nint binarySearchRightEdge(List<int> nums, int target) {\n  // Convert to finding the leftmost target + 1\n  int i = binarySearchInsertion(nums, target + 1);\n  // j points to the rightmost target, i points to the first element greater than target\n  int j = i - 1;\n  // Target not found, return -1\n  if (j == -1 || nums[j] != target) {\n    return -1;\n  }\n  // Found target, return index j\n  return j;\n}\n
      binary_search_edge.rs
      /* Binary search for the rightmost target */\nfn binary_search_right_edge(nums: &[i32], target: i32) -> i32 {\n    // Convert to finding the leftmost target + 1\n    let i = binary_search_insertion(nums, target + 1);\n    // j points to the rightmost target, i points to the first element greater than target\n    let j = i - 1;\n    // Target not found, return -1\n    if j == -1 || nums[j as usize] != target {\n        return -1;\n    }\n    // Found target, return index j\n    j\n}\n
      binary_search_edge.c
      /* Binary search for the rightmost target */\nint binarySearchRightEdge(int *nums, int numSize, int target) {\n    // Convert to finding the leftmost target + 1\n    int i = binarySearchInsertion(nums, numSize, target + 1);\n    // j points to the rightmost target, i points to the first element greater than target\n    int j = i - 1;\n    // Target not found, return -1\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // Found target, return index j\n    return j;\n}\n
      binary_search_edge.kt
      /* Binary search for the rightmost target */\nfun binarySearchRightEdge(nums: IntArray, target: Int): Int {\n    // Convert to finding the leftmost target + 1\n    val i = binarySearchInsertion(nums, target + 1)\n    // j points to the rightmost target, i points to the first element greater than target\n    val j = i - 1\n    // Target not found, return -1\n    if (j == -1 || nums[j] != target) {\n        return -1\n    }\n    // Found target, return index j\n    return j\n}\n
      binary_search_edge.rb
      ### Binary search rightmost target ###\ndef binary_search_right_edge(nums, target)\n  # Convert to finding the leftmost target + 1\n  i = binary_search_insertion(nums, target + 1)\n\n  # j points to the rightmost target, i points to the first element greater than target\n  j = i - 1\n\n  # Target not found, return -1\n  return -1 if j == -1 || nums[j] != target\n\n  j # Found target, return index j\nend\n
      "},{"location":"chapter_searching/binary_search_edge/#2-converting-to-element-search","title":"2. \u00a0 Converting to Element Search","text":"

      We know that when the array does not contain target, \\(i\\) and \\(j\\) will eventually point to the first elements greater than and less than target, respectively.

      Therefore, as shown in Figure 10-8, we can construct an element that does not exist in the array to find the left and right boundaries.

      • Finding the leftmost target: Can be converted to finding target - 0.5 and returning pointer \\(i\\).
      • Finding the rightmost target: Can be converted to finding target + 0.5 and returning pointer \\(j\\).

      Figure 10-8 \u00a0 Converting boundary search to element search

      The code is omitted here, but the following two points are worth noting:

      • Since the given array does not contain decimals, we don't need to worry about how to handle equal cases.
      • Because this method introduces decimals, the variable target in the function needs to be changed to a floating-point type (Python does not require this change).
      "},{"location":"chapter_searching/binary_search_insertion/","title":"10.2 \u00a0 Binary Search Insertion Point","text":"

      Binary search can not only be used to search for target elements but also to solve many variant problems, such as searching for the insertion position of a target element.

      "},{"location":"chapter_searching/binary_search_insertion/#1021-case-without-duplicate-elements","title":"10.2.1 \u00a0 Case Without Duplicate Elements","text":"

      Question

      Given a sorted array nums of length \\(n\\) and an element target, where the array contains no duplicate elements. Insert target into the array nums while maintaining its sorted order. If the array already contains the element target, insert it to its left. Return the index of target in the array after insertion. An example is shown in Figure 10-4.

      Figure 10-4 \u00a0 Binary search insertion point example data

      If we want to reuse the binary search code from the previous section, we need to answer the following two questions.

      Question 1: When the array contains target, is the insertion point index the same as that element's index?

      The problem requires inserting target to the left of equal elements, which means the newly inserted target replaces the position of the original target. In other words, when the array contains target, the insertion point index is the index of that target.

      Question 2: When the array does not contain target, what is the insertion point index?

      Further consider the binary search process: When nums[m] < target, \\(i\\) moves, which means pointer \\(i\\) is approaching elements greater than or equal to target. Similarly, pointer \\(j\\) is always approaching elements less than or equal to target.

      Therefore, when the binary search ends, we must have: \\(i\\) points to the first element greater than target, and \\(j\\) points to the first element less than target. It's easy to see that when the array does not contain target, the insertion index is \\(i\\). The code is shown below:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_insertion.py
      def binary_search_insertion_simple(nums: list[int], target: int) -> int:\n    \"\"\"Binary search for insertion point (no duplicate elements)\"\"\"\n    i, j = 0, len(nums) - 1  # Initialize closed interval [0, n-1]\n    while i <= j:\n        m = (i + j) // 2  # Calculate midpoint index m\n        if nums[m] < target:\n            i = m + 1  # target is in the interval [m+1, j]\n        elif nums[m] > target:\n            j = m - 1  # target is in the interval [i, m-1]\n        else:\n            return m  # Found target, return insertion point m\n    # Target not found, return insertion point i\n    return i\n
      binary_search_insertion.cpp
      /* Binary search for insertion point (no duplicate elements) */\nint binarySearchInsertionSimple(vector<int> &nums, int target) {\n    int i = 0, j = nums.size() - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            return m; // Found target, return insertion point m\n        }\n    }\n    // Target not found, return insertion point i\n    return i;\n}\n
      binary_search_insertion.java
      /* Binary search for insertion point (no duplicate elements) */\nint binarySearchInsertionSimple(int[] nums, int target) {\n    int i = 0, j = nums.length - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            return m; // Found target, return insertion point m\n        }\n    }\n    // Target not found, return insertion point i\n    return i;\n}\n
      binary_search_insertion.cs
      /* Binary search for insertion point (no duplicate elements) */\nint BinarySearchInsertionSimple(int[] nums, int target) {\n    int i = 0, j = nums.Length - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            return m; // Found target, return insertion point m\n        }\n    }\n    // Target not found, return insertion point i\n    return i;\n}\n
      binary_search_insertion.go
      /* Binary search for insertion point (no duplicate elements) */\nfunc binarySearchInsertionSimple(nums []int, target int) int {\n    // Initialize closed interval [0, n-1]\n    i, j := 0, len(nums)-1\n    for i <= j {\n        // Calculate the midpoint index m\n        m := i + (j-i)/2\n        if nums[m] < target {\n            // target is in the interval [m+1, j]\n            i = m + 1\n        } else if nums[m] > target {\n            // target is in the interval [i, m-1]\n            j = m - 1\n        } else {\n            // Found target, return insertion point m\n            return m\n        }\n    }\n    // Target not found, return insertion point i\n    return i\n}\n
      binary_search_insertion.swift
      /* Binary search for insertion point (no duplicate elements) */\nfunc binarySearchInsertionSimple(nums: [Int], target: Int) -> Int {\n    // Initialize closed interval [0, n-1]\n    var i = nums.startIndex\n    var j = nums.endIndex - 1\n    while i <= j {\n        let m = i + (j - i) / 2 // Calculate the midpoint index m\n        if nums[m] < target {\n            i = m + 1 // target is in the interval [m+1, j]\n        } else if nums[m] > target {\n            j = m - 1 // target is in the interval [i, m-1]\n        } else {\n            return m // Found target, return insertion point m\n        }\n    }\n    // Target not found, return insertion point i\n    return i\n}\n
      binary_search_insertion.js
      /* Binary search for insertion point (no duplicate elements) */\nfunction binarySearchInsertionSimple(nums, target) {\n    let i = 0,\n        j = nums.length - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        const m = Math.floor(i + (j - i) / 2); // Calculate midpoint index m, use Math.floor() to round down\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            return m; // Found target, return insertion point m\n        }\n    }\n    // Target not found, return insertion point i\n    return i;\n}\n
      binary_search_insertion.ts
      /* Binary search for insertion point (no duplicate elements) */\nfunction binarySearchInsertionSimple(\n    nums: Array<number>,\n    target: number\n): number {\n    let i = 0,\n        j = nums.length - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        const m = Math.floor(i + (j - i) / 2); // Calculate midpoint index m, use Math.floor() to round down\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            return m; // Found target, return insertion point m\n        }\n    }\n    // Target not found, return insertion point i\n    return i;\n}\n
      binary_search_insertion.dart
      /* Binary search for insertion point (no duplicate elements) */\nint binarySearchInsertionSimple(List<int> nums, int target) {\n  int i = 0, j = nums.length - 1; // Initialize closed interval [0, n-1]\n  while (i <= j) {\n    int m = i + (j - i) ~/ 2; // Calculate the midpoint index m\n    if (nums[m] < target) {\n      i = m + 1; // target is in the interval [m+1, j]\n    } else if (nums[m] > target) {\n      j = m - 1; // target is in the interval [i, m-1]\n    } else {\n      return m; // Found target, return insertion point m\n    }\n  }\n  // Target not found, return insertion point i\n  return i;\n}\n
      binary_search_insertion.rs
      /* Binary search for insertion point (no duplicate elements) */\nfn binary_search_insertion_simple(nums: &[i32], target: i32) -> i32 {\n    let (mut i, mut j) = (0, nums.len() as i32 - 1); // Initialize closed interval [0, n-1]\n    while i <= j {\n        let m = i + (j - i) / 2; // Calculate the midpoint index m\n        if nums[m as usize] < target {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if nums[m as usize] > target {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            return m;\n        }\n    }\n    // Target not found, return insertion point i\n    i\n}\n
      binary_search_insertion.c
      /* Binary search for insertion point (no duplicate elements) */\nint binarySearchInsertionSimple(int *nums, int numSize, int target) {\n    int i = 0, j = numSize - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            return m; // Found target, return insertion point m\n        }\n    }\n    // Target not found, return insertion point i\n    return i;\n}\n
      binary_search_insertion.kt
      /* Binary search for insertion point (no duplicate elements) */\nfun binarySearchInsertionSimple(nums: IntArray, target: Int): Int {\n    var i = 0\n    var j = nums.size - 1 // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        val m = i + (j - i) / 2 // Calculate the midpoint index m\n        if (nums[m] < target) {\n            i = m + 1 // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1 // target is in the interval [i, m-1]\n        } else {\n            return m // Found target, return insertion point m\n        }\n    }\n    // Target not found, return insertion point i\n    return i\n}\n
      binary_search_insertion.rb
      ### Binary search insertion point (no duplicates) ###\ndef binary_search_insertion_simple(nums, target)\n  # Initialize closed interval [0, n-1]\n  i, j = 0, nums.length - 1\n\n  while i <= j\n    # Calculate the midpoint index m\n    m = (i + j) / 2\n\n    if nums[m] < target\n      i = m + 1 # target is in the interval [m+1, j]\n    elsif nums[m] > target\n      j = m - 1 # target is in the interval [i, m-1]\n    else\n      return m  # Found target, return insertion point m\n    end\n  end\n\n  i # Target not found, return insertion point i\nend\n
      "},{"location":"chapter_searching/binary_search_insertion/#1022-case-with-duplicate-elements","title":"10.2.2 \u00a0 Case with Duplicate Elements","text":"

      Question

      Based on the previous problem, assume the array may contain duplicate elements, with everything else remaining the same.

      Suppose there are multiple target elements in the array. Ordinary binary search can only return the index of one target, and cannot determine how many target elements are to the left and right of that element.

      The problem requires inserting the target element at the leftmost position, so we need to find the index of the leftmost target in the array. Initially, consider implementing this through the steps shown in Figure 10-5:

      1. Perform binary search to obtain the index of any target, denoted as \\(k\\).
      2. Starting from index \\(k\\), perform linear traversal to the left, and return when the leftmost target is found.

      Figure 10-5 \u00a0 Linear search for insertion point of duplicate elements

      Although this method works, it includes linear search, resulting in a time complexity of \\(O(n)\\). When the array contains many duplicate target elements, this method is very inefficient.

      Now consider extending the binary search code. As shown in Figure 10-6, the overall process remains unchanged: calculate the midpoint index \\(m\\) in each round, then compare target with nums[m], divided into the following cases:

      • When nums[m] < target or nums[m] > target, it means target has not been found yet, so use the ordinary binary search interval narrowing operation to make pointers \\(i\\) and \\(j\\) approach target.
      • When nums[m] == target, it means elements less than target are in the interval \\([i, m - 1]\\), so use \\(j = m - 1\\) to narrow the interval, thereby making pointer \\(j\\) approach elements less than target.

      After the loop completes, \\(i\\) points to the leftmost target, and \\(j\\) points to the first element less than target, so index \\(i\\) is the insertion point.

      <1><2><3><4><5><6><7><8>

      Figure 10-6 \u00a0 Steps for binary search insertion point of duplicate elements

      Observe the following code: the operations for branches nums[m] > target and nums[m] == target are the same, so the two can be merged.

      Even so, we can still keep the conditional branches expanded, as the logic is clearer and more readable.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_insertion.py
      def binary_search_insertion(nums: list[int], target: int) -> int:\n    \"\"\"Binary search for insertion point (with duplicate elements)\"\"\"\n    i, j = 0, len(nums) - 1  # Initialize closed interval [0, n-1]\n    while i <= j:\n        m = (i + j) // 2  # Calculate midpoint index m\n        if nums[m] < target:\n            i = m + 1  # target is in the interval [m+1, j]\n        elif nums[m] > target:\n            j = m - 1  # target is in the interval [i, m-1]\n        else:\n            j = m - 1  # The first element less than target is in the interval [i, m-1]\n    # Return insertion point i\n    return i\n
      binary_search_insertion.cpp
      /* Binary search for insertion point (with duplicate elements) */\nint binarySearchInsertion(vector<int> &nums, int target) {\n    int i = 0, j = nums.size() - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            j = m - 1; // The first element less than target is in the interval [i, m-1]\n        }\n    }\n    // Return insertion point i\n    return i;\n}\n
      binary_search_insertion.java
      /* Binary search for insertion point (with duplicate elements) */\nint binarySearchInsertion(int[] nums, int target) {\n    int i = 0, j = nums.length - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            j = m - 1; // The first element less than target is in the interval [i, m-1]\n        }\n    }\n    // Return insertion point i\n    return i;\n}\n
      binary_search_insertion.cs
      /* Binary search for insertion point (with duplicate elements) */\nint BinarySearchInsertion(int[] nums, int target) {\n    int i = 0, j = nums.Length - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            j = m - 1; // The first element less than target is in the interval [i, m-1]\n        }\n    }\n    // Return insertion point i\n    return i;\n}\n
      binary_search_insertion.go
      /* Binary search for insertion point (with duplicate elements) */\nfunc binarySearchInsertion(nums []int, target int) int {\n    // Initialize closed interval [0, n-1]\n    i, j := 0, len(nums)-1\n    for i <= j {\n        // Calculate the midpoint index m\n        m := i + (j-i)/2\n        if nums[m] < target {\n            // target is in the interval [m+1, j]\n            i = m + 1\n        } else if nums[m] > target {\n            // target is in the interval [i, m-1]\n            j = m - 1\n        } else {\n            // The first element less than target is in the interval [i, m-1]\n            j = m - 1\n        }\n    }\n    // Return insertion point i\n    return i\n}\n
      binary_search_insertion.swift
      /* Binary search for insertion point (with duplicate elements) */\nfunc binarySearchInsertion(nums: [Int], target: Int) -> Int {\n    // Initialize closed interval [0, n-1]\n    var i = nums.startIndex\n    var j = nums.endIndex - 1\n    while i <= j {\n        let m = i + (j - i) / 2 // Calculate the midpoint index m\n        if nums[m] < target {\n            i = m + 1 // target is in the interval [m+1, j]\n        } else if nums[m] > target {\n            j = m - 1 // target is in the interval [i, m-1]\n        } else {\n            j = m - 1 // The first element less than target is in the interval [i, m-1]\n        }\n    }\n    // Return insertion point i\n    return i\n}\n
      binary_search_insertion.js
      /* Binary search for insertion point (with duplicate elements) */\nfunction binarySearchInsertion(nums, target) {\n    let i = 0,\n        j = nums.length - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        const m = Math.floor(i + (j - i) / 2); // Calculate midpoint index m, use Math.floor() to round down\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            j = m - 1; // The first element less than target is in the interval [i, m-1]\n        }\n    }\n    // Return insertion point i\n    return i;\n}\n
      binary_search_insertion.ts
      /* Binary search for insertion point (with duplicate elements) */\nfunction binarySearchInsertion(nums: Array<number>, target: number): number {\n    let i = 0,\n        j = nums.length - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        const m = Math.floor(i + (j - i) / 2); // Calculate midpoint index m, use Math.floor() to round down\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            j = m - 1; // The first element less than target is in the interval [i, m-1]\n        }\n    }\n    // Return insertion point i\n    return i;\n}\n
      binary_search_insertion.dart
      /* Binary search for insertion point (with duplicate elements) */\nint binarySearchInsertion(List<int> nums, int target) {\n  int i = 0, j = nums.length - 1; // Initialize closed interval [0, n-1]\n  while (i <= j) {\n    int m = i + (j - i) ~/ 2; // Calculate the midpoint index m\n    if (nums[m] < target) {\n      i = m + 1; // target is in the interval [m+1, j]\n    } else if (nums[m] > target) {\n      j = m - 1; // target is in the interval [i, m-1]\n    } else {\n      j = m - 1; // The first element less than target is in the interval [i, m-1]\n    }\n  }\n  // Return insertion point i\n  return i;\n}\n
      binary_search_insertion.rs
      /* Binary search for insertion point (with duplicate elements) */\npub fn binary_search_insertion(nums: &[i32], target: i32) -> i32 {\n    let (mut i, mut j) = (0, nums.len() as i32 - 1); // Initialize closed interval [0, n-1]\n    while i <= j {\n        let m = i + (j - i) / 2; // Calculate the midpoint index m\n        if nums[m as usize] < target {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if nums[m as usize] > target {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            j = m - 1; // The first element less than target is in the interval [i, m-1]\n        }\n    }\n    // Return insertion point i\n    i\n}\n
      binary_search_insertion.c
      /* Binary search for insertion point (with duplicate elements) */\nint binarySearchInsertion(int *nums, int numSize, int target) {\n    int i = 0, j = numSize - 1; // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // Calculate the midpoint index m\n        if (nums[m] < target) {\n            i = m + 1; // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target is in the interval [i, m-1]\n        } else {\n            j = m - 1; // The first element less than target is in the interval [i, m-1]\n        }\n    }\n    // Return insertion point i\n    return i;\n}\n
      binary_search_insertion.kt
      /* Binary search for insertion point (with duplicate elements) */\nfun binarySearchInsertion(nums: IntArray, target: Int): Int {\n    var i = 0\n    var j = nums.size - 1 // Initialize closed interval [0, n-1]\n    while (i <= j) {\n        val m = i + (j - i) / 2 // Calculate the midpoint index m\n        if (nums[m] < target) {\n            i = m + 1 // target is in the interval [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1 // target is in the interval [i, m-1]\n        } else {\n            j = m - 1 // The first element less than target is in the interval [i, m-1]\n        }\n    }\n    // Return insertion point i\n    return i\n}\n
      binary_search_insertion.rb
      ### Binary search insertion point (with duplicates) ###\ndef binary_search_insertion(nums, target)\n  # Initialize closed interval [0, n-1]\n  i, j = 0, nums.length - 1\n\n  while i <= j\n    # Calculate the midpoint index m\n    m = (i + j) / 2\n\n    if nums[m] < target\n      i = m + 1 # target is in the interval [m+1, j]\n    elsif nums[m] > target\n      j = m - 1 # target is in the interval [i, m-1]\n    else\n      j = m - 1 # The first element less than target is in the interval [i, m-1]\n    end\n  end\n\n  i # Return insertion point i\nend\n

      Tip

      The code in this section all uses the \"closed interval\" approach. Interested readers can implement the \"left-closed right-open\" approach themselves.

      Overall, binary search is simply about setting search targets for pointers \\(i\\) and \\(j\\) separately. The target could be a specific element (such as target) or a range of elements (such as elements less than target).

      Through continuous binary iterations, both pointers \\(i\\) and \\(j\\) gradually approach their preset targets. Ultimately, they either successfully find the answer or stop after crossing the boundaries.

      "},{"location":"chapter_searching/replace_linear_by_hashing/","title":"10.4 \u00a0 Hash Optimization Strategy","text":"

      In algorithm problems, we often reduce the time complexity of algorithms by replacing linear search with hash-based search. Let's use an algorithm problem to deepen our understanding.

      Question

      Given an integer array nums and a target element target, search for two elements in the array whose \"sum\" equals target, and return their array indices. Any solution will do.

      "},{"location":"chapter_searching/replace_linear_by_hashing/#1041-linear-search-trading-time-for-space","title":"10.4.1 \u00a0 Linear Search: Trading Time for Space","text":"

      Consider directly traversing all possible combinations. As shown in Figure 10-9, we open a two-layer loop and judge in each round whether the sum of two integers equals target. If so, return their indices.

      Figure 10-9 \u00a0 Linear search solution for two sum

      The code is shown below:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby two_sum.py
      def two_sum_brute_force(nums: list[int], target: int) -> list[int]:\n    \"\"\"Method 1: Brute force enumeration\"\"\"\n    # Two nested loops, time complexity is O(n^2)\n    for i in range(len(nums) - 1):\n        for j in range(i + 1, len(nums)):\n            if nums[i] + nums[j] == target:\n                return [i, j]\n    return []\n
      two_sum.cpp
      /* Method 1: Brute force enumeration */\nvector<int> twoSumBruteForce(vector<int> &nums, int target) {\n    int size = nums.size();\n    // Two nested loops, time complexity is O(n^2)\n    for (int i = 0; i < size - 1; i++) {\n        for (int j = i + 1; j < size; j++) {\n            if (nums[i] + nums[j] == target)\n                return {i, j};\n        }\n    }\n    return {};\n}\n
      two_sum.java
      /* Method 1: Brute force enumeration */\nint[] twoSumBruteForce(int[] nums, int target) {\n    int size = nums.length;\n    // Two nested loops, time complexity is O(n^2)\n    for (int i = 0; i < size - 1; i++) {\n        for (int j = i + 1; j < size; j++) {\n            if (nums[i] + nums[j] == target)\n                return new int[] { i, j };\n        }\n    }\n    return new int[0];\n}\n
      two_sum.cs
      /* Method 1: Brute force enumeration */\nint[] TwoSumBruteForce(int[] nums, int target) {\n    int size = nums.Length;\n    // Two nested loops, time complexity is O(n^2)\n    for (int i = 0; i < size - 1; i++) {\n        for (int j = i + 1; j < size; j++) {\n            if (nums[i] + nums[j] == target)\n                return [i, j];\n        }\n    }\n    return [];\n}\n
      two_sum.go
      /* Method 1: Brute force enumeration */\nfunc twoSumBruteForce(nums []int, target int) []int {\n    size := len(nums)\n    // Two nested loops, time complexity is O(n^2)\n    for i := 0; i < size-1; i++ {\n        for j := i + 1; j < size; j++ {\n            if nums[i]+nums[j] == target {\n                return []int{i, j}\n            }\n        }\n    }\n    return nil\n}\n
      two_sum.swift
      /* Method 1: Brute force enumeration */\nfunc twoSumBruteForce(nums: [Int], target: Int) -> [Int] {\n    // Two nested loops, time complexity is O(n^2)\n    for i in nums.indices.dropLast() {\n        for j in nums.indices.dropFirst(i + 1) {\n            if nums[i] + nums[j] == target {\n                return [i, j]\n            }\n        }\n    }\n    return [0]\n}\n
      two_sum.js
      /* Method 1: Brute force enumeration */\nfunction twoSumBruteForce(nums, target) {\n    const n = nums.length;\n    // Two nested loops, time complexity is O(n^2)\n    for (let i = 0; i < n; i++) {\n        for (let j = i + 1; j < n; j++) {\n            if (nums[i] + nums[j] === target) {\n                return [i, j];\n            }\n        }\n    }\n    return [];\n}\n
      two_sum.ts
      /* Method 1: Brute force enumeration */\nfunction twoSumBruteForce(nums: number[], target: number): number[] {\n    const n = nums.length;\n    // Two nested loops, time complexity is O(n^2)\n    for (let i = 0; i < n; i++) {\n        for (let j = i + 1; j < n; j++) {\n            if (nums[i] + nums[j] === target) {\n                return [i, j];\n            }\n        }\n    }\n    return [];\n}\n
      two_sum.dart
      /* Method 1: Brute force enumeration */\nList<int> twoSumBruteForce(List<int> nums, int target) {\n  int size = nums.length;\n  // Two nested loops, time complexity is O(n^2)\n  for (var i = 0; i < size - 1; i++) {\n    for (var j = i + 1; j < size; j++) {\n      if (nums[i] + nums[j] == target) return [i, j];\n    }\n  }\n  return [0];\n}\n
      two_sum.rs
      /* Method 1: Brute force enumeration */\npub fn two_sum_brute_force(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {\n    let size = nums.len();\n    // Two nested loops, time complexity is O(n^2)\n    for i in 0..size - 1 {\n        for j in i + 1..size {\n            if nums[i] + nums[j] == target {\n                return Some(vec![i as i32, j as i32]);\n            }\n        }\n    }\n    None\n}\n
      two_sum.c
      /* Method 1: Brute force enumeration */\nint *twoSumBruteForce(int *nums, int numsSize, int target, int *returnSize) {\n    for (int i = 0; i < numsSize; ++i) {\n        for (int j = i + 1; j < numsSize; ++j) {\n            if (nums[i] + nums[j] == target) {\n                int *res = malloc(sizeof(int) * 2);\n                res[0] = i, res[1] = j;\n                *returnSize = 2;\n                return res;\n            }\n        }\n    }\n    *returnSize = 0;\n    return NULL;\n}\n
      two_sum.kt
      /* Method 1: Brute force enumeration */\nfun twoSumBruteForce(nums: IntArray, target: Int): IntArray {\n    val size = nums.size\n    // Two nested loops, time complexity is O(n^2)\n    for (i in 0..<size - 1) {\n        for (j in i + 1..<size) {\n            if (nums[i] + nums[j] == target) return intArrayOf(i, j)\n        }\n    }\n    return IntArray(0)\n}\n
      two_sum.rb
      ### Method 1: Brute force enumeration ###\ndef two_sum_brute_force(nums, target)\n  # Two nested loops, time complexity is O(n^2)\n  for i in 0...(nums.length - 1)\n    for j in (i + 1)...nums.length\n      return [i, j] if nums[i] + nums[j] == target\n    end\n  end\n\n  []\nend\n

      This method has a time complexity of \\(O(n^2)\\) and a space complexity of \\(O(1)\\), which is very time-consuming with large data volumes.

      "},{"location":"chapter_searching/replace_linear_by_hashing/#1042-hash-based-search-trading-space-for-time","title":"10.4.2 \u00a0 Hash-Based Search: Trading Space for Time","text":"

      Consider using a hash table where key-value pairs are array elements and element indices respectively. Loop through the array, performing the steps shown in Figure 10-10 in each round:

      1. Check if the number target - nums[i] is in the hash table. If so, directly return the indices of these two elements.
      2. Add the key-value pair nums[i] and index i to the hash table.
      <1><2><3>

      Figure 10-10 \u00a0 Hash table solution for two sum

      The implementation code is shown below, requiring only a single loop:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby two_sum.py
      def two_sum_hash_table(nums: list[int], target: int) -> list[int]:\n    \"\"\"Method 2: Auxiliary hash table\"\"\"\n    # Auxiliary hash table, space complexity is O(n)\n    dic = {}\n    # Single loop, time complexity is O(n)\n    for i in range(len(nums)):\n        if target - nums[i] in dic:\n            return [dic[target - nums[i]], i]\n        dic[nums[i]] = i\n    return []\n
      two_sum.cpp
      /* Method 2: Auxiliary hash table */\nvector<int> twoSumHashTable(vector<int> &nums, int target) {\n    int size = nums.size();\n    // Auxiliary hash table, space complexity is O(n)\n    unordered_map<int, int> dic;\n    // Single loop, time complexity is O(n)\n    for (int i = 0; i < size; i++) {\n        if (dic.find(target - nums[i]) != dic.end()) {\n            return {dic[target - nums[i]], i};\n        }\n        dic.emplace(nums[i], i);\n    }\n    return {};\n}\n
      two_sum.java
      /* Method 2: Auxiliary hash table */\nint[] twoSumHashTable(int[] nums, int target) {\n    int size = nums.length;\n    // Auxiliary hash table, space complexity is O(n)\n    Map<Integer, Integer> dic = new HashMap<>();\n    // Single loop, time complexity is O(n)\n    for (int i = 0; i < size; i++) {\n        if (dic.containsKey(target - nums[i])) {\n            return new int[] { dic.get(target - nums[i]), i };\n        }\n        dic.put(nums[i], i);\n    }\n    return new int[0];\n}\n
      two_sum.cs
      /* Method 2: Auxiliary hash table */\nint[] TwoSumHashTable(int[] nums, int target) {\n    int size = nums.Length;\n    // Auxiliary hash table, space complexity is O(n)\n    Dictionary<int, int> dic = [];\n    // Single loop, time complexity is O(n)\n    for (int i = 0; i < size; i++) {\n        if (dic.ContainsKey(target - nums[i])) {\n            return [dic[target - nums[i]], i];\n        }\n        dic.Add(nums[i], i);\n    }\n    return [];\n}\n
      two_sum.go
      /* Method 2: Auxiliary hash table */\nfunc twoSumHashTable(nums []int, target int) []int {\n    // Auxiliary hash table, space complexity is O(n)\n    hashTable := map[int]int{}\n    // Single loop, time complexity is O(n)\n    for idx, val := range nums {\n        if preIdx, ok := hashTable[target-val]; ok {\n            return []int{preIdx, idx}\n        }\n        hashTable[val] = idx\n    }\n    return nil\n}\n
      two_sum.swift
      /* Method 2: Auxiliary hash table */\nfunc twoSumHashTable(nums: [Int], target: Int) -> [Int] {\n    // Auxiliary hash table, space complexity is O(n)\n    var dic: [Int: Int] = [:]\n    // Single loop, time complexity is O(n)\n    for i in nums.indices {\n        if let j = dic[target - nums[i]] {\n            return [j, i]\n        }\n        dic[nums[i]] = i\n    }\n    return [0]\n}\n
      two_sum.js
      /* Method 2: Auxiliary hash table */\nfunction twoSumHashTable(nums, target) {\n    // Auxiliary hash table, space complexity is O(n)\n    let m = {};\n    // Single loop, time complexity is O(n)\n    for (let i = 0; i < nums.length; i++) {\n        if (m[target - nums[i]] !== undefined) {\n            return [m[target - nums[i]], i];\n        } else {\n            m[nums[i]] = i;\n        }\n    }\n    return [];\n}\n
      two_sum.ts
      /* Method 2: Auxiliary hash table */\nfunction twoSumHashTable(nums: number[], target: number): number[] {\n    // Auxiliary hash table, space complexity is O(n)\n    let m: Map<number, number> = new Map();\n    // Single loop, time complexity is O(n)\n    for (let i = 0; i < nums.length; i++) {\n        let index = m.get(target - nums[i]);\n        if (index !== undefined) {\n            return [index, i];\n        } else {\n            m.set(nums[i], i);\n        }\n    }\n    return [];\n}\n
      two_sum.dart
      /* Method 2: Auxiliary hash table */\nList<int> twoSumHashTable(List<int> nums, int target) {\n  int size = nums.length;\n  // Auxiliary hash table, space complexity is O(n)\n  Map<int, int> dic = HashMap();\n  // Single loop, time complexity is O(n)\n  for (var i = 0; i < size; i++) {\n    if (dic.containsKey(target - nums[i])) {\n      return [dic[target - nums[i]]!, i];\n    }\n    dic.putIfAbsent(nums[i], () => i);\n  }\n  return [0];\n}\n
      two_sum.rs
      /* Method 2: Auxiliary hash table */\npub fn two_sum_hash_table(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {\n    // Auxiliary hash table, space complexity is O(n)\n    let mut dic = HashMap::new();\n    // Single loop, time complexity is O(n)\n    for (i, num) in nums.iter().enumerate() {\n        match dic.get(&(target - num)) {\n            Some(v) => return Some(vec![*v as i32, i as i32]),\n            None => dic.insert(num, i as i32),\n        };\n    }\n    None\n}\n
      two_sum.c
      /* Hash table */\ntypedef struct {\n    int key;\n    int val;\n    UT_hash_handle hh; // Implemented using uthash.h\n} HashTable;\n\n/* Hash table lookup */\nHashTable *find(HashTable *h, int key) {\n    HashTable *tmp;\n    HASH_FIND_INT(h, &key, tmp);\n    return tmp;\n}\n\n/* Hash table element insertion */\nvoid insert(HashTable **h, int key, int val) {\n    HashTable *t = find(*h, key);\n    if (t == NULL) {\n        HashTable *tmp = malloc(sizeof(HashTable));\n        tmp->key = key, tmp->val = val;\n        HASH_ADD_INT(*h, key, tmp);\n    } else {\n        t->val = val;\n    }\n}\n\n/* Method 2: Auxiliary hash table */\nint *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {\n    HashTable *hashtable = NULL;\n    for (int i = 0; i < numsSize; i++) {\n        HashTable *t = find(hashtable, target - nums[i]);\n        if (t != NULL) {\n            int *res = malloc(sizeof(int) * 2);\n            res[0] = t->val, res[1] = i;\n            *returnSize = 2;\n            return res;\n        }\n        insert(&hashtable, nums[i], i);\n    }\n    *returnSize = 0;\n    return NULL;\n}\n
      two_sum.kt
      /* Method 2: Auxiliary hash table */\nfun twoSumHashTable(nums: IntArray, target: Int): IntArray {\n    val size = nums.size\n    // Auxiliary hash table, space complexity is O(n)\n    val dic = HashMap<Int, Int>()\n    // Single loop, time complexity is O(n)\n    for (i in 0..<size) {\n        if (dic.containsKey(target - nums[i])) {\n            return intArrayOf(dic[target - nums[i]]!!, i)\n        }\n        dic[nums[i]] = i\n    }\n    return IntArray(0)\n}\n
      two_sum.rb
      ### Method 2: Auxiliary hash table ###\ndef two_sum_hash_table(nums, target)\n  # Auxiliary hash table, space complexity is O(n)\n  dic = {}\n  # Single loop, time complexity is O(n)\n  for i in 0...nums.length\n    return [dic[target - nums[i]], i] if dic.has_key?(target - nums[i])\n\n    dic[nums[i]] = i\n  end\n\n  []\nend\n

      This method reduces the time complexity from \\(O(n^2)\\) to \\(O(n)\\) through hash-based search, greatly improving runtime efficiency.

      Since an additional hash table needs to be maintained, the space complexity is \\(O(n)\\). Nevertheless, this method achieves a more balanced overall time-space efficiency, making it the optimal solution for this problem.

      "},{"location":"chapter_searching/searching_algorithm_revisited/","title":"10.5 \u00a0 Searching Algorithms Revisited","text":"

      Searching algorithms are used to search for one or a group of elements that meet specific conditions in data structures (such as arrays, linked lists, trees, or graphs).

      Searching algorithms can be divided into the following two categories based on their implementation approach:

      • Locating target elements by traversing the data structure, such as traversing arrays, linked lists, trees, and graphs.
      • Achieving efficient element search by utilizing data organization structure or prior information contained in the data, such as binary search, hash-based search, and binary search tree search.

      It's not hard to see that these topics have all been covered in previous chapters, so searching algorithms are not unfamiliar to us. In this section, we will approach from a more systematic perspective and re-examine searching algorithms.

      "},{"location":"chapter_searching/searching_algorithm_revisited/#1051-brute-force-search","title":"10.5.1 \u00a0 Brute-Force Search","text":"

      Brute-force search locates target elements by traversing each element of the data structure.

      • \"Linear search\" is applicable to linear data structures such as arrays and linked lists. It starts from one end of the data structure and accesses elements one by one until the target element is found or the other end is reached without finding the target element.
      • \"Breadth-first search\" and \"depth-first search\" are two traversal strategies for graphs and trees. Breadth-first search starts from the initial node and searches layer by layer, visiting nodes from near to far. Depth-first search starts from the initial node, follows a path to the end, then backtracks and tries other paths until the entire data structure is traversed.

      The advantage of brute-force search is that it is simple and has good generality, requiring no data preprocessing or additional data structures.

      However, the time complexity of such algorithms is \\(O(n)\\), where \\(n\\) is the number of elements, so performance is poor when dealing with large amounts of data.

      "},{"location":"chapter_searching/searching_algorithm_revisited/#1052-adaptive-search","title":"10.5.2 \u00a0 Adaptive Search","text":"

      Adaptive search utilizes the unique properties of data (such as orderliness) to optimize the search process, thereby locating target elements more efficiently.

      • \"Binary search\" uses the orderliness of data to achieve efficient searching, applicable only to arrays.
      • \"Hash-based search\" uses hash tables to establish key-value pair mappings between search data and target data, thereby achieving query operations.
      • \"Tree search\" in specific tree structures (such as binary search trees), quickly eliminates nodes based on comparing node values to locate target elements.

      The advantage of such algorithms is high efficiency, with time complexity reaching \\(O(\\log n)\\) or even \\(O(1)\\).

      However, using these algorithms often requires data preprocessing. For example, binary search requires pre-sorting the array, while hash-based search and tree search both require additional data structures, and maintaining these data structures also requires extra time and space overhead.

      Tip

      Adaptive search algorithms are often called lookup algorithms, mainly used to quickly retrieve target elements in specific data structures.

      "},{"location":"chapter_searching/searching_algorithm_revisited/#1053-search-method-selection","title":"10.5.3 \u00a0 Search Method Selection","text":"

      Given a dataset of size \\(n\\), we can use linear search, binary search, tree search, hash-based search, and other methods to search for the target element. The working principles of each method are shown in Figure 10-11.

      Figure 10-11 \u00a0 Multiple search strategies

      The operational efficiency and characteristics of the above methods are as follows:

      Table 10-1 \u00a0 Comparison of search algorithm efficiency

      Linear search Binary search Tree search Hash-based search Search element \\(O(n)\\) \\(O(\\log n)\\) \\(O(\\log n)\\) \\(O(1)\\) Insert element \\(O(1)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) Delete element \\(O(n)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) Extra space \\(O(1)\\) \\(O(1)\\) \\(O(n)\\) \\(O(n)\\) Data preprocessing / Sorting \\(O(n \\log n)\\) Tree building \\(O(n \\log n)\\) Hash table building \\(O(n)\\) Data ordered Unordered Ordered Ordered Unordered

      The choice of search algorithm also depends on data volume, search performance requirements, data query and update frequency, etc.

      Linear search

      • Good generality, requiring no data preprocessing operations. If we only need to query the data once, the data preprocessing time for the other three methods would be longer than linear search.
      • Suitable for small data volumes, where time complexity has less impact on efficiency.
      • Suitable for scenarios with high data update frequency, as this method does not require any additional data maintenance.

      Binary search

      • Suitable for large data volumes with stable efficiency performance, worst-case time complexity of \\(O(\\log n)\\).
      • Data volume cannot be too large, as storing arrays requires contiguous memory space.
      • Not suitable for scenarios with frequent data insertion and deletion, as maintaining a sorted array has high overhead.

      Hash-based search

      • Suitable for scenarios with high query performance requirements, with an average time complexity of \\(O(1)\\).
      • Not suitable for scenarios requiring ordered data or range searches, as hash tables cannot maintain data orderliness.
      • High dependence on hash functions and hash collision handling strategies, with significant risk of performance degradation.
      • Not suitable for excessively large data volumes, as hash tables require extra space to minimize collisions and thus provide good query performance.

      Tree search

      • Suitable for massive data, as tree nodes are stored dispersedly in memory.
      • Suitable for scenarios requiring maintained ordered data or range searches.
      • During continuous node insertion and deletion, binary search trees may become skewed, degrading time complexity to \\(O(n)\\).
      • If using AVL trees or red-black trees, all operations can run stably at \\(O(\\log n)\\) efficiency, but operations to maintain tree balance add extra overhead.
      "},{"location":"chapter_searching/summary/","title":"10.6 \u00a0 Summary","text":""},{"location":"chapter_searching/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • Binary search relies on data orderliness and progressively reduces the search interval by half through loops. It requires input data to be sorted and is only applicable to arrays or data structures based on array implementations.
      • Brute-force search locates data by traversing the data structure. Linear search is applicable to arrays and linked lists, while breadth-first search and depth-first search are applicable to graphs and trees. Such algorithms have good generality and require no data preprocessing, but have a relatively high time complexity of \\(O(n)\\).
      • Hash-based search, tree search, and binary search are efficient search methods that can quickly locate target elements in specific data structures. Such algorithms are highly efficient with time complexity reaching \\(O(\\log n)\\) or even \\(O(1)\\), but typically require additional data structures.
      • In practice, we need to analyze factors such as data scale, search performance requirements, and data query and update frequency to choose the appropriate search method.
      • Linear search is suitable for small-scale or frequently updated data; binary search is suitable for large-scale, sorted data; hash-based search is suitable for data with high query efficiency requirements and no need for range queries; tree search is suitable for large-scale dynamic data that needs to maintain order and support range queries.
      • Replacing linear search with hash-based search is a commonly used strategy to optimize runtime, reducing time complexity from \\(O(n)\\) to \\(O(1)\\).
      "},{"location":"chapter_sorting/","title":"Chapter 11. \u00a0 Sorting","text":"

      Abstract

      Sorting is like a magic key that transforms chaos into order, enabling us to understand and process data more efficiently.

      Whether it's simple ascending order or complex categorized arrangements, sorting demonstrates the harmonious beauty of data.

      "},{"location":"chapter_sorting/#chapter-contents","title":"Chapter contents","text":"
      • 11.1 \u00a0 Sorting Algorithms
      • 11.2 \u00a0 Selection Sort
      • 11.3 \u00a0 Bubble Sort
      • 11.4 \u00a0 Insertion Sort
      • 11.5 \u00a0 Quick Sort
      • 11.6 \u00a0 Merge Sort
      • 11.7 \u00a0 Heap Sort
      • 11.8 \u00a0 Bucket Sort
      • 11.9 \u00a0 Counting Sort
      • 11.10 \u00a0 Radix Sort
      • 11.11 \u00a0 Summary
      "},{"location":"chapter_sorting/bubble_sort/","title":"11.3 \u00a0 Bubble Sort","text":"

      Bubble sort (bubble sort) achieves sorting by continuously comparing and swapping adjacent elements. This process is like bubbles rising from the bottom to the top, hence the name bubble sort.

      As shown in Figure 11-4, the bubbling process can be simulated using element swap operations: starting from the leftmost end of the array and traversing to the right, compare the size of adjacent elements, and if \"left element > right element\", swap them. After completing the traversal, the largest element will be moved to the rightmost end of the array.

      <1><2><3><4><5><6><7>

      Figure 11-4 \u00a0 Simulating bubble using element swap operation

      "},{"location":"chapter_sorting/bubble_sort/#1131-algorithm-flow","title":"11.3.1 \u00a0 Algorithm Flow","text":"

      Assume the array has length \\(n\\). The steps of bubble sort are shown in Figure 11-5.

      1. First, perform \"bubbling\" on \\(n\\) elements, swapping the largest element of the array to its correct position.
      2. Next, perform \"bubbling\" on the remaining \\(n - 1\\) elements, swapping the second largest element to its correct position.
      3. And so on. After \\(n - 1\\) rounds of \"bubbling\", the largest \\(n - 1\\) elements have all been swapped to their correct positions.
      4. The only remaining element must be the smallest element, requiring no sorting, so the array sorting is complete.

      Figure 11-5 \u00a0 Bubble sort flow

      Example code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby bubble_sort.py
      def bubble_sort(nums: list[int]):\n    \"\"\"Bubble sort\"\"\"\n    n = len(nums)\n    # Outer loop: unsorted interval is [0, i]\n    for i in range(n - 1, 0, -1):\n        # Inner loop: swap the largest element in the unsorted interval [0, i] to the rightmost end of the interval\n        for j in range(i):\n            if nums[j] > nums[j + 1]:\n                # Swap nums[j] and nums[j + 1]\n                nums[j], nums[j + 1] = nums[j + 1], nums[j]\n
      bubble_sort.cpp
      /* Bubble sort */\nvoid bubbleSort(vector<int> &nums) {\n    // Outer loop: unsorted range is [0, i]\n    for (int i = nums.size() - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                // Using std::swap() function here\n                swap(nums[j], nums[j + 1]);\n            }\n        }\n    }\n}\n
      bubble_sort.java
      /* Bubble sort */\nvoid bubbleSort(int[] nums) {\n    // Outer loop: unsorted range is [0, i]\n    for (int i = nums.length - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n            }\n        }\n    }\n}\n
      bubble_sort.cs
      /* Bubble sort */\nvoid BubbleSort(int[] nums) {\n    // Outer loop: unsorted range is [0, i]\n    for (int i = nums.Length - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n            }\n        }\n    }\n}\n
      bubble_sort.go
      /* Bubble sort */\nfunc bubbleSort(nums []int) {\n    // Outer loop: unsorted range is [0, i]\n    for i := len(nums) - 1; i > 0; i-- {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for j := 0; j < i; j++ {\n            if nums[j] > nums[j+1] {\n                // Swap nums[j] and nums[j + 1]\n                nums[j], nums[j+1] = nums[j+1], nums[j]\n            }\n        }\n    }\n}\n
      bubble_sort.swift
      /* Bubble sort */\nfunc bubbleSort(nums: inout [Int]) {\n    // Outer loop: unsorted range is [0, i]\n    for i in nums.indices.dropFirst().reversed() {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for j in 0 ..< i {\n            if nums[j] > nums[j + 1] {\n                // Swap nums[j] and nums[j + 1]\n                nums.swapAt(j, j + 1)\n            }\n        }\n    }\n}\n
      bubble_sort.js
      /* Bubble sort */\nfunction bubbleSort(nums) {\n    // Outer loop: unsorted range is [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n            }\n        }\n    }\n}\n
      bubble_sort.ts
      /* Bubble sort */\nfunction bubbleSort(nums: number[]): void {\n    // Outer loop: unsorted range is [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n            }\n        }\n    }\n}\n
      bubble_sort.dart
      /* Bubble sort */\nvoid bubbleSort(List<int> nums) {\n  // Outer loop: unsorted range is [0, i]\n  for (int i = nums.length - 1; i > 0; i--) {\n    // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n    for (int j = 0; j < i; j++) {\n      if (nums[j] > nums[j + 1]) {\n        // Swap nums[j] and nums[j + 1]\n        int tmp = nums[j];\n        nums[j] = nums[j + 1];\n        nums[j + 1] = tmp;\n      }\n    }\n  }\n}\n
      bubble_sort.rs
      /* Bubble sort */\nfn bubble_sort(nums: &mut [i32]) {\n    // Outer loop: unsorted range is [0, i]\n    for i in (1..nums.len()).rev() {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for j in 0..i {\n            if nums[j] > nums[j + 1] {\n                // Swap nums[j] and nums[j + 1]\n                nums.swap(j, j + 1);\n            }\n        }\n    }\n}\n
      bubble_sort.c
      /* Bubble sort */\nvoid bubbleSort(int nums[], int size) {\n    // Outer loop: unsorted range is [0, i]\n    for (int i = size - 1; i > 0; i--) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                int temp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = temp;\n            }\n        }\n    }\n}\n
      bubble_sort.kt
      /* Bubble sort */\nfun bubbleSort(nums: IntArray) {\n    // Outer loop: unsorted range is [0, i]\n    for (i in nums.size - 1 downTo 1) {\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (j in 0..<i) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                val temp = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = temp\n            }\n        }\n    }\n}\n
      bubble_sort.rb
      ### Bubble sort ###\ndef bubble_sort(nums)\n  n = nums.length\n  # Outer loop: unsorted range is [0, i]\n  for i in (n - 1).downto(1)\n    # Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n    for j in 0...i\n      if nums[j] > nums[j + 1]\n        # Swap nums[j] and nums[j + 1]\n        nums[j], nums[j + 1] = nums[j + 1], nums[j]\n      end\n    end\n  end\nend\n
      "},{"location":"chapter_sorting/bubble_sort/#1132-efficiency-optimization","title":"11.3.2 \u00a0 Efficiency Optimization","text":"

      We notice that if no swap operations are performed during a certain round of \"bubbling\", it means the array has already completed sorting and can directly return the result. Therefore, we can add a flag flag to monitor this situation and return immediately once it occurs.

      After optimization, the worst-case time complexity and average time complexity of bubble sort remain \\(O(n^2)\\); but when the input array is completely ordered, the best-case time complexity can reach \\(O(n)\\).

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby bubble_sort.py
      def bubble_sort_with_flag(nums: list[int]):\n    \"\"\"Bubble sort (flag optimization)\"\"\"\n    n = len(nums)\n    # Outer loop: unsorted interval is [0, i]\n    for i in range(n - 1, 0, -1):\n        flag = False  # Initialize flag\n        # Inner loop: swap the largest element in the unsorted interval [0, i] to the rightmost end of the interval\n        for j in range(i):\n            if nums[j] > nums[j + 1]:\n                # Swap nums[j] and nums[j + 1]\n                nums[j], nums[j + 1] = nums[j + 1], nums[j]\n                flag = True  # Record element swap\n        if not flag:\n            break  # No elements were swapped in this round of \"bubbling\", exit directly\n
      bubble_sort.cpp
      /* Bubble sort (flag optimization)*/\nvoid bubbleSortWithFlag(vector<int> &nums) {\n    // Outer loop: unsorted range is [0, i]\n    for (int i = nums.size() - 1; i > 0; i--) {\n        bool flag = false; // Initialize flag\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                // Using std::swap() function here\n                swap(nums[j], nums[j + 1]);\n                flag = true; // Record element swap\n            }\n        }\n        if (!flag)\n            break; // No elements were swapped in this round of \"bubbling\", exit directly\n    }\n}\n
      bubble_sort.java
      /* Bubble sort (flag optimization) */\nvoid bubbleSortWithFlag(int[] nums) {\n    // Outer loop: unsorted range is [0, i]\n    for (int i = nums.length - 1; i > 0; i--) {\n        boolean flag = false; // Initialize flag\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                flag = true; // Record element swap\n            }\n        }\n        if (!flag)\n            break; // No elements were swapped in this round of \"bubbling\", exit directly\n    }\n}\n
      bubble_sort.cs
      /* Bubble sort (flag optimization) */\nvoid BubbleSortWithFlag(int[] nums) {\n    // Outer loop: unsorted range is [0, i]\n    for (int i = nums.Length - 1; i > 0; i--) {\n        bool flag = false; // Initialize flag\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n                flag = true;  // Record element swap\n            }\n        }\n        if (!flag) break;     // No elements were swapped in this round of \"bubbling\", exit directly\n    }\n}\n
      bubble_sort.go
      /* Bubble sort (flag optimization) */\nfunc bubbleSortWithFlag(nums []int) {\n    // Outer loop: unsorted range is [0, i]\n    for i := len(nums) - 1; i > 0; i-- {\n        flag := false // Initialize flag\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for j := 0; j < i; j++ {\n            if nums[j] > nums[j+1] {\n                // Swap nums[j] and nums[j + 1]\n                nums[j], nums[j+1] = nums[j+1], nums[j]\n                flag = true // Record element swap\n            }\n        }\n        if flag == false { // No elements were swapped in this round of \"bubbling\", exit directly\n            break\n        }\n    }\n}\n
      bubble_sort.swift
      /* Bubble sort (flag optimization) */\nfunc bubbleSortWithFlag(nums: inout [Int]) {\n    // Outer loop: unsorted range is [0, i]\n    for i in nums.indices.dropFirst().reversed() {\n        var flag = false // Initialize flag\n        for j in 0 ..< i {\n            if nums[j] > nums[j + 1] {\n                // Swap nums[j] and nums[j + 1]\n                nums.swapAt(j, j + 1)\n                flag = true // Record element swap\n            }\n        }\n        if !flag { // No elements were swapped in this round of \"bubbling\", exit directly\n            break\n        }\n    }\n}\n
      bubble_sort.js
      /* Bubble sort (flag optimization) */\nfunction bubbleSortWithFlag(nums) {\n    // Outer loop: unsorted range is [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        let flag = false; // Initialize flag\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                flag = true; // Record element swap\n            }\n        }\n        if (!flag) break; // No elements were swapped in this round of \"bubbling\", exit directly\n    }\n}\n
      bubble_sort.ts
      /* Bubble sort (flag optimization) */\nfunction bubbleSortWithFlag(nums: number[]): void {\n    // Outer loop: unsorted range is [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        let flag = false; // Initialize flag\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                flag = true; // Record element swap\n            }\n        }\n        if (!flag) break; // No elements were swapped in this round of \"bubbling\", exit directly\n    }\n}\n
      bubble_sort.dart
      /* Bubble sort (flag optimization) */\nvoid bubbleSortWithFlag(List<int> nums) {\n  // Outer loop: unsorted range is [0, i]\n  for (int i = nums.length - 1; i > 0; i--) {\n    bool flag = false; // Initialize flag\n    // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n    for (int j = 0; j < i; j++) {\n      if (nums[j] > nums[j + 1]) {\n        // Swap nums[j] and nums[j + 1]\n        int tmp = nums[j];\n        nums[j] = nums[j + 1];\n        nums[j + 1] = tmp;\n        flag = true; // Record element swap\n      }\n    }\n    if (!flag) break; // No elements were swapped in this round of \"bubbling\", exit directly\n  }\n}\n
      bubble_sort.rs
      /* Bubble sort (flag optimization) */\nfn bubble_sort_with_flag(nums: &mut [i32]) {\n    // Outer loop: unsorted range is [0, i]\n    for i in (1..nums.len()).rev() {\n        let mut flag = false; // Initialize flag\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for j in 0..i {\n            if nums[j] > nums[j + 1] {\n                // Swap nums[j] and nums[j + 1]\n                nums.swap(j, j + 1);\n                flag = true; // Record element swap\n            }\n        }\n        if !flag {\n            break; // No elements were swapped in this round of \"bubbling\", exit directly\n        };\n    }\n}\n
      bubble_sort.c
      /* Bubble sort (flag optimization) */\nvoid bubbleSortWithFlag(int nums[], int size) {\n    // Outer loop: unsorted range is [0, i]\n    for (int i = size - 1; i > 0; i--) {\n        bool flag = false;\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                int temp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = temp;\n                flag = true;\n            }\n        }\n        if (!flag)\n            break;\n    }\n}\n
      bubble_sort.kt
      /* Bubble sort (flag optimization) */\nfun bubbleSortWithFlag(nums: IntArray) {\n    // Outer loop: unsorted range is [0, i]\n    for (i in nums.size - 1 downTo 1) {\n        var flag = false // Initialize flag\n        // Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n        for (j in 0..<i) {\n            if (nums[j] > nums[j + 1]) {\n                // Swap nums[j] and nums[j + 1]\n                val temp = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = temp\n                flag = true // Record element swap\n            }\n        }\n        if (!flag) break // No elements were swapped in this round of \"bubbling\", exit directly\n    }\n}\n
      bubble_sort.rb
      ### Bubble sort (flag optimization) ###\ndef bubble_sort_with_flag(nums)\n  n = nums.length\n  # Outer loop: unsorted range is [0, i]\n  for i in (n - 1).downto(1)\n    flag = false # Initialize flag\n\n    # Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range\n    for j in 0...i\n      if nums[j] > nums[j + 1]\n        # Swap nums[j] and nums[j + 1]\n        nums[j], nums[j + 1] = nums[j + 1], nums[j]\n        flag = true # Record element swap\n      end\n    end\n\n    break unless flag # No elements were swapped in this round of \"bubbling\", exit directly\n  end\nend\n
      "},{"location":"chapter_sorting/bubble_sort/#1133-algorithm-characteristics","title":"11.3.3 \u00a0 Algorithm Characteristics","text":"
      • Time complexity of \\(O(n^2)\\), adaptive sorting: The array lengths traversed in each round of \"bubbling\" are \\(n - 1\\), \\(n - 2\\), \\(\\dots\\), \\(2\\), \\(1\\), totaling \\((n - 1) n / 2\\). After introducing the flag optimization, the best-case time complexity can reach \\(O(n)\\).
      • Space complexity of \\(O(1)\\), in-place sorting: Pointers \\(i\\) and \\(j\\) use a constant amount of extra space.
      • Stable sorting: Since equal elements are not swapped during \"bubbling\".
      "},{"location":"chapter_sorting/bucket_sort/","title":"11.8 \u00a0 Bucket Sort","text":"

      The several sorting algorithms mentioned earlier all belong to \"comparison-based sorting algorithms\", which achieve sorting by comparing the size of elements. The time complexity of such sorting algorithms cannot exceed \\(O(n \\log n)\\). Next, we will explore several \"non-comparison sorting algorithms\", whose time complexity can reach linear order.

      Bucket sort (bucket sort) is a typical application of the divide-and-conquer strategy. It works by setting up buckets with size order, each bucket corresponding to a data range, evenly distributing data to each bucket; then, sorting within each bucket separately; finally, merging all data in the order of the buckets.

      "},{"location":"chapter_sorting/bucket_sort/#1181-algorithm-flow","title":"11.8.1 \u00a0 Algorithm Flow","text":"

      Consider an array of length \\(n\\), whose elements are floating-point numbers in the range \\([0, 1)\\). The flow of bucket sort is shown in Figure 11-13.

      1. Initialize \\(k\\) buckets and distribute the \\(n\\) elements into the \\(k\\) buckets.
      2. Sort each bucket separately (here we use the built-in sorting function of the programming language).
      3. Merge the results in order from smallest to largest bucket.

      Figure 11-13 \u00a0 Bucket sort algorithm flow

      The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby bucket_sort.py
      def bucket_sort(nums: list[float]):\n    \"\"\"Bucket sort\"\"\"\n    # Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n    k = len(nums) // 2\n    buckets = [[] for _ in range(k)]\n    # 1. Distribute array elements into various buckets\n    for num in nums:\n        # Input data range is [0, 1), use num * k to map to index range [0, k-1]\n        i = int(num * k)\n        # Add num to bucket i\n        buckets[i].append(num)\n    # 2. Sort each bucket\n    for bucket in buckets:\n        # Use built-in sorting function, can also replace with other sorting algorithms\n        bucket.sort()\n    # 3. Traverse buckets to merge results\n    i = 0\n    for bucket in buckets:\n        for num in bucket:\n            nums[i] = num\n            i += 1\n
      bucket_sort.cpp
      /* Bucket sort */\nvoid bucketSort(vector<float> &nums) {\n    // Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n    int k = nums.size() / 2;\n    vector<vector<float>> buckets(k);\n    // 1. Distribute array elements into various buckets\n    for (float num : nums) {\n        // Input data range is [0, 1), use num * k to map to index range [0, k-1]\n        int i = num * k;\n        // Add num to bucket bucket_idx\n        buckets[i].push_back(num);\n    }\n    // 2. Sort each bucket\n    for (vector<float> &bucket : buckets) {\n        // Use built-in sorting function, can also replace with other sorting algorithms\n        sort(bucket.begin(), bucket.end());\n    }\n    // 3. Traverse buckets to merge results\n    int i = 0;\n    for (vector<float> &bucket : buckets) {\n        for (float num : bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
      bucket_sort.java
      /* Bucket sort */\nvoid bucketSort(float[] nums) {\n    // Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n    int k = nums.length / 2;\n    List<List<Float>> buckets = new ArrayList<>();\n    for (int i = 0; i < k; i++) {\n        buckets.add(new ArrayList<>());\n    }\n    // 1. Distribute array elements into various buckets\n    for (float num : nums) {\n        // Input data range is [0, 1), use num * k to map to index range [0, k-1]\n        int i = (int) (num * k);\n        // Add num to bucket i\n        buckets.get(i).add(num);\n    }\n    // 2. Sort each bucket\n    for (List<Float> bucket : buckets) {\n        // Use built-in sorting function, can also replace with other sorting algorithms\n        Collections.sort(bucket);\n    }\n    // 3. Traverse buckets to merge results\n    int i = 0;\n    for (List<Float> bucket : buckets) {\n        for (float num : bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
      bucket_sort.cs
      /* Bucket sort */\nvoid BucketSort(float[] nums) {\n    // Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n    int k = nums.Length / 2;\n    List<List<float>> buckets = [];\n    for (int i = 0; i < k; i++) {\n        buckets.Add([]);\n    }\n    // 1. Distribute array elements into various buckets\n    foreach (float num in nums) {\n        // Input data range is [0, 1), use num * k to map to index range [0, k-1]\n        int i = (int)(num * k);\n        // Add num to bucket i\n        buckets[i].Add(num);\n    }\n    // 2. Sort each bucket\n    foreach (List<float> bucket in buckets) {\n        // Use built-in sorting function, can also replace with other sorting algorithms\n        bucket.Sort();\n    }\n    // 3. Traverse buckets to merge results\n    int j = 0;\n    foreach (List<float> bucket in buckets) {\n        foreach (float num in bucket) {\n            nums[j++] = num;\n        }\n    }\n}\n
      bucket_sort.go
      /* Bucket sort */\nfunc bucketSort(nums []float64) {\n    // Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n    k := len(nums) / 2\n    buckets := make([][]float64, k)\n    for i := 0; i < k; i++ {\n        buckets[i] = make([]float64, 0)\n    }\n    // 1. Distribute array elements into various buckets\n    for _, num := range nums {\n        // Input data range is [0, 1), use num * k to map to index range [0, k-1]\n        i := int(num * float64(k))\n        // Add num to bucket i\n        buckets[i] = append(buckets[i], num)\n    }\n    // 2. Sort each bucket\n    for i := 0; i < k; i++ {\n        // Use built-in slice sorting function, can also be replaced with other sorting algorithms\n        sort.Float64s(buckets[i])\n    }\n    // 3. Traverse buckets to merge results\n    i := 0\n    for _, bucket := range buckets {\n        for _, num := range bucket {\n            nums[i] = num\n            i++\n        }\n    }\n}\n
      bucket_sort.swift
      /* Bucket sort */\nfunc bucketSort(nums: inout [Double]) {\n    // Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n    let k = nums.count / 2\n    var buckets = (0 ..< k).map { _ in [Double]() }\n    // 1. Distribute array elements into various buckets\n    for num in nums {\n        // Input data range is [0, 1), use num * k to map to index range [0, k-1]\n        let i = Int(num * Double(k))\n        // Add num to bucket i\n        buckets[i].append(num)\n    }\n    // 2. Sort each bucket\n    for i in buckets.indices {\n        // Use built-in sorting function, can also replace with other sorting algorithms\n        buckets[i].sort()\n    }\n    // 3. Traverse buckets to merge results\n    var i = nums.startIndex\n    for bucket in buckets {\n        for num in bucket {\n            nums[i] = num\n            i += 1\n        }\n    }\n}\n
      bucket_sort.js
      /* Bucket sort */\nfunction bucketSort(nums) {\n    // Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n    const k = nums.length / 2;\n    const buckets = [];\n    for (let i = 0; i < k; i++) {\n        buckets.push([]);\n    }\n    // 1. Distribute array elements into various buckets\n    for (const num of nums) {\n        // Input data range is [0, 1), use num * k to map to index range [0, k-1]\n        const i = Math.floor(num * k);\n        // Add num to bucket i\n        buckets[i].push(num);\n    }\n    // 2. Sort each bucket\n    for (const bucket of buckets) {\n        // Use built-in sorting function, can also replace with other sorting algorithms\n        bucket.sort((a, b) => a - b);\n    }\n    // 3. Traverse buckets to merge results\n    let i = 0;\n    for (const bucket of buckets) {\n        for (const num of bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
      bucket_sort.ts
      /* Bucket sort */\nfunction bucketSort(nums: number[]): void {\n    // Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n    const k = nums.length / 2;\n    const buckets: number[][] = [];\n    for (let i = 0; i < k; i++) {\n        buckets.push([]);\n    }\n    // 1. Distribute array elements into various buckets\n    for (const num of nums) {\n        // Input data range is [0, 1), use num * k to map to index range [0, k-1]\n        const i = Math.floor(num * k);\n        // Add num to bucket i\n        buckets[i].push(num);\n    }\n    // 2. Sort each bucket\n    for (const bucket of buckets) {\n        // Use built-in sorting function, can also replace with other sorting algorithms\n        bucket.sort((a, b) => a - b);\n    }\n    // 3. Traverse buckets to merge results\n    let i = 0;\n    for (const bucket of buckets) {\n        for (const num of bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
      bucket_sort.dart
      /* Bucket sort */\nvoid bucketSort(List<double> nums) {\n  // Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n  int k = nums.length ~/ 2;\n  List<List<double>> buckets = List.generate(k, (index) => []);\n\n  // 1. Distribute array elements into various buckets\n  for (double _num in nums) {\n    // Input data range is [0, 1), use _num * k to map to index range [0, k-1]\n    int i = (_num * k).toInt();\n    // Add _num to bucket bucket_idx\n    buckets[i].add(_num);\n  }\n  // 2. Sort each bucket\n  for (List<double> bucket in buckets) {\n    bucket.sort();\n  }\n  // 3. Traverse buckets to merge results\n  int i = 0;\n  for (List<double> bucket in buckets) {\n    for (double _num in bucket) {\n      nums[i++] = _num;\n    }\n  }\n}\n
      bucket_sort.rs
      /* Bucket sort */\nfn bucket_sort(nums: &mut [f64]) {\n    // Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n    let k = nums.len() / 2;\n    let mut buckets = vec![vec![]; k];\n    // 1. Distribute array elements into various buckets\n    for &num in nums.iter() {\n        // Input data range is [0, 1), use num * k to map to index range [0, k-1]\n        let i = (num * k as f64) as usize;\n        // Add num to bucket i\n        buckets[i].push(num);\n    }\n    // 2. Sort each bucket\n    for bucket in &mut buckets {\n        // Use built-in sorting function, can also replace with other sorting algorithms\n        bucket.sort_by(|a, b| a.partial_cmp(b).unwrap());\n    }\n    // 3. Traverse buckets to merge results\n    let mut i = 0;\n    for bucket in buckets.iter() {\n        for &num in bucket.iter() {\n            nums[i] = num;\n            i += 1;\n        }\n    }\n}\n
      bucket_sort.c
      /* Bucket sort */\nvoid bucketSort(float nums[], int n) {\n    int k = n / 2;                                 // Initialize k = n/2 buckets\n    int *sizes = malloc(k * sizeof(int));          // Record each bucket's size\n    float **buckets = malloc(k * sizeof(float *)); // Array of dynamic arrays (buckets)\n    // Pre-allocate sufficient space for each bucket\n    for (int i = 0; i < k; ++i) {\n        buckets[i] = (float *)malloc(n * sizeof(float));\n        sizes[i] = 0;\n    }\n    // 1. Distribute array elements into various buckets\n    for (int i = 0; i < n; ++i) {\n        int idx = (int)(nums[i] * k);\n        buckets[idx][sizes[idx]++] = nums[i];\n    }\n    // 2. Sort each bucket\n    for (int i = 0; i < k; ++i) {\n        qsort(buckets[i], sizes[i], sizeof(float), compare);\n    }\n    // 3. Merge sorted buckets\n    int idx = 0;\n    for (int i = 0; i < k; ++i) {\n        for (int j = 0; j < sizes[i]; ++j) {\n            nums[idx++] = buckets[i][j];\n        }\n        // Free memory\n        free(buckets[i]);\n    }\n}\n
      bucket_sort.kt
      /* Bucket sort */\nfun bucketSort(nums: FloatArray) {\n    // Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n    val k = nums.size / 2\n    val buckets = mutableListOf<MutableList<Float>>()\n    for (i in 0..<k) {\n        buckets.add(mutableListOf())\n    }\n    // 1. Distribute array elements into various buckets\n    for (num in nums) {\n        // Input data range is [0, 1), use num * k to map to index range [0, k-1]\n        val i = (num * k).toInt()\n        // Add num to bucket i\n        buckets[i].add(num)\n    }\n    // 2. Sort each bucket\n    for (bucket in buckets) {\n        // Use built-in sorting function, can also replace with other sorting algorithms\n        bucket.sort()\n    }\n    // 3. Traverse buckets to merge results\n    var i = 0\n    for (bucket in buckets) {\n        for (num in bucket) {\n            nums[i++] = num\n        }\n    }\n}\n
      bucket_sort.rb
      ### Bucket sort ###\ndef bucket_sort(nums)\n  # Initialize k = n/2 buckets, expected to allocate 2 elements per bucket\n  k = nums.length / 2\n  buckets = Array.new(k) { [] }\n\n  # 1. Distribute array elements into various buckets\n  nums.each do |num|\n    # Input data range is [0, 1), use num * k to map to index range [0, k-1]\n    i = (num * k).to_i\n    # Add num to bucket i\n    buckets[i] << num\n  end\n\n  # 2. Sort each bucket\n  buckets.each do |bucket|\n    # Use built-in sorting function, can also replace with other sorting algorithms\n    bucket.sort!\n  end\n\n  # 3. Traverse buckets to merge results\n  i = 0\n  buckets.each do |bucket|\n    bucket.each do |num|\n      nums[i] = num\n      i += 1\n    end\n  end\nend\n
      "},{"location":"chapter_sorting/bucket_sort/#1182-algorithm-characteristics","title":"11.8.2 \u00a0 Algorithm Characteristics","text":"

      Bucket sort is suitable for processing very large data volumes. For example, if the input data contains 1 million elements and system memory cannot load all the data at once, the data can be divided into 1000 buckets, each bucket sorted separately, and then the results merged.

      • Time complexity of \\(O(n + k)\\): Assuming the elements are evenly distributed among the buckets, then the number of elements in each bucket is \\(\\frac{n}{k}\\). Assuming sorting a single bucket uses \\(O(\\frac{n}{k} \\log\\frac{n}{k})\\) time, then sorting all buckets uses \\(O(n \\log\\frac{n}{k})\\) time. When the number of buckets \\(k\\) is relatively large, the time complexity approaches \\(O(n)\\). Merging results requires traversing all buckets and elements, taking \\(O(n + k)\\) time. In the worst case, all data is distributed into one bucket, and sorting that bucket uses \\(O(n^2)\\) time.
      • Space complexity of \\(O(n + k)\\), non-in-place sorting: Additional space is required for \\(k\\) buckets and a total of \\(n\\) elements.
      • Whether bucket sort is stable depends on whether the algorithm for sorting elements within buckets is stable.
      "},{"location":"chapter_sorting/bucket_sort/#1183-how-to-achieve-even-distribution","title":"11.8.3 \u00a0 How to Achieve Even Distribution","text":"

      Theoretically, bucket sort can achieve \\(O(n)\\) time complexity. The key is to evenly distribute elements to each bucket, because real data is often not evenly distributed. For example, if we want to evenly distribute all products on Taobao into 10 buckets by price range, there may be very many products below 100 yuan and very few above 1000 yuan. If the price intervals are evenly divided into 10, the difference in the number of products in each bucket will be very large.

      To achieve even distribution, we can first set an approximate dividing line to roughly divide the data into 3 buckets. After distribution is complete, continue dividing buckets with more products into 3 buckets until the number of elements in all buckets is roughly equal.

      As shown in Figure 11-14, this method essentially creates a recursion tree, with the goal of making the values of leaf nodes as even as possible. Of course, it is not necessary to divide the data into 3 buckets every round; the specific division method can be flexibly chosen according to data characteristics.

      Figure 11-14 \u00a0 Recursively dividing buckets

      If we know the probability distribution of product prices in advance, we can set the price dividing line for each bucket based on the data probability distribution. It is worth noting that the data distribution does not necessarily need to be specifically calculated, but can also be approximated using a certain probability model based on data characteristics.

      As shown in Figure 11-15, we assume that product prices follow a normal distribution, which allows us to reasonably set price intervals to evenly distribute products to each bucket.

      Figure 11-15 \u00a0 Dividing buckets based on probability distribution

      "},{"location":"chapter_sorting/counting_sort/","title":"11.9 \u00a0 Counting Sort","text":"

      Counting sort (counting sort) achieves sorting by counting the number of elements, typically applied to integer arrays.

      "},{"location":"chapter_sorting/counting_sort/#1191-simple-implementation","title":"11.9.1 \u00a0 Simple Implementation","text":"

      Let's start with a simple example. Given an array nums of length \\(n\\), where the elements are all \"non-negative integers\", the overall flow of counting sort is shown in Figure 11-16.

      1. Traverse the array to find the largest number, denoted as \\(m\\), and then create an auxiliary array counter of length \\(m + 1\\).
      2. Use counter to count the number of occurrences of each number in nums, where counter[num] corresponds to the number of occurrences of the number num. The counting method is simple: just traverse nums (let the current number be num), and increase counter[num] by \\(1\\) in each round.
      3. Since each index of counter is naturally ordered, this is equivalent to all numbers being sorted. Next, we traverse counter and fill in nums in ascending order based on the number of occurrences of each number.

      Figure 11-16 \u00a0 Counting sort flow

      The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby counting_sort.py
      def counting_sort_naive(nums: list[int]):\n    \"\"\"Counting sort\"\"\"\n    # Simple implementation, cannot be used for sorting objects\n    # 1. Count the maximum element m in the array\n    m = 0\n    for num in nums:\n        m = max(m, num)\n    # 2. Count the occurrence of each number\n    # counter[num] represents the occurrence of num\n    counter = [0] * (m + 1)\n    for num in nums:\n        counter[num] += 1\n    # 3. Traverse counter, filling each element back into the original array nums\n    i = 0\n    for num in range(m + 1):\n        for _ in range(counter[num]):\n            nums[i] = num\n            i += 1\n
      counting_sort.cpp
      /* Counting sort */\n// Simple implementation, cannot be used for sorting objects\nvoid countingSortNaive(vector<int> &nums) {\n    // 1. Count the maximum element m in the array\n    int m = 0;\n    for (int num : nums) {\n        m = max(m, num);\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    vector<int> counter(m + 1, 0);\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. Traverse counter, filling each element back into the original array nums\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
      counting_sort.java
      /* Counting sort */\n// Simple implementation, cannot be used for sorting objects\nvoid countingSortNaive(int[] nums) {\n    // 1. Count the maximum element m in the array\n    int m = 0;\n    for (int num : nums) {\n        m = Math.max(m, num);\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    int[] counter = new int[m + 1];\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. Traverse counter, filling each element back into the original array nums\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
      counting_sort.cs
      /* Counting sort */\n// Simple implementation, cannot be used for sorting objects\nvoid CountingSortNaive(int[] nums) {\n    // 1. Count the maximum element m in the array\n    int m = 0;\n    foreach (int num in nums) {\n        m = Math.Max(m, num);\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    int[] counter = new int[m + 1];\n    foreach (int num in nums) {\n        counter[num]++;\n    }\n    // 3. Traverse counter, filling each element back into the original array nums\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
      counting_sort.go
      /* Counting sort */\n// Simple implementation, cannot be used for sorting objects\nfunc countingSortNaive(nums []int) {\n    // 1. Count the maximum element m in the array\n    m := 0\n    for _, num := range nums {\n        if num > m {\n            m = num\n        }\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    counter := make([]int, m+1)\n    for _, num := range nums {\n        counter[num]++\n    }\n    // 3. Traverse counter, filling each element back into the original array nums\n    for i, num := 0, 0; num < m+1; num++ {\n        for j := 0; j < counter[num]; j++ {\n            nums[i] = num\n            i++\n        }\n    }\n}\n
      counting_sort.swift
      /* Counting sort */\n// Simple implementation, cannot be used for sorting objects\nfunc countingSortNaive(nums: inout [Int]) {\n    // 1. Count the maximum element m in the array\n    let m = nums.max()!\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    var counter = Array(repeating: 0, count: m + 1)\n    for num in nums {\n        counter[num] += 1\n    }\n    // 3. Traverse counter, filling each element back into the original array nums\n    var i = 0\n    for num in 0 ..< m + 1 {\n        for _ in 0 ..< counter[num] {\n            nums[i] = num\n            i += 1\n        }\n    }\n}\n
      counting_sort.js
      /* Counting sort */\n// Simple implementation, cannot be used for sorting objects\nfunction countingSortNaive(nums) {\n    // 1. Count the maximum element m in the array\n    let m = Math.max(...nums);\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    const counter = new Array(m + 1).fill(0);\n    for (const num of nums) {\n        counter[num]++;\n    }\n    // 3. Traverse counter, filling each element back into the original array nums\n    let i = 0;\n    for (let num = 0; num < m + 1; num++) {\n        for (let j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
      counting_sort.ts
      /* Counting sort */\n// Simple implementation, cannot be used for sorting objects\nfunction countingSortNaive(nums: number[]): void {\n    // 1. Count the maximum element m in the array\n    let m: number = Math.max(...nums);\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    const counter: number[] = new Array<number>(m + 1).fill(0);\n    for (const num of nums) {\n        counter[num]++;\n    }\n    // 3. Traverse counter, filling each element back into the original array nums\n    let i = 0;\n    for (let num = 0; num < m + 1; num++) {\n        for (let j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
      counting_sort.dart
      /* Counting sort */\n// Simple implementation, cannot be used for sorting objects\nvoid countingSortNaive(List<int> nums) {\n  // 1. Count the maximum element m in the array\n  int m = 0;\n  for (int _num in nums) {\n    m = max(m, _num);\n  }\n  // 2. Count the occurrence of each number\n  // counter[_num] represents occurrence count of _num\n  List<int> counter = List.filled(m + 1, 0);\n  for (int _num in nums) {\n    counter[_num]++;\n  }\n  // 3. Traverse counter, filling each element back into the original array nums\n  int i = 0;\n  for (int _num = 0; _num < m + 1; _num++) {\n    for (int j = 0; j < counter[_num]; j++, i++) {\n      nums[i] = _num;\n    }\n  }\n}\n
      counting_sort.rs
      /* Counting sort */\n// Simple implementation, cannot be used for sorting objects\nfn counting_sort_naive(nums: &mut [i32]) {\n    // 1. Count the maximum element m in the array\n    let m = *nums.iter().max().unwrap();\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    let mut counter = vec![0; m as usize + 1];\n    for &num in nums.iter() {\n        counter[num as usize] += 1;\n    }\n    // 3. Traverse counter, filling each element back into the original array nums\n    let mut i = 0;\n    for num in 0..m + 1 {\n        for _ in 0..counter[num as usize] {\n            nums[i] = num;\n            i += 1;\n        }\n    }\n}\n
      counting_sort.c
      /* Counting sort */\n// Simple implementation, cannot be used for sorting objects\nvoid countingSortNaive(int nums[], int size) {\n    // 1. Count the maximum element m in the array\n    int m = 0;\n    for (int i = 0; i < size; i++) {\n        if (nums[i] > m) {\n            m = nums[i];\n        }\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    int *counter = calloc(m + 1, sizeof(int));\n    for (int i = 0; i < size; i++) {\n        counter[nums[i]]++;\n    }\n    // 3. Traverse counter, filling each element back into the original array nums\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n    // 4. Free memory\n    free(counter);\n}\n
      counting_sort.kt
      /* Counting sort */\n// Simple implementation, cannot be used for sorting objects\nfun countingSortNaive(nums: IntArray) {\n    // 1. Count the maximum element m in the array\n    var m = 0\n    for (num in nums) {\n        m = max(m, num)\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    val counter = IntArray(m + 1)\n    for (num in nums) {\n        counter[num]++\n    }\n    // 3. Traverse counter, filling each element back into the original array nums\n    var i = 0\n    for (num in 0..<m + 1) {\n        var j = 0\n        while (j < counter[num]) {\n            nums[i] = num\n            j++\n            i++\n        }\n    }\n}\n
      counting_sort.rb
      ### Counting sort ###\ndef counting_sort_naive(nums)\n  # Simple implementation, cannot be used for sorting objects\n  # 1. Count the maximum element m in the array\n  m = 0\n  nums.each { |num| m = [m, num].max }\n  # 2. Count the occurrence of each number\n  # counter[num] represents the occurrence of num\n  counter = Array.new(m + 1, 0)\n  nums.each { |num| counter[num] += 1 }\n  # 3. Traverse counter, filling each element back into the original array nums\n  i = 0\n  for num in 0...(m + 1)\n    (0...counter[num]).each do\n      nums[i] = num\n      i += 1\n    end\n  end\nend\n

      Connection between counting sort and bucket sort

      From the perspective of bucket sort, we can regard each index of the counting array counter in counting sort as a bucket, and the process of counting quantities as distributing each element to the corresponding bucket. Essentially, counting sort is a special case of bucket sort for integer data.

      "},{"location":"chapter_sorting/counting_sort/#1192-complete-implementation","title":"11.9.2 \u00a0 Complete Implementation","text":"

      Observant readers may have noticed that if the input data is objects, step 3. above becomes invalid. Suppose the input data is product objects, and we want to sort the products by price (a member variable of the class), but the above algorithm can only give the sorting result of prices.

      So how can we obtain the sorting result of the original data? We first calculate the \"prefix sum\" of counter. As the name suggests, the prefix sum at index i, prefix[i], equals the sum of the first i elements of the array:

      \\[ \\text{prefix}[i] = \\sum_{j=0}^i \\text{counter[j]} \\]

      The prefix sum has a clear meaning: prefix[num] - 1 represents the index of the last occurrence of element num in the result array res. This information is very critical because it tells us where each element should appear in the result array. Next, we traverse each element num of the original array nums in reverse order, performing the following two steps in each iteration.

      1. Fill num into the array res at index prefix[num] - 1.
      2. Decrease the prefix sum prefix[num] by \\(1\\) to get the index for the next placement of num.

      After the traversal is complete, the array res contains the sorted result, and finally res is used to overwrite the original array nums. The complete counting sort flow is shown in Figure 11-17.

      <1><2><3><4><5><6><7><8>

      Figure 11-17 \u00a0 Counting sort steps

      The implementation code of counting sort is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby counting_sort.py
      def counting_sort(nums: list[int]):\n    \"\"\"Counting sort\"\"\"\n    # Complete implementation, can sort objects and is a stable sort\n    # 1. Count the maximum element m in the array\n    m = max(nums)\n    # 2. Count the occurrence of each number\n    # counter[num] represents the occurrence of num\n    counter = [0] * (m + 1)\n    for num in nums:\n        counter[num] += 1\n    # 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n    # counter[num]-1 is the last index where num appears in res\n    for i in range(m):\n        counter[i + 1] += counter[i]\n    # 4. Traverse nums in reverse order, placing each element into the result array res\n    # Initialize the array res to record results\n    n = len(nums)\n    res = [0] * n\n    for i in range(n - 1, -1, -1):\n        num = nums[i]\n        res[counter[num] - 1] = num  # Place num at the corresponding index\n        counter[num] -= 1  # Decrement the prefix sum by 1, getting the next index to place num\n    # Use result array res to overwrite the original array nums\n    for i in range(n):\n        nums[i] = res[i]\n
      counting_sort.cpp
      /* Counting sort */\n// Complete implementation, can sort objects and is a stable sort\nvoid countingSort(vector<int> &nums) {\n    // 1. Count the maximum element m in the array\n    int m = 0;\n    for (int num : nums) {\n        m = max(m, num);\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    vector<int> counter(m + 1, 0);\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n    // counter[num]-1 is the last index where num appears in res\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. Traverse nums in reverse order, placing each element into the result array res\n    // Initialize the array res to record results\n    int n = nums.size();\n    vector<int> res(n);\n    for (int i = n - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // Place num at the corresponding index\n        counter[num]--;              // Decrement the prefix sum by 1, getting the next index to place num\n    }\n    // Use result array res to overwrite the original array nums\n    nums = res;\n}\n
      counting_sort.java
      /* Counting sort */\n// Complete implementation, can sort objects and is a stable sort\nvoid countingSort(int[] nums) {\n    // 1. Count the maximum element m in the array\n    int m = 0;\n    for (int num : nums) {\n        m = Math.max(m, num);\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    int[] counter = new int[m + 1];\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n    // counter[num]-1 is the last index where num appears in res\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. Traverse nums in reverse order, placing each element into the result array res\n    // Initialize the array res to record results\n    int n = nums.length;\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // Place num at the corresponding index\n        counter[num]--; // Decrement the prefix sum by 1, getting the next index to place num\n    }\n    // Use result array res to overwrite the original array nums\n    for (int i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n
      counting_sort.cs
      /* Counting sort */\n// Complete implementation, can sort objects and is a stable sort\nvoid CountingSort(int[] nums) {\n    // 1. Count the maximum element m in the array\n    int m = 0;\n    foreach (int num in nums) {\n        m = Math.Max(m, num);\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    int[] counter = new int[m + 1];\n    foreach (int num in nums) {\n        counter[num]++;\n    }\n    // 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n    // counter[num]-1 is the last index where num appears in res\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. Traverse nums in reverse order, placing each element into the result array res\n    // Initialize the array res to record results\n    int n = nums.Length;\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // Place num at the corresponding index\n        counter[num]--; // Decrement the prefix sum by 1, getting the next index to place num\n    }\n    // Use result array res to overwrite the original array nums\n    for (int i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n
      counting_sort.go
      /* Counting sort */\n// Complete implementation, can sort objects and is a stable sort\nfunc countingSort(nums []int) {\n    // 1. Count the maximum element m in the array\n    m := 0\n    for _, num := range nums {\n        if num > m {\n            m = num\n        }\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    counter := make([]int, m+1)\n    for _, num := range nums {\n        counter[num]++\n    }\n    // 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n    // counter[num]-1 is the last index where num appears in res\n    for i := 0; i < m; i++ {\n        counter[i+1] += counter[i]\n    }\n    // 4. Traverse nums in reverse order, placing each element into the result array res\n    // Initialize the array res to record results\n    n := len(nums)\n    res := make([]int, n)\n    for i := n - 1; i >= 0; i-- {\n        num := nums[i]\n        // Place num at the corresponding index\n        res[counter[num]-1] = num\n        // Decrement the prefix sum by 1, getting the next index to place num\n        counter[num]--\n    }\n    // Use result array res to overwrite the original array nums\n    copy(nums, res)\n}\n
      counting_sort.swift
      /* Counting sort */\n// Complete implementation, can sort objects and is a stable sort\nfunc countingSort(nums: inout [Int]) {\n    // 1. Count the maximum element m in the array\n    let m = nums.max()!\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    var counter = Array(repeating: 0, count: m + 1)\n    for num in nums {\n        counter[num] += 1\n    }\n    // 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n    // counter[num]-1 is the last index where num appears in res\n    for i in 0 ..< m {\n        counter[i + 1] += counter[i]\n    }\n    // 4. Traverse nums in reverse order, placing each element into the result array res\n    // Initialize the array res to record results\n    var res = Array(repeating: 0, count: nums.count)\n    for i in nums.indices.reversed() {\n        let num = nums[i]\n        res[counter[num] - 1] = num // Place num at the corresponding index\n        counter[num] -= 1 // Decrement the prefix sum by 1, getting the next index to place num\n    }\n    // Use result array res to overwrite the original array nums\n    for i in nums.indices {\n        nums[i] = res[i]\n    }\n}\n
      counting_sort.js
      /* Counting sort */\n// Complete implementation, can sort objects and is a stable sort\nfunction countingSort(nums) {\n    // 1. Count the maximum element m in the array\n    let m = Math.max(...nums);\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    const counter = new Array(m + 1).fill(0);\n    for (const num of nums) {\n        counter[num]++;\n    }\n    // 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n    // counter[num]-1 is the last index where num appears in res\n    for (let i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. Traverse nums in reverse order, placing each element into the result array res\n    // Initialize the array res to record results\n    const n = nums.length;\n    const res = new Array(n);\n    for (let i = n - 1; i >= 0; i--) {\n        const num = nums[i];\n        res[counter[num] - 1] = num; // Place num at the corresponding index\n        counter[num]--; // Decrement the prefix sum by 1, getting the next index to place num\n    }\n    // Use result array res to overwrite the original array nums\n    for (let i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n
      counting_sort.ts
      /* Counting sort */\n// Complete implementation, can sort objects and is a stable sort\nfunction countingSort(nums: number[]): void {\n    // 1. Count the maximum element m in the array\n    let m: number = Math.max(...nums);\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    const counter: number[] = new Array<number>(m + 1).fill(0);\n    for (const num of nums) {\n        counter[num]++;\n    }\n    // 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n    // counter[num]-1 is the last index where num appears in res\n    for (let i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. Traverse nums in reverse order, placing each element into the result array res\n    // Initialize the array res to record results\n    const n = nums.length;\n    const res: number[] = new Array<number>(n);\n    for (let i = n - 1; i >= 0; i--) {\n        const num = nums[i];\n        res[counter[num] - 1] = num; // Place num at the corresponding index\n        counter[num]--; // Decrement the prefix sum by 1, getting the next index to place num\n    }\n    // Use result array res to overwrite the original array nums\n    for (let i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n
      counting_sort.dart
      /* Counting sort */\n// Complete implementation, can sort objects and is a stable sort\nvoid countingSort(List<int> nums) {\n  // 1. Count the maximum element m in the array\n  int m = 0;\n  for (int _num in nums) {\n    m = max(m, _num);\n  }\n  // 2. Count the occurrence of each number\n  // counter[_num] represents occurrence count of _num\n  List<int> counter = List.filled(m + 1, 0);\n  for (int _num in nums) {\n    counter[_num]++;\n  }\n  // 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n  // That is, counter[_num]-1 is the last occurrence index of _num in res\n  for (int i = 0; i < m; i++) {\n    counter[i + 1] += counter[i];\n  }\n  // 4. Traverse nums in reverse order, placing each element into the result array res\n  // Initialize the array res to record results\n  int n = nums.length;\n  List<int> res = List.filled(n, 0);\n  for (int i = n - 1; i >= 0; i--) {\n    int _num = nums[i];\n    res[counter[_num] - 1] = _num; // Place _num at corresponding index\n    counter[_num]--; // Decrement prefix sum by 1 to get next placement index for _num\n  }\n  // Use result array res to overwrite the original array nums\n  nums.setAll(0, res);\n}\n
      counting_sort.rs
      /* Counting sort */\n// Complete implementation, can sort objects and is a stable sort\nfn counting_sort(nums: &mut [i32]) {\n    // 1. Count the maximum element m in the array\n    let m = *nums.iter().max().unwrap() as usize;\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    let mut counter = vec![0; m + 1];\n    for &num in nums.iter() {\n        counter[num as usize] += 1;\n    }\n    // 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n    // counter[num]-1 is the last index where num appears in res\n    for i in 0..m {\n        counter[i + 1] += counter[i];\n    }\n    // 4. Traverse nums in reverse order, placing each element into the result array res\n    // Initialize the array res to record results\n    let n = nums.len();\n    let mut res = vec![0; n];\n    for i in (0..n).rev() {\n        let num = nums[i];\n        res[counter[num as usize] - 1] = num; // Place num at the corresponding index\n        counter[num as usize] -= 1; // Decrement the prefix sum by 1, getting the next index to place num\n    }\n    // Use result array res to overwrite the original array nums\n    nums.copy_from_slice(&res)\n}\n
      counting_sort.c
      /* Counting sort */\n// Complete implementation, can sort objects and is a stable sort\nvoid countingSort(int nums[], int size) {\n    // 1. Count the maximum element m in the array\n    int m = 0;\n    for (int i = 0; i < size; i++) {\n        if (nums[i] > m) {\n            m = nums[i];\n        }\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    int *counter = calloc(m, sizeof(int));\n    for (int i = 0; i < size; i++) {\n        counter[nums[i]]++;\n    }\n    // 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n    // counter[num]-1 is the last index where num appears in res\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. Traverse nums in reverse order, placing each element into the result array res\n    // Initialize the array res to record results\n    int *res = malloc(sizeof(int) * size);\n    for (int i = size - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // Place num at the corresponding index\n        counter[num]--;              // Decrement the prefix sum by 1, getting the next index to place num\n    }\n    // Use result array res to overwrite the original array nums\n    memcpy(nums, res, size * sizeof(int));\n    // 5. Free memory\n    free(res);\n    free(counter);\n}\n
      counting_sort.kt
      /* Counting sort */\n// Complete implementation, can sort objects and is a stable sort\nfun countingSort(nums: IntArray) {\n    // 1. Count the maximum element m in the array\n    var m = 0\n    for (num in nums) {\n        m = max(m, num)\n    }\n    // 2. Count the occurrence of each number\n    // counter[num] represents the occurrence of num\n    val counter = IntArray(m + 1)\n    for (num in nums) {\n        counter[num]++\n    }\n    // 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n    // counter[num]-1 is the last index where num appears in res\n    for (i in 0..<m) {\n        counter[i + 1] += counter[i]\n    }\n    // 4. Traverse nums in reverse order, placing each element into the result array res\n    // Initialize the array res to record results\n    val n = nums.size\n    val res = IntArray(n)\n    for (i in n - 1 downTo 0) {\n        val num = nums[i]\n        res[counter[num] - 1] = num // Place num at the corresponding index\n        counter[num]-- // Decrement the prefix sum by 1, getting the next index to place num\n    }\n    // Use result array res to overwrite the original array nums\n    for (i in 0..<n) {\n        nums[i] = res[i]\n    }\n}\n
      counting_sort.rb
      ### Counting sort ###\ndef counting_sort(nums)\n  # Complete implementation, can sort objects and is a stable sort\n  # 1. Count the maximum element m in the array\n  m = nums.max\n  # 2. Count the occurrence of each number\n  # counter[num] represents the occurrence of num\n  counter = Array.new(m + 1, 0)\n  nums.each { |num| counter[num] += 1 }\n  # 3. Calculate the prefix sum of counter, converting \"occurrence count\" to \"tail index\"\n  # counter[num]-1 is the last index where num appears in res\n  (0...m).each { |i| counter[i + 1] += counter[i] }\n  # 4. Traverse nums in reverse, fill elements into result array res\n  # Initialize the array res to record results\n  n = nums.length\n  res = Array.new(n, 0)\n  (n - 1).downto(0).each do |i|\n    num = nums[i]\n    res[counter[num] - 1] = num # Place num at the corresponding index\n    counter[num] -= 1 # Decrement the prefix sum by 1, getting the next index to place num\n  end\n  # Use result array res to overwrite the original array nums\n  (0...n).each { |i| nums[i] = res[i] }\nend\n
      "},{"location":"chapter_sorting/counting_sort/#1193-algorithm-characteristics","title":"11.9.3 \u00a0 Algorithm Characteristics","text":"
      • Time complexity of \\(O(n + m)\\), non-adaptive sorting: Involves traversing nums and traversing counter, both using linear time. Generally, \\(n \\gg m\\), and time complexity tends toward \\(O(n)\\).
      • Space complexity of \\(O(n + m)\\), non-in-place sorting: Uses arrays res and counter of lengths \\(n\\) and \\(m\\) respectively.
      • Stable sorting: Since elements are filled into res in a \"right-to-left\" order, traversing nums in reverse can avoid changing the relative positions of equal elements, thereby achieving stable sorting. In fact, traversing nums in forward order can also yield correct sorting results, but the result would be unstable.
      "},{"location":"chapter_sorting/counting_sort/#1194-limitations","title":"11.9.4 \u00a0 Limitations","text":"

      By this point, you might think counting sort is very clever, as it can achieve efficient sorting just by counting quantities. However, the prerequisites for using counting sort are relatively strict.

      Counting sort is only suitable for non-negative integers. If you want to apply it to other types of data, you need to ensure that the data can be converted to non-negative integers without changing the relative size relationships between elements. For example, for an integer array containing negative numbers, you can first add a constant to all numbers to convert them all to positive numbers, and then convert them back after sorting is complete.

      Counting sort is suitable for situations where the data volume is large but the data range is small. For example, in the above example, \\(m\\) cannot be too large, otherwise it will occupy too much space. And when \\(n \\ll m\\), counting sort uses \\(O(m)\\) time, which may be slower than \\(O(n \\log n)\\) sorting algorithms.

      "},{"location":"chapter_sorting/heap_sort/","title":"11.7 \u00a0 Heap Sort","text":"

      Tip

      Before reading this section, please ensure you have completed the \"Heap\" chapter.

      Heap sort (heap sort) is an efficient sorting algorithm based on the heap data structure. We can use the \"build heap operation\" and \"element out-heap operation\" that we have already learned to implement heap sort.

      1. Input the array and build a min-heap, at which point the smallest element is at the heap top.
      2. Continuously perform the out-heap operation, record the out-heap elements in sequence, and an ascending sorted sequence can be obtained.

      Although the above method is feasible, it requires an additional array to save the popped elements, which is quite wasteful of space. In practice, we usually use a more elegant implementation method.

      "},{"location":"chapter_sorting/heap_sort/#1171-algorithm-flow","title":"11.7.1 \u00a0 Algorithm Flow","text":"

      Assume the array length is \\(n\\). The flow of heap sort is shown in Figure 11-12.

      1. Input the array and build a max-heap. After completion, the largest element is at the heap top.
      2. Swap the heap top element (first element) with the heap bottom element (last element). After the swap is complete, reduce the heap length by \\(1\\) and increase the count of sorted elements by \\(1\\).
      3. Starting from the heap top element, perform top-to-bottom heapify operation (sift down). After heapify is complete, the heap property is restored.
      4. Loop through steps 2. and 3. After looping \\(n - 1\\) rounds, the array sorting can be completed.

      Tip

      In fact, the element out-heap operation also includes steps 2. and 3., with just an additional step to pop the element.

      <1><2><3><4><5><6><7><8><9><10><11><12>

      Figure 11-12 \u00a0 Heap sort steps

      In the code implementation, we use the same top-to-bottom heapify function sift_down() from the \"Heap\" chapter. It is worth noting that since the heap length will decrease as the largest element is extracted, we need to add a length parameter \\(n\\) to the sift_down() function to specify the current effective length of the heap. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby heap_sort.py
      def sift_down(nums: list[int], n: int, i: int):\n    \"\"\"Heap length is n, start heapifying node i, from top to bottom\"\"\"\n    while True:\n        # Determine the largest node among i, l, r, noted as ma\n        l = 2 * i + 1\n        r = 2 * i + 2\n        ma = i\n        if l < n and nums[l] > nums[ma]:\n            ma = l\n        if r < n and nums[r] > nums[ma]:\n            ma = r\n        # If node i is the largest or indices l, r are out of bounds, no further heapification needed, break\n        if ma == i:\n            break\n        # Swap two nodes\n        nums[i], nums[ma] = nums[ma], nums[i]\n        # Loop downwards heapification\n        i = ma\n\ndef heap_sort(nums: list[int]):\n    \"\"\"Heap sort\"\"\"\n    # Build heap operation: heapify all nodes except leaves\n    for i in range(len(nums) // 2 - 1, -1, -1):\n        sift_down(nums, len(nums), i)\n    # Extract the largest element from the heap and repeat for n-1 rounds\n    for i in range(len(nums) - 1, 0, -1):\n        # Swap the root node with the rightmost leaf node (swap the first element with the last element)\n        nums[0], nums[i] = nums[i], nums[0]\n        # Start heapifying the root node, from top to bottom\n        sift_down(nums, i, 0)\n
      heap_sort.cpp
      /* Heap length is n, start heapifying node i, from top to bottom */\nvoid siftDown(vector<int> &nums, int n, int i) {\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // Swap two nodes\n        if (ma == i) {\n            break;\n        }\n        // Swap two nodes\n        swap(nums[i], nums[ma]);\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n\n/* Heap sort */\nvoid heapSort(vector<int> &nums) {\n    // Build heap operation: heapify all nodes except leaves\n    for (int i = nums.size() / 2 - 1; i >= 0; --i) {\n        siftDown(nums, nums.size(), i);\n    }\n    // Extract the largest element from the heap and repeat for n-1 rounds\n    for (int i = nums.size() - 1; i > 0; --i) {\n        // Delete node\n        swap(nums[0], nums[i]);\n        // Start heapifying the root node, from top to bottom\n        siftDown(nums, i, 0);\n    }\n}\n
      heap_sort.java
      /* Heap length is n, start heapifying node i, from top to bottom */\nvoid siftDown(int[] nums, int n, int i) {\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // Swap two nodes\n        if (ma == i)\n            break;\n        // Swap two nodes\n        int temp = nums[i];\n        nums[i] = nums[ma];\n        nums[ma] = temp;\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n\n/* Heap sort */\nvoid heapSort(int[] nums) {\n    // Build heap operation: heapify all nodes except leaves\n    for (int i = nums.length / 2 - 1; i >= 0; i--) {\n        siftDown(nums, nums.length, i);\n    }\n    // Extract the largest element from the heap and repeat for n-1 rounds\n    for (int i = nums.length - 1; i > 0; i--) {\n        // Delete node\n        int tmp = nums[0];\n        nums[0] = nums[i];\n        nums[i] = tmp;\n        // Start heapifying the root node, from top to bottom\n        siftDown(nums, i, 0);\n    }\n}\n
      heap_sort.cs
      /* Heap length is n, start heapifying node i, from top to bottom */\nvoid SiftDown(int[] nums, int n, int i) {\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // Swap two nodes\n        if (ma == i)\n            break;\n        // Swap two nodes\n        (nums[ma], nums[i]) = (nums[i], nums[ma]);\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n\n/* Heap sort */\nvoid HeapSort(int[] nums) {\n    // Build heap operation: heapify all nodes except leaves\n    for (int i = nums.Length / 2 - 1; i >= 0; i--) {\n        SiftDown(nums, nums.Length, i);\n    }\n    // Extract the largest element from the heap and repeat for n-1 rounds\n    for (int i = nums.Length - 1; i > 0; i--) {\n        // Delete node\n        (nums[i], nums[0]) = (nums[0], nums[i]);\n        // Start heapifying the root node, from top to bottom\n        SiftDown(nums, i, 0);\n    }\n}\n
      heap_sort.go
      /* Heap length is n, start heapifying node i, from top to bottom */\nfunc siftDown(nums *[]int, n, i int) {\n    for true {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        l := 2*i + 1\n        r := 2*i + 2\n        ma := i\n        if l < n && (*nums)[l] > (*nums)[ma] {\n            ma = l\n        }\n        if r < n && (*nums)[r] > (*nums)[ma] {\n            ma = r\n        }\n        // Swap two nodes\n        if ma == i {\n            break\n        }\n        // Swap two nodes\n        (*nums)[i], (*nums)[ma] = (*nums)[ma], (*nums)[i]\n        // Loop downwards heapification\n        i = ma\n    }\n}\n\n/* Heap sort */\nfunc heapSort(nums *[]int) {\n    // Build heap operation: heapify all nodes except leaves\n    for i := len(*nums)/2 - 1; i >= 0; i-- {\n        siftDown(nums, len(*nums), i)\n    }\n    // Extract the largest element from the heap and repeat for n-1 rounds\n    for i := len(*nums) - 1; i > 0; i-- {\n        // Delete node\n        (*nums)[0], (*nums)[i] = (*nums)[i], (*nums)[0]\n        // Start heapifying the root node, from top to bottom\n        siftDown(nums, i, 0)\n    }\n}\n
      heap_sort.swift
      /* Heap length is n, start heapifying node i, from top to bottom */\nfunc siftDown(nums: inout [Int], n: Int, i: Int) {\n    var i = i\n    while true {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        let l = 2 * i + 1\n        let r = 2 * i + 2\n        var ma = i\n        if l < n, nums[l] > nums[ma] {\n            ma = l\n        }\n        if r < n, nums[r] > nums[ma] {\n            ma = r\n        }\n        // Swap two nodes\n        if ma == i {\n            break\n        }\n        // Swap two nodes\n        nums.swapAt(i, ma)\n        // Loop downwards heapification\n        i = ma\n    }\n}\n\n/* Heap sort */\nfunc heapSort(nums: inout [Int]) {\n    // Build heap operation: heapify all nodes except leaves\n    for i in stride(from: nums.count / 2 - 1, through: 0, by: -1) {\n        siftDown(nums: &nums, n: nums.count, i: i)\n    }\n    // Extract the largest element from the heap and repeat for n-1 rounds\n    for i in nums.indices.dropFirst().reversed() {\n        // Delete node\n        nums.swapAt(0, i)\n        // Start heapifying the root node, from top to bottom\n        siftDown(nums: &nums, n: i, i: 0)\n    }\n}\n
      heap_sort.js
      /* Heap length is n, start heapifying node i, from top to bottom */\nfunction siftDown(nums, n, i) {\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        let l = 2 * i + 1;\n        let r = 2 * i + 2;\n        let ma = i;\n        if (l < n && nums[l] > nums[ma]) {\n            ma = l;\n        }\n        if (r < n && nums[r] > nums[ma]) {\n            ma = r;\n        }\n        // Swap two nodes\n        if (ma === i) {\n            break;\n        }\n        // Swap two nodes\n        [nums[i], nums[ma]] = [nums[ma], nums[i]];\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n\n/* Heap sort */\nfunction heapSort(nums) {\n    // Build heap operation: heapify all nodes except leaves\n    for (let i = Math.floor(nums.length / 2) - 1; i >= 0; i--) {\n        siftDown(nums, nums.length, i);\n    }\n    // Extract the largest element from the heap and repeat for n-1 rounds\n    for (let i = nums.length - 1; i > 0; i--) {\n        // Delete node\n        [nums[0], nums[i]] = [nums[i], nums[0]];\n        // Start heapifying the root node, from top to bottom\n        siftDown(nums, i, 0);\n    }\n}\n
      heap_sort.ts
      /* Heap length is n, start heapifying node i, from top to bottom */\nfunction siftDown(nums: number[], n: number, i: number): void {\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        let l = 2 * i + 1;\n        let r = 2 * i + 2;\n        let ma = i;\n        if (l < n && nums[l] > nums[ma]) {\n            ma = l;\n        }\n        if (r < n && nums[r] > nums[ma]) {\n            ma = r;\n        }\n        // Swap two nodes\n        if (ma === i) {\n            break;\n        }\n        // Swap two nodes\n        [nums[i], nums[ma]] = [nums[ma], nums[i]];\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n\n/* Heap sort */\nfunction heapSort(nums: number[]): void {\n    // Build heap operation: heapify all nodes except leaves\n    for (let i = Math.floor(nums.length / 2) - 1; i >= 0; i--) {\n        siftDown(nums, nums.length, i);\n    }\n    // Extract the largest element from the heap and repeat for n-1 rounds\n    for (let i = nums.length - 1; i > 0; i--) {\n        // Delete node\n        [nums[0], nums[i]] = [nums[i], nums[0]];\n        // Start heapifying the root node, from top to bottom\n        siftDown(nums, i, 0);\n    }\n}\n
      heap_sort.dart
      /* Heap length is n, start heapifying node i, from top to bottom */\nvoid siftDown(List<int> nums, int n, int i) {\n  while (true) {\n    // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n    int l = 2 * i + 1;\n    int r = 2 * i + 2;\n    int ma = i;\n    if (l < n && nums[l] > nums[ma]) ma = l;\n    if (r < n && nums[r] > nums[ma]) ma = r;\n    // Swap two nodes\n    if (ma == i) break;\n    // Swap two nodes\n    int temp = nums[i];\n    nums[i] = nums[ma];\n    nums[ma] = temp;\n    // Loop downwards heapification\n    i = ma;\n  }\n}\n\n/* Heap sort */\nvoid heapSort(List<int> nums) {\n  // Build heap operation: heapify all nodes except leaves\n  for (int i = nums.length ~/ 2 - 1; i >= 0; i--) {\n    siftDown(nums, nums.length, i);\n  }\n  // Extract the largest element from the heap and repeat for n-1 rounds\n  for (int i = nums.length - 1; i > 0; i--) {\n    // Delete node\n    int tmp = nums[0];\n    nums[0] = nums[i];\n    nums[i] = tmp;\n    // Start heapifying the root node, from top to bottom\n    siftDown(nums, i, 0);\n  }\n}\n
      heap_sort.rs
      /* Heap length is n, start heapifying node i, from top to bottom */\nfn sift_down(nums: &mut [i32], n: usize, mut i: usize) {\n    loop {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        let l = 2 * i + 1;\n        let r = 2 * i + 2;\n        let mut ma = i;\n        if l < n && nums[l] > nums[ma] {\n            ma = l;\n        }\n        if r < n && nums[r] > nums[ma] {\n            ma = r;\n        }\n        // Swap two nodes\n        if ma == i {\n            break;\n        }\n        // Swap two nodes\n        nums.swap(i, ma);\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n\n/* Heap sort */\nfn heap_sort(nums: &mut [i32]) {\n    // Build heap operation: heapify all nodes except leaves\n    for i in (0..nums.len() / 2).rev() {\n        sift_down(nums, nums.len(), i);\n    }\n    // Extract the largest element from the heap and repeat for n-1 rounds\n    for i in (1..nums.len()).rev() {\n        // Delete node\n        nums.swap(0, i);\n        // Start heapifying the root node, from top to bottom\n        sift_down(nums, i, 0);\n    }\n}\n
      heap_sort.c
      /* Heap length is n, start heapifying node i, from top to bottom */\nvoid siftDown(int nums[], int n, int i) {\n    while (1) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // Swap two nodes\n        if (ma == i) {\n            break;\n        }\n        // Swap two nodes\n        int temp = nums[i];\n        nums[i] = nums[ma];\n        nums[ma] = temp;\n        // Loop downwards heapification\n        i = ma;\n    }\n}\n\n/* Heap sort */\nvoid heapSort(int nums[], int n) {\n    // Build heap operation: heapify all nodes except leaves\n    for (int i = n / 2 - 1; i >= 0; --i) {\n        siftDown(nums, n, i);\n    }\n    // Extract the largest element from the heap and repeat for n-1 rounds\n    for (int i = n - 1; i > 0; --i) {\n        // Delete node\n        int tmp = nums[0];\n        nums[0] = nums[i];\n        nums[i] = tmp;\n        // Start heapifying the root node, from top to bottom\n        siftDown(nums, i, 0);\n    }\n}\n
      heap_sort.kt
      /* Heap length is n, start heapifying node i, from top to bottom */\nfun siftDown(nums: IntArray, n: Int, li: Int) {\n    var i = li\n    while (true) {\n        // If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n        val l = 2 * i + 1\n        val r = 2 * i + 2\n        var ma = i\n        if (l < n && nums[l] > nums[ma]) \n            ma = l\n        if (r < n && nums[r] > nums[ma]) \n            ma = r\n        // Swap two nodes\n        if (ma == i) \n            break\n        // Swap two nodes\n        val temp = nums[i]\n        nums[i] = nums[ma]\n        nums[ma] = temp\n        // Loop downwards heapification\n        i = ma\n    }\n}\n\n/* Heap sort */\nfun heapSort(nums: IntArray) {\n    // Build heap operation: heapify all nodes except leaves\n    for (i in nums.size / 2 - 1 downTo 0) {\n        siftDown(nums, nums.size, i)\n    }\n    // Extract the largest element from the heap and repeat for n-1 rounds\n    for (i in nums.size - 1 downTo 1) {\n        // Delete node\n        val temp = nums[0]\n        nums[0] = nums[i]\n        nums[i] = temp\n        // Start heapifying the root node, from top to bottom\n        siftDown(nums, i, 0)\n    }\n}\n
      heap_sort.rb
      ### Heap length is n, heapify from node i, top to bottom ###\ndef sift_down(nums, n, i)\n  while true\n    # If node i is largest or indices l, r are out of bounds, no need to continue heapify, break\n    l = 2 * i + 1\n    r = 2 * i + 2\n    ma = i\n    ma = l if l < n && nums[l] > nums[ma]\n    ma = r if r < n && nums[r] > nums[ma]\n    # Swap two nodes\n    break if ma == i\n    # Swap two nodes\n    nums[i], nums[ma] = nums[ma], nums[i]\n    # Loop downwards heapification\n    i = ma\n  end\nend\n\n### Heap sort ###\ndef heap_sort(nums)\n  # Build heap operation: heapify all nodes except leaves\n  (nums.length / 2 - 1).downto(0) do |i|\n    sift_down(nums, nums.length, i)\n  end\n  # Extract the largest element from the heap and repeat for n-1 rounds\n  (nums.length - 1).downto(1) do |i|\n    # Delete node\n    nums[0], nums[i] = nums[i], nums[0]\n    # Start heapifying the root node, from top to bottom\n    sift_down(nums, i, 0)\n  end\nend\n
      "},{"location":"chapter_sorting/heap_sort/#1172-algorithm-characteristics","title":"11.7.2 \u00a0 Algorithm Characteristics","text":"
      • Time complexity of \\(O(n \\log n)\\), non-adaptive sorting: The build heap operation uses \\(O(n)\\) time. Extracting the largest element from the heap has a time complexity of \\(O(\\log n)\\), looping a total of \\(n - 1\\) rounds.
      • Space complexity of \\(O(1)\\), in-place sorting: A few pointer variables use \\(O(1)\\) space. Element swapping and heapify operations are both performed on the original array.
      • Non-stable sorting: When swapping the heap top element and heap bottom element, the relative positions of equal elements may change.
      "},{"location":"chapter_sorting/insertion_sort/","title":"11.4 \u00a0 Insertion Sort","text":"

      Insertion sort (insertion sort) is a simple sorting algorithm that works very similarly to the process of manually organizing a deck of cards.

      Specifically, we select a base element from the unsorted interval, compare the element with elements in the sorted interval to its left one by one, and insert the element into the correct position.

      Figure 11-6 shows the operation flow of inserting an element into the array. Let the base element be base. We need to move all elements from the target index to base one position to the right, and then assign base to the target index.

      Figure 11-6 \u00a0 Single insertion operation

      "},{"location":"chapter_sorting/insertion_sort/#1141-algorithm-flow","title":"11.4.1 \u00a0 Algorithm Flow","text":"

      The overall flow of insertion sort is shown in Figure 11-7.

      1. Initially, the first element of the array has completed sorting.
      2. Select the second element of the array as base, and after inserting it into the correct position, the first 2 elements of the array are sorted.
      3. Select the third element as base, and after inserting it into the correct position, the first 3 elements of the array are sorted.
      4. And so on. In the last round, select the last element as base, and after inserting it into the correct position, all elements are sorted.

      Figure 11-7 \u00a0 Insertion sort flow

      Example code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby insertion_sort.py
      def insertion_sort(nums: list[int]):\n    \"\"\"Insertion sort\"\"\"\n    # Outer loop: sorted interval is [0, i-1]\n    for i in range(1, len(nums)):\n        base = nums[i]\n        j = i - 1\n        # Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n        while j >= 0 and nums[j] > base:\n            nums[j + 1] = nums[j]  # Move nums[j] to the right by one position\n            j -= 1\n        nums[j + 1] = base  # Assign base to the correct position\n
      insertion_sort.cpp
      /* Insertion sort */\nvoid insertionSort(vector<int> &nums) {\n    // Outer loop: sorted interval is [0, i-1]\n    for (int i = 1; i < nums.size(); i++) {\n        int base = nums[i], j = i - 1;\n        // Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // Move nums[j] to the right by one position\n            j--;\n        }\n        nums[j + 1] = base; // Assign base to the correct position\n    }\n}\n
      insertion_sort.java
      /* Insertion sort */\nvoid insertionSort(int[] nums) {\n    // Outer loop: sorted interval is [0, i-1]\n    for (int i = 1; i < nums.length; i++) {\n        int base = nums[i], j = i - 1;\n        // Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // Move nums[j] to the right by one position\n            j--;\n        }\n        nums[j + 1] = base;        // Assign base to the correct position\n    }\n}\n
      insertion_sort.cs
      /* Insertion sort */\nvoid InsertionSort(int[] nums) {\n    // Outer loop: sorted interval is [0, i-1]\n    for (int i = 1; i < nums.Length; i++) {\n        int bas = nums[i], j = i - 1;\n        // Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n        while (j >= 0 && nums[j] > bas) {\n            nums[j + 1] = nums[j]; // Move nums[j] to the right by one position\n            j--;\n        }\n        nums[j + 1] = bas;         // Assign base to the correct position\n    }\n}\n
      insertion_sort.go
      /* Insertion sort */\nfunc insertionSort(nums []int) {\n    // Outer loop: sorted interval is [0, i-1]\n    for i := 1; i < len(nums); i++ {\n        base := nums[i]\n        j := i - 1\n        // Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n        for j >= 0 && nums[j] > base {\n            nums[j+1] = nums[j] // Move nums[j] to the right by one position\n            j--\n        }\n        nums[j+1] = base // Assign base to the correct position\n    }\n}\n
      insertion_sort.swift
      /* Insertion sort */\nfunc insertionSort(nums: inout [Int]) {\n    // Outer loop: sorted interval is [0, i-1]\n    for i in nums.indices.dropFirst() {\n        let base = nums[i]\n        var j = i - 1\n        // Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n        while j >= 0, nums[j] > base {\n            nums[j + 1] = nums[j] // Move nums[j] to the right by one position\n            j -= 1\n        }\n        nums[j + 1] = base // Assign base to the correct position\n    }\n}\n
      insertion_sort.js
      /* Insertion sort */\nfunction insertionSort(nums) {\n    // Outer loop: sorted interval is [0, i-1]\n    for (let i = 1; i < nums.length; i++) {\n        let base = nums[i],\n            j = i - 1;\n        // Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // Move nums[j] to the right by one position\n            j--;\n        }\n        nums[j + 1] = base; // Assign base to the correct position\n    }\n}\n
      insertion_sort.ts
      /* Insertion sort */\nfunction insertionSort(nums: number[]): void {\n    // Outer loop: sorted interval is [0, i-1]\n    for (let i = 1; i < nums.length; i++) {\n        const base = nums[i];\n        let j = i - 1;\n        // Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // Move nums[j] to the right by one position\n            j--;\n        }\n        nums[j + 1] = base; // Assign base to the correct position\n    }\n}\n
      insertion_sort.dart
      /* Insertion sort */\nvoid insertionSort(List<int> nums) {\n  // Outer loop: sorted interval is [0, i-1]\n  for (int i = 1; i < nums.length; i++) {\n    int base = nums[i], j = i - 1;\n    // Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n    while (j >= 0 && nums[j] > base) {\n      nums[j + 1] = nums[j]; // Move nums[j] to the right by one position\n      j--;\n    }\n    nums[j + 1] = base; // Assign base to the correct position\n  }\n}\n
      insertion_sort.rs
      /* Insertion sort */\nfn insertion_sort(nums: &mut [i32]) {\n    // Outer loop: sorted interval is [0, i-1]\n    for i in 1..nums.len() {\n        let (base, mut j) = (nums[i], (i - 1) as i32);\n        // Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n        while j >= 0 && nums[j as usize] > base {\n            nums[(j + 1) as usize] = nums[j as usize]; // Move nums[j] to the right by one position\n            j -= 1;\n        }\n        nums[(j + 1) as usize] = base; // Assign base to the correct position\n    }\n}\n
      insertion_sort.c
      /* Insertion sort */\nvoid insertionSort(int nums[], int size) {\n    // Outer loop: sorted interval is [0, i-1]\n    for (int i = 1; i < size; i++) {\n        int base = nums[i], j = i - 1;\n        // Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n        while (j >= 0 && nums[j] > base) {\n            // Move nums[j] to the right by one position\n            nums[j + 1] = nums[j];\n            j--;\n        }\n        // Assign base to the correct position\n        nums[j + 1] = base;\n    }\n}\n
      insertion_sort.kt
      /* Insertion sort */\nfun insertionSort(nums: IntArray) {\n    // Outer loop: sorted elements are 1, 2, ..., n\n    for (i in nums.indices) {\n        val base = nums[i]\n        var j = i - 1\n        // Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j] // Move nums[j] to the right by one position\n            j--\n        }\n        nums[j + 1] = base        // Assign base to the correct position\n    }\n}\n
      insertion_sort.rb
      ### Insertion sort ###\ndef insertion_sort(nums)\n  n = nums.length\n  # Outer loop: sorted interval is [0, i-1]\n  for i in 1...n\n    base = nums[i]\n    j = i - 1\n    # Inner loop: insert base into the correct position within the sorted interval [0, i-1]\n    while j >= 0 && nums[j] > base\n      nums[j + 1] = nums[j] # Move nums[j] to the right by one position\n      j -= 1\n    end\n    nums[j + 1] = base # Assign base to the correct position\n  end\nend\n
      "},{"location":"chapter_sorting/insertion_sort/#1142-algorithm-characteristics","title":"11.4.2 \u00a0 Algorithm Characteristics","text":"
      • Time complexity of \\(O(n^2)\\), adaptive sorting: In the worst case, each insertion operation requires loops of \\(n - 1\\), \\(n-2\\), \\(\\dots\\), \\(2\\), \\(1\\), summing to \\((n - 1) n / 2\\), so the time complexity is \\(O(n^2)\\). When encountering ordered data, the insertion operation will terminate early. When the input array is completely ordered, insertion sort achieves the best-case time complexity of \\(O(n)\\).
      • Space complexity of \\(O(1)\\), in-place sorting: Pointers \\(i\\) and \\(j\\) use a constant amount of extra space.
      • Stable sorting: During the insertion operation process, we insert elements to the right of equal elements, without changing their order.
      "},{"location":"chapter_sorting/insertion_sort/#1143-advantages-of-insertion-sort","title":"11.4.3 \u00a0 Advantages of Insertion Sort","text":"

      The time complexity of insertion sort is \\(O(n^2)\\), while the time complexity of quick sort, which we will learn about next, is \\(O(n \\log n)\\). Although insertion sort has a higher time complexity, insertion sort is usually faster for smaller data volumes.

      This conclusion is similar to the applicable situations of linear search and binary search. Algorithms like quick sort with \\(O(n \\log n)\\) complexity are sorting algorithms based on divide-and-conquer strategy and often contain more unit computation operations. When the data volume is small, \\(n^2\\) and \\(n \\log n\\) are numerically close, and complexity does not dominate; the number of unit operations per round plays a decisive role.

      In fact, the built-in sorting functions in many programming languages (such as Java) adopt insertion sort. The general approach is: for long arrays, use sorting algorithms based on divide-and-conquer strategy, such as quick sort; for short arrays, directly use insertion sort.

      Although bubble sort, selection sort, and insertion sort all have a time complexity of \\(O(n^2)\\), in actual situations, insertion sort is used significantly more frequently than bubble sort and selection sort, mainly for the following reasons.

      • Bubble sort is based on element swapping, requiring the use of a temporary variable, involving 3 unit operations; insertion sort is based on element assignment, requiring only 1 unit operation. Therefore, the computational overhead of bubble sort is usually higher than that of insertion sort.
      • Selection sort has a time complexity of \\(O(n^2)\\) in any case. If given a set of partially ordered data, insertion sort is usually more efficient than selection sort.
      • Selection sort is unstable and cannot be applied to multi-level sorting.
      "},{"location":"chapter_sorting/merge_sort/","title":"11.6 \u00a0 Merge Sort","text":"

      Merge sort (merge sort) is a sorting algorithm based on the divide-and-conquer strategy, which includes the \"divide\" and \"merge\" phases shown in Figure 11-10.

      1. Divide phase: Recursively split the array from the midpoint, transforming the sorting problem of a long array into the sorting problems of shorter arrays.
      2. Merge phase: When the sub-array length is 1, terminate the division and start merging, continuously merging two shorter sorted arrays into one longer sorted array until the process is complete.

      Figure 11-10 \u00a0 Divide and merge phases of merge sort

      "},{"location":"chapter_sorting/merge_sort/#1161-algorithm-flow","title":"11.6.1 \u00a0 Algorithm Flow","text":"

      As shown in Figure 11-11, the \"divide phase\" recursively splits the array from the midpoint into two sub-arrays from top to bottom.

      1. Calculate the array midpoint mid, recursively divide the left sub-array (interval [left, mid]) and right sub-array (interval [mid + 1, right]).
      2. Recursively execute step 1. until the sub-array interval length is 1, then terminate.

      The \"merge phase\" merges the left sub-array and right sub-array into a sorted array from bottom to top. Note that merging starts from sub-arrays of length 1, and each sub-array in the merge phase is sorted.

      <1><2><3><4><5><6><7><8><9><10>

      Figure 11-11 \u00a0 Merge sort steps

      It can be observed that the recursive order of merge sort is consistent with the post-order traversal of a binary tree.

      • Post-order traversal: First recursively traverse the left subtree, then recursively traverse the right subtree, and finally process the root node.
      • Merge sort: First recursively process the left sub-array, then recursively process the right sub-array, and finally perform the merge.

      The implementation of merge sort is shown in the code below. Note that the interval to be merged in nums is [left, right], while the corresponding interval in tmp is [0, right - left].

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby merge_sort.py
      def merge(nums: list[int], left: int, mid: int, right: int):\n    \"\"\"Merge left subarray and right subarray\"\"\"\n    # Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n    # Create a temporary array tmp to store the merged results\n    tmp = [0] * (right - left + 1)\n    # Initialize the start indices of the left and right subarrays\n    i, j, k = left, mid + 1, 0\n    # While both subarrays still have elements, compare and copy the smaller element into the temporary array\n    while i <= mid and j <= right:\n        if nums[i] <= nums[j]:\n            tmp[k] = nums[i]\n            i += 1\n        else:\n            tmp[k] = nums[j]\n            j += 1\n        k += 1\n    # Copy the remaining elements of the left and right subarrays into the temporary array\n    while i <= mid:\n        tmp[k] = nums[i]\n        i += 1\n        k += 1\n    while j <= right:\n        tmp[k] = nums[j]\n        j += 1\n        k += 1\n    # Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n    for k in range(0, len(tmp)):\n        nums[left + k] = tmp[k]\n\ndef merge_sort(nums: list[int], left: int, right: int):\n    \"\"\"Merge sort\"\"\"\n    # Termination condition\n    if left >= right:\n        return  # Terminate recursion when subarray length is 1\n    # Divide and conquer stage\n    mid = (left + right) // 2  # Calculate midpoint\n    merge_sort(nums, left, mid)  # Recursively process the left subarray\n    merge_sort(nums, mid + 1, right)  # Recursively process the right subarray\n    # Merge stage\n    merge(nums, left, mid, right)\n
      merge_sort.cpp
      /* Merge left subarray and right subarray */\nvoid merge(vector<int> &nums, int left, int mid, int right) {\n    // Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n    // Create a temporary array tmp to store the merged results\n    vector<int> tmp(right - left + 1);\n    // Initialize the start indices of the left and right subarrays\n    int i = left, j = mid + 1, k = 0;\n    // While both subarrays still have elements, compare and copy the smaller element into the temporary array\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++];\n        else\n            tmp[k++] = nums[j++];\n    }\n    // Copy the remaining elements of the left and right subarrays into the temporary array\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n    for (k = 0; k < tmp.size(); k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* Merge sort */\nvoid mergeSort(vector<int> &nums, int left, int right) {\n    // Termination condition\n    if (left >= right)\n        return; // Terminate recursion when subarray length is 1\n    // Divide and conquer stage\n    int mid = left + (right - left) / 2;    // Calculate midpoint\n    mergeSort(nums, left, mid);      // Recursively process the left subarray\n    mergeSort(nums, mid + 1, right); // Recursively process the right subarray\n    // Merge stage\n    merge(nums, left, mid, right);\n}\n
      merge_sort.java
      /* Merge left subarray and right subarray */\nvoid merge(int[] nums, int left, int mid, int right) {\n    // Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n    // Create a temporary array tmp to store the merged results\n    int[] tmp = new int[right - left + 1];\n    // Initialize the start indices of the left and right subarrays\n    int i = left, j = mid + 1, k = 0;\n    // While both subarrays still have elements, compare and copy the smaller element into the temporary array\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++];\n        else\n            tmp[k++] = nums[j++];\n    }\n    // Copy the remaining elements of the left and right subarrays into the temporary array\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n    for (k = 0; k < tmp.length; k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* Merge sort */\nvoid mergeSort(int[] nums, int left, int right) {\n    // Termination condition\n    if (left >= right)\n        return; // Terminate recursion when subarray length is 1\n    // Divide and conquer stage\n    int mid = left + (right - left) / 2; // Calculate midpoint\n    mergeSort(nums, left, mid); // Recursively process the left subarray\n    mergeSort(nums, mid + 1, right); // Recursively process the right subarray\n    // Merge stage\n    merge(nums, left, mid, right);\n}\n
      merge_sort.cs
      /* Merge left subarray and right subarray */\nvoid Merge(int[] nums, int left, int mid, int right) {\n    // Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n    // Create a temporary array tmp to store the merged results\n    int[] tmp = new int[right - left + 1];\n    // Initialize the start indices of the left and right subarrays\n    int i = left, j = mid + 1, k = 0;\n    // While both subarrays still have elements, compare and copy the smaller element into the temporary array\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++];\n        else\n            tmp[k++] = nums[j++];\n    }\n    // Copy the remaining elements of the left and right subarrays into the temporary array\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n    for (k = 0; k < tmp.Length; ++k) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* Merge sort */\nvoid MergeSort(int[] nums, int left, int right) {\n    // Termination condition\n    if (left >= right) return;       // Terminate recursion when subarray length is 1\n    // Divide and conquer stage\n    int mid = left + (right - left) / 2;    // Calculate midpoint\n    MergeSort(nums, left, mid);      // Recursively process the left subarray\n    MergeSort(nums, mid + 1, right); // Recursively process the right subarray\n    // Merge stage\n    Merge(nums, left, mid, right);\n}\n
      merge_sort.go
      /* Merge left subarray and right subarray */\nfunc merge(nums []int, left, mid, right int) {\n    // Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n    // Create a temporary array tmp to store the merged results\n    tmp := make([]int, right-left+1)\n    // Initialize the start indices of the left and right subarrays\n    i, j, k := left, mid+1, 0\n    // While both subarrays still have elements, compare and copy the smaller element into the temporary array\n    for i <= mid && j <= right {\n        if nums[i] <= nums[j] {\n            tmp[k] = nums[i]\n            i++\n        } else {\n            tmp[k] = nums[j]\n            j++\n        }\n        k++\n    }\n    // Copy the remaining elements of the left and right subarrays into the temporary array\n    for i <= mid {\n        tmp[k] = nums[i]\n        i++\n        k++\n    }\n    for j <= right {\n        tmp[k] = nums[j]\n        j++\n        k++\n    }\n    // Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n    for k := 0; k < len(tmp); k++ {\n        nums[left+k] = tmp[k]\n    }\n}\n\n/* Merge sort */\nfunc mergeSort(nums []int, left, right int) {\n    // Termination condition\n    if left >= right {\n        return\n    }\n    // Divide and conquer stage\n    mid := left + (right - left) / 2\n    mergeSort(nums, left, mid)\n    mergeSort(nums, mid+1, right)\n    // Merge stage\n    merge(nums, left, mid, right)\n}\n
      merge_sort.swift
      /* Merge left subarray and right subarray */\nfunc merge(nums: inout [Int], left: Int, mid: Int, right: Int) {\n    // Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n    // Create a temporary array tmp to store the merged results\n    var tmp = Array(repeating: 0, count: right - left + 1)\n    // Initialize the start indices of the left and right subarrays\n    var i = left, j = mid + 1, k = 0\n    // While both subarrays still have elements, compare and copy the smaller element into the temporary array\n    while i <= mid, j <= right {\n        if nums[i] <= nums[j] {\n            tmp[k] = nums[i]\n            i += 1\n        } else {\n            tmp[k] = nums[j]\n            j += 1\n        }\n        k += 1\n    }\n    // Copy the remaining elements of the left and right subarrays into the temporary array\n    while i <= mid {\n        tmp[k] = nums[i]\n        i += 1\n        k += 1\n    }\n    while j <= right {\n        tmp[k] = nums[j]\n        j += 1\n        k += 1\n    }\n    // Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n    for k in tmp.indices {\n        nums[left + k] = tmp[k]\n    }\n}\n\n/* Merge sort */\nfunc mergeSort(nums: inout [Int], left: Int, right: Int) {\n    // Termination condition\n    if left >= right { // Terminate recursion when subarray length is 1\n        return\n    }\n    // Divide and conquer stage\n    let mid = left + (right - left) / 2 // Calculate midpoint\n    mergeSort(nums: &nums, left: left, right: mid) // Recursively process the left subarray\n    mergeSort(nums: &nums, left: mid + 1, right: right) // Recursively process the right subarray\n    // Merge stage\n    merge(nums: &nums, left: left, mid: mid, right: right)\n}\n
      merge_sort.js
      /* Merge left subarray and right subarray */\nfunction merge(nums, left, mid, right) {\n    // Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n    // Create a temporary array tmp to store the merged results\n    const tmp = new Array(right - left + 1);\n    // Initialize the start indices of the left and right subarrays\n    let i = left,\n        j = mid + 1,\n        k = 0;\n    // While both subarrays still have elements, compare and copy the smaller element into the temporary array\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j]) {\n            tmp[k++] = nums[i++];\n        } else {\n            tmp[k++] = nums[j++];\n        }\n    }\n    // Copy the remaining elements of the left and right subarrays into the temporary array\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n    for (k = 0; k < tmp.length; k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* Merge sort */\nfunction mergeSort(nums, left, right) {\n    // Termination condition\n    if (left >= right) return; // Terminate recursion when subarray length is 1\n    // Divide and conquer stage\n    let mid = Math.floor(left + (right - left) / 2); // Calculate midpoint\n    mergeSort(nums, left, mid); // Recursively process the left subarray\n    mergeSort(nums, mid + 1, right); // Recursively process the right subarray\n    // Merge stage\n    merge(nums, left, mid, right);\n}\n
      merge_sort.ts
      /* Merge left subarray and right subarray */\nfunction merge(nums: number[], left: number, mid: number, right: number): void {\n    // Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n    // Create a temporary array tmp to store the merged results\n    const tmp = new Array(right - left + 1);\n    // Initialize the start indices of the left and right subarrays\n    let i = left,\n        j = mid + 1,\n        k = 0;\n    // While both subarrays still have elements, compare and copy the smaller element into the temporary array\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j]) {\n            tmp[k++] = nums[i++];\n        } else {\n            tmp[k++] = nums[j++];\n        }\n    }\n    // Copy the remaining elements of the left and right subarrays into the temporary array\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n    for (k = 0; k < tmp.length; k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* Merge sort */\nfunction mergeSort(nums: number[], left: number, right: number): void {\n    // Termination condition\n    if (left >= right) return; // Terminate recursion when subarray length is 1\n    // Divide and conquer stage\n    let mid = Math.floor(left + (right - left) / 2); // Calculate midpoint\n    mergeSort(nums, left, mid); // Recursively process the left subarray\n    mergeSort(nums, mid + 1, right); // Recursively process the right subarray\n    // Merge stage\n    merge(nums, left, mid, right);\n}\n
      merge_sort.dart
      /* Merge left subarray and right subarray */\nvoid merge(List<int> nums, int left, int mid, int right) {\n  // Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n  // Create a temporary array tmp to store the merged results\n  List<int> tmp = List.filled(right - left + 1, 0);\n  // Initialize the start indices of the left and right subarrays\n  int i = left, j = mid + 1, k = 0;\n  // While both subarrays still have elements, compare and copy the smaller element into the temporary array\n  while (i <= mid && j <= right) {\n    if (nums[i] <= nums[j])\n      tmp[k++] = nums[i++];\n    else\n      tmp[k++] = nums[j++];\n  }\n  // Copy the remaining elements of the left and right subarrays into the temporary array\n  while (i <= mid) {\n    tmp[k++] = nums[i++];\n  }\n  while (j <= right) {\n    tmp[k++] = nums[j++];\n  }\n  // Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n  for (k = 0; k < tmp.length; k++) {\n    nums[left + k] = tmp[k];\n  }\n}\n\n/* Merge sort */\nvoid mergeSort(List<int> nums, int left, int right) {\n  // Termination condition\n  if (left >= right) return; // Terminate recursion when subarray length is 1\n  // Divide and conquer stage\n  int mid = left + (right - left) ~/ 2; // Calculate midpoint\n  mergeSort(nums, left, mid); // Recursively process the left subarray\n  mergeSort(nums, mid + 1, right); // Recursively process the right subarray\n  // Merge stage\n  merge(nums, left, mid, right);\n}\n
      merge_sort.rs
      /* Merge left subarray and right subarray */\nfn merge(nums: &mut [i32], left: usize, mid: usize, right: usize) {\n    // Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n    // Create a temporary array tmp to store the merged results\n    let tmp_size = right - left + 1;\n    let mut tmp = vec![0; tmp_size];\n    // Initialize the start indices of the left and right subarrays\n    let (mut i, mut j, mut k) = (left, mid + 1, 0);\n    // While both subarrays still have elements, compare and copy the smaller element into the temporary array\n    while i <= mid && j <= right {\n        if nums[i] <= nums[j] {\n            tmp[k] = nums[i];\n            i += 1;\n        } else {\n            tmp[k] = nums[j];\n            j += 1;\n        }\n        k += 1;\n    }\n    // Copy the remaining elements of the left and right subarrays into the temporary array\n    while i <= mid {\n        tmp[k] = nums[i];\n        k += 1;\n        i += 1;\n    }\n    while j <= right {\n        tmp[k] = nums[j];\n        k += 1;\n        j += 1;\n    }\n    // Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n    for k in 0..tmp_size {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* Merge sort */\nfn merge_sort(nums: &mut [i32], left: usize, right: usize) {\n    // Termination condition\n    if left >= right {\n        return; // Terminate recursion when subarray length is 1\n    }\n\n    // Divide and conquer stage\n    let mid = left + (right - left) / 2; // Calculate midpoint\n    merge_sort(nums, left, mid); // Recursively process the left subarray\n    merge_sort(nums, mid + 1, right); // Recursively process the right subarray\n\n    // Merge stage\n    merge(nums, left, mid, right);\n}\n
      merge_sort.c
      /* Merge left subarray and right subarray */\nvoid merge(int *nums, int left, int mid, int right) {\n    // Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n    // Create a temporary array tmp to store the merged results\n    int tmpSize = right - left + 1;\n    int *tmp = (int *)malloc(tmpSize * sizeof(int));\n    // Initialize the start indices of the left and right subarrays\n    int i = left, j = mid + 1, k = 0;\n    // While both subarrays still have elements, compare and copy the smaller element into the temporary array\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j]) {\n            tmp[k++] = nums[i++];\n        } else {\n            tmp[k++] = nums[j++];\n        }\n    }\n    // Copy the remaining elements of the left and right subarrays into the temporary array\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n    for (k = 0; k < tmpSize; ++k) {\n        nums[left + k] = tmp[k];\n    }\n    // Free memory\n    free(tmp);\n}\n\n/* Merge sort */\nvoid mergeSort(int *nums, int left, int right) {\n    // Termination condition\n    if (left >= right)\n        return; // Terminate recursion when subarray length is 1\n    // Divide and conquer stage\n    int mid = left + (right - left) / 2;    // Calculate midpoint\n    mergeSort(nums, left, mid);      // Recursively process the left subarray\n    mergeSort(nums, mid + 1, right); // Recursively process the right subarray\n    // Merge stage\n    merge(nums, left, mid, right);\n}\n
      merge_sort.kt
      /* Merge left subarray and right subarray */\nfun merge(nums: IntArray, left: Int, mid: Int, right: Int) {\n    // Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n    // Create a temporary array tmp to store the merged results\n    val tmp = IntArray(right - left + 1)\n    // Initialize the start indices of the left and right subarrays\n    var i = left\n    var j = mid + 1\n    var k = 0\n    // While both subarrays still have elements, compare and copy the smaller element into the temporary array\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++]\n        else\n            tmp[k++] = nums[j++]\n    }\n    // Copy the remaining elements of the left and right subarrays into the temporary array\n    while (i <= mid) {\n        tmp[k++] = nums[i++]\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++]\n    }\n    // Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n    for (l in tmp.indices) {\n        nums[left + l] = tmp[l]\n    }\n}\n\n/* Merge sort */\nfun mergeSort(nums: IntArray, left: Int, right: Int) {\n    // Termination condition\n    if (left >= right) return  // Terminate recursion when subarray length is 1\n    // Divide and conquer stage\n    val mid = left + (right - left) / 2 // Calculate midpoint\n    mergeSort(nums, left, mid) // Recursively process the left subarray\n    mergeSort(nums, mid + 1, right) // Recursively process the right subarray\n    // Merge stage\n    merge(nums, left, mid, right)\n}\n
      merge_sort.rb
      ### Merge left and right subarrays ###\ndef merge(nums, left, mid, right)\n  # Left subarray interval is [left, mid], right subarray interval is [mid+1, right]\n  # Create temporary array tmp to store merged result\n  tmp = Array.new(right - left + 1, 0)\n  # Initialize the start indices of the left and right subarrays\n  i, j, k = left, mid + 1, 0\n  # While both subarrays still have elements, compare and copy the smaller element into the temporary array\n  while i <= mid && j <= right\n    if nums[i] <= nums[j]\n      tmp[k] = nums[i]\n      i += 1\n    else\n      tmp[k] = nums[j]\n      j += 1\n    end\n    k += 1\n  end\n  # Copy the remaining elements of the left and right subarrays into the temporary array\n  while i <= mid\n    tmp[k] = nums[i]\n    i += 1\n    k += 1\n  end\n  while j <= right\n    tmp[k] = nums[j]\n    j += 1\n    k += 1\n  end\n  # Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval\n  (0...tmp.length).each do |k|\n    nums[left + k] = tmp[k]\n  end\nend\n\n### Merge sort ###\ndef merge_sort(nums, left, right)\n  # Termination condition\n  # Terminate recursion when subarray length is 1\n  return if left >= right\n  # Divide and conquer stage\n  mid = left + (right - left) / 2 # Calculate midpoint\n  merge_sort(nums, left, mid) # Recursively process the left subarray\n  merge_sort(nums, mid + 1, right) # Recursively process the right subarray\n  # Merge stage\n  merge(nums, left, mid, right)\nend\n
      "},{"location":"chapter_sorting/merge_sort/#1162-algorithm-characteristics","title":"11.6.2 \u00a0 Algorithm Characteristics","text":"
      • Time complexity of \\(O(n \\log n)\\), non-adaptive sorting: The division produces a recursion tree of height \\(\\log n\\), and the total number of merge operations at each level is \\(n\\), so the overall time complexity is \\(O(n \\log n)\\).
      • Space complexity of \\(O(n)\\), non-in-place sorting: The recursion depth is \\(\\log n\\), using \\(O(\\log n)\\) size of stack frame space. The merge operation requires the aid of an auxiliary array, using \\(O(n)\\) size of additional space.
      • Stable sorting: In the merge process, the order of equal elements remains unchanged.
      "},{"location":"chapter_sorting/merge_sort/#1163-linked-list-sorting","title":"11.6.3 \u00a0 Linked List Sorting","text":"

      For linked lists, merge sort has significant advantages over other sorting algorithms, and can optimize the space complexity of linked list sorting tasks to \\(O(1)\\).

      • Divide phase: \"Iteration\" can be used instead of \"recursion\" to implement linked list division work, thus saving the stack frame space used by recursion.
      • Merge phase: In linked lists, node insertion and deletion operations can be achieved by just changing references (pointers), so there is no need to create additional linked lists during the merge phase (merging two short ordered linked lists into one long ordered linked list).

      The specific implementation details are quite complex, and interested readers can consult related materials for learning.

      "},{"location":"chapter_sorting/quick_sort/","title":"11.5 \u00a0 Quick Sort","text":"

      Quick sort (quick sort) is a sorting algorithm based on the divide-and-conquer strategy, which operates efficiently and is widely applied.

      The core operation of quick sort is \"sentinel partitioning\", which aims to: select a certain element in the array as the \"pivot\", move all elements smaller than the pivot to its left, and move elements larger than the pivot to its right. Specifically, the flow of sentinel partitioning is shown in Figure 11-8.

      1. Select the leftmost element of the array as the pivot, and initialize two pointers i and j pointing to the two ends of the array.
      2. Set up a loop in which i (j) is used in each round to find the first element larger (smaller) than the pivot, and then swap these two elements.
      3. Loop through step 2. until i and j meet, and finally swap the pivot to the boundary line of the two sub-arrays.
      <1><2><3><4><5><6><7><8><9>

      Figure 11-8 \u00a0 Sentinel partitioning steps

      After sentinel partitioning is complete, the original array is divided into three parts: left sub-array, pivot, right sub-array, satisfying \"any element in left sub-array \\(\\leq\\) pivot \\(\\leq\\) any element in right sub-array\". Therefore, we next only need to sort these two sub-arrays.

      Divide-and-conquer strategy of quick sort

      The essence of sentinel partitioning is to simplify the sorting problem of a longer array into the sorting problems of two shorter arrays.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
      def partition(self, nums: list[int], left: int, right: int) -> int:\n    \"\"\"Sentinel partition\"\"\"\n    # Use nums[left] as the pivot\n    i, j = left, right\n    while i < j:\n        while i < j and nums[j] >= nums[left]:\n            j -= 1  # Search from right to left for the first element smaller than the pivot\n        while i < j and nums[i] <= nums[left]:\n            i += 1  # Search from left to right for the first element greater than the pivot\n        # Swap elements\n        nums[i], nums[j] = nums[j], nums[i]\n    # Swap the pivot to the boundary between the two subarrays\n    nums[i], nums[left] = nums[left], nums[i]\n    return i  # Return the index of the pivot\n
      quick_sort.cpp
      /* Sentinel partition */\nint partition(vector<int> &nums, int left, int right) {\n    // Use nums[left] as the pivot\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;                // Search from right to left for the first element smaller than the pivot\n        while (i < j && nums[i] <= nums[left])\n            i++;                // Search from left to right for the first element greater than the pivot\n        swap(nums[i], nums[j]); // Swap these two elements\n    }\n    swap(nums[i], nums[left]);  // Swap the pivot to the boundary between the two subarrays\n    return i;                   // Return the index of the pivot\n}\n
      quick_sort.java
      /* Swap elements */\nvoid swap(int[] nums, int i, int j) {\n    int tmp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = tmp;\n}\n\n/* Sentinel partition */\nint partition(int[] nums, int left, int right) {\n    // Use nums[left] as the pivot\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // Search from right to left for the first element smaller than the pivot\n        while (i < j && nums[i] <= nums[left])\n            i++;          // Search from left to right for the first element greater than the pivot\n        swap(nums, i, j); // Swap these two elements\n    }\n    swap(nums, i, left);  // Swap the pivot to the boundary between the two subarrays\n    return i;             // Return the index of the pivot\n}\n
      quick_sort.cs
      /* Swap elements */\nvoid Swap(int[] nums, int i, int j) {\n    (nums[j], nums[i]) = (nums[i], nums[j]);\n}\n\n/* Sentinel partition */\nint Partition(int[] nums, int left, int right) {\n    // Use nums[left] as the pivot\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // Search from right to left for the first element smaller than the pivot\n        while (i < j && nums[i] <= nums[left])\n            i++;          // Search from left to right for the first element greater than the pivot\n        Swap(nums, i, j); // Swap these two elements\n    }\n    Swap(nums, i, left);  // Swap the pivot to the boundary between the two subarrays\n    return i;             // Return the index of the pivot\n}\n
      quick_sort.go
      /* Sentinel partition */\nfunc (q *quickSort) partition(nums []int, left, right int) int {\n    // Use nums[left] as the pivot\n    i, j := left, right\n    for i < j {\n        for i < j && nums[j] >= nums[left] {\n            j-- // Search from right to left for the first element smaller than the pivot\n        }\n        for i < j && nums[i] <= nums[left] {\n            i++ // Search from left to right for the first element greater than the pivot\n        }\n        // Swap elements\n        nums[i], nums[j] = nums[j], nums[i]\n    }\n    // Swap the pivot to the boundary between the two subarrays\n    nums[i], nums[left] = nums[left], nums[i]\n    return i // Return the index of the pivot\n}\n
      quick_sort.swift
      /* Sentinel partition */\nfunc partition(nums: inout [Int], left: Int, right: Int) -> Int {\n    // Use nums[left] as the pivot\n    var i = left\n    var j = right\n    while i < j {\n        while i < j, nums[j] >= nums[left] {\n            j -= 1 // Search from right to left for the first element smaller than the pivot\n        }\n        while i < j, nums[i] <= nums[left] {\n            i += 1 // Search from left to right for the first element greater than the pivot\n        }\n        nums.swapAt(i, j) // Swap these two elements\n    }\n    nums.swapAt(i, left) // Swap the pivot to the boundary between the two subarrays\n    return i // Return the index of the pivot\n}\n
      quick_sort.js
      /* Swap elements */\nswap(nums, i, j) {\n    let tmp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = tmp;\n}\n\n/* Sentinel partition */\npartition(nums, left, right) {\n    // Use nums[left] as the pivot\n    let i = left,\n        j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) {\n            j -= 1; // Search from right to left for the first element smaller than the pivot\n        }\n        while (i < j && nums[i] <= nums[left]) {\n            i += 1; // Search from left to right for the first element greater than the pivot\n        }\n        // Swap elements\n        this.swap(nums, i, j); // Swap these two elements\n    }\n    this.swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays\n    return i; // Return the index of the pivot\n}\n
      quick_sort.ts
      /* Swap elements */\nswap(nums: number[], i: number, j: number): void {\n    let tmp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = tmp;\n}\n\n/* Sentinel partition */\npartition(nums: number[], left: number, right: number): number {\n    // Use nums[left] as the pivot\n    let i = left,\n        j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) {\n            j -= 1; // Search from right to left for the first element smaller than the pivot\n        }\n        while (i < j && nums[i] <= nums[left]) {\n            i += 1; // Search from left to right for the first element greater than the pivot\n        }\n        // Swap elements\n        this.swap(nums, i, j); // Swap these two elements\n    }\n    this.swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays\n    return i; // Return the index of the pivot\n}\n
      quick_sort.dart
      /* Swap elements */\nvoid _swap(List<int> nums, int i, int j) {\n  int tmp = nums[i];\n  nums[i] = nums[j];\n  nums[j] = tmp;\n}\n\n/* Sentinel partition */\nint _partition(List<int> nums, int left, int right) {\n  // Use nums[left] as the pivot\n  int i = left, j = right;\n  while (i < j) {\n    while (i < j && nums[j] >= nums[left]) j--; // Search from right to left for the first element smaller than the pivot\n    while (i < j && nums[i] <= nums[left]) i++; // Search from left to right for the first element greater than the pivot\n    _swap(nums, i, j); // Swap these two elements\n  }\n  _swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays\n  return i; // Return the index of the pivot\n}\n
      quick_sort.rs
      /* Sentinel partition */\nfn partition(nums: &mut [i32], left: usize, right: usize) -> usize {\n    // Use nums[left] as the pivot\n    let (mut i, mut j) = (left, right);\n    while i < j {\n        while i < j && nums[j] >= nums[left] {\n            j -= 1; // Search from right to left for the first element smaller than the pivot\n        }\n        while i < j && nums[i] <= nums[left] {\n            i += 1; // Search from left to right for the first element greater than the pivot\n        }\n        nums.swap(i, j); // Swap these two elements\n    }\n    nums.swap(i, left); // Swap the pivot to the boundary between the two subarrays\n    i // Return the index of the pivot\n}\n
      quick_sort.c
      /* Swap elements */\nvoid swap(int nums[], int i, int j) {\n    int tmp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = tmp;\n}\n\n/* Sentinel partition */\nint partition(int nums[], int left, int right) {\n    // Use nums[left] as the pivot\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) {\n            j--; // Search from right to left for the first element smaller than the pivot\n        }\n        while (i < j && nums[i] <= nums[left]) {\n            i++; // Search from left to right for the first element greater than the pivot\n        }\n        // Swap these two elements\n        swap(nums, i, j);\n    }\n    // Swap the pivot to the boundary between the two subarrays\n    swap(nums, i, left);\n    // Return the index of the pivot\n    return i;\n}\n
      quick_sort.kt
      /* Swap elements */\nfun swap(nums: IntArray, i: Int, j: Int) {\n    val temp = nums[i]\n    nums[i] = nums[j]\n    nums[j] = temp\n}\n\n/* Sentinel partition */\nfun partition(nums: IntArray, left: Int, right: Int): Int {\n    // Use nums[left] as the pivot\n    var i = left\n    var j = right\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--           // Search from right to left for the first element smaller than the pivot\n        while (i < j && nums[i] <= nums[left])\n            i++           // Search from left to right for the first element greater than the pivot\n        swap(nums, i, j)  // Swap these two elements\n    }\n    swap(nums, i, left)   // Swap the pivot to the boundary between the two subarrays\n    return i              // Return the index of the pivot\n}\n
      quick_sort.rb
      ### Sentinel partition ###\ndef partition(nums, left, right)\n  # Use nums[left] as the pivot\n  i, j = left, right\n  while i < j\n    while i < j && nums[j] >= nums[left]\n      j -= 1 # Search from right to left for the first element smaller than the pivot\n    end\n    while i < j && nums[i] <= nums[left]\n      i += 1 # Search from left to right for the first element greater than the pivot\n    end\n    # Swap elements\n    nums[i], nums[j] = nums[j], nums[i]\n  end\n  # Swap the pivot to the boundary between the two subarrays\n  nums[i], nums[left] = nums[left], nums[i]\n  i # Return the index of the pivot\nend\n
      "},{"location":"chapter_sorting/quick_sort/#1151-algorithm-flow","title":"11.5.1 \u00a0 Algorithm Flow","text":"

      The overall flow of quick sort is shown in Figure 11-9.

      1. First, perform one \"sentinel partitioning\" on the original array to obtain the unsorted left sub-array and right sub-array.
      2. Then, recursively perform \"sentinel partitioning\" on the left sub-array and right sub-array respectively.
      3. Continue recursively until the sub-array length is 1, at which point sorting of the entire array is complete.

      Figure 11-9 \u00a0 Quick sort flow

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
      def quick_sort(self, nums: list[int], left: int, right: int):\n    \"\"\"Quick sort\"\"\"\n    # Terminate recursion when subarray length is 1\n    if left >= right:\n        return\n    # Sentinel partition\n    pivot = self.partition(nums, left, right)\n    # Recursively process the left subarray and right subarray\n    self.quick_sort(nums, left, pivot - 1)\n    self.quick_sort(nums, pivot + 1, right)\n
      quick_sort.cpp
      /* Quick sort */\nvoid quickSort(vector<int> &nums, int left, int right) {\n    // Terminate recursion when subarray length is 1\n    if (left >= right)\n        return;\n    // Sentinel partition\n    int pivot = partition(nums, left, right);\n    // Recursively process the left subarray and right subarray\n    quickSort(nums, left, pivot - 1);\n    quickSort(nums, pivot + 1, right);\n}\n
      quick_sort.java
      /* Quick sort */\nvoid quickSort(int[] nums, int left, int right) {\n    // Terminate recursion when subarray length is 1\n    if (left >= right)\n        return;\n    // Sentinel partition\n    int pivot = partition(nums, left, right);\n    // Recursively process the left subarray and right subarray\n    quickSort(nums, left, pivot - 1);\n    quickSort(nums, pivot + 1, right);\n}\n
      quick_sort.cs
      /* Quick sort */\nvoid QuickSort(int[] nums, int left, int right) {\n    // Terminate recursion when subarray length is 1\n    if (left >= right)\n        return;\n    // Sentinel partition\n    int pivot = Partition(nums, left, right);\n    // Recursively process the left subarray and right subarray\n    QuickSort(nums, left, pivot - 1);\n    QuickSort(nums, pivot + 1, right);\n}\n
      quick_sort.go
      /* Quick sort */\nfunc (q *quickSort) quickSort(nums []int, left, right int) {\n    // Terminate recursion when subarray length is 1\n    if left >= right {\n        return\n    }\n    // Sentinel partition\n    pivot := q.partition(nums, left, right)\n    // Recursively process the left subarray and right subarray\n    q.quickSort(nums, left, pivot-1)\n    q.quickSort(nums, pivot+1, right)\n}\n
      quick_sort.swift
      /* Quick sort */\nfunc quickSort(nums: inout [Int], left: Int, right: Int) {\n    // Terminate recursion when subarray length is 1\n    if left >= right {\n        return\n    }\n    // Sentinel partition\n    let pivot = partition(nums: &nums, left: left, right: right)\n    // Recursively process the left subarray and right subarray\n    quickSort(nums: &nums, left: left, right: pivot - 1)\n    quickSort(nums: &nums, left: pivot + 1, right: right)\n}\n
      quick_sort.js
      /* Quick sort */\nquickSort(nums, left, right) {\n    // Terminate recursion when subarray length is 1\n    if (left >= right) return;\n    // Sentinel partition\n    const pivot = this.partition(nums, left, right);\n    // Recursively process the left subarray and right subarray\n    this.quickSort(nums, left, pivot - 1);\n    this.quickSort(nums, pivot + 1, right);\n}\n
      quick_sort.ts
      /* Quick sort */\nquickSort(nums: number[], left: number, right: number): void {\n    // Terminate recursion when subarray length is 1\n    if (left >= right) {\n        return;\n    }\n    // Sentinel partition\n    const pivot = this.partition(nums, left, right);\n    // Recursively process the left subarray and right subarray\n    this.quickSort(nums, left, pivot - 1);\n    this.quickSort(nums, pivot + 1, right);\n}\n
      quick_sort.dart
      /* Quick sort */\nvoid quickSort(List<int> nums, int left, int right) {\n  // Terminate recursion when subarray length is 1\n  if (left >= right) return;\n  // Sentinel partition\n  int pivot = _partition(nums, left, right);\n  // Recursively process the left subarray and right subarray\n  quickSort(nums, left, pivot - 1);\n  quickSort(nums, pivot + 1, right);\n}\n
      quick_sort.rs
      /* Quick sort */\npub fn quick_sort(left: i32, right: i32, nums: &mut [i32]) {\n    // Terminate recursion when subarray length is 1\n    if left >= right {\n        return;\n    }\n    // Sentinel partition\n    let pivot = Self::partition(nums, left as usize, right as usize) as i32;\n    // Recursively process the left subarray and right subarray\n    Self::quick_sort(left, pivot - 1, nums);\n    Self::quick_sort(pivot + 1, right, nums);\n}\n
      quick_sort.c
      /* Quick sort */\nvoid quickSort(int nums[], int left, int right) {\n    // Terminate recursion when subarray length is 1\n    if (left >= right) {\n        return;\n    }\n    // Sentinel partition\n    int pivot = partition(nums, left, right);\n    // Recursively process the left subarray and right subarray\n    quickSort(nums, left, pivot - 1);\n    quickSort(nums, pivot + 1, right);\n}\n
      quick_sort.kt
      /* Quick sort */\nfun quickSort(nums: IntArray, left: Int, right: Int) {\n    // Terminate recursion when subarray length is 1\n    if (left >= right) return\n    // Sentinel partition\n    val pivot = partition(nums, left, right)\n    // Recursively process the left subarray and right subarray\n    quickSort(nums, left, pivot - 1)\n    quickSort(nums, pivot + 1, right)\n}\n
      quick_sort.rb
      ### Quick sort class ###\ndef quick_sort(nums, left, right)\n  # Recurse when subarray length is not 1\n  if left < right\n    # Sentinel partition\n    pivot = partition(nums, left, right)\n    # Recursively process the left subarray and right subarray\n    quick_sort(nums, left, pivot - 1)\n    quick_sort(nums, pivot + 1, right)\n  end\n  nums\nend\n
      "},{"location":"chapter_sorting/quick_sort/#1152-algorithm-characteristics","title":"11.5.2 \u00a0 Algorithm Characteristics","text":"
      • Time complexity of \\(O(n \\log n)\\), non-adaptive sorting: In the average case, the number of recursive levels of sentinel partitioning is \\(\\log n\\), and the total number of loops at each level is \\(n\\), using \\(O(n \\log n)\\) time overall. In the worst case, each round of sentinel partitioning divides an array of length \\(n\\) into two sub-arrays of length \\(0\\) and \\(n - 1\\), at which point the number of recursive levels reaches \\(n\\), the number of loops at each level is \\(n\\), and the total time used is \\(O(n^2)\\).
      • Space complexity of \\(O(n)\\), in-place sorting: In the case where the input array is completely reversed, the worst recursive depth reaches \\(n\\), using \\(O(n)\\) stack frame space. The sorting operation is performed on the original array without the aid of an additional array.
      • Non-stable sorting: In the last step of sentinel partitioning, the pivot may be swapped to the right of equal elements.
      "},{"location":"chapter_sorting/quick_sort/#1153-why-is-quick-sort-fast","title":"11.5.3 \u00a0 Why Is Quick Sort Fast","text":"

      From the name, we can see that quick sort should have certain advantages in terms of efficiency. Although the average time complexity of quick sort is the same as \"merge sort\" and \"heap sort\", quick sort is usually more efficient, mainly for the following reasons.

      • The probability of the worst case occurring is very low: Although the worst-case time complexity of quick sort is \\(O(n^2)\\), which is not as stable as merge sort, in the vast majority of cases, quick sort can run with a time complexity of \\(O(n \\log n)\\).
      • High cache utilization: When performing sentinel partitioning operations, the system can load the entire sub-array into the cache, so element access efficiency is relatively high. Algorithms like \"heap sort\" require jump-style access to elements, thus lacking this characteristic.
      • Small constant coefficient of complexity: Among the three algorithms mentioned above, quick sort has the smallest total number of operations such as comparisons, assignments, and swaps. This is similar to the reason why \"insertion sort\" is faster than \"bubble sort\".
      "},{"location":"chapter_sorting/quick_sort/#1154-pivot-optimization","title":"11.5.4 \u00a0 Pivot Optimization","text":"

      Quick sort may have reduced time efficiency for certain inputs. Take an extreme example: suppose the input array is completely reversed. Since we select the leftmost element as the pivot, after sentinel partitioning is complete, the pivot is swapped to the rightmost end of the array, causing the left sub-array length to be \\(n - 1\\) and the right sub-array length to be \\(0\\). If we recurse down like this, each round of sentinel partitioning will have a sub-array length of \\(0\\), the divide-and-conquer strategy fails, and quick sort degrades to a form approximate to \"bubble sort\".

      To avoid this situation as much as possible, we can optimize the pivot selection strategy in sentinel partitioning. For example, we can randomly select an element as the pivot. However, if luck is not good and we select a non-ideal pivot every time, efficiency is still not satisfactory.

      It should be noted that programming languages usually generate \"pseudo-random numbers\". If we construct a specific test case for a pseudo-random number sequence, the efficiency of quick sort may still degrade.

      For further improvement, we can select three candidate elements in the array (usually the first, last, and middle elements of the array), and use the median of these three candidate elements as the pivot. In this way, the probability that the pivot is \"neither too small nor too large\" will be greatly increased. Of course, we can also select more candidate elements to further improve the robustness of the algorithm. After adopting this method, the probability of time complexity degrading to \\(O(n^2)\\) is greatly reduced.

      Example code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
      def median_three(self, nums: list[int], left: int, mid: int, right: int) -> int:\n    \"\"\"Select the median of three candidate elements\"\"\"\n    l, m, r = nums[left], nums[mid], nums[right]\n    if (l <= m <= r) or (r <= m <= l):\n        return mid  # m is between l and r\n    if (m <= l <= r) or (r <= l <= m):\n        return left  # l is between m and r\n    return right\n\ndef partition(self, nums: list[int], left: int, right: int) -> int:\n    \"\"\"Sentinel partition (median of three)\"\"\"\n    # Use nums[left] as the pivot\n    med = self.median_three(nums, left, (left + right) // 2, right)\n    # Swap the median to the array's leftmost position\n    nums[left], nums[med] = nums[med], nums[left]\n    # Use nums[left] as the pivot\n    i, j = left, right\n    while i < j:\n        while i < j and nums[j] >= nums[left]:\n            j -= 1  # Search from right to left for the first element smaller than the pivot\n        while i < j and nums[i] <= nums[left]:\n            i += 1  # Search from left to right for the first element greater than the pivot\n        # Swap elements\n        nums[i], nums[j] = nums[j], nums[i]\n    # Swap the pivot to the boundary between the two subarrays\n    nums[i], nums[left] = nums[left], nums[i]\n    return i  # Return the index of the pivot\n
      quick_sort.cpp
      /* Select the median of three candidate elements */\nint medianThree(vector<int> &nums, int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m is between l and r\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l is between m and r\n    return right;\n}\n\n/* Sentinel partition (median of three) */\nint partition(vector<int> &nums, int left, int right) {\n    // Select the median of three candidate elements\n    int med = medianThree(nums, left, (left + right) / 2, right);\n    // Swap the median to the array's leftmost position\n    swap(nums[left], nums[med]);\n    // Use nums[left] as the pivot\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;                // Search from right to left for the first element smaller than the pivot\n        while (i < j && nums[i] <= nums[left])\n            i++;                // Search from left to right for the first element greater than the pivot\n        swap(nums[i], nums[j]); // Swap these two elements\n    }\n    swap(nums[i], nums[left]);  // Swap the pivot to the boundary between the two subarrays\n    return i;                   // Return the index of the pivot\n}\n
      quick_sort.java
      /* Select the median of three candidate elements */\nint medianThree(int[] nums, int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m is between l and r\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l is between m and r\n    return right;\n}\n\n/* Sentinel partition (median of three) */\nint partition(int[] nums, int left, int right) {\n    // Select the median of three candidate elements\n    int med = medianThree(nums, left, (left + right) / 2, right);\n    // Swap the median to the array's leftmost position\n    swap(nums, left, med);\n    // Use nums[left] as the pivot\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // Search from right to left for the first element smaller than the pivot\n        while (i < j && nums[i] <= nums[left])\n            i++;          // Search from left to right for the first element greater than the pivot\n        swap(nums, i, j); // Swap these two elements\n    }\n    swap(nums, i, left);  // Swap the pivot to the boundary between the two subarrays\n    return i;             // Return the index of the pivot\n}\n
      quick_sort.cs
      /* Select the median of three candidate elements */\nint MedianThree(int[] nums, int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m is between l and r\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l is between m and r\n    return right;\n}\n\n/* Sentinel partition (median of three) */\nint Partition(int[] nums, int left, int right) {\n    // Select the median of three candidate elements\n    int med = MedianThree(nums, left, (left + right) / 2, right);\n    // Swap the median to the array's leftmost position\n    Swap(nums, left, med);\n    // Use nums[left] as the pivot\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // Search from right to left for the first element smaller than the pivot\n        while (i < j && nums[i] <= nums[left])\n            i++;          // Search from left to right for the first element greater than the pivot\n        Swap(nums, i, j); // Swap these two elements\n    }\n    Swap(nums, i, left);  // Swap the pivot to the boundary between the two subarrays\n    return i;             // Return the index of the pivot\n}\n
      quick_sort.go
      /* Select the median of three candidate elements */\nfunc (q *quickSortMedian) medianThree(nums []int, left, mid, right int) int {\n    l, m, r := nums[left], nums[mid], nums[right]\n    if (l <= m && m <= r) || (r <= m && m <= l) {\n        return mid // m is between l and r\n    }\n    if (m <= l && l <= r) || (r <= l && l <= m) {\n        return left // l is between m and r\n    }\n    return right\n}\n\n/* Sentinel partition (median of three) */\nfunc (q *quickSortMedian) partition(nums []int, left, right int) int {\n    // Use nums[left] as the pivot\n    med := q.medianThree(nums, left, (left+right)/2, right)\n    // Swap the median to the array's leftmost position\n    nums[left], nums[med] = nums[med], nums[left]\n    // Use nums[left] as the pivot\n    i, j := left, right\n    for i < j {\n        for i < j && nums[j] >= nums[left] {\n            j-- // Search from right to left for the first element smaller than the pivot\n        }\n        for i < j && nums[i] <= nums[left] {\n            i++ // Search from left to right for the first element greater than the pivot\n        }\n        // Swap elements\n        nums[i], nums[j] = nums[j], nums[i]\n    }\n    // Swap the pivot to the boundary between the two subarrays\n    nums[i], nums[left] = nums[left], nums[i]\n    return i // Return the index of the pivot\n}\n
      quick_sort.swift
      /* Select the median of three candidate elements */\nfunc medianThree(nums: [Int], left: Int, mid: Int, right: Int) -> Int {\n    let l = nums[left]\n    let m = nums[mid]\n    let r = nums[right]\n    if (l <= m && m <= r) || (r <= m && m <= l) {\n        return mid // m is between l and r\n    }\n    if (m <= l && l <= r) || (r <= l && l <= m) {\n        return left // l is between m and r\n    }\n    return right\n}\n\n/* Sentinel partition (median of three) */\nfunc partitionMedian(nums: inout [Int], left: Int, right: Int) -> Int {\n    // Select the median of three candidate elements\n    let med = medianThree(nums: nums, left: left, mid: left + (right - left) / 2, right: right)\n    // Swap the median to the array's leftmost position\n    nums.swapAt(left, med)\n    return partition(nums: &nums, left: left, right: right)\n}\n
      quick_sort.js
      /* Select the median of three candidate elements */\nmedianThree(nums, left, mid, right) {\n    let l = nums[left],\n        m = nums[mid],\n        r = nums[right];\n    // m is between l and r\n    if ((l <= m && m <= r) || (r <= m && m <= l)) return mid;\n    // l is between m and r\n    if ((m <= l && l <= r) || (r <= l && l <= m)) return left;\n    return right;\n}\n\n/* Sentinel partition (median of three) */\npartition(nums, left, right) {\n    // Select the median of three candidate elements\n    let med = this.medianThree(\n        nums,\n        left,\n        Math.floor((left + right) / 2),\n        right\n    );\n    // Swap the median to the array's leftmost position\n    this.swap(nums, left, med);\n    // Use nums[left] as the pivot\n    let i = left,\n        j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) j--; // Search from right to left for the first element smaller than the pivot\n        while (i < j && nums[i] <= nums[left]) i++; // Search from left to right for the first element greater than the pivot\n        this.swap(nums, i, j); // Swap these two elements\n    }\n    this.swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays\n    return i; // Return the index of the pivot\n}\n
      quick_sort.ts
      /* Select the median of three candidate elements */\nmedianThree(\n    nums: number[],\n    left: number,\n    mid: number,\n    right: number\n): number {\n    let l = nums[left],\n        m = nums[mid],\n        r = nums[right];\n    // m is between l and r\n    if ((l <= m && m <= r) || (r <= m && m <= l)) return mid;\n    // l is between m and r\n    if ((m <= l && l <= r) || (r <= l && l <= m)) return left;\n    return right;\n}\n\n/* Sentinel partition (median of three) */\npartition(nums: number[], left: number, right: number): number {\n    // Select the median of three candidate elements\n    let med = this.medianThree(\n        nums,\n        left,\n        Math.floor((left + right) / 2),\n        right\n    );\n    // Swap the median to the array's leftmost position\n    this.swap(nums, left, med);\n    // Use nums[left] as the pivot\n    let i = left,\n        j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) {\n            j--; // Search from right to left for the first element smaller than the pivot\n        }\n        while (i < j && nums[i] <= nums[left]) {\n            i++; // Search from left to right for the first element greater than the pivot\n        }\n        this.swap(nums, i, j); // Swap these two elements\n    }\n    this.swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays\n    return i; // Return the index of the pivot\n}\n
      quick_sort.dart
      /* Select the median of three candidate elements */\nint _medianThree(List<int> nums, int left, int mid, int right) {\n  int l = nums[left], m = nums[mid], r = nums[right];\n  if ((l <= m && m <= r) || (r <= m && m <= l))\n    return mid; // m is between l and r\n  if ((m <= l && l <= r) || (r <= l && l <= m))\n    return left; // l is between m and r\n  return right;\n}\n\n/* Sentinel partition (median of three) */\nint _partition(List<int> nums, int left, int right) {\n  // Select the median of three candidate elements\n  int med = _medianThree(nums, left, (left + right) ~/ 2, right);\n  // Swap the median to the array's leftmost position\n  _swap(nums, left, med);\n  // Use nums[left] as the pivot\n  int i = left, j = right;\n  while (i < j) {\n    while (i < j && nums[j] >= nums[left]) j--; // Search from right to left for the first element smaller than the pivot\n    while (i < j && nums[i] <= nums[left]) i++; // Search from left to right for the first element greater than the pivot\n    _swap(nums, i, j); // Swap these two elements\n  }\n  _swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays\n  return i; // Return the index of the pivot\n}\n
      quick_sort.rs
      /* Select the median of three candidate elements */\nfn median_three(nums: &mut [i32], left: usize, mid: usize, right: usize) -> usize {\n    let (l, m, r) = (nums[left], nums[mid], nums[right]);\n    if (l <= m && m <= r) || (r <= m && m <= l) {\n        return mid; // m is between l and r\n    }\n    if (m <= l && l <= r) || (r <= l && l <= m) {\n        return left; // l is between m and r\n    }\n    right\n}\n\n/* Sentinel partition (median of three) */\nfn partition(nums: &mut [i32], left: usize, right: usize) -> usize {\n    // Select the median of three candidate elements\n    let med = Self::median_three(nums, left, (left + right) / 2, right);\n    // Swap the median to the array's leftmost position\n    nums.swap(left, med);\n    // Use nums[left] as the pivot\n    let (mut i, mut j) = (left, right);\n    while i < j {\n        while i < j && nums[j] >= nums[left] {\n            j -= 1; // Search from right to left for the first element smaller than the pivot\n        }\n        while i < j && nums[i] <= nums[left] {\n            i += 1; // Search from left to right for the first element greater than the pivot\n        }\n        nums.swap(i, j); // Swap these two elements\n    }\n    nums.swap(i, left); // Swap the pivot to the boundary between the two subarrays\n    i // Return the index of the pivot\n}\n
      quick_sort.c
      /* Select the median of three candidate elements */\nint medianThree(int nums[], int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m is between l and r\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l is between m and r\n    return right;\n}\n\n/* Sentinel partition (median of three) */\nint partitionMedian(int nums[], int left, int right) {\n    // Select the median of three candidate elements\n    int med = medianThree(nums, left, (left + right) / 2, right);\n    // Swap the median to the array's leftmost position\n    swap(nums, left, med);\n    // Use nums[left] as the pivot\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--; // Search from right to left for the first element smaller than the pivot\n        while (i < j && nums[i] <= nums[left])\n            i++;          // Search from left to right for the first element greater than the pivot\n        swap(nums, i, j); // Swap these two elements\n    }\n    swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays\n    return i;            // Return the index of the pivot\n}\n
      quick_sort.kt
      /* Select the median of three candidate elements */\nfun medianThree(nums: IntArray, left: Int, mid: Int, right: Int): Int {\n    val l = nums[left]\n    val m = nums[mid]\n    val r = nums[right]\n    if ((m in l..r) || (m in r..l))\n        return mid  // m is between l and r\n    if ((l in m..r) || (l in r..m))\n        return left // l is between m and r\n    return right\n}\n\n/* Sentinel partition (median of three) */\nfun partitionMedian(nums: IntArray, left: Int, right: Int): Int {\n    // Select the median of three candidate elements\n    val med = medianThree(nums, left, (left + right) / 2, right)\n    // Swap the median to the array's leftmost position\n    swap(nums, left, med)\n    // Use nums[left] as the pivot\n    var i = left\n    var j = right\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--                      // Search from right to left for the first element smaller than the pivot\n        while (i < j && nums[i] <= nums[left])\n            i++                      // Search from left to right for the first element greater than the pivot\n        swap(nums, i, j)             // Swap these two elements\n    }\n    swap(nums, i, left)              // Swap the pivot to the boundary between the two subarrays\n    return i                         // Return the index of the pivot\n}\n
      quick_sort.rb
      ### Select median of three candidate elements ###\ndef median_three(nums, left, mid, right)\n  # Select the median of three candidate elements\n  _l, _m, _r = nums[left], nums[mid], nums[right]\n  # m is between l and r\n  return mid if (_l <= _m && _m <= _r) || (_r <= _m && _m <= _l)\n  # l is between m and r\n  return left if (_m <= _l && _l <= _r) || (_r <= _l && _l <= _m)\n  return right\nend\n\n### Sentinel partition (median of three) ###\ndef partition(nums, left, right)\n  ### Use nums[left] as pivot\n  med = median_three(nums, left, (left + right) / 2, right)\n  # Swap median to leftmost position of array\n  nums[left], nums[med] = nums[med], nums[left]\n  i, j = left, right\n  while i < j\n    while i < j && nums[j] >= nums[left]\n      j -= 1 # Search from right to left for the first element smaller than the pivot\n    end\n    while i < j && nums[i] <= nums[left]\n      i += 1 # Search from left to right for the first element greater than the pivot\n    end\n    # Swap elements\n    nums[i], nums[j] = nums[j], nums[i]\n  end\n  # Swap the pivot to the boundary between the two subarrays\n  nums[i], nums[left] = nums[left], nums[i]\n  i # Return the index of the pivot\nend\n
      "},{"location":"chapter_sorting/quick_sort/#1155-recursive-depth-optimization","title":"11.5.5 \u00a0 Recursive Depth Optimization","text":"

      For certain inputs, quick sort may occupy more space. Taking a completely ordered input array as an example, let the length of the sub-array in recursion be \\(m\\). Each round of sentinel partitioning will produce a left sub-array of length \\(0\\) and a right sub-array of length \\(m - 1\\), which means that the problem scale reduced per recursive call is very small (only one element is reduced), and the height of the recursion tree will reach \\(n - 1\\), at which point \\(O(n)\\) size of stack frame space is required.

      To prevent the accumulation of stack frame space, we can compare the lengths of the two sub-arrays after each round of sentinel sorting is complete, and only recurse on the shorter sub-array. Since the length of the shorter sub-array will not exceed \\(n / 2\\), this method can ensure that the recursion depth does not exceed \\(\\log n\\), thus optimizing the worst-case space complexity to \\(O(\\log n)\\). The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
      def quick_sort(self, nums: list[int], left: int, right: int):\n    \"\"\"Quick sort (recursion depth optimization)\"\"\"\n    # Terminate when subarray length is 1\n    while left < right:\n        # Sentinel partition operation\n        pivot = self.partition(nums, left, right)\n        # Perform quick sort on the shorter of the two subarrays\n        if pivot - left < right - pivot:\n            self.quick_sort(nums, left, pivot - 1)  # Recursively sort the left subarray\n            left = pivot + 1  # Remaining unsorted interval is [pivot + 1, right]\n        else:\n            self.quick_sort(nums, pivot + 1, right)  # Recursively sort the right subarray\n            right = pivot - 1  # Remaining unsorted interval is [left, pivot - 1]\n
      quick_sort.cpp
      /* Quick sort (recursion depth optimization) */\nvoid quickSort(vector<int> &nums, int left, int right) {\n    // Terminate when subarray length is 1\n    while (left < right) {\n        // Sentinel partition operation\n        int pivot = partition(nums, left, right);\n        // Perform quick sort on the shorter of the two subarrays\n        if (pivot - left < right - pivot) {\n            quickSort(nums, left, pivot - 1); // Recursively sort the left subarray\n            left = pivot + 1;                 // Remaining unsorted interval is [pivot + 1, right]\n        } else {\n            quickSort(nums, pivot + 1, right); // Recursively sort the right subarray\n            right = pivot - 1;                 // Remaining unsorted interval is [left, pivot - 1]\n        }\n    }\n}\n
      quick_sort.java
      /* Quick sort (recursion depth optimization) */\nvoid quickSort(int[] nums, int left, int right) {\n    // Terminate when subarray length is 1\n    while (left < right) {\n        // Sentinel partition operation\n        int pivot = partition(nums, left, right);\n        // Perform quick sort on the shorter of the two subarrays\n        if (pivot - left < right - pivot) {\n            quickSort(nums, left, pivot - 1); // Recursively sort the left subarray\n            left = pivot + 1; // Remaining unsorted interval is [pivot + 1, right]\n        } else {\n            quickSort(nums, pivot + 1, right); // Recursively sort the right subarray\n            right = pivot - 1; // Remaining unsorted interval is [left, pivot - 1]\n        }\n    }\n}\n
      quick_sort.cs
      /* Quick sort (recursion depth optimization) */\nvoid QuickSort(int[] nums, int left, int right) {\n    // Terminate when subarray length is 1\n    while (left < right) {\n        // Sentinel partition operation\n        int pivot = Partition(nums, left, right);\n        // Perform quick sort on the shorter of the two subarrays\n        if (pivot - left < right - pivot) {\n            QuickSort(nums, left, pivot - 1);  // Recursively sort the left subarray\n            left = pivot + 1;  // Remaining unsorted interval is [pivot + 1, right]\n        } else {\n            QuickSort(nums, pivot + 1, right); // Recursively sort the right subarray\n            right = pivot - 1; // Remaining unsorted interval is [left, pivot - 1]\n        }\n    }\n}\n
      quick_sort.go
      /* Quick sort (recursion depth optimization) */\nfunc (q *quickSortTailCall) quickSort(nums []int, left, right int) {\n    // Terminate when subarray length is 1\n    for left < right {\n        // Sentinel partition operation\n        pivot := q.partition(nums, left, right)\n        // Perform quick sort on the shorter of the two subarrays\n        if pivot-left < right-pivot {\n            q.quickSort(nums, left, pivot-1) // Recursively sort the left subarray\n            left = pivot + 1                 // Remaining unsorted interval is [pivot + 1, right]\n        } else {\n            q.quickSort(nums, pivot+1, right) // Recursively sort the right subarray\n            right = pivot - 1                 // Remaining unsorted interval is [left, pivot - 1]\n        }\n    }\n}\n
      quick_sort.swift
      /* Quick sort (recursion depth optimization) */\nfunc quickSortTailCall(nums: inout [Int], left: Int, right: Int) {\n    var left = left\n    var right = right\n    // Terminate when subarray length is 1\n    while left < right {\n        // Sentinel partition operation\n        let pivot = partition(nums: &nums, left: left, right: right)\n        // Perform quick sort on the shorter of the two subarrays\n        if (pivot - left) < (right - pivot) {\n            quickSortTailCall(nums: &nums, left: left, right: pivot - 1) // Recursively sort the left subarray\n            left = pivot + 1 // Remaining unsorted interval is [pivot + 1, right]\n        } else {\n            quickSortTailCall(nums: &nums, left: pivot + 1, right: right) // Recursively sort the right subarray\n            right = pivot - 1 // Remaining unsorted interval is [left, pivot - 1]\n        }\n    }\n}\n
      quick_sort.js
      /* Quick sort (recursion depth optimization) */\nquickSort(nums, left, right) {\n    // Terminate when subarray length is 1\n    while (left < right) {\n        // Sentinel partition operation\n        let pivot = this.partition(nums, left, right);\n        // Perform quick sort on the shorter of the two subarrays\n        if (pivot - left < right - pivot) {\n            this.quickSort(nums, left, pivot - 1); // Recursively sort the left subarray\n            left = pivot + 1; // Remaining unsorted interval is [pivot + 1, right]\n        } else {\n            this.quickSort(nums, pivot + 1, right); // Recursively sort the right subarray\n            right = pivot - 1; // Remaining unsorted interval is [left, pivot - 1]\n        }\n    }\n}\n
      quick_sort.ts
      /* Quick sort (recursion depth optimization) */\nquickSort(nums: number[], left: number, right: number): void {\n    // Terminate when subarray length is 1\n    while (left < right) {\n        // Sentinel partition operation\n        let pivot = this.partition(nums, left, right);\n        // Perform quick sort on the shorter of the two subarrays\n        if (pivot - left < right - pivot) {\n            this.quickSort(nums, left, pivot - 1); // Recursively sort the left subarray\n            left = pivot + 1; // Remaining unsorted interval is [pivot + 1, right]\n        } else {\n            this.quickSort(nums, pivot + 1, right); // Recursively sort the right subarray\n            right = pivot - 1; // Remaining unsorted interval is [left, pivot - 1]\n        }\n    }\n}\n
      quick_sort.dart
      /* Quick sort (recursion depth optimization) */\nvoid quickSort(List<int> nums, int left, int right) {\n  // Terminate when subarray length is 1\n  while (left < right) {\n    // Sentinel partition operation\n    int pivot = _partition(nums, left, right);\n    // Perform quick sort on the shorter of the two subarrays\n    if (pivot - left < right - pivot) {\n      quickSort(nums, left, pivot - 1); // Recursively sort the left subarray\n      left = pivot + 1; // Remaining unsorted interval is [pivot + 1, right]\n    } else {\n      quickSort(nums, pivot + 1, right); // Recursively sort the right subarray\n      right = pivot - 1; // Remaining unsorted interval is [left, pivot - 1]\n    }\n  }\n}\n
      quick_sort.rs
      /* Quick sort (recursion depth optimization) */\npub fn quick_sort(mut left: i32, mut right: i32, nums: &mut [i32]) {\n    // Terminate when subarray length is 1\n    while left < right {\n        // Sentinel partition operation\n        let pivot = Self::partition(nums, left as usize, right as usize) as i32;\n        // Perform quick sort on the shorter of the two subarrays\n        if pivot - left < right - pivot {\n            Self::quick_sort(left, pivot - 1, nums); // Recursively sort the left subarray\n            left = pivot + 1; // Remaining unsorted interval is [pivot + 1, right]\n        } else {\n            Self::quick_sort(pivot + 1, right, nums); // Recursively sort the right subarray\n            right = pivot - 1; // Remaining unsorted interval is [left, pivot - 1]\n        }\n    }\n}\n
      quick_sort.c
      /* Quick sort (recursion depth optimization) */\nvoid quickSortTailCall(int nums[], int left, int right) {\n    // Terminate when subarray length is 1\n    while (left < right) {\n        // Sentinel partition operation\n        int pivot = partition(nums, left, right);\n        // Perform quick sort on the shorter of the two subarrays\n        if (pivot - left < right - pivot) {\n            // Recursively sort the left subarray\n            quickSortTailCall(nums, left, pivot - 1);\n            // Remaining unsorted interval is [pivot + 1, right]\n            left = pivot + 1;\n        } else {\n            // Recursively sort the right subarray\n            quickSortTailCall(nums, pivot + 1, right);\n            // Remaining unsorted interval is [left, pivot - 1]\n            right = pivot - 1;\n        }\n    }\n}\n
      quick_sort.kt
      /* Quick sort (recursion depth optimization) */\nfun quickSortTailCall(nums: IntArray, left: Int, right: Int) {\n    // Terminate when subarray length is 1\n    var l = left\n    var r = right\n    while (l < r) {\n        // Sentinel partition operation\n        val pivot = partition(nums, l, r)\n        // Perform quick sort on the shorter of the two subarrays\n        if (pivot - l < r - pivot) {\n            quickSort(nums, l, pivot - 1) // Recursively sort the left subarray\n            l = pivot + 1 // Remaining unsorted interval is [pivot + 1, right]\n        } else {\n            quickSort(nums, pivot + 1, r) // Recursively sort the right subarray\n            r = pivot - 1 // Remaining unsorted interval is [left, pivot - 1]\n        }\n    }\n}\n
      quick_sort.rb
      ### Quick sort (recursion depth optimization) ###\ndef quick_sort(nums, left, right)\n  # Recurse when subarray length is not 1\n  while left < right\n    # Sentinel partition\n    pivot = partition(nums, left, right)\n    # Perform quick sort on the shorter of the two subarrays\n    if pivot - left < right - pivot\n      quick_sort(nums, left, pivot - 1)\n      left = pivot + 1 # Remaining unsorted interval is [pivot + 1, right]\n    else\n      quick_sort(nums, pivot + 1, right)\n      right = pivot - 1 # Remaining unsorted interval is [left, pivot - 1]\n    end\n  end\nend\n
      "},{"location":"chapter_sorting/radix_sort/","title":"11.10 \u00a0 Radix Sort","text":"

      The previous section introduced counting sort, which is suitable for situations where the data volume \\(n\\) is large but the data range \\(m\\) is small. Suppose we need to sort \\(n = 10^6\\) student IDs, and the student ID is an 8-digit number, which means the data range \\(m = 10^8\\) is very large. Using counting sort would require allocating a large amount of memory space, whereas radix sort can avoid this situation.

      Radix sort (radix sort) has a core idea consistent with counting sort, which also achieves sorting by counting quantities. Building on this, radix sort utilizes the progressive relationship between the digits of numbers, sorting each digit in turn to obtain the final sorting result.

      "},{"location":"chapter_sorting/radix_sort/#11101-algorithm-flow","title":"11.10.1 \u00a0 Algorithm Flow","text":"

      Taking student ID data as an example, assume the lowest digit is the \\(1\\)st digit and the highest digit is the \\(8\\)th digit. The flow of radix sort is shown in Figure 11-18.

      1. Initialize the digit \\(k = 1\\).
      2. Perform \"counting sort\" on the \\(k\\)th digit of the student IDs. After completion, the data will be sorted from smallest to largest according to the \\(k\\)th digit.
      3. Increase \\(k\\) by \\(1\\), then return to step 2. and continue iterating until all digits are sorted, at which point the process ends.

      Figure 11-18 \u00a0 Radix sort algorithm flow

      Below we analyze the code implementation. For a \\(d\\)-base number \\(x\\), to get its \\(k\\)th digit \\(x_k\\), the following calculation formula can be used:

      \\[ x_k = \\lfloor\\frac{x}{d^{k-1}}\\rfloor \\bmod d \\]

      Where \\(\\lfloor a \\rfloor\\) denotes rounding down the floating-point number \\(a\\), and \\(\\bmod \\: d\\) denotes taking the modulo (remainder) with respect to \\(d\\). For student ID data, \\(d = 10\\) and \\(k \\in [1, 8]\\).

      Additionally, we need to slightly modify the counting sort code to make it sort based on the \\(k\\)th digit of the number:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby radix_sort.py
      def digit(num: int, exp: int) -> int:\n    \"\"\"Get the k-th digit of element num, where exp = 10^(k-1)\"\"\"\n    # Passing exp instead of k can avoid repeated expensive exponentiation here\n    return (num // exp) % 10\n\ndef counting_sort_digit(nums: list[int], exp: int):\n    \"\"\"Counting sort (based on nums k-th digit)\"\"\"\n    # Decimal digit range is 0~9, therefore need a bucket array of length 10\n    counter = [0] * 10\n    n = len(nums)\n    # Count the occurrence of digits 0~9\n    for i in range(n):\n        d = digit(nums[i], exp)  # Get the k-th digit of nums[i], noted as d\n        counter[d] += 1  # Count the occurrence of digit d\n    # Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n    for i in range(1, 10):\n        counter[i] += counter[i - 1]\n    # Traverse in reverse, based on bucket statistics, place each element into res\n    res = [0] * n\n    for i in range(n - 1, -1, -1):\n        d = digit(nums[i], exp)\n        j = counter[d] - 1  # Get the index j for d in the array\n        res[j] = nums[i]  # Place the current element at index j\n        counter[d] -= 1  # Decrease the count of d by 1\n    # Use result to overwrite the original array nums\n    for i in range(n):\n        nums[i] = res[i]\n\ndef radix_sort(nums: list[int]):\n    \"\"\"Radix sort\"\"\"\n    # Get the maximum element of the array, used to determine the maximum number of digits\n    m = max(nums)\n    # Traverse from the lowest to the highest digit\n    exp = 1\n    while exp <= m:\n        # Perform counting sort on the k-th digit of array elements\n        # k = 1 -> exp = 1\n        # k = 2 -> exp = 10\n        # i.e., exp = 10^(k-1)\n        counting_sort_digit(nums, exp)\n        exp *= 10\n
      radix_sort.cpp
      /* Get the k-th digit of element num, where exp = 10^(k-1) */\nint digit(int num, int exp) {\n    // Passing exp instead of k can avoid repeated expensive exponentiation here\n    return (num / exp) % 10;\n}\n\n/* Counting sort (based on nums k-th digit) */\nvoid countingSortDigit(vector<int> &nums, int exp) {\n    // Decimal digit range is 0~9, therefore need a bucket array of length 10\n    vector<int> counter(10, 0);\n    int n = nums.size();\n    // Count the occurrence of digits 0~9\n    for (int i = 0; i < n; i++) {\n        int d = digit(nums[i], exp); // Get the k-th digit of nums[i], noted as d\n        counter[d]++;                // Count the occurrence of digit d\n    }\n    // Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // Traverse in reverse, based on bucket statistics, place each element into res\n    vector<int> res(n, 0);\n    for (int i = n - 1; i >= 0; i--) {\n        int d = digit(nums[i], exp);\n        int j = counter[d] - 1; // Get the index j for d in the array\n        res[j] = nums[i];       // Place the current element at index j\n        counter[d]--;           // Decrease the count of d by 1\n    }\n    // Use result to overwrite the original array nums\n    for (int i = 0; i < n; i++)\n        nums[i] = res[i];\n}\n\n/* Radix sort */\nvoid radixSort(vector<int> &nums) {\n    // Get the maximum element of the array, used to determine the maximum number of digits\n    int m = *max_element(nums.begin(), nums.end());\n    // Traverse from the lowest to the highest digit\n    for (int exp = 1; exp <= m; exp *= 10)\n        // Perform counting sort on the k-th digit of array elements\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // i.e., exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n}\n
      radix_sort.java
      /* Get the k-th digit of element num, where exp = 10^(k-1) */\nint digit(int num, int exp) {\n    // Passing exp instead of k can avoid repeated expensive exponentiation here\n    return (num / exp) % 10;\n}\n\n/* Counting sort (based on nums k-th digit) */\nvoid countingSortDigit(int[] nums, int exp) {\n    // Decimal digit range is 0~9, therefore need a bucket array of length 10\n    int[] counter = new int[10];\n    int n = nums.length;\n    // Count the occurrence of digits 0~9\n    for (int i = 0; i < n; i++) {\n        int d = digit(nums[i], exp); // Get the k-th digit of nums[i], noted as d\n        counter[d]++;                // Count the occurrence of digit d\n    }\n    // Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // Traverse in reverse, based on bucket statistics, place each element into res\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int d = digit(nums[i], exp);\n        int j = counter[d] - 1; // Get the index j for d in the array\n        res[j] = nums[i];       // Place the current element at index j\n        counter[d]--;           // Decrease the count of d by 1\n    }\n    // Use result to overwrite the original array nums\n    for (int i = 0; i < n; i++)\n        nums[i] = res[i];\n}\n\n/* Radix sort */\nvoid radixSort(int[] nums) {\n    // Get the maximum element of the array, used to determine the maximum number of digits\n    int m = Integer.MIN_VALUE;\n    for (int num : nums)\n        if (num > m)\n            m = num;\n    // Traverse from the lowest to the highest digit\n    for (int exp = 1; exp <= m; exp *= 10) {\n        // Perform counting sort on the k-th digit of array elements\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // i.e., exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n    }\n}\n
      radix_sort.cs
      /* Get the k-th digit of element num, where exp = 10^(k-1) */\nint Digit(int num, int exp) {\n    // Passing exp instead of k can avoid repeated expensive exponentiation here\n    return (num / exp) % 10;\n}\n\n/* Counting sort (based on nums k-th digit) */\nvoid CountingSortDigit(int[] nums, int exp) {\n    // Decimal digit range is 0~9, therefore need a bucket array of length 10\n    int[] counter = new int[10];\n    int n = nums.Length;\n    // Count the occurrence of digits 0~9\n    for (int i = 0; i < n; i++) {\n        int d = Digit(nums[i], exp); // Get the k-th digit of nums[i], noted as d\n        counter[d]++;                // Count the occurrence of digit d\n    }\n    // Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // Traverse in reverse, based on bucket statistics, place each element into res\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int d = Digit(nums[i], exp);\n        int j = counter[d] - 1; // Get the index j for d in the array\n        res[j] = nums[i];       // Place the current element at index j\n        counter[d]--;           // Decrease the count of d by 1\n    }\n    // Use result to overwrite the original array nums\n    for (int i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n\n/* Radix sort */\nvoid RadixSort(int[] nums) {\n    // Get the maximum element of the array, used to determine the maximum number of digits\n    int m = int.MinValue;\n    foreach (int num in nums) {\n        if (num > m) m = num;\n    }\n    // Traverse from the lowest to the highest digit\n    for (int exp = 1; exp <= m; exp *= 10) {\n        // Perform counting sort on the k-th digit of array elements\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // i.e., exp = 10^(k-1)\n        CountingSortDigit(nums, exp);\n    }\n}\n
      radix_sort.go
      /* Get the k-th digit of element num, where exp = 10^(k-1) */\nfunc digit(num, exp int) int {\n    // Passing exp instead of k can avoid repeated expensive exponentiation here\n    return (num / exp) % 10\n}\n\n/* Counting sort (based on nums k-th digit) */\nfunc countingSortDigit(nums []int, exp int) {\n    // Decimal digit range is 0~9, therefore need a bucket array of length 10\n    counter := make([]int, 10)\n    n := len(nums)\n    // Count the occurrence of digits 0~9\n    for i := 0; i < n; i++ {\n        d := digit(nums[i], exp) // Get the k-th digit of nums[i], noted as d\n        counter[d]++             // Count the occurrence of digit d\n    }\n    // Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n    for i := 1; i < 10; i++ {\n        counter[i] += counter[i-1]\n    }\n    // Traverse in reverse, based on bucket statistics, place each element into res\n    res := make([]int, n)\n    for i := n - 1; i >= 0; i-- {\n        d := digit(nums[i], exp)\n        j := counter[d] - 1 // Get the index j for d in the array\n        res[j] = nums[i]    // Place the current element at index j\n        counter[d]--        // Decrease the count of d by 1\n    }\n    // Use result to overwrite the original array nums\n    for i := 0; i < n; i++ {\n        nums[i] = res[i]\n    }\n}\n\n/* Radix sort */\nfunc radixSort(nums []int) {\n    // Get the maximum element of the array, used to determine the maximum number of digits\n    max := math.MinInt\n    for _, num := range nums {\n        if num > max {\n            max = num\n        }\n    }\n    // Traverse from the lowest to the highest digit\n    for exp := 1; max >= exp; exp *= 10 {\n        // Perform counting sort on the k-th digit of array elements\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // i.e., exp = 10^(k-1)\n        countingSortDigit(nums, exp)\n    }\n}\n
      radix_sort.swift
      /* Get the k-th digit of element num, where exp = 10^(k-1) */\nfunc digit(num: Int, exp: Int) -> Int {\n    // Passing exp instead of k can avoid repeated expensive exponentiation here\n    (num / exp) % 10\n}\n\n/* Counting sort (based on nums k-th digit) */\nfunc countingSortDigit(nums: inout [Int], exp: Int) {\n    // Decimal digit range is 0~9, therefore need a bucket array of length 10\n    var counter = Array(repeating: 0, count: 10)\n    // Count the occurrence of digits 0~9\n    for i in nums.indices {\n        let d = digit(num: nums[i], exp: exp) // Get the k-th digit of nums[i], noted as d\n        counter[d] += 1 // Count the occurrence of digit d\n    }\n    // Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n    for i in 1 ..< 10 {\n        counter[i] += counter[i - 1]\n    }\n    // Traverse in reverse, based on bucket statistics, place each element into res\n    var res = Array(repeating: 0, count: nums.count)\n    for i in nums.indices.reversed() {\n        let d = digit(num: nums[i], exp: exp)\n        let j = counter[d] - 1 // Get the index j for d in the array\n        res[j] = nums[i] // Place the current element at index j\n        counter[d] -= 1 // Decrease the count of d by 1\n    }\n    // Use result to overwrite the original array nums\n    for i in nums.indices {\n        nums[i] = res[i]\n    }\n}\n\n/* Radix sort */\nfunc radixSort(nums: inout [Int]) {\n    // Get the maximum element of the array, used to determine the maximum number of digits\n    var m = Int.min\n    for num in nums {\n        if num > m {\n            m = num\n        }\n    }\n    // Traverse from the lowest to the highest digit\n    for exp in sequence(first: 1, next: { m >= ($0 * 10) ? $0 * 10 : nil }) {\n        // Perform counting sort on the k-th digit of array elements\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // i.e., exp = 10^(k-1)\n        countingSortDigit(nums: &nums, exp: exp)\n    }\n}\n
      radix_sort.js
      /* Get the k-th digit of element num, where exp = 10^(k-1) */\nfunction digit(num, exp) {\n    // Passing exp instead of k can avoid repeated expensive exponentiation here\n    return Math.floor(num / exp) % 10;\n}\n\n/* Counting sort (based on nums k-th digit) */\nfunction countingSortDigit(nums, exp) {\n    // Decimal digit range is 0~9, therefore need a bucket array of length 10\n    const counter = new Array(10).fill(0);\n    const n = nums.length;\n    // Count the occurrence of digits 0~9\n    for (let i = 0; i < n; i++) {\n        const d = digit(nums[i], exp); // Get the k-th digit of nums[i], noted as d\n        counter[d]++; // Count the occurrence of digit d\n    }\n    // Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n    for (let i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // Traverse in reverse, based on bucket statistics, place each element into res\n    const res = new Array(n).fill(0);\n    for (let i = n - 1; i >= 0; i--) {\n        const d = digit(nums[i], exp);\n        const j = counter[d] - 1; // Get the index j for d in the array\n        res[j] = nums[i]; // Place the current element at index j\n        counter[d]--; // Decrease the count of d by 1\n    }\n    // Use result to overwrite the original array nums\n    for (let i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n\n/* Radix sort */\nfunction radixSort(nums) {\n    // Get the maximum element of the array, used to determine the maximum number of digits\n    let m = Math.max(... nums);\n    // Traverse from the lowest to the highest digit\n    for (let exp = 1; exp <= m; exp *= 10) {\n        // Perform counting sort on the k-th digit of array elements\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // i.e., exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n    }\n}\n
      radix_sort.ts
      /* Get the k-th digit of element num, where exp = 10^(k-1) */\nfunction digit(num: number, exp: number): number {\n    // Passing exp instead of k can avoid repeated expensive exponentiation here\n    return Math.floor(num / exp) % 10;\n}\n\n/* Counting sort (based on nums k-th digit) */\nfunction countingSortDigit(nums: number[], exp: number): void {\n    // Decimal digit range is 0~9, therefore need a bucket array of length 10\n    const counter = new Array(10).fill(0);\n    const n = nums.length;\n    // Count the occurrence of digits 0~9\n    for (let i = 0; i < n; i++) {\n        const d = digit(nums[i], exp); // Get the k-th digit of nums[i], noted as d\n        counter[d]++; // Count the occurrence of digit d\n    }\n    // Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n    for (let i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // Traverse in reverse, based on bucket statistics, place each element into res\n    const res = new Array(n).fill(0);\n    for (let i = n - 1; i >= 0; i--) {\n        const d = digit(nums[i], exp);\n        const j = counter[d] - 1; // Get the index j for d in the array\n        res[j] = nums[i]; // Place the current element at index j\n        counter[d]--; // Decrease the count of d by 1\n    }\n    // Use result to overwrite the original array nums\n    for (let i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n\n/* Radix sort */\nfunction radixSort(nums: number[]): void {\n    // Get the maximum element of the array, used to determine the maximum number of digits\n    let m: number = Math.max(... nums);\n    // Traverse from the lowest to the highest digit\n    for (let exp = 1; exp <= m; exp *= 10) {\n        // Perform counting sort on the k-th digit of array elements\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // i.e., exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n    }\n}\n
      radix_sort.dart
      /* Get k-th digit of element _num, where exp = 10^(k-1) */\nint digit(int _num, int exp) {\n  // Passing exp instead of k can avoid repeated expensive exponentiation here\n  return (_num ~/ exp) % 10;\n}\n\n/* Counting sort (based on nums k-th digit) */\nvoid countingSortDigit(List<int> nums, int exp) {\n  // Decimal digit range is 0~9, therefore need a bucket array of length 10\n  List<int> counter = List<int>.filled(10, 0);\n  int n = nums.length;\n  // Count the occurrence of digits 0~9\n  for (int i = 0; i < n; i++) {\n    int d = digit(nums[i], exp); // Get the k-th digit of nums[i], noted as d\n    counter[d]++; // Count the occurrence of digit d\n  }\n  // Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n  for (int i = 1; i < 10; i++) {\n    counter[i] += counter[i - 1];\n  }\n  // Traverse in reverse, based on bucket statistics, place each element into res\n  List<int> res = List<int>.filled(n, 0);\n  for (int i = n - 1; i >= 0; i--) {\n    int d = digit(nums[i], exp);\n    int j = counter[d] - 1; // Get the index j for d in the array\n    res[j] = nums[i]; // Place the current element at index j\n    counter[d]--; // Decrease the count of d by 1\n  }\n  // Use result to overwrite the original array nums\n  for (int i = 0; i < n; i++) nums[i] = res[i];\n}\n\n/* Radix sort */\nvoid radixSort(List<int> nums) {\n  // Get the maximum element of the array, used to determine the maximum number of digits\n  // In Dart, int length is 64 bits\n  int m = -1 << 63;\n  for (int _num in nums) if (_num > m) m = _num;\n  // Traverse from the lowest to the highest digit\n  for (int exp = 1; exp <= m; exp *= 10)\n    // Perform counting sort on the k-th digit of array elements\n    // k = 1 -> exp = 1\n    // k = 2 -> exp = 10\n    // i.e., exp = 10^(k-1)\n    countingSortDigit(nums, exp);\n}\n
      radix_sort.rs
      /* Get the k-th digit of element num, where exp = 10^(k-1) */\nfn digit(num: i32, exp: i32) -> usize {\n    // Passing exp instead of k can avoid repeated expensive exponentiation here\n    return ((num / exp) % 10) as usize;\n}\n\n/* Counting sort (based on nums k-th digit) */\nfn counting_sort_digit(nums: &mut [i32], exp: i32) {\n    // Decimal digit range is 0~9, therefore need a bucket array of length 10\n    let mut counter = [0; 10];\n    let n = nums.len();\n    // Count the occurrence of digits 0~9\n    for i in 0..n {\n        let d = digit(nums[i], exp); // Get the k-th digit of nums[i], noted as d\n        counter[d] += 1; // Count the occurrence of digit d\n    }\n    // Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n    for i in 1..10 {\n        counter[i] += counter[i - 1];\n    }\n    // Traverse in reverse, based on bucket statistics, place each element into res\n    let mut res = vec![0; n];\n    for i in (0..n).rev() {\n        let d = digit(nums[i], exp);\n        let j = counter[d] - 1; // Get the index j for d in the array\n        res[j] = nums[i]; // Place the current element at index j\n        counter[d] -= 1; // Decrease the count of d by 1\n    }\n    // Use result to overwrite the original array nums\n    nums.copy_from_slice(&res);\n}\n\n/* Radix sort */\nfn radix_sort(nums: &mut [i32]) {\n    // Get the maximum element of the array, used to determine the maximum number of digits\n    let m = *nums.into_iter().max().unwrap();\n    // Traverse from the lowest to the highest digit\n    let mut exp = 1;\n    while exp <= m {\n        counting_sort_digit(nums, exp);\n        exp *= 10;\n    }\n}\n
      radix_sort.c
      /* Get the k-th digit of element num, where exp = 10^(k-1) */\nint digit(int num, int exp) {\n    // Passing exp instead of k can avoid repeated expensive exponentiation here\n    return (num / exp) % 10;\n}\n\n/* Counting sort (based on nums k-th digit) */\nvoid countingSortDigit(int nums[], int size, int exp) {\n    // Decimal digit range is 0~9, therefore need a bucket array of length 10\n    int *counter = (int *)malloc((sizeof(int) * 10));\n    memset(counter, 0, sizeof(int) * 10); // Initialize to 0 to support subsequent memory release\n    // Count the occurrence of digits 0~9\n    for (int i = 0; i < size; i++) {\n        // Get the k-th digit of nums[i], noted as d\n        int d = digit(nums[i], exp);\n        // Count the occurrence of digit d\n        counter[d]++;\n    }\n    // Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // Traverse in reverse, based on bucket statistics, place each element into res\n    int *res = (int *)malloc(sizeof(int) * size);\n    for (int i = size - 1; i >= 0; i--) {\n        int d = digit(nums[i], exp);\n        int j = counter[d] - 1; // Get the index j for d in the array\n        res[j] = nums[i];       // Place the current element at index j\n        counter[d]--;           // Decrease the count of d by 1\n    }\n    // Use result to overwrite the original array nums\n    for (int i = 0; i < size; i++) {\n        nums[i] = res[i];\n    }\n    // Free memory\n    free(res);\n    free(counter);\n}\n\n/* Radix sort */\nvoid radixSort(int nums[], int size) {\n    // Get the maximum element of the array, used to determine the maximum number of digits\n    int max = INT32_MIN;\n    for (int i = 0; i < size; i++) {\n        if (nums[i] > max) {\n            max = nums[i];\n        }\n    }\n    // Traverse from the lowest to the highest digit\n    for (int exp = 1; max >= exp; exp *= 10)\n        // Perform counting sort on the k-th digit of array elements\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // i.e., exp = 10^(k-1)\n        countingSortDigit(nums, size, exp);\n}\n
      radix_sort.kt
      /* Get the k-th digit of element num, where exp = 10^(k-1) */\nfun digit(num: Int, exp: Int): Int {\n    // Passing exp instead of k can avoid repeated expensive exponentiation here\n    return (num / exp) % 10\n}\n\n/* Counting sort (based on nums k-th digit) */\nfun countingSortDigit(nums: IntArray, exp: Int) {\n    // Decimal digit range is 0~9, therefore need a bucket array of length 10\n    val counter = IntArray(10)\n    val n = nums.size\n    // Count the occurrence of digits 0~9\n    for (i in 0..<n) {\n        val d = digit(nums[i], exp) // Get the k-th digit of nums[i], noted as d\n        counter[d]++                // Count the occurrence of digit d\n    }\n    // Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n    for (i in 1..9) {\n        counter[i] += counter[i - 1]\n    }\n    // Traverse in reverse, based on bucket statistics, place each element into res\n    val res = IntArray(n)\n    for (i in n - 1 downTo 0) {\n        val d = digit(nums[i], exp)\n        val j = counter[d] - 1 // Get the index j for d in the array\n        res[j] = nums[i]       // Place the current element at index j\n        counter[d]--           // Decrease the count of d by 1\n    }\n    // Use result to overwrite the original array nums\n    for (i in 0..<n)\n        nums[i] = res[i]\n}\n\n/* Radix sort */\nfun radixSort(nums: IntArray) {\n    // Get the maximum element of the array, used to determine the maximum number of digits\n    var m = Int.MIN_VALUE\n    for (num in nums) if (num > m) m = num\n    var exp = 1\n    // Traverse from the lowest to the highest digit\n    while (exp <= m) {\n        // Perform counting sort on the k-th digit of array elements\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // i.e., exp = 10^(k-1)\n        countingSortDigit(nums, exp)\n        exp *= 10\n    }\n}\n
      radix_sort.rb
      ### Get k-th digit of element num, where exp = 10^(k-1) ###\ndef digit(num, exp)\n  # Passing exp instead of k avoids expensive exponentiation calculations\n  (num / exp) % 10\nend\n\n### Counting sort (sort by k-th digit of nums) ###\ndef counting_sort_digit(nums, exp)\n  # Decimal digit range is 0~9, therefore need a bucket array of length 10\n  counter = Array.new(10, 0)\n  n = nums.length\n  # Count the occurrence of digits 0~9\n  for i in 0...n\n    d = digit(nums[i], exp) # Get the k-th digit of nums[i], noted as d\n    counter[d] += 1 # Count the occurrence of digit d\n  end\n  # Calculate prefix sum, converting \"occurrence count\" into \"array index\"\n  (1...10).each { |i| counter[i] += counter[i - 1] }\n  # Traverse in reverse, based on bucket statistics, place each element into res\n  res = Array.new(n, 0)\n  for i in (n - 1).downto(0)\n    d = digit(nums[i], exp)\n    j = counter[d] - 1 # Get the index j for d in the array\n    res[j] = nums[i] # Place the current element at index j\n    counter[d] -= 1 # Decrease the count of d by 1\n  end\n  # Use result to overwrite the original array nums\n  (0...n).each { |i| nums[i] = res[i] }\nend\n\n### Radix sort ###\ndef radix_sort(nums)\n  # Get the maximum element of the array, used to determine the maximum number of digits\n  m = nums.max\n  # Traverse from the lowest to the highest digit\n  exp = 1\n  while exp <= m\n    # Perform counting sort on the k-th digit of array elements\n    # k = 1 -> exp = 1\n    # k = 2 -> exp = 10\n    # i.e., exp = 10^(k-1)\n    counting_sort_digit(nums, exp)\n    exp *= 10\n  end\nend\n

      Why start sorting from the lowest digit?

      In successive sorting rounds, the result of a later round will override the result of an earlier round. For example, if the first round result is \\(a < b\\), while the second round result is \\(a > b\\), then the second round's result will replace the first round's result. Since higher-order digits have higher priority than lower-order digits, we should sort the lower digits first and then sort the higher digits.

      "},{"location":"chapter_sorting/radix_sort/#11102-algorithm-characteristics","title":"11.10.2 \u00a0 Algorithm Characteristics","text":"

      Compared to counting sort, radix sort is suitable for larger numerical ranges, but the prerequisite is that the data must be representable in a fixed number of digits, and the number of digits should not be too large. For example, floating-point numbers are not suitable for radix sort because their number of digits \\(k\\) may be too large, potentially leading to time complexity \\(O(nk) \\gg O(n^2)\\).

      • Time complexity of \\(O(nk)\\), non-adaptive sorting: Let the data volume be \\(n\\), the data be in base \\(d\\), and the maximum number of digits be \\(k\\). Then performing counting sort on a certain digit uses \\(O(n + d)\\) time, and sorting all \\(k\\) digits uses \\(O((n + d)k)\\) time. Typically, both \\(d\\) and \\(k\\) are relatively small, and the time complexity approaches \\(O(n)\\).
      • Space complexity of \\(O(n + d)\\), non-in-place sorting: Same as counting sort, radix sort requires auxiliary arrays res and counter of lengths \\(n\\) and \\(d\\).
      • Stable sorting: When counting sort is stable, radix sort is also stable; when counting sort is unstable, radix sort cannot guarantee obtaining correct sorting results.
      "},{"location":"chapter_sorting/selection_sort/","title":"11.2 \u00a0 Selection Sort","text":"

      Selection sort (selection sort) works very simply: it opens a loop, and in each round, selects the smallest element from the unsorted interval and places it at the end of the sorted interval.

      Assume the array has length \\(n\\). The algorithm flow of selection sort is shown in Figure 11-2.

      1. Initially, all elements are unsorted, i.e., the unsorted (index) interval is \\([0, n-1]\\).
      2. Select the smallest element in the interval \\([0, n-1]\\) and swap it with the element at index \\(0\\). After completion, the first element of the array is sorted.
      3. Select the smallest element in the interval \\([1, n-1]\\) and swap it with the element at index \\(1\\). After completion, the first 2 elements of the array are sorted.
      4. And so on. After \\(n - 1\\) rounds of selection and swapping, the first \\(n - 1\\) elements of the array are sorted.
      5. The only remaining element must be the largest element, requiring no sorting, so the array sorting is complete.
      <1><2><3><4><5><6><7><8><9><10><11>

      Figure 11-2 \u00a0 Selection sort steps

      In the code, we use \\(k\\) to record the smallest element within the unsorted interval:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby selection_sort.py
      def selection_sort(nums: list[int]):\n    \"\"\"Selection sort\"\"\"\n    n = len(nums)\n    # Outer loop: unsorted interval is [i, n-1]\n    for i in range(n - 1):\n        # Inner loop: find the smallest element within the unsorted interval\n        k = i\n        for j in range(i + 1, n):\n            if nums[j] < nums[k]:\n                k = j  # Record the index of the smallest element\n        # Swap the smallest element with the first element of the unsorted interval\n        nums[i], nums[k] = nums[k], nums[i]\n
      selection_sort.cpp
      /* Selection sort */\nvoid selectionSort(vector<int> &nums) {\n    int n = nums.size();\n    // Outer loop: unsorted interval is [i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // Inner loop: find the smallest element within the unsorted interval\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // Record the index of the smallest element\n        }\n        // Swap the smallest element with the first element of the unsorted interval\n        swap(nums[i], nums[k]);\n    }\n}\n
      selection_sort.java
      /* Selection sort */\nvoid selectionSort(int[] nums) {\n    int n = nums.length;\n    // Outer loop: unsorted interval is [i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // Inner loop: find the smallest element within the unsorted interval\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // Record the index of the smallest element\n        }\n        // Swap the smallest element with the first element of the unsorted interval\n        int temp = nums[i];\n        nums[i] = nums[k];\n        nums[k] = temp;\n    }\n}\n
      selection_sort.cs
      /* Selection sort */\nvoid SelectionSort(int[] nums) {\n    int n = nums.Length;\n    // Outer loop: unsorted interval is [i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // Inner loop: find the smallest element within the unsorted interval\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // Record the index of the smallest element\n        }\n        // Swap the smallest element with the first element of the unsorted interval\n        (nums[k], nums[i]) = (nums[i], nums[k]);\n    }\n}\n
      selection_sort.go
      /* Selection sort */\nfunc selectionSort(nums []int) {\n    n := len(nums)\n    // Outer loop: unsorted interval is [i, n-1]\n    for i := 0; i < n-1; i++ {\n        // Inner loop: find the smallest element within the unsorted interval\n        k := i\n        for j := i + 1; j < n; j++ {\n            if nums[j] < nums[k] {\n                // Record the index of the smallest element\n                k = j\n            }\n        }\n        // Swap the smallest element with the first element of the unsorted interval\n        nums[i], nums[k] = nums[k], nums[i]\n\n    }\n}\n
      selection_sort.swift
      /* Selection sort */\nfunc selectionSort(nums: inout [Int]) {\n    // Outer loop: unsorted interval is [i, n-1]\n    for i in nums.indices.dropLast() {\n        // Inner loop: find the smallest element within the unsorted interval\n        var k = i\n        for j in nums.indices.dropFirst(i + 1) {\n            if nums[j] < nums[k] {\n                k = j // Record the index of the smallest element\n            }\n        }\n        // Swap the smallest element with the first element of the unsorted interval\n        nums.swapAt(i, k)\n    }\n}\n
      selection_sort.js
      /* Selection sort */\nfunction selectionSort(nums) {\n    let n = nums.length;\n    // Outer loop: unsorted interval is [i, n-1]\n    for (let i = 0; i < n - 1; i++) {\n        // Inner loop: find the smallest element within the unsorted interval\n        let k = i;\n        for (let j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k]) {\n                k = j; // Record the index of the smallest element\n            }\n        }\n        // Swap the smallest element with the first element of the unsorted interval\n        [nums[i], nums[k]] = [nums[k], nums[i]];\n    }\n}\n
      selection_sort.ts
      /* Selection sort */\nfunction selectionSort(nums: number[]): void {\n    let n = nums.length;\n    // Outer loop: unsorted interval is [i, n-1]\n    for (let i = 0; i < n - 1; i++) {\n        // Inner loop: find the smallest element within the unsorted interval\n        let k = i;\n        for (let j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k]) {\n                k = j; // Record the index of the smallest element\n            }\n        }\n        // Swap the smallest element with the first element of the unsorted interval\n        [nums[i], nums[k]] = [nums[k], nums[i]];\n    }\n}\n
      selection_sort.dart
      /* Selection sort */\nvoid selectionSort(List<int> nums) {\n  int n = nums.length;\n  // Outer loop: unsorted interval is [i, n-1]\n  for (int i = 0; i < n - 1; i++) {\n    // Inner loop: find the smallest element within the unsorted interval\n    int k = i;\n    for (int j = i + 1; j < n; j++) {\n      if (nums[j] < nums[k]) k = j; // Record the index of the smallest element\n    }\n    // Swap the smallest element with the first element of the unsorted interval\n    int temp = nums[i];\n    nums[i] = nums[k];\n    nums[k] = temp;\n  }\n}\n
      selection_sort.rs
      /* Selection sort */\nfn selection_sort(nums: &mut [i32]) {\n    if nums.is_empty() {\n        return;\n    }\n    let n = nums.len();\n    // Outer loop: unsorted interval is [i, n-1]\n    for i in 0..n - 1 {\n        // Inner loop: find the smallest element within the unsorted interval\n        let mut k = i;\n        for j in i + 1..n {\n            if nums[j] < nums[k] {\n                k = j; // Record the index of the smallest element\n            }\n        }\n        // Swap the smallest element with the first element of the unsorted interval\n        nums.swap(i, k);\n    }\n}\n
      selection_sort.c
      /* Selection sort */\nvoid selectionSort(int nums[], int n) {\n    // Outer loop: unsorted interval is [i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // Inner loop: find the smallest element within the unsorted interval\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // Record the index of the smallest element\n        }\n        // Swap the smallest element with the first element of the unsorted interval\n        int temp = nums[i];\n        nums[i] = nums[k];\n        nums[k] = temp;\n    }\n}\n
      selection_sort.kt
      /* Selection sort */\nfun selectionSort(nums: IntArray) {\n    val n = nums.size\n    // Outer loop: unsorted interval is [i, n-1]\n    for (i in 0..<n - 1) {\n        var k = i\n        // Inner loop: find the smallest element within the unsorted interval\n        for (j in i + 1..<n) {\n            if (nums[j] < nums[k])\n                k = j // Record the index of the smallest element\n        }\n        // Swap the smallest element with the first element of the unsorted interval\n        val temp = nums[i]\n        nums[i] = nums[k]\n        nums[k] = temp\n    }\n}\n
      selection_sort.rb
      ### Selection sort ###\ndef selection_sort(nums)\n  n = nums.length\n  # Outer loop: unsorted interval is [i, n-1]\n  for i in 0...(n - 1)\n    # Inner loop: find the smallest element within the unsorted interval\n    k = i\n    for j in (i + 1)...n\n      if nums[j] < nums[k]\n        k = j # Record the index of the smallest element\n      end\n    end\n    # Swap the smallest element with the first element of the unsorted interval\n    nums[i], nums[k] = nums[k], nums[i]\n  end\nend\n
      "},{"location":"chapter_sorting/selection_sort/#1121-algorithm-characteristics","title":"11.2.1 \u00a0 Algorithm Characteristics","text":"
      • Time complexity of \\(O(n^2)\\), non-adaptive sorting: The outer loop has \\(n - 1\\) rounds in total. The length of the unsorted interval in the first round is \\(n\\), and the length of the unsorted interval in the last round is \\(2\\). That is, each round of the outer loop contains \\(n\\), \\(n - 1\\), \\(\\dots\\), \\(3\\), \\(2\\) inner loop iterations, summing to \\(\\frac{(n - 1)(n + 2)}{2}\\).
      • Space complexity of \\(O(1)\\), in-place sorting: Pointers \\(i\\) and \\(j\\) use a constant amount of extra space.
      • Non-stable sorting: As shown in Figure 11-3, element nums[i] may be swapped to the right of an element equal to it, causing a change in their relative order.

      Figure 11-3 \u00a0 Selection sort non-stability example

      "},{"location":"chapter_sorting/sorting_algorithm/","title":"11.1 \u00a0 Sorting Algorithm","text":"

      Sorting algorithm (sorting algorithm) is used to arrange a group of data in a specific order. Sorting algorithms have extensive applications because ordered data can usually be searched, analyzed, and processed more efficiently.

      As shown in Figure 11-1, data types in sorting algorithms can be integers, floating-point numbers, characters, or strings, etc. The sorting criterion can be set according to requirements, such as numerical size, character ASCII code order, or custom rules.

      Figure 11-1 \u00a0 Data type and criterion examples

      "},{"location":"chapter_sorting/sorting_algorithm/#1111-evaluation-dimensions","title":"11.1.1 \u00a0 Evaluation Dimensions","text":"

      Execution efficiency: We expect the time complexity of sorting algorithms to be as low as possible, with a smaller total number of operations (reducing the constant factor in time complexity). For large data volumes, execution efficiency is particularly important.

      In-place property: As the name implies, in-place sorting achieves sorting by operating directly on the original array without requiring additional auxiliary arrays, thus saving memory. Typically, in-place sorting involves fewer data movement operations and runs faster.

      Stability: Stable sorting ensures that the relative order of equal elements in the array does not change after sorting is completed.

      Stable sorting is a necessary condition for multi-level sorting scenarios. Suppose we have a table storing student information, where column 1 and column 2 are name and age, respectively. In this case, unstable sorting may cause the ordered nature of the input data to be lost:

      # Input Data Is Sorted by Name\n# (name, age)\n  ('A', 19)\n  ('B', 18)\n  ('C', 21)\n  ('D', 19)\n  ('E', 23)\n\n# Assuming We Use an Unstable Sorting Algorithm to Sort the List by Age,\n# In the Result, the Relative Positions of ('D', 19) and ('A', 19) Are Changed,\n# And the Property That the Input Data Is Sorted by Name Is Lost\n  ('B', 18)\n  ('D', 19)\n  ('A', 19)\n  ('C', 21)\n  ('E', 23)\n

      Adaptability: Adaptive sorting can utilize the existing order information in the input data to reduce the amount of computation, achieving better time efficiency. The best-case time complexity of adaptive sorting algorithms is typically better than the average time complexity.

      Comparison-based or not: Comparison-based sorting relies on comparison operators (\\(<\\), \\(=\\), \\(>\\)) to determine the relative order of elements, thereby sorting the entire array, with a theoretical optimal time complexity of \\(O(n \\log n)\\). Non-comparison sorting does not use comparison operators and can achieve a time complexity of \\(O(n)\\), but its versatility is relatively limited.

      "},{"location":"chapter_sorting/sorting_algorithm/#1112-ideal-sorting-algorithm","title":"11.1.2 \u00a0 Ideal Sorting Algorithm","text":"

      Fast execution, in-place, stable, adaptive, good versatility. Clearly, no sorting algorithm has been discovered to date that combines all of these characteristics. Therefore, when selecting a sorting algorithm, it is necessary to decide based on the specific characteristics of the data and the requirements of the problem.

      Next, we will learn about various sorting algorithms together and analyze the advantages and disadvantages of each sorting algorithm based on the above evaluation dimensions.

      "},{"location":"chapter_sorting/summary/","title":"11.11 \u00a0 Summary","text":""},{"location":"chapter_sorting/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • Bubble sort achieves sorting by swapping adjacent elements. By adding a flag to enable early return, we can optimize the best-case time complexity of bubble sort to \\(O(n)\\).
      • Insertion sort completes sorting by inserting elements from the unsorted interval into the correct position in the sorted interval each round. Although the time complexity of insertion sort is \\(O(n^2)\\), it is very popular in small data volume sorting tasks because it involves relatively few unit operations.
      • Quick sort is implemented based on sentinel partitioning operations. In sentinel partitioning, it is possible to select the worst pivot every time, causing the time complexity to degrade to \\(O(n^2)\\). Introducing median pivot or random pivot can reduce the probability of such degradation. By preferentially recursing on the shorter sub-interval, the recursion depth can be effectively reduced, optimizing the space complexity to \\(O(\\log n)\\).
      • Merge sort includes two phases: divide and merge, which typically embody the divide-and-conquer strategy. In merge sort, sorting an array requires creating auxiliary arrays, with a space complexity of \\(O(n)\\); however, the space complexity of sorting a linked list can be optimized to \\(O(1)\\).
      • Bucket sort consists of three steps: distributing data into buckets, sorting within buckets, and merging results. It also embodies the divide-and-conquer strategy and is suitable for very large data volumes. The key to bucket sort is distributing data evenly.
      • Counting sort is a special case of bucket sort, which achieves sorting by counting the number of occurrences of data. Counting sort is suitable for situations where the data volume is large but the data range is limited, and requires that data can be converted to positive integers.
      • Radix sort achieves data sorting by sorting digit by digit, requiring that data can be represented as fixed-digit numbers.
      • Overall, we hope to find a sorting algorithm that is efficient, stable, in-place, and adaptive, with good versatility. However, just like other data structures and algorithms, no sorting algorithm has been found so far that simultaneously possesses all these characteristics. In practical applications, we need to select the appropriate sorting algorithm based on the specific characteristics of the data.
      • Figure 11-19 compares mainstream sorting algorithms in terms of efficiency, stability, in-place property, and adaptability.

      Figure 11-19 \u00a0 Sorting algorithm comparison

      "},{"location":"chapter_sorting/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

      Q: In what situations is the stability of sorting algorithms necessary?

      In reality, we may sort based on a certain attribute of objects. For example, students have two attributes: name and height. We want to implement multi-level sorting: first sort by name to get (A, 180) (B, 185) (C, 170) (D, 170); then sort by height. Because the sorting algorithm is unstable, we may get (D, 170) (C, 170) (A, 180) (B, 185).

      It can be seen that the positions of students D and C have been swapped, and the orderliness of names has been disrupted, which is something we don't want to see.

      Q: Can the order of \"searching from right to left\" and \"searching from left to right\" in sentinel partitioning be swapped?

      No. When we use the leftmost element as the pivot, we must first \"search from right to left\" and then \"search from left to right\". This conclusion is somewhat counterintuitive; let's analyze the reason.

      The last step of sentinel partitioning partition() is to swap nums[left] and nums[i]. After the swap is complete, the elements to the left of the pivot are all <= the pivot, which requires that nums[left] >= nums[i] must hold before the last swap. Suppose we first \"search from left to right\", then if we cannot find an element larger than the pivot, we will exit the loop when i == j, at which point it may be that nums[j] == nums[i] > nums[left]. In other words, the last swap operation will swap an element larger than the pivot to the leftmost end of the array, causing sentinel partitioning to fail.

      For example, given the array [0, 0, 0, 0, 1], if we first \"search from left to right\", the array after sentinel partitioning is [1, 0, 0, 0, 0], which is incorrect.

      Thinking deeper, if we select nums[right] as the pivot, then it's exactly the opposite - we must first \"search from left to right\".

      Q: Regarding the optimization of recursion depth in quick sort, why can selecting the shorter array ensure that the recursion depth does not exceed \\(\\log n\\)?

      The recursion depth is the number of currently unreturned recursive methods. Each round of sentinel partitioning divides the original array into two sub-arrays. After recursion depth optimization, the length of the sub-array to be recursively processed is at most half of the original array length. Assuming the worst case is always half the length, the final recursion depth will be \\(\\log n\\).

      Reviewing the original quick sort, we may continuously recurse on the longer array. In the worst case, it would be \\(n\\), \\(n - 1\\), \\(\\dots\\), \\(2\\), \\(1\\), with a recursion depth of \\(n\\). Recursion depth optimization can avoid this situation.

      Q: When all elements in the array are equal, is the time complexity of quick sort \\(O(n^2)\\)? How should this degenerate case be handled?

      Yes. For this situation, consider partitioning the array into three parts through sentinel partitioning: less than, equal to, and greater than the pivot. Only recursively process the less than and greater than parts. Under this method, an array where all input elements are equal can complete sorting in just one round of sentinel partitioning.

      Q: Why is the worst-case time complexity of bucket sort \\(O(n^2)\\)?

      In the worst case, all elements are distributed into the same bucket. If we use an \\(O(n^2)\\) algorithm to sort these elements, the time complexity will be \\(O(n^2)\\).

      "},{"location":"chapter_stack_and_queue/","title":"Chapter 5. \u00a0 Stack and Queue","text":"

      Abstract

      Stacks are like stacking cats, while queues are like cats lining up.

      They represent LIFO (Last In First Out) and FIFO (First In First Out) logic, respectively.

      "},{"location":"chapter_stack_and_queue/#chapter-contents","title":"Chapter contents","text":"
      • 5.1 \u00a0 Stack
      • 5.2 \u00a0 Queue
      • 5.3 \u00a0 Double-Ended Queue
      • 5.4 \u00a0 Summary
      "},{"location":"chapter_stack_and_queue/deque/","title":"5.3 \u00a0 Deque","text":"

      In a queue, we can only remove elements from the front or add elements at the rear. As shown in Figure 5-7, a double-ended queue (deque) provides greater flexibility, allowing the addition or removal of elements at both the front and rear.

      Figure 5-7 \u00a0 Operations of deque

      "},{"location":"chapter_stack_and_queue/deque/#531-common-deque-operations","title":"5.3.1 \u00a0 Common Deque Operations","text":"

      The common operations on a deque are shown in Table 5-3. The specific method names depend on the programming language used.

      Table 5-3 \u00a0 Efficiency of Deque Operations

      Method Description Time Complexity push_first() Add element to front \\(O(1)\\) push_last() Add element to rear \\(O(1)\\) pop_first() Remove front element \\(O(1)\\) pop_last() Remove rear element \\(O(1)\\) peek_first() Access front element \\(O(1)\\) peek_last() Access rear element \\(O(1)\\)

      Similarly, we can directly use the deque classes already implemented in programming languages:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby deque.py
      from collections import deque\n\n# Initialize deque\ndeq: deque[int] = deque()\n\n# Enqueue elements\ndeq.append(2)      # Add to rear\ndeq.append(5)\ndeq.append(4)\ndeq.appendleft(3)  # Add to front\ndeq.appendleft(1)\n\n# Access elements\nfront: int = deq[0]  # Front element\nrear: int = deq[-1]  # Rear element\n\n# Dequeue elements\npop_front: int = deq.popleft()  # Front element dequeue\npop_rear: int = deq.pop()       # Rear element dequeue\n\n# Get deque length\nsize: int = len(deq)\n\n# Check if deque is empty\nis_empty: bool = len(deq) == 0\n
      deque.cpp
      /* Initialize deque */\ndeque<int> deque;\n\n/* Enqueue elements */\ndeque.push_back(2);   // Add to rear\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3);  // Add to front\ndeque.push_front(1);\n\n/* Access elements */\nint front = deque.front(); // Front element\nint back = deque.back();   // Rear element\n\n/* Dequeue elements */\ndeque.pop_front();  // Front element dequeue\ndeque.pop_back();   // Rear element dequeue\n\n/* Get deque length */\nint size = deque.size();\n\n/* Check if deque is empty */\nbool empty = deque.empty();\n
      deque.java
      /* Initialize deque */\nDeque<Integer> deque = new LinkedList<>();\n\n/* Enqueue elements */\ndeque.offerLast(2);   // Add to rear\ndeque.offerLast(5);\ndeque.offerLast(4);\ndeque.offerFirst(3);  // Add to front\ndeque.offerFirst(1);\n\n/* Access elements */\nint peekFirst = deque.peekFirst();  // Front element\nint peekLast = deque.peekLast();    // Rear element\n\n/* Dequeue elements */\nint popFirst = deque.pollFirst();  // Front element dequeue\nint popLast = deque.pollLast();    // Rear element dequeue\n\n/* Get deque length */\nint size = deque.size();\n\n/* Check if deque is empty */\nboolean isEmpty = deque.isEmpty();\n
      deque.cs
      /* Initialize deque */\n// In C#, use LinkedList as a deque\nLinkedList<int> deque = new();\n\n/* Enqueue elements */\ndeque.AddLast(2);   // Add to rear\ndeque.AddLast(5);\ndeque.AddLast(4);\ndeque.AddFirst(3);  // Add to front\ndeque.AddFirst(1);\n\n/* Access elements */\nint peekFirst = deque.First.Value;  // Front element\nint peekLast = deque.Last.Value;    // Rear element\n\n/* Dequeue elements */\ndeque.RemoveFirst();  // Front element dequeue\ndeque.RemoveLast();   // Rear element dequeue\n\n/* Get deque length */\nint size = deque.Count;\n\n/* Check if deque is empty */\nbool isEmpty = deque.Count == 0;\n
      deque_test.go
      /* Initialize deque */\n// In Go, use list as a deque\ndeque := list.New()\n\n/* Enqueue elements */\ndeque.PushBack(2)      // Add to rear\ndeque.PushBack(5)\ndeque.PushBack(4)\ndeque.PushFront(3)     // Add to front\ndeque.PushFront(1)\n\n/* Access elements */\nfront := deque.Front() // Front element\nrear := deque.Back()   // Rear element\n\n/* Dequeue elements */\ndeque.Remove(front)    // Front element dequeue\ndeque.Remove(rear)     // Rear element dequeue\n\n/* Get deque length */\nsize := deque.Len()\n\n/* Check if deque is empty */\nisEmpty := deque.Len() == 0\n
      deque.swift
      /* Initialize deque */\n// Swift does not have a built-in deque class, can use Array as a deque\nvar deque: [Int] = []\n\n/* Enqueue elements */\ndeque.append(2) // Add to rear\ndeque.append(5)\ndeque.append(4)\ndeque.insert(3, at: 0) // Add to front\ndeque.insert(1, at: 0)\n\n/* Access elements */\nlet peekFirst = deque.first! // Front element\nlet peekLast = deque.last! // Rear element\n\n/* Dequeue elements */\n// When using Array simulation, popFirst has O(n) complexity\nlet popFirst = deque.removeFirst() // Front element dequeue\nlet popLast = deque.removeLast() // Rear element dequeue\n\n/* Get deque length */\nlet size = deque.count\n\n/* Check if deque is empty */\nlet isEmpty = deque.isEmpty\n
      deque.js
      /* Initialize deque */\n// JavaScript does not have a built-in deque, can only use Array as a deque\nconst deque = [];\n\n/* Enqueue elements */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// Please note that since it's an array, unshift() has O(n) time complexity\ndeque.unshift(3);\ndeque.unshift(1);\n\n/* Access elements */\nconst peekFirst = deque[0];\nconst peekLast = deque[deque.length - 1];\n\n/* Dequeue elements */\n// Please note that since it's an array, shift() has O(n) time complexity\nconst popFront = deque.shift();\nconst popBack = deque.pop();\n\n/* Get deque length */\nconst size = deque.length;\n\n/* Check if deque is empty */\nconst isEmpty = size === 0;\n
      deque.ts
      /* Initialize deque */\n// TypeScript does not have a built-in deque, can only use Array as a deque\nconst deque: number[] = [];\n\n/* Enqueue elements */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// Please note that since it's an array, unshift() has O(n) time complexity\ndeque.unshift(3);\ndeque.unshift(1);\n\n/* Access elements */\nconst peekFirst: number = deque[0];\nconst peekLast: number = deque[deque.length - 1];\n\n/* Dequeue elements */\n// Please note that since it's an array, shift() has O(n) time complexity\nconst popFront: number = deque.shift() as number;\nconst popBack: number = deque.pop() as number;\n\n/* Get deque length */\nconst size: number = deque.length;\n\n/* Check if deque is empty */\nconst isEmpty: boolean = size === 0;\n
      deque.dart
      /* Initialize deque */\n// In Dart, Queue is defined as a deque\nQueue<int> deque = Queue<int>();\n\n/* Enqueue elements */\ndeque.addLast(2);  // Add to rear\ndeque.addLast(5);\ndeque.addLast(4);\ndeque.addFirst(3); // Add to front\ndeque.addFirst(1);\n\n/* Access elements */\nint peekFirst = deque.first; // Front element\nint peekLast = deque.last;   // Rear element\n\n/* Dequeue elements */\nint popFirst = deque.removeFirst(); // Front element dequeue\nint popLast = deque.removeLast();   // Rear element dequeue\n\n/* Get deque length */\nint size = deque.length;\n\n/* Check if deque is empty */\nbool isEmpty = deque.isEmpty;\n
      deque.rs
      /* Initialize deque */\nlet mut deque: VecDeque<u32> = VecDeque::new();\n\n/* Enqueue elements */\ndeque.push_back(2);  // Add to rear\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3); // Add to front\ndeque.push_front(1);\n\n/* Access elements */\nif let Some(front) = deque.front() { // Front element\n}\nif let Some(rear) = deque.back() {   // Rear element\n}\n\n/* Dequeue elements */\nif let Some(pop_front) = deque.pop_front() { // Front element dequeue\n}\nif let Some(pop_rear) = deque.pop_back() {   // Rear element dequeue\n}\n\n/* Get deque length */\nlet size = deque.len();\n\n/* Check if deque is empty */\nlet is_empty = deque.is_empty();\n
      deque.c
      // C does not provide a built-in deque\n
      deque.kt
      /* Initialize deque */\nval deque = LinkedList<Int>()\n\n/* Enqueue elements */\ndeque.offerLast(2)  // Add to rear\ndeque.offerLast(5)\ndeque.offerLast(4)\ndeque.offerFirst(3) // Add to front\ndeque.offerFirst(1)\n\n/* Access elements */\nval peekFirst = deque.peekFirst() // Front element\nval peekLast = deque.peekLast()   // Rear element\n\n/* Dequeue elements */\nval popFirst = deque.pollFirst() // Front element dequeue\nval popLast = deque.pollLast()   // Rear element dequeue\n\n/* Get deque length */\nval size = deque.size\n\n/* Check if deque is empty */\nval isEmpty = deque.isEmpty()\n
      deque.rb
      # Initialize deque\n# Ruby does not have a built-in deque, can only use Array as a deque\ndeque = []\n\n# Enqueue elements\ndeque << 2\ndeque << 5\ndeque << 4\n# Please note that since it's an array, Array#unshift has O(n) time complexity\ndeque.unshift(3)\ndeque.unshift(1)\n\n# Access elements\npeek_first = deque.first\npeek_last = deque.last\n\n# Dequeue elements\n# Please note that since it's an array, Array#shift has O(n) time complexity\npop_front = deque.shift\npop_back = deque.pop\n\n# Get deque length\nsize = deque.length\n\n# Check if deque is empty\nis_empty = size.zero?\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_stack_and_queue/deque/#532-deque-implementation","title":"5.3.2 \u00a0 Deque Implementation *","text":"

      The implementation of a deque is similar to that of a queue. You can choose either a linked list or an array as the underlying data structure.

      "},{"location":"chapter_stack_and_queue/deque/#1-doubly-linked-list-implementation","title":"1. \u00a0 Doubly Linked List Implementation","text":"

      Reviewing the previous section, we used a regular singly linked list to implement a queue because it conveniently allows deleting the head node (corresponding to dequeue) and adding new nodes after the tail node (corresponding to enqueue).

      For a deque, both the front and rear can perform enqueue and dequeue operations. In other words, a deque needs to implement operations in the opposite direction as well. For this reason, we use a \"doubly linked list\" as the underlying data structure for the deque.

      As shown in Figure 5-8, we treat the head and tail nodes of the doubly linked list as the front and rear of the deque, implementing functionality to add and remove nodes at both ends.

      LinkedListDequepush_last()push_first()pop_last()pop_first()

      Figure 5-8 \u00a0 Enqueue and dequeue operations in linked list implementation of deque

      The implementation code is shown below:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linkedlist_deque.py
      class ListNode:\n    \"\"\"Doubly linked list node\"\"\"\n\n    def __init__(self, val: int):\n        \"\"\"Constructor\"\"\"\n        self.val: int = val\n        self.next: ListNode | None = None  # Successor node reference\n        self.prev: ListNode | None = None  # Predecessor node reference\n\nclass LinkedListDeque:\n    \"\"\"Double-ended queue based on doubly linked list implementation\"\"\"\n\n    def __init__(self):\n        \"\"\"Constructor\"\"\"\n        self._front: ListNode | None = None  # Head node front\n        self._rear: ListNode | None = None  # Tail node rear\n        self._size: int = 0  # Length of the double-ended queue\n\n    def size(self) -> int:\n        \"\"\"Get the length of the double-ended queue\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"Check if the double-ended queue is empty\"\"\"\n        return self._size == 0\n\n    def push(self, num: int, is_front: bool):\n        \"\"\"Enqueue operation\"\"\"\n        node = ListNode(num)\n        # If the linked list is empty, make both front and rear point to node\n        if self.is_empty():\n            self._front = self._rear = node\n        # Front of the queue enqueue operation\n        elif is_front:\n            # Add node to the head of the linked list\n            self._front.prev = node\n            node.next = self._front\n            self._front = node  # Update head node\n        # Rear of the queue enqueue operation\n        else:\n            # Add node to the tail of the linked list\n            self._rear.next = node\n            node.prev = self._rear\n            self._rear = node  # Update tail node\n        self._size += 1  # Update queue length\n\n    def push_first(self, num: int):\n        \"\"\"Front of the queue enqueue\"\"\"\n        self.push(num, True)\n\n    def push_last(self, num: int):\n        \"\"\"Rear of the queue enqueue\"\"\"\n        self.push(num, False)\n\n    def pop(self, is_front: bool) -> int:\n        \"\"\"Dequeue operation\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Double-ended queue is empty\")\n        # Front of the queue dequeue operation\n        if is_front:\n            val: int = self._front.val  # Temporarily store head node value\n            # Delete head node\n            fnext: ListNode | None = self._front.next\n            if fnext is not None:\n                fnext.prev = None\n                self._front.next = None\n            self._front = fnext  # Update head node\n        # Rear of the queue dequeue operation\n        else:\n            val: int = self._rear.val  # Temporarily store tail node value\n            # Delete tail node\n            rprev: ListNode | None = self._rear.prev\n            if rprev is not None:\n                rprev.next = None\n                self._rear.prev = None\n            self._rear = rprev  # Update tail node\n        self._size -= 1  # Update queue length\n        return val\n\n    def pop_first(self) -> int:\n        \"\"\"Front of the queue dequeue\"\"\"\n        return self.pop(True)\n\n    def pop_last(self) -> int:\n        \"\"\"Rear of the queue dequeue\"\"\"\n        return self.pop(False)\n\n    def peek_first(self) -> int:\n        \"\"\"Access front of the queue element\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Double-ended queue is empty\")\n        return self._front.val\n\n    def peek_last(self) -> int:\n        \"\"\"Access rear of the queue element\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Double-ended queue is empty\")\n        return self._rear.val\n\n    def to_array(self) -> list[int]:\n        \"\"\"Return array for printing\"\"\"\n        node = self._front\n        res = [0] * self.size()\n        for i in range(self.size()):\n            res[i] = node.val\n            node = node.next\n        return res\n
      linkedlist_deque.cpp
      /* Doubly linked list node */\nstruct DoublyListNode {\n    int val;              // Node value\n    DoublyListNode *next; // Successor node pointer\n    DoublyListNode *prev; // Predecessor node pointer\n    DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {\n    }\n};\n\n/* Double-ended queue based on doubly linked list implementation */\nclass LinkedListDeque {\n  private:\n    DoublyListNode *front, *rear; // Head node front, tail node rear\n    int queSize = 0;              // Length of the double-ended queue\n\n  public:\n    /* Constructor */\n    LinkedListDeque() : front(nullptr), rear(nullptr) {\n    }\n\n    /* Destructor */\n    ~LinkedListDeque() {\n        // Traverse linked list to delete nodes and free memory\n        DoublyListNode *pre, *cur = front;\n        while (cur != nullptr) {\n            pre = cur;\n            cur = cur->next;\n            delete pre;\n        }\n    }\n\n    /* Get the length of the double-ended queue */\n    int size() {\n        return queSize;\n    }\n\n    /* Check if the double-ended queue is empty */\n    bool isEmpty() {\n        return size() == 0;\n    }\n\n    /* Enqueue operation */\n    void push(int num, bool isFront) {\n        DoublyListNode *node = new DoublyListNode(num);\n        // If the linked list is empty, make both front and rear point to node\n        if (isEmpty())\n            front = rear = node;\n        // Front of the queue enqueue operation\n        else if (isFront) {\n            // Add node to the head of the linked list\n            front->prev = node;\n            node->next = front;\n            front = node; // Update head node\n        // Rear of the queue enqueue operation\n        } else {\n            // Add node to the tail of the linked list\n            rear->next = node;\n            node->prev = rear;\n            rear = node; // Update tail node\n        }\n        queSize++; // Update queue length\n    }\n\n    /* Front of the queue enqueue */\n    void pushFirst(int num) {\n        push(num, true);\n    }\n\n    /* Rear of the queue enqueue */\n    void pushLast(int num) {\n        push(num, false);\n    }\n\n    /* Dequeue operation */\n    int pop(bool isFront) {\n        if (isEmpty())\n            throw out_of_range(\"Queue is empty\");\n        int val;\n        // Temporarily store head node value\n        if (isFront) {\n            val = front->val; // Delete head node\n            // Delete head node\n            DoublyListNode *fNext = front->next;\n            if (fNext != nullptr) {\n                fNext->prev = nullptr;\n                front->next = nullptr;\n            }\n            delete front;\n            front = fNext; // Update head node\n        // Temporarily store tail node value\n        } else {\n            val = rear->val; // Delete tail node\n            // Update tail node\n            DoublyListNode *rPrev = rear->prev;\n            if (rPrev != nullptr) {\n                rPrev->next = nullptr;\n                rear->prev = nullptr;\n            }\n            delete rear;\n            rear = rPrev; // Update tail node\n        }\n        queSize--; // Update queue length\n        return val;\n    }\n\n    /* Rear of the queue dequeue */\n    int popFirst() {\n        return pop(true);\n    }\n\n    /* Access rear of the queue element */\n    int popLast() {\n        return pop(false);\n    }\n\n    /* Return list for printing */\n    int peekFirst() {\n        if (isEmpty())\n            throw out_of_range(\"Deque is empty\");\n        return front->val;\n    }\n\n    /* Driver Code */\n    int peekLast() {\n        if (isEmpty())\n            throw out_of_range(\"Deque is empty\");\n        return rear->val;\n    }\n\n    /* Return array for printing */\n    vector<int> toVector() {\n        DoublyListNode *node = front;\n        vector<int> res(size());\n        for (int i = 0; i < res.size(); i++) {\n            res[i] = node->val;\n            node = node->next;\n        }\n        return res;\n    }\n};\n
      linkedlist_deque.java
      /* Doubly linked list node */\nclass ListNode {\n    int val; // Node value\n    ListNode next; // Successor node reference\n    ListNode prev; // Predecessor node reference\n\n    ListNode(int val) {\n        this.val = val;\n        prev = next = null;\n    }\n}\n\n/* Double-ended queue based on doubly linked list implementation */\nclass LinkedListDeque {\n    private ListNode front, rear; // Head node front, tail node rear\n    private int queSize = 0; // Length of the double-ended queue\n\n    public LinkedListDeque() {\n        front = rear = null;\n    }\n\n    /* Get the length of the double-ended queue */\n    public int size() {\n        return queSize;\n    }\n\n    /* Check if the double-ended queue is empty */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* Enqueue operation */\n    private void push(int num, boolean isFront) {\n        ListNode node = new ListNode(num);\n        // If the linked list is empty, make both front and rear point to node\n        if (isEmpty())\n            front = rear = node;\n        // Front of the queue enqueue operation\n        else if (isFront) {\n            // Add node to the head of the linked list\n            front.prev = node;\n            node.next = front;\n            front = node; // Update head node\n        // Rear of the queue enqueue operation\n        } else {\n            // Add node to the tail of the linked list\n            rear.next = node;\n            node.prev = rear;\n            rear = node; // Update tail node\n        }\n        queSize++; // Update queue length\n    }\n\n    /* Front of the queue enqueue */\n    public void pushFirst(int num) {\n        push(num, true);\n    }\n\n    /* Rear of the queue enqueue */\n    public void pushLast(int num) {\n        push(num, false);\n    }\n\n    /* Dequeue operation */\n    private int pop(boolean isFront) {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        int val;\n        // Temporarily store head node value\n        if (isFront) {\n            val = front.val; // Delete head node\n            // Delete head node\n            ListNode fNext = front.next;\n            if (fNext != null) {\n                fNext.prev = null;\n                front.next = null;\n            }\n            front = fNext; // Update head node\n        // Temporarily store tail node value\n        } else {\n            val = rear.val; // Delete tail node\n            // Update tail node\n            ListNode rPrev = rear.prev;\n            if (rPrev != null) {\n                rPrev.next = null;\n                rear.prev = null;\n            }\n            rear = rPrev; // Update tail node\n        }\n        queSize--; // Update queue length\n        return val;\n    }\n\n    /* Rear of the queue dequeue */\n    public int popFirst() {\n        return pop(true);\n    }\n\n    /* Access rear of the queue element */\n    public int popLast() {\n        return pop(false);\n    }\n\n    /* Return list for printing */\n    public int peekFirst() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return front.val;\n    }\n\n    /* Driver Code */\n    public int peekLast() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return rear.val;\n    }\n\n    /* Return array for printing */\n    public int[] toArray() {\n        ListNode node = front;\n        int[] res = new int[size()];\n        for (int i = 0; i < res.length; i++) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
      linkedlist_deque.cs
      /* Doubly linked list node */\nclass ListNode(int val) {\n    public int val = val;       // Node value\n    public ListNode? next = null; // Successor node reference\n    public ListNode? prev = null; // Predecessor node reference\n}\n\n/* Double-ended queue based on doubly linked list implementation */\nclass LinkedListDeque {\n    ListNode? front, rear; // Head node front, tail node rear\n    int queSize = 0;      // Length of the double-ended queue\n\n    public LinkedListDeque() {\n        front = null;\n        rear = null;\n    }\n\n    /* Get the length of the double-ended queue */\n    public int Size() {\n        return queSize;\n    }\n\n    /* Check if the double-ended queue is empty */\n    public bool IsEmpty() {\n        return Size() == 0;\n    }\n\n    /* Enqueue operation */\n    void Push(int num, bool isFront) {\n        ListNode node = new(num);\n        // If the linked list is empty, make both front and rear point to node\n        if (IsEmpty()) {\n            front = node;\n            rear = node;\n        }\n        // Front of the queue enqueue operation\n        else if (isFront) {\n            // Add node to the head of the linked list\n            front!.prev = node;\n            node.next = front;\n            front = node; // Update head node\n        }\n        // Rear of the queue enqueue operation\n        else {\n            // Add node to the tail of the linked list\n            rear!.next = node;\n            node.prev = rear;\n            rear = node;  // Update tail node\n        }\n\n        queSize++; // Update queue length\n    }\n\n    /* Front of the queue enqueue */\n    public void PushFirst(int num) {\n        Push(num, true);\n    }\n\n    /* Rear of the queue enqueue */\n    public void PushLast(int num) {\n        Push(num, false);\n    }\n\n    /* Dequeue operation */\n    int? Pop(bool isFront) {\n        if (IsEmpty())\n            throw new Exception();\n        int? val;\n        // Temporarily store head node value\n        if (isFront) {\n            val = front?.val; // Delete head node\n            // Delete head node\n            ListNode? fNext = front?.next;\n            if (fNext != null) {\n                fNext.prev = null;\n                front!.next = null;\n            }\n            front = fNext;   // Update head node\n        }\n        // Temporarily store tail node value\n        else {\n            val = rear?.val;  // Delete tail node\n            // Update tail node\n            ListNode? rPrev = rear?.prev;\n            if (rPrev != null) {\n                rPrev.next = null;\n                rear!.prev = null;\n            }\n            rear = rPrev;    // Update tail node\n        }\n\n        queSize--; // Update queue length\n        return val;\n    }\n\n    /* Rear of the queue dequeue */\n    public int? PopFirst() {\n        return Pop(true);\n    }\n\n    /* Access rear of the queue element */\n    public int? PopLast() {\n        return Pop(false);\n    }\n\n    /* Return list for printing */\n    public int? PeekFirst() {\n        if (IsEmpty())\n            throw new Exception();\n        return front?.val;\n    }\n\n    /* Driver Code */\n    public int? PeekLast() {\n        if (IsEmpty())\n            throw new Exception();\n        return rear?.val;\n    }\n\n    /* Return array for printing */\n    public int?[] ToArray() {\n        ListNode? node = front;\n        int?[] res = new int?[Size()];\n        for (int i = 0; i < res.Length; i++) {\n            res[i] = node?.val;\n            node = node?.next;\n        }\n\n        return res;\n    }\n}\n
      linkedlist_deque.go
      /* Double-ended queue based on doubly linked list implementation */\ntype linkedListDeque struct {\n    // Use built-in package list\n    data *list.List\n}\n\n/* Initialize deque */\nfunc newLinkedListDeque() *linkedListDeque {\n    return &linkedListDeque{\n        data: list.New(),\n    }\n}\n\n/* Front element enqueue */\nfunc (s *linkedListDeque) pushFirst(value any) {\n    s.data.PushFront(value)\n}\n\n/* Rear element enqueue */\nfunc (s *linkedListDeque) pushLast(value any) {\n    s.data.PushBack(value)\n}\n\n/* Check if the double-ended queue is empty */\nfunc (s *linkedListDeque) popFirst() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Front()\n    s.data.Remove(e)\n    return e.Value\n}\n\n/* Rear element dequeue */\nfunc (s *linkedListDeque) popLast() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Back()\n    s.data.Remove(e)\n    return e.Value\n}\n\n/* Return list for printing */\nfunc (s *linkedListDeque) peekFirst() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Front()\n    return e.Value\n}\n\n/* Driver Code */\nfunc (s *linkedListDeque) peekLast() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Back()\n    return e.Value\n}\n\n/* Get the length of the queue */\nfunc (s *linkedListDeque) size() int {\n    return s.data.Len()\n}\n\n/* Check if the queue is empty */\nfunc (s *linkedListDeque) isEmpty() bool {\n    return s.data.Len() == 0\n}\n\n/* Get List for printing */\nfunc (s *linkedListDeque) toList() *list.List {\n    return s.data\n}\n
      linkedlist_deque.swift
      /* Doubly linked list node */\nclass ListNode {\n    var val: Int // Node value\n    var next: ListNode? // Successor node reference\n    weak var prev: ListNode? // Predecessor node reference\n\n    init(val: Int) {\n        self.val = val\n    }\n}\n\n/* Double-ended queue based on doubly linked list implementation */\nclass LinkedListDeque {\n    private var front: ListNode? // Head node front\n    private var rear: ListNode? // Tail node rear\n    private var _size: Int // Length of the double-ended queue\n\n    init() {\n        _size = 0\n    }\n\n    /* Get the length of the double-ended queue */\n    func size() -> Int {\n        _size\n    }\n\n    /* Check if the double-ended queue is empty */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* Enqueue operation */\n    private func push(num: Int, isFront: Bool) {\n        let node = ListNode(val: num)\n        // If the linked list is empty, make both front and rear point to node\n        if isEmpty() {\n            front = node\n            rear = node\n        }\n        // Front of the queue enqueue operation\n        else if isFront {\n            // Add node to the head of the linked list\n            front?.prev = node\n            node.next = front\n            front = node // Update head node\n        }\n        // Rear of the queue enqueue operation\n        else {\n            // Add node to the tail of the linked list\n            rear?.next = node\n            node.prev = rear\n            rear = node // Update tail node\n        }\n        _size += 1 // Update queue length\n    }\n\n    /* Front of the queue enqueue */\n    func pushFirst(num: Int) {\n        push(num: num, isFront: true)\n    }\n\n    /* Rear of the queue enqueue */\n    func pushLast(num: Int) {\n        push(num: num, isFront: false)\n    }\n\n    /* Dequeue operation */\n    private func pop(isFront: Bool) -> Int {\n        if isEmpty() {\n            fatalError(\"Deque is empty\")\n        }\n        let val: Int\n        // Temporarily store head node value\n        if isFront {\n            val = front!.val // Delete head node\n            // Delete head node\n            let fNext = front?.next\n            if fNext != nil {\n                fNext?.prev = nil\n                front?.next = nil\n            }\n            front = fNext // Update head node\n        }\n        // Temporarily store tail node value\n        else {\n            val = rear!.val // Delete tail node\n            // Update tail node\n            let rPrev = rear?.prev\n            if rPrev != nil {\n                rPrev?.next = nil\n                rear?.prev = nil\n            }\n            rear = rPrev // Update tail node\n        }\n        _size -= 1 // Update queue length\n        return val\n    }\n\n    /* Rear of the queue dequeue */\n    func popFirst() -> Int {\n        pop(isFront: true)\n    }\n\n    /* Access rear of the queue element */\n    func popLast() -> Int {\n        pop(isFront: false)\n    }\n\n    /* Return list for printing */\n    func peekFirst() -> Int {\n        if isEmpty() {\n            fatalError(\"Deque is empty\")\n        }\n        return front!.val\n    }\n\n    /* Driver Code */\n    func peekLast() -> Int {\n        if isEmpty() {\n            fatalError(\"Deque is empty\")\n        }\n        return rear!.val\n    }\n\n    /* Return array for printing */\n    func toArray() -> [Int] {\n        var node = front\n        var res = Array(repeating: 0, count: size())\n        for i in res.indices {\n            res[i] = node!.val\n            node = node?.next\n        }\n        return res\n    }\n}\n
      linkedlist_deque.js
      /* Doubly linked list node */\nclass ListNode {\n    prev; // Predecessor node reference (pointer)\n    next; // Successor node reference (pointer)\n    val; // Node value\n\n    constructor(val) {\n        this.val = val;\n        this.next = null;\n        this.prev = null;\n    }\n}\n\n/* Double-ended queue based on doubly linked list implementation */\nclass LinkedListDeque {\n    #front; // Head node front\n    #rear; // Tail node rear\n    #queSize; // Length of the double-ended queue\n\n    constructor() {\n        this.#front = null;\n        this.#rear = null;\n        this.#queSize = 0;\n    }\n\n    /* Rear of the queue enqueue operation */\n    pushLast(val) {\n        const node = new ListNode(val);\n        // If the linked list is empty, make both front and rear point to node\n        if (this.#queSize === 0) {\n            this.#front = node;\n            this.#rear = node;\n        } else {\n            // Add node to the tail of the linked list\n            this.#rear.next = node;\n            node.prev = this.#rear;\n            this.#rear = node; // Update tail node\n        }\n        this.#queSize++;\n    }\n\n    /* Front of the queue enqueue operation */\n    pushFirst(val) {\n        const node = new ListNode(val);\n        // If the linked list is empty, make both front and rear point to node\n        if (this.#queSize === 0) {\n            this.#front = node;\n            this.#rear = node;\n        } else {\n            // Add node to the head of the linked list\n            this.#front.prev = node;\n            node.next = this.#front;\n            this.#front = node; // Update head node\n        }\n        this.#queSize++;\n    }\n\n    /* Temporarily store tail node value */\n    popLast() {\n        if (this.#queSize === 0) {\n            return null;\n        }\n        const value = this.#rear.val; // Store tail node value\n        // Update tail node\n        let temp = this.#rear.prev;\n        if (temp !== null) {\n            temp.next = null;\n            this.#rear.prev = null;\n        }\n        this.#rear = temp; // Update tail node\n        this.#queSize--;\n        return value;\n    }\n\n    /* Temporarily store head node value */\n    popFirst() {\n        if (this.#queSize === 0) {\n            return null;\n        }\n        const value = this.#front.val; // Store tail node value\n        // Delete head node\n        let temp = this.#front.next;\n        if (temp !== null) {\n            temp.prev = null;\n            this.#front.next = null;\n        }\n        this.#front = temp; // Update head node\n        this.#queSize--;\n        return value;\n    }\n\n    /* Driver Code */\n    peekLast() {\n        return this.#queSize === 0 ? null : this.#rear.val;\n    }\n\n    /* Return list for printing */\n    peekFirst() {\n        return this.#queSize === 0 ? null : this.#front.val;\n    }\n\n    /* Get the length of the double-ended queue */\n    size() {\n        return this.#queSize;\n    }\n\n    /* Check if the double-ended queue is empty */\n    isEmpty() {\n        return this.#queSize === 0;\n    }\n\n    /* Print deque */\n    print() {\n        const arr = [];\n        let temp = this.#front;\n        while (temp !== null) {\n            arr.push(temp.val);\n            temp = temp.next;\n        }\n        console.log('[' + arr.join(', ') + ']');\n    }\n}\n
      linkedlist_deque.ts
      /* Doubly linked list node */\nclass ListNode {\n    prev: ListNode; // Predecessor node reference (pointer)\n    next: ListNode; // Successor node reference (pointer)\n    val: number; // Node value\n\n    constructor(val: number) {\n        this.val = val;\n        this.next = null;\n        this.prev = null;\n    }\n}\n\n/* Double-ended queue based on doubly linked list implementation */\nclass LinkedListDeque {\n    private front: ListNode; // Head node front\n    private rear: ListNode; // Tail node rear\n    private queSize: number; // Length of the double-ended queue\n\n    constructor() {\n        this.front = null;\n        this.rear = null;\n        this.queSize = 0;\n    }\n\n    /* Rear of the queue enqueue operation */\n    pushLast(val: number): void {\n        const node: ListNode = new ListNode(val);\n        // If the linked list is empty, make both front and rear point to node\n        if (this.queSize === 0) {\n            this.front = node;\n            this.rear = node;\n        } else {\n            // Add node to the tail of the linked list\n            this.rear.next = node;\n            node.prev = this.rear;\n            this.rear = node; // Update tail node\n        }\n        this.queSize++;\n    }\n\n    /* Front of the queue enqueue operation */\n    pushFirst(val: number): void {\n        const node: ListNode = new ListNode(val);\n        // If the linked list is empty, make both front and rear point to node\n        if (this.queSize === 0) {\n            this.front = node;\n            this.rear = node;\n        } else {\n            // Add node to the head of the linked list\n            this.front.prev = node;\n            node.next = this.front;\n            this.front = node; // Update head node\n        }\n        this.queSize++;\n    }\n\n    /* Temporarily store tail node value */\n    popLast(): number {\n        if (this.queSize === 0) {\n            return null;\n        }\n        const value: number = this.rear.val; // Store tail node value\n        // Update tail node\n        let temp: ListNode = this.rear.prev;\n        if (temp !== null) {\n            temp.next = null;\n            this.rear.prev = null;\n        }\n        this.rear = temp; // Update tail node\n        this.queSize--;\n        return value;\n    }\n\n    /* Temporarily store head node value */\n    popFirst(): number {\n        if (this.queSize === 0) {\n            return null;\n        }\n        const value: number = this.front.val; // Store tail node value\n        // Delete head node\n        let temp: ListNode = this.front.next;\n        if (temp !== null) {\n            temp.prev = null;\n            this.front.next = null;\n        }\n        this.front = temp; // Update head node\n        this.queSize--;\n        return value;\n    }\n\n    /* Driver Code */\n    peekLast(): number {\n        return this.queSize === 0 ? null : this.rear.val;\n    }\n\n    /* Return list for printing */\n    peekFirst(): number {\n        return this.queSize === 0 ? null : this.front.val;\n    }\n\n    /* Get the length of the double-ended queue */\n    size(): number {\n        return this.queSize;\n    }\n\n    /* Check if the double-ended queue is empty */\n    isEmpty(): boolean {\n        return this.queSize === 0;\n    }\n\n    /* Print deque */\n    print(): void {\n        const arr: number[] = [];\n        let temp: ListNode = this.front;\n        while (temp !== null) {\n            arr.push(temp.val);\n            temp = temp.next;\n        }\n        console.log('[' + arr.join(', ') + ']');\n    }\n}\n
      linkedlist_deque.dart
      /* Doubly linked list node */\nclass ListNode {\n  int val; // Node value\n  ListNode? next; // Successor node reference\n  ListNode? prev; // Predecessor node reference\n\n  ListNode(this.val, {this.next, this.prev});\n}\n\n/* Deque implemented based on doubly linked list */\nclass LinkedListDeque {\n  late ListNode? _front; // Head node _front\n  late ListNode? _rear; // Tail node _rear\n  int _queSize = 0; // Length of the double-ended queue\n\n  LinkedListDeque() {\n    this._front = null;\n    this._rear = null;\n  }\n\n  /* Get deque length */\n  int size() {\n    return this._queSize;\n  }\n\n  /* Check if the double-ended queue is empty */\n  bool isEmpty() {\n    return size() == 0;\n  }\n\n  /* Enqueue operation */\n  void push(int _num, bool isFront) {\n    final ListNode node = ListNode(_num);\n    if (isEmpty()) {\n      // If list is empty, let both _front and _rear point to node\n      _front = _rear = node;\n    } else if (isFront) {\n      // Front of the queue enqueue operation\n      // Add node to the head of the linked list\n      _front!.prev = node;\n      node.next = _front;\n      _front = node; // Update head node\n    } else {\n      // Rear of the queue enqueue operation\n      // Add node to the tail of the linked list\n      _rear!.next = node;\n      node.prev = _rear;\n      _rear = node; // Update tail node\n    }\n    _queSize++; // Update queue length\n  }\n\n  /* Front of the queue enqueue */\n  void pushFirst(int _num) {\n    push(_num, true);\n  }\n\n  /* Rear of the queue enqueue */\n  void pushLast(int _num) {\n    push(_num, false);\n  }\n\n  /* Dequeue operation */\n  int? pop(bool isFront) {\n    // If queue is empty, return null directly\n    if (isEmpty()) {\n      return null;\n    }\n    final int val;\n    if (isFront) {\n      // Temporarily store head node value\n      val = _front!.val; // Delete head node\n      // Delete head node\n      ListNode? fNext = _front!.next;\n      if (fNext != null) {\n        fNext.prev = null;\n        _front!.next = null;\n      }\n      _front = fNext; // Update head node\n    } else {\n      // Temporarily store tail node value\n      val = _rear!.val; // Delete tail node\n      // Update tail node\n      ListNode? rPrev = _rear!.prev;\n      if (rPrev != null) {\n        rPrev.next = null;\n        _rear!.prev = null;\n      }\n      _rear = rPrev; // Update tail node\n    }\n    _queSize--; // Update queue length\n    return val;\n  }\n\n  /* Rear of the queue dequeue */\n  int? popFirst() {\n    return pop(true);\n  }\n\n  /* Access rear of the queue element */\n  int? popLast() {\n    return pop(false);\n  }\n\n  /* Return list for printing */\n  int? peekFirst() {\n    return _front?.val;\n  }\n\n  /* Driver Code */\n  int? peekLast() {\n    return _rear?.val;\n  }\n\n  /* Return array for printing */\n  List<int> toArray() {\n    ListNode? node = _front;\n    final List<int> res = [];\n    for (int i = 0; i < _queSize; i++) {\n      res.add(node!.val);\n      node = node.next;\n    }\n    return res;\n  }\n}\n
      linkedlist_deque.rs
      /* Doubly linked list node */\npub struct ListNode<T> {\n    pub val: T,                                 // Node value\n    pub next: Option<Rc<RefCell<ListNode<T>>>>, // Successor node pointer\n    pub prev: Option<Rc<RefCell<ListNode<T>>>>, // Predecessor node pointer\n}\n\nimpl<T> ListNode<T> {\n    pub fn new(val: T) -> Rc<RefCell<ListNode<T>>> {\n        Rc::new(RefCell::new(ListNode {\n            val,\n            next: None,\n            prev: None,\n        }))\n    }\n}\n\n/* Double-ended queue based on doubly linked list implementation */\n#[allow(dead_code)]\npub struct LinkedListDeque<T> {\n    front: Option<Rc<RefCell<ListNode<T>>>>, // Head node front\n    rear: Option<Rc<RefCell<ListNode<T>>>>,  // Tail node rear\n    que_size: usize,                         // Length of the double-ended queue\n}\n\nimpl<T: Copy> LinkedListDeque<T> {\n    pub fn new() -> Self {\n        Self {\n            front: None,\n            rear: None,\n            que_size: 0,\n        }\n    }\n\n    /* Get the length of the double-ended queue */\n    pub fn size(&self) -> usize {\n        return self.que_size;\n    }\n\n    /* Check if the double-ended queue is empty */\n    pub fn is_empty(&self) -> bool {\n        return self.que_size == 0;\n    }\n\n    /* Enqueue operation */\n    fn push(&mut self, num: T, is_front: bool) {\n        let node = ListNode::new(num);\n        // Front of the queue enqueue operation\n        if is_front {\n            match self.front.take() {\n                // If the linked list is empty, make both front and rear point to node\n                None => {\n                    self.rear = Some(node.clone());\n                    self.front = Some(node);\n                }\n                // Add node to the head of the linked list\n                Some(old_front) => {\n                    old_front.borrow_mut().prev = Some(node.clone());\n                    node.borrow_mut().next = Some(old_front);\n                    self.front = Some(node); // Update head node\n                }\n            }\n        }\n        // Rear of the queue enqueue operation\n        else {\n            match self.rear.take() {\n                // If the linked list is empty, make both front and rear point to node\n                None => {\n                    self.front = Some(node.clone());\n                    self.rear = Some(node);\n                }\n                // Add node to the tail of the linked list\n                Some(old_rear) => {\n                    old_rear.borrow_mut().next = Some(node.clone());\n                    node.borrow_mut().prev = Some(old_rear);\n                    self.rear = Some(node); // Update tail node\n                }\n            }\n        }\n        self.que_size += 1; // Update queue length\n    }\n\n    /* Front of the queue enqueue */\n    pub fn push_first(&mut self, num: T) {\n        self.push(num, true);\n    }\n\n    /* Rear of the queue enqueue */\n    pub fn push_last(&mut self, num: T) {\n        self.push(num, false);\n    }\n\n    /* Dequeue operation */\n    fn pop(&mut self, is_front: bool) -> Option<T> {\n        // If queue is empty, return None directly\n        if self.is_empty() {\n            return None;\n        };\n        // Temporarily store head node value\n        if is_front {\n            self.front.take().map(|old_front| {\n                match old_front.borrow_mut().next.take() {\n                    Some(new_front) => {\n                        new_front.borrow_mut().prev.take();\n                        self.front = Some(new_front); // Update head node\n                    }\n                    None => {\n                        self.rear.take();\n                    }\n                }\n                self.que_size -= 1; // Update queue length\n                old_front.borrow().val\n            })\n        }\n        // Temporarily store tail node value\n        else {\n            self.rear.take().map(|old_rear| {\n                match old_rear.borrow_mut().prev.take() {\n                    Some(new_rear) => {\n                        new_rear.borrow_mut().next.take();\n                        self.rear = Some(new_rear); // Update tail node\n                    }\n                    None => {\n                        self.front.take();\n                    }\n                }\n                self.que_size -= 1; // Update queue length\n                old_rear.borrow().val\n            })\n        }\n    }\n\n    /* Rear of the queue dequeue */\n    pub fn pop_first(&mut self) -> Option<T> {\n        return self.pop(true);\n    }\n\n    /* Access rear of the queue element */\n    pub fn pop_last(&mut self) -> Option<T> {\n        return self.pop(false);\n    }\n\n    /* Return list for printing */\n    pub fn peek_first(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n        self.front.as_ref()\n    }\n\n    /* Driver Code */\n    pub fn peek_last(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n        self.rear.as_ref()\n    }\n\n    /* Return array for printing */\n    pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n        let mut res: Vec<T> = Vec::new();\n        fn recur<T: Copy>(cur: Option<&Rc<RefCell<ListNode<T>>>>, res: &mut Vec<T>) {\n            if let Some(cur) = cur {\n                res.push(cur.borrow().val);\n                recur(cur.borrow().next.as_ref(), res);\n            }\n        }\n\n        recur(head, &mut res);\n        res\n    }\n}\n
      linkedlist_deque.c
      /* Doubly linked list node */\ntypedef struct DoublyListNode {\n    int val;                     // Node value\n    struct DoublyListNode *next; // Successor node\n    struct DoublyListNode *prev; // Predecessor node\n} DoublyListNode;\n\n/* Constructor */\nDoublyListNode *newDoublyListNode(int num) {\n    DoublyListNode *new = (DoublyListNode *)malloc(sizeof(DoublyListNode));\n    new->val = num;\n    new->next = NULL;\n    new->prev = NULL;\n    return new;\n}\n\n/* Destructor */\nvoid delDoublyListNode(DoublyListNode *node) {\n    free(node);\n}\n\n/* Double-ended queue based on doubly linked list implementation */\ntypedef struct {\n    DoublyListNode *front, *rear; // Head node front, tail node rear\n    int queSize;                  // Length of the double-ended queue\n} LinkedListDeque;\n\n/* Constructor */\nLinkedListDeque *newLinkedListDeque() {\n    LinkedListDeque *deque = (LinkedListDeque *)malloc(sizeof(LinkedListDeque));\n    deque->front = NULL;\n    deque->rear = NULL;\n    deque->queSize = 0;\n    return deque;\n}\n\n/* Destructor */\nvoid delLinkedListdeque(LinkedListDeque *deque) {\n    // Free all nodes\n    for (int i = 0; i < deque->queSize && deque->front != NULL; i++) {\n        DoublyListNode *tmp = deque->front;\n        deque->front = deque->front->next;\n        free(tmp);\n    }\n    // Free deque structure\n    free(deque);\n}\n\n/* Get the length of the queue */\nint size(LinkedListDeque *deque) {\n    return deque->queSize;\n}\n\n/* Check if the queue is empty */\nbool empty(LinkedListDeque *deque) {\n    return (size(deque) == 0);\n}\n\n/* Enqueue */\nvoid push(LinkedListDeque *deque, int num, bool isFront) {\n    DoublyListNode *node = newDoublyListNode(num);\n    // If list is empty, set both front and rear to node\n    if (empty(deque)) {\n        deque->front = deque->rear = node;\n    }\n    // Front of the queue enqueue operation\n    else if (isFront) {\n        // Add node to the head of the linked list\n        deque->front->prev = node;\n        node->next = deque->front;\n        deque->front = node; // Update head node\n    }\n    // Rear of the queue enqueue operation\n    else {\n        // Add node to the tail of the linked list\n        deque->rear->next = node;\n        node->prev = deque->rear;\n        deque->rear = node;\n    }\n    deque->queSize++; // Update queue length\n}\n\n/* Front of the queue enqueue */\nvoid pushFirst(LinkedListDeque *deque, int num) {\n    push(deque, num, true);\n}\n\n/* Rear of the queue enqueue */\nvoid pushLast(LinkedListDeque *deque, int num) {\n    push(deque, num, false);\n}\n\n/* Return list for printing */\nint peekFirst(LinkedListDeque *deque) {\n    assert(size(deque) && deque->front);\n    return deque->front->val;\n}\n\n/* Driver Code */\nint peekLast(LinkedListDeque *deque) {\n    assert(size(deque) && deque->rear);\n    return deque->rear->val;\n}\n\n/* Dequeue */\nint pop(LinkedListDeque *deque, bool isFront) {\n    if (empty(deque))\n        return -1;\n    int val;\n    // Temporarily store head node value\n    if (isFront) {\n        val = peekFirst(deque); // Delete head node\n        DoublyListNode *fNext = deque->front->next;\n        if (fNext) {\n            fNext->prev = NULL;\n            deque->front->next = NULL;\n        }\n        delDoublyListNode(deque->front);\n        deque->front = fNext; // Update head node\n    }\n    // Temporarily store tail node value\n    else {\n        val = peekLast(deque); // Delete tail node\n        DoublyListNode *rPrev = deque->rear->prev;\n        if (rPrev) {\n            rPrev->next = NULL;\n            deque->rear->prev = NULL;\n        }\n        delDoublyListNode(deque->rear);\n        deque->rear = rPrev; // Update tail node\n    }\n    deque->queSize--; // Update queue length\n    return val;\n}\n\n/* Rear of the queue dequeue */\nint popFirst(LinkedListDeque *deque) {\n    return pop(deque, true);\n}\n\n/* Access rear of the queue element */\nint popLast(LinkedListDeque *deque) {\n    return pop(deque, false);\n}\n\n/* Print queue */\nvoid printLinkedListDeque(LinkedListDeque *deque) {\n    int *arr = malloc(sizeof(int) * deque->queSize);\n    // Copy data from list to array\n    int i;\n    DoublyListNode *node;\n    for (i = 0, node = deque->front; i < deque->queSize; i++) {\n        arr[i] = node->val;\n        node = node->next;\n    }\n    printArray(arr, deque->queSize);\n    free(arr);\n}\n
      linkedlist_deque.kt
      /* Doubly linked list node */\nclass ListNode(var _val: Int) {\n    // Node value\n    var next: ListNode? = null // Successor node reference\n    var prev: ListNode? = null // Predecessor node reference\n}\n\n/* Double-ended queue based on doubly linked list implementation */\nclass LinkedListDeque {\n    private var front: ListNode? = null // Head node front\n    private var rear: ListNode? = null // Tail node rear\n    private var queSize: Int = 0 // Length of the double-ended queue\n\n    /* Get the length of the double-ended queue */\n    fun size(): Int {\n        return queSize\n    }\n\n    /* Check if the double-ended queue is empty */\n    fun isEmpty(): Boolean {\n        return size() == 0\n    }\n\n    /* Enqueue operation */\n    fun push(num: Int, isFront: Boolean) {\n        val node = ListNode(num)\n        // If the linked list is empty, make both front and rear point to node\n        if (isEmpty()) {\n            rear = node\n            front = rear\n            // Front of the queue enqueue operation\n        } else if (isFront) {\n            // Add node to the head of the linked list\n            front?.prev = node\n            node.next = front\n            front = node // Update head node\n            // Rear of the queue enqueue operation\n        } else {\n            // Add node to the tail of the linked list\n            rear?.next = node\n            node.prev = rear\n            rear = node // Update tail node\n        }\n        queSize++ // Update queue length\n    }\n\n    /* Front of the queue enqueue */\n    fun pushFirst(num: Int) {\n        push(num, true)\n    }\n\n    /* Rear of the queue enqueue */\n    fun pushLast(num: Int) {\n        push(num, false)\n    }\n\n    /* Dequeue operation */\n    fun pop(isFront: Boolean): Int {\n        if (isEmpty()) \n            throw IndexOutOfBoundsException()\n        val _val: Int\n        // Temporarily store head node value\n        if (isFront) {\n            _val = front!!._val // Delete head node\n            // Delete head node\n            val fNext = front!!.next\n            if (fNext != null) {\n                fNext.prev = null\n                front!!.next = null\n            }\n            front = fNext // Update head node\n            // Temporarily store tail node value\n        } else {\n            _val = rear!!._val // Delete tail node\n            // Update tail node\n            val rPrev = rear!!.prev\n            if (rPrev != null) {\n                rPrev.next = null\n                rear!!.prev = null\n            }\n            rear = rPrev // Update tail node\n        }\n        queSize-- // Update queue length\n        return _val\n    }\n\n    /* Rear of the queue dequeue */\n    fun popFirst(): Int {\n        return pop(true)\n    }\n\n    /* Access rear of the queue element */\n    fun popLast(): Int {\n        return pop(false)\n    }\n\n    /* Return list for printing */\n    fun peekFirst(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return front!!._val\n    }\n\n    /* Driver Code */\n    fun peekLast(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return rear!!._val\n    }\n\n    /* Return array for printing */\n    fun toArray(): IntArray {\n        var node = front\n        val res = IntArray(size())\n        for (i in res.indices) {\n            res[i] = node!!._val\n            node = node.next\n        }\n        return res\n    }\n}\n
      linkedlist_deque.rb
      =begin\nFile: linkedlist_deque.rb\nCreated Time: 2024-04-06\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n### Doubly linked list node\nclass ListNode\n  attr_accessor :val\n  attr_accessor :next # Successor node reference\n  attr_accessor :prev # Predecessor node reference\n\n  ### Constructor ###\n  def initialize(val)\n    @val = val\n  end\nend\n\n### Deque based on doubly linked list ###\nclass LinkedListDeque\n  ### Get deque length ###\n  attr_reader :size\n\n  ### Constructor ###\n  def initialize\n    @front = nil  # Head node front\n    @rear = nil   # Tail node rear\n    @size = 0     # Length of the double-ended queue\n  end\n\n  ### Check if deque is empty ###\n  def is_empty?\n    size.zero?\n  end\n\n  ### Enqueue operation ###\n  def push(num, is_front)\n    node = ListNode.new(num)\n    # If list is empty, set both front and rear to node\n    if is_empty?\n      @front = @rear = node\n    # Front of the queue enqueue operation\n    elsif is_front\n      # Add node to the head of the linked list\n      @front.prev = node\n      node.next = @front\n      @front = node # Update head node\n    # Rear of the queue enqueue operation\n    else\n      # Add node to the tail of the linked list\n      @rear.next = node\n      node.prev = @rear\n      @rear = node # Update tail node\n    end\n    @size += 1 # Update queue length\n  end\n\n  ### Enqueue at front ###\n  def push_first(num)\n    push(num, true)\n  end\n\n  ### Enqueue at rear ###\n  def push_last(num)\n    push(num, false)\n  end\n\n  ### Dequeue operation ###\n  def pop(is_front)\n    raise IndexError, 'Deque is empty' if is_empty?\n\n    # Temporarily store head node value\n    if is_front\n      val = @front.val # Delete head node\n      # Delete head node\n      fnext = @front.next\n      unless fnext.nil?\n        fnext.prev = nil\n        @front.next = nil\n      end\n      @front = fnext # Update head node\n    # Temporarily store tail node value\n    else\n      val = @rear.val # Delete tail node\n      # Update tail node\n      rprev = @rear.prev\n      unless rprev.nil?\n        rprev.next = nil\n        @rear.prev = nil\n      end\n      @rear = rprev # Update tail node\n    end\n    @size -= 1 # Update queue length\n\n    val\n  end\n\n  ### Dequeue from front ###\n  def pop_first\n    pop(true)\n  end\n\n  ### Dequeue from front ###\n  def pop_last\n    pop(false)\n  end\n\n  ### Access front element ###\n  def peek_first\n    raise IndexError, 'Deque is empty' if is_empty?\n\n    @front.val\n  end\n\n  ### Access rear element ###\n  def peek_last\n    raise IndexError, 'Deque is empty' if is_empty?\n\n    @rear.val\n  end\n\n  ### Return array for printing ###\n  def to_array\n    node = @front\n    res = Array.new(size, 0)\n    for i in 0...size\n      res[i] = node.val\n      node = node.next\n    end\n    res\n  end\nend\n
      "},{"location":"chapter_stack_and_queue/deque/#2-array-implementation","title":"2. \u00a0 Array Implementation","text":"

      As shown in Figure 5-9, similar to implementing a queue based on an array, we can also use a circular array to implement a deque.

      ArrayDequepush_last()push_first()pop_last()pop_first()

      Figure 5-9 \u00a0 Enqueue and dequeue operations in array implementation of deque

      Based on the queue implementation, we only need to add methods for \"enqueue at front\" and \"dequeue from rear\":

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_deque.py
      class ArrayDeque:\n    \"\"\"Double-ended queue based on circular array implementation\"\"\"\n\n    def __init__(self, capacity: int):\n        \"\"\"Constructor\"\"\"\n        self._nums: list[int] = [0] * capacity\n        self._front: int = 0\n        self._size: int = 0\n\n    def capacity(self) -> int:\n        \"\"\"Get the capacity of the double-ended queue\"\"\"\n        return len(self._nums)\n\n    def size(self) -> int:\n        \"\"\"Get the length of the double-ended queue\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"Check if the double-ended queue is empty\"\"\"\n        return self._size == 0\n\n    def index(self, i: int) -> int:\n        \"\"\"Calculate circular array index\"\"\"\n        # Use modulo operation to wrap the array head and tail together\n        # When i passes the tail of the array, return to the head\n        # When i passes the head of the array, return to the tail\n        return (i + self.capacity()) % self.capacity()\n\n    def push_first(self, num: int):\n        \"\"\"Front of the queue enqueue\"\"\"\n        if self._size == self.capacity():\n            print(\"Double-ended queue is full\")\n            return\n        # Front pointer moves one position to the left\n        # Use modulo operation to wrap front around to the tail after passing the head of the array\n        self._front = self.index(self._front - 1)\n        # Add num to the front of the queue\n        self._nums[self._front] = num\n        self._size += 1\n\n    def push_last(self, num: int):\n        \"\"\"Rear of the queue enqueue\"\"\"\n        if self._size == self.capacity():\n            print(\"Double-ended queue is full\")\n            return\n        # Calculate rear pointer, points to rear index + 1\n        rear = self.index(self._front + self._size)\n        # Add num to the rear of the queue\n        self._nums[rear] = num\n        self._size += 1\n\n    def pop_first(self) -> int:\n        \"\"\"Front of the queue dequeue\"\"\"\n        num = self.peek_first()\n        # Front pointer moves one position backward\n        self._front = self.index(self._front + 1)\n        self._size -= 1\n        return num\n\n    def pop_last(self) -> int:\n        \"\"\"Rear of the queue dequeue\"\"\"\n        num = self.peek_last()\n        self._size -= 1\n        return num\n\n    def peek_first(self) -> int:\n        \"\"\"Access front of the queue element\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Double-ended queue is empty\")\n        return self._nums[self._front]\n\n    def peek_last(self) -> int:\n        \"\"\"Access rear of the queue element\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Double-ended queue is empty\")\n        # Calculate tail element index\n        last = self.index(self._front + self._size - 1)\n        return self._nums[last]\n\n    def to_array(self) -> list[int]:\n        \"\"\"Return array for printing\"\"\"\n        # Only convert list elements within the valid length range\n        res = []\n        for i in range(self._size):\n            res.append(self._nums[self.index(self._front + i)])\n        return res\n
      array_deque.cpp
      /* Double-ended queue based on circular array implementation */\nclass ArrayDeque {\n  private:\n    vector<int> nums; // Array for storing double-ended queue elements\n    int front;        // Front pointer, points to the front of the queue element\n    int queSize;      // Double-ended queue length\n\n  public:\n    /* Constructor */\n    ArrayDeque(int capacity) {\n        nums.resize(capacity);\n        front = queSize = 0;\n    }\n\n    /* Get the capacity of the double-ended queue */\n    int capacity() {\n        return nums.size();\n    }\n\n    /* Get the length of the double-ended queue */\n    int size() {\n        return queSize;\n    }\n\n    /* Check if the double-ended queue is empty */\n    bool isEmpty() {\n        return queSize == 0;\n    }\n\n    /* Calculate circular array index */\n    int index(int i) {\n        // Use modulo operation to wrap the array head and tail together\n        // When i passes the tail of the array, return to the head\n        // When i passes the head of the array, return to the tail\n        return (i + capacity()) % capacity();\n    }\n\n    /* Front of the queue enqueue */\n    void pushFirst(int num) {\n        if (queSize == capacity()) {\n            cout << \"Double-ended queue is full\" << endl;\n            return;\n        }\n        // Use modulo operation to wrap front around to the tail after passing the head of the array\n        // Add num to the front of the queue\n        front = index(front - 1);\n        // Add num to front of queue\n        nums[front] = num;\n        queSize++;\n    }\n\n    /* Rear of the queue enqueue */\n    void pushLast(int num) {\n        if (queSize == capacity()) {\n            cout << \"Double-ended queue is full\" << endl;\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        int rear = index(front + queSize);\n        // Front pointer moves one position backward\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* Rear of the queue dequeue */\n    int popFirst() {\n        int num = peekFirst();\n        // Move front pointer backward by one position\n        front = index(front + 1);\n        queSize--;\n        return num;\n    }\n\n    /* Access rear of the queue element */\n    int popLast() {\n        int num = peekLast();\n        queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    int peekFirst() {\n        if (isEmpty())\n            throw out_of_range(\"Deque is empty\");\n        return nums[front];\n    }\n\n    /* Driver Code */\n    int peekLast() {\n        if (isEmpty())\n            throw out_of_range(\"Deque is empty\");\n        // Initialize double-ended queue\n        int last = index(front + queSize - 1);\n        return nums[last];\n    }\n\n    /* Return array for printing */\n    vector<int> toVector() {\n        // Elements enqueue\n        vector<int> res(queSize);\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[index(j)];\n        }\n        return res;\n    }\n};\n
      array_deque.java
      /* Double-ended queue based on circular array implementation */\nclass ArrayDeque {\n    private int[] nums; // Array for storing double-ended queue elements\n    private int front; // Front pointer, points to the front of the queue element\n    private int queSize; // Double-ended queue length\n\n    /* Constructor */\n    public ArrayDeque(int capacity) {\n        this.nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* Get the capacity of the double-ended queue */\n    public int capacity() {\n        return nums.length;\n    }\n\n    /* Get the length of the double-ended queue */\n    public int size() {\n        return queSize;\n    }\n\n    /* Check if the double-ended queue is empty */\n    public boolean isEmpty() {\n        return queSize == 0;\n    }\n\n    /* Calculate circular array index */\n    private int index(int i) {\n        // Use modulo operation to wrap the array head and tail together\n        // When i passes the tail of the array, return to the head\n        // When i passes the head of the array, return to the tail\n        return (i + capacity()) % capacity();\n    }\n\n    /* Front of the queue enqueue */\n    public void pushFirst(int num) {\n        if (queSize == capacity()) {\n            System.out.println(\"Double-ended queue is full\");\n            return;\n        }\n        // Use modulo operation to wrap front around to the tail after passing the head of the array\n        // Add num to the front of the queue\n        front = index(front - 1);\n        // Add num to front of queue\n        nums[front] = num;\n        queSize++;\n    }\n\n    /* Rear of the queue enqueue */\n    public void pushLast(int num) {\n        if (queSize == capacity()) {\n            System.out.println(\"Double-ended queue is full\");\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        int rear = index(front + queSize);\n        // Front pointer moves one position backward\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* Rear of the queue dequeue */\n    public int popFirst() {\n        int num = peekFirst();\n        // Move front pointer backward by one position\n        front = index(front + 1);\n        queSize--;\n        return num;\n    }\n\n    /* Access rear of the queue element */\n    public int popLast() {\n        int num = peekLast();\n        queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    public int peekFirst() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return nums[front];\n    }\n\n    /* Driver Code */\n    public int peekLast() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        // Initialize double-ended queue\n        int last = index(front + queSize - 1);\n        return nums[last];\n    }\n\n    /* Return array for printing */\n    public int[] toArray() {\n        // Elements enqueue\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[index(j)];\n        }\n        return res;\n    }\n}\n
      array_deque.cs
      /* Double-ended queue based on circular array implementation */\nclass ArrayDeque {\n    int[] nums;  // Array for storing double-ended queue elements\n    int front;   // Front pointer, points to the front of the queue element\n    int queSize; // Double-ended queue length\n\n    /* Constructor */\n    public ArrayDeque(int capacity) {\n        nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* Get the capacity of the double-ended queue */\n    int Capacity() {\n        return nums.Length;\n    }\n\n    /* Get the length of the double-ended queue */\n    public int Size() {\n        return queSize;\n    }\n\n    /* Check if the double-ended queue is empty */\n    public bool IsEmpty() {\n        return queSize == 0;\n    }\n\n    /* Calculate circular array index */\n    int Index(int i) {\n        // Use modulo operation to wrap the array head and tail together\n        // When i passes the tail of the array, return to the head\n        // When i passes the head of the array, return to the tail\n        return (i + Capacity()) % Capacity();\n    }\n\n    /* Front of the queue enqueue */\n    public void PushFirst(int num) {\n        if (queSize == Capacity()) {\n            Console.WriteLine(\"Double-ended queue is full\");\n            return;\n        }\n        // Use modulo operation to wrap front around to the tail after passing the head of the array\n        // Add num to the front of the queue\n        front = Index(front - 1);\n        // Add num to front of queue\n        nums[front] = num;\n        queSize++;\n    }\n\n    /* Rear of the queue enqueue */\n    public void PushLast(int num) {\n        if (queSize == Capacity()) {\n            Console.WriteLine(\"Double-ended queue is full\");\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        int rear = Index(front + queSize);\n        // Front pointer moves one position backward\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* Rear of the queue dequeue */\n    public int PopFirst() {\n        int num = PeekFirst();\n        // Move front pointer backward by one position\n        front = Index(front + 1);\n        queSize--;\n        return num;\n    }\n\n    /* Access rear of the queue element */\n    public int PopLast() {\n        int num = PeekLast();\n        queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    public int PeekFirst() {\n        if (IsEmpty()) {\n            throw new InvalidOperationException();\n        }\n        return nums[front];\n    }\n\n    /* Driver Code */\n    public int PeekLast() {\n        if (IsEmpty()) {\n            throw new InvalidOperationException();\n        }\n        // Initialize double-ended queue\n        int last = Index(front + queSize - 1);\n        return nums[last];\n    }\n\n    /* Return array for printing */\n    public int[] ToArray() {\n        // Elements enqueue\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[Index(j)];\n        }\n        return res;\n    }\n}\n
      array_deque.go
      /* Double-ended queue based on circular array implementation */\ntype arrayDeque struct {\n    nums        []int // Array for storing double-ended queue elements\n    front       int   // Front pointer, points to the front of the queue element\n    queSize     int   // Double-ended queue length\n    queCapacity int   // Queue capacity (maximum number of elements)\n}\n\n/* Access front of the queue element */\nfunc newArrayDeque(queCapacity int) *arrayDeque {\n    return &arrayDeque{\n        nums:        make([]int, queCapacity),\n        queCapacity: queCapacity,\n        front:       0,\n        queSize:     0,\n    }\n}\n\n/* Get the length of the double-ended queue */\nfunc (q *arrayDeque) size() int {\n    return q.queSize\n}\n\n/* Check if the double-ended queue is empty */\nfunc (q *arrayDeque) isEmpty() bool {\n    return q.queSize == 0\n}\n\n/* Calculate circular array index */\nfunc (q *arrayDeque) index(i int) int {\n    // Use modulo operation to wrap the array head and tail together\n    // When i passes the tail of the array, return to the head\n    // When i passes the head of the array, return to the tail\n    return (i + q.queCapacity) % q.queCapacity\n}\n\n/* Front of the queue enqueue */\nfunc (q *arrayDeque) pushFirst(num int) {\n    if q.queSize == q.queCapacity {\n        fmt.Println(\"Double-ended queue is full\")\n        return\n    }\n    // Use modulo operation to wrap front around to the tail after passing the head of the array\n    // Add num to the front of the queue\n    q.front = q.index(q.front - 1)\n    // Add num to front of queue\n    q.nums[q.front] = num\n    q.queSize++\n}\n\n/* Rear of the queue enqueue */\nfunc (q *arrayDeque) pushLast(num int) {\n    if q.queSize == q.queCapacity {\n        fmt.Println(\"Double-ended queue is full\")\n        return\n    }\n    // Use modulo operation to wrap rear around to the head after passing the tail of the array\n    rear := q.index(q.front + q.queSize)\n    // Front pointer moves one position backward\n    q.nums[rear] = num\n    q.queSize++\n}\n\n/* Rear of the queue dequeue */\nfunc (q *arrayDeque) popFirst() any {\n    num := q.peekFirst()\n    if num == nil {\n        return nil\n    }\n    // Move front pointer backward by one position\n    q.front = q.index(q.front + 1)\n    q.queSize--\n    return num\n}\n\n/* Access rear of the queue element */\nfunc (q *arrayDeque) popLast() any {\n    num := q.peekLast()\n    if num == nil {\n        return nil\n    }\n    q.queSize--\n    return num\n}\n\n/* Return list for printing */\nfunc (q *arrayDeque) peekFirst() any {\n    if q.isEmpty() {\n        return nil\n    }\n    return q.nums[q.front]\n}\n\n/* Driver Code */\nfunc (q *arrayDeque) peekLast() any {\n    if q.isEmpty() {\n        return nil\n    }\n    // Initialize double-ended queue\n    last := q.index(q.front + q.queSize - 1)\n    return q.nums[last]\n}\n\n/* Get Slice for printing */\nfunc (q *arrayDeque) toSlice() []int {\n    // Elements enqueue\n    res := make([]int, q.queSize)\n    for i, j := 0, q.front; i < q.queSize; i++ {\n        res[i] = q.nums[q.index(j)]\n        j++\n    }\n    return res\n}\n
      array_deque.swift
      /* Double-ended queue based on circular array implementation */\nclass ArrayDeque {\n    private var nums: [Int] // Array for storing double-ended queue elements\n    private var front: Int // Front pointer, points to the front of the queue element\n    private var _size: Int // Double-ended queue length\n\n    /* Constructor */\n    init(capacity: Int) {\n        nums = Array(repeating: 0, count: capacity)\n        front = 0\n        _size = 0\n    }\n\n    /* Get the capacity of the double-ended queue */\n    func capacity() -> Int {\n        nums.count\n    }\n\n    /* Get the length of the double-ended queue */\n    func size() -> Int {\n        _size\n    }\n\n    /* Check if the double-ended queue is empty */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* Calculate circular array index */\n    private func index(i: Int) -> Int {\n        // Use modulo operation to wrap the array head and tail together\n        // When i passes the tail of the array, return to the head\n        // When i passes the head of the array, return to the tail\n        (i + capacity()) % capacity()\n    }\n\n    /* Front of the queue enqueue */\n    func pushFirst(num: Int) {\n        if size() == capacity() {\n            print(\"Double-ended queue is full\")\n            return\n        }\n        // Use modulo operation to wrap front around to the tail after passing the head of the array\n        // Add num to the front of the queue\n        front = index(i: front - 1)\n        // Add num to front of queue\n        nums[front] = num\n        _size += 1\n    }\n\n    /* Rear of the queue enqueue */\n    func pushLast(num: Int) {\n        if size() == capacity() {\n            print(\"Double-ended queue is full\")\n            return\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        let rear = index(i: front + size())\n        // Front pointer moves one position backward\n        nums[rear] = num\n        _size += 1\n    }\n\n    /* Rear of the queue dequeue */\n    func popFirst() -> Int {\n        let num = peekFirst()\n        // Move front pointer backward by one position\n        front = index(i: front + 1)\n        _size -= 1\n        return num\n    }\n\n    /* Access rear of the queue element */\n    func popLast() -> Int {\n        let num = peekLast()\n        _size -= 1\n        return num\n    }\n\n    /* Return list for printing */\n    func peekFirst() -> Int {\n        if isEmpty() {\n            fatalError(\"Deque is empty\")\n        }\n        return nums[front]\n    }\n\n    /* Driver Code */\n    func peekLast() -> Int {\n        if isEmpty() {\n            fatalError(\"Deque is empty\")\n        }\n        // Initialize double-ended queue\n        let last = index(i: front + size() - 1)\n        return nums[last]\n    }\n\n    /* Return array for printing */\n    func toArray() -> [Int] {\n        // Elements enqueue\n        (front ..< front + size()).map { nums[index(i: $0)] }\n    }\n}\n
      array_deque.js
      /* Double-ended queue based on circular array implementation */\nclass ArrayDeque {\n    #nums; // Array for storing double-ended queue elements\n    #front; // Front pointer, points to the front of the queue element\n    #queSize; // Double-ended queue length\n\n    /* Constructor */\n    constructor(capacity) {\n        this.#nums = new Array(capacity);\n        this.#front = 0;\n        this.#queSize = 0;\n    }\n\n    /* Get the capacity of the double-ended queue */\n    capacity() {\n        return this.#nums.length;\n    }\n\n    /* Get the length of the double-ended queue */\n    size() {\n        return this.#queSize;\n    }\n\n    /* Check if the double-ended queue is empty */\n    isEmpty() {\n        return this.#queSize === 0;\n    }\n\n    /* Calculate circular array index */\n    index(i) {\n        // Use modulo operation to wrap the array head and tail together\n        // When i passes the tail of the array, return to the head\n        // When i passes the head of the array, return to the tail\n        return (i + this.capacity()) % this.capacity();\n    }\n\n    /* Front of the queue enqueue */\n    pushFirst(num) {\n        if (this.#queSize === this.capacity()) {\n            console.log('Double-ended queue is full');\n            return;\n        }\n        // Use modulo operation to wrap front around to the tail after passing the head of the array\n        // Add num to the front of the queue\n        this.#front = this.index(this.#front - 1);\n        // Add num to front of queue\n        this.#nums[this.#front] = num;\n        this.#queSize++;\n    }\n\n    /* Rear of the queue enqueue */\n    pushLast(num) {\n        if (this.#queSize === this.capacity()) {\n            console.log('Double-ended queue is full');\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        const rear = this.index(this.#front + this.#queSize);\n        // Front pointer moves one position backward\n        this.#nums[rear] = num;\n        this.#queSize++;\n    }\n\n    /* Rear of the queue dequeue */\n    popFirst() {\n        const num = this.peekFirst();\n        // Move front pointer backward by one position\n        this.#front = this.index(this.#front + 1);\n        this.#queSize--;\n        return num;\n    }\n\n    /* Access rear of the queue element */\n    popLast() {\n        const num = this.peekLast();\n        this.#queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peekFirst() {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        return this.#nums[this.#front];\n    }\n\n    /* Driver Code */\n    peekLast() {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        // Initialize double-ended queue\n        const last = this.index(this.#front + this.#queSize - 1);\n        return this.#nums[last];\n    }\n\n    /* Return array for printing */\n    toArray() {\n        // Elements enqueue\n        const res = [];\n        for (let i = 0, j = this.#front; i < this.#queSize; i++, j++) {\n            res[i] = this.#nums[this.index(j)];\n        }\n        return res;\n    }\n}\n
      array_deque.ts
      /* Double-ended queue based on circular array implementation */\nclass ArrayDeque {\n    private nums: number[]; // Array for storing double-ended queue elements\n    private front: number; // Front pointer, points to the front of the queue element\n    private queSize: number; // Double-ended queue length\n\n    /* Constructor */\n    constructor(capacity: number) {\n        this.nums = new Array(capacity);\n        this.front = 0;\n        this.queSize = 0;\n    }\n\n    /* Get the capacity of the double-ended queue */\n    capacity(): number {\n        return this.nums.length;\n    }\n\n    /* Get the length of the double-ended queue */\n    size(): number {\n        return this.queSize;\n    }\n\n    /* Check if the double-ended queue is empty */\n    isEmpty(): boolean {\n        return this.queSize === 0;\n    }\n\n    /* Calculate circular array index */\n    index(i: number): number {\n        // Use modulo operation to wrap the array head and tail together\n        // When i passes the tail of the array, return to the head\n        // When i passes the head of the array, return to the tail\n        return (i + this.capacity()) % this.capacity();\n    }\n\n    /* Front of the queue enqueue */\n    pushFirst(num: number): void {\n        if (this.queSize === this.capacity()) {\n            console.log('Double-ended queue is full');\n            return;\n        }\n        // Use modulo operation to wrap front around to the tail after passing the head of the array\n        // Add num to the front of the queue\n        this.front = this.index(this.front - 1);\n        // Add num to front of queue\n        this.nums[this.front] = num;\n        this.queSize++;\n    }\n\n    /* Rear of the queue enqueue */\n    pushLast(num: number): void {\n        if (this.queSize === this.capacity()) {\n            console.log('Double-ended queue is full');\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        const rear: number = this.index(this.front + this.queSize);\n        // Front pointer moves one position backward\n        this.nums[rear] = num;\n        this.queSize++;\n    }\n\n    /* Rear of the queue dequeue */\n    popFirst(): number {\n        const num: number = this.peekFirst();\n        // Move front pointer backward by one position\n        this.front = this.index(this.front + 1);\n        this.queSize--;\n        return num;\n    }\n\n    /* Access rear of the queue element */\n    popLast(): number {\n        const num: number = this.peekLast();\n        this.queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peekFirst(): number {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        return this.nums[this.front];\n    }\n\n    /* Driver Code */\n    peekLast(): number {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        // Initialize double-ended queue\n        const last = this.index(this.front + this.queSize - 1);\n        return this.nums[last];\n    }\n\n    /* Return array for printing */\n    toArray(): number[] {\n        // Elements enqueue\n        const res: number[] = [];\n        for (let i = 0, j = this.front; i < this.queSize; i++, j++) {\n            res[i] = this.nums[this.index(j)];\n        }\n        return res;\n    }\n}\n
      array_deque.dart
      /* Double-ended queue based on circular array implementation */\nclass ArrayDeque {\n  late List<int> _nums; // Array for storing double-ended queue elements\n  late int _front; // Front pointer, points to the front of the queue element\n  late int _queSize; // Double-ended queue length\n\n  /* Constructor */\n  ArrayDeque(int capacity) {\n    this._nums = List.filled(capacity, 0);\n    this._front = this._queSize = 0;\n  }\n\n  /* Get the capacity of the double-ended queue */\n  int capacity() {\n    return _nums.length;\n  }\n\n  /* Get the length of the double-ended queue */\n  int size() {\n    return _queSize;\n  }\n\n  /* Check if the double-ended queue is empty */\n  bool isEmpty() {\n    return _queSize == 0;\n  }\n\n  /* Calculate circular array index */\n  int index(int i) {\n    // Use modulo operation to wrap the array head and tail together\n    // When i passes the tail of the array, return to the head\n    // When i passes the head of the array, return to the tail\n    return (i + capacity()) % capacity();\n  }\n\n  /* Front of the queue enqueue */\n  void pushFirst(int _num) {\n    if (_queSize == capacity()) {\n      throw Exception(\"Double-ended queue is full\");\n    }\n    // Use modulo operation to wrap front around to the tail after passing the head of the array\n    // Use modulo operation to wrap _front from array head back to tail\n    _front = index(_front - 1);\n    // Add _num to queue front\n    _nums[_front] = _num;\n    _queSize++;\n  }\n\n  /* Rear of the queue enqueue */\n  void pushLast(int _num) {\n    if (_queSize == capacity()) {\n      throw Exception(\"Double-ended queue is full\");\n    }\n    // Use modulo operation to wrap rear around to the head after passing the tail of the array\n    int rear = index(_front + _queSize);\n    // Add _num to queue rear\n    _nums[rear] = _num;\n    _queSize++;\n  }\n\n  /* Rear of the queue dequeue */\n  int popFirst() {\n    int _num = peekFirst();\n    // Move front pointer right by one\n    _front = index(_front + 1);\n    _queSize--;\n    return _num;\n  }\n\n  /* Access rear of the queue element */\n  int popLast() {\n    int _num = peekLast();\n    _queSize--;\n    return _num;\n  }\n\n  /* Return list for printing */\n  int peekFirst() {\n    if (isEmpty()) {\n      throw Exception(\"Deque is empty\");\n    }\n    return _nums[_front];\n  }\n\n  /* Driver Code */\n  int peekLast() {\n    if (isEmpty()) {\n      throw Exception(\"Deque is empty\");\n    }\n    // Initialize double-ended queue\n    int last = index(_front + _queSize - 1);\n    return _nums[last];\n  }\n\n  /* Return array for printing */\n  List<int> toArray() {\n    // Elements enqueue\n    List<int> res = List.filled(_queSize, 0);\n    for (int i = 0, j = _front; i < _queSize; i++, j++) {\n      res[i] = _nums[index(j)];\n    }\n    return res;\n  }\n}\n
      array_deque.rs
      /* Double-ended queue based on circular array implementation */\nstruct ArrayDeque<T> {\n    nums: Vec<T>,    // Array for storing double-ended queue elements\n    front: usize,    // Front pointer, points to the front of the queue element\n    que_size: usize, // Double-ended queue length\n}\n\nimpl<T: Copy + Default> ArrayDeque<T> {\n    /* Constructor */\n    pub fn new(capacity: usize) -> Self {\n        Self {\n            nums: vec![T::default(); capacity],\n            front: 0,\n            que_size: 0,\n        }\n    }\n\n    /* Get the capacity of the double-ended queue */\n    pub fn capacity(&self) -> usize {\n        self.nums.len()\n    }\n\n    /* Get the length of the double-ended queue */\n    pub fn size(&self) -> usize {\n        self.que_size\n    }\n\n    /* Check if the double-ended queue is empty */\n    pub fn is_empty(&self) -> bool {\n        self.que_size == 0\n    }\n\n    /* Calculate circular array index */\n    fn index(&self, i: i32) -> usize {\n        // Use modulo operation to wrap the array head and tail together\n        // When i passes the tail of the array, return to the head\n        // When i passes the head of the array, return to the tail\n        ((i + self.capacity() as i32) % self.capacity() as i32) as usize\n    }\n\n    /* Front of the queue enqueue */\n    pub fn push_first(&mut self, num: T) {\n        if self.que_size == self.capacity() {\n            println!(\"Double-ended queue is full\");\n            return;\n        }\n        // Use modulo operation to wrap front around to the tail after passing the head of the array\n        // Add num to the front of the queue\n        self.front = self.index(self.front as i32 - 1);\n        // Add num to front of queue\n        self.nums[self.front] = num;\n        self.que_size += 1;\n    }\n\n    /* Rear of the queue enqueue */\n    pub fn push_last(&mut self, num: T) {\n        if self.que_size == self.capacity() {\n            println!(\"Double-ended queue is full\");\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        let rear = self.index(self.front as i32 + self.que_size as i32);\n        // Front pointer moves one position backward\n        self.nums[rear] = num;\n        self.que_size += 1;\n    }\n\n    /* Rear of the queue dequeue */\n    fn pop_first(&mut self) -> T {\n        let num = self.peek_first();\n        // Move front pointer backward by one position\n        self.front = self.index(self.front as i32 + 1);\n        self.que_size -= 1;\n        num\n    }\n\n    /* Access rear of the queue element */\n    fn pop_last(&mut self) -> T {\n        let num = self.peek_last();\n        self.que_size -= 1;\n        num\n    }\n\n    /* Return list for printing */\n    fn peek_first(&self) -> T {\n        if self.is_empty() {\n            panic!(\"Deque is empty\")\n        };\n        self.nums[self.front]\n    }\n\n    /* Driver Code */\n    fn peek_last(&self) -> T {\n        if self.is_empty() {\n            panic!(\"Deque is empty\")\n        };\n        // Initialize double-ended queue\n        let last = self.index(self.front as i32 + self.que_size as i32 - 1);\n        self.nums[last]\n    }\n\n    /* Return array for printing */\n    fn to_array(&self) -> Vec<T> {\n        // Elements enqueue\n        let mut res = vec![T::default(); self.que_size];\n        let mut j = self.front;\n        for i in 0..self.que_size {\n            res[i] = self.nums[self.index(j as i32)];\n            j += 1;\n        }\n        res\n    }\n}\n
      array_deque.c
      /* Double-ended queue based on circular array implementation */\ntypedef struct {\n    int *nums;       // Array for storing queue elements\n    int front;       // Front pointer, points to the front of the queue element\n    int queSize;     // Rear pointer, points to rear + 1\n    int queCapacity; // Queue capacity\n} ArrayDeque;\n\n/* Constructor */\nArrayDeque *newArrayDeque(int capacity) {\n    ArrayDeque *deque = (ArrayDeque *)malloc(sizeof(ArrayDeque));\n    // Initialize array\n    deque->queCapacity = capacity;\n    deque->nums = (int *)malloc(sizeof(int) * deque->queCapacity);\n    deque->front = deque->queSize = 0;\n    return deque;\n}\n\n/* Destructor */\nvoid delArrayDeque(ArrayDeque *deque) {\n    free(deque->nums);\n    free(deque);\n}\n\n/* Get the capacity of the double-ended queue */\nint capacity(ArrayDeque *deque) {\n    return deque->queCapacity;\n}\n\n/* Get the length of the double-ended queue */\nint size(ArrayDeque *deque) {\n    return deque->queSize;\n}\n\n/* Check if the double-ended queue is empty */\nbool empty(ArrayDeque *deque) {\n    return deque->queSize == 0;\n}\n\n/* Calculate circular array index */\nint dequeIndex(ArrayDeque *deque, int i) {\n    // Use modulo operation to wrap the array head and tail together\n    // When i exceeds array end, wrap to head\n    // When i passes the head of the array, return to the tail\n    return ((i + capacity(deque)) % capacity(deque));\n}\n\n/* Front of the queue enqueue */\nvoid pushFirst(ArrayDeque *deque, int num) {\n    if (deque->queSize == capacity(deque)) {\n        printf(\"Deque is full\\r\\n\");\n        return;\n    }\n    // Use modulo operation to wrap front around to the tail after passing the head of the array\n    // Use modulo to wrap front from array head to rear\n    deque->front = dequeIndex(deque, deque->front - 1);\n    // Add num to queue front\n    deque->nums[deque->front] = num;\n    deque->queSize++;\n}\n\n/* Rear of the queue enqueue */\nvoid pushLast(ArrayDeque *deque, int num) {\n    if (deque->queSize == capacity(deque)) {\n        printf(\"Deque is full\\r\\n\");\n        return;\n    }\n    // Use modulo operation to wrap rear around to the head after passing the tail of the array\n    int rear = dequeIndex(deque, deque->front + deque->queSize);\n    // Front pointer moves one position backward\n    deque->nums[rear] = num;\n    deque->queSize++;\n}\n\n/* Return list for printing */\nint peekFirst(ArrayDeque *deque) {\n    // Access error: Deque is empty\n    assert(empty(deque) == 0);\n    return deque->nums[deque->front];\n}\n\n/* Driver Code */\nint peekLast(ArrayDeque *deque) {\n    // Access error: Deque is empty\n    assert(empty(deque) == 0);\n    int last = dequeIndex(deque, deque->front + deque->queSize - 1);\n    return deque->nums[last];\n}\n\n/* Rear of the queue dequeue */\nint popFirst(ArrayDeque *deque) {\n    int num = peekFirst(deque);\n    // Move front pointer backward by one position\n    deque->front = dequeIndex(deque, deque->front + 1);\n    deque->queSize--;\n    return num;\n}\n\n/* Access rear of the queue element */\nint popLast(ArrayDeque *deque) {\n    int num = peekLast(deque);\n    deque->queSize--;\n    return num;\n}\n\n/* Return array for printing */\nint *toArray(ArrayDeque *deque, int *queSize) {\n    *queSize = deque->queSize;\n    int *res = (int *)calloc(deque->queSize, sizeof(int));\n    int j = deque->front;\n    for (int i = 0; i < deque->queSize; i++) {\n        res[i] = deque->nums[j % deque->queCapacity];\n        j++;\n    }\n    return res;\n}\n
      array_deque.kt
      /* Constructor */\nclass ArrayDeque(capacity: Int) {\n    private var nums: IntArray = IntArray(capacity) // Array for storing double-ended queue elements\n    private var front: Int = 0 // Front pointer, points to the front of the queue element\n    private var queSize: Int = 0 // Double-ended queue length\n\n    /* Get the capacity of the double-ended queue */\n    fun capacity(): Int {\n        return nums.size\n    }\n\n    /* Get the length of the double-ended queue */\n    fun size(): Int {\n        return queSize\n    }\n\n    /* Check if the double-ended queue is empty */\n    fun isEmpty(): Boolean {\n        return queSize == 0\n    }\n\n    /* Calculate circular array index */\n    private fun index(i: Int): Int {\n        // Use modulo operation to wrap the array head and tail together\n        // When i passes the tail of the array, return to the head\n        // When i passes the head of the array, return to the tail\n        return (i + capacity()) % capacity()\n    }\n\n    /* Front of the queue enqueue */\n    fun pushFirst(num: Int) {\n        if (queSize == capacity()) {\n            println(\"Double-ended queue is full\")\n            return\n        }\n        // Use modulo operation to wrap front around to the tail after passing the head of the array\n        // Add num to the front of the queue\n        front = index(front - 1)\n        // Add num to front of queue\n        nums[front] = num\n        queSize++\n    }\n\n    /* Rear of the queue enqueue */\n    fun pushLast(num: Int) {\n        if (queSize == capacity()) {\n            println(\"Double-ended queue is full\")\n            return\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        val rear = index(front + queSize)\n        // Front pointer moves one position backward\n        nums[rear] = num\n        queSize++\n    }\n\n    /* Rear of the queue dequeue */\n    fun popFirst(): Int {\n        val num = peekFirst()\n        // Move front pointer backward by one position\n        front = index(front + 1)\n        queSize--\n        return num\n    }\n\n    /* Access rear of the queue element */\n    fun popLast(): Int {\n        val num = peekLast()\n        queSize--\n        return num\n    }\n\n    /* Return list for printing */\n    fun peekFirst(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return nums[front]\n    }\n\n    /* Driver Code */\n    fun peekLast(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        // Initialize double-ended queue\n        val last = index(front + queSize - 1)\n        return nums[last]\n    }\n\n    /* Return array for printing */\n    fun toArray(): IntArray {\n        // Elements enqueue\n        val res = IntArray(queSize)\n        var i = 0\n        var j = front\n        while (i < queSize) {\n            res[i] = nums[index(j)]\n            i++\n            j++\n        }\n        return res\n    }\n}\n
      array_deque.rb
      ### Deque based on circular array ###\nclass ArrayDeque\n  ### Get deque length ###\n  attr_reader :size\n\n  ### Constructor ###\n  def initialize(capacity)\n    @nums = Array.new(capacity, 0)\n    @front = 0\n    @size = 0\n  end\n\n  ### Get deque capacity ###\n  def capacity\n    @nums.length\n  end\n\n  ### Check if deque is empty ###\n  def is_empty?\n    size.zero?\n  end\n\n  ### Enqueue at front ###\n  def push_first(num)\n    if size == capacity\n      puts 'Double-ended queue is full'\n      return\n    end\n\n    # Use modulo operation to wrap front around to the tail after passing the head of the array\n    # Add num to the front of the queue\n    @front = index(@front - 1)\n    # Add num to front of queue\n    @nums[@front] = num\n    @size += 1\n  end\n\n  ### Enqueue at rear ###\n  def push_last(num)\n    if size == capacity\n      puts 'Double-ended queue is full'\n      return\n    end\n\n    # Use modulo operation to wrap rear around to the head after passing the tail of the array\n    rear = index(@front + size)\n    # Front pointer moves one position backward\n    @nums[rear] = num\n    @size += 1\n  end\n\n  ### Dequeue from front ###\n  def pop_first\n    num = peek_first\n    # Move front pointer backward by one position\n    @front = index(@front + 1)\n    @size -= 1\n    num\n  end\n\n  ### Dequeue from rear ###\n  def pop_last\n    num = peek_last\n    @size -= 1\n    num\n  end\n\n  ### Access front element ###\n  def peek_first\n    raise IndexError, 'Deque is empty' if is_empty?\n\n    @nums[@front]\n  end\n\n  ### Access rear element ###\n  def peek_last\n    raise IndexError, 'Deque is empty' if is_empty?\n\n    # Initialize double-ended queue\n    last = index(@front + size - 1)\n    @nums[last]\n  end\n\n  ### Return array for printing ###\n  def to_array\n    # Elements enqueue\n    res = []\n    for i in 0...size\n      res << @nums[index(@front + i)]\n    end\n    res\n  end\n\n  private\n\n  ### Calculate circular array index ###\n  def index(i)\n    # Use modulo operation to wrap the array head and tail together\n    # When i passes the tail of the array, return to the head\n    # When i passes the head of the array, return to the tail\n    (i + capacity) % capacity\n  end\nend\n
      "},{"location":"chapter_stack_and_queue/deque/#533-deque-applications","title":"5.3.3 \u00a0 Deque Applications","text":"

      A deque combines the logic of both stacks and queues. Therefore, it can implement all application scenarios of both, while providing greater flexibility.

      We know that the \"undo\" function in software is typically implemented using a stack: the system pushes each change operation onto the stack and then implements undo through pop. However, considering system resource limitations, software usually limits the number of undo steps (for example, only allowing 50 steps to be saved). When the stack length exceeds 50, the software needs to perform a deletion operation at the bottom of the stack (front of the queue). But a stack cannot implement this functionality, so a deque is needed to replace the stack. Note that the core logic of \"undo\" still follows the LIFO principle of a stack; it's just that the deque can more flexibly implement some additional logic.

      "},{"location":"chapter_stack_and_queue/queue/","title":"5.2 \u00a0 Queue","text":"

      A queue is a linear data structure that follows the First In First Out (FIFO) rule. As the name suggests, a queue simulates the phenomenon of lining up, where newcomers continuously join the end of the queue, while people at the front of the queue leave one by one.

      As shown in Figure 5-4, we call the front of the queue the \"front\" and the end the \"rear.\" The operation of adding an element to the rear is called \"enqueue,\" and the operation of removing the front element is called \"dequeue.\"

      Figure 5-4 \u00a0 FIFO rule of queue

      "},{"location":"chapter_stack_and_queue/queue/#521-common-queue-operations","title":"5.2.1 \u00a0 Common Queue Operations","text":"

      The common operations on a queue are shown in Table 5-2. Note that method names may vary across different programming languages. We adopt the same naming convention as for stacks here.

      Table 5-2 \u00a0 Efficiency of Queue Operations

      Method Description Time Complexity push() Enqueue element, add element to rear \\(O(1)\\) pop() Dequeue front element \\(O(1)\\) peek() Access front element \\(O(1)\\)

      We can directly use the ready-made queue classes in programming languages:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby queue.py
      from collections import deque\n\n# Initialize queue\n# In Python, we generally use the deque class as a queue\n# Although queue.Queue() is a pure queue class, it is not very user-friendly, so it is not recommended\nque: deque[int] = deque()\n\n# Enqueue elements\nque.append(1)\nque.append(3)\nque.append(2)\nque.append(5)\nque.append(4)\n\n# Access front element\nfront: int = que[0]\n\n# Dequeue element\npop: int = que.popleft()\n\n# Get queue length\nsize: int = len(que)\n\n# Check if queue is empty\nis_empty: bool = len(que) == 0\n
      queue.cpp
      /* Initialize queue */\nqueue<int> queue;\n\n/* Enqueue elements */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* Access front element */\nint front = queue.front();\n\n/* Dequeue element */\nqueue.pop();\n\n/* Get queue length */\nint size = queue.size();\n\n/* Check if queue is empty */\nbool empty = queue.empty();\n
      queue.java
      /* Initialize queue */\nQueue<Integer> queue = new LinkedList<>();\n\n/* Enqueue elements */\nqueue.offer(1);\nqueue.offer(3);\nqueue.offer(2);\nqueue.offer(5);\nqueue.offer(4);\n\n/* Access front element */\nint peek = queue.peek();\n\n/* Dequeue element */\nint pop = queue.poll();\n\n/* Get queue length */\nint size = queue.size();\n\n/* Check if queue is empty */\nboolean isEmpty = queue.isEmpty();\n
      queue.cs
      /* Initialize queue */\nQueue<int> queue = new();\n\n/* Enqueue elements */\nqueue.Enqueue(1);\nqueue.Enqueue(3);\nqueue.Enqueue(2);\nqueue.Enqueue(5);\nqueue.Enqueue(4);\n\n/* Access front element */\nint peek = queue.Peek();\n\n/* Dequeue element */\nint pop = queue.Dequeue();\n\n/* Get queue length */\nint size = queue.Count;\n\n/* Check if queue is empty */\nbool isEmpty = queue.Count == 0;\n
      queue_test.go
      /* Initialize queue */\n// In Go, use list as a queue\nqueue := list.New()\n\n/* Enqueue elements */\nqueue.PushBack(1)\nqueue.PushBack(3)\nqueue.PushBack(2)\nqueue.PushBack(5)\nqueue.PushBack(4)\n\n/* Access front element */\npeek := queue.Front()\n\n/* Dequeue element */\npop := queue.Front()\nqueue.Remove(pop)\n\n/* Get queue length */\nsize := queue.Len()\n\n/* Check if queue is empty */\nisEmpty := queue.Len() == 0\n
      queue.swift
      /* Initialize queue */\n// Swift does not have a built-in queue class, can use Array as a queue\nvar queue: [Int] = []\n\n/* Enqueue elements */\nqueue.append(1)\nqueue.append(3)\nqueue.append(2)\nqueue.append(5)\nqueue.append(4)\n\n/* Access front element */\nlet peek = queue.first!\n\n/* Dequeue element */\n// Since it's an array, removeFirst has O(n) complexity\nlet pool = queue.removeFirst()\n\n/* Get queue length */\nlet size = queue.count\n\n/* Check if queue is empty */\nlet isEmpty = queue.isEmpty\n
      queue.js
      /* Initialize queue */\n// JavaScript does not have a built-in queue, can use Array as a queue\nconst queue = [];\n\n/* Enqueue elements */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* Access front element */\nconst peek = queue[0];\n\n/* Dequeue element */\n// The underlying structure is an array, so shift() has O(n) time complexity\nconst pop = queue.shift();\n\n/* Get queue length */\nconst size = queue.length;\n\n/* Check if queue is empty */\nconst empty = queue.length === 0;\n
      queue.ts
      /* Initialize queue */\n// TypeScript does not have a built-in queue, can use Array as a queue\nconst queue: number[] = [];\n\n/* Enqueue elements */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* Access front element */\nconst peek = queue[0];\n\n/* Dequeue element */\n// The underlying structure is an array, so shift() has O(n) time complexity\nconst pop = queue.shift();\n\n/* Get queue length */\nconst size = queue.length;\n\n/* Check if queue is empty */\nconst empty = queue.length === 0;\n
      queue.dart
      /* Initialize queue */\n// In Dart, the Queue class is a deque and can also be used as a queue\nQueue<int> queue = Queue();\n\n/* Enqueue elements */\nqueue.add(1);\nqueue.add(3);\nqueue.add(2);\nqueue.add(5);\nqueue.add(4);\n\n/* Access front element */\nint peek = queue.first;\n\n/* Dequeue element */\nint pop = queue.removeFirst();\n\n/* Get queue length */\nint size = queue.length;\n\n/* Check if queue is empty */\nbool isEmpty = queue.isEmpty;\n
      queue.rs
      /* Initialize deque */\n// In Rust, use deque as a regular queue\nlet mut deque: VecDeque<u32> = VecDeque::new();\n\n/* Enqueue elements */\ndeque.push_back(1);\ndeque.push_back(3);\ndeque.push_back(2);\ndeque.push_back(5);\ndeque.push_back(4);\n\n/* Access front element */\nif let Some(front) = deque.front() {\n}\n\n/* Dequeue element */\nif let Some(pop) = deque.pop_front() {\n}\n\n/* Get queue length */\nlet size = deque.len();\n\n/* Check if queue is empty */\nlet is_empty = deque.is_empty();\n
      queue.c
      // C does not provide a built-in queue\n
      queue.kt
      /* Initialize queue */\nval queue = LinkedList<Int>()\n\n/* Enqueue elements */\nqueue.offer(1)\nqueue.offer(3)\nqueue.offer(2)\nqueue.offer(5)\nqueue.offer(4)\n\n/* Access front element */\nval peek = queue.peek()\n\n/* Dequeue element */\nval pop = queue.poll()\n\n/* Get queue length */\nval size = queue.size\n\n/* Check if queue is empty */\nval isEmpty = queue.isEmpty()\n
      queue.rb
      # Initialize queue\n# Ruby's built-in queue (Thread::Queue) does not have peek and traversal methods, can use Array as a queue\nqueue = []\n\n# Enqueue elements\nqueue.push(1)\nqueue.push(3)\nqueue.push(2)\nqueue.push(5)\nqueue.push(4)\n\n# Access front element\npeek = queue.first\n\n# Dequeue element\n# Please note that since it's an array, Array#shift has O(n) time complexity\npop = queue.shift\n\n# Get queue length\nsize = queue.length\n\n# Check if queue is empty\nis_empty = queue.empty?\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_stack_and_queue/queue/#522-queue-implementation","title":"5.2.2 \u00a0 Queue Implementation","text":"

      To implement a queue, we need a data structure that allows adding elements at one end and removing elements at the other end. Both linked lists and arrays meet this requirement.

      "},{"location":"chapter_stack_and_queue/queue/#1-linked-list-implementation","title":"1. \u00a0 Linked List Implementation","text":"

      As shown in Figure 5-5, we can treat the \"head node\" and \"tail node\" of a linked list as the \"front\" and \"rear\" of the queue, respectively, with the rule that nodes can only be added at the rear and removed from the front.

      LinkedListQueuepush()pop()

      Figure 5-5 \u00a0 Enqueue and dequeue operations in linked list implementation of queue

      Below is the code for implementing a queue using a linked list:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linkedlist_queue.py
      class LinkedListQueue:\n    \"\"\"Queue based on linked list implementation\"\"\"\n\n    def __init__(self):\n        \"\"\"Constructor\"\"\"\n        self._front: ListNode | None = None  # Head node front\n        self._rear: ListNode | None = None  # Tail node rear\n        self._size: int = 0\n\n    def size(self) -> int:\n        \"\"\"Get the length of the queue\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"Check if the queue is empty\"\"\"\n        return self._size == 0\n\n    def push(self, num: int):\n        \"\"\"Enqueue\"\"\"\n        # Add num after the tail node\n        node = ListNode(num)\n        # If the queue is empty, make both front and rear point to the node\n        if self._front is None:\n            self._front = node\n            self._rear = node\n        # If the queue is not empty, add the node after the tail node\n        else:\n            self._rear.next = node\n            self._rear = node\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"Dequeue\"\"\"\n        num = self.peek()\n        # Delete head node\n        self._front = self._front.next\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"Access front of the queue element\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Queue is empty\")\n        return self._front.val\n\n    def to_list(self) -> list[int]:\n        \"\"\"Convert to list for printing\"\"\"\n        queue = []\n        temp = self._front\n        while temp:\n            queue.append(temp.val)\n            temp = temp.next\n        return queue\n
      linkedlist_queue.cpp
      /* Queue based on linked list implementation */\nclass LinkedListQueue {\n  private:\n    ListNode *front, *rear; // Head node front, tail node rear\n    int queSize;\n\n  public:\n    LinkedListQueue() {\n        front = nullptr;\n        rear = nullptr;\n        queSize = 0;\n    }\n\n    ~LinkedListQueue() {\n        // Traverse linked list to delete nodes and free memory\n        freeMemoryLinkedList(front);\n    }\n\n    /* Get the length of the queue */\n    int size() {\n        return queSize;\n    }\n\n    /* Check if the queue is empty */\n    bool isEmpty() {\n        return queSize == 0;\n    }\n\n    /* Enqueue */\n    void push(int num) {\n        // Add num after the tail node\n        ListNode *node = new ListNode(num);\n        // If the queue is empty, make both front and rear point to the node\n        if (front == nullptr) {\n            front = node;\n            rear = node;\n        }\n        // If the queue is not empty, add the node after the tail node\n        else {\n            rear->next = node;\n            rear = node;\n        }\n        queSize++;\n    }\n\n    /* Dequeue */\n    int pop() {\n        int num = peek();\n        // Delete head node\n        ListNode *tmp = front;\n        front = front->next;\n        // Free memory\n        delete tmp;\n        queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    int peek() {\n        if (size() == 0)\n            throw out_of_range(\"Queue is empty\");\n        return front->val;\n    }\n\n    /* Convert linked list to Vector and return */\n    vector<int> toVector() {\n        ListNode *node = front;\n        vector<int> res(size());\n        for (int i = 0; i < res.size(); i++) {\n            res[i] = node->val;\n            node = node->next;\n        }\n        return res;\n    }\n};\n
      linkedlist_queue.java
      /* Queue based on linked list implementation */\nclass LinkedListQueue {\n    private ListNode front, rear; // Head node front, tail node rear\n    private int queSize = 0;\n\n    public LinkedListQueue() {\n        front = null;\n        rear = null;\n    }\n\n    /* Get the length of the queue */\n    public int size() {\n        return queSize;\n    }\n\n    /* Check if the queue is empty */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* Enqueue */\n    public void push(int num) {\n        // Add num after the tail node\n        ListNode node = new ListNode(num);\n        // If the queue is empty, make both front and rear point to the node\n        if (front == null) {\n            front = node;\n            rear = node;\n        // If the queue is not empty, add the node after the tail node\n        } else {\n            rear.next = node;\n            rear = node;\n        }\n        queSize++;\n    }\n\n    /* Dequeue */\n    public int pop() {\n        int num = peek();\n        // Delete head node\n        front = front.next;\n        queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return front.val;\n    }\n\n    /* Convert linked list to Array and return */\n    public int[] toArray() {\n        ListNode node = front;\n        int[] res = new int[size()];\n        for (int i = 0; i < res.length; i++) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
      linkedlist_queue.cs
      /* Queue based on linked list implementation */\nclass LinkedListQueue {\n    ListNode? front, rear;  // Head node front, tail node rear\n    int queSize = 0;\n\n    public LinkedListQueue() {\n        front = null;\n        rear = null;\n    }\n\n    /* Get the length of the queue */\n    public int Size() {\n        return queSize;\n    }\n\n    /* Check if the queue is empty */\n    public bool IsEmpty() {\n        return Size() == 0;\n    }\n\n    /* Enqueue */\n    public void Push(int num) {\n        // Add num after the tail node\n        ListNode node = new(num);\n        // If the queue is empty, make both front and rear point to the node\n        if (front == null) {\n            front = node;\n            rear = node;\n            // If the queue is not empty, add the node after the tail node\n        } else if (rear != null) {\n            rear.next = node;\n            rear = node;\n        }\n        queSize++;\n    }\n\n    /* Dequeue */\n    public int Pop() {\n        int num = Peek();\n        // Delete head node\n        front = front?.next;\n        queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    public int Peek() {\n        if (IsEmpty())\n            throw new Exception();\n        return front!.val;\n    }\n\n    /* Convert linked list to Array and return */\n    public int[] ToArray() {\n        if (front == null)\n            return [];\n\n        ListNode? node = front;\n        int[] res = new int[Size()];\n        for (int i = 0; i < res.Length; i++) {\n            res[i] = node!.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
      linkedlist_queue.go
      /* Queue based on linked list implementation */\ntype linkedListQueue struct {\n    // Use built-in package list to implement queue\n    data *list.List\n}\n\n/* Access front of the queue element */\nfunc newLinkedListQueue() *linkedListQueue {\n    return &linkedListQueue{\n        data: list.New(),\n    }\n}\n\n/* Enqueue */\nfunc (s *linkedListQueue) push(value any) {\n    s.data.PushBack(value)\n}\n\n/* Dequeue */\nfunc (s *linkedListQueue) pop() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Front()\n    s.data.Remove(e)\n    return e.Value\n}\n\n/* Return list for printing */\nfunc (s *linkedListQueue) peek() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Front()\n    return e.Value\n}\n\n/* Get the length of the queue */\nfunc (s *linkedListQueue) size() int {\n    return s.data.Len()\n}\n\n/* Check if the queue is empty */\nfunc (s *linkedListQueue) isEmpty() bool {\n    return s.data.Len() == 0\n}\n\n/* Get List for printing */\nfunc (s *linkedListQueue) toList() *list.List {\n    return s.data\n}\n
      linkedlist_queue.swift
      /* Queue based on linked list implementation */\nclass LinkedListQueue {\n    private var front: ListNode? // Head node\n    private var rear: ListNode? // Tail node\n    private var _size: Int\n\n    init() {\n        _size = 0\n    }\n\n    /* Get the length of the queue */\n    func size() -> Int {\n        _size\n    }\n\n    /* Check if the queue is empty */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* Enqueue */\n    func push(num: Int) {\n        // Add num after the tail node\n        let node = ListNode(x: num)\n        // If the queue is empty, make both front and rear point to the node\n        if front == nil {\n            front = node\n            rear = node\n        }\n        // If the queue is not empty, add the node after the tail node\n        else {\n            rear?.next = node\n            rear = node\n        }\n        _size += 1\n    }\n\n    /* Dequeue */\n    @discardableResult\n    func pop() -> Int {\n        let num = peek()\n        // Delete head node\n        front = front?.next\n        _size -= 1\n        return num\n    }\n\n    /* Return list for printing */\n    func peek() -> Int {\n        if isEmpty() {\n            fatalError(\"Queue is empty\")\n        }\n        return front!.val\n    }\n\n    /* Convert linked list to Array and return */\n    func toArray() -> [Int] {\n        var node = front\n        var res = Array(repeating: 0, count: size())\n        for i in res.indices {\n            res[i] = node!.val\n            node = node?.next\n        }\n        return res\n    }\n}\n
      linkedlist_queue.js
      /* Queue based on linked list implementation */\nclass LinkedListQueue {\n    #front; // Front node #front\n    #rear; // Rear node #rear\n    #queSize = 0;\n\n    constructor() {\n        this.#front = null;\n        this.#rear = null;\n    }\n\n    /* Get the length of the queue */\n    get size() {\n        return this.#queSize;\n    }\n\n    /* Check if the queue is empty */\n    isEmpty() {\n        return this.size === 0;\n    }\n\n    /* Enqueue */\n    push(num) {\n        // Add num after the tail node\n        const node = new ListNode(num);\n        // If the queue is empty, make both front and rear point to the node\n        if (!this.#front) {\n            this.#front = node;\n            this.#rear = node;\n            // If the queue is not empty, add the node after the tail node\n        } else {\n            this.#rear.next = node;\n            this.#rear = node;\n        }\n        this.#queSize++;\n    }\n\n    /* Dequeue */\n    pop() {\n        const num = this.peek();\n        // Delete head node\n        this.#front = this.#front.next;\n        this.#queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peek() {\n        if (this.size === 0) throw new Error('Queue is empty');\n        return this.#front.val;\n    }\n\n    /* Convert linked list to Array and return */\n    toArray() {\n        let node = this.#front;\n        const res = new Array(this.size);\n        for (let i = 0; i < res.length; i++) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
      linkedlist_queue.ts
      /* Queue based on linked list implementation */\nclass LinkedListQueue {\n    private front: ListNode | null; // Head node front\n    private rear: ListNode | null; // Tail node rear\n    private queSize: number = 0;\n\n    constructor() {\n        this.front = null;\n        this.rear = null;\n    }\n\n    /* Get the length of the queue */\n    get size(): number {\n        return this.queSize;\n    }\n\n    /* Check if the queue is empty */\n    isEmpty(): boolean {\n        return this.size === 0;\n    }\n\n    /* Enqueue */\n    push(num: number): void {\n        // Add num after the tail node\n        const node = new ListNode(num);\n        // If the queue is empty, make both front and rear point to the node\n        if (!this.front) {\n            this.front = node;\n            this.rear = node;\n            // If the queue is not empty, add the node after the tail node\n        } else {\n            this.rear!.next = node;\n            this.rear = node;\n        }\n        this.queSize++;\n    }\n\n    /* Dequeue */\n    pop(): number {\n        const num = this.peek();\n        if (!this.front) throw new Error('Queue is empty');\n        // Delete head node\n        this.front = this.front.next;\n        this.queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peek(): number {\n        if (this.size === 0) throw new Error('Queue is empty');\n        return this.front!.val;\n    }\n\n    /* Convert linked list to Array and return */\n    toArray(): number[] {\n        let node = this.front;\n        const res = new Array<number>(this.size);\n        for (let i = 0; i < res.length; i++) {\n            res[i] = node!.val;\n            node = node!.next;\n        }\n        return res;\n    }\n}\n
      linkedlist_queue.dart
      /* Queue based on linked list implementation */\nclass LinkedListQueue {\n  ListNode? _front; // Head node _front\n  ListNode? _rear; // Tail node _rear\n  int _queSize = 0; // Queue length\n\n  LinkedListQueue() {\n    _front = null;\n    _rear = null;\n  }\n\n  /* Get the length of the queue */\n  int size() {\n    return _queSize;\n  }\n\n  /* Check if the queue is empty */\n  bool isEmpty() {\n    return _queSize == 0;\n  }\n\n  /* Enqueue */\n  void push(int _num) {\n    // Add _num after tail node\n    final node = ListNode(_num);\n    // If the queue is empty, make both front and rear point to the node\n    if (_front == null) {\n      _front = node;\n      _rear = node;\n    } else {\n      // If the queue is not empty, add the node after the tail node\n      _rear!.next = node;\n      _rear = node;\n    }\n    _queSize++;\n  }\n\n  /* Dequeue */\n  int pop() {\n    final int _num = peek();\n    // Delete head node\n    _front = _front!.next;\n    _queSize--;\n    return _num;\n  }\n\n  /* Return list for printing */\n  int peek() {\n    if (_queSize == 0) {\n      throw Exception('Queue is empty');\n    }\n    return _front!.val;\n  }\n\n  /* Convert linked list to Array and return */\n  List<int> toArray() {\n    ListNode? node = _front;\n    final List<int> queue = [];\n    while (node != null) {\n      queue.add(node.val);\n      node = node.next;\n    }\n    return queue;\n  }\n}\n
      linkedlist_queue.rs
      /* Queue based on linked list implementation */\n#[allow(dead_code)]\npub struct LinkedListQueue<T> {\n    front: Option<Rc<RefCell<ListNode<T>>>>, // Head node front\n    rear: Option<Rc<RefCell<ListNode<T>>>>,  // Tail node rear\n    que_size: usize,                         // Queue length\n}\n\nimpl<T: Copy> LinkedListQueue<T> {\n    pub fn new() -> Self {\n        Self {\n            front: None,\n            rear: None,\n            que_size: 0,\n        }\n    }\n\n    /* Get the length of the queue */\n    pub fn size(&self) -> usize {\n        return self.que_size;\n    }\n\n    /* Check if the queue is empty */\n    pub fn is_empty(&self) -> bool {\n        return self.que_size == 0;\n    }\n\n    /* Enqueue */\n    pub fn push(&mut self, num: T) {\n        // Add num after the tail node\n        let new_rear = ListNode::new(num);\n        match self.rear.take() {\n            // If the queue is not empty, add the node after the tail node\n            Some(old_rear) => {\n                old_rear.borrow_mut().next = Some(new_rear.clone());\n                self.rear = Some(new_rear);\n            }\n            // If the queue is empty, make both front and rear point to the node\n            None => {\n                self.front = Some(new_rear.clone());\n                self.rear = Some(new_rear);\n            }\n        }\n        self.que_size += 1;\n    }\n\n    /* Dequeue */\n    pub fn pop(&mut self) -> Option<T> {\n        self.front.take().map(|old_front| {\n            match old_front.borrow_mut().next.take() {\n                Some(new_front) => {\n                    self.front = Some(new_front);\n                }\n                None => {\n                    self.rear.take();\n                }\n            }\n            self.que_size -= 1;\n            old_front.borrow().val\n        })\n    }\n\n    /* Return list for printing */\n    pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n        self.front.as_ref()\n    }\n\n    /* Convert linked list to Array and return */\n    pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n        let mut res: Vec<T> = Vec::new();\n\n        fn recur<T: Copy>(cur: Option<&Rc<RefCell<ListNode<T>>>>, res: &mut Vec<T>) {\n            if let Some(cur) = cur {\n                res.push(cur.borrow().val);\n                recur(cur.borrow().next.as_ref(), res);\n            }\n        }\n\n        recur(head, &mut res);\n\n        res\n    }\n}\n
      linkedlist_queue.c
      /* Queue based on linked list implementation */\ntypedef struct {\n    ListNode *front, *rear;\n    int queSize;\n} LinkedListQueue;\n\n/* Constructor */\nLinkedListQueue *newLinkedListQueue() {\n    LinkedListQueue *queue = (LinkedListQueue *)malloc(sizeof(LinkedListQueue));\n    queue->front = NULL;\n    queue->rear = NULL;\n    queue->queSize = 0;\n    return queue;\n}\n\n/* Destructor */\nvoid delLinkedListQueue(LinkedListQueue *queue) {\n    // Free all nodes\n    while (queue->front != NULL) {\n        ListNode *tmp = queue->front;\n        queue->front = queue->front->next;\n        free(tmp);\n    }\n    // Free queue structure\n    free(queue);\n}\n\n/* Get the length of the queue */\nint size(LinkedListQueue *queue) {\n    return queue->queSize;\n}\n\n/* Check if the queue is empty */\nbool empty(LinkedListQueue *queue) {\n    return (size(queue) == 0);\n}\n\n/* Enqueue */\nvoid push(LinkedListQueue *queue, int num) {\n    // Add node at tail\n    ListNode *node = newListNode(num);\n    // If the queue is empty, make both front and rear point to the node\n    if (queue->front == NULL) {\n        queue->front = node;\n        queue->rear = node;\n    }\n    // If the queue is not empty, add the node after the tail node\n    else {\n        queue->rear->next = node;\n        queue->rear = node;\n    }\n    queue->queSize++;\n}\n\n/* Return list for printing */\nint peek(LinkedListQueue *queue) {\n    assert(size(queue) && queue->front);\n    return queue->front->val;\n}\n\n/* Dequeue */\nint pop(LinkedListQueue *queue) {\n    int num = peek(queue);\n    ListNode *tmp = queue->front;\n    queue->front = queue->front->next;\n    free(tmp);\n    queue->queSize--;\n    return num;\n}\n\n/* Print queue */\nvoid printLinkedListQueue(LinkedListQueue *queue) {\n    int *arr = malloc(sizeof(int) * queue->queSize);\n    // Copy data from list to array\n    int i;\n    ListNode *node;\n    for (i = 0, node = queue->front; i < queue->queSize; i++) {\n        arr[i] = node->val;\n        node = node->next;\n    }\n    printArray(arr, queue->queSize);\n    free(arr);\n}\n
      linkedlist_queue.kt
      /* Queue based on linked list implementation */\nclass LinkedListQueue(\n    // Head node front, tail node rear\n    private var front: ListNode? = null,\n    private var rear: ListNode? = null,\n    private var queSize: Int = 0\n) {\n\n    /* Get the length of the queue */\n    fun size(): Int {\n        return queSize\n    }\n\n    /* Check if the queue is empty */\n    fun isEmpty(): Boolean {\n        return size() == 0\n    }\n\n    /* Enqueue */\n    fun push(num: Int) {\n        // Add num after the tail node\n        val node = ListNode(num)\n        // If the queue is empty, make both front and rear point to the node\n        if (front == null) {\n            front = node\n            rear = node\n            // If the queue is not empty, add the node after the tail node\n        } else {\n            rear?.next = node\n            rear = node\n        }\n        queSize++\n    }\n\n    /* Dequeue */\n    fun pop(): Int {\n        val num = peek()\n        // Delete head node\n        front = front?.next\n        queSize--\n        return num\n    }\n\n    /* Return list for printing */\n    fun peek(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return front!!._val\n    }\n\n    /* Convert linked list to Array and return */\n    fun toArray(): IntArray {\n        var node = front\n        val res = IntArray(size())\n        for (i in res.indices) {\n            res[i] = node!!._val\n            node = node.next\n        }\n        return res\n    }\n}\n
      linkedlist_queue.rb
      ### Queue based on linked list ###\nclass LinkedListQueue\n  ### Get queue length ###\n  attr_reader :size\n\n  ### Constructor ###\n  def initialize\n    @front = nil  # Head node front\n    @rear = nil   # Tail node rear\n    @size = 0\n  end\n\n  ### Check if queue is empty ###\n  def is_empty?\n    @front.nil?\n  end\n\n  ### Enqueue ###\n  def push(num)\n    # Add num after the tail node\n    node = ListNode.new(num)\n\n    # If queue is empty, set both front and rear to this node\n    if @front.nil?\n      @front = node\n      @rear = node\n    # If queue is not empty, add this node after rear\n    else\n      @rear.next = node\n      @rear = node\n    end\n\n    @size += 1\n  end\n\n  ### Dequeue ###\n  def pop\n    num = peek\n    # Delete head node\n    @front = @front.next\n    @size -= 1\n    num\n  end\n\n  ### Access front element ###\n  def peek\n    raise IndexError, 'Queue is empty' if is_empty?\n\n    @front.val\n  end\n\n  ### Convert linked list to Array and return ###\n  def to_array\n    queue = []\n    temp = @front\n    while temp\n      queue << temp.val\n      temp = temp.next\n    end\n    queue\n  end\nend\n
      "},{"location":"chapter_stack_and_queue/queue/#2-array-implementation","title":"2. \u00a0 Array Implementation","text":"

      Deleting the first element in an array has a time complexity of \\(O(n)\\), which would make the dequeue operation inefficient. However, we can use the following clever method to avoid this problem.

      We can use a variable front to point to the index of the front element and maintain a variable size to record the queue length. We define rear = front + size, which calculates the position right after the rear element.

      Based on this design, the valid interval containing elements in the array is [front, rear - 1]. The implementation methods for various operations are shown in Figure 5-6:

      • Enqueue operation: Assign the input element to the rear index and increase size by 1.
      • Dequeue operation: Simply increase front by 1 and decrease size by 1.

      As you can see, both enqueue and dequeue operations require only one operation, with a time complexity of \\(O(1)\\).

      ArrayQueuepush()pop()

      Figure 5-6 \u00a0 Enqueue and dequeue operations in array implementation of queue

      You may notice a problem: as we continuously enqueue and dequeue, both front and rear move to the right. When they reach the end of the array, they cannot continue moving. To solve this problem, we can treat the array as a \"circular array\" with head and tail connected.

      For a circular array, we need to let front or rear wrap around to the beginning of the array when they cross the end. This periodic pattern can be implemented using the \"modulo operation,\" as shown in the code below:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_queue.py
      class ArrayQueue:\n    \"\"\"Queue based on circular array implementation\"\"\"\n\n    def __init__(self, size: int):\n        \"\"\"Constructor\"\"\"\n        self._nums: list[int] = [0] * size  # Array for storing queue elements\n        self._front: int = 0  # Front pointer, points to the front of the queue element\n        self._size: int = 0  # Queue length\n\n    def capacity(self) -> int:\n        \"\"\"Get the capacity of the queue\"\"\"\n        return len(self._nums)\n\n    def size(self) -> int:\n        \"\"\"Get the length of the queue\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"Check if the queue is empty\"\"\"\n        return self._size == 0\n\n    def push(self, num: int):\n        \"\"\"Enqueue\"\"\"\n        if self._size == self.capacity():\n            raise IndexError(\"Queue is full\")\n        # Calculate rear pointer, points to rear index + 1\n        # Use modulo operation to wrap rear around to the head after passing the tail of the array\n        rear: int = (self._front + self._size) % self.capacity()\n        # Add num to the rear of the queue\n        self._nums[rear] = num\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"Dequeue\"\"\"\n        num: int = self.peek()\n        # Front pointer moves one position backward, if it passes the tail, return to the head of the array\n        self._front = (self._front + 1) % self.capacity()\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"Access front of the queue element\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Queue is empty\")\n        return self._nums[self._front]\n\n    def to_list(self) -> list[int]:\n        \"\"\"Return list for printing\"\"\"\n        res = [0] * self.size()\n        j: int = self._front\n        for i in range(self.size()):\n            res[i] = self._nums[(j % self.capacity())]\n            j += 1\n        return res\n
      array_queue.cpp
      /* Queue based on circular array implementation */\nclass ArrayQueue {\n  private:\n    int *nums;       // Array for storing queue elements\n    int front;       // Front pointer, points to the front of the queue element\n    int queSize;     // Queue length\n    int queCapacity; // Queue capacity\n\n  public:\n    ArrayQueue(int capacity) {\n        // Initialize array\n        nums = new int[capacity];\n        queCapacity = capacity;\n        front = queSize = 0;\n    }\n\n    ~ArrayQueue() {\n        delete[] nums;\n    }\n\n    /* Get the capacity of the queue */\n    int capacity() {\n        return queCapacity;\n    }\n\n    /* Get the length of the queue */\n    int size() {\n        return queSize;\n    }\n\n    /* Check if the queue is empty */\n    bool isEmpty() {\n        return size() == 0;\n    }\n\n    /* Enqueue */\n    void push(int num) {\n        if (queSize == queCapacity) {\n            cout << \"Queue is full\" << endl;\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        // Add num to the rear of the queue\n        int rear = (front + queSize) % queCapacity;\n        // Front pointer moves one position backward\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* Dequeue */\n    int pop() {\n        int num = peek();\n        // Move front pointer backward by one position, if it passes the tail, return to array head\n        front = (front + 1) % queCapacity;\n        queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    int peek() {\n        if (isEmpty())\n            throw out_of_range(\"Queue is empty\");\n        return nums[front];\n    }\n\n    /* Convert array to Vector and return */\n    vector<int> toVector() {\n        // Elements enqueue\n        vector<int> arr(queSize);\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            arr[i] = nums[j % queCapacity];\n        }\n        return arr;\n    }\n};\n
      array_queue.java
      /* Queue based on circular array implementation */\nclass ArrayQueue {\n    private int[] nums; // Array for storing queue elements\n    private int front; // Front pointer, points to the front of the queue element\n    private int queSize; // Queue length\n\n    public ArrayQueue(int capacity) {\n        nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* Get the capacity of the queue */\n    public int capacity() {\n        return nums.length;\n    }\n\n    /* Get the length of the queue */\n    public int size() {\n        return queSize;\n    }\n\n    /* Check if the queue is empty */\n    public boolean isEmpty() {\n        return queSize == 0;\n    }\n\n    /* Enqueue */\n    public void push(int num) {\n        if (queSize == capacity()) {\n            System.out.println(\"Queue is full\");\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        // Add num to the rear of the queue\n        int rear = (front + queSize) % capacity();\n        // Front pointer moves one position backward\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* Dequeue */\n    public int pop() {\n        int num = peek();\n        // Move front pointer backward by one position, if it passes the tail, return to array head\n        front = (front + 1) % capacity();\n        queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return nums[front];\n    }\n\n    /* Return array */\n    public int[] toArray() {\n        // Elements enqueue\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[j % capacity()];\n        }\n        return res;\n    }\n}\n
      array_queue.cs
      /* Queue based on circular array implementation */\nclass ArrayQueue {\n    int[] nums;  // Array for storing queue elements\n    int front;   // Front pointer, points to the front of the queue element\n    int queSize; // Queue length\n\n    public ArrayQueue(int capacity) {\n        nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* Get the capacity of the queue */\n    int Capacity() {\n        return nums.Length;\n    }\n\n    /* Get the length of the queue */\n    public int Size() {\n        return queSize;\n    }\n\n    /* Check if the queue is empty */\n    public bool IsEmpty() {\n        return queSize == 0;\n    }\n\n    /* Enqueue */\n    public void Push(int num) {\n        if (queSize == Capacity()) {\n            Console.WriteLine(\"Queue is full\");\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        // Add num to the rear of the queue\n        int rear = (front + queSize) % Capacity();\n        // Front pointer moves one position backward\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* Dequeue */\n    public int Pop() {\n        int num = Peek();\n        // Move front pointer backward by one position, if it passes the tail, return to array head\n        front = (front + 1) % Capacity();\n        queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    public int Peek() {\n        if (IsEmpty())\n            throw new Exception();\n        return nums[front];\n    }\n\n    /* Return array */\n    public int[] ToArray() {\n        // Elements enqueue\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[j % this.Capacity()];\n        }\n        return res;\n    }\n}\n
      array_queue.go
      /* Queue based on circular array implementation */\ntype arrayQueue struct {\n    nums        []int // Array for storing queue elements\n    front       int   // Front pointer, points to the front of the queue element\n    queSize     int   // Queue length\n    queCapacity int   // Queue capacity (maximum number of elements)\n}\n\n/* Access front of the queue element */\nfunc newArrayQueue(queCapacity int) *arrayQueue {\n    return &arrayQueue{\n        nums:        make([]int, queCapacity),\n        queCapacity: queCapacity,\n        front:       0,\n        queSize:     0,\n    }\n}\n\n/* Get the length of the queue */\nfunc (q *arrayQueue) size() int {\n    return q.queSize\n}\n\n/* Check if the queue is empty */\nfunc (q *arrayQueue) isEmpty() bool {\n    return q.queSize == 0\n}\n\n/* Enqueue */\nfunc (q *arrayQueue) push(num int) {\n    // When rear == queCapacity, queue is full\n    if q.queSize == q.queCapacity {\n        return\n    }\n    // Use modulo operation to wrap rear around to the head after passing the tail of the array\n    // Add num to the rear of the queue\n    rear := (q.front + q.queSize) % q.queCapacity\n    // Front pointer moves one position backward\n    q.nums[rear] = num\n    q.queSize++\n}\n\n/* Dequeue */\nfunc (q *arrayQueue) pop() any {\n    num := q.peek()\n    if num == nil {\n        return nil\n    }\n\n    // Move front pointer backward by one position, if it passes the tail, return to array head\n    q.front = (q.front + 1) % q.queCapacity\n    q.queSize--\n    return num\n}\n\n/* Return list for printing */\nfunc (q *arrayQueue) peek() any {\n    if q.isEmpty() {\n        return nil\n    }\n    return q.nums[q.front]\n}\n\n/* Get Slice for printing */\nfunc (q *arrayQueue) toSlice() []int {\n    rear := (q.front + q.queSize)\n    if rear >= q.queCapacity {\n        rear %= q.queCapacity\n        return append(q.nums[q.front:], q.nums[:rear]...)\n    }\n    return q.nums[q.front:rear]\n}\n
      array_queue.swift
      /* Queue based on circular array implementation */\nclass ArrayQueue {\n    private var nums: [Int] // Array for storing queue elements\n    private var front: Int // Front pointer, points to the front of the queue element\n    private var _size: Int // Queue length\n\n    init(capacity: Int) {\n        // Initialize array\n        nums = Array(repeating: 0, count: capacity)\n        front = 0\n        _size = 0\n    }\n\n    /* Get the capacity of the queue */\n    func capacity() -> Int {\n        nums.count\n    }\n\n    /* Get the length of the queue */\n    func size() -> Int {\n        _size\n    }\n\n    /* Check if the queue is empty */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* Enqueue */\n    func push(num: Int) {\n        if size() == capacity() {\n            print(\"Queue is full\")\n            return\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        // Add num to the rear of the queue\n        let rear = (front + size()) % capacity()\n        // Front pointer moves one position backward\n        nums[rear] = num\n        _size += 1\n    }\n\n    /* Dequeue */\n    @discardableResult\n    func pop() -> Int {\n        let num = peek()\n        // Move front pointer backward by one position, if it passes the tail, return to array head\n        front = (front + 1) % capacity()\n        _size -= 1\n        return num\n    }\n\n    /* Return list for printing */\n    func peek() -> Int {\n        if isEmpty() {\n            fatalError(\"Queue is empty\")\n        }\n        return nums[front]\n    }\n\n    /* Return array */\n    func toArray() -> [Int] {\n        // Elements enqueue\n        (front ..< front + size()).map { nums[$0 % capacity()] }\n    }\n}\n
      array_queue.js
      /* Queue based on circular array implementation */\nclass ArrayQueue {\n    #nums; // Array for storing queue elements\n    #front = 0; // Front pointer, points to the front of the queue element\n    #queSize = 0; // Queue length\n\n    constructor(capacity) {\n        this.#nums = new Array(capacity);\n    }\n\n    /* Get the capacity of the queue */\n    get capacity() {\n        return this.#nums.length;\n    }\n\n    /* Get the length of the queue */\n    get size() {\n        return this.#queSize;\n    }\n\n    /* Check if the queue is empty */\n    isEmpty() {\n        return this.#queSize === 0;\n    }\n\n    /* Enqueue */\n    push(num) {\n        if (this.size === this.capacity) {\n            console.log('Queue is full');\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        // Add num to the rear of the queue\n        const rear = (this.#front + this.size) % this.capacity;\n        // Front pointer moves one position backward\n        this.#nums[rear] = num;\n        this.#queSize++;\n    }\n\n    /* Dequeue */\n    pop() {\n        const num = this.peek();\n        // Move front pointer backward by one position, if it passes the tail, return to array head\n        this.#front = (this.#front + 1) % this.capacity;\n        this.#queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peek() {\n        if (this.isEmpty()) throw new Error('Queue is empty');\n        return this.#nums[this.#front];\n    }\n\n    /* Return Array */\n    toArray() {\n        // Elements enqueue\n        const arr = new Array(this.size);\n        for (let i = 0, j = this.#front; i < this.size; i++, j++) {\n            arr[i] = this.#nums[j % this.capacity];\n        }\n        return arr;\n    }\n}\n
      array_queue.ts
      /* Queue based on circular array implementation */\nclass ArrayQueue {\n    private nums: number[]; // Array for storing queue elements\n    private front: number; // Front pointer, points to the front of the queue element\n    private queSize: number; // Queue length\n\n    constructor(capacity: number) {\n        this.nums = new Array(capacity);\n        this.front = this.queSize = 0;\n    }\n\n    /* Get the capacity of the queue */\n    get capacity(): number {\n        return this.nums.length;\n    }\n\n    /* Get the length of the queue */\n    get size(): number {\n        return this.queSize;\n    }\n\n    /* Check if the queue is empty */\n    isEmpty(): boolean {\n        return this.queSize === 0;\n    }\n\n    /* Enqueue */\n    push(num: number): void {\n        if (this.size === this.capacity) {\n            console.log('Queue is full');\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        // Add num to the rear of the queue\n        const rear = (this.front + this.queSize) % this.capacity;\n        // Front pointer moves one position backward\n        this.nums[rear] = num;\n        this.queSize++;\n    }\n\n    /* Dequeue */\n    pop(): number {\n        const num = this.peek();\n        // Move front pointer backward by one position, if it passes the tail, return to array head\n        this.front = (this.front + 1) % this.capacity;\n        this.queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peek(): number {\n        if (this.isEmpty()) throw new Error('Queue is empty');\n        return this.nums[this.front];\n    }\n\n    /* Return Array */\n    toArray(): number[] {\n        // Elements enqueue\n        const arr = new Array(this.size);\n        for (let i = 0, j = this.front; i < this.size; i++, j++) {\n            arr[i] = this.nums[j % this.capacity];\n        }\n        return arr;\n    }\n}\n
      array_queue.dart
      /* Queue based on circular array implementation */\nclass ArrayQueue {\n  late List<int> _nums; // Array for storing queue elements\n  late int _front; // Front pointer, points to the front of the queue element\n  late int _queSize; // Queue length\n\n  ArrayQueue(int capacity) {\n    _nums = List.filled(capacity, 0);\n    _front = _queSize = 0;\n  }\n\n  /* Get the capacity of the queue */\n  int capaCity() {\n    return _nums.length;\n  }\n\n  /* Get the length of the queue */\n  int size() {\n    return _queSize;\n  }\n\n  /* Check if the queue is empty */\n  bool isEmpty() {\n    return _queSize == 0;\n  }\n\n  /* Enqueue */\n  void push(int _num) {\n    if (_queSize == capaCity()) {\n      throw Exception(\"Queue is full\");\n    }\n    // Use modulo operation to wrap rear around to the head after passing the tail of the array\n    // Add num to the rear of the queue\n    int rear = (_front + _queSize) % capaCity();\n    // Add _num to queue rear\n    _nums[rear] = _num;\n    _queSize++;\n  }\n\n  /* Dequeue */\n  int pop() {\n    int _num = peek();\n    // Move front pointer backward by one position, if it passes the tail, return to array head\n    _front = (_front + 1) % capaCity();\n    _queSize--;\n    return _num;\n  }\n\n  /* Return list for printing */\n  int peek() {\n    if (isEmpty()) {\n      throw Exception(\"Queue is empty\");\n    }\n    return _nums[_front];\n  }\n\n  /* Return Array */\n  List<int> toArray() {\n    // Elements enqueue\n    final List<int> res = List.filled(_queSize, 0);\n    for (int i = 0, j = _front; i < _queSize; i++, j++) {\n      res[i] = _nums[j % capaCity()];\n    }\n    return res;\n  }\n}\n
      array_queue.rs
      /* Queue based on circular array implementation */\nstruct ArrayQueue<T> {\n    nums: Vec<T>,      // Array for storing queue elements\n    front: i32,        // Front pointer, points to the front of the queue element\n    que_size: i32,     // Queue length\n    que_capacity: i32, // Queue capacity\n}\n\nimpl<T: Copy + Default> ArrayQueue<T> {\n    /* Constructor */\n    fn new(capacity: i32) -> ArrayQueue<T> {\n        ArrayQueue {\n            nums: vec![T::default(); capacity as usize],\n            front: 0,\n            que_size: 0,\n            que_capacity: capacity,\n        }\n    }\n\n    /* Get the capacity of the queue */\n    fn capacity(&self) -> i32 {\n        self.que_capacity\n    }\n\n    /* Get the length of the queue */\n    fn size(&self) -> i32 {\n        self.que_size\n    }\n\n    /* Check if the queue is empty */\n    fn is_empty(&self) -> bool {\n        self.que_size == 0\n    }\n\n    /* Enqueue */\n    fn push(&mut self, num: T) {\n        if self.que_size == self.capacity() {\n            println!(\"Queue is full\");\n            return;\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        // Add num to the rear of the queue\n        let rear = (self.front + self.que_size) % self.que_capacity;\n        // Front pointer moves one position backward\n        self.nums[rear as usize] = num;\n        self.que_size += 1;\n    }\n\n    /* Dequeue */\n    fn pop(&mut self) -> T {\n        let num = self.peek();\n        // Move front pointer backward by one position, if it passes the tail, return to array head\n        self.front = (self.front + 1) % self.que_capacity;\n        self.que_size -= 1;\n        num\n    }\n\n    /* Return list for printing */\n    fn peek(&self) -> T {\n        if self.is_empty() {\n            panic!(\"index out of bounds\");\n        }\n        self.nums[self.front as usize]\n    }\n\n    /* Return array */\n    fn to_vector(&self) -> Vec<T> {\n        let cap = self.que_capacity;\n        let mut j = self.front;\n        let mut arr = vec![T::default(); cap as usize];\n        for i in 0..self.que_size {\n            arr[i as usize] = self.nums[(j % cap) as usize];\n            j += 1;\n        }\n        arr\n    }\n}\n
      array_queue.c
      /* Queue based on circular array implementation */\ntypedef struct {\n    int *nums;       // Array for storing queue elements\n    int front;       // Front pointer, points to the front of the queue element\n    int queSize;     // Rear pointer, points to rear + 1\n    int queCapacity; // Queue capacity\n} ArrayQueue;\n\n/* Constructor */\nArrayQueue *newArrayQueue(int capacity) {\n    ArrayQueue *queue = (ArrayQueue *)malloc(sizeof(ArrayQueue));\n    // Initialize array\n    queue->queCapacity = capacity;\n    queue->nums = (int *)malloc(sizeof(int) * queue->queCapacity);\n    queue->front = queue->queSize = 0;\n    return queue;\n}\n\n/* Destructor */\nvoid delArrayQueue(ArrayQueue *queue) {\n    free(queue->nums);\n    free(queue);\n}\n\n/* Get the capacity of the queue */\nint capacity(ArrayQueue *queue) {\n    return queue->queCapacity;\n}\n\n/* Get the length of the queue */\nint size(ArrayQueue *queue) {\n    return queue->queSize;\n}\n\n/* Check if the queue is empty */\nbool empty(ArrayQueue *queue) {\n    return queue->queSize == 0;\n}\n\n/* Return list for printing */\nint peek(ArrayQueue *queue) {\n    assert(size(queue) != 0);\n    return queue->nums[queue->front];\n}\n\n/* Enqueue */\nvoid push(ArrayQueue *queue, int num) {\n    if (size(queue) == capacity(queue)) {\n        printf(\"Queue is full\\r\\n\");\n        return;\n    }\n    // Use modulo operation to wrap rear around to the head after passing the tail of the array\n    // Add num to the rear of the queue\n    int rear = (queue->front + queue->queSize) % queue->queCapacity;\n    // Front pointer moves one position backward\n    queue->nums[rear] = num;\n    queue->queSize++;\n}\n\n/* Dequeue */\nint pop(ArrayQueue *queue) {\n    int num = peek(queue);\n    // Move front pointer backward by one position, if it passes the tail, return to array head\n    queue->front = (queue->front + 1) % queue->queCapacity;\n    queue->queSize--;\n    return num;\n}\n\n/* Return array for printing */\nint *toArray(ArrayQueue *queue, int *queSize) {\n    *queSize = queue->queSize;\n    int *res = (int *)calloc(queue->queSize, sizeof(int));\n    int j = queue->front;\n    for (int i = 0; i < queue->queSize; i++) {\n        res[i] = queue->nums[j % queue->queCapacity];\n        j++;\n    }\n    return res;\n}\n
      array_queue.kt
      /* Queue based on circular array implementation */\nclass ArrayQueue(capacity: Int) {\n    private val nums: IntArray = IntArray(capacity) // Array for storing queue elements\n    private var front: Int = 0 // Front pointer, points to the front of the queue element\n    private var queSize: Int = 0 // Queue length\n\n    /* Get the capacity of the queue */\n    fun capacity(): Int {\n        return nums.size\n    }\n\n    /* Get the length of the queue */\n    fun size(): Int {\n        return queSize\n    }\n\n    /* Check if the queue is empty */\n    fun isEmpty(): Boolean {\n        return queSize == 0\n    }\n\n    /* Enqueue */\n    fun push(num: Int) {\n        if (queSize == capacity()) {\n            println(\"Queue is full\")\n            return\n        }\n        // Use modulo operation to wrap rear around to the head after passing the tail of the array\n        // Add num to the rear of the queue\n        val rear = (front + queSize) % capacity()\n        // Front pointer moves one position backward\n        nums[rear] = num\n        queSize++\n    }\n\n    /* Dequeue */\n    fun pop(): Int {\n        val num = peek()\n        // Move front pointer backward by one position, if it passes the tail, return to array head\n        front = (front + 1) % capacity()\n        queSize--\n        return num\n    }\n\n    /* Return list for printing */\n    fun peek(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return nums[front]\n    }\n\n    /* Return array */\n    fun toArray(): IntArray {\n        // Elements enqueue\n        val res = IntArray(queSize)\n        var i = 0\n        var j = front\n        while (i < queSize) {\n            res[i] = nums[j % capacity()]\n            i++\n            j++\n        }\n        return res\n    }\n}\n
      array_queue.rb
      ### Queue based on circular array ###\nclass ArrayQueue\n  ### Get queue length ###\n  attr_reader :size\n\n  ### Constructor ###\n  def initialize(size)\n    @nums = Array.new(size, 0) # Array for storing queue elements\n    @front = 0 # Front pointer, points to the front of the queue element\n    @size = 0 # Queue length\n  end\n\n  ### Get queue capacity ###\n  def capacity\n    @nums.length\n  end\n\n  ### Check if queue is empty ###\n  def is_empty?\n    size.zero?\n  end\n\n  ### Enqueue ###\n  def push(num)\n    raise IndexError, 'Queue is full' if size == capacity\n\n    # Use modulo operation to wrap rear around to the head after passing the tail of the array\n    # Add num to the rear of the queue\n    rear = (@front + size) % capacity\n    # Front pointer moves one position backward\n    @nums[rear] = num\n    @size += 1\n  end\n\n  ### Dequeue ###\n  def pop\n    num = peek\n    # Move front pointer backward by one position, if it passes the tail, return to array head\n    @front = (@front + 1) % capacity\n    @size -= 1\n    num\n  end\n\n  ### Access front element ###\n  def peek\n    raise IndexError, 'Queue is empty' if is_empty?\n\n    @nums[@front]\n  end\n\n  ### Return list for printing ###\n  def to_array\n    res = Array.new(size, 0)\n    j = @front\n\n    for i in 0...size\n      res[i] = @nums[j % capacity]\n      j += 1\n    end\n\n    res\n  end\nend\n

      The queue implemented above still has limitations: its length is immutable. However, this problem is not difficult to solve. We can replace the array with a dynamic array to introduce an expansion mechanism. Interested readers can try to implement this themselves.

      The comparison conclusions for the two implementations are consistent with those for stacks and will not be repeated here.

      "},{"location":"chapter_stack_and_queue/queue/#523-typical-applications-of-queue","title":"5.2.3 \u00a0 Typical Applications of Queue","text":"
      • Taobao orders. After shoppers place orders, the orders are added to a queue, and the system subsequently processes the orders in the queue according to their sequence. During Double Eleven, massive orders are generated in a short time, and high concurrency becomes a key challenge that engineers need to tackle.
      • Various to-do tasks. Any scenario that needs to implement \"first come, first served\" functionality, such as a printer's task queue or a restaurant's order queue, can effectively maintain the processing order using queues.
      "},{"location":"chapter_stack_and_queue/stack/","title":"5.1 \u00a0 Stack","text":"

      A stack is a linear data structure that follows the Last In First Out (LIFO) logic.

      We can compare a stack to a pile of plates on a table. If we specify that only one plate can be moved at a time, then to get the bottom plate, we must first remove the plates above it one by one. If we replace the plates with various types of elements (such as integers, characters, objects, etc.), we get the stack data structure.

      As shown in Figure 5-1, we call the top of the stacked elements the \"top\" and the bottom the \"base.\" The operation of adding an element to the top is called \"push,\" and the operation of removing the top element is called \"pop.\"

      Figure 5-1 \u00a0 LIFO rule of stack

      "},{"location":"chapter_stack_and_queue/stack/#511-common-stack-operations","title":"5.1.1 \u00a0 Common Stack Operations","text":"

      The common operations on a stack are shown in Table 5-1. The specific method names depend on the programming language used. Here, we use the common naming convention of push(), pop(), and peek().

      Table 5-1 \u00a0 Efficiency of Stack Operations

      Method Description Time Complexity push() Push element onto stack (add to top) \\(O(1)\\) pop() Pop top element from stack \\(O(1)\\) peek() Access top element \\(O(1)\\)

      Typically, we can directly use the built-in stack class provided by the programming language. However, some languages may not provide a dedicated stack class. In these cases, we can use the language's \"array\" or \"linked list\" as a stack and ignore operations unrelated to the stack in the program logic.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby stack.py
      # Initialize stack\n# Python does not have a built-in stack class, can use list as a stack\nstack: list[int] = []\n\n# Push elements\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n# Access top element\npeek: int = stack[-1]\n\n# Pop element\npop: int = stack.pop()\n\n# Get stack length\nsize: int = len(stack)\n\n# Check if empty\nis_empty: bool = len(stack) == 0\n
      stack.cpp
      /* Initialize stack */\nstack<int> stack;\n\n/* Push elements */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* Access top element */\nint top = stack.top();\n\n/* Pop element */\nstack.pop(); // No return value\n\n/* Get stack length */\nint size = stack.size();\n\n/* Check if empty */\nbool empty = stack.empty();\n
      stack.java
      /* Initialize stack */\nStack<Integer> stack = new Stack<>();\n\n/* Push elements */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* Access top element */\nint peek = stack.peek();\n\n/* Pop element */\nint pop = stack.pop();\n\n/* Get stack length */\nint size = stack.size();\n\n/* Check if empty */\nboolean isEmpty = stack.isEmpty();\n
      stack.cs
      /* Initialize stack */\nStack<int> stack = new();\n\n/* Push elements */\nstack.Push(1);\nstack.Push(3);\nstack.Push(2);\nstack.Push(5);\nstack.Push(4);\n\n/* Access top element */\nint peek = stack.Peek();\n\n/* Pop element */\nint pop = stack.Pop();\n\n/* Get stack length */\nint size = stack.Count;\n\n/* Check if empty */\nbool isEmpty = stack.Count == 0;\n
      stack_test.go
      /* Initialize stack */\n// In Go, it is recommended to use Slice as a stack\nvar stack []int\n\n/* Push elements */\nstack = append(stack, 1)\nstack = append(stack, 3)\nstack = append(stack, 2)\nstack = append(stack, 5)\nstack = append(stack, 4)\n\n/* Access top element */\npeek := stack[len(stack)-1]\n\n/* Pop element */\npop := stack[len(stack)-1]\nstack = stack[:len(stack)-1]\n\n/* Get stack length */\nsize := len(stack)\n\n/* Check if empty */\nisEmpty := len(stack) == 0\n
      stack.swift
      /* Initialize stack */\n// Swift does not have a built-in stack class, can use Array as a stack\nvar stack: [Int] = []\n\n/* Push elements */\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n/* Access top element */\nlet peek = stack.last!\n\n/* Pop element */\nlet pop = stack.removeLast()\n\n/* Get stack length */\nlet size = stack.count\n\n/* Check if empty */\nlet isEmpty = stack.isEmpty\n
      stack.js
      /* Initialize stack */\n// JavaScript does not have a built-in stack class, can use Array as a stack\nconst stack = [];\n\n/* Push elements */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* Access top element */\nconst peek = stack[stack.length-1];\n\n/* Pop element */\nconst pop = stack.pop();\n\n/* Get stack length */\nconst size = stack.length;\n\n/* Check if empty */\nconst is_empty = stack.length === 0;\n
      stack.ts
      /* Initialize stack */\n// TypeScript does not have a built-in stack class, can use Array as a stack\nconst stack: number[] = [];\n\n/* Push elements */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* Access top element */\nconst peek = stack[stack.length - 1];\n\n/* Pop element */\nconst pop = stack.pop();\n\n/* Get stack length */\nconst size = stack.length;\n\n/* Check if empty */\nconst is_empty = stack.length === 0;\n
      stack.dart
      /* Initialize stack */\n// Dart does not have a built-in stack class, can use List as a stack\nList<int> stack = [];\n\n/* Push elements */\nstack.add(1);\nstack.add(3);\nstack.add(2);\nstack.add(5);\nstack.add(4);\n\n/* Access top element */\nint peek = stack.last;\n\n/* Pop element */\nint pop = stack.removeLast();\n\n/* Get stack length */\nint size = stack.length;\n\n/* Check if empty */\nbool isEmpty = stack.isEmpty;\n
      stack.rs
      /* Initialize stack */\n// Use Vec as a stack\nlet mut stack: Vec<i32> = Vec::new();\n\n/* Push elements */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* Access top element */\nlet top = stack.last().unwrap();\n\n/* Pop element */\nlet pop = stack.pop().unwrap();\n\n/* Get stack length */\nlet size = stack.len();\n\n/* Check if empty */\nlet is_empty = stack.is_empty();\n
      stack.c
      // C does not provide a built-in stack\n
      stack.kt
      /* Initialize stack */\nval stack = Stack<Int>()\n\n/* Push elements */\nstack.push(1)\nstack.push(3)\nstack.push(2)\nstack.push(5)\nstack.push(4)\n\n/* Access top element */\nval peek = stack.peek()\n\n/* Pop element */\nval pop = stack.pop()\n\n/* Get stack length */\nval size = stack.size\n\n/* Check if empty */\nval isEmpty = stack.isEmpty()\n
      stack.rb
      # Initialize stack\n# Ruby does not have a built-in stack class, can use Array as a stack\nstack = []\n\n# Push elements\nstack << 1\nstack << 3\nstack << 2\nstack << 5\nstack << 4\n\n# Access top element\npeek = stack.last\n\n# Pop element\npop = stack.pop\n\n# Get stack length\nsize = stack.length\n\n# Check if empty\nis_empty = stack.empty?\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_stack_and_queue/stack/#512-stack-implementation","title":"5.1.2 \u00a0 Stack Implementation","text":"

      To gain a deeper understanding of how a stack operates, let's try implementing a stack class ourselves.

      A stack follows the LIFO principle, so we can only add or remove elements at the top. However, both arrays and linked lists allow adding and removing elements at any position. Therefore, a stack can be viewed as a restricted array or linked list. In other words, we can \"shield\" some irrelevant operations of arrays or linked lists so that their external logic conforms to the characteristics of a stack.

      "},{"location":"chapter_stack_and_queue/stack/#1-linked-list-implementation","title":"1. \u00a0 Linked List Implementation","text":"

      When implementing a stack using a linked list, we can treat the head node of the linked list as the top of the stack and the tail node as the base.

      As shown in Figure 5-2, for the push operation, we simply insert an element at the head of the linked list. This node insertion method is called the \"head insertion method.\" For the pop operation, we just need to remove the head node from the linked list.

      LinkedListStackpush()pop()

      Figure 5-2 \u00a0 Push and pop operations in linked list implementation of stack

      Below is sample code for implementing a stack based on a linked list:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linkedlist_stack.py
      class LinkedListStack:\n    \"\"\"Stack based on linked list implementation\"\"\"\n\n    def __init__(self):\n        \"\"\"Constructor\"\"\"\n        self._peek: ListNode | None = None\n        self._size: int = 0\n\n    def size(self) -> int:\n        \"\"\"Get the length of the stack\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"Check if the stack is empty\"\"\"\n        return self._size == 0\n\n    def push(self, val: int):\n        \"\"\"Push\"\"\"\n        node = ListNode(val)\n        node.next = self._peek\n        self._peek = node\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"Pop\"\"\"\n        num = self.peek()\n        self._peek = self._peek.next\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"Access top of the stack element\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Stack is empty\")\n        return self._peek.val\n\n    def to_list(self) -> list[int]:\n        \"\"\"Convert to list for printing\"\"\"\n        arr = []\n        node = self._peek\n        while node:\n            arr.append(node.val)\n            node = node.next\n        arr.reverse()\n        return arr\n
      linkedlist_stack.cpp
      /* Stack based on linked list implementation */\nclass LinkedListStack {\n  private:\n    ListNode *stackTop; // Use head node as stack top\n    int stkSize;        // Stack length\n\n  public:\n    LinkedListStack() {\n        stackTop = nullptr;\n        stkSize = 0;\n    }\n\n    ~LinkedListStack() {\n        // Traverse linked list to delete nodes and free memory\n        freeMemoryLinkedList(stackTop);\n    }\n\n    /* Get the length of the stack */\n    int size() {\n        return stkSize;\n    }\n\n    /* Check if the stack is empty */\n    bool isEmpty() {\n        return size() == 0;\n    }\n\n    /* Push */\n    void push(int num) {\n        ListNode *node = new ListNode(num);\n        node->next = stackTop;\n        stackTop = node;\n        stkSize++;\n    }\n\n    /* Pop */\n    int pop() {\n        int num = top();\n        ListNode *tmp = stackTop;\n        stackTop = stackTop->next;\n        // Free memory\n        delete tmp;\n        stkSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    int top() {\n        if (isEmpty())\n            throw out_of_range(\"Stack is empty\");\n        return stackTop->val;\n    }\n\n    /* Convert List to Array and return */\n    vector<int> toVector() {\n        ListNode *node = stackTop;\n        vector<int> res(size());\n        for (int i = res.size() - 1; i >= 0; i--) {\n            res[i] = node->val;\n            node = node->next;\n        }\n        return res;\n    }\n};\n
      linkedlist_stack.java
      /* Stack based on linked list implementation */\nclass LinkedListStack {\n    private ListNode stackPeek; // Use head node as stack top\n    private int stkSize = 0; // Stack length\n\n    public LinkedListStack() {\n        stackPeek = null;\n    }\n\n    /* Get the length of the stack */\n    public int size() {\n        return stkSize;\n    }\n\n    /* Check if the stack is empty */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* Push */\n    public void push(int num) {\n        ListNode node = new ListNode(num);\n        node.next = stackPeek;\n        stackPeek = node;\n        stkSize++;\n    }\n\n    /* Pop */\n    public int pop() {\n        int num = peek();\n        stackPeek = stackPeek.next;\n        stkSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return stackPeek.val;\n    }\n\n    /* Convert List to Array and return */\n    public int[] toArray() {\n        ListNode node = stackPeek;\n        int[] res = new int[size()];\n        for (int i = res.length - 1; i >= 0; i--) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
      linkedlist_stack.cs
      /* Stack based on linked list implementation */\nclass LinkedListStack {\n    ListNode? stackPeek;  // Use head node as stack top\n    int stkSize = 0;   // Stack length\n\n    public LinkedListStack() {\n        stackPeek = null;\n    }\n\n    /* Get the length of the stack */\n    public int Size() {\n        return stkSize;\n    }\n\n    /* Check if the stack is empty */\n    public bool IsEmpty() {\n        return Size() == 0;\n    }\n\n    /* Push */\n    public void Push(int num) {\n        ListNode node = new(num) {\n            next = stackPeek\n        };\n        stackPeek = node;\n        stkSize++;\n    }\n\n    /* Pop */\n    public int Pop() {\n        int num = Peek();\n        stackPeek = stackPeek!.next;\n        stkSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    public int Peek() {\n        if (IsEmpty())\n            throw new Exception();\n        return stackPeek!.val;\n    }\n\n    /* Convert List to Array and return */\n    public int[] ToArray() {\n        if (stackPeek == null)\n            return [];\n\n        ListNode? node = stackPeek;\n        int[] res = new int[Size()];\n        for (int i = res.Length - 1; i >= 0; i--) {\n            res[i] = node!.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
      linkedlist_stack.go
      /* Stack based on linked list implementation */\ntype linkedListStack struct {\n    // Use built-in package list to implement stack\n    data *list.List\n}\n\n/* Access top of the stack element */\nfunc newLinkedListStack() *linkedListStack {\n    return &linkedListStack{\n        data: list.New(),\n    }\n}\n\n/* Push */\nfunc (s *linkedListStack) push(value int) {\n    s.data.PushBack(value)\n}\n\n/* Pop */\nfunc (s *linkedListStack) pop() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Back()\n    s.data.Remove(e)\n    return e.Value\n}\n\n/* Return list for printing */\nfunc (s *linkedListStack) peek() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Back()\n    return e.Value\n}\n\n/* Get the length of the stack */\nfunc (s *linkedListStack) size() int {\n    return s.data.Len()\n}\n\n/* Check if the stack is empty */\nfunc (s *linkedListStack) isEmpty() bool {\n    return s.data.Len() == 0\n}\n\n/* Get List for printing */\nfunc (s *linkedListStack) toList() *list.List {\n    return s.data\n}\n
      linkedlist_stack.swift
      /* Stack based on linked list implementation */\nclass LinkedListStack {\n    private var _peek: ListNode? // Use head node as stack top\n    private var _size: Int // Stack length\n\n    init() {\n        _size = 0\n    }\n\n    /* Get the length of the stack */\n    func size() -> Int {\n        _size\n    }\n\n    /* Check if the stack is empty */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* Push */\n    func push(num: Int) {\n        let node = ListNode(x: num)\n        node.next = _peek\n        _peek = node\n        _size += 1\n    }\n\n    /* Pop */\n    @discardableResult\n    func pop() -> Int {\n        let num = peek()\n        _peek = _peek?.next\n        _size -= 1\n        return num\n    }\n\n    /* Return list for printing */\n    func peek() -> Int {\n        if isEmpty() {\n            fatalError(\"Stack is empty\")\n        }\n        return _peek!.val\n    }\n\n    /* Convert List to Array and return */\n    func toArray() -> [Int] {\n        var node = _peek\n        var res = Array(repeating: 0, count: size())\n        for i in res.indices.reversed() {\n            res[i] = node!.val\n            node = node?.next\n        }\n        return res\n    }\n}\n
      linkedlist_stack.js
      /* Stack based on linked list implementation */\nclass LinkedListStack {\n    #stackPeek; // Use head node as stack top\n    #stkSize = 0; // Stack length\n\n    constructor() {\n        this.#stackPeek = null;\n    }\n\n    /* Get the length of the stack */\n    get size() {\n        return this.#stkSize;\n    }\n\n    /* Check if the stack is empty */\n    isEmpty() {\n        return this.size === 0;\n    }\n\n    /* Push */\n    push(num) {\n        const node = new ListNode(num);\n        node.next = this.#stackPeek;\n        this.#stackPeek = node;\n        this.#stkSize++;\n    }\n\n    /* Pop */\n    pop() {\n        const num = this.peek();\n        this.#stackPeek = this.#stackPeek.next;\n        this.#stkSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peek() {\n        if (!this.#stackPeek) throw new Error('Stack is empty');\n        return this.#stackPeek.val;\n    }\n\n    /* Convert linked list to Array and return */\n    toArray() {\n        let node = this.#stackPeek;\n        const res = new Array(this.size);\n        for (let i = res.length - 1; i >= 0; i--) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
      linkedlist_stack.ts
      /* Stack based on linked list implementation */\nclass LinkedListStack {\n    private stackPeek: ListNode | null; // Use head node as stack top\n    private stkSize: number = 0; // Stack length\n\n    constructor() {\n        this.stackPeek = null;\n    }\n\n    /* Get the length of the stack */\n    get size(): number {\n        return this.stkSize;\n    }\n\n    /* Check if the stack is empty */\n    isEmpty(): boolean {\n        return this.size === 0;\n    }\n\n    /* Push */\n    push(num: number): void {\n        const node = new ListNode(num);\n        node.next = this.stackPeek;\n        this.stackPeek = node;\n        this.stkSize++;\n    }\n\n    /* Pop */\n    pop(): number {\n        const num = this.peek();\n        if (!this.stackPeek) throw new Error('Stack is empty');\n        this.stackPeek = this.stackPeek.next;\n        this.stkSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peek(): number {\n        if (!this.stackPeek) throw new Error('Stack is empty');\n        return this.stackPeek.val;\n    }\n\n    /* Convert linked list to Array and return */\n    toArray(): number[] {\n        let node = this.stackPeek;\n        const res = new Array<number>(this.size);\n        for (let i = res.length - 1; i >= 0; i--) {\n            res[i] = node!.val;\n            node = node!.next;\n        }\n        return res;\n    }\n}\n
      linkedlist_stack.dart
      /* Stack implemented based on linked list class */\nclass LinkedListStack {\n  ListNode? _stackPeek; // Use head node as stack top\n  int _stkSize = 0; // Stack length\n\n  LinkedListStack() {\n    _stackPeek = null;\n  }\n\n  /* Get the length of the stack */\n  int size() {\n    return _stkSize;\n  }\n\n  /* Check if the stack is empty */\n  bool isEmpty() {\n    return _stkSize == 0;\n  }\n\n  /* Push */\n  void push(int _num) {\n    final ListNode node = ListNode(_num);\n    node.next = _stackPeek;\n    _stackPeek = node;\n    _stkSize++;\n  }\n\n  /* Pop */\n  int pop() {\n    final int _num = peek();\n    _stackPeek = _stackPeek!.next;\n    _stkSize--;\n    return _num;\n  }\n\n  /* Return list for printing */\n  int peek() {\n    if (_stackPeek == null) {\n      throw Exception(\"Stack is empty\");\n    }\n    return _stackPeek!.val;\n  }\n\n  /* Convert linked list to List and return */\n  List<int> toList() {\n    ListNode? node = _stackPeek;\n    List<int> list = [];\n    while (node != null) {\n      list.add(node.val);\n      node = node.next;\n    }\n    list = list.reversed.toList();\n    return list;\n  }\n}\n
      linkedlist_stack.rs
      /* Stack based on linked list implementation */\n#[allow(dead_code)]\npub struct LinkedListStack<T> {\n    stack_peek: Option<Rc<RefCell<ListNode<T>>>>, // Use head node as stack top\n    stk_size: usize,                              // Stack length\n}\n\nimpl<T: Copy> LinkedListStack<T> {\n    pub fn new() -> Self {\n        Self {\n            stack_peek: None,\n            stk_size: 0,\n        }\n    }\n\n    /* Get the length of the stack */\n    pub fn size(&self) -> usize {\n        return self.stk_size;\n    }\n\n    /* Check if the stack is empty */\n    pub fn is_empty(&self) -> bool {\n        return self.size() == 0;\n    }\n\n    /* Push */\n    pub fn push(&mut self, num: T) {\n        let node = ListNode::new(num);\n        node.borrow_mut().next = self.stack_peek.take();\n        self.stack_peek = Some(node);\n        self.stk_size += 1;\n    }\n\n    /* Pop */\n    pub fn pop(&mut self) -> Option<T> {\n        self.stack_peek.take().map(|old_head| {\n            self.stack_peek = old_head.borrow_mut().next.take();\n            self.stk_size -= 1;\n\n            old_head.borrow().val\n        })\n    }\n\n    /* Return list for printing */\n    pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n        self.stack_peek.as_ref()\n    }\n\n    /* Convert List to Array and return */\n    pub fn to_array(&self) -> Vec<T> {\n        fn _to_array<T: Sized + Copy>(head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n            if let Some(node) = head {\n                let mut nums = _to_array(node.borrow().next.as_ref());\n                nums.push(node.borrow().val);\n                return nums;\n            }\n            return Vec::new();\n        }\n\n        _to_array(self.peek())\n    }\n}\n
      linkedlist_stack.c
      /* Stack based on linked list implementation */\ntypedef struct {\n    ListNode *top; // Use head node as stack top\n    int size;      // Stack length\n} LinkedListStack;\n\n/* Constructor */\nLinkedListStack *newLinkedListStack() {\n    LinkedListStack *s = malloc(sizeof(LinkedListStack));\n    s->top = NULL;\n    s->size = 0;\n    return s;\n}\n\n/* Destructor */\nvoid delLinkedListStack(LinkedListStack *s) {\n    while (s->top) {\n        ListNode *n = s->top->next;\n        free(s->top);\n        s->top = n;\n    }\n    free(s);\n}\n\n/* Get the length of the stack */\nint size(LinkedListStack *s) {\n    return s->size;\n}\n\n/* Check if the stack is empty */\nbool isEmpty(LinkedListStack *s) {\n    return size(s) == 0;\n}\n\n/* Push */\nvoid push(LinkedListStack *s, int num) {\n    ListNode *node = (ListNode *)malloc(sizeof(ListNode));\n    node->next = s->top; // Update new node's pointer field\n    node->val = num;     // Update new node's data field\n    s->top = node;       // Update stack top\n    s->size++;           // Update stack size\n}\n\n/* Return list for printing */\nint peek(LinkedListStack *s) {\n    if (s->size == 0) {\n        printf(\"Stack is empty\\n\");\n        return INT_MAX;\n    }\n    return s->top->val;\n}\n\n/* Pop */\nint pop(LinkedListStack *s) {\n    int val = peek(s);\n    ListNode *tmp = s->top;\n    s->top = s->top->next;\n    // Free memory\n    free(tmp);\n    s->size--;\n    return val;\n}\n
      linkedlist_stack.kt
      /* Stack based on linked list implementation */\nclass LinkedListStack(\n    private var stackPeek: ListNode? = null, // Use head node as stack top\n    private var stkSize: Int = 0 // Stack length\n) {\n\n    /* Get the length of the stack */\n    fun size(): Int {\n        return stkSize\n    }\n\n    /* Check if the stack is empty */\n    fun isEmpty(): Boolean {\n        return size() == 0\n    }\n\n    /* Push */\n    fun push(num: Int) {\n        val node = ListNode(num)\n        node.next = stackPeek\n        stackPeek = node\n        stkSize++\n    }\n\n    /* Pop */\n    fun pop(): Int? {\n        val num = peek()\n        stackPeek = stackPeek?.next\n        stkSize--\n        return num\n    }\n\n    /* Return list for printing */\n    fun peek(): Int? {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return stackPeek?._val\n    }\n\n    /* Convert List to Array and return */\n    fun toArray(): IntArray {\n        var node = stackPeek\n        val res = IntArray(size())\n        for (i in res.size - 1 downTo 0) {\n            res[i] = node?._val!!\n            node = node.next\n        }\n        return res\n    }\n}\n
      linkedlist_stack.rb
      ### Stack based on linked list ###\nclass LinkedListStack\n  attr_reader :size\n\n  ### Constructor ###\n  def initialize\n    @size = 0\n  end\n\n  ### Check if stack is empty ###\n  def is_empty?\n    @peek.nil?\n  end\n\n  ### Push ###\n  def push(val)\n    node = ListNode.new(val)\n    node.next = @peek\n    @peek = node\n    @size += 1\n  end\n\n  ### Pop ###\n  def pop\n    num = peek\n    @peek = @peek.next\n    @size -= 1\n    num\n  end\n\n  ### Access top element ###\n  def peek\n    raise IndexError, 'Stack is empty' if is_empty?\n\n    @peek.val\n  end\n\n  ### Convert linked list to Array and return ###\n  def to_array\n    arr = []\n    node = @peek\n    while node\n      arr << node.val\n      node = node.next\n    end\n    arr.reverse\n  end\nend\n
      "},{"location":"chapter_stack_and_queue/stack/#2-array-implementation","title":"2. \u00a0 Array Implementation","text":"

      When implementing a stack using an array, we can treat the end of the array as the top of the stack. As shown in Figure 5-3, push and pop operations correspond to adding and removing elements at the end of the array, both with a time complexity of \\(O(1)\\).

      ArrayStackpush()pop()

      Figure 5-3 \u00a0 Push and pop operations in array implementation of stack

      Since elements pushed onto the stack may increase continuously, we can use a dynamic array, which eliminates the need to handle array expansion ourselves. Here is the sample code:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_stack.py
      class ArrayStack:\n    \"\"\"Stack based on array implementation\"\"\"\n\n    def __init__(self):\n        \"\"\"Constructor\"\"\"\n        self._stack: list[int] = []\n\n    def size(self) -> int:\n        \"\"\"Get the length of the stack\"\"\"\n        return len(self._stack)\n\n    def is_empty(self) -> bool:\n        \"\"\"Check if the stack is empty\"\"\"\n        return self.size() == 0\n\n    def push(self, item: int):\n        \"\"\"Push\"\"\"\n        self._stack.append(item)\n\n    def pop(self) -> int:\n        \"\"\"Pop\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Stack is empty\")\n        return self._stack.pop()\n\n    def peek(self) -> int:\n        \"\"\"Access top of the stack element\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Stack is empty\")\n        return self._stack[-1]\n\n    def to_list(self) -> list[int]:\n        \"\"\"Return list for printing\"\"\"\n        return self._stack\n
      array_stack.cpp
      /* Stack based on array implementation */\nclass ArrayStack {\n  private:\n    vector<int> stack;\n\n  public:\n    /* Get the length of the stack */\n    int size() {\n        return stack.size();\n    }\n\n    /* Check if the stack is empty */\n    bool isEmpty() {\n        return stack.size() == 0;\n    }\n\n    /* Push */\n    void push(int num) {\n        stack.push_back(num);\n    }\n\n    /* Pop */\n    int pop() {\n        int num = top();\n        stack.pop_back();\n        return num;\n    }\n\n    /* Return list for printing */\n    int top() {\n        if (isEmpty())\n            throw out_of_range(\"Stack is empty\");\n        return stack.back();\n    }\n\n    /* Return Vector */\n    vector<int> toVector() {\n        return stack;\n    }\n};\n
      array_stack.java
      /* Stack based on array implementation */\nclass ArrayStack {\n    private ArrayList<Integer> stack;\n\n    public ArrayStack() {\n        // Initialize list (dynamic array)\n        stack = new ArrayList<>();\n    }\n\n    /* Get the length of the stack */\n    public int size() {\n        return stack.size();\n    }\n\n    /* Check if the stack is empty */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* Push */\n    public void push(int num) {\n        stack.add(num);\n    }\n\n    /* Pop */\n    public int pop() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return stack.remove(size() - 1);\n    }\n\n    /* Return list for printing */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return stack.get(size() - 1);\n    }\n\n    /* Convert List to Array and return */\n    public Object[] toArray() {\n        return stack.toArray();\n    }\n}\n
      array_stack.cs
      /* Stack based on array implementation */\nclass ArrayStack {\n    List<int> stack;\n    public ArrayStack() {\n        // Initialize list (dynamic array)\n        stack = [];\n    }\n\n    /* Get the length of the stack */\n    public int Size() {\n        return stack.Count;\n    }\n\n    /* Check if the stack is empty */\n    public bool IsEmpty() {\n        return Size() == 0;\n    }\n\n    /* Push */\n    public void Push(int num) {\n        stack.Add(num);\n    }\n\n    /* Pop */\n    public int Pop() {\n        if (IsEmpty())\n            throw new Exception();\n        var val = Peek();\n        stack.RemoveAt(Size() - 1);\n        return val;\n    }\n\n    /* Return list for printing */\n    public int Peek() {\n        if (IsEmpty())\n            throw new Exception();\n        return stack[Size() - 1];\n    }\n\n    /* Convert List to Array and return */\n    public int[] ToArray() {\n        return [.. stack];\n    }\n}\n
      array_stack.go
      /* Stack based on array implementation */\ntype arrayStack struct {\n    data []int // Data\n}\n\n/* Access top of the stack element */\nfunc newArrayStack() *arrayStack {\n    return &arrayStack{\n        // Set stack length to 0, capacity to 16\n        data: make([]int, 0, 16),\n    }\n}\n\n/* Stack length */\nfunc (s *arrayStack) size() int {\n    return len(s.data)\n}\n\n/* Is stack empty */\nfunc (s *arrayStack) isEmpty() bool {\n    return s.size() == 0\n}\n\n/* Push */\nfunc (s *arrayStack) push(v int) {\n    // Slice will automatically expand\n    s.data = append(s.data, v)\n}\n\n/* Pop */\nfunc (s *arrayStack) pop() any {\n    val := s.peek()\n    s.data = s.data[:len(s.data)-1]\n    return val\n}\n\n/* Get stack top element */\nfunc (s *arrayStack) peek() any {\n    if s.isEmpty() {\n        return nil\n    }\n    val := s.data[len(s.data)-1]\n    return val\n}\n\n/* Get Slice for printing */\nfunc (s *arrayStack) toSlice() []int {\n    return s.data\n}\n
      array_stack.swift
      /* Stack based on array implementation */\nclass ArrayStack {\n    private var stack: [Int]\n\n    init() {\n        // Initialize list (dynamic array)\n        stack = []\n    }\n\n    /* Get the length of the stack */\n    func size() -> Int {\n        stack.count\n    }\n\n    /* Check if the stack is empty */\n    func isEmpty() -> Bool {\n        stack.isEmpty\n    }\n\n    /* Push */\n    func push(num: Int) {\n        stack.append(num)\n    }\n\n    /* Pop */\n    @discardableResult\n    func pop() -> Int {\n        if isEmpty() {\n            fatalError(\"Stack is empty\")\n        }\n        return stack.removeLast()\n    }\n\n    /* Return list for printing */\n    func peek() -> Int {\n        if isEmpty() {\n            fatalError(\"Stack is empty\")\n        }\n        return stack.last!\n    }\n\n    /* Convert List to Array and return */\n    func toArray() -> [Int] {\n        stack\n    }\n}\n
      array_stack.js
      /* Stack based on array implementation */\nclass ArrayStack {\n    #stack;\n    constructor() {\n        this.#stack = [];\n    }\n\n    /* Get the length of the stack */\n    get size() {\n        return this.#stack.length;\n    }\n\n    /* Check if the stack is empty */\n    isEmpty() {\n        return this.#stack.length === 0;\n    }\n\n    /* Push */\n    push(num) {\n        this.#stack.push(num);\n    }\n\n    /* Pop */\n    pop() {\n        if (this.isEmpty()) throw new Error('Stack is empty');\n        return this.#stack.pop();\n    }\n\n    /* Return list for printing */\n    top() {\n        if (this.isEmpty()) throw new Error('Stack is empty');\n        return this.#stack[this.#stack.length - 1];\n    }\n\n    /* Return Array */\n    toArray() {\n        return this.#stack;\n    }\n}\n
      array_stack.ts
      /* Stack based on array implementation */\nclass ArrayStack {\n    private stack: number[];\n    constructor() {\n        this.stack = [];\n    }\n\n    /* Get the length of the stack */\n    get size(): number {\n        return this.stack.length;\n    }\n\n    /* Check if the stack is empty */\n    isEmpty(): boolean {\n        return this.stack.length === 0;\n    }\n\n    /* Push */\n    push(num: number): void {\n        this.stack.push(num);\n    }\n\n    /* Pop */\n    pop(): number | undefined {\n        if (this.isEmpty()) throw new Error('Stack is empty');\n        return this.stack.pop();\n    }\n\n    /* Return list for printing */\n    top(): number | undefined {\n        if (this.isEmpty()) throw new Error('Stack is empty');\n        return this.stack[this.stack.length - 1];\n    }\n\n    /* Return Array */\n    toArray() {\n        return this.stack;\n    }\n}\n
      array_stack.dart
      /* Stack based on array implementation */\nclass ArrayStack {\n  late List<int> _stack;\n  ArrayStack() {\n    _stack = [];\n  }\n\n  /* Get the length of the stack */\n  int size() {\n    return _stack.length;\n  }\n\n  /* Check if the stack is empty */\n  bool isEmpty() {\n    return _stack.isEmpty;\n  }\n\n  /* Push */\n  void push(int _num) {\n    _stack.add(_num);\n  }\n\n  /* Pop */\n  int pop() {\n    if (isEmpty()) {\n      throw Exception(\"Stack is empty\");\n    }\n    return _stack.removeLast();\n  }\n\n  /* Return list for printing */\n  int peek() {\n    if (isEmpty()) {\n      throw Exception(\"Stack is empty\");\n    }\n    return _stack.last;\n  }\n\n  /* Convert stack to Array and return */\n  List<int> toArray() => _stack;\n}\n
      array_stack.rs
      /* Stack based on array implementation */\nstruct ArrayStack<T> {\n    stack: Vec<T>,\n}\n\nimpl<T> ArrayStack<T> {\n    /* Access top of the stack element */\n    fn new() -> ArrayStack<T> {\n        ArrayStack::<T> {\n            stack: Vec::<T>::new(),\n        }\n    }\n\n    /* Get the length of the stack */\n    fn size(&self) -> usize {\n        self.stack.len()\n    }\n\n    /* Check if the stack is empty */\n    fn is_empty(&self) -> bool {\n        self.size() == 0\n    }\n\n    /* Push */\n    fn push(&mut self, num: T) {\n        self.stack.push(num);\n    }\n\n    /* Pop */\n    fn pop(&mut self) -> Option<T> {\n        self.stack.pop()\n    }\n\n    /* Return list for printing */\n    fn peek(&self) -> Option<&T> {\n        if self.is_empty() {\n            panic!(\"Stack is empty\")\n        };\n        self.stack.last()\n    }\n\n    /* Return &Vec */\n    fn to_array(&self) -> &Vec<T> {\n        &self.stack\n    }\n}\n
      array_stack.c
      /* Stack based on array implementation */\ntypedef struct {\n    int *data;\n    int size;\n} ArrayStack;\n\n/* Constructor */\nArrayStack *newArrayStack() {\n    ArrayStack *stack = malloc(sizeof(ArrayStack));\n    // Initialize with large capacity to avoid expansion\n    stack->data = malloc(sizeof(int) * MAX_SIZE);\n    stack->size = 0;\n    return stack;\n}\n\n/* Destructor */\nvoid delArrayStack(ArrayStack *stack) {\n    free(stack->data);\n    free(stack);\n}\n\n/* Get the length of the stack */\nint size(ArrayStack *stack) {\n    return stack->size;\n}\n\n/* Check if the stack is empty */\nbool isEmpty(ArrayStack *stack) {\n    return stack->size == 0;\n}\n\n/* Push */\nvoid push(ArrayStack *stack, int num) {\n    if (stack->size == MAX_SIZE) {\n        printf(\"Stack is full\\n\");\n        return;\n    }\n    stack->data[stack->size] = num;\n    stack->size++;\n}\n\n/* Return list for printing */\nint peek(ArrayStack *stack) {\n    if (stack->size == 0) {\n        printf(\"Stack is empty\\n\");\n        return INT_MAX;\n    }\n    return stack->data[stack->size - 1];\n}\n\n/* Pop */\nint pop(ArrayStack *stack) {\n    int val = peek(stack);\n    stack->size--;\n    return val;\n}\n
      array_stack.kt
      /* Stack based on array implementation */\nclass ArrayStack {\n    // Initialize list (dynamic array)\n    private val stack = mutableListOf<Int>()\n\n    /* Get the length of the stack */\n    fun size(): Int {\n        return stack.size\n    }\n\n    /* Check if the stack is empty */\n    fun isEmpty(): Boolean {\n        return size() == 0\n    }\n\n    /* Push */\n    fun push(num: Int) {\n        stack.add(num)\n    }\n\n    /* Pop */\n    fun pop(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return stack.removeAt(size() - 1)\n    }\n\n    /* Return list for printing */\n    fun peek(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return stack[size() - 1]\n    }\n\n    /* Convert List to Array and return */\n    fun toArray(): Array<Any> {\n        return stack.toTypedArray()\n    }\n}\n
      array_stack.rb
      ### Stack based on array ###\nclass ArrayStack\n  ### Constructor ###\n  def initialize\n    @stack = []\n  end\n\n  ### Get stack length ###\n  def size\n    @stack.length\n  end\n\n  ### Check if stack is empty ###\n  def is_empty?\n    @stack.empty?\n  end\n\n  ### Push ###\n  def push(item)\n    @stack << item\n  end\n\n  ### Pop ###\n  def pop\n    raise IndexError, 'Stack is empty' if is_empty?\n\n    @stack.pop\n  end\n\n  ### Access top element ###\n  def peek\n    raise IndexError, 'Stack is empty' if is_empty?\n\n    @stack.last\n  end\n\n  ### Return list for printing ###\n  def to_array\n    @stack\n  end\nend\n
      "},{"location":"chapter_stack_and_queue/stack/#513-comparison-of-the-two-implementations","title":"5.1.3 \u00a0 Comparison of the Two Implementations","text":"

      Supported Operations

      Both implementations support all operations defined by the stack. The array implementation additionally supports random access, but this goes beyond the stack definition and is generally not used.

      Time Efficiency

      In the array-based implementation, both push and pop operations occur in pre-allocated contiguous memory, which has good cache locality and is therefore more efficient. However, if pushing exceeds the array capacity, it triggers an expansion mechanism, causing the time complexity of that particular push operation to become \\(O(n)\\).

      In the linked list-based implementation, list expansion is very flexible, and there is no issue of reduced efficiency due to array expansion. However, the push operation requires initializing a node object and modifying pointers, so it is relatively less efficient. Nevertheless, if the pushed elements are already node objects, the initialization step can be omitted, thereby improving efficiency.

      In summary, when the elements pushed and popped are basic data types such as int or double, we can draw the following conclusions:

      • The array-based stack implementation has reduced efficiency when expansion is triggered, but since expansion is an infrequent operation, the average efficiency is higher.
      • The linked list-based stack implementation can provide more stable efficiency performance.

      Space Efficiency

      When initializing a list, the system allocates an \"initial capacity\" that may exceed the actual need. Additionally, the expansion mechanism typically expands at a specific ratio (e.g., 2x), and the capacity after expansion may also exceed actual needs. Therefore, the array-based stack implementation may cause some space wastage.

      However, since linked list nodes need to store additional pointers, the space occupied by linked list nodes is relatively large.

      In summary, we cannot simply determine which implementation is more memory-efficient and need to analyze the specific situation.

      "},{"location":"chapter_stack_and_queue/stack/#514-typical-applications-of-stack","title":"5.1.4 \u00a0 Typical Applications of Stack","text":"
      • Back and forward in browsers, undo and redo in software. Every time we open a new webpage, the browser pushes the previous page onto the stack, allowing us to return to the previous page via the back operation. The back operation is essentially performing a pop. To support both back and forward, two stacks are needed to work together.
      • Program memory management. Each time a function is called, the system adds a stack frame to the top of the stack to record the function's context information. During recursion, the downward recursive phase continuously performs push operations, while the upward backtracking phase continuously performs pop operations.
      "},{"location":"chapter_stack_and_queue/summary/","title":"5.4 \u00a0 Summary","text":""},{"location":"chapter_stack_and_queue/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • A stack is a data structure that follows the LIFO principle and can be implemented using arrays or linked lists.
      • In terms of time efficiency, the array implementation of a stack has higher average efficiency, but during expansion, the time complexity of a single push operation degrades to \\(O(n)\\). In contrast, the linked list implementation of a stack provides more stable efficiency performance.
      • In terms of space efficiency, the array implementation of a stack may lead to some degree of space wastage. However, it should be noted that the memory space occupied by linked list nodes is larger than that of array elements.
      • A queue is a data structure that follows the FIFO principle and can also be implemented using arrays or linked lists. The conclusions regarding time efficiency and space efficiency comparisons for queues are similar to those for stacks mentioned above.
      • A deque is a queue with greater flexibility that allows adding and removing elements at both ends.
      "},{"location":"chapter_stack_and_queue/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

      Q: Is the browser's forward and backward functionality implemented with a doubly linked list?

      The forward and backward functionality of a browser is essentially a manifestation of a \"stack.\" When a user visits a new page, that page is added to the top of the stack; when the user clicks the back button, that page is popped from the top of the stack. Using a deque can conveniently implement some additional operations, as mentioned in the \"Deque\" section.

      Q: After popping from the stack, do we need to free the memory of the popped node?

      If the popped node will still be needed later, then memory does not need to be freed. If it won't be used afterward, languages like Java and Python have automatic garbage collection, so manual memory deallocation is not required; in C and C++, manual memory deallocation is necessary.

      Q: A deque seems like two stacks joined together. What is its purpose?

      A deque is like a combination of a stack and a queue, or two stacks joined together. It exhibits the logic of both stack and queue, so it can implement all applications of stacks and queues, and is more flexible.

      Q: How are undo and redo specifically implemented?

      Use two stacks: stack A for undo and stack B for redo.

      1. Whenever the user performs an operation, push this operation onto stack A and clear stack B.
      2. When the user performs \"undo,\" pop the most recent operation from stack A and push it onto stack B.
      3. When the user performs \"redo,\" pop the most recent operation from stack B and push it onto stack A.
      "},{"location":"chapter_tree/","title":"Chapter 7. \u00a0 Tree","text":"

      Abstract

      Towering trees are full of vitality, with deep roots and lush leaves, spreading branches and flourishing.

      They show us the vivid form of divide and conquer in data.

      "},{"location":"chapter_tree/#chapter-contents","title":"Chapter contents","text":"
      • 7.1 \u00a0 Binary Tree
      • 7.2 \u00a0 Binary Tree Traversal
      • 7.3 \u00a0 Array Representation of Tree
      • 7.4 \u00a0 Binary Search Tree
      • 7.5 \u00a0 AVL Tree *
      • 7.6 \u00a0 Summary
      "},{"location":"chapter_tree/array_representation_of_tree/","title":"7.3 \u00a0 Array Representation of Binary Trees","text":"

      Under the linked list representation, the storage unit of a binary tree is a node TreeNode, and nodes are connected by pointers. The previous section introduced the basic operations of binary trees under the linked list representation.

      So, can we use an array to represent a binary tree? The answer is yes.

      "},{"location":"chapter_tree/array_representation_of_tree/#731-representing-perfect-binary-trees","title":"7.3.1 \u00a0 Representing Perfect Binary Trees","text":"

      Let's analyze a simple case first. Given a perfect binary tree, we store all nodes in an array according to the order of level-order traversal, where each node corresponds to a unique array index.

      Based on the characteristics of level-order traversal, we can derive a \"mapping formula\" between parent node index and child node indices: If a node's index is \\(i\\), then its left child index is \\(2i + 1\\) and its right child index is \\(2i + 2\\). Figure 7-12 shows the mapping relationships between various node indices.

      Figure 7-12 \u00a0 Array representation of a perfect binary tree

      The mapping formula plays a role similar to the node references (pointers) in linked lists. Given any node in the array, we can access its left (right) child node using the mapping formula.

      "},{"location":"chapter_tree/array_representation_of_tree/#732-representing-any-binary-tree","title":"7.3.2 \u00a0 Representing Any Binary Tree","text":"

      Perfect binary trees are a special case; in the middle levels of a binary tree, there are typically many None values. Since the level-order traversal sequence does not include these None values, we cannot infer the number and distribution of None values based on this sequence alone. This means multiple binary tree structures can correspond to the same level-order traversal sequence.

      As shown in Figure 7-13, given a non-perfect binary tree, the above method of array representation fails.

      Figure 7-13 \u00a0 Level-order traversal sequence corresponds to multiple binary tree possibilities

      To solve this problem, we can consider explicitly writing out all None values in the level-order traversal sequence. As shown in Figure 7-14, after this treatment, the level-order traversal sequence can uniquely represent a binary tree. Example code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      # Array representation of a binary tree\n# Using None to represent empty slots\ntree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]\n
      /* Array representation of a binary tree */\n// Using the maximum integer value INT_MAX to mark empty slots\nvector<int> tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
      /* Array representation of a binary tree */\n// Using the Integer wrapper class allows for using null to mark empty slots\nInteger[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };\n
      /* Array representation of a binary tree */\n// Using nullable int (int?) allows for using null to mark empty slots\nint?[] tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
      /* Array representation of a binary tree */\n// Using an any type slice, allowing for nil to mark empty slots\ntree := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15}\n
      /* Array representation of a binary tree */\n// Using optional Int (Int?) allows for using nil to mark empty slots\nlet tree: [Int?] = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]\n
      /* Array representation of a binary tree */\n// Using null to represent empty slots\nlet tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
      /* Array representation of a binary tree */\n// Using null to represent empty slots\nlet tree: (number | null)[] = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
      /* Array representation of a binary tree */\n// Using nullable int (int?) allows for using null to mark empty slots\nList<int?> tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
      /* Array representation of a binary tree */\n// Using None to mark empty slots\nlet tree = [Some(1), Some(2), Some(3), Some(4), None, Some(6), Some(7), Some(8), Some(9), None, None, Some(12), None, None, Some(15)];\n
      /* Array representation of a binary tree */\n// Using the maximum int value to mark empty slots, therefore, node values must not be INT_MAX\nint tree[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
      /* Array representation of a binary tree */\n// Using null to represent empty slots\nval tree = arrayOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 )\n
      ### Array representation of a binary tree ###\n# Using nil to represent empty slots\ntree = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]\n

      Figure 7-14 \u00a0 Array representation of any type of binary tree

      It's worth noting that complete binary trees are very well-suited for array representation. Recalling the definition of a complete binary tree, None only appears at the bottom level and towards the right, meaning all None values must appear at the end of the level-order traversal sequence.

      This means that when using an array to represent a complete binary tree, it's possible to omit storing all None values, which is very convenient. Figure 7-15 gives an example.

      Figure 7-15 \u00a0 Array representation of a complete binary tree

      The following code implements a binary tree based on array representation, including the following operations:

      • Given a certain node, obtain its value, left (right) child node, and parent node.
      • Obtain the preorder, inorder, postorder, and level-order traversal sequences.
      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_binary_tree.py
      class ArrayBinaryTree:\n    \"\"\"Binary tree class represented by array\"\"\"\n\n    def __init__(self, arr: list[int | None]):\n        \"\"\"Constructor\"\"\"\n        self._tree = list(arr)\n\n    def size(self):\n        \"\"\"List capacity\"\"\"\n        return len(self._tree)\n\n    def val(self, i: int) -> int | None:\n        \"\"\"Get value of node at index i\"\"\"\n        # If index is out of bounds, return None, representing empty position\n        if i < 0 or i >= self.size():\n            return None\n        return self._tree[i]\n\n    def left(self, i: int) -> int | None:\n        \"\"\"Get index of left child node of node at index i\"\"\"\n        return 2 * i + 1\n\n    def right(self, i: int) -> int | None:\n        \"\"\"Get index of right child node of node at index i\"\"\"\n        return 2 * i + 2\n\n    def parent(self, i: int) -> int | None:\n        \"\"\"Get index of parent node of node at index i\"\"\"\n        return (i - 1) // 2\n\n    def level_order(self) -> list[int]:\n        \"\"\"Level-order traversal\"\"\"\n        self.res = []\n        # Traverse array directly\n        for i in range(self.size()):\n            if self.val(i) is not None:\n                self.res.append(self.val(i))\n        return self.res\n\n    def dfs(self, i: int, order: str):\n        \"\"\"Depth-first traversal\"\"\"\n        if self.val(i) is None:\n            return\n        # Preorder traversal\n        if order == \"pre\":\n            self.res.append(self.val(i))\n        self.dfs(self.left(i), order)\n        # Inorder traversal\n        if order == \"in\":\n            self.res.append(self.val(i))\n        self.dfs(self.right(i), order)\n        # Postorder traversal\n        if order == \"post\":\n            self.res.append(self.val(i))\n\n    def pre_order(self) -> list[int]:\n        \"\"\"Preorder traversal\"\"\"\n        self.res = []\n        self.dfs(0, order=\"pre\")\n        return self.res\n\n    def in_order(self) -> list[int]:\n        \"\"\"Inorder traversal\"\"\"\n        self.res = []\n        self.dfs(0, order=\"in\")\n        return self.res\n\n    def post_order(self) -> list[int]:\n        \"\"\"Postorder traversal\"\"\"\n        self.res = []\n        self.dfs(0, order=\"post\")\n        return self.res\n
      array_binary_tree.cpp
      /* Binary tree class represented by array */\nclass ArrayBinaryTree {\n  public:\n    /* Constructor */\n    ArrayBinaryTree(vector<int> arr) {\n        tree = arr;\n    }\n\n    /* List capacity */\n    int size() {\n        return tree.size();\n    }\n\n    /* Get value of node at index i */\n    int val(int i) {\n        // Return INT_MAX if index out of bounds, representing empty position\n        if (i < 0 || i >= size())\n            return INT_MAX;\n        return tree[i];\n    }\n\n    /* Get index of left child node of node at index i */\n    int left(int i) {\n        return 2 * i + 1;\n    }\n\n    /* Get index of right child node of node at index i */\n    int right(int i) {\n        return 2 * i + 2;\n    }\n\n    /* Get index of parent node of node at index i */\n    int parent(int i) {\n        return (i - 1) / 2;\n    }\n\n    /* Level-order traversal */\n    vector<int> levelOrder() {\n        vector<int> res;\n        // Traverse array directly\n        for (int i = 0; i < size(); i++) {\n            if (val(i) != INT_MAX)\n                res.push_back(val(i));\n        }\n        return res;\n    }\n\n    /* Preorder traversal */\n    vector<int> preOrder() {\n        vector<int> res;\n        dfs(0, \"pre\", res);\n        return res;\n    }\n\n    /* Inorder traversal */\n    vector<int> inOrder() {\n        vector<int> res;\n        dfs(0, \"in\", res);\n        return res;\n    }\n\n    /* Postorder traversal */\n    vector<int> postOrder() {\n        vector<int> res;\n        dfs(0, \"post\", res);\n        return res;\n    }\n\n  private:\n    vector<int> tree;\n\n    /* Depth-first traversal */\n    void dfs(int i, string order, vector<int> &res) {\n        // If empty position, return\n        if (val(i) == INT_MAX)\n            return;\n        // Preorder traversal\n        if (order == \"pre\")\n            res.push_back(val(i));\n        dfs(left(i), order, res);\n        // Inorder traversal\n        if (order == \"in\")\n            res.push_back(val(i));\n        dfs(right(i), order, res);\n        // Postorder traversal\n        if (order == \"post\")\n            res.push_back(val(i));\n    }\n};\n
      array_binary_tree.java
      /* Binary tree class represented by array */\nclass ArrayBinaryTree {\n    private List<Integer> tree;\n\n    /* Constructor */\n    public ArrayBinaryTree(List<Integer> arr) {\n        tree = new ArrayList<>(arr);\n    }\n\n    /* List capacity */\n    public int size() {\n        return tree.size();\n    }\n\n    /* Get value of node at index i */\n    public Integer val(int i) {\n        // If index out of bounds, return null to represent empty position\n        if (i < 0 || i >= size())\n            return null;\n        return tree.get(i);\n    }\n\n    /* Get index of left child node of node at index i */\n    public Integer left(int i) {\n        return 2 * i + 1;\n    }\n\n    /* Get index of right child node of node at index i */\n    public Integer right(int i) {\n        return 2 * i + 2;\n    }\n\n    /* Get index of parent node of node at index i */\n    public Integer parent(int i) {\n        return (i - 1) / 2;\n    }\n\n    /* Level-order traversal */\n    public List<Integer> levelOrder() {\n        List<Integer> res = new ArrayList<>();\n        // Traverse array directly\n        for (int i = 0; i < size(); i++) {\n            if (val(i) != null)\n                res.add(val(i));\n        }\n        return res;\n    }\n\n    /* Depth-first traversal */\n    private void dfs(Integer i, String order, List<Integer> res) {\n        // If empty position, return\n        if (val(i) == null)\n            return;\n        // Preorder traversal\n        if (\"pre\".equals(order))\n            res.add(val(i));\n        dfs(left(i), order, res);\n        // Inorder traversal\n        if (\"in\".equals(order))\n            res.add(val(i));\n        dfs(right(i), order, res);\n        // Postorder traversal\n        if (\"post\".equals(order))\n            res.add(val(i));\n    }\n\n    /* Preorder traversal */\n    public List<Integer> preOrder() {\n        List<Integer> res = new ArrayList<>();\n        dfs(0, \"pre\", res);\n        return res;\n    }\n\n    /* Inorder traversal */\n    public List<Integer> inOrder() {\n        List<Integer> res = new ArrayList<>();\n        dfs(0, \"in\", res);\n        return res;\n    }\n\n    /* Postorder traversal */\n    public List<Integer> postOrder() {\n        List<Integer> res = new ArrayList<>();\n        dfs(0, \"post\", res);\n        return res;\n    }\n}\n
      array_binary_tree.cs
      /* Binary tree class represented by array */\nclass ArrayBinaryTree(List<int?> arr) {\n    List<int?> tree = new(arr);\n\n    /* List capacity */\n    public int Size() {\n        return tree.Count;\n    }\n\n    /* Get value of node at index i */\n    public int? Val(int i) {\n        // If index out of bounds, return null to represent empty position\n        if (i < 0 || i >= Size())\n            return null;\n        return tree[i];\n    }\n\n    /* Get index of left child node of node at index i */\n    public int Left(int i) {\n        return 2 * i + 1;\n    }\n\n    /* Get index of right child node of node at index i */\n    public int Right(int i) {\n        return 2 * i + 2;\n    }\n\n    /* Get index of parent node of node at index i */\n    public int Parent(int i) {\n        return (i - 1) / 2;\n    }\n\n    /* Level-order traversal */\n    public List<int> LevelOrder() {\n        List<int> res = [];\n        // Traverse array directly\n        for (int i = 0; i < Size(); i++) {\n            if (Val(i).HasValue)\n                res.Add(Val(i)!.Value);\n        }\n        return res;\n    }\n\n    /* Depth-first traversal */\n    void DFS(int i, string order, List<int> res) {\n        // If empty position, return\n        if (!Val(i).HasValue)\n            return;\n        // Preorder traversal\n        if (order == \"pre\")\n            res.Add(Val(i)!.Value);\n        DFS(Left(i), order, res);\n        // Inorder traversal\n        if (order == \"in\")\n            res.Add(Val(i)!.Value);\n        DFS(Right(i), order, res);\n        // Postorder traversal\n        if (order == \"post\")\n            res.Add(Val(i)!.Value);\n    }\n\n    /* Preorder traversal */\n    public List<int> PreOrder() {\n        List<int> res = [];\n        DFS(0, \"pre\", res);\n        return res;\n    }\n\n    /* Inorder traversal */\n    public List<int> InOrder() {\n        List<int> res = [];\n        DFS(0, \"in\", res);\n        return res;\n    }\n\n    /* Postorder traversal */\n    public List<int> PostOrder() {\n        List<int> res = [];\n        DFS(0, \"post\", res);\n        return res;\n    }\n}\n
      array_binary_tree.go
      /* Binary tree class represented by array */\ntype arrayBinaryTree struct {\n    tree []any\n}\n\n/* Constructor */\nfunc newArrayBinaryTree(arr []any) *arrayBinaryTree {\n    return &arrayBinaryTree{\n        tree: arr,\n    }\n}\n\n/* List capacity */\nfunc (abt *arrayBinaryTree) size() int {\n    return len(abt.tree)\n}\n\n/* Get value of node at index i */\nfunc (abt *arrayBinaryTree) val(i int) any {\n    // If index out of bounds, return null to represent empty position\n    if i < 0 || i >= abt.size() {\n        return nil\n    }\n    return abt.tree[i]\n}\n\n/* Get index of left child node of node at index i */\nfunc (abt *arrayBinaryTree) left(i int) int {\n    return 2*i + 1\n}\n\n/* Get index of right child node of node at index i */\nfunc (abt *arrayBinaryTree) right(i int) int {\n    return 2*i + 2\n}\n\n/* Get index of parent node of node at index i */\nfunc (abt *arrayBinaryTree) parent(i int) int {\n    return (i - 1) / 2\n}\n\n/* Level-order traversal */\nfunc (abt *arrayBinaryTree) levelOrder() []any {\n    var res []any\n    // Traverse array directly\n    for i := 0; i < abt.size(); i++ {\n        if abt.val(i) != nil {\n            res = append(res, abt.val(i))\n        }\n    }\n    return res\n}\n\n/* Depth-first traversal */\nfunc (abt *arrayBinaryTree) dfs(i int, order string, res *[]any) {\n    // If empty position, return\n    if abt.val(i) == nil {\n        return\n    }\n    // Preorder traversal\n    if order == \"pre\" {\n        *res = append(*res, abt.val(i))\n    }\n    abt.dfs(abt.left(i), order, res)\n    // Inorder traversal\n    if order == \"in\" {\n        *res = append(*res, abt.val(i))\n    }\n    abt.dfs(abt.right(i), order, res)\n    // Postorder traversal\n    if order == \"post\" {\n        *res = append(*res, abt.val(i))\n    }\n}\n\n/* Preorder traversal */\nfunc (abt *arrayBinaryTree) preOrder() []any {\n    var res []any\n    abt.dfs(0, \"pre\", &res)\n    return res\n}\n\n/* Inorder traversal */\nfunc (abt *arrayBinaryTree) inOrder() []any {\n    var res []any\n    abt.dfs(0, \"in\", &res)\n    return res\n}\n\n/* Postorder traversal */\nfunc (abt *arrayBinaryTree) postOrder() []any {\n    var res []any\n    abt.dfs(0, \"post\", &res)\n    return res\n}\n
      array_binary_tree.swift
      /* Binary tree class represented by array */\nclass ArrayBinaryTree {\n    private var tree: [Int?]\n\n    /* Constructor */\n    init(arr: [Int?]) {\n        tree = arr\n    }\n\n    /* List capacity */\n    func size() -> Int {\n        tree.count\n    }\n\n    /* Get value of node at index i */\n    func val(i: Int) -> Int? {\n        // If index out of bounds, return null to represent empty position\n        if i < 0 || i >= size() {\n            return nil\n        }\n        return tree[i]\n    }\n\n    /* Get index of left child node of node at index i */\n    func left(i: Int) -> Int {\n        2 * i + 1\n    }\n\n    /* Get index of right child node of node at index i */\n    func right(i: Int) -> Int {\n        2 * i + 2\n    }\n\n    /* Get index of parent node of node at index i */\n    func parent(i: Int) -> Int {\n        (i - 1) / 2\n    }\n\n    /* Level-order traversal */\n    func levelOrder() -> [Int] {\n        var res: [Int] = []\n        // Traverse array directly\n        for i in 0 ..< size() {\n            if let val = val(i: i) {\n                res.append(val)\n            }\n        }\n        return res\n    }\n\n    /* Depth-first traversal */\n    private func dfs(i: Int, order: String, res: inout [Int]) {\n        // If empty position, return\n        guard let val = val(i: i) else {\n            return\n        }\n        // Preorder traversal\n        if order == \"pre\" {\n            res.append(val)\n        }\n        dfs(i: left(i: i), order: order, res: &res)\n        // Inorder traversal\n        if order == \"in\" {\n            res.append(val)\n        }\n        dfs(i: right(i: i), order: order, res: &res)\n        // Postorder traversal\n        if order == \"post\" {\n            res.append(val)\n        }\n    }\n\n    /* Preorder traversal */\n    func preOrder() -> [Int] {\n        var res: [Int] = []\n        dfs(i: 0, order: \"pre\", res: &res)\n        return res\n    }\n\n    /* Inorder traversal */\n    func inOrder() -> [Int] {\n        var res: [Int] = []\n        dfs(i: 0, order: \"in\", res: &res)\n        return res\n    }\n\n    /* Postorder traversal */\n    func postOrder() -> [Int] {\n        var res: [Int] = []\n        dfs(i: 0, order: \"post\", res: &res)\n        return res\n    }\n}\n
      array_binary_tree.js
      /* Binary tree class represented by array */\nclass ArrayBinaryTree {\n    #tree;\n\n    /* Constructor */\n    constructor(arr) {\n        this.#tree = arr;\n    }\n\n    /* List capacity */\n    size() {\n        return this.#tree.length;\n    }\n\n    /* Get value of node at index i */\n    val(i) {\n        // If index out of bounds, return null to represent empty position\n        if (i < 0 || i >= this.size()) return null;\n        return this.#tree[i];\n    }\n\n    /* Get index of left child node of node at index i */\n    left(i) {\n        return 2 * i + 1;\n    }\n\n    /* Get index of right child node of node at index i */\n    right(i) {\n        return 2 * i + 2;\n    }\n\n    /* Get index of parent node of node at index i */\n    parent(i) {\n        return Math.floor((i - 1) / 2); // Floor division\n    }\n\n    /* Level-order traversal */\n    levelOrder() {\n        let res = [];\n        // Traverse array directly\n        for (let i = 0; i < this.size(); i++) {\n            if (this.val(i) !== null) res.push(this.val(i));\n        }\n        return res;\n    }\n\n    /* Depth-first traversal */\n    #dfs(i, order, res) {\n        // If empty position, return\n        if (this.val(i) === null) return;\n        // Preorder traversal\n        if (order === 'pre') res.push(this.val(i));\n        this.#dfs(this.left(i), order, res);\n        // Inorder traversal\n        if (order === 'in') res.push(this.val(i));\n        this.#dfs(this.right(i), order, res);\n        // Postorder traversal\n        if (order === 'post') res.push(this.val(i));\n    }\n\n    /* Preorder traversal */\n    preOrder() {\n        const res = [];\n        this.#dfs(0, 'pre', res);\n        return res;\n    }\n\n    /* Inorder traversal */\n    inOrder() {\n        const res = [];\n        this.#dfs(0, 'in', res);\n        return res;\n    }\n\n    /* Postorder traversal */\n    postOrder() {\n        const res = [];\n        this.#dfs(0, 'post', res);\n        return res;\n    }\n}\n
      array_binary_tree.ts
      /* Binary tree class represented by array */\nclass ArrayBinaryTree {\n    #tree: (number | null)[];\n\n    /* Constructor */\n    constructor(arr: (number | null)[]) {\n        this.#tree = arr;\n    }\n\n    /* List capacity */\n    size(): number {\n        return this.#tree.length;\n    }\n\n    /* Get value of node at index i */\n    val(i: number): number | null {\n        // If index out of bounds, return null to represent empty position\n        if (i < 0 || i >= this.size()) return null;\n        return this.#tree[i];\n    }\n\n    /* Get index of left child node of node at index i */\n    left(i: number): number {\n        return 2 * i + 1;\n    }\n\n    /* Get index of right child node of node at index i */\n    right(i: number): number {\n        return 2 * i + 2;\n    }\n\n    /* Get index of parent node of node at index i */\n    parent(i: number): number {\n        return Math.floor((i - 1) / 2); // Floor division\n    }\n\n    /* Level-order traversal */\n    levelOrder(): number[] {\n        let res = [];\n        // Traverse array directly\n        for (let i = 0; i < this.size(); i++) {\n            if (this.val(i) !== null) res.push(this.val(i));\n        }\n        return res;\n    }\n\n    /* Depth-first traversal */\n    #dfs(i: number, order: Order, res: (number | null)[]): void {\n        // If empty position, return\n        if (this.val(i) === null) return;\n        // Preorder traversal\n        if (order === 'pre') res.push(this.val(i));\n        this.#dfs(this.left(i), order, res);\n        // Inorder traversal\n        if (order === 'in') res.push(this.val(i));\n        this.#dfs(this.right(i), order, res);\n        // Postorder traversal\n        if (order === 'post') res.push(this.val(i));\n    }\n\n    /* Preorder traversal */\n    preOrder(): (number | null)[] {\n        const res = [];\n        this.#dfs(0, 'pre', res);\n        return res;\n    }\n\n    /* Inorder traversal */\n    inOrder(): (number | null)[] {\n        const res = [];\n        this.#dfs(0, 'in', res);\n        return res;\n    }\n\n    /* Postorder traversal */\n    postOrder(): (number | null)[] {\n        const res = [];\n        this.#dfs(0, 'post', res);\n        return res;\n    }\n}\n
      array_binary_tree.dart
      /* Binary tree class represented by array */\nclass ArrayBinaryTree {\n  late List<int?> _tree;\n\n  /* Constructor */\n  ArrayBinaryTree(this._tree);\n\n  /* List capacity */\n  int size() {\n    return _tree.length;\n  }\n\n  /* Get value of node at index i */\n  int? val(int i) {\n    // If index out of bounds, return null to represent empty position\n    if (i < 0 || i >= size()) {\n      return null;\n    }\n    return _tree[i];\n  }\n\n  /* Get index of left child node of node at index i */\n  int? left(int i) {\n    return 2 * i + 1;\n  }\n\n  /* Get index of right child node of node at index i */\n  int? right(int i) {\n    return 2 * i + 2;\n  }\n\n  /* Get index of parent node of node at index i */\n  int? parent(int i) {\n    return (i - 1) ~/ 2;\n  }\n\n  /* Level-order traversal */\n  List<int> levelOrder() {\n    List<int> res = [];\n    for (int i = 0; i < size(); i++) {\n      if (val(i) != null) {\n        res.add(val(i)!);\n      }\n    }\n    return res;\n  }\n\n  /* Depth-first traversal */\n  void dfs(int i, String order, List<int?> res) {\n    // If empty position, return\n    if (val(i) == null) {\n      return;\n    }\n    // Preorder traversal\n    if (order == 'pre') {\n      res.add(val(i));\n    }\n    dfs(left(i)!, order, res);\n    // Inorder traversal\n    if (order == 'in') {\n      res.add(val(i));\n    }\n    dfs(right(i)!, order, res);\n    // Postorder traversal\n    if (order == 'post') {\n      res.add(val(i));\n    }\n  }\n\n  /* Preorder traversal */\n  List<int?> preOrder() {\n    List<int?> res = [];\n    dfs(0, 'pre', res);\n    return res;\n  }\n\n  /* Inorder traversal */\n  List<int?> inOrder() {\n    List<int?> res = [];\n    dfs(0, 'in', res);\n    return res;\n  }\n\n  /* Postorder traversal */\n  List<int?> postOrder() {\n    List<int?> res = [];\n    dfs(0, 'post', res);\n    return res;\n  }\n}\n
      array_binary_tree.rs
      /* Binary tree class represented by array */\nstruct ArrayBinaryTree {\n    tree: Vec<Option<i32>>,\n}\n\nimpl ArrayBinaryTree {\n    /* Constructor */\n    fn new(arr: Vec<Option<i32>>) -> Self {\n        Self { tree: arr }\n    }\n\n    /* List capacity */\n    fn size(&self) -> i32 {\n        self.tree.len() as i32\n    }\n\n    /* Get value of node at index i */\n    fn val(&self, i: i32) -> Option<i32> {\n        // If index is out of bounds, return None, representing empty position\n        if i < 0 || i >= self.size() {\n            None\n        } else {\n            self.tree[i as usize]\n        }\n    }\n\n    /* Get index of left child node of node at index i */\n    fn left(&self, i: i32) -> i32 {\n        2 * i + 1\n    }\n\n    /* Get index of right child node of node at index i */\n    fn right(&self, i: i32) -> i32 {\n        2 * i + 2\n    }\n\n    /* Get index of parent node of node at index i */\n    fn parent(&self, i: i32) -> i32 {\n        (i - 1) / 2\n    }\n\n    /* Level-order traversal */\n    fn level_order(&self) -> Vec<i32> {\n        self.tree.iter().filter_map(|&x| x).collect()\n    }\n\n    /* Depth-first traversal */\n    fn dfs(&self, i: i32, order: &'static str, res: &mut Vec<i32>) {\n        if self.val(i).is_none() {\n            return;\n        }\n        let val = self.val(i).unwrap();\n        // Preorder traversal\n        if order == \"pre\" {\n            res.push(val);\n        }\n        self.dfs(self.left(i), order, res);\n        // Inorder traversal\n        if order == \"in\" {\n            res.push(val);\n        }\n        self.dfs(self.right(i), order, res);\n        // Postorder traversal\n        if order == \"post\" {\n            res.push(val);\n        }\n    }\n\n    /* Preorder traversal */\n    fn pre_order(&self) -> Vec<i32> {\n        let mut res = vec![];\n        self.dfs(0, \"pre\", &mut res);\n        res\n    }\n\n    /* Inorder traversal */\n    fn in_order(&self) -> Vec<i32> {\n        let mut res = vec![];\n        self.dfs(0, \"in\", &mut res);\n        res\n    }\n\n    /* Postorder traversal */\n    fn post_order(&self) -> Vec<i32> {\n        let mut res = vec![];\n        self.dfs(0, \"post\", &mut res);\n        res\n    }\n}\n
      array_binary_tree.c
      /* Binary tree structure in array representation */\ntypedef struct {\n    int *tree;\n    int size;\n} ArrayBinaryTree;\n\n/* Constructor */\nArrayBinaryTree *newArrayBinaryTree(int *arr, int arrSize) {\n    ArrayBinaryTree *abt = (ArrayBinaryTree *)malloc(sizeof(ArrayBinaryTree));\n    abt->tree = malloc(sizeof(int) * arrSize);\n    memcpy(abt->tree, arr, sizeof(int) * arrSize);\n    abt->size = arrSize;\n    return abt;\n}\n\n/* Destructor */\nvoid delArrayBinaryTree(ArrayBinaryTree *abt) {\n    free(abt->tree);\n    free(abt);\n}\n\n/* List capacity */\nint size(ArrayBinaryTree *abt) {\n    return abt->size;\n}\n\n/* Get value of node at index i */\nint val(ArrayBinaryTree *abt, int i) {\n    // Return INT_MAX if index out of bounds, representing empty position\n    if (i < 0 || i >= size(abt))\n        return INT_MAX;\n    return abt->tree[i];\n}\n\n/* Level-order traversal */\nint *levelOrder(ArrayBinaryTree *abt, int *returnSize) {\n    int *res = (int *)malloc(sizeof(int) * size(abt));\n    int index = 0;\n    // Traverse array directly\n    for (int i = 0; i < size(abt); i++) {\n        if (val(abt, i) != INT_MAX)\n            res[index++] = val(abt, i);\n    }\n    *returnSize = index;\n    return res;\n}\n\n/* Depth-first traversal */\nvoid dfs(ArrayBinaryTree *abt, int i, char *order, int *res, int *index) {\n    // If empty position, return\n    if (val(abt, i) == INT_MAX)\n        return;\n    // Preorder traversal\n    if (strcmp(order, \"pre\") == 0)\n        res[(*index)++] = val(abt, i);\n    dfs(abt, left(i), order, res, index);\n    // Inorder traversal\n    if (strcmp(order, \"in\") == 0)\n        res[(*index)++] = val(abt, i);\n    dfs(abt, right(i), order, res, index);\n    // Postorder traversal\n    if (strcmp(order, \"post\") == 0)\n        res[(*index)++] = val(abt, i);\n}\n\n/* Preorder traversal */\nint *preOrder(ArrayBinaryTree *abt, int *returnSize) {\n    int *res = (int *)malloc(sizeof(int) * size(abt));\n    int index = 0;\n    dfs(abt, 0, \"pre\", res, &index);\n    *returnSize = index;\n    return res;\n}\n\n/* Inorder traversal */\nint *inOrder(ArrayBinaryTree *abt, int *returnSize) {\n    int *res = (int *)malloc(sizeof(int) * size(abt));\n    int index = 0;\n    dfs(abt, 0, \"in\", res, &index);\n    *returnSize = index;\n    return res;\n}\n\n/* Postorder traversal */\nint *postOrder(ArrayBinaryTree *abt, int *returnSize) {\n    int *res = (int *)malloc(sizeof(int) * size(abt));\n    int index = 0;\n    dfs(abt, 0, \"post\", res, &index);\n    *returnSize = index;\n    return res;\n}\n
      array_binary_tree.kt
      /* Binary tree class represented by array */\nclass ArrayBinaryTree(val tree: MutableList<Int?>) {\n    /* List capacity */\n    fun size(): Int {\n        return tree.size\n    }\n\n    /* Get value of node at index i */\n    fun _val(i: Int): Int? {\n        // If index out of bounds, return null to represent empty position\n        if (i < 0 || i >= size()) return null\n        return tree[i]\n    }\n\n    /* Get index of left child node of node at index i */\n    fun left(i: Int): Int {\n        return 2 * i + 1\n    }\n\n    /* Get index of right child node of node at index i */\n    fun right(i: Int): Int {\n        return 2 * i + 2\n    }\n\n    /* Get index of parent node of node at index i */\n    fun parent(i: Int): Int {\n        return (i - 1) / 2\n    }\n\n    /* Level-order traversal */\n    fun levelOrder(): MutableList<Int?> {\n        val res = mutableListOf<Int?>()\n        // Traverse array directly\n        for (i in 0..<size()) {\n            if (_val(i) != null)\n                res.add(_val(i))\n        }\n        return res\n    }\n\n    /* Depth-first traversal */\n    fun dfs(i: Int, order: String, res: MutableList<Int?>) {\n        // If empty position, return\n        if (_val(i) == null)\n            return\n        // Preorder traversal\n        if (\"pre\" == order)\n            res.add(_val(i))\n        dfs(left(i), order, res)\n        // Inorder traversal\n        if (\"in\" == order)\n            res.add(_val(i))\n        dfs(right(i), order, res)\n        // Postorder traversal\n        if (\"post\" == order)\n            res.add(_val(i))\n    }\n\n    /* Preorder traversal */\n    fun preOrder(): MutableList<Int?> {\n        val res = mutableListOf<Int?>()\n        dfs(0, \"pre\", res)\n        return res\n    }\n\n    /* Inorder traversal */\n    fun inOrder(): MutableList<Int?> {\n        val res = mutableListOf<Int?>()\n        dfs(0, \"in\", res)\n        return res\n    }\n\n    /* Postorder traversal */\n    fun postOrder(): MutableList<Int?> {\n        val res = mutableListOf<Int?>()\n        dfs(0, \"post\", res)\n        return res\n    }\n}\n
      array_binary_tree.rb
      ### Array representation of binary tree class ###\nclass ArrayBinaryTree\n  ### Constructor ###\n  def initialize(arr)\n    @tree = arr.to_a\n  end\n\n  ### List capacity ###\n  def size\n    @tree.length\n  end\n\n  ### Get value of node at index i ###\n  def val(i)\n    # Return nil if index out of bounds, representing empty position\n    return if i < 0 || i >= size\n\n    @tree[i]\n  end\n\n  ### Get left child index of node at index i ###\n  def left(i)\n    2 * i + 1\n  end\n\n  ### Get right child index of node at index i ###\n  def right(i)\n    2 * i + 2\n  end\n\n  ### Get parent node index of node at index i ###\n  def parent(i)\n    (i - 1) / 2\n  end\n\n  ### Level-order traversal ###\n  def level_order\n    @res = []\n\n    # Traverse array directly\n    for i in 0...size\n      @res << val(i) unless val(i).nil?\n    end\n\n    @res\n  end\n\n  ### Depth-first traversal ###\n  def dfs(i, order)\n    return if val(i).nil?\n    # Preorder traversal\n    @res << val(i) if order == :pre\n    dfs(left(i), order)\n    # Inorder traversal\n    @res << val(i) if order == :in\n    dfs(right(i), order)\n    # Postorder traversal\n    @res << val(i) if order == :post\n  end\n\n  ### Pre-order traversal ###\n  def pre_order\n    @res = []\n    dfs(0, :pre)\n    @res\n  end\n\n  ### In-order traversal ###\n  def in_order\n    @res = []\n    dfs(0, :in)\n    @res\n  end\n\n  ### Post-order traversal ###\n  def post_order\n    @res = []\n    dfs(0, :post)\n    @res\n  end\nend\n
      "},{"location":"chapter_tree/array_representation_of_tree/#733-advantages-and-limitations","title":"7.3.3 \u00a0 Advantages and Limitations","text":"

      The array representation of binary trees has the following advantages:

      • Arrays are stored in contiguous memory space, which is cache-friendly, allowing faster access and traversal.
      • It does not require storing pointers, which saves space.
      • It allows random access to nodes.

      However, the array representation also has some limitations:

      • Array storage requires contiguous memory space, so it is not suitable for storing trees with a large amount of data.
      • Adding or removing nodes requires array insertion and deletion operations, which have lower efficiency.
      • When there are many None values in the binary tree, the proportion of node data contained in the array is low, leading to lower space utilization.
      "},{"location":"chapter_tree/avl_tree/","title":"7.5 \u00a0 Avl Tree *","text":"

      In the \"Binary Search Tree\" section, we mentioned that after multiple insertion and removal operations, a binary search tree may degenerate into a linked list. In this case, the time complexity of all operations degrades from \\(O(\\log n)\\) to \\(O(n)\\).

      As shown in Figure 7-24, after two node removal operations, this binary search tree will degrade into a linked list.

      Figure 7-24 \u00a0 Degradation of an AVL tree after removing nodes

      For example, in the perfect binary tree shown in Figure 7-25, after inserting two nodes, the tree will lean heavily to the left, and the time complexity of search operations will also degrade.

      Figure 7-25 \u00a0 Degradation of an AVL tree after inserting nodes

      In 1962, G. M. Adelson-Velsky and E. M. Landis proposed the AVL tree in their paper \"An algorithm for the organization of information\". The paper described in detail a series of operations ensuring that after continuously adding and removing nodes, the AVL tree does not degenerate, thus keeping the time complexity of various operations at the \\(O(\\log n)\\) level. In other words, in scenarios requiring frequent insertions, deletions, searches, and modifications, the AVL tree can always maintain efficient data operation performance, making it very valuable in applications.

      "},{"location":"chapter_tree/avl_tree/#751-common-terminology-in-avl-trees","title":"7.5.1 \u00a0 Common Terminology in Avl Trees","text":"

      An AVL tree is both a binary search tree and a balanced binary tree, simultaneously satisfying all the properties of these two types of binary trees, hence it is a balanced binary search tree.

      "},{"location":"chapter_tree/avl_tree/#1-node-height","title":"1. \u00a0 Node Height","text":"

      Since the operations related to AVL trees require obtaining node heights, we need to add a height variable to the node class:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      class TreeNode:\n    \"\"\"AVL tree node\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val                 # Node value\n        self.height: int = 0                # Node height\n        self.left: TreeNode | None = None   # Left child reference\n        self.right: TreeNode | None = None  # Right child reference\n
      /* AVL tree node */\nstruct TreeNode {\n    int val{};          // Node value\n    int height = 0;     // Node height\n    TreeNode *left{};   // Left child\n    TreeNode *right{};  // Right child\n    TreeNode() = default;\n    explicit TreeNode(int x) : val(x){}\n};\n
      /* AVL tree node */\nclass TreeNode {\n    public int val;        // Node value\n    public int height;     // Node height\n    public TreeNode left;  // Left child\n    public TreeNode right; // Right child\n    public TreeNode(int x) { val = x; }\n}\n
      /* AVL tree node */\nclass TreeNode(int? x) {\n    public int? val = x;    // Node value\n    public int height;      // Node height\n    public TreeNode? left;  // Left child reference\n    public TreeNode? right; // Right child reference\n}\n
      /* AVL tree node */\ntype TreeNode struct {\n    Val    int       // Node value\n    Height int       // Node height\n    Left   *TreeNode // Left child reference\n    Right  *TreeNode // Right child reference\n}\n
      /* AVL tree node */\nclass TreeNode {\n    var val: Int // Node value\n    var height: Int // Node height\n    var left: TreeNode? // Left child\n    var right: TreeNode? // Right child\n\n    init(x: Int) {\n        val = x\n        height = 0\n    }\n}\n
      /* AVL tree node */\nclass TreeNode {\n    val; // Node value\n    height; // Node height\n    left; // Left child pointer\n    right; // Right child pointer\n    constructor(val, left, right, height) {\n        this.val = val === undefined ? 0 : val;\n        this.height = height === undefined ? 0 : height;\n        this.left = left === undefined ? null : left;\n        this.right = right === undefined ? null : right;\n    }\n}\n
      /* AVL tree node */\nclass TreeNode {\n    val: number;            // Node value\n    height: number;         // Node height\n    left: TreeNode | null;  // Left child pointer\n    right: TreeNode | null; // Right child pointer\n    constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {\n        this.val = val === undefined ? 0 : val;\n        this.height = height === undefined ? 0 : height; \n        this.left = left === undefined ? null : left; \n        this.right = right === undefined ? null : right; \n    }\n}\n
      /* AVL tree node */\nclass TreeNode {\n  int val;         // Node value\n  int height;      // Node height\n  TreeNode? left;  // Left child\n  TreeNode? right; // Right child\n  TreeNode(this.val, [this.height = 0, this.left, this.right]);\n}\n
      use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* AVL tree node */\nstruct TreeNode {\n    val: i32,                               // Node value\n    height: i32,                            // Node height\n    left: Option<Rc<RefCell<TreeNode>>>,    // Left child\n    right: Option<Rc<RefCell<TreeNode>>>,   // Right child\n}\n\nimpl TreeNode {\n    /* Constructor */\n    fn new(val: i32) -> Rc<RefCell<Self>> {\n        Rc::new(RefCell::new(Self {\n            val,\n            height: 0,\n            left: None,\n            right: None\n        }))\n    }\n}\n
      /* AVL tree node */\ntypedef struct TreeNode {\n    int val;\n    int height;\n    struct TreeNode *left;\n    struct TreeNode *right;\n} TreeNode;\n\n/* Constructor */\nTreeNode *newTreeNode(int val) {\n    TreeNode *node;\n\n    node = (TreeNode *)malloc(sizeof(TreeNode));\n    node->val = val;\n    node->height = 0;\n    node->left = NULL;\n    node->right = NULL;\n    return node;\n}\n
      /* AVL tree node */\nclass TreeNode(val _val: Int) {  // Node value\n    val height: Int = 0          // Node height\n    val left: TreeNode? = null   // Left child\n    val right: TreeNode? = null  // Right child\n}\n
      ### AVL tree node class ###\nclass TreeNode\n  attr_accessor :val    # Node value\n  attr_accessor :height # Node height\n  attr_accessor :left   # Left child reference\n  attr_accessor :right  # Right child reference\n\n  def initialize(val)\n    @val = val\n    @height = 0\n  end\nend\n

      The \"node height\" refers to the distance from that node to its farthest leaf node, i.e., the number of \"edges\" passed. It is important to note that the height of a leaf node is \\(0\\), and the height of a null node is \\(-1\\). We will create two utility functions for getting and updating the height of a node:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
      def height(self, node: TreeNode | None) -> int:\n    \"\"\"Get node height\"\"\"\n    # Empty node height is -1, leaf node height is 0\n    if node is not None:\n        return node.height\n    return -1\n\ndef update_height(self, node: TreeNode | None):\n    \"\"\"Update node height\"\"\"\n    # Node height equals the height of the tallest subtree + 1\n    node.height = max([self.height(node.left), self.height(node.right)]) + 1\n
      avl_tree.cpp
      /* Get node height */\nint height(TreeNode *node) {\n    // Empty node height is -1, leaf node height is 0\n    return node == nullptr ? -1 : node->height;\n}\n\n/* Update node height */\nvoid updateHeight(TreeNode *node) {\n    // Node height equals the height of the tallest subtree + 1\n    node->height = max(height(node->left), height(node->right)) + 1;\n}\n
      avl_tree.java
      /* Get node height */\nint height(TreeNode node) {\n    // Empty node height is -1, leaf node height is 0\n    return node == null ? -1 : node.height;\n}\n\n/* Update node height */\nvoid updateHeight(TreeNode node) {\n    // Node height equals the height of the tallest subtree + 1\n    node.height = Math.max(height(node.left), height(node.right)) + 1;\n}\n
      avl_tree.cs
      /* Get node height */\nint Height(TreeNode? node) {\n    // Empty node height is -1, leaf node height is 0\n    return node == null ? -1 : node.height;\n}\n\n/* Update node height */\nvoid UpdateHeight(TreeNode node) {\n    // Node height equals the height of the tallest subtree + 1\n    node.height = Math.Max(Height(node.left), Height(node.right)) + 1;\n}\n
      avl_tree.go
      /* Get node height */\nfunc (t *aVLTree) height(node *TreeNode) int {\n    // Empty node height is -1, leaf node height is 0\n    if node != nil {\n        return node.Height\n    }\n    return -1\n}\n\n/* Update node height */\nfunc (t *aVLTree) updateHeight(node *TreeNode) {\n    lh := t.height(node.Left)\n    rh := t.height(node.Right)\n    // Node height equals the height of the tallest subtree + 1\n    if lh > rh {\n        node.Height = lh + 1\n    } else {\n        node.Height = rh + 1\n    }\n}\n
      avl_tree.swift
      /* Get node height */\nfunc height(node: TreeNode?) -> Int {\n    // Empty node height is -1, leaf node height is 0\n    node?.height ?? -1\n}\n\n/* Update node height */\nfunc updateHeight(node: TreeNode?) {\n    // Node height equals the height of the tallest subtree + 1\n    node?.height = max(height(node: node?.left), height(node: node?.right)) + 1\n}\n
      avl_tree.js
      /* Get node height */\nheight(node) {\n    // Empty node height is -1, leaf node height is 0\n    return node === null ? -1 : node.height;\n}\n\n/* Update node height */\n#updateHeight(node) {\n    // Node height equals the height of the tallest subtree + 1\n    node.height =\n        Math.max(this.height(node.left), this.height(node.right)) + 1;\n}\n
      avl_tree.ts
      /* Get node height */\nheight(node: TreeNode): number {\n    // Empty node height is -1, leaf node height is 0\n    return node === null ? -1 : node.height;\n}\n\n/* Update node height */\nupdateHeight(node: TreeNode): void {\n    // Node height equals the height of the tallest subtree + 1\n    node.height =\n        Math.max(this.height(node.left), this.height(node.right)) + 1;\n}\n
      avl_tree.dart
      /* Get node height */\nint height(TreeNode? node) {\n  // Empty node height is -1, leaf node height is 0\n  return node == null ? -1 : node.height;\n}\n\n/* Update node height */\nvoid updateHeight(TreeNode? node) {\n  // Node height equals the height of the tallest subtree + 1\n  node!.height = max(height(node.left), height(node.right)) + 1;\n}\n
      avl_tree.rs
      /* Get node height */\nfn height(node: OptionTreeNodeRc) -> i32 {\n    // Empty node height is -1, leaf node height is 0\n    match node {\n        Some(node) => node.borrow().height,\n        None => -1,\n    }\n}\n\n/* Update node height */\nfn update_height(node: OptionTreeNodeRc) {\n    if let Some(node) = node {\n        let left = node.borrow().left.clone();\n        let right = node.borrow().right.clone();\n        // Node height equals the height of the tallest subtree + 1\n        node.borrow_mut().height = std::cmp::max(Self::height(left), Self::height(right)) + 1;\n    }\n}\n
      avl_tree.c
      /* Get node height */\nint height(TreeNode *node) {\n    // Empty node height is -1, leaf node height is 0\n    if (node != NULL) {\n        return node->height;\n    }\n    return -1;\n}\n\n/* Update node height */\nvoid updateHeight(TreeNode *node) {\n    int lh = height(node->left);\n    int rh = height(node->right);\n    // Node height equals the height of the tallest subtree + 1\n    if (lh > rh) {\n        node->height = lh + 1;\n    } else {\n        node->height = rh + 1;\n    }\n}\n
      avl_tree.kt
      /* Get node height */\nfun height(node: TreeNode?): Int {\n    // Empty node height is -1, leaf node height is 0\n    return node?.height ?: -1\n}\n\n/* Update node height */\nfun updateHeight(node: TreeNode?) {\n    // Node height equals the height of the tallest subtree + 1\n    node?.height = max(height(node?.left), height(node?.right)) + 1\n}\n
      avl_tree.rb
      ### Get node height ###\ndef height(node)\n  # Empty node height is -1, leaf node height is 0\n  return node.height unless node.nil?\n\n  -1\nend\n\n### Update node height ###\ndef update_height(node)\n  # Node height equals the height of the tallest subtree + 1\n  node.height = [height(node.left), height(node.right)].max + 1\nend\n
      "},{"location":"chapter_tree/avl_tree/#2-node-balance-factor","title":"2. \u00a0 Node Balance Factor","text":"

      The balance factor of a node is defined as the height of the node's left subtree minus the height of its right subtree, and the balance factor of a null node is defined as \\(0\\). We also encapsulate the function to obtain the node's balance factor for convenient subsequent use:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
      def balance_factor(self, node: TreeNode | None) -> int:\n    \"\"\"Get balance factor\"\"\"\n    # Empty node balance factor is 0\n    if node is None:\n        return 0\n    # Node balance factor = left subtree height - right subtree height\n    return self.height(node.left) - self.height(node.right)\n
      avl_tree.cpp
      /* Get balance factor */\nint balanceFactor(TreeNode *node) {\n    // Empty node balance factor is 0\n    if (node == nullptr)\n        return 0;\n    // Node balance factor = left subtree height - right subtree height\n    return height(node->left) - height(node->right);\n}\n
      avl_tree.java
      /* Get balance factor */\nint balanceFactor(TreeNode node) {\n    // Empty node balance factor is 0\n    if (node == null)\n        return 0;\n    // Node balance factor = left subtree height - right subtree height\n    return height(node.left) - height(node.right);\n}\n
      avl_tree.cs
      /* Get balance factor */\nint BalanceFactor(TreeNode? node) {\n    // Empty node balance factor is 0\n    if (node == null) return 0;\n    // Node balance factor = left subtree height - right subtree height\n    return Height(node.left) - Height(node.right);\n}\n
      avl_tree.go
      /* Get balance factor */\nfunc (t *aVLTree) balanceFactor(node *TreeNode) int {\n    // Empty node balance factor is 0\n    if node == nil {\n        return 0\n    }\n    // Node balance factor = left subtree height - right subtree height\n    return t.height(node.Left) - t.height(node.Right)\n}\n
      avl_tree.swift
      /* Get balance factor */\nfunc balanceFactor(node: TreeNode?) -> Int {\n    // Empty node balance factor is 0\n    guard let node = node else { return 0 }\n    // Node balance factor = left subtree height - right subtree height\n    return height(node: node.left) - height(node: node.right)\n}\n
      avl_tree.js
      /* Get balance factor */\nbalanceFactor(node) {\n    // Empty node balance factor is 0\n    if (node === null) return 0;\n    // Node balance factor = left subtree height - right subtree height\n    return this.height(node.left) - this.height(node.right);\n}\n
      avl_tree.ts
      /* Get balance factor */\nbalanceFactor(node: TreeNode): number {\n    // Empty node balance factor is 0\n    if (node === null) return 0;\n    // Node balance factor = left subtree height - right subtree height\n    return this.height(node.left) - this.height(node.right);\n}\n
      avl_tree.dart
      /* Get balance factor */\nint balanceFactor(TreeNode? node) {\n  // Empty node balance factor is 0\n  if (node == null) return 0;\n  // Node balance factor = left subtree height - right subtree height\n  return height(node.left) - height(node.right);\n}\n
      avl_tree.rs
      /* Get balance factor */\nfn balance_factor(node: OptionTreeNodeRc) -> i32 {\n    match node {\n        // Empty node balance factor is 0\n        None => 0,\n        // Node balance factor = left subtree height - right subtree height\n        Some(node) => {\n            Self::height(node.borrow().left.clone()) - Self::height(node.borrow().right.clone())\n        }\n    }\n}\n
      avl_tree.c
      /* Get balance factor */\nint balanceFactor(TreeNode *node) {\n    // Empty node balance factor is 0\n    if (node == NULL) {\n        return 0;\n    }\n    // Node balance factor = left subtree height - right subtree height\n    return height(node->left) - height(node->right);\n}\n
      avl_tree.kt
      /* Get balance factor */\nfun balanceFactor(node: TreeNode?): Int {\n    // Empty node balance factor is 0\n    if (node == null) return 0\n    // Node balance factor = left subtree height - right subtree height\n    return height(node.left) - height(node.right)\n}\n
      avl_tree.rb
      ### Get balance factor ###\ndef balance_factor(node)\n  # Empty node balance factor is 0\n  return 0 if node.nil?\n\n  # Node balance factor = left subtree height - right subtree height\n  height(node.left) - height(node.right)\nend\n

      Tip

      Let the balance factor be \\(f\\), then the balance factor of any node in an AVL tree satisfies \\(-1 \\le f \\le 1\\).

      "},{"location":"chapter_tree/avl_tree/#752-rotations-in-avl-trees","title":"7.5.2 \u00a0 Rotations in Avl Trees","text":"

      The characteristic of AVL trees lies in the \"rotation\" operation, which can restore balance to unbalanced nodes without affecting the inorder traversal sequence of the binary tree. In other words, rotation operations can both maintain the property of a \"binary search tree\" and make the tree return to a \"balanced binary tree\".

      We call nodes with a balance factor absolute value \\(> 1\\) \"unbalanced nodes\". Depending on the imbalance situation, rotation operations are divided into four types: right rotation, left rotation, left rotation then right rotation, and right rotation then left rotation. Below we describe these rotation operations in detail.

      "},{"location":"chapter_tree/avl_tree/#1-right-rotation","title":"1. \u00a0 Right Rotation","text":"

      As shown in Figure 7-26, the value below the node is the balance factor. From bottom to top, the first unbalanced node in the binary tree is \"node 3\". We focus on the subtree with this unbalanced node as the root, denoting the node as node and its left child as child, and perform a \"right rotation\" operation. After the right rotation is completed, the subtree regains balance and still maintains the properties of a binary search tree.

      <1><2><3><4>

      Figure 7-26 \u00a0 Steps of right rotation

      As shown in Figure 7-27, when the child node has a right child (denoted as grand_child), a step needs to be added in the right rotation: set grand_child as the left child of node.

      Figure 7-27 \u00a0 Right rotation with grand_child

      \"Right rotation\" is a figurative term; in practice, it is achieved by modifying node pointers, as shown in the following code:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
      def right_rotate(self, node: TreeNode | None) -> TreeNode | None:\n    \"\"\"Right rotation operation\"\"\"\n    child = node.left\n    grand_child = child.right\n    # Using child as pivot, rotate node to the right\n    child.right = node\n    node.left = grand_child\n    # Update node height\n    self.update_height(node)\n    self.update_height(child)\n    # Return root node of subtree after rotation\n    return child\n
      avl_tree.cpp
      /* Right rotation operation */\nTreeNode *rightRotate(TreeNode *node) {\n    TreeNode *child = node->left;\n    TreeNode *grandChild = child->right;\n    // Using child as pivot, rotate node to the right\n    child->right = node;\n    node->left = grandChild;\n    // Update node height\n    updateHeight(node);\n    updateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.java
      /* Right rotation operation */\nTreeNode rightRotate(TreeNode node) {\n    TreeNode child = node.left;\n    TreeNode grandChild = child.right;\n    // Using child as pivot, rotate node to the right\n    child.right = node;\n    node.left = grandChild;\n    // Update node height\n    updateHeight(node);\n    updateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.cs
      /* Right rotation operation */\nTreeNode? RightRotate(TreeNode? node) {\n    TreeNode? child = node?.left;\n    TreeNode? grandChild = child?.right;\n    // Using child as pivot, rotate node to the right\n    child.right = node;\n    node.left = grandChild;\n    // Update node height\n    UpdateHeight(node);\n    UpdateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.go
      /* Right rotation operation */\nfunc (t *aVLTree) rightRotate(node *TreeNode) *TreeNode {\n    child := node.Left\n    grandChild := child.Right\n    // Using child as pivot, rotate node to the right\n    child.Right = node\n    node.Left = grandChild\n    // Update node height\n    t.updateHeight(node)\n    t.updateHeight(child)\n    // Return root node of subtree after rotation\n    return child\n}\n
      avl_tree.swift
      /* Right rotation operation */\nfunc rightRotate(node: TreeNode?) -> TreeNode? {\n    let child = node?.left\n    let grandChild = child?.right\n    // Using child as pivot, rotate node to the right\n    child?.right = node\n    node?.left = grandChild\n    // Update node height\n    updateHeight(node: node)\n    updateHeight(node: child)\n    // Return root node of subtree after rotation\n    return child\n}\n
      avl_tree.js
      /* Right rotation operation */\n#rightRotate(node) {\n    const child = node.left;\n    const grandChild = child.right;\n    // Using child as pivot, rotate node to the right\n    child.right = node;\n    node.left = grandChild;\n    // Update node height\n    this.#updateHeight(node);\n    this.#updateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.ts
      /* Right rotation operation */\nrightRotate(node: TreeNode): TreeNode {\n    const child = node.left;\n    const grandChild = child.right;\n    // Using child as pivot, rotate node to the right\n    child.right = node;\n    node.left = grandChild;\n    // Update node height\n    this.updateHeight(node);\n    this.updateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.dart
      /* Right rotation operation */\nTreeNode? rightRotate(TreeNode? node) {\n  TreeNode? child = node!.left;\n  TreeNode? grandChild = child!.right;\n  // Using child as pivot, rotate node to the right\n  child.right = node;\n  node.left = grandChild;\n  // Update node height\n  updateHeight(node);\n  updateHeight(child);\n  // Return root node of subtree after rotation\n  return child;\n}\n
      avl_tree.rs
      /* Right rotation operation */\nfn right_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n    match node {\n        Some(node) => {\n            let child = node.borrow().left.clone().unwrap();\n            let grand_child = child.borrow().right.clone();\n            // Using child as pivot, rotate node to the right\n            child.borrow_mut().right = Some(node.clone());\n            node.borrow_mut().left = grand_child;\n            // Update node height\n            Self::update_height(Some(node));\n            Self::update_height(Some(child.clone()));\n            // Return root node of subtree after rotation\n            Some(child)\n        }\n        None => None,\n    }\n}\n
      avl_tree.c
      /* Right rotation operation */\nTreeNode *rightRotate(TreeNode *node) {\n    TreeNode *child, *grandChild;\n    child = node->left;\n    grandChild = child->right;\n    // Using child as pivot, rotate node to the right\n    child->right = node;\n    node->left = grandChild;\n    // Update node height\n    updateHeight(node);\n    updateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.kt
      /* Right rotation operation */\nfun rightRotate(node: TreeNode?): TreeNode {\n    val child = node!!.left\n    val grandChild = child!!.right\n    // Using child as pivot, rotate node to the right\n    child.right = node\n    node.left = grandChild\n    // Update node height\n    updateHeight(node)\n    updateHeight(child)\n    // Return root node of subtree after rotation\n    return child\n}\n
      avl_tree.rb
      ### Right rotation ###\ndef right_rotate(node)\n  child = node.left\n  grand_child = child.right\n  # Using child as pivot, rotate node to the right\n  child.right = node\n  node.left = grand_child\n  # Update node height\n  update_height(node)\n  update_height(child)\n  # Return root node of subtree after rotation\n  child\nend\n
      "},{"location":"chapter_tree/avl_tree/#2-left-rotation","title":"2. \u00a0 Left Rotation","text":"

      Correspondingly, if considering the \"mirror\" of the above unbalanced binary tree, the \"left rotation\" operation shown in Figure 7-28 needs to be performed.

      Figure 7-28 \u00a0 Left rotation operation

      Similarly, as shown in Figure 7-29, when the child node has a left child (denoted as grand_child), a step needs to be added in the left rotation: set grand_child as the right child of node.

      Figure 7-29 \u00a0 Left rotation with grand_child

      It can be observed that right rotation and left rotation operations are mirror symmetric in logic, and the two imbalance cases they solve are also symmetric. Based on symmetry, we only need to replace all left in the right rotation implementation code with right, and all right with left, to obtain the left rotation implementation code:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
      def left_rotate(self, node: TreeNode | None) -> TreeNode | None:\n    \"\"\"Left rotation operation\"\"\"\n    child = node.right\n    grand_child = child.left\n    # Using child as pivot, rotate node to the left\n    child.left = node\n    node.right = grand_child\n    # Update node height\n    self.update_height(node)\n    self.update_height(child)\n    # Return root node of subtree after rotation\n    return child\n
      avl_tree.cpp
      /* Left rotation operation */\nTreeNode *leftRotate(TreeNode *node) {\n    TreeNode *child = node->right;\n    TreeNode *grandChild = child->left;\n    // Using child as pivot, rotate node to the left\n    child->left = node;\n    node->right = grandChild;\n    // Update node height\n    updateHeight(node);\n    updateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.java
      /* Left rotation operation */\nTreeNode leftRotate(TreeNode node) {\n    TreeNode child = node.right;\n    TreeNode grandChild = child.left;\n    // Using child as pivot, rotate node to the left\n    child.left = node;\n    node.right = grandChild;\n    // Update node height\n    updateHeight(node);\n    updateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.cs
      /* Left rotation operation */\nTreeNode? LeftRotate(TreeNode? node) {\n    TreeNode? child = node?.right;\n    TreeNode? grandChild = child?.left;\n    // Using child as pivot, rotate node to the left\n    child.left = node;\n    node.right = grandChild;\n    // Update node height\n    UpdateHeight(node);\n    UpdateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.go
      /* Left rotation operation */\nfunc (t *aVLTree) leftRotate(node *TreeNode) *TreeNode {\n    child := node.Right\n    grandChild := child.Left\n    // Using child as pivot, rotate node to the left\n    child.Left = node\n    node.Right = grandChild\n    // Update node height\n    t.updateHeight(node)\n    t.updateHeight(child)\n    // Return root node of subtree after rotation\n    return child\n}\n
      avl_tree.swift
      /* Left rotation operation */\nfunc leftRotate(node: TreeNode?) -> TreeNode? {\n    let child = node?.right\n    let grandChild = child?.left\n    // Using child as pivot, rotate node to the left\n    child?.left = node\n    node?.right = grandChild\n    // Update node height\n    updateHeight(node: node)\n    updateHeight(node: child)\n    // Return root node of subtree after rotation\n    return child\n}\n
      avl_tree.js
      /* Left rotation operation */\n#leftRotate(node) {\n    const child = node.right;\n    const grandChild = child.left;\n    // Using child as pivot, rotate node to the left\n    child.left = node;\n    node.right = grandChild;\n    // Update node height\n    this.#updateHeight(node);\n    this.#updateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.ts
      /* Left rotation operation */\nleftRotate(node: TreeNode): TreeNode {\n    const child = node.right;\n    const grandChild = child.left;\n    // Using child as pivot, rotate node to the left\n    child.left = node;\n    node.right = grandChild;\n    // Update node height\n    this.updateHeight(node);\n    this.updateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.dart
      /* Left rotation operation */\nTreeNode? leftRotate(TreeNode? node) {\n  TreeNode? child = node!.right;\n  TreeNode? grandChild = child!.left;\n  // Using child as pivot, rotate node to the left\n  child.left = node;\n  node.right = grandChild;\n  // Update node height\n  updateHeight(node);\n  updateHeight(child);\n  // Return root node of subtree after rotation\n  return child;\n}\n
      avl_tree.rs
      /* Left rotation operation */\nfn left_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n    match node {\n        Some(node) => {\n            let child = node.borrow().right.clone().unwrap();\n            let grand_child = child.borrow().left.clone();\n            // Using child as pivot, rotate node to the left\n            child.borrow_mut().left = Some(node.clone());\n            node.borrow_mut().right = grand_child;\n            // Update node height\n            Self::update_height(Some(node));\n            Self::update_height(Some(child.clone()));\n            // Return root node of subtree after rotation\n            Some(child)\n        }\n        None => None,\n    }\n}\n
      avl_tree.c
      /* Left rotation operation */\nTreeNode *leftRotate(TreeNode *node) {\n    TreeNode *child, *grandChild;\n    child = node->right;\n    grandChild = child->left;\n    // Using child as pivot, rotate node to the left\n    child->left = node;\n    node->right = grandChild;\n    // Update node height\n    updateHeight(node);\n    updateHeight(child);\n    // Return root node of subtree after rotation\n    return child;\n}\n
      avl_tree.kt
      /* Left rotation operation */\nfun leftRotate(node: TreeNode?): TreeNode {\n    val child = node!!.right\n    val grandChild = child!!.left\n    // Using child as pivot, rotate node to the left\n    child.left = node\n    node.right = grandChild\n    // Update node height\n    updateHeight(node)\n    updateHeight(child)\n    // Return root node of subtree after rotation\n    return child\n}\n
      avl_tree.rb
      ### Left rotation ###\ndef left_rotate(node)\n  child = node.right\n  grand_child = child.left\n  # Using child as pivot, rotate node to the left\n  child.left = node\n  node.right = grand_child\n  # Update node height\n  update_height(node)\n  update_height(child)\n  # Return root node of subtree after rotation\n  child\nend\n
      "},{"location":"chapter_tree/avl_tree/#3-left-rotation-then-right-rotation","title":"3. \u00a0 Left Rotation Then Right Rotation","text":"

      For the unbalanced node 3 in Figure 7-30, using either left rotation or right rotation alone cannot restore the subtree to balance. In this case, a \"left rotation\" needs to be performed on child first, followed by a \"right rotation\" on node.

      Figure 7-30 \u00a0 Left-right rotation

      "},{"location":"chapter_tree/avl_tree/#4-right-rotation-then-left-rotation","title":"4. \u00a0 Right Rotation Then Left Rotation","text":"

      As shown in Figure 7-31, for the mirror case of the above unbalanced binary tree, a \"right rotation\" needs to be performed on child first, then a \"left rotation\" on node.

      Figure 7-31 \u00a0 Right-left rotation

      "},{"location":"chapter_tree/avl_tree/#5-choice-of-rotation","title":"5. \u00a0 Choice of Rotation","text":"

      The four imbalances shown in Figure 7-32 correspond one-to-one with the above cases, requiring right rotation, left rotation then right rotation, right rotation then left rotation, and left rotation operations respectively.

      Figure 7-32 \u00a0 The four rotation cases of AVL tree

      As shown in Table 7-3, we determine which case the unbalanced node belongs to by judging the signs of the balance factor of the unbalanced node and the balance factor of its taller-side child node.

      Table 7-3 \u00a0 Conditions for Choosing Among the Four Rotation Cases

      Balance factor of the unbalanced node Balance factor of the child node Rotation method to apply \\(> 1\\) (left-leaning tree) \\(\\geq 0\\) Right rotation \\(> 1\\) (left-leaning tree) \\(<0\\) Left rotation then right rotation \\(< -1\\) (right-leaning tree) \\(\\leq 0\\) Left rotation \\(< -1\\) (right-leaning tree) \\(>0\\) Right rotation then left rotation

      For ease of use, we encapsulate the rotation operations into a function. With this function, we can perform rotations for various imbalance situations, restoring balance to unbalanced nodes. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
      def rotate(self, node: TreeNode | None) -> TreeNode | None:\n    \"\"\"Perform rotation operation to restore balance to this subtree\"\"\"\n    # Get balance factor of node\n    balance_factor = self.balance_factor(node)\n    # Left-leaning tree\n    if balance_factor > 1:\n        if self.balance_factor(node.left) >= 0:\n            # Right rotation\n            return self.right_rotate(node)\n        else:\n            # First left rotation then right rotation\n            node.left = self.left_rotate(node.left)\n            return self.right_rotate(node)\n    # Right-leaning tree\n    elif balance_factor < -1:\n        if self.balance_factor(node.right) <= 0:\n            # Left rotation\n            return self.left_rotate(node)\n        else:\n            # First right rotation then left rotation\n            node.right = self.right_rotate(node.right)\n            return self.left_rotate(node)\n    # Balanced tree, no rotation needed, return directly\n    return node\n
      avl_tree.cpp
      /* Perform rotation operation to restore balance to this subtree */\nTreeNode *rotate(TreeNode *node) {\n    // Get balance factor of node\n    int _balanceFactor = balanceFactor(node);\n    // Left-leaning tree\n    if (_balanceFactor > 1) {\n        if (balanceFactor(node->left) >= 0) {\n            // Right rotation\n            return rightRotate(node);\n        } else {\n            // First left rotation then right rotation\n            node->left = leftRotate(node->left);\n            return rightRotate(node);\n        }\n    }\n    // Right-leaning tree\n    if (_balanceFactor < -1) {\n        if (balanceFactor(node->right) <= 0) {\n            // Left rotation\n            return leftRotate(node);\n        } else {\n            // First right rotation then left rotation\n            node->right = rightRotate(node->right);\n            return leftRotate(node);\n        }\n    }\n    // Balanced tree, no rotation needed, return directly\n    return node;\n}\n
      avl_tree.java
      /* Perform rotation operation to restore balance to this subtree */\nTreeNode rotate(TreeNode node) {\n    // Get balance factor of node\n    int balanceFactor = balanceFactor(node);\n    // Left-leaning tree\n    if (balanceFactor > 1) {\n        if (balanceFactor(node.left) >= 0) {\n            // Right rotation\n            return rightRotate(node);\n        } else {\n            // First left rotation then right rotation\n            node.left = leftRotate(node.left);\n            return rightRotate(node);\n        }\n    }\n    // Right-leaning tree\n    if (balanceFactor < -1) {\n        if (balanceFactor(node.right) <= 0) {\n            // Left rotation\n            return leftRotate(node);\n        } else {\n            // First right rotation then left rotation\n            node.right = rightRotate(node.right);\n            return leftRotate(node);\n        }\n    }\n    // Balanced tree, no rotation needed, return directly\n    return node;\n}\n
      avl_tree.cs
      /* Perform rotation operation to restore balance to this subtree */\nTreeNode? Rotate(TreeNode? node) {\n    // Get balance factor of node\n    int balanceFactorInt = BalanceFactor(node);\n    // Left-leaning tree\n    if (balanceFactorInt > 1) {\n        if (BalanceFactor(node?.left) >= 0) {\n            // Right rotation\n            return RightRotate(node);\n        } else {\n            // First left rotation then right rotation\n            node!.left = LeftRotate(node!.left);\n            return RightRotate(node);\n        }\n    }\n    // Right-leaning tree\n    if (balanceFactorInt < -1) {\n        if (BalanceFactor(node?.right) <= 0) {\n            // Left rotation\n            return LeftRotate(node);\n        } else {\n            // First right rotation then left rotation\n            node!.right = RightRotate(node!.right);\n            return LeftRotate(node);\n        }\n    }\n    // Balanced tree, no rotation needed, return directly\n    return node;\n}\n
      avl_tree.go
      /* Perform rotation operation to restore balance to this subtree */\nfunc (t *aVLTree) rotate(node *TreeNode) *TreeNode {\n    // Get balance factor of node\n    // Go recommends short variables, here bf refers to t.balanceFactor\n    bf := t.balanceFactor(node)\n    // Left-leaning tree\n    if bf > 1 {\n        if t.balanceFactor(node.Left) >= 0 {\n            // Right rotation\n            return t.rightRotate(node)\n        } else {\n            // First left rotation then right rotation\n            node.Left = t.leftRotate(node.Left)\n            return t.rightRotate(node)\n        }\n    }\n    // Right-leaning tree\n    if bf < -1 {\n        if t.balanceFactor(node.Right) <= 0 {\n            // Left rotation\n            return t.leftRotate(node)\n        } else {\n            // First right rotation then left rotation\n            node.Right = t.rightRotate(node.Right)\n            return t.leftRotate(node)\n        }\n    }\n    // Balanced tree, no rotation needed, return directly\n    return node\n}\n
      avl_tree.swift
      /* Perform rotation operation to restore balance to this subtree */\nfunc rotate(node: TreeNode?) -> TreeNode? {\n    // Get balance factor of node\n    let balanceFactor = balanceFactor(node: node)\n    // Left-leaning tree\n    if balanceFactor > 1 {\n        if self.balanceFactor(node: node?.left) >= 0 {\n            // Right rotation\n            return rightRotate(node: node)\n        } else {\n            // First left rotation then right rotation\n            node?.left = leftRotate(node: node?.left)\n            return rightRotate(node: node)\n        }\n    }\n    // Right-leaning tree\n    if balanceFactor < -1 {\n        if self.balanceFactor(node: node?.right) <= 0 {\n            // Left rotation\n            return leftRotate(node: node)\n        } else {\n            // First right rotation then left rotation\n            node?.right = rightRotate(node: node?.right)\n            return leftRotate(node: node)\n        }\n    }\n    // Balanced tree, no rotation needed, return directly\n    return node\n}\n
      avl_tree.js
      /* Perform rotation operation to restore balance to this subtree */\n#rotate(node) {\n    // Get balance factor of node\n    const balanceFactor = this.balanceFactor(node);\n    // Left-leaning tree\n    if (balanceFactor > 1) {\n        if (this.balanceFactor(node.left) >= 0) {\n            // Right rotation\n            return this.#rightRotate(node);\n        } else {\n            // First left rotation then right rotation\n            node.left = this.#leftRotate(node.left);\n            return this.#rightRotate(node);\n        }\n    }\n    // Right-leaning tree\n    if (balanceFactor < -1) {\n        if (this.balanceFactor(node.right) <= 0) {\n            // Left rotation\n            return this.#leftRotate(node);\n        } else {\n            // First right rotation then left rotation\n            node.right = this.#rightRotate(node.right);\n            return this.#leftRotate(node);\n        }\n    }\n    // Balanced tree, no rotation needed, return directly\n    return node;\n}\n
      avl_tree.ts
      /* Perform rotation operation to restore balance to this subtree */\nrotate(node: TreeNode): TreeNode {\n    // Get balance factor of node\n    const balanceFactor = this.balanceFactor(node);\n    // Left-leaning tree\n    if (balanceFactor > 1) {\n        if (this.balanceFactor(node.left) >= 0) {\n            // Right rotation\n            return this.rightRotate(node);\n        } else {\n            // First left rotation then right rotation\n            node.left = this.leftRotate(node.left);\n            return this.rightRotate(node);\n        }\n    }\n    // Right-leaning tree\n    if (balanceFactor < -1) {\n        if (this.balanceFactor(node.right) <= 0) {\n            // Left rotation\n            return this.leftRotate(node);\n        } else {\n            // First right rotation then left rotation\n            node.right = this.rightRotate(node.right);\n            return this.leftRotate(node);\n        }\n    }\n    // Balanced tree, no rotation needed, return directly\n    return node;\n}\n
      avl_tree.dart
      /* Perform rotation operation to restore balance to this subtree */\nTreeNode? rotate(TreeNode? node) {\n  // Get balance factor of node\n  int factor = balanceFactor(node);\n  // Left-leaning tree\n  if (factor > 1) {\n    if (balanceFactor(node!.left) >= 0) {\n      // Right rotation\n      return rightRotate(node);\n    } else {\n      // First left rotation then right rotation\n      node.left = leftRotate(node.left);\n      return rightRotate(node);\n    }\n  }\n  // Right-leaning tree\n  if (factor < -1) {\n    if (balanceFactor(node!.right) <= 0) {\n      // Left rotation\n      return leftRotate(node);\n    } else {\n      // First right rotation then left rotation\n      node.right = rightRotate(node.right);\n      return leftRotate(node);\n    }\n  }\n  // Balanced tree, no rotation needed, return directly\n  return node;\n}\n
      avl_tree.rs
      /* Perform rotation operation to restore balance to this subtree */\nfn rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n    // Get balance factor of node\n    let balance_factor = Self::balance_factor(node.clone());\n    // Left-leaning tree\n    if balance_factor > 1 {\n        let node = node.unwrap();\n        if Self::balance_factor(node.borrow().left.clone()) >= 0 {\n            // Right rotation\n            Self::right_rotate(Some(node))\n        } else {\n            // First left rotation then right rotation\n            let left = node.borrow().left.clone();\n            node.borrow_mut().left = Self::left_rotate(left);\n            Self::right_rotate(Some(node))\n        }\n    }\n    // Right-leaning tree\n    else if balance_factor < -1 {\n        let node = node.unwrap();\n        if Self::balance_factor(node.borrow().right.clone()) <= 0 {\n            // Left rotation\n            Self::left_rotate(Some(node))\n        } else {\n            // First right rotation then left rotation\n            let right = node.borrow().right.clone();\n            node.borrow_mut().right = Self::right_rotate(right);\n            Self::left_rotate(Some(node))\n        }\n    } else {\n        // Balanced tree, no rotation needed, return directly\n        node\n    }\n}\n
      avl_tree.c
      /* Perform rotation operation to restore balance to this subtree */\nTreeNode *rotate(TreeNode *node) {\n    // Get balance factor of node\n    int bf = balanceFactor(node);\n    // Left-leaning tree\n    if (bf > 1) {\n        if (balanceFactor(node->left) >= 0) {\n            // Right rotation\n            return rightRotate(node);\n        } else {\n            // First left rotation then right rotation\n            node->left = leftRotate(node->left);\n            return rightRotate(node);\n        }\n    }\n    // Right-leaning tree\n    if (bf < -1) {\n        if (balanceFactor(node->right) <= 0) {\n            // Left rotation\n            return leftRotate(node);\n        } else {\n            // First right rotation then left rotation\n            node->right = rightRotate(node->right);\n            return leftRotate(node);\n        }\n    }\n    // Balanced tree, no rotation needed, return directly\n    return node;\n}\n
      avl_tree.kt
      /* Perform rotation operation to restore balance to this subtree */\nfun rotate(node: TreeNode): TreeNode {\n    // Get balance factor of node\n    val balanceFactor = balanceFactor(node)\n    // Left-leaning tree\n    if (balanceFactor > 1) {\n        if (balanceFactor(node.left) >= 0) {\n            // Right rotation\n            return rightRotate(node)\n        } else {\n            // First left rotation then right rotation\n            node.left = leftRotate(node.left)\n            return rightRotate(node)\n        }\n    }\n    // Right-leaning tree\n    if (balanceFactor < -1) {\n        if (balanceFactor(node.right) <= 0) {\n            // Left rotation\n            return leftRotate(node)\n        } else {\n            // First right rotation then left rotation\n            node.right = rightRotate(node.right)\n            return leftRotate(node)\n        }\n    }\n    // Balanced tree, no rotation needed, return directly\n    return node\n}\n
      avl_tree.rb
      ### Perform rotation to rebalance subtree ###\ndef rotate(node)\n  # Get balance factor of node\n  balance_factor = balance_factor(node)\n  # Left-heavy tree\n  if balance_factor > 1\n    if balance_factor(node.left) >= 0\n      # Right rotation\n      return right_rotate(node)\n    else\n      # First left rotation then right rotation\n      node.left = left_rotate(node.left)\n      return right_rotate(node)\n    end\n  # Right-heavy tree\n  elsif balance_factor < -1\n    if balance_factor(node.right) <= 0\n      # Left rotation\n      return left_rotate(node)\n    else\n      # First right rotation then left rotation\n      node.right = right_rotate(node.right)\n      return left_rotate(node)\n    end\n  end\n  # Balanced tree, no rotation needed, return directly\n  node\nend\n
      "},{"location":"chapter_tree/avl_tree/#753-common-operations-in-avl-trees","title":"7.5.3 \u00a0 Common Operations in Avl Trees","text":""},{"location":"chapter_tree/avl_tree/#1-node-insertion","title":"1. \u00a0 Node Insertion","text":"

      The node insertion operation in AVL trees is similar in principle to that in binary search trees. The only difference is that after inserting a node in an AVL tree, a series of unbalanced nodes may appear on the path from that node to the root. Therefore, we need to start from this node and perform rotation operations from bottom to top, restoring balance to all unbalanced nodes. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
      def insert(self, val):\n    \"\"\"Insert node\"\"\"\n    self._root = self.insert_helper(self._root, val)\n\ndef insert_helper(self, node: TreeNode | None, val: int) -> TreeNode:\n    \"\"\"Recursively insert node (helper method)\"\"\"\n    if node is None:\n        return TreeNode(val)\n    # 1. Find insertion position and insert node\n    if val < node.val:\n        node.left = self.insert_helper(node.left, val)\n    elif val > node.val:\n        node.right = self.insert_helper(node.right, val)\n    else:\n        # Duplicate node not inserted, return directly\n        return node\n    # Update node height\n    self.update_height(node)\n    # 2. Perform rotation operation to restore balance to this subtree\n    return self.rotate(node)\n
      avl_tree.cpp
      /* Insert node */\nvoid insert(int val) {\n    root = insertHelper(root, val);\n}\n\n/* Recursively insert node (helper method) */\nTreeNode *insertHelper(TreeNode *node, int val) {\n    if (node == nullptr)\n        return new TreeNode(val);\n    /* 1. Find insertion position and insert node */\n    if (val < node->val)\n        node->left = insertHelper(node->left, val);\n    else if (val > node->val)\n        node->right = insertHelper(node->right, val);\n    else\n        return node;    // Duplicate node not inserted, return directly\n    updateHeight(node); // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.java
      /* Insert node */\nvoid insert(int val) {\n    root = insertHelper(root, val);\n}\n\n/* Recursively insert node (helper method) */\nTreeNode insertHelper(TreeNode node, int val) {\n    if (node == null)\n        return new TreeNode(val);\n    /* 1. Find insertion position and insert node */\n    if (val < node.val)\n        node.left = insertHelper(node.left, val);\n    else if (val > node.val)\n        node.right = insertHelper(node.right, val);\n    else\n        return node; // Duplicate node not inserted, return directly\n    updateHeight(node); // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.cs
      /* Insert node */\nvoid Insert(int val) {\n    root = InsertHelper(root, val);\n}\n\n/* Recursively insert node (helper method) */\nTreeNode? InsertHelper(TreeNode? node, int val) {\n    if (node == null) return new TreeNode(val);\n    /* 1. Find insertion position and insert node */\n    if (val < node.val)\n        node.left = InsertHelper(node.left, val);\n    else if (val > node.val)\n        node.right = InsertHelper(node.right, val);\n    else\n        return node;     // Duplicate node not inserted, return directly\n    UpdateHeight(node);  // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = Rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.go
      /* Insert node */\nfunc (t *aVLTree) insert(val int) {\n    t.root = t.insertHelper(t.root, val)\n}\n\n/* Recursively insert node (helper function) */\nfunc (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode {\n    if node == nil {\n        return NewTreeNode(val)\n    }\n    /* 1. Find insertion position and insert node */\n    if val < node.Val.(int) {\n        node.Left = t.insertHelper(node.Left, val)\n    } else if val > node.Val.(int) {\n        node.Right = t.insertHelper(node.Right, val)\n    } else {\n        // Duplicate node not inserted, return directly\n        return node\n    }\n    // Update node height\n    t.updateHeight(node)\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = t.rotate(node)\n    // Return root node of subtree\n    return node\n}\n
      avl_tree.swift
      /* Insert node */\nfunc insert(val: Int) {\n    root = insertHelper(node: root, val: val)\n}\n\n/* Recursively insert node (helper method) */\nfunc insertHelper(node: TreeNode?, val: Int) -> TreeNode? {\n    var node = node\n    if node == nil {\n        return TreeNode(x: val)\n    }\n    /* 1. Find insertion position and insert node */\n    if val < node!.val {\n        node?.left = insertHelper(node: node?.left, val: val)\n    } else if val > node!.val {\n        node?.right = insertHelper(node: node?.right, val: val)\n    } else {\n        return node // Duplicate node not inserted, return directly\n    }\n    updateHeight(node: node) // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = rotate(node: node)\n    // Return root node of subtree\n    return node\n}\n
      avl_tree.js
      /* Insert node */\ninsert(val) {\n    this.root = this.#insertHelper(this.root, val);\n}\n\n/* Recursively insert node (helper method) */\n#insertHelper(node, val) {\n    if (node === null) return new TreeNode(val);\n    /* 1. Find insertion position and insert node */\n    if (val < node.val) node.left = this.#insertHelper(node.left, val);\n    else if (val > node.val)\n        node.right = this.#insertHelper(node.right, val);\n    else return node; // Duplicate node not inserted, return directly\n    this.#updateHeight(node); // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = this.#rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.ts
      /* Insert node */\ninsert(val: number): void {\n    this.root = this.insertHelper(this.root, val);\n}\n\n/* Recursively insert node (helper method) */\ninsertHelper(node: TreeNode, val: number): TreeNode {\n    if (node === null) return new TreeNode(val);\n    /* 1. Find insertion position and insert node */\n    if (val < node.val) {\n        node.left = this.insertHelper(node.left, val);\n    } else if (val > node.val) {\n        node.right = this.insertHelper(node.right, val);\n    } else {\n        return node; // Duplicate node not inserted, return directly\n    }\n    this.updateHeight(node); // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = this.rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.dart
      /* Insert node */\nvoid insert(int val) {\n  root = insertHelper(root, val);\n}\n\n/* Recursively insert node (helper method) */\nTreeNode? insertHelper(TreeNode? node, int val) {\n  if (node == null) return TreeNode(val);\n  /* 1. Find insertion position and insert node */\n  if (val < node.val)\n    node.left = insertHelper(node.left, val);\n  else if (val > node.val)\n    node.right = insertHelper(node.right, val);\n  else\n    return node; // Duplicate node not inserted, return directly\n  updateHeight(node); // Update node height\n  /* 2. Perform rotation operation to restore balance to this subtree */\n  node = rotate(node);\n  // Return root node of subtree\n  return node;\n}\n
      avl_tree.rs
      /* Insert node */\nfn insert(&mut self, val: i32) {\n    self.root = Self::insert_helper(self.root.clone(), val);\n}\n\n/* Recursively insert node (helper method) */\nfn insert_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {\n    match node {\n        Some(mut node) => {\n            /* 1. Find insertion position and insert node */\n            match {\n                let node_val = node.borrow().val;\n                node_val\n            }\n            .cmp(&val)\n            {\n                Ordering::Greater => {\n                    let left = node.borrow().left.clone();\n                    node.borrow_mut().left = Self::insert_helper(left, val);\n                }\n                Ordering::Less => {\n                    let right = node.borrow().right.clone();\n                    node.borrow_mut().right = Self::insert_helper(right, val);\n                }\n                Ordering::Equal => {\n                    return Some(node); // Duplicate node not inserted, return directly\n                }\n            }\n            Self::update_height(Some(node.clone())); // Update node height\n\n            /* 2. Perform rotation operation to restore balance to this subtree */\n            node = Self::rotate(Some(node)).unwrap();\n            // Return root node of subtree\n            Some(node)\n        }\n        None => Some(TreeNode::new(val)),\n    }\n}\n
      avl_tree.c
      /* Insert node */\nvoid insert(AVLTree *tree, int val) {\n    tree->root = insertHelper(tree->root, val);\n}\n\n/* Recursively insert node (helper function) */\nTreeNode *insertHelper(TreeNode *node, int val) {\n    if (node == NULL) {\n        return newTreeNode(val);\n    }\n    /* 1. Find insertion position and insert node */\n    if (val < node->val) {\n        node->left = insertHelper(node->left, val);\n    } else if (val > node->val) {\n        node->right = insertHelper(node->right, val);\n    } else {\n        // Duplicate node not inserted, return directly\n        return node;\n    }\n    // Update node height\n    updateHeight(node);\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.kt
      /* Insert node */\nfun insert(_val: Int) {\n    root = insertHelper(root, _val)\n}\n\n/* Recursively insert node (helper method) */\nfun insertHelper(n: TreeNode?, _val: Int): TreeNode {\n    if (n == null)\n        return TreeNode(_val)\n    var node = n\n    /* 1. Find insertion position and insert node */\n    if (_val < node._val)\n        node.left = insertHelper(node.left, _val)\n    else if (_val > node._val)\n        node.right = insertHelper(node.right, _val)\n    else\n        return node // Duplicate node not inserted, return directly\n    updateHeight(node) // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = rotate(node)\n    // Return root node of subtree\n    return node\n}\n
      avl_tree.rb
      ### Insert node ###\ndef insert(val)\n  @root = insert_helper(@root, val)\nend\n\n### Recursively insert node (helper method) ###\ndef insert_helper(node, val)\n  return TreeNode.new(val) if node.nil?\n  # 1. Find insertion position and insert node\n  if val < node.val\n    node.left = insert_helper(node.left, val)\n  elsif val > node.val\n    node.right = insert_helper(node.right, val)\n  else\n    # Duplicate node not inserted, return directly\n    return node\n  end\n  # Update node height\n  update_height(node)\n  # 2. Perform rotation operation to restore balance to this subtree\n  rotate(node)\nend\n
      "},{"location":"chapter_tree/avl_tree/#2-node-removal","title":"2. \u00a0 Node Removal","text":"

      Similarly, on the basis of the binary search tree's node removal method, rotation operations need to be performed from bottom to top to restore balance to all unbalanced nodes. The code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
      def remove(self, val: int):\n    \"\"\"Delete node\"\"\"\n    self._root = self.remove_helper(self._root, val)\n\ndef remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None:\n    \"\"\"Recursively delete node (helper method)\"\"\"\n    if node is None:\n        return None\n    # 1. Find node and delete\n    if val < node.val:\n        node.left = self.remove_helper(node.left, val)\n    elif val > node.val:\n        node.right = self.remove_helper(node.right, val)\n    else:\n        if node.left is None or node.right is None:\n            child = node.left or node.right\n            # Number of child nodes = 0, delete node directly and return\n            if child is None:\n                return None\n            # Number of child nodes = 1, delete node directly\n            else:\n                node = child\n        else:\n            # Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n            temp = node.right\n            while temp.left is not None:\n                temp = temp.left\n            node.right = self.remove_helper(node.right, temp.val)\n            node.val = temp.val\n    # Update node height\n    self.update_height(node)\n    # 2. Perform rotation operation to restore balance to this subtree\n    return self.rotate(node)\n
      avl_tree.cpp
      /* Remove node */\nvoid remove(int val) {\n    root = removeHelper(root, val);\n}\n\n/* Recursively delete node (helper method) */\nTreeNode *removeHelper(TreeNode *node, int val) {\n    if (node == nullptr)\n        return nullptr;\n    /* 1. Find node and delete */\n    if (val < node->val)\n        node->left = removeHelper(node->left, val);\n    else if (val > node->val)\n        node->right = removeHelper(node->right, val);\n    else {\n        if (node->left == nullptr || node->right == nullptr) {\n            TreeNode *child = node->left != nullptr ? node->left : node->right;\n            // Number of child nodes = 0, delete node directly and return\n            if (child == nullptr) {\n                delete node;\n                return nullptr;\n            }\n            // Number of child nodes = 1, delete node directly\n            else {\n                delete node;\n                node = child;\n            }\n        } else {\n            // Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n            TreeNode *temp = node->right;\n            while (temp->left != nullptr) {\n                temp = temp->left;\n            }\n            int tempVal = temp->val;\n            node->right = removeHelper(node->right, temp->val);\n            node->val = tempVal;\n        }\n    }\n    updateHeight(node); // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.java
      /* Remove node */\nvoid remove(int val) {\n    root = removeHelper(root, val);\n}\n\n/* Recursively delete node (helper method) */\nTreeNode removeHelper(TreeNode node, int val) {\n    if (node == null)\n        return null;\n    /* 1. Find node and delete */\n    if (val < node.val)\n        node.left = removeHelper(node.left, val);\n    else if (val > node.val)\n        node.right = removeHelper(node.right, val);\n    else {\n        if (node.left == null || node.right == null) {\n            TreeNode child = node.left != null ? node.left : node.right;\n            // Number of child nodes = 0, delete node directly and return\n            if (child == null)\n                return null;\n            // Number of child nodes = 1, delete node directly\n            else\n                node = child;\n        } else {\n            // Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n            TreeNode temp = node.right;\n            while (temp.left != null) {\n                temp = temp.left;\n            }\n            node.right = removeHelper(node.right, temp.val);\n            node.val = temp.val;\n        }\n    }\n    updateHeight(node); // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.cs
      /* Remove node */\nvoid Remove(int val) {\n    root = RemoveHelper(root, val);\n}\n\n/* Recursively delete node (helper method) */\nTreeNode? RemoveHelper(TreeNode? node, int val) {\n    if (node == null) return null;\n    /* 1. Find node and delete */\n    if (val < node.val)\n        node.left = RemoveHelper(node.left, val);\n    else if (val > node.val)\n        node.right = RemoveHelper(node.right, val);\n    else {\n        if (node.left == null || node.right == null) {\n            TreeNode? child = node.left ?? node.right;\n            // Number of child nodes = 0, delete node directly and return\n            if (child == null)\n                return null;\n            // Number of child nodes = 1, delete node directly\n            else\n                node = child;\n        } else {\n            // Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n            TreeNode? temp = node.right;\n            while (temp.left != null) {\n                temp = temp.left;\n            }\n            node.right = RemoveHelper(node.right, temp.val!.Value);\n            node.val = temp.val;\n        }\n    }\n    UpdateHeight(node);  // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = Rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.go
      /* Remove node */\nfunc (t *aVLTree) remove(val int) {\n    t.root = t.removeHelper(t.root, val)\n}\n\n/* Recursively remove node (helper function) */\nfunc (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {\n    if node == nil {\n        return nil\n    }\n    /* 1. Find node and delete */\n    if val < node.Val.(int) {\n        node.Left = t.removeHelper(node.Left, val)\n    } else if val > node.Val.(int) {\n        node.Right = t.removeHelper(node.Right, val)\n    } else {\n        if node.Left == nil || node.Right == nil {\n            child := node.Left\n            if node.Right != nil {\n                child = node.Right\n            }\n            if child == nil {\n                // Number of child nodes = 0, delete node directly and return\n                return nil\n            } else {\n                // Number of child nodes = 1, delete node directly\n                node = child\n            }\n        } else {\n            // Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n            temp := node.Right\n            for temp.Left != nil {\n                temp = temp.Left\n            }\n            node.Right = t.removeHelper(node.Right, temp.Val.(int))\n            node.Val = temp.Val\n        }\n    }\n    // Update node height\n    t.updateHeight(node)\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = t.rotate(node)\n    // Return root node of subtree\n    return node\n}\n
      avl_tree.swift
      /* Remove node */\nfunc remove(val: Int) {\n    root = removeHelper(node: root, val: val)\n}\n\n/* Recursively delete node (helper method) */\nfunc removeHelper(node: TreeNode?, val: Int) -> TreeNode? {\n    var node = node\n    if node == nil {\n        return nil\n    }\n    /* 1. Find node and delete */\n    if val < node!.val {\n        node?.left = removeHelper(node: node?.left, val: val)\n    } else if val > node!.val {\n        node?.right = removeHelper(node: node?.right, val: val)\n    } else {\n        if node?.left == nil || node?.right == nil {\n            let child = node?.left ?? node?.right\n            // Number of child nodes = 0, delete node directly and return\n            if child == nil {\n                return nil\n            }\n            // Number of child nodes = 1, delete node directly\n            else {\n                node = child\n            }\n        } else {\n            // Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n            var temp = node?.right\n            while temp?.left != nil {\n                temp = temp?.left\n            }\n            node?.right = removeHelper(node: node?.right, val: temp!.val)\n            node?.val = temp!.val\n        }\n    }\n    updateHeight(node: node) // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = rotate(node: node)\n    // Return root node of subtree\n    return node\n}\n
      avl_tree.js
      /* Remove node */\nremove(val) {\n    this.root = this.#removeHelper(this.root, val);\n}\n\n/* Recursively delete node (helper method) */\n#removeHelper(node, val) {\n    if (node === null) return null;\n    /* 1. Find node and delete */\n    if (val < node.val) node.left = this.#removeHelper(node.left, val);\n    else if (val > node.val)\n        node.right = this.#removeHelper(node.right, val);\n    else {\n        if (node.left === null || node.right === null) {\n            const child = node.left !== null ? node.left : node.right;\n            // Number of child nodes = 0, delete node directly and return\n            if (child === null) return null;\n            // Number of child nodes = 1, delete node directly\n            else node = child;\n        } else {\n            // Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n            let temp = node.right;\n            while (temp.left !== null) {\n                temp = temp.left;\n            }\n            node.right = this.#removeHelper(node.right, temp.val);\n            node.val = temp.val;\n        }\n    }\n    this.#updateHeight(node); // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = this.#rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.ts
      /* Remove node */\nremove(val: number): void {\n    this.root = this.removeHelper(this.root, val);\n}\n\n/* Recursively delete node (helper method) */\nremoveHelper(node: TreeNode, val: number): TreeNode {\n    if (node === null) return null;\n    /* 1. Find node and delete */\n    if (val < node.val) {\n        node.left = this.removeHelper(node.left, val);\n    } else if (val > node.val) {\n        node.right = this.removeHelper(node.right, val);\n    } else {\n        if (node.left === null || node.right === null) {\n            const child = node.left !== null ? node.left : node.right;\n            // Number of child nodes = 0, delete node directly and return\n            if (child === null) {\n                return null;\n            } else {\n                // Number of child nodes = 1, delete node directly\n                node = child;\n            }\n        } else {\n            // Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n            let temp = node.right;\n            while (temp.left !== null) {\n                temp = temp.left;\n            }\n            node.right = this.removeHelper(node.right, temp.val);\n            node.val = temp.val;\n        }\n    }\n    this.updateHeight(node); // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = this.rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.dart
      /* Remove node */\nvoid remove(int val) {\n  root = removeHelper(root, val);\n}\n\n/* Recursively delete node (helper method) */\nTreeNode? removeHelper(TreeNode? node, int val) {\n  if (node == null) return null;\n  /* 1. Find node and delete */\n  if (val < node.val)\n    node.left = removeHelper(node.left, val);\n  else if (val > node.val)\n    node.right = removeHelper(node.right, val);\n  else {\n    if (node.left == null || node.right == null) {\n      TreeNode? child = node.left ?? node.right;\n      // Number of child nodes = 0, delete node directly and return\n      if (child == null)\n        return null;\n      // Number of child nodes = 1, delete node directly\n      else\n        node = child;\n    } else {\n      // Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n      TreeNode? temp = node.right;\n      while (temp!.left != null) {\n        temp = temp.left;\n      }\n      node.right = removeHelper(node.right, temp.val);\n      node.val = temp.val;\n    }\n  }\n  updateHeight(node); // Update node height\n  /* 2. Perform rotation operation to restore balance to this subtree */\n  node = rotate(node);\n  // Return root node of subtree\n  return node;\n}\n
      avl_tree.rs
      /* Remove node */\nfn remove(&self, val: i32) {\n    Self::remove_helper(self.root.clone(), val);\n}\n\n/* Recursively delete node (helper method) */\nfn remove_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {\n    match node {\n        Some(mut node) => {\n            /* 1. Find node and delete */\n            if val < node.borrow().val {\n                let left = node.borrow().left.clone();\n                node.borrow_mut().left = Self::remove_helper(left, val);\n            } else if val > node.borrow().val {\n                let right = node.borrow().right.clone();\n                node.borrow_mut().right = Self::remove_helper(right, val);\n            } else if node.borrow().left.is_none() || node.borrow().right.is_none() {\n                let child = if node.borrow().left.is_some() {\n                    node.borrow().left.clone()\n                } else {\n                    node.borrow().right.clone()\n                };\n                match child {\n                    // Number of child nodes = 0, delete node directly and return\n                    None => {\n                        return None;\n                    }\n                    // Number of child nodes = 1, delete node directly\n                    Some(child) => node = child,\n                }\n            } else {\n                // Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n                let mut temp = node.borrow().right.clone().unwrap();\n                loop {\n                    let temp_left = temp.borrow().left.clone();\n                    if temp_left.is_none() {\n                        break;\n                    }\n                    temp = temp_left.unwrap();\n                }\n                let right = node.borrow().right.clone();\n                node.borrow_mut().right = Self::remove_helper(right, temp.borrow().val);\n                node.borrow_mut().val = temp.borrow().val;\n            }\n            Self::update_height(Some(node.clone())); // Update node height\n\n            /* 2. Perform rotation operation to restore balance to this subtree */\n            node = Self::rotate(Some(node)).unwrap();\n            // Return root node of subtree\n            Some(node)\n        }\n        None => None,\n    }\n}\n
      avl_tree.c
      /* Remove node */\n// Cannot use remove keyword here due to stdio.h inclusion\nvoid removeItem(AVLTree *tree, int val) {\n    TreeNode *root = removeHelper(tree->root, val);\n}\n\n/* Recursively remove node (helper function) */\nTreeNode *removeHelper(TreeNode *node, int val) {\n    TreeNode *child, *grandChild;\n    if (node == NULL) {\n        return NULL;\n    }\n    /* 1. Find node and delete */\n    if (val < node->val) {\n        node->left = removeHelper(node->left, val);\n    } else if (val > node->val) {\n        node->right = removeHelper(node->right, val);\n    } else {\n        if (node->left == NULL || node->right == NULL) {\n            child = node->left;\n            if (node->right != NULL) {\n                child = node->right;\n            }\n            // Number of child nodes = 0, delete node directly and return\n            if (child == NULL) {\n                return NULL;\n            } else {\n                // Number of child nodes = 1, delete node directly\n                node = child;\n            }\n        } else {\n            // Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n            TreeNode *temp = node->right;\n            while (temp->left != NULL) {\n                temp = temp->left;\n            }\n            int tempVal = temp->val;\n            node->right = removeHelper(node->right, temp->val);\n            node->val = tempVal;\n        }\n    }\n    // Update node height\n    updateHeight(node);\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = rotate(node);\n    // Return root node of subtree\n    return node;\n}\n
      avl_tree.kt
      /* Remove node */\nfun remove(_val: Int) {\n    root = removeHelper(root, _val)\n}\n\n/* Recursively delete node (helper method) */\nfun removeHelper(n: TreeNode?, _val: Int): TreeNode? {\n    var node = n ?: return null\n    /* 1. Find node and delete */\n    if (_val < node._val)\n        node.left = removeHelper(node.left, _val)\n    else if (_val > node._val)\n        node.right = removeHelper(node.right, _val)\n    else {\n        if (node.left == null || node.right == null) {\n            val child = if (node.left != null)\n                node.left\n            else\n                node.right\n            // Number of child nodes = 0, delete node directly and return\n            if (child == null)\n                return null\n            // Number of child nodes = 1, delete node directly\n            else\n                node = child\n        } else {\n            // Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n            var temp = node.right\n            while (temp!!.left != null) {\n                temp = temp.left\n            }\n            node.right = removeHelper(node.right, temp._val)\n            node._val = temp._val\n        }\n    }\n    updateHeight(node) // Update node height\n    /* 2. Perform rotation operation to restore balance to this subtree */\n    node = rotate(node)\n    // Return root node of subtree\n    return node\n}\n
      avl_tree.rb
      ### Delete node ###\ndef remove(val)\n  @root = remove_helper(@root, val)\nend\n\n### Recursively delete node (helper method) ###\ndef remove_helper(node, val)\n  return if node.nil?\n  # 1. Find node and delete\n  if val < node.val\n    node.left = remove_helper(node.left, val)\n  elsif val > node.val\n    node.right = remove_helper(node.right, val)\n  else\n    if node.left.nil? || node.right.nil?\n      child = node.left || node.right\n      # Number of child nodes = 0, delete node directly and return\n      return if child.nil?\n      # Number of child nodes = 1, delete node directly\n      node = child\n    else\n      # Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it\n      temp = node.right\n      while !temp.left.nil?\n        temp = temp.left\n      end\n      node.right = remove_helper(node.right, temp.val)\n      node.val = temp.val\n    end\n  end\n  # Update node height\n  update_height(node)\n  # 2. Perform rotation operation to restore balance to this subtree\n  rotate(node)\nend\n
      "},{"location":"chapter_tree/avl_tree/#3-node-search","title":"3. \u00a0 Node Search","text":"

      The node search operation in AVL trees is consistent with that in binary search trees, and will not be elaborated here.

      "},{"location":"chapter_tree/avl_tree/#754-typical-applications-of-avl-trees","title":"7.5.4 \u00a0 Typical Applications of Avl Trees","text":"
      • Organizing and storing large-scale data, suitable for scenarios with high-frequency searches and low-frequency insertions and deletions.
      • Used to build index systems in databases.
      • Red-black trees are also a common type of balanced binary search tree. Compared to AVL trees, red-black trees have more relaxed balance conditions, require fewer rotation operations for node insertion and deletion, and have higher average efficiency for node addition and deletion operations.
      "},{"location":"chapter_tree/binary_search_tree/","title":"7.4 \u00a0 Binary Search Tree","text":"

      As shown in Figure 7-16, a binary search tree satisfies the following conditions.

      1. For the root node, the value of all nodes in the left subtree \\(<\\) the value of the root node \\(<\\) the value of all nodes in the right subtree.
      2. The left and right subtrees of any node are also binary search trees, i.e., they satisfy condition 1. as well.

      Figure 7-16 \u00a0 Binary search tree

      "},{"location":"chapter_tree/binary_search_tree/#741-operations-on-a-binary-search-tree","title":"7.4.1 \u00a0 Operations on a Binary Search Tree","text":"

      We encapsulate the binary search tree as a class BinarySearchTree and declare a member variable root pointing to the tree's root node.

      "},{"location":"chapter_tree/binary_search_tree/#1-searching-for-a-node","title":"1. \u00a0 Searching for a Node","text":"

      Given a target node value num, we can search according to the properties of the binary search tree. As shown in Figure 7-17, we declare a node cur and start from the binary tree's root node root, looping to compare the node value cur.val with num.

      • If cur.val < num, it means the target node is in cur's right subtree, thus execute cur = cur.right.
      • If cur.val > num, it means the target node is in cur's left subtree, thus execute cur = cur.left.
      • If cur.val = num, it means the target node is found, exit the loop, and return the node.
      <1><2><3><4>

      Figure 7-17 \u00a0 Example of searching for a node in a binary search tree

      The search operation in a binary search tree works on the same principle as the binary search algorithm, both eliminating half of the cases in each round. The number of loop iterations is at most the height of the binary tree. When the binary tree is balanced, it uses \\(O(\\log n)\\) time. The example code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_tree.py
      def search(self, num: int) -> TreeNode | None:\n    \"\"\"Search node\"\"\"\n    cur = self._root\n    # Loop search, exit after passing leaf node\n    while cur is not None:\n        # Target node is in cur's right subtree\n        if cur.val < num:\n            cur = cur.right\n        # Target node is in cur's left subtree\n        elif cur.val > num:\n            cur = cur.left\n        # Found target node, exit loop\n        else:\n            break\n    return cur\n
      binary_search_tree.cpp
      /* Search node */\nTreeNode *search(int num) {\n    TreeNode *cur = root;\n    // Loop search, exit after passing leaf node\n    while (cur != nullptr) {\n        // Target node is in cur's right subtree\n        if (cur->val < num)\n            cur = cur->right;\n        // Target node is in cur's left subtree\n        else if (cur->val > num)\n            cur = cur->left;\n        // Found target node, exit loop\n        else\n            break;\n    }\n    // Return target node\n    return cur;\n}\n
      binary_search_tree.java
      /* Search node */\nTreeNode search(int num) {\n    TreeNode cur = root;\n    // Loop search, exit after passing leaf node\n    while (cur != null) {\n        // Target node is in cur's right subtree\n        if (cur.val < num)\n            cur = cur.right;\n        // Target node is in cur's left subtree\n        else if (cur.val > num)\n            cur = cur.left;\n        // Found target node, exit loop\n        else\n            break;\n    }\n    // Return target node\n    return cur;\n}\n
      binary_search_tree.cs
      /* Search node */\nTreeNode? Search(int num) {\n    TreeNode? cur = root;\n    // Loop search, exit after passing leaf node\n    while (cur != null) {\n        // Target node is in cur's right subtree\n        if (cur.val < num) cur =\n            cur.right;\n        // Target node is in cur's left subtree\n        else if (cur.val > num)\n            cur = cur.left;\n        // Found target node, exit loop\n        else\n            break;\n    }\n    // Return target node\n    return cur;\n}\n
      binary_search_tree.go
      /* Search node */\nfunc (bst *binarySearchTree) search(num int) *TreeNode {\n    node := bst.root\n    // Loop search, exit after passing leaf node\n    for node != nil {\n        if node.Val.(int) < num {\n            // Target node is in cur's right subtree\n            node = node.Right\n        } else if node.Val.(int) > num {\n            // Target node is in cur's left subtree\n            node = node.Left\n        } else {\n            // Found target node, exit loop\n            break\n        }\n    }\n    // Return target node\n    return node\n}\n
      binary_search_tree.swift
      /* Search node */\nfunc search(num: Int) -> TreeNode? {\n    var cur = root\n    // Loop search, exit after passing leaf node\n    while cur != nil {\n        // Target node is in cur's right subtree\n        if cur!.val < num {\n            cur = cur?.right\n        }\n        // Target node is in cur's left subtree\n        else if cur!.val > num {\n            cur = cur?.left\n        }\n        // Found target node, exit loop\n        else {\n            break\n        }\n    }\n    // Return target node\n    return cur\n}\n
      binary_search_tree.js
      /* Search node */\nsearch(num) {\n    let cur = this.root;\n    // Loop search, exit after passing leaf node\n    while (cur !== null) {\n        // Target node is in cur's right subtree\n        if (cur.val < num) cur = cur.right;\n        // Target node is in cur's left subtree\n        else if (cur.val > num) cur = cur.left;\n        // Found target node, exit loop\n        else break;\n    }\n    // Return target node\n    return cur;\n}\n
      binary_search_tree.ts
      /* Search node */\nsearch(num: number): TreeNode | null {\n    let cur = this.root;\n    // Loop search, exit after passing leaf node\n    while (cur !== null) {\n        // Target node is in cur's right subtree\n        if (cur.val < num) cur = cur.right;\n        // Target node is in cur's left subtree\n        else if (cur.val > num) cur = cur.left;\n        // Found target node, exit loop\n        else break;\n    }\n    // Return target node\n    return cur;\n}\n
      binary_search_tree.dart
      /* Search node */\nTreeNode? search(int _num) {\n  TreeNode? cur = _root;\n  // Loop search, exit after passing leaf node\n  while (cur != null) {\n    // Target node is in cur's right subtree\n    if (cur.val < _num)\n      cur = cur.right;\n    // Target node is in cur's left subtree\n    else if (cur.val > _num)\n      cur = cur.left;\n    // Found target node, exit loop\n    else\n      break;\n  }\n  // Return target node\n  return cur;\n}\n
      binary_search_tree.rs
      /* Search node */\npub fn search(&self, num: i32) -> OptionTreeNodeRc {\n    let mut cur = self.root.clone();\n    // Loop search, exit after passing leaf node\n    while let Some(node) = cur.clone() {\n        match num.cmp(&node.borrow().val) {\n            // Target node is in cur's right subtree\n            Ordering::Greater => cur = node.borrow().right.clone(),\n            // Target node is in cur's left subtree\n            Ordering::Less => cur = node.borrow().left.clone(),\n            // Found target node, exit loop\n            Ordering::Equal => break,\n        }\n    }\n\n    // Return target node\n    cur\n}\n
      binary_search_tree.c
      /* Search node */\nTreeNode *search(BinarySearchTree *bst, int num) {\n    TreeNode *cur = bst->root;\n    // Loop search, exit after passing leaf node\n    while (cur != NULL) {\n        if (cur->val < num) {\n            // Target node is in cur's right subtree\n            cur = cur->right;\n        } else if (cur->val > num) {\n            // Target node is in cur's left subtree\n            cur = cur->left;\n        } else {\n            // Found target node, exit loop\n            break;\n        }\n    }\n    // Return target node\n    return cur;\n}\n
      binary_search_tree.kt
      /* Search node */\nfun search(num: Int): TreeNode? {\n    var cur = root\n    // Loop search, exit after passing leaf node\n    while (cur != null) {\n        // Target node is in cur's right subtree\n        cur = if (cur._val < num)\n            cur.right\n        // Target node is in cur's left subtree\n        else if (cur._val > num)\n            cur.left\n        // Found target node, exit loop\n        else\n            break\n    }\n    // Return target node\n    return cur\n}\n
      binary_search_tree.rb
      ### Search node ###\ndef search(num)\n  cur = @root\n\n  # Loop search, exit after passing leaf node\n  while !cur.nil?\n    # Target node is in cur's right subtree\n    if cur.val < num\n      cur = cur.right\n    # Target node is in cur's left subtree\n    elsif cur.val > num\n      cur = cur.left\n    # Found target node, exit loop\n    else\n      break\n    end\n  end\n\n  cur\nend\n
      "},{"location":"chapter_tree/binary_search_tree/#2-inserting-a-node","title":"2. \u00a0 Inserting a Node","text":"

      Given an element num to be inserted, in order to maintain the property of the binary search tree \"left subtree < root node < right subtree,\" the insertion process is as shown in Figure 7-18.

      1. Finding the insertion position: Similar to the search operation, start from the root node and loop downward searching according to the size relationship between the current node value and num, until passing the leaf node (traversing to None) and then exit the loop.
      2. Insert the node at that position: Initialize node num and place it at the None position.

      Figure 7-18 \u00a0 Inserting a node into a binary search tree

      In the code implementation, note the following two points:

      • Binary search trees do not allow duplicate nodes; otherwise, it would violate its definition. Therefore, if the node to be inserted already exists in the tree, the insertion is not performed and it returns directly.
      • To implement the node insertion, we need to use node pre to save the node from the previous loop iteration. This way, when traversing to None, we can obtain its parent node, thereby completing the node insertion operation.
      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_tree.py
      def insert(self, num: int):\n    \"\"\"Insert node\"\"\"\n    # If tree is empty, initialize root node\n    if self._root is None:\n        self._root = TreeNode(num)\n        return\n    # Loop search, exit after passing leaf node\n    cur, pre = self._root, None\n    while cur is not None:\n        # Found duplicate node, return directly\n        if cur.val == num:\n            return\n        pre = cur\n        # Insertion position is in cur's right subtree\n        if cur.val < num:\n            cur = cur.right\n        # Insertion position is in cur's left subtree\n        else:\n            cur = cur.left\n    # Insert node\n    node = TreeNode(num)\n    if pre.val < num:\n        pre.right = node\n    else:\n        pre.left = node\n
      binary_search_tree.cpp
      /* Insert node */\nvoid insert(int num) {\n    // If tree is empty, initialize root node\n    if (root == nullptr) {\n        root = new TreeNode(num);\n        return;\n    }\n    TreeNode *cur = root, *pre = nullptr;\n    // Loop search, exit after passing leaf node\n    while (cur != nullptr) {\n        // Found duplicate node, return directly\n        if (cur->val == num)\n            return;\n        pre = cur;\n        // Insertion position is in cur's right subtree\n        if (cur->val < num)\n            cur = cur->right;\n        // Insertion position is in cur's left subtree\n        else\n            cur = cur->left;\n    }\n    // Insert node\n    TreeNode *node = new TreeNode(num);\n    if (pre->val < num)\n        pre->right = node;\n    else\n        pre->left = node;\n}\n
      binary_search_tree.java
      /* Insert node */\nvoid insert(int num) {\n    // If tree is empty, initialize root node\n    if (root == null) {\n        root = new TreeNode(num);\n        return;\n    }\n    TreeNode cur = root, pre = null;\n    // Loop search, exit after passing leaf node\n    while (cur != null) {\n        // Found duplicate node, return directly\n        if (cur.val == num)\n            return;\n        pre = cur;\n        // Insertion position is in cur's right subtree\n        if (cur.val < num)\n            cur = cur.right;\n        // Insertion position is in cur's left subtree\n        else\n            cur = cur.left;\n    }\n    // Insert node\n    TreeNode node = new TreeNode(num);\n    if (pre.val < num)\n        pre.right = node;\n    else\n        pre.left = node;\n}\n
      binary_search_tree.cs
      /* Insert node */\nvoid Insert(int num) {\n    // If tree is empty, initialize root node\n    if (root == null) {\n        root = new TreeNode(num);\n        return;\n    }\n    TreeNode? cur = root, pre = null;\n    // Loop search, exit after passing leaf node\n    while (cur != null) {\n        // Found duplicate node, return directly\n        if (cur.val == num)\n            return;\n        pre = cur;\n        // Insertion position is in cur's right subtree\n        if (cur.val < num)\n            cur = cur.right;\n        // Insertion position is in cur's left subtree\n        else\n            cur = cur.left;\n    }\n\n    // Insert node\n    TreeNode node = new(num);\n    if (pre != null) {\n        if (pre.val < num)\n            pre.right = node;\n        else\n            pre.left = node;\n    }\n}\n
      binary_search_tree.go
      /* Insert node */\nfunc (bst *binarySearchTree) insert(num int) {\n    cur := bst.root\n    // If tree is empty, initialize root node\n    if cur == nil {\n        bst.root = NewTreeNode(num)\n        return\n    }\n    // Node position before the node to be inserted\n    var pre *TreeNode = nil\n    // Loop search, exit after passing leaf node\n    for cur != nil {\n        if cur.Val == num {\n            return\n        }\n        pre = cur\n        if cur.Val.(int) < num {\n            cur = cur.Right\n        } else {\n            cur = cur.Left\n        }\n    }\n    // Insert node\n    node := NewTreeNode(num)\n    if pre.Val.(int) < num {\n        pre.Right = node\n    } else {\n        pre.Left = node\n    }\n}\n
      binary_search_tree.swift
      /* Insert node */\nfunc insert(num: Int) {\n    // If tree is empty, initialize root node\n    if root == nil {\n        root = TreeNode(x: num)\n        return\n    }\n    var cur = root\n    var pre: TreeNode?\n    // Loop search, exit after passing leaf node\n    while cur != nil {\n        // Found duplicate node, return directly\n        if cur!.val == num {\n            return\n        }\n        pre = cur\n        // Insertion position is in cur's right subtree\n        if cur!.val < num {\n            cur = cur?.right\n        }\n        // Insertion position is in cur's left subtree\n        else {\n            cur = cur?.left\n        }\n    }\n    // Insert node\n    let node = TreeNode(x: num)\n    if pre!.val < num {\n        pre?.right = node\n    } else {\n        pre?.left = node\n    }\n}\n
      binary_search_tree.js
      /* Insert node */\ninsert(num) {\n    // If tree is empty, initialize root node\n    if (this.root === null) {\n        this.root = new TreeNode(num);\n        return;\n    }\n    let cur = this.root,\n        pre = null;\n    // Loop search, exit after passing leaf node\n    while (cur !== null) {\n        // Found duplicate node, return directly\n        if (cur.val === num) return;\n        pre = cur;\n        // Insertion position is in cur's right subtree\n        if (cur.val < num) cur = cur.right;\n        // Insertion position is in cur's left subtree\n        else cur = cur.left;\n    }\n    // Insert node\n    const node = new TreeNode(num);\n    if (pre.val < num) pre.right = node;\n    else pre.left = node;\n}\n
      binary_search_tree.ts
      /* Insert node */\ninsert(num: number): void {\n    // If tree is empty, initialize root node\n    if (this.root === null) {\n        this.root = new TreeNode(num);\n        return;\n    }\n    let cur: TreeNode | null = this.root,\n        pre: TreeNode | null = null;\n    // Loop search, exit after passing leaf node\n    while (cur !== null) {\n        // Found duplicate node, return directly\n        if (cur.val === num) return;\n        pre = cur;\n        // Insertion position is in cur's right subtree\n        if (cur.val < num) cur = cur.right;\n        // Insertion position is in cur's left subtree\n        else cur = cur.left;\n    }\n    // Insert node\n    const node = new TreeNode(num);\n    if (pre!.val < num) pre!.right = node;\n    else pre!.left = node;\n}\n
      binary_search_tree.dart
      /* Insert node */\nvoid insert(int _num) {\n  // If tree is empty, initialize root node\n  if (_root == null) {\n    _root = TreeNode(_num);\n    return;\n  }\n  TreeNode? cur = _root;\n  TreeNode? pre = null;\n  // Loop search, exit after passing leaf node\n  while (cur != null) {\n    // Found duplicate node, return directly\n    if (cur.val == _num) return;\n    pre = cur;\n    // Insertion position is in cur's right subtree\n    if (cur.val < _num)\n      cur = cur.right;\n    // Insertion position is in cur's left subtree\n    else\n      cur = cur.left;\n  }\n  // Insert node\n  TreeNode? node = TreeNode(_num);\n  if (pre!.val < _num)\n    pre.right = node;\n  else\n    pre.left = node;\n}\n
      binary_search_tree.rs
      /* Insert node */\npub fn insert(&mut self, num: i32) {\n    // If tree is empty, initialize root node\n    if self.root.is_none() {\n        self.root = Some(TreeNode::new(num));\n        return;\n    }\n    let mut cur = self.root.clone();\n    let mut pre = None;\n    // Loop search, exit after passing leaf node\n    while let Some(node) = cur.clone() {\n        match num.cmp(&node.borrow().val) {\n            // Found duplicate node, return directly\n            Ordering::Equal => return,\n            // Insertion position is in cur's right subtree\n            Ordering::Greater => {\n                pre = cur.clone();\n                cur = node.borrow().right.clone();\n            }\n            // Insertion position is in cur's left subtree\n            Ordering::Less => {\n                pre = cur.clone();\n                cur = node.borrow().left.clone();\n            }\n        }\n    }\n    // Insert node\n    let pre = pre.unwrap();\n    let node = Some(TreeNode::new(num));\n    if num > pre.borrow().val {\n        pre.borrow_mut().right = node;\n    } else {\n        pre.borrow_mut().left = node;\n    }\n}\n
      binary_search_tree.c
      /* Insert node */\nvoid insert(BinarySearchTree *bst, int num) {\n    // If tree is empty, initialize root node\n    if (bst->root == NULL) {\n        bst->root = newTreeNode(num);\n        return;\n    }\n    TreeNode *cur = bst->root, *pre = NULL;\n    // Loop search, exit after passing leaf node\n    while (cur != NULL) {\n        // Found duplicate node, return directly\n        if (cur->val == num) {\n            return;\n        }\n        pre = cur;\n        if (cur->val < num) {\n            // Insertion position is in cur's right subtree\n            cur = cur->right;\n        } else {\n            // Insertion position is in cur's left subtree\n            cur = cur->left;\n        }\n    }\n    // Insert node\n    TreeNode *node = newTreeNode(num);\n    if (pre->val < num) {\n        pre->right = node;\n    } else {\n        pre->left = node;\n    }\n}\n
      binary_search_tree.kt
      /* Insert node */\nfun insert(num: Int) {\n    // If tree is empty, initialize root node\n    if (root == null) {\n        root = TreeNode(num)\n        return\n    }\n    var cur = root\n    var pre: TreeNode? = null\n    // Loop search, exit after passing leaf node\n    while (cur != null) {\n        // Found duplicate node, return directly\n        if (cur._val == num)\n            return\n        pre = cur\n        // Insertion position is in cur's right subtree\n        cur = if (cur._val < num)\n            cur.right\n        // Insertion position is in cur's left subtree\n        else\n            cur.left\n    }\n    // Insert node\n    val node = TreeNode(num)\n    if (pre?._val!! < num)\n        pre.right = node\n    else\n        pre.left = node\n}\n
      binary_search_tree.rb
      ### Insert node ###\ndef insert(num)\n  # If tree is empty, initialize root node\n  if @root.nil?\n    @root = TreeNode.new(num)\n    return\n  end\n\n  # Loop search, exit after passing leaf node\n  cur, pre = @root, nil\n  while !cur.nil?\n    # Found duplicate node, return directly\n    return if cur.val == num\n\n    pre = cur\n    # Insertion position is in cur's right subtree\n    if cur.val < num\n      cur = cur.right\n    # Insertion position is in cur's left subtree\n    else\n      cur = cur.left\n    end\n  end\n\n  # Insert node\n  node = TreeNode.new(num)\n  if pre.val < num\n    pre.right = node\n  else\n    pre.left = node\n  end\nend\n

      Similar to searching for a node, inserting a node uses \\(O(\\log n)\\) time.

      "},{"location":"chapter_tree/binary_search_tree/#3-removing-a-node","title":"3. \u00a0 Removing a Node","text":"

      First, find the target node in the binary tree, then remove it. Similar to node insertion, we need to ensure that after the removal operation is completed, the binary search tree's property of \"left subtree \\(<\\) root node \\(<\\) right subtree\" is still maintained. Therefore, depending on the number of child nodes the target node has, we divide it into 0, 1, and 2 three cases, and execute the corresponding node removal operations.

      As shown in Figure 7-19, when the degree of the node to be removed is \\(0\\), it means the node is a leaf node and can be directly removed.

      Figure 7-19 \u00a0 Removing a node in a binary search tree (degree 0)

      As shown in Figure 7-20, when the degree of the node to be removed is \\(1\\), replacing the node to be removed with its child node is sufficient.

      Figure 7-20 \u00a0 Removing a node in a binary search tree (degree 1)

      When the degree of the node to be removed is \\(2\\), we cannot directly remove it; instead, we need to use a node to replace it. To maintain the binary search tree's property of \"left subtree \\(<\\) root node \\(<\\) right subtree,\" this node can be either the smallest node in the right subtree or the largest node in the left subtree.

      Assuming we choose the smallest node in the right subtree (the next node in the inorder traversal), the removal process is as shown in Figure 7-21.

      1. Find the next node of the node to be removed in the \"inorder traversal sequence,\" denoted as tmp.
      2. Replace the value of the node to be removed with the value of tmp, and recursively remove node tmp in the tree.
      <1><2><3><4>

      Figure 7-21 \u00a0 Removing a node in a binary search tree (degree 2)

      The node removal operation also uses \\(O(\\log n)\\) time, where finding the node to be removed requires \\(O(\\log n)\\) time, and obtaining the inorder successor node requires \\(O(\\log n)\\) time. Example code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_tree.py
      def remove(self, num: int):\n    \"\"\"Delete node\"\"\"\n    # If tree is empty, return directly\n    if self._root is None:\n        return\n    # Loop search, exit after passing leaf node\n    cur, pre = self._root, None\n    while cur is not None:\n        # Found node to delete, exit loop\n        if cur.val == num:\n            break\n        pre = cur\n        # Node to delete is in cur's right subtree\n        if cur.val < num:\n            cur = cur.right\n        # Node to delete is in cur's left subtree\n        else:\n            cur = cur.left\n    # If no node to delete, return directly\n    if cur is None:\n        return\n\n    # Number of child nodes = 0 or 1\n    if cur.left is None or cur.right is None:\n        # When number of child nodes = 0 / 1, child = null / that child node\n        child = cur.left or cur.right\n        # Delete node cur\n        if cur != self._root:\n            if pre.left == cur:\n                pre.left = child\n            else:\n                pre.right = child\n        else:\n            # If deleted node is root node, reassign root node\n            self._root = child\n    # Number of child nodes = 2\n    else:\n        # Get next node of cur in inorder traversal\n        tmp: TreeNode = cur.right\n        while tmp.left is not None:\n            tmp = tmp.left\n        # Recursively delete node tmp\n        self.remove(tmp.val)\n        # Replace cur with tmp\n        cur.val = tmp.val\n
      binary_search_tree.cpp
      /* Remove node */\nvoid remove(int num) {\n    // If tree is empty, return directly\n    if (root == nullptr)\n        return;\n    TreeNode *cur = root, *pre = nullptr;\n    // Loop search, exit after passing leaf node\n    while (cur != nullptr) {\n        // Found node to delete, exit loop\n        if (cur->val == num)\n            break;\n        pre = cur;\n        // Node to delete is in cur's right subtree\n        if (cur->val < num)\n            cur = cur->right;\n        // Node to delete is in cur's left subtree\n        else\n            cur = cur->left;\n    }\n    // If no node to delete, return directly\n    if (cur == nullptr)\n        return;\n    // Number of child nodes = 0 or 1\n    if (cur->left == nullptr || cur->right == nullptr) {\n        // When number of child nodes = 0 / 1, child = nullptr / that child node\n        TreeNode *child = cur->left != nullptr ? cur->left : cur->right;\n        // Delete node cur\n        if (cur != root) {\n            if (pre->left == cur)\n                pre->left = child;\n            else\n                pre->right = child;\n        } else {\n            // If deleted node is root node, reassign root node\n            root = child;\n        }\n        // Free memory\n        delete cur;\n    }\n    // Number of child nodes = 2\n    else {\n        // Get next node of cur in inorder traversal\n        TreeNode *tmp = cur->right;\n        while (tmp->left != nullptr) {\n            tmp = tmp->left;\n        }\n        int tmpVal = tmp->val;\n        // Recursively delete node tmp\n        remove(tmp->val);\n        // Replace cur with tmp\n        cur->val = tmpVal;\n    }\n}\n
      binary_search_tree.java
      /* Remove node */\nvoid remove(int num) {\n    // If tree is empty, return directly\n    if (root == null)\n        return;\n    TreeNode cur = root, pre = null;\n    // Loop search, exit after passing leaf node\n    while (cur != null) {\n        // Found node to delete, exit loop\n        if (cur.val == num)\n            break;\n        pre = cur;\n        // Node to delete is in cur's right subtree\n        if (cur.val < num)\n            cur = cur.right;\n        // Node to delete is in cur's left subtree\n        else\n            cur = cur.left;\n    }\n    // If no node to delete, return directly\n    if (cur == null)\n        return;\n    // Number of child nodes = 0 or 1\n    if (cur.left == null || cur.right == null) {\n        // When number of child nodes = 0 / 1, child = null / that child node\n        TreeNode child = cur.left != null ? cur.left : cur.right;\n        // Delete node cur\n        if (cur != root) {\n            if (pre.left == cur)\n                pre.left = child;\n            else\n                pre.right = child;\n        } else {\n            // If deleted node is root node, reassign root node\n            root = child;\n        }\n    }\n    // Number of child nodes = 2\n    else {\n        // Get next node of cur in inorder traversal\n        TreeNode tmp = cur.right;\n        while (tmp.left != null) {\n            tmp = tmp.left;\n        }\n        // Recursively delete node tmp\n        remove(tmp.val);\n        // Replace cur with tmp\n        cur.val = tmp.val;\n    }\n}\n
      binary_search_tree.cs
      /* Remove node */\nvoid Remove(int num) {\n    // If tree is empty, return directly\n    if (root == null)\n        return;\n    TreeNode? cur = root, pre = null;\n    // Loop search, exit after passing leaf node\n    while (cur != null) {\n        // Found node to delete, exit loop\n        if (cur.val == num)\n            break;\n        pre = cur;\n        // Node to delete is in cur's right subtree\n        if (cur.val < num)\n            cur = cur.right;\n        // Node to delete is in cur's left subtree\n        else\n            cur = cur.left;\n    }\n    // If no node to delete, return directly\n    if (cur == null)\n        return;\n    // Number of child nodes = 0 or 1\n    if (cur.left == null || cur.right == null) {\n        // When number of child nodes = 0 / 1, child = null / that child node\n        TreeNode? child = cur.left ?? cur.right;\n        // Delete node cur\n        if (cur != root) {\n            if (pre!.left == cur)\n                pre.left = child;\n            else\n                pre.right = child;\n        } else {\n            // If deleted node is root node, reassign root node\n            root = child;\n        }\n    }\n    // Number of child nodes = 2\n    else {\n        // Get next node of cur in inorder traversal\n        TreeNode? tmp = cur.right;\n        while (tmp.left != null) {\n            tmp = tmp.left;\n        }\n        // Recursively delete node tmp\n        Remove(tmp.val!.Value);\n        // Replace cur with tmp\n        cur.val = tmp.val;\n    }\n}\n
      binary_search_tree.go
      /* Remove node */\nfunc (bst *binarySearchTree) remove(num int) {\n    cur := bst.root\n    // If tree is empty, return directly\n    if cur == nil {\n        return\n    }\n    // Node position before the node to be removed\n    var pre *TreeNode = nil\n    // Loop search, exit after passing leaf node\n    for cur != nil {\n        if cur.Val == num {\n            break\n        }\n        pre = cur\n        if cur.Val.(int) < num {\n            // Node to be removed is in right subtree\n            cur = cur.Right\n        } else {\n            // Node to be removed is in left subtree\n            cur = cur.Left\n        }\n    }\n    // If no node to delete, return directly\n    if cur == nil {\n        return\n    }\n    // Number of child nodes is 0 or 1\n    if cur.Left == nil || cur.Right == nil {\n        var child *TreeNode = nil\n        // Get child node of node to be removed\n        if cur.Left != nil {\n            child = cur.Left\n        } else {\n            child = cur.Right\n        }\n        // Delete node cur\n        if cur != bst.root {\n            if pre.Left == cur {\n                pre.Left = child\n            } else {\n                pre.Right = child\n            }\n        } else {\n            // If deleted node is root node, reassign root node\n            bst.root = child\n        }\n        // Number of child nodes is 2\n    } else {\n        // Get next node of node cur to be removed in in-order traversal\n        tmp := cur.Right\n        for tmp.Left != nil {\n            tmp = tmp.Left\n        }\n        // Recursively delete node tmp\n        bst.remove(tmp.Val.(int))\n        // Replace cur with tmp\n        cur.Val = tmp.Val\n    }\n}\n
      binary_search_tree.swift
      /* Remove node */\nfunc remove(num: Int) {\n    // If tree is empty, return directly\n    if root == nil {\n        return\n    }\n    var cur = root\n    var pre: TreeNode?\n    // Loop search, exit after passing leaf node\n    while cur != nil {\n        // Found node to delete, exit loop\n        if cur!.val == num {\n            break\n        }\n        pre = cur\n        // Node to delete is in cur's right subtree\n        if cur!.val < num {\n            cur = cur?.right\n        }\n        // Node to delete is in cur's left subtree\n        else {\n            cur = cur?.left\n        }\n    }\n    // If no node to delete, return directly\n    if cur == nil {\n        return\n    }\n    // Number of child nodes = 0 or 1\n    if cur?.left == nil || cur?.right == nil {\n        // When number of child nodes = 0 / 1, child = null / that child node\n        let child = cur?.left ?? cur?.right\n        // Delete node cur\n        if cur !== root {\n            if pre?.left === cur {\n                pre?.left = child\n            } else {\n                pre?.right = child\n            }\n        } else {\n            // If deleted node is root node, reassign root node\n            root = child\n        }\n    }\n    // Number of child nodes = 2\n    else {\n        // Get next node of cur in inorder traversal\n        var tmp = cur?.right\n        while tmp?.left != nil {\n            tmp = tmp?.left\n        }\n        // Recursively delete node tmp\n        remove(num: tmp!.val)\n        // Replace cur with tmp\n        cur?.val = tmp!.val\n    }\n}\n
      binary_search_tree.js
      /* Remove node */\nremove(num) {\n    // If tree is empty, return directly\n    if (this.root === null) return;\n    let cur = this.root,\n        pre = null;\n    // Loop search, exit after passing leaf node\n    while (cur !== null) {\n        // Found node to delete, exit loop\n        if (cur.val === num) break;\n        pre = cur;\n        // Node to delete is in cur's right subtree\n        if (cur.val < num) cur = cur.right;\n        // Node to delete is in cur's left subtree\n        else cur = cur.left;\n    }\n    // If no node to delete, return directly\n    if (cur === null) return;\n    // Number of child nodes = 0 or 1\n    if (cur.left === null || cur.right === null) {\n        // When number of child nodes = 0 / 1, child = null / that child node\n        const child = cur.left !== null ? cur.left : cur.right;\n        // Delete node cur\n        if (cur !== this.root) {\n            if (pre.left === cur) pre.left = child;\n            else pre.right = child;\n        } else {\n            // If deleted node is root node, reassign root node\n            this.root = child;\n        }\n    }\n    // Number of child nodes = 2\n    else {\n        // Get next node of cur in inorder traversal\n        let tmp = cur.right;\n        while (tmp.left !== null) {\n            tmp = tmp.left;\n        }\n        // Recursively delete node tmp\n        this.remove(tmp.val);\n        // Replace cur with tmp\n        cur.val = tmp.val;\n    }\n}\n
      binary_search_tree.ts
      /* Remove node */\nremove(num: number): void {\n    // If tree is empty, return directly\n    if (this.root === null) return;\n    let cur: TreeNode | null = this.root,\n        pre: TreeNode | null = null;\n    // Loop search, exit after passing leaf node\n    while (cur !== null) {\n        // Found node to delete, exit loop\n        if (cur.val === num) break;\n        pre = cur;\n        // Node to delete is in cur's right subtree\n        if (cur.val < num) cur = cur.right;\n        // Node to delete is in cur's left subtree\n        else cur = cur.left;\n    }\n    // If no node to delete, return directly\n    if (cur === null) return;\n    // Number of child nodes = 0 or 1\n    if (cur.left === null || cur.right === null) {\n        // When number of child nodes = 0 / 1, child = null / that child node\n        const child: TreeNode | null =\n            cur.left !== null ? cur.left : cur.right;\n        // Delete node cur\n        if (cur !== this.root) {\n            if (pre!.left === cur) pre!.left = child;\n            else pre!.right = child;\n        } else {\n            // If deleted node is root node, reassign root node\n            this.root = child;\n        }\n    }\n    // Number of child nodes = 2\n    else {\n        // Get next node of cur in inorder traversal\n        let tmp: TreeNode | null = cur.right;\n        while (tmp!.left !== null) {\n            tmp = tmp!.left;\n        }\n        // Recursively delete node tmp\n        this.remove(tmp!.val);\n        // Replace cur with tmp\n        cur.val = tmp!.val;\n    }\n}\n
      binary_search_tree.dart
      /* Remove node */\nvoid remove(int _num) {\n  // If tree is empty, return directly\n  if (_root == null) return;\n  TreeNode? cur = _root;\n  TreeNode? pre = null;\n  // Loop search, exit after passing leaf node\n  while (cur != null) {\n    // Found node to delete, exit loop\n    if (cur.val == _num) break;\n    pre = cur;\n    // Node to delete is in cur's right subtree\n    if (cur.val < _num)\n      cur = cur.right;\n    // Node to delete is in cur's left subtree\n    else\n      cur = cur.left;\n  }\n  // If no node to delete, return directly\n  if (cur == null) return;\n  // Number of child nodes = 0 or 1\n  if (cur.left == null || cur.right == null) {\n    // When number of child nodes = 0 / 1, child = null / that child node\n    TreeNode? child = cur.left ?? cur.right;\n    // Delete node cur\n    if (cur != _root) {\n      if (pre!.left == cur)\n        pre.left = child;\n      else\n        pre.right = child;\n    } else {\n      // If deleted node is root node, reassign root node\n      _root = child;\n    }\n  } else {\n    // Number of child nodes = 2\n    // Get next node of cur in inorder traversal\n    TreeNode? tmp = cur.right;\n    while (tmp!.left != null) {\n      tmp = tmp.left;\n    }\n    // Recursively delete node tmp\n    remove(tmp.val);\n    // Replace cur with tmp\n    cur.val = tmp.val;\n  }\n}\n
      binary_search_tree.rs
      /* Remove node */\npub fn remove(&mut self, num: i32) {\n    // If tree is empty, return directly\n    if self.root.is_none() {\n        return;\n    }\n    let mut cur = self.root.clone();\n    let mut pre = None;\n    // Loop search, exit after passing leaf node\n    while let Some(node) = cur.clone() {\n        match num.cmp(&node.borrow().val) {\n            // Found node to delete, exit loop\n            Ordering::Equal => break,\n            // Node to delete is in cur's right subtree\n            Ordering::Greater => {\n                pre = cur.clone();\n                cur = node.borrow().right.clone();\n            }\n            // Node to delete is in cur's left subtree\n            Ordering::Less => {\n                pre = cur.clone();\n                cur = node.borrow().left.clone();\n            }\n        }\n    }\n    // If no node to delete, return directly\n    if cur.is_none() {\n        return;\n    }\n    let cur = cur.unwrap();\n    let (left_child, right_child) = (cur.borrow().left.clone(), cur.borrow().right.clone());\n    match (left_child.clone(), right_child.clone()) {\n        // Number of child nodes = 0 or 1\n        (None, None) | (Some(_), None) | (None, Some(_)) => {\n            // When number of child nodes = 0 / 1, child = nullptr / that child node\n            let child = left_child.or(right_child);\n            let pre = pre.unwrap();\n            // Delete node cur\n            if !Rc::ptr_eq(&cur, self.root.as_ref().unwrap()) {\n                let left = pre.borrow().left.clone();\n                if left.is_some() && Rc::ptr_eq(left.as_ref().unwrap(), &cur) {\n                    pre.borrow_mut().left = child;\n                } else {\n                    pre.borrow_mut().right = child;\n                }\n            } else {\n                // If deleted node is root node, reassign root node\n                self.root = child;\n            }\n        }\n        // Number of child nodes = 2\n        (Some(_), Some(_)) => {\n            // Get next node of cur in inorder traversal\n            let mut tmp = cur.borrow().right.clone();\n            while let Some(node) = tmp.clone() {\n                if node.borrow().left.is_some() {\n                    tmp = node.borrow().left.clone();\n                } else {\n                    break;\n                }\n            }\n            let tmp_val = tmp.unwrap().borrow().val;\n            // Recursively delete node tmp\n            self.remove(tmp_val);\n            // Replace cur with tmp\n            cur.borrow_mut().val = tmp_val;\n        }\n    }\n}\n
      binary_search_tree.c
      /* Remove node */\n// Cannot use remove keyword here due to stdio.h inclusion\nvoid removeItem(BinarySearchTree *bst, int num) {\n    // If tree is empty, return directly\n    if (bst->root == NULL)\n        return;\n    TreeNode *cur = bst->root, *pre = NULL;\n    // Loop search, exit after passing leaf node\n    while (cur != NULL) {\n        // Found node to delete, exit loop\n        if (cur->val == num)\n            break;\n        pre = cur;\n        if (cur->val < num) {\n            // Node to delete is in right subtree of root\n            cur = cur->right;\n        } else {\n            // Node to delete is in left subtree of root\n            cur = cur->left;\n        }\n    }\n    // If no node to delete, return directly\n    if (cur == NULL)\n        return;\n    // Check if node to delete has children\n    if (cur->left == NULL || cur->right == NULL) {\n        /* Number of child nodes = 0 or 1 */\n        // When number of child nodes = 0 / 1, child = nullptr / that child node\n        TreeNode *child = cur->left != NULL ? cur->left : cur->right;\n        // Delete node cur\n        if (pre->left == cur) {\n            pre->left = child;\n        } else {\n            pre->right = child;\n        }\n        // Free memory\n        free(cur);\n    } else {\n        /* Number of child nodes = 2 */\n        // Get next node of cur in inorder traversal\n        TreeNode *tmp = cur->right;\n        while (tmp->left != NULL) {\n            tmp = tmp->left;\n        }\n        int tmpVal = tmp->val;\n        // Recursively delete node tmp\n        removeItem(bst, tmp->val);\n        // Replace cur with tmp\n        cur->val = tmpVal;\n    }\n}\n
      binary_search_tree.kt
      /* Remove node */\nfun remove(num: Int) {\n    // If tree is empty, return directly\n    if (root == null)\n        return\n    var cur = root\n    var pre: TreeNode? = null\n    // Loop search, exit after passing leaf node\n    while (cur != null) {\n        // Found node to delete, exit loop\n        if (cur._val == num)\n            break\n        pre = cur\n        // Node to delete is in cur's right subtree\n        cur = if (cur._val < num)\n            cur.right\n        // Node to delete is in cur's left subtree\n        else\n            cur.left\n    }\n    // If no node to delete, return directly\n    if (cur == null)\n        return\n    // Number of child nodes = 0 or 1\n    if (cur.left == null || cur.right == null) {\n        // When number of child nodes = 0 / 1, child = null / that child node\n        val child = if (cur.left != null)\n            cur.left\n        else\n            cur.right\n        // Delete node cur\n        if (cur != root) {\n            if (pre!!.left == cur)\n                pre.left = child\n            else\n                pre.right = child\n        } else {\n            // If deleted node is root node, reassign root node\n            root = child\n        }\n        // Number of child nodes = 2\n    } else {\n        // Get next node of cur in inorder traversal\n        var tmp = cur.right\n        while (tmp!!.left != null) {\n            tmp = tmp.left\n        }\n        // Recursively delete node tmp\n        remove(tmp._val)\n        // Replace cur with tmp\n        cur._val = tmp._val\n    }\n}\n
      binary_search_tree.rb
      ### Delete node ###\ndef remove(num)\n  # If tree is empty, return directly\n  return if @root.nil?\n\n  # Loop search, exit after passing leaf node\n  cur, pre = @root, nil\n  while !cur.nil?\n    # Found node to delete, exit loop\n    break if cur.val == num\n\n    pre = cur\n    # Node to delete is in cur's right subtree\n    if cur.val < num\n      cur = cur.right\n    # Node to delete is in cur's left subtree\n    else\n      cur = cur.left\n    end\n  end\n  # If no node to delete, return directly\n  return if cur.nil?\n\n  # Number of child nodes = 0 or 1\n  if cur.left.nil? || cur.right.nil?\n    # When number of child nodes = 0 / 1, child = null / that child node\n    child = cur.left || cur.right\n    # Delete node cur\n    if cur != @root\n      if pre.left == cur\n        pre.left = child\n      else\n        pre.right = child\n      end\n    else\n      # If deleted node is root node, reassign root node\n      @root = child\n    end\n  # Number of child nodes = 2\n  else\n    # Get next node of cur in inorder traversal\n    tmp = cur.right\n    while !tmp.left.nil?\n      tmp = tmp.left\n    end\n    # Recursively delete node tmp\n    remove(tmp.val)\n    # Replace cur with tmp\n    cur.val = tmp.val\n  end\nend\n
      "},{"location":"chapter_tree/binary_search_tree/#4-inorder-traversal-is-ordered","title":"4. \u00a0 Inorder Traversal Is Ordered","text":"

      As shown in Figure 7-22, the inorder traversal of a binary tree follows the \"left \\(\\rightarrow\\) root \\(\\rightarrow\\) right\" traversal order, while the binary search tree satisfies the \"left child node \\(<\\) root node \\(<\\) right child node\" size relationship.

      This means that when performing an inorder traversal in a binary search tree, the next smallest node is always traversed first, thus yielding an important property: The inorder traversal sequence of a binary search tree is ascending.

      Using the property of inorder traversal being ascending, we can obtain ordered data in a binary search tree in only \\(O(n)\\) time, without the need for additional sorting operations, which is very efficient.

      Figure 7-22 \u00a0 Inorder traversal sequence of a binary search tree

      "},{"location":"chapter_tree/binary_search_tree/#742-efficiency-of-binary-search-trees","title":"7.4.2 \u00a0 Efficiency of Binary Search Trees","text":"

      Given a set of data, we consider using an array or a binary search tree for storage. Observing Table 7-2, all operations in a binary search tree have logarithmic time complexity, providing stable and efficient performance. Arrays are more efficient than binary search trees only in scenarios with high-frequency additions and low-frequency searches and deletions.

      Table 7-2 \u00a0 Efficiency comparison between arrays and search trees

      Unsorted array Binary search tree Search element \\(O(n)\\) \\(O(\\log n)\\) Insert element \\(O(1)\\) \\(O(\\log n)\\) Remove element \\(O(n)\\) \\(O(\\log n)\\)

      In the ideal case, a binary search tree is \"balanced,\" such that any node can be found within \\(\\log n\\) loop iterations.

      However, if we continuously insert and remove nodes in a binary search tree, it may degenerate into a linked list as shown in Figure 7-23, where the time complexity of various operations also degrades to \\(O(n)\\).

      Figure 7-23 \u00a0 Degradation of a binary search tree

      "},{"location":"chapter_tree/binary_search_tree/#743-common-applications-of-binary-search-trees","title":"7.4.3 \u00a0 Common Applications of Binary Search Trees","text":"
      • Used as multi-level indexes in systems to implement efficient search, insertion, and removal operations.
      • Serves as the underlying data structure for certain search algorithms.
      • Used to store data streams to maintain their ordered state.
      "},{"location":"chapter_tree/binary_tree/","title":"7.1 \u00a0 Binary Tree","text":"

      A binary tree is a non-linear data structure that represents the derivation relationship between \"ancestors\" and \"descendants\" and embodies the divide-and-conquer logic of \"one divides into two\". Similar to a linked list, the basic unit of a binary tree is a node, and each node contains a value, a reference to its left child node, and a reference to its right child node.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
      class TreeNode:\n    \"\"\"Binary tree node\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val                # Node value\n        self.left: TreeNode | None = None  # Reference to left child node\n        self.right: TreeNode | None = None # Reference to right child node\n
      /* Binary tree node */\nstruct TreeNode {\n    int val;          // Node value\n    TreeNode *left;   // Pointer to left child node\n    TreeNode *right;  // Pointer to right child node\n    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n};\n
      /* Binary tree node */\nclass TreeNode {\n    int val;         // Node value\n    TreeNode left;   // Reference to left child node\n    TreeNode right;  // Reference to right child node\n    TreeNode(int x) { val = x; }\n}\n
      /* Binary tree node */\nclass TreeNode(int? x) {\n    public int? val = x;    // Node value\n    public TreeNode? left;  // Reference to left child node\n    public TreeNode? right; // Reference to right child node\n}\n
      /* Binary tree node */\ntype TreeNode struct {\n    Val   int\n    Left  *TreeNode\n    Right *TreeNode\n}\n/* Constructor */\nfunc NewTreeNode(v int) *TreeNode {\n    return &TreeNode{\n        Left:  nil, // Pointer to left child node\n        Right: nil, // Pointer to right child node\n        Val:   v,   // Node value\n    }\n}\n
      /* Binary tree node */\nclass TreeNode {\n    var val: Int // Node value\n    var left: TreeNode? // Reference to left child node\n    var right: TreeNode? // Reference to right child node\n\n    init(x: Int) {\n        val = x\n    }\n}\n
      /* Binary tree node */\nclass TreeNode {\n    val; // Node value\n    left; // Pointer to left child node\n    right; // Pointer to right child node\n    constructor(val, left, right) {\n        this.val = val === undefined ? 0 : val;\n        this.left = left === undefined ? null : left;\n        this.right = right === undefined ? null : right;\n    }\n}\n
      /* Binary tree node */\nclass TreeNode {\n    val: number;\n    left: TreeNode | null;\n    right: TreeNode | null;\n\n    constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {\n        this.val = val === undefined ? 0 : val; // Node value\n        this.left = left === undefined ? null : left; // Reference to left child node\n        this.right = right === undefined ? null : right; // Reference to right child node\n    }\n}\n
      /* Binary tree node */\nclass TreeNode {\n  int val;         // Node value\n  TreeNode? left;  // Reference to left child node\n  TreeNode? right; // Reference to right child node\n  TreeNode(this.val, [this.left, this.right]);\n}\n
      use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* Binary tree node */\nstruct TreeNode {\n    val: i32,                               // Node value\n    left: Option<Rc<RefCell<TreeNode>>>,    // Reference to left child node\n    right: Option<Rc<RefCell<TreeNode>>>,   // Reference to right child node\n}\n\nimpl TreeNode {\n    /* Constructor */\n    fn new(val: i32) -> Rc<RefCell<Self>> {\n        Rc::new(RefCell::new(Self {\n            val,\n            left: None,\n            right: None\n        }))\n    }\n}\n
      /* Binary tree node */\ntypedef struct TreeNode {\n    int val;                // Node value\n    int height;             // Node height\n    struct TreeNode *left;  // Pointer to left child node\n    struct TreeNode *right; // Pointer to right child node\n} TreeNode;\n\n/* Constructor */\nTreeNode *newTreeNode(int val) {\n    TreeNode *node;\n\n    node = (TreeNode *)malloc(sizeof(TreeNode));\n    node->val = val;\n    node->height = 0;\n    node->left = NULL;\n    node->right = NULL;\n    return node;\n}\n
      /* Binary tree node */\nclass TreeNode(val _val: Int) {  // Node value\n    val left: TreeNode? = null   // Reference to left child node\n    val right: TreeNode? = null  // Reference to right child node\n}\n
      ### Binary tree node class ###\nclass TreeNode\n  attr_accessor :val    # Node value\n  attr_accessor :left   # Reference to left child node\n  attr_accessor :right  # Reference to right child node\n\n  def initialize(val)\n    @val = val\n  end\nend\n

      Each node has two references (pointers), pointing respectively to the left-child node and right-child node. This node is called the parent node of these two child nodes. When given a node of a binary tree, we call the tree formed by this node's left child and all nodes below it the left subtree of this node. Similarly, the right subtree can be defined.

      In a binary tree, except leaf nodes, all other nodes contain child nodes and non-empty subtrees. As shown in Figure 7-1, if \"Node 2\" is regarded as a parent node, its left and right child nodes are \"Node 4\" and \"Node 5\" respectively. The left subtree is formed by \"Node 4\" and all nodes beneath it, while the right subtree is formed by \"Node 5\" and all nodes beneath it.

      Figure 7-1 \u00a0 Parent Node, child Node, subtree

      "},{"location":"chapter_tree/binary_tree/#711-common-terminology-of-binary-trees","title":"7.1.1 \u00a0 Common Terminology of Binary Trees","text":"

      The commonly used terminology of binary trees is shown in Figure 7-2.

      • Root node: The node at the top level of a binary tree, which does not have a parent node.
      • Leaf node: A node that does not have any child nodes, with both of its pointers pointing to None.
      • Edge: A line segment that connects two nodes, representing a reference (pointer) between the nodes.
      • The level of a node: It increases from top to bottom, with the root node being at level 1.
      • The degree of a node: The number of child nodes that a node has. In a binary tree, the degree can be 0, 1, or 2.
      • The height of a binary tree: The number of edges from the root node to the farthest leaf node.
      • The depth of a node: The number of edges from the root node to the node.
      • The height of a node: The number of edges from the farthest leaf node to the node.

      Figure 7-2 \u00a0 Common Terminology of Binary Trees

      Tip

      Please note that we usually define \"height\" and \"depth\" as \"the number of edges traversed\", but some questions or textbooks may define them as \"the number of nodes traversed\". In this case, both height and depth need to be incremented by 1.

      "},{"location":"chapter_tree/binary_tree/#712-basic-operations-of-binary-trees","title":"7.1.2 \u00a0 Basic Operations of Binary Trees","text":""},{"location":"chapter_tree/binary_tree/#1-initializing-a-binary-tree","title":"1. \u00a0 Initializing a Binary Tree","text":"

      Similar to a linked list, the initialization of a binary tree involves first creating the nodes and then establishing the references (pointers) between them.

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree.py
      # Initializing a binary tree\n# Initializing nodes\nn1 = TreeNode(val=1)\nn2 = TreeNode(val=2)\nn3 = TreeNode(val=3)\nn4 = TreeNode(val=4)\nn5 = TreeNode(val=5)\n# Linking references (pointers) between nodes\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
      binary_tree.cpp
      /* Initializing a binary tree */\n// Initializing nodes\nTreeNode* n1 = new TreeNode(1);\nTreeNode* n2 = new TreeNode(2);\nTreeNode* n3 = new TreeNode(3);\nTreeNode* n4 = new TreeNode(4);\nTreeNode* n5 = new TreeNode(5);\n// Linking references (pointers) between nodes\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
      binary_tree.java
      // Initializing nodes\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// Linking references (pointers) between nodes\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
      binary_tree.cs
      /* Initializing a binary tree */\n// Initializing nodes\nTreeNode n1 = new(1);\nTreeNode n2 = new(2);\nTreeNode n3 = new(3);\nTreeNode n4 = new(4);\nTreeNode n5 = new(5);\n// Linking references (pointers) between nodes\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
      binary_tree.go
      /* Initializing a binary tree */\n// Initializing nodes\nn1 := NewTreeNode(1)\nn2 := NewTreeNode(2)\nn3 := NewTreeNode(3)\nn4 := NewTreeNode(4)\nn5 := NewTreeNode(5)\n// Linking references (pointers) between nodes\nn1.Left = n2\nn1.Right = n3\nn2.Left = n4\nn2.Right = n5\n
      binary_tree.swift
      // Initializing nodes\nlet n1 = TreeNode(x: 1)\nlet n2 = TreeNode(x: 2)\nlet n3 = TreeNode(x: 3)\nlet n4 = TreeNode(x: 4)\nlet n5 = TreeNode(x: 5)\n// Linking references (pointers) between nodes\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
      binary_tree.js
      /* Initializing a binary tree */\n// Initializing nodes\nlet n1 = new TreeNode(1),\n    n2 = new TreeNode(2),\n    n3 = new TreeNode(3),\n    n4 = new TreeNode(4),\n    n5 = new TreeNode(5);\n// Linking references (pointers) between nodes\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
      binary_tree.ts
      /* Initializing a binary tree */\n// Initializing nodes\nlet n1 = new TreeNode(1),\n    n2 = new TreeNode(2),\n    n3 = new TreeNode(3),\n    n4 = new TreeNode(4),\n    n5 = new TreeNode(5);\n// Linking references (pointers) between nodes\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
      binary_tree.dart
      /* Initializing a binary tree */\n// Initializing nodes\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// Linking references (pointers) between nodes\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
      binary_tree.rs
      // Initializing nodes\nlet n1 = TreeNode::new(1);\nlet n2 = TreeNode::new(2);\nlet n3 = TreeNode::new(3);\nlet n4 = TreeNode::new(4);\nlet n5 = TreeNode::new(5);\n// Linking references (pointers) between nodes\nn1.borrow_mut().left = Some(n2.clone());\nn1.borrow_mut().right = Some(n3);\nn2.borrow_mut().left = Some(n4);\nn2.borrow_mut().right = Some(n5);\n
      binary_tree.c
      /* Initializing a binary tree */\n// Initializing nodes\nTreeNode *n1 = newTreeNode(1);\nTreeNode *n2 = newTreeNode(2);\nTreeNode *n3 = newTreeNode(3);\nTreeNode *n4 = newTreeNode(4);\nTreeNode *n5 = newTreeNode(5);\n// Linking references (pointers) between nodes\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
      binary_tree.kt
      // Initializing nodes\nval n1 = TreeNode(1)\nval n2 = TreeNode(2)\nval n3 = TreeNode(3)\nval n4 = TreeNode(4)\nval n5 = TreeNode(5)\n// Linking references (pointers) between nodes\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
      binary_tree.rb
      # Initializing a binary tree\n# Initializing nodes\nn1 = TreeNode.new(1)\nn2 = TreeNode.new(2)\nn3 = TreeNode.new(3)\nn4 = TreeNode.new(4)\nn5 = TreeNode.new(5)\n# Linking references (pointers) between nodes\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
      Code Visualization

      Full Screen >

      "},{"location":"chapter_tree/binary_tree/#2-inserting-and-removing-nodes","title":"2. \u00a0 Inserting and Removing Nodes","text":"

      Similar to a linked list, inserting and removing nodes in a binary tree can be achieved by modifying pointers. Figure 7-3 provides an example.

      Figure 7-3 \u00a0 Inserting and removing nodes in a binary tree

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree.py
      # Inserting and removing nodes\np = TreeNode(0)\n# Inserting node P between n1 -> n2\nn1.left = p\np.left = n2\n# Removing node P\nn1.left = n2\n
      binary_tree.cpp
      /* Inserting and removing nodes */\nTreeNode* P = new TreeNode(0);\n// Inserting node P between n1 and n2\nn1->left = P;\nP->left = n2;\n// Removing node P\nn1->left = n2;\n
      binary_tree.java
      TreeNode P = new TreeNode(0);\n// Inserting node P between n1 and n2\nn1.left = P;\nP.left = n2;\n// Removing node P\nn1.left = n2;\n
      binary_tree.cs
      /* Inserting and removing nodes */\nTreeNode P = new(0);\n// Inserting node P between n1 and n2\nn1.left = P;\nP.left = n2;\n// Removing node P\nn1.left = n2;\n
      binary_tree.go
      /* Inserting and removing nodes */\n// Inserting node P between n1 and n2\np := NewTreeNode(0)\nn1.Left = p\np.Left = n2\n// Removing node P\nn1.Left = n2\n
      binary_tree.swift
      let P = TreeNode(x: 0)\n// Inserting node P between n1 and n2\nn1.left = P\nP.left = n2\n// Removing node P\nn1.left = n2\n
      binary_tree.js
      /* Inserting and removing nodes */\nlet P = new TreeNode(0);\n// Inserting node P between n1 and n2\nn1.left = P;\nP.left = n2;\n// Removing node P\nn1.left = n2;\n
      binary_tree.ts
      /* Inserting and removing nodes */\nconst P = new TreeNode(0);\n// Inserting node P between n1 and n2\nn1.left = P;\nP.left = n2;\n// Removing node P\nn1.left = n2;\n
      binary_tree.dart
      /* Inserting and removing nodes */\nTreeNode P = new TreeNode(0);\n// Inserting node P between n1 and n2\nn1.left = P;\nP.left = n2;\n// Removing node P\nn1.left = n2;\n
      binary_tree.rs
      let p = TreeNode::new(0);\n// Inserting node P between n1 and n2\nn1.borrow_mut().left = Some(p.clone());\np.borrow_mut().left = Some(n2.clone());\n// Removing node P\nn1.borrow_mut().left = Some(n2);\n
      binary_tree.c
      /* Inserting and removing nodes */\nTreeNode *P = newTreeNode(0);\n// Inserting node P between n1 and n2\nn1->left = P;\nP->left = n2;\n// Removing node P\nn1->left = n2;\n
      binary_tree.kt
      val P = TreeNode(0)\n// Inserting node P between n1 and n2\nn1.left = P\nP.left = n2\n// Removing node P\nn1.left = n2\n
      binary_tree.rb
      # Inserting and removing nodes\n_p = TreeNode.new(0)\n# Inserting node _p between n1 and n2\nn1.left = _p\n_p.left = n2\n# Removing node _p\nn1.left = n2\n
      Code Visualization

      Full Screen >

      Tip

      It should be noted that inserting nodes may change the original logical structure of the binary tree, while removing nodes typically involves removing the node and all its subtrees. Therefore, in a binary tree, insertion and removal are usually performed through a set of operations to achieve meaningful outcomes.

      "},{"location":"chapter_tree/binary_tree/#713-common-types-of-binary-trees","title":"7.1.3 \u00a0 Common Types of Binary Trees","text":""},{"location":"chapter_tree/binary_tree/#1-perfect-binary-tree","title":"1. \u00a0 Perfect Binary Tree","text":"

      As shown in Figure 7-4, a perfect binary tree has all levels completely filled with nodes. In a perfect binary tree, leaf nodes have a degree of \\(0\\), while all other nodes have a degree of \\(2\\). If the tree height is \\(h\\), the total number of nodes is \\(2^{h+1} - 1\\), exhibiting a standard exponential relationship that reflects the common phenomenon of cell division in nature.

      Tip

      Please note that in the Chinese community, a perfect binary tree is often referred to as a full binary tree.

      Figure 7-4 \u00a0 Perfect binary tree

      "},{"location":"chapter_tree/binary_tree/#2-complete-binary-tree","title":"2. \u00a0 Complete Binary Tree","text":"

      As shown in Figure 7-5, a complete binary tree only allows the bottom level to be incompletely filled, and the nodes at the bottom level must be filled continuously from left to right. Note that a perfect binary tree is also a complete binary tree.

      Figure 7-5 \u00a0 Complete binary tree

      "},{"location":"chapter_tree/binary_tree/#3-full-binary-tree","title":"3. \u00a0 Full Binary Tree","text":"

      As shown in Figure 7-6, in a full binary tree, all nodes except leaf nodes have two child nodes.

      Figure 7-6 \u00a0 Full binary tree

      "},{"location":"chapter_tree/binary_tree/#4-balanced-binary-tree","title":"4. \u00a0 Balanced Binary Tree","text":"

      As shown in Figure 7-7, in a balanced binary tree, the absolute difference between the height of the left and right subtrees of any node does not exceed 1.

      Figure 7-7 \u00a0 Balanced binary tree

      "},{"location":"chapter_tree/binary_tree/#714-degeneration-of-binary-trees","title":"7.1.4 \u00a0 Degeneration of Binary Trees","text":"

      Figure 7-8 shows the ideal and degenerate structures of binary trees. When every level of a binary tree is filled, it reaches the \"perfect binary tree\" state; when all nodes are biased toward one side, the binary tree degenerates into a \"linked list\".

      • A perfect binary tree is the ideal case, fully leveraging the \"divide and conquer\" advantage of binary trees.
      • A linked list represents the other extreme, where all operations become linear operations with time complexity degrading to \\(O(n)\\).

      Figure 7-8 \u00a0 The Best and Worst Structures of Binary Trees

      As shown in Table 7-1, in the best and worst structures, the binary tree achieves either maximum or minimum values for leaf node count, total number of nodes, and height.

      Table 7-1 \u00a0 The Best and Worst Structures of Binary Trees

      Perfect binary tree Linked list Number of nodes at level \\(i\\) \\(2^{i-1}\\) \\(1\\) Number of leaf nodes in a tree with height \\(h\\) \\(2^h\\) \\(1\\) Total number of nodes in a tree with height \\(h\\) \\(2^{h+1} - 1\\) \\(h + 1\\) Height of a tree with \\(n\\) total nodes \\(\\log_2 (n+1) - 1\\) \\(n - 1\\)"},{"location":"chapter_tree/binary_tree_traversal/","title":"7.2 \u00a0 Binary Tree Traversal","text":"

      From a physical structure perspective, a tree is a data structure based on linked lists. Hence, its traversal method involves accessing nodes one by one through pointers. However, a tree is a non-linear data structure, which makes traversing a tree more complex than traversing a linked list, requiring the assistance of search algorithms.

      The common traversal methods for binary trees include level-order traversal, pre-order traversal, in-order traversal, and post-order traversal.

      "},{"location":"chapter_tree/binary_tree_traversal/#721-level-order-traversal","title":"7.2.1 \u00a0 Level-Order Traversal","text":"

      As shown in Figure 7-9, level-order traversal traverses the binary tree from top to bottom, layer by layer. Within each level, it visits nodes from left to right.

      Level-order traversal is essentially breadth-first traversal, also known as breadth-first search (BFS), which embodies a \"expanding outward circle by circle\" layer-by-layer traversal method.

      Figure 7-9 \u00a0 Level-order traversal of a binary tree

      "},{"location":"chapter_tree/binary_tree_traversal/#1-code-implementation","title":"1. \u00a0 Code Implementation","text":"

      Breadth-first traversal is typically implemented with the help of a \"queue\". The queue follows the \"first in, first out\" rule, while breadth-first traversal follows the \"layer-by-layer progression\" rule; the underlying ideas of the two are consistent. The implementation code is as follows:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree_bfs.py
      def level_order(root: TreeNode | None) -> list[int]:\n    \"\"\"Level-order traversal\"\"\"\n    # Initialize queue, add root node\n    queue: deque[TreeNode] = deque()\n    queue.append(root)\n    # Initialize a list to save the traversal sequence\n    res = []\n    while queue:\n        node: TreeNode = queue.popleft()  # Dequeue\n        res.append(node.val)  # Save node value\n        if node.left is not None:\n            queue.append(node.left)  # Left child node enqueue\n        if node.right is not None:\n            queue.append(node.right)  # Right child node enqueue\n    return res\n
      binary_tree_bfs.cpp
      /* Level-order traversal */\nvector<int> levelOrder(TreeNode *root) {\n    // Initialize queue, add root node\n    queue<TreeNode *> queue;\n    queue.push(root);\n    // Initialize a list to save the traversal sequence\n    vector<int> vec;\n    while (!queue.empty()) {\n        TreeNode *node = queue.front();\n        queue.pop();              // Dequeue\n        vec.push_back(node->val); // Save node value\n        if (node->left != nullptr)\n            queue.push(node->left); // Left child node enqueue\n        if (node->right != nullptr)\n            queue.push(node->right); // Right child node enqueue\n    }\n    return vec;\n}\n
      binary_tree_bfs.java
      /* Level-order traversal */\nList<Integer> levelOrder(TreeNode root) {\n    // Initialize queue, add root node\n    Queue<TreeNode> queue = new LinkedList<>();\n    queue.add(root);\n    // Initialize a list to save the traversal sequence\n    List<Integer> list = new ArrayList<>();\n    while (!queue.isEmpty()) {\n        TreeNode node = queue.poll(); // Dequeue\n        list.add(node.val);           // Save node value\n        if (node.left != null)\n            queue.offer(node.left);   // Left child node enqueue\n        if (node.right != null)\n            queue.offer(node.right);  // Right child node enqueue\n    }\n    return list;\n}\n
      binary_tree_bfs.cs
      /* Level-order traversal */\nList<int> LevelOrder(TreeNode root) {\n    // Initialize queue, add root node\n    Queue<TreeNode> queue = new();\n    queue.Enqueue(root);\n    // Initialize a list to save the traversal sequence\n    List<int> list = [];\n    while (queue.Count != 0) {\n        TreeNode node = queue.Dequeue(); // Dequeue\n        list.Add(node.val!.Value);       // Save node value\n        if (node.left != null)\n            queue.Enqueue(node.left);    // Left child node enqueue\n        if (node.right != null)\n            queue.Enqueue(node.right);   // Right child node enqueue\n    }\n    return list;\n}\n
      binary_tree_bfs.go
      /* Level-order traversal */\nfunc levelOrder(root *TreeNode) []any {\n    // Initialize queue, add root node\n    queue := list.New()\n    queue.PushBack(root)\n    // Initialize a slice to save traversal sequence\n    nums := make([]any, 0)\n    for queue.Len() > 0 {\n        // Dequeue\n        node := queue.Remove(queue.Front()).(*TreeNode)\n        // Save node value\n        nums = append(nums, node.Val)\n        if node.Left != nil {\n            // Left child node enqueue\n            queue.PushBack(node.Left)\n        }\n        if node.Right != nil {\n            // Right child node enqueue\n            queue.PushBack(node.Right)\n        }\n    }\n    return nums\n}\n
      binary_tree_bfs.swift
      /* Level-order traversal */\nfunc levelOrder(root: TreeNode) -> [Int] {\n    // Initialize queue, add root node\n    var queue: [TreeNode] = [root]\n    // Initialize a list to save the traversal sequence\n    var list: [Int] = []\n    while !queue.isEmpty {\n        let node = queue.removeFirst() // Dequeue\n        list.append(node.val) // Save node value\n        if let left = node.left {\n            queue.append(left) // Left child node enqueue\n        }\n        if let right = node.right {\n            queue.append(right) // Right child node enqueue\n        }\n    }\n    return list\n}\n
      binary_tree_bfs.js
      /* Level-order traversal */\nfunction levelOrder(root) {\n    // Initialize queue, add root node\n    const queue = [root];\n    // Initialize a list to save the traversal sequence\n    const list = [];\n    while (queue.length) {\n        let node = queue.shift(); // Dequeue\n        list.push(node.val); // Save node value\n        if (node.left) queue.push(node.left); // Left child node enqueue\n        if (node.right) queue.push(node.right); // Right child node enqueue\n    }\n    return list;\n}\n
      binary_tree_bfs.ts
      /* Level-order traversal */\nfunction levelOrder(root: TreeNode | null): number[] {\n    // Initialize queue, add root node\n    const queue = [root];\n    // Initialize a list to save the traversal sequence\n    const list: number[] = [];\n    while (queue.length) {\n        let node = queue.shift() as TreeNode; // Dequeue\n        list.push(node.val); // Save node value\n        if (node.left) {\n            queue.push(node.left); // Left child node enqueue\n        }\n        if (node.right) {\n            queue.push(node.right); // Right child node enqueue\n        }\n    }\n    return list;\n}\n
      binary_tree_bfs.dart
      /* Level-order traversal */\nList<int> levelOrder(TreeNode? root) {\n  // Initialize queue, add root node\n  Queue<TreeNode?> queue = Queue();\n  queue.add(root);\n  // Initialize a list to save the traversal sequence\n  List<int> res = [];\n  while (queue.isNotEmpty) {\n    TreeNode? node = queue.removeFirst(); // Dequeue\n    res.add(node!.val); // Save node value\n    if (node.left != null) queue.add(node.left); // Left child node enqueue\n    if (node.right != null) queue.add(node.right); // Right child node enqueue\n  }\n  return res;\n}\n
      binary_tree_bfs.rs
      /* Level-order traversal */\nfn level_order(root: &Rc<RefCell<TreeNode>>) -> Vec<i32> {\n    // Initialize queue, add root node\n    let mut que = VecDeque::new();\n    que.push_back(root.clone());\n    // Initialize a list to save the traversal sequence\n    let mut vec = Vec::new();\n\n    while let Some(node) = que.pop_front() {\n        // Dequeue\n        vec.push(node.borrow().val); // Save node value\n        if let Some(left) = node.borrow().left.as_ref() {\n            que.push_back(left.clone()); // Left child node enqueue\n        }\n        if let Some(right) = node.borrow().right.as_ref() {\n            que.push_back(right.clone()); // Right child node enqueue\n        };\n    }\n    vec\n}\n
      binary_tree_bfs.c
      /* Level-order traversal */\nint *levelOrder(TreeNode *root, int *size) {\n    /* Auxiliary queue */\n    int front, rear;\n    int index, *arr;\n    TreeNode *node;\n    TreeNode **queue;\n\n    /* Auxiliary queue */\n    queue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_SIZE);\n    // Queue pointer\n    front = 0, rear = 0;\n    // Add root node\n    queue[rear++] = root;\n    // Initialize a list to save the traversal sequence\n    /* Auxiliary array */\n    arr = (int *)malloc(sizeof(int) * MAX_SIZE);\n    // Array pointer\n    index = 0;\n    while (front < rear) {\n        // Dequeue\n        node = queue[front++];\n        // Save node value\n        arr[index++] = node->val;\n        if (node->left != NULL) {\n            // Left child node enqueue\n            queue[rear++] = node->left;\n        }\n        if (node->right != NULL) {\n            // Right child node enqueue\n            queue[rear++] = node->right;\n        }\n    }\n    // Update array length value\n    *size = index;\n    arr = realloc(arr, sizeof(int) * (*size));\n\n    // Free auxiliary array space\n    free(queue);\n    return arr;\n}\n
      binary_tree_bfs.kt
      /* Level-order traversal */\nfun levelOrder(root: TreeNode?): MutableList<Int> {\n    // Initialize queue, add root node\n    val queue = LinkedList<TreeNode?>()\n    queue.add(root)\n    // Initialize a list to save the traversal sequence\n    val list = mutableListOf<Int>()\n    while (queue.isNotEmpty()) {\n        val node = queue.poll()      // Dequeue\n        list.add(node?._val!!)       // Save node value\n        if (node.left != null)\n            queue.offer(node.left)   // Left child node enqueue\n        if (node.right != null)\n            queue.offer(node.right)  // Right child node enqueue\n    }\n    return list\n}\n
      binary_tree_bfs.rb
      ### Level-order traversal ###\ndef level_order(root)\n  # Initialize queue, add root node\n  queue = [root]\n  # Initialize a list to save the traversal sequence\n  res = []\n  while !queue.empty?\n    node = queue.shift # Dequeue\n    res << node.val # Save node value\n    queue << node.left unless node.left.nil? # Left child node enqueue\n    queue << node.right unless node.right.nil? # Right child node enqueue\n  end\n  res\nend\n
      "},{"location":"chapter_tree/binary_tree_traversal/#2-complexity-analysis","title":"2. \u00a0 Complexity Analysis","text":"
      • Time complexity is \\(O(n)\\): All nodes are visited once, using \\(O(n)\\) time, where \\(n\\) is the number of nodes.
      • Space complexity is \\(O(n)\\): In the worst case, i.e., a full binary tree, before traversing to the bottom level, the queue contains at most \\((n + 1) / 2\\) nodes simultaneously, occupying \\(O(n)\\) space.
      "},{"location":"chapter_tree/binary_tree_traversal/#722-preorder-inorder-and-postorder-traversal","title":"7.2.2 \u00a0 Preorder, Inorder, and Postorder Traversal","text":"

      Correspondingly, preorder, inorder, and postorder traversals all belong to depth-first traversal, also known as depth-first search (DFS), which embodies a \"first go to the end, then backtrack and continue\" traversal method.

      Figure 7-10 shows how depth-first traversal works on a binary tree. Depth-first traversal is like \"walking\" around the perimeter of the entire binary tree, encountering three positions at each node, corresponding to preorder, inorder, and postorder traversal.

      Figure 7-10 \u00a0 Preorder, inorder, and postorder traversal of a binary tree

      "},{"location":"chapter_tree/binary_tree_traversal/#1-code-implementation_1","title":"1. \u00a0 Code Implementation","text":"

      Depth-first search is usually implemented based on recursion:

      PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree_dfs.py
      def pre_order(root: TreeNode | None):\n    \"\"\"Preorder traversal\"\"\"\n    if root is None:\n        return\n    # Visit priority: root node -> left subtree -> right subtree\n    res.append(root.val)\n    pre_order(root=root.left)\n    pre_order(root=root.right)\n\ndef in_order(root: TreeNode | None):\n    \"\"\"Inorder traversal\"\"\"\n    if root is None:\n        return\n    # Visit priority: left subtree -> root node -> right subtree\n    in_order(root=root.left)\n    res.append(root.val)\n    in_order(root=root.right)\n\ndef post_order(root: TreeNode | None):\n    \"\"\"Postorder traversal\"\"\"\n    if root is None:\n        return\n    # Visit priority: left subtree -> right subtree -> root node\n    post_order(root=root.left)\n    post_order(root=root.right)\n    res.append(root.val)\n
      binary_tree_dfs.cpp
      /* Preorder traversal */\nvoid preOrder(TreeNode *root) {\n    if (root == nullptr)\n        return;\n    // Visit priority: root node -> left subtree -> right subtree\n    vec.push_back(root->val);\n    preOrder(root->left);\n    preOrder(root->right);\n}\n\n/* Inorder traversal */\nvoid inOrder(TreeNode *root) {\n    if (root == nullptr)\n        return;\n    // Visit priority: left subtree -> root node -> right subtree\n    inOrder(root->left);\n    vec.push_back(root->val);\n    inOrder(root->right);\n}\n\n/* Postorder traversal */\nvoid postOrder(TreeNode *root) {\n    if (root == nullptr)\n        return;\n    // Visit priority: left subtree -> right subtree -> root node\n    postOrder(root->left);\n    postOrder(root->right);\n    vec.push_back(root->val);\n}\n
      binary_tree_dfs.java
      /* Preorder traversal */\nvoid preOrder(TreeNode root) {\n    if (root == null)\n        return;\n    // Visit priority: root node -> left subtree -> right subtree\n    list.add(root.val);\n    preOrder(root.left);\n    preOrder(root.right);\n}\n\n/* Inorder traversal */\nvoid inOrder(TreeNode root) {\n    if (root == null)\n        return;\n    // Visit priority: left subtree -> root node -> right subtree\n    inOrder(root.left);\n    list.add(root.val);\n    inOrder(root.right);\n}\n\n/* Postorder traversal */\nvoid postOrder(TreeNode root) {\n    if (root == null)\n        return;\n    // Visit priority: left subtree -> right subtree -> root node\n    postOrder(root.left);\n    postOrder(root.right);\n    list.add(root.val);\n}\n
      binary_tree_dfs.cs
      /* Preorder traversal */\nvoid PreOrder(TreeNode? root) {\n    if (root == null) return;\n    // Visit priority: root node -> left subtree -> right subtree\n    list.Add(root.val!.Value);\n    PreOrder(root.left);\n    PreOrder(root.right);\n}\n\n/* Inorder traversal */\nvoid InOrder(TreeNode? root) {\n    if (root == null) return;\n    // Visit priority: left subtree -> root node -> right subtree\n    InOrder(root.left);\n    list.Add(root.val!.Value);\n    InOrder(root.right);\n}\n\n/* Postorder traversal */\nvoid PostOrder(TreeNode? root) {\n    if (root == null) return;\n    // Visit priority: left subtree -> right subtree -> root node\n    PostOrder(root.left);\n    PostOrder(root.right);\n    list.Add(root.val!.Value);\n}\n
      binary_tree_dfs.go
      /* Preorder traversal */\nfunc preOrder(node *TreeNode) {\n    if node == nil {\n        return\n    }\n    // Visit priority: root node -> left subtree -> right subtree\n    nums = append(nums, node.Val)\n    preOrder(node.Left)\n    preOrder(node.Right)\n}\n\n/* Inorder traversal */\nfunc inOrder(node *TreeNode) {\n    if node == nil {\n        return\n    }\n    // Visit priority: left subtree -> root node -> right subtree\n    inOrder(node.Left)\n    nums = append(nums, node.Val)\n    inOrder(node.Right)\n}\n\n/* Postorder traversal */\nfunc postOrder(node *TreeNode) {\n    if node == nil {\n        return\n    }\n    // Visit priority: left subtree -> right subtree -> root node\n    postOrder(node.Left)\n    postOrder(node.Right)\n    nums = append(nums, node.Val)\n}\n
      binary_tree_dfs.swift
      /* Preorder traversal */\nfunc preOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    // Visit priority: root node -> left subtree -> right subtree\n    list.append(root.val)\n    preOrder(root: root.left)\n    preOrder(root: root.right)\n}\n\n/* Inorder traversal */\nfunc inOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    // Visit priority: left subtree -> root node -> right subtree\n    inOrder(root: root.left)\n    list.append(root.val)\n    inOrder(root: root.right)\n}\n\n/* Postorder traversal */\nfunc postOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    // Visit priority: left subtree -> right subtree -> root node\n    postOrder(root: root.left)\n    postOrder(root: root.right)\n    list.append(root.val)\n}\n
      binary_tree_dfs.js
      /* Preorder traversal */\nfunction preOrder(root) {\n    if (root === null) return;\n    // Visit priority: root node -> left subtree -> right subtree\n    list.push(root.val);\n    preOrder(root.left);\n    preOrder(root.right);\n}\n\n/* Inorder traversal */\nfunction inOrder(root) {\n    if (root === null) return;\n    // Visit priority: left subtree -> root node -> right subtree\n    inOrder(root.left);\n    list.push(root.val);\n    inOrder(root.right);\n}\n\n/* Postorder traversal */\nfunction postOrder(root) {\n    if (root === null) return;\n    // Visit priority: left subtree -> right subtree -> root node\n    postOrder(root.left);\n    postOrder(root.right);\n    list.push(root.val);\n}\n
      binary_tree_dfs.ts
      /* Preorder traversal */\nfunction preOrder(root: TreeNode | null): void {\n    if (root === null) {\n        return;\n    }\n    // Visit priority: root node -> left subtree -> right subtree\n    list.push(root.val);\n    preOrder(root.left);\n    preOrder(root.right);\n}\n\n/* Inorder traversal */\nfunction inOrder(root: TreeNode | null): void {\n    if (root === null) {\n        return;\n    }\n    // Visit priority: left subtree -> root node -> right subtree\n    inOrder(root.left);\n    list.push(root.val);\n    inOrder(root.right);\n}\n\n/* Postorder traversal */\nfunction postOrder(root: TreeNode | null): void {\n    if (root === null) {\n        return;\n    }\n    // Visit priority: left subtree -> right subtree -> root node\n    postOrder(root.left);\n    postOrder(root.right);\n    list.push(root.val);\n}\n
      binary_tree_dfs.dart
      /* Preorder traversal */\nvoid preOrder(TreeNode? node) {\n  if (node == null) return;\n  // Visit priority: root node -> left subtree -> right subtree\n  list.add(node.val);\n  preOrder(node.left);\n  preOrder(node.right);\n}\n\n/* Inorder traversal */\nvoid inOrder(TreeNode? node) {\n  if (node == null) return;\n  // Visit priority: left subtree -> root node -> right subtree\n  inOrder(node.left);\n  list.add(node.val);\n  inOrder(node.right);\n}\n\n/* Postorder traversal */\nvoid postOrder(TreeNode? node) {\n  if (node == null) return;\n  // Visit priority: left subtree -> right subtree -> root node\n  postOrder(node.left);\n  postOrder(node.right);\n  list.add(node.val);\n}\n
      binary_tree_dfs.rs
      /* Preorder traversal */\nfn pre_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n    let mut result = vec![];\n\n    fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {\n        if let Some(node) = root {\n            // Visit priority: root node -> left subtree -> right subtree\n            let node = node.borrow();\n            res.push(node.val);\n            dfs(node.left.as_ref(), res);\n            dfs(node.right.as_ref(), res);\n        }\n    }\n    dfs(root, &mut result);\n\n    result\n}\n\n/* Inorder traversal */\nfn in_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n    let mut result = vec![];\n\n    fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {\n        if let Some(node) = root {\n            // Visit priority: left subtree -> root node -> right subtree\n            let node = node.borrow();\n            dfs(node.left.as_ref(), res);\n            res.push(node.val);\n            dfs(node.right.as_ref(), res);\n        }\n    }\n    dfs(root, &mut result);\n\n    result\n}\n\n/* Postorder traversal */\nfn post_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n    let mut result = vec![];\n\n    fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {\n        if let Some(node) = root {\n            // Visit priority: left subtree -> right subtree -> root node\n            let node = node.borrow();\n            dfs(node.left.as_ref(), res);\n            dfs(node.right.as_ref(), res);\n            res.push(node.val);\n        }\n    }\n\n    dfs(root, &mut result);\n\n    result\n}\n
      binary_tree_dfs.c
      /* Preorder traversal */\nvoid preOrder(TreeNode *root, int *size) {\n    if (root == NULL)\n        return;\n    // Visit priority: root node -> left subtree -> right subtree\n    arr[(*size)++] = root->val;\n    preOrder(root->left, size);\n    preOrder(root->right, size);\n}\n\n/* Inorder traversal */\nvoid inOrder(TreeNode *root, int *size) {\n    if (root == NULL)\n        return;\n    // Visit priority: left subtree -> root node -> right subtree\n    inOrder(root->left, size);\n    arr[(*size)++] = root->val;\n    inOrder(root->right, size);\n}\n\n/* Postorder traversal */\nvoid postOrder(TreeNode *root, int *size) {\n    if (root == NULL)\n        return;\n    // Visit priority: left subtree -> right subtree -> root node\n    postOrder(root->left, size);\n    postOrder(root->right, size);\n    arr[(*size)++] = root->val;\n}\n
      binary_tree_dfs.kt
      /* Preorder traversal */\nfun preOrder(root: TreeNode?) {\n    if (root == null) return\n    // Visit priority: root node -> left subtree -> right subtree\n    list.add(root._val)\n    preOrder(root.left)\n    preOrder(root.right)\n}\n\n/* Inorder traversal */\nfun inOrder(root: TreeNode?) {\n    if (root == null) return\n    // Visit priority: left subtree -> root node -> right subtree\n    inOrder(root.left)\n    list.add(root._val)\n    inOrder(root.right)\n}\n\n/* Postorder traversal */\nfun postOrder(root: TreeNode?) {\n    if (root == null) return\n    // Visit priority: left subtree -> right subtree -> root node\n    postOrder(root.left)\n    postOrder(root.right)\n    list.add(root._val)\n}\n
      binary_tree_dfs.rb
      ### Pre-order traversal ###\ndef pre_order(root)\n  return if root.nil?\n\n  # Visit priority: root node -> left subtree -> right subtree\n  $res << root.val\n  pre_order(root.left)\n  pre_order(root.right)\nend\n\n### In-order traversal ###\ndef in_order(root)\n  return if root.nil?\n\n  # Visit priority: left subtree -> root node -> right subtree\n  in_order(root.left)\n  $res << root.val\n  in_order(root.right)\nend\n\n### Post-order traversal ###\ndef post_order(root)\n  return if root.nil?\n\n  # Visit priority: left subtree -> right subtree -> root node\n  post_order(root.left)\n  post_order(root.right)\n  $res << root.val\nend\n

      Tip

      Depth-first search can also be implemented based on iteration, interested readers can study this on their own.

      Figure 7-11 shows the recursive process of preorder traversal of a binary tree, which can be divided into two opposite parts: \"recursion\" and \"return\".

      1. \"Recursion\" means opening a new method, where the program accesses the next node in this process.
      2. \"Return\" means the function returns, indicating that the current node has been fully visited.
      <1><2><3><4><5><6><7><8><9><10><11>

      Figure 7-11 \u00a0 The recursive process of preorder traversal

      "},{"location":"chapter_tree/binary_tree_traversal/#2-complexity-analysis_1","title":"2. \u00a0 Complexity Analysis","text":"
      • Time complexity is \\(O(n)\\): All nodes are visited once, using \\(O(n)\\) time.
      • Space complexity is \\(O(n)\\): In the worst case, i.e., the tree degenerates into a linked list, the recursion depth reaches \\(n\\), and the system occupies \\(O(n)\\) stack frame space.
      "},{"location":"chapter_tree/summary/","title":"7.6 \u00a0 Summary","text":""},{"location":"chapter_tree/summary/#1-key-review","title":"1. \u00a0 Key Review","text":"
      • A binary tree is a non-linear data structure that embodies the divide-and-conquer logic of \"one divides into two\". Each binary tree node contains a value and two pointers, which respectively point to its left and right child nodes.
      • For a certain node in a binary tree, the tree formed by its left (right) child node and all nodes below is called the left (right) subtree of that node.
      • Related terminology of binary trees includes root node, leaf node, level, degree, edge, height, and depth.
      • The initialization, node insertion, and node removal operations of binary trees are similar to those of linked lists.
      • Common types of binary trees include perfect binary trees, complete binary trees, full binary trees, and balanced binary trees. The perfect binary tree is the ideal state, while the linked list is the worst state after degradation.
      • A binary tree can be represented using an array by arranging node values and empty slots in level-order traversal sequence, and implementing pointers based on the index mapping relationship between parent and child nodes.
      • Level-order traversal of a binary tree is a breadth-first search method, embodying a layer-by-layer traversal approach of \"expanding outward circle by circle\", typically implemented using a queue.
      • Preorder, inorder, and postorder traversals all belong to depth-first search, embodying a traversal approach of \"first go to the end, then backtrack and continue\", typically implemented using recursion.
      • A binary search tree is an efficient data structure for element searching, with search, insertion, and removal operations all having time complexity of \\(O(\\log n)\\). When a binary search tree degenerates into a linked list, all time complexities degrade to \\(O(n)\\).
      • An AVL tree, also known as a balanced binary search tree, ensures the tree remains balanced after continuous node insertions and removals through rotation operations.
      • Rotation operations in AVL trees include right rotation, left rotation, left rotation then right rotation, and right rotation then left rotation. After inserting or removing nodes, AVL trees perform rotation operations from bottom to top to restore the tree to balance.
      "},{"location":"chapter_tree/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

      Q: For a binary tree with only one node, are both the height of the tree and the depth of the root node \\(0\\)?

      Yes, because height and depth are typically defined as \"the number of edges passed.\"

      Q: The insertion and removal in a binary tree are generally accomplished by a set of operations. What does \"a set of operations\" refer to here? Does it imply releasing the resources of the child nodes?

      Taking the binary search tree as an example, the operation of removing a node needs to be handled in three different scenarios, each requiring multiple steps of node operations.

      Q: Why does DFS traversal of binary trees have three orders: preorder, inorder, and postorder, and what are their uses?

      Similar to forward and reverse traversal of arrays, preorder, inorder, and postorder traversals are three methods of binary tree traversal that allow us to obtain a traversal result in a specific order. For example, in a binary search tree, since nodes satisfy the relationship left child node value < root node value < right child node value, we only need to traverse the tree with the priority of \"left \\(\\rightarrow\\) root \\(\\rightarrow\\) right\" to obtain an ordered node sequence.

      Q: In a right rotation operation handling the relationship between unbalanced nodes node, child, and grand_child, doesn't the connection between node and its parent node get lost after the right rotation?

      We need to view this problem from a recursive perspective. The right rotation operation right_rotate(root) passes in the root node of the subtree and eventually returns the root node of the subtree after rotation with return child. The connection between the subtree's root node and its parent node is completed after the function returns, which is not within the maintenance scope of the right rotation operation.

      Q: In C++, functions are divided into private and public sections. What considerations are there for this? Why are the height() function and the updateHeight() function placed in public and private, respectively?

      It mainly depends on the method's usage scope. If a method is only used within the class, then it is designed as private. For example, calling updateHeight() alone by the user makes no sense, as it is only a step in insertion or removal operations. However, height() is used to access node height, similar to vector.size(), so it is set to public for ease of use.

      Q: How do you build a binary search tree from a set of input data? Is the choice of root node very important?

      Yes, the method for building a tree is provided in the build_tree() method in the binary search tree code. As for the choice of root node, we typically sort the input data, then select the middle element as the root node, and recursively build the left and right subtrees. This approach maximizes the tree's balance.

      Q: In Java, do you always have to use the equals() method for string comparison?

      In Java, for primitive data types, == is used to compare whether the values of two variables are equal. For reference types, the working principles of the two symbols are different.

      • ==: Used to compare whether two variables point to the same object, i.e., whether their positions in memory are the same.
      • equals(): Used to compare whether the values of two objects are equal.

      Therefore, if we want to compare values, we should use equals(). However, strings initialized via String a = \"hi\"; String b = \"hi\"; are stored in the string constant pool and point to the same object, so a == b can also be used to compare the contents of the two strings.

      Q: Before reaching the bottom level, is the number of nodes in the queue \\(2^h\\) in breadth-first traversal?

      Yes, for example, a full binary tree with height \\(h = 2\\) has a total of \\(n = 7\\) nodes, then the bottom level has \\(4 = 2^h = (n + 1) / 2\\) nodes.

      "}]} \ No newline at end of file diff --git a/en/stylesheets/extra.css b/en/stylesheets/extra.css index c6f94be40..a5c5392b6 100644 --- a/en/stylesheets/extra.css +++ b/en/stylesheets/extra.css @@ -567,4 +567,4 @@ a:hover .text-button span { left: 50%; transform: translate(-50%, -50%); } -/*! update cache: 20260104155751 */ +/*! update cache: 20260104161450 */ diff --git a/ja/assets/javascripts/bundle.79ae519e.min.js b/ja/assets/javascripts/bundle.79ae519e.min.js index 13db867ae..3cafe9eb8 100644 --- a/ja/assets/javascripts/bundle.79ae519e.min.js +++ b/ja/assets/javascripts/bundle.79ae519e.min.js @@ -14,4 +14,4 @@ `):"",this.name="UnsubscriptionError",this.errors=r}});function Ze(e,t){if(e){var r=e.indexOf(t);0<=r&&e.splice(r,1)}}var qe=(function(){function e(t){this.initialTeardown=t,this.closed=!1,this._parentage=null,this._finalizers=null}return e.prototype.unsubscribe=function(){var t,r,o,n,i;if(!this.closed){this.closed=!0;var s=this._parentage;if(s)if(this._parentage=null,Array.isArray(s))try{for(var a=Oe(s),c=a.next();!c.done;c=a.next()){var p=c.value;p.remove(this)}}catch(S){t={error:S}}finally{try{c&&!c.done&&(r=a.return)&&r.call(a)}finally{if(t)throw t.error}}else s.remove(this);var l=this.initialTeardown;if(I(l))try{l()}catch(S){i=S instanceof Jt?S.errors:[S]}var f=this._finalizers;if(f){this._finalizers=null;try{for(var u=Oe(f),d=u.next();!d.done;d=u.next()){var v=d.value;try{So(v)}catch(S){i=i!=null?i:[],S instanceof Jt?i=B(B([],K(i)),K(S.errors)):i.push(S)}}}catch(S){o={error:S}}finally{try{d&&!d.done&&(n=u.return)&&n.call(u)}finally{if(o)throw o.error}}}if(i)throw new Jt(i)}},e.prototype.add=function(t){var r;if(t&&t!==this)if(this.closed)So(t);else{if(t instanceof e){if(t.closed||t._hasParent(this))return;t._addParent(this)}(this._finalizers=(r=this._finalizers)!==null&&r!==void 0?r:[]).push(t)}},e.prototype._hasParent=function(t){var r=this._parentage;return r===t||Array.isArray(r)&&r.includes(t)},e.prototype._addParent=function(t){var r=this._parentage;this._parentage=Array.isArray(r)?(r.push(t),r):r?[r,t]:t},e.prototype._removeParent=function(t){var r=this._parentage;r===t?this._parentage=null:Array.isArray(r)&&Ze(r,t)},e.prototype.remove=function(t){var r=this._finalizers;r&&Ze(r,t),t instanceof e&&t._removeParent(this)},e.EMPTY=(function(){var t=new e;return t.closed=!0,t})(),e})();var $r=qe.EMPTY;function Xt(e){return e instanceof qe||e&&"closed"in e&&I(e.remove)&&I(e.add)&&I(e.unsubscribe)}function So(e){I(e)?e():e.unsubscribe()}var De={onUnhandledError:null,onStoppedNotification:null,Promise:void 0,useDeprecatedSynchronousErrorHandling:!1,useDeprecatedNextContext:!1};var xt={setTimeout:function(e,t){for(var r=[],o=2;o0},enumerable:!1,configurable:!0}),t.prototype._trySubscribe=function(r){return this._throwIfClosed(),e.prototype._trySubscribe.call(this,r)},t.prototype._subscribe=function(r){return this._throwIfClosed(),this._checkFinalizedStatuses(r),this._innerSubscribe(r)},t.prototype._innerSubscribe=function(r){var o=this,n=this,i=n.hasError,s=n.isStopped,a=n.observers;return i||s?$r:(this.currentObservers=null,a.push(r),new qe(function(){o.currentObservers=null,Ze(a,r)}))},t.prototype._checkFinalizedStatuses=function(r){var o=this,n=o.hasError,i=o.thrownError,s=o.isStopped;n?r.error(i):s&&r.complete()},t.prototype.asObservable=function(){var r=new F;return r.source=this,r},t.create=function(r,o){return new Ho(r,o)},t})(F);var Ho=(function(e){ie(t,e);function t(r,o){var n=e.call(this)||this;return n.destination=r,n.source=o,n}return t.prototype.next=function(r){var o,n;(n=(o=this.destination)===null||o===void 0?void 0:o.next)===null||n===void 0||n.call(o,r)},t.prototype.error=function(r){var o,n;(n=(o=this.destination)===null||o===void 0?void 0:o.error)===null||n===void 0||n.call(o,r)},t.prototype.complete=function(){var r,o;(o=(r=this.destination)===null||r===void 0?void 0:r.complete)===null||o===void 0||o.call(r)},t.prototype._subscribe=function(r){var o,n;return(n=(o=this.source)===null||o===void 0?void 0:o.subscribe(r))!==null&&n!==void 0?n:$r},t})(T);var jr=(function(e){ie(t,e);function t(r){var o=e.call(this)||this;return o._value=r,o}return Object.defineProperty(t.prototype,"value",{get:function(){return this.getValue()},enumerable:!1,configurable:!0}),t.prototype._subscribe=function(r){var o=e.prototype._subscribe.call(this,r);return!o.closed&&r.next(this._value),o},t.prototype.getValue=function(){var r=this,o=r.hasError,n=r.thrownError,i=r._value;if(o)throw n;return this._throwIfClosed(),i},t.prototype.next=function(r){e.prototype.next.call(this,this._value=r)},t})(T);var Rt={now:function(){return(Rt.delegate||Date).now()},delegate:void 0};var It=(function(e){ie(t,e);function t(r,o,n){r===void 0&&(r=1/0),o===void 0&&(o=1/0),n===void 0&&(n=Rt);var i=e.call(this)||this;return i._bufferSize=r,i._windowTime=o,i._timestampProvider=n,i._buffer=[],i._infiniteTimeWindow=!0,i._infiniteTimeWindow=o===1/0,i._bufferSize=Math.max(1,r),i._windowTime=Math.max(1,o),i}return t.prototype.next=function(r){var o=this,n=o.isStopped,i=o._buffer,s=o._infiniteTimeWindow,a=o._timestampProvider,c=o._windowTime;n||(i.push(r),!s&&i.push(a.now()+c)),this._trimBuffer(),e.prototype.next.call(this,r)},t.prototype._subscribe=function(r){this._throwIfClosed(),this._trimBuffer();for(var o=this._innerSubscribe(r),n=this,i=n._infiniteTimeWindow,s=n._buffer,a=s.slice(),c=0;c0?e.prototype.schedule.call(this,r,o):(this.delay=o,this.state=r,this.scheduler.flush(this),this)},t.prototype.execute=function(r,o){return o>0||this.closed?e.prototype.execute.call(this,r,o):this._execute(r,o)},t.prototype.requestAsyncId=function(r,o,n){return n===void 0&&(n=0),n!=null&&n>0||n==null&&this.delay>0?e.prototype.requestAsyncId.call(this,r,o,n):(r.flush(this),0)},t})(St);var Ro=(function(e){ie(t,e);function t(){return e!==null&&e.apply(this,arguments)||this}return t})(Ot);var Dr=new Ro(Po);var Io=(function(e){ie(t,e);function t(r,o){var n=e.call(this,r,o)||this;return n.scheduler=r,n.work=o,n}return t.prototype.requestAsyncId=function(r,o,n){return n===void 0&&(n=0),n!==null&&n>0?e.prototype.requestAsyncId.call(this,r,o,n):(r.actions.push(this),r._scheduled||(r._scheduled=Tt.requestAnimationFrame(function(){return r.flush(void 0)})))},t.prototype.recycleAsyncId=function(r,o,n){var i;if(n===void 0&&(n=0),n!=null?n>0:this.delay>0)return e.prototype.recycleAsyncId.call(this,r,o,n);var s=r.actions;o!=null&&o===r._scheduled&&((i=s[s.length-1])===null||i===void 0?void 0:i.id)!==o&&(Tt.cancelAnimationFrame(o),r._scheduled=void 0)},t})(St);var Fo=(function(e){ie(t,e);function t(){return e!==null&&e.apply(this,arguments)||this}return t.prototype.flush=function(r){this._active=!0;var o;r?o=r.id:(o=this._scheduled,this._scheduled=void 0);var n=this.actions,i;r=r||n.shift();do if(i=r.execute(r.state,r.delay))break;while((r=n[0])&&r.id===o&&n.shift());if(this._active=!1,i){for(;(r=n[0])&&r.id===o&&n.shift();)r.unsubscribe();throw i}},t})(Ot);var ye=new Fo(Io);var y=new F(function(e){return e.complete()});function tr(e){return e&&I(e.schedule)}function Vr(e){return e[e.length-1]}function pt(e){return I(Vr(e))?e.pop():void 0}function Fe(e){return tr(Vr(e))?e.pop():void 0}function rr(e,t){return typeof Vr(e)=="number"?e.pop():t}var Lt=(function(e){return e&&typeof e.length=="number"&&typeof e!="function"});function or(e){return I(e==null?void 0:e.then)}function nr(e){return I(e[wt])}function ir(e){return Symbol.asyncIterator&&I(e==null?void 0:e[Symbol.asyncIterator])}function ar(e){return new TypeError("You provided "+(e!==null&&typeof e=="object"?"an invalid object":"'"+e+"'")+" where a stream was expected. You can provide an Observable, Promise, ReadableStream, Array, AsyncIterable, or Iterable.")}function fa(){return typeof Symbol!="function"||!Symbol.iterator?"@@iterator":Symbol.iterator}var sr=fa();function cr(e){return I(e==null?void 0:e[sr])}function pr(e){return wo(this,arguments,function(){var r,o,n,i;return Gt(this,function(s){switch(s.label){case 0:r=e.getReader(),s.label=1;case 1:s.trys.push([1,,9,10]),s.label=2;case 2:return[4,dt(r.read())];case 3:return o=s.sent(),n=o.value,i=o.done,i?[4,dt(void 0)]:[3,5];case 4:return[2,s.sent()];case 5:return[4,dt(n)];case 6:return[4,s.sent()];case 7:return s.sent(),[3,2];case 8:return[3,10];case 9:return r.releaseLock(),[7];case 10:return[2]}})})}function lr(e){return I(e==null?void 0:e.getReader)}function U(e){if(e instanceof F)return e;if(e!=null){if(nr(e))return ua(e);if(Lt(e))return da(e);if(or(e))return ha(e);if(ir(e))return jo(e);if(cr(e))return ba(e);if(lr(e))return va(e)}throw ar(e)}function ua(e){return new F(function(t){var r=e[wt]();if(I(r.subscribe))return r.subscribe(t);throw new TypeError("Provided object does not correctly implement Symbol.observable")})}function da(e){return new F(function(t){for(var r=0;r=2;return function(o){return o.pipe(e?g(function(n,i){return e(n,i,o)}):be,Ee(1),r?Qe(t):tn(function(){return new fr}))}}function Yr(e){return e<=0?function(){return y}:E(function(t,r){var o=[];t.subscribe(w(r,function(n){o.push(n),e=2,!0))}function le(e){e===void 0&&(e={});var t=e.connector,r=t===void 0?function(){return new T}:t,o=e.resetOnError,n=o===void 0?!0:o,i=e.resetOnComplete,s=i===void 0?!0:i,a=e.resetOnRefCountZero,c=a===void 0?!0:a;return function(p){var l,f,u,d=0,v=!1,S=!1,X=function(){f==null||f.unsubscribe(),f=void 0},re=function(){X(),l=u=void 0,v=S=!1},ee=function(){var k=l;re(),k==null||k.unsubscribe()};return E(function(k,ut){d++,!S&&!v&&X();var je=u=u!=null?u:r();ut.add(function(){d--,d===0&&!S&&!v&&(f=Br(ee,c))}),je.subscribe(ut),!l&&d>0&&(l=new bt({next:function(R){return je.next(R)},error:function(R){S=!0,X(),f=Br(re,n,R),je.error(R)},complete:function(){v=!0,X(),f=Br(re,s),je.complete()}}),U(k).subscribe(l))})(p)}}function Br(e,t){for(var r=[],o=2;oe.next(document)),e}function M(e,t=document){return Array.from(t.querySelectorAll(e))}function j(e,t=document){let r=ue(e,t);if(typeof r=="undefined")throw new ReferenceError(`Missing element: expected "${e}" to be present`);return r}function ue(e,t=document){return t.querySelector(e)||void 0}function Ne(){var e,t,r,o;return(o=(r=(t=(e=document.activeElement)==null?void 0:e.shadowRoot)==null?void 0:t.activeElement)!=null?r:document.activeElement)!=null?o:void 0}var Ra=L(h(document.body,"focusin"),h(document.body,"focusout")).pipe(Ae(1),Q(void 0),m(()=>Ne()||document.body),Z(1));function Ye(e){return Ra.pipe(m(t=>e.contains(t)),Y())}function it(e,t){return H(()=>L(h(e,"mouseenter").pipe(m(()=>!0)),h(e,"mouseleave").pipe(m(()=>!1))).pipe(t?jt(r=>He(+!r*t)):be,Q(e.matches(":hover"))))}function sn(e,t){if(typeof t=="string"||typeof t=="number")e.innerHTML+=t.toString();else if(t instanceof Node)e.appendChild(t);else if(Array.isArray(t))for(let r of t)sn(e,r)}function x(e,t,...r){let o=document.createElement(e);if(t)for(let n of Object.keys(t))typeof t[n]!="undefined"&&(typeof t[n]!="boolean"?o.setAttribute(n,t[n]):o.setAttribute(n,""));for(let n of r)sn(o,n);return o}function br(e){if(e>999){let t=+((e-950)%1e3>99);return`${((e+1e-6)/1e3).toFixed(t)}k`}else return e.toString()}function _t(e){let t=x("script",{src:e});return H(()=>(document.head.appendChild(t),L(h(t,"load"),h(t,"error").pipe(b(()=>Nr(()=>new ReferenceError(`Invalid script: ${e}`))))).pipe(m(()=>{}),A(()=>document.head.removeChild(t)),Ee(1))))}var cn=new T,Ia=H(()=>typeof ResizeObserver=="undefined"?_t("https://unpkg.com/resize-observer-polyfill"):$(void 0)).pipe(m(()=>new ResizeObserver(e=>e.forEach(t=>cn.next(t)))),b(e=>L(tt,$(e)).pipe(A(()=>e.disconnect()))),Z(1));function de(e){return{width:e.offsetWidth,height:e.offsetHeight}}function Le(e){let t=e;for(;t.clientWidth===0&&t.parentElement;)t=t.parentElement;return Ia.pipe(O(r=>r.observe(t)),b(r=>cn.pipe(g(o=>o.target===t),A(()=>r.unobserve(t)))),m(()=>de(e)),Q(de(e)))}function At(e){return{width:e.scrollWidth,height:e.scrollHeight}}function vr(e){let t=e.parentElement;for(;t&&(e.scrollWidth<=t.scrollWidth&&e.scrollHeight<=t.scrollHeight);)t=(e=t).parentElement;return t?e:void 0}function pn(e){let t=[],r=e.parentElement;for(;r;)(e.clientWidth>r.clientWidth||e.clientHeight>r.clientHeight)&&t.push(r),r=(e=r).parentElement;return t.length===0&&t.push(document.documentElement),t}function Be(e){return{x:e.offsetLeft,y:e.offsetTop}}function ln(e){let t=e.getBoundingClientRect();return{x:t.x+window.scrollX,y:t.y+window.scrollY}}function mn(e){return L(h(window,"load"),h(window,"resize")).pipe($e(0,ye),m(()=>Be(e)),Q(Be(e)))}function gr(e){return{x:e.scrollLeft,y:e.scrollTop}}function Ge(e){return L(h(e,"scroll"),h(window,"scroll"),h(window,"resize")).pipe($e(0,ye),m(()=>gr(e)),Q(gr(e)))}var fn=new T,Fa=H(()=>$(new IntersectionObserver(e=>{for(let t of e)fn.next(t)},{threshold:0}))).pipe(b(e=>L(tt,$(e)).pipe(A(()=>e.disconnect()))),Z(1));function mt(e){return Fa.pipe(O(t=>t.observe(e)),b(t=>fn.pipe(g(({target:r})=>r===e),A(()=>t.unobserve(e)),m(({isIntersecting:r})=>r))))}function un(e,t=16){return Ge(e).pipe(m(({y:r})=>{let o=de(e),n=At(e);return r>=n.height-o.height-t}),Y())}var yr={drawer:j("[data-md-toggle=drawer]"),search:j("[data-md-toggle=search]")};function dn(e){return yr[e].checked}function at(e,t){yr[e].checked!==t&&yr[e].click()}function Je(e){let t=yr[e];return h(t,"change").pipe(m(()=>t.checked),Q(t.checked))}function ja(e,t){switch(e.constructor){case HTMLInputElement:return e.type==="radio"?/^Arrow/.test(t):!0;case HTMLSelectElement:case HTMLTextAreaElement:return!0;default:return e.isContentEditable}}function Ua(){return L(h(window,"compositionstart").pipe(m(()=>!0)),h(window,"compositionend").pipe(m(()=>!1))).pipe(Q(!1))}function hn(){let e=h(window,"keydown").pipe(g(t=>!(t.metaKey||t.ctrlKey)),m(t=>({mode:dn("search")?"search":"global",type:t.key,claim(){t.preventDefault(),t.stopPropagation()}})),g(({mode:t,type:r})=>{if(t==="global"){let o=Ne();if(typeof o!="undefined")return!ja(o,r)}return!0}),le());return Ua().pipe(b(t=>t?y:e))}function we(){return new URL(location.href)}function st(e,t=!1){if(V("navigation.instant")&&!t){let r=x("a",{href:e.href});document.body.appendChild(r),r.click(),r.remove()}else location.href=e.href}function bn(){return new T}function vn(){return location.hash.slice(1)}function gn(e){let t=x("a",{href:e});t.addEventListener("click",r=>r.stopPropagation()),t.click()}function Zr(e){return L(h(window,"hashchange"),e).pipe(m(vn),Q(vn()),g(t=>t.length>0),Z(1))}function yn(e){return Zr(e).pipe(m(t=>ue(`[id="${t}"]`)),g(t=>typeof t!="undefined"))}function Wt(e){let t=matchMedia(e);return ur(r=>t.addListener(()=>r(t.matches))).pipe(Q(t.matches))}function xn(){let e=matchMedia("print");return L(h(window,"beforeprint").pipe(m(()=>!0)),h(window,"afterprint").pipe(m(()=>!1))).pipe(Q(e.matches))}function eo(e,t){return e.pipe(b(r=>r?t():y))}function to(e,t){return new F(r=>{let o=new XMLHttpRequest;return o.open("GET",`${e}`),o.responseType="blob",o.addEventListener("load",()=>{o.status>=200&&o.status<300?(r.next(o.response),r.complete()):r.error(new Error(o.statusText))}),o.addEventListener("error",()=>{r.error(new Error("Network error"))}),o.addEventListener("abort",()=>{r.complete()}),typeof(t==null?void 0:t.progress$)!="undefined"&&(o.addEventListener("progress",n=>{var i;if(n.lengthComputable)t.progress$.next(n.loaded/n.total*100);else{let s=(i=o.getResponseHeader("Content-Length"))!=null?i:0;t.progress$.next(n.loaded/+s*100)}}),t.progress$.next(5)),o.send(),()=>o.abort()})}function ze(e,t){return to(e,t).pipe(b(r=>r.text()),m(r=>JSON.parse(r)),Z(1))}function xr(e,t){let r=new DOMParser;return to(e,t).pipe(b(o=>o.text()),m(o=>r.parseFromString(o,"text/html")),Z(1))}function En(e,t){let r=new DOMParser;return to(e,t).pipe(b(o=>o.text()),m(o=>r.parseFromString(o,"text/xml")),Z(1))}function wn(){return{x:Math.max(0,scrollX),y:Math.max(0,scrollY)}}function Tn(){return L(h(window,"scroll",{passive:!0}),h(window,"resize",{passive:!0})).pipe(m(wn),Q(wn()))}function Sn(){return{width:innerWidth,height:innerHeight}}function On(){return h(window,"resize",{passive:!0}).pipe(m(Sn),Q(Sn()))}function Ln(){return z([Tn(),On()]).pipe(m(([e,t])=>({offset:e,size:t})),Z(1))}function Er(e,{viewport$:t,header$:r}){let o=t.pipe(ne("size")),n=z([o,r]).pipe(m(()=>Be(e)));return z([r,t,n]).pipe(m(([{height:i},{offset:s,size:a},{x:c,y:p}])=>({offset:{x:s.x-c,y:s.y-p+i},size:a})))}function Wa(e){return h(e,"message",t=>t.data)}function Da(e){let t=new T;return t.subscribe(r=>e.postMessage(r)),t}function Mn(e,t=new Worker(e)){let r=Wa(t),o=Da(t),n=new T;n.subscribe(o);let i=o.pipe(oe(),ae(!0));return n.pipe(oe(),Ve(r.pipe(W(i))),le())}var Va=j("#__config"),Ct=JSON.parse(Va.textContent);Ct.base=`${new URL(Ct.base,we())}`;function Te(){return Ct}function V(e){return Ct.features.includes(e)}function Me(e,t){return typeof t!="undefined"?Ct.translations[e].replace("#",t.toString()):Ct.translations[e]}function Ce(e,t=document){return j(`[data-md-component=${e}]`,t)}function me(e,t=document){return M(`[data-md-component=${e}]`,t)}function Na(e){let t=j(".md-typeset > :first-child",e);return h(t,"click",{once:!0}).pipe(m(()=>j(".md-typeset",e)),m(r=>({hash:__md_hash(r.innerHTML)})))}function _n(e){if(!V("announce.dismiss")||!e.childElementCount)return y;if(!e.hidden){let t=j(".md-typeset",e);__md_hash(t.innerHTML)===__md_get("__announce")&&(e.hidden=!0)}return H(()=>{let t=new T;return t.subscribe(({hash:r})=>{e.hidden=!0,__md_set("__announce",r)}),Na(e).pipe(O(r=>t.next(r)),A(()=>t.complete()),m(r=>P({ref:e},r)))})}function za(e,{target$:t}){return t.pipe(m(r=>({hidden:r!==e})))}function An(e,t){let r=new T;return r.subscribe(({hidden:o})=>{e.hidden=o}),za(e,t).pipe(O(o=>r.next(o)),A(()=>r.complete()),m(o=>P({ref:e},o)))}function Dt(e,t){return t==="inline"?x("div",{class:"md-tooltip md-tooltip--inline",id:e,role:"tooltip"},x("div",{class:"md-tooltip__inner md-typeset"})):x("div",{class:"md-tooltip",id:e,role:"tooltip"},x("div",{class:"md-tooltip__inner md-typeset"}))}function wr(...e){return x("div",{class:"md-tooltip2",role:"dialog"},x("div",{class:"md-tooltip2__inner md-typeset"},e))}function Cn(...e){return x("div",{class:"md-tooltip2",role:"tooltip"},x("div",{class:"md-tooltip2__inner md-typeset"},e))}function kn(e,t){if(t=t?`${t}_annotation_${e}`:void 0,t){let r=t?`#${t}`:void 0;return x("aside",{class:"md-annotation",tabIndex:0},Dt(t),x("a",{href:r,class:"md-annotation__index",tabIndex:-1},x("span",{"data-md-annotation-id":e})))}else return x("aside",{class:"md-annotation",tabIndex:0},Dt(t),x("span",{class:"md-annotation__index",tabIndex:-1},x("span",{"data-md-annotation-id":e})))}function Hn(e){return x("button",{class:"md-code__button",title:Me("clipboard.copy"),"data-clipboard-target":`#${e} > code`,"data-md-type":"copy"})}function $n(){return x("button",{class:"md-code__button",title:"Toggle line selection","data-md-type":"select"})}function Pn(){return x("nav",{class:"md-code__nav"})}var In=$t(ro());function oo(e,t){let r=t&2,o=t&1,n=Object.keys(e.terms).filter(c=>!e.terms[c]).reduce((c,p)=>[...c,x("del",null,(0,In.default)(p))," "],[]).slice(0,-1),i=Te(),s=new URL(e.location,i.base);V("search.highlight")&&s.searchParams.set("h",Object.entries(e.terms).filter(([,c])=>c).reduce((c,[p])=>`${c} ${p}`.trim(),""));let{tags:a}=Te();return x("a",{href:`${s}`,class:"md-search-result__link",tabIndex:-1},x("article",{class:"md-search-result__article md-typeset","data-md-score":e.score.toFixed(2)},r>0&&x("div",{class:"md-search-result__icon md-icon"}),r>0&&x("h1",null,e.title),r<=0&&x("h2",null,e.title),o>0&&e.text.length>0&&e.text,e.tags&&x("nav",{class:"md-tags"},e.tags.map(c=>{let p=a?c in a?`md-tag-icon md-tag--${a[c]}`:"md-tag-icon":"";return x("span",{class:`md-tag ${p}`},c)})),o>0&&n.length>0&&x("p",{class:"md-search-result__terms"},Me("search.result.term.missing"),": ",...n)))}function Fn(e){let t=e[0].score,r=[...e],o=Te(),n=r.findIndex(l=>!`${new URL(l.location,o.base)}`.includes("#")),[i]=r.splice(n,1),s=r.findIndex(l=>l.scoreoo(l,1)),...c.length?[x("details",{class:"md-search-result__more"},x("summary",{tabIndex:-1},x("div",null,c.length>0&&c.length===1?Me("search.result.more.one"):Me("search.result.more.other",c.length))),...c.map(l=>oo(l,1)))]:[]];return x("li",{class:"md-search-result__item"},p)}function jn(e){return x("ul",{class:"md-source__facts"},Object.entries(e).map(([t,r])=>x("li",{class:`md-source__fact md-source__fact--${t}`},typeof r=="number"?br(r):r)))}function no(e){let t=`tabbed-control tabbed-control--${e}`;return x("div",{class:t,hidden:!0},x("button",{class:"tabbed-button",tabIndex:-1,"aria-hidden":"true"}))}function Un(e){return x("div",{class:"md-typeset__scrollwrap"},x("div",{class:"md-typeset__table"},e))}function Qa(e){var o;let t=Te(),r=new URL(`../${e.version}/`,t.base);return x("li",{class:"md-version__item"},x("a",{href:`${r}`,class:"md-version__link"},e.title,((o=t.version)==null?void 0:o.alias)&&e.aliases.length>0&&x("span",{class:"md-version__alias"},e.aliases[0])))}function Wn(e,t){var o;let r=Te();return e=e.filter(n=>{var i;return!((i=n.properties)!=null&&i.hidden)}),x("div",{class:"md-version"},x("button",{class:"md-version__current","aria-label":Me("select.version")},t.title,((o=r.version)==null?void 0:o.alias)&&t.aliases.length>0&&x("span",{class:"md-version__alias"},t.aliases[0])),x("ul",{class:"md-version__list"},e.map(Qa)))}var Ya=0;function Ba(e,t=250){let r=z([Ye(e),it(e,t)]).pipe(m(([n,i])=>n||i),Y()),o=H(()=>pn(e)).pipe(J(Ge),gt(1),Pe(r),m(()=>ln(e)));return r.pipe(Re(n=>n),b(()=>z([r,o])),m(([n,i])=>({active:n,offset:i})),le())}function Vt(e,t,r=250){let{content$:o,viewport$:n}=t,i=`__tooltip2_${Ya++}`;return H(()=>{let s=new T,a=new jr(!1);s.pipe(oe(),ae(!1)).subscribe(a);let c=a.pipe(jt(l=>He(+!l*250,Dr)),Y(),b(l=>l?o:y),O(l=>l.id=i),le());z([s.pipe(m(({active:l})=>l)),c.pipe(b(l=>it(l,250)),Q(!1))]).pipe(m(l=>l.some(f=>f))).subscribe(a);let p=a.pipe(g(l=>l),te(c,n),m(([l,f,{size:u}])=>{let d=e.getBoundingClientRect(),v=d.width/2;if(f.role==="tooltip")return{x:v,y:8+d.height};if(d.y>=u.height/2){let{height:S}=de(f);return{x:v,y:-16-S}}else return{x:v,y:16+d.height}}));return z([c,s,p]).subscribe(([l,{offset:f},u])=>{l.style.setProperty("--md-tooltip-host-x",`${f.x}px`),l.style.setProperty("--md-tooltip-host-y",`${f.y}px`),l.style.setProperty("--md-tooltip-x",`${u.x}px`),l.style.setProperty("--md-tooltip-y",`${u.y}px`),l.classList.toggle("md-tooltip2--top",u.y<0),l.classList.toggle("md-tooltip2--bottom",u.y>=0)}),a.pipe(g(l=>l),te(c,(l,f)=>f),g(l=>l.role==="tooltip")).subscribe(l=>{let f=de(j(":scope > *",l));l.style.setProperty("--md-tooltip-width",`${f.width}px`),l.style.setProperty("--md-tooltip-tail","0px")}),a.pipe(Y(),xe(ye),te(c)).subscribe(([l,f])=>{f.classList.toggle("md-tooltip2--active",l)}),z([a.pipe(g(l=>l)),c]).subscribe(([l,f])=>{f.role==="dialog"?(e.setAttribute("aria-controls",i),e.setAttribute("aria-haspopup","dialog")):e.setAttribute("aria-describedby",i)}),a.pipe(g(l=>!l)).subscribe(()=>{e.removeAttribute("aria-controls"),e.removeAttribute("aria-describedby"),e.removeAttribute("aria-haspopup")}),Ba(e,r).pipe(O(l=>s.next(l)),A(()=>s.complete()),m(l=>P({ref:e},l)))})}function Xe(e,{viewport$:t},r=document.body){return Vt(e,{content$:new F(o=>{let n=e.title,i=Cn(n);return o.next(i),e.removeAttribute("title"),r.append(i),()=>{i.remove(),e.setAttribute("title",n)}}),viewport$:t},0)}function Ga(e,t){let r=H(()=>z([mn(e),Ge(t)])).pipe(m(([{x:o,y:n},i])=>{let{width:s,height:a}=de(e);return{x:o-i.x+s/2,y:n-i.y+a/2}}));return Ye(e).pipe(b(o=>r.pipe(m(n=>({active:o,offset:n})),Ee(+!o||1/0))))}function Dn(e,t,{target$:r}){let[o,n]=Array.from(e.children);return H(()=>{let i=new T,s=i.pipe(oe(),ae(!0));return i.subscribe({next({offset:a}){e.style.setProperty("--md-tooltip-x",`${a.x}px`),e.style.setProperty("--md-tooltip-y",`${a.y}px`)},complete(){e.style.removeProperty("--md-tooltip-x"),e.style.removeProperty("--md-tooltip-y")}}),mt(e).pipe(W(s)).subscribe(a=>{e.toggleAttribute("data-md-visible",a)}),L(i.pipe(g(({active:a})=>a)),i.pipe(Ae(250),g(({active:a})=>!a))).subscribe({next({active:a}){a?e.prepend(o):o.remove()},complete(){e.prepend(o)}}),i.pipe($e(16,ye)).subscribe(({active:a})=>{o.classList.toggle("md-tooltip--active",a)}),i.pipe(gt(125,ye),g(()=>!!e.offsetParent),m(()=>e.offsetParent.getBoundingClientRect()),m(({x:a})=>a)).subscribe({next(a){a?e.style.setProperty("--md-tooltip-0",`${-a}px`):e.style.removeProperty("--md-tooltip-0")},complete(){e.style.removeProperty("--md-tooltip-0")}}),h(n,"click").pipe(W(s),g(a=>!(a.metaKey||a.ctrlKey))).subscribe(a=>{a.stopPropagation(),a.preventDefault()}),h(n,"mousedown").pipe(W(s),te(i)).subscribe(([a,{active:c}])=>{var p;if(a.button!==0||a.metaKey||a.ctrlKey)a.preventDefault();else if(c){a.preventDefault();let l=e.parentElement.closest(".md-annotation");l instanceof HTMLElement?l.focus():(p=Ne())==null||p.blur()}}),r.pipe(W(s),g(a=>a===o),nt(125)).subscribe(()=>e.focus()),Ga(e,t).pipe(O(a=>i.next(a)),A(()=>i.complete()),m(a=>P({ref:e},a)))})}function Ja(e){let t=Te();if(e.tagName!=="CODE")return[e];let r=[".c",".c1",".cm"];if(t.annotate&&typeof t.annotate=="object"){let o=e.closest("[class|=language]");if(o)for(let n of Array.from(o.classList)){if(!n.startsWith("language-"))continue;let[,i]=n.split("-");i in t.annotate&&r.push(...t.annotate[i])}}return M(r.join(", "),e)}function Xa(e){let t=[];for(let r of Ja(e)){let o=[],n=document.createNodeIterator(r,NodeFilter.SHOW_TEXT);for(let i=n.nextNode();i;i=n.nextNode())o.push(i);for(let i of o){let s;for(;s=/(\(\d+\))(!)?/.exec(i.textContent);){let[,a,c]=s;if(typeof c=="undefined"){let p=i.splitText(s.index);i=p.splitText(a.length),t.push(p)}else{i.textContent=a,t.push(i);break}}}}return t}function Vn(e,t){t.append(...Array.from(e.childNodes))}function Tr(e,t,{target$:r,print$:o}){let n=t.closest("[id]"),i=n==null?void 0:n.id,s=new Map;for(let a of Xa(t)){let[,c]=a.textContent.match(/\((\d+)\)/);ue(`:scope > li:nth-child(${c})`,e)&&(s.set(c,kn(c,i)),a.replaceWith(s.get(c)))}return s.size===0?y:H(()=>{let a=new T,c=a.pipe(oe(),ae(!0)),p=[];for(let[l,f]of s)p.push([j(".md-typeset",f),j(`:scope > li:nth-child(${l})`,e)]);return o.pipe(W(c)).subscribe(l=>{e.hidden=!l,e.classList.toggle("md-annotation-list",l);for(let[f,u]of p)l?Vn(f,u):Vn(u,f)}),L(...[...s].map(([,l])=>Dn(l,t,{target$:r}))).pipe(A(()=>a.complete()),le())})}function Nn(e){if(e.nextElementSibling){let t=e.nextElementSibling;if(t.tagName==="OL")return t;if(t.tagName==="P"&&!t.children.length)return Nn(t)}}function zn(e,t){return H(()=>{let r=Nn(e);return typeof r!="undefined"?Tr(r,e,t):y})}var Kn=$t(ao());var Za=0,qn=L(h(window,"keydown").pipe(m(()=>!0)),L(h(window,"keyup"),h(window,"contextmenu")).pipe(m(()=>!1))).pipe(Q(!1),Z(1));function Qn(e){if(e.nextElementSibling){let t=e.nextElementSibling;if(t.tagName==="OL")return t;if(t.tagName==="P"&&!t.children.length)return Qn(t)}}function es(e){return Le(e).pipe(m(({width:t})=>({scrollable:At(e).width>t})),ne("scrollable"))}function Yn(e,t){let{matches:r}=matchMedia("(hover)"),o=H(()=>{let n=new T,i=n.pipe(Yr(1));n.subscribe(({scrollable:d})=>{d&&r?e.setAttribute("tabindex","0"):e.removeAttribute("tabindex")});let s=[],a=e.closest("pre"),c=a.closest("[id]"),p=c?c.id:Za++;a.id=`__code_${p}`;let l=[],f=e.closest(".highlight");if(f instanceof HTMLElement){let d=Qn(f);if(typeof d!="undefined"&&(f.classList.contains("annotate")||V("content.code.annotate"))){let v=Tr(d,e,t);l.push(Le(f).pipe(W(i),m(({width:S,height:X})=>S&&X),Y(),b(S=>S?v:y)))}}let u=M(":scope > span[id]",e);if(u.length&&(e.classList.add("md-code__content"),e.closest(".select")||V("content.code.select")&&!e.closest(".no-select"))){let d=+u[0].id.split("-").pop(),v=$n();s.push(v),V("content.tooltips")&&l.push(Xe(v,{viewport$}));let S=h(v,"click").pipe(Ut(R=>!R,!1),O(()=>v.blur()),le());S.subscribe(R=>{v.classList.toggle("md-code__button--active",R)});let X=fe(u).pipe(J(R=>it(R).pipe(m(se=>[R,se]))));S.pipe(b(R=>R?X:y)).subscribe(([R,se])=>{let ce=ue(".hll.select",R);if(ce&&!se)ce.replaceWith(...Array.from(ce.childNodes));else if(!ce&&se){let he=document.createElement("span");he.className="hll select",he.append(...Array.from(R.childNodes).slice(1)),R.append(he)}});let re=fe(u).pipe(J(R=>h(R,"mousedown").pipe(O(se=>se.preventDefault()),m(()=>R)))),ee=S.pipe(b(R=>R?re:y),te(qn),m(([R,se])=>{var he;let ce=u.indexOf(R)+d;if(se===!1)return[ce,ce];{let Se=M(".hll",e).map(Ue=>u.indexOf(Ue.parentElement)+d);return(he=window.getSelection())==null||he.removeAllRanges(),[Math.min(ce,...Se),Math.max(ce,...Se)]}})),k=Zr(y).pipe(g(R=>R.startsWith(`__codelineno-${p}-`)));k.subscribe(R=>{let[,,se]=R.split("-"),ce=se.split(":").map(Se=>+Se-d+1);ce.length===1&&ce.push(ce[0]);for(let Se of M(".hll:not(.select)",e))Se.replaceWith(...Array.from(Se.childNodes));let he=u.slice(ce[0]-1,ce[1]);for(let Se of he){let Ue=document.createElement("span");Ue.className="hll",Ue.append(...Array.from(Se.childNodes).slice(1)),Se.append(Ue)}}),k.pipe(Ee(1),xe(pe)).subscribe(R=>{if(R.includes(":")){let se=document.getElementById(R.split(":")[0]);se&&setTimeout(()=>{let ce=se,he=-64;for(;ce!==document.body;)he+=ce.offsetTop,ce=ce.offsetParent;window.scrollTo({top:he})},1)}});let je=fe(M('a[href^="#__codelineno"]',f)).pipe(J(R=>h(R,"click").pipe(O(se=>se.preventDefault()),m(()=>R)))).pipe(W(i),te(qn),m(([R,se])=>{let he=+j(`[id="${R.hash.slice(1)}"]`).parentElement.id.split("-").pop();if(se===!1)return[he,he];{let Se=M(".hll",e).map(Ue=>+Ue.parentElement.id.split("-").pop());return[Math.min(he,...Se),Math.max(he,...Se)]}}));L(ee,je).subscribe(R=>{let se=`#__codelineno-${p}-`;R[0]===R[1]?se+=R[0]:se+=`${R[0]}:${R[1]}`,history.replaceState({},"",se),window.dispatchEvent(new HashChangeEvent("hashchange",{newURL:window.location.origin+window.location.pathname+se,oldURL:window.location.href}))})}if(Kn.default.isSupported()&&(e.closest(".copy")||V("content.code.copy")&&!e.closest(".no-copy"))){let d=Hn(a.id);s.push(d),V("content.tooltips")&&l.push(Xe(d,{viewport$}))}if(s.length){let d=Pn();d.append(...s),a.insertBefore(d,e)}return es(e).pipe(O(d=>n.next(d)),A(()=>n.complete()),m(d=>P({ref:e},d)),Ve(L(...l).pipe(W(i))))});return V("content.lazy")?mt(e).pipe(g(n=>n),Ee(1),b(()=>o)):o}function ts(e,{target$:t,print$:r}){let o=!0;return L(t.pipe(m(n=>n.closest("details:not([open])")),g(n=>e===n),m(()=>({action:"open",reveal:!0}))),r.pipe(g(n=>n||!o),O(()=>o=e.open),m(n=>({action:n?"open":"close"}))))}function Bn(e,t){return H(()=>{let r=new T;return r.subscribe(({action:o,reveal:n})=>{e.toggleAttribute("open",o==="open"),n&&e.scrollIntoView()}),ts(e,t).pipe(O(o=>r.next(o)),A(()=>r.complete()),m(o=>P({ref:e},o)))})}var Gn=0;function rs(e){let t=document.createElement("h3");t.innerHTML=e.innerHTML;let r=[t],o=e.nextElementSibling;for(;o&&!(o instanceof HTMLHeadingElement);)r.push(o),o=o.nextElementSibling;return r}function os(e,t){for(let r of M("[href], [src]",e))for(let o of["href","src"]){let n=r.getAttribute(o);if(n&&!/^(?:[a-z]+:)?\/\//i.test(n)){r[o]=new URL(r.getAttribute(o),t).toString();break}}for(let r of M("[name^=__], [for]",e))for(let o of["id","for","name"]){let n=r.getAttribute(o);n&&r.setAttribute(o,`${n}$preview_${Gn}`)}return Gn++,$(e)}function Jn(e,t){let{sitemap$:r}=t;if(!(e instanceof HTMLAnchorElement))return y;if(!(V("navigation.instant.preview")||e.hasAttribute("data-preview")))return y;e.removeAttribute("title");let o=z([Ye(e),it(e)]).pipe(m(([i,s])=>i||s),Y(),g(i=>i));return rt([r,o]).pipe(b(([i])=>{let s=new URL(e.href);return s.search=s.hash="",i.has(`${s}`)?$(s):y}),b(i=>xr(i).pipe(b(s=>os(s,i)))),b(i=>{let s=e.hash?`article [id="${e.hash.slice(1)}"]`:"article h1",a=ue(s,i);return typeof a=="undefined"?y:$(rs(a))})).pipe(b(i=>{let s=new F(a=>{let c=wr(...i);return a.next(c),document.body.append(c),()=>c.remove()});return Vt(e,P({content$:s},t))}))}var Xn=".node circle,.node ellipse,.node path,.node polygon,.node rect{fill:var(--md-mermaid-node-bg-color);stroke:var(--md-mermaid-node-fg-color)}marker{fill:var(--md-mermaid-edge-color)!important}.edgeLabel .label rect{fill:#0000}.flowchartTitleText{fill:var(--md-mermaid-label-fg-color)}.label{color:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}.label foreignObject{line-height:normal;overflow:visible}.label div .edgeLabel{color:var(--md-mermaid-label-fg-color)}.edgeLabel,.edgeLabel p,.label div .edgeLabel{background-color:var(--md-mermaid-label-bg-color)}.edgeLabel,.edgeLabel p{fill:var(--md-mermaid-label-bg-color);color:var(--md-mermaid-edge-color)}.edgePath .path,.flowchart-link{stroke:var(--md-mermaid-edge-color)}.edgePath .arrowheadPath{fill:var(--md-mermaid-edge-color);stroke:none}.cluster rect{fill:var(--md-default-fg-color--lightest);stroke:var(--md-default-fg-color--lighter)}.cluster span{color:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}g #flowchart-circleEnd,g #flowchart-circleStart,g #flowchart-crossEnd,g #flowchart-crossStart,g #flowchart-pointEnd,g #flowchart-pointStart{stroke:none}.classDiagramTitleText{fill:var(--md-mermaid-label-fg-color)}g.classGroup line,g.classGroup rect{fill:var(--md-mermaid-node-bg-color);stroke:var(--md-mermaid-node-fg-color)}g.classGroup text{fill:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}.classLabel .box{fill:var(--md-mermaid-label-bg-color);background-color:var(--md-mermaid-label-bg-color);opacity:1}.classLabel .label{fill:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}.node .divider{stroke:var(--md-mermaid-node-fg-color)}.relation{stroke:var(--md-mermaid-edge-color)}.cardinality{fill:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}.cardinality text{fill:inherit!important}defs marker.marker.composition.class path,defs marker.marker.dependency.class path,defs marker.marker.extension.class path{fill:var(--md-mermaid-edge-color)!important;stroke:var(--md-mermaid-edge-color)!important}defs marker.marker.aggregation.class path{fill:var(--md-mermaid-label-bg-color)!important;stroke:var(--md-mermaid-edge-color)!important}.statediagramTitleText{fill:var(--md-mermaid-label-fg-color)}g.stateGroup rect{fill:var(--md-mermaid-node-bg-color);stroke:var(--md-mermaid-node-fg-color)}g.stateGroup .state-title{fill:var(--md-mermaid-label-fg-color)!important;font-family:var(--md-mermaid-font-family)}g.stateGroup .composit{fill:var(--md-mermaid-label-bg-color)}.nodeLabel,.nodeLabel p{color:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}a .nodeLabel{text-decoration:underline}.node circle.state-end,.node circle.state-start,.start-state{fill:var(--md-mermaid-edge-color);stroke:none}.end-state-inner,.end-state-outer{fill:var(--md-mermaid-edge-color)}.end-state-inner,.node circle.state-end{stroke:var(--md-mermaid-label-bg-color)}.transition{stroke:var(--md-mermaid-edge-color)}[id^=state-fork] rect,[id^=state-join] rect{fill:var(--md-mermaid-edge-color)!important;stroke:none!important}.statediagram-cluster.statediagram-cluster .inner{fill:var(--md-default-bg-color)}.statediagram-cluster rect{fill:var(--md-mermaid-node-bg-color);stroke:var(--md-mermaid-node-fg-color)}.statediagram-state rect.divider{fill:var(--md-default-fg-color--lightest);stroke:var(--md-default-fg-color--lighter)}defs #statediagram-barbEnd{stroke:var(--md-mermaid-edge-color)}[id^=entity] path,[id^=entity] rect{fill:var(--md-default-bg-color)}.relationshipLine{stroke:var(--md-mermaid-edge-color)}defs .marker.oneOrMore.er *,defs .marker.onlyOne.er *,defs .marker.zeroOrMore.er *,defs .marker.zeroOrOne.er *{stroke:var(--md-mermaid-edge-color)!important}text:not([class]):last-child{fill:var(--md-mermaid-label-fg-color)}.actor{fill:var(--md-mermaid-sequence-actor-bg-color);stroke:var(--md-mermaid-sequence-actor-border-color)}text.actor>tspan{fill:var(--md-mermaid-sequence-actor-fg-color);font-family:var(--md-mermaid-font-family)}line{stroke:var(--md-mermaid-sequence-actor-line-color)}.actor-man circle,.actor-man line{fill:var(--md-mermaid-sequence-actorman-bg-color);stroke:var(--md-mermaid-sequence-actorman-line-color)}.messageLine0,.messageLine1{stroke:var(--md-mermaid-sequence-message-line-color)}.note{fill:var(--md-mermaid-sequence-note-bg-color);stroke:var(--md-mermaid-sequence-note-border-color)}.loopText,.loopText>tspan,.messageText,.noteText>tspan{stroke:none;font-family:var(--md-mermaid-font-family)!important}.messageText{fill:var(--md-mermaid-sequence-message-fg-color)}.loopText,.loopText>tspan{fill:var(--md-mermaid-sequence-loop-fg-color)}.noteText>tspan{fill:var(--md-mermaid-sequence-note-fg-color)}#arrowhead path{fill:var(--md-mermaid-sequence-message-line-color);stroke:none}.loopLine{fill:var(--md-mermaid-sequence-loop-bg-color);stroke:var(--md-mermaid-sequence-loop-border-color)}.labelBox{fill:var(--md-mermaid-sequence-label-bg-color);stroke:none}.labelText,.labelText>span{fill:var(--md-mermaid-sequence-label-fg-color);font-family:var(--md-mermaid-font-family)}.sequenceNumber{fill:var(--md-mermaid-sequence-number-fg-color)}rect.rect{fill:var(--md-mermaid-sequence-box-bg-color);stroke:none}rect.rect+text.text{fill:var(--md-mermaid-sequence-box-fg-color)}defs #sequencenumber{fill:var(--md-mermaid-sequence-number-bg-color)!important}";var so,is=0;function as(){return typeof mermaid=="undefined"||mermaid instanceof Element?_t("https://unpkg.com/mermaid@11/dist/mermaid.min.js"):$(void 0)}function Zn(e){return e.classList.remove("mermaid"),so||(so=as().pipe(O(()=>mermaid.initialize({startOnLoad:!1,themeCSS:Xn,sequence:{actorFontSize:"16px",messageFontSize:"16px",noteFontSize:"16px"}})),m(()=>{}),Z(1))),so.subscribe(()=>go(null,null,function*(){e.classList.add("mermaid");let t=`__mermaid_${is++}`,r=x("div",{class:"mermaid"}),o=e.textContent,{svg:n,fn:i}=yield mermaid.render(t,o),s=r.attachShadow({mode:"closed"});s.innerHTML=n,e.replaceWith(r),i==null||i(s)})),so.pipe(m(()=>({ref:e})))}var ei=x("table");function ti(e){return e.replaceWith(ei),ei.replaceWith(Un(e)),$({ref:e})}function ss(e){let t=e.find(r=>r.checked)||e[0];return L(...e.map(r=>h(r,"change").pipe(m(()=>j(`label[for="${r.id}"]`))))).pipe(Q(j(`label[for="${t.id}"]`)),m(r=>({active:r})))}function ri(e,{viewport$:t,target$:r}){let o=j(".tabbed-labels",e),n=M(":scope > input",e),i=no("prev");e.append(i);let s=no("next");return e.append(s),H(()=>{let a=new T,c=a.pipe(oe(),ae(!0));z([a,Le(e),mt(e)]).pipe(W(c),$e(1,ye)).subscribe({next([{active:p},l]){let f=Be(p),{width:u}=de(p);e.style.setProperty("--md-indicator-x",`${f.x}px`),e.style.setProperty("--md-indicator-width",`${u}px`);let d=gr(o);(f.xd.x+l.width)&&o.scrollTo({left:Math.max(0,f.x-16),behavior:"smooth"})},complete(){e.style.removeProperty("--md-indicator-x"),e.style.removeProperty("--md-indicator-width")}}),z([Ge(o),Le(o)]).pipe(W(c)).subscribe(([p,l])=>{let f=At(o);i.hidden=p.x<16,s.hidden=p.x>f.width-l.width-16}),L(h(i,"click").pipe(m(()=>-1)),h(s,"click").pipe(m(()=>1))).pipe(W(c)).subscribe(p=>{let{width:l}=de(o);o.scrollBy({left:l*p,behavior:"smooth"})}),r.pipe(W(c),g(p=>n.includes(p))).subscribe(p=>p.click()),o.classList.add("tabbed-labels--linked");for(let p of n){let l=j(`label[for="${p.id}"]`);l.replaceChildren(x("a",{href:`#${l.htmlFor}`,tabIndex:-1},...Array.from(l.childNodes))),h(l.firstElementChild,"click").pipe(W(c),g(f=>!(f.metaKey||f.ctrlKey)),O(f=>{f.preventDefault(),f.stopPropagation()})).subscribe(()=>{history.replaceState({},"",`#${l.htmlFor}`),l.click()})}return V("content.tabs.link")&&a.pipe(Ie(1),te(t)).subscribe(([{active:p},{offset:l}])=>{let f=p.innerText.trim();if(p.hasAttribute("data-md-switching"))p.removeAttribute("data-md-switching");else{let u=e.offsetTop-l.y;for(let v of M("[data-tabs]"))for(let S of M(":scope > input",v)){let X=j(`label[for="${S.id}"]`);if(X!==p&&X.innerText.trim()===f){X.setAttribute("data-md-switching",""),S.click();break}}window.scrollTo({top:e.offsetTop-u});let d=__md_get("__tabs")||[];__md_set("__tabs",[...new Set([f,...d])])}}),a.pipe(W(c)).subscribe(()=>{for(let p of M("audio, video",e))p.offsetWidth&&p.autoplay?p.play().catch(()=>{}):p.pause()}),ss(n).pipe(O(p=>a.next(p)),A(()=>a.complete()),m(p=>P({ref:e},p)))}).pipe(et(pe))}function oi(e,t){let{viewport$:r,target$:o,print$:n}=t;return L(...M(".annotate:not(.highlight)",e).map(i=>zn(i,{target$:o,print$:n})),...M("pre:not(.mermaid) > code",e).map(i=>Yn(i,{target$:o,print$:n})),...M("a",e).map(i=>Jn(i,t)),...M("pre.mermaid",e).map(i=>Zn(i)),...M("table:not([class])",e).map(i=>ti(i)),...M("details",e).map(i=>Bn(i,{target$:o,print$:n})),...M("[data-tabs]",e).map(i=>ri(i,{viewport$:r,target$:o})),...M("[title]:not([data-preview])",e).filter(()=>V("content.tooltips")).map(i=>Xe(i,{viewport$:r})),...M(".footnote-ref",e).filter(()=>V("content.footnote.tooltips")).map(i=>Vt(i,{content$:new F(s=>{let a=new URL(i.href).hash.slice(1),c=Array.from(document.getElementById(a).cloneNode(!0).children),p=wr(...c);return s.next(p),document.body.append(p),()=>p.remove()}),viewport$:r})))}function cs(e,{alert$:t}){return t.pipe(b(r=>L($(!0),$(!1).pipe(nt(2e3))).pipe(m(o=>({message:r,active:o})))))}function ni(e,t){let r=j(".md-typeset",e);return H(()=>{let o=new T;return o.subscribe(({message:n,active:i})=>{e.classList.toggle("md-dialog--active",i),r.textContent=n}),cs(e,t).pipe(O(n=>o.next(n)),A(()=>o.complete()),m(n=>P({ref:e},n)))})}var ps=0;function ls(e,t){document.body.append(e);let{width:r}=de(e);e.style.setProperty("--md-tooltip-width",`${r}px`),e.remove();let o=vr(t),n=typeof o!="undefined"?Ge(o):$({x:0,y:0}),i=L(Ye(t),it(t)).pipe(Y());return z([i,n]).pipe(m(([s,a])=>{let{x:c,y:p}=Be(t),l=de(t),f=t.closest("table");return f&&t.parentElement&&(c+=f.offsetLeft+t.parentElement.offsetLeft,p+=f.offsetTop+t.parentElement.offsetTop),{active:s,offset:{x:c-a.x+l.width/2-r/2,y:p-a.y+l.height+8}}}))}function ii(e){let t=e.title;if(!t.length)return y;let r=`__tooltip_${ps++}`,o=Dt(r,"inline"),n=j(".md-typeset",o);return n.innerHTML=t,H(()=>{let i=new T;return i.subscribe({next({offset:s}){o.style.setProperty("--md-tooltip-x",`${s.x}px`),o.style.setProperty("--md-tooltip-y",`${s.y}px`)},complete(){o.style.removeProperty("--md-tooltip-x"),o.style.removeProperty("--md-tooltip-y")}}),L(i.pipe(g(({active:s})=>s)),i.pipe(Ae(250),g(({active:s})=>!s))).subscribe({next({active:s}){s?(e.insertAdjacentElement("afterend",o),e.setAttribute("aria-describedby",r),e.removeAttribute("title")):(o.remove(),e.removeAttribute("aria-describedby"),e.setAttribute("title",t))},complete(){o.remove(),e.removeAttribute("aria-describedby"),e.setAttribute("title",t)}}),i.pipe($e(16,ye)).subscribe(({active:s})=>{o.classList.toggle("md-tooltip--active",s)}),i.pipe(gt(125,ye),g(()=>!!e.offsetParent),m(()=>e.offsetParent.getBoundingClientRect()),m(({x:s})=>s)).subscribe({next(s){s?o.style.setProperty("--md-tooltip-0",`${-s}px`):o.style.removeProperty("--md-tooltip-0")},complete(){o.style.removeProperty("--md-tooltip-0")}}),ls(o,e).pipe(O(s=>i.next(s)),A(()=>i.complete()),m(s=>P({ref:e},s)))}).pipe(et(pe))}function ms({viewport$:e}){if(!V("header.autohide"))return $(!1);let t=e.pipe(m(({offset:{y:n}})=>n),ot(2,1),m(([n,i])=>[nMath.abs(i-n.y)>100),m(([,[n]])=>n),Y()),o=Je("search");return z([e,o]).pipe(m(([{offset:n},i])=>n.y>400&&!i),Y(),b(n=>n?r:$(!1)),Q(!1))}function ai(e,t){return H(()=>z([Le(e),ms(t)])).pipe(m(([{height:r},o])=>({height:r,hidden:o})),Y((r,o)=>r.height===o.height&&r.hidden===o.hidden),Z(1))}function si(e,{header$:t,main$:r}){return H(()=>{let o=new T,n=o.pipe(oe(),ae(!0));o.pipe(ne("active"),Pe(t)).subscribe(([{active:s},{hidden:a}])=>{e.classList.toggle("md-header--shadow",s&&!a),e.hidden=a});let i=fe(M("[title]",e)).pipe(g(()=>V("content.tooltips")),J(s=>ii(s)));return r.subscribe(o),t.pipe(W(n),m(s=>P({ref:e},s)),Ve(i.pipe(W(n))))})}function fs(e,{viewport$:t,header$:r}){return Er(e,{viewport$:t,header$:r}).pipe(m(({offset:{y:o}})=>{let{height:n}=de(e);return{active:n>0&&o>=n}}),ne("active"))}function ci(e,t){return H(()=>{let r=new T;r.subscribe({next({active:n}){e.classList.toggle("md-header__title--active",n)},complete(){e.classList.remove("md-header__title--active")}});let o=ue(".md-content h1");return typeof o=="undefined"?y:fs(o,t).pipe(O(n=>r.next(n)),A(()=>r.complete()),m(n=>P({ref:e},n)))})}function pi(e,{viewport$:t,header$:r}){let o=r.pipe(m(({height:i})=>i),Y()),n=o.pipe(b(()=>Le(e).pipe(m(({height:i})=>({top:e.offsetTop,bottom:e.offsetTop+i})),ne("bottom"))));return z([o,n,t]).pipe(m(([i,{top:s,bottom:a},{offset:{y:c},size:{height:p}}])=>(p=Math.max(0,p-Math.max(0,s-c,i)-Math.max(0,p+c-a)),{offset:s-i,height:p,active:s-i<=c})),Y((i,s)=>i.offset===s.offset&&i.height===s.height&&i.active===s.active))}function us(e){let t=__md_get("__palette")||{index:e.findIndex(o=>matchMedia(o.getAttribute("data-md-color-media")).matches)},r=Math.max(0,Math.min(t.index,e.length-1));return $(...e).pipe(J(o=>h(o,"change").pipe(m(()=>o))),Q(e[r]),m(o=>({index:e.indexOf(o),color:{media:o.getAttribute("data-md-color-media"),scheme:o.getAttribute("data-md-color-scheme"),primary:o.getAttribute("data-md-color-primary"),accent:o.getAttribute("data-md-color-accent")}})),Z(1))}function li(e){let t=M("input",e),r=x("meta",{name:"theme-color"});document.head.appendChild(r);let o=x("meta",{name:"color-scheme"});document.head.appendChild(o);let n=Wt("(prefers-color-scheme: light)");return H(()=>{let i=new T;return i.subscribe(s=>{if(document.body.setAttribute("data-md-color-switching",""),s.color.media==="(prefers-color-scheme)"){let a=matchMedia("(prefers-color-scheme: light)"),c=document.querySelector(a.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");s.color.scheme=c.getAttribute("data-md-color-scheme"),s.color.primary=c.getAttribute("data-md-color-primary"),s.color.accent=c.getAttribute("data-md-color-accent")}for(let[a,c]of Object.entries(s.color))document.body.setAttribute(`data-md-color-${a}`,c);for(let a=0;as.key==="Enter"),te(i,(s,a)=>a)).subscribe(({index:s})=>{s=(s+1)%t.length,t[s].click(),t[s].focus()}),i.pipe(m(()=>{let s=Ce("header"),a=window.getComputedStyle(s);return o.content=a.colorScheme,a.backgroundColor.match(/\d+/g).map(c=>(+c).toString(16).padStart(2,"0")).join("")})).subscribe(s=>r.content=`#${s}`),i.pipe(xe(pe)).subscribe(()=>{document.body.removeAttribute("data-md-color-switching")}),us(t).pipe(W(n.pipe(Ie(1))),vt(),O(s=>i.next(s)),A(()=>i.complete()),m(s=>P({ref:e},s)))})}function mi(e,{progress$:t}){return H(()=>{let r=new T;return r.subscribe(({value:o})=>{e.style.setProperty("--md-progress-value",`${o}`)}),t.pipe(O(o=>r.next({value:o})),A(()=>r.complete()),m(o=>({ref:e,value:o})))})}function fi(e,t){return e.protocol=t.protocol,e.hostname=t.hostname,e}function ds(e,t){let r=new Map;for(let o of M("url",e)){let n=j("loc",o),i=[fi(new URL(n.textContent),t)];r.set(`${i[0]}`,i);for(let s of M("[rel=alternate]",o)){let a=s.getAttribute("href");a!=null&&i.push(fi(new URL(a),t))}}return r}function kt(e){return En(new URL("sitemap.xml",e)).pipe(m(t=>ds(t,new URL(e))),ve(()=>$(new Map)),le())}function ui({document$:e}){let t=new Map;e.pipe(b(()=>M("link[rel=alternate]")),m(r=>new URL(r.href)),g(r=>!t.has(r.toString())),J(r=>kt(r).pipe(m(o=>[r,o]),ve(()=>y)))).subscribe(([r,o])=>{t.set(r.toString().replace(/\/$/,""),o)}),h(document.body,"click").pipe(g(r=>!r.metaKey&&!r.ctrlKey),b(r=>{if(r.target instanceof Element){let o=r.target.closest("a");if(o&&!o.target){let n=[...t].find(([f])=>o.href.startsWith(`${f}/`));if(typeof n=="undefined")return y;let[i,s]=n,a=we();if(a.href.startsWith(i))return y;let c=Te(),p=a.href.replace(c.base,"");p=`${i}/${p}`;let l=s.has(p.split("#")[0])?new URL(p,c.base):new URL(i);return r.preventDefault(),$(l)}}return y})).subscribe(r=>st(r,!0))}var co=$t(ao());function hs(e){e.setAttribute("data-md-copying","");let t=e.closest("[data-copy]"),r=t?t.getAttribute("data-copy"):e.innerText;return e.removeAttribute("data-md-copying"),r.trimEnd()}function di({alert$:e}){co.default.isSupported()&&new F(t=>{new co.default("[data-clipboard-target], [data-clipboard-text]",{text:r=>r.getAttribute("data-clipboard-text")||hs(j(r.getAttribute("data-clipboard-target")))}).on("success",r=>t.next(r))}).pipe(O(t=>{t.trigger.focus()}),m(()=>Me("clipboard.copied"))).subscribe(e)}function hi(e,t){if(!(e.target instanceof Element))return y;let r=e.target.closest("a");if(r===null)return y;if(r.target||e.metaKey||e.ctrlKey)return y;let o=new URL(r.href);return o.search=o.hash="",t.has(`${o}`)?(e.preventDefault(),$(r)):y}function bi(e){let t=new Map;for(let r of M(":scope > *",e.head))t.set(r.outerHTML,r);return t}function vi(e){for(let t of M("[href], [src]",e))for(let r of["href","src"]){let o=t.getAttribute(r);if(o&&!/^(?:[a-z]+:)?\/\//i.test(o)){t[r]=t[r];break}}return $(e)}function bs(e){for(let o of["[data-md-component=announce]","[data-md-component=container]","[data-md-component=header-topic]","[data-md-component=outdated]","[data-md-component=logo]","[data-md-component=skip]",...V("navigation.tabs.sticky")?["[data-md-component=tabs]"]:[]]){let n=ue(o),i=ue(o,e);typeof n!="undefined"&&typeof i!="undefined"&&n.replaceWith(i)}let t=bi(document);for(let[o,n]of bi(e))t.has(o)?t.delete(o):document.head.appendChild(n);for(let o of t.values()){let n=o.getAttribute("name");n!=="theme-color"&&n!=="color-scheme"&&o.remove()}let r=Ce("container");return Ke(M("script",r)).pipe(b(o=>{let n=e.createElement("script");if(o.src){for(let i of o.getAttributeNames())n.setAttribute(i,o.getAttribute(i));return o.replaceWith(n),new F(i=>{n.onload=()=>i.complete()})}else return n.textContent=o.textContent,o.replaceWith(n),y}),oe(),ae(document))}function gi({sitemap$:e,location$:t,viewport$:r,progress$:o}){if(location.protocol==="file:")return y;$(document).subscribe(vi);let n=h(document.body,"click").pipe(Pe(e),b(([a,c])=>hi(a,c)),m(({href:a})=>new URL(a)),le()),i=h(window,"popstate").pipe(m(we),le());n.pipe(te(r)).subscribe(([a,{offset:c}])=>{history.replaceState(c,""),history.pushState(null,"",a)}),L(n,i).subscribe(t);let s=t.pipe(ne("pathname"),b(a=>xr(a,{progress$:o}).pipe(ve(()=>(st(a,!0),y)))),b(vi),b(bs),le());return L(s.pipe(te(t,(a,c)=>c)),s.pipe(b(()=>t),ne("hash")),t.pipe(Y((a,c)=>a.pathname===c.pathname&&a.hash===c.hash),b(()=>n),O(()=>history.back()))).subscribe(a=>{var c,p;history.state!==null||!a.hash?window.scrollTo(0,(p=(c=history.state)==null?void 0:c.y)!=null?p:0):(history.scrollRestoration="auto",gn(a.hash),history.scrollRestoration="manual")}),t.subscribe(()=>{history.scrollRestoration="manual"}),h(window,"beforeunload").subscribe(()=>{history.scrollRestoration="auto"}),r.pipe(ne("offset"),Ae(100)).subscribe(({offset:a})=>{history.replaceState(a,"")}),V("navigation.instant.prefetch")&&L(h(document.body,"mousemove"),h(document.body,"focusin")).pipe(Pe(e),b(([a,c])=>hi(a,c)),Ae(25),Qr(({href:a})=>a),hr(a=>{let c=document.createElement("link");return c.rel="prefetch",c.href=a.toString(),document.head.appendChild(c),h(c,"load").pipe(m(()=>c),Ee(1))})).subscribe(a=>a.remove()),s}var yi=$t(ro());function xi(e){let t=e.separator.split("|").map(n=>n.replace(/(\(\?[!=<][^)]+\))/g,"").length===0?"\uFFFD":n).join("|"),r=new RegExp(t,"img"),o=(n,i,s)=>`${i}${s}`;return n=>{n=n.replace(/[\s*+\-:~^]+/g," ").replace(/&/g,"&").trim();let i=new RegExp(`(^|${e.separator}|)(${n.replace(/[|\\{}()[\]^$+*?.-]/g,"\\$&").replace(r,"|")})`,"img");return s=>(0,yi.default)(s).replace(i,o).replace(/<\/mark>(\s+)]*>/img,"$1")}}function zt(e){return e.type===1}function Sr(e){return e.type===3}function Ei(e,t){let r=Mn(e);return L($(location.protocol!=="file:"),Je("search")).pipe(Re(o=>o),b(()=>t)).subscribe(({config:o,docs:n})=>r.next({type:0,data:{config:o,docs:n,options:{suggest:V("search.suggest")}}})),r}function wi(e){var l;let{selectedVersionSitemap:t,selectedVersionBaseURL:r,currentLocation:o,currentBaseURL:n}=e,i=(l=po(n))==null?void 0:l.pathname;if(i===void 0)return;let s=ys(o.pathname,i);if(s===void 0)return;let a=Es(t.keys());if(!t.has(a))return;let c=po(s,a);if(!c||!t.has(c.href))return;let p=po(s,r);if(p)return p.hash=o.hash,p.search=o.search,p}function po(e,t){try{return new URL(e,t)}catch(r){return}}function ys(e,t){if(e.startsWith(t))return e.slice(t.length)}function xs(e,t){let r=Math.min(e.length,t.length),o;for(o=0;oy)),o=r.pipe(m(n=>{let[,i]=t.base.match(/([^/]+)\/?$/);return n.find(({version:s,aliases:a})=>s===i||a.includes(i))||n[0]}));r.pipe(m(n=>new Map(n.map(i=>[`${new URL(`../${i.version}/`,t.base)}`,i]))),b(n=>h(document.body,"click").pipe(g(i=>!i.metaKey&&!i.ctrlKey),te(o),b(([i,s])=>{if(i.target instanceof Element){let a=i.target.closest("a");if(a&&!a.target&&n.has(a.href)){let c=a.href;return!i.target.closest(".md-version")&&n.get(c)===s?y:(i.preventDefault(),$(new URL(c)))}}return y}),b(i=>kt(i).pipe(m(s=>{var a;return(a=wi({selectedVersionSitemap:s,selectedVersionBaseURL:i,currentLocation:we(),currentBaseURL:t.base}))!=null?a:i})))))).subscribe(n=>st(n,!0)),z([r,o]).subscribe(([n,i])=>{j(".md-header__topic").appendChild(Wn(n,i))}),e.pipe(b(()=>o)).subscribe(n=>{var a;let i=new URL(t.base),s=__md_get("__outdated",sessionStorage,i);if(s===null){s=!0;let c=((a=t.version)==null?void 0:a.default)||"latest";Array.isArray(c)||(c=[c]);e:for(let p of c)for(let l of n.aliases.concat(n.version))if(new RegExp(p,"i").test(l)){s=!1;break e}__md_set("__outdated",s,sessionStorage,i)}if(s)for(let c of me("outdated"))c.hidden=!1})}function ws(e,{worker$:t}){let{searchParams:r}=we();r.has("q")&&(at("search",!0),e.value=r.get("q"),e.focus(),Je("search").pipe(Re(i=>!i)).subscribe(()=>{let i=we();i.searchParams.delete("q"),history.replaceState({},"",`${i}`)}));let o=Ye(e),n=L(t.pipe(Re(zt)),h(e,"keyup"),o).pipe(m(()=>e.value),Y());return z([n,o]).pipe(m(([i,s])=>({value:i,focus:s})),Z(1))}function Si(e,{worker$:t}){let r=new T,o=r.pipe(oe(),ae(!0));z([t.pipe(Re(zt)),r],(i,s)=>s).pipe(ne("value")).subscribe(({value:i})=>t.next({type:2,data:i})),r.pipe(ne("focus")).subscribe(({focus:i})=>{i&&at("search",i)}),h(e.form,"reset").pipe(W(o)).subscribe(()=>e.focus());let n=j("header [for=__search]");return h(n,"click").subscribe(()=>e.focus()),ws(e,{worker$:t}).pipe(O(i=>r.next(i)),A(()=>r.complete()),m(i=>P({ref:e},i)),Z(1))}function Oi(e,{worker$:t,query$:r}){let o=new T,n=un(e.parentElement).pipe(g(Boolean)),i=e.parentElement,s=j(":scope > :first-child",e),a=j(":scope > :last-child",e);Je("search").subscribe(l=>{a.setAttribute("role",l?"list":"presentation"),a.hidden=!l}),o.pipe(te(r),Gr(t.pipe(Re(zt)))).subscribe(([{items:l},{value:f}])=>{switch(l.length){case 0:s.textContent=f.length?Me("search.result.none"):Me("search.result.placeholder");break;case 1:s.textContent=Me("search.result.one");break;default:let u=br(l.length);s.textContent=Me("search.result.other",u)}});let c=o.pipe(O(()=>a.innerHTML=""),b(({items:l})=>L($(...l.slice(0,10)),$(...l.slice(10)).pipe(ot(4),Xr(n),b(([f])=>f)))),m(Fn),le());return c.subscribe(l=>a.appendChild(l)),c.pipe(J(l=>{let f=ue("details",l);return typeof f=="undefined"?y:h(f,"toggle").pipe(W(o),m(()=>f))})).subscribe(l=>{l.open===!1&&l.offsetTop<=i.scrollTop&&i.scrollTo({top:l.offsetTop})}),t.pipe(g(Sr),m(({data:l})=>l)).pipe(O(l=>o.next(l)),A(()=>o.complete()),m(l=>P({ref:e},l)))}function Ts(e,{query$:t}){return t.pipe(m(({value:r})=>{let o=we();return o.hash="",r=r.replace(/\s+/g,"+").replace(/&/g,"%26").replace(/=/g,"%3D"),o.search=`q=${r}`,{url:o}}))}function Li(e,t){let r=new T,o=r.pipe(oe(),ae(!0));return r.subscribe(({url:n})=>{e.setAttribute("data-clipboard-text",e.href),e.href=`${n}`}),h(e,"click").pipe(W(o)).subscribe(n=>n.preventDefault()),Ts(e,t).pipe(O(n=>r.next(n)),A(()=>r.complete()),m(n=>P({ref:e},n)))}function Mi(e,{worker$:t,keyboard$:r}){let o=new T,n=Ce("search-query"),i=L(h(n,"keydown"),h(n,"focus")).pipe(xe(pe),m(()=>n.value),Y());return o.pipe(Pe(i),m(([{suggest:a},c])=>{let p=c.split(/([\s-]+)/);if(a!=null&&a.length&&p[p.length-1]){let l=a[a.length-1];l.startsWith(p[p.length-1])&&(p[p.length-1]=l)}else p.length=0;return p})).subscribe(a=>e.innerHTML=a.join("").replace(/\s/g," ")),r.pipe(g(({mode:a})=>a==="search")).subscribe(a=>{a.type==="ArrowRight"&&e.innerText.length&&n.selectionStart===n.value.length&&(n.value=e.innerText)}),t.pipe(g(Sr),m(({data:a})=>a)).pipe(O(a=>o.next(a)),A(()=>o.complete()),m(()=>({ref:e})))}function _i(e,{index$:t,keyboard$:r}){let o=Te();try{let n=Ei(o.search,t),i=Ce("search-query",e),s=Ce("search-result",e);h(e,"click").pipe(g(({target:c})=>c instanceof Element&&!!c.closest("a"))).subscribe(()=>at("search",!1)),r.pipe(g(({mode:c})=>c==="search")).subscribe(c=>{let p=Ne();switch(c.type){case"Enter":if(p===i){let l=new Map;for(let f of M(":first-child [href]",s)){let u=f.firstElementChild;l.set(f,parseFloat(u.getAttribute("data-md-score")))}if(l.size){let[[f]]=[...l].sort(([,u],[,d])=>d-u);f.click()}c.claim()}break;case"Escape":case"Tab":at("search",!1),i.blur();break;case"ArrowUp":case"ArrowDown":if(typeof p=="undefined")i.focus();else{let l=[i,...M(":not(details) > [href], summary, details[open] [href]",s)],f=Math.max(0,(Math.max(0,l.indexOf(p))+l.length+(c.type==="ArrowUp"?-1:1))%l.length);l[f].focus()}c.claim();break;default:i!==Ne()&&i.focus()}}),r.pipe(g(({mode:c})=>c==="global")).subscribe(c=>{switch(c.type){case"f":case"s":case"/":i.focus(),i.select(),c.claim();break}});let a=Si(i,{worker$:n});return L(a,Oi(s,{worker$:n,query$:a})).pipe(Ve(...me("search-share",e).map(c=>Li(c,{query$:a})),...me("search-suggest",e).map(c=>Mi(c,{worker$:n,keyboard$:r}))))}catch(n){return e.hidden=!0,tt}}function Ai(e,{index$:t,location$:r}){return z([t,r.pipe(Q(we()),g(o=>!!o.searchParams.get("h")))]).pipe(m(([o,n])=>xi(o.config)(n.searchParams.get("h"))),m(o=>{var s;let n=new Map,i=document.createNodeIterator(e,NodeFilter.SHOW_TEXT);for(let a=i.nextNode();a;a=i.nextNode())if((s=a.parentElement)!=null&&s.offsetHeight){let c=a.textContent,p=o(c);p.length>c.length&&n.set(a,p)}for(let[a,c]of n){let{childNodes:p}=x("span",null,c);a.replaceWith(...Array.from(p))}return{ref:e,nodes:n}}))}function Ss(e,{viewport$:t,main$:r}){let o=e.closest(".md-grid"),n=o.offsetTop-o.parentElement.offsetTop;return z([r,t]).pipe(m(([{offset:i,height:s},{offset:{y:a}}])=>(s=s+Math.min(n,Math.max(0,a-i))-n,{height:s,locked:a>=i+n})),Y((i,s)=>i.height===s.height&&i.locked===s.locked))}function lo(e,o){var n=o,{header$:t}=n,r=vo(n,["header$"]);let i=j(".md-sidebar__scrollwrap",e),{y:s}=Be(i);return H(()=>{let a=new T,c=a.pipe(oe(),ae(!0)),p=a.pipe($e(0,ye));return p.pipe(te(t)).subscribe({next([{height:l},{height:f}]){i.style.height=`${l-2*s}px`,e.style.top=`${f}px`},complete(){i.style.height="",e.style.top=""}}),p.pipe(Re()).subscribe(()=>{for(let l of M(".md-nav__link--active[href]",e)){if(!l.clientHeight)continue;let f=l.closest(".md-sidebar__scrollwrap");if(typeof f!="undefined"){let u=l.offsetTop-f.offsetTop,{height:d}=de(f);f.scrollTo({top:u-d/2})}}}),fe(M("label[tabindex]",e)).pipe(J(l=>h(l,"click").pipe(xe(pe),m(()=>l),W(c)))).subscribe(l=>{let f=j(`[id="${l.htmlFor}"]`);j(`[aria-labelledby="${l.id}"]`).setAttribute("aria-expanded",`${f.checked}`)}),V("content.tooltips")&&fe(M("abbr[title]",e)).pipe(J(l=>Xe(l,{viewport$})),W(c)).subscribe(),Ss(e,r).pipe(O(l=>a.next(l)),A(()=>a.complete()),m(l=>P({ref:e},l)))})}function Ci(e,t){if(typeof t!="undefined"){let r=`https://api.github.com/repos/${e}/${t}`;return rt(ze(`${r}/releases/latest`).pipe(ve(()=>y),m(o=>({version:o.tag_name})),Qe({})),ze(r).pipe(ve(()=>y),m(o=>({stars:o.stargazers_count,forks:o.forks_count})),Qe({}))).pipe(m(([o,n])=>P(P({},o),n)))}else{let r=`https://api.github.com/users/${e}`;return ze(r).pipe(m(o=>({repositories:o.public_repos})),Qe({}))}}function ki(e,t){let r=`https://${e}/api/v4/projects/${encodeURIComponent(t)}`;return rt(ze(`${r}/releases/permalink/latest`).pipe(ve(()=>y),m(({tag_name:o})=>({version:o})),Qe({})),ze(r).pipe(ve(()=>y),m(({star_count:o,forks_count:n})=>({stars:o,forks:n})),Qe({}))).pipe(m(([o,n])=>P(P({},o),n)))}function Hi(e){let t=e.match(/^.+github\.com\/([^/]+)\/?([^/]+)?/i);if(t){let[,r,o]=t;return Ci(r,o)}if(t=e.match(/^.+?([^/]*gitlab[^/]+)\/(.+?)\/?$/i),t){let[,r,o]=t;return ki(r,o)}return y}var Os;function Ls(e){return Os||(Os=H(()=>{let t=__md_get("__source",sessionStorage);if(t)return $(t);if(me("consent").length){let o=__md_get("__consent");if(!(o&&o.github))return y}return Hi(e.href).pipe(O(o=>__md_set("__source",o,sessionStorage)))}).pipe(ve(()=>y),g(t=>Object.keys(t).length>0),m(t=>({facts:t})),Z(1)))}function $i(e){let t=j(":scope > :last-child",e);return H(()=>{let r=new T;return r.subscribe(({facts:o})=>{t.appendChild(jn(o)),t.classList.add("md-source__repository--active")}),Ls(e).pipe(O(o=>r.next(o)),A(()=>r.complete()),m(o=>P({ref:e},o)))})}function Ms(e,{viewport$:t,header$:r}){return Le(document.body).pipe(b(()=>Er(e,{header$:r,viewport$:t})),m(({offset:{y:o}})=>({hidden:o>=10})),ne("hidden"))}function Pi(e,t){return H(()=>{let r=new T;return r.subscribe({next({hidden:o}){e.hidden=o},complete(){e.hidden=!1}}),(V("navigation.tabs.sticky")?$({hidden:!1}):Ms(e,t)).pipe(O(o=>r.next(o)),A(()=>r.complete()),m(o=>P({ref:e},o)))})}function _s(e,{viewport$:t,header$:r}){let o=new Map,n=M(".md-nav__link",e);for(let a of n){let c=decodeURIComponent(a.hash.substring(1)),p=ue(`[id="${c}"]`);typeof p!="undefined"&&o.set(a,p)}let i=r.pipe(ne("height"),m(({height:a})=>{let c=Ce("main"),p=j(":scope > :first-child",c);return a+.8*(p.offsetTop-c.offsetTop)}),le());return Le(document.body).pipe(ne("height"),b(a=>H(()=>{let c=[];return $([...o].reduce((p,[l,f])=>{for(;c.length&&o.get(c[c.length-1]).tagName>=f.tagName;)c.pop();let u=f.offsetTop;for(;!u&&f.parentElement;)f=f.parentElement,u=f.offsetTop;let d=f.offsetParent;for(;d;d=d.offsetParent)u+=d.offsetTop;return p.set([...c=[...c,l]].reverse(),u)},new Map))}).pipe(m(c=>new Map([...c].sort(([,p],[,l])=>p-l))),Pe(i),b(([c,p])=>t.pipe(Ut(([l,f],{offset:{y:u},size:d})=>{let v=u+d.height>=Math.floor(a.height);for(;f.length;){let[,S]=f[0];if(S-p=u&&!v)f=[l.pop(),...f];else break}return[l,f]},[[],[...c]]),Y((l,f)=>l[0]===f[0]&&l[1]===f[1])))))).pipe(m(([a,c])=>({prev:a.map(([p])=>p),next:c.map(([p])=>p)})),Q({prev:[],next:[]}),ot(2,1),m(([a,c])=>a.prev.length{let i=new T,s=i.pipe(oe(),ae(!0));if(i.subscribe(({prev:a,next:c})=>{for(let[p]of c)p.classList.remove("md-nav__link--passed"),p.classList.remove("md-nav__link--active");for(let[p,[l]]of a.entries())l.classList.add("md-nav__link--passed"),l.classList.toggle("md-nav__link--active",p===a.length-1)}),V("toc.follow")){let a=L(t.pipe(Ae(1),m(()=>{})),t.pipe(Ae(250),m(()=>"smooth")));i.pipe(g(({prev:c})=>c.length>0),Pe(o.pipe(xe(pe))),te(a)).subscribe(([[{prev:c}],p])=>{let[l]=c[c.length-1];if(l.offsetHeight){let f=vr(l);if(typeof f!="undefined"){let u=l.offsetTop-f.offsetTop,{height:d}=de(f);f.scrollTo({top:u-d/2,behavior:p})}}})}return V("navigation.tracking")&&t.pipe(W(s),ne("offset"),Ae(250),Ie(1),W(n.pipe(Ie(1))),vt({delay:250}),te(i)).subscribe(([,{prev:a}])=>{let c=we(),p=a[a.length-1];if(p&&p.length){let[l]=p,{hash:f}=new URL(l.href);c.hash!==f&&(c.hash=f,history.replaceState({},"",`${c}`))}else c.hash="",history.replaceState({},"",`${c}`)}),_s(e,{viewport$:t,header$:r}).pipe(O(a=>i.next(a)),A(()=>i.complete()),m(a=>P({ref:e},a)))})}function As(e,{viewport$:t,main$:r,target$:o}){let n=t.pipe(m(({offset:{y:s}})=>s),ot(2,1),m(([s,a])=>s>a&&a>0),Y()),i=r.pipe(m(({active:s})=>s));return z([i,n]).pipe(m(([s,a])=>!(s&&a)),Y(),W(o.pipe(Ie(1))),ae(!0),vt({delay:250}),m(s=>({hidden:s})))}function Ii(e,{viewport$:t,header$:r,main$:o,target$:n}){let i=new T,s=i.pipe(oe(),ae(!0));return i.subscribe({next({hidden:a}){e.hidden=a,a?(e.setAttribute("tabindex","-1"),e.blur()):e.removeAttribute("tabindex")},complete(){e.style.top="",e.hidden=!0,e.removeAttribute("tabindex")}}),r.pipe(W(s),ne("height")).subscribe(({height:a})=>{e.style.top=`${a+16}px`}),h(e,"click").subscribe(a=>{a.preventDefault(),window.scrollTo({top:0})}),As(e,{viewport$:t,main$:o,target$:n}).pipe(O(a=>i.next(a)),A(()=>i.complete()),m(a=>P({ref:e},a)))}function Fi({document$:e,viewport$:t}){e.pipe(b(()=>M(".md-ellipsis")),J(r=>mt(r).pipe(W(e.pipe(Ie(1))),g(o=>o),m(()=>r),Ee(1))),g(r=>r.offsetWidth{let o=r.innerText,n=r.closest("a")||r;return n.title=o,V("content.tooltips")?Xe(n,{viewport$:t}).pipe(W(e.pipe(Ie(1))),A(()=>n.removeAttribute("title"))):y})).subscribe(),V("content.tooltips")&&e.pipe(b(()=>M(".md-status")),J(r=>Xe(r,{viewport$:t}))).subscribe()}function ji({document$:e,tablet$:t}){e.pipe(b(()=>M(".md-toggle--indeterminate")),O(r=>{r.indeterminate=!0,r.checked=!1}),J(r=>h(r,"change").pipe(Jr(()=>r.classList.contains("md-toggle--indeterminate")),m(()=>r))),te(t)).subscribe(([r,o])=>{r.classList.remove("md-toggle--indeterminate"),o&&(r.checked=!1)})}function Cs(){return/(iPad|iPhone|iPod)/.test(navigator.userAgent)}function Ui({document$:e}){e.pipe(b(()=>M("[data-md-scrollfix]")),O(t=>t.removeAttribute("data-md-scrollfix")),g(Cs),J(t=>h(t,"touchstart").pipe(m(()=>t)))).subscribe(t=>{let r=t.scrollTop;r===0?t.scrollTop=1:r+t.offsetHeight===t.scrollHeight&&(t.scrollTop=r-1)})}function Wi({viewport$:e,tablet$:t}){z([Je("search"),t]).pipe(m(([r,o])=>r&&!o),b(r=>$(r).pipe(nt(r?400:100))),te(e)).subscribe(([r,{offset:{y:o}}])=>{if(r)document.body.setAttribute("data-md-scrolllock",""),document.body.style.top=`-${o}px`;else{let n=-1*parseInt(document.body.style.top,10);document.body.removeAttribute("data-md-scrolllock"),document.body.style.top="",n&&window.scrollTo(0,n)}})}Object.entries||(Object.entries=function(e){let t=[];for(let r of Object.keys(e))t.push([r,e[r]]);return t});Object.values||(Object.values=function(e){let t=[];for(let r of Object.keys(e))t.push(e[r]);return t});typeof Element!="undefined"&&(Element.prototype.scrollTo||(Element.prototype.scrollTo=function(e,t){typeof e=="object"?(this.scrollLeft=e.left,this.scrollTop=e.top):(this.scrollLeft=e,this.scrollTop=t)}),Element.prototype.replaceWith||(Element.prototype.replaceWith=function(...e){let t=this.parentNode;if(t){e.length===0&&t.removeChild(this);for(let r=e.length-1;r>=0;r--){let o=e[r];typeof o=="string"?o=document.createTextNode(o):o.parentNode&&o.parentNode.removeChild(o),r?t.insertBefore(this.previousSibling,o):t.replaceChild(o,this)}}}));function ks(){return location.protocol==="file:"?_t(`${new URL("search/search_index.js",Or.base)}`).pipe(m(()=>__index),Z(1)):ze(new URL("search/search_index.json",Or.base))}document.documentElement.classList.remove("no-js");document.documentElement.classList.add("js");var ct=an(),Kt=bn(),Ht=yn(Kt),mo=hn(),ke=Ln(),Lr=Wt("(min-width: 60em)"),Vi=Wt("(min-width: 76.25em)"),Ni=xn(),Or=Te(),zi=document.forms.namedItem("search")?ks():tt,fo=new T;di({alert$:fo});ui({document$:ct});var uo=new T,qi=kt(Or.base);V("navigation.instant")&&gi({sitemap$:qi,location$:Kt,viewport$:ke,progress$:uo}).subscribe(ct);var Di;((Di=Or.version)==null?void 0:Di.provider)==="mike"&&Ti({document$:ct});L(Kt,Ht).pipe(nt(125)).subscribe(()=>{at("drawer",!1),at("search",!1)});mo.pipe(g(({mode:e})=>e==="global")).subscribe(e=>{switch(e.type){case"p":case",":let t=ue("link[rel=prev]");typeof t!="undefined"&&st(t);break;case"n":case".":let r=ue("link[rel=next]");typeof r!="undefined"&&st(r);break;case"Enter":let o=Ne();o instanceof HTMLLabelElement&&o.click()}});Fi({viewport$:ke,document$:ct});ji({document$:ct,tablet$:Lr});Ui({document$:ct});Wi({viewport$:ke,tablet$:Lr});var ft=ai(Ce("header"),{viewport$:ke}),qt=ct.pipe(m(()=>Ce("main")),b(e=>pi(e,{viewport$:ke,header$:ft})),Z(1)),Hs=L(...me("consent").map(e=>An(e,{target$:Ht})),...me("dialog").map(e=>ni(e,{alert$:fo})),...me("palette").map(e=>li(e)),...me("progress").map(e=>mi(e,{progress$:uo})),...me("search").map(e=>_i(e,{index$:zi,keyboard$:mo})),...me("source").map(e=>$i(e))),$s=H(()=>L(...me("announce").map(e=>_n(e)),...me("content").map(e=>oi(e,{sitemap$:qi,viewport$:ke,target$:Ht,print$:Ni})),...me("content").map(e=>V("search.highlight")?Ai(e,{index$:zi,location$:Kt}):y),...me("header").map(e=>si(e,{viewport$:ke,header$:ft,main$:qt})),...me("header-title").map(e=>ci(e,{viewport$:ke,header$:ft})),...me("sidebar").map(e=>e.getAttribute("data-md-type")==="navigation"?eo(Vi,()=>lo(e,{viewport$:ke,header$:ft,main$:qt})):eo(Lr,()=>lo(e,{viewport$:ke,header$:ft,main$:qt}))),...me("tabs").map(e=>Pi(e,{viewport$:ke,header$:ft})),...me("toc").map(e=>Ri(e,{viewport$:ke,header$:ft,main$:qt,target$:Ht})),...me("top").map(e=>Ii(e,{viewport$:ke,header$:ft,main$:qt,target$:Ht})))),Ki=ct.pipe(b(()=>$s),Ve(Hs),Z(1));Ki.subscribe();window.document$=ct;window.location$=Kt;window.target$=Ht;window.keyboard$=mo;window.viewport$=ke;window.tablet$=Lr;window.screen$=Vi;window.print$=Ni;window.alert$=fo;window.progress$=uo;window.component$=Ki;})(); //# sourceMappingURL=bundle.79ae519e.min.js.map -/*! update cache: 20260104155757 */ +/*! update cache: 20260104161456 */ diff --git a/ja/assets/javascripts/glightbox.min.js b/ja/assets/javascripts/glightbox.min.js index 59bdde0fc..c074a3a2b 100644 --- a/ja/assets/javascripts/glightbox.min.js +++ b/ja/assets/javascripts/glightbox.min.js @@ -1,2 +1,2 @@ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).GLightbox=t()}(this,(function(){"use strict";function e(t){return(e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(t)}function t(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){for(var i=0;i1&&void 0!==arguments[1]?arguments[1]:null,i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null,n=e[s]=e[s]||[],l={all:n,evt:null,found:null};return t&&i&&P(n)>0&&o(n,(function(e,n){if(e.eventName==t&&e.fn.toString()==i.toString())return l.found=!0,l.evt=n,!1})),l}function a(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=t.onElement,n=t.withCallback,s=t.avoidDuplicate,l=void 0===s||s,a=t.once,h=void 0!==a&&a,d=t.useCapture,c=void 0!==d&&d,u=arguments.length>2?arguments[2]:void 0,g=i||[];function v(e){T(n)&&n.call(u,e,this),h&&v.destroy()}return C(g)&&(g=document.querySelectorAll(g)),v.destroy=function(){o(g,(function(t){var i=r(t,e,v);i.found&&i.all.splice(i.evt,1),t.removeEventListener&&t.removeEventListener(e,v,c)}))},o(g,(function(t){var i=r(t,e,v);(t.addEventListener&&l&&!i.found||!l)&&(t.addEventListener(e,v,c),i.all.push({eventName:e,fn:v}))})),v}function h(e,t){o(t.split(" "),(function(t){return e.classList.add(t)}))}function d(e,t){o(t.split(" "),(function(t){return e.classList.remove(t)}))}function c(e,t){return e.classList.contains(t)}function u(e,t){for(;e!==document.body;){if(!(e=e.parentElement))return!1;if("function"==typeof e.matches?e.matches(t):e.msMatchesSelector(t))return e}}function g(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",i=arguments.length>2&&void 0!==arguments[2]&&arguments[2];if(!e||""===t)return!1;if("none"==t)return T(i)&&i(),!1;var n=x(),s=t.split(" ");o(s,(function(t){h(e,"g"+t)})),a(n,{onElement:e,avoidDuplicate:!1,once:!0,withCallback:function(e,t){o(s,(function(e){d(t,"g"+e)})),T(i)&&i()}})}function v(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";if(""==t)return e.style.webkitTransform="",e.style.MozTransform="",e.style.msTransform="",e.style.OTransform="",e.style.transform="",!1;e.style.webkitTransform=t,e.style.MozTransform=t,e.style.msTransform=t,e.style.OTransform=t,e.style.transform=t}function f(e){e.style.display="block"}function p(e){e.style.display="none"}function m(e){var t=document.createDocumentFragment(),i=document.createElement("div");for(i.innerHTML=e;i.firstChild;)t.appendChild(i.firstChild);return t}function y(){return{width:window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth,height:window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight}}function x(){var e,t=document.createElement("fakeelement"),i={animation:"animationend",OAnimation:"oAnimationEnd",MozAnimation:"animationend",WebkitAnimation:"webkitAnimationEnd"};for(e in i)if(void 0!==t.style[e])return i[e]}function b(e,t,i,n){if(e())t();else{var s;i||(i=100);var l=setInterval((function(){e()&&(clearInterval(l),s&&clearTimeout(s),t())}),i);n&&(s=setTimeout((function(){clearInterval(l)}),n))}}function S(e,t,i){if(I(e))console.error("Inject assets error");else if(T(t)&&(i=t,t=!1),C(t)&&t in window)T(i)&&i();else{var n;if(-1!==e.indexOf(".css")){if((n=document.querySelectorAll('link[href="'+e+'"]'))&&n.length>0)return void(T(i)&&i());var s=document.getElementsByTagName("head")[0],l=s.querySelectorAll('link[rel="stylesheet"]'),o=document.createElement("link");return o.rel="stylesheet",o.type="text/css",o.href=e,o.media="all",l?s.insertBefore(o,l[0]):s.appendChild(o),void(T(i)&&i())}if((n=document.querySelectorAll('script[src="'+e+'"]'))&&n.length>0){if(T(i)){if(C(t))return b((function(){return void 0!==window[t]}),(function(){i()})),!1;i()}}else{var r=document.createElement("script");r.type="text/javascript",r.src=e,r.onload=function(){if(T(i)){if(C(t))return b((function(){return void 0!==window[t]}),(function(){i()})),!1;i()}},document.body.appendChild(r)}}}function w(){return"navigator"in window&&window.navigator.userAgent.match(/(iPad)|(iPhone)|(iPod)|(Android)|(PlayBook)|(BB10)|(BlackBerry)|(Opera Mini)|(IEMobile)|(webOS)|(MeeGo)/i)}function T(e){return"function"==typeof e}function C(e){return"string"==typeof e}function k(e){return!(!e||!e.nodeType||1!=e.nodeType)}function E(e){return Array.isArray(e)}function A(e){return e&&e.length&&isFinite(e.length)}function L(t){return"object"===e(t)&&null!=t&&!T(t)&&!E(t)}function I(e){return null==e}function O(e,t){return null!==e&&hasOwnProperty.call(e,t)}function P(e){if(L(e)){if(e.keys)return e.keys().length;var t=0;for(var i in e)O(e,i)&&t++;return t}return e.length}function M(e){return!isNaN(parseFloat(e))&&isFinite(e)}function z(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:-1,t=document.querySelectorAll(".gbtn[data-taborder]:not(.disabled)");if(!t.length)return!1;if(1==t.length)return t[0];"string"==typeof e&&(e=parseInt(e));var i=[];o(t,(function(e){i.push(e.getAttribute("data-taborder"))}));var n=Math.max.apply(Math,i.map((function(e){return parseInt(e)}))),s=e<0?1:e+1;s>n&&(s="1");var l=i.filter((function(e){return e>=parseInt(s)})),r=l.sort()[0];return document.querySelector('.gbtn[data-taborder="'.concat(r,'"]'))}function X(e){if(e.events.hasOwnProperty("keyboard"))return!1;e.events.keyboard=a("keydown",{onElement:window,withCallback:function(t,i){var n=(t=t||window.event).keyCode;if(9==n){var s=document.querySelector(".gbtn.focused");if(!s){var l=!(!document.activeElement||!document.activeElement.nodeName)&&document.activeElement.nodeName.toLocaleLowerCase();if("input"==l||"textarea"==l||"button"==l)return}t.preventDefault();var o=document.querySelectorAll(".gbtn[data-taborder]");if(!o||o.length<=0)return;if(!s){var r=z();return void(r&&(r.focus(),h(r,"focused")))}var a=z(s.getAttribute("data-taborder"));d(s,"focused"),a&&(a.focus(),h(a,"focused"))}39==n&&e.nextSlide(),37==n&&e.prevSlide(),27==n&&e.close()}})}function Y(e){return Math.sqrt(e.x*e.x+e.y*e.y)}function q(e,t){var i=function(e,t){var i=Y(e)*Y(t);if(0===i)return 0;var n=function(e,t){return e.x*t.x+e.y*t.y}(e,t)/i;return n>1&&(n=1),Math.acos(n)}(e,t);return function(e,t){return e.x*t.y-t.x*e.y}(e,t)>0&&(i*=-1),180*i/Math.PI}var N=function(){function e(i){t(this,e),this.handlers=[],this.el=i}return n(e,[{key:"add",value:function(e){this.handlers.push(e)}},{key:"del",value:function(e){e||(this.handlers=[]);for(var t=this.handlers.length;t>=0;t--)this.handlers[t]===e&&this.handlers.splice(t,1)}},{key:"dispatch",value:function(){for(var e=0,t=this.handlers.length;e=0)console.log("ignore drag for this touched element",e.target.nodeName.toLowerCase());else{this.now=Date.now(),this.x1=e.touches[0].pageX,this.y1=e.touches[0].pageY,this.delta=this.now-(this.last||this.now),this.touchStart.dispatch(e,this.element),null!==this.preTapPosition.x&&(this.isDoubleTap=this.delta>0&&this.delta<=250&&Math.abs(this.preTapPosition.x-this.x1)<30&&Math.abs(this.preTapPosition.y-this.y1)<30,this.isDoubleTap&&clearTimeout(this.singleTapTimeout)),this.preTapPosition.x=this.x1,this.preTapPosition.y=this.y1,this.last=this.now;var t=this.preV;if(e.touches.length>1){this._cancelLongTap(),this._cancelSingleTap();var i={x:e.touches[1].pageX-this.x1,y:e.touches[1].pageY-this.y1};t.x=i.x,t.y=i.y,this.pinchStartLen=Y(t),this.multipointStart.dispatch(e,this.element)}this._preventTap=!1,this.longTapTimeout=setTimeout(function(){this.longTap.dispatch(e,this.element),this._preventTap=!0}.bind(this),750)}}}},{key:"move",value:function(e){if(e.touches){var t=this.preV,i=e.touches.length,n=e.touches[0].pageX,s=e.touches[0].pageY;if(this.isDoubleTap=!1,i>1){var l=e.touches[1].pageX,o=e.touches[1].pageY,r={x:e.touches[1].pageX-n,y:e.touches[1].pageY-s};null!==t.x&&(this.pinchStartLen>0&&(e.zoom=Y(r)/this.pinchStartLen,this.pinch.dispatch(e,this.element)),e.angle=q(r,t),this.rotate.dispatch(e,this.element)),t.x=r.x,t.y=r.y,null!==this.x2&&null!==this.sx2?(e.deltaX=(n-this.x2+l-this.sx2)/2,e.deltaY=(s-this.y2+o-this.sy2)/2):(e.deltaX=0,e.deltaY=0),this.twoFingerPressMove.dispatch(e,this.element),this.sx2=l,this.sy2=o}else{if(null!==this.x2){e.deltaX=n-this.x2,e.deltaY=s-this.y2;var a=Math.abs(this.x1-this.x2),h=Math.abs(this.y1-this.y2);(a>10||h>10)&&(this._preventTap=!0)}else e.deltaX=0,e.deltaY=0;this.pressMove.dispatch(e,this.element)}this.touchMove.dispatch(e,this.element),this._cancelLongTap(),this.x2=n,this.y2=s,i>1&&e.preventDefault()}}},{key:"end",value:function(e){if(e.changedTouches){this._cancelLongTap();var t=this;e.touches.length<2&&(this.multipointEnd.dispatch(e,this.element),this.sx2=this.sy2=null),this.x2&&Math.abs(this.x1-this.x2)>30||this.y2&&Math.abs(this.y1-this.y2)>30?(e.direction=this._swipeDirection(this.x1,this.x2,this.y1,this.y2),this.swipeTimeout=setTimeout((function(){t.swipe.dispatch(e,t.element)}),0)):(this.tapTimeout=setTimeout((function(){t._preventTap||t.tap.dispatch(e,t.element),t.isDoubleTap&&(t.doubleTap.dispatch(e,t.element),t.isDoubleTap=!1)}),0),t.isDoubleTap||(t.singleTapTimeout=setTimeout((function(){t.singleTap.dispatch(e,t.element)}),250))),this.touchEnd.dispatch(e,this.element),this.preV.x=0,this.preV.y=0,this.zoom=1,this.pinchStartLen=null,this.x1=this.x2=this.y1=this.y2=null}}},{key:"cancelAll",value:function(){this._preventTap=!0,clearTimeout(this.singleTapTimeout),clearTimeout(this.tapTimeout),clearTimeout(this.longTapTimeout),clearTimeout(this.swipeTimeout)}},{key:"cancel",value:function(e){this.cancelAll(),this.touchCancel.dispatch(e,this.element)}},{key:"_cancelLongTap",value:function(){clearTimeout(this.longTapTimeout)}},{key:"_cancelSingleTap",value:function(){clearTimeout(this.singleTapTimeout)}},{key:"_swipeDirection",value:function(e,t,i,n){return Math.abs(e-t)>=Math.abs(i-n)?e-t>0?"Left":"Right":i-n>0?"Up":"Down"}},{key:"on",value:function(e,t){this[e]&&this[e].add(t)}},{key:"off",value:function(e,t){this[e]&&this[e].del(t)}},{key:"destroy",value:function(){return this.singleTapTimeout&&clearTimeout(this.singleTapTimeout),this.tapTimeout&&clearTimeout(this.tapTimeout),this.longTapTimeout&&clearTimeout(this.longTapTimeout),this.swipeTimeout&&clearTimeout(this.swipeTimeout),this.element.removeEventListener("touchstart",this.start),this.element.removeEventListener("touchmove",this.move),this.element.removeEventListener("touchend",this.end),this.element.removeEventListener("touchcancel",this.cancel),this.rotate.del(),this.touchStart.del(),this.multipointStart.del(),this.multipointEnd.del(),this.pinch.del(),this.swipe.del(),this.tap.del(),this.doubleTap.del(),this.longTap.del(),this.singleTap.del(),this.pressMove.del(),this.twoFingerPressMove.del(),this.touchMove.del(),this.touchEnd.del(),this.touchCancel.del(),this.preV=this.pinchStartLen=this.zoom=this.isDoubleTap=this.delta=this.last=this.now=this.tapTimeout=this.singleTapTimeout=this.longTapTimeout=this.swipeTimeout=this.x1=this.x2=this.y1=this.y2=this.preTapPosition=this.rotate=this.touchStart=this.multipointStart=this.multipointEnd=this.pinch=this.swipe=this.tap=this.doubleTap=this.longTap=this.singleTap=this.pressMove=this.touchMove=this.touchEnd=this.touchCancel=this.twoFingerPressMove=null,window.removeEventListener("scroll",this._cancelAllHandler),null}}]),e}();function W(e){var t=function(){var e,t=document.createElement("fakeelement"),i={transition:"transitionend",OTransition:"oTransitionEnd",MozTransition:"transitionend",WebkitTransition:"webkitTransitionEnd"};for(e in i)if(void 0!==t.style[e])return i[e]}(),i=window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth,n=c(e,"gslide-media")?e:e.querySelector(".gslide-media"),s=u(n,".ginner-container"),l=e.querySelector(".gslide-description");i>769&&(n=s),h(n,"greset"),v(n,"translate3d(0, 0, 0)"),a(t,{onElement:n,once:!0,withCallback:function(e,t){d(n,"greset")}}),n.style.opacity="",l&&(l.style.opacity="")}function B(e){if(e.events.hasOwnProperty("touch"))return!1;var t,i,n,s=y(),l=s.width,o=s.height,r=!1,a=null,g=null,f=null,p=!1,m=1,x=1,b=!1,S=!1,w=null,T=null,C=null,k=null,E=0,A=0,L=!1,I=!1,O={},P={},M=0,z=0,X=document.getElementById("glightbox-slider"),Y=document.querySelector(".goverlay"),q=new _(X,{touchStart:function(t){if(r=!0,(c(t.targetTouches[0].target,"ginner-container")||u(t.targetTouches[0].target,".gslide-desc")||"a"==t.targetTouches[0].target.nodeName.toLowerCase())&&(r=!1),u(t.targetTouches[0].target,".gslide-inline")&&!c(t.targetTouches[0].target.parentNode,"gslide-inline")&&(r=!1),r){if(P=t.targetTouches[0],O.pageX=t.targetTouches[0].pageX,O.pageY=t.targetTouches[0].pageY,M=t.targetTouches[0].clientX,z=t.targetTouches[0].clientY,a=e.activeSlide,g=a.querySelector(".gslide-media"),n=a.querySelector(".gslide-inline"),f=null,c(g,"gslide-image")&&(f=g.querySelector("img")),(window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth)>769&&(g=a.querySelector(".ginner-container")),d(Y,"greset"),t.pageX>20&&t.pageXo){var a=O.pageX-P.pageX;if(Math.abs(a)<=13)return!1}p=!0;var h,d=s.targetTouches[0].clientX,c=s.targetTouches[0].clientY,u=M-d,m=z-c;if(Math.abs(u)>Math.abs(m)?(L=!1,I=!0):(I=!1,L=!0),t=P.pageX-O.pageX,E=100*t/l,i=P.pageY-O.pageY,A=100*i/o,L&&f&&(h=1-Math.abs(i)/o,Y.style.opacity=h,e.settings.touchFollowAxis&&(E=0)),I&&(h=1-Math.abs(t)/l,g.style.opacity=h,e.settings.touchFollowAxis&&(A=0)),!f)return v(g,"translate3d(".concat(E,"%, 0, 0)"));v(g,"translate3d(".concat(E,"%, ").concat(A,"%, 0)"))}},touchEnd:function(){if(r){if(p=!1,S||b)return C=w,void(k=T);var t=Math.abs(parseInt(A)),i=Math.abs(parseInt(E));if(!(t>29&&f))return t<29&&i<25?(h(Y,"greset"),Y.style.opacity=1,W(g)):void 0;e.close()}},multipointEnd:function(){setTimeout((function(){b=!1}),50)},multipointStart:function(){b=!0,m=x||1},pinch:function(e){if(!f||p)return!1;b=!0,f.scaleX=f.scaleY=m*e.zoom;var t=m*e.zoom;if(S=!0,t<=1)return S=!1,t=1,k=null,C=null,w=null,T=null,void f.setAttribute("style","");t>4.5&&(t=4.5),f.style.transform="scale3d(".concat(t,", ").concat(t,", 1)"),x=t},pressMove:function(e){if(S&&!b){var t=P.pageX-O.pageX,i=P.pageY-O.pageY;C&&(t+=C),k&&(i+=k),w=t,T=i;var n="translate3d(".concat(t,"px, ").concat(i,"px, 0)");x&&(n+=" scale3d(".concat(x,", ").concat(x,", 1)")),v(f,n)}},swipe:function(t){if(!S)if(b)b=!1;else{if("Left"==t.direction){if(e.index==e.elements.length-1)return W(g);e.nextSlide()}if("Right"==t.direction){if(0==e.index)return W(g);e.prevSlide()}}}});e.events.touch=q}var H=function(){function e(i,n){var s=this,l=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;if(t(this,e),this.img=i,this.slide=n,this.onclose=l,this.img.setZoomEvents)return!1;this.active=!1,this.zoomedIn=!1,this.dragging=!1,this.currentX=null,this.currentY=null,this.initialX=null,this.initialY=null,this.xOffset=0,this.yOffset=0,this.img.addEventListener("mousedown",(function(e){return s.dragStart(e)}),!1),this.img.addEventListener("mouseup",(function(e){return s.dragEnd(e)}),!1),this.img.addEventListener("mousemove",(function(e){return s.drag(e)}),!1),this.img.addEventListener("click",(function(e){return s.slide.classList.contains("dragging-nav")?(s.zoomOut(),!1):s.zoomedIn?void(s.zoomedIn&&!s.dragging&&s.zoomOut()):s.zoomIn()}),!1),this.img.setZoomEvents=!0}return n(e,[{key:"zoomIn",value:function(){var e=this.widowWidth();if(!(this.zoomedIn||e<=768)){var t=this.img;if(t.setAttribute("data-style",t.getAttribute("style")),t.style.maxWidth=t.naturalWidth+"px",t.style.maxHeight=t.naturalHeight+"px",t.naturalWidth>e){var i=e/2-t.naturalWidth/2;this.setTranslate(this.img.parentNode,i,0)}this.slide.classList.add("zoomed"),this.zoomedIn=!0}}},{key:"zoomOut",value:function(){this.img.parentNode.setAttribute("style",""),this.img.setAttribute("style",this.img.getAttribute("data-style")),this.slide.classList.remove("zoomed"),this.zoomedIn=!1,this.currentX=null,this.currentY=null,this.initialX=null,this.initialY=null,this.xOffset=0,this.yOffset=0,this.onclose&&"function"==typeof this.onclose&&this.onclose()}},{key:"dragStart",value:function(e){e.preventDefault(),this.zoomedIn?("touchstart"===e.type?(this.initialX=e.touches[0].clientX-this.xOffset,this.initialY=e.touches[0].clientY-this.yOffset):(this.initialX=e.clientX-this.xOffset,this.initialY=e.clientY-this.yOffset),e.target===this.img&&(this.active=!0,this.img.classList.add("dragging"))):this.active=!1}},{key:"dragEnd",value:function(e){var t=this;e.preventDefault(),this.initialX=this.currentX,this.initialY=this.currentY,this.active=!1,setTimeout((function(){t.dragging=!1,t.img.isDragging=!1,t.img.classList.remove("dragging")}),100)}},{key:"drag",value:function(e){this.active&&(e.preventDefault(),"touchmove"===e.type?(this.currentX=e.touches[0].clientX-this.initialX,this.currentY=e.touches[0].clientY-this.initialY):(this.currentX=e.clientX-this.initialX,this.currentY=e.clientY-this.initialY),this.xOffset=this.currentX,this.yOffset=this.currentY,this.img.isDragging=!0,this.dragging=!0,this.setTranslate(this.img,this.currentX,this.currentY))}},{key:"onMove",value:function(e){if(this.zoomedIn){var t=e.clientX-this.img.naturalWidth/2,i=e.clientY-this.img.naturalHeight/2;this.setTranslate(this.img,t,i)}}},{key:"setTranslate",value:function(e,t,i){e.style.transform="translate3d("+t+"px, "+i+"px, 0)"}},{key:"widowWidth",value:function(){return window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth}}]),e}(),V=function(){function e(){var i=this,n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,e);var s=n.dragEl,l=n.toleranceX,o=void 0===l?40:l,r=n.toleranceY,a=void 0===r?65:r,h=n.slide,d=void 0===h?null:h,c=n.instance,u=void 0===c?null:c;this.el=s,this.active=!1,this.dragging=!1,this.currentX=null,this.currentY=null,this.initialX=null,this.initialY=null,this.xOffset=0,this.yOffset=0,this.direction=null,this.lastDirection=null,this.toleranceX=o,this.toleranceY=a,this.toleranceReached=!1,this.dragContainer=this.el,this.slide=d,this.instance=u,this.el.addEventListener("mousedown",(function(e){return i.dragStart(e)}),!1),this.el.addEventListener("mouseup",(function(e){return i.dragEnd(e)}),!1),this.el.addEventListener("mousemove",(function(e){return i.drag(e)}),!1)}return n(e,[{key:"dragStart",value:function(e){if(this.slide.classList.contains("zoomed"))this.active=!1;else{"touchstart"===e.type?(this.initialX=e.touches[0].clientX-this.xOffset,this.initialY=e.touches[0].clientY-this.yOffset):(this.initialX=e.clientX-this.xOffset,this.initialY=e.clientY-this.yOffset);var t=e.target.nodeName.toLowerCase();e.target.classList.contains("nodrag")||u(e.target,".nodrag")||-1!==["input","select","textarea","button","a"].indexOf(t)?this.active=!1:(e.preventDefault(),(e.target===this.el||"img"!==t&&u(e.target,".gslide-inline"))&&(this.active=!0,this.el.classList.add("dragging"),this.dragContainer=u(e.target,".ginner-container")))}}},{key:"dragEnd",value:function(e){var t=this;e&&e.preventDefault(),this.initialX=0,this.initialY=0,this.currentX=null,this.currentY=null,this.initialX=null,this.initialY=null,this.xOffset=0,this.yOffset=0,this.active=!1,this.doSlideChange&&(this.instance.preventOutsideClick=!0,"right"==this.doSlideChange&&this.instance.prevSlide(),"left"==this.doSlideChange&&this.instance.nextSlide()),this.doSlideClose&&this.instance.close(),this.toleranceReached||this.setTranslate(this.dragContainer,0,0,!0),setTimeout((function(){t.instance.preventOutsideClick=!1,t.toleranceReached=!1,t.lastDirection=null,t.dragging=!1,t.el.isDragging=!1,t.el.classList.remove("dragging"),t.slide.classList.remove("dragging-nav"),t.dragContainer.style.transform="",t.dragContainer.style.transition=""}),100)}},{key:"drag",value:function(e){if(this.active){e.preventDefault(),this.slide.classList.add("dragging-nav"),"touchmove"===e.type?(this.currentX=e.touches[0].clientX-this.initialX,this.currentY=e.touches[0].clientY-this.initialY):(this.currentX=e.clientX-this.initialX,this.currentY=e.clientY-this.initialY),this.xOffset=this.currentX,this.yOffset=this.currentY,this.el.isDragging=!0,this.dragging=!0,this.doSlideChange=!1,this.doSlideClose=!1;var t=Math.abs(this.currentX),i=Math.abs(this.currentY);if(t>0&&t>=Math.abs(this.currentY)&&(!this.lastDirection||"x"==this.lastDirection)){this.yOffset=0,this.lastDirection="x",this.setTranslate(this.dragContainer,this.currentX,0);var n=this.shouldChange();if(!this.instance.settings.dragAutoSnap&&n&&(this.doSlideChange=n),this.instance.settings.dragAutoSnap&&n)return this.instance.preventOutsideClick=!0,this.toleranceReached=!0,this.active=!1,this.instance.preventOutsideClick=!0,this.dragEnd(null),"right"==n&&this.instance.prevSlide(),void("left"==n&&this.instance.nextSlide())}if(this.toleranceY>0&&i>0&&i>=t&&(!this.lastDirection||"y"==this.lastDirection)){this.xOffset=0,this.lastDirection="y",this.setTranslate(this.dragContainer,0,this.currentY);var s=this.shouldClose();return!this.instance.settings.dragAutoSnap&&s&&(this.doSlideClose=!0),void(this.instance.settings.dragAutoSnap&&s&&this.instance.close())}}}},{key:"shouldChange",value:function(){var e=!1;if(Math.abs(this.currentX)>=this.toleranceX){var t=this.currentX>0?"right":"left";("left"==t&&this.slide!==this.slide.parentNode.lastChild||"right"==t&&this.slide!==this.slide.parentNode.firstChild)&&(e=t)}return e}},{key:"shouldClose",value:function(){var e=!1;return Math.abs(this.currentY)>=this.toleranceY&&(e=!0),e}},{key:"setTranslate",value:function(e,t,i){var n=arguments.length>3&&void 0!==arguments[3]&&arguments[3];e.style.transition=n?"all .2s ease":"",e.style.transform="translate3d(".concat(t,"px, ").concat(i,"px, 0)")}}]),e}();function j(e,t,i,n){var s=e.querySelector(".gslide-media"),l=new Image,o="gSlideTitle_"+i,r="gSlideDesc_"+i;l.addEventListener("load",(function(){T(n)&&n()}),!1),l.src=t.href,""!=t.sizes&&""!=t.srcset&&(l.sizes=t.sizes,l.srcset=t.srcset),l.alt="",I(t.alt)||""===t.alt||(l.alt=t.alt),""!==t.title&&l.setAttribute("aria-labelledby",o),""!==t.description&&l.setAttribute("aria-describedby",r),t.hasOwnProperty("_hasCustomWidth")&&t._hasCustomWidth&&(l.style.width=t.width),t.hasOwnProperty("_hasCustomHeight")&&t._hasCustomHeight&&(l.style.height=t.height),s.insertBefore(l,s.firstChild)}function F(e,t,i,n){var s=this,l=e.querySelector(".ginner-container"),o="gvideo"+i,r=e.querySelector(".gslide-media"),a=this.getAllPlayers();h(l,"gvideo-container"),r.insertBefore(m('
      '),r.firstChild);var d=e.querySelector(".gvideo-wrapper");S(this.settings.plyr.css,"Plyr");var c=t.href,u=location.protocol.replace(":",""),g="",v="",f=!1;"file"==u&&(u="http"),r.style.maxWidth=t.width,S(this.settings.plyr.js,"Plyr",(function(){if(c.match(/vimeo\.com\/([0-9]*)/)){var l=/vimeo.*\/(\d+)/i.exec(c);g="vimeo",v=l[1]}if(c.match(/(youtube\.com|youtube-nocookie\.com)\/watch\?v=([a-zA-Z0-9\-_]+)/)||c.match(/youtu\.be\/([a-zA-Z0-9\-_]+)/)||c.match(/(youtube\.com|youtube-nocookie\.com)\/embed\/([a-zA-Z0-9\-_]+)/)){var r=function(e){var t="";t=void 0!==(e=e.replace(/(>|<)/gi,"").split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/))[2]?(t=e[2].split(/[^0-9a-z_\-]/i))[0]:e;return t}(c);g="youtube",v=r}if(null!==c.match(/\.(mp4|ogg|webm|mov)$/)){g="local";var u='")}var w=f||m('
      '));h(d,"".concat(g,"-video gvideo")),d.appendChild(w),d.setAttribute("data-id",o),d.setAttribute("data-index",i);var C=O(s.settings.plyr,"config")?s.settings.plyr.config:{},k=new Plyr("#"+o,C);k.on("ready",(function(e){var t=e.detail.plyr;a[o]=t,T(n)&&n()})),b((function(){return e.querySelector("iframe")&&"true"==e.querySelector("iframe").dataset.ready}),(function(){s.resize(e)})),k.on("enterfullscreen",R),k.on("exitfullscreen",R)}))}function R(e){var t=u(e.target,".gslide-media");"enterfullscreen"==e.type&&h(t,"fullscreen"),"exitfullscreen"==e.type&&d(t,"fullscreen")}function G(e,t,i,n){var s,l=this,o=e.querySelector(".gslide-media"),r=!(!O(t,"href")||!t.href)&&t.href.split("#").pop().trim(),d=!(!O(t,"content")||!t.content)&&t.content;if(d&&(C(d)&&(s=m('
      '.concat(d,"
      "))),k(d))){"none"==d.style.display&&(d.style.display="block");var c=document.createElement("div");c.className="ginlined-content",c.appendChild(d),s=c}if(r){var u=document.getElementById(r);if(!u)return!1;var g=u.cloneNode(!0);g.style.height=t.height,g.style.maxWidth=t.width,h(g,"ginlined-content"),s=g}if(!s)return console.error("Unable to append inline slide content",t),!1;o.style.height=t.height,o.style.width=t.width,o.appendChild(s),this.events["inlineclose"+r]=a("click",{onElement:o.querySelectorAll(".gtrigger-close"),withCallback:function(e){e.preventDefault(),l.close()}}),T(n)&&n()}function Z(e,t,i,n){var s=e.querySelector(".gslide-media"),l=function(e){var t=e.url,i=e.allow,n=e.callback,s=e.appendTo,l=document.createElement("iframe");return l.className="vimeo-video gvideo",l.src=t,l.style.width="100%",l.style.height="100%",i&&l.setAttribute("allow",i),l.onload=function(){h(l,"node-ready"),T(n)&&n()},s&&s.appendChild(l),l}({url:t.href,callback:n});s.parentNode.style.maxWidth=t.width,s.parentNode.style.height=t.height,s.appendChild(l)}var $=function(){function e(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,e),this.defaults={href:"",sizes:"",srcset:"",title:"",type:"",description:"",alt:"",descPosition:"bottom",effect:"",width:"",height:"",content:!1,zoomable:!0,draggable:!0},L(i)&&(this.defaults=l(this.defaults,i))}return n(e,[{key:"sourceType",value:function(e){var t=e;if(null!==(e=e.toLowerCase()).match(/\.(jpeg|jpg|jpe|gif|png|apn|webp|avif|svg)/))return"image";if(e.match(/(youtube\.com|youtube-nocookie\.com)\/watch\?v=([a-zA-Z0-9\-_]+)/)||e.match(/youtu\.be\/([a-zA-Z0-9\-_]+)/)||e.match(/(youtube\.com|youtube-nocookie\.com)\/embed\/([a-zA-Z0-9\-_]+)/))return"video";if(e.match(/vimeo\.com\/([0-9]*)/))return"video";if(null!==e.match(/\.(mp4|ogg|webm|mov)/))return"video";if(null!==e.match(/\.(mp3|wav|wma|aac|ogg)/))return"audio";if(e.indexOf("#")>-1&&""!==t.split("#").pop().trim())return"inline";return e.indexOf("goajax=true")>-1?"ajax":"external"}},{key:"parseConfig",value:function(e,t){var i=this,n=l({descPosition:t.descPosition},this.defaults);if(L(e)&&!k(e)){O(e,"type")||(O(e,"content")&&e.content?e.type="inline":O(e,"href")&&(e.type=this.sourceType(e.href)));var s=l(n,e);return this.setSize(s,t),s}var r="",a=e.getAttribute("data-glightbox"),h=e.nodeName.toLowerCase();if("a"===h&&(r=e.href),"img"===h&&(r=e.src,n.alt=e.alt),n.href=r,o(n,(function(s,l){O(t,l)&&"width"!==l&&(n[l]=t[l]);var o=e.dataset[l];I(o)||(n[l]=i.sanitizeValue(o))})),n.content&&(n.type="inline"),!n.type&&r&&(n.type=this.sourceType(r)),I(a)){if(!n.title&&"a"==h){var d=e.title;I(d)||""===d||(n.title=d)}if(!n.title&&"img"==h){var c=e.alt;I(c)||""===c||(n.title=c)}}else{var u=[];o(n,(function(e,t){u.push(";\\s?"+t)})),u=u.join("\\s?:|"),""!==a.trim()&&o(n,(function(e,t){var s=a,l=new RegExp("s?"+t+"s?:s?(.*?)("+u+"s?:|$)"),o=s.match(l);if(o&&o.length&&o[1]){var r=o[1].trim().replace(/;\s*$/,"");n[t]=i.sanitizeValue(r)}}))}if(n.description&&"."===n.description.substring(0,1)){var g;try{g=document.querySelector(n.description).innerHTML}catch(e){if(!(e instanceof DOMException))throw e}g&&(n.description=g)}if(!n.description){var v=e.querySelector(".glightbox-desc");v&&(n.description=v.innerHTML)}return this.setSize(n,t,e),this.slideConfig=n,n}},{key:"setSize",value:function(e,t){var i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null,n="video"==e.type?this.checkSize(t.videosWidth):this.checkSize(t.width),s=this.checkSize(t.height);return e.width=O(e,"width")&&""!==e.width?this.checkSize(e.width):n,e.height=O(e,"height")&&""!==e.height?this.checkSize(e.height):s,i&&"image"==e.type&&(e._hasCustomWidth=!!i.dataset.width,e._hasCustomHeight=!!i.dataset.height),e}},{key:"checkSize",value:function(e){return M(e)?"".concat(e,"px"):e}},{key:"sanitizeValue",value:function(e){return"true"!==e&&"false"!==e?e:"true"===e}}]),e}(),U=function(){function e(i,n,s){t(this,e),this.element=i,this.instance=n,this.index=s}return n(e,[{key:"setContent",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,i=arguments.length>1&&void 0!==arguments[1]&&arguments[1];if(c(t,"loaded"))return!1;var n=this.instance.settings,s=this.slideConfig,l=w();T(n.beforeSlideLoad)&&n.beforeSlideLoad({index:this.index,slide:t,player:!1});var o=s.type,r=s.descPosition,a=t.querySelector(".gslide-media"),d=t.querySelector(".gslide-title"),u=t.querySelector(".gslide-desc"),g=t.querySelector(".gdesc-inner"),v=i,f="gSlideTitle_"+this.index,p="gSlideDesc_"+this.index;if(T(n.afterSlideLoad)&&(v=function(){T(i)&&i(),n.afterSlideLoad({index:e.index,slide:t,player:e.instance.getSlidePlayerInstance(e.index)})}),""==s.title&&""==s.description?g&&g.parentNode.parentNode.removeChild(g.parentNode):(d&&""!==s.title?(d.id=f,d.innerHTML=s.title):d.parentNode.removeChild(d),u&&""!==s.description?(u.id=p,l&&n.moreLength>0?(s.smallDescription=this.slideShortDesc(s.description,n.moreLength,n.moreText),u.innerHTML=s.smallDescription,this.descriptionEvents(u,s)):u.innerHTML=s.description):u.parentNode.removeChild(u),h(a.parentNode,"desc-".concat(r)),h(g.parentNode,"description-".concat(r))),h(a,"gslide-".concat(o)),h(t,"loaded"),"video"!==o){if("external"!==o)return"inline"===o?(G.apply(this.instance,[t,s,this.index,v]),void(s.draggable&&new V({dragEl:t.querySelector(".gslide-inline"),toleranceX:n.dragToleranceX,toleranceY:n.dragToleranceY,slide:t,instance:this.instance}))):void("image"!==o?T(v)&&v():j(t,s,this.index,(function(){var i=t.querySelector("img");s.draggable&&new V({dragEl:i,toleranceX:n.dragToleranceX,toleranceY:n.dragToleranceY,slide:t,instance:e.instance}),s.zoomable&&i.naturalWidth>i.offsetWidth&&(h(i,"zoomable"),new H(i,t,(function(){e.instance.resize()}))),T(v)&&v()})));Z.apply(this,[t,s,this.index,v])}else F.apply(this.instance,[t,s,this.index,v])}},{key:"slideShortDesc",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:50,i=arguments.length>2&&void 0!==arguments[2]&&arguments[2],n=document.createElement("div");n.innerHTML=e;var s=n.innerText,l=i;if((e=s.trim()).length<=t)return e;var o=e.substr(0,t-1);return l?(n=null,o+'... '+i+""):o}},{key:"descriptionEvents",value:function(e,t){var i=this,n=e.querySelector(".desc-more");if(!n)return!1;a("click",{onElement:n,withCallback:function(e,n){e.preventDefault();var s=document.body,l=u(n,".gslide-desc");if(!l)return!1;l.innerHTML=t.description,h(s,"gdesc-open");var o=a("click",{onElement:[s,u(l,".gslide-description")],withCallback:function(e,n){"a"!==e.target.nodeName.toLowerCase()&&(d(s,"gdesc-open"),h(s,"gdesc-closed"),l.innerHTML=t.smallDescription,i.descriptionEvents(l,t),setTimeout((function(){d(s,"gdesc-closed")}),400),o.destroy())}})}})}},{key:"create",value:function(){return m(this.instance.settings.slideHTML)}},{key:"getConfig",value:function(){k(this.element)||this.element.hasOwnProperty("draggable")||(this.element.draggable=this.instance.settings.draggable);var e=new $(this.instance.settings.slideExtraAttributes);return this.slideConfig=e.parseConfig(this.element,this.instance.settings),this.slideConfig}}]),e}(),J=w(),K=null!==w()||void 0!==document.createTouch||"ontouchstart"in window||"onmsgesturechange"in window||navigator.msMaxTouchPoints,Q=document.getElementsByTagName("html")[0],ee={selector:".glightbox",elements:null,skin:"clean",theme:"clean",closeButton:!0,startAt:null,autoplayVideos:!0,autofocusVideos:!0,descPosition:"bottom",width:"900px",height:"506px",videosWidth:"960px",beforeSlideChange:null,afterSlideChange:null,beforeSlideLoad:null,afterSlideLoad:null,slideInserted:null,slideRemoved:null,slideExtraAttributes:null,onOpen:null,onClose:null,loop:!1,zoomable:!0,draggable:!0,dragAutoSnap:!1,dragToleranceX:40,dragToleranceY:65,preload:!0,oneSlidePerOpen:!1,touchNavigation:!0,touchFollowAxis:!0,keyboardNavigation:!0,closeOnOutsideClick:!0,plugins:!1,plyr:{css:"https://cdn.plyr.io/3.6.8/plyr.css",js:"https://cdn.plyr.io/3.6.8/plyr.js",config:{ratio:"16:9",fullscreen:{enabled:!0,iosNative:!0},youtube:{noCookie:!0,rel:0,showinfo:0,iv_load_policy:3},vimeo:{byline:!1,portrait:!1,title:!1,transparent:!1}}},openEffect:"zoom",closeEffect:"zoom",slideEffect:"slide",moreText:"See more",moreLength:60,cssEfects:{fade:{in:"fadeIn",out:"fadeOut"},zoom:{in:"zoomIn",out:"zoomOut"},slide:{in:"slideInRight",out:"slideOutLeft"},slideBack:{in:"slideInLeft",out:"slideOutRight"},none:{in:"none",out:"none"}},svg:{close:'',next:' ',prev:''},slideHTML:'
      \n
      \n
      \n
      \n
      \n
      \n
      \n

      \n
      \n
      \n
      \n
      \n
      \n
      ',lightboxHTML:''},te=function(){function e(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,e),this.customOptions=i,this.settings=l(ee,i),this.effectsClasses=this.getAnimationClasses(),this.videoPlayers={},this.apiEvents=[],this.fullElementsList=!1}return n(e,[{key:"init",value:function(){var e=this,t=this.getSelector();t&&(this.baseEvents=a("click",{onElement:t,withCallback:function(t,i){t.preventDefault(),e.open(i)}})),this.elements=this.getElements()}},{key:"open",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;if(0==this.elements.length)return!1;this.activeSlide=null,this.prevActiveSlideIndex=null,this.prevActiveSlide=null;var i=M(t)?t:this.settings.startAt;if(k(e)){var n=e.getAttribute("data-gallery");n&&(this.fullElementsList=this.elements,this.elements=this.getGalleryElements(this.elements,n)),I(i)&&(i=this.getElementIndex(e))<0&&(i=0)}M(i)||(i=0),this.build(),g(this.overlay,"none"==this.settings.openEffect?"none":this.settings.cssEfects.fade.in);var s=document.body,l=window.innerWidth-document.documentElement.clientWidth;if(l>0){var o=document.createElement("style");o.type="text/css",o.className="gcss-styles",o.innerText=".gscrollbar-fixer {margin-right: ".concat(l,"px}"),document.head.appendChild(o),h(s,"gscrollbar-fixer")}h(s,"glightbox-open"),h(Q,"glightbox-open"),J&&(h(document.body,"glightbox-mobile"),this.settings.slideEffect="slide"),this.showSlide(i,!0),1==this.elements.length?(h(this.prevButton,"glightbox-button-hidden"),h(this.nextButton,"glightbox-button-hidden")):(d(this.prevButton,"glightbox-button-hidden"),d(this.nextButton,"glightbox-button-hidden")),this.lightboxOpen=!0,this.trigger("open"),T(this.settings.onOpen)&&this.settings.onOpen(),K&&this.settings.touchNavigation&&B(this),this.settings.keyboardNavigation&&X(this)}},{key:"openAt",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;this.open(null,e)}},{key:"showSlide",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,i=arguments.length>1&&void 0!==arguments[1]&&arguments[1];f(this.loader),this.index=parseInt(t);var n=this.slidesContainer.querySelector(".current");n&&d(n,"current"),this.slideAnimateOut();var s=this.slidesContainer.querySelectorAll(".gslide")[t];if(c(s,"loaded"))this.slideAnimateIn(s,i),p(this.loader);else{f(this.loader);var l=this.elements[t],o={index:this.index,slide:s,slideNode:s,slideConfig:l.slideConfig,slideIndex:this.index,trigger:l.node,player:null};this.trigger("slide_before_load",o),l.instance.setContent(s,(function(){p(e.loader),e.resize(),e.slideAnimateIn(s,i),e.trigger("slide_after_load",o)}))}this.slideDescription=s.querySelector(".gslide-description"),this.slideDescriptionContained=this.slideDescription&&c(this.slideDescription.parentNode,"gslide-media"),this.settings.preload&&(this.preloadSlide(t+1),this.preloadSlide(t-1)),this.updateNavigationClasses(),this.activeSlide=s}},{key:"preloadSlide",value:function(e){var t=this;if(e<0||e>this.elements.length-1)return!1;if(I(this.elements[e]))return!1;var i=this.slidesContainer.querySelectorAll(".gslide")[e];if(c(i,"loaded"))return!1;var n=this.elements[e],s=n.type,l={index:e,slide:i,slideNode:i,slideConfig:n.slideConfig,slideIndex:e,trigger:n.node,player:null};this.trigger("slide_before_load",l),"video"==s||"external"==s?setTimeout((function(){n.instance.setContent(i,(function(){t.trigger("slide_after_load",l)}))}),200):n.instance.setContent(i,(function(){t.trigger("slide_after_load",l)}))}},{key:"prevSlide",value:function(){this.goToSlide(this.index-1)}},{key:"nextSlide",value:function(){this.goToSlide(this.index+1)}},{key:"goToSlide",value:function(){var e=arguments.length>0&&void 0!==arguments[0]&&arguments[0];if(this.prevActiveSlide=this.activeSlide,this.prevActiveSlideIndex=this.index,!this.loop()&&(e<0||e>this.elements.length-1))return!1;e<0?e=this.elements.length-1:e>=this.elements.length&&(e=0),this.showSlide(e)}},{key:"insertSlide",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:-1;t<0&&(t=this.elements.length);var i=new U(e,this,t),n=i.getConfig(),s=l({},n),o=i.create(),r=this.elements.length-1;s.index=t,s.node=!1,s.instance=i,s.slideConfig=n,this.elements.splice(t,0,s);var a=null,h=null;if(this.slidesContainer){if(t>r)this.slidesContainer.appendChild(o);else{var d=this.slidesContainer.querySelectorAll(".gslide")[t];this.slidesContainer.insertBefore(o,d)}(this.settings.preload&&0==this.index&&0==t||this.index-1==t||this.index+1==t)&&this.preloadSlide(t),0==this.index&&0==t&&(this.index=1),this.updateNavigationClasses(),a=this.slidesContainer.querySelectorAll(".gslide")[t],h=this.getSlidePlayerInstance(t),s.slideNode=a}this.trigger("slide_inserted",{index:t,slide:a,slideNode:a,slideConfig:n,slideIndex:t,trigger:null,player:h}),T(this.settings.slideInserted)&&this.settings.slideInserted({index:t,slide:a,player:h})}},{key:"removeSlide",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:-1;if(e<0||e>this.elements.length-1)return!1;var t=this.slidesContainer&&this.slidesContainer.querySelectorAll(".gslide")[e];t&&(this.getActiveSlideIndex()==e&&(e==this.elements.length-1?this.prevSlide():this.nextSlide()),t.parentNode.removeChild(t)),this.elements.splice(e,1),this.trigger("slide_removed",e),T(this.settings.slideRemoved)&&this.settings.slideRemoved(e)}},{key:"slideAnimateIn",value:function(e,t){var i=this,n=e.querySelector(".gslide-media"),s=e.querySelector(".gslide-description"),l={index:this.prevActiveSlideIndex,slide:this.prevActiveSlide,slideNode:this.prevActiveSlide,slideIndex:this.prevActiveSlide,slideConfig:I(this.prevActiveSlideIndex)?null:this.elements[this.prevActiveSlideIndex].slideConfig,trigger:I(this.prevActiveSlideIndex)?null:this.elements[this.prevActiveSlideIndex].node,player:this.getSlidePlayerInstance(this.prevActiveSlideIndex)},o={index:this.index,slide:this.activeSlide,slideNode:this.activeSlide,slideConfig:this.elements[this.index].slideConfig,slideIndex:this.index,trigger:this.elements[this.index].node,player:this.getSlidePlayerInstance(this.index)};if(n.offsetWidth>0&&s&&(p(s),s.style.display=""),d(e,this.effectsClasses),t)g(e,this.settings.cssEfects[this.settings.openEffect].in,(function(){i.settings.autoplayVideos&&i.slidePlayerPlay(e),i.trigger("slide_changed",{prev:l,current:o}),T(i.settings.afterSlideChange)&&i.settings.afterSlideChange.apply(i,[l,o])}));else{var r=this.settings.slideEffect,a="none"!==r?this.settings.cssEfects[r].in:r;this.prevActiveSlideIndex>this.index&&"slide"==this.settings.slideEffect&&(a=this.settings.cssEfects.slideBack.in),g(e,a,(function(){i.settings.autoplayVideos&&i.slidePlayerPlay(e),i.trigger("slide_changed",{prev:l,current:o}),T(i.settings.afterSlideChange)&&i.settings.afterSlideChange.apply(i,[l,o])}))}setTimeout((function(){i.resize(e)}),100),h(e,"current")}},{key:"slideAnimateOut",value:function(){if(!this.prevActiveSlide)return!1;var e=this.prevActiveSlide;d(e,this.effectsClasses),h(e,"prev");var t=this.settings.slideEffect,i="none"!==t?this.settings.cssEfects[t].out:t;this.slidePlayerPause(e),this.trigger("slide_before_change",{prev:{index:this.prevActiveSlideIndex,slide:this.prevActiveSlide,slideNode:this.prevActiveSlide,slideIndex:this.prevActiveSlideIndex,slideConfig:I(this.prevActiveSlideIndex)?null:this.elements[this.prevActiveSlideIndex].slideConfig,trigger:I(this.prevActiveSlideIndex)?null:this.elements[this.prevActiveSlideIndex].node,player:this.getSlidePlayerInstance(this.prevActiveSlideIndex)},current:{index:this.index,slide:this.activeSlide,slideNode:this.activeSlide,slideIndex:this.index,slideConfig:this.elements[this.index].slideConfig,trigger:this.elements[this.index].node,player:this.getSlidePlayerInstance(this.index)}}),T(this.settings.beforeSlideChange)&&this.settings.beforeSlideChange.apply(this,[{index:this.prevActiveSlideIndex,slide:this.prevActiveSlide,player:this.getSlidePlayerInstance(this.prevActiveSlideIndex)},{index:this.index,slide:this.activeSlide,player:this.getSlidePlayerInstance(this.index)}]),this.prevActiveSlideIndex>this.index&&"slide"==this.settings.slideEffect&&(i=this.settings.cssEfects.slideBack.out),g(e,i,(function(){var t=e.querySelector(".ginner-container"),i=e.querySelector(".gslide-media"),n=e.querySelector(".gslide-description");t.style.transform="",i.style.transform="",d(i,"greset"),i.style.opacity="",n&&(n.style.opacity=""),d(e,"prev")}))}},{key:"getAllPlayers",value:function(){return this.videoPlayers}},{key:"getSlidePlayerInstance",value:function(e){var t="gvideo"+e,i=this.getAllPlayers();return!(!O(i,t)||!i[t])&&i[t]}},{key:"stopSlideVideo",value:function(e){if(k(e)){var t=e.querySelector(".gvideo-wrapper");t&&(e=t.getAttribute("data-index"))}console.log("stopSlideVideo is deprecated, use slidePlayerPause");var i=this.getSlidePlayerInstance(e);i&&i.playing&&i.pause()}},{key:"slidePlayerPause",value:function(e){if(k(e)){var t=e.querySelector(".gvideo-wrapper");t&&(e=t.getAttribute("data-index"))}var i=this.getSlidePlayerInstance(e);i&&i.playing&&i.pause()}},{key:"playSlideVideo",value:function(e){if(k(e)){var t=e.querySelector(".gvideo-wrapper");t&&(e=t.getAttribute("data-index"))}console.log("playSlideVideo is deprecated, use slidePlayerPlay");var i=this.getSlidePlayerInstance(e);i&&!i.playing&&i.play()}},{key:"slidePlayerPlay",value:function(e){if(k(e)){var t=e.querySelector(".gvideo-wrapper");t&&(e=t.getAttribute("data-index"))}var i=this.getSlidePlayerInstance(e);i&&!i.playing&&(i.play(),this.settings.autofocusVideos&&i.elements.container.focus())}},{key:"setElements",value:function(e){var t=this;this.settings.elements=!1;var i=[];e&&e.length&&o(e,(function(e,n){var s=new U(e,t,n),o=s.getConfig(),r=l({},o);r.slideConfig=o,r.instance=s,r.index=n,i.push(r)})),this.elements=i,this.lightboxOpen&&(this.slidesContainer.innerHTML="",this.elements.length&&(o(this.elements,(function(){var e=m(t.settings.slideHTML);t.slidesContainer.appendChild(e)})),this.showSlide(0,!0)))}},{key:"getElementIndex",value:function(e){var t=!1;return o(this.elements,(function(i,n){if(O(i,"node")&&i.node==e)return t=n,!0})),t}},{key:"getElements",value:function(){var e=this,t=[];this.elements=this.elements?this.elements:[],!I(this.settings.elements)&&E(this.settings.elements)&&this.settings.elements.length&&o(this.settings.elements,(function(i,n){var s=new U(i,e,n),o=s.getConfig(),r=l({},o);r.node=!1,r.index=n,r.instance=s,r.slideConfig=o,t.push(r)}));var i=!1;return this.getSelector()&&(i=document.querySelectorAll(this.getSelector())),i?(o(i,(function(i,n){var s=new U(i,e,n),o=s.getConfig(),r=l({},o);r.node=i,r.index=n,r.instance=s,r.slideConfig=o,r.gallery=i.getAttribute("data-gallery"),t.push(r)})),t):t}},{key:"getGalleryElements",value:function(e,t){return e.filter((function(e){return e.gallery==t}))}},{key:"getSelector",value:function(){return!this.settings.elements&&(this.settings.selector&&"data-"==this.settings.selector.substring(0,5)?"*[".concat(this.settings.selector,"]"):this.settings.selector)}},{key:"getActiveSlide",value:function(){return this.slidesContainer.querySelectorAll(".gslide")[this.index]}},{key:"getActiveSlideIndex",value:function(){return this.index}},{key:"getAnimationClasses",value:function(){var e=[];for(var t in this.settings.cssEfects)if(this.settings.cssEfects.hasOwnProperty(t)){var i=this.settings.cssEfects[t];e.push("g".concat(i.in)),e.push("g".concat(i.out))}return e.join(" ")}},{key:"build",value:function(){var e=this;if(this.built)return!1;var t=document.body.childNodes,i=[];o(t,(function(e){e.parentNode==document.body&&"#"!==e.nodeName.charAt(0)&&e.hasAttribute&&!e.hasAttribute("aria-hidden")&&(i.push(e),e.setAttribute("aria-hidden","true"))}));var n=O(this.settings.svg,"next")?this.settings.svg.next:"",s=O(this.settings.svg,"prev")?this.settings.svg.prev:"",l=O(this.settings.svg,"close")?this.settings.svg.close:"",r=this.settings.lightboxHTML;r=m(r=(r=(r=r.replace(/{nextSVG}/g,n)).replace(/{prevSVG}/g,s)).replace(/{closeSVG}/g,l)),document.body.appendChild(r);var d=document.getElementById("glightbox-body");this.modal=d;var g=d.querySelector(".gclose");this.prevButton=d.querySelector(".gprev"),this.nextButton=d.querySelector(".gnext"),this.overlay=d.querySelector(".goverlay"),this.loader=d.querySelector(".gloader"),this.slidesContainer=document.getElementById("glightbox-slider"),this.bodyHiddenChildElms=i,this.events={},h(this.modal,"glightbox-"+this.settings.skin),this.settings.closeButton&&g&&(this.events.close=a("click",{onElement:g,withCallback:function(t,i){t.preventDefault(),e.close()}})),g&&!this.settings.closeButton&&g.parentNode.removeChild(g),this.nextButton&&(this.events.next=a("click",{onElement:this.nextButton,withCallback:function(t,i){t.preventDefault(),e.nextSlide()}})),this.prevButton&&(this.events.prev=a("click",{onElement:this.prevButton,withCallback:function(t,i){t.preventDefault(),e.prevSlide()}})),this.settings.closeOnOutsideClick&&(this.events.outClose=a("click",{onElement:d,withCallback:function(t,i){e.preventOutsideClick||c(document.body,"glightbox-mobile")||u(t.target,".ginner-container")||u(t.target,".gbtn")||c(t.target,"gnext")||c(t.target,"gprev")||e.close()}})),o(this.elements,(function(t,i){e.slidesContainer.appendChild(t.instance.create()),t.slideNode=e.slidesContainer.querySelectorAll(".gslide")[i]})),K&&h(document.body,"glightbox-touch"),this.events.resize=a("resize",{onElement:window,withCallback:function(){e.resize()}}),this.built=!0}},{key:"resize",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;if((e=e||this.activeSlide)&&!c(e,"zoomed")){var t=y(),i=e.querySelector(".gvideo-wrapper"),n=e.querySelector(".gslide-image"),s=this.slideDescription,l=t.width,o=t.height;if(l<=768?h(document.body,"glightbox-mobile"):d(document.body,"glightbox-mobile"),i||n){var r=!1;if(s&&(c(s,"description-bottom")||c(s,"description-top"))&&!c(s,"gabsolute")&&(r=!0),n)if(l<=768)n.querySelector("img");else if(r){var a=s.offsetHeight,u=n.querySelector("img");u.setAttribute("style","max-height: calc(100vh - ".concat(a,"px)")),s.setAttribute("style","max-width: ".concat(u.offsetWidth,"px;"))}if(i){var g=O(this.settings.plyr.config,"ratio")?this.settings.plyr.config.ratio:"";if(!g){var v=i.clientWidth,f=i.clientHeight,p=v/f;g="".concat(v/p,":").concat(f/p)}var m=g.split(":"),x=this.settings.videosWidth,b=this.settings.videosWidth,S=(b=M(x)||-1!==x.indexOf("px")?parseInt(x):-1!==x.indexOf("vw")?l*parseInt(x)/100:-1!==x.indexOf("vh")?o*parseInt(x)/100:-1!==x.indexOf("%")?l*parseInt(x)/100:parseInt(i.clientWidth))/(parseInt(m[0])/parseInt(m[1]));if(S=Math.floor(S),r&&(o-=s.offsetHeight),b>l||S>o||ob){var w=i.offsetWidth,T=i.offsetHeight,C=o/T,k={width:w*C,height:T*C};i.parentNode.setAttribute("style","max-width: ".concat(k.width,"px")),r&&s.setAttribute("style","max-width: ".concat(k.width,"px;"))}else i.parentNode.style.maxWidth="".concat(x),r&&s.setAttribute("style","max-width: ".concat(x,";"))}}}}},{key:"reload",value:function(){this.init()}},{key:"updateNavigationClasses",value:function(){var e=this.loop();d(this.nextButton,"disabled"),d(this.prevButton,"disabled"),0==this.index&&this.elements.length-1==0?(h(this.prevButton,"disabled"),h(this.nextButton,"disabled")):0!==this.index||e?this.index!==this.elements.length-1||e||h(this.nextButton,"disabled"):h(this.prevButton,"disabled")}},{key:"loop",value:function(){var e=O(this.settings,"loopAtEnd")?this.settings.loopAtEnd:null;return e=O(this.settings,"loop")?this.settings.loop:e,e}},{key:"close",value:function(){var e=this;if(!this.lightboxOpen){if(this.events){for(var t in this.events)this.events.hasOwnProperty(t)&&this.events[t].destroy();this.events=null}return!1}if(this.closing)return!1;this.closing=!0,this.slidePlayerPause(this.activeSlide),this.fullElementsList&&(this.elements=this.fullElementsList),this.bodyHiddenChildElms.length&&o(this.bodyHiddenChildElms,(function(e){e.removeAttribute("aria-hidden")})),h(this.modal,"glightbox-closing"),g(this.overlay,"none"==this.settings.openEffect?"none":this.settings.cssEfects.fade.out),g(this.activeSlide,this.settings.cssEfects[this.settings.closeEffect].out,(function(){if(e.activeSlide=null,e.prevActiveSlideIndex=null,e.prevActiveSlide=null,e.built=!1,e.events){for(var t in e.events)e.events.hasOwnProperty(t)&&e.events[t].destroy();e.events=null}var i=document.body;d(Q,"glightbox-open"),d(i,"glightbox-open touching gdesc-open glightbox-touch glightbox-mobile gscrollbar-fixer"),e.modal.parentNode.removeChild(e.modal),e.trigger("close"),T(e.settings.onClose)&&e.settings.onClose();var n=document.querySelector(".gcss-styles");n&&n.parentNode.removeChild(n),e.lightboxOpen=!1,e.closing=null}))}},{key:"destroy",value:function(){this.close(),this.clearAllEvents(),this.baseEvents&&this.baseEvents.destroy()}},{key:"on",value:function(e,t){var i=arguments.length>2&&void 0!==arguments[2]&&arguments[2];if(!e||!T(t))throw new TypeError("Event name and callback must be defined");this.apiEvents.push({evt:e,once:i,callback:t})}},{key:"once",value:function(e,t){this.on(e,t,!0)}},{key:"trigger",value:function(e){var t=this,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,n=[];o(this.apiEvents,(function(t,s){var l=t.evt,o=t.once,r=t.callback;l==e&&(r(i),o&&n.push(s))})),n.length&&o(n,(function(e){return t.apiEvents.splice(e,1)}))}},{key:"clearAllEvents",value:function(){this.apiEvents.splice(0,this.apiEvents.length)}},{key:"version",value:function(){return"3.1.1"}}]),e}();return function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=new te(e);return t.init(),t}})); -/*! update cache: 20260104155757 */ +/*! update cache: 20260104161456 */ diff --git a/ja/assets/stylesheets/glightbox.min.css b/ja/assets/stylesheets/glightbox.min.css index 238f083e8..e6539fe5f 100644 --- a/ja/assets/stylesheets/glightbox.min.css +++ b/ja/assets/stylesheets/glightbox.min.css @@ -1,2 +1,2 @@ .glightbox-container{width:100%;height:100%;position:fixed;top:0;left:0;z-index:999999!important;overflow:hidden;-ms-touch-action:none;touch-action:none;-webkit-text-size-adjust:100%;-moz-text-size-adjust:100%;-ms-text-size-adjust:100%;text-size-adjust:100%;-webkit-backface-visibility:hidden;backface-visibility:hidden;outline:0}.glightbox-container.inactive{display:none}.glightbox-container .gcontainer{position:relative;width:100%;height:100%;z-index:9999;overflow:hidden}.glightbox-container .gslider{-webkit-transition:-webkit-transform .4s ease;transition:-webkit-transform .4s ease;transition:transform .4s ease;transition:transform .4s ease,-webkit-transform .4s ease;height:100%;left:0;top:0;width:100%;position:relative;overflow:hidden;display:-webkit-box!important;display:-ms-flexbox!important;display:flex!important;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}.glightbox-container .gslide{width:100%;position:absolute;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;opacity:0}.glightbox-container .gslide.current{opacity:1;z-index:99999;position:relative}.glightbox-container .gslide.prev{opacity:1;z-index:9999}.glightbox-container .gslide-inner-content{width:100%}.glightbox-container .ginner-container{position:relative;width:100%;display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column;max-width:100%;margin:auto;height:100vh}.glightbox-container .ginner-container.gvideo-container{width:100%}.glightbox-container .ginner-container.desc-bottom,.glightbox-container .ginner-container.desc-top{-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column}.glightbox-container .ginner-container.desc-left,.glightbox-container .ginner-container.desc-right{max-width:100%!important}.gslide iframe,.gslide video{outline:0!important;border:none;min-height:165px;-webkit-overflow-scrolling:touch;-ms-touch-action:auto;touch-action:auto}.gslide:not(.current){pointer-events:none}.gslide-image{-webkit-box-align:center;-ms-flex-align:center;align-items:center}.gslide-image img{max-height:100vh;display:block;padding:0;float:none;outline:0;border:none;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;max-width:100vw;width:auto;height:auto;-o-object-fit:cover;object-fit:cover;-ms-touch-action:none;touch-action:none;margin:auto;min-width:200px}.desc-bottom .gslide-image img,.desc-top .gslide-image img{width:auto}.desc-left .gslide-image img,.desc-right .gslide-image img{width:auto;max-width:100%}.gslide-image img.zoomable{position:relative}.gslide-image img.dragging{cursor:-webkit-grabbing!important;cursor:grabbing!important;-webkit-transition:none;transition:none}.gslide-video{position:relative;max-width:100vh;width:100%!important}.gslide-video .plyr__poster-enabled.plyr--loading .plyr__poster{display:none}.gslide-video .gvideo-wrapper{width:100%;margin:auto}.gslide-video::before{content:'';position:absolute;width:100%;height:100%;background:rgba(255,0,0,.34);display:none}.gslide-video.playing::before{display:none}.gslide-video.fullscreen{max-width:100%!important;min-width:100%;height:75vh}.gslide-video.fullscreen video{max-width:100%!important;width:100%!important}.gslide-inline{background:#fff;text-align:left;max-height:calc(100vh - 40px);overflow:auto;max-width:100%;margin:auto}.gslide-inline .ginlined-content{padding:20px;width:100%}.gslide-inline .dragging{cursor:-webkit-grabbing!important;cursor:grabbing!important;-webkit-transition:none;transition:none}.ginlined-content{overflow:auto;display:block!important;opacity:1}.gslide-external{display:-webkit-box;display:-ms-flexbox;display:flex;width:100%;min-width:100%;background:#fff;padding:0;overflow:auto;max-height:75vh;height:100%}.gslide-media{display:-webkit-box;display:-ms-flexbox;display:flex;width:auto}.zoomed .gslide-media{-webkit-box-shadow:none!important;box-shadow:none!important}.desc-bottom .gslide-media,.desc-top .gslide-media{margin:0 auto;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column}.gslide-description{position:relative;-webkit-box-flex:1;-ms-flex:1 0 100%;flex:1 0 100%}.gslide-description.description-left,.gslide-description.description-right{max-width:100%}.gslide-description.description-bottom,.gslide-description.description-top{margin:0 auto;width:100%}.gslide-description p{margin-bottom:12px}.gslide-description p:last-child{margin-bottom:0}.zoomed .gslide-description{display:none}.glightbox-button-hidden{display:none}.glightbox-mobile .glightbox-container .gslide-description{height:auto!important;width:100%;position:absolute;bottom:0;padding:19px 11px;max-width:100vw!important;-webkit-box-ordinal-group:3!important;-ms-flex-order:2!important;order:2!important;max-height:78vh;overflow:auto!important;background:-webkit-gradient(linear,left top,left bottom,from(rgba(0,0,0,0)),to(rgba(0,0,0,.75)));background:linear-gradient(to bottom,rgba(0,0,0,0) 0,rgba(0,0,0,.75) 100%);-webkit-transition:opacity .3s linear;transition:opacity .3s linear;padding-bottom:50px}.glightbox-mobile .glightbox-container .gslide-title{color:#fff;font-size:1em}.glightbox-mobile .glightbox-container .gslide-desc{color:#a1a1a1}.glightbox-mobile .glightbox-container .gslide-desc a{color:#fff;font-weight:700}.glightbox-mobile .glightbox-container .gslide-desc *{color:inherit}.glightbox-mobile .glightbox-container .gslide-desc .desc-more{color:#fff;opacity:.4}.gdesc-open .gslide-media{-webkit-transition:opacity .5s ease;transition:opacity .5s ease;opacity:.4}.gdesc-open .gdesc-inner{padding-bottom:30px}.gdesc-closed .gslide-media{-webkit-transition:opacity .5s ease;transition:opacity .5s ease;opacity:1}.greset{-webkit-transition:all .3s ease;transition:all .3s ease}.gabsolute{position:absolute}.grelative{position:relative}.glightbox-desc{display:none!important}.glightbox-open{overflow:hidden}.gloader{height:25px;width:25px;-webkit-animation:lightboxLoader .8s infinite linear;animation:lightboxLoader .8s infinite linear;border:2px solid #fff;border-right-color:transparent;border-radius:50%;position:absolute;display:block;z-index:9999;left:0;right:0;margin:0 auto;top:47%}.goverlay{width:100%;height:calc(100vh + 1px);position:fixed;top:-1px;left:0;background:#000;will-change:opacity}.glightbox-mobile .goverlay{background:#000}.gclose,.gnext,.gprev{z-index:99999;cursor:pointer;width:26px;height:44px;border:none;display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column}.gclose svg,.gnext svg,.gprev svg{display:block;width:25px;height:auto;margin:0;padding:0}.gclose.disabled,.gnext.disabled,.gprev.disabled{opacity:.1}.gclose .garrow,.gnext .garrow,.gprev .garrow{stroke:#fff}.gbtn.focused{outline:2px solid #0f3d81}iframe.wait-autoplay{opacity:0}.glightbox-closing .gclose,.glightbox-closing .gnext,.glightbox-closing .gprev{opacity:0!important}.glightbox-clean .gslide-description{background:#fff}.glightbox-clean .gdesc-inner{padding:22px 20px}.glightbox-clean .gslide-title{font-size:1em;font-weight:400;font-family:arial;color:#000;margin-bottom:19px;line-height:1.4em}.glightbox-clean .gslide-desc{font-size:.86em;margin-bottom:0;font-family:arial;line-height:1.4em}.glightbox-clean .gslide-video{background:#000}.glightbox-clean .gclose,.glightbox-clean .gnext,.glightbox-clean .gprev{background-color:rgba(0,0,0,.75);border-radius:4px}.glightbox-clean .gclose path,.glightbox-clean .gnext path,.glightbox-clean .gprev path{fill:#fff}.glightbox-clean .gprev{position:absolute;top:-100%;left:30px;width:40px;height:50px}.glightbox-clean .gnext{position:absolute;top:-100%;right:30px;width:40px;height:50px}.glightbox-clean .gclose{width:35px;height:35px;top:15px;right:10px;position:absolute}.glightbox-clean .gclose svg{width:18px;height:auto}.glightbox-clean .gclose:hover{opacity:1}.gfadeIn{-webkit-animation:gfadeIn .5s ease;animation:gfadeIn .5s ease}.gfadeOut{-webkit-animation:gfadeOut .5s ease;animation:gfadeOut .5s ease}.gslideOutLeft{-webkit-animation:gslideOutLeft .3s ease;animation:gslideOutLeft .3s ease}.gslideInLeft{-webkit-animation:gslideInLeft .3s ease;animation:gslideInLeft .3s ease}.gslideOutRight{-webkit-animation:gslideOutRight .3s ease;animation:gslideOutRight .3s ease}.gslideInRight{-webkit-animation:gslideInRight .3s ease;animation:gslideInRight .3s ease}.gzoomIn{-webkit-animation:gzoomIn .5s ease;animation:gzoomIn .5s ease}.gzoomOut{-webkit-animation:gzoomOut .5s ease;animation:gzoomOut .5s ease}@-webkit-keyframes lightboxLoader{0%{-webkit-transform:rotate(0);transform:rotate(0)}100%{-webkit-transform:rotate(360deg);transform:rotate(360deg)}}@keyframes lightboxLoader{0%{-webkit-transform:rotate(0);transform:rotate(0)}100%{-webkit-transform:rotate(360deg);transform:rotate(360deg)}}@-webkit-keyframes gfadeIn{from{opacity:0}to{opacity:1}}@keyframes gfadeIn{from{opacity:0}to{opacity:1}}@-webkit-keyframes gfadeOut{from{opacity:1}to{opacity:0}}@keyframes gfadeOut{from{opacity:1}to{opacity:0}}@-webkit-keyframes gslideInLeft{from{opacity:0;-webkit-transform:translate3d(-60%,0,0);transform:translate3d(-60%,0,0)}to{visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0);opacity:1}}@keyframes gslideInLeft{from{opacity:0;-webkit-transform:translate3d(-60%,0,0);transform:translate3d(-60%,0,0)}to{visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0);opacity:1}}@-webkit-keyframes gslideOutLeft{from{opacity:1;visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}to{-webkit-transform:translate3d(-60%,0,0);transform:translate3d(-60%,0,0);opacity:0;visibility:hidden}}@keyframes gslideOutLeft{from{opacity:1;visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}to{-webkit-transform:translate3d(-60%,0,0);transform:translate3d(-60%,0,0);opacity:0;visibility:hidden}}@-webkit-keyframes gslideInRight{from{opacity:0;visibility:visible;-webkit-transform:translate3d(60%,0,0);transform:translate3d(60%,0,0)}to{-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0);opacity:1}}@keyframes gslideInRight{from{opacity:0;visibility:visible;-webkit-transform:translate3d(60%,0,0);transform:translate3d(60%,0,0)}to{-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0);opacity:1}}@-webkit-keyframes gslideOutRight{from{opacity:1;visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}to{-webkit-transform:translate3d(60%,0,0);transform:translate3d(60%,0,0);opacity:0}}@keyframes gslideOutRight{from{opacity:1;visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}to{-webkit-transform:translate3d(60%,0,0);transform:translate3d(60%,0,0);opacity:0}}@-webkit-keyframes gzoomIn{from{opacity:0;-webkit-transform:scale3d(.3,.3,.3);transform:scale3d(.3,.3,.3)}to{opacity:1}}@keyframes gzoomIn{from{opacity:0;-webkit-transform:scale3d(.3,.3,.3);transform:scale3d(.3,.3,.3)}to{opacity:1}}@-webkit-keyframes gzoomOut{from{opacity:1}50%{opacity:0;-webkit-transform:scale3d(.3,.3,.3);transform:scale3d(.3,.3,.3)}to{opacity:0}}@keyframes gzoomOut{from{opacity:1}50%{opacity:0;-webkit-transform:scale3d(.3,.3,.3);transform:scale3d(.3,.3,.3)}to{opacity:0}}@media (min-width:769px){.glightbox-container .ginner-container{width:auto;height:auto;-webkit-box-orient:horizontal;-webkit-box-direction:normal;-ms-flex-direction:row;flex-direction:row}.glightbox-container .ginner-container.desc-top .gslide-description{-webkit-box-ordinal-group:1;-ms-flex-order:0;order:0}.glightbox-container .ginner-container.desc-top .gslide-image,.glightbox-container .ginner-container.desc-top .gslide-image img{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}.glightbox-container .ginner-container.desc-left .gslide-description{-webkit-box-ordinal-group:1;-ms-flex-order:0;order:0}.glightbox-container .ginner-container.desc-left .gslide-image{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}.gslide-image img{max-height:97vh;max-width:100%}.gslide-image img.zoomable{cursor:-webkit-zoom-in;cursor:zoom-in}.zoomed .gslide-image img.zoomable{cursor:-webkit-grab;cursor:grab}.gslide-inline{max-height:95vh}.gslide-external{max-height:100vh}.gslide-description.description-left,.gslide-description.description-right{max-width:275px}.glightbox-open{height:auto}.goverlay{background:rgba(0,0,0,.92)}.glightbox-clean .gslide-media{-webkit-box-shadow:1px 2px 9px 0 rgba(0,0,0,.65);box-shadow:1px 2px 9px 0 rgba(0,0,0,.65)}.glightbox-clean .description-left .gdesc-inner,.glightbox-clean .description-right .gdesc-inner{position:absolute;height:100%;overflow-y:auto}.glightbox-clean .gclose,.glightbox-clean .gnext,.glightbox-clean .gprev{background-color:rgba(0,0,0,.32)}.glightbox-clean .gclose:hover,.glightbox-clean .gnext:hover,.glightbox-clean .gprev:hover{background-color:rgba(0,0,0,.7)}.glightbox-clean .gprev{top:45%}.glightbox-clean .gnext{top:45%}}@media (min-width:992px){.glightbox-clean .gclose{opacity:.7;right:20px}}@media screen and (max-height:420px){.goverlay{background:#000}} -/*! update cache: 20260104155757 */ +/*! update cache: 20260104161456 */ diff --git a/ja/assets/stylesheets/main.484c7ddc.min.css b/ja/assets/stylesheets/main.484c7ddc.min.css index 0c9d0211f..7e2a4bf48 100644 --- a/ja/assets/stylesheets/main.484c7ddc.min.css +++ b/ja/assets/stylesheets/main.484c7ddc.min.css @@ -1,2 +1,2 @@ @charset "UTF-8";html{-webkit-text-size-adjust:none;-moz-text-size-adjust:none;text-size-adjust:none;box-sizing:border-box}*,:after,:before{box-sizing:inherit}@media (prefers-reduced-motion){*,:after,:before{transition:none!important}}body{margin:0}a,button,input,label{-webkit-tap-highlight-color:transparent}a{color:inherit;text-decoration:none}hr{border:0;box-sizing:initial;display:block;height:.05rem;overflow:visible;padding:0}small{font-size:80%}sub,sup{line-height:1em}img{border-style:none}table{border-collapse:initial;border-spacing:0}td,th{font-weight:400;vertical-align:top}button{background:#0000;border:0;font-family:inherit;font-size:inherit;margin:0;padding:0}input{border:0;outline:none}:root{--md-primary-fg-color:#4051b5;--md-primary-fg-color--light:#5d6cc0;--md-primary-fg-color--dark:#303fa1;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3;--md-accent-fg-color:#526cfe;--md-accent-fg-color--transparent:#526cfe1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-scheme=default]{color-scheme:light}[data-md-color-scheme=default] img[src$="#gh-dark-mode-only"],[data-md-color-scheme=default] img[src$="#only-dark"]{display:none}:root,[data-md-color-scheme=default]{--md-hue:225deg;--md-default-fg-color:#000000de;--md-default-fg-color--light:#0000008a;--md-default-fg-color--lighter:#00000052;--md-default-fg-color--lightest:#00000012;--md-default-bg-color:#fff;--md-default-bg-color--light:#ffffffb3;--md-default-bg-color--lighter:#ffffff4d;--md-default-bg-color--lightest:#ffffff1f;--md-code-fg-color:#36464e;--md-code-bg-color:#f5f5f5;--md-code-bg-color--light:#f5f5f5b3;--md-code-bg-color--lighter:#f5f5f54d;--md-code-hl-color:#4287ff;--md-code-hl-color--light:#4287ff1a;--md-code-hl-number-color:#d52a2a;--md-code-hl-special-color:#db1457;--md-code-hl-function-color:#a846b9;--md-code-hl-constant-color:#6e59d9;--md-code-hl-keyword-color:#3f6ec6;--md-code-hl-string-color:#1c7d4d;--md-code-hl-name-color:var(--md-code-fg-color);--md-code-hl-operator-color:var(--md-default-fg-color--light);--md-code-hl-punctuation-color:var(--md-default-fg-color--light);--md-code-hl-comment-color:var(--md-default-fg-color--light);--md-code-hl-generic-color:var(--md-default-fg-color--light);--md-code-hl-variable-color:var(--md-default-fg-color--light);--md-typeset-color:var(--md-default-fg-color);--md-typeset-a-color:var(--md-primary-fg-color);--md-typeset-del-color:#f5503d26;--md-typeset-ins-color:#0bd57026;--md-typeset-kbd-color:#fafafa;--md-typeset-kbd-accent-color:#fff;--md-typeset-kbd-border-color:#b8b8b8;--md-typeset-mark-color:#ffff0080;--md-typeset-table-color:#0000001f;--md-typeset-table-color--light:rgba(0,0,0,.035);--md-admonition-fg-color:var(--md-default-fg-color);--md-admonition-bg-color:var(--md-default-bg-color);--md-warning-fg-color:#000000de;--md-warning-bg-color:#ff9;--md-footer-fg-color:#fff;--md-footer-fg-color--light:#ffffffb3;--md-footer-fg-color--lighter:#ffffff73;--md-footer-bg-color:#000000de;--md-footer-bg-color--dark:#00000052;--md-shadow-z1:0 0.2rem 0.5rem #0000000d,0 0 0.05rem #0000001a;--md-shadow-z2:0 0.2rem 0.5rem #0000001a,0 0 0.05rem #00000040;--md-shadow-z3:0 0.2rem 0.5rem #0003,0 0 0.05rem #00000059}.md-icon svg{fill:currentcolor;display:block;height:1.2rem;width:1.2rem}body{-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;--md-text-font-family:var(--md-text-font,_),-apple-system,BlinkMacSystemFont,Helvetica,Arial,sans-serif;--md-code-font-family:var(--md-code-font,_),SFMono-Regular,Consolas,Menlo,monospace}aside,body,input{font-feature-settings:"kern","liga";color:var(--md-typeset-color);font-family:var(--md-text-font-family)}code,kbd,pre{font-feature-settings:"kern";font-family:var(--md-code-font-family)}:root{--md-typeset-table-sort-icon:url('data:image/svg+xml;charset=utf-8,');--md-typeset-table-sort-icon--asc:url('data:image/svg+xml;charset=utf-8,');--md-typeset-table-sort-icon--desc:url('data:image/svg+xml;charset=utf-8,')}.md-typeset{-webkit-print-color-adjust:exact;color-adjust:exact;font-size:.8rem;line-height:1.6;overflow-wrap:break-word}@media print{.md-typeset{font-size:.68rem}}.md-typeset blockquote,.md-typeset dl,.md-typeset figure,.md-typeset ol,.md-typeset pre,.md-typeset ul{margin-bottom:1em;margin-top:1em}.md-typeset h1{color:var(--md-default-fg-color--light);font-size:2em;line-height:1.3;margin:0 0 1.25em}.md-typeset h1,.md-typeset h2{font-weight:300;letter-spacing:-.01em}.md-typeset h2{font-size:1.5625em;line-height:1.4;margin:1.6em 0 .64em}.md-typeset h3{font-size:1.25em;font-weight:400;letter-spacing:-.01em;line-height:1.5;margin:1.6em 0 .8em}.md-typeset h2+h3{margin-top:.8em}.md-typeset h4{font-weight:700;letter-spacing:-.01em;margin:1em 0}.md-typeset h5,.md-typeset h6{color:var(--md-default-fg-color--light);font-size:.8em;font-weight:700;letter-spacing:-.01em;margin:1.25em 0}.md-typeset h5{text-transform:uppercase}.md-typeset h5 code{text-transform:none}.md-typeset hr{border-bottom:.05rem solid var(--md-default-fg-color--lightest);display:flow-root;margin:1.5em 0}.md-typeset a{color:var(--md-typeset-a-color);word-break:break-word}.md-typeset a,.md-typeset a:before{transition:color 125ms}.md-typeset a:focus,.md-typeset a:hover{color:var(--md-accent-fg-color)}.md-typeset a:focus code,.md-typeset a:hover code{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}.md-typeset a code{color:var(--md-typeset-a-color)}.md-typeset a.focus-visible{outline-color:var(--md-accent-fg-color);outline-offset:.2rem}.md-typeset code,.md-typeset kbd,.md-typeset pre{color:var(--md-code-fg-color);direction:ltr;font-variant-ligatures:none;transition:background-color 125ms}@media print{.md-typeset code,.md-typeset kbd,.md-typeset pre{white-space:pre-wrap}}.md-typeset code{background-color:var(--md-code-bg-color);border-radius:.1rem;-webkit-box-decoration-break:clone;box-decoration-break:clone;font-size:.85em;padding:0 .2941176471em;transition:color 125ms,background-color 125ms;word-break:break-word}.md-typeset code:not(.focus-visible){-webkit-tap-highlight-color:transparent;outline:none}.md-typeset pre{display:flow-root;line-height:1.4;position:relative}.md-typeset pre>code{-webkit-box-decoration-break:slice;box-decoration-break:slice;box-shadow:none;display:block;margin:0;outline-color:var(--md-accent-fg-color);overflow:auto;padding:.7720588235em 1.1764705882em;scrollbar-color:var(--md-default-fg-color--lighter) #0000;scrollbar-width:thin;touch-action:auto;word-break:normal}.md-typeset pre>code:hover{scrollbar-color:var(--md-accent-fg-color) #0000}.md-typeset pre>code::-webkit-scrollbar{height:.2rem;width:.2rem}.md-typeset pre>code::-webkit-scrollbar-thumb{background-color:var(--md-default-fg-color--lighter)}.md-typeset pre>code::-webkit-scrollbar-thumb:hover{background-color:var(--md-accent-fg-color)}.md-typeset kbd{background-color:var(--md-typeset-kbd-color);border-radius:.1rem;box-shadow:0 .1rem 0 .05rem var(--md-typeset-kbd-border-color),0 .1rem 0 var(--md-typeset-kbd-border-color),0 -.1rem .2rem var(--md-typeset-kbd-accent-color) inset;color:var(--md-default-fg-color);display:inline-block;font-size:.75em;padding:0 .6666666667em;vertical-align:text-top;word-break:break-word}.md-typeset mark{background-color:var(--md-typeset-mark-color);-webkit-box-decoration-break:clone;box-decoration-break:clone;color:inherit;word-break:break-word}.md-typeset abbr{cursor:help;text-decoration:none}.md-typeset [data-preview],.md-typeset abbr{border-bottom:.05rem dotted var(--md-default-fg-color--light)}.md-typeset small{opacity:.75}[dir=ltr] .md-typeset sub,[dir=ltr] .md-typeset sup{margin-left:.078125em}[dir=rtl] .md-typeset sub,[dir=rtl] .md-typeset sup{margin-right:.078125em}[dir=ltr] .md-typeset blockquote{padding-left:.6rem}[dir=rtl] .md-typeset blockquote{padding-right:.6rem}[dir=ltr] .md-typeset blockquote{border-left:.2rem solid var(--md-default-fg-color--lighter)}[dir=rtl] .md-typeset blockquote{border-right:.2rem solid var(--md-default-fg-color--lighter)}.md-typeset blockquote{color:var(--md-default-fg-color--light);margin-left:0;margin-right:0}.md-typeset ul{list-style-type:disc}.md-typeset ul[type]{list-style-type:revert-layer}[dir=ltr] .md-typeset ol,[dir=ltr] .md-typeset ul{margin-left:.625em}[dir=rtl] .md-typeset ol,[dir=rtl] .md-typeset ul{margin-right:.625em}.md-typeset ol,.md-typeset ul{padding:0}.md-typeset ol:not([hidden]),.md-typeset ul:not([hidden]){display:flow-root}.md-typeset ol ol,.md-typeset ul ol{list-style-type:lower-alpha}.md-typeset ol ol ol,.md-typeset ul ol ol{list-style-type:lower-roman}.md-typeset ol ol ol ol,.md-typeset ul ol ol ol{list-style-type:upper-alpha}.md-typeset ol ol ol ol ol,.md-typeset ul ol ol ol ol{list-style-type:upper-roman}.md-typeset ol[type],.md-typeset ul[type]{list-style-type:revert-layer}[dir=ltr] .md-typeset ol li,[dir=ltr] .md-typeset ul li{margin-left:1.25em}[dir=rtl] .md-typeset ol li,[dir=rtl] .md-typeset ul li{margin-right:1.25em}.md-typeset ol li,.md-typeset ul li{margin-bottom:.5em}.md-typeset ol li blockquote,.md-typeset ol li p,.md-typeset ul li blockquote,.md-typeset ul li p{margin:.5em 0}.md-typeset ol li:last-child,.md-typeset ul li:last-child{margin-bottom:0}[dir=ltr] .md-typeset ol li ol,[dir=ltr] .md-typeset ol li ul,[dir=ltr] .md-typeset ul li ol,[dir=ltr] .md-typeset ul li ul{margin-left:.625em}[dir=rtl] .md-typeset ol li ol,[dir=rtl] .md-typeset ol li ul,[dir=rtl] .md-typeset ul li ol,[dir=rtl] .md-typeset ul li ul{margin-right:.625em}.md-typeset ol li ol,.md-typeset ol li ul,.md-typeset ul li ol,.md-typeset ul li ul{margin-bottom:.5em;margin-top:.5em}[dir=ltr] .md-typeset dd{margin-left:1.875em}[dir=rtl] .md-typeset dd{margin-right:1.875em}.md-typeset dd{margin-bottom:1.5em;margin-top:1em}.md-typeset img,.md-typeset svg,.md-typeset video{height:auto;max-width:100%}.md-typeset img[align=left]{margin:1em 1em 1em 0}.md-typeset img[align=right]{margin:1em 0 1em 1em}.md-typeset img[align]:only-child{margin-top:0}.md-typeset figure{display:flow-root;margin:1em auto;max-width:100%;text-align:center;width:fit-content}.md-typeset figure img{display:block;margin:0 auto}.md-typeset figcaption{font-style:italic;margin:1em auto;max-width:24rem}.md-typeset iframe{max-width:100%}.md-typeset table:not([class]){background-color:var(--md-default-bg-color);border:.05rem solid var(--md-typeset-table-color);border-radius:.1rem;display:inline-block;font-size:.64rem;max-width:100%;overflow:auto;touch-action:auto}@media print{.md-typeset table:not([class]){display:table}}.md-typeset table:not([class])+*{margin-top:1.5em}.md-typeset table:not([class]) td>:first-child,.md-typeset table:not([class]) th>:first-child{margin-top:0}.md-typeset table:not([class]) td>:last-child,.md-typeset table:not([class]) th>:last-child{margin-bottom:0}.md-typeset table:not([class]) td:not([align]),.md-typeset table:not([class]) th:not([align]){text-align:left}[dir=rtl] .md-typeset table:not([class]) td:not([align]),[dir=rtl] .md-typeset table:not([class]) th:not([align]){text-align:right}.md-typeset table:not([class]) th{font-weight:700;min-width:5rem;padding:.9375em 1.25em;vertical-align:top}.md-typeset table:not([class]) td{border-top:.05rem solid var(--md-typeset-table-color);padding:.9375em 1.25em;vertical-align:top}.md-typeset table:not([class]) tbody tr{transition:background-color 125ms}.md-typeset table:not([class]) tbody tr:hover{background-color:var(--md-typeset-table-color--light);box-shadow:0 .05rem 0 var(--md-default-bg-color) inset}.md-typeset table:not([class]) a{word-break:normal}.md-typeset table th[role=columnheader]{cursor:pointer}[dir=ltr] .md-typeset table th[role=columnheader]:after{margin-left:.5em}[dir=rtl] .md-typeset table th[role=columnheader]:after{margin-right:.5em}.md-typeset table th[role=columnheader]:after{content:"";display:inline-block;height:1.2em;-webkit-mask-image:var(--md-typeset-table-sort-icon);mask-image:var(--md-typeset-table-sort-icon);-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;transition:background-color 125ms;vertical-align:text-bottom;width:1.2em}.md-typeset table th[role=columnheader]:hover:after{background-color:var(--md-default-fg-color--lighter)}.md-typeset table th[role=columnheader][aria-sort=ascending]:after{background-color:var(--md-default-fg-color--light);-webkit-mask-image:var(--md-typeset-table-sort-icon--asc);mask-image:var(--md-typeset-table-sort-icon--asc)}.md-typeset table th[role=columnheader][aria-sort=descending]:after{background-color:var(--md-default-fg-color--light);-webkit-mask-image:var(--md-typeset-table-sort-icon--desc);mask-image:var(--md-typeset-table-sort-icon--desc)}.md-typeset__scrollwrap{margin:1em -.8rem;overflow-x:auto;touch-action:auto}.md-typeset__table{display:inline-block;margin-bottom:.5em;padding:0 .8rem}@media print{.md-typeset__table{display:block}}html .md-typeset__table table{display:table;margin:0;overflow:hidden;width:100%}@media screen and (max-width:44.984375em){.md-content__inner>pre{margin:1em -.8rem}.md-content__inner>pre code{border-radius:0}}.md-typeset .md-author{border-radius:100%;display:block;flex-shrink:0;height:1.6rem;overflow:hidden;position:relative;transition:color 125ms,transform 125ms;width:1.6rem}.md-typeset .md-author img{display:block}.md-typeset .md-author--more{background:var(--md-default-fg-color--lightest);color:var(--md-default-fg-color--lighter);font-size:.6rem;font-weight:700;line-height:1.6rem;text-align:center}.md-typeset .md-author--long{height:2.4rem;width:2.4rem}.md-typeset a.md-author{transform:scale(1)}.md-typeset a.md-author img{border-radius:100%;filter:grayscale(100%) opacity(75%);transition:filter 125ms}.md-typeset a.md-author:focus,.md-typeset a.md-author:hover{transform:scale(1.1);z-index:1}.md-typeset a.md-author:focus img,.md-typeset a.md-author:hover img{filter:grayscale(0)}.md-banner{background-color:var(--md-footer-bg-color);color:var(--md-footer-fg-color);overflow:auto}@media print{.md-banner{display:none}}.md-banner--warning{background-color:var(--md-warning-bg-color);color:var(--md-warning-fg-color)}.md-banner__inner{font-size:.7rem;margin:.6rem auto;padding:0 .8rem}[dir=ltr] .md-banner__button{float:right}[dir=rtl] .md-banner__button{float:left}.md-banner__button{color:inherit;cursor:pointer;transition:opacity .25s}.no-js .md-banner__button{display:none}.md-banner__button:hover{opacity:.7}html{font-size:125%;height:100%;overflow-x:hidden}@media screen and (min-width:100em){html{font-size:137.5%}}@media screen and (min-width:125em){html{font-size:150%}}body{background-color:var(--md-default-bg-color);display:flex;flex-direction:column;font-size:.5rem;min-height:100%;position:relative;width:100%}@media print{body{display:block}}@media screen and (max-width:59.984375em){body[data-md-scrolllock]{position:fixed}}.md-grid{margin-left:auto;margin-right:auto;max-width:61rem}.md-container{display:flex;flex-direction:column;flex-grow:1}@media print{.md-container{display:block}}.md-main{flex-grow:1}.md-main__inner{display:flex;height:100%;margin-top:1.5rem}.md-ellipsis{overflow:hidden;text-overflow:ellipsis}.md-toggle{display:none}.md-option{height:0;opacity:0;position:absolute;width:0}.md-option:checked+label:not([hidden]){display:block}.md-option.focus-visible+label{outline-color:var(--md-accent-fg-color);outline-style:auto}.md-skip{background-color:var(--md-default-fg-color);border-radius:.1rem;color:var(--md-default-bg-color);font-size:.64rem;margin:.5rem;opacity:0;outline-color:var(--md-accent-fg-color);padding:.3rem .5rem;position:fixed;transform:translateY(.4rem);z-index:-1}.md-skip:focus{opacity:1;transform:translateY(0);transition:transform .25s cubic-bezier(.4,0,.2,1),opacity 175ms 75ms;z-index:10}@page{margin:25mm}:root{--md-clipboard-icon:url('data:image/svg+xml;charset=utf-8,')}.md-clipboard{border-radius:.1rem;color:var(--md-default-fg-color--lightest);cursor:pointer;height:1.5em;outline-color:var(--md-accent-fg-color);outline-offset:.1rem;transition:color .25s;width:1.5em;z-index:1}@media print{.md-clipboard{display:none}}.md-clipboard:not(.focus-visible){-webkit-tap-highlight-color:transparent;outline:none}:hover>.md-clipboard{color:var(--md-default-fg-color--light)}.md-clipboard:focus,.md-clipboard:hover{color:var(--md-accent-fg-color)}.md-clipboard:after{background-color:currentcolor;content:"";display:block;height:1.125em;margin:0 auto;-webkit-mask-image:var(--md-clipboard-icon);mask-image:var(--md-clipboard-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:1.125em}.md-clipboard--inline{cursor:pointer}.md-clipboard--inline code{transition:color .25s,background-color .25s}.md-clipboard--inline:focus code,.md-clipboard--inline:hover code{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}:root{--md-code-select-icon:url('data:image/svg+xml;charset=utf-8,');--md-code-copy-icon:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .md-code__content{display:grid}.md-code__nav{background-color:var(--md-code-bg-color--lighter);border-radius:.1rem;display:flex;gap:.2rem;padding:.2rem;position:absolute;right:.25em;top:.25em;transition:background-color .25s;z-index:1}:hover>.md-code__nav{background-color:var(--md-code-bg-color--light)}.md-code__button{color:var(--md-default-fg-color--lightest);cursor:pointer;display:block;height:1.5em;outline-color:var(--md-accent-fg-color);outline-offset:.1rem;transition:color .25s;width:1.5em}:hover>*>.md-code__button{color:var(--md-default-fg-color--light)}.md-code__button.focus-visible,.md-code__button:hover{color:var(--md-accent-fg-color)}.md-code__button--active{color:var(--md-default-fg-color)!important}.md-code__button:after{background-color:currentcolor;content:"";display:block;height:1.125em;margin:0 auto;-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:1.125em}.md-code__button[data-md-type=select]:after{-webkit-mask-image:var(--md-code-select-icon);mask-image:var(--md-code-select-icon)}.md-code__button[data-md-type=copy]:after{-webkit-mask-image:var(--md-code-copy-icon);mask-image:var(--md-code-copy-icon)}@keyframes consent{0%{opacity:0;transform:translateY(100%)}to{opacity:1;transform:translateY(0)}}@keyframes overlay{0%{opacity:0}to{opacity:1}}.md-consent__overlay{animation:overlay .25s both;-webkit-backdrop-filter:blur(.1rem);backdrop-filter:blur(.1rem);background-color:#0000008a;height:100%;opacity:1;position:fixed;top:0;width:100%;z-index:5}.md-consent__inner{animation:consent .5s cubic-bezier(.1,.7,.1,1) both;background-color:var(--md-default-bg-color);border:0;border-radius:.1rem;bottom:0;box-shadow:0 0 .2rem #0000001a,0 .2rem .4rem #0003;max-height:100%;overflow:auto;padding:0;position:fixed;width:100%;z-index:5}.md-consent__form{padding:.8rem}.md-consent__settings{display:none;margin:1em 0}input:checked+.md-consent__settings{display:block}.md-consent__controls{margin-bottom:.8rem}.md-typeset .md-consent__controls .md-button{display:inline}@media screen and (max-width:44.984375em){.md-typeset .md-consent__controls .md-button{display:block;margin-top:.4rem;text-align:center;width:100%}}.md-consent label{cursor:pointer}.md-content{flex-grow:1;min-width:0}.md-content__inner{margin:0 .8rem 1.2rem;padding-top:.6rem}@media screen and (min-width:76.25em){[dir=ltr] .md-sidebar--primary:not([hidden])~.md-content>.md-content__inner{margin-left:1.2rem}[dir=ltr] .md-sidebar--secondary:not([hidden])~.md-content>.md-content__inner,[dir=rtl] .md-sidebar--primary:not([hidden])~.md-content>.md-content__inner{margin-right:1.2rem}[dir=rtl] .md-sidebar--secondary:not([hidden])~.md-content>.md-content__inner{margin-left:1.2rem}}.md-content__inner:before{content:"";display:block;height:.4rem}.md-content__inner>:last-child{margin-bottom:0}[dir=ltr] .md-content__button{float:right}[dir=rtl] .md-content__button{float:left}[dir=ltr] .md-content__button{margin-left:.4rem}[dir=rtl] .md-content__button{margin-right:.4rem}.md-content__button{margin:.4rem 0;padding:0}@media print{.md-content__button{display:none}}.md-typeset .md-content__button{color:var(--md-default-fg-color--lighter)}.md-content__button svg{display:inline;vertical-align:top}[dir=rtl] .md-content__button svg{transform:scaleX(-1)}[dir=ltr] .md-dialog{right:.8rem}[dir=rtl] .md-dialog{left:.8rem}.md-dialog{background-color:var(--md-default-fg-color);border-radius:.1rem;bottom:.8rem;box-shadow:var(--md-shadow-z3);min-width:11.1rem;opacity:0;padding:.4rem .6rem;pointer-events:none;position:fixed;transform:translateY(100%);transition:transform 0ms .4s,opacity .4s;z-index:4}@media print{.md-dialog{display:none}}.md-dialog--active{opacity:1;pointer-events:auto;transform:translateY(0);transition:transform .4s cubic-bezier(.075,.85,.175,1),opacity .4s}.md-dialog__inner{color:var(--md-default-bg-color);font-size:.7rem}.md-feedback{margin:2em 0 1em;text-align:center}.md-feedback fieldset{border:none;margin:0;padding:0}.md-feedback__title{font-weight:700;margin:1em auto}.md-feedback__inner{position:relative}.md-feedback__list{display:flex;flex-wrap:wrap;place-content:baseline center;position:relative}.md-feedback__list:hover .md-icon:not(:disabled){color:var(--md-default-fg-color--lighter)}:disabled .md-feedback__list{min-height:1.8rem}.md-feedback__icon{color:var(--md-default-fg-color--light);cursor:pointer;flex-shrink:0;margin:0 .1rem;transition:color 125ms}.md-feedback__icon:not(:disabled).md-icon:hover{color:var(--md-accent-fg-color)}.md-feedback__icon:disabled{color:var(--md-default-fg-color--lightest);pointer-events:none}.md-feedback__note{opacity:0;position:relative;transform:translateY(.4rem);transition:transform .4s cubic-bezier(.1,.7,.1,1),opacity .15s}.md-feedback__note>*{margin:0 auto;max-width:16rem}:disabled .md-feedback__note{opacity:1;transform:translateY(0)}@media print{.md-feedback{display:none}}.md-footer{background-color:var(--md-footer-bg-color);color:var(--md-footer-fg-color)}@media print{.md-footer{display:none}}.md-footer__inner{justify-content:space-between;overflow:auto;padding:.2rem}.md-footer__inner:not([hidden]){display:flex}.md-footer__link{align-items:end;display:flex;flex-grow:0.01;margin-bottom:.4rem;margin-top:1rem;max-width:100%;outline-color:var(--md-accent-fg-color);overflow:hidden;transition:opacity .25s}.md-footer__link:focus,.md-footer__link:hover{opacity:.7}[dir=rtl] .md-footer__link svg{transform:scaleX(-1)}@media screen and (max-width:44.984375em){.md-footer__link--prev{flex-shrink:0}.md-footer__link--prev .md-footer__title{display:none}}[dir=ltr] .md-footer__link--next{margin-left:auto}[dir=rtl] .md-footer__link--next{margin-right:auto}.md-footer__link--next{text-align:right}[dir=rtl] .md-footer__link--next{text-align:left}.md-footer__title{flex-grow:1;font-size:.9rem;margin-bottom:.7rem;max-width:calc(100% - 2.4rem);padding:0 1rem;white-space:nowrap}.md-footer__button{margin:.2rem;padding:.4rem}.md-footer__direction{font-size:.64rem;opacity:.7}.md-footer-meta{background-color:var(--md-footer-bg-color--dark)}.md-footer-meta__inner{display:flex;flex-wrap:wrap;justify-content:space-between;padding:.2rem}html .md-footer-meta.md-typeset a{color:var(--md-footer-fg-color--light)}html .md-footer-meta.md-typeset a:focus,html .md-footer-meta.md-typeset a:hover{color:var(--md-footer-fg-color)}.md-copyright{color:var(--md-footer-fg-color--lighter);font-size:.64rem;margin:auto .6rem;padding:.4rem 0;width:100%}@media screen and (min-width:45em){.md-copyright{width:auto}}.md-copyright__highlight{color:var(--md-footer-fg-color--light)}.md-social{display:inline-flex;gap:.2rem;margin:0 .4rem;padding:.2rem 0 .6rem}@media screen and (min-width:45em){.md-social{padding:.6rem 0}}.md-social__link{display:inline-block;height:1.6rem;text-align:center;width:1.6rem}.md-social__link:before{line-height:1.9}.md-social__link svg{fill:currentcolor;max-height:.8rem;vertical-align:-25%}.md-typeset .md-button{border:.1rem solid;border-radius:.1rem;color:var(--md-primary-fg-color);cursor:pointer;display:inline-block;font-weight:700;padding:.625em 2em;transition:color 125ms,background-color 125ms,border-color 125ms}.md-typeset .md-button--primary{background-color:var(--md-primary-fg-color);border-color:var(--md-primary-fg-color);color:var(--md-primary-bg-color)}.md-typeset .md-button:focus,.md-typeset .md-button:hover{background-color:var(--md-accent-fg-color);border-color:var(--md-accent-fg-color);color:var(--md-accent-bg-color)}[dir=ltr] .md-typeset .md-input{border-top-left-radius:.1rem}[dir=ltr] .md-typeset .md-input,[dir=rtl] .md-typeset .md-input{border-top-right-radius:.1rem}[dir=rtl] .md-typeset .md-input{border-top-left-radius:.1rem}.md-typeset .md-input{border-bottom:.1rem solid var(--md-default-fg-color--lighter);box-shadow:var(--md-shadow-z1);font-size:.8rem;height:1.8rem;padding:0 .6rem;transition:border .25s,box-shadow .25s}.md-typeset .md-input:focus,.md-typeset .md-input:hover{border-bottom-color:var(--md-accent-fg-color);box-shadow:var(--md-shadow-z2)}.md-typeset .md-input--stretch{width:100%}.md-header{background-color:var(--md-primary-fg-color);box-shadow:0 0 .2rem #0000,0 .2rem .4rem #0000;color:var(--md-primary-bg-color);display:block;left:0;position:sticky;right:0;top:0;z-index:4}@media print{.md-header{display:none}}.md-header[hidden]{transform:translateY(-100%);transition:transform .25s cubic-bezier(.8,0,.6,1),box-shadow .25s}.md-header--shadow{box-shadow:0 0 .2rem #0000001a,0 .2rem .4rem #0003;transition:transform .25s cubic-bezier(.1,.7,.1,1),box-shadow .25s}.md-header__inner{align-items:center;display:flex;padding:0 .2rem}.md-header__button{color:currentcolor;cursor:pointer;margin:.2rem;outline-color:var(--md-accent-fg-color);padding:.4rem;position:relative;transition:opacity .25s;vertical-align:middle;z-index:1}.md-header__button:hover{opacity:.7}.md-header__button:not([hidden]){display:inline-block}.md-header__button:not(.focus-visible){-webkit-tap-highlight-color:transparent;outline:none}.md-header__button.md-logo{margin:.2rem;padding:.4rem}@media screen and (max-width:76.234375em){.md-header__button.md-logo{display:none}}.md-header__button.md-logo img,.md-header__button.md-logo svg{fill:currentcolor;display:block;height:1.2rem;width:auto}@media screen and (min-width:60em){.md-header__button[for=__search]{display:none}}.no-js .md-header__button[for=__search]{display:none}[dir=rtl] .md-header__button[for=__search] svg{transform:scaleX(-1)}@media screen and (min-width:76.25em){.md-header__button[for=__drawer]{display:none}}.md-header__topic{display:flex;max-width:100%;position:absolute;transition:transform .4s cubic-bezier(.1,.7,.1,1),opacity .15s;white-space:nowrap}.md-header__topic+.md-header__topic{opacity:0;pointer-events:none;transform:translateX(1.25rem);transition:transform .4s cubic-bezier(1,.7,.1,.1),opacity .15s;z-index:-1}[dir=rtl] .md-header__topic+.md-header__topic{transform:translateX(-1.25rem)}.md-header__topic:first-child{font-weight:700}[dir=ltr] .md-header__title{margin-left:1rem;margin-right:.4rem}[dir=rtl] .md-header__title{margin-left:.4rem;margin-right:1rem}.md-header__title{flex-grow:1;font-size:.9rem;height:2.4rem;line-height:2.4rem}.md-header__title--active .md-header__topic{opacity:0;pointer-events:none;transform:translateX(-1.25rem);transition:transform .4s cubic-bezier(1,.7,.1,.1),opacity .15s;z-index:-1}[dir=rtl] .md-header__title--active .md-header__topic{transform:translateX(1.25rem)}.md-header__title--active .md-header__topic+.md-header__topic{opacity:1;pointer-events:auto;transform:translateX(0);transition:transform .4s cubic-bezier(.1,.7,.1,1),opacity .15s;z-index:0}.md-header__title>.md-header__ellipsis{height:100%;position:relative;width:100%}.md-header__option{display:flex;flex-shrink:0;max-width:100%;transition:max-width 0ms .25s,opacity .25s .25s;white-space:nowrap}[data-md-toggle=search]:checked~.md-header .md-header__option{max-width:0;opacity:0;transition:max-width 0ms,opacity 0ms}.md-header__option>input{bottom:0}.md-header__source{display:none}@media screen and (min-width:60em){[dir=ltr] .md-header__source{margin-left:1rem}[dir=rtl] .md-header__source{margin-right:1rem}.md-header__source{display:block;max-width:11.7rem;width:11.7rem}}@media screen and (min-width:76.25em){[dir=ltr] .md-header__source{margin-left:1.4rem}[dir=rtl] .md-header__source{margin-right:1.4rem}}.md-meta{color:var(--md-default-fg-color--light);font-size:.7rem;line-height:1.3}.md-meta__list{display:inline-flex;flex-wrap:wrap;list-style:none;margin:0;padding:0}.md-meta__item:not(:last-child):after{content:"·";margin-left:.2rem;margin-right:.2rem}.md-meta__link{color:var(--md-typeset-a-color)}.md-meta__link:focus,.md-meta__link:hover{color:var(--md-accent-fg-color)}.md-draft{background-color:#ff1744;border-radius:.125em;color:#fff;display:inline-block;font-weight:700;padding-left:.5714285714em;padding-right:.5714285714em}:root{--md-nav-icon--prev:url('data:image/svg+xml;charset=utf-8,');--md-nav-icon--next:url('data:image/svg+xml;charset=utf-8,');--md-toc-icon:url('data:image/svg+xml;charset=utf-8,')}.md-nav{font-size:.7rem;line-height:1.3}.md-nav__title{color:var(--md-default-fg-color--light);display:block;font-weight:700;overflow:hidden;padding:0 .6rem;text-overflow:ellipsis}.md-nav__title .md-nav__button{display:none}.md-nav__title .md-nav__button img{height:100%;width:auto}.md-nav__title .md-nav__button.md-logo img,.md-nav__title .md-nav__button.md-logo svg{fill:currentcolor;display:block;height:2.4rem;max-width:100%;object-fit:contain;width:auto}.md-nav__list{list-style:none;margin:0;padding:0}.md-nav__link{align-items:flex-start;display:flex;gap:.4rem;margin-top:.625em;scroll-snap-align:start;transition:color 125ms}.md-nav__link--passed,.md-nav__link--passed code{color:var(--md-default-fg-color--light)}.md-nav__item .md-nav__link--active,.md-nav__item .md-nav__link--active code{color:var(--md-typeset-a-color)}.md-nav__link .md-ellipsis{position:relative}.md-nav__link .md-ellipsis code{word-break:normal}[dir=ltr] .md-nav__link .md-icon:last-child{margin-left:auto}[dir=rtl] .md-nav__link .md-icon:last-child{margin-right:auto}.md-nav__link .md-typeset{font-size:.7rem;line-height:1.3}.md-nav__link svg{fill:currentcolor;flex-shrink:0;height:1.3em;position:relative}.md-nav__link[for]:focus,.md-nav__link[for]:hover,.md-nav__link[href]:focus,.md-nav__link[href]:hover{color:var(--md-accent-fg-color);cursor:pointer}.md-nav__link[for]:focus code,.md-nav__link[for]:hover code,.md-nav__link[href]:focus code,.md-nav__link[href]:hover code{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}.md-nav__link.focus-visible{outline-color:var(--md-accent-fg-color);outline-offset:.2rem}.md-nav--primary .md-nav__link[for=__toc]{display:none}.md-nav--primary .md-nav__link[for=__toc] .md-icon:after{background-color:currentcolor;display:block;height:100%;-webkit-mask-image:var(--md-toc-icon);mask-image:var(--md-toc-icon);width:100%}.md-nav--primary .md-nav__link[for=__toc]~.md-nav{display:none}.md-nav__container>.md-nav__link{margin-top:0}.md-nav__container>.md-nav__link:first-child{flex-grow:1;min-width:0}.md-nav__icon{flex-shrink:0}.md-nav__source{display:none}@media screen and (max-width:76.234375em){.md-nav--primary,.md-nav--primary .md-nav{background-color:var(--md-default-bg-color);display:flex;flex-direction:column;height:100%;left:0;position:absolute;right:0;top:0;z-index:1}.md-nav--primary .md-nav__item,.md-nav--primary .md-nav__title{font-size:.8rem;line-height:1.5}.md-nav--primary .md-nav__title{background-color:var(--md-default-fg-color--lightest);color:var(--md-default-fg-color--light);cursor:pointer;height:5.6rem;line-height:2.4rem;padding:3rem .8rem .2rem;position:relative;white-space:nowrap}[dir=ltr] .md-nav--primary .md-nav__title .md-nav__icon{left:.4rem}[dir=rtl] .md-nav--primary .md-nav__title .md-nav__icon{right:.4rem}.md-nav--primary .md-nav__title .md-nav__icon{display:block;height:1.2rem;margin:.2rem;position:absolute;top:.4rem;width:1.2rem}.md-nav--primary .md-nav__title .md-nav__icon:after{background-color:currentcolor;content:"";display:block;height:100%;-webkit-mask-image:var(--md-nav-icon--prev);mask-image:var(--md-nav-icon--prev);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:100%}.md-nav--primary .md-nav__title~.md-nav__list{background-color:var(--md-default-bg-color);box-shadow:0 .05rem 0 var(--md-default-fg-color--lightest) inset;overflow-y:auto;scroll-snap-type:y mandatory;touch-action:pan-y}.md-nav--primary .md-nav__title~.md-nav__list>:first-child{border-top:0}.md-nav--primary .md-nav__title[for=__drawer]{background-color:var(--md-primary-fg-color);color:var(--md-primary-bg-color);font-weight:700}.md-nav--primary .md-nav__title .md-logo{display:block;left:.2rem;margin:.2rem;padding:.4rem;position:absolute;right:.2rem;top:.2rem}.md-nav--primary .md-nav__list{flex:1}.md-nav--primary .md-nav__item{border-top:.05rem solid var(--md-default-fg-color--lightest)}.md-nav--primary .md-nav__item--active>.md-nav__link{color:var(--md-typeset-a-color)}.md-nav--primary .md-nav__item--active>.md-nav__link:focus,.md-nav--primary .md-nav__item--active>.md-nav__link:hover{color:var(--md-accent-fg-color)}.md-nav--primary .md-nav__link{margin-top:0;padding:.6rem .8rem}.md-nav--primary .md-nav__link svg{margin-top:.1em}.md-nav--primary .md-nav__link>.md-nav__link{padding:0}[dir=ltr] .md-nav--primary .md-nav__link .md-nav__icon{margin-right:-.2rem}[dir=rtl] .md-nav--primary .md-nav__link .md-nav__icon{margin-left:-.2rem}.md-nav--primary .md-nav__link .md-nav__icon{font-size:1.2rem;height:1.2rem;width:1.2rem}.md-nav--primary .md-nav__link .md-nav__icon:after{background-color:currentcolor;content:"";display:block;height:100%;-webkit-mask-image:var(--md-nav-icon--next);mask-image:var(--md-nav-icon--next);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:100%}[dir=rtl] .md-nav--primary .md-nav__icon:after{transform:scale(-1)}.md-nav--primary .md-nav--secondary .md-nav{background-color:initial;position:static}[dir=ltr] .md-nav--primary .md-nav--secondary .md-nav .md-nav__link{padding-left:1.4rem}[dir=rtl] .md-nav--primary .md-nav--secondary .md-nav .md-nav__link{padding-right:1.4rem}[dir=ltr] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav__link{padding-left:2rem}[dir=rtl] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav__link{padding-right:2rem}[dir=ltr] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav .md-nav__link{padding-left:2.6rem}[dir=rtl] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav .md-nav__link{padding-right:2.6rem}[dir=ltr] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav .md-nav .md-nav__link{padding-left:3.2rem}[dir=rtl] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav .md-nav .md-nav__link{padding-right:3.2rem}.md-nav--secondary{background-color:initial}.md-nav__toggle~.md-nav{display:flex;opacity:0;transform:translateX(100%);transition:transform .25s cubic-bezier(.8,0,.6,1),opacity 125ms 50ms}[dir=rtl] .md-nav__toggle~.md-nav{transform:translateX(-100%)}.md-nav__toggle:checked~.md-nav{opacity:1;transform:translateX(0);transition:transform .25s cubic-bezier(.4,0,.2,1),opacity 125ms 125ms}.md-nav__toggle:checked~.md-nav>.md-nav__list{backface-visibility:hidden}}@media screen and (max-width:59.984375em){.md-nav--primary .md-nav__link[for=__toc]{display:flex}.md-nav--primary .md-nav__link[for=__toc] .md-icon:after{content:""}.md-nav--primary .md-nav__link[for=__toc]+.md-nav__link{display:none}.md-nav--primary .md-nav__link[for=__toc]~.md-nav{display:flex}.md-nav__source{background-color:var(--md-primary-fg-color--dark);color:var(--md-primary-bg-color);display:block;padding:0 .2rem}}@media screen and (min-width:60em) and (max-width:76.234375em){.md-nav--integrated .md-nav__link[for=__toc]{display:flex}.md-nav--integrated .md-nav__link[for=__toc] .md-icon:after{content:""}.md-nav--integrated .md-nav__link[for=__toc]+.md-nav__link{display:none}.md-nav--integrated .md-nav__link[for=__toc]~.md-nav{display:flex}}@media screen and (min-width:60em){.md-nav{margin-bottom:-.4rem}.md-nav--secondary .md-nav__title{background:var(--md-default-bg-color);box-shadow:0 0 .4rem .4rem var(--md-default-bg-color);position:sticky;top:0;z-index:1}.md-nav--secondary .md-nav__title[for=__toc]{scroll-snap-align:start}.md-nav--secondary .md-nav__title .md-nav__icon{display:none}[dir=ltr] .md-nav--secondary .md-nav__list{padding-left:.6rem}[dir=rtl] .md-nav--secondary .md-nav__list{padding-right:.6rem}.md-nav--secondary .md-nav__list{padding-bottom:.4rem}[dir=ltr] .md-nav--secondary .md-nav__item>.md-nav__link{margin-right:.4rem}[dir=rtl] .md-nav--secondary .md-nav__item>.md-nav__link{margin-left:.4rem}}@media screen and (min-width:76.25em){.md-nav{margin-bottom:-.4rem;transition:max-height .25s cubic-bezier(.86,0,.07,1)}.md-nav--primary .md-nav__title{background:var(--md-default-bg-color);box-shadow:0 0 .4rem .4rem var(--md-default-bg-color);position:sticky;top:0;z-index:1}.md-nav--primary .md-nav__title[for=__drawer]{scroll-snap-align:start}.md-nav--primary .md-nav__title .md-nav__icon{display:none}[dir=ltr] .md-nav--primary .md-nav__list{padding-left:.6rem}[dir=rtl] .md-nav--primary .md-nav__list{padding-right:.6rem}.md-nav--primary .md-nav__list{padding-bottom:.4rem}[dir=ltr] .md-nav--primary .md-nav__item>.md-nav__link{margin-right:.4rem}[dir=rtl] .md-nav--primary .md-nav__item>.md-nav__link{margin-left:.4rem}.md-nav__toggle~.md-nav{display:grid;grid-template-rows:minmax(.4rem,0fr);opacity:0;transition:grid-template-rows .25s cubic-bezier(.86,0,.07,1),opacity .25s,visibility 0ms .25s;visibility:collapse}.md-nav__toggle~.md-nav>.md-nav__list{overflow:hidden}.md-nav__toggle.md-toggle--indeterminate~.md-nav,.md-nav__toggle:checked~.md-nav{grid-template-rows:minmax(.4rem,1fr);opacity:1;transition:grid-template-rows .25s cubic-bezier(.86,0,.07,1),opacity .15s .1s,visibility 0ms;visibility:visible}.md-nav__toggle.md-toggle--indeterminate~.md-nav{transition:none}.md-nav__item--nested>.md-nav>.md-nav__title{display:none}.md-nav__item--section{display:block;margin:1.25em 0}.md-nav__item--section:last-child{margin-bottom:0}.md-nav__item--section>.md-nav__link{font-weight:700}.md-nav__item--section>.md-nav__link[for]{color:var(--md-default-fg-color--light)}.md-nav__item--section>.md-nav__link:not(.md-nav__container){pointer-events:none}.md-nav__item--section>.md-nav__link .md-icon,.md-nav__item--section>.md-nav__link>[for]{display:none}[dir=ltr] .md-nav__item--section>.md-nav{margin-left:-.6rem}[dir=rtl] .md-nav__item--section>.md-nav{margin-right:-.6rem}.md-nav__item--section>.md-nav{display:block;opacity:1;visibility:visible}.md-nav__item--section>.md-nav>.md-nav__list>.md-nav__item{padding:0}.md-nav__icon{border-radius:100%;height:.9rem;transition:background-color .25s;width:.9rem}.md-nav__icon:hover{background-color:var(--md-accent-fg-color--transparent)}.md-nav__icon:after{background-color:currentcolor;border-radius:100%;content:"";display:inline-block;height:100%;-webkit-mask-image:var(--md-nav-icon--next);mask-image:var(--md-nav-icon--next);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;transition:transform .25s;vertical-align:-.1rem;width:100%}[dir=rtl] .md-nav__icon:after{transform:rotate(180deg)}.md-nav__item--nested .md-nav__toggle:checked~.md-nav__link .md-nav__icon:after,.md-nav__item--nested .md-toggle--indeterminate~.md-nav__link .md-nav__icon:after{transform:rotate(90deg)}.md-nav--lifted>.md-nav__list>.md-nav__item,.md-nav--lifted>.md-nav__title{display:none}.md-nav--lifted>.md-nav__list>.md-nav__item--active{display:block}.md-nav--lifted>.md-nav__list>.md-nav__item--active>.md-nav__link{background:var(--md-default-bg-color);box-shadow:0 0 .4rem .4rem var(--md-default-bg-color);margin-top:0;position:sticky;top:0;z-index:1}.md-nav--lifted>.md-nav__list>.md-nav__item--active>.md-nav__link:not(.md-nav__container){pointer-events:none}.md-nav--lifted>.md-nav__list>.md-nav__item--active.md-nav__item--section{margin:0}[dir=ltr] .md-nav--lifted>.md-nav__list>.md-nav__item>.md-nav:not(.md-nav--secondary){margin-left:-.6rem}[dir=rtl] .md-nav--lifted>.md-nav__list>.md-nav__item>.md-nav:not(.md-nav--secondary){margin-right:-.6rem}.md-nav--lifted>.md-nav__list>.md-nav__item>[for]{color:var(--md-default-fg-color--light)}.md-nav--lifted .md-nav[data-md-level="1"]{grid-template-rows:minmax(.4rem,1fr);opacity:1;visibility:visible}[dir=ltr] .md-nav--integrated>.md-nav__list>.md-nav__item--active .md-nav--secondary{border-left:.05rem solid var(--md-primary-fg-color)}[dir=rtl] .md-nav--integrated>.md-nav__list>.md-nav__item--active .md-nav--secondary{border-right:.05rem solid var(--md-primary-fg-color)}.md-nav--integrated>.md-nav__list>.md-nav__item--active .md-nav--secondary{display:block;margin-bottom:1.25em;opacity:1;visibility:visible}.md-nav--integrated>.md-nav__list>.md-nav__item--active .md-nav--secondary>.md-nav__list{overflow:visible;padding-bottom:0}.md-nav--integrated>.md-nav__list>.md-nav__item--active .md-nav--secondary>.md-nav__title{display:none}}.md-pagination{font-size:.8rem;font-weight:700;gap:.4rem}.md-pagination,.md-pagination>*{align-items:center;display:flex;justify-content:center}.md-pagination>*{border-radius:.2rem;height:1.8rem;min-width:1.8rem;text-align:center}.md-pagination__current{background-color:var(--md-default-fg-color--lightest);color:var(--md-default-fg-color--light)}.md-pagination__link{transition:color 125ms,background-color 125ms}.md-pagination__link:focus,.md-pagination__link:hover{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}.md-pagination__link:focus svg,.md-pagination__link:hover svg{color:var(--md-accent-fg-color)}.md-pagination__link.focus-visible{outline-color:var(--md-accent-fg-color);outline-offset:.2rem}.md-pagination__link svg{fill:currentcolor;color:var(--md-default-fg-color--lighter);display:block;max-height:100%;width:1.2rem}:root{--md-path-icon:url('data:image/svg+xml;charset=utf-8,')}.md-path{font-size:.7rem;margin:0 .8rem;overflow:auto;padding-top:1.2rem}.md-path:not([hidden]){display:block}@media screen and (min-width:76.25em){.md-path{margin:0 1.2rem}}.md-path__list{align-items:center;display:flex;gap:.2rem;list-style:none;margin:0;padding:0}.md-path__item:not(:first-child){display:inline-flex;gap:.2rem;white-space:nowrap}.md-path__item:not(:first-child):before{background-color:var(--md-default-fg-color--lighter);content:"";display:inline;height:.8rem;-webkit-mask-image:var(--md-path-icon);mask-image:var(--md-path-icon);width:.8rem}.md-path__link{align-items:center;color:var(--md-default-fg-color--light);display:flex}.md-path__link:focus,.md-path__link:hover{color:var(--md-accent-fg-color)}:root{--md-post-pin-icon:url('data:image/svg+xml;charset=utf-8,')}.md-post__back{border-bottom:.05rem solid var(--md-default-fg-color--lightest);margin-bottom:1.2rem;padding-bottom:1.2rem}@media screen and (max-width:76.234375em){.md-post__back{display:none}}[dir=rtl] .md-post__back svg{transform:scaleX(-1)}.md-post__authors{display:flex;flex-direction:column;gap:.6rem;margin:0 .6rem 1.2rem}.md-post .md-post__meta a{transition:color 125ms}.md-post .md-post__meta a:focus,.md-post .md-post__meta a:hover{color:var(--md-accent-fg-color)}.md-post__title{color:var(--md-default-fg-color--light);font-weight:700}.md-post--excerpt{margin-bottom:3.2rem}.md-post--excerpt .md-post__header{align-items:center;display:flex;gap:.6rem;min-height:1.6rem}.md-post--excerpt .md-post__authors{align-items:center;display:inline-flex;flex-direction:row;gap:.2rem;margin:0;min-height:2.4rem}[dir=ltr] .md-post--excerpt .md-post__meta .md-meta__list{margin-right:.4rem}[dir=rtl] .md-post--excerpt .md-post__meta .md-meta__list{margin-left:.4rem}.md-post--excerpt .md-post__content>:first-child{--md-scroll-margin:6rem;margin-top:0}.md-post>.md-nav--secondary{margin:1em 0}.md-pin{background:var(--md-default-fg-color--lightest);border-radius:1rem;margin-top:-.05rem;padding:.2rem}.md-pin:after{background-color:currentcolor;content:"";display:block;height:.6rem;margin:0 auto;-webkit-mask-image:var(--md-post-pin-icon);mask-image:var(--md-post-pin-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:.6rem}.md-profile{align-items:center;display:flex;font-size:.7rem;gap:.6rem;line-height:1.4;width:100%}.md-profile__description{flex-grow:1}.md-content--post{display:flex}@media screen and (max-width:76.234375em){.md-content--post{flex-flow:column-reverse}}.md-content--post>.md-content__inner{flex-grow:1;min-width:0}@media screen and (min-width:76.25em){[dir=ltr] .md-content--post>.md-content__inner{margin-left:1.2rem}[dir=rtl] .md-content--post>.md-content__inner{margin-right:1.2rem}}@media screen and (max-width:76.234375em){.md-sidebar.md-sidebar--post{padding:0;position:static;width:100%}.md-sidebar.md-sidebar--post .md-sidebar__scrollwrap{overflow:visible}.md-sidebar.md-sidebar--post .md-sidebar__inner{padding:0}.md-sidebar.md-sidebar--post .md-post__meta{margin-left:.6rem;margin-right:.6rem}.md-sidebar.md-sidebar--post .md-nav__item{border:none;display:inline}.md-sidebar.md-sidebar--post .md-nav__list{display:inline-flex;flex-wrap:wrap;gap:.6rem;padding-bottom:.6rem;padding-top:.6rem}.md-sidebar.md-sidebar--post .md-nav__link{padding:0}.md-sidebar.md-sidebar--post .md-nav{height:auto;margin-bottom:0;position:static}}:root{--md-progress-value:0;--md-progress-delay:400ms}.md-progress{background:var(--md-primary-bg-color);height:.075rem;opacity:min(clamp(0,var(--md-progress-value),1),clamp(0,100 - var(--md-progress-value),1));position:fixed;top:0;transform:scaleX(calc(var(--md-progress-value)*1%));transform-origin:left;transition:transform .5s cubic-bezier(.19,1,.22,1),opacity .25s var(--md-progress-delay);width:100%;z-index:4}:root{--md-search-result-icon:url('data:image/svg+xml;charset=utf-8,')}.md-search{position:relative}@media screen and (min-width:60em){.md-search{padding:.2rem 0}}.no-js .md-search{display:none}.md-search__overlay{opacity:0;z-index:1}@media screen and (max-width:59.984375em){[dir=ltr] .md-search__overlay{left:-2.2rem}[dir=rtl] .md-search__overlay{right:-2.2rem}.md-search__overlay{background-color:var(--md-default-bg-color);border-radius:1rem;height:2rem;overflow:hidden;pointer-events:none;position:absolute;top:-1rem;transform-origin:center;transition:transform .3s .1s,opacity .2s .2s;width:2rem}[data-md-toggle=search]:checked~.md-header .md-search__overlay{opacity:1;transition:transform .4s,opacity .1s}}@media screen and (min-width:60em){[dir=ltr] .md-search__overlay{left:0}[dir=rtl] .md-search__overlay{right:0}.md-search__overlay{background-color:#0000008a;cursor:pointer;height:0;position:fixed;top:0;transition:width 0ms .25s,height 0ms .25s,opacity .25s;width:0}[data-md-toggle=search]:checked~.md-header .md-search__overlay{height:200vh;opacity:1;transition:width 0ms,height 0ms,opacity .25s;width:100%}}@media screen and (max-width:29.984375em){[data-md-toggle=search]:checked~.md-header .md-search__overlay{transform:scale(45)}}@media screen and (min-width:30em) and (max-width:44.984375em){[data-md-toggle=search]:checked~.md-header .md-search__overlay{transform:scale(60)}}@media screen and (min-width:45em) and (max-width:59.984375em){[data-md-toggle=search]:checked~.md-header .md-search__overlay{transform:scale(75)}}.md-search__inner{backface-visibility:hidden}@media screen and (max-width:59.984375em){[dir=ltr] .md-search__inner{left:0}[dir=rtl] .md-search__inner{right:0}.md-search__inner{height:0;opacity:0;overflow:hidden;position:fixed;top:0;transform:translateX(5%);transition:width 0ms .3s,height 0ms .3s,transform .15s cubic-bezier(.4,0,.2,1) .15s,opacity .15s .15s;width:0;z-index:2}[dir=rtl] .md-search__inner{transform:translateX(-5%)}[data-md-toggle=search]:checked~.md-header .md-search__inner{height:100%;opacity:1;transform:translateX(0);transition:width 0ms 0ms,height 0ms 0ms,transform .15s cubic-bezier(.1,.7,.1,1) .15s,opacity .15s .15s;width:100%}}@media screen and (min-width:60em){[dir=ltr] .md-search__inner{float:right}[dir=rtl] .md-search__inner{float:left}.md-search__inner{padding:.1rem 0;position:relative;transition:width .25s cubic-bezier(.1,.7,.1,1);width:11.7rem}}@media screen and (min-width:60em) and (max-width:76.234375em){[data-md-toggle=search]:checked~.md-header .md-search__inner{width:23.4rem}}@media screen and (min-width:76.25em){[data-md-toggle=search]:checked~.md-header .md-search__inner{width:34.4rem}}.md-search__form{background-color:var(--md-default-bg-color);box-shadow:0 0 .6rem #0000;height:2.4rem;position:relative;transition:color .25s,background-color .25s;z-index:2}@media screen and (min-width:60em){.md-search__form{background-color:#00000042;border-radius:.1rem;height:1.8rem}.md-search__form:hover{background-color:#ffffff1f}}[data-md-toggle=search]:checked~.md-header .md-search__form{background-color:var(--md-default-bg-color);border-radius:.1rem .1rem 0 0;box-shadow:0 0 .6rem #00000012;color:var(--md-default-fg-color)}[dir=ltr] .md-search__input{padding-left:3.6rem;padding-right:2.2rem}[dir=rtl] .md-search__input{padding-left:2.2rem;padding-right:3.6rem}.md-search__input{background:#0000;font-size:.9rem;height:100%;position:relative;text-overflow:ellipsis;width:100%;z-index:2}.md-search__input::placeholder{transition:color .25s}.md-search__input::placeholder,.md-search__input~.md-search__icon{color:var(--md-default-fg-color--light)}.md-search__input::-ms-clear{display:none}@media screen and (max-width:59.984375em){.md-search__input{font-size:.9rem;height:2.4rem;width:100%}}@media screen and (min-width:60em){[dir=ltr] .md-search__input{padding-left:2.2rem}[dir=rtl] .md-search__input{padding-right:2.2rem}.md-search__input{color:inherit;font-size:.8rem}.md-search__input::placeholder{color:var(--md-primary-bg-color--light)}.md-search__input+.md-search__icon{color:var(--md-primary-bg-color)}[data-md-toggle=search]:checked~.md-header .md-search__input{text-overflow:clip}[data-md-toggle=search]:checked~.md-header .md-search__input+.md-search__icon{color:var(--md-default-fg-color--light)}[data-md-toggle=search]:checked~.md-header .md-search__input::placeholder{color:#0000}}.md-search__icon{cursor:pointer;display:inline-block;height:1.2rem;transition:color .25s,opacity .25s;width:1.2rem}.md-search__icon:hover{opacity:.7}[dir=ltr] .md-search__icon[for=__search]{left:.5rem}[dir=rtl] .md-search__icon[for=__search]{right:.5rem}.md-search__icon[for=__search]{position:absolute;top:.3rem;z-index:2}[dir=rtl] .md-search__icon[for=__search] svg{transform:scaleX(-1)}@media screen and (max-width:59.984375em){[dir=ltr] .md-search__icon[for=__search]{left:.8rem}[dir=rtl] .md-search__icon[for=__search]{right:.8rem}.md-search__icon[for=__search]{top:.6rem}.md-search__icon[for=__search] svg:first-child{display:none}}@media screen and (min-width:60em){.md-search__icon[for=__search]{pointer-events:none}.md-search__icon[for=__search] svg:last-child{display:none}}[dir=ltr] .md-search__options{right:.5rem}[dir=rtl] .md-search__options{left:.5rem}.md-search__options{pointer-events:none;position:absolute;top:.3rem;z-index:2}@media screen and (max-width:59.984375em){[dir=ltr] .md-search__options{right:.8rem}[dir=rtl] .md-search__options{left:.8rem}.md-search__options{top:.6rem}}[dir=ltr] .md-search__options>.md-icon{margin-left:.2rem}[dir=rtl] .md-search__options>.md-icon{margin-right:.2rem}.md-search__options>.md-icon{color:var(--md-default-fg-color--light);opacity:0;transform:scale(.75);transition:transform .15s cubic-bezier(.1,.7,.1,1),opacity .15s}.md-search__options>.md-icon:not(.focus-visible){-webkit-tap-highlight-color:transparent;outline:none}[data-md-toggle=search]:checked~.md-header .md-search__input:valid~.md-search__options>.md-icon{opacity:1;pointer-events:auto;transform:scale(1)}[data-md-toggle=search]:checked~.md-header .md-search__input:valid~.md-search__options>.md-icon:hover{opacity:.7}[dir=ltr] .md-search__suggest{padding-left:3.6rem;padding-right:2.2rem}[dir=rtl] .md-search__suggest{padding-left:2.2rem;padding-right:3.6rem}.md-search__suggest{align-items:center;color:var(--md-default-fg-color--lighter);display:flex;font-size:.9rem;height:100%;opacity:0;position:absolute;top:0;transition:opacity 50ms;white-space:nowrap;width:100%}@media screen and (min-width:60em){[dir=ltr] .md-search__suggest{padding-left:2.2rem}[dir=rtl] .md-search__suggest{padding-right:2.2rem}.md-search__suggest{font-size:.8rem}}[data-md-toggle=search]:checked~.md-header .md-search__suggest{opacity:1;transition:opacity .3s .1s}[dir=ltr] .md-search__output{border-bottom-left-radius:.1rem}[dir=ltr] .md-search__output,[dir=rtl] .md-search__output{border-bottom-right-radius:.1rem}[dir=rtl] .md-search__output{border-bottom-left-radius:.1rem}.md-search__output{overflow:hidden;position:absolute;width:100%;z-index:1}@media screen and (max-width:59.984375em){.md-search__output{bottom:0;top:2.4rem}}@media screen and (min-width:60em){.md-search__output{opacity:0;top:1.9rem;transition:opacity .4s}[data-md-toggle=search]:checked~.md-header .md-search__output{box-shadow:var(--md-shadow-z3);opacity:1}}.md-search__scrollwrap{backface-visibility:hidden;background-color:var(--md-default-bg-color);height:100%;overflow-y:auto;touch-action:pan-y}@media (-webkit-max-device-pixel-ratio:1),(max-resolution:1dppx){.md-search__scrollwrap{transform:translateZ(0)}}@media screen and (min-width:60em) and (max-width:76.234375em){.md-search__scrollwrap{width:23.4rem}}@media screen and (min-width:76.25em){.md-search__scrollwrap{width:34.4rem}}@media screen and (min-width:60em){.md-search__scrollwrap{max-height:0;scrollbar-color:var(--md-default-fg-color--lighter) #0000;scrollbar-width:thin}[data-md-toggle=search]:checked~.md-header .md-search__scrollwrap{max-height:75vh}.md-search__scrollwrap:hover{scrollbar-color:var(--md-accent-fg-color) #0000}.md-search__scrollwrap::-webkit-scrollbar{height:.2rem;width:.2rem}.md-search__scrollwrap::-webkit-scrollbar-thumb{background-color:var(--md-default-fg-color--lighter)}.md-search__scrollwrap::-webkit-scrollbar-thumb:hover{background-color:var(--md-accent-fg-color)}}.md-search-result{color:var(--md-default-fg-color);word-break:break-word}.md-search-result__meta{background-color:var(--md-default-fg-color--lightest);color:var(--md-default-fg-color--light);font-size:.64rem;line-height:1.8rem;padding:0 .8rem;scroll-snap-align:start}@media screen and (min-width:60em){[dir=ltr] .md-search-result__meta{padding-left:2.2rem}[dir=rtl] .md-search-result__meta{padding-right:2.2rem}}.md-search-result__list{list-style:none;margin:0;padding:0;-webkit-user-select:none;user-select:none}.md-search-result__item{box-shadow:0 -.05rem var(--md-default-fg-color--lightest)}.md-search-result__item:first-child{box-shadow:none}.md-search-result__link{display:block;outline:none;scroll-snap-align:start;transition:background-color .25s}.md-search-result__link:focus,.md-search-result__link:hover{background-color:var(--md-accent-fg-color--transparent)}.md-search-result__link:last-child p:last-child{margin-bottom:.6rem}.md-search-result__more>summary{cursor:pointer;display:block;outline:none;position:sticky;scroll-snap-align:start;top:0;z-index:1}.md-search-result__more>summary::marker{display:none}.md-search-result__more>summary::-webkit-details-marker{display:none}.md-search-result__more>summary>div{color:var(--md-typeset-a-color);font-size:.64rem;padding:.75em .8rem;transition:color .25s,background-color .25s}@media screen and (min-width:60em){[dir=ltr] .md-search-result__more>summary>div{padding-left:2.2rem}[dir=rtl] .md-search-result__more>summary>div{padding-right:2.2rem}}.md-search-result__more>summary:focus>div,.md-search-result__more>summary:hover>div{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}.md-search-result__more[open]>summary{background-color:var(--md-default-bg-color)}.md-search-result__article{overflow:hidden;padding:0 .8rem;position:relative}@media screen and (min-width:60em){[dir=ltr] .md-search-result__article{padding-left:2.2rem}[dir=rtl] .md-search-result__article{padding-right:2.2rem}}[dir=ltr] .md-search-result__icon{left:0}[dir=rtl] .md-search-result__icon{right:0}.md-search-result__icon{color:var(--md-default-fg-color--light);height:1.2rem;margin:.5rem;position:absolute;width:1.2rem}@media screen and (max-width:59.984375em){.md-search-result__icon{display:none}}.md-search-result__icon:after{background-color:currentcolor;content:"";display:inline-block;height:100%;-webkit-mask-image:var(--md-search-result-icon);mask-image:var(--md-search-result-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:100%}[dir=rtl] .md-search-result__icon:after{transform:scaleX(-1)}.md-search-result .md-typeset{color:var(--md-default-fg-color--light);font-size:.64rem;line-height:1.6}.md-search-result .md-typeset h1{color:var(--md-default-fg-color);font-size:.8rem;font-weight:400;line-height:1.4;margin:.55rem 0}.md-search-result .md-typeset h1 mark{text-decoration:none}.md-search-result .md-typeset h2{color:var(--md-default-fg-color);font-size:.64rem;font-weight:700;line-height:1.6;margin:.5em 0}.md-search-result .md-typeset h2 mark{text-decoration:none}.md-search-result__terms{color:var(--md-default-fg-color);display:block;font-size:.64rem;font-style:italic;margin:.5em 0}.md-search-result mark{background-color:initial;color:var(--md-accent-fg-color);text-decoration:underline}.md-select{position:relative;z-index:1}.md-select__inner{background-color:var(--md-default-bg-color);border-radius:.1rem;box-shadow:var(--md-shadow-z2);color:var(--md-default-fg-color);left:50%;margin-top:.2rem;max-height:0;opacity:0;position:absolute;top:calc(100% - .2rem);transform:translate3d(-50%,.3rem,0);transition:transform .25s 375ms,opacity .25s .25s,max-height 0ms .5s}.md-select:focus-within .md-select__inner,.md-select:hover .md-select__inner{max-height:10rem;opacity:1;transform:translate3d(-50%,0,0);transition:transform .25s cubic-bezier(.1,.7,.1,1),opacity .25s,max-height 0ms}.md-select__inner:after{border-bottom:.2rem solid #0000;border-bottom-color:var(--md-default-bg-color);border-left:.2rem solid #0000;border-right:.2rem solid #0000;border-top:0;content:"";height:0;left:50%;margin-left:-.2rem;margin-top:-.2rem;position:absolute;top:0;width:0}.md-select__list{border-radius:.1rem;font-size:.8rem;list-style-type:none;margin:0;max-height:inherit;overflow:auto;padding:0}.md-select__item{line-height:1.8rem}[dir=ltr] .md-select__link{padding-left:.6rem;padding-right:1.2rem}[dir=rtl] .md-select__link{padding-left:1.2rem;padding-right:.6rem}.md-select__link{cursor:pointer;display:block;outline:none;scroll-snap-align:start;transition:background-color .25s,color .25s;width:100%}.md-select__link:focus,.md-select__link:hover{color:var(--md-accent-fg-color)}.md-select__link:focus{background-color:var(--md-default-fg-color--lightest)}.md-sidebar{align-self:flex-start;flex-shrink:0;padding:1.2rem 0;position:sticky;top:2.4rem;width:12.1rem}@media print{.md-sidebar{display:none}}@media screen and (max-width:76.234375em){[dir=ltr] .md-sidebar--primary{left:-12.1rem}[dir=rtl] .md-sidebar--primary{right:-12.1rem}.md-sidebar--primary{background-color:var(--md-default-bg-color);display:block;height:100%;position:fixed;top:0;transform:translateX(0);transition:transform .25s cubic-bezier(.4,0,.2,1),box-shadow .25s;width:12.1rem;z-index:5}[data-md-toggle=drawer]:checked~.md-container .md-sidebar--primary{box-shadow:var(--md-shadow-z3);transform:translateX(12.1rem)}[dir=rtl] [data-md-toggle=drawer]:checked~.md-container .md-sidebar--primary{transform:translateX(-12.1rem)}.md-sidebar--primary .md-sidebar__scrollwrap{bottom:0;left:0;margin:0;overflow:hidden;position:absolute;right:0;scroll-snap-type:none;top:0}}@media screen and (min-width:76.25em){.md-sidebar{height:0}.no-js .md-sidebar{height:auto}.md-header--lifted~.md-container .md-sidebar{top:4.8rem}}.md-sidebar--secondary{display:none;order:2}@media screen and (min-width:60em){.md-sidebar--secondary{height:0}.no-js .md-sidebar--secondary{height:auto}.md-sidebar--secondary:not([hidden]){display:block}.md-sidebar--secondary .md-sidebar__scrollwrap{touch-action:pan-y}}.md-sidebar__scrollwrap{backface-visibility:hidden;margin:0 .2rem;overflow-y:auto;scrollbar-color:var(--md-default-fg-color--lighter) #0000}@media screen and (min-width:60em){.md-sidebar__scrollwrap{scrollbar-gutter:stable;scrollbar-width:thin}}.md-sidebar__scrollwrap::-webkit-scrollbar{height:.2rem;width:.2rem}.md-sidebar__scrollwrap:focus-within,.md-sidebar__scrollwrap:hover{scrollbar-color:var(--md-accent-fg-color) #0000}.md-sidebar__scrollwrap:focus-within::-webkit-scrollbar-thumb,.md-sidebar__scrollwrap:hover::-webkit-scrollbar-thumb{background-color:var(--md-default-fg-color--lighter)}.md-sidebar__scrollwrap:focus-within::-webkit-scrollbar-thumb:hover,.md-sidebar__scrollwrap:hover::-webkit-scrollbar-thumb:hover{background-color:var(--md-accent-fg-color)}@supports selector(::-webkit-scrollbar){.md-sidebar__scrollwrap{scrollbar-gutter:auto}[dir=ltr] .md-sidebar__inner{padding-right:calc(100% - 11.5rem)}[dir=rtl] .md-sidebar__inner{padding-left:calc(100% - 11.5rem)}}@media screen and (max-width:76.234375em){.md-overlay{background-color:#0000008a;height:0;opacity:0;position:fixed;top:0;transition:width 0ms .25s,height 0ms .25s,opacity .25s;width:0;z-index:5}[data-md-toggle=drawer]:checked~.md-overlay{height:100%;opacity:1;transition:width 0ms,height 0ms,opacity .25s;width:100%}}@keyframes facts{0%{height:0}to{height:.65rem}}@keyframes fact{0%{opacity:0;transform:translateY(100%)}50%{opacity:0}to{opacity:1;transform:translateY(0)}}:root{--md-source-forks-icon:url('data:image/svg+xml;charset=utf-8,');--md-source-repositories-icon:url('data:image/svg+xml;charset=utf-8,');--md-source-stars-icon:url('data:image/svg+xml;charset=utf-8,');--md-source-version-icon:url('data:image/svg+xml;charset=utf-8,')}.md-source{backface-visibility:hidden;display:block;font-size:.65rem;line-height:1.2;outline-color:var(--md-accent-fg-color);transition:opacity .25s;white-space:nowrap}.md-source:hover{opacity:.7}.md-source__icon{display:inline-block;height:2.4rem;vertical-align:middle;width:2rem}[dir=ltr] .md-source__icon svg{margin-left:.6rem}[dir=rtl] .md-source__icon svg{margin-right:.6rem}.md-source__icon svg{margin-top:.6rem}[dir=ltr] .md-source__icon+.md-source__repository{padding-left:2rem}[dir=rtl] .md-source__icon+.md-source__repository{padding-right:2rem}[dir=ltr] .md-source__icon+.md-source__repository{margin-left:-2rem}[dir=rtl] .md-source__icon+.md-source__repository{margin-right:-2rem}[dir=ltr] .md-source__repository{margin-left:.6rem}[dir=rtl] .md-source__repository{margin-right:.6rem}.md-source__repository{display:inline-block;max-width:calc(100% - 1.2rem);overflow:hidden;text-overflow:ellipsis;vertical-align:middle}.md-source__facts{display:flex;font-size:.55rem;gap:.4rem;list-style-type:none;margin:.1rem 0 0;opacity:.75;overflow:hidden;padding:0;width:100%}.md-source__repository--active .md-source__facts{animation:facts .25s ease-in}.md-source__fact{overflow:hidden;text-overflow:ellipsis}.md-source__repository--active .md-source__fact{animation:fact .4s ease-out}[dir=ltr] .md-source__fact:before{margin-right:.1rem}[dir=rtl] .md-source__fact:before{margin-left:.1rem}.md-source__fact:before{background-color:currentcolor;content:"";display:inline-block;height:.6rem;-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;vertical-align:text-top;width:.6rem}.md-source__fact:nth-child(1n+2){flex-shrink:0}.md-source__fact--version:before{-webkit-mask-image:var(--md-source-version-icon);mask-image:var(--md-source-version-icon)}.md-source__fact--stars:before{-webkit-mask-image:var(--md-source-stars-icon);mask-image:var(--md-source-stars-icon)}.md-source__fact--forks:before{-webkit-mask-image:var(--md-source-forks-icon);mask-image:var(--md-source-forks-icon)}.md-source__fact--repositories:before{-webkit-mask-image:var(--md-source-repositories-icon);mask-image:var(--md-source-repositories-icon)}.md-source-file{margin:1em 0}[dir=ltr] .md-source-file__fact{margin-right:.6rem}[dir=rtl] .md-source-file__fact{margin-left:.6rem}.md-source-file__fact{align-items:center;color:var(--md-default-fg-color--light);display:inline-flex;font-size:.68rem;gap:.3rem}.md-source-file__fact .md-icon{flex-shrink:0;margin-bottom:.05rem}[dir=ltr] .md-source-file__fact .md-author{float:left}[dir=rtl] .md-source-file__fact .md-author{float:right}.md-source-file__fact .md-author{margin-right:.2rem}.md-source-file__fact svg{width:.9rem}:root{--md-status:url('data:image/svg+xml;charset=utf-8,');--md-status--new:url('data:image/svg+xml;charset=utf-8,');--md-status--deprecated:url('data:image/svg+xml;charset=utf-8,');--md-status--encrypted:url('data:image/svg+xml;charset=utf-8,')}.md-status:after{background-color:var(--md-default-fg-color--light);content:"";display:inline-block;height:1.125em;-webkit-mask-image:var(--md-status);mask-image:var(--md-status);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;vertical-align:text-bottom;width:1.125em}.md-status:hover:after{background-color:currentcolor}.md-status--new:after{-webkit-mask-image:var(--md-status--new);mask-image:var(--md-status--new)}.md-status--deprecated:after{-webkit-mask-image:var(--md-status--deprecated);mask-image:var(--md-status--deprecated)}.md-status--encrypted:after{-webkit-mask-image:var(--md-status--encrypted);mask-image:var(--md-status--encrypted)}.md-tabs{background-color:var(--md-primary-fg-color);color:var(--md-primary-bg-color);display:block;line-height:1.3;overflow:auto;width:100%;z-index:3}@media print{.md-tabs{display:none}}@media screen and (max-width:76.234375em){.md-tabs{display:none}}.md-tabs[hidden]{pointer-events:none}[dir=ltr] .md-tabs__list{margin-left:.2rem}[dir=rtl] .md-tabs__list{margin-right:.2rem}.md-tabs__list{contain:content;display:flex;list-style:none;margin:0;overflow:auto;padding:0;scrollbar-width:none;white-space:nowrap}.md-tabs__list::-webkit-scrollbar{display:none}.md-tabs__item{height:2.4rem;padding-left:.6rem;padding-right:.6rem}.md-tabs__item--active .md-tabs__link{color:inherit;opacity:1}.md-tabs__link{backface-visibility:hidden;display:flex;font-size:.7rem;margin-top:.8rem;opacity:.7;outline-color:var(--md-accent-fg-color);outline-offset:.2rem;transition:transform .4s cubic-bezier(.1,.7,.1,1),opacity .25s}.md-tabs__link:focus,.md-tabs__link:hover{color:inherit;opacity:1}[dir=ltr] .md-tabs__link svg{margin-right:.4rem}[dir=rtl] .md-tabs__link svg{margin-left:.4rem}.md-tabs__link svg{fill:currentcolor;height:1.3em}.md-tabs__item:nth-child(2) .md-tabs__link{transition-delay:20ms}.md-tabs__item:nth-child(3) .md-tabs__link{transition-delay:40ms}.md-tabs__item:nth-child(4) .md-tabs__link{transition-delay:60ms}.md-tabs__item:nth-child(5) .md-tabs__link{transition-delay:80ms}.md-tabs__item:nth-child(6) .md-tabs__link{transition-delay:.1s}.md-tabs__item:nth-child(7) .md-tabs__link{transition-delay:.12s}.md-tabs__item:nth-child(8) .md-tabs__link{transition-delay:.14s}.md-tabs__item:nth-child(9) .md-tabs__link{transition-delay:.16s}.md-tabs__item:nth-child(10) .md-tabs__link{transition-delay:.18s}.md-tabs__item:nth-child(11) .md-tabs__link{transition-delay:.2s}.md-tabs__item:nth-child(12) .md-tabs__link{transition-delay:.22s}.md-tabs__item:nth-child(13) .md-tabs__link{transition-delay:.24s}.md-tabs__item:nth-child(14) .md-tabs__link{transition-delay:.26s}.md-tabs__item:nth-child(15) .md-tabs__link{transition-delay:.28s}.md-tabs__item:nth-child(16) .md-tabs__link{transition-delay:.3s}.md-tabs[hidden] .md-tabs__link{opacity:0;transform:translateY(50%);transition:transform 0ms .1s,opacity .1s}:root{--md-tag-icon:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .md-tags:not([hidden]){display:inline-flex;flex-wrap:wrap;gap:.5em;margin-bottom:.75em;margin-top:-.125em}.md-typeset .md-tag{align-items:center;background:var(--md-default-fg-color--lightest);border-radius:2.4rem;display:inline-flex;font-size:.64rem;font-size:min(.8em,.64rem);font-weight:700;gap:.5em;letter-spacing:normal;line-height:1.6;padding:.3125em .78125em}.md-typeset .md-tag[href]{-webkit-tap-highlight-color:transparent;color:inherit;outline:none;transition:color 125ms,background-color 125ms}.md-typeset .md-tag[href]:focus,.md-typeset .md-tag[href]:hover{background-color:var(--md-accent-fg-color);color:var(--md-accent-bg-color)}[id]>.md-typeset .md-tag{vertical-align:text-top}.md-typeset .md-tag-shadow{opacity:.5}.md-typeset .md-tag-icon:before{background-color:var(--md-default-fg-color--lighter);content:"";display:inline-block;height:1.2em;-webkit-mask-image:var(--md-tag-icon);mask-image:var(--md-tag-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;transition:background-color 125ms;vertical-align:text-bottom;width:1.2em}.md-typeset .md-tag-icon[href]:focus:before,.md-typeset .md-tag-icon[href]:hover:before{background-color:var(--md-accent-bg-color)}@keyframes pulse{0%{transform:scale(.95)}75%{transform:scale(1)}to{transform:scale(.95)}}:root{--md-annotation-bg-icon:url('data:image/svg+xml;charset=utf-8,');--md-annotation-icon:url('data:image/svg+xml;charset=utf-8,')}.md-tooltip{backface-visibility:hidden;background-color:var(--md-default-bg-color);border-radius:.1rem;box-shadow:var(--md-shadow-z2);color:var(--md-default-fg-color);font-family:var(--md-text-font-family);left:clamp(var(--md-tooltip-0,0rem) + .8rem,var(--md-tooltip-x),100vw + var(--md-tooltip-0,0rem) + .8rem - var(--md-tooltip-width) - 2 * .8rem);max-width:calc(100vw - 1.6rem);opacity:0;position:absolute;top:var(--md-tooltip-y);transform:translateY(-.4rem);transition:transform 0ms .25s,opacity .25s,z-index .25s;width:var(--md-tooltip-width);z-index:0}.md-tooltip--active{opacity:1;transform:translateY(0);transition:transform .25s cubic-bezier(.1,.7,.1,1),opacity .25s,z-index 0ms;z-index:2}.md-tooltip--inline{font-weight:700;-webkit-user-select:none;user-select:none;width:auto}.md-tooltip--inline:not(.md-tooltip--active){transform:translateY(.2rem) scale(.9)}.md-tooltip--inline .md-tooltip__inner{font-size:.5rem;padding:.2rem .4rem}[hidden]+.md-tooltip--inline{display:none}.focus-visible>.md-tooltip,.md-tooltip:target{outline:var(--md-accent-fg-color) auto}.md-tooltip__inner{font-size:.64rem;padding:.8rem}.md-tooltip__inner.md-typeset>:first-child{margin-top:0}.md-tooltip__inner.md-typeset>:last-child{margin-bottom:0}.md-annotation{font-style:normal;font-weight:400;outline:none;text-align:initial;vertical-align:text-bottom;white-space:normal}[dir=rtl] .md-annotation{direction:rtl}code .md-annotation{font-family:var(--md-code-font-family);font-size:inherit}.md-annotation:not([hidden]){display:inline-block;line-height:1.25}.md-annotation__index{border-radius:.01px;cursor:pointer;display:inline-block;margin-left:.4ch;margin-right:.4ch;outline:none;overflow:hidden;position:relative;-webkit-user-select:none;user-select:none;vertical-align:text-top;z-index:0}.md-annotation .md-annotation__index{transition:z-index .25s}@media screen{.md-annotation__index{width:2.2ch}[data-md-visible]>.md-annotation__index{animation:pulse 2s infinite}.md-annotation__index:before{background:var(--md-default-bg-color);-webkit-mask-image:var(--md-annotation-bg-icon);mask-image:var(--md-annotation-bg-icon)}.md-annotation__index:after,.md-annotation__index:before{content:"";height:2.2ch;-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;position:absolute;top:-.1ch;width:2.2ch;z-index:-1}.md-annotation__index:after{background-color:var(--md-default-fg-color--lighter);-webkit-mask-image:var(--md-annotation-icon);mask-image:var(--md-annotation-icon);transform:scale(1.0001);transition:background-color .25s,transform .25s}.md-tooltip--active+.md-annotation__index:after{transform:rotate(45deg)}.md-tooltip--active+.md-annotation__index:after,:hover>.md-annotation__index:after{background-color:var(--md-accent-fg-color)}}.md-tooltip--active+.md-annotation__index{animation-play-state:paused;transition-duration:0ms;z-index:2}.md-annotation__index [data-md-annotation-id]{display:inline-block}@media print{.md-annotation__index [data-md-annotation-id]{background:var(--md-default-fg-color--lighter);border-radius:2ch;color:var(--md-default-bg-color);font-weight:700;padding:0 .6ch;white-space:nowrap}.md-annotation__index [data-md-annotation-id]:after{content:attr(data-md-annotation-id)}}.md-typeset .md-annotation-list{counter-reset:annotation;list-style:none!important}.md-typeset .md-annotation-list li{position:relative}[dir=ltr] .md-typeset .md-annotation-list li:before{left:-2.125em}[dir=rtl] .md-typeset .md-annotation-list li:before{right:-2.125em}.md-typeset .md-annotation-list li:before{background:var(--md-default-fg-color--lighter);border-radius:2ch;color:var(--md-default-bg-color);content:counter(annotation);counter-increment:annotation;font-size:.8875em;font-weight:700;height:2ch;line-height:1.25;min-width:2ch;padding:0 .6ch;position:absolute;text-align:center;top:.25em}:root{--md-tooltip-width:20rem;--md-tooltip-tail:0.3rem}.md-tooltip2{backface-visibility:hidden;color:var(--md-default-fg-color);font-family:var(--md-text-font-family);opacity:0;pointer-events:none;position:absolute;top:calc(var(--md-tooltip-host-y) + var(--md-tooltip-y));transform:translateY(-.4rem);transform-origin:calc(var(--md-tooltip-host-x) + var(--md-tooltip-x)) 0;transition:transform 0ms .25s,opacity .25s,z-index .25s;width:100%;z-index:0}.md-tooltip2:before{border-left:var(--md-tooltip-tail) solid #0000;border-right:var(--md-tooltip-tail) solid #0000;content:"";display:block;left:clamp(1.5 * .8rem,var(--md-tooltip-host-x) + var(--md-tooltip-x) - var(--md-tooltip-tail),100vw - 2 * var(--md-tooltip-tail) - 1.5 * .8rem);position:absolute;z-index:1}.md-tooltip2--top:before{border-top:var(--md-tooltip-tail) solid var(--md-default-bg-color);bottom:calc(var(--md-tooltip-tail)*-1 + .025rem);filter:drop-shadow(0 1px 0 hsla(0,0%,0%,.05))}.md-tooltip2--bottom:before{border-bottom:var(--md-tooltip-tail) solid var(--md-default-bg-color);filter:drop-shadow(0 -1px 0 hsla(0,0%,0%,.05));top:calc(var(--md-tooltip-tail)*-1 + .025rem)}.md-tooltip2--active{opacity:1;transform:translateY(0);transition:transform .4s cubic-bezier(0,1,.5,1),opacity .25s,z-index 0ms;z-index:2}.md-tooltip2__inner{scrollbar-gutter:stable;background-color:var(--md-default-bg-color);border-radius:.1rem;box-shadow:var(--md-shadow-z2);left:clamp(.8rem,var(--md-tooltip-host-x) - .8rem,100vw - var(--md-tooltip-width) - .8rem);max-height:40vh;max-width:calc(100vw - 1.6rem);position:relative;scrollbar-width:thin}.md-tooltip2__inner::-webkit-scrollbar{height:.2rem;width:.2rem}.md-tooltip2__inner::-webkit-scrollbar-thumb{background-color:var(--md-default-fg-color--lighter)}.md-tooltip2__inner::-webkit-scrollbar-thumb:hover{background-color:var(--md-accent-fg-color)}[role=dialog]>.md-tooltip2__inner{font-size:.64rem;overflow:auto;padding:0 .8rem;pointer-events:auto;width:var(--md-tooltip-width)}[role=dialog]>.md-tooltip2__inner:after,[role=dialog]>.md-tooltip2__inner:before{content:"";display:block;height:.8rem;position:sticky;width:100%;z-index:10}[role=dialog]>.md-tooltip2__inner:before{background:linear-gradient(var(--md-default-bg-color),#0000 75%);top:0}[role=dialog]>.md-tooltip2__inner:after{background:linear-gradient(#0000,var(--md-default-bg-color) 75%);bottom:0}[role=tooltip]>.md-tooltip2__inner{font-size:.5rem;font-weight:700;left:clamp(.8rem,var(--md-tooltip-host-x) + var(--md-tooltip-x) - var(--md-tooltip-width)/2,100vw - var(--md-tooltip-width) - .8rem);max-width:min(100vw - 2 * .8rem,400px);padding:.2rem .4rem;-webkit-user-select:none;user-select:none;width:fit-content}.md-tooltip2__inner.md-typeset>:first-child{margin-top:0}.md-tooltip2__inner.md-typeset>:last-child{margin-bottom:0}[dir=ltr] .md-top{margin-left:50%}[dir=rtl] .md-top{margin-right:50%}.md-top{background-color:var(--md-default-bg-color);border-radius:1.6rem;box-shadow:var(--md-shadow-z2);color:var(--md-default-fg-color--light);cursor:pointer;display:block;font-size:.7rem;outline:none;padding:.4rem .8rem;position:fixed;top:3.2rem;transform:translate(-50%);transition:color 125ms,background-color 125ms,transform 125ms cubic-bezier(.4,0,.2,1),opacity 125ms;z-index:2}@media print{.md-top{display:none}}[dir=rtl] .md-top{transform:translate(50%)}.md-top[hidden]{opacity:0;pointer-events:none;transform:translate(-50%,.2rem);transition-duration:0ms}[dir=rtl] .md-top[hidden]{transform:translate(50%,.2rem)}.md-top:focus,.md-top:hover{background-color:var(--md-accent-fg-color);color:var(--md-accent-bg-color)}.md-top svg{display:inline-block;vertical-align:-.5em}@keyframes hoverfix{0%{pointer-events:none}}:root{--md-version-icon:url('data:image/svg+xml;charset=utf-8,')}.md-version{flex-shrink:0;font-size:.8rem;height:2.4rem}[dir=ltr] .md-version__current{margin-left:1.4rem;margin-right:.4rem}[dir=rtl] .md-version__current{margin-left:.4rem;margin-right:1.4rem}.md-version__current{color:inherit;cursor:pointer;outline:none;position:relative;top:.05rem}[dir=ltr] .md-version__current:after{margin-left:.4rem}[dir=rtl] .md-version__current:after{margin-right:.4rem}.md-version__current:after{background-color:currentcolor;content:"";display:inline-block;height:.6rem;-webkit-mask-image:var(--md-version-icon);mask-image:var(--md-version-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:.4rem}.md-version__alias{margin-left:.3rem;opacity:.7}.md-version__list{background-color:var(--md-default-bg-color);border-radius:.1rem;box-shadow:var(--md-shadow-z2);color:var(--md-default-fg-color);list-style-type:none;margin:.2rem .8rem;max-height:0;opacity:0;overflow:auto;padding:0;position:absolute;scroll-snap-type:y mandatory;top:.15rem;transition:max-height 0ms .5s,opacity .25s .25s;z-index:3}.md-version:focus-within .md-version__list,.md-version:hover .md-version__list{max-height:10rem;opacity:1;transition:max-height 0ms,opacity .25s}@media (hover:none),(pointer:coarse){.md-version:hover .md-version__list{animation:hoverfix .25s forwards}.md-version:focus-within .md-version__list{animation:none}}.md-version__item{line-height:1.8rem}[dir=ltr] .md-version__link{padding-left:.6rem;padding-right:1.2rem}[dir=rtl] .md-version__link{padding-left:1.2rem;padding-right:.6rem}.md-version__link{cursor:pointer;display:block;outline:none;scroll-snap-align:start;transition:color .25s,background-color .25s;white-space:nowrap;width:100%}.md-version__link:focus,.md-version__link:hover{color:var(--md-accent-fg-color)}.md-version__link:focus{background-color:var(--md-default-fg-color--lightest)}:root{--md-admonition-icon--note:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--abstract:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--info:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--tip:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--success:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--question:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--warning:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--failure:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--danger:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--bug:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--example:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--quote:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .admonition,.md-typeset details{background-color:var(--md-admonition-bg-color);border:.075rem solid #448aff;border-radius:.2rem;box-shadow:var(--md-shadow-z1);color:var(--md-admonition-fg-color);display:flow-root;font-size:.64rem;margin:1.5625em 0;padding:0 .6rem;page-break-inside:avoid;transition:box-shadow 125ms}@media print{.md-typeset .admonition,.md-typeset details{box-shadow:none}}.md-typeset .admonition:focus-within,.md-typeset details:focus-within{box-shadow:0 0 0 .2rem #448aff1a}.md-typeset .admonition>*,.md-typeset details>*{box-sizing:border-box}.md-typeset .admonition .admonition,.md-typeset .admonition details,.md-typeset details .admonition,.md-typeset details details{margin-bottom:1em;margin-top:1em}.md-typeset .admonition .md-typeset__scrollwrap,.md-typeset details .md-typeset__scrollwrap{margin:1em -.6rem}.md-typeset .admonition .md-typeset__table,.md-typeset details .md-typeset__table{padding:0 .6rem}.md-typeset .admonition>.tabbed-set:only-child,.md-typeset details>.tabbed-set:only-child{margin-top:0}html .md-typeset .admonition>:last-child,html .md-typeset details>:last-child{margin-bottom:.6rem}[dir=ltr] .md-typeset .admonition-title,[dir=ltr] .md-typeset summary{padding-left:2rem;padding-right:.6rem}[dir=rtl] .md-typeset .admonition-title,[dir=rtl] .md-typeset summary{padding-left:.6rem;padding-right:2rem}[dir=ltr] .md-typeset .admonition-title,[dir=ltr] .md-typeset summary{border-left-width:.2rem}[dir=rtl] .md-typeset .admonition-title,[dir=rtl] .md-typeset summary{border-right-width:.2rem}[dir=ltr] .md-typeset .admonition-title,[dir=ltr] .md-typeset summary{border-top-left-radius:.1rem}[dir=ltr] .md-typeset .admonition-title,[dir=ltr] .md-typeset summary,[dir=rtl] .md-typeset .admonition-title,[dir=rtl] .md-typeset summary{border-top-right-radius:.1rem}[dir=rtl] .md-typeset .admonition-title,[dir=rtl] .md-typeset summary{border-top-left-radius:.1rem}.md-typeset .admonition-title,.md-typeset summary{background-color:#448aff1a;border:none;font-weight:700;margin:0 -.6rem;padding-bottom:.4rem;padding-top:.4rem;position:relative}html .md-typeset .admonition-title:last-child,html .md-typeset summary:last-child{margin-bottom:0}[dir=ltr] .md-typeset .admonition-title:before,[dir=ltr] .md-typeset summary:before{left:.6rem}[dir=rtl] .md-typeset .admonition-title:before,[dir=rtl] .md-typeset summary:before{right:.6rem}.md-typeset .admonition-title:before,.md-typeset summary:before{background-color:#448aff;content:"";height:1rem;-webkit-mask-image:var(--md-admonition-icon--note);mask-image:var(--md-admonition-icon--note);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;position:absolute;top:.625em;width:1rem}.md-typeset .admonition-title code,.md-typeset summary code{box-shadow:0 0 0 .05rem var(--md-default-fg-color--lightest)}.md-typeset .admonition.note,.md-typeset details.note{border-color:#448aff}.md-typeset .admonition.note:focus-within,.md-typeset details.note:focus-within{box-shadow:0 0 0 .2rem #448aff1a}.md-typeset .note>.admonition-title,.md-typeset .note>summary{background-color:#448aff1a}.md-typeset .note>.admonition-title:before,.md-typeset .note>summary:before{background-color:#448aff;-webkit-mask-image:var(--md-admonition-icon--note);mask-image:var(--md-admonition-icon--note)}.md-typeset .note>.admonition-title:after,.md-typeset .note>summary:after{color:#448aff}.md-typeset .admonition.abstract,.md-typeset details.abstract{border-color:#00b0ff}.md-typeset .admonition.abstract:focus-within,.md-typeset details.abstract:focus-within{box-shadow:0 0 0 .2rem #00b0ff1a}.md-typeset .abstract>.admonition-title,.md-typeset .abstract>summary{background-color:#00b0ff1a}.md-typeset .abstract>.admonition-title:before,.md-typeset .abstract>summary:before{background-color:#00b0ff;-webkit-mask-image:var(--md-admonition-icon--abstract);mask-image:var(--md-admonition-icon--abstract)}.md-typeset .abstract>.admonition-title:after,.md-typeset .abstract>summary:after{color:#00b0ff}.md-typeset .admonition.info,.md-typeset details.info{border-color:#00b8d4}.md-typeset .admonition.info:focus-within,.md-typeset details.info:focus-within{box-shadow:0 0 0 .2rem #00b8d41a}.md-typeset .info>.admonition-title,.md-typeset .info>summary{background-color:#00b8d41a}.md-typeset .info>.admonition-title:before,.md-typeset .info>summary:before{background-color:#00b8d4;-webkit-mask-image:var(--md-admonition-icon--info);mask-image:var(--md-admonition-icon--info)}.md-typeset .info>.admonition-title:after,.md-typeset .info>summary:after{color:#00b8d4}.md-typeset .admonition.tip,.md-typeset details.tip{border-color:#00bfa5}.md-typeset .admonition.tip:focus-within,.md-typeset details.tip:focus-within{box-shadow:0 0 0 .2rem #00bfa51a}.md-typeset .tip>.admonition-title,.md-typeset .tip>summary{background-color:#00bfa51a}.md-typeset .tip>.admonition-title:before,.md-typeset .tip>summary:before{background-color:#00bfa5;-webkit-mask-image:var(--md-admonition-icon--tip);mask-image:var(--md-admonition-icon--tip)}.md-typeset .tip>.admonition-title:after,.md-typeset .tip>summary:after{color:#00bfa5}.md-typeset .admonition.success,.md-typeset details.success{border-color:#00c853}.md-typeset .admonition.success:focus-within,.md-typeset details.success:focus-within{box-shadow:0 0 0 .2rem #00c8531a}.md-typeset .success>.admonition-title,.md-typeset .success>summary{background-color:#00c8531a}.md-typeset .success>.admonition-title:before,.md-typeset .success>summary:before{background-color:#00c853;-webkit-mask-image:var(--md-admonition-icon--success);mask-image:var(--md-admonition-icon--success)}.md-typeset .success>.admonition-title:after,.md-typeset .success>summary:after{color:#00c853}.md-typeset .admonition.question,.md-typeset details.question{border-color:#64dd17}.md-typeset .admonition.question:focus-within,.md-typeset details.question:focus-within{box-shadow:0 0 0 .2rem #64dd171a}.md-typeset .question>.admonition-title,.md-typeset .question>summary{background-color:#64dd171a}.md-typeset .question>.admonition-title:before,.md-typeset .question>summary:before{background-color:#64dd17;-webkit-mask-image:var(--md-admonition-icon--question);mask-image:var(--md-admonition-icon--question)}.md-typeset .question>.admonition-title:after,.md-typeset .question>summary:after{color:#64dd17}.md-typeset .admonition.warning,.md-typeset details.warning{border-color:#ff9100}.md-typeset .admonition.warning:focus-within,.md-typeset details.warning:focus-within{box-shadow:0 0 0 .2rem #ff91001a}.md-typeset .warning>.admonition-title,.md-typeset .warning>summary{background-color:#ff91001a}.md-typeset .warning>.admonition-title:before,.md-typeset .warning>summary:before{background-color:#ff9100;-webkit-mask-image:var(--md-admonition-icon--warning);mask-image:var(--md-admonition-icon--warning)}.md-typeset .warning>.admonition-title:after,.md-typeset .warning>summary:after{color:#ff9100}.md-typeset .admonition.failure,.md-typeset details.failure{border-color:#ff5252}.md-typeset .admonition.failure:focus-within,.md-typeset details.failure:focus-within{box-shadow:0 0 0 .2rem #ff52521a}.md-typeset .failure>.admonition-title,.md-typeset .failure>summary{background-color:#ff52521a}.md-typeset .failure>.admonition-title:before,.md-typeset .failure>summary:before{background-color:#ff5252;-webkit-mask-image:var(--md-admonition-icon--failure);mask-image:var(--md-admonition-icon--failure)}.md-typeset .failure>.admonition-title:after,.md-typeset .failure>summary:after{color:#ff5252}.md-typeset .admonition.danger,.md-typeset details.danger{border-color:#ff1744}.md-typeset .admonition.danger:focus-within,.md-typeset details.danger:focus-within{box-shadow:0 0 0 .2rem #ff17441a}.md-typeset .danger>.admonition-title,.md-typeset .danger>summary{background-color:#ff17441a}.md-typeset .danger>.admonition-title:before,.md-typeset .danger>summary:before{background-color:#ff1744;-webkit-mask-image:var(--md-admonition-icon--danger);mask-image:var(--md-admonition-icon--danger)}.md-typeset .danger>.admonition-title:after,.md-typeset .danger>summary:after{color:#ff1744}.md-typeset .admonition.bug,.md-typeset details.bug{border-color:#f50057}.md-typeset .admonition.bug:focus-within,.md-typeset details.bug:focus-within{box-shadow:0 0 0 .2rem #f500571a}.md-typeset .bug>.admonition-title,.md-typeset .bug>summary{background-color:#f500571a}.md-typeset .bug>.admonition-title:before,.md-typeset .bug>summary:before{background-color:#f50057;-webkit-mask-image:var(--md-admonition-icon--bug);mask-image:var(--md-admonition-icon--bug)}.md-typeset .bug>.admonition-title:after,.md-typeset .bug>summary:after{color:#f50057}.md-typeset .admonition.example,.md-typeset details.example{border-color:#7c4dff}.md-typeset .admonition.example:focus-within,.md-typeset details.example:focus-within{box-shadow:0 0 0 .2rem #7c4dff1a}.md-typeset .example>.admonition-title,.md-typeset .example>summary{background-color:#7c4dff1a}.md-typeset .example>.admonition-title:before,.md-typeset .example>summary:before{background-color:#7c4dff;-webkit-mask-image:var(--md-admonition-icon--example);mask-image:var(--md-admonition-icon--example)}.md-typeset .example>.admonition-title:after,.md-typeset .example>summary:after{color:#7c4dff}.md-typeset .admonition.quote,.md-typeset details.quote{border-color:#9e9e9e}.md-typeset .admonition.quote:focus-within,.md-typeset details.quote:focus-within{box-shadow:0 0 0 .2rem #9e9e9e1a}.md-typeset .quote>.admonition-title,.md-typeset .quote>summary{background-color:#9e9e9e1a}.md-typeset .quote>.admonition-title:before,.md-typeset .quote>summary:before{background-color:#9e9e9e;-webkit-mask-image:var(--md-admonition-icon--quote);mask-image:var(--md-admonition-icon--quote)}.md-typeset .quote>.admonition-title:after,.md-typeset .quote>summary:after{color:#9e9e9e}:root{--md-footnotes-icon:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .footnote{color:var(--md-default-fg-color--light);font-size:.64rem}[dir=ltr] .md-typeset .footnote>ol{margin-left:0}[dir=rtl] .md-typeset .footnote>ol{margin-right:0}.md-typeset .footnote>ol>li{transition:color 125ms}.md-typeset .footnote>ol>li:target{color:var(--md-default-fg-color)}.md-typeset .footnote>ol>li:focus-within .footnote-backref{opacity:1;transform:translateX(0);transition:none}.md-typeset .footnote>ol>li:hover .footnote-backref,.md-typeset .footnote>ol>li:target .footnote-backref{opacity:1;transform:translateX(0)}.md-typeset .footnote>ol>li>:first-child{margin-top:0}.md-typeset .footnote-ref{font-size:.75em;font-weight:700}html .md-typeset .footnote-ref{outline-offset:.1rem}.md-typeset [id^="fnref:"]:target>.footnote-ref{outline:auto}.md-typeset .footnote-backref{color:var(--md-typeset-a-color);display:inline-block;font-size:0;opacity:0;transform:translateX(.25rem);transition:color .25s,transform .25s .25s,opacity 125ms .25s;vertical-align:text-bottom}@media print{.md-typeset .footnote-backref{color:var(--md-typeset-a-color);opacity:1;transform:translateX(0)}}[dir=rtl] .md-typeset .footnote-backref{transform:translateX(-.25rem)}.md-typeset .footnote-backref:hover{color:var(--md-accent-fg-color)}.md-typeset .footnote-backref:before{background-color:currentcolor;content:"";display:inline-block;height:.8rem;-webkit-mask-image:var(--md-footnotes-icon);mask-image:var(--md-footnotes-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:.8rem}[dir=rtl] .md-typeset .footnote-backref:before{transform:scaleX(-1)}[dir=ltr] .md-typeset .headerlink{margin-left:.5rem}[dir=rtl] .md-typeset .headerlink{margin-right:.5rem}.md-typeset .headerlink{color:var(--md-default-fg-color--lighter);display:inline-block;opacity:0;transition:color .25s,opacity 125ms}@media print{.md-typeset .headerlink{display:none}}.md-typeset .headerlink:focus,.md-typeset :hover>.headerlink,.md-typeset :target>.headerlink{opacity:1;transition:color .25s,opacity 125ms}.md-typeset .headerlink:focus,.md-typeset .headerlink:hover,.md-typeset :target>.headerlink{color:var(--md-accent-fg-color)}.md-typeset :target{--md-scroll-margin:3.6rem;--md-scroll-offset:0rem;scroll-margin-top:calc(var(--md-scroll-margin) - var(--md-scroll-offset))}@media screen and (min-width:76.25em){.md-header--lifted~.md-container .md-typeset :target{--md-scroll-margin:6rem}}.md-typeset h1:target,.md-typeset h2:target,.md-typeset h3:target{--md-scroll-offset:0.2rem}.md-typeset h4:target{--md-scroll-offset:0.15rem}.md-typeset div.arithmatex{overflow:auto}@media screen and (max-width:44.984375em){.md-typeset div.arithmatex{margin:0 -.8rem}.md-typeset div.arithmatex>*{width:min-content}}.md-typeset div.arithmatex>*{margin-left:auto!important;margin-right:auto!important;padding:0 .8rem;touch-action:auto}.md-typeset div.arithmatex>* mjx-container{margin:0!important}.md-typeset div.arithmatex mjx-assistive-mml{height:0}.md-typeset .katex-html svg{max-width:revert}.md-typeset del.critic{background-color:var(--md-typeset-del-color)}.md-typeset del.critic,.md-typeset ins.critic{-webkit-box-decoration-break:clone;box-decoration-break:clone}.md-typeset ins.critic{background-color:var(--md-typeset-ins-color)}.md-typeset .critic.comment{-webkit-box-decoration-break:clone;box-decoration-break:clone;color:var(--md-code-hl-comment-color)}.md-typeset .critic.comment:before{content:"/* "}.md-typeset .critic.comment:after{content:" */"}.md-typeset .critic.block{box-shadow:none;display:block;margin:1em 0;overflow:auto;padding-left:.8rem;padding-right:.8rem}.md-typeset .critic.block>:first-child{margin-top:.5em}.md-typeset .critic.block>:last-child{margin-bottom:.5em}:root{--md-details-icon:url('data:image/svg+xml;charset=utf-8,')}.md-typeset details{display:flow-root;overflow:visible;padding-top:0}.md-typeset details[open]>summary:after{transform:rotate(90deg)}.md-typeset details:not([open]){box-shadow:none;padding-bottom:0}.md-typeset details:not([open])>summary{border-radius:.1rem}[dir=ltr] .md-typeset summary{padding-right:1.8rem}[dir=rtl] .md-typeset summary{padding-left:1.8rem}[dir=ltr] .md-typeset summary{border-top-left-radius:.1rem}[dir=ltr] .md-typeset summary,[dir=rtl] .md-typeset summary{border-top-right-radius:.1rem}[dir=rtl] .md-typeset summary{border-top-left-radius:.1rem}.md-typeset summary{cursor:pointer;display:block;min-height:1rem;overflow:hidden}.md-typeset summary.focus-visible{outline-color:var(--md-accent-fg-color);outline-offset:.2rem}.md-typeset summary:not(.focus-visible){-webkit-tap-highlight-color:transparent;outline:none}[dir=ltr] .md-typeset summary:after{right:.4rem}[dir=rtl] .md-typeset summary:after{left:.4rem}.md-typeset summary:after{background-color:currentcolor;content:"";height:1rem;-webkit-mask-image:var(--md-details-icon);mask-image:var(--md-details-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;position:absolute;top:.625em;transform:rotate(0deg);transition:transform .25s;width:1rem}[dir=rtl] .md-typeset summary:after{transform:rotate(180deg)}.md-typeset summary::marker{display:none}.md-typeset summary::-webkit-details-marker{display:none}.md-typeset .emojione,.md-typeset .gemoji,.md-typeset .twemoji{--md-icon-size:1.125em;display:inline-flex;height:var(--md-icon-size);vertical-align:text-top}.md-typeset .emojione svg,.md-typeset .gemoji svg,.md-typeset .twemoji svg{fill:currentcolor;max-height:100%;width:var(--md-icon-size)}.md-typeset .lg,.md-typeset .xl,.md-typeset .xxl,.md-typeset .xxxl{vertical-align:text-bottom}.md-typeset .middle{vertical-align:middle}.md-typeset .lg{--md-icon-size:1.5em}.md-typeset .xl{--md-icon-size:2.25em}.md-typeset .xxl{--md-icon-size:3em}.md-typeset .xxxl{--md-icon-size:4em}.highlight .o,.highlight .ow{color:var(--md-code-hl-operator-color)}.highlight .p{color:var(--md-code-hl-punctuation-color)}.highlight .cpf,.highlight .l,.highlight .s,.highlight .s1,.highlight .s2,.highlight .sb,.highlight .sc,.highlight .si,.highlight .ss{color:var(--md-code-hl-string-color)}.highlight .cp,.highlight .se,.highlight .sh,.highlight .sr,.highlight .sx{color:var(--md-code-hl-special-color)}.highlight .il,.highlight .m,.highlight .mb,.highlight .mf,.highlight .mh,.highlight .mi,.highlight .mo{color:var(--md-code-hl-number-color)}.highlight .k,.highlight .kd,.highlight .kn,.highlight .kp,.highlight .kr,.highlight .kt{color:var(--md-code-hl-keyword-color)}.highlight .kc,.highlight .n{color:var(--md-code-hl-name-color)}.highlight .bp,.highlight .nb,.highlight .no{color:var(--md-code-hl-constant-color)}.highlight .nc,.highlight .ne,.highlight .nf,.highlight .nn{color:var(--md-code-hl-function-color)}.highlight .nd,.highlight .ni,.highlight .nl,.highlight .nt{color:var(--md-code-hl-keyword-color)}.highlight .c,.highlight .c1,.highlight .ch,.highlight .cm,.highlight .cs,.highlight .sd{color:var(--md-code-hl-comment-color)}.highlight .na,.highlight .nv,.highlight .vc,.highlight .vg,.highlight .vi{color:var(--md-code-hl-variable-color)}.highlight .ge,.highlight .gh,.highlight .go,.highlight .gp,.highlight .gr,.highlight .gs,.highlight .gt,.highlight .gu{color:var(--md-code-hl-generic-color)}.highlight .gd,.highlight .gi{border-radius:.1rem;margin:0 -.125em;padding:0 .125em}.highlight .gd{background-color:var(--md-typeset-del-color)}.highlight .gi{background-color:var(--md-typeset-ins-color)}.highlight .hll{background-color:var(--md-code-hl-color--light);box-shadow:2px 0 0 0 var(--md-code-hl-color) inset;display:block;margin:0 -1.1764705882em;padding:0 1.1764705882em}.highlight span.filename{background-color:var(--md-code-bg-color);border-bottom:.05rem solid var(--md-default-fg-color--lightest);border-top-left-radius:.1rem;border-top-right-radius:.1rem;display:flow-root;font-size:.85em;font-weight:700;margin-top:1em;padding:.6617647059em 1.1764705882em;position:relative}.highlight span.filename+pre{margin-top:0}.highlight span.filename+pre>code{border-top-left-radius:0;border-top-right-radius:0}.highlight [data-linenos]:before{background-color:var(--md-code-bg-color);box-shadow:-.05rem 0 var(--md-default-fg-color--lightest) inset;color:var(--md-default-fg-color--light);content:attr(data-linenos);float:left;left:-1.1764705882em;margin-left:-1.1764705882em;margin-right:1.1764705882em;padding-left:1.1764705882em;position:sticky;-webkit-user-select:none;user-select:none;z-index:3}.highlight code a[id]{position:absolute;visibility:hidden}.highlight code[data-md-copying]{display:initial}.highlight code[data-md-copying] .hll{display:contents}.highlight code[data-md-copying] .md-annotation{display:none}.highlighttable{display:flow-root}.highlighttable tbody,.highlighttable td{display:block;padding:0}.highlighttable tr{display:flex}.highlighttable pre{margin:0}.highlighttable th.filename{flex-grow:1;padding:0;text-align:left}.highlighttable th.filename span.filename{margin-top:0}.highlighttable .linenos{background-color:var(--md-code-bg-color);border-bottom-left-radius:.1rem;border-top-left-radius:.1rem;font-size:.85em;padding:.7720588235em 0 .7720588235em 1.1764705882em;-webkit-user-select:none;user-select:none}.highlighttable .linenodiv{box-shadow:-.05rem 0 var(--md-default-fg-color--lightest) inset}.highlighttable .linenodiv pre{color:var(--md-default-fg-color--light);text-align:right}.highlighttable .linenodiv span[class]{padding-right:.5882352941em}.highlighttable .code{flex:1;min-width:0}.linenodiv a{color:inherit}.md-typeset .highlighttable{direction:ltr;margin:1em 0}.md-typeset .highlighttable>tbody>tr>.code>div>pre>code{border-bottom-left-radius:0;border-top-left-radius:0}.md-typeset .highlight+.result{border:.05rem solid var(--md-code-bg-color);border-bottom-left-radius:.1rem;border-bottom-right-radius:.1rem;border-top-width:.1rem;margin-top:-1.125em;overflow:visible;padding:0 1em}.md-typeset .highlight+.result:after{clear:both;content:"";display:block}@media screen and (max-width:44.984375em){.md-content__inner>.highlight{margin:1em -.8rem}.md-content__inner>.highlight>.filename,.md-content__inner>.highlight>.highlighttable>tbody>tr>.code>div>pre>code,.md-content__inner>.highlight>.highlighttable>tbody>tr>.filename span.filename,.md-content__inner>.highlight>.highlighttable>tbody>tr>.linenos,.md-content__inner>.highlight>pre>code{border-radius:0}.md-content__inner>.highlight+.result{border-left-width:0;border-radius:0;border-right-width:0;margin-left:-.8rem;margin-right:-.8rem}}.md-typeset .keys kbd:after,.md-typeset .keys kbd:before{-moz-osx-font-smoothing:initial;-webkit-font-smoothing:initial;color:inherit;margin:0;position:relative}.md-typeset .keys span{color:var(--md-default-fg-color--light);padding:0 .2em}.md-typeset .keys .key-alt:before,.md-typeset .keys .key-left-alt:before,.md-typeset .keys .key-right-alt:before{content:"⎇";padding-right:.4em}.md-typeset .keys .key-command:before,.md-typeset .keys .key-left-command:before,.md-typeset .keys .key-right-command:before{content:"⌘";padding-right:.4em}.md-typeset .keys .key-control:before,.md-typeset .keys .key-left-control:before,.md-typeset .keys .key-right-control:before{content:"⌃";padding-right:.4em}.md-typeset .keys .key-left-meta:before,.md-typeset .keys .key-meta:before,.md-typeset .keys .key-right-meta:before{content:"◆";padding-right:.4em}.md-typeset .keys .key-left-option:before,.md-typeset .keys .key-option:before,.md-typeset .keys .key-right-option:before{content:"⌥";padding-right:.4em}.md-typeset .keys .key-left-shift:before,.md-typeset .keys .key-right-shift:before,.md-typeset .keys .key-shift:before{content:"⇧";padding-right:.4em}.md-typeset .keys .key-left-super:before,.md-typeset .keys .key-right-super:before,.md-typeset .keys .key-super:before{content:"❖";padding-right:.4em}.md-typeset .keys .key-left-windows:before,.md-typeset .keys .key-right-windows:before,.md-typeset .keys .key-windows:before{content:"⊞";padding-right:.4em}.md-typeset .keys .key-arrow-down:before{content:"↓";padding-right:.4em}.md-typeset .keys .key-arrow-left:before{content:"←";padding-right:.4em}.md-typeset .keys .key-arrow-right:before{content:"→";padding-right:.4em}.md-typeset .keys .key-arrow-up:before{content:"↑";padding-right:.4em}.md-typeset .keys .key-backspace:before{content:"⌫";padding-right:.4em}.md-typeset .keys .key-backtab:before{content:"⇤";padding-right:.4em}.md-typeset .keys .key-caps-lock:before{content:"⇪";padding-right:.4em}.md-typeset .keys .key-clear:before{content:"⌧";padding-right:.4em}.md-typeset .keys .key-context-menu:before{content:"☰";padding-right:.4em}.md-typeset .keys .key-delete:before{content:"⌦";padding-right:.4em}.md-typeset .keys .key-eject:before{content:"⏏";padding-right:.4em}.md-typeset .keys .key-end:before{content:"⤓";padding-right:.4em}.md-typeset .keys .key-escape:before{content:"⎋";padding-right:.4em}.md-typeset .keys .key-home:before{content:"⤒";padding-right:.4em}.md-typeset .keys .key-insert:before{content:"⎀";padding-right:.4em}.md-typeset .keys .key-page-down:before{content:"⇟";padding-right:.4em}.md-typeset .keys .key-page-up:before{content:"⇞";padding-right:.4em}.md-typeset .keys .key-print-screen:before{content:"⎙";padding-right:.4em}.md-typeset .keys .key-tab:after{content:"⇥";padding-left:.4em}.md-typeset .keys .key-num-enter:after{content:"⌤";padding-left:.4em}.md-typeset .keys .key-enter:after{content:"⏎";padding-left:.4em}:root{--md-tabbed-icon--prev:url('data:image/svg+xml;charset=utf-8,');--md-tabbed-icon--next:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .tabbed-set{border-radius:.1rem;display:flex;flex-flow:column wrap;margin:1em 0;position:relative}.md-typeset .tabbed-set>input{height:0;opacity:0;position:absolute;width:0}.md-typeset .tabbed-set>input:target{--md-scroll-offset:0.625em}.md-typeset .tabbed-set>input.focus-visible~.tabbed-labels:before{background-color:var(--md-accent-fg-color)}.md-typeset .tabbed-labels{-ms-overflow-style:none;box-shadow:0 -.05rem var(--md-default-fg-color--lightest) inset;display:flex;max-width:100%;overflow:auto;scrollbar-width:none}@media print{.md-typeset .tabbed-labels{display:contents}}@media screen{.js .md-typeset .tabbed-labels{position:relative}.js .md-typeset .tabbed-labels:before{background:var(--md-default-fg-color);bottom:0;content:"";display:block;height:2px;left:0;position:absolute;transform:translateX(var(--md-indicator-x));transition:width 225ms,background-color .25s,transform .25s;transition-timing-function:cubic-bezier(.4,0,.2,1);width:var(--md-indicator-width)}}.md-typeset .tabbed-labels::-webkit-scrollbar{display:none}.md-typeset .tabbed-labels>label{border-bottom:.1rem solid #0000;border-radius:.1rem .1rem 0 0;color:var(--md-default-fg-color--light);cursor:pointer;flex-shrink:0;font-size:.64rem;font-weight:700;padding:.78125em 1.25em .625em;scroll-margin-inline-start:1rem;transition:background-color .25s,color .25s;white-space:nowrap;width:auto}@media print{.md-typeset .tabbed-labels>label:first-child{order:1}.md-typeset .tabbed-labels>label:nth-child(2){order:2}.md-typeset .tabbed-labels>label:nth-child(3){order:3}.md-typeset .tabbed-labels>label:nth-child(4){order:4}.md-typeset .tabbed-labels>label:nth-child(5){order:5}.md-typeset .tabbed-labels>label:nth-child(6){order:6}.md-typeset .tabbed-labels>label:nth-child(7){order:7}.md-typeset .tabbed-labels>label:nth-child(8){order:8}.md-typeset .tabbed-labels>label:nth-child(9){order:9}.md-typeset .tabbed-labels>label:nth-child(10){order:10}.md-typeset .tabbed-labels>label:nth-child(11){order:11}.md-typeset .tabbed-labels>label:nth-child(12){order:12}.md-typeset .tabbed-labels>label:nth-child(13){order:13}.md-typeset .tabbed-labels>label:nth-child(14){order:14}.md-typeset .tabbed-labels>label:nth-child(15){order:15}.md-typeset .tabbed-labels>label:nth-child(16){order:16}.md-typeset .tabbed-labels>label:nth-child(17){order:17}.md-typeset .tabbed-labels>label:nth-child(18){order:18}.md-typeset .tabbed-labels>label:nth-child(19){order:19}.md-typeset .tabbed-labels>label:nth-child(20){order:20}}.md-typeset .tabbed-labels>label:hover{color:var(--md-default-fg-color)}.md-typeset .tabbed-labels>label>[href]:first-child{color:inherit}.md-typeset .tabbed-labels--linked>label{padding:0}.md-typeset .tabbed-labels--linked>label>a{display:block;padding:.78125em 1.25em .625em}.md-typeset .tabbed-content{width:100%}@media print{.md-typeset .tabbed-content{display:contents}}.md-typeset .tabbed-block{display:none}@media print{.md-typeset .tabbed-block{display:block}.md-typeset .tabbed-block:first-child{order:1}.md-typeset .tabbed-block:nth-child(2){order:2}.md-typeset .tabbed-block:nth-child(3){order:3}.md-typeset .tabbed-block:nth-child(4){order:4}.md-typeset .tabbed-block:nth-child(5){order:5}.md-typeset .tabbed-block:nth-child(6){order:6}.md-typeset .tabbed-block:nth-child(7){order:7}.md-typeset .tabbed-block:nth-child(8){order:8}.md-typeset .tabbed-block:nth-child(9){order:9}.md-typeset .tabbed-block:nth-child(10){order:10}.md-typeset .tabbed-block:nth-child(11){order:11}.md-typeset .tabbed-block:nth-child(12){order:12}.md-typeset .tabbed-block:nth-child(13){order:13}.md-typeset .tabbed-block:nth-child(14){order:14}.md-typeset .tabbed-block:nth-child(15){order:15}.md-typeset .tabbed-block:nth-child(16){order:16}.md-typeset .tabbed-block:nth-child(17){order:17}.md-typeset .tabbed-block:nth-child(18){order:18}.md-typeset .tabbed-block:nth-child(19){order:19}.md-typeset .tabbed-block:nth-child(20){order:20}}.md-typeset .tabbed-block>.highlight:first-child>pre,.md-typeset .tabbed-block>pre:first-child{margin:0}.md-typeset .tabbed-block>.highlight:first-child>pre>code,.md-typeset .tabbed-block>pre:first-child>code{border-top-left-radius:0;border-top-right-radius:0}.md-typeset .tabbed-block>.highlight:first-child>.filename{border-top-left-radius:0;border-top-right-radius:0;margin:0}.md-typeset .tabbed-block>.highlight:first-child>.highlighttable{margin:0}.md-typeset .tabbed-block>.highlight:first-child>.highlighttable>tbody>tr>.filename span.filename,.md-typeset .tabbed-block>.highlight:first-child>.highlighttable>tbody>tr>.linenos{border-top-left-radius:0;border-top-right-radius:0;margin:0}.md-typeset .tabbed-block>.highlight:first-child>.highlighttable>tbody>tr>.code>div>pre>code{border-top-left-radius:0;border-top-right-radius:0}.md-typeset .tabbed-block>.highlight:first-child+.result{margin-top:-.125em}.md-typeset .tabbed-block>.tabbed-set{margin:0}.md-typeset .tabbed-button{align-self:center;border-radius:100%;color:var(--md-default-fg-color--light);cursor:pointer;display:block;height:.9rem;margin-top:.1rem;pointer-events:auto;transition:background-color .25s;width:.9rem}.md-typeset .tabbed-button:hover{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}.md-typeset .tabbed-button:after{background-color:currentcolor;content:"";display:block;height:100%;-webkit-mask-image:var(--md-tabbed-icon--prev);mask-image:var(--md-tabbed-icon--prev);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;transition:background-color .25s,transform .25s;width:100%}.md-typeset .tabbed-control{background:linear-gradient(to right,var(--md-default-bg-color) 60%,#0000);display:flex;height:1.9rem;justify-content:start;pointer-events:none;position:absolute;transition:opacity 125ms;width:1.2rem}[dir=rtl] .md-typeset .tabbed-control{transform:rotate(180deg)}.md-typeset .tabbed-control[hidden]{opacity:0}.md-typeset .tabbed-control--next{background:linear-gradient(to left,var(--md-default-bg-color) 60%,#0000);justify-content:end;right:0}.md-typeset .tabbed-control--next .tabbed-button:after{-webkit-mask-image:var(--md-tabbed-icon--next);mask-image:var(--md-tabbed-icon--next)}@media screen and (max-width:44.984375em){[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels{padding-left:.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels{padding-right:.8rem}.md-content__inner>.tabbed-set .tabbed-labels{margin:0 -.8rem;max-width:100vw;scroll-padding-inline-start:.8rem}[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels:after{padding-right:.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels:after{padding-left:.8rem}.md-content__inner>.tabbed-set .tabbed-labels:after{content:""}[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--prev{padding-left:.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--prev{padding-right:.8rem}[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--prev{margin-left:-.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--prev{margin-right:-.8rem}.md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--prev{width:2rem}[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--next{padding-right:.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--next{padding-left:.8rem}[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--next{margin-right:-.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--next{margin-left:-.8rem}.md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--next{width:2rem}}@media screen{.md-typeset .tabbed-set>input:first-child:checked~.tabbed-labels>:first-child,.md-typeset .tabbed-set>input:nth-child(10):checked~.tabbed-labels>:nth-child(10),.md-typeset .tabbed-set>input:nth-child(11):checked~.tabbed-labels>:nth-child(11),.md-typeset .tabbed-set>input:nth-child(12):checked~.tabbed-labels>:nth-child(12),.md-typeset .tabbed-set>input:nth-child(13):checked~.tabbed-labels>:nth-child(13),.md-typeset .tabbed-set>input:nth-child(14):checked~.tabbed-labels>:nth-child(14),.md-typeset .tabbed-set>input:nth-child(15):checked~.tabbed-labels>:nth-child(15),.md-typeset .tabbed-set>input:nth-child(16):checked~.tabbed-labels>:nth-child(16),.md-typeset .tabbed-set>input:nth-child(17):checked~.tabbed-labels>:nth-child(17),.md-typeset .tabbed-set>input:nth-child(18):checked~.tabbed-labels>:nth-child(18),.md-typeset .tabbed-set>input:nth-child(19):checked~.tabbed-labels>:nth-child(19),.md-typeset .tabbed-set>input:nth-child(2):checked~.tabbed-labels>:nth-child(2),.md-typeset .tabbed-set>input:nth-child(20):checked~.tabbed-labels>:nth-child(20),.md-typeset .tabbed-set>input:nth-child(3):checked~.tabbed-labels>:nth-child(3),.md-typeset .tabbed-set>input:nth-child(4):checked~.tabbed-labels>:nth-child(4),.md-typeset .tabbed-set>input:nth-child(5):checked~.tabbed-labels>:nth-child(5),.md-typeset .tabbed-set>input:nth-child(6):checked~.tabbed-labels>:nth-child(6),.md-typeset .tabbed-set>input:nth-child(7):checked~.tabbed-labels>:nth-child(7),.md-typeset .tabbed-set>input:nth-child(8):checked~.tabbed-labels>:nth-child(8),.md-typeset .tabbed-set>input:nth-child(9):checked~.tabbed-labels>:nth-child(9){color:var(--md-default-fg-color)}.md-typeset .no-js .tabbed-set>input:first-child:checked~.tabbed-labels>:first-child,.md-typeset .no-js .tabbed-set>input:nth-child(10):checked~.tabbed-labels>:nth-child(10),.md-typeset .no-js .tabbed-set>input:nth-child(11):checked~.tabbed-labels>:nth-child(11),.md-typeset .no-js .tabbed-set>input:nth-child(12):checked~.tabbed-labels>:nth-child(12),.md-typeset .no-js .tabbed-set>input:nth-child(13):checked~.tabbed-labels>:nth-child(13),.md-typeset .no-js .tabbed-set>input:nth-child(14):checked~.tabbed-labels>:nth-child(14),.md-typeset .no-js .tabbed-set>input:nth-child(15):checked~.tabbed-labels>:nth-child(15),.md-typeset .no-js .tabbed-set>input:nth-child(16):checked~.tabbed-labels>:nth-child(16),.md-typeset .no-js .tabbed-set>input:nth-child(17):checked~.tabbed-labels>:nth-child(17),.md-typeset .no-js .tabbed-set>input:nth-child(18):checked~.tabbed-labels>:nth-child(18),.md-typeset .no-js .tabbed-set>input:nth-child(19):checked~.tabbed-labels>:nth-child(19),.md-typeset .no-js .tabbed-set>input:nth-child(2):checked~.tabbed-labels>:nth-child(2),.md-typeset .no-js .tabbed-set>input:nth-child(20):checked~.tabbed-labels>:nth-child(20),.md-typeset .no-js .tabbed-set>input:nth-child(3):checked~.tabbed-labels>:nth-child(3),.md-typeset .no-js .tabbed-set>input:nth-child(4):checked~.tabbed-labels>:nth-child(4),.md-typeset .no-js .tabbed-set>input:nth-child(5):checked~.tabbed-labels>:nth-child(5),.md-typeset .no-js .tabbed-set>input:nth-child(6):checked~.tabbed-labels>:nth-child(6),.md-typeset .no-js .tabbed-set>input:nth-child(7):checked~.tabbed-labels>:nth-child(7),.md-typeset .no-js .tabbed-set>input:nth-child(8):checked~.tabbed-labels>:nth-child(8),.md-typeset .no-js .tabbed-set>input:nth-child(9):checked~.tabbed-labels>:nth-child(9),.md-typeset [role=dialog] .tabbed-set>input:first-child:checked~.tabbed-labels>:first-child,.md-typeset [role=dialog] .tabbed-set>input:nth-child(10):checked~.tabbed-labels>:nth-child(10),.md-typeset [role=dialog] .tabbed-set>input:nth-child(11):checked~.tabbed-labels>:nth-child(11),.md-typeset [role=dialog] .tabbed-set>input:nth-child(12):checked~.tabbed-labels>:nth-child(12),.md-typeset [role=dialog] .tabbed-set>input:nth-child(13):checked~.tabbed-labels>:nth-child(13),.md-typeset [role=dialog] .tabbed-set>input:nth-child(14):checked~.tabbed-labels>:nth-child(14),.md-typeset [role=dialog] .tabbed-set>input:nth-child(15):checked~.tabbed-labels>:nth-child(15),.md-typeset [role=dialog] .tabbed-set>input:nth-child(16):checked~.tabbed-labels>:nth-child(16),.md-typeset [role=dialog] .tabbed-set>input:nth-child(17):checked~.tabbed-labels>:nth-child(17),.md-typeset [role=dialog] .tabbed-set>input:nth-child(18):checked~.tabbed-labels>:nth-child(18),.md-typeset [role=dialog] .tabbed-set>input:nth-child(19):checked~.tabbed-labels>:nth-child(19),.md-typeset [role=dialog] .tabbed-set>input:nth-child(2):checked~.tabbed-labels>:nth-child(2),.md-typeset [role=dialog] .tabbed-set>input:nth-child(20):checked~.tabbed-labels>:nth-child(20),.md-typeset [role=dialog] .tabbed-set>input:nth-child(3):checked~.tabbed-labels>:nth-child(3),.md-typeset [role=dialog] .tabbed-set>input:nth-child(4):checked~.tabbed-labels>:nth-child(4),.md-typeset [role=dialog] .tabbed-set>input:nth-child(5):checked~.tabbed-labels>:nth-child(5),.md-typeset [role=dialog] .tabbed-set>input:nth-child(6):checked~.tabbed-labels>:nth-child(6),.md-typeset [role=dialog] .tabbed-set>input:nth-child(7):checked~.tabbed-labels>:nth-child(7),.md-typeset [role=dialog] .tabbed-set>input:nth-child(8):checked~.tabbed-labels>:nth-child(8),.md-typeset [role=dialog] .tabbed-set>input:nth-child(9):checked~.tabbed-labels>:nth-child(9),.no-js .md-typeset .tabbed-set>input:first-child:checked~.tabbed-labels>:first-child,.no-js .md-typeset .tabbed-set>input:nth-child(10):checked~.tabbed-labels>:nth-child(10),.no-js .md-typeset .tabbed-set>input:nth-child(11):checked~.tabbed-labels>:nth-child(11),.no-js .md-typeset .tabbed-set>input:nth-child(12):checked~.tabbed-labels>:nth-child(12),.no-js .md-typeset .tabbed-set>input:nth-child(13):checked~.tabbed-labels>:nth-child(13),.no-js .md-typeset .tabbed-set>input:nth-child(14):checked~.tabbed-labels>:nth-child(14),.no-js .md-typeset .tabbed-set>input:nth-child(15):checked~.tabbed-labels>:nth-child(15),.no-js .md-typeset .tabbed-set>input:nth-child(16):checked~.tabbed-labels>:nth-child(16),.no-js .md-typeset .tabbed-set>input:nth-child(17):checked~.tabbed-labels>:nth-child(17),.no-js .md-typeset .tabbed-set>input:nth-child(18):checked~.tabbed-labels>:nth-child(18),.no-js .md-typeset .tabbed-set>input:nth-child(19):checked~.tabbed-labels>:nth-child(19),.no-js .md-typeset .tabbed-set>input:nth-child(2):checked~.tabbed-labels>:nth-child(2),.no-js .md-typeset .tabbed-set>input:nth-child(20):checked~.tabbed-labels>:nth-child(20),.no-js .md-typeset .tabbed-set>input:nth-child(3):checked~.tabbed-labels>:nth-child(3),.no-js .md-typeset .tabbed-set>input:nth-child(4):checked~.tabbed-labels>:nth-child(4),.no-js .md-typeset .tabbed-set>input:nth-child(5):checked~.tabbed-labels>:nth-child(5),.no-js .md-typeset .tabbed-set>input:nth-child(6):checked~.tabbed-labels>:nth-child(6),.no-js .md-typeset .tabbed-set>input:nth-child(7):checked~.tabbed-labels>:nth-child(7),.no-js .md-typeset .tabbed-set>input:nth-child(8):checked~.tabbed-labels>:nth-child(8),.no-js .md-typeset .tabbed-set>input:nth-child(9):checked~.tabbed-labels>:nth-child(9),[role=dialog] .md-typeset .tabbed-set>input:first-child:checked~.tabbed-labels>:first-child,[role=dialog] .md-typeset .tabbed-set>input:nth-child(10):checked~.tabbed-labels>:nth-child(10),[role=dialog] .md-typeset .tabbed-set>input:nth-child(11):checked~.tabbed-labels>:nth-child(11),[role=dialog] .md-typeset .tabbed-set>input:nth-child(12):checked~.tabbed-labels>:nth-child(12),[role=dialog] .md-typeset .tabbed-set>input:nth-child(13):checked~.tabbed-labels>:nth-child(13),[role=dialog] .md-typeset .tabbed-set>input:nth-child(14):checked~.tabbed-labels>:nth-child(14),[role=dialog] .md-typeset .tabbed-set>input:nth-child(15):checked~.tabbed-labels>:nth-child(15),[role=dialog] .md-typeset .tabbed-set>input:nth-child(16):checked~.tabbed-labels>:nth-child(16),[role=dialog] .md-typeset .tabbed-set>input:nth-child(17):checked~.tabbed-labels>:nth-child(17),[role=dialog] .md-typeset .tabbed-set>input:nth-child(18):checked~.tabbed-labels>:nth-child(18),[role=dialog] .md-typeset .tabbed-set>input:nth-child(19):checked~.tabbed-labels>:nth-child(19),[role=dialog] .md-typeset .tabbed-set>input:nth-child(2):checked~.tabbed-labels>:nth-child(2),[role=dialog] .md-typeset .tabbed-set>input:nth-child(20):checked~.tabbed-labels>:nth-child(20),[role=dialog] .md-typeset .tabbed-set>input:nth-child(3):checked~.tabbed-labels>:nth-child(3),[role=dialog] .md-typeset .tabbed-set>input:nth-child(4):checked~.tabbed-labels>:nth-child(4),[role=dialog] .md-typeset .tabbed-set>input:nth-child(5):checked~.tabbed-labels>:nth-child(5),[role=dialog] .md-typeset .tabbed-set>input:nth-child(6):checked~.tabbed-labels>:nth-child(6),[role=dialog] .md-typeset .tabbed-set>input:nth-child(7):checked~.tabbed-labels>:nth-child(7),[role=dialog] .md-typeset .tabbed-set>input:nth-child(8):checked~.tabbed-labels>:nth-child(8),[role=dialog] .md-typeset .tabbed-set>input:nth-child(9):checked~.tabbed-labels>:nth-child(9){border-color:var(--md-default-fg-color)}}.md-typeset .tabbed-set>input:first-child.focus-visible~.tabbed-labels>:first-child,.md-typeset .tabbed-set>input:nth-child(10).focus-visible~.tabbed-labels>:nth-child(10),.md-typeset .tabbed-set>input:nth-child(11).focus-visible~.tabbed-labels>:nth-child(11),.md-typeset .tabbed-set>input:nth-child(12).focus-visible~.tabbed-labels>:nth-child(12),.md-typeset .tabbed-set>input:nth-child(13).focus-visible~.tabbed-labels>:nth-child(13),.md-typeset .tabbed-set>input:nth-child(14).focus-visible~.tabbed-labels>:nth-child(14),.md-typeset .tabbed-set>input:nth-child(15).focus-visible~.tabbed-labels>:nth-child(15),.md-typeset .tabbed-set>input:nth-child(16).focus-visible~.tabbed-labels>:nth-child(16),.md-typeset .tabbed-set>input:nth-child(17).focus-visible~.tabbed-labels>:nth-child(17),.md-typeset .tabbed-set>input:nth-child(18).focus-visible~.tabbed-labels>:nth-child(18),.md-typeset .tabbed-set>input:nth-child(19).focus-visible~.tabbed-labels>:nth-child(19),.md-typeset .tabbed-set>input:nth-child(2).focus-visible~.tabbed-labels>:nth-child(2),.md-typeset .tabbed-set>input:nth-child(20).focus-visible~.tabbed-labels>:nth-child(20),.md-typeset .tabbed-set>input:nth-child(3).focus-visible~.tabbed-labels>:nth-child(3),.md-typeset .tabbed-set>input:nth-child(4).focus-visible~.tabbed-labels>:nth-child(4),.md-typeset .tabbed-set>input:nth-child(5).focus-visible~.tabbed-labels>:nth-child(5),.md-typeset .tabbed-set>input:nth-child(6).focus-visible~.tabbed-labels>:nth-child(6),.md-typeset .tabbed-set>input:nth-child(7).focus-visible~.tabbed-labels>:nth-child(7),.md-typeset .tabbed-set>input:nth-child(8).focus-visible~.tabbed-labels>:nth-child(8),.md-typeset .tabbed-set>input:nth-child(9).focus-visible~.tabbed-labels>:nth-child(9){color:var(--md-accent-fg-color)}.md-typeset .tabbed-set>input:first-child:checked~.tabbed-content>:first-child,.md-typeset .tabbed-set>input:nth-child(10):checked~.tabbed-content>:nth-child(10),.md-typeset .tabbed-set>input:nth-child(11):checked~.tabbed-content>:nth-child(11),.md-typeset .tabbed-set>input:nth-child(12):checked~.tabbed-content>:nth-child(12),.md-typeset .tabbed-set>input:nth-child(13):checked~.tabbed-content>:nth-child(13),.md-typeset .tabbed-set>input:nth-child(14):checked~.tabbed-content>:nth-child(14),.md-typeset .tabbed-set>input:nth-child(15):checked~.tabbed-content>:nth-child(15),.md-typeset .tabbed-set>input:nth-child(16):checked~.tabbed-content>:nth-child(16),.md-typeset .tabbed-set>input:nth-child(17):checked~.tabbed-content>:nth-child(17),.md-typeset .tabbed-set>input:nth-child(18):checked~.tabbed-content>:nth-child(18),.md-typeset .tabbed-set>input:nth-child(19):checked~.tabbed-content>:nth-child(19),.md-typeset .tabbed-set>input:nth-child(2):checked~.tabbed-content>:nth-child(2),.md-typeset .tabbed-set>input:nth-child(20):checked~.tabbed-content>:nth-child(20),.md-typeset .tabbed-set>input:nth-child(3):checked~.tabbed-content>:nth-child(3),.md-typeset .tabbed-set>input:nth-child(4):checked~.tabbed-content>:nth-child(4),.md-typeset .tabbed-set>input:nth-child(5):checked~.tabbed-content>:nth-child(5),.md-typeset .tabbed-set>input:nth-child(6):checked~.tabbed-content>:nth-child(6),.md-typeset .tabbed-set>input:nth-child(7):checked~.tabbed-content>:nth-child(7),.md-typeset .tabbed-set>input:nth-child(8):checked~.tabbed-content>:nth-child(8),.md-typeset .tabbed-set>input:nth-child(9):checked~.tabbed-content>:nth-child(9){display:block}:root{--md-tasklist-icon:url('data:image/svg+xml;charset=utf-8,');--md-tasklist-icon--checked:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .task-list-item{list-style-type:none;position:relative}[dir=ltr] .md-typeset .task-list-item [type=checkbox]{left:-2em}[dir=rtl] .md-typeset .task-list-item [type=checkbox]{right:-2em}.md-typeset .task-list-item [type=checkbox]{position:absolute;top:.45em}.md-typeset .task-list-control [type=checkbox]{opacity:0;z-index:-1}[dir=ltr] .md-typeset .task-list-indicator:before{left:-1.5em}[dir=rtl] .md-typeset .task-list-indicator:before{right:-1.5em}.md-typeset .task-list-indicator:before{background-color:var(--md-default-fg-color--lightest);content:"";height:1.25em;-webkit-mask-image:var(--md-tasklist-icon);mask-image:var(--md-tasklist-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;position:absolute;top:.15em;width:1.25em}.md-typeset [type=checkbox]:checked+.task-list-indicator:before{background-color:#00e676;-webkit-mask-image:var(--md-tasklist-icon--checked);mask-image:var(--md-tasklist-icon--checked)}@media print{.giscus,[id=__comments]{display:none}}:root>*{--md-mermaid-font-family:var(--md-text-font-family),sans-serif;--md-mermaid-edge-color:var(--md-code-fg-color);--md-mermaid-node-bg-color:var(--md-accent-fg-color--transparent);--md-mermaid-node-fg-color:var(--md-accent-fg-color);--md-mermaid-label-bg-color:var(--md-default-bg-color);--md-mermaid-label-fg-color:var(--md-code-fg-color);--md-mermaid-sequence-actor-bg-color:var(--md-mermaid-label-bg-color);--md-mermaid-sequence-actor-fg-color:var(--md-mermaid-label-fg-color);--md-mermaid-sequence-actor-border-color:var(--md-mermaid-node-fg-color);--md-mermaid-sequence-actor-line-color:var(--md-default-fg-color--lighter);--md-mermaid-sequence-actorman-bg-color:var(--md-mermaid-label-bg-color);--md-mermaid-sequence-actorman-line-color:var(--md-mermaid-node-fg-color);--md-mermaid-sequence-box-bg-color:var(--md-mermaid-node-bg-color);--md-mermaid-sequence-box-fg-color:var(--md-mermaid-edge-color);--md-mermaid-sequence-label-bg-color:var(--md-mermaid-node-bg-color);--md-mermaid-sequence-label-fg-color:var(--md-mermaid-node-fg-color);--md-mermaid-sequence-loop-bg-color:var(--md-mermaid-node-bg-color);--md-mermaid-sequence-loop-fg-color:var(--md-mermaid-edge-color);--md-mermaid-sequence-loop-border-color:var(--md-mermaid-node-fg-color);--md-mermaid-sequence-message-fg-color:var(--md-mermaid-edge-color);--md-mermaid-sequence-message-line-color:var(--md-mermaid-edge-color);--md-mermaid-sequence-note-bg-color:var(--md-mermaid-label-bg-color);--md-mermaid-sequence-note-fg-color:var(--md-mermaid-edge-color);--md-mermaid-sequence-note-border-color:var(--md-mermaid-label-fg-color);--md-mermaid-sequence-number-bg-color:var(--md-mermaid-node-fg-color);--md-mermaid-sequence-number-fg-color:var(--md-accent-bg-color)}.mermaid{line-height:normal;margin:1em 0}.md-typeset .grid{grid-gap:.4rem;display:grid;grid-template-columns:repeat(auto-fit,minmax(min(100%,16rem),1fr));margin:1em 0}.md-typeset .grid.cards>ol,.md-typeset .grid.cards>ul{display:contents}.md-typeset .grid.cards>ol>li,.md-typeset .grid.cards>ul>li,.md-typeset .grid>.card{border:.05rem solid var(--md-default-fg-color--lightest);border-radius:.1rem;display:block;margin:0;padding:.8rem;transition:border .25s,box-shadow .25s}.md-typeset .grid.cards>ol>li:focus-within,.md-typeset .grid.cards>ol>li:hover,.md-typeset .grid.cards>ul>li:focus-within,.md-typeset .grid.cards>ul>li:hover,.md-typeset .grid>.card:focus-within,.md-typeset .grid>.card:hover{border-color:#0000;box-shadow:var(--md-shadow-z2)}.md-typeset .grid.cards>ol>li>hr,.md-typeset .grid.cards>ul>li>hr,.md-typeset .grid>.card>hr{margin-bottom:1em;margin-top:1em}.md-typeset .grid.cards>ol>li>:first-child,.md-typeset .grid.cards>ul>li>:first-child,.md-typeset .grid>.card>:first-child{margin-top:0}.md-typeset .grid.cards>ol>li>:last-child,.md-typeset .grid.cards>ul>li>:last-child,.md-typeset .grid>.card>:last-child{margin-bottom:0}.md-typeset .grid>*,.md-typeset .grid>.admonition,.md-typeset .grid>.highlight>*,.md-typeset .grid>.highlighttable,.md-typeset .grid>.md-typeset details,.md-typeset .grid>details,.md-typeset .grid>pre{margin-bottom:0;margin-top:0}.md-typeset .grid>.highlight>pre:only-child,.md-typeset .grid>.highlight>pre>code,.md-typeset .grid>.highlighttable,.md-typeset .grid>.highlighttable>tbody,.md-typeset .grid>.highlighttable>tbody>tr,.md-typeset .grid>.highlighttable>tbody>tr>.code,.md-typeset .grid>.highlighttable>tbody>tr>.code>.highlight,.md-typeset .grid>.highlighttable>tbody>tr>.code>.highlight>pre,.md-typeset .grid>.highlighttable>tbody>tr>.code>.highlight>pre>code{height:100%}.md-typeset .grid>.tabbed-set{margin-bottom:0;margin-top:0}@media screen and (min-width:45em){[dir=ltr] .md-typeset .inline{float:left}[dir=rtl] .md-typeset .inline{float:right}[dir=ltr] .md-typeset .inline{margin-right:.8rem}[dir=rtl] .md-typeset .inline{margin-left:.8rem}.md-typeset .inline{margin-bottom:.8rem;margin-top:0;width:11.7rem}[dir=ltr] .md-typeset .inline.end{float:right}[dir=rtl] .md-typeset .inline.end{float:left}[dir=ltr] .md-typeset .inline.end{margin-left:.8rem;margin-right:0}[dir=rtl] .md-typeset .inline.end{margin-left:0;margin-right:.8rem}} -/*! update cache: 20260104155757 */ +/*! update cache: 20260104161456 */ diff --git a/ja/assets/stylesheets/palette.ab4e12ef.min.css b/ja/assets/stylesheets/palette.ab4e12ef.min.css index 9b312ef03..6ca677bee 100644 --- a/ja/assets/stylesheets/palette.ab4e12ef.min.css +++ b/ja/assets/stylesheets/palette.ab4e12ef.min.css @@ -1,2 +1,2 @@ @media screen{[data-md-color-scheme=slate]{--md-default-fg-color:hsla(var(--md-hue),15%,90%,0.82);--md-default-fg-color--light:hsla(var(--md-hue),15%,90%,0.56);--md-default-fg-color--lighter:hsla(var(--md-hue),15%,90%,0.32);--md-default-fg-color--lightest:hsla(var(--md-hue),15%,90%,0.12);--md-default-bg-color:hsla(var(--md-hue),15%,14%,1);--md-default-bg-color--light:hsla(var(--md-hue),15%,14%,0.54);--md-default-bg-color--lighter:hsla(var(--md-hue),15%,14%,0.26);--md-default-bg-color--lightest:hsla(var(--md-hue),15%,14%,0.07);--md-code-fg-color:hsla(var(--md-hue),18%,86%,0.82);--md-code-bg-color:hsla(var(--md-hue),15%,18%,1);--md-code-bg-color--light:hsla(var(--md-hue),15%,18%,0.9);--md-code-bg-color--lighter:hsla(var(--md-hue),15%,18%,0.54);--md-code-hl-color:#2977ff;--md-code-hl-color--light:#2977ff1a;--md-code-hl-number-color:#e6695b;--md-code-hl-special-color:#f06090;--md-code-hl-function-color:#c973d9;--md-code-hl-constant-color:#9383e2;--md-code-hl-keyword-color:#6791e0;--md-code-hl-string-color:#2fb170;--md-code-hl-name-color:var(--md-code-fg-color);--md-code-hl-operator-color:var(--md-default-fg-color--light);--md-code-hl-punctuation-color:var(--md-default-fg-color--light);--md-code-hl-comment-color:var(--md-default-fg-color--light);--md-code-hl-generic-color:var(--md-default-fg-color--light);--md-code-hl-variable-color:var(--md-default-fg-color--light);--md-typeset-color:var(--md-default-fg-color);--md-typeset-a-color:var(--md-primary-fg-color);--md-typeset-kbd-color:hsla(var(--md-hue),15%,90%,0.12);--md-typeset-kbd-accent-color:hsla(var(--md-hue),15%,90%,0.2);--md-typeset-kbd-border-color:hsla(var(--md-hue),15%,14%,1);--md-typeset-mark-color:#4287ff4d;--md-typeset-table-color:hsla(var(--md-hue),15%,95%,0.12);--md-typeset-table-color--light:hsla(var(--md-hue),15%,95%,0.035);--md-admonition-fg-color:var(--md-default-fg-color);--md-admonition-bg-color:var(--md-default-bg-color);--md-footer-bg-color:hsla(var(--md-hue),15%,10%,0.87);--md-footer-bg-color--dark:hsla(var(--md-hue),15%,8%,1);--md-shadow-z1:0 0.2rem 0.5rem #0000000d,0 0 0.05rem #0000001a;--md-shadow-z2:0 0.2rem 0.5rem #00000040,0 0 0.05rem #00000040;--md-shadow-z3:0 0.2rem 0.5rem #0006,0 0 0.05rem #00000059;color-scheme:dark}[data-md-color-scheme=slate] img[src$="#gh-light-mode-only"],[data-md-color-scheme=slate] img[src$="#only-light"]{display:none}[data-md-color-scheme=slate][data-md-color-primary=pink]{--md-typeset-a-color:#ed5487}[data-md-color-scheme=slate][data-md-color-primary=purple]{--md-typeset-a-color:#c46fd3}[data-md-color-scheme=slate][data-md-color-primary=deep-purple]{--md-typeset-a-color:#a47bea}[data-md-color-scheme=slate][data-md-color-primary=indigo]{--md-typeset-a-color:#5488e8}[data-md-color-scheme=slate][data-md-color-primary=teal]{--md-typeset-a-color:#00ccb8}[data-md-color-scheme=slate][data-md-color-primary=green]{--md-typeset-a-color:#71c174}[data-md-color-scheme=slate][data-md-color-primary=deep-orange]{--md-typeset-a-color:#ff764d}[data-md-color-scheme=slate][data-md-color-primary=brown]{--md-typeset-a-color:#c1775c}[data-md-color-scheme=slate][data-md-color-primary=black],[data-md-color-scheme=slate][data-md-color-primary=blue-grey],[data-md-color-scheme=slate][data-md-color-primary=grey],[data-md-color-scheme=slate][data-md-color-primary=white]{--md-typeset-a-color:#5e8bde}[data-md-color-switching] *,[data-md-color-switching] :after,[data-md-color-switching] :before{transition-duration:0ms!important}}[data-md-color-accent=red]{--md-accent-fg-color:#ff1947;--md-accent-fg-color--transparent:#ff19471a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=pink]{--md-accent-fg-color:#f50056;--md-accent-fg-color--transparent:#f500561a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=purple]{--md-accent-fg-color:#df41fb;--md-accent-fg-color--transparent:#df41fb1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=deep-purple]{--md-accent-fg-color:#7c4dff;--md-accent-fg-color--transparent:#7c4dff1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=indigo]{--md-accent-fg-color:#526cfe;--md-accent-fg-color--transparent:#526cfe1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=blue]{--md-accent-fg-color:#4287ff;--md-accent-fg-color--transparent:#4287ff1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=light-blue]{--md-accent-fg-color:#0091eb;--md-accent-fg-color--transparent:#0091eb1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=cyan]{--md-accent-fg-color:#00bad6;--md-accent-fg-color--transparent:#00bad61a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=teal]{--md-accent-fg-color:#00bda4;--md-accent-fg-color--transparent:#00bda41a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=green]{--md-accent-fg-color:#00c753;--md-accent-fg-color--transparent:#00c7531a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=light-green]{--md-accent-fg-color:#63de17;--md-accent-fg-color--transparent:#63de171a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=lime]{--md-accent-fg-color:#b0eb00;--md-accent-fg-color--transparent:#b0eb001a;--md-accent-bg-color:#000000de;--md-accent-bg-color--light:#0000008a}[data-md-color-accent=yellow]{--md-accent-fg-color:#ffd500;--md-accent-fg-color--transparent:#ffd5001a;--md-accent-bg-color:#000000de;--md-accent-bg-color--light:#0000008a}[data-md-color-accent=amber]{--md-accent-fg-color:#fa0;--md-accent-fg-color--transparent:#ffaa001a;--md-accent-bg-color:#000000de;--md-accent-bg-color--light:#0000008a}[data-md-color-accent=orange]{--md-accent-fg-color:#ff9100;--md-accent-fg-color--transparent:#ff91001a;--md-accent-bg-color:#000000de;--md-accent-bg-color--light:#0000008a}[data-md-color-accent=deep-orange]{--md-accent-fg-color:#ff6e42;--md-accent-fg-color--transparent:#ff6e421a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-primary=red]{--md-primary-fg-color:#ef5552;--md-primary-fg-color--light:#e57171;--md-primary-fg-color--dark:#e53734;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=pink]{--md-primary-fg-color:#e92063;--md-primary-fg-color--light:#ec417a;--md-primary-fg-color--dark:#c3185d;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=purple]{--md-primary-fg-color:#ab47bd;--md-primary-fg-color--light:#bb69c9;--md-primary-fg-color--dark:#8c24a8;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=deep-purple]{--md-primary-fg-color:#7e56c2;--md-primary-fg-color--light:#9574cd;--md-primary-fg-color--dark:#673ab6;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=indigo]{--md-primary-fg-color:#4051b5;--md-primary-fg-color--light:#5d6cc0;--md-primary-fg-color--dark:#303fa1;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=blue]{--md-primary-fg-color:#2094f3;--md-primary-fg-color--light:#42a5f5;--md-primary-fg-color--dark:#1975d2;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=light-blue]{--md-primary-fg-color:#02a6f2;--md-primary-fg-color--light:#28b5f6;--md-primary-fg-color--dark:#0287cf;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=cyan]{--md-primary-fg-color:#00bdd6;--md-primary-fg-color--light:#25c5da;--md-primary-fg-color--dark:#0097a8;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=teal]{--md-primary-fg-color:#009485;--md-primary-fg-color--light:#26a699;--md-primary-fg-color--dark:#007a6c;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=green]{--md-primary-fg-color:#4cae4f;--md-primary-fg-color--light:#68bb6c;--md-primary-fg-color--dark:#398e3d;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=light-green]{--md-primary-fg-color:#8bc34b;--md-primary-fg-color--light:#9ccc66;--md-primary-fg-color--dark:#689f38;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=lime]{--md-primary-fg-color:#cbdc38;--md-primary-fg-color--light:#d3e156;--md-primary-fg-color--dark:#b0b52c;--md-primary-bg-color:#000000de;--md-primary-bg-color--light:#0000008a}[data-md-color-primary=yellow]{--md-primary-fg-color:#ffec3d;--md-primary-fg-color--light:#ffee57;--md-primary-fg-color--dark:#fbc02d;--md-primary-bg-color:#000000de;--md-primary-bg-color--light:#0000008a}[data-md-color-primary=amber]{--md-primary-fg-color:#ffc105;--md-primary-fg-color--light:#ffc929;--md-primary-fg-color--dark:#ffa200;--md-primary-bg-color:#000000de;--md-primary-bg-color--light:#0000008a}[data-md-color-primary=orange]{--md-primary-fg-color:#ffa724;--md-primary-fg-color--light:#ffa724;--md-primary-fg-color--dark:#fa8900;--md-primary-bg-color:#000000de;--md-primary-bg-color--light:#0000008a}[data-md-color-primary=deep-orange]{--md-primary-fg-color:#ff6e42;--md-primary-fg-color--light:#ff8a66;--md-primary-fg-color--dark:#f4511f;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=brown]{--md-primary-fg-color:#795649;--md-primary-fg-color--light:#8d6e62;--md-primary-fg-color--dark:#5d4037;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=grey]{--md-primary-fg-color:#757575;--md-primary-fg-color--light:#9e9e9e;--md-primary-fg-color--dark:#616161;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3;--md-typeset-a-color:#4051b5}[data-md-color-primary=blue-grey]{--md-primary-fg-color:#546d78;--md-primary-fg-color--light:#607c8a;--md-primary-fg-color--dark:#455a63;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3;--md-typeset-a-color:#4051b5}[data-md-color-primary=light-green]:not([data-md-color-scheme=slate]){--md-typeset-a-color:#72ad2e}[data-md-color-primary=lime]:not([data-md-color-scheme=slate]){--md-typeset-a-color:#8b990a}[data-md-color-primary=yellow]:not([data-md-color-scheme=slate]){--md-typeset-a-color:#b8a500}[data-md-color-primary=amber]:not([data-md-color-scheme=slate]){--md-typeset-a-color:#d19d00}[data-md-color-primary=orange]:not([data-md-color-scheme=slate]){--md-typeset-a-color:#e68a00}[data-md-color-primary=white]{--md-primary-fg-color:hsla(var(--md-hue),0%,100%,1);--md-primary-fg-color--light:hsla(var(--md-hue),0%,100%,0.7);--md-primary-fg-color--dark:hsla(var(--md-hue),0%,0%,0.07);--md-primary-bg-color:hsla(var(--md-hue),0%,0%,0.87);--md-primary-bg-color--light:hsla(var(--md-hue),0%,0%,0.54);--md-typeset-a-color:#4051b5}[data-md-color-primary=white] .md-button{color:var(--md-typeset-a-color)}[data-md-color-primary=white] .md-button--primary{background-color:var(--md-typeset-a-color);border-color:var(--md-typeset-a-color);color:hsla(var(--md-hue),0%,100%,1)}@media screen and (min-width:60em){[data-md-color-primary=white] .md-search__form{background-color:hsla(var(--md-hue),0%,0%,.07)}[data-md-color-primary=white] .md-search__form:hover{background-color:hsla(var(--md-hue),0%,0%,.32)}[data-md-color-primary=white] .md-search__input+.md-search__icon{color:hsla(var(--md-hue),0%,0%,.87)}}@media screen and (min-width:76.25em){[data-md-color-primary=white] .md-tabs{border-bottom:.05rem solid #00000012}}[data-md-color-primary=black]{--md-primary-fg-color:hsla(var(--md-hue),15%,9%,1);--md-primary-fg-color--light:hsla(var(--md-hue),15%,9%,0.54);--md-primary-fg-color--dark:hsla(var(--md-hue),15%,9%,1);--md-primary-bg-color:hsla(var(--md-hue),15%,100%,1);--md-primary-bg-color--light:hsla(var(--md-hue),15%,100%,0.7);--md-typeset-a-color:#4051b5}[data-md-color-primary=black] .md-button{color:var(--md-typeset-a-color)}[data-md-color-primary=black] .md-button--primary{background-color:var(--md-typeset-a-color);border-color:var(--md-typeset-a-color);color:hsla(var(--md-hue),0%,100%,1)}[data-md-color-primary=black] .md-header{background-color:hsla(var(--md-hue),15%,9%,1)}@media screen and (max-width:59.984375em){[data-md-color-primary=black] .md-nav__source{background-color:hsla(var(--md-hue),15%,11%,.87)}}@media screen and (max-width:76.234375em){html [data-md-color-primary=black] .md-nav--primary .md-nav__title[for=__drawer]{background-color:hsla(var(--md-hue),15%,9%,1)}}@media screen and (min-width:76.25em){[data-md-color-primary=black] .md-tabs{background-color:hsla(var(--md-hue),15%,9%,1)}} -/*! update cache: 20260104155757 */ +/*! update cache: 20260104161456 */ diff --git a/ja/chapter_preface/about_the_book/index.html b/ja/chapter_preface/about_the_book/index.html index c93e9cddf..0743fbf13 100644 --- a/ja/chapter_preface/about_the_book/index.html +++ b/ja/chapter_preface/about_the_book/index.html @@ -4430,9 +4430,9 @@

      図 0-1   本書の主な内容

      0.1.3   謝辞

      -

      この本は、オープンソースコミュニティの多くの貢献者の共同努力により継続的に改善されています。時間とエネルギーを投資してくださった各執筆者に感謝いたします。GitHubで生成された順序で記載されています: krahets, coderonion, Gonglja, nuomi1, Reanon, justin-tse, hpstory, danielsss, curtishd, night-cruise, S-N-O-R-L-A-X, rongyi, msk397, gvenusleo, khoaxuantu, rivertwilight, K3v123, gyt95, zhuoqinyue, yuelinxin, Zuoxun, mingXta, Phoenix0415, FangYuan33, GN-Yu, longsizhuo, IsChristina, xBLACKICEx, guowei-gong, Cathay-Chen, pengchzn, QiLOL, magentaqin, hello-ikun, JoseHung, qualifier1024, thomasq0, sunshinesDL, L-Super, Guanngxu, Transmigration-zhou, WSL0809, Slone123c, lhxsm, yuan0221, what-is-me, Shyam-Chen, theNefelibatas, longranger2, codeberg-user, xiongsp, JeffersonHuang, prinpal, seven1240, Wonderdch, malone6, xiaomiusa87, gaofer, bluebean-cloud, a16su, SamJin98, hongyun-robot, nanlei, XiaChuerwu, yd-j, iron-irax, mgisr, steventimes, junminhong, heshuyue, danny900714, MolDuM, Nigh, Dr-XYZ, XC-Zero, reeswell, PXG-XPG, NI-SW, Horbin-Magician, Enlightenus, YangXuanyi, beatrix-chan, DullSword, xjr7670, jiaxianhua, qq909244296, iStig, boloboloda, hts0000, gledfish, wenjianmin, keshida, kilikilikid, lclc6, lwbaptx, linyejoe2, liuxjerry, llql1211, fbigm, echo1937, szu17dmy, dshlstarr, Yucao-cy, coderlef, czruby, bongbongbakudan, beintentional, ZongYangL, ZhongYuuu, ZhongGuanbin, hezhizhen, linzeyan, ZJKung, luluxia, xb534, ztkuaikuai, yw-1021, ElaBosak233, baagod, zhouLion, yishangzhang, yi427, yanedie, yabo083, weibk, wangwang105, th1nk3r-ing, tao363, 4yDX3906, syd168, sslmj2020, smilelsb, siqyka, selear, sdshaoda, Xi-Row, popozhu, nuquist19, noobcodemaker, XiaoK29, chadyi, lyl625760, lucaswangdev, 0130w, shanghai-Jerry, EJackYang, Javesun99, eltociear, lipusheng, KNChiu, BlindTerran, ShiMaRing, lovelock, FreddieLi, FloranceYeh, fanchenggang, gltianwen, goerll, nedchu, curly210102, CuB3y0nd, KraHsu, CarrotDLaw, youshaoXG, bubble9um, Asashishi, Asa0oo0o0o, fanenr, eagleanurag, akshiterate, 52coder, foursevenlove, KorsChen, GaochaoZhu, hopkings2008, yang-le, realwujing, Evilrabbit520, Umer-Jahangir, Turing-1024-Lee, Suremotoo, paoxiaomooo, Chieko-Seren, Allen-Scai, ymmmas, Risuntsy, Richard-Zhang1019, RafaelCaso, qingpeng9802, primexiao, Urbaner3, zhongfq, nidhoggfgg, MwumLi, CreatorMetaSky, martinx, ZnYang2018, hugtyftg, logan-qiu, psychelzh, Keynman, KeiichiKasai, and KawaiiAsh。

      +

      本書は、オープンソースコミュニティの多数の貢献者による共同の努力のもと、継続的に改善されています。時間と労力を注いで執筆に携わってくださったすべての方々に感謝します。貢献者は以下のとおりです(GitHub により自動生成された順序):krahets、coderonion、Gonglja、nuomi1、Reanon、justin-tse、hpstory、danielsss、curtishd、night-cruise、S-N-O-R-L-A-X、rongyi、msk397、gvenusleo、khoaxuantu、rivertwilight、K3v123、gyt95、zhuoqinyue、yuelinxin、Zuoxun、mingXta、Phoenix0415、FangYuan33、GN-Yu、longsizhuo、IsChristina、xBLACKICEx、guowei-gong、Cathay-Chen、pengchzn、QiLOL、magentaqin、hello-ikun、JoseHung、qualifier1024、thomasq0、sunshinesDL、L-Super、Guanngxu、Transmigration-zhou、WSL0809、Slone123c、lhxsm、yuan0221、what-is-me、Shyam-Chen、theNefelibatas、longranger2、codeberg-user、xiongsp、JeffersonHuang、prinpal、seven1240、Wonderdch、malone6、xiaomiusa87、gaofer、bluebean-cloud、a16su、SamJin98、hongyun-robot、nanlei、XiaChuerwu、yd-j、iron-irax、mgisr、steventimes、junminhong、heshuyue、danny900714、MolDuM、Nigh、Dr-XYZ、XC-Zero、reeswell、PXG-XPG、NI-SW、Horbin-Magician、Enlightenus、YangXuanyi、beatrix-chan、DullSword、xjr7670、jiaxianhua、qq909244296、iStig、boloboloda、hts0000、gledfish、wenjianmin、keshida、kilikilikid、lclc6、lwbaptx、linyejoe2、liuxjerry、llql1211、fbigm、echo1937、szu17dmy、dshlstarr、Yucao-cy、coderlef、czruby、bongbongbakudan、beintentional、ZongYangL、ZhongYuuu、ZhongGuanbin、hezhizhen、linzeyan、ZJKung、luluxia、xb534、ztkuaikuai、yw-1021、ElaBosak233、baagod、zhouLion、yishangzhang、yi427、yanedie、yabo083、weibk、wangwang105、th1nk3r-ing、tao363、4yDX3906、syd168、sslmj2020、smilelsb、siqyka、selear、sdshaoda、Xi-Row、popozhu、nuquist19、noobcodemaker、XiaoK29、chadyi、lyl625760、lucaswangdev、0130w、shanghai-Jerry、EJackYang、Javesun99、eltociear、lipusheng、KNChiu、BlindTerran、ShiMaRing、lovelock、FreddieLi、FloranceYeh、fanchenggang、gltianwen、goerll、nedchu、curly210102、CuB3y0nd、KraHsu、CarrotDLaw、youshaoXG、bubble9um、Asashishi、Asa0oo0o0o、fanenr、eagleanurag、akshiterate、52coder、foursevenlove、KorsChen、GaochaoZhu、hopkings2008、yang-le、realwujing、Evilrabbit520、Umer-Jahangir、Turing-1024-Lee、Suremotoo、paoxiaomooo、Chieko-Seren、Allen-Scai、ymmmas、Risuntsy、Richard-Zhang1019、RafaelCaso、qingpeng9802、primexiao、Urbaner3、zhongfq、nidhoggfgg、MwumLi、CreatorMetaSky、martinx、ZnYang2018、hugtyftg、logan-qiu、psychelzh、Keynman、KeiichiKasai、KawaiiAsh。

      この本のコードレビュー作業は、coderonion, Gonglja, gvenusleo, hpstory, justin‐tse, khoaxuantu, krahets, night-cruise, nuomi1, Reanon and rongyi(アルファベット順)によって完了されました。彼らの時間と努力に感謝し、様々な言語でのコードの標準化と統一性を確保してくださいました。

      -

      この本の繁体字中国語版は Shyam-Chen と Dr-XYZ によってレビューされ、英語版は yuelinxin, K3v123, QiLOL, Phoenix0415, SamJin98, yanedie, RafaelCaso, pengchzn, thomasq0, and magentaqin によってレビューされました。彼らの継続的な貢献により、この本がより広い読者に届き、役立つことができます。

      +

      本書の繁体字中国語版は Shyam-Chen および Dr-XYZ によってレビューされ、英語版は yuelinxin、K3v123、QiLOL、Phoenix0415、SamJin98、yanedie、RafaelCaso、pengchzn、thomasq0、magentaqin によってレビューされ、日本語版は eltociear によってレビューされました。彼らの継続的な貢献のおかげで、本書はより幅広い読者層に提供することができています。ここに深く感謝いたします。

      この本の制作過程において、多くの方々から貴重な支援をいただきました。これらに限定されませんが:

      • 会社でのメンター、李熙博士に感謝します。ある会話で「早く行動しろ」と励ましていただき、この本を書く決意を固めることができました。
      • diff --git a/ja/javascripts/katex.js b/ja/javascripts/katex.js index 8f142c23f..f71f1b2e4 100644 --- a/ja/javascripts/katex.js +++ b/ja/javascripts/katex.js @@ -8,4 +8,4 @@ document$.subscribe(({ body }) => { ], }); }); -/*! update cache: 20260104155757 */ +/*! update cache: 20260104161456 */ diff --git a/ja/javascripts/mathjax.js b/ja/javascripts/mathjax.js index 2e7cf3530..bb4273b40 100644 --- a/ja/javascripts/mathjax.js +++ b/ja/javascripts/mathjax.js @@ -15,4 +15,4 @@ window.MathJax = { document$.subscribe(() => { MathJax.typesetPromise(); }); -/*! update cache: 20260104155757 */ +/*! update cache: 20260104161456 */ diff --git a/ja/javascripts/starfield.js b/ja/javascripts/starfield.js index 0ebd9c585..e1f9f0624 100644 --- a/ja/javascripts/starfield.js +++ b/ja/javascripts/starfield.js @@ -469,4 +469,4 @@ return Starfield; }); -/*! update cache: 20260104155757 */ +/*! update cache: 20260104161456 */ diff --git a/ja/search/search_index.json b/ja/search/search_index.json index 638ea161e..ae2265d8c 100644 --- a/ja/search/search_index.json +++ b/ja/search/search_index.json @@ -1 +1 @@ -{"config":{"lang":["ja"],"separator":"[\\s\\-\u3000\u3001\u3002\uff0c\uff0e]+","pipeline":["stemmer"],"fields":{"title":{"boost":1000.0},"text":{"boost":1.0},"tags":{"boost":1000000.0}}},"docs":[{"location":"chapter_appendix/","title":"\u7b2c 16 \u7ae0 \u00a0 \u4ed8\u9332","text":""},{"location":"chapter_appendix/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 16.1 \u00a0 \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u74b0\u5883\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb
        • 16.2 \u00a0 \u4e00\u7dd2\u306b\u5275\u4f5c\u306b\u53c2\u52a0
        • 16.3 \u00a0 \u7528\u8a9e\u96c6
        "},{"location":"chapter_appendix/contribution/","title":"16.2 \u00a0 \u30b3\u30f3\u30c8\u30ea\u30d3\u30e5\u30fc\u30b7\u30e7\u30f3","text":"

        \u8457\u8005\u306e\u80fd\u529b\u306b\u9650\u308a\u304c\u3042\u308b\u305f\u3081\u3001\u672c\u66f8\u306b\u306f\u3044\u304f\u3064\u304b\u306e\u7701\u7565\u3084\u8aa4\u308a\u304c\u907f\u3051\u3089\u308c\u307e\u305b\u3093\u3002\u3054\u7406\u89e3\u3092\u304a\u9858\u3044\u3057\u307e\u3059\u3002\u8aa4\u5b57\u3001\u30ea\u30f3\u30af\u5207\u308c\u3001\u5185\u5bb9\u306e\u6b20\u843d\u3001\u6587\u7ae0\u306e\u66d6\u6627\u3055\u3001\u8aac\u660e\u306e\u4e0d\u660e\u78ba\u3055\u3001\u307e\u305f\u306f\u4e0d\u5408\u7406\u306a\u6587\u7ae0\u69cb\u9020\u3092\u767a\u898b\u3055\u308c\u305f\u5834\u5408\u306f\u3001\u8aad\u8005\u306b\u3088\u308a\u826f\u8cea\u306a\u5b66\u7fd2\u30ea\u30bd\u30fc\u30b9\u3092\u63d0\u4f9b\u3059\u308b\u305f\u3081\u3001\u4fee\u6b63\u306b\u3054\u5354\u529b\u304f\u3060\u3055\u3044\u3002

        \u3059\u3079\u3066\u306e\u30b3\u30f3\u30c8\u30ea\u30d3\u30e5\u30fc\u30bf\u30fc\u306eGitHub ID\u306f\u3001\u672c\u66f8\u306e\u30ea\u30dd\u30b8\u30c8\u30ea\u3001\u30a6\u30a7\u30d6\u3001PDF\u30d0\u30fc\u30b8\u30e7\u30f3\u306e\u30db\u30fc\u30e0\u30da\u30fc\u30b8\u306b\u8868\u793a\u3055\u308c\u3001\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u30b3\u30df\u30e5\u30cb\u30c6\u30a3\u3078\u306e\u7121\u79c1\u306e\u8ca2\u732e\u306b\u611f\u8b1d\u3044\u305f\u3057\u307e\u3059\u3002

        \u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u306e\u9b45\u529b

        \u7d19\u306e\u672c\u306e2\u3064\u306e\u5370\u5237\u7248\u306e\u9593\u9694\u306f\u3057\u3070\u3057\u3070\u9577\u304f\u3001\u5185\u5bb9\u306e\u66f4\u65b0\u304c\u975e\u5e38\u306b\u4e0d\u4fbf\u3067\u3059\u3002

        \u3057\u304b\u3057\u3001\u3053\u306e\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u306e\u672c\u3067\u306f\u3001\u5185\u5bb9\u306e\u66f4\u65b0\u30b5\u30a4\u30af\u30eb\u306f\u6570\u65e5\u3001\u3055\u3089\u306b\u306f\u6570\u6642\u9593\u306b\u77ed\u7e2e\u3055\u308c\u307e\u3059\u3002

        "},{"location":"chapter_appendix/contribution/#1","title":"1. \u00a0 \u5185\u5bb9\u306e\u5fae\u8abf\u6574","text":"

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5404\u30da\u30fc\u30b8\u306e\u53f3\u4e0a\u89d2\u306b\u300c\u7de8\u96c6\u30a2\u30a4\u30b3\u30f3\u300d\u304c\u3042\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u624b\u9806\u306b\u5f93\u3063\u3066\u30c6\u30ad\u30b9\u30c8\u3084\u30b3\u30fc\u30c9\u3092\u4fee\u6b63\u3067\u304d\u307e\u3059\u3002

        1. \u300c\u7de8\u96c6\u30a2\u30a4\u30b3\u30f3\u300d\u3092\u30af\u30ea\u30c3\u30af\u3057\u307e\u3059\u3002\u300c\u3053\u306e\u30ea\u30dd\u30b8\u30c8\u30ea\u3092\u30d5\u30a9\u30fc\u30af\u3057\u307e\u3059\u304b\u300d\u3068\u4fc3\u3055\u308c\u305f\u5834\u5408\u306f\u3001\u540c\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002
        2. Markdown\u30bd\u30fc\u30b9\u30d5\u30a1\u30a4\u30eb\u306e\u5185\u5bb9\u3092\u4fee\u6b63\u3057\u3001\u5185\u5bb9\u306e\u6b63\u78ba\u6027\u3092\u78ba\u8a8d\u3057\u3001\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u306e\u4e00\u8cab\u6027\u3092\u4fdd\u3064\u3088\u3046\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002
        3. \u30da\u30fc\u30b8\u306e\u4e0b\u90e8\u3067\u4fee\u6b63\u8aac\u660e\u3092\u8a18\u5165\u3057\u3001\u300cPropose file change\u300d\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3057\u307e\u3059\u3002\u30da\u30fc\u30b8\u304c\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3055\u308c\u305f\u5f8c\u3001\u300cCreate pull request\u300d\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3057\u3066\u30d7\u30eb\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u958b\u59cb\u3057\u307e\u3059\u3002

        \u56f3 16-3 \u00a0 \u30da\u30fc\u30b8\u7de8\u96c6\u30dc\u30bf\u30f3

        \u56f3\u306f\u76f4\u63a5\u4fee\u6b63\u3067\u304d\u306a\u3044\u305f\u3081\u3001\u65b0\u3057\u3044Issue\u3092\u4f5c\u6210\u3059\u308b\u304b\u3001\u554f\u984c\u3092\u8aac\u660e\u3059\u308b\u30b3\u30e1\u30f3\u30c8\u304c\u5fc5\u8981\u3067\u3059\u3002\u3067\u304d\u308b\u3060\u3051\u65e9\u304f\u56f3\u3092\u518d\u63cf\u753b\u3057\u3066\u7f6e\u304d\u63db\u3048\u307e\u3059\u3002

        "},{"location":"chapter_appendix/contribution/#2","title":"2. \u00a0 \u5185\u5bb9\u306e\u4f5c\u6210","text":"

        \u3053\u306e\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3078\u306e\u53c2\u52a0\u306b\u8208\u5473\u304c\u3042\u308b\u5834\u5408\u3001\u30b3\u30fc\u30c9\u3092\u4ed6\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u7ffb\u8a33\u3057\u305f\u308a\u3001\u8a18\u4e8b\u306e\u5185\u5bb9\u3092\u62e1\u5f35\u3057\u305f\u308a\u3059\u308b\u3053\u3068\u3092\u542b\u3081\u3066\u3001\u4ee5\u4e0b\u306e\u30d7\u30eb\u30ea\u30af\u30a8\u30b9\u30c8\u30ef\u30fc\u30af\u30d5\u30ed\u30fc\u3092\u5b9f\u88c5\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        1. GitHub\u306b\u30ed\u30b0\u30a4\u30f3\u3057\u3001\u672c\u66f8\u306e\u30b3\u30fc\u30c9\u30ea\u30dd\u30b8\u30c8\u30ea\u3092\u500b\u4eba\u30a2\u30ab\u30a6\u30f3\u30c8\u306b\u30d5\u30a9\u30fc\u30af\u3057\u307e\u3059\u3002
        2. \u30d5\u30a9\u30fc\u30af\u3057\u305f\u30ea\u30dd\u30b8\u30c8\u30ea\u306e\u30a6\u30a7\u30d6\u30da\u30fc\u30b8\u306b\u79fb\u52d5\u3057\u3001git clone\u30b3\u30de\u30f3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u30ea\u30dd\u30b8\u30c8\u30ea\u3092\u30ed\u30fc\u30ab\u30eb\u30de\u30b7\u30f3\u306b\u30af\u30ed\u30fc\u30f3\u3057\u307e\u3059\u3002
        3. \u30ed\u30fc\u30ab\u30eb\u3067\u5185\u5bb9\u3092\u4f5c\u6210\u3057\u3001\u5b8c\u5168\u306a\u30c6\u30b9\u30c8\u3092\u5b9f\u884c\u3057\u3066\u30b3\u30fc\u30c9\u306e\u6b63\u78ba\u6027\u3092\u691c\u8a3c\u3057\u307e\u3059\u3002
        4. \u30ed\u30fc\u30ab\u30eb\u3067\u884c\u3063\u305f\u5909\u66f4\u3092\u30b3\u30df\u30c3\u30c8\u3057\u3001\u30ea\u30e2\u30fc\u30c8\u30ea\u30dd\u30b8\u30c8\u30ea\u306b\u30d7\u30c3\u30b7\u30e5\u3057\u307e\u3059\u3002
        5. \u30ea\u30dd\u30b8\u30c8\u30ea\u306e\u30a6\u30a7\u30d6\u30da\u30fc\u30b8\u3092\u66f4\u65b0\u3057\u3001\u300cCreate pull request\u300d\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3057\u3066\u30d7\u30eb\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u958b\u59cb\u3057\u307e\u3059\u3002
        "},{"location":"chapter_appendix/contribution/#3-docker","title":"3. \u00a0 Docker\u30c7\u30d7\u30ed\u30a4\u30e1\u30f3\u30c8","text":"

        hello-algo\u30eb\u30fc\u30c8\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3067\u3001\u4ee5\u4e0b\u306eDocker\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u5b9f\u884c\u3057\u3066http://localhost:8000\u3067\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306b\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\uff1a

        docker-compose up -d\n

        \u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u30c7\u30d7\u30ed\u30a4\u30e1\u30f3\u30c8\u3092\u524a\u9664\u3057\u307e\u3059\uff1a

        docker-compose down\n
        "},{"location":"chapter_appendix/installation/","title":"16.1 \u00a0 \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb","text":""},{"location":"chapter_appendix/installation/#1611-ide","title":"16.1.1 \u00a0 IDE\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb","text":"

        \u30ed\u30fc\u30ab\u30eb\u306e\u7d71\u5408\u958b\u767a\u74b0\u5883\uff08IDE\uff09\u3068\u3057\u3066\u3001\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u3067\u8efd\u91cf\u306aVS Code\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002VS Code\u516c\u5f0f\u30a6\u30a7\u30d6\u30b5\u30a4\u30c8\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3001\u304a\u4f7f\u3044\u306e\u30aa\u30da\u30ec\u30fc\u30c6\u30a3\u30f3\u30b0\u30b7\u30b9\u30c6\u30e0\u306b\u9069\u3057\u305fVS Code\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u9078\u629e\u3057\u3066\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3001\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u56f3 16-1 \u00a0 \u516c\u5f0f\u30a6\u30a7\u30d6\u30b5\u30a4\u30c8\u304b\u3089VS Code\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9

        VS Code\u306b\u306f\u5f37\u529b\u306a\u62e1\u5f35\u6a5f\u80fd\u30a8\u30b3\u30b7\u30b9\u30c6\u30e0\u304c\u3042\u308a\u3001\u307b\u3068\u3093\u3069\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u5b9f\u884c\u3068\u30c7\u30d0\u30c3\u30b0\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u300cPython Extension Pack\u300d\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u305f\u5f8c\u3001Python\u30b3\u30fc\u30c9\u3092\u30c7\u30d0\u30c3\u30b0\u3067\u304d\u307e\u3059\u3002\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u624b\u9806\u3092\u4e0b\u306e\u56f3\u306b\u793a\u3057\u307e\u3059\u3002

        \u56f3 16-2 \u00a0 VS Code\u62e1\u5f35\u6a5f\u80fd\u30d1\u30c3\u30af\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb

        "},{"location":"chapter_appendix/installation/#1612","title":"16.1.2 \u00a0 \u8a00\u8a9e\u74b0\u5883\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb","text":""},{"location":"chapter_appendix/installation/#1-python","title":"1. \u00a0 Python\u74b0\u5883","text":"
        1. Miniconda3\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002Python 3.10\u4ee5\u964d\u304c\u5fc5\u8981\u3067\u3059\u3002
        2. VS Code\u62e1\u5f35\u6a5f\u80fd\u30de\u30fc\u30b1\u30c3\u30c8\u30d7\u30ec\u30a4\u30b9\u3067python\u3092\u691c\u7d22\u3057\u3001Python Extension Pack\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        3. \uff08\u30aa\u30d7\u30b7\u30e7\u30f3\uff09\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u3067pip install black\u3092\u5165\u529b\u3057\u3066\u3001\u30b3\u30fc\u30c9\u30d5\u30a9\u30fc\u30de\u30c3\u30c6\u30a3\u30f3\u30b0\u30c4\u30fc\u30eb\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        "},{"location":"chapter_appendix/installation/#2-cc","title":"2. \u00a0 C/C++\u74b0\u5883","text":"
        1. Windows\u30b7\u30b9\u30c6\u30e0\u3067\u306fMinGW\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff08\u8a2d\u5b9a\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\uff09\u3002MacOS\u306b\u306fClang\u304c\u4ed8\u5c5e\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u306f\u4e0d\u8981\u3067\u3059\u3002
        2. VS Code\u62e1\u5f35\u6a5f\u80fd\u30de\u30fc\u30b1\u30c3\u30c8\u30d7\u30ec\u30a4\u30b9\u3067c++\u3092\u691c\u7d22\u3057\u3001C/C++ Extension Pack\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        3. \uff08\u30aa\u30d7\u30b7\u30e7\u30f3\uff09\u8a2d\u5b9a\u30da\u30fc\u30b8\u3092\u958b\u304d\u3001Clang_format_fallback Style\u30b3\u30fc\u30c9\u30d5\u30a9\u30fc\u30de\u30c3\u30c6\u30a3\u30f3\u30b0\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u691c\u7d22\u3057\u3001{ BasedOnStyle: Microsoft, BreakBeforeBraces: Attach }\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002
        "},{"location":"chapter_appendix/installation/#3-java","title":"3. \u00a0 Java\u74b0\u5883","text":"
        1. OpenJDK\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\uff08\u30d0\u30fc\u30b8\u30e7\u30f3\u306fJDK 9\u3088\u308a\u65b0\u3057\u3044\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff09\u3002
        2. VS Code\u62e1\u5f35\u6a5f\u80fd\u30de\u30fc\u30b1\u30c3\u30c8\u30d7\u30ec\u30a4\u30b9\u3067java\u3092\u691c\u7d22\u3057\u3001Extension Pack for Java\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        "},{"location":"chapter_appendix/installation/#4-c","title":"4. \u00a0 C#\u74b0\u5883","text":"
        1. .Net 8.0\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        2. VS Code\u62e1\u5f35\u6a5f\u80fd\u30de\u30fc\u30b1\u30c3\u30c8\u30d7\u30ec\u30a4\u30b9\u3067C# Dev Kit\u3092\u691c\u7d22\u3057\u3001C# Dev Kit\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\uff08\u8a2d\u5b9a\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\uff09\u3002
        3. Visual Studio\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\uff08\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\uff09\u3002
        "},{"location":"chapter_appendix/installation/#5-go","title":"5. \u00a0 Go\u74b0\u5883","text":"
        1. go\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        2. VS Code\u62e1\u5f35\u6a5f\u80fd\u30de\u30fc\u30b1\u30c3\u30c8\u30d7\u30ec\u30a4\u30b9\u3067go\u3092\u691c\u7d22\u3057\u3001Go\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        3. Ctrl + Shift + P\u3092\u62bc\u3057\u3066\u30b3\u30de\u30f3\u30c9\u30d0\u30fc\u3092\u547c\u3073\u51fa\u3057\u3001go\u3068\u5165\u529b\u3057\u3001Go: Install/Update Tools\u3092\u9078\u629e\u3057\u3001\u3059\u3079\u3066\u3092\u9078\u629e\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        "},{"location":"chapter_appendix/installation/#6-swift","title":"6. \u00a0 Swift\u74b0\u5883","text":"
        1. Swift\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        2. VS Code\u62e1\u5f35\u6a5f\u80fd\u30de\u30fc\u30b1\u30c3\u30c8\u30d7\u30ec\u30a4\u30b9\u3067swift\u3092\u691c\u7d22\u3057\u3001Swift for Visual Studio Code\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        "},{"location":"chapter_appendix/installation/#7-javascript","title":"7. \u00a0 JavaScript\u74b0\u5883","text":"
        1. Node.js\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        2. \uff08\u30aa\u30d7\u30b7\u30e7\u30f3\uff09VS Code\u62e1\u5f35\u6a5f\u80fd\u30de\u30fc\u30b1\u30c3\u30c8\u30d7\u30ec\u30a4\u30b9\u3067Prettier\u3092\u691c\u7d22\u3057\u3001\u30b3\u30fc\u30c9\u30d5\u30a9\u30fc\u30de\u30c3\u30c6\u30a3\u30f3\u30b0\u30c4\u30fc\u30eb\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        "},{"location":"chapter_appendix/installation/#8-typescript","title":"8. \u00a0 TypeScript\u74b0\u5883","text":"
        1. JavaScript\u74b0\u5883\u3068\u540c\u3058\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u624b\u9806\u306b\u5f93\u3044\u307e\u3059\u3002
        2. TypeScript Execute (tsx)\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        3. VS Code\u62e1\u5f35\u6a5f\u80fd\u30de\u30fc\u30b1\u30c3\u30c8\u30d7\u30ec\u30a4\u30b9\u3067typescript\u3092\u691c\u7d22\u3057\u3001Pretty TypeScript Errors\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        "},{"location":"chapter_appendix/installation/#9-dart","title":"9. \u00a0 Dart\u74b0\u5883","text":"
        1. Dart\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        2. VS Code\u62e1\u5f35\u6a5f\u80fd\u30de\u30fc\u30b1\u30c3\u30c8\u30d7\u30ec\u30a4\u30b9\u3067dart\u3092\u691c\u7d22\u3057\u3001Dart\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        "},{"location":"chapter_appendix/installation/#10-rust","title":"10. \u00a0 Rust\u74b0\u5883","text":"
        1. Rust\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        2. VS Code\u62e1\u5f35\u6a5f\u80fd\u30de\u30fc\u30b1\u30c3\u30c8\u30d7\u30ec\u30a4\u30b9\u3067rust\u3092\u691c\u7d22\u3057\u3001rust-analyzer\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        "},{"location":"chapter_appendix/terminology/","title":"16.3 \u00a0 \u7528\u8a9e\u96c6","text":"

        \u4e0b\u306e\u8868\u306f\u672c\u66f8\u306b\u767b\u5834\u3059\u308b\u91cd\u8981\u306a\u7528\u8a9e\u3092\u30ea\u30b9\u30c8\u30a2\u30c3\u30d7\u3057\u3066\u304a\u308a\u3001\u4ee5\u4e0b\u306e\u70b9\u306b\u6ce8\u610f\u3059\u308b\u4fa1\u5024\u304c\u3042\u308a\u307e\u3059\u3002

        • \u82f1\u8a9e\u6587\u732e\u3092\u8aad\u307f\u3084\u3059\u304f\u3059\u308b\u305f\u3081\u3001\u7528\u8a9e\u306e\u82f1\u8a9e\u540d\u3092\u899a\u3048\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002
        • \u4e00\u90e8\u306e\u7528\u8a9e\u306f\u7c21\u4f53\u5b57\u4e2d\u56fd\u8a9e\u3068\u7e41\u4f53\u5b57\u4e2d\u56fd\u8a9e\u3067\u7570\u306a\u308b\u540d\u524d\u3092\u6301\u3061\u307e\u3059\u3002

        \u8868 16-1 \u00a0 \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u91cd\u8981\u7528\u8a9e

        English \u65e5\u672c\u8a9e \u7b80\u4f53\u4e2d\u6587 \u7e41\u4f53\u4e2d\u6587 algorithm \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0 \u7b97\u6cd5 \u6f14\u7b97\u6cd5 data structure \u30c7\u30fc\u30bf\u69cb\u9020 \u6570\u636e\u7ed3\u6784 \u8cc7\u6599\u7d50\u69cb code \u30b3\u30fc\u30c9 \u4ee3\u7801 \u7a0b\u5f0f\u78bc file \u30d5\u30a1\u30a4\u30eb \u6587\u4ef6 \u6a94\u6848 function \u95a2\u6570 \u51fd\u6570 \u51fd\u5f0f method \u30e1\u30bd\u30c3\u30c9 \u65b9\u6cd5 \u65b9\u6cd5 variable \u5909\u6570 \u53d8\u91cf \u8b8a\u6578 asymptotic complexity analysis \u6f38\u8fd1\u8a08\u7b97\u91cf\u89e3\u6790 \u6e10\u8fd1\u590d\u6742\u5ea6\u5206\u6790 \u6f38\u8fd1\u8907\u96dc\u5ea6\u5206\u6790 time complexity \u6642\u9593\u8a08\u7b97\u91cf \u65f6\u95f4\u590d\u6742\u5ea6 \u6642\u9593\u8907\u96dc\u5ea6 space complexity \u7a7a\u9593\u8a08\u7b97\u91cf \u7a7a\u95f4\u590d\u6742\u5ea6 \u7a7a\u9593\u8907\u96dc\u5ea6 loop \u30eb\u30fc\u30d7 \u5faa\u73af \u8ff4\u5708 iteration \u53cd\u5fa9 \u8fed\u4ee3 \u8fed\u4ee3 recursion \u518d\u5e30 \u9012\u5f52 \u905e\u8ff4 tail recursion \u672b\u5c3e\u518d\u5e30 \u5c3e\u9012\u5f52 \u5c3e\u905e\u8ff4 recursion tree \u518d\u5e30\u6728 \u9012\u5f52\u6811 \u905e\u8ff4\u6a39 big-\\(O\\) notation \u30d3\u30c3\u30b0O\u8a18\u6cd5 \u5927 \\(O\\) \u8bb0\u53f7 \u5927 \\(O\\) \u8a18\u865f asymptotic upper bound \u6f38\u8fd1\u4e0a\u754c \u6e10\u8fd1\u4e0a\u754c \u6f38\u8fd1\u4e0a\u754c sign-magnitude \u7b26\u53f7\u3068\u7d76\u5bfe\u5024 \u539f\u7801 \u539f\u78bc 1's complement 1\u306e\u88dc\u6570 \u53cd\u7801 \u4e00\u88dc\u6578 2's complement 2\u306e\u88dc\u6570 \u8865\u7801 \u4e8c\u88dc\u6578 array \u914d\u5217 \u6570\u7ec4 \u9663\u5217 index \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 \u7d22\u5f15 \u7d22\u5f15 linked list \u9023\u7d50\u30ea\u30b9\u30c8 \u94fe\u8868 \u93c8\u7d50\u4e32\u5217 linked list node, list node \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9 \u94fe\u8868\u8282\u70b9 \u93c8\u7d50\u4e32\u5217\u7bc0\u9ede head node \u5148\u982d\u30ce\u30fc\u30c9 \u5934\u8282\u70b9 \u982d\u7bc0\u9ede tail node \u672b\u5c3e\u30ce\u30fc\u30c9 \u5c3e\u8282\u70b9 \u5c3e\u7bc0\u9ede list \u30ea\u30b9\u30c8 \u5217\u8868 \u4e32\u5217 dynamic array \u52d5\u7684\u914d\u5217 \u52a8\u6001\u6570\u7ec4 \u52d5\u614b\u9663\u5217 hard disk \u30cf\u30fc\u30c9\u30c7\u30a3\u30b9\u30af \u786c\u76d8 \u786c\u789f random-access memory (RAM) \u30e1\u30e2\u30ea \u5185\u5b58 \u8a18\u61b6\u9ad4 cache memory \u30ad\u30e3\u30c3\u30b7\u30e5\u30e1\u30e2\u30ea \u7f13\u5b58 \u5feb\u53d6 cache miss \u30ad\u30e3\u30c3\u30b7\u30e5\u30df\u30b9 \u7f13\u5b58\u672a\u547d\u4e2d \u5feb\u53d6\u672a\u547d\u4e2d cache hit rate \u30ad\u30e3\u30c3\u30b7\u30e5\u30d2\u30c3\u30c8\u7387 \u7f13\u5b58\u547d\u4e2d\u7387 \u5feb\u53d6\u547d\u4e2d\u7387 stack \u30b9\u30bf\u30c3\u30af \u6808 \u5806\u758a top of the stack \u30b9\u30bf\u30c3\u30af\u30c8\u30c3\u30d7 \u6808\u9876 \u5806\u758a\u9802 bottom of the stack \u30b9\u30bf\u30c3\u30af\u30dc\u30c8\u30e0 \u6808\u5e95 \u5806\u758a\u5e95 queue \u30ad\u30e5\u30fc \u961f\u5217 \u4f47\u5217 double-ended queue \u4e21\u7aef\u30ad\u30e5\u30fc \u53cc\u5411\u961f\u5217 \u96d9\u5411\u4f47\u5217 front of the queue \u30ad\u30e5\u30fc\u306e\u5148\u982d \u961f\u9996 \u4f47\u5217\u9996 rear of the queue \u30ad\u30e5\u30fc\u306e\u672b\u5c3e \u961f\u5c3e \u4f47\u5217\u5c3e hash table \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb \u54c8\u5e0c\u8868 \u96dc\u6e4a\u8868 hash set \u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8 \u54c8\u5e0c\u96c6\u5408 \u96dc\u6e4a\u96c6\u5408 bucket \u30d0\u30b1\u30c3\u30c8 \u6876 \u6876 hash function \u30cf\u30c3\u30b7\u30e5\u95a2\u6570 \u54c8\u5e0c\u51fd\u6570 \u96dc\u6e4a\u51fd\u5f0f hash collision \u30cf\u30c3\u30b7\u30e5\u885d\u7a81 \u54c8\u5e0c\u51b2\u7a81 \u96dc\u6e4a\u885d\u7a81 load factor \u8ca0\u8377\u7387 \u8d1f\u8f7d\u56e0\u5b50 \u8ca0\u8f09\u56e0\u5b50 separate chaining \u30c1\u30a7\u30a4\u30f3\u6cd5 \u94fe\u5f0f\u5730\u5740 \u93c8\u7d50\u4f4d\u5740 open addressing \u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5 \u5f00\u653e\u5bfb\u5740 \u958b\u653e\u5b9a\u5740 linear probing \u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u6cd5 \u7ebf\u6027\u63a2\u6d4b \u7dda\u6027\u63a2\u67e5 lazy deletion \u9045\u5ef6\u524a\u9664 \u61d2\u5220\u9664 \u61f6\u522a\u9664 binary tree \u4e8c\u5206\u6728 \u4e8c\u53c9\u6811 \u4e8c\u5143\u6a39 tree node \u6728\u306e\u30ce\u30fc\u30c9 \u6811\u8282\u70b9 \u6a39\u7bc0\u9ede left-child node \u5de6\u306e\u5b50\u30ce\u30fc\u30c9 \u5de6\u5b50\u8282\u70b9 \u5de6\u5b50\u7bc0\u9ede right-child node \u53f3\u306e\u5b50\u30ce\u30fc\u30c9 \u53f3\u5b50\u8282\u70b9 \u53f3\u5b50\u7bc0\u9ede parent node \u89aa\u30ce\u30fc\u30c9 \u7236\u8282\u70b9 \u7236\u7bc0\u9ede left subtree \u5de6\u306e\u90e8\u5206\u6728 \u5de6\u5b50\u6811 \u5de6\u5b50\u6a39 right subtree \u53f3\u306e\u90e8\u5206\u6728 \u53f3\u5b50\u6811 \u53f3\u5b50\u6a39 root node \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9 \u6839\u8282\u70b9 \u6839\u7bc0\u9ede leaf node \u8449\u30ce\u30fc\u30c9 \u53f6\u8282\u70b9 \u8449\u7bc0\u9ede edge \u30a8\u30c3\u30b8 \u8fb9 \u908a level \u30ec\u30d9\u30eb \u5c42 \u5c64 degree \u6b21\u6570 \u5ea6 \u5ea6 height \u9ad8\u3055 \u9ad8\u5ea6 \u9ad8\u5ea6 depth \u6df1\u3055 \u6df1\u5ea6 \u6df1\u5ea6 perfect binary tree \u5b8c\u5168\u4e8c\u5206\u6728 \u5b8c\u7f8e\u4e8c\u53c9\u6811 \u5b8c\u7f8e\u4e8c\u5143\u6a39 complete binary tree \u5b8c\u5168\u4e8c\u5206\u6728 \u5b8c\u5168\u4e8c\u53c9\u6811 \u5b8c\u5168\u4e8c\u5143\u6a39 full binary tree \u6e80\u4e8c\u5206\u6728 \u5b8c\u6ee1\u4e8c\u53c9\u6811 \u5b8c\u6eff\u4e8c\u5143\u6a39 balanced binary tree \u5e73\u8861\u4e8c\u5206\u6728 \u5e73\u8861\u4e8c\u53c9\u6811 \u5e73\u8861\u4e8c\u5143\u6a39 binary search tree \u4e8c\u5206\u63a2\u7d22\u6728 \u4e8c\u53c9\u641c\u7d22\u6811 \u4e8c\u5143\u641c\u5c0b\u6a39 AVL tree AVL\u6728 AVL \u6811 AVL \u6a39 red-black tree \u8d64\u9ed2\u6728 \u7ea2\u9ed1\u6811 \u7d05\u9ed1\u6a39 level-order traversal \u30ec\u30d9\u30eb\u9806\u8d70\u67fb \u5c42\u5e8f\u904d\u5386 \u5c64\u5e8f\u8d70\u8a2a breadth-first traversal \u5e45\u512a\u5148\u8d70\u67fb \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 \u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a depth-first traversal \u6df1\u3055\u512a\u5148\u8d70\u67fb \u6df1\u5ea6\u4f18\u5148\u904d\u5386 \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a binary search tree \u4e8c\u5206\u63a2\u7d22\u6728 \u4e8c\u53c9\u641c\u7d22\u6811 \u4e8c\u5143\u641c\u5c0b\u6a39 balanced binary search tree \u5e73\u8861\u4e8c\u5206\u63a2\u7d22\u6728 \u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811 \u5e73\u8861\u4e8c\u5143\u641c\u5c0b\u6a39 balance factor \u5e73\u8861\u56e0\u5b50 \u5e73\u8861\u56e0\u5b50 \u5e73\u8861\u56e0\u5b50 heap \u30d2\u30fc\u30d7 \u5806 \u5806\u7a4d max heap \u6700\u5927\u30d2\u30fc\u30d7 \u5927\u9876\u5806 \u5927\u9802\u5806\u7a4d min heap \u6700\u5c0f\u30d2\u30fc\u30d7 \u5c0f\u9876\u5806 \u5c0f\u9802\u5806\u7a4d priority queue \u512a\u5148\u5ea6\u30ad\u30e5\u30fc \u4f18\u5148\u961f\u5217 \u512a\u5148\u4f47\u5217 heapify \u30d2\u30fc\u30d7\u5316 \u5806\u5316 \u5806\u7a4d\u5316 top-\\(k\\) problem Top-\\(k\\) \u554f\u984c Top-\\(k\\) \u95ee\u9898 Top-\\(k\\) \u554f\u984c graph \u30b0\u30e9\u30d5 \u56fe \u5716 vertex \u9802\u70b9 \u9876\u70b9 \u9802\u9ede undirected graph \u7121\u5411\u30b0\u30e9\u30d5 \u65e0\u5411\u56fe \u7121\u5411\u5716 directed graph \u6709\u5411\u30b0\u30e9\u30d5 \u6709\u5411\u56fe \u6709\u5411\u5716 connected graph \u9023\u7d50\u30b0\u30e9\u30d5 \u8fde\u901a\u56fe \u9023\u901a\u5716 disconnected graph \u975e\u9023\u7d50\u30b0\u30e9\u30d5 \u975e\u8fde\u901a\u56fe \u975e\u9023\u901a\u5716 weighted graph \u91cd\u307f\u4ed8\u304d\u30b0\u30e9\u30d5 \u6709\u6743\u56fe \u6709\u6b0a\u5716 adjacency \u96a3\u63a5 \u90bb\u63a5 \u9130\u63a5 path \u30d1\u30b9 \u8def\u5f84 \u8def\u5f91 in-degree \u5165\u6b21\u6570 \u5165\u5ea6 \u5165\u5ea6 out-degree \u51fa\u6b21\u6570 \u51fa\u5ea6 \u51fa\u5ea6 adjacency matrix \u96a3\u63a5\u884c\u5217 \u90bb\u63a5\u77e9\u9635 \u9130\u63a5\u77e9\u9663 adjacency list \u96a3\u63a5\u30ea\u30b9\u30c8 \u90bb\u63a5\u8868 \u9130\u63a5\u8868 breadth-first search \u5e45\u512a\u5148\u63a2\u7d22 \u5e7f\u5ea6\u4f18\u5148\u641c\u7d22 \u5ee3\u5ea6\u512a\u5148\u641c\u5c0b depth-first search \u6df1\u3055\u512a\u5148\u63a2\u7d22 \u6df1\u5ea6\u4f18\u5148\u641c\u7d22 \u6df1\u5ea6\u512a\u5148\u641c\u5c0b binary search \u4e8c\u5206\u63a2\u7d22 \u4e8c\u5206\u67e5\u627e \u4e8c\u5206\u641c\u5c0b searching algorithm \u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0 \u641c\u7d22\u7b97\u6cd5 \u641c\u5c0b\u6f14\u7b97\u6cd5 sorting algorithm \u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0 \u6392\u5e8f\u7b97\u6cd5 \u6392\u5e8f\u6f14\u7b97\u6cd5 selection sort \u9078\u629e\u30bd\u30fc\u30c8 \u9009\u62e9\u6392\u5e8f \u9078\u64c7\u6392\u5e8f bubble sort \u30d0\u30d6\u30eb\u30bd\u30fc\u30c8 \u5192\u6ce1\u6392\u5e8f \u6ce1\u6cab\u6392\u5e8f insertion sort \u633f\u5165\u30bd\u30fc\u30c8 \u63d2\u5165\u6392\u5e8f \u63d2\u5165\u6392\u5e8f quick sort \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8 \u5feb\u901f\u6392\u5e8f \u5feb\u901f\u6392\u5e8f merge sort \u30de\u30fc\u30b8\u30bd\u30fc\u30c8 \u5f52\u5e76\u6392\u5e8f \u5408\u4f75\u6392\u5e8f heap sort \u30d2\u30fc\u30d7\u30bd\u30fc\u30c8 \u5806\u6392\u5e8f \u5806\u7a4d\u6392\u5e8f bucket sort \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8 \u6876\u6392\u5e8f \u6876\u6392\u5e8f counting sort \u8a08\u6570\u30bd\u30fc\u30c8 \u8ba1\u6570\u6392\u5e8f \u8a08\u6578\u6392\u5e8f radix sort \u57fa\u6570\u30bd\u30fc\u30c8 \u57fa\u6570\u6392\u5e8f \u57fa\u6578\u6392\u5e8f divide and conquer \u5206\u5272\u7d71\u6cbb\u6cd5 \u5206\u6cbb \u5206\u6cbb hanota problem \u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c \u6c49\u8bfa\u5854\u95ee\u9898 \u6cb3\u5167\u5854\u554f\u984c backtracking algorithm \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0 \u56de\u6eaf\u7b97\u6cd5 \u56de\u6eaf\u6f14\u7b97\u6cd5 constraint \u5236\u7d04 \u7ea6\u675f \u7d04\u675f solution \u89e3 \u89e3 \u89e3 state \u72b6\u614b \u72b6\u6001 \u72c0\u614b pruning \u679d\u5208\u308a \u526a\u679d \u526a\u679d permutations problem \u9806\u5217\u554f\u984c \u5168\u6392\u5217\u95ee\u9898 \u5168\u6392\u5217\u554f\u984c subset-sum problem \u90e8\u5206\u96c6\u5408\u548c\u554f\u984c \u5b50\u96c6\u548c\u95ee\u9898 \u5b50\u96c6\u5408\u554f\u984c \\(n\\)-queens problem \\(n\\) \u30af\u30a4\u30fc\u30f3\u554f\u984c \\(n\\) \u7687\u540e\u95ee\u9898 \\(n\\) \u7687\u540e\u554f\u984c dynamic programming \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 \u52a8\u6001\u89c4\u5212 \u52d5\u614b\u898f\u5283 initial state \u521d\u671f\u72b6\u614b \u521d\u59cb\u72b6\u6001 \u521d\u59cb\u72c0\u614b state-transition equation \u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f \u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b \u72c0\u614b\u8f49\u79fb\u65b9\u7a0b knapsack problem \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c \u80cc\u5305\u95ee\u9898 \u80cc\u5305\u554f\u984c edit distance problem \u7de8\u96c6\u8ddd\u96e2\u554f\u984c \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898 \u7de8\u8f2f\u8ddd\u96e2\u554f\u984c greedy algorithm \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0 \u8d2a\u5fc3\u7b97\u6cd5 \u8caa\u5a6a\u6f14\u7b97\u6cd5"},{"location":"chapter_array_and_linkedlist/","title":"\u7b2c 4 \u7ae0 \u00a0 \u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8","text":"

        Abstract

        \u30c7\u30fc\u30bf\u69cb\u9020\u306e\u4e16\u754c\u306f\u9811\u4e08\u306a\u30ec\u30f3\u30ac\u306e\u58c1\u306b\u4f3c\u3066\u3044\u307e\u3059\u3002

        \u914d\u5217\u3067\u306f\u3001\u30ec\u30f3\u30ac\u304c\u3074\u3063\u305f\u308a\u3068\u6574\u5217\u3057\u3001\u305d\u308c\u305e\u308c\u304c\u6b21\u306e\u3082\u306e\u3068\u7d99\u304e\u76ee\u306a\u304f\u96a3\u308a\u5408\u3063\u3066\u3001\u7d71\u4e00\u3055\u308c\u305f\u5f62\u6210\u3092\u4f5c\u3063\u3066\u3044\u308b\u59ff\u3092\u60f3\u50cf\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4e00\u65b9\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u306f\u3001\u3053\u308c\u3089\u306e\u30ec\u30f3\u30ac\u304c\u81ea\u7531\u306b\u6563\u3089\u3070\u308a\u3001\u305d\u308c\u3089\u306e\u9593\u3092\u512a\u96c5\u306b\u7de8\u307f\u8fbc\u3080\u8526\u306b\u62b1\u304b\u308c\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_array_and_linkedlist/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 4.1 \u00a0 \u914d\u5217
        • 4.2 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8
        • 4.3 \u00a0 \u30ea\u30b9\u30c8
        • 4.4 \u00a0 \u30e1\u30e2\u30ea\u3068\u30ad\u30e3\u30c3\u30b7\u30e5 *
        • 4.5 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_array_and_linkedlist/array/","title":"4.1 \u00a0 \u914d\u5217","text":"

        \u914d\u5217\u306f\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3001\u540c\u3058\u3088\u3046\u306a\u9805\u76ee\u304c\u4e26\u3093\u3067\u3044\u308b\u3088\u3046\u306a\u3082\u306e\u3067\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u30e1\u30e2\u30ea\u5185\u306e\u9023\u7d9a\u3057\u305f\u7a7a\u9593\u306b\u4e00\u7dd2\u306b\u683c\u7d0d\u3055\u308c\u307e\u3059\u3002\u3053\u308c\u306f\u6574\u7406\u3055\u308c\u305f\u683c\u7d0d\u3092\u7dad\u6301\u3059\u308b\u30b7\u30fc\u30b1\u30f3\u30b9\u306e\u3088\u3046\u306a\u3082\u306e\u3067\u3059\u3002\u3053\u306e\u4e26\u3073\u306e\u5404\u9805\u76ee\u306b\u306f\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3068\u3057\u3066\u77e5\u3089\u308c\u308b\u72ec\u81ea\u306e\u300c\u4f4d\u7f6e\u300d\u304c\u3042\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u3092\u53c2\u7167\u3057\u3066\u3001\u914d\u5217\u306e\u52d5\u4f5c\u3092\u89b3\u5bdf\u3057\u3001\u3053\u308c\u3089\u306e\u91cd\u8981\u306a\u7528\u8a9e\u3092\u7406\u89e3\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u56f3 4-1 \u00a0 \u914d\u5217\u306e\u5b9a\u7fa9\u3068\u683c\u7d0d\u65b9\u6cd5

        "},{"location":"chapter_array_and_linkedlist/array/#411","title":"4.1.1 \u00a0 \u914d\u5217\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/array/#1","title":"1. \u00a0 \u914d\u5217\u306e\u521d\u671f\u5316","text":"

        \u914d\u5217\u306f\u5fc5\u8981\u306b\u5fdc\u3058\u30662\u3064\u306e\u65b9\u6cd5\u3067\u521d\u671f\u5316\u3067\u304d\u307e\u3059\uff1a\u521d\u671f\u5024\u306a\u3057\u307e\u305f\u306f\u6307\u5b9a\u3055\u308c\u305f\u521d\u671f\u5024\u4ed8\u304d\u3067\u3059\u3002\u521d\u671f\u5024\u304c\u6307\u5b9a\u3055\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u3001\u307b\u3068\u3093\u3069\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306f\u914d\u5217\u8981\u7d20\u3092\\(0\\)\u306b\u8a2d\u5b9a\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin array.py
        # \u914d\u5217\u3092\u521d\u671f\u5316\narr: list[int] = [0] * 5  # [ 0, 0, 0, 0, 0 ]\nnums: list[int] = [1, 3, 2, 5, 4]\n
        array.cpp
        /* \u914d\u5217\u3092\u521d\u671f\u5316 */\n// \u30b9\u30bf\u30c3\u30af\u306b\u683c\u7d0d\nint arr[5];\nint nums[5] = { 1, 3, 2, 5, 4 };\n// \u30d2\u30fc\u30d7\u306b\u683c\u7d0d\uff08\u624b\u52d5\u3067\u306e\u30e1\u30e2\u30ea\u89e3\u653e\u304c\u5fc5\u8981\uff09\nint* arr1 = new int[5];\nint* nums1 = new int[5] { 1, 3, 2, 5, 4 };\n
        array.java
        /* \u914d\u5217\u3092\u521d\u671f\u5316 */\nint[] arr = new int[5]; // { 0, 0, 0, 0, 0 }\nint[] nums = { 1, 3, 2, 5, 4 };\n
        array.cs
        /* \u914d\u5217\u3092\u521d\u671f\u5316 */\nint[] arr = new int[5]; // [ 0, 0, 0, 0, 0 ]\nint[] nums = [1, 3, 2, 5, 4];\n
        array.go
        /* \u914d\u5217\u3092\u521d\u671f\u5316 */\nvar arr [5]int\n// Go\u3067\u306f\u3001\u9577\u3055\u3092\u6307\u5b9a\uff08[5]int\uff09\u3059\u308b\u3068\u914d\u5217\u3092\u793a\u3057\u3001\u6307\u5b9a\u3057\u306a\u3044\uff08[]int\uff09\u3068\u30b9\u30e9\u30a4\u30b9\u3092\u793a\u3057\u307e\u3059\u3002\n// Go\u306e\u914d\u5217\u306f\u30b3\u30f3\u30d1\u30a4\u30eb\u6642\u306b\u56fa\u5b9a\u9577\u3092\u6301\u3064\u3088\u3046\u8a2d\u8a08\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u9577\u3055\u306e\u6307\u5b9a\u306b\u306f\u5b9a\u6570\u306e\u307f\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\n// extend()\u30e1\u30bd\u30c3\u30c9\u306e\u5b9f\u88c5\u306e\u4fbf\u5b9c\u4e0a\u3001\u3053\u3053\u3067\u306fSlice\u3092\u914d\u5217\u3068\u3057\u3066\u6271\u3044\u307e\u3059\u3002\nnums := []int{1, 3, 2, 5, 4}\n
        array.swift
        /* \u914d\u5217\u3092\u521d\u671f\u5316 */\nlet arr = Array(repeating: 0, count: 5) // [0, 0, 0, 0, 0]\nlet nums = [1, 3, 2, 5, 4]\n
        array.js
        /* \u914d\u5217\u3092\u521d\u671f\u5316 */\nvar arr = new Array(5).fill(0);\nvar nums = [1, 3, 2, 5, 4];\n
        array.ts
        /* \u914d\u5217\u3092\u521d\u671f\u5316 */\nlet arr: number[] = new Array(5).fill(0);\nlet nums: number[] = [1, 3, 2, 5, 4];\n
        array.dart
        /* \u914d\u5217\u3092\u521d\u671f\u5316 */\nList<int> arr = List.filled(5, 0); // [0, 0, 0, 0, 0]\nList<int> nums = [1, 3, 2, 5, 4];\n
        array.rs
        /* \u914d\u5217\u3092\u521d\u671f\u5316 */\nlet arr: [i32; 5] = [0; 5]; // [0, 0, 0, 0, 0]\nlet slice: &[i32] = &[0; 5];\n// Rust\u3067\u306f\u3001\u9577\u3055\u3092\u6307\u5b9a\uff08[i32; 5]\uff09\u3059\u308b\u3068\u914d\u5217\u3092\u793a\u3057\u3001\u6307\u5b9a\u3057\u306a\u3044\uff08&[i32]\uff09\u3068\u30b9\u30e9\u30a4\u30b9\u3092\u793a\u3057\u307e\u3059\u3002\n// Rust\u306e\u914d\u5217\u306f\u30b3\u30f3\u30d1\u30a4\u30eb\u6642\u306b\u56fa\u5b9a\u9577\u3092\u6301\u3064\u3088\u3046\u8a2d\u8a08\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u9577\u3055\u306e\u6307\u5b9a\u306b\u306f\u5b9a\u6570\u306e\u307f\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\n// \u4e00\u822c\u7684\u306bRust\u3067\u306f\u52d5\u7684\u914d\u5217\u3068\u3057\u3066Vector\u304c\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\n// extend()\u30e1\u30bd\u30c3\u30c9\u306e\u5b9f\u88c5\u306e\u4fbf\u5b9c\u4e0a\u3001\u3053\u3053\u3067\u306f\u30d9\u30af\u30bf\u30fc\u3092\u914d\u5217\u3068\u3057\u3066\u6271\u3044\u307e\u3059\u3002\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
        array.c
        /* \u914d\u5217\u3092\u521d\u671f\u5316 */\nint arr[5] = { 0 }; // { 0, 0, 0, 0, 0 }\nint nums[5] = { 1, 3, 2, 5, 4 };\n
        array.kt
        \n
        "},{"location":"chapter_array_and_linkedlist/array/#2","title":"2. \u00a0 \u8981\u7d20\u3078\u306e\u30a2\u30af\u30bb\u30b9","text":"

        \u914d\u5217\u5185\u306e\u8981\u7d20\u306f\u9023\u7d9a\u3057\u305f\u30e1\u30e2\u30ea\u7a7a\u9593\u306b\u683c\u7d0d\u3055\u308c\u308b\u305f\u3081\u3001\u5404\u8981\u7d20\u306e\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u3092\u8a08\u7b97\u3059\u308b\u3053\u3068\u304c\u7c21\u5358\u306b\u306a\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u308b\u516c\u5f0f\u306f\u3001\u914d\u5217\u306e\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\uff08\u7279\u306b\u3001\u6700\u521d\u306e\u8981\u7d20\u306e\u30a2\u30c9\u30ec\u30b9\uff09\u3068\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u5229\u7528\u3057\u3066\u3001\u8981\u7d20\u306e\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u3092\u6c7a\u5b9a\u3059\u308b\u306e\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002\u3053\u306e\u8a08\u7b97\u306b\u3088\u308a\u3001\u76ee\u7684\u306e\u8981\u7d20\u3078\u306e\u76f4\u63a5\u30a2\u30af\u30bb\u30b9\u304c\u5408\u7406\u5316\u3055\u308c\u307e\u3059\u3002

        \u56f3 4-2 \u00a0 \u914d\u5217\u8981\u7d20\u306e\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u8a08\u7b97

        \u4e0a\u306e\u56f3\u3067\u89b3\u5bdf\u3055\u308c\u308b\u3088\u3046\u306b\u3001\u914d\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u6163\u4f8b\u7684\u306b\\(0\\)\u304b\u3089\u59cb\u307e\u308a\u307e\u3059\u3002\u3053\u308c\u306f\u76f4\u611f\u306b\u53cd\u3059\u308b\u3088\u3046\u306b\u898b\u3048\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u6570\u3092\u6570\u3048\u308b\u306e\u306f\u901a\u5e38\\(1\\)\u304b\u3089\u59cb\u307e\u308b\u305f\u3081\u3067\u3059\u304c\u3001\u30a2\u30c9\u30ec\u30b9\u8a08\u7b97\u516c\u5f0f\u5185\u3067\u306f\u3001**\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u672c\u8cea\u7684\u306b\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u304b\u3089\u306e\u30aa\u30d5\u30bb\u30c3\u30c8**\u3067\u3059\u3002\u6700\u521d\u306e\u8981\u7d20\u306e\u30a2\u30c9\u30ec\u30b9\u3067\u306f\u3001\u3053\u306e\u30aa\u30d5\u30bb\u30c3\u30c8\u306f\\(0\\)\u3067\u3001\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\\(0\\)\u3067\u3042\u308b\u3053\u3068\u3092\u691c\u8a3c\u3057\u3066\u3044\u307e\u3059\u3002

        \u914d\u5217\u5185\u306e\u8981\u7d20\u3078\u306e\u30a2\u30af\u30bb\u30b9\u306f\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3001\\(O(1)\\)\u6642\u9593\u3067\u4efb\u610f\u306e\u8981\u7d20\u306b\u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\u3067\u304d\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def random_access(nums: list[int]) -> int:\n    \"\"\"\u8981\u7d20\u3078\u306e\u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\"\"\"\n    # \u533a\u9593 [0, len(nums)-1] \u304b\u3089\u6570\u5024\u3092\u30e9\u30f3\u30c0\u30e0\u306b\u9078\u629e\n    random_index = random.randint(0, len(nums) - 1)\n    # \u30e9\u30f3\u30c0\u30e0\u306a\u8981\u7d20\u3092\u53d6\u5f97\u3057\u3066\u8fd4\u3059\n    random_num = nums[random_index]\n    return random_num\n
        array.cpp
        /* \u8981\u7d20\u3078\u306e\u4e71\u6570\u30a2\u30af\u30bb\u30b9 */\nint randomAccess(int *nums, int size) {\n    // [0, size)\u306e\u7bc4\u56f2\u3067\u4e71\u6570\u3092\u9078\u629e\n    int randomIndex = rand() % size;\n    // \u4e71\u6570\u8981\u7d20\u3092\u53d6\u5f97\u3057\u3066\u8fd4\u5374\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
        array.java
        /* \u8981\u7d20\u3078\u306e\u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9 */\nint randomAccess(int[] nums) {\n    // \u533a\u9593 [0, nums.length) \u304b\u3089\u30e9\u30f3\u30c0\u30e0\u306b\u6570\u3092\u9078\u629e\n    int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);\n    // \u30e9\u30f3\u30c0\u30e0\u8981\u7d20\u3092\u53d6\u5f97\u3057\u3066\u8fd4\u3059\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
        array.cs
        [class]{array}-[func]{RandomAccess}\n
        array.go
        [class]{}-[func]{randomAccess}\n
        array.swift
        [class]{}-[func]{randomAccess}\n
        array.js
        [class]{}-[func]{randomAccess}\n
        array.ts
        [class]{}-[func]{randomAccess}\n
        array.dart
        [class]{}-[func]{randomAccess}\n
        array.rs
        [class]{}-[func]{random_access}\n
        array.c
        [class]{}-[func]{randomAccess}\n
        array.kt
        [class]{}-[func]{randomAccess}\n
        array.rb
        [class]{}-[func]{random_access}\n
        "},{"location":"chapter_array_and_linkedlist/array/#3","title":"3. \u00a0 \u8981\u7d20\u306e\u633f\u5165","text":"

        \u914d\u5217\u8981\u7d20\u306f\u30e1\u30e2\u30ea\u5185\u3067\u5bc6\u306b\u8a70\u307e\u3063\u3066\u304a\u308a\u3001\u305d\u308c\u3089\u306e\u9593\u306b\u8ffd\u52a0\u30c7\u30fc\u30bf\u3092\u53ce\u5bb9\u3059\u308b\u305f\u3081\u306e\u7a7a\u9593\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u914d\u5217\u306e\u4e2d\u592e\u306b\u8981\u7d20\u3092\u633f\u5165\u3059\u308b\u306b\u306f\u3001\u5f8c\u7d9a\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u305a\u3064\u5f8c\u308d\u306b\u30b7\u30d5\u30c8\u3057\u3066\u3001\u65b0\u3057\u3044\u8981\u7d20\u306e\u305f\u3081\u306e\u7a7a\u9593\u3092\u4f5c\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u56f3 4-3 \u00a0 \u914d\u5217\u8981\u7d20\u633f\u5165\u306e\u4f8b

        \u914d\u5217\u306e\u9577\u3055\u304c\u56fa\u5b9a\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u8981\u7d20\u3092\u633f\u5165\u3059\u308b\u3068\u5fc5\u7136\u7684\u306b\u914d\u5217\u306e\u6700\u5f8c\u306e\u8981\u7d20\u304c\u5931\u308f\u308c\u308b\u3053\u3068\u306b\u6ce8\u610f\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002\u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u65b9\u6cd5\u306f\u300c\u30ea\u30b9\u30c8\u300d\u306e\u7ae0\u3067\u63a2\u6c42\u3055\u308c\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def insert(nums: list[int], num: int, index: int):\n    \"\"\"\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 index \u306b\u8981\u7d20 num \u3092\u633f\u5165\"\"\"\n    # \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 index \u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u5f8c\u308d\u306b\u79fb\u52d5\n    for i in range(len(nums) - 1, index, -1):\n        nums[i] = nums[i - 1]\n    # num \u3092 index \u306e\u4f4d\u7f6e\u306e\u8981\u7d20\u306b\u4ee3\u5165\n    nums[index] = num\n
        array.cpp
        /* `index`\u306b\u8981\u7d20num\u3092\u633f\u5165 */\nvoid insert(int *nums, int size, int num, int index) {\n    // `index`\u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u5f8c\u308d\u306b\u79fb\u52d5\n    for (int i = size - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // index\u306e\u4f4d\u7f6e\u306bnum\u3092\u4ee3\u5165\n    nums[index] = num;\n}\n
        array.java
        /* `index` \u306b\u8981\u7d20 num \u3092\u633f\u5165 */\nvoid insert(int[] nums, int num, int index) {\n    // `index` \u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u5f8c\u308d\u306b\u79fb\u52d5\n    for (int i = nums.length - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // index \u306e\u8981\u7d20\u306b num \u3092\u4ee3\u5165\n    nums[index] = num;\n}\n
        array.cs
        [class]{array}-[func]{Insert}\n
        array.go
        [class]{}-[func]{insert}\n
        array.swift
        [class]{}-[func]{insert}\n
        array.js
        [class]{}-[func]{insert}\n
        array.ts
        [class]{}-[func]{insert}\n
        array.dart
        [class]{}-[func]{insert}\n
        array.rs
        [class]{}-[func]{insert}\n
        array.c
        [class]{}-[func]{insert}\n
        array.kt
        [class]{}-[func]{insert}\n
        array.rb
        [class]{}-[func]{insert}\n
        "},{"location":"chapter_array_and_linkedlist/array/#4","title":"4. \u00a0 \u8981\u7d20\u306e\u524a\u9664","text":"

        \u540c\u69d8\u306b\u3001\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(i\\)\u306e\u8981\u7d20\u3092\u524a\u9664\u3059\u308b\u306b\u306f\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(i\\)\u306b\u7d9a\u304f\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u305a\u3064\u524d\u306b\u79fb\u52d5\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u56f3 4-4 \u00a0 \u914d\u5217\u8981\u7d20\u524a\u9664\u306e\u4f8b

        \u524a\u9664\u5f8c\u3001\u5143\u306e\u6700\u5f8c\u306e\u8981\u7d20\u306f\u300c\u610f\u5473\u304c\u306a\u3044\u300d\u3082\u306e\u306b\u306a\u308b\u305f\u3081\u3001\u7279\u5b9a\u306e\u4fee\u6b63\u306f\u5fc5\u8981\u306a\u3044\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def remove(nums: list[int], index: int):\n    \"\"\"\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 index \u306e\u8981\u7d20\u3092\u524a\u9664\"\"\"\n    # \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 index \u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u524d\u306b\u79fb\u52d5\n    for i in range(index, len(nums) - 1):\n        nums[i] = nums[i + 1]\n
        array.cpp
        /* `index`\u306e\u8981\u7d20\u3092\u524a\u9664 */\nvoid remove(int *nums, int size, int index) {\n    // `index`\u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u524d\u306b\u79fb\u52d5\n    for (int i = index; i < size - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
        array.java
        /* `index` \u306e\u8981\u7d20\u3092\u524a\u9664 */\nvoid remove(int[] nums, int index) {\n    // `index` \u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u524d\u306b\u79fb\u52d5\n    for (int i = index; i < nums.length - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
        array.cs
        [class]{array}-[func]{Remove}\n
        array.go
        [class]{}-[func]{remove}\n
        array.swift
        [class]{}-[func]{remove}\n
        array.js
        [class]{}-[func]{remove}\n
        array.ts
        [class]{}-[func]{remove}\n
        array.dart
        [class]{}-[func]{remove}\n
        array.rs
        [class]{}-[func]{remove}\n
        array.c
        [class]{}-[func]{removeItem}\n
        array.kt
        [class]{}-[func]{remove}\n
        array.rb
        [class]{}-[func]{remove}\n

        \u8981\u7d04\u3059\u308b\u3068\u3001\u914d\u5217\u306e\u633f\u5165\u3068\u524a\u9664\u64cd\u4f5c\u306b\u306f\u4ee5\u4e0b\u306e\u6b20\u70b9\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u9ad8\u3044\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u914d\u5217\u306e\u633f\u5165\u3068\u524a\u9664\u306e\u4e21\u65b9\u306e\u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3001\u3053\u3053\u3067\\(n\\)\u306f\u914d\u5217\u306e\u9577\u3055\u3067\u3059\u3002
        • \u8981\u7d20\u306e\u640d\u5931\uff1a\u914d\u5217\u306e\u9577\u3055\u304c\u56fa\u5b9a\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u633f\u5165\u6642\u306b\u914d\u5217\u306e\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u8981\u7d20\u306f\u5931\u308f\u308c\u307e\u3059\u3002
        • \u30e1\u30e2\u30ea\u306e\u7121\u99c4\uff1a\u3088\u308a\u9577\u3044\u914d\u5217\u3092\u521d\u671f\u5316\u3057\u3066\u524d\u90e8\u5206\u306e\u307f\u3092\u5229\u7528\u3059\u308b\u3068\u3001\u633f\u5165\u6642\u306b\u300c\u610f\u5473\u306e\u306a\u3044\u300d\u672b\u5c3e\u8981\u7d20\u304c\u751f\u3058\u3001\u30e1\u30e2\u30ea\u7a7a\u9593\u306e\u7121\u99c4\u306b\u3064\u306a\u304c\u308a\u307e\u3059\u3002
        "},{"location":"chapter_array_and_linkedlist/array/#5","title":"5. \u00a0 \u914d\u5217\u306e\u8d70\u67fb","text":"

        \u307b\u3068\u3093\u3069\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u306f\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u4f7f\u7528\u3059\u308b\u304b\u3001\u5404\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9\u3059\u308b\u3053\u3068\u3067\u914d\u5217\u3092\u8d70\u67fb\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def traverse(nums: list[int]):\n    \"\"\"\u914d\u5217\u306e\u8d70\u67fb\"\"\"\n    count = 0\n    # \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u3088\u308b\u914d\u5217\u306e\u8d70\u67fb\n    for i in range(len(nums)):\n        count += nums[i]\n    # \u914d\u5217\u8981\u7d20\u306e\u8d70\u67fb\n    for num in nums:\n        count += num\n    # \u30c7\u30fc\u30bf\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3068\u8981\u7d20\u306e\u4e21\u65b9\u3092\u8d70\u67fb\n    for i, num in enumerate(nums):\n        count += nums[i]\n        count += num\n
        array.cpp
        /* \u914d\u5217\u306e\u8d70\u67fb */\nvoid traverse(int *nums, int size) {\n    int count = 0;\n    // \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u3088\u308b\u914d\u5217\u306e\u8d70\u67fb\n    for (int i = 0; i < size; i++) {\n        count += nums[i];\n    }\n}\n
        array.java
        /* \u914d\u5217\u3092\u8d70\u67fb */\nvoid traverse(int[] nums) {\n    int count = 0;\n    // \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u3088\u308b\u914d\u5217\u306e\u8d70\u67fb\n    for (int i = 0; i < nums.length; i++) {\n        count += nums[i];\n    }\n    // \u914d\u5217\u8981\u7d20\u306e\u8d70\u67fb\n    for (int num : nums) {\n        count += num;\n    }\n}\n
        array.cs
        [class]{array}-[func]{Traverse}\n
        array.go
        [class]{}-[func]{traverse}\n
        array.swift
        [class]{}-[func]{traverse}\n
        array.js
        [class]{}-[func]{traverse}\n
        array.ts
        [class]{}-[func]{traverse}\n
        array.dart
        [class]{}-[func]{traverse}\n
        array.rs
        [class]{}-[func]{traverse}\n
        array.c
        [class]{}-[func]{traverse}\n
        array.kt
        [class]{}-[func]{traverse}\n
        array.rb
        [class]{}-[func]{traverse}\n
        "},{"location":"chapter_array_and_linkedlist/array/#6","title":"6. \u00a0 \u8981\u7d20\u306e\u691c\u7d22","text":"

        \u914d\u5217\u5185\u306e\u7279\u5b9a\u306e\u8981\u7d20\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u306f\u3001\u914d\u5217\u3092\u53cd\u5fa9\u3057\u3001\u5404\u8981\u7d20\u3092\u30c1\u30a7\u30c3\u30af\u3057\u3066\u76ee\u7684\u306e\u5024\u3068\u4e00\u81f4\u3059\u308b\u304b\u3069\u3046\u304b\u3092\u6c7a\u5b9a\u3059\u308b\u3053\u3068\u3092\u542b\u307f\u307e\u3059\u3002

        \u914d\u5217\u306f\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3042\u308b\u305f\u3081\u3001\u3053\u306e\u64cd\u4f5c\u306f\u4e00\u822c\u7684\u306b\u300c\u7dda\u5f62\u63a2\u7d22\u300d\u3068\u547c\u3070\u308c\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def find(nums: list[int], target: int) -> int:\n    \"\"\"\u914d\u5217\u5185\u306e\u6307\u5b9a\u3055\u308c\u305f\u8981\u7d20\u3092\u691c\u7d22\"\"\"\n    for i in range(len(nums)):\n        if nums[i] == target:\n            return i\n    return -1\n
        array.cpp
        /* \u914d\u5217\u5185\u306e\u6307\u5b9a\u8981\u7d20\u3092\u691c\u7d22 */\nint find(int *nums, int size, int target) {\n    for (int i = 0; i < size; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
        array.java
        /* \u914d\u5217\u5185\u3067\u6307\u5b9a\u3055\u308c\u305f\u8981\u7d20\u3092\u691c\u7d22 */\nint find(int[] nums, int target) {\n    for (int i = 0; i < nums.length; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
        array.cs
        [class]{array}-[func]{Find}\n
        array.go
        [class]{}-[func]{find}\n
        array.swift
        [class]{}-[func]{find}\n
        array.js
        [class]{}-[func]{find}\n
        array.ts
        [class]{}-[func]{find}\n
        array.dart
        [class]{}-[func]{find}\n
        array.rs
        [class]{}-[func]{find}\n
        array.c
        [class]{}-[func]{find}\n
        array.kt
        [class]{}-[func]{find}\n
        array.rb
        [class]{}-[func]{find}\n
        "},{"location":"chapter_array_and_linkedlist/array/#7","title":"7. \u00a0 \u914d\u5217\u306e\u62e1\u5f35","text":"

        \u8907\u96d1\u306a\u30b7\u30b9\u30c6\u30e0\u74b0\u5883\u3067\u306f\u3001\u5b89\u5168\u306a\u5bb9\u91cf\u62e1\u5f35\u306e\u305f\u3081\u306b\u914d\u5217\u306e\u5f8c\u306b\u30e1\u30e2\u30ea\u7a7a\u9593\u306e\u53ef\u7528\u6027\u3092\u78ba\u4fdd\u3059\u308b\u3053\u3068\u304c\u56f0\u96e3\u306b\u306a\u308a\u307e\u3059\u3002\u305d\u306e\u7d50\u679c\u3001\u307b\u3068\u3093\u3069\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u306f\u3001**\u914d\u5217\u306e\u9577\u3055\u306f\u4e0d\u5909**\u3067\u3059\u3002

        \u914d\u5217\u3092\u62e1\u5f35\u3059\u308b\u306b\u306f\u3001\u3088\u308a\u5927\u304d\u306a\u914d\u5217\u3092\u4f5c\u6210\u3057\u3001\u5143\u306e\u914d\u5217\u304b\u3089\u8981\u7d20\u3092\u30b3\u30d4\u30fc\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3001\u5927\u304d\u306a\u914d\u5217\u3067\u306f\u6642\u9593\u304c\u304b\u304b\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def extend(nums: list[int], enlarge: int) -> list[int]:\n    \"\"\"\u914d\u5217\u306e\u9577\u3055\u3092\u62e1\u5f35\"\"\"\n    # \u62e1\u5f35\u3055\u308c\u305f\u9577\u3055\u306e\u914d\u5217\u3092\u521d\u671f\u5316\n    res = [0] * (len(nums) + enlarge)\n    # \u5143\u306e\u914d\u5217\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u65b0\u3057\u3044\u914d\u5217\u306b\u30b3\u30d4\u30fc\n    for i in range(len(nums)):\n        res[i] = nums[i]\n    # \u62e1\u5f35\u5f8c\u306e\u65b0\u3057\u3044\u914d\u5217\u3092\u8fd4\u3059\n    return res\n
        array.cpp
        /* \u914d\u5217\u9577\u306e\u62e1\u5f35 */\nint *extend(int *nums, int size, int enlarge) {\n    // \u62e1\u5f35\u3055\u308c\u305f\u9577\u3055\u306e\u914d\u5217\u3092\u521d\u671f\u5316\n    int *res = new int[size + enlarge];\n    // \u5143\u306e\u914d\u5217\u306e\u5168\u8981\u7d20\u3092\u65b0\u3057\u3044\u914d\u5217\u306b\u30b3\u30d4\u30fc\n    for (int i = 0; i < size; i++) {\n        res[i] = nums[i];\n    }\n    // \u30e1\u30e2\u30ea\u3092\u89e3\u653e\n    delete[] nums;\n    // \u62e1\u5f35\u5f8c\u306e\u65b0\u3057\u3044\u914d\u5217\u3092\u8fd4\u5374\n    return res;\n}\n
        array.java
        /* \u914d\u5217\u9577\u306e\u62e1\u5f35 */\nint[] extend(int[] nums, int enlarge) {\n    // \u62e1\u5f35\u3055\u308c\u305f\u9577\u3055\u306e\u914d\u5217\u3092\u521d\u671f\u5316\n    int[] res = new int[nums.length + enlarge];\n    // \u5143\u306e\u914d\u5217\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u65b0\u3057\u3044\u914d\u5217\u306b\u30b3\u30d4\u30fc\n    for (int i = 0; i < nums.length; i++) {\n        res[i] = nums[i];\n    }\n    // \u62e1\u5f35\u5f8c\u306e\u65b0\u3057\u3044\u914d\u5217\u3092\u8fd4\u3059\n    return res;\n}\n
        array.cs
        [class]{array}-[func]{Extend}\n
        array.go
        [class]{}-[func]{extend}\n
        array.swift
        [class]{}-[func]{extend}\n
        array.js
        [class]{}-[func]{extend}\n
        array.ts
        [class]{}-[func]{extend}\n
        array.dart
        [class]{}-[func]{extend}\n
        array.rs
        [class]{}-[func]{extend}\n
        array.c
        [class]{}-[func]{extend}\n
        array.kt
        [class]{}-[func]{extend}\n
        array.rb
        [class]{}-[func]{extend}\n
        "},{"location":"chapter_array_and_linkedlist/array/#412","title":"4.1.2 \u00a0 \u914d\u5217\u306e\u5229\u70b9\u3068\u5236\u9650","text":"

        \u914d\u5217\u306f\u9023\u7d9a\u3057\u305f\u30e1\u30e2\u30ea\u7a7a\u9593\u306b\u683c\u7d0d\u3055\u308c\u3001\u540c\u3058\u578b\u306e\u8981\u7d20\u3067\u69cb\u6210\u3055\u308c\u307e\u3059\u3002\u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u304c\u30c7\u30fc\u30bf\u69cb\u9020\u64cd\u4f5c\u306e\u52b9\u7387\u3092\u6700\u9069\u5316\u3059\u308b\u305f\u3081\u306b\u6d3b\u7528\u3067\u304d\u308b\u5b9f\u8cea\u7684\u306a\u4e8b\u524d\u60c5\u5831\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002

        • \u9ad8\u3044\u7a7a\u9593\u52b9\u7387\uff1a\u914d\u5217\u306f\u30c7\u30fc\u30bf\u306e\u305f\u3081\u306e\u9023\u7d9a\u3057\u305f\u30e1\u30e2\u30ea\u30d6\u30ed\u30c3\u30af\u3092\u5272\u308a\u5f53\u3066\u3001\u8ffd\u52a0\u306e\u69cb\u9020\u7684\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u306e\u5fc5\u8981\u6027\u3092\u6392\u9664\u3057\u307e\u3059\u3002
        • \u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\u306e\u30b5\u30dd\u30fc\u30c8\uff1a\u914d\u5217\u306f\u4efb\u610f\u306e\u8981\u7d20\u3078\u306e\\(O(1)\\)\u6642\u9593\u30a2\u30af\u30bb\u30b9\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002
        • \u30ad\u30e3\u30c3\u30b7\u30e5\u5c40\u6240\u6027\uff1a\u914d\u5217\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u304d\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306f\u305d\u308c\u3089\u3092\u8aad\u307f\u8fbc\u3080\u3060\u3051\u3067\u306a\u304f\u3001\u5468\u56f2\u306e\u30c7\u30fc\u30bf\u3082\u30ad\u30e3\u30c3\u30b7\u30e5\u3057\u3001\u9ad8\u901f\u30ad\u30e3\u30c3\u30b7\u30e5\u3092\u5229\u7528\u3057\u3066\u5f8c\u7d9a\u306e\u64cd\u4f5c\u901f\u5ea6\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002

        \u3057\u304b\u3057\u3001\u9023\u7d9a\u7a7a\u9593\u683c\u7d0d\u306f\u8af8\u5203\u306e\u5263\u3067\u3001\u4ee5\u4e0b\u306e\u5236\u9650\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u633f\u5165\u3068\u524a\u9664\u306e\u52b9\u7387\u304c\u4f4e\u3044\uff1a\u914d\u5217\u306b\u591a\u304f\u306e\u8981\u7d20\u304c\u84c4\u7a4d\u3055\u308c\u308b\u3068\u3001\u8981\u7d20\u306e\u633f\u5165\u3084\u524a\u9664\u306b\u306f\u5927\u91cf\u306e\u8981\u7d20\u3092\u30b7\u30d5\u30c8\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u56fa\u5b9a\u9577\uff1a\u914d\u5217\u306e\u9577\u3055\u306f\u521d\u671f\u5316\u5f8c\u306b\u56fa\u5b9a\u3055\u308c\u307e\u3059\u3002\u914d\u5217\u3092\u62e1\u5f35\u3059\u308b\u306b\u306f\u3001\u3059\u3079\u3066\u306e\u30c7\u30fc\u30bf\u3092\u65b0\u3057\u3044\u914d\u5217\u306b\u30b3\u30d4\u30fc\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u5927\u304d\u306a\u30b3\u30b9\u30c8\u304c\u304b\u304b\u308a\u307e\u3059\u3002
        • \u7a7a\u9593\u306e\u7121\u99c4\uff1a\u5272\u308a\u5f53\u3066\u3089\u308c\u305f\u914d\u5217\u30b5\u30a4\u30ba\u304c\u5fc5\u8981\u4ee5\u4e0a\u306b\u5927\u304d\u3044\u5834\u5408\u3001\u4f59\u5206\u306a\u7a7a\u9593\u304c\u7121\u99c4\u306b\u306a\u308a\u307e\u3059\u3002
        "},{"location":"chapter_array_and_linkedlist/array/#413","title":"4.1.3 \u00a0 \u914d\u5217\u306e\u5178\u578b\u7684\u306a\u5fdc\u7528","text":"

        \u914d\u5217\u306f\u57fa\u672c\u7684\u3067\u5e83\u304f\u4f7f\u7528\u3055\u308c\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3059\u3002\u69d8\u3005\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u983b\u7e41\u306b\u5fdc\u7528\u3055\u308c\u3001\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u5b9f\u88c5\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002

        • \u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\uff1a\u914d\u5217\u306f\u30e9\u30f3\u30c0\u30e0\u30b5\u30f3\u30d7\u30ea\u30f3\u30b0\u304c\u5fc5\u8981\u306a\u3068\u304d\u306e\u30c7\u30fc\u30bf\u683c\u7d0d\u306b\u7406\u60f3\u7684\u3067\u3059\u3002\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u57fa\u3065\u3044\u3066\u30e9\u30f3\u30c0\u30e0\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u751f\u6210\u3059\u308b\u3053\u3068\u3067\u3001\u52b9\u7387\u7684\u306b\u30e9\u30f3\u30c0\u30e0\u30b5\u30f3\u30d7\u30ea\u30f3\u30b0\u3092\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002
        • \u30bd\u30fc\u30c8\u3068\u691c\u7d22\uff1a\u914d\u5217\u306f\u30bd\u30fc\u30c8\u3068\u691c\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u6700\u3082\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3059\u3002\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u3001\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u3001\u4e8c\u5206\u63a2\u7d22\u306a\u3069\u306e\u6280\u8853\u306f\u4e3b\u306b\u914d\u5217\u3067\u52d5\u4f5c\u3057\u307e\u3059\u3002
        • \u30eb\u30c3\u30af\u30a2\u30c3\u30d7\u30c6\u30fc\u30d6\u30eb\uff1a\u914d\u5217\u306f\u8fc5\u901f\u306a\u8981\u7d20\u3084\u95a2\u4fc2\u306e\u53d6\u5f97\u306e\u305f\u3081\u306e\u52b9\u7387\u7684\u306a\u30eb\u30c3\u30af\u30a2\u30c3\u30d7\u30c6\u30fc\u30d6\u30eb\u3068\u3057\u3066\u6a5f\u80fd\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u6587\u5b57\u3092ASCII\u30b3\u30fc\u30c9\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3059\u308b\u3053\u3068\u306f\u3001ASCII\u30b3\u30fc\u30c9\u5024\u3092\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3068\u3057\u3066\u4f7f\u7528\u3057\u3001\u5bfe\u5fdc\u3059\u308b\u8981\u7d20\u3092\u914d\u5217\u306b\u683c\u7d0d\u3059\u308b\u3053\u3068\u3067\u7c21\u5358\u306b\u306a\u308a\u307e\u3059\u3002
        • \u6a5f\u68b0\u5b66\u7fd2\uff1a\u30cb\u30e5\u30fc\u30e9\u30eb\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306e\u9818\u57df\u3067\u306f\u3001\u914d\u5217\u306f\u30d9\u30af\u30c8\u30eb\u3001\u884c\u5217\u3001\u30c6\u30f3\u30bd\u30eb\u3092\u542b\u3080\u91cd\u8981\u306a\u7dda\u5f62\u4ee3\u6570\u6f14\u7b97\u306e\u5b9f\u884c\u306b\u304a\u3044\u3066\u91cd\u8981\u306a\u5f79\u5272\u3092\u679c\u305f\u3057\u307e\u3059\u3002\u914d\u5217\u306f\u30cb\u30e5\u30fc\u30e9\u30eb\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306b\u304a\u3044\u3066\u4e3b\u8981\u304b\u3064\u6700\u3082\u5e83\u7bc4\u56f2\u306b\u4f7f\u7528\u3055\u308c\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u3057\u3066\u6a5f\u80fd\u3057\u307e\u3059\u3002
        • \u30c7\u30fc\u30bf\u69cb\u9020\u306e\u5b9f\u88c5\uff1a\u914d\u5217\u306f\u3001\u30b9\u30bf\u30c3\u30af\u3001\u30ad\u30e5\u30fc\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3001\u30d2\u30fc\u30d7\u3001\u30b0\u30e9\u30d5\u306a\u3069\u3001\u69d8\u3005\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306e\u69cb\u6210\u8981\u7d20\u3068\u3057\u3066\u6a5f\u80fd\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u30b0\u30e9\u30d5\u306e\u96a3\u63a5\u884c\u5217\u8868\u73fe\u306f\u672c\u8cea\u7684\u306b\u4e8c\u6b21\u5143\u914d\u5217\u3067\u3059\u3002
        "},{"location":"chapter_array_and_linkedlist/linked_list/","title":"4.2 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8","text":"

        \u30e1\u30e2\u30ea\u7a7a\u9593\u306f\u3001\u3059\u3079\u3066\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u9593\u3067\u5171\u6709\u3055\u308c\u308b\u30ea\u30bd\u30fc\u30b9\u3067\u3059\u3002\u8907\u96d1\u306a\u30b7\u30b9\u30c6\u30e0\u74b0\u5883\u3067\u306f\u3001\u4f7f\u7528\u53ef\u80fd\u306a\u30e1\u30e2\u30ea\u304c\u30e1\u30e2\u30ea\u7a7a\u9593\u5168\u4f53\u306b\u5206\u6563\u3057\u3066\u3044\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u914d\u5217\u306b\u5272\u308a\u5f53\u3066\u3089\u308c\u308b\u30e1\u30e2\u30ea\u306f\u9023\u7d9a\u3057\u3066\u3044\u308b\u5fc5\u8981\u304c\u3042\u308b\u3053\u3068\u3092\u7406\u89e3\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u975e\u5e38\u306b\u5927\u304d\u306a\u914d\u5217\u306e\u5834\u5408\u3001\u5341\u5206\u306a\u5927\u304d\u3055\u306e\u9023\u7d9a\u30e1\u30e2\u30ea\u7a7a\u9593\u3092\u898b\u3064\u3051\u308b\u306e\u306f\u56f0\u96e3\u306a\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u3053\u3067\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u67d4\u8edf\u306a\u5229\u70b9\u304c\u660e\u3089\u304b\u306b\u306a\u308a\u307e\u3059\u3002

        \u9023\u7d50\u30ea\u30b9\u30c8\u306f\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3042\u308a\u3001\u5404\u8981\u7d20\u306f\u30ce\u30fc\u30c9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3001\u30ce\u30fc\u30c9\u306f\u300c\u53c2\u7167\u300d\u3092\u901a\u3058\u3066\u76f8\u4e92\u63a5\u7d9a\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u53c2\u7167\u306f\u5f8c\u7d9a\u30ce\u30fc\u30c9\u306e\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u3092\u4fdd\u6301\u3057\u30011\u3064\u306e\u30ce\u30fc\u30c9\u304b\u3089\u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u30ca\u30d3\u30b2\u30fc\u30b7\u30e7\u30f3\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002

        \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u8a2d\u8a08\u3067\u306f\u3001\u30ce\u30fc\u30c9\u3092\u9023\u7d9a\u3059\u308b\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u3092\u5fc5\u8981\u3068\u305b\u305a\u306b\u3001\u30e1\u30e2\u30ea\u4f4d\u7f6e\u5168\u4f53\u306b\u5206\u6563\u914d\u7f6e\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u56f3 4-5 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u5b9a\u7fa9\u3068\u683c\u7d0d\u65b9\u6cd5

        \u4e0a\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u57fa\u672c\u7684\u306a\u69cb\u6210\u8981\u7d20\u306f\u30ce\u30fc\u30c9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3059\u3002\u5404\u30ce\u30fc\u30c9\u306f2\u3064\u306e\u4e3b\u8981\u306a\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3067\u69cb\u6210\u3055\u308c\u3066\u3044\u307e\u3059\uff1a\u30ce\u30fc\u30c9\u306e\u300c\u5024\u300d\u3068\u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u300c\u53c2\u7167\u300d\u3067\u3059\u3002

        • \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u6700\u521d\u306e\u30ce\u30fc\u30c9\u306f\u300c\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u300d\u3001\u6700\u5f8c\u306e\u30ce\u30fc\u30c9\u306f\u300c\u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u300d\u3067\u3059\u3002
        • \u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u306f\u300cnull\u300d\u3092\u6307\u3057\u3001Java\u3067\u306fnull\u3001C++\u3067\u306fnullptr\u3001Python\u3067\u306fNone\u3068\u3057\u3066\u6307\u5b9a\u3055\u308c\u307e\u3059\u3002
        • C\u3001C++\u3001Go\u3001Rust\u306a\u3069\u306e\u30dd\u30a4\u30f3\u30bf\u3092\u30b5\u30dd\u30fc\u30c8\u3059\u308b\u8a00\u8a9e\u3067\u306f\u3001\u3053\u306e\u300c\u53c2\u7167\u300d\u306f\u901a\u5e38\u300c\u30dd\u30a4\u30f3\u30bf\u300d\u3068\u3057\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u304c\u793a\u3059\u3088\u3046\u306b\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306eListNode\u306f\u5024\u3092\u4fdd\u6301\u3059\u308b\u3060\u3051\u3067\u306a\u304f\u3001\u8ffd\u52a0\u306e\u53c2\u7167\uff08\u307e\u305f\u306f\u30dd\u30a4\u30f3\u30bf\uff09\u3082\u7dad\u6301\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u540c\u3058\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3059\u308b\u5834\u5408\u3001\u914d\u5217\u3088\u308a\u3082\u591a\u304f\u306e\u30e1\u30e2\u30ea\u7a7a\u9593\u3092\u5360\u6709\u3057\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin
        class ListNode:\n    \"\"\"\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val               # \u30ce\u30fc\u30c9\u5024\n        self.next: ListNode | None = None # \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u69cb\u9020\u4f53 */\nstruct ListNode {\n    int val;         // \u30ce\u30fc\u30c9\u5024\n    ListNode *next;  // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    ListNode(int x) : val(x), next(nullptr) {}  // \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\n};\n
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode {\n    int val;        // \u30ce\u30fc\u30c9\u5024\n    ListNode next;  // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    ListNode(int x) { val = x; }  // \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\n}\n
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode(int x) {  // \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\n    int val = x;         // \u30ce\u30fc\u30c9\u5024\n    ListNode? next;      // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n}\n
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u69cb\u9020\u4f53 */\ntype ListNode struct {\n    Val  int       // \u30ce\u30fc\u30c9\u5024\n    Next *ListNode // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n}\n\n// NewListNode \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3001\u65b0\u3057\u3044\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u4f5c\u6210\nfunc NewListNode(val int) *ListNode {\n    return &ListNode{\n        Val:  val,\n        Next: nil,\n    }\n}\n
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode {\n    var val: Int // \u30ce\u30fc\u30c9\u5024\n    var next: ListNode? // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n\n    init(x: Int) { // \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\n        val = x\n    }\n}\n
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode {\n    constructor(val, next) {\n        this.val = (val === undefined ? 0 : val);       // \u30ce\u30fc\u30c9\u5024\n        this.next = (next === undefined ? null : next); // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    }\n}\n
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode {\n    val: number;\n    next: ListNode | null;\n    constructor(val?: number, next?: ListNode | null) {\n        this.val = val === undefined ? 0 : val;        // \u30ce\u30fc\u30c9\u5024\n        this.next = next === undefined ? null : next;  // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    }\n}\n
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode {\n  int val; // \u30ce\u30fc\u30c9\u5024\n  ListNode? next; // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n  ListNode(this.val, [this.next]); // \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\n}\n
        use std::rc::Rc;\nuse std::cell::RefCell;\n/* \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\n#[derive(Debug)]\nstruct ListNode {\n    val: i32, // \u30ce\u30fc\u30c9\u5024\n    next: Option<Rc<RefCell<ListNode>>>, // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n}\n
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u69cb\u9020\u4f53 */\ntypedef struct ListNode {\n    int val;               // \u30ce\u30fc\u30c9\u5024\n    struct ListNode *next; // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n} ListNode;\n\n/* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\nListNode *newListNode(int val) {\n    ListNode *node;\n    node = (ListNode *) malloc(sizeof(ListNode));\n    node->val = val;\n    node->next = NULL;\n    return node;\n}\n
        \n
        "},{"location":"chapter_array_and_linkedlist/linked_list/#421","title":"4.2.1 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/linked_list/#1","title":"1. \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u521d\u671f\u5316","text":"

        \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u69cb\u7bc9\u306f2\u6bb5\u968e\u306e\u30d7\u30ed\u30bb\u30b9\u3067\u3059\uff1a\u307e\u305a\u5404\u30ce\u30fc\u30c9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u521d\u671f\u5316\u3057\u3001\u6b21\u306b\u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u30ea\u30f3\u30af\u3092\u5f62\u6210\u3057\u307e\u3059\u3002\u521d\u671f\u5316\u5f8c\u3001\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u304b\u3089next\u53c2\u7167\u3092\u305f\u3069\u3063\u3066\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3092\u9806\u6b21\u5de1\u56de\u3067\u304d\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin linked_list.py
        # \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316: 1 -> 3 -> 2 -> 5 -> 4\n# \u5404\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\nn0 = ListNode(1)\nn1 = ListNode(3)\nn2 = ListNode(2)\nn3 = ListNode(5)\nn4 = ListNode(4)\n# \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u3092\u69cb\u7bc9\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
        linked_list.cpp
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316: 1 -> 3 -> 2 -> 5 -> 4 */\n// \u5404\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\nListNode* n0 = new ListNode(1);\nListNode* n1 = new ListNode(3);\nListNode* n2 = new ListNode(2);\nListNode* n3 = new ListNode(5);\nListNode* n4 = new ListNode(4);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u3092\u69cb\u7bc9\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
        linked_list.java
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316: 1 -> 3 -> 2 -> 5 -> 4 */\n// \u5404\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\nListNode n0 = new ListNode(1);\nListNode n1 = new ListNode(3);\nListNode n2 = new ListNode(2);\nListNode n3 = new ListNode(5);\nListNode n4 = new ListNode(4);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u3092\u69cb\u7bc9\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
        linked_list.cs
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316: 1 -> 3 -> 2 -> 5 -> 4 */\n// \u5404\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\nListNode n0 = new(1);\nListNode n1 = new(3);\nListNode n2 = new(2);\nListNode n3 = new(5);\nListNode n4 = new(4);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u3092\u69cb\u7bc9\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
        linked_list.go
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316: 1 -> 3 -> 2 -> 5 -> 4 */\n// \u5404\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\nn0 := NewListNode(1)\nn1 := NewListNode(3)\nn2 := NewListNode(2)\nn3 := NewListNode(5)\nn4 := NewListNode(4)\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u3092\u69cb\u7bc9\nn0.Next = n1\nn1.Next = n2\nn2.Next = n3\nn3.Next = n4\n
        linked_list.swift
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316: 1 -> 3 -> 2 -> 5 -> 4 */\n// \u5404\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\nlet n0 = ListNode(x: 1)\nlet n1 = ListNode(x: 3)\nlet n2 = ListNode(x: 2)\nlet n3 = ListNode(x: 5)\nlet n4 = ListNode(x: 4)\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u3092\u69cb\u7bc9\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
        linked_list.js
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316: 1 -> 3 -> 2 -> 5 -> 4 */\n// \u5404\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u3092\u69cb\u7bc9\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
        linked_list.ts
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316: 1 -> 3 -> 2 -> 5 -> 4 */\n// \u5404\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u3092\u69cb\u7bc9\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
        linked_list.dart
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316: 1 -> 3 -> 2 -> 5 -> 4 */\n// \u5404\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\nListNode n0 = ListNode(1);\nListNode n1 = ListNode(3);\nListNode n2 = ListNode(2);\nListNode n3 = ListNode(5);\nListNode n4 = ListNode(4);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u3092\u69cb\u7bc9\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
        linked_list.rs
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316: 1 -> 3 -> 2 -> 5 -> 4 */\n// \u5404\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\nlet n0 = Rc::new(RefCell::new(ListNode { val: 1, next: None }));\nlet n1 = Rc::new(RefCell::new(ListNode { val: 3, next: None }));\nlet n2 = Rc::new(RefCell::new(ListNode { val: 2, next: None }));\nlet n3 = Rc::new(RefCell::new(ListNode { val: 5, next: None }));\nlet n4 = Rc::new(RefCell::new(ListNode { val: 4, next: None }));\n\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u3092\u69cb\u7bc9\nn0.borrow_mut().next = Some(n1.clone());\nn1.borrow_mut().next = Some(n2.clone());\nn2.borrow_mut().next = Some(n3.clone());\nn3.borrow_mut().next = Some(n4.clone());\n
        linked_list.c
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316: 1 -> 3 -> 2 -> 5 -> 4 */\n// \u5404\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\nListNode* n0 = newListNode(1);\nListNode* n1 = newListNode(3);\nListNode* n2 = newListNode(2);\nListNode* n3 = newListNode(5);\nListNode* n4 = newListNode(4);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u3092\u69cb\u7bc9\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
        linked_list.kt
        \n

        \u914d\u5217\u5168\u4f53\u306f1\u3064\u306e\u5909\u6570\u3067\u3059\u3002\u4f8b\u3048\u3070\u3001\u914d\u5217nums\u306b\u306fnums[0]\u3001nums[1]\u306a\u3069\u306e\u8981\u7d20\u304c\u542b\u307e\u308c\u307e\u3059\u304c\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u8907\u6570\u306e\u7570\u306a\u308b\u30ce\u30fc\u30c9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u69cb\u6210\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u901a\u5e38\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u305d\u306e\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u3067\u53c2\u7167\u3055\u308c\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u524d\u306e\u30b3\u30fc\u30c9\u30b9\u30cb\u30da\u30c3\u30c8\u306e\u9023\u7d50\u30ea\u30b9\u30c8\u306fn0\u3068\u3057\u3066\u53c2\u7167\u3055\u308c\u307e\u3059\u3002

        "},{"location":"chapter_array_and_linkedlist/linked_list/#2","title":"2. \u00a0 \u30ce\u30fc\u30c9\u306e\u633f\u5165","text":"

        \u9023\u7d50\u30ea\u30b9\u30c8\u306b\u30ce\u30fc\u30c9\u3092\u633f\u5165\u3059\u308b\u306e\u306f\u975e\u5e38\u306b\u7c21\u5358\u3067\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u96a3\u63a5\u3059\u308b2\u3064\u306e\u30ce\u30fc\u30c9n0\u3068n1\u306e\u9593\u306b\u65b0\u3057\u3044\u30ce\u30fc\u30c9P\u3092\u633f\u5165\u3059\u308b\u3053\u3068\u3092\u76ee\u6307\u3059\u3068\u3057\u307e\u3059\u3002\u3053\u308c\u306f2\u3064\u306e\u30ce\u30fc\u30c9\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u5909\u66f4\u3059\u308b\u3060\u3051\u3067\u5b9f\u73fe\u3067\u304d\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u3067\u3059\u3002

        \u6bd4\u8f03\u3059\u308b\u3068\u3001\u914d\u5217\u306b\u8981\u7d20\u3092\u633f\u5165\u3059\u308b\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3042\u308a\u3001\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u6271\u3046\u5834\u5408\u306b\u306f\u52b9\u7387\u304c\u60aa\u304f\u306a\u308a\u307e\u3059\u3002

        \u56f3 4-6 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u633f\u5165\u306e\u4f8b

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
        def insert(n0: ListNode, P: ListNode):\n    \"\"\"\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30ce\u30fc\u30c9 n0 \u306e\u5f8c\u306b\u30ce\u30fc\u30c9 P \u3092\u633f\u5165\"\"\"\n    n1 = n0.next\n    P.next = n1\n    n0.next = P\n
        linked_list.cpp
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30ce\u30fc\u30c9n0\u306e\u5f8c\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165 */\nvoid insert(ListNode *n0, ListNode *P) {\n    ListNode *n1 = n0->next;\n    P->next = n1;\n    n0->next = P;\n}\n
        linked_list.java
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3067\u30ce\u30fc\u30c9 n0 \u306e\u5f8c\u306b\u30ce\u30fc\u30c9 P \u3092\u633f\u5165 */\nvoid insert(ListNode n0, ListNode P) {\n    ListNode n1 = n0.next;\n    P.next = n1;\n    n0.next = P;\n}\n
        linked_list.cs
        [class]{linked_list}-[func]{Insert}\n
        linked_list.go
        [class]{}-[func]{insertNode}\n
        linked_list.swift
        [class]{}-[func]{insert}\n
        linked_list.js
        [class]{}-[func]{insert}\n
        linked_list.ts
        [class]{}-[func]{insert}\n
        linked_list.dart
        [class]{}-[func]{insert}\n
        linked_list.rs
        [class]{}-[func]{insert}\n
        linked_list.c
        [class]{}-[func]{insert}\n
        linked_list.kt
        [class]{}-[func]{insert}\n
        linked_list.rb
        [class]{}-[func]{insert}\n
        "},{"location":"chapter_array_and_linkedlist/linked_list/#3","title":"3. \u00a0 \u30ce\u30fc\u30c9\u306e\u524a\u9664","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u304b\u3089\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3059\u308b\u3053\u3068\u3082\u975e\u5e38\u306b\u7c21\u5358\u3067\u30011\u3064\u306e\u30ce\u30fc\u30c9\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u5909\u66f4\u3059\u308b\u3060\u3051\u3067\u3059\u3002

        \u91cd\u8981\u306a\u70b9\u306f\u3001\u30ce\u30fc\u30c9P\u304c\u524a\u9664\u3055\u308c\u305f\u5f8c\u3082n1\u3092\u6307\u3057\u7d9a\u3051\u3066\u3044\u308b\u3053\u3068\u3067\u3059\u304c\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u5de1\u56de\u4e2d\u306b\u306f\u30a2\u30af\u30bb\u30b9\u3067\u304d\u306a\u304f\u306a\u308b\u3053\u3068\u3067\u3059\u3002\u3053\u308c\u306f\u4e8b\u5b9f\u4e0a\u3001P\u304c\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u4e00\u90e8\u3067\u306f\u306a\u304f\u306a\u3063\u305f\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

        \u56f3 4-7 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u306e\u524a\u9664

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
        def remove(n0: ListNode):\n    \"\"\"\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30ce\u30fc\u30c9 n0 \u306e\u5f8c\u306e\u6700\u521d\u306e\u30ce\u30fc\u30c9\u3092\u524a\u9664\"\"\"\n    if not n0.next:\n        return\n    # n0 -> P -> n1\n    P = n0.next\n    n1 = P.next\n    n0.next = n1\n
        linked_list.cpp
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30ce\u30fc\u30c9n0\u306e\u5f8c\u306e\u6700\u521d\u306e\u30ce\u30fc\u30c9\u3092\u524a\u9664 */\nvoid remove(ListNode *n0) {\n    if (n0->next == nullptr)\n        return;\n    // n0 -> P -> n1\n    ListNode *P = n0->next;\n    ListNode *n1 = P->next;\n    n0->next = n1;\n    // \u30e1\u30e2\u30ea\u3092\u89e3\u653e\n    delete P;\n}\n
        linked_list.java
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3067\u30ce\u30fc\u30c9 n0 \u306e\u5f8c\u306e\u6700\u521d\u306e\u30ce\u30fc\u30c9\u3092\u524a\u9664 */\nvoid remove(ListNode n0) {\n    if (n0.next == null)\n        return;\n    // n0 -> P -> n1\n    ListNode P = n0.next;\n    ListNode n1 = P.next;\n    n0.next = n1;\n}\n
        linked_list.cs
        [class]{linked_list}-[func]{Remove}\n
        linked_list.go
        [class]{}-[func]{removeItem}\n
        linked_list.swift
        [class]{}-[func]{remove}\n
        linked_list.js
        [class]{}-[func]{remove}\n
        linked_list.ts
        [class]{}-[func]{remove}\n
        linked_list.dart
        [class]{}-[func]{remove}\n
        linked_list.rs
        [class]{}-[func]{remove}\n
        linked_list.c
        [class]{}-[func]{removeItem}\n
        linked_list.kt
        [class]{}-[func]{remove}\n
        linked_list.rb
        [class]{}-[func]{remove}\n
        "},{"location":"chapter_array_and_linkedlist/linked_list/#4","title":"4. \u00a0 \u30ce\u30fc\u30c9\u3078\u306e\u30a2\u30af\u30bb\u30b9","text":"

        \u9023\u7d50\u30ea\u30b9\u30c8\u3067\u306e\u30ce\u30fc\u30c9\u3078\u306e\u30a2\u30af\u30bb\u30b9\u306f\u52b9\u7387\u304c\u60aa\u3044\u3067\u3059\u3002\u524d\u8ff0\u3057\u305f\u3088\u3046\u306b\u3001\u914d\u5217\u306e\u4efb\u610f\u306e\u8981\u7d20\u306b\u306f\\(O(1)\\)\u6642\u9593\u3067\u30a2\u30af\u30bb\u30b9\u3067\u304d\u307e\u3059\u3002\u5bfe\u7167\u7684\u306b\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u306f\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u304b\u3089\u958b\u59cb\u3057\u3066\u76ee\u7684\u306e\u30ce\u30fc\u30c9\u304c\u898b\u3064\u304b\u308b\u307e\u3067\u9806\u6b21\u30ce\u30fc\u30c9\u3092\u5de1\u56de\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\\(i\\)\u756a\u76ee\u306e\u30ce\u30fc\u30c9\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u306b\u306f\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\\(i - 1\\)\u500b\u306e\u30ce\u30fc\u30c9\u3092\u53cd\u5fa9\u51e6\u7406\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u306b\u306a\u308a\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
        def access(head: ListNode, index: int) -> ListNode | None:\n    \"\"\"\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 index \u306e\u30ce\u30fc\u30c9\u306b\u30a2\u30af\u30bb\u30b9\"\"\"\n    for _ in range(index):\n        if not head:\n            return None\n        head = head.next\n    return head\n
        linked_list.cpp
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u306e`index`\u756a\u76ee\u306e\u30ce\u30fc\u30c9\u306b\u30a2\u30af\u30bb\u30b9 */\nListNode *access(ListNode *head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == nullptr)\n            return nullptr;\n        head = head->next;\n    }\n    return head;\n}\n
        linked_list.java
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u306e `index` \u306e\u30ce\u30fc\u30c9\u306b\u30a2\u30af\u30bb\u30b9 */\nListNode access(ListNode head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == null)\n            return null;\n        head = head.next;\n    }\n    return head;\n}\n
        linked_list.cs
        [class]{linked_list}-[func]{Access}\n
        linked_list.go
        [class]{}-[func]{access}\n
        linked_list.swift
        [class]{}-[func]{access}\n
        linked_list.js
        [class]{}-[func]{access}\n
        linked_list.ts
        [class]{}-[func]{access}\n
        linked_list.dart
        [class]{}-[func]{access}\n
        linked_list.rs
        [class]{}-[func]{access}\n
        linked_list.c
        [class]{}-[func]{access}\n
        linked_list.kt
        [class]{}-[func]{access}\n
        linked_list.rb
        [class]{}-[func]{access}\n
        "},{"location":"chapter_array_and_linkedlist/linked_list/#5","title":"5. \u00a0 \u30ce\u30fc\u30c9\u306e\u691c\u7d22","text":"

        \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u5de1\u56de\u3057\u3066\u3001\u5024\u304ctarget\u306b\u4e00\u81f4\u3059\u308b\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u5185\u3067\u306e\u305d\u306e\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u51fa\u529b\u3057\u307e\u3059\u3002\u3053\u306e\u624b\u9806\u3082\u7dda\u5f62\u691c\u7d22\u306e\u4f8b\u3067\u3059\u3002\u5bfe\u5fdc\u3059\u308b\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
        def find(head: ListNode, target: int) -> int:\n    \"\"\"\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u5024 target \u3092\u6301\u3064\u6700\u521d\u306e\u30ce\u30fc\u30c9\u3092\u691c\u7d22\"\"\"\n    index = 0\n    while head:\n        if head.val == target:\n            return index\n        head = head.next\n        index += 1\n    return -1\n
        linked_list.cpp
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3067\u5024\u304ctarget\u306e\u6700\u521d\u306e\u30ce\u30fc\u30c9\u3092\u691c\u7d22 */\nint find(ListNode *head, int target) {\n    int index = 0;\n    while (head != nullptr) {\n        if (head->val == target)\n            return index;\n        head = head->next;\n        index++;\n    }\n    return -1;\n}\n
        linked_list.java
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3067\u5024 target \u3092\u6301\u3064\u6700\u521d\u306e\u30ce\u30fc\u30c9\u3092\u691c\u7d22 */\nint find(ListNode head, int target) {\n    int index = 0;\n    while (head != null) {\n        if (head.val == target)\n            return index;\n        head = head.next;\n        index++;\n    }\n    return -1;\n}\n
        linked_list.cs
        [class]{linked_list}-[func]{Find}\n
        linked_list.go
        [class]{}-[func]{findNode}\n
        linked_list.swift
        [class]{}-[func]{find}\n
        linked_list.js
        [class]{}-[func]{find}\n
        linked_list.ts
        [class]{}-[func]{find}\n
        linked_list.dart
        [class]{}-[func]{find}\n
        linked_list.rs
        [class]{}-[func]{find}\n
        linked_list.c
        [class]{}-[func]{find}\n
        linked_list.kt
        [class]{}-[func]{find}\n
        linked_list.rb
        [class]{}-[func]{find}\n
        "},{"location":"chapter_array_and_linkedlist/linked_list/#422-vs","title":"4.2.2 \u00a0 \u914d\u5217 vs. \u9023\u7d50\u30ea\u30b9\u30c8","text":"

        \u4e0b\u8868\u306f\u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u7279\u6027\u3092\u307e\u3068\u3081\u3001\u69d8\u3005\u306a\u64cd\u4f5c\u306b\u304a\u3051\u308b\u52b9\u7387\u3082\u6bd4\u8f03\u3057\u3066\u3044\u307e\u3059\u3002\u305d\u308c\u305e\u308c\u304c\u5bfe\u7167\u7684\u306a\u683c\u7d0d\u6226\u7565\u3092\u4f7f\u7528\u3059\u308b\u305f\u3081\u3001\u305d\u308c\u305e\u308c\u306e\u7279\u6027\u3068\u64cd\u4f5c\u52b9\u7387\u306f\u660e\u78ba\u306b\u5bfe\u6bd4\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        \u8868 4-1 \u00a0 \u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u52b9\u7387\u6bd4\u8f03

        \u914d\u5217 \u9023\u7d50\u30ea\u30b9\u30c8 \u683c\u7d0d\u65b9\u5f0f \u9023\u7d9a\u30e1\u30e2\u30ea\u7a7a\u9593 \u5206\u6563\u30e1\u30e2\u30ea\u7a7a\u9593 \u5bb9\u91cf\u62e1\u5f35 \u56fa\u5b9a\u9577 \u67d4\u8edf\u306a\u62e1\u5f35 \u30e1\u30e2\u30ea\u52b9\u7387 \u8981\u7d20\u3042\u305f\u308a\u306e\u30e1\u30e2\u30ea\u5c11\u3001\u6f5c\u5728\u7684\u306a\u7a7a\u9593\u306e\u7121\u99c4 \u8981\u7d20\u3042\u305f\u308a\u306e\u30e1\u30e2\u30ea\u591a \u8981\u7d20\u3078\u306e\u30a2\u30af\u30bb\u30b9 \\(O(1)\\) \\(O(n)\\) \u8981\u7d20\u306e\u8ffd\u52a0 \\(O(n)\\) \\(O(1)\\) \u8981\u7d20\u306e\u524a\u9664 \\(O(n)\\) \\(O(1)\\)"},{"location":"chapter_array_and_linkedlist/linked_list/#423","title":"4.2.3 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u4e00\u822c\u7684\u306a\u7a2e\u985e","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u306f3\u3064\u306e\u4e00\u822c\u7684\u306a\u7a2e\u985e\u304c\u3042\u308a\u307e\u3059\u3002

        • \u5358\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\uff1a\u3053\u308c\u306f\u524d\u8ff0\u3057\u305f\u6a19\u6e96\u7684\u306a\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u3059\u3002\u5358\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30ce\u30fc\u30c9\u306b\u306f\u5024\u3068\u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\u304c\u542b\u307e\u308c\u307e\u3059\u3002\u6700\u521d\u306e\u30ce\u30fc\u30c9\u306f\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u3068\u547c\u3070\u308c\u3001null\uff08None\uff09\u3092\u6307\u3059\u6700\u5f8c\u306e\u30ce\u30fc\u30c9\u306f\u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u3067\u3059\u3002
        • \u5faa\u74b0\u9023\u7d50\u30ea\u30b9\u30c8\uff1a\u3053\u308c\u306f\u5358\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u304c\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u3092\u6307\u3057\u3066\u30eb\u30fc\u30d7\u3092\u4f5c\u308b\u3053\u3068\u3067\u5f62\u6210\u3055\u308c\u307e\u3059\u3002\u5faa\u74b0\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u306f\u3001\u4efb\u610f\u306e\u30ce\u30fc\u30c9\u304c\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u3068\u3057\u3066\u6a5f\u80fd\u3067\u304d\u307e\u3059\u3002
        • \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\uff1a\u5358\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u3068\u306f\u5bfe\u7167\u7684\u306b\u3001\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306f2\u3064\u306e\u65b9\u5411\u3067\u53c2\u7167\u3092\u7dad\u6301\u3057\u307e\u3059\u3002\u5404\u30ce\u30fc\u30c9\u306b\u306f\u5f8c\u7d9a\u8005\uff08\u6b21\u306e\u30ce\u30fc\u30c9\uff09\u3068\u524d\u4efb\u8005\uff08\u524d\u306e\u30ce\u30fc\u30c9\uff09\u306e\u4e21\u65b9\u3078\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u304c\u542b\u307e\u308c\u307e\u3059\u3002\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u3069\u3061\u3089\u306e\u65b9\u5411\u306b\u3082\u5de1\u56de\u3067\u304d\u308b\u3088\u308a\u591a\u304f\u306e\u67d4\u8edf\u6027\u3092\u63d0\u4f9b\u3057\u307e\u3059\u304c\u3001\u3088\u308a\u591a\u304f\u306e\u30e1\u30e2\u30ea\u7a7a\u9593\u3082\u6d88\u8cbb\u3057\u307e\u3059\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin
        class ListNode:\n    \"\"\"\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val                # \u30ce\u30fc\u30c9\u5024\n        self.next: ListNode | None = None  # \u5f8c\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n        self.prev: ListNode | None = None  # \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n
        /* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u69cb\u9020\u4f53 */\nstruct ListNode {\n    int val;         // \u30ce\u30fc\u30c9\u5024\n    ListNode *next;  // \u5f8c\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    ListNode *prev;  // \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    ListNode(int x) : val(x), next(nullptr), prev(nullptr) {}  // \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\n};\n
        /* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode {\n    int val;        // \u30ce\u30fc\u30c9\u5024\n    ListNode next;  // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    ListNode prev;  // \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    ListNode(int x) { val = x; }  // \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\n}\n
        /* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode(int x) {  // \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\n    int val = x;    // \u30ce\u30fc\u30c9\u5024\n    ListNode next;  // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    ListNode prev;  // \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n}\n
        /* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u69cb\u9020\u4f53 */\ntype DoublyListNode struct {\n    Val  int             // \u30ce\u30fc\u30c9\u5024\n    Next *DoublyListNode // \u5f8c\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    Prev *DoublyListNode // \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n}\n\n// NewDoublyListNode \u521d\u671f\u5316\nfunc NewDoublyListNode(val int) *DoublyListNode {\n    return &DoublyListNode{\n        Val:  val,\n        Next: nil,\n        Prev: nil,\n    }\n}\n
        /* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode {\n    var val: Int // \u30ce\u30fc\u30c9\u5024\n    var next: ListNode? // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    var prev: ListNode? // \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n\n    init(x: Int) { // \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\n        val = x\n    }\n}\n
        /* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode {\n    constructor(val, next, prev) {\n        this.val = val  ===  undefined ? 0 : val;        // \u30ce\u30fc\u30c9\u5024\n        this.next = next  ===  undefined ? null : next;  // \u5f8c\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n        this.prev = prev  ===  undefined ? null : prev;  // \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    }\n}\n
        /* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode {\n    val: number;\n    next: ListNode | null;\n    prev: ListNode | null;\n    constructor(val?: number, next?: ListNode | null, prev?: ListNode | null) {\n        this.val = val  ===  undefined ? 0 : val;        // \u30ce\u30fc\u30c9\u5024\n        this.next = next  ===  undefined ? null : next;  // \u5f8c\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n        this.prev = prev  ===  undefined ? null : prev;  // \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    }\n}\n
        /* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode {\n    int val;        // \u30ce\u30fc\u30c9\u5024\n    ListNode next;  // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    ListNode prev;  // \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    ListNode(this.val, [this.next, this.prev]);  // \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\n}\n
        use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u578b */\n#[derive(Debug)]\nstruct ListNode {\n    val: i32, // \u30ce\u30fc\u30c9\u5024\n    next: Option<Rc<RefCell<ListNode>>>, // \u5f8c\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    prev: Option<Rc<RefCell<ListNode>>>, // \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n}\n\n/* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\nimpl ListNode {\n    fn new(val: i32) -> Self {\n        ListNode {\n            val,\n            next: None,\n            prev: None,\n        }\n    }\n}\n
        /* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u69cb\u9020\u4f53 */\ntypedef struct ListNode {\n    int val;               // \u30ce\u30fc\u30c9\u5024\n    struct ListNode *next; // \u5f8c\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    struct ListNode *prev; // \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n} ListNode;\n\n/* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\nListNode *newListNode(int val) {\n    ListNode *node, *next;\n    node = (ListNode *) malloc(sizeof(ListNode));\n    node->val = val;\n    node->next = NULL;\n    node->prev = NULL;\n    return node;\n}\n
        \n

        \u56f3 4-8 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u4e00\u822c\u7684\u306a\u7a2e\u985e

        "},{"location":"chapter_array_and_linkedlist/linked_list/#424","title":"4.2.4 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u5178\u578b\u7684\u306a\u5fdc\u7528","text":"

        \u5358\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u3001\u30b9\u30bf\u30c3\u30af\u3001\u30ad\u30e5\u30fc\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3001\u30b0\u30e9\u30d5\u306e\u5b9f\u88c5\u306b\u3088\u304f\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002

        • \u30b9\u30bf\u30c3\u30af\u3068\u30ad\u30e5\u30fc\uff1a\u5358\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u3001\u633f\u5165\u3068\u524a\u9664\u304c\u540c\u3058\u7aef\u3067\u884c\u308f\u308c\u308b\u5834\u5408\u3001\u30b9\u30bf\u30c3\u30af\uff08\u5f8c\u5165\u5148\u51fa\uff09\u306e\u3088\u3046\u306b\u52d5\u4f5c\u3057\u307e\u3059\u3002\u9006\u306b\u3001\u633f\u5165\u304c\u4e00\u65b9\u306e\u7aef\u3067\u3001\u524a\u9664\u304c\u3082\u3046\u4e00\u65b9\u306e\u7aef\u3067\u884c\u308f\u308c\u308b\u5834\u5408\u3001\u30ad\u30e5\u30fc\uff08\u5148\u5165\u5148\u51fa\uff09\u306e\u3088\u3046\u306b\u6a5f\u80fd\u3057\u307e\u3059\u3002
        • \u30cf\u30c3\u30b7\u30e5\u8868\uff1a\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u89e3\u6c7a\u3059\u308b\u4eba\u6c17\u306e\u65b9\u6cd5\u3067\u3042\u308b\u9023\u9396\u6cd5\u3067\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u3053\u3053\u3067\u306f\u3001\u3059\u3079\u3066\u306e\u885d\u7a81\u3057\u305f\u8981\u7d20\u304c\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u30b0\u30eb\u30fc\u30d7\u5316\u3055\u308c\u307e\u3059\u3002
        • \u30b0\u30e9\u30d5\uff1a\u30b0\u30e9\u30d5\u8868\u73fe\u306e\u6a19\u6e96\u7684\u306a\u65b9\u6cd5\u3067\u3042\u308b\u96a3\u63a5\u30ea\u30b9\u30c8\u306f\u3001\u5404\u30b0\u30e9\u30d5\u9802\u70b9\u3092\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u95a2\u9023\u4ed8\u3051\u307e\u3059\u3002\u3053\u306e\u30ea\u30b9\u30c8\u306b\u306f\u3001\u5bfe\u5fdc\u3059\u308b\u9802\u70b9\u306b\u63a5\u7d9a\u3055\u308c\u305f\u9802\u70b9\u3092\u8868\u3059\u8981\u7d20\u304c\u542b\u307e\u308c\u307e\u3059\u3002

        \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u3001\u524d\u5f8c\u306e\u8981\u7d20\u3078\u306e\u9ad8\u901f\u30a2\u30af\u30bb\u30b9\u304c\u5fc5\u8981\u306a\u30b7\u30ca\u30ea\u30aa\u306b\u6700\u9069\u3067\u3059\u3002

        • \u9ad8\u5ea6\u306a\u30c7\u30fc\u30bf\u69cb\u9020\uff1a\u8d64\u9ed2\u6728\u3084B\u6728\u306a\u3069\u306e\u69cb\u9020\u3067\u306f\u3001\u30ce\u30fc\u30c9\u306e\u89aa\u3078\u306e\u30a2\u30af\u30bb\u30b9\u304c\u91cd\u8981\u3067\u3059\u3002\u3053\u308c\u306f\u5404\u30ce\u30fc\u30c9\u306b\u89aa\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\u3092\u7d44\u307f\u8fbc\u3080\u3053\u3068\u3067\u5b9f\u73fe\u3055\u308c\u3001\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u4f3c\u3066\u3044\u307e\u3059\u3002
        • \u30d6\u30e9\u30a6\u30b6\u5c65\u6b74\uff1aWeb\u30d6\u30e9\u30a6\u30b6\u3067\u306f\u3001\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u3088\u308a\u3001\u30e6\u30fc\u30b6\u30fc\u304c\u524d\u9032\u307e\u305f\u306f\u5f8c\u9000\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3057\u305f\u3068\u304d\u306e\u8a2a\u554f\u30da\u30fc\u30b8\u306e\u5c65\u6b74\u30ca\u30d3\u30b2\u30fc\u30b7\u30e7\u30f3\u304c\u5bb9\u6613\u306b\u306a\u308a\u307e\u3059\u3002
        • LRU\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u3001\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\uff08LRU\uff09\u30ad\u30e3\u30c3\u30b7\u30e5\u524a\u9664\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u9069\u3057\u3066\u304a\u308a\u3001\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u30c7\u30fc\u30bf\u306e\u8fc5\u901f\u306a\u8b58\u5225\u3068\u3001\u9ad8\u901f\u306a\u30ce\u30fc\u30c9\u8ffd\u52a0\u30fb\u524a\u9664\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002

        \u5faa\u74b0\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u3001\u30aa\u30da\u30ec\u30fc\u30c6\u30a3\u30f3\u30b0\u30b7\u30b9\u30c6\u30e0\u3067\u306e\u30ea\u30bd\u30fc\u30b9\u30b9\u30b1\u30b8\u30e5\u30fc\u30ea\u30f3\u30b0\u306a\u3069\u3001\u5468\u671f\u7684\u306a\u64cd\u4f5c\u304c\u5fc5\u8981\u306a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306b\u6700\u9069\u3067\u3059\u3002

        • \u30e9\u30a6\u30f3\u30c9\u30ed\u30d3\u30f3\u30b9\u30b1\u30b8\u30e5\u30fc\u30ea\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u30aa\u30da\u30ec\u30fc\u30c6\u30a3\u30f3\u30b0\u30b7\u30b9\u30c6\u30e0\u3067\u306f\u3001\u30e9\u30a6\u30f3\u30c9\u30ed\u30d3\u30f3\u30b9\u30b1\u30b8\u30e5\u30fc\u30ea\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u4e00\u822c\u7684\u306aCPU\u30b9\u30b1\u30b8\u30e5\u30fc\u30ea\u30f3\u30b0\u65b9\u6cd5\u3067\u3042\u308a\u3001\u30d7\u30ed\u30bb\u30b9\u306e\u30b0\u30eb\u30fc\u30d7\u3092\u5faa\u74b0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u5404\u30d7\u30ed\u30bb\u30b9\u306b\u306f\u30bf\u30a4\u30e0\u30b9\u30e9\u30a4\u30b9\u304c\u5272\u308a\u5f53\u3066\u3089\u308c\u3001\u671f\u9650\u5207\u308c\u306b\u306a\u308b\u3068CPU\u306f\u6b21\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u56de\u8ee2\u3057\u307e\u3059\u3002\u3053\u306e\u5faa\u74b0\u64cd\u4f5c\u306f\u5faa\u74b0\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u52b9\u7387\u7684\u306b\u5b9f\u73fe\u3067\u304d\u3001\u3059\u3079\u3066\u306e\u30d7\u30ed\u30bb\u30b9\u9593\u3067\u516c\u5e73\u304b\u3064\u6642\u5206\u5272\u30b7\u30b9\u30c6\u30e0\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002
        • \u30c7\u30fc\u30bf\u30d0\u30c3\u30d5\u30a1\uff1a\u5faa\u74b0\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u3001\u30aa\u30fc\u30c7\u30a3\u30aa\u3084\u30d3\u30c7\u30aa\u30d7\u30ec\u30fc\u30e4\u30fc\u306a\u3069\u306e\u30c7\u30fc\u30bf\u30d0\u30c3\u30d5\u30a1\u3067\u3082\u4f7f\u7528\u3055\u308c\u3001\u30c7\u30fc\u30bf\u30b9\u30c8\u30ea\u30fc\u30e0\u304c\u8907\u6570\u306e\u30d0\u30c3\u30d5\u30a1\u30d6\u30ed\u30c3\u30af\u306b\u5206\u5272\u3055\u308c\u3001\u30b7\u30fc\u30e0\u30ec\u30b9\u306a\u518d\u751f\u306e\u305f\u3081\u306b\u5faa\u74b0\u65b9\u5f0f\u3067\u914d\u7f6e\u3055\u308c\u307e\u3059\u3002
        "},{"location":"chapter_array_and_linkedlist/list/","title":"4.3 \u00a0 \u30ea\u30b9\u30c8","text":"

        \u30ea\u30b9\u30c8\u306f\u3001\u8981\u7d20\u3078\u306e\u30a2\u30af\u30bb\u30b9\u3001\u5909\u66f4\u3001\u8ffd\u52a0\u3001\u524a\u9664\u3001\u8d70\u67fb\u306a\u3069\u306e\u64cd\u4f5c\u3092\u30b5\u30dd\u30fc\u30c8\u3059\u308b\u3001\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u8981\u7d20\u306e\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3092\u8868\u3059\u62bd\u8c61\u7684\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u6982\u5ff5\u3067\u3042\u308a\u3001\u30e6\u30fc\u30b6\u30fc\u304c\u5bb9\u91cf\u5236\u9650\u3092\u8003\u616e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u305b\u3093\u3002\u30ea\u30b9\u30c8\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u307e\u305f\u306f\u914d\u5217\u306b\u57fa\u3065\u3044\u3066\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002

        • \u9023\u7d50\u30ea\u30b9\u30c8\u306f\u672c\u8cea\u7684\u306b\u30ea\u30b9\u30c8\u3068\u3057\u3066\u6a5f\u80fd\u3057\u3001\u8981\u7d20\u306e\u8ffd\u52a0\u3001\u524a\u9664\u3001\u691c\u7d22\u3001\u5909\u66f4\u306e\u64cd\u4f5c\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3001\u30b5\u30a4\u30ba\u3092\u52d5\u7684\u306b\u8abf\u6574\u3059\u308b\u67d4\u8edf\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        • \u914d\u5217\u3082\u3053\u308c\u3089\u306e\u64cd\u4f5c\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u307e\u3059\u304c\u3001\u9577\u3055\u304c\u4e0d\u5909\u3067\u3042\u308b\u305f\u3081\u3001\u9577\u3055\u5236\u9650\u306e\u3042\u308b\u30ea\u30b9\u30c8\u3068\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u914d\u5217\u3092\u4f7f\u7528\u3057\u3066\u30ea\u30b9\u30c8\u3092\u5b9f\u88c5\u3059\u308b\u5834\u5408\u3001\u9577\u3055\u306e\u4e0d\u5909\u6027\u306b\u3088\u308a\u30ea\u30b9\u30c8\u306e\u5b9f\u7528\u6027\u304c\u4f4e\u4e0b\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u4e8b\u524d\u306b\u683c\u7d0d\u3059\u308b\u30c7\u30fc\u30bf\u91cf\u3092\u4e88\u6e2c\u3059\u308b\u3053\u3068\u304c\u56f0\u96e3\u306a\u5834\u5408\u304c\u591a\u304f\u3001\u9069\u5207\u306a\u30ea\u30b9\u30c8\u9577\u3092\u9078\u629e\u3059\u308b\u3053\u3068\u304c\u56f0\u96e3\u3067\u3042\u308b\u305f\u3081\u3067\u3059\u3002\u9577\u3055\u304c\u5c0f\u3055\u3059\u304e\u308b\u3068\u8981\u4ef6\u3092\u6e80\u305f\u3055\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u5927\u304d\u3059\u304e\u308b\u3068\u30e1\u30e2\u30ea\u7a7a\u9593\u3092\u7121\u99c4\u306b\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        \u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3001\u52d5\u7684\u914d\u5217\u3092\u4f7f\u7528\u3057\u3066\u30ea\u30b9\u30c8\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306f\u914d\u5217\u306e\u5229\u70b9\u3092\u7d99\u627f\u3057\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c\u4e2d\u306b\u52d5\u7684\u306b\u62e1\u5f35\u3067\u304d\u307e\u3059\u3002

        \u5b9f\u969b\u3001\u591a\u304f\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u52d5\u7684\u914d\u5217\u3092\u4f7f\u7528\u3057\u3066\u30ea\u30b9\u30c8\u3092\u5b9f\u88c5\u3057\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001Python\u306elist\u3001Java\u306eArrayList\u3001C++\u306evector\u3001C#\u306eList\u306a\u3069\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u8b70\u8ad6\u3067\u306f\u3001\u300c\u30ea\u30b9\u30c8\u300d\u3068\u300c\u52d5\u7684\u914d\u5217\u300d\u3092\u540c\u7fa9\u306e\u6982\u5ff5\u3068\u3057\u3066\u6271\u3044\u307e\u3059\u3002

        "},{"location":"chapter_array_and_linkedlist/list/#431","title":"4.3.1 \u00a0 \u30ea\u30b9\u30c8\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/list/#1","title":"1. \u00a0 \u30ea\u30b9\u30c8\u306e\u521d\u671f\u5316","text":"

        \u901a\u5e38\u3001\u300c\u521d\u671f\u5024\u306a\u3057\u300d\u3068\u300c\u521d\u671f\u5024\u3042\u308a\u300d\u306e2\u3064\u306e\u521d\u671f\u5316\u65b9\u6cd5\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin list.py
        # \u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316\n# \u521d\u671f\u5024\u306a\u3057\nnums1: list[int] = []\n# \u521d\u671f\u5024\u3042\u308a\nnums: list[int] = [1, 3, 2, 5, 4]\n
        list.cpp
        /* \u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316 */\n// \u6ce8\u610f: C++\u3067\u306f\u3001vector\u304c\u3053\u3053\u3067\u8aac\u660e\u3055\u308c\u3066\u3044\u308bnums\u306b\u76f8\u5f53\u3057\u307e\u3059\n// \u521d\u671f\u5024\u306a\u3057\nvector<int> nums1;\n// \u521d\u671f\u5024\u3042\u308a\nvector<int> nums = { 1, 3, 2, 5, 4 };\n
        list.java
        /* \u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316 */\n// \u521d\u671f\u5024\u306a\u3057\nList<Integer> nums1 = new ArrayList<>();\n// \u521d\u671f\u5024\u3042\u308a\uff08\u8981\u7d20\u578b\u306fint[]\u306e\u30e9\u30c3\u30d1\u30fc\u30af\u30e9\u30b9Integer[]\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff09\nInteger[] numbers = new Integer[] { 1, 3, 2, 5, 4 };\nList<Integer> nums = new ArrayList<>(Arrays.asList(numbers));\n
        list.cs
        /* \u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316 */\n// \u521d\u671f\u5024\u306a\u3057\nList<int> nums1 = [];\n// \u521d\u671f\u5024\u3042\u308a\nint[] numbers = [1, 3, 2, 5, 4];\nList<int> nums = [.. numbers];\n
        list_test.go
        /* \u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316 */\n// \u521d\u671f\u5024\u306a\u3057\nnums1 := []int{}\n// \u521d\u671f\u5024\u3042\u308a\nnums := []int{1, 3, 2, 5, 4}\n
        list.swift
        /* \u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316 */\n// \u521d\u671f\u5024\u306a\u3057\nlet nums1: [Int] = []\n// \u521d\u671f\u5024\u3042\u308a\nvar nums = [1, 3, 2, 5, 4]\n
        list.js
        /* \u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316 */\n// \u521d\u671f\u5024\u306a\u3057\nconst nums1 = [];\n// \u521d\u671f\u5024\u3042\u308a\nconst nums = [1, 3, 2, 5, 4];\n
        list.ts
        /* \u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316 */\n// \u521d\u671f\u5024\u306a\u3057\nconst nums1: number[] = [];\n// \u521d\u671f\u5024\u3042\u308a\nconst nums: number[] = [1, 3, 2, 5, 4];\n
        list.dart
        /* \u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316 */\n// \u521d\u671f\u5024\u306a\u3057\nList<int> nums1 = [];\n// \u521d\u671f\u5024\u3042\u308a\nList<int> nums = [1, 3, 2, 5, 4];\n
        list.rs
        /* \u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316 */\n// \u521d\u671f\u5024\u306a\u3057\nlet nums1: Vec<i32> = Vec::new();\n// \u521d\u671f\u5024\u3042\u308a\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
        list.c
        // C\u306f\u7d44\u307f\u8fbc\u307f\u306e\u52d5\u7684\u914d\u5217\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        list.kt
        \n
        "},{"location":"chapter_array_and_linkedlist/list/#2","title":"2. \u00a0 \u8981\u7d20\u3078\u306e\u30a2\u30af\u30bb\u30b9","text":"

        \u30ea\u30b9\u30c8\u306f\u672c\u8cea\u7684\u306b\u914d\u5217\u3067\u3042\u308b\u305f\u3081\u3001\\(O(1)\\)\u6642\u9593\u3067\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\u3057\u66f4\u65b0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin list.py
        # \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\nnum: int = nums[1]  # \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\n\n# \u8981\u7d20\u3092\u66f4\u65b0\nnums[1] = 0    # \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u30920\u306b\u66f4\u65b0\n
        list.cpp
        /* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint num = nums[1];  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\n\n/* \u8981\u7d20\u3092\u66f4\u65b0 */\nnums[1] = 0;  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u30920\u306b\u66f4\u65b0\n
        list.java
        /* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint num = nums.get(1);  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\n\n/* \u8981\u7d20\u3092\u66f4\u65b0 */\nnums.set(1, 0);  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u30920\u306b\u66f4\u65b0\n
        list.cs
        /* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint num = nums[1];  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\n\n/* \u8981\u7d20\u3092\u66f4\u65b0 */\nnums[1] = 0;  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u30920\u306b\u66f4\u65b0\n
        list_test.go
        /* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nnum := nums[1]  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\n\n/* \u8981\u7d20\u3092\u66f4\u65b0 */\nnums[1] = 0     // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u30920\u306b\u66f4\u65b0\n
        list.swift
        /* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nlet num = nums[1] // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\n\n/* \u8981\u7d20\u3092\u66f4\u65b0 */\nnums[1] = 0 // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u30920\u306b\u66f4\u65b0\n
        list.js
        /* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nconst num = nums[1];  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\n\n/* \u8981\u7d20\u3092\u66f4\u65b0 */\nnums[1] = 0;  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u30920\u306b\u66f4\u65b0\n
        list.ts
        /* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nconst num: number = nums[1];  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\n\n/* \u8981\u7d20\u3092\u66f4\u65b0 */\nnums[1] = 0;  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u30920\u306b\u66f4\u65b0\n
        list.dart
        /* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint num = nums[1];  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\n\n/* \u8981\u7d20\u3092\u66f4\u65b0 */\nnums[1] = 0;  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u30920\u306b\u66f4\u65b0\n
        list.rs
        /* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nlet num: i32 = nums[1];  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\n/* \u8981\u7d20\u3092\u66f4\u65b0 */\nnums[1] = 0;             // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u30920\u306b\u66f4\u65b0\n
        list.c
        // C\u306f\u7d44\u307f\u8fbc\u307f\u306e\u52d5\u7684\u914d\u5217\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        list.kt
        \n
        "},{"location":"chapter_array_and_linkedlist/list/#3","title":"3. \u00a0 \u8981\u7d20\u306e\u633f\u5165\u3068\u524a\u9664","text":"

        \u914d\u5217\u3068\u6bd4\u8f03\u3057\u3066\u3001\u30ea\u30b9\u30c8\u306f\u8981\u7d20\u306e\u8ffd\u52a0\u3068\u524a\u9664\u306b\u304a\u3044\u3066\u3088\u308a\u67d4\u8edf\u6027\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002\u30ea\u30b9\u30c8\u306e\u672b\u5c3e\u3078\u306e\u8981\u7d20\u8ffd\u52a0\u306f\\(O(1)\\)\u64cd\u4f5c\u3067\u3059\u304c\u3001\u30ea\u30b9\u30c8\u306e\u4ed6\u306e\u5834\u6240\u3067\u306e\u8981\u7d20\u306e\u633f\u5165\u3068\u524a\u9664\u306e\u52b9\u7387\u306f\u914d\u5217\u3068\u540c\u3058\u307e\u307e\u3067\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin list.py
        # \u30ea\u30b9\u30c8\u3092\u30af\u30ea\u30a2\nnums.clear()\n\n# \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n# \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165\nnums.insert(3, 6)  # \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306b\u6570\u50246\u3092\u633f\u5165\n\n# \u8981\u7d20\u3092\u524a\u9664\nnums.pop(3)        # \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306e\u8981\u7d20\u3092\u524a\u9664\n
        list.cpp
        /* \u30ea\u30b9\u30c8\u3092\u30af\u30ea\u30a2 */\nnums.clear();\n\n/* \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 */\nnums.push_back(1);\nnums.push_back(3);\nnums.push_back(2);\nnums.push_back(5);\nnums.push_back(4);\n\n/* \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165 */\nnums.insert(nums.begin() + 3, 6);  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306b\u6570\u50246\u3092\u633f\u5165\n\n/* \u8981\u7d20\u3092\u524a\u9664 */\nnums.erase(nums.begin() + 3);      // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306e\u8981\u7d20\u3092\u524a\u9664\n
        list.java
        /* \u30ea\u30b9\u30c8\u3092\u30af\u30ea\u30a2 */\nnums.clear();\n\n/* \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165 */\nnums.add(3, 6);  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306b\u6570\u50246\u3092\u633f\u5165\n\n/* \u8981\u7d20\u3092\u524a\u9664 */\nnums.remove(3);  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306e\u8981\u7d20\u3092\u524a\u9664\n
        list.cs
        /* \u30ea\u30b9\u30c8\u3092\u30af\u30ea\u30a2 */\nnums.Clear();\n\n/* \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 */\nnums.Add(1);\nnums.Add(3);\nnums.Add(2);\nnums.Add(5);\nnums.Add(4);\n\n/* \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165 */\nnums.Insert(3, 6);\n\n/* \u8981\u7d20\u3092\u524a\u9664 */\nnums.RemoveAt(3);\n
        list_test.go
        /* \u30ea\u30b9\u30c8\u3092\u30af\u30ea\u30a2 */\nnums = nil\n\n/* \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 */\nnums = append(nums, 1)\nnums = append(nums, 3)\nnums = append(nums, 2)\nnums = append(nums, 5)\nnums = append(nums, 4)\n\n/* \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165 */\nnums = append(nums[:3], append([]int{6}, nums[3:]...)...) // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306b\u6570\u50246\u3092\u633f\u5165\n\n/* \u8981\u7d20\u3092\u524a\u9664 */\nnums = append(nums[:3], nums[4:]...) // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306e\u8981\u7d20\u3092\u524a\u9664\n
        list.swift
        /* \u30ea\u30b9\u30c8\u3092\u30af\u30ea\u30a2 */\nnums.removeAll()\n\n/* \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 */\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n/* \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165 */\nnums.insert(6, at: 3) // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306b\u6570\u50246\u3092\u633f\u5165\n\n/* \u8981\u7d20\u3092\u524a\u9664 */\nnums.remove(at: 3) // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306e\u8981\u7d20\u3092\u524a\u9664\n
        list.js
        /* \u30ea\u30b9\u30c8\u3092\u30af\u30ea\u30a2 */\nnums.length = 0;\n\n/* \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165 */\nnums.splice(3, 0, 6);\n\n/* \u8981\u7d20\u3092\u524a\u9664 */\nnums.splice(3, 1);\n
        list.ts
        /* \u30ea\u30b9\u30c8\u3092\u30af\u30ea\u30a2 */\nnums.length = 0;\n\n/* \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165 */\nnums.splice(3, 0, 6);\n\n/* \u8981\u7d20\u3092\u524a\u9664 */\nnums.splice(3, 1);\n
        list.dart
        /* \u30ea\u30b9\u30c8\u3092\u30af\u30ea\u30a2 */\nnums.clear();\n\n/* \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165 */\nnums.insert(3, 6); // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306b\u6570\u50246\u3092\u633f\u5165\n\n/* \u8981\u7d20\u3092\u524a\u9664 */\nnums.removeAt(3); // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306e\u8981\u7d20\u3092\u524a\u9664\n
        list.rs
        /* \u30ea\u30b9\u30c8\u3092\u30af\u30ea\u30a2 */\nnums.clear();\n\n/* \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165 */\nnums.insert(3, 6);  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306b\u6570\u50246\u3092\u633f\u5165\n\n/* \u8981\u7d20\u3092\u524a\u9664 */\nnums.remove(3);    // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306e\u8981\u7d20\u3092\u524a\u9664\n
        list.c
        // C\u306f\u7d44\u307f\u8fbc\u307f\u306e\u52d5\u7684\u914d\u5217\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        list.kt
        \n
        "},{"location":"chapter_array_and_linkedlist/list/#4","title":"4. \u00a0 \u30ea\u30b9\u30c8\u306e\u53cd\u5fa9","text":"

        \u914d\u5217\u3068\u540c\u69d8\u306b\u3001\u30ea\u30b9\u30c8\u306f\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u4f7f\u7528\u3057\u3066\u53cd\u5fa9\u3059\u308b\u3053\u3068\u3082\u3001\u5404\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin list.py
        # \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u30ea\u30b9\u30c8\u3092\u53cd\u5fa9\ncount = 0\nfor i in range(len(nums)):\n    count += nums[i]\n\n# \u30ea\u30b9\u30c8\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9\nfor num in nums:\n    count += num\n
        list.cpp
        /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u30ea\u30b9\u30c8\u3092\u53cd\u5fa9 */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\n    count += nums[i];\n}\n\n/* \u30ea\u30b9\u30c8\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9 */\ncount = 0;\nfor (int num : nums) {\n    count += num;\n}\n
        list.java
        /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u30ea\u30b9\u30c8\u3092\u53cd\u5fa9 */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\n    count += nums.get(i);\n}\n\n/* \u30ea\u30b9\u30c8\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9 */\nfor (int num : nums) {\n    count += num;\n}\n
        list.cs
        /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u30ea\u30b9\u30c8\u3092\u53cd\u5fa9 */\nint count = 0;\nfor (int i = 0; i < nums.Count; i++) {\n    count += nums[i];\n}\n\n/* \u30ea\u30b9\u30c8\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9 */\ncount = 0;\nforeach (int num in nums) {\n    count += num;\n}\n
        list_test.go
        /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u30ea\u30b9\u30c8\u3092\u53cd\u5fa9 */\ncount := 0\nfor i := 0; i < len(nums); i++ {\n    count += nums[i]\n}\n\n/* \u30ea\u30b9\u30c8\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9 */\ncount = 0\nfor _, num := range nums {\n    count += num\n}\n
        list.swift
        /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u30ea\u30b9\u30c8\u3092\u53cd\u5fa9 */\nvar count = 0\nfor i in nums.indices {\n    count += nums[i]\n}\n\n/* \u30ea\u30b9\u30c8\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9 */\ncount = 0\nfor num in nums {\n    count += num\n}\n
        list.js
        /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u30ea\u30b9\u30c8\u3092\u53cd\u5fa9 */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\n    count += nums[i];\n}\n\n/* \u30ea\u30b9\u30c8\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9 */\ncount = 0;\nfor (const num of nums) {\n    count += num;\n}\n
        list.ts
        /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u30ea\u30b9\u30c8\u3092\u53cd\u5fa9 */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\n    count += nums[i];\n}\n\n/* \u30ea\u30b9\u30c8\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9 */\ncount = 0;\nfor (const num of nums) {\n    count += num;\n}\n
        list.dart
        /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u30ea\u30b9\u30c8\u3092\u53cd\u5fa9 */\nint count = 0;\nfor (var i = 0; i < nums.length; i++) {\n    count += nums[i];\n}\n\n/* \u30ea\u30b9\u30c8\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9 */\ncount = 0;\nfor (var num in nums) {\n    count += num;\n}\n
        list.rs
        // \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u30ea\u30b9\u30c8\u3092\u53cd\u5fa9\nlet mut _count = 0;\nfor i in 0..nums.len() {\n    _count += nums[i];\n}\n\n// \u30ea\u30b9\u30c8\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9\n_count = 0;\nfor num in &nums {\n    _count += num;\n}\n
        list.c
        // C\u306f\u7d44\u307f\u8fbc\u307f\u306e\u52d5\u7684\u914d\u5217\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        list.kt
        \n
        "},{"location":"chapter_array_and_linkedlist/list/#5","title":"5. \u00a0 \u30ea\u30b9\u30c8\u306e\u9023\u7d50","text":"

        \u65b0\u3057\u3044\u30ea\u30b9\u30c8nums1\u304c\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001\u305d\u308c\u3092\u5143\u306e\u30ea\u30b9\u30c8\u306e\u672b\u5c3e\u306b\u8ffd\u52a0\u3067\u304d\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin list.py
        # 2\u3064\u306e\u30ea\u30b9\u30c8\u3092\u9023\u7d50\nnums1: list[int] = [6, 8, 7, 10, 9]\nnums += nums1  # nums1\u3092nums\u306e\u672b\u5c3e\u306b\u9023\u7d50\n
        list.cpp
        /* 2\u3064\u306e\u30ea\u30b9\u30c8\u3092\u9023\u7d50 */\nvector<int> nums1 = { 6, 8, 7, 10, 9 };\n// nums1\u3092nums\u306e\u672b\u5c3e\u306b\u9023\u7d50\nnums.insert(nums.end(), nums1.begin(), nums1.end());\n
        list.java
        /* 2\u3064\u306e\u30ea\u30b9\u30c8\u3092\u9023\u7d50 */\nList<Integer> nums1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));\nnums.addAll(nums1);  // nums1\u3092nums\u306e\u672b\u5c3e\u306b\u9023\u7d50\n
        list.cs
        /* 2\u3064\u306e\u30ea\u30b9\u30c8\u3092\u9023\u7d50 */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.AddRange(nums1);  // nums1\u3092nums\u306e\u672b\u5c3e\u306b\u9023\u7d50\n
        list_test.go
        /* 2\u3064\u306e\u30ea\u30b9\u30c8\u3092\u9023\u7d50 */\nnums1 := []int{6, 8, 7, 10, 9}\nnums = append(nums, nums1...)  // nums1\u3092nums\u306e\u672b\u5c3e\u306b\u9023\u7d50\n
        list.swift
        /* 2\u3064\u306e\u30ea\u30b9\u30c8\u3092\u9023\u7d50 */\nlet nums1 = [6, 8, 7, 10, 9]\nnums.append(contentsOf: nums1) // nums1\u3092nums\u306e\u672b\u5c3e\u306b\u9023\u7d50\n
        list.js
        /* 2\u3064\u306e\u30ea\u30b9\u30c8\u3092\u9023\u7d50 */\nconst nums1 = [6, 8, 7, 10, 9];\nnums.push(...nums1);  // nums1\u3092nums\u306e\u672b\u5c3e\u306b\u9023\u7d50\n
        list.ts
        /* 2\u3064\u306e\u30ea\u30b9\u30c8\u3092\u9023\u7d50 */\nconst nums1: number[] = [6, 8, 7, 10, 9];\nnums.push(...nums1);  // nums1\u3092nums\u306e\u672b\u5c3e\u306b\u9023\u7d50\n
        list.dart
        /* 2\u3064\u306e\u30ea\u30b9\u30c8\u3092\u9023\u7d50 */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.addAll(nums1);  // nums1\u3092nums\u306e\u672b\u5c3e\u306b\u9023\u7d50\n
        list.rs
        /* 2\u3064\u306e\u30ea\u30b9\u30c8\u3092\u9023\u7d50 */\nlet nums1: Vec<i32> = vec![6, 8, 7, 10, 9];\nnums.extend(nums1);\n
        list.c
        // C\u306f\u7d44\u307f\u8fbc\u307f\u306e\u52d5\u7684\u914d\u5217\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        list.kt
        \n
        "},{"location":"chapter_array_and_linkedlist/list/#6","title":"6. \u00a0 \u30ea\u30b9\u30c8\u306e\u30bd\u30fc\u30c8","text":"

        \u30ea\u30b9\u30c8\u304c\u30bd\u30fc\u30c8\u3055\u308c\u308b\u3068\u3001\u300c\u4e8c\u5206\u63a2\u7d22\u300d\u3084\u300c\u53cc\u30dd\u30a4\u30f3\u30bf\u300d\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306a\u3069\u3001\u914d\u5217\u95a2\u9023\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u3067\u3088\u304f\u4f7f\u7528\u3055\u308c\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin list.py
        # \u30ea\u30b9\u30c8\u3092\u30bd\u30fc\u30c8\nnums.sort()  # \u30bd\u30fc\u30c8\u5f8c\u3001\u30ea\u30b9\u30c8\u8981\u7d20\u306f\u6607\u9806\u306b\u306a\u308a\u307e\u3059\n
        list.cpp
        /* \u30ea\u30b9\u30c8\u3092\u30bd\u30fc\u30c8 */\nsort(nums.begin(), nums.end());  // \u30bd\u30fc\u30c8\u5f8c\u3001\u30ea\u30b9\u30c8\u8981\u7d20\u306f\u6607\u9806\u306b\u306a\u308a\u307e\u3059\n
        list.java
        /* \u30ea\u30b9\u30c8\u3092\u30bd\u30fc\u30c8 */\nCollections.sort(nums);  // \u30bd\u30fc\u30c8\u5f8c\u3001\u30ea\u30b9\u30c8\u8981\u7d20\u306f\u6607\u9806\u306b\u306a\u308a\u307e\u3059\n
        list.cs
        /* \u30ea\u30b9\u30c8\u3092\u30bd\u30fc\u30c8 */\nnums.Sort(); // \u30bd\u30fc\u30c8\u5f8c\u3001\u30ea\u30b9\u30c8\u8981\u7d20\u306f\u6607\u9806\u306b\u306a\u308a\u307e\u3059\n
        list_test.go
        /* \u30ea\u30b9\u30c8\u3092\u30bd\u30fc\u30c8 */\nsort.Ints(nums)  // \u30bd\u30fc\u30c8\u5f8c\u3001\u30ea\u30b9\u30c8\u8981\u7d20\u306f\u6607\u9806\u306b\u306a\u308a\u307e\u3059\n
        list.swift
        /* \u30ea\u30b9\u30c8\u3092\u30bd\u30fc\u30c8 */\nnums.sort() // \u30bd\u30fc\u30c8\u5f8c\u3001\u30ea\u30b9\u30c8\u8981\u7d20\u306f\u6607\u9806\u306b\u306a\u308a\u307e\u3059\n
        list.js
        /* \u30ea\u30b9\u30c8\u3092\u30bd\u30fc\u30c8 */\nnums.sort((a, b) => a - b);  // \u30bd\u30fc\u30c8\u5f8c\u3001\u30ea\u30b9\u30c8\u8981\u7d20\u306f\u6607\u9806\u306b\u306a\u308a\u307e\u3059\n
        list.ts
        /* \u30ea\u30b9\u30c8\u3092\u30bd\u30fc\u30c8 */\nnums.sort((a, b) => a - b);  // \u30bd\u30fc\u30c8\u5f8c\u3001\u30ea\u30b9\u30c8\u8981\u7d20\u306f\u6607\u9806\u306b\u306a\u308a\u307e\u3059\n
        list.dart
        /* \u30ea\u30b9\u30c8\u3092\u30bd\u30fc\u30c8 */\nnums.sort(); // \u30bd\u30fc\u30c8\u5f8c\u3001\u30ea\u30b9\u30c8\u8981\u7d20\u306f\u6607\u9806\u306b\u306a\u308a\u307e\u3059\n
        list.rs
        /* \u30ea\u30b9\u30c8\u3092\u30bd\u30fc\u30c8 */\nnums.sort(); // \u30bd\u30fc\u30c8\u5f8c\u3001\u30ea\u30b9\u30c8\u8981\u7d20\u306f\u6607\u9806\u306b\u306a\u308a\u307e\u3059\n
        list.c
        // C\u306f\u7d44\u307f\u8fbc\u307f\u306e\u52d5\u7684\u914d\u5217\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        list.kt
        \n
        "},{"location":"chapter_array_and_linkedlist/list/#432","title":"4.3.2 \u00a0 \u30ea\u30b9\u30c8\u306e\u5b9f\u88c5","text":"

        \u591a\u304f\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u306f\u3001Java\u3001C++\u3001Python\u306a\u3069\u3092\u542b\u3080\u7d44\u307f\u8fbc\u307f\u30ea\u30b9\u30c8\u304c\u4ed8\u5c5e\u3057\u3066\u3044\u307e\u3059\u3002\u305d\u308c\u3089\u306e\u5b9f\u88c5\u306f\u3001\u521d\u671f\u5bb9\u91cf\u3084\u62e1\u5f35\u4fc2\u6570\u306a\u3069\u306e\u69d8\u3005\u306a\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u614e\u91cd\u306b\u8003\u616e\u3057\u305f\u8a2d\u5b9a\u3067\u3001\u8907\u96d1\u306b\u306a\u308a\u304c\u3061\u3067\u3059\u3002\u8208\u5473\u306e\u3042\u308b\u8aad\u8005\u306f\u3001\u3055\u3089\u306a\u308b\u5b66\u7fd2\u306e\u305f\u3081\u306b\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u8abf\u3079\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u30ea\u30b9\u30c8\u304c\u3069\u306e\u3088\u3046\u306b\u52d5\u4f5c\u3059\u308b\u304b\u306e\u7406\u89e3\u3092\u6df1\u3081\u308b\u305f\u3081\u306b\u30013\u3064\u306e\u91cd\u8981\u306a\u8a2d\u8a08\u5074\u9762\u306b\u7126\u70b9\u3092\u5f53\u3066\u3066\u3001\u7c21\u7565\u5316\u3055\u308c\u305f\u30ea\u30b9\u30c8\u306e\u5b9f\u88c5\u3092\u8a66\u307f\u307e\u3059\uff1a

        • \u521d\u671f\u5bb9\u91cf\uff1a\u914d\u5217\u306b\u5408\u7406\u7684\u306a\u521d\u671f\u5bb9\u91cf\u3092\u9078\u629e\u3057\u307e\u3059\u3002\u3053\u306e\u4f8b\u3067\u306f\u3001\u521d\u671f\u5bb9\u91cf\u3068\u3057\u306610\u3092\u9078\u629e\u3057\u307e\u3059\u3002
        • \u30b5\u30a4\u30ba\u8a18\u9332\uff1a\u30ea\u30b9\u30c8\u5185\u306e\u73fe\u5728\u306e\u8981\u7d20\u6570\u3092\u8a18\u9332\u3059\u308b\u5909\u6570size\u3092\u5ba3\u8a00\u3057\u3001\u8981\u7d20\u306e\u633f\u5165\u3068\u524a\u9664\u3067\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u306b\u66f4\u65b0\u3057\u307e\u3059\u3002\u3053\u306e\u5909\u6570\u306b\u3088\u308a\u3001\u30ea\u30b9\u30c8\u306e\u672b\u5c3e\u3092\u7279\u5b9a\u3057\u3001\u62e1\u5f35\u304c\u5fc5\u8981\u304b\u3069\u3046\u304b\u3092\u5224\u65ad\u3067\u304d\u307e\u3059\u3002
        • \u62e1\u5f35\u30e1\u30ab\u30cb\u30ba\u30e0\uff1a\u8981\u7d20\u633f\u5165\u6642\u306b\u30ea\u30b9\u30c8\u304c\u6e80\u676f\u306b\u9054\u3057\u305f\u5834\u5408\u3001\u62e1\u5f35\u30d7\u30ed\u30bb\u30b9\u304c\u5fc5\u8981\u3067\u3059\u3002\u3053\u308c\u306b\u306f\u62e1\u5f35\u4fc2\u6570\u306b\u57fa\u3065\u3044\u3066\u3088\u308a\u5927\u304d\u306a\u914d\u5217\u3092\u4f5c\u6210\u3057\u3001\u73fe\u5728\u306e\u914d\u5217\u304b\u3089\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u65b0\u3057\u3044\u914d\u5217\u306b\u8ee2\u9001\u3059\u308b\u3053\u3068\u304c\u542b\u307e\u308c\u307e\u3059\u3002\u3053\u306e\u4f8b\u3067\u306f\u3001\u62e1\u5f35\u306e\u305f\u3073\u306b\u914d\u5217\u30b5\u30a4\u30ba\u30922\u500d\u306b\u3059\u308b\u3053\u3068\u3092\u898f\u5b9a\u3057\u307e\u3059\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_list.py
        class MyList:\n    \"\"\"\u30ea\u30b9\u30c8\u30af\u30e9\u30b9\"\"\"\n\n    def __init__(self):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        self._capacity: int = 10  # \u30ea\u30b9\u30c8\u306e\u5bb9\u91cf\n        self._arr: list[int] = [0] * self._capacity  # \u914d\u5217\uff08\u30ea\u30b9\u30c8\u8981\u7d20\u3092\u683c\u7d0d\uff09\n        self._size: int = 0  # \u30ea\u30b9\u30c8\u306e\u9577\u3055\uff08\u73fe\u5728\u306e\u8981\u7d20\u6570\uff09\n        self._extend_ratio: int = 2  # \u5404\u30ea\u30b9\u30c8\u62e1\u5f35\u306e\u500d\u6570\n\n    def size(self) -> int:\n        \"\"\"\u30ea\u30b9\u30c8\u306e\u9577\u3055\uff08\u73fe\u5728\u306e\u8981\u7d20\u6570\uff09\u3092\u53d6\u5f97\"\"\"\n        return self._size\n\n    def capacity(self) -> int:\n        \"\"\"\u30ea\u30b9\u30c8\u306e\u5bb9\u91cf\u3092\u53d6\u5f97\"\"\"\n        return self._capacity\n\n    def get(self, index: int) -> int:\n        \"\"\"\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\"\"\"\n        # \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u4f8b\u5916\u3092\u30b9\u30ed\u30fc\n        if index < 0 or index >= self._size:\n            raise IndexError(\"Index out of bounds\")\n        return self._arr[index]\n\n    def set(self, num: int, index: int):\n        \"\"\"\u8981\u7d20\u3092\u66f4\u65b0\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"Index out of bounds\")\n        self._arr[index] = num\n\n    def add(self, num: int):\n        \"\"\"\u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0\"\"\"\n        # \u8981\u7d20\u6570\u304c\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u62e1\u5f35\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u30c8\u30ea\u30ac\u30fc\n        if self.size() == self.capacity():\n            self.extend_capacity()\n        self._arr[self._size] = num\n        self._size += 1\n\n    def insert(self, num: int, index: int):\n        \"\"\"\u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"Index out of bounds\")\n        # \u8981\u7d20\u6570\u304c\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u62e1\u5f35\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u30c8\u30ea\u30ac\u30fc\n        if self._size == self.capacity():\n            self.extend_capacity()\n        # \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 index \u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u5f8c\u308d\u306b\u79fb\u52d5\n        for j in range(self._size - 1, index - 1, -1):\n            self._arr[j + 1] = self._arr[j]\n        self._arr[index] = num\n        # \u8981\u7d20\u6570\u3092\u66f4\u65b0\n        self._size += 1\n\n    def remove(self, index: int) -> int:\n        \"\"\"\u8981\u7d20\u3092\u524a\u9664\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"Index out of bounds\")\n        num = self._arr[index]\n        # \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 index \u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u524d\u306b\u79fb\u52d5\n        for j in range(index, self._size - 1):\n            self._arr[j] = self._arr[j + 1]\n        # \u8981\u7d20\u6570\u3092\u66f4\u65b0\n        self._size -= 1\n        # \u524a\u9664\u3055\u308c\u305f\u8981\u7d20\u3092\u8fd4\u3059\n        return num\n\n    def extend_capacity(self):\n        \"\"\"\u30ea\u30b9\u30c8\u3092\u62e1\u5f35\"\"\"\n        # \u5143\u306e\u914d\u5217\u306e _extend_ratio \u500d\u306e\u9577\u3055\u306e\u65b0\u3057\u3044\u914d\u5217\u3092\u4f5c\u6210\u3057\u3001\u5143\u306e\u914d\u5217\u3092\u65b0\u3057\u3044\u914d\u5217\u306b\u30b3\u30d4\u30fc\n        self._arr = self._arr + [0] * self.capacity() * (self._extend_ratio - 1)\n        # \u30ea\u30b9\u30c8\u306e\u5bb9\u91cf\u3092\u66f4\u65b0\n        self._capacity = len(self._arr)\n\n    def to_array(self) -> list[int]:\n        \"\"\"\u6709\u52b9\u306a\u9577\u3055\u306e\u30ea\u30b9\u30c8\u3092\u8fd4\u3059\"\"\"\n        return self._arr[: self._size]\n
        my_list.cpp
        /* \u30ea\u30b9\u30c8\u30af\u30e9\u30b9 */\nclass MyList {\n  private:\n    int *arr;             // \u914d\u5217\uff08\u30ea\u30b9\u30c8\u8981\u7d20\u3092\u683c\u7d0d\uff09\n    int arrCapacity = 10; // \u30ea\u30b9\u30c8\u306e\u5bb9\u91cf\n    int arrSize = 0;      // \u30ea\u30b9\u30c8\u306e\u9577\u3055\uff08\u73fe\u5728\u306e\u8981\u7d20\u6570\uff09\n    int extendRatio = 2;   // \u30ea\u30b9\u30c8\u62e1\u5f35\u6642\u306e\u500d\u7387\n\n  public:\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    MyList() {\n        arr = new int[arrCapacity];\n    }\n\n    /* \u30c7\u30b9\u30c8\u30e9\u30af\u30bf */\n    ~MyList() {\n        delete[] arr;\n    }\n\n    /* \u30ea\u30b9\u30c8\u306e\u9577\u3055\u3092\u53d6\u5f97\uff08\u73fe\u5728\u306e\u8981\u7d20\u6570\uff09*/\n    int size() {\n        return arrSize;\n    }\n\n    /* \u30ea\u30b9\u30c8\u306e\u5bb9\u91cf\u3092\u53d6\u5f97 */\n    int capacity() {\n        return arrCapacity;\n    }\n\n    /* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    int get(int index) {\n        // \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001\u4f8b\u5916\u3092\u30b9\u30ed\u30fc\uff08\u4ee5\u4e0b\u540c\u69d8\uff09\n        if (index < 0 || index >= size())\n            throw out_of_range(\"Index out of bounds\");\n        return arr[index];\n    }\n\n    /* \u8981\u7d20\u3092\u66f4\u65b0 */\n    void set(int index, int num) {\n        if (index < 0 || index >= size())\n            throw out_of_range(\"Index out of bounds\");\n        arr[index] = num;\n    }\n\n    /* \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 */\n    void add(int num) {\n        // \u8981\u7d20\u6570\u304c\u5bb9\u91cf\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u62e1\u5f35\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u30c8\u30ea\u30ac\u30fc\n        if (size() == capacity())\n            extendCapacity();\n        arr[size()] = num;\n        // \u8981\u7d20\u6570\u3092\u66f4\u65b0\n        arrSize++;\n    }\n\n    /* \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165 */\n    void insert(int index, int num) {\n        if (index < 0 || index >= size())\n            throw out_of_range(\"Index out of bounds\");\n        // \u8981\u7d20\u6570\u304c\u5bb9\u91cf\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u62e1\u5f35\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u30c8\u30ea\u30ac\u30fc\n        if (size() == capacity())\n            extendCapacity();\n        // `index`\u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u5f8c\u308d\u306b\u79fb\u52d5\n        for (int j = size() - 1; j >= index; j--) {\n            arr[j + 1] = arr[j];\n        }\n        arr[index] = num;\n        // \u8981\u7d20\u6570\u3092\u66f4\u65b0\n        arrSize++;\n    }\n\n    /* \u8981\u7d20\u3092\u524a\u9664 */\n    int remove(int index) {\n        if (index < 0 || index >= size())\n            throw out_of_range(\"Index out of bounds\");\n        int num = arr[index];\n        // `index`\u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u524d\u306b\u79fb\u52d5\n        for (int j = index; j < size() - 1; j++) {\n            arr[j] = arr[j + 1];\n        }\n        // \u8981\u7d20\u6570\u3092\u66f4\u65b0\n        arrSize--;\n        // \u524a\u9664\u3055\u308c\u305f\u8981\u7d20\u3092\u8fd4\u5374\n        return num;\n    }\n\n    /* \u30ea\u30b9\u30c8\u3092\u62e1\u5f35 */\n    void extendCapacity() {\n        // \u5143\u306e\u914d\u5217\u306eextendRatio\u500d\u306e\u9577\u3055\u3067\u65b0\u3057\u3044\u914d\u5217\u3092\u4f5c\u6210\n        int newCapacity = capacity() * extendRatio;\n        int *tmp = arr;\n        arr = new int[newCapacity];\n        // \u5143\u306e\u914d\u5217\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u65b0\u3057\u3044\u914d\u5217\u306b\u30b3\u30d4\u30fc\n        for (int i = 0; i < size(); i++) {\n            arr[i] = tmp[i];\n        }\n        // \u30e1\u30e2\u30ea\u3092\u89e3\u653e\n        delete[] tmp;\n        arrCapacity = newCapacity;\n    }\n\n    /* \u30ea\u30b9\u30c8\u3092Vector\u306b\u5909\u63db\u3057\u3066\u5370\u5237\u7528\u306b\u4f7f\u7528 */\n    vector<int> toVector() {\n        // \u6709\u52b9\u306a\u9577\u3055\u7bc4\u56f2\u5185\u306e\u8981\u7d20\u306e\u307f\u3092\u5909\u63db\n        vector<int> vec(size());\n        for (int i = 0; i < size(); i++) {\n            vec[i] = arr[i];\n        }\n        return vec;\n    }\n};\n
        my_list.java
        /* \u30ea\u30b9\u30c8\u30af\u30e9\u30b9 */\nclass MyList {\n    private int[] arr; // \u914d\u5217\uff08\u30ea\u30b9\u30c8\u8981\u7d20\u3092\u683c\u7d0d\uff09\n    private int capacity = 10; // \u30ea\u30b9\u30c8\u5bb9\u91cf\n    private int size = 0; // \u30ea\u30b9\u30c8\u9577\uff08\u73fe\u5728\u306e\u8981\u7d20\u6570\uff09\n    private int extendRatio = 2; // \u30ea\u30b9\u30c8\u306e\u5404\u62e1\u5f35\u500d\u7387\n\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    public MyList() {\n        arr = new int[capacity];\n    }\n\n    /* \u30ea\u30b9\u30c8\u9577\u3092\u53d6\u5f97\uff08\u73fe\u5728\u306e\u8981\u7d20\u6570\uff09 */\n    public int size() {\n        return size;\n    }\n\n    /* \u30ea\u30b9\u30c8\u5bb9\u91cf\u3092\u53d6\u5f97 */\n    public int capacity() {\n        return capacity;\n    }\n\n    /* \u8981\u7d20\u3078\u306e\u30a2\u30af\u30bb\u30b9 */\n    public int get(int index) {\n        // \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u4f8b\u5916\u3092\u30b9\u30ed\u30fc\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u3067\u3059\");\n        return arr[index];\n    }\n\n    /* \u8981\u7d20\u306e\u66f4\u65b0 */\n    public void set(int index, int num) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u3067\u3059\");\n        arr[index] = num;\n    }\n\n    /* \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 */\n    public void add(int num) {\n        // \u8981\u7d20\u6570\u304c\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u62e1\u5f35\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u5b9f\u884c\n        if (size == capacity())\n            extendCapacity();\n        arr[size] = num;\n        // \u8981\u7d20\u6570\u3092\u66f4\u65b0\n        size++;\n    }\n\n    /* \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165 */\n    public void insert(int index, int num) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u3067\u3059\");\n        // \u8981\u7d20\u6570\u304c\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u62e1\u5f35\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u5b9f\u884c\n        if (size == capacity())\n            extendCapacity();\n        // `index` \u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u5f8c\u308d\u306b\u79fb\u52d5\n        for (int j = size - 1; j >= index; j--) {\n            arr[j + 1] = arr[j];\n        }\n        arr[index] = num;\n        // \u8981\u7d20\u6570\u3092\u66f4\u65b0\n        size++;\n    }\n\n    /* \u8981\u7d20\u306e\u524a\u9664 */\n    public int remove(int index) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u3067\u3059\");\n        int num = arr[index];\n        // `index` \u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u524d\u306b\u79fb\u52d5\n        for (int j = index; j < size - 1; j++) {\n            arr[j] = arr[j + 1];\n        }\n        // \u8981\u7d20\u6570\u3092\u66f4\u65b0\n        size--;\n        // \u524a\u9664\u3055\u308c\u305f\u8981\u7d20\u3092\u8fd4\u3059\n        return num;\n    }\n\n    /* \u30ea\u30b9\u30c8\u3092\u62e1\u5f35 */\n    public void extendCapacity() {\n        // \u5143\u306e\u914d\u5217\u306e\u9577\u3055\u3092 extendRatio \u500d\u3057\u305f\u65b0\u3057\u3044\u914d\u5217\u3092\u4f5c\u6210\u3057\u3001\u5143\u306e\u914d\u5217\u3092\u65b0\u3057\u3044\u914d\u5217\u306b\u30b3\u30d4\u30fc\n        arr = Arrays.copyOf(arr, capacity() * extendRatio);\n        // \u30ea\u30b9\u30c8\u5bb9\u91cf\u3092\u66f4\u65b0\n        capacity = arr.length;\n    }\n\n    /* \u30ea\u30b9\u30c8\u3092\u914d\u5217\u306b\u5909\u63db */\n    public int[] toArray() {\n        int size = size();\n        // \u6709\u52b9\u306a\u9577\u3055\u7bc4\u56f2\u5185\u306e\u8981\u7d20\u306e\u307f\u3092\u5909\u63db\n        int[] arr = new int[size];\n        for (int i = 0; i < size; i++) {\n            arr[i] = get(i);\n        }\n        return arr;\n    }\n}\n
        my_list.cs
        [class]{MyList}-[func]{}\n
        my_list.go
        [class]{myList}-[func]{}\n
        my_list.swift
        [class]{MyList}-[func]{}\n
        my_list.js
        [class]{MyList}-[func]{}\n
        my_list.ts
        [class]{MyList}-[func]{}\n
        my_list.dart
        [class]{MyList}-[func]{}\n
        my_list.rs
        [class]{MyList}-[func]{}\n
        my_list.c
        [class]{MyList}-[func]{}\n
        my_list.kt
        [class]{MyList}-[func]{}\n
        my_list.rb
        [class]{MyList}-[func]{}\n
        "},{"location":"chapter_array_and_linkedlist/ram_and_cache/","title":"4.4 \u00a0 \u30e1\u30e2\u30ea\u3068\u30ad\u30e3\u30c3\u30b7\u30e5 *","text":"

        \u3053\u306e\u7ae0\u306e\u6700\u521d\u306e2\u3064\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u300c\u9023\u7d9a\u683c\u7d0d\u300d\u3068\u300c\u5206\u6563\u683c\u7d0d\u300d\u3092\u305d\u308c\u305e\u308c\u8868\u73fe\u3059\u308b2\u3064\u306e\u57fa\u672c\u7684\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3042\u308b\u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u63a2\u7a76\u3057\u307e\u3057\u305f\u3002

        \u5b9f\u969b\u3001\u7269\u7406\u69cb\u9020\u306f\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u30e1\u30e2\u30ea\u3068\u30ad\u30e3\u30c3\u30b7\u30e5\u3092\u3069\u306e\u7a0b\u5ea6\u52b9\u7387\u7684\u306b\u5229\u7528\u3059\u308b\u304b\u3092\u5927\u304d\u304f\u6c7a\u5b9a\u3057\u3001\u3053\u308c\u304c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5168\u4f53\u7684\u306a\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306b\u5f71\u97ff\u3092\u4e0e\u3048\u307e\u3059\u3002

        "},{"location":"chapter_array_and_linkedlist/ram_and_cache/#441","title":"4.4.1 \u00a0 \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u8a18\u61b6\u88c5\u7f6e","text":"

        \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306b\u306f3\u7a2e\u985e\u306e\u8a18\u61b6\u88c5\u7f6e\u304c\u3042\u308a\u307e\u3059\uff1a\u30cf\u30fc\u30c9\u30c7\u30a3\u30b9\u30af\u3001\u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\u30e1\u30e2\u30ea\uff08RAM\uff09\u3001\u304a\u3088\u3073\u30ad\u30e3\u30c3\u30b7\u30e5\u30e1\u30e2\u30ea\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u8868\u306f\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30b7\u30b9\u30c6\u30e0\u306b\u304a\u3051\u308b\u305d\u308c\u305e\u308c\u306e\u5f79\u5272\u3068\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u7279\u6027\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u8868 4-2 \u00a0 \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u8a18\u61b6\u88c5\u7f6e

        \u30cf\u30fc\u30c9\u30c7\u30a3\u30b9\u30af \u30e1\u30e2\u30ea \u30ad\u30e3\u30c3\u30b7\u30e5 \u7528\u9014 OS\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u3001\u30d5\u30a1\u30a4\u30eb\u306a\u3069\u306e\u30c7\u30fc\u30bf\u306e\u9577\u671f\u4fdd\u5b58 \u73fe\u5728\u5b9f\u884c\u4e2d\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3068\u51e6\u7406\u4e2d\u306e\u30c7\u30fc\u30bf\u306e\u4e00\u6642\u4fdd\u5b58 \u983b\u7e41\u306b\u30a2\u30af\u30bb\u30b9\u3055\u308c\u308b\u30c7\u30fc\u30bf\u3068\u547d\u4ee4\u3092\u4fdd\u5b58\u3057\u3001CPU\u306e\u30e1\u30e2\u30ea\u3078\u306e\u30a2\u30af\u30bb\u30b9\u6570\u3092\u524a\u6e1b \u63ee\u767a\u6027 \u96fb\u6e90\u30aa\u30d5\u5f8c\u3082\u30c7\u30fc\u30bf\u306f\u5931\u308f\u308c\u306a\u3044 \u96fb\u6e90\u30aa\u30d5\u5f8c\u306b\u30c7\u30fc\u30bf\u306f\u5931\u308f\u308c\u308b \u96fb\u6e90\u30aa\u30d5\u5f8c\u306b\u30c7\u30fc\u30bf\u306f\u5931\u308f\u308c\u308b \u5bb9\u91cf \u3088\u308a\u5927\u304d\u3044\u3001TB\u30ec\u30d9\u30eb \u3088\u308a\u5c0f\u3055\u3044\u3001GB\u30ec\u30d9\u30eb \u975e\u5e38\u306b\u5c0f\u3055\u3044\u3001MB\u30ec\u30d9\u30eb \u901f\u5ea6 \u3088\u308a\u9045\u3044\u3001\u6570\u767e\u304b\u3089\u6570\u5343MB/s \u3088\u308a\u9ad8\u901f\u3001\u6570\u5341GB/s \u975e\u5e38\u306b\u9ad8\u901f\u3001\u6570\u5341\u304b\u3089\u6570\u767eGB/s \u4fa1\u683c\uff08USD\uff09 \u3088\u308a\u5b89\u4fa1\u3001\u6570\u30bb\u30f3\u30c8/GB \u3088\u308a\u9ad8\u4fa1\u3001\u6570\u30c9\u30eb/GB \u975e\u5e38\u306b\u9ad8\u4fa1\u3001CPU\u3068\u4e00\u7dd2\u306b\u4fa1\u683c\u8a2d\u5b9a

        \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u8a18\u61b6\u30b7\u30b9\u30c6\u30e0\u306f\u3001\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u30d4\u30e9\u30df\u30c3\u30c9\u3068\u3057\u3066\u8996\u899a\u5316\u3067\u304d\u307e\u3059\u3002\u30d4\u30e9\u30df\u30c3\u30c9\u306e\u4e0a\u90e8\u306b\u3042\u308b\u8a18\u61b6\u88c5\u7f6e\u307b\u3069\u9ad8\u901f\u3067\u3001\u5bb9\u91cf\u304c\u5c0f\u3055\u304f\u3001\u3088\u308a\u9ad8\u4fa1\u3067\u3059\u3002\u3053\u306e\u30de\u30eb\u30c1\u30ec\u30d9\u30eb\u8a2d\u8a08\u306f\u5076\u7136\u3067\u306f\u306a\u304f\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u79d1\u5b66\u8005\u3068\u30a8\u30f3\u30b8\u30cb\u30a2\u306b\u3088\u308b\u614e\u91cd\u306a\u691c\u8a0e\u306e\u7d50\u679c\u3067\u3059\u3002

        • \u30cf\u30fc\u30c9\u30c7\u30a3\u30b9\u30af\u3092\u30e1\u30e2\u30ea\u306b\u7f6e\u304d\u63db\u3048\u308b\u306e\u306f\u56f0\u96e3\u3067\u3059\u3002\u7b2c\u4e00\u306b\u3001\u30e1\u30e2\u30ea\u5185\u306e\u30c7\u30fc\u30bf\u306f\u96fb\u6e90\u30aa\u30d5\u5f8c\u306b\u5931\u308f\u308c\u308b\u305f\u3081\u3001\u9577\u671f\u30c7\u30fc\u30bf\u4fdd\u5b58\u306b\u306f\u9069\u3057\u3066\u3044\u307e\u305b\u3093\u3002\u7b2c\u4e8c\u306b\u3001\u30e1\u30e2\u30ea\u306f\u30cf\u30fc\u30c9\u30c7\u30a3\u30b9\u30af\u3088\u308a\u3082\u5927\u5e45\u306b\u9ad8\u4fa1\u3067\u3001\u6d88\u8cbb\u8005\u5e02\u5834\u3067\u306e\u5e83\u7bc4\u56f2\u306a\u4f7f\u7528\u306e\u5b9f\u73fe\u53ef\u80fd\u6027\u3092\u5236\u9650\u3057\u3066\u3044\u307e\u3059\u3002
        • \u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u5927\u5bb9\u91cf\u3068\u9ad8\u901f\u306e\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u306b\u76f4\u9762\u3057\u3066\u3044\u307e\u3059\u3002L1\u3001L2\u3001L3\u30ad\u30e3\u30c3\u30b7\u30e5\u306e\u5bb9\u91cf\u304c\u5897\u52a0\u3059\u308b\u306b\u3064\u308c\u3066\u3001\u305d\u306e\u7269\u7406\u30b5\u30a4\u30ba\u304c\u5927\u304d\u304f\u306a\u308a\u3001CPU\u30b3\u30a2\u304b\u3089\u306e\u8ddd\u96e2\u304c\u5897\u52a0\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u30c7\u30fc\u30bf\u8ee2\u9001\u6642\u9593\u304c\u9577\u304f\u306a\u308a\u3001\u30a2\u30af\u30bb\u30b9\u9045\u5ef6\u304c\u9ad8\u304f\u306a\u308a\u307e\u3059\u3002\u73fe\u5728\u306e\u6280\u8853\u3067\u306f\u3001\u30de\u30eb\u30c1\u30ec\u30d9\u30eb\u30ad\u30e3\u30c3\u30b7\u30e5\u69cb\u9020\u304c\u5bb9\u91cf\u3001\u901f\u5ea6\u3001\u30b3\u30b9\u30c8\u306e\u9593\u306e\u6700\u9069\u306a\u30d0\u30e9\u30f3\u30b9\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002

        \u56f3 4-9 \u00a0 \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u8a18\u61b6\u30b7\u30b9\u30c6\u30e0

        Tip

        \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u8a18\u61b6\u968e\u5c64\u306f\u3001\u901f\u5ea6\u3001\u5bb9\u91cf\u3001\u30b3\u30b9\u30c8\u306e\u9593\u306e\u614e\u91cd\u306a\u30d0\u30e9\u30f3\u30b9\u3092\u53cd\u6620\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u30bf\u30a4\u30d7\u306e\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u306f\u69d8\u3005\u306a\u696d\u754c\u3067\u4e00\u822c\u7684\u3067\u3042\u308a\u3001\u5229\u76ca\u3068\u5236\u9650\u306e\u9593\u306e\u6700\u9069\u306a\u30d0\u30e9\u30f3\u30b9\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002

        \u5168\u4f53\u7684\u306b\u3001\u30cf\u30fc\u30c9\u30c7\u30a3\u30b9\u30af\u306f\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u306e\u9577\u671f\u4fdd\u5b58\u3092\u63d0\u4f9b\u3057\u3001\u30e1\u30e2\u30ea\u306f\u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c\u4e2d\u306b\u51e6\u7406\u3055\u308c\u308b\u30c7\u30fc\u30bf\u306e\u4e00\u6642\u4fdd\u5b58\u3068\u3057\u3066\u6a5f\u80fd\u3057\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u983b\u7e41\u306b\u30a2\u30af\u30bb\u30b9\u3055\u308c\u308b\u30c7\u30fc\u30bf\u3068\u547d\u4ee4\u3092\u4fdd\u5b58\u3057\u3066\u5b9f\u884c\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002\u305d\u308c\u3089\u306f\u4e00\u7dd2\u306b\u306a\u3063\u3066\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30b7\u30b9\u30c6\u30e0\u306e\u52b9\u7387\u7684\u306a\u52d5\u4f5c\u3092\u4fdd\u8a3c\u3057\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c\u4e2d\u3001\u30c7\u30fc\u30bf\u306f\u30cf\u30fc\u30c9\u30c7\u30a3\u30b9\u30af\u304b\u3089\u30e1\u30e2\u30ea\u306b\u8aad\u307f\u8fbc\u307e\u308c\u3001CPU\u8a08\u7b97\u304c\u884c\u308f\u308c\u307e\u3059\u3002CPU\u306e\u62e1\u5f35\u3068\u3057\u3066\u6a5f\u80fd\u3059\u308b\u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u3001**\u30e1\u30e2\u30ea\u304b\u3089\u30a4\u30f3\u30c6\u30ea\u30b8\u30a7\u30f3\u30c8\u306b\u30c7\u30fc\u30bf\u3092\u5148\u8aad\u307f**\u3057\u3001CPU\u306e\u3088\u308a\u9ad8\u901f\u306a\u30c7\u30fc\u30bf\u30a2\u30af\u30bb\u30b9\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c\u52b9\u7387\u304c\u5927\u5e45\u306b\u5411\u4e0a\u3057\u3001\u4f4e\u901f\u306a\u30e1\u30e2\u30ea\u3078\u306e\u4f9d\u5b58\u304c\u6e1b\u5c11\u3057\u307e\u3059\u3002

        \u56f3 4-10 \u00a0 \u30cf\u30fc\u30c9\u30c7\u30a3\u30b9\u30af\u3001\u30e1\u30e2\u30ea\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u9593\u306e\u30c7\u30fc\u30bf\u30d5\u30ed\u30fc

        "},{"location":"chapter_array_and_linkedlist/ram_and_cache/#442","title":"4.4.2 \u00a0 \u30c7\u30fc\u30bf\u69cb\u9020\u306e\u30e1\u30e2\u30ea\u52b9\u7387","text":"

        \u30e1\u30e2\u30ea\u7a7a\u9593\u5229\u7528\u306e\u89b3\u70b9\u304b\u3089\u3001\u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u306f\u305d\u308c\u305e\u308c\u5229\u70b9\u3068\u5236\u9650\u304c\u3042\u308a\u307e\u3059\u3002

        \u4e00\u65b9\u3067\u3001**\u30e1\u30e2\u30ea\u306f\u9650\u3089\u308c\u3066\u304a\u308a\u3001\u8907\u6570\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u9593\u3067\u5171\u6709\u3067\u304d\u306a\u3044**\u305f\u3081\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u306e\u7a7a\u9593\u4f7f\u7528\u306e\u6700\u9069\u5316\u306f\u91cd\u8981\u3067\u3059\u3002\u914d\u5217\u306f\u8981\u7d20\u304c\u5bc6\u63a5\u306b\u30d1\u30c3\u30af\u3055\u308c\u3066\u304a\u308a\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u3088\u3046\u306b\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u306e\u305f\u3081\u306e\u8ffd\u52a0\u30e1\u30e2\u30ea\u3092\u5fc5\u8981\u3068\u3057\u306a\u3044\u305f\u3081\u3001\u7a7a\u9593\u52b9\u7387\u7684\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u914d\u5217\u306f\u9023\u7d9a\u3057\u305f\u30e1\u30e2\u30ea\u30d6\u30ed\u30c3\u30af\u3092\u4e8b\u524d\u306b\u5272\u308a\u5f53\u3066\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u5272\u308a\u5f53\u3066\u3089\u308c\u305f\u7a7a\u9593\u304c\u5b9f\u969b\u306e\u5fc5\u8981\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u7121\u99c4\u306b\u3064\u306a\u304c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u914d\u5217\u306e\u62e1\u5f35\u3082\u8ffd\u52a0\u306e\u6642\u9593\u3068\u7a7a\u9593\u306e\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u3092\u4f34\u3044\u307e\u3059\u3002\u5bfe\u7167\u7684\u306b\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u5404\u30ce\u30fc\u30c9\u306b\u5bfe\u3057\u3066\u30e1\u30e2\u30ea\u3092\u52d5\u7684\u306b\u5272\u308a\u5f53\u3066\u30fb\u89e3\u653e\u3057\u3001\u30dd\u30a4\u30f3\u30bf\u306e\u305f\u3081\u306e\u8ffd\u52a0\u30e1\u30e2\u30ea\u306e\u30b3\u30b9\u30c8\u3067\u3088\u308a\u5927\u304d\u306a\u67d4\u8edf\u6027\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002

        \u4e00\u65b9\u3067\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c\u4e2d\u3001\u7e70\u308a\u8fd4\u3055\u308c\u308b\u30e1\u30e2\u30ea\u306e\u5272\u308a\u5f53\u3066\u3068\u89e3\u653e\u306f\u30e1\u30e2\u30ea\u306e\u65ad\u7247\u5316\u3092\u5897\u52a0\u3055\u305b\u3001\u30e1\u30e2\u30ea\u5229\u7528\u52b9\u7387\u3092\u4f4e\u4e0b\u3055\u305b\u307e\u3059\u3002\u914d\u5217\u306f\u9023\u7d9a\u8a18\u61b6\u65b9\u5f0f\u306b\u3088\u308a\u3001\u30e1\u30e2\u30ea\u65ad\u7247\u5316\u3092\u5f15\u304d\u8d77\u3053\u3059\u53ef\u80fd\u6027\u304c\u6bd4\u8f03\u7684\u4f4e\u3044\u3067\u3059\u3002\u5bfe\u7167\u7684\u306b\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u8981\u7d20\u3092\u975e\u9023\u7d9a\u306e\u5834\u6240\u306b\u4fdd\u5b58\u3057\u3001\u983b\u7e41\u306a\u633f\u5165\u3068\u524a\u9664\u306f\u30e1\u30e2\u30ea\u65ad\u7247\u5316\u3092\u60aa\u5316\u3055\u305b\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_array_and_linkedlist/ram_and_cache/#443","title":"4.4.3 \u00a0 \u30c7\u30fc\u30bf\u69cb\u9020\u306e\u30ad\u30e3\u30c3\u30b7\u30e5\u52b9\u7387","text":"

        \u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u30e1\u30e2\u30ea\u3088\u308a\u3082\u7a7a\u9593\u5bb9\u91cf\u304c\u306f\u308b\u304b\u306b\u5c0f\u3055\u3044\u3067\u3059\u304c\u3001\u306f\u308b\u304b\u306b\u9ad8\u901f\u3067\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c\u901f\u5ea6\u306b\u304a\u3044\u3066\u91cd\u8981\u306a\u5f79\u5272\u3092\u679c\u305f\u3057\u307e\u3059\u3002\u9650\u3089\u308c\u305f\u5bb9\u91cf\u306e\u305f\u3081\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u983b\u7e41\u306b\u30a2\u30af\u30bb\u30b9\u3055\u308c\u308b\u30c7\u30fc\u30bf\u306e\u30b5\u30d6\u30bb\u30c3\u30c8\u306e\u307f\u3092\u4fdd\u5b58\u3067\u304d\u307e\u3059\u3002CPU\u304c\u30ad\u30e3\u30c3\u30b7\u30e5\u306b\u5b58\u5728\u3057\u306a\u3044\u30c7\u30fc\u30bf\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u30df\u30b9\u304c\u767a\u751f\u3057\u3001CPU\u306f\u4f4e\u901f\u306a\u30e1\u30e2\u30ea\u304b\u3089\u5fc5\u8981\u306a\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306b\u5f71\u97ff\u3092\u4e0e\u3048\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        \u660e\u3089\u304b\u306b\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u30df\u30b9\u304c\u5c11\u306a\u3044\u307b\u3069\u3001CPU\u306e\u30c7\u30fc\u30bf\u8aad\u307f\u66f8\u304d\u52b9\u7387\u304c\u9ad8\u304f\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u5411\u4e0a\u3057\u307e\u3059\u3002CPU\u304c\u30ad\u30e3\u30c3\u30b7\u30e5\u304b\u3089\u30c7\u30fc\u30bf\u3092\u6b63\u5e38\u306b\u53d6\u5f97\u3059\u308b\u5272\u5408\u306f\u30ad\u30e3\u30c3\u30b7\u30e5\u30d2\u30c3\u30c8\u7387\u3068\u547c\u3070\u308c\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u52b9\u7387\u3092\u6e2c\u5b9a\u3059\u308b\u305f\u3081\u306b\u3088\u304f\u4f7f\u7528\u3055\u308c\u308b\u6307\u6a19\u3067\u3059\u3002

        \u3088\u308a\u9ad8\u3044\u52b9\u7387\u3092\u9054\u6210\u3059\u308b\u305f\u3081\u306b\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u4ee5\u4e0b\u306e\u30c7\u30fc\u30bf\u30ed\u30fc\u30c9\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u63a1\u7528\u3057\u307e\u3059\u3002

        • \u30ad\u30e3\u30c3\u30b7\u30e5\u30e9\u30a4\u30f3\uff1a\u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u500b\u3005\u306e\u30d0\u30a4\u30c8\u3067\u306f\u306a\u304f\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u30e9\u30a4\u30f3\u3068\u547c\u3070\u308c\u308b\u5358\u4f4d\u3067\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58\u30fb\u30ed\u30fc\u30c9\u3057\u3066\u52d5\u4f5c\u3057\u307e\u3059\u3002\u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\u3001\u4e00\u5ea6\u306b\u3088\u308a\u5927\u304d\u306a\u30c7\u30fc\u30bf\u30d6\u30ed\u30c3\u30af\u3092\u8ee2\u9001\u3059\u308b\u3053\u3068\u3067\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002
        • \u5148\u8aad\u307f\u30e1\u30ab\u30cb\u30ba\u30e0\uff1a\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u30c7\u30fc\u30bf\u30a2\u30af\u30bb\u30b9\u30d1\u30bf\u30fc\u30f3\uff08\u4f8b\uff1a\u9023\u7d9a\u307e\u305f\u306f\u56fa\u5b9a\u30b9\u30c8\u30e9\u30a4\u30c9\u30a2\u30af\u30bb\u30b9\uff09\u3092\u4e88\u6e2c\u3057\u3001\u3053\u308c\u3089\u306e\u30d1\u30bf\u30fc\u30f3\u306b\u57fa\u3065\u3044\u3066\u30c7\u30fc\u30bf\u3092\u30ad\u30e3\u30c3\u30b7\u30e5\u306b\u5148\u8aad\u307f\u3057\u3066\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u30d2\u30c3\u30c8\u7387\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002
        • \u7a7a\u9593\u7684\u5c40\u6240\u6027\uff1a\u7279\u5b9a\u306e\u30c7\u30fc\u30bf\u304c\u30a2\u30af\u30bb\u30b9\u3055\u308c\u308b\u3068\u3001\u8fd1\u304f\u306e\u30c7\u30fc\u30bf\u3082\u307e\u3082\u306a\u304f\u30a2\u30af\u30bb\u30b9\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u3092\u6d3b\u7528\u3059\u308b\u305f\u3081\u306b\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u8981\u6c42\u3055\u308c\u305f\u30c7\u30fc\u30bf\u3068\u4e00\u7dd2\u306b\u96a3\u63a5\u3059\u308b\u30c7\u30fc\u30bf\u3092\u30ed\u30fc\u30c9\u3057\u3001\u30d2\u30c3\u30c8\u7387\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002
        • \u6642\u9593\u7684\u5c40\u6240\u6027\uff1a\u30c7\u30fc\u30bf\u304c\u30a2\u30af\u30bb\u30b9\u3055\u308c\u305f\u5834\u5408\u3001\u8fd1\u3044\u5c06\u6765\u306b\u518d\u3073\u30a2\u30af\u30bb\u30b9\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u3053\u306e\u539f\u7406\u3092\u4f7f\u7528\u3057\u3066\u3001\u6700\u8fd1\u30a2\u30af\u30bb\u30b9\u3055\u308c\u305f\u30c7\u30fc\u30bf\u3092\u4fdd\u6301\u3057\u3066\u30d2\u30c3\u30c8\u7387\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002

        \u5b9f\u969b\u3001\u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u7570\u306a\u308b\u30ad\u30e3\u30c3\u30b7\u30e5\u5229\u7528\u52b9\u7387\u3092\u6301\u3061\u3001\u3053\u308c\u306f\u4e3b\u306b\u4ee5\u4e0b\u306e\u5074\u9762\u306b\u53cd\u6620\u3055\u308c\u307e\u3059\u3002

        • \u5360\u6709\u7a7a\u9593\uff1a\u9023\u7d50\u30ea\u30b9\u30c8\u8981\u7d20\u306f\u914d\u5217\u8981\u7d20\u3088\u308a\u3082\u591a\u304f\u306e\u7a7a\u9593\u3092\u5360\u6709\u3059\u308b\u305f\u3081\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u306b\u4fdd\u6301\u3055\u308c\u308b\u6709\u52b9\u30c7\u30fc\u30bf\u304c\u5c11\u306a\u304f\u306a\u308a\u307e\u3059\u3002
        • \u30ad\u30e3\u30c3\u30b7\u30e5\u30e9\u30a4\u30f3\uff1a\u9023\u7d50\u30ea\u30b9\u30c8\u30c7\u30fc\u30bf\u306f\u30e1\u30e2\u30ea\u5168\u4f53\u306b\u6563\u5728\u3057\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u300c\u884c\u5358\u4f4d\u3067\u30ed\u30fc\u30c9\u300d\u3055\u308c\u308b\u305f\u3081\u3001\u30ed\u30fc\u30c9\u3055\u308c\u308b\u7121\u52b9\u30c7\u30fc\u30bf\u306e\u5272\u5408\u304c\u9ad8\u304f\u306a\u308a\u307e\u3059\u3002
        • \u5148\u8aad\u307f\u30e1\u30ab\u30cb\u30ba\u30e0\uff1a\u914d\u5217\u306e\u30c7\u30fc\u30bf\u30a2\u30af\u30bb\u30b9\u30d1\u30bf\u30fc\u30f3\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u3088\u308a\u3082\u300c\u4e88\u6e2c\u53ef\u80fd\u300d\u3067\u3001\u3064\u307e\u308a\u30b7\u30b9\u30c6\u30e0\u304c\u3053\u308c\u304b\u3089\u30ed\u30fc\u30c9\u3055\u308c\u308b\u30c7\u30fc\u30bf\u3092\u63a8\u6e2c\u3057\u3084\u3059\u3044\u3067\u3059\u3002
        • \u7a7a\u9593\u7684\u5c40\u6240\u6027\uff1a\u914d\u5217\u306f\u9023\u7d9a\u3057\u305f\u30e1\u30e2\u30ea\u7a7a\u9593\u306b\u4fdd\u5b58\u3055\u308c\u308b\u305f\u3081\u3001\u30ed\u30fc\u30c9\u3055\u308c\u3066\u3044\u308b\u30c7\u30fc\u30bf\u306e\u8fd1\u304f\u306e\u30c7\u30fc\u30bf\u304c\u307e\u3082\u306a\u304f\u30a2\u30af\u30bb\u30b9\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u9ad8\u304f\u306a\u308a\u307e\u3059\u3002

        \u5168\u4f53\u7684\u306b\u3001\u914d\u5217\u306f\u3088\u308a\u9ad8\u3044\u30ad\u30e3\u30c3\u30b7\u30e5\u30d2\u30c3\u30c8\u7387\u3092\u6301\u3061\u3001\u4e00\u822c\u7684\u306b\u9023\u7d50\u30ea\u30b9\u30c8\u3088\u308a\u3082\u64cd\u4f5c\u52b9\u7387\u304c\u9ad8\u3044\u3067\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u914d\u5217\u306b\u57fa\u3065\u304f\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u306e\u89e3\u6c7a\u306b\u304a\u3044\u3066\u4eba\u6c17\u304c\u3042\u308a\u307e\u3059\u3002

        **\u9ad8\u3044\u30ad\u30e3\u30c3\u30b7\u30e5\u52b9\u7387\u304c\u914d\u5217\u304c\u5e38\u306b\u9023\u7d50\u30ea\u30b9\u30c8\u3088\u308a\u512a\u308c\u3066\u3044\u308b\u3068\u3044\u3046\u610f\u5473\u3067\u306f\u306a\u3044**\u3053\u3068\u306b\u6ce8\u610f\u3059\u3079\u304d\u3067\u3059\u3002\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u9078\u629e\u306f\u7279\u5b9a\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u8981\u4ef6\u306b\u4f9d\u5b58\u3059\u3079\u304d\u3067\u3059\u3002\u4f8b\u3048\u3070\u3001\u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u4e21\u65b9\u304c\u300c\u30b9\u30bf\u30c3\u30af\u300d\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\u304c\uff08\u6b21\u7ae0\u3067\u8a73\u7d30\u8aac\u660e\uff09\u3001\u305d\u308c\u3089\u306f\u7570\u306a\u308b\u30b7\u30ca\u30ea\u30aa\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002

        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u3067\u306f\u3001\u3088\u308a\u9ad8\u3044\u64cd\u4f5c\u52b9\u7387\u3068\u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\u6a5f\u80fd\u3092\u63d0\u4f9b\u3059\u308b\u305f\u3081\u3001\u914d\u5217\u306b\u57fa\u3065\u304f\u30b9\u30bf\u30c3\u30af\u3092\u9078\u629e\u3059\u308b\u50be\u5411\u304c\u3042\u308a\u307e\u3059\u3002\u552f\u4e00\u306e\u30b3\u30b9\u30c8\u306f\u914d\u5217\u306b\u5bfe\u3057\u3066\u4e00\u5b9a\u91cf\u306e\u30e1\u30e2\u30ea\u7a7a\u9593\u3092\u4e8b\u524d\u306b\u5272\u308a\u5f53\u3066\u308b\u5fc5\u8981\u304c\u3042\u308b\u3053\u3068\u3067\u3059\u3002
        • \u30c7\u30fc\u30bf\u91cf\u304c\u975e\u5e38\u306b\u5927\u304d\u304f\u3001\u9ad8\u5ea6\u306b\u52d5\u7684\u3067\u3001\u30b9\u30bf\u30c3\u30af\u306e\u4e88\u60f3\u30b5\u30a4\u30ba\u3092\u63a8\u5b9a\u3059\u308b\u306e\u304c\u56f0\u96e3\u306a\u5834\u5408\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u30b9\u30bf\u30c3\u30af\u304c\u3088\u308a\u826f\u3044\u9078\u629e\u3067\u3059\u3002\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u30e1\u30e2\u30ea\u306e\u7570\u306a\u308b\u90e8\u5206\u306b\u5206\u6563\u3067\u304d\u3001\u914d\u5217\u62e1\u5f35\u306e\u8ffd\u52a0\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u3092\u56de\u907f\u3067\u304d\u307e\u3059\u3002
        "},{"location":"chapter_array_and_linkedlist/summary/","title":"4.5 \u00a0 \u307e\u3068\u3081","text":""},{"location":"chapter_array_and_linkedlist/summary/#1","title":"1. \u00a0 \u91cd\u8981\u306a\u5fa9\u7fd2","text":"
        • \u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u306f2\u3064\u306e\u57fa\u672c\u7684\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3042\u308a\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30e1\u30e2\u30ea\u306b\u304a\u3051\u308b2\u3064\u306e\u683c\u7d0d\u65b9\u6cd5\u3092\u8868\u3057\u3066\u3044\u307e\u3059\uff1a\u9023\u7d9a\u7a7a\u9593\u683c\u7d0d\u3068\u975e\u9023\u7d9a\u7a7a\u9593\u683c\u7d0d\u3067\u3059\u3002\u305d\u308c\u3089\u306e\u7279\u6027\u306f\u4e92\u3044\u306b\u88dc\u5b8c\u3057\u5408\u3044\u307e\u3059\u3002
        • \u914d\u5217\u306f\u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3001\u4f7f\u7528\u3059\u308b\u30e1\u30e2\u30ea\u304c\u5c11\u306a\u3044\u4e00\u65b9\u3067\u3001\u8981\u7d20\u306e\u633f\u5165\u3068\u524a\u9664\u306f\u975e\u52b9\u7387\u7684\u3067\u3001\u521d\u671f\u5316\u5f8c\u306e\u9577\u3055\u304c\u56fa\u5b9a\u3055\u308c\u3066\u3044\u307e\u3059\u3002
        • \u9023\u7d50\u30ea\u30b9\u30c8\u306f\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u306e\u5909\u66f4\u306b\u3088\u3063\u3066\u52b9\u7387\u7684\u306a\u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664\u3092\u5b9f\u88c5\u3057\u3001\u9577\u3055\u3092\u67d4\u8edf\u306b\u8abf\u6574\u3067\u304d\u307e\u3059\u304c\u3001\u30ce\u30fc\u30c9\u30a2\u30af\u30bb\u30b9\u52b9\u7387\u304c\u4f4e\u304f\u3001\u3088\u308a\u591a\u304f\u306e\u30e1\u30e2\u30ea\u3092\u6d88\u8cbb\u3057\u307e\u3059\u3002
        • \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u4e00\u822c\u7684\u306a\u7a2e\u985e\u306b\u306f\u3001\u5358\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u5faa\u74b0\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u304c\u3042\u308a\u3001\u305d\u308c\u305e\u308c\u306b\u72ec\u81ea\u306e\u5fdc\u7528\u30b7\u30ca\u30ea\u30aa\u304c\u3042\u308a\u307e\u3059\u3002
        • \u30ea\u30b9\u30c8\u306f\u8981\u7d20\u306e\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3067\u3001\u8ffd\u52a0\u3001\u524a\u9664\u3001\u5909\u66f4\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3001\u901a\u5e38\u306f\u52d5\u7684\u914d\u5217\u306b\u57fa\u3065\u3044\u3066\u5b9f\u88c5\u3055\u308c\u3001\u914d\u5217\u306e\u5229\u70b9\u3092\u4fdd\u6301\u3057\u306a\u304c\u3089\u67d4\u8edf\u306a\u9577\u3055\u8abf\u6574\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002
        • \u30ea\u30b9\u30c8\u306e\u51fa\u73fe\u306b\u3088\u308a\u914d\u5217\u306e\u5b9f\u7528\u6027\u304c\u5927\u5e45\u306b\u5411\u4e0a\u3057\u307e\u3057\u305f\u304c\u3001\u4e00\u90e8\u306e\u30e1\u30e2\u30ea\u7a7a\u9593\u306e\u7121\u99c4\u306b\u3064\u306a\u304c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        • \u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c\u4e2d\u3001\u30c7\u30fc\u30bf\u306f\u4e3b\u306b\u30e1\u30e2\u30ea\u306b\u683c\u7d0d\u3055\u308c\u307e\u3059\u3002\u914d\u5217\u306f\u3088\u308a\u9ad8\u3044\u30e1\u30e2\u30ea\u7a7a\u9593\u52b9\u7387\u3092\u63d0\u4f9b\u3057\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u30e1\u30e2\u30ea\u4f7f\u7528\u306b\u304a\u3044\u3066\u3088\u308a\u67d4\u8edf\u3067\u3059\u3002
        • \u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u30e9\u30a4\u30f3\u3001\u5148\u8aad\u307f\u3001\u7a7a\u9593\u7684\u5c40\u6240\u6027\u3001\u6642\u9593\u7684\u5c40\u6240\u6027\u306a\u3069\u306e\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u901a\u3058\u3066CPU\u306b\u9ad8\u901f\u30c7\u30fc\u30bf\u30a2\u30af\u30bb\u30b9\u3092\u63d0\u4f9b\u3057\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c\u52b9\u7387\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002
        • \u3088\u308a\u9ad8\u3044\u30ad\u30e3\u30c3\u30b7\u30e5\u30d2\u30c3\u30c8\u7387\u306b\u3088\u308a\u3001\u914d\u5217\u306f\u4e00\u822c\u7684\u306b\u9023\u7d50\u30ea\u30b9\u30c8\u3088\u308a\u3082\u52b9\u7387\u7684\u3067\u3059\u3002\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u9078\u629e\u3059\u308b\u969b\u306f\u3001\u7279\u5b9a\u306e\u30cb\u30fc\u30ba\u3068\u30b7\u30ca\u30ea\u30aa\u306b\u57fa\u3065\u3044\u3066\u9069\u5207\u306a\u9078\u629e\u3092\u3059\u3079\u304d\u3067\u3059\u3002
        "},{"location":"chapter_array_and_linkedlist/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u914d\u5217\u3092\u30b9\u30bf\u30c3\u30af\u306b\u683c\u7d0d\u3059\u308b\u304b\u30d2\u30fc\u30d7\u306b\u683c\u7d0d\u3059\u308b\u304b\u306f\u3001\u6642\u9593\u3068\u7a7a\u9593\u52b9\u7387\u306b\u5f71\u97ff\u3057\u307e\u3059\u304b\uff1f

        \u30b9\u30bf\u30c3\u30af\u3068\u30d2\u30fc\u30d7\u306e\u4e21\u65b9\u306b\u683c\u7d0d\u3055\u308c\u308b\u914d\u5217\u306f\u9023\u7d9a\u3057\u305f\u30e1\u30e2\u30ea\u7a7a\u9593\u306b\u683c\u7d0d\u3055\u308c\u3001\u30c7\u30fc\u30bf\u64cd\u4f5c\u52b9\u7387\u306f\u672c\u8cea\u7684\u306b\u540c\u3058\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u30b9\u30bf\u30c3\u30af\u3068\u30d2\u30fc\u30d7\u306b\u306f\u72ec\u81ea\u306e\u7279\u6027\u304c\u3042\u308a\u3001\u4ee5\u4e0b\u306e\u9055\u3044\u304c\u751f\u3058\u307e\u3059\u3002

        1. \u5272\u308a\u5f53\u3066\u3068\u89e3\u653e\u52b9\u7387\uff1a\u30b9\u30bf\u30c3\u30af\u306f\u3088\u308a\u5c0f\u3055\u306a\u30e1\u30e2\u30ea\u30d6\u30ed\u30c3\u30af\u3067\u3001\u30b3\u30f3\u30d1\u30a4\u30e9\u306b\u3088\u3063\u3066\u81ea\u52d5\u7684\u306b\u5272\u308a\u5f53\u3066\u3089\u308c\u307e\u3059\u3002\u30d2\u30fc\u30d7\u30e1\u30e2\u30ea\u306f\u6bd4\u8f03\u7684\u5927\u304d\u304f\u3001\u30b3\u30fc\u30c9\u3067\u52d5\u7684\u306b\u5272\u308a\u5f53\u3066\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u65ad\u7247\u5316\u3057\u3084\u3059\u3044\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30d2\u30fc\u30d7\u3067\u306e\u5272\u308a\u5f53\u3066\u3068\u89e3\u653e\u64cd\u4f5c\u306f\u4e00\u822c\u7684\u306b\u30b9\u30bf\u30c3\u30af\u3088\u308a\u3082\u9045\u304f\u306a\u308a\u307e\u3059\u3002
        2. \u30b5\u30a4\u30ba\u5236\u9650\uff1a\u30b9\u30bf\u30c3\u30af\u30e1\u30e2\u30ea\u306f\u6bd4\u8f03\u7684\u5c0f\u3055\u304f\u3001\u30d2\u30fc\u30d7\u30b5\u30a4\u30ba\u306f\u4e00\u822c\u7684\u306b\u5229\u7528\u53ef\u80fd\u306a\u30e1\u30e2\u30ea\u306b\u3088\u3063\u3066\u5236\u9650\u3055\u308c\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30d2\u30fc\u30d7\u306f\u5927\u304d\u306a\u914d\u5217\u306e\u683c\u7d0d\u306b\u3088\u308a\u9069\u3057\u3066\u3044\u307e\u3059\u3002
        3. \u67d4\u8edf\u6027\uff1a\u30b9\u30bf\u30c3\u30af\u4e0a\u306e\u914d\u5217\u306e\u30b5\u30a4\u30ba\u306f\u30b3\u30f3\u30d1\u30a4\u30eb\u6642\u306b\u6c7a\u5b9a\u3055\u308c\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u30d2\u30fc\u30d7\u4e0a\u306e\u914d\u5217\u306e\u30b5\u30a4\u30ba\u306f\u5b9f\u884c\u6642\u306b\u52d5\u7684\u306b\u6c7a\u5b9a\u3067\u304d\u307e\u3059\u3002

        Q\uff1a\u306a\u305c\u914d\u5217\u306f\u540c\u3058\u578b\u306e\u8981\u7d20\u3092\u5fc5\u8981\u3068\u3057\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u540c\u3058\u578b\u306e\u8981\u7d20\u3092\u5f37\u8abf\u3057\u306a\u3044\u306e\u3067\u3059\u304b\uff1f

        \u9023\u7d50\u30ea\u30b9\u30c8\u306f\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u306b\u3088\u3063\u3066\u63a5\u7d9a\u3055\u308c\u305f\u30ce\u30fc\u30c9\u3067\u69cb\u6210\u3055\u308c\u3001\u5404\u30ce\u30fc\u30c9\u306fint\u3001double\u3001string\u3001object\u306a\u3069\u3001\u7570\u306a\u308b\u578b\u306e\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3067\u304d\u307e\u3059\u3002

        \u5bfe\u7167\u7684\u306b\u3001\u914d\u5217\u8981\u7d20\u306f\u540c\u3058\u578b\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u3053\u308c\u306b\u3088\u308a\u5bfe\u5fdc\u3059\u308b\u8981\u7d20\u4f4d\u7f6e\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u305f\u3081\u306e\u30aa\u30d5\u30bb\u30c3\u30c8\u3092\u8a08\u7b97\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001int\u3068long\u578b\u306e\u4e21\u65b9\u3092\u542b\u3080\u914d\u5217\u3067\u3001\u5358\u4e00\u8981\u7d20\u304c\u305d\u308c\u305e\u308c4\u30d0\u30a4\u30c8\u30688\u30d0\u30a4\u30c8\u3092\u5360\u6709\u3059\u308b\u5834\u5408\u3001\u914d\u5217\u306b2\u3064\u306e\u7570\u306a\u308b\u9577\u3055\u306e\u8981\u7d20\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u4ee5\u4e0b\u306e\u5f0f\u3092\u4f7f\u7528\u3057\u3066\u30aa\u30d5\u30bb\u30c3\u30c8\u3092\u8a08\u7b97\u3067\u304d\u307e\u305b\u3093\u3002

        # \u8981\u7d20\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9 = \u914d\u5217\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9 + \u8981\u7d20\u9577 * \u8981\u7d20\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\n

        Q\uff1a\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3057\u305f\u5f8c\u3001P.next\u3092None\u306b\u8a2d\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u304b\uff1f

        P.next\u3092\u5909\u66f4\u3057\u306a\u304f\u3066\u3082\u554f\u984c\u3042\u308a\u307e\u305b\u3093\u3002\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u89b3\u70b9\u304b\u3089\u3001\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u304b\u3089\u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u307e\u3067\u306e\u5de1\u56de\u3067P\u306b\u906d\u9047\u3059\u308b\u3053\u3068\u306f\u3082\u3046\u3042\u308a\u307e\u305b\u3093\u3002\u3053\u308c\u306f\u3001\u30ce\u30fc\u30c9P\u304c\u30ea\u30b9\u30c8\u304b\u3089\u52b9\u679c\u7684\u306b\u524a\u9664\u3055\u308c\u305f\u3053\u3068\u3092\u610f\u5473\u3057\u3001P\u304c\u6307\u3059\u5834\u6240\u306f\u3082\u306f\u3084\u30ea\u30b9\u30c8\u306b\u5f71\u97ff\u3057\u307e\u305b\u3093\u3002

        \u30ac\u30d9\u30fc\u30b8\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u89b3\u70b9\u304b\u3089\u3001Java\u3001Python\u3001Go\u306a\u3069\u306e\u81ea\u52d5\u30ac\u30d9\u30fc\u30b8\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u6301\u3064\u8a00\u8a9e\u3067\u306f\u3001\u30ce\u30fc\u30c9P\u304c\u53ce\u96c6\u3055\u308c\u308b\u304b\u3069\u3046\u304b\u306f\u3001\u305d\u308c\u3092\u6307\u3059\u53c2\u7167\u304c\u307e\u3060\u3042\u308b\u304b\u3069\u3046\u304b\u306b\u4f9d\u5b58\u3057\u3001P.next\u306e\u5024\u306b\u306f\u4f9d\u5b58\u3057\u307e\u305b\u3093\u3002C\u3084C++\u306a\u3069\u306e\u8a00\u8a9e\u3067\u306f\u3001\u30ce\u30fc\u30c9\u306e\u30e1\u30e2\u30ea\u3092\u624b\u52d5\u3067\u89e3\u653e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        Q\uff1a\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u306f\u3001\u633f\u5165\u3068\u524a\u9664\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306fO(1)\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u633f\u5165\u3084\u524a\u9664\u524d\u306e\u8981\u7d20\u691c\u7d22\u306b\u306fO(n)\u6642\u9593\u304c\u304b\u304b\u308b\u306e\u3067\u3001\u306a\u305c\u6642\u9593\u8a08\u7b97\u91cf\u306fO(n)\u3067\u306f\u306a\u3044\u306e\u3067\u3059\u304b\uff1f

        \u8981\u7d20\u3092\u6700\u521d\u306b\u691c\u7d22\u3057\u3066\u304b\u3089\u524a\u9664\u3059\u308b\u5834\u5408\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\u78ba\u304b\u306bO(n)\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u633f\u5165\u3068\u524a\u9664\u306b\u304a\u3051\u308bO(1)\u306e\u5229\u70b9\u306f\u4ed6\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u305f\u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u5b9f\u88c5\u3067\u306f\u3001\u5e38\u306b\u30d8\u30c3\u30c9\u3068\u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u3092\u6307\u3059\u30dd\u30a4\u30f3\u30bf\u3092\u7dad\u6301\u3057\u3001\u5404\u633f\u5165\u3068\u524a\u9664\u64cd\u4f5c\u3092O(1)\u306b\u3057\u307e\u3059\u3002

        Q\uff1a\u300c\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u5b9a\u7fa9\u3068\u683c\u7d0d\u65b9\u6cd5\u300d\u306e\u56f3\u3067\u3001\u8584\u9752\u8272\u306e\u683c\u7d0d\u30ce\u30fc\u30c9\u306f\u5358\u4e00\u306e\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u3092\u5360\u6709\u3057\u307e\u3059\u304b\u3001\u305d\u308c\u3068\u3082\u30ce\u30fc\u30c9\u5024\u3068\u534a\u5206\u3092\u5171\u6709\u3057\u307e\u3059\u304b\uff1f

        \u56f3\u306f\u5358\u306a\u308b\u5b9a\u6027\u7684\u306a\u8868\u73fe\u3067\u3042\u308a\u3001\u5b9a\u91cf\u7684\u5206\u6790\u306f\u7279\u5b9a\u306e\u72b6\u6cc1\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002

        • \u7570\u306a\u308b\u578b\u306e\u30ce\u30fc\u30c9\u5024\u306f\u7570\u306a\u308b\u91cf\u306e\u7a7a\u9593\u3092\u5360\u6709\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001int\u3001long\u3001double\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3067\u3059\u3002
        • \u30dd\u30a4\u30f3\u30bf\u5909\u6570\u306b\u3088\u3063\u3066\u5360\u6709\u3055\u308c\u308b\u30e1\u30e2\u30ea\u7a7a\u9593\u306f\u3001\u4f7f\u7528\u3055\u308c\u308b\u30aa\u30da\u30ec\u30fc\u30c6\u30a3\u30f3\u30b0\u30b7\u30b9\u30c6\u30e0\u3068\u30b3\u30f3\u30d1\u30a4\u30eb\u74b0\u5883\u306b\u4f9d\u5b58\u3057\u3001\u901a\u5e388\u30d0\u30a4\u30c8\u307e\u305f\u306f4\u30d0\u30a4\u30c8\u3067\u3059\u3002

        Q\uff1a\u30ea\u30b9\u30c8\u306e\u672b\u5c3e\u3078\u306e\u8981\u7d20\u8ffd\u52a0\u306f\u5e38\u306bO(1)\u3067\u3059\u304b\uff1f

        \u8981\u7d20\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3067\u30ea\u30b9\u30c8\u9577\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30ea\u30b9\u30c8\u306f\u6700\u521d\u306b\u62e1\u5f35\u3055\u308c\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u30b7\u30b9\u30c6\u30e0\u306f\u65b0\u3057\u3044\u30e1\u30e2\u30ea\u30d6\u30ed\u30c3\u30af\u3092\u8981\u6c42\u3057\u3001\u5143\u306e\u30ea\u30b9\u30c8\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u79fb\u52d5\u3059\u308b\u305f\u3081\u3001\u3053\u306e\u5834\u5408\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306fO(n)\u306b\u306a\u308a\u307e\u3059\u3002

        Q\uff1a\u300c\u30ea\u30b9\u30c8\u306e\u51fa\u73fe\u306b\u3088\u308a\u914d\u5217\u306e\u5b9f\u7528\u6027\u304c\u5927\u5e45\u306b\u5411\u4e0a\u3057\u307e\u3057\u305f\u304c\u3001\u4e00\u90e8\u306e\u30e1\u30e2\u30ea\u7a7a\u9593\u306e\u7121\u99c4\u306b\u3064\u306a\u304c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u300d\u3068\u3044\u3046\u6587\u306f\u3001\u5bb9\u91cf\u3001\u9577\u3055\u3001\u62e1\u5f35\u4fc2\u6570\u306a\u3069\u306e\u8ffd\u52a0\u5909\u6570\u306b\u3088\u3063\u3066\u5360\u6709\u3055\u308c\u308b\u30e1\u30e2\u30ea\u3092\u6307\u3057\u3066\u3044\u307e\u3059\u304b\uff1f

        \u3053\u3053\u3067\u306e\u7a7a\u9593\u306e\u7121\u99c4\u306f\u4e3b\u306b2\u3064\u306e\u5074\u9762\u3092\u6307\u3057\u307e\u3059\uff1a\u4e00\u65b9\u3067\u3001\u30ea\u30b9\u30c8\u306f\u521d\u671f\u9577\u3067\u8a2d\u5b9a\u3055\u308c\u307e\u3059\u304c\u3001\u5e38\u306b\u5fc5\u8981\u3068\u306f\u9650\u308a\u307e\u305b\u3093\u3002\u4ed6\u65b9\u3067\u3001\u983b\u7e41\u306a\u62e1\u5f35\u3092\u9632\u3050\u305f\u3081\u3001\u62e1\u5f35\u306f\u901a\u5e38\\(\\times 1.5\\)\u306a\u3069\u306e\u4fc2\u6570\u3067\u4e57\u7b97\u3055\u308c\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u591a\u304f\u306e\u7a7a\u304d\u30b9\u30ed\u30c3\u30c8\u304c\u751f\u307e\u308c\u3001\u901a\u5e38\u306f\u5b8c\u5168\u306b\u57cb\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u305b\u3093\u3002

        Q\uff1aPython\u3067n = [1, 2, 3]\u3092\u521d\u671f\u5316\u3057\u305f\u5f8c\u3001\u3053\u308c\u30893\u3064\u306e\u8981\u7d20\u306e\u30a2\u30c9\u30ec\u30b9\u306f\u9023\u7d9a\u3057\u3066\u3044\u307e\u3059\u304c\u3001m = [2, 1, 3]\u3092\u521d\u671f\u5316\u3059\u308b\u3068\u3001\u5404\u8981\u7d20\u306eid\u306f\u9023\u7d9a\u3057\u3066\u3044\u306a\u3044\u304cn\u306e\u3082\u306e\u3068\u540c\u4e00\u3067\u3059\u3002\u3053\u308c\u3089\u306e\u8981\u7d20\u306e\u30a2\u30c9\u30ec\u30b9\u304c\u9023\u7d9a\u3057\u3066\u3044\u306a\u3044\u5834\u5408\u3001m\u306f\u307e\u3060\u914d\u5217\u3067\u3059\u304b\uff1f

        \u30ea\u30b9\u30c8\u8981\u7d20\u3092\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9n = [n1, n2, n3, n4, n5]\u306b\u7f6e\u304d\u63db\u3048\u308b\u5834\u5408\u3001\u3053\u308c\u30895\u3064\u306e\u30ce\u30fc\u30c9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3082\u901a\u5e38\u30e1\u30e2\u30ea\u5168\u4f53\u306b\u5206\u6563\u3057\u3066\u3044\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u30ea\u30b9\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u4e0e\u3048\u3089\u308c\u308c\u3070\u3001O(1)\u6642\u9593\u3067\u30ce\u30fc\u30c9\u306e\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u3001\u5bfe\u5fdc\u3059\u308b\u30ce\u30fc\u30c9\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u914d\u5217\u304c\u30ce\u30fc\u30c9\u81ea\u4f53\u3067\u306f\u306a\u304f\u3001\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u3067\u3059\u3002

        \u591a\u304f\u306e\u8a00\u8a9e\u3068\u306f\u7570\u306a\u308a\u3001Python\u3067\u306f\u6570\u5024\u3082\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066\u30e9\u30c3\u30d7\u3055\u308c\u3001\u30ea\u30b9\u30c8\u306f\u6570\u5024\u81ea\u4f53\u3067\u306f\u306a\u304f\u3001\u3053\u308c\u3089\u306e\u6570\u5024\u3078\u306e\u53c2\u7167\u3092\u683c\u7d0d\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u30012\u3064\u306e\u914d\u5217\u306e\u540c\u3058\u6570\u5024\u304c\u540c\u3058id\u3092\u6301\u3061\u3001\u3053\u308c\u3089\u306e\u6570\u5024\u306e\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u306f\u9023\u7d9a\u3067\u3042\u308b\u5fc5\u8981\u304c\u306a\u3044\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002

        Q\uff1aC++ STL\u306estd::list\u306f\u3059\u3067\u306b\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u5b9f\u88c5\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u4e00\u90e8\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u66f8\u7c4d\u3067\u306f\u76f4\u63a5\u4f7f\u7528\u3057\u3066\u3044\u306a\u3044\u3088\u3046\u3067\u3059\u3002\u4f55\u304b\u5236\u9650\u304c\u3042\u308a\u307e\u3059\u304b\uff1f

        \u4e00\u65b9\u3067\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u5b9f\u88c5\u3059\u308b\u969b\u306f\u914d\u5217\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3092\u597d\u307f\u3001\u5fc5\u8981\u306a\u5834\u5408\u306e\u307f\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u4e3b\u306b2\u3064\u306e\u7406\u7531\u304c\u3042\u308a\u307e\u3059\u3002

        • \u7a7a\u9593\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\uff1a\u5404\u8981\u7d20\u306b2\u3064\u306e\u8ffd\u52a0\u30dd\u30a4\u30f3\u30bf\uff08\u524d\u306e\u8981\u7d20\u7528\u3068\u6b21\u306e\u8981\u7d20\u7528\uff09\u304c\u5fc5\u8981\u306a\u305f\u3081\u3001std::list\u306f\u901a\u5e38std::vector\u3088\u308a\u3082\u591a\u304f\u306e\u7a7a\u9593\u3092\u5360\u6709\u3057\u307e\u3059\u3002
        • \u30ad\u30e3\u30c3\u30b7\u30e5\u975e\u53cb\u597d\u7684\uff1a\u30c7\u30fc\u30bf\u304c\u9023\u7d9a\u3057\u3066\u683c\u7d0d\u3055\u308c\u3066\u3044\u306a\u3044\u305f\u3081\u3001std::list\u306f\u30ad\u30e3\u30c3\u30b7\u30e5\u5229\u7528\u7387\u304c\u4f4e\u304f\u306a\u308a\u307e\u3059\u3002\u4e00\u822c\u7684\u306b\u3001std::vector\u306e\u65b9\u304c\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u512a\u308c\u3066\u3044\u307e\u3059\u3002

        \u4ed6\u65b9\u3067\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u4e3b\u306b\u4e8c\u5206\u6728\u3068\u30b0\u30e9\u30d5\u306b\u5fc5\u8981\u3067\u3059\u3002\u30b9\u30bf\u30c3\u30af\u3068\u30ad\u30e5\u30fc\u306f\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u306f\u306a\u304f\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306estack\u3068queue\u30af\u30e9\u30b9\u3092\u4f7f\u7528\u3057\u3066\u5b9f\u88c5\u3055\u308c\u308b\u3053\u3068\u304c\u591a\u3044\u3067\u3059\u3002

        Q\uff1a\u30ea\u30b9\u30c8res = [0] * self.size()\u3092\u521d\u671f\u5316\u3059\u308b\u3068\u3001res\u306e\u5404\u8981\u7d20\u306f\u540c\u3058\u30a2\u30c9\u30ec\u30b9\u3092\u53c2\u7167\u3057\u307e\u3059\u304b\uff1f

        \u3044\u3044\u3048\u3002\u3057\u304b\u3057\u3001\u3053\u306e\u554f\u984c\u306f\u4e8c\u6b21\u5143\u914d\u5217\u3067\u767a\u751f\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u4e8c\u6b21\u5143\u30ea\u30b9\u30c8res = [[0]] * self.size()\u3092\u521d\u671f\u5316\u3059\u308b\u3068\u3001\u540c\u3058\u30ea\u30b9\u30c8[0]\u3092\u8907\u6570\u56de\u53c2\u7167\u3059\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002

        Q\uff1a\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3059\u308b\u969b\u3001\u305d\u306e\u5f8c\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\u3092\u65ad\u3064\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u304b\uff1f

        \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff08\u554f\u984c\u89e3\u6c7a\uff09\u306e\u89b3\u70b9\u304b\u3089\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u30ed\u30b8\u30c3\u30af\u304c\u6b63\u3057\u3044\u9650\u308a\u3001\u30ea\u30f3\u30af\u3092\u65ad\u305f\u306a\u304f\u3066\u3082\u554f\u984c\u3042\u308a\u307e\u305b\u3093\u3002\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u89b3\u70b9\u304b\u3089\u3001\u30ea\u30f3\u30af\u3092\u65ad\u3064\u65b9\u304c\u5b89\u5168\u3067\u8ad6\u7406\u7684\u306b\u660e\u78ba\u3067\u3059\u3002\u30ea\u30f3\u30af\u3092\u65ad\u305f\u305a\u3001\u524a\u9664\u3055\u308c\u305f\u30ce\u30fc\u30c9\u304c\u9069\u5207\u306b\u30ea\u30b5\u30a4\u30af\u30eb\u3055\u308c\u306a\u3044\u5834\u5408\u3001\u5f8c\u7d9a\u30ce\u30fc\u30c9\u306e\u30e1\u30e2\u30ea\u306e\u30ea\u30b5\u30a4\u30af\u30eb\u306b\u5f71\u97ff\u3092\u4e0e\u3048\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_backtracking/","title":"\u7b2c 13 \u7ae0 \u00a0 \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0","text":"

        Abstract

        \u8ff7\u8def\u306e\u63a2\u691c\u5bb6\u306e\u3088\u3046\u306b\u3001\u79c1\u305f\u3061\u306f\u524d\u9032\u3059\u308b\u9053\u3067\u969c\u5bb3\u306b\u906d\u9047\u3059\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002

        \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306e\u529b\u306f\u3001\u79c1\u305f\u3061\u306b\u65b0\u3057\u304f\u59cb\u3081\u308b\u3053\u3068\u3001\u8a66\u3057\u7d9a\u3051\u308b\u3053\u3068\u3001\u305d\u3057\u3066\u6700\u7d42\u7684\u306b\u5149\u3078\u306e\u51fa\u53e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002

        "},{"location":"chapter_backtracking/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 13.1 \u00a0 \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0
        • 13.2 \u00a0 \u9806\u5217\u554f\u984c
        • 13.3 \u00a0 \u90e8\u5206\u96c6\u5408\u548c\u554f\u984c
        • 13.4 \u00a0 N\u30af\u30a4\u30fc\u30f3\u554f\u984c
        • 13.5 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_backtracking/backtracking_algorithm/","title":"13.1 \u00a0 \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0","text":"

        \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u5168\u6570\u63a2\u7d22\u306b\u3088\u3063\u3066\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u65b9\u6cd5\u3067\u3059\u3002\u305d\u306e\u6838\u5fc3\u6982\u5ff5\u306f\u3001\u521d\u671f\u72b6\u614b\u304b\u3089\u958b\u59cb\u3057\u3066\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u89e3\u3092\u7dcf\u5f53\u305f\u308a\u3067\u63a2\u7d22\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u6b63\u3057\u3044\u3082\u306e\u3092\u8a18\u9332\u3057\u3001\u89e3\u304c\u898b\u3064\u304b\u308b\u304b\u3001\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u89e3\u304c\u8a66\u3055\u308c\u305f\u304c\u89e3\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002

        \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306f\u901a\u5e38\u300c\u6df1\u3055\u512a\u5148\u63a2\u7d22\u300d\u3092\u4f7f\u7528\u3057\u3066\u89e3\u7a7a\u9593\u3092\u8d70\u67fb\u3057\u307e\u3059\u3002\u300c\u4e8c\u5206\u6728\u300d\u306e\u7ae0\u3067\u3001\u524d\u9806\u3001\u4e2d\u9806\u3001\u5f8c\u9806\u8d70\u67fb\u306f\u3059\u3079\u3066\u6df1\u3055\u512a\u5148\u63a2\u7d22\u3067\u3042\u308b\u3053\u3068\u3092\u8ff0\u3079\u307e\u3057\u305f\u3002\u6b21\u306b\u3001\u524d\u9806\u8d70\u67fb\u3092\u4f7f\u7528\u3057\u3066\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u554f\u984c\u3092\u89e3\u6c7a\u3057\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52d5\u4f5c\u3092\u6bb5\u968e\u7684\u306b\u7406\u89e3\u3057\u3066\u3044\u304d\u307e\u3059\u3002

        \u4f8b1

        \u4e8c\u5206\u6728\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u5024\u304c \\(7\\) \u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3092\u691c\u7d22\u3057\u3066\u8a18\u9332\u3057\u3001\u30ea\u30b9\u30c8\u3067\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3001\u3053\u306e\u6728\u3092\u524d\u9806\u3067\u8d70\u67fb\u3057\u3001\u73fe\u5728\u306e\u30ce\u30fc\u30c9\u306e\u5024\u304c \\(7\\) \u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002\u305d\u3046\u3067\u3042\u308c\u3070\u3001\u30ce\u30fc\u30c9\u306e\u5024\u3092\u7d50\u679c\u30ea\u30b9\u30c8 res \u306b\u8ffd\u52a0\u3057\u307e\u3059\u3002\u30d7\u30ed\u30bb\u30b9\u306f\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_i_compact.py
        def pre_order(root: TreeNode):\n    \"\"\"\u524d\u9806\u8d70\u67fb\uff1a\u4f8b\u4e00\"\"\"\n    if root is None:\n        return\n    if root.val == 7:\n        # \u89e3\u3092\u8a18\u9332\n        res.append(root)\n    pre_order(root.left)\n    pre_order(root.right)\n
        preorder_traversal_i_compact.cpp
        /* \u524d\u9806\u8d70\u67fb\uff1a\u4f8b\uff11 */\nvoid preOrder(TreeNode *root) {\n    if (root == nullptr) {\n        return;\n    }\n    if (root->val == 7) {\n        // \u89e3\u3092\u8a18\u9332\n        res.push_back(root);\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n}\n
        preorder_traversal_i_compact.java
        /* \u524d\u9806\u8d70\u67fb\uff1a\u4f8b\uff11 */\nvoid preOrder(TreeNode root) {\n    if (root == null) {\n        return;\n    }\n    if (root.val == 7) {\n        // \u89e3\u3092\u8a18\u9332\n        res.add(root);\n    }\n    preOrder(root.left);\n    preOrder(root.right);\n}\n
        preorder_traversal_i_compact.cs
        [class]{preorder_traversal_i_compact}-[func]{PreOrder}\n
        preorder_traversal_i_compact.go
        [class]{}-[func]{preOrderI}\n
        preorder_traversal_i_compact.swift
        [class]{}-[func]{preOrder}\n
        preorder_traversal_i_compact.js
        [class]{}-[func]{preOrder}\n
        preorder_traversal_i_compact.ts
        [class]{}-[func]{preOrder}\n
        preorder_traversal_i_compact.dart
        [class]{}-[func]{preOrder}\n
        preorder_traversal_i_compact.rs
        [class]{}-[func]{pre_order}\n
        preorder_traversal_i_compact.c
        [class]{}-[func]{preOrder}\n
        preorder_traversal_i_compact.kt
        [class]{}-[func]{preOrder}\n
        preorder_traversal_i_compact.rb
        [class]{}-[func]{pre_order}\n

        \u56f3 13-1 \u00a0 \u524d\u9806\u8d70\u67fb\u3067\u306e\u30ce\u30fc\u30c9\u691c\u7d22

        "},{"location":"chapter_backtracking/backtracking_algorithm/#1311","title":"13.1.1 \u00a0 \u8a66\u884c\u3068\u5f8c\u9000","text":"

        \u89e3\u7a7a\u9593\u3092\u63a2\u7d22\u3059\u308b\u969b\u306b\u300c\u8a66\u884c\u300d\u3068\u300c\u5f8c\u9000\u300d\u6226\u7565\u3092\u4f7f\u7528\u3059\u308b\u305f\u3081\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u547c\u3070\u308c\u307e\u3059\u3002\u63a2\u7d22\u4e2d\u3001\u6e80\u8db3\u306e\u3044\u304f\u89e3\u3092\u5f97\u308b\u305f\u3081\u306b\u3082\u306f\u3084\u9032\u3081\u306a\u3044\u72b6\u614b\u306b\u906d\u9047\u3059\u308b\u305f\u3073\u306b\u3001\u524d\u306e\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3066\u524d\u306e\u72b6\u614b\u306b\u623b\u308a\u3001\u6b21\u306e\u8a66\u884c\u306e\u305f\u3081\u306b\u4ed6\u306e\u53ef\u80fd\u306a\u9078\u629e\u3092\u9078\u629e\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002

        \u4f8b1\u3067\u306f\u3001\u5404\u30ce\u30fc\u30c9\u306e\u8a2a\u554f\u304c\u300c\u8a66\u884c\u300d\u3092\u958b\u59cb\u3057\u307e\u3059\u3002\u305d\u3057\u3066\u8449\u30ce\u30fc\u30c9\u3092\u901a\u904e\u3059\u308b\u304b\u3001return \u6587\u3067\u89aa\u30ce\u30fc\u30c9\u306b\u623b\u308b\u3053\u3068\u304c\u300c\u5f8c\u9000\u300d\u3092\u793a\u5506\u3057\u307e\u3059\u3002

        \u5f8c\u9000\u306f\u5358\u306b\u95a2\u6570\u306e\u623b\u308a\u5024\u3067\u306f\u306a\u3044\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4f8b1\u306e\u554f\u984c\u3092\u5c11\u3057\u62e1\u5f35\u3057\u3066\u3001\u305d\u308c\u304c\u4f55\u3092\u610f\u5473\u3059\u308b\u304b\u3092\u8aac\u660e\u3057\u307e\u3059\u3002

        \u4f8b2

        \u4e8c\u5206\u6728\u3067\u3001\u5024\u304c \\(7\\) \u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3092\u691c\u7d22\u3057\u3001\u3059\u3079\u3066\u306e\u30de\u30c3\u30c1\u30f3\u30b0\u30ce\u30fc\u30c9\u306b\u3064\u3044\u3066\u3001\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u304b\u3089\u305d\u306e\u30ce\u30fc\u30c9\u307e\u3067\u306e\u30d1\u30b9\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u4f8b1\u306e\u30b3\u30fc\u30c9\u306b\u57fa\u3065\u3044\u3066\u3001\u8a2a\u554f\u3057\u305f\u30ce\u30fc\u30c9\u30d1\u30b9\u3092\u8a18\u9332\u3059\u308b\u305f\u3081\u306b path \u3068\u3044\u3046\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u5024\u304c \\(7\\) \u306e\u30ce\u30fc\u30c9\u306b\u5230\u9054\u3059\u308b\u3068\u3001path \u3092\u30b3\u30d4\u30fc\u3057\u3066\u7d50\u679c\u30ea\u30b9\u30c8 res \u306b\u8ffd\u52a0\u3057\u307e\u3059\u3002\u8d70\u67fb\u5f8c\u3001res \u306b\u306f\u3059\u3079\u3066\u306e\u89e3\u304c\u4fdd\u6301\u3055\u308c\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_ii_compact.py
        def pre_order(root: TreeNode):\n    \"\"\"\u524d\u9806\u8d70\u67fb\uff1a\u4f8b\u4e8c\"\"\"\n    if root is None:\n        return\n    # \u8a66\u884c\n    path.append(root)\n    if root.val == 7:\n        # \u89e3\u3092\u8a18\u9332\n        res.append(list(path))\n    pre_order(root.left)\n    pre_order(root.right)\n    # \u64a4\u56de\n    path.pop()\n
        preorder_traversal_ii_compact.cpp
        /* \u524d\u9806\u8d70\u67fb\uff1a\u4f8b\uff12 */\nvoid preOrder(TreeNode *root) {\n    if (root == nullptr) {\n        return;\n    }\n    // \u8a66\u884c\n    path.push_back(root);\n    if (root->val == 7) {\n        // \u89e3\u3092\u8a18\u9332\n        res.push_back(path);\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // \u56de\u9000\n    path.pop_back();\n}\n
        preorder_traversal_ii_compact.java
        /* \u524d\u9806\u8d70\u67fb\uff1a\u4f8b\uff12 */\nvoid preOrder(TreeNode root) {\n    if (root == null) {\n        return;\n    }\n    // \u8a66\u884c\n    path.add(root);\n    if (root.val == 7) {\n        // \u89e3\u3092\u8a18\u9332\n        res.add(new ArrayList<>(path));\n    }\n    preOrder(root.left);\n    preOrder(root.right);\n    // \u56de\u9000\n    path.remove(path.size() - 1);\n}\n
        preorder_traversal_ii_compact.cs
        [class]{preorder_traversal_ii_compact}-[func]{PreOrder}\n
        preorder_traversal_ii_compact.go
        [class]{}-[func]{preOrderII}\n
        preorder_traversal_ii_compact.swift
        [class]{}-[func]{preOrder}\n
        preorder_traversal_ii_compact.js
        [class]{}-[func]{preOrder}\n
        preorder_traversal_ii_compact.ts
        [class]{}-[func]{preOrder}\n
        preorder_traversal_ii_compact.dart
        [class]{}-[func]{preOrder}\n
        preorder_traversal_ii_compact.rs
        [class]{}-[func]{pre_order}\n
        preorder_traversal_ii_compact.c
        [class]{}-[func]{preOrder}\n
        preorder_traversal_ii_compact.kt
        [class]{}-[func]{preOrder}\n
        preorder_traversal_ii_compact.rb
        [class]{}-[func]{pre_order}\n

        \u5404\u300c\u8a66\u884c\u300d\u3067\u3001\u73fe\u5728\u306e\u30ce\u30fc\u30c9\u3092 path \u306b\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3067\u30d1\u30b9\u3092\u8a18\u9332\u3057\u307e\u3059\u3002\u300c\u5f8c\u9000\u300d\u304c\u5fc5\u8981\u306a\u3068\u304d\u306f\u3044\u3064\u3067\u3082\u3001path \u304b\u3089\u30ce\u30fc\u30c9\u3092\u30dd\u30c3\u30d7\u3057\u3066**\u3053\u306e\u5931\u6557\u3057\u305f\u8a66\u884c\u524d\u306e\u72b6\u614b\u3092\u5fa9\u5143\u3057\u307e\u3059**\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u30d7\u30ed\u30bb\u30b9\u3092\u89b3\u5bdf\u3059\u308b\u3053\u3068\u3067\u3001\u8a66\u884c\u306f\u300c\u524d\u9032\u300d\u306e\u3088\u3046\u3067\u3001\u5f8c\u9000\u306f\u300c\u5143\u306b\u623b\u3059\u300d\u306e\u3088\u3046\u3067\u3059\u3002\u5f8c\u8005\u306e\u30da\u30a2\u306f\u3001\u5bfe\u5fdc\u3059\u308b\u3082\u306e\u306b\u5bfe\u3059\u308b\u9006\u64cd\u4f5c\u3068\u898b\u306a\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        <1><2><3><4><5><6><7><8><9><10><11>

        \u56f3 13-2 \u00a0 \u8a66\u884c\u3068\u5f8c\u9000

        "},{"location":"chapter_backtracking/backtracking_algorithm/#1312","title":"13.1.2 \u00a0 \u526a\u5b9a","text":"

        \u8907\u96d1\u306a\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u554f\u984c\u306f\u901a\u5e381\u3064\u4ee5\u4e0a\u306e\u5236\u7d04\u3092\u542b\u307f\u3001\u3053\u308c\u3089\u306f\u300c\u526a\u5b9a\u300d\u306b\u3088\u304f\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002

        \u4f8b3

        \u4e8c\u5206\u6728\u3067\u3001\u5024\u304c \\(7\\) \u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3092\u691c\u7d22\u3057\u3001\u30eb\u30fc\u30c8\u304b\u3089\u3053\u308c\u3089\u306e\u30ce\u30fc\u30c9\u307e\u3067\u306e\u30d1\u30b9\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u305f\u3060\u3057\u3001\u30d1\u30b9\u306b\u306f\u5024\u304c \\(3\\) \u306e\u30ce\u30fc\u30c9\u3092\u542b\u307e\u306a\u3044\u3068\u3044\u3046\u5236\u9650\u304c\u3042\u308a\u307e\u3059\u3002

        \u4e0a\u8a18\u306e\u5236\u7d04\u3092\u6e80\u305f\u3059\u305f\u3081\u306b\u3001\u526a\u5b9a\u64cd\u4f5c\u3092\u8ffd\u52a0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff1a\u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u4e2d\u306b\u3001\u5024\u304c \\(3\\) \u306e\u30ce\u30fc\u30c9\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u305d\u306e\u30d1\u30b9\u3092\u901a\u3058\u3066\u3055\u3089\u306b\u691c\u7d22\u3059\u308b\u3053\u3068\u3092\u5373\u5ea7\u306b\u4e2d\u6b62\u3057\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_iii_compact.py
        def pre_order(root: TreeNode):\n    \"\"\"\u524d\u9806\u8d70\u67fb\uff1a\u4f8b\u4e09\"\"\"\n    # \u679d\u5208\u308a\n    if root is None or root.val == 3:\n        return\n    # \u8a66\u884c\n    path.append(root)\n    if root.val == 7:\n        # \u89e3\u3092\u8a18\u9332\n        res.append(list(path))\n    pre_order(root.left)\n    pre_order(root.right)\n    # \u64a4\u56de\n    path.pop()\n
        preorder_traversal_iii_compact.cpp
        /* \u524d\u9806\u8d70\u67fb\uff1a\u4f8b\uff13 */\nvoid preOrder(TreeNode *root) {\n    // \u526a\u5b9a\n    if (root == nullptr || root->val == 3) {\n        return;\n    }\n    // \u8a66\u884c\n    path.push_back(root);\n    if (root->val == 7) {\n        // \u89e3\u3092\u8a18\u9332\n        res.push_back(path);\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // \u56de\u9000\n    path.pop_back();\n}\n
        preorder_traversal_iii_compact.java
        /* \u524d\u9806\u8d70\u67fb\uff1a\u4f8b\uff13 */\nvoid preOrder(TreeNode root) {\n    // \u526a\u5b9a\n    if (root == null || root.val == 3) {\n        return;\n    }\n    // \u8a66\u884c\n    path.add(root);\n    if (root.val == 7) {\n        // \u89e3\u3092\u8a18\u9332\n        res.add(new ArrayList<>(path));\n    }\n    preOrder(root.left);\n    preOrder(root.right);\n    // \u56de\u9000\n    path.remove(path.size() - 1);\n}\n
        preorder_traversal_iii_compact.cs
        [class]{preorder_traversal_iii_compact}-[func]{PreOrder}\n
        preorder_traversal_iii_compact.go
        [class]{}-[func]{preOrderIII}\n
        preorder_traversal_iii_compact.swift
        [class]{}-[func]{preOrder}\n
        preorder_traversal_iii_compact.js
        [class]{}-[func]{preOrder}\n
        preorder_traversal_iii_compact.ts
        [class]{}-[func]{preOrder}\n
        preorder_traversal_iii_compact.dart
        [class]{}-[func]{preOrder}\n
        preorder_traversal_iii_compact.rs
        [class]{}-[func]{pre_order}\n
        preorder_traversal_iii_compact.c
        [class]{}-[func]{preOrder}\n
        preorder_traversal_iii_compact.kt
        [class]{}-[func]{preOrder}\n
        preorder_traversal_iii_compact.rb
        [class]{}-[func]{pre_order}\n

        \u300c\u526a\u5b9a\u300d\u306f\u975e\u5e38\u306b\u751f\u304d\u751f\u304d\u3068\u3057\u305f\u540d\u8a5e\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u3067\u3001\u5236\u7d04\u3092\u6e80\u305f\u3055\u306a\u3044\u691c\u7d22\u5206\u5c90\u3092\u300c\u5207\u308a\u53d6\u308a\u300d\u307e\u3059\u3002\u3055\u3089\u306a\u308b\u4e0d\u8981\u306a\u8a66\u884c\u3092\u907f\u3051\u3001\u691c\u7d22\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002

        \u56f3 13-3 \u00a0 \u5236\u7d04\u306b\u57fa\u3065\u304f\u526a\u5b9a

        "},{"location":"chapter_backtracking/backtracking_algorithm/#1313","title":"13.1.3 \u00a0 \u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u30b3\u30fc\u30c9","text":"

        \u4eca\u5ea6\u306f\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u304b\u3089\u300c\u8a66\u884c\u3001\u5f8c\u9000\u3001\u526a\u5b9a\u300d\u306e\u4e3b\u8981\u306a\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u62bd\u51fa\u3057\u3066\u3001\u30b3\u30fc\u30c9\u306e\u6c4e\u7528\u6027\u3092\u5411\u4e0a\u3055\u305b\u3066\u307f\u307e\u3057\u3087\u3046\u3002

        \u4ee5\u4e0b\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u30b3\u30fc\u30c9\u3067\u306f\u3001state \u306f\u554f\u984c\u306e\u73fe\u5728\u306e\u72b6\u614b\u3092\u8868\u3057\u3001choices \u306f\u73fe\u5728\u306e\u72b6\u614b\u3067\u5229\u7528\u53ef\u80fd\u306a\u9078\u629e\u80a2\u3092\u8868\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        def backtrack(state: State, choices: list[choice], res: list[state]):\n    \"\"\"\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\"\"\"\n    # \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if is_solution(state):\n        # \u89e3\u3092\u8a18\u9332\n        record_solution(state, res)\n        # \u691c\u7d22\u3092\u505c\u6b62\n        return\n    # \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    for choice in choices:\n        # \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if is_valid(state, choice):\n            # \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            make_choice(state, choice)\n            backtrack(state, choices, res)\n            # \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            undo_choice(state, choice)\n
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af */\nvoid backtrack(State *state, vector<Choice *> &choices, vector<State *> &res) {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if (isSolution(state)) {\n        // \u89e3\u3092\u8a18\u9332\n        recordSolution(state, res);\n        // \u691c\u7d22\u3092\u505c\u6b62\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    for (Choice choice : choices) {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if (isValid(state, choice)) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            undoChoice(state, choice);\n        }\n    }\n}\n
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af */\nvoid backtrack(State state, List<Choice> choices, List<State> res) {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if (isSolution(state)) {\n        // \u89e3\u3092\u8a18\u9332\n        recordSolution(state, res);\n        // \u691c\u7d22\u3092\u505c\u6b62\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    for (Choice choice : choices) {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if (isValid(state, choice)) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            undoChoice(state, choice);\n        }\n    }\n}\n
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af */\nvoid Backtrack(State state, List<Choice> choices, List<State> res) {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if (IsSolution(state)) {\n        // \u89e3\u3092\u8a18\u9332\n        RecordSolution(state, res);\n        // \u691c\u7d22\u3092\u505c\u6b62\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    foreach (Choice choice in choices) {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if (IsValid(state, choice)) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            MakeChoice(state, choice);\n            Backtrack(state, choices, res);\n            // \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            UndoChoice(state, choice);\n        }\n    }\n}\n
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af */\nfunc backtrack(state *State, choices []Choice, res *[]State) {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if isSolution(state) {\n        // \u89e3\u3092\u8a18\u9332\n        recordSolution(state, res)\n        // \u691c\u7d22\u3092\u505c\u6b62\n        return\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    for _, choice := range choices {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if isValid(state, choice) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            makeChoice(state, choice)\n            backtrack(state, choices, res)\n            // \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            undoChoice(state, choice)\n        }\n    }\n}\n
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af */\nfunc backtrack(state: inout State, choices: [Choice], res: inout [State]) {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if isSolution(state: state) {\n        // \u89e3\u3092\u8a18\u9332\n        recordSolution(state: state, res: &res)\n        // \u691c\u7d22\u3092\u505c\u6b62\n        return\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    for choice in choices {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if isValid(state: state, choice: choice) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            makeChoice(state: &state, choice: choice)\n            backtrack(state: &state, choices: choices, res: &res)\n            // \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            undoChoice(state: &state, choice: choice)\n        }\n    }\n}\n
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af */\nfunction backtrack(state, choices, res) {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if (isSolution(state)) {\n        // \u89e3\u3092\u8a18\u9332\n        recordSolution(state, res);\n        // \u691c\u7d22\u3092\u505c\u6b62\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    for (let choice of choices) {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if (isValid(state, choice)) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            undoChoice(state, choice);\n        }\n    }\n}\n
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af */\nfunction backtrack(state: State, choices: Choice[], res: State[]): void {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if (isSolution(state)) {\n        // \u89e3\u3092\u8a18\u9332\n        recordSolution(state, res);\n        // \u691c\u7d22\u3092\u505c\u6b62\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    for (let choice of choices) {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if (isValid(state, choice)) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            undoChoice(state, choice);\n        }\n    }\n}\n
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af */\nvoid backtrack(State state, List<Choice>, List<State> res) {\n  // \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n  if (isSolution(state)) {\n    // \u89e3\u3092\u8a18\u9332\n    recordSolution(state, res);\n    // \u691c\u7d22\u3092\u505c\u6b62\n    return;\n  }\n  // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n  for (Choice choice in choices) {\n    // \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if (isValid(state, choice)) {\n      // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n      makeChoice(state, choice);\n      backtrack(state, choices, res);\n      // \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n      undoChoice(state, choice);\n    }\n  }\n}\n
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af */\nfn backtrack(state: &mut State, choices: &Vec<Choice>, res: &mut Vec<State>) {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if is_solution(state) {\n        // \u89e3\u3092\u8a18\u9332\n        record_solution(state, res);\n        // \u691c\u7d22\u3092\u505c\u6b62\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    for choice in choices {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if is_valid(state, choice) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            make_choice(state, choice);\n            backtrack(state, choices, res);\n            // \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            undo_choice(state, choice);\n        }\n    }\n}\n
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af */\nvoid backtrack(State *state, Choice *choices, int numChoices, State *res, int numRes) {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if (isSolution(state)) {\n        // \u89e3\u3092\u8a18\u9332\n        recordSolution(state, res, numRes);\n        // \u691c\u7d22\u3092\u505c\u6b62\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    for (int i = 0; i < numChoices; i++) {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if (isValid(state, &choices[i])) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            makeChoice(state, &choices[i]);\n            backtrack(state, choices, numChoices, res, numRes);\n            // \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            undoChoice(state, &choices[i]);\n        }\n    }\n}\n
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af */\nfun backtrack(state: State?, choices: List<Choice?>, res: List<State?>?) {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if (isSolution(state)) {\n        // \u89e3\u3092\u8a18\u9332\n        recordSolution(state, res)\n        // \u691c\u7d22\u3092\u505c\u6b62\n        return\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    for (choice in choices) {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if (isValid(state, choice)) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            makeChoice(state, choice)\n            backtrack(state, choices, res)\n            // \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            undoChoice(state, choice)\n        }\n    }\n}\n
        ### \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af ###\ndef backtrack(state, choices, res)\n    # \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if is_solution?(state)\n        # \u89e3\u3092\u8a18\u9332\n        record_solution(state, res)\n        return\n    end\n\n    # \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    for choice in choices\n        # \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if is_valid?(state, choice)\n            # \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            make_choice(state, choice)\n            backtrack(state, choices, res)\n            # \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            undo_choice(state, choice)\n        end\n    end\nend\n

        \u6b21\u306b\u3001\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u30b3\u30fc\u30c9\u306b\u57fa\u3065\u3044\u3066\u4f8b\u984c 3 \u3092\u89e3\u304d\u307e\u3059\u3002\u72b6\u614b state \u306f\u30ce\u30fc\u30c9\u306e\u8d70\u67fb\u7d4c\u8def\u3092\u8868\u3057\u3001\u9078\u629e\u80a2 choices \u306f\u73fe\u5728\u30ce\u30fc\u30c9\u306e\u5de6\u5b50\u30ce\u30fc\u30c9\u3068\u53f3\u5b50\u30ce\u30fc\u30c9\u3001\u7d50\u679c res \u306f\u7d4c\u8def\u30ea\u30b9\u30c8\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_iii_template.py
        def is_solution(state: list[TreeNode]) -> bool:\n    \"\"\"\u73fe\u5728\u306e\u72b6\u614b\u304c\u89e3\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\"\"\"\n    return state and state[-1].val == 7\n\ndef record_solution(state: list[TreeNode], res: list[list[TreeNode]]):\n    \"\"\"\u89e3\u3092\u8a18\u9332\"\"\"\n    res.append(list(state))\n\ndef is_valid(state: list[TreeNode], choice: TreeNode) -> bool:\n    \"\"\"\u73fe\u5728\u306e\u72b6\u614b\u4e0b\u3067\u9078\u629e\u304c\u5408\u6cd5\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\"\"\"\n    return choice is not None and choice.val != 3\n\ndef make_choice(state: list[TreeNode], choice: TreeNode):\n    \"\"\"\u72b6\u614b\u3092\u66f4\u65b0\"\"\"\n    state.append(choice)\n\ndef undo_choice(state: list[TreeNode], choice: TreeNode):\n    \"\"\"\u72b6\u614b\u3092\u5fa9\u5143\"\"\"\n    state.pop()\n\ndef backtrack(\n    state: list[TreeNode], choices: list[TreeNode], res: list[list[TreeNode]]\n):\n    \"\"\"\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u4f8b\u4e09\"\"\"\n    # \u89e3\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\n    if is_solution(state):\n        # \u89e3\u3092\u8a18\u9332\n        record_solution(state, res)\n    # \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for choice in choices:\n        # \u679d\u5208\u308a\uff1a\u9078\u629e\u304c\u5408\u6cd5\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\n        if is_valid(state, choice):\n            # \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            make_choice(state, choice)\n            # \u6b21\u306e\u9078\u629e\u30e9\u30a6\u30f3\u30c9\u306b\u9032\u3080\n            backtrack(state, [choice.left, choice.right], res)\n            # \u64a4\u56de\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n            undo_choice(state, choice)\n
        preorder_traversal_iii_template.cpp
        /* \u73fe\u5728\u306e\u72b6\u614b\u304c\u89e3\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\nbool isSolution(vector<TreeNode *> &state) {\n    return !state.empty() && state.back()->val == 7;\n}\n\n/* \u89e3\u3092\u8a18\u9332 */\nvoid recordSolution(vector<TreeNode *> &state, vector<vector<TreeNode *>> &res) {\n    res.push_back(state);\n}\n\n/* \u73fe\u5728\u306e\u72b6\u614b\u4e0b\u3067\u9078\u629e\u304c\u5408\u6cd5\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\nbool isValid(vector<TreeNode *> &state, TreeNode *choice) {\n    return choice != nullptr && choice->val != 3;\n}\n\n/* \u72b6\u614b\u3092\u66f4\u65b0 */\nvoid makeChoice(vector<TreeNode *> &state, TreeNode *choice) {\n    state.push_back(choice);\n}\n\n/* \u72b6\u614b\u3092\u5fa9\u5143 */\nvoid undoChoice(vector<TreeNode *> &state, TreeNode *choice) {\n    state.pop_back();\n}\n\n/* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u4f8b\uff13 */\nvoid backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<vector<TreeNode *>> &res) {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\n    if (isSolution(state)) {\n        // \u89e3\u3092\u8a18\u9332\n        recordSolution(state, res);\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for (TreeNode *choice : choices) {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u304c\u5408\u6cd5\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\n        if (isValid(state, choice)) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            makeChoice(state, choice);\n            // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n            vector<TreeNode *> nextChoices{choice->left, choice->right};\n            backtrack(state, nextChoices, res);\n            // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n            undoChoice(state, choice);\n        }\n    }\n}\n
        preorder_traversal_iii_template.java
        /* \u73fe\u5728\u306e\u72b6\u614b\u304c\u89e3\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\nboolean isSolution(List<TreeNode> state) {\n    return !state.isEmpty() && state.get(state.size() - 1).val == 7;\n}\n\n/* \u89e3\u3092\u8a18\u9332 */\nvoid recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n    res.add(new ArrayList<>(state));\n}\n\n/* \u73fe\u5728\u306e\u72b6\u614b\u4e0b\u3067\u9078\u629e\u304c\u5408\u6cd5\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\nboolean isValid(List<TreeNode> state, TreeNode choice) {\n    return choice != null && choice.val != 3;\n}\n\n/* \u72b6\u614b\u3092\u66f4\u65b0 */\nvoid makeChoice(List<TreeNode> state, TreeNode choice) {\n    state.add(choice);\n}\n\n/* \u72b6\u614b\u3092\u5fa9\u5143 */\nvoid undoChoice(List<TreeNode> state, TreeNode choice) {\n    state.remove(state.size() - 1);\n}\n\n/* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u4f8b\uff13 */\nvoid backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\n    if (isSolution(state)) {\n        // \u89e3\u3092\u8a18\u9332\n        recordSolution(state, res);\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for (TreeNode choice : choices) {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u304c\u5408\u6cd5\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\n        if (isValid(state, choice)) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            makeChoice(state, choice);\n            // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n            backtrack(state, Arrays.asList(choice.left, choice.right), res);\n            // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n            undoChoice(state, choice);\n        }\n    }\n}\n
        preorder_traversal_iii_template.cs
        [class]{preorder_traversal_iii_template}-[func]{IsSolution}\n\n[class]{preorder_traversal_iii_template}-[func]{RecordSolution}\n\n[class]{preorder_traversal_iii_template}-[func]{IsValid}\n\n[class]{preorder_traversal_iii_template}-[func]{MakeChoice}\n\n[class]{preorder_traversal_iii_template}-[func]{UndoChoice}\n\n[class]{preorder_traversal_iii_template}-[func]{Backtrack}\n
        preorder_traversal_iii_template.go
        [class]{}-[func]{isSolution}\n\n[class]{}-[func]{recordSolution}\n\n[class]{}-[func]{isValid}\n\n[class]{}-[func]{makeChoice}\n\n[class]{}-[func]{undoChoice}\n\n[class]{}-[func]{backtrackIII}\n
        preorder_traversal_iii_template.swift
        [class]{}-[func]{isSolution}\n\n[class]{}-[func]{recordSolution}\n\n[class]{}-[func]{isValid}\n\n[class]{}-[func]{makeChoice}\n\n[class]{}-[func]{undoChoice}\n\n[class]{}-[func]{backtrack}\n
        preorder_traversal_iii_template.js
        [class]{}-[func]{isSolution}\n\n[class]{}-[func]{recordSolution}\n\n[class]{}-[func]{isValid}\n\n[class]{}-[func]{makeChoice}\n\n[class]{}-[func]{undoChoice}\n\n[class]{}-[func]{backtrack}\n
        preorder_traversal_iii_template.ts
        [class]{}-[func]{isSolution}\n\n[class]{}-[func]{recordSolution}\n\n[class]{}-[func]{isValid}\n\n[class]{}-[func]{makeChoice}\n\n[class]{}-[func]{undoChoice}\n\n[class]{}-[func]{backtrack}\n
        preorder_traversal_iii_template.dart
        [class]{}-[func]{isSolution}\n\n[class]{}-[func]{recordSolution}\n\n[class]{}-[func]{isValid}\n\n[class]{}-[func]{makeChoice}\n\n[class]{}-[func]{undoChoice}\n\n[class]{}-[func]{backtrack}\n
        preorder_traversal_iii_template.rs
        [class]{}-[func]{is_solution}\n\n[class]{}-[func]{record_solution}\n\n[class]{}-[func]{is_valid}\n\n[class]{}-[func]{make_choice}\n\n[class]{}-[func]{undo_choice}\n\n[class]{}-[func]{backtrack}\n
        preorder_traversal_iii_template.c
        [class]{}-[func]{isSolution}\n\n[class]{}-[func]{recordSolution}\n\n[class]{}-[func]{isValid}\n\n[class]{}-[func]{makeChoice}\n\n[class]{}-[func]{undoChoice}\n\n[class]{}-[func]{backtrack}\n
        preorder_traversal_iii_template.kt
        [class]{}-[func]{isSolution}\n\n[class]{}-[func]{recordSolution}\n\n[class]{}-[func]{isValid}\n\n[class]{}-[func]{makeChoice}\n\n[class]{}-[func]{undoChoice}\n\n[class]{}-[func]{backtrack}\n
        preorder_traversal_iii_template.rb
        [class]{}-[func]{is_solution}\n\n[class]{}-[func]{record_solution}\n\n[class]{}-[func]{is_valid}\n\n[class]{}-[func]{make_choice}\n\n[class]{}-[func]{undo_choice}\n\n[class]{}-[func]{backtrack}\n

        \u554f\u984c\u6587\u306e\u610f\u5473\u306b\u5f93\u3044\u3001\u5024\u304c \\(7\\) \u306e\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u305f\u5f8c\u3082\u63a2\u7d22\u3092\u7d9a\u3051\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u89e3\u3092\u8a18\u9332\u3057\u305f\u5f8c\u306e return \u6587\u3092\u524a\u9664\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u6b21\u306e\u56f3\u306f\u3001return \u6587\u3092\u4fdd\u6301\u3059\u308b\u5834\u5408\u3068\u524a\u9664\u3059\u308b\u5834\u5408\u306e\u63a2\u7d22\u904e\u7a0b\u306e\u6bd4\u8f03\u3067\u3059\u3002

        \u56f3 13-4 \u00a0 return\u3092\u4fdd\u6301\u3059\u308b\u5834\u5408\u3068\u524a\u9664\u3059\u308b\u5834\u5408\u306e\u63a2\u7d22\u904e\u7a0b\u306e\u6bd4\u8f03

        \u524d\u9806\u8d70\u67fb\u306b\u57fa\u3065\u304f\u30b3\u30fc\u30c9\u5b9f\u88c5\u3068\u6bd4\u3079\u308b\u3068\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306b\u57fa\u3065\u304f\u5b9f\u88c5\u306f\u3084\u3084\u5197\u9577\u306b\u898b\u3048\u307e\u3059\u304c\u3001\u6c4e\u7528\u6027\u306f\u3088\u308a\u9ad8\u3044\u3067\u3059\u3002\u5b9f\u969b\u3001\u591a\u304f\u306e\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u554f\u984c\u306f\u3053\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306e\u4e0b\u3067\u89e3\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u5177\u4f53\u7684\u306a\u554f\u984c\u306b\u5fdc\u3058\u3066 state \u3068 choices \u3092\u5b9a\u7fa9\u3057\u3001\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u5185\u306e\u5404\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u88c5\u3059\u308c\u3070\u3088\u3044\u306e\u3067\u3059\u3002

        "},{"location":"chapter_backtracking/backtracking_algorithm/#1314","title":"13.1.4 \u00a0 \u3088\u304f\u4f7f\u308f\u308c\u308b\u7528\u8a9e","text":"

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u3092\u3088\u308a\u660e\u78ba\u306b\u5206\u6790\u3059\u308b\u305f\u3081\u306b\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3088\u304f\u4f7f\u308f\u308c\u308b\u7528\u8a9e\u306e\u610f\u5473\u3092\u307e\u3068\u3081\u3001\u4f8b\u984c 3 \u306e\u5bfe\u5fdc\u4f8b\u3092\u4ee5\u4e0b\u306e\u8868\u306b\u793a\u3057\u307e\u3059\u3002

        \u8868 13-1 \u00a0 \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3088\u304f\u4f7f\u308f\u308c\u308b\u7528\u8a9e

        \u540d\u79f0 \u5b9a\u7fa9 \u4f8b\u984c 3 \u89e3\uff08solution\uff09 \u89e3\u306f\u554f\u984c\u306e\u7279\u5b9a\u6761\u4ef6\u3092\u6e80\u305f\u3059\u7b54\u3048\u3067\u3042\u308a\u30011 \u3064\u307e\u305f\u306f\u8907\u6570\u5b58\u5728\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b \u6839\u30ce\u30fc\u30c9\u304b\u3089\u30ce\u30fc\u30c9 \\(7\\) \u307e\u3067\u306e\u5236\u7d04\u6761\u4ef6\u3092\u6e80\u305f\u3059\u3059\u3079\u3066\u306e\u7d4c\u8def \u5236\u7d04\u6761\u4ef6\uff08constraint\uff09 \u5236\u7d04\u6761\u4ef6\u306f\u3001\u89e3\u306e\u5b9f\u73fe\u53ef\u80fd\u6027\u3092\u5236\u9650\u3059\u308b\u6761\u4ef6\u3067\u3042\u308a\u3001\u901a\u5e38\u306f\u679d\u5208\u308a\u306b\u4f7f\u7528\u3055\u308c\u308b \u7d4c\u8def\u306b\u30ce\u30fc\u30c9 \\(3\\) \u3092\u542b\u307e\u306a\u3044 \u72b6\u614b\uff08state\uff09 \u72b6\u614b\u306f\u3001\u3042\u308b\u6642\u70b9\u3067\u306e\u554f\u984c\u306e\u72b6\u6cc1\u3092\u8868\u3057\u3001\u3053\u308c\u307e\u3067\u306b\u884c\u3063\u305f\u9078\u629e\u3092\u542b\u3080 \u73fe\u5728\u8a2a\u554f\u3057\u305f\u30ce\u30fc\u30c9\u7d4c\u8def\u3001\u3059\u306a\u308f\u3061 path \u30ce\u30fc\u30c9\u30ea\u30b9\u30c8 \u8a66\u884c\uff08attempt\uff09 \u8a66\u884c\u306f\u3001\u5229\u7528\u53ef\u80fd\u306a\u9078\u629e\u80a2\u306b\u57fa\u3065\u3044\u3066\u89e3\u7a7a\u9593\u3092\u63a2\u7d22\u3059\u308b\u904e\u7a0b\u3067\u3042\u308a\u3001\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\u3057\u3001\u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\u3059\u308b \u5de6\uff08\u53f3\uff09\u5b50\u30ce\u30fc\u30c9\u3092\u518d\u5e30\u7684\u306b\u8a2a\u554f\u3057\u3001\u30ce\u30fc\u30c9\u3092 path \u306b\u8ffd\u52a0\u3057\u3001\u30ce\u30fc\u30c9\u306e\u5024\u304c \\(7\\) \u304b\u3092\u78ba\u8a8d\u3059\u308b \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30af\uff08backtracking\uff09 \u5236\u7d04\u6761\u4ef6\u3092\u6e80\u305f\u3055\u306a\u3044\u72b6\u614b\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u4ee5\u524d\u306e\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3066\u524d\u306e\u72b6\u614b\u306b\u623b\u308b\u3053\u3068 \u8449\u30ce\u30fc\u30c9\u3092\u8d8a\u3048\u305f\u3068\u304d\u3001\u63a2\u7d22\u7d42\u4e86\u3001\u5024\u304c \\(3\\) \u306e\u30ce\u30fc\u30c9\u306b\u906d\u9047\u3057\u305f\u3068\u304d\u63a2\u7d22\u3092\u7d42\u4e86\u3057\u3001\u95a2\u6570\u304c\u623b\u308b \u679d\u5208\u308a\uff08pruning\uff09 \u554f\u984c\u306e\u7279\u6027\u3084\u5236\u7d04\u6761\u4ef6\u306b\u57fa\u3065\u304d\u3001\u7121\u610f\u5473\u306a\u63a2\u7d22\u7d4c\u8def\u3092\u907f\u3051\u308b\u65b9\u6cd5\u3067\u3042\u308a\u3001\u63a2\u7d22\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u308b \u5024\u304c \\(3\\) \u306e\u30ce\u30fc\u30c9\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u305d\u308c\u4ee5\u4e0a\u63a2\u7d22\u3057\u306a\u3044

        Tip

        \u554f\u984c\u3001\u89e3\u3001\u72b6\u614b\u306a\u3069\u306e\u6982\u5ff5\u306f\u4e00\u822c\u7684\u306a\u3082\u306e\u3067\u3042\u308a\u3001\u5206\u5272\u7d71\u6cbb\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3001\u52d5\u7684\u8a08\u753b\u6cd5\u3001\u8caa\u6b32\u6cd5\u306a\u3069\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u3082\u95a2\u4fc2\u3057\u307e\u3059\u3002

        "},{"location":"chapter_backtracking/backtracking_algorithm/#1315","title":"13.1.5 \u00a0 \u9577\u6240\u3068\u9650\u754c","text":"

        \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u672c\u8cea\u7684\u306b\u6df1\u3055\u512a\u5148\u63a2\u7d22\uff08DFS\uff09\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u4e00\u7a2e\u3067\u3042\u308a\u3001\u6761\u4ef6\u3092\u6e80\u305f\u3059\u89e3\u3092\u898b\u3064\u3051\u308b\u307e\u3067\u3042\u3089\u3086\u308b\u53ef\u80fd\u306a\u89e3\u3092\u8a66\u307f\u307e\u3059\u3002\u3053\u306e\u65b9\u6cd5\u306e\u5229\u70b9\u306f\u3001\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u89e3\u3092\u898b\u3064\u3051\u3089\u308c\u308b\u70b9\u3067\u3042\u308a\u3001\u9069\u5207\u306a\u679d\u5208\u308a\u3092\u884c\u3048\u3070\u52b9\u7387\u304c\u9ad8\u3044\u3053\u3068\u3067\u3059\u3002

        \u3057\u304b\u3057\u3001\u5927\u898f\u6a21\u307e\u305f\u306f\u8907\u96d1\u306a\u554f\u984c\u3092\u6271\u3046\u5834\u5408\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u52b9\u7387\u306f\u8a31\u5bb9\u3067\u304d\u306a\u3044\u307b\u3069\u4f4e\u4e0b\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        • \u6642\u9593\uff1a\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u901a\u5e38\u3001\u72b6\u614b\u7a7a\u9593\u306e\u3059\u3079\u3066\u306e\u53ef\u80fd\u6027\u3092\u63a2\u7d22\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\u6307\u6570\u30aa\u30fc\u30c0\u30fc\u307e\u305f\u306f\u968e\u4e57\u30aa\u30fc\u30c0\u30fc\u306b\u9054\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        • \u7a7a\u9593\uff1a\u518d\u5e30\u547c\u3073\u51fa\u3057\u4e2d\u306b\u73fe\u5728\u306e\u72b6\u614b\uff08\u4f8b\uff1a\u7d4c\u8def\u3001\u679d\u5208\u308a\u7528\u306e\u88dc\u52a9\u5909\u6570\u306a\u3069\uff09\u3092\u4fdd\u5b58\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u6df1\u3055\u304c\u5927\u304d\u3044\u5834\u5408\u3001\u7a7a\u9593\u306e\u4f7f\u7528\u91cf\u304c\u5897\u52a0\u3057\u307e\u3059\u3002

        \u305d\u308c\u3067\u3082\u306a\u304a\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u7279\u5b9a\u306e\u63a2\u7d22\u554f\u984c\u3084\u5236\u7d04\u6e80\u8db3\u554f\u984c\u306e\u6700\u826f\u306e\u89e3\u6cd5\u3067\u3042\u308b\u3053\u3068\u304c\u591a\u3044\u3067\u3059\u3002\u3053\u308c\u3089\u306e\u554f\u984c\u3067\u306f\u3001\u3069\u306e\u9078\u629e\u304c\u6709\u52b9\u306a\u89e3\u3092\u751f\u6210\u3059\u308b\u304b\u3092\u4e88\u6e2c\u3067\u304d\u306a\u3044\u305f\u3081\u3001\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u9078\u629e\u3092\u8a66\u3059\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u3088\u3046\u306a\u5834\u5408\u3001**\u52b9\u7387\u306e\u6700\u9069\u5316\u304c\u9375**\u3068\u306a\u308a\u307e\u3059\u3002\u4e00\u822c\u7684\u306a\u6700\u9069\u5316\u624b\u6cd5\u306f\u6b21\u306e 2 \u3064\u3067\u3059\u3002

        • \u679d\u5208\u308a\uff1a\u89e3\u3092\u751f\u6210\u3057\u306a\u3044\u3053\u3068\u304c\u78ba\u5b9f\u306a\u7d4c\u8def\u3092\u907f\u3051\u308b\u3053\u3068\u3067\u3001\u6642\u9593\u3068\u7a7a\u9593\u3092\u7bc0\u7d04\u3057\u307e\u3059\u3002
        • \u30d2\u30e5\u30fc\u30ea\u30b9\u30c6\u30a3\u30c3\u30af\u63a2\u7d22\uff1a\u63a2\u7d22\u4e2d\u306b\u6226\u7565\u3084\u8a55\u4fa1\u5024\u3092\u5c0e\u5165\u3057\u3001\u6709\u52b9\u306a\u89e3\u3092\u751f\u6210\u3059\u308b\u53ef\u80fd\u6027\u304c\u9ad8\u3044\u7d4c\u8def\u3092\u512a\u5148\u7684\u306b\u63a2\u7d22\u3057\u307e\u3059\u3002
        "},{"location":"chapter_backtracking/backtracking_algorithm/#1316","title":"13.1.6 \u00a0 \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306e\u5178\u578b\u7684\u306a\u4f8b\u984c","text":"

        \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u591a\u304f\u306e\u63a2\u7d22\u554f\u984c\u3001\u5236\u7d04\u6e80\u8db3\u554f\u984c\u3001\u7d44\u5408\u305b\u6700\u9069\u5316\u554f\u984c\u3092\u89e3\u304f\u306e\u306b\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002

        \u63a2\u7d22\u554f\u984c\uff1a\u3053\u306e\u7a2e\u306e\u554f\u984c\u306e\u76ee\u6a19\u306f\u3001\u7279\u5b9a\u306e\u6761\u4ef6\u3092\u6e80\u305f\u3059\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3067\u3059\u3002

        • \u5168\u9806\u5217\u554f\u984c\uff1a\u4e0e\u3048\u3089\u308c\u305f\u96c6\u5408\u306e\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u9806\u5217\u3092\u6c42\u3081\u308b\u3002
        • \u90e8\u5206\u548c\u554f\u984c\uff1a\u4e0e\u3048\u3089\u308c\u305f\u96c6\u5408\u3068\u76ee\u6a19\u548c\u306b\u5bfe\u3057\u3066\u3001\u548c\u304c\u76ee\u6a19\u5024\u306b\u306a\u308b\u3059\u3079\u3066\u306e\u90e8\u5206\u96c6\u5408\u3092\u6c42\u3081\u308b\u3002
        • \u30cf\u30ce\u30a4\u306e\u5854\uff1a3 \u672c\u306e\u67f1\u3068\u7570\u306a\u308b\u30b5\u30a4\u30ba\u306e\u5186\u76e4\u304c\u3042\u308a\u3001\u3059\u3079\u3066\u306e\u5186\u76e4\u3092 1 \u672c\u306e\u67f1\u304b\u3089\u5225\u306e\u67f1\u306b\u79fb\u3059\u30021 \u56de\u306b 1 \u679a\u3057\u304b\u52d5\u304b\u305b\u305a\u3001\u5927\u304d\u306a\u5186\u76e4\u3092\u5c0f\u3055\u3044\u5186\u76e4\u306e\u4e0a\u306b\u7f6e\u304f\u3053\u3068\u306f\u3067\u304d\u306a\u3044\u3002

        \u5236\u7d04\u6e80\u8db3\u554f\u984c\uff1a\u3053\u306e\u7a2e\u306e\u554f\u984c\u306e\u76ee\u6a19\u306f\u3001\u3059\u3079\u3066\u306e\u5236\u7d04\u6761\u4ef6\u3092\u6e80\u305f\u3059\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3067\u3059\u3002

        • \\(n\\) \u30af\u30a4\u30fc\u30f3\u554f\u984c\uff1a\\(n imes n\\) \u306e\u30c1\u30a7\u30b9\u76e4\u306b \\(n\\) \u500b\u306e\u30af\u30a4\u30fc\u30f3\u3092\u914d\u7f6e\u3057\u3001\u4e92\u3044\u306b\u653b\u6483\u3057\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u3002
        • \u6570\u72ec\uff1a\\(9 imes 9\\) \u306e\u30b0\u30ea\u30c3\u30c9\u306b\u6570\u5b57 \\(1\\) ~ \\(9\\) \u3092\u5165\u529b\u3057\u3001\u5404\u884c\u3001\u5217\u3001\\(3 imes 3\\) \u306e\u30b5\u30d6\u30b0\u30ea\u30c3\u30c9\u306b\u91cd\u8907\u304c\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u3002
        • \u30b0\u30e9\u30d5\u5f69\u8272\u554f\u984c\uff1a\u4e0e\u3048\u3089\u308c\u305f\u7121\u5411\u30b0\u30e9\u30d5\u306b\u5bfe\u3057\u3001\u96a3\u63a5\u9802\u70b9\u304c\u7570\u306a\u308b\u8272\u306b\u306a\u308b\u3088\u3046\u306b\u6700\u5c0f\u9650\u306e\u8272\u3067\u5f69\u8272\u3059\u308b\u3002

        \u7d44\u5408\u305b\u6700\u9069\u5316\u554f\u984c\uff1a\u3053\u306e\u7a2e\u306e\u554f\u984c\u306e\u76ee\u6a19\u306f\u3001\u7d44\u5408\u305b\u7a7a\u9593\u5185\u3067\u7279\u5b9a\u306e\u6761\u4ef6\u3092\u6e80\u305f\u3059\u6700\u9069\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3067\u3059\u3002

        • 0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\uff1a\u4e0e\u3048\u3089\u308c\u305f\u7269\u54c1\u7fa4\u3068\u30d0\u30c3\u30af\u30d1\u30c3\u30af\u304c\u3042\u308a\u3001\u5404\u7269\u54c1\u306b\u306f\u4fa1\u5024\u3068\u91cd\u3055\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u3002\u30d0\u30c3\u30af\u30d1\u30c3\u30af\u306e\u5bb9\u91cf\u5236\u9650\u5185\u3067\u3001\u7dcf\u4fa1\u5024\u3092\u6700\u5927\u5316\u3059\u308b\u7269\u54c1\u306e\u9078\u629e\u3092\u6c42\u3081\u308b\u3002
        • \u65c5\u884c\u30bb\u30fc\u30eb\u30b9\u30de\u30f3\u554f\u984c\uff1a\u30b0\u30e9\u30d5\u4e0a\u3067\u30011 \u3064\u306e\u70b9\u304b\u3089\u51fa\u767a\u3057\u3001\u3059\u3079\u3066\u306e\u4ed6\u306e\u70b9\u3092 1 \u56de\u305a\u3064\u8a2a\u554f\u3057\u3066\u51fa\u767a\u70b9\u306b\u623b\u308b\u6700\u77ed\u7d4c\u8def\u3092\u6c42\u3081\u308b\u3002
        • \u6700\u5927\u30af\u30ea\u30fc\u30af\u554f\u984c\uff1a\u4e0e\u3048\u3089\u308c\u305f\u7121\u5411\u30b0\u30e9\u30d5\u306e\u4e2d\u3067\u3001\u4efb\u610f\u306e 2 \u9802\u70b9\u9593\u306b\u8fba\u304c\u5b58\u5728\u3059\u308b\u6700\u5927\u306e\u5b8c\u5168\u90e8\u5206\u30b0\u30e9\u30d5\u3092\u898b\u3064\u3051\u308b\u3002

        \u6ce8\u610f\u3059\u3079\u304d\u306f\u3001\u591a\u304f\u306e\u7d44\u5408\u305b\u6700\u9069\u5316\u554f\u984c\u306b\u5bfe\u3057\u3066\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u304c\u6700\u9069\u89e3\u6cd5\u3067\u306f\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002

        • 0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306f\u3001\u6642\u9593\u52b9\u7387\u3092\u9ad8\u3081\u308b\u305f\u3081\u306b\u52d5\u7684\u8a08\u753b\u6cd5\u304c\u3088\u304f\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        • \u65c5\u884c\u30bb\u30fc\u30eb\u30b9\u30de\u30f3\u554f\u984c\u306f\u6709\u540d\u306a NP-Hard \u554f\u984c\u3067\u3042\u308a\u3001\u907a\u4f1d\u7684\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3084\u30a2\u30f3\u30c8\u30b3\u30ed\u30cb\u30fc\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306a\u3069\u306e\u624b\u6cd5\u304c\u3088\u304f\u4f7f\u308f\u308c\u307e\u3059\u3002
        • \u6700\u5927\u30af\u30ea\u30fc\u30af\u554f\u984c\u306f\u30b0\u30e9\u30d5\u7406\u8ad6\u306e\u53e4\u5178\u7684\u306a\u554f\u984c\u3067\u3042\u308a\u3001\u8caa\u6b32\u6cd5\u306a\u3069\u306e\u30d2\u30e5\u30fc\u30ea\u30b9\u30c6\u30a3\u30c3\u30af\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u89e3\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
        "},{"location":"chapter_backtracking/n_queens_problem/","title":"13.4 \u00a0 N\u30af\u30a4\u30fc\u30f3\u554f\u984c","text":"

        Question

        \u30c1\u30a7\u30b9\u306e\u30eb\u30fc\u30eb\u306b\u3088\u308b\u3068\u3001\u30af\u30a4\u30fc\u30f3\u306f\u540c\u3058\u884c\u3001\u5217\u3001\u307e\u305f\u306f\u5bfe\u89d2\u7dda\u4e0a\u306e\u99d2\u3092\u653b\u6483\u3067\u304d\u307e\u3059\u3002\\(n\\) \u500b\u306e\u30af\u30a4\u30fc\u30f3\u3068 \\(n \\times n\\) \u306e\u30c1\u30a7\u30b9\u30dc\u30fc\u30c9\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u30012\u3064\u306e\u30af\u30a4\u30fc\u30f3\u304c\u4e92\u3044\u306b\u653b\u6483\u3067\u304d\u306a\u3044\u914d\u7f6e\u3092\u898b\u3064\u3051\u3066\u304f\u3060\u3055\u3044\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\\(n = 4\\) \u306e\u5834\u5408\u30012\u3064\u306e\u89e3\u304c\u3042\u308a\u307e\u3059\u3002\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u89b3\u70b9\u304b\u3089\u3001\\(n \\times n\\) \u306e\u30c1\u30a7\u30b9\u30dc\u30fc\u30c9\u306b\u306f \\(n^2\\) \u500b\u306e\u30de\u30b9\u304c\u3042\u308a\u3001\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u9078\u629e\u80a2 choices \u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u30c1\u30a7\u30b9\u30dc\u30fc\u30c9\u306e\u72b6\u614b state \u306f\u3001\u5404\u30af\u30a4\u30fc\u30f3\u304c\u914d\u7f6e\u3055\u308c\u308b\u306b\u3064\u308c\u3066\u7d99\u7d9a\u7684\u306b\u5909\u5316\u3057\u307e\u3059\u3002

        \u56f3 13-15 \u00a0 4\u30af\u30a4\u30fc\u30f3\u554f\u984c\u306e\u89e3

        \u4ee5\u4e0b\u306e\u56f3\u306f\u3001\u3053\u306e\u554f\u984c\u306e3\u3064\u306e\u5236\u7d04\u3092\u793a\u3057\u3066\u3044\u307e\u3059\uff1a\u8907\u6570\u306e\u30af\u30a4\u30fc\u30f3\u306f\u540c\u3058\u884c\u3001\u5217\u3001\u307e\u305f\u306f\u5bfe\u89d2\u7dda\u3092\u5360\u6709\u3067\u304d\u307e\u305b\u3093\u3002\u5bfe\u89d2\u7dda\u306f\u4e3b\u5bfe\u89d2\u7dda \\ \u3068\u526f\u5bfe\u89d2\u7dda / \u306b\u5206\u304b\u308c\u308b\u3053\u3068\u306b\u6ce8\u610f\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002

        \u56f3 13-16 \u00a0 N\u30af\u30a4\u30fc\u30f3\u554f\u984c\u306e\u5236\u7d04

        "},{"location":"chapter_backtracking/n_queens_problem/#1","title":"1. \u00a0 \u884c\u3054\u3068\u306e\u914d\u7f6e\u6226\u7565","text":"

        \u30af\u30a4\u30fc\u30f3\u306e\u6570\u304c\u30c1\u30a7\u30b9\u30dc\u30fc\u30c9\u306e\u884c\u6570\u3068\u7b49\u3057\u304f\u3001\u3069\u3061\u3089\u3082 \\(n\\) \u3067\u3042\u308b\u305f\u3081\u3001**\u30c1\u30a7\u30b9\u30dc\u30fc\u30c9\u306e\u5404\u884c\u306b\u306f1\u3064\u306e\u30af\u30a4\u30fc\u30f3\u306e\u307f\u304c\u914d\u7f6e\u3067\u304d\u308b\u3053\u3068\u304c**\u5bb9\u6613\u306b\u7d50\u8ad6\u4ed8\u3051\u3089\u308c\u307e\u3059\u3002

        \u3053\u308c\u306f\u3001\u884c\u3054\u3068\u306e\u914d\u7f6e\u6226\u7565\u3092\u63a1\u7528\u3067\u304d\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\uff1a\u6700\u521d\u306e\u884c\u304b\u3089\u958b\u59cb\u3057\u3066\u3001\u6700\u5f8c\u306e\u884c\u306b\u5230\u9054\u3059\u308b\u307e\u3067\u884c\u3054\u3068\u306b1\u3064\u306e\u30af\u30a4\u30fc\u30f3\u3092\u914d\u7f6e\u3057\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306f\u30014\u30af\u30a4\u30fc\u30f3\u554f\u984c\u306e\u884c\u3054\u3068\u306e\u914d\u7f6e\u30d7\u30ed\u30bb\u30b9\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u30b9\u30da\u30fc\u30b9\u306e\u5236\u9650\u306b\u3088\u308a\u3001\u56f3\u306f\u6700\u521d\u306e\u884c\u306e1\u3064\u306e\u691c\u7d22\u5206\u5c90\u306e\u307f\u3092\u5c55\u958b\u3057\u3001\u5217\u3068\u5bfe\u89d2\u7dda\u306e\u5236\u7d04\u3092\u6e80\u305f\u3055\u306a\u3044\u914d\u7f6e\u3092\u526a\u5b9a\u3057\u307e\u3059\u3002

        \u56f3 13-17 \u00a0 \u884c\u3054\u3068\u306e\u914d\u7f6e\u6226\u7565

        \u672c\u8cea\u7684\u306b\u3001\u884c\u3054\u3068\u306e\u914d\u7f6e\u6226\u7565\u306f\u526a\u5b9a\u95a2\u6570\u3068\u3057\u3066\u6a5f\u80fd\u3057\u3001\u540c\u3058\u884c\u306b\u8907\u6570\u306e\u30af\u30a4\u30fc\u30f3\u3092\u914d\u7f6e\u3059\u308b\u3059\u3079\u3066\u306e\u691c\u7d22\u5206\u5c90\u3092\u9664\u53bb\u3057\u307e\u3059\u3002

        "},{"location":"chapter_backtracking/n_queens_problem/#2","title":"2. \u00a0 \u5217\u3068\u5bfe\u89d2\u7dda\u306e\u526a\u5b9a","text":"

        \u5217\u306e\u5236\u7d04\u3092\u6e80\u305f\u3059\u305f\u3081\u306b\u3001\u9577\u3055 \\(n\\) \u306e\u30d6\u30fc\u30eb\u914d\u5217 cols \u3092\u4f7f\u7528\u3057\u3066\u3001\u5404\u5217\u306b\u30af\u30a4\u30fc\u30f3\u304c\u5360\u6709\u3055\u308c\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3092\u8ffd\u8de1\u3067\u304d\u307e\u3059\u3002\u5404\u914d\u7f6e\u6c7a\u5b9a\u306e\u524d\u306b\u3001cols \u3092\u4f7f\u7528\u3057\u3066\u3059\u3067\u306b\u30af\u30a4\u30fc\u30f3\u304c\u3042\u308b\u5217\u3092\u526a\u5b9a\u3057\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u4e2d\u306b\u52d5\u7684\u306b\u66f4\u65b0\u3055\u308c\u307e\u3059\u3002

        Tip

        \u884c\u5217\u306e\u539f\u70b9\u306f\u5de6\u4e0a\u9685\u306b\u3042\u308a\u3001\u884c\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u4e0a\u304b\u3089\u4e0b\u306b\u5897\u52a0\u3057\u3001\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u5de6\u304b\u3089\u53f3\u306b\u5897\u52a0\u3059\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u5bfe\u89d2\u7dda\u306e\u5236\u7d04\u306f\u3069\u3046\u3067\u3057\u3087\u3046\u304b\uff1f\u30c1\u30a7\u30b9\u30dc\u30fc\u30c9\u4e0a\u306e\u7279\u5b9a\u306e\u30bb\u30eb\u306e\u884c\u3068\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092 \\((row, col)\\) \u3068\u3057\u307e\u3059\u3002\u7279\u5b9a\u306e\u4e3b\u5bfe\u89d2\u7dda\u3092\u9078\u629e\u3059\u308b\u3053\u3068\u3067\u3001\u305d\u306e\u5bfe\u89d2\u7dda\u4e0a\u306e\u3059\u3079\u3066\u306e\u30bb\u30eb\u3067\u5dee \\(row - col\\) \u304c\u540c\u3058\u3067\u3042\u308b\u3053\u3068\u306b\u6c17\u4ed8\u304d\u307e\u3059\u3002\u3064\u307e\u308a\u3001\\(row - col\\) \u306f\u4e3b\u5bfe\u89d2\u7dda\u4e0a\u3067\u5b9a\u6570\u5024\u3067\u3059\u3002

        \u8a00\u3044\u63db\u3048\u308b\u3068\u30012\u3064\u306e\u30bb\u30eb\u304c \\(row_1 - col_1 = row_2 - col_2\\) \u3092\u6e80\u305f\u3059\u5834\u5408\u3001\u305d\u308c\u3089\u306f\u78ba\u5b9f\u306b\u540c\u3058\u4e3b\u5bfe\u89d2\u7dda\u4e0a\u306b\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u30d1\u30bf\u30fc\u30f3\u3092\u4f7f\u7528\u3057\u3066\u3001\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u914d\u5217 diags1 \u3092\u5229\u7528\u3057\u3066\u3001\u30af\u30a4\u30fc\u30f3\u304c\u4e3b\u5bfe\u89d2\u7dda\u4e0a\u306b\u3042\u308b\u304b\u3069\u3046\u304b\u3092\u8ffd\u8de1\u3067\u304d\u307e\u3059\u3002

        \u540c\u69d8\u306b\u3001\\(row + col\\) \u306e\u548c\u306f\u526f\u5bfe\u89d2\u7dda\u4e0a\u306e\u3059\u3079\u3066\u306e\u30bb\u30eb\u3067\u5b9a\u6570\u5024\u3067\u3059\u3002\u914d\u5217 diags2 \u3092\u4f7f\u7528\u3057\u3066\u526f\u5bfe\u89d2\u7dda\u306e\u5236\u7d04\u3082\u51e6\u7406\u3067\u304d\u307e\u3059\u3002

        \u56f3 13-18 \u00a0 \u5217\u3068\u5bfe\u89d2\u7dda\u306e\u5236\u7d04\u306e\u51e6\u7406

        "},{"location":"chapter_backtracking/n_queens_problem/#3","title":"3. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        \\(n\\) \u6b21\u5143\u306e\u6b63\u65b9\u884c\u5217\u3067\u306f\u3001\\(row - col\\) \u306e\u7bc4\u56f2\u306f \\([-n + 1, n - 1]\\) \u3067\u3001\\(row + col\\) \u306e\u7bc4\u56f2\u306f \\([0, 2n - 2]\\) \u3067\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u4e3b\u5bfe\u89d2\u7dda\u3068\u526f\u5bfe\u89d2\u7dda\u306e\u6570\u306f\u3069\u3061\u3089\u3082 \\(2n - 1\\) \u3067\u3001\u914d\u5217 diags1 \u3068 diags2 \u306e\u9577\u3055\u306f \\(2n - 1\\) \u3067\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby n_queens.py
        def backtrack(\n    row: int,\n    n: int,\n    state: list[list[str]],\n    res: list[list[list[str]]],\n    cols: list[bool],\n    diags1: list[bool],\n    diags2: list[bool],\n):\n    \"\"\"\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1an \u30af\u30a4\u30fc\u30f3\"\"\"\n    # \u3059\u3079\u3066\u306e\u884c\u304c\u914d\u7f6e\u3055\u308c\u305f\u3089\u3001\u89e3\u3092\u8a18\u9332\n    if row == n:\n        res.append([list(row) for row in state])\n        return\n    # \u3059\u3079\u3066\u306e\u5217\u3092\u8d70\u67fb\n    for col in range(n):\n        # \u30bb\u30eb\u306b\u5bfe\u5fdc\u3059\u308b\u4e3b\u5bfe\u89d2\u7dda\u3068\u526f\u5bfe\u89d2\u7dda\u3092\u8a08\u7b97\n        diag1 = row - col + n - 1\n        diag2 = row + col\n        # \u679d\u5208\u308a\uff1a\u30bb\u30eb\u306e\u5217\u3001\u4e3b\u5bfe\u89d2\u7dda\u3001\u526f\u5bfe\u89d2\u7dda\u306b\u30af\u30a4\u30fc\u30f3\u3092\u914d\u7f6e\u3057\u306a\u3044\n        if not cols[col] and not diags1[diag1] and not diags2[diag2]:\n            # \u8a66\u884c\uff1a\u30bb\u30eb\u306b\u30af\u30a4\u30fc\u30f3\u3092\u914d\u7f6e\n            state[row][col] = \"Q\"\n            cols[col] = diags1[diag1] = diags2[diag2] = True\n            # \u6b21\u306e\u884c\u3092\u914d\u7f6e\n            backtrack(row + 1, n, state, res, cols, diags1, diags2)\n            # \u64a4\u56de\uff1a\u30bb\u30eb\u3092\u7a7a\u306e\u30b9\u30dd\u30c3\u30c8\u306b\u5fa9\u5143\n            state[row][col] = \"#\"\n            cols[col] = diags1[diag1] = diags2[diag2] = False\n\ndef n_queens(n: int) -> list[list[list[str]]]:\n    \"\"\"n \u30af\u30a4\u30fc\u30f3\u3092\u89e3\u304f\"\"\"\n    # n*n \u30b5\u30a4\u30ba\u306e\u30c1\u30a7\u30b9\u30dc\u30fc\u30c9\u3092\u521d\u671f\u5316\u3001'Q' \u306f\u30af\u30a4\u30fc\u30f3\u3092\u8868\u3057\u3001'#' \u306f\u7a7a\u306e\u30b9\u30dd\u30c3\u30c8\u3092\u8868\u3059\n    state = [[\"#\" for _ in range(n)] for _ in range(n)]\n    cols = [False] * n  # \u30af\u30a4\u30fc\u30f3\u304c\u3042\u308b\u5217\u3092\u8a18\u9332\n    diags1 = [False] * (2 * n - 1)  # \u30af\u30a4\u30fc\u30f3\u304c\u3042\u308b\u4e3b\u5bfe\u89d2\u7dda\u3092\u8a18\u9332\n    diags2 = [False] * (2 * n - 1)  # \u30af\u30a4\u30fc\u30f3\u304c\u3042\u308b\u526f\u5bfe\u89d2\u7dda\u3092\u8a18\u9332\n    res = []\n    backtrack(0, n, state, res, cols, diags1, diags2)\n\n    return res\n
        n_queens.cpp
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1an \u30af\u30a4\u30fc\u30f3 */\nvoid backtrack(int row, int n, vector<vector<string>> &state, vector<vector<vector<string>>> &res, vector<bool> &cols,\n               vector<bool> &diags1, vector<bool> &diags2) {\n    // \u3059\u3079\u3066\u306e\u884c\u304c\u914d\u7f6e\u3055\u308c\u305f\u3089\u3001\u89e3\u3092\u8a18\u9332\n    if (row == n) {\n        res.push_back(state);\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u5217\u3092\u8d70\u67fb\n    for (int col = 0; col < n; col++) {\n        // \u30bb\u30eb\u306b\u5bfe\u5fdc\u3059\u308b\u4e3b\u5bfe\u89d2\u7dda\u3068\u526f\u5bfe\u89d2\u7dda\u3092\u8a08\u7b97\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // \u526a\u5b9a\uff1a\u30bb\u30eb\u306e\u5217\u3001\u4e3b\u5bfe\u89d2\u7dda\u3001\u526f\u5bfe\u89d2\u7dda\u306b\u30af\u30a4\u30fc\u30f3\u3092\u914d\u7f6e\u3059\u308b\u3053\u3068\u3092\u8a31\u53ef\u3057\u306a\u3044\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u8a66\u884c\uff1a\u30bb\u30eb\u306b\u30af\u30a4\u30fc\u30f3\u3092\u914d\u7f6e\n            state[row][col] = \"Q\";\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // \u6b21\u306e\u884c\u3092\u914d\u7f6e\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // \u56de\u9000\uff1a\u30bb\u30eb\u3092\u7a7a\u306e\u30b9\u30dd\u30c3\u30c8\u306b\u5fa9\u5143\n            state[row][col] = \"#\";\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* n \u30af\u30a4\u30fc\u30f3\u3092\u89e3\u304f */\nvector<vector<vector<string>>> nQueens(int n) {\n    // n*n \u30b5\u30a4\u30ba\u306e\u30c1\u30a7\u30b9\u30dc\u30fc\u30c9\u3092\u521d\u671f\u5316\u3001'Q' \u306f\u30af\u30a4\u30fc\u30f3\u3092\u8868\u3057\u3001'#' \u306f\u7a7a\u306e\u30b9\u30dd\u30c3\u30c8\u3092\u8868\u3059\n    vector<vector<string>> state(n, vector<string>(n, \"#\"));\n    vector<bool> cols(n, false);           // \u30af\u30a4\u30fc\u30f3\u306e\u3042\u308b\u5217\u3092\u8a18\u9332\n    vector<bool> diags1(2 * n - 1, false); // \u30af\u30a4\u30fc\u30f3\u306e\u3042\u308b\u4e3b\u5bfe\u89d2\u7dda\u3092\u8a18\u9332\n    vector<bool> diags2(2 * n - 1, false); // \u30af\u30a4\u30fc\u30f3\u306e\u3042\u308b\u526f\u5bfe\u89d2\u7dda\u3092\u8a18\u9332\n    vector<vector<vector<string>>> res;\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n\n    return res;\n}\n
        n_queens.java
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1an \u30af\u30a4\u30fc\u30f3 */\nvoid backtrack(int row, int n, List<List<String>> state, List<List<List<String>>> res,\n        boolean[] cols, boolean[] diags1, boolean[] diags2) {\n    // \u3059\u3079\u3066\u306e\u884c\u304c\u914d\u7f6e\u3055\u308c\u305f\u3089\u3001\u89e3\u3092\u8a18\u9332\n    if (row == n) {\n        List<List<String>> copyState = new ArrayList<>();\n        for (List<String> sRow : state) {\n            copyState.add(new ArrayList<>(sRow));\n        }\n        res.add(copyState);\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u5217\u3092\u8d70\u67fb\n    for (int col = 0; col < n; col++) {\n        // \u30bb\u30eb\u306b\u5bfe\u5fdc\u3059\u308b\u4e3b\u5bfe\u89d2\u7dda\u3068\u526f\u5bfe\u89d2\u7dda\u3092\u8a08\u7b97\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // \u526a\u5b9a\uff1a\u30bb\u30eb\u306e\u5217\u3001\u4e3b\u5bfe\u89d2\u7dda\u3001\u526f\u5bfe\u89d2\u7dda\u306b\u30af\u30a4\u30fc\u30f3\u3092\u914d\u7f6e\u3059\u308b\u3053\u3068\u3092\u8a31\u53ef\u3057\u306a\u3044\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u8a66\u884c\uff1a\u30bb\u30eb\u306b\u30af\u30a4\u30fc\u30f3\u3092\u914d\u7f6e\n            state.get(row).set(col, \"Q\");\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // \u6b21\u306e\u884c\u3092\u914d\u7f6e\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // \u56de\u9000\uff1a\u30bb\u30eb\u3092\u7a7a\u306e\u30b9\u30dd\u30c3\u30c8\u306b\u5fa9\u5143\n            state.get(row).set(col, \"#\");\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* n \u30af\u30a4\u30fc\u30f3\u3092\u89e3\u304f */\nList<List<List<String>>> nQueens(int n) {\n    // n*n \u30b5\u30a4\u30ba\u306e\u30c1\u30a7\u30b9\u30dc\u30fc\u30c9\u3092\u521d\u671f\u5316\u3001'Q' \u306f\u30af\u30a4\u30fc\u30f3\u3092\u8868\u3057\u3001'#' \u306f\u7a7a\u306e\u30b9\u30dd\u30c3\u30c8\u3092\u8868\u3059\n    List<List<String>> state = new ArrayList<>();\n    for (int i = 0; i < n; i++) {\n        List<String> row = new ArrayList<>();\n        for (int j = 0; j < n; j++) {\n            row.add(\"#\");\n        }\n        state.add(row);\n    }\n    boolean[] cols = new boolean[n]; // \u30af\u30a4\u30fc\u30f3\u306e\u3042\u308b\u5217\u3092\u8a18\u9332\n    boolean[] diags1 = new boolean[2 * n - 1]; // \u30af\u30a4\u30fc\u30f3\u306e\u3042\u308b\u4e3b\u5bfe\u89d2\u7dda\u3092\u8a18\u9332\n    boolean[] diags2 = new boolean[2 * n - 1]; // \u30af\u30a4\u30fc\u30f3\u306e\u3042\u308b\u526f\u5bfe\u89d2\u7dda\u3092\u8a18\u9332\n    List<List<List<String>>> res = new ArrayList<>();\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n\n    return res;\n}\n
        n_queens.cs
        [class]{n_queens}-[func]{Backtrack}\n\n[class]{n_queens}-[func]{NQueens}\n
        n_queens.go
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{nQueens}\n
        n_queens.swift
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{nQueens}\n
        n_queens.js
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{nQueens}\n
        n_queens.ts
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{nQueens}\n
        n_queens.dart
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{nQueens}\n
        n_queens.rs
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{n_queens}\n
        n_queens.c
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{nQueens}\n
        n_queens.kt
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{nQueens}\n
        n_queens.rb
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{n_queens}\n

        \\(n\\) \u500b\u306e\u30af\u30a4\u30fc\u30f3\u3092\u884c\u3054\u3068\u306b\u914d\u7f6e\u3057\u3001\u5217\u306e\u5236\u7d04\u3092\u8003\u616e\u3057\u3066\u3001\u6700\u521d\u306e\u884c\u304b\u3089\u6700\u5f8c\u306e\u884c\u307e\u3067\u3001\\(n\\)\u3001\\(n-1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u306e\u9078\u629e\u80a2\u304c\u3042\u308a\u3001\\(O(n!)\\) \u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u89e3\u3092\u8a18\u9332\u3059\u308b\u969b\u3001\u884c\u5217 state \u3092\u30b3\u30d4\u30fc\u3057\u3066 res \u306b\u8ffd\u52a0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u30b3\u30d4\u30fc\u64cd\u4f5c\u306f \\(O(n^2)\\) \u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u5168\u4f53\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n! \\cdot n^2)\\) \u3067\u3059\u3002\u5b9f\u969b\u306b\u306f\u3001\u5bfe\u89d2\u7dda\u5236\u7d04\u306b\u57fa\u3065\u304f\u526a\u5b9a\u306b\u3088\u308a\u691c\u7d22\u7a7a\u9593\u3092\u5927\u5e45\u306b\u524a\u6e1b\u3067\u304d\u308b\u305f\u3081\u3001\u591a\u304f\u306e\u5834\u5408\u3001\u691c\u7d22\u52b9\u7387\u306f\u4e0a\u8a18\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3088\u308a\u3082\u512a\u308c\u3066\u3044\u307e\u3059\u3002

        \u914d\u5217 state \u306f \\(O(n^2)\\) \u7a7a\u9593\u3092\u4f7f\u7528\u3057\u3001\u914d\u5217 cols\u3001diags1\u3001diags2 \u306f\u305d\u308c\u305e\u308c \\(O(n)\\) \u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u6700\u5927\u518d\u5e30\u6df1\u5ea6\u306f \\(n\\) \u3067\u3001\\(O(n)\\) \u306e\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(n^2)\\) \u3067\u3059\u3002

        "},{"location":"chapter_backtracking/permutations_problem/","title":"13.2 \u00a0 \u9806\u5217\u554f\u984c","text":"

        \u9806\u5217\u554f\u984c\u306f\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5178\u578b\u7684\u306a\u5fdc\u7528\u3067\u3059\u3002\u3053\u308c\u306f\u3001\u914d\u5217\u3084\u6587\u5b57\u5217\u306a\u3069\u306e\u4e0e\u3048\u3089\u308c\u305f\u96c6\u5408\u304b\u3089\u8981\u7d20\u306e\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u914d\u7f6e\uff08\u9806\u5217\uff09\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3092\u542b\u307f\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u8868\u306f\u3001\u5165\u529b\u914d\u5217\u3068\u305d\u306e\u5bfe\u5fdc\u3059\u308b\u9806\u5217\u3092\u542b\u3080\u3044\u304f\u3064\u304b\u306e\u4f8b\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u8868 13-2 \u00a0 \u9806\u5217\u306e\u4f8b

        \u5165\u529b\u914d\u5217 \u9806\u5217 \\([1]\\) \\([1]\\) \\([1, 2]\\) \\([1, 2], [2, 1]\\) \\([1, 2, 3]\\) \\([1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]\\)"},{"location":"chapter_backtracking/permutations_problem/#1321","title":"13.2.1 \u00a0 \u91cd\u8907\u8981\u7d20\u304c\u306a\u3044\u5834\u5408","text":"

        Question

        \u91cd\u8907\u8981\u7d20\u306e\u306a\u3044\u6574\u6570\u914d\u5217\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u9806\u5217\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306e\u89b3\u70b9\u304b\u3089\u3001\u9806\u5217\u3092\u751f\u6210\u3059\u308b\u30d7\u30ed\u30bb\u30b9\u3092\u4e00\u9023\u306e\u9078\u629e\u3068\u3057\u3066\u898b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 \u5165\u529b\u914d\u5217\u304c \\([1, 2, 3]\\) \u3060\u3068\u3057\u307e\u3059\u3002\u6700\u521d\u306b \\(1\\) \u3092\u9078\u629e\u3057\u3001\u6b21\u306b \\(3\\)\u3001\u6700\u5f8c\u306b \\(2\\) \u3092\u9078\u629e\u3059\u308b\u3068\u3001\u9806\u5217 \\([1, 3, 2]\\) \u304c\u5f97\u3089\u308c\u307e\u3059\u3002\u300c\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u300d\u306f\u524d\u306e\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3066\u3001\u4ee3\u66ff\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u63a2\u7d22\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

        \u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306e\u89b3\u70b9\u304b\u3089\u3001\u5019\u88dc\u96c6\u5408 choices \u306f\u5165\u529b\u914d\u5217\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u3067\u69cb\u6210\u3055\u308c\u3001state \u306f\u3053\u308c\u307e\u3067\u306b\u9078\u629e\u3055\u308c\u305f\u8981\u7d20\u3092\u4fdd\u6301\u3057\u307e\u3059\u3002\u5404\u8981\u7d20\u306f\u4e00\u5ea6\u3060\u3051\u9078\u629e\u3067\u304d\u308b\u305f\u3081\u3001state \u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u306f\u4e00\u610f\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u3092\u518d\u5e30\u6728\u306b\u5c55\u958b\u3067\u304d\u307e\u3059\u3002\u5404\u30ce\u30fc\u30c9\u306f\u73fe\u5728\u306e state \u3092\u8868\u3057\u307e\u3059\u3002\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u304b\u3089\u958b\u59cb\u3057\u3066\u30013\u56de\u306e\u9078\u629e\u306e\u5f8c\u3001\u8449\u30ce\u30fc\u30c9\u306b\u5230\u9054\u3057\u307e\u3059\u2014\u305d\u308c\u305e\u308c\u304c\u9806\u5217\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002

        \u56f3 13-5 \u00a0 \u9806\u5217\u306e\u518d\u5e30\u6728

        "},{"location":"chapter_backtracking/permutations_problem/#1","title":"1. \u00a0 \u91cd\u8907\u9078\u629e\u306e\u526a\u5b9a","text":"

        \u5404\u8981\u7d20\u304c\u4e00\u5ea6\u3060\u3051\u9078\u629e\u3055\u308c\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3059\u308b\u305f\u3081\u306b\u3001\u30d6\u30fc\u30eb\u914d\u5217 selected \u3092\u5c0e\u5165\u3057\u307e\u3059\u3002\u3053\u3053\u3067 selected[i] \u306f choices[i] \u304c\u9078\u629e\u3055\u308c\u305f\u304b\u3069\u3046\u304b\u3092\u793a\u3057\u307e\u3059\u3002\u6b21\u306b\u3001\u3053\u306e\u914d\u5217\u306b\u57fa\u3065\u3044\u3066\u526a\u5b9a\u30b9\u30c6\u30c3\u30d7\u3092\u5b9f\u884c\u3057\u307e\u3059\uff1a

        • choice[i] \u3092\u9078\u629e\u3057\u305f\u5f8c\u3001selected[i] \u3092 \\(\\text{True}\\) \u306b\u8a2d\u5b9a\u3057\u3066\u9078\u629e\u3055\u308c\u305f\u3068\u30de\u30fc\u30af\u3057\u307e\u3059\u3002
        • choices \u3092\u53cd\u5fa9\u51e6\u7406\u3059\u308b\u969b\u3001\u9078\u629e\u3055\u308c\u305f\u3068\u30de\u30fc\u30af\u3055\u308c\u305f\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u30b9\u30ad\u30c3\u30d7\u3057\u307e\u3059\uff08\u3064\u307e\u308a\u3001\u305d\u308c\u3089\u306e\u5206\u5c90\u3092\u526a\u5b9a\u3057\u307e\u3059\uff09\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u6700\u521d\u306e\u30e9\u30a6\u30f3\u30c9\u30671\u3092\u9078\u629e\u3057\u30012\u756a\u76ee\u306e\u30e9\u30a6\u30f3\u30c9\u30673\u3092\u9078\u629e\u3057\u3001\u6700\u5f8c\u306e\u30e9\u30a6\u30f3\u30c9\u30672\u3092\u9078\u629e\u3059\u308b\u3068\u3057\u307e\u3059\u30022\u756a\u76ee\u306e\u30e9\u30a6\u30f3\u30c9\u3067\u8981\u7d201\u306e\u5206\u5c90\u3068\u30013\u756a\u76ee\u306e\u30e9\u30a6\u30f3\u30c9\u3067\u8981\u7d201\u30683\u306e\u5206\u5c90\u3092\u526a\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u56f3 13-6 \u00a0 \u9806\u5217\u306e\u526a\u5b9a\u4f8b

        \u56f3\u304b\u3089\u3001\u3053\u306e\u526a\u5b9a\u30d7\u30ed\u30bb\u30b9\u304c\u691c\u7d22\u7a7a\u9593\u3092 \\(O(n^n)\\) \u304b\u3089 \\(O(n!)\\) \u306b\u524a\u6e1b\u3059\u308b\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002

        "},{"location":"chapter_backtracking/permutations_problem/#2","title":"2. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        \u3053\u306e\u7406\u89e3\u306b\u3088\u308a\u3001\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u30b3\u30fc\u30c9\u306e\u300c\u7a7a\u6b04\u3092\u57cb\u3081\u308b\u300d\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u5168\u4f53\u306e\u30b3\u30fc\u30c9\u3092\u7c21\u6f54\u306b\u4fdd\u3064\u305f\u3081\u3001\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306e\u5404\u90e8\u5206\u3092\u500b\u5225\u306b\u5b9f\u88c5\u305b\u305a\u3001\u4ee3\u308f\u308a\u306b backtrack() \u95a2\u6570\u3067\u3059\u3079\u3066\u3092\u5c55\u958b\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby permutations_i.py
        def backtrack(\n    state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]\n):\n    \"\"\"\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u9806\u5217 I\"\"\"\n    # \u72b6\u614b\u306e\u9577\u3055\u304c\u8981\u7d20\u6570\u3068\u7b49\u3057\u3044\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\n    if len(state) == len(choices):\n        res.append(list(state))\n        return\n    # \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for i, choice in enumerate(choices):\n        # \u679d\u5208\u308a\uff1a\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3092\u8a31\u53ef\u3057\u306a\u3044\n        if not selected[i]:\n            # \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            selected[i] = True\n            state.append(choice)\n            # \u6b21\u306e\u9078\u629e\u30e9\u30a6\u30f3\u30c9\u306b\u9032\u3080\n            backtrack(state, choices, selected, res)\n            # \u64a4\u56de\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n            selected[i] = False\n            state.pop()\n\ndef permutations_i(nums: list[int]) -> list[list[int]]:\n    \"\"\"\u9806\u5217 I\"\"\"\n    res = []\n    backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\n    return res\n
        permutations_i.cpp
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u9806\u5217 I */\nvoid backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &selected, vector<vector<int>> &res) {\n    // \u72b6\u614b\u306e\u9577\u3055\u304c\u8981\u7d20\u6570\u3068\u7b49\u3057\u304f\u306a\u3063\u305f\u3089\u3001\u89e3\u3092\u8a18\u9332\n    if (state.size() == choices.size()) {\n        res.push_back(state);\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for (int i = 0; i < choices.size(); i++) {\n        int choice = choices[i];\n        // \u526a\u5b9a\uff1a\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3092\u8a31\u53ef\u3057\u306a\u3044\n        if (!selected[i]) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            selected[i] = true;\n            state.push_back(choice);\n            // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n            selected[i] = false;\n            state.pop_back();\n        }\n    }\n}\n\n/* \u9806\u5217 I */\nvector<vector<int>> permutationsI(vector<int> nums) {\n    vector<int> state;\n    vector<bool> selected(nums.size(), false);\n    vector<vector<int>> res;\n    backtrack(state, nums, selected, res);\n    return res;\n}\n
        permutations_i.java
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u9806\u5217 I */\nvoid backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {\n    // \u72b6\u614b\u306e\u9577\u3055\u304c\u8981\u7d20\u6570\u3068\u7b49\u3057\u304f\u306a\u3063\u305f\u3089\u3001\u89e3\u3092\u8a18\u9332\n    if (state.size() == choices.length) {\n        res.add(new ArrayList<Integer>(state));\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for (int i = 0; i < choices.length; i++) {\n        int choice = choices[i];\n        // \u526a\u5b9a\uff1a\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3092\u8a31\u53ef\u3057\u306a\u3044\n        if (!selected[i]) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            selected[i] = true;\n            state.add(choice);\n            // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n            selected[i] = false;\n            state.remove(state.size() - 1);\n        }\n    }\n}\n\n/* \u9806\u5217 I */\nList<List<Integer>> permutationsI(int[] nums) {\n    List<List<Integer>> res = new ArrayList<List<Integer>>();\n    backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);\n    return res;\n}\n
        permutations_i.cs
        [class]{permutations_i}-[func]{Backtrack}\n\n[class]{permutations_i}-[func]{PermutationsI}\n
        permutations_i.go
        [class]{}-[func]{backtrackI}\n\n[class]{}-[func]{permutationsI}\n
        permutations_i.swift
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsI}\n
        permutations_i.js
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsI}\n
        permutations_i.ts
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsI}\n
        permutations_i.dart
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsI}\n
        permutations_i.rs
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutations_i}\n
        permutations_i.c
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsI}\n
        permutations_i.kt
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsI}\n
        permutations_i.rb
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutations_i}\n
        "},{"location":"chapter_backtracking/permutations_problem/#1322","title":"13.2.2 \u00a0 \u91cd\u8907\u8981\u7d20\u3092\u8003\u616e\u3059\u308b\u5834\u5408","text":"

        Question

        **\u91cd\u8907\u8981\u7d20\u3092\u542b\u3080\u53ef\u80fd\u6027\u306e\u3042\u308b**\u6574\u6570\u914d\u5217\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u3059\u3079\u3066\u306e\u4e00\u610f\u306e\u9806\u5217\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u5165\u529b\u914d\u5217\u304c \\([1, 1, 2]\\) \u3060\u3068\u3057\u307e\u3059\u30022\u3064\u306e\u540c\u4e00\u8981\u7d20 \\(1\\) \u3092\u533a\u5225\u3059\u308b\u305f\u3081\u306b\u30012\u756a\u76ee\u3092 \\(\\hat{1}\\) \u3068\u30e9\u30d9\u30eb\u4ed8\u3051\u3057\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u3053\u306e\u65b9\u6cd5\u3067\u751f\u6210\u3055\u308c\u308b\u9806\u5217\u306e\u534a\u5206\u306f\u91cd\u8907\u3067\u3059\uff1a

        \u56f3 13-7 \u00a0 \u91cd\u8907\u9806\u5217

        \u3067\u306f\u3001\u3053\u308c\u3089\u306e\u91cd\u8907\u9806\u5217\u3092\u3069\u306e\u3088\u3046\u306b\u9664\u53bb\u3067\u304d\u308b\u3067\u3057\u3087\u3046\u304b\uff1f\u4e00\u3064\u306e\u76f4\u63a5\u7684\u306a\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\u3001\u3059\u3079\u3066\u306e\u9806\u5217\u3092\u751f\u6210\u3057\u305f\u5f8c\u306b\u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8\u3092\u4f7f\u7528\u3057\u3066\u91cd\u8907\u3092\u9664\u53bb\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u3053\u308c\u306f\u3042\u307e\u308a\u512a\u96c5\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u91cd\u8907\u3092\u751f\u6210\u3059\u308b\u5206\u5c90\u306f\u672c\u6765\u4e0d\u8981\u3067\u3042\u308a\u3001\u4e8b\u524d\u306b\u526a\u5b9a\u3055\u308c\u308b\u3079\u304d\u3060\u304b\u3089\u3067\u3059\u3001\u3053\u308c\u306b\u3088\u308a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u304c\u5411\u4e0a\u3057\u307e\u3059\u3002

        "},{"location":"chapter_backtracking/permutations_problem/#1_1","title":"1. \u00a0 \u7b49\u5024\u8981\u7d20\u306e\u526a\u5b9a","text":"

        \u4ee5\u4e0b\u306e\u56f3\u3092\u898b\u308b\u3068\u3001\u6700\u521d\u306e\u30e9\u30a6\u30f3\u30c9\u3067 \\(1\\) \u307e\u305f\u306f \\(\\hat{1}\\) \u3092\u9078\u629e\u3059\u308b\u3068\u540c\u3058\u9806\u5217\u306b\u3064\u306a\u304c\u308b\u305f\u3081\u3001\\(\\hat{1}\\) \u3092\u526a\u5b9a\u3057\u307e\u3059\u3002

        \u540c\u69d8\u306b\u3001\u6700\u521d\u306e\u30e9\u30a6\u30f3\u30c9\u3067 \\(2\\) \u3092\u9078\u629e\u3057\u305f\u5f8c\u30012\u756a\u76ee\u306e\u30e9\u30a6\u30f3\u30c9\u3067 \\(1\\) \u307e\u305f\u306f \\(\\hat{1}\\) \u3092\u9078\u629e\u3057\u3066\u3082\u91cd\u8907\u5206\u5c90\u306b\u3064\u306a\u304c\u308b\u305f\u3081\u3001\u305d\u306e\u6642\u3082 \\(\\hat{1}\\) \u3092\u526a\u5b9a\u3057\u307e\u3059\u3002

        \u672c\u8cea\u7684\u306b\u3001\u79c1\u305f\u3061\u306e\u76ee\u6a19\u306f\u3001\u8907\u6570\u306e\u540c\u4e00\u8981\u7d20\u304c\u9078\u629e\u306e\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u4e00\u5ea6\u3060\u3051\u9078\u629e\u3055\u308c\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3059\u308b\u3053\u3068\u3067\u3059\u3002

        \u56f3 13-8 \u00a0 \u91cd\u8907\u9806\u5217\u306e\u526a\u5b9a

        "},{"location":"chapter_backtracking/permutations_problem/#2_1","title":"2. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        \u524d\u306e\u554f\u984c\u306e\u30b3\u30fc\u30c9\u306b\u57fa\u3065\u3044\u3066\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8 duplicated \u3092\u5c0e\u5165\u3057\u307e\u3059\u3002\u3053\u306e\u30bb\u30c3\u30c8\u306f\u3001\u3059\u3067\u306b\u8a66\u884c\u3057\u305f\u8981\u7d20\u3092\u8ffd\u8de1\u3057\u3001\u91cd\u8907\u3092\u526a\u5b9a\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby permutations_ii.py
        def backtrack(\n    state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]\n):\n    \"\"\"\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u9806\u5217 II\"\"\"\n    # \u72b6\u614b\u306e\u9577\u3055\u304c\u8981\u7d20\u6570\u3068\u7b49\u3057\u3044\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\n    if len(state) == len(choices):\n        res.append(list(state))\n        return\n    # \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    duplicated = set[int]()\n    for i, choice in enumerate(choices):\n        # \u679d\u5208\u308a\uff1a\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3092\u8a31\u53ef\u305b\u305a\u3001\u7b49\u3057\u3044\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3082\u8a31\u53ef\u3057\u306a\u3044\n        if not selected[i] and choice not in duplicated:\n            # \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            duplicated.add(choice)  # \u9078\u629e\u3055\u308c\u305f\u8981\u7d20\u5024\u3092\u8a18\u9332\n            selected[i] = True\n            state.append(choice)\n            # \u6b21\u306e\u9078\u629e\u30e9\u30a6\u30f3\u30c9\u306b\u9032\u3080\n            backtrack(state, choices, selected, res)\n            # \u64a4\u56de\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n            selected[i] = False\n            state.pop()\n\ndef permutations_ii(nums: list[int]) -> list[list[int]]:\n    \"\"\"\u9806\u5217 II\"\"\"\n    res = []\n    backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\n    return res\n
        permutations_ii.cpp
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u9806\u5217 II */\nvoid backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &selected, vector<vector<int>> &res) {\n    // \u72b6\u614b\u306e\u9577\u3055\u304c\u8981\u7d20\u6570\u3068\u7b49\u3057\u304f\u306a\u3063\u305f\u3089\u3001\u89e3\u3092\u8a18\u9332\n    if (state.size() == choices.size()) {\n        res.push_back(state);\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    unordered_set<int> duplicated;\n    for (int i = 0; i < choices.size(); i++) {\n        int choice = choices[i];\n        // \u526a\u5b9a\uff1a\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3092\u8a31\u53ef\u305b\u305a\u3001\u7b49\u3057\u3044\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3082\u8a31\u53ef\u3057\u306a\u3044\n        if (!selected[i] && duplicated.find(choice) == duplicated.end()) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            duplicated.emplace(choice); // \u9078\u629e\u3055\u308c\u305f\u8981\u7d20\u5024\u3092\u8a18\u9332\n            selected[i] = true;\n            state.push_back(choice);\n            // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n            selected[i] = false;\n            state.pop_back();\n        }\n    }\n}\n\n/* \u9806\u5217 II */\nvector<vector<int>> permutationsII(vector<int> nums) {\n    vector<int> state;\n    vector<bool> selected(nums.size(), false);\n    vector<vector<int>> res;\n    backtrack(state, nums, selected, res);\n    return res;\n}\n
        permutations_ii.java
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u9806\u5217 II */\nvoid backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {\n    // \u72b6\u614b\u306e\u9577\u3055\u304c\u8981\u7d20\u6570\u3068\u7b49\u3057\u304f\u306a\u3063\u305f\u3089\u3001\u89e3\u3092\u8a18\u9332\n    if (state.size() == choices.length) {\n        res.add(new ArrayList<Integer>(state));\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    Set<Integer> duplicated = new HashSet<Integer>();\n    for (int i = 0; i < choices.length; i++) {\n        int choice = choices[i];\n        // \u526a\u5b9a\uff1a\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3092\u8a31\u53ef\u305b\u305a\u3001\u7b49\u3057\u3044\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3082\u8a31\u53ef\u3057\u306a\u3044\n        if (!selected[i] && !duplicated.contains(choice)) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            duplicated.add(choice); // \u9078\u629e\u3055\u308c\u305f\u8981\u7d20\u5024\u3092\u8a18\u9332\n            selected[i] = true;\n            state.add(choice);\n            // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n            selected[i] = false;\n            state.remove(state.size() - 1);\n        }\n    }\n}\n\n/* \u9806\u5217 II */\nList<List<Integer>> permutationsII(int[] nums) {\n    List<List<Integer>> res = new ArrayList<List<Integer>>();\n    backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);\n    return res;\n}\n
        permutations_ii.cs
        [class]{permutations_ii}-[func]{Backtrack}\n\n[class]{permutations_ii}-[func]{PermutationsII}\n
        permutations_ii.go
        [class]{}-[func]{backtrackII}\n\n[class]{}-[func]{permutationsII}\n
        permutations_ii.swift
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsII}\n
        permutations_ii.js
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsII}\n
        permutations_ii.ts
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsII}\n
        permutations_ii.dart
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsII}\n
        permutations_ii.rs
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutations_ii}\n
        permutations_ii.c
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsII}\n
        permutations_ii.kt
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsII}\n
        permutations_ii.rb
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutations_ii}\n

        \u3059\u3079\u3066\u306e\u8981\u7d20\u304c\u7570\u306a\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\\(n\\) \u500b\u306e\u8981\u7d20\u306e\u9806\u5217\u306f \\(n!\\) \uff08\u968e\u4e57\uff09\u500b\u3042\u308a\u307e\u3059\u3002\u5404\u7d50\u679c\u3092\u8a18\u9332\u3059\u308b\u306b\u306f\u9577\u3055 \\(n\\) \u306e\u30ea\u30b9\u30c8\u3092\u30b3\u30d4\u30fc\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u3053\u308c\u306b\u306f \\(O(n)\\) \u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u7dcf\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n!n)\\) \u3067\u3059\u3002

        \u6700\u5927\u518d\u5e30\u6df1\u5ea6\u306f \\(n\\) \u3067\u3001\\(O(n)\\) \u306e\u30b9\u30bf\u30c3\u30af\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002selected \u914d\u5217\u3082 \\(O(n)\\) \u7a7a\u9593\u304c\u5fc5\u8981\u3067\u3059\u3002\u4e00\u5ea6\u306b\u6700\u5927 \\(n\\) \u500b\u306e\u500b\u5225\u306e duplicated \u30bb\u30c3\u30c8\u304c\u5b58\u5728\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3001\u305d\u308c\u3089\u306f\u96c6\u5408\u7684\u306b \\(O(n^2)\\) \u7a7a\u9593\u3092\u5360\u6709\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(n^2)\\) \u3067\u3059\u3002

        "},{"location":"chapter_backtracking/permutations_problem/#3-2","title":"3. \u00a0 2\u3064\u306e\u526a\u5b9a\u65b9\u6cd5\u306e\u6bd4\u8f03","text":"

        selected \u3068 duplicated \u306f\u3069\u3061\u3089\u3082\u526a\u5b9a\u30e1\u30ab\u30cb\u30ba\u30e0\u3068\u3057\u3066\u6a5f\u80fd\u3057\u307e\u3059\u304c\u3001\u7570\u306a\u308b\u554f\u984c\u3092\u30bf\u30fc\u30b2\u30c3\u30c8\u306b\u3057\u3066\u3044\u307e\u3059\uff1a

        • \u91cd\u8907\u9078\u629e\u306e\u526a\u5b9a\uff08selected \u7d4c\u7531\uff09\uff1a\u691c\u7d22\u5168\u4f53\u306b\u5358\u4e00\u306e selected \u914d\u5217\u304c\u3042\u308a\u3001\u73fe\u5728\u306e\u72b6\u614b\u306b\u3059\u3067\u306b\u3042\u308b\u8981\u7d20\u3092\u793a\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u540c\u3058\u8981\u7d20\u304c state \u306b\u8907\u6570\u56de\u73fe\u308c\u308b\u3053\u3068\u3092\u9632\u304e\u307e\u3059\u3002
        • \u7b49\u5024\u8981\u7d20\u306e\u526a\u5b9a\uff08duplicated \u7d4c\u7531\uff09\uff1abacktrack \u95a2\u6570\u306e\u5404\u547c\u3073\u51fa\u3057\u306f\u72ec\u81ea\u306e duplicated \u30bb\u30c3\u30c8\u3092\u4f7f\u7528\u3057\u3001\u305d\u306e\u7279\u5b9a\u306e\u53cd\u5fa9\uff08for \u30eb\u30fc\u30d7\uff09\u3067\u3059\u3067\u306b\u9078\u629e\u3055\u308c\u305f\u8981\u7d20\u3092\u8a18\u9332\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u7b49\u3057\u3044\u8981\u7d20\u304c\u9078\u629e\u306e\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u4e00\u5ea6\u3060\u3051\u9078\u629e\u3055\u308c\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3057\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306f\u3001\u3053\u308c\u30892\u3064\u306e\u526a\u5b9a\u6226\u7565\u306e\u7bc4\u56f2\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u6728\u306e\u5404\u30ce\u30fc\u30c9\u306f\u9078\u629e\u3092\u8868\u3057\u307e\u3059\u3002\u30eb\u30fc\u30c8\u304b\u3089\u4efb\u610f\u306e\u8449\u3078\u306e\u7d4c\u8def\u306f\u30011\u3064\u306e\u5b8c\u5168\u306a\u9806\u5217\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002

        \u56f3 13-9 \u00a0 2\u3064\u306e\u526a\u5b9a\u6761\u4ef6\u306e\u7bc4\u56f2

        "},{"location":"chapter_backtracking/subset_sum_problem/","title":"13.3 \u00a0 \u90e8\u5206\u96c6\u5408\u548c\u554f\u984c","text":""},{"location":"chapter_backtracking/subset_sum_problem/#1331","title":"13.3.1 \u00a0 \u91cd\u8907\u8981\u7d20\u304c\u306a\u3044\u5834\u5408","text":"

        Question

        \u6b63\u306e\u6574\u6570\u306e\u914d\u5217 nums \u3068\u30bf\u30fc\u30b2\u30c3\u30c8\u6b63\u6574\u6570 target \u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u7d44\u307f\u5408\u308f\u305b\u5185\u306e\u8981\u7d20\u306e\u548c\u304c target \u306b\u7b49\u3057\u304f\u306a\u308b\u3088\u3046\u306a\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u7d44\u307f\u5408\u308f\u305b\u3092\u898b\u3064\u3051\u3066\u304f\u3060\u3055\u3044\u3002\u4e0e\u3048\u3089\u308c\u305f\u914d\u5217\u306b\u306f\u91cd\u8907\u8981\u7d20\u304c\u306a\u304f\u3001\u5404\u8981\u7d20\u306f\u8907\u6570\u56de\u9078\u629e\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u7d44\u307f\u5408\u308f\u305b\u3092\u91cd\u8907\u3059\u308b\u7d44\u307f\u5408\u308f\u305b\u3092\u542b\u307e\u306a\u3044\u30ea\u30b9\u30c8\u3068\u3057\u3066\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u4f8b\u3048\u3070\u3001\u5165\u529b\u96c6\u5408 \\(\\{3, 4, 5\\}\\) \u3068\u30bf\u30fc\u30b2\u30c3\u30c8\u6574\u6570 \\(9\\) \u306e\u5834\u5408\u3001\u89e3\u306f \\(\\{3, 3, 3\\}, \\{4, 5\\}\\) \u3067\u3059\u3002\u4ee5\u4e0b\u306e2\u70b9\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        • \u5165\u529b\u96c6\u5408\u306e\u8981\u7d20\u306f\u7121\u5236\u9650\u306b\u9078\u629e\u3067\u304d\u307e\u3059\u3002
        • \u90e8\u5206\u96c6\u5408\u306f\u8981\u7d20\u306e\u9806\u5e8f\u3092\u533a\u5225\u3057\u307e\u305b\u3093\u3002\u4f8b\u3048\u3070 \\(\\{4, 5\\}\\) \u3068 \\(\\{5, 4\\}\\) \u306f\u540c\u3058\u90e8\u5206\u96c6\u5408\u3067\u3059\u3002
        "},{"location":"chapter_backtracking/subset_sum_problem/#1","title":"1. \u00a0 \u9806\u5217\u89e3\u6cd5\u306e\u53c2\u8003","text":"

        \u9806\u5217\u554f\u984c\u3068\u540c\u69d8\u306b\u3001\u90e8\u5206\u96c6\u5408\u306e\u751f\u6210\u3092\u4e00\u9023\u306e\u9078\u629e\u3068\u3057\u3066\u60f3\u50cf\u3067\u304d\u3001\u9078\u629e\u30d7\u30ed\u30bb\u30b9\u4e2d\u306b\u300c\u8981\u7d20\u548c\u300d\u3092\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u3067\u66f4\u65b0\u3067\u304d\u307e\u3059\u3002\u8981\u7d20\u548c\u304c target \u306b\u7b49\u3057\u304f\u306a\u3063\u305f\u3068\u304d\u3001\u90e8\u5206\u96c6\u5408\u3092\u7d50\u679c\u30ea\u30b9\u30c8\u306b\u8a18\u9332\u3057\u307e\u3059\u3002

        \u9806\u5217\u554f\u984c\u3068\u306f\u7570\u306a\u308a\u3001\u3053\u306e\u554f\u984c\u3067\u306f\u8981\u7d20\u306f\u7121\u5236\u9650\u306b\u9078\u629e\u3067\u304d\u308b\u305f\u3081\u3001\u8981\u7d20\u304c\u9078\u629e\u3055\u308c\u305f\u304b\u3069\u3046\u304b\u3092\u8a18\u9332\u3059\u308b\u305f\u3081\u306e selected \u30d6\u30fc\u30eb\u914d\u5217\u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u305b\u3093\u3002\u9806\u5217\u30b3\u30fc\u30c9\u306b\u8efd\u5fae\u306a\u4fee\u6b63\u3092\u52a0\u3048\u3066\u3001\u6700\u521d\u306b\u554f\u984c\u3092\u89e3\u6c7a\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby subset_sum_i_naive.py
        def backtrack(\n    state: list[int],\n    target: int,\n    total: int,\n    choices: list[int],\n    res: list[list[int]],\n):\n    \"\"\"\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c I\"\"\"\n    # \u90e8\u5206\u96c6\u5408\u306e\u548c\u304c target \u3068\u7b49\u3057\u3044\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\n    if total == target:\n        res.append(list(state))\n        return\n    # \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for i in range(len(choices)):\n        # \u679d\u5208\u308a\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c\u304c target \u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u305d\u306e\u9078\u629e\u3092\u30b9\u30ad\u30c3\u30d7\n        if total + choices[i] > target:\n            continue\n        # \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u8981\u7d20\u3068 total \u3092\u66f4\u65b0\n        state.append(choices[i])\n        # \u6b21\u306e\u9078\u629e\u30e9\u30a6\u30f3\u30c9\u306b\u9032\u3080\n        backtrack(state, target, total + choices[i], choices, res)\n        # \u64a4\u56de\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n        state.pop()\n\ndef subset_sum_i_naive(nums: list[int], target: int) -> list[list[int]]:\n    \"\"\"\u90e8\u5206\u96c6\u5408\u306e\u548c I \u3092\u89e3\u304f\uff08\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u3092\u542b\u3080\uff09\"\"\"\n    state = []  # \u72b6\u614b\uff08\u90e8\u5206\u96c6\u5408\uff09\n    total = 0  # \u90e8\u5206\u96c6\u5408\u306e\u548c\n    res = []  # \u7d50\u679c\u30ea\u30b9\u30c8\uff08\u90e8\u5206\u96c6\u5408\u30ea\u30b9\u30c8\uff09\n    backtrack(state, target, total, nums, res)\n    return res\n
        subset_sum_i_naive.cpp
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u90e8\u5206\u96c6\u5408\u548c I */\nvoid backtrack(vector<int> &state, int target, int total, vector<int> &choices, vector<vector<int>> &res) {\n    // \u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3068\u7b49\u3057\u3044\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\n    if (total == target) {\n        res.push_back(state);\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for (int i = 0; i < choices.size(); i++) {\n        // \u526a\u5b9a\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u305d\u306e\u9078\u629e\u3092\u30b9\u30ad\u30c3\u30d7\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u8981\u7d20\u3068total\u3092\u66f4\u65b0\n        state.push_back(choices[i]);\n        // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n        backtrack(state, target, total + choices[i], choices, res);\n        // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n        state.pop_back();\n    }\n}\n\n/* \u90e8\u5206\u96c6\u5408\u548c I \u3092\u89e3\u304f\uff08\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u3092\u542b\u3080\uff09 */\nvector<vector<int>> subsetSumINaive(vector<int> nums, int target) {\n    vector<int> state;       // \u72b6\u614b\uff08\u90e8\u5206\u96c6\u5408\uff09\n    int total = 0;           // \u90e8\u5206\u96c6\u5408\u306e\u548c\n    vector<vector<int>> res; // \u7d50\u679c\u30ea\u30b9\u30c8\uff08\u90e8\u5206\u96c6\u5408\u30ea\u30b9\u30c8\uff09\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
        subset_sum_i_naive.java
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u90e8\u5206\u96c6\u5408\u548c I */\nvoid backtrack(List<Integer> state, int target, int total, int[] choices, List<List<Integer>> res) {\n    // \u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3068\u7b49\u3057\u3044\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\n    if (total == target) {\n        res.add(new ArrayList<>(state));\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for (int i = 0; i < choices.length; i++) {\n        // \u526a\u5b9a\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u305d\u306e\u9078\u629e\u3092\u30b9\u30ad\u30c3\u30d7\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u8981\u7d20\u3068total\u3092\u66f4\u65b0\n        state.add(choices[i]);\n        // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n        backtrack(state, target, total + choices[i], choices, res);\n        // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n        state.remove(state.size() - 1);\n    }\n}\n\n/* \u90e8\u5206\u96c6\u5408\u548c I \u3092\u89e3\u304f\uff08\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u3092\u542b\u3080\uff09 */\nList<List<Integer>> subsetSumINaive(int[] nums, int target) {\n    List<Integer> state = new ArrayList<>(); // \u72b6\u614b\uff08\u90e8\u5206\u96c6\u5408\uff09\n    int total = 0; // \u90e8\u5206\u96c6\u5408\u306e\u548c\n    List<List<Integer>> res = new ArrayList<>(); // \u7d50\u679c\u30ea\u30b9\u30c8\uff08\u90e8\u5206\u96c6\u5408\u30ea\u30b9\u30c8\uff09\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
        subset_sum_i_naive.cs
        [class]{subset_sum_i_naive}-[func]{Backtrack}\n\n[class]{subset_sum_i_naive}-[func]{SubsetSumINaive}\n
        subset_sum_i_naive.go
        [class]{}-[func]{backtrackSubsetSumINaive}\n\n[class]{}-[func]{subsetSumINaive}\n
        subset_sum_i_naive.swift
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumINaive}\n
        subset_sum_i_naive.js
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumINaive}\n
        subset_sum_i_naive.ts
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumINaive}\n
        subset_sum_i_naive.dart
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumINaive}\n
        subset_sum_i_naive.rs
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subset_sum_i_naive}\n
        subset_sum_i_naive.c
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumINaive}\n
        subset_sum_i_naive.kt
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumINaive}\n
        subset_sum_i_naive.rb
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subset_sum_i_naive}\n

        \u914d\u5217 \\([3, 4, 5]\\) \u3068\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20 \\(9\\) \u3092\u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u306b\u5165\u529b\u3059\u308b\u3068\u3001\u7d50\u679c \\([3, 3, 3], [4, 5], [5, 4]\\) \u304c\u5f97\u3089\u308c\u307e\u3059\u3002\u548c\u304c \\(9\\) \u306e\u3059\u3079\u3066\u306e\u90e8\u5206\u96c6\u5408\u3092\u6b63\u5e38\u306b\u898b\u3064\u3051\u307e\u3057\u305f\u304c\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408 \\([4, 5]\\) \u3068 \\([5, 4]\\) \u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002

        \u3053\u308c\u306f\u3001\u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u304c\u9078\u629e\u306e\u9806\u5e8f\u3092\u533a\u5225\u3059\u308b\u305f\u3081\u3067\u3059\u304c\u3001\u90e8\u5206\u96c6\u5408\u306f\u9078\u629e\u9806\u5e8f\u3092\u533a\u5225\u3057\u307e\u305b\u3093\u3002\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\\(5\\) \u306e\u524d\u306b \\(4\\) \u3092\u9078\u629e\u3059\u308b\u3053\u3068\u3068 \\(4\\) \u306e\u524d\u306b \\(5\\) \u3092\u9078\u629e\u3059\u308b\u3053\u3068\u306f\u7570\u306a\u308b\u5206\u5c90\u3067\u3059\u304c\u3001\u540c\u3058\u90e8\u5206\u96c6\u5408\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002

        \u56f3 13-10 \u00a0 \u90e8\u5206\u96c6\u5408\u306e\u691c\u7d22\u3068\u5883\u754c\u5916\u306e\u526a\u5b9a

        \u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u3092\u9664\u53bb\u3059\u308b\u305f\u3081\u306b\u3001\u76f4\u63a5\u7684\u306a\u30a2\u30a4\u30c7\u30a2\u306f\u7d50\u679c\u30ea\u30b9\u30c8\u3092\u91cd\u8907\u9664\u53bb\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u3053\u306e\u65b9\u6cd5\u306f2\u3064\u306e\u7406\u7531\u3067\u975e\u5e38\u306b\u975e\u52b9\u7387\u7684\u3067\u3059\u3002

        • \u914d\u5217\u8981\u7d20\u304c\u591a\u3044\u5834\u5408\u3001\u7279\u306b target \u304c\u5927\u304d\u3044\u5834\u5408\u3001\u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u3067\u5927\u91cf\u306e\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u304c\u751f\u6210\u3055\u308c\u307e\u3059\u3002
        • \u90e8\u5206\u96c6\u5408\uff08\u914d\u5217\uff09\u306e\u5dee\u7570\u3092\u6bd4\u8f03\u3059\u308b\u3053\u3068\u306f\u975e\u5e38\u306b\u6642\u9593\u304c\u304b\u304b\u308a\u3001\u307e\u305a\u914d\u5217\u3092\u30bd\u30fc\u30c8\u3057\u3001\u6b21\u306b\u914d\u5217\u306e\u5404\u8981\u7d20\u306e\u5dee\u7570\u3092\u6bd4\u8f03\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        "},{"location":"chapter_backtracking/subset_sum_problem/#2","title":"2. \u00a0 \u91cd\u8907\u90e8\u5206\u96c6\u5408\u306e\u526a\u5b9a","text":"

        \u526a\u5b9a\u3092\u901a\u3058\u3066\u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u4e2d\u306b\u91cd\u8907\u9664\u53bb\u3092\u691c\u8a0e\u3057\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\u7570\u306a\u308b\u9806\u5e8f\u3067\u914d\u5217\u8981\u7d20\u3092\u9078\u629e\u3059\u308b\u3068\u304d\u306b\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u304c\u751f\u6210\u3055\u308c\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u72b6\u6cc1\u3067\u3059\u3002

        1. \u6700\u521d\u306e\u30e9\u30a6\u30f3\u30c9\u3067 \\(3\\) \u3092\u9078\u629e\u3057\u30012\u756a\u76ee\u306e\u30e9\u30a6\u30f3\u30c9\u3067 \\(4\\) \u3092\u9078\u629e\u3059\u308b\u3068\u3001\u3053\u308c\u30892\u3064\u306e\u8981\u7d20\u3092\u542b\u3080\u3059\u3079\u3066\u306e\u90e8\u5206\u96c6\u5408\u304c\u751f\u6210\u3055\u308c\u3001\\([3, 4, \\dots]\\) \u3068\u8868\u8a18\u3055\u308c\u307e\u3059\u3002
        2. \u5f8c\u3067\u3001\u6700\u521d\u306e\u30e9\u30a6\u30f3\u30c9\u3067 \\(4\\) \u304c\u9078\u629e\u3055\u308c\u305f\u3068\u304d\u30012\u756a\u76ee\u306e\u30e9\u30a6\u30f3\u30c9\u306f \\(3\\) \u3092\u30b9\u30ad\u30c3\u30d7\u3059\u3079\u304d\u3067\u3059\u3002\u3053\u306e\u9078\u629e\u306b\u3088\u3063\u3066\u751f\u6210\u3055\u308c\u308b\u90e8\u5206\u96c6\u5408 \\([4, 3, \\dots]\\) \u306f\u30b9\u30c6\u30c3\u30d7 1. \u306e\u90e8\u5206\u96c6\u5408\u3068\u5b8c\u5168\u306b\u91cd\u8907\u3059\u308b\u304b\u3089\u3067\u3059\u3002

        \u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u3067\u306f\u3001\u5404\u5c64\u306e\u9078\u629e\u304c\u5de6\u304b\u3089\u53f3\u306b\u4e00\u3064\u305a\u3064\u8a66\u884c\u3055\u308c\u308b\u305f\u3081\u3001\u53f3\u5074\u306e\u5206\u5c90\u307b\u3069\u3088\u308a\u591a\u304f\u526a\u5b9a\u3055\u308c\u307e\u3059\u3002

        1. \u6700\u521d\u306e2\u30e9\u30a6\u30f3\u30c9\u3067 \\(3\\) \u3068 \\(5\\) \u3092\u9078\u629e\u3057\u3001\u90e8\u5206\u96c6\u5408 \\([3, 5, \\dots]\\) \u3092\u751f\u6210\u3057\u307e\u3059\u3002
        2. \u6700\u521d\u306e2\u30e9\u30a6\u30f3\u30c9\u3067 \\(4\\) \u3068 \\(5\\) \u3092\u9078\u629e\u3057\u3001\u90e8\u5206\u96c6\u5408 \\([4, 5, \\dots]\\) \u3092\u751f\u6210\u3057\u307e\u3059\u3002
        3. \u6700\u521d\u306e\u30e9\u30a6\u30f3\u30c9\u3067 \\(5\\) \u304c\u9078\u629e\u3055\u308c\u305f\u5834\u5408\u30012\u756a\u76ee\u306e\u30e9\u30a6\u30f3\u30c9\u306f \\(3\\) \u3068 \\(4\\) \u3092\u30b9\u30ad\u30c3\u30d7\u3059\u3079\u304d\u3067\u3059\u3002\u90e8\u5206\u96c6\u5408 \\([5, 3, \\dots]\\) \u3068 \\([5, 4, \\dots]\\) \u306f\u30b9\u30c6\u30c3\u30d7 1. \u3068 2. \u3067\u8a18\u8ff0\u3055\u308c\u305f\u90e8\u5206\u96c6\u5408\u3068\u5b8c\u5168\u306b\u91cd\u8907\u3059\u308b\u304b\u3089\u3067\u3059\u3002

        \u56f3 13-11 \u00a0 \u7570\u306a\u308b\u9078\u629e\u9806\u5e8f\u306b\u3088\u308b\u91cd\u8907\u90e8\u5206\u96c6\u5408

        \u8981\u7d04\u3059\u308b\u3068\u3001\u5165\u529b\u914d\u5217 \\([x_1, x_2, \\dots, x_n]\\) \u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u3067\u306e\u9078\u629e\u30b7\u30fc\u30b1\u30f3\u30b9\u306f \\([x_{i_1}, x_{i_2}, \\dots, x_{i_m}]\\) \u3067\u3042\u308b\u3079\u304d\u3067\u3001\\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\) \u3092\u6e80\u305f\u3059\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u6761\u4ef6\u3092\u6e80\u305f\u3055\u306a\u3044\u9078\u629e\u30b7\u30fc\u30b1\u30f3\u30b9\u306f\u91cd\u8907\u3092\u5f15\u304d\u8d77\u3053\u3057\u3001\u526a\u5b9a\u3055\u308c\u308b\u3079\u304d\u3067\u3059\u3002

        "},{"location":"chapter_backtracking/subset_sum_problem/#3","title":"3. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        \u3053\u306e\u526a\u5b9a\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306b\u3001\u5909\u6570 start \u3092\u521d\u671f\u5316\u3057\u3001\u3053\u308c\u306f\u8d70\u67fb\u306e\u958b\u59cb\u70b9\u3092\u793a\u3057\u307e\u3059\u3002\u9078\u629e \\(x_{i}\\) \u3092\u884c\u3063\u305f\u5f8c\u3001\u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u3092\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 \\(i\\) \u304b\u3089\u958b\u59cb\u3059\u308b\u3088\u3046\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u9078\u629e\u30b7\u30fc\u30b1\u30f3\u30b9\u304c \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\) \u3092\u6e80\u305f\u3059\u3053\u3068\u304c\u4fdd\u8a3c\u3055\u308c\u3001\u90e8\u5206\u96c6\u5408\u306e\u4e00\u610f\u6027\u304c\u4fdd\u8a3c\u3055\u308c\u307e\u3059\u3002

        \u3055\u3089\u306b\u3001\u30b3\u30fc\u30c9\u306b\u4ee5\u4e0b\u306e2\u3064\u306e\u6700\u9069\u5316\u3092\u884c\u3044\u307e\u3057\u305f\u3002

        • \u691c\u7d22\u3092\u958b\u59cb\u3059\u308b\u524d\u306b\u3001\u914d\u5217 nums \u3092\u30bd\u30fc\u30c8\u3057\u307e\u3059\u3002\u3059\u3079\u3066\u306e\u9078\u629e\u306e\u8d70\u67fb\u3067\u3001\u90e8\u5206\u96c6\u5408\u548c\u304c target \u3092\u8d85\u3048\u305f\u3068\u304d\u306b\u30eb\u30fc\u30d7\u3092\u76f4\u63a5\u7d42\u4e86\u3057\u307e\u3059\u3002\u5f8c\u7d9a\u306e\u8981\u7d20\u306f\u3088\u308a\u5927\u304d\u304f\u3001\u305d\u308c\u3089\u306e\u90e8\u5206\u96c6\u5408\u548c\u306f\u78ba\u5b9f\u306b target \u3092\u8d85\u3048\u308b\u304b\u3089\u3067\u3059\u3002
        • \u8981\u7d20\u548c\u5909\u6570 total \u3092\u9664\u53bb\u3057\u3001target \u306b\u5bfe\u3057\u3066\u6e1b\u7b97\u3092\u5b9f\u884c\u3057\u3066\u8981\u7d20\u548c\u3092\u30ab\u30a6\u30f3\u30c8\u3057\u307e\u3059\u3002target \u304c \\(0\\) \u306b\u7b49\u3057\u304f\u306a\u3063\u305f\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\u3057\u307e\u3059\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby subset_sum_i.py
        def backtrack(\n    state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n    \"\"\"\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c I\"\"\"\n    # \u90e8\u5206\u96c6\u5408\u306e\u548c\u304c target \u3068\u7b49\u3057\u3044\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\n    if target == 0:\n        res.append(list(state))\n        return\n    # \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    # \u679d\u5208\u308a\u4e8c\uff1astart \u304b\u3089\u8d70\u67fb\u3092\u958b\u59cb\u3057\u3066\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u306e\u751f\u6210\u3092\u907f\u3051\u308b\n    for i in range(start, len(choices)):\n        # \u679d\u5208\u308a\u4e00\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c\u304c target \u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u76f4\u3061\u306b\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\n        # \u3053\u308c\u306f\u914d\u5217\u304c\u30bd\u30fc\u30c8\u3055\u308c\u3066\u304a\u308a\u3001\u5f8c\u306e\u8981\u7d20\u304c\u3088\u308a\u5927\u304d\u3044\u305f\u3081\u3001\u90e8\u5206\u96c6\u5408\u306e\u548c\u306f\u5fc5\u305a target \u3092\u8d85\u3048\u308b\u305f\u3081\n        if target - choices[i] < 0:\n            break\n        # \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001target\u3001start \u3092\u66f4\u65b0\n        state.append(choices[i])\n        # \u6b21\u306e\u9078\u629e\u30e9\u30a6\u30f3\u30c9\u306b\u9032\u3080\n        backtrack(state, target - choices[i], choices, i, res)\n        # \u64a4\u56de\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n        state.pop()\n\ndef subset_sum_i(nums: list[int], target: int) -> list[list[int]]:\n    \"\"\"\u90e8\u5206\u96c6\u5408\u306e\u548c I \u3092\u89e3\u304f\"\"\"\n    state = []  # \u72b6\u614b\uff08\u90e8\u5206\u96c6\u5408\uff09\n    nums.sort()  # nums \u3092\u30bd\u30fc\u30c8\n    start = 0  # \u8d70\u67fb\u306e\u958b\u59cb\u70b9\n    res = []  # \u7d50\u679c\u30ea\u30b9\u30c8\uff08\u90e8\u5206\u96c6\u5408\u30ea\u30b9\u30c8\uff09\n    backtrack(state, target, nums, start, res)\n    return res\n
        subset_sum_i.cpp
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u90e8\u5206\u96c6\u5408\u548c I */\nvoid backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {\n    // \u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3068\u7b49\u3057\u3044\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\n    if (target == 0) {\n        res.push_back(state);\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    // \u526a\u5b9a\u4e8c\uff1astart\u304b\u3089\u8d70\u67fb\u3092\u958b\u59cb\u3057\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u306e\u751f\u6210\u3092\u56de\u907f\n    for (int i = start; i < choices.size(); i++) {\n        // \u526a\u5b9a\u4e00\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u5373\u5ea7\u306b\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\n        // \u914d\u5217\u304c\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u5f8c\u306e\u8981\u7d20\u306f\u3055\u3089\u306b\u5927\u304d\u304f\u3001\u90e8\u5206\u96c6\u5408\u306e\u548c\u306f\u5fc5\u305atarget\u3092\u8d85\u3048\u308b\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001target\u3001start\u3092\u66f4\u65b0\n        state.push_back(choices[i]);\n        // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n        backtrack(state, target - choices[i], choices, i, res);\n        // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n        state.pop_back();\n    }\n}\n\n/* \u90e8\u5206\u96c6\u5408\u548c I \u3092\u89e3\u304f */\nvector<vector<int>> subsetSumI(vector<int> nums, int target) {\n    vector<int> state;              // \u72b6\u614b\uff08\u90e8\u5206\u96c6\u5408\uff09\n    sort(nums.begin(), nums.end()); // nums \u3092\u30bd\u30fc\u30c8\n    int start = 0;                  // \u8d70\u67fb\u306e\u958b\u59cb\u70b9\n    vector<vector<int>> res;        // \u7d50\u679c\u30ea\u30b9\u30c8\uff08\u90e8\u5206\u96c6\u5408\u30ea\u30b9\u30c8\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_i.java
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u90e8\u5206\u96c6\u5408\u548c I */\nvoid backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {\n    // \u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3068\u7b49\u3057\u3044\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\n    if (target == 0) {\n        res.add(new ArrayList<>(state));\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    // \u526a\u5b9a\u4e8c\uff1astart\u304b\u3089\u8d70\u67fb\u3092\u958b\u59cb\u3057\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u306e\u751f\u6210\u3092\u56de\u907f\n    for (int i = start; i < choices.length; i++) {\n        // \u526a\u5b9a\u4e00\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u5373\u5ea7\u306b\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\n        // \u914d\u5217\u304c\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u5f8c\u306e\u8981\u7d20\u306f\u3055\u3089\u306b\u5927\u304d\u304f\u3001\u90e8\u5206\u96c6\u5408\u306e\u548c\u306f\u5fc5\u305atarget\u3092\u8d85\u3048\u308b\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001target\u3001start\u3092\u66f4\u65b0\n        state.add(choices[i]);\n        // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n        backtrack(state, target - choices[i], choices, i, res);\n        // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n        state.remove(state.size() - 1);\n    }\n}\n\n/* \u90e8\u5206\u96c6\u5408\u548c I \u3092\u89e3\u304f */\nList<List<Integer>> subsetSumI(int[] nums, int target) {\n    List<Integer> state = new ArrayList<>(); // \u72b6\u614b\uff08\u90e8\u5206\u96c6\u5408\uff09\n    Arrays.sort(nums); // nums \u3092\u30bd\u30fc\u30c8\n    int start = 0; // \u8d70\u67fb\u306e\u958b\u59cb\u70b9\n    List<List<Integer>> res = new ArrayList<>(); // \u7d50\u679c\u30ea\u30b9\u30c8\uff08\u90e8\u5206\u96c6\u5408\u30ea\u30b9\u30c8\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_i.cs
        [class]{subset_sum_i}-[func]{Backtrack}\n\n[class]{subset_sum_i}-[func]{SubsetSumI}\n
        subset_sum_i.go
        [class]{}-[func]{backtrackSubsetSumI}\n\n[class]{}-[func]{subsetSumI}\n
        subset_sum_i.swift
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumI}\n
        subset_sum_i.js
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumI}\n
        subset_sum_i.ts
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumI}\n
        subset_sum_i.dart
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumI}\n
        subset_sum_i.rs
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subset_sum_i}\n
        subset_sum_i.c
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumI}\n
        subset_sum_i.kt
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumI}\n
        subset_sum_i.rb
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subset_sum_i}\n

        \u4ee5\u4e0b\u306e\u56f3\u306f\u3001\u914d\u5217 \\([3, 4, 5]\\) \u3068\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20 \\(9\\) \u3092\u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u306b\u5165\u529b\u3057\u305f\u5f8c\u306e\u5168\u4f53\u7684\u306a\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 13-12 \u00a0 \u90e8\u5206\u96c6\u5408\u548c I \u306e\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9

        "},{"location":"chapter_backtracking/subset_sum_problem/#1332","title":"13.3.2 \u00a0 \u91cd\u8907\u8981\u7d20\u304c\u3042\u308b\u5834\u5408\u3092\u8003\u616e","text":"

        Question

        \u6b63\u306e\u6574\u6570\u306e\u914d\u5217 nums \u3068\u30bf\u30fc\u30b2\u30c3\u30c8\u6b63\u6574\u6570 target \u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u7d44\u307f\u5408\u308f\u305b\u5185\u306e\u8981\u7d20\u306e\u548c\u304c target \u306b\u7b49\u3057\u304f\u306a\u308b\u3088\u3046\u306a\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u7d44\u307f\u5408\u308f\u305b\u3092\u898b\u3064\u3051\u3066\u304f\u3060\u3055\u3044\u3002\u4e0e\u3048\u3089\u308c\u305f\u914d\u5217\u306b\u306f\u91cd\u8907\u8981\u7d20\u304c\u542b\u307e\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u5404\u8981\u7d20\u306f\u4e00\u5ea6\u3060\u3051\u9078\u629e\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u7d44\u307f\u5408\u308f\u305b\u3092\u91cd\u8907\u3059\u308b\u7d44\u307f\u5408\u308f\u305b\u3092\u542b\u307e\u306a\u3044\u30ea\u30b9\u30c8\u3068\u3057\u3066\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u524d\u306e\u554f\u984c\u3068\u6bd4\u8f03\u3057\u3066\u3001\u3053\u306e\u554f\u984c\u306e\u5165\u529b\u914d\u5217\u306b\u306f\u91cd\u8907\u8981\u7d20\u304c\u542b\u307e\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u65b0\u3057\u3044\u554f\u984c\u304c\u5c0e\u5165\u3055\u308c\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u914d\u5217 \\([4, \\hat{4}, 5]\\) \u3068\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20 \\(9\\) \u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u65e2\u5b58\u306e\u30b3\u30fc\u30c9\u306e\u51fa\u529b\u7d50\u679c\u306f \\([4, 5], [\\hat{4}, 5]\\) \u3068\u306a\u308a\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u304c\u751f\u6210\u3055\u308c\u307e\u3059\u3002

        \u3053\u306e\u91cd\u8907\u306e\u7406\u7531\u306f\u3001\u7279\u5b9a\u306e\u30e9\u30a6\u30f3\u30c9\u3067\u7b49\u3057\u3044\u8981\u7d20\u304c\u8907\u6570\u56de\u9078\u629e\u3055\u308c\u308b\u3053\u3068\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u3067\u306f\u3001\u6700\u521d\u306e\u30e9\u30a6\u30f3\u30c9\u306b3\u3064\u306e\u9078\u629e\u80a2\u304c\u3042\u308a\u3001\u305d\u306e\u3046\u30612\u3064\u304c \\(4\\) \u3067\u3042\u308a\u30012\u3064\u306e\u91cd\u8907\u3059\u308b\u691c\u7d22\u5206\u5c90\u3092\u751f\u6210\u3057\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u3092\u51fa\u529b\u3057\u307e\u3059\u3002\u540c\u69d8\u306b\u30012\u756a\u76ee\u306e\u30e9\u30a6\u30f3\u30c9\u306e2\u3064\u306e \\(4\\) \u3082\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u3092\u751f\u6210\u3057\u307e\u3059\u3002

        \u56f3 13-13 \u00a0 \u7b49\u3057\u3044\u8981\u7d20\u306b\u3088\u308b\u91cd\u8907\u90e8\u5206\u96c6\u5408

        "},{"location":"chapter_backtracking/subset_sum_problem/#1_1","title":"1. \u00a0 \u7b49\u5024\u8981\u7d20\u306e\u526a\u5b9a","text":"

        \u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3001\u7b49\u3057\u3044\u8981\u7d20\u304c\u30e9\u30a6\u30f3\u30c9\u3054\u3068\u306b\u4e00\u5ea6\u3060\u3051\u9078\u629e\u3055\u308c\u308b\u3088\u3046\u306b\u5236\u9650\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u5b9f\u88c5\u306f\u975e\u5e38\u306b\u5de7\u5999\u3067\u3059\uff1a\u914d\u5217\u304c\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u7b49\u3057\u3044\u8981\u7d20\u306f\u96a3\u63a5\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u7279\u5b9a\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u3067\u3001\u73fe\u5728\u306e\u8981\u7d20\u304c\u305d\u306e\u5de6\u5074\u306e\u8981\u7d20\u3068\u7b49\u3057\u3044\u5834\u5408\u3001\u305d\u308c\u306f\u3059\u3067\u306b\u9078\u629e\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u305f\u3081\u3001\u73fe\u5728\u306e\u8981\u7d20\u3092\u76f4\u63a5\u30b9\u30ad\u30c3\u30d7\u3057\u307e\u3059\u3002

        \u540c\u6642\u306b\u3001\u3053\u306e\u554f\u984c\u3067\u306f\u5404\u914d\u5217\u8981\u7d20\u306f\u4e00\u5ea6\u3060\u3051\u9078\u629e\u3067\u304d\u308b\u3068\u898f\u5b9a\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u5e78\u3044\u3001\u5909\u6570 start \u3092\u4f7f\u7528\u3057\u3066\u3053\u306e\u5236\u7d04\u3082\u6e80\u305f\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a\u9078\u629e \\(x_{i}\\) \u3092\u884c\u3063\u305f\u5f8c\u3001\u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u3092\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 \\(i + 1\\) \u304b\u3089\u524d\u65b9\u306b\u958b\u59cb\u3059\u308b\u3088\u3046\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u304c\u9664\u53bb\u3055\u308c\u308b\u3060\u3051\u3067\u306a\u304f\u3001\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3082\u56de\u907f\u3055\u308c\u307e\u3059\u3002

        "},{"location":"chapter_backtracking/subset_sum_problem/#2_1","title":"2. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby subset_sum_ii.py
        def backtrack(\n    state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n    \"\"\"\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c II\"\"\"\n    # \u90e8\u5206\u96c6\u5408\u306e\u548c\u304c target \u3068\u7b49\u3057\u3044\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\n    if target == 0:\n        res.append(list(state))\n        return\n    # \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    # \u679d\u5208\u308a\u4e8c\uff1astart \u304b\u3089\u8d70\u67fb\u3092\u958b\u59cb\u3057\u3066\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u306e\u751f\u6210\u3092\u907f\u3051\u308b\n    # \u679d\u5208\u308a\u4e09\uff1astart \u304b\u3089\u8d70\u67fb\u3092\u958b\u59cb\u3057\u3066\u540c\u3058\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3092\u907f\u3051\u308b\n    for i in range(start, len(choices)):\n        # \u679d\u5208\u308a\u4e00\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c\u304c target \u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u76f4\u3061\u306b\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\n        # \u3053\u308c\u306f\u914d\u5217\u304c\u30bd\u30fc\u30c8\u3055\u308c\u3066\u304a\u308a\u3001\u5f8c\u306e\u8981\u7d20\u304c\u3088\u308a\u5927\u304d\u3044\u305f\u3081\u3001\u90e8\u5206\u96c6\u5408\u306e\u548c\u306f\u5fc5\u305a target \u3092\u8d85\u3048\u308b\u305f\u3081\n        if target - choices[i] < 0:\n            break\n        # \u679d\u5208\u308a\u56db\uff1a\u8981\u7d20\u304c\u5de6\u306e\u8981\u7d20\u3068\u7b49\u3057\u3044\u5834\u5408\u3001\u691c\u7d22\u5206\u5c90\u304c\u91cd\u8907\u3057\u3066\u3044\u308b\u3053\u3068\u3092\u793a\u3059\u305f\u3081\u3001\u30b9\u30ad\u30c3\u30d7\n        if i > start and choices[i] == choices[i - 1]:\n            continue\n        # \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001target\u3001start \u3092\u66f4\u65b0\n        state.append(choices[i])\n        # \u6b21\u306e\u9078\u629e\u30e9\u30a6\u30f3\u30c9\u306b\u9032\u3080\n        backtrack(state, target - choices[i], choices, i + 1, res)\n        # \u64a4\u56de\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n        state.pop()\n\ndef subset_sum_ii(nums: list[int], target: int) -> list[list[int]]:\n    \"\"\"\u90e8\u5206\u96c6\u5408\u306e\u548c II \u3092\u89e3\u304f\"\"\"\n    state = []  # \u72b6\u614b\uff08\u90e8\u5206\u96c6\u5408\uff09\n    nums.sort()  # nums \u3092\u30bd\u30fc\u30c8\n    start = 0  # \u8d70\u67fb\u306e\u958b\u59cb\u70b9\n    res = []  # \u7d50\u679c\u30ea\u30b9\u30c8\uff08\u90e8\u5206\u96c6\u5408\u30ea\u30b9\u30c8\uff09\n    backtrack(state, target, nums, start, res)\n    return res\n
        subset_sum_ii.cpp
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u90e8\u5206\u96c6\u5408\u548c II */\nvoid backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {\n    // \u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3068\u7b49\u3057\u3044\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\n    if (target == 0) {\n        res.push_back(state);\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    // \u526a\u5b9a\u4e8c\uff1astart\u304b\u3089\u8d70\u67fb\u3092\u958b\u59cb\u3057\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u306e\u751f\u6210\u3092\u56de\u907f\n    // \u526a\u5b9a\u4e09\uff1astart\u304b\u3089\u8d70\u67fb\u3092\u958b\u59cb\u3057\u3001\u540c\u3058\u8981\u7d20\u306e\u7e70\u308a\u8fd4\u3057\u9078\u629e\u3092\u56de\u907f\n    for (int i = start; i < choices.size(); i++) {\n        // \u526a\u5b9a\u4e00\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u5373\u5ea7\u306b\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\n        // \u914d\u5217\u304c\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u5f8c\u306e\u8981\u7d20\u306f\u3055\u3089\u306b\u5927\u304d\u304f\u3001\u90e8\u5206\u96c6\u5408\u306e\u548c\u306f\u5fc5\u305atarget\u3092\u8d85\u3048\u308b\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u526a\u5b9a\u56db\uff1a\u8981\u7d20\u304c\u5de6\u306e\u8981\u7d20\u3068\u7b49\u3057\u3044\u5834\u5408\u3001\u691c\u7d22\u30d6\u30e9\u30f3\u30c1\u306e\u91cd\u8907\u3092\u793a\u3059\u306e\u3067\u30b9\u30ad\u30c3\u30d7\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001target\u3001start\u3092\u66f4\u65b0\n        state.push_back(choices[i]);\n        // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n        state.pop_back();\n    }\n}\n\n/* \u90e8\u5206\u96c6\u5408\u548c II \u3092\u89e3\u304f */\nvector<vector<int>> subsetSumII(vector<int> nums, int target) {\n    vector<int> state;              // \u72b6\u614b\uff08\u90e8\u5206\u96c6\u5408\uff09\n    sort(nums.begin(), nums.end()); // nums \u3092\u30bd\u30fc\u30c8\n    int start = 0;                  // \u8d70\u67fb\u306e\u958b\u59cb\u70b9\n    vector<vector<int>> res;        // \u7d50\u679c\u30ea\u30b9\u30c8\uff08\u90e8\u5206\u96c6\u5408\u30ea\u30b9\u30c8\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_ii.java
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u90e8\u5206\u96c6\u5408\u548c II */\nvoid backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {\n    // \u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3068\u7b49\u3057\u3044\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\n    if (target == 0) {\n        res.add(new ArrayList<>(state));\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    // \u526a\u5b9a\u4e8c\uff1astart\u304b\u3089\u8d70\u67fb\u3092\u958b\u59cb\u3057\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u306e\u751f\u6210\u3092\u56de\u907f\n    // \u526a\u5b9a\u4e09\uff1astart\u304b\u3089\u8d70\u67fb\u3092\u958b\u59cb\u3057\u3001\u540c\u3058\u8981\u7d20\u306e\u7e70\u308a\u8fd4\u3057\u9078\u629e\u3092\u56de\u907f\n    for (int i = start; i < choices.length; i++) {\n        // \u526a\u5b9a\u4e00\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u5373\u5ea7\u306b\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\n        // \u914d\u5217\u304c\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u5f8c\u306e\u8981\u7d20\u306f\u3055\u3089\u306b\u5927\u304d\u304f\u3001\u90e8\u5206\u96c6\u5408\u306e\u548c\u306f\u5fc5\u305atarget\u3092\u8d85\u3048\u308b\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u526a\u5b9a\u56db\uff1a\u8981\u7d20\u304c\u5de6\u306e\u8981\u7d20\u3068\u7b49\u3057\u3044\u5834\u5408\u3001\u691c\u7d22\u30d6\u30e9\u30f3\u30c1\u306e\u91cd\u8907\u3092\u793a\u3059\u306e\u3067\u30b9\u30ad\u30c3\u30d7\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001target\u3001start\u3092\u66f4\u65b0\n        state.add(choices[i]);\n        // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n        state.remove(state.size() - 1);\n    }\n}\n\n/* \u90e8\u5206\u96c6\u5408\u548c II \u3092\u89e3\u304f */\nList<List<Integer>> subsetSumII(int[] nums, int target) {\n    List<Integer> state = new ArrayList<>(); // \u72b6\u614b\uff08\u90e8\u5206\u96c6\u5408\uff09\n    Arrays.sort(nums); // nums \u3092\u30bd\u30fc\u30c8\n    int start = 0; // \u8d70\u67fb\u306e\u958b\u59cb\u70b9\n    List<List<Integer>> res = new ArrayList<>(); // \u7d50\u679c\u30ea\u30b9\u30c8\uff08\u90e8\u5206\u96c6\u5408\u30ea\u30b9\u30c8\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_ii.cs
        [class]{subset_sum_ii}-[func]{Backtrack}\n\n[class]{subset_sum_ii}-[func]{SubsetSumII}\n
        subset_sum_ii.go
        [class]{}-[func]{backtrackSubsetSumII}\n\n[class]{}-[func]{subsetSumII}\n
        subset_sum_ii.swift
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumII}\n
        subset_sum_ii.js
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumII}\n
        subset_sum_ii.ts
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumII}\n
        subset_sum_ii.dart
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumII}\n
        subset_sum_ii.rs
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subset_sum_ii}\n
        subset_sum_ii.c
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumII}\n
        subset_sum_ii.kt
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumII}\n
        subset_sum_ii.rb
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subset_sum_ii}\n

        \u4ee5\u4e0b\u306e\u56f3\u306f\u3001\u914d\u5217 \\([4, 4, 5]\\) \u3068\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20 \\(9\\) \u306e\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9\u3092\u793a\u3057\u30014\u7a2e\u985e\u306e\u526a\u5b9a\u64cd\u4f5c\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002\u56f3\u3068\u30b3\u30fc\u30c9\u306e\u30b3\u30e1\u30f3\u30c8\u3092\u7d44\u307f\u5408\u308f\u305b\u3066\u3001\u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u5168\u4f53\u3068\u5404\u7a2e\u985e\u306e\u526a\u5b9a\u64cd\u4f5c\u306e\u52d5\u4f5c\u3092\u7406\u89e3\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u56f3 13-14 \u00a0 \u90e8\u5206\u96c6\u5408\u548c II \u306e\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9

        "},{"location":"chapter_backtracking/summary/","title":"13.5 \u00a0 \u307e\u3068\u3081","text":""},{"location":"chapter_backtracking/summary/#1","title":"1. \u00a0 \u91cd\u8981\u306a\u5fa9\u7fd2","text":"
        • \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u672c\u8cea\u306f\u5168\u6570\u63a2\u7d22\u3067\u3059\u3002\u89e3\u7a7a\u9593\u306e\u6df1\u3055\u512a\u5148\u8d70\u67fb\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u3067\u6761\u4ef6\u3092\u6e80\u305f\u3059\u89e3\u3092\u6c42\u3081\u307e\u3059\u3002\u691c\u7d22\u4e2d\u306b\u6e80\u8db3\u306e\u3044\u304f\u89e3\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001\u305d\u308c\u3092\u8a18\u9332\u3057\u3001\u3059\u3079\u3066\u306e\u89e3\u304c\u898b\u3064\u304b\u308b\u304b\u8d70\u67fb\u304c\u5b8c\u4e86\u3059\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002
        • \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u306b\u306f\u8a66\u884c\u3068\u5f8c\u9000\u304c\u542b\u307e\u308c\u307e\u3059\u3002\u6df1\u3055\u512a\u5148\u63a2\u7d22\u3092\u4f7f\u7528\u3057\u3066\u69d8\u3005\u306a\u9078\u629e\u3092\u63a2\u7d22\u3057\u3001\u9078\u629e\u304c\u5236\u7d04\u3092\u6e80\u305f\u3055\u306a\u3044\u5834\u5408\u3001\u524d\u306e\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u307e\u3059\u3002\u305d\u3057\u3066\u524d\u306e\u72b6\u614b\u306b\u623b\u3063\u3066\u4ed6\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u8a66\u3057\u7d9a\u3051\u307e\u3059\u3002\u8a66\u884c\u3068\u5f8c\u9000\u306f\u53cd\u5bfe\u65b9\u5411\u306e\u64cd\u4f5c\u3067\u3059\u3002
        • \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u554f\u984c\u306b\u306f\u901a\u5e38\u8907\u6570\u306e\u5236\u7d04\u304c\u542b\u307e\u308c\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u5236\u7d04\u306f\u526a\u5b9a\u64cd\u4f5c\u3092\u5b9f\u884c\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\u526a\u5b9a\u306f\u4e0d\u8981\u306a\u691c\u7d22\u5206\u5c90\u3092\u4e8b\u524d\u306b\u7d42\u4e86\u3057\u3001\u691c\u7d22\u52b9\u7387\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
        • \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u4e3b\u306b\u691c\u7d22\u554f\u984c\u3068\u5236\u7d04\u6e80\u8db3\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u7d44\u307f\u5408\u308f\u305b\u6700\u9069\u5316\u554f\u984c\u306f\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u3066\u89e3\u6c7a\u3067\u304d\u307e\u3059\u304c\u3001\u591a\u304f\u306e\u5834\u5408\u3001\u3088\u308a\u52b9\u7387\u7684\u307e\u305f\u306f\u52b9\u679c\u7684\u306a\u89e3\u6c7a\u65b9\u6cd5\u304c\u5229\u7528\u53ef\u80fd\u3067\u3059\u3002
        • \u9806\u5217\u554f\u984c\u306f\u3001\u4e0e\u3048\u3089\u308c\u305f\u96c6\u5408\u306e\u8981\u7d20\u306e\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u9806\u5217\u3092\u691c\u7d22\u3059\u308b\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u307e\u3059\u3002\u5404\u8981\u7d20\u304c\u9078\u629e\u3055\u308c\u305f\u304b\u3069\u3046\u304b\u3092\u8a18\u9332\u3059\u308b\u305f\u3081\u306b\u914d\u5217\u3092\u4f7f\u7528\u3057\u3001\u540c\u3058\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3092\u907f\u3051\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u5404\u8981\u7d20\u304c\u4e00\u5ea6\u3060\u3051\u9078\u629e\u3055\u308c\u308b\u3053\u3068\u304c\u4fdd\u8a3c\u3055\u308c\u307e\u3059\u3002
        • \u9806\u5217\u554f\u984c\u3067\u306f\u3001\u96c6\u5408\u306b\u91cd\u8907\u8981\u7d20\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u6700\u7d42\u7d50\u679c\u306b\u91cd\u8907\u9806\u5217\u304c\u542b\u307e\u308c\u307e\u3059\u3002\u540c\u4e00\u8981\u7d20\u304c\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u4e00\u5ea6\u3060\u3051\u9078\u629e\u3067\u304d\u308b\u3088\u3046\u306b\u5236\u9650\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u3053\u308c\u306f\u901a\u5e38\u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8\u3092\u4f7f\u7528\u3057\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002
        • \u90e8\u5206\u96c6\u5408\u548c\u554f\u984c\u306f\u3001\u4e0e\u3048\u3089\u308c\u305f\u96c6\u5408\u3067\u30bf\u30fc\u30b2\u30c3\u30c8\u5024\u306b\u5408\u8a08\u3059\u308b\u5168\u3066\u306e\u90e8\u5206\u96c6\u5408\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u307e\u3059\u3002\u96c6\u5408\u306f\u8981\u7d20\u306e\u9806\u5e8f\u3092\u533a\u5225\u3057\u307e\u305b\u3093\u304c\u3001\u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u3067\u306f\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u304c\u751f\u6210\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u7570\u306a\u308b\u8981\u7d20\u9806\u5e8f\u3092\u72ec\u7279\u306e\u30d1\u30b9\u3068\u3057\u3066\u63a2\u7d22\u3059\u308b\u305f\u3081\u306b\u767a\u751f\u3057\u307e\u3059\u3002\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306e\u524d\u306b\u3001\u30c7\u30fc\u30bf\u3092\u30bd\u30fc\u30c8\u3057\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u8d70\u67fb\u306e\u958b\u59cb\u70b9\u3092\u793a\u3059\u5909\u6570\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u3092\u751f\u6210\u3059\u308b\u691c\u7d22\u5206\u5c90\u3092\u526a\u5b9a\u3067\u304d\u307e\u3059\u3002
        • \u90e8\u5206\u96c6\u5408\u548c\u554f\u984c\u3067\u306f\u3001\u914d\u5217\u5185\u306e\u7b49\u3057\u3044\u8981\u7d20\u306f\u91cd\u8907\u96c6\u5408\u3092\u751f\u6210\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u914d\u5217\u304c\u3059\u3067\u306b\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u308b\u3068\u3044\u3046\u524d\u63d0\u6761\u4ef6\u3092\u4f7f\u7528\u3057\u3066\u3001\u96a3\u63a5\u3059\u308b\u8981\u7d20\u304c\u7b49\u3057\u3044\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\u3059\u308b\u3053\u3068\u3067\u526a\u5b9a\u3092\u884c\u3044\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u7b49\u3057\u3044\u8981\u7d20\u304c\u30e9\u30a6\u30f3\u30c9\u3054\u3068\u306b\u4e00\u5ea6\u3060\u3051\u9078\u629e\u3055\u308c\u308b\u3053\u3068\u304c\u4fdd\u8a3c\u3055\u308c\u307e\u3059\u3002
        • \\(n\\) \u30af\u30a4\u30fc\u30f3\u554f\u984c\u306f\u30012\u3064\u306e\u30af\u30a4\u30fc\u30f3\u304c\u4e92\u3044\u306b\u653b\u6483\u3067\u304d\u306a\u3044\u3088\u3046\u306b \\(n \\times n\\) \u306e\u30c1\u30a7\u30b9\u30dc\u30fc\u30c9\u306b \\(n\\) \u500b\u306e\u30af\u30a4\u30fc\u30f3\u3092\u914d\u7f6e\u3059\u308b\u65b9\u6848\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u307e\u3059\u3002\u554f\u984c\u306e\u5236\u7d04\u306b\u306f\u884c\u5236\u7d04\u3001\u5217\u5236\u7d04\u3001\u304a\u3088\u3073\u4e3b\u5bfe\u89d2\u7dda\u3068\u526f\u5bfe\u89d2\u7dda\u306e\u5236\u7d04\u304c\u542b\u307e\u308c\u307e\u3059\u3002\u884c\u5236\u7d04\u3092\u6e80\u305f\u3059\u305f\u3081\u306b\u3001\u884c\u3054\u3068\u306b1\u3064\u306e\u30af\u30a4\u30fc\u30f3\u3092\u914d\u7f6e\u3059\u308b\u6226\u7565\u3092\u63a1\u7528\u3057\u3001\u5404\u884c\u306b1\u3064\u306e\u30af\u30a4\u30fc\u30f3\u304c\u914d\u7f6e\u3055\u308c\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3057\u307e\u3059\u3002
        • \u5217\u5236\u7d04\u3068\u5bfe\u89d2\u7dda\u5236\u7d04\u306e\u51e6\u7406\u306f\u4f3c\u3066\u3044\u307e\u3059\u3002\u5217\u5236\u7d04\u306b\u3064\u3044\u3066\u306f\u3001\u5404\u5217\u306b\u30af\u30a4\u30fc\u30f3\u304c\u3042\u308b\u304b\u3069\u3046\u304b\u3092\u8a18\u9332\u3059\u308b\u914d\u5217\u3092\u4f7f\u7528\u3057\u3001\u9078\u629e\u3055\u308c\u305f\u30bb\u30eb\u304c\u5408\u6cd5\u304b\u3069\u3046\u304b\u3092\u793a\u3057\u307e\u3059\u3002\u5bfe\u89d2\u7dda\u5236\u7d04\u306b\u3064\u3044\u3066\u306f\u30012\u3064\u306e\u914d\u5217\u3092\u4f7f\u7528\u3057\u3066\u4e3b\u5bfe\u89d2\u7dda\u3068\u526f\u5bfe\u89d2\u7dda\u306b\u305d\u308c\u305e\u308c\u30af\u30a4\u30fc\u30f3\u306e\u5b58\u5728\u3092\u8a18\u9332\u3057\u307e\u3059\u3002\u8ab2\u984c\u306f\u3001\u540c\u3058\u4e3b\u5bfe\u89d2\u7dda\u307e\u305f\u306f\u526f\u5bfe\u89d2\u7dda\u4e0a\u306e\u30bb\u30eb\u306e\u884c\u3068\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u9593\u306e\u95a2\u4fc2\u3092\u6c7a\u5b9a\u3059\u308b\u3053\u3068\u3067\u3059\u3002
        "},{"location":"chapter_backtracking/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q: \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3068\u518d\u5e30\u306e\u95a2\u4fc2\u3092\u3069\u306e\u3088\u3046\u306b\u7406\u89e3\u3059\u308c\u3070\u3088\u3044\u3067\u3059\u304b\uff1f

        \u5168\u4f53\u7684\u306b\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306f\u300c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u6226\u7565\u300d\u3067\u3042\u308a\u3001\u518d\u5e30\u306f\u3088\u308a\u300c\u30c4\u30fc\u30eb\u300d\u3067\u3059\u3002

        • \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u901a\u5e38\u518d\u5e30\u306b\u57fa\u3065\u3044\u3066\u3044\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306f\u518d\u5e30\u306e\u5fdc\u7528\u30b7\u30ca\u30ea\u30aa\u306e\u4e00\u3064\u3067\u3042\u308a\u3001\u7279\u306b\u691c\u7d22\u554f\u984c\u306b\u304a\u3044\u3066\u3067\u3059\u3002
        • \u518d\u5e30\u306e\u69cb\u9020\u306f\u300c\u90e8\u5206\u554f\u984c\u5206\u89e3\u300d\u306e\u554f\u984c\u89e3\u6c7a\u30d1\u30e9\u30c0\u30a4\u30e0\u3092\u53cd\u6620\u3057\u307e\u3059\u3002\u5206\u5272\u7d71\u6cbb\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\uff08\u30e1\u30e2\u5316\u518d\u5e30\uff09\u3092\u542b\u3080\u554f\u984c\u306e\u89e3\u6c7a\u3067\u3088\u304f\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        "},{"location":"chapter_computational_complexity/","title":"\u7b2c 2 \u7ae0 \u00a0 \u8907\u96d1\u5ea6\u89e3\u6790","text":"

        Abstract

        \u8907\u96d1\u5ea6\u89e3\u6790\u306f\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5e83\u5927\u306a\u5b87\u5b99\u306b\u304a\u3051\u308b\u6642\u7a7a\u306e\u30ca\u30d3\u30b2\u30fc\u30bf\u30fc\u306e\u3088\u3046\u306a\u3082\u306e\u3067\u3059\u3002

        \u6642\u9593\u3068\u7a7a\u9593\u306e\u6b21\u5143\u3092\u3088\u308a\u6df1\u304f\u63a2\u6c42\u3057\u3001\u3088\u308a\u512a\u96c5\u306a\u89e3\u6c7a\u7b56\u3092\u6c42\u3081\u308b\u305f\u3081\u306e\u30ac\u30a4\u30c9\u3068\u306a\u308a\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 2.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u52b9\u7387\u8a55\u4fa1
        • 2.2 \u00a0 \u53cd\u5fa9\u3068\u518d\u5e30
        • 2.3 \u00a0 \u6642\u9593\u8a08\u7b97\u91cf
        • 2.4 \u00a0 \u7a7a\u9593\u8a08\u7b97\u91cf
        • 2.5 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_computational_complexity/iteration_and_recursion/","title":"2.2 \u00a0 \u53cd\u5fa9\u3068\u518d\u5e30","text":"

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u304a\u3044\u3066\u3001\u30bf\u30b9\u30af\u306e\u7e70\u308a\u8fd4\u3057\u5b9f\u884c\u306f\u975e\u5e38\u306b\u4e00\u822c\u7684\u3067\u3042\u308a\u3001\u8907\u96d1\u5ea6\u306e\u5206\u6790\u3068\u5bc6\u63a5\u306b\u95a2\u4fc2\u3057\u3066\u3044\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u6642\u9593\u8a08\u7b97\u91cf\u3068\u7a7a\u9593\u8a08\u7b97\u91cf\u306e\u6982\u5ff5\u3092\u8a73\u3057\u304f\u5b66\u3076\u524d\u306b\u3001\u307e\u305a\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3067\u7e70\u308a\u8fd4\u3057\u30bf\u30b9\u30af\u3092\u5b9f\u88c5\u3059\u308b\u65b9\u6cd5\u3092\u63a2\u7a76\u3057\u307e\u3057\u3087\u3046\u3002\u3053\u308c\u306b\u306f\u30012\u3064\u306e\u57fa\u672c\u7684\u306a\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u5236\u5fa1\u69cb\u9020\u3067\u3042\u308b\u53cd\u5fa9\u3068\u518d\u5e30\u306e\u7406\u89e3\u304c\u542b\u307e\u308c\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#221","title":"2.2.1 \u00a0 \u53cd\u5fa9","text":"

        \u53cd\u5fa9\u306f\u3001\u30bf\u30b9\u30af\u3092\u7e70\u308a\u8fd4\u3057\u5b9f\u884c\u3059\u308b\u305f\u3081\u306e\u5236\u5fa1\u69cb\u9020\u3067\u3059\u3002\u53cd\u5fa9\u3067\u306f\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u7279\u5b9a\u306e\u6761\u4ef6\u304c\u6e80\u305f\u3055\u308c\u3066\u3044\u308b\u9650\u308a\u30b3\u30fc\u30c9\u30d6\u30ed\u30c3\u30af\u3092\u7e70\u308a\u8fd4\u3057\u5b9f\u884c\u3057\u3001\u3053\u306e\u6761\u4ef6\u304c\u6e80\u305f\u3055\u308c\u306a\u304f\u306a\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#1-for","title":"1. \u00a0 for\u30eb\u30fc\u30d7","text":"

        for\u30eb\u30fc\u30d7\u306f\u53cd\u5fa9\u306e\u6700\u3082\u4e00\u822c\u7684\u306a\u5f62\u5f0f\u306e1\u3064\u3067\u3042\u308a\u3001\u53cd\u5fa9\u56de\u6570\u304c\u4e8b\u524d\u306b\u5206\u304b\u3063\u3066\u3044\u308b\u5834\u5408\u306b\u7279\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u95a2\u6570\u306ffor\u30eb\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u3066\\(1 + 2 + \\dots + n\\)\u306e\u5408\u8a08\u3092\u5b9f\u884c\u3057\u3001\u5408\u8a08\u3092\u5909\u6570res\u306b\u683c\u7d0d\u3057\u307e\u3059\u3002Python\u3067\u306f\u3001range(a, b)\u306fa\u3092\u542b\u307fb\u3092\u9664\u304f\u533a\u9593\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3064\u307e\u308a\u3001\\(a\\)\u304b\u3089\\(b\u22121\\)\u307e\u3067\u306e\u7bc4\u56f2\u3067\u53cd\u5fa9\u3057\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
        def for_loop(n: int) -> int:\n    \"\"\"for\u30eb\u30fc\u30d7\"\"\"\n    res = 0\n    # 1, 2, ..., n-1, n \u306e\u5408\u8a08\u3092\u30eb\u30fc\u30d7\n    for i in range(1, n + 1):\n        res += i\n    return res\n
        iteration.cpp
        /* for \u30eb\u30fc\u30d7 */\nint forLoop(int n) {\n    int res = 0;\n    // 1, 2, ..., n-1, n \u306e\u5408\u8a08\u3092\u30eb\u30fc\u30d7\u8a08\u7b97\n    for (int i = 1; i <= n; ++i) {\n        res += i;\n    }\n    return res;\n}\n
        iteration.java
        /* for \u30eb\u30fc\u30d7 */\nint forLoop(int n) {\n    int res = 0;\n    // 1, 2, ..., n-1, n \u306e\u5408\u8a08\u3092\u30eb\u30fc\u30d7\u8a08\u7b97\n    for (int i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
        iteration.cs
        [class]{iteration}-[func]{ForLoop}\n
        iteration.go
        [class]{}-[func]{forLoop}\n
        iteration.swift
        [class]{}-[func]{forLoop}\n
        iteration.js
        [class]{}-[func]{forLoop}\n
        iteration.ts
        [class]{}-[func]{forLoop}\n
        iteration.dart
        [class]{}-[func]{forLoop}\n
        iteration.rs
        [class]{}-[func]{for_loop}\n
        iteration.c
        [class]{}-[func]{forLoop}\n
        iteration.kt
        [class]{}-[func]{forLoop}\n
        iteration.rb
        [class]{}-[func]{for_loop}\n

        \u4ee5\u4e0b\u306e\u56f3\u306f\u3053\u306e\u5408\u8a08\u95a2\u6570\u3092\u8868\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 2-1 \u00a0 Flowchart of the sum function

        \u3053\u306e\u5408\u8a08\u95a2\u6570\u3067\u306e\u64cd\u4f5c\u6570\u306f\u5165\u529b\u30c7\u30fc\u30bf\u306e\u30b5\u30a4\u30ba\\(n\\)\u306b\u6bd4\u4f8b\u3059\u308b\u3001\u3064\u307e\u308a\u7dda\u5f62\u95a2\u4fc2\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u300c\u7dda\u5f62\u95a2\u4fc2\u300d\u3053\u305d\u304c\u6642\u9593\u8a08\u7b97\u91cf\u304c\u8a18\u8ff0\u3059\u308b\u3082\u306e\u3067\u3059\u3002\u3053\u306e\u30c8\u30d4\u30c3\u30af\u306b\u3064\u3044\u3066\u306f\u6b21\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u8a73\u3057\u304f\u8aac\u660e\u3057\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#2-while","title":"2. \u00a0 while\u30eb\u30fc\u30d7","text":"

        for\u30eb\u30fc\u30d7\u3068\u540c\u69d8\u306b\u3001while\u30eb\u30fc\u30d7\u306f\u53cd\u5fa9\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306e\u3082\u30461\u3064\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u3067\u3059\u3002while\u30eb\u30fc\u30d7\u3067\u306f\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u5404\u53cd\u5fa9\u306e\u958b\u59cb\u6642\u306b\u6761\u4ef6\u3092\u30c1\u30a7\u30c3\u30af\u3057\u3001\u6761\u4ef6\u304c\u771f\u306e\u5834\u5408\u306f\u5b9f\u884c\u3092\u7d99\u7d9a\u3057\u3001\u305d\u3046\u3067\u306a\u3051\u308c\u3070\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\u3057\u307e\u3059\u3002

        \u4ee5\u4e0b\u3067\u306fwhile\u30eb\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u3066\u5408\u8a08\\(1 + 2 + \\dots + n\\)\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
        def while_loop(n: int) -> int:\n    \"\"\"while\u30eb\u30fc\u30d7\"\"\"\n    res = 0\n    i = 1  # \u6761\u4ef6\u5909\u6570\u3092\u521d\u671f\u5316\n    # 1, 2, ..., n-1, n \u306e\u5408\u8a08\u3092\u30eb\u30fc\u30d7\n    while i <= n:\n        res += i\n        i += 1  # \u6761\u4ef6\u5909\u6570\u3092\u66f4\u65b0\n    return res\n
        iteration.cpp
        /* while \u30eb\u30fc\u30d7 */\nint whileLoop(int n) {\n    int res = 0;\n    int i = 1; // \u6761\u4ef6\u5909\u6570\u3092\u521d\u671f\u5316\n    // 1, 2, ..., n-1, n \u306e\u5408\u8a08\u3092\u30eb\u30fc\u30d7\u8a08\u7b97\n    while (i <= n) {\n        res += i;\n        i++; // \u6761\u4ef6\u5909\u6570\u3092\u66f4\u65b0\n    }\n    return res;\n}\n
        iteration.java
        /* while \u30eb\u30fc\u30d7 */\nint whileLoop(int n) {\n    int res = 0;\n    int i = 1; // \u6761\u4ef6\u5909\u6570\u3092\u521d\u671f\u5316\n    // 1, 2, ..., n-1, n \u306e\u5408\u8a08\u3092\u30eb\u30fc\u30d7\u8a08\u7b97\n    while (i <= n) {\n        res += i;\n        i++; // \u6761\u4ef6\u5909\u6570\u3092\u66f4\u65b0\n    }\n    return res;\n}\n
        iteration.cs
        [class]{iteration}-[func]{WhileLoop}\n
        iteration.go
        [class]{}-[func]{whileLoop}\n
        iteration.swift
        [class]{}-[func]{whileLoop}\n
        iteration.js
        [class]{}-[func]{whileLoop}\n
        iteration.ts
        [class]{}-[func]{whileLoop}\n
        iteration.dart
        [class]{}-[func]{whileLoop}\n
        iteration.rs
        [class]{}-[func]{while_loop}\n
        iteration.c
        [class]{}-[func]{whileLoop}\n
        iteration.kt
        [class]{}-[func]{whileLoop}\n
        iteration.rb
        [class]{}-[func]{while_loop}\n

        while\u30eb\u30fc\u30d7\u306ffor\u30eb\u30fc\u30d7\u3088\u308a\u3082\u67d4\u8edf\u6027\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002\u7279\u306b\u3001\u6761\u4ef6\u5909\u6570\u306e\u30ab\u30b9\u30bf\u30e0\u521d\u671f\u5316\u3068\u5404\u30b9\u30c6\u30c3\u30d7\u3067\u306e\u5909\u66f4\u304c\u53ef\u80fd\u3067\u3059\u3002

        \u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3001\u6761\u4ef6\u5909\u6570\\(i\\)\u304c\u5404\u30e9\u30a6\u30f3\u30c9\u30672\u56de\u66f4\u65b0\u3055\u308c\u307e\u3059\u304c\u3001\u3053\u308c\u306ffor\u30eb\u30fc\u30d7\u3067\u306f\u5b9f\u88c5\u304c\u4e0d\u4fbf\u3067\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
        def while_loop_ii(n: int) -> int:\n    \"\"\"while\u30eb\u30fc\u30d7\uff082\u3064\u306e\u66f4\u65b0\uff09\"\"\"\n    res = 0\n    i = 1  # \u6761\u4ef6\u5909\u6570\u3092\u521d\u671f\u5316\n    # 1, 4, 10, ... \u306e\u5408\u8a08\u3092\u30eb\u30fc\u30d7\n    while i <= n:\n        res += i\n        # \u6761\u4ef6\u5909\u6570\u3092\u66f4\u65b0\n        i += 1\n        i *= 2\n    return res\n
        iteration.cpp
        /* while \u30eb\u30fc\u30d7\uff082\u3064\u306e\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n    int res = 0;\n    int i = 1; // \u6761\u4ef6\u5909\u6570\u3092\u521d\u671f\u5316\n    // 1, 4, 10, ... \u306e\u5408\u8a08\u3092\u30eb\u30fc\u30d7\u8a08\u7b97\n    while (i <= n) {\n        res += i;\n        // \u6761\u4ef6\u5909\u6570\u3092\u66f4\u65b0\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
        iteration.java
        /* while \u30eb\u30fc\u30d7\uff082\u3064\u306e\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n    int res = 0;\n    int i = 1; // \u6761\u4ef6\u5909\u6570\u3092\u521d\u671f\u5316\n    // 1, 4, 10, ... \u306e\u5408\u8a08\u3092\u30eb\u30fc\u30d7\u8a08\u7b97\n    while (i <= n) {\n        res += i;\n        // \u6761\u4ef6\u5909\u6570\u3092\u66f4\u65b0\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
        iteration.cs
        [class]{iteration}-[func]{WhileLoopII}\n
        iteration.go
        [class]{}-[func]{whileLoopII}\n
        iteration.swift
        [class]{}-[func]{whileLoopII}\n
        iteration.js
        [class]{}-[func]{whileLoopII}\n
        iteration.ts
        [class]{}-[func]{whileLoopII}\n
        iteration.dart
        [class]{}-[func]{whileLoopII}\n
        iteration.rs
        [class]{}-[func]{while_loop_ii}\n
        iteration.c
        [class]{}-[func]{whileLoopII}\n
        iteration.kt
        [class]{}-[func]{whileLoopII}\n
        iteration.rb
        [class]{}-[func]{while_loop_ii}\n

        \u5168\u4f53\u7684\u306b\u3001for\u30eb\u30fc\u30d7\u306f\u3088\u308a\u7c21\u6f54\u3067\u3001while\u30eb\u30fc\u30d7\u306f\u3088\u308a\u67d4\u8edf\u3067\u3059\u3002\u3069\u3061\u3089\u3082\u53cd\u5fa9\u69cb\u9020\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002\u3069\u3061\u3089\u3092\u4f7f\u7528\u3059\u308b\u304b\u306f\u3001\u554f\u984c\u306e\u5177\u4f53\u7684\u306a\u8981\u4ef6\u306b\u57fa\u3065\u3044\u3066\u6c7a\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#3","title":"3. \u00a0 \u30cd\u30b9\u30c8\u3057\u305f\u30eb\u30fc\u30d7","text":"

        1\u3064\u306e\u30eb\u30fc\u30d7\u69cb\u9020\u3092\u5225\u306e\u30eb\u30fc\u30d7\u69cb\u9020\u5185\u306b\u30cd\u30b9\u30c8\u3067\u304d\u307e\u3059\u3002\u4ee5\u4e0b\u306ffor\u30eb\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u305f\u4f8b\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
        def nested_for_loop(n: int) -> str:\n    \"\"\"\u4e8c\u91cdfor\u30eb\u30fc\u30d7\"\"\"\n    res = \"\"\n    # i = 1, 2, ..., n-1, n \u3092\u30eb\u30fc\u30d7\n    for i in range(1, n + 1):\n        # j = 1, 2, ..., n-1, n \u3092\u30eb\u30fc\u30d7\n        for j in range(1, n + 1):\n            res += f\"({i}, {j}), \"\n    return res\n
        iteration.cpp
        /* 2\u91cd for \u30eb\u30fc\u30d7 */\nstring nestedForLoop(int n) {\n    ostringstream res;\n    // \u30eb\u30fc\u30d7 i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; ++i) {\n        // \u30eb\u30fc\u30d7 j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; ++j) {\n            res << \"(\" << i << \", \" << j << \"), \";\n        }\n    }\n    return res.str();\n}\n
        iteration.java
        /* 2\u91cd for \u30eb\u30fc\u30d7 */\nString nestedForLoop(int n) {\n    StringBuilder res = new StringBuilder();\n    // \u30eb\u30fc\u30d7 i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        // \u30eb\u30fc\u30d7 j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; j++) {\n            res.append(\"(\" + i + \", \" + j + \"), \");\n        }\n    }\n    return res.toString();\n}\n
        iteration.cs
        [class]{iteration}-[func]{NestedForLoop}\n
        iteration.go
        [class]{}-[func]{nestedForLoop}\n
        iteration.swift
        [class]{}-[func]{nestedForLoop}\n
        iteration.js
        [class]{}-[func]{nestedForLoop}\n
        iteration.ts
        [class]{}-[func]{nestedForLoop}\n
        iteration.dart
        [class]{}-[func]{nestedForLoop}\n
        iteration.rs
        [class]{}-[func]{nested_for_loop}\n
        iteration.c
        [class]{}-[func]{nestedForLoop}\n
        iteration.kt
        [class]{}-[func]{nestedForLoop}\n
        iteration.rb
        [class]{}-[func]{nested_for_loop}\n

        \u4ee5\u4e0b\u306e\u56f3\u306f\u3053\u306e\u30cd\u30b9\u30c8\u3057\u305f\u30eb\u30fc\u30d7\u3092\u8868\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 2-2 \u00a0 Flowchart of the nested loop

        \u3053\u306e\u3088\u3046\u306a\u5834\u5408\u3001\u95a2\u6570\u306e\u64cd\u4f5c\u6570\u306f\\(n^2\\)\u306b\u6bd4\u4f8b\u3057\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u6642\u9593\u3068\u5165\u529b\u30c7\u30fc\u30bf\u306e\u30b5\u30a4\u30ba\\(n\\)\u306b\u306f\u300c\u4e8c\u6b21\u95a2\u4fc2\u300d\u304c\u3042\u308a\u307e\u3059\u3002

        \u3055\u3089\u306b\u30cd\u30b9\u30c8\u3057\u305f\u30eb\u30fc\u30d7\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3067\u8907\u96d1\u5ea6\u3092\u9ad8\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u5404\u30ec\u30d9\u30eb\u306e\u30cd\u30b9\u30c8\u306f\u4e8b\u5b9f\u4e0a\u300c\u6b21\u5143\u3092\u5897\u52a0\u300d\u3055\u305b\u3001\u6642\u9593\u8a08\u7b97\u91cf\u3092\u300c\u4e09\u6b21\u300d\u3001\u300c\u56db\u6b21\u300d\u306a\u3069\u306b\u5f15\u304d\u4e0a\u3052\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#222","title":"2.2.2 \u00a0 \u518d\u5e30","text":"

        \u518d\u5e30\u306f\u3001\u95a2\u6570\u304c\u81ea\u5206\u81ea\u8eab\u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u3067\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u6226\u7565\u3067\u3059\u3002\u4e3b\u306b2\u3064\u306e\u30d5\u30a7\u30fc\u30ba\u304c\u542b\u307e\u308c\u307e\u3059\uff1a

        1. \u547c\u3073\u51fa\u3057: \u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u81ea\u5206\u81ea\u8eab\u3092\u7e70\u308a\u8fd4\u3057\u547c\u3073\u51fa\u3057\u3001\u3057\u3070\u3057\u3070\u3088\u308a\u5c0f\u3055\u3044\u307e\u305f\u306f\u3088\u308a\u5358\u7d14\u306a\u5f15\u6570\u3067\u3001\u300c\u7d42\u4e86\u6761\u4ef6\u300d\u306b\u5411\u304b\u3063\u3066\u9032\u307f\u307e\u3059\u3002
        2. \u8fd4\u5374: \u300c\u7d42\u4e86\u6761\u4ef6\u300d\u304c\u30c8\u30ea\u30ac\u30fc\u3055\u308c\u308b\u3068\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u6700\u3082\u6df1\u3044\u518d\u5e30\u95a2\u6570\u304b\u3089\u8fd4\u308a\u59cb\u3081\u3001\u5404\u30ec\u30a4\u30e4\u30fc\u306e\u7d50\u679c\u3092\u96c6\u7d04\u3057\u307e\u3059\u3002

        \u5b9f\u88c5\u306e\u89b3\u70b9\u304b\u3089\u3001\u518d\u5e30\u30b3\u30fc\u30c9\u306f\u4e3b\u306b3\u3064\u306e\u8981\u7d20\u3092\u542b\u307f\u307e\u3059\u3002

        1. \u7d42\u4e86\u6761\u4ef6: \u300c\u547c\u3073\u51fa\u3057\u300d\u304b\u3089\u300c\u8fd4\u5374\u300d\u306b\u3044\u3064\u5207\u308a\u66ff\u3048\u308b\u304b\u3092\u6c7a\u5b9a\u3057\u307e\u3059\u3002
        2. \u518d\u5e30\u547c\u3073\u51fa\u3057: \u300c\u547c\u3073\u51fa\u3057\u300d\u306b\u5bfe\u5fdc\u3057\u3001\u95a2\u6570\u304c\u81ea\u5206\u81ea\u8eab\u3092\u547c\u3073\u51fa\u3057\u3001\u901a\u5e38\u306f\u3088\u308a\u5c0f\u3055\u3044\u307e\u305f\u306f\u3088\u308a\u5358\u7d14\u5316\u3055\u308c\u305f\u30d1\u30e9\u30e1\u30fc\u30bf\u3067\u884c\u3044\u307e\u3059\u3002
        3. \u7d50\u679c\u306e\u8fd4\u5374: \u300c\u8fd4\u5374\u300d\u306b\u5bfe\u5fdc\u3057\u3001\u73fe\u5728\u306e\u518d\u5e30\u30ec\u30d9\u30eb\u306e\u7d50\u679c\u304c\u524d\u306e\u30ec\u30a4\u30e4\u30fc\u306b\u8fd4\u3055\u308c\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u89b3\u5bdf\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u5358\u7d14\u306b\u95a2\u6570recur(n)\u3092\u547c\u3073\u51fa\u3059\u3060\u3051\u3067\\(1 + 2 + \\dots + n\\)\u306e\u5408\u8a08\u3092\u8a08\u7b97\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
        def recur(n: int) -> int:\n    \"\"\"\u518d\u5e30\"\"\"\n    # \u7d42\u4e86\u6761\u4ef6\n    if n == 1:\n        return 1\n    # \u518d\u5e30\uff1a\u518d\u5e30\u547c\u3073\u51fa\u3057\n    res = recur(n - 1)\n    # \u5fa9\u5e30\uff1a\u7d50\u679c\u3092\u8fd4\u3059\n    return n + res\n
        recursion.cpp
        /* \u518d\u5e30 */\nint recur(int n) {\n    // \u7d42\u4e86\u6761\u4ef6\n    if (n == 1)\n        return 1;\n    // \u518d\u5e30\uff1a\u518d\u5e30\u547c\u3073\u51fa\u3057\n    int res = recur(n - 1);\n    // \u623b\u308a\u5024\uff1a\u7d50\u679c\u3092\u8fd4\u3059\n    return n + res;\n}\n
        recursion.java
        /* \u518d\u5e30 */\nint recur(int n) {\n    // \u7d42\u4e86\u6761\u4ef6\n    if (n == 1)\n        return 1;\n    // \u518d\u5e30\uff1a\u518d\u5e30\u547c\u3073\u51fa\u3057\n    int res = recur(n - 1);\n    // \u623b\u308a\u5024\uff1a\u7d50\u679c\u3092\u8fd4\u3059\n    return n + res;\n}\n
        recursion.cs
        [class]{recursion}-[func]{Recur}\n
        recursion.go
        [class]{}-[func]{recur}\n
        recursion.swift
        [class]{}-[func]{recur}\n
        recursion.js
        [class]{}-[func]{recur}\n
        recursion.ts
        [class]{}-[func]{recur}\n
        recursion.dart
        [class]{}-[func]{recur}\n
        recursion.rs
        [class]{}-[func]{recur}\n
        recursion.c
        [class]{}-[func]{recur}\n
        recursion.kt
        [class]{}-[func]{recur}\n
        recursion.rb
        [class]{}-[func]{recur}\n

        \u4ee5\u4e0b\u306e\u56f3\u306f\u3053\u306e\u95a2\u6570\u306e\u518d\u5e30\u30d7\u30ed\u30bb\u30b9\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 2-3 \u00a0 Recursive process of the sum function

        \u53cd\u5fa9\u3068\u518d\u5e30\u306f\u8a08\u7b97\u306e\u89b3\u70b9\u304b\u3089\u540c\u3058\u7d50\u679c\u3092\u9054\u6210\u3067\u304d\u307e\u3059\u304c\u3001\u305d\u308c\u3089\u306f\u601d\u8003\u3068\u554f\u984c\u89e3\u6c7a\u306e\u5168\u304f\u7570\u306a\u308b\u30d1\u30e9\u30c0\u30a4\u30e0\u3092\u8868\u3057\u307e\u3059\u3002

        • \u53cd\u5fa9: \u300c\u30dc\u30c8\u30e0\u30a2\u30c3\u30d7\u300d\u3067\u554f\u984c\u3092\u89e3\u6c7a\u3057\u307e\u3059\u3002\u6700\u3082\u57fa\u672c\u7684\u306a\u30b9\u30c6\u30c3\u30d7\u304b\u3089\u59cb\u307e\u308a\u3001\u30bf\u30b9\u30af\u304c\u5b8c\u4e86\u3059\u308b\u307e\u3067\u3053\u308c\u3089\u306e\u30b9\u30c6\u30c3\u30d7\u3092\u7e70\u308a\u8fd4\u3057\u8ffd\u52a0\u307e\u305f\u306f\u7d2f\u7a4d\u3057\u307e\u3059\u3002
        • \u518d\u5e30: \u300c\u30c8\u30c3\u30d7\u30c0\u30a6\u30f3\u300d\u3067\u554f\u984c\u3092\u89e3\u6c7a\u3057\u307e\u3059\u3002\u5143\u306e\u554f\u984c\u3092\u3088\u308a\u5c0f\u3055\u306a\u30b5\u30d6\u554f\u984c\u306b\u5206\u89e3\u3057\u3001\u5404\u30b5\u30d6\u554f\u984c\u306f\u5143\u306e\u554f\u984c\u3068\u540c\u3058\u5f62\u5f0f\u3092\u6301\u3061\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u30b5\u30d6\u554f\u984c\u306f\u3001\u89e3\u304c\u5206\u304b\u3063\u3066\u3044\u308b\u30d9\u30fc\u30b9\u30b1\u30fc\u30b9\u3067\u505c\u6b62\u3059\u308b\u307e\u3067\u3001\u3055\u3089\u306b\u5c0f\u3055\u306a\u30b5\u30d6\u554f\u984c\u306b\u5206\u89e3\u3055\u308c\u307e\u3059\u3002

        \u5148\u307b\u3069\u306e\u5408\u8a08\u95a2\u6570\u306e\u4f8b\u3092\u53d6\u3063\u3066\u307f\u307e\u3057\u3087\u3046\u3002\\(f(n) = 1 + 2 + \\dots + n\\)\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u307e\u3059\u3002

        • \u53cd\u5fa9: \u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u3067\u306f\u3001\u30eb\u30fc\u30d7\u5185\u3067\u5408\u8a08\u30d7\u30ed\u30bb\u30b9\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\u3057\u307e\u3059\u3002\\(1\\)\u304b\u3089\u59cb\u307e\u308a\\(n\\)\u307e\u3067\u6a2a\u65ad\u3057\u3001\u5404\u53cd\u5fa9\u3067\u5408\u8a08\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3066\u6700\u7d42\u7684\u306b\\(f(n)\\)\u3092\u8a08\u7b97\u3057\u307e\u3059\u3002
        • \u518d\u5e30: \u3053\u3053\u3067\u306f\u3001\u554f\u984c\u306f\u30b5\u30d6\u554f\u984c\u306b\u5206\u89e3\u3055\u308c\u307e\u3059\uff1a\\(f(n) = n + f(n-1)\\)\u3002\u3053\u306e\u5206\u89e3\u306f\u3001\u30d9\u30fc\u30b9\u30b1\u30fc\u30b9\u306e\\(f(1) = 1\\)\u306b\u5230\u9054\u3059\u308b\u307e\u3067\u518d\u5e30\u7684\u306b\u7d9a\u304d\u3001\u305d\u3053\u3067\u518d\u5e30\u304c\u7d42\u4e86\u3057\u307e\u3059\u3002
        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#1","title":"1. \u00a0 \u547c\u3073\u51fa\u3057\u30b9\u30bf\u30c3\u30af","text":"

        \u518d\u5e30\u95a2\u6570\u304c\u81ea\u5206\u81ea\u8eab\u3092\u547c\u3073\u51fa\u3059\u305f\u3073\u306b\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u65b0\u3057\u304f\u958b\u59cb\u3055\u308c\u305f\u95a2\u6570\u306b\u30e1\u30e2\u30ea\u3092\u5272\u308a\u5f53\u3066\u3066\u3001\u30ed\u30fc\u30ab\u30eb\u5909\u6570\u3001\u623b\u308a\u30a2\u30c9\u30ec\u30b9\u3001\u305d\u306e\u4ed6\u306e\u95a2\u9023\u60c5\u5831\u3092\u683c\u7d0d\u3057\u307e\u3059\u3002\u3053\u308c\u306f2\u3064\u306e\u4e3b\u8981\u306a\u7d50\u679c\u3092\u3082\u305f\u3089\u3057\u307e\u3059\u3002

        • \u95a2\u6570\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30c7\u30fc\u30bf\u306f\u300c\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u300d\u3068\u547c\u3070\u308c\u308b\u30e1\u30e2\u30ea\u9818\u57df\u306b\u683c\u7d0d\u3055\u308c\u3001\u95a2\u6570\u304c\u8fd4\u3055\u308c\u305f\u5f8c\u306b\u306e\u307f\u89e3\u653e\u3055\u308c\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u518d\u5e30\u306f\u4e00\u822c\u7684\u306b\u53cd\u5fa9\u3088\u308a\u3082\u591a\u304f\u306e\u30e1\u30e2\u30ea\u7a7a\u9593\u3092\u6d88\u8cbb\u3057\u307e\u3059\u3002
        • \u518d\u5e30\u547c\u3073\u51fa\u3057\u306f\u8ffd\u52a0\u306e\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u3092\u5c0e\u5165\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u518d\u5e30\u306f\u901a\u5e38\u30eb\u30fc\u30d7\u3088\u308a\u3082\u6642\u9593\u52b9\u7387\u304c\u52a3\u308a\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u3001\u7d42\u4e86\u6761\u4ef6\u304c\u30c8\u30ea\u30ac\u30fc\u3055\u308c\u308b\u524d\u306b\\(n\\)\u500b\u306e\u672a\u8fd4\u5374\u306e\u518d\u5e30\u95a2\u6570\u304c\u3042\u308a\u3001\u518d\u5e30\u306e\u6df1\u3055\u304c\\(n\\)\u3067\u3042\u308b\u3053\u3068\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 2-4 \u00a0 Recursion call depth

        \u5b9f\u969b\u306b\u306f\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u8a31\u53ef\u3055\u308c\u308b\u518d\u5e30\u306e\u6df1\u3055\u306f\u901a\u5e38\u5236\u9650\u3055\u308c\u3066\u304a\u308a\u3001\u904e\u5ea6\u306b\u6df1\u3044\u518d\u5e30\u306f\u30b9\u30bf\u30c3\u30af\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u30a8\u30e9\u30fc\u3092\u5f15\u304d\u8d77\u3053\u3059\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#2","title":"2. \u00a0 \u672b\u5c3e\u518d\u5e30","text":"

        \u8208\u5473\u6df1\u3044\u3053\u3068\u306b\u3001\u95a2\u6570\u304c\u8fd4\u3059\u76f4\u524d\u306e\u6700\u5f8c\u306e\u30b9\u30c6\u30c3\u30d7\u3068\u3057\u3066\u518d\u5e30\u547c\u3073\u51fa\u3057\u3092\u5b9f\u884c\u3059\u308b\u5834\u5408\u3001\u30b3\u30f3\u30d1\u30a4\u30e9\u307e\u305f\u306f\u30a4\u30f3\u30bf\u30fc\u30d7\u30ea\u30bf\u30fc\u306b\u3088\u3063\u3066\u53cd\u5fa9\u3068\u540c\u3058\u7a7a\u9593\u52b9\u7387\u306b\u306a\u308b\u3088\u3046\u306b\u6700\u9069\u5316\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u30b7\u30ca\u30ea\u30aa\u306f\u672b\u5c3e\u518d\u5e30\u3068\u3057\u3066\u77e5\u3089\u308c\u3066\u3044\u307e\u3059\u3002

        • \u901a\u5e38\u306e\u518d\u5e30: \u6a19\u6e96\u7684\u306a\u518d\u5e30\u3067\u306f\u3001\u95a2\u6570\u304c\u524d\u306e\u30ec\u30d9\u30eb\u306b\u623b\u3063\u305f\u3068\u304d\u3001\u3055\u3089\u306b\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3057\u7d9a\u3051\u308b\u305f\u3081\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u524d\u306e\u547c\u3073\u51fa\u3057\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u4fdd\u5b58\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u672b\u5c3e\u518d\u5e30: \u3053\u3053\u3067\u306f\u3001\u518d\u5e30\u547c\u3073\u51fa\u3057\u306f\u95a2\u6570\u304c\u8fd4\u3059\u524d\u306e\u6700\u7d42\u64cd\u4f5c\u3067\u3059\u3002\u3053\u308c\u306f\u3001\u524d\u306e\u30ec\u30d9\u30eb\u306b\u623b\u3063\u305f\u969b\u306b\u3001\u3055\u3089\u306a\u308b\u30a2\u30af\u30b7\u30e7\u30f3\u304c\u5fc5\u8981\u306a\u3044\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u305f\u3081\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u524d\u306e\u30ec\u30d9\u30eb\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u4fdd\u5b58\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u305b\u3093\u3002

        \u4f8b\u3048\u3070\u3001\\(1 + 2 + \\dots + n\\)\u306e\u8a08\u7b97\u3067\u306f\u3001\u7d50\u679c\u5909\u6570res\u3092\u95a2\u6570\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u306b\u3059\u308b\u3053\u3068\u3067\u3001\u672b\u5c3e\u518d\u5e30\u3092\u5b9f\u73fe\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
        def tail_recur(n, res):\n    \"\"\"\u672b\u5c3e\u518d\u5e30\"\"\"\n    # \u7d42\u4e86\u6761\u4ef6\n    if n == 0:\n        return res\n    # \u672b\u5c3e\u518d\u5e30\u547c\u3073\u51fa\u3057\n    return tail_recur(n - 1, res + n)\n
        recursion.cpp
        /* \u672b\u5c3e\u518d\u5e30 */\nint tailRecur(int n, int res) {\n    // \u7d42\u4e86\u6761\u4ef6\n    if (n == 0)\n        return res;\n    // \u672b\u5c3e\u518d\u5e30\u547c\u3073\u51fa\u3057\n    return tailRecur(n - 1, res + n);\n}\n
        recursion.java
        /* \u672b\u5c3e\u518d\u5e30 */\nint tailRecur(int n, int res) {\n    // \u7d42\u4e86\u6761\u4ef6\n    if (n == 0)\n        return res;\n    // \u672b\u5c3e\u518d\u5e30\u547c\u3073\u51fa\u3057\n    return tailRecur(n - 1, res + n);\n}\n
        recursion.cs
        [class]{recursion}-[func]{TailRecur}\n
        recursion.go
        [class]{}-[func]{tailRecur}\n
        recursion.swift
        [class]{}-[func]{tailRecur}\n
        recursion.js
        [class]{}-[func]{tailRecur}\n
        recursion.ts
        [class]{}-[func]{tailRecur}\n
        recursion.dart
        [class]{}-[func]{tailRecur}\n
        recursion.rs
        [class]{}-[func]{tail_recur}\n
        recursion.c
        [class]{}-[func]{tailRecur}\n
        recursion.kt
        [class]{}-[func]{tailRecur}\n
        recursion.rb
        [class]{}-[func]{tail_recur}\n

        \u672b\u5c3e\u518d\u5e30\u306e\u5b9f\u884c\u30d7\u30ed\u30bb\u30b9\u306f\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u901a\u5e38\u306e\u518d\u5e30\u3068\u672b\u5c3e\u518d\u5e30\u3092\u6bd4\u8f03\u3059\u308b\u3068\u3001\u5408\u8a08\u64cd\u4f5c\u306e\u30dd\u30a4\u30f3\u30c8\u304c\u7570\u306a\u308a\u307e\u3059\u3002

        • \u901a\u5e38\u306e\u518d\u5e30: \u5408\u8a08\u64cd\u4f5c\u306f\u300c\u8fd4\u5374\u300d\u30d5\u30a7\u30fc\u30ba\u3067\u767a\u751f\u3057\u3001\u5404\u30ec\u30a4\u30e4\u30fc\u304c\u8fd4\u3063\u305f\u5f8c\u306b\u3082\u3046\u4e00\u5ea6\u5408\u8a08\u304c\u5fc5\u8981\u3067\u3059\u3002
        • \u672b\u5c3e\u518d\u5e30: \u5408\u8a08\u64cd\u4f5c\u306f\u300c\u547c\u3073\u51fa\u3057\u300d\u30d5\u30a7\u30fc\u30ba\u3067\u767a\u751f\u3057\u3001\u300c\u8fd4\u5374\u300d\u30d5\u30a7\u30fc\u30ba\u306f\u5404\u30ec\u30a4\u30e4\u30fc\u3092\u901a\u3058\u3066\u8fd4\u3059\u3060\u3051\u3067\u3059\u3002

        \u56f3 2-5 \u00a0 Tail recursion process

        Tip

        \u591a\u304f\u306e\u30b3\u30f3\u30d1\u30a4\u30e9\u3084\u30a4\u30f3\u30bf\u30fc\u30d7\u30ea\u30bf\u30fc\u306f\u672b\u5c3e\u518d\u5e30\u6700\u9069\u5316\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u306a\u3044\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4f8b\u3048\u3070\u3001Python\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u672b\u5c3e\u518d\u5e30\u6700\u9069\u5316\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u306a\u3044\u305f\u3081\u3001\u95a2\u6570\u304c\u672b\u5c3e\u518d\u5e30\u306e\u5f62\u5f0f\u3067\u3042\u3063\u3066\u3082\u3001\u30b9\u30bf\u30c3\u30af\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u306e\u554f\u984c\u306b\u906d\u9047\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#3_1","title":"3. \u00a0 \u518d\u5e30\u6728","text":"

        \u300c\u5206\u5272\u7d71\u6cbb\u300d\u306b\u95a2\u9023\u3059\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u6271\u3046\u969b\u3001\u518d\u5e30\u306f\u53cd\u5fa9\u3088\u308a\u3082\u3057\u3070\u3057\u3070\u3088\u308a\u76f4\u611f\u7684\u306a\u30a2\u30d7\u30ed\u30fc\u30c1\u3068\u3088\u308a\u8aad\u307f\u3084\u3059\u3044\u30b3\u30fc\u30c9\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002\u300c\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u300d\u3092\u4f8b\u306b\u53d6\u3063\u3066\u307f\u307e\u3057\u3087\u3046\u3002

        Question

        \u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\\(0, 1, 1, 2, 3, 5, 8, 13, \\dots\\)\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u6570\u5217\u306e\\(n\\)\u756a\u76ee\u306e\u6570\u3092\u6c42\u3081\u306a\u3055\u3044\u3002

        \u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u306e\\(n\\)\u756a\u76ee\u306e\u6570\u3092\\(f(n)\\)\u3068\u3059\u308b\u3068\u30012\u3064\u306e\u7d50\u8ad6\u3092\u7c21\u5358\u306b\u5c0e\u304d\u51fa\u305b\u307e\u3059\uff1a

        • \u6570\u5217\u306e\u6700\u521d\u306e2\u3064\u306e\u6570\u306f\\(f(1) = 0\\)\u3068\\(f(2) = 1\\)\u3067\u3059\u3002
        • \u6570\u5217\u306e\u5404\u6570\u306f\u524d\u306e2\u3064\u306e\u6570\u306e\u5408\u8a08\u3067\u3059\u3002\u3064\u307e\u308a\u3001\\(f(n) = f(n - 1) + f(n - 2)\\)\u3067\u3059\u3002

        \u518d\u5e30\u95a2\u4fc2\u3092\u4f7f\u7528\u3057\u3001\u6700\u521d\u306e2\u3064\u306e\u6570\u3092\u7d42\u4e86\u6761\u4ef6\u3068\u3057\u3066\u8003\u616e\u3059\u308b\u3068\u3001\u518d\u5e30\u30b3\u30fc\u30c9\u3092\u66f8\u3051\u307e\u3059\u3002fib(n)\u3092\u547c\u3073\u51fa\u3059\u3068\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u306e\\(n\\)\u756a\u76ee\u306e\u6570\u304c\u5f97\u3089\u308c\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
        def fib(n: int) -> int:\n    \"\"\"\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\uff1a\u518d\u5e30\"\"\"\n    # \u7d42\u4e86\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if n == 1 or n == 2:\n        return n - 1\n    # \u518d\u5e30\u547c\u3073\u51fa\u3057 f(n) = f(n-1) + f(n-2)\n    res = fib(n - 1) + fib(n - 2)\n    # \u7d50\u679c f(n) \u3092\u8fd4\u3059\n    return res\n
        recursion.cpp
        /* \u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\uff1a\u518d\u5e30 */\nint fib(int n) {\n    // \u7d42\u4e86\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // \u518d\u5e30\u547c\u3073\u51fa\u3057 f(n) = f(n-1) + f(n-2)\n    int res = fib(n - 1) + fib(n - 2);\n    // \u7d50\u679c f(n) \u3092\u8fd4\u3059\n    return res;\n}\n
        recursion.java
        /* \u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\uff1a\u518d\u5e30 */\nint fib(int n) {\n    // \u7d42\u4e86\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // \u518d\u5e30\u547c\u3073\u51fa\u3057 f(n) = f(n-1) + f(n-2)\n    int res = fib(n - 1) + fib(n - 2);\n    // \u7d50\u679c f(n) \u3092\u8fd4\u3059\n    return res;\n}\n
        recursion.cs
        [class]{recursion}-[func]{Fib}\n
        recursion.go
        [class]{}-[func]{fib}\n
        recursion.swift
        [class]{}-[func]{fib}\n
        recursion.js
        [class]{}-[func]{fib}\n
        recursion.ts
        [class]{}-[func]{fib}\n
        recursion.dart
        [class]{}-[func]{fib}\n
        recursion.rs
        [class]{}-[func]{fib}\n
        recursion.c
        [class]{}-[func]{fib}\n
        recursion.kt
        [class]{}-[func]{fib}\n
        recursion.rb
        [class]{}-[func]{fib}\n

        \u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\u305d\u308c\u81ea\u4f53\u306e\u4e2d\u30672\u3064\u306e\u95a2\u6570\u3092\u518d\u5e30\u7684\u306b\u547c\u3073\u51fa\u3057\u3066\u3044\u308b\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002\u3064\u307e\u308a\u30011\u56de\u306e\u547c\u3073\u51fa\u3057\u30672\u3064\u306e\u5206\u5c90\u547c\u3073\u51fa\u3057\u304c\u751f\u6210\u3055\u308c\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u3001\u3053\u306e\u7d99\u7d9a\u7684\u306a\u518d\u5e30\u547c\u3073\u51fa\u3057\u306f\u6700\u7d42\u7684\u306b\u6df1\u3055\\(n\\)\u306e\u518d\u5e30\u6728\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002

        \u56f3 2-6 \u00a0 Fibonacci sequence recursion tree

        \u57fa\u672c\u7684\u306b\u3001\u518d\u5e30\u306f\u300c\u554f\u984c\u3092\u3088\u308a\u5c0f\u3055\u306a\u30b5\u30d6\u554f\u984c\u306b\u5206\u89e3\u3059\u308b\u300d\u30d1\u30e9\u30c0\u30a4\u30e0\u3092\u4f53\u73fe\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306f\u91cd\u8981\u3067\u3059\u3002

        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u89b3\u70b9\u304b\u3089\u3001\u63a2\u7d22\u3001\u30bd\u30fc\u30c8\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3001\u5206\u5272\u7d71\u6cbb\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306a\u3069\u306e\u591a\u304f\u306e\u91cd\u8981\u306a\u6226\u7565\u306f\u3001\u76f4\u63a5\u7684\u307e\u305f\u306f\u9593\u63a5\u7684\u306b\u3053\u306e\u601d\u8003\u65b9\u6cd5\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002
        • \u30c7\u30fc\u30bf\u69cb\u9020\u306e\u89b3\u70b9\u304b\u3089\u3001\u518d\u5e30\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u6728\u3001\u30b0\u30e9\u30d5\u3092\u6271\u3046\u306e\u306b\u81ea\u7136\u306b\u9069\u3057\u3066\u304a\u308a\u3001\u3053\u308c\u3089\u306f\u5206\u5272\u7d71\u6cbb\u30a2\u30d7\u30ed\u30fc\u30c1\u3092\u4f7f\u7528\u3057\u305f\u5206\u6790\u306b\u9069\u3057\u3066\u3044\u308b\u305f\u3081\u3067\u3059\u3002
        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#223","title":"2.2.3 \u00a0 \u6bd4\u8f03","text":"

        \u4e0a\u8a18\u306e\u5185\u5bb9\u3092\u307e\u3068\u3081\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u8868\u306f\u5b9f\u88c5\u3001\u6027\u80fd\u3001\u9069\u7528\u6027\u306e\u89b3\u70b9\u304b\u3089\u53cd\u5fa9\u3068\u518d\u5e30\u306e\u9055\u3044\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u8868: \u53cd\u5fa9\u3068\u518d\u5e30\u306e\u7279\u6027\u306e\u6bd4\u8f03

        \u53cd\u5fa9 \u518d\u5e30 \u30a2\u30d7\u30ed\u30fc\u30c1 \u30eb\u30fc\u30d7\u69cb\u9020 \u95a2\u6570\u304c\u81ea\u5206\u81ea\u8eab\u3092\u547c\u3073\u51fa\u3059 \u6642\u9593\u52b9\u7387 \u4e00\u822c\u7684\u306b\u3088\u308a\u9ad8\u3044\u52b9\u7387\u3001\u95a2\u6570\u547c\u3073\u51fa\u3057\u306e\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u306a\u3057 \u5404\u95a2\u6570\u547c\u3073\u51fa\u3057\u304c\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u3092\u751f\u6210 \u30e1\u30e2\u30ea\u4f7f\u7528\u91cf \u901a\u5e38\u306f\u56fa\u5b9a\u30b5\u30a4\u30ba\u306e\u30e1\u30e2\u30ea\u7a7a\u9593\u3092\u4f7f\u7528 \u7d2f\u7a4d\u7684\u306a\u95a2\u6570\u547c\u3073\u51fa\u3057\u304c\u5927\u91cf\u306e\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u4f7f\u7528\u3059\u308b\u53ef\u80fd\u6027 \u9069\u7528\u53ef\u80fd\u306a\u554f\u984c \u5358\u7d14\u306a\u30eb\u30fc\u30d7\u30bf\u30b9\u30af\u306b\u9069\u3057\u3066\u3044\u308b\u3001\u76f4\u611f\u7684\u3067\u8aad\u307f\u3084\u3059\u3044\u30b3\u30fc\u30c9 \u554f\u984c\u306e\u5206\u89e3\u306b\u9069\u3057\u3066\u3044\u308b\uff08\u6728\u3001\u30b0\u30e9\u30d5\u3001\u5206\u5272\u7d71\u6cbb\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306a\u3069\uff09\u3001\u7c21\u6f54\u3067\u660e\u78ba\u306a\u30b3\u30fc\u30c9\u69cb\u9020

        Tip

        \u4ee5\u4e0b\u306e\u5185\u5bb9\u304c\u7406\u89e3\u3057\u306b\u304f\u3044\u5834\u5408\u306f\u3001\u300c\u30b9\u30bf\u30c3\u30af\u300d\u306e\u7ae0\u3092\u8aad\u3093\u3060\u5f8c\u306b\u518d\u3073\u8a2a\u308c\u308b\u3053\u3068\u3092\u691c\u8a0e\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u305d\u308c\u3067\u306f\u3001\u53cd\u5fa9\u3068\u518d\u5e30\u306e\u672c\u8cea\u7684\u306a\u95a2\u9023\u306f\u4f55\u3067\u3057\u3087\u3046\u304b\uff1f\u4e0a\u8a18\u306e\u518d\u5e30\u95a2\u6570\u3092\u4f8b\u306b\u53d6\u308b\u3068\u3001\u5408\u8a08\u64cd\u4f5c\u306f\u518d\u5e30\u306e\u300c\u8fd4\u5374\u300d\u30d5\u30a7\u30fc\u30ba\u3067\u767a\u751f\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u6700\u521d\u306b\u547c\u3073\u51fa\u3055\u308c\u305f\u95a2\u6570\u304c\u6700\u5f8c\u306b\u5408\u8a08\u64cd\u4f5c\u3092\u5b8c\u4e86\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u3001\u30b9\u30bf\u30c3\u30af\u306e\u300c\u5f8c\u5165\u308c\u5148\u51fa\u3057\u300d\u539f\u7406\u3092\u53cd\u6620\u3057\u3066\u3044\u307e\u3059\u3002

        \u300c\u547c\u3073\u51fa\u3057\u30b9\u30bf\u30c3\u30af\u300d\u3084\u300c\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u300d\u306a\u3069\u306e\u518d\u5e30\u7528\u8a9e\u306f\u3001\u518d\u5e30\u3068\u30b9\u30bf\u30c3\u30af\u306e\u5bc6\u63a5\u306a\u95a2\u4fc2\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        1. \u547c\u3073\u51fa\u3057: \u95a2\u6570\u304c\u547c\u3073\u51fa\u3055\u308c\u308b\u3068\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u300c\u547c\u3073\u51fa\u3057\u30b9\u30bf\u30c3\u30af\u300d\u4e0a\u306b\u305d\u306e\u95a2\u6570\u7528\u306e\u65b0\u3057\u3044\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u3092\u5272\u308a\u5f53\u3066\u3001\u30ed\u30fc\u30ab\u30eb\u5909\u6570\u3001\u30d1\u30e9\u30e1\u30fc\u30bf\u3001\u623b\u308a\u30a2\u30c9\u30ec\u30b9\u3001\u305d\u306e\u4ed6\u306e\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3057\u307e\u3059\u3002
        2. \u8fd4\u5374: \u95a2\u6570\u304c\u5b9f\u884c\u3092\u5b8c\u4e86\u3057\u3066\u8fd4\u308b\u3068\u3001\u5bfe\u5fdc\u3059\u308b\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u304c\u300c\u547c\u3073\u51fa\u3057\u30b9\u30bf\u30c3\u30af\u300d\u304b\u3089\u524a\u9664\u3055\u308c\u3001\u524d\u306e\u95a2\u6570\u306e\u5b9f\u884c\u74b0\u5883\u304c\u5fa9\u5143\u3055\u308c\u307e\u3059\u3002

        \u3057\u305f\u304c\u3063\u3066\u3001\u660e\u793a\u7684\u306a\u30b9\u30bf\u30c3\u30af\u3092\u4f7f\u7528\u3057\u3066\u547c\u3073\u51fa\u3057\u30b9\u30bf\u30c3\u30af\u306e\u52d5\u4f5c\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u518d\u5e30\u3092\u53cd\u5fa9\u5f62\u5f0f\u306b\u5909\u63db\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
        def for_loop_recur(n: int) -> int:\n    \"\"\"\u53cd\u5fa9\u3067\u518d\u5e30\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\"\"\"\n    # \u660e\u793a\u7684\u306a\u30b9\u30bf\u30c3\u30af\u3092\u4f7f\u7528\u3057\u3066\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u30b9\u30bf\u30c3\u30af\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\n    stack = []\n    res = 0\n    # \u518d\u5e30\uff1a\u518d\u5e30\u547c\u3073\u51fa\u3057\n    for i in range(n, 0, -1):\n        # \u300c\u30b9\u30bf\u30c3\u30af\u3078\u306e\u30d7\u30c3\u30b7\u30e5\u300d\u3067\u300c\u518d\u5e30\u300d\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\n        stack.append(i)\n    # \u5fa9\u5e30\uff1a\u7d50\u679c\u3092\u8fd4\u3059\n    while stack:\n        # \u300c\u30b9\u30bf\u30c3\u30af\u304b\u3089\u306e\u30dd\u30c3\u30d7\u300d\u3067\u300c\u5fa9\u5e30\u300d\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\n        res += stack.pop()\n    # res = 1+2+3+...+n\n    return res\n
        recursion.cpp
        /* \u53cd\u5fa9\u3067\u518d\u5e30\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8 */\nint forLoopRecur(int n) {\n    // \u660e\u793a\u7684\u306a\u30b9\u30bf\u30c3\u30af\u3092\u4f7f\u7528\u3057\u3066\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u30b9\u30bf\u30c3\u30af\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\n    stack<int> stack;\n    int res = 0;\n    // \u518d\u5e30\uff1a\u518d\u5e30\u547c\u3073\u51fa\u3057\n    for (int i = n; i > 0; i--) {\n        // \u300c\u30b9\u30bf\u30c3\u30af\u3078\u306e\u30d7\u30c3\u30b7\u30e5\u300d\u3067\u300c\u518d\u5e30\u300d\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\n        stack.push(i);\n    }\n    // \u623b\u308a\u5024\uff1a\u7d50\u679c\u3092\u8fd4\u3059\n    while (!stack.empty()) {\n        // \u300c\u30b9\u30bf\u30c3\u30af\u304b\u3089\u306e\u30dd\u30c3\u30d7\u300d\u3067\u300c\u623b\u308a\u5024\u300d\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\n        res += stack.top();\n        stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
        recursion.java
        /* \u53cd\u5fa9\u3067\u518d\u5e30\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8 */\nint forLoopRecur(int n) {\n    // \u660e\u793a\u7684\u306a\u30b9\u30bf\u30c3\u30af\u3092\u4f7f\u7528\u3057\u3066\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u30b9\u30bf\u30c3\u30af\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\n    Stack<Integer> stack = new Stack<>();\n    int res = 0;\n    // \u518d\u5e30\uff1a\u518d\u5e30\u547c\u3073\u51fa\u3057\n    for (int i = n; i > 0; i--) {\n        // \u300c\u30b9\u30bf\u30c3\u30af\u3078\u306e\u30d7\u30c3\u30b7\u30e5\u300d\u3067\u300c\u518d\u5e30\u300d\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\n        stack.push(i);\n    }\n    // \u623b\u308a\u5024\uff1a\u7d50\u679c\u3092\u8fd4\u3059\n    while (!stack.isEmpty()) {\n        // \u300c\u30b9\u30bf\u30c3\u30af\u304b\u3089\u306e\u30dd\u30c3\u30d7\u300d\u3067\u300c\u623b\u308a\u5024\u300d\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\n        res += stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
        recursion.cs
        [class]{recursion}-[func]{ForLoopRecur}\n
        recursion.go
        [class]{}-[func]{forLoopRecur}\n
        recursion.swift
        [class]{}-[func]{forLoopRecur}\n
        recursion.js
        [class]{}-[func]{forLoopRecur}\n
        recursion.ts
        [class]{}-[func]{forLoopRecur}\n
        recursion.dart
        [class]{}-[func]{forLoopRecur}\n
        recursion.rs
        [class]{}-[func]{for_loop_recur}\n
        recursion.c
        [class]{}-[func]{forLoopRecur}\n
        recursion.kt
        [class]{}-[func]{forLoopRecur}\n
        recursion.rb
        [class]{}-[func]{for_loop_recur}\n

        \u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\u518d\u5e30\u304c\u53cd\u5fa9\u306b\u5909\u63db\u3055\u308c\u305f\u3068\u304d\u3001\u30b3\u30fc\u30c9\u306f\u3088\u308a\u8907\u96d1\u306b\u306a\u308a\u307e\u3059\u3002\u53cd\u5fa9\u3068\u518d\u5e30\u306f\u3057\u3070\u3057\u3070\u76f8\u4e92\u306b\u5909\u63db\u3067\u304d\u307e\u3059\u304c\u30012\u3064\u306e\u7406\u7531\u3067\u305d\u3046\u3059\u308b\u3053\u3068\u304c\u5e38\u306b\u63a8\u5968\u3055\u308c\u308b\u308f\u3051\u3067\u306f\u3042\u308a\u307e\u305b\u3093\uff1a

        • \u5909\u63db\u3055\u308c\u305f\u30b3\u30fc\u30c9\u306f\u7406\u89e3\u304c\u3088\u308a\u56f0\u96e3\u306b\u306a\u308a\u3001\u8aad\u307f\u306b\u304f\u304f\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        • \u4e00\u90e8\u306e\u8907\u96d1\u306a\u554f\u984c\u3067\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u306e\u547c\u3073\u51fa\u3057\u30b9\u30bf\u30c3\u30af\u306e\u52d5\u4f5c\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\u3059\u308b\u3053\u3068\u306f\u975e\u5e38\u306b\u56f0\u96e3\u3067\u3059\u3002

        \u7d50\u8ad6\u3068\u3057\u3066\u3001\u53cd\u5fa9\u307e\u305f\u306f\u518d\u5e30\u3092\u9078\u629e\u3059\u308b\u304b\u306f\u554f\u984c\u306e\u5177\u4f53\u7684\u306a\u6027\u8cea\u306b\u3088\u308a\u307e\u3059\u3002\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306e\u5b9f\u8df5\u3067\u306f\u3001\u4e21\u65b9\u306e\u9577\u6240\u3068\u77ed\u6240\u3092\u6bd4\u8f03\u691c\u8a0e\u3057\u3001\u624b\u5143\u306e\u72b6\u6cc1\u306b\u6700\u3082\u9069\u3057\u305f\u30a2\u30d7\u30ed\u30fc\u30c1\u3092\u9078\u629e\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002

        "},{"location":"chapter_computational_complexity/performance_evaluation/","title":"2.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u8a55\u4fa1","text":"

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u8a2d\u8a08\u306b\u304a\u3044\u3066\u3001\u79c1\u305f\u3061\u306f\u9806\u5e8f\u306b\u5f93\u3063\u3066\u4ee5\u4e0b\u306e2\u3064\u306e\u76ee\u6a19\u3092\u8ffd\u6c42\u3057\u307e\u3059\u3002

        1. \u554f\u984c\u306e\u89e3\u6c7a\u7b56\u3092\u898b\u3064\u3051\u308b: \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u6307\u5b9a\u3055\u308c\u305f\u5165\u529b\u7bc4\u56f2\u5185\u3067\u78ba\u5b9f\u306b\u6b63\u3057\u3044\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3079\u304d\u3067\u3059\u3002
        2. \u6700\u9069\u89e3\u3092\u6c42\u3081\u308b: \u540c\u3058\u554f\u984c\u306b\u5bfe\u3057\u3066\u8907\u6570\u306e\u89e3\u6c7a\u7b56\u304c\u5b58\u5728\u3059\u308b\u5834\u5408\u304c\u3042\u308a\u3001\u79c1\u305f\u3061\u306f\u53ef\u80fd\u306a\u9650\u308a\u6700\u3082\u52b9\u7387\u7684\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3092\u76ee\u6307\u3057\u307e\u3059\u3002

        \u3064\u307e\u308a\u3001\u554f\u984c\u3092\u89e3\u6c7a\u3067\u304d\u308b\u3053\u3068\u3092\u524d\u63d0\u3068\u3057\u3066\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u304c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u8a55\u4fa1\u3059\u308b\u4e3b\u8981\u306a\u57fa\u6e96\u3068\u306a\u3063\u3066\u304a\u308a\u3001\u3053\u308c\u306b\u306f\u4ee5\u4e0b\u306e2\u3064\u306e\u6b21\u5143\u304c\u542b\u307e\u308c\u307e\u3059\u3002

        • \u6642\u9593\u52b9\u7387: \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u5b9f\u884c\u3055\u308c\u308b\u901f\u5ea6\u3002
        • \u7a7a\u9593\u52b9\u7387: \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u5360\u6709\u3059\u308b\u30e1\u30e2\u30ea\u7a7a\u9593\u306e\u30b5\u30a4\u30ba\u3002

        \u8981\u3059\u308b\u306b\u3001\u79c1\u305f\u3061\u306e\u76ee\u6a19\u306f\u3001\u9ad8\u901f\u3067\u30e1\u30e2\u30ea\u52b9\u7387\u306e\u826f\u3044\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u8a2d\u8a08\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u52b9\u679c\u7684\u306b\u8a55\u4fa1\u3059\u308b\u3053\u3068\u306f\u91cd\u8981\u3067\u3059\u3002\u306a\u305c\u306a\u3089\u3001\u305d\u3046\u3059\u308b\u3053\u3068\u3067\u521d\u3081\u3066\u69d8\u3005\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u6bd4\u8f03\u3057\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u8a2d\u8a08\u3068\u6700\u9069\u5316\u30d7\u30ed\u30bb\u30b9\u3092\u5c0e\u304f\u3053\u3068\u304c\u3067\u304d\u308b\u304b\u3089\u3067\u3059\u3002

        \u52b9\u7387\u8a55\u4fa1\u306b\u306f\u4e3b\u306b2\u3064\u306e\u65b9\u6cd5\u304c\u3042\u308a\u307e\u3059\uff1a\u5b9f\u969b\u306e\u30c6\u30b9\u30c8\u3068\u7406\u8ad6\u7684\u63a8\u5b9a\u3067\u3059\u3002

        "},{"location":"chapter_computational_complexity/performance_evaluation/#211","title":"2.1.1 \u00a0 \u5b9f\u969b\u306e\u30c6\u30b9\u30c8","text":"

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u3068B\u304c\u3042\u308a\u3001\u3069\u3061\u3089\u3082\u540c\u3058\u554f\u984c\u3092\u89e3\u6c7a\u3067\u304d\u3001\u305d\u308c\u3089\u306e\u52b9\u7387\u3092\u6bd4\u8f03\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3068\u3057\u307e\u3059\u3002\u6700\u3082\u76f4\u63a5\u7684\u306a\u65b9\u6cd5\u306f\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u3092\u4f7f\u7528\u3057\u3066\u3053\u308c\u30892\u3064\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u5b9f\u884c\u3057\u3001\u5b9f\u884c\u6642\u9593\u3068\u30e1\u30e2\u30ea\u4f7f\u7528\u91cf\u3092\u76e3\u8996\u30fb\u8a18\u9332\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u3053\u306e\u8a55\u4fa1\u65b9\u6cd5\u306f\u5b9f\u969b\u306e\u72b6\u6cc1\u3092\u53cd\u6620\u3057\u307e\u3059\u304c\u3001\u5927\u304d\u306a\u5236\u9650\u304c\u3042\u308a\u307e\u3059\u3002

        \u4e00\u65b9\u3067\u3001\u30c6\u30b9\u30c8\u74b0\u5883\u304b\u3089\u306e\u5e72\u6e09\u3092\u6392\u9664\u3059\u308b\u3053\u3068\u306f\u56f0\u96e3\u3067\u3059\u3002\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u69cb\u6210\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6027\u80fd\u306b\u5f71\u97ff\u3092\u4e0e\u3048\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u4e26\u5217\u5ea6\u306e\u9ad8\u3044\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u30de\u30eb\u30c1\u30b3\u30a2CPU\u3067\u306e\u5b9f\u884c\u306b\u3088\u308a\u9069\u3057\u3066\u3044\u307e\u3059\u3057\u3001\u96c6\u7d04\u7684\u306a\u30e1\u30e2\u30ea\u64cd\u4f5c\u3092\u542b\u3080\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u9ad8\u6027\u80fd\u30e1\u30e2\u30ea\u3067\u3088\u308a\u826f\u3044\u6027\u80fd\u3092\u767a\u63ee\u3057\u307e\u3059\u3002\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u30c6\u30b9\u30c8\u7d50\u679c\u306f\u3001\u7570\u306a\u308b\u30de\u30b7\u30f3\u9593\u3067\u5909\u308f\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u5e73\u5747\u52b9\u7387\u3092\u8a08\u7b97\u3059\u308b\u305f\u3081\u306b\u8907\u6570\u306e\u30de\u30b7\u30f3\u3067\u30c6\u30b9\u30c8\u3059\u308b\u3053\u3068\u304c\u5b9f\u7528\u7684\u3067\u306a\u3044\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

        \u4e00\u65b9\u3067\u3001\u5b8c\u5168\u306a\u30c6\u30b9\u30c8\u3092\u5b9f\u65bd\u3059\u308b\u3053\u3068\u306f\u975e\u5e38\u306b\u30ea\u30bd\u30fc\u30b9\u96c6\u7d04\u7684\u3067\u3059\u3002\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u306f\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\u306b\u3088\u3063\u3066\u5909\u308f\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u30c7\u30fc\u30bf\u91cf\u304c\u5c11\u306a\u3044\u5834\u5408\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u304cB\u3088\u308a\u901f\u304f\u5b9f\u884c\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u30c7\u30fc\u30bf\u91cf\u304c\u591a\u3044\u5834\u5408\u306f\u30c6\u30b9\u30c8\u7d50\u679c\u304c\u9006\u306b\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u8aac\u5f97\u529b\u306e\u3042\u308b\u7d50\u8ad6\u3092\u5c0e\u304f\u305f\u3081\u306b\u306f\u3001\u5e45\u5e83\u3044\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\u3092\u30c6\u30b9\u30c8\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u3053\u308c\u306b\u306f\u904e\u5ea6\u306a\u8a08\u7b97\u30ea\u30bd\u30fc\u30b9\u304c\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/performance_evaluation/#212","title":"2.1.2 \u00a0 \u7406\u8ad6\u7684\u63a8\u5b9a","text":"

        \u5b9f\u969b\u306e\u30c6\u30b9\u30c8\u306e\u5927\u304d\u306a\u5236\u9650\u306b\u3088\u308a\u3001\u8a08\u7b97\u306e\u307f\u3067\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u8a55\u4fa1\u3059\u308b\u3053\u3068\u3092\u691c\u8a0e\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u63a8\u5b9a\u65b9\u6cd5\u306f\u6f38\u8fd1\u7684\u8907\u96d1\u5ea6\u89e3\u6790\u3001\u307e\u305f\u306f\u5358\u306b\u8907\u96d1\u5ea6\u89e3\u6790\u3068\u3057\u3066\u77e5\u3089\u308c\u3066\u3044\u307e\u3059\u3002

        \u8907\u96d1\u5ea6\u89e3\u6790\u306f\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u306b\u5fc5\u8981\u306a\u6642\u9593\u3068\u7a7a\u9593\u30ea\u30bd\u30fc\u30b9\u3068\u5165\u529b\u30c7\u30fc\u30bf\u306e\u30b5\u30a4\u30ba\u3068\u306e\u95a2\u4fc2\u3092\u53cd\u6620\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u5165\u529b\u30c7\u30fc\u30bf\u306e\u30b5\u30a4\u30ba\u304c\u5897\u52a0\u3059\u308b\u306b\u3064\u308c\u3066\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u5fc5\u8981\u306a\u6642\u9593\u3068\u7a7a\u9593\u306e\u5897\u52a0\u50be\u5411\u3092\u8a18\u8ff0\u3057\u307e\u3059\u3002\u3053\u306e\u5b9a\u7fa9\u306f\u8907\u96d1\u306b\u805e\u3053\u3048\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u3088\u308a\u826f\u304f\u7406\u89e3\u3059\u308b\u305f\u3081\u306b3\u3064\u306e\u91cd\u8981\u306a\u30dd\u30a4\u30f3\u30c8\u306b\u5206\u89e3\u3067\u304d\u307e\u3059\u3002

        • \u300c\u6642\u9593\u3068\u7a7a\u9593\u30ea\u30bd\u30fc\u30b9\u300d\u306f\u3001\u305d\u308c\u305e\u308c\u6642\u9593\u8a08\u7b97\u91cf\u3068\u7a7a\u9593\u8a08\u7b97\u91cf\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002
        • \u300c\u5165\u529b\u30c7\u30fc\u30bf\u306e\u30b5\u30a4\u30ba\u304c\u5897\u52a0\u3059\u308b\u306b\u3064\u308c\u3066\u300d\u306f\u3001\u8907\u96d1\u5ea6\u304c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3068\u5165\u529b\u30c7\u30fc\u30bf\u91cf\u3068\u306e\u95a2\u4fc2\u3092\u53cd\u6620\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002
        • \u300c\u6642\u9593\u3068\u7a7a\u9593\u306e\u5897\u52a0\u50be\u5411\u300d\u306f\u3001\u8907\u96d1\u5ea6\u89e3\u6790\u304c\u5b9f\u884c\u6642\u9593\u3084\u5360\u6709\u7a7a\u9593\u306e\u5177\u4f53\u7684\u306a\u5024\u3067\u306f\u306a\u304f\u3001\u6642\u9593\u3084\u7a7a\u9593\u304c\u5897\u52a0\u3059\u308b\u300c\u7387\u300d\u306b\u7126\u70b9\u3092\u5f53\u3066\u308b\u3053\u3068\u3092\u793a\u3057\u307e\u3059\u3002

        \u8907\u96d1\u5ea6\u89e3\u6790\u306f\u5b9f\u969b\u306e\u30c6\u30b9\u30c8\u65b9\u6cd5\u306e\u6b20\u70b9\u3092\u514b\u670d\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u4ee5\u4e0b\u306e\u5074\u9762\u3067\u53cd\u6620\u3055\u308c\u307e\u3059\uff1a

        • \u5b9f\u969b\u306b\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u306a\u3044\u305f\u3081\u3001\u3088\u308a\u74b0\u5883\u306b\u512a\u3057\u304f\u3001\u30a8\u30cd\u30eb\u30ae\u30fc\u52b9\u7387\u304c\u826f\u3044\u3067\u3059\u3002
        • \u30c6\u30b9\u30c8\u74b0\u5883\u306b\u4f9d\u5b58\u305b\u305a\u3001\u3059\u3079\u3066\u306e\u52d5\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u306b\u9069\u7528\u3067\u304d\u307e\u3059\u3002
        • \u7570\u306a\u308b\u30c7\u30fc\u30bf\u91cf\u3067\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u53cd\u6620\u3067\u304d\u3001\u7279\u306b\u5927\u91cf\u30c7\u30fc\u30bf\u3067\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6027\u80fd\u3092\u793a\u3057\u307e\u3059\u3002

        Tip

        \u8907\u96d1\u5ea6\u306e\u6982\u5ff5\u306b\u3064\u3044\u3066\u307e\u3060\u6df7\u4e71\u3057\u3066\u3044\u308b\u5834\u5408\u3067\u3082\u3001\u5fc3\u914d\u3057\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002\u4ee5\u964d\u306e\u7ae0\u3067\u8a73\u3057\u304f\u53d6\u308a\u4e0a\u3052\u307e\u3059\u3002

        \u8907\u96d1\u5ea6\u89e3\u6790\u306f\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u8a55\u4fa1\u3059\u308b\u300c\u3082\u306e\u3055\u3057\u300d\u3092\u63d0\u4f9b\u3057\u3001\u5b9f\u884c\u306b\u5fc5\u8981\u306a\u6642\u9593\u3068\u7a7a\u9593\u30ea\u30bd\u30fc\u30b9\u3092\u6e2c\u5b9a\u3057\u3001\u7570\u306a\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u6bd4\u8f03\u3059\u308b\u3053\u3068\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002

        \u8907\u96d1\u5ea6\u306f\u6570\u5b66\u7684\u6982\u5ff5\u3067\u3042\u308a\u3001\u521d\u5fc3\u8005\u306b\u306f\u62bd\u8c61\u7684\u3067\u56f0\u96e3\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u3053\u306e\u89b3\u70b9\u304b\u3089\u3001\u8907\u96d1\u5ea6\u89e3\u6790\u306f\u6700\u521d\u306b\u7d39\u4ecb\u3059\u308b\u306e\u306b\u6700\u3082\u9069\u3057\u305f\u30c8\u30d4\u30c3\u30af\u3067\u306f\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u3057\u304b\u3057\u3001\u7279\u5b9a\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u3084\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7279\u6027\u306b\u3064\u3044\u3066\u8b70\u8ad6\u3059\u308b\u3068\u304d\u3001\u305d\u306e\u901f\u5ea6\u3068\u7a7a\u9593\u4f7f\u7528\u91cf\u3092\u5206\u6790\u3059\u308b\u3053\u3068\u3092\u907f\u3051\u308b\u306e\u306f\u56f0\u96e3\u3067\u3059\u3002

        \u8981\u7d04\u3059\u308b\u3068\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u6df1\u304f\u5165\u308b\u524d\u306b\u8907\u96d1\u5ea6\u89e3\u6790\u306e\u57fa\u672c\u7684\u306a\u7406\u89e3\u3092\u8eab\u306b\u3064\u3051\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u7c21\u5358\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u8907\u96d1\u5ea6\u89e3\u6790\u3092\u5b9f\u884c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/space_complexity/","title":"2.4 \u00a0 \u7a7a\u9593\u8a08\u7b97\u91cf","text":"

        \u7a7a\u9593\u8a08\u7b97\u91cf\u306f\u3001\u30c7\u30fc\u30bf\u91cf\u304c\u5897\u52a0\u3059\u308b\u306b\u3064\u308c\u3066\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u5360\u6709\u3059\u308b\u30e1\u30e2\u30ea\u7a7a\u9593\u306e\u5897\u52a0\u50be\u5411\u3092\u6e2c\u5b9a\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u3053\u306e\u6982\u5ff5\u306f\u6642\u9593\u8a08\u7b97\u91cf\u3068\u975e\u5e38\u306b\u4f3c\u3066\u3044\u307e\u3059\u304c\u3001\u300c\u5b9f\u884c\u6642\u9593\u300d\u304c\u300c\u5360\u6709\u30e1\u30e2\u30ea\u7a7a\u9593\u300d\u306b\u7f6e\u304d\u63db\u3048\u3089\u308c\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/space_complexity/#241","title":"2.4.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u95a2\u9023\u3059\u308b\u7a7a\u9593","text":"

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u5b9f\u884c\u4e2d\u306b\u4f7f\u7528\u3059\u308b\u30e1\u30e2\u30ea\u7a7a\u9593\u306b\u306f\u3001\u4e3b\u306b\u4ee5\u4e0b\u306e\u7a2e\u985e\u304c\u3042\u308a\u307e\u3059\u3002

        • \u5165\u529b\u7a7a\u9593: \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5165\u529b\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        • \u4e00\u6642\u7a7a\u9593: \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u4e2d\u306b\u5909\u6570\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3001\u95a2\u6570\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3001\u305d\u306e\u4ed6\u306e\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        • \u51fa\u529b\u7a7a\u9593: \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u51fa\u529b\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002

        \u4e00\u822c\u7684\u306b\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306e\u7d71\u8a08\u7bc4\u56f2\u306b\u306f\u300c\u4e00\u6642\u7a7a\u9593\u300d\u3068\u300c\u51fa\u529b\u7a7a\u9593\u300d\u306e\u4e21\u65b9\u304c\u542b\u307e\u308c\u307e\u3059\u3002

        \u4e00\u6642\u7a7a\u9593\u306f\u3055\u3089\u306b3\u3064\u306e\u90e8\u5206\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        • \u4e00\u6642\u30c7\u30fc\u30bf: \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u4e2d\u306b\u69d8\u3005\u306a\u5b9a\u6570\u3001\u5909\u6570\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306a\u3069\u3092\u4fdd\u5b58\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        • \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593: \u547c\u3073\u51fa\u3055\u308c\u305f\u95a2\u6570\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u30b7\u30b9\u30c6\u30e0\u306f\u95a2\u6570\u304c\u547c\u3073\u51fa\u3055\u308c\u308b\u305f\u3073\u306b\u30b9\u30bf\u30c3\u30af\u306e\u9802\u4e0a\u306b\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u3092\u4f5c\u6210\u3057\u3001\u95a2\u6570\u304c\u8fd4\u3055\u308c\u305f\u5f8c\u306b\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u89e3\u653e\u3057\u307e\u3059\u3002
        • \u547d\u4ee4\u7a7a\u9593: \u30b3\u30f3\u30d1\u30a4\u30eb\u6e08\u307f\u30d7\u30ed\u30b0\u30e9\u30e0\u547d\u4ee4\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u3001\u5b9f\u969b\u306e\u7d71\u8a08\u3067\u306f\u901a\u5e38\u7121\u8996\u3067\u304d\u307e\u3059\u3002

        \u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u3092\u5206\u6790\u3059\u308b\u969b\u3001\u901a\u5e38\u306f\u4e00\u6642\u30c7\u30fc\u30bf\u3001\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3001\u51fa\u529b\u30c7\u30fc\u30bf\u3092\u30ab\u30a6\u30f3\u30c8\u3057\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        \u56f3 2-15 \u00a0 Space types used in algorithms

        \u95a2\u9023\u3059\u308b\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin
        class Node:\n    \"\"\"\u30af\u30e9\u30b9\"\"\"\n    def __init__(self, x: int):\n        self.val: int = x               # \u30ce\u30fc\u30c9\u5024\n        self.next: Node | None = None   # \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n\ndef function() -> int:\n    \"\"\"\u95a2\u6570\"\"\"\n    # \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c...\n    return 0\n\ndef algorithm(n) -> int:    # \u5165\u529b\u30c7\u30fc\u30bf\n    A = 0                   # \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5b9a\u6570\u3001\u901a\u5e38\u5927\u6587\u5b57\uff09\n    b = 0                   # \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5909\u6570\uff09\n    node = Node(0)          # \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\n    c = function()          # \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\uff08\u95a2\u6570\u547c\u3073\u51fa\u3057\uff09\n    return A + b + c        # \u51fa\u529b\u30c7\u30fc\u30bf\n
        /* \u69cb\u9020\u4f53 */\nstruct Node {\n    int val;\n    Node *next;\n    Node(int x) : val(x), next(nullptr) {}\n};\n\n/* \u95a2\u6570 */\nint func() {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c...\n    return 0;\n}\n\nint algorithm(int n) {          // \u5165\u529b\u30c7\u30fc\u30bf\n    const int a = 0;            // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5b9a\u6570\uff09\n    int b = 0;                  // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5909\u6570\uff09\n    Node* node = new Node(0);   // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\n    int c = func();             // \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\uff08\u95a2\u6570\u547c\u3073\u51fa\u3057\uff09\n    return a + b + c;           // \u51fa\u529b\u30c7\u30fc\u30bf\n}\n
        /* \u30af\u30e9\u30b9 */\nclass Node {\n    int val;\n    Node next;\n    Node(int x) { val = x; }\n}\n\n/* \u95a2\u6570 */\nint function() {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c...\n    return 0;\n}\n\nint algorithm(int n) {          // \u5165\u529b\u30c7\u30fc\u30bf\n    final int a = 0;            // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5b9a\u6570\uff09\n    int b = 0;                  // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5909\u6570\uff09\n    Node node = new Node(0);    // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\n    int c = function();         // \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\uff08\u95a2\u6570\u547c\u3073\u51fa\u3057\uff09\n    return a + b + c;           // \u51fa\u529b\u30c7\u30fc\u30bf\n}\n
        /* \u30af\u30e9\u30b9 */\nclass Node {\n    int val;\n    Node next;\n    Node(int x) { val = x; }\n}\n\n/* \u95a2\u6570 */\nint Function() {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c...\n    return 0;\n}\n\nint Algorithm(int n) {  // \u5165\u529b\u30c7\u30fc\u30bf\n    const int a = 0;    // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5b9a\u6570\uff09\n    int b = 0;          // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5909\u6570\uff09\n    Node node = new(0); // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\n    int c = Function(); // \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\uff08\u95a2\u6570\u547c\u3073\u51fa\u3057\uff09\n    return a + b + c;   // \u51fa\u529b\u30c7\u30fc\u30bf\n}\n
        /* \u69cb\u9020\u4f53 */\ntype node struct {\n    val  int\n    next *node\n}\n\n/* \u30ce\u30fc\u30c9\u69cb\u9020\u4f53\u3092\u4f5c\u6210 */\nfunc newNode(val int) *node {\n    return &node{val: val}\n}\n\n/* \u95a2\u6570 */\nfunc function() int {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c...\n    return 0\n}\n\nfunc algorithm(n int) int { // \u5165\u529b\u30c7\u30fc\u30bf\n    const a = 0             // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5b9a\u6570\uff09\n    b := 0                  // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5909\u6570\uff09\n    newNode(0)              // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\n    c := function()         // \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\uff08\u95a2\u6570\u547c\u3073\u51fa\u3057\uff09\n    return a + b + c        // \u51fa\u529b\u30c7\u30fc\u30bf\n}\n
        /* \u30af\u30e9\u30b9 */\nclass Node {\n    var val: Int\n    var next: Node?\n\n    init(x: Int) {\n        val = x\n    }\n}\n\n/* \u95a2\u6570 */\nfunc function() -> Int {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c...\n    return 0\n}\n\nfunc algorithm(n: Int) -> Int { // \u5165\u529b\u30c7\u30fc\u30bf\n    let a = 0                   // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5b9a\u6570\uff09\n    var b = 0                   // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5909\u6570\uff09\n    let node = Node(x: 0)       // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\n    let c = function()          // \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\uff08\u95a2\u6570\u547c\u3073\u51fa\u3057\uff09\n    return a + b + c            // \u51fa\u529b\u30c7\u30fc\u30bf\n}\n
        /* \u30af\u30e9\u30b9 */\nclass Node {\n    val;\n    next;\n    constructor(val) {\n        this.val = val === undefined ? 0 : val; // \u30ce\u30fc\u30c9\u5024\n        this.next = null;                       // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    }\n}\n\n/* \u95a2\u6570 */\nfunction constFunc() {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0;\n}\n\nfunction algorithm(n) {         // \u5165\u529b\u30c7\u30fc\u30bf\n    const a = 0;                // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5b9a\u6570\uff09\n    let b = 0;                  // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5909\u6570\uff09\n    const node = new Node(0);   // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\n    const c = constFunc();      // \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\uff08\u95a2\u6570\u547c\u3073\u51fa\u3057\uff09\n    return a + b + c;           // \u51fa\u529b\u30c7\u30fc\u30bf\n}\n
        /* \u30af\u30e9\u30b9 */\nclass Node {\n    val: number;\n    next: Node | null;\n    constructor(val?: number) {\n        this.val = val === undefined ? 0 : val; // \u30ce\u30fc\u30c9\u5024\n        this.next = null;                       // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    }\n}\n\n/* \u95a2\u6570 */\nfunction constFunc(): number {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0;\n}\n\nfunction algorithm(n: number): number { // \u5165\u529b\u30c7\u30fc\u30bf\n    const a = 0;                        // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5b9a\u6570\uff09\n    let b = 0;                          // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5909\u6570\uff09\n    const node = new Node(0);           // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\n    const c = constFunc();              // \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\uff08\u95a2\u6570\u547c\u3073\u51fa\u3057\uff09\n    return a + b + c;                   // \u51fa\u529b\u30c7\u30fc\u30bf\n}\n
        /* \u30af\u30e9\u30b9 */\nclass Node {\n  int val;\n  Node next;\n  Node(this.val, [this.next]);\n}\n\n/* \u95a2\u6570 */\nint function() {\n  // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c...\n  return 0;\n}\n\nint algorithm(int n) {  // \u5165\u529b\u30c7\u30fc\u30bf\n  const int a = 0;      // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5b9a\u6570\uff09\n  int b = 0;            // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5909\u6570\uff09\n  Node node = Node(0);  // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\n  int c = function();   // \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\uff08\u95a2\u6570\u547c\u3073\u51fa\u3057\uff09\n  return a + b + c;     // \u51fa\u529b\u30c7\u30fc\u30bf\n}\n
        use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u69cb\u9020\u4f53 */\nstruct Node {\n    val: i32,\n    next: Option<Rc<RefCell<Node>>>,\n}\n\n/* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\nimpl Node {\n    fn new(val: i32) -> Self {\n        Self { val: val, next: None }\n    }\n}\n\n/* \u95a2\u6570 */\nfn function() -> i32 {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c...\n    return 0;\n}\n\nfn algorithm(n: i32) -> i32 {   // \u5165\u529b\u30c7\u30fc\u30bf\n    const a: i32 = 0;           // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5b9a\u6570\uff09\n    let mut b = 0;              // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5909\u6570\uff09\n    let node = Node::new(0);    // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\n    let c = function();         // \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\uff08\u95a2\u6570\u547c\u3073\u51fa\u3057\uff09\n    return a + b + c;           // \u51fa\u529b\u30c7\u30fc\u30bf\n}\n
        /* \u95a2\u6570 */\nint func() {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c...\n    return 0;\n}\n\nint algorithm(int n) {  // \u5165\u529b\u30c7\u30fc\u30bf\n    const int a = 0;    // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5b9a\u6570\uff09\n    int b = 0;          // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5909\u6570\uff09\n    int c = func();     // \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\uff08\u95a2\u6570\u547c\u3073\u51fa\u3057\uff09\n    return a + b + c;   // \u51fa\u529b\u30c7\u30fc\u30bf\n}\n
        \n
        "},{"location":"chapter_computational_complexity/space_complexity/#242","title":"2.4.2 \u00a0 \u8a08\u7b97\u65b9\u6cd5","text":"

        \u7a7a\u9593\u8a08\u7b97\u91cf\u3092\u8a08\u7b97\u3059\u308b\u65b9\u6cd5\u306f\u6642\u9593\u8a08\u7b97\u91cf\u3068\u307b\u307c\u540c\u69d8\u3067\u3001\u7d71\u8a08\u5bfe\u8c61\u3092\u300c\u64cd\u4f5c\u6570\u300d\u304b\u3089\u300c\u4f7f\u7528\u7a7a\u9593\u306e\u30b5\u30a4\u30ba\u300d\u306b\u5909\u66f4\u3059\u308b\u3060\u3051\u3067\u3059\u3002

        \u3057\u304b\u3057\u3001\u6642\u9593\u8a08\u7b97\u91cf\u3068\u306f\u7570\u306a\u308a\u3001\u901a\u5e38\u306f\u6700\u60aa\u30b1\u30fc\u30b9\u7a7a\u9593\u8a08\u7b97\u91cf\u306e\u307f\u306b\u7126\u70b9\u3092\u5f53\u3066\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u30e1\u30e2\u30ea\u7a7a\u9593\u304c\u30cf\u30fc\u30c9\u8981\u4ef6\u3067\u3042\u308a\u3001\u3059\u3079\u3066\u306e\u5165\u529b\u30c7\u30fc\u30bf\u306e\u4e0b\u3067\u5341\u5206\u306a\u30e1\u30e2\u30ea\u7a7a\u9593\u304c\u78ba\u4fdd\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3067\u3059\u3002

        \u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u6700\u60aa\u30b1\u30fc\u30b9\u7a7a\u9593\u8a08\u7b97\u91cf\u306e\u300c\u6700\u60aa\u30b1\u30fc\u30b9\u300d\u3068\u3044\u3046\u7528\u8a9e\u306b\u306f2\u3064\u306e\u610f\u5473\u304c\u3042\u308a\u307e\u3059\u3002

        1. \u6700\u60aa\u306e\u5165\u529b\u30c7\u30fc\u30bf\u306b\u57fa\u3065\u304f: \\(n < 10\\)\u306e\u5834\u5408\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u3067\u3059\u304c\u3001\\(n > 10\\)\u306e\u5834\u5408\u3001\u521d\u671f\u5316\u3055\u308c\u305f\u914d\u5217nums\u304c\\(O(n)\\)\u306e\u7a7a\u9593\u3092\u5360\u6709\u3059\u308b\u305f\u3081\u3001\u6700\u60aa\u30b1\u30fc\u30b9\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3059\u3002
        2. \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u4e2d\u306b\u4f7f\u7528\u3055\u308c\u308b\u30d4\u30fc\u30af\u30e1\u30e2\u30ea\u306b\u57fa\u3065\u304f: \u4f8b\u3048\u3070\u3001\u6700\u5f8c\u306e\u884c\u3092\u5b9f\u884c\u3059\u308b\u524d\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\\(O(1)\\)\u306e\u7a7a\u9593\u3092\u5360\u6709\u3057\u307e\u3059\u3002\u914d\u5217nums\u3092\u521d\u671f\u5316\u3059\u308b\u969b\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\\(O(n)\\)\u306e\u7a7a\u9593\u3092\u5360\u6709\u3059\u308b\u305f\u3081\u3001\u6700\u60aa\u30b1\u30fc\u30b9\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3059\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin
        def algorithm(n: int):\n    a = 0               # O(1)\n    b = [0] * 10000     # O(1)\n    if n > 10:\n        nums = [0] * n  # O(n)\n
        void algorithm(int n) {\n    int a = 0;               // O(1)\n    vector<int> b(10000);    // O(1)\n    if (n > 10)\n        vector<int> nums(n); // O(n)\n}\n
        void algorithm(int n) {\n    int a = 0;                   // O(1)\n    int[] b = new int[10000];    // O(1)\n    if (n > 10)\n        int[] nums = new int[n]; // O(n)\n}\n
        void Algorithm(int n) {\n    int a = 0;                   // O(1)\n    int[] b = new int[10000];    // O(1)\n    if (n > 10) {\n        int[] nums = new int[n]; // O(n)\n    }\n}\n
        func algorithm(n int) {\n    a := 0                      // O(1)\n    b := make([]int, 10000)     // O(1)\n    var nums []int\n    if n > 10 {\n        nums := make([]int, n)  // O(n)\n    }\n    fmt.Println(a, b, nums)\n}\n
        func algorithm(n: Int) {\n    let a = 0 // O(1)\n    let b = Array(repeating: 0, count: 10000) // O(1)\n    if n > 10 {\n        let nums = Array(repeating: 0, count: n) // O(n)\n    }\n}\n
        function algorithm(n) {\n    const a = 0;                   // O(1)\n    const b = new Array(10000);    // O(1)\n    if (n > 10) {\n        const nums = new Array(n); // O(n)\n    }\n}\n
        function algorithm(n: number): void {\n    const a = 0;                   // O(1)\n    const b = new Array(10000);    // O(1)\n    if (n > 10) {\n        const nums = new Array(n); // O(n)\n    }\n}\n
        void algorithm(int n) {\n  int a = 0;                            // O(1)\n  List<int> b = List.filled(10000, 0);  // O(1)\n  if (n > 10) {\n    List<int> nums = List.filled(n, 0); // O(n)\n  }\n}\n
        fn algorithm(n: i32) {\n    let a = 0;                           // O(1)\n    let b = [0; 10000];                  // O(1)\n    if n > 10 {\n        let nums = vec![0; n as usize];  // O(n)\n    }\n}\n
        void algorithm(int n) {\n    int a = 0;               // O(1)\n    int b[10000];            // O(1)\n    if (n > 10)\n        int nums[n] = {0};   // O(n)\n}\n
        \n

        \u518d\u5e30\u95a2\u6570\u3067\u306f\u3001\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u8003\u616e\u306b\u5165\u308c\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin
        def function() -> int:\n    # \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0\n\ndef loop(n: int):\n    \"\"\"\u30eb\u30fc\u30d7 O(1)\"\"\"\n    for _ in range(n):\n        function()\n\ndef recur(n: int):\n    \"\"\"\u518d\u5e30 O(n)\"\"\"\n    if n == 1:\n        return\n    return recur(n - 1)\n
        int func() {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0;\n}\n/* \u30b5\u30a4\u30af\u30eb O(1) */\nvoid loop(int n) {\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n/* \u518d\u5e30 O(n) */\nvoid recur(int n) {\n    if (n == 1) return;\n    recur(n - 1);\n}\n
        int function() {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0;\n}\n/* \u30b5\u30a4\u30af\u30eb O(1) */\nvoid loop(int n) {\n    for (int i = 0; i < n; i++) {\n        function();\n    }\n}\n/* \u518d\u5e30 O(n) */\nvoid recur(int n) {\n    if (n == 1) return;\n    recur(n - 1);\n}\n
        int Function() {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0;\n}\n/* \u30b5\u30a4\u30af\u30eb O(1) */\nvoid Loop(int n) {\n    for (int i = 0; i < n; i++) {\n        Function();\n    }\n}\n/* \u518d\u5e30 O(n) */\nint Recur(int n) {\n    if (n == 1) return 1;\n    return Recur(n - 1);\n}\n
        func function() int {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0\n}\n\n/* \u30b5\u30a4\u30af\u30eb O(1) */\nfunc loop(n int) {\n    for i := 0; i < n; i++ {\n        function()\n    }\n}\n\n/* \u518d\u5e30 O(n) */\nfunc recur(n int) {\n    if n == 1 {\n        return\n    }\n    recur(n - 1)\n}\n
        @discardableResult\nfunc function() -> Int {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0\n}\n\n/* \u30b5\u30a4\u30af\u30eb O(1) */\nfunc loop(n: Int) {\n    for _ in 0 ..< n {\n        function()\n    }\n}\n\n/* \u518d\u5e30 O(n) */\nfunc recur(n: Int) {\n    if n == 1 {\n        return\n    }\n    recur(n: n - 1)\n}\n
        function constFunc() {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0;\n}\n/* \u30b5\u30a4\u30af\u30eb O(1) */\nfunction loop(n) {\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n/* \u518d\u5e30 O(n) */\nfunction recur(n) {\n    if (n === 1) return;\n    return recur(n - 1);\n}\n
        function constFunc(): number {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0;\n}\n/* \u30b5\u30a4\u30af\u30eb O(1) */\nfunction loop(n: number): void {\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n/* \u518d\u5e30 O(n) */\nfunction recur(n: number): void {\n    if (n === 1) return;\n    return recur(n - 1);\n}\n
        int function() {\n  // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n  return 0;\n}\n/* \u30b5\u30a4\u30af\u30eb O(1) */\nvoid loop(int n) {\n  for (int i = 0; i < n; i++) {\n    function();\n  }\n}\n/* \u518d\u5e30 O(n) */\nvoid recur(int n) {\n  if (n == 1) return;\n  recur(n - 1);\n}\n
        fn function() -> i32 {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0;\n}\n/* \u30b5\u30a4\u30af\u30eb O(1) */\nfn loop(n: i32) {\n    for i in 0..n {\n        function();\n    }\n}\n/* \u518d\u5e30 O(n) */\nvoid recur(n: i32) {\n    if n == 1 {\n        return;\n    }\n    recur(n - 1);\n}\n
        int func() {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0;\n}\n/* \u30b5\u30a4\u30af\u30eb O(1) */\nvoid loop(int n) {\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n/* \u518d\u5e30 O(n) */\nvoid recur(int n) {\n    if (n == 1) return;\n    recur(n - 1);\n}\n
        \n

        loop()\u95a2\u6570\u3068recur()\u95a2\u6570\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\u4e21\u65b9\u3068\u3082\\(O(n)\\)\u3067\u3059\u304c\u3001\u305d\u308c\u3089\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\u7570\u306a\u308a\u307e\u3059\u3002

        • loop()\u95a2\u6570\u306f\u30eb\u30fc\u30d7\u5185\u3067function()\u3092\\(n\\)\u56de\u547c\u3073\u51fa\u3057\u3001\u5404\u53cd\u5fa9\u306efunction()\u306f\u8fd4\u3063\u3066\u305d\u306e\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u89e3\u653e\u3059\u308b\u305f\u3081\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u306e\u307e\u307e\u3067\u3059\u3002
        • \u518d\u5e30\u95a2\u6570recur()\u306f\u5b9f\u884c\u4e2d\u306b\\(n\\)\u500b\u306e\u672a\u8fd4\u5374\u306erecur()\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u540c\u6642\u306b\u5b58\u5728\u3059\u308b\u305f\u3081\u3001\\(O(n)\\)\u306e\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u5360\u6709\u3057\u307e\u3059\u3002
        "},{"location":"chapter_computational_complexity/space_complexity/#243","title":"2.4.3 \u00a0 \u4e00\u822c\u7684\u306a\u7a2e\u985e","text":"

        \u5165\u529b\u30c7\u30fc\u30bf\u306e\u30b5\u30a4\u30ba\u3092\\(n\\)\u3068\u3059\u308b\u3068\u3001\u4e0b\u56f3\u306f\u4e00\u822c\u7684\u306a\u7a7a\u9593\u8a08\u7b97\u91cf\u306e\u7a2e\u985e\u3092\u793a\u3057\u3066\u3044\u307e\u3059\uff08\u4f4e\u3044\u3082\u306e\u304b\u3089\u9ad8\u3044\u3082\u306e\u3078\u3068\u4e26\u3079\u3089\u308c\u3066\u3044\u307e\u3059\uff09\u3002

        \\[ \\begin{aligned} & O(1) < O(\\log n) < O(n) < O(n^2) < O(2^n) \\newline & \\text{\u5b9a\u6570} < \\text{\u5bfe\u6570} < \\text{\u7dda\u5f62} < \\text{\u4e8c\u6b21} < \\text{\u6307\u6570} \\end{aligned} \\]

        \u56f3 2-16 \u00a0 Common types of space complexity

        "},{"location":"chapter_computational_complexity/space_complexity/#1-o1","title":"1. \u00a0 \u5b9a\u6570\u30aa\u30fc\u30c0\u30fc \\(O(1)\\)","text":"

        \u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\u306f\u3001\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\\(n\\)\u3068\u306f\u7121\u95a2\u4fc2\u306a\u5b9a\u6570\u3001\u5909\u6570\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u4e00\u822c\u7684\u3067\u3059\u3002

        \u30eb\u30fc\u30d7\u3067\u5909\u6570\u3092\u521d\u671f\u5316\u3057\u305f\u308a\u95a2\u6570\u3092\u547c\u3073\u51fa\u3057\u305f\u308a\u3059\u308b\u305f\u3081\u306b\u5360\u6709\u3055\u308c\u308b\u30e1\u30e2\u30ea\u306f\u3001\u6b21\u306e\u30b5\u30a4\u30af\u30eb\u306b\u5165\u308b\u969b\u306b\u89e3\u653e\u3055\u308c\u3001\u7a7a\u9593\u4e0a\u3067\u7d2f\u7a4d\u3055\u308c\u306a\u3044\u305f\u3081\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u306e\u307e\u307e\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def function() -> int:\n    \"\"\"\u95a2\u6570\"\"\"\n    # \u4f55\u3089\u304b\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0\n\ndef constant(n: int):\n    \"\"\"\u5b9a\u6570\u8907\u96d1\u5ea6\"\"\"\n    # \u5b9a\u6570\u3001\u5909\u6570\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f O(1) \u306e\u30b9\u30da\u30fc\u30b9\u3092\u5360\u6709\n    a = 0\n    nums = [0] * 10000\n    node = ListNode(0)\n    # \u30eb\u30fc\u30d7\u5185\u306e\u5909\u6570\u306f O(1) \u306e\u30b9\u30da\u30fc\u30b9\u3092\u5360\u6709\n    for _ in range(n):\n        c = 0\n    # \u30eb\u30fc\u30d7\u5185\u306e\u95a2\u6570\u306f O(1) \u306e\u30b9\u30da\u30fc\u30b9\u3092\u5360\u6709\n    for _ in range(n):\n        function()\n
        space_complexity.cpp
        /* \u95a2\u6570 */\nint func() {\n    // \u4f55\u3089\u304b\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0;\n}\n\n/* \u5b9a\u6570\u8a08\u7b97\u91cf */\nvoid constant(int n) {\n    // \u5b9a\u6570\u3001\u5909\u6570\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f O(1) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    const int a = 0;\n    int b = 0;\n    vector<int> nums(10000);\n    ListNode node(0);\n    // \u30eb\u30fc\u30d7\u5185\u306e\u5909\u6570\u306f O(1) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // \u30eb\u30fc\u30d7\u5185\u306e\u95a2\u6570\u306f O(1) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n
        space_complexity.java
        /* \u95a2\u6570 */\nint function() {\n    // \u4f55\u3089\u304b\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0;\n}\n\n/* \u5b9a\u6570\u8a08\u7b97\u91cf */\nvoid constant(int n) {\n    // \u5b9a\u6570\u3001\u5909\u6570\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f O(1) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    final int a = 0;\n    int b = 0;\n    int[] nums = new int[10000];\n    ListNode node = new ListNode(0);\n    // \u30eb\u30fc\u30d7\u5185\u306e\u5909\u6570\u306f O(1) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // \u30eb\u30fc\u30d7\u5185\u306e\u95a2\u6570\u306f O(1) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    for (int i = 0; i < n; i++) {\n        function();\n    }\n}\n
        space_complexity.cs
        [class]{space_complexity}-[func]{Function}\n\n[class]{space_complexity}-[func]{Constant}\n
        space_complexity.go
        [class]{}-[func]{function}\n\n[class]{}-[func]{spaceConstant}\n
        space_complexity.swift
        [class]{}-[func]{function}\n\n[class]{}-[func]{constant}\n
        space_complexity.js
        [class]{}-[func]{constFunc}\n\n[class]{}-[func]{constant}\n
        space_complexity.ts
        [class]{}-[func]{constFunc}\n\n[class]{}-[func]{constant}\n
        space_complexity.dart
        [class]{}-[func]{function}\n\n[class]{}-[func]{constant}\n
        space_complexity.rs
        [class]{}-[func]{function}\n\n[class]{}-[func]{constant}\n
        space_complexity.c
        [class]{}-[func]{func}\n\n[class]{}-[func]{constant}\n
        space_complexity.kt
        [class]{}-[func]{function}\n\n[class]{}-[func]{constant}\n
        space_complexity.rb
        [class]{}-[func]{function}\n\n[class]{}-[func]{constant}\n
        "},{"location":"chapter_computational_complexity/space_complexity/#2-on","title":"2. \u00a0 \u7dda\u5f62\u30aa\u30fc\u30c0\u30fc \\(O(n)\\)","text":"

        \u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\u306f\u914d\u5217\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u30b9\u30bf\u30c3\u30af\u3001\u30ad\u30e5\u30fc\u306a\u3069\u3067\u4e00\u822c\u7684\u3067\u3001\u8981\u7d20\u6570\u306f\\(n\\)\u306b\u6bd4\u4f8b\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def linear(n: int):\n    \"\"\"\u7dda\u5f62\u8907\u96d1\u5ea6\"\"\"\n    # \u9577\u3055 n \u306e\u30ea\u30b9\u30c8\u306f O(n) \u306e\u30b9\u30da\u30fc\u30b9\u3092\u5360\u6709\n    nums = [0] * n\n    # \u9577\u3055 n \u306e\u30cf\u30c3\u30b7\u30e5\u30de\u30c3\u30d7\u306f O(n) \u306e\u30b9\u30da\u30fc\u30b9\u3092\u5360\u6709\n    hmap = dict[int, str]()\n    for i in range(n):\n        hmap[i] = str(i)\n
        space_complexity.cpp
        /* \u7dda\u5f62\u8a08\u7b97\u91cf */\nvoid linear(int n) {\n    // \u9577\u3055 n \u306e\u914d\u5217\u306f O(n) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    vector<int> nums(n);\n    // \u9577\u3055 n \u306e\u30ea\u30b9\u30c8\u306f O(n) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    vector<ListNode> nodes;\n    for (int i = 0; i < n; i++) {\n        nodes.push_back(ListNode(i));\n    }\n    // \u9577\u3055 n \u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306f O(n) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    unordered_map<int, string> map;\n    for (int i = 0; i < n; i++) {\n        map[i] = to_string(i);\n    }\n}\n
        space_complexity.java
        /* \u7dda\u5f62\u8a08\u7b97\u91cf */\nvoid linear(int n) {\n    // \u9577\u3055 n \u306e\u914d\u5217\u306f O(n) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    int[] nums = new int[n];\n    // \u9577\u3055 n \u306e\u30ea\u30b9\u30c8\u306f O(n) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    List<ListNode> nodes = new ArrayList<>();\n    for (int i = 0; i < n; i++) {\n        nodes.add(new ListNode(i));\n    }\n    // \u9577\u3055 n \u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306f O(n) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    Map<Integer, String> map = new HashMap<>();\n    for (int i = 0; i < n; i++) {\n        map.put(i, String.valueOf(i));\n    }\n}\n
        space_complexity.cs
        [class]{space_complexity}-[func]{Linear}\n
        space_complexity.go
        [class]{}-[func]{spaceLinear}\n
        space_complexity.swift
        [class]{}-[func]{linear}\n
        space_complexity.js
        [class]{}-[func]{linear}\n
        space_complexity.ts
        [class]{}-[func]{linear}\n
        space_complexity.dart
        [class]{}-[func]{linear}\n
        space_complexity.rs
        [class]{}-[func]{linear}\n
        space_complexity.c
        [class]{HashTable}-[func]{}\n\n[class]{}-[func]{linear}\n
        space_complexity.kt
        [class]{}-[func]{linear}\n
        space_complexity.rb
        [class]{}-[func]{linear}\n

        \u4e0b\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u3001\u3053\u306e\u95a2\u6570\u306e\u518d\u5e30\u6df1\u5ea6\u306f\\(n\\)\u3067\u3001\\(n\\)\u500b\u306e\u672a\u8fd4\u5374\u306elinear_recur()\u95a2\u6570\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u3042\u308a\u3001\\(O(n)\\)\u30b5\u30a4\u30ba\u306e\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def linear_recur(n: int):\n    \"\"\"\u7dda\u5f62\u8907\u96d1\u5ea6\uff08\u518d\u5e30\u5b9f\u88c5\uff09\"\"\"\n    print(\"\u518d\u5e30 n =\", n)\n    if n == 1:\n        return\n    linear_recur(n - 1)\n
        space_complexity.cpp
        /* \u7dda\u5f62\u8a08\u7b97\u91cf\uff08\u518d\u5e30\u5b9f\u88c5\uff09 */\nvoid linearRecur(int n) {\n    cout << \"\u518d\u5e30 n = \" << n << endl;\n    if (n == 1)\n        return;\n    linearRecur(n - 1);\n}\n
        space_complexity.java
        /* \u7dda\u5f62\u8a08\u7b97\u91cf\uff08\u518d\u5e30\u5b9f\u88c5\uff09 */\nvoid linearRecur(int n) {\n    System.out.println(\"\u518d\u5e30 n = \" + n);\n    if (n == 1)\n        return;\n    linearRecur(n - 1);\n}\n
        space_complexity.cs
        [class]{space_complexity}-[func]{LinearRecur}\n
        space_complexity.go
        [class]{}-[func]{spaceLinearRecur}\n
        space_complexity.swift
        [class]{}-[func]{linearRecur}\n
        space_complexity.js
        [class]{}-[func]{linearRecur}\n
        space_complexity.ts
        [class]{}-[func]{linearRecur}\n
        space_complexity.dart
        [class]{}-[func]{linearRecur}\n
        space_complexity.rs
        [class]{}-[func]{linear_recur}\n
        space_complexity.c
        [class]{}-[func]{linearRecur}\n
        space_complexity.kt
        [class]{}-[func]{linearRecur}\n
        space_complexity.rb
        [class]{}-[func]{linear_recur}\n

        \u56f3 2-17 \u00a0 Recursive function generating linear order space complexity

        "},{"location":"chapter_computational_complexity/space_complexity/#3-on2","title":"3. \u00a0 \u4e8c\u6b21\u30aa\u30fc\u30c0\u30fc \\(O(n^2)\\)","text":"

        \u4e8c\u6b21\u30aa\u30fc\u30c0\u30fc\u306f\u884c\u5217\u3084\u30b0\u30e9\u30d5\u3067\u4e00\u822c\u7684\u3067\u3001\u8981\u7d20\u6570\u306f\\(n\\)\u306e\u4e8c\u4e57\u306b\u6bd4\u4f8b\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def quadratic(n: int):\n    \"\"\"\u5e73\u65b9\u8907\u96d1\u5ea6\"\"\"\n    # \u4e8c\u6b21\u5143\u30ea\u30b9\u30c8\u306f O(n^2) \u306e\u30b9\u30da\u30fc\u30b9\u3092\u5360\u6709\n    num_matrix = [[0] * n for _ in range(n)]\n
        space_complexity.cpp
        /* \u4e8c\u6b21\u8a08\u7b97\u91cf */\nvoid quadratic(int n) {\n    // \u4e8c\u6b21\u5143\u30ea\u30b9\u30c8\u306f O(n^2) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    vector<vector<int>> numMatrix;\n    for (int i = 0; i < n; i++) {\n        vector<int> tmp;\n        for (int j = 0; j < n; j++) {\n            tmp.push_back(0);\n        }\n        numMatrix.push_back(tmp);\n    }\n}\n
        space_complexity.java
        /* \u4e8c\u6b21\u8a08\u7b97\u91cf */\nvoid quadratic(int n) {\n    // \u884c\u5217\u306f O(n^2) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    int[][] numMatrix = new int[n][n];\n    // \u4e8c\u6b21\u5143\u30ea\u30b9\u30c8\u306f O(n^2) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    List<List<Integer>> numList = new ArrayList<>();\n    for (int i = 0; i < n; i++) {\n        List<Integer> tmp = new ArrayList<>();\n        for (int j = 0; j < n; j++) {\n            tmp.add(0);\n        }\n        numList.add(tmp);\n    }\n}\n
        space_complexity.cs
        [class]{space_complexity}-[func]{Quadratic}\n
        space_complexity.go
        [class]{}-[func]{spaceQuadratic}\n
        space_complexity.swift
        [class]{}-[func]{quadratic}\n
        space_complexity.js
        [class]{}-[func]{quadratic}\n
        space_complexity.ts
        [class]{}-[func]{quadratic}\n
        space_complexity.dart
        [class]{}-[func]{quadratic}\n
        space_complexity.rs
        [class]{}-[func]{quadratic}\n
        space_complexity.c
        [class]{}-[func]{quadratic}\n
        space_complexity.kt
        [class]{}-[func]{quadratic}\n
        space_complexity.rb
        [class]{}-[func]{quadratic}\n

        \u4e0b\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u3001\u3053\u306e\u95a2\u6570\u306e\u518d\u5e30\u6df1\u5ea6\u306f\\(n\\)\u3067\u3001\u5404\u518d\u5e30\u547c\u3073\u51fa\u3057\u3067\u9577\u3055\\(n\\)\u3001\\(n-1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\)\u306e\u914d\u5217\u304c\u521d\u671f\u5316\u3055\u308c\u3001\u5e73\u5747\\(n/2\\)\u3068\u306a\u308a\u3001\u5168\u4f53\u3068\u3057\u3066\\(O(n^2)\\)\u306e\u7a7a\u9593\u3092\u5360\u6709\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def quadratic_recur(n: int) -> int:\n    \"\"\"\u5e73\u65b9\u8907\u96d1\u5ea6\uff08\u518d\u5e30\u5b9f\u88c5\uff09\"\"\"\n    if n <= 0:\n        return 0\n    nums = [0] * n\n    print(f\"\u518d\u5e30 n = {n} \u306e\u4e2d\u3067\u914d\u5217\u306e\u9577\u3055 = {len(nums)}\")\n    return quadratic_recur(n - 1)\n
        space_complexity.cpp
        /* \u4e8c\u6b21\u8a08\u7b97\u91cf\uff08\u518d\u5e30\u5b9f\u88c5\uff09 */\nint quadraticRecur(int n) {\n    if (n <= 0)\n        return 0;\n    vector<int> nums(n);\n    cout << \"\u518d\u5e30 n = \" << n << \", nums \u306e\u9577\u3055 = \" << nums.size() << endl;\n    return quadraticRecur(n - 1);\n}\n
        space_complexity.java
        /* \u4e8c\u6b21\u8a08\u7b97\u91cf\uff08\u518d\u5e30\u5b9f\u88c5\uff09 */\nint quadraticRecur(int n) {\n    if (n <= 0)\n        return 0;\n    // \u914d\u5217 nums \u306e\u9577\u3055 = n, n-1, ..., 2, 1\n    int[] nums = new int[n];\n    System.out.println(\"\u518d\u5e30 n = \" + n + \" \u306e nums \u306e\u9577\u3055 = \" + nums.length);\n    return quadraticRecur(n - 1);\n}\n
        space_complexity.cs
        [class]{space_complexity}-[func]{QuadraticRecur}\n
        space_complexity.go
        [class]{}-[func]{spaceQuadraticRecur}\n
        space_complexity.swift
        [class]{}-[func]{quadraticRecur}\n
        space_complexity.js
        [class]{}-[func]{quadraticRecur}\n
        space_complexity.ts
        [class]{}-[func]{quadraticRecur}\n
        space_complexity.dart
        [class]{}-[func]{quadraticRecur}\n
        space_complexity.rs
        [class]{}-[func]{quadratic_recur}\n
        space_complexity.c
        [class]{}-[func]{quadraticRecur}\n
        space_complexity.kt
        [class]{}-[func]{quadraticRecur}\n
        space_complexity.rb
        [class]{}-[func]{quadratic_recur}\n

        \u56f3 2-18 \u00a0 Recursive function generating quadratic order space complexity

        "},{"location":"chapter_computational_complexity/space_complexity/#4-o2n","title":"4. \u00a0 \u6307\u6570\u30aa\u30fc\u30c0\u30fc \\(O(2^n)\\)","text":"

        \u6307\u6570\u30aa\u30fc\u30c0\u30fc\u306f\u4e8c\u5206\u6728\u3067\u4e00\u822c\u7684\u3067\u3059\u3002\u4e0b\u56f3\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\\(n\\)\u30ec\u30d9\u30eb\u306e\u300c\u5b8c\u5168\u4e8c\u5206\u6728\u300d\u306f\\(2^n - 1\\)\u500b\u306e\u30ce\u30fc\u30c9\u3092\u6301\u3061\u3001\\(O(2^n)\\)\u306e\u7a7a\u9593\u3092\u5360\u6709\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def build_tree(n: int) -> TreeNode | None:\n    \"\"\"\u6307\u6570\u8907\u96d1\u5ea6\uff08\u5b8c\u5168\u4e8c\u5206\u6728\u306e\u69cb\u7bc9\uff09\"\"\"\n    if n == 0:\n        return None\n    root = TreeNode(0)\n    root.left = build_tree(n - 1)\n    root.right = build_tree(n - 1)\n    return root\n
        space_complexity.cpp
        /* \u6307\u6570\u8a08\u7b97\u91cf\uff08\u5b8c\u5168\u4e8c\u5206\u6728\u306e\u69cb\u7bc9\uff09 */\nTreeNode *buildTree(int n) {\n    if (n == 0)\n        return nullptr;\n    TreeNode *root = new TreeNode(0);\n    root->left = buildTree(n - 1);\n    root->right = buildTree(n - 1);\n    return root;\n}\n
        space_complexity.java
        /* \u6307\u6570\u8a08\u7b97\u91cf\uff08\u5b8c\u5168\u4e8c\u5206\u6728\u306e\u69cb\u7bc9\uff09 */\nTreeNode buildTree(int n) {\n    if (n == 0)\n        return null;\n    TreeNode root = new TreeNode(0);\n    root.left = buildTree(n - 1);\n    root.right = buildTree(n - 1);\n    return root;\n}\n
        space_complexity.cs
        [class]{space_complexity}-[func]{BuildTree}\n
        space_complexity.go
        [class]{}-[func]{buildTree}\n
        space_complexity.swift
        [class]{}-[func]{buildTree}\n
        space_complexity.js
        [class]{}-[func]{buildTree}\n
        space_complexity.ts
        [class]{}-[func]{buildTree}\n
        space_complexity.dart
        [class]{}-[func]{buildTree}\n
        space_complexity.rs
        [class]{}-[func]{build_tree}\n
        space_complexity.c
        [class]{}-[func]{buildTree}\n
        space_complexity.kt
        [class]{}-[func]{buildTree}\n
        space_complexity.rb
        [class]{}-[func]{build_tree}\n

        \u56f3 2-19 \u00a0 Full binary tree generating exponential order space complexity

        "},{"location":"chapter_computational_complexity/space_complexity/#5-olog-n","title":"5. \u00a0 \u5bfe\u6570\u30aa\u30fc\u30c0\u30fc \\(O(\\log n)\\)","text":"

        \u5bfe\u6570\u30aa\u30fc\u30c0\u30fc\u306f\u5206\u5272\u7d71\u6cbb\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u4e00\u822c\u7684\u3067\u3059\u3002\u4f8b\u3048\u3070\u3001\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u3067\u306f\u3001\u9577\u3055\\(n\\)\u306e\u914d\u5217\u304c\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u518d\u5e30\u7684\u306b\u534a\u5206\u306b\u5206\u5272\u3055\u308c\u3001\u9ad8\u3055\\(\\log n\\)\u306e\u518d\u5e30\u6728\u3092\u5f62\u6210\u3057\u3001\\(O(\\log n)\\)\u306e\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        \u5225\u306e\u4f8b\u306f\u3001\u6570\u5024\u3092\u6587\u5b57\u5217\u306b\u5909\u63db\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u6b63\u306e\u6574\u6570\\(n\\)\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u305d\u306e\u6841\u6570\u306f\\(\\log_{10} n + 1\\)\u3067\u3001\u6587\u5b57\u5217\u306e\u9577\u3055\u306b\u5bfe\u5fdc\u3059\u308b\u305f\u3081\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(\\log_{10} n + 1) = O(\\log n)\\)\u3067\u3059\u3002

        "},{"location":"chapter_computational_complexity/space_complexity/#244","title":"2.4.4 \u00a0 \u6642\u9593\u3068\u7a7a\u9593\u306e\u30d0\u30e9\u30f3\u30b9","text":"

        \u7406\u60f3\u7684\u306b\u306f\u3001\u6642\u9593\u8a08\u7b97\u91cf\u3068\u7a7a\u9593\u8a08\u7b97\u91cf\u306e\u4e21\u65b9\u304c\u6700\u9069\u3067\u3042\u308b\u3053\u3068\u3092\u76ee\u6307\u3057\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u5b9f\u969b\u306b\u306f\u4e21\u65b9\u3092\u540c\u6642\u306b\u6700\u9069\u5316\u3059\u308b\u3053\u3068\u306f\u3057\u3070\u3057\u3070\u56f0\u96e3\u3067\u3059\u3002

        \u6642\u9593\u8a08\u7b97\u91cf\u3092\u4e0b\u3052\u308b\u3053\u3068\u306f\u901a\u5e38\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306e\u5897\u52a0\u3092\u4ee3\u511f\u3068\u3057\u3001\u305d\u306e\u9006\u3082\u540c\u69d8\u3067\u3059\u3002\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u901f\u5ea6\u3092\u5411\u4e0a\u3055\u305b\u308b\u305f\u3081\u306b\u30e1\u30e2\u30ea\u7a7a\u9593\u3092\u72a0\u7272\u306b\u3059\u308b\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\u300c\u6642\u7a7a\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u300d\u3068\u3057\u3066\u77e5\u3089\u308c\u3001\u305d\u306e\u9006\u306f\u300c\u7a7a\u6642\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u300d\u3068\u3057\u3066\u77e5\u3089\u308c\u3066\u3044\u307e\u3059\u3002

        \u9078\u629e\u306f\u3001\u3069\u3061\u3089\u306e\u5074\u9762\u3092\u3088\u308a\u91cd\u8996\u3059\u308b\u304b\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u6642\u9593\u306f\u7a7a\u9593\u3088\u308a\u3082\u8cb4\u91cd\u3067\u3042\u308b\u305f\u3081\u3001\u300c\u6642\u7a7a\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u300d\u304c\u3088\u308a\u4e00\u822c\u7684\u306a\u6226\u7565\u3067\u3059\u3002\u3082\u3061\u308d\u3093\u3001\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u6271\u3046\u969b\u306f\u7a7a\u9593\u8a08\u7b97\u91cf\u3092\u5236\u5fa1\u3059\u308b\u3053\u3068\u3082\u975e\u5e38\u306b\u91cd\u8981\u3067\u3059\u3002

        "},{"location":"chapter_computational_complexity/summary/","title":"2.5 \u00a0 \u307e\u3068\u3081","text":""},{"location":"chapter_computational_complexity/summary/#1","title":"1. \u00a0 \u91cd\u8981\u306a\u30ec\u30d3\u30e5\u30fc","text":"

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u52b9\u7387\u8a55\u4fa1

        • \u6642\u9593\u52b9\u7387\u3068\u7a7a\u9593\u52b9\u7387\u306f\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u512a\u52a3\u3092\u8a55\u4fa1\u3059\u308b2\u3064\u306e\u4e3b\u8981\u306a\u57fa\u6e96\u3067\u3059\u3002
        • \u5b9f\u969b\u306e\u30c6\u30b9\u30c8\u306b\u3088\u3063\u3066\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u8a55\u4fa1\u3067\u304d\u307e\u3059\u304c\u3001\u30c6\u30b9\u30c8\u74b0\u5883\u306e\u5f71\u97ff\u3092\u6392\u9664\u3059\u308b\u3053\u3068\u306f\u56f0\u96e3\u3067\u3001\u5927\u91cf\u306e\u8a08\u7b97\u30ea\u30bd\u30fc\u30b9\u3092\u6d88\u8cbb\u3057\u307e\u3059\u3002
        • \u8907\u96d1\u5ea6\u5206\u6790\u306f\u5b9f\u969b\u306e\u30c6\u30b9\u30c8\u306e\u6b20\u70b9\u3092\u514b\u670d\u3067\u304d\u307e\u3059\u3002\u305d\u306e\u7d50\u679c\u306f\u3059\u3079\u3066\u306e\u52d5\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u306b\u9069\u7528\u3067\u304d\u3001\u7570\u306a\u308b\u30c7\u30fc\u30bf\u30b9\u30b1\u30fc\u30eb\u3067\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u660e\u3089\u304b\u306b\u3067\u304d\u307e\u3059\u3002

        \u6642\u9593\u8a08\u7b97\u91cf

        • \u6642\u9593\u8a08\u7b97\u91cf\u306f\u3001\u30c7\u30fc\u30bf\u91cf\u306e\u5897\u52a0\u306b\u4f34\u3046\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u6642\u9593\u306e\u50be\u5411\u3092\u6e2c\u5b9a\u3057\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u52b9\u679c\u7684\u306b\u8a55\u4fa1\u3057\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u5165\u529b\u30c7\u30fc\u30bf\u91cf\u304c\u5c11\u306a\u3044\u5834\u5408\u3084\u6642\u9593\u8a08\u7b97\u91cf\u304c\u540c\u3058\u5834\u5408\u306a\u3069\u3001\u7279\u5b9a\u306e\u30b1\u30fc\u30b9\u3067\u306f\u5931\u6557\u3059\u308b\u3053\u3068\u304c\u3042\u308a\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u6b63\u78ba\u306b\u6bd4\u8f03\u3059\u308b\u3053\u3068\u304c\u56f0\u96e3\u306b\u306a\u308a\u307e\u3059\u3002
        • \u6700\u60aa\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\u306f\u30d3\u30c3\u30b0\\(O\\)\u8a18\u6cd5\u3092\u4f7f\u7528\u3057\u3066\u8868\u8a18\u3055\u308c\u3001\u6f38\u8fd1\u4e0a\u9650\u3092\u8868\u3057\u3001\\(n\\)\u304c\u7121\u9650\u5927\u306b\u8fd1\u3065\u304f\u306b\u3064\u308c\u3066\u306e\u64cd\u4f5c\u6570\\(T(n)\\)\u306e\u5897\u52a0\u30ec\u30d9\u30eb\u3092\u53cd\u6620\u3057\u307e\u3059\u3002
        • \u6642\u9593\u8a08\u7b97\u91cf\u306e\u8a08\u7b97\u306b\u306f2\u3064\u306e\u30b9\u30c6\u30c3\u30d7\u304c\u542b\u307e\u308c\u307e\u3059\uff1a\u307e\u305a\u64cd\u4f5c\u6570\u3092\u30ab\u30a6\u30f3\u30c8\u3057\u3001\u6b21\u306b\u6f38\u8fd1\u4e0a\u9650\u3092\u6c7a\u5b9a\u3057\u307e\u3059\u3002
        • \u4e00\u822c\u7684\u306a\u6642\u9593\u8a08\u7b97\u91cf\u306f\u3001\u4f4e\u3044\u3082\u306e\u304b\u3089\u9ad8\u3044\u3082\u306e\u3078\u3068\u4e26\u3079\u308b\u3068\u3001\\(O(1)\\)\u3001\\(O(\\log n)\\)\u3001\\(O(n)\\)\u3001\\(O(n \\log n)\\)\u3001\\(O(n^2)\\)\u3001\\(O(2^n)\\)\u3001\\(O(n!)\\)\u306a\u3069\u304c\u542b\u307e\u308c\u307e\u3059\u3002
        • \u4e00\u90e8\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\u56fa\u5b9a\u3055\u308c\u3066\u304a\u3089\u305a\u3001\u5165\u529b\u30c7\u30fc\u30bf\u306e\u5206\u5e03\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002\u6642\u9593\u8a08\u7b97\u91cf\u306f\u6700\u60aa\u3001\u6700\u826f\u3001\u5e73\u5747\u306e\u30b1\u30fc\u30b9\u306b\u5206\u3051\u3089\u308c\u307e\u3059\u3002\u6700\u826f\u30b1\u30fc\u30b9\u306f\u3001\u5165\u529b\u30c7\u30fc\u30bf\u304c\u6700\u826f\u30b1\u30fc\u30b9\u3092\u9054\u6210\u3059\u308b\u305f\u3081\u306b\u53b3\u683c\u306a\u6761\u4ef6\u3092\u6e80\u305f\u3059\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u307b\u3068\u3093\u3069\u4f7f\u7528\u3055\u308c\u307e\u305b\u3093\u3002
        • \u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf\u306f\u3001\u30e9\u30f3\u30c0\u30e0\u30c7\u30fc\u30bf\u5165\u529b\u4e0b\u3067\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u53cd\u6620\u3057\u3001\u5b9f\u969b\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6027\u80fd\u306b\u5bc6\u63a5\u306b\u985e\u4f3c\u3057\u3066\u3044\u307e\u3059\u3002\u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf\u306e\u8a08\u7b97\u306b\u306f\u3001\u5165\u529b\u30c7\u30fc\u30bf\u306e\u5206\u5e03\u3068\u305d\u306e\u5f8c\u306e\u6570\u5b66\u7684\u671f\u5f85\u5024\u3092\u8003\u616e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u7a7a\u9593\u8a08\u7b97\u91cf

        • \u7a7a\u9593\u8a08\u7b97\u91cf\u306f\u3001\u6642\u9593\u8a08\u7b97\u91cf\u3068\u540c\u69d8\u306b\u3001\u30c7\u30fc\u30bf\u91cf\u306e\u5897\u52a0\u306b\u4f34\u3046\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u5360\u6709\u3059\u308b\u30e1\u30e2\u30ea\u7a7a\u9593\u306e\u50be\u5411\u3092\u6e2c\u5b9a\u3057\u307e\u3059\u3002
        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u4e2d\u306b\u4f7f\u7528\u3055\u308c\u308b\u95a2\u9023\u30e1\u30e2\u30ea\u7a7a\u9593\u306f\u3001\u5165\u529b\u7a7a\u9593\u3001\u4e00\u6642\u7a7a\u9593\u3001\u51fa\u529b\u7a7a\u9593\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u4e00\u822c\u7684\u306b\u3001\u5165\u529b\u7a7a\u9593\u306f\u7a7a\u9593\u8a08\u7b97\u91cf\u306e\u8a08\u7b97\u306b\u542b\u307e\u308c\u307e\u305b\u3093\u3002\u4e00\u6642\u7a7a\u9593\u306f\u4e00\u6642\u30c7\u30fc\u30bf\u3001\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3001\u547d\u4ee4\u7a7a\u9593\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u306f\u901a\u5e38\u3001\u518d\u5e30\u95a2\u6570\u3067\u306e\u307f\u7a7a\u9593\u8a08\u7b97\u91cf\u306b\u5f71\u97ff\u3057\u307e\u3059\u3002
        • \u901a\u5e38\u306f\u6700\u60aa\u30b1\u30fc\u30b9\u7a7a\u9593\u8a08\u7b97\u91cf\u306e\u307f\u306b\u7126\u70b9\u3092\u5f53\u3066\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u6700\u60aa\u306e\u5165\u529b\u30c7\u30fc\u30bf\u3068\u64cd\u4f5c\u306e\u6700\u60aa\u306e\u77ac\u9593\u3067\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u3092\u8a08\u7b97\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002
        • \u4e00\u822c\u7684\u306a\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\u3001\u4f4e\u3044\u3082\u306e\u304b\u3089\u9ad8\u3044\u3082\u306e\u3078\u3068\u4e26\u3079\u308b\u3068\u3001\\(O(1)\\)\u3001\\(O(\\log n)\\)\u3001\\(O(n)\\)\u3001\\(O(n^2)\\)\u3001\\(O(2^n)\\)\u306a\u3069\u304c\u542b\u307e\u308c\u307e\u3059\u3002
        "},{"location":"chapter_computational_complexity/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q: \u672b\u5c3e\u518d\u5e30\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u3067\u3059\u304b\uff1f

        \u7406\u8ad6\u7684\u306b\u306f\u3001\u672b\u5c3e\u518d\u5e30\u95a2\u6570\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u306b\u6700\u9069\u5316\u3067\u304d\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u307b\u3068\u3093\u3069\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\uff08Java\u3001Python\u3001C++\u3001Go\u3001C#\u306a\u3069\uff09\u306f\u672b\u5c3e\u518d\u5e30\u306e\u81ea\u52d5\u6700\u9069\u5316\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u306a\u3044\u305f\u3081\u3001\u4e00\u822c\u7684\u306b\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3068\u8003\u3048\u3089\u308c\u3066\u3044\u307e\u3059\u3002

        Q: \u300c\u95a2\u6570\u300d\u3068\u300c\u30e1\u30bd\u30c3\u30c9\u300d\u3068\u3044\u3046\u7528\u8a9e\u306e\u9055\u3044\u306f\u4f55\u3067\u3059\u304b\uff1f

        \u95a2\u6570\u306f\u72ec\u7acb\u3057\u3066\u5b9f\u884c\u3067\u304d\u3001\u3059\u3079\u3066\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u660e\u793a\u7684\u306b\u6e21\u3055\u308c\u307e\u3059\u3002\u30e1\u30bd\u30c3\u30c9\u306f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u95a2\u9023\u4ed8\u3051\u3089\u308c\u3001\u305d\u308c\u3092\u547c\u3073\u51fa\u3059\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u6697\u9ed9\u7684\u306b\u6e21\u3055\u308c\u3001\u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5185\u306b\u542b\u307e\u308c\u308b\u30c7\u30fc\u30bf\u3092\u64cd\u4f5c\u3067\u304d\u307e\u3059\u3002

        \u4e00\u822c\u7684\u306a\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u304b\u3089\u306e\u4f8b\u3092\u3044\u304f\u3064\u304b\u793a\u3057\u307e\u3059\uff1a

        • C\u306f\u624b\u7d9a\u304d\u578b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u306e\u6982\u5ff5\u304c\u306a\u3044\u305f\u3081\u3001\u95a2\u6570\u306e\u307f\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u69cb\u9020\u4f53\uff08struct\uff09\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u3067\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\u3067\u304d\u3001\u3053\u308c\u3089\u306e\u69cb\u9020\u4f53\u306b\u95a2\u9023\u4ed8\u3051\u3089\u308c\u305f\u95a2\u6570\u306f\u4ed6\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u30e1\u30bd\u30c3\u30c9\u3068\u540c\u7b49\u3067\u3059\u3002
        • Java\u3068C#\u306f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u3001\u30b3\u30fc\u30c9\u30d6\u30ed\u30c3\u30af\uff08\u30e1\u30bd\u30c3\u30c9\uff09\u306f\u901a\u5e38\u30af\u30e9\u30b9\u306e\u4e00\u90e8\u3067\u3059\u3002\u9759\u7684\u30e1\u30bd\u30c3\u30c9\u306f\u30af\u30e9\u30b9\u306b\u30d0\u30a4\u30f3\u30c9\u3055\u308c\u3001\u7279\u5b9a\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5909\u6570\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u306a\u3044\u305f\u3081\u3001\u95a2\u6570\u306e\u3088\u3046\u306b\u52d5\u4f5c\u3057\u307e\u3059\u3002
        • C++\u3068Python\u306f\u624b\u7d9a\u304d\u578b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\uff08\u95a2\u6570\uff09\u3068\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\uff08\u30e1\u30bd\u30c3\u30c9\uff09\u306e\u4e21\u65b9\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u307e\u3059\u3002

        Q: \u300c\u7a7a\u9593\u8a08\u7b97\u91cf\u306e\u4e00\u822c\u7684\u306a\u7a2e\u985e\u300d\u306e\u56f3\u306f\u3001\u5360\u6709\u7a7a\u9593\u306e\u7d76\u5bfe\u30b5\u30a4\u30ba\u3092\u53cd\u6620\u3057\u3066\u3044\u307e\u3059\u304b\uff1f

        \u3044\u3044\u3048\u3001\u56f3\u306f\u7a7a\u9593\u8a08\u7b97\u91cf\u3092\u793a\u3057\u3066\u304a\u308a\u3001\u3053\u308c\u306f\u5897\u52a0\u50be\u5411\u3092\u53cd\u6620\u3059\u308b\u3082\u306e\u3067\u3042\u308a\u3001\u5360\u6709\u7a7a\u9593\u306e\u7d76\u5bfe\u30b5\u30a4\u30ba\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002

        \\(n = 8\\)\u3092\u53d6\u308b\u3068\u3001\u5404\u66f2\u7dda\u306e\u5024\u304c\u305d\u306e\u95a2\u6570\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u306a\u3044\u3053\u3068\u306b\u6c17\u3065\u304f\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u3053\u308c\u306f\u3001\u5404\u66f2\u7dda\u306b\u5b9a\u6570\u9805\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u305f\u3081\u3067\u3001\u5024\u306e\u7bc4\u56f2\u3092\u8996\u899a\u7684\u306b\u5feb\u9069\u306a\u7bc4\u56f2\u306b\u5727\u7e2e\u3059\u308b\u3053\u3068\u3092\u610f\u56f3\u3057\u3066\u3044\u307e\u3059\u3002

        \u5b9f\u969b\u306b\u306f\u3001\u901a\u5e38\u306f\u5404\u30e1\u30bd\u30c3\u30c9\u306e\u300c\u5b9a\u6570\u9805\u300d\u8907\u96d1\u5ea6\u3092\u77e5\u3089\u306a\u3044\u305f\u3081\u3001\u8907\u96d1\u5ea6\u306e\u307f\u306b\u57fa\u3065\u3044\u3066\\(n = 8\\)\u306e\u6700\u826f\u30bd\u30ea\u30e5\u30fc\u30b7\u30e7\u30f3\u3092\u9078\u629e\u3059\u308b\u3053\u3068\u306f\u4e00\u822c\u7684\u306b\u4e0d\u53ef\u80fd\u3067\u3059\u3002\u3057\u304b\u3057\u3001\\(n = 8^5\\)\u306e\u5834\u5408\u3001\u5897\u52a0\u50be\u5411\u304c\u652f\u914d\u7684\u306b\u306a\u308b\u305f\u3081\u3001\u9078\u629e\u304c\u306f\u308b\u304b\u306b\u5bb9\u6613\u306b\u306a\u308a\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/","title":"2.3 \u00a0 \u6642\u9593\u8a08\u7b97\u91cf","text":"

        \u5b9f\u884c\u6642\u9593\u306f\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u76f4\u611f\u7684\u306b\u8a55\u4fa1\u3067\u304d\u307e\u3059\u3002\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u6642\u9593\u3092\u6b63\u78ba\u306b\u63a8\u5b9a\u3059\u308b\u306b\u306f\u3069\u3046\u3059\u308c\u3070\u3088\u3044\u3067\u3057\u3087\u3046\u304b\uff1f

        1. \u5b9f\u884c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u306e\u6c7a\u5b9a: \u3053\u308c\u306b\u306f\u3001\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u69cb\u6210\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3001\u30b7\u30b9\u30c6\u30e0\u74b0\u5883\u306a\u3069\u304c\u542b\u307e\u308c\u3001\u3053\u308c\u3089\u3059\u3079\u3066\u304c\u30b3\u30fc\u30c9\u306e\u5b9f\u884c\u52b9\u7387\u306b\u5f71\u97ff\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        2. \u69d8\u3005\u306a\u8a08\u7b97\u64cd\u4f5c\u306e\u5b9f\u884c\u6642\u9593\u306e\u8a55\u4fa1: \u4f8b\u3048\u3070\u3001\u52a0\u7b97\u64cd\u4f5c+\u306f1 ns\u3001\u4e57\u7b97\u64cd\u4f5c*\u306f10 ns\u3001\u5370\u5237\u64cd\u4f5cprint()\u306f5 ns\u306a\u3069\u304b\u304b\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        3. \u30b3\u30fc\u30c9\u5185\u306e\u3059\u3079\u3066\u306e\u8a08\u7b97\u64cd\u4f5c\u3092\u30ab\u30a6\u30f3\u30c8: \u3053\u308c\u3089\u3059\u3079\u3066\u306e\u64cd\u4f5c\u306e\u5b9f\u884c\u6642\u9593\u3092\u5408\u8a08\u3059\u308b\u3068\u3001\u7dcf\u5b9f\u884c\u6642\u9593\u304c\u5f97\u3089\u308c\u307e\u3059\u3002

        \u4f8b\u3048\u3070\u3001\u5165\u529b\u30b5\u30a4\u30ba\u304c\\(n\\)\u306e\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin
        # \u7279\u5b9a\u306e\u64cd\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4e0b\u3067\ndef algorithm(n: int):\n    a = 2      # 1 ns\n    a = a + 1  # 1 ns\n    a = a * 2  # 10 ns\n    # n\u56de\u30eb\u30fc\u30d7\n    for _ in range(n):  # 1 ns\n        print(0)        # 5 ns\n
        // \u7279\u5b9a\u306e\u64cd\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4e0b\u3067\nvoid algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // n\u56de\u30eb\u30fc\u30d7\n    for (int i = 0; i < n; i++) {  // 1 ns, \u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b\n        cout << 0 << endl;         // 5 ns\n    }\n}\n
        // \u7279\u5b9a\u306e\u64cd\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4e0b\u3067\nvoid algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // n\u56de\u30eb\u30fc\u30d7\n    for (int i = 0; i < n; i++) {  // 1 ns, \u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b\n        System.out.println(0);     // 5 ns\n    }\n}\n
        // \u7279\u5b9a\u306e\u64cd\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4e0b\u3067\nvoid Algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // n\u56de\u30eb\u30fc\u30d7\n    for (int i = 0; i < n; i++) {  // 1 ns, \u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b\n        Console.WriteLine(0);      // 5 ns\n    }\n}\n
        // \u7279\u5b9a\u306e\u64cd\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4e0b\u3067\nfunc algorithm(n int) {\n    a := 2     // 1 ns\n    a = a + 1  // 1 ns\n    a = a * 2  // 10 ns\n    // n\u56de\u30eb\u30fc\u30d7\n    for i := 0; i < n; i++ {  // 1 ns\n        fmt.Println(a)        // 5 ns\n    }\n}\n
        // \u7279\u5b9a\u306e\u64cd\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4e0b\u3067\nfunc algorithm(n: Int) {\n    var a = 2 // 1 ns\n    a = a + 1 // 1 ns\n    a = a * 2 // 10 ns\n    // n\u56de\u30eb\u30fc\u30d7\n    for _ in 0 ..< n { // 1 ns\n        print(0) // 5 ns\n    }\n}\n
        // \u7279\u5b9a\u306e\u64cd\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4e0b\u3067\nfunction algorithm(n) {\n    var a = 2; // 1 ns\n    a = a + 1; // 1 ns\n    a = a * 2; // 10 ns\n    // n\u56de\u30eb\u30fc\u30d7\n    for(let i = 0; i < n; i++) { // 1 ns, \u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b\n        console.log(0); // 5 ns\n    }\n}\n
        // \u7279\u5b9a\u306e\u64cd\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4e0b\u3067\nfunction algorithm(n: number): void {\n    var a: number = 2; // 1 ns\n    a = a + 1; // 1 ns\n    a = a * 2; // 10 ns\n    // n\u56de\u30eb\u30fc\u30d7\n    for(let i = 0; i < n; i++) { // 1 ns, \u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b\n        console.log(0); // 5 ns\n    }\n}\n
        // \u7279\u5b9a\u306e\u64cd\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4e0b\u3067\nvoid algorithm(int n) {\n  int a = 2; // 1 ns\n  a = a + 1; // 1 ns\n  a = a * 2; // 10 ns\n  // n\u56de\u30eb\u30fc\u30d7\n  for (int i = 0; i < n; i++) { // 1 ns, \u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b\n    print(0); // 5 ns\n  }\n}\n
        // \u7279\u5b9a\u306e\u64cd\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4e0b\u3067\nfn algorithm(n: i32) {\n    let mut a = 2;      // 1 ns\n    a = a + 1;          // 1 ns\n    a = a * 2;          // 10 ns\n    // n\u56de\u30eb\u30fc\u30d7\n    for _ in 0..n {     // \u6bce\u56dei++\u30671 ns\n        println!(\"{}\", 0);  // 5 ns\n    }\n}\n
        // \u7279\u5b9a\u306e\u64cd\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4e0b\u3067\nvoid algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // n\u56de\u30eb\u30fc\u30d7\n    for (int i = 0; i < n; i++) {   // 1 ns, \u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b\n        printf(\"%d\", 0);            // 5 ns\n    }\n}\n
        \n

        \u4e0a\u8a18\u306e\u65b9\u6cd5\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u6642\u9593\u306f\\((6n + 12)\\) ns\u3068\u3057\u3066\u8a08\u7b97\u3067\u304d\u307e\u3059\uff1a

        \\[ 1 + 1 + 10 + (1 + 5) \\times n = 6n + 12 \\]

        \u3057\u304b\u3057\u3001\u5b9f\u969b\u306b\u306f\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u6642\u9593\u3092\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u3053\u3068\u306f\u5b9f\u7528\u7684\u3067\u3082\u5408\u7406\u7684\u3067\u3082\u3042\u308a\u307e\u305b\u3093\u3002\u7b2c\u4e00\u306b\u3001\u63a8\u5b9a\u6642\u9593\u3092\u5b9f\u884c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u306b\u7d50\u3073\u4ed8\u3051\u305f\u304f\u3042\u308a\u307e\u305b\u3093\u3002\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u69d8\u3005\u306a\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u3067\u5b9f\u884c\u3055\u308c\u308b\u5fc5\u8981\u304c\u3042\u308b\u304b\u3089\u3067\u3059\u3002\u7b2c\u4e8c\u306b\u3001\u5404\u7a2e\u64cd\u4f5c\u306e\u5b9f\u884c\u6642\u9593\u3092\u77e5\u308b\u3053\u3068\u306f\u56f0\u96e3\u3067\u3042\u308a\u3001\u63a8\u5b9a\u30d7\u30ed\u30bb\u30b9\u3092\u96e3\u3057\u304f\u3057\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#231","title":"2.3.1 \u00a0 \u6642\u9593\u5897\u52a0\u50be\u5411\u306e\u8a55\u4fa1","text":"

        \u6642\u9593\u8a08\u7b97\u91cf\u5206\u6790\u306f\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u6642\u9593\u3092\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u306e\u3067\u306f\u306a\u304f\u3001**\u30c7\u30fc\u30bf\u91cf\u304c\u5897\u52a0\u3059\u308b\u306b\u3064\u308c\u3066\u306e\u5b9f\u884c\u6642\u9593\u306e\u5897\u52a0\u50be\u5411**\u3092\u5206\u6790\u3057\u307e\u3059\u3002

        \u3053\u306e\u300c\u6642\u9593\u5897\u52a0\u50be\u5411\u300d\u306e\u6982\u5ff5\u3092\u4f8b\u3067\u7406\u89e3\u3057\u307e\u3057\u3087\u3046\u3002\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\u3092\\(n\\)\u3068\u3057\u30013\u3064\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u3001B\u3001C\u3092\u8003\u3048\u3066\u307f\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin
        # \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\ndef algorithm_A(n: int):\n    print(0)\n# \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\ndef algorithm_B(n: int):\n    for _ in range(n):\n        print(0)\n# \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\ndef algorithm_C(n: int):\n    for _ in range(1000000):\n        print(0)\n
        // \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nvoid algorithm_A(int n) {\n    cout << 0 << endl;\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\nvoid algorithm_B(int n) {\n    for (int i = 0; i < n; i++) {\n        cout << 0 << endl;\n    }\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nvoid algorithm_C(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        cout << 0 << endl;\n    }\n}\n
        // \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nvoid algorithm_A(int n) {\n    System.out.println(0);\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\nvoid algorithm_B(int n) {\n    for (int i = 0; i < n; i++) {\n        System.out.println(0);\n    }\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nvoid algorithm_C(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        System.out.println(0);\n    }\n}\n
        // \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nvoid AlgorithmA(int n) {\n    Console.WriteLine(0);\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\nvoid AlgorithmB(int n) {\n    for (int i = 0; i < n; i++) {\n        Console.WriteLine(0);\n    }\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nvoid AlgorithmC(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        Console.WriteLine(0);\n    }\n}\n
        // \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nfunc algorithm_A(n int) {\n    fmt.Println(0)\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\nfunc algorithm_B(n int) {\n    for i := 0; i < n; i++ {\n        fmt.Println(0)\n    }\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nfunc algorithm_C(n int) {\n    for i := 0; i < 1000000; i++ {\n        fmt.Println(0)\n    }\n}\n
        // \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nfunc algorithmA(n: Int) {\n    print(0)\n}\n\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\nfunc algorithmB(n: Int) {\n    for _ in 0 ..< n {\n        print(0)\n    }\n}\n\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nfunc algorithmC(n: Int) {\n    for _ in 0 ..< 1_000_000 {\n        print(0)\n    }\n}\n
        // \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nfunction algorithm_A(n) {\n    console.log(0);\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\nfunction algorithm_B(n) {\n    for (let i = 0; i < n; i++) {\n        console.log(0);\n    }\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nfunction algorithm_C(n) {\n    for (let i = 0; i < 1000000; i++) {\n        console.log(0);\n    }\n}\n
        // \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nfunction algorithm_A(n: number): void {\n    console.log(0);\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\nfunction algorithm_B(n: number): void {\n    for (let i = 0; i < n; i++) {\n        console.log(0);\n    }\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nfunction algorithm_C(n: number): void {\n    for (let i = 0; i < 1000000; i++) {\n        console.log(0);\n    }\n}\n
        // \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nvoid algorithmA(int n) {\n  print(0);\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\nvoid algorithmB(int n) {\n  for (int i = 0; i < n; i++) {\n    print(0);\n  }\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nvoid algorithmC(int n) {\n  for (int i = 0; i < 1000000; i++) {\n    print(0);\n  }\n}\n
        // \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nfn algorithm_A(n: i32) {\n    println!(\"{}\", 0);\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\nfn algorithm_B(n: i32) {\n    for _ in 0..n {\n        println!(\"{}\", 0);\n    }\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nfn algorithm_C(n: i32) {\n    for _ in 0..1000000 {\n        println!(\"{}\", 0);\n    }\n}\n
        // \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nvoid algorithm_A(int n) {\n    printf(\"%d\", 0);\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\nvoid algorithm_B(int n) {\n    for (int i = 0; i < n; i++) {\n        printf(\"%d\", 0);\n    }\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nvoid algorithm_C(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        printf(\"%d\", 0);\n    }\n}\n
        \n

        \u4e0b\u56f3\u306f\u3053\u308c\u30893\u3064\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306b\u306f1\u3064\u306e\u5370\u5237\u64cd\u4f5c\u306e\u307f\u304c\u3042\u308a\u3001\u305d\u306e\u5b9f\u884c\u6642\u9593\u306f\\(n\\)\u3068\u3068\u3082\u306b\u5897\u52a0\u3057\u307e\u305b\u3093\u3002\u305d\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\u300c\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\u300d\u3068\u8003\u3048\u3089\u308c\u307e\u3059\u3002
        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306b\u306f\\(n\\)\u56de\u30eb\u30fc\u30d7\u3059\u308b\u5370\u5237\u64cd\u4f5c\u304c\u3042\u308a\u3001\u305d\u306e\u5b9f\u884c\u6642\u9593\u306f\\(n\\)\u3068\u7dda\u5f62\u306b\u5897\u52a0\u3057\u307e\u3059\u3002\u305d\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\u300c\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\u300d\u3067\u3059\u3002
        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306b\u306f1,000,000\u56de\u30eb\u30fc\u30d7\u3059\u308b\u5370\u5237\u64cd\u4f5c\u304c\u3042\u308a\u307e\u3059\u3002\u6642\u9593\u306f\u304b\u304b\u308a\u307e\u3059\u304c\u3001\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\\(n\\)\u3068\u306f\u7121\u95a2\u4fc2\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001C\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306fA\u3068\u540c\u3058\u300c\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\u300d\u3067\u3059\u3002

        \u56f3 2-7 \u00a0 Time growth trend of algorithms a, b, and c

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u6642\u9593\u3092\u76f4\u63a5\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u3053\u3068\u3068\u6bd4\u8f03\u3057\u3066\u3001\u6642\u9593\u8a08\u7b97\u91cf\u5206\u6790\u306e\u7279\u5fb4\u306f\u4f55\u3067\u3057\u3087\u3046\u304b\uff1f

        • \u6642\u9593\u8a08\u7b97\u91cf\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u52b9\u679c\u7684\u306b\u8a55\u4fa1\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306f\u7dda\u5f62\u306b\u5897\u52a0\u3059\u308b\u5b9f\u884c\u6642\u9593\u3092\u6301\u3061\u3001\\(n > 1\\)\u306e\u6642\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u3088\u308a\u9045\u304f\u3001\\(n > 1,000,000\\)\u306e\u6642\u306fC\u3088\u308a\u9045\u304f\u306a\u308a\u307e\u3059\u3002\u5b9f\u969b\u3001\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\\(n\\)\u304c\u5341\u5206\u306b\u5927\u304d\u3044\u9650\u308a\u3001\u300c\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\u300d\u8907\u96d1\u5ea6\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u5e38\u306b\u300c\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\u300d\u3088\u308a\u3082\u512a\u308c\u3066\u304a\u308a\u3001\u6642\u9593\u5897\u52a0\u50be\u5411\u306e\u672c\u8cea\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002
        • \u6642\u9593\u8a08\u7b97\u91cf\u5206\u6790\u306f\u3088\u308a\u76f4\u611f\u7684\u3067\u3059\u3002\u660e\u3089\u304b\u306b\u3001\u5b9f\u884c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u3068\u8a08\u7b97\u64cd\u4f5c\u306e\u7a2e\u985e\u306f\u5b9f\u884c\u6642\u9593\u5897\u52a0\u306e\u50be\u5411\u3068\u306f\u7121\u95a2\u4fc2\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u6642\u9593\u8a08\u7b97\u91cf\u5206\u6790\u3067\u306f\u3001\u3059\u3079\u3066\u306e\u8a08\u7b97\u64cd\u4f5c\u306e\u5b9f\u884c\u6642\u9593\u3092\u540c\u3058\u300c\u5358\u4f4d\u6642\u9593\u300d\u3068\u3057\u3066\u6271\u3046\u3053\u3068\u304c\u3067\u304d\u3001\u300c\u8a08\u7b97\u64cd\u4f5c\u5b9f\u884c\u6642\u9593\u30ab\u30a6\u30f3\u30c8\u300d\u3092\u300c\u8a08\u7b97\u64cd\u4f5c\u30ab\u30a6\u30f3\u30c8\u300d\u306b\u5358\u7d14\u5316\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u63a8\u5b9a\u306e\u8907\u96d1\u3055\u304c\u5927\u5e45\u306b\u8efd\u6e1b\u3055\u308c\u307e\u3059\u3002
        • \u6642\u9593\u8a08\u7b97\u91cf\u306b\u306f\u5236\u9650\u304c\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u3068C\u306f\u540c\u3058\u6642\u9593\u8a08\u7b97\u91cf\u3092\u6301\u3061\u307e\u3059\u304c\u3001\u5b9f\u969b\u306e\u5b9f\u884c\u6642\u9593\u306f\u5927\u304d\u304f\u7570\u306a\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u540c\u69d8\u306b\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306fC\u3088\u308a\u3082\u9ad8\u3044\u6642\u9593\u8a08\u7b97\u91cf\u3092\u6301\u3061\u307e\u3059\u304c\u3001\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\\(n\\)\u304c\u5c0f\u3055\u3044\u5834\u5408\u306f\u660e\u3089\u304b\u306b\u512a\u308c\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u5834\u5408\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306e\u307f\u306b\u57fa\u3065\u3044\u3066\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u5224\u65ad\u3059\u308b\u3053\u3068\u306f\u56f0\u96e3\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u3053\u308c\u3089\u306e\u554f\u984c\u306b\u3082\u304b\u304b\u308f\u3089\u305a\u3001\u8907\u96d1\u5ea6\u5206\u6790\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u8a55\u4fa1\u3059\u308b\u305f\u3081\u306e\u6700\u3082\u52b9\u679c\u7684\u3067\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u308b\u65b9\u6cd5\u3067\u3059\u3002
        "},{"location":"chapter_computational_complexity/time_complexity/#232","title":"2.3.2 \u00a0 \u6f38\u8fd1\u4e0a\u9650","text":"

        \u5165\u529b\u30b5\u30a4\u30ba\u304c\\(n\\)\u306e\u95a2\u6570\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin
        def algorithm(n: int):\n    a = 1      # +1\n    a = a + 1  # +1\n    a = a * 2  # +1\n    # n\u56de\u30eb\u30fc\u30d7\n    for i in range(n):  # +1\n        print(0)        # +1\n
        void algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // n\u56de\u30eb\u30fc\u30d7\n    for (int i = 0; i < n; i++) { // +1 (\u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b)\n        cout << 0 << endl;    // +1\n    }\n}\n
        void algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // n\u56de\u30eb\u30fc\u30d7\n    for (int i = 0; i < n; i++) { // +1 (\u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b)\n        System.out.println(0);    // +1\n    }\n}\n
        void Algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // n\u56de\u30eb\u30fc\u30d7\n    for (int i = 0; i < n; i++) {   // +1 (\u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b)\n        Console.WriteLine(0);   // +1\n    }\n}\n
        func algorithm(n int) {\n    a := 1      // +1\n    a = a + 1   // +1\n    a = a * 2   // +1\n    // n\u56de\u30eb\u30fc\u30d7\n    for i := 0; i < n; i++ {   // +1\n        fmt.Println(a)         // +1\n    }\n}\n
        func algorithm(n: Int) {\n    var a = 1 // +1\n    a = a + 1 // +1\n    a = a * 2 // +1\n    // n\u56de\u30eb\u30fc\u30d7\n    for _ in 0 ..< n { // +1\n        print(0) // +1\n    }\n}\n
        function algorithm(n) {\n    var a = 1; // +1\n    a += 1; // +1\n    a *= 2; // +1\n    // n\u56de\u30eb\u30fc\u30d7\n    for(let i = 0; i < n; i++){ // +1 (\u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b)\n        console.log(0); // +1\n    }\n}\n
        function algorithm(n: number): void{\n    var a: number = 1; // +1\n    a += 1; // +1\n    a *= 2; // +1\n    // n\u56de\u30eb\u30fc\u30d7\n    for(let i = 0; i < n; i++){ // +1 (\u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b)\n        console.log(0); // +1\n    }\n}\n
        void algorithm(int n) {\n  int a = 1; // +1\n  a = a + 1; // +1\n  a = a * 2; // +1\n  // n\u56de\u30eb\u30fc\u30d7\n  for (int i = 0; i < n; i++) { // +1 (\u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b)\n    print(0); // +1\n  }\n}\n
        fn algorithm(n: i32) {\n    let mut a = 1;   // +1\n    a = a + 1;      // +1\n    a = a * 2;      // +1\n\n    // n\u56de\u30eb\u30fc\u30d7\n    for _ in 0..n { // +1 (\u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b)\n        println!(\"{}\", 0); // +1\n    }\n}\n
        void algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // n\u56de\u30eb\u30fc\u30d7\n    for (int i = 0; i < n; i++) {   // +1 (\u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b)\n        printf(\"%d\", 0);            // +1\n    }\n}\n
        \n

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u64cd\u4f5c\u6570\u3092\u5165\u529b\u30b5\u30a4\u30ba\\(n\\)\u306e\u95a2\u6570\u3068\u3057\u3066\u8868\u3059\u95a2\u6570\u3092\\(T(n)\\)\u3068\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u4f8b\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\uff1a

        \\[ T(n) = 3 + 2n \\]

        \\(T(n)\\)\u306f\u7dda\u5f62\u95a2\u6570\u3067\u3042\u308b\u305f\u3081\u3001\u305d\u306e\u5897\u52a0\u50be\u5411\u306f\u7dda\u5f62\u3067\u3042\u308a\u3001\u3057\u305f\u304c\u3063\u3066\u3001\u305d\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\u3067\u3001\\(O(n)\\)\u3068\u8868\u8a18\u3055\u308c\u307e\u3059\u3002\u3053\u306e\u6570\u5b66\u8a18\u6cd5\u306f\u30d3\u30c3\u30b0O\u8a18\u6cd5\u3068\u3057\u3066\u77e5\u3089\u308c\u3001\u95a2\u6570\\(T(n)\\)\u306e\u6f38\u8fd1\u4e0a\u9650\u3092\u8868\u3057\u307e\u3059\u3002

        \u672c\u8cea\u7684\u306b\u3001\u6642\u9593\u8a08\u7b97\u91cf\u5206\u6790\u306f\u300c\u64cd\u4f5c\u6570\\(T(n)\\)\u300d\u306e\u6f38\u8fd1\u4e0a\u9650\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3067\u3059\u3002\u305d\u308c\u306b\u306f\u6b63\u78ba\u306a\u6570\u5b66\u7684\u5b9a\u7fa9\u304c\u3042\u308a\u307e\u3059\u3002

        \u6f38\u8fd1\u4e0a\u9650

        \u3059\u3079\u3066\u306e\\(n > n_0\\)\u306b\u5bfe\u3057\u3066\\(T(n) \\leq c \\cdot f(n)\\)\u3068\u306a\u308b\u3088\u3046\u306a\u6b63\u306e\u5b9f\u6570\\(c\\)\u3068\\(n_0\\)\u304c\u5b58\u5728\u3059\u308b\u5834\u5408\u3001\\(f(n)\\)\u306f\\(T(n)\\)\u306e\u6f38\u8fd1\u4e0a\u9650\u3068\u307f\u306a\u3055\u308c\u3001\\(T(n) = O(f(n))\\)\u3068\u8868\u8a18\u3055\u308c\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u3001\u6f38\u8fd1\u4e0a\u9650\u306e\u8a08\u7b97\u3067\u306f\u3001\\(n\\)\u304c\u7121\u9650\u5927\u306b\u8fd1\u3065\u304f\u306b\u3064\u308c\u3066\u3001\\(T(n)\\)\u3068\\(f(n)\\)\u304c\u540c\u3058\u5897\u52a0\u30aa\u30fc\u30c0\u30fc\u3092\u6301\u3061\u3001\u5b9a\u6570\u56e0\u5b50\\(c\\)\u306e\u307f\u304c\u7570\u306a\u308b\u3088\u3046\u306a\u95a2\u6570\\(f(n)\\)\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u542b\u307e\u308c\u307e\u3059\u3002

        \u56f3 2-8 \u00a0 Asymptotic upper bound of a function

        "},{"location":"chapter_computational_complexity/time_complexity/#233","title":"2.3.3 \u00a0 \u8a08\u7b97\u65b9\u6cd5","text":"

        \u6f38\u8fd1\u4e0a\u9650\u306e\u6982\u5ff5\u306f\u6570\u5b66\u7684\u306b\u6fc3\u5bc6\u306b\u898b\u3048\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u4eca\u3059\u3050\u5b8c\u5168\u306b\u7406\u89e3\u3059\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u307e\u305a\u8a08\u7b97\u65b9\u6cd5\u3092\u7406\u89e3\u3057\u3001\u6642\u9593\u3092\u304b\u3051\u3066\u7df4\u7fd2\u3057\u7406\u89e3\u3057\u307e\u3057\u3087\u3046\u3002

        \\(f(n)\\)\u304c\u6c7a\u307e\u308c\u3070\u3001\u6642\u9593\u8a08\u7b97\u91cf\\(O(f(n))\\)\u304c\u5f97\u3089\u308c\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u6f38\u8fd1\u4e0a\u9650\\(f(n)\\)\u3092\u3069\u306e\u3088\u3046\u306b\u6c7a\u5b9a\u3059\u308b\u306e\u3067\u3057\u3087\u3046\u304b\uff1f\u3053\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u306f\u4e00\u822c\u7684\u306b2\u3064\u306e\u30b9\u30c6\u30c3\u30d7\u304c\u542b\u307e\u308c\u307e\u3059\uff1a\u64cd\u4f5c\u6570\u306e\u30ab\u30a6\u30f3\u30c8\u3068\u6f38\u8fd1\u4e0a\u9650\u306e\u6c7a\u5b9a\u3067\u3059\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#1-1","title":"1. \u00a0 \u30b9\u30c6\u30c3\u30d71: \u64cd\u4f5c\u6570\u306e\u30ab\u30a6\u30f3\u30c8","text":"

        \u3053\u306e\u30b9\u30c6\u30c3\u30d7\u3067\u306f\u3001\u30b3\u30fc\u30c9\u3092\u884c\u3054\u3068\u306b\u78ba\u8a8d\u3057\u307e\u3059\u3002\u3057\u304b\u3057\u3001\\(c \\cdot f(n)\\)\u306e\u5b9a\u6570\\(c\\)\u306e\u5b58\u5728\u306b\u3088\u308a\u3001\\(T(n)\\)\u306e\u3059\u3079\u3066\u306e\u4fc2\u6570\u3068\u5b9a\u6570\u9805\u306f\u7121\u8996\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u539f\u7406\u306b\u3088\u308a\u3001\u64cd\u4f5c\u3092\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u969b\u306e\u7c21\u7565\u5316\u6280\u6cd5\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002

        1. \\(T(n)\\)\u306e\u5b9a\u6570\u9805\u3092\u7121\u8996\u3057\u307e\u3059\u3002\u3053\u308c\u3089\u306f\\(n\\)\u3068\u306f\u7121\u95a2\u4fc2\u3067\u3042\u308b\u305f\u3081\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306b\u5f71\u97ff\u3057\u307e\u305b\u3093\u3002
        2. \u3059\u3079\u3066\u306e\u4fc2\u6570\u3092\u7701\u7565\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\\(2n\\)\u3001\\(5n + 1\\)\u56de\u306a\u3069\u306e\u30eb\u30fc\u30d7\u306f\u3001\\(n\\)\u306e\u524d\u306e\u4fc2\u6570\u304c\u6642\u9593\u8a08\u7b97\u91cf\u306b\u5f71\u97ff\u3057\u306a\u3044\u305f\u3081\u3001\\(n\\)\u56de\u306b\u7c21\u7565\u5316\u3067\u304d\u307e\u3059\u3002
        3. \u30cd\u30b9\u30c8\u3057\u305f\u30eb\u30fc\u30d7\u306b\u306f\u4e57\u7b97\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u7dcf\u64cd\u4f5c\u6570\u306f\u5404\u30eb\u30fc\u30d7\u306e\u64cd\u4f5c\u6570\u306e\u7a4d\u3067\u3042\u308a\u3001\u30dd\u30a4\u30f3\u30c81\u30682\u306e\u7c21\u7565\u5316\u6280\u6cd5\u3092\u5404\u30eb\u30fc\u30d7\u30ec\u30d9\u30eb\u306b\u9069\u7528\u3057\u307e\u3059\u3002

        \u95a2\u6570\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u3053\u308c\u3089\u306e\u6280\u6cd5\u3092\u4f7f\u7528\u3057\u3066\u64cd\u4f5c\u3092\u30ab\u30a6\u30f3\u30c8\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin
        def algorithm(n: int):\n    a = 1      # +0 (\u6280\u6cd51)\n    a = a + n  # +0 (\u6280\u6cd51)\n    # +n (\u6280\u6cd52)\n    for i in range(5 * n + 1):\n        print(0)\n    # +n*n (\u6280\u6cd53)\n    for i in range(2 * n):\n        for j in range(n + 1):\n            print(0)\n
        void algorithm(int n) {\n    int a = 1;  // +0 (\u6280\u6cd51)\n    a = a + n;  // +0 (\u6280\u6cd51)\n    // +n (\u6280\u6cd52)\n    for (int i = 0; i < 5 * n + 1; i++) {\n        cout << 0 << endl;\n    }\n    // +n*n (\u6280\u6cd53)\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            cout << 0 << endl;\n        }\n    }\n}\n
        void algorithm(int n) {\n    int a = 1;  // +0 (\u6280\u6cd51)\n    a = a + n;  // +0 (\u6280\u6cd51)\n    // +n (\u6280\u6cd52)\n    for (int i = 0; i < 5 * n + 1; i++) {\n        System.out.println(0);\n    }\n    // +n*n (\u6280\u6cd53)\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            System.out.println(0);\n        }\n    }\n}\n
        void Algorithm(int n) {\n    int a = 1;  // +0 (\u6280\u6cd51)\n    a = a + n;  // +0 (\u6280\u6cd51)\n    // +n (\u6280\u6cd52)\n    for (int i = 0; i < 5 * n + 1; i++) {\n        Console.WriteLine(0);\n    }\n    // +n*n (\u6280\u6cd53)\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            Console.WriteLine(0);\n        }\n    }\n}\n
        func algorithm(n int) {\n    a := 1     // +0 (\u6280\u6cd51)\n    a = a + n  // +0 (\u6280\u6cd51)\n    // +n (\u6280\u6cd52)\n    for i := 0; i < 5 * n + 1; i++ {\n        fmt.Println(0)\n    }\n    // +n*n (\u6280\u6cd53)\n    for i := 0; i < 2 * n; i++ {\n        for j := 0; j < n + 1; j++ {\n            fmt.Println(0)\n        }\n    }\n}\n
        func algorithm(n: Int) {\n    var a = 1 // +0 (\u6280\u6cd51)\n    a = a + n // +0 (\u6280\u6cd51)\n    // +n (\u6280\u6cd52)\n    for _ in 0 ..< (5 * n + 1) {\n        print(0)\n    }\n    // +n*n (\u6280\u6cd53)\n    for _ in 0 ..< (2 * n) {\n        for _ in 0 ..< (n + 1) {\n            print(0)\n        }\n    }\n}\n
        function algorithm(n) {\n    let a = 1;  // +0 (\u6280\u6cd51)\n    a = a + n;  // +0 (\u6280\u6cd51)\n    // +n (\u6280\u6cd52)\n    for (let i = 0; i < 5 * n + 1; i++) {\n        console.log(0);\n    }\n    // +n*n (\u6280\u6cd53)\n    for (let i = 0; i < 2 * n; i++) {\n        for (let j = 0; j < n + 1; j++) {\n            console.log(0);\n        }\n    }\n}\n
        function algorithm(n: number): void {\n    let a = 1;  // +0 (\u6280\u6cd51)\n    a = a + n;  // +0 (\u6280\u6cd51)\n    // +n (\u6280\u6cd52)\n    for (let i = 0; i < 5 * n + 1; i++) {\n        console.log(0);\n    }\n    // +n*n (\u6280\u6cd53)\n    for (let i = 0; i < 2 * n; i++) {\n        for (let j = 0; j < n + 1; j++) {\n            console.log(0);\n        }\n    }\n}\n
        void algorithm(int n) {\n  int a = 1; // +0 (\u6280\u6cd51)\n  a = a + n; // +0 (\u6280\u6cd51)\n  // +n (\u6280\u6cd52)\n  for (int i = 0; i < 5 * n + 1; i++) {\n    print(0);\n  }\n  // +n*n (\u6280\u6cd53)\n  for (int i = 0; i < 2 * n; i++) {\n    for (int j = 0; j < n + 1; j++) {\n      print(0);\n    }\n  }\n}\n
        fn algorithm(n: i32) {\n    let mut a = 1;     // +0 (\u6280\u6cd51)\n    a = a + n;        // +0 (\u6280\u6cd51)\n\n    // +n (\u6280\u6cd52)\n    for i in 0..(5 * n + 1) {\n        println!(\"{}\", 0);\n    }\n\n    // +n*n (\u6280\u6cd53)\n    for i in 0..(2 * n) {\n        for j in 0..(n + 1) {\n            println!(\"{}\", 0);\n        }\n    }\n}\n
        void algorithm(int n) {\n    int a = 1;  // +0 (\u6280\u6cd51)\n    a = a + n;  // +0 (\u6280\u6cd51)\n    // +n (\u6280\u6cd52)\n    for (int i = 0; i < 5 * n + 1; i++) {\n        printf(\"%d\", 0);\n    }\n    // +n*n (\u6280\u6cd53)\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            printf(\"%d\", 0);\n        }\n    }\n}\n
        \n

        \u4ee5\u4e0b\u306e\u5f0f\u306f\u3001\u7c21\u7565\u5316\u524d\u5f8c\u306e\u30ab\u30a6\u30f3\u30c8\u7d50\u679c\u3092\u793a\u3057\u3066\u304a\u308a\u3001\u3069\u3061\u3089\u3082\\(O(n^2)\\)\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306b\u5c0e\u304d\u307e\u3059\uff1a

        \\[ \\begin{aligned} T(n) & = 2n(n + 1) + (5n + 1) + 2 & \\text{\u5b8c\u5168\u30ab\u30a6\u30f3\u30c8 (-.-|||)} \\newline & = 2n^2 + 7n + 3 \\newline T(n) & = n^2 + n & \\text{\u7c21\u7565\u5316\u30ab\u30a6\u30f3\u30c8 (o.O)} \\end{aligned} \\]"},{"location":"chapter_computational_complexity/time_complexity/#2-2","title":"2. \u00a0 \u30b9\u30c6\u30c3\u30d72: \u6f38\u8fd1\u4e0a\u9650\u306e\u6c7a\u5b9a","text":"

        \u6642\u9593\u8a08\u7b97\u91cf\u306f\\(T(n)\\)\u306e\u6700\u9ad8\u6b21\u9805\u306b\u3088\u3063\u3066\u6c7a\u5b9a\u3055\u308c\u307e\u3059\u3002\u3053\u308c\u306f\u3001\\(n\\)\u304c\u7121\u9650\u5927\u306b\u8fd1\u3065\u304f\u306b\u3064\u308c\u3066\u3001\u6700\u9ad8\u6b21\u9805\u304c\u652f\u914d\u7684\u306b\u306a\u308a\u3001\u4ed6\u306e\u9805\u306e\u5f71\u97ff\u306f\u7121\u8996\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u305f\u3081\u3067\u3059\u3002

        \u4ee5\u4e0b\u306e\u8868\u306f\u3001\u7570\u306a\u308b\u64cd\u4f5c\u30ab\u30a6\u30f3\u30c8\u3068\u305d\u308c\u306b\u5bfe\u5fdc\u3059\u308b\u6642\u9593\u8a08\u7b97\u91cf\u306e\u4f8b\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u4fc2\u6570\u304c\u5897\u52a0\u30aa\u30fc\u30c0\u30fc\u3092\u5909\u66f4\u3067\u304d\u306a\u3044\u3053\u3068\u3092\u5f37\u8abf\u3059\u308b\u305f\u3081\u306b\u3001\u8a87\u5f35\u3055\u308c\u305f\u5024\u304c\u4f7f\u7528\u3055\u308c\u3066\u3044\u307e\u3059\u3002\\(n\\)\u304c\u975e\u5e38\u306b\u5927\u304d\u304f\u306a\u308b\u3068\u3001\u3053\u308c\u3089\u306e\u5b9a\u6570\u306f\u91cd\u8981\u3067\u306a\u304f\u306a\u308a\u307e\u3059\u3002

        \u8868: \u7570\u306a\u308b\u64cd\u4f5c\u30ab\u30a6\u30f3\u30c8\u306b\u5bfe\u3059\u308b\u6642\u9593\u8a08\u7b97\u91cf

        \u64cd\u4f5c\u30ab\u30a6\u30f3\u30c8 \\(T(n)\\) \u6642\u9593\u8a08\u7b97\u91cf \\(O(f(n))\\) \\(100000\\) \\(O(1)\\) \\(3n + 2\\) \\(O(n)\\) \\(2n^2 + 3n + 2\\) \\(O(n^2)\\) \\(n^3 + 10000n^2\\) \\(O(n^3)\\) \\(2^n + 10000n^{10000}\\) \\(O(2^n)\\)"},{"location":"chapter_computational_complexity/time_complexity/#234","title":"2.3.4 \u00a0 \u4e00\u822c\u7684\u306a\u6642\u9593\u8a08\u7b97\u91cf\u306e\u7a2e\u985e","text":"

        \u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\u3092\\(n\\)\u3068\u3057\u307e\u3057\u3087\u3046\u3002\u4e00\u822c\u7684\u306a\u6642\u9593\u8a08\u7b97\u91cf\u306e\u7a2e\u985e\u3092\u4e0b\u56f3\u306b\u793a\u3057\u3001\u4f4e\u3044\u3082\u306e\u304b\u3089\u9ad8\u3044\u3082\u306e\u3078\u3068\u4e26\u3079\u3066\u3044\u307e\u3059\uff1a

        \\[ \\begin{aligned} & O(1) < O(\\log n) < O(n) < O(n \\log n) < O(n^2) < O(2^n) < O(n!) \\newline & \\text{\u5b9a\u6570} < \\text{\u5bfe\u6570} < \\text{\u7dda\u5f62} < \\text{\u7dda\u5f62\u5bfe\u6570} < \\text{\u4e8c\u6b21} < \\text{\u6307\u6570} < \\text{\u968e\u4e57} \\end{aligned} \\]

        \u56f3 2-9 \u00a0 Common types of time complexity

        "},{"location":"chapter_computational_complexity/time_complexity/#1-o1","title":"1. \u00a0 \u5b9a\u6570\u30aa\u30fc\u30c0\u30fc \\(O(1)\\)","text":"

        \u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\u306f\u3001\u64cd\u4f5c\u6570\u304c\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\\(n\\)\u3068\u306f\u7121\u95a2\u4fc2\u3067\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u95a2\u6570\u3067\u306f\u3001\u64cd\u4f5c\u6570size\u304c\u5927\u304d\u3044\u5834\u5408\u3067\u3082\u3001\\(n\\)\u3068\u306f\u7121\u95a2\u4fc2\u3067\u3042\u308b\u305f\u3081\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u306e\u307e\u307e\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def constant(n: int) -> int:\n    \"\"\"\u5b9a\u6570\u8907\u96d1\u5ea6\"\"\"\n    count = 0\n    size = 100000\n    for _ in range(size):\n        count += 1\n    return count\n
        time_complexity.cpp
        /* \u5b9a\u6570\u8a08\u7b97\u91cf */\nint constant(int n) {\n    int count = 0;\n    int size = 100000;\n    for (int i = 0; i < size; i++)\n        count++;\n    return count;\n}\n
        time_complexity.java
        /* \u5b9a\u6570\u8a08\u7b97\u91cf */\nint constant(int n) {\n    int count = 0;\n    int size = 100000;\n    for (int i = 0; i < size; i++)\n        count++;\n    return count;\n}\n
        time_complexity.cs
        [class]{time_complexity}-[func]{Constant}\n
        time_complexity.go
        [class]{}-[func]{constant}\n
        time_complexity.swift
        [class]{}-[func]{constant}\n
        time_complexity.js
        [class]{}-[func]{constant}\n
        time_complexity.ts
        [class]{}-[func]{constant}\n
        time_complexity.dart
        [class]{}-[func]{constant}\n
        time_complexity.rs
        [class]{}-[func]{constant}\n
        time_complexity.c
        [class]{}-[func]{constant}\n
        time_complexity.kt
        [class]{}-[func]{constant}\n
        time_complexity.rb
        [class]{}-[func]{constant}\n
        "},{"location":"chapter_computational_complexity/time_complexity/#2-on","title":"2. \u00a0 \u7dda\u5f62\u30aa\u30fc\u30c0\u30fc \\(O(n)\\)","text":"

        \u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\u306f\u3001\u64cd\u4f5c\u6570\u304c\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\\(n\\)\u3068\u7dda\u5f62\u306b\u5897\u52a0\u3059\u308b\u3053\u3068\u3092\u793a\u3057\u307e\u3059\u3002\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\u306f\u4e00\u822c\u7684\u306b\u5358\u4e00\u30eb\u30fc\u30d7\u69cb\u9020\u3067\u73fe\u308c\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def linear(n: int) -> int:\n    \"\"\"\u7dda\u5f62\u8907\u96d1\u5ea6\"\"\"\n    count = 0\n    for _ in range(n):\n        count += 1\n    return count\n
        time_complexity.cpp
        /* \u7dda\u5f62\u8a08\u7b97\u91cf */\nint linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++)\n        count++;\n    return count;\n}\n
        time_complexity.java
        /* \u7dda\u5f62\u8a08\u7b97\u91cf */\nint linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++)\n        count++;\n    return count;\n}\n
        time_complexity.cs
        [class]{time_complexity}-[func]{Linear}\n
        time_complexity.go
        [class]{}-[func]{linear}\n
        time_complexity.swift
        [class]{}-[func]{linear}\n
        time_complexity.js
        [class]{}-[func]{linear}\n
        time_complexity.ts
        [class]{}-[func]{linear}\n
        time_complexity.dart
        [class]{}-[func]{linear}\n
        time_complexity.rs
        [class]{}-[func]{linear}\n
        time_complexity.c
        [class]{}-[func]{linear}\n
        time_complexity.kt
        [class]{}-[func]{linear}\n
        time_complexity.rb
        [class]{}-[func]{linear}\n

        \u914d\u5217\u306e\u8d70\u67fb\u3084\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u8d70\u67fb\u306a\u3069\u306e\u64cd\u4f5c\u306f\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(n)\\)\u3067\u3001\\(n\\)\u306f\u914d\u5217\u307e\u305f\u306f\u30ea\u30b9\u30c8\u306e\u9577\u3055\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def array_traversal(nums: list[int]) -> int:\n    \"\"\"\u7dda\u5f62\u8907\u96d1\u5ea6\uff08\u914d\u5217\u306e\u8d70\u67fb\uff09\"\"\"\n    count = 0\n    # \u30eb\u30fc\u30d7\u56de\u6570\u306f\u914d\u5217\u306e\u9577\u3055\u306b\u6bd4\u4f8b\u3059\u308b\n    for num in nums:\n        count += 1\n    return count\n
        time_complexity.cpp
        /* \u7dda\u5f62\u8a08\u7b97\u91cf\uff08\u914d\u5217\u306e\u8d70\u67fb\uff09 */\nint arrayTraversal(vector<int> &nums) {\n    int count = 0;\n    // \u30eb\u30fc\u30d7\u56de\u6570\u306f\u914d\u5217\u306e\u9577\u3055\u306b\u6bd4\u4f8b\n    for (int num : nums) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u7dda\u5f62\u8a08\u7b97\u91cf\uff08\u914d\u5217\u306e\u8d70\u67fb\uff09 */\nint arrayTraversal(int[] nums) {\n    int count = 0;\n    // \u30eb\u30fc\u30d7\u56de\u6570\u306f\u914d\u5217\u306e\u9577\u3055\u306b\u6bd4\u4f8b\n    for (int num : nums) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.cs
        [class]{time_complexity}-[func]{ArrayTraversal}\n
        time_complexity.go
        [class]{}-[func]{arrayTraversal}\n
        time_complexity.swift
        [class]{}-[func]{arrayTraversal}\n
        time_complexity.js
        [class]{}-[func]{arrayTraversal}\n
        time_complexity.ts
        [class]{}-[func]{arrayTraversal}\n
        time_complexity.dart
        [class]{}-[func]{arrayTraversal}\n
        time_complexity.rs
        [class]{}-[func]{array_traversal}\n
        time_complexity.c
        [class]{}-[func]{arrayTraversal}\n
        time_complexity.kt
        [class]{}-[func]{arrayTraversal}\n
        time_complexity.rb
        [class]{}-[func]{array_traversal}\n

        \u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\\(n\\)\u306f\u5165\u529b\u30c7\u30fc\u30bf\u306e\u7a2e\u985e\u306b\u57fa\u3065\u3044\u3066\u6c7a\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u6700\u521d\u306e\u4f8b\u3067\u306f\u3001\\(n\\)\u306f\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\u3092\u8868\u3057\u30012\u756a\u76ee\u306e\u4f8b\u3067\u306f\u3001\u914d\u5217\u306e\u9577\u3055\\(n\\)\u304c\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\u3067\u3059\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#3-on2","title":"3. \u00a0 \u4e8c\u6b21\u30aa\u30fc\u30c0\u30fc \\(O(n^2)\\)","text":"

        \u4e8c\u6b21\u30aa\u30fc\u30c0\u30fc\u306f\u3001\u64cd\u4f5c\u6570\u304c\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\\(n\\)\u306e\u4e8c\u4e57\u306b\u6bd4\u4f8b\u3057\u3066\u5897\u52a0\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u4e8c\u6b21\u30aa\u30fc\u30c0\u30fc\u306f\u901a\u5e38\u30cd\u30b9\u30c8\u3057\u305f\u30eb\u30fc\u30d7\u3067\u73fe\u308c\u3001\u5916\u5074\u3068\u5185\u5074\u306e\u30eb\u30fc\u30d7\u306e\u4e21\u65b9\u304c\u6642\u9593\u8a08\u7b97\u91cf\\(O(n)\\)\u3092\u6301\u3061\u3001\u5168\u4f53\u306e\u8907\u96d1\u5ea6\u306f\\(O(n^2)\\)\u306b\u306a\u308a\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def quadratic(n: int) -> int:\n    \"\"\"\u4e8c\u6b21\u8907\u96d1\u5ea6\"\"\"\n    count = 0\n    # \u30eb\u30fc\u30d7\u56de\u6570\u306f\u30c7\u30fc\u30bf\u30b5\u30a4\u30ban\u306e\u4e8c\u4e57\u306b\u6bd4\u4f8b\u3059\u308b\n    for i in range(n):\n        for j in range(n):\n            count += 1\n    return count\n
        time_complexity.cpp
        /* \u4e8c\u6b21\u8a08\u7b97\u91cf */\nint quadratic(int n) {\n    int count = 0;\n    // \u30eb\u30fc\u30d7\u56de\u6570\u306f\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba n \u306e\u4e8c\u4e57\u306b\u6bd4\u4f8b\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u4e8c\u6b21\u8a08\u7b97\u91cf */\nint quadratic(int n) {\n    int count = 0;\n    // \u30eb\u30fc\u30d7\u56de\u6570\u306f\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba n \u306e\u4e8c\u4e57\u306b\u6bd4\u4f8b\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
        time_complexity.cs
        [class]{time_complexity}-[func]{Quadratic}\n
        time_complexity.go
        [class]{}-[func]{quadratic}\n
        time_complexity.swift
        [class]{}-[func]{quadratic}\n
        time_complexity.js
        [class]{}-[func]{quadratic}\n
        time_complexity.ts
        [class]{}-[func]{quadratic}\n
        time_complexity.dart
        [class]{}-[func]{quadratic}\n
        time_complexity.rs
        [class]{}-[func]{quadratic}\n
        time_complexity.c
        [class]{}-[func]{quadratic}\n
        time_complexity.kt
        [class]{}-[func]{quadratic}\n
        time_complexity.rb
        [class]{}-[func]{quadratic}\n

        \u4e0b\u56f3\u306f\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\u3001\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\u3001\u4e8c\u6b21\u30aa\u30fc\u30c0\u30fc\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3092\u6bd4\u8f03\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 2-10 \u00a0 Constant, linear, and quadratic order time complexities

        \u4f8b\u3048\u3070\u3001\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u3067\u306f\u3001\u5916\u5074\u306e\u30eb\u30fc\u30d7\u304c\\(n - 1\\)\u56de\u5b9f\u884c\u3055\u308c\u3001\u5185\u5074\u306e\u30eb\u30fc\u30d7\u304c\\(n-1\\)\u3001\\(n-2\\)\u3001...\u3001\\(2\\)\u3001\\(1\\)\u56de\u5b9f\u884c\u3055\u308c\u3001\u5e73\u5747\\(n / 2\\)\u56de\u3068\u306a\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O((n - 1) n / 2) = O(n^2)\\)\u306b\u306a\u308a\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def bubble_sort(nums: list[int]) -> int:\n    \"\"\"\u4e8c\u6b21\u8907\u96d1\u5ea6\uff08\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\uff09\"\"\"\n    count = 0  # \u30ab\u30a6\u30f3\u30bf\n    # \u5916\u5074\u306e\u30eb\u30fc\u30d7: \u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f [0, i]\n    for i in range(len(nums) - 1, 0, -1):\n        # \u5185\u5074\u306e\u30eb\u30fc\u30d7: \u672a\u30bd\u30fc\u30c8\u7bc4\u56f2 [0, i] \u306e\u6700\u5927\u8981\u7d20\u3092\u53f3\u7aef\u306b\u30b9\u30ef\u30c3\u30d7\n        for j in range(i):\n            if nums[j] > nums[j + 1]:\n                # nums[j] \u3068 nums[j + 1] \u3092\u30b9\u30ef\u30c3\u30d7\n                tmp: int = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = tmp\n                count += 3  # \u8981\u7d20\u306e\u30b9\u30ef\u30c3\u30d7\u306f3\u3064\u306e\u500b\u5225\u64cd\u4f5c\u3092\u542b\u3080\n    return count\n
        time_complexity.cpp
        /* \u4e8c\u6b21\u8a08\u7b97\u91cf\uff08\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\uff09 */\nint bubbleSort(vector<int> &nums) {\n    int count = 0; // \u30ab\u30a6\u30f3\u30bf\u30fc\n    // \u5916\u5074\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f [0, i]\n    for (int i = nums.size() - 1; i > 0; i--) {\n        // \u5185\u5074\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2 [0, i] \u306e\u6700\u5927\u8981\u7d20\u3092\u7bc4\u56f2\u306e\u53f3\u7aef\u306b\u30b9\u30ef\u30c3\u30d7\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // nums[j] \u3068 nums[j + 1] \u3092\u30b9\u30ef\u30c3\u30d7\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // \u8981\u7d20\u306e\u30b9\u30ef\u30c3\u30d7\u306b\u306f3\u3064\u306e\u500b\u5225\u64cd\u4f5c\u304c\u542b\u307e\u308c\u308b\n            }\n        }\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u4e8c\u6b21\u8a08\u7b97\u91cf\uff08\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\uff09 */\nint bubbleSort(int[] nums) {\n    int count = 0; // \u30ab\u30a6\u30f3\u30bf\u30fc\n    // \u5916\u5074\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f [0, i]\n    for (int i = nums.length - 1; i > 0; i--) {\n        // \u5185\u5074\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2 [0, i] \u306e\u6700\u5927\u8981\u7d20\u3092\u7bc4\u56f2\u306e\u53f3\u7aef\u306b\u30b9\u30ef\u30c3\u30d7\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // nums[j] \u3068 nums[j + 1] \u3092\u30b9\u30ef\u30c3\u30d7\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // \u8981\u7d20\u306e\u30b9\u30ef\u30c3\u30d7\u306b\u306f3\u3064\u306e\u500b\u5225\u64cd\u4f5c\u304c\u542b\u307e\u308c\u308b\n            }\n        }\n    }\n    return count;\n}\n
        time_complexity.cs
        [class]{time_complexity}-[func]{BubbleSort}\n
        time_complexity.go
        [class]{}-[func]{bubbleSort}\n
        time_complexity.swift
        [class]{}-[func]{bubbleSort}\n
        time_complexity.js
        [class]{}-[func]{bubbleSort}\n
        time_complexity.ts
        [class]{}-[func]{bubbleSort}\n
        time_complexity.dart
        [class]{}-[func]{bubbleSort}\n
        time_complexity.rs
        [class]{}-[func]{bubble_sort}\n
        time_complexity.c
        [class]{}-[func]{bubbleSort}\n
        time_complexity.kt
        [class]{}-[func]{bubbleSort}\n
        time_complexity.rb
        [class]{}-[func]{bubble_sort}\n
        "},{"location":"chapter_computational_complexity/time_complexity/#4-o2n","title":"4. \u00a0 \u6307\u6570\u30aa\u30fc\u30c0\u30fc \\(O(2^n)\\)","text":"

        \u751f\u7269\u5b66\u7684\u300c\u7d30\u80de\u5206\u88c2\u300d\u306f\u6307\u6570\u30aa\u30fc\u30c0\u30fc\u5897\u52a0\u306e\u5178\u578b\u4f8b\u3067\u3059\uff1a1\u3064\u306e\u7d30\u80de\u304b\u3089\u59cb\u307e\u308a\u30011\u56de\u306e\u5206\u88c2\u5f8c\u306b2\u3064\u30012\u56de\u306e\u5206\u88c2\u5f8c\u306b4\u3064\u3068\u306a\u308a\u3001\\(n\\)\u56de\u306e\u5206\u88c2\u5f8c\u306b\\(2^n\\)\u500b\u306e\u7d30\u80de\u306b\u306a\u308a\u307e\u3059\u3002

        \u4e0b\u56f3\u3068\u30b3\u30fc\u30c9\u306f\u7d30\u80de\u5206\u88c2\u30d7\u30ed\u30bb\u30b9\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\u3057\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(2^n)\\)\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def exponential(n: int) -> int:\n    \"\"\"\u6307\u6570\u8907\u96d1\u5ea6\uff08\u30eb\u30fc\u30d7\u5b9f\u88c5\uff09\"\"\"\n    count = 0\n    base = 1\n    # \u30bb\u30eb\u306f\u6bce\u56de2\u3064\u306b\u5206\u88c2\u3057\u30011, 2, 4, 8, ..., 2^(n-1) \u306e\u6570\u5217\u3092\u5f62\u6210\u3059\u308b\n    for _ in range(n):\n        for _ in range(base):\n            count += 1\n        base *= 2\n    # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count\n
        time_complexity.cpp
        /* \u6307\u6570\u8a08\u7b97\u91cf\uff08\u30eb\u30fc\u30d7\u5b9f\u88c5\uff09 */\nint exponential(int n) {\n    int count = 0, base = 1;\n    // \u30bb\u30eb\u306f\u6bce\u30e9\u30a6\u30f3\u30c92\u3064\u306b\u5206\u88c2\u3057\u3001\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1) \u3092\u5f62\u6210\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
        time_complexity.java
        /* \u6307\u6570\u8a08\u7b97\u91cf\uff08\u30eb\u30fc\u30d7\u5b9f\u88c5\uff09 */\nint exponential(int n) {\n    int count = 0, base = 1;\n    // \u30bb\u30eb\u306f\u6bce\u30e9\u30a6\u30f3\u30c92\u3064\u306b\u5206\u88c2\u3057\u3001\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1) \u3092\u5f62\u6210\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
        time_complexity.cs
        [class]{time_complexity}-[func]{Exponential}\n
        time_complexity.go
        [class]{}-[func]{exponential}\n
        time_complexity.swift
        [class]{}-[func]{exponential}\n
        time_complexity.js
        [class]{}-[func]{exponential}\n
        time_complexity.ts
        [class]{}-[func]{exponential}\n
        time_complexity.dart
        [class]{}-[func]{exponential}\n
        time_complexity.rs
        [class]{}-[func]{exponential}\n
        time_complexity.c
        [class]{}-[func]{exponential}\n
        time_complexity.kt
        [class]{}-[func]{exponential}\n
        time_complexity.rb
        [class]{}-[func]{exponential}\n

        \u56f3 2-11 \u00a0 Exponential order time complexity

        \u5b9f\u969b\u306b\u306f\u3001\u6307\u6570\u30aa\u30fc\u30c0\u30fc\u306f\u518d\u5e30\u95a2\u6570\u3067\u3088\u304f\u73fe\u308c\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3001\u518d\u5e30\u7684\u306b2\u3064\u306e\u534a\u5206\u306b\u5206\u5272\u3057\u3001\\(n\\)\u56de\u306e\u5206\u5272\u5f8c\u306b\u505c\u6b62\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def exp_recur(n: int) -> int:\n    \"\"\"\u6307\u6570\u8907\u96d1\u5ea6\uff08\u518d\u5e30\u5b9f\u88c5\uff09\"\"\"\n    if n == 1:\n        return 1\n    return exp_recur(n - 1) + exp_recur(n - 1) + 1\n
        time_complexity.cpp
        /* \u6307\u6570\u8a08\u7b97\u91cf\uff08\u518d\u5e30\u5b9f\u88c5\uff09 */\nint expRecur(int n) {\n    if (n == 1)\n        return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
        time_complexity.java
        /* \u6307\u6570\u8a08\u7b97\u91cf\uff08\u518d\u5e30\u5b9f\u88c5\uff09 */\nint expRecur(int n) {\n    if (n == 1)\n        return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
        time_complexity.cs
        [class]{time_complexity}-[func]{ExpRecur}\n
        time_complexity.go
        [class]{}-[func]{expRecur}\n
        time_complexity.swift
        [class]{}-[func]{expRecur}\n
        time_complexity.js
        [class]{}-[func]{expRecur}\n
        time_complexity.ts
        [class]{}-[func]{expRecur}\n
        time_complexity.dart
        [class]{}-[func]{expRecur}\n
        time_complexity.rs
        [class]{}-[func]{exp_recur}\n
        time_complexity.c
        [class]{}-[func]{expRecur}\n
        time_complexity.kt
        [class]{}-[func]{expRecur}\n
        time_complexity.rb
        [class]{}-[func]{exp_recur}\n

        \u6307\u6570\u30aa\u30fc\u30c0\u30fc\u306e\u5897\u52a0\u306f\u6975\u3081\u3066\u6025\u901f\u3067\u3001\u5168\u6570\u63a2\u7d22\u6cd5\uff08\u30d6\u30eb\u30fc\u30c8\u30d5\u30a9\u30fc\u30b9\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306a\u3069\uff09\u3067\u3088\u304f\u898b\u3089\u308c\u307e\u3059\u3002\u5927\u898f\u6a21\u554f\u984c\u3067\u306f\u3001\u6307\u6570\u30aa\u30fc\u30c0\u30fc\u306f\u53d7\u3051\u5165\u308c\u3089\u308c\u305a\u3001\u3057\u3070\u3057\u3070\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3084\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u89e3\u6c7a\u7b56\u3068\u3057\u3066\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#5-olog-n","title":"5. \u00a0 \u5bfe\u6570\u30aa\u30fc\u30c0\u30fc \\(O(\\log n)\\)","text":"

        \u6307\u6570\u30aa\u30fc\u30c0\u30fc\u3068\u306f\u5bfe\u7167\u7684\u306b\u3001\u5bfe\u6570\u30aa\u30fc\u30c0\u30fc\u306f\u300c\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u30b5\u30a4\u30ba\u304c\u534a\u5206\u306b\u306a\u308b\u300d\u72b6\u6cc1\u3092\u53cd\u6620\u3057\u307e\u3059\u3002\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\u304c\\(n\\)\u306e\u5834\u5408\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u30b5\u30a4\u30ba\u304c\u534a\u5206\u306b\u306a\u308b\u305f\u3081\u3001\u53cd\u5fa9\u56de\u6570\u306f\\(\\log_2 n\\)\u3067\u3001\u3053\u308c\u306f\\(2^n\\)\u306e\u9006\u95a2\u6570\u3067\u3059\u3002

        \u4e0b\u56f3\u3068\u30b3\u30fc\u30c9\u306f\u300c\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u534a\u5206\u306b\u3059\u308b\u300d\u30d7\u30ed\u30bb\u30b9\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\u3057\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(\\log_2 n)\\)\u3067\u3001\u4e00\u822c\u7684\u306b\\(O(\\log n)\\)\u3068\u7701\u7565\u3055\u308c\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def logarithmic(n: int) -> int:\n    \"\"\"\u5bfe\u6570\u8907\u96d1\u5ea6\uff08\u30eb\u30fc\u30d7\u5b9f\u88c5\uff09\"\"\"\n    count = 0\n    while n > 1:\n        n = n / 2\n        count += 1\n    return count\n
        time_complexity.cpp
        /* \u5bfe\u6570\u8a08\u7b97\u91cf\uff08\u30eb\u30fc\u30d7\u5b9f\u88c5\uff09 */\nint logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u5bfe\u6570\u8a08\u7b97\u91cf\uff08\u30eb\u30fc\u30d7\u5b9f\u88c5\uff09 */\nint logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
        time_complexity.cs
        [class]{time_complexity}-[func]{Logarithmic}\n
        time_complexity.go
        [class]{}-[func]{logarithmic}\n
        time_complexity.swift
        [class]{}-[func]{logarithmic}\n
        time_complexity.js
        [class]{}-[func]{logarithmic}\n
        time_complexity.ts
        [class]{}-[func]{logarithmic}\n
        time_complexity.dart
        [class]{}-[func]{logarithmic}\n
        time_complexity.rs
        [class]{}-[func]{logarithmic}\n
        time_complexity.c
        [class]{}-[func]{logarithmic}\n
        time_complexity.kt
        [class]{}-[func]{logarithmic}\n
        time_complexity.rb
        [class]{}-[func]{logarithmic}\n

        \u56f3 2-12 \u00a0 Logarithmic order time complexity

        \u6307\u6570\u30aa\u30fc\u30c0\u30fc\u3068\u540c\u69d8\u306b\u3001\u5bfe\u6570\u30aa\u30fc\u30c0\u30fc\u3082\u518d\u5e30\u95a2\u6570\u3067\u983b\u7e41\u306b\u73fe\u308c\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u306f\u9ad8\u3055\\(\\log_2 n\\)\u306e\u518d\u5e30\u6728\u3092\u5f62\u6210\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def log_recur(n: int) -> int:\n    \"\"\"\u5bfe\u6570\u8907\u96d1\u5ea6\uff08\u518d\u5e30\u5b9f\u88c5\uff09\"\"\"\n    if n <= 1:\n        return 0\n    return log_recur(n / 2) + 1\n
        time_complexity.cpp
        /* \u5bfe\u6570\u8a08\u7b97\u91cf\uff08\u518d\u5e30\u5b9f\u88c5\uff09 */\nint logRecur(int n) {\n    if (n <= 1)\n        return 0;\n    return logRecur(n / 2) + 1;\n}\n
        time_complexity.java
        /* \u5bfe\u6570\u8a08\u7b97\u91cf\uff08\u518d\u5e30\u5b9f\u88c5\uff09 */\nint logRecur(int n) {\n    if (n <= 1)\n        return 0;\n    return logRecur(n / 2) + 1;\n}\n
        time_complexity.cs
        [class]{time_complexity}-[func]{LogRecur}\n
        time_complexity.go
        [class]{}-[func]{logRecur}\n
        time_complexity.swift
        [class]{}-[func]{logRecur}\n
        time_complexity.js
        [class]{}-[func]{logRecur}\n
        time_complexity.ts
        [class]{}-[func]{logRecur}\n
        time_complexity.dart
        [class]{}-[func]{logRecur}\n
        time_complexity.rs
        [class]{}-[func]{log_recur}\n
        time_complexity.c
        [class]{}-[func]{logRecur}\n
        time_complexity.kt
        [class]{}-[func]{logRecur}\n
        time_complexity.rb
        [class]{}-[func]{log_recur}\n

        \u5bfe\u6570\u30aa\u30fc\u30c0\u30fc\u306f\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306b\u57fa\u3065\u304f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5178\u578b\u3067\u3001\u300c\u591a\u304f\u306b\u5206\u5272\u300d\u3068\u300c\u8907\u96d1\u306a\u554f\u984c\u3092\u5358\u7d14\u5316\u300d\u3059\u308b\u30a2\u30d7\u30ed\u30fc\u30c1\u3092\u4f53\u73fe\u3057\u3066\u3044\u307e\u3059\u3002\u5897\u52a0\u304c\u9045\u304f\u3001\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\u306e\u6b21\u306b\u6700\u3082\u7406\u60f3\u7684\u306a\u6642\u9593\u8a08\u7b97\u91cf\u3067\u3059\u3002

        \\(O(\\log n)\\)\u306e\u5e95\u306f\u4f55\u3067\u3059\u304b\uff1f

        \u6280\u8853\u7684\u306b\u306f\u3001\u300c\\(m\\)\u306b\u5206\u5272\u300d\u306f\u6642\u9593\u8a08\u7b97\u91cf\\(O(\\log_m n)\\)\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002\u5bfe\u6570\u306e\u5e95\u5909\u66f4\u516c\u5f0f\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u7570\u306a\u308b\u5bfe\u6570\u8907\u96d1\u5ea6\u3092\u7b49\u4fa1\u306b\u3067\u304d\u307e\u3059\uff1a

        \\[ O(\\log_m n) = O(\\log_k n / \\log_k m) = O(\\log_k n) \\]

        \u3053\u308c\u306f\u3001\u5e95\\(m\\)\u3092\u8907\u96d1\u5ea6\u306b\u5f71\u97ff\u3092\u4e0e\u3048\u308b\u3053\u3068\u306a\u304f\u5909\u66f4\u3067\u304d\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u3057\u3070\u3057\u3070\u5e95\\(m\\)\u3092\u7701\u7565\u3057\u3001\u5358\u306b\u5bfe\u6570\u30aa\u30fc\u30c0\u30fc\u3092\\(O(\\log n)\\)\u3068\u8868\u8a18\u3057\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#6-on-log-n","title":"6. \u00a0 \u7dda\u5f62\u5bfe\u6570\u30aa\u30fc\u30c0\u30fc \\(O(n \\log n)\\)","text":"

        \u7dda\u5f62\u5bfe\u6570\u30aa\u30fc\u30c0\u30fc\u306f\u30cd\u30b9\u30c8\u3057\u305f\u30eb\u30fc\u30d7\u3067\u3088\u304f\u73fe\u308c\u30012\u3064\u306e\u30eb\u30fc\u30d7\u306e\u8907\u96d1\u5ea6\u304c\u305d\u308c\u305e\u308c\\(O(\\log n)\\)\u3068\\(O(n)\\)\u3067\u3059\u3002\u95a2\u9023\u3059\u308b\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def linear_log_recur(n: int) -> int:\n    \"\"\"\u7dda\u5f62\u5bfe\u6570\u8907\u96d1\u5ea6\"\"\"\n    if n <= 1:\n        return 1\n    count: int = linear_log_recur(n // 2) + linear_log_recur(n // 2)\n    for _ in range(n):\n        count += 1\n    return count\n
        time_complexity.cpp
        /* \u7dda\u5f62\u5bfe\u6570\u8a08\u7b97\u91cf */\nint linearLogRecur(int n) {\n    if (n <= 1)\n        return 1;\n    int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u7dda\u5f62\u5bfe\u6570\u8a08\u7b97\u91cf */\nint linearLogRecur(int n) {\n    if (n <= 1)\n        return 1;\n    int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.cs
        [class]{time_complexity}-[func]{LinearLogRecur}\n
        time_complexity.go
        [class]{}-[func]{linearLogRecur}\n
        time_complexity.swift
        [class]{}-[func]{linearLogRecur}\n
        time_complexity.js
        [class]{}-[func]{linearLogRecur}\n
        time_complexity.ts
        [class]{}-[func]{linearLogRecur}\n
        time_complexity.dart
        [class]{}-[func]{linearLogRecur}\n
        time_complexity.rs
        [class]{}-[func]{linear_log_recur}\n
        time_complexity.c
        [class]{}-[func]{linearLogRecur}\n
        time_complexity.kt
        [class]{}-[func]{linearLogRecur}\n
        time_complexity.rb
        [class]{}-[func]{linear_log_recur}\n

        \u4e0b\u56f3\u306f\u7dda\u5f62\u5bfe\u6570\u30aa\u30fc\u30c0\u30fc\u304c\u3069\u306e\u3088\u3046\u306b\u751f\u6210\u3055\u308c\u308b\u304b\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u4e8c\u5206\u6728\u306e\u5404\u30ec\u30d9\u30eb\u306b\u306f\\(n\\)\u500b\u306e\u64cd\u4f5c\u304c\u3042\u308a\u3001\u6728\u306b\u306f\\(\\log_2 n + 1\\)\u30ec\u30d9\u30eb\u304c\u3042\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n \\log n)\\)\u306b\u306a\u308a\u307e\u3059\u3002

        \u56f3 2-13 \u00a0 Linear-logarithmic order time complexity

        \u4e3b\u6d41\u306e\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u901a\u5e38\\(O(n \\log n)\\)\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3092\u6301\u3061\u3001\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u3001\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u3001\u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\u306a\u3069\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#7-on","title":"7. \u00a0 \u968e\u4e57\u30aa\u30fc\u30c0\u30fc \\(O(n!)\\)","text":"

        \u968e\u4e57\u30aa\u30fc\u30c0\u30fc\u306f\u300c\u5168\u9806\u5217\u300d\u306e\u6570\u5b66\u554f\u984c\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002\\(n\\)\u500b\u306e\u7570\u306a\u308b\u8981\u7d20\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u53ef\u80fd\u306a\u9806\u5217\u306e\u7dcf\u6570\u306f\uff1a

        \\[ n! = n \\times (n - 1) \\times (n - 2) \\times \\dots \\times 2 \\times 1 \\]

        \u968e\u4e57\u306f\u901a\u5e38\u518d\u5e30\u3092\u4f7f\u7528\u3057\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3068\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u3001\u7b2c1\u30ec\u30d9\u30eb\u306f\\(n\\)\u500b\u306e\u5206\u5c90\u306b\u5206\u5272\u3055\u308c\u3001\u7b2c2\u30ec\u30d9\u30eb\u306f\\(n - 1\\)\u500b\u306e\u5206\u5c90\u306b\u5206\u5272\u3055\u308c\u3001\u7b2c\\(n\\)\u30ec\u30d9\u30eb\u5f8c\u306b\u505c\u6b62\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def factorial_recur(n: int) -> int:\n    \"\"\"\u968e\u4e57\u8907\u96d1\u5ea6\uff08\u518d\u5e30\u5b9f\u88c5\uff09\"\"\"\n    if n == 0:\n        return 1\n    count = 0\n    # 1\u3064\u304b\u3089n\u306b\u5206\u5c90\n    for _ in range(n):\n        count += factorial_recur(n - 1)\n    return count\n
        time_complexity.cpp
        /* \u968e\u4e57\u8a08\u7b97\u91cf\uff08\u518d\u5e30\u5b9f\u88c5\uff09 */\nint factorialRecur(int n) {\n    if (n == 0)\n        return 1;\n    int count = 0;\n    // 1\u304b\u3089 n \u306b\u5206\u88c2\n    for (int i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u968e\u4e57\u8a08\u7b97\u91cf\uff08\u518d\u5e30\u5b9f\u88c5\uff09 */\nint factorialRecur(int n) {\n    if (n == 0)\n        return 1;\n    int count = 0;\n    // 1\u304b\u3089 n \u306b\u5206\u88c2\n    for (int i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
        time_complexity.cs
        [class]{time_complexity}-[func]{FactorialRecur}\n
        time_complexity.go
        [class]{}-[func]{factorialRecur}\n
        time_complexity.swift
        [class]{}-[func]{factorialRecur}\n
        time_complexity.js
        [class]{}-[func]{factorialRecur}\n
        time_complexity.ts
        [class]{}-[func]{factorialRecur}\n
        time_complexity.dart
        [class]{}-[func]{factorialRecur}\n
        time_complexity.rs
        [class]{}-[func]{factorial_recur}\n
        time_complexity.c
        [class]{}-[func]{factorialRecur}\n
        time_complexity.kt
        [class]{}-[func]{factorialRecur}\n
        time_complexity.rb
        [class]{}-[func]{factorial_recur}\n

        \u56f3 2-14 \u00a0 Factorial order time complexity

        \u968e\u4e57\u30aa\u30fc\u30c0\u30fc\u306f\u6307\u6570\u30aa\u30fc\u30c0\u30fc\u3088\u308a\u3082\u3055\u3089\u306b\u901f\u304f\u5897\u52a0\u3059\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3088\u308a\u5927\u304d\u306a\\(n\\)\u5024\u3067\u306f\u53d7\u3051\u5165\u308c\u3089\u308c\u307e\u305b\u3093\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#235","title":"2.3.5 \u00a0 \u6700\u60aa\u3001\u6700\u826f\u3001\u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf","text":"

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6642\u9593\u52b9\u7387\u306f\u56fa\u5b9a\u3055\u308c\u3066\u3044\u306a\u3044\u3053\u3068\u304c\u591a\u304f\u3001\u5165\u529b\u30c7\u30fc\u30bf\u306e\u5206\u5e03\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002\u9577\u3055\\(n\\)\u306e\u914d\u5217nums\u304c\u3042\u308a\u3001\\(1\\)\u304b\u3089\\(n\\)\u307e\u3067\u306e\u6570\u3067\u69cb\u6210\u3055\u308c\u3001\u305d\u308c\u305e\u308c\u304c\u4e00\u5ea6\u3060\u3051\u73fe\u308c\u307e\u3059\u304c\u3001\u30e9\u30f3\u30c0\u30e0\u306b\u30b7\u30e3\u30c3\u30d5\u30eb\u3055\u308c\u305f\u9806\u5e8f\u3067\u3042\u308b\u3068\u3057\u307e\u3059\u3002\u30bf\u30b9\u30af\u306f\u8981\u7d20\\(1\\)\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\u3053\u3068\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u7d50\u8ad6\u3092\u5c0e\u3051\u307e\u3059\uff1a

        • nums = [?, ?, ..., 1]\u306e\u5834\u5408\u3001\u3064\u307e\u308a\u6700\u5f8c\u306e\u8981\u7d20\u304c\\(1\\)\u306e\u5834\u5408\u3001\u914d\u5217\u306e\u5b8c\u5168\u306a\u8d70\u67fb\u304c\u5fc5\u8981\u3067\u3001\u6700\u60aa\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\\(O(n)\\)\u3092\u9054\u6210\u3057\u307e\u3059\u3002
        • nums = [1, ?, ?, ...]\u306e\u5834\u5408\u3001\u3064\u307e\u308a\u6700\u521d\u306e\u8981\u7d20\u304c\\(1\\)\u306e\u5834\u5408\u3001\u914d\u5217\u306e\u9577\u3055\u306b\u95a2\u4fc2\u306a\u304f\u3001\u3055\u3089\u306a\u308b\u8d70\u67fb\u306f\u4e0d\u8981\u3067\u3001\u6700\u826f\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\\(\\Omega(1)\\)\u3092\u9054\u6210\u3057\u307e\u3059\u3002

        \u300c\u6700\u60aa\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\u300d\u306f\u6f38\u8fd1\u4e0a\u9650\u306b\u5bfe\u5fdc\u3057\u3001\u5927\u304d\u306a\\(O\\)\u8a18\u6cd5\u3067\u8868\u3055\u308c\u307e\u3059\u3002\u5bfe\u5fdc\u3057\u3066\u3001\u300c\u6700\u826f\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\u300d\u306f\u6f38\u8fd1\u4e0b\u9650\u306b\u5bfe\u5fdc\u3057\u3001\\(\\Omega\\)\u3067\u8868\u3055\u308c\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby worst_best_time_complexity.py
        def random_numbers(n: int) -> list[int]:\n    \"\"\"\u8981\u7d20 1, 2, ..., n \u3092\u542b\u3080\u914d\u5217\u3092\u751f\u6210\u3001\u9806\u5e8f\u306f\u30b7\u30e3\u30c3\u30d5\u30eb\"\"\"\n    # \u914d\u5217 nums = 1, 2, 3, ..., n \u3092\u751f\u6210\n    nums = [i for i in range(1, n + 1)]\n    # \u914d\u5217\u8981\u7d20\u3092\u30e9\u30f3\u30c0\u30e0\u306b\u30b7\u30e3\u30c3\u30d5\u30eb\n    random.shuffle(nums)\n    return nums\n\ndef find_one(nums: list[int]) -> int:\n    \"\"\"\u914d\u5217 nums \u3067\u6570\u5024 1 \u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22\"\"\"\n    for i in range(len(nums)):\n        # \u8981\u7d20 1 \u304c\u914d\u5217\u306e\u6700\u521d\u306b\u3042\u308b\u5834\u5408\u3001\u6700\u826f\u6642\u9593\u8a08\u7b97\u91cf O(1) \u3092\u9054\u6210\n        # \u8981\u7d20 1 \u304c\u914d\u5217\u306e\u6700\u5f8c\u306b\u3042\u308b\u5834\u5408\u3001\u6700\u60aa\u6642\u9593\u8a08\u7b97\u91cf O(n) \u3092\u9054\u6210\n        if nums[i] == 1:\n            return i\n    return -1\n
        worst_best_time_complexity.cpp
        /* \u8981\u7d20 {1, 2, ..., n} \u3092\u30e9\u30f3\u30c0\u30e0\u306b\u30b7\u30e3\u30c3\u30d5\u30eb\u3057\u305f\u914d\u5217\u3092\u751f\u6210 */\nvector<int> randomNumbers(int n) {\n    vector<int> nums(n);\n    // \u914d\u5217 nums = { 1, 2, 3, ..., n } \u3092\u751f\u6210\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // \u30b7\u30b9\u30c6\u30e0\u6642\u523b\u3092\u4f7f\u7528\u3057\u3066\u30e9\u30f3\u30c0\u30e0\u30b7\u30fc\u30c9\u3092\u751f\u6210\n    unsigned seed = chrono::system_clock::now().time_since_epoch().count();\n    // \u914d\u5217\u8981\u7d20\u3092\u30e9\u30f3\u30c0\u30e0\u306b\u30b7\u30e3\u30c3\u30d5\u30eb\n    shuffle(nums.begin(), nums.end(), default_random_engine(seed));\n    return nums;\n}\n\n/* \u914d\u5217 nums \u3067\u6570\u50241\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u898b\u3064\u3051\u308b */\nint findOne(vector<int> &nums) {\n    for (int i = 0; i < nums.size(); i++) {\n        // \u8981\u7d201\u304c\u914d\u5217\u306e\u5148\u982d\u306b\u3042\u308b\u5834\u5408\u3001\u6700\u826f\u6642\u9593\u8a08\u7b97\u91cf O(1) \u3092\u9054\u6210\n        // \u8981\u7d201\u304c\u914d\u5217\u306e\u672b\u5c3e\u306b\u3042\u308b\u5834\u5408\u3001\u6700\u60aa\u6642\u9593\u8a08\u7b97\u91cf O(n) \u3092\u9054\u6210\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
        worst_best_time_complexity.java
        /* \u8981\u7d20 {1, 2, ..., n} \u3092\u30e9\u30f3\u30c0\u30e0\u306b\u30b7\u30e3\u30c3\u30d5\u30eb\u3057\u305f\u914d\u5217\u3092\u751f\u6210 */\nint[] randomNumbers(int n) {\n    Integer[] nums = new Integer[n];\n    // \u914d\u5217 nums = { 1, 2, 3, ..., n } \u3092\u751f\u6210\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // \u914d\u5217\u8981\u7d20\u3092\u30e9\u30f3\u30c0\u30e0\u306b\u30b7\u30e3\u30c3\u30d5\u30eb\n    Collections.shuffle(Arrays.asList(nums));\n    // Integer[] -> int[]\n    int[] res = new int[n];\n    for (int i = 0; i < n; i++) {\n        res[i] = nums[i];\n    }\n    return res;\n}\n\n/* \u914d\u5217 nums \u3067\u6570\u50241\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u898b\u3064\u3051\u308b */\nint findOne(int[] nums) {\n    for (int i = 0; i < nums.length; i++) {\n        // \u8981\u7d201\u304c\u914d\u5217\u306e\u5148\u982d\u306b\u3042\u308b\u5834\u5408\u3001\u6700\u826f\u6642\u9593\u8a08\u7b97\u91cf O(1) \u3092\u9054\u6210\n        // \u8981\u7d201\u304c\u914d\u5217\u306e\u672b\u5c3e\u306b\u3042\u308b\u5834\u5408\u3001\u6700\u60aa\u6642\u9593\u8a08\u7b97\u91cf O(n) \u3092\u9054\u6210\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
        worst_best_time_complexity.cs
        [class]{worst_best_time_complexity}-[func]{RandomNumbers}\n\n[class]{worst_best_time_complexity}-[func]{FindOne}\n
        worst_best_time_complexity.go
        [class]{}-[func]{randomNumbers}\n\n[class]{}-[func]{findOne}\n
        worst_best_time_complexity.swift
        [class]{}-[func]{randomNumbers}\n\n[class]{}-[func]{findOne}\n
        worst_best_time_complexity.js
        [class]{}-[func]{randomNumbers}\n\n[class]{}-[func]{findOne}\n
        worst_best_time_complexity.ts
        [class]{}-[func]{randomNumbers}\n\n[class]{}-[func]{findOne}\n
        worst_best_time_complexity.dart
        [class]{}-[func]{randomNumbers}\n\n[class]{}-[func]{findOne}\n
        worst_best_time_complexity.rs
        [class]{}-[func]{random_numbers}\n\n[class]{}-[func]{find_one}\n
        worst_best_time_complexity.c
        [class]{}-[func]{randomNumbers}\n\n[class]{}-[func]{findOne}\n
        worst_best_time_complexity.kt
        [class]{}-[func]{randomNumbers}\n\n[class]{}-[func]{findOne}\n
        worst_best_time_complexity.rb
        [class]{}-[func]{random_numbers}\n\n[class]{}-[func]{find_one}\n

        \u6700\u826f\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\u306f\u5b9f\u969b\u306b\u306f\u307b\u3068\u3093\u3069\u4f7f\u7528\u3055\u308c\u306a\u3044\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u901a\u5e38\u306f\u975e\u5e38\u306b\u4f4e\u3044\u78ba\u7387\u3067\u306e\u307f\u9054\u6210\u53ef\u80fd\u3067\u3001\u8aa4\u89e3\u3092\u62db\u304f\u53ef\u80fd\u6027\u304c\u3042\u308b\u304b\u3089\u3067\u3059\u3002\u6700\u60aa\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\u306f\u3088\u308a\u5b9f\u7528\u7684\u3067\u3001\u52b9\u7387\u306e\u5b89\u5168\u5024\u3092\u63d0\u4f9b\u3057\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u81ea\u4fe1\u3092\u6301\u3063\u3066\u4f7f\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002

        \u4e0a\u8a18\u306e\u4f8b\u304b\u3089\u3001\u6700\u60aa\u30b1\u30fc\u30b9\u3068\u6700\u826f\u30b1\u30fc\u30b9\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\u4e21\u65b9\u3068\u3082\u300c\u7279\u6b8a\u306a\u30c7\u30fc\u30bf\u5206\u5e03\u300d\u4e0b\u3067\u306e\u307f\u767a\u751f\u3057\u3001\u767a\u751f\u78ba\u7387\u304c\u5c0f\u3055\u304f\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u52b9\u7387\u3092\u6b63\u78ba\u306b\u53cd\u6620\u3057\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308b\u3053\u3068\u304c\u660e\u3089\u304b\u3067\u3059\u3002\u5bfe\u7167\u7684\u306b\u3001\u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf\u306f\u30e9\u30f3\u30c0\u30e0\u5165\u529b\u30c7\u30fc\u30bf\u4e0b\u3067\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u53cd\u6620\u3067\u304d\u3001\\(\\Theta\\)\u8a18\u6cd5\u3067\u8868\u3055\u308c\u307e\u3059\u3002

        \u4e00\u90e8\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u306f\u3001\u30e9\u30f3\u30c0\u30e0\u30c7\u30fc\u30bf\u5206\u5e03\u4e0b\u3067\u306e\u5e73\u5747\u30b1\u30fc\u30b9\u3092\u7c21\u5358\u306b\u63a8\u5b9a\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u524d\u8ff0\u306e\u4f8b\u3067\u306f\u3001\u5165\u529b\u914d\u5217\u304c\u30b7\u30e3\u30c3\u30d5\u30eb\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u8981\u7d20\\(1\\)\u304c\u4efb\u610f\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u73fe\u308c\u308b\u78ba\u7387\u306f\u7b49\u3057\u3044\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5e73\u5747\u30eb\u30fc\u30d7\u6570\u306f\u914d\u5217\u9577\u3055\u306e\u534a\u5206\\(n / 2\\)\u3067\u3001\u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(\\Theta(n / 2) = \\Theta(n)\\)\u3067\u3059\u3002

        \u3057\u304b\u3057\u3001\u3088\u308a\u8907\u96d1\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf\u3092\u8a08\u7b97\u3059\u308b\u3053\u3068\u306f\u975e\u5e38\u306b\u56f0\u96e3\u3067\u3059\u3002\u30c7\u30fc\u30bf\u5206\u5e03\u4e0b\u3067\u306e\u5168\u4f53\u7684\u306a\u6570\u5b66\u7684\u671f\u5f85\u5024\u3092\u5206\u6790\u3059\u308b\u3053\u3068\u304c\u56f0\u96e3\u3060\u304b\u3089\u3067\u3059\u3002\u305d\u306e\u3088\u3046\u306a\u5834\u5408\u3001\u901a\u5e38\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u5224\u65ad\u3059\u308b\u57fa\u6e96\u3068\u3057\u3066\u6700\u60aa\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        \\(\\Theta\\)\u8a18\u53f7\u306f\u306a\u305c\u307b\u3068\u3093\u3069\u898b\u3089\u308c\u306a\u3044\u306e\u3067\u3059\u304b\uff1f

        \u304a\u305d\u3089\u304f\\(O\\)\u8a18\u6cd5\u304c\u3088\u308a\u4e00\u822c\u7684\u306b\u8a71\u3055\u308c\u308b\u305f\u3081\u3001\u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf\u3092\u8868\u3059\u305f\u3081\u306b\u3088\u304f\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u53b3\u5bc6\u306b\u8a00\u3048\u3070\u3001\u3053\u306e\u5b9f\u8df5\u306f\u6b63\u78ba\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u3053\u306e\u672c\u3084\u4ed6\u306e\u8cc7\u6599\u3067\u300c\u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf\\(O(n)\\)\u300d\u306e\u3088\u3046\u306a\u8868\u73fe\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u306f\u3001\u76f4\u63a5\\(\\Theta(n)\\)\u3068\u3057\u3066\u7406\u89e3\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        "},{"location":"chapter_data_structure/","title":"\u7b2c 3 \u7ae0 \u00a0 \u30c7\u30fc\u30bf\u69cb\u9020","text":"

        Abstract

        \u30c7\u30fc\u30bf\u69cb\u9020\u306f\u5805\u7262\u3067\u591a\u69d8\u306a\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3068\u3057\u3066\u6a5f\u80fd\u3057\u307e\u3059\u3002

        \u30c7\u30fc\u30bf\u306e\u6574\u7136\u3068\u3057\u305f\u7d44\u7e54\u5316\u306e\u305f\u3081\u306e\u8a2d\u8a08\u56f3\u3092\u63d0\u4f9b\u3057\u3001\u305d\u306e\u4e0a\u3067\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u751f\u304d\u751f\u304d\u3068\u52d5\u304d\u51fa\u3057\u307e\u3059\u3002

        "},{"location":"chapter_data_structure/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 3.1 \u00a0 \u30c7\u30fc\u30bf\u69cb\u9020\u306e\u5206\u985e
        • 3.2 \u00a0 \u57fa\u672c\u30c7\u30fc\u30bf\u578b
        • 3.3 \u00a0 \u6570\u5024\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0 *
        • 3.4 \u00a0 \u6587\u5b57\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0 *
        • 3.5 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_data_structure/basic_data_types/","title":"3.2 \u00a0 \u57fa\u672c\u30c7\u30fc\u30bf\u578b","text":"

        \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u5185\u306e\u30c7\u30fc\u30bf\u306b\u3064\u3044\u3066\u8003\u3048\u308b\u969b\u3001\u30c6\u30ad\u30b9\u30c8\u3001\u753b\u50cf\u3001\u52d5\u753b\u3001\u97f3\u58f0\u30013D\u30e2\u30c7\u30eb\u306a\u3069\u3001\u69d8\u3005\u306a\u5f62\u5f0f\u304c\u601d\u3044\u6d6e\u304b\u3073\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u7d44\u7e54\u7684\u306a\u5f62\u5f0f\u306f\u7570\u306a\u308a\u307e\u3059\u304c\u3001\u3059\u3079\u3066\u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u304b\u3089\u69cb\u6210\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        **\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3068\u306f\u3001CPU\u304c\u76f4\u63a5\u64cd\u4f5c\u3067\u304d\u308b\u3082\u306e**\u3067\u3042\u308a\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u76f4\u63a5\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u4e3b\u306b\u4ee5\u4e0b\u304c\u542b\u307e\u308c\u307e\u3059\u3002

        • \u6574\u6570\u578b\uff1abyte\u3001short\u3001int\u3001long
        • \u6d6e\u52d5\u5c0f\u6570\u70b9\u578b\uff1afloat\u3001double\u3001\u5c0f\u6570\u3092\u8868\u73fe\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528
        • \u6587\u5b57\u578b\uff1achar\u3001\u69d8\u3005\u306a\u8a00\u8a9e\u306e\u6587\u5b57\u3001\u53e5\u8aad\u70b9\u3001\u3055\u3089\u306b\u306f\u7d75\u6587\u5b57\u3092\u8868\u73fe\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528
        • \u30d6\u30fc\u30eb\u578b\uff1abool\u3001\u300c\u306f\u3044\u300d\u307e\u305f\u306f\u300c\u3044\u3044\u3048\u300d\u306e\u5224\u65ad\u3092\u8868\u73fe\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528

        \u57fa\u672c\u30c7\u30fc\u30bf\u578b\u306f\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u5185\u3067\u4e8c\u9032\u5f62\u5f0f\u3067\u683c\u7d0d\u3055\u308c\u307e\u3059\u30021\u3064\u306e\u4e8c\u9032\u6841\u306f1\u30d3\u30c3\u30c8\u3067\u3059\u3002\u307b\u3068\u3093\u3069\u306e\u73fe\u4ee3\u7684\u306a\u30aa\u30da\u30ec\u30fc\u30c6\u30a3\u30f3\u30b0\u30b7\u30b9\u30c6\u30e0\u3067\u306f\u30011\u30d0\u30a4\u30c8\u306f8\u30d3\u30c3\u30c8\u3067\u69cb\u6210\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        \u57fa\u672c\u30c7\u30fc\u30bf\u578b\u306e\u5024\u306e\u7bc4\u56f2\u306f\u3001\u305d\u308c\u3089\u304c\u5360\u3081\u308b\u7a7a\u9593\u306e\u30b5\u30a4\u30ba\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002\u4ee5\u4e0b\u3067\u306f\u3001Java\u3092\u4f8b\u306b\u8aac\u660e\u3057\u307e\u3059\u3002

        • \u6574\u6570\u578bbyte\u306f1\u30d0\u30a4\u30c8 = 8\u30d3\u30c3\u30c8\u3092\u5360\u3081\u3001\\(2^8\\)\u500b\u306e\u6570\u5024\u3092\u8868\u73fe\u3067\u304d\u307e\u3059\u3002
        • \u6574\u6570\u578bint\u306f4\u30d0\u30a4\u30c8 = 32\u30d3\u30c3\u30c8\u3092\u5360\u3081\u3001\\(2^{32}\\)\u500b\u306e\u6570\u5024\u3092\u8868\u73fe\u3067\u304d\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u8868\u306f\u3001Java\u306b\u304a\u3051\u308b\u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u304c\u5360\u3081\u308b\u7a7a\u9593\u3001\u5024\u306e\u7bc4\u56f2\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u8868\u3092\u6697\u8a18\u3059\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\u304c\u3001\u4e00\u822c\u7684\u306a\u7406\u89e3\u3092\u6301\u3061\u3001\u5fc5\u8981\u6642\u306b\u53c2\u7167\u3059\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002

        \u8868 3-1 \u00a0 \u57fa\u672c\u30c7\u30fc\u30bf\u578b\u304c\u5360\u3081\u308b\u7a7a\u9593\u3068\u5024\u306e\u7bc4\u56f2

        \u578b \u30b7\u30f3\u30dc\u30eb \u5360\u6709\u7a7a\u9593 \u6700\u5c0f\u5024 \u6700\u5927\u5024 \u30c7\u30d5\u30a9\u30eb\u30c8\u5024 \u6574\u6570 byte 1\u30d0\u30a4\u30c8 \\(-2^7\\) (\\(-128\\)) \\(2^7 - 1\\) (\\(127\\)) 0 short 2\u30d0\u30a4\u30c8 \\(-2^{15}\\) \\(2^{15} - 1\\) 0 int 4\u30d0\u30a4\u30c8 \\(-2^{31}\\) \\(2^{31} - 1\\) 0 long 8\u30d0\u30a4\u30c8 \\(-2^{63}\\) \\(2^{63} - 1\\) 0 \u6d6e\u52d5\u5c0f\u6570\u70b9 float 4\u30d0\u30a4\u30c8 \\(1.175 \\times 10^{-38}\\) \\(3.403 \\times 10^{38}\\) \\(0.0\\text{f}\\) double 8\u30d0\u30a4\u30c8 \\(2.225 \\times 10^{-308}\\) \\(1.798 \\times 10^{308}\\) 0.0 \u6587\u5b57 char 2\u30d0\u30a4\u30c8 0 \\(2^{16} - 1\\) 0 \u30d6\u30fc\u30eb bool 1\u30d0\u30a4\u30c8 \\(\\text{false}\\) \\(\\text{true}\\) \\(\\text{false}\\)

        \u4e0a\u8a18\u306e\u8868\u306fJava\u306e\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u306b\u7279\u6709\u3067\u3042\u308b\u3053\u3068\u306b\u3054\u6ce8\u610f\u304f\u3060\u3055\u3044\u3002\u3059\u3079\u3066\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u306f\u72ec\u81ea\u306e\u30c7\u30fc\u30bf\u578b\u5b9a\u7fa9\u304c\u3042\u308a\u3001\u5360\u6709\u7a7a\u9593\u3001\u5024\u306e\u7bc4\u56f2\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u304c\u7570\u306a\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002

        • Python\u3067\u306f\u3001\u6574\u6570\u578bint\u306f\u4efb\u610f\u306e\u30b5\u30a4\u30ba\u306b\u306a\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u5229\u7528\u53ef\u80fd\u306a\u30e1\u30e2\u30ea\u306b\u3088\u3063\u3066\u306e\u307f\u5236\u9650\u3055\u308c\u307e\u3059\u3002\u6d6e\u52d5\u5c0f\u6570\u70b9float\u306f\u500d\u7cbe\u5ea664\u30d3\u30c3\u30c8\u3067\u3059\u3002char\u578b\u306f\u5b58\u5728\u305b\u305a\u3001\u5358\u4e00\u6587\u5b57\u306f\u5b9f\u969b\u306b\u306f\u9577\u30551\u306e\u6587\u5b57\u5217str\u3067\u3059\u3002
        • C\u304a\u3088\u3073C++\u3067\u306f\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u306e\u30b5\u30a4\u30ba\u304c\u6307\u5b9a\u3055\u308c\u3066\u304a\u3089\u305a\u3001\u5b9f\u88c5\u3068\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u306b\u3088\u3063\u3066\u7570\u306a\u308a\u307e\u3059\u3002\u4e0a\u8a18\u306e\u8868\u306fLP64\u30c7\u30fc\u30bf\u30e2\u30c7\u30eb\u306b\u5f93\u3063\u3066\u304a\u308a\u3001Linux\u3084macOS\u3092\u542b\u3080Unix 64\u30d3\u30c3\u30c8\u30aa\u30da\u30ec\u30fc\u30c6\u30a3\u30f3\u30b0\u30b7\u30b9\u30c6\u30e0\u3067\u4f7f\u7528\u3055\u308c\u3066\u3044\u307e\u3059\u3002
        • C\u304a\u3088\u3073C++\u306b\u304a\u3051\u308bchar\u306e\u30b5\u30a4\u30ba\u306f1\u30d0\u30a4\u30c8\u3067\u3059\u304c\u3001\u307b\u3068\u3093\u3069\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u306f\u3001\u7279\u5b9a\u306e\u6587\u5b57\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u65b9\u6cd5\u306b\u4f9d\u5b58\u3057\u3001\u8a73\u7d30\u306f\u300c\u6587\u5b57\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u300d\u306e\u7ae0\u3067\u8aac\u660e\u3055\u308c\u3066\u3044\u307e\u3059\u3002
        • \u30d6\u30fc\u30eb\u5024\u306e\u8868\u73fe\u306b\u306f1\u30d3\u30c3\u30c8\uff080\u307e\u305f\u306f1\uff09\u306e\u307f\u304c\u5fc5\u8981\u3067\u3059\u304c\u3001\u901a\u5e38\u306f\u30e1\u30e2\u30ea\u5185\u306b1\u30d0\u30a4\u30c8\u3068\u3057\u3066\u683c\u7d0d\u3055\u308c\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u73fe\u4ee3\u306e\u30b3\u30f3\u30d4\u30e5\u30fc\u30bfCPU\u304c\u901a\u5e381\u30d0\u30a4\u30c8\u3092\u6700\u5c0f\u306e\u30a2\u30c9\u30ec\u30b9\u53ef\u80fd\u306a\u30e1\u30e2\u30ea\u5358\u4f4d\u3068\u3057\u3066\u4f7f\u7528\u3059\u308b\u305f\u3081\u3067\u3059\u3002

        \u3067\u306f\u3001\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3068\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u95a2\u4fc2\u306f\u4f55\u3067\u3057\u3087\u3046\u304b\uff1f\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u306f\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u5185\u3067\u30c7\u30fc\u30bf\u3092\u7d44\u7e54\u5316\u3057\u683c\u7d0d\u3059\u308b\u65b9\u6cd5\u3067\u3042\u308b\u3053\u3068\u3092\u77e5\u3063\u3066\u3044\u307e\u3059\u3002\u3053\u3053\u3067\u306e\u7126\u70b9\u306f\u300c\u30c7\u30fc\u30bf\u300d\u3067\u306f\u306a\u304f\u300c\u69cb\u9020\u300d\u3067\u3059\u3002

        \u300c\u6570\u5024\u306e\u5217\u300d\u3092\u8868\u73fe\u3057\u305f\u3044\u5834\u5408\u3001\u81ea\u7136\u306b\u914d\u5217\u306e\u4f7f\u7528\u3092\u8003\u3048\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u914d\u5217\u306e\u7dda\u5f62\u69cb\u9020\u304c\u6570\u5024\u306e\u96a3\u63a5\u6027\u3068\u9806\u5e8f\u6027\u3092\u8868\u73fe\u3067\u304d\u308b\u305f\u3081\u3067\u3059\u304c\u3001\u683c\u7d0d\u3055\u308c\u308b\u5185\u5bb9\u304c\u6574\u6570int\u3001\u5c0f\u6570float\u3001\u6587\u5b57char\u306e\u3044\u305a\u308c\u3067\u3042\u3063\u3066\u3082\u3001\u300c\u30c7\u30fc\u30bf\u69cb\u9020\u300d\u3068\u306f\u7121\u95a2\u4fc2\u3067\u3059\u3002

        \u8a00\u3044\u63db\u3048\u308b\u3068\u3001\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u306f\u30c7\u30fc\u30bf\u306e\u300c\u5185\u5bb9\u578b\u300d\u3092\u63d0\u4f9b\u3057\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u30c7\u30fc\u30bf\u306e\u300c\u7d44\u7e54\u5316\u65b9\u6cd5\u300d\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3001\u540c\u3058\u30c7\u30fc\u30bf\u69cb\u9020\uff08\u914d\u5217\uff09\u3092\u4f7f\u7528\u3057\u3066\u3001int\u3001float\u3001char\u3001bool\u306a\u3069\u306e\u7570\u306a\u308b\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3092\u683c\u7d0d\u3057\u8868\u73fe\u3057\u3066\u3044\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin
        # \u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u3092\u521d\u671f\u5316\nnumbers: list[int] = [0] * 5\ndecimals: list[float] = [0.0] * 5\n# Python\u306e\u6587\u5b57\u306f\u5b9f\u969b\u306b\u306f\u9577\u30551\u306e\u6587\u5b57\u5217\ncharacters: list[str] = ['0'] * 5\nbools: list[bool] = [False] * 5\n# Python\u306e\u30ea\u30b9\u30c8\u306f\u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3068\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u53c2\u7167\u3092\u81ea\u7531\u306b\u683c\u7d0d\u53ef\u80fd\ndata = [0, 0.0, 'a', False, ListNode(0)]\n
        // \u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u3092\u521d\u671f\u5316\nint numbers[5];\nfloat decimals[5];\nchar characters[5];\nbool bools[5];\n
        // \u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u3092\u521d\u671f\u5316\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nboolean[] bools = new boolean[5];\n
        // \u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u3092\u521d\u671f\u5316\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nbool[] bools = new bool[5];\n
        // \u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u3092\u521d\u671f\u5316\nvar numbers = [5]int{}\nvar decimals = [5]float64{}\nvar characters = [5]byte{}\nvar bools = [5]bool{}\n
        // \u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u3092\u521d\u671f\u5316\nlet numbers = Array(repeating: 0, count: 5)\nlet decimals = Array(repeating: 0.0, count: 5)\nlet characters: [Character] = Array(repeating: \"a\", count: 5)\nlet bools = Array(repeating: false, count: 5)\n
        // JavaScript\u306e\u914d\u5217\u306f\u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3068\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u81ea\u7531\u306b\u683c\u7d0d\u53ef\u80fd\nconst array = [0, 0.0, 'a', false];\n
        // \u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u3092\u521d\u671f\u5316\nconst numbers: number[] = [];\nconst characters: string[] = [];\nconst bools: boolean[] = [];\n
        // \u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u3092\u521d\u671f\u5316\nList<int> numbers = List.filled(5, 0);\nList<double> decimals = List.filled(5, 0.0);\nList<String> characters = List.filled(5, 'a');\nList<bool> bools = List.filled(5, false);\n
        // \u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u3092\u521d\u671f\u5316\nlet numbers: Vec<i32> = vec![0; 5];\nlet decimals: Vec<f32> = vec![0.0, 5];\nlet characters: Vec<char> = vec!['0'; 5];\nlet bools: Vec<bool> = vec![false; 5];\n
        // \u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u3092\u521d\u671f\u5316\nint numbers[10];\nfloat decimals[10];\nchar characters[10];\nbool bools[10];\n
        \n
        "},{"location":"chapter_data_structure/character_encoding/","title":"3.4 \u00a0 \u6587\u5b57\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0 *","text":"

        \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30b7\u30b9\u30c6\u30e0\u3067\u306f\u3001\u3059\u3079\u3066\u306e\u30c7\u30fc\u30bf\u304c\u4e8c\u9032\u5f62\u5f0f\u3067\u683c\u7d0d\u3055\u308c\u3001char\u3082\u4f8b\u5916\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u6587\u5b57\u3092\u8868\u73fe\u3059\u308b\u305f\u3081\u306b\u3001\u5404\u6587\u5b57\u3068\u4e8c\u9032\u6570\u306e\u4e00\u5bfe\u4e00\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u3092\u5b9a\u7fa9\u3059\u308b\u300c\u6587\u5b57\u30bb\u30c3\u30c8\u300d\u3092\u958b\u767a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u6587\u5b57\u30bb\u30c3\u30c8\u304c\u3042\u308c\u3070\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306f\u8868\u3092\u53c2\u7167\u3057\u3066\u4e8c\u9032\u6570\u3092\u6587\u5b57\u306b\u5909\u63db\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_data_structure/character_encoding/#341-ascii","title":"3.4.1 \u00a0 ASCII\u6587\u5b57\u30bb\u30c3\u30c8","text":"

        ASCII\u30b3\u30fc\u30c9\u306f\u6700\u3082\u521d\u671f\u306e\u6587\u5b57\u30bb\u30c3\u30c8\u306e\u4e00\u3064\u3067\u3001\u6b63\u5f0f\u306b\u306fAmerican Standard Code for Information Interchange\u3068\u3057\u3066\u77e5\u3089\u308c\u3066\u3044\u307e\u3059\u30027\u3064\u306e\u4e8c\u9032\u6841\uff081\u30d0\u30a4\u30c8\u306e\u4e0b\u4f4d7\u30d3\u30c3\u30c8\uff09\u3092\u4f7f\u7528\u3057\u3066\u6587\u5b57\u3092\u8868\u73fe\u3057\u3001\u6700\u5927128\u7a2e\u985e\u306e\u7570\u306a\u308b\u6587\u5b57\u3092\u8868\u73fe\u3067\u304d\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001ASCII\u306b\u306f\u82f1\u8a9e\u306e\u5927\u6587\u5b57\u3068\u5c0f\u6587\u5b57\u30010\u301c9\u306e\u6570\u5b57\u3001\u69d8\u3005\u306a\u53e5\u8aad\u70b9\u3001\u7279\u5b9a\u306e\u5236\u5fa1\u6587\u5b57\uff08\u6539\u884c\u3084\u30bf\u30d6\u306a\u3069\uff09\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002

        \u56f3 3-6 \u00a0 ASCII\u30b3\u30fc\u30c9

        \u3057\u304b\u3057\u3001ASCII\u306f\u82f1\u8a9e\u306e\u6587\u5b57\u306e\u307f\u3092\u8868\u73fe\u3067\u304d\u307e\u3059\u3002\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u30b0\u30ed\u30fc\u30d0\u30eb\u5316\u306b\u4f34\u3044\u3001\u3088\u308a\u591a\u304f\u306e\u8a00\u8a9e\u3092\u8868\u73fe\u3059\u308b\u305f\u3081\u306bEASCII\u3068\u547c\u3070\u308c\u308b\u6587\u5b57\u30bb\u30c3\u30c8\u304c\u958b\u767a\u3055\u308c\u307e\u3057\u305f\u3002ASCII\u306e7\u30d3\u30c3\u30c8\u69cb\u9020\u304b\u30898\u30d3\u30c3\u30c8\u306b\u62e1\u5f35\u3057\u3001256\u6587\u5b57\u306e\u8868\u73fe\u3092\u53ef\u80fd\u306b\u3057\u307e\u3057\u305f\u3002

        \u4e16\u754c\u7684\u306b\u3001\u69d8\u3005\u306a\u5730\u57df\u56fa\u6709\u306eEASCII\u6587\u5b57\u30bb\u30c3\u30c8\u304c\u5c0e\u5165\u3055\u308c\u307e\u3057\u305f\u3002\u3053\u308c\u3089\u306e\u30bb\u30c3\u30c8\u306e\u6700\u521d\u306e128\u6587\u5b57\u306fASCII\u3068\u4e00\u81f4\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u6b8b\u308a\u306e128\u6587\u5b57\u306f\u7570\u306a\u308b\u8a00\u8a9e\u306e\u8981\u4ef6\u306b\u5bfe\u5fdc\u3059\u308b\u305f\u3081\u306b\u7570\u306a\u3063\u3066\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_data_structure/character_encoding/#342-gbk","title":"3.4.2 \u00a0 GBK\u6587\u5b57\u30bb\u30c3\u30c8","text":"

        \u5f8c\u306b\u3001**EASCII\u3067\u3082\u591a\u304f\u306e\u8a00\u8a9e\u306e\u6587\u5b57\u8981\u4ef6\u3092\u6e80\u305f\u3059\u3053\u3068\u304c\u3067\u304d\u306a\u3044**\u3053\u3068\u304c\u5224\u660e\u3057\u307e\u3057\u305f\u3002\u4f8b\u3048\u3070\u3001\u4e2d\u56fd\u8a9e\u306b\u306f\u7d0410\u4e07\u306e\u6f22\u5b57\u304c\u3042\u308a\u3001\u305d\u306e\u3046\u3061\u6570\u5343\u304c\u5b9a\u671f\u7684\u306b\u4f7f\u7528\u3055\u308c\u3066\u3044\u307e\u3059\u30021980\u5e74\u3001\u4e2d\u56fd\u6a19\u6e96\u5316\u59d4\u54e1\u4f1a\u306f6763\u306e\u4e2d\u56fd\u8a9e\u6587\u5b57\u3092\u542b\u3080GB2312\u6587\u5b57\u30bb\u30c3\u30c8\u3092\u767a\u8868\u3057\u3001\u4e2d\u56fd\u8a9e\u306e\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u51e6\u7406\u30cb\u30fc\u30ba\u3092\u672c\u8cea\u7684\u306b\u6e80\u305f\u3057\u307e\u3057\u305f\u3002

        \u3057\u304b\u3057\u3001GB2312\u306f\u4e00\u90e8\u306e\u7a00\u5c11\u6587\u5b57\u3084\u7e41\u4f53\u5b57\u3092\u51e6\u7406\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002GBK\u6587\u5b57\u30bb\u30c3\u30c8\u306fGB2312\u3092\u62e1\u5f35\u3057\u300121886\u306e\u4e2d\u56fd\u8a9e\u6587\u5b57\u3092\u542b\u3093\u3067\u3044\u307e\u3059\u3002GBK\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u30b9\u30ad\u30fc\u30e0\u3067\u306f\u3001ASCII\u6587\u5b57\u306f1\u30d0\u30a4\u30c8\u3067\u8868\u73fe\u3055\u308c\u3001\u4e2d\u56fd\u8a9e\u6587\u5b57\u306f2\u30d0\u30a4\u30c8\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        "},{"location":"chapter_data_structure/character_encoding/#343-unicode","title":"3.4.3 \u00a0 Unicode\u6587\u5b57\u30bb\u30c3\u30c8","text":"

        \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u6280\u8853\u306e\u6025\u901f\u306a\u767a\u5c55\u3068\u591a\u6570\u306e\u6587\u5b57\u30bb\u30c3\u30c8\u304a\u3088\u3073\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u6a19\u6e96\u306b\u3088\u308a\u3001\u6570\u591a\u304f\u306e\u554f\u984c\u304c\u767a\u751f\u3057\u307e\u3057\u305f\u3002\u4e00\u65b9\u3067\u306f\u3001\u3053\u308c\u3089\u306e\u6587\u5b57\u30bb\u30c3\u30c8\u306f\u4e00\u822c\u7684\u306b\u7279\u5b9a\u306e\u8a00\u8a9e\u306e\u6587\u5b57\u306e\u307f\u3092\u5b9a\u7fa9\u3057\u3001\u591a\u8a00\u8a9e\u74b0\u5883\u3067\u306f\u9069\u5207\u306b\u6a5f\u80fd\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002\u4ed6\u65b9\u3067\u306f\u3001\u540c\u3058\u8a00\u8a9e\u306b\u5bfe\u3059\u308b\u8907\u6570\u306e\u6587\u5b57\u30bb\u30c3\u30c8\u6a19\u6e96\u306e\u5b58\u5728\u306b\u3088\u308a\u3001\u7570\u306a\u308b\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u6a19\u6e96\u3092\u4f7f\u7528\u3059\u308b\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u9593\u3067\u60c5\u5831\u4ea4\u63db\u3092\u884c\u3046\u969b\u306b\u6587\u5b57\u5316\u3051\u304c\u767a\u751f\u3057\u307e\u3057\u305f\u3002

        \u5f53\u6642\u306e\u7814\u7a76\u8005\u305f\u3061\u306f\u8003\u3048\u307e\u3057\u305f\uff1a\u4e16\u754c\u306e\u3059\u3079\u3066\u306e\u8a00\u8a9e\u3068\u8a18\u53f7\u3092\u542b\u3080\u5305\u62ec\u7684\u306a\u6587\u5b57\u30bb\u30c3\u30c8\u304c\u958b\u767a\u3055\u308c\u308c\u3070\u3001\u8a00\u8a9e\u6a2a\u65ad\u74b0\u5883\u3068\u6587\u5b57\u5316\u3051\u306b\u95a2\u9023\u3059\u308b\u554f\u984c\u3092\u89e3\u6c7a\u3067\u304d\u308b\u306e\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046\u304b\uff1f \u3053\u306e\u30a2\u30a4\u30c7\u30a2\u306b\u30a4\u30f3\u30b9\u30d1\u30a4\u30a2\u3055\u308c\u3066\u3001\u5e83\u7bc4\u56f2\u306a\u6587\u5b57\u30bb\u30c3\u30c8\u3067\u3042\u308bUnicode\u304c\u8a95\u751f\u3057\u307e\u3057\u305f\u3002

        Unicode\u306f\u4e2d\u56fd\u8a9e\u3067\u300c\u7edf\u4e00\u7801\u300d\uff08\u7d71\u4e00\u30b3\u30fc\u30c9\uff09\u3068\u547c\u3070\u308c\u3001\u7406\u8ad6\u7684\u306b100\u4e07\u6587\u5b57\u4ee5\u4e0a\u3092\u53ce\u5bb9\u3067\u304d\u307e\u3059\u3002\u4e16\u754c\u4e2d\u306e\u3059\u3079\u3066\u306e\u6587\u5b57\u3092\u5358\u4e00\u306e\u30bb\u30c3\u30c8\u306b\u7d44\u307f\u8fbc\u307f\u3001\u69d8\u3005\u306a\u8a00\u8a9e\u306e\u51e6\u7406\u3068\u8868\u793a\u306e\u305f\u3081\u306e\u6c4e\u7528\u6587\u5b57\u30bb\u30c3\u30c8\u3092\u63d0\u4f9b\u3057\u3001\u7570\u306a\u308b\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u6a19\u6e96\u306b\u3088\u308b\u6587\u5b57\u5316\u3051\u306e\u554f\u984c\u3092\u6e1b\u3089\u3059\u3053\u3068\u3092\u76ee\u6307\u3057\u3066\u3044\u307e\u3059\u3002

        1991\u5e74\u306e\u30ea\u30ea\u30fc\u30b9\u4ee5\u6765\u3001Unicode\u306f\u65b0\u3057\u3044\u8a00\u8a9e\u3068\u6587\u5b57\u3092\u542b\u3080\u3088\u3046\u7d99\u7d9a\u7684\u306b\u62e1\u5f35\u3055\u308c\u3066\u3044\u307e\u3059\u30022022\u5e749\u6708\u73fe\u5728\u3001Unicode\u306b\u306f149,186\u6587\u5b57\u304c\u542b\u307e\u308c\u3066\u304a\u308a\u3001\u69d8\u3005\u306a\u8a00\u8a9e\u306e\u6587\u5b57\u3001\u8a18\u53f7\u3001\u3055\u3089\u306b\u306f\u7d75\u6587\u5b57\u3082\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002\u5e83\u5927\u306aUnicode\u6587\u5b57\u30bb\u30c3\u30c8\u3067\u306f\u3001\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u308b\u6587\u5b57\u306f2\u30d0\u30a4\u30c8\u3092\u5360\u6709\u3057\u3001\u4e00\u90e8\u306e\u7a00\u5c11\u306a\u6587\u5b57\u306f3\u30d0\u30a4\u30c8\u307e\u305f\u306f4\u30d0\u30a4\u30c8\u3092\u5360\u6709\u3059\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002

        Unicode\u306f\u5404\u6587\u5b57\u306b\u6570\u5024\uff08\u300c\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u300d\u3068\u547c\u3070\u308c\u308b\uff09\u3092\u5272\u308a\u5f53\u3066\u308b\u6c4e\u7528\u6587\u5b57\u30bb\u30c3\u30c8\u3067\u3059\u304c\u3001\u3053\u308c\u3089\u306e\u6587\u5b57\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u304c\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30b7\u30b9\u30c6\u30e0\u306b\u3069\u306e\u3088\u3046\u306b\u683c\u7d0d\u3055\u308c\u308b\u3079\u304d\u304b\u306f\u6307\u5b9a\u3057\u3066\u3044\u307e\u305b\u3093\u3002\u7591\u554f\u304c\u751f\u3058\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\uff1a\u30b7\u30b9\u30c6\u30e0\u306f\u30c6\u30ad\u30b9\u30c8\u5185\u306e\u7570\u306a\u308b\u9577\u3055\u306eUnicode\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u3092\u3069\u306e\u3088\u3046\u306b\u89e3\u91c8\u3059\u308b\u306e\u3067\u3057\u3087\u3046\u304b\uff1f\u4f8b\u3048\u3070\u30012\u30d0\u30a4\u30c8\u306e\u30b3\u30fc\u30c9\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u305d\u308c\u304c\u5358\u4e00\u306e2\u30d0\u30a4\u30c8\u6587\u5b57\u3092\u8868\u3059\u306e\u304b\u30012\u3064\u306e1\u30d0\u30a4\u30c8\u6587\u5b57\u3092\u8868\u3059\u306e\u304b\u3092\u3069\u306e\u3088\u3046\u306b\u5224\u65ad\u3059\u308b\u306e\u3067\u3057\u3087\u3046\u304b\uff1f

        \u3053\u306e\u554f\u984c\u306b\u5bfe\u3059\u308b\u7c21\u5358\u306a\u89e3\u6c7a\u7b56\u306f\u3001\u3059\u3079\u3066\u306e\u6587\u5b57\u3092\u7b49\u9577\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3068\u3057\u3066\u683c\u7d0d\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u300cHello\u300d\u306e\u5404\u6587\u5b57\u306f1\u30d0\u30a4\u30c8\u3092\u5360\u6709\u3057\u3001\u300c\u7b97\u6cd5\u300d\uff08\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff09\u306e\u5404\u6587\u5b57\u306f2\u30d0\u30a4\u30c8\u3092\u5360\u6709\u3057\u307e\u3059\u3002\u4e0a\u4f4d\u30d3\u30c3\u30c8\u3092\u30bc\u30ed\u3067\u57cb\u3081\u308b\u3053\u3068\u3067\u3001\u300cHello \u7b97\u6cd5\u300d\u306e\u3059\u3079\u3066\u306e\u6587\u5b57\u30922\u30d0\u30a4\u30c8\u3068\u3057\u3066\u30a8\u30f3\u30b3\u30fc\u30c9\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u65b9\u6cd5\u306b\u3088\u308a\u3001\u30b7\u30b9\u30c6\u30e0\u306f2\u30d0\u30a4\u30c8\u3054\u3068\u306b\u6587\u5b57\u3092\u89e3\u91c8\u3057\u3001\u30d5\u30ec\u30fc\u30ba\u306e\u5185\u5bb9\u3092\u5fa9\u5143\u3067\u304d\u307e\u3059\u3002

        \u56f3 3-7 \u00a0 Unicode\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u4f8b

        \u3057\u304b\u3057\u3001ASCII\u304c\u793a\u3057\u305f\u3088\u3046\u306b\u3001\u82f1\u8a9e\u306e\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306b\u306f1\u30d0\u30a4\u30c8\u306e\u307f\u304c\u5fc5\u8981\u3067\u3059\u3002\u4e0a\u8a18\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u82f1\u8a9e\u30c6\u30ad\u30b9\u30c8\u304c\u5360\u6709\u3059\u308b\u7a7a\u9593\u304cASCII\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3068\u6bd4\u8f03\u3057\u30662\u500d\u306b\u306a\u308a\u3001\u30e1\u30e2\u30ea\u7a7a\u9593\u306e\u7121\u99c4\u306b\u306a\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u3088\u308a\u52b9\u7387\u7684\u306aUnicode\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u65b9\u6cd5\u304c\u5fc5\u8981\u3067\u3059\u3002

        "},{"location":"chapter_data_structure/character_encoding/#344-utf-8","title":"3.4.4 \u00a0 UTF-8\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0","text":"

        \u73fe\u5728\u3001UTF-8\u306f\u56fd\u969b\u7684\u306b\u6700\u3082\u5e83\u304f\u4f7f\u7528\u3055\u308c\u3066\u3044\u308bUnicode\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u65b9\u6cd5\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002**\u3053\u308c\u306f\u53ef\u5909\u9577\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0**\u3067\u3001\u6587\u5b57\u306e\u8907\u96d1\u3055\u306b\u5fdc\u3058\u30661\u301c4\u30d0\u30a4\u30c8\u3092\u4f7f\u7528\u3057\u3066\u6587\u5b57\u3092\u8868\u73fe\u3057\u307e\u3059\u3002ASCII\u6587\u5b57\u306f1\u30d0\u30a4\u30c8\u306e\u307f\u304c\u5fc5\u8981\u3067\u3001\u30e9\u30c6\u30f3\u6587\u5b57\u3068\u30ae\u30ea\u30b7\u30e3\u6587\u5b57\u306f2\u30d0\u30a4\u30c8\u3001\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u308b\u4e2d\u56fd\u8a9e\u6587\u5b57\u306f3\u30d0\u30a4\u30c8\u3001\u305d\u306e\u4ed6\u306e\u7a00\u5c11\u306a\u6587\u5b57\u306f4\u30d0\u30a4\u30c8\u304c\u5fc5\u8981\u3067\u3059\u3002

        UTF-8\u306e\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u898f\u5247\u306f\u8907\u96d1\u3067\u306f\u306a\u304f\u30012\u3064\u306e\u30b1\u30fc\u30b9\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a

        • 1\u30d0\u30a4\u30c8\u6587\u5b57\u306e\u5834\u5408\u3001\u6700\u4e0a\u4f4d\u30d3\u30c3\u30c8\u3092\\(0\\)\u306b\u8a2d\u5b9a\u3057\u3001\u6b8b\u308a\u306e7\u30d3\u30c3\u30c8\u3092Unicode\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002\u6ce8\u76ee\u3059\u3079\u304d\u306f\u3001ASCII\u6587\u5b57\u304cUnicode\u30bb\u30c3\u30c8\u306e\u6700\u521d\u306e128\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u3092\u5360\u6709\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u3053\u308c\u306f**UTF-8\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u304cASCII\u3068\u5f8c\u65b9\u4e92\u63db\u6027\u304c\u3042\u308b**\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u3001UTF-8\u3092\u4f7f\u7528\u3057\u3066\u53e4\u3044ASCII\u30c6\u30ad\u30b9\u30c8\u3092\u89e3\u6790\u3067\u304d\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002
        • \u9577\u3055\\(n\\)\u30d0\u30a4\u30c8\u306e\u6587\u5b57\uff08\\(n > 1\\)\uff09\u306e\u5834\u5408\u3001\u6700\u521d\u306e\u30d0\u30a4\u30c8\u306e\u6700\u4e0a\u4f4d\\(n\\)\u30d3\u30c3\u30c8\u3092\\(1\\)\u306b\u8a2d\u5b9a\u3057\u3001\\((n + 1)^{\\text{th}}\\)\u30d3\u30c3\u30c8\u3092\\(0\\)\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u30022\u756a\u76ee\u306e\u30d0\u30a4\u30c8\u304b\u3089\u3001\u5404\u30d0\u30a4\u30c8\u306e\u6700\u4e0a\u4f4d2\u30d3\u30c3\u30c8\u3092\\(10\\)\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002\u6b8b\u308a\u306e\u30d3\u30c3\u30c8\u306fUnicode\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u3092\u57cb\u3081\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306f\u300cHello\u7b97\u6cd5\u300d\u306eUTF-8\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u6700\u4e0a\u4f4d\\(n\\)\u30d3\u30c3\u30c8\u304c\\(1\\)\u306b\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u6700\u4e0a\u4f4d\u30d3\u30c3\u30c8\u3067\\(1\\)\u306b\u8a2d\u5b9a\u3055\u308c\u305f\u30d3\u30c3\u30c8\u6570\u3092\u6570\u3048\u308b\u3053\u3068\u3067\u6587\u5b57\u306e\u9577\u3055\u3092\\(n\\)\u3068\u3057\u3066\u6c7a\u5b9a\u3067\u304d\u308b\u3053\u3068\u304c\u89b3\u5bdf\u3067\u304d\u307e\u3059\u3002

        \u3057\u304b\u3057\u3001\u306a\u305c\u6b8b\u308a\u306e\u30d0\u30a4\u30c8\u306e\u6700\u4e0a\u4f4d2\u30d3\u30c3\u30c8\u3092\\(10\\)\u306b\u8a2d\u5b9a\u3059\u308b\u306e\u3067\u3057\u3087\u3046\u304b\uff1f\u5b9f\u969b\u3001\u3053\u306e\\(10\\)\u306f\u4e00\u7a2e\u306e\u30c1\u30a7\u30c3\u30af\u30b5\u30e0\u3068\u3057\u3066\u6a5f\u80fd\u3057\u307e\u3059\u3002\u30b7\u30b9\u30c6\u30e0\u304c\u9593\u9055\u3063\u305f\u30d0\u30a4\u30c8\u304b\u3089\u30c6\u30ad\u30b9\u30c8\u306e\u89e3\u6790\u3092\u958b\u59cb\u3057\u305f\u5834\u5408\u3001\u30d0\u30a4\u30c8\u306e\u5148\u982d\u306e\\(10\\)\u306b\u3088\u308a\u30b7\u30b9\u30c6\u30e0\u306f\u7570\u5e38\u3092\u8fc5\u901f\u306b\u691c\u51fa\u3067\u304d\u307e\u3059\u3002

        \\(10\\)\u3092\u30c1\u30a7\u30c3\u30af\u30b5\u30e0\u3068\u3057\u3066\u4f7f\u7528\u3059\u308b\u7406\u7531\u306f\u3001UTF-8\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u898f\u5247\u306e\u4e0b\u3067\u306f\u3001\u6587\u5b57\u306e\u6700\u4e0a\u4f4d2\u30d3\u30c3\u30c8\u304c\\(10\\)\u306b\u306a\u308b\u3053\u3068\u306f\u4e0d\u53ef\u80fd\u3060\u304b\u3089\u3067\u3059\u3002\u3053\u308c\u306f\u77db\u76fe\u306b\u3088\u308a\u8a3c\u660e\u3067\u304d\u307e\u3059\uff1a\u6587\u5b57\u306e\u6700\u4e0a\u4f4d2\u30d3\u30c3\u30c8\u304c\\(10\\)\u306e\u5834\u5408\u3001\u6587\u5b57\u306e\u9577\u3055\u304c\\(1\\)\u3067\u3042\u308b\u3053\u3068\u3092\u793a\u3057\u3001\u3053\u308c\u306fASCII\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002\u3057\u304b\u3057\u3001ASCII\u6587\u5b57\u306e\u6700\u4e0a\u4f4d\u30d3\u30c3\u30c8\u306f\\(0\\)\u3067\u3042\u308b\u3079\u304d\u3067\u3001\u3053\u308c\u306f\u4eee\u5b9a\u3068\u77db\u76fe\u3057\u307e\u3059\u3002

        \u56f3 3-8 \u00a0 UTF-8\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u4f8b

        UTF-8\u4ee5\u5916\u306b\u3082\u3001\u4ed6\u306e\u4e00\u822c\u7684\u306a\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u65b9\u6cd5\u306b\u306f\u4ee5\u4e0b\u304c\u3042\u308a\u307e\u3059\uff1a

        • UTF-16\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\uff1a2\u307e\u305f\u306f4\u30d0\u30a4\u30c8\u3092\u4f7f\u7528\u3057\u3066\u6587\u5b57\u3092\u8868\u73fe\u3057\u307e\u3059\u3002\u3059\u3079\u3066\u306eASCII\u6587\u5b57\u3068\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u308b\u975e\u82f1\u8a9e\u6587\u5b57\u306f2\u30d0\u30a4\u30c8\u3067\u8868\u73fe\u3055\u308c\u3001\u5c11\u6570\u306e\u6587\u5b57\u306f4\u30d0\u30a4\u30c8\u304c\u5fc5\u8981\u3067\u3059\u30022\u30d0\u30a4\u30c8\u6587\u5b57\u306e\u5834\u5408\u3001UTF-16\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306fUnicode\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u3068\u7b49\u3057\u304f\u306a\u308a\u307e\u3059\u3002
        • UTF-32\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\uff1a\u3059\u3079\u3066\u306e\u6587\u5b57\u304c4\u30d0\u30a4\u30c8\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u3001UTF-32\u304cUTF-8\u3084UTF-16\u3088\u308a\u3082\u591a\u304f\u306e\u7a7a\u9593\u3092\u5360\u6709\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u3001\u7279\u306bASCII\u6587\u5b57\u306e\u5272\u5408\u304c\u9ad8\u3044\u30c6\u30ad\u30b9\u30c8\u3067\u306f\u9855\u8457\u3067\u3059\u3002

        \u30b9\u30c8\u30ec\u30fc\u30b8\u7a7a\u9593\u306e\u89b3\u70b9\u304b\u3089\u3001UTF-8\u3092\u4f7f\u7528\u3057\u3066\u82f1\u8a9e\u6587\u5b57\u3092\u8868\u73fe\u3059\u308b\u3053\u3068\u306f1\u30d0\u30a4\u30c8\u306e\u307f\u304c\u5fc5\u8981\u306a\u305f\u3081\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3059\u3002UTF-16\u3092\u4f7f\u7528\u3057\u3066\u4e00\u90e8\u306e\u975e\u82f1\u8a9e\u6587\u5b57\uff08\u4e2d\u56fd\u8a9e\u306a\u3069\uff09\u3092\u30a8\u30f3\u30b3\u30fc\u30c9\u3059\u308b\u3053\u3068\u306f\u30012\u30d0\u30a4\u30c8\u306e\u307f\u304c\u5fc5\u8981\u306a\u305f\u3081\u3088\u308a\u52b9\u7387\u7684\u306b\u306a\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u4e00\u65b9\u3001UTF-8\u3067\u306f3\u30d0\u30a4\u30c8\u304c\u5fc5\u8981\u306b\u306a\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002

        \u4e92\u63db\u6027\u306e\u89b3\u70b9\u304b\u3089\u3001UTF-8\u306f\u6700\u3082\u6c4e\u7528\u6027\u304c\u3042\u308a\u3001\u591a\u304f\u306e\u30c4\u30fc\u30eb\u3068\u30e9\u30a4\u30d6\u30e9\u30ea\u304cUTF-8\u3092\u512a\u5148\u7684\u306b\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_data_structure/character_encoding/#345","title":"3.4.5 \u00a0 \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u304a\u3051\u308b\u6587\u5b57\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0","text":"

        \u6b74\u53f2\u7684\u306b\u3001\u591a\u304f\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306f\u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c\u4e2d\u306e\u6587\u5b57\u5217\u51e6\u7406\u306bUTF-16\u3084UTF-32\u306a\u3069\u306e\u56fa\u5b9a\u9577\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u5229\u7528\u3057\u3066\u3044\u307e\u3057\u305f\u3002\u3053\u308c\u306b\u3088\u308a\u6587\u5b57\u5217\u3092\u914d\u5217\u3068\u3057\u3066\u51e6\u7406\u3067\u304d\u3001\u3044\u304f\u3064\u304b\u306e\u5229\u70b9\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\uff1aUTF-16\u3067\u30a8\u30f3\u30b3\u30fc\u30c9\u3055\u308c\u305f\u6587\u5b57\u5217\u306f\u7c21\u5358\u306b\u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\u3067\u304d\u307e\u3059\u3002\u53ef\u5909\u9577\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3067\u3042\u308bUTF-8\u306e\u5834\u5408\u3001\\(i^{th}\\)\u6587\u5b57\u306e\u4f4d\u7f6e\u3092\u7279\u5b9a\u3059\u308b\u306b\u306f\u6587\u5b57\u5217\u306e\u958b\u59cb\u304b\u3089\\(i^{th}\\)\u4f4d\u7f6e\u307e\u3067\u8d70\u67fb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\\(O(n)\\)\u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002
        • \u6587\u5b57\u6570\u30ab\u30a6\u30f3\u30c8\uff1a\u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\u3068\u540c\u69d8\u306b\u3001UTF-16\u3067\u30a8\u30f3\u30b3\u30fc\u30c9\u3055\u308c\u305f\u6587\u5b57\u5217\u306e\u6587\u5b57\u6570\u3092\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u3053\u3068\u306f\\(O(1)\\)\u64cd\u4f5c\u3067\u3059\u3002\u3057\u304b\u3057\u3001UTF-8\u3067\u30a8\u30f3\u30b3\u30fc\u30c9\u3055\u308c\u305f\u6587\u5b57\u5217\u306e\u6587\u5b57\u6570\u3092\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u306b\u306f\u6587\u5b57\u5217\u5168\u4f53\u3092\u8d70\u67fb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u6587\u5b57\u5217\u64cd\u4f5c\uff1a\u5206\u5272\u3001\u9023\u7d50\u3001\u633f\u5165\u3001\u524a\u9664\u306a\u3069\u306e\u591a\u304f\u306e\u6587\u5b57\u5217\u64cd\u4f5c\u306f\u3001UTF-16\u3067\u30a8\u30f3\u30b3\u30fc\u30c9\u3055\u308c\u305f\u6587\u5b57\u5217\u3067\u7c21\u5358\u3067\u3059\u3002\u3053\u308c\u3089\u306e\u64cd\u4f5c\u306f\u4e00\u822c\u7684\u306b\u3001UTF-8\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306e\u6709\u52b9\u6027\u3092\u78ba\u4fdd\u3059\u308b\u305f\u3081\u306bUTF-8\u3067\u30a8\u30f3\u30b3\u30fc\u30c9\u3055\u308c\u305f\u6587\u5b57\u5217\u3067\u8ffd\u52a0\u306e\u8a08\u7b97\u304c\u5fc5\u8981\u3067\u3059\u3002

        \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u304a\u3051\u308b\u6587\u5b57\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u30b9\u30ad\u30fc\u30e0\u306e\u8a2d\u8a08\u306f\u3001\u69d8\u3005\u306a\u8981\u56e0\u3092\u542b\u3080\u8208\u5473\u6df1\u3044\u30c8\u30d4\u30c3\u30af\u3067\u3059\uff1a

        • Java\u306eString\u578b\u306fUTF-16\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u3001\u5404\u6587\u5b57\u304c2\u30d0\u30a4\u30c8\u3092\u5360\u6709\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u300116\u30d3\u30c3\u30c8\u304c\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u6587\u5b57\u3092\u8868\u73fe\u3059\u308b\u306e\u306b\u5341\u5206\u3067\u3042\u308b\u3068\u3044\u3046\u521d\u671f\u306e\u4fe1\u5ff5\u306b\u57fa\u3065\u3044\u3066\u304a\u308a\u3001\u5f8c\u306b\u9593\u9055\u3044\u3067\u3042\u308b\u3053\u3068\u304c\u8a3c\u660e\u3055\u308c\u307e\u3057\u305f\u3002Unicode\u6a19\u6e96\u304c16\u30d3\u30c3\u30c8\u3092\u8d85\u3048\u3066\u62e1\u5f35\u3055\u308c\u308b\u3068\u3001Java\u306e\u6587\u5b57\u306f\u300c\u30b5\u30ed\u30b2\u30fc\u30c8\u30da\u30a2\u300d\u3068\u3057\u3066\u77e5\u3089\u308c\u308b16\u30d3\u30c3\u30c8\u5024\u306e\u30da\u30a2\u3067\u8868\u73fe\u3055\u308c\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002
        • JavaScript\u3068TypeScript\u306f\u3001Java\u3068\u540c\u69d8\u306e\u7406\u7531\u3067UTF-16\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002JavaScript\u304c1995\u5e74\u306bNetscape\u306b\u3088\u3063\u3066\u6700\u521d\u306b\u5c0e\u5165\u3055\u308c\u305f\u3068\u304d\u3001Unicode\u306f\u307e\u3060\u521d\u671f\u6bb5\u968e\u306b\u3042\u308a\u300116\u30d3\u30c3\u30c8\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306f\u3059\u3079\u3066\u306eUnicode\u6587\u5b57\u3092\u8868\u73fe\u3059\u308b\u306e\u306b\u5341\u5206\u3067\u3057\u305f\u3002
        • C#\u306fUTF-16\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u3001\u3053\u308c\u306f\u4e3b\u306bMicrosoft\u306b\u3088\u3063\u3066\u8a2d\u8a08\u3055\u308c\u305f.NET\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u3001\u304a\u3088\u3073\u591a\u304f\u306eMicrosoft\u6280\u8853\uff08Windows\u30aa\u30da\u30ec\u30fc\u30c6\u30a3\u30f3\u30b0\u30b7\u30b9\u30c6\u30e0\u3092\u542b\u3080\uff09\u304cUTF-16\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u5e83\u7bc4\u56f2\u306b\u4f7f\u7528\u3057\u3066\u3044\u308b\u305f\u3081\u3067\u3059\u3002

        \u6587\u5b57\u6570\u306e\u904e\u5c0f\u8a55\u4fa1\u306b\u3088\u308a\u3001\u3053\u308c\u3089\u306e\u8a00\u8a9e\u306f16\u30d3\u30c3\u30c8\u3092\u8d85\u3048\u308bUnicode\u6587\u5b57\u3092\u8868\u73fe\u3059\u308b\u305f\u3081\u306b\u300c\u30b5\u30ed\u30b2\u30fc\u30c8\u30da\u30a2\u300d\u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3057\u305f\u3002\u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u306b\u306f\u6b20\u70b9\u304c\u3042\u308a\u307e\u3059\uff1a\u30b5\u30ed\u30b2\u30fc\u30c8\u30da\u30a2\u3092\u542b\u3080\u6587\u5b57\u5217\u306f2\u30d0\u30a4\u30c8\u307e\u305f\u306f4\u30d0\u30a4\u30c8\u3092\u5360\u6709\u3059\u308b\u6587\u5b57\u3092\u6301\u3064\u5834\u5408\u304c\u3042\u308a\u3001\u56fa\u5b9a\u9577\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306e\u5229\u70b9\u3092\u5931\u3044\u307e\u3059\u3002\u3055\u3089\u306b\u3001\u30b5\u30ed\u30b2\u30fc\u30c8\u30da\u30a2\u306e\u51e6\u7406\u306f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306b\u8907\u96d1\u3055\u3068\u30c7\u30d0\u30c3\u30b0\u306e\u56f0\u96e3\u3055\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002

        \u3053\u308c\u3089\u306e\u8ab2\u984c\u306b\u5bfe\u51e6\u3059\u308b\u305f\u3081\u3001\u4e00\u90e8\u306e\u8a00\u8a9e\u306f\u4ee3\u66ff\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u6226\u7565\u3092\u63a1\u7528\u3057\u3066\u3044\u307e\u3059\uff1a

        • Python\u306estr\u578b\u306f\u3001\u6587\u5b57\u306e\u30b9\u30c8\u30ec\u30fc\u30b8\u9577\u304c\u6587\u5b57\u5217\u5185\u306e\u6700\u5927\u306eUnicode\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u306b\u4f9d\u5b58\u3059\u308b\u67d4\u8edf\u306a\u8868\u73fe\u3067Unicode\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u3059\u3079\u3066\u306e\u6587\u5b57\u304cASCII\u306e\u5834\u5408\u3001\u5404\u6587\u5b57\u306f1\u30d0\u30a4\u30c8\u3092\u5360\u6709\u3057\u3001\u57fa\u672c\u591a\u8a00\u8a9e\u9762\uff08BMP\uff09\u5185\u306e\u6587\u5b57\u306f2\u30d0\u30a4\u30c8\u3001BMP\u3092\u8d85\u3048\u308b\u6587\u5b57\u306f4\u30d0\u30a4\u30c8\u3092\u5360\u6709\u3057\u307e\u3059\u3002
        • Go\u306estring\u578b\u306f\u5185\u90e8\u7684\u306bUTF-8\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002Go\u306f\u500b\u5225\u306eUnicode\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u3092\u8868\u73fe\u3059\u308b\u305f\u3081\u306erune\u578b\u3082\u63d0\u4f9b\u3057\u307e\u3059\u3002
        • Rust\u306estr\u3068String\u578b\u306f\u5185\u90e8\u7684\u306bUTF-8\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002Rust\u306f\u500b\u5225\u306eUnicode\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u7528\u306echar\u578b\u3082\u63d0\u4f9b\u3057\u307e\u3059\u3002

        \u4e0a\u8a18\u306e\u8b70\u8ad6\u306f\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u306e\u6587\u5b57\u5217\u306e\u683c\u7d0d\u65b9\u6cd5\u306b\u95a2\u3059\u308b\u3082\u306e\u3067\u3042\u308a\u3001**\u30d5\u30a1\u30a4\u30eb\u3067\u306e\u6587\u5b57\u5217\u306e\u683c\u7d0d\u65b9\u6cd5\u3084\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u4e0a\u3067\u306e\u9001\u4fe1\u65b9\u6cd5\u3068\u306f\u7570\u306a\u308b**\u3053\u3068\u306b\u6ce8\u610f\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002\u30d5\u30a1\u30a4\u30eb\u30b9\u30c8\u30ec\u30fc\u30b8\u3084\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u9001\u4fe1\u3067\u306f\u3001\u6587\u5b57\u5217\u306f\u901a\u5e38\u3001\u6700\u9069\u306a\u4e92\u63db\u6027\u3068\u7a7a\u9593\u52b9\u7387\u306e\u305f\u3081\u306bUTF-8\u5f62\u5f0f\u3067\u30a8\u30f3\u30b3\u30fc\u30c9\u3055\u308c\u307e\u3059\u3002

        "},{"location":"chapter_data_structure/classification_of_data_structure/","title":"3.1 \u00a0 \u30c7\u30fc\u30bf\u69cb\u9020\u306e\u5206\u985e","text":"

        \u4e00\u822c\u7684\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u306f\u3001\u914d\u5217\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u30b9\u30bf\u30c3\u30af\u3001\u30ad\u30e5\u30fc\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3001\u6728\u3001\u30d2\u30fc\u30d7\u3001\u30b0\u30e9\u30d5\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u3089\u306f\u300c\u8ad6\u7406\u69cb\u9020\u300d\u3068\u300c\u7269\u7406\u69cb\u9020\u300d\u306b\u5206\u985e\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_data_structure/classification_of_data_structure/#311","title":"3.1.1 \u00a0 \u8ad6\u7406\u69cb\u9020\uff1a\u7dda\u5f62\u3068\u975e\u7dda\u5f62","text":"

        \u8ad6\u7406\u69cb\u9020\u306f\u30c7\u30fc\u30bf\u8981\u7d20\u9593\u306e\u8ad6\u7406\u7684\u95a2\u4fc2\u3092\u660e\u3089\u304b\u306b\u3057\u307e\u3059\u3002\u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u306f\u3001\u30c7\u30fc\u30bf\u306f\u7279\u5b9a\u306e\u9806\u5e8f\u3067\u914d\u7f6e\u3055\u308c\u3001\u30c7\u30fc\u30bf\u9593\u306e\u7dda\u5f62\u95a2\u4fc2\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u4e00\u65b9\u3001\u6728\u3067\u306f\u3001\u30c7\u30fc\u30bf\u306f\u4e0a\u304b\u3089\u4e0b\u3078\u968e\u5c64\u7684\u306b\u914d\u7f6e\u3055\u308c\u3001\u300c\u7956\u5148\u300d\u3068\u300c\u5b50\u5b6b\u300d\u9593\u306e\u6d3e\u751f\u95a2\u4fc2\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u305d\u3057\u3066\u3001\u30b0\u30e9\u30d5\u306f\u30ce\u30fc\u30c9\u3068\u30a8\u30c3\u30b8\u304b\u3089\u69cb\u6210\u3055\u308c\u3001\u8907\u96d1\u306a\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u95a2\u4fc2\u3092\u53cd\u6620\u3057\u3066\u3044\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u3001\u8ad6\u7406\u69cb\u9020\u306f\u300c\u7dda\u5f62\u300d\u3068\u300c\u975e\u7dda\u5f62\u300d\u306e2\u3064\u306e\u4e3b\u8981\u30ab\u30c6\u30b4\u30ea\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u7dda\u5f62\u69cb\u9020\u306f\u3088\u308a\u76f4\u611f\u7684\u3067\u3001\u30c7\u30fc\u30bf\u304c\u8ad6\u7406\u95a2\u4fc2\u306b\u304a\u3044\u3066\u7dda\u5f62\u306b\u914d\u7f6e\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u975e\u7dda\u5f62\u69cb\u9020\u306f\u3001\u9006\u306b\u975e\u7dda\u5f62\u306b\u914d\u7f6e\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        • \u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\uff1a\u914d\u5217\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u30b9\u30bf\u30c3\u30af\u3001\u30ad\u30e5\u30fc\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3002\u8981\u7d20\u304c\u4e00\u5bfe\u4e00\u306e\u9806\u6b21\u95a2\u4fc2\u3092\u6301\u3061\u307e\u3059\u3002
        • \u975e\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\uff1a\u6728\u3001\u30d2\u30fc\u30d7\u3001\u30b0\u30e9\u30d5\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3002

        \u975e\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u3001\u3055\u3089\u306b\u6728\u69cb\u9020\u3068\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u69cb\u9020\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        • \u6728\u69cb\u9020\uff1a\u6728\u3001\u30d2\u30fc\u30d7\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3002\u8981\u7d20\u304c\u4e00\u5bfe\u591a\u306e\u95a2\u4fc2\u3092\u6301\u3061\u307e\u3059\u3002
        • \u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u69cb\u9020\uff1a\u30b0\u30e9\u30d5\u3002\u8981\u7d20\u304c\u591a\u5bfe\u591a\u306e\u95a2\u4fc2\u3092\u6301\u3061\u307e\u3059\u3002

        \u56f3 3-1 \u00a0 Linear and non-linear data structures

        "},{"location":"chapter_data_structure/classification_of_data_structure/#312","title":"3.1.2 \u00a0 \u7269\u7406\u69cb\u9020\uff1a\u9023\u7d9a\u3068\u5206\u6563","text":"

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u4e2d\u3001\u51e6\u7406\u3055\u308c\u308b\u30c7\u30fc\u30bf\u306f\u30e1\u30e2\u30ea\u306b\u683c\u7d0d\u3055\u308c\u307e\u3059\u3002\u4e0b\u56f3\u306f\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u30e1\u30e2\u30ea\u30b9\u30c6\u30a3\u30c3\u30af\u3092\u793a\u3057\u3066\u304a\u308a\u3001\u5404\u9ed2\u3044\u6b63\u65b9\u5f62\u306f\u7269\u7406\u30e1\u30e2\u30ea\u7a7a\u9593\u3067\u3059\u3002\u30e1\u30e2\u30ea\u3092\u5de8\u5927\u306aExcel\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u3068\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u5404\u30bb\u30eb\u306f\u4e00\u5b9a\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3067\u304d\u307e\u3059\u3002

        \u30b7\u30b9\u30c6\u30e0\u306f\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u306b\u3088\u3063\u3066\u76ee\u6a19\u4f4d\u7f6e\u306e\u30c7\u30fc\u30bf\u306b\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306f\u7279\u5b9a\u306e\u30eb\u30fc\u30eb\u306b\u5f93\u3063\u3066\u8868\u306e\u5404\u30bb\u30eb\u306b\u4e00\u610f\u306e\u8b58\u5225\u5b50\u3092\u5272\u308a\u5f53\u3066\u3001\u5404\u30e1\u30e2\u30ea\u7a7a\u9593\u304c\u4e00\u610f\u306e\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u3092\u6301\u3064\u3053\u3068\u3092\u4fdd\u8a3c\u3057\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u30a2\u30c9\u30ec\u30b9\u306b\u3088\u308a\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u30e1\u30e2\u30ea\u306b\u683c\u7d0d\u3055\u308c\u305f\u30c7\u30fc\u30bf\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u307e\u3059\u3002

        \u56f3 3-2 \u00a0 Memory stick, memory spaces, memory addresses

        Tip

        \u30e1\u30e2\u30ea\u3092Excel\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u306b\u6bd4\u8f03\u3059\u308b\u3053\u3068\u306f\u7c21\u7565\u5316\u3055\u308c\u305f\u985e\u63a8\u3067\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u30e1\u30e2\u30ea\u306e\u5b9f\u969b\u306e\u52d5\u4f5c\u30e1\u30ab\u30cb\u30ba\u30e0\u306f\u3088\u308a\u8907\u96d1\u3067\u3001\u30a2\u30c9\u30ec\u30b9\u7a7a\u9593\u3001\u30e1\u30e2\u30ea\u7ba1\u7406\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u30e1\u30ab\u30cb\u30ba\u30e0\u3001\u4eee\u60f3\u30e1\u30e2\u30ea\u3001\u7269\u7406\u30e1\u30e2\u30ea\u306a\u3069\u306e\u6982\u5ff5\u304c\u95a2\u4fc2\u3057\u3066\u3044\u307e\u3059\u3002

        \u30e1\u30e2\u30ea\u306f\u3059\u3079\u3066\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u5171\u6709\u30ea\u30bd\u30fc\u30b9\u3067\u3059\u3002\u3042\u308b\u30e1\u30e2\u30ea\u30d6\u30ed\u30c3\u30af\u304c1\u3064\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u3088\u3063\u3066\u5360\u6709\u3055\u308c\u308b\u3068\u3001\u4ed6\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u540c\u6642\u306b\u4f7f\u7528\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30e1\u30e2\u30ea\u30ea\u30bd\u30fc\u30b9\u306f\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u8a2d\u8a08\u306b\u304a\u3051\u308b\u91cd\u8981\u306a\u8003\u616e\u4e8b\u9805\u3067\u3059\u3002\u4f8b\u3048\u3070\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u30d4\u30fc\u30af\u30e1\u30e2\u30ea\u4f7f\u7528\u91cf\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u306e\u6b8b\u308a\u7a7a\u304d\u30e1\u30e2\u30ea\u3092\u8d85\u3048\u3066\u306f\u3044\u3051\u307e\u305b\u3093\u3002\u9023\u7d9a\u3057\u305f\u30e1\u30e2\u30ea\u30d6\u30ed\u30c3\u30af\u304c\u4e0d\u8db3\u3057\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u975e\u9023\u7d9a\u30e1\u30e2\u30ea\u30d6\u30ed\u30c3\u30af\u306b\u683c\u7d0d\u3067\u304d\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u9078\u629e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u3001\u7269\u7406\u69cb\u9020\u306f\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30e1\u30e2\u30ea\u306b\u304a\u3051\u308b\u30c7\u30fc\u30bf\u306e\u683c\u7d0d\u65b9\u6cd5\u3092\u53cd\u6620\u3057\u3001\u9023\u7d9a\u7a7a\u9593\u683c\u7d0d\uff08\u914d\u5217\uff09\u3068\u975e\u9023\u7d9a\u7a7a\u9593\u683c\u7d0d\uff08\u9023\u7d50\u30ea\u30b9\u30c8\uff09\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u30022\u3064\u306e\u30bf\u30a4\u30d7\u306e\u7269\u7406\u69cb\u9020\u306f\u3001\u6642\u9593\u52b9\u7387\u3068\u7a7a\u9593\u52b9\u7387\u306e\u89b3\u70b9\u3067\u88dc\u5b8c\u7684\u306a\u7279\u6027\u3092\u793a\u3057\u307e\u3059\u3002

        \u56f3 3-3 \u00a0 Contiguous space storage and dispersed space storage

        \u3059\u3079\u3066\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u914d\u5217\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u307e\u305f\u306f\u305d\u306e\u7d44\u307f\u5408\u308f\u305b\u306b\u57fa\u3065\u3044\u3066\u5b9f\u88c5\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4f8b\u3048\u3070\u3001\u30b9\u30bf\u30c3\u30af\u3068\u30ad\u30e5\u30fc\u306f\u914d\u5217\u307e\u305f\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u3069\u3061\u3089\u3067\u3082\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u5b9f\u88c5\u306b\u306f\u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u4e21\u65b9\u304c\u95a2\u4fc2\u3059\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002

        • \u914d\u5217\u30d9\u30fc\u30b9\u306e\u5b9f\u88c5\uff1a\u30b9\u30bf\u30c3\u30af\u3001\u30ad\u30e5\u30fc\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3001\u6728\u3001\u30d2\u30fc\u30d7\u3001\u30b0\u30e9\u30d5\u3001\u884c\u5217\u3001\u30c6\u30f3\u30bd\u30eb\uff08\u6b21\u5143\\(\\geq 3\\)\u306e\u914d\u5217\uff09\u3002
        • \u9023\u7d50\u30ea\u30b9\u30c8\u30d9\u30fc\u30b9\u306e\u5b9f\u88c5\uff1a\u30b9\u30bf\u30c3\u30af\u3001\u30ad\u30e5\u30fc\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3001\u6728\u3001\u30d2\u30fc\u30d7\u3001\u30b0\u30e9\u30d5\u306a\u3069\u3002

        \u914d\u5217\u306b\u57fa\u3065\u3044\u3066\u5b9f\u88c5\u3055\u308c\u305f\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u300c\u9759\u7684\u30c7\u30fc\u30bf\u69cb\u9020\u300d\u3068\u3082\u547c\u3070\u308c\u3001\u521d\u671f\u5316\u5f8c\u306b\u9577\u3055\u3092\u5909\u66f4\u3067\u304d\u306a\u3044\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u9006\u306b\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u3044\u305f\u3082\u306e\u306f\u300c\u52d5\u7684\u30c7\u30fc\u30bf\u69cb\u9020\u300d\u3068\u547c\u3070\u308c\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c\u4e2d\u306b\u30b5\u30a4\u30ba\u3092\u8abf\u6574\u3067\u304d\u307e\u3059\u3002

        Tip

        \u7269\u7406\u69cb\u9020\u3092\u7406\u89e3\u3059\u308b\u306e\u304c\u56f0\u96e3\u306a\u5834\u5408\u306f\u3001\u6b21\u306e\u7ae0\u300c\u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u300d\u3092\u8aad\u3093\u3067\u304b\u3089\u3001\u3053\u306e\u7bc0\u306b\u623b\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002

        "},{"location":"chapter_data_structure/number_encoding/","title":"3.3 \u00a0 \u6570\u5024\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0 *","text":"

        Tip

        \u672c\u66f8\u3067\u306f\u3001\u30a2\u30b9\u30bf\u30ea\u30b9\u30af\u300c*\u300d\u304c\u4ed8\u3044\u305f\u7ae0\u306f\u4efb\u610f\u8aad\u66f8\u3067\u3059\u3002\u6642\u9593\u304c\u4e0d\u8db3\u3057\u3066\u3044\u308b\u5834\u5408\u3084\u96e3\u3057\u3044\u3068\u611f\u3058\u308b\u5834\u5408\u306f\u3001\u6700\u521d\u306f\u3053\u308c\u3089\u3092\u30b9\u30ad\u30c3\u30d7\u3057\u3066\u3001\u5fc5\u9808\u306e\u7ae0\u3092\u5b8c\u4e86\u3057\u305f\u5f8c\u306b\u623b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_data_structure/number_encoding/#331","title":"3.3.1 \u00a0 \u6574\u6570\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0","text":"

        \u524d\u306e\u7bc0\u306e\u8868\u3067\u3001\u3059\u3079\u3066\u306e\u6574\u6570\u578b\u306f\u6b63\u306e\u6570\u3088\u308a\u30821\u3064\u591a\u3044\u8ca0\u306e\u6570\u3092\u8868\u73fe\u3067\u304d\u308b\u3053\u3068\u3092\u89b3\u5bdf\u3057\u307e\u3057\u305f\u3002\u4f8b\u3048\u3070\u3001byte\u306e\u7bc4\u56f2\u306f\\([-128, 127]\\)\u3067\u3059\u3002\u3053\u306e\u73fe\u8c61\u306f\u76f4\u611f\u306b\u53cd\u3059\u308b\u3088\u3046\u306b\u898b\u3048\u3001\u305d\u306e\u6839\u672c\u7684\u306a\u7406\u7531\u306b\u306f\u7b26\u53f7\u7d76\u5bfe\u5024\u30011\u306e\u88dc\u6570\u30012\u306e\u88dc\u6570\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306e\u77e5\u8b58\u304c\u95a2\u4e0e\u3057\u3066\u3044\u307e\u3059\u3002

        \u307e\u305a\u91cd\u8981\u306a\u3053\u3068\u306f\u3001**\u6570\u5024\u306f\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u5185\u30672\u306e\u88dc\u6570\u5f62\u5f0f\u3067\u683c\u7d0d\u3055\u308c\u308b**\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002\u306a\u305c\u305d\u3046\u306a\u306e\u304b\u3092\u5206\u6790\u3059\u308b\u524d\u306b\u3001\u3053\u308c\u30893\u3064\u306e\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u65b9\u6cd5\u3092\u5b9a\u7fa9\u3057\u307e\u3057\u3087\u3046\uff1a

        • \u7b26\u53f7\u7d76\u5bfe\u5024\uff1a\u6570\u5024\u306e\u4e8c\u9032\u8868\u73fe\u306e\u6700\u4e0a\u4f4d\u30d3\u30c3\u30c8\u3092\u7b26\u53f7\u30d3\u30c3\u30c8\u3068\u3057\u3001\\(0\\)\u306f\u6b63\u306e\u6570\u3001\\(1\\)\u306f\u8ca0\u306e\u6570\u3092\u8868\u3057\u307e\u3059\u3002\u6b8b\u308a\u306e\u30d3\u30c3\u30c8\u306f\u6570\u5024\u306e\u5024\u3092\u8868\u3057\u307e\u3059\u3002
        • 1\u306e\u88dc\u6570\uff1a\u6b63\u306e\u6570\u306e1\u306e\u88dc\u6570\u306f\u7b26\u53f7\u7d76\u5bfe\u5024\u3068\u540c\u3058\u3067\u3059\u3002\u8ca0\u306e\u6570\u306e\u5834\u5408\u3001\u7b26\u53f7\u30d3\u30c3\u30c8\u4ee5\u5916\u306e\u3059\u3079\u3066\u306e\u30d3\u30c3\u30c8\u3092\u53cd\u8ee2\u3057\u3066\u5f97\u3089\u308c\u307e\u3059\u3002
        • 2\u306e\u88dc\u6570\uff1a\u6b63\u306e\u6570\u306e2\u306e\u88dc\u6570\u306f\u7b26\u53f7\u7d76\u5bfe\u5024\u3068\u540c\u3058\u3067\u3059\u3002\u8ca0\u306e\u6570\u306e\u5834\u5408\u3001\u305d\u306e1\u306e\u88dc\u6570\u306b\\(1\\)\u3092\u52a0\u3048\u3066\u5f97\u3089\u308c\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306f\u3001\u7b26\u53f7\u7d76\u5bfe\u5024\u30011\u306e\u88dc\u6570\u30012\u306e\u88dc\u6570\u9593\u306e\u5909\u63db\u3092\u793a\u3057\u3066\u3044\u307e\u3059\uff1a

        \u56f3 3-4 \u00a0 \u7b26\u53f7\u7d76\u5bfe\u5024\u30011\u306e\u88dc\u6570\u30012\u306e\u88dc\u6570\u9593\u306e\u5909\u63db

        \u7b26\u53f7\u7d76\u5bfe\u5024\u306f\u6700\u3082\u76f4\u611f\u7684\u3067\u3059\u304c\u3001\u5236\u9650\u304c\u3042\u308a\u307e\u3059\u3002\u4e00\u3064\u306b\u306f\u3001\u7b26\u53f7\u7d76\u5bfe\u5024\u306e\u8ca0\u306e\u6570\u306f\u8a08\u7b97\u3067\u76f4\u63a5\u4f7f\u7528\u3067\u304d\u307e\u305b\u3093\u3002\u4f8b\u3048\u3070\u3001\u7b26\u53f7\u7d76\u5bfe\u5024\u3067\\(1 + (-2)\\)\u3092\u8a08\u7b97\u3059\u308b\u3068\\(-3\\)\u306b\u306a\u308a\u3001\u3053\u308c\u306f\u6b63\u3057\u304f\u3042\u308a\u307e\u305b\u3093\u3002

        \\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 + 1000 \\; 0010 \\newline & = 1000 \\; 0011 \\newline & \\rightarrow -3 \\end{aligned} \\]

        \u3053\u306e\u554f\u984c\u306b\u5bfe\u51e6\u3059\u308b\u305f\u3081\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306f1\u306e\u88dc\u6570\u3092\u5c0e\u5165\u3057\u307e\u3057\u305f\u30021\u306e\u88dc\u6570\u306b\u5909\u63db\u3057\u3066\\(1 + (-2)\\)\u3092\u8a08\u7b97\u3057\u3001\u7d50\u679c\u3092\u7b26\u53f7\u7d76\u5bfe\u5024\u306b\u623b\u3059\u3068\u3001\u6b63\u3057\u3044\u7d50\u679c\\(-1\\)\u304c\u5f97\u3089\u308c\u307e\u3059\u3002

        \\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 \\; \\text{(\u7b26\u53f7\u7d76\u5bfe\u5024)} + 1000 \\; 0010 \\; \\text{(\u7b26\u53f7\u7d76\u5bfe\u5024)} \\newline & = 0000 \\; 0001 \\; \\text{(1\u306e\u88dc\u6570)} + 1111 \\; 1101 \\; \\text{(1\u306e\u88dc\u6570)} \\newline & = 1111 \\; 1110 \\; \\text{(1\u306e\u88dc\u6570)} \\newline & = 1000 \\; 0001 \\; \\text{(\u7b26\u53f7\u7d76\u5bfe\u5024)} \\newline & \\rightarrow -1 \\end{aligned} \\]

        \u307e\u305f\u3001\u7b26\u53f7\u7d76\u5bfe\u5024\u3067\u306f0\u306b2\u3064\u306e\u8868\u73fe\u304c\u3042\u308a\u307e\u3059\uff1a\\(+0\\)\u3068\\(-0\\)\u3067\u3059\u3002\u3053\u308c\u306f0\u306b\u5bfe\u3057\u30662\u3064\u306e\u7570\u306a\u308b\u4e8c\u9032\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u304c\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u3001\u66d6\u6627\u3055\u3092\u5f15\u304d\u8d77\u3053\u3059\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u6761\u4ef6\u30c1\u30a7\u30c3\u30af\u3067\u6b63\u3068\u8ca0\u306e0\u3092\u533a\u5225\u3057\u306a\u3044\u3068\u3001\u6b63\u3057\u304f\u306a\u3044\u7d50\u679c\u306b\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u66d6\u6627\u3055\u306b\u5bfe\u51e6\u3059\u308b\u306b\u306f\u8ffd\u52a0\u306e\u30c1\u30a7\u30c3\u30af\u304c\u5fc5\u8981\u3067\u3001\u8a08\u7b97\u52b9\u7387\u304c\u4f4e\u4e0b\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        \\[ \\begin{aligned} +0 & \\rightarrow 0000 \\; 0000 \\newline -0 & \\rightarrow 1000 \\; 0000 \\end{aligned} \\]

        \u7b26\u53f7\u7d76\u5bfe\u5024\u3068\u540c\u69d8\u306b\u30011\u306e\u88dc\u6570\u3082\u6b63\u3068\u8ca0\u306e0\u306e\u66d6\u6627\u3055\u306b\u60a9\u307e\u3055\u308c\u307e\u3059\u3002\u305d\u306e\u305f\u3081\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306f\u3055\u3089\u306b2\u306e\u88dc\u6570\u3092\u5c0e\u5165\u3057\u307e\u3057\u305f\u3002\u7b26\u53f7\u7d76\u5bfe\u5024\u30011\u306e\u88dc\u6570\u30012\u306e\u88dc\u6570\u306b\u304a\u3051\u308b\u8ca0\u306e0\u306e\u5909\u63db\u904e\u7a0b\u3092\u89b3\u5bdf\u3057\u3066\u307f\u307e\u3057\u3087\u3046\uff1a

        \\[ \\begin{aligned} -0 \\rightarrow \\; & 1000 \\; 0000 \\; \\text{(\u7b26\u53f7\u7d76\u5bfe\u5024)} \\newline = \\; & 1111 \\; 1111 \\; \\text{(1\u306e\u88dc\u6570)} \\newline = 1 \\; & 0000 \\; 0000 \\; \\text{(2\u306e\u88dc\u6570)} \\newline \\end{aligned} \\]

        \u8ca0\u306e0\u306e1\u306e\u88dc\u6570\u306b\\(1\\)\u3092\u52a0\u3048\u308b\u3068\u6841\u4e0a\u304c\u308a\u304c\u767a\u751f\u3057\u307e\u3059\u304c\u3001byte\u306e\u9577\u3055\u306f8\u30d3\u30c3\u30c8\u306e\u307f\u306e\u305f\u3081\u30019\u756a\u76ee\u306e\u30d3\u30c3\u30c8\u3078\u306e\u6841\u4e0a\u304c\u308a\\(1\\)\u306f\u7834\u68c4\u3055\u308c\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001**\u8ca0\u306e0\u306e2\u306e\u88dc\u6570\u306f\\(0000 \\; 0000\\)**\u3067\u3001\u6b63\u306e0\u3068\u540c\u3058\u306b\u306a\u308a\u3001\u66d6\u6627\u3055\u304c\u89e3\u6c7a\u3055\u308c\u307e\u3059\u3002

        \u6700\u5f8c\u306e\u8b0e\u306f\u3001byte\u306e\\([-128, 127]\\)\u306e\u7bc4\u56f2\u3067\u3001\u8ffd\u52a0\u306e\u8ca0\u306e\u6570\\(-128\\)\u304c\u3042\u308b\u3053\u3068\u3067\u3059\u3002\\([-127, +127]\\)\u306e\u533a\u9593\u3067\u306f\u3001\u3059\u3079\u3066\u306e\u6574\u6570\u306b\u5bfe\u5fdc\u3059\u308b\u7b26\u53f7\u7d76\u5bfe\u5024\u30011\u306e\u88dc\u6570\u30012\u306e\u88dc\u6570\u304c\u3042\u308a\u3001\u76f8\u4e92\u5909\u63db\u304c\u53ef\u80fd\u3067\u3042\u308b\u3053\u3068\u3092\u89b3\u5bdf\u3057\u307e\u3059\u3002

        \u3057\u304b\u3057\u30012\u306e\u88dc\u6570\\(1000 \\; 0000\\)\u306f\u5bfe\u5fdc\u3059\u308b\u7b26\u53f7\u7d76\u5bfe\u5024\u3092\u6301\u305f\u306a\u3044\u4f8b\u5916\u3067\u3059\u3002\u5909\u63db\u65b9\u6cd5\u306b\u3088\u308b\u3068\u3001\u305d\u306e\u7b26\u53f7\u7d76\u5bfe\u5024\u306f\\(0000 \\; 0000\\)\u3067\u30010\u3092\u793a\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u77db\u76fe\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u306a\u305c\u306a\u3089\u3001\u305d\u306e2\u306e\u88dc\u6570\u306f\u81ea\u5206\u81ea\u8eab\u3092\u8868\u3059\u3079\u304d\u3060\u304b\u3089\u3067\u3059\u3002\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306f\u3001\u3053\u306e\u7279\u5225\u306a2\u306e\u88dc\u6570\\(1000 \\; 0000\\)\u3092\\(-128\\)\u3092\u8868\u3059\u3082\u306e\u3068\u3057\u3066\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002\u5b9f\u969b\u30012\u306e\u88dc\u6570\u3067\u306e\\((-1) + (-127)\\)\u306e\u8a08\u7b97\u7d50\u679c\u306f\\(-128\\)\u306b\u306a\u308a\u307e\u3059\u3002

        \\[ \\begin{aligned} & (-127) + (-1) \\newline & \\rightarrow 1111 \\; 1111 \\; \\text{(\u7b26\u53f7\u7d76\u5bfe\u5024)} + 1000 \\; 0001 \\; \\text{(\u7b26\u53f7\u7d76\u5bfe\u5024)} \\newline & = 1000 \\; 0000 \\; \\text{(1\u306e\u88dc\u6570)} + 1111 \\; 1110 \\; \\text{(1\u306e\u88dc\u6570)} \\newline & = 1000 \\; 0001 \\; \\text{(2\u306e\u88dc\u6570)} + 1111 \\; 1111 \\; \\text{(2\u306e\u88dc\u6570)} \\newline & = 1000 \\; 0000 \\; \\text{(2\u306e\u88dc\u6570)} \\newline & \\rightarrow -128 \\end{aligned} \\]

        \u304a\u6c17\u3065\u304d\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u3053\u308c\u3089\u306e\u8a08\u7b97\u306f\u3059\u3079\u3066\u52a0\u7b97\u3067\u3042\u308a\u3001\u91cd\u8981\u306a\u4e8b\u5b9f\u3092\u793a\u5506\u3057\u3066\u3044\u307e\u3059\uff1a\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u5185\u90e8\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u56de\u8def\u306f\u4e3b\u306b\u52a0\u7b97\u6f14\u7b97\u3092\u4e2d\u5fc3\u306b\u8a2d\u8a08\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u52a0\u7b97\u304c\u4e57\u7b97\u3001\u9664\u7b97\u3001\u6e1b\u7b97\u306a\u3069\u306e\u4ed6\u306e\u6f14\u7b97\u3068\u6bd4\u8f03\u3057\u3066\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u3067\u5b9f\u88c5\u3057\u3084\u3059\u304f\u3001\u4e26\u5217\u5316\u304c\u5bb9\u6613\u3067\u9ad8\u901f\u8a08\u7b97\u304c\u53ef\u80fd\u3060\u304b\u3089\u3067\u3059\u3002

        \u3053\u308c\u306f\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u304c\u52a0\u7b97\u306e\u307f\u3092\u5b9f\u884c\u3067\u304d\u308b\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u3082\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u52a0\u7b97\u3068\u57fa\u672c\u7684\u306a\u8ad6\u7406\u6f14\u7b97\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306f\u69d8\u3005\u306a\u4ed6\u306e\u6570\u5b66\u6f14\u7b97\u3092\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u6e1b\u7b97\\(a - b\\)\u306f\\(a + (-b)\\)\u306b\u5909\u63db\u3067\u304d\u3001\u4e57\u7b97\u3068\u9664\u7b97\u306f\u8907\u6570\u306e\u52a0\u7b97\u307e\u305f\u306f\u6e1b\u7b97\u306b\u5909\u63db\u3067\u304d\u307e\u3059\u3002

        \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30672\u306e\u88dc\u6570\u3092\u4f7f\u7528\u3059\u308b\u7406\u7531\u3092\u307e\u3068\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a2\u306e\u88dc\u6570\u8868\u73fe\u306b\u3088\u308a\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306f\u540c\u3058\u56de\u8def\u3068\u6f14\u7b97\u3092\u4f7f\u7528\u3057\u3066\u6b63\u3068\u8ca0\u306e\u6570\u306e\u52a0\u7b97\u3092\u51e6\u7406\u3067\u304d\u3001\u6e1b\u7b97\u7528\u306e\u7279\u5225\u306a\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u56de\u8def\u306e\u5fc5\u8981\u6027\u3092\u6392\u9664\u3057\u3001\u6b63\u3068\u8ca0\u306e0\u306e\u66d6\u6627\u3055\u3092\u56de\u907f\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u8a2d\u8a08\u304c\u5927\u5e45\u306b\u7c21\u7d20\u5316\u3055\u308c\u3001\u8a08\u7b97\u52b9\u7387\u304c\u5411\u4e0a\u3057\u307e\u3059\u3002

        2\u306e\u88dc\u6570\u306e\u8a2d\u8a08\u306f\u975e\u5e38\u306b\u5de7\u5999\u3067\u3001\u30b9\u30da\u30fc\u30b9\u306e\u5236\u7d04\u306b\u3088\u308a\u3001\u3053\u3053\u3067\u505c\u6b62\u3057\u307e\u3059\u3002\u8208\u5473\u306e\u3042\u308b\u8aad\u8005\u306f\u3055\u3089\u306b\u63a2\u6c42\u3059\u308b\u3053\u3068\u3092\u5968\u52b1\u3057\u307e\u3059\u3002

        "},{"location":"chapter_data_structure/number_encoding/#332","title":"3.3.2 \u00a0 \u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0","text":"

        \u8208\u5473\u6df1\u3044\u3053\u3068\u306b\u6c17\u3065\u3044\u305f\u304b\u3082\u3057\u308c\u307e\u305b\u3093\uff1a\u540c\u30584\u30d0\u30a4\u30c8\u306e\u9577\u3055\u306b\u3082\u304b\u304b\u308f\u3089\u305a\u3001\u306a\u305cfloat\u306fint\u3068\u6bd4\u8f03\u3057\u3066\u306f\u308b\u304b\u306b\u5927\u304d\u3044\u5024\u306e\u7bc4\u56f2\u3092\u6301\u3064\u306e\u3067\u3057\u3087\u3046\u304b\uff1f\u3053\u308c\u306f\u76f4\u611f\u306b\u53cd\u3059\u308b\u3088\u3046\u306b\u898b\u3048\u307e\u3059\u3002float\u306f\u5206\u6570\u3092\u8868\u73fe\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u7bc4\u56f2\u304c\u7e2e\u5c0f\u3059\u308b\u3068\u4e88\u60f3\u3055\u308c\u308b\u304b\u3089\u3067\u3059\u3002

        \u5b9f\u969b\u3001\u3053\u308c\u306f\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\uff08float\uff09\u3067\u4f7f\u7528\u3055\u308c\u308b\u7570\u306a\u308b\u8868\u73fe\u65b9\u6cd5\u306b\u3088\u308b\u3082\u306e\u3067\u3059\u300232\u30d3\u30c3\u30c8\u306e\u4e8c\u9032\u6570\u3092\u6b21\u306e\u3088\u3046\u306b\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\uff1a

        \\[ b_{31} b_{30} b_{29} \\ldots b_2 b_1 b_0 \\]

        IEEE 754\u6a19\u6e96\u306b\u3088\u308b\u3068\u300132\u30d3\u30c3\u30c8\u306efloat\u306f\u6b21\u306e3\u3064\u306e\u90e8\u5206\u3067\u69cb\u6210\u3055\u308c\u307e\u3059\uff1a

        • \u7b26\u53f7\u30d3\u30c3\u30c8\\(\\mathrm{S}\\)\uff1a1\u30d3\u30c3\u30c8\u3092\u5360\u6709\u3057\u3001\\(b_{31}\\)\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002
        • \u6307\u6570\u30d3\u30c3\u30c8\\(\\mathrm{E}\\)\uff1a8\u30d3\u30c3\u30c8\u3092\u5360\u6709\u3057\u3001\\(b_{30} b_{29} \\ldots b_{23}\\)\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002
        • \u4eee\u6570\u30d3\u30c3\u30c8\\(\\mathrm{N}\\)\uff1a23\u30d3\u30c3\u30c8\u3092\u5360\u6709\u3057\u3001\\(b_{22} b_{21} \\ldots b_0\\)\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002

        \u4e8c\u9032float\u6570\u306e\u5024\u306f\u6b21\u306e\u3088\u3046\u306b\u8a08\u7b97\u3055\u308c\u307e\u3059\uff1a

        \\[ \\text{val} = (-1)^{b_{31}} \\times 2^{\\left(b_{30} b_{29} \\ldots b_{23}\\right)_2 - 127} \\times \\left(1 . b_{22} b_{21} \\ldots b_0\\right)_2 \\]

        \u5341\u9032\u516c\u5f0f\u306b\u5909\u63db\u3059\u308b\u3068\u3001\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\uff1a

        \\[ \\text{val} = (-1)^{\\mathrm{S}} \\times 2^{\\mathrm{E} - 127} \\times (1 + \\mathrm{N}) \\]

        \u5404\u6210\u5206\u306e\u7bc4\u56f2\u306f\uff1a

        \\[ \\begin{aligned} \\mathrm{S} \\in & \\{ 0, 1\\}, \\quad \\mathrm{E} \\in \\{ 1, 2, \\dots, 254 \\} \\newline (1 + \\mathrm{N}) = & (1 + \\sum_{i=1}^{23} b_{23-i} \\times 2^{-i}) \\subset [1, 2 - 2^{-23}] \\end{aligned} \\]

        \u56f3 3-5 \u00a0 IEEE 754\u6a19\u6e96\u3067\u306e\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u306e\u8a08\u7b97\u4f8b

        \u4e0a\u306e\u56f3\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\u4f8b\u306e\u30c7\u30fc\u30bf\\(\\mathrm{S} = 0\\)\u3001\\(\\mathrm{E} = 124\\)\u3001\\(\\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\\)\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\uff1a

        \\[ \\text{val} = (-1)^0 \\times 2^{124 - 127} \\times (1 + 0.375) = 0.171875 \\]

        \u3053\u308c\u3067\u6700\u521d\u306e\u8cea\u554f\u306b\u7b54\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1afloat\u306e\u8868\u73fe\u306b\u306f\u6307\u6570\u30d3\u30c3\u30c8\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u305f\u3081\u3001int\u3088\u308a\u3082\u306f\u308b\u304b\u306b\u5927\u304d\u3044\u7bc4\u56f2\u3092\u6301\u3061\u307e\u3059\u3002\u4e0a\u8a18\u306e\u8a08\u7b97\u306b\u57fa\u3065\u304f\u3068\u3001float\u3067\u8868\u73fe\u53ef\u80fd\u306a\u6700\u5927\u6b63\u306e\u6570\u306f\u7d04\\(2^{254 - 127} \\times (2 - 2^{-23}) \\approx 3.4 \\times 10^{38}\\)\u3067\u3001\u6700\u5c0f\u8ca0\u306e\u6570\u306f\u7b26\u53f7\u30d3\u30c3\u30c8\u3092\u5207\u308a\u66ff\u3048\u308b\u3053\u3068\u3067\u5f97\u3089\u308c\u307e\u3059\u3002

        \u3057\u304b\u3057\u3001float\u306e\u62e1\u5f35\u3055\u308c\u305f\u7bc4\u56f2\u306e\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u306f\u7cbe\u5ea6\u306e\u72a0\u7272\u3067\u3059\u3002\u6574\u6570\u578bint\u306f32\u30d3\u30c3\u30c8\u3059\u3079\u3066\u3092\u6570\u5024\u8868\u73fe\u306b\u4f7f\u7528\u3057\u3001\u5024\u306f\u5747\u7b49\u306b\u5206\u5e03\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u6307\u6570\u30d3\u30c3\u30c8\u306e\u305f\u3081\u3001float\u306e\u5024\u304c\u5927\u304d\u3044\u307b\u3069\u3001\u96a3\u63a5\u3059\u308b\u6570\u5024\u9593\u306e\u5dee\u304c\u5927\u304d\u304f\u306a\u308a\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u8868\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u6307\u6570\u30d3\u30c3\u30c8\\(\\mathrm{E} = 0\\)\u3068\\(\\mathrm{E} = 255\\)\u306f\u7279\u5225\u306a\u610f\u5473\u3092\u6301\u3061\u30010\u3001\u7121\u9650\u5927\u3001\\(\\mathrm{NaN}\\)\u306a\u3069\u3092\u8868\u73fe\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002

        \u8868 3-2 \u00a0 \u6307\u6570\u30d3\u30c3\u30c8\u306e\u610f\u5473

        \u6307\u6570\u30d3\u30c3\u30c8E \u4eee\u6570\u30d3\u30c3\u30c8\\(\\mathrm{N} = 0\\) \u4eee\u6570\u30d3\u30c3\u30c8\\(\\mathrm{N} \\ne 0\\) \u8a08\u7b97\u516c\u5f0f \\(0\\) \\(\\pm 0\\) \u975e\u6b63\u898f\u5316\u6570 \\((-1)^{\\mathrm{S}} \\times 2^{-126} \\times (0.\\mathrm{N})\\) \\(1, 2, \\dots, 254\\) \u6b63\u898f\u5316\u6570 \u6b63\u898f\u5316\u6570 \\((-1)^{\\mathrm{S}} \\times 2^{(\\mathrm{E} -127)} \\times (1.\\mathrm{N})\\) \\(255\\) \\(\\pm \\infty\\) \\(\\mathrm{NaN}\\)

        \u975e\u6b63\u898f\u5316\u6570\u306f\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u306e\u7cbe\u5ea6\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u306f\u6ce8\u76ee\u306b\u5024\u3057\u307e\u3059\u3002\u6700\u5c0f\u306e\u6b63\u306e\u6b63\u898f\u5316\u6570\u306f\\(2^{-126}\\)\u3067\u3001\u6700\u5c0f\u306e\u6b63\u306e\u975e\u6b63\u898f\u5316\u6570\u306f\\(2^{-126} \\times 2^{-23}\\)\u3067\u3059\u3002

        \u500d\u7cbe\u5ea6double\u3082float\u3068\u540c\u69d8\u306e\u8868\u73fe\u65b9\u6cd5\u3092\u4f7f\u7528\u3057\u307e\u3059\u304c\u3001\u7c21\u6f54\u3055\u306e\u305f\u3081\u3053\u3053\u3067\u306f\u8a73\u8ff0\u3057\u307e\u305b\u3093\u3002

        "},{"location":"chapter_data_structure/summary/","title":"3.5 \u00a0 \u307e\u3068\u3081","text":""},{"location":"chapter_data_structure/summary/#1","title":"1. \u00a0 \u91cd\u8981\u306a\u30dd\u30a4\u30f3\u30c8","text":"
        • \u30c7\u30fc\u30bf\u69cb\u9020\u306f\u8ad6\u7406\u69cb\u9020\u3068\u7269\u7406\u69cb\u9020\u306e2\u3064\u306e\u89b3\u70b9\u304b\u3089\u5206\u985e\u3067\u304d\u307e\u3059\u3002\u8ad6\u7406\u69cb\u9020\u306f\u30c7\u30fc\u30bf\u9593\u306e\u8ad6\u7406\u7684\u95a2\u4fc2\u3092\u8a18\u8ff0\u3057\u3001\u7269\u7406\u69cb\u9020\u306f\u30c7\u30fc\u30bf\u304c\u30e1\u30e2\u30ea\u306b\u3069\u306e\u3088\u3046\u306b\u683c\u7d0d\u3055\u308c\u308b\u304b\u3092\u8a18\u8ff0\u3057\u307e\u3059\u3002
        • \u3088\u304f\u4f7f\u7528\u3055\u308c\u308b\u8ad6\u7406\u69cb\u9020\u306b\u306f\u3001\u7dda\u5f62\u69cb\u9020\u3001\u6728\u3001\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u304c\u3042\u308a\u307e\u3059\u3002\u901a\u5e38\u3001\u8ad6\u7406\u69cb\u9020\u306b\u57fa\u3065\u3044\u3066\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u7dda\u5f62\uff08\u914d\u5217\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u30b9\u30bf\u30c3\u30af\u3001\u30ad\u30e5\u30fc\uff09\u3068\u975e\u7dda\u5f62\uff08\u6728\u3001\u30b0\u30e9\u30d5\u3001\u30d2\u30fc\u30d7\uff09\u306b\u5206\u3051\u307e\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u5b9f\u88c5\u306f\u7dda\u5f62\u3068\u975e\u7dda\u5f62\u306e\u4e21\u65b9\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u542b\u3080\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002
        • \u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u5b9f\u884c\u4e2d\u306e\u969b\u3001\u30c7\u30fc\u30bf\u306f\u30e1\u30e2\u30ea\u306b\u683c\u7d0d\u3055\u308c\u307e\u3059\u3002\u5404\u30e1\u30e2\u30ea\u7a7a\u9593\u306b\u306f\u5bfe\u5fdc\u3059\u308b\u30a2\u30c9\u30ec\u30b9\u304c\u3042\u308a\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u3053\u308c\u3089\u306e\u30a2\u30c9\u30ec\u30b9\u3092\u901a\u3058\u3066\u30c7\u30fc\u30bf\u306b\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002
        • \u7269\u7406\u69cb\u9020\u306f\u9023\u7d9a\u7a7a\u9593\u683c\u7d0d\uff08\u914d\u5217\uff09\u3068\u96e2\u6563\u7a7a\u9593\u683c\u7d0d\uff08\u9023\u7d50\u30ea\u30b9\u30c8\uff09\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3059\u3079\u3066\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u914d\u5217\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u307e\u305f\u306f\u305d\u306e\u4e21\u65b9\u306e\u7d44\u307f\u5408\u308f\u305b\u3092\u4f7f\u7528\u3057\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002
        • \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u306b\u306f\u3001\u6574\u6570\uff08byte\u3001short\u3001int\u3001long\uff09\u3001\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\uff08float\u3001double\uff09\u3001\u6587\u5b57\uff08char\uff09\u3001\u30d6\u30fc\u30eb\u5024\uff08bool\uff09\u304c\u542b\u307e\u308c\u307e\u3059\u3002\u30c7\u30fc\u30bf\u578b\u306e\u5024\u306e\u7bc4\u56f2\u306f\u3001\u305d\u306e\u30b5\u30a4\u30ba\u3068\u8868\u73fe\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002
        • \u7b26\u53f7\u7d76\u5bfe\u5024\u30011\u306e\u88dc\u6570\u30012\u306e\u88dc\u6570\u306f\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u3067\u6574\u6570\u3092\u30a8\u30f3\u30b3\u30fc\u30c9\u3059\u308b3\u3064\u306e\u65b9\u6cd5\u3067\u3042\u308a\u3001\u76f8\u4e92\u306b\u5909\u63db\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u7b26\u53f7\u7d76\u5bfe\u5024\u306e\u6700\u4e0a\u4f4d\u30d3\u30c3\u30c8\u306f\u7b26\u53f7\u30d3\u30c3\u30c8\u3067\u3001\u6b8b\u308a\u306e\u30d3\u30c3\u30c8\u306f\u6570\u5024\u306e\u5024\u3092\u8868\u3057\u307e\u3059\u3002
        • \u6574\u6570\u306f\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30672\u306e\u88dc\u6570\u306b\u3088\u3063\u3066\u30a8\u30f3\u30b3\u30fc\u30c9\u3055\u308c\u307e\u3059\u3002\u3053\u306e\u8868\u73fe\u306e\u5229\u70b9\u306b\u306f\u3001(i)\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u304c\u6b63\u3068\u8ca0\u306e\u6574\u6570\u306e\u52a0\u7b97\u3092\u7d71\u4e00\u3067\u304d\u308b\u3001(ii)\u6e1b\u7b97\u7528\u306e\u7279\u5225\u306a\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u56de\u8def\u3092\u8a2d\u8a08\u3059\u308b\u5fc5\u8981\u304c\u306a\u3044\u3001(iii)\u6b63\u3068\u8ca0\u306e0\u306e\u66d6\u6627\u3055\u304c\u306a\u3044\u3001\u304c\u3042\u308a\u307e\u3059\u3002
        • \u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u306e\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306f\u30011\u3064\u306e\u7b26\u53f7\u30d3\u30c3\u30c8\u30018\u3064\u306e\u6307\u6570\u30d3\u30c3\u30c8\u300123\u306e\u4eee\u6570\u30d3\u30c3\u30c8\u3067\u69cb\u6210\u3055\u308c\u307e\u3059\u3002\u6307\u6570\u30d3\u30c3\u30c8\u306e\u305f\u3081\u3001\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u306e\u7bc4\u56f2\u306f\u6574\u6570\u3088\u308a\u3082\u306f\u308b\u304b\u306b\u5927\u304d\u304f\u306a\u308a\u307e\u3059\u304c\u3001\u7cbe\u5ea6\u3092\u72a0\u7272\u306b\u3057\u307e\u3059\u3002
        • ASCII\u306f\u6700\u521d\u671f\u306e\u82f1\u8a9e\u6587\u5b57\u30bb\u30c3\u30c8\u3067\u30011\u30d0\u30a4\u30c8\u306e\u9577\u3055\u3067\u8a08127\u6587\u5b57\u3067\u3059\u3002GBK\u306f\u4eba\u6c17\u306e\u3042\u308b\u4e2d\u56fd\u8a9e\u6587\u5b57\u30bb\u30c3\u30c8\u3067\u30012\u4e07\u6587\u5b57\u4ee5\u4e0a\u306e\u4e2d\u56fd\u8a9e\u6587\u5b57\u3092\u542b\u307f\u307e\u3059\u3002Unicode\u306f\u4e16\u754c\u306e\u69d8\u3005\u306a\u8a00\u8a9e\u306e\u6587\u5b57\u3092\u542b\u3080\u5b8c\u5168\u306a\u6587\u5b57\u30bb\u30c3\u30c8\u6a19\u6e96\u3092\u63d0\u4f9b\u3059\u308b\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u3001\u6587\u5b57\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u65b9\u6cd5\u306e\u4e0d\u4e00\u81f4\u306b\u3088\u308b\u6587\u5b57\u5316\u3051\u554f\u984c\u3092\u89e3\u6c7a\u3057\u307e\u3059\u3002
        • UTF-8\u306f\u6700\u3082\u4eba\u6c17\u304c\u3042\u308a\u4e00\u822c\u7684\u306aUnicode\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u65b9\u6cd5\u3067\u3059\u3002\u3053\u308c\u306f\u53ef\u5909\u9577\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u65b9\u6cd5\u3067\u3001\u512a\u308c\u305f\u62e1\u5f35\u6027\u3068\u7a7a\u9593\u52b9\u7387\u3092\u6301\u3061\u307e\u3059\u3002UTF-16\u3068UTF-32\u306f\u56fa\u5b9a\u9577\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u65b9\u6cd5\u3067\u3059\u3002\u4e2d\u56fd\u8a9e\u6587\u5b57\u3092\u30a8\u30f3\u30b3\u30fc\u30c9\u3059\u308b\u969b\u3001UTF-16\u306fUTF-8\u3088\u308a\u3082\u5c11\u306a\u3044\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002Java\u3084C#\u306a\u3069\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067UTF-16\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        "},{"location":"chapter_data_structure/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q: \u306a\u305c\u30cf\u30c3\u30b7\u30e5\u8868\u306f\u7dda\u5f62\u3068\u975e\u7dda\u5f62\u306e\u4e21\u65b9\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u542b\u3080\u306e\u3067\u3059\u304b\uff1f

        \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u57fa\u790e\u69cb\u9020\u306f\u914d\u5217\u3067\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3001\u300c\u30c1\u30a7\u30a4\u30f3\u6cd5\u300d\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\uff08\u5f8c\u306e\u7bc0\u300c\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u300d\u3067\u8aac\u660e\uff09\uff1a\u914d\u5217\u306e\u5404\u30d0\u30b1\u30c3\u30c8\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u6307\u3057\u3001\u305d\u306e\u9577\u3055\u304c\u7279\u5b9a\u306e\u95be\u5024\u3088\u308a\u5927\u304d\u304f\u306a\u308b\u3068\u6728\uff08\u901a\u5e38\u306f\u8d64\u9ed2\u6728\uff09\u306b\u5909\u63db\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002 \u683c\u7d0d\u306e\u89b3\u70b9\u304b\u3089\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u57fa\u790e\u69cb\u9020\u306f\u914d\u5217\u3067\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u306b\u306f\u5024\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u307e\u305f\u306f\u6728\u304c\u542b\u307e\u308c\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306f\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\uff08\u914d\u5217\u3001\u9023\u7d50\u30ea\u30b9\u30c8\uff09\u3068\u975e\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\uff08\u6728\uff09\u306e\u4e21\u65b9\u3092\u542b\u3080\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002

        Q: char\u578b\u306e\u9577\u3055\u306f1\u30d0\u30a4\u30c8\u3067\u3059\u304b\uff1f

        char\u578b\u306e\u9577\u3055\u306f\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u65b9\u6cd5\u306b\u3088\u3063\u3066\u6c7a\u307e\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001Java\u3001JavaScript\u3001TypeScript\u3001C#\u306f\u3059\u3079\u3066UTF-16\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\uff08Unicode\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u3092\u4fdd\u5b58\u3059\u308b\u305f\u3081\uff09\u3092\u4f7f\u7528\u3059\u308b\u305f\u3081\u3001char\u578b\u306e\u9577\u3055\u306f2\u30d0\u30a4\u30c8\u3067\u3059\u3002

        Q: \u914d\u5217\u30d9\u30fc\u30b9\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u300c\u9759\u7684\u30c7\u30fc\u30bf\u69cb\u9020\u300d\u3068\u547c\u3076\u3053\u3068\u306b\u66d6\u6627\u3055\u306f\u3042\u308a\u307e\u305b\u3093\u304b\uff1f\u30b9\u30bf\u30c3\u30af\u3082\u30d7\u30c3\u30b7\u30e5\u3084\u30dd\u30c3\u30d7\u306a\u3069\u306e\u300c\u52d5\u7684\u300d\u64cd\u4f5c\u3092\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002

        \u30b9\u30bf\u30c3\u30af\u306f\u52d5\u7684\u306a\u30c7\u30fc\u30bf\u64cd\u4f5c\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\u304c\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u4f9d\u7136\u3068\u3057\u3066\u300c\u9759\u7684\u300d\u3067\u3059\uff08\u9577\u3055\u304c\u56fa\u5b9a\uff09\u3002\u914d\u5217\u30d9\u30fc\u30b9\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u52d5\u7684\u306b\u8981\u7d20\u3092\u8ffd\u52a0\u307e\u305f\u306f\u524a\u9664\u3067\u304d\u307e\u3059\u304c\u3001\u305d\u306e\u5bb9\u91cf\u306f\u56fa\u5b9a\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u30b9\u30bf\u30c3\u30af\u30b5\u30a4\u30ba\u304c\u4e8b\u524d\u306b\u5272\u308a\u5f53\u3066\u3089\u308c\u305f\u30b5\u30a4\u30ba\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u53e4\u3044\u914d\u5217\u306f\u65b0\u3057\u304f\u4f5c\u6210\u3055\u308c\u305f\u3088\u308a\u5927\u304d\u306a\u914d\u5217\u306b\u30b3\u30d4\u30fc\u3055\u308c\u307e\u3059\u3002

        Q: \u30b9\u30bf\u30c3\u30af\uff08\u30ad\u30e5\u30fc\uff09\u3092\u69cb\u7bc9\u3059\u308b\u969b\u3001\u305d\u306e\u30b5\u30a4\u30ba\u304c\u6307\u5b9a\u3055\u308c\u3066\u3044\u306a\u3044\u306e\u306b\u3001\u306a\u305c\u300c\u9759\u7684\u30c7\u30fc\u30bf\u69cb\u9020\u300d\u306a\u306e\u3067\u3059\u304b\uff1f

        \u9ad8\u7d1a\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u306f\u3001\u30b9\u30bf\u30c3\u30af\uff08\u30ad\u30e5\u30fc\uff09\u306e\u521d\u671f\u5bb9\u91cf\u3092\u624b\u52d5\u3067\u6307\u5b9a\u3059\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u3053\u306e\u30bf\u30b9\u30af\u306f\u30af\u30e9\u30b9\u5185\u3067\u81ea\u52d5\u7684\u306b\u5b8c\u4e86\u3055\u308c\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001Java\u306eArrayList\u306e\u521d\u671f\u5bb9\u91cf\u306f\u901a\u5e3810\u3067\u3059\u3002\u3055\u3089\u306b\u3001\u62e1\u5f35\u64cd\u4f5c\u3082\u81ea\u52d5\u7684\u306b\u5b8c\u4e86\u3055\u308c\u307e\u3059\u3002\u8a73\u7d30\u306b\u3064\u3044\u3066\u306f\u3001\u5f8c\u7d9a\u306e\u300c\u30ea\u30b9\u30c8\u300d\u306e\u7ae0\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        Q: \u7b26\u53f7\u7d76\u5bfe\u5024\u30922\u306e\u88dc\u6570\u306b\u5909\u63db\u3059\u308b\u65b9\u6cd5\u306f\u300c\u6700\u521d\u306b\u5426\u5b9a\u3057\u3066\u304b\u30891\u3092\u52a0\u3048\u308b\u300d\u3067\u3059\u306e\u3067\u30012\u306e\u88dc\u6570\u3092\u7b26\u53f7\u7d76\u5bfe\u5024\u306b\u5909\u63db\u3059\u308b\u3053\u3068\u306f\u305d\u306e\u9006\u64cd\u4f5c\u300c\u6700\u521d\u306b1\u3092\u6e1b\u7b97\u3057\u3066\u304b\u3089\u5426\u5b9a\u3059\u308b\u300d\u3067\u3042\u308b\u3079\u304d\u3067\u3059\u3002 \u3057\u304b\u3057\u30012\u306e\u88dc\u6570\u3082\u300c\u6700\u521d\u306b\u5426\u5b9a\u3057\u3066\u304b\u30891\u3092\u52a0\u3048\u308b\u300d\u3092\u901a\u3058\u3066\u7b26\u53f7\u7d76\u5bfe\u5024\u306b\u5909\u63db\u3067\u304d\u307e\u3059\u3002\u306a\u305c\u3067\u3059\u304b\uff1f

        A: \u3053\u308c\u306f\u3001\u7b26\u53f7\u7d76\u5bfe\u5024\u30682\u306e\u88dc\u6570\u9593\u306e\u76f8\u4e92\u5909\u63db\u304c\u300c\u88dc\u6570\u300d\u306e\u8a08\u7b97\u3068\u7b49\u4fa1\u3060\u304b\u3089\u3067\u3059\u3002\u307e\u305a\u88dc\u6570\u3092\u5b9a\u7fa9\u3057\u307e\u3059\uff1a\\(a + b = c\\)\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\\(a\\)\u306f\\(b\\)\u306e\\(c\\)\u306b\u5bfe\u3059\u308b\u88dc\u6570\u3068\u8a00\u3044\u3001\u9006\u306b\\(b\\)\u306f\\(a\\)\u306e\\(c\\)\u306b\u5bfe\u3059\u308b\u88dc\u6570\u3068\u8a00\u3044\u307e\u3059\u3002

        \u9577\u3055\\(n = 4\\)\u306e\u4e8c\u9032\u6570\\(0010\\)\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u3053\u306e\u6570\u304c\u7b26\u53f7\u7d76\u5bfe\u5024\uff08\u7b26\u53f7\u30d3\u30c3\u30c8\u3092\u7121\u8996\uff09\u306e\u5834\u5408\u3001\u305d\u306e2\u306e\u88dc\u6570\u306f\u300c\u6700\u521d\u306b\u5426\u5b9a\u3057\u3066\u304b\u30891\u3092\u52a0\u3048\u308b\u300d\u3053\u3068\u3067\u5f97\u3089\u308c\u307e\u3059\uff1a

        \\[ 0010 \\rightarrow 1101 \\rightarrow 1110 \\]

        \u7b26\u53f7\u7d76\u5bfe\u5024\u30682\u306e\u88dc\u6570\u306e\u548c\u304c\\(0010 + 1110 = 10000\\)\u3067\u3042\u308b\u3053\u3068\u3092\u89b3\u5bdf\u3057\u307e\u3059\u3002\u3064\u307e\u308a\u30012\u306e\u88dc\u6570\\(1110\\)\u306f\u7b26\u53f7\u7d76\u5bfe\u5024\\(0010\\)\u306e\\(10000\\)\u306b\u5bfe\u3059\u308b\u300c\u88dc\u6570\u300d\u3067\u3059\u3002\u3053\u308c\u306f\u3001\u4e0a\u8a18\u306e\u300c\u6700\u521d\u306b\u5426\u5b9a\u3057\u3066\u304b\u30891\u3092\u52a0\u3048\u308b\u300d\u304c\\(10000\\)\u306b\u5bfe\u3059\u308b\u88dc\u6570\u306e\u8a08\u7b97\u3068\u7b49\u4fa1\u3067\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

        \u3067\u306f\u3001\\(1110\\)\u306e\\(10000\\)\u306b\u5bfe\u3059\u308b\u300c\u88dc\u6570\u300d\u306f\u4f55\u3067\u3057\u3087\u3046\u304b\uff1f\u300c\u6700\u521d\u306b\u5426\u5b9a\u3057\u3066\u304b\u30891\u3092\u52a0\u3048\u308b\u300d\u3053\u3068\u3067\u8a08\u7b97\u3067\u304d\u307e\u3059\uff1a

        \\[ 1110 \\rightarrow 0001 \\rightarrow 0010 \\]

        \u8a00\u3044\u63db\u3048\u308b\u3068\u3001\u7b26\u53f7\u7d76\u5bfe\u5024\u30682\u306e\u88dc\u6570\u306f\u4e92\u3044\u306b\\(10000\\)\u306b\u5bfe\u3059\u308b\u300c\u88dc\u6570\u300d\u3067\u3042\u308b\u305f\u3081\u3001\u300c\u7b26\u53f7\u7d76\u5bfe\u5024\u304b\u30892\u306e\u88dc\u6570\u300d\u3068\u300c2\u306e\u88dc\u6570\u304b\u3089\u7b26\u53f7\u7d76\u5bfe\u5024\u300d\u306f\u540c\u3058\u64cd\u4f5c\uff08\u6700\u521d\u306b\u5426\u5b9a\u3057\u3066\u304b\u30891\u3092\u52a0\u3048\u308b\uff09\u3067\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002

        \u3082\u3061\u308d\u3093\u3001\u300c\u6700\u521d\u306b\u5426\u5b9a\u3057\u3066\u304b\u30891\u3092\u52a0\u3048\u308b\u300d\u306e\u9006\u64cd\u4f5c\u3092\u4f7f\u7528\u3057\u30662\u306e\u88dc\u6570\\(1110\\)\u306e\u7b26\u53f7\u7d76\u5bfe\u5024\u3092\u6c42\u3081\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u300c\u6700\u521d\u306b1\u3092\u6e1b\u7b97\u3057\u3066\u304b\u3089\u5426\u5b9a\u3059\u308b\u300d\uff1a

        \\[ 1110 \\rightarrow 1101 \\rightarrow 0010 \\]

        \u8981\u7d04\u3059\u308b\u3068\u3001\u300c\u6700\u521d\u306b\u5426\u5b9a\u3057\u3066\u304b\u30891\u3092\u52a0\u3048\u308b\u300d\u3068\u300c\u6700\u521d\u306b1\u3092\u6e1b\u7b97\u3057\u3066\u304b\u3089\u5426\u5b9a\u3059\u308b\u300d\u306f\u4e21\u65b9\u3068\u3082\\(10000\\)\u306b\u5bfe\u3059\u308b\u88dc\u6570\u3092\u8a08\u7b97\u3057\u3066\u304a\u308a\u3001\u7b49\u4fa1\u3067\u3059\u3002

        \u672c\u8cea\u7684\u306b\u3001\u300c\u5426\u5b9a\u300d\u64cd\u4f5c\u306f\u5b9f\u969b\u306b\u306f\\(1111\\)\u306b\u5bfe\u3059\u308b\u88dc\u6570\u3092\u6c42\u3081\u308b\u3053\u3068\u3067\u3059\uff08\u7b26\u53f7\u7d76\u5bfe\u5024 + 1\u306e\u88dc\u6570 = 1111\u304c\u5e38\u306b\u6210\u308a\u7acb\u3064\u305f\u3081\uff09\u3002\u305d\u3057\u30661\u306e\u88dc\u6570\u306b1\u3092\u52a0\u3048\u308b\u3053\u3068\u306f\\(10000\\)\u306b\u5bfe\u3059\u308b2\u306e\u88dc\u6570\u3068\u7b49\u3057\u304f\u306a\u308a\u307e\u3059\u3002

        \u4e0a\u8a18\u3067\u306f\\(n = 4\\)\u3092\u4f8b\u306b\u53d6\u308a\u307e\u3057\u305f\u304c\u3001\u4efb\u610f\u306e\u6841\u6570\u306e\u4efb\u610f\u306e\u4e8c\u9032\u6570\u306b\u4e00\u822c\u5316\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_divide_and_conquer/","title":"\u7b2c 12 \u7ae0 \u00a0 \u5206\u5272\u7d71\u6cbb","text":"

        Abstract

        \u56f0\u96e3\u306a\u554f\u984c\u306f\u5c64\u3092\u91cd\u306d\u3066\u5206\u89e3\u3055\u308c\u3001\u5404\u5206\u89e3\u306b\u3088\u3063\u3066\u3088\u308a\u5358\u7d14\u306b\u306a\u308a\u307e\u3059\u3002

        \u5206\u5272\u7d71\u6cbb\u306f\u6df1\u3044\u771f\u7406\u3092\u660e\u3089\u304b\u306b\u3057\u307e\u3059\uff1a\u5358\u7d14\u3055\u304b\u3089\u59cb\u3081\u308c\u3070\u3001\u8907\u96d1\u3055\u306f\u89e3\u6c7a\u3055\u308c\u308b\u3002

        "},{"location":"chapter_divide_and_conquer/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 12.1 \u00a0 \u5206\u5272\u7d71\u6cbb\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0
        • 12.2 \u00a0 \u5206\u5272\u7d71\u6cbb\u63a2\u7d22\u6226\u7565
        • 12.3 \u00a0 \u4e8c\u5206\u6728\u69cb\u7bc9\u554f\u984c
        • 12.4 \u00a0 \u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c
        • 12.5 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_divide_and_conquer/binary_search_recur/","title":"12.2 \u00a0 \u5206\u5272\u7d71\u6cbb\u691c\u7d22\u6226\u7565","text":"

        \u79c1\u305f\u3061\u306f\u691c\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u4e3b\u306b2\u3064\u306e\u30ab\u30c6\u30b4\u30ea\u306b\u5206\u985e\u3055\u308c\u308b\u3053\u3068\u3092\u5b66\u3073\u307e\u3057\u305f\u3002

        • \u7dcf\u5f53\u305f\u308a\u691c\u7d22\uff1a\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u8d70\u67fb\u3059\u308b\u3053\u3068\u3067\u5b9f\u88c5\u3055\u308c\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u3067\u3059\u3002
        • \u9069\u5fdc\u691c\u7d22\uff1a\u72ec\u7279\u306a\u30c7\u30fc\u30bf\u7d44\u7e54\u5f62\u5f0f\u3084\u4e8b\u524d\u60c5\u5831\u3092\u5229\u7528\u3057\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(\\log n)\\) \u307e\u305f\u306f \\(O(1)\\) \u306b\u9054\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u5b9f\u969b\u3001\u6642\u9593\u8a08\u7b97\u91cf\u304c \\(O(\\log n)\\) \u306e\u691c\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u901a\u5e38\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306b\u57fa\u3065\u3044\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u4e8c\u5206\u63a2\u7d22\u3084\u6728\u306a\u3069\u3067\u3059\u3002

        • \u4e8c\u5206\u63a2\u7d22\u306e\u5404\u30b9\u30c6\u30c3\u30d7\u306f\u3001\u554f\u984c\uff08\u914d\u5217\u5185\u3067\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u3092\u691c\u7d22\u3059\u308b\uff09\u3092\u3088\u308a\u5c0f\u3055\u306a\u554f\u984c\uff08\u914d\u5217\u306e\u534a\u5206\u3067\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u3092\u691c\u7d22\u3059\u308b\uff09\u306b\u5206\u5272\u3057\u3001\u914d\u5217\u304c\u7a7a\u306b\u306a\u308b\u304b\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u304c\u898b\u3064\u304b\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002
        • \u6728\u306f\u5206\u5272\u7d71\u6cbb\u306e\u30a2\u30a4\u30c7\u30a2\u3092\u8868\u73fe\u3057\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u3001AVL\u6728\u3001\u30d2\u30fc\u30d7\u306a\u3069\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u306f\u3001\u69d8\u3005\u306a\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(\\log n)\\) \u3067\u3059\u3002

        \u4e8c\u5206\u63a2\u7d22\u306e\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002

        • \u554f\u984c\u3092\u5206\u5272\u3067\u304d\u308b\uff1a\u4e8c\u5206\u63a2\u7d22\u306f\u5143\u306e\u554f\u984c\uff08\u914d\u5217\u5185\u3067\u306e\u691c\u7d22\uff09\u3092\u90e8\u5206\u554f\u984c\uff08\u914d\u5217\u306e\u534a\u5206\u3067\u306e\u691c\u7d22\uff09\u306b\u518d\u5e30\u7684\u306b\u5206\u5272\u3057\u3001\u4e2d\u9593\u8981\u7d20\u3068\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u3092\u6bd4\u8f03\u3059\u308b\u3053\u3068\u3067\u5b9f\u73fe\u3055\u308c\u307e\u3059\u3002
        • \u90e8\u5206\u554f\u984c\u306f\u72ec\u7acb\u3057\u3066\u3044\u308b\uff1a\u4e8c\u5206\u63a2\u7d22\u3067\u306f\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u4e00\u3064\u306e\u90e8\u5206\u554f\u984c\u3092\u51e6\u7406\u3057\u3001\u4ed6\u306e\u90e8\u5206\u554f\u984c\u306b\u5f71\u97ff\u3055\u308c\u307e\u305b\u3093\u3002
        • \u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u30de\u30fc\u30b8\u3059\u308b\u5fc5\u8981\u304c\u306a\u3044\uff1a\u4e8c\u5206\u63a2\u7d22\u306f\u7279\u5b9a\u306e\u8981\u7d20\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u30de\u30fc\u30b8\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u305b\u3093\u3002\u90e8\u5206\u554f\u984c\u304c\u89e3\u6c7a\u3055\u308c\u308b\u3068\u3001\u5143\u306e\u554f\u984c\u3082\u89e3\u6c7a\u3055\u308c\u307e\u3059\u3002

        \u5206\u5272\u7d71\u6cbb\u306f\u691c\u7d22\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u306a\u305c\u306a\u3089\u3001\u7dcf\u5f53\u305f\u308a\u691c\u7d22\u306f\u30e9\u30a6\u30f3\u30c9\u3054\u3068\u306b1\u3064\u306e\u9078\u629e\u80a2\u3057\u304b\u9664\u53bb\u3067\u304d\u307e\u305b\u3093\u304c\u3001\u5206\u5272\u7d71\u6cbb\u306f\u9078\u629e\u80a2\u306e\u534a\u5206\u3092\u9664\u53bb\u3067\u304d\u308b\u304b\u3089\u3067\u3059\u3002

        "},{"location":"chapter_divide_and_conquer/binary_search_recur/#1","title":"1. \u00a0 \u5206\u5272\u7d71\u6cbb\u306b\u57fa\u3065\u304f\u4e8c\u5206\u63a2\u7d22\u306e\u5b9f\u88c5","text":"

        \u524d\u306e\u7ae0\u3067\u306f\u3001\u4e8c\u5206\u63a2\u7d22\u306f\u53cd\u5fa9\u306b\u57fa\u3065\u3044\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3057\u305f\u3002\u4eca\u5ea6\u306f\u3001\u5206\u5272\u7d71\u6cbb\uff08\u518d\u5e30\uff09\u306b\u57fa\u3065\u3044\u3066\u5b9f\u88c5\u3057\u307e\u3059\u3002

        Question

        \u9577\u3055 \\(n\\) \u306e\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u914d\u5217 nums \u304c\u4e0e\u3048\u3089\u308c\u3001\u3059\u3079\u3066\u306e\u8981\u7d20\u304c\u4e00\u610f\u3067\u3042\u308b\u5834\u5408\u3001\u8981\u7d20 target \u3092\u898b\u3064\u3051\u3066\u304f\u3060\u3055\u3044\u3002

        \u5206\u5272\u7d71\u6cbb\u306e\u89b3\u70b9\u304b\u3089\u3001\u691c\u7d22\u533a\u9593 \\([i, j]\\) \u306b\u5bfe\u5fdc\u3059\u308b\u90e8\u5206\u554f\u984c\u3092 \\(f(i, j)\\) \u3068\u8868\u3057\u307e\u3059\u3002

        \u5143\u306e\u554f\u984c \\(f(0, n-1)\\) \u304b\u3089\u958b\u59cb\u3057\u3066\u3001\u4ee5\u4e0b\u306e\u30b9\u30c6\u30c3\u30d7\u3067\u4e8c\u5206\u63a2\u7d22\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002

        1. \u691c\u7d22\u533a\u9593 \\([i, j]\\) \u306e\u4e2d\u70b9 \\(m\\) \u3092\u8a08\u7b97\u3057\u3001\u305d\u308c\u3092\u4f7f\u7528\u3057\u3066\u691c\u7d22\u533a\u9593\u306e\u534a\u5206\u3092\u9664\u53bb\u3057\u307e\u3059\u3002
        2. \u534a\u5206\u306e\u30b5\u30a4\u30ba\u306b\u7e2e\u5c0f\u3055\u308c\u305f\u90e8\u5206\u554f\u984c\u3092\u518d\u5e30\u7684\u306b\u89e3\u6c7a\u3057\u307e\u3059\u3002\u3053\u308c\u306f \\(f(i, m-1)\\) \u307e\u305f\u306f \\(f(m+1, j)\\) \u306b\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        3. target \u304c\u898b\u3064\u304b\u308b\u304b\u533a\u9593\u304c\u7a7a\u306b\u306a\u3063\u3066\u30ea\u30bf\u30fc\u30f3\u3059\u308b\u307e\u3067\u3001\u30b9\u30c6\u30c3\u30d7 1. \u3068 2. \u3092\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306f\u3001\u914d\u5217\u5185\u3067\u8981\u7d20 \\(6\\) \u3092\u63a2\u3059\u4e8c\u5206\u63a2\u7d22\u306e\u5206\u5272\u7d71\u6cbb\u904e\u7a0b\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 12-4 \u00a0 \u4e8c\u5206\u63a2\u7d22\u306e\u5206\u5272\u7d71\u6cbb\u904e\u7a0b

        \u5b9f\u88c5\u30b3\u30fc\u30c9\u3067\u306f\u3001\u554f\u984c \\(f(i, j)\\) \u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u518d\u5e30\u95a2\u6570 dfs() \u3092\u5ba3\u8a00\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_recur.py
        def dfs(nums: list[int], target: int, i: int, j: int) -> int:\n    \"\"\"\u4e8c\u5206\u63a2\u7d22\uff1a\u554f\u984c f(i, j)\"\"\"\n    # \u533a\u9593\u304c\u7a7a\u306e\u5834\u5408\u3001\u5bfe\u8c61\u8981\u7d20\u304c\u306a\u3044\u3053\u3068\u3092\u793a\u3059\u305f\u3081\u3001-1 \u3092\u8fd4\u3059\n    if i > j:\n        return -1\n    # \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 m \u3092\u8a08\u7b97\n    m = (i + j) // 2\n    if nums[m] < target:\n        # \u518d\u5e30\u90e8\u5206\u554f\u984c f(m+1, j)\n        return dfs(nums, target, m + 1, j)\n    elif nums[m] > target:\n        # \u518d\u5e30\u90e8\u5206\u554f\u984c f(i, m-1)\n        return dfs(nums, target, i, m - 1)\n    else:\n        # \u5bfe\u8c61\u8981\u7d20\u3092\u767a\u898b\u3057\u305f\u305f\u3081\u3001\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n        return m\n\ndef binary_search(nums: list[int], target: int) -> int:\n    \"\"\"\u4e8c\u5206\u63a2\u7d22\"\"\"\n    n = len(nums)\n    # \u554f\u984c f(0, n-1) \u3092\u89e3\u304f\n    return dfs(nums, target, 0, n - 1)\n
        binary_search_recur.cpp
        /* \u4e8c\u5206\u63a2\u7d22\uff1a\u554f\u984c f(i, j) */\nint dfs(vector<int> &nums, int target, int i, int j) {\n    // \u533a\u9593\u304c\u7a7a\u306e\u5834\u5408\u3001\u5bfe\u8c61\u8981\u7d20\u304c\u5b58\u5728\u3057\u306a\u3044\u3053\u3068\u3092\u793a\u3059\u305f\u3081\u3001-1 \u3092\u8fd4\u3059\n    if (i > j) {\n        return -1;\n    }\n    // \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 m \u3092\u8a08\u7b97\n    int m = i + (j - i) / 2;\n    if (nums[m] < target) {\n        // \u518d\u5e30\u7684\u306a\u90e8\u5206\u554f\u984c f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // \u518d\u5e30\u7684\u306a\u90e8\u5206\u554f\u984c f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // \u5bfe\u8c61\u8981\u7d20\u304c\u898b\u3064\u304b\u3063\u305f\u305f\u3081\u3001\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n        return m;\n    }\n}\n\n/* \u4e8c\u5206\u63a2\u7d22 */\nint binarySearch(vector<int> &nums, int target) {\n    int n = nums.size();\n    // \u554f\u984c f(0, n-1) \u3092\u89e3\u304f\n    return dfs(nums, target, 0, n - 1);\n}\n
        binary_search_recur.java
        /* \u4e8c\u5206\u63a2\u7d22\uff1a\u554f\u984c f(i, j) */\nint dfs(int[] nums, int target, int i, int j) {\n    // \u533a\u9593\u304c\u7a7a\u306e\u5834\u5408\u3001\u5bfe\u8c61\u8981\u7d20\u304c\u5b58\u5728\u3057\u306a\u3044\u3053\u3068\u3092\u793a\u3059\u305f\u3081\u3001-1 \u3092\u8fd4\u3059\n    if (i > j) {\n        return -1;\n    }\n    // \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 m \u3092\u8a08\u7b97\n    int m = i + (j - i) / 2;\n    if (nums[m] < target) {\n        // \u518d\u5e30\u7684\u306a\u90e8\u5206\u554f\u984c f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // \u518d\u5e30\u7684\u306a\u90e8\u5206\u554f\u984c f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // \u5bfe\u8c61\u8981\u7d20\u304c\u898b\u3064\u304b\u3063\u305f\u305f\u3081\u3001\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n        return m;\n    }\n}\n\n/* \u4e8c\u5206\u63a2\u7d22 */\nint binarySearch(int[] nums, int target) {\n    int n = nums.length;\n    // \u554f\u984c f(0, n-1) \u3092\u89e3\u304f\n    return dfs(nums, target, 0, n - 1);\n}\n
        binary_search_recur.cs
        [class]{binary_search_recur}-[func]{DFS}\n\n[class]{binary_search_recur}-[func]{BinarySearch}\n
        binary_search_recur.go
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{binarySearch}\n
        binary_search_recur.swift
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{binarySearch}\n
        binary_search_recur.js
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{binarySearch}\n
        binary_search_recur.ts
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{binarySearch}\n
        binary_search_recur.dart
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{binarySearch}\n
        binary_search_recur.rs
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{binary_search}\n
        binary_search_recur.c
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{binarySearch}\n
        binary_search_recur.kt
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{binarySearch}\n
        binary_search_recur.rb
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{binary_search}\n
        "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/","title":"12.3 \u00a0 \u4e8c\u5206\u6728\u69cb\u7bc9\u554f\u984c","text":"

        Question

        \u4e8c\u5206\u6728\u306e\u524d\u9806\u8d70\u67fb preorder \u30b7\u30fc\u30b1\u30f3\u30b9\u3068\u4e2d\u9806\u8d70\u67fb inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u4e8c\u5206\u6728\u3092\u69cb\u7bc9\u3057\u3066\u305d\u306e\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4e8c\u5206\u6728\u306b\u91cd\u8907\u3059\u308b\u30ce\u30fc\u30c9\u5024\u304c\u306a\u3044\u3068\u4eee\u5b9a\u3057\u307e\u3059\uff08\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\uff09\u3002

        \u56f3 12-5 \u00a0 \u4e8c\u5206\u6728\u69cb\u7bc9\u306e\u30b5\u30f3\u30d7\u30eb\u30c7\u30fc\u30bf

        "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#1","title":"1. \u00a0 \u5206\u5272\u7d71\u6cbb\u554f\u984c\u304b\u3069\u3046\u304b\u306e\u5224\u5b9a","text":"

        preorder \u3068 inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u304b\u3089\u4e8c\u5206\u6728\u3092\u69cb\u7bc9\u3059\u308b\u5143\u306e\u554f\u984c\u306f\u3001\u5178\u578b\u7684\u306a\u5206\u5272\u7d71\u6cbb\u554f\u984c\u3067\u3059\u3002

        • \u554f\u984c\u3092\u5206\u89e3\u3067\u304d\u308b\uff1a\u5206\u5272\u7d71\u6cbb\u306e\u89b3\u70b9\u304b\u3089\u3001\u5143\u306e\u554f\u984c\u30922\u3064\u306e\u90e8\u5206\u554f\u984c\uff08\u5de6\u306e\u90e8\u5206\u6728\u306e\u69cb\u7bc9\u3068\u53f3\u306e\u90e8\u5206\u6728\u306e\u69cb\u7bc9\uff09\u3068\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\u3068\u3044\u30461\u3064\u306e\u64cd\u4f5c\u306b\u5206\u5272\u3067\u304d\u307e\u3059\u3002\u5404\u90e8\u5206\u6728\uff08\u90e8\u5206\u554f\u984c\uff09\u306b\u3064\u3044\u3066\u3001\u540c\u3058\u30a2\u30d7\u30ed\u30fc\u30c1\u3092\u7d99\u7d9a\u7684\u306b\u9069\u7528\u3057\u3001\u3088\u308a\u5c0f\u3055\u306a\u90e8\u5206\u6728\uff08\u90e8\u5206\u554f\u984c\uff09\u306b\u5206\u5272\u3057\u3001\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\uff08\u7a7a\u306e\u90e8\u5206\u6728\uff09\u306b\u5230\u9054\u3059\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002
        • \u90e8\u5206\u554f\u984c\u306f\u72ec\u7acb\u3057\u3066\u3044\u308b\uff1a\u5de6\u3068\u53f3\u306e\u90e8\u5206\u6728\u306f\u91cd\u8907\u3057\u307e\u305b\u3093\u3002\u5de6\u306e\u90e8\u5206\u6728\u3092\u69cb\u7bc9\u3059\u308b\u969b\u3001\u5de6\u306e\u90e8\u5206\u6728\u306b\u5bfe\u5fdc\u3059\u308b\u4e2d\u9806\u8d70\u67fb\u3068\u524d\u9806\u8d70\u67fb\u306e\u30bb\u30b0\u30e1\u30f3\u30c8\u306e\u307f\u304c\u5fc5\u8981\u3067\u3059\u3002\u53f3\u306e\u90e8\u5206\u6728\u306b\u3082\u540c\u3058\u30a2\u30d7\u30ed\u30fc\u30c1\u304c\u9069\u7528\u3055\u308c\u307e\u3059\u3002
        • \u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u308b\uff1a\u5de6\u3068\u53f3\u306e\u90e8\u5206\u6728\uff08\u90e8\u5206\u554f\u984c\u306e\u89e3\uff09\u3092\u69cb\u7bc9\u3057\u305f\u3089\u3001\u305d\u308c\u3089\u3092\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u306b\u63a5\u7d9a\u3057\u3066\u5143\u306e\u554f\u984c\u306e\u89e3\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002
        "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#2","title":"2. \u00a0 \u90e8\u5206\u6728\u306e\u5206\u5272\u65b9\u6cd5","text":"

        \u4e0a\u8a18\u306e\u5206\u6790\u306b\u57fa\u3065\u3044\u3066\u3001\u3053\u306e\u554f\u984c\u306f\u5206\u5272\u7d71\u6cbb\u3092\u4f7f\u7528\u3057\u3066\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u524d\u9806\u8d70\u67fb preorder \u30b7\u30fc\u30b1\u30f3\u30b9\u3068\u4e2d\u9806\u8d70\u67fb inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u4f7f\u7528\u3057\u3066\u5de6\u3068\u53f3\u306e\u90e8\u5206\u6728\u3092\u3069\u306e\u3088\u3046\u306b\u5206\u5272\u3059\u308c\u3070\u3088\u3044\u3067\u3057\u3087\u3046\u304b\uff1f

        \u5b9a\u7fa9\u306b\u3088\u308a\u3001preorder \u3068 inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u306e\u4e21\u65b9\u30923\u3064\u306e\u90e8\u5206\u306b\u5206\u5272\u3067\u304d\u307e\u3059\uff1a

        • \u524d\u9806\u8d70\u67fb\uff1a[ \u30eb\u30fc\u30c8 | \u5de6\u306e\u90e8\u5206\u6728 | \u53f3\u306e\u90e8\u5206\u6728 ]\u3002\u4f8b\u3048\u3070\u3001\u56f3\u3067\u306f\u3001\u6728\u306f [ 3 | 9 | 2 1 7 ] \u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002
        • \u4e2d\u9806\u8d70\u67fb\uff1a[ \u5de6\u306e\u90e8\u5206\u6728 | \u30eb\u30fc\u30c8 | \u53f3\u306e\u90e8\u5206\u6728 ]\u3002\u4f8b\u3048\u3070\u3001\u56f3\u3067\u306f\u3001\u6728\u306f [ 9 | 3 | 1 2 7 ] \u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002

        \u524d\u306e\u56f3\u306e\u30c7\u30fc\u30bf\u3092\u4f7f\u7528\u3057\u3066\u3001\u6b21\u306e\u56f3\u306b\u793a\u3059\u30b9\u30c6\u30c3\u30d7\u306b\u5f93\u3063\u3066\u5206\u5272\u7d50\u679c\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\uff1a

        1. \u524d\u9806\u8d70\u67fb\u306e\u6700\u521d\u306e\u8981\u7d203\u304c\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u306e\u5024\u3067\u3059\u3002
        2. inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u5185\u3067\u30eb\u30fc\u30c8\u30ce\u30fc\u30c93\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u898b\u3064\u3051\u3001\u3053\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u4f7f\u7528\u3057\u3066 inorder \u3092 [ 9 | 3 \uff5c 1 2 7 ] \u306b\u5206\u5272\u3057\u307e\u3059\u3002
        3. inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u306e\u5206\u5272\u306b\u5f93\u3063\u3066\u3001\u5de6\u3068\u53f3\u306e\u90e8\u5206\u6728\u304c\u305d\u308c\u305e\u308c1\u500b\u30683\u500b\u306e\u30ce\u30fc\u30c9\u3092\u542b\u3080\u3053\u3068\u304c\u7c21\u5358\u306b\u6c7a\u5b9a\u3067\u304d\u308b\u305f\u3081\u3001preorder \u30b7\u30fc\u30b1\u30f3\u30b9\u3092 [ 3 | 9 | 2 1 7 ] \u306b\u5bfe\u5fdc\u3057\u3066\u5206\u5272\u3067\u304d\u307e\u3059\u3002

        \u56f3 12-6 \u00a0 \u524d\u9806\u8d70\u67fb\u3068\u4e2d\u9806\u8d70\u67fb\u3067\u306e\u90e8\u5206\u6728\u306e\u5206\u5272

        "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#3","title":"3. \u00a0 \u5909\u6570\u306b\u57fa\u3065\u304f\u90e8\u5206\u6728\u7bc4\u56f2\u306e\u8a18\u8ff0","text":"

        \u4e0a\u8a18\u306e\u5206\u5272\u65b9\u6cd5\u306b\u57fa\u3065\u3044\u3066\u3001preorder \u3068 inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u306b\u304a\u3051\u308b\u30eb\u30fc\u30c8\u3001\u5de6\u306e\u90e8\u5206\u6728\u3001\u53f3\u306e\u90e8\u5206\u6728\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2\u3092\u53d6\u5f97\u3057\u307e\u3057\u305f\u3002\u3053\u308c\u3089\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2\u3092\u8a18\u8ff0\u3059\u308b\u305f\u3081\u306b\u3001\u3044\u304f\u3064\u304b\u306e\u30dd\u30a4\u30f3\u30bf\u5909\u6570\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        • \u73fe\u5728\u306e\u6728\u306e\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u306e preorder \u30b7\u30fc\u30b1\u30f3\u30b9\u3067\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092 \\(i\\) \u3068\u3057\u307e\u3059\u3002
        • \u73fe\u5728\u306e\u6728\u306e\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u306e inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u3067\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092 \\(m\\) \u3068\u3057\u307e\u3059\u3002
        • \u73fe\u5728\u306e\u6728\u306e inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u3067\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2\u3092 \\([l, r]\\) \u3068\u3057\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u8868\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u3053\u308c\u3089\u306e\u5909\u6570\u306f preorder \u30b7\u30fc\u30b1\u30f3\u30b9\u3067\u306e\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3068 inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u3067\u306e\u90e8\u5206\u6728\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2\u3092\u8868\u3057\u307e\u3059\u3002

        \u8868 12-1 \u00a0 \u524d\u9806\u8d70\u67fb\u3068\u4e2d\u9806\u8d70\u67fb\u3067\u306e\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3068\u90e8\u5206\u6728\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9

        preorder \u3067\u306e\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 inorder \u3067\u306e\u90e8\u5206\u6728\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2 \u73fe\u5728\u306e\u6728 \\(i\\) \\([l, r]\\) \u5de6\u306e\u90e8\u5206\u6728 \\(i + 1\\) \\([l, m-1]\\) \u53f3\u306e\u90e8\u5206\u6728 \\(i + 1 + (m - l)\\) \\([m+1, r]\\)

        \u53f3\u306e\u90e8\u5206\u6728\u306e\u30eb\u30fc\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e \\((m-l)\\) \u306f\u300c\u5de6\u306e\u90e8\u5206\u6728\u306e\u30ce\u30fc\u30c9\u6570\u300d\u3092\u8868\u3059\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3088\u308a\u660e\u78ba\u306a\u7406\u89e3\u306e\u305f\u3081\u306b\u3001\u4ee5\u4e0b\u306e\u56f3\u3092\u53c2\u7167\u3059\u308b\u3053\u3068\u304c\u5f79\u7acb\u3064\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002

        \u56f3 12-7 \u00a0 \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3068\u5de6\u53f3\u306e\u90e8\u5206\u6728\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9

        "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#4","title":"4. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        \\(m\\) \u306e\u554f\u3044\u5408\u308f\u305b\u306e\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u308b\u305f\u3081\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb hmap \u3092\u4f7f\u7528\u3057\u3066 inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u306e\u8981\u7d20\u304b\u3089\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3078\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u3092\u683c\u7d0d\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby build_tree.py
        def dfs(\n    preorder: list[int],\n    inorder_map: dict[int, int],\n    i: int,\n    l: int,\n    r: int,\n) -> TreeNode | None:\n    \"\"\"\u4e8c\u5206\u6728\u306e\u69cb\u7bc9\uff1a\u5206\u5272\u7d71\u6cbb\"\"\"\n    # \u90e8\u5206\u6728\u306e\u533a\u9593\u304c\u7a7a\u306e\u3068\u304d\u7d42\u4e86\n    if r - l < 0:\n        return None\n    # \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\n    root = TreeNode(preorder[i])\n    # m \u3092\u30af\u30a8\u30ea\u3057\u3066\u5de6\u90e8\u5206\u6728\u3068\u53f3\u90e8\u5206\u6728\u3092\u5206\u5272\n    m = inorder_map[preorder[i]]\n    # \u90e8\u5206\u554f\u984c\uff1a\u5de6\u90e8\u5206\u6728\u3092\u69cb\u7bc9\n    root.left = dfs(preorder, inorder_map, i + 1, l, m - 1)\n    # \u90e8\u5206\u554f\u984c\uff1a\u53f3\u90e8\u5206\u6728\u3092\u69cb\u7bc9\n    root.right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r)\n    # \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u8fd4\u3059\n    return root\n\ndef build_tree(preorder: list[int], inorder: list[int]) -> TreeNode | None:\n    \"\"\"\u4e8c\u5206\u6728\u3092\u69cb\u7bc9\"\"\"\n    # \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3001\u4e2d\u9806\u8d70\u67fb\u306e\u8981\u7d20\u304b\u3089\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3078\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u3092\u4fdd\u5b58\n    inorder_map = {val: i for i, val in enumerate(inorder)}\n    root = dfs(preorder, inorder_map, 0, 0, len(inorder) - 1)\n    return root\n
        build_tree.cpp
        /* \u4e8c\u5206\u6728\u306e\u69cb\u7bc9\uff1a\u5206\u5272\u7d71\u6cbb */\nTreeNode *dfs(vector<int> &preorder, unordered_map<int, int> &inorderMap, int i, int l, int r) {\n    // \u90e8\u5206\u6728\u306e\u533a\u9593\u304c\u7a7a\u306e\u5834\u5408\u306b\u7d42\u4e86\n    if (r - l < 0)\n        return NULL;\n    // \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\n    TreeNode *root = new TreeNode(preorder[i]);\n    // m \u3092\u554f\u3044\u5408\u308f\u305b\u3066\u5de6\u53f3\u306e\u90e8\u5206\u6728\u3092\u5206\u5272\n    int m = inorderMap[preorder[i]];\n    // \u90e8\u5206\u554f\u984c\uff1a\u5de6\u306e\u90e8\u5206\u6728\u3092\u69cb\u7bc9\n    root->left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // \u90e8\u5206\u554f\u984c\uff1a\u53f3\u306e\u90e8\u5206\u6728\u3092\u69cb\u7bc9\n    root->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u8fd4\u3059\n    return root;\n}\n\n/* \u4e8c\u5206\u6728\u306e\u69cb\u7bc9 */\nTreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {\n    // \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3057\u3001\u4e2d\u9593\u9806\u5e8f\u306e\u8981\u7d20\u304b\u3089\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3078\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u3092\u683c\u7d0d\n    unordered_map<int, int> inorderMap;\n    for (int i = 0; i < inorder.size(); i++) {\n        inorderMap[inorder[i]] = i;\n    }\n    TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorder.size() - 1);\n    return root;\n}\n
        build_tree.java
        /* \u4e8c\u5206\u6728\u306e\u69cb\u7bc9\uff1a\u5206\u5272\u7d71\u6cbb */\nTreeNode dfs(int[] preorder, Map<Integer, Integer> inorderMap, int i, int l, int r) {\n    // \u90e8\u5206\u6728\u306e\u533a\u9593\u304c\u7a7a\u306e\u5834\u5408\u306b\u7d42\u4e86\n    if (r - l < 0)\n        return null;\n    // \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\n    TreeNode root = new TreeNode(preorder[i]);\n    // m \u3092\u554f\u3044\u5408\u308f\u305b\u3066\u5de6\u53f3\u306e\u90e8\u5206\u6728\u3092\u5206\u5272\n    int m = inorderMap.get(preorder[i]);\n    // \u90e8\u5206\u554f\u984c\uff1a\u5de6\u306e\u90e8\u5206\u6728\u3092\u69cb\u7bc9\n    root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // \u90e8\u5206\u554f\u984c\uff1a\u53f3\u306e\u90e8\u5206\u6728\u3092\u69cb\u7bc9\n    root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u8fd4\u3059\n    return root;\n}\n\n/* \u4e8c\u5206\u6728\u306e\u69cb\u7bc9 */\nTreeNode buildTree(int[] preorder, int[] inorder) {\n    // \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3057\u3001\u4e2d\u9593\u9806\u5e8f\u306e\u8981\u7d20\u304b\u3089\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3078\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u3092\u683c\u7d0d\n    Map<Integer, Integer> inorderMap = new HashMap<>();\n    for (int i = 0; i < inorder.length; i++) {\n        inorderMap.put(inorder[i], i);\n    }\n    TreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n    return root;\n}\n
        build_tree.cs
        [class]{build_tree}-[func]{DFS}\n\n[class]{build_tree}-[func]{BuildTree}\n
        build_tree.go
        [class]{}-[func]{dfsBuildTree}\n\n[class]{}-[func]{buildTree}\n
        build_tree.swift
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{buildTree}\n
        build_tree.js
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{buildTree}\n
        build_tree.ts
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{buildTree}\n
        build_tree.dart
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{buildTree}\n
        build_tree.rs
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{build_tree}\n
        build_tree.c
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{buildTree}\n
        build_tree.kt
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{buildTree}\n
        build_tree.rb
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{build_tree}\n

        \u4ee5\u4e0b\u306e\u56f3\u306f\u3001\u4e8c\u5206\u6728\u3092\u69cb\u7bc9\u3059\u308b\u518d\u5e30\u904e\u7a0b\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u5404\u30ce\u30fc\u30c9\u306f\u518d\u5e30\u306e\u300c\u4e0b\u964d\u300d\u6bb5\u968e\u3067\u4f5c\u6210\u3055\u308c\u3001\u5404\u30a8\u30c3\u30b8\uff08\u53c2\u7167\uff09\u306f\u300c\u4e0a\u6607\u300d\u6bb5\u968e\u3067\u5f62\u6210\u3055\u308c\u307e\u3059\u3002

        <1><2><3><4><5><6><7><8><9>

        \u56f3 12-8 \u00a0 \u4e8c\u5206\u6728\u69cb\u7bc9\u306e\u518d\u5e30\u904e\u7a0b

        \u5404\u518d\u5e30\u95a2\u6570\u306e preorder \u3068 inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u306e\u5206\u5272\u306f\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        \u56f3 12-9 \u00a0 \u5404\u518d\u5e30\u95a2\u6570\u3067\u306e\u5206\u5272

        \u4e8c\u5206\u6728\u304c \\(n\\) \u500b\u306e\u30ce\u30fc\u30c9\u3092\u6301\u3064\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u5404\u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\uff08\u518d\u5e30\u95a2\u6570 dfs() \u306e\u547c\u3073\u51fa\u3057\uff09\u306b\u306f \\(O(1)\\) \u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u5168\u4f53\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u3067\u3059\u3002

        \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306f inorder \u8981\u7d20\u304b\u3089\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3078\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u3001\\(O(n)\\) \u30b9\u30da\u30fc\u30b9\u304c\u5fc5\u8981\u3067\u3059\u3002\u6700\u60aa\u306e\u5834\u5408\u3001\u4e8c\u5206\u6728\u304c\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u9000\u5316\u3059\u308b\u3068\u3001\u518d\u5e30\u306e\u6df1\u3055\u306f \\(n\\) \u306b\u9054\u3057\u3001\\(O(n)\\) \u306e\u30b9\u30bf\u30c3\u30af\u30b9\u30da\u30fc\u30b9\u3092\u6d88\u8cbb\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u5168\u4f53\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u3067\u3059\u3002

        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/","title":"12.1 \u00a0 \u5206\u5272\u7d71\u6cbb\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0","text":"

        \u5206\u5272\u7d71\u6cbb\u306f\u91cd\u8981\u3067\u4eba\u6c17\u306e\u3042\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u6226\u7565\u3067\u3059\u3002\u540d\u524d\u304c\u793a\u3059\u3088\u3046\u306b\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u901a\u5e38\u518d\u5e30\u7684\u306b\u5b9f\u88c5\u3055\u308c\u3001\u300c\u5206\u5272\u300d\u3068\u300c\u7d71\u6cbb\u300d\u306e2\u3064\u306e\u30b9\u30c6\u30c3\u30d7\u304b\u3089\u69cb\u6210\u3055\u308c\u307e\u3059\u3002

        1. \u5206\u5272\uff08\u5206\u5272\u6bb5\u968e\uff09\uff1a\u5143\u306e\u554f\u984c\u3092\u518d\u5e30\u7684\u306b2\u3064\u4ee5\u4e0a\u306e\u5c0f\u3055\u306a\u90e8\u5206\u554f\u984c\u306b\u5206\u89e3\u3057\u3001\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306b\u5230\u9054\u3059\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002
        2. \u7d71\u6cbb\uff08\u30de\u30fc\u30b8\u6bb5\u968e\uff09\uff1a\u89e3\u6c7a\u65b9\u6cd5\u304c\u65e2\u77e5\u306e\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u304b\u3089\u958b\u59cb\u3057\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u30dc\u30c8\u30e0\u30a2\u30c3\u30d7\u65b9\u5f0f\u3067\u30de\u30fc\u30b8\u3057\u3066\u5143\u306e\u554f\u984c\u306e\u89e3\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u300c\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u300d\u306f\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306e\u5178\u578b\u7684\u306a\u5fdc\u7528\u306e\u4e00\u3064\u3067\u3059\u3002

        1. \u5206\u5272\uff1a\u5143\u306e\u914d\u5217\uff08\u5143\u306e\u554f\u984c\uff09\u3092\u518d\u5e30\u7684\u306b2\u3064\u306e\u526f\u914d\u5217\uff08\u90e8\u5206\u554f\u984c\uff09\u306b\u5206\u5272\u3057\u3001\u526f\u914d\u5217\u304c1\u3064\u306e\u8981\u7d20\u306e\u307f\u306b\u306a\u308b\u307e\u3067\uff08\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\uff09\u7d9a\u3051\u307e\u3059\u3002
        2. \u7d71\u6cbb\uff1a\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u526f\u914d\u5217\uff08\u90e8\u5206\u554f\u984c\u306e\u89e3\uff09\u3092\u30dc\u30c8\u30e0\u30a2\u30c3\u30d7\u3067\u30de\u30fc\u30b8\u3057\u3066\u3001\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u5143\u306e\u914d\u5217\uff08\u5143\u306e\u554f\u984c\u306e\u89e3\uff09\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002

        \u56f3 12-1 \u00a0 \u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u306e\u5206\u5272\u7d71\u6cbb\u6226\u7565

        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1211","title":"12.1.1 \u00a0 \u5206\u5272\u7d71\u6cbb\u554f\u984c\u3092\u7279\u5b9a\u3059\u308b\u65b9\u6cd5","text":"

        \u554f\u984c\u304c\u5206\u5272\u7d71\u6cbb\u89e3\u6c7a\u306b\u9069\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u306f\u3001\u901a\u5e38\u4ee5\u4e0b\u306e\u57fa\u6e96\u306b\u57fa\u3065\u3044\u3066\u6c7a\u5b9a\u3067\u304d\u307e\u3059\u3002

        1. \u554f\u984c\u3092\u3088\u308a\u5c0f\u3055\u306a\u3082\u306e\u306b\u5206\u89e3\u3067\u304d\u308b\uff1a\u5143\u306e\u554f\u984c\u3092\u3088\u308a\u5c0f\u3055\u304f\u985e\u4f3c\u3057\u305f\u90e8\u5206\u554f\u984c\u306b\u5206\u5272\u3067\u304d\u3001\u305d\u306e\u3088\u3046\u306a\u904e\u7a0b\u3092\u540c\u3058\u65b9\u6cd5\u3067\u518d\u5e30\u7684\u306b\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002
        2. \u90e8\u5206\u554f\u984c\u306f\u72ec\u7acb\u3057\u3066\u3044\u308b\uff1a\u90e8\u5206\u554f\u984c\u9593\u306b\u91cd\u8907\u304c\u306a\u304f\u3001\u72ec\u7acb\u3057\u3066\u304a\u308a\u3001\u500b\u5225\u306b\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002
        3. \u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u30de\u30fc\u30b8\u3067\u304d\u308b\uff1a\u5143\u306e\u554f\u984c\u306e\u89e3\u306f\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u5c0e\u51fa\u3055\u308c\u307e\u3059\u3002

        \u660e\u3089\u304b\u306b\u3001\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u306f\u3053\u308c\u30893\u3064\u306e\u57fa\u6e96\u3092\u6e80\u305f\u3057\u3066\u3044\u307e\u3059\u3002

        1. \u554f\u984c\u3092\u3088\u308a\u5c0f\u3055\u306a\u3082\u306e\u306b\u5206\u89e3\u3067\u304d\u308b\uff1a\u914d\u5217\uff08\u5143\u306e\u554f\u984c\uff09\u3092\u518d\u5e30\u7684\u306b2\u3064\u306e\u526f\u914d\u5217\uff08\u90e8\u5206\u554f\u984c\uff09\u306b\u5206\u5272\u3057\u307e\u3059\u3002
        2. \u90e8\u5206\u554f\u984c\u306f\u72ec\u7acb\u3057\u3066\u3044\u308b\uff1a\u5404\u526f\u914d\u5217\u306f\u72ec\u7acb\u3057\u3066\u30bd\u30fc\u30c8\u3067\u304d\u307e\u3059\uff08\u90e8\u5206\u554f\u984c\u306f\u72ec\u7acb\u3057\u3066\u89e3\u6c7a\u3067\u304d\u307e\u3059\uff09\u3002
        3. \u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u30de\u30fc\u30b8\u3067\u304d\u308b\uff1a2\u3064\u306e\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u526f\u914d\u5217\uff08\u90e8\u5206\u554f\u984c\u306e\u89e3\uff09\u30921\u3064\u306e\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u914d\u5217\uff08\u5143\u306e\u554f\u984c\u306e\u89e3\uff09\u306b\u30de\u30fc\u30b8\u3067\u304d\u307e\u3059\u3002
        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1212","title":"12.1.2 \u00a0 \u5206\u5272\u7d71\u6cbb\u306b\u3088\u308b\u52b9\u7387\u306e\u5411\u4e0a","text":"

        \u5206\u5272\u7d71\u6cbb\u6226\u7565\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u3092\u52b9\u679c\u7684\u306b\u89e3\u6c7a\u3059\u308b\u3060\u3051\u3067\u306a\u304f\u3001\u3057\u3070\u3057\u3070\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u306f\u3001\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u3001\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u3001\u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\u306f\u3001\u5206\u5272\u7d71\u6cbb\u6226\u7565\u3092\u9069\u7528\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u9078\u629e\u30bd\u30fc\u30c8\u3001\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u3001\u633f\u5165\u30bd\u30fc\u30c8\u3088\u308a\u3082\u9ad8\u901f\u3067\u3059\u3002

        \u79c1\u305f\u3061\u306e\u5fc3\u306b\u306f\u7591\u554f\u304c\u3042\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\uff1a\u306a\u305c\u5206\u5272\u7d71\u6cbb\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u305d\u306e\u6839\u672c\u7684\u306a\u8ad6\u7406\u306f\u4f55\u3067\u3059\u304b\uff1f \u3064\u307e\u308a\u3001\u554f\u984c\u3092\u90e8\u5206\u554f\u984c\u306b\u5206\u89e3\u3057\u3001\u305d\u308c\u3089\u3092\u89e3\u6c7a\u3057\u3001\u305d\u308c\u3089\u306e\u89e3\u3092\u7d44\u307f\u5408\u308f\u305b\u3066\u5143\u306e\u554f\u984c\u306b\u5bfe\u51e6\u3059\u308b\u3053\u3068\u304c\u3001\u5143\u306e\u554f\u984c\u3092\u76f4\u63a5\u89e3\u6c7a\u3059\u308b\u3088\u308a\u3082\u52b9\u7387\u7684\u3067\u3042\u308b\u7406\u7531\u306f\u4f55\u3067\u3059\u304b\uff1f\u3053\u306e\u8cea\u554f\u306f2\u3064\u306e\u5074\u9762\u304b\u3089\u5206\u6790\u3067\u304d\u307e\u3059\uff1a\u64cd\u4f5c\u6570\u3068\u4e26\u5217\u8a08\u7b97\u3002

        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1","title":"1. \u00a0 \u64cd\u4f5c\u6570\u306e\u6700\u9069\u5316","text":"

        \u300c\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u300d\u3092\u4f8b\u306b\u3068\u308b\u3068\u3001\u9577\u3055 \\(n\\) \u306e\u914d\u5217\u3092\u51e6\u7406\u3059\u308b\u306e\u306b \\(O(n^2)\\) \u6642\u9593\u304c\u5fc5\u8981\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u914d\u5217\u3092\u4e2d\u70b9\u304b\u30892\u3064\u306e\u526f\u914d\u5217\u306b\u5206\u5272\u3059\u308b\u3068\u3057\u307e\u3059\u3002\u305d\u306e\u3088\u3046\u306a\u5206\u5272\u306b\u306f \\(O(n)\\) \u6642\u9593\u304c\u5fc5\u8981\u3067\u3059\u3002\u5404\u526f\u914d\u5217\u306e\u30bd\u30fc\u30c8\u306b\u306f \\(O((n / 2)^2)\\) \u6642\u9593\u304c\u5fc5\u8981\u3067\u3059\u3002\u305d\u3057\u30662\u3064\u306e\u526f\u914d\u5217\u306e\u30de\u30fc\u30b8\u306b\u306f \\(O(n)\\) \u6642\u9593\u304c\u5fc5\u8981\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u5168\u4f53\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\uff1a

        \\[ O(n + (\\frac{n}{2})^2 \\times 2 + n) = O(\\frac{n^2}{2} + 2n) \\]

        \u56f3 12-2 \u00a0 \u914d\u5217\u5206\u5272\u524d\u5f8c\u306e\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8

        \u4ee5\u4e0b\u306e\u4e0d\u7b49\u5f0f\u3092\u8a08\u7b97\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u5de6\u5074\u306f\u5206\u5272\u524d\u306e\u7dcf\u64cd\u4f5c\u6570\u3092\u8868\u3057\u3001\u53f3\u5074\u306f\u5206\u5272\u5f8c\u306e\u7dcf\u64cd\u4f5c\u6570\u3092\u305d\u308c\u305e\u308c\u8868\u3057\u307e\u3059\uff1a

        \\[ \\begin{aligned} n^2 & > \\frac{n^2}{2} + 2n \\newline n^2 - \\frac{n^2}{2} - 2n & > 0 \\newline n(n - 4) & > 0 \\end{aligned} \\]

        \u3053\u308c\u306f \\(n > 4\\) \u306e\u5834\u5408\u3001\u5206\u5272\u5f8c\u306e\u64cd\u4f5c\u6570\u304c\u5c11\u306a\u304f\u3001\u3088\u308a\u826f\u3044\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306b\u3064\u306a\u304c\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u5206\u5272\u5f8c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\u4f9d\u7136\u3068\u3057\u3066\u4e8c\u6b21 \\(O(n^2)\\) \u3067\u3059\u304c\u3001\u8a08\u7b97\u91cf\u306e\u5b9a\u6570\u4fc2\u6570\u304c\u6e1b\u5c11\u3057\u3066\u3044\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u3055\u3089\u306b\u9032\u3080\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u526f\u914d\u5217\u3092\u305d\u306e\u4e2d\u70b9\u304b\u3089\u3055\u3089\u306b2\u3064\u306e\u526f\u914d\u5217\u306b\u5206\u5272\u3057\u7d9a\u3051\u3066\u3001\u526f\u914d\u5217\u304c1\u3064\u306e\u8981\u7d20\u306e\u307f\u306b\u306a\u308b\u307e\u3067\u7d9a\u3051\u305f\u3089\u3069\u3046\u3067\u3057\u3087\u3046\u304b\uff1f \u3053\u306e\u30a2\u30a4\u30c7\u30a2\u306f\u5b9f\u969b\u306b\u306f\u300c\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u300d\u3067\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n \\log n)\\) \u3067\u3059\u3002

        \u5c11\u3057\u9055\u3046\u3053\u3068\u3092\u8a66\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u30022\u3064\u3067\u306f\u306a\u304f\u3001\u3088\u308a\u591a\u304f\u306e\u5206\u5272\u306b\u5206\u5272\u3057\u305f\u3089\u3069\u3046\u3067\u3057\u3087\u3046\u304b\uff1f \u4f8b\u3048\u3070\u3001\u5143\u306e\u914d\u5217\u3092 \\(k\\) \u500b\u306e\u526f\u914d\u5217\u306b\u5747\u7b49\u306b\u5206\u5272\u3057\u307e\u3059\u304b\uff1f\u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\u300c\u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u300d\u3068\u975e\u5e38\u306b\u4f3c\u3066\u304a\u308a\u3001\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u306e\u30bd\u30fc\u30c8\u306b\u975e\u5e38\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u7406\u8ad6\u7684\u306b\u306f\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n + k)\\) \u306b\u9054\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#2","title":"2. \u00a0 \u4e26\u5217\u8a08\u7b97\u306b\u3088\u308b\u6700\u9069\u5316","text":"

        \u5206\u5272\u7d71\u6cbb\u306b\u3088\u3063\u3066\u751f\u6210\u3055\u308c\u308b\u90e8\u5206\u554f\u984c\u306f\u4e92\u3044\u306b\u72ec\u7acb\u3057\u3066\u3044\u308b\u3053\u3068\u304c\u5206\u304b\u3063\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u305d\u308c\u3089\u3092\u4e26\u5217\u3067\u89e3\u6c7a\u3067\u304d\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002 \u305d\u306e\u7d50\u679c\u3001\u5206\u5272\u7d71\u6cbb\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3092\u6e1b\u3089\u3059\u3060\u3051\u3067\u306a\u304f\u3001\u73fe\u4ee3\u306e\u30aa\u30da\u30ec\u30fc\u30c6\u30a3\u30f3\u30b0\u30b7\u30b9\u30c6\u30e0\u306b\u3088\u308b\u4e26\u5217\u6700\u9069\u5316\u3082\u4fc3\u9032\u3057\u307e\u3059\u3002

        \u4e26\u5217\u6700\u9069\u5316\u306f\u3001\u8907\u6570\u306e\u30b3\u30a2\u3084\u30d7\u30ed\u30bb\u30c3\u30b5\u3092\u6301\u3064\u74b0\u5883\u3067\u7279\u306b\u52b9\u679c\u7684\u3067\u3059\u3002\u30b7\u30b9\u30c6\u30e0\u304c\u8907\u6570\u306e\u90e8\u5206\u554f\u984c\u3092\u540c\u6642\u306b\u51e6\u7406\u3067\u304d\u308b\u305f\u3081\u3001\u8a08\u7b97\u30ea\u30bd\u30fc\u30b9\u3092\u5b8c\u5168\u306b\u6d3b\u7528\u3057\u3001\u5168\u4f53\u7684\u306a\u5b9f\u884c\u6642\u9593\u304c\u5927\u5e45\u306b\u77ed\u7e2e\u3055\u308c\u307e\u3059\u3002

        \u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u300c\u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u300d\u3067\u306f\u3001\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u69d8\u3005\u306a\u30d0\u30b1\u30c3\u30c8\u306b\u5747\u7b49\u306b\u5206\u89e3\u3057\u307e\u3059\u3002\u5404\u30d0\u30b1\u30c3\u30c8\u306e\u30bd\u30fc\u30c8\u4f5c\u696d\u306f\u3001\u5229\u7528\u53ef\u80fd\u306a\u8a08\u7b97\u30e6\u30cb\u30c3\u30c8\u306b\u5272\u308a\u5f53\u3066\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3059\u3079\u3066\u306e\u4f5c\u696d\u304c\u5b8c\u4e86\u3059\u308b\u3068\u3001\u3059\u3079\u3066\u306e\u30bd\u30fc\u30c8\u3055\u308c\u305f\u30d0\u30b1\u30c3\u30c8\u304c\u30de\u30fc\u30b8\u3055\u308c\u3066\u6700\u7d42\u7d50\u679c\u304c\u751f\u6210\u3055\u308c\u307e\u3059\u3002

        \u56f3 12-3 \u00a0 \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306e\u4e26\u5217\u8a08\u7b97

        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1213","title":"12.1.3 \u00a0 \u5206\u5272\u7d71\u6cbb\u306e\u4e00\u822c\u7684\u306a\u5fdc\u7528","text":"

        \u5206\u5272\u7d71\u6cbb\u306f\u591a\u304f\u306e\u53e4\u5178\u7684\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002

        • \u6700\u8fd1\u70b9\u5bfe\u306e\u767a\u898b\uff1a\u3053\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u70b9\u306e\u96c6\u5408\u30922\u3064\u306e\u534a\u5206\u306b\u5206\u5272\u3059\u308b\u3053\u3068\u3067\u52d5\u4f5c\u3057\u307e\u3059\u3002\u305d\u3057\u3066\u5404\u534a\u5206\u3067\u518d\u5e30\u7684\u306b\u6700\u8fd1\u70b9\u5bfe\u3092\u898b\u3064\u3051\u307e\u3059\u3002\u6700\u5f8c\u306b\u30012\u3064\u306e\u534a\u5206\u306b\u307e\u305f\u304c\u308b\u30da\u30a2\u3092\u8003\u616e\u3057\u3066\u3001\u5168\u4f53\u306e\u6700\u8fd1\u70b9\u5bfe\u3092\u898b\u3064\u3051\u307e\u3059\u3002
        • \u5927\u6574\u6570\u306e\u4e57\u7b97\uff1a\u4e00\u3064\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306fKaratsuba\u3068\u547c\u3070\u308c\u307e\u3059\u3002\u5927\u6574\u6570\u306e\u4e57\u7b97\u3092\u3044\u304f\u3064\u304b\u306e\u5c0f\u3055\u306a\u6574\u6570\u306e\u4e57\u7b97\u3068\u52a0\u7b97\u306b\u5206\u89e3\u3057\u307e\u3059\u3002
        • \u884c\u5217\u306e\u4e57\u7b97\uff1a\u4e00\u4f8b\u306fStrassen\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3059\u3002\u5927\u304d\u306a\u884c\u5217\u306e\u4e57\u7b97\u3092\u8907\u6570\u306e\u5c0f\u3055\u306a\u884c\u5217\u306e\u4e57\u7b97\u3068\u52a0\u7b97\u306b\u5206\u89e3\u3057\u307e\u3059\u3002
        • \u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c\uff1a\u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c\u306f\u518d\u5e30\u7684\u306b\u89e3\u6c7a\u3067\u304d\u3001\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306e\u5178\u578b\u7684\u306a\u5fdc\u7528\u3067\u3059\u3002
        • \u8ee2\u5012\u5bfe\u306e\u89e3\u6c7a\uff1a\u30b7\u30fc\u30b1\u30f3\u30b9\u3067\u3001\u524d\u306e\u6570\u304c\u5f8c\u306e\u6570\u3088\u308a\u5927\u304d\u3044\u5834\u5408\u3001\u3053\u308c\u30892\u3064\u306e\u6570\u306f\u8ee2\u5012\u5bfe\u3092\u69cb\u6210\u3057\u307e\u3059\u3002\u8ee2\u5012\u5bfe\u554f\u984c\u306e\u89e3\u6c7a\u306f\u3001\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u306e\u52a9\u3051\u3092\u501f\u308a\u3066\u3001\u5206\u5272\u7d71\u6cbb\u306e\u30a2\u30a4\u30c7\u30a2\u3092\u5229\u7528\u3067\u304d\u307e\u3059\u3002

        \u5206\u5272\u7d71\u6cbb\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u8a2d\u8a08\u306b\u3082\u5e83\u304f\u5fdc\u7528\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        • \u4e8c\u5206\u63a2\u7d22\uff1a\u4e8c\u5206\u63a2\u7d22\u306f\u3001\u30bd\u30fc\u30c8\u6e08\u307f\u914d\u5217\u3092\u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304b\u30892\u3064\u306e\u534a\u5206\u306b\u5206\u5272\u3057\u307e\u3059\u3002\u305d\u3057\u3066\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u5024\u3068\u4e2d\u9593\u8981\u7d20\u5024\u306e\u6bd4\u8f03\u7d50\u679c\u306b\u57fa\u3065\u3044\u3066\u3001\u4e00\u65b9\u306e\u534a\u5206\u304c\u7834\u68c4\u3055\u308c\u307e\u3059\u3002\u540c\u3058\u30d7\u30ed\u30bb\u30b9\u3067\u6b8b\u308a\u306e\u534a\u5206\u3067\u691c\u7d22\u304c\u7d9a\u884c\u3055\u308c\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u304c\u898b\u3064\u304b\u308b\u304b\u6b8b\u308a\u306e\u8981\u7d20\u304c\u306a\u304f\u306a\u308b\u307e\u3067\u7d9a\u304d\u307e\u3059\u3002
        • \u30de\u30fc\u30b8\u30bd\u30fc\u30c8\uff1a\u3053\u306e\u7bc0\u306e\u5192\u982d\u3067\u3059\u3067\u306b\u7d39\u4ecb\u3057\u305f\u305f\u3081\u3001\u3055\u3089\u306a\u308b\u8a73\u8ff0\u306f\u4e0d\u8981\u3067\u3059\u3002
        • \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\uff1a\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306f\u30d4\u30dc\u30c3\u30c8\u5024\u3092\u9078\u629e\u3057\u3066\u914d\u5217\u30922\u3064\u306e\u526f\u914d\u5217\u306b\u5206\u5272\u3057\u3001\u4e00\u65b9\u306f\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5c0f\u3055\u3044\u8981\u7d20\u3001\u3082\u3046\u4e00\u65b9\u306f\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5927\u304d\u3044\u8981\u7d20\u3092\u6301\u3061\u307e\u3059\u3002\u3053\u306e\u30d7\u30ed\u30bb\u30b9\u306f\u3001\u3053\u308c\u30892\u3064\u306e\u526f\u914d\u5217\u306e\u305d\u308c\u305e\u308c\u306b\u5bfe\u3057\u3066\u30011\u3064\u306e\u8981\u7d20\u306e\u307f\u3092\u4fdd\u6301\u3059\u308b\u307e\u3067\u7d9a\u304d\u307e\u3059\u3002
        • \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\uff1a\u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306e\u57fa\u672c\u7684\u306a\u30a2\u30a4\u30c7\u30a2\u306f\u3001\u30c7\u30fc\u30bf\u3092\u8907\u6570\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u6563\u3055\u305b\u308b\u3053\u3068\u3067\u3059\u3002\u5404\u30d0\u30b1\u30c3\u30c8\u5185\u306e\u8981\u7d20\u3092\u30bd\u30fc\u30c8\u3057\u305f\u5f8c\u3001\u30d0\u30b1\u30c3\u30c8\u304b\u3089\u9806\u5e8f\u3088\u304f\u8981\u7d20\u3092\u53d6\u5f97\u3057\u3066\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u914d\u5217\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002
        • \u6728\uff1a\u4f8b\u3048\u3070\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u3001AVL\u6728\u3001\u8d64\u9ed2\u6728\u3001B\u6728\u3001B+\u6728\u306a\u3069\u3002\u305d\u306e\u64cd\u4f5c\uff08\u691c\u7d22\u3001\u633f\u5165\u3001\u524a\u9664\uff09\u306f\u3059\u3079\u3066\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306e\u5fdc\u7528\u3068\u898b\u306a\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
        • \u30d2\u30fc\u30d7\uff1a\u30d2\u30fc\u30d7\u306f\u7279\u5225\u306a\u30bf\u30a4\u30d7\u306e\u5b8c\u5168\u4e8c\u5206\u6728\u3067\u3059\u3002\u305d\u306e\u69d8\u3005\u306a\u64cd\u4f5c\uff08\u633f\u5165\u3001\u524a\u9664\u3001\u30d2\u30fc\u30d7\u5316\uff09\u306f\u3001\u5b9f\u969b\u306b\u5206\u5272\u7d71\u6cbb\u306e\u30a2\u30a4\u30c7\u30a2\u3092\u542b\u610f\u3057\u3066\u3044\u307e\u3059\u3002
        • \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\uff1a\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306f\u76f4\u63a5\u5206\u5272\u7d71\u6cbb\u3092\u9069\u7528\u3057\u307e\u305b\u3093\u304c\u3001\u4e00\u90e8\u306e\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u89e3\u6c7a\u30bd\u30ea\u30e5\u30fc\u30b7\u30e7\u30f3\u306f\u9593\u63a5\u7684\u306b\u3053\u306e\u6226\u7565\u3092\u9069\u7528\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u30c1\u30a7\u30a4\u30f3\u6cd5\u306e\u9577\u3044\u30ea\u30b9\u30c8\u306f\u3001\u30af\u30a8\u30ea\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u308b\u305f\u3081\u306b\u8d64\u9ed2\u6728\u306b\u5909\u63db\u3055\u308c\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002

        **\u5206\u5272\u7d71\u6cbb\u306f\u5de7\u5999\u306b\u6d78\u900f\u3059\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30a2\u30a4\u30c7\u30a2**\u3067\u3042\u308a\u3001\u69d8\u3005\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u7d44\u307f\u8fbc\u307e\u308c\u3066\u3044\u308b\u3053\u3068\u304c\u5206\u304b\u308a\u307e\u3059\u3002

        "},{"location":"chapter_divide_and_conquer/hanota_problem/","title":"12.4 \u00a0 \u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c","text":"

        \u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u3068\u4e8c\u5206\u6728\u69cb\u7bc9\u306e\u4e21\u65b9\u3067\u3001\u5143\u306e\u554f\u984c\u30922\u3064\u306e\u90e8\u5206\u554f\u984c\u306b\u5206\u89e3\u3057\u3001\u305d\u308c\u305e\u308c\u304c\u5143\u306e\u554f\u984c\u306e\u30b5\u30a4\u30ba\u306e\u534a\u5206\u3067\u3057\u305f\u3002\u3057\u304b\u3057\u3001\u30cf\u30ce\u30a4\u306e\u5854\u3067\u306f\u3001\u7570\u306a\u308b\u5206\u89e3\u6226\u7565\u3092\u63a1\u7528\u3057\u307e\u3059\u3002

        Question

        3\u3064\u306e\u67f1\u304c\u3042\u308a\u3001\u305d\u308c\u305e\u308c A\u3001B\u3001C \u3068\u8868\u8a18\u3055\u308c\u307e\u3059\u3002\u6700\u521d\u3001\u67f1 A \u306b\u306f \\(n\\) \u679a\u306e\u5186\u76e4\u304c\u3042\u308a\u3001\u4e0a\u304b\u3089\u4e0b\u306b\u5411\u304b\u3063\u3066\u6607\u9806\u306e\u30b5\u30a4\u30ba\u3067\u914d\u7f6e\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u79c1\u305f\u3061\u306e\u30bf\u30b9\u30af\u306f\u3001\u3053\u308c\u3089\u306e \\(n\\) \u679a\u306e\u5186\u76e4\u3092\u67f1 C \u306b\u79fb\u52d5\u3057\u3001\u5143\u306e\u9806\u5e8f\u3092\u7dad\u6301\u3059\u308b\u3053\u3068\u3067\u3059\uff08\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\uff09\u3002\u79fb\u52d5\u4e2d\u306b\u306f\u4ee5\u4e0b\u306e\u30eb\u30fc\u30eb\u304c\u9069\u7528\u3055\u308c\u307e\u3059\uff1a

        1. \u5186\u76e4\u306f\u67f1\u306e\u4e0a\u90e8\u304b\u3089\u306e\u307f\u53d6\u308a\u9664\u304f\u3053\u3068\u304c\u3067\u304d\u3001\u5225\u306e\u67f1\u306e\u4e0a\u90e8\u306b\u7f6e\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        2. \u4e00\u5ea6\u306b\u79fb\u52d5\u3067\u304d\u308b\u306e\u306f1\u679a\u306e\u5186\u76e4\u306e\u307f\u3067\u3059\u3002
        3. \u5c0f\u3055\u3044\u5186\u76e4\u306f\u5e38\u306b\u5927\u304d\u3044\u5186\u76e4\u306e\u4e0a\u306b\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u56f3 12-10 \u00a0 \u30cf\u30ce\u30a4\u306e\u5854\u306e\u4f8b

        \u30b5\u30a4\u30ba \\(i\\) \u306e\u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c\u3092 \\(f(i)\\) \u3068\u8868\u8a18\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\\(f(3)\\) \u306f3\u679a\u306e\u5186\u76e4\u3092\u67f1 A \u304b\u3089\u67f1 C \u306b\u79fb\u52d5\u3059\u308b\u3053\u3068\u3092\u8868\u3057\u307e\u3059\u3002

        "},{"location":"chapter_divide_and_conquer/hanota_problem/#1","title":"1. \u00a0 \u57fa\u672c\u30b1\u30fc\u30b9\u3092\u8003\u3048\u308b","text":"

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u554f\u984c \\(f(1)\\)\uff08\u5186\u76e4\u304c1\u679a\u306e\u307f\uff09\u306b\u3064\u3044\u3066\u306f\u3001A \u304b\u3089 C \u306b\u76f4\u63a5\u79fb\u52d5\u3067\u304d\u307e\u3059\u3002

        <1><2>

        \u56f3 12-11 \u00a0 \u30b5\u30a4\u30ba1\u306e\u554f\u984c\u306e\u89e3

        \\(f(2)\\)\uff08\u5186\u76e4\u304c2\u679a\uff09\u306b\u3064\u3044\u3066\u306f\u3001**\u67f1 B \u306e\u52a9\u3051\u3092\u501f\u308a\u3066\u5c0f\u3055\u3044\u5186\u76e4\u3092\u5927\u304d\u3044\u5186\u76e4\u306e\u4e0a\u306b\u4fdd\u3064**\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\uff1a

        1. \u307e\u305a\u3001\u5c0f\u3055\u3044\u5186\u76e4\u3092 A \u304b\u3089 B \u306b\u79fb\u52d5\u3057\u307e\u3059\u3002
        2. \u6b21\u306b\u3001\u5927\u304d\u3044\u5186\u76e4\u3092 A \u304b\u3089 C \u306b\u79fb\u52d5\u3057\u307e\u3059\u3002
        3. \u6700\u5f8c\u306b\u3001\u5c0f\u3055\u3044\u5186\u76e4\u3092 B \u304b\u3089 C \u306b\u79fb\u52d5\u3057\u307e\u3059\u3002
        <1><2><3><4>

        \u56f3 12-12 \u00a0 \u30b5\u30a4\u30ba2\u306e\u554f\u984c\u306e\u89e3

        \\(f(2)\\) \u3092\u89e3\u6c7a\u3059\u308b\u904e\u7a0b\u306f\u6b21\u306e\u3088\u3046\u306b\u8981\u7d04\u3067\u304d\u307e\u3059\uff1aB \u306e\u52a9\u3051\u3092\u501f\u308a\u30662\u679a\u306e\u5186\u76e4\u3092 A \u304b\u3089 C \u306b\u79fb\u52d5\u3059\u308b\u3002\u3053\u3053\u3067\u3001C \u3092\u30bf\u30fc\u30b2\u30c3\u30c8\u67f1\u3001B \u3092\u30d0\u30c3\u30d5\u30a1\u67f1\u3068\u547c\u3073\u307e\u3059\u3002

        "},{"location":"chapter_divide_and_conquer/hanota_problem/#2","title":"2. \u00a0 \u90e8\u5206\u554f\u984c\u306e\u5206\u89e3","text":"

        \u554f\u984c \\(f(3)\\)\uff08\u3064\u307e\u308a\u3001\u5186\u76e4\u304c3\u679a\u306e\u5834\u5408\uff09\u306b\u3064\u3044\u3066\u306f\u3001\u72b6\u6cc1\u304c\u3084\u3084\u8907\u96d1\u306b\u306a\u308a\u307e\u3059\u3002

        \u3059\u3067\u306b \\(f(1)\\) \u3068 \\(f(2)\\) \u306e\u89e3\u304c\u5206\u304b\u3063\u3066\u3044\u308b\u306e\u3067\u3001\u5206\u5272\u7d71\u6cbb\u306e\u89b3\u70b9\u3092\u63a1\u7528\u3057\u3001A \u306e\u4e0a\u306e2\u679a\u306e\u5186\u76e4\u30921\u3064\u306e\u5358\u4f4d\u3068\u3057\u3066\u6271\u3044\u3001\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u30b9\u30c6\u30c3\u30d7\u3092\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u30013\u679a\u306e\u5186\u76e4\u3092 A \u304b\u3089 C \u306b\u6b63\u5e38\u306b\u79fb\u52d5\u3067\u304d\u307e\u3059\u3002

        1. B \u3092\u30bf\u30fc\u30b2\u30c3\u30c8\u67f1\u3001C \u3092\u30d0\u30c3\u30d5\u30a1\u67f1\u3068\u3057\u3066\u30012\u679a\u306e\u5186\u76e4\u3092 A \u304b\u3089 B \u306b\u79fb\u52d5\u3057\u307e\u3059\u3002
        2. \u6b8b\u308a\u306e\u5186\u76e4\u3092 A \u304b\u3089\u76f4\u63a5 C \u306b\u79fb\u52d5\u3057\u307e\u3059\u3002
        3. C \u3092\u30bf\u30fc\u30b2\u30c3\u30c8\u67f1\u3001A \u3092\u30d0\u30c3\u30d5\u30a1\u67f1\u3068\u3057\u3066\u30012\u679a\u306e\u5186\u76e4\u3092 B \u304b\u3089 C \u306b\u79fb\u52d5\u3057\u307e\u3059\u3002
        <1><2><3><4>

        \u56f3 12-13 \u00a0 \u30b5\u30a4\u30ba3\u306e\u554f\u984c\u306e\u89e3

        \u672c\u8cea\u7684\u306b\u3001\\(f(3)\\) \u30922\u3064\u306e \\(f(2)\\) \u90e8\u5206\u554f\u984c\u30681\u3064\u306e \\(f(1)\\) \u90e8\u5206\u554f\u984c\u306b\u5206\u89e3\u3057\u307e\u3059\u3002\u3053\u308c\u30893\u3064\u306e\u90e8\u5206\u554f\u984c\u3092\u9806\u6b21\u89e3\u6c7a\u3059\u308b\u3053\u3068\u3067\u3001\u5143\u306e\u554f\u984c\u304c\u89e3\u6c7a\u3055\u308c\u3001\u90e8\u5206\u554f\u984c\u304c\u72ec\u7acb\u3057\u3066\u304a\u308a\u3001\u305d\u308c\u3089\u306e\u89e3\u3092\u30de\u30fc\u30b8\u3067\u304d\u308b\u3053\u3068\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u3053\u3053\u304b\u3089\u3001\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u30cf\u30ce\u30a4\u306e\u5854\u306e\u5206\u5272\u7d71\u6cbb\u6226\u7565\u3092\u8981\u7d04\u3067\u304d\u307e\u3059\u3002\u5143\u306e\u554f\u984c \\(f(n)\\) \u30922\u3064\u306e\u90e8\u5206\u554f\u984c \\(f(n-1)\\) \u30681\u3064\u306e\u90e8\u5206\u554f\u984c \\(f(1)\\) \u306b\u5206\u5272\u3057\u3001\u4ee5\u4e0b\u306e\u9806\u5e8f\u3067\u3053\u308c\u30893\u3064\u306e\u90e8\u5206\u554f\u984c\u3092\u89e3\u6c7a\u3057\u307e\u3059\uff1a

        1. C \u3092\u30d0\u30c3\u30d5\u30a1\u3068\u3057\u3066\u4f7f\u7528\u3057\u3001\\(n-1\\) \u679a\u306e\u5186\u76e4\u3092 A \u304b\u3089 B \u306b\u79fb\u52d5\u3057\u307e\u3059\u3002
        2. \u6b8b\u308a\u306e\u5186\u76e4\u3092 A \u304b\u3089\u76f4\u63a5 C \u306b\u79fb\u52d5\u3057\u307e\u3059\u3002
        3. A \u3092\u30d0\u30c3\u30d5\u30a1\u3068\u3057\u3066\u4f7f\u7528\u3057\u3001\\(n-1\\) \u679a\u306e\u5186\u76e4\u3092 B \u304b\u3089 C \u306b\u79fb\u52d5\u3057\u307e\u3059\u3002

        \u5404 \\(f(n-1)\\) \u90e8\u5206\u554f\u984c\u306b\u3064\u3044\u3066\u3001\u540c\u3058\u518d\u5e30\u5206\u5272\u3092\u9069\u7528\u3067\u304d\u3001\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c \\(f(1)\\) \u306b\u5230\u9054\u3059\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002\\(f(1)\\) \u306f\u5358\u4e00\u306e\u79fb\u52d5\u306e\u307f\u304c\u5fc5\u8981\u3067\u3042\u308b\u3053\u3068\u304c\u3059\u3067\u306b\u5206\u304b\u3063\u3066\u3044\u308b\u305f\u3081\u3001\u89e3\u6c7a\u3059\u308b\u306e\u306f\u7c21\u5358\u3067\u3059\u3002

        \u56f3 12-14 \u00a0 \u30cf\u30ce\u30a4\u306e\u5854\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306e\u5206\u5272\u7d71\u6cbb\u6226\u7565

        "},{"location":"chapter_divide_and_conquer/hanota_problem/#3","title":"3. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        \u30b3\u30fc\u30c9\u3067\u306f\u3001\u518d\u5e30\u95a2\u6570 dfs(i, src, buf, tar) \u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u67f1 src \u304b\u3089\u4e0a\u306e \\(i\\) \u679a\u306e\u5186\u76e4\u3092\u67f1 tar \u306b\u79fb\u52d5\u3057\u3001\u67f1 buf \u3092\u30d0\u30c3\u30d5\u30a1\u3068\u3057\u3066\u4f7f\u7528\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hanota.py
        def move(src: list[int], tar: list[int]):\n    \"\"\"\u5186\u76e4\u3092\u79fb\u52d5\"\"\"\n    # src \u306e\u4e0a\u304b\u3089\u5186\u76e4\u3092\u53d6\u308a\u51fa\u3059\n    pan = src.pop()\n    # \u5186\u76e4\u3092 tar \u306e\u4e0a\u306b\u7f6e\u304f\n    tar.append(pan)\n\ndef dfs(i: int, src: list[int], buf: list[int], tar: list[int]):\n    \"\"\"\u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c f(i) \u3092\u89e3\u304f\"\"\"\n    # src \u306b\u5186\u76e4\u304c 1 \u3064\u3060\u3051\u6b8b\u3063\u3066\u3044\u308b\u5834\u5408\u3001\u305d\u308c\u3092 tar \u306b\u79fb\u52d5\n    if i == 1:\n        move(src, tar)\n        return\n    # \u90e8\u5206\u554f\u984c f(i-1)\uff1atar \u306e\u52a9\u3051\u3092\u501f\u308a\u3066 src \u306e\u4e0a\u306e i-1 \u500b\u306e\u5186\u76e4\u3092 buf \u306b\u79fb\u52d5\n    dfs(i - 1, src, tar, buf)\n    # \u90e8\u5206\u554f\u984c f(1)\uff1a\u6b8b\u308a\u306e 1 \u500b\u306e\u5186\u76e4\u3092 src \u304b\u3089 tar \u306b\u79fb\u52d5\n    move(src, tar)\n    # \u90e8\u5206\u554f\u984c f(i-1)\uff1asrc \u306e\u52a9\u3051\u3092\u501f\u308a\u3066 buf \u306e\u4e0a\u306e i-1 \u500b\u306e\u5186\u76e4\u3092 tar \u306b\u79fb\u52d5\n    dfs(i - 1, buf, src, tar)\n\ndef solve_hanota(A: list[int], B: list[int], C: list[int]):\n    \"\"\"\u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c\u3092\u89e3\u304f\"\"\"\n    n = len(A)\n    # B \u306e\u52a9\u3051\u3092\u501f\u308a\u3066 A \u306e\u4e0a\u306e n \u500b\u306e\u5186\u76e4\u3092 C \u306b\u79fb\u52d5\n    dfs(n, A, B, C)\n
        hanota.cpp
        /* \u5186\u76e4\u3092\u79fb\u52d5 */\nvoid move(vector<int> &src, vector<int> &tar) {\n    // src \u306e\u6700\u4e0a\u90e8\u304b\u3089\u5186\u76e4\u3092\u53d6\u308a\u51fa\u3059\n    int pan = src.back();\n    src.pop_back();\n    // \u5186\u76e4\u3092 tar \u306e\u6700\u4e0a\u90e8\u306b\u914d\u7f6e\n    tar.push_back(pan);\n}\n\n/* \u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c f(i) \u3092\u89e3\u304f */\nvoid dfs(int i, vector<int> &src, vector<int> &buf, vector<int> &tar) {\n    // src \u306b\u5186\u76e4\u304c1\u3064\u3060\u3051\u6b8b\u3063\u3066\u3044\u308b\u5834\u5408\u3001\u305d\u308c\u3092 tar \u306b\u79fb\u52d5\n    if (i == 1) {\n        move(src, tar);\n        return;\n    }\n    // \u90e8\u5206\u554f\u984c f(i-1)\uff1atar \u306e\u52a9\u3051\u3092\u501f\u308a\u3066\u3001\u4e0a\u4f4d i-1 \u500b\u306e\u5186\u76e4\u3092 src \u304b\u3089 buf \u306b\u79fb\u52d5\n    dfs(i - 1, src, tar, buf);\n    // \u90e8\u5206\u554f\u984c f(1)\uff1a\u6b8b\u308a\u306e1\u3064\u306e\u5186\u76e4\u3092 src \u304b\u3089 tar \u306b\u79fb\u52d5\n    move(src, tar);\n    // \u90e8\u5206\u554f\u984c f(i-1)\uff1asrc \u306e\u52a9\u3051\u3092\u501f\u308a\u3066\u3001\u4e0a\u4f4d i-1 \u500b\u306e\u5186\u76e4\u3092 buf \u304b\u3089 tar \u306b\u79fb\u52d5\n    dfs(i - 1, buf, src, tar);\n}\n\n/* \u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c\u3092\u89e3\u304f */\nvoid solveHanota(vector<int> &A, vector<int> &B, vector<int> &C) {\n    int n = A.size();\n    // B \u306e\u52a9\u3051\u3092\u501f\u308a\u3066\u3001\u4e0a\u4f4d n \u500b\u306e\u5186\u76e4\u3092 A \u304b\u3089 C \u306b\u79fb\u52d5\n    dfs(n, A, B, C);\n}\n
        hanota.java
        /* \u5186\u76e4\u3092\u79fb\u52d5 */\nvoid move(List<Integer> src, List<Integer> tar) {\n    // src \u306e\u6700\u4e0a\u90e8\u304b\u3089\u5186\u76e4\u3092\u53d6\u308a\u51fa\u3059\n    Integer pan = src.remove(src.size() - 1);\n    // \u5186\u76e4\u3092 tar \u306e\u6700\u4e0a\u90e8\u306b\u914d\u7f6e\n    tar.add(pan);\n}\n\n/* \u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c f(i) \u3092\u89e3\u304f */\nvoid dfs(int i, List<Integer> src, List<Integer> buf, List<Integer> tar) {\n    // src \u306b\u5186\u76e4\u304c1\u3064\u3060\u3051\u6b8b\u3063\u3066\u3044\u308b\u5834\u5408\u3001\u305d\u308c\u3092 tar \u306b\u79fb\u52d5\n    if (i == 1) {\n        move(src, tar);\n        return;\n    }\n    // \u90e8\u5206\u554f\u984c f(i-1)\uff1atar \u306e\u52a9\u3051\u3092\u501f\u308a\u3066\u3001\u4e0a\u4f4d i-1 \u500b\u306e\u5186\u76e4\u3092 src \u304b\u3089 buf \u306b\u79fb\u52d5\n    dfs(i - 1, src, tar, buf);\n    // \u90e8\u5206\u554f\u984c f(1)\uff1a\u6b8b\u308a\u306e1\u3064\u306e\u5186\u76e4\u3092 src \u304b\u3089 tar \u306b\u79fb\u52d5\n    move(src, tar);\n    // \u90e8\u5206\u554f\u984c f(i-1)\uff1asrc \u306e\u52a9\u3051\u3092\u501f\u308a\u3066\u3001\u4e0a\u4f4d i-1 \u500b\u306e\u5186\u76e4\u3092 buf \u304b\u3089 tar \u306b\u79fb\u52d5\n    dfs(i - 1, buf, src, tar);\n}\n\n/* \u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c\u3092\u89e3\u304f */\nvoid solveHanota(List<Integer> A, List<Integer> B, List<Integer> C) {\n    int n = A.size();\n    // B \u306e\u52a9\u3051\u3092\u501f\u308a\u3066\u3001\u4e0a\u4f4d n \u500b\u306e\u5186\u76e4\u3092 A \u304b\u3089 C \u306b\u79fb\u52d5\n    dfs(n, A, B, C);\n}\n
        hanota.cs
        [class]{hanota}-[func]{Move}\n\n[class]{hanota}-[func]{DFS}\n\n[class]{hanota}-[func]{SolveHanota}\n
        hanota.go
        [class]{}-[func]{move}\n\n[class]{}-[func]{dfsHanota}\n\n[class]{}-[func]{solveHanota}\n
        hanota.swift
        [class]{}-[func]{move}\n\n[class]{}-[func]{dfs}\n\n[class]{}-[func]{solveHanota}\n
        hanota.js
        [class]{}-[func]{move}\n\n[class]{}-[func]{dfs}\n\n[class]{}-[func]{solveHanota}\n
        hanota.ts
        [class]{}-[func]{move}\n\n[class]{}-[func]{dfs}\n\n[class]{}-[func]{solveHanota}\n
        hanota.dart
        [class]{}-[func]{move}\n\n[class]{}-[func]{dfs}\n\n[class]{}-[func]{solveHanota}\n
        hanota.rs
        [class]{}-[func]{move_pan}\n\n[class]{}-[func]{dfs}\n\n[class]{}-[func]{solve_hanota}\n
        hanota.c
        [class]{}-[func]{move}\n\n[class]{}-[func]{dfs}\n\n[class]{}-[func]{solveHanota}\n
        hanota.kt
        [class]{}-[func]{move}\n\n[class]{}-[func]{dfs}\n\n[class]{}-[func]{solveHanota}\n
        hanota.rb
        [class]{}-[func]{move}\n\n[class]{}-[func]{dfs}\n\n[class]{}-[func]{solve_hanota}\n

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c\u306f\u9ad8\u3055 \\(n\\) \u306e\u518d\u5e30\u6728\u3068\u3057\u3066\u8996\u899a\u5316\u3067\u304d\u307e\u3059\u3002\u5404\u30ce\u30fc\u30c9\u306f\u90e8\u5206\u554f\u984c\u3092\u8868\u3057\u3001dfs() \u306e\u547c\u3073\u51fa\u3057\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(2^n)\\)\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u3067\u3059\u3002

        \u56f3 12-15 \u00a0 \u30cf\u30ce\u30a4\u306e\u5854\u306e\u518d\u5e30\u6728

        Quote

        \u30cf\u30ce\u30a4\u306e\u5854\u306f\u53e4\u4ee3\u306e\u4f1d\u8aac\u306b\u7531\u6765\u3057\u307e\u3059\u3002\u53e4\u4ee3\u30a4\u30f3\u30c9\u306e\u5bfa\u9662\u3067\u3001\u50e7\u4fb6\u305f\u3061\u306f3\u672c\u306e\u9ad8\u3044\u30c0\u30a4\u30e4\u30e2\u30f3\u30c9\u306e\u67f1\u3068\u3001\u7570\u306a\u308b\u30b5\u30a4\u30ba\u306e \\(64\\) \u679a\u306e\u91d1\u306e\u5186\u76e4\u3092\u6301\u3063\u3066\u3044\u307e\u3057\u305f\u3002\u5f7c\u3089\u306f\u3001\u6700\u5f8c\u306e\u5186\u76e4\u304c\u6b63\u3057\u304f\u7f6e\u304b\u308c\u305f\u3068\u304d\u3001\u4e16\u754c\u304c\u7d42\u308f\u308b\u3068\u4fe1\u3058\u3066\u3044\u307e\u3057\u305f\u3002

        \u3057\u304b\u3057\u3001\u50e7\u4fb6\u305f\u3061\u304c1\u79d2\u306b1\u679a\u306e\u5186\u76e4\u3092\u79fb\u52d5\u3057\u305f\u3068\u3057\u3066\u3082\u3001\u7d04 \\(2^{64} \\approx 1.84\u00d710^{19}\\) \u2014\u7d045850\u5104\u5e74\u2014\u304b\u304b\u308a\u3001\u5b87\u5b99\u306e\u5e74\u9f62\u306e\u73fe\u5728\u306e\u63a8\u5b9a\u3092\u306f\u308b\u304b\u306b\u8d85\u3048\u3066\u3044\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u3053\u306e\u4f1d\u8aac\u304c\u771f\u5b9f\u3067\u3042\u308c\u3070\u3001\u4e16\u754c\u306e\u7d42\u308f\u308a\u306b\u3064\u3044\u3066\u5fc3\u914d\u3059\u308b\u5fc5\u8981\u306f\u304a\u305d\u3089\u304f\u306a\u3044\u3067\u3057\u3087\u3046\u3002

        "},{"location":"chapter_divide_and_conquer/summary/","title":"12.5 \u00a0 \u307e\u3068\u3081","text":"
        • \u5206\u5272\u7d71\u6cbb\u306f\u4e00\u822c\u7684\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u8a2d\u8a08\u6226\u7565\u3067\u3001\u5206\u5272\uff08\u5206\u5272\uff09\u3068\u7d71\u6cbb\uff08\u30de\u30fc\u30b8\uff09\u306e2\u3064\u306e\u6bb5\u968e\u304b\u3089\u69cb\u6210\u3055\u308c\u3001\u4e00\u822c\u7684\u306b\u518d\u5e30\u3092\u4f7f\u7528\u3057\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002
        • \u554f\u984c\u304c\u5206\u5272\u7d71\u6cbb\u30a2\u30d7\u30ed\u30fc\u30c1\u306b\u9069\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3092\u5224\u65ad\u3059\u308b\u305f\u3081\u306b\u3001\u554f\u984c\u304c\u5206\u89e3\u53ef\u80fd\u304b\u3069\u3046\u304b\u3001\u90e8\u5206\u554f\u984c\u304c\u72ec\u7acb\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3001\u90e8\u5206\u554f\u984c\u3092\u30de\u30fc\u30b8\u3067\u304d\u308b\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002
        • \u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u306f\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306e\u5178\u578b\u7684\u306a\u4f8b\u3067\u3059\u3002\u914d\u5217\u3092\u518d\u5e30\u7684\u306b2\u3064\u306e\u7b49\u3057\u3044\u9577\u3055\u306e\u526f\u914d\u5217\u306b\u5206\u5272\u3057\u30011\u3064\u306e\u8981\u7d20\u306e\u307f\u304c\u6b8b\u308b\u307e\u3067\u7d9a\u3051\u3001\u6b21\u306b\u3053\u308c\u3089\u306e\u526f\u914d\u5217\u3092\u5c64\u3054\u3068\u306b\u30de\u30fc\u30b8\u3057\u3066\u30bd\u30fc\u30c8\u3092\u5b8c\u4e86\u3057\u307e\u3059\u3002
        • \u5206\u5272\u7d71\u6cbb\u6226\u7565\u306e\u5c0e\u5165\u306f\u3001\u3057\u3070\u3057\u3070\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002\u4e00\u65b9\u3067\u306f\u64cd\u4f5c\u6570\u3092\u6e1b\u3089\u3057\u3001\u4ed6\u65b9\u3067\u306f\u5206\u5272\u5f8c\u306e\u30b7\u30b9\u30c6\u30e0\u306e\u4e26\u5217\u6700\u9069\u5316\u3092\u4fc3\u9032\u3057\u307e\u3059\u3002
        • \u5206\u5272\u7d71\u6cbb\u306f\u591a\u6570\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u306b\u9069\u7528\u3067\u304d\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u8a2d\u8a08\u3067\u5e83\u304f\u4f7f\u7528\u3055\u308c\u3001\u591a\u304f\u306e\u30b7\u30ca\u30ea\u30aa\u306b\u73fe\u308c\u307e\u3059\u3002
        • \u7dcf\u5f53\u305f\u308a\u691c\u7d22\u3068\u6bd4\u8f03\u3057\u3066\u3001\u9069\u5fdc\u691c\u7d22\u306f\u3088\u308a\u52b9\u7387\u7684\u3067\u3059\u3002\u6642\u9593\u8a08\u7b97\u91cf\u304c \\(O(\\log n)\\) \u306e\u691c\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u901a\u5e38\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306b\u57fa\u3065\u3044\u3066\u3044\u307e\u3059\u3002
        • \u4e8c\u5206\u63a2\u7d22\u306f\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306e\u3082\u3046\u4e00\u3064\u306e\u53e4\u5178\u7684\u306a\u5fdc\u7528\u3067\u3059\u3002\u90e8\u5206\u554f\u984c\u306e\u89e3\u306e\u30de\u30fc\u30b8\u3092\u542b\u307e\u305a\u3001\u518d\u5e30\u7684\u306a\u5206\u5272\u7d71\u6cbb\u30a2\u30d7\u30ed\u30fc\u30c1\u3067\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002
        • \u4e8c\u5206\u6728\u69cb\u7bc9\u554f\u984c\u3067\u306f\u3001\u6728\u306e\u69cb\u7bc9\uff08\u5143\u306e\u554f\u984c\uff09\u3092\u5de6\u306e\u90e8\u5206\u6728\u3068\u53f3\u306e\u90e8\u5206\u6728\u306e\u69cb\u7bc9\uff08\u90e8\u5206\u554f\u984c\uff09\u306b\u5206\u5272\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306f\u524d\u9806\u8d70\u67fb\u3068\u4e2d\u9806\u8d70\u67fb\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2\u3092\u5206\u5272\u3059\u308b\u3053\u3068\u3067\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002
        • \u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c\u3067\u306f\u3001\u30b5\u30a4\u30ba \\(n\\) \u306e\u554f\u984c\u3092\u30b5\u30a4\u30ba \\(n-1\\) \u306e2\u3064\u306e\u90e8\u5206\u554f\u984c\u3068\u30b5\u30a4\u30ba \\(1\\) \u306e1\u3064\u306e\u90e8\u5206\u554f\u984c\u306b\u5206\u89e3\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u30893\u3064\u306e\u90e8\u5206\u554f\u984c\u3092\u9806\u6b21\u89e3\u6c7a\u3059\u308b\u3053\u3068\u3067\u3001\u5143\u306e\u554f\u984c\u304c\u89e3\u6c7a\u3055\u308c\u307e\u3059\u3002
        "},{"location":"chapter_dynamic_programming/","title":"\u7b2c 14 \u7ae0 \u00a0 \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0","text":"

        Abstract

        \u5ddd\u304c\u6d41\u308c\u3066\u6d77\u306b\u6ce8\u3050\u3088\u3046\u306b\u3001

        \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u5c0f\u3055\u306a\u554f\u984c\u306e\u89e3\u3092\u7e54\u308a\u5408\u308f\u305b\u3066\u3001\u3088\u308a\u5927\u304d\u306a\u554f\u984c\u306e\u89e3\u3078\u3068\u5c0e\u304d\u307e\u3059\u3002\u4e00\u6b69\u4e00\u6b69\u9032\u3093\u3067\u3001\u6700\u7d42\u7684\u306a\u7b54\u3048\u304c\u5f85\u3064\u5f7c\u5cb8\u3078\u3068\u5411\u304b\u3044\u307e\u3059\u3002

        "},{"location":"chapter_dynamic_programming/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 14.1 \u00a0 \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u5165\u9580
        • 14.2 \u00a0 DP\u554f\u984c\u306e\u7279\u6027
        • 14.3 \u00a0 DP\u554f\u984c\u89e3\u6c7a\u30a2\u30d7\u30ed\u30fc\u30c1
        • 14.4 \u00a0 0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c
        • 14.5 \u00a0 \u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c
        • 14.6 \u00a0 \u7de8\u96c6\u8ddd\u96e2\u554f\u984c
        • 14.7 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_dynamic_programming/dp_problem_features/","title":"14.2 \u00a0 \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u306e\u7279\u5fb4","text":"

        \u524d\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u304c\u554f\u984c\u3092\u90e8\u5206\u554f\u984c\u306b\u5206\u89e3\u3059\u308b\u3053\u3068\u3067\u5143\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u65b9\u6cd5\u3092\u5b66\u3073\u307e\u3057\u305f\u3002\u5b9f\u969b\u3001\u90e8\u5206\u554f\u984c\u306e\u5206\u89e3\u306f\u4e00\u822c\u7684\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30a2\u30d7\u30ed\u30fc\u30c1\u3067\u3042\u308a\u3001\u5206\u5272\u7d71\u6cbb\u6cd5\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3067\u306f\u7570\u306a\u308b\u91cd\u70b9\u304c\u3042\u308a\u307e\u3059\u3002

        • \u5206\u5272\u7d71\u6cbb\u6cd5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u5143\u306e\u554f\u984c\u3092\u8907\u6570\u306e\u72ec\u7acb\u3057\u305f\u90e8\u5206\u554f\u984c\u306b\u518d\u5e30\u7684\u306b\u5206\u5272\u3057\u3001\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306b\u5230\u9054\u3059\u308b\u307e\u3067\u7d9a\u3051\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u6642\u306b\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u7d44\u307f\u5408\u308f\u305b\u3066\u6700\u7d42\u7684\u306b\u5143\u306e\u554f\u984c\u306e\u89e3\u3092\u5f97\u307e\u3059\u3002
        • \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3082\u554f\u984c\u3092\u518d\u5e30\u7684\u306b\u5206\u89e3\u3057\u307e\u3059\u304c\u3001\u5206\u5272\u7d71\u6cbb\u6cd5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u306e\u4e3b\u306a\u9055\u3044\u306f\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306e\u90e8\u5206\u554f\u984c\u304c\u76f8\u4e92\u4f9d\u5b58\u7684\u3067\u3042\u308a\u3001\u5206\u89e3\u30d7\u30ed\u30bb\u30b9\u4e2d\u306b\u591a\u304f\u306e\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u304c\u73fe\u308c\u308b\u3053\u3068\u3067\u3059\u3002
        • \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u8a66\u884c\u932f\u8aa4\u306b\u3088\u3063\u3066\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u89e3\u3092\u7db2\u7f85\u3057\u3001\u679d\u5208\u308a\u306b\u3088\u3063\u3066\u4e0d\u5fc5\u8981\u306a\u63a2\u7d22\u5206\u5c90\u3092\u907f\u3051\u307e\u3059\u3002\u5143\u306e\u554f\u984c\u306e\u89e3\u306f\u4e00\u9023\u306e\u6c7a\u5b9a\u30b9\u30c6\u30c3\u30d7\u304b\u3089\u69cb\u6210\u3055\u308c\u3001\u5404\u6c7a\u5b9a\u30b9\u30c6\u30c3\u30d7\u524d\u306e\u5404\u90e8\u5206\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u90e8\u5206\u554f\u984c\u3068\u3057\u3066\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u5b9f\u969b\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u6700\u9069\u5316\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3088\u304f\u4f7f\u7528\u3055\u308c\u3001\u3053\u308c\u3089\u306f\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u3092\u542b\u3080\u3060\u3051\u3067\u306a\u304f\u3001\u4ed6\u306b2\u3064\u306e\u4e3b\u8981\u306a\u7279\u5fb4\u304c\u3042\u308a\u307e\u3059\uff1a\u6700\u9069\u90e8\u5206\u69cb\u9020\u3068\u7121\u8a18\u61b6\u6027\u3067\u3059\u3002

        "},{"location":"chapter_dynamic_programming/dp_problem_features/#1421","title":"14.2.1 \u00a0 \u6700\u9069\u90e8\u5206\u69cb\u9020","text":"

        \u968e\u6bb5\u767b\u308a\u554f\u984c\u3092\u5c11\u3057\u4fee\u6b63\u3057\u3066\u3001\u6700\u9069\u90e8\u5206\u69cb\u9020\u306e\u6982\u5ff5\u3092\u5b9f\u8a3c\u3059\u308b\u306e\u306b\u3088\u308a\u9069\u3057\u305f\u3082\u306e\u306b\u3057\u307e\u3059\u3002

        \u968e\u6bb5\u767b\u308a\u306e\u6700\u5c0f\u30b3\u30b9\u30c8

        \u968e\u6bb5\u304c\u3042\u308a\u3001\u4e00\u5ea6\u306b1\u6bb5\u307e\u305f\u306f2\u6bb5\u4e0a\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u968e\u6bb5\u306e\u5404\u6bb5\u306b\u306f\u305d\u306e\u6bb5\u3067\u652f\u6255\u3046\u5fc5\u8981\u304c\u3042\u308b\u30b3\u30b9\u30c8\u3092\u8868\u3059\u975e\u8ca0\u306e\u6574\u6570\u304c\u3042\u308a\u307e\u3059\u3002\u975e\u8ca0\u306e\u6574\u6570\u914d\u5217 \\(cost\\) \u304c\u4e0e\u3048\u3089\u308c\u3001\\(cost[i]\\) \u306f \\(i\\) \u6bb5\u76ee\u3067\u652f\u6255\u3046\u5fc5\u8981\u304c\u3042\u308b\u30b3\u30b9\u30c8\u3092\u8868\u3057\u3001\\(cost[0]\\) \u306f\u5730\u9762\uff08\u958b\u59cb\u70b9\uff09\u3067\u3059\u3002\u9802\u4e0a\u306b\u5230\u9054\u3059\u308b\u305f\u3081\u306b\u5fc5\u8981\u306a\u6700\u5c0f\u30b3\u30b9\u30c8\u306f\u4f55\u3067\u3059\u304b\uff1f

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u30011\u6bb5\u76ee\u30012\u6bb5\u76ee\u30013\u6bb5\u76ee\u306e\u30b3\u30b9\u30c8\u304c\u305d\u308c\u305e\u308c \\(1\\)\u3001\\(10\\)\u3001\\(1\\) \u306e\u5834\u5408\u3001\u5730\u9762\u304b\u30893\u6bb5\u76ee\u306b\u767b\u308b\u6700\u5c0f\u30b3\u30b9\u30c8\u306f \\(2\\) \u3067\u3059\u3002

        \u56f3 14-6 \u00a0 3\u6bb5\u76ee\u306b\u767b\u308b\u6700\u5c0f\u30b3\u30b9\u30c8

        \\(dp[i]\\) \u3092 \\(i\\) \u6bb5\u76ee\u306b\u767b\u308b\u7d2f\u7a4d\u30b3\u30b9\u30c8\u3068\u3057\u307e\u3059\u3002\\(i\\) \u6bb5\u76ee\u306f \\(i-1\\) \u6bb5\u76ee\u307e\u305f\u306f \\(i-2\\) \u6bb5\u76ee\u304b\u3089\u306e\u307f\u6765\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u305f\u3081\u3001\\(dp[i]\\) \u306f \\(dp[i-1] + cost[i]\\) \u307e\u305f\u306f \\(dp[i-2] + cost[i]\\) \u306e\u3044\u305a\u308c\u304b\u3057\u304b\u3042\u308a\u3048\u307e\u305b\u3093\u3002\u30b3\u30b9\u30c8\u3092\u6700\u5c0f\u5316\u3059\u308b\u305f\u3081\u306b\u30012\u3064\u306e\u3046\u3061\u5c0f\u3055\u3044\u65b9\u3092\u9078\u629e\u3059\u3079\u304d\u3067\u3059\uff1a

        \\[ dp[i] = \\min(dp[i-1], dp[i-2]) + cost[i] \\]

        \u3053\u308c\u306b\u3088\u308a\u6700\u9069\u90e8\u5206\u69cb\u9020\u306e\u610f\u5473\u304c\u308f\u304b\u308a\u307e\u3059\uff1a\u5143\u306e\u554f\u984c\u306e\u6700\u9069\u89e3\u306f\u90e8\u5206\u554f\u984c\u306e\u6700\u9069\u89e3\u304b\u3089\u69cb\u7bc9\u3055\u308c\u308b\u3002

        \u3053\u306e\u554f\u984c\u306f\u660e\u3089\u304b\u306b\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u6301\u3063\u3066\u3044\u307e\u3059\uff1a2\u3064\u306e\u90e8\u5206\u554f\u984c \\(dp[i-1]\\) \u3068 \\(dp[i-2]\\) \u306e\u6700\u9069\u89e3\u304b\u3089\u3088\u308a\u826f\u3044\u65b9\u3092\u9078\u629e\u3057\u3001\u305d\u308c\u3092\u4f7f\u7528\u3057\u3066\u5143\u306e\u554f\u984c \\(dp[i]\\) \u306e\u6700\u9069\u89e3\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002

        \u3067\u306f\u3001\u524d\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u306e\u968e\u6bb5\u767b\u308a\u554f\u984c\u306f\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u6301\u3063\u3066\u3044\u308b\u3067\u3057\u3087\u3046\u304b\uff1f\u305d\u306e\u76ee\u6a19\u306f\u89e3\u306e\u6570\u3092\u6c42\u3081\u308b\u3053\u3068\u3067\u3001\u3053\u308c\u306f\u6570\u3048\u4e0a\u3052\u554f\u984c\u306e\u3088\u3046\u3067\u3059\u304c\u3001\u5225\u306e\u65b9\u6cd5\u3067\u5c0b\u306d\u3066\u307f\u307e\u3057\u3087\u3046\uff1a\u300c\u89e3\u306e\u6700\u5927\u6570\u3092\u6c42\u3081\u308b\u300d\u3002\u9a5a\u304f\u3053\u3068\u306b\u3001**\u554f\u984c\u304c\u5909\u308f\u3063\u305f\u306b\u3082\u304b\u304b\u308f\u3089\u305a\u3001\u6700\u9069\u90e8\u5206\u69cb\u9020\u304c\u73fe\u308c\u305f**\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\uff1a\\(n\\) \u6bb5\u76ee\u3067\u306e\u89e3\u306e\u6700\u5927\u6570\u306f\u3001\\(n-1\\) \u6bb5\u76ee\u3068 \\(n-2\\) \u6bb5\u76ee\u3067\u306e\u89e3\u306e\u6700\u5927\u6570\u306e\u548c\u306b\u7b49\u3057\u3044\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u6700\u9069\u90e8\u5206\u69cb\u9020\u306e\u89e3\u91c8\u306f\u975e\u5e38\u306b\u67d4\u8edf\u3067\u3001\u7570\u306a\u308b\u554f\u984c\u3067\u306f\u7570\u306a\u308b\u610f\u5473\u3092\u6301\u3061\u307e\u3059\u3002

        \u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3068\u521d\u671f\u72b6\u614b \\(dp[1] = cost[1]\\) \u304a\u3088\u3073 \\(dp[2] = cost[2]\\) \u306b\u5f93\u3063\u3066\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30b3\u30fc\u30c9\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_cost_climbing_stairs_dp.py
        def min_cost_climbing_stairs_dp(cost: list[int]) -> int:\n    \"\"\"\u6700\u5c0f\u30b3\u30b9\u30c8\u968e\u6bb5\u767b\u308a\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n = len(cost) - 1\n    if n == 1 or n == 2:\n        return cost[n]\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    dp = [0] * (n + 1)\n    # \u521d\u671f\u72b6\u614b\uff1a\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4e8b\u524d\u8a2d\u5b9a\n    dp[1], dp[2] = cost[1], cost[2]\n    # \u72b6\u614b\u9077\u79fb\uff1a\u5c0f\u3055\u3044\u90e8\u5206\u554f\u984c\u304b\u3089\u5927\u304d\u3044\u90e8\u5206\u554f\u984c\u3092\u6bb5\u968e\u7684\u306b\u89e3\u304f\n    for i in range(3, n + 1):\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n    return dp[n]\n
        min_cost_climbing_stairs_dp.cpp
        /* \u6700\u5c0f\u30b3\u30b9\u30c8\u968e\u6bb5\u767b\u308a\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint minCostClimbingStairsDP(vector<int> &cost) {\n    int n = cost.size() - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3057\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    vector<int> dp(n + 1);\n    // \u521d\u671f\u72b6\u614b\uff1a\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4e8b\u524d\u8a2d\u5b9a\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u72b6\u614b\u9077\u79fb\uff1a\u5c0f\u3055\u306a\u554f\u984c\u304b\u3089\u5927\u304d\u306a\u90e8\u5206\u554f\u984c\u3092\u6bb5\u968e\u7684\u306b\u89e3\u304f\n    for (int i = 3; i <= n; i++) {\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
        min_cost_climbing_stairs_dp.java
        /* \u6700\u5c0f\u30b3\u30b9\u30c8\u968e\u6bb5\u767b\u308a\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint minCostClimbingStairsDP(int[] cost) {\n    int n = cost.length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3057\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    int[] dp = new int[n + 1];\n    // \u521d\u671f\u72b6\u614b\uff1a\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4e8b\u524d\u8a2d\u5b9a\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u72b6\u614b\u9077\u79fb\uff1a\u5c0f\u3055\u306a\u554f\u984c\u304b\u3089\u5927\u304d\u306a\u90e8\u5206\u554f\u984c\u3092\u6bb5\u968e\u7684\u306b\u89e3\u304f\n    for (int i = 3; i <= n; i++) {\n        dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
        min_cost_climbing_stairs_dp.cs
        [class]{min_cost_climbing_stairs_dp}-[func]{MinCostClimbingStairsDP}\n
        min_cost_climbing_stairs_dp.go
        [class]{}-[func]{minCostClimbingStairsDP}\n
        min_cost_climbing_stairs_dp.swift
        [class]{}-[func]{minCostClimbingStairsDP}\n
        min_cost_climbing_stairs_dp.js
        [class]{}-[func]{minCostClimbingStairsDP}\n
        min_cost_climbing_stairs_dp.ts
        [class]{}-[func]{minCostClimbingStairsDP}\n
        min_cost_climbing_stairs_dp.dart
        [class]{}-[func]{minCostClimbingStairsDP}\n
        min_cost_climbing_stairs_dp.rs
        [class]{}-[func]{min_cost_climbing_stairs_dp}\n
        min_cost_climbing_stairs_dp.c
        [class]{}-[func]{minCostClimbingStairsDP}\n
        min_cost_climbing_stairs_dp.kt
        [class]{}-[func]{minCostClimbingStairsDP}\n
        min_cost_climbing_stairs_dp.rb
        [class]{}-[func]{min_cost_climbing_stairs_dp}\n

        \u4e0b\u306e\u56f3\u306f\u4e0a\u8a18\u30b3\u30fc\u30c9\u306e\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 14-7 \u00a0 \u968e\u6bb5\u767b\u308a\u306e\u6700\u5c0f\u30b3\u30b9\u30c8\u306e\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9

        \u3053\u306e\u554f\u984c\u3082\u7a7a\u9593\u6700\u9069\u5316\u304c\u53ef\u80fd\u3067\u30011\u6b21\u5143\u30920\u306b\u5727\u7e2e\u3057\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u3092 \\(O(n)\\) \u304b\u3089 \\(O(1)\\) \u306b\u524a\u6e1b\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_cost_climbing_stairs_dp.py
        def min_cost_climbing_stairs_dp_comp(cost: list[int]) -> int:\n    \"\"\"\u6700\u5c0f\u30b3\u30b9\u30c8\u968e\u6bb5\u767b\u308a\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n = len(cost) - 1\n    if n == 1 or n == 2:\n        return cost[n]\n    a, b = cost[1], cost[2]\n    for i in range(3, n + 1):\n        a, b = b, min(a, b) + cost[i]\n    return b\n
        min_cost_climbing_stairs_dp.cpp
        /* \u6700\u5c0f\u30b3\u30b9\u30c8\u968e\u6bb5\u767b\u308a\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint minCostClimbingStairsDPComp(vector<int> &cost) {\n    int n = cost.size() - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
        min_cost_climbing_stairs_dp.java
        /* \u6700\u5c0f\u30b3\u30b9\u30c8\u968e\u6bb5\u767b\u308a\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint minCostClimbingStairsDPComp(int[] cost) {\n    int n = cost.length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = Math.min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
        min_cost_climbing_stairs_dp.cs
        [class]{min_cost_climbing_stairs_dp}-[func]{MinCostClimbingStairsDPComp}\n
        min_cost_climbing_stairs_dp.go
        [class]{}-[func]{minCostClimbingStairsDPComp}\n
        min_cost_climbing_stairs_dp.swift
        [class]{}-[func]{minCostClimbingStairsDPComp}\n
        min_cost_climbing_stairs_dp.js
        [class]{}-[func]{minCostClimbingStairsDPComp}\n
        min_cost_climbing_stairs_dp.ts
        [class]{}-[func]{minCostClimbingStairsDPComp}\n
        min_cost_climbing_stairs_dp.dart
        [class]{}-[func]{minCostClimbingStairsDPComp}\n
        min_cost_climbing_stairs_dp.rs
        [class]{}-[func]{min_cost_climbing_stairs_dp_comp}\n
        min_cost_climbing_stairs_dp.c
        [class]{}-[func]{minCostClimbingStairsDPComp}\n
        min_cost_climbing_stairs_dp.kt
        [class]{}-[func]{minCostClimbingStairsDPComp}\n
        min_cost_climbing_stairs_dp.rb
        [class]{}-[func]{min_cost_climbing_stairs_dp_comp}\n
        "},{"location":"chapter_dynamic_programming/dp_problem_features/#1422","title":"14.2.2 \u00a0 \u7121\u8a18\u61b6\u6027","text":"

        \u7121\u8a18\u61b6\u6027\u306f\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u304c\u554f\u984c\u89e3\u6c7a\u306b\u52b9\u679c\u7684\u3067\u3042\u308b\u3053\u3068\u3092\u53ef\u80fd\u306b\u3059\u308b\u91cd\u8981\u306a\u7279\u5fb4\u306e1\u3064\u3067\u3059\u3002\u305d\u306e\u5b9a\u7fa9\u306f\uff1a\u7279\u5b9a\u306e\u72b6\u614b\u304c\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001\u305d\u306e\u5c06\u6765\u306e\u767a\u5c55\u306f\u73fe\u5728\u306e\u72b6\u614b\u306e\u307f\u306b\u95a2\u9023\u3057\u3001\u904e\u53bb\u306b\u7d4c\u9a13\u3057\u305f\u3059\u3079\u3066\u306e\u72b6\u614b\u3068\u306f\u7121\u95a2\u4fc2\u3067\u3042\u308b\u3002

        \u968e\u6bb5\u767b\u308a\u554f\u984c\u3092\u4f8b\u306b\u53d6\u308b\u3068\u3001\u72b6\u614b \\(i\\) \u304c\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001\u305d\u308c\u306f\u72b6\u614b \\(i+1\\) \u3068 \\(i+2\\) \u306b\u767a\u5c55\u3057\u3001\u305d\u308c\u305e\u308c1\u6bb5\u30b8\u30e3\u30f3\u30d7\u30682\u6bb5\u30b8\u30e3\u30f3\u30d7\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002\u3053\u308c\u30892\u3064\u306e\u9078\u629e\u3092\u3059\u308b\u3068\u304d\u3001\u72b6\u614b \\(i\\) \u3088\u308a\u524d\u306e\u72b6\u614b\u3092\u8003\u616e\u3059\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u306a\u305c\u306a\u3089\u3001\u305d\u308c\u3089\u306f\u72b6\u614b \\(i\\) \u306e\u5c06\u6765\u306b\u5f71\u97ff\u3057\u306a\u3044\u304b\u3089\u3067\u3059\u3002

        \u3057\u304b\u3057\u3001\u968e\u6bb5\u767b\u308a\u554f\u984c\u306b\u5236\u7d04\u3092\u8ffd\u52a0\u3059\u308b\u3068\u3001\u72b6\u6cc1\u304c\u5909\u308f\u308a\u307e\u3059\u3002

        \u5236\u7d04\u4ed8\u304d\u968e\u6bb5\u767b\u308a

        \\(n\\) \u6bb5\u306e\u968e\u6bb5\u304c\u3042\u308a\u3001\u6bce\u56de1\u6bb5\u307e\u305f\u306f2\u6bb5\u4e0a\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u304c\u30011\u6bb5\u30922\u56de\u9023\u7d9a\u3067\u30b8\u30e3\u30f3\u30d7\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002\u9802\u4e0a\u306b\u767b\u308b\u65b9\u6cd5\u306f\u4f55\u901a\u308a\u3042\u308a\u307e\u3059\u304b\uff1f

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u30013\u6bb5\u76ee\u306b\u767b\u308b\u5b9f\u884c\u53ef\u80fd\u306a\u9078\u629e\u80a2\u306f2\u3064\u3060\u3051\u3067\u30011\u6bb5\u30923\u56de\u9023\u7d9a\u3067\u30b8\u30e3\u30f3\u30d7\u3059\u308b\u9078\u629e\u80a2\u306f\u5236\u7d04\u6761\u4ef6\u3092\u6e80\u305f\u3055\u306a\u3044\u305f\u3081\u7834\u68c4\u3055\u308c\u307e\u3059\u3002

        \u56f3 14-8 \u00a0 \u5236\u7d04\u4ed8\u304d\u30673\u6bb5\u76ee\u306b\u767b\u308b\u5b9f\u884c\u53ef\u80fd\u306a\u9078\u629e\u80a2\u306e\u6570

        \u3053\u306e\u554f\u984c\u3067\u306f\u3001\u524d\u56de\u304c1\u6bb5\u30b8\u30e3\u30f3\u30d7\u3060\u3063\u305f\u5834\u5408\u3001\u6b21\u56de\u306f\u5fc5\u305a2\u6bb5\u30b8\u30e3\u30f3\u30d7\u3067\u306a\u3051\u308c\u3070\u306a\u308a\u307e\u305b\u3093\u3002\u3053\u308c\u306f**\u6b21\u306e\u30b9\u30c6\u30c3\u30d7\u306e\u9078\u629e\u304c\u73fe\u5728\u306e\u72b6\u614b\uff08\u73fe\u5728\u306e\u968e\u6bb5\u6bb5\u6570\uff09\u3060\u3051\u3067\u306f\u72ec\u7acb\u3057\u3066\u6c7a\u5b9a\u3067\u304d\u305a\u3001\u524d\u306e\u72b6\u614b\uff08\u524d\u56de\u306e\u968e\u6bb5\u6bb5\u6570\uff09\u306b\u3082\u4f9d\u5b58\u3059\u308b**\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

        \u3053\u306e\u554f\u984c\u304c\u3082\u306f\u3084\u7121\u8a18\u61b6\u6027\u3092\u6e80\u305f\u3055\u305a\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f \\(dp[i] = dp[i-1] + dp[i-2]\\) \u3082\u5931\u6557\u3059\u308b\u3053\u3068\u306f\u5bb9\u6613\u306b\u308f\u304b\u308a\u307e\u3059\u3002\u306a\u305c\u306a\u3089 \\(dp[i-1]\\) \u306f\u4eca\u56de\u306e1\u6bb5\u30b8\u30e3\u30f3\u30d7\u3092\u8868\u3057\u307e\u3059\u304c\u3001\u591a\u304f\u306e\u300c\u524d\u56de\u304c1\u6bb5\u30b8\u30e3\u30f3\u30d7\u3060\u3063\u305f\u300d\u9078\u629e\u80a2\u3092\u542b\u3093\u3067\u304a\u308a\u3001\u5236\u7d04\u3092\u6e80\u305f\u3059\u305f\u3081\u306b\u306f\u3053\u308c\u3089\u3092\u76f4\u63a5 \\(dp[i]\\) \u306b\u542b\u3081\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002

        \u3053\u306e\u305f\u3081\u3001\u72b6\u614b\u5b9a\u7fa9\u3092\u62e1\u5f35\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff1a\u72b6\u614b \\([i, j]\\) \u306f \\(i\\) \u6bb5\u76ee\u306b\u3044\u3066\u3001\u524d\u56de\u304c \\(j\\) \u6bb5\u30b8\u30e3\u30f3\u30d7\u3060\u3063\u305f\u3053\u3068\u3092\u8868\u3059\u3002\u3053\u3053\u3067 \\(j \\in \\{1, 2\\}\\) \u3067\u3059\u3002\u3053\u306e\u72b6\u614b\u5b9a\u7fa9\u306f\u524d\u56de\u304c1\u6bb5\u30b8\u30e3\u30f3\u30d7\u3060\u3063\u305f\u304b2\u6bb5\u30b8\u30e3\u30f3\u30d7\u3060\u3063\u305f\u304b\u3092\u52b9\u679c\u7684\u306b\u533a\u5225\u3057\u3001\u73fe\u5728\u306e\u72b6\u614b\u304c\u3069\u3053\u304b\u3089\u6765\u305f\u304b\u3092\u9069\u5207\u306b\u5224\u65ad\u3067\u304d\u307e\u3059\u3002

        • \u524d\u56de\u304c1\u6bb5\u30b8\u30e3\u30f3\u30d7\u3060\u3063\u305f\u5834\u5408\u3001\u524d\u3005\u56de\u306f\u5fc5\u305a2\u6bb5\u30b8\u30e3\u30f3\u30d7\u3092\u9078\u629e\u3057\u3066\u3044\u305f\u306f\u305a\u3067\u3059\u3002\u3064\u307e\u308a\u3001\\(dp[i, 1]\\) \u306f \\(dp[i-1, 2]\\) \u304b\u3089\u306e\u307f\u9077\u79fb\u3067\u304d\u307e\u3059\u3002
        • \u524d\u56de\u304c2\u6bb5\u30b8\u30e3\u30f3\u30d7\u3060\u3063\u305f\u5834\u5408\u3001\u524d\u3005\u56de\u306f1\u6bb5\u30b8\u30e3\u30f3\u30d7\u307e\u305f\u306f2\u6bb5\u30b8\u30e3\u30f3\u30d7\u3092\u9078\u629e\u3067\u304d\u307e\u3057\u305f\u3002\u3064\u307e\u308a\u3001\\(dp[i, 2]\\) \u306f \\(dp[i-2, 1]\\) \u307e\u305f\u306f \\(dp[i-2, 2]\\) \u304b\u3089\u9077\u79fb\u3067\u304d\u307e\u3059\u3002

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\\(dp[i, j]\\) \u306f\u72b6\u614b \\([i, j]\\) \u306e\u89e3\u306e\u6570\u3092\u8868\u3057\u307e\u3059\u3002\u3053\u306e\u6642\u70b9\u3067\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u306f\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\uff1a

        \\[ \\begin{cases} dp[i, 1] = dp[i-1, 2] \\\\ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2] \\end{cases} \\]

        \u56f3 14-9 \u00a0 \u5236\u7d04\u3092\u8003\u616e\u3057\u305f\u518d\u5e30\u95a2\u4fc2

        \u6700\u7d42\u7684\u306b\u3001\\(dp[n, 1] + dp[n, 2]\\) \u3092\u8fd4\u305b\u3070\u3088\u304f\u3001\u3053\u306e2\u3064\u306e\u5408\u8a08\u304c \\(n\\) \u6bb5\u76ee\u306b\u767b\u308b\u89e3\u306e\u7dcf\u6570\u3092\u8868\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_constraint_dp.py
        def climbing_stairs_constraint_dp(n: int) -> int:\n    \"\"\"\u5236\u7d04\u4ed8\u304d\u968e\u6bb5\u767b\u308a\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    if n == 1 or n == 2:\n        return 1\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    dp = [[0] * 3 for _ in range(n + 1)]\n    # \u521d\u671f\u72b6\u614b\uff1a\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4e8b\u524d\u8a2d\u5b9a\n    dp[1][1], dp[1][2] = 1, 0\n    dp[2][1], dp[2][2] = 0, 1\n    # \u72b6\u614b\u9077\u79fb\uff1a\u5c0f\u3055\u3044\u90e8\u5206\u554f\u984c\u304b\u3089\u5927\u304d\u3044\u90e8\u5206\u554f\u984c\u3092\u6bb5\u968e\u7684\u306b\u89e3\u304f\n    for i in range(3, n + 1):\n        dp[i][1] = dp[i - 1][2]\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n    return dp[n][1] + dp[n][2]\n
        climbing_stairs_constraint_dp.cpp
        /* \u5236\u7d04\u4ed8\u304d\u968e\u6bb5\u767b\u308a\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint climbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3057\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    vector<vector<int>> dp(n + 1, vector<int>(3, 0));\n    // \u521d\u671f\u72b6\u614b\uff1a\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4e8b\u524d\u8a2d\u5b9a\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // \u72b6\u614b\u9077\u79fb\uff1a\u5c0f\u3055\u306a\u554f\u984c\u304b\u3089\u5927\u304d\u306a\u90e8\u5206\u554f\u984c\u3092\u6bb5\u968e\u7684\u306b\u89e3\u304f\n    for (int i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
        climbing_stairs_constraint_dp.java
        /* \u5236\u7d04\u4ed8\u304d\u968e\u6bb5\u767b\u308a\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint climbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3057\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    int[][] dp = new int[n + 1][3];\n    // \u521d\u671f\u72b6\u614b\uff1a\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4e8b\u524d\u8a2d\u5b9a\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // \u72b6\u614b\u9077\u79fb\uff1a\u5c0f\u3055\u306a\u554f\u984c\u304b\u3089\u5927\u304d\u306a\u90e8\u5206\u554f\u984c\u3092\u6bb5\u968e\u7684\u306b\u89e3\u304f\n    for (int i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
        climbing_stairs_constraint_dp.cs
        [class]{climbing_stairs_constraint_dp}-[func]{ClimbingStairsConstraintDP}\n
        climbing_stairs_constraint_dp.go
        [class]{}-[func]{climbingStairsConstraintDP}\n
        climbing_stairs_constraint_dp.swift
        [class]{}-[func]{climbingStairsConstraintDP}\n
        climbing_stairs_constraint_dp.js
        [class]{}-[func]{climbingStairsConstraintDP}\n
        climbing_stairs_constraint_dp.ts
        [class]{}-[func]{climbingStairsConstraintDP}\n
        climbing_stairs_constraint_dp.dart
        [class]{}-[func]{climbingStairsConstraintDP}\n
        climbing_stairs_constraint_dp.rs
        [class]{}-[func]{climbing_stairs_constraint_dp}\n
        climbing_stairs_constraint_dp.c
        [class]{}-[func]{climbingStairsConstraintDP}\n
        climbing_stairs_constraint_dp.kt
        [class]{}-[func]{climbingStairsConstraintDP}\n
        climbing_stairs_constraint_dp.rb
        [class]{}-[func]{climbing_stairs_constraint_dp}\n

        \u4e0a\u8a18\u306e\u30b1\u30fc\u30b9\u3067\u306f\u3001\u524d\u306e\u72b6\u614b\u306e\u307f\u3092\u8003\u616e\u3059\u308c\u3070\u3088\u3044\u305f\u3081\u3001\u72b6\u614b\u5b9a\u7fa9\u3092\u62e1\u5f35\u3059\u308b\u3053\u3068\u3067\u4f9d\u7136\u3068\u3057\u3066\u7121\u8a18\u61b6\u6027\u3092\u6e80\u305f\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u4e00\u90e8\u306e\u554f\u984c\u3067\u306f\u975e\u5e38\u306b\u6df1\u523b\u306a\u300c\u72b6\u614b\u52b9\u679c\u300d\u304c\u3042\u308a\u307e\u3059\u3002

        \u969c\u5bb3\u7269\u751f\u6210\u4ed8\u304d\u968e\u6bb5\u767b\u308a

        \\(n\\) \u6bb5\u306e\u968e\u6bb5\u304c\u3042\u308a\u3001\u6bce\u56de1\u6bb5\u307e\u305f\u306f2\u6bb5\u4e0a\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002**\\(i\\) \u6bb5\u76ee\u306b\u767b\u3063\u305f\u3068\u304d\u3001\u30b7\u30b9\u30c6\u30e0\u304c\u81ea\u52d5\u7684\u306b \\(2i\\) \u6bb5\u76ee\u306b\u969c\u5bb3\u7269\u3092\u7f6e\u304d\u3001\u305d\u306e\u5f8c\u306e\u3059\u3079\u3066\u306e\u30e9\u30a6\u30f3\u30c9\u3067 \\(2i\\) \u6bb5\u76ee\u306b\u30b8\u30e3\u30f3\u30d7\u3059\u308b\u3053\u3068\u304c\u7981\u6b62\u3055\u308c\u308b**\u3068\u898f\u5b9a\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u6700\u521d\u306e2\u30e9\u30a6\u30f3\u30c9\u30672\u6bb5\u76ee\u30683\u6bb5\u76ee\u306b\u30b8\u30e3\u30f3\u30d7\u3057\u305f\u5834\u5408\u3001\u305d\u306e\u5f8c\u306f4\u6bb5\u76ee\u30686\u6bb5\u76ee\u306b\u30b8\u30e3\u30f3\u30d7\u3067\u304d\u307e\u305b\u3093\u3002\u9802\u4e0a\u306b\u767b\u308b\u65b9\u6cd5\u306f\u4f55\u901a\u308a\u3042\u308a\u307e\u3059\u304b\uff1f

        \u3053\u306e\u554f\u984c\u3067\u306f\u3001\u6b21\u306e\u30b8\u30e3\u30f3\u30d7\u306f\u3059\u3079\u3066\u306e\u904e\u53bb\u306e\u72b6\u614b\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002\u5404\u30b8\u30e3\u30f3\u30d7\u304c\u3088\u308a\u9ad8\u3044\u6bb5\u306b\u969c\u5bb3\u7269\u3092\u7f6e\u304d\u3001\u5c06\u6765\u306e\u30b8\u30e3\u30f3\u30d7\u306b\u5f71\u97ff\u3059\u308b\u304b\u3089\u3067\u3059\u3002\u3053\u306e\u3088\u3046\u306a\u554f\u984c\u3067\u306f\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u3057\u3070\u3057\u3070\u89e3\u6c7a\u306b\u82e6\u52b4\u3057\u307e\u3059\u3002

        \u5b9f\u969b\u3001\u591a\u304f\u306e\u8907\u96d1\u306a\u7d44\u307f\u5408\u308f\u305b\u6700\u9069\u5316\u554f\u984c\uff08\u5de1\u56de\u30bb\u30fc\u30eb\u30b9\u30de\u30f3\u554f\u984c\u306a\u3069\uff09\u306f\u7121\u8a18\u61b6\u6027\u3092\u6e80\u305f\u3057\u307e\u305b\u3093\u3002\u3053\u306e\u3088\u3046\u306a\u554f\u984c\u306b\u5bfe\u3057\u3066\u306f\u3001\u901a\u5e38\u3001\u30d2\u30e5\u30fc\u30ea\u30b9\u30c6\u30a3\u30c3\u30af\u63a2\u7d22\u3001\u907a\u4f1d\u7684\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3001\u5f37\u5316\u5b66\u7fd2\u306a\u3069\u306e\u4ed6\u306e\u65b9\u6cd5\u3092\u9078\u629e\u3057\u3066\u3001\u9650\u3089\u308c\u305f\u6642\u9593\u5185\u306b\u4f7f\u7528\u53ef\u80fd\u306a\u5c40\u6240\u6700\u9069\u89e3\u3092\u5f97\u307e\u3059\u3002

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/","title":"14.3 \u00a0 \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u89e3\u6c7a\u30a2\u30d7\u30ed\u30fc\u30c1","text":"

        \u524d\u306e2\u3064\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u306e\u4e3b\u8981\u306a\u7279\u5fb4\u3092\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002\u6b21\u306b\u3001\u3088\u308a\u5b9f\u7528\u7684\u306a2\u3064\u306e\u554f\u984c\u3092\u4e00\u7dd2\u306b\u63a2\u7d22\u3057\u307e\u3057\u3087\u3046\u3002

        1. \u554f\u984c\u304c\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u304b\u3069\u3046\u304b\u3092\u3069\u306e\u3088\u3046\u306b\u5224\u65ad\u3059\u308b\u304b\uff1f
        2. \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u5b8c\u5168\u306a\u30b9\u30c6\u30c3\u30d7\u306f\u4f55\u304b\uff1f
        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1431","title":"14.3.1 \u00a0 \u554f\u984c\u306e\u5224\u5b9a","text":"

        \u4e00\u822c\u7684\u306b\u8a00\u3048\u3070\u3001\u554f\u984c\u304c\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u3001\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u542b\u307f\u3001\u7121\u8a18\u61b6\u6027\u3092\u793a\u3059\u5834\u5408\u3001\u901a\u5e38\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u89e3\u6cd5\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u554f\u984c\u306e\u8aac\u660e\u304b\u3089\u76f4\u63a5\u3053\u308c\u3089\u306e\u7279\u5fb4\u3092\u62bd\u51fa\u3059\u308b\u3053\u3068\u306f\u3057\u3070\u3057\u3070\u56f0\u96e3\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u901a\u5e38\u306f\u6761\u4ef6\u3092\u7de9\u548c\u3057\u3001**\u307e\u305a\u554f\u984c\u304c\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\uff08\u5168\u63a2\u7d22\uff09\u3092\u4f7f\u7528\u3057\u305f\u89e3\u6c7a\u306b\u9069\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3092\u89b3\u5bdf**\u3057\u307e\u3059\u3002

        **\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306b\u9069\u3057\u305f\u554f\u984c\u306f\u901a\u5e38\u300c\u6c7a\u5b9a\u6728\u30e2\u30c7\u30eb\u300d\u306b\u9069\u5408**\u3057\u3001\u3053\u308c\u306f\u6728\u69cb\u9020\u3092\u4f7f\u7528\u3057\u3066\u8a18\u8ff0\u3067\u304d\u3001\u5404\u30ce\u30fc\u30c9\u306f\u6c7a\u5b9a\u3092\u8868\u3057\u3001\u5404\u30d1\u30b9\u306f\u6c7a\u5b9a\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u8868\u3057\u307e\u3059\u3002

        \u8a00\u3044\u63db\u3048\u308b\u3068\u3001\u554f\u984c\u304c\u660e\u793a\u7684\u306a\u6c7a\u5b9a\u6982\u5ff5\u3092\u542b\u307f\u3001\u89e3\u304c\u4e00\u9023\u306e\u6c7a\u5b9a\u3092\u901a\u3058\u3066\u751f\u6210\u3055\u308c\u308b\u5834\u5408\u3001\u305d\u308c\u306f\u6c7a\u5b9a\u6728\u30e2\u30c7\u30eb\u306b\u9069\u5408\u3057\u3001\u901a\u5e38\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u3066\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002

        \u3053\u306e\u57fa\u790e\u306e\u4e0a\u3067\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u3092\u5224\u5b9a\u3059\u308b\u305f\u3081\u306e\u300c\u30dc\u30fc\u30ca\u30b9\u30dd\u30a4\u30f3\u30c8\u300d\u304c\u3042\u308a\u307e\u3059\u3002

        • \u554f\u984c\u306b\u6700\u5927\u5316\uff08\u6700\u5c0f\u5316\uff09\u307e\u305f\u306f\u6700\u3082\uff08\u6700\u3082\u5c11\u306a\u3044\uff09\u6700\u9069\u306a\u89e3\u3092\u898b\u3064\u3051\u308b\u3068\u3044\u3046\u8a18\u8ff0\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u3002
        • \u554f\u984c\u306e\u72b6\u614b\u304c\u30ea\u30b9\u30c8\u3001\u591a\u6b21\u5143\u884c\u5217\u3001\u307e\u305f\u306f\u6728\u3092\u4f7f\u7528\u3057\u3066\u8868\u73fe\u3067\u304d\u3001\u72b6\u614b\u304c\u305d\u306e\u5468\u56f2\u306e\u72b6\u614b\u3068\u518d\u5e30\u95a2\u4fc2\u3092\u6301\u3063\u3066\u3044\u308b\u3002

        \u5bfe\u5fdc\u3057\u3066\u3001\u300c\u30da\u30ca\u30eb\u30c6\u30a3\u30dd\u30a4\u30f3\u30c8\u300d\u3082\u3042\u308a\u307e\u3059\u3002

        • \u554f\u984c\u306e\u76ee\u6a19\u306f\u6700\u9069\u89e3\u3060\u3051\u3067\u306a\u304f\u3001\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3067\u3042\u308b\u3002
        • \u554f\u984c\u306e\u8aac\u660e\u306b\u9806\u5217\u3068\u7d44\u307f\u5408\u308f\u305b\u306e\u660e\u3089\u304b\u306a\u7279\u5fb4\u304c\u3042\u308a\u3001\u7279\u5b9a\u306e\u8907\u6570\u306e\u89e3\u3092\u8fd4\u3059\u5fc5\u8981\u304c\u3042\u308b\u3002

        \u554f\u984c\u304c\u6c7a\u5b9a\u6728\u30e2\u30c7\u30eb\u306b\u9069\u5408\u3057\u3001\u6bd4\u8f03\u7684\u660e\u3089\u304b\u306a\u300c\u30dc\u30fc\u30ca\u30b9\u30dd\u30a4\u30f3\u30c8\u300d\u3092\u6301\u3064\u5834\u5408\u3001\u305d\u308c\u304c\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u3067\u3042\u308b\u3068\u4eee\u5b9a\u3057\u3001\u89e3\u6c7a\u30d7\u30ed\u30bb\u30b9\u4e2d\u306b\u691c\u8a3c\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1432","title":"14.3.2 \u00a0 \u554f\u984c\u89e3\u6c7a\u30b9\u30c6\u30c3\u30d7","text":"

        \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u89e3\u6c7a\u30d7\u30ed\u30bb\u30b9\u306f\u554f\u984c\u306e\u6027\u8cea\u3068\u96e3\u6613\u5ea6\u306b\u3088\u3063\u3066\u7570\u306a\u308a\u307e\u3059\u304c\u3001\u4e00\u822c\u7684\u306b\u6b21\u306e\u30b9\u30c6\u30c3\u30d7\u306b\u5f93\u3044\u307e\u3059\uff1a\u6c7a\u5b9a\u306e\u8a18\u8ff0\u3001\u72b6\u614b\u306e\u5b9a\u7fa9\u3001\\(dp\\) \u30c6\u30fc\u30d6\u30eb\u306e\u78ba\u7acb\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u306e\u5c0e\u51fa\u3001\u5883\u754c\u6761\u4ef6\u306e\u6c7a\u5b9a\u306a\u3069\u3002

        \u554f\u984c\u89e3\u6c7a\u30b9\u30c6\u30c3\u30d7\u3092\u3088\u308a\u5177\u4f53\u7684\u306b\u8aac\u660e\u3059\u308b\u305f\u3081\u306b\u3001\u53e4\u5178\u7684\u306a\u554f\u984c\u300c\u6700\u5c0f\u7d4c\u8def\u548c\u300d\u3092\u4f8b\u3068\u3057\u3066\u4f7f\u7528\u3057\u307e\u3059\u3002

        Question

        \\(n \\times m\\) \u306e\u4e8c\u6b21\u5143\u30b0\u30ea\u30c3\u30c9 grid \u304c\u4e0e\u3048\u3089\u308c\u3001\u30b0\u30ea\u30c3\u30c9\u306e\u5404\u30bb\u30eb\u306b\u306f\u8ca0\u3067\u306a\u3044\u6574\u6570\u304c\u542b\u307e\u308c\u3001\u305d\u306e\u30bb\u30eb\u306e\u30b3\u30b9\u30c8\u3092\u8868\u3057\u307e\u3059\u3002\u30ed\u30dc\u30c3\u30c8\u306f\u5de6\u4e0a\u306e\u30bb\u30eb\u304b\u3089\u59cb\u307e\u308a\u3001\u5404\u30b9\u30c6\u30c3\u30d7\u3067\u4e0b\u307e\u305f\u306f\u53f3\u306b\u306e\u307f\u79fb\u52d5\u3067\u304d\u3001\u53f3\u4e0b\u306e\u30bb\u30eb\u306b\u5230\u9054\u3059\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002\u5de6\u4e0a\u304b\u3089\u53f3\u4e0b\u3078\u306e\u6700\u5c0f\u7d4c\u8def\u548c\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u4e0b\u306e\u56f3\u306f\u4f8b\u3092\u793a\u3057\u3066\u304a\u308a\u3001\u4e0e\u3048\u3089\u308c\u305f\u30b0\u30ea\u30c3\u30c9\u306e\u6700\u5c0f\u7d4c\u8def\u548c\u306f \\(13\\) \u3067\u3059\u3002

        \u56f3 14-10 \u00a0 \u6700\u5c0f\u7d4c\u8def\u548c\u306e\u4f8b\u30c7\u30fc\u30bf

        \u7b2c1\u30b9\u30c6\u30c3\u30d7\uff1a\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u6c7a\u5b9a\u3092\u8003\u3048\u3001\u72b6\u614b\u3092\u5b9a\u7fa9\u3057\u3001\u305d\u308c\u306b\u3088\u308a \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u3092\u5f97\u308b

        \u3053\u306e\u554f\u984c\u306e\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u6c7a\u5b9a\u306f\u3001\u73fe\u5728\u306e\u30bb\u30eb\u304b\u3089\u4e0b\u307e\u305f\u306f\u53f3\u306b1\u30b9\u30c6\u30c3\u30d7\u79fb\u52d5\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u73fe\u5728\u306e\u30bb\u30eb\u306e\u884c\u3068\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c \\([i, j]\\) \u3067\u3042\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u4e0b\u307e\u305f\u306f\u53f3\u306b\u79fb\u52d5\u3057\u305f\u5f8c\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f \\([i+1, j]\\) \u307e\u305f\u306f \\([i, j+1]\\) \u306b\u306a\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u72b6\u614b\u306b\u306f2\u3064\u306e\u5909\u6570\u304c\u542b\u307e\u308c\u308b\u3079\u304d\u3067\u3059\uff1a\u884c\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3068\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3001\\([i, j]\\) \u3068\u8868\u8a18\u3055\u308c\u307e\u3059\u3002

        \u72b6\u614b \\([i, j]\\) \u306f\u90e8\u5206\u554f\u984c\u306b\u5bfe\u5fdc\u3057\u307e\u3059\uff1a\u958b\u59cb\u70b9 \\([0, 0]\\) \u304b\u3089 \\([i, j]\\) \u3078\u306e\u6700\u5c0f\u7d4c\u8def\u548c\u3001\\(dp[i, j]\\) \u3068\u8868\u8a18\u3055\u308c\u307e\u3059\u3002

        \u3053\u306e\u3088\u3046\u306b\u3057\u3066\u3001\u4e0b\u306e\u56f3\u306b\u793a\u3059\u4e8c\u6b21\u5143 \\(dp\\) \u884c\u5217\u3092\u5f97\u307e\u3059\u3002\u305d\u306e\u30b5\u30a4\u30ba\u306f\u5165\u529b\u30b0\u30ea\u30c3\u30c9 \\(grid\\) \u3068\u540c\u3058\u3067\u3059\u3002

        \u56f3 14-11 \u00a0 \u72b6\u614b\u5b9a\u7fa9\u3068DP\u30c6\u30fc\u30d6\u30eb

        Note

        \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3068\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306f\u6c7a\u5b9a\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3068\u3057\u3066\u8a18\u8ff0\u3067\u304d\u3001\u72b6\u614b\u306f\u3059\u3079\u3066\u306e\u6c7a\u5b9a\u5909\u6570\u304b\u3089\u69cb\u6210\u3055\u308c\u307e\u3059\u3002\u554f\u984c\u89e3\u6c7a\u306e\u9032\u884c\u3092\u8a18\u8ff0\u3059\u308b\u3059\u3079\u3066\u306e\u5909\u6570\u3092\u542b\u3080\u3079\u304d\u3067\u3001\u6b21\u306e\u72b6\u614b\u3092\u5c0e\u51fa\u3059\u308b\u306e\u306b\u5341\u5206\u306a\u60c5\u5831\u3092\u542b\u3093\u3067\u3044\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u5404\u72b6\u614b\u306f\u90e8\u5206\u554f\u984c\u306b\u5bfe\u5fdc\u3057\u3001\u3059\u3079\u3066\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4fdd\u5b58\u3059\u308b\u305f\u3081\u306e \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002\u72b6\u614b\u306e\u5404\u72ec\u7acb\u5909\u6570\u306f \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u306e\u6b21\u5143\u3067\u3059\u3002\u672c\u8cea\u7684\u306b\u3001\\(dp\\) \u30c6\u30fc\u30d6\u30eb\u306f\u72b6\u614b\u3068\u90e8\u5206\u554f\u984c\u306e\u89e3\u306e\u9593\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u3067\u3059\u3002

        \u7b2c2\u30b9\u30c6\u30c3\u30d7\uff1a\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u7279\u5b9a\u3057\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3092\u5c0e\u51fa\u3059\u308b

        \u72b6\u614b \\([i, j]\\) \u306b\u3064\u3044\u3066\u3001\u305d\u308c\u306f\u4e0a\u306e\u30bb\u30eb \\([i-1, j]\\) \u307e\u305f\u306f\u5de6\u306e\u30bb\u30eb \\([i, j-1]\\) \u304b\u3089\u306e\u307f\u5c0e\u51fa\u3067\u304d\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u6700\u9069\u90e8\u5206\u69cb\u9020\u306f\uff1a\\([i, j]\\) \u306b\u5230\u9054\u3059\u308b\u6700\u5c0f\u7d4c\u8def\u548c\u306f\u3001\\([i, j-1]\\) \u3068 \\([i-1, j]\\) \u306e\u6700\u5c0f\u7d4c\u8def\u548c\u306e\u5c0f\u3055\u3044\u65b9\u306b\u3088\u3063\u3066\u6c7a\u5b9a\u3055\u308c\u307e\u3059\u3002

        \u4e0a\u8a18\u306e\u5206\u6790\u306b\u57fa\u3065\u3044\u3066\u3001\u4e0b\u306e\u56f3\u306b\u793a\u3059\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3092\u5c0e\u51fa\u3067\u304d\u307e\u3059\uff1a

        \\[ dp[i, j] = \\min(dp[i-1, j], dp[i, j-1]) + grid[i, j] \\]

        \u56f3 14-12 \u00a0 \u6700\u9069\u90e8\u5206\u69cb\u9020\u3068\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f

        Note

        \u5b9a\u7fa9\u3055\u308c\u305f \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u306b\u57fa\u3065\u3044\u3066\u3001\u5143\u306e\u554f\u984c\u3068\u90e8\u5206\u554f\u984c\u306e\u95a2\u4fc2\u3092\u8003\u3048\u3001\u90e8\u5206\u554f\u984c\u306e\u6700\u9069\u89e3\u304b\u3089\u5143\u306e\u554f\u984c\u306e\u6700\u9069\u89e3\u3092\u3069\u306e\u3088\u3046\u306b\u69cb\u7bc9\u3059\u308b\u304b\u3001\u3064\u307e\u308a\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u898b\u3064\u3051\u307e\u3059\u3002

        \u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u7279\u5b9a\u3057\u305f\u3089\u3001\u305d\u308c\u3092\u4f7f\u7528\u3057\u3066\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3092\u69cb\u7bc9\u3067\u304d\u307e\u3059\u3002

        \u7b2c3\u30b9\u30c6\u30c3\u30d7\uff1a\u5883\u754c\u6761\u4ef6\u3068\u72b6\u614b\u9077\u79fb\u9806\u5e8f\u3092\u6c7a\u5b9a\u3059\u308b

        \u3053\u306e\u554f\u984c\u3067\u306f\u3001\u6700\u521d\u306e\u884c\u306e\u72b6\u614b\u306f\u5de6\u306e\u72b6\u614b\u304b\u3089\u306e\u307f\u6765\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u6700\u521d\u306e\u5217\u306e\u72b6\u614b\u306f\u4e0a\u306e\u72b6\u614b\u304b\u3089\u306e\u307f\u6765\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u305f\u3081\u3001\u6700\u521d\u306e\u884c \\(i = 0\\) \u3068\u6700\u521d\u306e\u5217 \\(j = 0\\) \u304c\u5883\u754c\u6761\u4ef6\u3067\u3059\u3002

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5404\u30bb\u30eb\u306f\u5de6\u306e\u30bb\u30eb\u3068\u4e0a\u306e\u30bb\u30eb\u304b\u3089\u5c0e\u51fa\u3055\u308c\u308b\u305f\u3081\u3001\u30eb\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u3066\u884c\u5217\u3092\u8d70\u67fb\u3057\u3001\u5916\u5074\u306e\u30eb\u30fc\u30d7\u306f\u884c\u3092\u53cd\u5fa9\u3057\u3001\u5185\u5074\u306e\u30eb\u30fc\u30d7\u306f\u5217\u3092\u53cd\u5fa9\u3057\u307e\u3059\u3002

        \u56f3 14-13 \u00a0 \u5883\u754c\u6761\u4ef6\u3068\u72b6\u614b\u9077\u79fb\u9806\u5e8f

        Note

        \u5883\u754c\u6761\u4ef6\u306f\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3067 \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u3001\u63a2\u7d22\u3067\u306f\u679d\u5208\u308a\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002

        \u72b6\u614b\u9077\u79fb\u9806\u5e8f\u306e\u6838\u5fc3\u306f\u3001\u73fe\u5728\u306e\u554f\u984c\u306e\u89e3\u3092\u8a08\u7b97\u3059\u308b\u3068\u304d\u3001\u305d\u308c\u304c\u4f9d\u5b58\u3059\u308b\u3059\u3079\u3066\u306e\u5c0f\u3055\u306a\u90e8\u5206\u554f\u984c\u304c\u65e2\u306b\u6b63\u3057\u304f\u8a08\u7b97\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u4fdd\u3059\u308b\u3053\u3068\u3067\u3059\u3002

        \u4e0a\u8a18\u306e\u5206\u6790\u306b\u57fa\u3065\u3044\u3066\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30b3\u30fc\u30c9\u3092\u76f4\u63a5\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u90e8\u5206\u554f\u984c\u306e\u5206\u89e3\u306f\u30c8\u30c3\u30d7\u30c0\u30a6\u30f3\u30a2\u30d7\u30ed\u30fc\u30c1\u3067\u3042\u308b\u305f\u3081\u3001\u300c\u529b\u4efb\u305b\u63a2\u7d22 \u2192 \u30e1\u30e2\u5316\u63a2\u7d22 \u2192 \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u300d\u306e\u9806\u5e8f\u3067\u5b9f\u88c5\u3059\u308b\u3053\u3068\u304c\u7fd2\u6163\u7684\u306a\u601d\u8003\u306b\u3088\u308a\u9069\u5408\u3057\u307e\u3059\u3002

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1-1","title":"1. \u00a0 \u65b9\u6cd51\uff1a\u529b\u4efb\u305b\u63a2\u7d22","text":"

        \u72b6\u614b \\([i, j]\\) \u304b\u3089\u63a2\u7d22\u3092\u958b\u59cb\u3057\u3001\u305d\u308c\u3092\u5e38\u306b\u3088\u308a\u5c0f\u3055\u306a\u72b6\u614b \\([i-1, j]\\) \u3068 \\([i, j-1]\\) \u306b\u5206\u89e3\u3057\u307e\u3059\u3002\u518d\u5e30\u95a2\u6570\u306b\u306f\u4ee5\u4e0b\u306e\u8981\u7d20\u304c\u542b\u307e\u308c\u307e\u3059\u3002

        • \u518d\u5e30\u30d1\u30e9\u30e1\u30fc\u30bf\uff1a\u72b6\u614b \\([i, j]\\)\u3002
        • \u623b\u308a\u5024\uff1a\\([0, 0]\\) \u304b\u3089 \\([i, j]\\) \u3078\u306e\u6700\u5c0f\u7d4c\u8def\u548c \\(dp[i, j]\\)\u3002
        • \u7d42\u4e86\u6761\u4ef6\uff1a\\(i = 0\\) \u304b\u3064 \\(j = 0\\) \u306e\u3068\u304d\u3001\u30b3\u30b9\u30c8 \\(grid[0, 0]\\) \u3092\u8fd4\u3059\u3002
        • \u679d\u5208\u308a\uff1a\\(i < 0\\) \u307e\u305f\u306f \\(j < 0\\) \u3067\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u3068\u304d\u3001\u30b3\u30b9\u30c8 \\(+\\infty\\) \u3092\u8fd4\u3057\u3001\u5b9f\u884c\u4e0d\u53ef\u80fd\u6027\u3092\u8868\u3059\u3002

        \u5b9f\u88c5\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
        def min_path_sum_dfs(grid: list[list[int]], i: int, j: int) -> int:\n    \"\"\"\u6700\u5c0f\u30d1\u30b9\u548c\uff1a\u30d6\u30eb\u30fc\u30c8\u30d5\u30a9\u30fc\u30b9\u63a2\u7d22\"\"\"\n    # \u5de6\u4e0a\u306e\u30bb\u30eb\u306e\u5834\u5408\u3001\u63a2\u7d22\u3092\u7d42\u4e86\n    if i == 0 and j == 0:\n        return grid[0][0]\n    # \u884c\u307e\u305f\u306f\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001+\u221e \u30b3\u30b9\u30c8\u3092\u8fd4\u3059\n    if i < 0 or j < 0:\n        return inf\n    # \u5de6\u4e0a\u304b\u3089 (i-1, j) \u3068 (i, j-1) \u3078\u306e\u6700\u5c0f\u30d1\u30b9\u30b3\u30b9\u30c8\u3092\u8a08\u7b97\n    up = min_path_sum_dfs(grid, i - 1, j)\n    left = min_path_sum_dfs(grid, i, j - 1)\n    # \u5de6\u4e0a\u304b\u3089 (i, j) \u3078\u306e\u6700\u5c0f\u30d1\u30b9\u30b3\u30b9\u30c8\u3092\u8fd4\u3059\n    return min(left, up) + grid[i][j]\n
        min_path_sum.cpp
        /* \u6700\u5c0f\u30d1\u30b9\u548c\uff1a\u30d6\u30eb\u30fc\u30c8\u30d5\u30a9\u30fc\u30b9\u63a2\u7d22 */\nint minPathSumDFS(vector<vector<int>> &grid, int i, int j) {\n    // \u5de6\u4e0a\u306e\u30bb\u30eb\u306e\u5834\u5408\u3001\u63a2\u7d22\u3092\u7d42\u4e86\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u884c\u307e\u305f\u306f\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001+\u221e \u306e\u30b3\u30b9\u30c8\u3092\u8fd4\u3059\n    if (i < 0 || j < 0) {\n        return INT_MAX;\n    }\n    // \u5de6\u4e0a\u304b\u3089 (i-1, j) \u3068 (i, j-1) \u3078\u306e\u6700\u5c0f\u30d1\u30b9\u30b3\u30b9\u30c8\u3092\u8a08\u7b97\n    int up = minPathSumDFS(grid, i - 1, j);\n    int left = minPathSumDFS(grid, i, j - 1);\n    // \u5de6\u4e0a\u304b\u3089 (i, j) \u3078\u306e\u6700\u5c0f\u30d1\u30b9\u30b3\u30b9\u30c8\u3092\u8fd4\u3059\n    return min(left, up) + grid[i][j];\n}\n
        min_path_sum.java
        /* \u6700\u5c0f\u30d1\u30b9\u548c\uff1a\u30d6\u30eb\u30fc\u30c8\u30d5\u30a9\u30fc\u30b9\u63a2\u7d22 */\nint minPathSumDFS(int[][] grid, int i, int j) {\n    // \u5de6\u4e0a\u306e\u30bb\u30eb\u306e\u5834\u5408\u3001\u63a2\u7d22\u3092\u7d42\u4e86\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u884c\u307e\u305f\u306f\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001+\u221e \u306e\u30b3\u30b9\u30c8\u3092\u8fd4\u3059\n    if (i < 0 || j < 0) {\n        return Integer.MAX_VALUE;\n    }\n    // \u5de6\u4e0a\u304b\u3089 (i-1, j) \u3068 (i, j-1) \u3078\u306e\u6700\u5c0f\u30d1\u30b9\u30b3\u30b9\u30c8\u3092\u8a08\u7b97\n    int up = minPathSumDFS(grid, i - 1, j);\n    int left = minPathSumDFS(grid, i, j - 1);\n    // \u5de6\u4e0a\u304b\u3089 (i, j) \u3078\u306e\u6700\u5c0f\u30d1\u30b9\u30b3\u30b9\u30c8\u3092\u8fd4\u3059\n    return Math.min(left, up) + grid[i][j];\n}\n
        min_path_sum.cs
        [class]{min_path_sum}-[func]{MinPathSumDFS}\n
        min_path_sum.go
        [class]{}-[func]{minPathSumDFS}\n
        min_path_sum.swift
        [class]{}-[func]{minPathSumDFS}\n
        min_path_sum.js
        [class]{}-[func]{minPathSumDFS}\n
        min_path_sum.ts
        [class]{}-[func]{minPathSumDFS}\n
        min_path_sum.dart
        [class]{}-[func]{minPathSumDFS}\n
        min_path_sum.rs
        [class]{}-[func]{min_path_sum_dfs}\n
        min_path_sum.c
        [class]{}-[func]{minPathSumDFS}\n
        min_path_sum.kt
        [class]{}-[func]{minPathSumDFS}\n
        min_path_sum.rb
        [class]{}-[func]{min_path_sum_dfs}\n

        \u4e0b\u306e\u56f3\u306f \\(dp[2, 1]\\) \u3092\u6839\u3068\u3059\u308b\u518d\u5e30\u6728\u3092\u793a\u3057\u3066\u304a\u308a\u3001\u3044\u304f\u3064\u304b\u306e\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u3092\u542b\u307f\u3001\u305d\u306e\u6570\u306f\u30b0\u30ea\u30c3\u30c9 grid \u306e\u30b5\u30a4\u30ba\u304c\u5897\u52a0\u3059\u308b\u3068\u6025\u6fc0\u306b\u5897\u52a0\u3057\u307e\u3059\u3002

        \u672c\u8cea\u7684\u306b\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u306e\u7406\u7531\u306f\uff1a**\u5de6\u4e0a\u9685\u304b\u3089\u7279\u5b9a\u306e\u30bb\u30eb\u306b\u5230\u9054\u3059\u308b\u8907\u6570\u306e\u30d1\u30b9\u304c\u5b58\u5728\u3059\u308b**\u3053\u3068\u3067\u3059\u3002

        \u56f3 14-14 \u00a0 \u529b\u4efb\u305b\u63a2\u7d22\u306e\u518d\u5e30\u6728

        \u5404\u72b6\u614b\u306b\u306f\u4e0b\u3068\u53f3\u306e2\u3064\u306e\u9078\u629e\u304c\u3042\u308b\u305f\u3081\u3001\u5de6\u4e0a\u9685\u304b\u3089\u53f3\u4e0b\u9685\u307e\u3067\u306e\u7dcf\u30b9\u30c6\u30c3\u30d7\u6570\u306f \\(m + n - 2\\) \u3067\u3001\u6700\u60aa\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(2^{m + n})\\) \u3067\u3059\u3002\u3053\u306e\u8a08\u7b97\u65b9\u6cd5\u306f\u30b0\u30ea\u30c3\u30c9\u30a8\u30c3\u30b8\u8fd1\u304f\u306e\u72b6\u6cc1\u3092\u8003\u616e\u3057\u3066\u3044\u306a\u3044\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u30a8\u30c3\u30b8\u306b\u5230\u9054\u3057\u305f\u3068\u304d\u3001\u9078\u629e\u80a2\u304c1\u3064\u3057\u304b\u6b8b\u3089\u306a\u3044\u305f\u3081\u3001\u5b9f\u969b\u306e\u30d1\u30b9\u6570\u306f\u3088\u308a\u5c11\u306a\u304f\u306a\u308a\u307e\u3059\u3002

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#2-2","title":"2. \u00a0 \u65b9\u6cd52\uff1a\u30e1\u30e2\u5316\u63a2\u7d22","text":"

        \u30b0\u30ea\u30c3\u30c9 grid \u3068\u540c\u3058\u30b5\u30a4\u30ba\u306e\u30e1\u30e2\u30ea\u30b9\u30c8 mem \u3092\u5c0e\u5165\u3057\u3001\u69d8\u3005\u306a\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u8a18\u9332\u3057\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u3092\u679d\u5208\u308a\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
        def min_path_sum_dfs_mem(\n    grid: list[list[int]], mem: list[list[int]], i: int, j: int\n) -> int:\n    \"\"\"\u6700\u5c0f\u30d1\u30b9\u548c\uff1a\u8a18\u61b6\u5316\u63a2\u7d22\"\"\"\n    # \u5de6\u4e0a\u306e\u30bb\u30eb\u306e\u5834\u5408\u3001\u63a2\u7d22\u3092\u7d42\u4e86\n    if i == 0 and j == 0:\n        return grid[0][0]\n    # \u884c\u307e\u305f\u306f\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001+\u221e \u30b3\u30b9\u30c8\u3092\u8fd4\u3059\n    if i < 0 or j < 0:\n        return inf\n    # \u8a18\u9332\u304c\u3042\u308b\u5834\u5408\u3001\u305d\u308c\u3092\u8fd4\u3059\n    if mem[i][j] != -1:\n        return mem[i][j]\n    # \u5de6\u3068\u4e0a\u306e\u30bb\u30eb\u304b\u3089\u306e\u6700\u5c0f\u30d1\u30b9\u30b3\u30b9\u30c8\n    up = min_path_sum_dfs_mem(grid, mem, i - 1, j)\n    left = min_path_sum_dfs_mem(grid, mem, i, j - 1)\n    # \u5de6\u4e0a\u304b\u3089 (i, j) \u3078\u306e\u6700\u5c0f\u30d1\u30b9\u30b3\u30b9\u30c8\u3092\u8a18\u9332\u3057\u3066\u8fd4\u3059\n    mem[i][j] = min(left, up) + grid[i][j]\n    return mem[i][j]\n
        min_path_sum.cpp
        [class]{}-[func]{minPathSumDFSMem}\n
        min_path_sum.java
        /* \u6700\u5c0f\u30d1\u30b9\u548c\uff1a\u30e1\u30e2\u5316\u63a2\u7d22 */\nint minPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n    // \u5de6\u4e0a\u306e\u30bb\u30eb\u306e\u5834\u5408\u3001\u63a2\u7d22\u3092\u7d42\u4e86\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u884c\u307e\u305f\u306f\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001+\u221e \u306e\u30b3\u30b9\u30c8\u3092\u8fd4\u3059\n    if (i < 0 || j < 0) {\n        return Integer.MAX_VALUE;\n    }\n    // \u8a18\u9332\u304c\u3042\u308b\u5834\u5408\u3001\u305d\u308c\u3092\u8fd4\u3059\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // \u5de6\u3068\u4e0a\u306e\u30bb\u30eb\u304b\u3089\u306e\u6700\u5c0f\u30d1\u30b9\u30b3\u30b9\u30c8\n    int up = minPathSumDFSMem(grid, mem, i - 1, j);\n    int left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // \u5de6\u4e0a\u304b\u3089 (i, j) \u3078\u306e\u6700\u5c0f\u30d1\u30b9\u30b3\u30b9\u30c8\u3092\u8a18\u9332\u3057\u3066\u8fd4\u3059\n    mem[i][j] = Math.min(left, up) + grid[i][j];\n    return mem[i][j];\n}\n
        min_path_sum.cs
        [class]{min_path_sum}-[func]{MinPathSumDFSMem}\n
        min_path_sum.go
        [class]{}-[func]{minPathSumDFSMem}\n
        min_path_sum.swift
        [class]{}-[func]{minPathSumDFSMem}\n
        min_path_sum.js
        [class]{}-[func]{minPathSumDFSMem}\n
        min_path_sum.ts
        [class]{}-[func]{minPathSumDFSMem}\n
        min_path_sum.dart
        [class]{}-[func]{minPathSumDFSMem}\n
        min_path_sum.rs
        [class]{}-[func]{min_path_sum_dfs_mem}\n
        min_path_sum.c
        [class]{}-[func]{minPathSumDFSMem}\n
        min_path_sum.kt
        [class]{}-[func]{minPathSumDFSMem}\n
        min_path_sum.rb
        [class]{}-[func]{min_path_sum_dfs_mem}\n

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30e1\u30e2\u5316\u3092\u5c0e\u5165\u3057\u305f\u5f8c\u3001\u3059\u3079\u3066\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u306f\u4e00\u5ea6\u3060\u3051\u8a08\u7b97\u3055\u308c\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\u72b6\u614b\u306e\u7dcf\u6570\u3001\u3064\u307e\u308a\u30b0\u30ea\u30c3\u30c9\u30b5\u30a4\u30ba \\(O(nm)\\) \u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002

        \u56f3 14-15 \u00a0 \u30e1\u30e2\u5316\u63a2\u7d22\u306e\u518d\u5e30\u6728

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#3-3","title":"3. \u00a0 \u65b9\u6cd53\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0","text":"

        \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u89e3\u6cd5\u3092\u53cd\u5fa9\u7684\u306b\u5b9f\u88c5\u3057\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
        def min_path_sum_dp(grid: list[list[int]]) -> int:\n    \"\"\"\u6700\u5c0f\u30d1\u30b9\u548c\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n, m = len(grid), len(grid[0])\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    dp = [[0] * m for _ in range(n)]\n    dp[0][0] = grid[0][0]\n    # \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\n    for j in range(1, m):\n        dp[0][j] = dp[0][j - 1] + grid[0][j]\n    # \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u5217\n    for i in range(1, n):\n        dp[i][0] = dp[i - 1][0] + grid[i][0]\n    # \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\u3068\u5217\n    for i in range(1, n):\n        for j in range(1, m):\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n    return dp[n - 1][m - 1]\n
        min_path_sum.cpp
        /* \u6700\u5c0f\u30d1\u30b9\u548c\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint minPathSumDP(vector<vector<int>> &grid) {\n    int n = grid.size(), m = grid[0].size();\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    vector<vector<int>> dp(n, vector<int>(m));\n    dp[0][0] = grid[0][0];\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\n    for (int j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u5217\n    for (int i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\u3068\u5217\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
        min_path_sum.java
        /* \u6700\u5c0f\u30d1\u30b9\u548c\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint minPathSumDP(int[][] grid) {\n    int n = grid.length, m = grid[0].length;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    int[][] dp = new int[n][m];\n    dp[0][0] = grid[0][0];\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\n    for (int j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u5217\n    for (int i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\u3068\u5217\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
        min_path_sum.cs
        [class]{min_path_sum}-[func]{MinPathSumDP}\n
        min_path_sum.go
        [class]{}-[func]{minPathSumDP}\n
        min_path_sum.swift
        [class]{}-[func]{minPathSumDP}\n
        min_path_sum.js
        [class]{}-[func]{minPathSumDP}\n
        min_path_sum.ts
        [class]{}-[func]{minPathSumDP}\n
        min_path_sum.dart
        [class]{}-[func]{minPathSumDP}\n
        min_path_sum.rs
        [class]{}-[func]{min_path_sum_dp}\n
        min_path_sum.c
        [class]{}-[func]{minPathSumDP}\n
        min_path_sum.kt
        [class]{}-[func]{minPathSumDP}\n
        min_path_sum.rb
        [class]{}-[func]{min_path_sum_dp}\n

        \u4e0b\u306e\u56f3\u306f\u6700\u5c0f\u7d4c\u8def\u548c\u306e\u72b6\u614b\u9077\u79fb\u30d7\u30ed\u30bb\u30b9\u3092\u793a\u3057\u3001\u30b0\u30ea\u30c3\u30c9\u5168\u4f53\u3092\u8d70\u67fb\u3059\u308b\u305f\u3081\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(nm)\\) \u3067\u3059\u3002

        \u914d\u5217 dp \u306e\u30b5\u30a4\u30ba\u306f \\(n \\times m\\) \u3067\u3042\u308b\u305f\u3081\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(nm)\\) \u3067\u3059\u3002

        <1><2><3><4><5><6><7><8><9><10><11><12>

        \u56f3 14-16 \u00a0 \u6700\u5c0f\u7d4c\u8def\u548c\u306e\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#4","title":"4. \u00a0 \u7a7a\u9593\u6700\u9069\u5316","text":"

        \u5404\u30bb\u30eb\u306f\u5de6\u3068\u4e0a\u306e\u30bb\u30eb\u306e\u307f\u306b\u95a2\u9023\u3059\u308b\u305f\u3081\u3001\u5358\u4e00\u884c\u914d\u5217\u3092\u4f7f\u7528\u3057\u3066 \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002

        \u914d\u5217 dp \u306f1\u884c\u306e\u72b6\u614b\u306e\u307f\u3092\u8868\u73fe\u3067\u304d\u308b\u305f\u3081\u3001\u6700\u521d\u306e\u5217\u306e\u72b6\u614b\u3092\u4e8b\u524d\u306b\u521d\u671f\u5316\u3067\u304d\u305a\u3001\u5404\u884c\u3092\u8d70\u67fb\u3059\u308b\u3068\u304d\u306b\u66f4\u65b0\u3059\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
        def min_path_sum_dp_comp(grid: list[list[int]]) -> int:\n    \"\"\"\u6700\u5c0f\u30d1\u30b9\u548c\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n, m = len(grid), len(grid[0])\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    dp = [0] * m\n    # \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\n    dp[0] = grid[0][0]\n    for j in range(1, m):\n        dp[j] = dp[j - 1] + grid[0][j]\n    # \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\n    for i in range(1, n):\n        # \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u5217\n        dp[0] = dp[0] + grid[i][0]\n        # \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u5217\n        for j in range(1, m):\n            dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n    return dp[m - 1]\n
        min_path_sum.cpp
        [class]{}-[func]{minPathSumDPComp}\n
        min_path_sum.java
        /* \u6700\u5c0f\u30d1\u30b9\u548c\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint minPathSumDPComp(int[][] grid) {\n    int n = grid.length, m = grid[0].length;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    int[] dp = new int[m];\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\n    dp[0] = grid[0][0];\n    for (int j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\n    for (int i = 1; i < n; i++) {\n        // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u5217\n        dp[0] = dp[0] + grid[i][0];\n        // \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u5217\n        for (int j = 1; j < m; j++) {\n            dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
        min_path_sum.cs
        [class]{min_path_sum}-[func]{MinPathSumDPComp}\n
        min_path_sum.go
        [class]{}-[func]{minPathSumDPComp}\n
        min_path_sum.swift
        [class]{}-[func]{minPathSumDPComp}\n
        min_path_sum.js
        [class]{}-[func]{minPathSumDPComp}\n
        min_path_sum.ts
        [class]{}-[func]{minPathSumDPComp}\n
        min_path_sum.dart
        [class]{}-[func]{minPathSumDPComp}\n
        min_path_sum.rs
        [class]{}-[func]{min_path_sum_dp_comp}\n
        min_path_sum.c
        [class]{}-[func]{minPathSumDPComp}\n
        min_path_sum.kt
        [class]{}-[func]{minPathSumDPComp}\n
        min_path_sum.rb
        [class]{}-[func]{min_path_sum_dp_comp}\n
        "},{"location":"chapter_dynamic_programming/edit_distance_problem/","title":"14.6 \u00a0 \u7de8\u96c6\u8ddd\u96e2\u554f\u984c","text":"

        \u7de8\u96c6\u8ddd\u96e2\u306f\u3001\u30ec\u30fc\u30d9\u30f3\u30b7\u30e5\u30bf\u30a4\u30f3\u8ddd\u96e2\u3068\u3082\u547c\u3070\u308c\u3001\u4e00\u3064\u306e\u6587\u5b57\u5217\u3092\u5225\u306e\u6587\u5b57\u5217\u306b\u5909\u63db\u3059\u308b\u305f\u3081\u306b\u5fc5\u8981\u306a\u6700\u5c0f\u4fee\u6b63\u56de\u6570\u3092\u6307\u3057\u3001\u60c5\u5831\u691c\u7d22\u3084\u81ea\u7136\u8a00\u8a9e\u51e6\u7406\u30672\u3064\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u9593\u306e\u985e\u4f3c\u5ea6\u3092\u6e2c\u5b9a\u3059\u308b\u305f\u3081\u306b\u3088\u304f\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002

        Question

        2\u3064\u306e\u6587\u5b57\u5217 \\(s\\) \u3068 \\(t\\) \u304c\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001\\(s\\) \u3092 \\(t\\) \u306b\u5909\u63db\u3059\u308b\u305f\u3081\u306b\u5fc5\u8981\u306a\u6700\u5c0f\u7de8\u96c6\u56de\u6570\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u6587\u5b57\u5217\u306b\u5bfe\u3057\u30663\u7a2e\u985e\u306e\u7de8\u96c6\u3092\u5b9f\u884c\u3067\u304d\u307e\u3059\uff1a\u6587\u5b57\u306e\u633f\u5165\u3001\u6587\u5b57\u306e\u524a\u9664\u3001\u307e\u305f\u306f\u6587\u5b57\u3092\u4ed6\u306e\u4efb\u610f\u306e\u6587\u5b57\u306b\u7f6e\u63db\u3002

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001kitten \u3092 sitting \u306b\u5909\u63db\u3059\u308b\u306b\u306f3\u56de\u306e\u7de8\u96c6\u304c\u5fc5\u8981\u3067\u30012\u56de\u306e\u7f6e\u63db\u30681\u56de\u306e\u633f\u5165\u3092\u542b\u307f\u307e\u3059\u3002hello \u3092 algo \u306b\u5909\u63db\u3059\u308b\u306b\u306f3\u30b9\u30c6\u30c3\u30d7\u304c\u5fc5\u8981\u3067\u30012\u56de\u306e\u7f6e\u63db\u30681\u56de\u306e\u524a\u9664\u3092\u542b\u307f\u307e\u3059\u3002

        \u56f3 14-27 \u00a0 \u7de8\u96c6\u8ddd\u96e2\u306e\u4f8b\u30c7\u30fc\u30bf

        \u7de8\u96c6\u8ddd\u96e2\u554f\u984c\u306f\u6c7a\u5b9a\u6728\u30e2\u30c7\u30eb\u3067\u81ea\u7136\u306b\u8aac\u660e\u3067\u304d\u307e\u3059\u3002\u6587\u5b57\u5217\u306f\u6728\u306e\u30ce\u30fc\u30c9\u306b\u5bfe\u5fdc\u3057\u30011\u30e9\u30a6\u30f3\u30c9\u306e\u6c7a\u5b9a\uff08\u7de8\u96c6\u64cd\u4f5c\uff09\u306f\u6728\u306e\u30a8\u30c3\u30b8\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u64cd\u4f5c\u306b\u5236\u9650\u304c\u306a\u3044\u5834\u5408\u3001\u5404\u30ce\u30fc\u30c9\u306f\u591a\u304f\u306e\u30a8\u30c3\u30b8\u3092\u5c0e\u51fa\u3067\u304d\u3001\u305d\u308c\u305e\u308c\u304c1\u3064\u306e\u64cd\u4f5c\u306b\u5bfe\u5fdc\u3059\u308b\u305f\u3081\u3001hello \u3092 algo \u306b\u5909\u63db\u3059\u308b\u53ef\u80fd\u306a\u7d4c\u8def\u306f\u591a\u6570\u3042\u308a\u307e\u3059\u3002

        \u6c7a\u5b9a\u6728\u306e\u89b3\u70b9\u304b\u3089\u3001\u3053\u306e\u554f\u984c\u306e\u76ee\u6a19\u306f\u3001\u30ce\u30fc\u30c9 hello \u3068\u30ce\u30fc\u30c9 algo \u306e\u9593\u306e\u6700\u77ed\u7d4c\u8def\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3067\u3059\u3002

        \u56f3 14-28 \u00a0 \u6c7a\u5b9a\u6728\u30e2\u30c7\u30eb\u306b\u57fa\u3065\u3044\u3066\u8868\u73fe\u3055\u308c\u305f\u7de8\u96c6\u8ddd\u96e2\u554f\u984c

        "},{"location":"chapter_dynamic_programming/edit_distance_problem/#1","title":"1. \u00a0 \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30a2\u30d7\u30ed\u30fc\u30c1","text":"

        \u30b9\u30c6\u30c3\u30d71\uff1a\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u6c7a\u5b9a\u3092\u8003\u3048\u3001\u72b6\u614b\u3092\u5b9a\u7fa9\u3057\u3001\u305d\u308c\u306b\u3088\u308a \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u3092\u5f97\u308b

        \u5404\u30e9\u30a6\u30f3\u30c9\u306e\u6c7a\u5b9a\u306f\u3001\u6587\u5b57\u5217 \\(s\\) \u306b\u5bfe\u3057\u30661\u3064\u306e\u7de8\u96c6\u64cd\u4f5c\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u3092\u542b\u307f\u307e\u3059\u3002

        \u7de8\u96c6\u30d7\u30ed\u30bb\u30b9\u4e2d\u306b\u554f\u984c\u306e\u30b5\u30a4\u30ba\u3092\u6bb5\u968e\u7684\u306b\u7e2e\u5c0f\u3059\u308b\u3053\u3068\u3092\u76ee\u6307\u3057\u3001\u3053\u308c\u306b\u3088\u308a\u90e8\u5206\u554f\u984c\u3092\u69cb\u7bc9\u3067\u304d\u307e\u3059\u3002\u6587\u5b57\u5217 \\(s\\) \u3068 \\(t\\) \u306e\u9577\u3055\u3092\u305d\u308c\u305e\u308c \\(n\\) \u3068 \\(m\\) \u3068\u3057\u307e\u3059\u3002\u307e\u305a\u3001\u4e21\u65b9\u306e\u6587\u5b57\u5217\u306e\u672b\u5c3e\u6587\u5b57 \\(s[n-1]\\) \u3068 \\(t[m-1]\\) \u3092\u8003\u616e\u3057\u307e\u3059\u3002

        • \\(s[n-1]\\) \u3068 \\(t[m-1]\\) \u304c\u540c\u3058\u5834\u5408\u3001\u305d\u308c\u3089\u3092\u30b9\u30ad\u30c3\u30d7\u3057\u3066\u76f4\u63a5 \\(s[n-2]\\) \u3068 \\(t[m-2]\\) \u3092\u8003\u616e\u3067\u304d\u307e\u3059\u3002
        • \\(s[n-1]\\) \u3068 \\(t[m-1]\\) \u304c\u7570\u306a\u308b\u5834\u5408\u3001\\(s\\) \u306b\u5bfe\u3057\u30661\u3064\u306e\u7de8\u96c6\uff08\u633f\u5165\u3001\u524a\u9664\u3001\u7f6e\u63db\uff09\u3092\u5b9f\u884c\u3057\u3066\u30012\u3064\u306e\u6587\u5b57\u5217\u306e\u672b\u5c3e\u6587\u5b57\u3092\u4e00\u81f4\u3055\u305b\u3001\u305d\u308c\u3089\u3092\u30b9\u30ad\u30c3\u30d7\u3057\u3066\u3088\u308a\u5c0f\u898f\u6a21\u306a\u554f\u984c\u3092\u8003\u616e\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u3057\u305f\u304c\u3063\u3066\u3001\u6587\u5b57\u5217 \\(s\\) \u3067\u306e\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u6c7a\u5b9a\uff08\u7de8\u96c6\u64cd\u4f5c\uff09\u306f\u3001\\(s\\) \u3068 \\(t\\) \u3067\u30de\u30c3\u30c1\u3055\u308c\u308b\u6b8b\u308a\u306e\u6587\u5b57\u3092\u5909\u66f4\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u72b6\u614b\u306f \\(s\\) \u3068 \\(t\\) \u3067\u73fe\u5728\u8003\u616e\u3055\u308c\u3066\u3044\u308b \\(i\\) \u756a\u76ee\u3068 \\(j\\) \u756a\u76ee\u306e\u6587\u5b57\u3067\u3042\u308a\u3001\\([i, j]\\) \u3068\u8868\u8a18\u3055\u308c\u307e\u3059\u3002

        \u72b6\u614b \\([i, j]\\) \u306f\u90e8\u5206\u554f\u984c\u306b\u5bfe\u5fdc\u3057\u307e\u3059\uff1a\\(s\\) \u306e\u6700\u521d\u306e \\(i\\) \u6587\u5b57\u3092 \\(t\\) \u306e\u6700\u521d\u306e \\(j\\) \u6587\u5b57\u306b\u5909\u66f4\u3059\u308b\u305f\u3081\u306b\u5fc5\u8981\u306a\u6700\u5c0f\u7de8\u96c6\u56de\u6570\u3002

        \u3053\u308c\u304b\u3089\u3001\u30b5\u30a4\u30ba \\((i+1) \\times (j+1)\\) \u306e\u4e8c\u6b21\u5143 \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u3092\u5f97\u307e\u3059\u3002

        \u30b9\u30c6\u30c3\u30d72\uff1a\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u7279\u5b9a\u3057\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3092\u5c0e\u51fa\u3059\u308b

        \u90e8\u5206\u554f\u984c \\(dp[i, j]\\) \u3092\u8003\u616e\u3059\u308b\u3068\u3001\u3053\u308c\u306b\u5bfe\u5fdc\u3059\u308b2\u3064\u306e\u6587\u5b57\u5217\u306e\u672b\u5c3e\u6587\u5b57\u306f \\(s[i-1]\\) \u3068 \\(t[j-1]\\) \u3067\u3042\u308a\u3001\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b3\u3064\u306e\u30b7\u30ca\u30ea\u30aa\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        1. \\(s[i-1]\\) \u306e\u5f8c\u306b \\(t[j-1]\\) \u3092\u8ffd\u52a0\u3059\u308b\u3068\u3001\u6b8b\u308a\u306e\u90e8\u5206\u554f\u984c\u306f \\(dp[i, j-1]\\) \u3067\u3059\u3002
        2. \\(s[i-1]\\) \u3092\u524a\u9664\u3059\u308b\u3068\u3001\u6b8b\u308a\u306e\u90e8\u5206\u554f\u984c\u306f \\(dp[i-1, j]\\) \u3067\u3059\u3002
        3. \\(s[i-1]\\) \u3092 \\(t[j-1]\\) \u306b\u7f6e\u63db\u3059\u308b\u3068\u3001\u6b8b\u308a\u306e\u90e8\u5206\u554f\u984c\u306f \\(dp[i-1, j-1]\\) \u3067\u3059\u3002

        \u56f3 14-29 \u00a0 \u7de8\u96c6\u8ddd\u96e2\u306e\u72b6\u614b\u9077\u79fb

        \u4e0a\u8a18\u306e\u5206\u6790\u306b\u57fa\u3065\u3044\u3066\u3001\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u6c7a\u5b9a\u3067\u304d\u307e\u3059\uff1a\\(dp[i, j]\\) \u306e\u6700\u5c0f\u7de8\u96c6\u56de\u6570\u306f\u3001\\(dp[i, j-1]\\)\u3001\\(dp[i-1, j]\\)\u3001\\(dp[i-1, j-1]\\) \u306e\u4e2d\u306e\u6700\u5c0f\u5024\u306b\u7de8\u96c6\u30b9\u30c6\u30c3\u30d7 \\(1\\) \u3092\u52a0\u3048\u305f\u3082\u306e\u3067\u3059\u3002\u5bfe\u5fdc\u3059\u308b\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u306f\uff1a

        \\[ dp[i, j] = \\min(dp[i, j-1], dp[i-1, j], dp[i-1, j-1]) + 1 \\]

        \u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\\(s[i-1]\\) \u3068 \\(t[j-1]\\) \u304c\u540c\u3058\u5834\u5408\u3001\u73fe\u5728\u306e\u6587\u5b57\u306b\u5bfe\u3057\u3066\u7de8\u96c6\u306f\u5fc5\u8981\u3042\u308a\u307e\u305b\u3093\u3002\u3053\u306e\u5834\u5408\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u306f\uff1a

        \\[ dp[i, j] = dp[i-1, j-1] \\]

        \u30b9\u30c6\u30c3\u30d73\uff1a\u5883\u754c\u6761\u4ef6\u3068\u72b6\u614b\u9077\u79fb\u306e\u9806\u5e8f\u3092\u6c7a\u5b9a\u3059\u308b

        \u4e21\u65b9\u306e\u6587\u5b57\u5217\u304c\u7a7a\u306e\u5834\u5408\u3001\u7de8\u96c6\u56de\u6570\u306f \\(0\\) \u3067\u3059\u3002\u3064\u307e\u308a\u3001\\(dp[0, 0] = 0\\) \u3067\u3059\u3002\\(s\\) \u304c\u7a7a\u3067 \\(t\\) \u304c\u7a7a\u3067\u306a\u3044\u5834\u5408\u3001\u6700\u5c0f\u7de8\u96c6\u56de\u6570\u306f \\(t\\) \u306e\u9577\u3055\u306b\u7b49\u3057\u304f\u3001\u3064\u307e\u308a\u6700\u521d\u306e\u884c \\(dp[0, j] = j\\) \u3067\u3059\u3002\\(s\\) \u304c\u7a7a\u3067\u306a\u304f \\(t\\) \u304c\u7a7a\u306e\u5834\u5408\u3001\u6700\u5c0f\u7de8\u96c6\u56de\u6570\u306f \\(s\\) \u306e\u9577\u3055\u306b\u7b49\u3057\u304f\u3001\u3064\u307e\u308a\u6700\u521d\u306e\u5217 \\(dp[i, 0] = i\\) \u3067\u3059\u3002

        \u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\\(dp[i, j]\\) \u306e\u89e3\u6c7a\u306f\u5de6\u3001\u4e0a\u3001\u5de6\u4e0a\u306e\u89e3\u306b\u4f9d\u5b58\u3059\u308b\u305f\u3081\u3001\u4e8c\u91cd\u30eb\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u3066\u6b63\u3057\u3044\u9806\u5e8f\u3067 \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u5168\u4f53\u3092\u8d70\u67fb\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_dynamic_programming/edit_distance_problem/#2","title":"2. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby edit_distance.py
        def edit_distance_dp(s: str, t: str) -> int:\n    \"\"\"\u7de8\u96c6\u8ddd\u96e2\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n, m = len(s), len(t)\n    dp = [[0] * (m + 1) for _ in range(n + 1)]\n    # \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\u3068\u6700\u521d\u306e\u5217\n    for i in range(1, n + 1):\n        dp[i][0] = i\n    for j in range(1, m + 1):\n        dp[0][j] = j\n    # \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\u3068\u5217\n    for i in range(1, n + 1):\n        for j in range(1, m + 1):\n            if s[i - 1] == t[j - 1]:\n                # 2 \u3064\u306e\u6587\u5b57\u304c\u7b49\u3057\u3044\u5834\u5408\u3001\u3053\u308c\u3089 2 \u3064\u306e\u6587\u5b57\u3092\u30b9\u30ad\u30c3\u30d7\n                dp[i][j] = dp[i - 1][j - 1]\n            else:\n                # \u6700\u5c0f\u7de8\u96c6\u6570 = 3 \u3064\u306e\u64cd\u4f5c\uff08\u633f\u5165\u3001\u524a\u9664\u3001\u7f6e\u63db\uff09\u304b\u3089\u306e\u6700\u5c0f\u7de8\u96c6\u6570 + 1\n                dp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1\n    return dp[n][m]\n
        edit_distance.cpp
        /* \u7de8\u96c6\u8ddd\u96e2\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint editDistanceDP(string s, string t) {\n    int n = s.length(), m = t.length();\n    vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\u3068\u6700\u521d\u306e\u5217\n    for (int i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\u3068\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s[i - 1] == t[j - 1]) {\n                // 2\u3064\u306e\u6587\u5b57\u304c\u7b49\u3057\u3044\u5834\u5408\u3001\u3053\u308c\u30892\u3064\u306e\u6587\u5b57\u3092\u30b9\u30ad\u30c3\u30d7\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // \u6700\u5c0f\u7de8\u96c6\u6570 = 3\u3064\u306e\u64cd\u4f5c\uff08\u633f\u5165\u3001\u524a\u9664\u3001\u7f6e\u63db\uff09\u304b\u3089\u306e\u6700\u5c0f\u7de8\u96c6\u6570 + 1\n                dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
        edit_distance.java
        /* \u7de8\u96c6\u8ddd\u96e2\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint editDistanceDP(String s, String t) {\n    int n = s.length(), m = t.length();\n    int[][] dp = new int[n + 1][m + 1];\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\u3068\u6700\u521d\u306e\u5217\n    for (int i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\u3068\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s.charAt(i - 1) == t.charAt(j - 1)) {\n                // 2\u3064\u306e\u6587\u5b57\u304c\u7b49\u3057\u3044\u5834\u5408\u3001\u3053\u308c\u30892\u3064\u306e\u6587\u5b57\u3092\u30b9\u30ad\u30c3\u30d7\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // \u6700\u5c0f\u7de8\u96c6\u6570 = 3\u3064\u306e\u64cd\u4f5c\uff08\u633f\u5165\u3001\u524a\u9664\u3001\u7f6e\u63db\uff09\u304b\u3089\u306e\u6700\u5c0f\u7de8\u96c6\u6570 + 1\n                dp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
        edit_distance.cs
        [class]{edit_distance}-[func]{EditDistanceDP}\n
        edit_distance.go
        [class]{}-[func]{editDistanceDP}\n
        edit_distance.swift
        [class]{}-[func]{editDistanceDP}\n
        edit_distance.js
        [class]{}-[func]{editDistanceDP}\n
        edit_distance.ts
        [class]{}-[func]{editDistanceDP}\n
        edit_distance.dart
        [class]{}-[func]{editDistanceDP}\n
        edit_distance.rs
        [class]{}-[func]{edit_distance_dp}\n
        edit_distance.c
        [class]{}-[func]{editDistanceDP}\n
        edit_distance.kt
        [class]{}-[func]{editDistanceDP}\n
        edit_distance.rb
        [class]{}-[func]{edit_distance_dp}\n

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u7de8\u96c6\u8ddd\u96e2\u554f\u984c\u306e\u72b6\u614b\u9077\u79fb\u30d7\u30ed\u30bb\u30b9\u306f\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3068\u975e\u5e38\u306b\u4f3c\u3066\u304a\u308a\u3001\u4e8c\u6b21\u5143\u30b0\u30ea\u30c3\u30c9\u3092\u57cb\u3081\u308b\u3053\u3068\u3068\u898b\u306a\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        <1><2><3><4><5><6><7><8><9><10><11><12><13><14><15>

        \u56f3 14-30 \u00a0 \u7de8\u96c6\u8ddd\u96e2\u306e\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9

        "},{"location":"chapter_dynamic_programming/edit_distance_problem/#3","title":"3. \u00a0 \u7a7a\u9593\u6700\u9069\u5316","text":"

        \\(dp[i, j]\\) \u306f\u4e0a\u306e \\(dp[i-1, j]\\)\u3001\u5de6\u306e \\(dp[i, j-1]\\)\u3001\u5de6\u4e0a\u306e \\(dp[i-1, j-1]\\) \u306e\u89e3\u304b\u3089\u5c0e\u51fa\u3055\u308c\u3001\u76f4\u63a5\u8d70\u67fb\u3067\u306f\u5de6\u4e0a\u306e\u89e3 \\(dp[i-1, j-1]\\) \u304c\u5931\u308f\u308c\u3001\u9006\u8d70\u67fb\u3067\u306f\u4e8b\u524d\u306b \\(dp[i, j-1]\\) \u3092\u69cb\u7bc9\u3067\u304d\u306a\u3044\u305f\u3081\u3001\u3069\u3061\u3089\u306e\u8d70\u67fb\u9806\u5e8f\u3082\u5b9f\u884c\u53ef\u80fd\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002

        \u3053\u306e\u7406\u7531\u3067\u3001\u5909\u6570 leftup \u3092\u4f7f\u7528\u3057\u3066\u5de6\u4e0a\u306e \\(dp[i-1, j-1]\\) \u304b\u3089\u306e\u89e3\u3092\u4e00\u6642\u7684\u306b\u4fdd\u5b58\u3057\u3001\u5de6\u3068\u4e0a\u306e\u89e3\u306e\u307f\u3092\u8003\u616e\u3059\u308c\u3070\u3088\u304f\u306a\u308a\u307e\u3059\u3002\u3053\u306e\u72b6\u6cc1\u306f\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3068\u4f3c\u3066\u304a\u308a\u3001\u76f4\u63a5\u8d70\u67fb\u304c\u53ef\u80fd\u3067\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby edit_distance.py
        def edit_distance_dp_comp(s: str, t: str) -> int:\n    \"\"\"\u7de8\u96c6\u8ddd\u96e2\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n, m = len(s), len(t)\n    dp = [0] * (m + 1)\n    # \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\n    for j in range(1, m + 1):\n        dp[j] = j\n    # \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\n    for i in range(1, n + 1):\n        # \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u5217\n        leftup = dp[0]  # dp[i-1, j-1] \u3092\u4e00\u6642\u7684\u306b\u4fdd\u5b58\n        dp[0] += 1\n        # \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u5217\n        for j in range(1, m + 1):\n            temp = dp[j]\n            if s[i - 1] == t[j - 1]:\n                # 2 \u3064\u306e\u6587\u5b57\u304c\u7b49\u3057\u3044\u5834\u5408\u3001\u3053\u308c\u3089 2 \u3064\u306e\u6587\u5b57\u3092\u30b9\u30ad\u30c3\u30d7\n                dp[j] = leftup\n            else:\n                # \u6700\u5c0f\u7de8\u96c6\u6570 = 3 \u3064\u306e\u64cd\u4f5c\uff08\u633f\u5165\u3001\u524a\u9664\u3001\u7f6e\u63db\uff09\u304b\u3089\u306e\u6700\u5c0f\u7de8\u96c6\u6570 + 1\n                dp[j] = min(dp[j - 1], dp[j], leftup) + 1\n            leftup = temp  # \u6b21\u306e dp[i-1, j-1] \u306e\u305f\u3081\u306b\u66f4\u65b0\n    return dp[m]\n
        edit_distance.cpp
        [class]{}-[func]{editDistanceDPComp}\n
        edit_distance.java
        /* \u7de8\u96c6\u8ddd\u96e2\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint editDistanceDPComp(String s, String t) {\n    int n = s.length(), m = t.length();\n    int[] dp = new int[m + 1];\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\n    for (int j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\n    for (int i = 1; i <= n; i++) {\n        // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u5217\n        int leftup = dp[0]; // dp[i-1, j-1] \u3092\u4e00\u6642\u7684\u306b\u683c\u7d0d\n        dp[0] = i;\n        // \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u5217\n        for (int j = 1; j <= m; j++) {\n            int temp = dp[j];\n            if (s.charAt(i - 1) == t.charAt(j - 1)) {\n                // 2\u3064\u306e\u6587\u5b57\u304c\u7b49\u3057\u3044\u5834\u5408\u3001\u3053\u308c\u30892\u3064\u306e\u6587\u5b57\u3092\u30b9\u30ad\u30c3\u30d7\n                dp[j] = leftup;\n            } else {\n                // \u6700\u5c0f\u7de8\u96c6\u6570 = 3\u3064\u306e\u64cd\u4f5c\uff08\u633f\u5165\u3001\u524a\u9664\u3001\u7f6e\u63db\uff09\u304b\u3089\u306e\u6700\u5c0f\u7de8\u96c6\u6570 + 1\n                dp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e dp[i-1, j-1] \u306e\u305f\u3081\u306b\u66f4\u65b0\n        }\n    }\n    return dp[m];\n}\n
        edit_distance.cs
        [class]{edit_distance}-[func]{EditDistanceDPComp}\n
        edit_distance.go
        [class]{}-[func]{editDistanceDPComp}\n
        edit_distance.swift
        [class]{}-[func]{editDistanceDPComp}\n
        edit_distance.js
        [class]{}-[func]{editDistanceDPComp}\n
        edit_distance.ts
        [class]{}-[func]{editDistanceDPComp}\n
        edit_distance.dart
        [class]{}-[func]{editDistanceDPComp}\n
        edit_distance.rs
        [class]{}-[func]{edit_distance_dp_comp}\n
        edit_distance.c
        [class]{}-[func]{editDistanceDPComp}\n
        edit_distance.kt
        [class]{}-[func]{editDistanceDPComp}\n
        edit_distance.rb
        [class]{}-[func]{edit_distance_dp_comp}\n
        "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/","title":"14.1 \u00a0 \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306e\u7d39\u4ecb","text":"

        \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u91cd\u8981\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d1\u30e9\u30c0\u30a4\u30e0\u3067\u3042\u308a\u3001\u554f\u984c\u3092\u4e00\u9023\u306e\u5c0f\u3055\u306a\u90e8\u5206\u554f\u984c\u306b\u5206\u89e3\u3057\u3001\u3053\u308c\u3089\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4fdd\u5b58\u3059\u308b\u3053\u3068\u3067\u5197\u9577\u306a\u8a08\u7b97\u3092\u907f\u3051\u3001\u6642\u9593\u52b9\u7387\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002

        \u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u53e4\u5178\u7684\u306a\u554f\u984c\u304b\u3089\u59cb\u3081\u3066\u3001\u307e\u305a\u529b\u4efb\u305b\u306e\u63a2\u7d22\u6cd5\u306b\u3088\u308b\u89e3\u6cd5\u3092\u63d0\u793a\u3057\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u3092\u7279\u5b9a\u3057\u3066\u304b\u3089\u3001\u3088\u308a\u52b9\u7387\u7684\u306a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u89e3\u6cd5\u3092\u6bb5\u968e\u7684\u306b\u5c0e\u51fa\u3057\u307e\u3059\u3002

        \u968e\u6bb5\u767b\u308a

        \\(n\\) \u6bb5\u306e\u968e\u6bb5\u304c\u3042\u308a\u3001\u4e00\u5ea6\u306b \\(1\\) \u6bb5\u307e\u305f\u306f \\(2\\) \u6bb5\u4e0a\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u9802\u4e0a\u306b\u5230\u9054\u3059\u308b\u65b9\u6cd5\u306f\u4f55\u901a\u308a\u3042\u308a\u307e\u3059\u304b\uff1f

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\\(3\\) \u6bb5\u306e\u968e\u6bb5\u306e\u9802\u4e0a\u306b\u5230\u9054\u3059\u308b\u65b9\u6cd5\u306f \\(3\\) \u901a\u308a\u3042\u308a\u307e\u3059\u3002

        \u56f3 14-1 \u00a0 3\u6bb5\u76ee\u306b\u5230\u9054\u3059\u308b\u65b9\u6cd5\u306e\u6570

        \u3053\u306e\u554f\u984c\u306f**\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3092\u7528\u3044\u3066\u3059\u3079\u3066\u306e\u53ef\u80fd\u6027\u3092\u7db2\u7f85**\u3059\u308b\u3053\u3068\u3067\u65b9\u6cd5\u306e\u6570\u3092\u8a08\u7b97\u3059\u308b\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u3066\u3044\u307e\u3059\u3002\u5177\u4f53\u7684\u306b\u306f\u3001\u968e\u6bb5\u767b\u308a\u306e\u554f\u984c\u3092\u591a\u6bb5\u968e\u9078\u629e\u30d7\u30ed\u30bb\u30b9\u3068\u3057\u3066\u8003\u3048\u307e\u3059\uff1a\u5730\u9762\u304b\u3089\u59cb\u3081\u3066\u3001\u6bce\u56de \\(1\\) \u6bb5\u307e\u305f\u306f \\(2\\) \u6bb5\u4e0a\u308b\u304b\u3092\u9078\u629e\u3057\u3001\u968e\u6bb5\u306e\u9802\u4e0a\u306b\u5230\u9054\u3057\u305f\u3089\u65b9\u6cd5\u306e\u6570\u3092\u30ab\u30a6\u30f3\u30c8\u3057\u3001\u9802\u4e0a\u3092\u8d85\u3048\u305f\u5834\u5408\u306f\u30d7\u30eb\u30fc\u30cb\u30f3\u30b0\uff08\u679d\u5208\u308a\uff09\u3092\u884c\u3044\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_backtrack.py
        def backtrack(choices: list[int], state: int, n: int, res: list[int]) -> int:\n    \"\"\"\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\"\"\"\n    # n \u6bb5\u76ee\u306b\u767b\u3063\u305f\u3068\u304d\u3001\u89e3\u306e\u6570\u306b 1 \u3092\u52a0\u3048\u308b\n    if state == n:\n        res[0] += 1\n    # \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for choice in choices:\n        # \u679d\u5208\u308a\uff1an \u6bb5\u3092\u8d85\u3048\u3066\u767b\u308b\u3053\u3068\u3092\u8a31\u53ef\u3057\u306a\u3044\n        if state + choice > n:\n            continue\n        # \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n        backtrack(choices, state + choice, n, res)\n        # \u64a4\u56de\n\ndef climbing_stairs_backtrack(n: int) -> int:\n    \"\"\"\u968e\u6bb5\u767b\u308a\uff1a\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\"\"\"\n    choices = [1, 2]  # 1 \u6bb5\u307e\u305f\u306f 2 \u6bb5\u767b\u308b\u3053\u3068\u3092\u9078\u629e\u53ef\u80fd\n    state = 0  # 0 \u6bb5\u76ee\u304b\u3089\u767b\u308a\u59cb\u3081\u308b\n    res = [0]  # res[0] \u3092\u4f7f\u7528\u3057\u3066\u89e3\u306e\u6570\u3092\u8a18\u9332\n    backtrack(choices, state, n, res)\n    return res[0]\n
        climbing_stairs_backtrack.cpp
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0 */\nvoid backtrack(vector<int> &choices, int state, int n, vector<int> &res) {\n    // n\u6bb5\u76ee\u306b\u5230\u9054\u3057\u305f\u3068\u304d\u3001\u89e3\u306e\u6570\u306b1\u3092\u52a0\u3048\u308b\n    if (state == n)\n        res[0]++;\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for (auto &choice : choices) {\n        // \u526a\u5b9a\uff1an\u6bb5\u3092\u8d85\u3048\u3066\u767b\u308b\u3053\u3068\u3092\u8a31\u53ef\u3057\u306a\u3044\n        if (state + choice > n)\n            continue;\n        // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n        backtrack(choices, state + choice, n, res);\n        // \u64a4\u56de\n    }\n}\n\n/* \u968e\u6bb5\u767b\u308a\uff1a\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0 */\nint climbingStairsBacktrack(int n) {\n    vector<int> choices = {1, 2}; // 1\u6bb5\u307e\u305f\u306f2\u6bb5\u767b\u308b\u3053\u3068\u3092\u9078\u629e\u53ef\u80fd\n    int state = 0;                // 0\u6bb5\u76ee\u304b\u3089\u767b\u308a\u59cb\u3081\u308b\n    vector<int> res = {0};        // res[0] \u3092\u4f7f\u7528\u3057\u3066\u89e3\u306e\u6570\u3092\u8a18\u9332\n    backtrack(choices, state, n, res);\n    return res[0];\n}\n
        climbing_stairs_backtrack.java
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0 */\nvoid backtrack(List<Integer> choices, int state, int n, List<Integer> res) {\n    // n\u6bb5\u76ee\u306b\u5230\u9054\u3057\u305f\u3068\u304d\u3001\u89e3\u306e\u6570\u306b1\u3092\u52a0\u3048\u308b\n    if (state == n)\n        res.set(0, res.get(0) + 1);\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for (Integer choice : choices) {\n        // \u526a\u5b9a\uff1an\u6bb5\u3092\u8d85\u3048\u3066\u767b\u308b\u3053\u3068\u3092\u8a31\u53ef\u3057\u306a\u3044\n        if (state + choice > n)\n            continue;\n        // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n        backtrack(choices, state + choice, n, res);\n        // \u64a4\u56de\n    }\n}\n\n/* \u968e\u6bb5\u767b\u308a\uff1a\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0 */\nint climbingStairsBacktrack(int n) {\n    List<Integer> choices = Arrays.asList(1, 2); // 1\u6bb5\u307e\u305f\u306f2\u6bb5\u767b\u308b\u3053\u3068\u3092\u9078\u629e\u53ef\u80fd\n    int state = 0; // 0\u6bb5\u76ee\u304b\u3089\u767b\u308a\u59cb\u3081\u308b\n    List<Integer> res = new ArrayList<>();\n    res.add(0); // res[0] \u3092\u4f7f\u7528\u3057\u3066\u89e3\u306e\u6570\u3092\u8a18\u9332\n    backtrack(choices, state, n, res);\n    return res.get(0);\n}\n
        climbing_stairs_backtrack.cs
        [class]{climbing_stairs_backtrack}-[func]{Backtrack}\n\n[class]{climbing_stairs_backtrack}-[func]{ClimbingStairsBacktrack}\n
        climbing_stairs_backtrack.go
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{climbingStairsBacktrack}\n
        climbing_stairs_backtrack.swift
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{climbingStairsBacktrack}\n
        climbing_stairs_backtrack.js
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{climbingStairsBacktrack}\n
        climbing_stairs_backtrack.ts
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{climbingStairsBacktrack}\n
        climbing_stairs_backtrack.dart
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{climbingStairsBacktrack}\n
        climbing_stairs_backtrack.rs
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{climbing_stairs_backtrack}\n
        climbing_stairs_backtrack.c
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{climbingStairsBacktrack}\n
        climbing_stairs_backtrack.kt
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{climbingStairsBacktrack}\n
        climbing_stairs_backtrack.rb
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{climbing_stairs_backtrack}\n
        "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1411-1","title":"14.1.1 \u00a0 \u65b9\u6cd51\uff1a\u529b\u4efb\u305b\u63a2\u7d22","text":"

        \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u554f\u984c\u3092\u660e\u793a\u7684\u306b\u90e8\u5206\u554f\u984c\u306b\u5206\u89e3\u3057\u307e\u305b\u3093\u3002\u4ee3\u308f\u308a\u306b\u3001\u554f\u984c\u3092\u4e00\u9023\u306e\u6c7a\u5b9a\u30b9\u30c6\u30c3\u30d7\u3068\u3057\u3066\u6271\u3044\u3001\u8a66\u884c\u3068\u679d\u5208\u308a\u3092\u901a\u3058\u3066\u3059\u3079\u3066\u306e\u53ef\u80fd\u6027\u3092\u63a2\u7d22\u3057\u307e\u3059\u3002

        \u3053\u306e\u554f\u984c\u3092\u5206\u89e3\u30a2\u30d7\u30ed\u30fc\u30c1\u3092\u4f7f\u3063\u3066\u5206\u6790\u3067\u304d\u307e\u3059\u3002\\(dp[i]\\) \u3092 \\(i\\) \u6bb5\u76ee\u306b\u5230\u9054\u3059\u308b\u65b9\u6cd5\u306e\u6570\u3068\u3057\u307e\u3059\u3002\u3053\u306e\u5834\u5408\u3001\\(dp[i]\\) \u304c\u5143\u306e\u554f\u984c\u3067\u3042\u308a\u3001\u305d\u306e\u90e8\u5206\u554f\u984c\u306f\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\uff1a

        \\[ dp[i-1], dp[i-2], \\dots, dp[2], dp[1] \\]

        \u5404\u79fb\u52d5\u306f \\(1\\) \u6bb5\u307e\u305f\u306f \\(2\\) \u6bb5\u3057\u304b\u9032\u3081\u306a\u3044\u305f\u3081\u3001\\(i\\) \u6bb5\u76ee\u306b\u7acb\u3063\u3066\u3044\u308b\u3068\u304d\u3001\u524d\u306e\u30b9\u30c6\u30c3\u30d7\u306f \\(i-1\\) \u6bb5\u76ee\u307e\u305f\u306f \\(i-2\\) \u6bb5\u76ee\u306e\u3044\u305a\u308c\u304b\u306b\u3044\u305f\u306f\u305a\u3067\u3059\u3002\u3064\u307e\u308a\u3001\\(i\\) \u6bb5\u76ee\u306b\u306f \\(i-1\\) \u6bb5\u76ee\u307e\u305f\u306f \\(i-2\\) \u6bb5\u76ee\u304b\u3089\u3057\u304b\u5230\u9054\u3067\u304d\u307e\u305b\u3093\u3002

        \u3053\u308c\u306b\u3088\u308a\u91cd\u8981\u306a\u7d50\u8ad6\u304c\u5f97\u3089\u308c\u307e\u3059\uff1a\\(i-1\\) \u6bb5\u76ee\u306b\u5230\u9054\u3059\u308b\u65b9\u6cd5\u306e\u6570\u306b \\(i-2\\) \u6bb5\u76ee\u306b\u5230\u9054\u3059\u308b\u65b9\u6cd5\u306e\u6570\u3092\u52a0\u3048\u305f\u3082\u306e\u304c\u3001\\(i\\) \u6bb5\u76ee\u306b\u5230\u9054\u3059\u308b\u65b9\u6cd5\u306e\u6570\u306b\u7b49\u3057\u3044\u3002\u5f0f\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        \\[ dp[i] = dp[i-1] + dp[i-2] \\]

        \u3053\u308c\u306f\u3001\u968e\u6bb5\u767b\u308a\u554f\u984c\u306b\u304a\u3044\u3066\u90e8\u5206\u554f\u984c\u9593\u306b\u518d\u5e30\u95a2\u4fc2\u304c\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u3001\u5143\u306e\u554f\u984c\u306e\u89e3\u306f\u90e8\u5206\u554f\u984c\u306e\u89e3\u304b\u3089\u69cb\u7bc9\u3067\u304d\u307e\u3059\u3002\u4e0b\u306e\u56f3\u306f\u3053\u306e\u518d\u5e30\u95a2\u4fc2\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 14-2 \u00a0 \u89e3\u306e\u6570\u306e\u518d\u5e30\u95a2\u4fc2

        \u518d\u5e30\u5f0f\u306b\u5f93\u3063\u3066\u529b\u4efb\u305b\u63a2\u7d22\u89e3\u6cd5\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\\(dp[n]\\) \u304b\u3089\u59cb\u3081\u3066\u3001**\u3088\u308a\u5927\u304d\u306a\u554f\u984c\u3092\u518d\u5e30\u7684\u306b2\u3064\u306e\u5c0f\u3055\u306a\u90e8\u5206\u554f\u984c\u306e\u548c\u306b\u5206\u89e3**\u3057\u3001\u89e3\u304c\u65e2\u77e5\u306e\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c \\(dp[1]\\) \u3068 \\(dp[2]\\) \u306b\u5230\u9054\u3059\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002\\(dp[1] = 1\\) \u3068 \\(dp[2] = 2\\) \u3067\u3001\u305d\u308c\u305e\u308c1\u6bb5\u76ee\u30682\u6bb5\u76ee\u306b\u767b\u308b\u65b9\u6cd5\u304c \\(1\\) \u901a\u308a\u3068 \\(2\\) \u901a\u308a\u3042\u308b\u3053\u3068\u3092\u8868\u3057\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\u6a19\u6e96\u7684\u306a\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30b3\u30fc\u30c9\u3068\u540c\u69d8\u306b\u6df1\u3055\u512a\u5148\u63a2\u7d22\u306b\u5c5e\u3057\u307e\u3059\u304c\u3001\u3088\u308a\u7c21\u6f54\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dfs.py
        def dfs(i: int) -> int:\n    \"\"\"\u63a2\u7d22\"\"\"\n    # \u65e2\u77e5\u306e dp[1] \u3068 dp[2] \u306f\u3001\u305d\u308c\u3089\u3092\u8fd4\u3059\n    if i == 1 or i == 2:\n        return i\n    # dp[i] = dp[i-1] + dp[i-2]\n    count = dfs(i - 1) + dfs(i - 2)\n    return count\n\ndef climbing_stairs_dfs(n: int) -> int:\n    \"\"\"\u968e\u6bb5\u767b\u308a\uff1a\u63a2\u7d22\"\"\"\n    return dfs(n)\n
        climbing_stairs_dfs.cpp
        /* \u63a2\u7d22 */\nint dfs(int i) {\n    // \u65e2\u77e5\u306e dp[1] \u3068 dp[2] \u3092\u8fd4\u3059\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* \u968e\u6bb5\u767b\u308a\uff1a\u63a2\u7d22 */\nint climbingStairsDFS(int n) {\n    return dfs(n);\n}\n
        climbing_stairs_dfs.java
        /* \u63a2\u7d22 */\nint dfs(int i) {\n    // \u65e2\u77e5\u306e dp[1] \u3068 dp[2] \u3092\u8fd4\u3059\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* \u968e\u6bb5\u767b\u308a\uff1a\u63a2\u7d22 */\nint climbingStairsDFS(int n) {\n    return dfs(n);\n}\n
        climbing_stairs_dfs.cs
        [class]{climbing_stairs_dfs}-[func]{DFS}\n\n[class]{climbing_stairs_dfs}-[func]{ClimbingStairsDFS}\n
        climbing_stairs_dfs.go
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFS}\n
        climbing_stairs_dfs.swift
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFS}\n
        climbing_stairs_dfs.js
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFS}\n
        climbing_stairs_dfs.ts
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFS}\n
        climbing_stairs_dfs.dart
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFS}\n
        climbing_stairs_dfs.rs
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbing_stairs_dfs}\n
        climbing_stairs_dfs.c
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFS}\n
        climbing_stairs_dfs.kt
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFS}\n
        climbing_stairs_dfs.rb
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbing_stairs_dfs}\n

        \u4e0b\u306e\u56f3\u306f\u529b\u4efb\u305b\u63a2\u7d22\u306b\u3088\u3063\u3066\u5f62\u6210\u3055\u308c\u308b\u518d\u5e30\u6728\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u554f\u984c \\(dp[n]\\) \u306b\u3064\u3044\u3066\u3001\u305d\u306e\u518d\u5e30\u6728\u306e\u6df1\u3055\u306f \\(n\\) \u3067\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(2^n)\\) \u3067\u3059\u3002\u3053\u306e\u6307\u6570\u7684\u5897\u52a0\u306b\u3088\u308a\u3001\\(n\\) \u304c\u5927\u304d\u3044\u3068\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u5b9f\u884c\u304c\u306f\u308b\u304b\u306b\u9045\u304f\u306a\u308a\u3001\u9577\u3044\u5f85\u6a5f\u6642\u9593\u304c\u751f\u3058\u307e\u3059\u3002

        \u56f3 14-3 \u00a0 \u968e\u6bb5\u767b\u308a\u306e\u518d\u5e30\u6728

        \u4e0a\u306e\u56f3\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001**\u6307\u6570\u6642\u9593\u8a08\u7b97\u91cf\u306f\u300c\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u300d\u306b\u3088\u3063\u3066\u5f15\u304d\u8d77\u3053\u3055\u308c\u308b**\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\\(dp[9]\\) \u306f \\(dp[8]\\) \u3068 \\(dp[7]\\) \u306b\u5206\u89e3\u3055\u308c\u3001\\(dp[8]\\) \u306f\u3055\u3089\u306b \\(dp[7]\\) \u3068 \\(dp[6]\\) \u306b\u5206\u89e3\u3055\u308c\u3001\u4e21\u65b9\u3068\u3082\u90e8\u5206\u554f\u984c \\(dp[7]\\) \u3092\u542b\u3093\u3067\u3044\u307e\u3059\u3002

        \u3053\u306e\u3088\u3046\u306b\u3001\u90e8\u5206\u554f\u984c\u306b\u306f\u3055\u3089\u306b\u5c0f\u3055\u306a\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u304c\u542b\u307e\u308c\u3001\u3053\u308c\u306f\u7121\u9650\u306b\u7d9a\u304d\u307e\u3059\u3002\u8a08\u7b97\u30ea\u30bd\u30fc\u30b9\u306e\u5927\u90e8\u5206\u304c\u3053\u308c\u3089\u306e\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u306b\u6d6a\u8cbb\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1412-2","title":"14.1.2 \u00a0 \u65b9\u6cd52\uff1a\u30e1\u30e2\u5316\u63a2\u7d22","text":"

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u308b\u305f\u3081\u3001**\u3059\u3079\u3066\u306e\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u3092\u4e00\u5ea6\u3060\u3051\u8a08\u7b97\u3057\u305f\u3044**\u3068\u8003\u3048\u307e\u3059\u3002\u3053\u306e\u76ee\u7684\u306e\u305f\u3081\u3001\u5404\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u8a18\u9332\u3059\u308b\u914d\u5217 mem \u3092\u5ba3\u8a00\u3057\u3001\u63a2\u7d22\u30d7\u30ed\u30bb\u30b9\u4e2d\u306b\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u3092\u679d\u5208\u308a\u3057\u307e\u3059\u3002

        1. \\(dp[i]\\) \u304c\u521d\u3081\u3066\u8a08\u7b97\u3055\u308c\u308b\u3068\u304d\u3001\u5f8c\u3067\u4f7f\u7528\u3059\u308b\u305f\u3081\u306b mem[i] \u306b\u8a18\u9332\u3057\u307e\u3059\u3002
        2. \\(dp[i]\\) \u3092\u518d\u5ea6\u8a08\u7b97\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3068\u304d\u3001mem[i] \u304b\u3089\u76f4\u63a5\u7d50\u679c\u3092\u53d6\u5f97\u3067\u304d\u3001\u305d\u306e\u90e8\u5206\u554f\u984c\u306e\u5197\u9577\u306a\u8a08\u7b97\u3092\u907f\u3051\u3089\u308c\u307e\u3059\u3002

        \u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dfs_mem.py
        def dfs(i: int, mem: list[int]) -> int:\n    \"\"\"\u8a18\u61b6\u5316\u63a2\u7d22\"\"\"\n    # \u65e2\u77e5\u306e dp[1] \u3068 dp[2] \u306f\u3001\u305d\u308c\u3089\u3092\u8fd4\u3059\n    if i == 1 or i == 2:\n        return i\n    # dp[i] \u306e\u8a18\u9332\u304c\u3042\u308b\u5834\u5408\u3001\u305d\u308c\u3092\u8fd4\u3059\n    if mem[i] != -1:\n        return mem[i]\n    # dp[i] = dp[i-1] + dp[i-2]\n    count = dfs(i - 1, mem) + dfs(i - 2, mem)\n    # dp[i] \u3092\u8a18\u9332\n    mem[i] = count\n    return count\n\ndef climbing_stairs_dfs_mem(n: int) -> int:\n    \"\"\"\u968e\u6bb5\u767b\u308a\uff1a\u8a18\u61b6\u5316\u63a2\u7d22\"\"\"\n    # mem[i] \u306f i \u6bb5\u76ee\u306b\u767b\u308b\u89e3\u306e\u7dcf\u6570\u3092\u8a18\u9332\u3001-1 \u306f\u8a18\u9332\u306a\u3057\u3092\u610f\u5473\u3059\u308b\n    mem = [-1] * (n + 1)\n    return dfs(n, mem)\n
        climbing_stairs_dfs_mem.cpp
        /* \u30e1\u30e2\u5316\u63a2\u7d22 */\nint dfs(int i, vector<int> &mem) {\n    // \u65e2\u77e5\u306e dp[1] \u3068 dp[2] \u3092\u8fd4\u3059\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] \u306e\u8a18\u9332\u304c\u3042\u308b\u5834\u5408\u3001\u305d\u308c\u3092\u8fd4\u3059\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // dp[i] \u3092\u8a18\u9332\n    mem[i] = count;\n    return count;\n}\n\n/* \u968e\u6bb5\u767b\u308a\uff1a\u30e1\u30e2\u5316\u63a2\u7d22 */\nint climbingStairsDFSMem(int n) {\n    // mem[i] \u306f i \u6bb5\u76ee\u306b\u767b\u308b\u7dcf\u89e3\u6570\u3092\u8a18\u9332\u3001-1 \u306f\u8a18\u9332\u306a\u3057\u3092\u610f\u5473\u3059\u308b\n    vector<int> mem(n + 1, -1);\n    return dfs(n, mem);\n}\n
        climbing_stairs_dfs_mem.java
        /* \u30e1\u30e2\u5316\u63a2\u7d22 */\nint dfs(int i, int[] mem) {\n    // \u65e2\u77e5\u306e dp[1] \u3068 dp[2] \u3092\u8fd4\u3059\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] \u306e\u8a18\u9332\u304c\u3042\u308b\u5834\u5408\u3001\u305d\u308c\u3092\u8fd4\u3059\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // dp[i] \u3092\u8a18\u9332\n    mem[i] = count;\n    return count;\n}\n\n/* \u968e\u6bb5\u767b\u308a\uff1a\u30e1\u30e2\u5316\u63a2\u7d22 */\nint climbingStairsDFSMem(int n) {\n    // mem[i] \u306f i \u6bb5\u76ee\u306b\u767b\u308b\u7dcf\u89e3\u6570\u3092\u8a18\u9332\u3001-1 \u306f\u8a18\u9332\u306a\u3057\u3092\u610f\u5473\u3059\u308b\n    int[] mem = new int[n + 1];\n    Arrays.fill(mem, -1);\n    return dfs(n, mem);\n}\n
        climbing_stairs_dfs_mem.cs
        [class]{climbing_stairs_dfs_mem}-[func]{DFS}\n\n[class]{climbing_stairs_dfs_mem}-[func]{ClimbingStairsDFSMem}\n
        climbing_stairs_dfs_mem.go
        [class]{}-[func]{dfsMem}\n\n[class]{}-[func]{climbingStairsDFSMem}\n
        climbing_stairs_dfs_mem.swift
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFSMem}\n
        climbing_stairs_dfs_mem.js
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFSMem}\n
        climbing_stairs_dfs_mem.ts
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFSMem}\n
        climbing_stairs_dfs_mem.dart
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFSMem}\n
        climbing_stairs_dfs_mem.rs
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbing_stairs_dfs_mem}\n
        climbing_stairs_dfs_mem.c
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFSMem}\n
        climbing_stairs_dfs_mem.kt
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFSMem}\n
        climbing_stairs_dfs_mem.rb
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbing_stairs_dfs_mem}\n

        \u4e0b\u306e\u56f3\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001**\u30e1\u30e2\u5316\u5f8c\u3001\u3059\u3079\u3066\u306e\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u306f\u4e00\u5ea6\u3060\u3051\u8a08\u7b97\u3055\u308c\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u3092 \\(O(n)\\) \u306b\u6700\u9069\u5316**\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u5927\u5e45\u306a\u6539\u5584\u3067\u3059\u3002

        \u56f3 14-4 \u00a0 \u30e1\u30e2\u5316\u63a2\u7d22\u306b\u3088\u308b\u518d\u5e30\u6728

        "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1413-3","title":"14.1.3 \u00a0 \u65b9\u6cd53\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0","text":"

        **\u30e1\u30e2\u5316\u63a2\u7d22\u306f\u300c\u30c8\u30c3\u30d7\u30c0\u30a6\u30f3\u300d\u65b9\u5f0f**\u3067\u3059\uff1a\u5143\u306e\u554f\u984c\uff08\u6839\u30ce\u30fc\u30c9\uff09\u304b\u3089\u59cb\u3081\u3066\u3001\u3088\u308a\u5927\u304d\u306a\u90e8\u5206\u554f\u984c\u3092\u3088\u308a\u5c0f\u3055\u306a\u3082\u306e\u306b\u518d\u5e30\u7684\u306b\u5206\u89e3\u3057\u3001\u6700\u5c0f\u306e\u65e2\u77e5\u306e\u90e8\u5206\u554f\u984c\uff08\u8449\u30ce\u30fc\u30c9\uff09\u306e\u89e3\u306b\u5230\u9054\u3059\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002\u305d\u306e\u5f8c\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306b\u3088\u308a\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u53ce\u96c6\u3057\u3001\u5143\u306e\u554f\u984c\u306e\u89e3\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002

        \u4e00\u65b9\u3001**\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u300c\u30dc\u30c8\u30e0\u30a2\u30c3\u30d7\u300d\u65b9\u5f0f**\u3067\u3059\uff1a\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u304b\u3089\u59cb\u3081\u3066\u3001\u5143\u306e\u554f\u984c\u304c\u89e3\u6c7a\u3055\u308c\u308b\u307e\u3067\u3001\u3088\u308a\u5927\u304d\u306a\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u53cd\u5fa9\u7684\u306b\u69cb\u7bc9\u3057\u307e\u3059\u3002

        \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3092\u5fc5\u8981\u3068\u3057\u306a\u3044\u305f\u3081\u3001\u30eb\u30fc\u30d7\u3092\u4f7f\u3063\u305f\u53cd\u5fa9\u306e\u307f\u304c\u5fc5\u8981\u3067\u3001\u518d\u5e30\u306f\u4e0d\u8981\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3001\u914d\u5217 dp \u3092\u521d\u671f\u5316\u3057\u3066\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4fdd\u5b58\u3057\u3001\u30e1\u30e2\u5316\u63a2\u7d22\u306e\u914d\u5217 mem \u3068\u540c\u3058\u8a18\u9332\u6a5f\u80fd\u3092\u679c\u305f\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dp.py
        def climbing_stairs_dp(n: int) -> int:\n    \"\"\"\u968e\u6bb5\u767b\u308a\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    if n == 1 or n == 2:\n        return n\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u4f7f\u7528\n    dp = [0] * (n + 1)\n    # \u521d\u671f\u72b6\u614b\uff1a\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4e8b\u524d\u8a2d\u5b9a\n    dp[1], dp[2] = 1, 2\n    # \u72b6\u614b\u9077\u79fb\uff1a\u5c0f\u3055\u3044\u90e8\u5206\u554f\u984c\u304b\u3089\u5927\u304d\u3044\u90e8\u5206\u554f\u984c\u3092\u6bb5\u968e\u7684\u306b\u89e3\u304f\n    for i in range(3, n + 1):\n        dp[i] = dp[i - 1] + dp[i - 2]\n    return dp[n]\n
        climbing_stairs_dp.cpp
        /* \u968e\u6bb5\u767b\u308a\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint climbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3057\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    vector<int> dp(n + 1);\n    // \u521d\u671f\u72b6\u614b\uff1a\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4e8b\u524d\u8a2d\u5b9a\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u72b6\u614b\u9077\u79fb\uff1a\u5c0f\u3055\u306a\u554f\u984c\u304b\u3089\u5927\u304d\u306a\u90e8\u5206\u554f\u984c\u3092\u6bb5\u968e\u7684\u306b\u89e3\u304f\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
        climbing_stairs_dp.java
        /* \u968e\u6bb5\u767b\u308a\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint climbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3057\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    int[] dp = new int[n + 1];\n    // \u521d\u671f\u72b6\u614b\uff1a\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4e8b\u524d\u8a2d\u5b9a\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u72b6\u614b\u9077\u79fb\uff1a\u5c0f\u3055\u306a\u554f\u984c\u304b\u3089\u5927\u304d\u306a\u90e8\u5206\u554f\u984c\u3092\u6bb5\u968e\u7684\u306b\u89e3\u304f\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
        climbing_stairs_dp.cs
        [class]{climbing_stairs_dp}-[func]{ClimbingStairsDP}\n
        climbing_stairs_dp.go
        [class]{}-[func]{climbingStairsDP}\n
        climbing_stairs_dp.swift
        [class]{}-[func]{climbingStairsDP}\n
        climbing_stairs_dp.js
        [class]{}-[func]{climbingStairsDP}\n
        climbing_stairs_dp.ts
        [class]{}-[func]{climbingStairsDP}\n
        climbing_stairs_dp.dart
        [class]{}-[func]{climbingStairsDP}\n
        climbing_stairs_dp.rs
        [class]{}-[func]{climbing_stairs_dp}\n
        climbing_stairs_dp.c
        [class]{}-[func]{climbingStairsDP}\n
        climbing_stairs_dp.kt
        [class]{}-[func]{climbingStairsDP}\n
        climbing_stairs_dp.rb
        [class]{}-[func]{climbing_stairs_dp}\n

        \u4e0b\u306e\u56f3\u306f\u4e0a\u8a18\u30b3\u30fc\u30c9\u306e\u5b9f\u884c\u30d7\u30ed\u30bb\u30b9\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 14-5 \u00a0 \u968e\u6bb5\u767b\u308a\u306e\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9

        \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u540c\u69d8\u306b\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3082\u300c\u72b6\u614b\u300d\u306e\u6982\u5ff5\u3092\u4f7f\u7528\u3057\u3066\u554f\u984c\u89e3\u6c7a\u306e\u7279\u5b9a\u306e\u6bb5\u968e\u3092\u8868\u73fe\u3057\u3001\u5404\u72b6\u614b\u306f\u90e8\u5206\u554f\u984c\u3068\u305d\u306e\u5c40\u6240\u6700\u9069\u89e3\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u968e\u6bb5\u767b\u308a\u554f\u984c\u306e\u72b6\u614b\u306f\u73fe\u5728\u306e\u30b9\u30c6\u30c3\u30d7\u756a\u53f7 \\(i\\) \u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u307e\u3059\u3002

        \u4e0a\u8a18\u306e\u5185\u5bb9\u306b\u57fa\u3065\u3044\u3066\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3067\u3088\u304f\u4f7f\u7528\u3055\u308c\u308b\u7528\u8a9e\u3092\u307e\u3068\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        • \u914d\u5217 dp \u306fDP\u30c6\u30fc\u30d6\u30eb\u3068\u547c\u3070\u308c\u3001\\(dp[i]\\) \u306f\u72b6\u614b \\(i\\) \u306b\u5bfe\u5fdc\u3059\u308b\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u8868\u3057\u307e\u3059\u3002
        • \u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\uff08\u30b9\u30c6\u30c3\u30d7 \\(1\\) \u3068 \\(2\\)\uff09\u306b\u5bfe\u5fdc\u3059\u308b\u72b6\u614b\u306f\u521d\u671f\u72b6\u614b\u3068\u547c\u3070\u308c\u307e\u3059\u3002
        • \u518d\u5e30\u5f0f \\(dp[i] = dp[i-1] + dp[i-2]\\) \u306f\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3068\u547c\u3070\u308c\u307e\u3059\u3002
        "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1414","title":"14.1.4 \u00a0 \u7a7a\u9593\u6700\u9069\u5316","text":"

        \u6ce8\u610f\u6df1\u3044\u8aad\u8005\u306f**\\(dp[i]\\) \u306f \\(dp[i-1]\\) \u3068 \\(dp[i-2]\\) \u306e\u307f\u306b\u95a2\u9023\u3059\u308b\u305f\u3081\u3001\u3059\u3079\u3066\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4fdd\u5b58\u3059\u308b\u305f\u3081\u306b\u914d\u5217 dp \u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u306a\u3044**\u3053\u3068\u306b\u6c17\u3065\u304f\u3067\u3057\u3087\u3046\u3002\u5358\u306b2\u3064\u306e\u5909\u6570\u3092\u4f7f\u3063\u3066\u53cd\u5fa9\u7684\u306b\u9032\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dp.py
        def climbing_stairs_dp_comp(n: int) -> int:\n    \"\"\"\u968e\u6bb5\u767b\u308a\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    if n == 1 or n == 2:\n        return n\n    a, b = 1, 2\n    for _ in range(3, n + 1):\n        a, b = b, a + b\n    return b\n
        climbing_stairs_dp.cpp
        /* \u968e\u6bb5\u767b\u308a\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint climbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
        climbing_stairs_dp.java
        /* \u968e\u6bb5\u767b\u308a\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint climbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
        climbing_stairs_dp.cs
        [class]{climbing_stairs_dp}-[func]{ClimbingStairsDPComp}\n
        climbing_stairs_dp.go
        [class]{}-[func]{climbingStairsDPComp}\n
        climbing_stairs_dp.swift
        [class]{}-[func]{climbingStairsDPComp}\n
        climbing_stairs_dp.js
        [class]{}-[func]{climbingStairsDPComp}\n
        climbing_stairs_dp.ts
        [class]{}-[func]{climbingStairsDPComp}\n
        climbing_stairs_dp.dart
        [class]{}-[func]{climbingStairsDPComp}\n
        climbing_stairs_dp.rs
        [class]{}-[func]{climbing_stairs_dp_comp}\n
        climbing_stairs_dp.c
        [class]{}-[func]{climbingStairsDPComp}\n
        climbing_stairs_dp.kt
        [class]{}-[func]{climbingStairsDPComp}\n
        climbing_stairs_dp.rb
        [class]{}-[func]{climbing_stairs_dp_comp}\n

        \u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\u914d\u5217 dp \u304c\u5360\u6709\u3057\u3066\u3044\u305f\u7a7a\u9593\u304c\u524a\u9664\u3055\u308c\u308b\u305f\u3081\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u304b\u3089 \\(O(1)\\) \u306b\u524a\u6e1b\u3055\u308c\u307e\u3059\u3002

        \u591a\u304f\u306e\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u3067\u306f\u3001\u73fe\u5728\u306e\u72b6\u614b\u306f\u9650\u3089\u308c\u305f\u6570\u306e\u524d\u306e\u72b6\u614b\u306e\u307f\u306b\u4f9d\u5b58\u3059\u308b\u305f\u3081\u3001\u5fc5\u8981\u306a\u72b6\u614b\u306e\u307f\u3092\u4fdd\u6301\u3057\u3001\u300c\u6b21\u5143\u524a\u6e1b\u300d\u306b\u3088\u3063\u3066\u30e1\u30e2\u30ea\u7a7a\u9593\u3092\u7bc0\u7d04\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u7a7a\u9593\u6700\u9069\u5316\u6280\u8853\u306f\u300c\u30ed\u30fc\u30ea\u30f3\u30b0\u5909\u6570\u300d\u307e\u305f\u306f\u300c\u30ed\u30fc\u30ea\u30f3\u30b0\u914d\u5217\u300d\u3068\u3057\u3066\u77e5\u3089\u308c\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_dynamic_programming/knapsack_problem/","title":"14.4 \u00a0 0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c","text":"

        \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306f\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306e\u512a\u308c\u305f\u5165\u9580\u554f\u984c\u3067\u3042\u308a\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3067\u6700\u3082\u4e00\u822c\u7684\u306a\u554f\u984c\u30bf\u30a4\u30d7\u3067\u3059\u30020-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3001\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3001\u8907\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306a\u3069\u3001\u591a\u304f\u306e\u5909\u7a2e\u304c\u3042\u308a\u307e\u3059\u3002

        \u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u307e\u305a\u6700\u3082\u4e00\u822c\u7684\u306a0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3092\u89e3\u6c7a\u3057\u307e\u3059\u3002

        Question

        \\(n\\) \u500b\u306e\u30a2\u30a4\u30c6\u30e0\u304c\u4e0e\u3048\u3089\u308c\u3001\\(i\\) \u756a\u76ee\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u91cd\u91cf\u306f \\(wgt[i-1]\\) \u3067\u5024\u306f \\(val[i-1]\\) \u3067\u3059\u3002\u5bb9\u91cf\u304c \\(cap\\) \u306e\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u304c\u3042\u308a\u307e\u3059\u3002\u5404\u30a2\u30a4\u30c6\u30e0\u306f1\u56de\u306e\u307f\u9078\u629e\u3067\u304d\u307e\u3059\u3002\u5bb9\u91cf\u5236\u9650\u4e0b\u3067\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u30a2\u30a4\u30c6\u30e0\u306e\u6700\u5927\u5024\u306f\u4f55\u3067\u3059\u304b\uff1f

        \u4e0b\u306e\u56f3\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\u30a2\u30a4\u30c6\u30e0\u756a\u53f7 \\(i\\) \u306f1\u304b\u3089\u6570\u3048\u59cb\u3081\u3001\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f0\u304b\u3089\u59cb\u307e\u308b\u305f\u3081\u3001\u30a2\u30a4\u30c6\u30e0 \\(i\\) \u306e\u91cd\u91cf\u306f \\(wgt[i-1]\\) \u306b\u5bfe\u5fdc\u3057\u3001\u5024\u306f \\(val[i-1]\\) \u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002

        \u56f3 14-17 \u00a0 0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u4f8b\u30c7\u30fc\u30bf

        0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3092 \\(n\\) \u30e9\u30a6\u30f3\u30c9\u306e\u6c7a\u5b9a\u304b\u3089\u69cb\u6210\u3055\u308c\u308b\u30d7\u30ed\u30bb\u30b9\u3068\u3057\u3066\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u5404\u30a2\u30a4\u30c6\u30e0\u306b\u3064\u3044\u3066\u5165\u308c\u306a\u3044\u3001\u307e\u305f\u306f\u5165\u308c\u308b\u3068\u3044\u30462\u3064\u306e\u6c7a\u5b9a\u304c\u3042\u308a\u3001\u3057\u305f\u304c\u3063\u3066\u554f\u984c\u306f\u6c7a\u5b9a\u6728\u30e2\u30c7\u30eb\u306b\u9069\u5408\u3057\u307e\u3059\u3002

        \u3053\u306e\u554f\u984c\u306e\u76ee\u7684\u306f\u300c\u9650\u3089\u308c\u305f\u5bb9\u91cf\u306e\u4e0b\u3067\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u30a2\u30a4\u30c6\u30e0\u306e\u5024\u3092\u6700\u5927\u5316\u3059\u308b\u300d\u3053\u3068\u3067\u3042\u308a\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u3067\u3042\u308b\u53ef\u80fd\u6027\u304c\u9ad8\u3044\u3067\u3059\u3002

        \u7b2c1\u30b9\u30c6\u30c3\u30d7\uff1a\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u6c7a\u5b9a\u3092\u8003\u3048\u3001\u72b6\u614b\u3092\u5b9a\u7fa9\u3057\u3001\u305d\u308c\u306b\u3088\u308a \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u3092\u5f97\u308b

        \u5404\u30a2\u30a4\u30c6\u30e0\u306b\u3064\u3044\u3066\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u306a\u3051\u308c\u3070\u5bb9\u91cf\u306f\u5909\u308f\u3089\u305a\u3001\u5165\u308c\u308c\u3070\u5bb9\u91cf\u306f\u6e1b\u5c11\u3057\u307e\u3059\u3002\u3053\u308c\u304b\u3089\u72b6\u614b\u5b9a\u7fa9\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a\u73fe\u5728\u306e\u30a2\u30a4\u30c6\u30e0\u756a\u53f7 \\(i\\) \u3068\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf \\(c\\)\u3001\\([i, c]\\) \u3068\u8868\u8a18\u3055\u308c\u307e\u3059\u3002

        \u72b6\u614b \\([i, c]\\) \u306f\u90e8\u5206\u554f\u984c\u306b\u5bfe\u5fdc\u3057\u307e\u3059\uff1a\u5bb9\u91cf \\(c\\) \u306e\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u3067\u306e\u6700\u521d\u306e \\(i\\) \u500b\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u6700\u5927\u5024\u3001\\(dp[i, c]\\) \u3068\u8868\u8a18\u3055\u308c\u307e\u3059\u3002

        \u63a2\u3057\u3066\u3044\u308b\u89e3\u306f \\(dp[n, cap]\\) \u3067\u3042\u308b\u305f\u3081\u3001\u30b5\u30a4\u30ba \\((n+1) \\times (cap+1)\\) \u306e\u4e8c\u6b21\u5143 \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u304c\u5fc5\u8981\u3067\u3059\u3002

        \u7b2c2\u30b9\u30c6\u30c3\u30d7\uff1a\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u7279\u5b9a\u3057\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3092\u5c0e\u51fa\u3059\u308b

        \u30a2\u30a4\u30c6\u30e0 \\(i\\) \u306e\u6c7a\u5b9a\u3092\u884c\u3063\u305f\u5f8c\u3001\u6b8b\u308b\u306e\u306f\u6700\u521d\u306e \\(i-1\\) \u500b\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u6c7a\u5b9a\u306e\u90e8\u5206\u554f\u984c\u3067\u3042\u308a\u3001\u3053\u308c\u306f2\u3064\u306e\u30b1\u30fc\u30b9\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        • \u30a2\u30a4\u30c6\u30e0 \\(i\\) \u3092\u5165\u308c\u306a\u3044\uff1a\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf\u306f\u5909\u308f\u3089\u305a\u3001\u72b6\u614b\u306f \\([i-1, c]\\) \u306b\u5909\u308f\u308a\u307e\u3059\u3002
        • \u30a2\u30a4\u30c6\u30e0 \\(i\\) \u3092\u5165\u308c\u308b\uff1a\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf\u306f \\(wgt[i-1]\\) \u3060\u3051\u6e1b\u5c11\u3057\u3001\u5024\u306f \\(val[i-1]\\) \u3060\u3051\u5897\u52a0\u3057\u3001\u72b6\u614b\u306f \\([i-1, c-wgt[i-1]]\\) \u306b\u5909\u308f\u308a\u307e\u3059\u3002

        \u4e0a\u8a18\u306e\u5206\u6790\u306b\u3088\u308a\u3001\u3053\u306e\u554f\u984c\u306e\u6700\u9069\u90e8\u5206\u69cb\u9020\u304c\u660e\u3089\u304b\u306b\u306a\u308a\u307e\u3059\uff1a\u6700\u5927\u5024 \\(dp[i, c]\\) \u306f\u3001\u30a2\u30a4\u30c6\u30e0 \\(i\\) \u3092\u5165\u308c\u306a\u3044\u65b9\u6848\u3068\u30a2\u30a4\u30c6\u30e0 \\(i\\) \u3092\u5165\u308c\u308b\u65b9\u6848\u306e2\u3064\u306e\u3046\u3061\u3001\u3088\u308a\u5927\u304d\u306a\u5024\u306b\u7b49\u3057\u3044\u3002\u3053\u308c\u304b\u3089\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3092\u5c0e\u51fa\u3067\u304d\u307e\u3059\uff1a

        \\[ dp[i, c] = \\max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1]) \\]

        \u73fe\u5728\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u91cd\u91cf \\(wgt[i - 1]\\) \u304c\u6b8b\u308a\u306e\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf \\(c\\) \u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u552f\u4e00\u306e\u9078\u629e\u80a2\u306f\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u306a\u3044\u3053\u3068\u3067\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002

        \u7b2c3\u30b9\u30c6\u30c3\u30d7\uff1a\u5883\u754c\u6761\u4ef6\u3068\u72b6\u614b\u9077\u79fb\u306e\u9806\u5e8f\u3092\u6c7a\u5b9a\u3059\u308b

        \u30a2\u30a4\u30c6\u30e0\u304c\u306a\u3044\u5834\u5408\u307e\u305f\u306f\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf\u304c \\(0\\) \u306e\u5834\u5408\u3001\u6700\u5927\u5024\u306f \\(0\\) \u3067\u3059\u3002\u3064\u307e\u308a\u3001\u6700\u521d\u306e\u5217 \\(dp[i, 0]\\) \u3068\u6700\u521d\u306e\u884c \\(dp[0, c]\\) \u306f\u3069\u3061\u3089\u3082 \\(0\\) \u306b\u7b49\u3057\u3044\u3067\u3059\u3002

        \u73fe\u5728\u306e\u72b6\u614b \\([i, c]\\) \u306f\u76f4\u63a5\u4e0a\u306e\u72b6\u614b \\([i-1, c]\\) \u3068\u5de6\u4e0a\u306e\u72b6\u614b \\([i-1, c-wgt[i-1]]\\) \u304b\u3089\u9077\u79fb\u3059\u308b\u305f\u3081\u30012\u5c64\u306e\u30eb\u30fc\u30d7\u3092\u901a\u3058\u3066 \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u5168\u4f53\u3092\u9806\u5e8f\u901a\u308a\u306b\u8d70\u67fb\u3057\u307e\u3059\u3002

        \u4e0a\u8a18\u306e\u5206\u6790\u306b\u5f93\u3063\u3066\u3001\u6b21\u306b\u529b\u4efb\u305b\u63a2\u7d22\u3001\u30e1\u30e2\u5316\u63a2\u7d22\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306e\u9806\u5e8f\u3067\u89e3\u6cd5\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002

        "},{"location":"chapter_dynamic_programming/knapsack_problem/#1-1","title":"1. \u00a0 \u65b9\u6cd51\uff1a\u529b\u4efb\u305b\u63a2\u7d22","text":"

        \u63a2\u7d22\u30b3\u30fc\u30c9\u306b\u306f\u4ee5\u4e0b\u306e\u8981\u7d20\u304c\u542b\u307e\u308c\u307e\u3059\u3002

        • \u518d\u5e30\u30d1\u30e9\u30e1\u30fc\u30bf\uff1a\u72b6\u614b \\([i, c]\\)\u3002
        • \u623b\u308a\u5024\uff1a\u90e8\u5206\u554f\u984c \\(dp[i, c]\\) \u306e\u89e3\u3002
        • \u7d42\u4e86\u6761\u4ef6\uff1a\u30a2\u30a4\u30c6\u30e0\u756a\u53f7\u304c\u7bc4\u56f2\u5916 \\(i = 0\\) \u307e\u305f\u306f\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u6b8b\u308a\u5bb9\u91cf\u304c \\(0\\) \u306e\u3068\u304d\u3001\u518d\u5e30\u3092\u7d42\u4e86\u3057\u5024 \\(0\\) \u3092\u8fd4\u3059\u3002
        • \u679d\u5208\u308a\uff1a\u73fe\u5728\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u91cd\u91cf\u304c\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u6b8b\u308a\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u552f\u4e00\u306e\u9078\u629e\u80a2\u306f\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u306a\u3044\u3053\u3068\u3067\u3059\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
        def knapsack_dfs(wgt: list[int], val: list[int], i: int, c: int) -> int:\n    \"\"\"0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u30d6\u30eb\u30fc\u30c8\u30d5\u30a9\u30fc\u30b9\u63a2\u7d22\"\"\"\n    # \u3059\u3079\u3066\u306e\u30a2\u30a4\u30c6\u30e0\u304c\u9078\u629e\u3055\u308c\u305f\u304b\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u6b8b\u308a\u5bb9\u91cf\u304c\u306a\u3044\u5834\u5408\u3001\u5024 0 \u3092\u8fd4\u3059\n    if i == 0 or c == 0:\n        return 0\n    # \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u306a\u3044\u3053\u3068\u3057\u304b\u9078\u629e\u3067\u304d\u306a\u3044\n    if wgt[i - 1] > c:\n        return knapsack_dfs(wgt, val, i - 1, c)\n    # \u30a2\u30a4\u30c6\u30e0 i \u3092\u5165\u308c\u306a\u3044\u306e\u3068\u5165\u308c\u308b\u306e\u3068\u306e\u6700\u5927\u5024\u3092\u8a08\u7b97\n    no = knapsack_dfs(wgt, val, i - 1, c)\n    yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]\n    # 2 \u3064\u306e\u9078\u629e\u80a2\u306e\u3046\u3061\u5927\u304d\u3044\u5024\u3092\u8fd4\u3059\n    return max(no, yes)\n
        knapsack.cpp
        /* 0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u30d6\u30eb\u30fc\u30c8\u30d5\u30a9\u30fc\u30b9\u63a2\u7d22 */\nint knapsackDFS(vector<int> &wgt, vector<int> &val, int i, int c) {\n    // \u3059\u3079\u3066\u306e\u30a2\u30a4\u30c6\u30e0\u304c\u9078\u629e\u3055\u308c\u305f\u304b\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u6b8b\u308a\u5bb9\u91cf\u304c\u306a\u3044\u5834\u5408\u3001\u5024 0 \u3092\u8fd4\u3059\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u306a\u3044\u3053\u3068\u3057\u304b\u9078\u629e\u3067\u304d\u306a\u3044\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // \u30a2\u30a4\u30c6\u30e0 i \u3092\u5165\u308c\u306a\u3044\u5834\u5408\u3068\u5165\u308c\u308b\u5834\u5408\u306e\u6700\u5927\u5024\u3092\u8a08\u7b97\n    int no = knapsackDFS(wgt, val, i - 1, c);\n    int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // 2\u3064\u306e\u9078\u629e\u80a2\u306e\u3088\u308a\u5927\u304d\u3044\u5024\u3092\u8fd4\u3059\n    return max(no, yes);\n}\n
        knapsack.java
        /* 0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u30d6\u30eb\u30fc\u30c8\u30d5\u30a9\u30fc\u30b9\u63a2\u7d22 */\nint knapsackDFS(int[] wgt, int[] val, int i, int c) {\n    // \u3059\u3079\u3066\u306e\u30a2\u30a4\u30c6\u30e0\u304c\u9078\u629e\u3055\u308c\u305f\u304b\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u6b8b\u308a\u5bb9\u91cf\u304c\u306a\u3044\u5834\u5408\u3001\u5024 0 \u3092\u8fd4\u3059\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u306a\u3044\u3053\u3068\u3057\u304b\u9078\u629e\u3067\u304d\u306a\u3044\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // \u30a2\u30a4\u30c6\u30e0 i \u3092\u5165\u308c\u306a\u3044\u5834\u5408\u3068\u5165\u308c\u308b\u5834\u5408\u306e\u6700\u5927\u5024\u3092\u8a08\u7b97\n    int no = knapsackDFS(wgt, val, i - 1, c);\n    int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // 2\u3064\u306e\u9078\u629e\u80a2\u306e\u3088\u308a\u5927\u304d\u3044\u5024\u3092\u8fd4\u3059\n    return Math.max(no, yes);\n}\n
        knapsack.cs
        [class]{knapsack}-[func]{KnapsackDFS}\n
        knapsack.go
        [class]{}-[func]{knapsackDFS}\n
        knapsack.swift
        [class]{}-[func]{knapsackDFS}\n
        knapsack.js
        [class]{}-[func]{knapsackDFS}\n
        knapsack.ts
        [class]{}-[func]{knapsackDFS}\n
        knapsack.dart
        [class]{}-[func]{knapsackDFS}\n
        knapsack.rs
        [class]{}-[func]{knapsack_dfs}\n
        knapsack.c
        [class]{}-[func]{knapsackDFS}\n
        knapsack.kt
        [class]{}-[func]{knapsackDFS}\n
        knapsack.rb
        [class]{}-[func]{knapsack_dfs}\n

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5404\u30a2\u30a4\u30c6\u30e0\u306f\u9078\u629e\u3057\u306a\u3044\u3068\u9078\u629e\u3059\u308b\u3068\u3044\u30462\u3064\u306e\u63a2\u7d22\u5206\u5c90\u3092\u751f\u6210\u3059\u308b\u305f\u3081\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(2^n)\\) \u3067\u3059\u3002

        \u518d\u5e30\u6728\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\\(dp[1, 10]\\) \u306a\u3069\u306e\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u304c\u3042\u308b\u3053\u3068\u304c\u5bb9\u6613\u306b\u308f\u304b\u308a\u307e\u3059\u3002\u30a2\u30a4\u30c6\u30e0\u304c\u591a\u304f\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf\u304c\u5927\u304d\u3044\u5834\u5408\u3001\u7279\u306b\u540c\u3058\u91cd\u91cf\u306e\u30a2\u30a4\u30c6\u30e0\u304c\u591a\u3044\u5834\u5408\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u306e\u6570\u306f\u5927\u5e45\u306b\u5897\u52a0\u3057\u307e\u3059\u3002

        \u56f3 14-18 \u00a0 0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u529b\u4efb\u305b\u63a2\u7d22\u518d\u5e30\u6728

        "},{"location":"chapter_dynamic_programming/knapsack_problem/#2-2","title":"2. \u00a0 \u65b9\u6cd52\uff1a\u30e1\u30e2\u5316\u63a2\u7d22","text":"

        \u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u304c\u4e00\u5ea6\u3060\u3051\u8a08\u7b97\u3055\u308c\u308b\u3053\u3068\u3092\u78ba\u4fdd\u3059\u308b\u305f\u3081\u306b\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u8a18\u9332\u3059\u308b\u30e1\u30e2\u5316\u30ea\u30b9\u30c8 mem \u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u3053\u3053\u3067 mem[i][c] \u306f \\(dp[i, c]\\) \u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002

        \u30e1\u30e2\u5316\u3092\u5c0e\u5165\u3057\u305f\u5f8c\u3001**\u6642\u9593\u8a08\u7b97\u91cf\u306f\u90e8\u5206\u554f\u984c\u306e\u6570\u306b\u4f9d\u5b58**\u3057\u3001\\(O(n \\times cap)\\) \u306b\u306a\u308a\u307e\u3059\u3002\u5b9f\u88c5\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
        def knapsack_dfs_mem(\n    wgt: list[int], val: list[int], mem: list[list[int]], i: int, c: int\n) -> int:\n    \"\"\"0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u8a18\u61b6\u5316\u63a2\u7d22\"\"\"\n    # \u3059\u3079\u3066\u306e\u30a2\u30a4\u30c6\u30e0\u304c\u9078\u629e\u3055\u308c\u305f\u304b\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u6b8b\u308a\u5bb9\u91cf\u304c\u306a\u3044\u5834\u5408\u3001\u5024 0 \u3092\u8fd4\u3059\n    if i == 0 or c == 0:\n        return 0\n    # \u8a18\u9332\u304c\u3042\u308b\u5834\u5408\u3001\u305d\u308c\u3092\u8fd4\u3059\n    if mem[i][c] != -1:\n        return mem[i][c]\n    # \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u306a\u3044\u3053\u3068\u3057\u304b\u9078\u629e\u3067\u304d\u306a\u3044\n    if wgt[i - 1] > c:\n        return knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n    # \u30a2\u30a4\u30c6\u30e0 i \u3092\u5165\u308c\u306a\u3044\u306e\u3068\u5165\u308c\u308b\u306e\u3068\u306e\u6700\u5927\u5024\u3092\u8a08\u7b97\n    no = knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n    yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1]\n    # 2 \u3064\u306e\u9078\u629e\u80a2\u306e\u3046\u3061\u5927\u304d\u3044\u5024\u3092\u8a18\u9332\u3057\u3066\u8fd4\u3059\n    mem[i][c] = max(no, yes)\n    return mem[i][c]\n
        knapsack.cpp
        [class]{}-[func]{knapsackDFSMem}\n
        knapsack.java
        /* 0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u30e1\u30e2\u5316\u63a2\u7d22 */\nint knapsackDFSMem(int[] wgt, int[] val, int[][] mem, int i, int c) {\n    // \u3059\u3079\u3066\u306e\u30a2\u30a4\u30c6\u30e0\u304c\u9078\u629e\u3055\u308c\u305f\u304b\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u6b8b\u308a\u5bb9\u91cf\u304c\u306a\u3044\u5834\u5408\u3001\u5024 0 \u3092\u8fd4\u3059\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u8a18\u9332\u304c\u3042\u308b\u5834\u5408\u3001\u305d\u308c\u3092\u8fd4\u3059\n    if (mem[i][c] != -1) {\n        return mem[i][c];\n    }\n    // \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u306a\u3044\u3053\u3068\u3057\u304b\u9078\u629e\u3067\u304d\u306a\u3044\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, mem, i - 1, c);\n    }\n    // \u30a2\u30a4\u30c6\u30e0 i \u3092\u5165\u308c\u306a\u3044\u5834\u5408\u3068\u5165\u308c\u308b\u5834\u5408\u306e\u6700\u5927\u5024\u3092\u8a08\u7b97\n    int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n    int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // 2\u3064\u306e\u9078\u629e\u80a2\u306e\u3088\u308a\u5927\u304d\u3044\u5024\u3092\u8a18\u9332\u3057\u3066\u8fd4\u3059\n    mem[i][c] = Math.max(no, yes);\n    return mem[i][c];\n}\n
        knapsack.cs
        [class]{knapsack}-[func]{KnapsackDFSMem}\n
        knapsack.go
        [class]{}-[func]{knapsackDFSMem}\n
        knapsack.swift
        [class]{}-[func]{knapsackDFSMem}\n
        knapsack.js
        [class]{}-[func]{knapsackDFSMem}\n
        knapsack.ts
        [class]{}-[func]{knapsackDFSMem}\n
        knapsack.dart
        [class]{}-[func]{knapsackDFSMem}\n
        knapsack.rs
        [class]{}-[func]{knapsack_dfs_mem}\n
        knapsack.c
        [class]{}-[func]{knapsackDFSMem}\n
        knapsack.kt
        [class]{}-[func]{knapsackDFSMem}\n
        knapsack.rb
        [class]{}-[func]{knapsack_dfs_mem}\n

        \u4e0b\u306e\u56f3\u306f\u30e1\u30e2\u5316\u63a2\u7d22\u3067\u679d\u5208\u308a\u3055\u308c\u308b\u63a2\u7d22\u5206\u5c90\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 14-19 \u00a0 0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u30e1\u30e2\u5316\u63a2\u7d22\u518d\u5e30\u6728

        "},{"location":"chapter_dynamic_programming/knapsack_problem/#3-3","title":"3. \u00a0 \u65b9\u6cd53\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0","text":"

        \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u672c\u8cea\u7684\u306b\u72b6\u614b\u9077\u79fb\u4e2d\u306b \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u3092\u57cb\u3081\u308b\u3053\u3068\u3092\u542b\u307f\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
        def knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n = len(wgt)\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    dp = [[0] * (cap + 1) for _ in range(n + 1)]\n    # \u72b6\u614b\u9077\u79fb\n    for i in range(1, n + 1):\n        for c in range(1, cap + 1):\n            if wgt[i - 1] > c:\n                # \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][c] = dp[i - 1][c]\n            else:\n                # \u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\u306e\u3068\u9078\u629e\u3059\u308b\u306e\u3068\u3067\u5927\u304d\u3044\u5024\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\n    return dp[n][cap]\n
        knapsack.cpp
        /* 0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint knapsackDP(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5927\u304d\u3044\u5024\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        knapsack.java
        /* 0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint knapsackDP(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    int[][] dp = new int[n + 1][cap + 1];\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5927\u304d\u3044\u5024\n                dp[i][c] = Math.max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        knapsack.cs
        [class]{knapsack}-[func]{KnapsackDP}\n
        knapsack.go
        [class]{}-[func]{knapsackDP}\n
        knapsack.swift
        [class]{}-[func]{knapsackDP}\n
        knapsack.js
        [class]{}-[func]{knapsackDP}\n
        knapsack.ts
        [class]{}-[func]{knapsackDP}\n
        knapsack.dart
        [class]{}-[func]{knapsackDP}\n
        knapsack.rs
        [class]{}-[func]{knapsack_dp}\n
        knapsack.c
        [class]{}-[func]{knapsackDP}\n
        knapsack.kt
        [class]{}-[func]{knapsackDP}\n
        knapsack.rb
        [class]{}-[func]{knapsack_dp}\n

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u6642\u9593\u8a08\u7b97\u91cf\u3068\u7a7a\u9593\u8a08\u7b97\u91cf\u306e\u4e21\u65b9\u304c\u914d\u5217 dp \u306e\u30b5\u30a4\u30ba\u3001\u3064\u307e\u308a \\(O(n \\times cap)\\) \u306b\u3088\u3063\u3066\u6c7a\u5b9a\u3055\u308c\u307e\u3059\u3002

        <1><2><3><4><5><6><7><8><9><10><11><12><13><14>

        \u56f3 14-20 \u00a0 0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9

        "},{"location":"chapter_dynamic_programming/knapsack_problem/#4","title":"4. \u00a0 \u7a7a\u9593\u6700\u9069\u5316","text":"

        \u5404\u72b6\u614b\u306f\u4e0a\u306e\u884c\u306e\u72b6\u614b\u306e\u307f\u306b\u95a2\u9023\u3059\u308b\u305f\u3081\u30012\u3064\u306e\u914d\u5217\u3092\u4f7f\u7528\u3057\u3066\u30ed\u30fc\u30ea\u30f3\u30b0\u524d\u9032\u3055\u305b\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u3092 \\(O(n^2)\\) \u304b\u3089 \\(O(n)\\) \u306b\u524a\u6e1b\u3067\u304d\u307e\u3059\u3002

        \u3055\u3089\u306b\u8003\u3048\u3066\u307f\u308b\u3068\u30011\u3064\u306e\u914d\u5217\u3060\u3051\u3067\u7a7a\u9593\u6700\u9069\u5316\u3092\u9054\u6210\u3067\u304d\u308b\u3067\u3057\u3087\u3046\u304b\uff1f\u5404\u72b6\u614b\u304c\u76f4\u63a5\u4e0a\u306e\u30bb\u30eb\u307e\u305f\u306f\u5de6\u4e0a\u306e\u30bb\u30eb\u304b\u3089\u9077\u79fb\u3059\u308b\u3053\u3068\u304c\u89b3\u5bdf\u3067\u304d\u307e\u3059\u3002\u914d\u5217\u304c1\u3064\u3057\u304b\u306a\u3044\u5834\u5408\u3001\\(i\\) \u884c\u76ee\u306e\u8d70\u67fb\u3092\u958b\u59cb\u3059\u308b\u3068\u304d\u3001\u305d\u306e\u914d\u5217\u306f\u307e\u3060 \\(i-1\\) \u884c\u76ee\u306e\u72b6\u614b\u3092\u4fdd\u5b58\u3057\u3066\u3044\u307e\u3059\u3002

        • \u901a\u5e38\u306e\u9806\u5e8f\u3067\u8d70\u67fb\u3059\u308b\u5834\u5408\u3001\\(dp[i, j]\\) \u306b\u8d70\u67fb\u3057\u305f\u3068\u304d\u3001\u5de6\u4e0a\u306e \\(dp[i-1, 1]\\) \uff5e \\(dp[i-1, j-1]\\) \u306e\u5024\u304c\u3059\u3067\u306b\u4e0a\u66f8\u304d\u3055\u308c\u3066\u3044\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u6b63\u3057\u3044\u72b6\u614b\u9077\u79fb\u7d50\u679c\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u305b\u3093\u3002
        • \u9006\u9806\u3067\u8d70\u67fb\u3059\u308b\u5834\u5408\u3001\u4e0a\u66f8\u304d\u554f\u984c\u306f\u306a\u304f\u3001\u72b6\u614b\u9077\u79fb\u3092\u6b63\u3057\u304f\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002

        \u4e0b\u306e\u56f3\u306f\u5358\u4e00\u914d\u5217\u3067\u306e \\(i = 1\\) \u884c\u76ee\u304b\u3089 \\(i = 2\\) \u884c\u76ee\u3078\u306e\u9077\u79fb\u30d7\u30ed\u30bb\u30b9\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u901a\u5e38\u9806\u5e8f\u8d70\u67fb\u3068\u9006\u9806\u8d70\u67fb\u306e\u9055\u3044\u306b\u3064\u3044\u3066\u8003\u3048\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002

        <1><2><3><4><5><6>

        \u56f3 14-21 \u00a0 0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9

        \u30b3\u30fc\u30c9\u5b9f\u88c5\u3067\u306f\u3001\u914d\u5217 dp \u306e\u6700\u521d\u306e\u6b21\u5143 \\(i\\) \u3092\u524a\u9664\u3057\u3001\u5185\u5074\u306e\u30eb\u30fc\u30d7\u3092\u9006\u8d70\u67fb\u306b\u5909\u66f4\u3059\u308b\u3060\u3051\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
        def knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n = len(wgt)\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    dp = [0] * (cap + 1)\n    # \u72b6\u614b\u9077\u79fb\n    for i in range(1, n + 1):\n        # \u9006\u9806\u3067\u8d70\u67fb\n        for c in range(cap, 0, -1):\n            if wgt[i - 1] > c:\n                # \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[c] = dp[c]\n            else:\n                # \u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\u306e\u3068\u9078\u629e\u3059\u308b\u306e\u3068\u3067\u5927\u304d\u3044\u5024\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n    return dp[cap]\n
        knapsack.cpp
        /* 0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint knapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    vector<int> dp(cap + 1, 0);\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        // \u9006\u9806\u3067\u8d70\u67fb\n        for (int c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5927\u304d\u3044\u5024\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        knapsack.java
        /* 0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint knapsackDPComp(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    int[] dp = new int[cap + 1];\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        // \u9006\u9806\u3067\u8d70\u67fb\n        for (int c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5927\u304d\u3044\u5024\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        knapsack.cs
        [class]{knapsack}-[func]{KnapsackDPComp}\n
        knapsack.go
        [class]{}-[func]{knapsackDPComp}\n
        knapsack.swift
        [class]{}-[func]{knapsackDPComp}\n
        knapsack.js
        [class]{}-[func]{knapsackDPComp}\n
        knapsack.ts
        [class]{}-[func]{knapsackDPComp}\n
        knapsack.dart
        [class]{}-[func]{knapsackDPComp}\n
        knapsack.rs
        [class]{}-[func]{knapsack_dp_comp}\n
        knapsack.c
        [class]{}-[func]{knapsackDPComp}\n
        knapsack.kt
        [class]{}-[func]{knapsackDPComp}\n
        knapsack.rb
        [class]{}-[func]{knapsack_dp_comp}\n
        "},{"location":"chapter_dynamic_programming/summary/","title":"14.7 \u00a0 \u307e\u3068\u3081","text":"
        • \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u554f\u984c\u3092\u5206\u89e3\u3057\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4fdd\u5b58\u3059\u308b\u3053\u3068\u3067\u5197\u9577\u306a\u8a08\u7b97\u3092\u907f\u3051\u3001\u8a08\u7b97\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002
        • \u6642\u9593\u3092\u8003\u616e\u3057\u306a\u3051\u308c\u3070\u3001\u3059\u3079\u3066\u306e\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u306f\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\uff08\u529b\u4efb\u305b\u63a2\u7d22\uff09\u3092\u4f7f\u7528\u3057\u3066\u89e3\u6c7a\u3067\u304d\u307e\u3059\u304c\u3001\u518d\u5e30\u6728\u306b\u306f\u591a\u304f\u306e\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u304c\u3042\u308a\u3001\u52b9\u7387\u304c\u975e\u5e38\u306b\u4f4e\u304f\u306a\u308a\u307e\u3059\u3002\u8a18\u61b6\u5316\u30ea\u30b9\u30c8\u3092\u5c0e\u5165\u3059\u308b\u3053\u3068\u3067\u3001\u8a08\u7b97\u3055\u308c\u305f\u3059\u3079\u3066\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4fdd\u5b58\u3057\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u304c\u4e00\u5ea6\u3060\u3051\u8a08\u7b97\u3055\u308c\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3067\u304d\u307e\u3059\u3002
        • \u8a18\u61b6\u5316\u63a2\u7d22\u306f\u30c8\u30c3\u30d7\u30c0\u30a6\u30f3\u306e\u518d\u5e30\u89e3\u6cd5\u3067\u3042\u308a\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u30dc\u30c8\u30e0\u30a2\u30c3\u30d7\u306e\u53cd\u5fa9\u30a2\u30d7\u30ed\u30fc\u30c1\u306b\u5bfe\u5fdc\u3057\u3001\u300c\u8868\u3092\u57cb\u3081\u308b\u300d\u3053\u3068\u306b\u4f3c\u3066\u3044\u307e\u3059\u3002\u73fe\u5728\u306e\u72b6\u614b\u306f\u7279\u5b9a\u306e\u5c40\u6240\u72b6\u614b\u306e\u307f\u306b\u4f9d\u5b58\u3059\u308b\u305f\u3081\u3001dp\u30c6\u30fc\u30d6\u30eb\u306e1\u6b21\u5143\u3092\u524a\u9664\u3057\u3066\u7a7a\u9593\u8a08\u7b97\u91cf\u3092\u524a\u6e1b\u3067\u304d\u307e\u3059\u3002
        • \u90e8\u5206\u554f\u984c\u306e\u5206\u89e3\u306f\u6c4e\u7528\u7684\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30a2\u30d7\u30ed\u30fc\u30c1\u3067\u3042\u308a\u3001\u5206\u5272\u7d71\u6cbb\u6cd5\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3067\u7279\u5fb4\u304c\u7570\u306a\u308a\u307e\u3059\u3002
        • \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u306b\u306f3\u3064\u306e\u4e3b\u8981\u306a\u7279\u5fb4\u304c\u3042\u308a\u307e\u3059\uff1a\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u3001\u6700\u9069\u90e8\u5206\u69cb\u9020\u3001\u7121\u8a18\u61b6\u6027\u3002
        • \u5143\u306e\u554f\u984c\u306e\u6700\u9069\u89e3\u304c\u305d\u306e\u90e8\u5206\u554f\u984c\u306e\u6700\u9069\u89e3\u304b\u3089\u69cb\u7bc9\u3067\u304d\u308b\u5834\u5408\u3001\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u6301\u3061\u307e\u3059\u3002
        • \u7121\u8a18\u61b6\u6027\u3068\u306f\u3001\u72b6\u614b\u306e\u5c06\u6765\u306e\u767a\u5c55\u304c\u73fe\u5728\u306e\u72b6\u614b\u306e\u307f\u306b\u4f9d\u5b58\u3057\u3001\u904e\u53bb\u306b\u7d4c\u9a13\u3057\u305f\u3059\u3079\u3066\u306e\u72b6\u614b\u306b\u4f9d\u5b58\u3057\u306a\u3044\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u591a\u304f\u306e\u7d44\u307f\u5408\u308f\u305b\u6700\u9069\u5316\u554f\u984c\u306b\u306f\u3053\u306e\u7279\u6027\u304c\u306a\u304f\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u3066\u8fc5\u901f\u306b\u89e3\u6c7a\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002

        \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c

        • \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306f\u6700\u3082\u5178\u578b\u7684\u306a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u306e1\u3064\u3067\u30010-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u3001\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u3001\u8907\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306a\u3069\u306e\u5909\u7a2e\u304c\u3042\u308a\u307e\u3059\u3002
        • 0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u72b6\u614b\u5b9a\u7fa9\u306f\u3001\u6700\u521d\u306e \\(i\\) \u500b\u306e\u30a2\u30a4\u30c6\u30e0\u3092\u542b\u3080\u5bb9\u91cf \\(c\\) \u306e\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u3067\u306e\u6700\u5927\u5024\u3067\u3059\u3002\u30a2\u30a4\u30c6\u30e0\u3092\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u306a\u3044\u307e\u305f\u306f\u5165\u308c\u308b\u3068\u3044\u3046\u6c7a\u5b9a\u306b\u57fa\u3065\u3044\u3066\u3001\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u7279\u5b9a\u3057\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3092\u69cb\u7bc9\u3067\u304d\u307e\u3059\u3002\u7a7a\u9593\u6700\u9069\u5316\u3067\u306f\u3001\u5404\u72b6\u614b\u304c\u76f4\u63a5\u4e0a\u3068\u5de6\u4e0a\u306e\u72b6\u614b\u306b\u4f9d\u5b58\u3059\u308b\u305f\u3081\u3001\u5de6\u4e0a\u306e\u72b6\u614b\u306e\u4e0a\u66f8\u304d\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u30ea\u30b9\u30c8\u3092\u9006\u9806\u3067\u8d70\u67fb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3067\u306f\u3001\u5404\u7a2e\u985e\u306e\u30a2\u30a4\u30c6\u30e0\u3092\u9078\u629e\u3067\u304d\u308b\u6570\u306b\u5236\u9650\u304c\u306a\u3044\u305f\u3081\u3001\u30a2\u30a4\u30c6\u30e0\u3092\u542b\u3081\u308b\u72b6\u614b\u9077\u79fb\u306f0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u3068\u7570\u306a\u308a\u307e\u3059\u3002\u72b6\u614b\u304c\u76f4\u63a5\u4e0a\u3068\u5de6\u306e\u72b6\u614b\u306b\u4f9d\u5b58\u3059\u308b\u305f\u3081\u3001\u7a7a\u9593\u6700\u9069\u5316\u3067\u306f\u524d\u65b9\u8d70\u67fb\u3092\u542b\u3081\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u306f\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u5909\u7a2e\u3067\u3001\u300c\u6700\u5927\u300d\u5024\u3092\u6c42\u3081\u308b\u3053\u3068\u304b\u3089\u300c\u6700\u5c0f\u300d\u30b3\u30a4\u30f3\u6570\u3092\u6c42\u3081\u308b\u3053\u3068\u306b\u5909\u308f\u308a\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u306f \\(\\max()\\) \u3092 \\(\\min()\\) \u306b\u5909\u66f4\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u5bb9\u91cf\u3092\u300c\u8d85\u3048\u306a\u3044\u300d\u3053\u3068\u3092\u8ffd\u6c42\u3059\u308b\u3053\u3068\u304b\u3089\u3001\u6b63\u78ba\u306b\u76ee\u6a19\u91d1\u984d\u3092\u6c42\u3081\u308b\u3053\u3068\u306b\u5909\u308f\u308a\u3001\u300c\u76ee\u6a19\u91d1\u984d\u3092\u69cb\u6210\u3067\u304d\u306a\u3044\u300d\u7121\u52b9\u89e3\u3092\u8868\u3059\u305f\u3081\u306b \\(amt + 1\\) \u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984cII\u306f\u300c\u6700\u5c0f\u30b3\u30a4\u30f3\u6570\u300d\u3092\u6c42\u3081\u308b\u3053\u3068\u304b\u3089\u300c\u30b3\u30a4\u30f3\u306e\u7d44\u307f\u5408\u308f\u305b\u6570\u300d\u3092\u6c42\u3081\u308b\u3053\u3068\u306b\u5909\u308f\u308a\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3092 \\(\\min()\\) \u304b\u3089\u548c\u7b97\u6f14\u7b97\u5b50\u306b\u5909\u66f4\u3057\u307e\u3059\u3002

        \u7de8\u96c6\u8ddd\u96e2\u554f\u984c

        • \u7de8\u96c6\u8ddd\u96e2\uff08\u30ec\u30fc\u30d9\u30f3\u30b7\u30e5\u30bf\u30a4\u30f3\u8ddd\u96e2\uff09\u306f2\u3064\u306e\u6587\u5b57\u5217\u9593\u306e\u985e\u4f3c\u5ea6\u3092\u6e2c\u5b9a\u3057\u3001\u4e00\u3064\u306e\u6587\u5b57\u5217\u3092\u5225\u306e\u6587\u5b57\u5217\u306b\u5909\u66f4\u3059\u308b\u305f\u3081\u306b\u5fc5\u8981\u306a\u6700\u5c0f\u7de8\u96c6\u30b9\u30c6\u30c3\u30d7\u6570\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u3001\u7de8\u96c6\u64cd\u4f5c\u306b\u306f\u8ffd\u52a0\u3001\u524a\u9664\u3001\u7f6e\u63db\u304c\u542b\u307e\u308c\u307e\u3059\u3002
        • \u7de8\u96c6\u8ddd\u96e2\u554f\u984c\u306e\u72b6\u614b\u5b9a\u7fa9\u306f\u3001\\(s\\) \u306e\u6700\u521d\u306e \\(i\\) \u6587\u5b57\u3092 \\(t\\) \u306e\u6700\u521d\u306e \\(j\\) \u6587\u5b57\u306b\u5909\u66f4\u3059\u308b\u305f\u3081\u306b\u5fc5\u8981\u306a\u6700\u5c0f\u7de8\u96c6\u30b9\u30c6\u30c3\u30d7\u6570\u3067\u3059\u3002\\(s[i] \\ne t[j]\\) \u306e\u5834\u5408\u3001\u8ffd\u52a0\u3001\u524a\u9664\u3001\u7f6e\u63db\u306e3\u3064\u306e\u6c7a\u5b9a\u304c\u3042\u308a\u3001\u305d\u308c\u305e\u308c\u306b\u5bfe\u5fdc\u3059\u308b\u6b8b\u4f59\u90e8\u5206\u554f\u984c\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u304b\u3089\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u7279\u5b9a\u3057\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3092\u69cb\u7bc9\u3067\u304d\u307e\u3059\u3002\\(s[i] = t[j]\\) \u306e\u5834\u5408\u3001\u73fe\u5728\u306e\u6587\u5b57\u306e\u7de8\u96c6\u306f\u5fc5\u8981\u3042\u308a\u307e\u305b\u3093\u3002
        • \u7de8\u96c6\u8ddd\u96e2\u3067\u306f\u3001\u72b6\u614b\u304c\u76f4\u63a5\u4e0a\u3001\u5de6\u3001\u5de6\u4e0a\u306e\u72b6\u614b\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u7a7a\u9593\u6700\u9069\u5316\u5f8c\u3001\u524d\u65b9\u8d70\u67fb\u3082\u9006\u8d70\u67fb\u3082\u6b63\u3057\u304f\u72b6\u614b\u9077\u79fb\u3092\u5b9f\u884c\u3067\u304d\u307e\u305b\u3093\u3002\u3053\u308c\u306b\u5bfe\u51e6\u3059\u308b\u305f\u3081\u3001\u5909\u6570\u3092\u4f7f\u7528\u3057\u3066\u5de6\u4e0a\u306e\u72b6\u614b\u3092\u4e00\u6642\u7684\u306b\u4fdd\u5b58\u3057\u3001\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u72b6\u6cc1\u3068\u540c\u7b49\u306b\u3057\u3001\u7a7a\u9593\u6700\u9069\u5316\u5f8c\u306b\u524d\u65b9\u8d70\u67fb\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002
        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/","title":"14.5 \u00a0 \u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c","text":"

        \u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u307e\u305a\u5225\u306e\u4e00\u822c\u7684\u306a\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3067\u3042\u308b\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u3092\u89e3\u6c7a\u3057\u3001\u6b21\u306b\u305d\u306e\u7279\u6b8a\u30b1\u30fc\u30b9\u3067\u3042\u308b\u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u3092\u63a2\u7d22\u3057\u307e\u3059\u3002

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1451","title":"14.5.1 \u00a0 \u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c","text":"

        Question

        \\(n\\) \u500b\u306e\u30a2\u30a4\u30c6\u30e0\u304c\u4e0e\u3048\u3089\u308c\u3001\\(i\\) \u756a\u76ee\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u91cd\u91cf\u306f \\(wgt[i-1]\\) \u3067\u5024\u306f \\(val[i-1]\\) \u3067\u3059\u3002\u5bb9\u91cf\u304c \\(cap\\) \u306e\u30d0\u30c3\u30af\u30d1\u30c3\u30af\u304c\u3042\u308a\u307e\u3059\u3002\u5404\u30a2\u30a4\u30c6\u30e0\u306f\u8907\u6570\u56de\u9078\u629e\u3067\u304d\u307e\u3059\u3002\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u3053\u3068\u306a\u304f\u30d0\u30c3\u30af\u30d1\u30c3\u30af\u306b\u5165\u308c\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u30a2\u30a4\u30c6\u30e0\u306e\u6700\u5927\u5024\u306f\u4f55\u3067\u3059\u304b\uff1f\u4ee5\u4e0b\u306e\u4f8b\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u56f3 14-22 \u00a0 \u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u4f8b\u30c7\u30fc\u30bf

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1","title":"1. \u00a0 \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30a2\u30d7\u30ed\u30fc\u30c1","text":"

        \u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306f0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3068\u975e\u5e38\u306b\u4f3c\u3066\u304a\u308a\u3001\u552f\u4e00\u306e\u9055\u3044\u306f\u30a2\u30a4\u30c6\u30e0\u3092\u9078\u629e\u3067\u304d\u308b\u56de\u6570\u306b\u5236\u9650\u304c\u306a\u3044\u3053\u3068\u3067\u3059\u3002

        • 0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3067\u306f\u3001\u5404\u30a2\u30a4\u30c6\u30e0\u306f1\u3064\u3057\u304b\u306a\u3044\u305f\u3081\u3001\u30a2\u30a4\u30c6\u30e0 \\(i\\) \u3092\u30d0\u30c3\u30af\u30d1\u30c3\u30af\u306b\u5165\u308c\u305f\u5f8c\u306f\u3001\u524d\u306e \\(i-1\\) \u500b\u306e\u30a2\u30a4\u30c6\u30e0\u304b\u3089\u306e\u307f\u9078\u629e\u3067\u304d\u307e\u3059\u3002
        • \u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3067\u306f\u3001\u5404\u30a2\u30a4\u30c6\u30e0\u306e\u6570\u91cf\u306f\u7121\u5236\u9650\u3067\u3042\u308b\u305f\u3081\u3001\u30a2\u30a4\u30c6\u30e0 \\(i\\) \u3092\u30d0\u30c3\u30af\u30d1\u30c3\u30af\u306b\u5165\u308c\u305f\u5f8c\u3082\u3001\u524d\u306e \\(i\\) \u500b\u306e\u30a2\u30a4\u30c6\u30e0\u304b\u3089\u5f15\u304d\u7d9a\u304d\u9078\u629e\u3067\u304d\u307e\u3059\u3002

        \u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u30eb\u30fc\u30eb\u306e\u4e0b\u3067\u3001\u72b6\u614b \\([i, c]\\) \u306f2\u3064\u306e\u65b9\u6cd5\u3067\u5909\u5316\u3067\u304d\u307e\u3059\u3002

        • \u30a2\u30a4\u30c6\u30e0 \\(i\\) \u3092\u5165\u308c\u306a\u3044\uff1a0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3068\u540c\u69d8\u306b\u3001\\([i-1, c]\\) \u306b\u9077\u79fb\u3057\u307e\u3059\u3002
        • \u30a2\u30a4\u30c6\u30e0 \\(i\\) \u3092\u5165\u308c\u308b\uff1a0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3068\u306f\u7570\u306a\u308a\u3001\\([i, c-wgt[i-1]]\\) \u306b\u9077\u79fb\u3057\u307e\u3059\u3002

        \u3057\u305f\u304c\u3063\u3066\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u306f\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\uff1a

        \\[ dp[i, c] = \\max(dp[i-1, c], dp[i, c - wgt[i-1]] + val[i-1]) \\]"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2","title":"2. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        2\u3064\u306e\u554f\u984c\u306e\u30b3\u30fc\u30c9\u3092\u6bd4\u8f03\u3059\u308b\u3068\u3001\u72b6\u614b\u9077\u79fb\u304c \\(i-1\\) \u304b\u3089 \\(i\\) \u306b\u5909\u308f\u308a\u3001\u6b8b\u308a\u306f\u5b8c\u5168\u306b\u540c\u4e00\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby unbounded_knapsack.py
        def unbounded_knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"\u5b8c\u5168\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n = len(wgt)\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    dp = [[0] * (cap + 1) for _ in range(n + 1)]\n    # \u72b6\u614b\u9077\u79fb\n    for i in range(1, n + 1):\n        for c in range(1, cap + 1):\n            if wgt[i - 1] > c:\n                # \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][c] = dp[i - 1][c]\n            else:\n                # \u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\u306e\u3068\u9078\u629e\u3059\u308b\u306e\u3068\u3067\u5927\u304d\u3044\u5024\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\n    return dp[n][cap]\n
        unbounded_knapsack.cpp
        /* \u5b8c\u5168\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint unboundedKnapsackDP(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5927\u304d\u3044\u5024\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        unbounded_knapsack.java
        /* \u5b8c\u5168\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint unboundedKnapsackDP(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    int[][] dp = new int[n + 1][cap + 1];\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5927\u304d\u3044\u5024\n                dp[i][c] = Math.max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        unbounded_knapsack.cs
        [class]{unbounded_knapsack}-[func]{UnboundedKnapsackDP}\n
        unbounded_knapsack.go
        [class]{}-[func]{unboundedKnapsackDP}\n
        unbounded_knapsack.swift
        [class]{}-[func]{unboundedKnapsackDP}\n
        unbounded_knapsack.js
        [class]{}-[func]{unboundedKnapsackDP}\n
        unbounded_knapsack.ts
        [class]{}-[func]{unboundedKnapsackDP}\n
        unbounded_knapsack.dart
        [class]{}-[func]{unboundedKnapsackDP}\n
        unbounded_knapsack.rs
        [class]{}-[func]{unbounded_knapsack_dp}\n
        unbounded_knapsack.c
        [class]{}-[func]{unboundedKnapsackDP}\n
        unbounded_knapsack.kt
        [class]{}-[func]{unboundedKnapsackDP}\n
        unbounded_knapsack.rb
        [class]{}-[func]{unbounded_knapsack_dp}\n
        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3","title":"3. \u00a0 \u7a7a\u9593\u6700\u9069\u5316","text":"

        \u73fe\u5728\u306e\u72b6\u614b\u306f\u5de6\u3068\u4e0a\u306e\u72b6\u614b\u304b\u3089\u6765\u308b\u305f\u3081\u3001\u7a7a\u9593\u6700\u9069\u5316\u89e3\u6cd5\u306f \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u306e\u5404\u884c\u306b\u5bfe\u3057\u3066\u524d\u65b9\u8d70\u67fb\u3092\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u3053\u306e\u8d70\u67fb\u9806\u5e8f\u306f0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u5834\u5408\u3068\u306f\u9006\u3067\u3059\u3002\u9055\u3044\u3092\u7406\u89e3\u3059\u308b\u305f\u3081\u306b\u4e0b\u306e\u56f3\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        <1><2><3><4><5><6>

        \u56f3 14-23 \u00a0 \u7a7a\u9593\u6700\u9069\u5316\u5f8c\u306e\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9

        \u30b3\u30fc\u30c9\u5b9f\u88c5\u306f\u975e\u5e38\u306b\u7c21\u5358\u3067\u3001\u914d\u5217 dp \u306e\u6700\u521d\u306e\u6b21\u5143\u3092\u524a\u9664\u3059\u308b\u3060\u3051\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby unbounded_knapsack.py
        def unbounded_knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"\u5b8c\u5168\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n = len(wgt)\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    dp = [0] * (cap + 1)\n    # \u72b6\u614b\u9077\u79fb\n    for i in range(1, n + 1):\n        # \u9806\u5e8f\u3067\u8d70\u67fb\n        for c in range(1, cap + 1):\n            if wgt[i - 1] > c:\n                # \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[c] = dp[c]\n            else:\n                # \u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\u306e\u3068\u9078\u629e\u3059\u308b\u306e\u3068\u3067\u5927\u304d\u3044\u5024\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n    return dp[cap]\n
        unbounded_knapsack.cpp
        /* \u5b8c\u5168\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint unboundedKnapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    vector<int> dp(cap + 1, 0);\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[c] = dp[c];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5927\u304d\u3044\u5024\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        unbounded_knapsack.java
        /* \u5b8c\u5168\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint unboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    int[] dp = new int[cap + 1];\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[c] = dp[c];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5927\u304d\u3044\u5024\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        unbounded_knapsack.cs
        [class]{unbounded_knapsack}-[func]{UnboundedKnapsackDPComp}\n
        unbounded_knapsack.go
        [class]{}-[func]{unboundedKnapsackDPComp}\n
        unbounded_knapsack.swift
        [class]{}-[func]{unboundedKnapsackDPComp}\n
        unbounded_knapsack.js
        [class]{}-[func]{unboundedKnapsackDPComp}\n
        unbounded_knapsack.ts
        [class]{}-[func]{unboundedKnapsackDPComp}\n
        unbounded_knapsack.dart
        [class]{}-[func]{unboundedKnapsackDPComp}\n
        unbounded_knapsack.rs
        [class]{}-[func]{unbounded_knapsack_dp_comp}\n
        unbounded_knapsack.c
        [class]{}-[func]{unboundedKnapsackDPComp}\n
        unbounded_knapsack.kt
        [class]{}-[func]{unboundedKnapsackDPComp}\n
        unbounded_knapsack.rb
        [class]{}-[func]{unbounded_knapsack_dp_comp}\n
        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1452","title":"14.5.2 \u00a0 \u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c","text":"

        \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306f\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u306e\u5927\u304d\u306a\u30af\u30e9\u30b9\u306e\u4ee3\u8868\u3067\u3042\u308a\u3001\u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u306a\u3069\u591a\u304f\u306e\u5909\u7a2e\u304c\u3042\u308a\u307e\u3059\u3002

        Question

        \\(n\\) \u7a2e\u985e\u306e\u30b3\u30a4\u30f3\u304c\u4e0e\u3048\u3089\u308c\u3001\\(i\\) \u756a\u76ee\u306e\u7a2e\u985e\u306e\u30b3\u30a4\u30f3\u306e\u984d\u9762\u306f \\(coins[i - 1]\\) \u3067\u3001\u76ee\u6a19\u91d1\u984d\u306f \\(amt\\) \u3067\u3059\u3002\u5404\u7a2e\u985e\u306e\u30b3\u30a4\u30f3\u306f\u8907\u6570\u56de\u9078\u629e\u3067\u304d\u307e\u3059\u3002\u76ee\u6a19\u91d1\u984d\u3092\u69cb\u6210\u3059\u308b\u306e\u306b\u5fc5\u8981\u306a\u6700\u5c0f\u30b3\u30a4\u30f3\u6570\u306f\u4f55\u3067\u3059\u304b\uff1f\u76ee\u6a19\u91d1\u984d\u3092\u69cb\u6210\u3067\u304d\u306a\u3044\u5834\u5408\u306f \\(-1\\) \u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4ee5\u4e0b\u306e\u4f8b\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u56f3 14-24 \u00a0 \u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u306e\u4f8b\u30c7\u30fc\u30bf

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1_1","title":"1. \u00a0 \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30a2\u30d7\u30ed\u30fc\u30c1","text":"

        \u30b3\u30a4\u30f3\u4ea4\u63db\u306f\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u7279\u6b8a\u30b1\u30fc\u30b9\u3068\u898b\u306a\u3059\u3053\u3068\u304c\u3067\u304d\u3001\u4ee5\u4e0b\u306e\u985e\u4f3c\u70b9\u3068\u76f8\u9055\u70b9\u3092\u5171\u6709\u3057\u3066\u3044\u307e\u3059\u3002

        • 2\u3064\u306e\u554f\u984c\u306f\u4e92\u3044\u306b\u5909\u63db\u3067\u304d\u307e\u3059\uff1a\u300c\u30a2\u30a4\u30c6\u30e0\u300d\u306f\u300c\u30b3\u30a4\u30f3\u300d\u306b\u5bfe\u5fdc\u3057\u3001\u300c\u30a2\u30a4\u30c6\u30e0\u306e\u91cd\u91cf\u300d\u306f\u300c\u30b3\u30a4\u30f3\u306e\u984d\u9762\u300d\u306b\u5bfe\u5fdc\u3057\u3001\u300c\u30d0\u30c3\u30af\u30d1\u30c3\u30af\u306e\u5bb9\u91cf\u300d\u306f\u300c\u76ee\u6a19\u91d1\u984d\u300d\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002
        • \u6700\u9069\u5316\u76ee\u6a19\u306f\u9006\u3067\u3059\uff1a\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306f\u30a2\u30a4\u30c6\u30e0\u306e\u5024\u3092\u6700\u5927\u5316\u3059\u308b\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u3001\u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u306f\u30b3\u30a4\u30f3\u306e\u6570\u3092\u6700\u5c0f\u5316\u3059\u308b\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u307e\u3059\u3002
        • \u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306f\u30d0\u30c3\u30af\u30d1\u30c3\u30af\u5bb9\u91cf\u3092\u300c\u8d85\u3048\u306a\u3044\u300d\u89e3\u3092\u6c42\u3081\u3001\u30b3\u30a4\u30f3\u4ea4\u63db\u306f\u76ee\u6a19\u91d1\u984d\u3092\u300c\u6b63\u78ba\u306b\u300d\u69cb\u6210\u3059\u308b\u89e3\u3092\u6c42\u3081\u307e\u3059\u3002

        \u7b2c1\u30b9\u30c6\u30c3\u30d7\uff1a\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u610f\u601d\u6c7a\u5b9a\u3092\u8003\u3048\u3001\u72b6\u614b\u3092\u5b9a\u7fa9\u3057\u3001\u305d\u308c\u306b\u3088\u308a \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u3092\u5c0e\u51fa\u3059\u308b

        \u72b6\u614b \\([i, a]\\) \u306f\u90e8\u5206\u554f\u984c\u306b\u5bfe\u5fdc\u3057\u307e\u3059\uff1a\u6700\u521d\u306e \\(i\\) \u7a2e\u985e\u306e\u30b3\u30a4\u30f3\u3092\u4f7f\u7528\u3057\u3066\u91d1\u984d \\(a\\) \u3092\u69cb\u6210\u3067\u304d\u308b\u6700\u5c0f\u30b3\u30a4\u30f3\u6570\u3001\\(dp[i, a]\\) \u3068\u8868\u8a18\u3055\u308c\u307e\u3059\u3002

        \u4e8c\u6b21\u5143 \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u306e\u30b5\u30a4\u30ba\u306f \\((n+1) \\times (amt+1)\\) \u3067\u3059\u3002

        \u7b2c2\u30b9\u30c6\u30c3\u30d7\uff1a\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u7279\u5b9a\u3057\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3092\u5c0e\u51fa\u3059\u308b

        \u3053\u306e\u554f\u984c\u306f\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u306e2\u3064\u306e\u5074\u9762\u3067\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3068\u7570\u306a\u308a\u307e\u3059\u3002

        • \u3053\u306e\u554f\u984c\u306f\u6700\u5c0f\u5024\u3092\u6c42\u3081\u308b\u305f\u3081\u3001\u6f14\u7b97\u5b50 \\(\\max()\\) \u3092 \\(\\min()\\) \u306b\u5909\u66f4\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u6700\u9069\u5316\u306f\u30b3\u30a4\u30f3\u306e\u6570\u306b\u7126\u70b9\u3092\u5f53\u3066\u3066\u3044\u308b\u305f\u3081\u3001\u30b3\u30a4\u30f3\u304c\u9078\u629e\u3055\u308c\u305f\u3068\u304d\u306b\u5358\u7d14\u306b \\(+1\\) \u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002
        \\[ dp[i, a] = \\min(dp[i-1, a], dp[i, a - coins[i-1]] + 1) \\]

        \u7b2c3\u30b9\u30c6\u30c3\u30d7\uff1a\u5883\u754c\u6761\u4ef6\u3068\u72b6\u614b\u9077\u79fb\u9806\u5e8f\u3092\u5b9a\u7fa9\u3059\u308b

        \u76ee\u6a19\u91d1\u984d\u304c \\(0\\) \u306e\u5834\u5408\u3001\u305d\u308c\u3092\u69cb\u6210\u3059\u308b\u306e\u306b\u5fc5\u8981\u306a\u6700\u5c0f\u30b3\u30a4\u30f3\u6570\u306f \\(0\\) \u3067\u3042\u308b\u305f\u3081\u3001\u6700\u521d\u306e\u5217\u306e\u3059\u3079\u3066\u306e \\(dp[i, 0]\\) \u306f \\(0\\) \u3067\u3059\u3002

        \u30b3\u30a4\u30f3\u304c\u306a\u3044\u5834\u5408\u3001**\u4efb\u610f\u306e\u91d1\u984d >0 \u3092\u69cb\u6210\u3059\u308b\u3053\u3068\u306f\u4e0d\u53ef\u80fd**\u3067\u3042\u308a\u3001\u3053\u308c\u306f\u7121\u52b9\u306a\u89e3\u3067\u3059\u3002\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u306e \\(\\min()\\) \u95a2\u6570\u304c\u7121\u52b9\u306a\u89e3\u3092\u8a8d\u8b58\u3057\u3066\u30d5\u30a3\u30eb\u30bf\u30ea\u30f3\u30b0\u3067\u304d\u308b\u3088\u3046\u306b\u3001\\(+\\infty\\) \u3092\u4f7f\u7528\u3057\u3066\u305d\u308c\u3089\u3092\u8868\u73fe\u3059\u308b\u3053\u3068\u3092\u691c\u8a0e\u3057\u3001\u3064\u307e\u308a\u6700\u521d\u306e\u884c\u306e\u3059\u3079\u3066\u306e \\(dp[0, a]\\) \u3092 \\(+\\infty\\) \u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2_1","title":"2. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        \u307b\u3068\u3093\u3069\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306f \\(+\\infty\\) \u5909\u6570\u3092\u63d0\u4f9b\u3057\u3066\u304a\u3089\u305a\u3001\u6574\u6570 int \u306e\u6700\u5927\u5024\u306e\u307f\u3092\u4ee3\u66ff\u3068\u3057\u3066\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u304c\u767a\u751f\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\uff1a\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u306e \\(+1\\) \u6f14\u7b97\u304c\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        \u3053\u306e\u7406\u7531\u3067\u3001\u6570\u5024 \\(amt + 1\\) \u3092\u4f7f\u7528\u3057\u3066\u7121\u52b9\u306a\u89e3\u3092\u8868\u3057\u307e\u3059\u3002\u306a\u305c\u306a\u3089\u3001\\(amt\\) \u3092\u69cb\u6210\u3059\u308b\u306e\u306b\u5fc5\u8981\u306a\u6700\u5927\u30b3\u30a4\u30f3\u6570\u306f\u6700\u5927\u3067\u3082 \\(amt\\) \u3060\u304b\u3089\u3067\u3059\u3002\u7d50\u679c\u3092\u8fd4\u3059\u524d\u306b\u3001\\(dp[n, amt]\\) \u304c \\(amt + 1\\) \u306b\u7b49\u3057\u3044\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\u3057\u3001\u305d\u3046\u3067\u3042\u308c\u3070 \\(-1\\) \u3092\u8fd4\u3057\u3001\u76ee\u6a19\u91d1\u984d\u3092\u69cb\u6210\u3067\u304d\u306a\u3044\u3053\u3068\u3092\u793a\u3057\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change.py
        def coin_change_dp(coins: list[int], amt: int) -> int:\n    \"\"\"\u786c\u8ca8\u4ea4\u63db\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n = len(coins)\n    MAX = amt + 1\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    dp = [[0] * (amt + 1) for _ in range(n + 1)]\n    # \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\u3068\u6700\u521d\u306e\u5217\n    for a in range(1, amt + 1):\n        dp[0][a] = MAX\n    # \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\u3068\u5217\n    for i in range(1, n + 1):\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][a] = dp[i - 1][a]\n            else:\n                # \u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\u306e\u3068\u9078\u629e\u3059\u308b\u306e\u3068\u3067\u5c0f\u3055\u3044\u5024\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n    return dp[n][amt] if dp[n][amt] != MAX else -1\n
        coin_change.cpp
        /* \u786c\u8ca8\u4e21\u66ff\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint coinChangeDP(vector<int> &coins, int amt) {\n    int n = coins.size();\n    int MAX = amt + 1;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    vector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\u3068\u6700\u521d\u306e\u5217\n    for (int a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\u3068\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u786c\u8ca8 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5c0f\u3055\u3044\u5024\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
        coin_change.java
        /* \u786c\u8ca8\u4e21\u66ff\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint coinChangeDP(int[] coins, int amt) {\n    int n = coins.length;\n    int MAX = amt + 1;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    int[][] dp = new int[n + 1][amt + 1];\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\u3068\u6700\u521d\u306e\u5217\n    for (int a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\u3068\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u786c\u8ca8 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5c0f\u3055\u3044\u5024\n                dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
        coin_change.cs
        [class]{coin_change}-[func]{CoinChangeDP}\n
        coin_change.go
        [class]{}-[func]{coinChangeDP}\n
        coin_change.swift
        [class]{}-[func]{coinChangeDP}\n
        coin_change.js
        [class]{}-[func]{coinChangeDP}\n
        coin_change.ts
        [class]{}-[func]{coinChangeDP}\n
        coin_change.dart
        [class]{}-[func]{coinChangeDP}\n
        coin_change.rs
        [class]{}-[func]{coin_change_dp}\n
        coin_change.c
        [class]{}-[func]{coinChangeDP}\n
        coin_change.kt
        [class]{}-[func]{coinChangeDP}\n
        coin_change.rb
        [class]{}-[func]{coin_change_dp}\n

        \u4e0b\u306e\u56f3\u306f\u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u306e\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9\u3092\u793a\u3057\u3066\u304a\u308a\u3001\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3068\u975e\u5e38\u306b\u4f3c\u3066\u3044\u307e\u3059\u3002

        <1><2><3><4><5><6><7><8><9><10><11><12><13><14><15>

        \u56f3 14-25 \u00a0 \u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u306e\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3_1","title":"3. \u00a0 \u7a7a\u9593\u6700\u9069\u5316","text":"

        \u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u306e\u7a7a\u9593\u6700\u9069\u5316\u306f\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3068\u540c\u3058\u65b9\u6cd5\u3067\u51e6\u7406\u3055\u308c\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change.py
        def coin_change_dp_comp(coins: list[int], amt: int) -> int:\n    \"\"\"\u786c\u8ca8\u4ea4\u63db\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n = len(coins)\n    MAX = amt + 1\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    dp = [MAX] * (amt + 1)\n    dp[0] = 0\n    # \u72b6\u614b\u9077\u79fb\n    for i in range(1, n + 1):\n        # \u9806\u5e8f\u3067\u8d70\u67fb\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[a] = dp[a]\n            else:\n                # \u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\u306e\u3068\u9078\u629e\u3059\u308b\u306e\u3068\u3067\u5c0f\u3055\u3044\u5024\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n    return dp[amt] if dp[amt] != MAX else -1\n
        coin_change.cpp
        /* \u786c\u8ca8\u4e21\u66ff\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint coinChangeDPComp(vector<int> &coins, int amt) {\n    int n = coins.size();\n    int MAX = amt + 1;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    vector<int> dp(amt + 1, MAX);\n    dp[0] = 0;\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[a] = dp[a];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u786c\u8ca8 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5c0f\u3055\u3044\u5024\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1;\n}\n
        coin_change.java
        /* \u786c\u8ca8\u4e21\u66ff\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint coinChangeDPComp(int[] coins, int amt) {\n    int n = coins.length;\n    int MAX = amt + 1;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    int[] dp = new int[amt + 1];\n    Arrays.fill(dp, MAX);\n    dp[0] = 0;\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[a] = dp[a];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u786c\u8ca8 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5c0f\u3055\u3044\u5024\n                dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1;\n}\n
        coin_change.cs
        [class]{coin_change}-[func]{CoinChangeDPComp}\n
        coin_change.go
        [class]{}-[func]{coinChangeDPComp}\n
        coin_change.swift
        [class]{}-[func]{coinChangeDPComp}\n
        coin_change.js
        [class]{}-[func]{coinChangeDPComp}\n
        coin_change.ts
        [class]{}-[func]{coinChangeDPComp}\n
        coin_change.dart
        [class]{}-[func]{coinChangeDPComp}\n
        coin_change.rs
        [class]{}-[func]{coin_change_dp_comp}\n
        coin_change.c
        [class]{}-[func]{coinChangeDPComp}\n
        coin_change.kt
        [class]{}-[func]{coinChangeDPComp}\n
        coin_change.rb
        [class]{}-[func]{coin_change_dp_comp}\n
        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1453-ii","title":"14.5.3 \u00a0 \u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984cII","text":"

        Question

        \\(n\\) \u7a2e\u985e\u306e\u30b3\u30a4\u30f3\u304c\u4e0e\u3048\u3089\u308c\u3001\\(i\\) \u756a\u76ee\u306e\u7a2e\u985e\u306e\u30b3\u30a4\u30f3\u306e\u984d\u9762\u306f \\(coins[i - 1]\\) \u3067\u3001\u76ee\u6a19\u91d1\u984d\u306f \\(amt\\) \u3067\u3059\u3002\u5404\u7a2e\u985e\u306e\u30b3\u30a4\u30f3\u306f\u8907\u6570\u56de\u9078\u629e\u3067\u304d\u3001\u76ee\u6a19\u91d1\u984d\u3092\u69cb\u6210\u3067\u304d\u308b\u30b3\u30a4\u30f3\u306e\u7d44\u307f\u5408\u308f\u305b\u306f\u4f55\u901a\u308a\u3042\u308a\u307e\u3059\u304b\u3002\u4ee5\u4e0b\u306e\u4f8b\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u56f3 14-26 \u00a0 \u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984cII\u306e\u4f8b\u30c7\u30fc\u30bf

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1_2","title":"1. \u00a0 \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30a2\u30d7\u30ed\u30fc\u30c1","text":"

        \u524d\u306e\u554f\u984c\u3068\u6bd4\u8f03\u3057\u3066\u3001\u3053\u306e\u554f\u984c\u306e\u76ee\u6a19\u306f\u7d44\u307f\u5408\u308f\u305b\u306e\u6570\u3092\u6c7a\u5b9a\u3059\u308b\u3053\u3068\u3067\u3042\u308b\u305f\u3081\u3001\u90e8\u5206\u554f\u984c\u306f\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\uff1a\u6700\u521d\u306e \\(i\\) \u7a2e\u985e\u306e\u30b3\u30a4\u30f3\u3092\u4f7f\u7528\u3057\u3066\u91d1\u984d \\(a\\) \u3092\u69cb\u6210\u3067\u304d\u308b\u7d44\u307f\u5408\u308f\u305b\u306e\u6570\u3002\\(dp\\) \u30c6\u30fc\u30d6\u30eb\u306f\u30b5\u30a4\u30ba \\((n+1) \\times (amt + 1)\\) \u306e\u4e8c\u6b21\u5143\u884c\u5217\u306e\u307e\u307e\u6b8b\u308a\u307e\u3059\u3002

        \u73fe\u5728\u306e\u72b6\u614b\u306e\u7d44\u307f\u5408\u308f\u305b\u6570\u306f\u3001\u73fe\u5728\u306e\u30b3\u30a4\u30f3\u3092\u9078\u629e\u3057\u306a\u3044\u7d44\u307f\u5408\u308f\u305b\u3068\u73fe\u5728\u306e\u30b3\u30a4\u30f3\u3092\u9078\u629e\u3059\u308b\u7d44\u307f\u5408\u308f\u305b\u306e\u5408\u8a08\u3067\u3059\u3002\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u306f\uff1a

        \\[ dp[i, a] = dp[i-1, a] + dp[i, a - coins[i-1]] \\]

        \u76ee\u6a19\u91d1\u984d\u304c \\(0\\) \u306e\u5834\u5408\u3001\u76ee\u6a19\u91d1\u984d\u3092\u69cb\u6210\u3059\u308b\u306e\u306b\u30b3\u30a4\u30f3\u306f\u5fc5\u8981\u306a\u3044\u305f\u3081\u3001\u6700\u521d\u306e\u5217\u306e\u3059\u3079\u3066\u306e \\(dp[i, 0]\\) \u306f \\(1\\) \u306b\u521d\u671f\u5316\u3055\u308c\u308b\u3079\u304d\u3067\u3059\u3002\u30b3\u30a4\u30f3\u304c\u306a\u3044\u5834\u5408\u3001\u4efb\u610f\u306e\u91d1\u984d >0 \u3092\u69cb\u6210\u3059\u308b\u3053\u3068\u306f\u4e0d\u53ef\u80fd\u3067\u3042\u308b\u305f\u3081\u3001\u6700\u521d\u306e\u884c\u306e\u3059\u3079\u3066\u306e \\(dp[0, a]\\) \u306f \\(0\\) \u306b\u8a2d\u5b9a\u3055\u308c\u308b\u3079\u304d\u3067\u3059\u3002

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2_2","title":"2. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change_ii.py
        def coin_change_ii_dp(coins: list[int], amt: int) -> int:\n    \"\"\"\u786c\u8ca8\u4ea4\u63db II\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n = len(coins)\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    dp = [[0] * (amt + 1) for _ in range(n + 1)]\n    # \u6700\u521d\u306e\u5217\u3092\u521d\u671f\u5316\n    for i in range(n + 1):\n        dp[i][0] = 1\n    # \u72b6\u614b\u9077\u79fb\n    for i in range(1, n + 1):\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][a] = dp[i - 1][a]\n            else:\n                # \u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\u306e\u3068\u9078\u629e\u3059\u308b\u306e\u3068\u306e\u4e21\u65b9\u306e\u9078\u629e\u80a2\u306e\u548c\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n    return dp[n][amt]\n
        coin_change_ii.cpp
        /* \u786c\u8ca8\u4e21\u66ff II\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint coinChangeIIDP(vector<int> &coins, int amt) {\n    int n = coins.size();\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    vector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n    // \u6700\u521d\u306e\u5217\u3092\u521d\u671f\u5316\n    for (int i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u786c\u8ca8 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e2\u3064\u306e\u9078\u629e\u80a2\u306e\u5408\u8a08\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
        coin_change_ii.java
        /* \u786c\u8ca8\u4e21\u66ff II\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint coinChangeIIDP(int[] coins, int amt) {\n    int n = coins.length;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    int[][] dp = new int[n + 1][amt + 1];\n    // \u6700\u521d\u306e\u5217\u3092\u521d\u671f\u5316\n    for (int i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u786c\u8ca8 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e2\u3064\u306e\u9078\u629e\u80a2\u306e\u5408\u8a08\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
        coin_change_ii.cs
        [class]{coin_change_ii}-[func]{CoinChangeIIDP}\n
        coin_change_ii.go
        [class]{}-[func]{coinChangeIIDP}\n
        coin_change_ii.swift
        [class]{}-[func]{coinChangeIIDP}\n
        coin_change_ii.js
        [class]{}-[func]{coinChangeIIDP}\n
        coin_change_ii.ts
        [class]{}-[func]{coinChangeIIDP}\n
        coin_change_ii.dart
        [class]{}-[func]{coinChangeIIDP}\n
        coin_change_ii.rs
        [class]{}-[func]{coin_change_ii_dp}\n
        coin_change_ii.c
        [class]{}-[func]{coinChangeIIDP}\n
        coin_change_ii.kt
        [class]{}-[func]{coinChangeIIDP}\n
        coin_change_ii.rb
        [class]{}-[func]{coin_change_ii_dp}\n
        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3_2","title":"3. \u00a0 \u7a7a\u9593\u6700\u9069\u5316","text":"

        \u7a7a\u9593\u6700\u9069\u5316\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\u540c\u3058\u3067\u3001\u30b3\u30a4\u30f3\u306e\u6b21\u5143\u3092\u524a\u9664\u3059\u308b\u3060\u3051\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change_ii.py
        def coin_change_ii_dp_comp(coins: list[int], amt: int) -> int:\n    \"\"\"\u786c\u8ca8\u4ea4\u63db II\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n = len(coins)\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    dp = [0] * (amt + 1)\n    dp[0] = 1\n    # \u72b6\u614b\u9077\u79fb\n    for i in range(1, n + 1):\n        # \u9806\u5e8f\u3067\u8d70\u67fb\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[a] = dp[a]\n            else:\n                # \u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\u306e\u3068\u9078\u629e\u3059\u308b\u306e\u3068\u306e\u4e21\u65b9\u306e\u9078\u629e\u80a2\u306e\u548c\n                dp[a] = dp[a] + dp[a - coins[i - 1]]\n    return dp[amt]\n
        coin_change_ii.cpp
        /* \u786c\u8ca8\u4e21\u66ff II\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint coinChangeIIDPComp(vector<int> &coins, int amt) {\n    int n = coins.size();\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    vector<int> dp(amt + 1, 0);\n    dp[0] = 1;\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[a] = dp[a];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u786c\u8ca8 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e2\u3064\u306e\u9078\u629e\u80a2\u306e\u5408\u8a08\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
        coin_change_ii.java
        /* \u786c\u8ca8\u4e21\u66ff II\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint coinChangeIIDPComp(int[] coins, int amt) {\n    int n = coins.length;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    int[] dp = new int[amt + 1];\n    dp[0] = 1;\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[a] = dp[a];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u786c\u8ca8 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e2\u3064\u306e\u9078\u629e\u80a2\u306e\u5408\u8a08\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
        coin_change_ii.cs
        [class]{coin_change_ii}-[func]{CoinChangeIIDPComp}\n
        coin_change_ii.go
        [class]{}-[func]{coinChangeIIDPComp}\n
        coin_change_ii.swift
        [class]{}-[func]{coinChangeIIDPComp}\n
        coin_change_ii.js
        [class]{}-[func]{coinChangeIIDPComp}\n
        coin_change_ii.ts
        [class]{}-[func]{coinChangeIIDPComp}\n
        coin_change_ii.dart
        [class]{}-[func]{coinChangeIIDPComp}\n
        coin_change_ii.rs
        [class]{}-[func]{coin_change_ii_dp_comp}\n
        coin_change_ii.c
        [class]{}-[func]{coinChangeIIDPComp}\n
        coin_change_ii.kt
        [class]{}-[func]{coinChangeIIDPComp}\n
        coin_change_ii.rb
        [class]{}-[func]{coin_change_ii_dp_comp}\n
        "},{"location":"chapter_graph/","title":"\u7b2c 9 \u7ae0 \u00a0 \u30b0\u30e9\u30d5","text":"

        Abstract

        \u4eba\u751f\u306e\u65c5\u8def\u306b\u304a\u3044\u3066\u3001\u79c1\u305f\u3061\u306e\u4e00\u4eba\u4e00\u4eba\u306f\u30ce\u30fc\u30c9\u3067\u3042\u308a\u3001\u7121\u6570\u306e\u898b\u3048\u306a\u3044\u8fba\u3067\u7d50\u3070\u308c\u3066\u3044\u307e\u3059\u3002

        \u4e00\u3064\u4e00\u3064\u306e\u51fa\u4f1a\u3044\u3068\u5225\u308c\u304c\u3001\u3053\u306e\u5e83\u5927\u306a\u4eba\u751f\u306e\u30b0\u30e9\u30d5\u306b\u72ec\u7279\u306e\u5370\u3092\u6b8b\u3057\u3066\u3044\u304d\u307e\u3059\u3002

        "},{"location":"chapter_graph/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 9.1 \u00a0 \u30b0\u30e9\u30d5
        • 9.2 \u00a0 \u30b0\u30e9\u30d5\u306e\u57fa\u672c\u64cd\u4f5c
        • 9.3 \u00a0 \u30b0\u30e9\u30d5\u306e\u8d70\u67fb
        • 9.4 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_graph/graph/","title":"9.1 \u00a0 \u30b0\u30e9\u30d5","text":"

        \u30b0\u30e9\u30d5\u306f\u975e\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u4e00\u7a2e\u3067\u3001\u9802\u70b9\u3068\u8fba\u3067\u69cb\u6210\u3055\u308c\u307e\u3059\u3002\u30b0\u30e9\u30d5\\(G\\)\u306f\u3001\u9802\u70b9\u306e\u96c6\u5408\\(V\\)\u3068\u8fba\u306e\u96c6\u5408\\(E\\)\u306e\u7d44\u307f\u5408\u308f\u305b\u3068\u3057\u3066\u62bd\u8c61\u7684\u306b\u8868\u73fe\u3067\u304d\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u4f8b\u306f\u30015\u3064\u306e\u9802\u70b9\u30687\u3064\u306e\u8fba\u3092\u542b\u3080\u30b0\u30e9\u30d5\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \\[ \\begin{aligned} V & = \\{ 1, 2, 3, 4, 5 \\} \\newline E & = \\{ (1,2), (1,3), (1,5), (2,3), (2,4), (2,5), (4,5) \\} \\newline G & = \\{ V, E \\} \\newline \\end{aligned} \\]

        \u9802\u70b9\u3092\u30ce\u30fc\u30c9\u3001\u8fba\u3092\u30ce\u30fc\u30c9\u3092\u63a5\u7d9a\u3059\u308b\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3068\u898b\u306a\u3059\u3068\u3001\u30b0\u30e9\u30d5\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u304b\u3089\u62e1\u5f35\u3055\u308c\u305f\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u3057\u3066\u898b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u7dda\u5f62\u95a2\u4fc2\uff08\u9023\u7d50\u30ea\u30b9\u30c8\uff09\u3084\u5206\u5272\u7d71\u6cbb\u95a2\u4fc2\uff08\u6728\uff09\u3068\u6bd4\u8f03\u3057\u3066\u3001\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u95a2\u4fc2\uff08\u30b0\u30e9\u30d5\uff09\u306f\u81ea\u7531\u5ea6\u304c\u9ad8\u3044\u305f\u3081\u3001\u3088\u308a\u8907\u96d1\u3067\u3059\u3002

        \u56f3 9-1 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u3001\u6728\u3001\u30b0\u30e9\u30d5\u306e\u95a2\u4fc2

        "},{"location":"chapter_graph/graph/#911","title":"9.1.1 \u00a0 \u30b0\u30e9\u30d5\u306e\u4e00\u822c\u7684\u306a\u7a2e\u985e\u3068\u7528\u8a9e","text":"

        \u30b0\u30e9\u30d5\u306f\u3001\u8fba\u306b\u65b9\u5411\u304c\u3042\u308b\u304b\u3069\u3046\u304b\u306b\u3088\u3063\u3066\u7121\u5411\u30b0\u30e9\u30d5\u3068\u6709\u5411\u30b0\u30e9\u30d5\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff08\u4e0b\u56f3\u53c2\u7167\uff09\u3002

        • \u7121\u5411\u30b0\u30e9\u30d5\u3067\u306f\u3001\u8fba\u306f2\u3064\u306e\u9802\u70b9\u9593\u306e\u300c\u53cc\u65b9\u5411\u300d\u63a5\u7d9a\u3092\u8868\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001Facebook\u306e\u300c\u53cb\u9054\u300d\u95a2\u4fc2\u3067\u3059\u3002
        • \u6709\u5411\u30b0\u30e9\u30d5\u3067\u306f\u3001\u8fba\u306b\u65b9\u5411\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u8fba\\(A \\rightarrow B\\)\u3068\\(A \\leftarrow B\\)\u306f\u4e92\u3044\u306b\u72ec\u7acb\u3057\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001Instagram\u3084TikTok\u306e\u300c\u30d5\u30a9\u30ed\u30fc\u300d\u3068\u300c\u30d5\u30a9\u30ed\u30ef\u30fc\u300d\u306e\u95a2\u4fc2\u3067\u3059\u3002

        \u56f3 9-2 \u00a0 \u6709\u5411\u30b0\u30e9\u30d5\u3068\u7121\u5411\u30b0\u30e9\u30d5

        \u3059\u3079\u3066\u306e\u9802\u70b9\u304c\u63a5\u7d9a\u3055\u308c\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u306b\u3088\u3063\u3066\u3001\u30b0\u30e9\u30d5\u306f\u9023\u7d50\u30b0\u30e9\u30d5\u3068\u975e\u9023\u7d50\u30b0\u30e9\u30d5\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff08\u4e0b\u56f3\u53c2\u7167\uff09\u3002

        • \u9023\u7d50\u30b0\u30e9\u30d5\u3067\u306f\u3001\u4efb\u610f\u306e\u9802\u70b9\u304b\u3089\u958b\u59cb\u3057\u3066\u4ed6\u306e\u4efb\u610f\u306e\u9802\u70b9\u306b\u5230\u9054\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002
        • \u975e\u9023\u7d50\u30b0\u30e9\u30d5\u3067\u306f\u3001\u4efb\u610f\u306e\u958b\u59cb\u9802\u70b9\u304b\u3089\u5230\u9054\u3067\u304d\u306a\u3044\u9802\u70b9\u304c\u5c11\u306a\u304f\u3068\u30821\u3064\u5b58\u5728\u3057\u307e\u3059\u3002

        \u56f3 9-3 \u00a0 \u9023\u7d50\u30b0\u30e9\u30d5\u3068\u975e\u9023\u7d50\u30b0\u30e9\u30d5

        \u8fba\u306b\u91cd\u307f\u5909\u6570\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u3001\u305d\u306e\u7d50\u679c\u3068\u3057\u3066\u91cd\u307f\u4ed8\u304d\u30b0\u30e9\u30d5\u304c\u751f\u307e\u308c\u307e\u3059\uff08\u4e0b\u56f3\u53c2\u7167\uff09\u3002\u4f8b\u3048\u3070\u3001Instagram\u3067\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u304c\u3042\u306a\u305f\u3068\u4ed6\u306e\u30e6\u30fc\u30b6\u30fc\u3068\u306e\u9593\u306e\u76f8\u4e92\u4f5c\u7528\u30ec\u30d9\u30eb\uff08\u3044\u3044\u306d\u3001\u95b2\u89a7\u3001\u30b3\u30e1\u30f3\u30c8\u306a\u3069\uff09\u306b\u3088\u3063\u3066\u30d5\u30a9\u30ed\u30ef\u30fc\u3068\u30d5\u30a9\u30ed\u30fc\u4e2d\u306e\u30ea\u30b9\u30c8\u3092\u30bd\u30fc\u30c8\u3057\u307e\u3059\u3002\u3053\u306e\u3088\u3046\u306a\u76f8\u4e92\u4f5c\u7528\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306f\u91cd\u307f\u4ed8\u304d\u30b0\u30e9\u30d5\u3067\u8868\u73fe\u3067\u304d\u307e\u3059\u3002

        \u56f3 9-4 \u00a0 \u91cd\u307f\u4ed8\u304d\u30b0\u30e9\u30d5\u3068\u91cd\u307f\u306a\u3057\u30b0\u30e9\u30d5

        \u30b0\u30e9\u30d5\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u308b\u7528\u8a9e\u304c\u3042\u308a\u307e\u3059\u3002

        • \u96a3\u63a5\uff1a2\u3064\u306e\u9802\u70b9\u3092\u63a5\u7d9a\u3059\u308b\u8fba\u304c\u3042\u308b\u5834\u5408\u3001\u3053\u308c\u30892\u3064\u306e\u9802\u70b9\u306f\u300c\u96a3\u63a5\u300d\u3057\u3066\u3044\u308b\u3068\u8a00\u308f\u308c\u307e\u3059\u3002\u4e0a\u56f3\u3067\u306f\u3001\u9802\u70b91\u306e\u96a3\u63a5\u9802\u70b9\u306f\u9802\u70b92\u30013\u30015\u3067\u3059\u3002
        • \u30d1\u30b9\uff1a\u9802\u70b9A\u304b\u3089\u9802\u70b9B\u307e\u3067\u306b\u901a\u904e\u3059\u308b\u8fba\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u3001A\u304b\u3089B\u3078\u306e\u30d1\u30b9\u3068\u547c\u3073\u307e\u3059\u3002\u4e0a\u56f3\u3067\u306f\u3001\u8fba\u306e\u30b7\u30fc\u30b1\u30f3\u30b91-5-2-4\u306f\u9802\u70b91\u304b\u3089\u9802\u70b94\u3078\u306e\u30d1\u30b9\u3067\u3059\u3002
        • \u6b21\u6570\uff1a\u9802\u70b9\u304c\u6301\u3064\u8fba\u306e\u6570\u3067\u3059\u3002\u6709\u5411\u30b0\u30e9\u30d5\u306e\u5834\u5408\u3001\u5165\u6b21\u6570\u306f\u305d\u306e\u9802\u70b9\u3092\u6307\u3059\u8fba\u306e\u6570\u3001\u51fa\u6b21\u6570\u306f\u305d\u306e\u9802\u70b9\u304b\u3089\u51fa\u308b\u8fba\u306e\u6570\u3092\u6307\u3057\u307e\u3059\u3002
        "},{"location":"chapter_graph/graph/#912","title":"9.1.2 \u00a0 \u30b0\u30e9\u30d5\u306e\u8868\u73fe","text":"

        \u30b0\u30e9\u30d5\u306e\u4e00\u822c\u7684\u306a\u8868\u73fe\u306b\u306f\u300c\u96a3\u63a5\u884c\u5217\u300d\u3068\u300c\u96a3\u63a5\u30ea\u30b9\u30c8\u300d\u304c\u3042\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u4f8b\u3067\u306f\u7121\u5411\u30b0\u30e9\u30d5\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        "},{"location":"chapter_graph/graph/#1","title":"1. \u00a0 \u96a3\u63a5\u884c\u5217","text":"

        \u30b0\u30e9\u30d5\u306e\u9802\u70b9\u6570\u3092\\(n\\)\u3068\u3059\u308b\u3068\u3001\u96a3\u63a5\u884c\u5217\u306f\\(n \\times n\\)\u306e\u884c\u5217\u3092\u4f7f\u7528\u3057\u3066\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3057\u307e\u3059\u3002\u5404\u884c\uff08\u5217\uff09\u306f\u9802\u70b9\u3092\u8868\u3057\u3001\u884c\u5217\u8981\u7d20\u306f\u8fba\u3092\u8868\u3057\u30012\u3064\u306e\u9802\u70b9\u9593\u306b\u8fba\u304c\u3042\u308b\u304b\u3069\u3046\u304b\u3092\\(1\\)\u307e\u305f\u306f\\(0\\)\u3067\u793a\u3057\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u96a3\u63a5\u884c\u5217\u3092\\(M\\)\u3001\u9802\u70b9\u306e\u30ea\u30b9\u30c8\u3092\\(V\\)\u3068\u3059\u308b\u3068\u3001\u884c\u5217\u8981\u7d20\\(M[i, j] = 1\\)\u306f\u9802\u70b9\\(V[i]\\)\u3068\u9802\u70b9\\(V[j]\\)\u306e\u9593\u306b\u8fba\u304c\u3042\u308b\u3053\u3068\u3092\u793a\u3057\u3001\u9006\u306b\\(M[i, j] = 0\\)\u306f2\u3064\u306e\u9802\u70b9\u9593\u306b\u8fba\u304c\u306a\u3044\u3053\u3068\u3092\u793a\u3057\u307e\u3059\u3002

        \u56f3 9-5 \u00a0 \u96a3\u63a5\u884c\u5217\u306b\u3088\u308b\u30b0\u30e9\u30d5\u306e\u8868\u73fe

        \u96a3\u63a5\u884c\u5217\u306b\u306f\u4ee5\u4e0b\u306e\u7279\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        • \u9802\u70b9\u306f\u81ea\u5206\u81ea\u8eab\u306b\u63a5\u7d9a\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u306a\u3044\u305f\u3081\u3001\u96a3\u63a5\u884c\u5217\u306e\u4e3b\u5bfe\u89d2\u7dda\u4e0a\u306e\u8981\u7d20\u306f\u610f\u5473\u304c\u3042\u308a\u307e\u305b\u3093\u3002
        • \u7121\u5411\u30b0\u30e9\u30d5\u306e\u5834\u5408\u3001\u4e21\u65b9\u5411\u306e\u8fba\u306f\u7b49\u4fa1\u3067\u3042\u308b\u305f\u3081\u3001\u96a3\u63a5\u884c\u5217\u306f\u4e3b\u5bfe\u89d2\u7dda\u306b\u95a2\u3057\u3066\u5bfe\u79f0\u3067\u3059\u3002
        • \u96a3\u63a5\u884c\u5217\u306e\u8981\u7d20\u3092\\(1\\)\u3068\\(0\\)\u304b\u3089\u91cd\u307f\u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u3067\u3001\u91cd\u307f\u4ed8\u304d\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3067\u304d\u307e\u3059\u3002

        \u96a3\u63a5\u884c\u5217\u3067\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3059\u308b\u5834\u5408\u3001\u884c\u5217\u8981\u7d20\u306b\u76f4\u63a5\u30a2\u30af\u30bb\u30b9\u3057\u3066\u8fba\u3092\u53d6\u5f97\u3067\u304d\u308b\u305f\u3081\u3001\u8ffd\u52a0\u3001\u524a\u9664\u3001\u691c\u7d22\u3001\u5909\u66f4\u306e\u64cd\u4f5c\u304c\u52b9\u7387\u7684\u3067\u3001\u3059\u3079\u3066\u6642\u9593\u8a08\u7b97\u91cf\\(O(1)\\)\u3067\u3059\u3002\u305f\u3060\u3057\u3001\u884c\u5217\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(n^2)\\)\u3067\u3001\u3088\u308a\u591a\u304f\u306e\u30e1\u30e2\u30ea\u3092\u6d88\u8cbb\u3057\u307e\u3059\u3002

        "},{"location":"chapter_graph/graph/#2","title":"2. \u00a0 \u96a3\u63a5\u30ea\u30b9\u30c8","text":"

        \u96a3\u63a5\u30ea\u30b9\u30c8\u306f\\(n\\)\u500b\u306e\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3057\u3001\u5404\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u306f\u9802\u70b9\u3092\u8868\u3057\u307e\u3059\u3002\\(i\\)\u756a\u76ee\u306e\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u9802\u70b9\\(i\\)\u306b\u5bfe\u5fdc\u3057\u3001\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\uff08\u305d\u306e\u9802\u70b9\u306b\u63a5\u7d9a\u3055\u308c\u305f\u9802\u70b9\uff09\u3092\u542b\u307f\u307e\u3059\u3002\u4e0b\u56f3\u306f\u96a3\u63a5\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u683c\u7d0d\u3055\u308c\u305f\u30b0\u30e9\u30d5\u306e\u4f8b\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 9-6 \u00a0 \u96a3\u63a5\u30ea\u30b9\u30c8\u306b\u3088\u308b\u30b0\u30e9\u30d5\u306e\u8868\u73fe

        \u96a3\u63a5\u30ea\u30b9\u30c8\u306f\u5b9f\u969b\u306e\u8fba\u306e\u307f\u3092\u683c\u7d0d\u3057\u3001\u8fba\u306e\u7dcf\u6570\u306f\\(n^2\\)\u3088\u308a\u3082\u306f\u308b\u304b\u306b\u5c11\u306a\u3044\u3053\u3068\u304c\u591a\u304f\u3001\u3088\u308a\u7a7a\u9593\u52b9\u7387\u7684\u3067\u3059\u3002\u305f\u3060\u3057\u3001\u96a3\u63a5\u30ea\u30b9\u30c8\u3067\u8fba\u3092\u898b\u3064\u3051\u308b\u306b\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8d70\u67fb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u305d\u306e\u6642\u9593\u52b9\u7387\u306f\u96a3\u63a5\u884c\u5217\u307b\u3069\u826f\u304f\u3042\u308a\u307e\u305b\u3093\u3002

        \u4e0a\u56f3\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\u96a3\u63a5\u30ea\u30b9\u30c8\u306e\u69cb\u9020\u306f\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306e\u300c\u30c1\u30a7\u30a4\u30f3\u6cd5\u300d\u3068\u975e\u5e38\u306b\u4f3c\u3066\u3044\u308b\u305f\u3081\u3001\u540c\u69d8\u306e\u65b9\u6cd5\u3092\u4f7f\u7528\u3057\u3066\u52b9\u7387\u3092\u6700\u9069\u5316\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u304c\u9577\u3044\u5834\u5408\u3001\u305d\u308c\u3092AVL\u6728\u3084\u8d64\u9ed2\u6728\u306b\u5909\u63db\u3057\u3066\u3001\u6642\u9593\u52b9\u7387\u3092\\(O(n)\\)\u304b\u3089\\(O(\\log n)\\)\u306b\u6700\u9069\u5316\u3067\u304d\u307e\u3059\u3002\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306b\u5909\u63db\u3059\u308b\u3053\u3068\u3067\u3001\u6642\u9593\u8a08\u7b97\u91cf\u3092\\(O(1)\\)\u306b\u524a\u6e1b\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_graph/graph/#913","title":"9.1.3 \u00a0 \u30b0\u30e9\u30d5\u306e\u4e00\u822c\u7684\u306a\u5fdc\u7528","text":"

        \u4e0b\u8868\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u591a\u304f\u306e\u73fe\u5b9f\u4e16\u754c\u306e\u30b7\u30b9\u30c6\u30e0\u306f\u30b0\u30e9\u30d5\u3067\u30e2\u30c7\u30eb\u5316\u3067\u304d\u3001\u5bfe\u5fdc\u3059\u308b\u554f\u984c\u306f\u30b0\u30e9\u30d5\u8a08\u7b97\u554f\u984c\u306b\u524a\u6e1b\u3067\u304d\u307e\u3059\u3002

        \u8868 9-1 \u00a0 \u73fe\u5b9f\u751f\u6d3b\u306e\u4e00\u822c\u7684\u306a\u30b0\u30e9\u30d5

        \u9802\u70b9 \u8fba \u30b0\u30e9\u30d5\u8a08\u7b97\u554f\u984c \u30bd\u30fc\u30b7\u30e3\u30eb\u30cd\u30c3\u30c8\u30ef\u30fc\u30af \u30e6\u30fc\u30b6\u30fc \u30d5\u30a9\u30ed\u30fc / \u30d5\u30a9\u30ed\u30ef\u30fc\u95a2\u4fc2 \u6f5c\u5728\u7684\u30d5\u30a9\u30ed\u30fc\u63a8\u85a6 \u5730\u4e0b\u9244\u8def\u7dda \u99c5 \u99c5\u9593\u306e\u63a5\u7d9a\u6027 \u6700\u77ed\u30eb\u30fc\u30c8\u63a8\u85a6 \u592a\u967d\u7cfb \u5929\u4f53 \u5929\u4f53\u9593\u306e\u91cd\u529b \u60d1\u661f\u8ecc\u9053\u8a08\u7b97"},{"location":"chapter_graph/graph_operations/","title":"9.2 \u00a0 \u30b0\u30e9\u30d5\u306e\u57fa\u672c\u64cd\u4f5c","text":"

        \u30b0\u30e9\u30d5\u306e\u57fa\u672c\u64cd\u4f5c\u306f\u300c\u8fba\u300d\u306b\u5bfe\u3059\u308b\u64cd\u4f5c\u3068\u300c\u9802\u70b9\u300d\u306b\u5bfe\u3059\u308b\u64cd\u4f5c\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u300c\u96a3\u63a5\u884c\u5217\u300d\u3068\u300c\u96a3\u63a5\u30ea\u30b9\u30c8\u300d\u306e2\u3064\u306e\u8868\u73fe\u65b9\u6cd5\u306e\u4e0b\u3067\u306f\u3001\u5b9f\u88c5\u304c\u7570\u306a\u308a\u307e\u3059\u3002

        "},{"location":"chapter_graph/graph_operations/#921","title":"9.2.1 \u00a0 \u96a3\u63a5\u884c\u5217\u306b\u57fa\u3065\u304f\u5b9f\u88c5","text":"

        \\(n\\)\u500b\u306e\u9802\u70b9\u3092\u6301\u3064\u7121\u5411\u30b0\u30e9\u30d5\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u3055\u307e\u3056\u307e\u306a\u64cd\u4f5c\u306f\u4e0b\u56f3\u306e\u3088\u3046\u306b\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002

        • \u8fba\u306e\u8ffd\u52a0\u307e\u305f\u306f\u524a\u9664\uff1a\u96a3\u63a5\u884c\u5217\u5185\u306e\u6307\u5b9a\u3055\u308c\u305f\u8fba\u3092\u76f4\u63a5\u5909\u66f4\u3057\u3001\\(O(1)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u7121\u5411\u30b0\u30e9\u30d5\u3067\u3042\u308b\u305f\u3081\u3001\u4e21\u65b9\u5411\u306e\u8fba\u3092\u540c\u6642\u306b\u66f4\u65b0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u9802\u70b9\u306e\u8ffd\u52a0\uff1a\u96a3\u63a5\u884c\u5217\u306e\u672b\u5c3e\u306b\u884c\u3068\u5217\u3092\u8ffd\u52a0\u3057\u3001\u3059\u3079\u3066\\(0\\)\u3067\u57cb\u3081\u307e\u3059\u3002\\(O(n)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u9802\u70b9\u306e\u524a\u9664\uff1a\u96a3\u63a5\u884c\u5217\u5185\u306e\u884c\u3068\u5217\u3092\u524a\u9664\u3057\u307e\u3059\u3002\u6700\u60aa\u306e\u5834\u5408\u306f\u6700\u521d\u306e\u884c\u3068\u5217\u304c\u524a\u9664\u3055\u308c\u308b\u3068\u304d\u3067\u3001\\((n-1)^2\\)\u500b\u306e\u8981\u7d20\u3092\u300c\u4e0a\u3068\u5de6\u306b\u79fb\u52d5\u300d\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\\(O(n^2)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u521d\u671f\u5316\uff1a\\(n\\)\u500b\u306e\u9802\u70b9\u3092\u6e21\u3057\u3001\u9577\u3055\\(n\\)\u306e\u9802\u70b9\u30ea\u30b9\u30c8vertices\u3092\u521d\u671f\u5316\u3057\u3001\\(O(n)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\\(n \\times n\\)\u30b5\u30a4\u30ba\u306e\u96a3\u63a5\u884c\u5217adjMat\u3092\u521d\u671f\u5316\u3057\u3001\\(O(n^2)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        \u96a3\u63a5\u884c\u5217\u306e\u521d\u671f\u5316\u8fba\u306e\u8ffd\u52a0\u8fba\u306e\u524a\u9664\u9802\u70b9\u306e\u8ffd\u52a0\u9802\u70b9\u306e\u524a\u9664

        \u56f3 9-7 \u00a0 \u96a3\u63a5\u884c\u5217\u3067\u306e\u521d\u671f\u5316\u3001\u8fba\u306e\u8ffd\u52a0\u3068\u524a\u9664\u3001\u9802\u70b9\u306e\u8ffd\u52a0\u3068\u524a\u9664

        \u4ee5\u4e0b\u306f\u96a3\u63a5\u884c\u5217\u3092\u4f7f\u7528\u3057\u3066\u8868\u73fe\u3055\u308c\u305f\u30b0\u30e9\u30d5\u306e\u5b9f\u88c5\u30b3\u30fc\u30c9\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_adjacency_matrix.py
        class GraphAdjMat:\n    \"\"\"\u96a3\u63a5\u884c\u5217\u306b\u57fa\u3065\u304f\u7121\u5411\u30b0\u30e9\u30d5\u30af\u30e9\u30b9\"\"\"\n\n    def __init__(self, vertices: list[int], edges: list[list[int]]):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        # \u9802\u70b9\u30ea\u30b9\u30c8\u3001\u8981\u7d20\u306f\u300c\u9802\u70b9\u5024\u300d\u3092\u8868\u3057\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u300c\u9802\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u3092\u8868\u3059\n        self.vertices: list[int] = []\n        # \u96a3\u63a5\u884c\u5217\u3001\u884c\u3068\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u300c\u9802\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u306b\u5bfe\u5fdc\n        self.adj_mat: list[list[int]] = []\n        # \u9802\u70b9\u3092\u8ffd\u52a0\n        for val in vertices:\n            self.add_vertex(val)\n        # \u8fba\u3092\u8ffd\u52a0\n        # edges\u8981\u7d20\u306f\u9802\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8868\u3059\n        for e in edges:\n            self.add_edge(e[0], e[1])\n\n    def size(self) -> int:\n        \"\"\"\u9802\u70b9\u6570\u3092\u53d6\u5f97\"\"\"\n        return len(self.vertices)\n\n    def add_vertex(self, val: int):\n        \"\"\"\u9802\u70b9\u3092\u8ffd\u52a0\"\"\"\n        n = self.size()\n        # \u9802\u70b9\u30ea\u30b9\u30c8\u306b\u65b0\u3057\u3044\u9802\u70b9\u5024\u3092\u8ffd\u52a0\n        self.vertices.append(val)\n        # \u96a3\u63a5\u884c\u5217\u306b\u884c\u3092\u8ffd\u52a0\n        new_row = [0] * n\n        self.adj_mat.append(new_row)\n        # \u96a3\u63a5\u884c\u5217\u306b\u5217\u3092\u8ffd\u52a0\n        for row in self.adj_mat:\n            row.append(0)\n\n    def remove_vertex(self, index: int):\n        \"\"\"\u9802\u70b9\u3092\u524a\u9664\"\"\"\n        if index >= self.size():\n            raise IndexError()\n        # \u9802\u70b9\u30ea\u30b9\u30c8\u304b\u3089`index`\u306e\u9802\u70b9\u3092\u524a\u9664\n        self.vertices.pop(index)\n        # \u96a3\u63a5\u884c\u5217\u304b\u3089`index`\u306e\u884c\u3092\u524a\u9664\n        self.adj_mat.pop(index)\n        # \u96a3\u63a5\u884c\u5217\u304b\u3089`index`\u306e\u5217\u3092\u524a\u9664\n        for row in self.adj_mat:\n            row.pop(index)\n\n    def add_edge(self, i: int, j: int):\n        \"\"\"\u8fba\u3092\u8ffd\u52a0\"\"\"\n        # \u30d1\u30e9\u30e1\u30fc\u30bfi\u3001j\u306f\u9802\u70b9\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5bfe\u5fdc\n        # \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u7bc4\u56f2\u5916\u3068\u7b49\u4fa1\u6027\u3092\u51e6\u7406\n        if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\n            raise IndexError()\n        # \u7121\u5411\u30b0\u30e9\u30d5\u3067\u306f\u3001\u96a3\u63a5\u884c\u5217\u306f\u4e3b\u5bfe\u89d2\u7dda\u306b\u3064\u3044\u3066\u5bfe\u79f0\u3001\u3059\u306a\u308f\u3061 (i, j) == (j, i) \u3092\u6e80\u305f\u3059\n        self.adj_mat[i][j] = 1\n        self.adj_mat[j][i] = 1\n\n    def remove_edge(self, i: int, j: int):\n        \"\"\"\u8fba\u3092\u524a\u9664\"\"\"\n        # \u30d1\u30e9\u30e1\u30fc\u30bfi\u3001j\u306f\u9802\u70b9\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5bfe\u5fdc\n        # \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u7bc4\u56f2\u5916\u3068\u7b49\u4fa1\u6027\u3092\u51e6\u7406\n        if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\n            raise IndexError()\n        self.adj_mat[i][j] = 0\n        self.adj_mat[j][i] = 0\n\n    def print(self):\n        \"\"\"\u96a3\u63a5\u884c\u5217\u3092\u51fa\u529b\"\"\"\n        print(\"\u9802\u70b9\u30ea\u30b9\u30c8 =\", self.vertices)\n        print(\"\u96a3\u63a5\u884c\u5217 =\")\n        print_matrix(self.adj_mat)\n
        graph_adjacency_matrix.cpp
        /* \u96a3\u63a5\u884c\u5217\u306b\u57fa\u3065\u304f\u7121\u5411\u30b0\u30e9\u30d5\u30af\u30e9\u30b9 */\nclass GraphAdjMat {\n    vector<int> vertices;       // \u9802\u70b9\u30ea\u30b9\u30c8\u3001\u8981\u7d20\u306f\u300c\u9802\u70b9\u5024\u300d\u3092\u8868\u3057\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u300c\u9802\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u3092\u8868\u3059\n    vector<vector<int>> adjMat; // \u96a3\u63a5\u884c\u5217\u3001\u884c\u3068\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u300c\u9802\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u306b\u5bfe\u5fdc\n\n  public:\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    GraphAdjMat(const vector<int> &vertices, const vector<vector<int>> &edges) {\n        // \u9802\u70b9\u3092\u8ffd\u52a0\n        for (int val : vertices) {\n            addVertex(val);\n        }\n        // \u8fba\u3092\u8ffd\u52a0\n        // \u8fba\u306e\u8981\u7d20\u306f\u9802\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8868\u3059\n        for (const vector<int> &edge : edges) {\n            addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u9802\u70b9\u6570\u3092\u53d6\u5f97 */\n    int size() const {\n        return vertices.size();\n    }\n\n    /* \u9802\u70b9\u3092\u8ffd\u52a0 */\n    void addVertex(int val) {\n        int n = size();\n        // \u9802\u70b9\u30ea\u30b9\u30c8\u306b\u65b0\u3057\u3044\u9802\u70b9\u5024\u3092\u8ffd\u52a0\n        vertices.push_back(val);\n        // \u96a3\u63a5\u884c\u5217\u306b\u884c\u3092\u8ffd\u52a0\n        adjMat.emplace_back(vector<int>(n, 0));\n        // \u96a3\u63a5\u884c\u5217\u306b\u5217\u3092\u8ffd\u52a0\n        for (vector<int> &row : adjMat) {\n            row.push_back(0);\n        }\n    }\n\n    /* \u9802\u70b9\u3092\u524a\u9664 */\n    void removeVertex(int index) {\n        if (index >= size()) {\n            throw out_of_range(\"Vertex does not exist\");\n        }\n        // \u9802\u70b9\u30ea\u30b9\u30c8\u304b\u3089`index`\u306e\u9802\u70b9\u3092\u524a\u9664\n        vertices.erase(vertices.begin() + index);\n        // \u96a3\u63a5\u884c\u5217\u304b\u3089`index`\u306e\u884c\u3092\u524a\u9664\n        adjMat.erase(adjMat.begin() + index);\n        // \u96a3\u63a5\u884c\u5217\u304b\u3089`index`\u306e\u5217\u3092\u524a\u9664\n        for (vector<int> &row : adjMat) {\n            row.erase(row.begin() + index);\n        }\n    }\n\n    /* \u8fba\u3092\u8ffd\u52a0 */\n    // \u30d1\u30e9\u30e1\u30fc\u30bfi\u3001j\u306f\u9802\u70b9\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5bfe\u5fdc\n    void addEdge(int i, int j) {\n        // \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2\u5916\u3068\u7b49\u4fa1\u6027\u3092\u51e6\u7406\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n            throw out_of_range(\"Vertex does not exist\");\n        }\n        // \u7121\u5411\u30b0\u30e9\u30d5\u3067\u306f\u3001\u96a3\u63a5\u884c\u5217\u306f\u4e3b\u5bfe\u89d2\u7dda\u306b\u3064\u3044\u3066\u5bfe\u79f0\u3001\u5373\u3061(i, j) == (j, i)\u3092\u6e80\u305f\u3059\n        adjMat[i][j] = 1;\n        adjMat[j][i] = 1;\n    }\n\n    /* \u8fba\u3092\u524a\u9664 */\n    // \u30d1\u30e9\u30e1\u30fc\u30bfi\u3001j\u306f\u9802\u70b9\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5bfe\u5fdc\n    void removeEdge(int i, int j) {\n        // \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2\u5916\u3068\u7b49\u4fa1\u6027\u3092\u51e6\u7406\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n            throw out_of_range(\"Vertex does not exist\");\n        }\n        adjMat[i][j] = 0;\n        adjMat[j][i] = 0;\n    }\n\n    /* \u96a3\u63a5\u884c\u5217\u3092\u5370\u5237 */\n    void print() {\n        cout << \"\u9802\u70b9\u30ea\u30b9\u30c8 = \";\n        printVector(vertices);\n        cout << \"\u96a3\u63a5\u884c\u5217 =\" << endl;\n        printVectorMatrix(adjMat);\n    }\n};\n
        graph_adjacency_matrix.java
        /* \u96a3\u63a5\u884c\u5217\u306b\u57fa\u3065\u304f\u7121\u5411\u30b0\u30e9\u30d5\u30af\u30e9\u30b9 */\nclass GraphAdjMat {\n    List<Integer> vertices; // \u9802\u70b9\u30ea\u30b9\u30c8\u3001\u8981\u7d20\u306f\u300c\u9802\u70b9\u5024\u300d\u3092\u8868\u3057\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u300c\u9802\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u3092\u8868\u3059\n    List<List<Integer>> adjMat; // \u96a3\u63a5\u884c\u5217\u3001\u884c\u3068\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u300c\u9802\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u306b\u5bfe\u5fdc\n\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    public GraphAdjMat(int[] vertices, int[][] edges) {\n        this.vertices = new ArrayList<>();\n        this.adjMat = new ArrayList<>();\n        // \u9802\u70b9\u3092\u8ffd\u52a0\n        for (int val : vertices) {\n            addVertex(val);\n        }\n        // \u8fba\u3092\u8ffd\u52a0\n        // \u8fba\u306e\u8981\u7d20\u306f\u9802\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8868\u3059\n        for (int[] e : edges) {\n            addEdge(e[0], e[1]);\n        }\n    }\n\n    /* \u9802\u70b9\u6570\u3092\u53d6\u5f97 */\n    public int size() {\n        return vertices.size();\n    }\n\n    /* \u9802\u70b9\u3092\u8ffd\u52a0 */\n    public void addVertex(int val) {\n        int n = size();\n        // \u9802\u70b9\u30ea\u30b9\u30c8\u306b\u65b0\u3057\u3044\u9802\u70b9\u5024\u3092\u8ffd\u52a0\n        vertices.add(val);\n        // \u96a3\u63a5\u884c\u5217\u306b\u884c\u3092\u8ffd\u52a0\n        List<Integer> newRow = new ArrayList<>(n);\n        for (int j = 0; j < n; j++) {\n            newRow.add(0);\n        }\n        adjMat.add(newRow);\n        // \u96a3\u63a5\u884c\u5217\u306b\u5217\u3092\u8ffd\u52a0\n        for (List<Integer> row : adjMat) {\n            row.add(0);\n        }\n    }\n\n    /* \u9802\u70b9\u3092\u524a\u9664 */\n    public void removeVertex(int index) {\n        if (index >= size())\n            throw new IndexOutOfBoundsException();\n        // \u9802\u70b9\u30ea\u30b9\u30c8\u304b\u3089 `index` \u306e\u9802\u70b9\u3092\u524a\u9664\n        vertices.remove(index);\n        // \u96a3\u63a5\u884c\u5217\u304b\u3089 `index` \u306e\u884c\u3092\u524a\u9664\n        adjMat.remove(index);\n        // \u96a3\u63a5\u884c\u5217\u304b\u3089 `index` \u306e\u5217\u3092\u524a\u9664\n        for (List<Integer> row : adjMat) {\n            row.remove(index);\n        }\n    }\n\n    /* \u8fba\u3092\u8ffd\u52a0 */\n    // \u30d1\u30e9\u30e1\u30fc\u30bf i\u3001j \u306f\u9802\u70b9\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5bfe\u5fdc\n    public void addEdge(int i, int j) {\n        // \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u7bc4\u56f2\u5916\u3068\u7b49\u4fa1\u6027\u3092\u51e6\u7406\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw new IndexOutOfBoundsException();\n        // \u7121\u5411\u30b0\u30e9\u30d5\u3067\u306f\u3001\u96a3\u63a5\u884c\u5217\u306f\u4e3b\u5bfe\u89d2\u7dda\u306b\u3064\u3044\u3066\u5bfe\u79f0\u3001\u3059\u306a\u308f\u3061 (i, j) == (j, i) \u3092\u6e80\u305f\u3059\n        adjMat.get(i).set(j, 1);\n        adjMat.get(j).set(i, 1);\n    }\n\n    /* \u8fba\u3092\u524a\u9664 */\n    // \u30d1\u30e9\u30e1\u30fc\u30bf i\u3001j \u306f\u9802\u70b9\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5bfe\u5fdc\n    public void removeEdge(int i, int j) {\n        // \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u7bc4\u56f2\u5916\u3068\u7b49\u4fa1\u6027\u3092\u51e6\u7406\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw new IndexOutOfBoundsException();\n        adjMat.get(i).set(j, 0);\n        adjMat.get(j).set(i, 0);\n    }\n\n    /* \u96a3\u63a5\u884c\u5217\u3092\u51fa\u529b */\n    public void print() {\n        System.out.print(\"\u9802\u70b9\u30ea\u30b9\u30c8 = \");\n        System.out.println(vertices);\n        System.out.println(\"\u96a3\u63a5\u884c\u5217 =\");\n        PrintUtil.printMatrix(adjMat);\n    }\n}\n
        graph_adjacency_matrix.cs
        [class]{GraphAdjMat}-[func]{}\n
        graph_adjacency_matrix.go
        [class]{graphAdjMat}-[func]{}\n
        graph_adjacency_matrix.swift
        [class]{GraphAdjMat}-[func]{}\n
        graph_adjacency_matrix.js
        [class]{GraphAdjMat}-[func]{}\n
        graph_adjacency_matrix.ts
        [class]{GraphAdjMat}-[func]{}\n
        graph_adjacency_matrix.dart
        [class]{GraphAdjMat}-[func]{}\n
        graph_adjacency_matrix.rs
        [class]{GraphAdjMat}-[func]{}\n
        graph_adjacency_matrix.c
        [class]{GraphAdjMat}-[func]{}\n
        graph_adjacency_matrix.kt
        [class]{GraphAdjMat}-[func]{}\n
        graph_adjacency_matrix.rb
        [class]{GraphAdjMat}-[func]{}\n
        "},{"location":"chapter_graph/graph_operations/#922","title":"9.2.2 \u00a0 \u96a3\u63a5\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u5b9f\u88c5","text":"

        \u7dcf\u8a08\\(n\\)\u500b\u306e\u9802\u70b9\u3068\\(m\\)\u500b\u306e\u8fba\u3092\u6301\u3064\u7121\u5411\u30b0\u30e9\u30d5\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u3055\u307e\u3056\u307e\u306a\u64cd\u4f5c\u306f\u4e0b\u56f3\u306e\u3088\u3046\u306b\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002

        • \u8fba\u306e\u8ffd\u52a0\uff1a\u5bfe\u5fdc\u3059\u308b\u9802\u70b9\u306e\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u672b\u5c3e\u306b\u8fba\u3092\u8ffd\u52a0\u3059\u308b\u3060\u3051\u3067\u3001\\(O(1)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u7121\u5411\u30b0\u30e9\u30d5\u3067\u3042\u308b\u305f\u3081\u3001\u4e21\u65b9\u5411\u306b\u540c\u6642\u306b\u8fba\u3092\u8ffd\u52a0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u8fba\u306e\u524a\u9664\uff1a\u5bfe\u5fdc\u3059\u308b\u9802\u70b9\u306e\u9023\u7d50\u30ea\u30b9\u30c8\u5185\u3067\u6307\u5b9a\u3055\u308c\u305f\u8fba\u3092\u898b\u3064\u3051\u3066\u524a\u9664\u3057\u3001\\(O(m)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u7121\u5411\u30b0\u30e9\u30d5\u3067\u306f\u3001\u4e21\u65b9\u5411\u306e\u8fba\u3092\u540c\u6642\u306b\u524a\u9664\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u9802\u70b9\u306e\u8ffd\u52a0\uff1a\u96a3\u63a5\u30ea\u30b9\u30c8\u306b\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8ffd\u52a0\u3057\u3001\u65b0\u3057\u3044\u9802\u70b9\u3092\u30ea\u30b9\u30c8\u306e\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u306b\u3057\u3001\\(O(1)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u9802\u70b9\u306e\u524a\u9664\uff1a\u96a3\u63a5\u30ea\u30b9\u30c8\u5168\u4f53\u3092\u8d70\u67fb\u3057\u3001\u6307\u5b9a\u3055\u308c\u305f\u9802\u70b9\u3092\u542b\u3080\u3059\u3079\u3066\u306e\u8fba\u3092\u524a\u9664\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\\(O(n + m)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u521d\u671f\u5316\uff1a\u96a3\u63a5\u30ea\u30b9\u30c8\u306b\\(n\\)\u500b\u306e\u9802\u70b9\u3068\\(2m\\)\u500b\u306e\u8fba\u3092\u4f5c\u6210\u3057\u3001\\(O(n + m)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        \u96a3\u63a5\u30ea\u30b9\u30c8\u306e\u521d\u671f\u5316\u8fba\u306e\u8ffd\u52a0\u8fba\u306e\u524a\u9664\u9802\u70b9\u306e\u8ffd\u52a0\u9802\u70b9\u306e\u524a\u9664

        \u56f3 9-8 \u00a0 \u96a3\u63a5\u30ea\u30b9\u30c8\u3067\u306e\u521d\u671f\u5316\u3001\u8fba\u306e\u8ffd\u52a0\u3068\u524a\u9664\u3001\u9802\u70b9\u306e\u8ffd\u52a0\u3068\u524a\u9664

        \u4ee5\u4e0b\u306f\u96a3\u63a5\u30ea\u30b9\u30c8\u306e\u30b3\u30fc\u30c9\u5b9f\u88c5\u3067\u3059\u3002\u4e0a\u56f3\u3068\u6bd4\u8f03\u3057\u3066\u3001\u5b9f\u969b\u306e\u30b3\u30fc\u30c9\u306b\u306f\u4ee5\u4e0b\u306e\u9055\u3044\u304c\u3042\u308a\u307e\u3059\u3002

        • \u9802\u70b9\u306e\u8ffd\u52a0\u3068\u524a\u9664\u306e\u4fbf\u5b9c\u3001\u304a\u3088\u3073\u30b3\u30fc\u30c9\u306e\u7c21\u7d20\u5316\u306e\u305f\u3081\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u4ee3\u308f\u308a\u306b\u30ea\u30b9\u30c8\uff08\u52d5\u7684\u914d\u5217\uff09\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u4f7f\u7528\u3057\u3066\u96a3\u63a5\u30ea\u30b9\u30c8\u3092\u683c\u7d0d\u3057\u3001key\u304c\u9802\u70b9\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3001value\u304c\u305d\u306e\u9802\u70b9\u306e\u96a3\u63a5\u9802\u70b9\u306e\u30ea\u30b9\u30c8\uff08\u9023\u7d50\u30ea\u30b9\u30c8\uff09\u3067\u3059\u3002

        \u3055\u3089\u306b\u3001\u96a3\u63a5\u30ea\u30b9\u30c8\u3067\u9802\u70b9\u3092\u8868\u73fe\u3059\u308b\u305f\u3081\u306bVertex\u30af\u30e9\u30b9\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u305d\u306e\u7406\u7531\u306f\uff1a\u96a3\u63a5\u884c\u5217\u306e\u3088\u3046\u306b\u30ea\u30b9\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u4f7f\u7528\u3057\u3066\u7570\u306a\u308b\u9802\u70b9\u3092\u533a\u5225\u3059\u308b\u5834\u5408\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(i\\)\u306e\u9802\u70b9\u3092\u524a\u9664\u3057\u305f\u3044\u5834\u5408\u3001\u96a3\u63a5\u30ea\u30b9\u30c8\u5168\u4f53\u3092\u8d70\u67fb\u3057\u3001\\(i\\)\u3088\u308a\u5927\u304d\u3044\u3059\u3079\u3066\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u30921\u3064\u305a\u3064\u6e1b\u5c11\u3055\u305b\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u3053\u308c\u306f\u975e\u5e38\u306b\u975e\u52b9\u7387\u7684\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u5404\u9802\u70b9\u304c\u4e00\u610f\u306eVertex\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3067\u3042\u308b\u5834\u5408\u3001\u9802\u70b9\u3092\u524a\u9664\u3057\u3066\u3082\u4ed6\u306e\u9802\u70b9\u306b\u5909\u66f4\u3092\u52a0\u3048\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u305b\u3093\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_adjacency_list.py
        class GraphAdjList:\n    \"\"\"\u96a3\u63a5\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u7121\u5411\u30b0\u30e9\u30d5\u30af\u30e9\u30b9\"\"\"\n\n    def __init__(self, edges: list[list[Vertex]]):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        # \u96a3\u63a5\u30ea\u30b9\u30c8\u3001\u30ad\u30fc: \u9802\u70b9\u3001\u5024: \u305d\u306e\u9802\u70b9\u306e\u96a3\u63a5\u3059\u308b\u5168\u9802\u70b9\n        self.adj_list = dict[Vertex, list[Vertex]]()\n        # \u3059\u3079\u3066\u306e\u9802\u70b9\u3068\u8fba\u3092\u8ffd\u52a0\n        for edge in edges:\n            self.add_vertex(edge[0])\n            self.add_vertex(edge[1])\n            self.add_edge(edge[0], edge[1])\n\n    def size(self) -> int:\n        \"\"\"\u9802\u70b9\u6570\u3092\u53d6\u5f97\"\"\"\n        return len(self.adj_list)\n\n    def add_edge(self, vet1: Vertex, vet2: Vertex):\n        \"\"\"\u8fba\u3092\u8ffd\u52a0\"\"\"\n        if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n            raise ValueError()\n        # \u8fba vet1 - vet2 \u3092\u8ffd\u52a0\n        self.adj_list[vet1].append(vet2)\n        self.adj_list[vet2].append(vet1)\n\n    def remove_edge(self, vet1: Vertex, vet2: Vertex):\n        \"\"\"\u8fba\u3092\u524a\u9664\"\"\"\n        if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n            raise ValueError()\n        # \u8fba vet1 - vet2 \u3092\u524a\u9664\n        self.adj_list[vet1].remove(vet2)\n        self.adj_list[vet2].remove(vet1)\n\n    def add_vertex(self, vet: Vertex):\n        \"\"\"\u9802\u70b9\u3092\u8ffd\u52a0\"\"\"\n        if vet in self.adj_list:\n            return\n        # \u96a3\u63a5\u30ea\u30b9\u30c8\u306b\u65b0\u3057\u3044\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8ffd\u52a0\n        self.adj_list[vet] = []\n\n    def remove_vertex(self, vet: Vertex):\n        \"\"\"\u9802\u70b9\u3092\u524a\u9664\"\"\"\n        if vet not in self.adj_list:\n            raise ValueError()\n        # \u96a3\u63a5\u30ea\u30b9\u30c8\u304b\u3089\u9802\u70b9vet\u306b\u5bfe\u5fdc\u3059\u308b\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u524a\u9664\n        self.adj_list.pop(vet)\n        # \u4ed6\u306e\u9802\u70b9\u306e\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8d70\u67fb\u3057\u3001vet\u3092\u542b\u3080\u3059\u3079\u3066\u306e\u8fba\u3092\u524a\u9664\n        for vertex in self.adj_list:\n            if vet in self.adj_list[vertex]:\n                self.adj_list[vertex].remove(vet)\n\n    def print(self):\n        \"\"\"\u96a3\u63a5\u30ea\u30b9\u30c8\u3092\u51fa\u529b\"\"\"\n        print(\"\u96a3\u63a5\u30ea\u30b9\u30c8 =\")\n        for vertex in self.adj_list:\n            tmp = [v.val for v in self.adj_list[vertex]]\n            print(f\"{vertex.val}: {tmp},\")\n
        graph_adjacency_list.cpp
        /* \u96a3\u63a5\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u7121\u5411\u30b0\u30e9\u30d5\u30af\u30e9\u30b9 */\nclass GraphAdjList {\n  public:\n    // \u96a3\u63a5\u30ea\u30b9\u30c8\u3001\u30ad\u30fc\uff1a\u9802\u70b9\u3001\u5024\uff1a\u305d\u306e\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\n    unordered_map<Vertex *, vector<Vertex *>> adjList;\n\n    /* \u30d9\u30af\u30bf\u30fc\u304b\u3089\u6307\u5b9a\u3055\u308c\u305f\u30ce\u30fc\u30c9\u3092\u524a\u9664 */\n    void remove(vector<Vertex *> &vec, Vertex *vet) {\n        for (int i = 0; i < vec.size(); i++) {\n            if (vec[i] == vet) {\n                vec.erase(vec.begin() + i);\n                break;\n            }\n        }\n    }\n\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    GraphAdjList(const vector<vector<Vertex *>> &edges) {\n        // \u3059\u3079\u3066\u306e\u9802\u70b9\u3068\u8fba\u3092\u8ffd\u52a0\n        for (const vector<Vertex *> &edge : edges) {\n            addVertex(edge[0]);\n            addVertex(edge[1]);\n            addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u9802\u70b9\u6570\u3092\u53d6\u5f97 */\n    int size() {\n        return adjList.size();\n    }\n\n    /* \u8fba\u3092\u8ffd\u52a0 */\n    void addEdge(Vertex *vet1, Vertex *vet2) {\n        if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n            throw invalid_argument(\"Vertex does not exist\");\n        // \u8fba vet1 - vet2 \u3092\u8ffd\u52a0\n        adjList[vet1].push_back(vet2);\n        adjList[vet2].push_back(vet1);\n    }\n\n    /* \u8fba\u3092\u524a\u9664 */\n    void removeEdge(Vertex *vet1, Vertex *vet2) {\n        if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n            throw invalid_argument(\"Vertex does not exist\");\n        // \u8fba vet1 - vet2 \u3092\u524a\u9664\n        remove(adjList[vet1], vet2);\n        remove(adjList[vet2], vet1);\n    }\n\n    /* \u9802\u70b9\u3092\u8ffd\u52a0 */\n    void addVertex(Vertex *vet) {\n        if (adjList.count(vet))\n            return;\n        // \u96a3\u63a5\u30ea\u30b9\u30c8\u306b\u65b0\u3057\u3044\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8ffd\u52a0\n        adjList[vet] = vector<Vertex *>();\n    }\n\n    /* \u9802\u70b9\u3092\u524a\u9664 */\n    void removeVertex(Vertex *vet) {\n        if (!adjList.count(vet))\n            throw invalid_argument(\"Vertex does not exist\");\n        // \u96a3\u63a5\u30ea\u30b9\u30c8\u304b\u3089\u9802\u70b9vet\u306b\u5bfe\u5fdc\u3059\u308b\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u524a\u9664\n        adjList.erase(vet);\n        // \u4ed6\u306e\u9802\u70b9\u306e\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8d70\u67fb\u3057\u3001vet\u3092\u542b\u3080\u3059\u3079\u3066\u306e\u8fba\u3092\u524a\u9664\n        for (auto &adj : adjList) {\n            remove(adj.second, vet);\n        }\n    }\n\n    /* \u96a3\u63a5\u30ea\u30b9\u30c8\u3092\u5370\u5237 */\n    void print() {\n        cout << \"\u96a3\u63a5\u30ea\u30b9\u30c8 =\" << endl;\n        for (auto &adj : adjList) {\n            const auto &key = adj.first;\n            const auto &vec = adj.second;\n            cout << key->val << \": \";\n            printVector(vetsToVals(vec));\n        }\n    }\n};\n
        graph_adjacency_list.java
        /* \u96a3\u63a5\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u7121\u5411\u30b0\u30e9\u30d5\u30af\u30e9\u30b9 */\nclass GraphAdjList {\n    // \u96a3\u63a5\u30ea\u30b9\u30c8\u3001\u30ad\u30fc: \u9802\u70b9\u3001\u5024: \u305d\u306e\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\n    Map<Vertex, List<Vertex>> adjList;\n\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    public GraphAdjList(Vertex[][] edges) {\n        this.adjList = new HashMap<>();\n        // \u3059\u3079\u3066\u306e\u9802\u70b9\u3068\u8fba\u3092\u8ffd\u52a0\n        for (Vertex[] edge : edges) {\n            addVertex(edge[0]);\n            addVertex(edge[1]);\n            addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u9802\u70b9\u6570\u3092\u53d6\u5f97 */\n    public int size() {\n        return adjList.size();\n    }\n\n    /* \u8fba\u3092\u8ffd\u52a0 */\n    public void addEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw new IllegalArgumentException();\n        // \u8fba vet1 - vet2 \u3092\u8ffd\u52a0\n        adjList.get(vet1).add(vet2);\n        adjList.get(vet2).add(vet1);\n    }\n\n    /* \u8fba\u3092\u524a\u9664 */\n    public void removeEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw new IllegalArgumentException();\n        // \u8fba vet1 - vet2 \u3092\u524a\u9664\n        adjList.get(vet1).remove(vet2);\n        adjList.get(vet2).remove(vet1);\n    }\n\n    /* \u9802\u70b9\u3092\u8ffd\u52a0 */\n    public void addVertex(Vertex vet) {\n        if (adjList.containsKey(vet))\n            return;\n        // \u96a3\u63a5\u30ea\u30b9\u30c8\u306b\u65b0\u3057\u3044\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8ffd\u52a0\n        adjList.put(vet, new ArrayList<>());\n    }\n\n    /* \u9802\u70b9\u3092\u524a\u9664 */\n    public void removeVertex(Vertex vet) {\n        if (!adjList.containsKey(vet))\n            throw new IllegalArgumentException();\n        // \u96a3\u63a5\u30ea\u30b9\u30c8\u304b\u3089\u9802\u70b9 vet \u306b\u5bfe\u5fdc\u3059\u308b\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u524a\u9664\n        adjList.remove(vet);\n        // \u4ed6\u306e\u9802\u70b9\u306e\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8d70\u67fb\u3057\u3001vet \u3092\u542b\u3080\u3059\u3079\u3066\u306e\u8fba\u3092\u524a\u9664\n        for (List<Vertex> list : adjList.values()) {\n            list.remove(vet);\n        }\n    }\n\n    /* \u96a3\u63a5\u30ea\u30b9\u30c8\u3092\u51fa\u529b */\n    public void print() {\n        System.out.println(\"\u96a3\u63a5\u30ea\u30b9\u30c8 =\");\n        for (Map.Entry<Vertex, List<Vertex>> pair : adjList.entrySet()) {\n            List<Integer> tmp = new ArrayList<>();\n            for (Vertex vertex : pair.getValue())\n                tmp.add(vertex.val);\n            System.out.println(pair.getKey().val + \": \" + tmp + \",\");\n        }\n    }\n}\n
        graph_adjacency_list.cs
        [class]{GraphAdjList}-[func]{}\n
        graph_adjacency_list.go
        [class]{graphAdjList}-[func]{}\n
        graph_adjacency_list.swift
        [class]{GraphAdjList}-[func]{}\n
        graph_adjacency_list.js
        [class]{GraphAdjList}-[func]{}\n
        graph_adjacency_list.ts
        [class]{GraphAdjList}-[func]{}\n
        graph_adjacency_list.dart
        [class]{GraphAdjList}-[func]{}\n
        graph_adjacency_list.rs
        [class]{GraphAdjList}-[func]{}\n
        graph_adjacency_list.c
        [class]{AdjListNode}-[func]{}\n\n[class]{GraphAdjList}-[func]{}\n
        graph_adjacency_list.kt
        [class]{GraphAdjList}-[func]{}\n
        graph_adjacency_list.rb
        [class]{GraphAdjList}-[func]{}\n
        "},{"location":"chapter_graph/graph_operations/#923","title":"9.2.3 \u00a0 \u52b9\u7387\u306e\u6bd4\u8f03","text":"

        \u30b0\u30e9\u30d5\u306b\\(n\\)\u500b\u306e\u9802\u70b9\u3068\\(m\\)\u500b\u306e\u8fba\u304c\u3042\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u4e0b\u8868\u306f\u96a3\u63a5\u884c\u5217\u3068\u96a3\u63a5\u30ea\u30b9\u30c8\u306e\u6642\u9593\u52b9\u7387\u3068\u7a7a\u9593\u52b9\u7387\u3092\u6bd4\u8f03\u3057\u3066\u3044\u307e\u3059\u3002

        \u8868 9-2 \u00a0 \u96a3\u63a5\u884c\u5217\u3068\u96a3\u63a5\u30ea\u30b9\u30c8\u306e\u6bd4\u8f03

        \u96a3\u63a5\u884c\u5217 \u96a3\u63a5\u30ea\u30b9\u30c8\uff08\u9023\u7d50\u30ea\u30b9\u30c8\uff09 \u96a3\u63a5\u30ea\u30b9\u30c8\uff08\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\uff09 \u96a3\u63a5\u6027\u306e\u5224\u5b9a \\(O(1)\\) \\(O(m)\\) \\(O(1)\\) \u8fba\u306e\u8ffd\u52a0 \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u8fba\u306e\u524a\u9664 \\(O(1)\\) \\(O(m)\\) \\(O(1)\\) \u9802\u70b9\u306e\u8ffd\u52a0 \\(O(n)\\) \\(O(1)\\) \\(O(1)\\) \u9802\u70b9\u306e\u524a\u9664 \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n)\\) \u30e1\u30e2\u30ea\u7a7a\u9593\u4f7f\u7528\u91cf \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n + m)\\)

        \u4e0a\u8868\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\u96a3\u63a5\u30ea\u30b9\u30c8\uff08\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\uff09\u304c\u6700\u9ad8\u306e\u6642\u9593\u52b9\u7387\u3068\u7a7a\u9593\u52b9\u7387\u3092\u6301\u3063\u3066\u3044\u308b\u3088\u3046\u306b\u898b\u3048\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u5b9f\u969b\u306b\u306f\u3001\u96a3\u63a5\u884c\u5217\u3067\u306e\u8fba\u306b\u5bfe\u3059\u308b\u64cd\u4f5c\u304c\u3088\u308a\u52b9\u7387\u7684\u3067\u3001\u5358\u4e00\u306e\u914d\u5217\u30a2\u30af\u30bb\u30b9\u307e\u305f\u306f\u4ee3\u5165\u64cd\u4f5c\u306e\u307f\u304c\u5fc5\u8981\u3067\u3059\u3002\u5168\u4f53\u7684\u306b\u3001\u96a3\u63a5\u884c\u5217\u306f\u300c\u7a7a\u9593\u3068\u6642\u9593\u306e\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u300d\u306e\u539f\u5247\u3092\u4f8b\u793a\u3057\u3001\u96a3\u63a5\u30ea\u30b9\u30c8\u306f\u300c\u6642\u9593\u3068\u7a7a\u9593\u306e\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u300d\u3092\u4f8b\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_graph/graph_traversal/","title":"9.3 \u00a0 \u30b0\u30e9\u30d5\u8d70\u67fb","text":"

        \u6728\u306f\u300c\u4e00\u5bfe\u591a\u300d\u306e\u95a2\u4fc2\u3092\u8868\u73fe\u3057\u3001\u30b0\u30e9\u30d5\u306f\u3088\u308a\u9ad8\u3044\u81ea\u7531\u5ea6\u3092\u6301\u3061\u3001\u4efb\u610f\u306e\u300c\u591a\u5bfe\u591a\u300d\u306e\u95a2\u4fc2\u3092\u8868\u73fe\u3067\u304d\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u6728\u3092\u30b0\u30e9\u30d5\u306e\u7279\u5225\u306a\u30b1\u30fc\u30b9\u3068\u898b\u306a\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u660e\u3089\u304b\u306b\u3001\u6728\u306e\u8d70\u67fb\u64cd\u4f5c\u3082\u30b0\u30e9\u30d5\u8d70\u67fb\u64cd\u4f5c\u306e\u7279\u5225\u306a\u30b1\u30fc\u30b9\u3067\u3059\u3002

        \u30b0\u30e9\u30d5\u3068\u6728\u306e\u4e21\u65b9\u3067\u3001\u8d70\u67fb\u64cd\u4f5c\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306b\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5fdc\u7528\u304c\u5fc5\u8981\u3067\u3059\u3002\u30b0\u30e9\u30d5\u8d70\u67fb\u306f2\u3064\u306e\u30bf\u30a4\u30d7\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a\u5e45\u512a\u5148\u63a2\u7d22\uff08BFS\uff09\u3068\u6df1\u3055\u512a\u5148\u63a2\u7d22\uff08DFS\uff09\u3067\u3059\u3002

        "},{"location":"chapter_graph/graph_traversal/#931","title":"9.3.1 \u00a0 \u5e45\u512a\u5148\u63a2\u7d22","text":"

        \u5e45\u512a\u5148\u63a2\u7d22\u306f\u8fd1\u304f\u304b\u3089\u9060\u304f\u3078\u306e\u8d70\u67fb\u65b9\u6cd5\u3067\u3001\u3042\u308b\u9802\u70b9\u304b\u3089\u958b\u59cb\u3057\u3001\u5e38\u306b\u6700\u3082\u8fd1\u3044\u9802\u70b9\u3092\u512a\u5148\u7684\u306b\u8a2a\u554f\u3057\u3001\u5c64\u3054\u3068\u306b\u5916\u5074\u306b\u5c55\u958b\u3057\u3066\u3044\u304d\u307e\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5de6\u4e0a\u306e\u9802\u70b9\u304b\u3089\u958b\u59cb\u3057\u3001\u307e\u305a\u305d\u306e\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u8d70\u67fb\u3057\u3001\u6b21\u306b\u6b21\u306e\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u8d70\u67fb\u3057\u3001\u4ee5\u4e0b\u540c\u69d8\u306b\u3001\u3059\u3079\u3066\u306e\u9802\u70b9\u304c\u8a2a\u554f\u3055\u308c\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002

        \u56f3 9-9 \u00a0 \u30b0\u30e9\u30d5\u306e\u5e45\u512a\u5148\u8d70\u67fb

        "},{"location":"chapter_graph/graph_traversal/#1","title":"1. \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u88c5","text":"

        BFS\u306f\u901a\u5e38\u30ad\u30e5\u30fc\u306e\u52a9\u3051\u3092\u501f\u308a\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\uff08\u4e0b\u8a18\u306e\u30b3\u30fc\u30c9\u53c2\u7167\uff09\u3002\u30ad\u30e5\u30fc\u306f\u300c\u5148\u5165\u5148\u51fa\u300d\u3067\u3001\u3053\u308c\u306f\u300c\u8fd1\u304f\u304b\u3089\u9060\u304f\u3078\u300d\u8d70\u67fb\u3059\u308bBFS\u306e\u8003\u3048\u65b9\u3068\u4e00\u81f4\u3057\u307e\u3059\u3002

        1. \u958b\u59cb\u9802\u70b9startVet\u3092\u30ad\u30e5\u30fc\u306b\u8ffd\u52a0\u3057\u3001\u30eb\u30fc\u30d7\u3092\u958b\u59cb\u3057\u307e\u3059\u3002
        2. \u30eb\u30fc\u30d7\u306e\u5404\u53cd\u5fa9\u3067\u3001\u30ad\u30e5\u30fc\u306e\u5148\u982d\u306e\u9802\u70b9\u3092\u30dd\u30c3\u30d7\u3057\u3001\u305d\u308c\u3092\u8a2a\u554f\u6e08\u307f\u3068\u3057\u3066\u8a18\u9332\u3057\u3001\u6b21\u306b\u305d\u306e\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u30ad\u30e5\u30fc\u306e\u672b\u5c3e\u306b\u8ffd\u52a0\u3057\u307e\u3059\u3002
        3. \u3059\u3079\u3066\u306e\u9802\u70b9\u304c\u8a2a\u554f\u3055\u308c\u308b\u307e\u3067\u624b\u98062.\u3092\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002

        \u9802\u70b9\u306e\u518d\u8a2a\u554f\u3092\u9632\u3050\u305f\u3081\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8visited\u3092\u4f7f\u7528\u3057\u3066\u3069\u306e\u30ce\u30fc\u30c9\u304c\u8a2a\u554f\u3055\u308c\u305f\u304b\u3092\u8a18\u9332\u3057\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_bfs.py
        def graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n    \"\"\"\u5e45\u512a\u5148\u8d70\u67fb\"\"\"\n    # \u96a3\u63a5\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3057\u3001\u6307\u5b9a\u3055\u308c\u305f\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u53d6\u5f97\n    # \u9802\u70b9\u8d70\u67fb\u30b7\u30fc\u30b1\u30f3\u30b9\n    res = []\n    # \u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8\u3001\u8a2a\u554f\u6e08\u307f\u9802\u70b9\u3092\u8a18\u9332\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    visited = set[Vertex]([start_vet])\n    # BFS\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u308b\u30ad\u30e5\u30fc\n    que = deque[Vertex]([start_vet])\n    # \u9802\u70b9vet\u304b\u3089\u958b\u59cb\u3057\u3001\u3059\u3079\u3066\u306e\u9802\u70b9\u304c\u8a2a\u554f\u3055\u308c\u308b\u307e\u3067\u30eb\u30fc\u30d7\n    while len(que) > 0:\n        vet = que.popleft()  # \u30ad\u30e5\u30fc\u306e\u5148\u982d\u306e\u9802\u70b9\u3092\u30c7\u30ad\u30e5\u30fc\n        res.append(vet)  # \u8a2a\u554f\u6e08\u307f\u9802\u70b9\u3092\u8a18\u9332\n        # \u305d\u306e\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u8d70\u67fb\n        for adj_vet in graph.adj_list[vet]:\n            if adj_vet in visited:\n                continue  # \u65e2\u306b\u8a2a\u554f\u6e08\u307f\u306e\u9802\u70b9\u3092\u30b9\u30ad\u30c3\u30d7\n            que.append(adj_vet)  # \u672a\u8a2a\u554f\u306e\u9802\u70b9\u306e\u307f\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\n            visited.add(adj_vet)  # \u9802\u70b9\u3092\u8a2a\u554f\u6e08\u307f\u3068\u3057\u3066\u30de\u30fc\u30af\n    # \u9802\u70b9\u8d70\u67fb\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u8fd4\u3059\n    return res\n
        graph_bfs.cpp
        /* \u5e45\u512a\u5148\u8d70\u67fb */\n// \u96a3\u63a5\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3057\u3001\u6307\u5b9a\u3055\u308c\u305f\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u53d6\u5f97\nvector<Vertex *> graphBFS(GraphAdjList &graph, Vertex *startVet) {\n    // \u9802\u70b9\u8d70\u67fb\u9806\u5e8f\n    vector<Vertex *> res;\n    // \u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8\u3001\u8a2a\u554f\u6e08\u307f\u9802\u70b9\u3092\u8a18\u9332\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    unordered_set<Vertex *> visited = {startVet};\n    // BFS\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u308b\u30ad\u30e5\u30fc\n    queue<Vertex *> que;\n    que.push(startVet);\n    // \u9802\u70b9vet\u304b\u3089\u958b\u59cb\u3057\u3001\u3059\u3079\u3066\u306e\u9802\u70b9\u304c\u8a2a\u554f\u3055\u308c\u308b\u307e\u3067\u30eb\u30fc\u30d7\n    while (!que.empty()) {\n        Vertex *vet = que.front();\n        que.pop();          // \u30ad\u30e5\u30fc\u306e\u5148\u982d\u306e\u9802\u70b9\u3092\u30c7\u30ad\u30e5\u30fc\n        res.push_back(vet); // \u8a2a\u554f\u6e08\u307f\u9802\u70b9\u3092\u8a18\u9332\n        // \u305d\u306e\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u8d70\u67fb\n        for (auto adjVet : graph.adjList[vet]) {\n            if (visited.count(adjVet))\n                continue;            // \u3059\u3067\u306b\u8a2a\u554f\u6e08\u307f\u306e\u9802\u70b9\u3092\u30b9\u30ad\u30c3\u30d7\n            que.push(adjVet);        // \u672a\u8a2a\u554f\u306e\u9802\u70b9\u306e\u307f\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\n            visited.emplace(adjVet); // \u9802\u70b9\u3092\u8a2a\u554f\u6e08\u307f\u3068\u3057\u3066\u30de\u30fc\u30af\n        }\n    }\n    // \u9802\u70b9\u8d70\u67fb\u9806\u5e8f\u3092\u8fd4\u3059\n    return res;\n}\n
        graph_bfs.java
        /* \u5e45\u512a\u5148\u8d70\u67fb */\n// \u96a3\u63a5\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3057\u3001\u6307\u5b9a\u3057\u305f\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u53d6\u5f97\nList<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {\n    // \u9802\u70b9\u8d70\u67fb\u9806\u5e8f\n    List<Vertex> res = new ArrayList<>();\n    // \u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8\u3001\u8a2a\u554f\u6e08\u307f\u306e\u9802\u70b9\u3092\u8a18\u9332\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    Set<Vertex> visited = new HashSet<>();\n    visited.add(startVet);\n    // BFS \u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3059\u308b\u30ad\u30e5\u30fc\n    Queue<Vertex> que = new LinkedList<>();\n    que.offer(startVet);\n    // \u9802\u70b9 vet \u304b\u3089\u958b\u59cb\u3057\u3001\u3059\u3079\u3066\u306e\u9802\u70b9\u304c\u8a2a\u554f\u3055\u308c\u308b\u307e\u3067\u30eb\u30fc\u30d7\n    while (!que.isEmpty()) {\n        Vertex vet = que.poll(); // \u30ad\u30e5\u30fc\u306e\u5148\u982d\u306e\u9802\u70b9\u3092\u30c7\u30ad\u30e5\u30fc\n        res.add(vet);            // \u8a2a\u554f\u3057\u305f\u9802\u70b9\u3092\u8a18\u9332\n        // \u305d\u306e\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u8d70\u67fb\n        for (Vertex adjVet : graph.adjList.get(vet)) {\n            if (visited.contains(adjVet))\n                continue;        // \u3059\u3067\u306b\u8a2a\u554f\u6e08\u307f\u306e\u9802\u70b9\u3092\u30b9\u30ad\u30c3\u30d7\n            que.offer(adjVet);   // \u672a\u8a2a\u554f\u306e\u9802\u70b9\u306e\u307f\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\n            visited.add(adjVet); // \u9802\u70b9\u3092\u8a2a\u554f\u6e08\u307f\u3068\u3057\u3066\u30de\u30fc\u30af\n        }\n    }\n    // \u9802\u70b9\u8d70\u67fb\u9806\u5e8f\u3092\u8fd4\u3059\n    return res;\n}\n
        graph_bfs.cs
        [class]{graph_bfs}-[func]{GraphBFS}\n
        graph_bfs.go
        [class]{}-[func]{graphBFS}\n
        graph_bfs.swift
        [class]{}-[func]{graphBFS}\n
        graph_bfs.js
        [class]{}-[func]{graphBFS}\n
        graph_bfs.ts
        [class]{}-[func]{graphBFS}\n
        graph_bfs.dart
        [class]{}-[func]{graphBFS}\n
        graph_bfs.rs
        [class]{}-[func]{graph_bfs}\n
        graph_bfs.c
        [class]{Queue}-[func]{}\n\n[class]{}-[func]{isVisited}\n\n[class]{}-[func]{graphBFS}\n
        graph_bfs.kt
        [class]{}-[func]{graphBFS}\n
        graph_bfs.rb
        [class]{}-[func]{graph_bfs}\n

        \u30b3\u30fc\u30c9\u306f\u6bd4\u8f03\u7684\u62bd\u8c61\u7684\u3067\u3059\u304c\u3001\u4e0b\u56f3\u3068\u6bd4\u8f03\u3059\u308b\u3053\u3068\u3067\u3088\u308a\u826f\u304f\u7406\u89e3\u3067\u304d\u307e\u3059\u3002

        <1><2><3><4><5><6><7><8><9><10><11>

        \u56f3 9-10 \u00a0 \u30b0\u30e9\u30d5\u306e\u5e45\u512a\u5148\u63a2\u7d22\u306e\u624b\u9806

        \u5e45\u512a\u5148\u8d70\u67fb\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u306f\u4e00\u610f\u3067\u3059\u304b\uff1f

        \u4e00\u610f\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u5e45\u512a\u5148\u8d70\u67fb\u306f\u300c\u8fd1\u304f\u304b\u3089\u9060\u304f\u300d\u306e\u9806\u5e8f\u3067\u8d70\u67fb\u3059\u308b\u3053\u3068\u306e\u307f\u3092\u8981\u6c42\u3057\u3001\u540c\u3058\u8ddd\u96e2\u306e\u9802\u70b9\u306e\u8d70\u67fb\u9806\u5e8f\u306f\u4efb\u610f\u306b\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u4e0a\u56f3\u3067\u306f\u3001\u9802\u70b9\\(1\\)\u3068\\(3\\)\u306e\u8a2a\u554f\u9806\u5e8f\u3092\u4ea4\u63db\u3067\u304d\u307e\u3059\u3057\u3001\u9802\u70b9\\(2\\)\u3001\\(4\\)\u3001\\(6\\)\u306e\u9806\u5e8f\u3082\u540c\u69d8\u3067\u3059\u3002

        "},{"location":"chapter_graph/graph_traversal/#2","title":"2. \u00a0 \u8a08\u7b97\u91cf\u5206\u6790","text":"

        \u6642\u9593\u8a08\u7b97\u91cf\uff1a\u3059\u3079\u3066\u306e\u9802\u70b9\u304c\u4e00\u5ea6\u305a\u3064\u30a8\u30f3\u30ad\u30e5\u30fc\u304a\u3088\u3073\u30c7\u30ad\u30e5\u30fc\u3055\u308c\u3001\\(O(|V|)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u96a3\u63a5\u9802\u70b9\u3092\u8d70\u67fb\u3059\u308b\u904e\u7a0b\u3067\u3001\u7121\u5411\u30b0\u30e9\u30d5\u3067\u3042\u308b\u305f\u3081\u3001\u3059\u3079\u3066\u306e\u8fba\u304c\\(2\\)\u56de\u8a2a\u554f\u3055\u308c\u3001\\(O(2|E|)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u5168\u4f53\u3067\\(O(|V| + |E|)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        \u7a7a\u9593\u8a08\u7b97\u91cf\uff1a\u30ea\u30b9\u30c8res\u3001\u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8visited\u3001\u30ad\u30e5\u30fcque\u306e\u6700\u5927\u9802\u70b9\u6570\u306f\\(|V|\\)\u3067\u3001\\(O(|V|)\\)\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        "},{"location":"chapter_graph/graph_traversal/#932","title":"9.3.2 \u00a0 \u6df1\u3055\u512a\u5148\u63a2\u7d22","text":"

        \u6df1\u3055\u512a\u5148\u63a2\u7d22\u306f\u53ef\u80fd\u306a\u9650\u308a\u9060\u304f\u307e\u3067\u884c\u304d\u3001\u305d\u308c\u4ee5\u4e0a\u306e\u30d1\u30b9\u304c\u306a\u3044\u5834\u5408\u306b\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30af\u3059\u308b\u8d70\u67fb\u65b9\u6cd5\u3067\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5de6\u4e0a\u306e\u9802\u70b9\u304b\u3089\u958b\u59cb\u3057\u3001\u305d\u308c\u4ee5\u4e0a\u306e\u30d1\u30b9\u304c\u306a\u304f\u306a\u308b\u307e\u3067\u73fe\u5728\u306e\u9802\u70b9\u306e\u3044\u305a\u308c\u304b\u306e\u96a3\u63a5\u9802\u70b9\u3092\u8a2a\u554f\u3057\u3001\u6b21\u306b\u623b\u3063\u3066\u7d9a\u884c\u3057\u3001\u3059\u3079\u3066\u306e\u9802\u70b9\u304c\u8d70\u67fb\u3055\u308c\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002

        \u56f3 9-11 \u00a0 \u30b0\u30e9\u30d5\u306e\u6df1\u3055\u512a\u5148\u8d70\u67fb

        "},{"location":"chapter_graph/graph_traversal/#1_1","title":"1. \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u88c5","text":"

        \u3053\u306e\u300c\u53ef\u80fd\u306a\u9650\u308a\u9060\u304f\u307e\u3067\u884c\u3063\u3066\u304b\u3089\u623b\u308b\u300d\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d1\u30e9\u30c0\u30a4\u30e0\u306f\u901a\u5e38\u518d\u5e30\u306b\u57fa\u3065\u3044\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002\u5e45\u512a\u5148\u63a2\u7d22\u3068\u540c\u69d8\u306b\u3001\u6df1\u3055\u512a\u5148\u63a2\u7d22\u3067\u3082\u3001\u518d\u8a2a\u554f\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u8a2a\u554f\u6e08\u307f\u9802\u70b9\u3092\u8a18\u9332\u3059\u308b\u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8visited\u306e\u52a9\u3051\u304c\u5fc5\u8981\u3067\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_dfs.py
        def dfs(graph: GraphAdjList, visited: set[Vertex], res: list[Vertex], vet: Vertex):\n    \"\"\"\u6df1\u3055\u512a\u5148\u8d70\u67fb\u306e\u30d8\u30eb\u30d1\u30fc\u95a2\u6570\"\"\"\n    res.append(vet)  # \u8a2a\u554f\u6e08\u307f\u9802\u70b9\u3092\u8a18\u9332\n    visited.add(vet)  # \u9802\u70b9\u3092\u8a2a\u554f\u6e08\u307f\u3068\u3057\u3066\u30de\u30fc\u30af\n    # \u305d\u306e\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u8d70\u67fb\n    for adjVet in graph.adj_list[vet]:\n        if adjVet in visited:\n            continue  # \u65e2\u306b\u8a2a\u554f\u6e08\u307f\u306e\u9802\u70b9\u3092\u30b9\u30ad\u30c3\u30d7\n        # \u96a3\u63a5\u9802\u70b9\u3092\u518d\u5e30\u7684\u306b\u8a2a\u554f\n        dfs(graph, visited, res, adjVet)\n\ndef graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n    \"\"\"\u6df1\u3055\u512a\u5148\u8d70\u67fb\"\"\"\n    # \u96a3\u63a5\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3057\u3001\u6307\u5b9a\u3055\u308c\u305f\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u53d6\u5f97\n    # \u9802\u70b9\u8d70\u67fb\u30b7\u30fc\u30b1\u30f3\u30b9\n    res = []\n    # \u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8\u3001\u8a2a\u554f\u6e08\u307f\u9802\u70b9\u3092\u8a18\u9332\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    visited = set[Vertex]()\n    dfs(graph, visited, res, start_vet)\n    return res\n
        graph_dfs.cpp
        /* \u6df1\u3055\u512a\u5148\u8d70\u67fb\u30d8\u30eb\u30d1\u30fc\u95a2\u6570 */\nvoid dfs(GraphAdjList &graph, unordered_set<Vertex *> &visited, vector<Vertex *> &res, Vertex *vet) {\n    res.push_back(vet);   // \u8a2a\u554f\u6e08\u307f\u9802\u70b9\u3092\u8a18\u9332\n    visited.emplace(vet); // \u9802\u70b9\u3092\u8a2a\u554f\u6e08\u307f\u3068\u3057\u3066\u30de\u30fc\u30af\n    // \u305d\u306e\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u8d70\u67fb\n    for (Vertex *adjVet : graph.adjList[vet]) {\n        if (visited.count(adjVet))\n            continue; // \u3059\u3067\u306b\u8a2a\u554f\u6e08\u307f\u306e\u9802\u70b9\u3092\u30b9\u30ad\u30c3\u30d7\n        // \u96a3\u63a5\u9802\u70b9\u3092\u518d\u5e30\u7684\u306b\u8a2a\u554f\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* \u6df1\u3055\u512a\u5148\u8d70\u67fb */\n// \u96a3\u63a5\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3057\u3001\u6307\u5b9a\u3055\u308c\u305f\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u53d6\u5f97\nvector<Vertex *> graphDFS(GraphAdjList &graph, Vertex *startVet) {\n    // \u9802\u70b9\u8d70\u67fb\u9806\u5e8f\n    vector<Vertex *> res;\n    // \u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8\u3001\u8a2a\u554f\u6e08\u307f\u9802\u70b9\u3092\u8a18\u9332\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    unordered_set<Vertex *> visited;\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
        graph_dfs.java
        /* \u6df1\u3055\u512a\u5148\u8d70\u67fb\u306e\u88dc\u52a9\u95a2\u6570 */\nvoid dfs(GraphAdjList graph, Set<Vertex> visited, List<Vertex> res, Vertex vet) {\n    res.add(vet);     // \u8a2a\u554f\u3057\u305f\u9802\u70b9\u3092\u8a18\u9332\n    visited.add(vet); // \u9802\u70b9\u3092\u8a2a\u554f\u6e08\u307f\u3068\u3057\u3066\u30de\u30fc\u30af\n    // \u305d\u306e\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u8d70\u67fb\n    for (Vertex adjVet : graph.adjList.get(vet)) {\n        if (visited.contains(adjVet))\n            continue; // \u3059\u3067\u306b\u8a2a\u554f\u6e08\u307f\u306e\u9802\u70b9\u3092\u30b9\u30ad\u30c3\u30d7\n        // \u96a3\u63a5\u9802\u70b9\u3092\u518d\u5e30\u7684\u306b\u8a2a\u554f\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* \u6df1\u3055\u512a\u5148\u8d70\u67fb */\n// \u96a3\u63a5\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3057\u3001\u6307\u5b9a\u3057\u305f\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u53d6\u5f97\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n    // \u9802\u70b9\u8d70\u67fb\u9806\u5e8f\n    List<Vertex> res = new ArrayList<>();\n    // \u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8\u3001\u8a2a\u554f\u6e08\u307f\u306e\u9802\u70b9\u3092\u8a18\u9332\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    Set<Vertex> visited = new HashSet<>();\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
        graph_dfs.cs
        [class]{graph_dfs}-[func]{DFS}\n\n[class]{graph_dfs}-[func]{GraphDFS}\n
        graph_dfs.go
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{graphDFS}\n
        graph_dfs.swift
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{graphDFS}\n
        graph_dfs.js
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{graphDFS}\n
        graph_dfs.ts
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{graphDFS}\n
        graph_dfs.dart
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{graphDFS}\n
        graph_dfs.rs
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{graph_dfs}\n
        graph_dfs.c
        [class]{}-[func]{isVisited}\n\n[class]{}-[func]{dfs}\n\n[class]{}-[func]{graphDFS}\n
        graph_dfs.kt
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{graphDFS}\n
        graph_dfs.rb
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{graph_dfs}\n

        \u6df1\u3055\u512a\u5148\u63a2\u7d22\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d7\u30ed\u30bb\u30b9\u3092\u4e0b\u56f3\u306b\u793a\u3057\u307e\u3059\u3002

        • \u7834\u7dda\u306f\u4e0b\u5411\u304d\u306e\u518d\u5e30\u3092\u8868\u3057\u3001\u65b0\u3057\u3044\u9802\u70b9\u3092\u8a2a\u554f\u3059\u308b\u305f\u3081\u306b\u65b0\u3057\u3044\u518d\u5e30\u30e1\u30bd\u30c3\u30c9\u304c\u958b\u59cb\u3055\u308c\u305f\u3053\u3068\u3092\u793a\u3057\u307e\u3059\u3002
        • \u66f2\u7dda\u306e\u7834\u7dda\u306f\u4e0a\u5411\u304d\u306e\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30af\u3092\u8868\u3057\u3001\u3053\u306e\u518d\u5e30\u30e1\u30bd\u30c3\u30c9\u304c\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u304c\u958b\u59cb\u3055\u308c\u305f\u4f4d\u7f6e\u306b\u623b\u3063\u305f\u3053\u3068\u3092\u793a\u3057\u307e\u3059\u3002

        \u7406\u89e3\u3092\u6df1\u3081\u308b\u305f\u3081\u3001\u4e0b\u56f3\u3068\u30b3\u30fc\u30c9\u3092\u7d44\u307f\u5408\u308f\u305b\u3066\u3001DFS\u30d7\u30ed\u30bb\u30b9\u5168\u4f53\u3092\u982d\u306e\u4e2d\u3067\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\uff08\u307e\u305f\u306f\u63cf\u753b\uff09\u3059\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002\u5404\u518d\u5e30\u30e1\u30bd\u30c3\u30c9\u304c\u3044\u3064\u958b\u59cb\u3055\u308c\u3001\u3044\u3064\u623b\u308b\u304b\u3092\u542b\u3081\u3066\u3067\u3059\u3002

        <1><2><3><4><5><6><7><8><9><10><11>

        \u56f3 9-12 \u00a0 \u30b0\u30e9\u30d5\u306e\u6df1\u3055\u512a\u5148\u63a2\u7d22\u306e\u624b\u9806

        \u6df1\u3055\u512a\u5148\u8d70\u67fb\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u306f\u4e00\u610f\u3067\u3059\u304b\uff1f

        \u5e45\u512a\u5148\u8d70\u67fb\u3068\u540c\u69d8\u306b\u3001\u6df1\u3055\u512a\u5148\u8d70\u67fb\u30b7\u30fc\u30b1\u30f3\u30b9\u306e\u9806\u5e8f\u3082\u4e00\u610f\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u3042\u308b\u9802\u70b9\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u3069\u306e\u65b9\u5411\u3092\u6700\u521d\u306b\u63a2\u7d22\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u3002\u3064\u307e\u308a\u3001\u96a3\u63a5\u9802\u70b9\u306e\u9806\u5e8f\u306f\u4efb\u610f\u306b\u30b7\u30e3\u30c3\u30d5\u30eb\u3067\u304d\u307e\u3059\u304c\u3001\u3059\u3079\u3066\u6df1\u3055\u512a\u5148\u8d70\u67fb\u306e\u4e00\u90e8\u3067\u3059\u3002

        \u6728\u306e\u8d70\u67fb\u3092\u4f8b\u306b\u53d6\u308b\u3068\u3001\u300c\u6839 \\(\\rightarrow\\) \u5de6 \\(\\rightarrow\\) \u53f3\u300d\u3001\u300c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u300d\u3001\u300c\u5de6 \\(\\rightarrow\\) \u53f3 \\(\\rightarrow\\) \u6839\u300d\u306f\u3001\u305d\u308c\u305e\u308c\u524d\u9806\u3001\u4e2d\u9806\u3001\u5f8c\u9806\u8d70\u67fb\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002\u3053\u308c\u3089\u306f3\u3064\u306e\u7570\u306a\u308b\u8d70\u67fb\u512a\u5148\u5ea6\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u304c\u30013\u3064\u3059\u3079\u3066\u304c\u6df1\u3055\u512a\u5148\u8d70\u67fb\u3068\u898b\u306a\u3055\u308c\u307e\u3059\u3002

        "},{"location":"chapter_graph/graph_traversal/#2_1","title":"2. \u00a0 \u8a08\u7b97\u91cf\u5206\u6790","text":"

        \u6642\u9593\u8a08\u7b97\u91cf\uff1a\u3059\u3079\u3066\u306e\u9802\u70b9\u304c\u4e00\u5ea6\u8a2a\u554f\u3055\u308c\u3001\\(O(|V|)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u3059\u3079\u3066\u306e\u8fba\u304c2\u56de\u8a2a\u554f\u3055\u308c\u3001\\(O(2|E|)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u5168\u4f53\u3067\\(O(|V| + |E|)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        \u7a7a\u9593\u8a08\u7b97\u91cf\uff1a\u30ea\u30b9\u30c8res\u3001\u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8visited\u306e\u6700\u5927\u9802\u70b9\u6570\u306f\\(|V|\\)\u3067\u3001\u6700\u5927\u518d\u5e30\u6df1\u5ea6\u306f\\(|V|\\)\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\\(O(|V|)\\)\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        "},{"location":"chapter_graph/summary/","title":"9.4 \u00a0 \u307e\u3068\u3081","text":""},{"location":"chapter_graph/summary/#1","title":"1. \u00a0 \u91cd\u8981\u306a\u5fa9\u7fd2","text":"
        • \u30b0\u30e9\u30d5\u306f\u9802\u70b9\u3068\u8fba\u3067\u69cb\u6210\u3055\u308c\u307e\u3059\u3002\u9802\u70b9\u306e\u96c6\u5408\u3068\u8fba\u306e\u96c6\u5408\u3068\u3057\u3066\u8a18\u8ff0\u3067\u304d\u307e\u3059\u3002
        • \u7dda\u5f62\u95a2\u4fc2\uff08\u9023\u7d50\u30ea\u30b9\u30c8\u306a\u3069\uff09\u3084\u968e\u5c64\u95a2\u4fc2\uff08\u6728\u306a\u3069\uff09\u3068\u6bd4\u8f03\u3057\u3066\u3001\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u95a2\u4fc2\uff08\u30b0\u30e9\u30d5\uff09\u306f\u3088\u308a\u5927\u304d\u306a\u67d4\u8edf\u6027\u3092\u63d0\u4f9b\u3057\u3001\u3088\u308a\u8907\u96d1\u306b\u306a\u308a\u307e\u3059\u3002
        • \u6709\u5411\u30b0\u30e9\u30d5\u3067\u306f\u3001\u8fba\u306b\u65b9\u5411\u304c\u3042\u308a\u307e\u3059\u3002\u9023\u7d50\u30b0\u30e9\u30d5\u3067\u306f\u3001\u4efb\u610f\u306e\u9802\u70b9\u304b\u3089\u4ed6\u306e\u4efb\u610f\u306e\u9802\u70b9\u306b\u5230\u9054\u3067\u304d\u307e\u3059\u3002\u91cd\u307f\u4ed8\u304d\u30b0\u30e9\u30d5\u3067\u306f\u3001\u5404\u8fba\u306b\u95a2\u9023\u3059\u308b\u91cd\u307f\u5909\u6570\u304c\u3042\u308a\u307e\u3059\u3002
        • \u96a3\u63a5\u884c\u5217\u306f\u3001\u884c\u5217\uff082\u6b21\u5143\u914d\u5217\uff09\u3092\u4f7f\u7528\u3057\u3066\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3059\u308b\u65b9\u6cd5\u3067\u3059\u3002\u884c\u3068\u5217\u306f\u9802\u70b9\u3092\u8868\u3057\u307e\u3059\u3002\u884c\u5217\u8981\u7d20\u306e\u5024\u306f\u30012\u3064\u306e\u9802\u70b9\u9593\u306b\u8fba\u304c\u3042\u308b\u304b\u3069\u3046\u304b\u3092\u793a\u3057\u3001\u8fba\u304c\u3042\u308b\u5834\u5408\u306f\\(1\\)\u3001\u306a\u3044\u5834\u5408\u306f\\(0\\)\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u96a3\u63a5\u884c\u5217\u306f\u8fba\u306e\u8ffd\u52a0\u3001\u524a\u9664\u3001\u30c1\u30a7\u30c3\u30af\u306a\u3069\u306e\u64cd\u4f5c\u306b\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3059\u304c\u3001\u3088\u308a\u591a\u304f\u306e\u30b9\u30da\u30fc\u30b9\u304c\u5fc5\u8981\u3067\u3059\u3002
        • \u96a3\u63a5\u30ea\u30b9\u30c8\u306f\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u96c6\u5408\u3092\u4f7f\u7528\u3057\u3066\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3059\u308b\u3082\u3046\u4e00\u3064\u306e\u4e00\u822c\u7684\u306a\u65b9\u6cd5\u3067\u3059\u3002\u30b0\u30e9\u30d5\u5185\u306e\u5404\u9802\u70b9\u306b\u306f\u3001\u305d\u306e\u96a3\u63a5\u3059\u308b\u3059\u3079\u3066\u306e\u9802\u70b9\u3092\u542b\u3080\u30ea\u30b9\u30c8\u304c\u3042\u308a\u307e\u3059\u3002\\(i\\)\u756a\u76ee\u306e\u30ea\u30b9\u30c8\u306f\u9802\u70b9\\(i\\)\u3092\u8868\u3057\u307e\u3059\u3002\u96a3\u63a5\u30ea\u30b9\u30c8\u306f\u96a3\u63a5\u884c\u5217\u3068\u6bd4\u8f03\u3057\u3066\u3088\u308a\u5c11\u306a\u3044\u30b9\u30da\u30fc\u30b9\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u8fba\u3092\u898b\u3064\u3051\u308b\u305f\u3081\u306b\u30ea\u30b9\u30c8\u3092\u8d70\u67fb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u6642\u9593\u52b9\u7387\u306f\u4f4e\u304f\u306a\u308a\u307e\u3059\u3002
        • \u96a3\u63a5\u30ea\u30b9\u30c8\u306e\u9023\u7d50\u30ea\u30b9\u30c8\u304c\u5341\u5206\u306b\u9577\u304f\u306a\u3063\u305f\u3068\u304d\u3001\u30eb\u30c3\u30af\u30a2\u30c3\u30d7\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u308b\u305f\u3081\u306b\u8d64\u9ed2\u6728\u3084\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306b\u5909\u63db\u3067\u304d\u307e\u3059\u3002
        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u8a2d\u8a08\u306e\u89b3\u70b9\u304b\u3089\u3001\u96a3\u63a5\u884c\u5217\u306f\u300c\u7a7a\u9593\u3068\u6642\u9593\u306e\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u300d\u306e\u6982\u5ff5\u3092\u53cd\u6620\u3057\u3001\u96a3\u63a5\u30ea\u30b9\u30c8\u306f\u300c\u6642\u9593\u3068\u7a7a\u9593\u306e\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u300d\u3092\u53cd\u6620\u3057\u307e\u3059\u3002
        • \u30b0\u30e9\u30d5\u306f\u3001\u30bd\u30fc\u30b7\u30e3\u30eb\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u3084\u5730\u4e0b\u9244\u8def\u7dda\u306a\u3069\u3001\u3055\u307e\u3056\u307e\u306a\u73fe\u5b9f\u4e16\u754c\u306e\u30b7\u30b9\u30c6\u30e0\u3092\u30e2\u30c7\u30eb\u5316\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002
        • \u6728\u306f\u30b0\u30e9\u30d5\u306e\u7279\u5225\u306a\u30b1\u30fc\u30b9\u3067\u3042\u308a\u3001\u6728\u306e\u8d70\u67fb\u3082\u30b0\u30e9\u30d5\u8d70\u67fb\u306e\u7279\u5225\u306a\u30b1\u30fc\u30b9\u3067\u3059\u3002
        • \u30b0\u30e9\u30d5\u306e\u5e45\u512a\u5148\u8d70\u67fb\u306f\u3001\u8fd1\u304f\u304b\u3089\u9060\u304f\u3078\u3068\u5c64\u3054\u3068\u306b\u62e1\u5f35\u3059\u308b\u63a2\u7d22\u65b9\u6cd5\u3067\u3001\u901a\u5e38\u30ad\u30e5\u30fc\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u30b0\u30e9\u30d5\u306e\u6df1\u3055\u512a\u5148\u8d70\u67fb\u306f\u3001\u305d\u308c\u4ee5\u4e0a\u306e\u30d1\u30b9\u304c\u306a\u3044\u5834\u5408\u306b\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30af\u3059\u308b\u524d\u306b\u3001\u307e\u305a\u7d42\u7aef\u306b\u5230\u9054\u3059\u308b\u3053\u3068\u3092\u512a\u5148\u3059\u308b\u63a2\u7d22\u65b9\u6cd5\u3067\u3059\u3002\u3057\u3070\u3057\u3070\u518d\u5e30\u3092\u4f7f\u7528\u3057\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002
        "},{"location":"chapter_graph/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q: \u30d1\u30b9\u306f\u9802\u70b9\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u307e\u3059\u304b\u3001\u305d\u308c\u3068\u3082\u8fba\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u307e\u3059\u304b\uff1f

        \u30b0\u30e9\u30d5\u7406\u8ad6\u3067\u306f\u3001\u30b0\u30e9\u30d5\u5185\u306e\u30d1\u30b9\u306f\u9802\u70b9\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u7d50\u3076\u6709\u9650\u307e\u305f\u306f\u7121\u9650\u306e\u8fba\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3067\u3059\u3002

        \u3053\u306e\u6587\u66f8\u3067\u306f\u3001\u30d1\u30b9\u306f\u9802\u70b9\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3067\u306f\u306a\u304f\u3001\u8fba\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3068\u8003\u3048\u3089\u308c\u307e\u3059\u3002\u3053\u308c\u306f\u30012\u3064\u306e\u9802\u70b9\u3092\u7d50\u3076\u8907\u6570\u306e\u8fba\u304c\u3042\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u305d\u306e\u5834\u5408\u5404\u8fba\u304c\u30d1\u30b9\u306b\u5bfe\u5fdc\u3059\u308b\u305f\u3081\u3067\u3059\u3002

        Q: \u975e\u9023\u7d50\u30b0\u30e9\u30d5\u3067\u306f\u3001\u8d70\u67fb\u3067\u304d\u306a\u3044\u70b9\u304c\u3042\u308a\u307e\u3059\u304b\uff1f

        \u975e\u9023\u7d50\u30b0\u30e9\u30d5\u3067\u306f\u3001\u7279\u5b9a\u306e\u70b9\u304b\u3089\u5230\u9054\u3067\u304d\u306a\u3044\u9802\u70b9\u304c\u5c11\u306a\u304f\u3068\u30821\u3064\u3042\u308a\u307e\u3059\u3002\u975e\u9023\u7d50\u30b0\u30e9\u30d5\u3092\u8d70\u67fb\u3059\u308b\u306b\u306f\u3001\u30b0\u30e9\u30d5\u306e\u3059\u3079\u3066\u306e\u9023\u7d50\u6210\u5206\u3092\u8d70\u67fb\u3059\u308b\u305f\u3081\u306b\u8907\u6570\u306e\u958b\u59cb\u70b9\u3092\u8a2d\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        Q: \u96a3\u63a5\u30ea\u30b9\u30c8\u3067\u3001\u300c\u305d\u306e\u9802\u70b9\u306b\u63a5\u7d9a\u3055\u308c\u305f\u3059\u3079\u3066\u306e\u9802\u70b9\u300d\u306e\u9806\u5e8f\u306f\u91cd\u8981\u3067\u3059\u304b\uff1f

        \u4efb\u610f\u306e\u9806\u5e8f\u3067\u69cb\u3044\u307e\u305b\u3093\u3002\u305f\u3060\u3057\u3001\u5b9f\u969b\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u306f\u3001\u9802\u70b9\u304c\u8ffd\u52a0\u3055\u308c\u305f\u9806\u5e8f\u3084\u9802\u70b9\u5024\u306e\u9806\u5e8f\u306a\u3069\u3001\u7279\u5b9a\u306e\u30eb\u30fc\u30eb\u306b\u5f93\u3063\u3066\u305d\u308c\u3089\u3092\u30bd\u30fc\u30c8\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u7279\u5b9a\u306e\u6975\u5024\u3092\u6301\u3064\u9802\u70b9\u3092\u7d20\u65e9\u304f\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_greedy/","title":"\u7b2c 15 \u7ae0 \u00a0 \u8caa\u6b32\u6cd5","text":"

        Abstract

        \u3072\u307e\u308f\u308a\u306f\u592a\u967d\u306e\u65b9\u3092\u5411\u304d\u3001\u5e38\u306b\u81ea\u5206\u306b\u3068\u3063\u3066\u6700\u5927\u306e\u6210\u9577\u3092\u6c42\u3081\u307e\u3059\u3002

        \u8caa\u6b32\u306a\u6226\u7565\u306f\u3001\u4e00\u9023\u306e\u5358\u7d14\u306a\u9078\u629e\u3092\u901a\u3058\u3066\u3001\u6bb5\u968e\u7684\u306b\u6700\u826f\u306e\u7b54\u3048\u3078\u3068\u5c0e\u304d\u307e\u3059\u3002

        "},{"location":"chapter_greedy/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 15.1 \u00a0 \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0
        • 15.2 \u00a0 \u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c
        • 15.3 \u00a0 \u6700\u5927\u5bb9\u91cf\u554f\u984c
        • 15.4 \u00a0 \u6700\u5927\u7a4d\u5207\u65ad\u554f\u984c
        • 15.5 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_greedy/fractional_knapsack_problem/","title":"15.2 \u00a0 \u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c","text":"

        Question

        \\(n\\) \u500b\u306e\u30a2\u30a4\u30c6\u30e0\u304c\u4e0e\u3048\u3089\u308c\u3001\\(i\\) \u756a\u76ee\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u91cd\u91cf\u306f \\(wgt[i-1]\\) \u3067\u5024\u306f \\(val[i-1]\\) \u3067\u3059\u3002\u5bb9\u91cf\u304c \\(cap\\) \u306e\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u304c\u3042\u308a\u307e\u3059\u3002\u5404\u30a2\u30a4\u30c6\u30e0\u306f1\u56de\u306e\u307f\u9078\u629e\u3067\u304d\u307e\u3059\u304c\u3001\u30a2\u30a4\u30c6\u30e0\u306e\u4e00\u90e8\u3092\u9078\u629e\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u305d\u306e\u5024\u306f\u9078\u629e\u3055\u308c\u305f\u91cd\u91cf\u306e\u5272\u5408\u306b\u57fa\u3065\u3044\u3066\u8a08\u7b97\u3055\u308c\u307e\u3059\u3002\u9650\u3089\u308c\u305f\u5bb9\u91cf\u306e\u4e0b\u3067\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5185\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u6700\u5927\u5024\u306f\u4f55\u3067\u3059\u304b\uff1f\u4f8b\u3092\u4e0b\u306e\u56f3\u306b\u793a\u3057\u307e\u3059\u3002

        \u56f3 15-3 \u00a0 \u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u4f8b\u30c7\u30fc\u30bf

        \u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306f\u5168\u4f53\u7684\u306b0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3068\u975e\u5e38\u306b\u4f3c\u3066\u304a\u308a\u3001\u73fe\u5728\u306e\u30a2\u30a4\u30c6\u30e0 \\(i\\) \u3068\u5bb9\u91cf \\(c\\) \u3092\u542b\u307f\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u9650\u3089\u308c\u305f\u5bb9\u91cf\u5185\u3067\u5024\u3092\u6700\u5927\u5316\u3059\u308b\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u3066\u3044\u307e\u3059\u3002

        \u9055\u3044\u306f\u3001\u3053\u306e\u554f\u984c\u3067\u306f\u30a2\u30a4\u30c6\u30e0\u306e\u4e00\u90e8\u306e\u307f\u3092\u9078\u629e\u3067\u304d\u308b\u3053\u3068\u3067\u3059\u3002\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30a2\u30a4\u30c6\u30e0\u3092\u4efb\u610f\u306b\u5206\u5272\u3057\u3001\u91cd\u91cf\u306e\u5272\u5408\u306b\u57fa\u3065\u3044\u3066\u5bfe\u5fdc\u3059\u308b\u5024\u3092\u8a08\u7b97\u3067\u304d\u307e\u3059\u3002

        1. \u30a2\u30a4\u30c6\u30e0 \\(i\\) \u306b\u3064\u3044\u3066\u3001\u305d\u306e\u5358\u4f4d\u91cd\u91cf\u3042\u305f\u308a\u306e\u5024\u306f \\(val[i-1] / wgt[i-1]\\) \u3067\u3001\u5358\u4f4d\u5024\u3068\u547c\u3070\u308c\u307e\u3059\u3002
        2. \u91cd\u91cf \\(w\\) \u306e\u30a2\u30a4\u30c6\u30e0 \\(i\\) \u306e\u4e00\u90e8\u3092\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u308b\u3068\u3059\u308b\u3068\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u8ffd\u52a0\u3055\u308c\u308b\u5024\u306f \\(w \\times val[i-1] / wgt[i-1]\\) \u3067\u3059\u3002

        \u56f3 15-4 \u00a0 \u30a2\u30a4\u30c6\u30e0\u306e\u5358\u4f4d\u91cd\u91cf\u3042\u305f\u308a\u306e\u5024

        "},{"location":"chapter_greedy/fractional_knapsack_problem/#1","title":"1. \u00a0 \u8caa\u6b32\u6226\u7565\u306e\u6c7a\u5b9a","text":"

        \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5185\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u7dcf\u5024\u3092\u6700\u5927\u5316\u3059\u308b\u3053\u3068\u306f\u3001\u672c\u8cea\u7684\u306b\u5358\u4f4d\u91cd\u91cf\u3042\u305f\u308a\u306e\u5024\u3092\u6700\u5927\u5316\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u3053\u308c\u304b\u3089\u3001\u4e0b\u306e\u56f3\u306b\u793a\u3059\u8caa\u6b32\u6226\u7565\u3092\u5c0e\u51fa\u3067\u304d\u307e\u3059\u3002

        1. \u30a2\u30a4\u30c6\u30e0\u3092\u5358\u4f4d\u5024\u306e\u9ad8\u3044\u9806\u304b\u3089\u4f4e\u3044\u9806\u306b\u30bd\u30fc\u30c8\u3057\u307e\u3059\u3002
        2. \u3059\u3079\u3066\u306e\u30a2\u30a4\u30c6\u30e0\u3092\u53cd\u5fa9\u3057\u3001**\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u6700\u3082\u9ad8\u3044\u5358\u4f4d\u5024\u3092\u6301\u3064\u30a2\u30a4\u30c6\u30e0\u3092\u8caa\u6b32\u306b\u9078\u629e**\u3057\u307e\u3059\u3002
        3. \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u6b8b\u308a\u5bb9\u91cf\u304c\u4e0d\u5341\u5206\u306a\u5834\u5408\u3001\u73fe\u5728\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u4e00\u90e8\u3092\u4f7f\u7528\u3057\u3066\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u3092\u6e80\u305f\u3057\u307e\u3059\u3002

        \u56f3 15-5 \u00a0 \u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u8caa\u6b32\u6226\u7565

        "},{"location":"chapter_greedy/fractional_knapsack_problem/#2","title":"2. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        \u30a2\u30a4\u30c6\u30e0\u3092\u5358\u4f4d\u5024\u3067\u30bd\u30fc\u30c8\u3059\u308b\u305f\u3081\u306b Item \u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3057\u307e\u3057\u305f\u3002\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u304c\u6e80\u305f\u3055\u308c\u308b\u307e\u3067\u30eb\u30fc\u30d7\u3057\u3066\u8caa\u6b32\u306a\u9078\u629e\u3092\u884c\u3044\u3001\u305d\u306e\u5f8c\u7d42\u4e86\u3057\u3066\u89e3\u3092\u8fd4\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby fractional_knapsack.py
        class Item:\n    \"\"\"\u30a2\u30a4\u30c6\u30e0\"\"\"\n\n    def __init__(self, w: int, v: int):\n        self.w = w  # \u30a2\u30a4\u30c6\u30e0\u306e\u91cd\u91cf\n        self.v = v  # \u30a2\u30a4\u30c6\u30e0\u306e\u4fa1\u5024\n\ndef fractional_knapsack(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"\u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u8caa\u6b32\u6cd5\"\"\"\n    # \u30a2\u30a4\u30c6\u30e0\u30ea\u30b9\u30c8\u3092\u4f5c\u6210\u30012 \u3064\u306e\u5c5e\u6027\u3092\u542b\u3080\uff1a\u91cd\u91cf\u3001\u4fa1\u5024\n    items = [Item(w, v) for w, v in zip(wgt, val)]\n    # \u5358\u4f4d\u4fa1\u5024 item.v / item.w \u3067\u9ad8\u3044\u9806\u306b\u30bd\u30fc\u30c8\n    items.sort(key=lambda item: item.v / item.w, reverse=True)\n    # \u8caa\u6b32\u9078\u629e\u3092\u30eb\u30fc\u30d7\n    res = 0\n    for item in items:\n        if item.w <= cap:\n            # \u6b8b\u308a\u5bb9\u91cf\u304c\u5341\u5206\u306a\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0\u5168\u4f53\u3092\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u308b\n            res += item.v\n            cap -= item.w\n        else:\n            # \u6b8b\u308a\u5bb9\u91cf\u304c\u4e0d\u5341\u5206\u306a\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0\u306e\u4e00\u90e8\u3092\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u308b\n            res += (item.v / item.w) * cap\n            # \u6b8b\u308a\u5bb9\u91cf\u304c\u306a\u304f\u306a\u3063\u305f\u305f\u3081\u3001\u30eb\u30fc\u30d7\u3092\u4e2d\u65ad\n            break\n    return res\n
        fractional_knapsack.cpp
        /* \u30a2\u30a4\u30c6\u30e0 */\nclass Item {\n  public:\n    int w; // \u30a2\u30a4\u30c6\u30e0\u306e\u91cd\u91cf\n    int v; // \u30a2\u30a4\u30c6\u30e0\u306e\u4fa1\u5024\n\n    Item(int w, int v) : w(w), v(v) {\n    }\n};\n\n/* \u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u8caa\u6b32\u6cd5 */\ndouble fractionalKnapsack(vector<int> &wgt, vector<int> &val, int cap) {\n    // \u30a2\u30a4\u30c6\u30e0\u30ea\u30b9\u30c8\u3092\u4f5c\u6210\u30012\u3064\u306e\u5c5e\u6027\u3092\u542b\u3080\uff1a\u91cd\u91cf\u3001\u4fa1\u5024\n    vector<Item> items;\n    for (int i = 0; i < wgt.size(); i++) {\n        items.push_back(Item(wgt[i], val[i]));\n    }\n    // \u5358\u4f4d\u4fa1\u5024 item.v / item.w \u3067\u9ad8\u3044\u9806\u306b\u30bd\u30fc\u30c8\n    sort(items.begin(), items.end(), [](Item &a, Item &b) { return (double)a.v / a.w > (double)b.v / b.w; });\n    // \u8caa\u6b32\u9078\u629e\u3092\u30eb\u30fc\u30d7\n    double res = 0;\n    for (auto &item : items) {\n        if (item.w <= cap) {\n            // \u6b8b\u308a\u5bb9\u91cf\u304c\u5341\u5206\u306a\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0\u5168\u4f53\u3092\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u308b\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // \u6b8b\u308a\u5bb9\u91cf\u304c\u4e0d\u5341\u5206\u306a\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0\u306e\u4e00\u90e8\u3092\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u308b\n            res += (double)item.v / item.w * cap;\n            // \u6b8b\u308a\u5bb9\u91cf\u304c\u306a\u304f\u306a\u3063\u305f\u305f\u3081\u3001\u30eb\u30fc\u30d7\u3092\u4e2d\u65ad\n            break;\n        }\n    }\n    return res;\n}\n
        fractional_knapsack.java
        /* \u30a2\u30a4\u30c6\u30e0 */\nclass Item {\n    int w; // \u30a2\u30a4\u30c6\u30e0\u306e\u91cd\u91cf\n    int v; // \u30a2\u30a4\u30c6\u30e0\u306e\u4fa1\u5024\n\n    public Item(int w, int v) {\n        this.w = w;\n        this.v = v;\n    }\n}\n\n/* \u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u8caa\u6b32\u6cd5 */\ndouble fractionalKnapsack(int[] wgt, int[] val, int cap) {\n    // \u30a2\u30a4\u30c6\u30e0\u30ea\u30b9\u30c8\u3092\u4f5c\u6210\u30012\u3064\u306e\u5c5e\u6027\u3092\u542b\u3080\uff1a\u91cd\u91cf\u3001\u4fa1\u5024\n    Item[] items = new Item[wgt.length];\n    for (int i = 0; i < wgt.length; i++) {\n        items[i] = new Item(wgt[i], val[i]);\n    }\n    // \u5358\u4f4d\u4fa1\u5024 item.v / item.w \u3067\u9ad8\u3044\u9806\u306b\u30bd\u30fc\u30c8\n    Arrays.sort(items, Comparator.comparingDouble(item -> -((double) item.v / item.w)));\n    // \u8caa\u6b32\u9078\u629e\u3092\u30eb\u30fc\u30d7\n    double res = 0;\n    for (Item item : items) {\n        if (item.w <= cap) {\n            // \u6b8b\u308a\u5bb9\u91cf\u304c\u5341\u5206\u306a\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0\u5168\u4f53\u3092\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u308b\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // \u6b8b\u308a\u5bb9\u91cf\u304c\u4e0d\u5341\u5206\u306a\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0\u306e\u4e00\u90e8\u3092\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u308b\n            res += (double) item.v / item.w * cap;\n            // \u6b8b\u308a\u5bb9\u91cf\u304c\u306a\u304f\u306a\u3063\u305f\u305f\u3081\u3001\u30eb\u30fc\u30d7\u3092\u4e2d\u65ad\n            break;\n        }\n    }\n    return res;\n}\n
        fractional_knapsack.cs
        [class]{Item}-[func]{}\n\n[class]{fractional_knapsack}-[func]{FractionalKnapsack}\n
        fractional_knapsack.go
        [class]{Item}-[func]{}\n\n[class]{}-[func]{fractionalKnapsack}\n
        fractional_knapsack.swift
        [class]{Item}-[func]{}\n\n[class]{}-[func]{fractionalKnapsack}\n
        fractional_knapsack.js
        [class]{Item}-[func]{}\n\n[class]{}-[func]{fractionalKnapsack}\n
        fractional_knapsack.ts
        [class]{Item}-[func]{}\n\n[class]{}-[func]{fractionalKnapsack}\n
        fractional_knapsack.dart
        [class]{Item}-[func]{}\n\n[class]{}-[func]{fractionalKnapsack}\n
        fractional_knapsack.rs
        [class]{Item}-[func]{}\n\n[class]{}-[func]{fractional_knapsack}\n
        fractional_knapsack.c
        [class]{Item}-[func]{}\n\n[class]{}-[func]{fractionalKnapsack}\n
        fractional_knapsack.kt
        [class]{Item}-[func]{}\n\n[class]{}-[func]{fractionalKnapsack}\n
        fractional_knapsack.rb
        [class]{Item}-[func]{}\n\n[class]{}-[func]{fractional_knapsack}\n

        \u30bd\u30fc\u30c8\u4ee5\u5916\u306b\u3001\u6700\u60aa\u306e\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0\u306e\u30ea\u30b9\u30c8\u5168\u4f53\u3092\u8d70\u67fb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u3067\u3059\u3002\u3053\u3053\u3067 \\(n\\) \u306f\u30a2\u30a4\u30c6\u30e0\u306e\u6570\u3067\u3059\u3002

        Item \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30ea\u30b9\u30c8\u304c\u521d\u671f\u5316\u3055\u308c\u308b\u305f\u3081\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u3067\u3059\u3002

        "},{"location":"chapter_greedy/fractional_knapsack_problem/#3","title":"3. \u00a0 \u6b63\u3057\u3055\u306e\u8a3c\u660e","text":"

        \u80cc\u7406\u6cd5\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u30a2\u30a4\u30c6\u30e0 \\(x\\) \u304c\u6700\u9ad8\u306e\u5358\u4f4d\u5024\u3092\u6301\u3061\u3001\u3042\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u6700\u5927\u5024 res \u3092\u751f\u6210\u3059\u308b\u304c\u3001\u89e3\u306b\u30a2\u30a4\u30c6\u30e0 \\(x\\) \u304c\u542b\u307e\u308c\u3066\u3044\u306a\u3044\u3068\u4eee\u5b9a\u3057\u307e\u3059\u3002

        \u4eca\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u304b\u3089\u4efb\u610f\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u5358\u4f4d\u91cd\u91cf\u3092\u53d6\u308a\u9664\u304d\u3001\u30a2\u30a4\u30c6\u30e0 \\(x\\) \u306e\u5358\u4f4d\u91cd\u91cf\u3067\u7f6e\u304d\u63db\u3048\u307e\u3059\u3002\u30a2\u30a4\u30c6\u30e0 \\(x\\) \u306e\u5358\u4f4d\u5024\u304c\u6700\u9ad8\u3067\u3042\u308b\u305f\u3081\u3001\u7f6e\u304d\u63db\u3048\u5f8c\u306e\u7dcf\u5024\u306f\u78ba\u5b9f\u306b res \u3088\u308a\u5927\u304d\u304f\u306a\u308a\u307e\u3059\u3002\u3053\u308c\u306f res \u304c\u6700\u9069\u89e3\u3067\u3042\u308b\u3068\u3044\u3046\u4eee\u5b9a\u3068\u77db\u76fe\u3057\u3001\u6700\u9069\u89e3\u306b\u306f\u5fc5\u305a\u30a2\u30a4\u30c6\u30e0 \\(x\\) \u304c\u542b\u307e\u308c\u308b\u3053\u3068\u3092\u8a3c\u660e\u3057\u307e\u3059\u3002

        \u3053\u306e\u89e3\u306e\u4ed6\u306e\u30a2\u30a4\u30c6\u30e0\u306b\u3064\u3044\u3066\u3082\u3001\u4e0a\u8a18\u306e\u77db\u76fe\u3092\u69cb\u7bc9\u3067\u304d\u307e\u3059\u3002\u5168\u4f53\u7684\u306b\u3001**\u5358\u4f4d\u5024\u304c\u3088\u308a\u5927\u304d\u3044\u30a2\u30a4\u30c6\u30e0\u306f\u5e38\u306b\u3088\u308a\u826f\u3044\u9078\u629e**\u3067\u3042\u308a\u3001\u8caa\u6b32\u6226\u7565\u304c\u52b9\u679c\u7684\u3067\u3042\u308b\u3053\u3068\u3092\u8a3c\u660e\u3057\u307e\u3059\u3002

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30a2\u30a4\u30c6\u30e0\u306e\u91cd\u91cf\u3068\u5358\u4f4d\u5024\u3092\u305d\u308c\u305e\u308c\u4e8c\u6b21\u5143\u30c1\u30e3\u30fc\u30c8\u306e\u6a2a\u8ef8\u3068\u7e26\u8ef8\u3068\u898b\u306a\u3059\u3068\u3001\u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306f\u300c\u9650\u3089\u308c\u305f\u6a2a\u8ef8\u7bc4\u56f2\u5185\u3067\u56f2\u307e\u308c\u308b\u6700\u5927\u9762\u7a4d\u3092\u6c42\u3081\u308b\u300d\u3053\u3068\u306b\u5909\u63db\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u985e\u63a8\u306f\u3001\u5e7e\u4f55\u5b66\u7684\u89b3\u70b9\u304b\u3089\u8caa\u6b32\u6226\u7565\u306e\u52b9\u679c\u3092\u7406\u89e3\u3059\u308b\u306e\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002

        \u56f3 15-6 \u00a0 \u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u5e7e\u4f55\u5b66\u7684\u8868\u73fe

        "},{"location":"chapter_greedy/greedy_algorithm/","title":"15.1 \u00a0 \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0","text":"

        \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u6700\u9069\u5316\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306e\u4e00\u822c\u7684\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3001\u57fa\u672c\u7684\u306b\u554f\u984c\u306e\u5404\u610f\u601d\u6c7a\u5b9a\u6bb5\u968e\u3067\u6700\u3082\u826f\u3044\u9078\u629e\u3092\u3059\u308b\u3053\u3068\u3001\u3064\u307e\u308a\u5c40\u6240\u7684\u306b\u6700\u9069\u306a\u6c7a\u5b9a\u3092\u8caa\u6b32\u306b\u884c\u3044\u3001\u30b0\u30ed\u30fc\u30d0\u30eb\u306b\u6700\u9069\u306a\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3092\u671b\u307f\u307e\u3059\u3002\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u7c21\u6f54\u3067\u52b9\u7387\u7684\u3067\u3042\u308a\u3001\u591a\u304f\u306e\u5b9f\u7528\u7684\u306a\u554f\u984c\u3067\u5e83\u304f\u4f7f\u7528\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u3001\u3069\u3061\u3089\u3082\u6700\u9069\u5316\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3088\u304f\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u4e21\u8005\u306f\u6700\u9069\u90e8\u5206\u69cb\u9020\u306e\u6027\u8cea\u306b\u4f9d\u5b58\u3059\u308b\u306a\u3069\u3001\u3044\u304f\u3064\u304b\u306e\u985e\u4f3c\u70b9\u3092\u5171\u6709\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u52d5\u4f5c\u65b9\u6cd5\u304c\u7570\u306a\u308a\u307e\u3059\u3002

        • \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u73fe\u5728\u306e\u6c7a\u5b9a\u6bb5\u968e\u3067\u3059\u3079\u3066\u306e\u4ee5\u524d\u306e\u6c7a\u5b9a\u3092\u8003\u616e\u3057\u3001\u904e\u53bb\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4f7f\u7528\u3057\u3066\u73fe\u5728\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002
        • \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u904e\u53bb\u306e\u6c7a\u5b9a\u3092\u8003\u616e\u305b\u305a\u3001\u4ee3\u308f\u308a\u306b\u8caa\u6b32\u306a\u9078\u629e\u3092\u7d9a\u3051\u3001\u554f\u984c\u304c\u89e3\u6c7a\u3055\u308c\u308b\u307e\u3067\u554f\u984c\u306e\u7bc4\u56f2\u3092\u7d99\u7d9a\u7684\u306b\u72ed\u3081\u307e\u3059\u3002

        \u307e\u305a\u3001\u300c\u5b8c\u5168\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u300d\u306e\u7ae0\u3067\u7d39\u4ecb\u3055\u308c\u305f\u300c\u30b3\u30a4\u30f3\u4ea4\u63db\u300d\u306e\u4f8b\u3092\u901a\u3058\u3066\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52d5\u4f5c\u539f\u7406\u3092\u7406\u89e3\u3057\u307e\u3057\u3087\u3046\u3002\u3059\u3067\u306b\u3088\u304f\u77e5\u3063\u3066\u3044\u308b\u3068\u601d\u3044\u307e\u3059\u3002

        Question

        \\(n\\) \u7a2e\u985e\u306e\u30b3\u30a4\u30f3\u304c\u4e0e\u3048\u3089\u308c\u3001\\(i\\) \u756a\u76ee\u306e\u7a2e\u985e\u306e\u30b3\u30a4\u30f3\u306e\u984d\u9762\u306f \\(coins[i - 1]\\) \u3067\u3001\u76ee\u6a19\u91d1\u984d\u306f \\(amt\\) \u3067\u3059\u3002\u5404\u7a2e\u985e\u306e\u30b3\u30a4\u30f3\u306f\u7121\u5236\u9650\u306b\u5229\u7528\u53ef\u80fd\u3067\u3001\u76ee\u6a19\u91d1\u984d\u3092\u69cb\u6210\u3059\u308b\u306e\u306b\u5fc5\u8981\u306a\u6700\u5c0f\u30b3\u30a4\u30f3\u6570\u306f\u4f55\u3067\u3059\u304b\uff1f\u76ee\u6a19\u91d1\u984d\u3092\u69cb\u6210\u3067\u304d\u306a\u3044\u5834\u5408\u306f \\(-1\\) \u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u3053\u306e\u554f\u984c\u3067\u63a1\u7528\u3055\u308c\u308b\u8caa\u6b32\u6226\u7565\u3092\u4e0b\u306e\u56f3\u306b\u793a\u3057\u307e\u3059\u3002\u76ee\u6a19\u91d1\u984d\u304c\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001**\u305d\u308c\u306b\u6700\u3082\u8fd1\u304f\u3001\u305d\u308c\u3092\u8d85\u3048\u306a\u3044\u30b3\u30a4\u30f3\u3092\u8caa\u6b32\u306b\u9078\u629e**\u3057\u3001\u76ee\u6a19\u91d1\u984d\u304c\u6e80\u305f\u3055\u308c\u308b\u307e\u3067\u3053\u306e\u30b9\u30c6\u30c3\u30d7\u3092\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002

        \u56f3 15-1 \u00a0 \u30b3\u30a4\u30f3\u4ea4\u63db\u306e\u8caa\u6b32\u6226\u7565

        \u5b9f\u88c5\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change_greedy.py
        def coin_change_greedy(coins: list[int], amt: int) -> int:\n    \"\"\"\u786c\u8ca8\u4ea4\u63db\uff1a\u8caa\u6b32\u6cd5\"\"\"\n    # coins \u30ea\u30b9\u30c8\u304c\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u308b\u3068\u4eee\u5b9a\n    i = len(coins) - 1\n    count = 0\n    # \u6b8b\u308a\u91d1\u984d\u304c\u306a\u304f\u306a\u308b\u307e\u3067\u8caa\u6b32\u9078\u629e\u3092\u30eb\u30fc\u30d7\n    while amt > 0:\n        # \u6b8b\u308a\u91d1\u984d\u306b\u6700\u3082\u8fd1\u304f\u3001\u305d\u308c\u3088\u308a\u5c0f\u3055\u3044\u786c\u8ca8\u3092\u898b\u3064\u3051\u308b\n        while i > 0 and coins[i] > amt:\n            i -= 1\n        # coins[i] \u3092\u9078\u629e\n        amt -= coins[i]\n        count += 1\n    # \u5b9f\u884c\u53ef\u80fd\u306a\u89e3\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3001-1 \u3092\u8fd4\u3059\n    return count if amt == 0 else -1\n
        coin_change_greedy.cpp
        /* \u786c\u8ca8\u4e21\u66ff\uff1a\u8caa\u6b32\u6cd5 */\nint coinChangeGreedy(vector<int> &coins, int amt) {\n    // \u786c\u8ca8\u30ea\u30b9\u30c8\u304c\u9806\u5e8f\u4ed8\u3051\u3055\u308c\u3066\u3044\u308b\u3068\u4eee\u5b9a\n    int i = coins.size() - 1;\n    int count = 0;\n    // \u6b8b\u308a\u91d1\u984d\u304c\u306a\u304f\u306a\u308b\u307e\u3067\u8caa\u6b32\u9078\u629e\u3092\u30eb\u30fc\u30d7\n    while (amt > 0) {\n        // \u6b8b\u308a\u91d1\u984d\u306b\u8fd1\u304f\u3001\u305d\u308c\u4ee5\u4e0b\u306e\u6700\u5c0f\u786c\u8ca8\u3092\u898b\u3064\u3051\u308b\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // coins[i] \u3092\u9078\u629e\n        amt -= coins[i];\n        count++;\n    }\n    // \u5b9f\u884c\u53ef\u80fd\u306a\u89e3\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3001-1 \u3092\u8fd4\u3059\n    return amt == 0 ? count : -1;\n}\n
        coin_change_greedy.java
        /* \u786c\u8ca8\u4e21\u66ff\uff1a\u8caa\u6b32\u6cd5 */\nint coinChangeGreedy(int[] coins, int amt) {\n    // \u786c\u8ca8\u30ea\u30b9\u30c8\u304c\u9806\u5e8f\u4ed8\u3051\u3055\u308c\u3066\u3044\u308b\u3068\u4eee\u5b9a\n    int i = coins.length - 1;\n    int count = 0;\n    // \u6b8b\u308a\u91d1\u984d\u304c\u306a\u304f\u306a\u308b\u307e\u3067\u8caa\u6b32\u9078\u629e\u3092\u30eb\u30fc\u30d7\n    while (amt > 0) {\n        // \u6b8b\u308a\u91d1\u984d\u306b\u8fd1\u304f\u3001\u305d\u308c\u4ee5\u4e0b\u306e\u6700\u5c0f\u786c\u8ca8\u3092\u898b\u3064\u3051\u308b\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // coins[i] \u3092\u9078\u629e\n        amt -= coins[i];\n        count++;\n    }\n    // \u5b9f\u884c\u53ef\u80fd\u306a\u89e3\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3001-1 \u3092\u8fd4\u3059\n    return amt == 0 ? count : -1;\n}\n
        coin_change_greedy.cs
        [class]{coin_change_greedy}-[func]{CoinChangeGreedy}\n
        coin_change_greedy.go
        [class]{}-[func]{coinChangeGreedy}\n
        coin_change_greedy.swift
        [class]{}-[func]{coinChangeGreedy}\n
        coin_change_greedy.js
        [class]{}-[func]{coinChangeGreedy}\n
        coin_change_greedy.ts
        [class]{}-[func]{coinChangeGreedy}\n
        coin_change_greedy.dart
        [class]{}-[func]{coinChangeGreedy}\n
        coin_change_greedy.rs
        [class]{}-[func]{coin_change_greedy}\n
        coin_change_greedy.c
        [class]{}-[func]{coinChangeGreedy}\n
        coin_change_greedy.kt
        [class]{}-[func]{coinChangeGreedy}\n
        coin_change_greedy.rb
        [class]{}-[func]{coin_change_greedy}\n

        \u611f\u5606\u3059\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\uff1a\u306a\u3093\u3066\u7c21\u6f54\u306a\u3093\u3060\uff01\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u7d0410\u884c\u306e\u30b3\u30fc\u30c9\u3067\u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u3092\u89e3\u6c7a\u3057\u307e\u3059\u3002

        "},{"location":"chapter_greedy/greedy_algorithm/#1511","title":"15.1.1 \u00a0 \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5229\u70b9\u3068\u5236\u9650","text":"

        \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u76f4\u63a5\u7684\u3067\u5b9f\u88c5\u304c\u7c21\u5358\u3067\u3042\u308b\u3060\u3051\u3067\u306a\u304f\u3001\u901a\u5e38\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3082\u3042\u308a\u307e\u3059\u3002\u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u3067\u3001\u6700\u5c0f\u306e\u30b3\u30a4\u30f3\u984d\u9762\u3092 \\(\\min(coins)\\) \u3068\u3059\u308b\u3068\u3001\u8caa\u6b32\u306a\u9078\u629e\u30eb\u30fc\u30d7\u306f\u6700\u5927 \\(amt / \\min(coins)\\) \u56de\u5b9f\u884c\u3055\u308c\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(amt / \\min(coins))\\) \u306b\u306a\u308a\u307e\u3059\u3002\u3053\u308c\u306f\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u89e3\u6cd5\u306e\u6642\u9593\u8a08\u7b97\u91cf \\(O(n \\times amt)\\) \u3088\u308a\u3082\u4e00\u6841\u5c0f\u3055\u3044\u3067\u3059\u3002

        \u3057\u304b\u3057\u3001\u4e00\u90e8\u306e\u30b3\u30a4\u30f3\u984d\u9762\u306e\u7d44\u307f\u5408\u308f\u305b\u3067\u306f\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u6700\u9069\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u305b\u3093\u3002\u4e0b\u306e\u56f3\u306f2\u3064\u306e\u4f8b\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        • \u6b63\u306e\u4f8b \\(coins = [1, 5, 10, 20, 50, 100]\\)\uff1a\u3053\u306e\u30b3\u30a4\u30f3\u306e\u7d44\u307f\u5408\u308f\u305b\u3067\u306f\u3001\u4efb\u610f\u306e \\(amt\\) \u306b\u5bfe\u3057\u3066\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u6700\u9069\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
        • \u8ca0\u306e\u4f8b \\(coins = [1, 20, 50]\\)\uff1a\\(amt = 60\\) \u3068\u3059\u308b\u3068\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u7d44\u307f\u5408\u308f\u305b \\(50 + 1 \\times 10\\) \u3057\u304b\u898b\u3064\u3051\u3089\u308c\u305a\u3001\u5408\u8a0811\u679a\u306e\u30b3\u30a4\u30f3\u3067\u3059\u304c\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u6700\u9069\u89e3 \\(20 + 20 + 20\\) \u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u30013\u679a\u306e\u30b3\u30a4\u30f3\u306e\u307f\u304c\u5fc5\u8981\u3067\u3059\u3002
        • \u8ca0\u306e\u4f8b \\(coins = [1, 49, 50]\\)\uff1a\\(amt = 98\\) \u3068\u3059\u308b\u3068\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u7d44\u307f\u5408\u308f\u305b \\(50 + 1 \\times 48\\) \u3057\u304b\u898b\u3064\u3051\u3089\u308c\u305a\u3001\u5408\u8a0849\u679a\u306e\u30b3\u30a4\u30f3\u3067\u3059\u304c\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u6700\u9069\u89e3 \\(49 + 49\\) \u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u30012\u679a\u306e\u30b3\u30a4\u30f3\u306e\u307f\u304c\u5fc5\u8981\u3067\u3059\u3002

        \u56f3 15-2 \u00a0 \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u6700\u9069\u89e3\u3092\u898b\u3064\u3051\u3089\u308c\u306a\u3044\u4f8b

        \u3053\u308c\u306f\u3001\u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u306b\u304a\u3044\u3066\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u30b0\u30ed\u30fc\u30d0\u30eb\u306b\u6700\u9069\u306a\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3067\u304d\u305a\u3001\u975e\u5e38\u306b\u60aa\u3044\u89e3\u3092\u898b\u3064\u3051\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306e\u65b9\u304c\u9069\u3057\u3066\u3044\u307e\u3059\u3002

        \u4e00\u822c\u7684\u306b\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u9069\u7528\u6027\u306f2\u3064\u306e\u30ab\u30c6\u30b4\u30ea\u306b\u5206\u985e\u3055\u308c\u307e\u3059\u3002

        1. \u6700\u9069\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u4fdd\u8a3c\u3055\u308c\u308b\uff1a\u3053\u308c\u3089\u306e\u5834\u5408\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3057\u3070\u3057\u3070\u6700\u826f\u306e\u9078\u629e\u3067\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3084\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3088\u308a\u3082\u52b9\u7387\u7684\u3067\u3042\u308b\u50be\u5411\u304c\u3042\u308a\u307e\u3059\u3002
        2. \u6e96\u6700\u9069\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u308b\uff1a\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3053\u3053\u3067\u3082\u9069\u7528\u53ef\u80fd\u3067\u3059\u3002\u591a\u304f\u306e\u8907\u96d1\u306a\u554f\u984c\u3067\u306f\u3001\u30b0\u30ed\u30fc\u30d0\u30eb\u6700\u9069\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u306f\u975e\u5e38\u306b\u56f0\u96e3\u3067\u3042\u308a\u3001\u9ad8\u52b9\u7387\u306e\u6e96\u6700\u9069\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3082\u975e\u5e38\u306b\u4fa1\u5024\u304c\u3042\u308a\u307e\u3059\u3002
        "},{"location":"chapter_greedy/greedy_algorithm/#1512","title":"15.1.2 \u00a0 \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7279\u5fb4","text":"

        \u305d\u308c\u3067\u306f\u3001\u3069\u306e\u3088\u3046\u306a\u554f\u984c\u304c\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u89e3\u6c7a\u3059\u308b\u306e\u306b\u9069\u3057\u3066\u3044\u308b\u306e\u3067\u3057\u3087\u3046\u304b\uff1f\u8a00\u3044\u63db\u3048\u308c\u3070\u3001\u3069\u306e\u3088\u3046\u306a\u6761\u4ef6\u4e0b\u3067\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u6700\u9069\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3067\u304d\u308b\u306e\u3067\u3057\u3087\u3046\u304b\uff1f

        \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3068\u6bd4\u8f03\u3057\u3066\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3088\u308a\u53b3\u3057\u3044\u4f7f\u7528\u6761\u4ef6\u3092\u6301\u3061\u3001\u4e3b\u306b\u554f\u984c\u306e2\u3064\u306e\u6027\u8cea\u306b\u7126\u70b9\u3092\u5f53\u3066\u3066\u3044\u307e\u3059\u3002

        • \u8caa\u6b32\u9078\u629e\u6027\uff1a\u5c40\u6240\u7684\u306b\u6700\u9069\u306a\u9078\u629e\u304c\u5e38\u306b\u30b0\u30ed\u30fc\u30d0\u30eb\u306b\u6700\u9069\u306a\u89e3\u306b\u5c0e\u304f\u3053\u3068\u304c\u3067\u304d\u308b\u5834\u5408\u306e\u307f\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u6700\u9069\u89e3\u3092\u5f97\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3067\u304d\u307e\u3059\u3002
        • \u6700\u9069\u90e8\u5206\u69cb\u9020\uff1a\u5143\u306e\u554f\u984c\u306e\u6700\u9069\u89e3\u306f\u305d\u306e\u90e8\u5206\u554f\u984c\u306e\u6700\u9069\u89e3\u3092\u542b\u307f\u307e\u3059\u3002

        \u6700\u9069\u90e8\u5206\u69cb\u9020\u306f\u300c\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u300d\u306e\u7ae0\u3067\u3059\u3067\u306b\u7d39\u4ecb\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u3053\u3053\u3067\u306f\u3053\u308c\u4ee5\u4e0a\u8b70\u8ad6\u3057\u307e\u305b\u3093\u3002\u4e00\u90e8\u306e\u554f\u984c\u306b\u306f\u660e\u3089\u304b\u306a\u6700\u9069\u90e8\u5206\u69cb\u9020\u304c\u3042\u308a\u307e\u305b\u3093\u304c\u3001\u305d\u308c\u3067\u3082\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u4f7f\u7528\u3057\u3066\u89e3\u6c7a\u3067\u304d\u308b\u3053\u3068\u306b\u6ce8\u610f\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002

        \u4e3b\u306b\u8caa\u6b32\u9078\u629e\u6027\u3092\u6c7a\u5b9a\u3059\u308b\u65b9\u6cd5\u3092\u63a2\u7d22\u3057\u307e\u3059\u3002\u305d\u306e\u8a18\u8ff0\u306f\u5358\u7d14\u306b\u898b\u3048\u307e\u3059\u304c\u3001\u5b9f\u969b\u306b\u306f\u3001\u591a\u304f\u306e\u554f\u984c\u306e\u8caa\u6b32\u9078\u629e\u6027\u3092\u8a3c\u660e\u3059\u308b\u3053\u3068\u306f\u5bb9\u6613\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002

        \u4f8b\u3048\u3070\u3001\u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u3067\u306f\u3001\u8caa\u6b32\u9078\u629e\u6027\u3092\u53cd\u8a3c\u3059\u308b\u305f\u3081\u306b\u53cd\u4f8b\u3092\u7c21\u5358\u306b\u6319\u3052\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u304c\u3001\u305d\u308c\u3092\u8a3c\u660e\u3059\u308b\u3053\u3068\u306f\u306f\u308b\u304b\u306b\u56f0\u96e3\u3067\u3059\u3002**\u30b3\u30a4\u30f3\u306e\u7d44\u307f\u5408\u308f\u305b\u304c\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u4f7f\u7528\u3057\u3066\u89e3\u6c7a\u3067\u304d\u308b\u305f\u3081\u306b\u306f\u3001\u3069\u306e\u3088\u3046\u306a\u6761\u4ef6\u3092\u6e80\u305f\u3059\u5fc5\u8981\u304c\u3042\u308b\u304b**\u3068\u5c0b\u306d\u3089\u308c\u305f\u5834\u5408\u3001\u53b3\u5bc6\u306a\u6570\u5b66\u7684\u8a3c\u660e\u3092\u63d0\u4f9b\u3059\u308b\u3053\u3068\u304c\u56f0\u96e3\u3067\u3042\u308b\u305f\u3081\u3001\u3057\u3070\u3057\u3070\u76f4\u611f\u3084\u4f8b\u306b\u983c\u3063\u3066\u66d6\u6627\u306a\u7b54\u3048\u3092\u63d0\u4f9b\u3057\u306a\u3051\u308c\u3070\u306a\u308a\u307e\u305b\u3093\u3002

        Quote

        \u3042\u308b\u8ad6\u6587\u3067\u306f\u3001\u30b3\u30a4\u30f3\u306e\u7d44\u307f\u5408\u308f\u305b\u304c\u4efb\u610f\u306e\u91d1\u984d\u306b\u5bfe\u3057\u3066\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u4f7f\u7528\u3057\u3066\u6700\u9069\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\u3059\u308b\u305f\u3081\u306e\u6642\u9593\u8a08\u7b97\u91cf \\(O(n^3)\\) \u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u63d0\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        Pearson, D. A polynomial-time algorithm for the change-making problem[J]. Operations Research Letters, 2005, 33(3): 231-234.

        "},{"location":"chapter_greedy/greedy_algorithm/#1513","title":"15.1.3 \u00a0 \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u3088\u308b\u554f\u984c\u89e3\u6c7a\u306e\u30b9\u30c6\u30c3\u30d7","text":"

        \u8caa\u6b32\u554f\u984c\u306e\u554f\u984c\u89e3\u6c7a\u30d7\u30ed\u30bb\u30b9\u306f\u3001\u4e00\u822c\u7684\u306b\u4ee5\u4e0b\u306e3\u3064\u306e\u30b9\u30c6\u30c3\u30d7\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        1. \u554f\u984c\u5206\u6790\uff1a\u554f\u984c\u306e\u7279\u5fb4\u3092\u6574\u7406\u3057\u7406\u89e3\u3059\u308b\u3002\u72b6\u614b\u5b9a\u7fa9\u3001\u6700\u9069\u5316\u76ee\u6a19\u3001\u5236\u7d04\u306a\u3069\u3092\u542b\u307f\u307e\u3059\u3002\u3053\u306e\u30b9\u30c6\u30c3\u30d7\u306f\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3084\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3067\u3082\u95a2\u4e0e\u3057\u307e\u3059\u3002
        2. \u8caa\u6b32\u6226\u7565\u306e\u6c7a\u5b9a\uff1a\u5404\u30b9\u30c6\u30c3\u30d7\u3067\u8caa\u6b32\u306a\u9078\u629e\u3092\u3059\u308b\u65b9\u6cd5\u3092\u6c7a\u5b9a\u3059\u308b\u3002\u3053\u306e\u6226\u7565\u306f\u5404\u30b9\u30c6\u30c3\u30d7\u3067\u554f\u984c\u306e\u898f\u6a21\u3092\u7e2e\u5c0f\u3057\u3001\u6700\u7d42\u7684\u306b\u554f\u984c\u5168\u4f53\u3092\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002
        3. \u6b63\u78ba\u6027\u306e\u8a3c\u660e\uff1a\u901a\u5e38\u3001\u554f\u984c\u304c\u8caa\u6b32\u9078\u629e\u6027\u3068\u6700\u9069\u90e8\u5206\u69cb\u9020\u306e\u4e21\u65b9\u3092\u6301\u3064\u3053\u3068\u3092\u8a3c\u660e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u30b9\u30c6\u30c3\u30d7\u306b\u306f\u3001\u5e30\u7d0d\u6cd5\u3084\u80cc\u7406\u6cd5\u306a\u3069\u306e\u6570\u5b66\u7684\u8a3c\u660e\u304c\u5fc5\u8981\u306a\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002

        \u8caa\u6b32\u6226\u7565\u306e\u6c7a\u5b9a\u306f\u554f\u984c\u89e3\u6c7a\u306e\u6838\u5fc3\u30b9\u30c6\u30c3\u30d7\u3067\u3059\u304c\u3001\u5b9f\u88c5\u306f\u5bb9\u6613\u3067\u306f\u306a\u3044\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u4e3b\u306a\u7406\u7531\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002

        • \u7570\u306a\u308b\u554f\u984c\u9593\u3067\u8caa\u6b32\u6226\u7565\u306f\u5927\u304d\u304f\u7570\u306a\u308b\u3002\u591a\u304f\u306e\u554f\u984c\u3067\u306f\u3001\u8caa\u6b32\u6226\u7565\u306f\u304b\u306a\u308a\u76f4\u63a5\u7684\u3067\u3001\u4e00\u822c\u7684\u306a\u601d\u8003\u3068\u8a66\u884c\u3092\u901a\u3058\u3066\u601d\u3044\u3064\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u4e00\u90e8\u306e\u8907\u96d1\u306a\u554f\u984c\u3067\u306f\u3001\u8caa\u6b32\u6226\u7565\u306f\u975e\u5e38\u306b\u898b\u3064\u3051\u306b\u304f\u304f\u3001\u3053\u308c\u306f\u500b\u4eba\u306e\u554f\u984c\u89e3\u6c7a\u7d4c\u9a13\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u80fd\u529b\u306e\u771f\u306e\u30c6\u30b9\u30c8\u3067\u3059\u3002
        • \u4e00\u90e8\u306e\u8caa\u6b32\u6226\u7565\u306f\u975e\u5e38\u306b\u8aa4\u89e3\u3092\u62db\u304f\u3002\u81ea\u4fe1\u3092\u6301\u3063\u3066\u8caa\u6b32\u6226\u7565\u3092\u8a2d\u8a08\u3057\u3001\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u30c6\u30b9\u30c8\u306b\u63d0\u51fa\u3057\u305f\u3068\u304d\u3001\u4e00\u90e8\u306e\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u304c\u901a\u3089\u306a\u3044\u53ef\u80fd\u6027\u304c\u9ad8\u3044\u3067\u3059\u3002\u3053\u308c\u306f\u8a2d\u8a08\u3055\u308c\u305f\u8caa\u6b32\u6226\u7565\u304c\u300c\u90e8\u5206\u7684\u306b\u6b63\u3057\u3044\u300d\u3060\u3051\u3067\u3042\u308b\u305f\u3081\u3067\u3001\u4e0a\u8a18\u306e\u30b3\u30a4\u30f3\u4ea4\u63db\u306e\u4f8b\u3067\u8aac\u660e\u3057\u305f\u901a\u308a\u3067\u3059\u3002

        \u6b63\u78ba\u6027\u3092\u78ba\u4fdd\u3059\u308b\u305f\u3081\u306b\u3001\u8caa\u6b32\u6226\u7565\u306b\u5bfe\u3057\u3066\u53b3\u5bc6\u306a\u6570\u5b66\u7684\u8a3c\u660e\u3092\u63d0\u4f9b\u3059\u3079\u304d\u3067\u3001\u901a\u5e38\u306f\u80cc\u7406\u6cd5\u3084\u6570\u5b66\u7684\u5e30\u7d0d\u6cd5\u3092\u542b\u307f\u307e\u3059\u3002

        \u3057\u304b\u3057\u3001\u6b63\u78ba\u6027\u3092\u8a3c\u660e\u3059\u308b\u3053\u3068\u306f\u5bb9\u6613\u306a\u4f5c\u696d\u3067\u306f\u306a\u3044\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u9014\u65b9\u306b\u66ae\u308c\u305f\u5834\u5408\u3001\u901a\u5e38\u306f\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u306b\u57fa\u3065\u3044\u3066\u30b3\u30fc\u30c9\u3092\u30c7\u30d0\u30c3\u30b0\u3057\u3001\u8caa\u6b32\u6226\u7565\u3092\u6bb5\u968e\u7684\u306b\u4fee\u6b63\u3057\u691c\u8a3c\u3059\u308b\u3053\u3068\u3092\u9078\u629e\u3057\u307e\u3059\u3002

        "},{"location":"chapter_greedy/greedy_algorithm/#1514","title":"15.1.4 \u00a0 \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u89e3\u6c7a\u3055\u308c\u308b\u5178\u578b\u7684\u306a\u554f\u984c","text":"

        \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u8caa\u6b32\u9078\u629e\u3068\u6700\u9069\u90e8\u5206\u69cb\u9020\u306e\u6027\u8cea\u3092\u6e80\u305f\u3059\u6700\u9069\u5316\u554f\u984c\u306b\u3088\u304f\u9069\u7528\u3055\u308c\u307e\u3059\u3002\u4ee5\u4e0b\u306f\u5178\u578b\u7684\u306a\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u306e\u3044\u304f\u3064\u304b\u3067\u3059\u3002

        • \u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\uff1a\u4e00\u90e8\u306e\u30b3\u30a4\u30f3\u306e\u7d44\u307f\u5408\u308f\u305b\u3067\u306f\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u5e38\u306b\u6700\u9069\u89e3\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002
        • \u533a\u9593\u30b9\u30b1\u30b8\u30e5\u30fc\u30ea\u30f3\u30b0\u554f\u984c\uff1a\u3044\u304f\u3064\u304b\u306e\u30bf\u30b9\u30af\u304c\u3042\u308a\u3001\u305d\u308c\u305e\u308c\u304c\u4e00\u5b9a\u671f\u9593\u306b\u308f\u305f\u3063\u3066\u884c\u308f\u308c\u308b\u3068\u3057\u307e\u3059\u3002\u76ee\u6a19\u306f\u3067\u304d\u308b\u3060\u3051\u591a\u304f\u306e\u30bf\u30b9\u30af\u3092\u5b8c\u4e86\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u5e38\u306b\u6700\u3082\u65e9\u304f\u7d42\u4e86\u3059\u308b\u30bf\u30b9\u30af\u3092\u9078\u629e\u3059\u308b\u3068\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u6700\u9069\u89e3\u3092\u9054\u6210\u3067\u304d\u307e\u3059\u3002
        • \u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\uff1a\u30a2\u30a4\u30c6\u30e0\u306e\u30bb\u30c3\u30c8\u3068\u904b\u642c\u5bb9\u91cf\u304c\u4e0e\u3048\u3089\u308c\u3001\u76ee\u6a19\u306f\u7dcf\u91cd\u91cf\u304c\u904b\u642c\u5bb9\u91cf\u3092\u8d85\u3048\u305a\u3001\u7dcf\u4fa1\u5024\u304c\u6700\u5927\u5316\u3055\u308c\u308b\u3088\u3046\u306a\u30a2\u30a4\u30c6\u30e0\u306e\u30bb\u30c3\u30c8\u3092\u9078\u629e\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u5e38\u306b\u6700\u9ad8\u306e\u4fa1\u5024\u5bfe\u91cd\u91cf\u6bd4\uff08\u4fa1\u5024/\u91cd\u91cf\uff09\u306e\u30a2\u30a4\u30c6\u30e0\u3092\u9078\u629e\u3059\u308b\u3068\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u4e00\u90e8\u306e\u30b1\u30fc\u30b9\u3067\u6700\u9069\u89e3\u3092\u9054\u6210\u3067\u304d\u307e\u3059\u3002
        • \u682a\u5f0f\u53d6\u5f15\u554f\u984c\uff1a\u682a\u4fa1\u306e\u5c65\u6b74\u306e\u30bb\u30c3\u30c8\u304c\u4e0e\u3048\u3089\u308c\u3001\u8907\u6570\u56de\u306e\u53d6\u5f15\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u304c\u3001\u3059\u3067\u306b\u682a\u5f0f\u3092\u6240\u6709\u3057\u3066\u3044\u308b\u5834\u5408\u306f\u58f2\u5374\u5f8c\u3067\u306a\u3044\u3068\u518d\u5ea6\u8cfc\u5165\u3067\u304d\u307e\u305b\u3093\u3002\u76ee\u6a19\u306f\u6700\u5927\u5229\u76ca\u3092\u9054\u6210\u3059\u308b\u3053\u3068\u3067\u3059\u3002
        • \u30cf\u30d5\u30de\u30f3\u7b26\u53f7\u5316\uff1a\u30cf\u30d5\u30de\u30f3\u7b26\u53f7\u5316\u306f\u7121\u640d\u5931\u30c7\u30fc\u30bf\u5727\u7e2e\u306b\u4f7f\u7528\u3055\u308c\u308b\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3059\u3002\u30cf\u30d5\u30de\u30f3\u6728\u3092\u69cb\u7bc9\u3059\u308b\u3053\u3068\u306b\u3088\u308a\u3001\u5e38\u306b\u6700\u4f4e\u983b\u5ea6\u306e2\u3064\u306e\u30ce\u30fc\u30c9\u3092\u7d71\u5408\u3057\u3001\u6700\u5c0f\u91cd\u307f\u4ed8\u304d\u30d1\u30b9\u9577\uff08\u7b26\u53f7\u5316\u9577\uff09\u306e\u30cf\u30d5\u30de\u30f3\u6728\u3092\u751f\u6210\u3057\u307e\u3059\u3002
        • \u30c0\u30a4\u30af\u30b9\u30c8\u30e9\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u3053\u308c\u306f\u4e0e\u3048\u3089\u308c\u305f\u30bd\u30fc\u30b9\u9802\u70b9\u304b\u3089\u4ed6\u306e\u3059\u3079\u3066\u306e\u9802\u70b9\u3078\u306e\u6700\u77ed\u7d4c\u8def\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306e\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3059\u3002
        "},{"location":"chapter_greedy/max_capacity_problem/","title":"15.3 \u00a0 \u6700\u5927\u5bb9\u91cf\u554f\u984c","text":"

        Question

        \u914d\u5217 \\(ht\\) \u3092\u5165\u529b\u3057\u307e\u3059\u3002\u5404\u8981\u7d20\u306f\u5782\u76f4\u4ed5\u5207\u308a\u306e\u9ad8\u3055\u3092\u8868\u3057\u307e\u3059\u3002\u914d\u5217\u5185\u306e\u4efb\u610f\u306e2\u3064\u306e\u4ed5\u5207\u308a\u3068\u3001\u305d\u308c\u3089\u306e\u9593\u306e\u30b9\u30da\u30fc\u30b9\u306b\u3088\u3063\u3066\u30b3\u30f3\u30c6\u30ca\u3092\u5f62\u6210\u3067\u304d\u307e\u3059\u3002

        \u30b3\u30f3\u30c6\u30ca\u306e\u5bb9\u91cf\u306f\u9ad8\u3055\u3068\u5e45\u306e\u7a4d\uff08\u9762\u7a4d\uff09\u3067\u3001\u9ad8\u3055\u306f\u77ed\u3044\u65b9\u306e\u4ed5\u5207\u308a\u306b\u3088\u3063\u3066\u6c7a\u5b9a\u3055\u308c\u3001\u5e45\u306f2\u3064\u306e\u4ed5\u5207\u308a\u306e\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u5dee\u3067\u3059\u3002

        \u30b3\u30f3\u30c6\u30ca\u306e\u5bb9\u91cf\u3092\u6700\u5927\u5316\u3059\u308b2\u3064\u306e\u4ed5\u5207\u308a\u3092\u914d\u5217\u304b\u3089\u9078\u629e\u3057\u3001\u3053\u306e\u6700\u5927\u5bb9\u91cf\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4f8b\u3092\u4e0b\u306e\u56f3\u306b\u793a\u3057\u307e\u3059\u3002

        \u56f3 15-7 \u00a0 \u6700\u5927\u5bb9\u91cf\u554f\u984c\u306e\u4f8b\u30c7\u30fc\u30bf

        \u30b3\u30f3\u30c6\u30ca\u306f\u4efb\u610f\u306e2\u3064\u306e\u4ed5\u5207\u308a\u306b\u3088\u3063\u3066\u5f62\u6210\u3055\u308c\u308b\u305f\u3081\u3001\u3053\u306e\u554f\u984c\u306e\u72b6\u614b\u306f2\u3064\u306e\u4ed5\u5207\u308a\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u8868\u73fe\u3055\u308c\u3001\\([i, j]\\) \u3068\u8868\u8a18\u3055\u308c\u307e\u3059\u3002

        \u554f\u984c\u306e\u8a18\u8ff0\u306b\u3088\u308c\u3070\u3001\u5bb9\u91cf\u306f\u9ad8\u3055\u3068\u5e45\u306e\u7a4d\u306b\u7b49\u3057\u304f\u3001\u9ad8\u3055\u306f\u77ed\u3044\u65b9\u306e\u4ed5\u5207\u308a\u306b\u3088\u3063\u3066\u6c7a\u5b9a\u3055\u308c\u3001\u5e45\u306f2\u3064\u306e\u4ed5\u5207\u308a\u306e\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u5dee\u3067\u3059\u3002\u5bb9\u91cf \\(cap[i, j]\\) \u306e\u5f0f\u306f\uff1a

        \\[ cap[i, j] = \\min(ht[i], ht[j]) \\times (j - i) \\]

        \u914d\u5217\u306e\u9577\u3055\u3092 \\(n\\) \u3068\u4eee\u5b9a\u3059\u308b\u3068\u30012\u3064\u306e\u4ed5\u5207\u308a\u306e\u7d44\u307f\u5408\u308f\u305b\u6570\uff08\u72b6\u614b\u306e\u7dcf\u6570\uff09\u306f \\(C_n^2 = \\frac{n(n - 1)}{2}\\) \u3067\u3059\u3002\u6700\u3082\u76f4\u63a5\u7684\u306a\u30a2\u30d7\u30ed\u30fc\u30c1\u306f**\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u72b6\u614b\u3092\u5217\u6319\u3059\u308b**\u3053\u3068\u3067\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n^2)\\) \u306b\u306a\u308a\u307e\u3059\u3002

        "},{"location":"chapter_greedy/max_capacity_problem/#1","title":"1. \u00a0 \u8caa\u6b32\u6226\u7565\u306e\u6c7a\u5b9a","text":"

        \u3053\u306e\u554f\u984c\u306b\u306f\u3088\u308a\u52b9\u7387\u7684\u306a\u89e3\u6cd5\u304c\u3042\u308a\u307e\u3059\u3002\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 \\(i < j\\) \u304b\u3064\u9ad8\u3055 \\(ht[i] < ht[j]\\) \u306e\u72b6\u614b \\([i, j]\\) \u3092\u9078\u629e\u3057\u307e\u3059\u3002\u3064\u307e\u308a\u3001\\(i\\) \u306f\u77ed\u3044\u4ed5\u5207\u308a\u3001\\(j\\) \u306f\u9ad8\u3044\u4ed5\u5207\u308a\u3067\u3059\u3002

        \u56f3 15-8 \u00a0 \u521d\u671f\u72b6\u614b

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u9ad8\u3044\u4ed5\u5207\u308a \\(j\\) \u3092\u77ed\u3044\u4ed5\u5207\u308a \\(i\\) \u306b\u8fd1\u3065\u3051\u3066\u79fb\u52d5\u3059\u308b\u3068\u3001\u5bb9\u91cf\u306f\u78ba\u5b9f\u306b\u6e1b\u5c11\u3057\u307e\u3059\u3002

        \u3053\u308c\u306f\u3001\u9ad8\u3044\u4ed5\u5207\u308a \\(j\\) \u3092\u79fb\u52d5\u3059\u308b\u3068\u3001\u5e45 \\(j-i\\) \u304c\u78ba\u5b9f\u306b\u6e1b\u5c11\u3059\u308b\u305f\u3081\u3067\u3059\u3002\u9ad8\u3055\u306f\u77ed\u3044\u4ed5\u5207\u308a\u306b\u3088\u3063\u3066\u6c7a\u5b9a\u3055\u308c\u308b\u305f\u3081\u3001\u9ad8\u3055\u306f\u540c\u3058\u307e\u307e\uff08\\(i\\) \u304c\u77ed\u3044\u4ed5\u5207\u308a\u306e\u307e\u307e\uff09\u304b\u6e1b\u5c11\uff08\u79fb\u52d5\u3057\u305f \\(j\\) \u304c\u77ed\u3044\u4ed5\u5207\u308a\u306b\u306a\u308b\uff09\u3057\u304b\u3042\u308a\u307e\u305b\u3093\u3002

        \u56f3 15-9 \u00a0 \u9ad8\u3044\u4ed5\u5207\u308a\u3092\u5185\u5074\u306b\u79fb\u52d5\u3057\u305f\u5f8c\u306e\u72b6\u614b

        \u9006\u306b\u3001\u77ed\u3044\u4ed5\u5207\u308a \\(i\\) \u3092\u5185\u5074\u306b\u79fb\u52d5\u3059\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u306e\u307f\u5bb9\u91cf\u3092\u5897\u52a0\u3055\u305b\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002\u5e45\u306f\u78ba\u5b9f\u306b\u6e1b\u5c11\u3057\u307e\u3059\u304c\u3001\u9ad8\u3055\u304c\u5897\u52a0\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\uff08\u79fb\u52d5\u3057\u305f\u77ed\u3044\u4ed5\u5207\u308a \\(i\\) \u304c\u9ad8\u304f\u306a\u308b\u5834\u5408\uff09\u3002\u4f8b\u3048\u3070\u3001\u4e0b\u306e\u56f3\u3067\u306f\u3001\u77ed\u3044\u4ed5\u5207\u308a\u3092\u79fb\u52d5\u3057\u305f\u5f8c\u306b\u9762\u7a4d\u304c\u5897\u52a0\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 15-10 \u00a0 \u77ed\u3044\u4ed5\u5207\u308a\u3092\u5185\u5074\u306b\u79fb\u52d5\u3057\u305f\u5f8c\u306e\u72b6\u614b

        \u3053\u308c\u306b\u3088\u308a\u3001\u3053\u306e\u554f\u984c\u306e\u8caa\u6b32\u6226\u7565\u304c\u5c0e\u304b\u308c\u307e\u3059\uff1a\u30b3\u30f3\u30c6\u30ca\u306e\u4e21\u7aef\u306b2\u3064\u306e\u30dd\u30a4\u30f3\u30bf\u3092\u521d\u671f\u5316\u3057\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u77ed\u3044\u4ed5\u5207\u308a\u306b\u5bfe\u5fdc\u3059\u308b\u30dd\u30a4\u30f3\u30bf\u3092\u5185\u5074\u306b\u79fb\u52d5\u3057\u30012\u3064\u306e\u30dd\u30a4\u30f3\u30bf\u304c\u51fa\u4f1a\u3046\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002

        \u4e0b\u306e\u56f3\u306f\u8caa\u6b32\u6226\u7565\u306e\u5b9f\u884c\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        1. \u6700\u521d\u306b\u3001\u30dd\u30a4\u30f3\u30bf \\(i\\) \u3068 \\(j\\) \u304c\u914d\u5217\u306e\u4e21\u7aef\u306b\u914d\u7f6e\u3055\u308c\u307e\u3059\u3002
        2. \u73fe\u5728\u306e\u72b6\u614b\u306e\u5bb9\u91cf \\(cap[i, j]\\) \u3092\u8a08\u7b97\u3057\u3001\u6700\u5927\u5bb9\u91cf\u3092\u66f4\u65b0\u3057\u307e\u3059\u3002
        3. \u4ed5\u5207\u308a \\(i\\) \u3068 \\(j\\) \u306e\u9ad8\u3055\u3092\u6bd4\u8f03\u3057\u3001\u77ed\u3044\u4ed5\u5207\u308a\u30921\u30b9\u30c6\u30c3\u30d7\u5185\u5074\u306b\u79fb\u52d5\u3057\u307e\u3059\u3002
        4. \\(i\\) \u3068 \\(j\\) \u304c\u51fa\u4f1a\u3046\u307e\u3067\u30b9\u30c6\u30c3\u30d7 2. \u3068 3. \u3092\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002
        <1><2><3><4><5><6><7><8><9>

        \u56f3 15-11 \u00a0 \u6700\u5927\u5bb9\u91cf\u554f\u984c\u306e\u8caa\u6b32\u30d7\u30ed\u30bb\u30b9

        "},{"location":"chapter_greedy/max_capacity_problem/#2","title":"2. \u00a0 \u5b9f\u88c5","text":"

        \u30b3\u30fc\u30c9\u306f\u6700\u5927 \\(n\\) \u56de\u30eb\u30fc\u30d7\u3059\u308b\u305f\u3081\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u3067\u3059\u3002

        \u5909\u6570 \\(i\\)\u3001\\(j\\)\u3001\\(res\\) \u306f\u4e00\u5b9a\u91cf\u306e\u8ffd\u52a0\u30b9\u30da\u30fc\u30b9\u3092\u4f7f\u7528\u3059\u308b\u305f\u3081\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(1)\\) \u3067\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby max_capacity.py
        def max_capacity(ht: list[int]) -> int:\n    \"\"\"\u6700\u5927\u5bb9\u91cf\uff1a\u8caa\u6b32\u6cd5\"\"\"\n    # i\u3001j \u3092\u521d\u671f\u5316\u3001\u914d\u5217\u306e\u4e21\u7aef\u3067\u5206\u5272\u3055\u305b\u308b\n    i, j = 0, len(ht) - 1\n    # \u521d\u671f\u6700\u5927\u5bb9\u91cf\u306f 0\n    res = 0\n    # 2 \u3064\u306e\u677f\u304c\u51fa\u4f1a\u3046\u307e\u3067\u8caa\u6b32\u9078\u629e\u3092\u30eb\u30fc\u30d7\n    while i < j:\n        # \u6700\u5927\u5bb9\u91cf\u3092\u66f4\u65b0\n        cap = min(ht[i], ht[j]) * (j - i)\n        res = max(res, cap)\n        # \u77ed\u3044\u677f\u3092\u5185\u5074\u306b\u79fb\u52d5\n        if ht[i] < ht[j]:\n            i += 1\n        else:\n            j -= 1\n    return res\n
        max_capacity.cpp
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8caa\u6b32\u6cd5 */\nint maxCapacity(vector<int> &ht) {\n    // i\u3001j \u3092\u521d\u671f\u5316\u3057\u3001\u914d\u5217\u306e\u4e21\u7aef\u3067\u5206\u5272\u3055\u305b\u308b\n    int i = 0, j = ht.size() - 1;\n    // \u521d\u671f\u6700\u5927\u5bb9\u91cf\u306f 0\n    int res = 0;\n    // 2\u3064\u306e\u677f\u304c\u51fa\u4f1a\u3046\u307e\u3067\u8caa\u6b32\u9078\u629e\u3092\u30eb\u30fc\u30d7\n    while (i < j) {\n        // \u6700\u5927\u5bb9\u91cf\u3092\u66f4\u65b0\n        int cap = min(ht[i], ht[j]) * (j - i);\n        res = max(res, cap);\n        // \u3088\u308a\u77ed\u3044\u677f\u3092\u5185\u5074\u306b\u79fb\u52d5\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
        max_capacity.java
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8caa\u6b32\u6cd5 */\nint maxCapacity(int[] ht) {\n    // i\u3001j \u3092\u521d\u671f\u5316\u3057\u3001\u914d\u5217\u306e\u4e21\u7aef\u3067\u5206\u5272\u3055\u305b\u308b\n    int i = 0, j = ht.length - 1;\n    // \u521d\u671f\u6700\u5927\u5bb9\u91cf\u306f 0\n    int res = 0;\n    // 2\u3064\u306e\u677f\u304c\u51fa\u4f1a\u3046\u307e\u3067\u8caa\u6b32\u9078\u629e\u3092\u30eb\u30fc\u30d7\n    while (i < j) {\n        // \u6700\u5927\u5bb9\u91cf\u3092\u66f4\u65b0\n        int cap = Math.min(ht[i], ht[j]) * (j - i);\n        res = Math.max(res, cap);\n        // \u3088\u308a\u77ed\u3044\u677f\u3092\u5185\u5074\u306b\u79fb\u52d5\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
        max_capacity.cs
        [class]{max_capacity}-[func]{MaxCapacity}\n
        max_capacity.go
        [class]{}-[func]{maxCapacity}\n
        max_capacity.swift
        [class]{}-[func]{maxCapacity}\n
        max_capacity.js
        [class]{}-[func]{maxCapacity}\n
        max_capacity.ts
        [class]{}-[func]{maxCapacity}\n
        max_capacity.dart
        [class]{}-[func]{maxCapacity}\n
        max_capacity.rs
        [class]{}-[func]{max_capacity}\n
        max_capacity.c
        [class]{}-[func]{maxCapacity}\n
        max_capacity.kt
        [class]{}-[func]{maxCapacity}\n
        max_capacity.rb
        [class]{}-[func]{max_capacity}\n
        "},{"location":"chapter_greedy/max_capacity_problem/#3","title":"3. \u00a0 \u6b63\u3057\u3055\u306e\u8a3c\u660e","text":"

        \u8caa\u6b32\u6cd5\u304c\u5217\u6319\u3088\u308a\u3082\u9ad8\u901f\u3067\u3042\u308b\u7406\u7531\u306f\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u8caa\u6b32\u9078\u629e\u304c\u4e00\u90e8\u306e\u72b6\u614b\u3092\u300c\u30b9\u30ad\u30c3\u30d7\u300d\u3059\u308b\u304b\u3089\u3067\u3059\u3002

        \u4f8b\u3048\u3070\u3001\\(i\\) \u304c\u77ed\u3044\u4ed5\u5207\u308a\u3067 \\(j\\) \u304c\u9ad8\u3044\u4ed5\u5207\u308a\u3067\u3042\u308b\u72b6\u614b \\(cap[i, j]\\) \u306e\u4e0b\u3067\u3001\u77ed\u3044\u4ed5\u5207\u308a \\(i\\) \u3092\u8caa\u6b32\u306b1\u30b9\u30c6\u30c3\u30d7\u5185\u5074\u306b\u79fb\u52d5\u3059\u308b\u3068\u3001\u4e0b\u306e\u56f3\u306b\u793a\u3059\u300c\u30b9\u30ad\u30c3\u30d7\u3055\u308c\u305f\u300d\u72b6\u614b\u306b\u3064\u306a\u304c\u308a\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u3053\u308c\u3089\u306e\u72b6\u614b\u306e\u5bb9\u91cf\u3092\u5f8c\u3067\u691c\u8a3c\u3067\u304d\u306a\u3044\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

        \\[ cap[i, i+1], cap[i, i+2], \\dots, cap[i, j-2], cap[i, j-1] \\]

        \u56f3 15-12 \u00a0 \u77ed\u3044\u4ed5\u5207\u308a\u306e\u79fb\u52d5\u306b\u3088\u3063\u3066\u30b9\u30ad\u30c3\u30d7\u3055\u308c\u308b\u72b6\u614b

        \u89b3\u5bdf\u3059\u308b\u3068\u3001\u3053\u308c\u3089\u306e\u30b9\u30ad\u30c3\u30d7\u3055\u308c\u305f\u72b6\u614b\u306f\u5b9f\u969b\u306b\u306f\u9ad8\u3044\u4ed5\u5207\u308a \\(j\\) \u304c\u5185\u5074\u306b\u79fb\u52d5\u3057\u305f\u3059\u3079\u3066\u306e\u72b6\u614b**\u3067\u3059\u3002\u9ad8\u3044\u4ed5\u5207\u308a\u3092\u5185\u5074\u306b\u79fb\u52d5\u3059\u308b\u3068\u5bb9\u91cf\u304c\u78ba\u5b9f\u306b\u6e1b\u5c11\u3059\u308b\u3053\u3068\u3092\u3059\u3067\u306b\u8a3c\u660e\u3057\u307e\u3057\u305f\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30b9\u30ad\u30c3\u30d7\u3055\u308c\u305f\u72b6\u614b\u306f\u6700\u9069\u89e3\u3067\u3042\u308b\u53ef\u80fd\u6027\u304c\u306a\u304f\u3001**\u305d\u308c\u3089\u3092\u30b9\u30ad\u30c3\u30d7\u3057\u3066\u3082\u6700\u9069\u89e3\u3092\u898b\u9003\u3059\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u3002

        \u5206\u6790\u306b\u3088\u308a\u3001\u77ed\u3044\u4ed5\u5207\u308a\u3092\u79fb\u52d5\u3059\u308b\u64cd\u4f5c\u306f\u300c\u5b89\u5168\u300d\u3067\u3042\u308a\u3001\u8caa\u6b32\u6226\u7565\u304c\u52b9\u679c\u7684\u3067\u3042\u308b\u3053\u3068\u304c\u793a\u3055\u308c\u307e\u3059\u3002

        "},{"location":"chapter_greedy/max_product_cutting_problem/","title":"15.4 \u00a0 \u6700\u5927\u7a4d\u5207\u65ad\u554f\u984c","text":"

        Question

        \u6b63\u306e\u6574\u6570 \\(n\\) \u304c\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001\u305d\u308c\u3092\u5408\u8a08\u304c \\(n\\) \u306b\u306a\u308b\u5c11\u306a\u304f\u3068\u30822\u3064\u306e\u6b63\u306e\u6574\u6570\u306b\u5206\u5272\u3057\u3001\u3053\u308c\u3089\u306e\u6574\u6570\u306e\u6700\u5927\u7a4d\u3092\u6c42\u3081\u3066\u304f\u3060\u3055\u3044\u3002\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3068\u304a\u308a\u3067\u3059\u3002

        \u56f3 15-13 \u00a0 \u6700\u5927\u7a4d\u5207\u65ad\u554f\u984c\u306e\u5b9a\u7fa9

        \\(n\\) \u3092 \\(m\\) \u500b\u306e\u6574\u6570\u56e0\u5b50\u306b\u5206\u5272\u3059\u308b\u3068\u4eee\u5b9a\u3057\u3001\\(i\\) \u756a\u76ee\u306e\u56e0\u5b50\u3092 \\(n_i\\) \u3068\u8868\u8a18\u3059\u308b\u3068\u3001

        \\[ n = \\sum_{i=1}^{m}n_i \\]

        \u3053\u306e\u554f\u984c\u306e\u76ee\u6a19\u306f\u3001\u3059\u3079\u3066\u306e\u6574\u6570\u56e0\u5b50\u306e\u6700\u5927\u7a4d\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3067\u3059\u3002\u3059\u306a\u308f\u3061\u3001

        \\[ \\max(\\prod_{i=1}^{m}n_i) \\]

        \u8003\u616e\u3059\u3079\u304d\u70b9\uff1a\u5206\u5272\u6570 \\(m\\) \u306f\u3069\u306e\u7a0b\u5ea6\u5927\u304d\u304f\u3059\u3079\u304d\u304b\u3001\u5404 \\(n_i\\) \u306f\u4f55\u3067\u3042\u308b\u3079\u304d\u304b\uff1f

        "},{"location":"chapter_greedy/max_product_cutting_problem/#1","title":"1. \u00a0 \u8caa\u6b32\u6226\u7565\u306e\u6c7a\u5b9a","text":"

        \u7d4c\u9a13\u7684\u306b\u30012\u3064\u306e\u6574\u6570\u306e\u7a4d\u306f\u591a\u304f\u306e\u5834\u5408\u305d\u306e\u548c\u3088\u308a\u3082\u5927\u304d\u304f\u306a\u308a\u307e\u3059\u3002\\(n\\) \u304b\u3089\u56e0\u5b50 \\(2\\) \u3092\u5206\u5272\u3059\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u305d\u306e\u7a4d\u306f \\(2(n-2)\\) \u3067\u3059\u3002\u3053\u306e\u7a4d\u3092 \\(n\\) \u3068\u6bd4\u8f03\u3057\u307e\u3059\uff1a

        \\[ \\begin{aligned} 2(n-2) & \\geq n \\newline 2n - n - 4 & \\geq 0 \\newline n & \\geq 4 \\end{aligned} \\]

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\\(n \\geq 4\\) \u306e\u3068\u304d\u3001\\(2\\) \u3092\u5206\u5272\u3059\u308b\u3068\u7a4d\u304c\u5897\u52a0\u3057\u307e\u3059\u3002\u3053\u308c\u306f4\u4ee5\u4e0a\u306e\u6574\u6570\u3092\u5206\u5272\u3059\u3079\u304d\u3067\u3042\u308b\u3053\u3068\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u8caa\u6b32\u6226\u75651\uff1a\u5206\u5272\u30b9\u30ad\u30fc\u30e0\u304c \\(\\geq 4\\) \u306e\u56e0\u5b50\u3092\u542b\u3080\u5834\u5408\u3001\u305d\u308c\u3089\u306f\u3055\u3089\u306b\u5206\u5272\u3055\u308c\u308b\u3079\u304d\u3067\u3059\u3002\u6700\u7d42\u7684\u306a\u5206\u5272\u306f\u56e0\u5b50 \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u306e\u307f\u3092\u542b\u3080\u3079\u304d\u3067\u3059\u3002

        \u56f3 15-14 \u00a0 \u5206\u5272\u306b\u3088\u308b\u7a4d\u306e\u5897\u52a0

        \u6b21\u306b\u3001\u3069\u306e\u56e0\u5b50\u304c\u6700\u9069\u304b\u3092\u8003\u616e\u3057\u307e\u3059\u3002\u56e0\u5b50 \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u306e\u4e2d\u3067\u3001\u660e\u3089\u304b\u306b \\(1\\) \u304c\u6700\u60aa\u3067\u3059\u3002\\(1 \\times (n-1) < n\\) \u304c\u5e38\u306b\u6210\u308a\u7acb\u3064\u305f\u3081\u3001\\(1\\) \u3092\u5206\u5272\u3059\u308b\u3068\u5b9f\u969b\u306b\u7a4d\u304c\u6e1b\u5c11\u3057\u307e\u3059\u3002

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\\(n = 6\\) \u306e\u3068\u304d\u3001\\(3 \\times 3 > 2 \\times 2 \\times 2\\) \u3067\u3059\u3002\u3053\u308c\u306f \\(3\\) \u3092\u5206\u5272\u3059\u308b\u65b9\u304c \\(2\\) \u3092\u5206\u5272\u3059\u308b\u3088\u308a\u3082\u826f\u3044\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

        \u8caa\u6b32\u6226\u75652\uff1a\u5206\u5272\u30b9\u30ad\u30fc\u30e0\u306b\u306f\u6700\u5927\u30672\u3064\u306e \\(2\\) \u304c\u3042\u308b\u3079\u304d\u3067\u3059\u30023\u3064\u306e \\(2\\) \u306f\u5e38\u306b2\u3064\u306e \\(3\\) \u306b\u7f6e\u304d\u63db\u3048\u3066\u3088\u308a\u9ad8\u3044\u7a4d\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u304b\u3089\u3067\u3059\u3002

        \u56f3 15-15 \u00a0 \u6700\u9069\u306a\u5206\u5272\u56e0\u5b50

        \u4e0a\u8a18\u304b\u3089\u3001\u4ee5\u4e0b\u306e\u8caa\u6b32\u6226\u7565\u3092\u5c0e\u51fa\u3067\u304d\u307e\u3059\u3002

        1. \u5165\u529b\u6574\u6570 \\(n\\) \u306b\u3064\u3044\u3066\u3001\u4f59\u308a\u304c \\(0\\)\u3001\\(1\\)\u3001\u307e\u305f\u306f \\(2\\) \u306b\u306a\u308b\u307e\u3067\u56e0\u5b50 \\(3\\) \u3092\u7d99\u7d9a\u7684\u306b\u5206\u5272\u3057\u307e\u3059\u3002
        2. \u4f59\u308a\u304c \\(0\\) \u306e\u5834\u5408\u3001\\(n\\) \u304c \\(3\\) \u306e\u500d\u6570\u3067\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u305f\u3081\u3001\u305d\u308c\u4ee5\u4e0a\u306e\u884c\u52d5\u306f\u53d6\u308a\u307e\u305b\u3093\u3002
        3. \u4f59\u308a\u304c \\(2\\) \u306e\u5834\u5408\u3001\u3055\u3089\u306b\u5206\u5272\u3092\u7d9a\u3051\u305a\u3001\u305d\u306e\u307e\u307e\u4fdd\u6301\u3057\u307e\u3059\u3002
        4. \u4f59\u308a\u304c \\(1\\) \u306e\u5834\u5408\u3001\\(2 \\times 2 > 1 \\times 3\\) \u3067\u3042\u308b\u305f\u3081\u3001\u6700\u5f8c\u306e \\(3\\) \u3092 \\(2\\) \u306b\u7f6e\u304d\u63db\u3048\u308b\u3079\u304d\u3067\u3059\u3002
        "},{"location":"chapter_greedy/max_product_cutting_problem/#2","title":"2. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u6574\u6570\u3092\u5206\u5272\u3059\u308b\u305f\u3081\u306b\u30eb\u30fc\u30d7\u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u306f\u306a\u304f\u3001\u5e8a\u9664\u7b97\u6f14\u7b97\u3092\u4f7f\u7528\u3057\u3066 \\(3\\) \u306e\u6570 \\(a\\) \u3092\u53d6\u5f97\u3057\u3001\u5270\u4f59\u6f14\u7b97\u3092\u4f7f\u7528\u3057\u3066\u4f59\u308a \\(b\\) \u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\uff1a

        \\[ n = 3a + b \\]

        \\(n \\leq 3\\) \u306e\u5883\u754c\u30b1\u30fc\u30b9\u3067\u306f\u3001\\(1\\) \u3092\u5206\u5272\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u7a4d\u306f \\(1 \\times (n - 1)\\) \u3067\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby max_product_cutting.py
        def max_product_cutting(n: int) -> int:\n    \"\"\"\u5207\u65ad\u306e\u6700\u5927\u7a4d\uff1a\u8caa\u6b32\u6cd5\"\"\"\n    # n <= 3 \u306e\u5834\u5408\u30011 \u3092\u5207\u308a\u51fa\u3059\u5fc5\u8981\u304c\u3042\u308b\n    if n <= 3:\n        return 1 * (n - 1)\n    # \u8caa\u6b32\u7684\u306b 3 \u3092\u5207\u308a\u51fa\u3059\u3001a \u306f 3 \u306e\u500b\u6570\u3001b \u306f\u4f59\u308a\n    a, b = n // 3, n % 3\n    if b == 1:\n        # \u4f59\u308a\u304c 1 \u306e\u5834\u5408\u30011 * 3 \u306e\u30da\u30a2\u3092 2 * 2 \u306b\u5909\u63db\n        return int(math.pow(3, a - 1)) * 2 * 2\n    if b == 2:\n        # \u4f59\u308a\u304c 2 \u306e\u5834\u5408\u3001\u4f55\u3082\u3057\u306a\u3044\n        return int(math.pow(3, a)) * 2\n    # \u4f59\u308a\u304c 0 \u306e\u5834\u5408\u3001\u4f55\u3082\u3057\u306a\u3044\n    return int(math.pow(3, a))\n
        max_product_cutting.cpp
        /* \u6700\u5927\u7a4d\u5207\u65ad\uff1a\u8caa\u6b32\u6cd5 */\nint maxProductCutting(int n) {\n    // n <= 3 \u306e\u5834\u5408\u30011 \u3092\u5207\u308a\u51fa\u3059\u5fc5\u8981\u304c\u3042\u308b\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // \u8caa\u6b32\u306b 3 \u3092\u5207\u308a\u51fa\u3059\u3002a \u306f 3 \u306e\u500b\u6570\u3001b \u306f\u4f59\u308a\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // \u4f59\u308a\u304c 1 \u306e\u5834\u5408\u30011 * 3 \u306e\u30da\u30a2\u3092 2 * 2 \u306b\u5909\u63db\n        return (int)pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // \u4f59\u308a\u304c 2 \u306e\u5834\u5408\u3001\u4f55\u3082\u3057\u306a\u3044\n        return (int)pow(3, a) * 2;\n    }\n    // \u4f59\u308a\u304c 0 \u306e\u5834\u5408\u3001\u4f55\u3082\u3057\u306a\u3044\n    return (int)pow(3, a);\n}\n
        max_product_cutting.java
        /* \u6700\u5927\u7a4d\u5207\u65ad\uff1a\u8caa\u6b32\u6cd5 */\nint maxProductCutting(int n) {\n    // n <= 3 \u306e\u5834\u5408\u30011 \u3092\u5207\u308a\u51fa\u3059\u5fc5\u8981\u304c\u3042\u308b\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // \u8caa\u6b32\u306b 3 \u3092\u5207\u308a\u51fa\u3059\u3002a \u306f 3 \u306e\u500b\u6570\u3001b \u306f\u4f59\u308a\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // \u4f59\u308a\u304c 1 \u306e\u5834\u5408\u30011 * 3 \u306e\u30da\u30a2\u3092 2 * 2 \u306b\u5909\u63db\n        return (int) Math.pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // \u4f59\u308a\u304c 2 \u306e\u5834\u5408\u3001\u4f55\u3082\u3057\u306a\u3044\n        return (int) Math.pow(3, a) * 2;\n    }\n    // \u4f59\u308a\u304c 0 \u306e\u5834\u5408\u3001\u4f55\u3082\u3057\u306a\u3044\n    return (int) Math.pow(3, a);\n}\n
        max_product_cutting.cs
        [class]{max_product_cutting}-[func]{MaxProductCutting}\n
        max_product_cutting.go
        [class]{}-[func]{maxProductCutting}\n
        max_product_cutting.swift
        [class]{}-[func]{maxProductCutting}\n
        max_product_cutting.js
        [class]{}-[func]{maxProductCutting}\n
        max_product_cutting.ts
        [class]{}-[func]{maxProductCutting}\n
        max_product_cutting.dart
        [class]{}-[func]{maxProductCutting}\n
        max_product_cutting.rs
        [class]{}-[func]{max_product_cutting}\n
        max_product_cutting.c
        [class]{}-[func]{maxProductCutting}\n
        max_product_cutting.kt
        [class]{}-[func]{maxProductCutting}\n
        max_product_cutting.rb
        [class]{}-[func]{max_product_cutting}\n

        \u56f3 15-16 \u00a0 \u5207\u65ad\u5f8c\u306e\u6700\u5927\u7a4d\u306e\u8a08\u7b97\u65b9\u6cd5

        \u6642\u9593\u8a08\u7b97\u91cf\u306f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u3079\u304d\u4e57\u6f14\u7b97\u306e\u5b9f\u88c5\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002Python\u3067\u306f\u3001\u3088\u304f\u4f7f\u7528\u3055\u308c\u308b\u3079\u304d\u4e57\u8a08\u7b97\u95a2\u6570\u306f3\u7a2e\u985e\u3042\u308a\u307e\u3059\uff1a

        • \u6f14\u7b97\u5b50 ** \u3068\u95a2\u6570 pow() \u306e\u4e21\u65b9\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(\\log\u2061 a)\\) \u3067\u3059\u3002
        • math.pow() \u95a2\u6570\u306f\u5185\u90e8\u3067C\u8a00\u8a9e\u30e9\u30a4\u30d6\u30e9\u30ea\u306e pow() \u95a2\u6570\u3092\u547c\u3073\u51fa\u3057\u3001\u6d6e\u52d5\u5c0f\u6570\u70b9\u3079\u304d\u4e57\u3092\u5b9f\u884c\u3057\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(1)\\) \u3067\u3059\u3002

        \u5909\u6570 \\(a\\) \u3068 \\(b\\) \u306f\u4e00\u5b9a\u30b5\u30a4\u30ba\u306e\u8ffd\u52a0\u30b9\u30da\u30fc\u30b9\u3092\u4f7f\u7528\u3059\u308b\u305f\u3081\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(1)\\) \u3067\u3059\u3002

        "},{"location":"chapter_greedy/max_product_cutting_problem/#3","title":"3. \u00a0 \u6b63\u3057\u3055\u306e\u8a3c\u660e","text":"

        \u80cc\u7406\u6cd5\u3092\u4f7f\u7528\u3057\u3001\\(n \\geq 3\\) \u306e\u30b1\u30fc\u30b9\u306e\u307f\u3092\u5206\u6790\u3057\u307e\u3059\u3002

        1. \u3059\u3079\u3066\u306e\u56e0\u5b50 \\(\\leq 3\\)\uff1a\u6700\u9069\u5206\u5272\u30b9\u30ad\u30fc\u30e0\u304c\u56e0\u5b50 \\(x \\geq 4\\) \u3092\u542b\u3080\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u305d\u308c\u3092\u78ba\u5b9f\u306b \\(2(x-2)\\) \u306b\u3055\u3089\u306b\u5206\u5272\u3067\u304d\u3001\u3088\u308a\u5927\u304d\u306a\u7a4d\u3092\u5f97\u3089\u308c\u307e\u3059\u3002\u3053\u308c\u306f\u4eee\u5b9a\u3068\u77db\u76fe\u3057\u307e\u3059\u3002
        2. \u5206\u5272\u30b9\u30ad\u30fc\u30e0\u306b \\(1\\) \u304c\u542b\u307e\u308c\u306a\u3044\uff1a\u6700\u9069\u5206\u5272\u30b9\u30ad\u30fc\u30e0\u304c\u56e0\u5b50 \\(1\\) \u3092\u542b\u3080\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u305d\u308c\u3092\u78ba\u5b9f\u306b\u5225\u306e\u56e0\u5b50\u3068\u7d50\u5408\u3057\u3066\u3088\u308a\u5927\u304d\u306a\u7a4d\u3092\u5f97\u3089\u308c\u307e\u3059\u3002\u3053\u308c\u306f\u4eee\u5b9a\u3068\u77db\u76fe\u3057\u307e\u3059\u3002
        3. \u5206\u5272\u30b9\u30ad\u30fc\u30e0\u306b\u306f\u6700\u5927\u30672\u3064\u306e \\(2\\) \u304c\u542b\u307e\u308c\u308b\uff1a\u6700\u9069\u5206\u5272\u30b9\u30ad\u30fc\u30e0\u304c3\u3064\u306e \\(2\\) \u3092\u542b\u3080\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u305d\u308c\u3089\u3092\u78ba\u5b9f\u306b2\u3064\u306e \\(3\\) \u306b\u7f6e\u304d\u63db\u3048\u3066\u3001\u3088\u308a\u9ad8\u3044\u7a4d\u3092\u9054\u6210\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306f\u4eee\u5b9a\u3068\u77db\u76fe\u3057\u307e\u3059\u3002
        "},{"location":"chapter_greedy/summary/","title":"15.5 \u00a0 \u307e\u3068\u3081","text":"
        • \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u6700\u9069\u5316\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3088\u304f\u4f7f\u7528\u3055\u308c\u3001\u539f\u7406\u306f\u5404\u6c7a\u5b9a\u6bb5\u968e\u3067\u5c40\u6240\u7684\u306b\u6700\u9069\u306a\u6c7a\u5b9a\u3092\u884c\u3044\u3001\u30b0\u30ed\u30fc\u30d0\u30eb\u306b\u6700\u9069\u306a\u89e3\u3092\u9054\u6210\u3059\u308b\u3053\u3068\u3067\u3059\u3002
        • \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u8caa\u6b32\u306a\u9078\u629e\u3092\u6b21\u3005\u3068\u53cd\u5fa9\u7684\u306b\u884c\u3044\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u554f\u984c\u3092\u3088\u308a\u5c0f\u3055\u306a\u90e8\u5206\u554f\u984c\u306b\u5909\u63db\u3057\u3001\u554f\u984c\u304c\u89e3\u6c7a\u3055\u308c\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002
        • \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u5b9f\u88c5\u304c\u7c21\u5358\u306a\u3060\u3051\u3067\u306a\u304f\u3001\u554f\u984c\u89e3\u6c7a\u52b9\u7387\u3082\u9ad8\u3044\u3067\u3059\u3002\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3068\u6bd4\u8f03\u3057\u3066\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u4e00\u822c\u7684\u306b\u3088\u308a\u4f4e\u3044\u6642\u9593\u8a08\u7b97\u91cf\u3092\u6301\u3061\u307e\u3059\u3002
        • \u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u306b\u304a\u3044\u3066\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u7279\u5b9a\u306e\u30b3\u30a4\u30f3\u306e\u7d44\u307f\u5408\u308f\u305b\u306b\u5bfe\u3057\u3066\u6700\u9069\u89e3\u3092\u4fdd\u8a3c\u3067\u304d\u307e\u3059\u304c\u3001\u4ed6\u306e\u7d44\u307f\u5408\u308f\u305b\u3067\u306f\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u975e\u5e38\u306b\u60aa\u3044\u89e3\u3092\u898b\u3064\u3051\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        • \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u89e3\u6cd5\u306b\u9069\u3057\u305f\u554f\u984c\u306f2\u3064\u306e\u4e3b\u8981\u306a\u6027\u8cea\u3092\u6301\u3061\u307e\u3059\uff1a\u8caa\u6b32\u9078\u629e\u6027\u3068\u6700\u9069\u90e8\u5206\u69cb\u9020\u3002\u8caa\u6b32\u9078\u629e\u6027\u306f\u8caa\u6b32\u6226\u7565\u306e\u52b9\u679c\u3092\u8868\u3057\u307e\u3059\u3002
        • \u4e00\u90e8\u306e\u8907\u96d1\u306a\u554f\u984c\u3067\u306f\u3001\u8caa\u6b32\u9078\u629e\u6027\u3092\u8a3c\u660e\u3059\u308b\u3053\u3068\u306f\u7c21\u5358\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u9006\u306b\u3001\u7121\u52b9\u6027\u3092\u8a3c\u660e\u3059\u308b\u3053\u3068\u306f\u3057\u3070\u3057\u3070\u3088\u308a\u5bb9\u6613\u3067\u3001\u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u306a\u3069\u304c\u305d\u306e\u4f8b\u3067\u3059\u3002
        • \u8caa\u6b32\u554f\u984c\u306e\u89e3\u6c7a\u306f\u4e3b\u306b3\u3064\u306e\u30b9\u30c6\u30c3\u30d7\u304b\u3089\u69cb\u6210\u3055\u308c\u307e\u3059\uff1a\u554f\u984c\u5206\u6790\u3001\u8caa\u6b32\u6226\u7565\u306e\u6c7a\u5b9a\u3001\u6b63\u3057\u3055\u306e\u8a3c\u660e\u3002\u3053\u306e\u3046\u3061\u3001\u8caa\u6b32\u6226\u7565\u306e\u6c7a\u5b9a\u304c\u91cd\u8981\u306a\u30b9\u30c6\u30c3\u30d7\u3067\u3042\u308a\u3001\u6b63\u3057\u3055\u306e\u8a3c\u660e\u304c\u3057\u3070\u3057\u3070\u6311\u6226\u3068\u306a\u308a\u307e\u3059\u3002
        • \u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306f0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306b\u57fa\u3065\u3044\u3066\u30a2\u30a4\u30c6\u30e0\u306e\u4e00\u90e8\u306e\u9078\u629e\u3092\u53ef\u80fd\u306b\u3057\u3001\u3057\u305f\u304c\u3063\u3066\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u4f7f\u7528\u3057\u3066\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002\u8caa\u6b32\u6226\u7565\u306e\u6b63\u3057\u3055\u306f\u80cc\u7406\u6cd5\u306b\u3088\u3063\u3066\u8a3c\u660e\u3067\u304d\u307e\u3059\u3002
        • \u6700\u5927\u5bb9\u91cf\u554f\u984c\u306f\u5168\u63a2\u7d22\u6cd5\u3067\u89e3\u6c7a\u3067\u304d\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n^2)\\) \u3067\u3059\u3002\u8caa\u6b32\u6226\u7565\u3092\u8a2d\u8a08\u3059\u308b\u3053\u3068\u3067\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u77ed\u3044\u677f\u3092\u5185\u5074\u306b\u79fb\u52d5\u3057\u3001\u6642\u9593\u8a08\u7b97\u91cf\u3092 \\(O(n)\\) \u306b\u6700\u9069\u5316\u3057\u307e\u3059\u3002
        • \u5207\u65ad\u5f8c\u306e\u6700\u5927\u7a4d\u554f\u984c\u306b\u304a\u3044\u3066\u30012\u3064\u306e\u8caa\u6b32\u6226\u7565\u3092\u5c0e\u51fa\u3057\u307e\u3059\uff1a\\(\\geq 4\\) \u306e\u6574\u6570\u306f\u7d99\u7d9a\u7684\u306b\u5207\u65ad\u3055\u308c\u308b\u3079\u304d\u3067\u3001\u6700\u9069\u306a\u5207\u65ad\u56e0\u5b50\u306f \\(3\\) \u3067\u3059\u3002\u30b3\u30fc\u30c9\u306b\u306f\u3079\u304d\u4e57\u6f14\u7b97\u304c\u542b\u307e\u308c\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\u3079\u304d\u4e57\u6f14\u7b97\u306e\u5b9f\u88c5\u65b9\u6cd5\u306b\u4f9d\u5b58\u3057\u3001\u4e00\u822c\u7684\u306b \\(O(1)\\) \u307e\u305f\u306f \\(O(\\log n)\\) \u3067\u3059\u3002
        "},{"location":"chapter_hashing/","title":"\u7b2c 6 \u7ae0 \u00a0 \u30cf\u30c3\u30b7\u30e5\u8868","text":"

        Abstract

        \u30b3\u30f3\u30d4\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0\u306e\u4e16\u754c\u306b\u304a\u3044\u3066\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306f\u8ce2\u3044\u53f8\u66f8\u306e\u3088\u3046\u306a\u3082\u306e\u3067\u3059\u3002

        \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u756a\u53f7\u306e\u8a08\u7b97\u65b9\u6cd5\u3092\u7406\u89e3\u3057\u3001\u76ee\u7684\u306e\u672c\u3092\u8fc5\u901f\u306b\u53d6\u5f97\u3059\u308b\u3053\u3068\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002

        "},{"location":"chapter_hashing/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 6.1 \u00a0 \u30cf\u30c3\u30b7\u30e5\u8868
        • 6.2 \u00a0 \u30cf\u30c3\u30b7\u30e5\u885d\u7a81
        • 6.3 \u00a0 \u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0
        • 6.4 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_hashing/hash_algorithm/","title":"6.3 \u00a0 \u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0","text":"

        \u524d\u306e2\u3064\u306e\u7bc0\u3067\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u52d5\u4f5c\u539f\u7406\u3068\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u51e6\u7406\u3059\u308b\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002\u3057\u304b\u3057\u3001\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u3068\u9023\u9396\u6cd5\u306f\u3069\u3061\u3089\u3082**\u885d\u7a81\u304c\u767a\u751f\u3057\u305f\u969b\u306b\u30cf\u30c3\u30b7\u30e5\u8868\u304c\u6b63\u5e38\u306b\u6a5f\u80fd\u3059\u308b\u3053\u3068\u306e\u307f\u3092\u4fdd\u8a3c\u3067\u304d\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306e\u767a\u751f\u983b\u5ea6\u3092\u6e1b\u3089\u3059\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093**\u3002

        \u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u304c\u3042\u307e\u308a\u306b\u3082\u983b\u7e41\u306b\u767a\u751f\u3059\u308b\u3068\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u6027\u80fd\u306f\u5287\u7684\u306b\u60aa\u5316\u3057\u307e\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u9023\u9396\u6cd5\u30cf\u30c3\u30b7\u30e5\u8868\u3067\u306f\u3001\u7406\u60f3\u7684\u306a\u30b1\u30fc\u30b9\u3067\u306f\u30ad\u30fc\u5024\u30da\u30a2\u304c\u30d0\u30b1\u30c3\u30c8\u9593\u306b\u5747\u7b49\u306b\u5206\u6563\u3055\u308c\u3001\u6700\u9069\u306a\u30af\u30a8\u30ea\u52b9\u7387\u3092\u5b9f\u73fe\u3057\u307e\u3059\u3002\u6700\u60aa\u306e\u30b1\u30fc\u30b9\u3067\u306f\u3001\u3059\u3079\u3066\u306e\u30ad\u30fc\u5024\u30da\u30a2\u304c\u540c\u3058\u30d0\u30b1\u30c3\u30c8\u306b\u683c\u7d0d\u3055\u308c\u3001\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(n)\\)\u306b\u60aa\u5316\u3057\u307e\u3059\u3002

        \u56f3 6-8 \u00a0 \u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306e\u7406\u60f3\u7684\u304a\u3088\u3073\u6700\u60aa\u306e\u30b1\u30fc\u30b9

        \u30ad\u30fc\u5024\u30da\u30a2\u306e\u5206\u5e03\u306f\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306b\u3088\u3063\u3066\u6c7a\u5b9a\u3055\u308c\u307e\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u8a08\u7b97\u30b9\u30c6\u30c3\u30d7\u3092\u601d\u3044\u51fa\u3059\u3068\u3001\u307e\u305a\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u8a08\u7b97\u3057\u3001\u6b21\u306b\u914d\u5217\u9577\u3067\u5270\u4f59\u3092\u53d6\u308a\u307e\u3059\uff1a

        index = hash(key) % capacity\n

        \u4e0a\u8a18\u306e\u5f0f\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u5bb9\u91cfcapacity\u304c\u56fa\u5b9a\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0hash()\u304c\u51fa\u529b\u5024\u3092\u6c7a\u5b9a\u3057\u3001\u305d\u308c\u306b\u3088\u3063\u3066\u30cf\u30c3\u30b7\u30e5\u8868\u306b\u304a\u3051\u308b\u30ad\u30fc\u5024\u30da\u30a2\u306e\u5206\u5e03\u3092\u6c7a\u5b9a\u3057\u307e\u3059\u3002

        \u3053\u308c\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306e\u78ba\u7387\u3092\u6e1b\u3089\u3059\u305f\u3081\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0hash()\u306e\u8a2d\u8a08\u306b\u7126\u70b9\u3092\u5f53\u3066\u308b\u3079\u304d\u3067\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

        "},{"location":"chapter_hashing/hash_algorithm/#631","title":"6.3.1 \u00a0 \u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u76ee\u6a19","text":"

        \u300c\u9ad8\u901f\u3067\u5b89\u5b9a\u3057\u305f\u300d\u30cf\u30c3\u30b7\u30e5\u8868\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u5b9f\u73fe\u3059\u308b\u305f\u3081\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u4ee5\u4e0b\u306e\u7279\u6027\u3092\u6301\u3064\u3079\u304d\u3067\u3059\uff1a

        • \u6c7a\u5b9a\u6027: \u540c\u3058\u5165\u529b\u306b\u5bfe\u3057\u3066\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u5e38\u306b\u540c\u3058\u51fa\u529b\u3092\u751f\u6210\u3059\u308b\u3079\u304d\u3067\u3059\u3002\u305d\u3046\u3067\u306a\u3051\u308c\u3070\u30cf\u30c3\u30b7\u30e5\u8868\u306f\u4fe1\u983c\u3067\u304d\u307e\u305b\u3093\u3002
        • \u9ad8\u52b9\u7387: \u30cf\u30c3\u30b7\u30e5\u5024\u3092\u8a08\u7b97\u3059\u308b\u30d7\u30ed\u30bb\u30b9\u306f\u5341\u5206\u306b\u9ad8\u901f\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u8a08\u7b97\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u304c\u5c0f\u3055\u3044\u307b\u3069\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306f\u3088\u308a\u5b9f\u7528\u7684\u306b\u306a\u308a\u307e\u3059\u3002
        • \u5747\u7b49\u5206\u6563: \u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u30ad\u30fc\u5024\u30da\u30a2\u304c\u30cf\u30c3\u30b7\u30e5\u8868\u306b\u5747\u7b49\u306b\u5206\u6563\u3055\u308c\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3059\u308b\u3079\u304d\u3067\u3059\u3002\u5206\u6563\u304c\u5747\u7b49\u3067\u3042\u308b\u307b\u3069\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306e\u78ba\u7387\u306f\u4f4e\u304f\u306a\u308a\u307e\u3059\u3002

        \u5b9f\u969b\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u5b9f\u88c5\u3060\u3051\u3067\u306a\u304f\u3001\u4ed6\u306e\u5206\u91ce\u3067\u3082\u5e83\u304f\u5fdc\u7528\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        • \u30d1\u30b9\u30ef\u30fc\u30c9\u4fdd\u5b58: \u30e6\u30fc\u30b6\u30fc\u30d1\u30b9\u30ef\u30fc\u30c9\u306e\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u3092\u4fdd\u8b77\u3059\u308b\u305f\u3081\u306b\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u901a\u5e38\u5e73\u6587\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u4fdd\u5b58\u305b\u305a\u3001\u30d1\u30b9\u30ef\u30fc\u30c9\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u4fdd\u5b58\u3057\u307e\u3059\u3002\u30e6\u30fc\u30b6\u30fc\u304c\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u5165\u529b\u3059\u308b\u3068\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u5165\u529b\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u8a08\u7b97\u3057\u3001\u4fdd\u5b58\u3055\u308c\u3066\u3044\u308b\u30cf\u30c3\u30b7\u30e5\u5024\u3068\u6bd4\u8f03\u3057\u307e\u3059\u3002\u4e00\u81f4\u3059\u308c\u3070\u3001\u30d1\u30b9\u30ef\u30fc\u30c9\u306f\u6b63\u3057\u3044\u3068\u898b\u306a\u3055\u308c\u307e\u3059\u3002
        • \u30c7\u30fc\u30bf\u6574\u5408\u6027\u30c1\u30a7\u30c3\u30af: \u30c7\u30fc\u30bf\u9001\u4fe1\u8005\u306f\u30c7\u30fc\u30bf\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u8a08\u7b97\u3057\u3066\u4e00\u7dd2\u306b\u9001\u4fe1\u3067\u304d\u307e\u3059\u3002\u53d7\u4fe1\u8005\u306f\u53d7\u4fe1\u3057\u305f\u30c7\u30fc\u30bf\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u518d\u8a08\u7b97\u3057\u3001\u53d7\u4fe1\u3057\u305f\u30cf\u30c3\u30b7\u30e5\u5024\u3068\u6bd4\u8f03\u3067\u304d\u307e\u3059\u3002\u4e00\u81f4\u3059\u308c\u3070\u3001\u30c7\u30fc\u30bf\u306f\u5b8c\u5168\u3067\u3042\u308b\u3068\u898b\u306a\u3055\u308c\u307e\u3059\u3002

        \u6697\u53f7\u5316\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u5024\u304b\u3089\u5143\u306e\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u63a8\u6e2c\u3059\u308b\u306a\u3069\u306e\u9006\u884c\u5206\u6790\u3092\u9632\u3050\u305f\u3081\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3088\u308a\u9ad8\u3044\u30ec\u30d9\u30eb\u306e\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u6a5f\u80fd\u304c\u5fc5\u8981\u3067\u3059\u3002

        • \u4e00\u65b9\u5411\u6027: \u30cf\u30c3\u30b7\u30e5\u5024\u304b\u3089\u5165\u529b\u30c7\u30fc\u30bf\u306b\u95a2\u3059\u308b\u60c5\u5831\u3092\u63a8\u6e2c\u3059\u308b\u3053\u3068\u306f\u4e0d\u53ef\u80fd\u3067\u3042\u308b\u3079\u304d\u3067\u3059\u3002
        • \u885d\u7a81\u8010\u6027: \u540c\u3058\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u751f\u6210\u3059\u308b2\u3064\u306e\u7570\u306a\u308b\u5165\u529b\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u306f\u6975\u3081\u3066\u56f0\u96e3\u3067\u3042\u308b\u3079\u304d\u3067\u3059\u3002
        • \u96ea\u5d29\u52b9\u679c: \u5165\u529b\u306e\u5c0f\u3055\u306a\u5909\u66f4\u306f\u3001\u51fa\u529b\u306b\u5927\u304d\u304f\u4e88\u6e2c\u4e0d\u53ef\u80fd\u306a\u5909\u5316\u3092\u3082\u305f\u3089\u3059\u3079\u304d\u3067\u3059\u3002

        **\u300c\u5747\u7b49\u5206\u6563\u300d\u3068\u300c\u885d\u7a81\u8010\u6027\u300d\u306f2\u3064\u306e\u5225\u3005\u306e\u6982\u5ff5**\u3067\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u5747\u7b49\u5206\u6563\u3092\u6e80\u305f\u3057\u3066\u3082\u3001\u5fc5\u305a\u3057\u3082\u885d\u7a81\u8010\u6027\u304c\u3042\u308b\u3068\u306f\u9650\u308a\u307e\u305b\u3093\u3002\u4f8b\u3048\u3070\u3001\u30e9\u30f3\u30c0\u30e0\u306a\u5165\u529bkey\u306e\u4e0b\u3067\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570key % 100\u306f\u5747\u7b49\u306b\u5206\u6563\u3055\u308c\u305f\u51fa\u529b\u3092\u751f\u6210\u3067\u304d\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u3053\u306e\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u904e\u5ea6\u306b\u30b7\u30f3\u30d7\u30eb\u3067\u3001\u4e0b\u4e8c\u6841\u304c\u540c\u3058\u3059\u3079\u3066\u306ekey\u306f\u540c\u3058\u51fa\u529b\u3092\u6301\u3064\u305f\u3081\u3001\u30cf\u30c3\u30b7\u30e5\u5024\u304b\u3089\u4f7f\u7528\u53ef\u80fd\u306akey\u3092\u7c21\u5358\u306b\u63a8\u6e2c\u3067\u304d\u3001\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u7834\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_hashing/hash_algorithm/#632","title":"6.3.2 \u00a0 \u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u8a2d\u8a08","text":"

        \u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u8a2d\u8a08\u306f\u591a\u304f\u306e\u8981\u56e0\u3092\u8003\u616e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u8907\u96d1\u306a\u554f\u984c\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u8981\u6c42\u304c\u5c11\u306a\u3044\u4e00\u90e8\u306e\u30b7\u30ca\u30ea\u30aa\u3067\u306f\u3001\u3044\u304f\u3064\u304b\u306e\u7c21\u5358\u306a\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u8a2d\u8a08\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002

        • \u52a0\u7b97\u30cf\u30c3\u30b7\u30e5: \u5165\u529b\u306e\u5404\u6587\u5b57\u306eASCII\u30b3\u30fc\u30c9\u3092\u5408\u8a08\u3057\u3001\u5408\u8a08\u3092\u30cf\u30c3\u30b7\u30e5\u5024\u3068\u3057\u3066\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u4e57\u7b97\u30cf\u30c3\u30b7\u30e5: \u4e57\u7b97\u306e\u975e\u76f8\u95a2\u6027\u3092\u5229\u7528\u3057\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u5b9a\u6570\u3092\u4e57\u7b97\u3057\u3001\u5404\u6587\u5b57\u306eASCII\u30b3\u30fc\u30c9\u3092\u30cf\u30c3\u30b7\u30e5\u5024\u306b\u7d2f\u7a4d\u3057\u307e\u3059\u3002
        • XOR\u30cf\u30c3\u30b7\u30e5: \u5165\u529b\u30c7\u30fc\u30bf\u306e\u5404\u8981\u7d20\u3092XOR\u3059\u308b\u3053\u3068\u3067\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u7d2f\u7a4d\u3057\u307e\u3059\u3002
        • \u56de\u8ee2\u30cf\u30c3\u30b7\u30e5: \u5404\u6587\u5b57\u306eASCII\u30b3\u30fc\u30c9\u3092\u30cf\u30c3\u30b7\u30e5\u5024\u306b\u7d2f\u7a4d\u3057\u3001\u5404\u7d2f\u7a4d\u524d\u306b\u30cf\u30c3\u30b7\u30e5\u5024\u306b\u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby simple_hash.py
        def add_hash(key: str) -> int:\n    \"\"\"\u52a0\u6cd5\u30cf\u30c3\u30b7\u30e5\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash += ord(c)\n    return hash % modulus\n\ndef mul_hash(key: str) -> int:\n    \"\"\"\u4e57\u6cd5\u30cf\u30c3\u30b7\u30e5\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash = 31 * hash + ord(c)\n    return hash % modulus\n\ndef xor_hash(key: str) -> int:\n    \"\"\"XOR\u30cf\u30c3\u30b7\u30e5\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash ^= ord(c)\n    return hash % modulus\n\ndef rot_hash(key: str) -> int:\n    \"\"\"\u56de\u8ee2\u30cf\u30c3\u30b7\u30e5\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash = (hash << 4) ^ (hash >> 28) ^ ord(c)\n    return hash % modulus\n
        simple_hash.cpp
        /* \u52a0\u7b97\u30cf\u30c3\u30b7\u30e5 */\nint addHash(string key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash = (hash + (int)c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* \u4e57\u7b97\u30cf\u30c3\u30b7\u30e5 */\nint mulHash(string key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash = (31 * hash + (int)c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* XOR\u30cf\u30c3\u30b7\u30e5 */\nint xorHash(string key) {\n    int hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash ^= (int)c;\n    }\n    return hash & MODULUS;\n}\n\n/* \u56de\u8ee2\u30cf\u30c3\u30b7\u30e5 */\nint rotHash(string key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ (int)c) % MODULUS;\n    }\n    return (int)hash;\n}\n
        simple_hash.java
        /* \u52a0\u7b97\u30cf\u30c3\u30b7\u30e5 */\nint addHash(String key) {\n    long hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash = (hash + (int) c) % MODULUS;\n    }\n    return (int) hash;\n}\n\n/* \u4e57\u7b97\u30cf\u30c3\u30b7\u30e5 */\nint mulHash(String key) {\n    long hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash = (31 * hash + (int) c) % MODULUS;\n    }\n    return (int) hash;\n}\n\n/* XOR\u30cf\u30c3\u30b7\u30e5 */\nint xorHash(String key) {\n    int hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash ^= (int) c;\n    }\n    return hash & MODULUS;\n}\n\n/* \u56de\u8ee2\u30cf\u30c3\u30b7\u30e5 */\nint rotHash(String key) {\n    long hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ (int) c) % MODULUS;\n    }\n    return (int) hash;\n}\n
        simple_hash.cs
        [class]{simple_hash}-[func]{AddHash}\n\n[class]{simple_hash}-[func]{MulHash}\n\n[class]{simple_hash}-[func]{XorHash}\n\n[class]{simple_hash}-[func]{RotHash}\n
        simple_hash.go
        [class]{}-[func]{addHash}\n\n[class]{}-[func]{mulHash}\n\n[class]{}-[func]{xorHash}\n\n[class]{}-[func]{rotHash}\n
        simple_hash.swift
        [class]{}-[func]{addHash}\n\n[class]{}-[func]{mulHash}\n\n[class]{}-[func]{xorHash}\n\n[class]{}-[func]{rotHash}\n
        simple_hash.js
        [class]{}-[func]{addHash}\n\n[class]{}-[func]{mulHash}\n\n[class]{}-[func]{xorHash}\n\n[class]{}-[func]{rotHash}\n
        simple_hash.ts
        [class]{}-[func]{addHash}\n\n[class]{}-[func]{mulHash}\n\n[class]{}-[func]{xorHash}\n\n[class]{}-[func]{rotHash}\n
        simple_hash.dart
        [class]{}-[func]{addHash}\n\n[class]{}-[func]{mulHash}\n\n[class]{}-[func]{xorHash}\n\n[class]{}-[func]{rotHash}\n
        simple_hash.rs
        [class]{}-[func]{add_hash}\n\n[class]{}-[func]{mul_hash}\n\n[class]{}-[func]{xor_hash}\n\n[class]{}-[func]{rot_hash}\n
        simple_hash.c
        [class]{}-[func]{addHash}\n\n[class]{}-[func]{mulHash}\n\n[class]{}-[func]{xorHash}\n\n[class]{}-[func]{rotHash}\n
        simple_hash.kt
        [class]{}-[func]{addHash}\n\n[class]{}-[func]{mulHash}\n\n[class]{}-[func]{xorHash}\n\n[class]{}-[func]{rotHash}\n
        simple_hash.rb
        [class]{}-[func]{add_hash}\n\n[class]{}-[func]{mul_hash}\n\n[class]{}-[func]{xor_hash}\n\n[class]{}-[func]{rot_hash}\n

        \u5404\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6700\u5f8c\u306e\u30b9\u30c6\u30c3\u30d7\u304c\u5927\u304d\u306a\u7d20\u6570\\(1000000007\\)\u306e\u5270\u4f59\u3092\u53d6\u308b\u3053\u3068\u3067\u3001\u30cf\u30c3\u30b7\u30e5\u5024\u304c\u9069\u5207\u306a\u7bc4\u56f2\u5185\u306b\u3042\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3057\u3066\u3044\u308b\u3053\u3068\u304c\u89b3\u5bdf\u3055\u308c\u307e\u3059\u3002\u306a\u305c\u7d20\u6570\u306e\u5270\u4f59\u3092\u53d6\u308b\u3053\u3068\u304c\u5f37\u8abf\u3055\u308c\u308b\u306e\u304b\u3001\u307e\u305f\u306f\u5408\u6210\u6570\u306e\u5270\u4f59\u3092\u53d6\u308b\u3053\u3068\u306e\u6b20\u70b9\u306f\u4f55\u304b\u3092\u8003\u3048\u308b\u4fa1\u5024\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u306f\u8208\u5473\u6df1\u3044\u8cea\u554f\u3067\u3059\u3002

        \u7d50\u8ad6\u3068\u3057\u3066\uff1a\u5927\u304d\u306a\u7d20\u6570\u3092\u5270\u4f59\u3068\u3057\u3066\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30cf\u30c3\u30b7\u30e5\u5024\u306e\u5747\u7b49\u5206\u6563\u3092\u6700\u5927\u5316\u3067\u304d\u307e\u3059\u3002\u7d20\u6570\u306f\u4ed6\u306e\u6570\u3068\u5171\u901a\u56e0\u5b50\u3092\u6301\u305f\u306a\u3044\u305f\u3081\u3001\u5270\u4f59\u6f14\u7b97\u306b\u3088\u3063\u3066\u5f15\u304d\u8d77\u3053\u3055\u308c\u308b\u5468\u671f\u7684\u30d1\u30bf\u30fc\u30f3\u3092\u6e1b\u3089\u3057\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u56de\u907f\u3067\u304d\u307e\u3059\u3002

        \u4f8b\u3048\u3070\u3001\u5408\u6210\u6570\\(9\\)\u3092\u5270\u4f59\u3068\u3057\u3066\u9078\u629e\u3059\u308b\u3068\u3057\u307e\u3059\u3002\u3053\u308c\u306f\\(3\\)\u3067\u5272\u308a\u5207\u308c\u308b\u305f\u3081\u3001\\(3\\)\u3067\u5272\u308a\u5207\u308c\u308b\u3059\u3079\u3066\u306ekey\u306f\u30cf\u30c3\u30b7\u30e5\u5024\\(0\\)\u3001\\(3\\)\u3001\\(6\\)\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3055\u308c\u307e\u3059\u3002

        \\[ \\begin{aligned} \\text{modulus} & = 9 \\newline \\text{key} & = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\dots \\} \\newline \\text{hash} & = \\{ 0, 3, 6, 0, 3, 6, 0, 3, 6, 0, 3, 6,\\dots \\} \\end{aligned} \\]

        \u5165\u529bkey\u304c\u305f\u307e\u305f\u307e\u3053\u306e\u7a2e\u306e\u7b49\u5dee\u6570\u5217\u5206\u5e03\u3092\u6301\u3064\u5834\u5408\u3001\u30cf\u30c3\u30b7\u30e5\u5024\u304c\u30af\u30e9\u30b9\u30bf\u30fc\u3057\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u60aa\u5316\u3055\u305b\u307e\u3059\u3002\u4eca\u5ea6\u306fmodulus\u3092\u7d20\u6570\\(13\\)\u306b\u7f6e\u304d\u63db\u3048\u308b\u3068\u3057\u307e\u3059\u3002key\u3068modulus\u306e\u9593\u306b\u5171\u901a\u56e0\u5b50\u304c\u306a\u3044\u305f\u3081\u3001\u51fa\u529b\u30cf\u30c3\u30b7\u30e5\u5024\u306e\u5747\u7b49\u6027\u304c\u5927\u5e45\u306b\u6539\u5584\u3055\u308c\u307e\u3059\u3002

        \\[ \\begin{aligned} \\text{modulus} & = 13 \\newline \\text{key} & = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\dots \\} \\newline \\text{hash} & = \\{ 0, 3, 6, 9, 12, 2, 5, 8, 11, 1, 4, 7, \\dots \\} \\end{aligned} \\]

        key\u304c\u30e9\u30f3\u30c0\u30e0\u3067\u5747\u7b49\u306b\u5206\u6563\u3055\u308c\u308b\u3053\u3068\u304c\u4fdd\u8a3c\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u5270\u4f59\u3068\u3057\u3066\u7d20\u6570\u307e\u305f\u306f\u5408\u6210\u6570\u3092\u9078\u629e\u3057\u3066\u3082\u3001\u4e21\u65b9\u3068\u3082\u5747\u7b49\u306b\u5206\u6563\u3055\u308c\u305f\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u751f\u6210\u3067\u304d\u308b\u3053\u3068\u306f\u6ce8\u76ee\u306b\u5024\u3057\u307e\u3059\u3002\u3057\u304b\u3057\u3001key\u306e\u5206\u5e03\u306b\u3042\u308b\u7a2e\u306e\u5468\u671f\u6027\u304c\u3042\u308b\u5834\u5408\u3001\u5408\u6210\u6570\u306e\u5270\u4f59\u306f\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u3092\u5f15\u304d\u8d77\u3053\u3057\u3084\u3059\u304f\u306a\u308a\u307e\u3059\u3002

        \u8981\u7d04\u3059\u308b\u3068\u3001\u901a\u5e38\u306f\u7d20\u6570\u3092\u5270\u4f59\u3068\u3057\u3066\u9078\u629e\u3057\u3001\u3053\u306e\u7d20\u6570\u306f\u5468\u671f\u7684\u30d1\u30bf\u30fc\u30f3\u3092\u53ef\u80fd\u306a\u9650\u308a\u6392\u9664\u3057\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5805\u7262\u6027\u3092\u5411\u4e0a\u3055\u305b\u308b\u305f\u3081\u306b\u5341\u5206\u5927\u304d\u304f\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_hashing/hash_algorithm/#633","title":"6.3.3 \u00a0 \u4e00\u822c\u7684\u306a\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0","text":"

        \u4e0a\u8a18\u3067\u8a00\u53ca\u3057\u305f\u7c21\u5358\u306a\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u304b\u306a\u308a\u300c\u8106\u5f31\u300d\u3067\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u8a2d\u8a08\u76ee\u6a19\u304b\u3089\u7a0b\u9060\u3044\u3053\u3068\u306f\u96e3\u3057\u304f\u3042\u308a\u307e\u305b\u3093\u3002\u4f8b\u3048\u3070\u3001\u52a0\u7b97\u3068XOR\u306f\u4ea4\u63db\u6cd5\u5247\u306b\u5f93\u3046\u305f\u3081\u3001\u52a0\u7b97\u30cf\u30c3\u30b7\u30e5\u3068XOR\u30cf\u30c3\u30b7\u30e5\u306f\u540c\u3058\u5185\u5bb9\u3060\u304c\u9806\u5e8f\u304c\u7570\u306a\u308b\u6587\u5b57\u5217\u3092\u533a\u5225\u3067\u304d\u305a\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u60aa\u5316\u3055\u305b\u3001\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u554f\u984c\u3092\u5f15\u304d\u8d77\u3053\u3059\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        \u5b9f\u969b\u306b\u306f\u3001\u901a\u5e38MD5\u3001SHA-1\u3001SHA-2\u3001SHA-3\u306a\u3069\u306e\u6a19\u6e96\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u3053\u308c\u3089\u306f\u4efb\u610f\u306e\u9577\u3055\u306e\u5165\u529b\u30c7\u30fc\u30bf\u3092\u56fa\u5b9a\u9577\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3067\u304d\u307e\u3059\u3002

        \u904e\u53bb1\u4e16\u7d00\u306b\u308f\u305f\u3063\u3066\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u7d99\u7d9a\u7684\u306a\u30a2\u30c3\u30d7\u30b0\u30ec\u30fc\u30c9\u3068\u6700\u9069\u5316\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u3042\u308a\u307e\u3057\u305f\u3002\u4e00\u90e8\u306e\u7814\u7a76\u8005\u306f\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6027\u80fd\u5411\u4e0a\u306b\u52aa\u3081\u3001\u30cf\u30c3\u30ab\u30fc\u3092\u542b\u3080\u4ed6\u306e\u4eba\u3005\u306f\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u554f\u984c\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u306b\u5c02\u5ff5\u3057\u3066\u3044\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u8868\u306f\u3001\u5b9f\u7528\u7684\u306a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u308b\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        • MD5\u3068SHA-1\u306f\u8907\u6570\u56de\u653b\u6483\u306b\u6210\u529f\u3057\u3066\u304a\u308a\u3001\u3055\u307e\u3056\u307e\u306a\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u653e\u68c4\u3055\u308c\u3066\u3044\u307e\u3059\u3002
        • SHA-2\u30b7\u30ea\u30fc\u30ba\u3001\u7279\u306bSHA-256\u306f\u3001\u73fe\u5728\u6700\u3082\u5b89\u5168\u306a\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e1\u3064\u3067\u3001\u6210\u529f\u3057\u305f\u653b\u6483\u306f\u5831\u544a\u3055\u308c\u3066\u304a\u3089\u305a\u3001\u3055\u307e\u3056\u307e\u306a\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3068\u30d7\u30ed\u30c8\u30b3\u30eb\u3067\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u3066\u3044\u307e\u3059\u3002
        • SHA-3\u306fSHA-2\u3068\u6bd4\u8f03\u3057\u3066\u5b9f\u88c5\u30b3\u30b9\u30c8\u304c\u4f4e\u304f\u3001\u8a08\u7b97\u52b9\u7387\u304c\u9ad8\u3044\u3067\u3059\u304c\u3001\u73fe\u5728\u306e\u4f7f\u7528\u7bc4\u56f2\u306fSHA-2\u30b7\u30ea\u30fc\u30ba\u307b\u3069\u5e83\u7bc4\u56f2\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002

        \u8868 6-2 \u00a0 \u4e00\u822c\u7684\u306a\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0

        MD5 SHA-1 SHA-2 SHA-3 \u30ea\u30ea\u30fc\u30b9\u5e74 1992 1995 2002 2008 \u51fa\u529b\u9577 128 bit 160 bit 256/512 bit 224/256/384/512 bit \u30cf\u30c3\u30b7\u30e5\u885d\u7a81 \u983b\u7e41 \u983b\u7e41 \u307e\u308c \u307e\u308c \u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u30ec\u30d9\u30eb \u4f4e\u3001\u653b\u6483\u306b\u6210\u529f\u3057\u3066\u3044\u308b \u4f4e\u3001\u653b\u6483\u306b\u6210\u529f\u3057\u3066\u3044\u308b \u9ad8 \u9ad8 \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3 \u653e\u68c4\u3001\u30c7\u30fc\u30bf\u6574\u5408\u6027\u30c1\u30a7\u30c3\u30af\u306b\u307e\u3060\u4f7f\u7528 \u653e\u68c4 \u6697\u53f7\u901a\u8ca8\u53d6\u5f15\u691c\u8a3c\u3001\u30c7\u30b8\u30bf\u30eb\u7f72\u540d\u306a\u3069 SHA-2\u306e\u4ee3\u66ff\u3068\u3057\u3066\u4f7f\u7528\u53ef\u80fd"},{"location":"chapter_hashing/hash_algorithm/#_1","title":"\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u304a\u3051\u308b\u30cf\u30c3\u30b7\u30e5\u5024","text":"

        \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30ad\u30fc\u306f\u6574\u6570\u3001\u5c0f\u6570\u3001\u6587\u5b57\u5217\u306a\u3069\u306e\u3055\u307e\u3056\u307e\u306a\u30c7\u30fc\u30bf\u578b\u306b\u3067\u304d\u308b\u3053\u3068\u3092\u77e5\u3063\u3066\u3044\u307e\u3059\u3002\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306f\u901a\u5e38\u3001\u3053\u308c\u3089\u306e\u30c7\u30fc\u30bf\u578b\u306b\u5bfe\u3057\u3066\u7d44\u307f\u8fbc\u307f\u306e\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u63d0\u4f9b\u3057\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97\u3057\u307e\u3059\u3002Python\u3092\u4f8b\u306b\u53d6\u308b\u3068\u3001hash()\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u3055\u307e\u3056\u307e\u306a\u30c7\u30fc\u30bf\u578b\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u8a08\u7b97\u3067\u304d\u307e\u3059\u3002

        • \u6574\u6570\u3068\u30d6\u30fc\u30eb\u5024\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f\u3001\u305d\u308c\u3089\u81ea\u8eab\u306e\u5024\u3067\u3059\u3002
        • \u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u3068\u6587\u5b57\u5217\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306e\u8a08\u7b97\u306f\u3088\u308a\u8907\u96d1\u3067\u3001\u8208\u5473\u306e\u3042\u308b\u8aad\u8005\u306f\u81ea\u5206\u3067\u7814\u7a76\u3059\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002
        • \u30bf\u30d7\u30eb\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f\u3001\u305d\u306e\u5404\u8981\u7d20\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306e\u7d44\u307f\u5408\u308f\u305b\u3067\u3001\u5358\u4e00\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306b\u306a\u308a\u307e\u3059\u3002
        • \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f\u3001\u305d\u306e\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u306b\u57fa\u3065\u3044\u3066\u751f\u6210\u3055\u308c\u307e\u3059\u3002\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30cf\u30c3\u30b7\u30e5\u30e1\u30bd\u30c3\u30c9\u3092\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\u3059\u308b\u3053\u3068\u3067\u3001\u5185\u5bb9\u306b\u57fa\u3065\u3044\u3066\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u751f\u6210\u3067\u304d\u307e\u3059\u3002

        Tip

        \u7570\u306a\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u304a\u3051\u308b\u7d44\u307f\u8fbc\u307f\u30cf\u30c3\u30b7\u30e5\u5024\u8a08\u7b97\u95a2\u6570\u306e\u5b9a\u7fa9\u3068\u65b9\u6cd5\u306f\u7570\u306a\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin built_in_hash.py
        num = 3\nhash_num = hash(num)\n# \u6574\u65703\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f3\n\nbol = True\nhash_bol = hash(bol)\n# \u30d6\u30fc\u30eb\u5024True\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f1\n\ndec = 3.14159\nhash_dec = hash(dec)\n# \u5c0f\u65703.14159\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f326484311674566659\n\nstr = \"Hello \u7b97\u6cd5\"\nhash_str = hash(str)\n# \u6587\u5b57\u5217\"Hello \u7b97\u6cd5\"\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f4617003410720528961\n\ntup = (12836, \"\u5c0f\u54c8\")\nhash_tup = hash(tup)\n# \u30bf\u30d7\u30eb(12836, '\u5c0f\u54c8')\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f1029005403108185979\n\nobj = ListNode(0)\nhash_obj = hash(obj)\n# ListNode\u30aa\u30d6\u30b8\u30a7\u30af\u30c80x1058fd810\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f274267521\n
        built_in_hash.cpp
        int num = 3;\nsize_t hashNum = hash<int>()(num);\n// \u6574\u65703\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f3\n\nbool bol = true;\nsize_t hashBol = hash<bool>()(bol);\n// \u30d6\u30fc\u30eb\u50241\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f1\n\ndouble dec = 3.14159;\nsize_t hashDec = hash<double>()(dec);\n// \u5c0f\u65703.14159\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f4614256650576692846\n\nstring str = \"Hello \u7b97\u6cd5\";\nsize_t hashStr = hash<string>()(str);\n// \u6587\u5b57\u5217\"Hello \u7b97\u6cd5\"\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f15466937326284535026\n\n// C++\u3067\u306f\u3001\u7d44\u307f\u8fbc\u307fstd::hash()\u306f\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306e\u307f\u3092\u63d0\u4f9b\n// \u914d\u5217\u3068\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f\u5225\u9014\u5b9f\u88c5\u304c\u5fc5\u8981\n
        built_in_hash.java
        int num = 3;\nint hashNum = Integer.hashCode(num);\n// \u6574\u65703\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f3\n\nboolean bol = true;\nint hashBol = Boolean.hashCode(bol);\n// \u30d6\u30fc\u30eb\u5024true\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f1231\n\ndouble dec = 3.14159;\nint hashDec = Double.hashCode(dec);\n// \u5c0f\u65703.14159\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f-1340954729\n\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode();\n// \u6587\u5b57\u5217\"Hello \u7b97\u6cd5\"\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f-727081396\n\nObject[] arr = { 12836, \"\u5c0f\u54c8\" };\nint hashTup = Arrays.hashCode(arr);\n// \u914d\u5217[12836, \u5c0f\u54c8]\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f1151158\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode();\n// ListNode\u30aa\u30d6\u30b8\u30a7\u30af\u30c8utils.ListNode@7dc5e7b4\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f2110121908\n
        built_in_hash.cs
        int num = 3;\nint hashNum = num.GetHashCode();\n// \u6574\u65703\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f3;\n\nbool bol = true;\nint hashBol = bol.GetHashCode();\n// \u30d6\u30fc\u30eb\u5024true\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f1;\n\ndouble dec = 3.14159;\nint hashDec = dec.GetHashCode();\n// \u5c0f\u65703.14159\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f-1340954729;\n\nstring str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.GetHashCode();\n// \u6587\u5b57\u5217\"Hello \u7b97\u6cd5\"\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f-586107568;\n\nobject[] arr = [12836, \"\u5c0f\u54c8\"];\nint hashTup = arr.GetHashCode();\n// \u914d\u5217[12836, \u5c0f\u54c8]\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f42931033;\n\nListNode obj = new(0);\nint hashObj = obj.GetHashCode();\n// ListNode\u30aa\u30d6\u30b8\u30a7\u30af\u30c80\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f39053774;\n
        built_in_hash.go
        // Go\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30cf\u30c3\u30b7\u30e5\u30b3\u30fc\u30c9\u95a2\u6570\u304c\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u305b\u3093\n
        built_in_hash.swift
        let num = 3\nlet hashNum = num.hashValue\n// \u6574\u65703\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f9047044699613009734\n\nlet bol = true\nlet hashBol = bol.hashValue\n// \u30d6\u30fc\u30eb\u5024true\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f-4431640247352757451\n\nlet dec = 3.14159\nlet hashDec = dec.hashValue\n// \u5c0f\u65703.14159\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f-2465384235396674631\n\nlet str = \"Hello \u7b97\u6cd5\"\nlet hashStr = str.hashValue\n// \u6587\u5b57\u5217\"Hello \u7b97\u6cd5\"\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f-7850626797806988787\n\nlet arr = [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")]\nlet hashTup = arr.hashValue\n// \u914d\u5217[AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")]\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f-2308633508154532996\n\nlet obj = ListNode(x: 0)\nlet hashObj = obj.hashValue\n// ListNode\u30aa\u30d6\u30b8\u30a7\u30af\u30c8utils.ListNode\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f-2434780518035996159\n
        built_in_hash.js
        // JavaScript\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30cf\u30c3\u30b7\u30e5\u30b3\u30fc\u30c9\u95a2\u6570\u304c\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u305b\u3093\n
        built_in_hash.ts
        // TypeScript\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30cf\u30c3\u30b7\u30e5\u30b3\u30fc\u30c9\u95a2\u6570\u304c\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u305b\u3093\n
        built_in_hash.dart
        int num = 3;\nint hashNum = num.hashCode;\n// \u6574\u65703\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f34803\n\nbool bol = true;\nint hashBol = bol.hashCode;\n// \u30d6\u30fc\u30eb\u5024true\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f1231\n\ndouble dec = 3.14159;\nint hashDec = dec.hashCode;\n// \u5c0f\u65703.14159\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f2570631074981783\n\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode;\n// \u6587\u5b57\u5217\"Hello \u7b97\u6cd5\"\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f468167534\n\nList arr = [12836, \"\u5c0f\u54c8\"];\nint hashArr = arr.hashCode;\n// \u914d\u5217[12836, \u5c0f\u54c8]\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f976512528\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode;\n// ListNode\u30aa\u30d6\u30b8\u30a7\u30af\u30c8Instance of 'ListNode'\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f1033450432\n
        built_in_hash.rs
        use std::collections::hash_map::DefaultHasher;\nuse std::hash::{Hash, Hasher};\n\nlet num = 3;\nlet mut num_hasher = DefaultHasher::new();\nnum.hash(&mut num_hasher);\nlet hash_num = num_hasher.finish();\n// \u6574\u65703\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f568126464209439262\n\nlet bol = true;\nlet mut bol_hasher = DefaultHasher::new();\nbol.hash(&mut bol_hasher);\nlet hash_bol = bol_hasher.finish();\n// \u30d6\u30fc\u30eb\u5024true\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f4952851536318644461\n\nlet dec: f32 = 3.14159;\nlet mut dec_hasher = DefaultHasher::new();\ndec.to_bits().hash(&mut dec_hasher);\nlet hash_dec = dec_hasher.finish();\n// \u5c0f\u65703.14159\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f2566941990314602357\n\nlet str = \"Hello \u7b97\u6cd5\";\nlet mut str_hasher = DefaultHasher::new();\nstr.hash(&mut str_hasher);\nlet hash_str = str_hasher.finish();\n// \u6587\u5b57\u5217\"Hello \u7b97\u6cd5\"\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f16092673739211250988\n\nlet arr = (&12836, &\"\u5c0f\u54c8\");\nlet mut tup_hasher = DefaultHasher::new();\narr.hash(&mut tup_hasher);\nlet hash_tup = tup_hasher.finish();\n// \u30bf\u30d7\u30eb(12836, \"\u5c0f\u54c8\")\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f1885128010422702749\n\nlet node = ListNode::new(42);\nlet mut hasher = DefaultHasher::new();\nnode.borrow().val.hash(&mut hasher);\nlet hash = hasher.finish();\n// ListNode\u30aa\u30d6\u30b8\u30a7\u30af\u30c8RefCell { value: ListNode { val: 42, next: None } }\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f15387811073369036852\n
        built_in_hash.c
        // C\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30cf\u30c3\u30b7\u30e5\u30b3\u30fc\u30c9\u95a2\u6570\u304c\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u305b\u3093\n
        built_in_hash.kt
        \n

        \u591a\u304f\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u306f\u3001\u4e0d\u5909\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u307f\u304c\u30cf\u30c3\u30b7\u30e5\u8868\u306ekey\u3068\u3057\u3066\u6a5f\u80fd\u3067\u304d\u307e\u3059\u3002\u30ea\u30b9\u30c8\uff08\u52d5\u7684\u914d\u5217\uff09\u3092key\u3068\u3057\u3066\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001\u30ea\u30b9\u30c8\u306e\u5185\u5bb9\u304c\u5909\u66f4\u3055\u308c\u308b\u3068\u3001\u305d\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u3082\u5909\u66f4\u3055\u308c\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3067\u5143\u306evalue\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u306a\u304f\u306a\u308a\u307e\u3059\u3002

        \u30ab\u30b9\u30bf\u30e0\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff08\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u306a\u3069\uff09\u306e\u30e1\u30f3\u30d0\u30fc\u5909\u6570\u306f\u53ef\u5909\u3067\u3059\u304c\u3001\u30cf\u30c3\u30b7\u30e5\u53ef\u80fd\u3067\u3059\u3002\u3053\u308c\u306f\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u304c\u901a\u5e38\u305d\u306e\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u306b\u57fa\u3065\u3044\u3066\u751f\u6210\u3055\u308c\u308b\u305f\u3081\u3067\u3059\u3002\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5185\u5bb9\u304c\u5909\u66f4\u3055\u308c\u3066\u3082\u3001\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u306f\u540c\u3058\u307e\u307e\u306a\u306e\u3067\u3001\u30cf\u30c3\u30b7\u30e5\u5024\u306f\u5909\u66f4\u3055\u308c\u307e\u305b\u3093\u3002

        \u7570\u306a\u308b\u30b3\u30f3\u30bd\u30fc\u30eb\u3067\u51fa\u529b\u3055\u308c\u308b\u30cf\u30c3\u30b7\u30e5\u5024\u304c\u7570\u306a\u308b\u3053\u3068\u306b\u6c17\u3065\u3044\u305f\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u3053\u308c\u306f\u3001Python\u30a4\u30f3\u30bf\u30fc\u30d7\u30ea\u30bf\u30fc\u304c\u8d77\u52d5\u3059\u308b\u305f\u3073\u306b\u6587\u5b57\u5217\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306b\u30e9\u30f3\u30c0\u30e0\u30bd\u30eb\u30c8\u3092\u8ffd\u52a0\u3059\u308b\u305f\u3081\u3067\u3059\u3002\u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u306fHashDoS\u653b\u6483\u3092\u52b9\u679c\u7684\u306b\u9632\u304e\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002

        "},{"location":"chapter_hashing/hash_collision/","title":"6.2 \u00a0 \u30cf\u30c3\u30b7\u30e5\u885d\u7a81","text":"

        \u524d\u7bc0\u3067\u8ff0\u3079\u305f\u3088\u3046\u306b\u3001**\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u5165\u529b\u7a7a\u9593\u306f\u51fa\u529b\u7a7a\u9593\u3088\u308a\u3082\u306f\u308b\u304b\u306b\u5927\u304d\u3044**\u305f\u3081\u3001\u7406\u8ad6\u7684\u306b\u306f\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306f\u907f\u3051\u3089\u308c\u307e\u305b\u3093\u3002\u4f8b\u3048\u3070\u3001\u5165\u529b\u7a7a\u9593\u304c\u3059\u3079\u3066\u306e\u6574\u6570\u3067\u3001\u51fa\u529b\u7a7a\u9593\u304c\u914d\u5217\u5bb9\u91cf\u306e\u30b5\u30a4\u30ba\u306e\u5834\u5408\u3001\u8907\u6570\u306e\u6574\u6570\u304c\u5fc5\u7136\u7684\u306b\u540c\u3058\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3055\u308c\u307e\u3059\u3002

        \u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306f\u8aa4\u3063\u305f\u30af\u30a8\u30ea\u7d50\u679c\u306b\u3064\u306a\u304c\u308a\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u4f7f\u3044\u3084\u3059\u3055\u306b\u6df1\u523b\u306a\u5f71\u97ff\u3092\u4e0e\u3048\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u554f\u984c\u306b\u5bfe\u51e6\u3059\u308b\u305f\u3081\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u304c\u767a\u751f\u3059\u308b\u305f\u3073\u306b\u3001\u885d\u7a81\u304c\u6d88\u3048\u308b\u307e\u3067\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30ea\u30b5\u30a4\u30ba\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002\u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\u975e\u5e38\u306b\u30b7\u30f3\u30d7\u30eb\u3067\u76f4\u63a5\u7684\u3067\u3042\u308a\u3001\u3046\u307e\u304f\u6a5f\u80fd\u3057\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u30c6\u30fc\u30d6\u30eb\u306e\u62e1\u5f35\u306b\u306f\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u79fb\u884c\u3068\u30cf\u30c3\u30b7\u30e5\u30b3\u30fc\u30c9\u306e\u518d\u8a08\u7b97\u304c\u542b\u307e\u308c\u3001\u3053\u308c\u3089\u306f\u9ad8\u30b3\u30b9\u30c8\u3067\u3042\u308b\u305f\u3081\u3001\u975e\u5e38\u306b\u975e\u52b9\u7387\u7684\u306b\u898b\u3048\u307e\u3059\u3002\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u308b\u305f\u3081\u306b\u3001\u4ee5\u4e0b\u306e\u6226\u7565\u3092\u63a1\u7528\u3067\u304d\u307e\u3059\uff1a

        1. **\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u304c\u767a\u751f\u3057\u305f\u5834\u5408\u3067\u3082\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u306e\u691c\u7d22\u304c\u9069\u5207\u306b\u6a5f\u80fd\u3059\u308b**\u3088\u3046\u306b\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u6539\u5584\u3059\u308b\u3002
        2. \u6df1\u523b\u306a\u885d\u7a81\u304c\u89b3\u5bdf\u3055\u308c\u3001\u5fc5\u8981\u306b\u306a\u308b\u524d\u306b\u3001\u62e1\u5f35\u306f\u6700\u5f8c\u306e\u624b\u6bb5\u3068\u3059\u308b\u3002

        \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u69cb\u9020\u3092\u6539\u5584\u3059\u308b\u4e3b\u306a\u65b9\u6cd5\u306f2\u3064\u3042\u308a\u307e\u3059\uff1a\u300c\u9023\u9396\u6cd5\u300d\u3068\u300c\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u300d\u3067\u3059\u3002

        "},{"location":"chapter_hashing/hash_collision/#621","title":"6.2.1 \u00a0 \u9023\u9396\u6cd5","text":"

        \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u8868\u3067\u306f\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u306f1\u3064\u306e\u30ad\u30fc\u5024\u30da\u30a2\u306e\u307f\u3092\u683c\u7d0d\u3067\u304d\u307e\u3059\u3002\u9023\u9396\u6cd5\u306f\u5358\u4e00\u306e\u8981\u7d20\u3092\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u5909\u63db\u3057\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u3068\u3057\u3066\u6271\u3044\u3001\u885d\u7a81\u3059\u308b\u3059\u3079\u3066\u306e\u30ad\u30fc\u5024\u30da\u30a2\u3092\u540c\u3058\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u683c\u7d0d\u3057\u307e\u3059\u3002\u4e0b\u56f3\u306f\u9023\u9396\u6cd5\u3092\u4f7f\u7528\u3057\u305f\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u4f8b\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 6-5 \u00a0 \u9023\u9396\u6cd5\u30cf\u30c3\u30b7\u30e5\u8868

        \u9023\u9396\u6cd5\u3067\u5b9f\u88c5\u3055\u308c\u305f\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u64cd\u4f5c\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5909\u66f4\u3055\u308c\u307e\u3059\uff1a

        • \u8981\u7d20\u306e\u30af\u30a8\u30ea: key\u3092\u5165\u529b\u3057\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u3092\u901a\u3057\u3066\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\u3057\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u306b\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8d70\u67fb\u3057\u3066\u30ad\u30fc\u3092\u6bd4\u8f03\u3057\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u30ad\u30fc\u5024\u30da\u30a2\u3092\u898b\u3064\u3051\u307e\u3059\u3002
        • \u8981\u7d20\u306e\u8ffd\u52a0: \u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u3092\u901a\u3057\u3066\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3001\u30ce\u30fc\u30c9\uff08\u30ad\u30fc\u5024\u30da\u30a2\uff09\u3092\u30ea\u30b9\u30c8\u306b\u8ffd\u52a0\u3057\u307e\u3059\u3002
        • \u8981\u7d20\u306e\u524a\u9664: \u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u7d50\u679c\u306b\u57fa\u3065\u3044\u3066\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30d8\u30c3\u30c9\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8d70\u67fb\u3057\u3066\u30bf\u30fc\u30b2\u30c3\u30c8\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u3066\u524a\u9664\u3057\u307e\u3059\u3002

        \u9023\u9396\u6cd5\u306b\u306f\u4ee5\u4e0b\u306e\u5236\u9650\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u7a7a\u9593\u4f7f\u7528\u91cf\u306e\u5897\u52a0: \u9023\u7d50\u30ea\u30b9\u30c8\u306b\u306f\u30ce\u30fc\u30c9\u30dd\u30a4\u30f3\u30bf\u304c\u542b\u307e\u308c\u3066\u304a\u308a\u3001\u914d\u5217\u3088\u308a\u3082\u591a\u304f\u306e\u30e1\u30e2\u30ea\u7a7a\u9593\u3092\u6d88\u8cbb\u3057\u307e\u3059\u3002
        • \u30af\u30a8\u30ea\u52b9\u7387\u306e\u4f4e\u4e0b: \u5bfe\u5fdc\u3059\u308b\u8981\u7d20\u3092\u898b\u3064\u3051\u308b\u305f\u3081\u306b\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u7dda\u5f62\u8d70\u67fb\u304c\u5fc5\u8981\u306b\u306a\u308b\u305f\u3081\u3067\u3059\u3002

        \u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u306f\u9023\u9396\u6cd5\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u7c21\u5358\u306a\u5b9f\u88c5\u3092\u63d0\u4f9b\u3057\u3001\u6ce8\u610f\u3059\u3079\u304d2\u3064\u306e\u70b9\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u7c21\u5358\u306b\u3059\u308b\u305f\u3081\u306b\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u4ee3\u308f\u308a\u306b\u30ea\u30b9\u30c8\uff08\u52d5\u7684\u914d\u5217\uff09\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u3053\u306e\u8a2d\u5b9a\u3067\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u8868\uff08\u914d\u5217\uff09\u306f\u8907\u6570\u306e\u30d0\u30b1\u30c3\u30c8\u3092\u542b\u307f\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u306f\u30ea\u30b9\u30c8\u3067\u3059\u3002
        • \u3053\u306e\u5b9f\u88c5\u306b\u306f\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30ea\u30b5\u30a4\u30ba\u30e1\u30bd\u30c3\u30c9\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002\u8ca0\u8377\u7387\u304c\\(\\frac{2}{3}\\)\u3092\u8d85\u3048\u308b\u3068\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3092\u5143\u306e\u30b5\u30a4\u30ba\u306e2\u500d\u306b\u62e1\u5f35\u3057\u307e\u3059\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map_chaining.py
        class HashMapChaining:\n    \"\"\"\u30c1\u30a7\u30fc\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\"\"\"\n\n    def __init__(self):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        self.size = 0  # \u30ad\u30fc\u5024\u30da\u30a2\u306e\u6570\n        self.capacity = 4  # \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306e\u5bb9\u91cf\n        self.load_thres = 2.0 / 3.0  # \u62e1\u5f35\u3092\u30c8\u30ea\u30ac\u30fc\u3059\u308b\u8ca0\u8377\u7387\u306e\u95be\u5024\n        self.extend_ratio = 2  # \u62e1\u5f35\u306e\u500d\u6570\n        self.buckets = [[] for _ in range(self.capacity)]  # \u30d0\u30b1\u30c3\u30c8\u914d\u5217\n\n    def hash_func(self, key: int) -> int:\n        \"\"\"\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\"\"\"\n        return key % self.capacity\n\n    def load_factor(self) -> float:\n        \"\"\"\u8ca0\u8377\u7387\"\"\"\n        return self.size / self.capacity\n\n    def get(self, key: int) -> str | None:\n        \"\"\"\u7167\u4f1a\u64cd\u4f5c\"\"\"\n        index = self.hash_func(key)\n        bucket = self.buckets[index]\n        # \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3057\u3001\u30ad\u30fc\u304c\u898b\u3064\u304b\u308c\u3070\u5bfe\u5fdc\u3059\u308b val \u3092\u8fd4\u3059\n        for pair in bucket:\n            if pair.key == key:\n                return pair.val\n        # \u30ad\u30fc\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3001None \u3092\u8fd4\u3059\n        return None\n\n    def put(self, key: int, val: str):\n        \"\"\"\u8ffd\u52a0\u64cd\u4f5c\"\"\"\n        # \u8ca0\u8377\u7387\u304c\u95be\u5024\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u62e1\u5f35\u3092\u5b9f\u884c\n        if self.load_factor() > self.load_thres:\n            self.extend()\n        index = self.hash_func(key)\n        bucket = self.buckets[index]\n        # \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3057\u3001\u6307\u5b9a\u3055\u308c\u305f\u30ad\u30fc\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u5bfe\u5fdc\u3059\u308b val \u3092\u66f4\u65b0\u3057\u3066\u8fd4\u3059\n        for pair in bucket:\n            if pair.key == key:\n                pair.val = val\n                return\n        # \u30ad\u30fc\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u672b\u5c3e\u306b\u8ffd\u52a0\n        pair = Pair(key, val)\n        bucket.append(pair)\n        self.size += 1\n\n    def remove(self, key: int):\n        \"\"\"\u524a\u9664\u64cd\u4f5c\"\"\"\n        index = self.hash_func(key)\n        bucket = self.buckets[index]\n        # \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3057\u3001\u305d\u306e\u4e2d\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2\u3092\u524a\u9664\n        for pair in bucket:\n            if pair.key == key:\n                bucket.remove(pair)\n                self.size -= 1\n                break\n\n    def extend(self):\n        \"\"\"\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u62e1\u5f35\"\"\"\n        # \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u4e00\u6642\u7684\u306b\u4fdd\u5b58\n        buckets = self.buckets\n        # \u62e1\u5f35\u3055\u308c\u305f\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n        self.capacity *= self.extend_ratio\n        self.buckets = [[] for _ in range(self.capacity)]\n        self.size = 0\n        # \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306b\u30ad\u30fc\u5024\u30da\u30a2\u3092\u79fb\u52d5\n        for bucket in buckets:\n            for pair in bucket:\n                self.put(pair.key, pair.val)\n\n    def print(self):\n        \"\"\"\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u51fa\u529b\"\"\"\n        for bucket in self.buckets:\n            res = []\n            for pair in bucket:\n                res.append(str(pair.key) + \" -> \" + pair.val)\n            print(res)\n
        hash_map_chaining.cpp
        /* \u30c1\u30a7\u30a4\u30f3\u6cd5\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb */\nclass HashMapChaining {\n  private:\n    int size;                       // \u30ad\u30fc\u5024\u30da\u30a2\u306e\u6570\n    int capacity;                   // \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306e\u5bb9\u91cf\n    double loadThres;               // \u62e1\u5f35\u3092\u30c8\u30ea\u30ac\u30fc\u3059\u308b\u8ca0\u8377\u7387\u306e\u95be\u5024\n    int extendRatio;                // \u62e1\u5f35\u500d\u7387\n    vector<vector<Pair *>> buckets; // \u30d0\u30b1\u30c3\u30c8\u914d\u5217\n\n  public:\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    HashMapChaining() : size(0), capacity(4), loadThres(2.0 / 3.0), extendRatio(2) {\n        buckets.resize(capacity);\n    }\n\n    /* \u30c7\u30b9\u30c8\u30e9\u30af\u30bf */\n    ~HashMapChaining() {\n        for (auto &bucket : buckets) {\n            for (Pair *pair : bucket) {\n                // \u30e1\u30e2\u30ea\u3092\u89e3\u653e\n                delete pair;\n            }\n        }\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u95a2\u6570 */\n    int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u8ca0\u8377\u7387 */\n    double loadFactor() {\n        return (double)size / (double)capacity;\n    }\n\n    /* \u30af\u30a8\u30ea\u64cd\u4f5c */\n    string get(int key) {\n        int index = hashFunc(key);\n        // \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3001\u30ad\u30fc\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001\u5bfe\u5fdc\u3059\u308bval\u3092\u8fd4\u5374\n        for (Pair *pair : buckets[index]) {\n            if (pair->key == key) {\n                return pair->val;\n            }\n        }\n        // \u30ad\u30fc\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3001\u7a7a\u6587\u5b57\u5217\u3092\u8fd4\u5374\n        return \"\";\n    }\n\n    /* \u8ffd\u52a0\u64cd\u4f5c */\n    void put(int key, string val) {\n        // \u8ca0\u8377\u7387\u304c\u95be\u5024\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u62e1\u5f35\u3092\u5b9f\u884c\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        int index = hashFunc(key);\n        // \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3001\u6307\u5b9a\u30ad\u30fc\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u5bfe\u5fdc\u3059\u308bval\u3092\u66f4\u65b0\u3057\u3066\u8fd4\u5374\n        for (Pair *pair : buckets[index]) {\n            if (pair->key == key) {\n                pair->val = val;\n                return;\n            }\n        }\n        // \u30ad\u30fc\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u672b\u5c3e\u306b\u8ffd\u52a0\n        buckets[index].push_back(new Pair(key, val));\n        size++;\n    }\n\n    /* \u524a\u9664\u64cd\u4f5c */\n    void remove(int key) {\n        int index = hashFunc(key);\n        auto &bucket = buckets[index];\n        // \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u524a\u9664\n        for (int i = 0; i < bucket.size(); i++) {\n            if (bucket[i]->key == key) {\n                Pair *tmp = bucket[i];\n                bucket.erase(bucket.begin() + i); // \u30ad\u30fc\u5024\u30da\u30a2\u3092\u524a\u9664\n                delete tmp;                       // \u30e1\u30e2\u30ea\u3092\u89e3\u653e\n                size--;\n                return;\n            }\n        }\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u62e1\u5f35 */\n    void extend() {\n        // \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u4e00\u6642\u4fdd\u5b58\n        vector<vector<Pair *>> bucketsTmp = buckets;\n        // \u62e1\u5f35\u3055\u308c\u305f\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n        capacity *= extendRatio;\n        buckets.clear();\n        buckets.resize(capacity);\n        size = 0;\n        // \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306b\u30ad\u30fc\u5024\u30da\u30a2\u3092\u79fb\u52d5\n        for (auto &bucket : bucketsTmp) {\n            for (Pair *pair : bucket) {\n                put(pair->key, pair->val);\n                // \u30e1\u30e2\u30ea\u3092\u89e3\u653e\n                delete pair;\n            }\n        }\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u5370\u5237 */\n    void print() {\n        for (auto &bucket : buckets) {\n            cout << \"[\";\n            for (Pair *pair : bucket) {\n                cout << pair->key << \" -> \" << pair->val << \", \";\n            }\n            cout << \"]\\n\";\n        }\n    }\n};\n
        hash_map_chaining.java
        /* \u30c1\u30a7\u30a4\u30f3\u6cd5\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb */\nclass HashMapChaining {\n    int size; // \u30ad\u30fc\u5024\u30da\u30a2\u306e\u6570\n    int capacity; // \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306e\u5bb9\u91cf\n    double loadThres; // \u62e1\u5f35\u3092\u30c8\u30ea\u30ac\u30fc\u3059\u308b\u8ca0\u8377\u7387\u306e\u95be\u5024\n    int extendRatio; // \u62e1\u5f35\u500d\u7387\n    List<List<Pair>> buckets; // \u30d0\u30b1\u30c3\u30c8\u914d\u5217\n\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    public HashMapChaining() {\n        size = 0;\n        capacity = 4;\n        loadThres = 2.0 / 3.0;\n        extendRatio = 2;\n        buckets = new ArrayList<>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.add(new ArrayList<>());\n        }\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u95a2\u6570 */\n    int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u8ca0\u8377\u7387 */\n    double loadFactor() {\n        return (double) size / capacity;\n    }\n\n    /* \u30af\u30a8\u30ea\u64cd\u4f5c */\n    String get(int key) {\n        int index = hashFunc(key);\n        List<Pair> bucket = buckets.get(index);\n        // \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3001\u30ad\u30fc\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u5bfe\u5fdc\u3059\u308bval\u3092\u8fd4\u3059\n        for (Pair pair : bucket) {\n            if (pair.key == key) {\n                return pair.val;\n            }\n        }\n        // \u30ad\u30fc\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3001null\u3092\u8fd4\u3059\n        return null;\n    }\n\n    /* \u8ffd\u52a0\u64cd\u4f5c */\n    void put(int key, String val) {\n        // \u8ca0\u8377\u7387\u304c\u95be\u5024\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u62e1\u5f35\u3092\u5b9f\u884c\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        int index = hashFunc(key);\n        List<Pair> bucket = buckets.get(index);\n        // \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3001\u6307\u5b9a\u3057\u305f\u30ad\u30fc\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u5bfe\u5fdc\u3059\u308bval\u3092\u66f4\u65b0\u3057\u3066\u623b\u308b\n        for (Pair pair : bucket) {\n            if (pair.key == key) {\n                pair.val = val;\n                return;\n            }\n        }\n        // \u30ad\u30fc\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u672b\u5c3e\u306b\u8ffd\u52a0\n        Pair pair = new Pair(key, val);\n        bucket.add(pair);\n        size++;\n    }\n\n    /* \u524a\u9664\u64cd\u4f5c */\n    void remove(int key) {\n        int index = hashFunc(key);\n        List<Pair> bucket = buckets.get(index);\n        // \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3001\u305d\u306e\u4e2d\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2\u3092\u524a\u9664\n        for (Pair pair : bucket) {\n            if (pair.key == key) {\n                bucket.remove(pair);\n                size--;\n                break;\n            }\n        }\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u62e1\u5f35 */\n    void extend() {\n        // \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u4e00\u6642\u7684\u306b\u4fdd\u5b58\n        List<List<Pair>> bucketsTmp = buckets;\n        // \u62e1\u5f35\u3055\u308c\u305f\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n        capacity *= extendRatio;\n        buckets = new ArrayList<>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.add(new ArrayList<>());\n        }\n        size = 0;\n        // \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306b\u30ad\u30fc\u5024\u30da\u30a2\u3092\u79fb\u52d5\n        for (List<Pair> bucket : bucketsTmp) {\n            for (Pair pair : bucket) {\n                put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u5370\u5237 */\n    void print() {\n        for (List<Pair> bucket : buckets) {\n            List<String> res = new ArrayList<>();\n            for (Pair pair : bucket) {\n                res.add(pair.key + \" -> \" + pair.val);\n            }\n            System.out.println(res);\n        }\n    }\n}\n
        hash_map_chaining.cs
        [class]{HashMapChaining}-[func]{}\n
        hash_map_chaining.go
        [class]{hashMapChaining}-[func]{}\n
        hash_map_chaining.swift
        [class]{HashMapChaining}-[func]{}\n
        hash_map_chaining.js
        [class]{HashMapChaining}-[func]{}\n
        hash_map_chaining.ts
        [class]{HashMapChaining}-[func]{}\n
        hash_map_chaining.dart
        [class]{HashMapChaining}-[func]{}\n
        hash_map_chaining.rs
        [class]{HashMapChaining}-[func]{}\n
        hash_map_chaining.c
        [class]{Node}-[func]{}\n\n[class]{HashMapChaining}-[func]{}\n
        hash_map_chaining.kt
        [class]{HashMapChaining}-[func]{}\n
        hash_map_chaining.rb
        [class]{HashMapChaining}-[func]{}\n

        \u9023\u7d50\u30ea\u30b9\u30c8\u304c\u975e\u5e38\u306b\u9577\u3044\u5834\u5408\u3001\u30af\u30a8\u30ea\u52b9\u7387\\(O(n)\\)\u304c\u60aa\u3044\u3053\u3068\u306f\u6ce8\u76ee\u306b\u5024\u3057\u307e\u3059\u3002\u3053\u306e\u5834\u5408\u3001\u30ea\u30b9\u30c8\u3092\u300cAVL\u6728\u300d\u307e\u305f\u306f\u300c\u8d64\u9ed2\u6728\u300d\u306b\u5909\u63db\u3057\u3066\u3001\u30af\u30a8\u30ea\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3092\\(O(\\log n)\\)\u306b\u6700\u9069\u5316\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_hashing/hash_collision/#622","title":"6.2.2 \u00a0 \u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5","text":"

        \u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u306f\u8ffd\u52a0\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u5c0e\u5165\u305b\u305a\u3001\u4ee3\u308f\u308a\u306b\u300c\u8907\u6570\u56de\u30d7\u30ed\u30fc\u30d6\u300d\u3092\u901a\u3057\u3066\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u51e6\u7406\u3057\u307e\u3059\u3002\u30d7\u30ed\u30fc\u30d6\u65b9\u6cd5\u306b\u306f\u4e3b\u306b\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u3001\u4e8c\u6b21\u30d7\u30ed\u30fc\u30d6\u3001\u4e8c\u91cd\u30cf\u30c3\u30b7\u30e5\u304c\u3042\u308a\u307e\u3059\u3002

        \u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u3092\u4f8b\u306b\u3057\u3066\u3001\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u7d39\u4ecb\u3057\u307e\u3057\u3087\u3046\u3002

        "},{"location":"chapter_hashing/hash_collision/#1","title":"1. \u00a0 \u7dda\u5f62\u30d7\u30ed\u30fc\u30d6","text":"

        \u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u306f\u56fa\u5b9a\u30b9\u30c6\u30c3\u30d7\u306e\u7dda\u5f62\u691c\u7d22\u3092\u30d7\u30ed\u30fc\u30d6\u306b\u4f7f\u7528\u3057\u3001\u901a\u5e38\u306e\u30cf\u30c3\u30b7\u30e5\u8868\u3068\u306f\u7570\u306a\u308a\u307e\u3059\u3002

        • \u8981\u7d20\u306e\u633f\u5165: \u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97\u3057\u307e\u3059\u3002\u30d0\u30b1\u30c3\u30c8\u306b\u65e2\u306b\u8981\u7d20\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u885d\u7a81\u4f4d\u7f6e\u304b\u3089\u7dda\u5f62\u306b\u524d\u65b9\u306b\u8d70\u67fb\u3057\uff08\u901a\u5e38\u30b9\u30c6\u30c3\u30d7\u30b5\u30a4\u30ba\u306f\\(1\\)\uff09\u3001\u7a7a\u306e\u30d0\u30b1\u30c3\u30c8\u304c\u898b\u3064\u304b\u308b\u307e\u3067\u9032\u307f\u3001\u8981\u7d20\u3092\u633f\u5165\u3057\u307e\u3059\u3002
        • \u8981\u7d20\u306e\u691c\u7d22: \u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u540c\u3058\u30b9\u30c6\u30c3\u30d7\u30b5\u30a4\u30ba\u3092\u4f7f\u7528\u3057\u3066\u7dda\u5f62\u306b\u524d\u65b9\u306b\u8d70\u67fb\u3057\u3001\u5bfe\u5fdc\u3059\u308b\u8981\u7d20\u304c\u898b\u3064\u304b\u3063\u305f\u3089value\u3092\u8fd4\u3057\u307e\u3059\u3002\u7a7a\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u304c\u30cf\u30c3\u30b7\u30e5\u8868\u306b\u306a\u3044\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u305f\u3081\u3001None\u3092\u8fd4\u3057\u307e\u3059\u3002

        \u4e0b\u56f3\u306f\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\uff08\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\uff09\u30cf\u30c3\u30b7\u30e5\u8868\u306b\u304a\u3051\u308b\u30ad\u30fc\u5024\u30da\u30a2\u306e\u5206\u5e03\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306b\u3088\u308b\u3068\u3001\u4e0b\u4e8c\u6841\u304c\u540c\u3058\u30ad\u30fc\u306f\u540c\u3058\u30d0\u30b1\u30c3\u30c8\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3055\u308c\u307e\u3059\u3002\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u3092\u901a\u3057\u3066\u3001\u305d\u308c\u3089\u306f\u305d\u306e\u30d0\u30b1\u30c3\u30c8\u3068\u305d\u306e\u4e0b\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u9806\u6b21\u683c\u7d0d\u3055\u308c\u307e\u3059\u3002

        \u56f3 6-6 \u00a0 \u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\uff08\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\uff09\u30cf\u30c3\u30b7\u30e5\u8868\u306b\u304a\u3051\u308b\u30ad\u30fc\u5024\u30da\u30a2\u306e\u5206\u5e03

        \u3057\u304b\u3057\u3001\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u306f\u300c\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u300d\u3092\u4f5c\u308a\u3084\u3059\u3044\u50be\u5411\u304c\u3042\u308a\u307e\u3059\u3002\u5177\u4f53\u7684\u306b\u306f\u3001\u914d\u5217\u5185\u306e\u9023\u7d9a\u7684\u306b\u5360\u6709\u3055\u308c\u305f\u4f4d\u7f6e\u304c\u9577\u3044\u307b\u3069\u3001\u3053\u308c\u3089\u306e\u9023\u7d9a\u3057\u305f\u4f4d\u7f6e\u3067\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u304c\u767a\u751f\u3059\u308b\u78ba\u7387\u304c\u9ad8\u304f\u306a\u308a\u3001\u305d\u306e\u4f4d\u7f6e\u3067\u306e\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u306e\u6210\u9577\u3092\u3055\u3089\u306b\u4fc3\u9032\u3057\u3001\u60aa\u5faa\u74b0\u3092\u5f62\u6210\u3057\u3001\u6700\u7d42\u7684\u306b\u633f\u5165\u3001\u524a\u9664\u3001\u30af\u30a8\u30ea\u3001\u66f4\u65b0\u64cd\u4f5c\u306e\u52b9\u7387\u4f4e\u4e0b\u306b\u3064\u306a\u304c\u308a\u307e\u3059\u3002

        **\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u30cf\u30c3\u30b7\u30e5\u8868\u3067\u306f\u8981\u7d20\u3092\u76f4\u63a5\u524a\u9664\u3067\u304d\u306a\u3044**\u3053\u3068\u306b\u6ce8\u610f\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002\u8981\u7d20\u3092\u524a\u9664\u3059\u308b\u3068\u3001\u914d\u5217\u306b\u7a7a\u306e\u30d0\u30b1\u30c3\u30c8None\u304c\u4f5c\u6210\u3055\u308c\u307e\u3059\u3002\u8981\u7d20\u3092\u691c\u7d22\u3059\u308b\u969b\u3001\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u304c\u3053\u306e\u7a7a\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u906d\u9047\u3059\u308b\u3068\u623b\u3063\u3066\u3057\u307e\u3044\u3001\u3053\u306e\u30d0\u30b1\u30c3\u30c8\u306e\u4e0b\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u306a\u304f\u306a\u308a\u307e\u3059\u3002\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u3053\u308c\u3089\u306e\u8981\u7d20\u304c\u5b58\u5728\u3057\u306a\u3044\u3068\u8aa4\u3063\u3066\u4eee\u5b9a\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3068\u304a\u308a\u3067\u3059\u3002

        \u56f3 6-7 \u00a0 \u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u3067\u306e\u524a\u9664\u306b\u3088\u308b\u30af\u30a8\u30ea\u554f\u984c

        \u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3001\u9045\u5ef6\u524a\u9664\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u63a1\u7528\u3067\u304d\u307e\u3059\uff1a\u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u8981\u7d20\u3092\u76f4\u63a5\u524a\u9664\u3059\u308b\u4ee3\u308f\u308a\u306b\u3001\u5b9a\u6570TOMBSTONE\u3092\u4f7f\u7528\u3057\u3066\u30d0\u30b1\u30c3\u30c8\u3092\u30de\u30fc\u30af\u3057\u307e\u3059\u3002\u3053\u306e\u30e1\u30ab\u30cb\u30ba\u30e0\u3067\u306f\u3001None\u3068TOMBSTONE\u306e\u4e21\u65b9\u304c\u7a7a\u306e\u30d0\u30b1\u30c3\u30c8\u3092\u8868\u3057\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u4fdd\u6301\u3067\u304d\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u304cTOMBSTONE\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u305d\u306e\u4e0b\u306b\u307e\u3060\u30ad\u30fc\u5024\u30da\u30a2\u304c\u3042\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3001\u8d70\u67fb\u3092\u7d9a\u3051\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u3057\u304b\u3057\u3001\u9045\u5ef6\u524a\u9664\u306f\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u6027\u80fd\u52a3\u5316\u3092\u52a0\u901f\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u524a\u9664\u64cd\u4f5c\u306e\u305f\u3073\u306b\u524a\u9664\u30de\u30fc\u30af\u304c\u751f\u6210\u3055\u308c\u3001TOMBSTONE\u304c\u5897\u52a0\u3059\u308b\u3068\u3001\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u304c\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u3092\u898b\u3064\u3051\u308b\u305f\u3081\u306b\u8907\u6570\u306eTOMBSTONE\u3092\u30b9\u30ad\u30c3\u30d7\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3001\u691c\u7d22\u6642\u9593\u3082\u5897\u52a0\u3057\u307e\u3059\u3002

        \u3053\u308c\u306b\u5bfe\u51e6\u3059\u308b\u305f\u3081\u306b\u3001\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u4e2d\u306b\u6700\u521d\u306b\u906d\u9047\u3057\u305fTOMBSTONE\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a18\u9332\u3057\u3001\u691c\u7d22\u3055\u308c\u305f\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u3068\u305d\u306eTOMBSTONE\u306e\u4f4d\u7f6e\u3092\u4ea4\u63db\u3059\u308b\u3053\u3068\u3092\u691c\u8a0e\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3053\u308c\u3092\u884c\u3046\u5229\u70b9\u306f\u3001\u8981\u7d20\u304c\u30af\u30a8\u30ea\u307e\u305f\u306f\u8ffd\u52a0\u3055\u308c\u308b\u305f\u3073\u306b\u3001\u8981\u7d20\u304c\u305d\u306e\u7406\u60f3\u7684\u306a\u4f4d\u7f6e\uff08\u30d7\u30ed\u30fc\u30d6\u306e\u958b\u59cb\u70b9\uff09\u306b\u3088\u308a\u8fd1\u3044\u30d0\u30b1\u30c3\u30c8\u306b\u79fb\u52d5\u3055\u308c\u3001\u30af\u30a8\u30ea\u52b9\u7387\u304c\u6700\u9069\u5316\u3055\u308c\u308b\u3053\u3068\u3067\u3059\u3002

        \u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u306f\u3001\u9045\u5ef6\u524a\u9664\u3092\u4f7f\u7528\u3057\u305f\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\uff08\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\uff09\u30cf\u30c3\u30b7\u30e5\u8868\u3092\u5b9f\u88c5\u3057\u3066\u3044\u307e\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u7a7a\u9593\u3092\u3088\u308a\u6709\u52b9\u306b\u6d3b\u7528\u3059\u308b\u305f\u3081\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3092\u300c\u5faa\u74b0\u914d\u5217\u300d\u3068\u3057\u3066\u6271\u3044\u307e\u3059\u3002\u914d\u5217\u306e\u7d42\u308f\u308a\u3092\u8d85\u3048\u308b\u3068\u3001\u6700\u521d\u306b\u623b\u3063\u3066\u8d70\u67fb\u3092\u7d9a\u3051\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map_open_addressing.py
        class HashMapOpenAddressing:\n    \"\"\"\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\"\"\"\n\n    def __init__(self):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        self.size = 0  # \u30ad\u30fc\u5024\u30da\u30a2\u306e\u6570\n        self.capacity = 4  # \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306e\u5bb9\u91cf\n        self.load_thres = 2.0 / 3.0  # \u62e1\u5f35\u3092\u30c8\u30ea\u30ac\u30fc\u3059\u308b\u8ca0\u8377\u7387\u306e\u95be\u5024\n        self.extend_ratio = 2  # \u62e1\u5f35\u306e\u500d\u6570\n        self.buckets: list[Pair | None] = [None] * self.capacity  # \u30d0\u30b1\u30c3\u30c8\u914d\u5217\n        self.TOMBSTONE = Pair(-1, \"-1\")  # \u524a\u9664\u30de\u30fc\u30af\n\n    def hash_func(self, key: int) -> int:\n        \"\"\"\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\"\"\"\n        return key % self.capacity\n\n    def load_factor(self) -> float:\n        \"\"\"\u8ca0\u8377\u7387\"\"\"\n        return self.size / self.capacity\n\n    def find_bucket(self, key: int) -> int:\n        \"\"\"key \u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22\"\"\"\n        index = self.hash_func(key)\n        first_tombstone = -1\n        # \u7dda\u5f62\u63a2\u67fb\u3001\u7a7a\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u906d\u9047\u3057\u305f\u3089\u30d6\u30ec\u30fc\u30af\n        while self.buckets[index] is not None:\n            # \u30ad\u30fc\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n            if self.buckets[index].key == key:\n                # \u524a\u9664\u30de\u30fc\u30af\u304c\u4ee5\u524d\u306b\u906d\u9047\u3057\u3066\u3044\u305f\u5834\u5408\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u79fb\u52d5\n                if first_tombstone != -1:\n                    self.buckets[first_tombstone] = self.buckets[index]\n                    self.buckets[index] = self.TOMBSTONE\n                    return first_tombstone  # \u79fb\u52d5\u3055\u308c\u305f\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n                return index  # \u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n            # \u6700\u521d\u306b\u906d\u9047\u3057\u305f\u524a\u9664\u30de\u30fc\u30af\u3092\u8a18\u9332\n            if first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE:\n                first_tombstone = index\n            # \u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97\u3001\u672b\u5c3e\u3092\u8d85\u3048\u305f\u5834\u5408\u306f\u5148\u982d\u306b\u623b\u308b\n            index = (index + 1) % self.capacity\n        # \u30ad\u30fc\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001\u633f\u5165\u30dd\u30a4\u30f3\u30c8\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n        return index if first_tombstone == -1 else first_tombstone\n\n    def get(self, key: int) -> str:\n        \"\"\"\u7167\u4f1a\u64cd\u4f5c\"\"\"\n        # key \u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22\n        index = self.find_bucket(key)\n        # \u30ad\u30fc\u5024\u30da\u30a2\u304c\u898b\u3064\u304b\u308c\u3070\u3001\u5bfe\u5fdc\u3059\u308b val \u3092\u8fd4\u3059\n        if self.buckets[index] not in [None, self.TOMBSTONE]:\n            return self.buckets[index].val\n        # \u30ad\u30fc\u5024\u30da\u30a2\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001None \u3092\u8fd4\u3059\n        return None\n\n    def put(self, key: int, val: str):\n        \"\"\"\u8ffd\u52a0\u64cd\u4f5c\"\"\"\n        # \u8ca0\u8377\u7387\u304c\u95be\u5024\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u62e1\u5f35\u3092\u5b9f\u884c\n        if self.load_factor() > self.load_thres:\n            self.extend()\n        # key \u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22\n        index = self.find_bucket(key)\n        # \u30ad\u30fc\u5024\u30da\u30a2\u304c\u898b\u3064\u304b\u308c\u3070\u3001val \u3092\u4e0a\u66f8\u304d\u3057\u3066\u8fd4\u3059\n        if self.buckets[index] not in [None, self.TOMBSTONE]:\n            self.buckets[index].val = val\n            return\n        # \u30ad\u30fc\u5024\u30da\u30a2\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u8ffd\u52a0\n        self.buckets[index] = Pair(key, val)\n        self.size += 1\n\n    def remove(self, key: int):\n        \"\"\"\u524a\u9664\u64cd\u4f5c\"\"\"\n        # key \u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22\n        index = self.find_bucket(key)\n        # \u30ad\u30fc\u5024\u30da\u30a2\u304c\u898b\u3064\u304b\u308c\u3070\u3001\u524a\u9664\u30de\u30fc\u30af\u3067\u8986\u3046\n        if self.buckets[index] not in [None, self.TOMBSTONE]:\n            self.buckets[index] = self.TOMBSTONE\n            self.size -= 1\n\n    def extend(self):\n        \"\"\"\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u62e1\u5f35\"\"\"\n        # \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u4e00\u6642\u7684\u306b\u4fdd\u5b58\n        buckets_tmp = self.buckets\n        # \u62e1\u5f35\u3055\u308c\u305f\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n        self.capacity *= self.extend_ratio\n        self.buckets = [None] * self.capacity\n        self.size = 0\n        # \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306b\u30ad\u30fc\u5024\u30da\u30a2\u3092\u79fb\u52d5\n        for pair in buckets_tmp:\n            if pair not in [None, self.TOMBSTONE]:\n                self.put(pair.key, pair.val)\n\n    def print(self):\n        \"\"\"\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u51fa\u529b\"\"\"\n        for pair in self.buckets:\n            if pair is None:\n                print(\"None\")\n            elif pair is self.TOMBSTONE:\n                print(\"TOMBSTONE\")\n            else:\n                print(pair.key, \"->\", pair.val)\n
        hash_map_open_addressing.cpp
        /* \u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb */\nclass HashMapOpenAddressing {\n  private:\n    int size;                             // \u30ad\u30fc\u5024\u30da\u30a2\u306e\u6570\n    int capacity = 4;                     // \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306e\u5bb9\u91cf\n    const double loadThres = 2.0 / 3.0;     // \u62e1\u5f35\u3092\u30c8\u30ea\u30ac\u30fc\u3059\u308b\u8ca0\u8377\u7387\u306e\u95be\u5024\n    const int extendRatio = 2;            // \u62e1\u5f35\u500d\u7387\n    vector<Pair *> buckets;               // \u30d0\u30b1\u30c3\u30c8\u914d\u5217\n    Pair *TOMBSTONE = new Pair(-1, \"-1\"); // \u524a\u9664\u30de\u30fc\u30af\n\n  public:\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    HashMapOpenAddressing() : size(0), buckets(capacity, nullptr) {\n    }\n\n    /* \u30c7\u30b9\u30c8\u30e9\u30af\u30bf */\n    ~HashMapOpenAddressing() {\n        for (Pair *pair : buckets) {\n            if (pair != nullptr && pair != TOMBSTONE) {\n                delete pair;\n            }\n        }\n        delete TOMBSTONE;\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u95a2\u6570 */\n    int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u8ca0\u8377\u7387 */\n    double loadFactor() {\n        return (double)size / capacity;\n    }\n\n    /* key\u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22 */\n    int findBucket(int key) {\n        int index = hashFunc(key);\n        int firstTombstone = -1;\n        // \u7dda\u5f62\u63a2\u67fb\u3001\u7a7a\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u906d\u9047\u3057\u305f\u3089\u4e2d\u65ad\n        while (buckets[index] != nullptr) {\n            // key\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u5374\n            if (buckets[index]->key == key) {\n                // \u4ee5\u524d\u306b\u524a\u9664\u30de\u30fc\u30af\u306b\u906d\u9047\u3057\u3066\u3044\u305f\u5834\u5408\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u79fb\u52d5\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index];\n                    buckets[index] = TOMBSTONE;\n                    return firstTombstone; // \u79fb\u52d5\u3055\u308c\u305f\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u5374\n                }\n                return index; // \u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u5374\n            }\n            // \u6700\u521d\u306b\u906d\u9047\u3057\u305f\u524a\u9664\u30de\u30fc\u30af\u3092\u8a18\u9332\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index;\n            }\n            // \u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97\u3001\u672b\u5c3e\u3092\u8d85\u3048\u305f\u5834\u5408\u306f\u5148\u982d\u306b\u623b\u308b\n            index = (index + 1) % capacity;\n        }\n        // key\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001\u633f\u5165\u30dd\u30a4\u30f3\u30c8\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u5374\n        return firstTombstone == -1 ? index : firstTombstone;\n    }\n\n    /* \u30af\u30a8\u30ea\u64cd\u4f5c */\n    string get(int key) {\n        // key\u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22\n        int index = findBucket(key);\n        // \u30ad\u30fc\u5024\u30da\u30a2\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001\u5bfe\u5fdc\u3059\u308bval\u3092\u8fd4\u5374\n        if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n            return buckets[index]->val;\n        }\n        // \u30ad\u30fc\u5024\u30da\u30a2\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001\u7a7a\u6587\u5b57\u5217\u3092\u8fd4\u5374\n        return \"\";\n    }\n\n    /* \u8ffd\u52a0\u64cd\u4f5c */\n    void put(int key, string val) {\n        // \u8ca0\u8377\u7387\u304c\u95be\u5024\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u62e1\u5f35\u3092\u5b9f\u884c\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        // key\u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22\n        int index = findBucket(key);\n        // \u30ad\u30fc\u5024\u30da\u30a2\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001val\u3092\u4e0a\u66f8\u304d\u3057\u3066\u8fd4\u5374\n        if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n            buckets[index]->val = val;\n            return;\n        }\n        // \u30ad\u30fc\u5024\u30da\u30a2\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u8ffd\u52a0\n        buckets[index] = new Pair(key, val);\n        size++;\n    }\n\n    /* \u524a\u9664\u64cd\u4f5c */\n    void remove(int key) {\n        // key\u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22\n        int index = findBucket(key);\n        // \u30ad\u30fc\u5024\u30da\u30a2\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001\u524a\u9664\u30de\u30fc\u30af\u3067\u8986\u3046\n        if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n            delete buckets[index];\n            buckets[index] = TOMBSTONE;\n            size--;\n        }\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u62e1\u5f35 */\n    void extend() {\n        // \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u4e00\u6642\u4fdd\u5b58\n        vector<Pair *> bucketsTmp = buckets;\n        // \u62e1\u5f35\u3055\u308c\u305f\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n        capacity *= extendRatio;\n        buckets = vector<Pair *>(capacity, nullptr);\n        size = 0;\n        // \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306b\u30ad\u30fc\u5024\u30da\u30a2\u3092\u79fb\u52d5\n        for (Pair *pair : bucketsTmp) {\n            if (pair != nullptr && pair != TOMBSTONE) {\n                put(pair->key, pair->val);\n                delete pair;\n            }\n        }\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u5370\u5237 */\n    void print() {\n        for (Pair *pair : buckets) {\n            if (pair == nullptr) {\n                cout << \"nullptr\" << endl;\n            } else if (pair == TOMBSTONE) {\n                cout << \"TOMBSTONE\" << endl;\n            } else {\n                cout << pair->key << \" -> \" << pair->val << endl;\n            }\n        }\n    }\n};\n
        hash_map_open_addressing.java
        /* \u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb */\nclass HashMapOpenAddressing {\n    private int size; // \u30ad\u30fc\u5024\u30da\u30a2\u306e\u6570\n    private int capacity = 4; // \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306e\u5bb9\u91cf\n    private final double loadThres = 2.0 / 3.0; // \u62e1\u5f35\u3092\u30c8\u30ea\u30ac\u30fc\u3059\u308b\u8ca0\u8377\u7387\u306e\u95be\u5024\n    private final int extendRatio = 2; // \u62e1\u5f35\u500d\u7387\n    private Pair[] buckets; // \u30d0\u30b1\u30c3\u30c8\u914d\u5217\n    private final Pair TOMBSTONE = new Pair(-1, \"-1\"); // \u524a\u9664\u30de\u30fc\u30af\n\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    public HashMapOpenAddressing() {\n        size = 0;\n        buckets = new Pair[capacity];\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u95a2\u6570 */\n    private int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u8ca0\u8377\u7387 */\n    private double loadFactor() {\n        return (double) size / capacity;\n    }\n\n    /* key\u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22 */\n    private int findBucket(int key) {\n        int index = hashFunc(key);\n        int firstTombstone = -1;\n        // \u7dda\u5f62\u63a2\u67fb\u3001\u7a7a\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u906d\u9047\u3057\u305f\u3089\u7d42\u4e86\n        while (buckets[index] != null) {\n            // key\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n            if (buckets[index].key == key) {\n                // \u4ee5\u524d\u306b\u524a\u9664\u30de\u30fc\u30af\u306b\u906d\u9047\u3057\u3066\u3044\u305f\u5834\u5408\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u79fb\u52d5\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index];\n                    buckets[index] = TOMBSTONE;\n                    return firstTombstone; // \u79fb\u52d5\u5f8c\u306e\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n                }\n                return index; // \u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n            }\n            // \u6700\u521d\u306b\u906d\u9047\u3057\u305f\u524a\u9664\u30de\u30fc\u30af\u3092\u8a18\u9332\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index;\n            }\n            // \u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97\u3001\u672b\u5c3e\u3092\u8d85\u3048\u305f\u5834\u5408\u306f\u5148\u982d\u306b\u623b\u308b\n            index = (index + 1) % capacity;\n        }\n        // key\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001\u633f\u5165\u30dd\u30a4\u30f3\u30c8\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n        return firstTombstone == -1 ? index : firstTombstone;\n    }\n\n    /* \u30af\u30a8\u30ea\u64cd\u4f5c */\n    public String get(int key) {\n        // key\u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22\n        int index = findBucket(key);\n        // \u30ad\u30fc\u5024\u30da\u30a2\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001\u5bfe\u5fdc\u3059\u308bval\u3092\u8fd4\u3059\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            return buckets[index].val;\n        }\n        // \u30ad\u30fc\u5024\u30da\u30a2\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001null\u3092\u8fd4\u3059\n        return null;\n    }\n\n    /* \u8ffd\u52a0\u64cd\u4f5c */\n    public void put(int key, String val) {\n        // \u8ca0\u8377\u7387\u304c\u95be\u5024\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u62e1\u5f35\u3092\u5b9f\u884c\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        // key\u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22\n        int index = findBucket(key);\n        // \u30ad\u30fc\u5024\u30da\u30a2\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001val\u3092\u4e0a\u66f8\u304d\u3057\u3066\u623b\u308b\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index].val = val;\n            return;\n        }\n        // \u30ad\u30fc\u5024\u30da\u30a2\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u8ffd\u52a0\n        buckets[index] = new Pair(key, val);\n        size++;\n    }\n\n    /* \u524a\u9664\u64cd\u4f5c */\n    public void remove(int key) {\n        // key\u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22\n        int index = findBucket(key);\n        // \u30ad\u30fc\u5024\u30da\u30a2\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001\u524a\u9664\u30de\u30fc\u30af\u3067\u8986\u3046\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index] = TOMBSTONE;\n            size--;\n        }\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u62e1\u5f35 */\n    private void extend() {\n        // \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u4e00\u6642\u7684\u306b\u4fdd\u5b58\n        Pair[] bucketsTmp = buckets;\n        // \u62e1\u5f35\u3055\u308c\u305f\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n        capacity *= extendRatio;\n        buckets = new Pair[capacity];\n        size = 0;\n        // \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306b\u30ad\u30fc\u5024\u30da\u30a2\u3092\u79fb\u52d5\n        for (Pair pair : bucketsTmp) {\n            if (pair != null && pair != TOMBSTONE) {\n                put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u5370\u5237 */\n    public void print() {\n        for (Pair pair : buckets) {\n            if (pair == null) {\n                System.out.println(\"null\");\n            } else if (pair == TOMBSTONE) {\n                System.out.println(\"TOMBSTONE\");\n            } else {\n                System.out.println(pair.key + \" -> \" + pair.val);\n            }\n        }\n    }\n}\n
        hash_map_open_addressing.cs
        [class]{HashMapOpenAddressing}-[func]{}\n
        hash_map_open_addressing.go
        [class]{hashMapOpenAddressing}-[func]{}\n
        hash_map_open_addressing.swift
        [class]{HashMapOpenAddressing}-[func]{}\n
        hash_map_open_addressing.js
        [class]{HashMapOpenAddressing}-[func]{}\n
        hash_map_open_addressing.ts
        [class]{HashMapOpenAddressing}-[func]{}\n
        hash_map_open_addressing.dart
        [class]{HashMapOpenAddressing}-[func]{}\n
        hash_map_open_addressing.rs
        [class]{HashMapOpenAddressing}-[func]{}\n
        hash_map_open_addressing.c
        [class]{HashMapOpenAddressing}-[func]{}\n
        hash_map_open_addressing.kt
        [class]{HashMapOpenAddressing}-[func]{}\n
        hash_map_open_addressing.rb
        [class]{HashMapOpenAddressing}-[func]{}\n
        "},{"location":"chapter_hashing/hash_collision/#2","title":"2. \u00a0 \u4e8c\u6b21\u30d7\u30ed\u30fc\u30d6","text":"

        \u4e8c\u6b21\u30d7\u30ed\u30fc\u30d6\u306f\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u306b\u4f3c\u3066\u304a\u308a\u3001\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u306e\u4e00\u822c\u7684\u306a\u6226\u7565\u306e1\u3064\u3067\u3059\u3002\u885d\u7a81\u304c\u767a\u751f\u3057\u305f\u5834\u5408\u3001\u4e8c\u6b21\u30d7\u30ed\u30fc\u30d6\u306f\u5358\u7d14\u306b\u56fa\u5b9a\u30b9\u30c6\u30c3\u30d7\u6570\u3092\u30b9\u30ad\u30c3\u30d7\u3059\u308b\u306e\u3067\u306f\u306a\u304f\u3001\u300c\u30d7\u30ed\u30fc\u30d6\u56de\u6570\u306e\u4e8c\u4e57\u300d\u306b\u7b49\u3057\u3044\u30b9\u30c6\u30c3\u30d7\u6570\u3001\u3064\u307e\u308a\\(1, 4, 9, \\dots\\)\u30b9\u30c6\u30c3\u30d7\u3092\u30b9\u30ad\u30c3\u30d7\u3057\u307e\u3059\u3002

        \u4e8c\u6b21\u30d7\u30ed\u30fc\u30d6\u306b\u306f\u4ee5\u4e0b\u306e\u5229\u70b9\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u4e8c\u6b21\u30d7\u30ed\u30fc\u30d6\u306f\u3001\u30d7\u30ed\u30fc\u30d6\u56de\u6570\u306e\u4e8c\u4e57\u306e\u8ddd\u96e2\u3092\u30b9\u30ad\u30c3\u30d7\u3059\u308b\u3053\u3068\u3067\u3001\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u306e\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u52b9\u679c\u3092\u8efd\u6e1b\u3057\u3088\u3046\u3068\u3057\u307e\u3059\u3002
        • \u4e8c\u6b21\u30d7\u30ed\u30fc\u30d6\u306f\u3088\u308a\u5927\u304d\u306a\u8ddd\u96e2\u3092\u30b9\u30ad\u30c3\u30d7\u3057\u3066\u7a7a\u306e\u4f4d\u7f6e\u3092\u898b\u3064\u3051\u3001\u30c7\u30fc\u30bf\u3092\u3088\u308a\u5747\u7b49\u306b\u5206\u6563\u3059\u308b\u306e\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002

        \u3057\u304b\u3057\u3001\u4e8c\u6b21\u30d7\u30ed\u30fc\u30d6\u306f\u5b8c\u74a7\u3067\u306f\u3042\u308a\u307e\u305b\u3093\uff1a

        • \u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u306f\u4f9d\u7136\u3068\u3057\u3066\u5b58\u5728\u3057\u3001\u3064\u307e\u308a\u4e00\u90e8\u306e\u4f4d\u7f6e\u306f\u4ed6\u306e\u4f4d\u7f6e\u3088\u308a\u3082\u5360\u6709\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u9ad8\u3044\u3067\u3059\u3002
        • \u4e8c\u4e57\u306e\u6210\u9577\u306b\u3088\u308a\u3001\u4e8c\u6b21\u30d7\u30ed\u30fc\u30d6\u306f\u30cf\u30c3\u30b7\u30e5\u8868\u5168\u4f53\u3092\u30d7\u30ed\u30fc\u30d6\u3067\u304d\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306b\u7a7a\u306e\u30d0\u30b1\u30c3\u30c8\u304c\u3042\u3063\u3066\u3082\u3001\u4e8c\u6b21\u30d7\u30ed\u30fc\u30d6\u304c\u30a2\u30af\u30bb\u30b9\u3067\u304d\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        "},{"location":"chapter_hashing/hash_collision/#3","title":"3. \u00a0 \u4e8c\u91cd\u30cf\u30c3\u30b7\u30e5","text":"

        \u540d\u524d\u304c\u793a\u3059\u3088\u3046\u306b\u3001\u4e8c\u91cd\u30cf\u30c3\u30b7\u30e5\u6cd5\u306f\u8907\u6570\u306e\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\\(f_1(x)\\)\u3001\\(f_2(x)\\)\u3001\\(f_3(x)\\)\u3001\\(\\dots\\)\u3092\u30d7\u30ed\u30fc\u30d6\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002

        • \u8981\u7d20\u306e\u633f\u5165: \u30cf\u30c3\u30b7\u30e5\u95a2\u6570\\(f_1(x)\\)\u304c\u885d\u7a81\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\\(f_2(x)\\)\u3092\u8a66\u3057\u3001\u4ee5\u4e0b\u540c\u69d8\u306b\u3001\u7a7a\u306e\u4f4d\u7f6e\u304c\u898b\u3064\u304b\u3063\u3066\u8981\u7d20\u304c\u633f\u5165\u3055\u308c\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002
        • \u8981\u7d20\u306e\u691c\u7d22: \u540c\u3058\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u9806\u5e8f\u3067\u691c\u7d22\u3057\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u304c\u898b\u3064\u304b\u3063\u3066\u8fd4\u3055\u308c\u308b\u307e\u3067\u3001\u307e\u305f\u306f\u7a7a\u306e\u4f4d\u7f6e\u306b\u906d\u9047\u3059\u308b\u304b\u3059\u3079\u3066\u306e\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u304c\u8a66\u3055\u308c\u308b\u307e\u3067\u7d9a\u3051\u3001\u8981\u7d20\u304c\u30cf\u30c3\u30b7\u30e5\u8868\u306b\u306a\u3044\u3053\u3068\u3092\u793a\u3057\u3001None\u3092\u8fd4\u3057\u307e\u3059\u3002

        \u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u3068\u6bd4\u8f03\u3057\u3066\u3001\u4e8c\u91cd\u30cf\u30c3\u30b7\u30e5\u6cd5\u306f\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u304c\u8d77\u3053\u308a\u306b\u304f\u3044\u3067\u3059\u304c\u3001\u8907\u6570\u306e\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306f\u8ffd\u52a0\u306e\u8a08\u7b97\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u3092\u5c0e\u5165\u3057\u307e\u3059\u3002

        Tip

        \u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\uff08\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u3001\u4e8c\u6b21\u30d7\u30ed\u30fc\u30d6\u3001\u4e8c\u91cd\u30cf\u30c3\u30b7\u30e5\uff09\u30cf\u30c3\u30b7\u30e5\u8868\u306f\u3059\u3079\u3066\u300c\u8981\u7d20\u3092\u76f4\u63a5\u524a\u9664\u3067\u304d\u306a\u3044\u300d\u3068\u3044\u3046\u554f\u984c\u304c\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        "},{"location":"chapter_hashing/hash_collision/#623","title":"6.2.3 \u00a0 \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u9078\u629e","text":"

        \u7570\u306a\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306f\u7570\u306a\u308b\u30cf\u30c3\u30b7\u30e5\u8868\u5b9f\u88c5\u6226\u7565\u3092\u63a1\u7528\u3057\u3066\u3044\u307e\u3059\u3002\u4ee5\u4e0b\u306b\u3044\u304f\u3064\u304b\u306e\u4f8b\u3092\u793a\u3057\u307e\u3059\uff1a

        • Python\u306f\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002dict\u8f9e\u66f8\u306f\u30d7\u30ed\u30fc\u30d6\u306b\u7591\u4f3c\u4e71\u6570\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • Java\u306f\u9023\u9396\u6cd5\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002JDK 1.8\u4ee5\u964d\u3001HashMap\u306e\u914d\u5217\u9577\u304c64\u306b\u9054\u3057\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u9577\u3055\u304c8\u306b\u9054\u3059\u308b\u3068\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u691c\u7d22\u6027\u80fd\u3092\u5411\u4e0a\u3055\u305b\u308b\u305f\u3081\u306b\u8d64\u9ed2\u6728\u306b\u5909\u63db\u3055\u308c\u307e\u3059\u3002
        • Go\u306f\u9023\u9396\u6cd5\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002Go\u306f\u5404\u30d0\u30b1\u30c3\u30c8\u304c\u6700\u59278\u3064\u306e\u30ad\u30fc\u5024\u30da\u30a2\u3092\u683c\u7d0d\u3067\u304d\u308b\u3053\u3068\u3092\u898f\u5b9a\u3057\u3001\u5bb9\u91cf\u3092\u8d85\u3048\u305f\u5834\u5408\u306f\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u30d0\u30b1\u30c3\u30c8\u304c\u9023\u7d50\u3055\u308c\u307e\u3059\u3002\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u30d0\u30b1\u30c3\u30c8\u304c\u591a\u3059\u304e\u308b\u5834\u5408\u3001\u6027\u80fd\u3092\u78ba\u4fdd\u3059\u308b\u305f\u3081\u306b\u7279\u5225\u306a\u7b49\u5bb9\u91cf\u30ea\u30b5\u30a4\u30ba\u64cd\u4f5c\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002
        "},{"location":"chapter_hashing/hash_map/","title":"6.1 \u00a0 \u30cf\u30c3\u30b7\u30e5\u8868","text":"

        \u30cf\u30c3\u30b7\u30e5\u8868\u306f\u30cf\u30c3\u30b7\u30e5\u30de\u30c3\u30d7\u3068\u3082\u547c\u3070\u308c\u3001\u30ad\u30fc\u3068\u5024\u306e\u9593\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u3092\u78ba\u7acb\u3057\u3001\u52b9\u7387\u7684\u306a\u8981\u7d20\u306e\u53d6\u5f97\u3092\u53ef\u80fd\u306b\u3059\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3059\u3002\u5177\u4f53\u7684\u306b\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306bkey\u3092\u5165\u529b\u3059\u308b\u3068\u3001\\(O(1)\\)\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3067\u5bfe\u5fdc\u3059\u308bvalue\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\\(n\\)\u4eba\u306e\u5b66\u751f\u304c\u3044\u3066\u3001\u5404\u5b66\u751f\u306b\u306f\u300c\u540d\u524d\u300d\u3068\u300c\u5b66\u7c4d\u756a\u53f7\u300d\u306e2\u3064\u306e\u30c7\u30fc\u30bf\u30d5\u30a3\u30fc\u30eb\u30c9\u304c\u3042\u308b\u3068\u3057\u307e\u3059\u3002\u5b66\u7c4d\u756a\u53f7\u3092\u5165\u529b\u3068\u3057\u3066\u5bfe\u5fdc\u3059\u308b\u540d\u524d\u3092\u8fd4\u3059\u30af\u30a8\u30ea\u6a5f\u80fd\u3092\u5b9f\u88c5\u3057\u305f\u3044\u5834\u5408\u3001\u4e0b\u56f3\u306b\u793a\u3059\u30cf\u30c3\u30b7\u30e5\u8868\u3092\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002

        \u56f3 6-1 \u00a0 \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u62bd\u8c61\u7684\u306a\u8868\u73fe

        \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u52a0\u3048\u3066\u3001\u914d\u5217\u3084\u9023\u7d50\u30ea\u30b9\u30c8\u3082\u30af\u30a8\u30ea\u6a5f\u80fd\u306e\u5b9f\u88c5\u306b\u4f7f\u7528\u3067\u304d\u307e\u3059\u304c\u3001\u6642\u9593\u8a08\u7b97\u91cf\u304c\u7570\u306a\u308a\u307e\u3059\u3002\u52b9\u7387\u306f\u4ee5\u4e0b\u306e\u8868\u3067\u6bd4\u8f03\u3055\u308c\u3066\u3044\u307e\u3059\uff1a

        • \u8981\u7d20\u306e\u633f\u5165: \u914d\u5217\uff08\u307e\u305f\u306f\u9023\u7d50\u30ea\u30b9\u30c8\uff09\u306e\u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0\u3059\u308b\u3060\u3051\u3067\u3059\u3002\u3053\u306e\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u3067\u3059\u3002
        • \u8981\u7d20\u306e\u691c\u7d22: \u914d\u5217\uff08\u307e\u305f\u306f\u9023\u7d50\u30ea\u30b9\u30c8\uff09\u304c\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u306a\u3044\u305f\u3081\u3001\u8981\u7d20\u3092\u691c\u7d22\u3059\u308b\u306b\u306f\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u8d70\u67fb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3059\u3002
        • \u8981\u7d20\u306e\u524a\u9664: \u8981\u7d20\u3092\u524a\u9664\u3059\u308b\u306b\u306f\u3001\u307e\u305a\u305d\u306e\u8981\u7d20\u3092\u898b\u3064\u3051\u3066\u304b\u3089\u3001\u914d\u5217\uff08\u307e\u305f\u306f\u9023\u7d50\u30ea\u30b9\u30c8\uff09\u304b\u3089\u524a\u9664\u3057\u307e\u3059\u3002\u3053\u306e\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3059\u3002

        \u8868 6-1 \u00a0 \u4e00\u822c\u7684\u306a\u64cd\u4f5c\u306e\u6642\u9593\u52b9\u7387\u306e\u6bd4\u8f03

        \u914d\u5217 \u9023\u7d50\u30ea\u30b9\u30c8 \u30cf\u30c3\u30b7\u30e5\u8868 \u8981\u7d20\u306e\u691c\u7d22 \\(O(n)\\) \\(O(n)\\) \\(O(1)\\) \u8981\u7d20\u306e\u633f\u5165 \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u8981\u7d20\u306e\u524a\u9664 \\(O(n)\\) \\(O(n)\\) \\(O(1)\\)

        \u89b3\u5bdf\u3055\u308c\u308b\u3088\u3046\u306b\u3001**\u30cf\u30c3\u30b7\u30e5\u8868\u306b\u304a\u3051\u308b\u64cd\u4f5c\uff08\u633f\u5165\u3001\u524a\u9664\u3001\u691c\u7d22\u3001\u5909\u66f4\uff09\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)**\u3067\u3001\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3059\u3002

        "},{"location":"chapter_hashing/hash_map/#611","title":"6.1.1 \u00a0 \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c","text":"

        \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c\u306b\u306f\u3001\u521d\u671f\u5316\u3001\u30af\u30a8\u30ea\u3001\u30ad\u30fc\u5024\u30da\u30a2\u306e\u8ffd\u52a0\u3001\u30ad\u30fc\u5024\u30da\u30a2\u306e\u524a\u9664\u304c\u3042\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306f\u30b3\u30fc\u30c9\u4f8b\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin hash_map.py
        # \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u521d\u671f\u5316\nhmap: dict = {}\n\n# \u8ffd\u52a0\u64cd\u4f5c\n# \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u8ffd\u52a0\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u5570\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9e2d\"\n\n# \u30af\u30a8\u30ea\u64cd\u4f5c\n# \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u3092\u5165\u529b\u3057\u3001\u5024\u3092\u53d6\u5f97\nname: str = hmap[15937]\n\n# \u524a\u9664\u64cd\u4f5c\n# \u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u524a\u9664\nhmap.pop(10583)\n
        hash_map.cpp
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u521d\u671f\u5316 */\nunordered_map<int, string> map;\n\n/* \u8ffd\u52a0\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u8ffd\u52a0\nmap[12836] = \"\u5c0f\u54c8\";\nmap[15937] = \"\u5c0f\u5570\";\nmap[16750] = \"\u5c0f\u7b97\";\nmap[13276] = \"\u5c0f\u6cd5\";\nmap[10583] = \"\u5c0f\u9e2d\";\n\n/* \u30af\u30a8\u30ea\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u3092\u5165\u529b\u3057\u3001\u5024\u3092\u53d6\u5f97\nstring name = map[15937];\n\n/* \u524a\u9664\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u524a\u9664\nmap.erase(10583);\n
        hash_map.java
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u521d\u671f\u5316 */\nMap<Integer, String> map = new HashMap<>();\n\n/* \u8ffd\u52a0\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u8ffd\u52a0\nmap.put(12836, \"\u5c0f\u54c8\");\nmap.put(15937, \"\u5c0f\u5570\");\nmap.put(16750, \"\u5c0f\u7b97\");\nmap.put(13276, \"\u5c0f\u6cd5\");\nmap.put(10583, \"\u5c0f\u9e2d\");\n\n/* \u30af\u30a8\u30ea\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u3092\u5165\u529b\u3057\u3001\u5024\u3092\u53d6\u5f97\nString name = map.get(15937);\n\n/* \u524a\u9664\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u524a\u9664\nmap.remove(10583);\n
        hash_map.cs
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u521d\u671f\u5316 */\nDictionary<int, string> map = new() {\n    /* \u8ffd\u52a0\u64cd\u4f5c */\n    // \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u8ffd\u52a0\n    { 12836, \"\u5c0f\u54c8\" },\n    { 15937, \"\u5c0f\u5570\" },\n    { 16750, \"\u5c0f\u7b97\" },\n    { 13276, \"\u5c0f\u6cd5\" },\n    { 10583, \"\u5c0f\u9e2d\" }\n};\n\n/* \u30af\u30a8\u30ea\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u3092\u5165\u529b\u3057\u3001\u5024\u3092\u53d6\u5f97\nstring name = map[15937];\n\n/* \u524a\u9664\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u524a\u9664\nmap.Remove(10583);\n
        hash_map_test.go
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u521d\u671f\u5316 */\nhmap := make(map[int]string)\n\n/* \u8ffd\u52a0\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u8ffd\u52a0\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u5570\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9e2d\"\n\n/* \u30af\u30a8\u30ea\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u3092\u5165\u529b\u3057\u3001\u5024\u3092\u53d6\u5f97\nname := hmap[15937]\n\n/* \u524a\u9664\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u524a\u9664\ndelete(hmap, 10583)\n
        hash_map.swift
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u521d\u671f\u5316 */\nvar map: [Int: String] = [:]\n\n/* \u8ffd\u52a0\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u8ffd\u52a0\nmap[12836] = \"\u5c0f\u54c8\"\nmap[15937] = \"\u5c0f\u5570\"\nmap[16750] = \"\u5c0f\u7b97\"\nmap[13276] = \"\u5c0f\u6cd5\"\nmap[10583] = \"\u5c0f\u9e2d\"\n\n/* \u30af\u30a8\u30ea\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u3092\u5165\u529b\u3057\u3001\u5024\u3092\u53d6\u5f97\nlet name = map[15937]!\n\n/* \u524a\u9664\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u524a\u9664\nmap.removeValue(forKey: 10583)\n
        hash_map.js
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u521d\u671f\u5316 */\nconst map = new Map();\n/* \u8ffd\u52a0\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u8ffd\u52a0\nmap.set(12836, '\u5c0f\u54c8');\nmap.set(15937, '\u5c0f\u5570');\nmap.set(16750, '\u5c0f\u7b97');\nmap.set(13276, '\u5c0f\u6cd5');\nmap.set(10583, '\u5c0f\u9e2d');\n\n/* \u30af\u30a8\u30ea\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u3092\u5165\u529b\u3057\u3001\u5024\u3092\u53d6\u5f97\nlet name = map.get(15937);\n\n/* \u524a\u9664\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u524a\u9664\nmap.delete(10583);\n
        hash_map.ts
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u521d\u671f\u5316 */\nconst map = new Map<number, string>();\n/* \u8ffd\u52a0\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u8ffd\u52a0\nmap.set(12836, '\u5c0f\u54c8');\nmap.set(15937, '\u5c0f\u5570');\nmap.set(16750, '\u5c0f\u7b97');\nmap.set(13276, '\u5c0f\u6cd5');\nmap.set(10583, '\u5c0f\u9e2d');\nconsole.info('\\n\u8ffd\u52a0\u5f8c\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306f\\nKey -> Value');\nconsole.info(map);\n\n/* \u30af\u30a8\u30ea\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u3092\u5165\u529b\u3057\u3001\u5024\u3092\u53d6\u5f97\nlet name = map.get(15937);\nconsole.info('\\n\u5b66\u7c4d\u756a\u53f715937\u3092\u5165\u529b\u3001\u540d\u524d\u3092\u554f\u3044\u5408\u308f\u305b ' + name);\n\n/* \u524a\u9664\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u524a\u9664\nmap.delete(10583);\nconsole.info('\\n10583\u3092\u524a\u9664\u5f8c\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306f\\nKey -> Value');\nconsole.info(map);\n
        hash_map.dart
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u521d\u671f\u5316 */\nMap<int, String> map = {};\n\n/* \u8ffd\u52a0\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u8ffd\u52a0\nmap[12836] = \"\u5c0f\u54c8\";\nmap[15937] = \"\u5c0f\u5570\";\nmap[16750] = \"\u5c0f\u7b97\";\nmap[13276] = \"\u5c0f\u6cd5\";\nmap[10583] = \"\u5c0f\u9e2d\";\n\n/* \u30af\u30a8\u30ea\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u3092\u5165\u529b\u3057\u3001\u5024\u3092\u53d6\u5f97\nString name = map[15937];\n\n/* \u524a\u9664\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u524a\u9664\nmap.remove(10583);\n
        hash_map.rs
        use std::collections::HashMap;\n\n/* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u521d\u671f\u5316 */\nlet mut map: HashMap<i32, String> = HashMap::new();\n\n/* \u8ffd\u52a0\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u8ffd\u52a0\nmap.insert(12836, \"\u5c0f\u54c8\".to_string());\nmap.insert(15937, \"\u5c0f\u5570\".to_string());\nmap.insert(16750, \"\u5c0f\u7b97\".to_string());\nmap.insert(13279, \"\u5c0f\u6cd5\".to_string());\nmap.insert(10583, \"\u5c0f\u9e2d\".to_string());\n\n/* \u30af\u30a8\u30ea\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u3092\u5165\u529b\u3057\u3001\u5024\u3092\u53d6\u5f97\nlet _name: Option<&String> = map.get(&15937);\n\n/* \u524a\u9664\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u524a\u9664\nlet _removed_value: Option<String> = map.remove(&10583);\n
        hash_map.c
        // C\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30cf\u30c3\u30b7\u30e5\u8868\u304c\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u305b\u3093\n
        hash_map.kt
        \n

        \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u8d70\u67fb\u3059\u308b\u4e00\u822c\u7684\u306a\u65b9\u6cd5\u306f3\u3064\u3042\u308a\u307e\u3059\uff1a\u30ad\u30fc\u5024\u30da\u30a2\u306e\u8d70\u67fb\u3001\u30ad\u30fc\u306e\u8d70\u67fb\u3001\u5024\u306e\u8d70\u67fb\u3002\u4ee5\u4e0b\u306f\u30b3\u30fc\u30c9\u4f8b\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin hash_map.py
        # \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u8d70\u67fb\n# \u30ad\u30fc\u5024\u30da\u30a2 key->value \u3092\u8d70\u67fb\nfor key, value in hmap.items():\n    print(key, \"->\", value)\n# \u30ad\u30fc\u306e\u307f\u3092\u8d70\u67fb\nfor key in hmap.keys():\n    print(key)\n# \u5024\u306e\u307f\u3092\u8d70\u67fb\nfor value in hmap.values():\n    print(value)\n
        hash_map.cpp
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u8d70\u67fb */\n// \u30ad\u30fc\u5024\u30da\u30a2 key->value \u3092\u8d70\u67fb\nfor (auto kv: map) {\n    cout << kv.first << \" -> \" << kv.second << endl;\n}\n// \u30a4\u30c6\u30ec\u30fc\u30bf\u3092\u4f7f\u7528\u3057\u3066\u30ad\u30fc\u5024\u30da\u30a2 key->value \u3092\u8d70\u67fb\nfor (auto iter = map.begin(); iter != map.end(); iter++) {\n    cout << iter->first << \"->\" << iter->second << endl;\n}\n
        hash_map.java
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u8d70\u67fb */\n// \u30ad\u30fc\u5024\u30da\u30a2 key->value \u3092\u8d70\u67fb\nfor (Map.Entry<Integer, String> kv: map.entrySet()) {\n    System.out.println(kv.getKey() + \" -> \" + kv.getValue());\n}\n// \u30ad\u30fc\u306e\u307f\u3092\u8d70\u67fb\nfor (int key: map.keySet()) {\n    System.out.println(key);\n}\n// \u5024\u306e\u307f\u3092\u8d70\u67fb\nfor (String val: map.values()) {\n    System.out.println(val);\n}\n
        hash_map.cs
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u8d70\u67fb */\n// \u30ad\u30fc\u5024\u30da\u30a2 Key->Value \u3092\u8d70\u67fb\nforeach (var kv in map) {\n    Console.WriteLine(kv.Key + \" -> \" + kv.Value);\n}\n// \u30ad\u30fc\u306e\u307f\u3092\u8d70\u67fb\nforeach (int key in map.Keys) {\n    Console.WriteLine(key);\n}\n// \u5024\u306e\u307f\u3092\u8d70\u67fb\nforeach (string val in map.Values) {\n    Console.WriteLine(val);\n}\n
        hash_map_test.go
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u8d70\u67fb */\n// \u30ad\u30fc\u5024\u30da\u30a2 key->value \u3092\u8d70\u67fb\nfor key, value := range hmap {\n    fmt.Println(key, \"->\", value)\n}\n// \u30ad\u30fc\u306e\u307f\u3092\u8d70\u67fb\nfor key := range hmap {\n    fmt.Println(key)\n}\n// \u5024\u306e\u307f\u3092\u8d70\u67fb\nfor _, value := range hmap {\n    fmt.Println(value)\n}\n
        hash_map.swift
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u8d70\u67fb */\n// \u30ad\u30fc\u5024\u30da\u30a2 Key->Value \u3092\u8d70\u67fb\nfor (key, value) in map {\n    print(\"\\(key) -> \\(value)\")\n}\n// \u30ad\u30fc\u306e\u307f\u3092\u8d70\u67fb\nfor key in map.keys {\n    print(key)\n}\n// \u5024\u306e\u307f\u3092\u8d70\u67fb\nfor value in map.values {\n    print(value)\n}\n
        hash_map.js
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u8d70\u67fb */\nconsole.info('\\n\u30ad\u30fc\u5024\u30da\u30a2 Key->Value \u3092\u8d70\u67fb');\nfor (const [k, v] of map.entries()) {\n    console.info(k + ' -> ' + v);\n}\nconsole.info('\\n\u30ad\u30fc\u306e\u307f\u3092\u8d70\u67fb Key');\nfor (const k of map.keys()) {\n    console.info(k);\n}\nconsole.info('\\n\u5024\u306e\u307f\u3092\u8d70\u67fb Value');\nfor (const v of map.values()) {\n    console.info(v);\n}\n
        hash_map.ts
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u8d70\u67fb */\nconsole.info('\\n\u30ad\u30fc\u5024\u30da\u30a2 Key->Value \u3092\u8d70\u67fb');\nfor (const [k, v] of map.entries()) {\n    console.info(k + ' -> ' + v);\n}\nconsole.info('\\n\u30ad\u30fc\u306e\u307f\u3092\u8d70\u67fb Key');\nfor (const k of map.keys()) {\n    console.info(k);\n}\nconsole.info('\\n\u5024\u306e\u307f\u3092\u8d70\u67fb Value');\nfor (const v of map.values()) {\n    console.info(v);\n}\n
        hash_map.dart
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u8d70\u67fb */\n// \u30ad\u30fc\u5024\u30da\u30a2 Key->Value \u3092\u8d70\u67fb\nmap.forEach((key, value) {\nprint('$key -> $value');\n});\n\n// \u30ad\u30fc\u306e\u307f\u3092\u8d70\u67fb Key\nmap.keys.forEach((key) {\nprint(key);\n});\n\n// \u5024\u306e\u307f\u3092\u8d70\u67fb Value\nmap.values.forEach((value) {\nprint(value);\n});\n
        hash_map.rs
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u8d70\u67fb */\n// \u30ad\u30fc\u5024\u30da\u30a2 Key->Value \u3092\u8d70\u67fb\nfor (key, value) in &map {\n    println!(\"{key} -> {value}\");\n}\n\n// \u30ad\u30fc\u306e\u307f\u3092\u8d70\u67fb Key\nfor key in map.keys() {\n    println!(\"{key}\");\n}\n\n// \u5024\u306e\u307f\u3092\u8d70\u67fb Value\nfor value in map.values() {\n    println!(\"{value}\");\n}\n
        hash_map.c
        // C\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30cf\u30c3\u30b7\u30e5\u8868\u304c\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u305b\u3093\n
        hash_map.kt
        \n
        "},{"location":"chapter_hashing/hash_map/#612","title":"6.1.2 \u00a0 \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u7c21\u5358\u306a\u5b9f\u88c5","text":"

        \u307e\u305a\u3001\u6700\u3082\u7c21\u5358\u306a\u30b1\u30fc\u30b9\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\uff1a\u914d\u5217\u306e\u307f\u3092\u4f7f\u3063\u3066\u30cf\u30c3\u30b7\u30e5\u8868\u3092\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3002\u30cf\u30c3\u30b7\u30e5\u8868\u306b\u304a\u3044\u3066\u3001\u914d\u5217\u306e\u5404\u7a7a\u304d\u30b9\u30ed\u30c3\u30c8\u306f\u30d0\u30b1\u30c3\u30c8\u3068\u547c\u3070\u308c\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u306f\u30ad\u30fc\u5024\u30da\u30a2\u3092\u683c\u7d0d\u3067\u304d\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30af\u30a8\u30ea\u64cd\u4f5c\u306fkey\u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u3092\u898b\u3064\u3051\u3001\u305d\u3053\u304b\u3089value\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002

        \u3067\u306f\u3001key\u306b\u57fa\u3065\u3044\u3066\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u3092\u3069\u306e\u3088\u3046\u306b\u7279\u5b9a\u3059\u308b\u306e\u3067\u3057\u3087\u3046\u304b\uff1f\u3053\u308c\u306f\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306b\u3088\u3063\u3066\u5b9f\u73fe\u3055\u308c\u307e\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u5f79\u5272\u306f\u3001\u3088\u308a\u5927\u304d\u306a\u5165\u529b\u7a7a\u9593\u3092\u3088\u308a\u5c0f\u3055\u306a\u51fa\u529b\u7a7a\u9593\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u8868\u3067\u306f\u3001\u5165\u529b\u7a7a\u9593\u306f\u3059\u3079\u3066\u306e\u30ad\u30fc\u3067\u69cb\u6210\u3055\u308c\u3001\u51fa\u529b\u7a7a\u9593\u306f\u3059\u3079\u3066\u306e\u30d0\u30b1\u30c3\u30c8\uff08\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\uff09\u3067\u69cb\u6210\u3055\u308c\u307e\u3059\u3002\u3064\u307e\u308a\u3001key\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u5bfe\u5fdc\u3059\u308b\u30ad\u30fc\u5024\u30da\u30a2\u306e\u914d\u5217\u5185\u306e\u683c\u7d0d\u4f4d\u7f6e\u3092\u6c7a\u5b9a\u3067\u304d\u307e\u3059\u3002

        \u4e0e\u3048\u3089\u308c\u305fkey\u306b\u5bfe\u3057\u3066\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u8a08\u7b97\u306f2\u3064\u306e\u30b9\u30c6\u30c3\u30d7\u3067\u69cb\u6210\u3055\u308c\u307e\u3059\uff1a

        1. \u7279\u5b9a\u306e\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0hash()\u3092\u4f7f\u7528\u3057\u3066\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u8a08\u7b97\u3057\u307e\u3059\u3002
        2. \u30cf\u30c3\u30b7\u30e5\u5024\u3092\u30d0\u30b1\u30c3\u30c8\u6570\uff08\u914d\u5217\u9577\uff09capacity\u3067\u5270\u4f59\u3092\u53d6\u308a\u3001\u30ad\u30fc\u306b\u5bfe\u5fdc\u3059\u308b\u914d\u5217index\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002
        index = hash(key) % capacity\n

        \u305d\u306e\u5f8c\u3001index\u3092\u4f7f\u7528\u3057\u3066\u30cf\u30c3\u30b7\u30e5\u8868\u5185\u306e\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3001value\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002

        \u914d\u5217\u9577\u304ccapacity = 100\u3067\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304chash(key) = key\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u3068\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306fkey % 100\u3068\u3057\u3066\u8868\u73fe\u3067\u304d\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u306f\u3001key\u3092\u5b66\u7c4d\u756a\u53f7\u3001value\u3092\u540d\u524d\u3068\u3057\u3066\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u52d5\u4f5c\u539f\u7406\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 6-2 \u00a0 \u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u52d5\u4f5c\u539f\u7406

        \u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u306f\u7c21\u5358\u306a\u30cf\u30c3\u30b7\u30e5\u8868\u3092\u5b9f\u88c5\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u3053\u3067\u306f\u3001key\u3068value\u3092Pair\u30af\u30e9\u30b9\u306b\u30ab\u30d7\u30bb\u30eb\u5316\u3057\u3066\u30ad\u30fc\u5024\u30da\u30a2\u3092\u8868\u73fe\u3057\u3066\u3044\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_hash_map.py
        class Pair:\n    \"\"\"\u30ad\u30fc\u5024\u30da\u30a2\"\"\"\n\n    def __init__(self, key: int, val: str):\n        self.key = key\n        self.val = val\n\nclass ArrayHashMap:\n    \"\"\"\u914d\u5217\u5b9f\u88c5\u306b\u57fa\u3065\u304f\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\"\"\"\n\n    def __init__(self):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        # 100\u500b\u306e\u30d0\u30b1\u30c3\u30c8\u3092\u542b\u3080\u914d\u5217\u3092\u521d\u671f\u5316\n        self.buckets: list[Pair | None] = [None] * 100\n\n    def hash_func(self, key: int) -> int:\n        \"\"\"\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\"\"\"\n        index = key % 100\n        return index\n\n    def get(self, key: int) -> str:\n        \"\"\"\u7167\u4f1a\u64cd\u4f5c\"\"\"\n        index: int = self.hash_func(key)\n        pair: Pair = self.buckets[index]\n        if pair is None:\n            return None\n        return pair.val\n\n    def put(self, key: int, val: str):\n        \"\"\"\u8ffd\u52a0\u64cd\u4f5c\"\"\"\n        pair = Pair(key, val)\n        index: int = self.hash_func(key)\n        self.buckets[index] = pair\n\n    def remove(self, key: int):\n        \"\"\"\u524a\u9664\u64cd\u4f5c\"\"\"\n        index: int = self.hash_func(key)\n        # None \u306b\u8a2d\u5b9a\u3057\u3001\u524a\u9664\u3092\u8868\u73fe\n        self.buckets[index] = None\n\n    def entry_set(self) -> list[Pair]:\n        \"\"\"\u3059\u3079\u3066\u306e\u30ad\u30fc\u5024\u30da\u30a2\u3092\u53d6\u5f97\"\"\"\n        result: list[Pair] = []\n        for pair in self.buckets:\n            if pair is not None:\n                result.append(pair)\n        return result\n\n    def key_set(self) -> list[int]:\n        \"\"\"\u3059\u3079\u3066\u306e\u30ad\u30fc\u3092\u53d6\u5f97\"\"\"\n        result = []\n        for pair in self.buckets:\n            if pair is not None:\n                result.append(pair.key)\n        return result\n\n    def value_set(self) -> list[str]:\n        \"\"\"\u3059\u3079\u3066\u306e\u5024\u3092\u53d6\u5f97\"\"\"\n        result = []\n        for pair in self.buckets:\n            if pair is not None:\n                result.append(pair.val)\n        return result\n\n    def print(self):\n        \"\"\"\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u51fa\u529b\"\"\"\n        for pair in self.buckets:\n            if pair is not None:\n                print(pair.key, \"->\", pair.val)\n
        array_hash_map.cpp
        /* \u30ad\u30fc\u5024\u30da\u30a2 */\nstruct Pair {\n  public:\n    int key;\n    string val;\n    Pair(int key, string val) {\n        this->key = key;\n        this->val = val;\n    }\n};\n\n/* \u914d\u5217\u5b9f\u88c5\u306b\u57fa\u3065\u304f\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb */\nclass ArrayHashMap {\n  private:\n    vector<Pair *> buckets;\n\n  public:\n    ArrayHashMap() {\n        // \u914d\u5217\u3092\u521d\u671f\u5316\u3001100\u500b\u306e\u30d0\u30b1\u30c3\u30c8\u3092\u542b\u3080\n        buckets = vector<Pair *>(100);\n    }\n\n    ~ArrayHashMap() {\n        // \u30e1\u30e2\u30ea\u3092\u89e3\u653e\n        for (const auto &bucket : buckets) {\n            delete bucket;\n        }\n        buckets.clear();\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u95a2\u6570 */\n    int hashFunc(int key) {\n        int index = key % 100;\n        return index;\n    }\n\n    /* \u30af\u30a8\u30ea\u64cd\u4f5c */\n    string get(int key) {\n        int index = hashFunc(key);\n        Pair *pair = buckets[index];\n        if (pair == nullptr)\n            return \"\";\n        return pair->val;\n    }\n\n    /* \u8ffd\u52a0\u64cd\u4f5c */\n    void put(int key, string val) {\n        Pair *pair = new Pair(key, val);\n        int index = hashFunc(key);\n        buckets[index] = pair;\n    }\n\n    /* \u524a\u9664\u64cd\u4f5c */\n    void remove(int key) {\n        int index = hashFunc(key);\n        // \u30e1\u30e2\u30ea\u3092\u89e3\u653e\u3057\u3066nullptr\u306b\u8a2d\u5b9a\n        delete buckets[index];\n        buckets[index] = nullptr;\n    }\n\n    /* \u3059\u3079\u3066\u306e\u30ad\u30fc\u5024\u30da\u30a2\u3092\u53d6\u5f97 */\n    vector<Pair *> pairSet() {\n        vector<Pair *> pairSet;\n        for (Pair *pair : buckets) {\n            if (pair != nullptr) {\n                pairSet.push_back(pair);\n            }\n        }\n        return pairSet;\n    }\n\n    /* \u3059\u3079\u3066\u306e\u30ad\u30fc\u3092\u53d6\u5f97 */\n    vector<int> keySet() {\n        vector<int> keySet;\n        for (Pair *pair : buckets) {\n            if (pair != nullptr) {\n                keySet.push_back(pair->key);\n            }\n        }\n        return keySet;\n    }\n\n    /* \u3059\u3079\u3066\u306e\u5024\u3092\u53d6\u5f97 */\n    vector<string> valueSet() {\n        vector<string> valueSet;\n        for (Pair *pair : buckets) {\n            if (pair != nullptr) {\n                valueSet.push_back(pair->val);\n            }\n        }\n        return valueSet;\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u5370\u5237 */\n    void print() {\n        for (Pair *kv : pairSet()) {\n            cout << kv->key << \" -> \" << kv->val << endl;\n        }\n    }\n};\n
        array_hash_map.java
        /* \u30ad\u30fc\u5024\u30da\u30a2 */\nclass Pair {\n    public int key;\n    public String val;\n\n    public Pair(int key, String val) {\n        this.key = key;\n        this.val = val;\n    }\n}\n\n/* \u914d\u5217\u5b9f\u88c5\u306b\u57fa\u3065\u304f\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb */\nclass ArrayHashMap {\n    private List<Pair> buckets;\n\n    public ArrayHashMap() {\n        // 100\u500b\u306e\u30d0\u30b1\u30c3\u30c8\u3092\u542b\u3080\u914d\u5217\u3092\u521d\u671f\u5316\n        buckets = new ArrayList<>();\n        for (int i = 0; i < 100; i++) {\n            buckets.add(null);\n        }\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u95a2\u6570 */\n    private int hashFunc(int key) {\n        int index = key % 100;\n        return index;\n    }\n\n    /* \u30af\u30a8\u30ea\u64cd\u4f5c */\n    public String get(int key) {\n        int index = hashFunc(key);\n        Pair pair = buckets.get(index);\n        if (pair == null)\n            return null;\n        return pair.val;\n    }\n\n    /* \u8ffd\u52a0\u64cd\u4f5c */\n    public void put(int key, String val) {\n        Pair pair = new Pair(key, val);\n        int index = hashFunc(key);\n        buckets.set(index, pair);\n    }\n\n    /* \u524a\u9664\u64cd\u4f5c */\n    public void remove(int key) {\n        int index = hashFunc(key);\n        // null\u306b\u8a2d\u5b9a\u3057\u3066\u524a\u9664\u3092\u793a\u3059\n        buckets.set(index, null);\n    }\n\n    /* \u3059\u3079\u3066\u306e\u30ad\u30fc\u5024\u30da\u30a2\u3092\u53d6\u5f97 */\n    public List<Pair> pairSet() {\n        List<Pair> pairSet = new ArrayList<>();\n        for (Pair pair : buckets) {\n            if (pair != null)\n                pairSet.add(pair);\n        }\n        return pairSet;\n    }\n\n    /* \u3059\u3079\u3066\u306e\u30ad\u30fc\u3092\u53d6\u5f97 */\n    public List<Integer> keySet() {\n        List<Integer> keySet = new ArrayList<>();\n        for (Pair pair : buckets) {\n            if (pair != null)\n                keySet.add(pair.key);\n        }\n        return keySet;\n    }\n\n    /* \u3059\u3079\u3066\u306e\u5024\u3092\u53d6\u5f97 */\n    public List<String> valueSet() {\n        List<String> valueSet = new ArrayList<>();\n        for (Pair pair : buckets) {\n            if (pair != null)\n                valueSet.add(pair.val);\n        }\n        return valueSet;\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u5370\u5237 */\n    public void print() {\n        for (Pair kv : pairSet()) {\n            System.out.println(kv.key + \" -> \" + kv.val);\n        }\n    }\n}\n
        array_hash_map.cs
        [class]{Pair}-[func]{}\n\n[class]{ArrayHashMap}-[func]{}\n
        array_hash_map.go
        [class]{pair}-[func]{}\n\n[class]{arrayHashMap}-[func]{}\n
        array_hash_map.swift
        [file]{utils/pair.swift}-[class]{Pair}-[func]{}\n\n[class]{ArrayHashMap}-[func]{}\n
        array_hash_map.js
        [class]{Pair}-[func]{}\n\n[class]{ArrayHashMap}-[func]{}\n
        array_hash_map.ts
        [class]{Pair}-[func]{}\n\n[class]{ArrayHashMap}-[func]{}\n
        array_hash_map.dart
        [class]{Pair}-[func]{}\n\n[class]{ArrayHashMap}-[func]{}\n
        array_hash_map.rs
        [class]{Pair}-[func]{}\n\n[class]{ArrayHashMap}-[func]{}\n
        array_hash_map.c
        [class]{Pair}-[func]{}\n\n[class]{ArrayHashMap}-[func]{}\n
        array_hash_map.kt
        [class]{Pair}-[func]{}\n\n[class]{ArrayHashMap}-[func]{}\n
        array_hash_map.rb
        [class]{Pair}-[func]{}\n\n[class]{ArrayHashMap}-[func]{}\n
        "},{"location":"chapter_hashing/hash_map/#613","title":"6.1.3 \u00a0 \u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3068\u30ea\u30b5\u30a4\u30ba","text":"

        \u672c\u8cea\u7684\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u5f79\u5272\u306f\u3001\u3059\u3079\u3066\u306e\u30ad\u30fc\u306e\u5165\u529b\u7a7a\u9593\u5168\u4f53\u3092\u3001\u3059\u3079\u3066\u306e\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u51fa\u529b\u7a7a\u9593\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u5165\u529b\u7a7a\u9593\u306f\u51fa\u529b\u7a7a\u9593\u3088\u308a\u3082\u306f\u308b\u304b\u306b\u5927\u304d\u3044\u3053\u3068\u304c\u3088\u304f\u3042\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u7406\u8ad6\u7684\u306b\u306f\u3001\u300c\u8907\u6570\u306e\u5165\u529b\u304c\u540c\u3058\u51fa\u529b\u306b\u5bfe\u5fdc\u3059\u308b\u300d\u30b1\u30fc\u30b9\u304c\u5e38\u306b\u5b58\u5728\u3057\u307e\u3059\u3002

        \u4e0a\u8a18\u306e\u4f8b\u3067\u306f\u3001\u4e0e\u3048\u3089\u308c\u305f\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u3067\u3001\u5165\u529bkey\u306e\u4e0b\u4e8c\u6841\u304c\u540c\u3058\u5834\u5408\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306f\u540c\u3058\u51fa\u529b\u3092\u751f\u6210\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u5b66\u7c4d\u756a\u53f712836\u306820336\u306e2\u4eba\u306e\u5b66\u751f\u3092\u30af\u30a8\u30ea\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\uff1a

        12836 % 100 = 36\n20336 % 100 = 36\n

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u4e21\u65b9\u306e\u5b66\u7c4d\u756a\u53f7\u304c\u540c\u3058\u540d\u524d\u3092\u6307\u3057\u3066\u304a\u308a\u3001\u3053\u308c\u306f\u660e\u3089\u304b\u306b\u9593\u9055\u3063\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u8907\u6570\u306e\u5165\u529b\u304c\u540c\u3058\u51fa\u529b\u306b\u5bfe\u5fdc\u3059\u308b\u72b6\u6cc1\u3092\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3068\u547c\u3073\u307e\u3059\u3002

        \u56f3 6-3 \u00a0 \u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306e\u4f8b

        \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u5bb9\u91cf\\(n\\)\u304c\u5897\u52a0\u3059\u308b\u306b\u3064\u308c\u3066\u3001\u8907\u6570\u306e\u30ad\u30fc\u304c\u540c\u3058\u30d0\u30b1\u30c3\u30c8\u306b\u5272\u308a\u5f53\u3066\u3089\u308c\u308b\u78ba\u7387\u304c\u6e1b\u5c11\u3057\u3001\u885d\u7a81\u304c\u5c11\u306a\u304f\u306a\u308b\u3053\u3068\u306f\u7406\u89e3\u3057\u3084\u3059\u3044\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3092\u30ea\u30b5\u30a4\u30ba\u3059\u308b\u3053\u3068\u3067\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u6e1b\u3089\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30ea\u30b5\u30a4\u30ba\u524d\u306f\u3001\u30ad\u30fc\u5024\u30da\u30a2(136, A)\u3068(236, D)\u304c\u885d\u7a81\u3057\u3066\u3044\u307e\u3057\u305f\u3002\u3057\u304b\u3057\u3001\u30ea\u30b5\u30a4\u30ba\u5f8c\u306f\u885d\u7a81\u304c\u89e3\u6c7a\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        \u56f3 6-4 \u00a0 \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30ea\u30b5\u30a4\u30ba

        \u914d\u5217\u306e\u62e1\u5f35\u3068\u540c\u69d8\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30ea\u30b5\u30a4\u30ba\u306b\u306f\u3059\u3079\u3066\u306e\u30ad\u30fc\u5024\u30da\u30a2\u3092\u5143\u306e\u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u65b0\u3057\u3044\u3082\u306e\u306b\u79fb\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002\u3055\u3089\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306ecapacity\u304c\u5909\u66f4\u3055\u308c\u308b\u305f\u3081\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u3059\u3079\u3066\u306e\u30ad\u30fc\u5024\u30da\u30a2\u306e\u683c\u7d0d\u4f4d\u7f6e\u3092\u518d\u8a08\u7b97\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u30ea\u30b5\u30a4\u30ba\u30d7\u30ed\u30bb\u30b9\u306e\u8a08\u7b97\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u304c\u3055\u3089\u306b\u5897\u52a0\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306f\u983b\u7e41\u306a\u30ea\u30b5\u30a4\u30ba\u3092\u9632\u3050\u305f\u3081\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306b\u5341\u5206\u5927\u304d\u306a\u5bb9\u91cf\u3092\u5272\u308a\u5f53\u3066\u308b\u3053\u3068\u304c\u3088\u304f\u3042\u308a\u307e\u3059\u3002

        \u8ca0\u8377\u7387\u306f\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u91cd\u8981\u306a\u6982\u5ff5\u3067\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u8868\u5185\u306e\u8981\u7d20\u6570\u3068\u30d0\u30b1\u30c3\u30c8\u6570\u306e\u6bd4\u7387\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u307e\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306e\u6df1\u523b\u5ea6\u3092\u6e2c\u5b9a\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u3001\u3057\u3070\u3057\u3070\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30ea\u30b5\u30a4\u30ba\u306e\u30c8\u30ea\u30ac\u30fc\u3068\u3057\u3066\u3082\u6a5f\u80fd\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001Java\u3067\u306f\u3001\u8ca0\u8377\u7387\u304c\\(0.75\\)\u3092\u8d85\u3048\u308b\u3068\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u30cf\u30c3\u30b7\u30e5\u8868\u3092\u5143\u306e\u30b5\u30a4\u30ba\u306e2\u500d\u306b\u30ea\u30b5\u30a4\u30ba\u3057\u307e\u3059\u3002

        "},{"location":"chapter_hashing/summary/","title":"6.4 \u00a0 \u307e\u3068\u3081","text":""},{"location":"chapter_hashing/summary/#1","title":"1. \u00a0 \u91cd\u8981\u306a\u30dd\u30a4\u30f3\u30c8","text":"
        • \u5165\u529bkey\u304c\u4e0e\u3048\u3089\u308c\u308b\u3068\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306f\\(O(1)\\)\u306e\u6642\u9593\u3067\u5bfe\u5fdc\u3059\u308bvalue\u3092\u53d6\u5f97\u3067\u304d\u3001\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3059\u3002
        • \u4e00\u822c\u7684\u306a\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u64cd\u4f5c\u306b\u306f\u3001\u30af\u30a8\u30ea\u3001\u30ad\u30fc\u5024\u30da\u30a2\u306e\u8ffd\u52a0\u3001\u30ad\u30fc\u5024\u30da\u30a2\u306e\u524a\u9664\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u8d70\u67fb\u304c\u3042\u308a\u307e\u3059\u3002
        • \u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306fkey\u3092\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3057\u3001\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3066value\u3092\u53d6\u5f97\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002
        • 2\u3064\u306e\u7570\u306a\u308b\u30ad\u30fc\u304c\u30cf\u30c3\u30b7\u30e5\u5316\u5f8c\u306b\u540c\u3058\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u306a\u308b\u5834\u5408\u304c\u3042\u308a\u3001\u8aa4\u3063\u305f\u30af\u30a8\u30ea\u7d50\u679c\u306b\u3064\u306a\u304c\u308a\u307e\u3059\u3002\u3053\u306e\u73fe\u8c61\u306f\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3068\u3057\u3066\u77e5\u3089\u308c\u3066\u3044\u307e\u3059\u3002
        • \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u5bb9\u91cf\u304c\u5927\u304d\u3044\u307b\u3069\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306e\u78ba\u7387\u306f\u4f4e\u304f\u306a\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30ea\u30b5\u30a4\u30ba\u306f\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u7de9\u548c\u3067\u304d\u307e\u3059\u3002\u914d\u5217\u306e\u30ea\u30b5\u30a4\u30ba\u3068\u540c\u69d8\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30ea\u30b5\u30a4\u30ba\u306f\u30b3\u30b9\u30c8\u304c\u9ad8\u3044\u3067\u3059\u3002
        • \u8981\u7d20\u6570\u3092\u30d0\u30b1\u30c3\u30c8\u6570\u3067\u5272\u3063\u305f\u8ca0\u8377\u7387\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306e\u6df1\u523b\u5ea6\u3092\u53cd\u6620\u3057\u3001\u3057\u3070\u3057\u3070\u30cf\u30c3\u30b7\u30e5\u8868\u30ea\u30b5\u30a4\u30ba\u306e\u30c8\u30ea\u30ac\u30fc\u6761\u4ef6\u3068\u3057\u3066\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        • \u9023\u9396\u6cd5\u306f\u5404\u8981\u7d20\u3092\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u5909\u63db\u3057\u3001\u885d\u7a81\u3059\u308b\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u540c\u3058\u30ea\u30b9\u30c8\u306b\u683c\u7d0d\u3059\u308b\u3053\u3068\u3067\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306b\u5bfe\u51e6\u3057\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u904e\u5ea6\u306b\u9577\u3044\u30ea\u30b9\u30c8\u306f\u30af\u30a8\u30ea\u52b9\u7387\u3092\u4f4e\u4e0b\u3055\u305b\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u30ea\u30b9\u30c8\u3092\u8d64\u9ed2\u6728\u306b\u5909\u63db\u3059\u308b\u3053\u3068\u3067\u6539\u5584\u3067\u304d\u307e\u3059\u3002
        • \u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u306f\u8907\u6570\u56de\u306e\u30d7\u30ed\u30fc\u30d6\u3092\u901a\u3057\u3066\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u51e6\u7406\u3057\u307e\u3059\u3002\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u306f\u56fa\u5b9a\u30b9\u30c6\u30c3\u30d7\u30b5\u30a4\u30ba\u3092\u4f7f\u7528\u3057\u307e\u3059\u304c\u3001\u8981\u7d20\u3092\u524a\u9664\u3067\u304d\u305a\u3001\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u3092\u8d77\u3053\u3057\u3084\u3059\u3044\u50be\u5411\u304c\u3042\u308a\u307e\u3059\u3002\u591a\u91cd\u30cf\u30c3\u30b7\u30e5\u306f\u30d7\u30ed\u30fc\u30d6\u306b\u8907\u6570\u306e\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3001\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u3068\u6bd4\u8f03\u3057\u3066\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u3092\u6e1b\u3089\u3057\u307e\u3059\u304c\u3001\u8a08\u7b97\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u304c\u5897\u52a0\u3057\u307e\u3059\u3002
        • \u7570\u306a\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306f\u3055\u307e\u3056\u307e\u306a\u30cf\u30c3\u30b7\u30e5\u8868\u5b9f\u88c5\u3092\u63a1\u7528\u3057\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001Java\u306eHashMap\u306f\u9023\u9396\u6cd5\u3092\u4f7f\u7528\u3057\u3001Python\u306edict\u306f\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u3092\u63a1\u7528\u3057\u3066\u3044\u307e\u3059\u3002
        • \u30cf\u30c3\u30b7\u30e5\u8868\u3067\u306f\u3001\u6c7a\u5b9a\u6027\u3001\u9ad8\u52b9\u7387\u3001\u5747\u7b49\u5206\u6563\u3092\u6301\u3064\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u671b\u307e\u308c\u307e\u3059\u3002\u6697\u53f7\u5316\u3067\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u885d\u7a81\u8010\u6027\u3068\u96ea\u5d29\u52b9\u679c\u3082\u6301\u3064\u3079\u304d\u3067\u3059\u3002
        • \u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u901a\u5e38\u3001\u30cf\u30c3\u30b7\u30e5\u5024\u306e\u5747\u7b49\u5206\u6563\u3092\u4fdd\u8a3c\u3057\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u6e1b\u3089\u3059\u305f\u3081\u306b\u3001\u5927\u304d\u306a\u7d20\u6570\u3092\u5270\u4f59\u3068\u3057\u3066\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u4e00\u822c\u7684\u306a\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u306f\u3001MD5\u3001SHA-1\u3001SHA-2\u3001SHA-3\u304c\u3042\u308a\u307e\u3059\u3002MD5\u306f\u30d5\u30a1\u30a4\u30eb\u6574\u5408\u6027\u30c1\u30a7\u30c3\u30af\u306b\u3088\u304f\u4f7f\u7528\u3055\u308c\u3001SHA-2\u306f\u5b89\u5168\u306a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3068\u30d7\u30ed\u30c8\u30b3\u30eb\u3067\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        • \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306f\u901a\u5e38\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97\u3059\u308b\u305f\u3081\u306b\u3001\u30c7\u30fc\u30bf\u578b\u306b\u5bfe\u3057\u3066\u7d44\u307f\u8fbc\u307f\u306e\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002\u4e00\u822c\u7684\u306b\u3001\u4e0d\u5909\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u307f\u304c\u30cf\u30c3\u30b7\u30e5\u53ef\u80fd\u3067\u3059\u3002
        "},{"location":"chapter_hashing/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q: \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(n)\\)\u306b\u60aa\u5316\u3059\u308b\u306e\u306f\u3044\u3064\u3067\u3059\u304b\uff1f

        \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u304c\u6df1\u523b\u306a\u5834\u5408\u306b\\(O(n)\\)\u306b\u60aa\u5316\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u304c\u9069\u5207\u306b\u8a2d\u8a08\u3055\u308c\u3001\u5bb9\u91cf\u304c\u9069\u5207\u306b\u8a2d\u5b9a\u3055\u308c\u3001\u885d\u7a81\u304c\u5747\u7b49\u306b\u5206\u6563\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u3067\u3059\u3002\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u7d44\u307f\u8fbc\u307f\u30cf\u30c3\u30b7\u30e5\u8868\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001\u901a\u5e38\u306f\u6642\u9593\u8a08\u7b97\u91cf\u3092\\(O(1)\\)\u3068\u8003\u3048\u307e\u3059\u3002

        Q: \u306a\u305c\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\\(f(x) = x\\)\u3092\u4f7f\u7528\u3057\u306a\u3044\u306e\u3067\u3059\u304b\uff1f\u3053\u308c\u306a\u3089\u885d\u7a81\u3092\u6392\u9664\u3067\u304d\u307e\u3059\u3002

        \u30cf\u30c3\u30b7\u30e5\u95a2\u6570\\(f(x) = x\\)\u3067\u306f\u3001\u5404\u8981\u7d20\u304c\u4e00\u610f\u306e\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5bfe\u5fdc\u3057\u3001\u3053\u308c\u306f\u914d\u5217\u3068\u540c\u7b49\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u5165\u529b\u7a7a\u9593\u306f\u901a\u5e38\u51fa\u529b\u7a7a\u9593\uff08\u914d\u5217\u9577\uff09\u3088\u308a\u3082\u306f\u308b\u304b\u306b\u5927\u304d\u3044\u305f\u3081\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u6700\u5f8c\u306e\u30b9\u30c6\u30c3\u30d7\u306f\u914d\u5217\u9577\u306e\u5270\u4f59\u3092\u53d6\u308b\u3053\u3068\u304c\u3088\u304f\u3042\u308a\u307e\u3059\u3002\u8a00\u3044\u63db\u3048\u308b\u3068\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u76ee\u6a19\u306f\u3001\\(O(1)\\)\u306e\u30af\u30a8\u30ea\u52b9\u7387\u3092\u63d0\u4f9b\u3057\u306a\u304c\u3089\u3001\u3088\u308a\u5927\u304d\u306a\u72b6\u614b\u7a7a\u9593\u3092\u3088\u308a\u5c0f\u3055\u306a\u3082\u306e\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3059\u308b\u3053\u3068\u3067\u3059\u3002

        Q: \u30cf\u30c3\u30b7\u30e5\u8868\u304c\u3053\u308c\u3089\u306e\u69cb\u9020\u3092\u4f7f\u3063\u3066\u5b9f\u88c5\u3055\u308c\u3066\u3044\u308b\u306b\u3082\u304b\u304b\u308f\u3089\u305a\u3001\u306a\u305c\u914d\u5217\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u4e8c\u5206\u6728\u3088\u308a\u3082\u52b9\u7387\u7684\u306b\u306a\u308c\u308b\u306e\u3067\u3059\u304b\uff1f

        \u307e\u305a\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306f\u6642\u9593\u52b9\u7387\u304c\u9ad8\u3044\u3067\u3059\u304c\u3001\u7a7a\u9593\u52b9\u7387\u306f\u4f4e\u3044\u3067\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30e1\u30e2\u30ea\u306e\u5927\u90e8\u5206\u306f\u672a\u4f7f\u7528\u306e\u307e\u307e\u3067\u3059\u3002

        \u6b21\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306f\u7279\u5b9a\u306e\u30e6\u30fc\u30b9\u30b1\u30fc\u30b9\u3067\u306e\u307f\u6642\u9593\u52b9\u7387\u304c\u9ad8\u3044\u3067\u3059\u3002\u914d\u5217\u3084\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u540c\u3058\u6642\u9593\u8a08\u7b97\u91cf\u3067\u6a5f\u80fd\u3092\u5b9f\u88c5\u3067\u304d\u308b\u5834\u5408\u3001\u901a\u5e38\u306f\u30cf\u30c3\u30b7\u30e5\u8868\u3092\u4f7f\u7528\u3059\u308b\u3088\u308a\u3082\u9ad8\u901f\u3067\u3059\u3002\u3053\u308c\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u8a08\u7b97\u304c\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u3092\u767a\u751f\u3055\u305b\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306e\u5b9a\u6570\u56e0\u5b50\u304c\u5927\u304d\u304f\u306a\u308b\u305f\u3081\u3067\u3059\u3002

        \u6700\u5f8c\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\u60aa\u5316\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u9023\u9396\u6cd5\u3067\u306f\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3084\u8d64\u9ed2\u6728\u3067\u691c\u7d22\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3001\u3053\u308c\u306f\u4f9d\u7136\u3068\u3057\u3066\\(O(n)\\)\u6642\u9593\u306b\u60aa\u5316\u3059\u308b\u30ea\u30b9\u30af\u304c\u3042\u308a\u307e\u3059\u3002

        Q: \u591a\u91cd\u30cf\u30c3\u30b7\u30e5\u306b\u3082\u8981\u7d20\u3092\u76f4\u63a5\u524a\u9664\u3067\u304d\u306a\u3044\u3068\u3044\u3046\u6b20\u9665\u304c\u3042\u308a\u307e\u3059\u304b\uff1f\u524a\u9664\u3068\u3057\u3066\u30de\u30fc\u30af\u3055\u308c\u305f\u7a7a\u9593\u306f\u518d\u5229\u7528\u3067\u304d\u307e\u3059\u304b\uff1f

        \u591a\u91cd\u30cf\u30c3\u30b7\u30e5\u306f\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u306e\u4e00\u5f62\u614b\u3067\u3042\u308a\u3001\u3059\u3079\u3066\u306e\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u306b\u306f\u8981\u7d20\u3092\u76f4\u63a5\u524a\u9664\u3067\u304d\u306a\u3044\u3068\u3044\u3046\u6b20\u70b9\u304c\u3042\u308a\u307e\u3059\u3002\u8981\u7d20\u3092\u524a\u9664\u6e08\u307f\u3068\u3057\u3066\u30de\u30fc\u30af\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u30de\u30fc\u30af\u3055\u308c\u305f\u7a7a\u9593\u306f\u518d\u5229\u7528\u3067\u304d\u307e\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u8868\u306b\u65b0\u3057\u3044\u8981\u7d20\u3092\u633f\u5165\u3059\u308b\u969b\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u304c\u524a\u9664\u6e08\u307f\u3068\u3057\u3066\u30de\u30fc\u30af\u3055\u308c\u305f\u4f4d\u7f6e\u3092\u6307\u3057\u3066\u3044\u308b\u5834\u5408\u3001\u305d\u306e\u4f4d\u7f6e\u306f\u65b0\u3057\u3044\u8981\u7d20\u306b\u3088\u3063\u3066\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30d7\u30ed\u30fc\u30d6\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u7dad\u6301\u3057\u306a\u304c\u3089\u3001\u7a7a\u9593\u306e\u52b9\u7387\u7684\u306a\u4f7f\u7528\u304c\u4fdd\u8a3c\u3055\u308c\u307e\u3059\u3002

        Q: \u306a\u305c\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u306e\u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u4e2d\u306b\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u304c\u767a\u751f\u3059\u308b\u306e\u3067\u3059\u304b\uff1f

        \u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u4e2d\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306f\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u3068\u30ad\u30fc\u5024\u30da\u30a2\u3092\u6307\u3057\u307e\u3059\u3002key\u304c\u4e00\u81f4\u3057\u306a\u3044\u5834\u5408\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u793a\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u306f\u6b63\u3057\u3044\u30ad\u30fc\u5024\u30da\u30a2\u304c\u898b\u3064\u304b\u308b\u304b\u691c\u7d22\u304c\u5931\u6557\u3059\u308b\u307e\u3067\u3001\u4e8b\u524d\u306b\u6c7a\u3081\u3089\u308c\u305f\u30b9\u30c6\u30c3\u30d7\u30b5\u30a4\u30ba\u3067\u4e0b\u65b9\u5411\u306b\u691c\u7d22\u3057\u307e\u3059\u3002

        Q: \u306a\u305c\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30ea\u30b5\u30a4\u30ba\u304c\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u7de9\u548c\u3067\u304d\u308b\u306e\u3067\u3059\u304b\uff1f

        \u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u6700\u5f8c\u306e\u30b9\u30c6\u30c3\u30d7\u306f\u3001\u51fa\u529b\u3092\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2\u5185\u306b\u4fdd\u3064\u305f\u3081\u306b\u3001\u914d\u5217\u9577\\(n\\)\u306e\u5270\u4f59\u3092\u53d6\u308b\u3053\u3068\u304c\u3088\u304f\u3042\u308a\u307e\u3059\u3002\u30ea\u30b5\u30a4\u30ba\u6642\u3001\u914d\u5217\u9577\\(n\\)\u304c\u5909\u5316\u3057\u3001\u30ad\u30fc\u306b\u5bfe\u5fdc\u3059\u308b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3082\u5909\u5316\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u4ee5\u524d\u306b\u540c\u3058\u30d0\u30b1\u30c3\u30c8\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3055\u308c\u3066\u3044\u305f\u30ad\u30fc\u304c\u3001\u30ea\u30b5\u30a4\u30ba\u5f8c\u306b\u8907\u6570\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u6563\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u305d\u308c\u306b\u3088\u3063\u3066\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u304c\u7de9\u548c\u3055\u308c\u307e\u3059\u3002

        "},{"location":"chapter_heap/","title":"\u7b2c 8 \u7ae0 \u00a0 \u30d2\u30fc\u30d7","text":"

        Abstract

        \u30d2\u30fc\u30d7\u306f\u5c71\u3068\u305d\u306e\u967a\u3057\u3044\u5cf0\u306e\u3088\u3046\u306b\u3001\u5c64\u3092\u306a\u3057\u3066\u8d77\u4f0f\u3057\u3001\u305d\u308c\u305e\u308c\u304c\u72ec\u7279\u306e\u5f62\u3092\u6301\u3063\u3066\u3044\u307e\u3059\u3002

        \u5404\u5c71\u306e\u9802\u306f\u6563\u3089\u3070\u3063\u305f\u9ad8\u3055\u3067\u4e0a\u4e0b\u3057\u307e\u3059\u304c\u3001\u6700\u3082\u9ad8\u3044\u3082\u306e\u304c\u5e38\u306b\u6700\u521d\u306b\u6ce8\u76ee\u3092\u96c6\u3081\u307e\u3059\u3002

        "},{"location":"chapter_heap/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 8.1 \u00a0 \u30d2\u30fc\u30d7
        • 8.2 \u00a0 \u30d2\u30fc\u30d7\u306e\u69cb\u7bc9
        • 8.3 \u00a0 Top-k\u554f\u984c
        • 8.4 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_heap/build_heap/","title":"8.2 \u00a0 \u30d2\u30fc\u30d7\u69cb\u7bc9\u64cd\u4f5c","text":"

        \u5834\u5408\u306b\u3088\u3063\u3066\u306f\u3001\u30ea\u30b9\u30c8\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u4f7f\u7528\u3057\u3066\u30d2\u30fc\u30d7\u3092\u69cb\u7bc9\u3057\u305f\u3044\u3053\u3068\u304c\u3042\u308a\u3001\u3053\u306e\u30d7\u30ed\u30bb\u30b9\u306f\u300c\u30d2\u30fc\u30d7\u69cb\u7bc9\u64cd\u4f5c\u300d\u3068\u3057\u3066\u77e5\u3089\u308c\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_heap/build_heap/#821","title":"8.2.1 \u00a0 \u30d2\u30fc\u30d7\u633f\u5165\u64cd\u4f5c\u306b\u3088\u308b\u5b9f\u88c5","text":"

        \u307e\u305a\u3001\u7a7a\u306e\u30d2\u30fc\u30d7\u3092\u4f5c\u6210\u3057\u3001\u6b21\u306b\u30ea\u30b9\u30c8\u3092\u53cd\u5fa9\u51e6\u7406\u3057\u3066\u3001\u5404\u8981\u7d20\u306b\u5bfe\u3057\u3066\u9806\u756a\u306b\u300c\u30d2\u30fc\u30d7\u633f\u5165\u64cd\u4f5c\u300d\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306e\u672b\u5c3e\u306b\u8ffd\u52a0\u3057\u3001\u6b21\u306b\u4e0b\u304b\u3089\u4e0a\u306b\u300c\u30d2\u30fc\u30d7\u5316\u300d\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

        \u30d2\u30fc\u30d7\u306b\u8981\u7d20\u304c\u8ffd\u52a0\u3055\u308c\u308b\u305f\u3073\u306b\u3001\u30d2\u30fc\u30d7\u306e\u9577\u3055\u306f1\u3064\u305a\u3064\u5897\u52a0\u3057\u307e\u3059\u3002\u30ce\u30fc\u30c9\u306f\u4e8c\u5206\u6728\u306b\u4e0a\u304b\u3089\u4e0b\u306b\u8ffd\u52a0\u3055\u308c\u308b\u305f\u3081\u3001\u30d2\u30fc\u30d7\u306f\u300c\u4e0a\u304b\u3089\u4e0b\u306b\u300d\u69cb\u7bc9\u3055\u308c\u307e\u3059\u3002

        \u8981\u7d20\u6570\u3092\\(n\\)\u3068\u3059\u308b\u3068\u3001\u5404\u8981\u7d20\u306e\u633f\u5165\u64cd\u4f5c\u306f\\(O(\\log{n})\\)\u6642\u9593\u304b\u304b\u308b\u305f\u3081\u3001\u3053\u306e\u30d2\u30fc\u30d7\u69cb\u7bc9\u65b9\u6cd5\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n \\log n)\\)\u3067\u3059\u3002

        "},{"location":"chapter_heap/build_heap/#822","title":"8.2.2 \u00a0 \u8d70\u67fb\u306b\u3088\u308b\u30d2\u30fc\u30d7\u5316\u306e\u5b9f\u88c5","text":"

        \u5b9f\u969b\u306b\u306f\u30012\u3064\u306e\u30b9\u30c6\u30c3\u30d7\u3067\u3088\u308a\u52b9\u7387\u7684\u306a\u30d2\u30fc\u30d7\u69cb\u7bc9\u65b9\u6cd5\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002

        1. \u30ea\u30b9\u30c8\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u305d\u306e\u307e\u307e\u30d2\u30fc\u30d7\u306b\u8ffd\u52a0\u3057\u307e\u3059\u3002\u3053\u306e\u6642\u70b9\u3067\u306f\u3001\u30d2\u30fc\u30d7\u306e\u6027\u8cea\u306f\u307e\u3060\u6e80\u305f\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002
        2. \u30d2\u30fc\u30d7\u3092\u9006\u9806\uff08\u30ec\u30d9\u30eb\u9806\u8d70\u67fb\u306e\u9006\uff09\u3067\u8d70\u67fb\u3057\u3001\u5404\u975e\u8449\u30ce\u30fc\u30c9\u306b\u5bfe\u3057\u3066\u300c\u4e0a\u304b\u3089\u4e0b\u306e\u30d2\u30fc\u30d7\u5316\u300d\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002

        \u30ce\u30fc\u30c9\u3092\u30d2\u30fc\u30d7\u5316\u3057\u305f\u5f8c\u3001\u305d\u306e\u30ce\u30fc\u30c9\u3092\u6839\u3068\u3059\u308b\u90e8\u5206\u6728\u306f\u6709\u52b9\u306a\u90e8\u5206\u30d2\u30fc\u30d7\u306b\u306a\u308a\u307e\u3059\u3002\u8d70\u67fb\u304c\u9006\u9806\u3067\u3042\u308b\u305f\u3081\u3001\u30d2\u30fc\u30d7\u306f\u300c\u4e0b\u304b\u3089\u4e0a\u306b\u300d\u69cb\u7bc9\u3055\u308c\u307e\u3059\u3002

        \u9006\u8d70\u67fb\u3092\u9078\u629e\u3059\u308b\u7406\u7531\u306f\u3001\u73fe\u5728\u306e\u30ce\u30fc\u30c9\u306e\u4e0b\u306e\u90e8\u5206\u6728\u304c\u3059\u3067\u306b\u6709\u52b9\u306a\u90e8\u5206\u30d2\u30fc\u30d7\u3067\u3042\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3057\u3001\u73fe\u5728\u306e\u30ce\u30fc\u30c9\u306e\u30d2\u30fc\u30d7\u5316\u3092\u52b9\u679c\u7684\u306b\u3059\u308b\u305f\u3081\u3067\u3059\u3002

        \u8a00\u53ca\u3059\u308b\u4fa1\u5024\u304c\u3042\u308b\u306e\u306f\u3001**\u8449\u30ce\u30fc\u30c9\u306f\u5b50\u3092\u6301\u305f\u306a\u3044\u305f\u3081\u3001\u81ea\u7136\u306b\u6709\u52b9\u306a\u90e8\u5206\u30d2\u30fc\u30d7\u3092\u5f62\u6210\u3057\u3001\u30d2\u30fc\u30d7\u5316\u3059\u308b\u5fc5\u8981\u304c\u306a\u3044**\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u6700\u5f8c\u306e\u975e\u8449\u30ce\u30fc\u30c9\u306f\u6700\u5f8c\u306e\u30ce\u30fc\u30c9\u306e\u89aa\u3067\u3059\u3002\u305d\u3053\u304b\u3089\u958b\u59cb\u3057\u3066\u9006\u9806\u306b\u8d70\u67fb\u3057\u3066\u30d2\u30fc\u30d7\u5316\u3092\u5b9f\u884c\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
        def __init__(self, nums: list[int]):\n    \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3001\u5165\u529b\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u3044\u3066\u30d2\u30fc\u30d7\u3092\u69cb\u7bc9\"\"\"\n    # \u3059\u3079\u3066\u306e\u30ea\u30b9\u30c8\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u8ffd\u52a0\n    self.max_heap = nums\n    # \u8449\u4ee5\u5916\u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3092\u30d2\u30fc\u30d7\u5316\n    for i in range(self.parent(self.size() - 1), -1, -1):\n        self.sift_down(i)\n
        my_heap.cpp
        /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3001\u5165\u529b\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u3044\u3066\u30d2\u30fc\u30d7\u3092\u69cb\u7bc9 */\nMaxHeap(vector<int> nums) {\n    // \u3059\u3079\u3066\u306e\u30ea\u30b9\u30c8\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u8ffd\u52a0\n    maxHeap = nums;\n    // \u8449\u4ee5\u5916\u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3092\u30d2\u30fc\u30d7\u5316\n    for (int i = parent(size() - 1); i >= 0; i--) {\n        siftDown(i);\n    }\n}\n
        my_heap.java
        /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3001\u5165\u529b\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u3044\u3066\u30d2\u30fc\u30d7\u3092\u69cb\u7bc9 */\nMaxHeap(List<Integer> nums) {\n    // \u3059\u3079\u3066\u306e\u30ea\u30b9\u30c8\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u8ffd\u52a0\n    maxHeap = new ArrayList<>(nums);\n    // \u8449\u3092\u9664\u304f\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3092\u30d2\u30fc\u30d7\u5316\n    for (int i = parent(size() - 1); i >= 0; i--) {\n        siftDown(i);\n    }\n}\n
        my_heap.cs
        [class]{MaxHeap}-[func]{MaxHeap}\n
        my_heap.go
        [class]{maxHeap}-[func]{newMaxHeap}\n
        my_heap.swift
        [class]{MaxHeap}-[func]{init}\n
        my_heap.js
        [class]{MaxHeap}-[func]{constructor}\n
        my_heap.ts
        [class]{MaxHeap}-[func]{constructor}\n
        my_heap.dart
        [class]{MaxHeap}-[func]{MaxHeap}\n
        my_heap.rs
        [class]{MaxHeap}-[func]{new}\n
        my_heap.c
        [class]{MaxHeap}-[func]{newMaxHeap}\n
        my_heap.kt
        [class]{MaxHeap}-[func]{}\n
        my_heap.rb
        [class]{MaxHeap}-[func]{initialize}\n
        "},{"location":"chapter_heap/build_heap/#823","title":"8.2.3 \u00a0 \u8a08\u7b97\u91cf\u5206\u6790","text":"

        \u6b21\u306b\u3001\u3053\u306e\u7b2c2\u306e\u30d2\u30fc\u30d7\u69cb\u7bc9\u65b9\u6cd5\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3092\u8a08\u7b97\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002

        • \u5b8c\u5099\u4e8c\u5206\u6728\u306e\u30ce\u30fc\u30c9\u6570\u3092\\(n\\)\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u8449\u30ce\u30fc\u30c9\u306e\u6570\u306f\\((n + 1) / 2\\)\u3067\u3059\u3002\u3053\u3053\u3067\\(/\\) \u306f\u6574\u6570\u9664\u7b97\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30d2\u30fc\u30d7\u5316\u304c\u5fc5\u8981\u306a\u30ce\u30fc\u30c9\u306e\u6570\u306f\\((n - 1) / 2\\)\u3067\u3059\u3002
        • \u300c\u4e0a\u304b\u3089\u4e0b\u306e\u30d2\u30fc\u30d7\u5316\u300d\u306e\u30d7\u30ed\u30bb\u30b9\u3067\u306f\u3001\u5404\u30ce\u30fc\u30c9\u306f\u6700\u5927\u3067\u8449\u30ce\u30fc\u30c9\u307e\u3067\u30d2\u30fc\u30d7\u5316\u3055\u308c\u308b\u305f\u3081\u3001\u6700\u5927\u53cd\u5fa9\u56de\u6570\u306f\u4e8c\u5206\u6728\u306e\u9ad8\u3055\\(\\log n\\)\u3067\u3059\u3002

        \u3053\u306e2\u3064\u3092\u639b\u3051\u5408\u308f\u305b\u308b\u3068\u3001\u30d2\u30fc\u30d7\u69cb\u7bc9\u30d7\u30ed\u30bb\u30b9\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n \\log n)\\)\u3068\u306a\u308a\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u3053\u306e\u63a8\u5b9a\u306f\u6b63\u78ba\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u4e8c\u5206\u6728\u306e\u4e0b\u4f4d\u30ec\u30d9\u30eb\u306b\u306f\u4e0a\u4f4d\u3088\u308a\u3082\u306f\u308b\u304b\u306b\u591a\u304f\u306e\u30ce\u30fc\u30c9\u304c\u3042\u308b\u3068\u3044\u3046\u6027\u8cea\u3092\u8003\u616e\u3057\u3066\u3044\u306a\u3044\u304b\u3089\u3067\u3059\u3002

        \u3088\u308a\u6b63\u78ba\u306a\u8a08\u7b97\u3092\u884c\u3044\u307e\u3057\u3087\u3046\u3002\u8a08\u7b97\u3092\u7c21\u7d20\u5316\u3059\u308b\u305f\u3081\u3001\\(n\\)\u500b\u306e\u30ce\u30fc\u30c9\u3068\u9ad8\u3055\\(h\\)\u3092\u6301\u3064\u300c\u5b8c\u5168\u4e8c\u5206\u6728\u300d\u3092\u4eee\u5b9a\u3057\u307e\u3059\u3002\u3053\u306e\u4eee\u5b9a\u306f\u7d50\u679c\u306e\u6b63\u78ba\u6027\u306b\u5f71\u97ff\u3057\u307e\u305b\u3093\u3002

        \u56f3 8-5 \u00a0 \u5b8c\u5168\u4e8c\u5206\u6728\u306e\u5404\u30ec\u30d9\u30eb\u306e\u30ce\u30fc\u30c9\u6570

        \u4e0a\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30ce\u30fc\u30c9\u304c\u300c\u4e0a\u304b\u3089\u4e0b\u306b\u30d2\u30fc\u30d7\u5316\u3055\u308c\u308b\u300d\u6700\u5927\u53cd\u5fa9\u56de\u6570\u306f\u3001\u305d\u306e\u30ce\u30fc\u30c9\u304b\u3089\u8449\u30ce\u30fc\u30c9\u307e\u3067\u306e\u8ddd\u96e2\u3068\u7b49\u3057\u304f\u3001\u3053\u308c\u306f\u6b63\u78ba\u306b\u300c\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u300d\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u5404\u30ec\u30d9\u30eb\u3067\u300c\u30ce\u30fc\u30c9\u6570\u00d7\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u300d\u3092\u5408\u8a08\u3057\u3066\u3001**\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u306e\u7dcf\u30d2\u30fc\u30d7\u5316\u53cd\u5fa9\u56de\u6570\u3092\u5f97\u308b**\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \\[ T(h) = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{(h-1)}\\times1 \\]

        \u4e0a\u8a18\u306e\u65b9\u7a0b\u5f0f\u3092\u7c21\u7d20\u5316\u3059\u308b\u305f\u3081\u306b\u3001\u9ad8\u6821\u306e\u6570\u5217\u306e\u77e5\u8b58\u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u307e\u305a\\(T(h)\\)\u306b\\(2\\)\u3092\u639b\u3051\u3066\u4ee5\u4e0b\u3092\u5f97\u307e\u3059\uff1a

        \\[ \\begin{aligned} T(h) & = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{h-1}\\times1 \\newline 2T(h) & = 2^1h + 2^2(h-1) + 2^3(h-2) + \\dots + 2^h\\times1 \\newline \\end{aligned} \\]

        \u5909\u4f4d\u6cd5\u3092\u4f7f\u7528\u3057\u3066\\(2T(h)\\)\u304b\u3089\\(T(h)\\)\u3092\u6e1b\u7b97\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u3092\u5f97\u307e\u3059\uff1a

        \\[ 2T(h) - T(h) = T(h) = -2^0h + 2^1 + 2^2 + \\dots + 2^{h-1} + 2^h \\]

        \u65b9\u7a0b\u5f0f\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\\(T(h)\\)\u306f\u7b49\u6bd4\u6570\u5217\u3067\u3042\u308a\u3001\u548c\u306e\u516c\u5f0f\u3092\u4f7f\u7528\u3057\u3066\u76f4\u63a5\u8a08\u7b97\u3067\u304d\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\u4ee5\u4e0b\u306b\u306a\u308a\u307e\u3059\uff1a

        \\[ \\begin{aligned} T(h) & = 2 \\frac{1 - 2^h}{1 - 2} - h \\newline & = 2^{h+1} - h - 2 \\newline & = O(2^h) \\end{aligned} \\]

        \u3055\u3089\u306b\u3001\u9ad8\u3055\\(h\\)\u306e\u5b8c\u5168\u4e8c\u5206\u6728\u306f\\(n = 2^{h+1} - 1\\)\u500b\u306e\u30ce\u30fc\u30c9\u3092\u6301\u3064\u305f\u3081\u3001\u8a08\u7b97\u91cf\u306f\\(O(2^h) = O(n)\\)\u3067\u3059\u3002\u3053\u306e\u8a08\u7b97\u306f\u3001**\u30ea\u30b9\u30c8\u3092\u5165\u529b\u3057\u3066\u30d2\u30fc\u30d7\u3092\u69cb\u7bc9\u3059\u308b\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(n)\\)\u3067\u3042\u308a\u3001\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3042\u308b**\u3053\u3068\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_heap/heap/","title":"8.1 \u00a0 \u30d2\u30fc\u30d7","text":"

        \u30d2\u30fc\u30d7\u306f\u7279\u5b9a\u306e\u6761\u4ef6\u3092\u6e80\u305f\u3059\u5b8c\u5099\u4e8c\u5206\u6728\u3067\u3001\u4e3b\u306b\u6b21\u306e2\u3064\u306e\u30bf\u30a4\u30d7\u306b\u5206\u985e\u3055\u308c\u307e\u3059\uff08\u4e0b\u56f3\u53c2\u7167\uff09\u3002

        • \u6700\u5c0f\u30d2\u30fc\u30d7\uff1a\u4efb\u610f\u306e\u30ce\u30fc\u30c9\u306e\u5024 \\(\\leq\\) \u305d\u306e\u5b50\u30ce\u30fc\u30c9\u306e\u5024\u3002
        • \u6700\u5927\u30d2\u30fc\u30d7\uff1a\u4efb\u610f\u306e\u30ce\u30fc\u30c9\u306e\u5024 \\(\\geq\\) \u305d\u306e\u5b50\u30ce\u30fc\u30c9\u306e\u5024\u3002

        \u56f3 8-1 \u00a0 \u6700\u5c0f\u30d2\u30fc\u30d7\u3068\u6700\u5927\u30d2\u30fc\u30d7

        \u5b8c\u5099\u4e8c\u5206\u6728\u306e\u7279\u5225\u306a\u30b1\u30fc\u30b9\u3068\u3057\u3066\u3001\u30d2\u30fc\u30d7\u306b\u306f\u4ee5\u4e0b\u306e\u7279\u6027\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u6700\u4e0b\u4f4d\u5c64\u306e\u30ce\u30fc\u30c9\u306f\u5de6\u304b\u3089\u53f3\u306b\u57cb\u3081\u3089\u308c\u3001\u4ed6\u306e\u5c64\u306e\u30ce\u30fc\u30c9\u306f\u5b8c\u5168\u306b\u57cb\u3081\u3089\u308c\u3066\u3044\u307e\u3059\u3002
        • \u4e8c\u5206\u6728\u306e\u6839\u30ce\u30fc\u30c9\u3092\u30d2\u30fc\u30d7\u306e\u300c\u5148\u982d\u300d\u3068\u547c\u3073\u3001\u6700\u3082\u53f3\u4e0b\u306e\u30ce\u30fc\u30c9\u3092\u30d2\u30fc\u30d7\u306e\u300c\u672b\u5c3e\u300d\u3068\u547c\u3073\u307e\u3059\u3002
        • \u6700\u5927\u30d2\u30fc\u30d7\uff08\u6700\u5c0f\u30d2\u30fc\u30d7\uff09\u306e\u5834\u5408\u3001\u5148\u982d\u8981\u7d20\uff08\u6839\uff09\u306e\u5024\u306f\u3059\u3079\u3066\u306e\u8981\u7d20\u306e\u4e2d\u3067\u6700\u5927\uff08\u6700\u5c0f\uff09\u3067\u3059\u3002
        "},{"location":"chapter_heap/heap/#811","title":"8.1.1 \u00a0 \u30d2\u30fc\u30d7\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c","text":"

        \u591a\u304f\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u304c\u512a\u5148\u5ea6\u30ad\u30e5\u30fc\u3092\u63d0\u4f9b\u3057\u3066\u3044\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3053\u308c\u306f\u512a\u5148\u5ea6\u4ed8\u304d\u30bd\u30fc\u30c8\u3092\u6301\u3064\u30ad\u30e5\u30fc\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u308b\u62bd\u8c61\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3059\u3002

        \u5b9f\u969b\u306b\u306f\u3001\u30d2\u30fc\u30d7\u306f\u512a\u5148\u5ea6\u30ad\u30e5\u30fc\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306b\u3088\u304f\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u6700\u5927\u30d2\u30fc\u30d7\u306f\u3001\u8981\u7d20\u304c\u964d\u9806\u3067\u30c7\u30ad\u30e5\u30fc\u3055\u308c\u308b\u512a\u5148\u5ea6\u30ad\u30e5\u30fc\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002\u4f7f\u7528\u306e\u89b3\u70b9\u304b\u3089\u3001\u300c\u512a\u5148\u5ea6\u30ad\u30e5\u30fc\u300d\u3068\u300c\u30d2\u30fc\u30d7\u300d\u3092\u540c\u7b49\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u3053\u306e\u672c\u3067\u306f\u4e21\u8005\u3092\u7279\u5225\u306b\u533a\u5225\u305b\u305a\u3001\u7d71\u4e00\u3057\u3066\u300c\u30d2\u30fc\u30d7\u300d\u3068\u547c\u3073\u307e\u3059\u3002

        \u30d2\u30fc\u30d7\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c\u3092\u4e0b\u8868\u306b\u793a\u3057\u307e\u3059\u3002\u30e1\u30bd\u30c3\u30c9\u540d\u306f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u3088\u3063\u3066\u7570\u306a\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002

        \u8868 8-1 \u00a0 \u30d2\u30fc\u30d7\u64cd\u4f5c\u306e\u52b9\u7387

        \u30e1\u30bd\u30c3\u30c9\u540d \u8aac\u660e \u6642\u9593\u8a08\u7b97\u91cf push() \u30d2\u30fc\u30d7\u306b\u8981\u7d20\u3092\u8ffd\u52a0 \\(O(\\log n)\\) pop() \u30d2\u30fc\u30d7\u304b\u3089\u5148\u982d\u8981\u7d20\u3092\u524a\u9664 \\(O(\\log n)\\) peek() \u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\uff08\u6700\u5927/\u6700\u5c0f\u30d2\u30fc\u30d7\u306e\u5834\u5408\u3001\u6700\u5927/\u6700\u5c0f\u5024\uff09 \\(O(1)\\) size() \u30d2\u30fc\u30d7\u5185\u306e\u8981\u7d20\u6570\u3092\u53d6\u5f97 \\(O(1)\\) isEmpty() \u30d2\u30fc\u30d7\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af \\(O(1)\\)

        \u5b9f\u969b\u306b\u306f\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u3088\u3063\u3066\u63d0\u4f9b\u3055\u308c\u308b\u30d2\u30fc\u30d7\u30af\u30e9\u30b9\uff08\u307e\u305f\u306f\u512a\u5148\u5ea6\u30ad\u30e5\u30fc\u30af\u30e9\u30b9\uff09\u3092\u76f4\u63a5\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002

        \u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u300c\u6607\u9806\u300d\u3068\u300c\u964d\u9806\u300d\u304c\u3042\u308b\u3088\u3046\u306b\u3001flag\u3092\u8a2d\u5b9a\u3059\u308b\u304bComparator\u3092\u5909\u66f4\u3059\u308b\u3053\u3068\u3067\u300c\u6700\u5c0f\u30d2\u30fc\u30d7\u300d\u3068\u300c\u6700\u5927\u30d2\u30fc\u30d7\u300d\u3092\u5207\u308a\u66ff\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby heap.py
        # \u6700\u5c0f\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\nmin_heap, flag = [], 1\n# \u6700\u5927\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\nmax_heap, flag = [], -1\n\n# Python\u306eheapq\u30e2\u30b8\u30e5\u30fc\u30eb\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u6700\u5c0f\u30d2\u30fc\u30d7\u3092\u5b9f\u88c5\n# \u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u30d7\u30c3\u30b7\u30e5\u3059\u308b\u524d\u306b\u8ca0\u306e\u5024\u306b\u3059\u308b\u3053\u3068\u3067\u3001\u9806\u5e8f\u3092\u53cd\u8ee2\u3055\u305b\u3001\u6700\u5927\u30d2\u30fc\u30d7\u3092\u5b9f\u88c5\n# \u3053\u306e\u4f8b\u3067\u306f\u3001flag = 1\u306f\u6700\u5c0f\u30d2\u30fc\u30d7\u306b\u5bfe\u5fdc\u3057\u3001flag = -1\u306f\u6700\u5927\u30d2\u30fc\u30d7\u306b\u5bfe\u5fdc\n\n# \u30d2\u30fc\u30d7\u306b\u8981\u7d20\u3092\u30d7\u30c3\u30b7\u30e5\nheapq.heappush(max_heap, flag * 1)\nheapq.heappush(max_heap, flag * 3)\nheapq.heappush(max_heap, flag * 2)\nheapq.heappush(max_heap, flag * 5)\nheapq.heappush(max_heap, flag * 4)\n\n# \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u53d6\u5f97\npeek: int = flag * max_heap[0] # 5\n\n# \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u30dd\u30c3\u30d7\n# \u30dd\u30c3\u30d7\u3055\u308c\u305f\u8981\u7d20\u306f\u964d\u9806\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u5f62\u6210\nval = flag * heapq.heappop(max_heap) # 5\nval = flag * heapq.heappop(max_heap) # 4\nval = flag * heapq.heappop(max_heap) # 3\nval = flag * heapq.heappop(max_heap) # 2\nval = flag * heapq.heappop(max_heap) # 1\n\n# \u30d2\u30fc\u30d7\u306e\u30b5\u30a4\u30ba\u3092\u53d6\u5f97\nsize: int = len(max_heap)\n\n# \u30d2\u30fc\u30d7\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\nis_empty: bool = not max_heap\n\n# \u30ea\u30b9\u30c8\u304b\u3089\u30d2\u30fc\u30d7\u3092\u4f5c\u6210\nmin_heap: list[int] = [1, 3, 2, 5, 4]\nheapq.heapify(min_heap)\n
        heap.cpp
        /* \u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316 */\n// \u6700\u5c0f\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\npriority_queue<int, vector<int>, greater<int>> minHeap;\n// \u6700\u5927\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\npriority_queue<int, vector<int>, less<int>> maxHeap;\n\n/* \u30d2\u30fc\u30d7\u306b\u8981\u7d20\u3092\u30d7\u30c3\u30b7\u30e5 */\nmaxHeap.push(1);\nmaxHeap.push(3);\nmaxHeap.push(2);\nmaxHeap.push(5);\nmaxHeap.push(4);\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u53d6\u5f97 */\nint peek = maxHeap.top(); // 5\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\n// \u30dd\u30c3\u30d7\u3055\u308c\u305f\u8981\u7d20\u306f\u964d\u9806\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u5f62\u6210\nmaxHeap.pop(); // 5\nmaxHeap.pop(); // 4\nmaxHeap.pop(); // 3\nmaxHeap.pop(); // 2\nmaxHeap.pop(); // 1\n\n/* \u30d2\u30fc\u30d7\u306e\u30b5\u30a4\u30ba\u3092\u53d6\u5f97 */\nint size = maxHeap.size();\n\n/* \u30d2\u30fc\u30d7\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af */\nbool isEmpty = maxHeap.empty();\n\n/* \u30ea\u30b9\u30c8\u304b\u3089\u30d2\u30fc\u30d7\u3092\u4f5c\u6210 */\nvector<int> input{1, 3, 2, 5, 4};\npriority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());\n
        heap.java
        /* \u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316 */\n// \u6700\u5c0f\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\nQueue<Integer> minHeap = new PriorityQueue<>();\n// \u6700\u5927\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\uff08\u30e9\u30e0\u30c0\u5f0f\u3067Comparator\u3092\u5909\u66f4\u3059\u308b\u3060\u3051\uff09\nQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);\n\n/* \u30d2\u30fc\u30d7\u306b\u8981\u7d20\u3092\u30d7\u30c3\u30b7\u30e5 */\nmaxHeap.offer(1);\nmaxHeap.offer(3);\nmaxHeap.offer(2);\nmaxHeap.offer(5);\nmaxHeap.offer(4);\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u53d6\u5f97 */\nint peek = maxHeap.peek(); // 5\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\n// \u30dd\u30c3\u30d7\u3055\u308c\u305f\u8981\u7d20\u306f\u964d\u9806\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u5f62\u6210\npeek = maxHeap.poll(); // 5\npeek = maxHeap.poll(); // 4\npeek = maxHeap.poll(); // 3\npeek = maxHeap.poll(); // 2\npeek = maxHeap.poll(); // 1\n\n/* \u30d2\u30fc\u30d7\u306e\u30b5\u30a4\u30ba\u3092\u53d6\u5f97 */\nint size = maxHeap.size();\n\n/* \u30d2\u30fc\u30d7\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af */\nboolean isEmpty = maxHeap.isEmpty();\n\n/* \u30ea\u30b9\u30c8\u304b\u3089\u30d2\u30fc\u30d7\u3092\u4f5c\u6210 */\nminHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));\n
        heap.cs
        /* \u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316 */\n// \u6700\u5c0f\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\nPriorityQueue<int, int> minHeap = new();\n// \u6700\u5927\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\uff08\u30e9\u30e0\u30c0\u5f0f\u3067Comparator\u3092\u5909\u66f4\u3059\u308b\u3060\u3051\uff09\nPriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y - x));\n\n/* \u30d2\u30fc\u30d7\u306b\u8981\u7d20\u3092\u30d7\u30c3\u30b7\u30e5 */\nmaxHeap.Enqueue(1, 1);\nmaxHeap.Enqueue(3, 3);\nmaxHeap.Enqueue(2, 2);\nmaxHeap.Enqueue(5, 5);\nmaxHeap.Enqueue(4, 4);\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u53d6\u5f97 */\nint peek = maxHeap.Peek();//5\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\n// \u30dd\u30c3\u30d7\u3055\u308c\u305f\u8981\u7d20\u306f\u964d\u9806\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u5f62\u6210\npeek = maxHeap.Dequeue();  // 5\npeek = maxHeap.Dequeue();  // 4\npeek = maxHeap.Dequeue();  // 3\npeek = maxHeap.Dequeue();  // 2\npeek = maxHeap.Dequeue();  // 1\n\n/* \u30d2\u30fc\u30d7\u306e\u30b5\u30a4\u30ba\u3092\u53d6\u5f97 */\nint size = maxHeap.Count;\n\n/* \u30d2\u30fc\u30d7\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af */\nbool isEmpty = maxHeap.Count == 0;\n\n/* \u30ea\u30b9\u30c8\u304b\u3089\u30d2\u30fc\u30d7\u3092\u4f5c\u6210 */\nminHeap = new PriorityQueue<int, int>([(1, 1), (3, 3), (2, 2), (5, 5), (4, 4)]);\n
        heap.go
        // Go\u3067\u306f\u3001heap.Interface\u3092\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3067\u6574\u6570\u306e\u6700\u5927\u30d2\u30fc\u30d7\u3092\u69cb\u7bc9\u3067\u304d\u307e\u3059\n// heap.Interface\u3092\u5b9f\u88c5\u3059\u308b\u306b\u306f\u3001sort.Interface\u3082\u5b9f\u88c5\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\ntype intHeap []any\n\n// heap.Interface\u306ePush\u30e1\u30bd\u30c3\u30c9\u3001\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u30d7\u30c3\u30b7\u30e5\nfunc (h *intHeap) Push(x any) {\n    // Push\u3068Pop\u306e\u4e21\u65b9\u3067\u30dd\u30a4\u30f3\u30bf\u30ec\u30b7\u30fc\u30d0\u30fc\u3092\u4f7f\u7528\n    // \u30b9\u30e9\u30a4\u30b9\u306e\u8981\u7d20\u3092\u8abf\u6574\u3059\u308b\u3060\u3051\u3067\u306a\u304f\u3001\u305d\u306e\u9577\u3055\u3082\u5909\u66f4\u3059\u308b\u305f\u3081\n    *h = append(*h, x.(int))\n}\n\n// heap.Interface\u306ePop\u30e1\u30bd\u30c3\u30c9\u3001\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u524a\u9664\nfunc (h *intHeap) Pop() any {\n    // \u30d2\u30fc\u30d7\u304b\u3089\u30dd\u30c3\u30d7\u3059\u308b\u8981\u7d20\u306f\u672b\u5c3e\u306b\u683c\u7d0d\n    last := (*h)[len(*h)-1]\n    *h = (*h)[:len(*h)-1]\n    return last\n}\n\n// sort.Interface\u306eLen\u30e1\u30bd\u30c3\u30c9\nfunc (h *intHeap) Len() int {\n    return len(*h)\n}\n\n// sort.Interface\u306eLess\u30e1\u30bd\u30c3\u30c9\nfunc (h *intHeap) Less(i, j int) bool {\n    // \u6700\u5c0f\u30d2\u30fc\u30d7\u3092\u5b9f\u88c5\u3057\u305f\u3044\u5834\u5408\u306f\u3001\u3053\u308c\u3092\u5c0f\u306a\u308a\u6bd4\u8f03\u306b\u5909\u66f4\n    return (*h)[i].(int) > (*h)[j].(int)\n}\n\n// sort.Interface\u306eSwap\u30e1\u30bd\u30c3\u30c9\nfunc (h *intHeap) Swap(i, j int) {\n    (*h)[i], (*h)[j] = (*h)[j], (*h)[i]\n}\n\n// Top \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u53d6\u5f97\nfunc (h *intHeap) Top() any {\n    return (*h)[0]\n}\n\n/* \u30c9\u30e9\u30a4\u30d0\u30fc\u30b3\u30fc\u30c9 */\nfunc TestHeap(t *testing.T) {\n    /* \u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316 */\n    // \u6700\u5927\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\n    maxHeap := &intHeap{}\n    heap.Init(maxHeap)\n    /* \u30d2\u30fc\u30d7\u306b\u8981\u7d20\u3092\u30d7\u30c3\u30b7\u30e5 */\n    // heap.Interface\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u3066\u8981\u7d20\u3092\u8ffd\u52a0\n    heap.Push(maxHeap, 1)\n    heap.Push(maxHeap, 3)\n    heap.Push(maxHeap, 2)\n    heap.Push(maxHeap, 4)\n    heap.Push(maxHeap, 5)\n\n    /* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u53d6\u5f97 */\n    top := maxHeap.Top()\n    fmt.Printf(\"\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u306f %d\\n\", top)\n\n    /* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\n    // heap.Interface\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u3066\u8981\u7d20\u3092\u524a\u9664\n    heap.Pop(maxHeap) // 5\n    heap.Pop(maxHeap) // 4\n    heap.Pop(maxHeap) // 3\n    heap.Pop(maxHeap) // 2\n    heap.Pop(maxHeap) // 1\n\n    /* \u30d2\u30fc\u30d7\u306e\u30b5\u30a4\u30ba\u3092\u53d6\u5f97 */\n    size := len(*maxHeap)\n    fmt.Printf(\"\u30d2\u30fc\u30d7\u5185\u306e\u8981\u7d20\u6570\u306f %d\\n\", size)\n\n    /* \u30d2\u30fc\u30d7\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af */\n    isEmpty := len(*maxHeap) == 0\n    fmt.Printf(\"\u30d2\u30fc\u30d7\u306f\u7a7a\u3067\u3059\u304b\uff1f %t\\n\", isEmpty)\n}\n
        heap.swift
        /* \u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316 */\n// Swift\u306eHeap\u578b\u306f\u6700\u5927\u30d2\u30fc\u30d7\u3068\u6700\u5c0f\u30d2\u30fc\u30d7\u306e\u4e21\u65b9\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3001swift-collections\u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u5fc5\u8981\nvar heap = Heap<Int>()\n\n/* \u30d2\u30fc\u30d7\u306b\u8981\u7d20\u3092\u30d7\u30c3\u30b7\u30e5 */\nheap.insert(1)\nheap.insert(3)\nheap.insert(2)\nheap.insert(5)\nheap.insert(4)\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u53d6\u5f97 */\nvar peek = heap.max()!\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\npeek = heap.removeMax() // 5\npeek = heap.removeMax() // 4\npeek = heap.removeMax() // 3\npeek = heap.removeMax() // 2\npeek = heap.removeMax() // 1\n\n/* \u30d2\u30fc\u30d7\u306e\u30b5\u30a4\u30ba\u3092\u53d6\u5f97 */\nlet size = heap.count\n\n/* \u30d2\u30fc\u30d7\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af */\nlet isEmpty = heap.isEmpty\n\n/* \u30ea\u30b9\u30c8\u304b\u3089\u30d2\u30fc\u30d7\u3092\u4f5c\u6210 */\nlet heap2 = Heap([1, 3, 2, 5, 4])\n
        heap.js
        // JavaScript\u306f\u7d44\u307f\u8fbc\u307f\u306eHeap\u30af\u30e9\u30b9\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        heap.ts
        // TypeScript\u306f\u7d44\u307f\u8fbc\u307f\u306eHeap\u30af\u30e9\u30b9\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        heap.dart
        // Dart\u306f\u7d44\u307f\u8fbc\u307f\u306eHeap\u30af\u30e9\u30b9\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        heap.rs
        use std::collections::BinaryHeap;\nuse std::cmp::Reverse;\n\n/* \u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316 */\n// \u6700\u5c0f\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\nlet mut min_heap = BinaryHeap::<Reverse<i32>>::new();\n// \u6700\u5927\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\nlet mut max_heap = BinaryHeap::new();\n\n/* \u30d2\u30fc\u30d7\u306b\u8981\u7d20\u3092\u30d7\u30c3\u30b7\u30e5 */\nmax_heap.push(1);\nmax_heap.push(3);\nmax_heap.push(2);\nmax_heap.push(5);\nmax_heap.push(4);\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u53d6\u5f97 */\nlet peek = max_heap.peek().unwrap();  // 5\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\n// \u30dd\u30c3\u30d7\u3055\u308c\u305f\u8981\u7d20\u306f\u964d\u9806\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u5f62\u6210\nlet peek = max_heap.pop().unwrap();   // 5\nlet peek = max_heap.pop().unwrap();   // 4\nlet peek = max_heap.pop().unwrap();   // 3\nlet peek = max_heap.pop().unwrap();   // 2\nlet peek = max_heap.pop().unwrap();   // 1\n\n/* \u30d2\u30fc\u30d7\u306e\u30b5\u30a4\u30ba\u3092\u53d6\u5f97 */\nlet size = max_heap.len();\n\n/* \u30d2\u30fc\u30d7\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af */\nlet is_empty = max_heap.is_empty();\n\n/* \u30ea\u30b9\u30c8\u304b\u3089\u30d2\u30fc\u30d7\u3092\u4f5c\u6210 */\nlet min_heap = BinaryHeap::from(vec![Reverse(1), Reverse(3), Reverse(2), Reverse(5), Reverse(4)]);\n
        heap.c
        // C\u306f\u7d44\u307f\u8fbc\u307f\u306eHeap\u30af\u30e9\u30b9\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        heap.kt
        /* \u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316 */\n// \u6700\u5c0f\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\nvar minHeap = PriorityQueue<Int>()\n// \u6700\u5927\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\uff08\u30e9\u30e0\u30c0\u5f0f\u3067Comparator\u3092\u5909\u66f4\u3059\u308b\u3060\u3051\uff09\nval maxHeap = PriorityQueue { a: Int, b: Int -> b - a }\n\n/* \u30d2\u30fc\u30d7\u306b\u8981\u7d20\u3092\u30d7\u30c3\u30b7\u30e5 */\nmaxHeap.offer(1)\nmaxHeap.offer(3)\nmaxHeap.offer(2)\nmaxHeap.offer(5)\nmaxHeap.offer(4)\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u53d6\u5f97 */\nvar peek = maxHeap.peek() // 5\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\n// \u30dd\u30c3\u30d7\u3055\u308c\u305f\u8981\u7d20\u306f\u964d\u9806\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u5f62\u6210\npeek = maxHeap.poll() // 5\npeek = maxHeap.poll() // 4\npeek = maxHeap.poll() // 3\npeek = maxHeap.poll() // 2\npeek = maxHeap.poll() // 1\n\n/* \u30d2\u30fc\u30d7\u306e\u30b5\u30a4\u30ba\u3092\u53d6\u5f97 */\nval size = maxHeap.size\n\n/* \u30d2\u30fc\u30d7\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af */\nval isEmpty = maxHeap.isEmpty()\n\n/* \u30ea\u30b9\u30c8\u304b\u3089\u30d2\u30fc\u30d7\u3092\u4f5c\u6210 */\nminHeap = PriorityQueue(mutableListOf(1, 3, 2, 5, 4))\n
        heap.rb
        \n
        "},{"location":"chapter_heap/heap/#812","title":"8.1.2 \u00a0 \u30d2\u30fc\u30d7\u306e\u5b9f\u88c5","text":"

        \u4ee5\u4e0b\u306e\u5b9f\u88c5\u306f\u6700\u5927\u30d2\u30fc\u30d7\u3067\u3059\u3002\u6700\u5c0f\u30d2\u30fc\u30d7\u306b\u5909\u63db\u3059\u308b\u306b\u306f\u3001\u3059\u3079\u3066\u306e\u30b5\u30a4\u30ba\u8ad6\u7406\u6bd4\u8f03\u3092\u53cd\u8ee2\u3055\u305b\u308b\u3060\u3051\u3067\u3059\uff08\u4f8b\u3048\u3070\u3001\\(\\geq\\)\u3092\\(\\leq\\)\u306b\u7f6e\u304d\u63db\u3048\u308b\uff09\u3002\u8208\u5473\u306e\u3042\u308b\u8aad\u8005\u306f\u81ea\u5206\u3067\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002

        "},{"location":"chapter_heap/heap/#1","title":"1. \u00a0 \u30d2\u30fc\u30d7\u306e\u683c\u7d0d\u3068\u8868\u73fe","text":"

        \u300c\u4e8c\u5206\u6728\u300d\u306e\u7bc0\u3067\u8ff0\u3079\u305f\u3088\u3046\u306b\u3001\u5b8c\u5099\u4e8c\u5206\u6728\u306f\u914d\u5217\u8868\u73fe\u306b\u975e\u5e38\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u30d2\u30fc\u30d7\u306f\u5b8c\u5099\u4e8c\u5206\u6728\u306e\u4e00\u7a2e\u306a\u306e\u3067\u3001\u914d\u5217\u3092\u4f7f\u7528\u3057\u3066\u30d2\u30fc\u30d7\u3092\u683c\u7d0d\u3057\u307e\u3059\u3002

        \u914d\u5217\u3092\u4f7f\u7528\u3057\u3066\u4e8c\u5206\u6728\u3092\u8868\u73fe\u3059\u308b\u5834\u5408\u3001\u8981\u7d20\u306f\u30ce\u30fc\u30c9\u5024\u3092\u8868\u3057\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u4e8c\u5206\u6728\u5185\u306e\u30ce\u30fc\u30c9\u4f4d\u7f6e\u3092\u8868\u3057\u307e\u3059\u3002\u30ce\u30fc\u30c9\u30dd\u30a4\u30f3\u30bf\u306f\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u30de\u30c3\u30d4\u30f3\u30b0\u516c\u5f0f\u3092\u901a\u3058\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(i\\)\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u305d\u306e\u5de6\u306e\u5b50\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\\(2i + 1\\)\u3001\u53f3\u306e\u5b50\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\\(2i + 2\\)\u3001\u89aa\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\\((i - 1) / 2\\)\uff08\u5e8a\u9664\u7b97\uff09\u3067\u3059\u3002\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001null\u30ce\u30fc\u30c9\u307e\u305f\u306f\u30ce\u30fc\u30c9\u304c\u5b58\u5728\u3057\u306a\u3044\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

        \u56f3 8-2 \u00a0 \u30d2\u30fc\u30d7\u306e\u8868\u73fe\u3068\u683c\u7d0d

        \u5f8c\u3067\u4fbf\u5229\u306b\u4f7f\u7528\u3059\u308b\u305f\u3081\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u30de\u30c3\u30d4\u30f3\u30b0\u516c\u5f0f\u3092\u95a2\u6570\u306b\u30ab\u30d7\u30bb\u30eb\u5316\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
        def left(self, i: int) -> int:\n    \"\"\"\u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\"\"\"\n    return 2 * i + 1\n\ndef right(self, i: int) -> int:\n    \"\"\"\u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\"\"\"\n    return 2 * i + 2\n\ndef parent(self, i: int) -> int:\n    \"\"\"\u89aa\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\"\"\"\n    return (i - 1) // 2  # \u6574\u6570\u9664\u7b97\u3067\u5207\u308a\u4e0b\u3052\n
        my_heap.cpp
        /* \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\nint left(int i) {\n    return 2 * i + 1;\n}\n\n/* \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\nint right(int i) {\n    return 2 * i + 2;\n}\n\n/* \u89aa\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\nint parent(int i) {\n    return (i - 1) / 2; // \u6574\u6570\u9664\u7b97\u3067\u5207\u308a\u4e0b\u3052\n}\n
        my_heap.java
        /* \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\nint left(int i) {\n    return 2 * i + 1;\n}\n\n/* \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\nint right(int i) {\n    return 2 * i + 2;\n}\n\n/* \u89aa\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\nint parent(int i) {\n    return (i - 1) / 2; // \u6574\u6570\u9664\u7b97\u3067\u5207\u308a\u4e0b\u3052\n}\n
        my_heap.cs
        [class]{MaxHeap}-[func]{Left}\n\n[class]{MaxHeap}-[func]{Right}\n\n[class]{MaxHeap}-[func]{Parent}\n
        my_heap.go
        [class]{maxHeap}-[func]{left}\n\n[class]{maxHeap}-[func]{right}\n\n[class]{maxHeap}-[func]{parent}\n
        my_heap.swift
        [class]{MaxHeap}-[func]{left}\n\n[class]{MaxHeap}-[func]{right}\n\n[class]{MaxHeap}-[func]{parent}\n
        my_heap.js
        [class]{MaxHeap}-[func]{left}\n\n[class]{MaxHeap}-[func]{right}\n\n[class]{MaxHeap}-[func]{parent}\n
        my_heap.ts
        [class]{MaxHeap}-[func]{left}\n\n[class]{MaxHeap}-[func]{right}\n\n[class]{MaxHeap}-[func]{parent}\n
        my_heap.dart
        [class]{MaxHeap}-[func]{_left}\n\n[class]{MaxHeap}-[func]{_right}\n\n[class]{MaxHeap}-[func]{_parent}\n
        my_heap.rs
        [class]{MaxHeap}-[func]{left}\n\n[class]{MaxHeap}-[func]{right}\n\n[class]{MaxHeap}-[func]{parent}\n
        my_heap.c
        [class]{MaxHeap}-[func]{left}\n\n[class]{MaxHeap}-[func]{right}\n\n[class]{MaxHeap}-[func]{parent}\n
        my_heap.kt
        [class]{MaxHeap}-[func]{left}\n\n[class]{MaxHeap}-[func]{right}\n\n[class]{MaxHeap}-[func]{parent}\n
        my_heap.rb
        [class]{MaxHeap}-[func]{left}\n\n[class]{MaxHeap}-[func]{right}\n\n[class]{MaxHeap}-[func]{parent}\n
        "},{"location":"chapter_heap/heap/#2","title":"2. \u00a0 \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3078\u306e\u30a2\u30af\u30bb\u30b9","text":"

        \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u306f\u4e8c\u5206\u6728\u306e\u6839\u30ce\u30fc\u30c9\u3067\u3001\u30ea\u30b9\u30c8\u306e\u6700\u521d\u306e\u8981\u7d20\u3067\u3082\u3042\u308a\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
        def peek(self) -> int:\n    \"\"\"\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\"\"\"\n    return self.max_heap[0]\n
        my_heap.cpp
        /* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint peek() {\n    return maxHeap[0];\n}\n
        my_heap.java
        /* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint peek() {\n    return maxHeap.get(0);\n}\n
        my_heap.cs
        [class]{MaxHeap}-[func]{Peek}\n
        my_heap.go
        [class]{maxHeap}-[func]{peek}\n
        my_heap.swift
        [class]{MaxHeap}-[func]{peek}\n
        my_heap.js
        [class]{MaxHeap}-[func]{peek}\n
        my_heap.ts
        [class]{MaxHeap}-[func]{peek}\n
        my_heap.dart
        [class]{MaxHeap}-[func]{peek}\n
        my_heap.rs
        [class]{MaxHeap}-[func]{peek}\n
        my_heap.c
        [class]{MaxHeap}-[func]{peek}\n
        my_heap.kt
        [class]{MaxHeap}-[func]{peek}\n
        my_heap.rb
        [class]{MaxHeap}-[func]{peek}\n
        "},{"location":"chapter_heap/heap/#3","title":"3. \u00a0 \u30d2\u30fc\u30d7\u3078\u306e\u8981\u7d20\u633f\u5165","text":"

        \u8981\u7d20val\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u307e\u305a\u305d\u308c\u3092\u30d2\u30fc\u30d7\u306e\u5e95\u306b\u8ffd\u52a0\u3057\u307e\u3059\u3002\u8ffd\u52a0\u5f8c\u3001val\u304c\u30d2\u30fc\u30d7\u5185\u306e\u4ed6\u306e\u8981\u7d20\u3088\u308a\u5927\u304d\u3044\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3001\u30d2\u30fc\u30d7\u306e\u5b8c\u5168\u6027\u304c\u640d\u306a\u308f\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u633f\u5165\u3055\u308c\u305f\u30ce\u30fc\u30c9\u304b\u3089\u6839\u30ce\u30fc\u30c9\u307e\u3067\u306e\u30d1\u30b9\u3092\u4fee\u5fa9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u64cd\u4f5c\u306f\u30d2\u30fc\u30d7\u5316\u3068\u547c\u3070\u308c\u307e\u3059\u3002

        \u633f\u5165\u3055\u308c\u305f\u30ce\u30fc\u30c9\u304b\u3089\u958b\u59cb\u3057\u3066\u3001\u4e0b\u304b\u3089\u4e0a\u306b\u30d2\u30fc\u30d7\u5316\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u633f\u5165\u3055\u308c\u305f\u30ce\u30fc\u30c9\u306e\u5024\u3092\u305d\u306e\u89aa\u30ce\u30fc\u30c9\u3068\u6bd4\u8f03\u3057\u3001\u633f\u5165\u3055\u308c\u305f\u30ce\u30fc\u30c9\u304c\u5927\u304d\u3044\u5834\u5408\u306f\u305d\u308c\u3089\u3092\u4ea4\u63db\u3057\u307e\u3059\u3002\u6b21\u306b\u3053\u306e\u64cd\u4f5c\u3092\u7d9a\u884c\u3057\u3001\u6839\u306b\u5230\u9054\u3059\u308b\u304b\u3001\u4ea4\u63db\u304c\u4e0d\u8981\u306a\u30ce\u30fc\u30c9\u306b\u906d\u9047\u3059\u308b\u307e\u3067\u3001\u4e0b\u304b\u3089\u4e0a\u306b\u30d2\u30fc\u30d7\u5185\u306e\u5404\u30ce\u30fc\u30c9\u3092\u4fee\u5fa9\u3057\u307e\u3059\u3002

        <1><2><3><4><5><6><7><8><9>

        \u56f3 8-3 \u00a0 \u30d2\u30fc\u30d7\u3078\u306e\u8981\u7d20\u633f\u5165\u306e\u624b\u9806

        \u7dcf\u30ce\u30fc\u30c9\u6570\u3092\\(n\\)\u3068\u3059\u308b\u3068\u3001\u6728\u306e\u9ad8\u3055\u306f\\(O(\\log n)\\)\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30d2\u30fc\u30d7\u5316\u64cd\u4f5c\u306e\u30eb\u30fc\u30d7\u53cd\u5fa9\u56de\u6570\u306f\u6700\u5927\\(O(\\log n)\\)\u3067\u3001\u8981\u7d20\u633f\u5165\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(\\log n)\\)\u306b\u306a\u308a\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
        def push(self, val: int):\n    \"\"\"\u30d2\u30fc\u30d7\u306b\u8981\u7d20\u3092\u30d7\u30c3\u30b7\u30e5\"\"\"\n    # \u30ce\u30fc\u30c9\u3092\u8ffd\u52a0\n    self.max_heap.append(val)\n    # \u4e0b\u304b\u3089\u4e0a\u3078\u30d2\u30fc\u30d7\u5316\n    self.sift_up(self.size() - 1)\n\ndef sift_up(self, i: int):\n    \"\"\"\u30ce\u30fc\u30c9i\u304b\u3089\u958b\u59cb\u3057\u3066\u3001\u4e0b\u304b\u3089\u4e0a\u3078\u30d2\u30fc\u30d7\u5316\"\"\"\n    while True:\n        # \u30ce\u30fc\u30c9i\u306e\u89aa\u30ce\u30fc\u30c9\u3092\u53d6\u5f97\n        p = self.parent(i)\n        # \u300c\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u8d8a\u3048\u308b\u300d\u307e\u305f\u306f\u300c\u30ce\u30fc\u30c9\u304c\u4fee\u5fa9\u4e0d\u8981\u300d\u306e\u5834\u5408\u3001\u30d2\u30fc\u30d7\u5316\u3092\u7d42\u4e86\n        if p < 0 or self.max_heap[i] <= self.max_heap[p]:\n            break\n        # 2\u3064\u306e\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\n        self.swap(i, p)\n        # \u4e0a\u5411\u304d\u306e\u30eb\u30fc\u30d7\u30d2\u30fc\u30d7\u5316\n        i = p\n
        my_heap.cpp
        /* \u30d2\u30fc\u30d7\u306b\u8981\u7d20\u3092\u30d7\u30c3\u30b7\u30e5 */\nvoid push(int val) {\n    // \u30ce\u30fc\u30c9\u3092\u8ffd\u52a0\n    maxHeap.push_back(val);\n    // \u4e0b\u304b\u3089\u4e0a\u3078\u30d2\u30fc\u30d7\u5316\n    siftUp(size() - 1);\n}\n\n/* \u30ce\u30fc\u30c9i\u304b\u3089\u4e0a\u5411\u304d\u306b\u30d2\u30fc\u30d7\u5316\u3092\u958b\u59cb */\nvoid siftUp(int i) {\n    while (true) {\n        // \u30ce\u30fc\u30c9i\u306e\u89aa\u30ce\u30fc\u30c9\u3092\u53d6\u5f97\n        int p = parent(i);\n        // \u300c\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u8d85\u3048\u308b\u300d\u307e\u305f\u306f\u300c\u30ce\u30fc\u30c9\u304c\u4fee\u5fa9\u4e0d\u8981\u300d\u306e\u5834\u5408\u3001\u30d2\u30fc\u30d7\u5316\u3092\u7d42\u4e86\n        if (p < 0 || maxHeap[i] <= maxHeap[p])\n            break;\n        // 2\u3064\u306e\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\n        swap(maxHeap[i], maxHeap[p]);\n        // \u4e0a\u5411\u304d\u306b\u30eb\u30fc\u30d7\u3057\u3066\u30d2\u30fc\u30d7\u5316\n        i = p;\n    }\n}\n
        my_heap.java
        /* \u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u30d7\u30c3\u30b7\u30e5 */\nvoid push(int val) {\n    // \u30ce\u30fc\u30c9\u3092\u8ffd\u52a0\n    maxHeap.add(val);\n    // \u4e0b\u304b\u3089\u4e0a\u3078\u30d2\u30fc\u30d7\u5316\n    siftUp(size() - 1);\n}\n\n/* \u30ce\u30fc\u30c9 i \u304b\u3089\u4e0a\u5411\u304d\u306b\u30d2\u30fc\u30d7\u5316\u3092\u958b\u59cb */\nvoid siftUp(int i) {\n    while (true) {\n        // \u30ce\u30fc\u30c9 i \u306e\u89aa\u30ce\u30fc\u30c9\u3092\u53d6\u5f97\n        int p = parent(i);\n        // \u300c\u6839\u30ce\u30fc\u30c9\u3092\u8d8a\u3048\u308b\u300d\u307e\u305f\u306f\u300c\u30ce\u30fc\u30c9\u304c\u4fee\u5fa9\u4e0d\u8981\u300d\u306e\u5834\u5408\u3001\u30d2\u30fc\u30d7\u5316\u3092\u7d42\u4e86\n        if (p < 0 || maxHeap.get(i) <= maxHeap.get(p))\n            break;\n        // 2\u3064\u306e\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\n        swap(i, p);\n        // \u4e0a\u5411\u304d\u306b\u30d2\u30fc\u30d7\u5316\u3092\u30eb\u30fc\u30d7\n        i = p;\n    }\n}\n
        my_heap.cs
        [class]{MaxHeap}-[func]{Push}\n\n[class]{MaxHeap}-[func]{SiftUp}\n
        my_heap.go
        [class]{maxHeap}-[func]{push}\n\n[class]{maxHeap}-[func]{siftUp}\n
        my_heap.swift
        [class]{MaxHeap}-[func]{push}\n\n[class]{MaxHeap}-[func]{siftUp}\n
        my_heap.js
        [class]{MaxHeap}-[func]{push}\n\n[class]{MaxHeap}-[func]{siftUp}\n
        my_heap.ts
        [class]{MaxHeap}-[func]{push}\n\n[class]{MaxHeap}-[func]{siftUp}\n
        my_heap.dart
        [class]{MaxHeap}-[func]{push}\n\n[class]{MaxHeap}-[func]{siftUp}\n
        my_heap.rs
        [class]{MaxHeap}-[func]{push}\n\n[class]{MaxHeap}-[func]{sift_up}\n
        my_heap.c
        [class]{MaxHeap}-[func]{push}\n\n[class]{MaxHeap}-[func]{siftUp}\n
        my_heap.kt
        [class]{MaxHeap}-[func]{push}\n\n[class]{MaxHeap}-[func]{siftUp}\n
        my_heap.rb
        [class]{MaxHeap}-[func]{push}\n\n[class]{MaxHeap}-[func]{sift_up}\n
        "},{"location":"chapter_heap/heap/#4","title":"4. \u00a0 \u30d2\u30fc\u30d7\u304b\u3089\u306e\u5148\u982d\u8981\u7d20\u524a\u9664","text":"

        \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u306f\u4e8c\u5206\u6728\u306e\u6839\u30ce\u30fc\u30c9\u3001\u3064\u307e\u308a\u30ea\u30b9\u30c8\u306e\u6700\u521d\u306e\u8981\u7d20\u3067\u3059\u3002\u30ea\u30b9\u30c8\u304b\u3089\u6700\u521d\u306e\u8981\u7d20\u3092\u76f4\u63a5\u524a\u9664\u3059\u308b\u3068\u3001\u4e8c\u5206\u6728\u5185\u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u5909\u66f4\u3055\u308c\u3001\u5f8c\u7d9a\u306e\u4fee\u5fa9\u306b\u30d2\u30fc\u30d7\u5316\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u56f0\u96e3\u306b\u306a\u308a\u307e\u3059\u3002\u8981\u7d20\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u5909\u66f4\u3092\u6700\u5c0f\u9650\u306b\u6291\u3048\u308b\u305f\u3081\u3001\u6b21\u306e\u624b\u9806\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        1. \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3068\u5e95\u306e\u8981\u7d20\u3092\u4ea4\u63db\u3057\u307e\u3059\uff08\u6839\u30ce\u30fc\u30c9\u3068\u6700\u3082\u53f3\u306e\u8449\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\uff09\u3002
        2. \u4ea4\u63db\u5f8c\u3001\u30ea\u30b9\u30c8\u304b\u3089\u30d2\u30fc\u30d7\u306e\u5e95\u3092\u524a\u9664\u3057\u307e\u3059\uff08\u4ea4\u63db\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u5b9f\u969b\u306b\u306f\u5143\u306e\u5148\u982d\u8981\u7d20\u304c\u524a\u9664\u3055\u308c\u308b\uff09\u3002
        3. \u6839\u30ce\u30fc\u30c9\u304b\u3089\u958b\u59cb\u3057\u3066\u3001\u4e0a\u304b\u3089\u4e0b\u306b\u30d2\u30fc\u30d7\u5316\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u300c\u4e0a\u304b\u3089\u4e0b\u306e\u30d2\u30fc\u30d7\u5316\u300d\u306e\u65b9\u5411\u306f\u300c\u4e0b\u304b\u3089\u4e0a\u306e\u30d2\u30fc\u30d7\u5316\u300d\u3068\u53cd\u5bfe\u3067\u3059\u3002\u6839\u30ce\u30fc\u30c9\u306e\u5024\u3092\u305d\u306e2\u3064\u306e\u5b50\u3068\u6bd4\u8f03\u3057\u3001\u6700\u5927\u306e\u5b50\u3068\u4ea4\u63db\u3057\u307e\u3059\u3002\u6b21\u306b\u3001\u8449\u30ce\u30fc\u30c9\u306b\u5230\u9054\u3059\u308b\u304b\u3001\u4ea4\u63db\u304c\u4e0d\u8981\u306a\u30ce\u30fc\u30c9\u306b\u906d\u9047\u3059\u308b\u307e\u3067\u3001\u3053\u306e\u64cd\u4f5c\u3092\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002

        <1><2><3><4><5><6><7><8><9><10>

        \u56f3 8-4 \u00a0 \u30d2\u30fc\u30d7\u304b\u3089\u306e\u5148\u982d\u8981\u7d20\u524a\u9664\u306e\u624b\u9806

        \u8981\u7d20\u633f\u5165\u64cd\u4f5c\u3068\u540c\u69d8\u306b\u3001\u5148\u982d\u8981\u7d20\u524a\u9664\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3082\\(O(\\log n)\\)\u3067\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
        def pop(self) -> int:\n    \"\"\"\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u304b\u3089\u51fa\u3059\"\"\"\n    # \u7a7a\u306e\u51e6\u7406\n    if self.is_empty():\n        raise IndexError(\"Heap is empty\")\n    # \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3068\u6700\u53f3\u7aef\u306e\u8449\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\uff08\u6700\u521d\u306e\u8981\u7d20\u3068\u6700\u5f8c\u306e\u8981\u7d20\u3092\u4ea4\u63db\uff09\n    self.swap(0, self.size() - 1)\n    # \u30ce\u30fc\u30c9\u3092\u524a\u9664\n    val = self.max_heap.pop()\n    # \u4e0a\u304b\u3089\u4e0b\u3078\u30d2\u30fc\u30d7\u5316\n    self.sift_down(0)\n    # \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u8fd4\u3059\n    return val\n\ndef sift_down(self, i: int):\n    \"\"\"\u30ce\u30fc\u30c9i\u304b\u3089\u958b\u59cb\u3057\u3066\u3001\u4e0a\u304b\u3089\u4e0b\u3078\u30d2\u30fc\u30d7\u5316\"\"\"\n    while True:\n        # i\u3001l\u3001r\u306e\u4e2d\u3067\u6700\u5927\u306e\u30ce\u30fc\u30c9\u3092\u6c7a\u5b9a\u3057\u3001ma\u3068\u3059\u308b\n        l, r, ma = self.left(i), self.right(i), i\n        if l < self.size() and self.max_heap[l] > self.max_heap[ma]:\n            ma = l\n        if r < self.size() and self.max_heap[r] > self.max_heap[ma]:\n            ma = r\n        # \u30ce\u30fc\u30c9i\u304c\u6700\u5927\u307e\u305f\u306f\u30a4\u30f3\u30c7\u30c3\u30af\u30b9l\u3001r\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001\u3055\u3089\u306a\u308b\u30d2\u30fc\u30d7\u5316\u306f\u4e0d\u8981\u3001\u30d6\u30ec\u30fc\u30af\n        if ma == i:\n            break\n        # 2\u3064\u306e\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\n        self.swap(i, ma)\n        # \u4e0b\u5411\u304d\u306e\u30eb\u30fc\u30d7\u30d2\u30fc\u30d7\u5316\n        i = ma\n
        my_heap.cpp
        /* \u8981\u7d20\u304c\u30d2\u30fc\u30d7\u304b\u3089\u9000\u51fa */\nvoid pop() {\n    // \u7a7a\u306e\u51e6\u7406\n    if (isEmpty()) {\n        throw out_of_range(\"Heap is empty\");\n    }\n    // \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u6700\u3082\u53f3\u306e\u8449\u30ce\u30fc\u30c9\u3068\u4ea4\u63db\uff08\u6700\u521d\u306e\u8981\u7d20\u3068\u6700\u5f8c\u306e\u8981\u7d20\u3092\u4ea4\u63db\uff09\n    swap(maxHeap[0], maxHeap[size() - 1]);\n    // \u30ce\u30fc\u30c9\u3092\u524a\u9664\n    maxHeap.pop_back();\n    // \u4e0a\u304b\u3089\u4e0b\u3078\u30d2\u30fc\u30d7\u5316\n    siftDown(0);\n}\n\n/* \u30ce\u30fc\u30c9i\u304b\u3089\u4e0b\u5411\u304d\u306b\u30d2\u30fc\u30d7\u5316\u3092\u958b\u59cb */\nvoid siftDown(int i) {\n    while (true) {\n        // i\u3001l\u3001r\u306e\u4e2d\u3067\u6700\u5927\u306e\u30ce\u30fc\u30c9\u3092\u6c7a\u5b9a\u3057\u3001ma\u3068\u3057\u3066\u8a18\u9332\n        int l = left(i), r = right(i), ma = i;\n        if (l < size() && maxHeap[l] > maxHeap[ma])\n            ma = l;\n        if (r < size() && maxHeap[r] > maxHeap[ma])\n            ma = r;\n        // \u30ce\u30fc\u30c9i\u304c\u6700\u5927\u3001\u307e\u305f\u306f\u30a4\u30f3\u30c7\u30c3\u30af\u30b9l\u3001r\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001\u3053\u308c\u4ee5\u4e0a\u306e\u30d2\u30fc\u30d7\u5316\u306f\u4e0d\u8981\u3001\u30d6\u30ec\u30fc\u30af\n        if (ma == i)\n            break;\n        swap(maxHeap[i], maxHeap[ma]);\n        // \u4e0b\u5411\u304d\u306b\u30eb\u30fc\u30d7\u3057\u3066\u30d2\u30fc\u30d7\u5316\n        i = ma;\n    }\n}\n
        my_heap.java
        /* \u8981\u7d20\u304c\u30d2\u30fc\u30d7\u304b\u3089\u9000\u51fa */\nint pop() {\n    // \u7a7a\u306e\u51e6\u7406\n    if (isEmpty())\n        throw new IndexOutOfBoundsException();\n    // \u6839\u30ce\u30fc\u30c9\u3092\u6700\u3082\u53f3\u306e\u8449\u30ce\u30fc\u30c9\u3068\u4ea4\u63db\uff08\u6700\u521d\u306e\u8981\u7d20\u3092\u6700\u5f8c\u306e\u8981\u7d20\u3068\u4ea4\u63db\uff09\n    swap(0, size() - 1);\n    // \u30ce\u30fc\u30c9\u3092\u524a\u9664\n    int val = maxHeap.remove(size() - 1);\n    // \u4e0a\u304b\u3089\u4e0b\u3078\u30d2\u30fc\u30d7\u5316\n    siftDown(0);\n    // \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u8fd4\u3059\n    return val;\n}\n\n/* \u30ce\u30fc\u30c9 i \u304b\u3089\u4e0b\u5411\u304d\u306b\u30d2\u30fc\u30d7\u5316\u3092\u958b\u59cb */\nvoid siftDown(int i) {\n    while (true) {\n        // i\u3001l\u3001r \u306e\u4e2d\u3067\u6700\u5927\u306e\u30ce\u30fc\u30c9\u3092\u6c7a\u5b9a\u3057\u3001ma \u3068\u3059\u308b\n        int l = left(i), r = right(i), ma = i;\n        if (l < size() && maxHeap.get(l) > maxHeap.get(ma))\n            ma = l;\n        if (r < size() && maxHeap.get(r) > maxHeap.get(ma))\n            ma = r;\n        // \u30ce\u30fc\u30c9 i \u304c\u6700\u5927\u306e\u5834\u5408\u3001\u307e\u305f\u306f\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 l\u3001r \u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001\u3055\u3089\u306a\u308b\u30d2\u30fc\u30d7\u5316\u306f\u4e0d\u8981\u3001\u7d42\u4e86\n        if (ma == i)\n            break;\n        // 2\u3064\u306e\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\n        swap(i, ma);\n        // \u4e0b\u5411\u304d\u306b\u30d2\u30fc\u30d7\u5316\u3092\u30eb\u30fc\u30d7\n        i = ma;\n    }\n}\n
        my_heap.cs
        [class]{MaxHeap}-[func]{Pop}\n\n[class]{MaxHeap}-[func]{SiftDown}\n
        my_heap.go
        [class]{maxHeap}-[func]{pop}\n\n[class]{maxHeap}-[func]{siftDown}\n
        my_heap.swift
        [class]{MaxHeap}-[func]{pop}\n\n[class]{MaxHeap}-[func]{siftDown}\n
        my_heap.js
        [class]{MaxHeap}-[func]{pop}\n\n[class]{MaxHeap}-[func]{siftDown}\n
        my_heap.ts
        [class]{MaxHeap}-[func]{pop}\n\n[class]{MaxHeap}-[func]{siftDown}\n
        my_heap.dart
        [class]{MaxHeap}-[func]{pop}\n\n[class]{MaxHeap}-[func]{siftDown}\n
        my_heap.rs
        [class]{MaxHeap}-[func]{pop}\n\n[class]{MaxHeap}-[func]{sift_down}\n
        my_heap.c
        [class]{MaxHeap}-[func]{pop}\n\n[class]{MaxHeap}-[func]{siftDown}\n
        my_heap.kt
        [class]{MaxHeap}-[func]{pop}\n\n[class]{MaxHeap}-[func]{siftDown}\n
        my_heap.rb
        [class]{MaxHeap}-[func]{pop}\n\n[class]{MaxHeap}-[func]{sift_down}\n
        "},{"location":"chapter_heap/heap/#813","title":"8.1.3 \u00a0 \u30d2\u30fc\u30d7\u306e\u4e00\u822c\u7684\u306a\u5fdc\u7528","text":"
        • \u512a\u5148\u5ea6\u30ad\u30e5\u30fc\uff1a\u30d2\u30fc\u30d7\u306f\u512a\u5148\u5ea6\u30ad\u30e5\u30fc\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306e\u597d\u307e\u3057\u3044\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3001\u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c\u3068\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\u306e\u4e21\u65b9\u306e\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(\\log n)\\)\u3001\u30ad\u30e5\u30fc\u69cb\u7bc9\u306e\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(n)\\)\u3067\u3001\u3059\u3079\u3066\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3059\u3002
        • \u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\uff1a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u305d\u308c\u3089\u304b\u3089\u30d2\u30fc\u30d7\u3092\u4f5c\u6210\u3057\u3001\u6b21\u306b\u8981\u7d20\u524a\u9664\u64cd\u4f5c\u3092\u7d99\u7d9a\u7684\u306b\u5b9f\u884c\u3057\u3066\u9806\u5e8f\u4ed8\u3051\u3055\u308c\u305f\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\u3092\u5b9f\u88c5\u3059\u308b\u3088\u308a\u6d17\u7df4\u3055\u308c\u305f\u65b9\u6cd5\u304c\u3042\u308a\u3001\u300c\u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\u300d\u306e\u7ae0\u3067\u8aac\u660e\u3055\u308c\u3066\u3044\u307e\u3059\u3002
        • \u6700\u5927\\(k\\)\u500b\u306e\u8981\u7d20\u306e\u767a\u898b\uff1a\u3053\u308c\u306f\u53e4\u5178\u7684\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u3067\u3042\u308a\u3001\u4e00\u822c\u7684\u306a\u4f7f\u7528\u4f8b\u3067\u3082\u3042\u308a\u307e\u3059\u3002Weibo\u30db\u30c3\u30c8\u691c\u7d22\u306e\u30c8\u30c3\u30d710\u30db\u30c3\u30c8\u30cb\u30e5\u30fc\u30b9\u306e\u9078\u629e\u3084\u3001\u30c8\u30c3\u30d710\u306e\u58f2\u308c\u7b4b\u5546\u54c1\u306e\u9078\u629e\u306a\u3069\u3067\u3059\u3002
        "},{"location":"chapter_heap/summary/","title":"8.4 \u00a0 \u307e\u3068\u3081","text":""},{"location":"chapter_heap/summary/#1","title":"1. \u00a0 \u91cd\u8981\u306a\u5fa9\u7fd2","text":"
        • \u30d2\u30fc\u30d7\u306f\u5b8c\u5099\u4e8c\u5206\u6728\u3067\u3001\u305d\u306e\u69cb\u7bc9\u6027\u8cea\u306b\u57fa\u3065\u3044\u3066\u6700\u5927\u30d2\u30fc\u30d7\u307e\u305f\u306f\u6700\u5c0f\u30d2\u30fc\u30d7\u306b\u5206\u985e\u3067\u304d\u307e\u3059\u3002\u6700\u5927\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u306f\u6700\u5927\u3067\u3001\u6700\u5c0f\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u306f\u6700\u5c0f\u3067\u3059\u3002
        • \u512a\u5148\u5ea6\u30ad\u30e5\u30fc\u306f\u3001\u30c7\u30ad\u30e5\u30fc\u306e\u512a\u5148\u5ea6\u3092\u6301\u3064\u30ad\u30e5\u30fc\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u3001\u901a\u5e38\u30d2\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002
        • \u30d2\u30fc\u30d7\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c\u3068\u305d\u308c\u306b\u5bfe\u5fdc\u3059\u308b\u6642\u9593\u8a08\u7b97\u91cf\u306b\u306f\u4ee5\u4e0b\u304c\u3042\u308a\u307e\u3059\uff1a\u30d2\u30fc\u30d7\u3078\u306e\u8981\u7d20\u633f\u5165\\(O(\\log n)\\)\u3001\u30d2\u30fc\u30d7\u304b\u3089\u306e\u5148\u982d\u8981\u7d20\u524a\u9664\\(O(\\log n)\\)\u3001\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3078\u306e\u30a2\u30af\u30bb\u30b9\\(O(1)\\)\u3002
        • \u5b8c\u5099\u4e8c\u5206\u6728\u306f\u914d\u5217\u3067\u8868\u73fe\u3059\u308b\u306e\u306b\u9069\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u30d2\u30fc\u30d7\u306f\u4e00\u822c\u7684\u306b\u914d\u5217\u3092\u4f7f\u7528\u3057\u3066\u683c\u7d0d\u3055\u308c\u307e\u3059\u3002
        • \u30d2\u30fc\u30d7\u5316\u64cd\u4f5c\u306f\u30d2\u30fc\u30d7\u306e\u6027\u8cea\u3092\u7dad\u6301\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u3001\u30d2\u30fc\u30d7\u306e\u633f\u5165\u64cd\u4f5c\u3068\u524a\u9664\u64cd\u4f5c\u306e\u4e21\u65b9\u3067\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        • \\(n\\)\u500b\u306e\u8981\u7d20\u304c\u5165\u529b\u3068\u3057\u3066\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u306e\u30d2\u30fc\u30d7\u69cb\u7bc9\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u306b\u6700\u9069\u5316\u3067\u304d\u3001\u3053\u308c\u306f\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3059\u3002
        • Top-k\u306f\u53e4\u5178\u7684\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u3067\u3001\u30d2\u30fc\u30d7\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u4f7f\u7528\u3057\u3066\u52b9\u7387\u7684\u306b\u89e3\u6c7a\u3067\u304d\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n \\log k)\\)\u3067\u3059\u3002
        "},{"location":"chapter_heap/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q: \u30c7\u30fc\u30bf\u69cb\u9020\u306e\u300c\u30d2\u30fc\u30d7\u300d\u3068\u30e1\u30e2\u30ea\u7ba1\u7406\u306e\u300c\u30d2\u30fc\u30d7\u300d\u306f\u540c\u3058\u6982\u5ff5\u3067\u3059\u304b\uff1f

        \u3053\u306e2\u3064\u306f\u3001\u3069\u3061\u3089\u3082\u300c\u30d2\u30fc\u30d7\u300d\u3068\u547c\u3070\u308c\u307e\u3059\u304c\u3001\u540c\u3058\u6982\u5ff5\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30b7\u30b9\u30c6\u30e0\u30e1\u30e2\u30ea\u306e\u30d2\u30fc\u30d7\u306f\u52d5\u7684\u30e1\u30e2\u30ea\u5272\u308a\u5f53\u3066\u306e\u4e00\u90e8\u3067\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u5b9f\u884c\u4e2d\u306b\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3084\u914d\u5217\u306a\u3069\u306e\u8907\u96d1\u306a\u69cb\u9020\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u3001\u4e00\u5b9a\u91cf\u306e\u30d2\u30fc\u30d7\u30e1\u30e2\u30ea\u3092\u8981\u6c42\u3067\u304d\u307e\u3059\u3002\u5272\u308a\u5f53\u3066\u3089\u308c\u305f\u30c7\u30fc\u30bf\u304c\u4e0d\u8981\u306b\u306a\u3063\u305f\u3068\u304d\u306f\u3001\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u3092\u9632\u3050\u305f\u3081\u306b\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u3053\u306e\u30e1\u30e2\u30ea\u3092\u89e3\u653e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u30b9\u30bf\u30c3\u30af\u30e1\u30e2\u30ea\u3068\u6bd4\u8f03\u3057\u3066\u3001\u30d2\u30fc\u30d7\u30e1\u30e2\u30ea\u306e\u7ba1\u7406\u3068\u4f7f\u7528\u306b\u306f\u3088\u308a\u591a\u304f\u306e\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3001\u4e0d\u9069\u5207\u306a\u4f7f\u7528\u306f\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u3084\u30c0\u30f3\u30b0\u30ea\u30f3\u30b0\u30dd\u30a4\u30f3\u30bf\u306b\u3064\u306a\u304c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_heap/top_k/","title":"8.3 \u00a0 Top-k\u554f\u984c","text":"

        Question

        \u9577\u3055\\(n\\)\u306e\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u3066\u3044\u306a\u3044\u914d\u5217nums\u304c\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001\u914d\u5217\u5185\u306e\u6700\u5927\\(k\\)\u500b\u306e\u8981\u7d20\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u3053\u306e\u554f\u984c\u306b\u3064\u3044\u3066\u3001\u307e\u305a2\u3064\u306e\u76f4\u63a5\u7684\u306a\u89e3\u6cd5\u3092\u7d39\u4ecb\u3057\u3001\u6b21\u306b\u3088\u308a\u52b9\u7387\u7684\u306a\u30d2\u30fc\u30d7\u30d9\u30fc\u30b9\u306e\u65b9\u6cd5\u3092\u8aac\u660e\u3057\u307e\u3059\u3002

        "},{"location":"chapter_heap/top_k/#831-1","title":"8.3.1 \u00a0 \u65b9\u6cd51\uff1a\u53cd\u5fa9\u9078\u629e","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\\(k\\)\u56de\u306e\u53cd\u5fa9\u3092\u5b9f\u884c\u3057\u3001\u5404\u56de\u3067\\(1\\)\u756a\u76ee\u3001\\(2\\)\u756a\u76ee\u3001\\(\\dots\\)\u3001\\(k\\)\u756a\u76ee\u306b\u5927\u304d\u3044\u8981\u7d20\u3092\u62bd\u51fa\u3067\u304d\u307e\u3059\u3002\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(nk)\\)\u3067\u3059\u3002

        \u3053\u306e\u65b9\u6cd5\u306f\\(k \\ll n\\)\u306e\u5834\u5408\u306b\u306e\u307f\u9069\u3057\u3066\u3044\u307e\u3059\u3002\\(k\\)\u304c\\(n\\)\u306b\u8fd1\u3044\u5834\u5408\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n^2)\\)\u306b\u8fd1\u3065\u304d\u3001\u975e\u5e38\u306b\u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002

        \u56f3 8-6 \u00a0 \u6700\u5927k\u500b\u306e\u8981\u7d20\u3092\u53cd\u5fa9\u7684\u306b\u898b\u3064\u3051\u308b

        Tip

        \\(k = n\\)\u306e\u5834\u5408\u3001\u5b8c\u5168\u306b\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u3053\u308c\u306f\u300c\u9078\u629e\u30bd\u30fc\u30c8\u300d\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u540c\u7b49\u3067\u3059\u3002

        "},{"location":"chapter_heap/top_k/#832-2","title":"8.3.2 \u00a0 \u65b9\u6cd52\uff1a\u30bd\u30fc\u30c8","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u307e\u305a\u914d\u5217nums\u3092\u30bd\u30fc\u30c8\u3057\u3001\u6b21\u306b\u6700\u5f8c\u306e\\(k\\)\u500b\u306e\u8981\u7d20\u3092\u8fd4\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n \\log n)\\)\u3067\u3059\u3002

        \u660e\u3089\u304b\u306b\u3001\u3053\u306e\u65b9\u6cd5\u306f\u30bf\u30b9\u30af\u3092\u300c\u3084\u308a\u3059\u304e\u300d\u3066\u3044\u307e\u3059\u3002\u6700\u5927\\(k\\)\u500b\u306e\u8981\u7d20\u3092\u898b\u3064\u3051\u308b\u3060\u3051\u3067\u3088\u304f\u3001\u4ed6\u306e\u8981\u7d20\u3092\u30bd\u30fc\u30c8\u3059\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\u3002

        \u56f3 8-7 \u00a0 \u30bd\u30fc\u30c8\u306b\u3088\u308b\u6700\u5927k\u500b\u306e\u8981\u7d20\u306e\u767a\u898b

        "},{"location":"chapter_heap/top_k/#833-3","title":"8.3.3 \u00a0 \u65b9\u6cd53\uff1a\u30d2\u30fc\u30d7","text":"

        \u4ee5\u4e0b\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30d2\u30fc\u30d7\u306b\u57fa\u3065\u3044\u3066Top-k\u554f\u984c\u3092\u3088\u308a\u52b9\u7387\u7684\u306b\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002

        1. \u6700\u5c0f\u30d2\u30fc\u30d7\u3092\u521d\u671f\u5316\u3057\u307e\u3059\u3002\u5148\u982d\u8981\u7d20\u304c\u6700\u5c0f\u306b\u306a\u308a\u307e\u3059\u3002
        2. \u307e\u305a\u3001\u914d\u5217\u306e\u6700\u521d\u306e\\(k\\)\u500b\u306e\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u633f\u5165\u3057\u307e\u3059\u3002
        3. \\(k + 1\\)\u756a\u76ee\u306e\u8981\u7d20\u304b\u3089\u958b\u59cb\u3057\u3001\u73fe\u5728\u306e\u8981\u7d20\u304c\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3088\u308a\u5927\u304d\u3044\u5834\u5408\u3001\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u524a\u9664\u3057\u3001\u73fe\u5728\u306e\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u633f\u5165\u3057\u307e\u3059\u3002
        4. \u8d70\u67fb\u3092\u5b8c\u4e86\u3057\u305f\u5f8c\u3001\u30d2\u30fc\u30d7\u306b\u306f\u6700\u5927\\(k\\)\u500b\u306e\u8981\u7d20\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002
        <1><2><3><4><5><6><7><8><9>

        \u56f3 8-8 \u00a0 \u30d2\u30fc\u30d7\u306b\u57fa\u3065\u304f\u6700\u5927k\u500b\u306e\u8981\u7d20\u306e\u767a\u898b

        \u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby top_k.py
        def top_k_heap(nums: list[int], k: int) -> list[int]:\n    \"\"\"\u30d2\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u5185\u306e\u6700\u5927k\u500b\u306e\u8981\u7d20\u3092\u898b\u3064\u3051\u308b\"\"\"\n    # \u6700\u5c0f\u30d2\u30fc\u30d7\u3092\u521d\u671f\u5316\n    heap = []\n    # \u914d\u5217\u306e\u6700\u521d\u306ek\u500b\u306e\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u5165\u529b\n    for i in range(k):\n        heapq.heappush(heap, nums[i])\n    # k+1\u756a\u76ee\u306e\u8981\u7d20\u304b\u3089\u3001\u30d2\u30fc\u30d7\u306e\u9577\u3055\u3092k\u306b\u4fdd\u3064\n    for i in range(k, len(nums)):\n        # \u73fe\u5728\u306e\u8981\u7d20\u304c\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3088\u308a\u5927\u304d\u3044\u5834\u5408\u3001\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u524a\u9664\u3057\u3001\u73fe\u5728\u306e\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u5165\u529b\n        if nums[i] > heap[0]:\n            heapq.heappop(heap)\n            heapq.heappush(heap, nums[i])\n    return heap\n
        top_k.cpp
        /* \u30d2\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u5185\u306e\u6700\u5927k\u500b\u306e\u8981\u7d20\u3092\u898b\u3064\u3051\u308b */\npriority_queue<int, vector<int>, greater<int>> topKHeap(vector<int> &nums, int k) {\n    // \u6700\u5c0f\u30d2\u30fc\u30d7\u3092\u521d\u671f\u5316\n    priority_queue<int, vector<int>, greater<int>> heap;\n    // \u914d\u5217\u306e\u6700\u521d\u306ek\u500b\u306e\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u5165\u529b\n    for (int i = 0; i < k; i++) {\n        heap.push(nums[i]);\n    }\n    // k+1\u756a\u76ee\u306e\u8981\u7d20\u304b\u3089\u3001\u30d2\u30fc\u30d7\u306e\u9577\u3055\u3092k\u306b\u4fdd\u3064\n    for (int i = k; i < nums.size(); i++) {\n        // \u73fe\u5728\u306e\u8981\u7d20\u304c\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3088\u308a\u5927\u304d\u3044\u5834\u5408\u3001\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u524a\u9664\u3057\u3001\u73fe\u5728\u306e\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u5165\u529b\n        if (nums[i] > heap.top()) {\n            heap.pop();\n            heap.push(nums[i]);\n        }\n    }\n    return heap;\n}\n
        top_k.java
        /* \u30d2\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u5185\u306e\u6700\u5927 k \u500b\u306e\u8981\u7d20\u3092\u691c\u7d22 */\nQueue<Integer> topKHeap(int[] nums, int k) {\n    // \u6700\u5c0f\u30d2\u30fc\u30d7\u3092\u521d\u671f\u5316\n    Queue<Integer> heap = new PriorityQueue<Integer>();\n    // \u914d\u5217\u306e\u6700\u521d\u306e k \u500b\u306e\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u5165\u529b\n    for (int i = 0; i < k; i++) {\n        heap.offer(nums[i]);\n    }\n    // k+1 \u756a\u76ee\u306e\u8981\u7d20\u304b\u3089\u3001\u30d2\u30fc\u30d7\u306e\u9577\u3055\u3092 k \u306b\u4fdd\u3064\n    for (int i = k; i < nums.length; i++) {\n        // \u73fe\u5728\u306e\u8981\u7d20\u304c\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3088\u308a\u5927\u304d\u3044\u5834\u5408\u3001\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u524a\u9664\u3057\u3001\u73fe\u5728\u306e\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u5165\u529b\n        if (nums[i] > heap.peek()) {\n            heap.poll();\n            heap.offer(nums[i]);\n        }\n    }\n    return heap;\n}\n
        top_k.cs
        [class]{top_k}-[func]{TopKHeap}\n
        top_k.go
        [class]{}-[func]{topKHeap}\n
        top_k.swift
        [class]{}-[func]{topKHeap}\n
        top_k.js
        [class]{}-[func]{pushMinHeap}\n\n[class]{}-[func]{popMinHeap}\n\n[class]{}-[func]{peekMinHeap}\n\n[class]{}-[func]{getMinHeap}\n\n[class]{}-[func]{topKHeap}\n
        top_k.ts
        [class]{}-[func]{pushMinHeap}\n\n[class]{}-[func]{popMinHeap}\n\n[class]{}-[func]{peekMinHeap}\n\n[class]{}-[func]{getMinHeap}\n\n[class]{}-[func]{topKHeap}\n
        top_k.dart
        [class]{}-[func]{topKHeap}\n
        top_k.rs
        [class]{}-[func]{top_k_heap}\n
        top_k.c
        [class]{}-[func]{pushMinHeap}\n\n[class]{}-[func]{popMinHeap}\n\n[class]{}-[func]{peekMinHeap}\n\n[class]{}-[func]{getMinHeap}\n\n[class]{}-[func]{topKHeap}\n
        top_k.kt
        [class]{}-[func]{topKHeap}\n
        top_k.rb
        [class]{}-[func]{top_k_heap}\n

        \u5408\u8a08\\(n\\)\u56de\u306e\u30d2\u30fc\u30d7\u633f\u5165\u3068\u524a\u9664\u304c\u5b9f\u884c\u3055\u308c\u3001\u6700\u5927\u30d2\u30fc\u30d7\u30b5\u30a4\u30ba\u304c\\(k\\)\u3067\u3042\u308b\u305f\u3081\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n \\log k)\\)\u3067\u3059\u3002\u3053\u306e\u65b9\u6cd5\u306f\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3001\\(k\\)\u304c\u5c0f\u3055\u3044\u5834\u5408\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u306b\u8fd1\u3065\u304d\u3001\\(k\\)\u304c\u5927\u304d\u3044\u5834\u5408\u3067\u3082\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n \\log n)\\)\u3092\u8d85\u3048\u307e\u305b\u3093\u3002

        \u3055\u3089\u306b\u3001\u3053\u306e\u65b9\u6cd5\u306f\u52d5\u7684\u30c7\u30fc\u30bf\u30b9\u30c8\u30ea\u30fc\u30e0\u306e\u30b7\u30ca\u30ea\u30aa\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u30c7\u30fc\u30bf\u3092\u7d99\u7d9a\u7684\u306b\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3067\u3001\u30d2\u30fc\u30d7\u5185\u306e\u8981\u7d20\u3092\u7dad\u6301\u3057\u3001\u6700\u5927\\(k\\)\u500b\u306e\u8981\u7d20\u306e\u52d5\u7684\u66f4\u65b0\u3092\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_hello_algo/","title":"\u306f\u3058\u3081\u306b","text":"

        \u6570\u5e74\u524d\u3001\u79c1\u306fLeetCode\u3067\u300c\u5263\u6307Offer\u300d\u306e\u554f\u984c\u89e3\u7b54\u3092\u5171\u6709\u3057\u3001\u591a\u304f\u306e\u8aad\u8005\u304b\u3089\u52b1\u307e\u3057\u3068\u30b5\u30dd\u30fc\u30c8\u3092\u53d7\u3051\u307e\u3057\u305f\u3002\u8aad\u8005\u3068\u306e\u3084\u308a\u53d6\u308a\u306e\u4e2d\u3067\u3001\u6700\u3082\u3088\u304f\u805e\u304b\u308c\u305f\u8cea\u554f\u306f\u300c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52c9\u5f37\u3092\u3069\u3046\u59cb\u3081\u305f\u3089\u3088\u3044\u304b\u300d\u3067\u3057\u305f\u3002\u6b21\u7b2c\u306b\u3001\u79c1\u306f\u3053\u306e\u8cea\u554f\u306b\u5f37\u3044\u95a2\u5fc3\u3092\u62b1\u304f\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002

        \u554f\u984c\u3092\u76f4\u63a5\u89e3\u304f\u3053\u3068\u304c\u6700\u3082\u4eba\u6c17\u306e\u3042\u308b\u65b9\u6cd5\u306e\u3088\u3046\u3067\u3059\u3002\u3053\u308c\u306f\u30b7\u30f3\u30d7\u30eb\u3067\u76f4\u63a5\u7684\u3067\u52b9\u679c\u7684\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u554f\u984c\u89e3\u6c7a\u306f\u30de\u30a4\u30f3\u30b9\u30a4\u30fc\u30d1\u30fc\u3092\u30d7\u30ec\u30a4\u3059\u308b\u3088\u3046\u306a\u3082\u306e\u3067\u3059\u3002\u81ea\u5b66\u81ea\u7fd2\u306e\u80fd\u529b\u304c\u9ad8\u3044\u4eba\u306f\u3001\u5730\u96f7\u3092\u4e00\u3064\u305a\u3064\u56de\u907f\u3057\u3066\u3044\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u304c\u3001\u3057\u3063\u304b\u308a\u3068\u3057\u305f\u57fa\u790e\u304c\u306a\u3044\u4eba\u306f\u3001\u4f55\u5ea6\u3082\u3064\u307e\u305a\u3044\u3066\u632b\u6298\u3057\u306a\u304c\u3089\u5f8c\u9000\u3059\u308b\u3053\u3068\u306b\u306a\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u6559\u79d1\u66f8\u3092\u8aad\u3080\u3053\u3068\u3082\u4e00\u822c\u7684\u306a\u65b9\u6cd5\u3067\u3059\u304c\u3001\u5c31\u8077\u6d3b\u52d5\u4e2d\u306e\u4eba\u306b\u3068\u3063\u3066\u3001\u5352\u696d\u8ad6\u6587\u306e\u57f7\u7b46\u3001\u5c65\u6b74\u66f8\u306e\u63d0\u51fa\u3001\u7b46\u8a18\u8a66\u9a13\u3084\u9762\u63a5\u306e\u6e96\u5099\u304c\u65e2\u306b\u30a8\u30cd\u30eb\u30ae\u30fc\u306e\u5927\u90e8\u5206\u3092\u6d88\u8cbb\u3057\u3066\u304a\u308a\u3001\u5206\u539a\u3044\u672c\u3092\u8aad\u3080\u3053\u3068\u306f\u3057\u3070\u3057\u3070\u56f0\u96e3\u306a\u6311\u6226\u3068\u306a\u308a\u307e\u3059\u3002

        \u3082\u3057\u3042\u306a\u305f\u304c\u540c\u69d8\u306e\u60a9\u307f\u3092\u62b1\u3048\u3066\u3044\u308b\u306a\u3089\u3001\u3053\u306e\u672c\u304c\u3042\u306a\u305f\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u3066\u5e78\u904b\u3067\u3059\u3002\u3053\u306e\u672c\u306f\u3001\u3053\u306e\u8cea\u554f\u306b\u5bfe\u3059\u308b\u79c1\u306e\u7b54\u3048\u3067\u3059\u3002\u3053\u308c\u304c\u6700\u826f\u306e\u89e3\u6c7a\u7b56\u3067\u306f\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u5c11\u306a\u304f\u3068\u3082\u7a4d\u6975\u7684\u306a\u8a66\u307f\u3067\u3059\u3002\u3053\u306e\u672c\u304c\u3042\u306a\u305f\u306b\u76f4\u63a5\u5185\u5b9a\u3092\u3082\u305f\u3089\u3059\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u304c\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u300c\u77e5\u8b58\u5730\u56f3\u300d\u3092\u63a2\u7d22\u3059\u308b\u624b\u5f15\u304d\u3068\u306a\u308a\u3001\u3055\u307e\u3056\u307e\u306a\u300c\u5730\u96f7\u300d\u306e\u5f62\u3001\u5927\u304d\u3055\u3001\u4f4d\u7f6e\u3092\u7406\u89e3\u3057\u3001\u3055\u307e\u3056\u307e\u306a\u300c\u5730\u96f7\u9664\u53bb\u65b9\u6cd5\u300d\u3092\u30de\u30b9\u30bf\u30fc\u3067\u304d\u308b\u3088\u3046\u304a\u624b\u4f1d\u3044\u3057\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u30b9\u30ad\u30eb\u304c\u3042\u308c\u3070\u3001\u3088\u308a\u5feb\u9069\u306b\u554f\u984c\u3092\u89e3\u304d\u3001\u6587\u732e\u3092\u8aad\u3080\u3053\u3068\u304c\u3067\u304d\u3001\u5f90\u3005\u306b\u77e5\u8b58\u4f53\u7cfb\u3092\u69cb\u7bc9\u3067\u304d\u308b\u3068\u4fe1\u3058\u3066\u3044\u307e\u3059\u3002

        \u79c1\u306f\u3001\u30d5\u30a1\u30a4\u30f3\u30de\u30f3\u6559\u6388\u306e\u8a00\u8449\u306b\u6df1\u304f\u540c\u611f\u3057\u307e\u3059\u3002\u300c\u77e5\u8b58\u306f\u7121\u6599\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u6ce8\u610f\u3092\u6255\u308f\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u306e\u3067\u3059\u3002\u300d\u3053\u306e\u610f\u5473\u3067\u3001\u3053\u306e\u672c\u306f\u5b8c\u5168\u306b\u300c\u7121\u6599\u300d\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u3053\u306e\u672c\u306b\u5bfe\u3059\u308b\u3042\u306a\u305f\u306e\u8cb4\u91cd\u306a\u300c\u6ce8\u610f\u300d\u306b\u5fdc\u3048\u308b\u305f\u3081\u306b\u3001\u79c1\u306f\u6700\u5584\u3092\u5c3d\u304f\u3057\u3001\u6700\u5927\u306e\u300c\u6ce8\u610f\u300d\u3092\u6255\u3063\u3066\u3053\u306e\u672c\u3092\u66f8\u304d\u307e\u3059\u3002

        \u81ea\u5206\u306e\u9650\u754c\u3092\u8a8d\u8b58\u3057\u3066\u304a\u308a\u3001\u3053\u306e\u672c\u306e\u5185\u5bb9\u304c\u6642\u9593\u3092\u304b\u3051\u3066\u6d17\u7df4\u3055\u308c\u305f\u306b\u3082\u304b\u304b\u308f\u3089\u305a\u3001\u9593\u9055\u3044\u306f\u78ba\u5b9f\u306b\u6b8b\u3063\u3066\u3044\u308b\u3053\u3068\u3092\u7406\u89e3\u3057\u3066\u3044\u307e\u3059\u3002\u5148\u751f\u65b9\u3084\u5b66\u751f\u306e\u7686\u69d8\u304b\u3089\u306e\u6279\u8a55\u3068\u8a02\u6b63\u3092\u5fc3\u304b\u3089\u6b53\u8fce\u3044\u305f\u3057\u307e\u3059\u3002

        Hello, Algo!

        \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u51fa\u73fe\u306f\u4e16\u754c\u306b\u5927\u304d\u306a\u5909\u5316\u3092\u3082\u305f\u3089\u3057\u307e\u3057\u305f\u3002\u9ad8\u901f\u306a\u8a08\u7b97\u80fd\u529b\u3068\u512a\u308c\u305f\u30d7\u30ed\u30b0\u30e9\u30e0\u53ef\u80fd\u6027\u306b\u3088\u308a\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u5b9f\u884c\u3057\u30c7\u30fc\u30bf\u3092\u51e6\u7406\u3059\u308b\u305f\u3081\u306e\u7406\u60f3\u7684\u306a\u5a92\u4f53\u3068\u306a\u308a\u307e\u3057\u305f\u3002\u30d3\u30c7\u30aa\u30b2\u30fc\u30e0\u306e\u30ea\u30a2\u30eb\u306a\u30b0\u30e9\u30d5\u30a3\u30c3\u30af\u30b9\u3001\u81ea\u52d5\u904b\u8ee2\u306e\u77e5\u7684\u306a\u5224\u65ad\u3001AlphaGo\u306e\u898b\u4e8b\u306a\u56f2\u7881\u30b2\u30fc\u30e0\u3001ChatGPT\u306e\u81ea\u7136\u306a\u5bfe\u8a71\u306a\u3069\u3001\u3053\u308c\u3089\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306f\u3059\u3079\u3066\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u4e0a\u3067\u52d5\u4f5c\u3059\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7cbe\u5de7\u306a\u5b9f\u6f14\u3067\u3059\u3002

        \u5b9f\u969b\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u51fa\u73fe\u4ee5\u524d\u304b\u3089\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u4e16\u754c\u306e\u81f3\u308b\u6240\u306b\u5b58\u5728\u3057\u3066\u3044\u307e\u3057\u305f\u3002\u521d\u671f\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u6bd4\u8f03\u7684\u30b7\u30f3\u30d7\u30eb\u3067\u3001\u53e4\u4ee3\u306e\u8a08\u6570\u65b9\u6cd5\u3084\u9053\u5177\u4f5c\u308a\u306e\u624b\u9806\u306a\u3069\u304c\u3042\u308a\u307e\u3057\u305f\u3002\u6587\u660e\u304c\u9032\u6b69\u3059\u308b\u306b\u3064\u308c\u3066\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3088\u308a\u6d17\u7df4\u3055\u308c\u8907\u96d1\u306b\u306a\u308a\u307e\u3057\u305f\u3002\u8077\u4eba\u306e\u7cbe\u5de7\u306a\u6280\u8853\u304b\u3089\u3001\u751f\u7523\u529b\u3092\u89e3\u653e\u3059\u308b\u5de5\u696d\u88fd\u54c1\u3001\u5b87\u5b99\u3092\u652f\u914d\u3059\u308b\u79d1\u5b66\u6cd5\u5247\u307e\u3067\u3001\u307b\u307c\u3059\u3079\u3066\u306e\u5e73\u51e1\u307e\u305f\u306f\u9a5a\u7570\u7684\u306a\u3053\u3068\u306e\u80cc\u5f8c\u306b\u306f\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5de7\u5999\u306a\u601d\u8003\u304c\u3042\u308a\u307e\u3059\u3002

        \u540c\u69d8\u306b\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u81f3\u308b\u6240\u306b\u3042\u308a\u307e\u3059\u3002\u30bd\u30fc\u30b7\u30e3\u30eb\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u304b\u3089\u5730\u4e0b\u9244\u8def\u7dda\u307e\u3067\u3001\u591a\u304f\u306e\u30b7\u30b9\u30c6\u30e0\u306f\u300c\u30b0\u30e9\u30d5\u300d\u3068\u3057\u3066\u30e2\u30c7\u30eb\u5316\u3067\u304d\u307e\u3059\u3002\u56fd\u304b\u3089\u5bb6\u65cf\u307e\u3067\u3001\u793e\u4f1a\u7d44\u7e54\u306e\u4e3b\u8981\u306a\u5f62\u614b\u306f\u300c\u6728\u300d\u306e\u7279\u5fb4\u3092\u793a\u3057\u307e\u3059\u3002\u51ac\u670d\u306f\u300c\u30b9\u30bf\u30c3\u30af\u300d\u306e\u3088\u3046\u3067\u3001\u6700\u521d\u306b\u7740\u305f\u3082\u306e\u304c\u6700\u5f8c\u306b\u8131\u304c\u308c\u307e\u3059\u3002\u30d0\u30c9\u30df\u30f3\u30c8\u30f3\u306e\u30b7\u30e3\u30c8\u30eb\u7b52\u306f\u300c\u30ad\u30e5\u30fc\u300d\u306b\u4f3c\u3066\u304a\u308a\u3001\u4e00\u65b9\u306e\u7aef\u3067\u633f\u5165\u3057\u3001\u3082\u3046\u4e00\u65b9\u306e\u7aef\u3067\u53d6\u308a\u51fa\u3057\u307e\u3059\u3002\u8f9e\u66f8\u306f\u300c\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u300d\u306e\u3088\u3046\u3067\u3001\u76ee\u6a19\u30a8\u30f3\u30c8\u30ea\u3092\u7d20\u65e9\u304f\u691c\u7d22\u3067\u304d\u307e\u3059\u3002

        \u3053\u306e\u672c\u306f\u3001\u660e\u78ba\u3067\u7406\u89e3\u3057\u3084\u3059\u3044\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u56f3\u89e3\u3068\u5b9f\u884c\u53ef\u80fd\u306a\u30b3\u30fc\u30c9\u4f8b\u3092\u901a\u3058\u3066\u3001\u8aad\u8005\u304c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u6838\u5fc3\u6982\u5ff5\u3092\u7406\u89e3\u3057\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3092\u901a\u3058\u3066\u305d\u308c\u3089\u3092\u5b9f\u88c5\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u3053\u3068\u3092\u76ee\u6307\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u57fa\u76e4\u306e\u4e0a\u3067\u3001\u3053\u306e\u672c\u306f\u8907\u96d1\u306a\u4e16\u754c\u306b\u304a\u3051\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u751f\u304d\u751f\u304d\u3068\u3057\u305f\u73fe\u308c\u3092\u660e\u3089\u304b\u306b\u3057\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7f8e\u3057\u3055\u3092\u793a\u3059\u3053\u3068\u306b\u52aa\u3081\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u672c\u304c\u3042\u306a\u305f\u306e\u304a\u5f79\u306b\u7acb\u3066\u308b\u3053\u3068\u3092\u9858\u3063\u3066\u3044\u307e\u3059\uff01

        "},{"location":"chapter_introduction/","title":"\u7b2c 1 \u7ae0 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u306e\u51fa\u4f1a\u3044","text":"

        Abstract

        \u512a\u96c5\u306a\u4e59\u5973\u304c\u8e0a\u308a\u307e\u3059\u3002\u30c7\u30fc\u30bf\u3068\u7d61\u307f\u5408\u3044\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u30e1\u30ed\u30c7\u30a3\u30fc\u306b\u5408\u308f\u305b\u3066\u30b9\u30ab\u30fc\u30c8\u3092\u306a\u3073\u304b\u305b\u306a\u304c\u3089\u3002

        \u5f7c\u5973\u304c\u3042\u306a\u305f\u3092\u30c0\u30f3\u30b9\u306b\u8a98\u3044\u307e\u3059\u3002\u5f7c\u5973\u306e\u30b9\u30c6\u30c3\u30d7\u306b\u5f93\u3063\u3066\u3001\u8ad6\u7406\u3068\u7f8e\u306b\u6e80\u3061\u305f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u4e16\u754c\u306b\u5165\u308a\u307e\u3057\u3087\u3046\u3002

        "},{"location":"chapter_introduction/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 1.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3069\u3053\u306b\u3067\u3082\u3042\u308b
        • 1.2 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u306f\u4f55\u304b
        • 1.3 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_introduction/algorithms_are_everywhere/","title":"1.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u81f3\u308b\u6240\u306b\u3042\u308b","text":"

        \u300c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u300d\u3068\u3044\u3046\u8a00\u8449\u3092\u805e\u304f\u3068\u3001\u81ea\u7136\u306b\u6570\u5b66\u3092\u601d\u3044\u6d6e\u304b\u3079\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u591a\u304f\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u8907\u96d1\u306a\u6570\u5b66\u3092\u542b\u307e\u305a\u3001\u57fa\u672c\u7684\u306a\u8ad6\u7406\u306b\u3088\u308a\u591a\u304f\u4f9d\u5b58\u3057\u3066\u304a\u308a\u3001\u3053\u308c\u306f\u79c1\u305f\u3061\u306e\u65e5\u5e38\u751f\u6d3b\u306e\u81f3\u308b\u6240\u3067\u898b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u3064\u3044\u3066\u6b63\u5f0f\u306b\u8b70\u8ad6\u3092\u59cb\u3081\u308b\u524d\u306b\u3001\u8208\u5473\u6df1\u3044\u4e8b\u5b9f\u3092\u5171\u6709\u3059\u308b\u4fa1\u5024\u304c\u3042\u308a\u307e\u3059\u3002\u3042\u306a\u305f\u306f\u7121\u610f\u8b58\u306e\u3046\u3061\u306b\u591a\u304f\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u5b66\u3073\u3001\u65e5\u5e38\u751f\u6d3b\u3067\u305d\u308c\u3089\u3092\u5fdc\u7528\u3059\u308b\u3053\u3068\u306b\u6163\u308c\u3066\u3044\u307e\u3059\u3002\u3053\u3053\u3067\u3001\u3053\u306e\u70b9\u3092\u8a3c\u660e\u3059\u308b\u305f\u3081\u306b\u3044\u304f\u3064\u304b\u306e\u5177\u4f53\u7684\u306a\u4f8b\u3092\u6319\u3052\u307e\u3059\u3002

        \u4f8b1\uff1a\u8f9e\u66f8\u306e\u5f15\u304d\u65b9\u3002\u82f1\u8a9e\u306e\u8f9e\u66f8\u3067\u306f\u3001\u5358\u8a9e\u304c\u30a2\u30eb\u30d5\u30a1\u30d9\u30c3\u30c8\u9806\u306b\u4e26\u3093\u3067\u3044\u307e\u3059\u3002\\(r\\)\u3067\u59cb\u307e\u308b\u5358\u8a9e\u3092\u63a2\u3057\u3066\u3044\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u901a\u5e38\u306f\u4ee5\u4e0b\u306e\u65b9\u6cd5\u3067\u884c\u3044\u307e\u3059\uff1a

        1. \u8f9e\u66f8\u3092\u5927\u4f53\u534a\u5206\u3050\u3089\u3044\u306e\u3068\u3053\u308d\u3067\u958b\u304d\u3001\u305d\u306e\u30da\u30fc\u30b8\u306e\u6700\u521d\u306e\u8a9e\u5f59\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\\(m\\)\u3067\u59cb\u307e\u308b\u6587\u5b57\u3060\u3068\u3057\u307e\u3057\u3087\u3046\u3002
        2. \\(r\\)\u306f\u30a2\u30eb\u30d5\u30a1\u30d9\u30c3\u30c8\u3067\\(m\\)\u306e\u5f8c\u306b\u6765\u308b\u306e\u3067\u3001\u524d\u534a\u3092\u7121\u8996\u3057\u3066\u3001\u63a2\u7d22\u7a7a\u9593\u3092\u5f8c\u534a\u306b\u7d5e\u308a\u307e\u3059\u3002
        3. \\(r\\)\u3067\u59cb\u307e\u308b\u5358\u8a9e\u3092\u898b\u3064\u3051\u308b\u307e\u3067\u3001\u30b9\u30c6\u30c3\u30d71.\u30682.\u3092\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002
        <1><2><3><4><5>

        \u56f3 1-1 \u00a0 \u8f9e\u66f8\u3092\u5f15\u304f\u904e\u7a0b

        \u8f9e\u66f8\u3092\u5f15\u304f\u3053\u3068\u306f\u3001\u5c0f\u5b66\u751f\u306b\u3068\u3063\u3066\u5fc5\u9808\u306e\u30b9\u30ad\u30eb\u3067\u3059\u304c\u3001\u5b9f\u969b\u306b\u306f\u6709\u540d\u306a\u300c\u4e8c\u5206\u63a2\u7d22\u300d\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3059\u3002\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u89b3\u70b9\u304b\u3089\u3001\u8f9e\u66f8\u3092\u30bd\u30fc\u30c8\u3055\u308c\u305f\u300c\u914d\u5217\u300d\u3068\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u89b3\u70b9\u304b\u3089\u3001\u8f9e\u66f8\u3067\u5358\u8a9e\u3092\u63a2\u3059\u305f\u3081\u306b\u53d6\u3089\u308c\u308b\u4e00\u9023\u306e\u884c\u52d5\u306f\u3001\u300c\u4e8c\u5206\u63a2\u7d22\u300d\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u898b\u306a\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u4f8b2\uff1a\u30c8\u30e9\u30f3\u30d7\u306e\u6574\u7406\u3002\u30c8\u30e9\u30f3\u30d7\u3092\u30d7\u30ec\u30a4\u3059\u308b\u3068\u304d\u3001\u624b\u672d\u3092\u6607\u9806\u306b\u4e26\u3079\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u904e\u7a0b\u3067\u793a\u3055\u308c\u307e\u3059\u3002

        1. \u30c8\u30e9\u30f3\u30d7\u3092\u300c\u6574\u5217\u6e08\u307f\u300d\u3068\u300c\u672a\u6574\u5217\u300d\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u306b\u5206\u3051\u307e\u3059\u3002\u6700\u521d\u306f\u4e00\u756a\u5de6\u306e\u30ab\u30fc\u30c9\u304c\u65e2\u306b\u6574\u5217\u3057\u3066\u3044\u308b\u3068\u4eee\u5b9a\u3057\u307e\u3059\u3002
        2. \u672a\u6574\u5217\u30bb\u30af\u30b7\u30e7\u30f3\u304b\u3089\u30ab\u30fc\u30c9\u30921\u679a\u53d6\u308a\u51fa\u3057\u3001\u6574\u5217\u6e08\u307f\u30bb\u30af\u30b7\u30e7\u30f3\u306e\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u633f\u5165\u3057\u307e\u3059\u3002\u3053\u306e\u5f8c\u3001\u5de6\u7aef\u306e2\u679a\u306e\u30ab\u30fc\u30c9\u304c\u6574\u5217\u3057\u307e\u3059\u3002
        3. \u3059\u3079\u3066\u306e\u30ab\u30fc\u30c9\u304c\u6574\u5217\u3059\u308b\u307e\u3067\u3001\u30b9\u30c6\u30c3\u30d72\u3092\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002

        \u56f3 1-2 \u00a0 \u30c8\u30e9\u30f3\u30d7\u306e\u6574\u7406\u904e\u7a0b

        \u4e0a\u8a18\u306e\u30c8\u30e9\u30f3\u30d7\u3092\u6574\u7406\u3059\u308b\u65b9\u6cd5\u306f\u3001\u5b9f\u8cea\u7684\u306b\u300c\u633f\u5165\u30bd\u30fc\u30c8\u300d\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3042\u308a\u3001\u5c0f\u3055\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306b\u5bfe\u3057\u3066\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3059\u3002\u591a\u304f\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u30bd\u30fc\u30c8\u95a2\u6570\u306b\u306f\u633f\u5165\u30bd\u30fc\u30c8\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002

        \u4f8b3\uff1a\u304a\u91e3\u308a\u306e\u8a08\u7b97\u3002\u30b9\u30fc\u30d1\u30fc\u30de\u30fc\u30b1\u30c3\u30c8\u3067\\(69\\)\u306e\u8cb7\u3044\u7269\u3092\u3057\u305f\u3068\u4eee\u5b9a\u3057\u307e\u3059\u3002\u30ec\u30b8\u4fc2\u306b\\(100\\)\u3092\u6e21\u3059\u3068\u3001\\(31\\)\u306e\u304a\u91e3\u308a\u3092\u63d0\u4f9b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u904e\u7a0b\u306f\u4ee5\u4e0b\u306e\u56f3\u3067\u660e\u78ba\u306b\u7406\u89e3\u3067\u304d\u307e\u3059\u3002

        1. \u9078\u629e\u80a2\u306f\\(31\\)\u4ee5\u4e0b\u306e\u4fa1\u5024\u306e\u3042\u308b\u901a\u8ca8\u3067\u3001\\(1\\)\u3001\\(5\\)\u3001\\(10\\)\u3001\\(20\\)\u304c\u542b\u307e\u308c\u307e\u3059\u3002
        2. \u9078\u629e\u80a2\u304b\u3089\u6700\u5927\u306e\\(20\\)\u3092\u53d6\u308a\u51fa\u3057\u3001\\(31 - 20 = 11\\)\u304c\u6b8b\u308a\u307e\u3059\u3002
        3. \u6b8b\u308a\u306e\u9078\u629e\u80a2\u304b\u3089\u6700\u5927\u306e\\(10\\)\u3092\u53d6\u308a\u51fa\u3057\u3001\\(11 - 10 = 1\\)\u304c\u6b8b\u308a\u307e\u3059\u3002
        4. \u6b8b\u308a\u306e\u9078\u629e\u80a2\u304b\u3089\u6700\u5927\u306e\\(1\\)\u3092\u53d6\u308a\u51fa\u3057\u3001\\(1 - 1 = 0\\)\u304c\u6b8b\u308a\u307e\u3059\u3002
        5. \u304a\u91e3\u308a\u306e\u8a08\u7b97\u304c\u5b8c\u4e86\u3057\u3001\u89e3\u7b54\u306f\\(20 + 10 + 1 = 31\\)\u3067\u3059\u3002

        \u56f3 1-3 \u00a0 \u304a\u91e3\u308a\u306e\u8a08\u7b97\u904e\u7a0b

        \u8a18\u8ff0\u3055\u308c\u305f\u30b9\u30c6\u30c3\u30d7\u3067\u306f\u3001\u5229\u7528\u53ef\u80fd\u306a\u6700\u5927\u306e\u984d\u9762\u3092\u4f7f\u7528\u3057\u3066\u5404\u6bb5\u968e\u3067\u6700\u826f\u306e\u9078\u629e\u80a2\u3092\u9078\u3076\u3053\u3068\u3067\u3001\u52b9\u679c\u7684\u306a\u304a\u91e3\u308a\u8a08\u7b97\u6226\u7565\u306b\u3064\u306a\u304c\u308a\u307e\u3059\u3002\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u89b3\u70b9\u304b\u3089\u3001\u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\u300c\u8caa\u6b32\u300d\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u3057\u3066\u77e5\u3089\u308c\u3066\u3044\u307e\u3059\u3002

        \u6599\u7406\u306e\u6e96\u5099\u304b\u3089\u5b87\u5b99\u65c5\u884c\u307e\u3067\u3001\u307b\u307c\u3059\u3079\u3066\u306e\u554f\u984c\u89e3\u6c7a\u306b\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u95a2\u308f\u3063\u3066\u3044\u307e\u3059\u3002\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u51fa\u73fe\u306b\u3088\u308a\u3001\u30e1\u30e2\u30ea\u306b\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u683c\u7d0d\u3057\u3001CPU\u3068GPU\u3092\u547c\u3073\u51fa\u3057\u3066\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u5b9f\u884c\u3059\u308b\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002\u3053\u306e\u3088\u3046\u306b\u3057\u3066\u3001\u73fe\u5b9f\u4e16\u754c\u306e\u554f\u984c\u3092\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306b\u79fb\u3057\u3001\u3088\u308a\u52b9\u7387\u7684\u306a\u65b9\u6cd5\u3067\u3055\u307e\u3056\u307e\u306a\u8907\u96d1\u306a\u554f\u984c\u3092\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002

        Tip

        \u30c7\u30fc\u30bf\u69cb\u9020\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3001\u914d\u5217\u3001\u4e8c\u5206\u63a2\u7d22\u306a\u3069\u306e\u6982\u5ff5\u306b\u3064\u3044\u3066\u307e\u3060\u6df7\u4e71\u3057\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u8aad\u307f\u7d9a\u3051\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002\u3053\u306e\u672c\u306f\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7406\u89e3\u306e\u9818\u57df\u3078\u3068\u512a\u3057\u304f\u5c0e\u3044\u3066\u304f\u308c\u308b\u3067\u3057\u3087\u3046\u3002

        "},{"location":"chapter_introduction/summary/","title":"1.3 \u00a0 \u307e\u3068\u3081","text":"
        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u65e5\u5e38\u751f\u6d3b\u306b\u3042\u308a\u3075\u308c\u3066\u304a\u308a\u3001\u601d\u3063\u3066\u3044\u308b\u307b\u3069\u30a2\u30af\u30bb\u30b9\u3057\u306b\u304f\u304f\u8907\u96d1\u306a\u3082\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u5b9f\u969b\u3001\u79c1\u305f\u3061\u306f\u65e2\u306b\u7121\u610f\u8b58\u306e\u3046\u3061\u306b\u591a\u304f\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u5b66\u3073\u3001\u751f\u6d3b\u306e\u69d8\u3005\u306a\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002
        • \u8f9e\u66f8\u3067\u5358\u8a9e\u3092\u5f15\u304f\u539f\u7406\u306f\u4e8c\u5206\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u4e00\u81f4\u3057\u3066\u3044\u307e\u3059\u3002\u4e8c\u5206\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u5206\u5272\u7d71\u6cbb\u3068\u3044\u3046\u91cd\u8981\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u6982\u5ff5\u3092\u4f53\u73fe\u3057\u3066\u3044\u307e\u3059\u3002
        • \u30c8\u30e9\u30f3\u30d7\u3092\u6574\u7406\u3059\u308b\u904e\u7a0b\u306f\u633f\u5165\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u975e\u5e38\u306b\u4f3c\u3066\u3044\u307e\u3059\u3002\u633f\u5165\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u5c0f\u3055\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306e\u30bd\u30fc\u30c8\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002
        • \u901a\u8ca8\u3067\u304a\u91e3\u308a\u3092\u8a08\u7b97\u3059\u308b\u30b9\u30c6\u30c3\u30d7\u306f\u672c\u8cea\u7684\u306b\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u5f93\u3063\u3066\u304a\u308a\u3001\u5404\u30b9\u30c6\u30c3\u30d7\u3067\u305d\u306e\u6642\u70b9\u3067\u306e\u6700\u826f\u306e\u9078\u629e\u3092\u3057\u307e\u3059\u3002
        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u6709\u9650\u6642\u9593\u5185\u3067\u7279\u5b9a\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306e\u6bb5\u968e\u7684\u306a\u6307\u793a\u306e\u30bb\u30c3\u30c8\u3067\u3059\u304c\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u3067\u306e\u30c7\u30fc\u30bf\u306e\u7d44\u7e54\u5316\u3068\u4fdd\u5b58\u65b9\u6cd5\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002
        • \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u5bc6\u63a5\u306b\u95a2\u9023\u3057\u3066\u3044\u307e\u3059\u3002\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u57fa\u790e\u3067\u3042\u308a\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u6a5f\u80fd\u3092\u6d3b\u7528\u3059\u308b\u30b9\u30c6\u30fc\u30b8\u3067\u3059\u3002
        • \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u30d6\u30ed\u30c3\u30af\u306e\u7d44\u307f\u7acb\u3066\u306b\u4f8b\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u30d6\u30ed\u30c3\u30af\u306f\u30c7\u30fc\u30bf\u3092\u8868\u3057\u3001\u30d6\u30ed\u30c3\u30af\u306e\u5f62\u72b6\u3068\u63a5\u7d9a\u65b9\u6cd5\u306f\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u8868\u3057\u3001\u30d6\u30ed\u30c3\u30af\u3092\u7d44\u307f\u7acb\u3066\u308b\u30b9\u30c6\u30c3\u30d7\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002
        "},{"location":"chapter_introduction/summary/#1-q-a","title":"1. \u00a0 Q & A","text":"

        Q\uff1a\u30d7\u30ed\u30b0\u30e9\u30de\u30fc\u3068\u3057\u3066\u3001\u65e5\u5e38\u306e\u4ed5\u4e8b\u3067\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u624b\u52d5\u3067\u5b9f\u88c5\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3053\u3068\u306f\u3081\u3063\u305f\u306b\u3042\u308a\u307e\u305b\u3093\u3002\u6700\u3082\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u65e2\u306b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3068\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u7d44\u307f\u8fbc\u307e\u308c\u3066\u304a\u308a\u3001\u3059\u3050\u306b\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u79c1\u305f\u3061\u304c\u4ed5\u4e8b\u3067\u906d\u9047\u3059\u308b\u554f\u984c\u304c\u3001\u30ab\u30b9\u30bf\u30e0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u8a2d\u8a08\u3092\u5fc5\u8981\u3068\u3059\u308b\u8907\u96d1\u3055\u306e\u30ec\u30d9\u30eb\u306b\u307e\u3060\u9054\u3057\u3066\u3044\u306a\u3044\u3053\u3068\u3092\u793a\u5506\u3057\u3066\u3044\u307e\u3059\u304b\uff1f

        \u7279\u5b9a\u306e\u4ed5\u4e8b\u30b9\u30ad\u30eb\u304c\u6b66\u8853\u306e\u300c\u6280\u300d\u306e\u3088\u3046\u306a\u3082\u306e\u3060\u3068\u3059\u308c\u3070\u3001\u57fa\u790e\u79d1\u76ee\u306f\u300c\u5185\u529f\u300d\u306e\u3088\u3046\u306a\u3082\u306e\u3067\u3059\u3002

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff08\u304a\u3088\u3073\u305d\u306e\u4ed6\u306e\u57fa\u790e\u79d1\u76ee\uff09\u3092\u5b66\u3076\u610f\u7fa9\u306f\u3001\u5fc5\u305a\u3057\u3082\u4ed5\u4e8b\u3067\u305d\u308c\u3089\u3092\u4e00\u304b\u3089\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3067\u306f\u306a\u304f\u3001\u6982\u5ff5\u306e\u78ba\u56fa\u305f\u308b\u7406\u89e3\u306b\u57fa\u3065\u3044\u3066\u3001\u3088\u308a\u5c02\u9580\u7684\u306a\u610f\u601d\u6c7a\u5b9a\u3068\u554f\u984c\u89e3\u6c7a\u3092\u53ef\u80fd\u306b\u3057\u3001\u305d\u308c\u306b\u3088\u3063\u3066\u4ed5\u4e8b\u306e\u5168\u4f53\u7684\u306a\u8cea\u3092\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u3060\u3068\u79c1\u306f\u4fe1\u3058\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u3059\u3079\u3066\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30bd\u30fc\u30c8\u95a2\u6570\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u5b66\u3093\u3067\u3044\u306a\u3044\u5834\u5408\u3001\u3069\u3093\u306a\u30c7\u30fc\u30bf\u304c\u4e0e\u3048\u3089\u308c\u3066\u3082\u3001\u3053\u306e\u30bd\u30fc\u30c8\u95a2\u6570\u306b\u6e21\u3059\u3060\u3051\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u30b9\u30e0\u30fc\u30ba\u306b\u52d5\u4f5c\u3057\u3001\u826f\u3044\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u793a\u3057\u3001\u554f\u984c\u304c\u306a\u3044\u3088\u3046\u306b\u898b\u3048\u307e\u3059\u3002
        • \u3057\u304b\u3057\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u5b66\u3093\u3060\u3053\u3068\u304c\u3042\u308c\u3070\u3001\u7d44\u307f\u8fbc\u307f\u306e\u30bd\u30fc\u30c8\u95a2\u6570\u306e\u6642\u9593\u8907\u96d1\u5ea6\u306f\u901a\u5e38\\(O(n \\log n)\\)\u3067\u3042\u308b\u3053\u3068\u3092\u7406\u89e3\u3057\u3066\u3044\u307e\u3059\u3002\u3055\u3089\u306b\u3001\u30c7\u30fc\u30bf\u304c\u56fa\u5b9a\u6841\u6570\u306e\u6574\u6570\uff08\u5b66\u751fID\u306a\u3069\uff09\u3067\u69cb\u6210\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u57fa\u6570\u30bd\u30fc\u30c8\u306e\u3088\u3046\u306a\u3088\u308a\u52b9\u7387\u7684\u306a\u30a2\u30d7\u30ed\u30fc\u30c1\u3092\u9069\u7528\u3067\u304d\u3001\u6642\u9593\u8907\u96d1\u5ea6\u3092O(nk)\u306b\u524a\u6e1b\u3067\u304d\u307e\u3059\u3002\u3053\u3053\u3067k\u306f\u6841\u6570\u3067\u3059\u3002\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u51e6\u7406\u3059\u308b\u969b\u3001\u7bc0\u7d04\u3055\u308c\u305f\u6642\u9593\u306f\u91cd\u8981\u306a\u4fa1\u5024\u306b\u5909\u308f\u308a\u307e\u3059 \u2014 \u30b3\u30b9\u30c8\u306e\u524a\u6e1b\u3001\u30e6\u30fc\u30b6\u30fc\u30a8\u30af\u30b9\u30da\u30ea\u30a8\u30f3\u30b9\u306e\u5411\u4e0a\u3001\u30b7\u30b9\u30c6\u30e0\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306e\u5411\u4e0a\u3002

        \u30a8\u30f3\u30b8\u30cb\u30a2\u30ea\u30f3\u30b0\u3067\u306f\u3001\u591a\u304f\u306e\u554f\u984c\u3092\u6700\u9069\u306b\u89e3\u6c7a\u3059\u308b\u3053\u3068\u306f\u56f0\u96e3\u3067\u3059\u3002\u307b\u3068\u3093\u3069\u306f\u300c\u6e96\u6700\u9069\u300d\u89e3\u6c7a\u7b56\u3067\u5bfe\u51e6\u3055\u308c\u307e\u3059\u3002\u554f\u984c\u306e\u96e3\u3057\u3055\u306f\u3001\u305d\u306e\u56fa\u6709\u306e\u8907\u96d1\u3055\u3060\u3051\u3067\u306a\u304f\u3001\u305d\u308c\u306b\u53d6\u308a\u7d44\u3080\u4eba\u306e\u77e5\u8b58\u3068\u7d4c\u9a13\u306b\u3082\u4f9d\u5b58\u3057\u307e\u3059\u3002\u5c02\u9580\u77e5\u8b58\u3068\u7d4c\u9a13\u304c\u6df1\u3044\u307b\u3069\u3001\u5206\u6790\u304c\u3088\u308a\u5fb9\u5e95\u7684\u306b\u306a\u308a\u3001\u554f\u984c\u3092\u3088\u308a\u512a\u96c5\u306b\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_introduction/what_is_dsa/","title":"1.2 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u306f\u4f55\u304b","text":""},{"location":"chapter_introduction/what_is_dsa/#121","title":"1.2.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9a\u7fa9","text":"

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u6709\u9650\u6642\u9593\u5185\u3067\u7279\u5b9a\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306e\u4e00\u9023\u306e\u6307\u793a\u307e\u305f\u306f\u30b9\u30c6\u30c3\u30d7\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u7279\u5fb4\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u554f\u984c\u304c\u660e\u78ba\u306b\u5b9a\u7fa9\u3055\u308c\u3066\u304a\u308a\u3001\u5165\u529b\u3068\u51fa\u529b\u306e\u660e\u78ba\u306a\u5b9a\u7fa9\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002
        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u5b9f\u884c\u53ef\u80fd\u3067\u3001\u6709\u9650\u306e\u56de\u6570\u306e\u30b9\u30c6\u30c3\u30d7\u3001\u6642\u9593\u3001\u30e1\u30e2\u30ea\u7a7a\u9593\u5185\u3067\u5b8c\u4e86\u3067\u304d\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002
        • \u5404\u30b9\u30c6\u30c3\u30d7\u306b\u306f\u660e\u78ba\u306a\u610f\u5473\u304c\u3042\u308a\u307e\u3059\u3002\u540c\u3058\u5165\u529b\u3068\u6761\u4ef6\u306e\u4e0b\u3067\u51fa\u529b\u306f\u4e00\u8cab\u3057\u3066\u540c\u3058\u3067\u3059\u3002
        "},{"location":"chapter_introduction/what_is_dsa/#122","title":"1.2.2 \u00a0 \u30c7\u30fc\u30bf\u69cb\u9020\u306e\u5b9a\u7fa9","text":"

        \u30c7\u30fc\u30bf\u69cb\u9020\u306f\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u5185\u3067\u30c7\u30fc\u30bf\u3092\u7d44\u7e54\u3057\u4fdd\u5b58\u3059\u308b\u65b9\u6cd5\u3067\u3001\u4ee5\u4e0b\u306e\u8a2d\u8a08\u76ee\u6a19\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u30e1\u30e2\u30ea\u3092\u7bc0\u7d04\u3059\u308b\u305f\u3081\u306b\u7a7a\u9593\u5360\u6709\u3092\u6700\u5c0f\u5316\u3059\u308b\u3002
        • \u30c7\u30fc\u30bf\u64cd\u4f5c\u3092\u53ef\u80fd\u306a\u9650\u308a\u9ad8\u901f\u306b\u3057\u3001\u30c7\u30fc\u30bf\u306e\u30a2\u30af\u30bb\u30b9\u3001\u8ffd\u52a0\u3001\u524a\u9664\u3001\u66f4\u65b0\u306a\u3069\u3092\u30ab\u30d0\u30fc\u3059\u308b\u3002
        • \u52b9\u7387\u7684\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u5b9f\u884c\u3092\u53ef\u80fd\u306b\u3059\u308b\u305f\u3081\u306b\u3001\u7c21\u6f54\u306a\u30c7\u30fc\u30bf\u8868\u73fe\u3068\u8ad6\u7406\u60c5\u5831\u3092\u63d0\u4f9b\u3059\u308b\u3002

        \u30c7\u30fc\u30bf\u69cb\u9020\u306e\u8a2d\u8a08\u306f\u30d0\u30e9\u30f3\u30b9\u3092\u53d6\u308b\u884c\u70ba\u3067\u3042\u308a\u3001\u3057\u3070\u3057\u3070\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u304c\u5fc5\u8981\u3067\u3059\u3002\u4e00\u3064\u306e\u5074\u9762\u3092\u6539\u5584\u3057\u305f\u3044\u5834\u5408\u3001\u3057\u3070\u3057\u3070\u5225\u306e\u5074\u9762\u3067\u59a5\u5354\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306f2\u3064\u306e\u4f8b\u3067\u3059\uff1a

        • \u914d\u5217\u3068\u6bd4\u8f03\u3057\u3066\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u30c7\u30fc\u30bf\u306e\u8ffd\u52a0\u3068\u524a\u9664\u306b\u304a\u3044\u3066\u3088\u308a\u4fbf\u5229\u3067\u3059\u304c\u3001\u30c7\u30fc\u30bf\u30a2\u30af\u30bb\u30b9\u901f\u5ea6\u3092\u72a0\u7272\u306b\u3057\u307e\u3059\u3002
        • \u9023\u7d50\u30ea\u30b9\u30c8\u3068\u6bd4\u8f03\u3057\u3066\u3001\u30b0\u30e9\u30d5\u306f\u3088\u308a\u8c4a\u5bcc\u306a\u8ad6\u7406\u60c5\u5831\u3092\u63d0\u4f9b\u3057\u307e\u3059\u304c\u3001\u3088\u308a\u591a\u304f\u306e\u30e1\u30e2\u30ea\u7a7a\u9593\u304c\u5fc5\u8981\u3067\u3059\u3002
        "},{"location":"chapter_introduction/what_is_dsa/#123","title":"1.2.3 \u00a0 \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u95a2\u4fc2","text":"

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u9ad8\u5ea6\u306b\u95a2\u9023\u3057\u3001\u5bc6\u63a5\u306b\u7d71\u5408\u3055\u308c\u3066\u304a\u308a\u3001\u5177\u4f53\u7684\u306b\u306f\u4ee5\u4e0b\u306e3\u3064\u306e\u5074\u9762\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u30c7\u30fc\u30bf\u69cb\u9020\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u57fa\u790e\u3067\u3059\u3002\u69cb\u9020\u5316\u3055\u308c\u305f\u30c7\u30fc\u30bf\u4fdd\u5b58\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u305f\u3081\u306e\u30c7\u30fc\u30bf\u64cd\u4f5c\u65b9\u6cd5\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002
        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u6d3b\u529b\u3092\u6ce8\u5165\u3057\u307e\u3059\u3002\u30c7\u30fc\u30bf\u69cb\u9020\u3060\u3051\u3067\u306f\u30c7\u30fc\u30bf\u60c5\u5831\u3092\u4fdd\u5b58\u3059\u308b\u3060\u3051\u3067\u3059\u3002\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5fdc\u7528\u306b\u3088\u3063\u3066\u3001\u7279\u5b9a\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002
        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u7570\u306a\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u57fa\u3065\u3044\u3066\u5b9f\u88c5\u3067\u304d\u308b\u3053\u3068\u304c\u591a\u3044\u3067\u3059\u304c\u3001\u5b9f\u884c\u52b9\u7387\u306f\u5927\u304d\u304f\u7570\u306a\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002\u9069\u5207\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u9078\u629e\u3059\u308b\u3053\u3068\u304c\u9375\u3067\u3059\u3002

        \u56f3 1-4 \u00a0 \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u95a2\u4fc2

        \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30d6\u30ed\u30c3\u30af\u306e\u30bb\u30c3\u30c8\u306b\u4f8b\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u30d6\u30ed\u30c3\u30af\u30bb\u30c3\u30c8\u306b\u306f\u591a\u6570\u306e\u30d4\u30fc\u30b9\u304c\u542b\u307e\u308c\u3001\u8a73\u7d30\u306a\u7d44\u307f\u7acb\u3066\u8aac\u660e\u66f8\u304c\u4ed8\u3044\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u8aac\u660e\u66f8\u306b\u6bb5\u968e\u7684\u306b\u5f93\u3046\u3053\u3068\u3067\u3001\u8907\u96d1\u306a\u30d6\u30ed\u30c3\u30af\u30e2\u30c7\u30eb\u3092\u69cb\u7bc9\u3067\u304d\u307e\u3059\u3002

        \u56f3 1-5 \u00a0 \u30d6\u30ed\u30c3\u30af\u306e\u7d44\u307f\u7acb\u3066

        \u4e21\u8005\u306e\u8a73\u7d30\u306a\u5bfe\u5fdc\u95a2\u4fc2\u306f\u4ee5\u4e0b\u306e\u8868\u306b\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        \u8868 1-1 \u00a0 \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u30d6\u30ed\u30c3\u30af\u3068\u6bd4\u8f03

        \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0 \u30d6\u30ed\u30c3\u30af \u5165\u529b\u30c7\u30fc\u30bf \u672a\u7d44\u307f\u7acb\u3066\u306e\u30d6\u30ed\u30c3\u30af \u30c7\u30fc\u30bf\u69cb\u9020 \u30d6\u30ed\u30c3\u30af\u306e\u7d44\u7e54\u3001\u5f62\u72b6\u3001\u30b5\u30a4\u30ba\u3001\u63a5\u7d9a\u306a\u3069\u3092\u542b\u3080 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0 \u30d6\u30ed\u30c3\u30af\u3092\u671b\u307e\u3057\u3044\u5f62\u72b6\u306b\u7d44\u307f\u7acb\u3066\u308b\u4e00\u9023\u306e\u30b9\u30c6\u30c3\u30d7 \u51fa\u529b\u30c7\u30fc\u30bf \u5b8c\u6210\u3057\u305f\u30d6\u30ed\u30c3\u30af\u30e2\u30c7\u30eb

        \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u304b\u3089\u72ec\u7acb\u3057\u3066\u3044\u308b\u3053\u3068\u306f\u6ce8\u76ee\u306b\u5024\u3057\u307e\u3059\u3002\u3053\u306e\u7406\u7531\u306b\u3088\u308a\u3001\u3053\u306e\u672c\u306f\u8907\u6570\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u306e\u5b9f\u88c5\u3092\u63d0\u4f9b\u3067\u304d\u307e\u3059\u3002

        \u6163\u7fd2\u7684\u306a\u7565\u8a9e

        \u5b9f\u751f\u6d3b\u306e\u8b70\u8ad6\u3067\u306f\u3001\u300c\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u300d\u3092\u5358\u7d14\u306b\u300c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u300d\u3068\u547c\u3076\u3053\u3068\u304c\u3088\u304f\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u3088\u304f\u77e5\u3089\u308c\u305fLeetCode\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u306f\u3001\u5b9f\u969b\u306b\u306f\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u4e21\u65b9\u306e\u77e5\u8b58\u3092\u30c6\u30b9\u30c8\u3057\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_preface/","title":"\u7b2c 0 \u7ae0 \u00a0 \u5e8f\u6587","text":"

        Abstract

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u7f8e\u3057\u3044\u4ea4\u97ff\u66f2\u306e\u3088\u3046\u3067\u3001\u30b3\u30fc\u30c9\u306e\u4e00\u884c\u4e00\u884c\u304c\u30ea\u30ba\u30e0\u306e\u3088\u3046\u306b\u6d41\u308c\u3066\u3044\u307e\u3059\u3002

        \u3053\u306e\u672c\u304c\u3042\u306a\u305f\u306e\u5fc3\u306e\u4e2d\u3067\u9759\u304b\u306b\u97ff\u304d\u3001\u72ec\u7279\u3067\u6df1\u3044\u65cb\u5f8b\u3092\u6b8b\u3059\u3053\u3068\u3092\u9858\u3063\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_preface/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 0.1 \u00a0 \u3053\u306e\u672c\u306b\u3064\u3044\u3066
        • 0.2 \u00a0 \u672c\u66f8\u306e\u4f7f\u3044\u65b9
        • 0.3 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_preface/about_the_book/","title":"0.1 \u00a0 \u3053\u306e\u672c\u306b\u3064\u3044\u3066","text":"

        \u3053\u306e\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306f\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u95a2\u3059\u308b\u7121\u6599\u3067\u521d\u5fc3\u8005\u306b\u3084\u3055\u3057\u3044\u30af\u30e9\u30c3\u30b7\u30e5\u30b3\u30fc\u30b9\u306e\u4f5c\u6210\u3092\u76ee\u6307\u3057\u3066\u3044\u307e\u3059\u3002

        • \u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u4ed8\u304d\u306e\u56f3\u89e3\u3001\u7406\u89e3\u3057\u3084\u3059\u3044\u5185\u5bb9\u3001\u6ed1\u3089\u304b\u306a\u5b66\u7fd2\u66f2\u7dda\u306b\u3088\u308a\u3001\u521d\u5fc3\u8005\u304c\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u300c\u77e5\u8b58\u30de\u30c3\u30d7\u300d\u3092\u63a2\u7d22\u3059\u308b\u306e\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002
        • \u30ef\u30f3\u30af\u30ea\u30c3\u30af\u3067\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3067\u304d\u308b\u305f\u3081\u3001\u8aad\u8005\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30b9\u30ad\u30eb\u306e\u5411\u4e0a\u3068\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52d5\u4f5c\u539f\u7406\u304a\u3088\u3073\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u57fa\u790e\u5b9f\u88c5\u306e\u7406\u89e3\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002
        • \u6559\u3048\u308b\u3053\u3068\u306b\u3088\u308b\u5b66\u7fd2\u3092\u4fc3\u9032\u3057\u3001\u8cea\u554f\u3084\u6d1e\u5bdf\u306e\u5171\u6709\u3092\u81ea\u7531\u306b\u884c\u3063\u3066\u304f\u3060\u3055\u3044\u3002\u8b70\u8ad6\u3092\u901a\u3058\u3066\u4e00\u7dd2\u306b\u6210\u9577\u3057\u307e\u3057\u3087\u3046\u3002
        "},{"location":"chapter_preface/about_the_book/#011","title":"0.1.1 \u00a0 \u5bfe\u8c61\u8aad\u8005","text":"

        \u3082\u3057\u3042\u306a\u305f\u304c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u89e6\u308c\u305f\u3070\u304b\u308a\u3067\u7d4c\u9a13\u304c\u9650\u3089\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u307e\u305f\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3042\u308b\u7a0b\u5ea6\u306e\u7d4c\u9a13\u3092\u7a4d\u3093\u3067\u3044\u3066\u3082\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u3064\u3044\u3066\u66d6\u6627\u306a\u7406\u89e3\u3057\u304b\u306a\u304f\u3001\u5e38\u306b\u300c\u5206\u304b\u3063\u305f\u300d\u3068\u300c\u3046\u30fc\u3093\u300d\u306e\u9593\u3092\u884c\u304d\u6765\u3057\u3066\u3044\u308b\u5834\u5408\u3001\u3053\u306e\u672c\u306f\u3042\u306a\u305f\u306e\u305f\u3081\u306e\u3082\u306e\u3067\u3059\uff01

        \u3059\u3067\u306b\u3042\u308b\u7a0b\u5ea6\u306e\u554f\u984c\u89e3\u6c7a\u7d4c\u9a13\u3092\u7a4d\u3093\u3067\u304a\u308a\u3001\u307b\u3068\u3093\u3069\u306e\u30bf\u30a4\u30d7\u306e\u554f\u984c\u306b\u7cbe\u901a\u3057\u3066\u3044\u308b\u5834\u5408\u3001\u3053\u306e\u672c\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u77e5\u8b58\u4f53\u7cfb\u3092\u5fa9\u7fd2\u3057\u6574\u7406\u3059\u308b\u306e\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002\u30ea\u30dd\u30b8\u30c8\u30ea\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306f\u300c\u554f\u984c\u89e3\u6c7a\u30c4\u30fc\u30eb\u30ad\u30c3\u30c8\u300d\u3084\u300c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30c1\u30fc\u30c8\u30b7\u30fc\u30c8\u300d\u3068\u3057\u3066\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002

        \u3082\u3057\u3042\u306a\u305f\u304c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5c02\u9580\u5bb6\u3067\u3042\u308c\u3070\u3001\u8cb4\u91cd\u306a\u63d0\u6848\u3092\u3044\u305f\u3060\u304f\u304b\u3001\u53c2\u52a0\u3057\u3066\u5354\u529b\u3057\u3066\u3044\u305f\u3060\u3051\u308c\u3070\u3068\u601d\u3044\u307e\u3059\u3002

        \u524d\u63d0\u6761\u4ef6

        \u5c11\u306a\u304f\u3068\u3082\u4e00\u3064\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u7c21\u5358\u306a\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u8aad\u3080\u3053\u3068\u304c\u3067\u304d\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_preface/about_the_book/#012","title":"0.1.2 \u00a0 \u5185\u5bb9\u69cb\u6210","text":"

        \u672c\u66f8\u306e\u4e3b\u306a\u5185\u5bb9\u3092\u4e0b\u56f3\u306b\u793a\u3057\u307e\u3059\u3002

        • \u8a08\u7b97\u91cf\u89e3\u6790: \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u8a55\u4fa1\u3059\u308b\u5074\u9762\u3068\u65b9\u6cd5\u3092\u63a2\u6c42\u3057\u307e\u3059\u3002\u6642\u9593\u8a08\u7b97\u91cf\u3068\u7a7a\u9593\u8a08\u7b97\u91cf\u3092\u5c0e\u51fa\u3059\u308b\u65b9\u6cd5\u3001\u304a\u3088\u3073\u4e00\u822c\u7684\u306a\u30bf\u30a4\u30d7\u3068\u4f8b\u3092\u6271\u3044\u307e\u3059\u3002
        • \u30c7\u30fc\u30bf\u69cb\u9020: \u57fa\u672c\u7684\u306a\u30c7\u30fc\u30bf\u578b\u3001\u5206\u985e\u65b9\u6cd5\u3001\u5b9a\u7fa9\u3001\u9577\u6240\u3068\u77ed\u6240\u3001\u4e00\u822c\u7684\u306a\u64cd\u4f5c\u3001\u30bf\u30a4\u30d7\u3001\u5fdc\u7528\u3001\u304a\u3088\u3073\u914d\u5217\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u30b9\u30bf\u30c3\u30af\u3001\u30ad\u30e5\u30fc\u3001\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3001\u6728\u3001\u30d2\u30fc\u30d7\u3001\u30b0\u30e9\u30d5\u306a\u3069\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u5b9f\u88c5\u65b9\u6cd5\u306b\u7126\u70b9\u3092\u5f53\u3066\u307e\u3059\u3002
        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0: \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u5b9a\u7fa9\u3057\u3001\u305d\u306e\u9577\u6240\u3068\u77ed\u6240\u3001\u52b9\u7387\u6027\u3001\u5fdc\u7528\u30b7\u30ca\u30ea\u30aa\u3001\u554f\u984c\u89e3\u6c7a\u30b9\u30c6\u30c3\u30d7\u306b\u3064\u3044\u3066\u8b70\u8ad6\u3057\u3001\u691c\u7d22\u3001\u30bd\u30fc\u30c8\u3001\u5206\u5272\u7d71\u6cbb\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306a\u3069\u3001\u3055\u307e\u3056\u307e\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u30b5\u30f3\u30d7\u30eb\u554f\u984c\u3092\u542b\u307f\u307e\u3059\u3002

        \u56f3 0-1 \u00a0 \u672c\u66f8\u306e\u4e3b\u306a\u5185\u5bb9

        "},{"location":"chapter_preface/about_the_book/#013","title":"0.1.3 \u00a0 \u8b1d\u8f9e","text":"

        \u3053\u306e\u672c\u306f\u3001\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u30b3\u30df\u30e5\u30cb\u30c6\u30a3\u306e\u591a\u304f\u306e\u8ca2\u732e\u8005\u306e\u5171\u540c\u52aa\u529b\u306b\u3088\u308a\u7d99\u7d9a\u7684\u306b\u6539\u5584\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u6642\u9593\u3068\u30a8\u30cd\u30eb\u30ae\u30fc\u3092\u6295\u8cc7\u3057\u3066\u304f\u3060\u3055\u3063\u305f\u5404\u57f7\u7b46\u8005\u306b\u611f\u8b1d\u3044\u305f\u3057\u307e\u3059\u3002GitHub\u3067\u751f\u6210\u3055\u308c\u305f\u9806\u5e8f\u3067\u8a18\u8f09\u3055\u308c\u3066\u3044\u307e\u3059: krahets, coderonion, Gonglja, nuomi1, Reanon, justin-tse, hpstory, danielsss, curtishd, night-cruise, S-N-O-R-L-A-X, rongyi, msk397, gvenusleo, khoaxuantu, rivertwilight, K3v123, gyt95, zhuoqinyue, yuelinxin, Zuoxun, mingXta, Phoenix0415, FangYuan33, GN-Yu, longsizhuo, IsChristina, xBLACKICEx, guowei-gong, Cathay-Chen, pengchzn, QiLOL, magentaqin, hello-ikun, JoseHung, qualifier1024, thomasq0, sunshinesDL, L-Super, Guanngxu, Transmigration-zhou, WSL0809, Slone123c, lhxsm, yuan0221, what-is-me, Shyam-Chen, theNefelibatas, longranger2, codeberg-user, xiongsp, JeffersonHuang, prinpal, seven1240, Wonderdch, malone6, xiaomiusa87, gaofer, bluebean-cloud, a16su, SamJin98, hongyun-robot, nanlei, XiaChuerwu, yd-j, iron-irax, mgisr, steventimes, junminhong, heshuyue, danny900714, MolDuM, Nigh, Dr-XYZ, XC-Zero, reeswell, PXG-XPG, NI-SW, Horbin-Magician, Enlightenus, YangXuanyi, beatrix-chan, DullSword, xjr7670, jiaxianhua, qq909244296, iStig, boloboloda, hts0000, gledfish, wenjianmin, keshida, kilikilikid, lclc6, lwbaptx, linyejoe2, liuxjerry, llql1211, fbigm, echo1937, szu17dmy, dshlstarr, Yucao-cy, coderlef, czruby, bongbongbakudan, beintentional, ZongYangL, ZhongYuuu, ZhongGuanbin, hezhizhen, linzeyan, ZJKung, luluxia, xb534, ztkuaikuai, yw-1021, ElaBosak233, baagod, zhouLion, yishangzhang, yi427, yanedie, yabo083, weibk, wangwang105, th1nk3r-ing, tao363, 4yDX3906, syd168, sslmj2020, smilelsb, siqyka, selear, sdshaoda, Xi-Row, popozhu, nuquist19, noobcodemaker, XiaoK29, chadyi, lyl625760, lucaswangdev, 0130w, shanghai-Jerry, EJackYang, Javesun99, eltociear, lipusheng, KNChiu, BlindTerran, ShiMaRing, lovelock, FreddieLi, FloranceYeh, fanchenggang, gltianwen, goerll, nedchu, curly210102, CuB3y0nd, KraHsu, CarrotDLaw, youshaoXG, bubble9um, Asashishi, Asa0oo0o0o, fanenr, eagleanurag, akshiterate, 52coder, foursevenlove, KorsChen, GaochaoZhu, hopkings2008, yang-le, realwujing, Evilrabbit520, Umer-Jahangir, Turing-1024-Lee, Suremotoo, paoxiaomooo, Chieko-Seren, Allen-Scai, ymmmas, Risuntsy, Richard-Zhang1019, RafaelCaso, qingpeng9802, primexiao, Urbaner3, zhongfq, nidhoggfgg, MwumLi, CreatorMetaSky, martinx, ZnYang2018, hugtyftg, logan-qiu, psychelzh, Keynman, KeiichiKasai, and KawaiiAsh\u3002

        \u3053\u306e\u672c\u306e\u30b3\u30fc\u30c9\u30ec\u30d3\u30e5\u30fc\u4f5c\u696d\u306f\u3001coderonion, Gonglja, gvenusleo, hpstory, justin\u2010tse, khoaxuantu, krahets, night-cruise, nuomi1, Reanon and rongyi\uff08\u30a2\u30eb\u30d5\u30a1\u30d9\u30c3\u30c8\u9806\uff09\u306b\u3088\u3063\u3066\u5b8c\u4e86\u3055\u308c\u307e\u3057\u305f\u3002\u5f7c\u3089\u306e\u6642\u9593\u3068\u52aa\u529b\u306b\u611f\u8b1d\u3057\u3001\u69d8\u3005\u306a\u8a00\u8a9e\u3067\u306e\u30b3\u30fc\u30c9\u306e\u6a19\u6e96\u5316\u3068\u7d71\u4e00\u6027\u3092\u78ba\u4fdd\u3057\u3066\u304f\u3060\u3055\u3044\u307e\u3057\u305f\u3002

        \u3053\u306e\u672c\u306e\u7e41\u4f53\u5b57\u4e2d\u56fd\u8a9e\u7248\u306f Shyam-Chen \u3068 Dr-XYZ \u306b\u3088\u3063\u3066\u30ec\u30d3\u30e5\u30fc\u3055\u308c\u3001\u82f1\u8a9e\u7248\u306f yuelinxin, K3v123, QiLOL, Phoenix0415, SamJin98, yanedie, RafaelCaso, pengchzn, thomasq0, and magentaqin \u306b\u3088\u3063\u3066\u30ec\u30d3\u30e5\u30fc\u3055\u308c\u307e\u3057\u305f\u3002\u5f7c\u3089\u306e\u7d99\u7d9a\u7684\u306a\u8ca2\u732e\u306b\u3088\u308a\u3001\u3053\u306e\u672c\u304c\u3088\u308a\u5e83\u3044\u8aad\u8005\u306b\u5c4a\u304d\u3001\u5f79\u7acb\u3064\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u3053\u306e\u672c\u306e\u5236\u4f5c\u904e\u7a0b\u306b\u304a\u3044\u3066\u3001\u591a\u304f\u306e\u65b9\u3005\u304b\u3089\u8cb4\u91cd\u306a\u652f\u63f4\u3092\u3044\u305f\u3060\u304d\u307e\u3057\u305f\u3002\u3053\u308c\u3089\u306b\u9650\u5b9a\u3055\u308c\u307e\u305b\u3093\u304c\uff1a

        • \u4f1a\u793e\u3067\u306e\u30e1\u30f3\u30bf\u30fc\u3001\u674e\u7199\u535a\u58eb\u306b\u611f\u8b1d\u3057\u307e\u3059\u3002\u3042\u308b\u4f1a\u8a71\u3067\u300c\u65e9\u304f\u884c\u52d5\u3057\u308d\u300d\u3068\u52b1\u307e\u3057\u3066\u3044\u305f\u3060\u304d\u3001\u3053\u306e\u672c\u3092\u66f8\u304f\u6c7a\u610f\u3092\u56fa\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002
        • \u30ac\u30fc\u30eb\u30d5\u30ec\u30f3\u30c9\u306eBubble\u306b\u611f\u8b1d\u3057\u307e\u3059\u3002\u3053\u306e\u672c\u306e\u6700\u521d\u306e\u8aad\u8005\u3068\u3057\u3066\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u521d\u5fc3\u8005\u306e\u8996\u70b9\u304b\u3089\u591a\u304f\u306e\u8cb4\u91cd\u306a\u63d0\u6848\u3092\u3044\u305f\u3060\u304d\u3001\u3053\u306e\u672c\u3092\u521d\u5fc3\u8005\u306b\u3088\u308a\u9069\u3057\u305f\u3082\u306e\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u307e\u3057\u305f\u3002
        • Tengbao\u3001Qibao\u3001Feibao\u306b\u611f\u8b1d\u3057\u307e\u3059\u3002\u3053\u306e\u672c\u306e\u30af\u30ea\u30a8\u30a4\u30c6\u30a3\u30d6\u306a\u540d\u524d\u3092\u8003\u3048\u3066\u304f\u3060\u3055\u308a\u3001\u307f\u3093\u306a\u304c\u521d\u3081\u3066\u300cHello World!\u300d\u3092\u66f8\u3044\u305f\u6642\u306e\u7d20\u6674\u3089\u3057\u3044\u601d\u3044\u51fa\u3092\u547c\u3073\u8d77\u3053\u3057\u3066\u304f\u308c\u307e\u3057\u305f\u3002
        • Xiaoquan\u306b\u611f\u8b1d\u3057\u307e\u3059\u3002\u77e5\u7684\u8ca1\u7523\u306b\u95a2\u3059\u308b\u5c02\u9580\u7684\u306a\u652f\u63f4\u3092\u63d0\u4f9b\u3057\u3066\u304f\u3060\u3055\u308a\u3001\u3053\u306e\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u672c\u306e\u958b\u767a\u306b\u304a\u3044\u3066\u91cd\u8981\u306a\u5f79\u5272\u3092\u679c\u305f\u3057\u3066\u304f\u3060\u3055\u3044\u307e\u3057\u305f\u3002
        • Sutong\u306b\u611f\u8b1d\u3057\u307e\u3059\u3002\u3053\u306e\u672c\u306e\u7f8e\u3057\u3044\u30ab\u30d0\u30fc\u3068\u30ed\u30b4\u3092\u30c7\u30b6\u30a4\u30f3\u3057\u3066\u304f\u3060\u3055\u308a\u3001\u79c1\u306e\u8981\u6c42\u3067\u4f55\u5ea6\u3082\u4fee\u6b63\u3092\u8f9b\u62b1\u5f37\u304f\u884c\u3063\u3066\u304f\u3060\u3055\u3044\u307e\u3057\u305f\u3002
        • @squidfunk \u306b\u611f\u8b1d\u3057\u307e\u3059\u3002\u57f7\u7b46\u3068\u7d44\u7248\u306e\u63d0\u6848\u3001\u304a\u3088\u3073\u5f7c\u304c\u958b\u767a\u3057\u305f\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u30c6\u30fc\u30de Material-for-MkDocs \u3092\u63d0\u4f9b\u3057\u3066\u304f\u3060\u3055\u3044\u307e\u3057\u305f\u3002

        \u57f7\u7b46\u306e\u904e\u7a0b\u3067\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u95a2\u3059\u308b\u591a\u6570\u306e\u6559\u79d1\u66f8\u3084\u8a18\u4e8b\u3092\u6df1\u304f\u7814\u7a76\u3057\u307e\u3057\u305f\u3002\u3053\u308c\u3089\u306e\u4f5c\u54c1\u306f\u6a21\u7bc4\u7684\u306a\u30e2\u30c7\u30eb\u3068\u3057\u3066\u6a5f\u80fd\u3057\u3001\u3053\u306e\u672c\u306e\u5185\u5bb9\u306e\u6b63\u78ba\u6027\u3068\u54c1\u8cea\u3092\u78ba\u4fdd\u3057\u3066\u304f\u3060\u3055\u3044\u307e\u3057\u305f\u3002\u5148\u4eba\u306e\u65b9\u3005\u306e\u8cb4\u91cd\u306a\u8ca2\u732e\u306b\u611f\u8b1d\u3044\u305f\u3057\u307e\u3059\uff01

        \u3053\u306e\u672c\u306f\u3001\u7406\u8ad6\u3068\u5b9f\u8df5\u3092\u7d44\u307f\u5408\u308f\u305b\u305f\u5b66\u7fd2\u3092\u63d0\u5531\u3057\u3066\u304a\u308a\u3001\u3053\u306e\u70b9\u3067 \"Dive into Deep Learning\" \u304b\u3089\u30a4\u30f3\u30b9\u30d4\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u53d7\u3051\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u512a\u308c\u305f\u672c\u3092\u3059\u3079\u3066\u306e\u8aad\u8005\u306b\u5f37\u304f\u304a\u52e7\u3081\u3057\u307e\u3059\u3002

        \u7d99\u7d9a\u7684\u306a\u652f\u63f4\u3068\u52b1\u307e\u3057\u306b\u3088\u308a\u3001\u3053\u306e\u8208\u5473\u6df1\u3044\u4ed5\u4e8b\u3092\u3059\u308b\u3053\u3068\u3092\u53ef\u80fd\u306b\u3057\u3066\u304f\u3060\u3055\u3063\u305f\u4e21\u89aa\u306b\u5fc3\u304b\u3089\u611f\u8b1d\u3044\u305f\u3057\u307e\u3059\u3002

        "},{"location":"chapter_preface/suggestions/","title":"0.2 \u00a0 \u8aad\u307f\u65b9","text":"

        Tip

        \u6700\u826f\u306e\u8aad\u66f8\u4f53\u9a13\u306e\u305f\u3081\u306b\u3001\u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3092\u901a\u8aad\u3059\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002

        "},{"location":"chapter_preface/suggestions/#021","title":"0.2.1 \u00a0 \u8a18\u8ff0\u898f\u5247","text":"
        • \u30bf\u30a4\u30c8\u30eb\u306e\u5f8c\u306b\u300c*\u300d\u304c\u4ed8\u3044\u305f\u7ae0\u306f\u4efb\u610f\u3067\u3042\u308a\u3001\u6bd4\u8f03\u7684\u96e3\u6613\u5ea6\u306e\u9ad8\u3044\u5185\u5bb9\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002\u6642\u9593\u306b\u5236\u7d04\u304c\u3042\u308b\u5834\u5408\u306f\u3001\u3053\u308c\u3089\u3092\u30b9\u30ad\u30c3\u30d7\u3059\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002
        • \u6280\u8853\u7528\u8a9e\u306f\u592a\u5b57\uff08\u5370\u5237\u7248\u304a\u3088\u3073PDF\u7248\uff09\u307e\u305f\u306f\u4e0b\u7dda\uff08Web\u7248\uff09\u3067\u8868\u793a\u3055\u308c\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u914d\u5217\u306a\u3069\u3067\u3059\u3002\u6280\u8853\u6587\u66f8\u3092\u3088\u308a\u826f\u304f\u7406\u89e3\u3059\u308b\u305f\u3081\u306b\u3001\u3053\u308c\u3089\u306b\u6163\u308c\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002
        • **\u592a\u5b57\u306e\u30c6\u30ad\u30b9\u30c8**\u306f\u91cd\u8981\u306a\u5185\u5bb9\u3084\u8981\u7d04\u6587\u3092\u793a\u3057\u3001\u7279\u5225\u306a\u6ce8\u610f\u3092\u6255\u3046\u4fa1\u5024\u304c\u3042\u308a\u307e\u3059\u3002
        • \u7279\u5b9a\u306e\u610f\u5473\u3092\u6301\u3064\u5358\u8a9e\u3084\u53e5\u306f\u300c\u5f15\u7528\u7b26\u300d\u3067\u793a\u3055\u308c\u3001\u66d6\u6627\u3055\u3092\u907f\u3051\u307e\u3059\u3002
        • \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u9593\u3067\u4e00\u81f4\u3057\u306a\u3044\u7528\u8a9e\u306b\u3064\u3044\u3066\u306f\u3001\u3053\u306e\u672c\u306fPython\u306b\u5f93\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001null\u3092\u610f\u5473\u3059\u308b\u305f\u3081\u306bNone\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u3053\u306e\u672c\u306f\u3001\u3088\u308a\u30b3\u30f3\u30d1\u30af\u30c8\u306a\u30b3\u30f3\u30c6\u30f3\u30c4\u30ec\u30a4\u30a2\u30a6\u30c8\u3068\u5f15\u304d\u63db\u3048\u306b\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u30b3\u30e1\u30f3\u30c8\u898f\u7d04\u3092\u90e8\u5206\u7684\u306b\u7121\u8996\u3057\u3066\u3044\u307e\u3059\u3002\u30b3\u30e1\u30f3\u30c8\u306f\u4e3b\u306b3\u3064\u306e\u30bf\u30a4\u30d7\u3067\u69cb\u6210\u3055\u308c\u3066\u3044\u307e\u3059\uff1a\u30bf\u30a4\u30c8\u30eb\u30b3\u30e1\u30f3\u30c8\u3001\u5185\u5bb9\u30b3\u30e1\u30f3\u30c8\u3001\u8907\u6570\u884c\u30b3\u30e1\u30f3\u30c8\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin
        \"\"\"\u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8\"\"\"\n\n# \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n\"\"\"\n\u8907\u6570\u884c\n\u30b3\u30e1\u30f3\u30c8\n\"\"\"\n
        /* \u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8 */\n\n// \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n/**\n * \u8907\u6570\u884c\n * \u30b3\u30e1\u30f3\u30c8\n */\n
        /* \u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8 */\n\n// \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n/**\n * \u8907\u6570\u884c\n * \u30b3\u30e1\u30f3\u30c8\n */\n
        /* \u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8 */\n\n// \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n/**\n * \u8907\u6570\u884c\n * \u30b3\u30e1\u30f3\u30c8\n */\n
        /* \u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8 */\n\n// \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n/**\n * \u8907\u6570\u884c\n * \u30b3\u30e1\u30f3\u30c8\n */\n
        /* \u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8 */\n\n// \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n/**\n * \u8907\u6570\u884c\n * \u30b3\u30e1\u30f3\u30c8\n */\n
        /* \u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8 */\n\n// \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n/**\n * \u8907\u6570\u884c\n * \u30b3\u30e1\u30f3\u30c8\n */\n
        /* \u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8 */\n\n// \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n/**\n * \u8907\u6570\u884c\n * \u30b3\u30e1\u30f3\u30c8\n */\n
        /* \u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8 */\n\n// \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n/**\n * \u8907\u6570\u884c\n * \u30b3\u30e1\u30f3\u30c8\n */\n
        /* \u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8 */\n\n// \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n/**\n * \u8907\u6570\u884c\n * \u30b3\u30e1\u30f3\u30c8\n */\n
        /* \u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8 */\n\n// \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n/**\n * \u8907\u6570\u884c\n * \u30b3\u30e1\u30f3\u30c8\n */\n
        /* \u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8 */\n\n// \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n/**\n * \u8907\u6570\u884c\n * \u30b3\u30e1\u30f3\u30c8\n */\n
        "},{"location":"chapter_preface/suggestions/#022","title":"0.2.2 \u00a0 \u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u56f3\u89e3\u306b\u3088\u308b\u52b9\u7387\u7684\u5b66\u7fd2","text":"

        \u30c6\u30ad\u30b9\u30c8\u3068\u6bd4\u8f03\u3057\u3066\u3001\u52d5\u753b\u3084\u753b\u50cf\u306f\u60c5\u5831\u5bc6\u5ea6\u304c\u9ad8\u304f\u3001\u3088\u308a\u69cb\u9020\u5316\u3055\u308c\u3066\u304a\u308a\u3001\u7406\u89e3\u3057\u3084\u3059\u304f\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u672c\u3067\u306f\u3001\u91cd\u8981\u3067\u96e3\u3057\u3044\u6982\u5ff5\u306f\u4e3b\u306b\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3068\u56f3\u89e3\u3092\u901a\u3058\u3066\u63d0\u793a\u3055\u308c\u3001\u30c6\u30ad\u30b9\u30c8\u306f\u8aac\u660e\u3068\u88dc\u8db3\u3068\u3057\u3066\u6a5f\u80fd\u3057\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306a\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3084\u56f3\u89e3\u306e\u3042\u308b\u5185\u5bb9\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u56f3\u306e\u7406\u89e3\u3092\u512a\u5148\u3057\u3001\u30c6\u30ad\u30b9\u30c8\u3092\u88dc\u8db3\u3068\u3057\u3066\u3001\u4e21\u65b9\u3092\u7d71\u5408\u3057\u3066\u5305\u62ec\u7684\u306a\u7406\u89e3\u3092\u5f97\u3066\u304f\u3060\u3055\u3044\u3002

        \u56f3 0-2 \u00a0 \u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u56f3\u89e3\u306e\u4f8b

        "},{"location":"chapter_preface/suggestions/#023","title":"0.2.3 \u00a0 \u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u5b9f\u8df5\u306b\u3088\u308b\u7406\u89e3\u306e\u6df1\u5316","text":"

        \u3053\u306e\u672c\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306fGitHub\u30ea\u30dd\u30b8\u30c8\u30ea\u3067\u30db\u30b9\u30c8\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306b\u306f\u30c6\u30b9\u30c8\u4f8b\u304c\u4ed8\u5c5e\u3057\u3066\u304a\u308a\u3001\u30ef\u30f3\u30af\u30ea\u30c3\u30af\u3067\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002

        \u6642\u9593\u306b\u4f59\u88d5\u304c\u3042\u308b\u5834\u5408\u306f\u3001\u81ea\u5206\u3067\u30b3\u30fc\u30c9\u3092\u30bf\u30a4\u30d7\u3059\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002\u6642\u9593\u304c\u306a\u3044\u5834\u5408\u306f\u3001\u5c11\u306a\u304f\u3068\u3082\u3059\u3079\u3066\u306e\u30b3\u30fc\u30c9\u3092\u8aad\u3093\u3067\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u30b3\u30fc\u30c9\u3092\u8aad\u3080\u3060\u3051\u3068\u6bd4\u8f03\u3057\u3066\u3001\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3053\u3068\u306f\u591a\u304f\u306e\u5834\u5408\u3001\u3088\u308a\u591a\u304f\u306e\u5b66\u7fd2\u3092\u3082\u305f\u3089\u3057\u307e\u3059\u3002\u5b9f\u8df5\u306b\u3088\u308b\u5b66\u7fd2\u3053\u305d\u304c\u771f\u306e\u5b66\u7fd2\u65b9\u6cd5\u3067\u3059\u3002

        \u56f3 0-3 \u00a0 \u30b3\u30fc\u30c9\u5b9f\u884c\u4f8b

        \u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u305f\u3081\u306e\u8a2d\u5b9a\u306b\u306f\u3001\u4e3b\u306b3\u3064\u306e\u30b9\u30c6\u30c3\u30d7\u304c\u542b\u307e\u308c\u307e\u3059\u3002

        \u30b9\u30c6\u30c3\u30d71\uff1a\u30ed\u30fc\u30ab\u30eb\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u74b0\u5883\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3002\u4ed8\u9332\u306e\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u306b\u5f93\u3063\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u304b\u3001\u3059\u3067\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u306f\u3053\u306e\u30b9\u30c6\u30c3\u30d7\u3092\u30b9\u30ad\u30c3\u30d7\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u30b9\u30c6\u30c3\u30d72\uff1a\u30b3\u30fc\u30c9\u30ea\u30dd\u30b8\u30c8\u30ea\u3092\u30af\u30ed\u30fc\u30f3\u307e\u305f\u306f\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3002GitHub\u30ea\u30dd\u30b8\u30c8\u30ea\u3092\u8a2a\u554f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        Git\u304c\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u6b21\u306e\u30b3\u30de\u30f3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u30ea\u30dd\u30b8\u30c8\u30ea\u3092\u30af\u30ed\u30fc\u30f3\u3057\u307e\u3059\uff1a

        git clone https://github.com/krahets/hello-algo.git\n

        \u307e\u305f\u306f\u3001\u4e0b\u56f3\u306b\u793a\u3059\u5834\u6240\u306b\u3042\u308b\u300cDownload ZIP\u300d\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3057\u3066\u3001\u30b3\u30fc\u30c9\u3092\u5727\u7e2eZIP\u30d5\u30a1\u30a4\u30eb\u3068\u3057\u3066\u76f4\u63a5\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002\u305d\u306e\u5f8c\u3001\u30ed\u30fc\u30ab\u30eb\u3067\u5c55\u958b\u3059\u308b\u3060\u3051\u3067\u3059\u3002

        \u56f3 0-4 \u00a0 \u30ea\u30dd\u30b8\u30c8\u30ea\u306e\u30af\u30ed\u30fc\u30f3\u3068\u30b3\u30fc\u30c9\u306e\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9

        \u30b9\u30c6\u30c3\u30d73\uff1a\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u4e0a\u90e8\u306b\u30d5\u30a1\u30a4\u30eb\u540d\u304c\u8a18\u8f09\u3055\u308c\u305f\u30b3\u30fc\u30c9\u30d6\u30ed\u30c3\u30af\u306b\u3064\u3044\u3066\u306f\u3001\u30ea\u30dd\u30b8\u30c8\u30ea\u306ecodes\u30d5\u30a9\u30eb\u30c0\u3067\u5bfe\u5fdc\u3059\u308b\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u30d5\u30a1\u30a4\u30eb\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u30d5\u30a1\u30a4\u30eb\u306f\u30ef\u30f3\u30af\u30ea\u30c3\u30af\u3067\u5b9f\u884c\u3067\u304d\u3001\u4e0d\u8981\u306a\u30c7\u30d0\u30c3\u30b0\u6642\u9593\u3092\u7bc0\u7d04\u3057\u3001\u5b66\u7fd2\u306b\u96c6\u4e2d\u3067\u304d\u307e\u3059\u3002

        \u56f3 0-5 \u00a0 \u30b3\u30fc\u30c9\u30d6\u30ed\u30c3\u30af\u3068\u5bfe\u5fdc\u3059\u308b\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u30d5\u30a1\u30a4\u30eb

        "},{"location":"chapter_preface/suggestions/#024","title":"0.2.4 \u00a0 \u8b70\u8ad6\u306b\u3088\u308b\u5171\u540c\u5b66\u7fd2","text":"

        \u3053\u306e\u672c\u3092\u8aad\u3093\u3067\u3044\u308b\u9593\u3001\u5b66\u3079\u306a\u304b\u3063\u305f\u70b9\u3092\u98db\u3070\u3055\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002\u30b3\u30e1\u30f3\u30c8\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u6c17\u8efd\u306b\u8cea\u554f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u559c\u3093\u3067\u304a\u7b54\u3048\u3057\u3001\u901a\u5e382\u65e5\u4ee5\u5185\u306b\u56de\u7b54\u3067\u304d\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5404\u7ae0\u306e\u4e0b\u90e8\u306b\u30b3\u30e1\u30f3\u30c8\u30bb\u30af\u30b7\u30e7\u30f3\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u30b3\u30e1\u30f3\u30c8\u306b\u6ce8\u610f\u3092\u6255\u3046\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002\u4ed6\u306e\u4eba\u304c\u906d\u9047\u3057\u305f\u554f\u984c\u3092\u77e5\u308b\u3053\u3068\u3067\u3001\u77e5\u8b58\u306e\u30ae\u30e3\u30c3\u30d7\u3092\u7279\u5b9a\u3057\u3001\u3088\u308a\u6df1\u3044\u601d\u7d22\u3092\u4fc3\u3059\u3060\u3051\u3067\u306a\u304f\u3001\u4ef2\u9593\u306e\u8aad\u8005\u306e\u8cea\u554f\u306b\u7b54\u3048\u305f\u308a\u3001\u6d1e\u5bdf\u3092\u5171\u6709\u3057\u305f\u308a\u3001\u76f8\u4e92\u306e\u5411\u4e0a\u3092\u4fc3\u9032\u3057\u305f\u308a\u3059\u308b\u3053\u3068\u3067\u5bdb\u5927\u306b\u8ca2\u732e\u3059\u308b\u3053\u3068\u3082\u62db\u5f85\u3057\u307e\u3059\u3002

        \u56f3 0-6 \u00a0 \u30b3\u30e1\u30f3\u30c8\u30bb\u30af\u30b7\u30e7\u30f3\u306e\u4f8b

        "},{"location":"chapter_preface/suggestions/#025","title":"0.2.5 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u5b66\u7fd2\u30d1\u30b9","text":"

        \u5168\u4f53\u7684\u306b\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u30de\u30b9\u30bf\u30fc\u3059\u308b\u65c5\u306f3\u3064\u306e\u6bb5\u968e\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a

        1. \u6bb5\u968e1\uff1a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5165\u9580\u3002\u3055\u307e\u3056\u307e\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u7279\u6027\u3068\u4f7f\u7528\u6cd5\u306b\u6163\u308c\u3001\u7570\u306a\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u539f\u7406\u3001\u30d7\u30ed\u30bb\u30b9\u3001\u7528\u9014\u3001\u52b9\u7387\u306b\u3064\u3044\u3066\u5b66\u3076\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        2. \u6bb5\u968e2\uff1a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u306e\u7df4\u7fd2\u3002Sword for Offer\u3084LeetCode Hot 100\u306a\u3069\u306e\u4eba\u6c17\u306e\u3042\u308b\u554f\u984c\u304b\u3089\u59cb\u3081\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u3001\u5c11\u306a\u304f\u3068\u3082100\u554f\u3092\u84c4\u7a4d\u3057\u3066\u4e3b\u6d41\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u306b\u6163\u308c\u308b\u3053\u3068\u3067\u3059\u3002\u7df4\u7fd2\u3092\u59cb\u3081\u308b\u3068\u5fd8\u5374\u304c\u8ab2\u984c\u306b\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u3053\u308c\u306f\u6b63\u5e38\u306a\u3053\u3068\u3067\u3059\u306e\u3067\u3054\u5b89\u5fc3\u304f\u3060\u3055\u3044\u3002\u300c\u30a8\u30d3\u30f3\u30b0\u30cf\u30a6\u30b9\u306e\u5fd8\u5374\u66f2\u7dda\u300d\u306b\u5f93\u3063\u3066\u554f\u984c\u3092\u5fa9\u7fd2\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u901a\u5e383\u301c5\u56de\u306e\u53cd\u5fa9\u306e\u5f8c\u3001\u305d\u308c\u3089\u3092\u899a\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3067\u3057\u3087\u3046\u3002
        3. \u6bb5\u968e3\uff1a\u77e5\u8b58\u4f53\u7cfb\u306e\u69cb\u7bc9\u3002\u5b66\u7fd2\u306e\u9762\u3067\u306f\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30b3\u30e9\u30e0\u8a18\u4e8b\u3001\u89e3\u6cd5\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u6559\u79d1\u66f8\u3092\u8aad\u3093\u3067\u77e5\u8b58\u4f53\u7cfb\u3092\u7d99\u7d9a\u7684\u306b\u8c4a\u304b\u306b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u7df4\u7fd2\u306e\u9762\u3067\u306f\u3001\u30c8\u30d4\u30c3\u30af\u5225\u5206\u985e\u3001\u4e00\u3064\u306e\u554f\u984c\u306b\u5bfe\u3059\u308b\u8907\u6570\u306e\u89e3\u6cd5\u3001\u8907\u6570\u306e\u554f\u984c\u306b\u5bfe\u3059\u308b\u4e00\u3064\u306e\u89e3\u6cd5\u306a\u3069\u3001\u9ad8\u5ea6\u306a\u6226\u7565\u3092\u8a66\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u6226\u7565\u306b\u95a2\u3059\u308b\u6d1e\u5bdf\u306f\u3001\u3055\u307e\u3056\u307e\u306a\u30b3\u30df\u30e5\u30cb\u30c6\u30a3\u3067\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u3053\u306e\u672c\u306f\u4e3b\u306b\u300c\u6bb5\u968e1\u300d\u3092\u30ab\u30d0\u30fc\u3057\u3066\u304a\u308a\u3001\u6bb5\u968e2\u30683\u306b\u3088\u308a\u52b9\u7387\u7684\u306b\u53d6\u308a\u7d44\u3080\u306e\u306b\u5f79\u7acb\u3064\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 0-7 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u5b66\u7fd2\u30d1\u30b9

        "},{"location":"chapter_preface/summary/","title":"0.3 \u00a0 \u307e\u3068\u3081","text":"
        • \u3053\u306e\u672c\u306e\u4e3b\u306a\u8aad\u8005\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u521d\u5fc3\u8005\u3067\u3059\u3002\u3059\u3067\u306b\u57fa\u672c\u7684\u306a\u77e5\u8b58\u3092\u304a\u6301\u3061\u306e\u5834\u5408\u3001\u3053\u306e\u672c\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u77e5\u8b58\u3092\u4f53\u7cfb\u7684\u306b\u5fa9\u7fd2\u3059\u308b\u306e\u306b\u5f79\u7acb\u3061\u3001\u3053\u306e\u672c\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306f\u300c\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u30c4\u30fc\u30eb\u30ad\u30c3\u30c8\u300d\u3068\u3057\u3066\u3082\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002
        • \u3053\u306e\u672c\u306f3\u3064\u306e\u4e3b\u8981\u306a\u30bb\u30af\u30b7\u30e7\u30f3\u3001\u8a08\u7b97\u91cf\u89e3\u6790\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u69cb\u6210\u3055\u308c\u3066\u304a\u308a\u3001\u3053\u306e\u5206\u91ce\u306e\u307b\u3068\u3093\u3069\u306e\u30c8\u30d4\u30c3\u30af\u3092\u30ab\u30d0\u30fc\u3057\u3066\u3044\u307e\u3059\u3002
        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u521d\u5fc3\u8005\u306b\u3068\u3063\u3066\u3001\u591a\u304f\u306e\u56de\u308a\u9053\u3084\u4e00\u822c\u7684\u306a\u843d\u3068\u3057\u7a74\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u3001\u521d\u671f\u6bb5\u968e\u3067\u5165\u9580\u66f8\u3092\u8aad\u3080\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002
        • \u672c\u66f8\u5185\u306e\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3068\u56f3\u306f\u901a\u5e38\u3001\u91cd\u8981\u306a\u30dd\u30a4\u30f3\u30c8\u3068\u96e3\u3057\u3044\u77e5\u8b58\u3092\u7d39\u4ecb\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u672c\u3092\u8aad\u3080\u969b\u306b\u306f\u3053\u308c\u3089\u306b\u3088\u308a\u591a\u304f\u306e\u6ce8\u610f\u3092\u6255\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u5b9f\u8df5\u306f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3092\u5b66\u3076\u6700\u826f\u306e\u65b9\u6cd5\u3067\u3059\u3002\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3057\u3001\u81ea\u5206\u3067\u30b3\u30fc\u30c9\u3092\u30bf\u30a4\u30d7\u3059\u308b\u3053\u3068\u3092\u5f37\u304f\u304a\u52e7\u3081\u3057\u307e\u3059\u3002
        • \u3053\u306e\u672c\u306eWeb\u7248\u306e\u5404\u7ae0\u306b\u306f\u8b70\u8ad6\u30bb\u30af\u30b7\u30e7\u30f3\u304c\u3042\u308a\u3001\u3044\u3064\u3067\u3082\u8cea\u554f\u3084\u6d1e\u5bdf\u3092\u5171\u6709\u3059\u308b\u3053\u3068\u3092\u6b53\u8fce\u3057\u307e\u3059\u3002
        "},{"location":"chapter_reference/","title":"\u53c2\u8003\u6587\u732e","text":"

        [1] Thomas H. Cormen, et al. Introduction to Algorithms (3rd Edition).

        [2] Aditya Bhargava. Grokking Algorithms: An Illustrated Guide for Programmers and Other Curious People (1st Edition).

        [3] Robert Sedgewick, et al. Algorithms (4th Edition).

        [4] Yan Weimin. Data Structures (C Language Version).

        [5] Deng Junhui. Data Structures (C++ Language Version, Third Edition).

        [6] Mark Allen Weiss, translated by Chen Yue. Data Structures and Algorithm Analysis in Java (Third Edition).

        [7] Cheng Jie. Speaking of Data Structures.

        [8] Wang Zheng. The Beauty of Data Structures and Algorithms.

        [9] Gayle Laakmann McDowell. Cracking the Coding Interview: 189 Programming Questions and Solutions (6th Edition).

        [10] Aston Zhang, et al. Dive into Deep Learning.

        "},{"location":"chapter_searching/","title":"\u7b2c 10 \u7ae0 \u00a0 \u63a2\u7d22","text":"

        Abstract

        \u63a2\u7d22\u306f\u672a\u77e5\u3078\u306e\u5192\u967a\u3067\u3059\u3002\u795e\u79d8\u7684\u306a\u7a7a\u9593\u306e\u9685\u3005\u307e\u3067\u5de1\u308b\u5fc5\u8981\u304c\u3042\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3057\u3001\u3042\u308b\u3044\u306f\u3059\u3050\u306b\u76ee\u6a19\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002

        \u3053\u306e\u767a\u898b\u306e\u65c5\u306b\u304a\u3044\u3066\u3001\u305d\u308c\u305e\u308c\u306e\u63a2\u67fb\u306f\u4e88\u671f\u3057\u306a\u3044\u7b54\u3048\u3067\u7d42\u308f\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002

        "},{"location":"chapter_searching/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 10.1 \u00a0 \u4e8c\u5206\u63a2\u7d22
        • 10.2 \u00a0 \u4e8c\u5206\u63a2\u7d22\u633f\u5165\u70b9
        • 10.3 \u00a0 \u4e8c\u5206\u63a2\u7d22\u306e\u5883\u754c
        • 10.4 \u00a0 \u30cf\u30c3\u30b7\u30e5\u6700\u9069\u5316\u6226\u7565
        • 10.5 \u00a0 \u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u518d\u8003
        • 10.6 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_searching/binary_search/","title":"10.1 \u00a0 \u4e8c\u5206\u63a2\u7d22","text":"

        \u4e8c\u5206\u63a2\u7d22\u306f\u5206\u5272\u7d71\u6cbb\u6226\u7565\u3092\u7528\u3044\u308b\u52b9\u7387\u7684\u306a\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3059\u3002\u914d\u5217\u5185\u306e\u8981\u7d20\u306e\u6574\u5217\u9806\u5e8f\u3092\u5229\u7528\u3057\u3001\u5404\u53cd\u5fa9\u3067\u63a2\u7d22\u533a\u9593\u3092\u534a\u5206\u306b\u6e1b\u3089\u3057\u306a\u304c\u3089\u3001\u76ee\u6a19\u8981\u7d20\u304c\u898b\u3064\u304b\u308b\u304b\u63a2\u7d22\u533a\u9593\u304c\u7a7a\u306b\u306a\u308b\u307e\u3067\u7d9a\u884c\u3057\u307e\u3059\u3002

        Question

        \u9577\u3055\\(n\\)\u306e\u914d\u5217nums\u304c\u4e0e\u3048\u3089\u308c\u3001\u8981\u7d20\u306f\u91cd\u8907\u306a\u3057\u3067\u6607\u9806\u306b\u914d\u5217\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u914d\u5217\u5185\u306e\u8981\u7d20target\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u898b\u3064\u3051\u3066\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u914d\u5217\u306b\u8981\u7d20\u304c\u542b\u307e\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u306f\\(-1\\)\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4f8b\u3092\u4e0b\u56f3\u306b\u793a\u3057\u307e\u3059\u3002

        \u56f3 10-1 \u00a0 Binary search example data

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u307e\u305a\\(i = 0\\)\u3068\\(j = n - 1\\)\u3067\u30dd\u30a4\u30f3\u30bf\u3092\u521d\u671f\u5316\u3057\u3001\u305d\u308c\u305e\u308c\u914d\u5217\u306e\u6700\u521d\u3068\u6700\u5f8c\u306e\u8981\u7d20\u3092\u6307\u3057\u307e\u3059\u3002\u3053\u308c\u3089\u306f\u307e\u305f\u5168\u4f53\u306e\u63a2\u7d22\u533a\u9593\\([0, n - 1]\\)\u3092\u8868\u3057\u307e\u3059\u3002\u89d2\u62ec\u5f27\u306f\u9589\u533a\u9593\u3092\u793a\u3057\u3001\u5883\u754c\u5024\u81ea\u8eab\u3082\u542b\u3080\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u305d\u3057\u3066\u3001\u4ee5\u4e0b\u306e2\u3064\u306e\u30b9\u30c6\u30c3\u30d7\u3092\u30eb\u30fc\u30d7\u3067\u5b9f\u884c\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        1. \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(m = \\lfloor {(i + j) / 2} \\rfloor\\)\u3092\u8a08\u7b97\u3057\u307e\u3059\u3002\u3053\u3053\u3067\\(\\lfloor \\: \\rfloor\\)\u306f\u5e8a\u95a2\u6570\u3092\u8868\u3057\u307e\u3059\u3002
        2. nums[m]\u3068target\u306e\u6bd4\u8f03\u306b\u57fa\u3065\u3044\u3066\u3001\u4ee5\u4e0b\u306e3\u3064\u306e\u30b1\u30fc\u30b9\u306e\u3046\u30611\u3064\u3092\u9078\u629e\u3057\u3066\u5b9f\u884c\u3057\u307e\u3059\u3002
          1. nums[m] < target\u306e\u5834\u5408\u3001target\u306f\u533a\u9593\\([m + 1, j]\\)\u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\u305f\u3081\u3001\\(i = m + 1\\)\u3068\u3057\u307e\u3059\u3002
          2. nums[m] > target\u306e\u5834\u5408\u3001target\u306f\u533a\u9593\\([i, m - 1]\\)\u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\u305f\u3081\u3001\\(j = m - 1\\)\u3068\u3057\u307e\u3059\u3002
          3. nums[m] = target\u306e\u5834\u5408\u3001target\u304c\u898b\u3064\u304b\u3063\u305f\u3053\u3068\u3092\u793a\u3059\u305f\u3081\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(m\\)\u3092\u8fd4\u3057\u307e\u3059\u3002

        \u914d\u5217\u306b\u76ee\u6a19\u8981\u7d20\u304c\u542b\u307e\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u3001\u63a2\u7d22\u533a\u9593\u306f\u6700\u7d42\u7684\u306b\u7a7a\u306b\u306a\u308a\u3001\\(-1\\)\u3092\u8fd4\u3057\u3066\u7d42\u4e86\u3057\u307e\u3059\u3002

        <1><2><3><4><5><6><7>

        \u56f3 10-2 \u00a0 Binary search process

        \\(i\\)\u3068\\(j\\)\u304c\u4e21\u65b9\u3068\u3082int\u578b\u3067\u3042\u308b\u305f\u3081\u3001**\\(i + j\\)\u306fint\u578b\u306e\u7bc4\u56f2\u3092\u8d85\u3048\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b**\u3053\u3068\u306f\u6ce8\u76ee\u306b\u5024\u3057\u307e\u3059\u3002\u5927\u304d\u306a\u6570\u306e\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u3092\u907f\u3051\u308b\u305f\u3081\u3001\u901a\u5e38\u306f\u5f0f\\(m = \\lfloor {i + (j - i) / 2} \\rfloor\\)\u3092\u4f7f\u7528\u3057\u3066\u4e2d\u70b9\u3092\u8a08\u7b97\u3057\u307e\u3059\u3002

        \u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search.py
        def binary_search(nums: list[int], target: int) -> int:\n    \"\"\"\u4e8c\u5206\u63a2\u7d22\uff08\u4e21\u7aef\u9589\u533a\u9593\uff09\"\"\"\n    # \u4e21\u7aef\u9589\u533a\u9593 [0, n-1] \u3092\u521d\u671f\u5316\u3001\u3059\u306a\u308f\u3061 i, j \u306f\u305d\u308c\u305e\u308c\u914d\u5217\u306e\u6700\u521d\u306e\u8981\u7d20\u3068\u6700\u5f8c\u306e\u8981\u7d20\u3092\u6307\u3059\n    i, j = 0, len(nums) - 1\n    # \u691c\u7d22\u533a\u9593\u304c\u7a7a\u306b\u306a\u308b\u307e\u3067\u30eb\u30fc\u30d7\uff08i > j \u306e\u3068\u304d\u7a7a\uff09\n    while i <= j:\n        # \u7406\u8ad6\u7684\u306b\u306f\u3001Python\u306e\u6570\u5024\u306f\u7121\u9650\u306b\u5927\u304d\u304f\u306a\u308b\u3053\u3068\u304c\u3067\u304d\u308b\uff08\u30e1\u30e2\u30ea\u30b5\u30a4\u30ba\u306b\u4f9d\u5b58\uff09\u305f\u3081\u3001\u5927\u304d\u306a\u6570\u306e\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u3092\u8003\u616e\u3059\u308b\u5fc5\u8981\u306f\u306a\u3044\n        m = i + (j - i) // 2  # \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 m \u3092\u8a08\u7b97\n        if nums[m] < target:\n            i = m + 1  # \u3053\u306e\u5834\u5408\u3001target \u306f\u533a\u9593 [m+1, j] \u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n        elif nums[m] > target:\n            j = m - 1  # \u3053\u306e\u5834\u5408\u3001target \u306f\u533a\u9593 [i, m-1] \u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n        else:\n            return m  # \u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u304c\u898b\u3064\u304b\u3063\u305f\u305f\u3081\u3001\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n    return -1  # \u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u305f\u3081\u3001-1 \u3092\u8fd4\u3059\n
        binary_search.cpp
        /* \u4e8c\u5206\u63a2\u7d22\uff08\u4e21\u7aef\u9589\u533a\u9593\uff09 */\nint binarySearch(vector<int> &nums, int target) {\n    // \u4e21\u7aef\u9589\u533a\u9593[0, n-1]\u3092\u521d\u671f\u5316\u3001\u3059\u306a\u308f\u3061i\u3001j\u306f\u305d\u308c\u305e\u308c\u914d\u5217\u306e\u6700\u521d\u306e\u8981\u7d20\u3068\u6700\u5f8c\u306e\u8981\u7d20\u3092\u6307\u3059\n    int i = 0, j = nums.size() - 1;\n    // \u63a2\u7d22\u533a\u9593\u304c\u7a7a\u306b\u306a\u308b\u307e\u3067\u30eb\u30fc\u30d7\uff08i > j\u306e\u6642\u7a7a\u306b\u306a\u308b\uff09\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9m\u3092\u8a08\u7b97\n        if (nums[m] < target)    // \u3053\u306e\u72b6\u6cc1\u306ftarget\u304c\u533a\u9593[m+1, j]\u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n            i = m + 1;\n        else if (nums[m] > target) // \u3053\u306e\u72b6\u6cc1\u306ftarget\u304c\u533a\u9593[i, m-1]\u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n            j = m - 1;\n        else // \u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u304c\u898b\u3064\u304b\u3063\u305f\u305f\u3081\u3001\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n            return m;\n    }\n    // \u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u305f\u3081\u3001-1\u3092\u8fd4\u3059\n    return -1;\n}\n
        binary_search.java
        /* \u4e8c\u5206\u63a2\u7d22\uff08\u4e21\u7aef\u9589\u533a\u9593\uff09 */\nint binarySearch(int[] nums, int target) {\n    // \u4e21\u7aef\u9589\u533a\u9593 [0, n-1] \u3092\u521d\u671f\u5316\u3001\u3059\u306a\u308f\u3061 i, j \u306f\u305d\u308c\u305e\u308c\u914d\u5217\u306e\u6700\u521d\u306e\u8981\u7d20\u3068\u6700\u5f8c\u306e\u8981\u7d20\u3092\u6307\u3059\n    int i = 0, j = nums.length - 1;\n    // \u63a2\u7d22\u533a\u9593\u304c\u7a7a\u306b\u306a\u308b\u307e\u3067\u30eb\u30fc\u30d7\uff08i > j \u306e\u3068\u304d\u7a7a\uff09\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 m \u3092\u8a08\u7b97\n        if (nums[m] < target) // \u3053\u306e\u72b6\u6cc1\u306f target \u304c\u533a\u9593 [m+1, j] \u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n            i = m + 1;\n        else if (nums[m] > target) // \u3053\u306e\u72b6\u6cc1\u306f target \u304c\u533a\u9593 [i, m-1] \u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n            j = m - 1;\n        else // \u76ee\u6a19\u8981\u7d20\u3092\u898b\u3064\u3051\u305f\u306e\u3067\u3001\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n            return m;\n    }\n    // \u76ee\u6a19\u8981\u7d20\u3092\u898b\u3064\u3051\u3089\u308c\u306a\u304b\u3063\u305f\u306e\u3067\u3001-1 \u3092\u8fd4\u3059\n    return -1;\n}\n
        binary_search.cs
        [class]{binary_search}-[func]{BinarySearch}\n
        binary_search.go
        [class]{}-[func]{binarySearch}\n
        binary_search.swift
        [class]{}-[func]{binarySearch}\n
        binary_search.js
        [class]{}-[func]{binarySearch}\n
        binary_search.ts
        [class]{}-[func]{binarySearch}\n
        binary_search.dart
        [class]{}-[func]{binarySearch}\n
        binary_search.rs
        [class]{}-[func]{binary_search}\n
        binary_search.c
        [class]{}-[func]{binarySearch}\n
        binary_search.kt
        [class]{}-[func]{binarySearch}\n
        binary_search.rb
        [class]{}-[func]{binary_search}\n

        \u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(\\log n)\\)\u3067\u3059\uff1a\u4e8c\u5206\u30eb\u30fc\u30d7\u306b\u304a\u3044\u3066\u3001\u533a\u9593\u306f\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u534a\u5206\u306b\u6e1b\u5c11\u3059\u308b\u305f\u3081\u3001\u53cd\u5fa9\u56de\u6570\u306f\\(\\log_2 n\\)\u3068\u306a\u308a\u307e\u3059\u3002

        \u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u3067\u3059\uff1a\u30dd\u30a4\u30f3\u30bf\\(i\\)\u3068\\(j\\)\u306f\u5b9a\u6570\u30b5\u30a4\u30ba\u306e\u7a7a\u9593\u3092\u5360\u6709\u3057\u307e\u3059\u3002

        "},{"location":"chapter_searching/binary_search/#1011","title":"10.1.1 \u00a0 \u533a\u9593\u8868\u73fe\u65b9\u6cd5","text":"

        \u4e0a\u8a18\u306e\u9589\u533a\u9593\u306e\u4ed6\u306b\u3001\u3082\u3046\u4e00\u3064\u306e\u4e00\u822c\u7684\u306a\u533a\u9593\u8868\u73fe\u306f\u300c\u5de6\u9589\u53f3\u958b\u300d\u533a\u9593\u3067\u3001\\([0, n)\\)\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u3001\u5de6\u5883\u754c\u306f\u81ea\u8eab\u3092\u542b\u307f\u3001\u53f3\u5883\u754c\u306f\u542b\u307f\u307e\u305b\u3093\u3002\u3053\u306e\u8868\u73fe\u3067\u306f\u3001\\(i = j\\)\u306e\u3068\u304d\u533a\u9593\\([i, j)\\)\u306f\u7a7a\u306b\u306a\u308a\u307e\u3059\u3002

        \u3053\u306e\u8868\u73fe\u306b\u57fa\u3065\u3044\u3066\u540c\u3058\u6a5f\u80fd\u3092\u6301\u3064\u4e8c\u5206\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search.py
        def binary_search_lcro(nums: list[int], target: int) -> int:\n    \"\"\"\u4e8c\u5206\u63a2\u7d22\uff08\u5de6\u9589\u53f3\u958b\u533a\u9593\uff09\"\"\"\n    # \u5de6\u9589\u53f3\u958b\u533a\u9593 [0, n) \u3092\u521d\u671f\u5316\u3001\u3059\u306a\u308f\u3061 i, j \u306f\u305d\u308c\u305e\u308c\u914d\u5217\u306e\u6700\u521d\u306e\u8981\u7d20\u3068\u6700\u5f8c\u306e\u8981\u7d20+1\u3092\u6307\u3059\n    i, j = 0, len(nums)\n    # \u691c\u7d22\u533a\u9593\u304c\u7a7a\u306b\u306a\u308b\u307e\u3067\u30eb\u30fc\u30d7\uff08i = j \u306e\u3068\u304d\u7a7a\uff09\n    while i < j:\n        m = i + (j - i) // 2  # \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 m \u3092\u8a08\u7b97\n        if nums[m] < target:\n            i = m + 1  # \u3053\u306e\u5834\u5408\u3001target \u306f\u533a\u9593 [m+1, j) \u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n        elif nums[m] > target:\n            j = m  # \u3053\u306e\u5834\u5408\u3001target \u306f\u533a\u9593 [i, m) \u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n        else:\n            return m  # \u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u304c\u898b\u3064\u304b\u3063\u305f\u305f\u3081\u3001\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n    return -1  # \u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u305f\u3081\u3001-1 \u3092\u8fd4\u3059\n
        binary_search.cpp
        /* \u4e8c\u5206\u63a2\u7d22\uff08\u5de6\u9589\u53f3\u958b\u533a\u9593\uff09 */\nint binarySearchLCRO(vector<int> &nums, int target) {\n    // \u5de6\u9589\u53f3\u958b\u533a\u9593[0, n)\u3092\u521d\u671f\u5316\u3001\u3059\u306a\u308f\u3061i\u3001j\u306f\u305d\u308c\u305e\u308c\u914d\u5217\u306e\u6700\u521d\u306e\u8981\u7d20\u3068\u6700\u5f8c\u306e\u8981\u7d20+1\u3092\u6307\u3059\n    int i = 0, j = nums.size();\n    // \u63a2\u7d22\u533a\u9593\u304c\u7a7a\u306b\u306a\u308b\u307e\u3067\u30eb\u30fc\u30d7\uff08i = j\u306e\u6642\u7a7a\u306b\u306a\u308b\uff09\n    while (i < j) {\n        int m = i + (j - i) / 2; // \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9m\u3092\u8a08\u7b97\n        if (nums[m] < target)    // \u3053\u306e\u72b6\u6cc1\u306ftarget\u304c\u533a\u9593[m+1, j)\u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n            i = m + 1;\n        else if (nums[m] > target) // \u3053\u306e\u72b6\u6cc1\u306ftarget\u304c\u533a\u9593[i, m)\u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n            j = m;\n        else // \u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u304c\u898b\u3064\u304b\u3063\u305f\u305f\u3081\u3001\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n            return m;\n    }\n    // \u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u305f\u3081\u3001-1\u3092\u8fd4\u3059\n    return -1;\n}\n
        binary_search.java
        /* \u4e8c\u5206\u63a2\u7d22\uff08\u5de6\u9589\u53f3\u958b\u533a\u9593\uff09 */\nint binarySearchLCRO(int[] nums, int target) {\n    // \u5de6\u9589\u53f3\u958b\u533a\u9593 [0, n) \u3092\u521d\u671f\u5316\u3001\u3059\u306a\u308f\u3061 i, j \u306f\u305d\u308c\u305e\u308c\u914d\u5217\u306e\u6700\u521d\u306e\u8981\u7d20\u3068\u6700\u5f8c\u306e\u8981\u7d20+1\u3092\u6307\u3059\n    int i = 0, j = nums.length;\n    // \u63a2\u7d22\u533a\u9593\u304c\u7a7a\u306b\u306a\u308b\u307e\u3067\u30eb\u30fc\u30d7\uff08i = j \u306e\u3068\u304d\u7a7a\uff09\n    while (i < j) {\n        int m = i + (j - i) / 2; // \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 m \u3092\u8a08\u7b97\n        if (nums[m] < target) // \u3053\u306e\u72b6\u6cc1\u306f target \u304c\u533a\u9593 [m+1, j) \u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n            i = m + 1;\n        else if (nums[m] > target) // \u3053\u306e\u72b6\u6cc1\u306f target \u304c\u533a\u9593 [i, m) \u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n            j = m;\n        else // \u76ee\u6a19\u8981\u7d20\u3092\u898b\u3064\u3051\u305f\u306e\u3067\u3001\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n            return m;\n    }\n    // \u76ee\u6a19\u8981\u7d20\u3092\u898b\u3064\u3051\u3089\u308c\u306a\u304b\u3063\u305f\u306e\u3067\u3001-1 \u3092\u8fd4\u3059\n    return -1;\n}\n
        binary_search.cs
        [class]{binary_search}-[func]{BinarySearchLCRO}\n
        binary_search.go
        [class]{}-[func]{binarySearchLCRO}\n
        binary_search.swift
        [class]{}-[func]{binarySearchLCRO}\n
        binary_search.js
        [class]{}-[func]{binarySearchLCRO}\n
        binary_search.ts
        [class]{}-[func]{binarySearchLCRO}\n
        binary_search.dart
        [class]{}-[func]{binarySearchLCRO}\n
        binary_search.rs
        [class]{}-[func]{binary_search_lcro}\n
        binary_search.c
        [class]{}-[func]{binarySearchLCRO}\n
        binary_search.kt
        [class]{}-[func]{binarySearchLCRO}\n
        binary_search.rb
        [class]{}-[func]{binary_search_lcro}\n

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u30012\u3064\u306e\u533a\u9593\u8868\u73fe\u30bf\u30a4\u30d7\u306b\u304a\u3044\u3066\u3001\u4e8c\u5206\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u521d\u671f\u5316\u3001\u30eb\u30fc\u30d7\u6761\u4ef6\u3001\u533a\u9593\u7e2e\u5c0f\u64cd\u4f5c\u304c\u7570\u306a\u308a\u307e\u3059\u3002

        \u300c\u9589\u533a\u9593\u300d\u8868\u73fe\u3067\u306f\u4e21\u65b9\u306e\u5883\u754c\u304c\u5305\u542b\u7684\u3067\u3042\u308b\u305f\u3081\u3001\u30dd\u30a4\u30f3\u30bf\\(i\\)\u3068\\(j\\)\u306b\u3088\u308b\u533a\u9593\u7e2e\u5c0f\u64cd\u4f5c\u3082\u5bfe\u79f0\u7684\u3067\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u306b\u304f\u304f\u306a\u308b\u305f\u3081\u3001\u4e00\u822c\u7684\u306b\u300c\u9589\u533a\u9593\u300d\u30a2\u30d7\u30ed\u30fc\u30c1\u306e\u4f7f\u7528\u304c\u63a8\u5968\u3055\u308c\u307e\u3059\u3002

        \u56f3 10-3 \u00a0 Two types of interval definitions

        "},{"location":"chapter_searching/binary_search/#1012","title":"10.1.2 \u00a0 \u5229\u70b9\u3068\u5236\u9650","text":"

        \u4e8c\u5206\u63a2\u7d22\u306f\u6642\u9593\u3068\u7a7a\u9593\u306e\u4e21\u65b9\u306e\u9762\u3067\u826f\u597d\u306a\u6027\u80fd\u3092\u793a\u3057\u307e\u3059\u3002

        • \u4e8c\u5206\u63a2\u7d22\u306f\u6642\u9593\u52b9\u7387\u304c\u826f\u3044\u3067\u3059\u3002\u5927\u304d\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3067\u306f\u3001\u5bfe\u6570\u6642\u9593\u8a08\u7b97\u91cf\u304c\u5927\u304d\u306a\u5229\u70b9\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u30b5\u30a4\u30ba\\(n = 2^{20}\\)\u306e\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u7dda\u5f62\u63a2\u7d22\u306f\\(2^{20} = 1048576\\)\u56de\u306e\u53cd\u5fa9\u304c\u5fc5\u8981\u3067\u3059\u304c\u3001\u4e8c\u5206\u63a2\u7d22\u306f\\(\\log_2 2^{20} = 20\\)\u56de\u306e\u30eb\u30fc\u30d7\u306e\u307f\u3067\u6e08\u307f\u307e\u3059\u3002
        • \u4e8c\u5206\u63a2\u7d22\u306b\u306f\u8ffd\u52a0\u306e\u7a7a\u9593\u304c\u5fc5\u8981\u3042\u308a\u307e\u305b\u3093\u3002\u8ffd\u52a0\u306e\u7a7a\u9593\u306b\u4f9d\u5b58\u3059\u308b\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff08\u30cf\u30c3\u30b7\u30e5\u63a2\u7d22\u306a\u3069\uff09\u3068\u6bd4\u8f03\u3057\u3066\u3001\u4e8c\u5206\u63a2\u7d22\u306f\u3088\u308a\u7a7a\u9593\u52b9\u7387\u7684\u3067\u3059\u3002

        \u3057\u304b\u3057\u3001\u4e8c\u5206\u63a2\u7d22\u306f\u4ee5\u4e0b\u306e\u61f8\u5ff5\u306b\u3088\u308a\u3001\u3059\u3079\u3066\u306e\u30b7\u30ca\u30ea\u30aa\u306b\u9069\u3057\u3066\u3044\u308b\u3068\u306f\u9650\u308a\u307e\u305b\u3093\u3002

        • \u4e8c\u5206\u63a2\u7d22\u306f\u30bd\u30fc\u30c8\u6e08\u307f\u30c7\u30fc\u30bf\u306b\u306e\u307f\u9069\u7528\u3067\u304d\u307e\u3059\u3002\u672a\u30bd\u30fc\u30c8\u306e\u30c7\u30fc\u30bf\u306f\u4e8c\u5206\u63a2\u7d22\u3092\u9069\u7528\u3059\u308b\u524d\u306b\u30bd\u30fc\u30c8\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u901a\u5e38\\(O(n \\log n)\\)\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3092\u6301\u3064\u305f\u3081\u3001\u3053\u308c\u306f\u4fa1\u5024\u304c\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u3053\u306e\u30b3\u30b9\u30c8\u306f\u7dda\u5f62\u63a2\u7d22\u3088\u308a\u3082\u9ad8\u304f\u3001\u4e8c\u5206\u63a2\u7d22\u81ea\u4f53\u306f\u8a00\u3046\u307e\u3067\u3082\u3042\u308a\u307e\u305b\u3093\u3002\u983b\u7e41\u306a\u633f\u5165\u304c\u3042\u308b\u30b7\u30ca\u30ea\u30aa\u3067\u306f\u3001\u914d\u5217\u3092\u9806\u5e8f\u306b\u4fdd\u3064\u30b3\u30b9\u30c8\u306f\u975e\u5e38\u306b\u9ad8\u304f\u3001\u7279\u5b9a\u306e\u4f4d\u7f6e\u306b\u65b0\u3057\u3044\u8981\u7d20\u3092\u633f\u5165\u3059\u308b\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3059\u3002
        • \u4e8c\u5206\u63a2\u7d22\u306f\u914d\u5217\u306e\u307f\u3092\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\u4e8c\u5206\u63a2\u7d22\u306b\u306f\u975e\u9023\u7d9a\uff08\u30b8\u30e3\u30f3\u30d7\uff09\u8981\u7d20\u30a2\u30af\u30bb\u30b9\u304c\u5fc5\u8981\u3067\u3001\u3053\u308c\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u306f\u975e\u52b9\u7387\u7684\u3067\u3059\u3002\u305d\u306e\u305f\u3081\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3084\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u3053\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u9069\u3057\u3066\u3044\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        • \u7dda\u5f62\u63a2\u7d22\u306f\u5c0f\u3055\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3067\u3088\u308a\u826f\u3044\u6027\u80fd\u3092\u793a\u3057\u307e\u3059\u3002\u7dda\u5f62\u63a2\u7d22\u3067\u306f\u5404\u53cd\u5fa9\u30671\u3064\u306e\u5224\u5b9a\u64cd\u4f5c\u306e\u307f\u304c\u5fc5\u8981\u3067\u3059\u304c\u3001\u4e8c\u5206\u63a2\u7d22\u3067\u306f1\u3064\u306e\u52a0\u7b97\u30011\u3064\u306e\u9664\u7b97\u30011\u3064\u304b\u30893\u3064\u306e\u5224\u5b9a\u64cd\u4f5c\u30011\u3064\u306e\u52a0\u7b97\uff08\u6e1b\u7b97\uff09\u3092\u542b\u307f\u3001\u5408\u8a084\u3064\u304b\u30896\u3064\u306e\u64cd\u4f5c\u304c\u5fc5\u8981\u3067\u3059\u3002\u305d\u306e\u305f\u3081\u3001\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\\(n\\)\u304c\u5c0f\u3055\u3044\u5834\u5408\u3001\u7dda\u5f62\u63a2\u7d22\u306f\u4e8c\u5206\u63a2\u7d22\u3088\u308a\u3082\u9ad8\u901f\u3067\u3059\u3002
        "},{"location":"chapter_searching/binary_search_edge/","title":"10.3 \u00a0 \u4e8c\u5206\u63a2\u7d22\u306e\u5883\u754c","text":""},{"location":"chapter_searching/binary_search_edge/#1031","title":"10.3.1 \u00a0 \u5de6\u5883\u754c\u3092\u898b\u3064\u3051\u308b","text":"

        Question

        \u91cd\u8907\u8981\u7d20\u3092\u542b\u3080\u53ef\u80fd\u6027\u304c\u3042\u308b\u9577\u3055\\(n\\)\u306e\u30bd\u30fc\u30c8\u6e08\u307f\u914d\u5217nums\u304c\u4e0e\u3048\u3089\u308c\u3001\u6700\u3082\u5de6\u306e\u8981\u7d20target\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u8981\u7d20\u304c\u914d\u5217\u306b\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u306f\u3001\\(-1\\)\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u633f\u5165\u4f4d\u7f6e\u306e\u4e8c\u5206\u63a2\u7d22\u65b9\u6cd5\u3092\u601d\u3044\u51fa\u3059\u3068\u3001\u63a2\u7d22\u5b8c\u4e86\u5f8c\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(i\\)\u306ftarget\u306e\u6700\u3082\u5de6\u306e\u51fa\u73fe\u3092\u6307\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u633f\u5165\u4f4d\u7f6e\u306e\u63a2\u7d22\u306f\u672c\u8cea\u7684\u306b\u6700\u3082\u5de6\u306etarget\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3068\u540c\u3058\u3067\u3059\u3002

        \u633f\u5165\u4f4d\u7f6e\u3092\u898b\u3064\u3051\u308b\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066target\u306e\u5de6\u5883\u754c\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u914d\u5217\u306btarget\u304c\u542b\u307e\u308c\u3066\u3044\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3053\u308c\u306f\u4ee5\u4e0b\u306e2\u3064\u306e\u7d50\u679c\u306b\u3064\u306a\u304c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u633f\u5165\u4f4d\u7f6e\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(i\\)\u304c\u7bc4\u56f2\u5916\u3067\u3059\u3002
        • \u8981\u7d20nums[i]\u304ctarget\u3068\u7b49\u3057\u304f\u3042\u308a\u307e\u305b\u3093\u3002

        \u3053\u308c\u3089\u306e\u5834\u5408\u3001\u5358\u306b\\(-1\\)\u3092\u8fd4\u3057\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_edge.py
        def binary_search_left_edge(nums: list[int], target: int) -> int:\n    \"\"\"\u6700\u5de6\u7aef\u306e\u30bf\u30fc\u30b2\u30c3\u30c8\u306e\u4e8c\u5206\u63a2\u7d22\"\"\"\n    # \u30bf\u30fc\u30b2\u30c3\u30c8\u306e\u633f\u5165\u4f4d\u7f6e\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3068\u540c\u7b49\n    i = binary_search_insertion(nums, target)\n    # \u30bf\u30fc\u30b2\u30c3\u30c8\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u5834\u5408\u3001-1 \u3092\u8fd4\u3059\n    if i == len(nums) or nums[i] != target:\n        return -1\n    # \u30bf\u30fc\u30b2\u30c3\u30c8\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i \u3092\u8fd4\u3059\n    return i\n
        binary_search_edge.cpp
        /* \u6700\u5de6\u306e\u30bf\u30fc\u30b2\u30c3\u30c8\u306e\u4e8c\u5206\u63a2\u7d22 */\nint binarySearchLeftEdge(vector<int> &nums, int target) {\n    // target\u306e\u633f\u5165\u30dd\u30a4\u30f3\u30c8\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3068\u7b49\u4fa1\n    int i = binarySearchInsertion(nums, target);\n    // target\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u305f\u3081\u3001-1\u3092\u8fd4\u3059\n    if (i == nums.size() || nums[i] != target) {\n        return -1;\n    }\n    // target\u304c\u898b\u3064\u304b\u3063\u305f\u305f\u3081\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9i\u3092\u8fd4\u3059\n    return i;\n}\n
        binary_search_edge.java
        /* \u6700\u3082\u5de6\u306e target \u3092\u4e8c\u5206\u63a2\u7d22 */\nint binarySearchLeftEdge(int[] nums, int target) {\n    // target \u306e\u633f\u5165\u70b9\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3068\u7b49\u4fa1\n    int i = binary_search_insertion.binarySearchInsertion(nums, target);\n    // target \u3092\u898b\u3064\u3051\u3089\u308c\u306a\u304b\u3063\u305f\u306e\u3067\u3001-1 \u3092\u8fd4\u3059\n    if (i == nums.length || nums[i] != target) {\n        return -1;\n    }\n    // target \u3092\u898b\u3064\u3051\u305f\u306e\u3067\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i \u3092\u8fd4\u3059\n    return i;\n}\n
        binary_search_edge.cs
        [class]{binary_search_edge}-[func]{BinarySearchLeftEdge}\n
        binary_search_edge.go
        [class]{}-[func]{binarySearchLeftEdge}\n
        binary_search_edge.swift
        [class]{}-[func]{binarySearchLeftEdge}\n
        binary_search_edge.js
        [class]{}-[func]{binarySearchLeftEdge}\n
        binary_search_edge.ts
        [class]{}-[func]{binarySearchLeftEdge}\n
        binary_search_edge.dart
        [class]{}-[func]{binarySearchLeftEdge}\n
        binary_search_edge.rs
        [class]{}-[func]{binary_search_left_edge}\n
        binary_search_edge.c
        [class]{}-[func]{binarySearchLeftEdge}\n
        binary_search_edge.kt
        [class]{}-[func]{binarySearchLeftEdge}\n
        binary_search_edge.rb
        [class]{}-[func]{binary_search_left_edge}\n
        "},{"location":"chapter_searching/binary_search_edge/#1032","title":"10.3.2 \u00a0 \u53f3\u5883\u754c\u3092\u898b\u3064\u3051\u308b","text":"

        target\u306e\u6700\u3082\u53f3\u306e\u51fa\u73fe\u3092\u3069\u306e\u3088\u3046\u306b\u898b\u3064\u3051\u308b\u3067\u3057\u3087\u3046\u304b\uff1f\u6700\u3082\u76f4\u63a5\u7684\u306a\u65b9\u6cd5\u306f\u3001nums[m] == target\u306e\u5834\u5408\u306b\u63a2\u7d22\u5883\u754c\u3092\u8abf\u6574\u3059\u308b\u65b9\u6cd5\u3092\u5909\u66f4\u3057\u3066\u3001\u5f93\u6765\u306e\u4e8c\u5206\u63a2\u7d22\u30ed\u30b8\u30c3\u30af\u3092\u4fee\u6b63\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u30b3\u30fc\u30c9\u306f\u3053\u3053\u3067\u306f\u7701\u7565\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u8208\u5473\u304c\u3042\u308b\u5834\u5408\u306f\u3001\u81ea\u5206\u3067\u30b3\u30fc\u30c9\u3092\u5b9f\u88c5\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002

        \u4ee5\u4e0b\u3067\u306f\u3001\u3055\u3089\u306b2\u3064\u306e\u5de7\u5999\u306a\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002

        "},{"location":"chapter_searching/binary_search_edge/#1","title":"1. \u00a0 \u5de6\u5883\u754c\u63a2\u7d22\u3092\u518d\u5229\u7528\u3059\u308b","text":"

        target\u306e\u6700\u3082\u53f3\u306e\u51fa\u73fe\u3092\u898b\u3064\u3051\u308b\u306b\u306f\u3001\u6700\u3082\u5de6\u306etarget\u3092\u898b\u3064\u3051\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u305f\u95a2\u6570\u3092\u518d\u5229\u7528\u3067\u304d\u307e\u3059\u3002\u5177\u4f53\u7684\u306b\u306f\u3001\u6700\u3082\u53f3\u306e\u30bf\u30fc\u30b2\u30c3\u30c8\u306e\u63a2\u7d22\u3092\u6700\u3082\u5de6\u306e\u30bf\u30fc\u30b2\u30c3\u30c8 + 1\u306e\u63a2\u7d22\u306b\u5909\u63db\u3057\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u63a2\u7d22\u5b8c\u4e86\u5f8c\u3001\u30dd\u30a4\u30f3\u30bf\\(i\\)\u306f\u6700\u3082\u5de6\u306etarget + 1\uff08\u5b58\u5728\u3059\u308b\u5834\u5408\uff09\u3092\u6307\u3057\u3001\u30dd\u30a4\u30f3\u30bf\\(j\\)\u306ftarget\u306e\u6700\u3082\u53f3\u306e\u51fa\u73fe\u3092\u6307\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\\(j\\)\u3092\u8fd4\u3059\u3053\u3068\u3067\u53f3\u5883\u754c\u304c\u5f97\u3089\u308c\u307e\u3059\u3002

        \u56f3 10-7 \u00a0 Transforming the search for the right boundary into the search for the left boundary

        \u8fd4\u3055\u308c\u308b\u633f\u5165\u4f4d\u7f6e\u306f\\(i\\)\u3067\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3057\u305f\u304c\u3063\u3066\u3001\\(j\\)\u3092\u5f97\u308b\u305f\u3081\u306b\u306f1\u3092\u5f15\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_edge.py
        def binary_search_right_edge(nums: list[int], target: int) -> int:\n    \"\"\"\u6700\u53f3\u7aef\u306e\u30bf\u30fc\u30b2\u30c3\u30c8\u306e\u4e8c\u5206\u63a2\u7d22\"\"\"\n    # \u6700\u5de6\u7aef\u306e\u30bf\u30fc\u30b2\u30c3\u30c8 + 1 \u3092\u898b\u3064\u3051\u308b\u3053\u3068\u306b\u5909\u63db\n    i = binary_search_insertion(nums, target + 1)\n    # j \u306f\u6700\u53f3\u7aef\u306e\u30bf\u30fc\u30b2\u30c3\u30c8\u3092\u6307\u3057\u3001i \u306f\u30bf\u30fc\u30b2\u30c3\u30c8\u3088\u308a\u5927\u304d\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u6307\u3059\n    j = i - 1\n    # \u30bf\u30fc\u30b2\u30c3\u30c8\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u5834\u5408\u3001-1 \u3092\u8fd4\u3059\n    if j == -1 or nums[j] != target:\n        return -1\n    # \u30bf\u30fc\u30b2\u30c3\u30c8\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 j \u3092\u8fd4\u3059\n    return j\n
        binary_search_edge.cpp
        /* \u6700\u53f3\u306e\u30bf\u30fc\u30b2\u30c3\u30c8\u306e\u4e8c\u5206\u63a2\u7d22 */\nint binarySearchRightEdge(vector<int> &nums, int target) {\n    // \u6700\u5de6\u306etarget + 1\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u306b\u5909\u63db\n    int i = binarySearchInsertion(nums, target + 1);\n    // j\u306f\u6700\u53f3\u306e\u30bf\u30fc\u30b2\u30c3\u30c8\u3092\u6307\u3057\u3001i\u306ftarget\u3088\u308a\u5927\u304d\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u6307\u3059\n    int j = i - 1;\n    // target\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u305f\u3081\u3001-1\u3092\u8fd4\u3059\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // target\u304c\u898b\u3064\u304b\u3063\u305f\u305f\u3081\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9j\u3092\u8fd4\u3059\n    return j;\n}\n
        binary_search_edge.java
        /* \u6700\u3082\u53f3\u306e target \u3092\u4e8c\u5206\u63a2\u7d22 */\nint binarySearchRightEdge(int[] nums, int target) {\n    // \u6700\u3082\u5de6\u306e target + 1 \u3092\u898b\u3064\u3051\u308b\u3053\u3068\u306b\u5909\u63db\n    int i = binary_search_insertion.binarySearchInsertion(nums, target + 1);\n    // j \u306f\u6700\u3082\u53f3\u306e target \u3092\u6307\u3057\u3001i \u306f target \u3088\u308a\u5927\u304d\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u6307\u3059\n    int j = i - 1;\n    // target \u3092\u898b\u3064\u3051\u3089\u308c\u306a\u304b\u3063\u305f\u306e\u3067\u3001-1 \u3092\u8fd4\u3059\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // target \u3092\u898b\u3064\u3051\u305f\u306e\u3067\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 j \u3092\u8fd4\u3059\n    return j;\n}\n
        binary_search_edge.cs
        [class]{binary_search_edge}-[func]{BinarySearchRightEdge}\n
        binary_search_edge.go
        [class]{}-[func]{binarySearchRightEdge}\n
        binary_search_edge.swift
        [class]{}-[func]{binarySearchRightEdge}\n
        binary_search_edge.js
        [class]{}-[func]{binarySearchRightEdge}\n
        binary_search_edge.ts
        [class]{}-[func]{binarySearchRightEdge}\n
        binary_search_edge.dart
        [class]{}-[func]{binarySearchRightEdge}\n
        binary_search_edge.rs
        [class]{}-[func]{binary_search_right_edge}\n
        binary_search_edge.c
        [class]{}-[func]{binarySearchRightEdge}\n
        binary_search_edge.kt
        [class]{}-[func]{binarySearchRightEdge}\n
        binary_search_edge.rb
        [class]{}-[func]{binary_search_right_edge}\n
        "},{"location":"chapter_searching/binary_search_edge/#2","title":"2. \u00a0 \u8981\u7d20\u63a2\u7d22\u306b\u5909\u63db\u3059\u308b","text":"

        \u914d\u5217\u306btarget\u304c\u542b\u307e\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u3001\\(i\\)\u3068\\(j\\)\u306f\u6700\u7d42\u7684\u306btarget\u3088\u308a\u5927\u304d\u3044\u6700\u521d\u306e\u8981\u7d20\u3068\u5c0f\u3055\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u305d\u308c\u305e\u308c\u6307\u3057\u307e\u3059\u3002

        \u3057\u305f\u304c\u3063\u3066\u3001\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u914d\u5217\u306b\u5b58\u5728\u3057\u306a\u3044\u8981\u7d20\u3092\u69cb\u7bc9\u3057\u3066\u3001\u5de6\u3068\u53f3\u306e\u5883\u754c\u3092\u63a2\u7d22\u3067\u304d\u307e\u3059\u3002

        • \u6700\u3082\u5de6\u306etarget\u3092\u898b\u3064\u3051\u308b\u306b\u306f\uff1atarget - 0.5\u3092\u63a2\u7d22\u3059\u308b\u3053\u3068\u306b\u5909\u63db\u3067\u304d\u3001\u30dd\u30a4\u30f3\u30bf\\(i\\)\u3092\u8fd4\u3057\u307e\u3059\u3002
        • \u6700\u3082\u53f3\u306etarget\u3092\u898b\u3064\u3051\u308b\u306b\u306f\uff1atarget + 0.5\u3092\u63a2\u7d22\u3059\u308b\u3053\u3068\u306b\u5909\u63db\u3067\u304d\u3001\u30dd\u30a4\u30f3\u30bf\\(j\\)\u3092\u8fd4\u3057\u307e\u3059\u3002

        \u56f3 10-8 \u00a0 Transforming the search for boundaries into the search for an element

        \u30b3\u30fc\u30c9\u306f\u3053\u3053\u3067\u306f\u7701\u7565\u3055\u308c\u3066\u3044\u307e\u3059\u304c\u3001\u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u306b\u3064\u3044\u3066\u6ce8\u610f\u3059\u3079\u304d2\u3064\u306e\u91cd\u8981\u306a\u70b9\u304c\u3042\u308a\u307e\u3059\u3002

        • \u4e0e\u3048\u3089\u308c\u305f\u914d\u5217nums\u306b\u306f\u5c0f\u6570\u304c\u542b\u307e\u308c\u3066\u3044\u306a\u3044\u305f\u3081\u3001\u7b49\u3057\u3044\u5834\u5408\u306e\u51e6\u7406\u306f\u5fc3\u914d\u3042\u308a\u307e\u305b\u3093\u3002
        • \u305f\u3060\u3057\u3001\u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u3067\u5c0f\u6570\u3092\u5c0e\u5165\u3059\u308b\u306b\u306f\u3001target\u5909\u6570\u3092\u6d6e\u52d5\u5c0f\u6570\u70b9\u578b\u306b\u5909\u66f4\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff08Python\u3067\u306f\u5909\u66f4\u306f\u4e0d\u8981\u3067\u3059\uff09\u3002
        "},{"location":"chapter_searching/binary_search_insertion/","title":"10.2 \u00a0 \u4e8c\u5206\u63a2\u7d22\u306b\u3088\u308b\u633f\u5165","text":"

        \u4e8c\u5206\u63a2\u7d22\u306f\u76ee\u6a19\u8981\u7d20\u3092\u63a2\u7d22\u3059\u308b\u3060\u3051\u3067\u306a\u304f\u3001\u76ee\u6a19\u8981\u7d20\u306e\u633f\u5165\u4f4d\u7f6e\u3092\u63a2\u7d22\u3059\u308b\u306a\u3069\u3001\u591a\u304f\u306e\u5909\u7a2e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3082\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002

        "},{"location":"chapter_searching/binary_search_insertion/#1021","title":"10.2.1 \u00a0 \u91cd\u8907\u8981\u7d20\u304c\u306a\u3044\u5834\u5408","text":"

        Question

        \u4e00\u610f\u306e\u8981\u7d20\u3092\u6301\u3064\u9577\u3055\\(n\\)\u306e\u30bd\u30fc\u30c8\u6e08\u307f\u914d\u5217nums\u3068\u8981\u7d20target\u304c\u4e0e\u3048\u3089\u308c\u3001\u30bd\u30fc\u30c8\u9806\u3092\u7dad\u6301\u3057\u306a\u304c\u3089target\u3092nums\u306b\u633f\u5165\u3057\u307e\u3059\u3002target\u304c\u914d\u5217\u306b\u3059\u3067\u306b\u5b58\u5728\u3059\u308b\u5834\u5408\u306f\u3001\u65e2\u5b58\u306e\u8981\u7d20\u306e\u5de6\u5074\u306b\u633f\u5165\u3057\u307e\u3059\u3002\u633f\u5165\u5f8c\u306e\u914d\u5217\u306b\u304a\u3051\u308btarget\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4e0b\u56f3\u306b\u793a\u3059\u4f8b\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u56f3 10-4 \u00a0 Example data for binary search insertion point

        \u524d\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u306e\u4e8c\u5206\u63a2\u7d22\u30b3\u30fc\u30c9\u3092\u518d\u5229\u7528\u3057\u305f\u3044\u5834\u5408\u3001\u4ee5\u4e0b\u306e2\u3064\u306e\u8cea\u554f\u306b\u7b54\u3048\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u8cea\u554f1\uff1a\u914d\u5217\u306b\u3059\u3067\u306btarget\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u633f\u5165\u4f4d\u7f6e\u306f\u65e2\u5b58\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u306a\u308a\u307e\u3059\u304b\uff1f

        target\u3092\u7b49\u3057\u3044\u8981\u7d20\u306e\u5de6\u5074\u306b\u633f\u5165\u3059\u308b\u3068\u3044\u3046\u8981\u4ef6\u306f\u3001\u65b0\u3057\u304f\u633f\u5165\u3055\u308c\u308btarget\u304c\u5143\u306etarget\u306e\u4f4d\u7f6e\u3092\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u914d\u5217\u306btarget\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u633f\u5165\u4f4d\u7f6e\u306f\u78ba\u304b\u306b\u305d\u306etarget\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u3059\u3002

        \u8cea\u554f2\uff1a\u914d\u5217\u306btarget\u304c\u542b\u307e\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u3001\u3069\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u633f\u5165\u3055\u308c\u307e\u3059\u304b\uff1f

        \u4e8c\u5206\u63a2\u7d22\u30d7\u30ed\u30bb\u30b9\u3092\u3055\u3089\u306b\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\uff1anums[m] < target\u306e\u3068\u304d\u3001\u30dd\u30a4\u30f3\u30bf\\(i\\)\u304c\u79fb\u52d5\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u30dd\u30a4\u30f3\u30bf\\(i\\)\u304ctarget\u4ee5\u4e0a\u306e\u8981\u7d20\u306b\u8fd1\u3065\u3044\u3066\u3044\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u540c\u69d8\u306b\u3001\u30dd\u30a4\u30f3\u30bf\\(j\\)\u306f\u5e38\u306btarget\u4ee5\u4e0b\u306e\u8981\u7d20\u306b\u8fd1\u3065\u3044\u3066\u3044\u307e\u3059\u3002

        \u3057\u305f\u304c\u3063\u3066\u3001\u4e8c\u5206\u306e\u7d42\u4e86\u6642\u306b\u306f\u78ba\u5b9f\u306b\uff1a\\(i\\)\u306ftarget\u3088\u308a\u5927\u304d\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u6307\u3057\u3001\\(j\\)\u306ftarget\u3088\u308a\u5c0f\u3055\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u6307\u3057\u307e\u3059\u3002\u914d\u5217\u306btarget\u304c\u542b\u307e\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u3001\u633f\u5165\u4f4d\u7f6e\u306f\\(i\\)\u3067\u3042\u308b\u3053\u3068\u306f\u660e\u3089\u304b\u3067\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_insertion.py
        def binary_search_insertion_simple(nums: list[int], target: int) -> int:\n    \"\"\"\u633f\u5165\u4f4d\u7f6e\u306e\u4e8c\u5206\u63a2\u7d22\uff08\u91cd\u8907\u8981\u7d20\u306a\u3057\uff09\"\"\"\n    i, j = 0, len(nums) - 1  # \u4e21\u7aef\u9589\u533a\u9593 [0, n-1] \u3092\u521d\u671f\u5316\n    while i <= j:\n        m = i + (j - i) // 2  # \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 m \u3092\u8a08\u7b97\n        if nums[m] < target:\n            i = m + 1  # \u30bf\u30fc\u30b2\u30c3\u30c8\u306f\u533a\u9593 [m+1, j] \u306b\u3042\u308b\n        elif nums[m] > target:\n            j = m - 1  # \u30bf\u30fc\u30b2\u30c3\u30c8\u306f\u533a\u9593 [i, m-1] \u306b\u3042\u308b\n        else:\n            return m  # \u30bf\u30fc\u30b2\u30c3\u30c8\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001\u633f\u5165\u4f4d\u7f6e m \u3092\u8fd4\u3059\n    # \u30bf\u30fc\u30b2\u30c3\u30c8\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u5834\u5408\u3001\u633f\u5165\u4f4d\u7f6e i \u3092\u8fd4\u3059\n    return i\n
        binary_search_insertion.cpp
        /* \u633f\u5165\u30dd\u30a4\u30f3\u30c8\u306e\u4e8c\u5206\u63a2\u7d22\uff08\u91cd\u8907\u8981\u7d20\u306a\u3057\uff09 */\nint binarySearchInsertionSimple(vector<int> &nums, int target) {\n    int i = 0, j = nums.size() - 1; // \u4e21\u7aef\u9589\u533a\u9593[0, n-1]\u3092\u521d\u671f\u5316\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9m\u3092\u8a08\u7b97\n        if (nums[m] < target) {\n            i = m + 1; // \u30bf\u30fc\u30b2\u30c3\u30c8\u306f\u533a\u9593[m+1, j]\u306b\u3042\u308b\n        } else if (nums[m] > target) {\n            j = m - 1; // \u30bf\u30fc\u30b2\u30c3\u30c8\u306f\u533a\u9593[i, m-1]\u306b\u3042\u308b\n        } else {\n            return m; // \u30bf\u30fc\u30b2\u30c3\u30c8\u304c\u898b\u3064\u304b\u3063\u305f\u305f\u3081\u3001\u633f\u5165\u30dd\u30a4\u30f3\u30c8m\u3092\u8fd4\u3059\n        }\n    }\n    // \u30bf\u30fc\u30b2\u30c3\u30c8\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u305f\u3081\u3001\u633f\u5165\u30dd\u30a4\u30f3\u30c8i\u3092\u8fd4\u3059\n    return i;\n}\n
        binary_search_insertion.java
        /* \u633f\u5165\u70b9\u306e\u4e8c\u5206\u63a2\u7d22\uff08\u91cd\u8907\u8981\u7d20\u306a\u3057\uff09 */\nint binarySearchInsertionSimple(int[] nums, int target) {\n    int i = 0, j = nums.length - 1; // \u4e21\u7aef\u9589\u533a\u9593 [0, n-1] \u3092\u521d\u671f\u5316\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 m \u3092\u8a08\u7b97\n        if (nums[m] < target) {\n            i = m + 1; // target \u306f\u533a\u9593 [m+1, j] \u306b\u3042\u308b\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u306f\u533a\u9593 [i, m-1] \u306b\u3042\u308b\n        } else {\n            return m; // target \u3092\u898b\u3064\u3051\u305f\u306e\u3067\u3001\u633f\u5165\u70b9 m \u3092\u8fd4\u3059\n        }\n    }\n    // target \u3092\u898b\u3064\u3051\u3089\u308c\u306a\u304b\u3063\u305f\u306e\u3067\u3001\u633f\u5165\u70b9 i \u3092\u8fd4\u3059\n    return i;\n}\n
        binary_search_insertion.cs
        [class]{binary_search_insertion}-[func]{BinarySearchInsertionSimple}\n
        binary_search_insertion.go
        [class]{}-[func]{binarySearchInsertionSimple}\n
        binary_search_insertion.swift
        [class]{}-[func]{binarySearchInsertionSimple}\n
        binary_search_insertion.js
        [class]{}-[func]{binarySearchInsertionSimple}\n
        binary_search_insertion.ts
        [class]{}-[func]{binarySearchInsertionSimple}\n
        binary_search_insertion.dart
        [class]{}-[func]{binarySearchInsertionSimple}\n
        binary_search_insertion.rs
        [class]{}-[func]{binary_search_insertion_simple}\n
        binary_search_insertion.c
        [class]{}-[func]{binarySearchInsertionSimple}\n
        binary_search_insertion.kt
        [class]{}-[func]{binarySearchInsertionSimple}\n
        binary_search_insertion.rb
        [class]{}-[func]{binary_search_insertion_simple}\n
        "},{"location":"chapter_searching/binary_search_insertion/#1022","title":"10.2.2 \u00a0 \u91cd\u8907\u8981\u7d20\u304c\u3042\u308b\u5834\u5408","text":"

        Question

        \u524d\u306e\u8cea\u554f\u306b\u57fa\u3065\u3044\u3066\u3001\u914d\u5217\u306b\u91cd\u8907\u8981\u7d20\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3068\u4eee\u5b9a\u3057\u3001\u4ed6\u306f\u3059\u3079\u3066\u540c\u3058\u3068\u3057\u307e\u3059\u3002

        \u914d\u5217\u306btarget\u306e\u8907\u6570\u306e\u51fa\u73fe\u304c\u3042\u308b\u5834\u5408\u3001\u901a\u5e38\u306e\u4e8c\u5206\u63a2\u7d22\u306ftarget\u306e1\u3064\u306e\u51fa\u73fe\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u307f\u3092\u8fd4\u3059\u3053\u3068\u304c\u3067\u304d\u3001\u305d\u306e\u4f4d\u7f6e\u306e\u5de6\u53f3\u306btarget\u306e\u51fa\u73fe\u304c\u3044\u304f\u3064\u3042\u308b\u304b\u3092\u7279\u5b9a\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002

        \u554f\u984c\u3067\u306f\u76ee\u6a19\u8981\u7d20\u3092\u6700\u3082\u5de6\u306e\u4f4d\u7f6e\u306b\u633f\u5165\u3059\u308b\u3053\u3068\u304c\u8981\u6c42\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u914d\u5217\u5185\u306e\u6700\u3082\u5de6\u306etarget\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u898b\u3064\u3051\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u6700\u521d\u306b\u4e0b\u56f3\u306b\u793a\u3059\u30b9\u30c6\u30c3\u30d7\u3092\u901a\u3057\u3066\u3053\u308c\u3092\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002

        1. \u4e8c\u5206\u63a2\u7d22\u3092\u5b9f\u884c\u3057\u3066target\u306e\u4efb\u610f\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3001\u4f8b\u3048\u3070\\(k\\)\u3092\u898b\u3064\u3051\u307e\u3059\u3002
        2. \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(k\\)\u304b\u3089\u958b\u59cb\u3057\u3066\u3001\u6700\u3082\u5de6\u306etarget\u306e\u51fa\u73fe\u304c\u898b\u3064\u304b\u308b\u307e\u3067\u5de6\u306b\u7dda\u5f62\u63a2\u7d22\u3092\u884c\u3044\u3001\u3053\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3057\u307e\u3059\u3002

        \u56f3 10-5 \u00a0 Linear search for the insertion point of duplicate elements

        \u3053\u306e\u65b9\u6cd5\u306f\u5b9f\u73fe\u53ef\u80fd\u3067\u3059\u304c\u3001\u7dda\u5f62\u63a2\u7d22\u3092\u542b\u3080\u305f\u3081\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3059\u3002\u3053\u306e\u65b9\u6cd5\u306f\u3001\u914d\u5217\u306b\u591a\u304f\u306e\u91cd\u8907\u3059\u308btarget\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u5834\u5408\u306b\u975e\u52b9\u7387\u3067\u3059\u3002

        \u4eca\u5ea6\u306f\u4e8c\u5206\u63a2\u7d22\u30b3\u30fc\u30c9\u3092\u62e1\u5f35\u3059\u308b\u3053\u3068\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5168\u4f53\u7684\u306a\u30d7\u30ed\u30bb\u30b9\u306f\u540c\u3058\u307e\u307e\u3067\u3059\u3002\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u3001\u307e\u305a\u4e2d\u9593\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(m\\)\u3092\u8a08\u7b97\u3057\u3001\u6b21\u306btarget\u3068nums[m]\u306e\u5024\u3092\u6bd4\u8f03\u3057\u3066\u3001\u4ee5\u4e0b\u306e\u30b1\u30fc\u30b9\u306b\u306a\u308a\u307e\u3059\u3002

        • nums[m] < target\u307e\u305f\u306fnums[m] > target\u306e\u3068\u304d\u3001\u3053\u308c\u306ftarget\u304c\u307e\u3060\u898b\u3064\u304b\u3063\u3066\u3044\u306a\u3044\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u305f\u3081\u3001\u901a\u5e38\u306e\u4e8c\u5206\u63a2\u7d22\u3092\u4f7f\u7528\u3057\u3066\u63a2\u7d22\u7bc4\u56f2\u3092\u72ed\u3081\u3001\u30dd\u30a4\u30f3\u30bf\\(i\\)\u3068\\(j\\)\u3092target\u306b\u8fd1\u3065\u3051\u307e\u3059\u3002
        • nums[m] == target\u306e\u3068\u304d\u3001\u3053\u308c\u306ftarget\u3088\u308a\u5c0f\u3055\u3044\u8981\u7d20\u304c\u7bc4\u56f2\\([i, m - 1]\\)\u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\u305f\u3081\u3001\\(j = m - 1\\)\u3092\u4f7f\u7528\u3057\u3066\u7bc4\u56f2\u3092\u72ed\u3081\u3001\u30dd\u30a4\u30f3\u30bf\\(j\\)\u3092target\u3088\u308a\u5c0f\u3055\u3044\u8981\u7d20\u306b\u8fd1\u3065\u3051\u307e\u3059\u3002

        \u30eb\u30fc\u30d7\u5f8c\u3001\\(i\\)\u306f\u6700\u3082\u5de6\u306etarget\u3092\u6307\u3057\u3001\\(j\\)\u306ftarget\u3088\u308a\u5c0f\u3055\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u6307\u3059\u305f\u3081\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(i\\)\u304c\u633f\u5165\u4f4d\u7f6e\u3067\u3059\u3002

        <1><2><3><4><5><6><7><8>

        \u56f3 10-6 \u00a0 Steps for binary search insertion point of duplicate elements

        \u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u89b3\u5bdf\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u5206\u5c90nums[m] > target\u3068nums[m] == target\u306e\u64cd\u4f5c\u306f\u540c\u3058\u3067\u3042\u308b\u305f\u3081\u3001\u3053\u308c\u30892\u3064\u306e\u5206\u5c90\u3092\u30de\u30fc\u30b8\u3067\u304d\u307e\u3059\u3002

        \u305d\u308c\u3067\u3082\u3001\u30ed\u30b8\u30c3\u30af\u304c\u3088\u308a\u660e\u78ba\u306b\u306a\u308a\u3001\u53ef\u8aad\u6027\u304c\u5411\u4e0a\u3059\u308b\u305f\u3081\u3001\u6761\u4ef6\u3092\u5c55\u958b\u3057\u305f\u307e\u307e\u306b\u3057\u3066\u304a\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_insertion.py
        def binary_search_insertion(nums: list[int], target: int) -> int:\n    \"\"\"\u633f\u5165\u4f4d\u7f6e\u306e\u4e8c\u5206\u63a2\u7d22\uff08\u91cd\u8907\u8981\u7d20\u3042\u308a\uff09\"\"\"\n    i, j = 0, len(nums) - 1  # \u4e21\u7aef\u9589\u533a\u9593 [0, n-1] \u3092\u521d\u671f\u5316\n    while i <= j:\n        m = i + (j - i) // 2  # \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 m \u3092\u8a08\u7b97\n        if nums[m] < target:\n            i = m + 1  # \u30bf\u30fc\u30b2\u30c3\u30c8\u306f\u533a\u9593 [m+1, j] \u306b\u3042\u308b\n        elif nums[m] > target:\n            j = m - 1  # \u30bf\u30fc\u30b2\u30c3\u30c8\u306f\u533a\u9593 [i, m-1] \u306b\u3042\u308b\n        else:\n            j = m - 1  # \u30bf\u30fc\u30b2\u30c3\u30c8\u672a\u6e80\u306e\u6700\u521d\u306e\u8981\u7d20\u306f\u533a\u9593 [i, m-1] \u306b\u3042\u308b\n    # \u633f\u5165\u4f4d\u7f6e i \u3092\u8fd4\u3059\n    return i\n
        binary_search_insertion.cpp
        /* \u633f\u5165\u30dd\u30a4\u30f3\u30c8\u306e\u4e8c\u5206\u63a2\u7d22\uff08\u91cd\u8907\u8981\u7d20\u3042\u308a\uff09 */\nint binarySearchInsertion(vector<int> &nums, int target) {\n    int i = 0, j = nums.size() - 1; // \u4e21\u7aef\u9589\u533a\u9593[0, n-1]\u3092\u521d\u671f\u5316\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9m\u3092\u8a08\u7b97\n        if (nums[m] < target) {\n            i = m + 1; // \u30bf\u30fc\u30b2\u30c3\u30c8\u306f\u533a\u9593[m+1, j]\u306b\u3042\u308b\n        } else if (nums[m] > target) {\n            j = m - 1; // \u30bf\u30fc\u30b2\u30c3\u30c8\u306f\u533a\u9593[i, m-1]\u306b\u3042\u308b\n        } else {\n            j = m - 1; // \u30bf\u30fc\u30b2\u30c3\u30c8\u672a\u6e80\u306e\u6700\u521d\u306e\u8981\u7d20\u306f\u533a\u9593[i, m-1]\u306b\u3042\u308b\n        }\n    }\n    // \u633f\u5165\u30dd\u30a4\u30f3\u30c8i\u3092\u8fd4\u3059\n    return i;\n}\n
        binary_search_insertion.java
        /* \u633f\u5165\u70b9\u306e\u4e8c\u5206\u63a2\u7d22\uff08\u91cd\u8907\u8981\u7d20\u3042\u308a\uff09 */\nint binarySearchInsertion(int[] nums, int target) {\n    int i = 0, j = nums.length - 1; // \u4e21\u7aef\u9589\u533a\u9593 [0, n-1] \u3092\u521d\u671f\u5316\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 m \u3092\u8a08\u7b97\n        if (nums[m] < target) {\n            i = m + 1; // target \u306f\u533a\u9593 [m+1, j] \u306b\u3042\u308b\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u306f\u533a\u9593 [i, m-1] \u306b\u3042\u308b\n        } else {\n            j = m - 1; // target \u3088\u308a\u5c0f\u3055\u3044\u6700\u521d\u306e\u8981\u7d20\u306f\u533a\u9593 [i, m-1] \u306b\u3042\u308b\n        }\n    }\n    // \u633f\u5165\u70b9 i \u3092\u8fd4\u3059\n    return i;\n}\n
        binary_search_insertion.cs
        [class]{binary_search_insertion}-[func]{BinarySearchInsertion}\n
        binary_search_insertion.go
        [class]{}-[func]{binarySearchInsertion}\n
        binary_search_insertion.swift
        [class]{}-[func]{binarySearchInsertion}\n
        binary_search_insertion.js
        [class]{}-[func]{binarySearchInsertion}\n
        binary_search_insertion.ts
        [class]{}-[func]{binarySearchInsertion}\n
        binary_search_insertion.dart
        [class]{}-[func]{binarySearchInsertion}\n
        binary_search_insertion.rs
        [class]{}-[func]{binary_search_insertion}\n
        binary_search_insertion.c
        [class]{}-[func]{binarySearchInsertion}\n
        binary_search_insertion.kt
        [class]{}-[func]{binarySearchInsertion}\n
        binary_search_insertion.rb
        [class]{}-[func]{binary_search_insertion}\n

        Tip

        \u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u306e\u30b3\u30fc\u30c9\u306f\u300c\u9589\u533a\u9593\u300d\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002\u300c\u5de6\u9589\u53f3\u958b\u300d\u306b\u8208\u5473\u304c\u3042\u308b\u5834\u5408\u306f\u3001\u81ea\u5206\u3067\u30b3\u30fc\u30c9\u3092\u5b9f\u88c5\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002

        \u8981\u7d04\u3059\u308b\u3068\u3001\u4e8c\u5206\u63a2\u7d22\u306f\u672c\u8cea\u7684\u306b\u30dd\u30a4\u30f3\u30bf\\(i\\)\u3068\\(j\\)\u306e\u63a2\u7d22\u76ee\u6a19\u3092\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u3053\u308c\u3089\u306e\u76ee\u6a19\u306f\u7279\u5b9a\u306e\u8981\u7d20\uff08target\u306a\u3069\uff09\u307e\u305f\u306f\u8981\u7d20\u306e\u7bc4\u56f2\uff08target\u3088\u308a\u5c0f\u3055\u3044\u3082\u306e\u306a\u3069\uff09\u3067\u3042\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        \u4e8c\u5206\u63a2\u7d22\u306e\u9023\u7d9a\u30eb\u30fc\u30d7\u306b\u304a\u3044\u3066\u3001\u30dd\u30a4\u30f3\u30bf\\(i\\)\u3068\\(j\\)\u306f\u6bb5\u968e\u7684\u306b\u4e8b\u524d\u5b9a\u7fa9\u3055\u308c\u305f\u76ee\u6a19\u306b\u8fd1\u3065\u304d\u307e\u3059\u3002\u6700\u7d42\u7684\u306b\u3001\u305d\u308c\u3089\u306f\u7b54\u3048\u3092\u898b\u3064\u3051\u308b\u304b\u3001\u5883\u754c\u3092\u8d8a\u3048\u305f\u5f8c\u306b\u505c\u6b62\u3057\u307e\u3059\u3002

        "},{"location":"chapter_searching/replace_linear_by_hashing/","title":"10.4 \u00a0 \u30cf\u30c3\u30b7\u30e5\u6700\u9069\u5316\u6226\u7565","text":"

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u306b\u304a\u3044\u3066\u3001\u7dda\u5f62\u63a2\u7d22\u3092\u30cf\u30c3\u30b7\u30e5\u30d9\u30fc\u30b9\u306e\u63a2\u7d22\u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u3067\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3092\u524a\u6e1b\u3059\u308b\u3053\u3068\u304c\u3088\u304f\u3042\u308a\u307e\u3059\u3002\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u3092\u4f7f\u7528\u3057\u3066\u7406\u89e3\u3092\u6df1\u3081\u307e\u3057\u3087\u3046\u3002

        Question

        \u6574\u6570\u914d\u5217nums\u3068\u76ee\u6a19\u8981\u7d20target\u304c\u4e0e\u3048\u3089\u308c\u3001\u914d\u5217\u5185\u3067\u300c\u548c\u300d\u304ctarget\u306b\u7b49\u3057\u30442\u3064\u306e\u8981\u7d20\u3092\u63a2\u7d22\u3057\u3001\u305d\u308c\u3089\u306e\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4efb\u610f\u306e\u89e3\u304c\u53d7\u3051\u5165\u308c\u3089\u308c\u307e\u3059\u3002

        "},{"location":"chapter_searching/replace_linear_by_hashing/#1041","title":"10.4.1 \u00a0 \u7dda\u5f62\u63a2\u7d22\uff1a\u6642\u9593\u3092\u7a7a\u9593\u3068\u4ea4\u63db","text":"

        \u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u7d44\u307f\u5408\u308f\u305b\u3092\u76f4\u63a5\u6a2a\u65ad\u3059\u308b\u3053\u3068\u3092\u8003\u3048\u3066\u307f\u307e\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30cd\u30b9\u30c8\u3057\u305f\u30eb\u30fc\u30d7\u3092\u958b\u59cb\u3057\u3001\u5404\u53cd\u5fa9\u30672\u3064\u306e\u6574\u6570\u306e\u548c\u304ctarget\u306b\u7b49\u3057\u3044\u304b\u3069\u3046\u304b\u3092\u5224\u65ad\u3057\u307e\u3059\u3002\u305d\u3046\u3067\u3042\u308c\u3070\u3001\u305d\u308c\u3089\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3057\u307e\u3059\u3002

        \u56f3 10-9 \u00a0 Linear search solution for two-sum problem

        \u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby two_sum.py
        def two_sum_brute_force(nums: list[int], target: int) -> list[int]:\n    \"\"\"\u65b9\u6cd5\u4e00\uff1a\u30d6\u30eb\u30fc\u30c8\u30d5\u30a9\u30fc\u30b9\u5217\u6319\"\"\"\n    # \u4e8c\u91cd\u30eb\u30fc\u30d7\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f O(n^2)\n    for i in range(len(nums) - 1):\n        for j in range(i + 1, len(nums)):\n            if nums[i] + nums[j] == target:\n                return [i, j]\n    return []\n
        two_sum.cpp
        /* \u65b9\u6cd5\u4e00\uff1a\u30d6\u30eb\u30fc\u30c8\u30d5\u30a9\u30fc\u30b9\u5217\u6319 */\nvector<int> twoSumBruteForce(vector<int> &nums, int target) {\n    int size = nums.size();\n    // \u4e8c\u91cd\u30eb\u30fc\u30d7\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306fO(n^2)\n    for (int i = 0; i < size - 1; i++) {\n        for (int j = i + 1; j < size; j++) {\n            if (nums[i] + nums[j] == target)\n                return {i, j};\n        }\n    }\n    return {};\n}\n
        two_sum.java
        /* \u65b9\u6cd5\u4e00: \u66b4\u529b\u5217\u6319 */\nint[] twoSumBruteForce(int[] nums, int target) {\n    int size = nums.length;\n    // \u4e8c\u91cd\u30eb\u30fc\u30d7\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f O(n^2)\n    for (int i = 0; i < size - 1; i++) {\n        for (int j = i + 1; j < size; j++) {\n            if (nums[i] + nums[j] == target)\n                return new int[] { i, j };\n        }\n    }\n    return new int[0];\n}\n
        two_sum.cs
        [class]{two_sum}-[func]{TwoSumBruteForce}\n
        two_sum.go
        [class]{}-[func]{twoSumBruteForce}\n
        two_sum.swift
        [class]{}-[func]{twoSumBruteForce}\n
        two_sum.js
        [class]{}-[func]{twoSumBruteForce}\n
        two_sum.ts
        [class]{}-[func]{twoSumBruteForce}\n
        two_sum.dart
        [class]{}-[func]{twoSumBruteForce}\n
        two_sum.rs
        [class]{}-[func]{two_sum_brute_force}\n
        two_sum.c
        [class]{}-[func]{twoSumBruteForce}\n
        two_sum.kt
        [class]{}-[func]{twoSumBruteForce}\n
        two_sum.rb
        [class]{}-[func]{two_sum_brute_force}\n

        \u3053\u306e\u65b9\u6cd5\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n^2)\\)\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u3067\u3001\u5927\u5bb9\u91cf\u30c7\u30fc\u30bf\u3067\u306f\u975e\u5e38\u306b\u6642\u9593\u304c\u304b\u304b\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_searching/replace_linear_by_hashing/#1042","title":"10.4.2 \u00a0 \u30cf\u30c3\u30b7\u30e5\u63a2\u7d22\uff1a\u7a7a\u9593\u3092\u6642\u9593\u3068\u4ea4\u63db","text":"

        \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306e\u4f7f\u7528\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u30ad\u30fc\u3068\u5024\u306e\u30da\u30a2\u306f\u305d\u308c\u305e\u308c\u914d\u5217\u8981\u7d20\u3068\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u3059\u3002\u914d\u5217\u3092\u30eb\u30fc\u30d7\u3057\u3001\u5404\u53cd\u5fa9\u4e2d\u306b\u4e0b\u56f3\u306b\u793a\u3059\u30b9\u30c6\u30c3\u30d7\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002

        1. \u6570\u5024target - nums[i]\u304c\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306b\u3042\u308b\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002\u3042\u308b\u5834\u5408\u306f\u3001\u3053\u308c\u30892\u3064\u306e\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u76f4\u63a5\u8fd4\u3057\u307e\u3059\u3002
        2. \u30ad\u30fc\u3068\u5024\u306e\u30da\u30a2nums[i]\u3068\u30a4\u30f3\u30c7\u30c3\u30af\u30b9i\u3092\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306b\u8ffd\u52a0\u3057\u307e\u3059\u3002
        <1><2><3>

        \u56f3 10-10 \u00a0 Help hash table solve two-sum

        \u5b9f\u88c5\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306b\u793a\u3055\u308c\u3001\u5358\u4e00\u306e\u30eb\u30fc\u30d7\u306e\u307f\u304c\u5fc5\u8981\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby two_sum.py
        def two_sum_hash_table(nums: list[int], target: int) -> list[int]:\n    \"\"\"\u65b9\u6cd5\u4e8c\uff1a\u88dc\u52a9\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\"\"\"\n    # \u88dc\u52a9\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f O(n)\n    dic = {}\n    # \u5358\u4e00\u30eb\u30fc\u30d7\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f O(n)\n    for i in range(len(nums)):\n        if target - nums[i] in dic:\n            return [dic[target - nums[i]], i]\n        dic[nums[i]] = i\n    return []\n
        two_sum.cpp
        /* \u65b9\u6cd5\u4e8c\uff1a\u88dc\u52a9\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb */\nvector<int> twoSumHashTable(vector<int> &nums, int target) {\n    int size = nums.size();\n    // \u88dc\u52a9\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306fO(n)\n    unordered_map<int, int> dic;\n    // \u5358\u5c64\u30eb\u30fc\u30d7\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306fO(n)\n    for (int i = 0; i < size; i++) {\n        if (dic.find(target - nums[i]) != dic.end()) {\n            return {dic[target - nums[i]], i};\n        }\n        dic.emplace(nums[i], i);\n    }\n    return {};\n}\n
        two_sum.java
        /* \u65b9\u6cd5\u4e8c: \u88dc\u52a9\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb */\nint[] twoSumHashTable(int[] nums, int target) {\n    int size = nums.length;\n    // \u88dc\u52a9\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f O(n)\n    Map<Integer, Integer> dic = new HashMap<>();\n    // \u5358\u4e00\u5c64\u30eb\u30fc\u30d7\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f O(n)\n    for (int i = 0; i < size; i++) {\n        if (dic.containsKey(target - nums[i])) {\n            return new int[] { dic.get(target - nums[i]), i };\n        }\n        dic.put(nums[i], i);\n    }\n    return new int[0];\n}\n
        two_sum.cs
        [class]{two_sum}-[func]{TwoSumHashTable}\n
        two_sum.go
        [class]{}-[func]{twoSumHashTable}\n
        two_sum.swift
        [class]{}-[func]{twoSumHashTable}\n
        two_sum.js
        [class]{}-[func]{twoSumHashTable}\n
        two_sum.ts
        [class]{}-[func]{twoSumHashTable}\n
        two_sum.dart
        [class]{}-[func]{twoSumHashTable}\n
        two_sum.rs
        [class]{}-[func]{two_sum_hash_table}\n
        two_sum.c
        [class]{HashTable}-[func]{}\n\n[class]{}-[func]{twoSumHashTable}\n
        two_sum.kt
        [class]{}-[func]{twoSumHashTable}\n
        two_sum.rb
        [class]{}-[func]{two_sum_hash_table}\n

        \u3053\u306e\u65b9\u6cd5\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u63a2\u7d22\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u6642\u9593\u8a08\u7b97\u91cf\u3092\\(O(n^2)\\)\u304b\u3089\\(O(n)\\)\u306b\u524a\u6e1b\u3057\u3001\u5b9f\u884c\u6642\u52b9\u7387\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002

        \u8ffd\u52a0\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u7dad\u6301\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3059\u3002\u305d\u308c\u306b\u3082\u304b\u304b\u308f\u3089\u305a\u3001\u3053\u306e\u65b9\u6cd5\u306f\u5168\u4f53\u7684\u306b\u3088\u308a\u5747\u8861\u306e\u3068\u308c\u305f\u6642\u7a7a\u9593\u52b9\u7387\u3092\u6301\u3061\u3001\u3053\u306e\u554f\u984c\u306e\u6700\u9069\u89e3\u3068\u306a\u308a\u307e\u3059\u3002

        "},{"location":"chapter_searching/searching_algorithm_revisited/","title":"10.5 \u00a0 \u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u518d\u691c\u8a0e","text":"

        \u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff08\u691c\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff09\u306f\u3001\u914d\u5217\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u6728\u3001\u30b0\u30e9\u30d5\u306a\u3069\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u5185\u3067\u7279\u5b9a\u306e\u57fa\u6e96\u3092\u6e80\u305f\u30591\u3064\u4ee5\u4e0a\u306e\u8981\u7d20\u3092\u53d6\u5f97\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002

        \u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u305d\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u306b\u57fa\u3065\u3044\u3066\u4ee5\u4e0b\u306e2\u3064\u306e\u30ab\u30c6\u30b4\u30ea\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        • \u30c7\u30fc\u30bf\u69cb\u9020\u3092\u6a2a\u65ad\u3059\u308b\u3053\u3068\u3067\u76ee\u6a19\u8981\u7d20\u3092\u7279\u5b9a\u3059\u308b\uff1a\u914d\u5217\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u6728\u3001\u30b0\u30e9\u30d5\u306e\u6a2a\u65ad\u306a\u3069\u3002
        • \u30c7\u30fc\u30bf\u306e\u7d44\u7e54\u69cb\u9020\u3084\u65e2\u5b58\u306e\u30c7\u30fc\u30bf\u3092\u4f7f\u7528\u3057\u3066\u52b9\u7387\u7684\u306a\u8981\u7d20\u63a2\u7d22\u3092\u5b9f\u73fe\u3059\u308b\uff1a\u4e8c\u5206\u63a2\u7d22\u3001\u30cf\u30c3\u30b7\u30e5\u63a2\u7d22\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u63a2\u7d22\u306a\u3069\u3002

        \u3053\u308c\u3089\u306e\u30c8\u30d4\u30c3\u30af\u306f\u524d\u306e\u7ae0\u3067\u7d39\u4ecb\u3055\u308c\u305f\u305f\u3081\u3001\u79c1\u305f\u3061\u306b\u306f\u99b4\u67d3\u307f\u306e\u306a\u3044\u3082\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u3088\u308a\u4f53\u7cfb\u7684\u306a\u89b3\u70b9\u304b\u3089\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u518d\u691c\u8a0e\u3057\u307e\u3059\u3002

        "},{"location":"chapter_searching/searching_algorithm_revisited/#1051","title":"10.5.1 \u00a0 \u7dcf\u5f53\u305f\u308a\u63a2\u7d22","text":"

        \u7dcf\u5f53\u305f\u308a\u63a2\u7d22\u306f\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u6a2a\u65ad\u3059\u308b\u3053\u3068\u3067\u76ee\u6a19\u8981\u7d20\u3092\u7279\u5b9a\u3057\u307e\u3059\u3002

        • \u300c\u7dda\u5f62\u63a2\u7d22\u300d\u306f\u914d\u5217\u3084\u9023\u7d50\u30ea\u30b9\u30c8\u306a\u3069\u306e\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u4e00\u7aef\u304b\u3089\u958b\u59cb\u3057\u3001\u76ee\u6a19\u8981\u7d20\u304c\u898b\u3064\u304b\u308b\u304b\u3001\u76ee\u6a19\u8981\u7d20\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u306a\u304f\u4ed6\u7aef\u306b\u5230\u9054\u3059\u308b\u307e\u3067\u3001\u5404\u8981\u7d20\u306b\u4e00\u3064\u305a\u3064\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002
        • \u300c\u5e45\u512a\u5148\u63a2\u7d22\u300d\u3068\u300c\u6df1\u3055\u512a\u5148\u63a2\u7d22\u300d\u306f\u3001\u30b0\u30e9\u30d5\u3068\u6728\u306e2\u3064\u306e\u6a2a\u65ad\u6226\u7565\u3067\u3059\u3002\u5e45\u512a\u5148\u63a2\u7d22\u306f\u521d\u671f\u30ce\u30fc\u30c9\u304b\u3089\u958b\u59cb\u3057\u3001\u5c64\u3054\u3068\uff08\u5de6\u304b\u3089\u53f3\u3078\uff09\u306b\u63a2\u7d22\u3057\u3001\u8fd1\u304f\u304b\u3089\u9060\u304f\u306e\u30ce\u30fc\u30c9\u306b\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002\u6df1\u3055\u512a\u5148\u63a2\u7d22\u306f\u521d\u671f\u30ce\u30fc\u30c9\u304b\u3089\u958b\u59cb\u3057\u3001\u30d1\u30b9\u306e\u7d42\u7aef\uff08\u4e0a\u304b\u3089\u4e0b\u3078\uff09\u307e\u3067\u8ffd\u8de1\u3057\u3001\u305d\u306e\u5f8c\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30af\u3057\u3066\u4ed6\u306e\u30d1\u30b9\u3092\u8a66\u3057\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u5168\u4f53\u304c\u6a2a\u65ad\u3055\u308c\u308b\u307e\u3067\u7d9a\u884c\u3057\u307e\u3059\u3002

        \u7dcf\u5f53\u305f\u308a\u63a2\u7d22\u306e\u5229\u70b9\u306f\u3001\u305d\u306e\u5358\u7d14\u3055\u3068\u6c4e\u7528\u6027\u3067\u3042\u308a\u3001**\u30c7\u30fc\u30bf\u306e\u524d\u51e6\u7406\u3084\u8ffd\u52a0\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u52a9\u3051\u304c\u4e0d\u8981**\u3067\u3059\u3002

        \u305f\u3060\u3057\u3001**\u3053\u306e\u30bf\u30a4\u30d7\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)**\u3067\u3001\\(n\\)\u306f\u8981\u7d20\u6570\u3067\u3042\u308b\u305f\u3081\u3001\u5927\u898f\u6a21\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3067\u306f\u6027\u80fd\u304c\u60aa\u304f\u306a\u308a\u307e\u3059\u3002

        "},{"location":"chapter_searching/searching_algorithm_revisited/#1052","title":"10.5.2 \u00a0 \u9069\u5fdc\u7684\u63a2\u7d22","text":"

        \u9069\u5fdc\u7684\u63a2\u7d22\u306f\u3001\u30c7\u30fc\u30bf\u306e\u56fa\u6709\u306e\u6027\u8cea\uff08\u9806\u5e8f\u306a\u3069\uff09\u3092\u4f7f\u7528\u3057\u3066\u63a2\u7d22\u30d7\u30ed\u30bb\u30b9\u3092\u6700\u9069\u5316\u3057\u3001\u305d\u308c\u306b\u3088\u308a\u76ee\u6a19\u8981\u7d20\u3092\u3088\u308a\u52b9\u7387\u7684\u306b\u7279\u5b9a\u3057\u307e\u3059\u3002

        • \u300c\u4e8c\u5206\u63a2\u7d22\u300d\u306f\u30c7\u30fc\u30bf\u306e\u6574\u5217\u6027\u3092\u4f7f\u7528\u3057\u3066\u52b9\u7387\u7684\u306a\u63a2\u7d22\u3092\u5b9f\u73fe\u3057\u3001\u914d\u5217\u306b\u306e\u307f\u9069\u7528\u53ef\u80fd\u3067\u3059\u3002
        • \u300c\u30cf\u30c3\u30b7\u30e5\u63a2\u7d22\u300d\u306f\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u4f7f\u7528\u3057\u3066\u63a2\u7d22\u30c7\u30fc\u30bf\u3068\u76ee\u6a19\u30c7\u30fc\u30bf\u306e\u9593\u306b\u30ad\u30fc\u3068\u5024\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u3092\u78ba\u7acb\u3057\u3001\u305d\u308c\u306b\u3088\u308a\u30af\u30a8\u30ea\u64cd\u4f5c\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002
        • \u7279\u5b9a\u306e\u6728\u69cb\u9020\uff08\u4e8c\u5206\u63a2\u7d22\u6728\u306a\u3069\uff09\u3067\u306e\u300c\u6728\u63a2\u7d22\u300d\u306f\u3001\u30ce\u30fc\u30c9\u5024\u306e\u6bd4\u8f03\u306b\u57fa\u3065\u3044\u3066\u30ce\u30fc\u30c9\u3092\u8fc5\u901f\u306b\u9664\u5916\u3057\u3001\u305d\u308c\u306b\u3088\u308a\u76ee\u6a19\u8981\u7d20\u3092\u7279\u5b9a\u3057\u307e\u3059\u3002

        \u3053\u308c\u3089\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5229\u70b9\u306f\u9ad8\u52b9\u7387\u3067\u3042\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(\\log n)\\)\u307e\u305f\u306f\\(O(1)\\)\u306b\u307e\u3067\u9054\u3057\u307e\u3059\u3002

        \u305f\u3060\u3057\u3001\u3053\u308c\u3089\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u4f7f\u7528\u3059\u308b\u306b\u306f\u3001\u591a\u304f\u306e\u5834\u5408\u30c7\u30fc\u30bf\u306e\u524d\u51e6\u7406\u304c\u5fc5\u8981\u3067\u3059\u3002\u4f8b\u3048\u3070\u3001\u4e8c\u5206\u63a2\u7d22\u3067\u306f\u4e8b\u524d\u306b\u914d\u5217\u3092\u30bd\u30fc\u30c8\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u30cf\u30c3\u30b7\u30e5\u63a2\u7d22\u3068\u6728\u63a2\u7d22\u306e\u4e21\u65b9\u3067\u8ffd\u52a0\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u52a9\u3051\u304c\u5fc5\u8981\u3067\u3059\u3002\u3053\u308c\u3089\u306e\u69cb\u9020\u3092\u7dad\u6301\u3059\u308b\u3053\u3068\u3082\u3001\u6642\u9593\u3068\u7a7a\u9593\u306e\u9762\u3067\u3088\u308a\u591a\u304f\u306e\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u304c\u5fc5\u8981\u3067\u3059\u3002

        Tip

        \u9069\u5fdc\u7684\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u591a\u304f\u306e\u5834\u5408\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u547c\u3070\u308c\u3001\u4e3b\u306b\u7279\u5b9a\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u5185\u3067\u76ee\u6a19\u8981\u7d20\u3092\u8fc5\u901f\u306b\u53d6\u5f97\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002

        "},{"location":"chapter_searching/searching_algorithm_revisited/#1053","title":"10.5.3 \u00a0 \u63a2\u7d22\u65b9\u6cd5\u306e\u9078\u629e","text":"

        \u30b5\u30a4\u30ba\\(n\\)\u306e\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u7dda\u5f62\u63a2\u7d22\u3001\u4e8c\u5206\u63a2\u7d22\u3001\u6728\u63a2\u7d22\u3001\u30cf\u30c3\u30b7\u30e5\u63a2\u7d22\u3001\u307e\u305f\u306f\u305d\u306e\u4ed6\u306e\u65b9\u6cd5\u3092\u4f7f\u7528\u3057\u3066\u76ee\u6a19\u8981\u7d20\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u65b9\u6cd5\u306e\u52d5\u4f5c\u539f\u7406\u3092\u4e0b\u56f3\u306b\u793a\u3057\u307e\u3059\u3002

        \u56f3 10-11 \u00a0 Various search strategies

        \u524d\u8ff0\u306e\u65b9\u6cd5\u306e\u7279\u6027\u3068\u64cd\u4f5c\u52b9\u7387\u3092\u4ee5\u4e0b\u306e\u8868\u306b\u793a\u3057\u307e\u3059\u3002

        \u8868 10-1 \u00a0 \u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u52b9\u7387\u306e\u6bd4\u8f03

        \u7dda\u5f62\u63a2\u7d22 \u4e8c\u5206\u63a2\u7d22 \u6728\u63a2\u7d22 \u30cf\u30c3\u30b7\u30e5\u63a2\u7d22 \u8981\u7d20\u63a2\u7d22 \\(O(n)\\) \\(O(\\log n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u8981\u7d20\u633f\u5165 \\(O(1)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u8981\u7d20\u524a\u9664 \\(O(n)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u8ffd\u52a0\u7a7a\u9593 \\(O(1)\\) \\(O(1)\\) \\(O(n)\\) \\(O(n)\\) \u30c7\u30fc\u30bf\u524d\u51e6\u7406 / \u30bd\u30fc\u30c8 \\(O(n \\log n)\\) \u6728\u69cb\u7bc9 \\(O(n \\log n)\\) \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u69cb\u7bc9 \\(O(n)\\) \u30c7\u30fc\u30bf\u9806\u5e8f\u6027 \u7121\u9806\u5e8f \u9806\u5e8f \u9806\u5e8f \u7121\u9806\u5e8f

        \u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u9078\u629e\u306f\u3001\u30c7\u30fc\u30bf\u91cf\u3001\u63a2\u7d22\u6027\u80fd\u8981\u4ef6\u3001\u30c7\u30fc\u30bf\u30af\u30a8\u30ea\u3068\u66f4\u65b0\u306e\u983b\u5ea6\u306a\u3069\u306b\u3082\u4f9d\u5b58\u3057\u307e\u3059\u3002

        \u7dda\u5f62\u63a2\u7d22

        • \u6c4e\u7528\u6027\u304c\u826f\u304f\u3001\u30c7\u30fc\u30bf\u524d\u51e6\u7406\u64cd\u4f5c\u304c\u4e0d\u8981\u3067\u3059\u3002\u30c7\u30fc\u30bf\u3092\u4e00\u5ea6\u3060\u3051\u30af\u30a8\u30ea\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u3001\u4ed6\u306e3\u3064\u306e\u65b9\u6cd5\u306e\u30c7\u30fc\u30bf\u524d\u51e6\u7406\u6642\u9593\u306f\u7dda\u5f62\u63a2\u7d22\u306e\u6642\u9593\u3088\u308a\u3082\u9577\u304f\u306a\u308a\u307e\u3059\u3002
        • \u5c0f\u5bb9\u91cf\u306e\u30c7\u30fc\u30bf\u306b\u9069\u3057\u3066\u304a\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u304c\u52b9\u7387\u306b\u4e0e\u3048\u308b\u5f71\u97ff\u306f\u5c0f\u3055\u3044\u3067\u3059\u3002
        • \u30c7\u30fc\u30bf\u66f4\u65b0\u304c\u975e\u5e38\u306b\u983b\u7e41\u306a\u30b7\u30ca\u30ea\u30aa\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u65b9\u6cd5\u306f\u30c7\u30fc\u30bf\u306e\u8ffd\u52a0\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u3092\u5fc5\u8981\u3068\u3057\u306a\u3044\u305f\u3081\u3067\u3059\u3002

        \u4e8c\u5206\u63a2\u7d22

        • \u3088\u308a\u5927\u304d\u306a\u30c7\u30fc\u30bf\u91cf\u306b\u9069\u3057\u3066\u304a\u308a\u3001\u5b89\u5b9a\u3057\u305f\u6027\u80fd\u3068\u6700\u60aa\u30b1\u30fc\u30b9\u306e\u6642\u9593\u8a08\u7b97\u91cf\\(O(\\log n)\\)\u3092\u6301\u3061\u307e\u3059\u3002
        • \u305f\u3060\u3057\u3001\u30c7\u30fc\u30bf\u91cf\u304c\u5927\u304d\u3059\u304e\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002\u914d\u5217\u306e\u4fdd\u5b58\u306b\u306f\u9023\u7d9a\u3057\u305f\u30e1\u30e2\u30ea\u7a7a\u9593\u304c\u5fc5\u8981\u3060\u304b\u3089\u3067\u3059\u3002
        • \u983b\u7e41\u306a\u8ffd\u52a0\u3068\u524a\u9664\u304c\u3042\u308b\u30b7\u30ca\u30ea\u30aa\u306b\u306f\u9069\u3057\u3066\u3044\u307e\u305b\u3093\u3002\u9806\u5e8f\u4ed8\u304d\u914d\u5217\u306e\u7dad\u6301\u306b\u591a\u304f\u306e\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u304c\u767a\u751f\u3059\u308b\u305f\u3081\u3067\u3059\u3002

        \u30cf\u30c3\u30b7\u30e5\u63a2\u7d22

        • \u9ad8\u901f\u30af\u30a8\u30ea\u6027\u80fd\u304c\u4e0d\u53ef\u6b20\u306a\u30b7\u30ca\u30ea\u30aa\u306b\u9069\u3057\u3066\u304a\u308a\u3001\u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u3067\u3059\u3002
        • \u9806\u5e8f\u4ed8\u304d\u30c7\u30fc\u30bf\u3084\u7bc4\u56f2\u63a2\u7d22\u304c\u5fc5\u8981\u306a\u30b7\u30ca\u30ea\u30aa\u306b\u306f\u9069\u3057\u3066\u3044\u307e\u305b\u3093\u3002\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306f\u30c7\u30fc\u30bf\u306e\u9806\u5e8f\u6027\u3092\u7dad\u6301\u3067\u304d\u306a\u3044\u305f\u3081\u3067\u3059\u3002
        • \u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u3068\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u51e6\u7406\u6226\u7565\u3078\u306e\u4f9d\u5b58\u5ea6\u304c\u9ad8\u304f\u3001\u6027\u80fd\u52a3\u5316\u306e\u30ea\u30b9\u30af\u304c\u5927\u304d\u3044\u3067\u3059\u3002
        • \u904e\u5ea6\u306b\u5927\u5bb9\u91cf\u306e\u30c7\u30fc\u30bf\u306b\u306f\u9069\u3057\u3066\u3044\u307e\u305b\u3093\u3002\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306f\u885d\u7a81\u3092\u6700\u5c0f\u5316\u3057\u3001\u826f\u597d\u306a\u30af\u30a8\u30ea\u6027\u80fd\u3092\u63d0\u4f9b\u3059\u308b\u305f\u3081\u306b\u8ffd\u52a0\u306e\u7a7a\u9593\u304c\u5fc5\u8981\u3060\u304b\u3089\u3067\u3059\u3002

        \u6728\u63a2\u7d22

        • \u5927\u5bb9\u91cf\u30c7\u30fc\u30bf\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u6728\u30ce\u30fc\u30c9\u306f\u30e1\u30e2\u30ea\u5185\u306b\u5206\u6563\u3057\u3066\u4fdd\u5b58\u3055\u308c\u308b\u305f\u3081\u3067\u3059\u3002
        • \u9806\u5e8f\u4ed8\u304d\u30c7\u30fc\u30bf\u306e\u7dad\u6301\u3084\u7bc4\u56f2\u63a2\u7d22\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002
        • \u30ce\u30fc\u30c9\u306e\u7d99\u7d9a\u7684\u306a\u8ffd\u52a0\u3068\u524a\u9664\u306b\u3088\u308a\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u306f\u504f\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(n)\\)\u306b\u52a3\u5316\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        • AVL\u6728\u3084\u8d64\u9ed2\u6728\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001\u64cd\u4f5c\u306f\\(O(\\log n)\\)\u52b9\u7387\u3067\u5b89\u5b9a\u3057\u3066\u5b9f\u884c\u3067\u304d\u307e\u3059\u304c\u3001\u6728\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u7dad\u6301\u3059\u308b\u64cd\u4f5c\u306b\u3088\u308a\u8ffd\u52a0\u306e\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u304c\u8ffd\u52a0\u3055\u308c\u307e\u3059\u3002
        "},{"location":"chapter_searching/summary/","title":"10.6 \u00a0 \u307e\u3068\u3081","text":"
        • \u4e8c\u5206\u63a2\u7d22\u306f\u30c7\u30fc\u30bf\u306e\u9806\u5e8f\u306b\u4f9d\u5b58\u3057\u3001\u63a2\u7d22\u533a\u9593\u3092\u53cd\u5fa9\u7684\u306b\u534a\u5206\u306b\u3059\u308b\u3053\u3068\u3067\u63a2\u7d22\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002\u5165\u529b\u30c7\u30fc\u30bf\u304c\u30bd\u30fc\u30c8\u6e08\u307f\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u914d\u5217\u307e\u305f\u306f\u914d\u5217\u30d9\u30fc\u30b9\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u306e\u307f\u9069\u7528\u53ef\u80fd\u3067\u3059\u3002
        • \u7121\u9806\u5e8f\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u5185\u306e\u30a8\u30f3\u30c8\u30ea\u3092\u898b\u3064\u3051\u308b\u306b\u306f\u3001\u7dcf\u5f53\u305f\u308a\u63a2\u7d22\u304c\u5fc5\u8981\u306a\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u57fa\u3065\u3044\u3066\u7570\u306a\u308b\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u9069\u7528\u3067\u304d\u307e\u3059\uff1a\u7dda\u5f62\u63a2\u7d22\u306f\u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u9069\u3057\u3066\u304a\u308a\u3001\u5e45\u512a\u5148\u63a2\u7d22\uff08BFS\uff09\u3068\u6df1\u3055\u512a\u5148\u63a2\u7d22\uff08DFS\uff09\u306f\u30b0\u30e9\u30d5\u3068\u6728\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u975e\u5e38\u306b\u6c4e\u7528\u6027\u304c\u9ad8\u304f\u3001\u30c7\u30fc\u30bf\u306e\u524d\u51e6\u7406\u304c\u4e0d\u8981\u3067\u3059\u304c\u3001\\(O(n)\\)\u3068\u3044\u3046\u9ad8\u3044\u6642\u9593\u8a08\u7b97\u91cf\u3092\u6301\u3061\u307e\u3059\u3002
        • \u30cf\u30c3\u30b7\u30e5\u63a2\u7d22\u3001\u6728\u63a2\u7d22\u3001\u4e8c\u5206\u63a2\u7d22\u306f\u52b9\u7387\u7684\u306a\u63a2\u7d22\u65b9\u6cd5\u3067\u3001\u7279\u5b9a\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u5185\u3067\u76ee\u6a19\u8981\u7d20\u3092\u8fc5\u901f\u306b\u7279\u5b9a\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3001\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(\\log n)\\)\u307e\u305f\u306f\\(O(1)\\)\u306b\u307e\u3067\u9054\u3057\u307e\u3059\u304c\u3001\u901a\u5e38\u306f\u8ffd\u52a0\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u53ce\u5bb9\u3059\u308b\u305f\u3081\u306b\u8ffd\u52a0\u306e\u7a7a\u9593\u304c\u5fc5\u8981\u3067\u3059\u3002
        • \u5b9f\u969b\u306b\u306f\u3001\u30c7\u30fc\u30bf\u91cf\u3001\u63a2\u7d22\u6027\u80fd\u8981\u4ef6\u3001\u30c7\u30fc\u30bf\u30af\u30a8\u30ea\u3068\u66f4\u65b0\u983b\u5ea6\u306a\u3069\u306e\u8981\u56e0\u3092\u5206\u6790\u3057\u3066\u3001\u9069\u5207\u306a\u63a2\u7d22\u65b9\u6cd5\u3092\u9078\u629e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u7dda\u5f62\u63a2\u7d22\u306f\u5c0f\u3055\u306a\u30c7\u30fc\u30bf\u3084\u983b\u7e41\u306b\u66f4\u65b0\u3055\u308c\u308b\uff08\u5909\u52d5\u6027\u306e\u9ad8\u3044\uff09\u30c7\u30fc\u30bf\u306b\u7406\u60f3\u7684\u3067\u3059\u3002\u4e8c\u5206\u63a2\u7d22\u306f\u5927\u304d\u304f\u3066\u30bd\u30fc\u30c8\u6e08\u307f\u306e\u30c7\u30fc\u30bf\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u63a2\u7d22\u306f\u9ad8\u3044\u30af\u30a8\u30ea\u52b9\u7387\u304c\u5fc5\u8981\u3067\u7bc4\u56f2\u30af\u30a8\u30ea\u304c\u4e0d\u8981\u306a\u30c7\u30fc\u30bf\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u6728\u63a2\u7d22\u306f\u9806\u5e8f\u3092\u7dad\u6301\u3057\u3001\u7bc4\u56f2\u30af\u30a8\u30ea\u3092\u30b5\u30dd\u30fc\u30c8\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5927\u304d\u306a\u52d5\u7684\u30c7\u30fc\u30bf\u306b\u6700\u3082\u9069\u3057\u3066\u3044\u307e\u3059\u3002
        • \u7dda\u5f62\u63a2\u7d22\u3092\u30cf\u30c3\u30b7\u30e5\u63a2\u7d22\u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u306f\u3001\u5b9f\u884c\u6642\u6027\u80fd\u3092\u6700\u9069\u5316\u3059\u308b\u4e00\u822c\u7684\u306a\u6226\u7565\u3067\u3001\u6642\u9593\u8a08\u7b97\u91cf\u3092\\(O(n)\\)\u304b\u3089\\(O(1)\\)\u306b\u524a\u6e1b\u3057\u307e\u3059\u3002
        "},{"location":"chapter_sorting/","title":"\u7b2c 11 \u7ae0 \u00a0 \u30bd\u30fc\u30c8","text":"

        Abstract

        \u30bd\u30fc\u30c8\u306f\u6df7\u6c8c\u3092\u79e9\u5e8f\u306b\u5909\u3048\u308b\u9b54\u6cd5\u306e\u9375\u306e\u3088\u3046\u306a\u3082\u306e\u3067\u3001\u30c7\u30fc\u30bf\u3092\u3088\u308a\u52b9\u7387\u7684\u306b\u7406\u89e3\u3057\u51e6\u7406\u3059\u308b\u3053\u3068\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002

        \u5358\u7d14\u306a\u6607\u9806\u3067\u3042\u308d\u3046\u3068\u8907\u96d1\u306a\u30ab\u30c6\u30b4\u30ea\u914d\u5217\u3067\u3042\u308d\u3046\u3068\u3001\u30bd\u30fc\u30c8\u306f\u30c7\u30fc\u30bf\u306e\u8abf\u548c\u7f8e\u3092\u660e\u3089\u304b\u306b\u3057\u307e\u3059\u3002

        "},{"location":"chapter_sorting/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 11.1 \u00a0 \u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0
        • 11.2 \u00a0 \u9078\u629e\u30bd\u30fc\u30c8
        • 11.3 \u00a0 \u30d0\u30d6\u30eb\u30bd\u30fc\u30c8
        • 11.4 \u00a0 \u633f\u5165\u30bd\u30fc\u30c8
        • 11.5 \u00a0 \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8
        • 11.6 \u00a0 \u30de\u30fc\u30b8\u30bd\u30fc\u30c8
        • 11.7 \u00a0 \u30d2\u30fc\u30d7\u30bd\u30fc\u30c8
        • 11.8 \u00a0 \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8
        • 11.9 \u00a0 \u8a08\u6570\u30bd\u30fc\u30c8
        • 11.10 \u00a0 \u57fa\u6570\u30bd\u30fc\u30c8
        • 11.11 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_sorting/bubble_sort/","title":"11.3 \u00a0 \u30d0\u30d6\u30eb\u30bd\u30fc\u30c8","text":"

        \u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u306f\u3001\u96a3\u63a5\u3059\u308b\u8981\u7d20\u3092\u7d99\u7d9a\u7684\u306b\u6bd4\u8f03\u3057\u4ea4\u63db\u3059\u308b\u3053\u3068\u3067\u52d5\u4f5c\u3057\u307e\u3059\u3002\u3053\u306e\u30d7\u30ed\u30bb\u30b9\u306f\u6ce1\u304c\u5e95\u304b\u3089\u4e0a\u306b\u4e0a\u6607\u3059\u308b\u3088\u3046\u306a\u3082\u306e\u306a\u306e\u3067\u3001\u300c\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u300d\u3068\u540d\u4ed8\u3051\u3089\u308c\u307e\u3057\u305f\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30d0\u30d6\u30ea\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9\u306f\u8981\u7d20\u4ea4\u63db\u3092\u4f7f\u7528\u3057\u3066\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\u3067\u304d\u307e\u3059\uff1a\u914d\u5217\u306e\u5de6\u7aef\u304b\u3089\u958b\u59cb\u3057\u3066\u53f3\u306b\u79fb\u52d5\u3057\u3001\u96a3\u63a5\u3059\u308b\u8981\u7d20\u306e\u5404\u30da\u30a2\u3092\u6bd4\u8f03\u3057\u307e\u3059\u3002\u5de6\u306e\u8981\u7d20\u304c\u53f3\u306e\u8981\u7d20\u3088\u308a\u5927\u304d\u3044\u5834\u5408\u306f\u3001\u305d\u308c\u3089\u3092\u4ea4\u63db\u3057\u307e\u3059\u3002\u6a2a\u65ad\u5f8c\u3001\u6700\u5927\u8981\u7d20\u306f\u914d\u5217\u306e\u53f3\u7aef\u306b\u30d0\u30d6\u30eb\u30a2\u30c3\u30d7\u3057\u307e\u3059\u3002

        <1><2><3><4><5><6><7>

        \u56f3 11-4 \u00a0 Simulating bubble process using element swap

        "},{"location":"chapter_sorting/bubble_sort/#1131","title":"11.3.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d7\u30ed\u30bb\u30b9","text":"

        \u914d\u5217\u306e\u9577\u3055\u3092\\(n\\)\u3068\u3057\u307e\u3059\u3002\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u306e\u30b9\u30c6\u30c3\u30d7\u306f\u4e0b\u56f3\u306b\u793a\u3055\u308c\u307e\u3059\uff1a

        1. \u307e\u305a\u3001\\(n\\)\u500b\u306e\u8981\u7d20\u306b\u5bfe\u3057\u30661\u56de\u306e\u300c\u30d0\u30d6\u30eb\u300d\u30d1\u30b9\u3092\u5b9f\u884c\u3057\u3001\u6700\u5927\u8981\u7d20\u3092\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u4ea4\u63db\u3057\u307e\u3059\u3002
        2. \u6b21\u306b\u3001\u6b8b\u308a\u306e\\(n - 1\\)\u500b\u306e\u8981\u7d20\u306b\u5bfe\u3057\u3066\u300c\u30d0\u30d6\u30eb\u300d\u30d1\u30b9\u3092\u5b9f\u884c\u3057\u30012\u756a\u76ee\u306b\u5927\u304d\u3044\u8981\u7d20\u3092\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u4ea4\u63db\u3057\u307e\u3059\u3002
        3. \u3053\u306e\u65b9\u6cd5\u3067\u7d9a\u884c\u3057\u307e\u3059\uff1b\\(n - 1\\)\u56de\u306e\u30d1\u30b9\u306e\u5f8c\u3001\u6700\u5927\\(n - 1\\)\u500b\u306e\u8981\u7d20\u304c\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u79fb\u52d5\u3055\u308c\u307e\u3059\u3002
        4. \u6b8b\u308a\u306e\u552f\u4e00\u306e\u8981\u7d20\u306f**\u5fc5\u305a**\u6700\u5c0f\u3067\u3042\u308b\u305f\u3081\u3001**\u3055\u3089\u306a\u308b**\u30bd\u30fc\u30c8\u306f\u5fc5\u8981\u3042\u308a\u307e\u305b\u3093\u3002\u3053\u306e\u6642\u70b9\u3067\u3001\u914d\u5217\u306f\u30bd\u30fc\u30c8\u3055\u308c\u307e\u3059\u3002

        \u56f3 11-5 \u00a0 Bubble sort process

        \u30b3\u30fc\u30c9\u4f8b\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby bubble_sort.py
        def bubble_sort(nums: list[int]):\n    \"\"\"\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\"\"\"\n    n = len(nums)\n    # \u5916\u5074\u306e\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f [0, i]\n    for i in range(n - 1, 0, -1):\n        # \u5185\u5074\u306e\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2 [0, i] \u306e\u6700\u5927\u8981\u7d20\u3092\u7bc4\u56f2\u306e\u53f3\u7aef\u306b\u79fb\u52d5\n        for j in range(i):\n            if nums[j] > nums[j + 1]:\n                # nums[j] \u3068 nums[j + 1] \u3092\u4ea4\u63db\n                nums[j], nums[j + 1] = nums[j + 1], nums[j]\n
        bubble_sort.cpp
        /* \u30d0\u30d6\u30eb\u30bd\u30fc\u30c8 */\nvoid bubbleSort(vector<int> &nums) {\n    // \u5916\u5074\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f[0, i]\n    for (int i = nums.size() - 1; i > 0; i--) {\n        // \u5185\u5074\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2[0, i]\u5185\u306e\u6700\u5927\u8981\u7d20\u3092\u7bc4\u56f2\u306e\u53f3\u7aef\u306b\u4ea4\u63db\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // nums[j]\u3068nums[j + 1]\u3092\u4ea4\u63db\n                // \u3053\u3053\u3067\u306fstd\u306eswap\u3092\u4f7f\u7528\n                swap(nums[j], nums[j + 1]);\n            }\n        }\n    }\n}\n
        bubble_sort.java
        /* \u30d0\u30d6\u30eb\u30bd\u30fc\u30c8 */\nvoid bubbleSort(int[] nums) {\n    // \u5916\u5074\u30eb\u30fc\u30d7: \u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f [0, i]\n    for (int i = nums.length - 1; i > 0; i--) {\n        // \u5185\u5074\u30eb\u30fc\u30d7: \u672a\u30bd\u30fc\u30c8\u7bc4\u56f2 [0, i] \u306e\u6700\u5927\u8981\u7d20\u3092\u7bc4\u56f2\u306e\u53f3\u7aef\u306b\u4ea4\u63db\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // nums[j] \u3068 nums[j + 1] \u3092\u4ea4\u63db\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n            }\n        }\n    }\n}\n
        bubble_sort.cs
        [class]{bubble_sort}-[func]{BubbleSort}\n
        bubble_sort.go
        [class]{}-[func]{bubbleSort}\n
        bubble_sort.swift
        [class]{}-[func]{bubbleSort}\n
        bubble_sort.js
        [class]{}-[func]{bubbleSort}\n
        bubble_sort.ts
        [class]{}-[func]{bubbleSort}\n
        bubble_sort.dart
        [class]{}-[func]{bubbleSort}\n
        bubble_sort.rs
        [class]{}-[func]{bubble_sort}\n
        bubble_sort.c
        [class]{}-[func]{bubbleSort}\n
        bubble_sort.kt
        [class]{}-[func]{bubbleSort}\n
        bubble_sort.rb
        [class]{}-[func]{bubble_sort}\n
        "},{"location":"chapter_sorting/bubble_sort/#1132","title":"11.3.2 \u00a0 \u52b9\u7387\u306e\u6700\u9069\u5316","text":"

        \u300c\u30d0\u30d6\u30ea\u30f3\u30b0\u300d\u306e\u30e9\u30a6\u30f3\u30c9\u4e2d\u306b\u4ea4\u63db\u304c\u767a\u751f\u3057\u306a\u3044\u5834\u5408\u3001\u914d\u5217\u306f\u3059\u3067\u306b\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u3059\u3050\u306b\u623b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u3092\u691c\u51fa\u3059\u308b\u305f\u3081\u306b\u3001flag\u5909\u6570\u3092\u8ffd\u52a0\u3067\u304d\u307e\u3059\uff1b\u30d1\u30b9\u3067\u4ea4\u63db\u304c\u884c\u308f\u308c\u306a\u3044\u5834\u5408\u306f\u3001\u30d5\u30e9\u30b0\u3092\u8a2d\u5b9a\u3057\u3066\u65e9\u671f\u306b\u623b\u308a\u307e\u3059\u3002

        \u3053\u306e\u6700\u9069\u5316\u304c\u3042\u3063\u3066\u3082\u3001\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u306e\u6700\u60aa\u6642\u9593\u8a08\u7b97\u91cf\u3068\u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n^2)\\)\u306e\u307e\u307e\u3067\u3059\u3002\u305f\u3060\u3057\u3001\u5165\u529b\u914d\u5217\u304c\u3059\u3067\u306b\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u6700\u826f\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u307e\u3067\u4f4e\u304f\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby bubble_sort.py
        def bubble_sort_with_flag(nums: list[int]):\n    \"\"\"\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\uff08\u30d5\u30e9\u30b0\u306b\u3088\u308b\u6700\u9069\u5316\uff09\"\"\"\n    n = len(nums)\n    # \u5916\u5074\u306e\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f [0, i]\n    for i in range(n - 1, 0, -1):\n        flag = False  # \u30d5\u30e9\u30b0\u3092\u521d\u671f\u5316\n        # \u5185\u5074\u306e\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2 [0, i] \u306e\u6700\u5927\u8981\u7d20\u3092\u7bc4\u56f2\u306e\u53f3\u7aef\u306b\u79fb\u52d5\n        for j in range(i):\n            if nums[j] > nums[j + 1]:\n                # nums[j] \u3068 nums[j + 1] \u3092\u4ea4\u63db\n                nums[j], nums[j + 1] = nums[j + 1], nums[j]\n                flag = True  # \u8981\u7d20\u3092\u4ea4\u63db\u3057\u305f\u3053\u3068\u3092\u8a18\u9332\n        if not flag:\n            break  # \u3053\u306e\u56de\u306e\u300c\u30d0\u30d6\u30ea\u30f3\u30b0\u300d\u3067\u8981\u7d20\u304c\u4ea4\u63db\u3055\u308c\u306a\u304b\u3063\u305f\u5834\u5408\u3001\u7d42\u4e86\n
        bubble_sort.cpp
        /* \u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\uff08\u30d5\u30e9\u30b0\u6700\u9069\u5316\u7248\uff09*/\nvoid bubbleSortWithFlag(vector<int> &nums) {\n    // \u5916\u5074\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f[0, i]\n    for (int i = nums.size() - 1; i > 0; i--) {\n        bool flag = false; // \u30d5\u30e9\u30b0\u3092\u521d\u671f\u5316\n        // \u5185\u5074\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2[0, i]\u5185\u306e\u6700\u5927\u8981\u7d20\u3092\u7bc4\u56f2\u306e\u53f3\u7aef\u306b\u4ea4\u63db\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // nums[j]\u3068nums[j + 1]\u3092\u4ea4\u63db\n                // \u3053\u3053\u3067\u306fstd\u306eswap\u3092\u4f7f\u7528\n                swap(nums[j], nums[j + 1]);\n                flag = true; // \u4ea4\u63db\u3055\u308c\u305f\u8981\u7d20\u3092\u8a18\u9332\n            }\n        }\n        if (!flag)\n            break; // \u3053\u306e\u56de\u306e\u300c\u30d0\u30d6\u30ea\u30f3\u30b0\u300d\u3067\u8981\u7d20\u304c\u4ea4\u63db\u3055\u308c\u306a\u304b\u3063\u305f\u5834\u5408\u3001\u7d42\u4e86\n    }\n}\n
        bubble_sort.java
        /* \u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\uff08\u30d5\u30e9\u30b0\u306b\u3088\u308b\u6700\u9069\u5316\uff09 */\nvoid bubbleSortWithFlag(int[] nums) {\n    // \u5916\u5074\u30eb\u30fc\u30d7: \u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f [0, i]\n    for (int i = nums.length - 1; i > 0; i--) {\n        boolean flag = false; // \u30d5\u30e9\u30b0\u3092\u521d\u671f\u5316\n        // \u5185\u5074\u30eb\u30fc\u30d7: \u672a\u30bd\u30fc\u30c8\u7bc4\u56f2 [0, i] \u306e\u6700\u5927\u8981\u7d20\u3092\u7bc4\u56f2\u306e\u53f3\u7aef\u306b\u4ea4\u63db\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // nums[j] \u3068 nums[j + 1] \u3092\u4ea4\u63db\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                flag = true; // \u4ea4\u63db\u3055\u308c\u305f\u8981\u7d20\u3092\u8a18\u9332\n            }\n        }\n        if (!flag)\n            break; // \u3053\u306e\u300c\u30d0\u30d6\u30ea\u30f3\u30b0\u300d\u30e9\u30a6\u30f3\u30c9\u3067\u8981\u7d20\u304c\u4ea4\u63db\u3055\u308c\u306a\u304b\u3063\u305f\u5834\u5408\u3001\u7d42\u4e86\n    }\n}\n
        bubble_sort.cs
        [class]{bubble_sort}-[func]{BubbleSortWithFlag}\n
        bubble_sort.go
        [class]{}-[func]{bubbleSortWithFlag}\n
        bubble_sort.swift
        [class]{}-[func]{bubbleSortWithFlag}\n
        bubble_sort.js
        [class]{}-[func]{bubbleSortWithFlag}\n
        bubble_sort.ts
        [class]{}-[func]{bubbleSortWithFlag}\n
        bubble_sort.dart
        [class]{}-[func]{bubbleSortWithFlag}\n
        bubble_sort.rs
        [class]{}-[func]{bubble_sort_with_flag}\n
        bubble_sort.c
        [class]{}-[func]{bubbleSortWithFlag}\n
        bubble_sort.kt
        [class]{}-[func]{bubbleSortWithFlag}\n
        bubble_sort.rb
        [class]{}-[func]{bubble_sort_with_flag}\n
        "},{"location":"chapter_sorting/bubble_sort/#1133","title":"11.3.3 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7279\u6027","text":"
        • \\(O(n^2)\\)\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3001\u9069\u5fdc\u30bd\u30fc\u30c8\u3002 \u5404\u300c\u30d0\u30d6\u30ea\u30f3\u30b0\u300d\u30e9\u30a6\u30f3\u30c9\u306f\u9577\u3055\\(n - 1\\)\u3001\\(n - 2\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\)\u306e\u914d\u5217\u30bb\u30b0\u30e1\u30f3\u30c8\u3092\u6a2a\u65ad\u3057\u3001\u5408\u8a08\u306f\\((n - 1) n / 2\\)\u3068\u306a\u308a\u307e\u3059\u3002flag\u6700\u9069\u5316\u306b\u3088\u308a\u3001\u914d\u5217\u304c\u3059\u3067\u306b\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u6700\u826f\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u306b\u9054\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        • \\(O(1)\\)\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u3001\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u30bd\u30fc\u30c8\u3002 \u30dd\u30a4\u30f3\u30bf\\(i\\)\u3068\\(j\\)\u306b\u3088\u3063\u3066\u5b9a\u6570\u91cf\u306e\u8ffd\u52a0\u7a7a\u9593\u306e\u307f\u304c\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        • \u5b89\u5b9a\u30bd\u30fc\u30c8\u3002 \u7b49\u3057\u3044\u8981\u7d20\u306f\u300c\u30d0\u30d6\u30ea\u30f3\u30b0\u300d\u4e2d\u306b\u4ea4\u63db\u3055\u308c\u306a\u3044\u305f\u3081\u3001\u5143\u306e\u9806\u5e8f\u304c\u4fdd\u6301\u3055\u308c\u3001\u3053\u308c\u306f\u5b89\u5b9a\u30bd\u30fc\u30c8\u306b\u306a\u308a\u307e\u3059\u3002
        "},{"location":"chapter_sorting/bucket_sort/","title":"11.8 \u00a0 \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8","text":"

        \u524d\u8ff0\u306e\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3059\u3079\u3066\u300c\u6bd4\u8f03\u30d9\u30fc\u30b9\u306e\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u300d\u3067\u3001\u5024\u3092\u6bd4\u8f03\u3059\u308b\u3053\u3068\u3067\u8981\u7d20\u3092\u30bd\u30fc\u30c8\u3057\u307e\u3059\u3002\u3053\u306e\u3088\u3046\u306a\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f \\(O(n \\log n)\\) \u3088\u308a\u826f\u3044\u6642\u9593\u8a08\u7b97\u91cf\u3092\u6301\u3064\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002\u6b21\u306b\u3001\u7dda\u5f62\u6642\u9593\u8a08\u7b97\u91cf\u3092\u9054\u6210\u3067\u304d\u308b\u3044\u304f\u3064\u304b\u306e\u300c\u975e\u6bd4\u8f03\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u300d\u306b\u3064\u3044\u3066\u8b70\u8ad6\u3057\u307e\u3059\u3002

        \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306f\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306e\u5178\u578b\u7684\u306a\u5fdc\u7528\u3067\u3059\u3002\u4e00\u9023\u306e\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u30d0\u30b1\u30c3\u30c8\u3092\u8a2d\u5b9a\u3057\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u304c\u30c7\u30fc\u30bf\u306e\u7bc4\u56f2\u3092\u542b\u307f\u3001\u5165\u529b\u30c7\u30fc\u30bf\u3092\u3053\u308c\u3089\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u5747\u7b49\u306b\u5206\u6563\u3055\u305b\u308b\u3053\u3068\u3067\u52d5\u4f5c\u3057\u307e\u3059\u3002\u305d\u3057\u3066\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u5185\u306e\u30c7\u30fc\u30bf\u3092\u500b\u5225\u306b\u30bd\u30fc\u30c8\u3057\u307e\u3059\u3002\u6700\u5f8c\u306b\u3001\u3059\u3079\u3066\u306e\u30d0\u30b1\u30c3\u30c8\u304b\u3089\u306e\u30bd\u30fc\u30c8\u6e08\u307f\u30c7\u30fc\u30bf\u3092\u9806\u6b21\u30de\u30fc\u30b8\u3057\u3066\u6700\u7d42\u7d50\u679c\u3092\u751f\u6210\u3057\u307e\u3059\u3002

        "},{"location":"chapter_sorting/bucket_sort/#1181","title":"11.8.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u904e\u7a0b","text":"

        \u9577\u3055 \\(n\\) \u306e\u914d\u5217\u3067\u3001\\([0, 1)\\) \u306e\u7bc4\u56f2\u306e\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u3092\u8003\u3048\u3066\u307f\u307e\u3059\u3002\u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306e\u904e\u7a0b\u306f\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        1. \\(k\\) \u500b\u306e\u30d0\u30b1\u30c3\u30c8\u3092\u521d\u671f\u5316\u3057\u3001\\(n\\) \u500b\u306e\u8981\u7d20\u3092\u3053\u308c\u3089\u306e \\(k\\) \u500b\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u6563\u3055\u305b\u307e\u3059\u3002
        2. \u5404\u30d0\u30b1\u30c3\u30c8\u3092\u500b\u5225\u306b\u30bd\u30fc\u30c8\u3057\u307e\u3059\uff08\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u7d44\u307f\u8fbc\u307f\u30bd\u30fc\u30c8\u95a2\u6570\u3092\u4f7f\u7528\uff09\u3002
        3. \u6700\u5c0f\u304b\u3089\u6700\u5927\u306e\u30d0\u30b1\u30c3\u30c8\u306e\u9806\u5e8f\u3067\u7d50\u679c\u3092\u30de\u30fc\u30b8\u3057\u307e\u3059\u3002

        \u56f3 11-13 \u00a0 \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u904e\u7a0b

        \u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby bucket_sort.py
        def bucket_sort(nums: list[float]):\n    \"\"\"\u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\"\"\"\n    # k = n/2 \u500b\u306e\u30d0\u30b1\u30c3\u30c8\u3092\u521d\u671f\u5316\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u306b\u5e73\u57472\u500b\u306e\u8981\u7d20\u3092\u914d\u7f6e\u3059\u308b\u3053\u3068\u3092\u671f\u5f85\n    k = len(nums) // 2\n    buckets = [[] for _ in range(k)]\n    # 1. \u914d\u5217\u8981\u7d20\u3092\u5404\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u6563\n    for num in nums:\n        # \u5165\u529b\u30c7\u30fc\u30bf\u7bc4\u56f2\u306f [0, 1)\u3001num * k \u3092\u4f7f\u7528\u3057\u3066\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2 [0, k-1] \u306b\u30de\u30c3\u30d4\u30f3\u30b0\n        i = int(num * k)\n        # num \u3092\u30d0\u30b1\u30c3\u30c8 i \u306b\u8ffd\u52a0\n        buckets[i].append(num)\n    # 2. \u5404\u30d0\u30b1\u30c3\u30c8\u3092\u30bd\u30fc\u30c8\n    for bucket in buckets:\n        # \u7d44\u307f\u8fbc\u307f\u30bd\u30fc\u30c8\u95a2\u6570\u3092\u4f7f\u7528\u3001\u4ed6\u306e\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u3082\u53ef\u80fd\n        bucket.sort()\n    # 3. \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3057\u3066\u7d50\u679c\u3092\u30de\u30fc\u30b8\n    i = 0\n    for bucket in buckets:\n        for num in bucket:\n            nums[i] = num\n            i += 1\n
        bucket_sort.cpp
        /* \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8 */\nvoid bucketSort(vector<float> &nums) {\n    // k = n/2\u500b\u306e\u30d0\u30b1\u30c3\u30c8\u3092\u521d\u671f\u5316\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u306b2\u3064\u306e\u8981\u7d20\u3092\u5272\u308a\u5f53\u3066\u308b\u3053\u3068\u3092\u671f\u5f85\n    int k = nums.size() / 2;\n    vector<vector<float>> buckets(k);\n    // 1. \u914d\u5217\u8981\u7d20\u3092\u5404\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u914d\n    for (float num : nums) {\n        // \u5165\u529b\u30c7\u30fc\u30bf\u7bc4\u56f2\u306f[0, 1)\u3001num * k\u3092\u4f7f\u7528\u3057\u3066\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2[0, k-1]\u306b\u30de\u30c3\u30d7\n        int i = num * k;\n        // bucket_idx\u30d0\u30b1\u30c3\u30c8\u306b\u6570\u5024\u3092\u8ffd\u52a0\n        buckets[i].push_back(num);\n    }\n    // 2. \u5404\u30d0\u30b1\u30c3\u30c8\u3092\u30bd\u30fc\u30c8\n    for (vector<float> &bucket : buckets) {\n        // \u7d44\u307f\u8fbc\u307f\u30bd\u30fc\u30c8\u95a2\u6570\u3092\u4f7f\u7528\u3001\u4ed6\u306e\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u3082\u53ef\u80fd\n        sort(bucket.begin(), bucket.end());\n    }\n    // 3. \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3057\u3066\u7d50\u679c\u3092\u30de\u30fc\u30b8\n    int i = 0;\n    for (vector<float> &bucket : buckets) {\n        for (float num : bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
        bucket_sort.java
        /* \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8 */\nvoid bucketSort(float[] nums) {\n    // k = n/2 \u500b\u306e\u30d0\u30b1\u30c3\u30c8\u3092\u521d\u671f\u5316\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u306b\u671f\u5f85\u3055\u308c\u308b\u8981\u7d20\u6570\u306f 2 \u500b\n    int k = nums.length / 2;\n    List<List<Float>> buckets = new ArrayList<>();\n    for (int i = 0; i < k; i++) {\n        buckets.add(new ArrayList<>());\n    }\n    // 1. \u914d\u5217\u8981\u7d20\u3092\u5404\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u6563\n    for (float num : nums) {\n        // \u5165\u529b\u30c7\u30fc\u30bf\u7bc4\u56f2\u306f [0, 1)\u3001num * k \u3092\u4f7f\u3063\u3066\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2 [0, k-1] \u306b\u30de\u30c3\u30d4\u30f3\u30b0\n        int i = (int) (num * k);\n        // num \u3092\u30d0\u30b1\u30c3\u30c8 i \u306b\u8ffd\u52a0\n        buckets.get(i).add(num);\n    }\n    // 2. \u5404\u30d0\u30b1\u30c3\u30c8\u3092\u30bd\u30fc\u30c8\n    for (List<Float> bucket : buckets) {\n        // \u7d44\u307f\u8fbc\u307f\u30bd\u30fc\u30c8\u95a2\u6570\u3092\u4f7f\u7528\u3001\u4ed6\u306e\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u3082\u53ef\u80fd\n        Collections.sort(bucket);\n    }\n    // 3. \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3057\u3066\u7d50\u679c\u3092\u30de\u30fc\u30b8\n    int i = 0;\n    for (List<Float> bucket : buckets) {\n        for (float num : bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
        bucket_sort.cs
        [class]{bucket_sort}-[func]{BucketSort}\n
        bucket_sort.go
        [class]{}-[func]{bucketSort}\n
        bucket_sort.swift
        [class]{}-[func]{bucketSort}\n
        bucket_sort.js
        [class]{}-[func]{bucketSort}\n
        bucket_sort.ts
        [class]{}-[func]{bucketSort}\n
        bucket_sort.dart
        [class]{}-[func]{bucketSort}\n
        bucket_sort.rs
        [class]{}-[func]{bucket_sort}\n
        bucket_sort.c
        [class]{}-[func]{bucketSort}\n
        bucket_sort.kt
        [class]{}-[func]{bucketSort}\n
        bucket_sort.rb
        [class]{}-[func]{bucket_sort}\n
        "},{"location":"chapter_sorting/bucket_sort/#1182","title":"11.8.2 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7279\u5fb4","text":"

        \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306f\u975e\u5e38\u306b\u5927\u304d\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306e\u51e6\u7406\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u5165\u529b\u30c7\u30fc\u30bf\u306b100\u4e07\u500b\u306e\u8981\u7d20\u304c\u542b\u307e\u308c\u3001\u30b7\u30b9\u30c6\u30e0\u30e1\u30e2\u30ea\u306e\u5236\u9650\u306b\u3088\u308a\u3059\u3079\u3066\u306e\u30c7\u30fc\u30bf\u3092\u540c\u6642\u306b\u30ed\u30fc\u30c9\u3067\u304d\u306a\u3044\u5834\u5408\u3001\u30c7\u30fc\u30bf\u30921,000\u500b\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u5272\u3057\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u3092\u500b\u5225\u306b\u30bd\u30fc\u30c8\u3057\u3066\u304b\u3089\u7d50\u679c\u3092\u30de\u30fc\u30b8\u3067\u304d\u307e\u3059\u3002

        • \u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n + k)\\)\uff1a\u8981\u7d20\u304c\u30d0\u30b1\u30c3\u30c8\u9593\u3067\u5747\u7b49\u306b\u5206\u6563\u3055\u308c\u3066\u3044\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u5185\u306e\u8981\u7d20\u6570\u306f \\(n/k\\) \u3067\u3059\u3002\u5358\u4e00\u306e\u30d0\u30b1\u30c3\u30c8\u306e\u30bd\u30fc\u30c8\u306b \\(O(n/k \\log(n/k))\\) \u6642\u9593\u304c\u304b\u304b\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u3059\u3079\u3066\u306e\u30d0\u30b1\u30c3\u30c8\u306e\u30bd\u30fc\u30c8\u306b \\(O(n \\log(n/k))\\) \u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002\u30d0\u30b1\u30c3\u30c8\u6570 \\(k\\) \u304c\u6bd4\u8f03\u7684\u5927\u304d\u3044\u3068\u304d\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u306b\u8fd1\u3065\u304d\u307e\u3059\u3002\u7d50\u679c\u306e\u30de\u30fc\u30b8\u306b\u306f\u3001\u3059\u3079\u3066\u306e\u30d0\u30b1\u30c3\u30c8\u3068\u8981\u7d20\u3092\u8d70\u67fb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\\(O(n + k)\\) \u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002\u6700\u60aa\u306e\u5834\u5408\u3001\u3059\u3079\u3066\u306e\u30c7\u30fc\u30bf\u304c\u5358\u4e00\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u6563\u3055\u308c\u3001\u305d\u306e\u30d0\u30b1\u30c3\u30c8\u306e\u30bd\u30fc\u30c8\u306b\u306f \\(O(n^2)\\) \u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002
        • \u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(n + k)\\)\u3001\u975e\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u30bd\u30fc\u30c8\uff1a\\(k\\) \u500b\u306e\u30d0\u30b1\u30c3\u30c8\u3068\u5408\u8a08 \\(n\\) \u500b\u306e\u8981\u7d20\u306e\u305f\u3081\u306e\u8ffd\u52a0\u30b9\u30da\u30fc\u30b9\u304c\u5fc5\u8981\u3067\u3059\u3002
        • \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u304c\u5b89\u5b9a\u304b\u3069\u3046\u304b\u306f\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u5185\u3067\u4f7f\u7528\u3055\u308c\u308b\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u5b89\u5b9a\u304b\u3069\u3046\u304b\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002
        "},{"location":"chapter_sorting/bucket_sort/#1183","title":"11.8.3 \u00a0 \u5747\u7b49\u5206\u6563\u3092\u9054\u6210\u3059\u308b\u65b9\u6cd5","text":"

        \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306e\u7406\u8ad6\u7684\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u306b\u9054\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u91cd\u8981\u306a\u3053\u3068\u306f\u3001\u3059\u3079\u3066\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u8981\u7d20\u3092\u5747\u7b49\u306b\u5206\u6563\u3055\u305b\u308b\u3053\u3068\u3067\u3059\u3002\u5b9f\u4e16\u754c\u306e\u30c7\u30fc\u30bf\u306f\u3057\u3070\u3057\u3070\u5747\u4e00\u306b\u5206\u6563\u3055\u308c\u3066\u3044\u306a\u3044\u304b\u3089\u3067\u3059\u3002\u4f8b\u3048\u3070\u3001eBay\u306e\u3059\u3079\u3066\u306e\u5546\u54c1\u3092\u4fa1\u683c\u7bc4\u56f2\u306710\u500b\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u5747\u7b49\u306b\u5206\u6563\u3055\u305b\u305f\u3044\u3068\u3057\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u5546\u54c1\u4fa1\u683c\u306e\u5206\u6563\u306f\u5747\u7b49\u3067\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001100\u30c9\u30eb\u672a\u6e80\u306e\u5546\u54c1\u304c\u591a\u304f\u3001500\u30c9\u30eb\u4ee5\u4e0a\u306e\u5546\u54c1\u304c\u5c11\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u4fa1\u683c\u7bc4\u56f2\u3092\u5747\u7b49\u306b10\u5206\u5272\u3059\u308b\u3068\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u306e\u5546\u54c1\u6570\u306e\u5dee\u304c\u5927\u304d\u304f\u306a\u308a\u307e\u3059\u3002

        \u5747\u7b49\u5206\u6563\u3092\u9054\u6210\u3059\u308b\u305f\u3081\u306b\u3001\u6700\u521d\u306b\u304a\u304a\u3088\u305d\u306e\u5883\u754c\u3092\u8a2d\u5b9a\u3057\u3066\u3001\u30c7\u30fc\u30bf\u30923\u3064\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u5927\u307e\u304b\u306b\u5206\u5272\u3067\u304d\u307e\u3059\u3002\u5206\u6563\u304c\u5b8c\u4e86\u3057\u305f\u5f8c\u3001\u3088\u308a\u591a\u304f\u306e\u30a2\u30a4\u30c6\u30e0\u3092\u6301\u3064\u30d0\u30b1\u30c3\u30c8\u3092\u3055\u3089\u306b3\u3064\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u5272\u3057\u3001\u3059\u3079\u3066\u306e\u30d0\u30b1\u30c3\u30c8\u306e\u8981\u7d20\u6570\u304c\u307b\u307c\u7b49\u3057\u304f\u306a\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u3053\u306e\u65b9\u6cd5\u306f\u672c\u8cea\u7684\u306b\u518d\u5e30\u6728\u3092\u69cb\u7bc9\u3057\u3001\u8449\u30ce\u30fc\u30c9\u306e\u8981\u7d20\u6570\u304c\u3067\u304d\u308b\u3060\u3051\u5747\u7b49\u306b\u306a\u308b\u3053\u3068\u3092\u76ee\u6307\u3057\u307e\u3059\u3002\u3082\u3061\u308d\u3093\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u30c7\u30fc\u30bf\u30923\u3064\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u5272\u3059\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093 - \u5206\u5272\u6226\u7565\u306f\u30c7\u30fc\u30bf\u306e\u72ec\u7279\u306a\u7279\u6027\u306b\u9069\u5fdc\u7684\u306b\u8abf\u6574\u3067\u304d\u307e\u3059\u3002

        \u56f3 11-14 \u00a0 \u30d0\u30b1\u30c3\u30c8\u306e\u518d\u5e30\u7684\u5206\u5272

        \u5546\u54c1\u4fa1\u683c\u306e\u78ba\u7387\u5206\u5e03\u3092\u4e8b\u524d\u306b\u77e5\u3063\u3066\u3044\u308b\u5834\u5408\u3001\u30c7\u30fc\u30bf\u306e\u78ba\u7387\u5206\u5e03\u306b\u57fa\u3065\u3044\u3066\u5404\u30d0\u30b1\u30c3\u30c8\u306e\u4fa1\u683c\u5883\u754c\u3092\u8a2d\u5b9a\u3067\u304d\u307e\u3059\u3002\u30c7\u30fc\u30bf\u5206\u5e03\u3092\u5177\u4f53\u7684\u306b\u8a08\u7b97\u3059\u308b\u5fc5\u8981\u306f\u5fc5\u305a\u3057\u3082\u306a\u304f\u3001\u4ee3\u308f\u308a\u306b\u78ba\u7387\u30e2\u30c7\u30eb\u3092\u4f7f\u7528\u3057\u3066\u30c7\u30fc\u30bf\u7279\u6027\u306b\u57fa\u3065\u3044\u3066\u8fd1\u4f3c\u3067\u304d\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5546\u54c1\u4fa1\u683c\u304c\u6b63\u898f\u5206\u5e03\u306b\u5f93\u3046\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u30d0\u30b1\u30c3\u30c8\u9593\u3067\u30a2\u30a4\u30c6\u30e0\u306e\u5206\u6563\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u53d6\u308b\u305f\u3081\u306b\u5408\u7406\u7684\u306a\u4fa1\u683c\u533a\u9593\u3092\u5b9a\u7fa9\u3067\u304d\u307e\u3059\u3002

        \u56f3 11-15 \u00a0 \u78ba\u7387\u5206\u5e03\u306b\u57fa\u3065\u304f\u30d0\u30b1\u30c3\u30c8\u5206\u5272

        "},{"location":"chapter_sorting/counting_sort/","title":"11.9 \u00a0 \u8a08\u6570\u30bd\u30fc\u30c8","text":"

        \u8a08\u6570\u30bd\u30fc\u30c8\u306f\u8981\u7d20\u306e\u6570\u3092\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u3053\u3068\u3067\u30bd\u30fc\u30c8\u3092\u5b9f\u73fe\u3057\u3001\u901a\u5e38\u306f\u6574\u6570\u914d\u5217\u306b\u9069\u7528\u3055\u308c\u307e\u3059\u3002

        "},{"location":"chapter_sorting/counting_sort/#1191","title":"11.9.1 \u00a0 \u7c21\u5358\u306a\u5b9f\u88c5","text":"

        \u7c21\u5358\u306a\u4f8b\u304b\u3089\u59cb\u3081\u307e\u3057\u3087\u3046\u3002\u9577\u3055 \\(n\\) \u306e\u914d\u5217 nums \u304c\u4e0e\u3048\u3089\u308c\u3001\u3059\u3079\u3066\u306e\u8981\u7d20\u304c\u300c\u975e\u8ca0\u6574\u6570\u300d\u3067\u3042\u308b\u5834\u5408\u3001\u8a08\u6570\u30bd\u30fc\u30c8\u306e\u5168\u4f53\u7684\u306a\u904e\u7a0b\u306f\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        1. \u914d\u5217\u3092\u8d70\u67fb\u3057\u3066\u6700\u5927\u6570\u3092\u898b\u3064\u3051\u3001\u305d\u308c\u3092 \\(m\\) \u3068\u3057\u3001\u9577\u3055 \\(m + 1\\) \u306e\u88dc\u52a9\u914d\u5217 counter \u3092\u4f5c\u6210\u3057\u307e\u3059\u3002
        2. counter \u3092\u4f7f\u7528\u3057\u3066 nums \u5185\u306e\u5404\u6570\u306e\u51fa\u73fe\u56de\u6570\u3092\u30ab\u30a6\u30f3\u30c8\u3057\u307e\u3059\u3002\u3053\u3053\u3067 counter[num] \u306f\u6570 num \u306e\u51fa\u73fe\u56de\u6570\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002\u30ab\u30a6\u30f3\u30c8\u65b9\u6cd5\u306f\u7c21\u5358\u3067\u3001nums \u3092\u8d70\u67fb\u3057\uff08\u73fe\u5728\u306e\u6570\u3092 num \u3068\u3059\u308b\uff09\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u3067 counter[num] \u3092 \\(1\\) \u5897\u3084\u3057\u307e\u3059\u3002
        3. counter \u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u81ea\u7136\u306b\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u3059\u3079\u3066\u306e\u6570\u306f\u672c\u8cea\u7684\u306b\u3059\u3067\u306b\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u6b21\u306b\u3001counter \u3092\u8d70\u67fb\u3057\u3001\u51fa\u73fe\u9806\u306b nums \u3092\u6607\u9806\u3067\u57cb\u3081\u307e\u3059\u3002

        \u56f3 11-16 \u00a0 \u8a08\u6570\u30bd\u30fc\u30c8\u306e\u904e\u7a0b

        \u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby counting_sort.py
        def counting_sort_naive(nums: list[int]):\n    \"\"\"\u8a08\u6570\u30bd\u30fc\u30c8\"\"\"\n    # \u30b7\u30f3\u30d7\u30eb\u306a\u5b9f\u88c5\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30bd\u30fc\u30c8\u306b\u306f\u4f7f\u7528\u3067\u304d\u306a\u3044\n    # 1. \u914d\u5217\u5185\u306e\u6700\u5927\u8981\u7d20 m \u3092\u7d71\u8a08\n    m = 0\n    for num in nums:\n        m = max(m, num)\n    # 2. \u5404\u6570\u5b57\u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    # counter[num] \u306f num \u306e\u51fa\u73fe\u56de\u6570\u3092\u8868\u3059\n    counter = [0] * (m + 1)\n    for num in nums:\n        counter[num] += 1\n    # 3. counter \u3092\u8d70\u67fb\u3057\u3001\u5404\u8981\u7d20\u3092\u5143\u306e\u914d\u5217 nums \u306b\u57cb\u3081\u623b\u3057\n    i = 0\n    for num in range(m + 1):\n        for _ in range(counter[num]):\n            nums[i] = num\n            i += 1\n
        counting_sort.cpp
        /* \u30ab\u30a6\u30f3\u30c8\u30bd\u30fc\u30c8 */\n// \u7c21\u5358\u306a\u5b9f\u88c5\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30bd\u30fc\u30c8\u306b\u306f\u4f7f\u7528\u3067\u304d\u306a\u3044\nvoid countingSortNaive(vector<int> &nums) {\n    // 1. \u914d\u5217\u306e\u6700\u5927\u8981\u7d20m\u3092\u7d71\u8a08\n    int m = 0;\n    for (int num : nums) {\n        m = max(m, num);\n    }\n    // 2. \u5404\u6570\u5b57\u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    // counter[num]\u306fnum\u306e\u51fa\u73fe\u56de\u6570\u3092\u8868\u3059\n    vector<int> counter(m + 1, 0);\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. counter\u3092\u8d70\u67fb\u3057\u3001\u5404\u8981\u7d20\u3092\u5143\u306e\u914d\u5217nums\u306b\u623b\u3059\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
        counting_sort.java
        /* \u8a08\u6570\u30bd\u30fc\u30c8 */\n// \u7c21\u5358\u306a\u5b9f\u88c5\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30bd\u30fc\u30c8\u306b\u306f\u4f7f\u7528\u3067\u304d\u306a\u3044\nvoid countingSortNaive(int[] nums) {\n    // 1. \u914d\u5217\u306e\u6700\u5927\u8981\u7d20 m \u3092\u7d71\u8a08\n    int m = 0;\n    for (int num : nums) {\n        m = Math.max(m, num);\n    }\n    // 2. \u5404\u6570\u5b57\u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    // counter[num] \u306f num \u306e\u51fa\u73fe\u56de\u6570\u3092\u8868\u3059\n    int[] counter = new int[m + 1];\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. counter \u3092\u8d70\u67fb\u3057\u3001\u5404\u8981\u7d20\u3092\u5143\u306e\u914d\u5217 nums \u306b\u623b\u3059\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
        counting_sort.cs
        [class]{counting_sort}-[func]{CountingSortNaive}\n
        counting_sort.go
        [class]{}-[func]{countingSortNaive}\n
        counting_sort.swift
        [class]{}-[func]{countingSortNaive}\n
        counting_sort.js
        [class]{}-[func]{countingSortNaive}\n
        counting_sort.ts
        [class]{}-[func]{countingSortNaive}\n
        counting_sort.dart
        [class]{}-[func]{countingSortNaive}\n
        counting_sort.rs
        [class]{}-[func]{counting_sort_naive}\n
        counting_sort.c
        [class]{}-[func]{countingSortNaive}\n
        counting_sort.kt
        [class]{}-[func]{countingSortNaive}\n
        counting_sort.rb
        [class]{}-[func]{counting_sort_naive}\n

        \u8a08\u6570\u30bd\u30fc\u30c8\u3068\u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306e\u95a2\u4fc2

        \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306e\u89b3\u70b9\u304b\u3089\u3001\u8a08\u6570\u30bd\u30fc\u30c8\u306b\u304a\u3051\u308b\u8a08\u6570\u914d\u5217 counter \u306e\u5404\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u30d0\u30b1\u30c3\u30c8\u3068\u8003\u3048\u3001\u30ab\u30a6\u30f3\u30c8\u306e\u904e\u7a0b\u3092\u8981\u7d20\u3092\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u6563\u3055\u305b\u308b\u3053\u3068\u3068\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u672c\u8cea\u7684\u306b\u3001\u8a08\u6570\u30bd\u30fc\u30c8\u306f\u6574\u6570\u30c7\u30fc\u30bf\u306e\u305f\u3081\u306e\u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306e\u7279\u5225\u306a\u30b1\u30fc\u30b9\u3067\u3059\u3002

        "},{"location":"chapter_sorting/counting_sort/#1192","title":"11.9.2 \u00a0 \u5b8c\u5168\u306a\u5b9f\u88c5","text":"

        \u6ce8\u610f\u6df1\u3044\u8aad\u8005\u306f\u6c17\u4ed8\u304f\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u5165\u529b\u30c7\u30fc\u30bf\u304c\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5834\u5408\u3001\u4e0a\u8a18\u306e\u624b\u9806 3. \u306f\u7121\u52b9\u3067\u3059\u3002\u5165\u529b\u30c7\u30fc\u30bf\u304c\u5546\u54c1\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3001\u4fa1\u683c\uff08\u30af\u30e9\u30b9\u30e1\u30f3\u30d0\u5909\u6570\uff09\u3067\u5546\u54c1\u3092\u30bd\u30fc\u30c8\u3057\u305f\u3044\u3068\u3057\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u4e0a\u8a18\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u7d50\u679c\u3068\u3057\u3066\u30bd\u30fc\u30c8\u6e08\u307f\u306e\u4fa1\u683c\u306e\u307f\u3092\u63d0\u4f9b\u3067\u304d\u307e\u3059\u3002

        \u3067\u306f\u3001\u5143\u306e\u30c7\u30fc\u30bf\u306e\u30bd\u30fc\u30c8\u7d50\u679c\u3092\u3069\u306e\u3088\u3046\u306b\u53d6\u5f97\u3067\u304d\u308b\u3067\u3057\u3087\u3046\u304b\uff1f\u307e\u305a\u3001counter \u306e\u300c\u524d\u7f6e\u548c\u300d\u3092\u8a08\u7b97\u3057\u307e\u3059\u3002\u540d\u524d\u304c\u793a\u3059\u3088\u3046\u306b\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i \u3067\u306e\u524d\u7f6e\u548c prefix[i] \u306f\u3001\u914d\u5217\u306e\u6700\u521d\u306e i \u500b\u306e\u8981\u7d20\u306e\u548c\u306b\u7b49\u3057\u3044\u3067\u3059\uff1a

        \\[ \\text{prefix}[i] = \\sum_{j=0}^i \\text{counter[j]} \\]

        \u524d\u7f6e\u548c\u306b\u306f\u660e\u78ba\u306a\u610f\u5473\u304c\u3042\u308a\u307e\u3059\u3002prefix[num] - 1 \u306f\u7d50\u679c\u914d\u5217 res \u306b\u304a\u3051\u308b\u8981\u7d20 num \u306e\u6700\u5f8c\u306e\u51fa\u73fe\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8868\u3057\u307e\u3059\u3002\u3053\u306e\u60c5\u5831\u306f\u91cd\u8981\u3067\u3001\u5404\u8981\u7d20\u304c\u7d50\u679c\u914d\u5217\u306e\u3069\u3053\u306b\u73fe\u308c\u308b\u3079\u304d\u304b\u3092\u6559\u3048\u3066\u304f\u308c\u307e\u3059\u3002\u6b21\u306b\u3001\u5143\u306e\u914d\u5217 nums \u306e\u5404\u8981\u7d20 num \u3092\u9006\u9806\u3067\u8d70\u67fb\u3057\u3001\u5404\u53cd\u5fa9\u3067\u4ee5\u4e0b\u306e2\u3064\u306e\u624b\u9806\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002

        1. \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 prefix[num] - 1 \u3067\u914d\u5217 res \u306b num \u3092\u57cb\u3081\u307e\u3059\u3002
        2. \u524d\u7f6e\u548c prefix[num] \u3092 \\(1\\) \u6e1b\u3089\u3057\u3066\u3001num \u3092\u914d\u7f6e\u3059\u308b\u6b21\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002

        \u8d70\u67fb\u5f8c\u3001\u914d\u5217 res \u306b\u306f\u30bd\u30fc\u30c8\u3055\u308c\u305f\u7d50\u679c\u304c\u542b\u307e\u308c\u3001\u6700\u5f8c\u306b res \u304c\u5143\u306e\u914d\u5217 nums \u3092\u7f6e\u304d\u63db\u3048\u307e\u3059\u3002\u5b8c\u5168\u306a\u8a08\u6570\u30bd\u30fc\u30c8\u306e\u904e\u7a0b\u306f\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        <1><2><3><4><5><6><7><8>

        \u56f3 11-17 \u00a0 \u8a08\u6570\u30bd\u30fc\u30c8\u306e\u904e\u7a0b

        \u8a08\u6570\u30bd\u30fc\u30c8\u306e\u5b9f\u88c5\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby counting_sort.py
        def counting_sort(nums: list[int]):\n    \"\"\"\u8a08\u6570\u30bd\u30fc\u30c8\"\"\"\n    # \u5b8c\u5168\u306a\u5b9f\u88c5\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30bd\u30fc\u30c8\u304c\u53ef\u80fd\u3067\u3001\u5b89\u5b9a\u30bd\u30fc\u30c8\n    # 1. \u914d\u5217\u5185\u306e\u6700\u5927\u8981\u7d20 m \u3092\u7d71\u8a08\n    m = max(nums)\n    # 2. \u5404\u6570\u5b57\u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    # counter[num] \u306f num \u306e\u51fa\u73fe\u56de\u6570\u3092\u8868\u3059\n    counter = [0] * (m + 1)\n    for num in nums:\n        counter[num] += 1\n    # 3. counter \u306e\u524d\u7f6e\u548c\u3092\u8a08\u7b97\u3057\u3001\u300c\u51fa\u73fe\u56de\u6570\u300d\u3092\u300c\u672b\u5c3e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u306b\u5909\u63db\n    # counter[num]-1 \u306f res \u306b\u304a\u3044\u3066 num \u304c\u6700\u5f8c\u306b\u51fa\u73fe\u3059\u308b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\n    for i in range(m):\n        counter[i + 1] += counter[i]\n    # 4. nums \u3092\u9006\u9806\u306b\u8d70\u67fb\u3057\u3001\u5404\u8981\u7d20\u3092\u7d50\u679c\u914d\u5217 res \u306b\u914d\u7f6e\n    # \u7d50\u679c\u3092\u8a18\u9332\u3059\u308b\u305f\u3081\u306e\u914d\u5217 res \u3092\u521d\u671f\u5316\n    n = len(nums)\n    res = [0] * n\n    for i in range(n - 1, -1, -1):\n        num = nums[i]\n        res[counter[num] - 1] = num  # num \u3092\u5bfe\u5fdc\u3059\u308b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u914d\u7f6e\n        counter[num] -= 1  # \u524d\u7f6e\u548c\u30921\u6e1b\u3089\u3057\u3001num \u3092\u914d\u7f6e\u3059\u308b\u6b21\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\n    # \u7d50\u679c\u914d\u5217 res \u3092\u4f7f\u7528\u3057\u3066\u5143\u306e\u914d\u5217 nums \u3092\u4e0a\u66f8\u304d\n    for i in range(n):\n        nums[i] = res[i]\n
        counting_sort.cpp
        /* \u30ab\u30a6\u30f3\u30c8\u30bd\u30fc\u30c8 */\n// \u5b8c\u5168\u306a\u5b9f\u88c5\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30bd\u30fc\u30c8\u304c\u53ef\u80fd\u3067\u5b89\u5b9a\u30bd\u30fc\u30c8\nvoid countingSort(vector<int> &nums) {\n    // 1. \u914d\u5217\u306e\u6700\u5927\u8981\u7d20m\u3092\u7d71\u8a08\n    int m = 0;\n    for (int num : nums) {\n        m = max(m, num);\n    }\n    // 2. \u5404\u6570\u5b57\u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    // counter[num]\u306fnum\u306e\u51fa\u73fe\u56de\u6570\u3092\u8868\u3059\n    vector<int> counter(m + 1, 0);\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. counter\u306e\u524d\u7f00\u548c\u3092\u8a08\u7b97\u3057\u3001\u300c\u51fa\u73fe\u56de\u6570\u300d\u3092\u300c\u672b\u5c3e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u306b\u5909\u63db\n    // counter[num]-1\u306fnum\u304cres\u3067\u73fe\u308c\u308b\u6700\u5f8c\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. nums\u3092\u9006\u9806\u3067\u8d70\u67fb\u3057\u3001\u5404\u8981\u7d20\u3092\u7d50\u679c\u914d\u5217res\u306b\u914d\u7f6e\n    // \u7d50\u679c\u3092\u8a18\u9332\u3059\u308b\u914d\u5217res\u3092\u521d\u671f\u5316\n    int n = nums.size();\n    vector<int> res(n);\n    for (int i = n - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // num\u3092\u5bfe\u5fdc\u3059\u308b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u914d\u7f6e\n        counter[num]--;              // \u524d\u7f00\u548c\u30921\u6e1b\u3089\u3057\u3001num\u3092\u914d\u7f6e\u3059\u308b\u6b21\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\n    }\n    // \u7d50\u679c\u914d\u5217res\u3067\u5143\u306e\u914d\u5217nums\u3092\u4e0a\u66f8\u304d\n    nums = res;\n}\n
        counting_sort.java
        /* \u8a08\u6570\u30bd\u30fc\u30c8 */\n// \u5b8c\u5168\u306a\u5b9f\u88c5\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u30bd\u30fc\u30c8\u3067\u304d\u3001\u5b89\u5b9a\u30bd\u30fc\u30c8\nvoid countingSort(int[] nums) {\n    // 1. \u914d\u5217\u306e\u6700\u5927\u8981\u7d20 m \u3092\u7d71\u8a08\n    int m = 0;\n    for (int num : nums) {\n        m = Math.max(m, num);\n    }\n    // 2. \u5404\u6570\u5b57\u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    // counter[num] \u306f num \u306e\u51fa\u73fe\u56de\u6570\u3092\u8868\u3059\n    int[] counter = new int[m + 1];\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. counter \u306e\u7d2f\u7a4d\u548c\u3092\u8a08\u7b97\u3057\u3001\u300c\u51fa\u73fe\u56de\u6570\u300d\u3092\u300c\u5c3b\u5c3e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u306b\u5909\u63db\n    // counter[num]-1 \u306f res \u5185\u3067 num \u304c\u51fa\u73fe\u3059\u308b\u6700\u5f8c\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. nums \u3092\u9006\u9806\u306b\u8d70\u67fb\u3057\u3001\u5404\u8981\u7d20\u3092\u7d50\u679c\u914d\u5217 res \u306b\u914d\u7f6e\n    // \u7d50\u679c\u3092\u8a18\u9332\u3059\u308b\u914d\u5217 res \u3092\u521d\u671f\u5316\n    int n = nums.length;\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // num \u3092\u5bfe\u5fdc\u3059\u308b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u914d\u7f6e\n        counter[num]--; // \u7d2f\u7a4d\u548c\u3092 1 \u6e1b\u7b97\u3057\u3001num \u3092\u914d\u7f6e\u3059\u308b\u6b21\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\n    }\n    // \u7d50\u679c\u914d\u5217 res \u3092\u4f7f\u3063\u3066\u5143\u306e\u914d\u5217 nums \u3092\u4e0a\u66f8\u304d\n    for (int i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n
        counting_sort.cs
        [class]{counting_sort}-[func]{CountingSort}\n
        counting_sort.go
        [class]{}-[func]{countingSort}\n
        counting_sort.swift
        [class]{}-[func]{countingSort}\n
        counting_sort.js
        [class]{}-[func]{countingSort}\n
        counting_sort.ts
        [class]{}-[func]{countingSort}\n
        counting_sort.dart
        [class]{}-[func]{countingSort}\n
        counting_sort.rs
        [class]{}-[func]{counting_sort}\n
        counting_sort.c
        [class]{}-[func]{countingSort}\n
        counting_sort.kt
        [class]{}-[func]{countingSort}\n
        counting_sort.rb
        [class]{}-[func]{counting_sort}\n
        "},{"location":"chapter_sorting/counting_sort/#1193","title":"11.9.3 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7279\u5fb4","text":"
        • \u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n + m)\\)\u3001\u975e\u9069\u5fdc\u30bd\u30fc\u30c8\uff1anums \u3068 counter \u306e\u8d70\u67fb\u304c\u542b\u307e\u308c\u3001\u3069\u3061\u3089\u3082\u7dda\u5f62\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u4e00\u822c\u7684\u306b\u3001\\(n \\gg m\\) \u3067\u3042\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u306b\u8fd1\u3065\u304d\u307e\u3059\u3002
        • \u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(n + m)\\)\u3001\u975e\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u30bd\u30fc\u30c8\uff1a\u9577\u3055 \\(n\\) \u306e\u914d\u5217 res \u3068\u9577\u3055 \\(m\\) \u306e\u914d\u5217 counter \u3092\u305d\u308c\u305e\u308c\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u5b89\u5b9a\u30bd\u30fc\u30c8\uff1a\u8981\u7d20\u304c\u300c\u53f3\u304b\u3089\u5de6\u300d\u306e\u9806\u5e8f\u3067 res \u306b\u57cb\u3081\u3089\u308c\u308b\u305f\u3081\u3001nums \u306e\u8d70\u67fb\u3092\u9006\u9806\u306b\u3059\u308b\u3053\u3068\u3067\u3001\u7b49\u3057\u3044\u8981\u7d20\u9593\u306e\u76f8\u5bfe\u4f4d\u7f6e\u306e\u5909\u5316\u3092\u9632\u304e\u3001\u5b89\u5b9a\u3057\u305f\u30bd\u30fc\u30c8\u3092\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002\u5b9f\u969b\u3001nums \u3092\u9806\u756a\u306b\u8d70\u67fb\u3057\u3066\u3082\u6b63\u3057\u3044\u30bd\u30fc\u30c8\u7d50\u679c\u3092\u751f\u6210\u3067\u304d\u307e\u3059\u304c\u3001\u7d50\u679c\u306f\u4e0d\u5b89\u5b9a\u3067\u3059\u3002
        "},{"location":"chapter_sorting/counting_sort/#1194","title":"11.9.4 \u00a0 \u5236\u9650\u4e8b\u9805","text":"

        \u4eca\u307e\u3067\u306b\u3001\u8a08\u6570\u30bd\u30fc\u30c8\u306f\u975e\u5e38\u306b\u5de7\u5999\u3060\u3068\u611f\u3058\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u5358\u306b\u91cf\u3092\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u3060\u3051\u3067\u52b9\u7387\u7684\u306a\u30bd\u30fc\u30c8\u3092\u5b9f\u73fe\u3067\u304d\u308b\u304b\u3089\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u8a08\u6570\u30bd\u30fc\u30c8\u3092\u4f7f\u7528\u3059\u308b\u305f\u3081\u306e\u524d\u63d0\u6761\u4ef6\u306f\u6bd4\u8f03\u7684\u53b3\u3057\u3044\u3067\u3059\u3002

        \u8a08\u6570\u30bd\u30fc\u30c8\u306f\u975e\u8ca0\u6574\u6570\u306b\u306e\u307f\u9069\u7528\u3067\u304d\u307e\u3059\u3002\u4ed6\u306e\u30bf\u30a4\u30d7\u306e\u30c7\u30fc\u30bf\u306b\u9069\u7528\u3057\u305f\u3044\u5834\u5408\u3001\u3053\u308c\u3089\u306e\u30c7\u30fc\u30bf\u304c\u8981\u7d20\u306e\u5143\u306e\u9806\u5e8f\u3092\u5909\u66f4\u3059\u308b\u3053\u3068\u306a\u304f\u975e\u8ca0\u6574\u6570\u306b\u5909\u63db\u3067\u304d\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u8ca0\u306e\u6574\u6570\u3092\u542b\u3080\u914d\u5217\u306e\u5834\u5408\u3001\u6700\u521d\u306b\u3059\u3079\u3066\u306e\u6570\u306b\u5b9a\u6570\u3092\u52a0\u3048\u3066\u3001\u3059\u3079\u3066\u3092\u6b63\u306e\u6570\u306b\u5909\u63db\u3057\u3001\u30bd\u30fc\u30c8\u5b8c\u4e86\u5f8c\u306b\u5143\u306b\u623b\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u8a08\u6570\u30bd\u30fc\u30c8\u306f\u5024\u306e\u7bc4\u56f2\u304c\u5c0f\u3055\u3044\u5927\u304d\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u4e0a\u8a18\u306e\u4f8b\u3067\u306f\u3001\\(m\\) \u306f\u5927\u304d\u3059\u304e\u308b\u3079\u304d\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u305d\u3046\u3067\u306a\u3051\u308c\u3070\u3001\u3042\u307e\u308a\u306b\u3082\u591a\u304f\u306e\u30b9\u30da\u30fc\u30b9\u3092\u5360\u6709\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u3002\u305d\u3057\u3066 \\(n \\ll m\\) \u306e\u5834\u5408\u3001\u8a08\u6570\u30bd\u30fc\u30c8\u306f \\(O(m)\\) \u6642\u9593\u3092\u4f7f\u7528\u3057\u3001\\(O(n \\log n)\\) \u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3088\u308a\u9045\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_sorting/heap_sort/","title":"11.7 \u00a0 \u30d2\u30fc\u30d7\u30bd\u30fc\u30c8","text":"

        Tip

        \u3053\u306e\u7bc0\u3092\u8aad\u3080\u524d\u306b\u3001\u300c\u30d2\u30fc\u30d7\u300d\u306e\u7ae0\u3092\u5fc5\u305a\u5b8c\u4e86\u3055\u305b\u3066\u304f\u3060\u3055\u3044\u3002

        \u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\u306f\u3001\u30d2\u30fc\u30d7\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u57fa\u3065\u304f\u52b9\u7387\u7684\u306a\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3059\u3002\u3059\u3067\u306b\u5b66\u7fd2\u3057\u305f\u300c\u30d2\u30fc\u30d7\u306e\u69cb\u7bc9\u300d\u3068\u300c\u8981\u7d20\u306e\u62bd\u51fa\u300d\u64cd\u4f5c\u3092\u4f7f\u7528\u3057\u3066\u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002

        1. \u914d\u5217\u3092\u5165\u529b\u3057\u3001\u6700\u5c0f\u30d2\u30fc\u30d7\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002\u3053\u3053\u3067\u3001\u6700\u5c0f\u8981\u7d20\u304c\u30d2\u30fc\u30d7\u306e\u9802\u4e0a\u306b\u4f4d\u7f6e\u3057\u307e\u3059\u3002
        2. \u7d99\u7d9a\u7684\u306b\u62bd\u51fa\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3001\u62bd\u51fa\u3055\u308c\u305f\u8981\u7d20\u3092\u9806\u6b21\u8a18\u9332\u3057\u3066\u3001\u6700\u5c0f\u304b\u3089\u6700\u5927\u307e\u3067\u306e\u30bd\u30fc\u30c8\u6e08\u307f\u30ea\u30b9\u30c8\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002

        \u4e0a\u8a18\u306e\u65b9\u6cd5\u306f\u5b9f\u73fe\u53ef\u80fd\u3067\u3059\u304c\u3001\u30dd\u30c3\u30d7\u3055\u308c\u305f\u8981\u7d20\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306e\u8ffd\u52a0\u306e\u914d\u5217\u304c\u5fc5\u8981\u3067\u3001\u3084\u3084\u7a7a\u9593\u3092\u6d88\u8cbb\u3057\u307e\u3059\u3002\u5b9f\u969b\u306b\u306f\u3001\u901a\u5e38\u3001\u3088\u308a\u512a\u96c5\u306a\u5b9f\u88c5\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        "},{"location":"chapter_sorting/heap_sort/#1171","title":"11.7.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6d41\u308c","text":"

        \u914d\u5217\u306e\u9577\u3055\u3092 \\(n\\) \u3068\u3059\u308b\u3068\u3001\u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\u306e\u904e\u7a0b\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002

        1. \u914d\u5217\u3092\u5165\u529b\u3057\u3001\u6700\u5927\u30d2\u30fc\u30d7\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002\u3053\u306e\u624b\u9806\u306e\u5f8c\u3001\u6700\u5927\u8981\u7d20\u304c\u30d2\u30fc\u30d7\u306e\u9802\u4e0a\u306b\u4f4d\u7f6e\u3057\u307e\u3059\u3002
        2. \u30d2\u30fc\u30d7\u306e\u9802\u4e0a\u8981\u7d20\uff08\u6700\u521d\u306e\u8981\u7d20\uff09\u3068\u30d2\u30fc\u30d7\u306e\u5e95\u90e8\u8981\u7d20\uff08\u6700\u5f8c\u306e\u8981\u7d20\uff09\u3092\u4ea4\u63db\u3057\u307e\u3059\u3002\u3053\u306e\u4ea4\u63db\u306e\u5f8c\u3001\u30d2\u30fc\u30d7\u306e\u9577\u3055\u3092 \\(1\\) \u6e1b\u3089\u3057\u3001\u30bd\u30fc\u30c8\u6e08\u307f\u8981\u7d20\u306e\u6570\u3092 \\(1\\) \u5897\u3084\u3057\u307e\u3059\u3002
        3. \u30d2\u30fc\u30d7\u306e\u9802\u4e0a\u304b\u3089\u958b\u59cb\u3057\u3066\u3001\u4e0a\u304b\u3089\u4e0b\u3078\u306esift-down\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002sift-down\u306e\u5f8c\u3001\u30d2\u30fc\u30d7\u306e\u6027\u8cea\u304c\u5fa9\u5143\u3055\u308c\u307e\u3059\u3002
        4. \u624b\u9806 2. \u3068 3. \u3092\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002\\(n - 1\\) \u30e9\u30a6\u30f3\u30c9\u30eb\u30fc\u30d7\u3057\u3066\u3001\u914d\u5217\u306e\u30bd\u30fc\u30c8\u3092\u5b8c\u4e86\u3057\u307e\u3059\u3002

        Tip

        \u5b9f\u969b\u3001\u8981\u7d20\u62bd\u51fa\u64cd\u4f5c\u3082\u624b\u9806 2. \u3068 3. \u3092\u542b\u307f\u3001\u62bd\u51fa\u3055\u308c\u305f\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u304b\u3089\u524a\u9664\u3059\u308b\u8ffd\u52a0\u306e\u624b\u9806\u304c\u3042\u308a\u307e\u3059\u3002

        <1><2><3><4><5><6><7><8><9><10><11><12>

        \u56f3 11-12 \u00a0 \u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\u306e\u904e\u7a0b

        \u30b3\u30fc\u30c9\u306e\u5b9f\u88c5\u3067\u306f\u3001\u300c\u30d2\u30fc\u30d7\u300d\u306e\u7ae0\u304b\u3089\u306esift-down\u95a2\u6570 sift_down() \u3092\u4f7f\u7528\u3057\u307e\u3057\u305f\u3002\u6700\u5927\u8981\u7d20\u304c\u62bd\u51fa\u3055\u308c\u308b\u306b\u3064\u308c\u3066\u30d2\u30fc\u30d7\u306e\u9577\u3055\u304c\u6e1b\u5c11\u3059\u308b\u305f\u3081\u3001sift_down() \u95a2\u6570\u306b\u9577\u3055\u30d1\u30e9\u30e1\u30fc\u30bf \\(n\\) \u3092\u8ffd\u52a0\u3057\u3066\u3001\u30d2\u30fc\u30d7\u306e\u73fe\u5728\u306e\u6709\u52b9\u9577\u3092\u6307\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby heap_sort.py
        def sift_down(nums: list[int], n: int, i: int):\n    \"\"\"\u30d2\u30fc\u30d7\u306e\u9577\u3055\u304c n\u3001\u30ce\u30fc\u30c9 i \u304b\u3089\u4e0a\u304b\u3089\u4e0b\u3078\u30d2\u30fc\u30d7\u5316\u3092\u958b\u59cb\"\"\"\n    while True:\n        # i\u3001l\u3001r \u306e\u4e2d\u3067\u6700\u5927\u306e\u30ce\u30fc\u30c9\u3092\u5224\u5b9a\u3057\u3001ma \u3068\u3059\u308b\n        l = 2 * i + 1\n        r = 2 * i + 2\n        ma = i\n        if l < n and nums[l] > nums[ma]:\n            ma = l\n        if r < n and nums[r] > nums[ma]:\n            ma = r\n        # \u30ce\u30fc\u30c9 i \u304c\u6700\u5927\u307e\u305f\u306f l\u3001r \u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001\u3055\u3089\u306a\u308b\u30d2\u30fc\u30d7\u5316\u306f\u4e0d\u8981\u3001\u30eb\u30fc\u30d7\u3092\u629c\u3051\u308b\n        if ma == i:\n            break\n        # 2\u3064\u306e\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\n        nums[i], nums[ma] = nums[ma], nums[i]\n        # \u4e0b\u5411\u304d\u306b\u30d2\u30fc\u30d7\u5316\u3092\u30eb\u30fc\u30d7\n        i = ma\n\ndef heap_sort(nums: list[int]):\n    \"\"\"\u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\"\"\"\n    # \u30d2\u30fc\u30d7\u69cb\u7bc9\u64cd\u4f5c\uff1a\u8449\u30ce\u30fc\u30c9\u4ee5\u5916\u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3092\u30d2\u30fc\u30d7\u5316\n    for i in range(len(nums) // 2 - 1, -1, -1):\n        sift_down(nums, len(nums), i)\n    # \u30d2\u30fc\u30d7\u304b\u3089\u6700\u5927\u8981\u7d20\u3092\u62bd\u51fa\u3057\u3001n-1 \u56de\u7e70\u308a\u8fd4\u3059\n    for i in range(len(nums) - 1, 0, -1):\n        # \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3068\u6700\u3082\u53f3\u306e\u8449\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\uff08\u6700\u521d\u306e\u8981\u7d20\u3068\u6700\u5f8c\u306e\u8981\u7d20\u3092\u4ea4\u63db\uff09\n        nums[0], nums[i] = nums[i], nums[0]\n        # \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u304b\u3089\u4e0a\u304b\u3089\u4e0b\u3078\u30d2\u30fc\u30d7\u5316\u3092\u958b\u59cb\n        sift_down(nums, i, 0)\n
        heap_sort.cpp
        /* \u30d2\u30fc\u30d7\u306e\u9577\u3055\u306fn\u3001\u30ce\u30fc\u30c9i\u304b\u3089\u4e0a\u304b\u3089\u4e0b\u3078\u30d2\u30fc\u30d7\u5316\u3092\u958b\u59cb */\nvoid siftDown(vector<int> &nums, int n, int i) {\n    while (true) {\n        // i\u3001l\u3001r \u306e\u4e2d\u3067\u6700\u5927\u306e\u30ce\u30fc\u30c9\u3092\u6c7a\u5b9a\u3057\u3001ma\u3068\u3057\u3066\u8a18\u9332\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // \u30ce\u30fc\u30c9i\u304c\u6700\u5927\u304b\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9l\u3001r\u304c\u5883\u754c\u5916\u306e\u5834\u5408\u3001\u305d\u308c\u4ee5\u4e0a\u306e\u30d2\u30fc\u30d7\u5316\u306f\u4e0d\u8981\u3067\u7d42\u4e86\n        if (ma == i) {\n            break;\n        }\n        // \u4e8c\u3064\u306e\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\n        swap(nums[i], nums[ma]);\n        // \u4e0b\u5411\u304d\u306b\u30d2\u30fc\u30d7\u5316\u3092\u30eb\u30fc\u30d7\n        i = ma;\n    }\n}\n\n/* \u30d2\u30fc\u30d7\u30bd\u30fc\u30c8 */\nvoid heapSort(vector<int> &nums) {\n    // \u30d2\u30fc\u30d7\u69cb\u7bc9\u64cd\u4f5c\uff1a\u8449\u4ee5\u5916\u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3092\u30d2\u30fc\u30d7\u5316\n    for (int i = nums.size() / 2 - 1; i >= 0; --i) {\n        siftDown(nums, nums.size(), i);\n    }\n    // \u30d2\u30fc\u30d7\u304b\u3089\u6700\u5927\u8981\u7d20\u3092\u62bd\u51fa\u3057\u3001n-1\u56de\u7e70\u308a\u8fd4\u3059\n    for (int i = nums.size() - 1; i > 0; --i) {\n        // \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u6700\u53f3\u8449\u30ce\u30fc\u30c9\u3068\u4ea4\u63db\uff08\u6700\u521d\u306e\u8981\u7d20\u3092\u6700\u5f8c\u306e\u8981\u7d20\u3068\u4ea4\u63db\uff09\n        swap(nums[0], nums[i]);\n        // \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u304b\u3089\u4e0a\u304b\u3089\u4e0b\u3078\u30d2\u30fc\u30d7\u5316\u3092\u958b\u59cb\n        siftDown(nums, i, 0);\n    }\n}\n
        heap_sort.java
        /* \u30d2\u30fc\u30d7\u306e\u9577\u3055\u306f n\u3001\u30ce\u30fc\u30c9 i \u304b\u3089\u4e0a\u304b\u3089\u4e0b\u3078\u30d2\u30fc\u30d7\u5316\u958b\u59cb */\nvoid siftDown(int[] nums, int n, int i) {\n    while (true) {\n        // i, l, r \u306e\u4e2d\u3067\u6700\u5927\u306e\u30ce\u30fc\u30c9\u3092\u5224\u5b9a\u3057\u3001ma \u3068\u3059\u308b\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // \u30ce\u30fc\u30c9 i \u304c\u6700\u5927\u3001\u307e\u305f\u306f\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 l, r \u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001\u3055\u3089\u306a\u308b\u30d2\u30fc\u30d7\u5316\u306f\u4e0d\u8981\u3001\u30d6\u30ec\u30fc\u30af\n        if (ma == i)\n            break;\n        // 2\u3064\u306e\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\n        int temp = nums[i];\n        nums[i] = nums[ma];\n        nums[ma] = temp;\n        // \u4e0b\u5411\u304d\u306b\u30d2\u30fc\u30d7\u5316\u3092\u30eb\u30fc\u30d7\n        i = ma;\n    }\n}\n\n/* \u30d2\u30fc\u30d7\u30bd\u30fc\u30c8 */\nvoid heapSort(int[] nums) {\n    // \u30d2\u30fc\u30d7\u69cb\u7bc9\u64cd\u4f5c: \u8449\u30ce\u30fc\u30c9\u4ee5\u5916\u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3092\u30d2\u30fc\u30d7\u5316\n    for (int i = nums.length / 2 - 1; i >= 0; i--) {\n        siftDown(nums, nums.length, i);\n    }\n    // \u30d2\u30fc\u30d7\u304b\u3089\u6700\u5927\u8981\u7d20\u3092\u62bd\u51fa\u3057\u3001n-1 \u56de\u7e70\u308a\u8fd4\u3057\n    for (int i = nums.length - 1; i > 0; i--) {\n        // \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3068\u6700\u3082\u53f3\u306e\u8449\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\uff08\u6700\u521d\u306e\u8981\u7d20\u3068\u6700\u5f8c\u306e\u8981\u7d20\u3092\u4ea4\u63db\uff09\n        int tmp = nums[0];\n        nums[0] = nums[i];\n        nums[i] = tmp;\n        // \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u304b\u3089\u4e0a\u304b\u3089\u4e0b\u3078\u30d2\u30fc\u30d7\u5316\u958b\u59cb\n        siftDown(nums, i, 0);\n    }\n}\n
        heap_sort.cs
        [class]{heap_sort}-[func]{SiftDown}\n\n[class]{heap_sort}-[func]{HeapSort}\n
        heap_sort.go
        [class]{}-[func]{siftDown}\n\n[class]{}-[func]{heapSort}\n
        heap_sort.swift
        [class]{}-[func]{siftDown}\n\n[class]{}-[func]{heapSort}\n
        heap_sort.js
        [class]{}-[func]{siftDown}\n\n[class]{}-[func]{heapSort}\n
        heap_sort.ts
        [class]{}-[func]{siftDown}\n\n[class]{}-[func]{heapSort}\n
        heap_sort.dart
        [class]{}-[func]{siftDown}\n\n[class]{}-[func]{heapSort}\n
        heap_sort.rs
        [class]{}-[func]{sift_down}\n\n[class]{}-[func]{heap_sort}\n
        heap_sort.c
        [class]{}-[func]{siftDown}\n\n[class]{}-[func]{heapSort}\n
        heap_sort.kt
        [class]{}-[func]{siftDown}\n\n[class]{}-[func]{heapSort}\n
        heap_sort.rb
        [class]{}-[func]{sift_down}\n\n[class]{}-[func]{heap_sort}\n
        "},{"location":"chapter_sorting/heap_sort/#1172","title":"11.7.2 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7279\u5fb4","text":"
        • \u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n \\log n)\\)\u3001\u975e\u9069\u5fdc\u30bd\u30fc\u30c8\uff1a\u30d2\u30fc\u30d7\u306e\u69cb\u7bc9\u306f \\(O(n)\\) \u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u30d2\u30fc\u30d7\u304b\u3089\u6700\u5927\u8981\u7d20\u3092\u62bd\u51fa\u3059\u308b\u306b\u306f \\(O(\\log n)\\) \u6642\u9593\u304c\u304b\u304b\u308a\u3001\\(n - 1\\) \u30e9\u30a6\u30f3\u30c9\u30eb\u30fc\u30d7\u3057\u307e\u3059\u3002
        • \u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(1)\\)\u3001\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u30bd\u30fc\u30c8\uff1a\u3044\u304f\u3064\u304b\u306e\u30dd\u30a4\u30f3\u30bf\u5909\u6570\u304c \\(O(1)\\) \u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u8981\u7d20\u306e\u4ea4\u63db\u3068\u30d2\u30fc\u30d7\u5316\u64cd\u4f5c\u306f\u5143\u306e\u914d\u5217\u3067\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002
        • \u975e\u5b89\u5b9a\u30bd\u30fc\u30c8\uff1a\u30d2\u30fc\u30d7\u306e\u9802\u4e0a\u3068\u5e95\u90e8\u8981\u7d20\u306e\u4ea4\u63db\u4e2d\u306b\u3001\u7b49\u3057\u3044\u8981\u7d20\u306e\u76f8\u5bfe\u4f4d\u7f6e\u304c\u5909\u308f\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        "},{"location":"chapter_sorting/insertion_sort/","title":"11.4 \u00a0 \u633f\u5165\u30bd\u30fc\u30c8","text":"

        \u633f\u5165\u30bd\u30fc\u30c8\u306f\u3001\u30c8\u30e9\u30f3\u30d7\u306e\u30c7\u30c3\u30ad\u3092\u624b\u52d5\u3067\u30bd\u30fc\u30c8\u3059\u308b\u30d7\u30ed\u30bb\u30b9\u306b\u3088\u304f\u4f3c\u305f\u52d5\u4f5c\u3092\u3059\u308b\u30b7\u30f3\u30d7\u30eb\u306a\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3059\u3002

        \u5177\u4f53\u7684\u306b\u306f\u3001\u672a\u30bd\u30fc\u30c8\u533a\u9593\u304b\u3089\u30d9\u30fc\u30b9\u8981\u7d20\u3092\u9078\u629e\u3057\u3001\u305d\u306e\u5de6\u5074\u306e\u30bd\u30fc\u30c8\u6e08\u307f\u533a\u9593\u306e\u8981\u7d20\u3068\u6bd4\u8f03\u3057\u3066\u3001\u8981\u7d20\u3092\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u633f\u5165\u3057\u307e\u3059\u3002

        \u4e0b\u56f3\u306f\u3001\u8981\u7d20\u304c\u914d\u5217\u306b\u633f\u5165\u3055\u308c\u308b\u65b9\u6cd5\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u30d9\u30fc\u30b9\u8981\u7d20\u3092base\u3068\u3059\u308b\u3068\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304b\u3089base\u307e\u3067\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u53f3\u306b1\u3064\u305a\u3064\u30b7\u30d5\u30c8\u3057\u3001\u305d\u306e\u5f8cbase\u3092\u30bf\u30fc\u30b2\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5272\u308a\u5f53\u3066\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u56f3 11-6 \u00a0 Single insertion operation

        "},{"location":"chapter_sorting/insertion_sort/#1141","title":"11.4.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d7\u30ed\u30bb\u30b9","text":"

        \u633f\u5165\u30bd\u30fc\u30c8\u306e\u5168\u4f53\u7684\u306a\u30d7\u30ed\u30bb\u30b9\u306f\u4e0b\u56f3\u306b\u793a\u3055\u308c\u307e\u3059\u3002

        1. \u914d\u5217\u306e\u6700\u521d\u306e\u8981\u7d20\u3092\u30bd\u30fc\u30c8\u6e08\u307f\u3068\u307f\u306a\u3057\u307e\u3059\u3002
        2. 2\u756a\u76ee\u306e\u8981\u7d20\u3092base\u3068\u3057\u3066\u9078\u629e\u3057\u3001\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u633f\u5165\u3057\u3066\u3001\u6700\u521d\u306e2\u3064\u306e\u8981\u7d20\u3092\u30bd\u30fc\u30c8\u6e08\u307f\u306b\u3057\u307e\u3059\u3002
        3. 3\u756a\u76ee\u306e\u8981\u7d20\u3092base\u3068\u3057\u3066\u9078\u629e\u3057\u3001\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u633f\u5165\u3057\u3066\u3001\u6700\u521d\u306e3\u3064\u306e\u8981\u7d20\u3092\u30bd\u30fc\u30c8\u6e08\u307f\u306b\u3057\u307e\u3059\u3002
        4. \u3053\u306e\u65b9\u6cd5\u3067\u7d9a\u884c\u3057\u3001\u6700\u5f8c\u306e\u53cd\u5fa9\u3067\u306f\u3001\u6700\u5f8c\u306e\u8981\u7d20\u3092base\u3068\u3057\u3066\u53d6\u308a\u3001\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u633f\u5165\u3057\u305f\u5f8c\u3001\u3059\u3079\u3066\u306e\u8981\u7d20\u304c\u30bd\u30fc\u30c8\u3055\u308c\u307e\u3059\u3002

        \u56f3 11-7 \u00a0 Insertion sort process

        \u30b3\u30fc\u30c9\u4f8b\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby insertion_sort.py
        def insertion_sort(nums: list[int]):\n    \"\"\"\u633f\u5165\u30bd\u30fc\u30c8\"\"\"\n    # \u5916\u5074\u306e\u30eb\u30fc\u30d7\uff1a\u30bd\u30fc\u30c8\u6e08\u307f\u7bc4\u56f2\u306f [0, i-1]\n    for i in range(1, len(nums)):\n        base = nums[i]\n        j = i - 1\n        # \u5185\u5074\u306e\u30eb\u30fc\u30d7\uff1abase \u3092\u30bd\u30fc\u30c8\u6e08\u307f\u7bc4\u56f2 [0, i-1] \u306e\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u633f\u5165\n        while j >= 0 and nums[j] > base:\n            nums[j + 1] = nums[j]  # nums[j] \u3092\u53f3\u306b1\u3064\u79fb\u52d5\n            j -= 1\n        nums[j + 1] = base  # base \u3092\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u4ee3\u5165\n
        insertion_sort.cpp
        /* \u633f\u5165\u30bd\u30fc\u30c8 */\nvoid insertionSort(vector<int> &nums) {\n    // \u5916\u5074\u30eb\u30fc\u30d7\uff1a\u30bd\u30fc\u30c8\u6e08\u307f\u7bc4\u56f2\u306f[0, i-1]\n    for (int i = 1; i < nums.size(); i++) {\n        int base = nums[i], j = i - 1;\n        // \u5185\u5074\u30eb\u30fc\u30d7\uff1abase\u3092\u30bd\u30fc\u30c8\u6e08\u307f\u7bc4\u56f2[0, i-1]\u5185\u306e\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u633f\u5165\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // nums[j]\u3092\u4e00\u3064\u53f3\u306b\u79fb\u52d5\n            j--;\n        }\n        nums[j + 1] = base; // base\u3092\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u4ee3\u5165\n    }\n}\n
        insertion_sort.java
        /* \u633f\u5165\u30bd\u30fc\u30c8 */\nvoid insertionSort(int[] nums) {\n    // \u5916\u5074\u30eb\u30fc\u30d7: \u30bd\u30fc\u30c8\u6e08\u307f\u7bc4\u56f2\u306f [0, i-1]\n    for (int i = 1; i < nums.length; i++) {\n        int base = nums[i], j = i - 1;\n        // \u5185\u5074\u30eb\u30fc\u30d7: base \u3092\u30bd\u30fc\u30c8\u6e08\u307f\u7bc4\u56f2 [0, i-1] \u306e\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u633f\u5165\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // nums[j] \u3092\u53f3\u306b1\u3064\u79fb\u52d5\n            j--;\n        }\n        nums[j + 1] = base;        // base \u3092\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u4ee3\u5165\n    }\n}\n
        insertion_sort.cs
        [class]{insertion_sort}-[func]{InsertionSort}\n
        insertion_sort.go
        [class]{}-[func]{insertionSort}\n
        insertion_sort.swift
        [class]{}-[func]{insertionSort}\n
        insertion_sort.js
        [class]{}-[func]{insertionSort}\n
        insertion_sort.ts
        [class]{}-[func]{insertionSort}\n
        insertion_sort.dart
        [class]{}-[func]{insertionSort}\n
        insertion_sort.rs
        [class]{}-[func]{insertion_sort}\n
        insertion_sort.c
        [class]{}-[func]{insertionSort}\n
        insertion_sort.kt
        [class]{}-[func]{insertionSort}\n
        insertion_sort.rb
        [class]{}-[func]{insertion_sort}\n
        "},{"location":"chapter_sorting/insertion_sort/#1142","title":"11.4.2 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7279\u6027","text":"
        • \u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n^2)\\)\u3001\u9069\u5fdc\u30bd\u30fc\u30c8\uff1a\u6700\u60aa\u306e\u5834\u5408\u3001\u5404\u633f\u5165\u64cd\u4f5c\u306b\u306f\\(n - 1\\)\u3001\\(n-2\\)\u3001...\u3001\\(2\\)\u3001\\(1\\)\u306e\u30eb\u30fc\u30d7\u304c\u5fc5\u8981\u3067\u3001\u5408\u8a08\u306f\\((n - 1) n / 2\\)\u3068\u306a\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n^2)\\)\u3067\u3059\u3002\u9806\u5e8f\u4ed8\u304d\u30c7\u30fc\u30bf\u306e\u5834\u5408\u3001\u633f\u5165\u64cd\u4f5c\u306f\u65e9\u671f\u306b\u7d42\u4e86\u3057\u307e\u3059\u3002\u5165\u529b\u914d\u5217\u304c\u5b8c\u5168\u306b\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u633f\u5165\u30bd\u30fc\u30c8\u306f\u6700\u826f\u6642\u9593\u8a08\u7b97\u91cf\\(O(n)\\)\u3092\u5b9f\u73fe\u3057\u307e\u3059\u3002
        • \u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u3001\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u30bd\u30fc\u30c8\uff1a\u30dd\u30a4\u30f3\u30bf\\(i\\)\u3068\\(j\\)\u306f\u5b9a\u6570\u91cf\u306e\u8ffd\u52a0\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u5b89\u5b9a\u30bd\u30fc\u30c8\uff1a\u633f\u5165\u64cd\u4f5c\u4e2d\u3001\u7b49\u3057\u3044\u8981\u7d20\u306e\u53f3\u5074\u306b\u8981\u7d20\u3092\u633f\u5165\u3057\u3001\u9806\u5e8f\u3092\u5909\u66f4\u3057\u307e\u305b\u3093\u3002
        "},{"location":"chapter_sorting/insertion_sort/#1143","title":"11.4.3 \u00a0 \u633f\u5165\u30bd\u30fc\u30c8\u306e\u5229\u70b9","text":"

        \u633f\u5165\u30bd\u30fc\u30c8\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n^2)\\)\u3067\u3001\u6b21\u306b\u5b66\u7fd2\u3059\u308b\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n \\log n)\\)\u3067\u3059\u3002\u633f\u5165\u30bd\u30fc\u30c8\u306f\u3088\u308a\u9ad8\u3044\u6642\u9593\u8a08\u7b97\u91cf\u3092\u6301\u3061\u307e\u3059\u304c\u3001\u5c0f\u3055\u306a\u5165\u529b\u30b5\u30a4\u30ba\u3067\u306f\u901a\u5e38\u3088\u308a\u9ad8\u901f\u3067\u3059\u3002

        \u3053\u306e\u7d50\u8ad6\u306f\u7dda\u5f62\u63a2\u7d22\u3068\u4e8c\u5206\u63a2\u7d22\u306e\u7d50\u8ad6\u3068\u4f3c\u3066\u3044\u307e\u3059\u3002\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(n \\log n)\\)\u3067\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306b\u57fa\u3065\u304f\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306a\u3069\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u591a\u304f\u306e\u5834\u5408\u3088\u308a\u591a\u304f\u306e\u5358\u4f4d\u64cd\u4f5c\u3092\u542b\u307f\u307e\u3059\u3002\u5c0f\u3055\u306a\u5165\u529b\u30b5\u30a4\u30ba\u3067\u306f\u3001\\(n^2\\)\u3068\\(n \\log n\\)\u306e\u6570\u5024\u306f\u8fd1\u304f\u3001\u8a08\u7b97\u91cf\u304c\u652f\u914d\u7684\u3067\u306a\u304f\u3001\u30e9\u30a6\u30f3\u30c9\u3042\u305f\u308a\u306e\u5358\u4f4d\u64cd\u4f5c\u6570\u304c\u6c7a\u5b9a\u7684\u306a\u5f79\u5272\u3092\u679c\u305f\u3057\u307e\u3059\u3002

        \u5b9f\u969b\u3001\u591a\u304f\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\uff08Java\u306a\u3069\uff09\u306f\u3001\u7d44\u307f\u8fbc\u307f\u30bd\u30fc\u30c8\u95a2\u6570\u5185\u3067\u633f\u5165\u30bd\u30fc\u30c8\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002\u4e00\u822c\u7684\u306a\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\uff1a\u9577\u3044\u914d\u5217\u306b\u5bfe\u3057\u3066\u306f\u3001\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306a\u3069\u306e\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306b\u57fa\u3065\u304f\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u4f7f\u7528\u3057\u3001\u77ed\u3044\u914d\u5217\u306b\u5bfe\u3057\u3066\u306f\u633f\u5165\u30bd\u30fc\u30c8\u3092\u76f4\u63a5\u4f7f\u7528\u3057\u307e\u3059\u3002

        \u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u3001\u9078\u629e\u30bd\u30fc\u30c8\u3001\u633f\u5165\u30bd\u30fc\u30c8\u306f\u3059\u3079\u3066\u6642\u9593\u8a08\u7b97\u91cf\\(O(n^2)\\)\u3092\u6301\u3061\u307e\u3059\u304c\u3001\u5b9f\u969b\u306b\u306f\u3001\u633f\u5165\u30bd\u30fc\u30c8\u306f\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u3084\u9078\u629e\u30bd\u30fc\u30c8\u3088\u308a\u3082\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u4e3b\u306a\u7406\u7531\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002

        • \u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u306f\u8981\u7d20\u4ea4\u63db\u306b\u57fa\u3065\u304d\u3001\u4e00\u6642\u5909\u6570\u306e\u4f7f\u7528\u304c\u5fc5\u8981\u3067\u30013\u3064\u306e\u5358\u4f4d\u64cd\u4f5c\u3092\u542b\u307f\u307e\u3059\uff1b\u633f\u5165\u30bd\u30fc\u30c8\u306f\u8981\u7d20\u4ee3\u5165\u306b\u57fa\u3065\u304d\u30011\u3064\u306e\u5358\u4f4d\u64cd\u4f5c\u306e\u307f\u304c\u5fc5\u8981\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u306e\u8a08\u7b97\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u306f\u4e00\u822c\u7684\u306b\u633f\u5165\u30bd\u30fc\u30c8\u3088\u308a\u3082\u9ad8\u304f\u306a\u308a\u307e\u3059\u3002
        • \u9078\u629e\u30bd\u30fc\u30c8\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\u5e38\u306b\\(O(n^2)\\)\u3067\u3059\u3002\u90e8\u5206\u7684\u306b\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u30c7\u30fc\u30bf\u306e\u30bb\u30c3\u30c8\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u633f\u5165\u30bd\u30fc\u30c8\u306f\u901a\u5e38\u9078\u629e\u30bd\u30fc\u30c8\u3088\u308a\u3082\u52b9\u7387\u7684\u3067\u3059\u3002
        • \u9078\u629e\u30bd\u30fc\u30c8\u306f\u4e0d\u5b89\u5b9a\u3067\u3001\u30de\u30eb\u30c1\u30ec\u30d9\u30eb\u30bd\u30fc\u30c8\u306b\u9069\u7528\u3067\u304d\u307e\u305b\u3093\u3002
        "},{"location":"chapter_sorting/merge_sort/","title":"11.6 \u00a0 \u30de\u30fc\u30b8\u30bd\u30fc\u30c8","text":"

        \u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u306f\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306b\u57fa\u3065\u304f\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3001\u4e0b\u56f3\u306b\u793a\u3059\u300c\u5206\u5272\u300d\u3068\u300c\u30de\u30fc\u30b8\u300d\u30d5\u30a7\u30fc\u30ba\u3092\u542b\u307f\u307e\u3059\u3002

        1. \u5206\u5272\u30d5\u30a7\u30fc\u30ba\uff1a\u4e2d\u70b9\u304b\u3089\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u5206\u5272\u3057\u3001\u9577\u3044\u914d\u5217\u306e\u30bd\u30fc\u30c8\u554f\u984c\u3092\u3088\u308a\u77ed\u3044\u914d\u5217\u306b\u5909\u63db\u3057\u307e\u3059\u3002
        2. \u30de\u30fc\u30b8\u30d5\u30a7\u30fc\u30ba\uff1a\u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c1\u306b\u306a\u3063\u305f\u3068\u304d\u306b\u5206\u5272\u3092\u505c\u6b62\u3057\u3001\u305d\u306e\u5f8c\u30de\u30fc\u30b8\u3092\u958b\u59cb\u3057\u307e\u3059\u30022\u3064\u306e\u77ed\u3044\u30bd\u30fc\u30c8\u6e08\u307f\u914d\u5217\u3092\u9023\u7d9a\u7684\u306b\u3088\u308a\u9577\u3044\u30bd\u30fc\u30c8\u6e08\u307f\u914d\u5217\u306b\u30de\u30fc\u30b8\u3057\u3001\u30d7\u30ed\u30bb\u30b9\u304c\u5b8c\u4e86\u3059\u308b\u307e\u3067\u7d9a\u884c\u3057\u307e\u3059\u3002

        \u56f3 11-10 \u00a0 The divide and merge phases of merge sort

        "},{"location":"chapter_sorting/merge_sort/#1161","title":"11.6.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30ef\u30fc\u30af\u30d5\u30ed\u30fc","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u300c\u5206\u5272\u30d5\u30a7\u30fc\u30ba\u300d\u306f\u4e2d\u70b9\u304b\u3089\u914d\u5217\u3092\u4e0a\u304b\u3089\u4e0b\u306b2\u3064\u306e\u30b5\u30d6\u914d\u5217\u306b\u518d\u5e30\u7684\u306b\u5206\u5272\u3057\u307e\u3059\u3002

        1. \u4e2d\u70b9mid\u3092\u8a08\u7b97\u3057\u3001\u5de6\u30b5\u30d6\u914d\u5217\uff08\u533a\u9593[left, mid]\uff09\u3068\u53f3\u30b5\u30d6\u914d\u5217\uff08\u533a\u9593[mid + 1, right]\uff09\u3092\u518d\u5e30\u7684\u306b\u5206\u5272\u3057\u307e\u3059\u3002
        2. \u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c1\u306b\u306a\u308b\u307e\u3067\u30b9\u30c6\u30c3\u30d71.\u3092\u518d\u5e30\u7684\u306b\u7d9a\u884c\u3057\u3001\u305d\u306e\u5f8c\u505c\u6b62\u3057\u307e\u3059\u3002

        \u300c\u30de\u30fc\u30b8\u30d5\u30a7\u30fc\u30ba\u300d\u306f\u5de6\u3068\u53f3\u306e\u30b5\u30d6\u914d\u5217\u3092\u4e0b\u304b\u3089\u4e0a\u306b\u30bd\u30fc\u30c8\u6e08\u307f\u914d\u5217\u306b\u7d50\u5408\u3057\u307e\u3059\u3002\u91cd\u8981\u306a\u306e\u306f\u3001\u30de\u30fc\u30b8\u304c\u9577\u30551\u306e\u30b5\u30d6\u914d\u5217\u304b\u3089\u958b\u59cb\u3055\u308c\u3001\u30de\u30fc\u30b8\u30d5\u30a7\u30fc\u30ba\u4e2d\u306b\u5404\u30b5\u30d6\u914d\u5217\u304c\u30bd\u30fc\u30c8\u3055\u308c\u308b\u3053\u3068\u3067\u3059\u3002

        <1><2><3><4><5><6><7><8><9><10>

        \u56f3 11-11 \u00a0 Merge sort process

        \u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u306e\u518d\u5e30\u9806\u5e8f\u306f\u4e8c\u5206\u6728\u306e\u5f8c\u9806\u6a2a\u65ad\u3068\u4e00\u81f4\u3059\u308b\u3053\u3068\u304c\u89b3\u5bdf\u3067\u304d\u307e\u3059\u3002

        • \u5f8c\u9806\u6a2a\u65ad\uff1a\u307e\u305a\u5de6\u306e\u30b5\u30d6\u30c4\u30ea\u30fc\u3092\u518d\u5e30\u7684\u306b\u6a2a\u65ad\u3057\u3001\u6b21\u306b\u53f3\u306e\u30b5\u30d6\u30c4\u30ea\u30fc\u3092\u6a2a\u65ad\u3057\u3001\u6700\u5f8c\u306b\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u51e6\u7406\u3057\u307e\u3059\u3002
        • \u30de\u30fc\u30b8\u30bd\u30fc\u30c8\uff1a\u307e\u305a\u5de6\u306e\u30b5\u30d6\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u51e6\u7406\u3057\u3001\u6b21\u306b\u53f3\u306e\u30b5\u30d6\u914d\u5217\u3092\u51e6\u7406\u3057\u3001\u6700\u5f8c\u306b\u30de\u30fc\u30b8\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002

        \u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u306e\u5b9f\u88c5\u306f\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u306b\u793a\u3055\u308c\u307e\u3059\u3002nums\u3067\u30de\u30fc\u30b8\u3055\u308c\u308b\u533a\u9593\u306f[left, right]\u3067\u3001tmp\u306e\u5bfe\u5fdc\u3059\u308b\u533a\u9593\u306f[0, right - left]\u3067\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby merge_sort.py
        def merge(nums: list[int], left: int, mid: int, right: int):\n    \"\"\"\u5de6\u30b5\u30d6\u914d\u5217\u3068\u53f3\u30b5\u30d6\u914d\u5217\u3092\u30de\u30fc\u30b8\"\"\"\n    # \u5de6\u30b5\u30d6\u914d\u5217\u533a\u9593\u306f [left, mid]\u3001\u53f3\u30b5\u30d6\u914d\u5217\u533a\u9593\u306f [mid+1, right]\n    # \u4e00\u6642\u914d\u5217 tmp \u3092\u4f5c\u6210\u3057\u3066\u30de\u30fc\u30b8\u7d50\u679c\u3092\u683c\u7d0d\n    tmp = [0] * (right - left + 1)\n    # \u5de6\u53f3\u30b5\u30d6\u914d\u5217\u306e\u958b\u59cb\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u521d\u671f\u5316\n    i, j, k = left, mid + 1, 0\n    # \u4e21\u65b9\u306e\u30b5\u30d6\u914d\u5217\u306b\u8981\u7d20\u304c\u6b8b\u3063\u3066\u3044\u308b\u9593\u3001\u3088\u308a\u5c0f\u3055\u3044\u8981\u7d20\u3092\u4e00\u6642\u914d\u5217\u306b\u30b3\u30d4\u30fc\n    while i <= mid and j <= right:\n        if nums[i] <= nums[j]:\n            tmp[k] = nums[i]\n            i += 1\n        else:\n            tmp[k] = nums[j]\n            j += 1\n        k += 1\n    # \u6b8b\u3063\u305f\u5de6\u53f3\u30b5\u30d6\u914d\u5217\u306e\u8981\u7d20\u3092\u4e00\u6642\u914d\u5217\u306b\u30b3\u30d4\u30fc\n    while i <= mid:\n        tmp[k] = nums[i]\n        i += 1\n        k += 1\n    while j <= right:\n        tmp[k] = nums[j]\n        j += 1\n        k += 1\n    # \u4e00\u6642\u914d\u5217 tmp \u306e\u8981\u7d20\u3092\u5143\u306e\u914d\u5217 nums \u306e\u5bfe\u5fdc\u3059\u308b\u533a\u9593\u306b\u30b3\u30d4\u30fc\u30d0\u30c3\u30af\n    for k in range(0, len(tmp)):\n        nums[left + k] = tmp[k]\n\ndef merge_sort(nums: list[int], left: int, right: int):\n    \"\"\"\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\"\"\"\n    # \u7d42\u4e86\u6761\u4ef6\n    if left >= right:\n        return  # \u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c1\u306e\u3068\u304d\u306b\u518d\u5e30\u3092\u7d42\u4e86\n    # \u5206\u5272\u6bb5\u968e\n    mid = left + (right - left) // 2  # \u4e2d\u70b9\u3092\u8a08\u7b97\n    merge_sort(nums, left, mid)  # \u5de6\u30b5\u30d6\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u51e6\u7406\n    merge_sort(nums, mid + 1, right)  # \u53f3\u30b5\u30d6\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u51e6\u7406\n    # \u30de\u30fc\u30b8\u6bb5\u968e\n    merge(nums, left, mid, right)\n
        merge_sort.cpp
        /* \u5de6\u30b5\u30d6\u914d\u5217\u3068\u53f3\u30b5\u30d6\u914d\u5217\u3092\u30de\u30fc\u30b8 */\nvoid merge(vector<int> &nums, int left, int mid, int right) {\n    // \u5de6\u30b5\u30d6\u914d\u5217\u306e\u533a\u9593\u306f[left, mid]\u3001\u53f3\u30b5\u30d6\u914d\u5217\u306e\u533a\u9593\u306f[mid+1, right]\n    // \u30de\u30fc\u30b8\u7d50\u679c\u3092\u4fdd\u5b58\u3059\u308b\u4e00\u6642\u914d\u5217tmp\u3092\u4f5c\u6210\n    vector<int> tmp(right - left + 1);\n    // \u5de6\u53f3\u30b5\u30d6\u914d\u5217\u306e\u958b\u59cb\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u521d\u671f\u5316\n    int i = left, j = mid + 1, k = 0;\n    // \u4e21\u30b5\u30d6\u914d\u5217\u306b\u8981\u7d20\u304c\u3042\u308b\u9593\u3001\u5c0f\u3055\u3044\u65b9\u306e\u8981\u7d20\u3092\u4e00\u6642\u914d\u5217\u306b\u30b3\u30d4\u30fc\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++];\n        else\n            tmp[k++] = nums[j++];\n    }\n    // \u5de6\u53f3\u30b5\u30d6\u914d\u5217\u306e\u6b8b\u308a\u306e\u8981\u7d20\u3092\u4e00\u6642\u914d\u5217\u306b\u30b3\u30d4\u30fc\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // \u4e00\u6642\u914d\u5217tmp\u306e\u8981\u7d20\u3092\u5143\u306e\u914d\u5217nums\u306e\u5bfe\u5fdc\u3059\u308b\u533a\u9593\u306b\u30b3\u30d4\u30fc\n    for (k = 0; k < tmp.size(); k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* \u30de\u30fc\u30b8\u30bd\u30fc\u30c8 */\nvoid mergeSort(vector<int> &nums, int left, int right) {\n    // \u7d42\u4e86\u6761\u4ef6\n    if (left >= right)\n        return; // \u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c1\u306e\u6642\u3001\u518d\u5e30\u3092\u7d42\u4e86\n    // \u5206\u5272\u6bb5\u968e\n    int mid = left + (right - left) / 2;    // \u4e2d\u70b9\u3092\u8a08\u7b97\n    mergeSort(nums, left, mid);      // \u5de6\u30b5\u30d6\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u51e6\u7406\n    mergeSort(nums, mid + 1, right); // \u53f3\u30b5\u30d6\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u51e6\u7406\n    // \u30de\u30fc\u30b8\u6bb5\u968e\n    merge(nums, left, mid, right);\n}\n
        merge_sort.java
        /* \u5de6\u90e8\u5206\u914d\u5217\u3068\u53f3\u90e8\u5206\u914d\u5217\u3092\u30de\u30fc\u30b8 */\nvoid merge(int[] nums, int left, int mid, int right) {\n    // \u5de6\u90e8\u5206\u914d\u5217\u533a\u9593\u306f [left, mid]\u3001\u53f3\u90e8\u5206\u914d\u5217\u533a\u9593\u306f [mid+1, right]\n    // \u4e00\u6642\u914d\u5217 tmp \u3092\u4f5c\u6210\u3057\u3066\u30de\u30fc\u30b8\u7d50\u679c\u3092\u683c\u7d0d\n    int[] tmp = new int[right - left + 1];\n    // \u5de6\u53f3\u90e8\u5206\u914d\u5217\u306e\u958b\u59cb\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u521d\u671f\u5316\n    int i = left, j = mid + 1, k = 0;\n    // \u4e21\u90e8\u5206\u914d\u5217\u306b\u307e\u3060\u8981\u7d20\u304c\u3042\u308b\u9593\u3001\u6bd4\u8f03\u3057\u3066\u3088\u308a\u5c0f\u3055\u3044\u8981\u7d20\u3092\u4e00\u6642\u914d\u5217\u306b\u30b3\u30d4\u30fc\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++];\n        else\n            tmp[k++] = nums[j++];\n    }\n    // \u5de6\u53f3\u90e8\u5206\u914d\u5217\u306e\u6b8b\u308a\u306e\u8981\u7d20\u3092\u4e00\u6642\u914d\u5217\u306b\u30b3\u30d4\u30fc\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // \u4e00\u6642\u914d\u5217 tmp \u306e\u8981\u7d20\u3092\u5143\u306e\u914d\u5217 nums \u306e\u5bfe\u5fdc\u3059\u308b\u533a\u9593\u306b\u30b3\u30d4\u30fc\u30d0\u30c3\u30af\n    for (k = 0; k < tmp.length; k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* \u30de\u30fc\u30b8\u30bd\u30fc\u30c8 */\nvoid mergeSort(int[] nums, int left, int right) {\n    // \u7d42\u4e86\u6761\u4ef6\n    if (left >= right)\n        return; // \u90e8\u5206\u914d\u5217\u306e\u9577\u3055\u304c 1 \u306e\u3068\u304d\u518d\u5e30\u3092\u7d42\u4e86\n    // \u5206\u5272\u6bb5\u968e\n    int mid = left + (right - left) / 2; // \u4e2d\u70b9\u3092\u8a08\u7b97\n    mergeSort(nums, left, mid); // \u5de6\u90e8\u5206\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u51e6\u7406\n    mergeSort(nums, mid + 1, right); // \u53f3\u90e8\u5206\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u51e6\u7406\n    // \u30de\u30fc\u30b8\u6bb5\u968e\n    merge(nums, left, mid, right);\n}\n
        merge_sort.cs
        [class]{merge_sort}-[func]{Merge}\n\n[class]{merge_sort}-[func]{MergeSort}\n
        merge_sort.go
        [class]{}-[func]{merge}\n\n[class]{}-[func]{mergeSort}\n
        merge_sort.swift
        [class]{}-[func]{merge}\n\n[class]{}-[func]{mergeSort}\n
        merge_sort.js
        [class]{}-[func]{merge}\n\n[class]{}-[func]{mergeSort}\n
        merge_sort.ts
        [class]{}-[func]{merge}\n\n[class]{}-[func]{mergeSort}\n
        merge_sort.dart
        [class]{}-[func]{merge}\n\n[class]{}-[func]{mergeSort}\n
        merge_sort.rs
        [class]{}-[func]{merge}\n\n[class]{}-[func]{merge_sort}\n
        merge_sort.c
        [class]{}-[func]{merge}\n\n[class]{}-[func]{mergeSort}\n
        merge_sort.kt
        [class]{}-[func]{merge}\n\n[class]{}-[func]{mergeSort}\n
        merge_sort.rb
        [class]{}-[func]{merge}\n\n[class]{}-[func]{merge_sort}\n
        "},{"location":"chapter_sorting/merge_sort/#1162","title":"11.6.2 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7279\u6027","text":"
        • \\(O(n \\log n)\\)\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3001\u975e\u9069\u5fdc\u30bd\u30fc\u30c8\uff1a\u5206\u5272\u306b\u3088\u308a\u9ad8\u3055\\(\\log n\\)\u306e\u518d\u5e30\u30c4\u30ea\u30fc\u304c\u4f5c\u6210\u3055\u308c\u3001\u5404\u5c64\u3067\u5408\u8a08\\(n\\)\u56de\u306e\u64cd\u4f5c\u3092\u30de\u30fc\u30b8\u3057\u3001\u5168\u4f53\u7684\u306a\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n \\log n)\\)\u3068\u306a\u308a\u307e\u3059\u3002
        • \\(O(n)\\)\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u3001\u975e\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u30bd\u30fc\u30c8\uff1a\u518d\u5e30\u6df1\u5ea6\u306f\\(\\log n\\)\u3067\u3001\\(O(\\log n)\\)\u306e\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u30de\u30fc\u30b8\u64cd\u4f5c\u306b\u306f\u88dc\u52a9\u914d\u5217\u304c\u5fc5\u8981\u3067\u3001\u8ffd\u52a0\u306e\\(O(n)\\)\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u5b89\u5b9a\u30bd\u30fc\u30c8\uff1a\u30de\u30fc\u30b8\u30d7\u30ed\u30bb\u30b9\u4e2d\u3001\u7b49\u3057\u3044\u8981\u7d20\u306e\u9806\u5e8f\u306f\u5909\u66f4\u3055\u308c\u307e\u305b\u3093\u3002
        "},{"location":"chapter_sorting/merge_sort/#1163","title":"11.6.3 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30bd\u30fc\u30c8","text":"

        \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u5834\u5408\u3001\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u306f\u4ed6\u306e\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3088\u308a\u3082\u5927\u304d\u306a\u5229\u70b9\u304c\u3042\u308a\u307e\u3059\u3002\u9023\u7d50\u30ea\u30b9\u30c8\u30bd\u30fc\u30c8\u30bf\u30b9\u30af\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u3092\\(O(1)\\)\u306b\u6700\u9069\u5316\u3067\u304d\u307e\u3059\u3002

        • \u5206\u5272\u30d5\u30a7\u30fc\u30ba\uff1a\u300c\u518d\u5e30\u300d\u306e\u4ee3\u308f\u308a\u306b\u300c\u53cd\u5fa9\u300d\u3092\u4f7f\u7528\u3057\u3066\u9023\u7d50\u30ea\u30b9\u30c8\u5206\u5272\u4f5c\u696d\u3092\u5b9f\u884c\u3067\u304d\u308b\u305f\u3081\u3001\u518d\u5e30\u3067\u4f7f\u7528\u3055\u308c\u308b\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u7bc0\u7d04\u3067\u304d\u307e\u3059\u3002
        • \u30de\u30fc\u30b8\u30d5\u30a7\u30fc\u30ba\uff1a\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u306f\u3001\u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664\u64cd\u4f5c\u306f\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u5909\u66f4\u3059\u308b\u3053\u3068\u3067\u5b9f\u73fe\u3067\u304d\u308b\u305f\u3081\u3001\u30de\u30fc\u30b8\u30d5\u30a7\u30fc\u30ba\uff082\u3064\u306e\u77ed\u3044\u9806\u5e8f\u4ed8\u304d\u30ea\u30b9\u30c8\u30921\u3064\u306e\u9577\u3044\u9806\u5e8f\u4ed8\u304d\u30ea\u30b9\u30c8\u306b\u7d50\u5408\uff09\u4e2d\u306b\u8ffd\u52a0\u306e\u30ea\u30b9\u30c8\u3092\u4f5c\u6210\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u305b\u3093\u3002

        \u5b9f\u88c5\u306e\u8a73\u7d30\u306f\u6bd4\u8f03\u7684\u8907\u96d1\u3067\u3001\u8208\u5473\u306e\u3042\u308b\u8aad\u8005\u306f\u95a2\u9023\u8cc7\u6599\u3092\u53c2\u7167\u3057\u3066\u5b66\u7fd2\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        "},{"location":"chapter_sorting/quick_sort/","title":"11.5 \u00a0 \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8","text":"

        \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306f\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306b\u57fa\u3065\u304f\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3001\u305d\u306e\u52b9\u7387\u6027\u3068\u5e45\u5e83\u3044\u5fdc\u7528\u3067\u77e5\u3089\u308c\u3066\u3044\u307e\u3059\u3002

        \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306e\u30b3\u30a2\u64cd\u4f5c\u306f\u300c\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u300d\u3067\u3001\u914d\u5217\u304b\u3089\u8981\u7d20\u3092\u300c\u30d4\u30dc\u30c3\u30c8\u300d\u3068\u3057\u3066\u9078\u629e\u3057\u3001\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5c0f\u3055\u3044\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u305d\u306e\u5de6\u5074\u306b\u79fb\u52d5\u3057\u3001\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5927\u304d\u3044\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u305d\u306e\u53f3\u5074\u306b\u79fb\u52d5\u3059\u308b\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u3066\u3044\u307e\u3059\u3002\u5177\u4f53\u7684\u306b\u3001\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u306e\u30d7\u30ed\u30bb\u30b9\u306f\u4e0b\u56f3\u306b\u793a\u3055\u308c\u307e\u3059\u3002

        1. \u914d\u5217\u306e\u6700\u3082\u5de6\u306e\u8981\u7d20\u3092\u30d4\u30dc\u30c3\u30c8\u3068\u3057\u3066\u9078\u629e\u3057\u30012\u3064\u306e\u30dd\u30a4\u30f3\u30bfi\u3068j\u3092\u521d\u671f\u5316\u3057\u3066\u914d\u5217\u306e\u4e21\u7aef\u3092\u305d\u308c\u305e\u308c\u6307\u3059\u3088\u3046\u306b\u3057\u307e\u3059\u3002
        2. \u5404\u30e9\u30a6\u30f3\u30c9\u3067i\uff08j\uff09\u3092\u4f7f\u7528\u3057\u3066\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5927\u304d\u3044\uff08\u5c0f\u3055\u3044\uff09\u6700\u521d\u306e\u8981\u7d20\u3092\u63a2\u7d22\u3057\u3001\u6b21\u306b\u3053\u308c\u30892\u3064\u306e\u8981\u7d20\u3092\u4ea4\u63db\u3059\u308b\u30eb\u30fc\u30d7\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002
        3. i\u3068j\u304c\u51fa\u4f1a\u3046\u307e\u3067\u30b9\u30c6\u30c3\u30d72.\u3092\u7e70\u308a\u8fd4\u3057\u3001\u6700\u5f8c\u306b\u30d4\u30dc\u30c3\u30c8\u30922\u3064\u306e\u30b5\u30d6\u914d\u5217\u306e\u5883\u754c\u306b\u4ea4\u63db\u3057\u307e\u3059\u3002
        <1><2><3><4><5><6><7><8><9>

        \u56f3 11-8 \u00a0 Pivot division process

        \u30d4\u30dc\u30c3\u30c8\u5206\u5272\u5f8c\u3001\u5143\u306e\u914d\u5217\u306f3\u3064\u306e\u90e8\u5206\u306b\u5206\u5272\u3055\u308c\u307e\u3059\uff1a\u5de6\u30b5\u30d6\u914d\u5217\u3001\u30d4\u30dc\u30c3\u30c8\u3001\u53f3\u30b5\u30d6\u914d\u5217\u3067\u3001\u300c\u5de6\u30b5\u30d6\u914d\u5217\u306e\u4efb\u610f\u306e\u8981\u7d20 \\(\\leq\\) \u30d4\u30dc\u30c3\u30c8 \\(\\leq\\) \u53f3\u30b5\u30d6\u914d\u5217\u306e\u4efb\u610f\u306e\u8981\u7d20\u300d\u3092\u6e80\u305f\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u3053\u308c\u30892\u3064\u306e\u30b5\u30d6\u914d\u5217\u306e\u307f\u3092\u30bd\u30fc\u30c8\u3059\u308c\u3070\u3088\u3044\u306e\u3067\u3059\u3002

        \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306e\u5206\u5272\u7d71\u6cbb\u6226\u7565

        \u30d4\u30dc\u30c3\u30c8\u5206\u5272\u306e\u672c\u8cea\u306f\u3001\u3088\u308a\u9577\u3044\u914d\u5217\u306e\u30bd\u30fc\u30c8\u554f\u984c\u3092\u3088\u308a\u77ed\u30442\u3064\u306e\u914d\u5217\u306b\u7c21\u7d20\u5316\u3059\u308b\u3053\u3068\u3067\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
        def partition(self, nums: list[int], left: int, right: int) -> int:\n    \"\"\"\u5206\u5272\"\"\"\n    # nums[left] \u3092\u30d4\u30dc\u30c3\u30c8\u3068\u3057\u3066\u4f7f\u7528\n    i, j = left, right\n    while i < j:\n        while i < j and nums[j] >= nums[left]:\n            j -= 1  # \u53f3\u304b\u3089\u5de6\u3078\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5c0f\u3055\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u63a2\u3059\n        while i < j and nums[i] <= nums[left]:\n            i += 1  # \u5de6\u304b\u3089\u53f3\u3078\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5927\u304d\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u63a2\u3059\n        # \u8981\u7d20\u3092\u4ea4\u63db\n        nums[i], nums[j] = nums[j], nums[i]\n    # \u30d4\u30dc\u30c3\u30c8\u30922\u3064\u306e\u30b5\u30d6\u914d\u5217\u306e\u5883\u754c\u306b\u4ea4\u63db\n    nums[i], nums[left] = nums[left], nums[i]\n    return i  # \u30d4\u30dc\u30c3\u30c8\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n
        quick_sort.cpp
        /* \u5206\u5272 */\nint partition(vector<int> &nums, int left, int right) {\n    // nums[left]\u3092\u30d4\u30dc\u30c3\u30c8\u3068\u3057\u3066\u4f7f\u7528\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--; // \u53f3\u304b\u3089\u5de6\u3078\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5c0f\u3055\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u691c\u7d22\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u5de6\u304b\u3089\u53f3\u3078\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5927\u304d\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u691c\u7d22\n        swap(nums, i, j); // \u3053\u308c\u3089\u4e8c\u3064\u306e\u8981\u7d20\u3092\u4ea4\u63db\n    }\n    swap(nums, i, left); // \u30d4\u30dc\u30c3\u30c8\u3092\u4e8c\u3064\u306e\u30b5\u30d6\u914d\u5217\u306e\u5883\u754c\u306b\u4ea4\u63db\n    return i;            // \u30d4\u30dc\u30c3\u30c8\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n}\n
        quick_sort.java
        /* \u8981\u7d20\u3092\u4ea4\u63db */\nvoid swap(int[] nums, int i, int j) {\n    int tmp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = tmp;\n}\n\n/* \u5206\u5272 */\nint partition(int[] nums, int left, int right) {\n    // nums[left] \u3092\u57fa\u6e96\u5024\u3068\u3057\u3066\u4f7f\u7528\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // \u53f3\u304b\u3089\u5de6\u3078\u3001\u57fa\u6e96\u5024\u3088\u308a\u5c0f\u3055\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u691c\u7d22\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u5de6\u304b\u3089\u53f3\u3078\u3001\u57fa\u6e96\u5024\u3088\u308a\u5927\u304d\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u691c\u7d22\n        swap(nums, i, j); // \u3053\u308c\u30892\u3064\u306e\u8981\u7d20\u3092\u4ea4\u63db\n    }\n    swap(nums, i, left);  // \u57fa\u6e96\u5024\u30922\u3064\u306e\u90e8\u5206\u914d\u5217\u306e\u5883\u754c\u306b\u4ea4\u63db\n    return i;             // \u57fa\u6e96\u5024\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n}\n
        quick_sort.cs
        [class]{quickSort}-[func]{Swap}\n\n[class]{quickSort}-[func]{Partition}\n
        quick_sort.go
        [class]{quickSort}-[func]{partition}\n
        quick_sort.swift
        [class]{}-[func]{partition}\n
        quick_sort.js
        [class]{QuickSort}-[func]{swap}\n\n[class]{QuickSort}-[func]{partition}\n
        quick_sort.ts
        [class]{QuickSort}-[func]{swap}\n\n[class]{QuickSort}-[func]{partition}\n
        quick_sort.dart
        [class]{QuickSort}-[func]{_swap}\n\n[class]{QuickSort}-[func]{_partition}\n
        quick_sort.rs
        [class]{QuickSort}-[func]{partition}\n
        quick_sort.c
        [class]{}-[func]{swap}\n\n[class]{}-[func]{partition}\n
        quick_sort.kt
        [class]{}-[func]{swap}\n\n[class]{}-[func]{partition}\n
        quick_sort.rb
        [class]{QuickSort}-[func]{partition}\n
        "},{"location":"chapter_sorting/quick_sort/#1151","title":"11.5.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d7\u30ed\u30bb\u30b9","text":"

        \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306e\u5168\u4f53\u7684\u306a\u30d7\u30ed\u30bb\u30b9\u306f\u4e0b\u56f3\u306b\u793a\u3055\u308c\u307e\u3059\u3002

        1. \u307e\u305a\u3001\u5143\u306e\u914d\u5217\u306b\u5bfe\u3057\u3066\u300c\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u300d\u3092\u5b9f\u884c\u3057\u3001\u672a\u30bd\u30fc\u30c8\u306e\u5de6\u3068\u53f3\u306e\u30b5\u30d6\u914d\u5217\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002
        2. \u6b21\u306b\u3001\u5de6\u3068\u53f3\u306e\u30b5\u30d6\u914d\u5217\u306b\u5bfe\u3057\u3066\u305d\u308c\u305e\u308c\u518d\u5e30\u7684\u306b\u300c\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u300d\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002
        3. \u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c1\u306b\u306a\u308b\u307e\u3067\u518d\u5e30\u3092\u7d9a\u3051\u3001\u914d\u5217\u5168\u4f53\u306e\u30bd\u30fc\u30c8\u3092\u5b8c\u4e86\u3057\u307e\u3059\u3002

        \u56f3 11-9 \u00a0 Quick sort process

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
        def quick_sort(self, nums: list[int], left: int, right: int):\n    \"\"\"\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\"\"\"\n    # \u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c1\u306e\u3068\u304d\u306b\u518d\u5e30\u3092\u7d42\u4e86\n    if left >= right:\n        return\n    # \u5206\u5272\n    pivot = self.partition(nums, left, right)\n    # \u5de6\u30b5\u30d6\u914d\u5217\u3068\u53f3\u30b5\u30d6\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u51e6\u7406\n    self.quick_sort(nums, left, pivot - 1)\n    self.quick_sort(nums, pivot + 1, right)\n
        quick_sort.cpp
        /* \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8 */\nvoid quickSort(vector<int> &nums, int left, int right) {\n    // \u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c1\u306e\u6642\u3001\u518d\u5e30\u3092\u7d42\u4e86\n    if (left >= right)\n        return;\n    // \u5206\u5272\n    int pivot = partition(nums, left, right);\n    // \u5de6\u30b5\u30d6\u914d\u5217\u3068\u53f3\u30b5\u30d6\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u51e6\u7406\n    quickSort(nums, left, pivot - 1);\n    quickSort(nums, pivot + 1, right);\n}\n
        quick_sort.java
        /* \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8 */\nvoid quickSort(int[] nums, int left, int right) {\n    // \u90e8\u5206\u914d\u5217\u306e\u9577\u3055\u304c 1 \u306e\u3068\u304d\u518d\u5e30\u3092\u7d42\u4e86\n    if (left >= right)\n        return;\n    // \u5206\u5272\n    int pivot = partition(nums, left, right);\n    // \u5de6\u90e8\u5206\u914d\u5217\u3068\u53f3\u90e8\u5206\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u51e6\u7406\n    quickSort(nums, left, pivot - 1);\n    quickSort(nums, pivot + 1, right);\n}\n
        quick_sort.cs
        [class]{quickSort}-[func]{QuickSort}\n
        quick_sort.go
        [class]{quickSort}-[func]{quickSort}\n
        quick_sort.swift
        [class]{}-[func]{quickSort}\n
        quick_sort.js
        [class]{QuickSort}-[func]{quickSort}\n
        quick_sort.ts
        [class]{QuickSort}-[func]{quickSort}\n
        quick_sort.dart
        [class]{QuickSort}-[func]{quickSort}\n
        quick_sort.rs
        [class]{QuickSort}-[func]{quick_sort}\n
        quick_sort.c
        [class]{}-[func]{quickSort}\n
        quick_sort.kt
        [class]{}-[func]{quickSort}\n
        quick_sort.rb
        [class]{QuickSort}-[func]{quick_sort}\n
        "},{"location":"chapter_sorting/quick_sort/#1152","title":"11.5.2 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7279\u5fb4","text":"
        • \\(O(n \\log n)\\)\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3001\u975e\u9069\u5fdc\u30bd\u30fc\u30c8\uff1a\u5e73\u5747\u7684\u306a\u30b1\u30fc\u30b9\u3067\u306f\u3001\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u306e\u518d\u5e30\u30ec\u30d9\u30eb\u306f\\(\\log n\\)\u3067\u3001\u30ec\u30d9\u30eb\u3042\u305f\u308a\u306e\u30eb\u30fc\u30d7\u306e\u7dcf\u6570\u306f\\(n\\)\u3067\u3042\u308a\u3001\u5168\u4f53\u3067\\(O(n \\log n)\\)\u306e\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u6700\u60aa\u306e\u5834\u5408\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u306f\u9577\u3055\\(n\\)\u306e\u914d\u5217\u3092\u9577\u3055\\(0\\)\u3068\\(n - 1\\)\u306e2\u3064\u306e\u30b5\u30d6\u914d\u5217\u306b\u5206\u5272\u3057\u3001\u518d\u5e30\u30ec\u30d9\u30eb\u6570\u304c\\(n\\)\u306b\u9054\u3059\u308b\u3068\u3001\u5404\u30ec\u30d9\u30eb\u306e\u30eb\u30fc\u30d7\u6570\u306f\\(n\\)\u3067\u3001\u4f7f\u7528\u3055\u308c\u308b\u7dcf\u6642\u9593\u306f\\(O(n^2)\\)\u3067\u3059\u3002
        • \\(O(n)\\)\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u3001\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u30bd\u30fc\u30c8\uff1a\u5165\u529b\u914d\u5217\u304c\u5b8c\u5168\u306b\u9006\u9806\u306e\u5834\u5408\u3001\u6700\u60aa\u306e\u518d\u5e30\u6df1\u5ea6\u306f\\(n\\)\u306b\u9054\u3057\u3001\\(O(n)\\)\u306e\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u30bd\u30fc\u30c8\u64cd\u4f5c\u306f\u8ffd\u52a0\u306e\u914d\u5217\u306e\u52a9\u3051\u306a\u3057\u306b\u5143\u306e\u914d\u5217\u3067\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002
        • \u975e\u5b89\u5b9a\u30bd\u30fc\u30c8\uff1a\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u306e\u6700\u7d42\u30b9\u30c6\u30c3\u30d7\u3067\u3001\u30d4\u30dc\u30c3\u30c8\u306f\u7b49\u3057\u3044\u8981\u7d20\u306e\u53f3\u5074\u306b\u4ea4\u63db\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        "},{"location":"chapter_sorting/quick_sort/#1153","title":"11.5.3 \u00a0 \u306a\u305c\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306f\u9ad8\u901f\u306a\u306e\u304b","text":"

        \u540d\u524d\u304c\u793a\u3059\u3088\u3046\u306b\u3001\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306f\u52b9\u7387\u6027\u306e\u9762\u3067\u4e00\u5b9a\u306e\u5229\u70b9\u3092\u6301\u3064\u3079\u304d\u3067\u3059\u3002\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306e\u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf\u306f\u300c\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u300d\u3084\u300c\u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\u300d\u3068\u540c\u3058\u3067\u3059\u304c\u3001\u4ee5\u4e0b\u306e\u7406\u7531\u3067\u4e00\u822c\u7684\u306b\u3088\u308a\u52b9\u7387\u7684\u3067\u3059\u3002

        • \u6700\u60aa\u30b1\u30fc\u30b9\u30b7\u30ca\u30ea\u30aa\u306e\u4f4e\u3044\u78ba\u7387\uff1a\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306e\u6700\u60aa\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n^2)\\)\u3067\u3001\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u307b\u3069\u5b89\u5b9a\u3057\u3066\u3044\u307e\u305b\u3093\u304c\u3001\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306f\\(O(n \\log n)\\)\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3067\u52d5\u4f5c\u3067\u304d\u307e\u3059\u3002
        • \u9ad8\u3044\u30ad\u30e3\u30c3\u30b7\u30e5\u5229\u7528\u7387\uff1a\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u64cd\u4f5c\u4e2d\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u30b5\u30d6\u914d\u5217\u5168\u4f53\u3092\u30ad\u30e3\u30c3\u30b7\u30e5\u306b\u30ed\u30fc\u30c9\u3067\u304d\u308b\u305f\u3081\u3001\u8981\u7d20\u306b\u3088\u308a\u52b9\u7387\u7684\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u307e\u3059\u3002\u5bfe\u7167\u7684\u306b\u3001\u300c\u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\u300d\u306a\u3069\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u8981\u7d20\u306b\u30b8\u30e3\u30f3\u30d7\u65b9\u5f0f\u3067\u30a2\u30af\u30bb\u30b9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u3053\u306e\u7279\u5fb4\u3092\u6b20\u3044\u3066\u3044\u307e\u3059\u3002
        • \u8a08\u7b97\u91cf\u306e\u5c0f\u3055\u306a\u5b9a\u6570\u4fc2\u6570\uff1a\u4e0a\u8a183\u3064\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u4e2d\u3067\u3001\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306f\u6bd4\u8f03\u3001\u4ee3\u5165\u3001\u4ea4\u63db\u306a\u3069\u306e\u64cd\u4f5c\u306e\u7dcf\u6570\u304c\u6700\u3082\u5c11\u306a\u3044\u3067\u3059\u3002\u3053\u308c\u306f\u300c\u633f\u5165\u30bd\u30fc\u30c8\u300d\u304c\u300c\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u300d\u3088\u308a\u3082\u9ad8\u901f\u306a\u7406\u7531\u3068\u4f3c\u3066\u3044\u307e\u3059\u3002
        "},{"location":"chapter_sorting/quick_sort/#1154","title":"11.5.4 \u00a0 \u30d4\u30dc\u30c3\u30c8\u6700\u9069\u5316","text":"

        \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306e\u6642\u9593\u52b9\u7387\u306f\u7279\u5b9a\u306e\u5165\u529b\u3067\u52a3\u5316\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u5165\u529b\u914d\u5217\u304c\u5b8c\u5168\u306b\u9006\u9806\u306e\u5834\u5408\u3001\u6700\u3082\u5de6\u306e\u8981\u7d20\u3092\u30d4\u30dc\u30c3\u30c8\u3068\u3057\u3066\u9078\u629e\u3059\u308b\u305f\u3081\u3001\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u5f8c\u3001\u30d4\u30dc\u30c3\u30c8\u306f\u914d\u5217\u306e\u53f3\u7aef\u306b\u4ea4\u63db\u3055\u308c\u3001\u5de6\u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c\\(n - 1\\)\u3001\u53f3\u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c\\(0\\)\u306b\u306a\u308a\u307e\u3059\u3002\u3053\u306e\u65b9\u6cd5\u3092\u7d9a\u3051\u308b\u3068\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u3067\u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c\\(0\\)\u306b\u306a\u308a\u3001\u5206\u5272\u7d71\u6cbb\u6226\u7565\u304c\u5931\u6557\u3057\u3001\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306f\u300c\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u300d\u306b\u4f3c\u305f\u5f62\u306b\u52a3\u5316\u3057\u307e\u3059\u3002

        \u3053\u306e\u72b6\u6cc1\u3092\u907f\u3051\u308b\u305f\u3081\u3001\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u3067\u30d4\u30dc\u30c3\u30c8\u9078\u629e\u6226\u7565\u3092\u6700\u9069\u5316\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u8981\u7d20\u3092\u30e9\u30f3\u30c0\u30e0\u306b\u9078\u629e\u3057\u3066\u30d4\u30dc\u30c3\u30c8\u3068\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u904b\u304c\u60aa\u304f\u3001\u4e00\u8cab\u3057\u3066\u6700\u9069\u3067\u306a\u3044\u30d4\u30dc\u30c3\u30c8\u3092\u9078\u629e\u3057\u305f\u5834\u5408\u3001\u52b9\u7387\u306f\u307e\u3060\u6e80\u8db3\u3067\u304d\u307e\u305b\u3093\u3002

        \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306f\u901a\u5e38\u300c\u7591\u4f3c\u4e71\u6570\u300d\u3092\u751f\u6210\u3059\u308b\u3053\u3068\u306b\u6ce8\u610f\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002\u7591\u4f3c\u4e71\u6570\u30b7\u30fc\u30b1\u30f3\u30b9\u306b\u5bfe\u3057\u3066\u7279\u5b9a\u306e\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u3092\u69cb\u7bc9\u3059\u308b\u3068\u3001\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306e\u52b9\u7387\u306f\u307e\u3060\u52a3\u5316\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        \u3055\u3089\u306a\u308b\u6539\u5584\u306e\u305f\u3081\u30013\u3064\u306e\u5019\u88dc\u8981\u7d20\uff08\u901a\u5e38\u306f\u914d\u5217\u306e\u6700\u521d\u3001\u6700\u5f8c\u3001\u4e2d\u70b9\u306e\u8981\u7d20\uff09\u3092\u9078\u629e\u3057\u3001**\u3053\u308c\u30893\u3064\u306e\u5019\u88dc\u8981\u7d20\u306e\u4e2d\u592e\u5024\u3092\u30d4\u30dc\u30c3\u30c8\u3068\u3057\u3066\u4f7f\u7528**\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u65b9\u6cd5\u3067\u3001\u30d4\u30dc\u30c3\u30c8\u304c\u300c\u5c0f\u3055\u3059\u304e\u305a\u5927\u304d\u3059\u304e\u306a\u3044\u300d\u78ba\u7387\u304c\u5927\u5e45\u306b\u5897\u52a0\u3057\u307e\u3059\u3002\u3082\u3061\u308d\u3093\u3001\u3055\u3089\u306b\u591a\u304f\u306e\u5019\u88dc\u8981\u7d20\u3092\u9078\u629e\u3057\u3066\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5805\u7262\u6027\u3092\u3055\u3089\u306b\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u65b9\u6cd5\u306b\u3088\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(n^2)\\)\u306b\u52a3\u5316\u3059\u308b\u78ba\u7387\u304c\u5927\u5e45\u306b\u524a\u6e1b\u3055\u308c\u307e\u3059\u3002

        \u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
        def median_three(self, nums: list[int], left: int, mid: int, right: int) -> int:\n    \"\"\"3\u3064\u306e\u5019\u88dc\u8981\u7d20\u306e\u4e2d\u592e\u5024\u3092\u9078\u629e\"\"\"\n    l, m, r = nums[left], nums[mid], nums[right]\n    if (l <= m <= r) or (r <= m <= l):\n        return mid  # m \u306f l \u3068 r \u306e\u9593\n    if (m <= l <= r) or (r <= l <= m):\n        return left  # l \u306f m \u3068 r \u306e\u9593\n    return right\n\ndef partition(self, nums: list[int], left: int, right: int) -> int:\n    \"\"\"\u5206\u5272\uff08\u4e09\u70b9\u4e2d\u592e\u5024\uff09\"\"\"\n    # nums[left] \u3092\u30d4\u30dc\u30c3\u30c8\u3068\u3057\u3066\u4f7f\u7528\n    med = self.median_three(nums, left, (left + right) // 2, right)\n    # \u4e2d\u592e\u5024\u3092\u914d\u5217\u306e\u6700\u5de6\u7aef\u306b\u4ea4\u63db\n    nums[left], nums[med] = nums[med], nums[left]\n    # nums[left] \u3092\u30d4\u30dc\u30c3\u30c8\u3068\u3057\u3066\u4f7f\u7528\n    i, j = left, right\n    while i < j:\n        while i < j and nums[j] >= nums[left]:\n            j -= 1  # \u53f3\u304b\u3089\u5de6\u3078\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5c0f\u3055\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u63a2\u3059\n        while i < j and nums[i] <= nums[left]:\n            i += 1  # \u5de6\u304b\u3089\u53f3\u3078\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5927\u304d\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u63a2\u3059\n        # \u8981\u7d20\u3092\u4ea4\u63db\n        nums[i], nums[j] = nums[j], nums[i]\n    # \u30d4\u30dc\u30c3\u30c8\u30922\u3064\u306e\u30b5\u30d6\u914d\u5217\u306e\u5883\u754c\u306b\u4ea4\u63db\n    nums[i], nums[left] = nums[left], nums[i]\n    return i  # \u30d4\u30dc\u30c3\u30c8\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n
        quick_sort.cpp
        /* \u4e09\u3064\u306e\u5019\u88dc\u8981\u7d20\u306e\u4e2d\u592e\u5024\u3092\u9078\u629e */\nint medianThree(vector<int> &nums, int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m\u306fl\u3068r\u306e\u9593\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l\u306fm\u3068r\u306e\u9593\n    return right;\n}\n\n/* \u5206\u5272\uff08\u4e09\u3064\u306e\u4e2d\u592e\u5024\uff09 */\nint partition(vector<int> &nums, int left, int right) {\n    // \u4e09\u3064\u306e\u5019\u88dc\u8981\u7d20\u306e\u4e2d\u592e\u5024\u3092\u9078\u629e\n    int med = medianThree(nums, left, (left + right) / 2, right);\n    // \u4e2d\u592e\u5024\u3092\u914d\u5217\u306e\u6700\u5de6\u4f4d\u7f6e\u306b\u4ea4\u63db\n    swap(nums, left, med);\n    // nums[left]\u3092\u30d4\u30dc\u30c3\u30c8\u3068\u3057\u3066\u4f7f\u7528\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--; // \u53f3\u304b\u3089\u5de6\u3078\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5c0f\u3055\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u691c\u7d22\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u5de6\u304b\u3089\u53f3\u3078\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5927\u304d\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u691c\u7d22\n        swap(nums, i, j); // \u3053\u308c\u3089\u4e8c\u3064\u306e\u8981\u7d20\u3092\u4ea4\u63db\n    }\n    swap(nums, i, left); // \u30d4\u30dc\u30c3\u30c8\u3092\u4e8c\u3064\u306e\u30b5\u30d6\u914d\u5217\u306e\u5883\u754c\u306b\u4ea4\u63db\n    return i;            // \u30d4\u30dc\u30c3\u30c8\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n}\n
        quick_sort.java
        /* 3\u3064\u306e\u5019\u88dc\u8981\u7d20\u306e\u4e2d\u592e\u5024\u3092\u9078\u629e */\nint medianThree(int[] nums, int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m \u306f l \u3068 r \u306e\u9593\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l \u306f m \u3068 r \u306e\u9593\n    return right;\n}\n\n/* \u5206\u5272\uff083\u3064\u306e\u4e2d\u592e\u5024\uff09 */\nint partition(int[] nums, int left, int right) {\n    // 3\u3064\u306e\u5019\u88dc\u8981\u7d20\u306e\u4e2d\u592e\u5024\u3092\u9078\u629e\n    int med = medianThree(nums, left, (left + right) / 2, right);\n    // \u4e2d\u592e\u5024\u3092\u914d\u5217\u306e\u6700\u5de6\u7aef\u306e\u4f4d\u7f6e\u306b\u4ea4\u63db\n    swap(nums, left, med);\n    // nums[left] \u3092\u57fa\u6e96\u5024\u3068\u3057\u3066\u4f7f\u7528\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // \u53f3\u304b\u3089\u5de6\u3078\u3001\u57fa\u6e96\u5024\u3088\u308a\u5c0f\u3055\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u691c\u7d22\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u5de6\u304b\u3089\u53f3\u3078\u3001\u57fa\u6e96\u5024\u3088\u308a\u5927\u304d\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u691c\u7d22\n        swap(nums, i, j); // \u3053\u308c\u30892\u3064\u306e\u8981\u7d20\u3092\u4ea4\u63db\n    }\n    swap(nums, i, left);  // \u57fa\u6e96\u5024\u30922\u3064\u306e\u90e8\u5206\u914d\u5217\u306e\u5883\u754c\u306b\u4ea4\u63db\n    return i;             // \u57fa\u6e96\u5024\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n}\n
        quick_sort.cs
        [class]{QuickSortMedian}-[func]{MedianThree}\n\n[class]{QuickSortMedian}-[func]{Partition}\n
        quick_sort.go
        [class]{quickSortMedian}-[func]{medianThree}\n\n[class]{quickSortMedian}-[func]{partition}\n
        quick_sort.swift
        [class]{}-[func]{medianThree}\n\n[class]{}-[func]{partitionMedian}\n
        quick_sort.js
        [class]{QuickSortMedian}-[func]{medianThree}\n\n[class]{QuickSortMedian}-[func]{partition}\n
        quick_sort.ts
        [class]{QuickSortMedian}-[func]{medianThree}\n\n[class]{QuickSortMedian}-[func]{partition}\n
        quick_sort.dart
        [class]{QuickSortMedian}-[func]{_medianThree}\n\n[class]{QuickSortMedian}-[func]{_partition}\n
        quick_sort.rs
        [class]{QuickSortMedian}-[func]{median_three}\n\n[class]{QuickSortMedian}-[func]{partition}\n
        quick_sort.c
        [class]{}-[func]{medianThree}\n\n[class]{}-[func]{partitionMedian}\n
        quick_sort.kt
        [class]{}-[func]{medianThree}\n\n[class]{}-[func]{partitionMedian}\n
        quick_sort.rb
        [class]{QuickSortMedian}-[func]{median_three}\n\n[class]{QuickSortMedian}-[func]{partition}\n
        "},{"location":"chapter_sorting/quick_sort/#1155","title":"11.5.5 \u00a0 \u672b\u5c3e\u518d\u5e30\u6700\u9069\u5316","text":"

        \u7279\u5b9a\u306e\u5165\u529b\u3067\u306f\u3001\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306f\u3088\u308a\u591a\u304f\u306e\u7a7a\u9593\u3092\u5360\u6709\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u5b8c\u5168\u306b\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u5165\u529b\u914d\u5217\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u518d\u5e30\u3067\u306e\u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u3092\\(m\\)\u3068\u3057\u307e\u3059\u3002\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u3067\u3001\u9577\u3055\\(0\\)\u306e\u5de6\u30b5\u30d6\u914d\u5217\u3068\u9577\u3055\\(m - 1\\)\u306e\u53f3\u30b5\u30d6\u914d\u5217\u304c\u751f\u6210\u3055\u308c\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u518d\u5e30\u547c\u3073\u51fa\u3057\u3054\u3068\u306b\u554f\u984c\u30b5\u30a4\u30ba\u304c1\u3064\u306e\u8981\u7d20\u306e\u307f\u6e1b\u5c11\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u3001\u5404\u30ec\u30d9\u30eb\u306e\u518d\u5e30\u3067\u306e\u524a\u6e1b\u304c\u975e\u5e38\u306b\u5c0f\u3055\u304f\u306a\u308a\u307e\u3059\u3002 \u7d50\u679c\u3068\u3057\u3066\u3001\u518d\u5e30\u30c4\u30ea\u30fc\u306e\u9ad8\u3055\u306f\\(n \u2212 1\\)\u306b\u9054\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u3053\u308c\u306b\u306f\\(O(n)\\)\u306e\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u304c\u5fc5\u8981\u3067\u3059\u3002

        \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u306e\u84c4\u7a4d\u3092\u9632\u3050\u305f\u3081\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u30d4\u30dc\u30c3\u30c8\u30bd\u30fc\u30c8\u5f8c\u306b2\u3064\u306e\u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u3092\u6bd4\u8f03\u3057\u3001**\u3088\u308a\u77ed\u3044\u30b5\u30d6\u914d\u5217\u306e\u307f\u3092\u518d\u5e30\u7684\u306b\u30bd\u30fc\u30c8**\u3067\u304d\u307e\u3059\u3002\u3088\u308a\u77ed\u3044\u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u306f\\(n / 2\\)\u3092\u8d85\u3048\u306a\u3044\u305f\u3081\u3001\u3053\u306e\u65b9\u6cd5\u306f\u518d\u5e30\u6df1\u5ea6\u304c\\(\\log n\\)\u3092\u8d85\u3048\u306a\u3044\u3053\u3068\u3092\u4fdd\u8a3c\u3057\u3001\u6700\u60aa\u7a7a\u9593\u8a08\u7b97\u91cf\u3092\\(O(\\log n)\\)\u306b\u6700\u9069\u5316\u3057\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
        def quick_sort(self, nums: list[int], left: int, right: int):\n    \"\"\"\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\uff08\u672b\u5c3e\u518d\u5e30\u6700\u9069\u5316\uff09\"\"\"\n    # \u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c1\u306e\u3068\u304d\u306b\u7d42\u4e86\n    while left < right:\n        # \u5206\u5272\u64cd\u4f5c\n        pivot = self.partition(nums, left, right)\n        # 2\u3064\u306e\u30b5\u30d6\u914d\u5217\u306e\u3046\u3061\u77ed\u3044\u65b9\u306b\u5bfe\u3057\u3066\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u3092\u5b9f\u884c\n        if pivot - left < right - pivot:\n            self.quick_sort(nums, left, pivot - 1)  # \u5de6\u30b5\u30d6\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u30bd\u30fc\u30c8\n            left = pivot + 1  # \u6b8b\u308a\u306e\u672a\u30bd\u30fc\u30c8\u533a\u9593\u306f [pivot + 1, right]\n        else:\n            self.quick_sort(nums, pivot + 1, right)  # \u53f3\u30b5\u30d6\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u30bd\u30fc\u30c8\n            right = pivot - 1  # \u6b8b\u308a\u306e\u672a\u30bd\u30fc\u30c8\u533a\u9593\u306f [left, pivot - 1]\n
        quick_sort.cpp
        /* \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\uff08\u672b\u5c3e\u518d\u5e30\u6700\u9069\u5316\uff09 */\nvoid quickSort(vector<int> &nums, int left, int right) {\n    // \u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c1\u306e\u6642\u7d42\u4e86\n    while (left < right) {\n        // \u5206\u5272\u64cd\u4f5c\n        int pivot = partition(nums, left, right);\n        // \u4e8c\u3064\u306e\u30b5\u30d6\u914d\u5217\u306e\u3046\u3061\u77ed\u3044\u65b9\u3067\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u3092\u5b9f\u884c\n        if (pivot - left < right - pivot) {\n            quickSort(nums, left, pivot - 1); // \u5de6\u30b5\u30d6\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u30bd\u30fc\u30c8\n            left = pivot + 1;                 // \u6b8b\u308a\u306e\u672a\u30bd\u30fc\u30c8\u533a\u9593\u306f[pivot + 1, right]\n        } else {\n            quickSort(nums, pivot + 1, right); // \u53f3\u30b5\u30d6\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u30bd\u30fc\u30c8\n            right = pivot - 1;                 // \u6b8b\u308a\u306e\u672a\u30bd\u30fc\u30c8\u533a\u9593\u306f[left, pivot - 1]\n        }\n    }\n}\n
        quick_sort.java
        /* \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\uff08\u672b\u5c3e\u518d\u5e30\u6700\u9069\u5316\uff09 */\nvoid quickSort(int[] nums, int left, int right) {\n    // \u90e8\u5206\u914d\u5217\u306e\u9577\u3055\u304c 1 \u306e\u3068\u304d\u7d42\u4e86\n    while (left < right) {\n        // \u5206\u5272\u64cd\u4f5c\n        int pivot = partition(nums, left, right);\n        // 2\u3064\u306e\u90e8\u5206\u914d\u5217\u306e\u3046\u3061\u77ed\u3044\u65b9\u306b\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u3092\u5b9f\u884c\n        if (pivot - left < right - pivot) {\n            quickSort(nums, left, pivot - 1); // \u5de6\u90e8\u5206\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u30bd\u30fc\u30c8\n            left = pivot + 1; // \u6b8b\u308a\u306e\u672a\u30bd\u30fc\u30c8\u533a\u9593\u306f [pivot + 1, right]\n        } else {\n            quickSort(nums, pivot + 1, right); // \u53f3\u90e8\u5206\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u30bd\u30fc\u30c8\n            right = pivot - 1; // \u6b8b\u308a\u306e\u672a\u30bd\u30fc\u30c8\u533a\u9593\u306f [left, pivot - 1]\n        }\n    }\n}\n
        quick_sort.cs
        [class]{QuickSortTailCall}-[func]{QuickSort}\n
        quick_sort.go
        [class]{quickSortTailCall}-[func]{quickSort}\n
        quick_sort.swift
        [class]{}-[func]{quickSortTailCall}\n
        quick_sort.js
        [class]{QuickSortTailCall}-[func]{quickSort}\n
        quick_sort.ts
        [class]{QuickSortTailCall}-[func]{quickSort}\n
        quick_sort.dart
        [class]{QuickSortTailCall}-[func]{quickSort}\n
        quick_sort.rs
        [class]{QuickSortTailCall}-[func]{quick_sort}\n
        quick_sort.c
        [class]{}-[func]{quickSortTailCall}\n
        quick_sort.kt
        [class]{}-[func]{quickSortTailCall}\n
        quick_sort.rb
        [class]{QuickSortTailCall}-[func]{quick_sort}\n
        "},{"location":"chapter_sorting/radix_sort/","title":"11.10 \u00a0 \u57fa\u6570\u30bd\u30fc\u30c8","text":"

        \u524d\u306e\u7bc0\u3067\u306f\u8a08\u6570\u30bd\u30fc\u30c8\u3092\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002\u3053\u308c\u306f\u3001\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba \\(n\\) \u304c\u5927\u304d\u3044\u304c\u30c7\u30fc\u30bf\u7bc4\u56f2 \\(m\\) \u304c\u5c0f\u3055\u3044\u30b7\u30ca\u30ea\u30aa\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\\(n = 10^6\\) \u306e\u5b66\u751fID\u3092\u30bd\u30fc\u30c8\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u5404ID\u304c \\(8\\) \u6841\u306e\u6570\u5b57\u3067\u3042\u308b\u3068\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u30c7\u30fc\u30bf\u7bc4\u56f2 \\(m = 10^8\\) \u304c\u975e\u5e38\u306b\u5927\u304d\u3044\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u3053\u306e\u5834\u5408\u3001\u8a08\u6570\u30bd\u30fc\u30c8\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u5927\u91cf\u306e\u30e1\u30e2\u30ea\u30b9\u30da\u30fc\u30b9\u304c\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059\u3002\u57fa\u6570\u30bd\u30fc\u30c8\u306f\u3053\u306e\u72b6\u6cc1\u3092\u56de\u907f\u3067\u304d\u307e\u3059\u3002

        \u57fa\u6570\u30bd\u30fc\u30c8\u306f\u8a08\u6570\u30bd\u30fc\u30c8\u3068\u540c\u3058\u6838\u5fc3\u6982\u5ff5\u3092\u5171\u6709\u3057\u3001\u8981\u7d20\u306e\u983b\u5ea6\u3092\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u3053\u3068\u3067\u30bd\u30fc\u30c8\u3057\u307e\u3059\u3002\u540c\u6642\u306b\u3001\u57fa\u6570\u30bd\u30fc\u30c8\u306f\u6570\u5b57\u306e\u6841\u9593\u306e\u6f38\u9032\u7684\u95a2\u4fc2\u3092\u5229\u7528\u3057\u3066\u3053\u308c\u3092\u57fa\u76e4\u3068\u3057\u3066\u3044\u307e\u3059\u3002\u6841\u3092\u4e00\u5ea6\u306b\u4e00\u3064\u305a\u3064\u51e6\u7406\u3057\u3066\u30bd\u30fc\u30c8\u3057\u3001\u6700\u7d42\u7684\u306a\u30bd\u30fc\u30c8\u9806\u5e8f\u3092\u9054\u6210\u3057\u307e\u3059\u3002

        "},{"location":"chapter_sorting/radix_sort/#11101","title":"11.10.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u904e\u7a0b","text":"

        \u5b66\u751fID\u30c7\u30fc\u30bf\u3092\u4f8b\u3068\u3057\u3066\u3001\u6700\u4e0b\u4f4d\u6841\u3092 \\(1\\) \u756a\u76ee\u3001\u6700\u4e0a\u4f4d\u6841\u3092 \\(8\\) \u756a\u76ee\u3068\u3059\u308b\u3068\u3001\u57fa\u6570\u30bd\u30fc\u30c8\u306e\u904e\u7a0b\u306f\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        1. \u6841 \\(k = 1\\) \u3092\u521d\u671f\u5316\u3057\u307e\u3059\u3002
        2. \u5b66\u751fID\u306e \\(k\\) \u756a\u76ee\u306e\u6841\u306b\u5bfe\u3057\u3066\u300c\u8a08\u6570\u30bd\u30fc\u30c8\u300d\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002\u5b8c\u4e86\u5f8c\u3001\u30c7\u30fc\u30bf\u306f \\(k\\) \u756a\u76ee\u306e\u6841\u306b\u57fa\u3065\u3044\u3066\u6700\u5c0f\u304b\u3089\u6700\u5927\u307e\u3067\u30bd\u30fc\u30c8\u3055\u308c\u307e\u3059\u3002
        3. \\(k\\) \u3092 \\(1\\) \u5897\u3084\u3057\u3001\u624b\u9806 2. \u306b\u623b\u3063\u3066\u53cd\u5fa9\u3092\u7d9a\u3051\u3001\u3059\u3079\u3066\u306e\u6841\u304c\u30bd\u30fc\u30c8\u3055\u308c\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002\u3053\u306e\u6642\u70b9\u3067\u904e\u7a0b\u304c\u7d42\u4e86\u3057\u307e\u3059\u3002

        \u56f3 11-18 \u00a0 \u57fa\u6570\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u904e\u7a0b

        \u4ee5\u4e0b\u3001\u30b3\u30fc\u30c9\u5b9f\u88c5\u3092\u8a73\u3057\u304f\u898b\u3066\u307f\u307e\u3059\u3002\u57fa\u6570 \\(d\\) \u3067\u306e\u6570 \\(x\\) \u306b\u5bfe\u3057\u3066\u3001\u305d\u306e \\(k\\) \u756a\u76ee\u306e\u6841 \\(x_k\\) \u3092\u53d6\u5f97\u3059\u308b\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u8a08\u7b97\u5f0f\u3092\u4f7f\u7528\u3067\u304d\u307e\u3059\uff1a

        \\[ x_k = \\lfloor\\frac{x}{d^{k-1}}\\rfloor \\bmod d \\]

        \u3053\u3053\u3067 \\(\\lfloor a \\rfloor\\) \u306f\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570 \\(a\\) \u306e\u5207\u308a\u6368\u3066\u3092\u8868\u3057\u3001\\(\\bmod \\: d\\) \u306f \\(d\\) \u306b\u3088\u308b\u5270\u4f59\u3092\u8868\u3057\u307e\u3059\u3002\u5b66\u751fID\u30c7\u30fc\u30bf\u306e\u5834\u5408\u3001\\(d = 10\\) \u3067 \\(k \\in [1, 8]\\) \u3067\u3059\u3002

        \u3055\u3089\u306b\u3001\\(k\\) \u756a\u76ee\u306e\u6841\u306b\u57fa\u3065\u3044\u3066\u30bd\u30fc\u30c8\u3067\u304d\u308b\u3088\u3046\u306b\u3001\u8a08\u6570\u30bd\u30fc\u30c8\u306e\u30b3\u30fc\u30c9\u3092\u5c11\u3057\u4fee\u6b63\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby radix_sort.py
        def digit(num: int, exp: int) -> int:\n    \"\"\"\u8981\u7d20 num \u306e k \u756a\u76ee\u306e\u6841\u3092\u53d6\u5f97\u3001exp = 10^(k-1)\"\"\"\n    # k \u306e\u4ee3\u308f\u308a\u306b exp \u3092\u6e21\u3059\u3053\u3068\u3067\u3001\u3053\u3053\u3067\u30b3\u30b9\u30c8\u306e\u9ad8\u3044\u7d2f\u4e57\u8a08\u7b97\u3092\u907f\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u308b\n    return (num // exp) % 10\n\ndef counting_sort_digit(nums: list[int], exp: int):\n    \"\"\"\u8a08\u6570\u30bd\u30fc\u30c8\uff08nums \u306e k \u756a\u76ee\u306e\u6841\u306b\u57fa\u3065\u304f\uff09\"\"\"\n    # 10\u9032\u6570\u306e\u6841\u306e\u7bc4\u56f2\u306f 0~9\u3001\u3057\u305f\u304c\u3063\u3066\u9577\u305510\u306e\u30d0\u30b1\u30c3\u30c8\u914d\u5217\u304c\u5fc5\u8981\n    counter = [0] * 10\n    n = len(nums)\n    # \u6570\u5b57 0~9 \u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    for i in range(n):\n        d = digit(nums[i], exp)  # nums[i] \u306e k \u756a\u76ee\u306e\u6841\u3092\u53d6\u5f97\u3001d \u3068\u3059\u308b\n        counter[d] += 1  # \u6570\u5b57 d \u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    # \u524d\u7f6e\u548c\u3092\u8a08\u7b97\u3057\u3001\u300c\u51fa\u73fe\u56de\u6570\u300d\u3092\u300c\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u306b\u5909\u63db\n    for i in range(1, 10):\n        counter[i] += counter[i - 1]\n    # \u9006\u9806\u306b\u8d70\u67fb\u3057\u3001\u30d0\u30b1\u30c3\u30c8\u7d71\u8a08\u306b\u57fa\u3065\u3044\u3066\u5404\u8981\u7d20\u3092 res \u306b\u914d\u7f6e\n    res = [0] * n\n    for i in range(n - 1, -1, -1):\n        d = digit(nums[i], exp)\n        j = counter[d] - 1  # \u914d\u5217\u5185\u306e d \u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 j \u3092\u53d6\u5f97\n        res[j] = nums[i]  # \u73fe\u5728\u306e\u8981\u7d20\u3092\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 j \u306b\u914d\u7f6e\n        counter[d] -= 1  # d \u306e\u6570\u30921\u6e1b\u3089\u3059\n    # \u7d50\u679c\u3092\u4f7f\u7528\u3057\u3066\u5143\u306e\u914d\u5217 nums \u3092\u4e0a\u66f8\u304d\n    for i in range(n):\n        nums[i] = res[i]\n\ndef radix_sort(nums: list[int]):\n    \"\"\"\u57fa\u6570\u30bd\u30fc\u30c8\"\"\"\n    # \u914d\u5217\u306e\u6700\u5927\u8981\u7d20\u3092\u53d6\u5f97\u3057\u3001\u6700\u5927\u6841\u6570\u3092\u5224\u5b9a\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    m = max(nums)\n    # \u6700\u4e0b\u4f4d\u6841\u304b\u3089\u6700\u4e0a\u4f4d\u6841\u307e\u3067\u8d70\u67fb\n    exp = 1\n    while exp <= m:\n        # \u914d\u5217\u8981\u7d20\u306e k \u756a\u76ee\u306e\u6841\u306b\u5bfe\u3057\u3066\u8a08\u6570\u30bd\u30fc\u30c8\u3092\u5b9f\u884c\n        # k = 1 -> exp = 1\n        # k = 2 -> exp = 10\n        # \u3064\u307e\u308a\u3001exp = 10^(k-1)\n        counting_sort_digit(nums, exp)\n        exp *= 10\n
        radix_sort.cpp
        /* \u8981\u7d20num\u306ek\u756a\u76ee\u306e\u6841\u3092\u53d6\u5f97\u3001exp = 10^(k-1) */\nint digit(int num, int exp) {\n    // k\u306e\u4ee3\u308f\u308a\u306bexp\u3092\u6e21\u3059\u3053\u3068\u3067\u3001\u3053\u3053\u3067\u7e70\u308a\u8fd4\u3055\u308c\u308b\u9ad8\u4fa1\u306a\u51aa\u4e57\u8a08\u7b97\u3092\u907f\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u308b\n    return (num / exp) % 10;\n}\n\n/* \u30ab\u30a6\u30f3\u30c8\u30bd\u30fc\u30c8\uff08nums\u306ek\u756a\u76ee\u306e\u6841\u306b\u57fa\u3065\u304f\uff09 */\nvoid countingSortDigit(vector<int> &nums, int exp) {\n    // 10\u9032\u6570\u306e\u6841\u7bc4\u56f2\u306f0~9\u306a\u306e\u3067\u3001\u9577\u305510\u306e\u30d0\u30b1\u30c3\u30c8\u914d\u5217\u304c\u5fc5\u8981\n    vector<int> counter(10, 0);\n    int n = nums.size();\n    // \u6570\u5b570~9\u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    for (int i = 0; i < n; i++) {\n        int d = digit(nums[i], exp); // nums[i]\u306ek\u756a\u76ee\u306e\u6841\u3092\u53d6\u5f97\u3001d\u3068\u3057\u3066\u8a18\u9332\n        counter[d]++;                // \u6570\u5b57d\u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    }\n    // \u524d\u7f00\u548c\u3092\u8a08\u7b97\u3057\u3001\u300c\u51fa\u73fe\u56de\u6570\u300d\u3092\u300c\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u306b\u5909\u63db\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // \u9006\u9806\u3067\u8d70\u67fb\u3057\u3001\u30d0\u30b1\u30c3\u30c8\u7d71\u8a08\u306b\u57fa\u3065\u3044\u3066\u5404\u8981\u7d20\u3092res\u306b\u914d\u7f6e\n    vector<int> res(n, 0);\n    for (int i = n - 1; i >= 0; i--) {\n        int d = digit(nums[i], exp);\n        int j = counter[d] - 1; // d\u304c\u914d\u5217\u5185\u306b\u3042\u308b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9j\u3092\u53d6\u5f97\n        res[j] = nums[i];       // \u73fe\u5728\u306e\u8981\u7d20\u3092\u30a4\u30f3\u30c7\u30c3\u30af\u30b9j\u306b\u914d\u7f6e\n        counter[d]--;           // d\u306e\u30ab\u30a6\u30f3\u30c8\u30921\u6e1b\u3089\u3059\n    }\n    // \u7d50\u679c\u3067\u5143\u306e\u914d\u5217nums\u3092\u4e0a\u66f8\u304d\n    for (int i = 0; i < n; i++)\n        nums[i] = res[i];\n}\n\n/* \u57fa\u6570\u30bd\u30fc\u30c8 */\nvoid radixSort(vector<int> &nums) {\n    // \u914d\u5217\u306e\u6700\u5927\u8981\u7d20\u3092\u53d6\u5f97\u3001\u6700\u5927\u6841\u6570\u3092\u5224\u5b9a\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    int m = *max_element(nums.begin(), nums.end());\n    // \u6700\u4e0b\u4f4d\u6841\u304b\u3089\u6700\u4e0a\u4f4d\u6841\u307e\u3067\u8d70\u67fb\n    for (int exp = 1; exp <= m; exp *= 10)\n        // \u914d\u5217\u8981\u7d20\u306ek\u756a\u76ee\u306e\u6841\u3067\u30ab\u30a6\u30f3\u30c8\u30bd\u30fc\u30c8\u3092\u5b9f\u884c\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u3064\u307e\u308a\u3001exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n}\n
        radix_sort.java
        /* \u8981\u7d20 num \u306e k \u756a\u76ee\u306e\u6841\u3092\u53d6\u5f97\u3001exp = 10^(k-1) */\nint digit(int num, int exp) {\n    // k \u306e\u4ee3\u308f\u308a\u306b exp \u3092\u6e21\u3059\u3053\u3068\u3067\u3001\u3053\u3053\u3067\u30b3\u30b9\u30c8\u306e\u9ad8\u3044\u7d2f\u4e57\u8a08\u7b97\u306e\u7e70\u308a\u8fd4\u3057\u3092\u907f\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u308b\n    return (num / exp) % 10;\n}\n\n/* \u8a08\u6570\u30bd\u30fc\u30c8\uff08nums \u306e k \u756a\u76ee\u306e\u6841\u306b\u57fa\u3065\u304f\uff09 */\nvoid countingSortDigit(int[] nums, int exp) {\n    // 10\u9032\u6570\u306e\u6841\u306e\u7bc4\u56f2\u306f 0~9\u3001\u3057\u305f\u304c\u3063\u3066\u9577\u3055 10 \u306e\u30d0\u30b1\u30c3\u30c8\u914d\u5217\u304c\u5fc5\u8981\n    int[] counter = new int[10];\n    int n = nums.length;\n    // \u6841 0~9 \u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    for (int i = 0; i < n; i++) {\n        int d = digit(nums[i], exp); // nums[i] \u306e k \u756a\u76ee\u306e\u6841\u3092\u53d6\u5f97\u3001d \u3068\u3059\u308b\n        counter[d]++;                // \u6841 d \u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    }\n    // \u7d2f\u7a4d\u548c\u3092\u8a08\u7b97\u3057\u3001\u300c\u51fa\u73fe\u56de\u6570\u300d\u3092\u300c\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u306b\u5909\u63db\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // \u9006\u9806\u306b\u8d70\u67fb\u3057\u3001\u30d0\u30b1\u30c3\u30c8\u7d71\u8a08\u306b\u57fa\u3065\u3044\u3066\u5404\u8981\u7d20\u3092 res \u306b\u914d\u7f6e\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int d = digit(nums[i], exp);\n        int j = counter[d] - 1; // \u914d\u5217\u5185\u3067\u306e d \u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 j \u3092\u53d6\u5f97\n        res[j] = nums[i];       // \u73fe\u5728\u306e\u8981\u7d20\u3092\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 j \u306b\u914d\u7f6e\n        counter[d]--;           // d \u306e\u30ab\u30a6\u30f3\u30c8\u3092 1 \u6e1b\u3089\u3059\n    }\n    // \u7d50\u679c\u3067\u5143\u306e\u914d\u5217 nums \u3092\u4e0a\u66f8\u304d\n    for (int i = 0; i < n; i++)\n        nums[i] = res[i];\n}\n\n/* \u57fa\u6570\u30bd\u30fc\u30c8 */\nvoid radixSort(int[] nums) {\n    // \u914d\u5217\u306e\u6700\u5927\u8981\u7d20\u3092\u53d6\u5f97\u3057\u3001\u6700\u5927\u6841\u6570\u3092\u5224\u5b9a\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    int m = Integer.MIN_VALUE;\n    for (int num : nums)\n        if (num > m)\n            m = num;\n    // \u6700\u4e0b\u4f4d\u6841\u304b\u3089\u6700\u4e0a\u4f4d\u6841\u307e\u3067\u8d70\u67fb\n    for (int exp = 1; exp <= m; exp *= 10) {\n        // \u914d\u5217\u8981\u7d20\u306e k \u756a\u76ee\u306e\u6841\u306b\u5bfe\u3057\u3066\u8a08\u6570\u30bd\u30fc\u30c8\u3092\u5b9f\u884c\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u3059\u306a\u308f\u3061 exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n    }\n}\n
        radix_sort.cs
        [class]{radix_sort}-[func]{Digit}\n\n[class]{radix_sort}-[func]{CountingSortDigit}\n\n[class]{radix_sort}-[func]{RadixSort}\n
        radix_sort.go
        [class]{}-[func]{digit}\n\n[class]{}-[func]{countingSortDigit}\n\n[class]{}-[func]{radixSort}\n
        radix_sort.swift
        [class]{}-[func]{digit}\n\n[class]{}-[func]{countingSortDigit}\n\n[class]{}-[func]{radixSort}\n
        radix_sort.js
        [class]{}-[func]{digit}\n\n[class]{}-[func]{countingSortDigit}\n\n[class]{}-[func]{radixSort}\n
        radix_sort.ts
        [class]{}-[func]{digit}\n\n[class]{}-[func]{countingSortDigit}\n\n[class]{}-[func]{radixSort}\n
        radix_sort.dart
        [class]{}-[func]{digit}\n\n[class]{}-[func]{countingSortDigit}\n\n[class]{}-[func]{radixSort}\n
        radix_sort.rs
        [class]{}-[func]{digit}\n\n[class]{}-[func]{counting_sort_digit}\n\n[class]{}-[func]{radix_sort}\n
        radix_sort.c
        [class]{}-[func]{digit}\n\n[class]{}-[func]{countingSortDigit}\n\n[class]{}-[func]{radixSort}\n
        radix_sort.kt
        [class]{}-[func]{digit}\n\n[class]{}-[func]{countingSortDigit}\n\n[class]{}-[func]{radixSort}\n
        radix_sort.rb
        [class]{}-[func]{digit}\n\n[class]{}-[func]{counting_sort_digit}\n\n[class]{}-[func]{radix_sort}\n

        \u306a\u305c\u6700\u4e0b\u4f4d\u6841\u304b\u3089\u958b\u59cb\u3059\u308b\u306e\u304b\uff1f

        \u9023\u7d9a\u3059\u308b\u30bd\u30fc\u30c8\u30e9\u30a6\u30f3\u30c9\u3067\u306f\u3001\u5f8c\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u7d50\u679c\u304c\u524d\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u7d50\u679c\u3092\u4e0a\u66f8\u304d\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u6700\u521d\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u7d50\u679c\u304c \\(a < b\\) \u3067\u30012\u756a\u76ee\u306e\u30e9\u30a6\u30f3\u30c9\u304c \\(a > b\\) \u306e\u5834\u5408\u30012\u756a\u76ee\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u7d50\u679c\u304c\u6700\u521d\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u7d50\u679c\u3092\u7f6e\u304d\u63db\u3048\u307e\u3059\u3002\u4e0a\u4f4d\u6841\u306f\u4e0b\u4f4d\u6841\u3088\u308a\u512a\u5148\u3055\u308c\u308b\u305f\u3081\u3001\u4e0a\u4f4d\u6841\u306e\u524d\u306b\u4e0b\u4f4d\u6841\u3092\u30bd\u30fc\u30c8\u3059\u308b\u3053\u3068\u304c\u7406\u306b\u304b\u306a\u3063\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_sorting/radix_sort/#11102","title":"11.10.2 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7279\u5fb4","text":"

        \u8a08\u6570\u30bd\u30fc\u30c8\u3068\u6bd4\u8f03\u3057\u3066\u3001\u57fa\u6570\u30bd\u30fc\u30c8\u306f\u3088\u308a\u5927\u304d\u306a\u6570\u5024\u7bc4\u56f2\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u30c7\u30fc\u30bf\u304c\u56fa\u5b9a\u6841\u6570\u3067\u8868\u73fe\u3067\u304d\u3001\u6841\u6570\u304c\u3042\u307e\u308a\u5927\u304d\u304f\u306a\u3044\u3053\u3068\u3092\u524d\u63d0\u3068\u3057\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u306f\u6841\u6570 \\(k\\) \u304c\u5927\u304d\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf \\(O(nk) \\gg O(n^2)\\) \u306b\u3064\u306a\u304c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3001\u57fa\u6570\u30bd\u30fc\u30c8\u306b\u306f\u9069\u3057\u3066\u3044\u307e\u305b\u3093\u3002

        • \u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(nk)\\)\u3001\u975e\u9069\u5fdc\u30bd\u30fc\u30c8\uff1a\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\u3092 \\(n\\)\u3001\u30c7\u30fc\u30bf\u304c\u57fa\u6570 \\(d\\)\u3001\u6700\u5927\u6841\u6570\u3092 \\(k\\) \u3068\u3059\u308b\u3068\u3001\u5358\u4e00\u6841\u306e\u30bd\u30fc\u30c8\u306b\u306f \\(O(n + d)\\) \u6642\u9593\u304c\u304b\u304b\u308a\u3001\u3059\u3079\u3066\u306e \\(k\\) \u6841\u306e\u30bd\u30fc\u30c8\u306b\u306f \\(O((n + d)k)\\) \u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002\u4e00\u822c\u7684\u306b\u3001\\(d\\) \u3068 \\(k\\) \u306f\u3069\u3061\u3089\u3082\u6bd4\u8f03\u7684\u5c0f\u3055\u304f\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u306b\u8fd1\u3065\u304d\u307e\u3059\u3002
        • \u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(n + d)\\)\u3001\u975e\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u30bd\u30fc\u30c8\uff1a\u8a08\u6570\u30bd\u30fc\u30c8\u3068\u540c\u69d8\u306b\u3001\u57fa\u6570\u30bd\u30fc\u30c8\u306f\u9577\u3055 \\(n\\) \u3068 \\(d\\) \u306e\u914d\u5217 res \u3068 counter \u306b\u305d\u308c\u305e\u308c\u4f9d\u5b58\u3057\u307e\u3059\u3002
        • \u5b89\u5b9a\u30bd\u30fc\u30c8\uff1a\u8a08\u6570\u30bd\u30fc\u30c8\u304c\u5b89\u5b9a\u306a\u5834\u5408\u3001\u57fa\u6570\u30bd\u30fc\u30c8\u3082\u5b89\u5b9a\u3067\u3059\u3002\u8a08\u6570\u30bd\u30fc\u30c8\u304c\u4e0d\u5b89\u5b9a\u306a\u5834\u5408\u3001\u57fa\u6570\u30bd\u30fc\u30c8\u306f\u6b63\u3057\u3044\u30bd\u30fc\u30c8\u9806\u5e8f\u3092\u4fdd\u8a3c\u3067\u304d\u307e\u305b\u3093\u3002
        "},{"location":"chapter_sorting/selection_sort/","title":"11.2 \u00a0 \u9078\u629e\u30bd\u30fc\u30c8","text":"

        \u9078\u629e\u30bd\u30fc\u30c8\u306f\u975e\u5e38\u306b\u30b7\u30f3\u30d7\u30eb\u306a\u539f\u7406\u3067\u52d5\u4f5c\u3057\u307e\u3059\uff1a\u5404\u53cd\u5fa9\u3067\u672a\u30bd\u30fc\u30c8\u533a\u9593\u304b\u3089\u6700\u5c0f\u8981\u7d20\u3092\u9078\u629e\u3057\u3001\u30bd\u30fc\u30c8\u6e08\u307f\u30bb\u30af\u30b7\u30e7\u30f3\u306e\u672b\u5c3e\u306b\u79fb\u52d5\u3059\u308b\u30eb\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        \u914d\u5217\u306e\u9577\u3055\u3092\\(n\\)\u3068\u3059\u308b\u3068\u3001\u9078\u629e\u30bd\u30fc\u30c8\u306e\u30b9\u30c6\u30c3\u30d7\u306f\u4e0b\u56f3\u306b\u793a\u3055\u308c\u307e\u3059\u3002

        1. \u6700\u521d\u306b\u3001\u3059\u3079\u3066\u306e\u8981\u7d20\u306f\u672a\u30bd\u30fc\u30c8\u3067\u3001\u3064\u307e\u308a\u672a\u30bd\u30fc\u30c8\uff08\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\uff09\u533a\u9593\u306f\\([0, n-1]\\)\u3067\u3059\u3002
        2. \u533a\u9593\\([0, n-1]\\)\u306e\u6700\u5c0f\u8981\u7d20\u3092\u9078\u629e\u3057\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(0\\)\u306e\u8981\u7d20\u3068\u4ea4\u63db\u3057\u307e\u3059\u3002\u3053\u306e\u5f8c\u3001\u914d\u5217\u306e\u6700\u521d\u306e\u8981\u7d20\u304c\u30bd\u30fc\u30c8\u3055\u308c\u307e\u3059\u3002
        3. \u533a\u9593\\([1, n-1]\\)\u306e\u6700\u5c0f\u8981\u7d20\u3092\u9078\u629e\u3057\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(1\\)\u306e\u8981\u7d20\u3068\u4ea4\u63db\u3057\u307e\u3059\u3002\u3053\u306e\u5f8c\u3001\u914d\u5217\u306e\u6700\u521d\u306e2\u3064\u306e\u8981\u7d20\u304c\u30bd\u30fc\u30c8\u3055\u308c\u307e\u3059\u3002
        4. \u3053\u306e\u65b9\u6cd5\u3067\u7d9a\u884c\u3057\u307e\u3059\u3002\\(n - 1\\)\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u3068\u4ea4\u63db\u306e\u5f8c\u3001\u6700\u521d\u306e\\(n - 1\\)\u500b\u306e\u8981\u7d20\u304c\u30bd\u30fc\u30c8\u3055\u308c\u307e\u3059\u3002
        5. \u6b8b\u308a\u306e\u552f\u4e00\u306e\u8981\u7d20\u306f\u7d50\u679c\u7684\u306b\u6700\u5927\u8981\u7d20\u3067\u3042\u308a\u3001\u30bd\u30fc\u30c8\u3059\u308b\u5fc5\u8981\u304c\u306a\u3044\u305f\u3081\u3001\u914d\u5217\u306f\u30bd\u30fc\u30c8\u3055\u308c\u307e\u3059\u3002
        <1><2><3><4><5><6><7><8><9><10><11>

        \u56f3 11-2 \u00a0 Selection sort process

        \u30b3\u30fc\u30c9\u3067\u306f\u3001\\(k\\)\u3092\u4f7f\u7528\u3057\u3066\u672a\u30bd\u30fc\u30c8\u533a\u9593\u5185\u306e\u6700\u5c0f\u8981\u7d20\u3092\u8a18\u9332\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby selection_sort.py
        def selection_sort(nums: list[int]):\n    \"\"\"\u9078\u629e\u30bd\u30fc\u30c8\"\"\"\n    n = len(nums)\n    # \u5916\u5074\u306e\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f [i, n-1]\n    for i in range(n - 1):\n        # \u5185\u5074\u306e\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u5185\u3067\u6700\u5c0f\u8981\u7d20\u3092\u898b\u3064\u3051\u308b\n        k = i\n        for j in range(i + 1, n):\n            if nums[j] < nums[k]:\n                k = j  # \u6700\u5c0f\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a18\u9332\n        # \u6700\u5c0f\u8981\u7d20\u3092\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306e\u5148\u982d\u8981\u7d20\u3068\u4ea4\u63db\n        nums[i], nums[k] = nums[k], nums[i]\n
        selection_sort.cpp
        /* \u9078\u629e\u30bd\u30fc\u30c8 */\nvoid selectionSort(vector<int> &nums) {\n    int n = nums.size();\n    // \u5916\u5074\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f[i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // \u5185\u5074\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u5185\u3067\u6700\u5c0f\u8981\u7d20\u3092\u898b\u3064\u3051\u308b\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // \u6700\u5c0f\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a18\u9332\n        }\n        // \u6700\u5c0f\u8981\u7d20\u3092\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306e\u6700\u521d\u306e\u8981\u7d20\u3068\u4ea4\u63db\n        swap(nums[i], nums[k]);\n    }\n}\n
        selection_sort.java
        /* \u9078\u629e\u30bd\u30fc\u30c8 */\nvoid selectionSort(int[] nums) {\n    int n = nums.length;\n    // \u5916\u5074\u30eb\u30fc\u30d7: \u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f [i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // \u5185\u5074\u30eb\u30fc\u30d7: \u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u5185\u3067\u6700\u5c0f\u8981\u7d20\u3092\u898b\u3064\u3051\u308b\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // \u6700\u5c0f\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a18\u9332\n        }\n        // \u6700\u5c0f\u8981\u7d20\u3068\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306e\u6700\u521d\u306e\u8981\u7d20\u3092\u4ea4\u63db\n        int temp = nums[i];\n        nums[i] = nums[k];\n        nums[k] = temp;\n    }\n}\n
        selection_sort.cs
        [class]{selection_sort}-[func]{SelectionSort}\n
        selection_sort.go
        [class]{}-[func]{selectionSort}\n
        selection_sort.swift
        [class]{}-[func]{selectionSort}\n
        selection_sort.js
        [class]{}-[func]{selectionSort}\n
        selection_sort.ts
        [class]{}-[func]{selectionSort}\n
        selection_sort.dart
        [class]{}-[func]{selectionSort}\n
        selection_sort.rs
        [class]{}-[func]{selection_sort}\n
        selection_sort.c
        [class]{}-[func]{selectionSort}\n
        selection_sort.kt
        [class]{}-[func]{selectionSort}\n
        selection_sort.rb
        [class]{}-[func]{selection_sort}\n
        "},{"location":"chapter_sorting/selection_sort/#1121","title":"11.2.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7279\u6027","text":"
        • \\(O(n^2)\\)\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3001\u975e\u9069\u5fdc\u30bd\u30fc\u30c8\uff1a\u5916\u5074\u30eb\u30fc\u30d7\u306b\\(n - 1\\)\u56de\u306e\u53cd\u5fa9\u304c\u3042\u308a\u3001\u672a\u30bd\u30fc\u30c8\u30bb\u30af\u30b7\u30e7\u30f3\u306e\u9577\u3055\u306f\u6700\u521d\u306e\u53cd\u5fa9\u3067\\(n\\)\u304b\u3089\u59cb\u307e\u308a\u3001\u6700\u5f8c\u306e\u53cd\u5fa9\u3067\\(2\\)\u307e\u3067\u6e1b\u5c11\u3057\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u5404\u5916\u5074\u30eb\u30fc\u30d7\u53cd\u5fa9\u306b\u306f\u305d\u308c\u305e\u308c\\(n\\)\u3001\\(n - 1\\)\u3001\\(\\dots\\)\u3001\\(3\\)\u3001\\(2\\)\u56de\u306e\u5185\u5074\u30eb\u30fc\u30d7\u53cd\u5fa9\u304c\u542b\u307e\u308c\u3001\u5408\u8a08\u306f\\(\\frac{(n - 1)(n + 2)}{2}\\)\u3068\u306a\u308a\u307e\u3059\u3002
        • \\(O(1)\\)\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u3001\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u30bd\u30fc\u30c8\uff1a\u30dd\u30a4\u30f3\u30bf\\(i\\)\u3068\\(j\\)\u3067\u5b9a\u6570\u306e\u8ffd\u52a0\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u975e\u5b89\u5b9a\u30bd\u30fc\u30c8\uff1a\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u8981\u7d20nums[i]\u306f\u7b49\u3057\u3044\u8981\u7d20\u306e\u53f3\u5074\u306b\u4ea4\u63db\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u76f8\u5bfe\u9806\u5e8f\u304c\u5909\u308f\u308b\u539f\u56e0\u3068\u306a\u308a\u307e\u3059\u3002

        \u56f3 11-3 \u00a0 Selection sort instability example

        "},{"location":"chapter_sorting/sorting_algorithm/","title":"11.1 \u00a0 \u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0","text":"

        \u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3092\u7279\u5b9a\u306e\u9806\u5e8f\u3067\u914d\u5217\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u30c7\u30fc\u30bf\u306f\u901a\u5e38\u3001\u3088\u308a\u52b9\u7387\u7684\u306b\u63a2\u7d22\u3001\u5206\u6790\u3001\u51e6\u7406\u3067\u304d\u308b\u305f\u3081\u3001\u5e45\u5e83\u3044\u5fdc\u7528\u304c\u3042\u308a\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u30c7\u30fc\u30bf\u578b\u306f\u6574\u6570\u3001\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u3001\u6587\u5b57\u3001\u6587\u5b57\u5217\u306a\u3069\u3067\u3059\u3002\u30bd\u30fc\u30c8\u57fa\u6e96\u306f\u3001\u6570\u5024\u30b5\u30a4\u30ba\u3001\u6587\u5b57ASCII\u9806\u5e8f\u3001\u307e\u305f\u306f\u30ab\u30b9\u30bf\u30e0\u57fa\u6e96\u306a\u3069\u3001\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u8a2d\u5b9a\u3067\u304d\u307e\u3059\u3002

        \u56f3 11-1 \u00a0 Data types and comparator examples

        "},{"location":"chapter_sorting/sorting_algorithm/#1111","title":"11.1.1 \u00a0 \u8a55\u4fa1\u6b21\u5143","text":"

        \u5b9f\u884c\u52b9\u7387\uff1a\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6642\u9593\u8a08\u7b97\u91cf\u304c\u3067\u304d\u308b\u3060\u3051\u4f4e\u3044\u3053\u3068\u3092\u671f\u5f85\u3057\u3001\u5168\u4f53\u7684\u306a\u64cd\u4f5c\u6570\u3082\u5c11\u306a\u3044\u3053\u3068\uff08\u6642\u9593\u8a08\u7b97\u91cf\u306e\u5b9a\u6570\u9805\u3092\u4e0b\u3052\u308b\uff09\u3092\u671b\u307f\u307e\u3059\u3002\u5927\u5bb9\u91cf\u30c7\u30fc\u30bf\u3067\u306f\u3001\u5b9f\u884c\u52b9\u7387\u304c\u7279\u306b\u91cd\u8981\u3067\u3059\u3002

        \u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u6027\uff1a\u540d\u524d\u304c\u793a\u3059\u3068\u304a\u308a\u3001\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u30bd\u30fc\u30c8\u306f\u5143\u306e\u914d\u5217\u3092\u76f4\u63a5\u64cd\u4f5c\u3059\u308b\u3053\u3068\u3067\u5b9f\u73fe\u3055\u308c\u3001\u8ffd\u52a0\u306e\u30d8\u30eb\u30d1\u30fc\u914d\u5217\u304c\u4e0d\u8981\u3067\u3042\u308b\u305f\u3081\u3001\u30e1\u30e2\u30ea\u3092\u7bc0\u7d04\u3057\u307e\u3059\u3002\u4e00\u822c\u7684\u306b\u3001\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u30bd\u30fc\u30c8\u306f\u30c7\u30fc\u30bf\u79fb\u52d5\u64cd\u4f5c\u304c\u5c11\u306a\u304f\u3001\u9ad8\u901f\u3067\u3059\u3002

        \u5b89\u5b9a\u6027\uff1a\u5b89\u5b9a\u30bd\u30fc\u30c8\u306f\u3001\u30bd\u30fc\u30c8\u5f8c\u306b\u914d\u5217\u5185\u306e\u7b49\u3057\u3044\u8981\u7d20\u306e\u76f8\u5bfe\u9806\u5e8f\u304c\u5909\u308f\u3089\u306a\u3044\u3053\u3068\u3092\u4fdd\u8a3c\u3057\u307e\u3059\u3002

        \u5b89\u5b9a\u30bd\u30fc\u30c8\u306f\u3001\u30de\u30eb\u30c1\u30ad\u30fc\u30bd\u30fc\u30c8\u30b7\u30ca\u30ea\u30aa\u306b\u304a\u3044\u3066\u5fc5\u8981\u6761\u4ef6\u3067\u3059\u3002\u5b66\u751f\u60c5\u5831\u3092\u683c\u7d0d\u3059\u308b\u30c6\u30fc\u30d6\u30eb\u304c\u3042\u308a\u3001\u7b2c1\u5217\u3068\u7b2c2\u5217\u304c\u305d\u308c\u305e\u308c\u540d\u524d\u3068\u5e74\u9f62\u3067\u3042\u308b\u3068\u3057\u307e\u3059\u3002\u3053\u306e\u5834\u5408\u3001\u4e0d\u5b89\u5b9a\u30bd\u30fc\u30c8\u306f\u5165\u529b\u30c7\u30fc\u30bf\u306e\u9806\u5e8f\u3092\u5931\u3046\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\uff1a

        # \u5165\u529b\u30c7\u30fc\u30bf\u306f\u540d\u524d\u3067\u30bd\u30fc\u30c8\u6e08\u307f\n# (\u540d\u524d, \u5e74\u9f62)\n  ('A', 19)\n  ('B', 18)\n  ('C', 21)\n  ('D', 19)\n  ('E', 23)\n\n# \u4e0d\u5b89\u5b9a\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u4f7f\u7528\u3057\u3066\u30ea\u30b9\u30c8\u3092\u5e74\u9f62\u3067\u30bd\u30fc\u30c8\u3059\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\n# \u7d50\u679c\u306f('D', 19)\u3068('A', 19)\u306e\u76f8\u5bfe\u4f4d\u7f6e\u3092\u5909\u66f4\u3057\u3001\n# \u5165\u529b\u30c7\u30fc\u30bf\u304c\u540d\u524d\u3067\u30bd\u30fc\u30c8\u6e08\u307f\u3067\u3042\u308b\u3068\u3044\u3046\u6027\u8cea\u304c\u5931\u308f\u308c\u308b\n  ('B', 18)\n  ('D', 19)\n  ('A', 19)\n  ('C', 21)\n  ('E', 23)\n

        \u9069\u5fdc\u6027\uff1a\u9069\u5fdc\u30bd\u30fc\u30c8\u306f\u5165\u529b\u30c7\u30fc\u30bf\u5185\u306e\u65e2\u5b58\u306e\u9806\u5e8f\u60c5\u5831\u3092\u6d3b\u7528\u3057\u3066\u8a08\u7b97\u8ca0\u8377\u3092\u524a\u6e1b\u3057\u3001\u3088\u308a\u6700\u9069\u306a\u6642\u9593\u52b9\u7387\u3092\u5b9f\u73fe\u3057\u307e\u3059\u3002\u9069\u5fdc\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6700\u826f\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\u306f\u3001\u901a\u5e38\u5e73\u5747\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\u3088\u308a\u3082\u512a\u308c\u3066\u3044\u307e\u3059\u3002

        \u6bd4\u8f03\u30d9\u30fc\u30b9\u307e\u305f\u306f\u975e\u6bd4\u8f03\u30d9\u30fc\u30b9\uff1a\u6bd4\u8f03\u30d9\u30fc\u30b9\u30bd\u30fc\u30c8\u306f\u6bd4\u8f03\u6f14\u7b97\u5b50\uff08\\(<\\)\u3001\\(=\\)\u3001\\(>\\)\uff09\u306b\u4f9d\u5b58\u3057\u3066\u8981\u7d20\u306e\u76f8\u5bfe\u9806\u5e8f\u3092\u6c7a\u5b9a\u3057\u3001\u914d\u5217\u5168\u4f53\u3092\u30bd\u30fc\u30c8\u3057\u307e\u3059\u3002\u7406\u8ad6\u7684\u6700\u9069\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n \\log n)\\)\u3067\u3059\u3002\u4e00\u65b9\u3001\u975e\u6bd4\u8f03\u30bd\u30fc\u30c8\u306f\u6bd4\u8f03\u6f14\u7b97\u5b50\u3092\u4f7f\u7528\u305b\u305a\u3001\\(O(n)\\)\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3092\u5b9f\u73fe\u3067\u304d\u307e\u3059\u304c\u3001\u6c4e\u7528\u6027\u306f\u6bd4\u8f03\u7684\u52a3\u308a\u307e\u3059\u3002

        "},{"location":"chapter_sorting/sorting_algorithm/#1112","title":"11.1.2 \u00a0 \u7406\u60f3\u7684\u306a\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0","text":"

        \u9ad8\u901f\u5b9f\u884c\u3001\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u3001\u5b89\u5b9a\u3001\u9069\u5fdc\u3001\u6c4e\u7528\u3002\u660e\u3089\u304b\u306b\u3001\u3053\u308c\u3089\u306e\u3059\u3079\u3066\u306e\u7279\u5fb4\u3092\u7d44\u307f\u5408\u308f\u305b\u305f\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u4eca\u65e5\u307e\u3067\u898b\u3064\u304b\u3063\u3066\u3044\u307e\u305b\u3093\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u9078\u629e\u3059\u308b\u969b\u306f\u3001\u30c7\u30fc\u30bf\u306e\u7279\u5b9a\u306e\u7279\u5fb4\u3068\u554f\u984c\u306e\u8981\u4ef6\u306b\u57fa\u3065\u3044\u3066\u6c7a\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u6b21\u306b\u3001\u3055\u307e\u3056\u307e\u306a\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u4e00\u7dd2\u306b\u5b66\u3073\u3001\u4e0a\u8a18\u306e\u8a55\u4fa1\u6b21\u5143\u306b\u57fa\u3065\u3044\u3066\u305d\u308c\u305e\u308c\u306e\u5229\u70b9\u3068\u6b20\u70b9\u3092\u5206\u6790\u3057\u307e\u3059\u3002

        "},{"location":"chapter_sorting/summary/","title":"11.11 \u00a0 \u307e\u3068\u3081","text":""},{"location":"chapter_sorting/summary/#1","title":"1. \u00a0 \u91cd\u8981\u306a\u5fa9\u7fd2","text":"
        • \u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u306f\u96a3\u63a5\u3059\u308b\u8981\u7d20\u3092\u4ea4\u63db\u3059\u308b\u3053\u3068\u3067\u52d5\u4f5c\u3057\u307e\u3059\u3002\u30d5\u30e9\u30b0\u3092\u8ffd\u52a0\u3057\u3066\u65e9\u671f\u30ea\u30bf\u30fc\u30f3\u3092\u53ef\u80fd\u306b\u3059\u308b\u3053\u3068\u3067\u3001\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u306e\u6700\u826f\u30b1\u30fc\u30b9\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3092 \\(O(n)\\) \u306b\u6700\u9069\u5316\u3067\u304d\u307e\u3059\u3002
        • \u633f\u5165\u30bd\u30fc\u30c8\u306f\u3001\u672a\u30bd\u30fc\u30c8\u533a\u9593\u304b\u3089\u8981\u7d20\u3092\u53d6\u308a\u51fa\u3057\u3066\u30bd\u30fc\u30c8\u6e08\u307f\u533a\u9593\u306e\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u633f\u5165\u3059\u308b\u3053\u3068\u3067\u5404\u30e9\u30a6\u30f3\u30c9\u3092\u30bd\u30fc\u30c8\u3057\u307e\u3059\u3002\u633f\u5165\u30bd\u30fc\u30c8\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n^2)\\) \u3067\u3059\u304c\u3001\u5358\u4f4d\u3042\u305f\u308a\u306e\u64cd\u4f5c\u304c\u6bd4\u8f03\u7684\u5c11\u306a\u3044\u305f\u3081\u3001\u5c11\u91cf\u306e\u30c7\u30fc\u30bf\u306e\u30bd\u30fc\u30c8\u3067\u306f\u975e\u5e38\u306b\u4eba\u6c17\u304c\u3042\u308a\u307e\u3059\u3002
        • \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306f\u6b69\u54e8\u5206\u5272\u64cd\u4f5c\u306b\u57fa\u3065\u3044\u3066\u3044\u307e\u3059\u3002\u6b69\u54e8\u5206\u5272\u3067\u306f\u3001\u5e38\u306b\u6700\u60aa\u306e\u30d4\u30dc\u30c3\u30c8\u3092\u9078\u3076\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u304c \\(O(n^2)\\) \u306b\u52a3\u5316\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u4e2d\u592e\u5024\u3084\u30e9\u30f3\u30c0\u30e0\u30d4\u30dc\u30c3\u30c8\u3092\u5c0e\u5165\u3059\u308b\u3053\u3068\u3067\u3001\u305d\u306e\u3088\u3046\u306a\u52a3\u5316\u306e\u78ba\u7387\u3092\u6e1b\u3089\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u672b\u5c3e\u518d\u5e30\u306f\u518d\u5e30\u306e\u6df1\u3055\u3092\u52b9\u679c\u7684\u306b\u6e1b\u3089\u3057\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u3092 \\(O(\\log n)\\) \u306b\u6700\u9069\u5316\u3057\u307e\u3059\u3002
        • \u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u306b\u306f\u5206\u5272\u3068\u30de\u30fc\u30b8\u306e2\u3064\u306e\u6bb5\u968e\u304c\u3042\u308a\u3001\u901a\u5e38\u5206\u5272\u7d71\u6cbb\u6226\u7565\u3092\u4f53\u73fe\u3057\u3066\u3044\u307e\u3059\u3002\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u3067\u306f\u3001\u914d\u5217\u306e\u30bd\u30fc\u30c8\u306b\u306f\u88dc\u52a9\u914d\u5217\u306e\u4f5c\u6210\u304c\u5fc5\u8981\u3067\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u306b\u306a\u308a\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u30ea\u30b9\u30c8\u306e\u30bd\u30fc\u30c8\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(1)\\) \u306b\u6700\u9069\u5316\u3067\u304d\u307e\u3059\u3002
        • \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306f3\u3064\u306e\u624b\u9806\u304b\u3089\u69cb\u6210\u3055\u308c\u307e\u3059\uff1a\u30c7\u30fc\u30bf\u3092\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u6563\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u5185\u3067\u306e\u30bd\u30fc\u30c8\u3001\u30d0\u30b1\u30c3\u30c8\u9806\u3067\u306e\u7d50\u679c\u306e\u30de\u30fc\u30b8\u3002\u3053\u308c\u3082\u5206\u5272\u7d71\u6cbb\u6226\u7565\u3092\u4f53\u73fe\u3057\u3001\u975e\u5e38\u306b\u5927\u304d\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306e\u9375\u306f\u30c7\u30fc\u30bf\u306e\u5747\u7b49\u5206\u6563\u3067\u3059\u3002
        • \u8a08\u6570\u30bd\u30fc\u30c8\u306f\u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306e\u5909\u5f62\u3067\u3001\u5404\u30c7\u30fc\u30bf\u30dd\u30a4\u30f3\u30c8\u306e\u51fa\u73fe\u56de\u6570\u3092\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u3053\u3068\u3067\u30bd\u30fc\u30c8\u3057\u307e\u3059\u3002\u8a08\u6570\u30bd\u30fc\u30c8\u306f\u9650\u3089\u308c\u305f\u7bc4\u56f2\u306e\u30c7\u30fc\u30bf\u3092\u6301\u3064\u5927\u304d\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306b\u9069\u3057\u3066\u304a\u308a\u3001\u30c7\u30fc\u30bf\u3092\u6b63\u306e\u6574\u6570\u306b\u5909\u63db\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u57fa\u6570\u30bd\u30fc\u30c8\u306f\u6841\u3054\u3068\u306b\u30bd\u30fc\u30c8\u3059\u308b\u3053\u3068\u3067\u30c7\u30fc\u30bf\u3092\u51e6\u7406\u3057\u3001\u30c7\u30fc\u30bf\u304c\u56fa\u5b9a\u9577\u306e\u6570\u5024\u3068\u3057\u3066\u8868\u73fe\u3055\u308c\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u5168\u4f53\u7684\u306b\u3001\u79c1\u305f\u3061\u306f\u9ad8\u52b9\u7387\u3001\u5b89\u5b9a\u6027\u3001\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u64cd\u4f5c\u3001\u9069\u5fdc\u6027\u3092\u6301\u3064\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u6c42\u3081\u3066\u3044\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u4ed6\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u3084\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u540c\u69d8\u306b\u3001\u3053\u308c\u3089\u3059\u3079\u3066\u306e\u6761\u4ef6\u3092\u540c\u6642\u306b\u6e80\u305f\u3059\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u5b58\u5728\u3057\u307e\u305b\u3093\u3002\u5b9f\u969b\u306e\u5fdc\u7528\u3067\u306f\u3001\u30c7\u30fc\u30bf\u306e\u7279\u6027\u306b\u57fa\u3065\u3044\u3066\u9069\u5207\u306a\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u9078\u629e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u4ee5\u4e0b\u306e\u56f3\u306f\u3001\u52b9\u7387\u6027\u3001\u5b89\u5b9a\u6027\u3001\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u6027\u3001\u9069\u5fdc\u6027\u306e\u89b3\u70b9\u304b\u3089\u4e3b\u6d41\u306e\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u6bd4\u8f03\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 11-19 \u00a0 \u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6bd4\u8f03

        "},{"location":"chapter_sorting/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q: \u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b89\u5b9a\u6027\u306f\u3044\u3064\u5fc5\u8981\u3067\u3059\u304b\uff1f

        \u5b9f\u969b\u306b\u306f\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u4e00\u3064\u306e\u5c5e\u6027\u306b\u57fa\u3065\u3044\u3066\u30bd\u30fc\u30c8\u3059\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u5b66\u751f\u306f\u540d\u524d\u3068\u8eab\u9577\u306e\u5c5e\u6027\u3092\u6301\u3061\u3001\u591a\u6bb5\u968e\u30bd\u30fc\u30c8\u3092\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3092\u76ee\u6307\u3057\u307e\u3059\uff1a\u6700\u521d\u306b\u540d\u524d\u3067 (A, 180) (B, 185) (C, 170) (D, 170) \u3092\u53d6\u5f97\u3057\u3001\u6b21\u306b\u8eab\u9577\u3067\u3002\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u4e0d\u5b89\u5b9a\u306a\u305f\u3081\u3001(D, 170) (C, 170) (A, 180) (B, 185) \u306b\u306a\u3063\u3066\u3057\u307e\u3046\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        \u5b66\u751fD\u3068C\u306e\u4f4d\u7f6e\u304c\u4ea4\u63db\u3055\u308c\u3001\u540d\u524d\u306e\u9806\u5e8f\u6027\u304c\u7834\u3089\u308c\u3066\u3044\u308b\u306e\u304c\u5206\u304b\u308a\u307e\u3059\u3002\u3053\u308c\u306f\u671b\u307e\u3057\u304f\u3042\u308a\u307e\u305b\u3093\u3002

        Q: \u6b69\u54e8\u5206\u5272\u3067\u306e\u300c\u53f3\u304b\u3089\u5de6\u3078\u306e\u691c\u7d22\u300d\u3068\u300c\u5de6\u304b\u3089\u53f3\u3078\u306e\u691c\u7d22\u300d\u306e\u9806\u5e8f\u3092\u4ea4\u63db\u3067\u304d\u307e\u3059\u304b\uff1f

        \u3044\u3044\u3048\u3001\u6700\u5de6\u8981\u7d20\u3092\u30d4\u30dc\u30c3\u30c8\u3068\u3057\u3066\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001\u6700\u521d\u306b\u300c\u53f3\u304b\u3089\u5de6\u3078\u306e\u691c\u7d22\u300d\u3092\u884c\u3044\u3001\u6b21\u306b\u300c\u5de6\u304b\u3089\u53f3\u3078\u306e\u691c\u7d22\u300d\u3092\u884c\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u7d50\u8ad6\u306f\u3084\u3084\u76f4\u89b3\u306b\u53cd\u3059\u308b\u306e\u3067\u3001\u7406\u7531\u3092\u5206\u6790\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002

        \u6b69\u54e8\u5206\u5272 partition() \u306e\u6700\u5f8c\u306e\u30b9\u30c6\u30c3\u30d7\u306f nums[left] \u3068 nums[i] \u3092\u4ea4\u63db\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u4ea4\u63db\u5f8c\u3001\u30d4\u30dc\u30c3\u30c8\u306e\u5de6\u5074\u306e\u8981\u7d20\u306f\u3059\u3079\u3066\u30d4\u30dc\u30c3\u30c8\u4ee5\u4e0b\u306b\u306a\u308a\u307e\u3059\u3002\u3053\u308c\u306b\u306f\u6700\u5f8c\u306e\u4ea4\u63db\u524d\u306b nums[left] >= nums[i] \u304c\u6210\u308a\u7acb\u3064\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u300c\u5de6\u304b\u3089\u53f3\u3078\u306e\u691c\u7d22\u300d\u3092\u6700\u521d\u306b\u884c\u3044\u3001\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5927\u304d\u3044\u8981\u7d20\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3001i == j \u3067\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\u3057\u3001nums[j] == nums[i] > nums[left] \u3068\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u6700\u7d42\u4ea4\u63db\u64cd\u4f5c\u306f\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5927\u304d\u3044\u8981\u7d20\u3092\u914d\u5217\u306e\u5de6\u7aef\u306b\u4ea4\u63db\u3057\u3001\u6b69\u54e8\u5206\u5272\u3092\u5931\u6557\u3055\u305b\u307e\u3059\u3002

        \u4f8b\u3048\u3070\u3001\u914d\u5217 [0, 0, 0, 0, 1] \u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u6700\u521d\u306b\u300c\u5de6\u304b\u3089\u53f3\u3078\u306e\u691c\u7d22\u300d\u3092\u884c\u3046\u3068\u3001\u6b69\u54e8\u5206\u5272\u5f8c\u306e\u914d\u5217\u306f [1, 0, 0, 0, 0] \u3068\u306a\u308a\u3001\u3053\u308c\u306f\u6b63\u3057\u304f\u3042\u308a\u307e\u305b\u3093\u3002

        \u3055\u3089\u306b\u8003\u3048\u308b\u3068\u3001nums[right] \u3092\u30d4\u30dc\u30c3\u30c8\u3068\u3057\u3066\u9078\u629e\u3059\u308b\u5834\u5408\u3001\u307e\u3063\u305f\u304f\u9006\u3067\u3001\u6700\u521d\u306b\u300c\u5de6\u304b\u3089\u53f3\u3078\u306e\u691c\u7d22\u300d\u3092\u884c\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        Q: \u672b\u5c3e\u518d\u5e30\u6700\u9069\u5316\u306b\u3064\u3044\u3066\u3001\u77ed\u3044\u914d\u5217\u3092\u9078\u629e\u3059\u308b\u3053\u3068\u3067\u518d\u5e30\u306e\u6df1\u3055\u304c \\(\\log n\\) \u3092\u8d85\u3048\u306a\u3044\u3053\u3068\u3092\u4fdd\u8a3c\u3059\u308b\u306e\u306f\u306a\u305c\u3067\u3059\u304b\uff1f

        \u518d\u5e30\u306e\u6df1\u3055\u306f\u73fe\u5728\u30ea\u30bf\u30fc\u30f3\u3057\u3066\u3044\u306a\u3044\u518d\u5e30\u30e1\u30bd\u30c3\u30c9\u306e\u6570\u3067\u3059\u3002\u6b69\u54e8\u5206\u5272\u306e\u5404\u30e9\u30a6\u30f3\u30c9\u306f\u5143\u306e\u914d\u5217\u30922\u3064\u306e\u526f\u914d\u5217\u306b\u5206\u5272\u3057\u307e\u3059\u3002\u672b\u5c3e\u518d\u5e30\u6700\u9069\u5316\u306b\u3088\u308a\u3001\u518d\u5e30\u7684\u306b\u7d9a\u884c\u3059\u308b\u526f\u914d\u5217\u306e\u9577\u3055\u306f\u6700\u5927\u3067\u3082\u5143\u306e\u914d\u5217\u9577\u306e\u534a\u5206\u3067\u3059\u3002\u6700\u60aa\u306e\u5834\u5408\u5e38\u306b\u9577\u3055\u3092\u534a\u5206\u306b\u3059\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u6700\u7d42\u7684\u306a\u518d\u5e30\u306e\u6df1\u3055\u306f \\(\\log n\\) \u306b\u306a\u308a\u307e\u3059\u3002

        \u5143\u306e\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u3092\u898b\u76f4\u3059\u3068\u3001\u3088\u308a\u5927\u304d\u306a\u914d\u5217\u3092\u7d99\u7d9a\u7684\u306b\u518d\u5e30\u51e6\u7406\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u6700\u60aa\u306e\u5834\u5408 \\(n\\)\u3001\\(n - 1\\)\u3001...\u3001\\(2\\)\u3001\\(1\\) \u3067\u3001\u518d\u5e30\u306e\u6df1\u3055\u306f \\(n\\) \u306b\u306a\u308a\u307e\u3059\u3002\u672b\u5c3e\u518d\u5e30\u6700\u9069\u5316\u306f\u3053\u306e\u30b7\u30ca\u30ea\u30aa\u3092\u56de\u907f\u3067\u304d\u307e\u3059\u3002

        Q: \u914d\u5217\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u304c\u7b49\u3057\u3044\u5834\u5408\u3001\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n^2)\\) \u3067\u3059\u304b\uff1f\u3053\u306e\u52a3\u5316\u30b1\u30fc\u30b9\u3092\u3069\u3046\u51e6\u7406\u3059\u3079\u304d\u3067\u3059\u304b\uff1f

        \u306f\u3044\u3002\u3053\u306e\u72b6\u6cc1\u306b\u3064\u3044\u3066\u306f\u3001\u6b69\u54e8\u5206\u5272\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u3092\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5c0f\u3055\u3044\u3001\u7b49\u3057\u3044\u3001\u5927\u304d\u3044\u306e3\u3064\u306e\u90e8\u5206\u306b\u5206\u5272\u3059\u308b\u3053\u3068\u3092\u691c\u8a0e\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u5c0f\u3055\u3044\u90e8\u5206\u3068\u5927\u304d\u3044\u90e8\u5206\u306e\u307f\u3092\u518d\u5e30\u7684\u306b\u9032\u3081\u307e\u3059\u3002\u3053\u306e\u65b9\u6cd5\u3067\u306f\u3001\u3059\u3079\u3066\u306e\u5165\u529b\u8981\u7d20\u304c\u7b49\u3057\u3044\u914d\u5217\u30921\u30e9\u30a6\u30f3\u30c9\u306e\u6b69\u54e8\u5206\u5272\u3060\u3051\u3067\u30bd\u30fc\u30c8\u3067\u304d\u307e\u3059\u3002

        Q: \u306a\u305c\u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306e\u6700\u60aa\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n^2)\\) \u3067\u3059\u304b\uff1f

        \u6700\u60aa\u306e\u5834\u5408\u3001\u3059\u3079\u3066\u306e\u8981\u7d20\u304c\u540c\u3058\u30d0\u30b1\u30c3\u30c8\u306b\u914d\u7f6e\u3055\u308c\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u8981\u7d20\u3092\u30bd\u30fc\u30c8\u3059\u308b\u305f\u3081\u306b \\(O(n^2)\\) \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n^2)\\) \u306b\u306a\u308a\u307e\u3059\u3002

        "},{"location":"chapter_stack_and_queue/","title":"\u7b2c 5 \u7ae0 \u00a0 \u30b9\u30bf\u30c3\u30af\u3068\u30ad\u30e5\u30fc","text":"

        Abstract

        \u30b9\u30bf\u30c3\u30af\u306f\u7a4d\u307f\u91cd\u306d\u3089\u308c\u305f\u732b\u306e\u3088\u3046\u306a\u3082\u306e\u3067\u3001\u30ad\u30e5\u30fc\u306f\u4e00\u5217\u306b\u4e26\u3093\u3060\u732b\u306e\u3088\u3046\u306a\u3082\u306e\u3067\u3059\u3002

        \u305d\u308c\u3089\u306f\u305d\u308c\u305e\u308c\u3001\u5f8c\u5165\u5148\u51fa\uff08LIFO\uff09\u3068\u5148\u5165\u5148\u51fa\uff08FIFO\uff09\u306e\u8ad6\u7406\u95a2\u4fc2\u3092\u8868\u3057\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_stack_and_queue/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 5.1 \u00a0 \u30b9\u30bf\u30c3\u30af
        • 5.2 \u00a0 \u30ad\u30e5\u30fc
        • 5.3 \u00a0 \u4e21\u7aef\u30ad\u30e5\u30fc
        • 5.4 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_stack_and_queue/deque/","title":"5.3 \u00a0 \u4e21\u7aef\u30ad\u30e5\u30fc","text":"

        \u30ad\u30e5\u30fc\u3067\u306f\u3001\u5148\u982d\u304b\u3089\u306e\u8981\u7d20\u306e\u524a\u9664\u3084\u672b\u5c3e\u3078\u306e\u8981\u7d20\u306e\u8ffd\u52a0\u306e\u307f\u304c\u53ef\u80fd\u3067\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u4e21\u7aef\u30ad\u30e5\u30fc\uff08deque\uff09\u306f\u3088\u308a\u67d4\u8edf\u6027\u3092\u63d0\u4f9b\u3057\u3001\u5148\u982d\u3068\u672b\u5c3e\u306e\u4e21\u65b9\u3067\u8981\u7d20\u306e\u8ffd\u52a0\u3084\u524a\u9664\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002

        \u56f3 5-7 \u00a0 \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u64cd\u4f5c

        "},{"location":"chapter_stack_and_queue/deque/#531","title":"5.3.1 \u00a0 \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c","text":"

        \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002\u5177\u4f53\u7684\u306a\u30e1\u30bd\u30c3\u30c9\u540d\u306f\u4f7f\u7528\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u3088\u3063\u3066\u7570\u306a\u308a\u307e\u3059\u3002

        \u8868 5-3 \u00a0 \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u64cd\u4f5c\u52b9\u7387

        \u30e1\u30bd\u30c3\u30c9\u540d \u8aac\u660e \u6642\u9593\u8a08\u7b97\u91cf pushFirst() \u5148\u982d\u306b\u8981\u7d20\u3092\u8ffd\u52a0 \\(O(1)\\) pushLast() \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 \\(O(1)\\) popFirst() \u5148\u982d\u8981\u7d20\u3092\u524a\u9664 \\(O(1)\\) popLast() \u672b\u5c3e\u8981\u7d20\u3092\u524a\u9664 \\(O(1)\\) peekFirst() \u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 \\(O(1)\\) peekLast() \u672b\u5c3e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 \\(O(1)\\)

        \u540c\u69d8\u306b\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u5b9f\u88c5\u3055\u308c\u305f\u4e21\u7aef\u30ad\u30e5\u30fc\u30af\u30e9\u30b9\u3092\u76f4\u63a5\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin deque.py
        from collections import deque\n\n# \u4e21\u7aef\u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316\ndeq: deque[int] = deque()\n\n# \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\ndeq.append(2)      # \u672b\u5c3e\u306b\u8ffd\u52a0\ndeq.append(5)\ndeq.append(4)\ndeq.appendleft(3)  # \u5148\u982d\u306b\u8ffd\u52a0\ndeq.appendleft(1)\n\n# \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\nfront: int = deq[0]  # \u5148\u982d\u8981\u7d20\nrear: int = deq[-1]  # \u672b\u5c3e\u8981\u7d20\n\n# \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\npop_front: int = deq.popleft()  # \u5148\u982d\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\npop_rear: int = deq.pop()       # \u672b\u5c3e\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\n\n# \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97\nsize: int = len(deq)\n\n# \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\nis_empty: bool = len(deq) == 0\n
        deque.cpp
        /* \u4e21\u7aef\u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\ndeque<int> deque;\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\ndeque.push_back(2);   // \u672b\u5c3e\u306b\u8ffd\u52a0\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3);  // \u5148\u982d\u306b\u8ffd\u52a0\ndeque.push_front(1);\n\n/* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint front = deque.front(); // \u5148\u982d\u8981\u7d20\nint back = deque.back();   // \u672b\u5c3e\u8981\u7d20\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\ndeque.pop_front();  // \u5148\u982d\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\ndeque.pop_back();   // \u672b\u5c3e\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = deque.size();\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d */\nbool empty = deque.empty();\n
        deque.java
        /* \u4e21\u7aef\u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\nDeque<Integer> deque = new LinkedList<>();\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\ndeque.offerLast(2);   // \u672b\u5c3e\u306b\u8ffd\u52a0\ndeque.offerLast(5);\ndeque.offerLast(4);\ndeque.offerFirst(3);  // \u5148\u982d\u306b\u8ffd\u52a0\ndeque.offerFirst(1);\n\n/* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint peekFirst = deque.peekFirst();  // \u5148\u982d\u8981\u7d20\nint peekLast = deque.peekLast();    // \u672b\u5c3e\u8981\u7d20\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\nint popFirst = deque.pollFirst();  // \u5148\u982d\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\nint popLast = deque.pollLast();    // \u672b\u5c3e\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = deque.size();\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d */\nboolean isEmpty = deque.isEmpty();\n
        deque.cs
        /* \u4e21\u7aef\u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// C#\u3067\u306f\u3001LinkedList\u3092\u4e21\u7aef\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\nLinkedList<int> deque = new();\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\ndeque.AddLast(2);   // \u672b\u5c3e\u306b\u8ffd\u52a0\ndeque.AddLast(5);\ndeque.AddLast(4);\ndeque.AddFirst(3);  // \u5148\u982d\u306b\u8ffd\u52a0\ndeque.AddFirst(1);\n\n/* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint peekFirst = deque.First.Value;  // \u5148\u982d\u8981\u7d20\nint peekLast = deque.Last.Value;    // \u672b\u5c3e\u8981\u7d20\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\ndeque.RemoveFirst();  // \u5148\u982d\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\ndeque.RemoveLast();   // \u672b\u5c3e\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = deque.Count;\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d */\nbool isEmpty = deque.Count == 0;\n
        deque_test.go
        /* \u4e21\u7aef\u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// Go\u3067\u306f\u3001list\u3092\u4e21\u7aef\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\ndeque := list.New()\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\ndeque.PushBack(2)      // \u672b\u5c3e\u306b\u8ffd\u52a0\ndeque.PushBack(5)\ndeque.PushBack(4)\ndeque.PushFront(3)     // \u5148\u982d\u306b\u8ffd\u52a0\ndeque.PushFront(1)\n\n/* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nfront := deque.Front() // \u5148\u982d\u8981\u7d20\nrear := deque.Back()   // \u672b\u5c3e\u8981\u7d20\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\ndeque.Remove(front)    // \u5148\u982d\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\ndeque.Remove(rear)     // \u672b\u5c3e\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nsize := deque.Len()\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d */\nisEmpty := deque.Len() == 0\n
        deque.swift
        /* \u4e21\u7aef\u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// Swift\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u4e21\u7aef\u30ad\u30e5\u30fc\u30af\u30e9\u30b9\u304c\u306a\u3044\u305f\u3081\u3001Array\u3092\u4e21\u7aef\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\nvar deque: [Int] = []\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\ndeque.append(2) // \u672b\u5c3e\u306b\u8ffd\u52a0\ndeque.append(5)\ndeque.append(4)\ndeque.insert(3, at: 0) // \u5148\u982d\u306b\u8ffd\u52a0\ndeque.insert(1, at: 0)\n\n/* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nlet peekFirst = deque.first! // \u5148\u982d\u8981\u7d20\nlet peekLast = deque.last!   // \u672b\u5c3e\u8981\u7d20\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\n// Array\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001popFirst\u306e\u8a08\u7b97\u91cf\u306fO(n)\nlet popFirst = deque.removeFirst() // \u5148\u982d\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\nlet popLast = deque.removeLast()   // \u672b\u5c3e\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nlet size = deque.count\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d */\nlet isEmpty = deque.isEmpty\n
        deque.js
        /* \u4e21\u7aef\u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// JavaScript\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u306a\u3044\u305f\u3081\u3001Array\u3092\u4e21\u7aef\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\nconst deque = [];\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// \u6ce8\u610f\uff1aunshift()\u306f\u914d\u5217\u306e\u305f\u3081\u6642\u9593\u8a08\u7b97\u91cf\u304cO(n)\ndeque.unshift(3);\ndeque.unshift(1);\n\n/* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nconst peekFirst = deque[0]; // \u5148\u982d\u8981\u7d20\nconst peekLast = deque[deque.length - 1]; // \u672b\u5c3e\u8981\u7d20\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\n// \u6ce8\u610f\uff1ashift()\u306f\u914d\u5217\u306e\u305f\u3081\u6642\u9593\u8a08\u7b97\u91cf\u304cO(n)\nconst popFront = deque.shift(); // \u5148\u982d\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\nconst popBack = deque.pop();    // \u672b\u5c3e\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nconst size = deque.length;\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d */\nconst isEmpty = size === 0;\n
        deque.ts
        /* \u4e21\u7aef\u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// TypeScript\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u306a\u3044\u305f\u3081\u3001Array\u3092\u4e21\u7aef\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\nconst deque: number[] = [];\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// \u6ce8\u610f\uff1aunshift()\u306f\u914d\u5217\u306e\u305f\u3081\u6642\u9593\u8a08\u7b97\u91cf\u304cO(n)\ndeque.unshift(3);\ndeque.unshift(1);\n\n/* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nconst peekFirst: number = deque[0]; // \u5148\u982d\u8981\u7d20\nconst peekLast: number = deque[deque.length - 1]; // \u672b\u5c3e\u8981\u7d20\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\n// \u6ce8\u610f\uff1ashift()\u306f\u914d\u5217\u306e\u305f\u3081\u6642\u9593\u8a08\u7b97\u91cf\u304cO(n)\nconst popFront: number = deque.shift() as number; // \u5148\u982d\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\nconst popBack: number = deque.pop() as number;    // \u672b\u5c3e\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nconst size: number = deque.length;\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d */\nconst isEmpty: boolean = size === 0;\n
        deque.dart
        /* \u4e21\u7aef\u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// Dart\u3067\u306f\u3001Queue\u304c\u4e21\u7aef\u30ad\u30e5\u30fc\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u308b\nQueue<int> deque = Queue<int>();\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\ndeque.addLast(2);  // \u672b\u5c3e\u306b\u8ffd\u52a0\ndeque.addLast(5);\ndeque.addLast(4);\ndeque.addFirst(3); // \u5148\u982d\u306b\u8ffd\u52a0\ndeque.addFirst(1);\n\n/* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint peekFirst = deque.first; // \u5148\u982d\u8981\u7d20\nint peekLast = deque.last;   // \u672b\u5c3e\u8981\u7d20\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\nint popFirst = deque.removeFirst(); // \u5148\u982d\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\nint popLast = deque.removeLast();   // \u672b\u5c3e\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = deque.length;\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d */\nbool isEmpty = deque.isEmpty;\n
        deque.rs
        /* \u4e21\u7aef\u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\nlet mut deque: VecDeque<u32> = VecDeque::new();\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\ndeque.push_back(2);  // \u672b\u5c3e\u306b\u8ffd\u52a0\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3); // \u5148\u982d\u306b\u8ffd\u52a0\ndeque.push_front(1);\n\n/* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nif let Some(front) = deque.front() { // \u5148\u982d\u8981\u7d20\n}\nif let Some(rear) = deque.back() {   // \u672b\u5c3e\u8981\u7d20\n}\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\nif let Some(pop_front) = deque.pop_front() { // \u5148\u982d\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\n}\nif let Some(pop_rear) = deque.pop_back() {   // \u672b\u5c3e\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\n}\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nlet size = deque.len();\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d */\nlet is_empty = deque.is_empty();\n
        deque.c
        // C\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u305b\u3093\n
        deque.kt
        \n
        "},{"location":"chapter_stack_and_queue/deque/#532","title":"5.3.2 \u00a0 \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u5b9f\u88c5 *","text":"

        \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u5b9f\u88c5\u306f\u901a\u5e38\u306e\u30ad\u30e5\u30fc\u306e\u5b9f\u88c5\u3068\u4f3c\u3066\u304a\u308a\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u307e\u305f\u306f\u914d\u5217\u3092\u57fa\u76e4\u3068\u306a\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u3057\u3066\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_stack_and_queue/deque/#1","title":"1. \u00a0 \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u5b9f\u88c5","text":"

        \u524d\u7bc0\u3067\u3001\u901a\u5e38\u306e\u5358\u4e00\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u4f7f\u3063\u3066\u30ad\u30e5\u30fc\u3092\u5b9f\u88c5\u3057\u305f\u3053\u3068\u3092\u601d\u3044\u51fa\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3053\u308c\u306f\u5148\u982d\u304b\u3089\u306e\u524a\u9664\uff08\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\u306b\u5bfe\u5fdc\uff09\u3068\u672b\u5c3e\u3078\u306e\u65b0\u3057\u3044\u8981\u7d20\u306e\u8ffd\u52a0\uff08\u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c\u306b\u5bfe\u5fdc\uff09\u3092\u4fbf\u5229\u306b\u884c\u3048\u308b\u305f\u3081\u3067\u3057\u305f\u3002

        \u4e21\u7aef\u30ad\u30e5\u30fc\u3067\u306f\u3001\u5148\u982d\u3068\u672b\u5c3e\u306e\u4e21\u65b9\u3067\u30a8\u30f3\u30ad\u30e5\u30fc\u3068\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\u3092\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u4e21\u7aef\u30ad\u30e5\u30fc\u306f\u9006\u65b9\u5411\u306e\u64cd\u4f5c\u3082\u5b9f\u88c5\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u305f\u3081\u3001\u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u57fa\u76e4\u3068\u306a\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u3057\u3066\u300c\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u300d\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u5148\u982d\u30ce\u30fc\u30c9\u3068\u672b\u5c3e\u30ce\u30fc\u30c9\u3092\u305d\u308c\u305e\u308c\u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u524d\u7aef\u3068\u5f8c\u7aef\u3068\u3057\u3066\u6271\u3044\u3001\u4e21\u7aef\u3067\u306e\u30ce\u30fc\u30c9\u306e\u8ffd\u52a0\u3068\u524a\u9664\u6a5f\u80fd\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002

        LinkedListDequepushLast()pushFirst()popLast()popFirst()

        \u56f3 5-8 \u00a0 \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u3088\u308b\u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u30a8\u30f3\u30ad\u30e5\u30fc\u3068\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\u306e\u5b9f\u88c5

        \u5b9f\u88c5\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linkedlist_deque.py
        class ListNode:\n    \"\"\"\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\"\"\"\n\n    def __init__(self, val: int):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        self.val: int = val\n        self.next: ListNode | None = None  # \u5f8c\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n        self.prev: ListNode | None = None  # \u524d\u99c6\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n\nclass LinkedListDeque:\n    \"\"\"\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30d9\u30fc\u30b9\u306e\u53cc\u7aef\u30ad\u30e5\u30fc\u30af\u30e9\u30b9\"\"\"\n\n    def __init__(self):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        self._front: ListNode | None = None  # \u30d8\u30c3\u30c9\u30ce\u30fc\u30c9 front\n        self._rear: ListNode | None = None  # \u30c6\u30fc\u30eb\u30ce\u30fc\u30c9 rear\n        self._size: int = 0  # \u53cc\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\n\n    def size(self) -> int:\n        \"\"\"\u53cc\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u53cc\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\"\"\"\n        return self._size == 0\n\n    def push(self, num: int, is_front: bool):\n        \"\"\"\u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c\"\"\"\n        node = ListNode(num)\n        # \u30ea\u30b9\u30c8\u304c\u7a7a\u306e\u5834\u5408\u3001front \u3068 rear \u306e\u4e21\u65b9\u3092 node \u306b\u5411\u3051\u308b\n        if self.is_empty():\n            self._front = self._rear = node\n        # \u524d\u7aef\u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c\n        elif is_front:\n            # \u30ce\u30fc\u30c9\u3092\u30ea\u30b9\u30c8\u306e\u5148\u982d\u306b\u8ffd\u52a0\n            self._front.prev = node\n            node.next = self._front\n            self._front = node  # \u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u3092\u66f4\u65b0\n        # \u5f8c\u7aef\u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c\n        else:\n            # \u30ce\u30fc\u30c9\u3092\u30ea\u30b9\u30c8\u306e\u672b\u5c3e\u306b\u8ffd\u52a0\n            self._rear.next = node\n            node.prev = self._rear\n            self._rear = node  # \u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u3092\u66f4\u65b0\n        self._size += 1  # \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u66f4\u65b0\n\n    def push_first(self, num: int):\n        \"\"\"\u524d\u7aef\u30a8\u30f3\u30ad\u30e5\u30fc\"\"\"\n        self.push(num, True)\n\n    def push_last(self, num: int):\n        \"\"\"\u5f8c\u7aef\u30a8\u30f3\u30ad\u30e5\u30fc\"\"\"\n        self.push(num, False)\n\n    def pop(self, is_front: bool) -> int:\n        \"\"\"\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Double-ended queue is empty\")\n        # \u524d\u7aef\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\n        if is_front:\n            val: int = self._front.val  # \u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u306e\u5024\u3092\u4e00\u6642\u7684\u306b\u4fdd\u5b58\n            # \u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u3092\u524a\u9664\n            fnext: ListNode | None = self._front.next\n            if fnext is not None:\n                fnext.prev = None\n                self._front.next = None\n            self._front = fnext  # \u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u3092\u66f4\u65b0\n        # \u5f8c\u7aef\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\n        else:\n            val: int = self._rear.val  # \u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u306e\u5024\u3092\u4e00\u6642\u7684\u306b\u4fdd\u5b58\n            # \u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u3092\u524a\u9664\n            rprev: ListNode | None = self._rear.prev\n            if rprev is not None:\n                rprev.next = None\n                self._rear.prev = None\n            self._rear = rprev  # \u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u3092\u66f4\u65b0\n        self._size -= 1  # \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u66f4\u65b0\n        return val\n\n    def pop_first(self) -> int:\n        \"\"\"\u524d\u7aef\u30c7\u30ad\u30e5\u30fc\"\"\"\n        return self.pop(True)\n\n    def pop_last(self) -> int:\n        \"\"\"\u5f8c\u7aef\u30c7\u30ad\u30e5\u30fc\"\"\"\n        return self.pop(False)\n\n    def peek_first(self) -> int:\n        \"\"\"\u524d\u7aef\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Double-ended queue is empty\")\n        return self._front.val\n\n    def peek_last(self) -> int:\n        \"\"\"\u5f8c\u7aef\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Double-ended queue is empty\")\n        return self._rear.val\n\n    def to_array(self) -> list[int]:\n        \"\"\"\u51fa\u529b\u7528\u306e\u914d\u5217\u3092\u8fd4\u3059\"\"\"\n        node = self._front\n        res = [0] * self.size()\n        for i in range(self.size()):\n            res[i] = node.val\n            node = node.next\n        return res\n
        linkedlist_deque.cpp
        /* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9 */\nstruct DoublyListNode {\n    int val;              // \u30ce\u30fc\u30c9\u306e\u5024\n    DoublyListNode *next; // \u5f8c\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    DoublyListNode *prev; // \u524d\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {\n    }\n};\n\n/* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u4e21\u7aef\u30ad\u30e5\u30fc\u30af\u30e9\u30b9 */\nclass LinkedListDeque {\n  private:\n    DoublyListNode *front, *rear; // \u5148\u982d\u30ce\u30fc\u30c9front\u3001\u672b\u5c3e\u30ce\u30fc\u30c9rear\n    int queSize = 0;              // \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\n\n  public:\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    LinkedListDeque() : front(nullptr), rear(nullptr) {\n    }\n\n    /* \u30c7\u30b9\u30c8\u30e9\u30af\u30bf */\n    ~LinkedListDeque() {\n        // \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8d70\u67fb\u3001\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3001\u30e1\u30e2\u30ea\u3092\u89e3\u653e\n        DoublyListNode *pre, *cur = front;\n        while (cur != nullptr) {\n            pre = cur;\n            cur = cur->next;\n            delete pre;\n        }\n    }\n\n    /* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    int size() {\n        return queSize;\n    }\n\n    /* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    bool isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c */\n    void push(int num, bool isFront) {\n        DoublyListNode *node = new DoublyListNode(num);\n        // \u30ea\u30b9\u30c8\u304c\u7a7a\u306e\u5834\u5408\u3001front\u3068rear\u306e\u4e21\u65b9\u3092node\u306b\u5411\u3051\u308b\n        if (isEmpty())\n            front = rear = node;\n        // \u5148\u982d\u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c\n        else if (isFront) {\n            // \u30ce\u30fc\u30c9\u3092\u30ea\u30b9\u30c8\u306e\u5148\u982d\u306b\u8ffd\u52a0\n            front->prev = node;\n            node->next = front;\n            front = node; // \u5148\u982d\u30ce\u30fc\u30c9\u3092\u66f4\u65b0\n        // \u672b\u5c3e\u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c\n        } else {\n            // \u30ce\u30fc\u30c9\u3092\u30ea\u30b9\u30c8\u306e\u672b\u5c3e\u306b\u8ffd\u52a0\n            rear->next = node;\n            node->prev = rear;\n            rear = node; // \u672b\u5c3e\u30ce\u30fc\u30c9\u3092\u66f4\u65b0\n        }\n        queSize++; // \u30ad\u30e5\u30fc\u9577\u3092\u66f4\u65b0\n    }\n\n    /* \u5148\u982d\u30a8\u30f3\u30ad\u30e5\u30fc */\n    void pushFirst(int num) {\n        push(num, true);\n    }\n\n    /* \u672b\u5c3e\u30a8\u30f3\u30ad\u30e5\u30fc */\n    void pushLast(int num) {\n        push(num, false);\n    }\n\n    /* \u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c */\n    int pop(bool isFront) {\n        if (isEmpty())\n            throw out_of_range(\"Queue is empty\");\n        int val;\n        // \u5148\u982d\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\n        if (isFront) {\n            val = front->val; // \u5148\u982d\u30ce\u30fc\u30c9\u306e\u5024\u3092\u4e00\u6642\u4fdd\u5b58\n            // \u5148\u982d\u30ce\u30fc\u30c9\u3092\u524a\u9664\n            DoublyListNode *fNext = front->next;\n            if (fNext != nullptr) {\n                fNext->prev = nullptr;\n                front->next = nullptr;\n            }\n            delete front;\n            front = fNext; // \u5148\u982d\u30ce\u30fc\u30c9\u3092\u66f4\u65b0\n        // \u672b\u5c3e\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\n        } else {\n            val = rear->val; // \u672b\u5c3e\u30ce\u30fc\u30c9\u306e\u5024\u3092\u4e00\u6642\u4fdd\u5b58\n            // \u672b\u5c3e\u30ce\u30fc\u30c9\u3092\u524a\u9664\n            DoublyListNode *rPrev = rear->prev;\n            if (rPrev != nullptr) {\n                rPrev->next = nullptr;\n                rear->prev = nullptr;\n            }\n            delete rear;\n            rear = rPrev; // \u672b\u5c3e\u30ce\u30fc\u30c9\u3092\u66f4\u65b0\n        }\n        queSize--; // \u30ad\u30e5\u30fc\u9577\u3092\u66f4\u65b0\n        return val;\n    }\n\n    /* \u5148\u982d\u30c7\u30ad\u30e5\u30fc */\n    int popFirst() {\n        return pop(true);\n    }\n\n    /* \u672b\u5c3e\u30c7\u30ad\u30e5\u30fc */\n    int popLast() {\n        return pop(false);\n    }\n\n    /* \u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    int peekFirst() {\n        if (isEmpty())\n            throw out_of_range(\"Double-ended queue is empty\");\n        return front->val;\n    }\n\n    /* \u672b\u5c3e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    int peekLast() {\n        if (isEmpty())\n            throw out_of_range(\"Double-ended queue is empty\");\n        return rear->val;\n    }\n\n    /* \u5370\u5237\u7528\u306b\u914d\u5217\u3092\u8fd4\u5374 */\n    vector<int> toVector() {\n        DoublyListNode *node = front;\n        vector<int> res(size());\n        for (int i = 0; i < res.size(); i++) {\n            res[i] = node->val;\n            node = node->next;\n        }\n        return res;\n    }\n};\n
        linkedlist_deque.java
        /* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9 */\nclass ListNode {\n    int val; // \u30ce\u30fc\u30c9\u5024\n    ListNode next; // \u5f8c\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    ListNode prev; // \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n\n    ListNode(int val) {\n        this.val = val;\n        prev = next = null;\n    }\n}\n\n/* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u4e21\u7aef\u30ad\u30e5\u30fc\u30af\u30e9\u30b9 */\nclass LinkedListDeque {\n    private ListNode front, rear; // \u5148\u982d\u30ce\u30fc\u30c9 front\u3001\u672b\u5c3e\u30ce\u30fc\u30c9 rear\n    private int queSize = 0; // \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\n\n    public LinkedListDeque() {\n        front = rear = null;\n    }\n\n    /* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    public int size() {\n        return queSize;\n    }\n\n    /* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c */\n    private void push(int num, boolean isFront) {\n        ListNode node = new ListNode(num);\n        // \u30ea\u30b9\u30c8\u304c\u7a7a\u306e\u5834\u5408\u3001front \u3068 rear \u306e\u4e21\u65b9\u3092 node \u306b\u6307\u3059\n        if (isEmpty())\n            front = rear = node;\n        // \u5148\u982d\u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c\n        else if (isFront) {\n            // node \u3092\u30ea\u30b9\u30c8\u306e\u5148\u982d\u306b\u8ffd\u52a0\n            front.prev = node;\n            node.next = front;\n            front = node; // front \u3092\u66f4\u65b0\n        // \u672b\u5c3e\u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c\n        } else {\n            // node \u3092\u30ea\u30b9\u30c8\u306e\u672b\u5c3e\u306b\u8ffd\u52a0\n            rear.next = node;\n            node.prev = rear;\n            rear = node; // rear \u3092\u66f4\u65b0\n        }\n        queSize++; // \u9577\u3055\u3092\u66f4\u65b0\n    }\n\n    /* \u5148\u982d\u30a8\u30f3\u30ad\u30e5\u30fc */\n    public void pushFirst(int num) {\n        push(num, true);\n    }\n\n    /* \u672b\u5c3e\u30a8\u30f3\u30ad\u30e5\u30fc */\n    public void pushLast(int num) {\n        push(num, false);\n    }\n\n    /* \u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c */\n    private int pop(boolean isFront) {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        int val;\n        // \u5148\u982d\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\n        if (isFront) {\n            val = front.val; // \u4e00\u6642\u7684\u306b\u5148\u982d\u30ce\u30fc\u30c9\u5024\u3092\u4fdd\u5b58\n            // \u6b21\u306e\u30ce\u30fc\u30c9\u3092\u524a\u9664\n            ListNode fNext = front.next;\n            if (fNext != null) {\n                fNext.prev = null;\n                front.next = null;\n            }\n            front = fNext; // front \u3092\u66f4\u65b0\n        // \u672b\u5c3e\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\n        } else {\n            val = rear.val; // \u4e00\u6642\u7684\u306b\u672b\u5c3e\u30ce\u30fc\u30c9\u5024\u3092\u4fdd\u5b58\n            // \u524d\u306e\u30ce\u30fc\u30c9\u3092\u524a\u9664\n            ListNode rPrev = rear.prev;\n            if (rPrev != null) {\n                rPrev.next = null;\n                rear.prev = null;\n            }\n            rear = rPrev; // rear \u3092\u66f4\u65b0\n        }\n        queSize--; // \u9577\u3055\u3092\u66f4\u65b0\n        return val;\n    }\n\n    /* \u5148\u982d\u30c7\u30ad\u30e5\u30fc */\n    public int popFirst() {\n        return pop(true);\n    }\n\n    /* \u672b\u5c3e\u30c7\u30ad\u30e5\u30fc */\n    public int popLast() {\n        return pop(false);\n    }\n\n    /* \u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    public int peekFirst() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return front.val;\n    }\n\n    /* \u672b\u5c3e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    public int peekLast() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return rear.val;\n    }\n\n    /* \u914d\u5217\u3092\u8fd4\u3059 */\n    public int[] toArray() {\n        ListNode node = front;\n        int[] res = new int[size()];\n        for (int i = 0; i < res.length; i++) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_deque.cs
        [class]{ListNode}-[func]{}\n\n[class]{LinkedListDeque}-[func]{}\n
        linkedlist_deque.go
        [class]{linkedListDeque}-[func]{}\n
        linkedlist_deque.swift
        [class]{ListNode}-[func]{}\n\n[class]{LinkedListDeque}-[func]{}\n
        linkedlist_deque.js
        [class]{ListNode}-[func]{}\n\n[class]{LinkedListDeque}-[func]{}\n
        linkedlist_deque.ts
        [class]{ListNode}-[func]{}\n\n[class]{LinkedListDeque}-[func]{}\n
        linkedlist_deque.dart
        [class]{ListNode}-[func]{}\n\n[class]{LinkedListDeque}-[func]{}\n
        linkedlist_deque.rs
        [class]{ListNode}-[func]{}\n\n[class]{LinkedListDeque}-[func]{}\n
        linkedlist_deque.c
        [class]{DoublyListNode}-[func]{}\n\n[class]{LinkedListDeque}-[func]{}\n
        linkedlist_deque.kt
        [class]{ListNode}-[func]{}\n\n[class]{LinkedListDeque}-[func]{}\n
        linkedlist_deque.rb
        [class]{ListNode}-[func]{}\n\n[class]{LinkedListDeque}-[func]{}\n
        "},{"location":"chapter_stack_and_queue/deque/#2","title":"2. \u00a0 \u914d\u5217\u306b\u57fa\u3065\u304f\u5b9f\u88c5","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u914d\u5217\u3067\u30ad\u30e5\u30fc\u3092\u5b9f\u88c5\u3059\u308b\u306e\u3068\u540c\u69d8\u306b\u3001\u5faa\u74b0\u914d\u5217\u3092\u4f7f\u3063\u3066\u4e21\u7aef\u30ad\u30e5\u30fc\u3092\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002

        ArrayDequepushLast()pushFirst()popLast()popFirst()

        \u56f3 5-9 \u00a0 \u914d\u5217\u306b\u3088\u308b\u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u30a8\u30f3\u30ad\u30e5\u30fc\u3068\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\u306e\u5b9f\u88c5

        \u5b9f\u88c5\u3067\u306f\u300c\u524d\u7aef\u30a8\u30f3\u30ad\u30e5\u30fc\u300d\u3068\u300c\u5f8c\u7aef\u30c7\u30ad\u30e5\u30fc\u300d\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u8ffd\u52a0\u3059\u308b\u3060\u3051\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_deque.py
        class ArrayDeque:\n    \"\"\"\u5faa\u74b0\u914d\u5217\u30d9\u30fc\u30b9\u306e\u53cc\u7aef\u30ad\u30e5\u30fc\u30af\u30e9\u30b9\"\"\"\n\n    def __init__(self, capacity: int):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        self._nums: list[int] = [0] * capacity\n        self._front: int = 0\n        self._size: int = 0\n\n    def capacity(self) -> int:\n        \"\"\"\u53cc\u7aef\u30ad\u30e5\u30fc\u306e\u5bb9\u91cf\u3092\u53d6\u5f97\"\"\"\n        return len(self._nums)\n\n    def size(self) -> int:\n        \"\"\"\u53cc\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u53cc\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\"\"\"\n        return self._size == 0\n\n    def index(self, i: int) -> int:\n        \"\"\"\u5faa\u74b0\u914d\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97\"\"\"\n        # \u30e2\u30b8\u30e5\u30ed\u6f14\u7b97\u306b\u3088\u3063\u3066\u5faa\u74b0\u914d\u5217\u3092\u5b9f\u88c5\n        # i \u304c\u914d\u5217\u306e\u672b\u5c3e\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u5148\u982d\u306b\u623b\u308b\n        # i \u304c\u914d\u5217\u306e\u5148\u982d\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u672b\u5c3e\u306b\u623b\u308b\n        return (i + self.capacity()) % self.capacity()\n\n    def push_first(self, num: int):\n        \"\"\"\u524d\u7aef\u30a8\u30f3\u30ad\u30e5\u30fc\"\"\"\n        if self._size == self.capacity():\n            print(\"\u53cc\u7aef\u30ad\u30e5\u30fc\u304c\u6e80\u676f\u3067\u3059\")\n            return\n        # \u30d5\u30ed\u30f3\u30c8\u30dd\u30a4\u30f3\u30bf\u3092\u5de6\u306b1\u3064\u79fb\u52d5\n        # \u30e2\u30b8\u30e5\u30ed\u6f14\u7b97\u306b\u3088\u3063\u3066\u30d5\u30ed\u30f3\u30c8\u304c\u914d\u5217\u306e\u5148\u982d\u3092\u8d85\u3048\u3066\u672b\u5c3e\u306b\u623b\u308b\u3053\u3068\u3092\u5b9f\u88c5\n        self._front = self.index(self._front - 1)\n        # num \u3092\u524d\u7aef\u306b\u8ffd\u52a0\n        self._nums[self._front] = num\n        self._size += 1\n\n    def push_last(self, num: int):\n        \"\"\"\u5f8c\u7aef\u30a8\u30f3\u30ad\u30e5\u30fc\"\"\"\n        if self._size == self.capacity():\n            print(\"\u53cc\u7aef\u30ad\u30e5\u30fc\u304c\u6e80\u676f\u3067\u3059\")\n            return\n        # \u30ea\u30a2\u30dd\u30a4\u30f3\u30bf\u3092\u8a08\u7b97\u3001\u30ea\u30a2\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 + 1 \u3092\u6307\u3059\n        rear = self.index(self._front + self._size)\n        # num \u3092\u5f8c\u7aef\u306b\u8ffd\u52a0\n        self._nums[rear] = num\n        self._size += 1\n\n    def pop_first(self) -> int:\n        \"\"\"\u524d\u7aef\u30c7\u30ad\u30e5\u30fc\"\"\"\n        num = self.peek_first()\n        # \u30d5\u30ed\u30f3\u30c8\u30dd\u30a4\u30f3\u30bf\u30921\u3064\u5f8c\u308d\u306b\u79fb\u52d5\n        self._front = self.index(self._front + 1)\n        self._size -= 1\n        return num\n\n    def pop_last(self) -> int:\n        \"\"\"\u5f8c\u7aef\u30c7\u30ad\u30e5\u30fc\"\"\"\n        num = self.peek_last()\n        self._size -= 1\n        return num\n\n    def peek_first(self) -> int:\n        \"\"\"\u524d\u7aef\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Double-ended queue is empty\")\n        return self._nums[self._front]\n\n    def peek_last(self) -> int:\n        \"\"\"\u5f8c\u7aef\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Double-ended queue is empty\")\n        # \u5f8c\u7aef\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97\n        last = self.index(self._front + self._size - 1)\n        return self._nums[last]\n\n    def to_array(self) -> list[int]:\n        \"\"\"\u51fa\u529b\u7528\u306e\u914d\u5217\u3092\u8fd4\u3059\"\"\"\n        # \u6709\u52b9\u306a\u9577\u3055\u7bc4\u56f2\u5185\u306e\u8981\u7d20\u306e\u307f\u3092\u5909\u63db\n        res = []\n        for i in range(self._size):\n            res.append(self._nums[self.index(self._front + i)])\n        return res\n
        array_deque.cpp
        /* \u5faa\u74b0\u914d\u5217\u306b\u57fa\u3065\u304f\u4e21\u7aef\u30ad\u30e5\u30fc\u30af\u30e9\u30b9 */\nclass ArrayDeque {\n  private:\n    vector<int> nums; // \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u8981\u7d20\u3092\u683c\u7d0d\u3059\u308b\u914d\u5217\n    int front;        // \u5148\u982d\u30dd\u30a4\u30f3\u30bf\u3001\u5148\u982d\u8981\u7d20\u3092\u6307\u3059\n    int queSize;      // \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\n\n  public:\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    ArrayDeque(int capacity) {\n        nums.resize(capacity);\n        front = queSize = 0;\n    }\n\n    /* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u5bb9\u91cf\u3092\u53d6\u5f97 */\n    int capacity() {\n        return nums.size();\n    }\n\n    /* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    int size() {\n        return queSize;\n    }\n\n    /* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    bool isEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u5faa\u74b0\u914d\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97 */\n    int index(int i) {\n        // \u5270\u4f59\u6f14\u7b97\u3067\u5faa\u74b0\u914d\u5217\u3092\u5b9f\u73fe\n        // i\u304c\u914d\u5217\u306e\u672b\u5c3e\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u5148\u982d\u306b\u623b\u308b\n        // i\u304c\u914d\u5217\u306e\u5148\u982d\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u672b\u5c3e\u306b\u623b\u308b\n        return (i + capacity()) % capacity();\n    }\n\n    /* \u5148\u982d\u30a8\u30f3\u30ad\u30e5\u30fc */\n    void pushFirst(int num) {\n        if (queSize == capacity()) {\n            cout << \"Double-ended queue is full\" << endl;\n            return;\n        }\n        // \u5148\u982d\u30dd\u30a4\u30f3\u30bf\u30921\u3064\u5de6\u306b\u79fb\u52d5\n        // \u5270\u4f59\u6f14\u7b97\u3067front\u304c\u914d\u5217\u306e\u5148\u982d\u3092\u8d8a\u3048\u3066\u672b\u5c3e\u306b\u623b\u308b\u3053\u3068\u3092\u5b9f\u73fe\n        front = index(front - 1);\n        // num\u3092\u5148\u982d\u306b\u8ffd\u52a0\n        nums[front] = num;\n        queSize++;\n    }\n\n    /* \u672b\u5c3e\u30a8\u30f3\u30ad\u30e5\u30fc */\n    void pushLast(int num) {\n        if (queSize == capacity()) {\n            cout << \"Double-ended queue is full\" << endl;\n            return;\n        }\n        // \u672b\u5c3e\u30dd\u30a4\u30f3\u30bf\u3092\u8a08\u7b97\u3001\u672b\u5c3e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 + 1\u3092\u6307\u3059\n        int rear = index(front + queSize);\n        // num\u3092\u672b\u5c3e\u306b\u8ffd\u52a0\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u5148\u982d\u30c7\u30ad\u30e5\u30fc */\n    int popFirst() {\n        int num = peekFirst();\n        // \u5148\u982d\u30dd\u30a4\u30f3\u30bf\u30921\u3064\u5f8c\u308d\u306b\u79fb\u52d5\n        front = index(front + 1);\n        queSize--;\n        return num;\n    }\n\n    /* \u672b\u5c3e\u30c7\u30ad\u30e5\u30fc */\n    int popLast() {\n        int num = peekLast();\n        queSize--;\n        return num;\n    }\n\n    /* \u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    int peekFirst() {\n        if (isEmpty())\n            throw out_of_range(\"Double-ended queue is empty\");\n        return nums[front];\n    }\n\n    /* \u672b\u5c3e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    int peekLast() {\n        if (isEmpty())\n            throw out_of_range(\"Double-ended queue is empty\");\n        // \u672b\u5c3e\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97\n        int last = index(front + queSize - 1);\n        return nums[last];\n    }\n\n    /* \u5370\u5237\u7528\u306b\u914d\u5217\u3092\u8fd4\u5374 */\n    vector<int> toVector() {\n        // \u6709\u52b9\u306a\u9577\u3055\u7bc4\u56f2\u5185\u306e\u8981\u7d20\u306e\u307f\u3092\u5909\u63db\n        vector<int> res(queSize);\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[index(j)];\n        }\n        return res;\n    }\n};\n
        array_deque.java
        /* \u5faa\u74b0\u914d\u5217\u306b\u57fa\u3065\u304f\u4e21\u7aef\u30ad\u30e5\u30fc\u30af\u30e9\u30b9 */\nclass ArrayDeque {\n    private int[] nums; // \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u8981\u7d20\u3092\u683c\u7d0d\u3059\u308b\u914d\u5217\n    private int front; // \u5148\u982d\u30dd\u30a4\u30f3\u30bf\u3001\u5148\u982d\u8981\u7d20\u3092\u6307\u3059\n    private int queSize; // \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\n\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    public ArrayDeque(int capacity) {\n        this.nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u5bb9\u91cf\u3092\u53d6\u5f97 */\n    public int capacity() {\n        return nums.length;\n    }\n\n    /* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    public int size() {\n        return queSize;\n    }\n\n    /* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    public boolean isEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u5faa\u74b0\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97 */\n    private int index(int i) {\n        // \u30e2\u30b8\u30e5\u30ed\u6f14\u7b97\u306b\u3088\u308a\u5faa\u74b0\u914d\u5217\u3092\u5b9f\u88c5\n        // i \u304c\u914d\u5217\u306e\u672b\u5c3e\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u5148\u982d\u306b\u623b\u308b\n        // i \u304c\u914d\u5217\u306e\u5148\u982d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u672b\u5c3e\u306b\u623b\u308b\n        return (i + capacity()) % capacity();\n    }\n\n    /* \u5148\u982d\u30a8\u30f3\u30ad\u30e5\u30fc */\n    public void pushFirst(int num) {\n        if (queSize == capacity()) {\n            System.out.println(\"\u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u6e80\u676f\u3067\u3059\");\n            return;\n        }\n        // \u5148\u982d\u30dd\u30a4\u30f3\u30bf\u3092\u5de6\u306b\u79fb\u52d5\u3057\u3001\u5883\u754c\u3092\u8d8a\u3048\u308b\u5834\u5408\u306f\u914d\u5217\u306e\u672b\u5c3e\u306b\u56de\u308b\n        front = index(front - 1);\n        // \u5148\u982d\u306b num \u3092\u8ffd\u52a0\n        nums[front] = num;\n        queSize++;\n    }\n\n    /* \u672b\u5c3e\u30a8\u30f3\u30ad\u30e5\u30fc */\n    public void pushLast(int num) {\n        if (queSize == capacity()) {\n            System.out.println(\"\u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u6e80\u676f\u3067\u3059\");\n            return;\n        }\n        // \u672b\u5c3e\u30dd\u30a4\u30f3\u30bf\u3092\u8a08\u7b97\u3057\u3001\u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0\n        int rear = index(front + queSize);\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u5148\u982d\u30c7\u30ad\u30e5\u30fc */\n    public int popFirst() {\n        int num = peekFirst();\n        // \u5148\u982d\u30dd\u30a4\u30f3\u30bf\u3092\u53f3\u306b\u79fb\u52d5\n        front = index(front + 1);\n        queSize--;\n        return num;\n    }\n\n    /* \u672b\u5c3e\u30c7\u30ad\u30e5\u30fc */\n    public int popLast() {\n        int num = peekLast();\n        queSize--;\n        return num;\n    }\n\n    /* \u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    public int peekFirst() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return nums[front];\n    }\n\n    /* \u672b\u5c3e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    public int peekLast() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        // \u672b\u5c3e\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97\n        int last = index(front + queSize - 1);\n        return nums[last];\n    }\n\n    /* \u914d\u5217\u3092\u8fd4\u3059 */\n    public int[] toArray() {\n        // front \u304b\u3089\u958b\u59cb\u3057\u3066 queSize \u500b\u306e\u8981\u7d20\u306e\u307f\u3092\u30b3\u30d4\u30fc\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[index(j)];\n        }\n        return res;\n    }\n}\n
        array_deque.cs
        [class]{ArrayDeque}-[func]{}\n
        array_deque.go
        [class]{arrayDeque}-[func]{}\n
        array_deque.swift
        [class]{ArrayDeque}-[func]{}\n
        array_deque.js
        [class]{ArrayDeque}-[func]{}\n
        array_deque.ts
        [class]{ArrayDeque}-[func]{}\n
        array_deque.dart
        [class]{ArrayDeque}-[func]{}\n
        array_deque.rs
        [class]{ArrayDeque}-[func]{}\n
        array_deque.c
        [class]{ArrayDeque}-[func]{}\n
        array_deque.kt
        [class]{ArrayDeque}-[func]{}\n
        array_deque.rb
        [class]{ArrayDeque}-[func]{}\n
        "},{"location":"chapter_stack_and_queue/deque/#533","title":"5.3.3 \u00a0 \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u5fdc\u7528","text":"

        \u4e21\u7aef\u30ad\u30e5\u30fc\u306f\u30b9\u30bf\u30c3\u30af\u3068\u30ad\u30e5\u30fc\u306e\u4e21\u65b9\u306e\u30ed\u30b8\u30c3\u30af\u3092\u7d44\u307f\u5408\u308f\u305b\u3066\u3044\u308b\u305f\u3081\u3001\u305d\u308c\u305e\u308c\u306e\u3059\u3079\u3066\u306e\u30e6\u30fc\u30b9\u30b1\u30fc\u30b9\u3092\u5b9f\u88c5\u3067\u304d\u3001\u3088\u308a\u5927\u304d\u306a\u67d4\u8edf\u6027\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002

        \u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u306e\u300c\u5143\u306b\u623b\u3059\u300d\u6a5f\u80fd\u306f\u901a\u5e38\u30b9\u30bf\u30c3\u30af\u3092\u4f7f\u3063\u3066\u5b9f\u88c5\u3055\u308c\u308b\u3053\u3068\u3092\u77e5\u3063\u3066\u3044\u307e\u3059\uff1a\u30b7\u30b9\u30c6\u30e0\u306f\u5404\u5909\u66f4\u64cd\u4f5c\u3092\u30b9\u30bf\u30c3\u30af\u306bpush\u3057\u3001\u6b21\u306bpop\u3057\u3066\u5143\u306b\u623b\u3059\u3053\u3068\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u30b7\u30b9\u30c6\u30e0\u30ea\u30bd\u30fc\u30b9\u306e\u5236\u9650\u3092\u8003\u616e\u3057\u3066\u3001\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u306f\u5143\u306b\u623b\u3059\u30b9\u30c6\u30c3\u30d7\u306e\u6570\u3092\u5236\u9650\u3059\u308b\u3053\u3068\u304c\u3088\u304f\u3042\u308a\u307e\u3059\uff08\u4f8b\u3048\u3070\u3001\u6700\u5f8c\u306e50\u30b9\u30c6\u30c3\u30d7\u306e\u307f\u3092\u8a31\u53ef\uff09\u3002\u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u304c50\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u306f\u30b9\u30bf\u30c3\u30af\u306e\u5e95\u90e8\uff08\u30ad\u30e5\u30fc\u306e\u524d\u7aef\uff09\u3067\u524a\u9664\u64cd\u4f5c\u3092\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u901a\u5e38\u306e\u30b9\u30bf\u30c3\u30af\u3067\u306f\u3053\u306e\u6a5f\u80fd\u3092\u5b9f\u884c\u3067\u304d\u306a\u3044\u305f\u3081\u3001\u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059\u3002\u300c\u5143\u306b\u623b\u3059\u300d\u306e\u30b3\u30a2\u30ed\u30b8\u30c3\u30af\u306f\u4f9d\u7136\u3068\u3057\u3066\u30b9\u30bf\u30c3\u30af\u306e\u5f8c\u5165\u308c\u5148\u51fa\u3057\u539f\u5247\u306b\u5f93\u3044\u307e\u3059\u304c\u3001\u4e21\u7aef\u30ad\u30e5\u30fc\u306f\u3088\u308a\u67d4\u8edf\u306b\u3044\u304f\u3064\u304b\u306e\u8ffd\u52a0\u30ed\u30b8\u30c3\u30af\u3092\u5b9f\u88c5\u3067\u304d\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        "},{"location":"chapter_stack_and_queue/queue/","title":"5.2 \u00a0 \u30ad\u30e5\u30fc","text":"

        \u30ad\u30e5\u30fc\u306f\u3001\u5148\u5165\u5148\u51fa\uff08FIFO\uff09\u30eb\u30fc\u30eb\u306b\u5f93\u3046\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3059\u3002\u540d\u524d\u304c\u793a\u3059\u3088\u3046\u306b\u3001\u30ad\u30e5\u30fc\u306f\u884c\u5217\u306e\u73fe\u8c61\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\u3057\u3001\u65b0\u53c2\u8005\u306f\u5217\u306e\u5f8c\u308d\u306b\u4e26\u3073\u3001\u524d\u306e\u4eba\u304c\u6700\u521d\u306b\u5217\u3092\u96e2\u308c\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30ad\u30e5\u30fc\u306e\u524d\u9762\u3092\u300c\u30d8\u30c3\u30c9\u300d\u3001\u5f8c\u9762\u3092\u300c\u30c6\u30fc\u30eb\u300d\u3068\u547c\u3073\u307e\u3059\u3002\u30ad\u30e5\u30fc\u306e\u5f8c\u308d\u306b\u8981\u7d20\u3092\u8ffd\u52a0\u3059\u308b\u64cd\u4f5c\u3092\u300c\u30a8\u30f3\u30ad\u30e5\u30fc\u300d\u3001\u524d\u304b\u3089\u8981\u7d20\u3092\u524a\u9664\u3059\u308b\u64cd\u4f5c\u3092\u300c\u30c7\u30ad\u30e5\u30fc\u300d\u3068\u547c\u3073\u307e\u3059\u3002

        \u56f3 5-4 \u00a0 \u30ad\u30e5\u30fc\u306e\u5148\u5165\u5148\u51fa\u30eb\u30fc\u30eb

        "},{"location":"chapter_stack_and_queue/queue/#521","title":"5.2.1 \u00a0 \u30ad\u30e5\u30fc\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c","text":"

        \u30ad\u30e5\u30fc\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c\u3092\u4e0b\u8868\u306b\u793a\u3057\u307e\u3059\u3002\u30e1\u30bd\u30c3\u30c9\u540d\u306f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u3088\u3063\u3066\u7570\u306a\u308b\u5834\u5408\u304c\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3053\u3053\u3067\u306f\u3001\u30b9\u30bf\u30c3\u30af\u3067\u4f7f\u7528\u3057\u305f\u306e\u3068\u540c\u3058\u547d\u540d\u898f\u5247\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        \u8868 5-2 \u00a0 \u30ad\u30e5\u30fc\u64cd\u4f5c\u306e\u52b9\u7387

        \u30e1\u30bd\u30c3\u30c9\u540d \u8aac\u660e \u6642\u9593\u8a08\u7b97\u91cf push() \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\u3001\u30c6\u30fc\u30eb\u306b\u8ffd\u52a0 \\(O(1)\\) pop() \u30d8\u30c3\u30c9\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc \\(O(1)\\) peek() \u30d8\u30c3\u30c9\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 \\(O(1)\\)

        \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u7528\u610f\u3055\u308c\u3066\u3044\u308b\u30ad\u30e5\u30fc\u30af\u30e9\u30b9\u3092\u76f4\u63a5\u4f7f\u7528\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin queue.py
        from collections import deque\n\n# \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316\n# Python\u3067\u306f\u3001\u4e00\u822c\u7684\u306bdeque\u30af\u30e9\u30b9\u3092\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\u3057\u307e\u3059\n# queue.Queue()\u306f\u7d14\u7c8b\u306a\u30ad\u30e5\u30fc\u30af\u30e9\u30b9\u3067\u3059\u304c\u3001\u4f7f\u3044\u306b\u304f\u3044\u305f\u3081\u63a8\u5968\u3055\u308c\u307e\u305b\u3093\nque: deque[int] = deque()\n\n# \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\nque.append(1)\nque.append(3)\nque.append(2)\nque.append(5)\nque.append(4)\n\n# \u6700\u521d\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\nfront: int = que[0]\n\n# \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\npop: int = que.popleft()\n\n# \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97\nsize: int = len(que)\n\n# \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af\nis_empty: bool = len(que) == 0\n
        queue.cpp
        /* \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\nqueue<int> queue;\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u6700\u521d\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint front = queue.front();\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\nqueue.pop();\n\n/* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = queue.size();\n\n/* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nbool empty = queue.empty();\n
        queue.java
        /* \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\nQueue<Integer> queue = new LinkedList<>();\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\nqueue.offer(1);\nqueue.offer(3);\nqueue.offer(2);\nqueue.offer(5);\nqueue.offer(4);\n\n/* \u6700\u521d\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint peek = queue.peek();\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\nint pop = queue.poll();\n\n/* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = queue.size();\n\n/* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nboolean isEmpty = queue.isEmpty();\n
        queue.cs
        /* \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\nQueue<int> queue = new();\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\nqueue.Enqueue(1);\nqueue.Enqueue(3);\nqueue.Enqueue(2);\nqueue.Enqueue(5);\nqueue.Enqueue(4);\n\n/* \u6700\u521d\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint peek = queue.Peek();\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\nint pop = queue.Dequeue();\n\n/* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = queue.Count;\n\n/* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nbool isEmpty = queue.Count == 0;\n
        queue_test.go
        /* \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// Go\u3067\u306f\u3001list\u3092\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\nqueue := list.New()\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\nqueue.PushBack(1)\nqueue.PushBack(3)\nqueue.PushBack(2)\nqueue.PushBack(5)\nqueue.PushBack(4)\n\n/* \u6700\u521d\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\npeek := queue.Front()\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\npop := queue.Front()\nqueue.Remove(pop)\n\n/* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nsize := queue.Len()\n\n/* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nisEmpty := queue.Len() == 0\n
        queue.swift
        /* \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// Swift\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30ad\u30e5\u30fc\u30af\u30e9\u30b9\u304c\u306a\u3044\u305f\u3081\u3001Array\u3092\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\nvar queue: [Int] = []\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\nqueue.append(1)\nqueue.append(3)\nqueue.append(2)\nqueue.append(5)\nqueue.append(4)\n\n/* \u6700\u521d\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nlet peek = queue.first!\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\n// \u914d\u5217\u306a\u306e\u3067\u3001removeFirst\u306e\u8a08\u7b97\u91cf\u306fO(n)\nlet pool = queue.removeFirst()\n\n/* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nlet size = queue.count\n\n/* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nlet isEmpty = queue.isEmpty\n
        queue.js
        /* \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// JavaScript\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30ad\u30e5\u30fc\u304c\u306a\u3044\u305f\u3081\u3001Array\u3092\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\nconst queue = [];\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u6700\u521d\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nconst peek = queue[0];\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\n// \u57fa\u790e\u69cb\u9020\u304c\u914d\u5217\u306a\u306e\u3067\u3001shift()\u30e1\u30bd\u30c3\u30c9\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306fO(n)\nconst pop = queue.shift();\n\n/* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nconst size = queue.length;\n\n/* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nconst empty = queue.length === 0;\n
        queue.ts
        /* \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// TypeScript\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30ad\u30e5\u30fc\u304c\u306a\u3044\u305f\u3081\u3001Array\u3092\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\nconst queue: number[] = [];\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u6700\u521d\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nconst peek = queue[0];\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\n// \u57fa\u790e\u69cb\u9020\u304c\u914d\u5217\u306a\u306e\u3067\u3001shift()\u30e1\u30bd\u30c3\u30c9\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306fO(n)\nconst pop = queue.shift();\n\n/* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nconst size = queue.length;\n\n/* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nconst empty = queue.length === 0;\n
        queue.dart
        /* \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// Dart\u306eQueue\u30af\u30e9\u30b9\u306f\u53cc\u65b9\u5411\u30ad\u30e5\u30fc\u3067\u3059\u304c\u3001\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\u3067\u304d\u307e\u3059\nQueue<int> queue = Queue();\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\nqueue.add(1);\nqueue.add(3);\nqueue.add(2);\nqueue.add(5);\nqueue.add(4);\n\n/* \u6700\u521d\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint peek = queue.first;\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\nint pop = queue.removeFirst();\n\n/* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = queue.length;\n\n/* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nbool isEmpty = queue.isEmpty;\n
        queue.rs
        /* \u53cc\u65b9\u5411\u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// Rust\u3067\u306f\u3001\u53cc\u65b9\u5411\u30ad\u30e5\u30fc\u3092\u901a\u5e38\u306e\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\nlet mut deque: VecDeque<u32> = VecDeque::new();\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\ndeque.push_back(1);\ndeque.push_back(3);\ndeque.push_back(2);\ndeque.push_back(5);\ndeque.push_back(4);\n\n/* \u6700\u521d\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nif let Some(front) = deque.front() {\n}\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\nif let Some(pop) = deque.pop_front() {\n}\n\n/* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nlet size = deque.len();\n\n/* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nlet is_empty = deque.is_empty();\n
        queue.c
        // C\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30ad\u30e5\u30fc\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        queue.kt
        \n
        "},{"location":"chapter_stack_and_queue/queue/#522","title":"5.2.2 \u00a0 \u30ad\u30e5\u30fc\u306e\u5b9f\u88c5","text":"

        \u30ad\u30e5\u30fc\u3092\u5b9f\u88c5\u3059\u308b\u306b\u306f\u3001\u4e00\u65b9\u306e\u7aef\u3067\u8981\u7d20\u3092\u8ffd\u52a0\u3057\u3001\u3082\u3046\u4e00\u65b9\u306e\u7aef\u3067\u8981\u7d20\u3092\u524a\u9664\u3067\u304d\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u304c\u5fc5\u8981\u3067\u3059\u3002\u9023\u7d50\u30ea\u30b9\u30c8\u3068\u914d\u5217\u306e\u4e21\u65b9\u304c\u3053\u306e\u8981\u4ef6\u3092\u6e80\u305f\u3057\u307e\u3059\u3002

        "},{"location":"chapter_stack_and_queue/queue/#1","title":"1. \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u30d9\u30fc\u30b9\u306e\u5b9f\u88c5","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u300c\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u300d\u3068\u300c\u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u300d\u3092\u305d\u308c\u305e\u308c\u30ad\u30e5\u30fc\u306e\u300c\u30d5\u30ed\u30f3\u30c8\u300d\u3068\u300c\u30ea\u30a2\u300d\u3068\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u30ce\u30fc\u30c9\u306f\u5f8c\u308d\u3067\u306e\u307f\u8ffd\u52a0\u3067\u304d\u3001\u524d\u3067\u306e\u307f\u524a\u9664\u3067\u304d\u308b\u3088\u3046\u306b\u898f\u5b9a\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        LinkedListQueuepush()pop()

        \u56f3 5-5 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u306b\u3088\u308b\u30ad\u30e5\u30fc\u5b9f\u88c5\u306e\u30a8\u30f3\u30ad\u30e5\u30fc\u3068\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c

        \u4ee5\u4e0b\u306f\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u30ad\u30e5\u30fc\u3092\u5b9f\u88c5\u3059\u308b\u30b3\u30fc\u30c9\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linkedlist_queue.py
        class LinkedListQueue:\n    \"\"\"\u9023\u7d50\u30ea\u30b9\u30c8\u30d9\u30fc\u30b9\u306e\u30ad\u30e5\u30fc\u30af\u30e9\u30b9\"\"\"\n\n    def __init__(self):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        self._front: ListNode | None = None  # \u30d8\u30c3\u30c9\u30ce\u30fc\u30c9 front\n        self._rear: ListNode | None = None  # \u30c6\u30fc\u30eb\u30ce\u30fc\u30c9 rear\n        self._size: int = 0\n\n    def size(self) -> int:\n        \"\"\"\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\"\"\"\n        return self._size == 0\n\n    def push(self, num: int):\n        \"\"\"\u30a8\u30f3\u30ad\u30e5\u30fc\"\"\"\n        # \u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u306e\u5f8c\u308d\u306b num \u3092\u8ffd\u52a0\n        node = ListNode(num)\n        # \u30ad\u30e5\u30fc\u304c\u7a7a\u306e\u5834\u5408\u3001\u30d8\u30c3\u30c9\u3068\u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u306e\u4e21\u65b9\u3092\u305d\u306e\u30ce\u30fc\u30c9\u306b\u5411\u3051\u308b\n        if self._front is None:\n            self._front = node\n            self._rear = node\n        # \u30ad\u30e5\u30fc\u304c\u7a7a\u3067\u306a\u3044\u5834\u5408\u3001\u305d\u306e\u30ce\u30fc\u30c9\u3092\u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u306e\u5f8c\u308d\u306b\u8ffd\u52a0\n        else:\n            self._rear.next = node\n            self._rear = node\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"\u30c7\u30ad\u30e5\u30fc\"\"\"\n        num = self.peek()\n        # \u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u3092\u524a\u9664\n        self._front = self._front.next\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"\u30d5\u30ed\u30f3\u30c8\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Queue is empty\")\n        return self._front.val\n\n    def to_list(self) -> list[int]:\n        \"\"\"\u51fa\u529b\u7528\u306e\u30ea\u30b9\u30c8\u306b\u5909\u63db\"\"\"\n        queue = []\n        temp = self._front\n        while temp:\n            queue.append(temp.val)\n            temp = temp.next\n        return queue\n
        linkedlist_queue.cpp
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u30ad\u30e5\u30fc\u30af\u30e9\u30b9 */\nclass LinkedListQueue {\n  private:\n    ListNode *front, *rear; // \u5148\u982d\u30ce\u30fc\u30c9front\u3001\u672b\u5c3e\u30ce\u30fc\u30c9rear\n    int queSize;\n\n  public:\n    LinkedListQueue() {\n        front = nullptr;\n        rear = nullptr;\n        queSize = 0;\n    }\n\n    ~LinkedListQueue() {\n        // \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8d70\u67fb\u3001\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3001\u30e1\u30e2\u30ea\u3092\u89e3\u653e\n        freeMemoryLinkedList(front);\n    }\n\n    /* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    int size() {\n        return queSize;\n    }\n\n    /* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    bool isEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u30a8\u30f3\u30ad\u30e5\u30fc */\n    void push(int num) {\n        // \u672b\u5c3e\u30ce\u30fc\u30c9\u306e\u5f8c\u308d\u306bnum\u3092\u8ffd\u52a0\n        ListNode *node = new ListNode(num);\n        // \u30ad\u30e5\u30fc\u304c\u7a7a\u306e\u5834\u5408\u3001\u5148\u982d\u3068\u672b\u5c3e\u30ce\u30fc\u30c9\u306e\u4e21\u65b9\u3092\u305d\u306e\u30ce\u30fc\u30c9\u306b\u5411\u3051\u308b\n        if (front == nullptr) {\n            front = node;\n            rear = node;\n        }\n        // \u30ad\u30e5\u30fc\u304c\u7a7a\u3067\u306a\u3044\u5834\u5408\u3001\u305d\u306e\u30ce\u30fc\u30c9\u3092\u672b\u5c3e\u30ce\u30fc\u30c9\u306e\u5f8c\u308d\u306b\u8ffd\u52a0\n        else {\n            rear->next = node;\n            rear = node;\n        }\n        queSize++;\n    }\n\n    /* \u30c7\u30ad\u30e5\u30fc */\n    int pop() {\n        int num = peek();\n        // \u5148\u982d\u30ce\u30fc\u30c9\u3092\u524a\u9664\n        ListNode *tmp = front;\n        front = front->next;\n        // \u30e1\u30e2\u30ea\u3092\u89e3\u653e\n        delete tmp;\n        queSize--;\n        return num;\n    }\n\n    /* \u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    int peek() {\n        if (size() == 0)\n            throw out_of_range(\"Queue is empty\");\n        return front->val;\n    }\n\n    /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092Vector\u306b\u5909\u63db\u3057\u3066\u8fd4\u5374 */\n    vector<int> toVector() {\n        ListNode *node = front;\n        vector<int> res(size());\n        for (int i = 0; i < res.size(); i++) {\n            res[i] = node->val;\n            node = node->next;\n        }\n        return res;\n    }\n};\n
        linkedlist_queue.java
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u30ad\u30e5\u30fc\u30af\u30e9\u30b9 */\nclass LinkedListQueue {\n    private ListNode front, rear; // \u5148\u982d\u30ce\u30fc\u30c9 front\u3001\u672b\u5c3e\u30ce\u30fc\u30c9 rear\n    private int queSize = 0;\n\n    public LinkedListQueue() {\n        front = null;\n        rear = null;\n    }\n\n    /* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    public int size() {\n        return queSize;\n    }\n\n    /* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u30a8\u30f3\u30ad\u30e5\u30fc */\n    public void push(int num) {\n        // \u672b\u5c3e\u30ce\u30fc\u30c9\u306e\u5f8c\u308d\u306b num \u3092\u8ffd\u52a0\n        ListNode node = new ListNode(num);\n        // \u30ad\u30e5\u30fc\u304c\u7a7a\u306e\u5834\u5408\u3001\u5148\u982d\u3068\u672b\u5c3e\u30ce\u30fc\u30c9\u306e\u4e21\u65b9\u3092\u305d\u306e\u30ce\u30fc\u30c9\u306b\u30dd\u30a4\u30f3\u30c8\n        if (front == null) {\n            front = node;\n            rear = node;\n        // \u30ad\u30e5\u30fc\u304c\u7a7a\u3067\u306a\u3044\u5834\u5408\u3001\u305d\u306e\u30ce\u30fc\u30c9\u3092\u672b\u5c3e\u30ce\u30fc\u30c9\u306e\u5f8c\u308d\u306b\u8ffd\u52a0\n        } else {\n            rear.next = node;\n            rear = node;\n        }\n        queSize++;\n    }\n\n    /* \u30c7\u30ad\u30e5\u30fc */\n    public int pop() {\n        int num = peek();\n        // \u5148\u982d\u30ce\u30fc\u30c9\u3092\u524a\u9664\n        front = front.next;\n        queSize--;\n        return num;\n    }\n\n    /* \u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return front.val;\n    }\n\n    /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u914d\u5217\u306b\u5909\u63db\u3057\u3066\u8fd4\u3059 */\n    public int[] toArray() {\n        ListNode node = front;\n        int[] res = new int[size()];\n        for (int i = 0; i < res.length; i++) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_queue.cs
        [class]{LinkedListQueue}-[func]{}\n
        linkedlist_queue.go
        [class]{linkedListQueue}-[func]{}\n
        linkedlist_queue.swift
        [class]{LinkedListQueue}-[func]{}\n
        linkedlist_queue.js
        [class]{LinkedListQueue}-[func]{}\n
        linkedlist_queue.ts
        [class]{LinkedListQueue}-[func]{}\n
        linkedlist_queue.dart
        [class]{LinkedListQueue}-[func]{}\n
        linkedlist_queue.rs
        [class]{LinkedListQueue}-[func]{}\n
        linkedlist_queue.c
        [class]{LinkedListQueue}-[func]{}\n
        linkedlist_queue.kt
        [class]{LinkedListQueue}-[func]{}\n
        linkedlist_queue.rb
        [class]{LinkedListQueue}-[func]{}\n
        "},{"location":"chapter_stack_and_queue/queue/#2","title":"2. \u00a0 \u914d\u5217\u30d9\u30fc\u30b9\u306e\u5b9f\u88c5","text":"

        \u914d\u5217\u306e\u6700\u521d\u306e\u8981\u7d20\u3092\u524a\u9664\u3059\u308b\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3001\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\u304c\u975e\u52b9\u7387\u306b\u306a\u308a\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u3053\u306e\u554f\u984c\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5de7\u5999\u306b\u56de\u907f\u3067\u304d\u307e\u3059\u3002

        \u5909\u6570front\u3092\u4f7f\u7528\u3057\u3066\u30d5\u30ed\u30f3\u30c8\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u793a\u3057\u3001\u5909\u6570size\u3092\u7dad\u6301\u3057\u3066\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u8a18\u9332\u3057\u307e\u3059\u3002rear = front + size\u3092\u5b9a\u7fa9\u3057\u3001\u3053\u308c\u306f\u30c6\u30fc\u30eb\u8981\u7d20\u306e\u76f4\u5f8c\u306e\u4f4d\u7f6e\u3092\u6307\u3057\u307e\u3059\u3002

        \u3053\u306e\u8a2d\u8a08\u306b\u3088\u308a\u3001\u914d\u5217\u5185\u306e\u8981\u7d20\u306e\u6709\u52b9\u306a\u9593\u9694\u306f[front, rear - 1]\u3067\u3059\u3002\u5404\u64cd\u4f5c\u306e\u5b9f\u88c5\u65b9\u6cd5\u3092\u4e0b\u56f3\u306b\u793a\u3057\u307e\u3059\u3002

        • \u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c\uff1a\u5165\u529b\u8981\u7d20\u3092rear\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5272\u308a\u5f53\u3066\u3001size\u30921\u5897\u52a0\u3055\u305b\u307e\u3059\u3002
        • \u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\uff1a\u5358\u306bfront\u30921\u5897\u52a0\u3055\u305b\u3001size\u30921\u6e1b\u5c11\u3055\u305b\u307e\u3059\u3002

        \u30a8\u30f3\u30ad\u30e5\u30fc\u3068\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\u306f\u4e21\u65b9\u3068\u3082\u5358\u4e00\u306e\u64cd\u4f5c\u306e\u307f\u3092\u5fc5\u8981\u3068\u3057\u3001\u305d\u308c\u305e\u308c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u3067\u3059\u3002

        ArrayQueuepush()pop()

        \u56f3 5-6 \u00a0 \u914d\u5217\u306b\u3088\u308b\u30ad\u30e5\u30fc\u5b9f\u88c5\u306e\u30a8\u30f3\u30ad\u30e5\u30fc\u3068\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c

        \u554f\u984c\u306b\u6c17\u3065\u304f\u304b\u3082\u3057\u308c\u307e\u305b\u3093\uff1a\u30a8\u30f3\u30ad\u30e5\u30fc\u3068\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\u304c\u7d99\u7d9a\u7684\u306b\u5b9f\u884c\u3055\u308c\u308b\u3068\u3001front\u3068rear\u306e\u4e21\u65b9\u304c\u53f3\u306b\u79fb\u52d5\u3057\u3001\u6700\u7d42\u7684\u306b\u914d\u5217\u306e\u672b\u5c3e\u306b\u5230\u9054\u3057\u3066\u305d\u308c\u4ee5\u4e0a\u79fb\u52d5\u3067\u304d\u306a\u304f\u306a\u308a\u307e\u3059\u3002\u3053\u308c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3001\u914d\u5217\u3092\u300c\u5faa\u74b0\u914d\u5217\u300d\u3068\u3057\u3066\u6271\u3044\u3001\u914d\u5217\u306e\u672b\u5c3e\u3092\u5148\u982d\u306b\u63a5\u7d9a\u3057\u307e\u3059\u3002

        \u5faa\u74b0\u914d\u5217\u3067\u306f\u3001front\u307e\u305f\u306frear\u304c\u672b\u5c3e\u306b\u5230\u9054\u3059\u308b\u3068\u3001\u914d\u5217\u306e\u5148\u982d\u306b\u30eb\u30fc\u30d7\u30d0\u30c3\u30af\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u5faa\u74b0\u30d1\u30bf\u30fc\u30f3\u306f\u3001\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u306b\u793a\u3059\u3088\u3046\u306b\u300c\u5270\u4f59\u6f14\u7b97\u300d\u3067\u5b9f\u73fe\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_queue.py
        class ArrayQueue:\n    \"\"\"\u5faa\u74b0\u914d\u5217\u30d9\u30fc\u30b9\u306e\u30ad\u30e5\u30fc\u30af\u30e9\u30b9\"\"\"\n\n    def __init__(self, size: int):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        self._nums: list[int] = [0] * size  # \u30ad\u30e5\u30fc\u8981\u7d20\u3092\u683c\u7d0d\u3059\u308b\u914d\u5217\n        self._front: int = 0  # \u30d5\u30ed\u30f3\u30c8\u30dd\u30a4\u30f3\u30bf\u3001\u30d5\u30ed\u30f3\u30c8\u8981\u7d20\u3092\u6307\u3059\n        self._size: int = 0  # \u30ad\u30e5\u30fc\u306e\u9577\u3055\n\n    def capacity(self) -> int:\n        \"\"\"\u30ad\u30e5\u30fc\u306e\u5bb9\u91cf\u3092\u53d6\u5f97\"\"\"\n        return len(self._nums)\n\n    def size(self) -> int:\n        \"\"\"\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\"\"\"\n        return self._size == 0\n\n    def push(self, num: int):\n        \"\"\"\u30a8\u30f3\u30ad\u30e5\u30fc\"\"\"\n        if self._size == self.capacity():\n            raise IndexError(\"Queue is full\")\n        # \u30ea\u30a2\u30dd\u30a4\u30f3\u30bf\u3092\u8a08\u7b97\u3001\u30ea\u30a2\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 + 1 \u3092\u6307\u3059\n        # \u30e2\u30b8\u30e5\u30ed\u6f14\u7b97\u3092\u4f7f\u7528\u3057\u3066\u30ea\u30a2\u30dd\u30a4\u30f3\u30bf\u3092\u914d\u5217\u306e\u672b\u5c3e\u304b\u3089\u5148\u982d\u306b\u623b\u3059\n        rear: int = (self._front + self._size) % self.capacity()\n        # num \u3092\u30ea\u30a2\u306b\u8ffd\u52a0\n        self._nums[rear] = num\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"\u30c7\u30ad\u30e5\u30fc\"\"\"\n        num: int = self.peek()\n        # \u30d5\u30ed\u30f3\u30c8\u30dd\u30a4\u30f3\u30bf\u30921\u3064\u5f8c\u308d\u306b\u79fb\u52d5\u3001\u672b\u5c3e\u3092\u8d85\u3048\u305f\u5834\u5408\u306f\u914d\u5217\u306e\u5148\u982d\u306b\u623b\u308b\n        self._front = (self._front + 1) % self.capacity()\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"\u30d5\u30ed\u30f3\u30c8\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Queue is empty\")\n        return self._nums[self._front]\n\n    def to_list(self) -> list[int]:\n        \"\"\"\u51fa\u529b\u7528\u306e\u914d\u5217\u3092\u8fd4\u3059\"\"\"\n        res = [0] * self.size()\n        j: int = self._front\n        for i in range(self.size()):\n            res[i] = self._nums[(j % self.capacity())]\n            j += 1\n        return res\n
        array_queue.cpp
        /* \u5faa\u74b0\u914d\u5217\u306b\u57fa\u3065\u304f\u30ad\u30e5\u30fc\u30af\u30e9\u30b9 */\nclass ArrayQueue {\n  private:\n    int *nums;       // \u30ad\u30e5\u30fc\u8981\u7d20\u3092\u683c\u7d0d\u3059\u308b\u914d\u5217\n    int front;       // \u5148\u982d\u30dd\u30a4\u30f3\u30bf\u3001\u5148\u982d\u8981\u7d20\u3092\u6307\u3059\n    int queSize;     // \u30ad\u30e5\u30fc\u306e\u9577\u3055\n    int queCapacity; // \u30ad\u30e5\u30fc\u306e\u5bb9\u91cf\n\n  public:\n    ArrayQueue(int capacity) {\n        // \u914d\u5217\u3092\u521d\u671f\u5316\n        nums = new int[capacity];\n        queCapacity = capacity;\n        front = queSize = 0;\n    }\n\n    ~ArrayQueue() {\n        delete[] nums;\n    }\n\n    /* \u30ad\u30e5\u30fc\u306e\u5bb9\u91cf\u3092\u53d6\u5f97 */\n    int capacity() {\n        return queCapacity;\n    }\n\n    /* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    int size() {\n        return queSize;\n    }\n\n    /* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    bool isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u30a8\u30f3\u30ad\u30e5\u30fc */\n    void push(int num) {\n        if (queSize == queCapacity) {\n            cout << \"Queue is full\" << endl;\n            return;\n        }\n        // \u672b\u5c3e\u30dd\u30a4\u30f3\u30bf\u3092\u8a08\u7b97\u3001\u672b\u5c3e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 + 1\u3092\u6307\u3059\n        // \u5270\u4f59\u6f14\u7b97\u3092\u4f7f\u7528\u3057\u3066\u672b\u5c3e\u30dd\u30a4\u30f3\u30bf\u304c\u914d\u5217\u306e\u672b\u5c3e\u304b\u3089\u5148\u982d\u306b\u623b\u308b\u3088\u3046\u306b\u30e9\u30c3\u30d7\n        int rear = (front + queSize) % queCapacity;\n        // num\u3092\u672b\u5c3e\u306b\u8ffd\u52a0\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u30c7\u30ad\u30e5\u30fc */\n    int pop() {\n        int num = peek();\n        // \u5148\u982d\u30dd\u30a4\u30f3\u30bf\u30921\u3064\u5f8c\u308d\u306b\u79fb\u52d5\u3001\u672b\u5c3e\u3092\u8d85\u3048\u305f\u5834\u5408\u306f\u914d\u5217\u306e\u5148\u982d\u306b\u623b\u308b\n        front = (front + 1) % queCapacity;\n        queSize--;\n        return num;\n    }\n\n    /* \u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    int peek() {\n        if (isEmpty())\n            throw out_of_range(\"Queue is empty\");\n        return nums[front];\n    }\n\n    /* \u914d\u5217\u3092Vector\u306b\u5909\u63db\u3057\u3066\u8fd4\u5374 */\n    vector<int> toVector() {\n        // \u6709\u52b9\u306a\u9577\u3055\u7bc4\u56f2\u5185\u306e\u8981\u7d20\u306e\u307f\u3092\u5909\u63db\n        vector<int> arr(queSize);\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            arr[i] = nums[j % queCapacity];\n        }\n        return arr;\n    }\n};\n
        array_queue.java
        /* \u914d\u5217\u306b\u57fa\u3065\u304f\u30ad\u30e5\u30fc\u30af\u30e9\u30b9 */\nclass ArrayQueue {\n    private int[] nums; // \u8981\u7d20\u3092\u683c\u7d0d\u3059\u308b\u914d\u5217\n    private int front; // \u30ad\u30e5\u30fc\u30d8\u30c3\u30c9\u30dd\u30a4\u30f3\u30bf\u3001\u6700\u521d\u306e\u8981\u7d20\u3092\u6307\u3059\n    private int queSize; // \u30ad\u30e5\u30fc\u306e\u9577\u3055\n\n    public ArrayQueue(int capacity) {\n        nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* \u30ad\u30e5\u30fc\u306e\u5bb9\u91cf\u3092\u53d6\u5f97 */\n    public int capacity() {\n        return nums.length;\n    }\n\n    /* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    public int size() {\n        return queSize;\n    }\n\n    /* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    public boolean isEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u30a8\u30f3\u30ad\u30e5\u30fc */\n    public void push(int num) {\n        if (queSize == capacity()) {\n            System.out.println(\"\u30ad\u30e5\u30fc\u304c\u6e80\u676f\u3067\u3059\");\n            return;\n        }\n        // \u30ea\u30a2\u30dd\u30a4\u30f3\u30bf\u3092\u8a08\u7b97\uff1afront + queSize\n        // \u30e2\u30b8\u30e5\u30ed\u64cd\u4f5c\u306b\u3088\u308a rear \u304c\u914d\u5217\u306e\u9577\u3055\u3092\u8d85\u3048\u308b\u3053\u3068\u3092\u56de\u907f\n        int rear = (front + queSize) % capacity();\n        // \u8981\u7d20\u3092\u30ad\u30e5\u30fc\u30ea\u30a2\u306b\u8ffd\u52a0\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u30c7\u30ad\u30e5\u30fc */\n    public int pop() {\n        int num = peek();\n        // \u30ad\u30e5\u30fc\u30d8\u30c3\u30c9\u30dd\u30a4\u30f3\u30bf\u3092\u5f8c\u308d\u306b1\u3064\u79fb\u52d5\u3001\u30e2\u30b8\u30e5\u30ed\u64cd\u4f5c\u306b\u3088\u308a\u7bc4\u56f2\u3092\u8d85\u3048\u308b\u3053\u3068\u3092\u56de\u907f\n        front = (front + 1) % capacity();\n        queSize--;\n        return num;\n    }\n\n    /* \u30ad\u30e5\u30fc\u30d8\u30c3\u30c9\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return nums[front];\n    }\n\n    /* \u914d\u5217\u3092\u8fd4\u3059 */\n    public int[] toArray() {\n        // front \u304b\u3089\u958b\u59cb\u3057\u3066 queSize \u500b\u306e\u8981\u7d20\u306e\u307f\u3092\u30b3\u30d4\u30fc\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[j % capacity()];\n        }\n        return res;\n    }\n}\n
        array_queue.cs
        [class]{ArrayQueue}-[func]{}\n
        array_queue.go
        [class]{arrayQueue}-[func]{}\n
        array_queue.swift
        [class]{ArrayQueue}-[func]{}\n
        array_queue.js
        [class]{ArrayQueue}-[func]{}\n
        array_queue.ts
        [class]{ArrayQueue}-[func]{}\n
        array_queue.dart
        [class]{ArrayQueue}-[func]{}\n
        array_queue.rs
        [class]{ArrayQueue}-[func]{}\n
        array_queue.c
        [class]{ArrayQueue}-[func]{}\n
        array_queue.kt
        [class]{ArrayQueue}-[func]{}\n
        array_queue.rb
        [class]{ArrayQueue}-[func]{}\n

        \u4e0a\u8a18\u306e\u30ad\u30e5\u30fc\u5b9f\u88c5\u306b\u306f\u307e\u3060\u5236\u9650\u304c\u3042\u308a\u307e\u3059\uff1a\u9577\u3055\u304c\u56fa\u5b9a\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u3053\u306e\u554f\u984c\u306f\u89e3\u6c7a\u304c\u56f0\u96e3\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u914d\u5217\u3092\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u81ea\u52d5\u62e1\u5f35\u3067\u304d\u308b\u52d5\u7684\u914d\u5217\u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u8208\u5473\u306e\u3042\u308b\u8aad\u8005\u306f\u81ea\u5206\u3067\u5b9f\u88c5\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002

        2\u3064\u306e\u5b9f\u88c5\u306e\u6bd4\u8f03\u306f\u30b9\u30bf\u30c3\u30af\u306e\u5834\u5408\u3068\u4e00\u8cab\u3057\u3066\u304a\u308a\u3001\u3053\u3053\u3067\u306f\u7e70\u308a\u8fd4\u3057\u307e\u305b\u3093\u3002

        "},{"location":"chapter_stack_and_queue/queue/#523","title":"5.2.3 \u00a0 \u30ad\u30e5\u30fc\u306e\u5178\u578b\u7684\u306a\u5fdc\u7528","text":"
        • Amazon\u306e\u6ce8\u6587\uff1a\u8cb7\u3044\u7269\u5ba2\u304c\u6ce8\u6587\u3092\u884c\u3063\u305f\u5f8c\u3001\u3053\u308c\u3089\u306e\u6ce8\u6587\u306f\u30ad\u30e5\u30fc\u306b\u53c2\u52a0\u3057\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u9806\u756a\u306b\u51e6\u7406\u3057\u307e\u3059\u3002\u72ec\u8eab\u306e\u65e5\u306a\u3069\u306e\u30a4\u30d9\u30f3\u30c8\u4e2d\u306f\u3001\u77ed\u6642\u9593\u3067\u5927\u91cf\u306e\u6ce8\u6587\u304c\u751f\u6210\u3055\u308c\u3001\u9ad8\u3044\u540c\u6642\u5b9f\u884c\u6027\u304c\u30a8\u30f3\u30b8\u30cb\u30a2\u306b\u3068\u3063\u3066\u91cd\u8981\u306a\u8ab2\u984c\u3068\u306a\u308a\u307e\u3059\u3002
        • \u69d8\u3005\u306aToDo\u30ea\u30b9\u30c8\uff1a\u300c\u5148\u7740\u9806\u300d\u6a5f\u80fd\u304c\u5fc5\u8981\u306a\u30b7\u30ca\u30ea\u30aa\u3001\u4f8b\u3048\u3070\u30d7\u30ea\u30f3\u30bf\u30fc\u306e\u30bf\u30b9\u30af\u30ad\u30e5\u30fc\u3084\u30ec\u30b9\u30c8\u30e9\u30f3\u306e\u914d\u9054\u30ad\u30e5\u30fc\u306a\u3069\u3001\u30ad\u30e5\u30fc\u3067\u51e6\u7406\u9806\u5e8f\u3092\u52b9\u679c\u7684\u306b\u7dad\u6301\u3067\u304d\u307e\u3059\u3002
        "},{"location":"chapter_stack_and_queue/stack/","title":"5.1 \u00a0 \u30b9\u30bf\u30c3\u30af","text":"

        \u30b9\u30bf\u30c3\u30af\u306f\u3001\u5f8c\u5165\u5148\u51fa\uff08LIFO\uff09\u306e\u539f\u5247\u306b\u5f93\u3046\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3059\u3002

        \u30b9\u30bf\u30c3\u30af\u3092\u30c6\u30fc\u30d6\u30eb\u4e0a\u306e\u76bf\u306e\u5c71\u306b\u4f8b\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u5e95\u306e\u76bf\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u306b\u306f\u3001\u307e\u305a\u4e0a\u306e\u76bf\u3092\u53d6\u308a\u9664\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u76bf\u3092\u69d8\u3005\u306a\u7a2e\u985e\u306e\u8981\u7d20\uff08\u6574\u6570\u3001\u6587\u5b57\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306a\u3069\uff09\u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u3067\u3001\u30b9\u30bf\u30c3\u30af\u3068\u547c\u3070\u308c\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u8981\u7d20\u306e\u5c71\u306e\u4e0a\u90e8\u3092\u300c\u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u300d\u3001\u4e0b\u90e8\u3092\u300c\u30b9\u30bf\u30c3\u30af\u306e\u30dc\u30c8\u30e0\u300d\u3068\u547c\u3073\u307e\u3059\u3002\u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u306b\u8981\u7d20\u3092\u8ffd\u52a0\u3059\u308b\u64cd\u4f5c\u3092\u300c\u30d7\u30c3\u30b7\u30e5\u300d\u3001\u30c8\u30c3\u30d7\u8981\u7d20\u3092\u524a\u9664\u3059\u308b\u64cd\u4f5c\u3092\u300c\u30dd\u30c3\u30d7\u300d\u3068\u547c\u3073\u307e\u3059\u3002

        \u56f3 5-1 \u00a0 \u30b9\u30bf\u30c3\u30af\u306e\u5f8c\u5165\u5148\u51fa\u30eb\u30fc\u30eb

        "},{"location":"chapter_stack_and_queue/stack/#511","title":"5.1.1 \u00a0 \u30b9\u30bf\u30c3\u30af\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c","text":"

        \u30b9\u30bf\u30c3\u30af\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c\u3092\u4e0b\u8868\u306b\u793a\u3057\u307e\u3059\u3002\u5177\u4f53\u7684\u306a\u30e1\u30bd\u30c3\u30c9\u540d\u306f\u4f7f\u7528\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u3088\u3063\u3066\u7570\u306a\u308a\u307e\u3059\u3002\u3053\u3053\u3067\u306f\u3001\u4f8b\u3068\u3057\u3066push()\u3001pop()\u3001peek()\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        \u8868 5-1 \u00a0 \u30b9\u30bf\u30c3\u30af\u64cd\u4f5c\u306e\u52b9\u7387

        \u30e1\u30bd\u30c3\u30c9 \u8aac\u660e \u6642\u9593\u8a08\u7b97\u91cf push() \u8981\u7d20\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5\uff08\u30c8\u30c3\u30d7\u306b\u8ffd\u52a0\uff09 \\(O(1)\\) pop() \u30b9\u30bf\u30c3\u30af\u304b\u3089\u30c8\u30c3\u30d7\u8981\u7d20\u3092\u30dd\u30c3\u30d7 \\(O(1)\\) peek() \u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 \\(O(1)\\)

        \u901a\u5e38\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u7d44\u307f\u8fbc\u307e\u308c\u3066\u3044\u308b\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9\u3092\u76f4\u63a5\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u4e00\u90e8\u306e\u8a00\u8a9e\u3067\u306f\u5177\u4f53\u7684\u306b\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9\u3092\u63d0\u4f9b\u3057\u3066\u3044\u306a\u3044\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u5834\u5408\u3001\u8a00\u8a9e\u306e\u300c\u914d\u5217\u300d\u307e\u305f\u306f\u300c\u9023\u7d50\u30ea\u30b9\u30c8\u300d\u3092\u30b9\u30bf\u30c3\u30af\u3068\u3057\u3066\u4f7f\u7528\u3057\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u30b9\u30bf\u30c3\u30af\u30ed\u30b8\u30c3\u30af\u306b\u95a2\u9023\u3057\u306a\u3044\u64cd\u4f5c\u3092\u7121\u8996\u3067\u304d\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin stack.py
        # \u30b9\u30bf\u30c3\u30af\u3092\u521d\u671f\u5316\n# Python\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9\u304c\u306a\u3044\u305f\u3081\u3001list\u3092\u30b9\u30bf\u30c3\u30af\u3068\u3057\u3066\u4f7f\u7528\nstack: list[int] = []\n\n# \u8981\u7d20\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n# \u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\npeek: int = stack[-1]\n\n# \u30b9\u30bf\u30c3\u30af\u304b\u3089\u8981\u7d20\u3092\u30dd\u30c3\u30d7\npop: int = stack.pop()\n\n# \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97\nsize: int = len(stack)\n\n# \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af\nis_empty: bool = len(stack) == 0\n
        stack.cpp
        /* \u30b9\u30bf\u30c3\u30af\u3092\u521d\u671f\u5316 */\nstack<int> stack;\n\n/* \u8981\u7d20\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint top = stack.top();\n\n/* \u30b9\u30bf\u30c3\u30af\u304b\u3089\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\nstack.pop(); // \u623b\u308a\u5024\u306a\u3057\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = stack.size();\n\n/* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nbool empty = stack.empty();\n
        stack.java
        /* \u30b9\u30bf\u30c3\u30af\u3092\u521d\u671f\u5316 */\nStack<Integer> stack = new Stack<>();\n\n/* \u8981\u7d20\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint peek = stack.peek();\n\n/* \u30b9\u30bf\u30c3\u30af\u304b\u3089\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\nint pop = stack.pop();\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = stack.size();\n\n/* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nboolean isEmpty = stack.isEmpty();\n
        stack.cs
        /* \u30b9\u30bf\u30c3\u30af\u3092\u521d\u671f\u5316 */\nStack<int> stack = new();\n\n/* \u8981\u7d20\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5 */\nstack.Push(1);\nstack.Push(3);\nstack.Push(2);\nstack.Push(5);\nstack.Push(4);\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint peek = stack.Peek();\n\n/* \u30b9\u30bf\u30c3\u30af\u304b\u3089\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\nint pop = stack.Pop();\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = stack.Count;\n\n/* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nbool isEmpty = stack.Count == 0;\n
        stack_test.go
        /* \u30b9\u30bf\u30c3\u30af\u3092\u521d\u671f\u5316 */\n// Go\u3067\u306f\u3001Slice\u3092\u30b9\u30bf\u30c3\u30af\u3068\u3057\u3066\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u63a8\u5968\u3055\u308c\u307e\u3059\nvar stack []int\n\n/* \u8981\u7d20\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5 */\nstack = append(stack, 1)\nstack = append(stack, 3)\nstack = append(stack, 2)\nstack = append(stack, 5)\nstack = append(stack, 4)\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\npeek := stack[len(stack)-1]\n\n/* \u30b9\u30bf\u30c3\u30af\u304b\u3089\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\npop := stack[len(stack)-1]\nstack = stack[:len(stack)-1]\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nsize := len(stack)\n\n/* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nisEmpty := len(stack) == 0\n
        stack.swift
        /* \u30b9\u30bf\u30c3\u30af\u3092\u521d\u671f\u5316 */\n// Swift\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9\u304c\u306a\u3044\u305f\u3081\u3001Array\u3092\u30b9\u30bf\u30c3\u30af\u3068\u3057\u3066\u4f7f\u7528\nvar stack: [Int] = []\n\n/* \u8981\u7d20\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5 */\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nlet peek = stack.last!\n\n/* \u30b9\u30bf\u30c3\u30af\u304b\u3089\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\nlet pop = stack.removeLast()\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nlet size = stack.count\n\n/* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nlet isEmpty = stack.isEmpty\n
        stack.js
        /* \u30b9\u30bf\u30c3\u30af\u3092\u521d\u671f\u5316 */\n// JavaScript\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9\u304c\u306a\u3044\u305f\u3081\u3001Array\u3092\u30b9\u30bf\u30c3\u30af\u3068\u3057\u3066\u4f7f\u7528\nconst stack = [];\n\n/* \u8981\u7d20\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nconst peek = stack[stack.length-1];\n\n/* \u30b9\u30bf\u30c3\u30af\u304b\u3089\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\nconst pop = stack.pop();\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nconst size = stack.length;\n\n/* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nconst is_empty = stack.length === 0;\n
        stack.ts
        /* \u30b9\u30bf\u30c3\u30af\u3092\u521d\u671f\u5316 */\n// TypeScript\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9\u304c\u306a\u3044\u305f\u3081\u3001Array\u3092\u30b9\u30bf\u30c3\u30af\u3068\u3057\u3066\u4f7f\u7528\nconst stack: number[] = [];\n\n/* \u8981\u7d20\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nconst peek = stack[stack.length - 1];\n\n/* \u30b9\u30bf\u30c3\u30af\u304b\u3089\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\nconst pop = stack.pop();\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nconst size = stack.length;\n\n/* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nconst is_empty = stack.length === 0;\n
        stack.dart
        /* \u30b9\u30bf\u30c3\u30af\u3092\u521d\u671f\u5316 */\n// Dart\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9\u304c\u306a\u3044\u305f\u3081\u3001List\u3092\u30b9\u30bf\u30c3\u30af\u3068\u3057\u3066\u4f7f\u7528\nList<int> stack = [];\n\n/* \u8981\u7d20\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5 */\nstack.add(1);\nstack.add(3);\nstack.add(2);\nstack.add(5);\nstack.add(4);\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint peek = stack.last;\n\n/* \u30b9\u30bf\u30c3\u30af\u304b\u3089\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\nint pop = stack.removeLast();\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = stack.length;\n\n/* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nbool isEmpty = stack.isEmpty;\n
        stack.rs
        /* \u30b9\u30bf\u30c3\u30af\u3092\u521d\u671f\u5316 */\n// Vec\u3092\u30b9\u30bf\u30c3\u30af\u3068\u3057\u3066\u4f7f\u7528\nlet mut stack: Vec<i32> = Vec::new();\n\n/* \u8981\u7d20\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nlet top = stack.last().unwrap();\n\n/* \u30b9\u30bf\u30c3\u30af\u304b\u3089\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\nlet pop = stack.pop().unwrap();\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nlet size = stack.len();\n\n/* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nlet is_empty = stack.is_empty();\n
        stack.c
        // C\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30b9\u30bf\u30c3\u30af\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        stack.kt
        \n
        "},{"location":"chapter_stack_and_queue/stack/#512","title":"5.1.2 \u00a0 \u30b9\u30bf\u30c3\u30af\u306e\u5b9f\u88c5","text":"

        \u30b9\u30bf\u30c3\u30af\u304c\u3069\u306e\u3088\u3046\u306b\u52d5\u4f5c\u3059\u308b\u304b\u3092\u3088\u308a\u6df1\u304f\u7406\u89e3\u3059\u308b\u305f\u3081\u306b\u3001\u81ea\u5206\u3067\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9\u3092\u5b9f\u88c5\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002

        \u30b9\u30bf\u30c3\u30af\u306f\u5f8c\u5165\u5148\u51fa\u306e\u539f\u5247\u306b\u5f93\u3046\u305f\u3081\u3001\u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u3067\u306e\u307f\u8981\u7d20\u3092\u8ffd\u52a0\u307e\u305f\u306f\u524a\u9664\u3067\u304d\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u4e21\u65b9\u306f\u4efb\u610f\u306e\u4f4d\u7f6e\u3067\u8981\u7d20\u3092\u8ffd\u52a0\u30fb\u524a\u9664\u3067\u304d\u308b\u305f\u3081\u3001\u30b9\u30bf\u30c3\u30af\u306f\u5236\u9650\u3055\u308c\u305f\u914d\u5217\u307e\u305f\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u3068\u898b\u306a\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u8a00\u3044\u63db\u3048\u308c\u3070\u3001\u914d\u5217\u3084\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u7279\u5b9a\u306e\u7121\u95a2\u4fc2\u306a\u64cd\u4f5c\u3092\u300c\u906e\u853d\u300d\u3057\u3066\u3001\u5916\u90e8\u306e\u52d5\u4f5c\u3092\u30b9\u30bf\u30c3\u30af\u306e\u7279\u6027\u306b\u5408\u308f\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_stack_and_queue/stack/#1","title":"1. \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u30d9\u30fc\u30b9\u306e\u5b9f\u88c5","text":"

        \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u30b9\u30bf\u30c3\u30af\u3092\u5b9f\u88c5\u3059\u308b\u5834\u5408\u3001\u30ea\u30b9\u30c8\u306e\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u3092\u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u3001\u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u3092\u30b9\u30bf\u30c3\u30af\u306e\u30dc\u30c8\u30e0\u3068\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30d7\u30c3\u30b7\u30e5\u64cd\u4f5c\u3067\u306f\u3001\u5358\u306b\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30d8\u30c3\u30c9\u306b\u8981\u7d20\u3092\u633f\u5165\u3057\u307e\u3059\u3002\u3053\u306e\u30ce\u30fc\u30c9\u633f\u5165\u65b9\u6cd5\u306f\u300c\u30d8\u30c3\u30c9\u633f\u5165\u300d\u3068\u3057\u3066\u77e5\u3089\u308c\u3066\u3044\u307e\u3059\u3002\u30dd\u30c3\u30d7\u64cd\u4f5c\u3067\u306f\u3001\u30ea\u30b9\u30c8\u304b\u3089\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3059\u308b\u3060\u3051\u3067\u3059\u3002

        LinkedListStackpush()pop()

        \u56f3 5-2 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u306b\u3088\u308b\u30b9\u30bf\u30c3\u30af\u5b9f\u88c5\u306e\u30d7\u30c3\u30b7\u30e5\u3068\u30dd\u30c3\u30d7\u64cd\u4f5c

        \u4ee5\u4e0b\u306f\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u30b9\u30bf\u30c3\u30af\u5b9f\u88c5\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linkedlist_stack.py
        class LinkedListStack:\n    \"\"\"\u9023\u7d50\u30ea\u30b9\u30c8\u30d9\u30fc\u30b9\u306e\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9\"\"\"\n\n    def __init__(self):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        self._peek: ListNode | None = None\n        self._size: int = 0\n\n    def size(self) -> int:\n        \"\"\"\u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\"\"\"\n        return self._size == 0\n\n    def push(self, val: int):\n        \"\"\"\u30d7\u30c3\u30b7\u30e5\"\"\"\n        node = ListNode(val)\n        node.next = self._peek\n        self._peek = node\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"\u30dd\u30c3\u30d7\"\"\"\n        num = self.peek()\n        self._peek = self._peek.next\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"\u30b9\u30bf\u30c3\u30af\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Stack is empty\")\n        return self._peek.val\n\n    def to_list(self) -> list[int]:\n        \"\"\"\u51fa\u529b\u7528\u306e\u30ea\u30b9\u30c8\u306b\u5909\u63db\"\"\"\n        arr = []\n        node = self._peek\n        while node:\n            arr.append(node.val)\n            node = node.next\n        arr.reverse()\n        return arr\n
        linkedlist_stack.cpp
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9 */\nclass LinkedListStack {\n  private:\n    ListNode *stackTop; // \u5148\u982d\u30ce\u30fc\u30c9\u3092\u30b9\u30bf\u30c3\u30af\u30c8\u30c3\u30d7\u3068\u3057\u3066\u4f7f\u7528\n    int stkSize;        // \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\n\n  public:\n    LinkedListStack() {\n        stackTop = nullptr;\n        stkSize = 0;\n    }\n\n    ~LinkedListStack() {\n        // \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8d70\u67fb\u3001\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3001\u30e1\u30e2\u30ea\u3092\u89e3\u653e\n        freeMemoryLinkedList(stackTop);\n    }\n\n    /* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    int size() {\n        return stkSize;\n    }\n\n    /* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    bool isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u30d7\u30c3\u30b7\u30e5 */\n    void push(int num) {\n        ListNode *node = new ListNode(num);\n        node->next = stackTop;\n        stackTop = node;\n        stkSize++;\n    }\n\n    /* \u30dd\u30c3\u30d7 */\n    int pop() {\n        int num = top();\n        ListNode *tmp = stackTop;\n        stackTop = stackTop->next;\n        // \u30e1\u30e2\u30ea\u3092\u89e3\u653e\n        delete tmp;\n        stkSize--;\n        return num;\n    }\n\n    /* \u30b9\u30bf\u30c3\u30af\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    int top() {\n        if (isEmpty())\n            throw out_of_range(\"Stack is empty\");\n        return stackTop->val;\n    }\n\n    /* \u30ea\u30b9\u30c8\u3092\u914d\u5217\u306b\u5909\u63db\u3057\u3066\u8fd4\u5374 */\n    vector<int> toVector() {\n        ListNode *node = stackTop;\n        vector<int> res(size());\n        for (int i = res.size() - 1; i >= 0; i--) {\n            res[i] = node->val;\n            node = node->next;\n        }\n        return res;\n    }\n};\n
        linkedlist_stack.java
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9 */\nclass LinkedListStack {\n    private ListNode stackPeek; // \u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u3092\u30b9\u30bf\u30c3\u30af\u30c8\u30c3\u30d7\u3068\u3057\u3066\u4f7f\u7528\n    private int stkSize = 0; // \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\n\n    public LinkedListStack() {\n        stackPeek = null;\n    }\n\n    /* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    public int size() {\n        return stkSize;\n    }\n\n    /* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u30d7\u30c3\u30b7\u30e5 */\n    public void push(int num) {\n        ListNode node = new ListNode(num);\n        node.next = stackPeek;\n        stackPeek = node;\n        stkSize++;\n    }\n\n    /* \u30dd\u30c3\u30d7 */\n    public int pop() {\n        int num = peek();\n        stackPeek = stackPeek.next;\n        stkSize--;\n        return num;\n    }\n\n    /* \u30b9\u30bf\u30c3\u30af\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return stackPeek.val;\n    }\n\n    /* List \u3092 Array \u306b\u5909\u63db\u3057\u3066\u8fd4\u3059 */\n    public int[] toArray() {\n        ListNode node = stackPeek;\n        int[] res = new int[size()];\n        for (int i = res.length - 1; i >= 0; i--) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_stack.cs
        [class]{LinkedListStack}-[func]{}\n
        linkedlist_stack.go
        [class]{linkedListStack}-[func]{}\n
        linkedlist_stack.swift
        [class]{LinkedListStack}-[func]{}\n
        linkedlist_stack.js
        [class]{LinkedListStack}-[func]{}\n
        linkedlist_stack.ts
        [class]{LinkedListStack}-[func]{}\n
        linkedlist_stack.dart
        [class]{LinkedListStack}-[func]{}\n
        linkedlist_stack.rs
        [class]{LinkedListStack}-[func]{}\n
        linkedlist_stack.c
        [class]{LinkedListStack}-[func]{}\n
        linkedlist_stack.kt
        [class]{LinkedListStack}-[func]{}\n
        linkedlist_stack.rb
        [class]{LinkedListStack}-[func]{}\n
        "},{"location":"chapter_stack_and_queue/stack/#2","title":"2. \u00a0 \u914d\u5217\u30d9\u30fc\u30b9\u306e\u5b9f\u88c5","text":"

        \u914d\u5217\u3092\u4f7f\u7528\u3057\u3066\u30b9\u30bf\u30c3\u30af\u3092\u5b9f\u88c5\u3059\u308b\u5834\u5408\u3001\u914d\u5217\u306e\u672b\u5c3e\u3092\u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u3068\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30d7\u30c3\u30b7\u30e5\u3068\u30dd\u30c3\u30d7\u64cd\u4f5c\u306f\u3001\u305d\u308c\u305e\u308c\u914d\u5217\u306e\u672b\u5c3e\u3067\u306e\u8981\u7d20\u306e\u8ffd\u52a0\u3068\u524a\u9664\u306b\u5bfe\u5fdc\u3057\u3001\u3069\u3061\u3089\u3082\u6642\u9593\u8a08\u7b97\u91cf\\(O(1)\\)\u3067\u3059\u3002

        ArrayStackpush()pop()

        \u56f3 5-3 \u00a0 \u914d\u5217\u306b\u3088\u308b\u30b9\u30bf\u30c3\u30af\u5b9f\u88c5\u306e\u30d7\u30c3\u30b7\u30e5\u3068\u30dd\u30c3\u30d7\u64cd\u4f5c

        \u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5\u3055\u308c\u308b\u8981\u7d20\u304c\u7d99\u7d9a\u7684\u306b\u5897\u52a0\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3001\u52d5\u7684\u914d\u5217\u3092\u4f7f\u7528\u3067\u304d\u3001\u914d\u5217\u62e1\u5f35\u3092\u81ea\u5206\u3067\u51e6\u7406\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u305b\u3093\u3002\u4ee5\u4e0b\u306f\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_stack.py
        class ArrayStack:\n    \"\"\"\u914d\u5217\u30d9\u30fc\u30b9\u306e\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9\"\"\"\n\n    def __init__(self):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        self._stack: list[int] = []\n\n    def size(self) -> int:\n        \"\"\"\u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97\"\"\"\n        return len(self._stack)\n\n    def is_empty(self) -> bool:\n        \"\"\"\u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\"\"\"\n        return self.size() == 0\n\n    def push(self, item: int):\n        \"\"\"\u30d7\u30c3\u30b7\u30e5\"\"\"\n        self._stack.append(item)\n\n    def pop(self) -> int:\n        \"\"\"\u30dd\u30c3\u30d7\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Stack is empty\")\n        return self._stack.pop()\n\n    def peek(self) -> int:\n        \"\"\"\u30b9\u30bf\u30c3\u30af\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Stack is empty\")\n        return self._stack[-1]\n\n    def to_list(self) -> list[int]:\n        \"\"\"\u51fa\u529b\u7528\u306e\u914d\u5217\u3092\u8fd4\u3059\"\"\"\n        return self._stack\n
        array_stack.cpp
        /* \u914d\u5217\u306b\u57fa\u3065\u304f\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9 */\nclass ArrayStack {\n  private:\n    vector<int> stack;\n\n  public:\n    /* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    int size() {\n        return stack.size();\n    }\n\n    /* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    bool isEmpty() {\n        return stack.size() == 0;\n    }\n\n    /* \u30d7\u30c3\u30b7\u30e5 */\n    void push(int num) {\n        stack.push_back(num);\n    }\n\n    /* \u30dd\u30c3\u30d7 */\n    int pop() {\n        int num = top();\n        stack.pop_back();\n        return num;\n    }\n\n    /* \u30b9\u30bf\u30c3\u30af\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    int top() {\n        if (isEmpty())\n            throw out_of_range(\"Stack is empty\");\n        return stack.back();\n    }\n\n    /* Vector\u3092\u8fd4\u5374 */\n    vector<int> toVector() {\n        return stack;\n    }\n};\n
        array_stack.java
        /* \u914d\u5217\u306b\u57fa\u3065\u304f\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9 */\nclass ArrayStack {\n    private ArrayList<Integer> stack;\n\n    public ArrayStack() {\n        // \u30ea\u30b9\u30c8\uff08\u52d5\u7684\u914d\u5217\uff09\u3092\u521d\u671f\u5316\n        stack = new ArrayList<>();\n    }\n\n    /* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    public int size() {\n        return stack.size();\n    }\n\n    /* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u30d7\u30c3\u30b7\u30e5 */\n    public void push(int num) {\n        stack.add(num);\n    }\n\n    /* \u30dd\u30c3\u30d7 */\n    public int pop() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return stack.remove(size() - 1);\n    }\n\n    /* \u30b9\u30bf\u30c3\u30af\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return stack.get(size() - 1);\n    }\n\n    /* List \u3092 Array \u306b\u5909\u63db\u3057\u3066\u8fd4\u3059 */\n    public Object[] toArray() {\n        return stack.toArray();\n    }\n}\n
        array_stack.cs
        [class]{ArrayStack}-[func]{}\n
        array_stack.go
        [class]{arrayStack}-[func]{}\n
        array_stack.swift
        [class]{ArrayStack}-[func]{}\n
        array_stack.js
        [class]{ArrayStack}-[func]{}\n
        array_stack.ts
        [class]{ArrayStack}-[func]{}\n
        array_stack.dart
        [class]{ArrayStack}-[func]{}\n
        array_stack.rs
        [class]{ArrayStack}-[func]{}\n
        array_stack.c
        [class]{ArrayStack}-[func]{}\n
        array_stack.kt
        [class]{ArrayStack}-[func]{}\n
        array_stack.rb
        [class]{ArrayStack}-[func]{}\n
        "},{"location":"chapter_stack_and_queue/stack/#513-2","title":"5.1.3 \u00a0 2\u3064\u306e\u5b9f\u88c5\u306e\u6bd4\u8f03","text":"

        \u30b5\u30dd\u30fc\u30c8\u3055\u308c\u308b\u64cd\u4f5c

        \u4e21\u65b9\u306e\u5b9f\u88c5\u306f\u3001\u30b9\u30bf\u30c3\u30af\u3067\u5b9a\u7fa9\u3055\u308c\u305f\u3059\u3079\u3066\u306e\u64cd\u4f5c\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u307e\u3059\u3002\u914d\u5217\u5b9f\u88c5\u306f\u3055\u3089\u306b\u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u307e\u3059\u304c\u3001\u3053\u308c\u306f\u30b9\u30bf\u30c3\u30af\u5b9a\u7fa9\u306e\u7bc4\u56f2\u3092\u8d85\u3048\u3066\u304a\u308a\u3001\u4e00\u822c\u7684\u306b\u306f\u4f7f\u7528\u3055\u308c\u307e\u305b\u3093\u3002

        \u6642\u9593\u52b9\u7387

        \u914d\u5217\u30d9\u30fc\u30b9\u306e\u5b9f\u88c5\u3067\u306f\u3001\u30d7\u30c3\u30b7\u30e5\u3068\u30dd\u30c3\u30d7\u64cd\u4f5c\u306e\u4e21\u65b9\u304c\u4e8b\u524d\u306b\u5272\u308a\u5f53\u3066\u3089\u308c\u305f\u9023\u7d9a\u30e1\u30e2\u30ea\u3067\u767a\u751f\u3057\u3001\u826f\u597d\u306a\u30ad\u30e3\u30c3\u30b7\u30e5\u5c40\u6240\u6027\u304c\u3042\u308b\u305f\u3081\u52b9\u7387\u304c\u9ad8\u304f\u306a\u308a\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u30d7\u30c3\u30b7\u30e5\u64cd\u4f5c\u304c\u914d\u5217\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30ea\u30b5\u30a4\u30ba\u30e1\u30ab\u30cb\u30ba\u30e0\u304c\u30c8\u30ea\u30ac\u30fc\u3055\u308c\u3001\u305d\u306e\u30d7\u30c3\u30b7\u30e5\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u306b\u306a\u308a\u307e\u3059\u3002

        \u9023\u7d50\u30ea\u30b9\u30c8\u5b9f\u88c5\u3067\u306f\u3001\u30ea\u30b9\u30c8\u62e1\u5f35\u306f\u975e\u5e38\u306b\u67d4\u8edf\u3067\u3001\u914d\u5217\u62e1\u5f35\u306e\u3088\u3046\u306a\u52b9\u7387\u4f4e\u4e0b\u306e\u554f\u984c\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u3057\u304b\u3057\u3001\u30d7\u30c3\u30b7\u30e5\u64cd\u4f5c\u306b\u306f\u30ce\u30fc\u30c9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u521d\u671f\u5316\u3068\u30dd\u30a4\u30f3\u30bf\u306e\u5909\u66f4\u304c\u5fc5\u8981\u306a\u305f\u3081\u3001\u52b9\u7387\u306f\u6bd4\u8f03\u7684\u4f4e\u304f\u306a\u308a\u307e\u3059\u3002\u30d7\u30c3\u30b7\u30e5\u3055\u308c\u308b\u8981\u7d20\u304c\u3059\u3067\u306b\u30ce\u30fc\u30c9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5834\u5408\u3001\u521d\u671f\u5316\u30b9\u30c6\u30c3\u30d7\u3092\u30b9\u30ad\u30c3\u30d7\u3067\u304d\u3001\u52b9\u7387\u304c\u5411\u4e0a\u3057\u307e\u3059\u3002

        \u3057\u305f\u304c\u3063\u3066\u3001\u30d7\u30c3\u30b7\u30e5\u3068\u30dd\u30c3\u30d7\u64cd\u4f5c\u306e\u8981\u7d20\u304cint\u3084double\u306a\u3069\u306e\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u306e\u5834\u5408\u3001\u4ee5\u4e0b\u306e\u7d50\u8ad6\u3092\u5c0e\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a

        • \u914d\u5217\u30d9\u30fc\u30b9\u306e\u30b9\u30bf\u30c3\u30af\u5b9f\u88c5\u306f\u62e1\u5f35\u6642\u306b\u52b9\u7387\u304c\u4f4e\u4e0b\u3057\u307e\u3059\u304c\u3001\u62e1\u5f35\u306f\u4f4e\u983b\u5ea6\u64cd\u4f5c\u3067\u3042\u308b\u305f\u3081\u3001\u5e73\u5747\u52b9\u7387\u306f\u9ad8\u304f\u306a\u308a\u307e\u3059\u3002
        • \u9023\u7d50\u30ea\u30b9\u30c8\u30d9\u30fc\u30b9\u306e\u30b9\u30bf\u30c3\u30af\u5b9f\u88c5\u306f\u3088\u308a\u5b89\u5b9a\u3057\u305f\u52b9\u7387\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002

        \u7a7a\u9593\u52b9\u7387

        \u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316\u3059\u308b\u969b\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u300c\u521d\u671f\u5bb9\u91cf\u300d\u3092\u5272\u308a\u5f53\u3066\u307e\u3059\u304c\u3001\u3053\u308c\u306f\u5b9f\u969b\u306e\u5fc5\u8981\u91cf\u3092\u8d85\u3048\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3055\u3089\u306b\u3001\u62e1\u5f35\u30e1\u30ab\u30cb\u30ba\u30e0\u306f\u901a\u5e38\u3001\u7279\u5b9a\u306e\u4fc2\u6570\uff082\u500d\u306a\u3069\uff09\u3067\u5bb9\u91cf\u3092\u5897\u52a0\u3055\u305b\u3001\u3053\u308c\u3082\u5b9f\u969b\u306e\u5fc5\u8981\u91cf\u3092\u8d85\u3048\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u914d\u5217\u30d9\u30fc\u30b9\u306e\u30b9\u30bf\u30c3\u30af\u306f\u4e00\u90e8\u306e\u7a7a\u9593\u3092\u7121\u99c4\u306b\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        \u3057\u304b\u3057\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u306f\u30dd\u30a4\u30f3\u30bf\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306e\u8ffd\u52a0\u7a7a\u9593\u304c\u5fc5\u8981\u306a\u305f\u3081\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u304c\u5360\u6709\u3059\u308b\u7a7a\u9593\u306f\u6bd4\u8f03\u7684\u5927\u304d\u304f\u306a\u308a\u307e\u3059\u3002

        \u307e\u3068\u3081\u308b\u3068\u3001\u3069\u3061\u3089\u306e\u5b9f\u88c5\u304c\u3088\u308a\u30e1\u30e2\u30ea\u52b9\u7387\u7684\u304b\u3092\u5358\u7d14\u306b\u5224\u65ad\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002\u7279\u5b9a\u306e\u72b6\u6cc1\u306b\u57fa\u3065\u304f\u5206\u6790\u304c\u5fc5\u8981\u3067\u3059\u3002

        "},{"location":"chapter_stack_and_queue/stack/#514","title":"5.1.4 \u00a0 \u30b9\u30bf\u30c3\u30af\u306e\u5178\u578b\u7684\u306a\u5fdc\u7528","text":"
        • \u30d6\u30e9\u30a6\u30b6\u306e\u623b\u308b\u3068\u9032\u3080\u3001\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u306e\u5143\u306b\u623b\u3059\u3068\u3084\u308a\u76f4\u3057\u3002\u65b0\u3057\u3044Web\u30da\u30fc\u30b8\u3092\u958b\u304f\u305f\u3073\u306b\u3001\u30d6\u30e9\u30a6\u30b6\u306f\u524d\u306e\u30da\u30fc\u30b8\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5\u3057\u3001\u623b\u308b\u64cd\u4f5c\uff08\u672c\u8cea\u7684\u306b\u306f\u30dd\u30c3\u30d7\u64cd\u4f5c\uff09\u3092\u901a\u3058\u3066\u524d\u306e\u30da\u30fc\u30b8\u306b\u623b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u623b\u308b\u3068\u9032\u3080\u306e\u4e21\u65b9\u3092\u30b5\u30dd\u30fc\u30c8\u3059\u308b\u306b\u306f\u30012\u3064\u306e\u30b9\u30bf\u30c3\u30af\u304c\u9023\u643a\u3057\u3066\u52d5\u4f5c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u30e1\u30e2\u30ea\u7ba1\u7406\u3002\u95a2\u6570\u304c\u547c\u3073\u51fa\u3055\u308c\u308b\u305f\u3073\u306b\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u306b\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u3092\u8ffd\u52a0\u3057\u3066\u95a2\u6570\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u60c5\u5831\u3092\u8a18\u9332\u3057\u307e\u3059\u3002\u518d\u5e30\u95a2\u6570\u3067\u306f\u3001\u4e0b\u65b9\u5411\u306e\u518d\u5e30\u30d5\u30a7\u30fc\u30ba\u306f\u30b9\u30bf\u30c3\u30af\u3078\u306e\u30d7\u30c3\u30b7\u30e5\u3092\u7d9a\u3051\u3001\u4e0a\u65b9\u5411\u306e\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30d5\u30a7\u30fc\u30ba\u306f\u30b9\u30bf\u30c3\u30af\u304b\u3089\u306e\u30dd\u30c3\u30d7\u3092\u7d9a\u3051\u307e\u3059\u3002
        "},{"location":"chapter_stack_and_queue/summary/","title":"5.4 \u00a0 \u307e\u3068\u3081","text":""},{"location":"chapter_stack_and_queue/summary/#1","title":"1. \u00a0 \u91cd\u8981\u306a\u30dd\u30a4\u30f3\u30c8","text":"
        • \u30b9\u30bf\u30c3\u30af\u306f\u5f8c\u5165\u308c\u5148\u51fa\u3057\uff08LIFO\uff09\u306e\u539f\u5247\u306b\u5f93\u3046\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3001\u914d\u5217\u307e\u305f\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u4f7f\u3063\u3066\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002
        • \u6642\u9593\u52b9\u7387\u306e\u89b3\u70b9\u3067\u306f\u3001\u30b9\u30bf\u30c3\u30af\u306e\u914d\u5217\u5b9f\u88c5\u306e\u65b9\u304c\u5e73\u5747\u7684\u306a\u52b9\u7387\u304c\u9ad8\u3044\u3067\u3059\u3002\u305f\u3060\u3057\u3001\u62e1\u5f35\u6642\u306b\u306f\u5358\u4e00\u306e\u30d7\u30c3\u30b7\u30e5\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(n)\\)\u306b\u60aa\u5316\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u5bfe\u7167\u7684\u306b\u3001\u30b9\u30bf\u30c3\u30af\u306e\u9023\u7d50\u30ea\u30b9\u30c8\u5b9f\u88c5\u306f\u3088\u308a\u5b89\u5b9a\u3057\u305f\u52b9\u7387\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002
        • \u7a7a\u9593\u52b9\u7387\u306b\u95a2\u3057\u3066\u306f\u3001\u30b9\u30bf\u30c3\u30af\u306e\u914d\u5217\u5b9f\u88c5\u306f\u4e00\u5b9a\u7a0b\u5ea6\u306e\u7a7a\u9593\u306e\u7121\u99c4\u306b\u3064\u306a\u304c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30ce\u30fc\u30c9\u304c\u5360\u6709\u3059\u308b\u30e1\u30e2\u30ea\u7a7a\u9593\u306f\u4e00\u822c\u7684\u306b\u914d\u5217\u306e\u8981\u7d20\u3088\u308a\u3082\u5927\u304d\u3044\u3053\u3068\u306b\u6ce8\u610f\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002
        • \u30ad\u30e5\u30fc\u306f\u5148\u5165\u308c\u5148\u51fa\u3057\uff08FIFO\uff09\u306e\u539f\u5247\u306b\u5f93\u3046\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3001\u540c\u69d8\u306b\u914d\u5217\u307e\u305f\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u4f7f\u3063\u3066\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002\u30ad\u30e5\u30fc\u306e\u6642\u9593\u3068\u7a7a\u9593\u52b9\u7387\u306b\u95a2\u3059\u308b\u7d50\u8ad6\u306f\u3001\u30b9\u30bf\u30c3\u30af\u3068\u4f3c\u3066\u3044\u307e\u3059\u3002
        • \u4e21\u7aef\u30ad\u30e5\u30fc\uff08deque\uff09\u306f\u3088\u308a\u67d4\u8edf\u306a\u30ad\u30e5\u30fc\u306e\u7a2e\u985e\u3067\u3001\u4e21\u7aef\u3067\u306e\u8981\u7d20\u306e\u8ffd\u52a0\u3068\u524a\u9664\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002
        "},{"location":"chapter_stack_and_queue/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q: \u30d6\u30e9\u30a6\u30b6\u306e\u9032\u3080\u30fb\u623b\u308b\u6a5f\u80fd\u306f\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u5b9f\u88c5\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3059\u304b\uff1f

        \u30d6\u30e9\u30a6\u30b6\u306e\u9032\u3080\u30fb\u623b\u308b\u30ca\u30d3\u30b2\u30fc\u30b7\u30e7\u30f3\u306f\u672c\u8cea\u7684\u306b\u300c\u30b9\u30bf\u30c3\u30af\u300d\u6982\u5ff5\u306e\u73fe\u308c\u3067\u3059\u3002\u30e6\u30fc\u30b6\u30fc\u304c\u65b0\u3057\u3044\u30da\u30fc\u30b8\u3092\u8a2a\u554f\u3059\u308b\u3068\u3001\u305d\u306e\u30da\u30fc\u30b8\u304c\u30b9\u30bf\u30c3\u30af\u306e\u5148\u982d\u306b\u8ffd\u52a0\u3055\u308c\u307e\u3059\u3002\u623b\u308b\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u3068\u3001\u30da\u30fc\u30b8\u304c\u30b9\u30bf\u30c3\u30af\u306e\u5148\u982d\u304b\u3089\u30dd\u30c3\u30d7\u3055\u308c\u307e\u3059\u3002\u4e21\u7aef\u30ad\u30e5\u30fc\uff08deque\uff09\u306f\u3001\u300c\u4e21\u7aef\u30ad\u30e5\u30fc\u300d\u306e\u7ae0\u3067\u8ff0\u3079\u305f\u3088\u3046\u306b\u3001\u3044\u304f\u3064\u304b\u306e\u8ffd\u52a0\u64cd\u4f5c\u3092\u4fbf\u5229\u306b\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002

        Q: \u30b9\u30bf\u30c3\u30af\u304b\u3089\u30dd\u30c3\u30d7\u3057\u305f\u5f8c\u3001\u30dd\u30c3\u30d7\u3055\u308c\u305f\u30ce\u30fc\u30c9\u306e\u30e1\u30e2\u30ea\u3092\u89e3\u653e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u304b\uff1f

        \u30dd\u30c3\u30d7\u3055\u308c\u305f\u30ce\u30fc\u30c9\u304c\u5f8c\u3067\u4f7f\u7528\u3055\u308c\u308b\u5834\u5408\u306f\u3001\u305d\u306e\u30e1\u30e2\u30ea\u3092\u89e3\u653e\u3059\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u81ea\u52d5\u30ac\u30d9\u30fc\u30b8\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3092\u6301\u3064Java\u3084Python\u306a\u3069\u306e\u8a00\u8a9e\u3067\u306f\u3001\u624b\u52d5\u306e\u30e1\u30e2\u30ea\u89e3\u653e\u306f\u5fc5\u8981\u3042\u308a\u307e\u305b\u3093\u3002C\u3084C++\u3067\u306f\u3001\u624b\u52d5\u306e\u30e1\u30e2\u30ea\u89e3\u653e\u304c\u5fc5\u8981\u3067\u3059\u3002

        Q: \u4e21\u7aef\u30ad\u30e5\u30fc\u306f2\u3064\u306e\u30b9\u30bf\u30c3\u30af\u3092\u7d50\u5408\u3057\u305f\u3082\u306e\u306e\u3088\u3046\u306b\u898b\u3048\u307e\u3059\u3002\u305d\u306e\u7528\u9014\u306f\u4f55\u3067\u3059\u304b\uff1f

        \u4e21\u7aef\u30ad\u30e5\u30fc\u306f\u3001\u30b9\u30bf\u30c3\u30af\u3068\u30ad\u30e5\u30fc\u306e\u7d44\u307f\u5408\u308f\u305b\u307e\u305f\u306f2\u3064\u306e\u30b9\u30bf\u30c3\u30af\u3092\u7d50\u5408\u3057\u305f\u3082\u306e\u3067\u3001\u30b9\u30bf\u30c3\u30af\u3068\u30ad\u30e5\u30fc\u306e\u4e21\u65b9\u306e\u30ed\u30b8\u30c3\u30af\u3092\u793a\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30b9\u30bf\u30c3\u30af\u3068\u30ad\u30e5\u30fc\u306e\u3059\u3079\u3066\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u5b9f\u88c5\u3067\u304d\u3001\u3088\u308a\u5927\u304d\u306a\u67d4\u8edf\u6027\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002

        Q: \u5143\u306b\u623b\u3059\u3068\u3084\u308a\u76f4\u3057\u306f\u5177\u4f53\u7684\u306b\u3069\u306e\u3088\u3046\u306b\u5b9f\u88c5\u3055\u308c\u308b\u306e\u3067\u3059\u304b\uff1f

        \u5143\u306b\u623b\u3059\u3068\u3084\u308a\u76f4\u3057\u306e\u64cd\u4f5c\u306f2\u3064\u306e\u30b9\u30bf\u30c3\u30af\u3092\u4f7f\u3063\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\uff1a\u5143\u306b\u623b\u3059\u7528\u306e\u30b9\u30bf\u30c3\u30afA\u3068\u3084\u308a\u76f4\u3057\u7528\u306e\u30b9\u30bf\u30c3\u30afB\u3067\u3059\u3002

        1. \u30e6\u30fc\u30b6\u30fc\u304c\u64cd\u4f5c\u3092\u5b9f\u884c\u3059\u308b\u305f\u3073\u306b\u3001\u305d\u308c\u304c\u30b9\u30bf\u30c3\u30afA\u306b\u30d7\u30c3\u30b7\u30e5\u3055\u308c\u3001\u30b9\u30bf\u30c3\u30afB\u304c\u30af\u30ea\u30a2\u3055\u308c\u307e\u3059\u3002
        2. \u30e6\u30fc\u30b6\u30fc\u304c\u300c\u5143\u306b\u623b\u3059\u300d\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001\u6700\u65b0\u306e\u64cd\u4f5c\u304c\u30b9\u30bf\u30c3\u30afA\u304b\u3089\u30dd\u30c3\u30d7\u3055\u308c\u3001\u30b9\u30bf\u30c3\u30afB\u306b\u30d7\u30c3\u30b7\u30e5\u3055\u308c\u307e\u3059\u3002
        3. \u30e6\u30fc\u30b6\u30fc\u304c\u300c\u3084\u308a\u76f4\u3057\u300d\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001\u6700\u65b0\u306e\u64cd\u4f5c\u304c\u30b9\u30bf\u30c3\u30afB\u304b\u3089\u30dd\u30c3\u30d7\u3055\u308c\u3001\u30b9\u30bf\u30c3\u30afA\u306b\u623b\u3055\u308c\u307e\u3059\u3002
        "},{"location":"chapter_tree/","title":"\u7b2c 7 \u7ae0 \u00a0 \u6728","text":"

        Abstract

        \u305d\u3073\u3048\u7acb\u3064\u6728\u306f\u6d3b\u529b\u306b\u6e80\u3061\u305f\u672c\u8cea\u3092\u653e\u3061\u3001\u6df1\u3044\u6839\u3068\u8c4a\u304b\u306a\u8449\u3092\u8a87\u308a\u306a\u304c\u3089\u3082\u3001\u305d\u306e\u679d\u306f\u758e\u3089\u306b\u6563\u3089\u3070\u308a\u3001\u5e7d\u7384\u306a\u96f0\u56f2\u6c17\u3092\u91b8\u3057\u51fa\u3057\u3066\u3044\u307e\u3059\u3002

        \u305d\u308c\u306f\u30c7\u30fc\u30bf\u306b\u304a\u3051\u308b\u5206\u5272\u7d71\u6cbb\u306e\u9bae\u3084\u304b\u306a\u5f62\u3092\u79c1\u305f\u3061\u306b\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_tree/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 7.1 \u00a0 \u4e8c\u5206\u6728
        • 7.2 \u00a0 \u4e8c\u5206\u6728\u306e\u8d70\u67fb
        • 7.3 \u00a0 \u6728\u306e\u914d\u5217\u8868\u73fe
        • 7.4 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728
        • 7.5 \u00a0 AVL\u6728 *
        • 7.6 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_tree/array_representation_of_tree/","title":"7.3 \u00a0 \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe","text":"

        \u9023\u7d50\u30ea\u30b9\u30c8\u8868\u73fe\u3067\u306f\u3001\u4e8c\u5206\u6728\u306e\u683c\u7d0d\u5358\u4f4d\u306f\u30ce\u30fc\u30c9TreeNode\u3067\u3042\u308a\u3001\u30ce\u30fc\u30c9\u306f\u30dd\u30a4\u30f3\u30bf\u306b\u3088\u3063\u3066\u63a5\u7d9a\u3055\u308c\u307e\u3059\u3002\u9023\u7d50\u30ea\u30b9\u30c8\u8868\u73fe\u3067\u306e\u4e8c\u5206\u6728\u306e\u57fa\u672c\u64cd\u4f5c\u306b\u3064\u3044\u3066\u306f\u524d\u306e\u7bc0\u3067\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002

        \u3067\u306f\u3001\u914d\u5217\u3092\u4f7f\u3063\u3066\u4e8c\u5206\u6728\u3092\u8868\u73fe\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u308b\u3067\u3057\u3087\u3046\u304b\uff1f\u7b54\u3048\u306f\u30a4\u30a8\u30b9\u3067\u3059\u3002

        "},{"location":"chapter_tree/array_representation_of_tree/#731","title":"7.3.1 \u00a0 \u5b8c\u5168\u4e8c\u5206\u6728\u306e\u8868\u73fe","text":"

        \u307e\u305a\u7c21\u5358\u306a\u30b1\u30fc\u30b9\u304b\u3089\u5206\u6790\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u5b8c\u5168\u4e8c\u5206\u6728\u304c\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001\u30ec\u30d9\u30eb\u9806\u63a2\u7d22\u306e\u9806\u5e8f\u306b\u5f93\u3063\u3066\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3092\u914d\u5217\u306b\u683c\u7d0d\u3057\u3001\u5404\u30ce\u30fc\u30c9\u306f\u4e00\u610f\u306e\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002

        \u30ec\u30d9\u30eb\u9806\u63a2\u7d22\u306e\u7279\u6027\u306b\u57fa\u3065\u3044\u3066\u3001\u89aa\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3068\u305d\u306e\u5b50\u30ce\u30fc\u30c9\u306e\u9593\u306e\u300c\u30de\u30c3\u30d4\u30f3\u30b0\u516c\u5f0f\u300d\u3092\u5c0e\u304d\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\\(i\\)\u306e\u5834\u5408\u3001\u305d\u306e\u5de6\u306e\u5b50\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\\(2i + 1\\)\u3001\u53f3\u306e\u5b50\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\\(2i + 2\\)\u3067\u3059\u3002\u4e0b\u56f3\u306f\u3001\u3055\u307e\u3056\u307e\u306a\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u9593\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u95a2\u4fc2\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 7-12 \u00a0 \u5b8c\u5168\u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe

        \u30de\u30c3\u30d4\u30f3\u30b0\u516c\u5f0f\u306f\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30ce\u30fc\u30c9\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3068\u540c\u69d8\u306e\u5f79\u5272\u3092\u679c\u305f\u3057\u307e\u3059\u3002\u914d\u5217\u5185\u306e\u4efb\u610f\u306e\u30ce\u30fc\u30c9\u304c\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001\u30de\u30c3\u30d4\u30f3\u30b0\u516c\u5f0f\u3092\u4f7f\u7528\u3057\u3066\u305d\u306e\u5de6\uff08\u53f3\uff09\u306e\u5b50\u30ce\u30fc\u30c9\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_tree/array_representation_of_tree/#732","title":"7.3.2 \u00a0 \u4efb\u610f\u306e\u4e8c\u5206\u6728\u306e\u8868\u73fe","text":"

        \u5b8c\u5168\u4e8c\u5206\u6728\u306f\u7279\u5225\u306a\u30b1\u30fc\u30b9\u3067\u3059\u3002\u4e8c\u5206\u6728\u306e\u4e2d\u9593\u30ec\u30d9\u30eb\u306b\u306f\u591a\u304f\u306eNone\u5024\u304c\u5b58\u5728\u3059\u308b\u3053\u3068\u304c\u3088\u304f\u3042\u308a\u307e\u3059\u3002\u30ec\u30d9\u30eb\u9806\u63a2\u7d22\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u306b\u306f\u3053\u308c\u3089\u306eNone\u5024\u304c\u542b\u307e\u308c\u306a\u3044\u305f\u3081\u3001\u3053\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3060\u3051\u306b\u4f9d\u5b58\u3057\u3066None\u5024\u306e\u6570\u3068\u5206\u5e03\u3092\u63a8\u6e2c\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002\u3064\u307e\u308a\u3001\u8907\u6570\u306e\u4e8c\u5206\u6728\u69cb\u9020\u304c\u540c\u3058\u30ec\u30d9\u30eb\u9806\u63a2\u7d22\u30b7\u30fc\u30b1\u30f3\u30b9\u3068\u4e00\u81f4\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5b8c\u5168\u3067\u306a\u3044\u4e8c\u5206\u6728\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u4e0a\u8a18\u306e\u914d\u5217\u8868\u73fe\u65b9\u6cd5\u306f\u5931\u6557\u3057\u307e\u3059\u3002

        \u56f3 7-13 \u00a0 \u30ec\u30d9\u30eb\u9806\u63a2\u7d22\u30b7\u30fc\u30b1\u30f3\u30b9\u304c\u8907\u6570\u306e\u4e8c\u5206\u6728\u306e\u53ef\u80fd\u6027\u306b\u5bfe\u5fdc

        \u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3001\u30ec\u30d9\u30eb\u9806\u63a2\u7d22\u30b7\u30fc\u30b1\u30f3\u30b9\u3067\u3059\u3079\u3066\u306eNone\u5024\u3092\u660e\u793a\u7684\u306b\u66f8\u304d\u51fa\u3059\u3053\u3068\u3092\u691c\u8a0e\u3067\u304d\u307e\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u3053\u306e\u51e6\u7406\u5f8c\u3001\u30ec\u30d9\u30eb\u9806\u63a2\u7d22\u30b7\u30fc\u30b1\u30f3\u30b9\u306f\u4e8c\u5206\u6728\u3092\u4e00\u610f\u306b\u8868\u73fe\u3067\u304d\u307e\u3059\u3002\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        # \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe\n# None\u3092\u4f7f\u7528\u3057\u3066\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u8868\u73fe\ntree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]\n
        /* \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe */\n// \u6700\u5927\u6574\u6570\u5024INT_MAX\u3092\u4f7f\u7528\u3057\u3066\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u30de\u30fc\u30af\nvector<int> tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
        /* \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe */\n// Integer\u30e9\u30c3\u30d1\u30fc\u30af\u30e9\u30b9\u3092\u4f7f\u7528\u3057\u3066null\u3067\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u30de\u30fc\u30af\nInteger[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };\n
        /* \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe */\n// nullable int (int?)\u3092\u4f7f\u7528\u3057\u3066null\u3067\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u30de\u30fc\u30af\nint?[] tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
        /* \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe */\n// any\u578b\u30b9\u30e9\u30a4\u30b9\u3092\u4f7f\u7528\u3057\u3066nil\u3067\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u30de\u30fc\u30af\ntree := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15}\n
        /* \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe */\n// optional Int (Int?)\u3092\u4f7f\u7528\u3057\u3066nil\u3067\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u30de\u30fc\u30af\nlet tree: [Int?] = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]\n
        /* \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe */\n// null\u3092\u4f7f\u7528\u3057\u3066\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u8868\u73fe\nlet tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
        /* \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe */\n// null\u3092\u4f7f\u7528\u3057\u3066\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u8868\u73fe\nlet tree: (number | null)[] = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
        /* \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe */\n// nullable int (int?)\u3092\u4f7f\u7528\u3057\u3066null\u3067\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u30de\u30fc\u30af\nList<int?> tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
        /* \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe */\n// None\u3092\u4f7f\u7528\u3057\u3066\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u30de\u30fc\u30af\nlet tree = [Some(1), Some(2), Some(3), Some(4), None, Some(6), Some(7), Some(8), Some(9), None, None, Some(12), None, None, Some(15)];\n
        /* \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe */\n// \u6700\u5927int\u5024\u3092\u4f7f\u7528\u3057\u3066\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u30de\u30fc\u30af\u3001\u3057\u305f\u304c\u3063\u3066\u30ce\u30fc\u30c9\u5024\u306fINT_MAX\u3067\u3042\u3063\u3066\u306f\u306a\u3089\u306a\u3044\nint tree[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
        /* \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe */\n// null\u3092\u4f7f\u7528\u3057\u3066\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u8868\u73fe\nval tree = mutableListOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 )\n
        \n

        \u56f3 7-14 \u00a0 \u4efb\u610f\u306e\u7a2e\u985e\u306e\u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe

        \u6ce8\u76ee\u3059\u3079\u304d\u306f\u3001\u5b8c\u5099\u4e8c\u5206\u6728\u306f\u914d\u5217\u8868\u73fe\u306b\u975e\u5e38\u306b\u9069\u3057\u3066\u3044\u308b**\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002\u5b8c\u5099\u4e8c\u5206\u6728\u306e\u5b9a\u7fa9\u3092\u601d\u3044\u51fa\u3059\u3068\u3001None\u306f\u6700\u4e0b\u4f4d\u30ec\u30d9\u30eb\u3067\u306e\u307f\u3001\u304b\u3064\u53f3\u5074\u306b\u5411\u304b\u3063\u3066\u73fe\u308c\u307e\u3059\u3002**\u3064\u307e\u308a\u3001\u3059\u3079\u3066\u306eNone\u5024\u306f\u78ba\u5b9f\u306b\u30ec\u30d9\u30eb\u9806\u63a2\u7d22\u30b7\u30fc\u30b1\u30f3\u30b9\u306e\u6700\u5f8c\u306b\u73fe\u308c\u307e\u3059\u3002

        \u3053\u308c\u306f\u3001\u914d\u5217\u3092\u4f7f\u7528\u3057\u3066\u5b8c\u5099\u4e8c\u5206\u6728\u3092\u8868\u73fe\u3059\u308b\u969b\u3001\u3059\u3079\u3066\u306eNone\u5024\u306e\u683c\u7d0d\u3092\u7701\u7565\u3067\u304d\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u3001\u975e\u5e38\u306b\u4fbf\u5229\u3067\u3059\u3002\u4e0b\u56f3\u306b\u4f8b\u3092\u793a\u3057\u307e\u3059\u3002

        \u56f3 7-15 \u00a0 \u5b8c\u5099\u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe

        \u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u306f\u3001\u914d\u5217\u8868\u73fe\u306b\u57fa\u3065\u304f\u4e8c\u5206\u6728\u3092\u5b9f\u88c5\u3057\u3001\u6b21\u306e\u64cd\u4f5c\u3092\u542b\u307f\u307e\u3059\uff1a

        • \u30ce\u30fc\u30c9\u304c\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001\u305d\u306e\u5024\u3001\u5de6\uff08\u53f3\uff09\u306e\u5b50\u30ce\u30fc\u30c9\u3001\u304a\u3088\u3073\u89aa\u30ce\u30fc\u30c9\u3092\u53d6\u5f97\u3059\u308b\u3002
        • \u524d\u9806\u3001\u4e2d\u9806\u3001\u5f8c\u9806\u3001\u304a\u3088\u3073\u30ec\u30d9\u30eb\u9806\u63a2\u7d22\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u53d6\u5f97\u3059\u308b\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_binary_tree.py
        class ArrayBinaryTree:\n    \"\"\"\u914d\u5217\u30d9\u30fc\u30b9\u306e\u4e8c\u5206\u6728\u30af\u30e9\u30b9\"\"\"\n\n    def __init__(self, arr: list[int | None]):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        self._tree = list(arr)\n\n    def size(self):\n        \"\"\"\u30ea\u30b9\u30c8\u306e\u5bb9\u91cf\"\"\"\n        return len(self._tree)\n\n    def val(self, i: int) -> int | None:\n        \"\"\"\u30a4\u30f3\u30c7\u30c3\u30af\u30b9i\u306e\u30ce\u30fc\u30c9\u306e\u5024\u3092\u53d6\u5f97\"\"\"\n        # \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001None\u3092\u8fd4\u3057\u3001\u7a7a\u5e2d\u3092\u8868\u3059\n        if i < 0 or i >= self.size():\n            return None\n        return self._tree[i]\n\n    def left(self, i: int) -> int | None:\n        \"\"\"\u30a4\u30f3\u30c7\u30c3\u30af\u30b9i\u306e\u30ce\u30fc\u30c9\u306e\u5de6\u306e\u5b50\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\"\"\"\n        return 2 * i + 1\n\n    def right(self, i: int) -> int | None:\n        \"\"\"\u30a4\u30f3\u30c7\u30c3\u30af\u30b9i\u306e\u30ce\u30fc\u30c9\u306e\u53f3\u306e\u5b50\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\"\"\"\n        return 2 * i + 2\n\n    def parent(self, i: int) -> int | None:\n        \"\"\"\u30a4\u30f3\u30c7\u30c3\u30af\u30b9i\u306e\u30ce\u30fc\u30c9\u306e\u89aa\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\"\"\"\n        return (i - 1) // 2\n\n    def level_order(self) -> list[int]:\n        \"\"\"\u30ec\u30d9\u30eb\u9806\u8d70\u67fb\"\"\"\n        self.res = []\n        # \u914d\u5217\u3092\u8d70\u67fb\n        for i in range(self.size()):\n            if self.val(i) is not None:\n                self.res.append(self.val(i))\n        return self.res\n\n    def dfs(self, i: int, order: str):\n        \"\"\"\u6df1\u3055\u512a\u5148\u8d70\u67fb\"\"\"\n        if self.val(i) is None:\n            return\n        # \u524d\u9806\u8d70\u67fb\n        if order == \"pre\":\n            self.res.append(self.val(i))\n        self.dfs(self.left(i), order)\n        # \u4e2d\u9806\u8d70\u67fb\n        if order == \"in\":\n            self.res.append(self.val(i))\n        self.dfs(self.right(i), order)\n        # \u5f8c\u9806\u8d70\u67fb\n        if order == \"post\":\n            self.res.append(self.val(i))\n\n    def pre_order(self) -> list[int]:\n        \"\"\"\u524d\u9806\u8d70\u67fb\"\"\"\n        self.res = []\n        self.dfs(0, order=\"pre\")\n        return self.res\n\n    def in_order(self) -> list[int]:\n        \"\"\"\u4e2d\u9806\u8d70\u67fb\"\"\"\n        self.res = []\n        self.dfs(0, order=\"in\")\n        return self.res\n\n    def post_order(self) -> list[int]:\n        \"\"\"\u5f8c\u9806\u8d70\u67fb\"\"\"\n        self.res = []\n        self.dfs(0, order=\"post\")\n        return self.res\n
        array_binary_tree.cpp
        /* \u914d\u5217\u30d9\u30fc\u30b9\u306e\u4e8c\u5206\u6728\u30af\u30e9\u30b9 */\nclass ArrayBinaryTree {\n  public:\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    ArrayBinaryTree(vector<int> arr) {\n        tree = arr;\n    }\n\n    /* \u30ea\u30b9\u30c8\u306e\u5bb9\u91cf */\n    int size() {\n        return tree.size();\n    }\n\n    /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i \u306e\u30ce\u30fc\u30c9\u306e\u5024\u3092\u53d6\u5f97 */\n    int val(int i) {\n        // \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001INT_MAX \u3092\u8fd4\u3059\uff08null \u3092\u8868\u3059\uff09\n        if (i < 0 || i >= size())\n            return INT_MAX;\n        return tree[i];\n    }\n\n    /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i \u306e\u30ce\u30fc\u30c9\u306e\u5de6\u306e\u5b50\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\n    int left(int i) {\n        return 2 * i + 1;\n    }\n\n    /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i \u306e\u30ce\u30fc\u30c9\u306e\u53f3\u306e\u5b50\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\n    int right(int i) {\n        return 2 * i + 2;\n    }\n\n    /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i \u306e\u30ce\u30fc\u30c9\u306e\u89aa\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\n    int parent(int i) {\n        return (i - 1) / 2;\n    }\n\n    /* \u30ec\u30d9\u30eb\u9806\u8d70\u67fb */\n    vector<int> levelOrder() {\n        vector<int> res;\n        // \u914d\u5217\u3092\u8d70\u67fb\n        for (int i = 0; i < size(); i++) {\n            if (val(i) != INT_MAX)\n                res.push_back(val(i));\n        }\n        return res;\n    }\n\n    /* \u524d\u9806\u8d70\u67fb */\n    vector<int> preOrder() {\n        vector<int> res;\n        dfs(0, \"pre\", res);\n        return res;\n    }\n\n    /* \u4e2d\u9806\u8d70\u67fb */\n    vector<int> inOrder() {\n        vector<int> res;\n        dfs(0, \"in\", res);\n        return res;\n    }\n\n    /* \u5f8c\u9806\u8d70\u67fb */\n    vector<int> postOrder() {\n        vector<int> res;\n        dfs(0, \"post\", res);\n        return res;\n    }\n\n  private:\n    vector<int> tree;\n\n    /* \u6df1\u3055\u512a\u5148\u8d70\u67fb */\n    void dfs(int i, string order, vector<int> &res) {\n        // \u7a7a\u306e\u4f4d\u7f6e\u306e\u5834\u5408\u3001\u623b\u308b\n        if (val(i) == INT_MAX)\n            return;\n        // \u524d\u9806\u8d70\u67fb\n        if (order == \"pre\")\n            res.push_back(val(i));\n        dfs(left(i), order, res);\n        // \u4e2d\u9806\u8d70\u67fb\n        if (order == \"in\")\n            res.push_back(val(i));\n        dfs(right(i), order, res);\n        // \u5f8c\u9806\u8d70\u67fb\n        if (order == \"post\")\n            res.push_back(val(i));\n    }\n};\n
        array_binary_tree.java
        /* \u914d\u5217\u30d9\u30fc\u30b9\u306e\u4e8c\u5206\u6728\u30af\u30e9\u30b9 */\nclass ArrayBinaryTree {\n    private List<Integer> tree;\n\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    public ArrayBinaryTree(List<Integer> arr) {\n        tree = new ArrayList<>(arr);\n    }\n\n    /* \u30ea\u30b9\u30c8\u306e\u5bb9\u91cf */\n    public int size() {\n        return tree.size();\n    }\n\n    /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i \u306e\u30ce\u30fc\u30c9\u306e\u5024\u3092\u53d6\u5f97 */\n    public Integer val(int i) {\n        // \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001null \u3092\u8fd4\u3059\uff08\u7a7a\u306e\u4f4d\u7f6e\u3092\u8868\u3059\uff09\n        if (i < 0 || i >= size())\n            return null;\n        return tree.get(i);\n    }\n\n    /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i \u306e\u30ce\u30fc\u30c9\u306e\u5de6\u306e\u5b50\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\n    public Integer left(int i) {\n        return 2 * i + 1;\n    }\n\n    /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i \u306e\u30ce\u30fc\u30c9\u306e\u53f3\u306e\u5b50\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\n    public Integer right(int i) {\n        return 2 * i + 2;\n    }\n\n    /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i \u306e\u30ce\u30fc\u30c9\u306e\u89aa\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\n    public Integer parent(int i) {\n        return (i - 1) / 2;\n    }\n\n    /* \u30ec\u30d9\u30eb\u9806\u8d70\u67fb */\n    public List<Integer> levelOrder() {\n        List<Integer> res = new ArrayList<>();\n        // \u914d\u5217\u3092\u8d70\u67fb\n        for (int i = 0; i < size(); i++) {\n            if (val(i) != null)\n                res.add(val(i));\n        }\n        return res;\n    }\n\n    /* \u6df1\u3055\u512a\u5148\u8d70\u67fb */\n    private void dfs(Integer i, String order, List<Integer> res) {\n        // \u7a7a\u306e\u4f4d\u7f6e\u306e\u5834\u5408\u3001\u623b\u308b\n        if (val(i) == null)\n            return;\n        // \u524d\u9806\u8d70\u67fb\n        if (\"pre\".equals(order))\n            res.add(val(i));\n        dfs(left(i), order, res);\n        // \u4e2d\u9806\u8d70\u67fb\n        if (\"in\".equals(order))\n            res.add(val(i));\n        dfs(right(i), order, res);\n        // \u5f8c\u9806\u8d70\u67fb\n        if (\"post\".equals(order))\n            res.add(val(i));\n    }\n\n    /* \u524d\u9806\u8d70\u67fb */\n    public List<Integer> preOrder() {\n        List<Integer> res = new ArrayList<>();\n        dfs(0, \"pre\", res);\n        return res;\n    }\n\n    /* \u4e2d\u9806\u8d70\u67fb */\n    public List<Integer> inOrder() {\n        List<Integer> res = new ArrayList<>();\n        dfs(0, \"in\", res);\n        return res;\n    }\n\n    /* \u5f8c\u9806\u8d70\u67fb */\n    public List<Integer> postOrder() {\n        List<Integer> res = new ArrayList<>();\n        dfs(0, \"post\", res);\n        return res;\n    }\n}\n
        array_binary_tree.cs
        [class]{ArrayBinaryTree}-[func]{}\n
        array_binary_tree.go
        [class]{arrayBinaryTree}-[func]{}\n
        array_binary_tree.swift
        [class]{ArrayBinaryTree}-[func]{}\n
        array_binary_tree.js
        [class]{ArrayBinaryTree}-[func]{}\n
        array_binary_tree.ts
        [class]{ArrayBinaryTree}-[func]{}\n
        array_binary_tree.dart
        [class]{ArrayBinaryTree}-[func]{}\n
        array_binary_tree.rs
        [class]{ArrayBinaryTree}-[func]{}\n
        array_binary_tree.c
        [class]{ArrayBinaryTree}-[func]{}\n
        array_binary_tree.kt
        [class]{ArrayBinaryTree}-[func]{}\n
        array_binary_tree.rb
        [class]{ArrayBinaryTree}-[func]{}\n
        "},{"location":"chapter_tree/array_representation_of_tree/#733","title":"7.3.3 \u00a0 \u5229\u70b9\u3068\u5236\u9650","text":"

        \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe\u306b\u306f\u4ee5\u4e0b\u306e\u5229\u70b9\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u914d\u5217\u306f\u9023\u7d9a\u3057\u305f\u30e1\u30e2\u30ea\u7a7a\u9593\u306b\u683c\u7d0d\u3055\u308c\u308b\u305f\u3081\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u30d5\u30ec\u30f3\u30c9\u30ea\u30fc\u3067\u3001\u3088\u308a\u9ad8\u901f\u306a\u30a2\u30af\u30bb\u30b9\u3068\u63a2\u7d22\u304c\u53ef\u80fd\u3067\u3059\u3002
        • \u30dd\u30a4\u30f3\u30bf\u3092\u683c\u7d0d\u3059\u308b\u5fc5\u8981\u304c\u306a\u3044\u305f\u3081\u3001\u30b9\u30da\u30fc\u30b9\u3092\u7bc0\u7d04\u3067\u304d\u307e\u3059\u3002
        • \u30ce\u30fc\u30c9\u3078\u306e\u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\u304c\u53ef\u80fd\u3067\u3059\u3002

        \u3057\u304b\u3057\u3001\u914d\u5217\u8868\u73fe\u306b\u306f\u3044\u304f\u3064\u304b\u306e\u5236\u9650\u3082\u3042\u308a\u307e\u3059\uff1a

        • \u914d\u5217\u683c\u7d0d\u306b\u306f\u9023\u7d9a\u3057\u305f\u30e1\u30e2\u30ea\u7a7a\u9593\u304c\u5fc5\u8981\u306a\u305f\u3081\u3001\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u6301\u3064\u6728\u306e\u683c\u7d0d\u306b\u306f\u9069\u3057\u3066\u3044\u307e\u305b\u3093\u3002
        • \u30ce\u30fc\u30c9\u306e\u8ffd\u52a0\u3084\u524a\u9664\u306b\u306f\u914d\u5217\u306e\u633f\u5165\u3084\u524a\u9664\u64cd\u4f5c\u304c\u5fc5\u8981\u3067\u3001\u52b9\u7387\u304c\u4f4e\u304f\u306a\u308a\u307e\u3059\u3002
        • \u4e8c\u5206\u6728\u306b\u591a\u304f\u306eNone\u5024\u304c\u3042\u308b\u5834\u5408\u3001\u914d\u5217\u306b\u542b\u307e\u308c\u308b\u30ce\u30fc\u30c9\u30c7\u30fc\u30bf\u306e\u5272\u5408\u304c\u4f4e\u304f\u306a\u308a\u3001\u7a7a\u9593\u5229\u7528\u7387\u304c\u4f4e\u4e0b\u3057\u307e\u3059\u3002
        "},{"location":"chapter_tree/avl_tree/","title":"7.5 \u00a0 AVL\u6728 *","text":"

        \u300c\u4e8c\u5206\u63a2\u7d22\u6728\u300d\u306e\u7bc0\u3067\u306f\u3001\u8907\u6570\u306e\u633f\u5165\u3068\u524a\u9664\u306e\u5f8c\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u304c\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u9000\u5316\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3053\u3068\u3092\u8ff0\u3079\u307e\u3057\u305f\u3002\u3053\u306e\u3088\u3046\u306a\u5834\u5408\u3001\u3059\u3079\u3066\u306e\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(\\log n)\\)\u304b\u3089\\(O(n)\\)\u306b\u60aa\u5316\u3057\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u30012\u3064\u306e\u30ce\u30fc\u30c9\u524a\u9664\u64cd\u4f5c\u306e\u5f8c\u3001\u3053\u306e\u4e8c\u5206\u63a2\u7d22\u6728\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u9000\u5316\u3057\u307e\u3059\u3002

        \u56f3 7-24 \u00a0 \u30ce\u30fc\u30c9\u524a\u9664\u5f8c\u306eAVL\u6728\u306e\u9000\u5316

        \u4f8b\u3048\u3070\u3001\u4e0b\u56f3\u306b\u793a\u3059\u5b8c\u5168\u4e8c\u5206\u6728\u3067\u306f\u30012\u3064\u306e\u30ce\u30fc\u30c9\u3092\u633f\u5165\u3057\u305f\u5f8c\u3001\u6728\u304c\u5de6\u306b\u5927\u304d\u304f\u50be\u304d\u3001\u691c\u7d22\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3082\u60aa\u5316\u3057\u307e\u3059\u3002

        \u56f3 7-25 \u00a0 \u30ce\u30fc\u30c9\u633f\u5165\u5f8c\u306eAVL\u6728\u306e\u9000\u5316

        1962\u5e74\u3001G. M. Adelson-Velsky\u3068E. M. Landis\u304c\u8ad6\u6587\u300cAn algorithm for the organization of information\u300d\u3067AVL\u6728\u3092\u63d0\u6848\u3057\u307e\u3057\u305f\u3002\u3053\u306e\u8ad6\u6587\u3067\u306f\u3001\u30ce\u30fc\u30c9\u306e\u7d99\u7d9a\u7684\u306a\u8ffd\u52a0\u3068\u524a\u9664\u306e\u5f8c\u3082AVL\u6728\u304c\u9000\u5316\u3057\u306a\u3044\u3053\u3068\u3092\u4fdd\u8a3c\u3059\u308b\u4e00\u9023\u306e\u64cd\u4f5c\u306b\u3064\u3044\u3066\u8a73\u8ff0\u3057\u3001\u3055\u307e\u3056\u307e\u306a\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3092\\(O(\\log n)\\)\u30ec\u30d9\u30eb\u306b\u7dad\u6301\u3057\u307e\u3057\u305f\u3002\u3064\u307e\u308a\u3001\u983b\u7e41\u306a\u8ffd\u52a0\u3001\u524a\u9664\u3001\u691c\u7d22\u3001\u5909\u66f4\u304c\u5fc5\u8981\u306a\u30b7\u30ca\u30ea\u30aa\u3067\u3001AVL\u6728\u306f\u5e38\u306b\u52b9\u7387\u7684\u306a\u30c7\u30fc\u30bf\u64cd\u4f5c\u6027\u80fd\u3092\u7dad\u6301\u3067\u304d\u3001\u5927\u304d\u306a\u5fdc\u7528\u4fa1\u5024\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_tree/avl_tree/#751-avl","title":"7.5.1 \u00a0 AVL\u6728\u306e\u4e00\u822c\u7684\u306a\u7528\u8a9e","text":"

        AVL\u6728\u306f\u4e8c\u5206\u63a2\u7d22\u6728\u3067\u3042\u308a\u304b\u3064\u5e73\u8861\u4e8c\u5206\u6728\u3067\u3082\u3042\u308a\u3001\u3053\u308c\u30892\u3064\u306e\u7a2e\u985e\u306e\u4e8c\u5206\u6728\u306e\u3059\u3079\u3066\u306e\u6027\u8cea\u3092\u6e80\u305f\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u5e73\u8861\u4e8c\u5206\u63a2\u7d22\u6728\u3067\u3059\u3002

        "},{"location":"chapter_tree/avl_tree/#1","title":"1. \u00a0 \u30ce\u30fc\u30c9\u306e\u9ad8\u3055","text":"

        AVL\u6728\u306b\u95a2\u9023\u3059\u308b\u64cd\u4f5c\u3067\u306f\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u53d6\u5f97\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u30ce\u30fc\u30c9\u30af\u30e9\u30b9\u306bheight\u5909\u6570\u3092\u8ffd\u52a0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        class TreeNode:\n    \"\"\"AVL\u6728\u30ce\u30fc\u30c9\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val                 # \u30ce\u30fc\u30c9\u5024\n        self.height: int = 0                # \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n        self.left: TreeNode | None = None   # \u5de6\u306e\u5b50\u3078\u306e\u53c2\u7167\n        self.right: TreeNode | None = None  # \u53f3\u306e\u5b50\u3078\u306e\u53c2\u7167\n
        /* AVL\u6728\u30ce\u30fc\u30c9 */\nstruct TreeNode {\n    int val{};          // \u30ce\u30fc\u30c9\u5024\n    int height = 0;     // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n    TreeNode *left{};   // \u5de6\u306e\u5b50\n    TreeNode *right{};  // \u53f3\u306e\u5b50\n    TreeNode() = default;\n    explicit TreeNode(int x) : val(x){}\n};\n
        /* AVL\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode {\n    public int val;        // \u30ce\u30fc\u30c9\u5024\n    public int height;     // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n    public TreeNode left;  // \u5de6\u306e\u5b50\n    public TreeNode right; // \u53f3\u306e\u5b50\n    public TreeNode(int x) { val = x; }\n}\n
        /* AVL\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode(int? x) {\n    public int? val = x;    // \u30ce\u30fc\u30c9\u5024\n    public int height;      // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n    public TreeNode? left;  // \u5de6\u306e\u5b50\u3078\u306e\u53c2\u7167\n    public TreeNode? right; // \u53f3\u306e\u5b50\u3078\u306e\u53c2\u7167\n}\n
        /* AVL\u6728\u30ce\u30fc\u30c9 */\ntype TreeNode struct {\n    Val    int       // \u30ce\u30fc\u30c9\u5024\n    Height int       // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n    Left   *TreeNode // \u5de6\u306e\u5b50\u3078\u306e\u53c2\u7167\n    Right  *TreeNode // \u53f3\u306e\u5b50\u3078\u306e\u53c2\u7167\n}\n
        /* AVL\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode {\n    var val: Int // \u30ce\u30fc\u30c9\u5024\n    var height: Int // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n    var left: TreeNode? // \u5de6\u306e\u5b50\n    var right: TreeNode? // \u53f3\u306e\u5b50\n\n    init(x: Int) {\n        val = x\n        height = 0\n    }\n}\n
        /* AVL\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode {\n    val; // \u30ce\u30fc\u30c9\u5024\n    height; // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n    left; // \u5de6\u306e\u5b50\u30dd\u30a4\u30f3\u30bf\n    right; // \u53f3\u306e\u5b50\u30dd\u30a4\u30f3\u30bf\n    constructor(val, left, right, height) {\n        this.val = val === undefined ? 0 : val;\n        this.height = height === undefined ? 0 : height;\n        this.left = left === undefined ? null : left;\n        this.right = right === undefined ? null : right;\n    }\n}\n
        /* AVL\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode {\n    val: number;            // \u30ce\u30fc\u30c9\u5024\n    height: number;         // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n    left: TreeNode | null;  // \u5de6\u306e\u5b50\u30dd\u30a4\u30f3\u30bf\n    right: TreeNode | null; // \u53f3\u306e\u5b50\u30dd\u30a4\u30f3\u30bf\n    constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {\n        this.val = val === undefined ? 0 : val;\n        this.height = height === undefined ? 0 : height;\n        this.left = left === undefined ? null : left;\n        this.right = right === undefined ? null : right;\n    }\n}\n
        /* AVL\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode {\n  int val;         // \u30ce\u30fc\u30c9\u5024\n  int height;      // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n  TreeNode? left;  // \u5de6\u306e\u5b50\n  TreeNode? right; // \u53f3\u306e\u5b50\n  TreeNode(this.val, [this.height = 0, this.left, this.right]);\n}\n
        use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* AVL\u6728\u30ce\u30fc\u30c9 */\nstruct TreeNode {\n    val: i32,                               // \u30ce\u30fc\u30c9\u5024\n    height: i32,                            // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n    left: Option<Rc<RefCell<TreeNode>>>,    // \u5de6\u306e\u5b50\n    right: Option<Rc<RefCell<TreeNode>>>,   // \u53f3\u306e\u5b50\n}\n\nimpl TreeNode {\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    fn new(val: i32) -> Rc<RefCell<Self>> {\n        Rc::new(RefCell::new(Self {\n            val,\n            height: 0,\n            left: None,\n            right: None\n        }))\n    }\n}\n
        /* AVL\u6728\u30ce\u30fc\u30c9 */\nTreeNode struct TreeNode {\n    int val;\n    int height;\n    struct TreeNode *left;\n    struct TreeNode *right;\n} TreeNode;\n\n/* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\nTreeNode *newTreeNode(int val) {\n    TreeNode *node;\n\n    node = (TreeNode *)malloc(sizeof(TreeNode));\n    node->val = val;\n    node->height = 0;\n    node->left = NULL;\n    node->right = NULL;\n    return node;\n}\n
        /* AVL\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode(val _val: Int) {  // \u30ce\u30fc\u30c9\u5024\n    val height: Int = 0          // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n    val left: TreeNode? = null   // \u5de6\u306e\u5b50\n    val right: TreeNode? = null  // \u53f3\u306e\u5b50\n}\n
        \n

        \u300c\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u300d\u3068\u306f\u3001\u305d\u306e\u30ce\u30fc\u30c9\u304b\u3089\u6700\u3082\u9060\u3044\u8449\u30ce\u30fc\u30c9\u307e\u3067\u306e\u8ddd\u96e2\u3001\u3064\u307e\u308a\u901a\u904e\u3059\u308b\u300c\u8fba\u300d\u306e\u6570\u3092\u6307\u3057\u307e\u3059\u3002\u91cd\u8981\u306a\u306e\u306f\u3001\u8449\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u306f\\(0\\)\u3067\u3001null\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u306f\\(-1\\)\u3067\u3042\u308b\u3053\u3068\u3067\u3059\u3002\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u53d6\u5f97\u3057\u3001\u66f4\u65b0\u3059\u308b\u305f\u3081\u306e2\u3064\u306e\u30e6\u30fc\u30c6\u30a3\u30ea\u30c6\u30a3\u95a2\u6570\u3092\u4f5c\u6210\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def height(self, node: TreeNode | None) -> int:\n    \"\"\"\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u53d6\u5f97\"\"\"\n    # \u7a7a\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u306f-1\u3001\u8449\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u306f0\n    if node is not None:\n        return node.height\n    return -1\n\ndef update_height(self, node: TreeNode | None):\n    \"\"\"\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\"\"\"\n    # \u30ce\u30fc\u30c9\u306e\u9ad8\u3055 = \u6700\u3082\u9ad8\u3044\u90e8\u5206\u6728\u306e\u9ad8\u3055 + 1\n    node.height = max([self.height(node.left), self.height(node.right)]) + 1\n
        avl_tree.cpp
        /* \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u53d6\u5f97 */\nint height(TreeNode *node) {\n    // \u7a7a\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u306f-1\u3001\u8449\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u306f0\n    return node == nullptr ? -1 : node->height;\n}\n\n/* \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0 */\nvoid updateHeight(TreeNode *node) {\n    // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055 = \u6700\u3082\u9ad8\u3044\u90e8\u5206\u6728\u306e\u9ad8\u3055 + 1\n    node->height = max(height(node->left), height(node->right)) + 1;\n}\n
        avl_tree.java
        /* \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u53d6\u5f97 */\nint height(TreeNode node) {\n    // \u7a7a\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u306f -1\u3001\u8449\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u306f 0\n    return node == null ? -1 : node.height;\n}\n\n/* \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0 */\nvoid updateHeight(TreeNode node) {\n    // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u306f\u6700\u3082\u9ad8\u3044\u90e8\u5206\u6728\u306e\u9ad8\u3055 + 1\n    node.height = Math.max(height(node.left), height(node.right)) + 1;\n}\n
        avl_tree.cs
        [class]{AVLTree}-[func]{Height}\n\n[class]{AVLTree}-[func]{UpdateHeight}\n
        avl_tree.go
        [class]{aVLTree}-[func]{height}\n\n[class]{aVLTree}-[func]{updateHeight}\n
        avl_tree.swift
        [class]{AVLTree}-[func]{height}\n\n[class]{AVLTree}-[func]{updateHeight}\n
        avl_tree.js
        [class]{AVLTree}-[func]{height}\n\n[class]{AVLTree}-[func]{updateHeight}\n
        avl_tree.ts
        [class]{AVLTree}-[func]{height}\n\n[class]{AVLTree}-[func]{updateHeight}\n
        avl_tree.dart
        [class]{AVLTree}-[func]{height}\n\n[class]{AVLTree}-[func]{updateHeight}\n
        avl_tree.rs
        [class]{AVLTree}-[func]{height}\n\n[class]{AVLTree}-[func]{update_height}\n
        avl_tree.c
        [class]{}-[func]{height}\n\n[class]{}-[func]{updateHeight}\n
        avl_tree.kt
        [class]{AVLTree}-[func]{height}\n\n[class]{AVLTree}-[func]{updateHeight}\n
        avl_tree.rb
        [class]{AVLTree}-[func]{height}\n\n[class]{AVLTree}-[func]{update_height}\n
        "},{"location":"chapter_tree/avl_tree/#2","title":"2. \u00a0 \u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50","text":"

        \u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50\u306f\u3001\u305d\u306e\u30ce\u30fc\u30c9\u306e\u5de6\u90e8\u5206\u6728\u306e\u9ad8\u3055\u304b\u3089\u53f3\u90e8\u5206\u6728\u306e\u9ad8\u3055\u3092\u5f15\u3044\u305f\u5024\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u3001null\u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50\u306f\\(0\\)\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u307e\u3059\u3002\u5f8c\u3067\u4f7f\u3044\u3084\u3059\u304f\u3059\u308b\u305f\u3081\u3001\u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50\u3092\u53d6\u5f97\u3059\u308b\u6a5f\u80fd\u3082\u95a2\u6570\u306b\u30ab\u30d7\u30bb\u30eb\u5316\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def balance_factor(self, node: TreeNode | None) -> int:\n    \"\"\"\u30d0\u30e9\u30f3\u30b9\u56e0\u5b50\u3092\u53d6\u5f97\"\"\"\n    # \u7a7a\u30ce\u30fc\u30c9\u306e\u30d0\u30e9\u30f3\u30b9\u56e0\u5b50\u306f0\n    if node is None:\n        return 0\n    # \u30ce\u30fc\u30c9\u306e\u30d0\u30e9\u30f3\u30b9\u56e0\u5b50 = \u5de6\u90e8\u5206\u6728\u306e\u9ad8\u3055 - \u53f3\u90e8\u5206\u6728\u306e\u9ad8\u3055\n    return self.height(node.left) - self.height(node.right)\n
        avl_tree.cpp
        /* \u5e73\u8861\u56e0\u5b50\u3092\u53d6\u5f97 */\nint balanceFactor(TreeNode *node) {\n    // \u7a7a\u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50\u306f0\n    if (node == nullptr)\n        return 0;\n    // \u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50 = \u5de6\u90e8\u5206\u6728\u306e\u9ad8\u3055 - \u53f3\u90e8\u5206\u6728\u306e\u9ad8\u3055\n    return height(node->left) - height(node->right);\n}\n
        avl_tree.java
        /* \u5e73\u8861\u56e0\u5b50\u3092\u53d6\u5f97 */\nint balanceFactor(TreeNode node) {\n    // \u7a7a\u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50\u306f 0\n    if (node == null)\n        return 0;\n    // \u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50 = \u5de6\u90e8\u5206\u6728\u306e\u9ad8\u3055 - \u53f3\u90e8\u5206\u6728\u306e\u9ad8\u3055\n    return height(node.left) - height(node.right);\n}\n
        avl_tree.cs
        [class]{AVLTree}-[func]{BalanceFactor}\n
        avl_tree.go
        [class]{aVLTree}-[func]{balanceFactor}\n
        avl_tree.swift
        [class]{AVLTree}-[func]{balanceFactor}\n
        avl_tree.js
        [class]{AVLTree}-[func]{balanceFactor}\n
        avl_tree.ts
        [class]{AVLTree}-[func]{balanceFactor}\n
        avl_tree.dart
        [class]{AVLTree}-[func]{balanceFactor}\n
        avl_tree.rs
        [class]{AVLTree}-[func]{balance_factor}\n
        avl_tree.c
        [class]{}-[func]{balanceFactor}\n
        avl_tree.kt
        [class]{AVLTree}-[func]{balanceFactor}\n
        avl_tree.rb
        [class]{AVLTree}-[func]{balance_factor}\n

        Tip

        \u5e73\u8861\u56e0\u5b50\u3092\\(f\\)\u3068\u3059\u308b\u3068\u3001AVL\u6728\u306e\u4efb\u610f\u306e\u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50\u306f\\(-1 \\le f \\le 1\\)\u3092\u6e80\u305f\u3057\u307e\u3059\u3002

        "},{"location":"chapter_tree/avl_tree/#752-avl","title":"7.5.2 \u00a0 AVL\u6728\u306e\u56de\u8ee2","text":"

        AVL\u6728\u306e\u7279\u5fb4\u7684\u306a\u6a5f\u80fd\u306f\u300c\u56de\u8ee2\u300d\u64cd\u4f5c\u3067\u3001\u3053\u308c\u306f\u4e8c\u5206\u6728\u306e\u4e2d\u9806\u63a2\u7d22\u30b7\u30fc\u30b1\u30f3\u30b9\u306b\u5f71\u97ff\u3092\u4e0e\u3048\u308b\u3053\u3068\u306a\u304f\u3001\u4e0d\u5e73\u8861\u306a\u30ce\u30fc\u30c9\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u56de\u5fa9\u3067\u304d\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u56de\u8ee2\u64cd\u4f5c\u306f\u300c\u4e8c\u5206\u63a2\u7d22\u6728\u300d\u306e\u6027\u8cea\u3092\u7dad\u6301\u3057\u306a\u304c\u3089\u3001\u6728\u3092\u300c\u5e73\u8861\u4e8c\u5206\u6728\u300d\u306b\u623b\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u7d76\u5bfe\u5e73\u8861\u56e0\u5b50\u304c\\(> 1\\)\u306e\u30ce\u30fc\u30c9\u3092\u300c\u4e0d\u5e73\u8861\u30ce\u30fc\u30c9\u300d\u3068\u547c\u3073\u307e\u3059\u3002\u4e0d\u5e73\u8861\u306e\u30bf\u30a4\u30d7\u306b\u5fdc\u3058\u3066\u30014\u7a2e\u985e\u306e\u56de\u8ee2\u304c\u3042\u308a\u307e\u3059\uff1a\u53f3\u56de\u8ee2\u3001\u5de6\u56de\u8ee2\u3001\u53f3\u5de6\u56de\u8ee2\u3001\u5de6\u53f3\u56de\u8ee2\u3067\u3059\u3002\u4ee5\u4e0b\u3001\u3053\u308c\u3089\u306e\u56de\u8ee2\u64cd\u4f5c\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u8aac\u660e\u3057\u307e\u3059\u3002

        "},{"location":"chapter_tree/avl_tree/#1_1","title":"1. \u00a0 \u53f3\u56de\u8ee2","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u4e8c\u5206\u6728\u3067\u4e0b\u304b\u3089\u4e0a\u3078\u306e\u6700\u521d\u306e\u4e0d\u5e73\u8861\u30ce\u30fc\u30c9\u306f\u300c\u30ce\u30fc\u30c93\u300d\u3067\u3059\u3002\u3053\u306e\u4e0d\u5e73\u8861\u30ce\u30fc\u30c9\u3092\u6839\u3068\u3059\u308b\u90e8\u5206\u6728\u306b\u7126\u70b9\u3092\u5f53\u3066\u3001\u3053\u308c\u3092node\u3068\u3057\u3001\u305d\u306e\u5de6\u306e\u5b50\u3092child\u3068\u3057\u3066\u3001\u300c\u53f3\u56de\u8ee2\u300d\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002\u53f3\u56de\u8ee2\u5f8c\u3001\u90e8\u5206\u6728\u306f\u518d\u3073\u30d0\u30e9\u30f3\u30b9\u304c\u53d6\u308c\u3001\u540c\u6642\u306b\u4e8c\u5206\u63a2\u7d22\u6728\u306e\u6027\u8cea\u3082\u7dad\u6301\u3055\u308c\u307e\u3059\u3002

        <1><2><3><4>

        \u56f3 7-26 \u00a0 \u53f3\u56de\u8ee2\u306e\u624b\u9806

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001child\u30ce\u30fc\u30c9\u306b\u53f3\u306e\u5b50\uff08grand_child\u3068\u8868\u8a18\uff09\u304c\u3042\u308b\u5834\u5408\u3001\u53f3\u56de\u8ee2\u3067\u624b\u9806\u3092\u8ffd\u52a0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff1agrand_child\u3092node\u306e\u5de6\u306e\u5b50\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002

        \u56f3 7-27 \u00a0 grand_child\u304c\u3042\u308b\u53f3\u56de\u8ee2

        \u300c\u53f3\u56de\u8ee2\u300d\u306f\u6bd4\u55a9\u7684\u306a\u7528\u8a9e\u3067\u3001\u5b9f\u969b\u306b\u306f\u30ce\u30fc\u30c9\u30dd\u30a4\u30f3\u30bf\u3092\u5909\u66f4\u3059\u308b\u3053\u3068\u3067\u5b9f\u73fe\u3055\u308c\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3067\u793a\u3055\u308c\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def right_rotate(self, node: TreeNode | None) -> TreeNode | None:\n    \"\"\"\u53f3\u56de\u8ee2\u64cd\u4f5c\"\"\"\n    child = node.left\n    grand_child = child.right\n    # child\u3092\u4e2d\u5fc3\u306bnode\u3092\u53f3\u306b\u56de\u8ee2\n    child.right = node\n    node.left = grand_child\n    # \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    self.update_height(node)\n    self.update_height(child)\n    # \u56de\u8ee2\u5f8c\u306e\u90e8\u5206\u6728\u306e\u30eb\u30fc\u30c8\u3092\u8fd4\u3059\n    return child\n
        avl_tree.cpp
        /* \u53f3\u56de\u8ee2\u64cd\u4f5c */\nTreeNode *rightRotate(TreeNode *node) {\n    TreeNode *child = node->left;\n    TreeNode *grandChild = child->right;\n    // child\u3092\u4e2d\u5fc3\u306bnode\u3092\u53f3\u306b\u56de\u8ee2\n    child->right = node;\n    node->left = grandChild;\n    // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    updateHeight(node);\n    updateHeight(child);\n    // \u56de\u8ee2\u5f8c\u306e\u90e8\u5206\u6728\u306e\u30eb\u30fc\u30c8\u3092\u8fd4\u3059\n    return child;\n}\n
        avl_tree.java
        /* \u53f3\u56de\u8ee2\u64cd\u4f5c */\nTreeNode rightRotate(TreeNode node) {\n    TreeNode child = node.left;\n    TreeNode grandChild = child.right;\n    // child \u3092\u8ef8\u3068\u3057\u3066 node \u3092\u53f3\u306b\u56de\u8ee2\n    child.right = node;\n    node.left = grandChild;\n    // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    updateHeight(node);\n    updateHeight(child);\n    // \u56de\u8ee2\u5f8c\u306e\u90e8\u5206\u6728\u306e\u6839\u3092\u8fd4\u3059\n    return child;\n}\n
        avl_tree.cs
        [class]{AVLTree}-[func]{RightRotate}\n
        avl_tree.go
        [class]{aVLTree}-[func]{rightRotate}\n
        avl_tree.swift
        [class]{AVLTree}-[func]{rightRotate}\n
        avl_tree.js
        [class]{AVLTree}-[func]{rightRotate}\n
        avl_tree.ts
        [class]{AVLTree}-[func]{rightRotate}\n
        avl_tree.dart
        [class]{AVLTree}-[func]{rightRotate}\n
        avl_tree.rs
        [class]{AVLTree}-[func]{right_rotate}\n
        avl_tree.c
        [class]{}-[func]{rightRotate}\n
        avl_tree.kt
        [class]{AVLTree}-[func]{rightRotate}\n
        avl_tree.rb
        [class]{AVLTree}-[func]{right_rotate}\n
        "},{"location":"chapter_tree/avl_tree/#2_1","title":"2. \u00a0 \u5de6\u56de\u8ee2","text":"

        \u5bfe\u5fdc\u3057\u3066\u3001\u4e0a\u8a18\u306e\u4e0d\u5e73\u8861\u4e8c\u5206\u6728\u306e\u300c\u93e1\u50cf\u300d\u3092\u8003\u616e\u3059\u308b\u3068\u3001\u4e0b\u56f3\u306b\u793a\u3059\u300c\u5de6\u56de\u8ee2\u300d\u64cd\u4f5c\u3092\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u56f3 7-28 \u00a0 \u5de6\u56de\u8ee2\u64cd\u4f5c

        \u540c\u69d8\u306b\u3001\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001child\u30ce\u30fc\u30c9\u306b\u5de6\u306e\u5b50\uff08grand_child\u3068\u8868\u8a18\uff09\u304c\u3042\u308b\u5834\u5408\u3001\u5de6\u56de\u8ee2\u3067\u624b\u9806\u3092\u8ffd\u52a0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff1agrand_child\u3092node\u306e\u53f3\u306e\u5b50\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002

        \u56f3 7-29 \u00a0 grand_child\u304c\u3042\u308b\u5de6\u56de\u8ee2

        **\u53f3\u56de\u8ee2\u3068\u5de6\u56de\u8ee2\u306e\u64cd\u4f5c\u306f\u8ad6\u7406\u7684\u306b\u5bfe\u79f0\u3067\u3042\u308a\u30012\u3064\u306e\u5bfe\u79f0\u7684\u306a\u4e0d\u5e73\u8861\u30bf\u30a4\u30d7\u3092\u89e3\u6c7a\u3057\u307e\u3059**\u3053\u3068\u304c\u89b3\u5bdf\u3067\u304d\u307e\u3059\u3002\u5bfe\u79f0\u6027\u306b\u57fa\u3065\u3044\u3066\u3001\u53f3\u56de\u8ee2\u306e\u5b9f\u88c5\u30b3\u30fc\u30c9\u3067\u3059\u3079\u3066\u306eleft\u3092right\u306b\u3001\u3059\u3079\u3066\u306eright\u3092left\u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u3067\u3001\u5de6\u56de\u8ee2\u306e\u5b9f\u88c5\u30b3\u30fc\u30c9\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def left_rotate(self, node: TreeNode | None) -> TreeNode | None:\n    \"\"\"\u5de6\u56de\u8ee2\u64cd\u4f5c\"\"\"\n    child = node.right\n    grand_child = child.left\n    # child\u3092\u4e2d\u5fc3\u306bnode\u3092\u5de6\u306b\u56de\u8ee2\n    child.left = node\n    node.right = grand_child\n    # \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    self.update_height(node)\n    self.update_height(child)\n    # \u56de\u8ee2\u5f8c\u306e\u90e8\u5206\u6728\u306e\u30eb\u30fc\u30c8\u3092\u8fd4\u3059\n    return child\n
        avl_tree.cpp
        /* \u5de6\u56de\u8ee2\u64cd\u4f5c */\nTreeNode *leftRotate(TreeNode *node) {\n    TreeNode *child = node->right;\n    TreeNode *grandChild = child->left;\n    // child\u3092\u4e2d\u5fc3\u306bnode\u3092\u5de6\u306b\u56de\u8ee2\n    child->left = node;\n    node->right = grandChild;\n    // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    updateHeight(node);\n    updateHeight(child);\n    // \u56de\u8ee2\u5f8c\u306e\u90e8\u5206\u6728\u306e\u30eb\u30fc\u30c8\u3092\u8fd4\u3059\n    return child;\n}\n
        avl_tree.java
        /* \u5de6\u56de\u8ee2\u64cd\u4f5c */\nTreeNode leftRotate(TreeNode node) {\n    TreeNode child = node.right;\n    TreeNode grandChild = child.left;\n    // child \u3092\u8ef8\u3068\u3057\u3066 node \u3092\u5de6\u306b\u56de\u8ee2\n    child.left = node;\n    node.right = grandChild;\n    // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    updateHeight(node);\n    updateHeight(child);\n    // \u56de\u8ee2\u5f8c\u306e\u90e8\u5206\u6728\u306e\u6839\u3092\u8fd4\u3059\n    return child;\n}\n
        avl_tree.cs
        [class]{AVLTree}-[func]{LeftRotate}\n
        avl_tree.go
        [class]{aVLTree}-[func]{leftRotate}\n
        avl_tree.swift
        [class]{AVLTree}-[func]{leftRotate}\n
        avl_tree.js
        [class]{AVLTree}-[func]{leftRotate}\n
        avl_tree.ts
        [class]{AVLTree}-[func]{leftRotate}\n
        avl_tree.dart
        [class]{AVLTree}-[func]{leftRotate}\n
        avl_tree.rs
        [class]{AVLTree}-[func]{left_rotate}\n
        avl_tree.c
        [class]{}-[func]{leftRotate}\n
        avl_tree.kt
        [class]{AVLTree}-[func]{leftRotate}\n
        avl_tree.rb
        [class]{AVLTree}-[func]{left_rotate}\n
        "},{"location":"chapter_tree/avl_tree/#3","title":"3. \u00a0 \u5de6\u53f3\u56de\u8ee2","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u4e0d\u5e73\u8861\u30ce\u30fc\u30c93\u306e\u5834\u5408\u3001\u5de6\u56de\u8ee2\u307e\u305f\u306f\u53f3\u56de\u8ee2\u306e\u3044\u305a\u308c\u304b\u3060\u3051\u3067\u306f\u90e8\u5206\u6728\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u56de\u5fa9\u3067\u304d\u307e\u305b\u3093\u3002\u3053\u306e\u5834\u5408\u3001\u307e\u305achild\u306b\u5bfe\u3057\u3066\u300c\u5de6\u56de\u8ee2\u300d\u3092\u5b9f\u884c\u3057\u3001\u6b21\u306bnode\u306b\u5bfe\u3057\u3066\u300c\u53f3\u56de\u8ee2\u300d\u3092\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u56f3 7-30 \u00a0 \u5de6\u53f3\u56de\u8ee2

        "},{"location":"chapter_tree/avl_tree/#4","title":"4. \u00a0 \u53f3\u5de6\u56de\u8ee2","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u4e0a\u8a18\u306e\u4e0d\u5e73\u8861\u4e8c\u5206\u6728\u306e\u93e1\u50cf\u30b1\u30fc\u30b9\u3067\u306f\u3001\u307e\u305achild\u306b\u5bfe\u3057\u3066\u300c\u53f3\u56de\u8ee2\u300d\u3092\u5b9f\u884c\u3057\u3001\u6b21\u306bnode\u306b\u5bfe\u3057\u3066\u300c\u5de6\u56de\u8ee2\u300d\u3092\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u56f3 7-31 \u00a0 \u53f3\u5de6\u56de\u8ee2

        "},{"location":"chapter_tree/avl_tree/#5","title":"5. \u00a0 \u56de\u8ee2\u306e\u9078\u629e","text":"

        \u4e0b\u56f3\u306b\u793a\u30594\u7a2e\u985e\u306e\u4e0d\u5e73\u8861\u306f\u3001\u305d\u308c\u305e\u308c\u4e0a\u8a18\u3067\u8aac\u660e\u3057\u305f\u30b1\u30fc\u30b9\u306b\u5bfe\u5fdc\u3057\u3001\u53f3\u56de\u8ee2\u3001\u5de6\u53f3\u56de\u8ee2\u3001\u53f3\u5de6\u56de\u8ee2\u3001\u5de6\u56de\u8ee2\u304c\u5fc5\u8981\u3067\u3059\u3002

        \u56f3 7-32 \u00a0 AVL\u6728\u306e4\u3064\u306e\u56de\u8ee2\u30b1\u30fc\u30b9

        \u4e0b\u8868\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u4e0d\u5e73\u8861\u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50\u3068\u305d\u306e\u9ad8\u3044\u5074\u306e\u5b50\u306e\u5e73\u8861\u56e0\u5b50\u306e\u7b26\u53f7\u3092\u5224\u65ad\u3059\u308b\u3053\u3068\u3067\u3001\u4e0d\u5e73\u8861\u30ce\u30fc\u30c9\u304c\u4e0a\u8a18\u306e\u3069\u306e\u30b1\u30fc\u30b9\u306b\u5c5e\u3059\u308b\u304b\u3092\u6c7a\u5b9a\u3057\u307e\u3059\u3002

        \u8868 7-3 \u00a0 4\u3064\u306e\u56de\u8ee2\u30b1\u30fc\u30b9\u306e\u9078\u629e\u6761\u4ef6

        \u4e0d\u5e73\u8861\u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50 \u5b50\u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50 \u4f7f\u7528\u3059\u308b\u56de\u8ee2\u65b9\u6cd5 \\(> 1\\)\uff08\u5de6\u306b\u50be\u3044\u305f\u6728\uff09 \\(\\geq 0\\) \u53f3\u56de\u8ee2 \\(> 1\\)\uff08\u5de6\u306b\u50be\u3044\u305f\u6728\uff09 \\(<0\\) \u5de6\u56de\u8ee2\u3057\u3066\u304b\u3089\u53f3\u56de\u8ee2 \\(< -1\\)\uff08\u53f3\u306b\u50be\u3044\u305f\u6728\uff09 \\(\\leq 0\\) \u5de6\u56de\u8ee2 \\(< -1\\)\uff08\u53f3\u306b\u50be\u3044\u305f\u6728\uff09 \\(>0\\) \u53f3\u56de\u8ee2\u3057\u3066\u304b\u3089\u5de6\u56de\u8ee2

        \u4fbf\u5b9c\u4e0a\u3001\u56de\u8ee2\u64cd\u4f5c\u3092\u95a2\u6570\u306b\u30ab\u30d7\u30bb\u30eb\u5316\u3057\u307e\u3059\u3002\u3053\u306e\u95a2\u6570\u306b\u3088\u308a\u3001\u3055\u307e\u3056\u307e\u306a\u7a2e\u985e\u306e\u4e0d\u5e73\u8861\u306b\u5bfe\u3057\u3066\u56de\u8ee2\u3092\u5b9f\u884c\u3057\u3001\u4e0d\u5e73\u8861\u30ce\u30fc\u30c9\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u56de\u5fa9\u3067\u304d\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def rotate(self, node: TreeNode | None) -> TreeNode | None:\n    \"\"\"\u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3066\u90e8\u5206\u6728\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u5fa9\u5143\"\"\"\n    # node\u306e\u30d0\u30e9\u30f3\u30b9\u56e0\u5b50\u3092\u53d6\u5f97\n    balance_factor = self.balance_factor(node)\n    # \u5de6\u504f\u308a\u6728\n    if balance_factor > 1:\n        if self.balance_factor(node.left) >= 0:\n            # \u53f3\u56de\u8ee2\n            return self.right_rotate(node)\n        else:\n            # \u5de6\u56de\u8ee2\u3057\u3066\u304b\u3089\u53f3\u56de\u8ee2\n            node.left = self.left_rotate(node.left)\n            return self.right_rotate(node)\n    # \u53f3\u504f\u308a\u6728\n    elif balance_factor < -1:\n        if self.balance_factor(node.right) <= 0:\n            # \u5de6\u56de\u8ee2\n            return self.left_rotate(node)\n        else:\n            # \u53f3\u56de\u8ee2\u3057\u3066\u304b\u3089\u5de6\u56de\u8ee2\n            node.right = self.right_rotate(node.right)\n            return self.left_rotate(node)\n    # \u30d0\u30e9\u30f3\u30b9\u306e\u53d6\u308c\u305f\u6728\u3001\u56de\u8ee2\u4e0d\u8981\u3001\u623b\u308b\n    return node\n
        avl_tree.cpp
        /* \u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3066\u90e8\u5206\u6728\u306e\u5e73\u8861\u3092\u56de\u5fa9 */\nTreeNode *rotate(TreeNode *node) {\n    // node\u306e\u5e73\u8861\u56e0\u5b50\u3092\u53d6\u5f97\n    int _balanceFactor = balanceFactor(node);\n    // \u5de6\u306b\u50be\u3044\u305f\u6728\n    if (_balanceFactor > 1) {\n        if (balanceFactor(node->left) >= 0) {\n            // \u53f3\u56de\u8ee2\n            return rightRotate(node);\n        } else {\n            // \u5148\u306b\u5de6\u56de\u8ee2\u3001\u305d\u306e\u5f8c\u53f3\u56de\u8ee2\n            node->left = leftRotate(node->left);\n            return rightRotate(node);\n        }\n    }\n    // \u53f3\u306b\u50be\u3044\u305f\u6728\n    if (_balanceFactor < -1) {\n        if (balanceFactor(node->right) <= 0) {\n            // \u5de6\u56de\u8ee2\n            return leftRotate(node);\n        } else {\n            // \u5148\u306b\u53f3\u56de\u8ee2\u3001\u305d\u306e\u5f8c\u5de6\u56de\u8ee2\n            node->right = rightRotate(node->right);\n            return leftRotate(node);\n        }\n    }\n    // \u5e73\u8861\u306a\u6728\u3001\u56de\u8ee2\u4e0d\u8981\u3001\u305d\u306e\u307e\u307e\u623b\u308b\n    return node;\n}\n
        avl_tree.java
        /* \u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3066\u90e8\u5206\u6728\u306e\u5e73\u8861\u3092\u56de\u5fa9 */\nTreeNode rotate(TreeNode node) {\n    // node \u306e\u5e73\u8861\u56e0\u5b50\u3092\u53d6\u5f97\n    int balanceFactor = balanceFactor(node);\n    // \u5de6\u50be\u659c\u306e\u6728\n    if (balanceFactor > 1) {\n        if (balanceFactor(node.left) >= 0) {\n            // \u53f3\u56de\u8ee2\n            return rightRotate(node);\n        } else {\n            // \u5148\u306b\u5de6\u56de\u8ee2\u3001\u305d\u306e\u5f8c\u53f3\u56de\u8ee2\n            node.left = leftRotate(node.left);\n            return rightRotate(node);\n        }\n    }\n    // \u53f3\u50be\u659c\u306e\u6728\n    if (balanceFactor < -1) {\n        if (balanceFactor(node.right) <= 0) {\n            // \u5de6\u56de\u8ee2\n            return leftRotate(node);\n        } else {\n            // \u5148\u306b\u53f3\u56de\u8ee2\u3001\u305d\u306e\u5f8c\u5de6\u56de\u8ee2\n            node.right = rightRotate(node.right);\n            return leftRotate(node);\n        }\n    }\n    // \u5e73\u8861\u6728\u3001\u56de\u8ee2\u306f\u4e0d\u8981\u3001\u623b\u308b\n    return node;\n}\n
        avl_tree.cs
        [class]{AVLTree}-[func]{Rotate}\n
        avl_tree.go
        [class]{aVLTree}-[func]{rotate}\n
        avl_tree.swift
        [class]{AVLTree}-[func]{rotate}\n
        avl_tree.js
        [class]{AVLTree}-[func]{rotate}\n
        avl_tree.ts
        [class]{AVLTree}-[func]{rotate}\n
        avl_tree.dart
        [class]{AVLTree}-[func]{rotate}\n
        avl_tree.rs
        [class]{AVLTree}-[func]{rotate}\n
        avl_tree.c
        [class]{}-[func]{rotate}\n
        avl_tree.kt
        [class]{AVLTree}-[func]{rotate}\n
        avl_tree.rb
        [class]{AVLTree}-[func]{rotate}\n
        "},{"location":"chapter_tree/avl_tree/#753-avl","title":"7.5.3 \u00a0 AVL\u6728\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c","text":""},{"location":"chapter_tree/avl_tree/#1_2","title":"1. \u00a0 \u30ce\u30fc\u30c9\u306e\u633f\u5165","text":"

        AVL\u6728\u306e\u30ce\u30fc\u30c9\u633f\u5165\u64cd\u4f5c\u306f\u4e8c\u5206\u63a2\u7d22\u6728\u306e\u305d\u308c\u3068\u4f3c\u3066\u3044\u307e\u3059\u3002\u552f\u4e00\u306e\u9055\u3044\u306f\u3001AVL\u6728\u3067\u30ce\u30fc\u30c9\u3092\u633f\u5165\u3057\u305f\u5f8c\u3001\u305d\u306e\u30ce\u30fc\u30c9\u304b\u3089\u6839\u30ce\u30fc\u30c9\u307e\u3067\u306e\u30d1\u30b9\u4e0a\u306b\u4e00\u9023\u306e\u4e0d\u5e73\u8861\u30ce\u30fc\u30c9\u304c\u73fe\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3053\u3068\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u3053\u306e\u30ce\u30fc\u30c9\u304b\u3089\u59cb\u3081\u3066\u4e0a\u5411\u304d\u306b\u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3001\u3059\u3079\u3066\u306e\u4e0d\u5e73\u8861\u30ce\u30fc\u30c9\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u56de\u5fa9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def insert(self, val):\n    \"\"\"\u30ce\u30fc\u30c9\u3092\u633f\u5165\"\"\"\n    self._root = self.insert_helper(self._root, val)\n\ndef insert_helper(self, node: TreeNode | None, val: int) -> TreeNode:\n    \"\"\"\u518d\u5e30\u7684\u306b\u30ce\u30fc\u30c9\u3092\u633f\u5165\uff08\u30d8\u30eb\u30d1\u30fc\u30e1\u30bd\u30c3\u30c9\uff09\"\"\"\n    if node is None:\n        return TreeNode(val)\n    # 1. \u633f\u5165\u4f4d\u7f6e\u3092\u898b\u3064\u3051\u3066\u30ce\u30fc\u30c9\u3092\u633f\u5165\n    if val < node.val:\n        node.left = self.insert_helper(node.left, val)\n    elif val > node.val:\n        node.right = self.insert_helper(node.right, val)\n    else:\n        # \u91cd\u8907\u30ce\u30fc\u30c9\u306f\u633f\u5165\u3057\u306a\u3044\u3001\u623b\u308b\n        return node\n    # \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    self.update_height(node)\n    # 2. \u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3066\u90e8\u5206\u6728\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u5fa9\u5143\n    return self.rotate(node)\n
        avl_tree.cpp
        /* \u30ce\u30fc\u30c9\u3092\u633f\u5165 */\nvoid insert(int val) {\n    root = insertHelper(root, val);\n}\n\n/* \u30ce\u30fc\u30c9\u3092\u518d\u5e30\u7684\u306b\u633f\u5165\uff08\u30d8\u30eb\u30d1\u30fc\u30e1\u30bd\u30c3\u30c9\uff09 */\nTreeNode *insertHelper(TreeNode *node, int val) {\n    if (node == nullptr)\n        return new TreeNode(val);\n    /* 1. \u633f\u5165\u4f4d\u7f6e\u3092\u898b\u3064\u3051\u3066\u30ce\u30fc\u30c9\u3092\u633f\u5165 */\n    if (val < node->val)\n        node->left = insertHelper(node->left, val);\n    else if (val > node->val)\n        node->right = insertHelper(node->right, val);\n    else\n        return node;    // \u91cd\u8907\u30ce\u30fc\u30c9\u306f\u633f\u5165\u3057\u306a\u3044\u3001\u305d\u306e\u307e\u307e\u623b\u308b\n    updateHeight(node); // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    /* 2. \u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3066\u90e8\u5206\u6728\u306e\u5e73\u8861\u3092\u56de\u5fa9 */\n    node = rotate(node);\n    // \u90e8\u5206\u6728\u306e\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u8fd4\u3059\n    return node;\n}\n
        avl_tree.java
        /* \u30ce\u30fc\u30c9\u3092\u633f\u5165 */\nvoid insert(int val) {\n    root = insertHelper(root, val);\n}\n\n/* \u518d\u5e30\u7684\u306b\u30ce\u30fc\u30c9\u3092\u633f\u5165\uff08\u88dc\u52a9\u30e1\u30bd\u30c3\u30c9\uff09 */\nTreeNode insertHelper(TreeNode node, int val) {\n    if (node == null)\n        return new TreeNode(val);\n    /* 1. \u633f\u5165\u4f4d\u7f6e\u3092\u898b\u3064\u3051\u3066\u30ce\u30fc\u30c9\u3092\u633f\u5165 */\n    if (val < node.val)\n        node.left = insertHelper(node.left, val);\n    else if (val > node.val)\n        node.right = insertHelper(node.right, val);\n    else\n        return node; // \u91cd\u8907\u30ce\u30fc\u30c9\u306f\u633f\u5165\u3057\u306a\u3044\u3001\u623b\u308b\n    updateHeight(node); // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    /* 2. \u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3066\u90e8\u5206\u6728\u306e\u5e73\u8861\u3092\u56de\u5fa9 */\n    node = rotate(node);\n    // \u90e8\u5206\u6728\u306e\u6839\u30ce\u30fc\u30c9\u3092\u8fd4\u3059\n    return node;\n}\n
        avl_tree.cs
        [class]{AVLTree}-[func]{Insert}\n\n[class]{AVLTree}-[func]{InsertHelper}\n
        avl_tree.go
        [class]{aVLTree}-[func]{insert}\n\n[class]{aVLTree}-[func]{insertHelper}\n
        avl_tree.swift
        [class]{AVLTree}-[func]{insert}\n\n[class]{AVLTree}-[func]{insertHelper}\n
        avl_tree.js
        [class]{AVLTree}-[func]{insert}\n\n[class]{AVLTree}-[func]{insertHelper}\n
        avl_tree.ts
        [class]{AVLTree}-[func]{insert}\n\n[class]{AVLTree}-[func]{insertHelper}\n
        avl_tree.dart
        [class]{AVLTree}-[func]{insert}\n\n[class]{AVLTree}-[func]{insertHelper}\n
        avl_tree.rs
        [class]{AVLTree}-[func]{insert}\n\n[class]{AVLTree}-[func]{insert_helper}\n
        avl_tree.c
        [class]{AVLTree}-[func]{insert}\n\n[class]{}-[func]{insertHelper}\n
        avl_tree.kt
        [class]{AVLTree}-[func]{insert}\n\n[class]{AVLTree}-[func]{insertHelper}\n
        avl_tree.rb
        [class]{AVLTree}-[func]{insert}\n\n[class]{AVLTree}-[func]{insert_helper}\n
        "},{"location":"chapter_tree/avl_tree/#2_2","title":"2. \u00a0 \u30ce\u30fc\u30c9\u306e\u524a\u9664","text":"

        \u540c\u69d8\u306b\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u3067\u306e\u30ce\u30fc\u30c9\u524a\u9664\u65b9\u6cd5\u306b\u57fa\u3065\u3044\u3066\u3001\u4e0b\u304b\u3089\u4e0a\u3078\u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3066\u3059\u3079\u3066\u306e\u4e0d\u5e73\u8861\u30ce\u30fc\u30c9\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u56de\u5fa9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def remove(self, val: int):\n    \"\"\"\u30ce\u30fc\u30c9\u3092\u524a\u9664\"\"\"\n    self._root = self.remove_helper(self._root, val)\n\ndef remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None:\n    \"\"\"\u518d\u5e30\u7684\u306b\u30ce\u30fc\u30c9\u3092\u524a\u9664\uff08\u30d8\u30eb\u30d1\u30fc\u30e1\u30bd\u30c3\u30c9\uff09\"\"\"\n    if node is None:\n        return None\n    # 1. \u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u3066\u524a\u9664\n    if val < node.val:\n        node.left = self.remove_helper(node.left, val)\n    elif val > node.val:\n        node.right = self.remove_helper(node.right, val)\n    else:\n        if node.left is None or node.right is None:\n            child = node.left or node.right\n            # \u5b50\u30ce\u30fc\u30c9\u6570 = 0\u3001\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3057\u3066\u623b\u308b\n            if child is None:\n                return None\n            # \u5b50\u30ce\u30fc\u30c9\u6570 = 1\u3001\u30ce\u30fc\u30c9\u3092\u524a\u9664\n            else:\n                node = child\n        else:\n            # \u5b50\u30ce\u30fc\u30c9\u6570 = 2\u3001\u4e2d\u9806\u8d70\u67fb\u306e\u6b21\u306e\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3057\u3001\u305d\u308c\u3067\u73fe\u5728\u306e\u30ce\u30fc\u30c9\u3092\u7f6e\u304d\u63db\u3048\n            temp = node.right\n            while temp.left is not None:\n                temp = temp.left\n            node.right = self.remove_helper(node.right, temp.val)\n            node.val = temp.val\n    # \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    self.update_height(node)\n    # 2. \u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3066\u90e8\u5206\u6728\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u5fa9\u5143\n    return self.rotate(node)\n
        avl_tree.cpp
        /* \u30ce\u30fc\u30c9\u3092\u524a\u9664 */\nvoid remove(int val) {\n    root = removeHelper(root, val);\n}\n\n/* \u30ce\u30fc\u30c9\u3092\u518d\u5e30\u7684\u306b\u524a\u9664\uff08\u30d8\u30eb\u30d1\u30fc\u30e1\u30bd\u30c3\u30c9\uff09 */\nTreeNode *removeHelper(TreeNode *node, int val) {\n    if (node == nullptr)\n        return nullptr;\n    /* 1. \u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u3066\u524a\u9664 */\n    if (val < node->val)\n        node->left = removeHelper(node->left, val);\n    else if (val > node->val)\n        node->right = removeHelper(node->right, val);\n    else {\n        if (node->left == nullptr || node->right == nullptr) {\n            TreeNode *child = node->left != nullptr ? node->left : node->right;\n            // \u5b50\u30ce\u30fc\u30c9\u6570 = 0\u3001\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3057\u3066\u623b\u308b\n            if (child == nullptr) {\n                delete node;\n                return nullptr;\n            }\n            // \u5b50\u30ce\u30fc\u30c9\u6570 = 1\u3001\u30ce\u30fc\u30c9\u3092\u524a\u9664\n            else {\n                delete node;\n                node = child;\n            }\n        } else {\n            // \u5b50\u30ce\u30fc\u30c9\u6570 = 2\u3001\u4e2d\u9806\u8d70\u67fb\u306e\u6b21\u306e\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3057\u3001\u73fe\u5728\u306e\u30ce\u30fc\u30c9\u3068\u7f6e\u304d\u63db\u3048\u308b\n            TreeNode *temp = node->right;\n            while (temp->left != nullptr) {\n                temp = temp->left;\n            }\n            int tempVal = temp->val;\n            node->right = removeHelper(node->right, temp->val);\n            node->val = tempVal;\n        }\n    }\n    updateHeight(node); // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    /* 2. \u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3066\u90e8\u5206\u6728\u306e\u5e73\u8861\u3092\u56de\u5fa9 */\n    node = rotate(node);\n    // \u90e8\u5206\u6728\u306e\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u8fd4\u3059\n    return node;\n}\n
        avl_tree.java
        /* \u30ce\u30fc\u30c9\u3092\u524a\u9664 */\nvoid remove(int val) {\n    root = removeHelper(root, val);\n}\n\n/* \u518d\u5e30\u7684\u306b\u30ce\u30fc\u30c9\u3092\u524a\u9664\uff08\u88dc\u52a9\u30e1\u30bd\u30c3\u30c9\uff09 */\nTreeNode removeHelper(TreeNode node, int val) {\n    if (node == null)\n        return null;\n    /* 1. \u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u3066\u524a\u9664 */\n    if (val < node.val)\n        node.left = removeHelper(node.left, val);\n    else if (val > node.val)\n        node.right = removeHelper(node.right, val);\n    else {\n        if (node.left == null || node.right == null) {\n            TreeNode child = node.left != null ? node.left : node.right;\n            // \u5b50\u30ce\u30fc\u30c9\u6570 = 0\u3001\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3057\u3066\u623b\u308b\n            if (child == null)\n                return null;\n            // \u5b50\u30ce\u30fc\u30c9\u6570 = 1\u3001\u30ce\u30fc\u30c9\u3092\u524a\u9664\n            else\n                node = child;\n        } else {\n            // \u5b50\u30ce\u30fc\u30c9\u6570 = 2\u3001\u4e2d\u9806\u8d70\u67fb\u306e\u6b21\u306e\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3057\u3001\u73fe\u5728\u306e\u30ce\u30fc\u30c9\u3092\u305d\u308c\u3067\u7f6e\u304d\u63db\u3048\u308b\n            TreeNode temp = node.right;\n            while (temp.left != null) {\n                temp = temp.left;\n            }\n            node.right = removeHelper(node.right, temp.val);\n            node.val = temp.val;\n        }\n    }\n    updateHeight(node); // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    /* 2. \u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3066\u90e8\u5206\u6728\u306e\u5e73\u8861\u3092\u56de\u5fa9 */\n    node = rotate(node);\n    // \u90e8\u5206\u6728\u306e\u6839\u30ce\u30fc\u30c9\u3092\u8fd4\u3059\n    return node;\n}\n
        avl_tree.cs
        [class]{AVLTree}-[func]{Remove}\n\n[class]{AVLTree}-[func]{RemoveHelper}\n
        avl_tree.go
        [class]{aVLTree}-[func]{remove}\n\n[class]{aVLTree}-[func]{removeHelper}\n
        avl_tree.swift
        [class]{AVLTree}-[func]{remove}\n\n[class]{AVLTree}-[func]{removeHelper}\n
        avl_tree.js
        [class]{AVLTree}-[func]{remove}\n\n[class]{AVLTree}-[func]{removeHelper}\n
        avl_tree.ts
        [class]{AVLTree}-[func]{remove}\n\n[class]{AVLTree}-[func]{removeHelper}\n
        avl_tree.dart
        [class]{AVLTree}-[func]{remove}\n\n[class]{AVLTree}-[func]{removeHelper}\n
        avl_tree.rs
        [class]{AVLTree}-[func]{remove}\n\n[class]{AVLTree}-[func]{remove_helper}\n
        avl_tree.c
        [class]{AVLTree}-[func]{removeItem}\n\n[class]{}-[func]{removeHelper}\n
        avl_tree.kt
        [class]{AVLTree}-[func]{remove}\n\n[class]{AVLTree}-[func]{removeHelper}\n
        avl_tree.rb
        [class]{AVLTree}-[func]{remove}\n\n[class]{AVLTree}-[func]{remove_helper}\n
        "},{"location":"chapter_tree/avl_tree/#3_1","title":"3. \u00a0 \u30ce\u30fc\u30c9\u306e\u691c\u7d22","text":"

        AVL\u6728\u3067\u306e\u30ce\u30fc\u30c9\u691c\u7d22\u64cd\u4f5c\u306f\u4e8c\u5206\u63a2\u7d22\u6728\u306e\u305d\u308c\u3068\u4e00\u81f4\u3057\u3066\u304a\u308a\u3001\u3053\u3053\u3067\u306f\u8a73\u8ff0\u3057\u307e\u305b\u3093\u3002

        "},{"location":"chapter_tree/avl_tree/#754-avl","title":"7.5.4 \u00a0 AVL\u6728\u306e\u5178\u578b\u7684\u306a\u5fdc\u7528","text":"
        • \u5927\u91cf\u306e\u30c7\u30fc\u30bf\u306e\u6574\u7406\u3068\u683c\u7d0d\u306b\u4f7f\u7528\u3055\u308c\u3001\u691c\u7d22\u983b\u5ea6\u304c\u9ad8\u304f\u3001\u633f\u5165\u3068\u524a\u9664\u306e\u983b\u5ea6\u304c\u4f4e\u3044\u30b7\u30ca\u30ea\u30aa\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002
        • \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u30b7\u30b9\u30c6\u30e0\u306e\u69cb\u7bc9\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        • \u8d64\u9ed2\u6728\u3082\u4e00\u822c\u7684\u306a\u5e73\u8861\u4e8c\u5206\u63a2\u7d22\u6728\u306e\u4e00\u7a2e\u3067\u3059\u3002AVL\u6728\u3068\u6bd4\u8f03\u3057\u3066\u3001\u8d64\u9ed2\u6728\u306f\u3088\u308a\u7de9\u3044\u5e73\u8861\u6761\u4ef6\u3092\u6301\u3061\u3001\u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664\u306b\u304b\u304b\u308b\u56de\u8ee2\u6570\u304c\u5c11\u306a\u304f\u3001\u30ce\u30fc\u30c9\u306e\u8ffd\u52a0\u3068\u524a\u9664\u64cd\u4f5c\u306e\u5e73\u5747\u52b9\u7387\u304c\u9ad8\u304f\u306a\u308a\u307e\u3059\u3002
        "},{"location":"chapter_tree/binary_search_tree/","title":"7.4 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u306f\u4ee5\u4e0b\u306e\u6761\u4ef6\u3092\u6e80\u305f\u3057\u307e\u3059\u3002

        1. \u6839\u30ce\u30fc\u30c9\u306b\u3064\u3044\u3066\u3001\u5de6\u90e8\u5206\u6728\u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u306e\u5024 \\(<\\) \u6839\u30ce\u30fc\u30c9\u306e\u5024 \\(<\\) \u53f3\u90e8\u5206\u6728\u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u306e\u5024\u3002
        2. \u4efb\u610f\u306e\u30ce\u30fc\u30c9\u306e\u5de6\u3068\u53f3\u306e\u90e8\u5206\u6728\u3082\u4e8c\u5206\u63a2\u7d22\u6728\u3067\u3059\u3002\u3064\u307e\u308a\u3001\u6761\u4ef61.\u3082\u6e80\u305f\u3057\u307e\u3059\u3002

        \u56f3 7-16 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728

        "},{"location":"chapter_tree/binary_search_tree/#741","title":"7.4.1 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728\u306e\u64cd\u4f5c","text":"

        \u4e8c\u5206\u63a2\u7d22\u6728\u3092\u30af\u30e9\u30b9BinarySearchTree\u3068\u3057\u3066\u30ab\u30d7\u30bb\u30eb\u5316\u3057\u3001\u6728\u306e\u6839\u30ce\u30fc\u30c9\u3092\u6307\u3059\u30e1\u30f3\u30d0\u30fc\u5909\u6570root\u3092\u5ba3\u8a00\u3057\u307e\u3059\u3002

        "},{"location":"chapter_tree/binary_search_tree/#1","title":"1. \u00a0 \u30ce\u30fc\u30c9\u306e\u691c\u7d22","text":"

        \u30bf\u30fc\u30b2\u30c3\u30c8\u30ce\u30fc\u30c9\u5024num\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u306e\u6027\u8cea\u306b\u5f93\u3063\u3066\u691c\u7d22\u3067\u304d\u307e\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30ce\u30fc\u30c9cur\u3092\u5ba3\u8a00\u3057\u3001\u4e8c\u5206\u6728\u306e\u6839\u30ce\u30fc\u30c9root\u304b\u3089\u958b\u59cb\u3057\u3001\u30ce\u30fc\u30c9\u5024cur.val\u3068num\u306e\u30b5\u30a4\u30ba\u3092\u6bd4\u8f03\u3059\u308b\u30eb\u30fc\u30d7\u3092\u884c\u3044\u307e\u3059\u3002

        • cur.val < num\u306e\u5834\u5408\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u30ce\u30fc\u30c9\u306fcur\u306e\u53f3\u90e8\u5206\u6728\u306b\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u305f\u3081\u3001cur = cur.right\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002
        • cur.val > num\u306e\u5834\u5408\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u30ce\u30fc\u30c9\u306fcur\u306e\u5de6\u90e8\u5206\u6728\u306b\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u305f\u3081\u3001cur = cur.left\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002
        • cur.val = num\u306e\u5834\u5408\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u30ce\u30fc\u30c9\u304c\u898b\u3064\u304b\u3063\u305f\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u305f\u3081\u3001\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\u3057\u3066\u30ce\u30fc\u30c9\u3092\u8fd4\u3057\u307e\u3059\u3002
        <1><2><3><4>

        \u56f3 7-17 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728\u3067\u306e\u30ce\u30fc\u30c9\u691c\u7d22\u4f8b

        \u4e8c\u5206\u63a2\u7d22\u6728\u3067\u306e\u691c\u7d22\u64cd\u4f5c\u306f\u4e8c\u5206\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u540c\u3058\u539f\u7406\u3067\u52d5\u4f5c\u3057\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u30b1\u30fc\u30b9\u306e\u534a\u5206\u3092\u6392\u9664\u3057\u307e\u3059\u3002\u30eb\u30fc\u30d7\u6570\u306f\u6700\u5927\u3067\u4e8c\u5206\u6728\u306e\u9ad8\u3055\u3067\u3059\u3002\u4e8c\u5206\u6728\u304c\u5e73\u8861\u3057\u3066\u3044\u308b\u5834\u5408\u3001\\(O(\\log n)\\)\u306e\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u30b3\u30fc\u30c9\u4f8b\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_tree.py
        def search(self, num: int) -> TreeNode | None:\n    \"\"\"\u30ce\u30fc\u30c9\u3092\u63a2\u7d22\"\"\"\n    cur = self._root\n    # \u30eb\u30fc\u30d7\u3067\u63a2\u7d22\u3001\u8449\u30ce\u30fc\u30c9\u3092\u901a\u904e\u3057\u305f\u5f8c\u306b\u30d6\u30ec\u30fc\u30af\n    while cur is not None:\n        # \u30bf\u30fc\u30b2\u30c3\u30c8\u30ce\u30fc\u30c9\u306fcur\u306e\u53f3\u90e8\u5206\u6728\u306b\u3042\u308b\n        if cur.val < num:\n            cur = cur.right\n        # \u30bf\u30fc\u30b2\u30c3\u30c8\u30ce\u30fc\u30c9\u306fcur\u306e\u5de6\u90e8\u5206\u6728\u306b\u3042\u308b\n        elif cur.val > num:\n            cur = cur.left\n        # \u30bf\u30fc\u30b2\u30c3\u30c8\u30ce\u30fc\u30c9\u3092\u767a\u898b\u3001\u30eb\u30fc\u30d7\u3092\u30d6\u30ec\u30fc\u30af\n        else:\n            break\n    return cur\n
        binary_search_tree.cpp
        /* \u30ce\u30fc\u30c9\u3092\u691c\u7d22 */\nTreeNode *search(int num) {\n    TreeNode *cur = root;\n    // \u30eb\u30fc\u30d7\u3067\u691c\u7d22\u3001\u8449\u30ce\u30fc\u30c9\u3092\u901a\u308a\u904e\u304e\u305f\u3089\u7d42\u4e86\n    while (cur != nullptr) {\n        // \u76ee\u6a19\u30ce\u30fc\u30c9\u306fcur\u306e\u53f3\u90e8\u5206\u6728\u306b\u3042\u308b\n        if (cur->val < num)\n            cur = cur->right;\n        // \u76ee\u6a19\u30ce\u30fc\u30c9\u306fcur\u306e\u5de6\u90e8\u5206\u6728\u306b\u3042\u308b\n        else if (cur->val > num)\n            cur = cur->left;\n        // \u76ee\u6a19\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u305f\u3001\u30eb\u30fc\u30d7\u3092\u629c\u3051\u308b\n        else\n            break;\n    }\n    // \u76ee\u6a19\u30ce\u30fc\u30c9\u3092\u8fd4\u3059\n    return cur;\n}\n
        binary_search_tree.java
        /* \u30ce\u30fc\u30c9\u3092\u691c\u7d22 */\nTreeNode search(int num) {\n    TreeNode cur = root;\n    // \u30eb\u30fc\u30d7\u3067\u691c\u7d22\u3001\u8449\u30ce\u30fc\u30c9\u3092\u901a\u904e\u5f8c\u306b\u7d42\u4e86\n    while (cur != null) {\n        // \u5bfe\u8c61\u30ce\u30fc\u30c9\u306f cur \u306e\u53f3\u90e8\u5206\u6728\u306b\u3042\u308b\n        if (cur.val < num)\n            cur = cur.right;\n        // \u5bfe\u8c61\u30ce\u30fc\u30c9\u306f cur \u306e\u5de6\u90e8\u5206\u6728\u306b\u3042\u308b\n        else if (cur.val > num)\n            cur = cur.left;\n        // \u5bfe\u8c61\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u305f\u3001\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\n        else\n            break;\n    }\n    // \u5bfe\u8c61\u30ce\u30fc\u30c9\u3092\u8fd4\u3059\n    return cur;\n}\n
        binary_search_tree.cs
        [class]{BinarySearchTree}-[func]{Search}\n
        binary_search_tree.go
        [class]{binarySearchTree}-[func]{search}\n
        binary_search_tree.swift
        [class]{BinarySearchTree}-[func]{search}\n
        binary_search_tree.js
        [class]{BinarySearchTree}-[func]{search}\n
        binary_search_tree.ts
        [class]{BinarySearchTree}-[func]{search}\n
        binary_search_tree.dart
        [class]{BinarySearchTree}-[func]{search}\n
        binary_search_tree.rs
        [class]{BinarySearchTree}-[func]{search}\n
        binary_search_tree.c
        [class]{BinarySearchTree}-[func]{search}\n
        binary_search_tree.kt
        [class]{BinarySearchTree}-[func]{search}\n
        binary_search_tree.rb
        [class]{BinarySearchTree}-[func]{search}\n
        "},{"location":"chapter_tree/binary_search_tree/#2","title":"2. \u00a0 \u30ce\u30fc\u30c9\u306e\u633f\u5165","text":"

        \u633f\u5165\u3059\u308b\u8981\u7d20num\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u306e\u6027\u8cea\u300c\u5de6\u90e8\u5206\u6728 < \u6839\u30ce\u30fc\u30c9 < \u53f3\u90e8\u5206\u6728\u300d\u3092\u7dad\u6301\u3059\u308b\u305f\u3081\u3001\u633f\u5165\u64cd\u4f5c\u306f\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u9032\u884c\u3057\u307e\u3059\u3002

        1. \u633f\u5165\u4f4d\u7f6e\u3092\u898b\u3064\u3051\u308b: \u691c\u7d22\u64cd\u4f5c\u3068\u540c\u69d8\u306b\u3001\u6839\u30ce\u30fc\u30c9\u304b\u3089\u958b\u59cb\u3057\u3001\u73fe\u5728\u306e\u30ce\u30fc\u30c9\u5024\u3068num\u306e\u30b5\u30a4\u30ba\u95a2\u4fc2\u306b\u5f93\u3063\u3066\u4e0b\u5411\u304d\u306b\u30eb\u30fc\u30d7\u3057\u3001\u8449\u30ce\u30fc\u30c9\u3092\u901a\u904e\uff08None\u306b\u8d70\u67fb\uff09\u3059\u308b\u307e\u3067\u3001\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\u3057\u307e\u3059\u3002
        2. \u3053\u306e\u4f4d\u7f6e\u306b\u30ce\u30fc\u30c9\u3092\u633f\u5165: \u30ce\u30fc\u30c9num\u3092\u521d\u671f\u5316\u3057\u3001None\u304c\u3042\u3063\u305f\u5834\u6240\u306b\u914d\u7f6e\u3057\u307e\u3059\u3002

        \u56f3 7-18 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728\u3078\u306e\u30ce\u30fc\u30c9\u633f\u5165

        \u30b3\u30fc\u30c9\u5b9f\u88c5\u3067\u306f\u3001\u4ee5\u4e0b\u306e2\u70b9\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        • \u4e8c\u5206\u63a2\u7d22\u6728\u306f\u91cd\u8907\u30ce\u30fc\u30c9\u306e\u5b58\u5728\u3092\u8a31\u53ef\u3057\u307e\u305b\u3093\u3002\u305d\u3046\u3067\u306a\u3051\u308c\u3070\u3001\u305d\u306e\u5b9a\u7fa9\u306b\u9055\u53cd\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u633f\u5165\u3059\u308b\u30ce\u30fc\u30c9\u304c\u65e2\u306b\u6728\u306b\u5b58\u5728\u3059\u308b\u5834\u5408\u3001\u633f\u5165\u306f\u5b9f\u884c\u3055\u308c\u305a\u3001\u30ce\u30fc\u30c9\u306f\u76f4\u63a5\u623b\u308a\u307e\u3059\u3002
        • \u633f\u5165\u64cd\u4f5c\u3092\u5b9f\u884c\u3059\u308b\u306b\u306f\u3001\u524d\u306e\u30eb\u30fc\u30d7\u304b\u3089\u306e\u30ce\u30fc\u30c9\u3092\u4fdd\u5b58\u3059\u308b\u305f\u3081\u306b\u30ce\u30fc\u30c9pre\u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u3088\u3046\u306b\u3057\u3066\u3001None\u306b\u8d70\u67fb\u3057\u305f\u3068\u304d\u306b\u3001\u305d\u306e\u89aa\u30ce\u30fc\u30c9\u3092\u53d6\u5f97\u3067\u304d\u3001\u30ce\u30fc\u30c9\u633f\u5165\u64cd\u4f5c\u3092\u5b8c\u4e86\u3067\u304d\u307e\u3059\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_tree.py
        def insert(self, num: int):\n    \"\"\"\u30ce\u30fc\u30c9\u3092\u633f\u5165\"\"\"\n    # \u6728\u304c\u7a7a\u306e\u5834\u5408\u3001\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\n    if self._root is None:\n        self._root = TreeNode(num)\n        return\n    # \u30eb\u30fc\u30d7\u3067\u63a2\u7d22\u3001\u8449\u30ce\u30fc\u30c9\u3092\u901a\u904e\u3057\u305f\u5f8c\u306b\u30d6\u30ec\u30fc\u30af\n    cur, pre = self._root, None\n    while cur is not None:\n        # \u91cd\u8907\u30ce\u30fc\u30c9\u3092\u767a\u898b\u3057\u305f\u305f\u3081\u3001\u623b\u308b\n        if cur.val == num:\n            return\n        pre = cur\n        # \u633f\u5165\u4f4d\u7f6e\u306fcur\u306e\u53f3\u90e8\u5206\u6728\u306b\u3042\u308b\n        if cur.val < num:\n            cur = cur.right\n        # \u633f\u5165\u4f4d\u7f6e\u306fcur\u306e\u5de6\u90e8\u5206\u6728\u306b\u3042\u308b\n        else:\n            cur = cur.left\n    # \u30ce\u30fc\u30c9\u3092\u633f\u5165\n    node = TreeNode(num)\n    if pre.val < num:\n        pre.right = node\n    else:\n        pre.left = node\n
        binary_search_tree.cpp
        /* \u30ce\u30fc\u30c9\u3092\u633f\u5165 */\nvoid insert(int num) {\n    // \u6728\u304c\u7a7a\u306e\u5834\u5408\u3001\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\n    if (root == nullptr) {\n        root = new TreeNode(num);\n        return;\n    }\n    TreeNode *cur = root, *pre = nullptr;\n    // \u30eb\u30fc\u30d7\u3067\u691c\u7d22\u3001\u8449\u30ce\u30fc\u30c9\u3092\u901a\u308a\u904e\u304e\u305f\u3089\u7d42\u4e86\n    while (cur != nullptr) {\n        // \u91cd\u8907\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u305f\u5834\u5408\u3001\u623b\u308b\n        if (cur->val == num)\n            return;\n        pre = cur;\n        // \u633f\u5165\u4f4d\u7f6e\u306fcur\u306e\u53f3\u90e8\u5206\u6728\u306b\u3042\u308b\n        if (cur->val < num)\n            cur = cur->right;\n        // \u633f\u5165\u4f4d\u7f6e\u306fcur\u306e\u5de6\u90e8\u5206\u6728\u306b\u3042\u308b\n        else\n            cur = cur->left;\n    }\n    // \u30ce\u30fc\u30c9\u3092\u633f\u5165\n    TreeNode *node = new TreeNode(num);\n    if (pre->val < num)\n        pre->right = node;\n    else\n        pre->left = node;\n}\n
        binary_search_tree.java
        /* \u30ce\u30fc\u30c9\u3092\u633f\u5165 */\nvoid insert(int num) {\n    // \u6728\u304c\u7a7a\u306e\u5834\u5408\u3001\u6839\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\n    if (root == null) {\n        root = new TreeNode(num);\n        return;\n    }\n    TreeNode cur = root, pre = null;\n    // \u30eb\u30fc\u30d7\u3067\u691c\u7d22\u3001\u8449\u30ce\u30fc\u30c9\u3092\u901a\u904e\u5f8c\u306b\u7d42\u4e86\n    while (cur != null) {\n        // \u91cd\u8907\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u305f\u5834\u5408\u3001\u623b\u308b\n        if (cur.val == num)\n            return;\n        pre = cur;\n        // \u633f\u5165\u4f4d\u7f6e\u306f cur \u306e\u53f3\u90e8\u5206\u6728\u306b\u3042\u308b\n        if (cur.val < num)\n            cur = cur.right;\n        // \u633f\u5165\u4f4d\u7f6e\u306f cur \u306e\u5de6\u90e8\u5206\u6728\u306b\u3042\u308b\n        else\n            cur = cur.left;\n    }\n    // \u30ce\u30fc\u30c9\u3092\u633f\u5165\n    TreeNode node = new TreeNode(num);\n    if (pre.val < num)\n        pre.right = node;\n    else\n        pre.left = node;\n}\n
        binary_search_tree.cs
        [class]{BinarySearchTree}-[func]{Insert}\n
        binary_search_tree.go
        [class]{binarySearchTree}-[func]{insert}\n
        binary_search_tree.swift
        [class]{BinarySearchTree}-[func]{insert}\n
        binary_search_tree.js
        [class]{BinarySearchTree}-[func]{insert}\n
        binary_search_tree.ts
        [class]{BinarySearchTree}-[func]{insert}\n
        binary_search_tree.dart
        [class]{BinarySearchTree}-[func]{insert}\n
        binary_search_tree.rs
        [class]{BinarySearchTree}-[func]{insert}\n
        binary_search_tree.c
        [class]{BinarySearchTree}-[func]{insert}\n
        binary_search_tree.kt
        [class]{BinarySearchTree}-[func]{insert}\n
        binary_search_tree.rb
        [class]{BinarySearchTree}-[func]{insert}\n

        \u30ce\u30fc\u30c9\u306e\u691c\u7d22\u3068\u540c\u69d8\u306b\u3001\u30ce\u30fc\u30c9\u306e\u633f\u5165\u306b\u306f\\(O(\\log n)\\)\u306e\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        "},{"location":"chapter_tree/binary_search_tree/#3","title":"3. \u00a0 \u30ce\u30fc\u30c9\u306e\u524a\u9664","text":"

        \u307e\u305a\u3001\u4e8c\u5206\u6728\u3067\u30bf\u30fc\u30b2\u30c3\u30c8\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u3001\u305d\u308c\u3092\u524a\u9664\u3057\u307e\u3059\u3002\u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u540c\u69d8\u306b\u3001\u524a\u9664\u64cd\u4f5c\u304c\u5b8c\u4e86\u3057\u305f\u5f8c\u3082\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u306e\u6027\u8cea\u300c\u5de6\u90e8\u5206\u6728 < \u6839\u30ce\u30fc\u30c9 < \u53f3\u90e8\u5206\u6728\u300d\u304c\u6e80\u305f\u3055\u308c\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u30ce\u30fc\u30c9\u306e\u5b50\u30ce\u30fc\u30c9\u6570\u306b\u57fa\u3065\u3044\u3066\u30010\u30011\u30012\u306e3\u3064\u306e\u30b1\u30fc\u30b9\u306b\u5206\u3051\u3001\u5bfe\u5fdc\u3059\u308b\u30ce\u30fc\u30c9\u524a\u9664\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u306e\u6b21\u6570\u304c\\(0\\)\u306e\u5834\u5408\u3001\u305d\u306e\u30ce\u30fc\u30c9\u306f\u8449\u30ce\u30fc\u30c9\u3067\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u3001\u76f4\u63a5\u524a\u9664\u3067\u304d\u307e\u3059\u3002

        \u56f3 7-19 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728\u3067\u306e\u30ce\u30fc\u30c9\u524a\u9664\uff08\u6b21\u65700\uff09

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u306e\u6b21\u6570\u304c\\(1\\)\u306e\u5834\u5408\u3001\u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u3092\u305d\u306e\u5b50\u30ce\u30fc\u30c9\u3067\u7f6e\u304d\u63db\u3048\u308b\u3060\u3051\u3067\u5341\u5206\u3067\u3059\u3002

        \u56f3 7-20 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728\u3067\u306e\u30ce\u30fc\u30c9\u524a\u9664\uff08\u6b21\u65701\uff09

        \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u306e\u6b21\u6570\u304c\\(2\\)\u306e\u5834\u5408\u3001\u76f4\u63a5\u524a\u9664\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u304c\u3001\u30ce\u30fc\u30c9\u3092\u4f7f\u7528\u3057\u3066\u7f6e\u304d\u63db\u3048\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4e8c\u5206\u63a2\u7d22\u6728\u306e\u6027\u8cea\u300c\u5de6\u90e8\u5206\u6728 \\(<\\) \u6839\u30ce\u30fc\u30c9 \\(<\\) \u53f3\u90e8\u5206\u6728\u300d\u3092\u7dad\u6301\u3059\u308b\u305f\u3081\u3001\u3053\u306e\u30ce\u30fc\u30c9\u306f\u53f3\u90e8\u5206\u6728\u306e\u6700\u5c0f\u30ce\u30fc\u30c9\u307e\u305f\u306f\u5de6\u90e8\u5206\u6728\u306e\u6700\u5927\u30ce\u30fc\u30c9\u306e\u3044\u305a\u308c\u304b\u3067\u3059\u3002

        \u53f3\u90e8\u5206\u6728\u306e\u6700\u5c0f\u30ce\u30fc\u30c9\uff08\u4e2d\u9806\u8d70\u67fb\u3067\u306e\u6b21\u306e\u30ce\u30fc\u30c9\uff09\u3092\u9078\u629e\u3059\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u524a\u9664\u64cd\u4f5c\u306f\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u9032\u884c\u3057\u307e\u3059\u3002

        1. \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u306e\u300c\u4e2d\u9806\u8d70\u67fb\u30b7\u30fc\u30b1\u30f3\u30b9\u300d\u3067\u306e\u6b21\u306e\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u3001tmp\u3068\u3057\u3066\u793a\u3057\u307e\u3059\u3002
        2. \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u306e\u5024\u3092tmp\u306e\u5024\u3067\u7f6e\u304d\u63db\u3048\u3001\u6728\u5185\u3067\u30ce\u30fc\u30c9tmp\u3092\u518d\u5e30\u7684\u306b\u524a\u9664\u3057\u307e\u3059\u3002
        <1><2><3><4>

        \u56f3 7-21 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728\u3067\u306e\u30ce\u30fc\u30c9\u524a\u9664\uff08\u6b21\u65702\uff09

        \u30ce\u30fc\u30c9\u3092\u524a\u9664\u3059\u308b\u64cd\u4f5c\u3082\\(O(\\log n)\\)\u306e\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u308b\u306e\u306b\\(O(\\log n)\\)\u306e\u6642\u9593\u304c\u5fc5\u8981\u3067\u3001\u4e2d\u9806\u8d70\u67fb\u306e\u5f8c\u7d99\u30ce\u30fc\u30c9\u3092\u53d6\u5f97\u3059\u308b\u306e\u306b\\(O(\\log n)\\)\u306e\u6642\u9593\u304c\u5fc5\u8981\u3067\u3059\u3002\u30b3\u30fc\u30c9\u4f8b\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_tree.py
        def remove(self, num: int):\n    \"\"\"\u30ce\u30fc\u30c9\u3092\u524a\u9664\"\"\"\n    # \u6728\u304c\u7a7a\u306e\u5834\u5408\u3001\u623b\u308b\n    if self._root is None:\n        return\n    # \u30eb\u30fc\u30d7\u3067\u63a2\u7d22\u3001\u8449\u30ce\u30fc\u30c9\u3092\u901a\u904e\u3057\u305f\u5f8c\u306b\u30d6\u30ec\u30fc\u30af\n    cur, pre = self._root, None\n    while cur is not None:\n        # \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u3092\u767a\u898b\u3001\u30eb\u30fc\u30d7\u3092\u30d6\u30ec\u30fc\u30af\n        if cur.val == num:\n            break\n        pre = cur\n        # \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u306fcur\u306e\u53f3\u90e8\u5206\u6728\u306b\u3042\u308b\n        if cur.val < num:\n            cur = cur.right\n        # \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u306fcur\u306e\u5de6\u90e8\u5206\u6728\u306b\u3042\u308b\n        else:\n            cur = cur.left\n    # \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001\u623b\u308b\n    if cur is None:\n        return\n\n    # \u5b50\u30ce\u30fc\u30c9\u6570 = 0 \u307e\u305f\u306f 1\n    if cur.left is None or cur.right is None:\n        # \u5b50\u30ce\u30fc\u30c9\u6570 = 0/1\u306e\u5834\u5408\u3001child = null/\u305d\u306e\u5b50\u30ce\u30fc\u30c9\n        child = cur.left or cur.right\n        # \u30ce\u30fc\u30c9cur\u3092\u524a\u9664\n        if cur != self._root:\n            if pre.left == cur:\n                pre.left = child\n            else:\n                pre.right = child\n        else:\n            # \u524a\u9664\u3055\u308c\u308b\u30ce\u30fc\u30c9\u304c\u30eb\u30fc\u30c8\u306e\u5834\u5408\u3001\u30eb\u30fc\u30c8\u3092\u518d\u5272\u308a\u5f53\u3066\n            self._root = child\n    # \u5b50\u30ce\u30fc\u30c9\u6570 = 2\n    else:\n        # cur\u306e\u4e2d\u9806\u8d70\u67fb\u306e\u6b21\u306e\u30ce\u30fc\u30c9\u3092\u53d6\u5f97\n        tmp: TreeNode = cur.right\n        while tmp.left is not None:\n            tmp = tmp.left\n        # \u518d\u5e30\u7684\u306b\u30ce\u30fc\u30c9tmp\u3092\u524a\u9664\n        self.remove(tmp.val)\n        # cur\u3092tmp\u3067\u7f6e\u304d\u63db\u3048\n        cur.val = tmp.val\n
        binary_search_tree.cpp
        /* \u30ce\u30fc\u30c9\u3092\u524a\u9664 */\nvoid remove(int num) {\n    // \u6728\u304c\u7a7a\u306e\u5834\u5408\u3001\u623b\u308b\n    if (root == nullptr)\n        return;\n    TreeNode *cur = root, *pre = nullptr;\n    // \u30eb\u30fc\u30d7\u3067\u691c\u7d22\u3001\u8449\u30ce\u30fc\u30c9\u3092\u901a\u308a\u904e\u304e\u305f\u3089\u7d42\u4e86\n    while (cur != nullptr) {\n        // \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u305f\u3001\u30eb\u30fc\u30d7\u3092\u629c\u3051\u308b\n        if (cur->val == num)\n            break;\n        pre = cur;\n        // \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u306fcur\u306e\u53f3\u90e8\u5206\u6728\u306b\u3042\u308b\n        if (cur->val < num)\n            cur = cur->right;\n        // \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u306fcur\u306e\u5de6\u90e8\u5206\u6728\u306b\u3042\u308b\n        else\n            cur = cur->left;\n    }\n    // \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u304c\u306a\u3044\u5834\u5408\u3001\u623b\u308b\n    if (cur == nullptr)\n        return;\n    // \u5b50\u30ce\u30fc\u30c9\u6570 = 0 \u307e\u305f\u306f 1\n    if (cur->left == nullptr || cur->right == nullptr) {\n        // \u5b50\u30ce\u30fc\u30c9\u6570 = 0 / 1\u306e\u5834\u5408\u3001child = nullptr / \u305d\u306e\u5b50\u30ce\u30fc\u30c9\n        TreeNode *child = cur->left != nullptr ? cur->left : cur->right;\n        // \u30ce\u30fc\u30c9cur\u3092\u524a\u9664\n        if (cur != root) {\n            if (pre->left == cur)\n                pre->left = child;\n            else\n                pre->right = child;\n        } else {\n            // \u524a\u9664\u3055\u308c\u308b\u30ce\u30fc\u30c9\u304c\u30eb\u30fc\u30c8\u306e\u5834\u5408\u3001\u30eb\u30fc\u30c8\u3092\u518d\u5272\u308a\u5f53\u3066\n            root = child;\n        }\n        // \u30e1\u30e2\u30ea\u3092\u89e3\u653e\n        delete cur;\n    }\n    // \u5b50\u30ce\u30fc\u30c9\u6570 = 2\n    else {\n        // cur\u306e\u4e2d\u9806\u8d70\u67fb\u306e\u6b21\u306e\u30ce\u30fc\u30c9\u3092\u53d6\u5f97\n        TreeNode *tmp = cur->right;\n        while (tmp->left != nullptr) {\n            tmp = tmp->left;\n        }\n        int tmpVal = tmp->val;\n        // \u30ce\u30fc\u30c9tmp\u3092\u518d\u5e30\u7684\u306b\u524a\u9664\n        remove(tmp->val);\n        // cur\u3092tmp\u3067\u7f6e\u304d\u63db\u3048\n        cur->val = tmpVal;\n    }\n}\n
        binary_search_tree.java
        /* \u30ce\u30fc\u30c9\u3092\u524a\u9664 */\nvoid remove(int num) {\n    // \u6728\u304c\u7a7a\u306e\u5834\u5408\u3001\u623b\u308b\n    if (root == null)\n        return;\n    TreeNode cur = root, pre = null;\n    // \u30eb\u30fc\u30d7\u3067\u691c\u7d22\u3001\u8449\u30ce\u30fc\u30c9\u3092\u901a\u904e\u5f8c\u306b\u7d42\u4e86\n    while (cur != null) {\n        // \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u305f\u3001\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\n        if (cur.val == num)\n            break;\n        pre = cur;\n        // \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u306f cur \u306e\u53f3\u90e8\u5206\u6728\u306b\u3042\u308b\n        if (cur.val < num)\n            cur = cur.right;\n        // \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u306f cur \u306e\u5de6\u90e8\u5206\u6728\u306b\u3042\u308b\n        else\n            cur = cur.left;\n    }\n    // \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u304c\u306a\u3044\u5834\u5408\u3001\u623b\u308b\n    if (cur == null)\n        return;\n    // \u5b50\u30ce\u30fc\u30c9\u6570 = 0 \u307e\u305f\u306f 1\n    if (cur.left == null || cur.right == null) {\n        // \u5b50\u30ce\u30fc\u30c9\u6570 = 0/1 \u306e\u5834\u5408\u3001child = null/\u305d\u306e\u5b50\u30ce\u30fc\u30c9\n        TreeNode child = cur.left != null ? cur.left : cur.right;\n        // \u30ce\u30fc\u30c9 cur \u3092\u524a\u9664\n        if (cur != root) {\n            if (pre.left == cur)\n                pre.left = child;\n            else\n                pre.right = child;\n        } else {\n            // \u524a\u9664\u3055\u308c\u308b\u30ce\u30fc\u30c9\u304c\u6839\u306e\u5834\u5408\u3001\u6839\u3092\u518d\u5272\u308a\u5f53\u3066\n            root = child;\n        }\n    }\n    // \u5b50\u30ce\u30fc\u30c9\u6570 = 2\n    else {\n        // cur \u306e\u4e2d\u9806\u8d70\u67fb\u306e\u6b21\u306e\u30ce\u30fc\u30c9\u3092\u53d6\u5f97\n        TreeNode tmp = cur.right;\n        while (tmp.left != null) {\n            tmp = tmp.left;\n        }\n        // \u518d\u5e30\u7684\u306b\u30ce\u30fc\u30c9 tmp \u3092\u524a\u9664\n        remove(tmp.val);\n        // cur \u3092 tmp \u3067\u7f6e\u304d\u63db\u3048\u308b\n        cur.val = tmp.val;\n    }\n}\n
        binary_search_tree.cs
        [class]{BinarySearchTree}-[func]{Remove}\n
        binary_search_tree.go
        [class]{binarySearchTree}-[func]{remove}\n
        binary_search_tree.swift
        [class]{BinarySearchTree}-[func]{remove}\n
        binary_search_tree.js
        [class]{BinarySearchTree}-[func]{remove}\n
        binary_search_tree.ts
        [class]{BinarySearchTree}-[func]{remove}\n
        binary_search_tree.dart
        [class]{BinarySearchTree}-[func]{remove}\n
        binary_search_tree.rs
        [class]{BinarySearchTree}-[func]{remove}\n
        binary_search_tree.c
        [class]{BinarySearchTree}-[func]{removeItem}\n
        binary_search_tree.kt
        [class]{BinarySearchTree}-[func]{remove}\n
        binary_search_tree.rb
        [class]{BinarySearchTree}-[func]{remove}\n
        "},{"location":"chapter_tree/binary_search_tree/#4","title":"4. \u00a0 \u4e2d\u9806\u8d70\u67fb\u306f\u9806\u5e8f\u4ed8\u3051\u3055\u308c\u3066\u3044\u308b","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u4e8c\u5206\u6728\u306e\u4e2d\u9806\u8d70\u67fb\u306f\u300c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u300d\u306e\u8d70\u67fb\u9806\u5e8f\u306b\u5f93\u3044\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u306f\u300c\u5de6\u5b50\u30ce\u30fc\u30c9 \\(<\\) \u6839\u30ce\u30fc\u30c9 \\(<\\) \u53f3\u5b50\u30ce\u30fc\u30c9\u300d\u306e\u30b5\u30a4\u30ba\u95a2\u4fc2\u3092\u6e80\u305f\u3057\u307e\u3059\u3002

        \u3053\u308c\u306f\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u3067\u4e2d\u9806\u8d70\u67fb\u3092\u5b9f\u884c\u3059\u308b\u3068\u304d\u306b\u3001\u5e38\u306b\u6b21\u306b\u5c0f\u3055\u3044\u30ce\u30fc\u30c9\u304c\u6700\u521d\u306b\u8d70\u67fb\u3055\u308c\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u3001\u91cd\u8981\u306a\u6027\u8cea\u306b\u3064\u306a\u304c\u308a\u307e\u3059\uff1a\u4e8c\u5206\u63a2\u7d22\u6728\u306e\u4e2d\u9806\u8d70\u67fb\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u306f\u6607\u9806\u3067\u3059\u3002

        \u4e2d\u9806\u8d70\u67fb\u306e\u6607\u9806\u6027\u8cea\u3092\u4f7f\u7528\u3057\u3066\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u3067\u9806\u5e8f\u4ed8\u3051\u3055\u308c\u305f\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u306b\u306f\\(O(n)\\)\u306e\u6642\u9593\u306e\u307f\u304c\u5fc5\u8981\u3067\u3001\u8ffd\u52a0\u306e\u30bd\u30fc\u30c8\u64cd\u4f5c\u306f\u4e0d\u8981\u3067\u3042\u308a\u3001\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3059\u3002

        \u56f3 7-22 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728\u306e\u4e2d\u9806\u8d70\u67fb\u30b7\u30fc\u30b1\u30f3\u30b9

        "},{"location":"chapter_tree/binary_search_tree/#742","title":"7.4.2 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728\u306e\u52b9\u7387","text":"

        \u30c7\u30fc\u30bf\u306e\u30bb\u30c3\u30c8\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u914d\u5217\u307e\u305f\u306f\u4e8c\u5206\u63a2\u7d22\u6728\u3092\u4f7f\u7528\u3057\u3066\u683c\u7d0d\u3059\u308b\u3053\u3068\u3092\u691c\u8a0e\u3057\u307e\u3059\u3002\u4e0b\u306e\u8868\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u306e\u3059\u3079\u3066\u306e\u64cd\u4f5c\u306f\u5bfe\u6570\u6642\u9593\u8a08\u7b97\u91cf\u3092\u6301\u3061\u3001\u5b89\u5b9a\u3057\u3066\u52b9\u7387\u7684\u3067\u3059\u3002\u914d\u5217\u306f\u3001\u983b\u7e41\u306a\u8ffd\u52a0\u3068\u691c\u7d22\u3084\u524a\u9664\u306e\u983b\u5ea6\u304c\u5c11\u306a\u3044\u30b7\u30ca\u30ea\u30aa\u3067\u306e\u307f\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u3088\u308a\u3082\u52b9\u7387\u7684\u3067\u3059\u3002

        \u8868 7-2 \u00a0 \u914d\u5217\u3068\u63a2\u7d22\u6728\u306e\u52b9\u7387\u6bd4\u8f03

        \u672a\u30bd\u30fc\u30c8\u914d\u5217 \u4e8c\u5206\u63a2\u7d22\u6728 \u8981\u7d20\u306e\u691c\u7d22 \\(O(n)\\) \\(O(\\log n)\\) \u8981\u7d20\u306e\u633f\u5165 \\(O(1)\\) \\(O(\\log n)\\) \u8981\u7d20\u306e\u524a\u9664 \\(O(n)\\) \\(O(\\log n)\\)

        \u7406\u60f3\u7684\u306b\u306f\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u306f\u300c\u5e73\u8861\u300d\u3057\u3066\u304a\u308a\u3001\u4efb\u610f\u306e\u30ce\u30fc\u30c9\u3092\\(\\log n\\)\u30eb\u30fc\u30d7\u5185\u3067\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u3057\u304b\u3057\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u3067\u7d99\u7d9a\u7684\u306b\u30ce\u30fc\u30c9\u3092\u633f\u5165\u304a\u3088\u3073\u524a\u9664\u3059\u308b\u3068\u3001\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u9000\u5316\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u3055\u307e\u3056\u307e\u306a\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3082\\(O(n)\\)\u306b\u60aa\u5316\u3057\u307e\u3059\u3002

        \u56f3 7-23 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728\u306e\u9000\u5316

        "},{"location":"chapter_tree/binary_search_tree/#743","title":"7.4.3 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728\u306e\u4e00\u822c\u7684\u306a\u5fdc\u7528","text":"
        • \u30b7\u30b9\u30c6\u30e0\u3067\u306e\u591a\u30ec\u30d9\u30eb\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3068\u3057\u3066\u4f7f\u7528\u3055\u308c\u3001\u52b9\u7387\u7684\u306a\u691c\u7d22\u3001\u633f\u5165\u3001\u524a\u9664\u64cd\u4f5c\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002
        • \u7279\u5b9a\u306e\u691c\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u57fa\u76e4\u3068\u306a\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u3057\u3066\u6a5f\u80fd\u3057\u307e\u3059\u3002
        • \u30c7\u30fc\u30bf\u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u683c\u7d0d\u3057\u3066\u3001\u305d\u306e\u9806\u5e8f\u4ed8\u3051\u3055\u308c\u305f\u72b6\u614b\u3092\u7dad\u6301\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        "},{"location":"chapter_tree/binary_tree/","title":"7.1 \u00a0 \u4e8c\u5206\u6728","text":"

        \u4e8c\u5206\u6728\u306f\u3001\u7956\u5148\u3068\u5b50\u5b6b\u306e\u9593\u306e\u968e\u5c64\u95a2\u4fc2\u3092\u8868\u73fe\u3057\u3001\u300c\u4e8c\u3064\u306b\u5206\u5272\u3059\u308b\u300d\u5206\u5272\u7d71\u6cbb\u6cd5\u306e\u8ad6\u7406\u3092\u4f53\u73fe\u3059\u308b\u975e\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3059\u3002\u9023\u7d50\u30ea\u30b9\u30c8\u3068\u540c\u69d8\u306b\u3001\u4e8c\u5206\u6728\u306e\u57fa\u672c\u5358\u4f4d\u306f\u30ce\u30fc\u30c9\u3067\u3042\u308a\u3001\u5404\u30ce\u30fc\u30c9\u306f\u5024\u3001\u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\u3001\u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\u3092\u542b\u307f\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        class TreeNode:\n    \"\"\"\u4e8c\u5206\u6728\u30ce\u30fc\u30c9\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val                # \u30ce\u30fc\u30c9\u5024\n        self.left: TreeNode | None = None  # \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n        self.right: TreeNode | None = None # \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n
        /* \u4e8c\u5206\u6728\u30ce\u30fc\u30c9 */\nstruct TreeNode {\n    int val;          // \u30ce\u30fc\u30c9\u5024\n    TreeNode *left;   // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    TreeNode *right;  // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n};\n
        /* \u4e8c\u5206\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode {\n    int val;         // \u30ce\u30fc\u30c9\u5024\n    TreeNode left;   // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    TreeNode right;  // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    TreeNode(int x) { val = x; }\n}\n
        /* \u4e8c\u5206\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode(int? x) {\n    public int? val = x;    // \u30ce\u30fc\u30c9\u5024\n    public TreeNode? left;  // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    public TreeNode? right; // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n}\n
        /* \u4e8c\u5206\u6728\u30ce\u30fc\u30c9 */\ntype TreeNode struct {\n    Val   int\n    Left  *TreeNode\n    Right *TreeNode\n}\n/* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\nfunc NewTreeNode(v int) *TreeNode {\n    return &TreeNode{\n        Left:  nil, // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n        Right: nil, // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n        Val:   v,   // \u30ce\u30fc\u30c9\u5024\n    }\n}\n
        /* \u4e8c\u5206\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode {\n    var val: Int // \u30ce\u30fc\u30c9\u5024\n    var left: TreeNode? // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    var right: TreeNode? // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n\n    init(x: Int) {\n        val = x\n    }\n}\n
        /* \u4e8c\u5206\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode {\n    val; // \u30ce\u30fc\u30c9\u5024\n    left; // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    right; // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    constructor(val, left, right) {\n        this.val = val === undefined ? 0 : val;\n        this.left = left === undefined ? null : left;\n        this.right = right === undefined ? null : right;\n    }\n}\n
        /* \u4e8c\u5206\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode {\n    val: number;\n    left: TreeNode | null;\n    right: TreeNode | null;\n\n    constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {\n        this.val = val === undefined ? 0 : val; // \u30ce\u30fc\u30c9\u5024\n        this.left = left === undefined ? null : left; // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n        this.right = right === undefined ? null : right; // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    }\n}\n
        /* \u4e8c\u5206\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode {\n  int val;         // \u30ce\u30fc\u30c9\u5024\n  TreeNode? left;  // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n  TreeNode? right; // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n  TreeNode(this.val, [this.left, this.right]);\n}\n
        use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u4e8c\u5206\u6728\u30ce\u30fc\u30c9 */\nstruct TreeNode {\n    val: i32,                               // \u30ce\u30fc\u30c9\u5024\n    left: Option<Rc<RefCell<TreeNode>>>,    // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    right: Option<Rc<RefCell<TreeNode>>>,   // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n}\n\nimpl TreeNode {\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    fn new(val: i32) -> Rc<RefCell<Self>> {\n        Rc::new(RefCell::new(Self {\n            val,\n            left: None,\n            right: None\n        }))\n    }\n}\n
        /* \u4e8c\u5206\u6728\u30ce\u30fc\u30c9 */\ntypedef struct TreeNode {\n    int val;                // \u30ce\u30fc\u30c9\u5024\n    int height;             // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n    struct TreeNode *left;  // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    struct TreeNode *right; // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n} TreeNode;\n\n/* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\nTreeNode *newTreeNode(int val) {\n    TreeNode *node;\n\n    node = (TreeNode *)malloc(sizeof(TreeNode));\n    node->val = val;\n    node->height = 0;\n    node->left = NULL;\n    node->right = NULL;\n    return node;\n}\n
        /* \u4e8c\u5206\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode(val _val: Int) {  // \u30ce\u30fc\u30c9\u5024\n    val left: TreeNode? = null   // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    val right: TreeNode? = null  // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n}\n
        \n

        \u5404\u30ce\u30fc\u30c9\u306f2\u3064\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u6301\u3061\u3001\u305d\u308c\u305e\u308c\u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3068\u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3092\u6307\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u30ce\u30fc\u30c9\u306f\u3001\u3053\u308c\u30892\u3064\u306e\u5b50\u30ce\u30fc\u30c9\u306e\u89aa\u30ce\u30fc\u30c9\u3068\u547c\u3070\u308c\u307e\u3059\u3002\u4e8c\u5206\u6728\u306e\u30ce\u30fc\u30c9\u304c\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001\u3053\u306e\u30ce\u30fc\u30c9\u306e\u5de6\u306e\u5b50\u3068\u305d\u306e\u4e0b\u306b\u3042\u308b\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3067\u5f62\u6210\u3055\u308c\u308b\u6728\u3092\u3001\u3053\u306e\u30ce\u30fc\u30c9\u306e\u5de6\u90e8\u5206\u6728\u3068\u547c\u3073\u307e\u3059\u3002\u540c\u69d8\u306b\u3001\u53f3\u90e8\u5206\u6728\u3082\u5b9a\u7fa9\u3067\u304d\u307e\u3059\u3002

        \u4e8c\u5206\u6728\u3067\u306f\u3001\u8449\u30ce\u30fc\u30c9\u3092\u9664\u3044\u3066\u3001\u4ed6\u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u306f\u5b50\u30ce\u30fc\u30c9\u3068\u7a7a\u3067\u306a\u3044\u90e8\u5206\u6728\u3092\u542b\u307f\u307e\u3059\u3002 \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u300c\u30ce\u30fc\u30c92\u300d\u3092\u89aa\u30ce\u30fc\u30c9\u3068\u3057\u3066\u898b\u308b\u3068\u3001\u305d\u306e\u5de6\u3068\u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u306f\u305d\u308c\u305e\u308c\u300c\u30ce\u30fc\u30c94\u300d\u3068\u300c\u30ce\u30fc\u30c95\u300d\u3067\u3059\u3002\u5de6\u90e8\u5206\u6728\u306f\u300c\u30ce\u30fc\u30c94\u300d\u3068\u305d\u306e\u4e0b\u306b\u3042\u308b\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3067\u5f62\u6210\u3055\u308c\u3001\u53f3\u90e8\u5206\u6728\u306f\u300c\u30ce\u30fc\u30c95\u300d\u3068\u305d\u306e\u4e0b\u306b\u3042\u308b\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3067\u5f62\u6210\u3055\u308c\u307e\u3059\u3002

        \u56f3 7-1 \u00a0 \u89aa\u30ce\u30fc\u30c9\u3001\u5b50\u30ce\u30fc\u30c9\u3001\u90e8\u5206\u6728

        "},{"location":"chapter_tree/binary_tree/#711","title":"7.1.1 \u00a0 \u4e8c\u5206\u6728\u306e\u4e00\u822c\u7684\u306a\u7528\u8a9e","text":"

        \u4e8c\u5206\u6728\u3067\u3088\u304f\u4f7f\u7528\u3055\u308c\u308b\u7528\u8a9e\u3092\u4e0b\u56f3\u306b\u793a\u3057\u307e\u3059\u3002

        • \u6839\u30ce\u30fc\u30c9\uff1a\u4e8c\u5206\u6728\u306e\u6700\u4e0a\u4f4d\u30ec\u30d9\u30eb\u306b\u3042\u308b\u30ce\u30fc\u30c9\u3067\u3001\u89aa\u30ce\u30fc\u30c9\u3092\u6301\u3061\u307e\u305b\u3093\u3002
        • \u8449\u30ce\u30fc\u30c9\uff1a\u5b50\u30ce\u30fc\u30c9\u3092\u6301\u305f\u306a\u3044\u30ce\u30fc\u30c9\u3067\u3001\u4e21\u65b9\u306e\u30dd\u30a4\u30f3\u30bf\u304cNone\u3092\u6307\u3057\u3066\u3044\u307e\u3059\u3002
        • \u8fba\uff1a2\u3064\u306e\u30ce\u30fc\u30c9\u3092\u7d50\u3076\u7dda\u5206\u3067\u3001\u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u8868\u73fe\u3057\u307e\u3059\u3002
        • \u30ce\u30fc\u30c9\u306e\u30ec\u30d9\u30eb\uff1a\u4e0a\u304b\u3089\u4e0b\u306b\u5411\u304b\u3063\u3066\u5897\u52a0\u3057\u3001\u6839\u30ce\u30fc\u30c9\u304c\u30ec\u30d9\u30eb1\u3067\u3059\u3002
        • \u30ce\u30fc\u30c9\u306e\u6b21\u6570\uff1a\u30ce\u30fc\u30c9\u304c\u6301\u3064\u5b50\u30ce\u30fc\u30c9\u306e\u6570\u3067\u3059\u3002\u4e8c\u5206\u6728\u3067\u306f\u3001\u6b21\u6570\u306f0\u30011\u3001\u307e\u305f\u306f2\u306b\u306a\u308a\u307e\u3059\u3002
        • \u4e8c\u5206\u6728\u306e\u9ad8\u3055\uff1a\u6839\u30ce\u30fc\u30c9\u304b\u3089\u6700\u3082\u9060\u3044\u8449\u30ce\u30fc\u30c9\u307e\u3067\u306e\u8fba\u306e\u6570\u3067\u3059\u3002
        • \u30ce\u30fc\u30c9\u306e\u6df1\u3055\uff1a\u6839\u30ce\u30fc\u30c9\u304b\u3089\u305d\u306e\u30ce\u30fc\u30c9\u307e\u3067\u306e\u8fba\u306e\u6570\u3067\u3059\u3002
        • \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\uff1a\u6700\u3082\u9060\u3044\u8449\u30ce\u30fc\u30c9\u304b\u3089\u305d\u306e\u30ce\u30fc\u30c9\u307e\u3067\u306e\u8fba\u306e\u6570\u3067\u3059\u3002

        \u56f3 7-2 \u00a0 \u4e8c\u5206\u6728\u306e\u4e00\u822c\u7684\u306a\u7528\u8a9e

        Tip

        \u300c\u9ad8\u3055\u300d\u3068\u300c\u6df1\u3055\u300d\u306f\u901a\u5e38\u300c\u901a\u904e\u3059\u308b\u8fba\u306e\u6570\u300d\u3068\u3057\u3066\u5b9a\u7fa9\u3057\u307e\u3059\u304c\u3001\u4e00\u90e8\u306e\u554f\u984c\u3084\u6559\u79d1\u66f8\u3067\u306f\u300c\u901a\u904e\u3059\u308b\u30ce\u30fc\u30c9\u306e\u6570\u300d\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u5834\u5408\u3001\u9ad8\u3055\u3068\u6df1\u3055\u306e\u4e21\u65b9\u30921\u3060\u3051\u5897\u3084\u3059\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_tree/binary_tree/#712","title":"7.1.2 \u00a0 \u4e8c\u5206\u6728\u306e\u57fa\u672c\u64cd\u4f5c","text":""},{"location":"chapter_tree/binary_tree/#1","title":"1. \u00a0 \u4e8c\u5206\u6728\u306e\u521d\u671f\u5316","text":"

        \u9023\u7d50\u30ea\u30b9\u30c8\u3068\u540c\u69d8\u306b\u3001\u4e8c\u5206\u6728\u306e\u521d\u671f\u5316\u3067\u306f\u3001\u307e\u305a\u30ce\u30fc\u30c9\u3092\u4f5c\u6210\u3057\u3001\u6b21\u306b\u305d\u308c\u3089\u306e\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u78ba\u7acb\u3057\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree.py
        # \u4e8c\u5206\u6728\u306e\u521d\u671f\u5316\n# \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nn1 = TreeNode(val=1)\nn2 = TreeNode(val=2)\nn3 = TreeNode(val=3)\nn4 = TreeNode(val=4)\nn5 = TreeNode(val=5)\n# \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
        binary_tree.cpp
        /* \u4e8c\u5206\u6728\u306e\u521d\u671f\u5316 */\n// \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nTreeNode* n1 = new TreeNode(1);\nTreeNode* n2 = new TreeNode(2);\nTreeNode* n3 = new TreeNode(3);\nTreeNode* n4 = new TreeNode(4);\nTreeNode* n5 = new TreeNode(5);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
        binary_tree.java
        // \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
        binary_tree.cs
        /* \u4e8c\u5206\u6728\u306e\u521d\u671f\u5316 */\n// \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nTreeNode n1 = new(1);\nTreeNode n2 = new(2);\nTreeNode n3 = new(3);\nTreeNode n4 = new(4);\nTreeNode n5 = new(5);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
        binary_tree.go
        /* \u4e8c\u5206\u6728\u306e\u521d\u671f\u5316 */\n// \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nn1 := NewTreeNode(1)\nn2 := NewTreeNode(2)\nn3 := NewTreeNode(3)\nn4 := NewTreeNode(4)\nn5 := NewTreeNode(5)\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1.Left = n2\nn1.Right = n3\nn2.Left = n4\nn2.Right = n5\n
        binary_tree.swift
        // \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nlet n1 = TreeNode(x: 1)\nlet n2 = TreeNode(x: 2)\nlet n3 = TreeNode(x: 3)\nlet n4 = TreeNode(x: 4)\nlet n5 = TreeNode(x: 5)\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
        binary_tree.js
        /* \u4e8c\u5206\u6728\u306e\u521d\u671f\u5316 */\n// \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nlet n1 = new TreeNode(1),\n    n2 = new TreeNode(2),\n    n3 = new TreeNode(3),\n    n4 = new TreeNode(4),\n    n5 = new TreeNode(5);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
        binary_tree.ts
        /* \u4e8c\u5206\u6728\u306e\u521d\u671f\u5316 */\n// \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nlet n1 = new TreeNode(1),\n    n2 = new TreeNode(2),\n    n3 = new TreeNode(3),\n    n4 = new TreeNode(4),\n    n5 = new TreeNode(5);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
        binary_tree.dart
        /* \u4e8c\u5206\u6728\u306e\u521d\u671f\u5316 */\n// \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
        binary_tree.rs
        // \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nlet n1 = TreeNode::new(1);\nlet n2 = TreeNode::new(2);\nlet n3 = TreeNode::new(3);\nlet n4 = TreeNode::new(4);\nlet n5 = TreeNode::new(5);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1.borrow_mut().left = Some(n2.clone());\nn1.borrow_mut().right = Some(n3);\nn2.borrow_mut().left = Some(n4);\nn2.borrow_mut().right = Some(n5);\n
        binary_tree.c
        /* \u4e8c\u5206\u6728\u306e\u521d\u671f\u5316 */\n// \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nTreeNode *n1 = newTreeNode(1);\nTreeNode *n2 = newTreeNode(2);\nTreeNode *n3 = newTreeNode(3);\nTreeNode *n4 = newTreeNode(4);\nTreeNode *n5 = newTreeNode(5);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
        binary_tree.kt
        // \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nval n1 = TreeNode(1)\nval n2 = TreeNode(2)\nval n3 = TreeNode(3)\nval n4 = TreeNode(4)\nval n5 = TreeNode(5)\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
        binary_tree.rb
        \n
        "},{"location":"chapter_tree/binary_tree/#2","title":"2. \u00a0 \u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664","text":"

        \u9023\u7d50\u30ea\u30b9\u30c8\u3068\u540c\u69d8\u306b\u3001\u4e8c\u5206\u6728\u3067\u306e\u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664\u306f\u30dd\u30a4\u30f3\u30bf\u3092\u5909\u66f4\u3059\u308b\u3053\u3068\u3067\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002\u4e0b\u56f3\u306b\u4f8b\u3092\u793a\u3057\u307e\u3059\u3002

        \u56f3 7-3 \u00a0 \u4e8c\u5206\u6728\u3067\u306e\u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree.py
        # \u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664\np = TreeNode(0)\n# n1 -> n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\nn1.left = p\np.left = n2\n# \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1.left = n2\n
        binary_tree.cpp
        /* \u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664 */\nTreeNode* P = new TreeNode(0);\n// n1\u3068n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\nn1->left = P;\nP->left = n2;\n// \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1->left = n2;\n
        binary_tree.java
        TreeNode P = new TreeNode(0);\n// n1\u3068n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\nn1.left = P;\nP.left = n2;\n// \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1.left = n2;\n
        binary_tree.cs
        /* \u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664 */\nTreeNode P = new(0);\n// n1\u3068n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\nn1.left = P;\nP.left = n2;\n// \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1.left = n2;\n
        binary_tree.go
        /* \u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664 */\n// n1\u3068n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\np := NewTreeNode(0)\nn1.Left = p\np.Left = n2\n// \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1.Left = n2\n
        binary_tree.swift
        let P = TreeNode(x: 0)\n// n1\u3068n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\nn1.left = P\nP.left = n2\n// \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1.left = n2\n
        binary_tree.js
        /* \u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664 */\nlet P = new TreeNode(0);\n// n1\u3068n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\nn1.left = P;\nP.left = n2;\n// \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1.left = n2;\n
        binary_tree.ts
        /* \u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664 */\nconst P = new TreeNode(0);\n// n1\u3068n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\nn1.left = P;\nP.left = n2;\n// \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1.left = n2;\n
        binary_tree.dart
        /* \u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664 */\nTreeNode P = new TreeNode(0);\n// n1\u3068n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\nn1.left = P;\nP.left = n2;\n// \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1.left = n2;\n
        binary_tree.rs
        let p = TreeNode::new(0);\n// n1\u3068n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\nn1.borrow_mut().left = Some(p.clone());\np.borrow_mut().left = Some(n2.clone());\n// \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1.borrow_mut().left = Some(n2);\n
        binary_tree.c
        /* \u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664 */\nTreeNode *P = newTreeNode(0);\n// n1\u3068n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\nn1->left = P;\nP->left = n2;\n// \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1->left = n2;\n
        binary_tree.kt
        val P = TreeNode(0)\n// n1\u3068n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\nn1.left = P\nP.left = n2\n// \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1.left = n2\n
        binary_tree.rb
        \n

        Tip

        \u30ce\u30fc\u30c9\u306e\u633f\u5165\u306f\u4e8c\u5206\u6728\u306e\u5143\u306e\u8ad6\u7406\u69cb\u9020\u3092\u5909\u66f4\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u30ce\u30fc\u30c9\u306e\u524a\u9664\u306f\u901a\u5e38\u305d\u306e\u30ce\u30fc\u30c9\u3068\u305d\u306e\u3059\u3079\u3066\u306e\u90e8\u5206\u6728\u3092\u524a\u9664\u3059\u308b\u3053\u3068\u306b\u306a\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u4e8c\u5206\u6728\u3067\u306f\u3001\u633f\u5165\u3068\u524a\u9664\u306f\u901a\u5e38\u4e00\u9023\u306e\u64cd\u4f5c\u3092\u901a\u3058\u3066\u5b9f\u884c\u3055\u308c\u3001\u610f\u5473\u306e\u3042\u308b\u7d50\u679c\u3092\u5f97\u307e\u3059\u3002

        "},{"location":"chapter_tree/binary_tree/#713","title":"7.1.3 \u00a0 \u4e8c\u5206\u6728\u306e\u4e00\u822c\u7684\u306a\u7a2e\u985e","text":""},{"location":"chapter_tree/binary_tree/#1_1","title":"1. \u00a0 \u5b8c\u5168\u4e8c\u5206\u6728","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5b8c\u5168\u4e8c\u5206\u6728\u3067\u306f\u3001\u3059\u3079\u3066\u306e\u30ec\u30d9\u30eb\u304c\u30ce\u30fc\u30c9\u3067\u5b8c\u5168\u306b\u57cb\u3081\u3089\u308c\u3066\u3044\u307e\u3059\u3002\u5b8c\u5168\u4e8c\u5206\u6728\u3067\u306f\u3001\u8449\u30ce\u30fc\u30c9\u306e\u6b21\u6570\u306f\\(0\\)\u3067\u3001\u4ed6\u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u306e\u6b21\u6570\u306f\\(2\\)\u3067\u3059\u3002\u30ce\u30fc\u30c9\u306e\u7dcf\u6570\u306f\\(2^{h+1} - 1\\)\u3068\u3057\u3066\u8a08\u7b97\u3067\u304d\u3001\u3053\u3053\u3067\\(h\\)\u306f\u6728\u306e\u9ad8\u3055\u3067\u3059\u3002\u3053\u308c\u306f\u6a19\u6e96\u7684\u306a\u6307\u6570\u95a2\u4fc2\u3092\u793a\u3057\u3001\u81ea\u7136\u754c\u306e\u7d30\u80de\u5206\u88c2\u306e\u4e00\u822c\u7684\u306a\u73fe\u8c61\u3092\u53cd\u6620\u3057\u3066\u3044\u307e\u3059\u3002

        Tip

        \u4e2d\u56fd\u8a9e\u570f\u3067\u306f\u3001\u5b8c\u5168\u4e8c\u5206\u6728\u306f\u3057\u3070\u3057\u3070\u6e80\u4e8c\u5206\u6728\u3068\u547c\u3070\u308c\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u56f3 7-4 \u00a0 \u5b8c\u5168\u4e8c\u5206\u6728

        "},{"location":"chapter_tree/binary_tree/#2_1","title":"2. \u00a0 \u5b8c\u5099\u4e8c\u5206\u6728","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5b8c\u5099\u4e8c\u5206\u6728\u306f\u3001\u6700\u4e0b\u4f4d\u30ec\u30d9\u30eb\u306e\u307f\u304c\u5b8c\u5168\u306b\u57cb\u3081\u3089\u308c\u3066\u3044\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308b\u4e8c\u5206\u6728\u3067\u3001\u6700\u4e0b\u4f4d\u30ec\u30d9\u30eb\u306e\u30ce\u30fc\u30c9\u306f\u5de6\u304b\u3089\u53f3\u306b\u9023\u7d9a\u3057\u3066\u57cb\u3081\u3089\u308c\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u5b8c\u5168\u4e8c\u5206\u6728\u306f\u5b8c\u5099\u4e8c\u5206\u6728\u3067\u3082\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u56f3 7-5 \u00a0 \u5b8c\u5099\u4e8c\u5206\u6728

        "},{"location":"chapter_tree/binary_tree/#3","title":"3. \u00a0 \u6e80\u4e8c\u5206\u6728","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u6e80\u4e8c\u5206\u6728\u3067\u306f\u3001\u8449\u30ce\u30fc\u30c9\u3092\u9664\u3044\u3066\u3001\u4ed6\u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u304c2\u3064\u306e\u5b50\u30ce\u30fc\u30c9\u3092\u6301\u3061\u307e\u3059\u3002

        \u56f3 7-6 \u00a0 \u6e80\u4e8c\u5206\u6728

        "},{"location":"chapter_tree/binary_tree/#4","title":"4. \u00a0 \u5e73\u8861\u4e8c\u5206\u6728","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5e73\u8861\u4e8c\u5206\u6728\u3067\u306f\u3001\u4efb\u610f\u306e\u30ce\u30fc\u30c9\u306e\u5de6\u3068\u53f3\u306e\u90e8\u5206\u6728\u306e\u9ad8\u3055\u306e\u7d76\u5bfe\u5dee\u304c1\u3092\u8d85\u3048\u307e\u305b\u3093\u3002

        \u56f3 7-7 \u00a0 \u5e73\u8861\u4e8c\u5206\u6728

        "},{"location":"chapter_tree/binary_tree/#714","title":"7.1.4 \u00a0 \u4e8c\u5206\u6728\u306e\u9000\u5316","text":"

        \u4e0b\u56f3\u306f\u3001\u4e8c\u5206\u6728\u306e\u7406\u60f3\u7684\u306a\u69cb\u9020\u3068\u9000\u5316\u3057\u305f\u69cb\u9020\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u4e8c\u5206\u6728\u306f\u3001\u3059\u3079\u3066\u306e\u30ec\u30d9\u30eb\u304c\u57cb\u3081\u3089\u308c\u3066\u3044\u308b\u3068\u304d\u306b\u300c\u5b8c\u5168\u4e8c\u5206\u6728\u300d\u306b\u306a\u308a\u3001\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u304c\u4e00\u65b9\u306b\u504f\u3063\u3066\u3044\u308b\u3068\u304d\u306b\u300c\u9023\u7d50\u30ea\u30b9\u30c8\u300d\u306b\u9000\u5316\u3057\u307e\u3059\u3002

        • \u5b8c\u5168\u4e8c\u5206\u6728\u306f\u3001\u4e8c\u5206\u6728\u306e\u300c\u5206\u5272\u7d71\u6cbb\u6cd5\u300d\u306e\u5229\u70b9\u3092\u5341\u5206\u306b\u6d3b\u7528\u3067\u304d\u308b\u7406\u60f3\u7684\u306a\u30b7\u30ca\u30ea\u30aa\u3067\u3059\u3002
        • \u4e00\u65b9\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u5225\u306e\u6975\u7aef\u3092\u8868\u3057\u3001\u3059\u3079\u3066\u306e\u64cd\u4f5c\u304c\u7dda\u5f62\u306b\u306a\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(n)\\)\u306b\u306a\u308a\u307e\u3059\u3002

        \u56f3 7-8 \u00a0 \u4e8c\u5206\u6728\u306e\u6700\u826f\u3068\u6700\u60aa\u306e\u69cb\u9020

        \u4e0b\u8868\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u6700\u826f\u3068\u6700\u60aa\u306e\u69cb\u9020\u3067\u306f\u3001\u4e8c\u5206\u6728\u306f\u8449\u30ce\u30fc\u30c9\u6570\u3001\u7dcf\u30ce\u30fc\u30c9\u6570\u3001\u9ad8\u3055\u306e\u6700\u5927\u5024\u307e\u305f\u306f\u6700\u5c0f\u5024\u3092\u9054\u6210\u3057\u307e\u3059\u3002

        \u8868 7-1 \u00a0 \u4e8c\u5206\u6728\u306e\u6700\u826f\u3068\u6700\u60aa\u306e\u69cb\u9020

        \u5b8c\u5168\u4e8c\u5206\u6728 \u9023\u7d50\u30ea\u30b9\u30c8 \u30ec\u30d9\u30eb\\(i\\)\u306e\u30ce\u30fc\u30c9\u6570 \\(2^{i-1}\\) \\(1\\) \u9ad8\u3055\\(h\\)\u306e\u6728\u306e\u8449\u30ce\u30fc\u30c9\u6570 \\(2^h\\) \\(1\\) \u9ad8\u3055\\(h\\)\u306e\u6728\u306e\u7dcf\u30ce\u30fc\u30c9\u6570 \\(2^{h+1} - 1\\) \\(h + 1\\) \u7dcf\u30ce\u30fc\u30c9\u6570\\(n\\)\u306e\u6728\u306e\u9ad8\u3055 \\(\\log_2 (n+1) - 1\\) \\(n - 1\\)"},{"location":"chapter_tree/binary_tree_traversal/","title":"7.2 \u00a0 \u4e8c\u5206\u6728\u306e\u8d70\u67fb","text":"

        \u7269\u7406\u7684\u69cb\u9020\u306e\u89b3\u70b9\u304b\u3089\u898b\u308b\u3068\u3001\u6728\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u305d\u306e\u8d70\u67fb\u65b9\u6cd5\u306f\u30dd\u30a4\u30f3\u30bf\u3092\u901a\u3057\u3066\u30ce\u30fc\u30c9\u306b\u4e00\u3064\u305a\u3064\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u3092\u542b\u307f\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u6728\u306f\u975e\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3042\u308b\u305f\u3081\u3001\u6728\u306e\u8d70\u67fb\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u8d70\u67fb\u3088\u308a\u3082\u8907\u96d1\u3067\u3001\u691c\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u652f\u63f4\u304c\u5fc5\u8981\u3067\u3059\u3002

        \u4e8c\u5206\u6728\u306e\u4e00\u822c\u7684\u306a\u8d70\u67fb\u65b9\u6cd5\u306b\u306f\u3001\u30ec\u30d9\u30eb\u9806\u8d70\u67fb\u3001\u524d\u9806\u8d70\u67fb\u3001\u4e2d\u9806\u8d70\u67fb\u3001\u5f8c\u9806\u8d70\u67fb\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_tree/binary_tree_traversal/#721","title":"7.2.1 \u00a0 \u30ec\u30d9\u30eb\u9806\u8d70\u67fb","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30ec\u30d9\u30eb\u9806\u8d70\u67fb\u306f\u4e8c\u5206\u6728\u3092\u4e0a\u304b\u3089\u4e0b\u3078\u3001\u5c64\u3054\u3068\u306b\u8d70\u67fb\u3057\u307e\u3059\u3002\u5404\u30ec\u30d9\u30eb\u5185\u3067\u306f\u3001\u5de6\u304b\u3089\u53f3\u3078\u30ce\u30fc\u30c9\u3092\u8a2a\u554f\u3057\u307e\u3059\u3002

        \u30ec\u30d9\u30eb\u9806\u8d70\u67fb\u306f\u672c\u8cea\u7684\u306b\u5e45\u512a\u5148\u8d70\u67fb\u306e\u4e00\u7a2e\u3067\u3001\u5e45\u512a\u5148\u63a2\u7d22\uff08BFS\uff09\u3068\u3082\u547c\u3070\u308c\u3001\u300c\u5468\u56f2\u306b\u5411\u304b\u3063\u3066\u5916\u5411\u304d\u306b\u62e1\u5f35\u3059\u308b\u300d\u5c64\u3054\u3068\u306e\u8d70\u67fb\u65b9\u6cd5\u3092\u4f53\u73fe\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 7-9 \u00a0 \u4e8c\u5206\u6728\u306e\u30ec\u30d9\u30eb\u9806\u8d70\u67fb

        "},{"location":"chapter_tree/binary_tree_traversal/#1","title":"1. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        \u5e45\u512a\u5148\u8d70\u67fb\u306f\u901a\u5e38\u300c\u30ad\u30e5\u30fc\u300d\u306e\u52a9\u3051\u3092\u501f\u308a\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002\u30ad\u30e5\u30fc\u306f\u300c\u5148\u5165\u308c\u5148\u51fa\u3057\u300d\u306e\u898f\u5247\u306b\u5f93\u3044\u3001\u5e45\u512a\u5148\u8d70\u67fb\u306f\u300c\u5c64\u3054\u3068\u306e\u9032\u884c\u300d\u898f\u5247\u306b\u5f93\u3044\u307e\u3059\u3002\u4e21\u8005\u306e\u57fa\u672c\u7684\u306a\u8003\u3048\u65b9\u306f\u4e00\u81f4\u3057\u3066\u3044\u307e\u3059\u3002\u5b9f\u88c5\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree_bfs.py
        def level_order(root: TreeNode | None) -> list[int]:\n    \"\"\"\u30ec\u30d9\u30eb\u9806\u8d70\u67fb\"\"\"\n    # \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316\u3057\u3001\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u8ffd\u52a0\n    queue: deque[TreeNode] = deque()\n    queue.append(root)\n    # \u8d70\u67fb\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u683c\u7d0d\u3059\u308b\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316\n    res = []\n    while queue:\n        node: TreeNode = queue.popleft()  # \u30ad\u30e5\u30fc\u304b\u3089\u30c7\u30ad\u30e5\u30fc\n        res.append(node.val)  # \u30ce\u30fc\u30c9\u306e\u5024\u3092\u4fdd\u5b58\n        if node.left is not None:\n            queue.append(node.left)  # \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\n        if node.right is not None:\n            queue.append(node.right)  # \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\n    return res\n
        binary_tree_bfs.cpp
        /* \u30ec\u30d9\u30eb\u9806\u8d70\u67fb */\nvector<int> levelOrder(TreeNode *root) {\n    // \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316\u3001\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u8ffd\u52a0\n    queue<TreeNode *> queue;\n    queue.push(root);\n    // \u8d70\u67fb\u9806\u5e8f\u3092\u4fdd\u5b58\u3059\u308b\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316\n    vector<int> vec;\n    while (!queue.empty()) {\n        TreeNode *node = queue.front();\n        queue.pop();              // \u30ad\u30e5\u30fc\u304b\u3089\u30c7\u30ad\u30e5\u30fc\n        vec.push_back(node->val); // \u30ce\u30fc\u30c9\u5024\u3092\u4fdd\u5b58\n        if (node->left != nullptr)\n            queue.push(node->left); // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\n        if (node->right != nullptr)\n            queue.push(node->right); // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\n    }\n    return vec;\n}\n
        binary_tree_bfs.java
        /* \u30ec\u30d9\u30eb\u9806\u8d70\u67fb */\nList<Integer> levelOrder(TreeNode root) {\n    // \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316\u3057\u3001\u6839\u30ce\u30fc\u30c9\u3092\u8ffd\u52a0\n    Queue<TreeNode> queue = new LinkedList<>();\n    queue.add(root);\n    // \u8d70\u67fb\u9806\u5e8f\u3092\u683c\u7d0d\u3059\u308b\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316\n    List<Integer> list = new ArrayList<>();\n    while (!queue.isEmpty()) {\n        TreeNode node = queue.poll(); // \u30ad\u30e5\u30fc\u306e\u30c7\u30ad\u30e5\u30fc\n        list.add(node.val);           // \u30ce\u30fc\u30c9\u306e\u5024\u3092\u4fdd\u5b58\n        if (node.left != null)\n            queue.offer(node.left);   // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\n        if (node.right != null)\n            queue.offer(node.right);  // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\n    }\n    return list;\n}\n
        binary_tree_bfs.cs
        [class]{binary_tree_bfs}-[func]{LevelOrder}\n
        binary_tree_bfs.go
        [class]{}-[func]{levelOrder}\n
        binary_tree_bfs.swift
        [class]{}-[func]{levelOrder}\n
        binary_tree_bfs.js
        [class]{}-[func]{levelOrder}\n
        binary_tree_bfs.ts
        [class]{}-[func]{levelOrder}\n
        binary_tree_bfs.dart
        [class]{}-[func]{levelOrder}\n
        binary_tree_bfs.rs
        [class]{}-[func]{level_order}\n
        binary_tree_bfs.c
        [class]{}-[func]{levelOrder}\n
        binary_tree_bfs.kt
        [class]{}-[func]{levelOrder}\n
        binary_tree_bfs.rb
        [class]{}-[func]{level_order}\n
        "},{"location":"chapter_tree/binary_tree_traversal/#2","title":"2. \u00a0 \u8a08\u7b97\u91cf\u5206\u6790","text":"
        • \u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\): \u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u304c\u4e00\u5ea6\u305a\u3064\u8a2a\u554f\u3055\u308c\u3001\\(O(n)\\)\u306e\u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002\u3053\u3053\u3067\\(n\\)\u306f\u30ce\u30fc\u30c9\u6570\u3067\u3059\u3002
        • \u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\): \u6700\u60aa\u306e\u5834\u5408\u3001\u3064\u307e\u308a\u5b8c\u5168\u4e8c\u5206\u6728\u306e\u5834\u5408\u3001\u6700\u4e0b\u4f4d\u30ec\u30d9\u30eb\u306b\u8d70\u67fb\u3059\u308b\u524d\u306b\u3001\u30ad\u30e5\u30fc\u306f\u6700\u5927\\((n + 1) / 2\\)\u500b\u306e\u30ce\u30fc\u30c9\u3092\u540c\u6642\u306b\u542b\u3080\u3053\u3068\u304c\u3067\u304d\u3001\\(O(n)\\)\u306e\u7a7a\u9593\u3092\u5360\u6709\u3057\u307e\u3059\u3002
        "},{"location":"chapter_tree/binary_tree_traversal/#722","title":"7.2.2 \u00a0 \u524d\u9806\u3001\u4e2d\u9806\u3001\u5f8c\u9806\u8d70\u67fb","text":"

        \u5bfe\u5fdc\u3057\u3066\u3001\u524d\u9806\u3001\u4e2d\u9806\u3001\u5f8c\u9806\u8d70\u67fb\u306f\u3059\u3079\u3066\u6df1\u5ea6\u512a\u5148\u8d70\u67fb\u306b\u5c5e\u3057\u3001\u6df1\u5ea6\u512a\u5148\u63a2\u7d22\uff08DFS\uff09\u3068\u3082\u547c\u3070\u308c\u3001\u300c\u307e\u305a\u6700\u5f8c\u307e\u3067\u9032\u307f\u3001\u305d\u306e\u5f8c\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30af\u3057\u3066\u7d9a\u884c\u3059\u308b\u300d\u8d70\u67fb\u65b9\u6cd5\u3092\u4f53\u73fe\u3057\u3066\u3044\u307e\u3059\u3002

        \u4e0b\u56f3\u306f\u4e8c\u5206\u6728\u306b\u5bfe\u3057\u3066\u6df1\u5ea6\u512a\u5148\u8d70\u67fb\u3092\u5b9f\u884c\u3059\u308b\u52d5\u4f5c\u539f\u7406\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u6df1\u5ea6\u512a\u5148\u8d70\u67fb\u306f\u4e8c\u5206\u6728\u5168\u4f53\u3092\u300c\u6b69\u304d\u56de\u308b\u300d\u3088\u3046\u306a\u3082\u306e\u3067\u3001\u5404\u30ce\u30fc\u30c9\u30673\u3064\u306e\u4f4d\u7f6e\u306b\u906d\u9047\u3057\u3001\u305d\u308c\u3089\u306f\u524d\u9806\u3001\u4e2d\u9806\u3001\u5f8c\u9806\u8d70\u67fb\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 7-10 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728\u306e\u524d\u9806\u3001\u4e2d\u9806\u3001\u5f8c\u9806\u8d70\u67fb

        "},{"location":"chapter_tree/binary_tree_traversal/#1_1","title":"1. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        \u6df1\u5ea6\u512a\u5148\u63a2\u7d22\u306f\u901a\u5e38\u518d\u5e30\u306b\u57fa\u3065\u3044\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree_dfs.py
        def pre_order(root: TreeNode | None):\n    \"\"\"\u524d\u9806\u8d70\u67fb\"\"\"\n    if root is None:\n        return\n    # \u8a2a\u554f\u9806\u5e8f: \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9 -> \u5de6\u90e8\u5206\u6728 -> \u53f3\u90e8\u5206\u6728\n    res.append(root.val)\n    pre_order(root=root.left)\n    pre_order(root=root.right)\n\ndef in_order(root: TreeNode | None):\n    \"\"\"\u4e2d\u9806\u8d70\u67fb\"\"\"\n    if root is None:\n        return\n    # \u8a2a\u554f\u9806\u5e8f: \u5de6\u90e8\u5206\u6728 -> \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9 -> \u53f3\u90e8\u5206\u6728\n    in_order(root=root.left)\n    res.append(root.val)\n    in_order(root=root.right)\n\ndef post_order(root: TreeNode | None):\n    \"\"\"\u5f8c\u9806\u8d70\u67fb\"\"\"\n    if root is None:\n        return\n    # \u8a2a\u554f\u9806\u5e8f: \u5de6\u90e8\u5206\u6728 -> \u53f3\u90e8\u5206\u6728 -> \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\n    post_order(root=root.left)\n    post_order(root=root.right)\n    res.append(root.val)\n
        binary_tree_dfs.cpp
        /* \u524d\u9806\u8d70\u67fb */\nvoid preOrder(TreeNode *root) {\n    if (root == nullptr)\n        return;\n    // \u8a2a\u554f\u512a\u5148\u5ea6\uff1a\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9 -> \u5de6\u90e8\u5206\u6728 -> \u53f3\u90e8\u5206\u6728\n    vec.push_back(root->val);\n    preOrder(root->left);\n    preOrder(root->right);\n}\n\n/* \u4e2d\u9806\u8d70\u67fb */\nvoid inOrder(TreeNode *root) {\n    if (root == nullptr)\n        return;\n    // \u8a2a\u554f\u512a\u5148\u5ea6\uff1a\u5de6\u90e8\u5206\u6728 -> \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9 -> \u53f3\u90e8\u5206\u6728\n    inOrder(root->left);\n    vec.push_back(root->val);\n    inOrder(root->right);\n}\n\n/* \u5f8c\u9806\u8d70\u67fb */\nvoid postOrder(TreeNode *root) {\n    if (root == nullptr)\n        return;\n    // \u8a2a\u554f\u512a\u5148\u5ea6\uff1a\u5de6\u90e8\u5206\u6728 -> \u53f3\u90e8\u5206\u6728 -> \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\n    postOrder(root->left);\n    postOrder(root->right);\n    vec.push_back(root->val);\n}\n
        binary_tree_dfs.java
        /* \u524d\u9806\u8d70\u67fb */\nvoid preOrder(TreeNode root) {\n    if (root == null)\n        return;\n    // \u8a2a\u554f\u512a\u5148\u5ea6: \u6839\u30ce\u30fc\u30c9 -> \u5de6\u90e8\u5206\u6728 -> \u53f3\u90e8\u5206\u6728\n    list.add(root.val);\n    preOrder(root.left);\n    preOrder(root.right);\n}\n\n/* \u4e2d\u9806\u8d70\u67fb */\nvoid inOrder(TreeNode root) {\n    if (root == null)\n        return;\n    // \u8a2a\u554f\u512a\u5148\u5ea6: \u5de6\u90e8\u5206\u6728 -> \u6839\u30ce\u30fc\u30c9 -> \u53f3\u90e8\u5206\u6728\n    inOrder(root.left);\n    list.add(root.val);\n    inOrder(root.right);\n}\n\n/* \u5f8c\u9806\u8d70\u67fb */\nvoid postOrder(TreeNode root) {\n    if (root == null)\n        return;\n    // \u8a2a\u554f\u512a\u5148\u5ea6: \u5de6\u90e8\u5206\u6728 -> \u53f3\u90e8\u5206\u6728 -> \u6839\u30ce\u30fc\u30c9\n    postOrder(root.left);\n    postOrder(root.right);\n    list.add(root.val);\n}\n
        binary_tree_dfs.cs
        [class]{binary_tree_dfs}-[func]{PreOrder}\n\n[class]{binary_tree_dfs}-[func]{InOrder}\n\n[class]{binary_tree_dfs}-[func]{PostOrder}\n
        binary_tree_dfs.go
        [class]{}-[func]{preOrder}\n\n[class]{}-[func]{inOrder}\n\n[class]{}-[func]{postOrder}\n
        binary_tree_dfs.swift
        [class]{}-[func]{preOrder}\n\n[class]{}-[func]{inOrder}\n\n[class]{}-[func]{postOrder}\n
        binary_tree_dfs.js
        [class]{}-[func]{preOrder}\n\n[class]{}-[func]{inOrder}\n\n[class]{}-[func]{postOrder}\n
        binary_tree_dfs.ts
        [class]{}-[func]{preOrder}\n\n[class]{}-[func]{inOrder}\n\n[class]{}-[func]{postOrder}\n
        binary_tree_dfs.dart
        [class]{}-[func]{preOrder}\n\n[class]{}-[func]{inOrder}\n\n[class]{}-[func]{postOrder}\n
        binary_tree_dfs.rs
        [class]{}-[func]{pre_order}\n\n[class]{}-[func]{in_order}\n\n[class]{}-[func]{post_order}\n
        binary_tree_dfs.c
        [class]{}-[func]{preOrder}\n\n[class]{}-[func]{inOrder}\n\n[class]{}-[func]{postOrder}\n
        binary_tree_dfs.kt
        [class]{}-[func]{preOrder}\n\n[class]{}-[func]{inOrder}\n\n[class]{}-[func]{postOrder}\n
        binary_tree_dfs.rb
        [class]{}-[func]{pre_order}\n\n[class]{}-[func]{in_order}\n\n[class]{}-[func]{post_order}\n

        Tip

        \u6df1\u5ea6\u512a\u5148\u63a2\u7d22\u306f\u53cd\u5fa9\u306b\u57fa\u3065\u3044\u3066\u3082\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002\u8208\u5473\u306e\u3042\u308b\u8aad\u8005\u306f\u81ea\u5206\u3067\u5b66\u7fd2\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u4e0b\u56f3\u306f\u4e8c\u5206\u6728\u306e\u524d\u9806\u8d70\u67fb\u306e\u518d\u5e30\u30d7\u30ed\u30bb\u30b9\u3092\u793a\u3057\u3066\u304a\u308a\u3001\u3053\u308c\u306f\u300c\u518d\u5e30\u300d\u3068\u300c\u5fa9\u5e30\u300d\u3068\u3044\u30462\u3064\u306e\u53cd\u5bfe\u306e\u90e8\u5206\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        1. \u300c\u518d\u5e30\u300d\u306f\u65b0\u3057\u3044\u30e1\u30bd\u30c3\u30c9\u3092\u958b\u59cb\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u3053\u306e\u30d7\u30ed\u30bb\u30b9\u3067\u6b21\u306e\u30ce\u30fc\u30c9\u306b\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002
        2. \u300c\u5fa9\u5e30\u300d\u306f\u95a2\u6570\u304c\u623b\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u3001\u73fe\u5728\u306e\u30ce\u30fc\u30c9\u304c\u5b8c\u5168\u306b\u30a2\u30af\u30bb\u30b9\u3055\u308c\u305f\u3053\u3068\u3092\u793a\u3057\u307e\u3059\u3002
        <1><2><3><4><5><6><7><8><9><10><11>

        \u56f3 7-11 \u00a0 \u524d\u9806\u8d70\u67fb\u306e\u518d\u5e30\u30d7\u30ed\u30bb\u30b9

        "},{"location":"chapter_tree/binary_tree_traversal/#2_1","title":"2. \u00a0 \u8a08\u7b97\u91cf\u5206\u6790","text":"
        • \u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\): \u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u304c\u4e00\u5ea6\u305a\u3064\u8a2a\u554f\u3055\u308c\u3001\\(O(n)\\)\u306e\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\): \u6700\u60aa\u306e\u5834\u5408\u3001\u3064\u307e\u308a\u6728\u304c\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u9000\u5316\u3057\u305f\u5834\u5408\u3001\u518d\u5e30\u306e\u6df1\u3055\u306f\\(n\\)\u306b\u9054\u3057\u3001\u30b7\u30b9\u30c6\u30e0\u306f\\(O(n)\\)\u306e\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u5360\u6709\u3057\u307e\u3059\u3002
        "},{"location":"chapter_tree/summary/","title":"7.6 \u00a0 \u307e\u3068\u3081","text":""},{"location":"chapter_tree/summary/#1","title":"1. \u00a0 \u91cd\u8981\u306a\u30dd\u30a4\u30f3\u30c8","text":"
        • \u4e8c\u5206\u6728\u306f\u975e\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3001\u300c\u4e00\u3064\u3092\u4e8c\u3064\u306b\u5206\u3051\u308b\u300d\u5206\u5272\u7d71\u6cbb\u306e\u30ed\u30b8\u30c3\u30af\u3092\u53cd\u6620\u3057\u3066\u3044\u307e\u3059\u3002\u5404\u4e8c\u5206\u6728\u30ce\u30fc\u30c9\u306b\u306f\u5024\u30682\u3064\u306e\u30dd\u30a4\u30f3\u30bf\u304c\u542b\u307e\u308c\u3001\u305d\u308c\u305e\u308c\u5de6\u3068\u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3092\u6307\u3057\u307e\u3059\u3002
        • \u4e8c\u5206\u6728\u306e\u30ce\u30fc\u30c9\u306b\u3064\u3044\u3066\u3001\u305d\u306e\u5de6\uff08\u53f3\uff09\u5b50\u30ce\u30fc\u30c9\u3068\u305d\u306e\u4e0b\u306b\u5f62\u6210\u3055\u308c\u308b\u6728\u306f\u3001\u307e\u3068\u3081\u3066\u305d\u306e\u30ce\u30fc\u30c9\u306e\u5de6\uff08\u53f3\uff09\u90e8\u5206\u6728\u3068\u547c\u3070\u308c\u307e\u3059\u3002
        • \u4e8c\u5206\u6728\u306b\u95a2\u9023\u3059\u308b\u7528\u8a9e\u306b\u306f\u3001\u6839\u30ce\u30fc\u30c9\u3001\u8449\u30ce\u30fc\u30c9\u3001\u30ec\u30d9\u30eb\u3001\u6b21\u6570\u3001\u30a8\u30c3\u30b8\u3001\u9ad8\u3055\u3001\u6df1\u3055\u304c\u3042\u308a\u307e\u3059\u3002
        • \u4e8c\u5206\u6728\u306e\u521d\u671f\u5316\u3001\u30ce\u30fc\u30c9\u306e\u633f\u5165\u3001\u30ce\u30fc\u30c9\u306e\u524a\u9664\u306e\u64cd\u4f5c\u306f\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u64cd\u4f5c\u3068\u4f3c\u3066\u3044\u307e\u3059\u3002
        • \u4e00\u822c\u7684\u306a\u4e8c\u5206\u6728\u306e\u7a2e\u985e\u306b\u306f\u3001\u5b8c\u5168\u4e8c\u5206\u6728\u3001\u5b8c\u5099\u4e8c\u5206\u6728\u3001\u6e80\u4e8c\u5206\u6728\u3001\u5e73\u8861\u4e8c\u5206\u6728\u304c\u3042\u308a\u307e\u3059\u3002\u5b8c\u5168\u4e8c\u5206\u6728\u306f\u7406\u60f3\u7684\u306a\u72b6\u614b\u3092\u8868\u3057\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u9000\u5316\u5f8c\u306e\u6700\u60aa\u306e\u72b6\u614b\u3067\u3059\u3002
        • \u4e8c\u5206\u6728\u306f\u3001\u30ce\u30fc\u30c9\u5024\u3068\u7a7a\u304d\u30b9\u30ed\u30c3\u30c8\u3092\u30ec\u30d9\u30eb\u9806\u8d70\u67fb\u30b7\u30fc\u30b1\u30f3\u30b9\u3067\u914d\u7f6e\u3057\u3001\u89aa\u30ce\u30fc\u30c9\u3068\u5b50\u30ce\u30fc\u30c9\u9593\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u30de\u30c3\u30d4\u30f3\u30b0\u95a2\u4fc2\u306b\u57fa\u3065\u3044\u3066\u30dd\u30a4\u30f3\u30bf\u3092\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3067\u3001\u914d\u5217\u3092\u4f7f\u7528\u3057\u3066\u8868\u73fe\u3067\u304d\u307e\u3059\u3002
        • \u4e8c\u5206\u6728\u306e\u30ec\u30d9\u30eb\u9806\u8d70\u67fb\u306f\u5e45\u512a\u5148\u63a2\u7d22\u624b\u6cd5\u3067\u3001\u300c\u5186\u3092\u62e1\u5927\u3057\u306a\u304c\u3089\u300d\u306e\u5c64\u3054\u3068\u306e\u8d70\u67fb\u65b9\u5f0f\u3092\u53cd\u6620\u3057\u3066\u3044\u307e\u3059\u3002\u901a\u5e38\u306f\u30ad\u30e5\u30fc\u3092\u4f7f\u7528\u3057\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002
        • \u524d\u9806\u3001\u4e2d\u9806\u3001\u5f8c\u9806\u8d70\u67fb\u306f\u3059\u3079\u3066\u6df1\u5ea6\u512a\u5148\u63a2\u7d22\u624b\u6cd5\u3067\u3001\u300c\u307e\u305a\u6700\u5f8c\u307e\u3067\u884c\u304d\u3001\u305d\u306e\u5f8c\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30af\u3057\u3066\u7d9a\u884c\u3059\u308b\u300d\u8d70\u67fb\u65b9\u5f0f\u3092\u53cd\u6620\u3057\u3066\u3044\u307e\u3059\u3002\u901a\u5e38\u306f\u518d\u5e30\u3092\u4f7f\u7528\u3057\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002
        • \u4e8c\u5206\u63a2\u7d22\u6728\u306f\u8981\u7d20\u691c\u7d22\u306e\u305f\u3081\u306e\u52b9\u7387\u7684\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3001\u691c\u7d22\u3001\u633f\u5165\u3001\u524a\u9664\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\u3059\u3079\u3066\\(O(\\log n)\\)\u3067\u3059\u3002\u4e8c\u5206\u63a2\u7d22\u6728\u304c\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u9000\u5316\u3059\u308b\u3068\u3001\u3053\u308c\u3089\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u306b\u60aa\u5316\u3057\u307e\u3059\u3002
        • AVL\u6728\u306f\u5e73\u8861\u4e8c\u5206\u63a2\u7d22\u6728\u3068\u3082\u547c\u3070\u308c\u3001\u56de\u8ee2\u64cd\u4f5c\u3092\u901a\u3057\u3066\u7d99\u7d9a\u7684\u306a\u30ce\u30fc\u30c9\u633f\u5165\u3068\u524a\u9664\u5f8c\u3082\u6728\u304c\u5e73\u8861\u3092\u4fdd\u3064\u3053\u3068\u3092\u4fdd\u8a3c\u3057\u307e\u3059\u3002
        • AVL\u6728\u306e\u56de\u8ee2\u64cd\u4f5c\u306b\u306f\u3001\u53f3\u56de\u8ee2\u3001\u5de6\u56de\u8ee2\u3001\u53f3\u5de6\u56de\u8ee2\u3001\u5de6\u53f3\u56de\u8ee2\u304c\u3042\u308a\u307e\u3059\u3002\u30ce\u30fc\u30c9\u306e\u633f\u5165\u307e\u305f\u306f\u524a\u9664\u5f8c\u3001AVL\u6728\u306f\u30dc\u30c8\u30e0\u30a2\u30c3\u30d7\u65b9\u5f0f\u3067\u3053\u308c\u3089\u306e\u56de\u8ee2\u3092\u5b9f\u884c\u3057\u3066\u81ea\u5df1\u5e73\u8861\u3092\u53d6\u308a\u307e\u3059\u3002
        "},{"location":"chapter_tree/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q: \u4e00\u3064\u306e\u30ce\u30fc\u30c9\u306e\u307f\u3092\u6301\u3064\u4e8c\u5206\u6728\u306b\u3064\u3044\u3066\u3001\u6728\u306e\u9ad8\u3055\u3068\u6839\u30ce\u30fc\u30c9\u306e\u6df1\u3055\u306e\u4e21\u65b9\u304c\\(0\\)\u3067\u3059\u304b\uff1f

        \u306f\u3044\u3001\u9ad8\u3055\u3068\u6df1\u3055\u306f\u901a\u5e38\u300c\u901a\u904e\u3057\u305f\u30a8\u30c3\u30b8\u306e\u6570\u300d\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u308b\u305f\u3081\u3067\u3059\u3002

        Q: \u4e8c\u5206\u6728\u306b\u304a\u3051\u308b\u633f\u5165\u3068\u524a\u9664\u306f\u4e00\u822c\u7684\u306b\u4e00\u9023\u306e\u64cd\u4f5c\u306b\u3088\u3063\u3066\u9054\u6210\u3055\u308c\u307e\u3059\u3002\u3053\u3053\u3067\u306e\u300c\u4e00\u9023\u306e\u64cd\u4f5c\u300d\u3068\u306f\u4f55\u3092\u6307\u3057\u307e\u3059\u304b\uff1f\u5b50\u30ce\u30fc\u30c9\u306e\u30ea\u30bd\u30fc\u30b9\u3092\u89e3\u653e\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u304b\uff1f

        \u4e8c\u5206\u63a2\u7d22\u6728\u3092\u4f8b\u306b\u53d6\u308b\u3068\u3001\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3059\u308b\u64cd\u4f5c\u306f3\u3064\u306e\u7570\u306a\u308b\u30b7\u30ca\u30ea\u30aa\u3067\u51e6\u7406\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u305d\u308c\u305e\u308c\u8907\u6570\u30b9\u30c6\u30c3\u30d7\u306e\u30ce\u30fc\u30c9\u64cd\u4f5c\u304c\u5fc5\u8981\u3067\u3059\u3002

        Q: \u4e8c\u5206\u6728\u306eDFS\u8d70\u67fb\u3067\u524d\u9806\u3001\u4e2d\u9806\u3001\u5f8c\u9806\u306e3\u3064\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u304c\u3042\u308b\u306e\u306f\u306a\u305c\u3067\u3059\u304b\uff1f\u305d\u306e\u7528\u9014\u306f\u4f55\u3067\u3059\u304b\uff1f

        \u914d\u5217\u306e\u9806\u6b21\u304a\u3088\u3073\u9006\u9806\u8d70\u67fb\u3068\u540c\u69d8\u306b\u3001\u524d\u9806\u3001\u4e2d\u9806\u3001\u5f8c\u9806\u8d70\u67fb\u306f\u4e8c\u5206\u6728\u3092\u8d70\u67fb\u3059\u308b3\u3064\u306e\u65b9\u6cd5\u3067\u3042\u308a\u3001\u7279\u5b9a\u306e\u9806\u5e8f\u3067\u8d70\u67fb\u7d50\u679c\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u3067\u306f\u3001\u30ce\u30fc\u30c9\u30b5\u30a4\u30ba\u304c\u300c\u5de6\u5b50\u30ce\u30fc\u30c9\u5024 < \u6839\u30ce\u30fc\u30c9\u5024 < \u53f3\u5b50\u30ce\u30fc\u30c9\u5024\u300d\u3092\u6e80\u305f\u3059\u305f\u3081\u3001\u300c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u300d\u306e\u512a\u5148\u9806\u4f4d\u3067\u6728\u3092\u8d70\u67fb\u3059\u308b\u3053\u3068\u3067\u3001\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u30ce\u30fc\u30c9\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002

        Q: \u4e0d\u5e73\u8861\u30ce\u30fc\u30c9node\u3001child\u3001grand_child\u9593\u306e\u95a2\u4fc2\u3092\u51e6\u7406\u3059\u308b\u53f3\u56de\u8ee2\u64cd\u4f5c\u306b\u304a\u3044\u3066\u3001\u53f3\u56de\u8ee2\u5f8c\u306bnode\u3068\u305d\u306e\u89aa\u30ce\u30fc\u30c9\u9593\u306e\u63a5\u7d9a\u3068node\u306e\u5143\u306e\u30ea\u30f3\u30af\u304c\u5931\u308f\u308c\u308b\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u304b\uff1f

        \u3053\u306e\u554f\u984c\u3092\u518d\u5e30\u7684\u306a\u89b3\u70b9\u304b\u3089\u898b\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002right_rotate(root)\u64cd\u4f5c\u306f\u90e8\u5206\u6728\u306e\u6839\u30ce\u30fc\u30c9\u3092\u6e21\u3057\u3001\u6700\u7d42\u7684\u306breturn child\u3067\u56de\u8ee2\u3055\u308c\u305f\u90e8\u5206\u6728\u306e\u6839\u30ce\u30fc\u30c9\u3092\u8fd4\u3057\u307e\u3059\u3002\u90e8\u5206\u6728\u306e\u6839\u30ce\u30fc\u30c9\u3068\u305d\u306e\u89aa\u30ce\u30fc\u30c9\u9593\u306e\u63a5\u7d9a\u306f\u3001\u3053\u306e\u95a2\u6570\u304c\u623b\u3063\u305f\u5f8c\u306b\u78ba\u7acb\u3055\u308c\u3001\u3053\u308c\u306f\u53f3\u56de\u8ee2\u64cd\u4f5c\u306e\u4fdd\u5b88\u7bc4\u56f2\u5916\u3067\u3059\u3002

        Q: C++\u3067\u306f\u3001\u95a2\u6570\u306fprivate\u3068public\u30bb\u30af\u30b7\u30e7\u30f3\u306b\u5206\u304b\u308c\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u306b\u306f\u3069\u306e\u3088\u3046\u306a\u8003\u616e\u4e8b\u9805\u304c\u3042\u308a\u307e\u3059\u304b\uff1f\u306a\u305cheight()\u95a2\u6570\u3068updateHeight()\u95a2\u6570\u304c\u305d\u308c\u305e\u308cpublic\u3068private\u306b\u914d\u7f6e\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3059\u304b\uff1f

        \u3053\u308c\u306f\u30e1\u30bd\u30c3\u30c9\u306e\u4f7f\u7528\u7bc4\u56f2\u306b\u3088\u308a\u307e\u3059\u3002\u30e1\u30bd\u30c3\u30c9\u304c\u30af\u30e9\u30b9\u5185\u3067\u306e\u307f\u4f7f\u7528\u3055\u308c\u308b\u5834\u5408\u3001private\u306b\u8a2d\u8a08\u3055\u308c\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u30e6\u30fc\u30b6\u30fc\u304c\u72ec\u81ea\u306bupdateHeight()\u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u306f\u610f\u5473\u304c\u3042\u308a\u307e\u305b\u3093\u3002\u3053\u308c\u306f\u633f\u5165\u307e\u305f\u306f\u524a\u9664\u64cd\u4f5c\u306e\u4e00\u30b9\u30c6\u30c3\u30d7\u306b\u904e\u304e\u306a\u3044\u304b\u3089\u3067\u3059\u3002\u3057\u304b\u3057\u3001height()\u306f\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u305f\u3081\u306e\u3082\u306e\u3067\u3001vector.size()\u3068\u540c\u69d8\u3067\u3042\u308b\u305f\u3081\u3001\u4f7f\u7528\u306e\u305f\u3081\u306bpublic\u306b\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        Q: \u5165\u529b\u30c7\u30fc\u30bf\u306e\u30bb\u30c3\u30c8\u304b\u3089\u4e8c\u5206\u63a2\u7d22\u6728\u3092\u3069\u306e\u3088\u3046\u306b\u69cb\u7bc9\u3057\u307e\u3059\u304b\uff1f\u6839\u30ce\u30fc\u30c9\u306e\u9078\u629e\u306f\u975e\u5e38\u306b\u91cd\u8981\u3067\u3059\u304b\uff1f

        \u306f\u3044\u3001\u6728\u3092\u69cb\u7bc9\u3059\u308b\u65b9\u6cd5\u306f\u4e8c\u5206\u63a2\u7d22\u6728\u30b3\u30fc\u30c9\u306ebuild_tree()\u30e1\u30bd\u30c3\u30c9\u3067\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u6839\u30ce\u30fc\u30c9\u306e\u9078\u629e\u306b\u3064\u3044\u3066\u306f\u3001\u901a\u5e38\u5165\u529b\u30c7\u30fc\u30bf\u3092\u30bd\u30fc\u30c8\u3057\u3001\u4e2d\u592e\u306e\u8981\u7d20\u3092\u6839\u30ce\u30fc\u30c9\u3068\u3057\u3066\u9078\u629e\u3057\u3001\u518d\u5e30\u7684\u306b\u5de6\u3068\u53f3\u306e\u90e8\u5206\u6728\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002\u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\u6728\u306e\u5e73\u8861\u3092\u6700\u5927\u5316\u3057\u307e\u3059\u3002

        Q: Java\u3067\u306f\u3001\u6587\u5b57\u5217\u6bd4\u8f03\u306b\u5e38\u306bequals()\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u304b\uff1f

        Java\u3067\u306f\u3001\u30d7\u30ea\u30df\u30c6\u30a3\u30d6\u30c7\u30fc\u30bf\u578b\u306e\u5834\u5408\u3001==\u306f2\u3064\u306e\u5909\u6570\u306e\u5024\u304c\u7b49\u3057\u3044\u304b\u3069\u3046\u304b\u3092\u6bd4\u8f03\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u53c2\u7167\u578b\u306e\u5834\u5408\u30012\u3064\u306e\u30b7\u30f3\u30dc\u30eb\u306e\u52d5\u4f5c\u539f\u7406\u306f\u7570\u306a\u308a\u307e\u3059\u3002

        • ==: 2\u3064\u306e\u5909\u6570\u304c\u540c\u3058\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6307\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3001\u3064\u307e\u308a\u30e1\u30e2\u30ea\u5185\u306e\u4f4d\u7f6e\u304c\u540c\u3058\u304b\u3069\u3046\u304b\u3092\u6bd4\u8f03\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        • equals(): 2\u3064\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5024\u304c\u7b49\u3057\u3044\u304b\u3069\u3046\u304b\u3092\u6bd4\u8f03\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002

        \u3057\u305f\u304c\u3063\u3066\u3001\u5024\u3092\u6bd4\u8f03\u3059\u308b\u306b\u306fequals()\u3092\u4f7f\u7528\u3059\u3079\u304d\u3067\u3059\u3002\u305f\u3060\u3057\u3001String a = \"hi\"; String b = \"hi\";\u3067\u521d\u671f\u5316\u3055\u308c\u305f\u6587\u5b57\u5217\u306f\u6587\u5b57\u5217\u5b9a\u6570\u30d7\u30fc\u30eb\u306b\u683c\u7d0d\u3055\u308c\u3001\u540c\u3058\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6307\u3059\u305f\u3081\u3001a == b\u30822\u3064\u306e\u6587\u5b57\u5217\u306e\u5185\u5bb9\u3092\u6bd4\u8f03\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002

        Q: \u6700\u4e0b\u4f4d\u30ec\u30d9\u30eb\u306b\u5230\u9054\u3059\u308b\u524d\u306b\u3001\u5e45\u512a\u5148\u8d70\u67fb\u3067\u30ad\u30e5\u30fc\u5185\u306e\u30ce\u30fc\u30c9\u6570\u306f\\(2^h\\)\u3067\u3059\u304b\uff1f

        \u306f\u3044\u3001\u4f8b\u3048\u3070\u9ad8\u3055\\(h = 2\\)\u306e\u6e80\u4e8c\u5206\u6728\u306f\u5408\u8a08\\(n = 7\\)\u500b\u306e\u30ce\u30fc\u30c9\u3092\u6301\u3061\u3001\u6700\u4e0b\u4f4d\u30ec\u30d9\u30eb\u306b\u306f\\(4 = 2^h = (n + 1) / 2\\)\u500b\u306e\u30ce\u30fc\u30c9\u304c\u3042\u308a\u307e\u3059\u3002

        "}]} \ No newline at end of file +{"config":{"lang":["ja"],"separator":"[\\s\\-\u3000\u3001\u3002\uff0c\uff0e]+","pipeline":["stemmer"],"fields":{"title":{"boost":1000.0},"text":{"boost":1.0},"tags":{"boost":1000000.0}}},"docs":[{"location":"chapter_appendix/","title":"\u7b2c 16 \u7ae0 \u00a0 \u4ed8\u9332","text":""},{"location":"chapter_appendix/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 16.1 \u00a0 \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u74b0\u5883\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb
        • 16.2 \u00a0 \u4e00\u7dd2\u306b\u5275\u4f5c\u306b\u53c2\u52a0
        • 16.3 \u00a0 \u7528\u8a9e\u96c6
        "},{"location":"chapter_appendix/contribution/","title":"16.2 \u00a0 \u30b3\u30f3\u30c8\u30ea\u30d3\u30e5\u30fc\u30b7\u30e7\u30f3","text":"

        \u8457\u8005\u306e\u80fd\u529b\u306b\u9650\u308a\u304c\u3042\u308b\u305f\u3081\u3001\u672c\u66f8\u306b\u306f\u3044\u304f\u3064\u304b\u306e\u7701\u7565\u3084\u8aa4\u308a\u304c\u907f\u3051\u3089\u308c\u307e\u305b\u3093\u3002\u3054\u7406\u89e3\u3092\u304a\u9858\u3044\u3057\u307e\u3059\u3002\u8aa4\u5b57\u3001\u30ea\u30f3\u30af\u5207\u308c\u3001\u5185\u5bb9\u306e\u6b20\u843d\u3001\u6587\u7ae0\u306e\u66d6\u6627\u3055\u3001\u8aac\u660e\u306e\u4e0d\u660e\u78ba\u3055\u3001\u307e\u305f\u306f\u4e0d\u5408\u7406\u306a\u6587\u7ae0\u69cb\u9020\u3092\u767a\u898b\u3055\u308c\u305f\u5834\u5408\u306f\u3001\u8aad\u8005\u306b\u3088\u308a\u826f\u8cea\u306a\u5b66\u7fd2\u30ea\u30bd\u30fc\u30b9\u3092\u63d0\u4f9b\u3059\u308b\u305f\u3081\u3001\u4fee\u6b63\u306b\u3054\u5354\u529b\u304f\u3060\u3055\u3044\u3002

        \u3059\u3079\u3066\u306e\u30b3\u30f3\u30c8\u30ea\u30d3\u30e5\u30fc\u30bf\u30fc\u306eGitHub ID\u306f\u3001\u672c\u66f8\u306e\u30ea\u30dd\u30b8\u30c8\u30ea\u3001\u30a6\u30a7\u30d6\u3001PDF\u30d0\u30fc\u30b8\u30e7\u30f3\u306e\u30db\u30fc\u30e0\u30da\u30fc\u30b8\u306b\u8868\u793a\u3055\u308c\u3001\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u30b3\u30df\u30e5\u30cb\u30c6\u30a3\u3078\u306e\u7121\u79c1\u306e\u8ca2\u732e\u306b\u611f\u8b1d\u3044\u305f\u3057\u307e\u3059\u3002

        \u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u306e\u9b45\u529b

        \u7d19\u306e\u672c\u306e2\u3064\u306e\u5370\u5237\u7248\u306e\u9593\u9694\u306f\u3057\u3070\u3057\u3070\u9577\u304f\u3001\u5185\u5bb9\u306e\u66f4\u65b0\u304c\u975e\u5e38\u306b\u4e0d\u4fbf\u3067\u3059\u3002

        \u3057\u304b\u3057\u3001\u3053\u306e\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u306e\u672c\u3067\u306f\u3001\u5185\u5bb9\u306e\u66f4\u65b0\u30b5\u30a4\u30af\u30eb\u306f\u6570\u65e5\u3001\u3055\u3089\u306b\u306f\u6570\u6642\u9593\u306b\u77ed\u7e2e\u3055\u308c\u307e\u3059\u3002

        "},{"location":"chapter_appendix/contribution/#1","title":"1. \u00a0 \u5185\u5bb9\u306e\u5fae\u8abf\u6574","text":"

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5404\u30da\u30fc\u30b8\u306e\u53f3\u4e0a\u89d2\u306b\u300c\u7de8\u96c6\u30a2\u30a4\u30b3\u30f3\u300d\u304c\u3042\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u624b\u9806\u306b\u5f93\u3063\u3066\u30c6\u30ad\u30b9\u30c8\u3084\u30b3\u30fc\u30c9\u3092\u4fee\u6b63\u3067\u304d\u307e\u3059\u3002

        1. \u300c\u7de8\u96c6\u30a2\u30a4\u30b3\u30f3\u300d\u3092\u30af\u30ea\u30c3\u30af\u3057\u307e\u3059\u3002\u300c\u3053\u306e\u30ea\u30dd\u30b8\u30c8\u30ea\u3092\u30d5\u30a9\u30fc\u30af\u3057\u307e\u3059\u304b\u300d\u3068\u4fc3\u3055\u308c\u305f\u5834\u5408\u306f\u3001\u540c\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002
        2. Markdown\u30bd\u30fc\u30b9\u30d5\u30a1\u30a4\u30eb\u306e\u5185\u5bb9\u3092\u4fee\u6b63\u3057\u3001\u5185\u5bb9\u306e\u6b63\u78ba\u6027\u3092\u78ba\u8a8d\u3057\u3001\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u306e\u4e00\u8cab\u6027\u3092\u4fdd\u3064\u3088\u3046\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002
        3. \u30da\u30fc\u30b8\u306e\u4e0b\u90e8\u3067\u4fee\u6b63\u8aac\u660e\u3092\u8a18\u5165\u3057\u3001\u300cPropose file change\u300d\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3057\u307e\u3059\u3002\u30da\u30fc\u30b8\u304c\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3055\u308c\u305f\u5f8c\u3001\u300cCreate pull request\u300d\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3057\u3066\u30d7\u30eb\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u958b\u59cb\u3057\u307e\u3059\u3002

        \u56f3 16-3 \u00a0 \u30da\u30fc\u30b8\u7de8\u96c6\u30dc\u30bf\u30f3

        \u56f3\u306f\u76f4\u63a5\u4fee\u6b63\u3067\u304d\u306a\u3044\u305f\u3081\u3001\u65b0\u3057\u3044Issue\u3092\u4f5c\u6210\u3059\u308b\u304b\u3001\u554f\u984c\u3092\u8aac\u660e\u3059\u308b\u30b3\u30e1\u30f3\u30c8\u304c\u5fc5\u8981\u3067\u3059\u3002\u3067\u304d\u308b\u3060\u3051\u65e9\u304f\u56f3\u3092\u518d\u63cf\u753b\u3057\u3066\u7f6e\u304d\u63db\u3048\u307e\u3059\u3002

        "},{"location":"chapter_appendix/contribution/#2","title":"2. \u00a0 \u5185\u5bb9\u306e\u4f5c\u6210","text":"

        \u3053\u306e\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3078\u306e\u53c2\u52a0\u306b\u8208\u5473\u304c\u3042\u308b\u5834\u5408\u3001\u30b3\u30fc\u30c9\u3092\u4ed6\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u7ffb\u8a33\u3057\u305f\u308a\u3001\u8a18\u4e8b\u306e\u5185\u5bb9\u3092\u62e1\u5f35\u3057\u305f\u308a\u3059\u308b\u3053\u3068\u3092\u542b\u3081\u3066\u3001\u4ee5\u4e0b\u306e\u30d7\u30eb\u30ea\u30af\u30a8\u30b9\u30c8\u30ef\u30fc\u30af\u30d5\u30ed\u30fc\u3092\u5b9f\u88c5\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        1. GitHub\u306b\u30ed\u30b0\u30a4\u30f3\u3057\u3001\u672c\u66f8\u306e\u30b3\u30fc\u30c9\u30ea\u30dd\u30b8\u30c8\u30ea\u3092\u500b\u4eba\u30a2\u30ab\u30a6\u30f3\u30c8\u306b\u30d5\u30a9\u30fc\u30af\u3057\u307e\u3059\u3002
        2. \u30d5\u30a9\u30fc\u30af\u3057\u305f\u30ea\u30dd\u30b8\u30c8\u30ea\u306e\u30a6\u30a7\u30d6\u30da\u30fc\u30b8\u306b\u79fb\u52d5\u3057\u3001git clone\u30b3\u30de\u30f3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u30ea\u30dd\u30b8\u30c8\u30ea\u3092\u30ed\u30fc\u30ab\u30eb\u30de\u30b7\u30f3\u306b\u30af\u30ed\u30fc\u30f3\u3057\u307e\u3059\u3002
        3. \u30ed\u30fc\u30ab\u30eb\u3067\u5185\u5bb9\u3092\u4f5c\u6210\u3057\u3001\u5b8c\u5168\u306a\u30c6\u30b9\u30c8\u3092\u5b9f\u884c\u3057\u3066\u30b3\u30fc\u30c9\u306e\u6b63\u78ba\u6027\u3092\u691c\u8a3c\u3057\u307e\u3059\u3002
        4. \u30ed\u30fc\u30ab\u30eb\u3067\u884c\u3063\u305f\u5909\u66f4\u3092\u30b3\u30df\u30c3\u30c8\u3057\u3001\u30ea\u30e2\u30fc\u30c8\u30ea\u30dd\u30b8\u30c8\u30ea\u306b\u30d7\u30c3\u30b7\u30e5\u3057\u307e\u3059\u3002
        5. \u30ea\u30dd\u30b8\u30c8\u30ea\u306e\u30a6\u30a7\u30d6\u30da\u30fc\u30b8\u3092\u66f4\u65b0\u3057\u3001\u300cCreate pull request\u300d\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3057\u3066\u30d7\u30eb\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u958b\u59cb\u3057\u307e\u3059\u3002
        "},{"location":"chapter_appendix/contribution/#3-docker","title":"3. \u00a0 Docker\u30c7\u30d7\u30ed\u30a4\u30e1\u30f3\u30c8","text":"

        hello-algo\u30eb\u30fc\u30c8\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3067\u3001\u4ee5\u4e0b\u306eDocker\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u5b9f\u884c\u3057\u3066http://localhost:8000\u3067\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306b\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\uff1a

        docker-compose up -d\n

        \u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u30c7\u30d7\u30ed\u30a4\u30e1\u30f3\u30c8\u3092\u524a\u9664\u3057\u307e\u3059\uff1a

        docker-compose down\n
        "},{"location":"chapter_appendix/installation/","title":"16.1 \u00a0 \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb","text":""},{"location":"chapter_appendix/installation/#1611-ide","title":"16.1.1 \u00a0 IDE\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb","text":"

        \u30ed\u30fc\u30ab\u30eb\u306e\u7d71\u5408\u958b\u767a\u74b0\u5883\uff08IDE\uff09\u3068\u3057\u3066\u3001\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u3067\u8efd\u91cf\u306aVS Code\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002VS Code\u516c\u5f0f\u30a6\u30a7\u30d6\u30b5\u30a4\u30c8\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3001\u304a\u4f7f\u3044\u306e\u30aa\u30da\u30ec\u30fc\u30c6\u30a3\u30f3\u30b0\u30b7\u30b9\u30c6\u30e0\u306b\u9069\u3057\u305fVS Code\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u9078\u629e\u3057\u3066\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3001\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u56f3 16-1 \u00a0 \u516c\u5f0f\u30a6\u30a7\u30d6\u30b5\u30a4\u30c8\u304b\u3089VS Code\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9

        VS Code\u306b\u306f\u5f37\u529b\u306a\u62e1\u5f35\u6a5f\u80fd\u30a8\u30b3\u30b7\u30b9\u30c6\u30e0\u304c\u3042\u308a\u3001\u307b\u3068\u3093\u3069\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u5b9f\u884c\u3068\u30c7\u30d0\u30c3\u30b0\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u300cPython Extension Pack\u300d\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u305f\u5f8c\u3001Python\u30b3\u30fc\u30c9\u3092\u30c7\u30d0\u30c3\u30b0\u3067\u304d\u307e\u3059\u3002\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u624b\u9806\u3092\u4e0b\u306e\u56f3\u306b\u793a\u3057\u307e\u3059\u3002

        \u56f3 16-2 \u00a0 VS Code\u62e1\u5f35\u6a5f\u80fd\u30d1\u30c3\u30af\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb

        "},{"location":"chapter_appendix/installation/#1612","title":"16.1.2 \u00a0 \u8a00\u8a9e\u74b0\u5883\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb","text":""},{"location":"chapter_appendix/installation/#1-python","title":"1. \u00a0 Python\u74b0\u5883","text":"
        1. Miniconda3\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002Python 3.10\u4ee5\u964d\u304c\u5fc5\u8981\u3067\u3059\u3002
        2. VS Code\u62e1\u5f35\u6a5f\u80fd\u30de\u30fc\u30b1\u30c3\u30c8\u30d7\u30ec\u30a4\u30b9\u3067python\u3092\u691c\u7d22\u3057\u3001Python Extension Pack\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        3. \uff08\u30aa\u30d7\u30b7\u30e7\u30f3\uff09\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u3067pip install black\u3092\u5165\u529b\u3057\u3066\u3001\u30b3\u30fc\u30c9\u30d5\u30a9\u30fc\u30de\u30c3\u30c6\u30a3\u30f3\u30b0\u30c4\u30fc\u30eb\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        "},{"location":"chapter_appendix/installation/#2-cc","title":"2. \u00a0 C/C++\u74b0\u5883","text":"
        1. Windows\u30b7\u30b9\u30c6\u30e0\u3067\u306fMinGW\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff08\u8a2d\u5b9a\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\uff09\u3002MacOS\u306b\u306fClang\u304c\u4ed8\u5c5e\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u306f\u4e0d\u8981\u3067\u3059\u3002
        2. VS Code\u62e1\u5f35\u6a5f\u80fd\u30de\u30fc\u30b1\u30c3\u30c8\u30d7\u30ec\u30a4\u30b9\u3067c++\u3092\u691c\u7d22\u3057\u3001C/C++ Extension Pack\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        3. \uff08\u30aa\u30d7\u30b7\u30e7\u30f3\uff09\u8a2d\u5b9a\u30da\u30fc\u30b8\u3092\u958b\u304d\u3001Clang_format_fallback Style\u30b3\u30fc\u30c9\u30d5\u30a9\u30fc\u30de\u30c3\u30c6\u30a3\u30f3\u30b0\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u691c\u7d22\u3057\u3001{ BasedOnStyle: Microsoft, BreakBeforeBraces: Attach }\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002
        "},{"location":"chapter_appendix/installation/#3-java","title":"3. \u00a0 Java\u74b0\u5883","text":"
        1. OpenJDK\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\uff08\u30d0\u30fc\u30b8\u30e7\u30f3\u306fJDK 9\u3088\u308a\u65b0\u3057\u3044\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff09\u3002
        2. VS Code\u62e1\u5f35\u6a5f\u80fd\u30de\u30fc\u30b1\u30c3\u30c8\u30d7\u30ec\u30a4\u30b9\u3067java\u3092\u691c\u7d22\u3057\u3001Extension Pack for Java\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        "},{"location":"chapter_appendix/installation/#4-c","title":"4. \u00a0 C#\u74b0\u5883","text":"
        1. .Net 8.0\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        2. VS Code\u62e1\u5f35\u6a5f\u80fd\u30de\u30fc\u30b1\u30c3\u30c8\u30d7\u30ec\u30a4\u30b9\u3067C# Dev Kit\u3092\u691c\u7d22\u3057\u3001C# Dev Kit\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\uff08\u8a2d\u5b9a\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\uff09\u3002
        3. Visual Studio\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\uff08\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\uff09\u3002
        "},{"location":"chapter_appendix/installation/#5-go","title":"5. \u00a0 Go\u74b0\u5883","text":"
        1. go\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        2. VS Code\u62e1\u5f35\u6a5f\u80fd\u30de\u30fc\u30b1\u30c3\u30c8\u30d7\u30ec\u30a4\u30b9\u3067go\u3092\u691c\u7d22\u3057\u3001Go\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        3. Ctrl + Shift + P\u3092\u62bc\u3057\u3066\u30b3\u30de\u30f3\u30c9\u30d0\u30fc\u3092\u547c\u3073\u51fa\u3057\u3001go\u3068\u5165\u529b\u3057\u3001Go: Install/Update Tools\u3092\u9078\u629e\u3057\u3001\u3059\u3079\u3066\u3092\u9078\u629e\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        "},{"location":"chapter_appendix/installation/#6-swift","title":"6. \u00a0 Swift\u74b0\u5883","text":"
        1. Swift\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        2. VS Code\u62e1\u5f35\u6a5f\u80fd\u30de\u30fc\u30b1\u30c3\u30c8\u30d7\u30ec\u30a4\u30b9\u3067swift\u3092\u691c\u7d22\u3057\u3001Swift for Visual Studio Code\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        "},{"location":"chapter_appendix/installation/#7-javascript","title":"7. \u00a0 JavaScript\u74b0\u5883","text":"
        1. Node.js\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        2. \uff08\u30aa\u30d7\u30b7\u30e7\u30f3\uff09VS Code\u62e1\u5f35\u6a5f\u80fd\u30de\u30fc\u30b1\u30c3\u30c8\u30d7\u30ec\u30a4\u30b9\u3067Prettier\u3092\u691c\u7d22\u3057\u3001\u30b3\u30fc\u30c9\u30d5\u30a9\u30fc\u30de\u30c3\u30c6\u30a3\u30f3\u30b0\u30c4\u30fc\u30eb\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        "},{"location":"chapter_appendix/installation/#8-typescript","title":"8. \u00a0 TypeScript\u74b0\u5883","text":"
        1. JavaScript\u74b0\u5883\u3068\u540c\u3058\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u624b\u9806\u306b\u5f93\u3044\u307e\u3059\u3002
        2. TypeScript Execute (tsx)\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        3. VS Code\u62e1\u5f35\u6a5f\u80fd\u30de\u30fc\u30b1\u30c3\u30c8\u30d7\u30ec\u30a4\u30b9\u3067typescript\u3092\u691c\u7d22\u3057\u3001Pretty TypeScript Errors\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        "},{"location":"chapter_appendix/installation/#9-dart","title":"9. \u00a0 Dart\u74b0\u5883","text":"
        1. Dart\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        2. VS Code\u62e1\u5f35\u6a5f\u80fd\u30de\u30fc\u30b1\u30c3\u30c8\u30d7\u30ec\u30a4\u30b9\u3067dart\u3092\u691c\u7d22\u3057\u3001Dart\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        "},{"location":"chapter_appendix/installation/#10-rust","title":"10. \u00a0 Rust\u74b0\u5883","text":"
        1. Rust\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        2. VS Code\u62e1\u5f35\u6a5f\u80fd\u30de\u30fc\u30b1\u30c3\u30c8\u30d7\u30ec\u30a4\u30b9\u3067rust\u3092\u691c\u7d22\u3057\u3001rust-analyzer\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
        "},{"location":"chapter_appendix/terminology/","title":"16.3 \u00a0 \u7528\u8a9e\u96c6","text":"

        \u4e0b\u306e\u8868\u306f\u672c\u66f8\u306b\u767b\u5834\u3059\u308b\u91cd\u8981\u306a\u7528\u8a9e\u3092\u30ea\u30b9\u30c8\u30a2\u30c3\u30d7\u3057\u3066\u304a\u308a\u3001\u4ee5\u4e0b\u306e\u70b9\u306b\u6ce8\u610f\u3059\u308b\u4fa1\u5024\u304c\u3042\u308a\u307e\u3059\u3002

        • \u82f1\u8a9e\u6587\u732e\u3092\u8aad\u307f\u3084\u3059\u304f\u3059\u308b\u305f\u3081\u3001\u7528\u8a9e\u306e\u82f1\u8a9e\u540d\u3092\u899a\u3048\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002
        • \u4e00\u90e8\u306e\u7528\u8a9e\u306f\u7c21\u4f53\u5b57\u4e2d\u56fd\u8a9e\u3068\u7e41\u4f53\u5b57\u4e2d\u56fd\u8a9e\u3067\u7570\u306a\u308b\u540d\u524d\u3092\u6301\u3061\u307e\u3059\u3002

        \u8868 16-1 \u00a0 \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u91cd\u8981\u7528\u8a9e

        English \u65e5\u672c\u8a9e \u7b80\u4f53\u4e2d\u6587 \u7e41\u4f53\u4e2d\u6587 algorithm \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0 \u7b97\u6cd5 \u6f14\u7b97\u6cd5 data structure \u30c7\u30fc\u30bf\u69cb\u9020 \u6570\u636e\u7ed3\u6784 \u8cc7\u6599\u7d50\u69cb code \u30b3\u30fc\u30c9 \u4ee3\u7801 \u7a0b\u5f0f\u78bc file \u30d5\u30a1\u30a4\u30eb \u6587\u4ef6 \u6a94\u6848 function \u95a2\u6570 \u51fd\u6570 \u51fd\u5f0f method \u30e1\u30bd\u30c3\u30c9 \u65b9\u6cd5 \u65b9\u6cd5 variable \u5909\u6570 \u53d8\u91cf \u8b8a\u6578 asymptotic complexity analysis \u6f38\u8fd1\u8a08\u7b97\u91cf\u89e3\u6790 \u6e10\u8fd1\u590d\u6742\u5ea6\u5206\u6790 \u6f38\u8fd1\u8907\u96dc\u5ea6\u5206\u6790 time complexity \u6642\u9593\u8a08\u7b97\u91cf \u65f6\u95f4\u590d\u6742\u5ea6 \u6642\u9593\u8907\u96dc\u5ea6 space complexity \u7a7a\u9593\u8a08\u7b97\u91cf \u7a7a\u95f4\u590d\u6742\u5ea6 \u7a7a\u9593\u8907\u96dc\u5ea6 loop \u30eb\u30fc\u30d7 \u5faa\u73af \u8ff4\u5708 iteration \u53cd\u5fa9 \u8fed\u4ee3 \u8fed\u4ee3 recursion \u518d\u5e30 \u9012\u5f52 \u905e\u8ff4 tail recursion \u672b\u5c3e\u518d\u5e30 \u5c3e\u9012\u5f52 \u5c3e\u905e\u8ff4 recursion tree \u518d\u5e30\u6728 \u9012\u5f52\u6811 \u905e\u8ff4\u6a39 big-\\(O\\) notation \u30d3\u30c3\u30b0O\u8a18\u6cd5 \u5927 \\(O\\) \u8bb0\u53f7 \u5927 \\(O\\) \u8a18\u865f asymptotic upper bound \u6f38\u8fd1\u4e0a\u754c \u6e10\u8fd1\u4e0a\u754c \u6f38\u8fd1\u4e0a\u754c sign-magnitude \u7b26\u53f7\u3068\u7d76\u5bfe\u5024 \u539f\u7801 \u539f\u78bc 1's complement 1\u306e\u88dc\u6570 \u53cd\u7801 \u4e00\u88dc\u6578 2's complement 2\u306e\u88dc\u6570 \u8865\u7801 \u4e8c\u88dc\u6578 array \u914d\u5217 \u6570\u7ec4 \u9663\u5217 index \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 \u7d22\u5f15 \u7d22\u5f15 linked list \u9023\u7d50\u30ea\u30b9\u30c8 \u94fe\u8868 \u93c8\u7d50\u4e32\u5217 linked list node, list node \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9 \u94fe\u8868\u8282\u70b9 \u93c8\u7d50\u4e32\u5217\u7bc0\u9ede head node \u5148\u982d\u30ce\u30fc\u30c9 \u5934\u8282\u70b9 \u982d\u7bc0\u9ede tail node \u672b\u5c3e\u30ce\u30fc\u30c9 \u5c3e\u8282\u70b9 \u5c3e\u7bc0\u9ede list \u30ea\u30b9\u30c8 \u5217\u8868 \u4e32\u5217 dynamic array \u52d5\u7684\u914d\u5217 \u52a8\u6001\u6570\u7ec4 \u52d5\u614b\u9663\u5217 hard disk \u30cf\u30fc\u30c9\u30c7\u30a3\u30b9\u30af \u786c\u76d8 \u786c\u789f random-access memory (RAM) \u30e1\u30e2\u30ea \u5185\u5b58 \u8a18\u61b6\u9ad4 cache memory \u30ad\u30e3\u30c3\u30b7\u30e5\u30e1\u30e2\u30ea \u7f13\u5b58 \u5feb\u53d6 cache miss \u30ad\u30e3\u30c3\u30b7\u30e5\u30df\u30b9 \u7f13\u5b58\u672a\u547d\u4e2d \u5feb\u53d6\u672a\u547d\u4e2d cache hit rate \u30ad\u30e3\u30c3\u30b7\u30e5\u30d2\u30c3\u30c8\u7387 \u7f13\u5b58\u547d\u4e2d\u7387 \u5feb\u53d6\u547d\u4e2d\u7387 stack \u30b9\u30bf\u30c3\u30af \u6808 \u5806\u758a top of the stack \u30b9\u30bf\u30c3\u30af\u30c8\u30c3\u30d7 \u6808\u9876 \u5806\u758a\u9802 bottom of the stack \u30b9\u30bf\u30c3\u30af\u30dc\u30c8\u30e0 \u6808\u5e95 \u5806\u758a\u5e95 queue \u30ad\u30e5\u30fc \u961f\u5217 \u4f47\u5217 double-ended queue \u4e21\u7aef\u30ad\u30e5\u30fc \u53cc\u5411\u961f\u5217 \u96d9\u5411\u4f47\u5217 front of the queue \u30ad\u30e5\u30fc\u306e\u5148\u982d \u961f\u9996 \u4f47\u5217\u9996 rear of the queue \u30ad\u30e5\u30fc\u306e\u672b\u5c3e \u961f\u5c3e \u4f47\u5217\u5c3e hash table \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb \u54c8\u5e0c\u8868 \u96dc\u6e4a\u8868 hash set \u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8 \u54c8\u5e0c\u96c6\u5408 \u96dc\u6e4a\u96c6\u5408 bucket \u30d0\u30b1\u30c3\u30c8 \u6876 \u6876 hash function \u30cf\u30c3\u30b7\u30e5\u95a2\u6570 \u54c8\u5e0c\u51fd\u6570 \u96dc\u6e4a\u51fd\u5f0f hash collision \u30cf\u30c3\u30b7\u30e5\u885d\u7a81 \u54c8\u5e0c\u51b2\u7a81 \u96dc\u6e4a\u885d\u7a81 load factor \u8ca0\u8377\u7387 \u8d1f\u8f7d\u56e0\u5b50 \u8ca0\u8f09\u56e0\u5b50 separate chaining \u30c1\u30a7\u30a4\u30f3\u6cd5 \u94fe\u5f0f\u5730\u5740 \u93c8\u7d50\u4f4d\u5740 open addressing \u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5 \u5f00\u653e\u5bfb\u5740 \u958b\u653e\u5b9a\u5740 linear probing \u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u6cd5 \u7ebf\u6027\u63a2\u6d4b \u7dda\u6027\u63a2\u67e5 lazy deletion \u9045\u5ef6\u524a\u9664 \u61d2\u5220\u9664 \u61f6\u522a\u9664 binary tree \u4e8c\u5206\u6728 \u4e8c\u53c9\u6811 \u4e8c\u5143\u6a39 tree node \u6728\u306e\u30ce\u30fc\u30c9 \u6811\u8282\u70b9 \u6a39\u7bc0\u9ede left-child node \u5de6\u306e\u5b50\u30ce\u30fc\u30c9 \u5de6\u5b50\u8282\u70b9 \u5de6\u5b50\u7bc0\u9ede right-child node \u53f3\u306e\u5b50\u30ce\u30fc\u30c9 \u53f3\u5b50\u8282\u70b9 \u53f3\u5b50\u7bc0\u9ede parent node \u89aa\u30ce\u30fc\u30c9 \u7236\u8282\u70b9 \u7236\u7bc0\u9ede left subtree \u5de6\u306e\u90e8\u5206\u6728 \u5de6\u5b50\u6811 \u5de6\u5b50\u6a39 right subtree \u53f3\u306e\u90e8\u5206\u6728 \u53f3\u5b50\u6811 \u53f3\u5b50\u6a39 root node \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9 \u6839\u8282\u70b9 \u6839\u7bc0\u9ede leaf node \u8449\u30ce\u30fc\u30c9 \u53f6\u8282\u70b9 \u8449\u7bc0\u9ede edge \u30a8\u30c3\u30b8 \u8fb9 \u908a level \u30ec\u30d9\u30eb \u5c42 \u5c64 degree \u6b21\u6570 \u5ea6 \u5ea6 height \u9ad8\u3055 \u9ad8\u5ea6 \u9ad8\u5ea6 depth \u6df1\u3055 \u6df1\u5ea6 \u6df1\u5ea6 perfect binary tree \u5b8c\u5168\u4e8c\u5206\u6728 \u5b8c\u7f8e\u4e8c\u53c9\u6811 \u5b8c\u7f8e\u4e8c\u5143\u6a39 complete binary tree \u5b8c\u5168\u4e8c\u5206\u6728 \u5b8c\u5168\u4e8c\u53c9\u6811 \u5b8c\u5168\u4e8c\u5143\u6a39 full binary tree \u6e80\u4e8c\u5206\u6728 \u5b8c\u6ee1\u4e8c\u53c9\u6811 \u5b8c\u6eff\u4e8c\u5143\u6a39 balanced binary tree \u5e73\u8861\u4e8c\u5206\u6728 \u5e73\u8861\u4e8c\u53c9\u6811 \u5e73\u8861\u4e8c\u5143\u6a39 binary search tree \u4e8c\u5206\u63a2\u7d22\u6728 \u4e8c\u53c9\u641c\u7d22\u6811 \u4e8c\u5143\u641c\u5c0b\u6a39 AVL tree AVL\u6728 AVL \u6811 AVL \u6a39 red-black tree \u8d64\u9ed2\u6728 \u7ea2\u9ed1\u6811 \u7d05\u9ed1\u6a39 level-order traversal \u30ec\u30d9\u30eb\u9806\u8d70\u67fb \u5c42\u5e8f\u904d\u5386 \u5c64\u5e8f\u8d70\u8a2a breadth-first traversal \u5e45\u512a\u5148\u8d70\u67fb \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 \u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a depth-first traversal \u6df1\u3055\u512a\u5148\u8d70\u67fb \u6df1\u5ea6\u4f18\u5148\u904d\u5386 \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a binary search tree \u4e8c\u5206\u63a2\u7d22\u6728 \u4e8c\u53c9\u641c\u7d22\u6811 \u4e8c\u5143\u641c\u5c0b\u6a39 balanced binary search tree \u5e73\u8861\u4e8c\u5206\u63a2\u7d22\u6728 \u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811 \u5e73\u8861\u4e8c\u5143\u641c\u5c0b\u6a39 balance factor \u5e73\u8861\u56e0\u5b50 \u5e73\u8861\u56e0\u5b50 \u5e73\u8861\u56e0\u5b50 heap \u30d2\u30fc\u30d7 \u5806 \u5806\u7a4d max heap \u6700\u5927\u30d2\u30fc\u30d7 \u5927\u9876\u5806 \u5927\u9802\u5806\u7a4d min heap \u6700\u5c0f\u30d2\u30fc\u30d7 \u5c0f\u9876\u5806 \u5c0f\u9802\u5806\u7a4d priority queue \u512a\u5148\u5ea6\u30ad\u30e5\u30fc \u4f18\u5148\u961f\u5217 \u512a\u5148\u4f47\u5217 heapify \u30d2\u30fc\u30d7\u5316 \u5806\u5316 \u5806\u7a4d\u5316 top-\\(k\\) problem Top-\\(k\\) \u554f\u984c Top-\\(k\\) \u95ee\u9898 Top-\\(k\\) \u554f\u984c graph \u30b0\u30e9\u30d5 \u56fe \u5716 vertex \u9802\u70b9 \u9876\u70b9 \u9802\u9ede undirected graph \u7121\u5411\u30b0\u30e9\u30d5 \u65e0\u5411\u56fe \u7121\u5411\u5716 directed graph \u6709\u5411\u30b0\u30e9\u30d5 \u6709\u5411\u56fe \u6709\u5411\u5716 connected graph \u9023\u7d50\u30b0\u30e9\u30d5 \u8fde\u901a\u56fe \u9023\u901a\u5716 disconnected graph \u975e\u9023\u7d50\u30b0\u30e9\u30d5 \u975e\u8fde\u901a\u56fe \u975e\u9023\u901a\u5716 weighted graph \u91cd\u307f\u4ed8\u304d\u30b0\u30e9\u30d5 \u6709\u6743\u56fe \u6709\u6b0a\u5716 adjacency \u96a3\u63a5 \u90bb\u63a5 \u9130\u63a5 path \u30d1\u30b9 \u8def\u5f84 \u8def\u5f91 in-degree \u5165\u6b21\u6570 \u5165\u5ea6 \u5165\u5ea6 out-degree \u51fa\u6b21\u6570 \u51fa\u5ea6 \u51fa\u5ea6 adjacency matrix \u96a3\u63a5\u884c\u5217 \u90bb\u63a5\u77e9\u9635 \u9130\u63a5\u77e9\u9663 adjacency list \u96a3\u63a5\u30ea\u30b9\u30c8 \u90bb\u63a5\u8868 \u9130\u63a5\u8868 breadth-first search \u5e45\u512a\u5148\u63a2\u7d22 \u5e7f\u5ea6\u4f18\u5148\u641c\u7d22 \u5ee3\u5ea6\u512a\u5148\u641c\u5c0b depth-first search \u6df1\u3055\u512a\u5148\u63a2\u7d22 \u6df1\u5ea6\u4f18\u5148\u641c\u7d22 \u6df1\u5ea6\u512a\u5148\u641c\u5c0b binary search \u4e8c\u5206\u63a2\u7d22 \u4e8c\u5206\u67e5\u627e \u4e8c\u5206\u641c\u5c0b searching algorithm \u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0 \u641c\u7d22\u7b97\u6cd5 \u641c\u5c0b\u6f14\u7b97\u6cd5 sorting algorithm \u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0 \u6392\u5e8f\u7b97\u6cd5 \u6392\u5e8f\u6f14\u7b97\u6cd5 selection sort \u9078\u629e\u30bd\u30fc\u30c8 \u9009\u62e9\u6392\u5e8f \u9078\u64c7\u6392\u5e8f bubble sort \u30d0\u30d6\u30eb\u30bd\u30fc\u30c8 \u5192\u6ce1\u6392\u5e8f \u6ce1\u6cab\u6392\u5e8f insertion sort \u633f\u5165\u30bd\u30fc\u30c8 \u63d2\u5165\u6392\u5e8f \u63d2\u5165\u6392\u5e8f quick sort \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8 \u5feb\u901f\u6392\u5e8f \u5feb\u901f\u6392\u5e8f merge sort \u30de\u30fc\u30b8\u30bd\u30fc\u30c8 \u5f52\u5e76\u6392\u5e8f \u5408\u4f75\u6392\u5e8f heap sort \u30d2\u30fc\u30d7\u30bd\u30fc\u30c8 \u5806\u6392\u5e8f \u5806\u7a4d\u6392\u5e8f bucket sort \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8 \u6876\u6392\u5e8f \u6876\u6392\u5e8f counting sort \u8a08\u6570\u30bd\u30fc\u30c8 \u8ba1\u6570\u6392\u5e8f \u8a08\u6578\u6392\u5e8f radix sort \u57fa\u6570\u30bd\u30fc\u30c8 \u57fa\u6570\u6392\u5e8f \u57fa\u6578\u6392\u5e8f divide and conquer \u5206\u5272\u7d71\u6cbb\u6cd5 \u5206\u6cbb \u5206\u6cbb hanota problem \u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c \u6c49\u8bfa\u5854\u95ee\u9898 \u6cb3\u5167\u5854\u554f\u984c backtracking algorithm \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0 \u56de\u6eaf\u7b97\u6cd5 \u56de\u6eaf\u6f14\u7b97\u6cd5 constraint \u5236\u7d04 \u7ea6\u675f \u7d04\u675f solution \u89e3 \u89e3 \u89e3 state \u72b6\u614b \u72b6\u6001 \u72c0\u614b pruning \u679d\u5208\u308a \u526a\u679d \u526a\u679d permutations problem \u9806\u5217\u554f\u984c \u5168\u6392\u5217\u95ee\u9898 \u5168\u6392\u5217\u554f\u984c subset-sum problem \u90e8\u5206\u96c6\u5408\u548c\u554f\u984c \u5b50\u96c6\u548c\u95ee\u9898 \u5b50\u96c6\u5408\u554f\u984c \\(n\\)-queens problem \\(n\\) \u30af\u30a4\u30fc\u30f3\u554f\u984c \\(n\\) \u7687\u540e\u95ee\u9898 \\(n\\) \u7687\u540e\u554f\u984c dynamic programming \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 \u52a8\u6001\u89c4\u5212 \u52d5\u614b\u898f\u5283 initial state \u521d\u671f\u72b6\u614b \u521d\u59cb\u72b6\u6001 \u521d\u59cb\u72c0\u614b state-transition equation \u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f \u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b \u72c0\u614b\u8f49\u79fb\u65b9\u7a0b knapsack problem \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c \u80cc\u5305\u95ee\u9898 \u80cc\u5305\u554f\u984c edit distance problem \u7de8\u96c6\u8ddd\u96e2\u554f\u984c \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898 \u7de8\u8f2f\u8ddd\u96e2\u554f\u984c greedy algorithm \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0 \u8d2a\u5fc3\u7b97\u6cd5 \u8caa\u5a6a\u6f14\u7b97\u6cd5"},{"location":"chapter_array_and_linkedlist/","title":"\u7b2c 4 \u7ae0 \u00a0 \u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8","text":"

        Abstract

        \u30c7\u30fc\u30bf\u69cb\u9020\u306e\u4e16\u754c\u306f\u9811\u4e08\u306a\u30ec\u30f3\u30ac\u306e\u58c1\u306b\u4f3c\u3066\u3044\u307e\u3059\u3002

        \u914d\u5217\u3067\u306f\u3001\u30ec\u30f3\u30ac\u304c\u3074\u3063\u305f\u308a\u3068\u6574\u5217\u3057\u3001\u305d\u308c\u305e\u308c\u304c\u6b21\u306e\u3082\u306e\u3068\u7d99\u304e\u76ee\u306a\u304f\u96a3\u308a\u5408\u3063\u3066\u3001\u7d71\u4e00\u3055\u308c\u305f\u5f62\u6210\u3092\u4f5c\u3063\u3066\u3044\u308b\u59ff\u3092\u60f3\u50cf\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4e00\u65b9\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u306f\u3001\u3053\u308c\u3089\u306e\u30ec\u30f3\u30ac\u304c\u81ea\u7531\u306b\u6563\u3089\u3070\u308a\u3001\u305d\u308c\u3089\u306e\u9593\u3092\u512a\u96c5\u306b\u7de8\u307f\u8fbc\u3080\u8526\u306b\u62b1\u304b\u308c\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_array_and_linkedlist/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 4.1 \u00a0 \u914d\u5217
        • 4.2 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8
        • 4.3 \u00a0 \u30ea\u30b9\u30c8
        • 4.4 \u00a0 \u30e1\u30e2\u30ea\u3068\u30ad\u30e3\u30c3\u30b7\u30e5 *
        • 4.5 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_array_and_linkedlist/array/","title":"4.1 \u00a0 \u914d\u5217","text":"

        \u914d\u5217\u306f\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3001\u540c\u3058\u3088\u3046\u306a\u9805\u76ee\u304c\u4e26\u3093\u3067\u3044\u308b\u3088\u3046\u306a\u3082\u306e\u3067\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u30e1\u30e2\u30ea\u5185\u306e\u9023\u7d9a\u3057\u305f\u7a7a\u9593\u306b\u4e00\u7dd2\u306b\u683c\u7d0d\u3055\u308c\u307e\u3059\u3002\u3053\u308c\u306f\u6574\u7406\u3055\u308c\u305f\u683c\u7d0d\u3092\u7dad\u6301\u3059\u308b\u30b7\u30fc\u30b1\u30f3\u30b9\u306e\u3088\u3046\u306a\u3082\u306e\u3067\u3059\u3002\u3053\u306e\u4e26\u3073\u306e\u5404\u9805\u76ee\u306b\u306f\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3068\u3057\u3066\u77e5\u3089\u308c\u308b\u72ec\u81ea\u306e\u300c\u4f4d\u7f6e\u300d\u304c\u3042\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u3092\u53c2\u7167\u3057\u3066\u3001\u914d\u5217\u306e\u52d5\u4f5c\u3092\u89b3\u5bdf\u3057\u3001\u3053\u308c\u3089\u306e\u91cd\u8981\u306a\u7528\u8a9e\u3092\u7406\u89e3\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u56f3 4-1 \u00a0 \u914d\u5217\u306e\u5b9a\u7fa9\u3068\u683c\u7d0d\u65b9\u6cd5

        "},{"location":"chapter_array_and_linkedlist/array/#411","title":"4.1.1 \u00a0 \u914d\u5217\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/array/#1","title":"1. \u00a0 \u914d\u5217\u306e\u521d\u671f\u5316","text":"

        \u914d\u5217\u306f\u5fc5\u8981\u306b\u5fdc\u3058\u30662\u3064\u306e\u65b9\u6cd5\u3067\u521d\u671f\u5316\u3067\u304d\u307e\u3059\uff1a\u521d\u671f\u5024\u306a\u3057\u307e\u305f\u306f\u6307\u5b9a\u3055\u308c\u305f\u521d\u671f\u5024\u4ed8\u304d\u3067\u3059\u3002\u521d\u671f\u5024\u304c\u6307\u5b9a\u3055\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u3001\u307b\u3068\u3093\u3069\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306f\u914d\u5217\u8981\u7d20\u3092\\(0\\)\u306b\u8a2d\u5b9a\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin array.py
        # \u914d\u5217\u3092\u521d\u671f\u5316\narr: list[int] = [0] * 5  # [ 0, 0, 0, 0, 0 ]\nnums: list[int] = [1, 3, 2, 5, 4]\n
        array.cpp
        /* \u914d\u5217\u3092\u521d\u671f\u5316 */\n// \u30b9\u30bf\u30c3\u30af\u306b\u683c\u7d0d\nint arr[5];\nint nums[5] = { 1, 3, 2, 5, 4 };\n// \u30d2\u30fc\u30d7\u306b\u683c\u7d0d\uff08\u624b\u52d5\u3067\u306e\u30e1\u30e2\u30ea\u89e3\u653e\u304c\u5fc5\u8981\uff09\nint* arr1 = new int[5];\nint* nums1 = new int[5] { 1, 3, 2, 5, 4 };\n
        array.java
        /* \u914d\u5217\u3092\u521d\u671f\u5316 */\nint[] arr = new int[5]; // { 0, 0, 0, 0, 0 }\nint[] nums = { 1, 3, 2, 5, 4 };\n
        array.cs
        /* \u914d\u5217\u3092\u521d\u671f\u5316 */\nint[] arr = new int[5]; // [ 0, 0, 0, 0, 0 ]\nint[] nums = [1, 3, 2, 5, 4];\n
        array.go
        /* \u914d\u5217\u3092\u521d\u671f\u5316 */\nvar arr [5]int\n// Go\u3067\u306f\u3001\u9577\u3055\u3092\u6307\u5b9a\uff08[5]int\uff09\u3059\u308b\u3068\u914d\u5217\u3092\u793a\u3057\u3001\u6307\u5b9a\u3057\u306a\u3044\uff08[]int\uff09\u3068\u30b9\u30e9\u30a4\u30b9\u3092\u793a\u3057\u307e\u3059\u3002\n// Go\u306e\u914d\u5217\u306f\u30b3\u30f3\u30d1\u30a4\u30eb\u6642\u306b\u56fa\u5b9a\u9577\u3092\u6301\u3064\u3088\u3046\u8a2d\u8a08\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u9577\u3055\u306e\u6307\u5b9a\u306b\u306f\u5b9a\u6570\u306e\u307f\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\n// extend()\u30e1\u30bd\u30c3\u30c9\u306e\u5b9f\u88c5\u306e\u4fbf\u5b9c\u4e0a\u3001\u3053\u3053\u3067\u306fSlice\u3092\u914d\u5217\u3068\u3057\u3066\u6271\u3044\u307e\u3059\u3002\nnums := []int{1, 3, 2, 5, 4}\n
        array.swift
        /* \u914d\u5217\u3092\u521d\u671f\u5316 */\nlet arr = Array(repeating: 0, count: 5) // [0, 0, 0, 0, 0]\nlet nums = [1, 3, 2, 5, 4]\n
        array.js
        /* \u914d\u5217\u3092\u521d\u671f\u5316 */\nvar arr = new Array(5).fill(0);\nvar nums = [1, 3, 2, 5, 4];\n
        array.ts
        /* \u914d\u5217\u3092\u521d\u671f\u5316 */\nlet arr: number[] = new Array(5).fill(0);\nlet nums: number[] = [1, 3, 2, 5, 4];\n
        array.dart
        /* \u914d\u5217\u3092\u521d\u671f\u5316 */\nList<int> arr = List.filled(5, 0); // [0, 0, 0, 0, 0]\nList<int> nums = [1, 3, 2, 5, 4];\n
        array.rs
        /* \u914d\u5217\u3092\u521d\u671f\u5316 */\nlet arr: [i32; 5] = [0; 5]; // [0, 0, 0, 0, 0]\nlet slice: &[i32] = &[0; 5];\n// Rust\u3067\u306f\u3001\u9577\u3055\u3092\u6307\u5b9a\uff08[i32; 5]\uff09\u3059\u308b\u3068\u914d\u5217\u3092\u793a\u3057\u3001\u6307\u5b9a\u3057\u306a\u3044\uff08&[i32]\uff09\u3068\u30b9\u30e9\u30a4\u30b9\u3092\u793a\u3057\u307e\u3059\u3002\n// Rust\u306e\u914d\u5217\u306f\u30b3\u30f3\u30d1\u30a4\u30eb\u6642\u306b\u56fa\u5b9a\u9577\u3092\u6301\u3064\u3088\u3046\u8a2d\u8a08\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u9577\u3055\u306e\u6307\u5b9a\u306b\u306f\u5b9a\u6570\u306e\u307f\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\n// \u4e00\u822c\u7684\u306bRust\u3067\u306f\u52d5\u7684\u914d\u5217\u3068\u3057\u3066Vector\u304c\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\n// extend()\u30e1\u30bd\u30c3\u30c9\u306e\u5b9f\u88c5\u306e\u4fbf\u5b9c\u4e0a\u3001\u3053\u3053\u3067\u306f\u30d9\u30af\u30bf\u30fc\u3092\u914d\u5217\u3068\u3057\u3066\u6271\u3044\u307e\u3059\u3002\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
        array.c
        /* \u914d\u5217\u3092\u521d\u671f\u5316 */\nint arr[5] = { 0 }; // { 0, 0, 0, 0, 0 }\nint nums[5] = { 1, 3, 2, 5, 4 };\n
        array.kt
        \n
        "},{"location":"chapter_array_and_linkedlist/array/#2","title":"2. \u00a0 \u8981\u7d20\u3078\u306e\u30a2\u30af\u30bb\u30b9","text":"

        \u914d\u5217\u5185\u306e\u8981\u7d20\u306f\u9023\u7d9a\u3057\u305f\u30e1\u30e2\u30ea\u7a7a\u9593\u306b\u683c\u7d0d\u3055\u308c\u308b\u305f\u3081\u3001\u5404\u8981\u7d20\u306e\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u3092\u8a08\u7b97\u3059\u308b\u3053\u3068\u304c\u7c21\u5358\u306b\u306a\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u308b\u516c\u5f0f\u306f\u3001\u914d\u5217\u306e\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\uff08\u7279\u306b\u3001\u6700\u521d\u306e\u8981\u7d20\u306e\u30a2\u30c9\u30ec\u30b9\uff09\u3068\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u5229\u7528\u3057\u3066\u3001\u8981\u7d20\u306e\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u3092\u6c7a\u5b9a\u3059\u308b\u306e\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002\u3053\u306e\u8a08\u7b97\u306b\u3088\u308a\u3001\u76ee\u7684\u306e\u8981\u7d20\u3078\u306e\u76f4\u63a5\u30a2\u30af\u30bb\u30b9\u304c\u5408\u7406\u5316\u3055\u308c\u307e\u3059\u3002

        \u56f3 4-2 \u00a0 \u914d\u5217\u8981\u7d20\u306e\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u8a08\u7b97

        \u4e0a\u306e\u56f3\u3067\u89b3\u5bdf\u3055\u308c\u308b\u3088\u3046\u306b\u3001\u914d\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u6163\u4f8b\u7684\u306b\\(0\\)\u304b\u3089\u59cb\u307e\u308a\u307e\u3059\u3002\u3053\u308c\u306f\u76f4\u611f\u306b\u53cd\u3059\u308b\u3088\u3046\u306b\u898b\u3048\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u6570\u3092\u6570\u3048\u308b\u306e\u306f\u901a\u5e38\\(1\\)\u304b\u3089\u59cb\u307e\u308b\u305f\u3081\u3067\u3059\u304c\u3001\u30a2\u30c9\u30ec\u30b9\u8a08\u7b97\u516c\u5f0f\u5185\u3067\u306f\u3001**\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u672c\u8cea\u7684\u306b\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u304b\u3089\u306e\u30aa\u30d5\u30bb\u30c3\u30c8**\u3067\u3059\u3002\u6700\u521d\u306e\u8981\u7d20\u306e\u30a2\u30c9\u30ec\u30b9\u3067\u306f\u3001\u3053\u306e\u30aa\u30d5\u30bb\u30c3\u30c8\u306f\\(0\\)\u3067\u3001\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\\(0\\)\u3067\u3042\u308b\u3053\u3068\u3092\u691c\u8a3c\u3057\u3066\u3044\u307e\u3059\u3002

        \u914d\u5217\u5185\u306e\u8981\u7d20\u3078\u306e\u30a2\u30af\u30bb\u30b9\u306f\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3001\\(O(1)\\)\u6642\u9593\u3067\u4efb\u610f\u306e\u8981\u7d20\u306b\u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\u3067\u304d\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def random_access(nums: list[int]) -> int:\n    \"\"\"\u8981\u7d20\u3078\u306e\u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\"\"\"\n    # \u533a\u9593 [0, len(nums)-1] \u304b\u3089\u6570\u5024\u3092\u30e9\u30f3\u30c0\u30e0\u306b\u9078\u629e\n    random_index = random.randint(0, len(nums) - 1)\n    # \u30e9\u30f3\u30c0\u30e0\u306a\u8981\u7d20\u3092\u53d6\u5f97\u3057\u3066\u8fd4\u3059\n    random_num = nums[random_index]\n    return random_num\n
        array.cpp
        /* \u8981\u7d20\u3078\u306e\u4e71\u6570\u30a2\u30af\u30bb\u30b9 */\nint randomAccess(int *nums, int size) {\n    // [0, size)\u306e\u7bc4\u56f2\u3067\u4e71\u6570\u3092\u9078\u629e\n    int randomIndex = rand() % size;\n    // \u4e71\u6570\u8981\u7d20\u3092\u53d6\u5f97\u3057\u3066\u8fd4\u5374\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
        array.java
        /* \u8981\u7d20\u3078\u306e\u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9 */\nint randomAccess(int[] nums) {\n    // \u533a\u9593 [0, nums.length) \u304b\u3089\u30e9\u30f3\u30c0\u30e0\u306b\u6570\u3092\u9078\u629e\n    int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);\n    // \u30e9\u30f3\u30c0\u30e0\u8981\u7d20\u3092\u53d6\u5f97\u3057\u3066\u8fd4\u3059\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
        array.cs
        [class]{array}-[func]{RandomAccess}\n
        array.go
        [class]{}-[func]{randomAccess}\n
        array.swift
        [class]{}-[func]{randomAccess}\n
        array.js
        [class]{}-[func]{randomAccess}\n
        array.ts
        [class]{}-[func]{randomAccess}\n
        array.dart
        [class]{}-[func]{randomAccess}\n
        array.rs
        [class]{}-[func]{random_access}\n
        array.c
        [class]{}-[func]{randomAccess}\n
        array.kt
        [class]{}-[func]{randomAccess}\n
        array.rb
        [class]{}-[func]{random_access}\n
        "},{"location":"chapter_array_and_linkedlist/array/#3","title":"3. \u00a0 \u8981\u7d20\u306e\u633f\u5165","text":"

        \u914d\u5217\u8981\u7d20\u306f\u30e1\u30e2\u30ea\u5185\u3067\u5bc6\u306b\u8a70\u307e\u3063\u3066\u304a\u308a\u3001\u305d\u308c\u3089\u306e\u9593\u306b\u8ffd\u52a0\u30c7\u30fc\u30bf\u3092\u53ce\u5bb9\u3059\u308b\u305f\u3081\u306e\u7a7a\u9593\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u914d\u5217\u306e\u4e2d\u592e\u306b\u8981\u7d20\u3092\u633f\u5165\u3059\u308b\u306b\u306f\u3001\u5f8c\u7d9a\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u305a\u3064\u5f8c\u308d\u306b\u30b7\u30d5\u30c8\u3057\u3066\u3001\u65b0\u3057\u3044\u8981\u7d20\u306e\u305f\u3081\u306e\u7a7a\u9593\u3092\u4f5c\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u56f3 4-3 \u00a0 \u914d\u5217\u8981\u7d20\u633f\u5165\u306e\u4f8b

        \u914d\u5217\u306e\u9577\u3055\u304c\u56fa\u5b9a\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u8981\u7d20\u3092\u633f\u5165\u3059\u308b\u3068\u5fc5\u7136\u7684\u306b\u914d\u5217\u306e\u6700\u5f8c\u306e\u8981\u7d20\u304c\u5931\u308f\u308c\u308b\u3053\u3068\u306b\u6ce8\u610f\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002\u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u65b9\u6cd5\u306f\u300c\u30ea\u30b9\u30c8\u300d\u306e\u7ae0\u3067\u63a2\u6c42\u3055\u308c\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def insert(nums: list[int], num: int, index: int):\n    \"\"\"\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 index \u306b\u8981\u7d20 num \u3092\u633f\u5165\"\"\"\n    # \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 index \u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u5f8c\u308d\u306b\u79fb\u52d5\n    for i in range(len(nums) - 1, index, -1):\n        nums[i] = nums[i - 1]\n    # num \u3092 index \u306e\u4f4d\u7f6e\u306e\u8981\u7d20\u306b\u4ee3\u5165\n    nums[index] = num\n
        array.cpp
        /* `index`\u306b\u8981\u7d20num\u3092\u633f\u5165 */\nvoid insert(int *nums, int size, int num, int index) {\n    // `index`\u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u5f8c\u308d\u306b\u79fb\u52d5\n    for (int i = size - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // index\u306e\u4f4d\u7f6e\u306bnum\u3092\u4ee3\u5165\n    nums[index] = num;\n}\n
        array.java
        /* `index` \u306b\u8981\u7d20 num \u3092\u633f\u5165 */\nvoid insert(int[] nums, int num, int index) {\n    // `index` \u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u5f8c\u308d\u306b\u79fb\u52d5\n    for (int i = nums.length - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // index \u306e\u8981\u7d20\u306b num \u3092\u4ee3\u5165\n    nums[index] = num;\n}\n
        array.cs
        [class]{array}-[func]{Insert}\n
        array.go
        [class]{}-[func]{insert}\n
        array.swift
        [class]{}-[func]{insert}\n
        array.js
        [class]{}-[func]{insert}\n
        array.ts
        [class]{}-[func]{insert}\n
        array.dart
        [class]{}-[func]{insert}\n
        array.rs
        [class]{}-[func]{insert}\n
        array.c
        [class]{}-[func]{insert}\n
        array.kt
        [class]{}-[func]{insert}\n
        array.rb
        [class]{}-[func]{insert}\n
        "},{"location":"chapter_array_and_linkedlist/array/#4","title":"4. \u00a0 \u8981\u7d20\u306e\u524a\u9664","text":"

        \u540c\u69d8\u306b\u3001\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(i\\)\u306e\u8981\u7d20\u3092\u524a\u9664\u3059\u308b\u306b\u306f\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(i\\)\u306b\u7d9a\u304f\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u305a\u3064\u524d\u306b\u79fb\u52d5\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u56f3 4-4 \u00a0 \u914d\u5217\u8981\u7d20\u524a\u9664\u306e\u4f8b

        \u524a\u9664\u5f8c\u3001\u5143\u306e\u6700\u5f8c\u306e\u8981\u7d20\u306f\u300c\u610f\u5473\u304c\u306a\u3044\u300d\u3082\u306e\u306b\u306a\u308b\u305f\u3081\u3001\u7279\u5b9a\u306e\u4fee\u6b63\u306f\u5fc5\u8981\u306a\u3044\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def remove(nums: list[int], index: int):\n    \"\"\"\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 index \u306e\u8981\u7d20\u3092\u524a\u9664\"\"\"\n    # \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 index \u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u524d\u306b\u79fb\u52d5\n    for i in range(index, len(nums) - 1):\n        nums[i] = nums[i + 1]\n
        array.cpp
        /* `index`\u306e\u8981\u7d20\u3092\u524a\u9664 */\nvoid remove(int *nums, int size, int index) {\n    // `index`\u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u524d\u306b\u79fb\u52d5\n    for (int i = index; i < size - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
        array.java
        /* `index` \u306e\u8981\u7d20\u3092\u524a\u9664 */\nvoid remove(int[] nums, int index) {\n    // `index` \u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u524d\u306b\u79fb\u52d5\n    for (int i = index; i < nums.length - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
        array.cs
        [class]{array}-[func]{Remove}\n
        array.go
        [class]{}-[func]{remove}\n
        array.swift
        [class]{}-[func]{remove}\n
        array.js
        [class]{}-[func]{remove}\n
        array.ts
        [class]{}-[func]{remove}\n
        array.dart
        [class]{}-[func]{remove}\n
        array.rs
        [class]{}-[func]{remove}\n
        array.c
        [class]{}-[func]{removeItem}\n
        array.kt
        [class]{}-[func]{remove}\n
        array.rb
        [class]{}-[func]{remove}\n

        \u8981\u7d04\u3059\u308b\u3068\u3001\u914d\u5217\u306e\u633f\u5165\u3068\u524a\u9664\u64cd\u4f5c\u306b\u306f\u4ee5\u4e0b\u306e\u6b20\u70b9\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u9ad8\u3044\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u914d\u5217\u306e\u633f\u5165\u3068\u524a\u9664\u306e\u4e21\u65b9\u306e\u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3001\u3053\u3053\u3067\\(n\\)\u306f\u914d\u5217\u306e\u9577\u3055\u3067\u3059\u3002
        • \u8981\u7d20\u306e\u640d\u5931\uff1a\u914d\u5217\u306e\u9577\u3055\u304c\u56fa\u5b9a\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u633f\u5165\u6642\u306b\u914d\u5217\u306e\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u8981\u7d20\u306f\u5931\u308f\u308c\u307e\u3059\u3002
        • \u30e1\u30e2\u30ea\u306e\u7121\u99c4\uff1a\u3088\u308a\u9577\u3044\u914d\u5217\u3092\u521d\u671f\u5316\u3057\u3066\u524d\u90e8\u5206\u306e\u307f\u3092\u5229\u7528\u3059\u308b\u3068\u3001\u633f\u5165\u6642\u306b\u300c\u610f\u5473\u306e\u306a\u3044\u300d\u672b\u5c3e\u8981\u7d20\u304c\u751f\u3058\u3001\u30e1\u30e2\u30ea\u7a7a\u9593\u306e\u7121\u99c4\u306b\u3064\u306a\u304c\u308a\u307e\u3059\u3002
        "},{"location":"chapter_array_and_linkedlist/array/#5","title":"5. \u00a0 \u914d\u5217\u306e\u8d70\u67fb","text":"

        \u307b\u3068\u3093\u3069\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u306f\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u4f7f\u7528\u3059\u308b\u304b\u3001\u5404\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9\u3059\u308b\u3053\u3068\u3067\u914d\u5217\u3092\u8d70\u67fb\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def traverse(nums: list[int]):\n    \"\"\"\u914d\u5217\u306e\u8d70\u67fb\"\"\"\n    count = 0\n    # \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u3088\u308b\u914d\u5217\u306e\u8d70\u67fb\n    for i in range(len(nums)):\n        count += nums[i]\n    # \u914d\u5217\u8981\u7d20\u306e\u8d70\u67fb\n    for num in nums:\n        count += num\n    # \u30c7\u30fc\u30bf\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3068\u8981\u7d20\u306e\u4e21\u65b9\u3092\u8d70\u67fb\n    for i, num in enumerate(nums):\n        count += nums[i]\n        count += num\n
        array.cpp
        /* \u914d\u5217\u306e\u8d70\u67fb */\nvoid traverse(int *nums, int size) {\n    int count = 0;\n    // \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u3088\u308b\u914d\u5217\u306e\u8d70\u67fb\n    for (int i = 0; i < size; i++) {\n        count += nums[i];\n    }\n}\n
        array.java
        /* \u914d\u5217\u3092\u8d70\u67fb */\nvoid traverse(int[] nums) {\n    int count = 0;\n    // \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u3088\u308b\u914d\u5217\u306e\u8d70\u67fb\n    for (int i = 0; i < nums.length; i++) {\n        count += nums[i];\n    }\n    // \u914d\u5217\u8981\u7d20\u306e\u8d70\u67fb\n    for (int num : nums) {\n        count += num;\n    }\n}\n
        array.cs
        [class]{array}-[func]{Traverse}\n
        array.go
        [class]{}-[func]{traverse}\n
        array.swift
        [class]{}-[func]{traverse}\n
        array.js
        [class]{}-[func]{traverse}\n
        array.ts
        [class]{}-[func]{traverse}\n
        array.dart
        [class]{}-[func]{traverse}\n
        array.rs
        [class]{}-[func]{traverse}\n
        array.c
        [class]{}-[func]{traverse}\n
        array.kt
        [class]{}-[func]{traverse}\n
        array.rb
        [class]{}-[func]{traverse}\n
        "},{"location":"chapter_array_and_linkedlist/array/#6","title":"6. \u00a0 \u8981\u7d20\u306e\u691c\u7d22","text":"

        \u914d\u5217\u5185\u306e\u7279\u5b9a\u306e\u8981\u7d20\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u306f\u3001\u914d\u5217\u3092\u53cd\u5fa9\u3057\u3001\u5404\u8981\u7d20\u3092\u30c1\u30a7\u30c3\u30af\u3057\u3066\u76ee\u7684\u306e\u5024\u3068\u4e00\u81f4\u3059\u308b\u304b\u3069\u3046\u304b\u3092\u6c7a\u5b9a\u3059\u308b\u3053\u3068\u3092\u542b\u307f\u307e\u3059\u3002

        \u914d\u5217\u306f\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3042\u308b\u305f\u3081\u3001\u3053\u306e\u64cd\u4f5c\u306f\u4e00\u822c\u7684\u306b\u300c\u7dda\u5f62\u63a2\u7d22\u300d\u3068\u547c\u3070\u308c\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def find(nums: list[int], target: int) -> int:\n    \"\"\"\u914d\u5217\u5185\u306e\u6307\u5b9a\u3055\u308c\u305f\u8981\u7d20\u3092\u691c\u7d22\"\"\"\n    for i in range(len(nums)):\n        if nums[i] == target:\n            return i\n    return -1\n
        array.cpp
        /* \u914d\u5217\u5185\u306e\u6307\u5b9a\u8981\u7d20\u3092\u691c\u7d22 */\nint find(int *nums, int size, int target) {\n    for (int i = 0; i < size; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
        array.java
        /* \u914d\u5217\u5185\u3067\u6307\u5b9a\u3055\u308c\u305f\u8981\u7d20\u3092\u691c\u7d22 */\nint find(int[] nums, int target) {\n    for (int i = 0; i < nums.length; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
        array.cs
        [class]{array}-[func]{Find}\n
        array.go
        [class]{}-[func]{find}\n
        array.swift
        [class]{}-[func]{find}\n
        array.js
        [class]{}-[func]{find}\n
        array.ts
        [class]{}-[func]{find}\n
        array.dart
        [class]{}-[func]{find}\n
        array.rs
        [class]{}-[func]{find}\n
        array.c
        [class]{}-[func]{find}\n
        array.kt
        [class]{}-[func]{find}\n
        array.rb
        [class]{}-[func]{find}\n
        "},{"location":"chapter_array_and_linkedlist/array/#7","title":"7. \u00a0 \u914d\u5217\u306e\u62e1\u5f35","text":"

        \u8907\u96d1\u306a\u30b7\u30b9\u30c6\u30e0\u74b0\u5883\u3067\u306f\u3001\u5b89\u5168\u306a\u5bb9\u91cf\u62e1\u5f35\u306e\u305f\u3081\u306b\u914d\u5217\u306e\u5f8c\u306b\u30e1\u30e2\u30ea\u7a7a\u9593\u306e\u53ef\u7528\u6027\u3092\u78ba\u4fdd\u3059\u308b\u3053\u3068\u304c\u56f0\u96e3\u306b\u306a\u308a\u307e\u3059\u3002\u305d\u306e\u7d50\u679c\u3001\u307b\u3068\u3093\u3069\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u306f\u3001**\u914d\u5217\u306e\u9577\u3055\u306f\u4e0d\u5909**\u3067\u3059\u3002

        \u914d\u5217\u3092\u62e1\u5f35\u3059\u308b\u306b\u306f\u3001\u3088\u308a\u5927\u304d\u306a\u914d\u5217\u3092\u4f5c\u6210\u3057\u3001\u5143\u306e\u914d\u5217\u304b\u3089\u8981\u7d20\u3092\u30b3\u30d4\u30fc\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3001\u5927\u304d\u306a\u914d\u5217\u3067\u306f\u6642\u9593\u304c\u304b\u304b\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def extend(nums: list[int], enlarge: int) -> list[int]:\n    \"\"\"\u914d\u5217\u306e\u9577\u3055\u3092\u62e1\u5f35\"\"\"\n    # \u62e1\u5f35\u3055\u308c\u305f\u9577\u3055\u306e\u914d\u5217\u3092\u521d\u671f\u5316\n    res = [0] * (len(nums) + enlarge)\n    # \u5143\u306e\u914d\u5217\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u65b0\u3057\u3044\u914d\u5217\u306b\u30b3\u30d4\u30fc\n    for i in range(len(nums)):\n        res[i] = nums[i]\n    # \u62e1\u5f35\u5f8c\u306e\u65b0\u3057\u3044\u914d\u5217\u3092\u8fd4\u3059\n    return res\n
        array.cpp
        /* \u914d\u5217\u9577\u306e\u62e1\u5f35 */\nint *extend(int *nums, int size, int enlarge) {\n    // \u62e1\u5f35\u3055\u308c\u305f\u9577\u3055\u306e\u914d\u5217\u3092\u521d\u671f\u5316\n    int *res = new int[size + enlarge];\n    // \u5143\u306e\u914d\u5217\u306e\u5168\u8981\u7d20\u3092\u65b0\u3057\u3044\u914d\u5217\u306b\u30b3\u30d4\u30fc\n    for (int i = 0; i < size; i++) {\n        res[i] = nums[i];\n    }\n    // \u30e1\u30e2\u30ea\u3092\u89e3\u653e\n    delete[] nums;\n    // \u62e1\u5f35\u5f8c\u306e\u65b0\u3057\u3044\u914d\u5217\u3092\u8fd4\u5374\n    return res;\n}\n
        array.java
        /* \u914d\u5217\u9577\u306e\u62e1\u5f35 */\nint[] extend(int[] nums, int enlarge) {\n    // \u62e1\u5f35\u3055\u308c\u305f\u9577\u3055\u306e\u914d\u5217\u3092\u521d\u671f\u5316\n    int[] res = new int[nums.length + enlarge];\n    // \u5143\u306e\u914d\u5217\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u65b0\u3057\u3044\u914d\u5217\u306b\u30b3\u30d4\u30fc\n    for (int i = 0; i < nums.length; i++) {\n        res[i] = nums[i];\n    }\n    // \u62e1\u5f35\u5f8c\u306e\u65b0\u3057\u3044\u914d\u5217\u3092\u8fd4\u3059\n    return res;\n}\n
        array.cs
        [class]{array}-[func]{Extend}\n
        array.go
        [class]{}-[func]{extend}\n
        array.swift
        [class]{}-[func]{extend}\n
        array.js
        [class]{}-[func]{extend}\n
        array.ts
        [class]{}-[func]{extend}\n
        array.dart
        [class]{}-[func]{extend}\n
        array.rs
        [class]{}-[func]{extend}\n
        array.c
        [class]{}-[func]{extend}\n
        array.kt
        [class]{}-[func]{extend}\n
        array.rb
        [class]{}-[func]{extend}\n
        "},{"location":"chapter_array_and_linkedlist/array/#412","title":"4.1.2 \u00a0 \u914d\u5217\u306e\u5229\u70b9\u3068\u5236\u9650","text":"

        \u914d\u5217\u306f\u9023\u7d9a\u3057\u305f\u30e1\u30e2\u30ea\u7a7a\u9593\u306b\u683c\u7d0d\u3055\u308c\u3001\u540c\u3058\u578b\u306e\u8981\u7d20\u3067\u69cb\u6210\u3055\u308c\u307e\u3059\u3002\u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u304c\u30c7\u30fc\u30bf\u69cb\u9020\u64cd\u4f5c\u306e\u52b9\u7387\u3092\u6700\u9069\u5316\u3059\u308b\u305f\u3081\u306b\u6d3b\u7528\u3067\u304d\u308b\u5b9f\u8cea\u7684\u306a\u4e8b\u524d\u60c5\u5831\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002

        • \u9ad8\u3044\u7a7a\u9593\u52b9\u7387\uff1a\u914d\u5217\u306f\u30c7\u30fc\u30bf\u306e\u305f\u3081\u306e\u9023\u7d9a\u3057\u305f\u30e1\u30e2\u30ea\u30d6\u30ed\u30c3\u30af\u3092\u5272\u308a\u5f53\u3066\u3001\u8ffd\u52a0\u306e\u69cb\u9020\u7684\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u306e\u5fc5\u8981\u6027\u3092\u6392\u9664\u3057\u307e\u3059\u3002
        • \u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\u306e\u30b5\u30dd\u30fc\u30c8\uff1a\u914d\u5217\u306f\u4efb\u610f\u306e\u8981\u7d20\u3078\u306e\\(O(1)\\)\u6642\u9593\u30a2\u30af\u30bb\u30b9\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002
        • \u30ad\u30e3\u30c3\u30b7\u30e5\u5c40\u6240\u6027\uff1a\u914d\u5217\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u304d\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306f\u305d\u308c\u3089\u3092\u8aad\u307f\u8fbc\u3080\u3060\u3051\u3067\u306a\u304f\u3001\u5468\u56f2\u306e\u30c7\u30fc\u30bf\u3082\u30ad\u30e3\u30c3\u30b7\u30e5\u3057\u3001\u9ad8\u901f\u30ad\u30e3\u30c3\u30b7\u30e5\u3092\u5229\u7528\u3057\u3066\u5f8c\u7d9a\u306e\u64cd\u4f5c\u901f\u5ea6\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002

        \u3057\u304b\u3057\u3001\u9023\u7d9a\u7a7a\u9593\u683c\u7d0d\u306f\u8af8\u5203\u306e\u5263\u3067\u3001\u4ee5\u4e0b\u306e\u5236\u9650\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u633f\u5165\u3068\u524a\u9664\u306e\u52b9\u7387\u304c\u4f4e\u3044\uff1a\u914d\u5217\u306b\u591a\u304f\u306e\u8981\u7d20\u304c\u84c4\u7a4d\u3055\u308c\u308b\u3068\u3001\u8981\u7d20\u306e\u633f\u5165\u3084\u524a\u9664\u306b\u306f\u5927\u91cf\u306e\u8981\u7d20\u3092\u30b7\u30d5\u30c8\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u56fa\u5b9a\u9577\uff1a\u914d\u5217\u306e\u9577\u3055\u306f\u521d\u671f\u5316\u5f8c\u306b\u56fa\u5b9a\u3055\u308c\u307e\u3059\u3002\u914d\u5217\u3092\u62e1\u5f35\u3059\u308b\u306b\u306f\u3001\u3059\u3079\u3066\u306e\u30c7\u30fc\u30bf\u3092\u65b0\u3057\u3044\u914d\u5217\u306b\u30b3\u30d4\u30fc\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u5927\u304d\u306a\u30b3\u30b9\u30c8\u304c\u304b\u304b\u308a\u307e\u3059\u3002
        • \u7a7a\u9593\u306e\u7121\u99c4\uff1a\u5272\u308a\u5f53\u3066\u3089\u308c\u305f\u914d\u5217\u30b5\u30a4\u30ba\u304c\u5fc5\u8981\u4ee5\u4e0a\u306b\u5927\u304d\u3044\u5834\u5408\u3001\u4f59\u5206\u306a\u7a7a\u9593\u304c\u7121\u99c4\u306b\u306a\u308a\u307e\u3059\u3002
        "},{"location":"chapter_array_and_linkedlist/array/#413","title":"4.1.3 \u00a0 \u914d\u5217\u306e\u5178\u578b\u7684\u306a\u5fdc\u7528","text":"

        \u914d\u5217\u306f\u57fa\u672c\u7684\u3067\u5e83\u304f\u4f7f\u7528\u3055\u308c\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3059\u3002\u69d8\u3005\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u983b\u7e41\u306b\u5fdc\u7528\u3055\u308c\u3001\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u5b9f\u88c5\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002

        • \u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\uff1a\u914d\u5217\u306f\u30e9\u30f3\u30c0\u30e0\u30b5\u30f3\u30d7\u30ea\u30f3\u30b0\u304c\u5fc5\u8981\u306a\u3068\u304d\u306e\u30c7\u30fc\u30bf\u683c\u7d0d\u306b\u7406\u60f3\u7684\u3067\u3059\u3002\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u57fa\u3065\u3044\u3066\u30e9\u30f3\u30c0\u30e0\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u751f\u6210\u3059\u308b\u3053\u3068\u3067\u3001\u52b9\u7387\u7684\u306b\u30e9\u30f3\u30c0\u30e0\u30b5\u30f3\u30d7\u30ea\u30f3\u30b0\u3092\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002
        • \u30bd\u30fc\u30c8\u3068\u691c\u7d22\uff1a\u914d\u5217\u306f\u30bd\u30fc\u30c8\u3068\u691c\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u6700\u3082\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3059\u3002\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u3001\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u3001\u4e8c\u5206\u63a2\u7d22\u306a\u3069\u306e\u6280\u8853\u306f\u4e3b\u306b\u914d\u5217\u3067\u52d5\u4f5c\u3057\u307e\u3059\u3002
        • \u30eb\u30c3\u30af\u30a2\u30c3\u30d7\u30c6\u30fc\u30d6\u30eb\uff1a\u914d\u5217\u306f\u8fc5\u901f\u306a\u8981\u7d20\u3084\u95a2\u4fc2\u306e\u53d6\u5f97\u306e\u305f\u3081\u306e\u52b9\u7387\u7684\u306a\u30eb\u30c3\u30af\u30a2\u30c3\u30d7\u30c6\u30fc\u30d6\u30eb\u3068\u3057\u3066\u6a5f\u80fd\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u6587\u5b57\u3092ASCII\u30b3\u30fc\u30c9\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3059\u308b\u3053\u3068\u306f\u3001ASCII\u30b3\u30fc\u30c9\u5024\u3092\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3068\u3057\u3066\u4f7f\u7528\u3057\u3001\u5bfe\u5fdc\u3059\u308b\u8981\u7d20\u3092\u914d\u5217\u306b\u683c\u7d0d\u3059\u308b\u3053\u3068\u3067\u7c21\u5358\u306b\u306a\u308a\u307e\u3059\u3002
        • \u6a5f\u68b0\u5b66\u7fd2\uff1a\u30cb\u30e5\u30fc\u30e9\u30eb\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306e\u9818\u57df\u3067\u306f\u3001\u914d\u5217\u306f\u30d9\u30af\u30c8\u30eb\u3001\u884c\u5217\u3001\u30c6\u30f3\u30bd\u30eb\u3092\u542b\u3080\u91cd\u8981\u306a\u7dda\u5f62\u4ee3\u6570\u6f14\u7b97\u306e\u5b9f\u884c\u306b\u304a\u3044\u3066\u91cd\u8981\u306a\u5f79\u5272\u3092\u679c\u305f\u3057\u307e\u3059\u3002\u914d\u5217\u306f\u30cb\u30e5\u30fc\u30e9\u30eb\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306b\u304a\u3044\u3066\u4e3b\u8981\u304b\u3064\u6700\u3082\u5e83\u7bc4\u56f2\u306b\u4f7f\u7528\u3055\u308c\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u3057\u3066\u6a5f\u80fd\u3057\u307e\u3059\u3002
        • \u30c7\u30fc\u30bf\u69cb\u9020\u306e\u5b9f\u88c5\uff1a\u914d\u5217\u306f\u3001\u30b9\u30bf\u30c3\u30af\u3001\u30ad\u30e5\u30fc\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3001\u30d2\u30fc\u30d7\u3001\u30b0\u30e9\u30d5\u306a\u3069\u3001\u69d8\u3005\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306e\u69cb\u6210\u8981\u7d20\u3068\u3057\u3066\u6a5f\u80fd\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u30b0\u30e9\u30d5\u306e\u96a3\u63a5\u884c\u5217\u8868\u73fe\u306f\u672c\u8cea\u7684\u306b\u4e8c\u6b21\u5143\u914d\u5217\u3067\u3059\u3002
        "},{"location":"chapter_array_and_linkedlist/linked_list/","title":"4.2 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8","text":"

        \u30e1\u30e2\u30ea\u7a7a\u9593\u306f\u3001\u3059\u3079\u3066\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u9593\u3067\u5171\u6709\u3055\u308c\u308b\u30ea\u30bd\u30fc\u30b9\u3067\u3059\u3002\u8907\u96d1\u306a\u30b7\u30b9\u30c6\u30e0\u74b0\u5883\u3067\u306f\u3001\u4f7f\u7528\u53ef\u80fd\u306a\u30e1\u30e2\u30ea\u304c\u30e1\u30e2\u30ea\u7a7a\u9593\u5168\u4f53\u306b\u5206\u6563\u3057\u3066\u3044\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u914d\u5217\u306b\u5272\u308a\u5f53\u3066\u3089\u308c\u308b\u30e1\u30e2\u30ea\u306f\u9023\u7d9a\u3057\u3066\u3044\u308b\u5fc5\u8981\u304c\u3042\u308b\u3053\u3068\u3092\u7406\u89e3\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u975e\u5e38\u306b\u5927\u304d\u306a\u914d\u5217\u306e\u5834\u5408\u3001\u5341\u5206\u306a\u5927\u304d\u3055\u306e\u9023\u7d9a\u30e1\u30e2\u30ea\u7a7a\u9593\u3092\u898b\u3064\u3051\u308b\u306e\u306f\u56f0\u96e3\u306a\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u3053\u3067\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u67d4\u8edf\u306a\u5229\u70b9\u304c\u660e\u3089\u304b\u306b\u306a\u308a\u307e\u3059\u3002

        \u9023\u7d50\u30ea\u30b9\u30c8\u306f\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3042\u308a\u3001\u5404\u8981\u7d20\u306f\u30ce\u30fc\u30c9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3001\u30ce\u30fc\u30c9\u306f\u300c\u53c2\u7167\u300d\u3092\u901a\u3058\u3066\u76f8\u4e92\u63a5\u7d9a\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u53c2\u7167\u306f\u5f8c\u7d9a\u30ce\u30fc\u30c9\u306e\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u3092\u4fdd\u6301\u3057\u30011\u3064\u306e\u30ce\u30fc\u30c9\u304b\u3089\u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u30ca\u30d3\u30b2\u30fc\u30b7\u30e7\u30f3\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002

        \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u8a2d\u8a08\u3067\u306f\u3001\u30ce\u30fc\u30c9\u3092\u9023\u7d9a\u3059\u308b\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u3092\u5fc5\u8981\u3068\u305b\u305a\u306b\u3001\u30e1\u30e2\u30ea\u4f4d\u7f6e\u5168\u4f53\u306b\u5206\u6563\u914d\u7f6e\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u56f3 4-5 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u5b9a\u7fa9\u3068\u683c\u7d0d\u65b9\u6cd5

        \u4e0a\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u57fa\u672c\u7684\u306a\u69cb\u6210\u8981\u7d20\u306f\u30ce\u30fc\u30c9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3059\u3002\u5404\u30ce\u30fc\u30c9\u306f2\u3064\u306e\u4e3b\u8981\u306a\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3067\u69cb\u6210\u3055\u308c\u3066\u3044\u307e\u3059\uff1a\u30ce\u30fc\u30c9\u306e\u300c\u5024\u300d\u3068\u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u300c\u53c2\u7167\u300d\u3067\u3059\u3002

        • \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u6700\u521d\u306e\u30ce\u30fc\u30c9\u306f\u300c\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u300d\u3001\u6700\u5f8c\u306e\u30ce\u30fc\u30c9\u306f\u300c\u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u300d\u3067\u3059\u3002
        • \u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u306f\u300cnull\u300d\u3092\u6307\u3057\u3001Java\u3067\u306fnull\u3001C++\u3067\u306fnullptr\u3001Python\u3067\u306fNone\u3068\u3057\u3066\u6307\u5b9a\u3055\u308c\u307e\u3059\u3002
        • C\u3001C++\u3001Go\u3001Rust\u306a\u3069\u306e\u30dd\u30a4\u30f3\u30bf\u3092\u30b5\u30dd\u30fc\u30c8\u3059\u308b\u8a00\u8a9e\u3067\u306f\u3001\u3053\u306e\u300c\u53c2\u7167\u300d\u306f\u901a\u5e38\u300c\u30dd\u30a4\u30f3\u30bf\u300d\u3068\u3057\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u304c\u793a\u3059\u3088\u3046\u306b\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306eListNode\u306f\u5024\u3092\u4fdd\u6301\u3059\u308b\u3060\u3051\u3067\u306a\u304f\u3001\u8ffd\u52a0\u306e\u53c2\u7167\uff08\u307e\u305f\u306f\u30dd\u30a4\u30f3\u30bf\uff09\u3082\u7dad\u6301\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u540c\u3058\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3059\u308b\u5834\u5408\u3001\u914d\u5217\u3088\u308a\u3082\u591a\u304f\u306e\u30e1\u30e2\u30ea\u7a7a\u9593\u3092\u5360\u6709\u3057\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin
        class ListNode:\n    \"\"\"\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val               # \u30ce\u30fc\u30c9\u5024\n        self.next: ListNode | None = None # \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u69cb\u9020\u4f53 */\nstruct ListNode {\n    int val;         // \u30ce\u30fc\u30c9\u5024\n    ListNode *next;  // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    ListNode(int x) : val(x), next(nullptr) {}  // \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\n};\n
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode {\n    int val;        // \u30ce\u30fc\u30c9\u5024\n    ListNode next;  // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    ListNode(int x) { val = x; }  // \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\n}\n
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode(int x) {  // \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\n    int val = x;         // \u30ce\u30fc\u30c9\u5024\n    ListNode? next;      // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n}\n
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u69cb\u9020\u4f53 */\ntype ListNode struct {\n    Val  int       // \u30ce\u30fc\u30c9\u5024\n    Next *ListNode // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n}\n\n// NewListNode \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3001\u65b0\u3057\u3044\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u4f5c\u6210\nfunc NewListNode(val int) *ListNode {\n    return &ListNode{\n        Val:  val,\n        Next: nil,\n    }\n}\n
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode {\n    var val: Int // \u30ce\u30fc\u30c9\u5024\n    var next: ListNode? // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n\n    init(x: Int) { // \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\n        val = x\n    }\n}\n
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode {\n    constructor(val, next) {\n        this.val = (val === undefined ? 0 : val);       // \u30ce\u30fc\u30c9\u5024\n        this.next = (next === undefined ? null : next); // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    }\n}\n
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode {\n    val: number;\n    next: ListNode | null;\n    constructor(val?: number, next?: ListNode | null) {\n        this.val = val === undefined ? 0 : val;        // \u30ce\u30fc\u30c9\u5024\n        this.next = next === undefined ? null : next;  // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    }\n}\n
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode {\n  int val; // \u30ce\u30fc\u30c9\u5024\n  ListNode? next; // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n  ListNode(this.val, [this.next]); // \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\n}\n
        use std::rc::Rc;\nuse std::cell::RefCell;\n/* \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\n#[derive(Debug)]\nstruct ListNode {\n    val: i32, // \u30ce\u30fc\u30c9\u5024\n    next: Option<Rc<RefCell<ListNode>>>, // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n}\n
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u69cb\u9020\u4f53 */\ntypedef struct ListNode {\n    int val;               // \u30ce\u30fc\u30c9\u5024\n    struct ListNode *next; // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n} ListNode;\n\n/* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\nListNode *newListNode(int val) {\n    ListNode *node;\n    node = (ListNode *) malloc(sizeof(ListNode));\n    node->val = val;\n    node->next = NULL;\n    return node;\n}\n
        \n
        "},{"location":"chapter_array_and_linkedlist/linked_list/#421","title":"4.2.1 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/linked_list/#1","title":"1. \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u521d\u671f\u5316","text":"

        \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u69cb\u7bc9\u306f2\u6bb5\u968e\u306e\u30d7\u30ed\u30bb\u30b9\u3067\u3059\uff1a\u307e\u305a\u5404\u30ce\u30fc\u30c9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u521d\u671f\u5316\u3057\u3001\u6b21\u306b\u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u30ea\u30f3\u30af\u3092\u5f62\u6210\u3057\u307e\u3059\u3002\u521d\u671f\u5316\u5f8c\u3001\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u304b\u3089next\u53c2\u7167\u3092\u305f\u3069\u3063\u3066\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3092\u9806\u6b21\u5de1\u56de\u3067\u304d\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin linked_list.py
        # \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316: 1 -> 3 -> 2 -> 5 -> 4\n# \u5404\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\nn0 = ListNode(1)\nn1 = ListNode(3)\nn2 = ListNode(2)\nn3 = ListNode(5)\nn4 = ListNode(4)\n# \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u3092\u69cb\u7bc9\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
        linked_list.cpp
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316: 1 -> 3 -> 2 -> 5 -> 4 */\n// \u5404\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\nListNode* n0 = new ListNode(1);\nListNode* n1 = new ListNode(3);\nListNode* n2 = new ListNode(2);\nListNode* n3 = new ListNode(5);\nListNode* n4 = new ListNode(4);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u3092\u69cb\u7bc9\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
        linked_list.java
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316: 1 -> 3 -> 2 -> 5 -> 4 */\n// \u5404\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\nListNode n0 = new ListNode(1);\nListNode n1 = new ListNode(3);\nListNode n2 = new ListNode(2);\nListNode n3 = new ListNode(5);\nListNode n4 = new ListNode(4);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u3092\u69cb\u7bc9\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
        linked_list.cs
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316: 1 -> 3 -> 2 -> 5 -> 4 */\n// \u5404\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\nListNode n0 = new(1);\nListNode n1 = new(3);\nListNode n2 = new(2);\nListNode n3 = new(5);\nListNode n4 = new(4);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u3092\u69cb\u7bc9\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
        linked_list.go
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316: 1 -> 3 -> 2 -> 5 -> 4 */\n// \u5404\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\nn0 := NewListNode(1)\nn1 := NewListNode(3)\nn2 := NewListNode(2)\nn3 := NewListNode(5)\nn4 := NewListNode(4)\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u3092\u69cb\u7bc9\nn0.Next = n1\nn1.Next = n2\nn2.Next = n3\nn3.Next = n4\n
        linked_list.swift
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316: 1 -> 3 -> 2 -> 5 -> 4 */\n// \u5404\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\nlet n0 = ListNode(x: 1)\nlet n1 = ListNode(x: 3)\nlet n2 = ListNode(x: 2)\nlet n3 = ListNode(x: 5)\nlet n4 = ListNode(x: 4)\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u3092\u69cb\u7bc9\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
        linked_list.js
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316: 1 -> 3 -> 2 -> 5 -> 4 */\n// \u5404\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u3092\u69cb\u7bc9\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
        linked_list.ts
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316: 1 -> 3 -> 2 -> 5 -> 4 */\n// \u5404\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u3092\u69cb\u7bc9\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
        linked_list.dart
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316: 1 -> 3 -> 2 -> 5 -> 4 */\n// \u5404\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\nListNode n0 = ListNode(1);\nListNode n1 = ListNode(3);\nListNode n2 = ListNode(2);\nListNode n3 = ListNode(5);\nListNode n4 = ListNode(4);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u3092\u69cb\u7bc9\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
        linked_list.rs
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316: 1 -> 3 -> 2 -> 5 -> 4 */\n// \u5404\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\nlet n0 = Rc::new(RefCell::new(ListNode { val: 1, next: None }));\nlet n1 = Rc::new(RefCell::new(ListNode { val: 3, next: None }));\nlet n2 = Rc::new(RefCell::new(ListNode { val: 2, next: None }));\nlet n3 = Rc::new(RefCell::new(ListNode { val: 5, next: None }));\nlet n4 = Rc::new(RefCell::new(ListNode { val: 4, next: None }));\n\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u3092\u69cb\u7bc9\nn0.borrow_mut().next = Some(n1.clone());\nn1.borrow_mut().next = Some(n2.clone());\nn2.borrow_mut().next = Some(n3.clone());\nn3.borrow_mut().next = Some(n4.clone());\n
        linked_list.c
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316: 1 -> 3 -> 2 -> 5 -> 4 */\n// \u5404\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\nListNode* n0 = newListNode(1);\nListNode* n1 = newListNode(3);\nListNode* n2 = newListNode(2);\nListNode* n3 = newListNode(5);\nListNode* n4 = newListNode(4);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\u3092\u69cb\u7bc9\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
        linked_list.kt
        \n

        \u914d\u5217\u5168\u4f53\u306f1\u3064\u306e\u5909\u6570\u3067\u3059\u3002\u4f8b\u3048\u3070\u3001\u914d\u5217nums\u306b\u306fnums[0]\u3001nums[1]\u306a\u3069\u306e\u8981\u7d20\u304c\u542b\u307e\u308c\u307e\u3059\u304c\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u8907\u6570\u306e\u7570\u306a\u308b\u30ce\u30fc\u30c9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u69cb\u6210\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u901a\u5e38\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u305d\u306e\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u3067\u53c2\u7167\u3055\u308c\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u524d\u306e\u30b3\u30fc\u30c9\u30b9\u30cb\u30da\u30c3\u30c8\u306e\u9023\u7d50\u30ea\u30b9\u30c8\u306fn0\u3068\u3057\u3066\u53c2\u7167\u3055\u308c\u307e\u3059\u3002

        "},{"location":"chapter_array_and_linkedlist/linked_list/#2","title":"2. \u00a0 \u30ce\u30fc\u30c9\u306e\u633f\u5165","text":"

        \u9023\u7d50\u30ea\u30b9\u30c8\u306b\u30ce\u30fc\u30c9\u3092\u633f\u5165\u3059\u308b\u306e\u306f\u975e\u5e38\u306b\u7c21\u5358\u3067\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u96a3\u63a5\u3059\u308b2\u3064\u306e\u30ce\u30fc\u30c9n0\u3068n1\u306e\u9593\u306b\u65b0\u3057\u3044\u30ce\u30fc\u30c9P\u3092\u633f\u5165\u3059\u308b\u3053\u3068\u3092\u76ee\u6307\u3059\u3068\u3057\u307e\u3059\u3002\u3053\u308c\u306f2\u3064\u306e\u30ce\u30fc\u30c9\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u5909\u66f4\u3059\u308b\u3060\u3051\u3067\u5b9f\u73fe\u3067\u304d\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u3067\u3059\u3002

        \u6bd4\u8f03\u3059\u308b\u3068\u3001\u914d\u5217\u306b\u8981\u7d20\u3092\u633f\u5165\u3059\u308b\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3042\u308a\u3001\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u6271\u3046\u5834\u5408\u306b\u306f\u52b9\u7387\u304c\u60aa\u304f\u306a\u308a\u307e\u3059\u3002

        \u56f3 4-6 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u633f\u5165\u306e\u4f8b

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
        def insert(n0: ListNode, P: ListNode):\n    \"\"\"\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30ce\u30fc\u30c9 n0 \u306e\u5f8c\u306b\u30ce\u30fc\u30c9 P \u3092\u633f\u5165\"\"\"\n    n1 = n0.next\n    P.next = n1\n    n0.next = P\n
        linked_list.cpp
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30ce\u30fc\u30c9n0\u306e\u5f8c\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165 */\nvoid insert(ListNode *n0, ListNode *P) {\n    ListNode *n1 = n0->next;\n    P->next = n1;\n    n0->next = P;\n}\n
        linked_list.java
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3067\u30ce\u30fc\u30c9 n0 \u306e\u5f8c\u306b\u30ce\u30fc\u30c9 P \u3092\u633f\u5165 */\nvoid insert(ListNode n0, ListNode P) {\n    ListNode n1 = n0.next;\n    P.next = n1;\n    n0.next = P;\n}\n
        linked_list.cs
        [class]{linked_list}-[func]{Insert}\n
        linked_list.go
        [class]{}-[func]{insertNode}\n
        linked_list.swift
        [class]{}-[func]{insert}\n
        linked_list.js
        [class]{}-[func]{insert}\n
        linked_list.ts
        [class]{}-[func]{insert}\n
        linked_list.dart
        [class]{}-[func]{insert}\n
        linked_list.rs
        [class]{}-[func]{insert}\n
        linked_list.c
        [class]{}-[func]{insert}\n
        linked_list.kt
        [class]{}-[func]{insert}\n
        linked_list.rb
        [class]{}-[func]{insert}\n
        "},{"location":"chapter_array_and_linkedlist/linked_list/#3","title":"3. \u00a0 \u30ce\u30fc\u30c9\u306e\u524a\u9664","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u304b\u3089\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3059\u308b\u3053\u3068\u3082\u975e\u5e38\u306b\u7c21\u5358\u3067\u30011\u3064\u306e\u30ce\u30fc\u30c9\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u5909\u66f4\u3059\u308b\u3060\u3051\u3067\u3059\u3002

        \u91cd\u8981\u306a\u70b9\u306f\u3001\u30ce\u30fc\u30c9P\u304c\u524a\u9664\u3055\u308c\u305f\u5f8c\u3082n1\u3092\u6307\u3057\u7d9a\u3051\u3066\u3044\u308b\u3053\u3068\u3067\u3059\u304c\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u5de1\u56de\u4e2d\u306b\u306f\u30a2\u30af\u30bb\u30b9\u3067\u304d\u306a\u304f\u306a\u308b\u3053\u3068\u3067\u3059\u3002\u3053\u308c\u306f\u4e8b\u5b9f\u4e0a\u3001P\u304c\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u4e00\u90e8\u3067\u306f\u306a\u304f\u306a\u3063\u305f\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

        \u56f3 4-7 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u306e\u524a\u9664

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
        def remove(n0: ListNode):\n    \"\"\"\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30ce\u30fc\u30c9 n0 \u306e\u5f8c\u306e\u6700\u521d\u306e\u30ce\u30fc\u30c9\u3092\u524a\u9664\"\"\"\n    if not n0.next:\n        return\n    # n0 -> P -> n1\n    P = n0.next\n    n1 = P.next\n    n0.next = n1\n
        linked_list.cpp
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30ce\u30fc\u30c9n0\u306e\u5f8c\u306e\u6700\u521d\u306e\u30ce\u30fc\u30c9\u3092\u524a\u9664 */\nvoid remove(ListNode *n0) {\n    if (n0->next == nullptr)\n        return;\n    // n0 -> P -> n1\n    ListNode *P = n0->next;\n    ListNode *n1 = P->next;\n    n0->next = n1;\n    // \u30e1\u30e2\u30ea\u3092\u89e3\u653e\n    delete P;\n}\n
        linked_list.java
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3067\u30ce\u30fc\u30c9 n0 \u306e\u5f8c\u306e\u6700\u521d\u306e\u30ce\u30fc\u30c9\u3092\u524a\u9664 */\nvoid remove(ListNode n0) {\n    if (n0.next == null)\n        return;\n    // n0 -> P -> n1\n    ListNode P = n0.next;\n    ListNode n1 = P.next;\n    n0.next = n1;\n}\n
        linked_list.cs
        [class]{linked_list}-[func]{Remove}\n
        linked_list.go
        [class]{}-[func]{removeItem}\n
        linked_list.swift
        [class]{}-[func]{remove}\n
        linked_list.js
        [class]{}-[func]{remove}\n
        linked_list.ts
        [class]{}-[func]{remove}\n
        linked_list.dart
        [class]{}-[func]{remove}\n
        linked_list.rs
        [class]{}-[func]{remove}\n
        linked_list.c
        [class]{}-[func]{removeItem}\n
        linked_list.kt
        [class]{}-[func]{remove}\n
        linked_list.rb
        [class]{}-[func]{remove}\n
        "},{"location":"chapter_array_and_linkedlist/linked_list/#4","title":"4. \u00a0 \u30ce\u30fc\u30c9\u3078\u306e\u30a2\u30af\u30bb\u30b9","text":"

        \u9023\u7d50\u30ea\u30b9\u30c8\u3067\u306e\u30ce\u30fc\u30c9\u3078\u306e\u30a2\u30af\u30bb\u30b9\u306f\u52b9\u7387\u304c\u60aa\u3044\u3067\u3059\u3002\u524d\u8ff0\u3057\u305f\u3088\u3046\u306b\u3001\u914d\u5217\u306e\u4efb\u610f\u306e\u8981\u7d20\u306b\u306f\\(O(1)\\)\u6642\u9593\u3067\u30a2\u30af\u30bb\u30b9\u3067\u304d\u307e\u3059\u3002\u5bfe\u7167\u7684\u306b\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u306f\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u304b\u3089\u958b\u59cb\u3057\u3066\u76ee\u7684\u306e\u30ce\u30fc\u30c9\u304c\u898b\u3064\u304b\u308b\u307e\u3067\u9806\u6b21\u30ce\u30fc\u30c9\u3092\u5de1\u56de\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\\(i\\)\u756a\u76ee\u306e\u30ce\u30fc\u30c9\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u306b\u306f\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\\(i - 1\\)\u500b\u306e\u30ce\u30fc\u30c9\u3092\u53cd\u5fa9\u51e6\u7406\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u306b\u306a\u308a\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
        def access(head: ListNode, index: int) -> ListNode | None:\n    \"\"\"\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 index \u306e\u30ce\u30fc\u30c9\u306b\u30a2\u30af\u30bb\u30b9\"\"\"\n    for _ in range(index):\n        if not head:\n            return None\n        head = head.next\n    return head\n
        linked_list.cpp
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u306e`index`\u756a\u76ee\u306e\u30ce\u30fc\u30c9\u306b\u30a2\u30af\u30bb\u30b9 */\nListNode *access(ListNode *head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == nullptr)\n            return nullptr;\n        head = head->next;\n    }\n    return head;\n}\n
        linked_list.java
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u306e `index` \u306e\u30ce\u30fc\u30c9\u306b\u30a2\u30af\u30bb\u30b9 */\nListNode access(ListNode head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == null)\n            return null;\n        head = head.next;\n    }\n    return head;\n}\n
        linked_list.cs
        [class]{linked_list}-[func]{Access}\n
        linked_list.go
        [class]{}-[func]{access}\n
        linked_list.swift
        [class]{}-[func]{access}\n
        linked_list.js
        [class]{}-[func]{access}\n
        linked_list.ts
        [class]{}-[func]{access}\n
        linked_list.dart
        [class]{}-[func]{access}\n
        linked_list.rs
        [class]{}-[func]{access}\n
        linked_list.c
        [class]{}-[func]{access}\n
        linked_list.kt
        [class]{}-[func]{access}\n
        linked_list.rb
        [class]{}-[func]{access}\n
        "},{"location":"chapter_array_and_linkedlist/linked_list/#5","title":"5. \u00a0 \u30ce\u30fc\u30c9\u306e\u691c\u7d22","text":"

        \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u5de1\u56de\u3057\u3066\u3001\u5024\u304ctarget\u306b\u4e00\u81f4\u3059\u308b\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u5185\u3067\u306e\u305d\u306e\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u51fa\u529b\u3057\u307e\u3059\u3002\u3053\u306e\u624b\u9806\u3082\u7dda\u5f62\u691c\u7d22\u306e\u4f8b\u3067\u3059\u3002\u5bfe\u5fdc\u3059\u308b\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
        def find(head: ListNode, target: int) -> int:\n    \"\"\"\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u5024 target \u3092\u6301\u3064\u6700\u521d\u306e\u30ce\u30fc\u30c9\u3092\u691c\u7d22\"\"\"\n    index = 0\n    while head:\n        if head.val == target:\n            return index\n        head = head.next\n        index += 1\n    return -1\n
        linked_list.cpp
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3067\u5024\u304ctarget\u306e\u6700\u521d\u306e\u30ce\u30fc\u30c9\u3092\u691c\u7d22 */\nint find(ListNode *head, int target) {\n    int index = 0;\n    while (head != nullptr) {\n        if (head->val == target)\n            return index;\n        head = head->next;\n        index++;\n    }\n    return -1;\n}\n
        linked_list.java
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u3067\u5024 target \u3092\u6301\u3064\u6700\u521d\u306e\u30ce\u30fc\u30c9\u3092\u691c\u7d22 */\nint find(ListNode head, int target) {\n    int index = 0;\n    while (head != null) {\n        if (head.val == target)\n            return index;\n        head = head.next;\n        index++;\n    }\n    return -1;\n}\n
        linked_list.cs
        [class]{linked_list}-[func]{Find}\n
        linked_list.go
        [class]{}-[func]{findNode}\n
        linked_list.swift
        [class]{}-[func]{find}\n
        linked_list.js
        [class]{}-[func]{find}\n
        linked_list.ts
        [class]{}-[func]{find}\n
        linked_list.dart
        [class]{}-[func]{find}\n
        linked_list.rs
        [class]{}-[func]{find}\n
        linked_list.c
        [class]{}-[func]{find}\n
        linked_list.kt
        [class]{}-[func]{find}\n
        linked_list.rb
        [class]{}-[func]{find}\n
        "},{"location":"chapter_array_and_linkedlist/linked_list/#422-vs","title":"4.2.2 \u00a0 \u914d\u5217 vs. \u9023\u7d50\u30ea\u30b9\u30c8","text":"

        \u4e0b\u8868\u306f\u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u7279\u6027\u3092\u307e\u3068\u3081\u3001\u69d8\u3005\u306a\u64cd\u4f5c\u306b\u304a\u3051\u308b\u52b9\u7387\u3082\u6bd4\u8f03\u3057\u3066\u3044\u307e\u3059\u3002\u305d\u308c\u305e\u308c\u304c\u5bfe\u7167\u7684\u306a\u683c\u7d0d\u6226\u7565\u3092\u4f7f\u7528\u3059\u308b\u305f\u3081\u3001\u305d\u308c\u305e\u308c\u306e\u7279\u6027\u3068\u64cd\u4f5c\u52b9\u7387\u306f\u660e\u78ba\u306b\u5bfe\u6bd4\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        \u8868 4-1 \u00a0 \u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u52b9\u7387\u6bd4\u8f03

        \u914d\u5217 \u9023\u7d50\u30ea\u30b9\u30c8 \u683c\u7d0d\u65b9\u5f0f \u9023\u7d9a\u30e1\u30e2\u30ea\u7a7a\u9593 \u5206\u6563\u30e1\u30e2\u30ea\u7a7a\u9593 \u5bb9\u91cf\u62e1\u5f35 \u56fa\u5b9a\u9577 \u67d4\u8edf\u306a\u62e1\u5f35 \u30e1\u30e2\u30ea\u52b9\u7387 \u8981\u7d20\u3042\u305f\u308a\u306e\u30e1\u30e2\u30ea\u5c11\u3001\u6f5c\u5728\u7684\u306a\u7a7a\u9593\u306e\u7121\u99c4 \u8981\u7d20\u3042\u305f\u308a\u306e\u30e1\u30e2\u30ea\u591a \u8981\u7d20\u3078\u306e\u30a2\u30af\u30bb\u30b9 \\(O(1)\\) \\(O(n)\\) \u8981\u7d20\u306e\u8ffd\u52a0 \\(O(n)\\) \\(O(1)\\) \u8981\u7d20\u306e\u524a\u9664 \\(O(n)\\) \\(O(1)\\)"},{"location":"chapter_array_and_linkedlist/linked_list/#423","title":"4.2.3 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u4e00\u822c\u7684\u306a\u7a2e\u985e","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u306f3\u3064\u306e\u4e00\u822c\u7684\u306a\u7a2e\u985e\u304c\u3042\u308a\u307e\u3059\u3002

        • \u5358\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\uff1a\u3053\u308c\u306f\u524d\u8ff0\u3057\u305f\u6a19\u6e96\u7684\u306a\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u3059\u3002\u5358\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30ce\u30fc\u30c9\u306b\u306f\u5024\u3068\u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\u304c\u542b\u307e\u308c\u307e\u3059\u3002\u6700\u521d\u306e\u30ce\u30fc\u30c9\u306f\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u3068\u547c\u3070\u308c\u3001null\uff08None\uff09\u3092\u6307\u3059\u6700\u5f8c\u306e\u30ce\u30fc\u30c9\u306f\u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u3067\u3059\u3002
        • \u5faa\u74b0\u9023\u7d50\u30ea\u30b9\u30c8\uff1a\u3053\u308c\u306f\u5358\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u304c\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u3092\u6307\u3057\u3066\u30eb\u30fc\u30d7\u3092\u4f5c\u308b\u3053\u3068\u3067\u5f62\u6210\u3055\u308c\u307e\u3059\u3002\u5faa\u74b0\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u306f\u3001\u4efb\u610f\u306e\u30ce\u30fc\u30c9\u304c\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u3068\u3057\u3066\u6a5f\u80fd\u3067\u304d\u307e\u3059\u3002
        • \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\uff1a\u5358\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u3068\u306f\u5bfe\u7167\u7684\u306b\u3001\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306f2\u3064\u306e\u65b9\u5411\u3067\u53c2\u7167\u3092\u7dad\u6301\u3057\u307e\u3059\u3002\u5404\u30ce\u30fc\u30c9\u306b\u306f\u5f8c\u7d9a\u8005\uff08\u6b21\u306e\u30ce\u30fc\u30c9\uff09\u3068\u524d\u4efb\u8005\uff08\u524d\u306e\u30ce\u30fc\u30c9\uff09\u306e\u4e21\u65b9\u3078\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u304c\u542b\u307e\u308c\u307e\u3059\u3002\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u3069\u3061\u3089\u306e\u65b9\u5411\u306b\u3082\u5de1\u56de\u3067\u304d\u308b\u3088\u308a\u591a\u304f\u306e\u67d4\u8edf\u6027\u3092\u63d0\u4f9b\u3057\u307e\u3059\u304c\u3001\u3088\u308a\u591a\u304f\u306e\u30e1\u30e2\u30ea\u7a7a\u9593\u3082\u6d88\u8cbb\u3057\u307e\u3059\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin
        class ListNode:\n    \"\"\"\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val                # \u30ce\u30fc\u30c9\u5024\n        self.next: ListNode | None = None  # \u5f8c\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n        self.prev: ListNode | None = None  # \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n
        /* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u69cb\u9020\u4f53 */\nstruct ListNode {\n    int val;         // \u30ce\u30fc\u30c9\u5024\n    ListNode *next;  // \u5f8c\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    ListNode *prev;  // \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    ListNode(int x) : val(x), next(nullptr), prev(nullptr) {}  // \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\n};\n
        /* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode {\n    int val;        // \u30ce\u30fc\u30c9\u5024\n    ListNode next;  // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    ListNode prev;  // \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    ListNode(int x) { val = x; }  // \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\n}\n
        /* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode(int x) {  // \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\n    int val = x;    // \u30ce\u30fc\u30c9\u5024\n    ListNode next;  // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    ListNode prev;  // \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n}\n
        /* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u69cb\u9020\u4f53 */\ntype DoublyListNode struct {\n    Val  int             // \u30ce\u30fc\u30c9\u5024\n    Next *DoublyListNode // \u5f8c\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    Prev *DoublyListNode // \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n}\n\n// NewDoublyListNode \u521d\u671f\u5316\nfunc NewDoublyListNode(val int) *DoublyListNode {\n    return &DoublyListNode{\n        Val:  val,\n        Next: nil,\n        Prev: nil,\n    }\n}\n
        /* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode {\n    var val: Int // \u30ce\u30fc\u30c9\u5024\n    var next: ListNode? // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    var prev: ListNode? // \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n\n    init(x: Int) { // \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\n        val = x\n    }\n}\n
        /* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode {\n    constructor(val, next, prev) {\n        this.val = val  ===  undefined ? 0 : val;        // \u30ce\u30fc\u30c9\u5024\n        this.next = next  ===  undefined ? null : next;  // \u5f8c\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n        this.prev = prev  ===  undefined ? null : prev;  // \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    }\n}\n
        /* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode {\n    val: number;\n    next: ListNode | null;\n    prev: ListNode | null;\n    constructor(val?: number, next?: ListNode | null, prev?: ListNode | null) {\n        this.val = val  ===  undefined ? 0 : val;        // \u30ce\u30fc\u30c9\u5024\n        this.next = next  ===  undefined ? null : next;  // \u5f8c\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n        this.prev = prev  ===  undefined ? null : prev;  // \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    }\n}\n
        /* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u30af\u30e9\u30b9 */\nclass ListNode {\n    int val;        // \u30ce\u30fc\u30c9\u5024\n    ListNode next;  // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    ListNode prev;  // \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    ListNode(this.val, [this.next, this.prev]);  // \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\n}\n
        use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u578b */\n#[derive(Debug)]\nstruct ListNode {\n    val: i32, // \u30ce\u30fc\u30c9\u5024\n    next: Option<Rc<RefCell<ListNode>>>, // \u5f8c\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    prev: Option<Rc<RefCell<ListNode>>>, // \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n}\n\n/* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\nimpl ListNode {\n    fn new(val: i32) -> Self {\n        ListNode {\n            val,\n            next: None,\n            prev: None,\n        }\n    }\n}\n
        /* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u69cb\u9020\u4f53 */\ntypedef struct ListNode {\n    int val;               // \u30ce\u30fc\u30c9\u5024\n    struct ListNode *next; // \u5f8c\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    struct ListNode *prev; // \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n} ListNode;\n\n/* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\nListNode *newListNode(int val) {\n    ListNode *node, *next;\n    node = (ListNode *) malloc(sizeof(ListNode));\n    node->val = val;\n    node->next = NULL;\n    node->prev = NULL;\n    return node;\n}\n
        \n

        \u56f3 4-8 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u4e00\u822c\u7684\u306a\u7a2e\u985e

        "},{"location":"chapter_array_and_linkedlist/linked_list/#424","title":"4.2.4 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u5178\u578b\u7684\u306a\u5fdc\u7528","text":"

        \u5358\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u3001\u30b9\u30bf\u30c3\u30af\u3001\u30ad\u30e5\u30fc\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3001\u30b0\u30e9\u30d5\u306e\u5b9f\u88c5\u306b\u3088\u304f\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002

        • \u30b9\u30bf\u30c3\u30af\u3068\u30ad\u30e5\u30fc\uff1a\u5358\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u3001\u633f\u5165\u3068\u524a\u9664\u304c\u540c\u3058\u7aef\u3067\u884c\u308f\u308c\u308b\u5834\u5408\u3001\u30b9\u30bf\u30c3\u30af\uff08\u5f8c\u5165\u5148\u51fa\uff09\u306e\u3088\u3046\u306b\u52d5\u4f5c\u3057\u307e\u3059\u3002\u9006\u306b\u3001\u633f\u5165\u304c\u4e00\u65b9\u306e\u7aef\u3067\u3001\u524a\u9664\u304c\u3082\u3046\u4e00\u65b9\u306e\u7aef\u3067\u884c\u308f\u308c\u308b\u5834\u5408\u3001\u30ad\u30e5\u30fc\uff08\u5148\u5165\u5148\u51fa\uff09\u306e\u3088\u3046\u306b\u6a5f\u80fd\u3057\u307e\u3059\u3002
        • \u30cf\u30c3\u30b7\u30e5\u8868\uff1a\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u89e3\u6c7a\u3059\u308b\u4eba\u6c17\u306e\u65b9\u6cd5\u3067\u3042\u308b\u9023\u9396\u6cd5\u3067\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u3053\u3053\u3067\u306f\u3001\u3059\u3079\u3066\u306e\u885d\u7a81\u3057\u305f\u8981\u7d20\u304c\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u30b0\u30eb\u30fc\u30d7\u5316\u3055\u308c\u307e\u3059\u3002
        • \u30b0\u30e9\u30d5\uff1a\u30b0\u30e9\u30d5\u8868\u73fe\u306e\u6a19\u6e96\u7684\u306a\u65b9\u6cd5\u3067\u3042\u308b\u96a3\u63a5\u30ea\u30b9\u30c8\u306f\u3001\u5404\u30b0\u30e9\u30d5\u9802\u70b9\u3092\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u95a2\u9023\u4ed8\u3051\u307e\u3059\u3002\u3053\u306e\u30ea\u30b9\u30c8\u306b\u306f\u3001\u5bfe\u5fdc\u3059\u308b\u9802\u70b9\u306b\u63a5\u7d9a\u3055\u308c\u305f\u9802\u70b9\u3092\u8868\u3059\u8981\u7d20\u304c\u542b\u307e\u308c\u307e\u3059\u3002

        \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u3001\u524d\u5f8c\u306e\u8981\u7d20\u3078\u306e\u9ad8\u901f\u30a2\u30af\u30bb\u30b9\u304c\u5fc5\u8981\u306a\u30b7\u30ca\u30ea\u30aa\u306b\u6700\u9069\u3067\u3059\u3002

        • \u9ad8\u5ea6\u306a\u30c7\u30fc\u30bf\u69cb\u9020\uff1a\u8d64\u9ed2\u6728\u3084B\u6728\u306a\u3069\u306e\u69cb\u9020\u3067\u306f\u3001\u30ce\u30fc\u30c9\u306e\u89aa\u3078\u306e\u30a2\u30af\u30bb\u30b9\u304c\u91cd\u8981\u3067\u3059\u3002\u3053\u308c\u306f\u5404\u30ce\u30fc\u30c9\u306b\u89aa\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\u3092\u7d44\u307f\u8fbc\u3080\u3053\u3068\u3067\u5b9f\u73fe\u3055\u308c\u3001\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u4f3c\u3066\u3044\u307e\u3059\u3002
        • \u30d6\u30e9\u30a6\u30b6\u5c65\u6b74\uff1aWeb\u30d6\u30e9\u30a6\u30b6\u3067\u306f\u3001\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u3088\u308a\u3001\u30e6\u30fc\u30b6\u30fc\u304c\u524d\u9032\u307e\u305f\u306f\u5f8c\u9000\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3057\u305f\u3068\u304d\u306e\u8a2a\u554f\u30da\u30fc\u30b8\u306e\u5c65\u6b74\u30ca\u30d3\u30b2\u30fc\u30b7\u30e7\u30f3\u304c\u5bb9\u6613\u306b\u306a\u308a\u307e\u3059\u3002
        • LRU\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u3001\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\uff08LRU\uff09\u30ad\u30e3\u30c3\u30b7\u30e5\u524a\u9664\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u9069\u3057\u3066\u304a\u308a\u3001\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u30c7\u30fc\u30bf\u306e\u8fc5\u901f\u306a\u8b58\u5225\u3068\u3001\u9ad8\u901f\u306a\u30ce\u30fc\u30c9\u8ffd\u52a0\u30fb\u524a\u9664\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002

        \u5faa\u74b0\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u3001\u30aa\u30da\u30ec\u30fc\u30c6\u30a3\u30f3\u30b0\u30b7\u30b9\u30c6\u30e0\u3067\u306e\u30ea\u30bd\u30fc\u30b9\u30b9\u30b1\u30b8\u30e5\u30fc\u30ea\u30f3\u30b0\u306a\u3069\u3001\u5468\u671f\u7684\u306a\u64cd\u4f5c\u304c\u5fc5\u8981\u306a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306b\u6700\u9069\u3067\u3059\u3002

        • \u30e9\u30a6\u30f3\u30c9\u30ed\u30d3\u30f3\u30b9\u30b1\u30b8\u30e5\u30fc\u30ea\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u30aa\u30da\u30ec\u30fc\u30c6\u30a3\u30f3\u30b0\u30b7\u30b9\u30c6\u30e0\u3067\u306f\u3001\u30e9\u30a6\u30f3\u30c9\u30ed\u30d3\u30f3\u30b9\u30b1\u30b8\u30e5\u30fc\u30ea\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u4e00\u822c\u7684\u306aCPU\u30b9\u30b1\u30b8\u30e5\u30fc\u30ea\u30f3\u30b0\u65b9\u6cd5\u3067\u3042\u308a\u3001\u30d7\u30ed\u30bb\u30b9\u306e\u30b0\u30eb\u30fc\u30d7\u3092\u5faa\u74b0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u5404\u30d7\u30ed\u30bb\u30b9\u306b\u306f\u30bf\u30a4\u30e0\u30b9\u30e9\u30a4\u30b9\u304c\u5272\u308a\u5f53\u3066\u3089\u308c\u3001\u671f\u9650\u5207\u308c\u306b\u306a\u308b\u3068CPU\u306f\u6b21\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u56de\u8ee2\u3057\u307e\u3059\u3002\u3053\u306e\u5faa\u74b0\u64cd\u4f5c\u306f\u5faa\u74b0\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u52b9\u7387\u7684\u306b\u5b9f\u73fe\u3067\u304d\u3001\u3059\u3079\u3066\u306e\u30d7\u30ed\u30bb\u30b9\u9593\u3067\u516c\u5e73\u304b\u3064\u6642\u5206\u5272\u30b7\u30b9\u30c6\u30e0\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002
        • \u30c7\u30fc\u30bf\u30d0\u30c3\u30d5\u30a1\uff1a\u5faa\u74b0\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u3001\u30aa\u30fc\u30c7\u30a3\u30aa\u3084\u30d3\u30c7\u30aa\u30d7\u30ec\u30fc\u30e4\u30fc\u306a\u3069\u306e\u30c7\u30fc\u30bf\u30d0\u30c3\u30d5\u30a1\u3067\u3082\u4f7f\u7528\u3055\u308c\u3001\u30c7\u30fc\u30bf\u30b9\u30c8\u30ea\u30fc\u30e0\u304c\u8907\u6570\u306e\u30d0\u30c3\u30d5\u30a1\u30d6\u30ed\u30c3\u30af\u306b\u5206\u5272\u3055\u308c\u3001\u30b7\u30fc\u30e0\u30ec\u30b9\u306a\u518d\u751f\u306e\u305f\u3081\u306b\u5faa\u74b0\u65b9\u5f0f\u3067\u914d\u7f6e\u3055\u308c\u307e\u3059\u3002
        "},{"location":"chapter_array_and_linkedlist/list/","title":"4.3 \u00a0 \u30ea\u30b9\u30c8","text":"

        \u30ea\u30b9\u30c8\u306f\u3001\u8981\u7d20\u3078\u306e\u30a2\u30af\u30bb\u30b9\u3001\u5909\u66f4\u3001\u8ffd\u52a0\u3001\u524a\u9664\u3001\u8d70\u67fb\u306a\u3069\u306e\u64cd\u4f5c\u3092\u30b5\u30dd\u30fc\u30c8\u3059\u308b\u3001\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u8981\u7d20\u306e\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3092\u8868\u3059\u62bd\u8c61\u7684\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u6982\u5ff5\u3067\u3042\u308a\u3001\u30e6\u30fc\u30b6\u30fc\u304c\u5bb9\u91cf\u5236\u9650\u3092\u8003\u616e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u305b\u3093\u3002\u30ea\u30b9\u30c8\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u307e\u305f\u306f\u914d\u5217\u306b\u57fa\u3065\u3044\u3066\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002

        • \u9023\u7d50\u30ea\u30b9\u30c8\u306f\u672c\u8cea\u7684\u306b\u30ea\u30b9\u30c8\u3068\u3057\u3066\u6a5f\u80fd\u3057\u3001\u8981\u7d20\u306e\u8ffd\u52a0\u3001\u524a\u9664\u3001\u691c\u7d22\u3001\u5909\u66f4\u306e\u64cd\u4f5c\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3001\u30b5\u30a4\u30ba\u3092\u52d5\u7684\u306b\u8abf\u6574\u3059\u308b\u67d4\u8edf\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        • \u914d\u5217\u3082\u3053\u308c\u3089\u306e\u64cd\u4f5c\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u307e\u3059\u304c\u3001\u9577\u3055\u304c\u4e0d\u5909\u3067\u3042\u308b\u305f\u3081\u3001\u9577\u3055\u5236\u9650\u306e\u3042\u308b\u30ea\u30b9\u30c8\u3068\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u914d\u5217\u3092\u4f7f\u7528\u3057\u3066\u30ea\u30b9\u30c8\u3092\u5b9f\u88c5\u3059\u308b\u5834\u5408\u3001\u9577\u3055\u306e\u4e0d\u5909\u6027\u306b\u3088\u308a\u30ea\u30b9\u30c8\u306e\u5b9f\u7528\u6027\u304c\u4f4e\u4e0b\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u4e8b\u524d\u306b\u683c\u7d0d\u3059\u308b\u30c7\u30fc\u30bf\u91cf\u3092\u4e88\u6e2c\u3059\u308b\u3053\u3068\u304c\u56f0\u96e3\u306a\u5834\u5408\u304c\u591a\u304f\u3001\u9069\u5207\u306a\u30ea\u30b9\u30c8\u9577\u3092\u9078\u629e\u3059\u308b\u3053\u3068\u304c\u56f0\u96e3\u3067\u3042\u308b\u305f\u3081\u3067\u3059\u3002\u9577\u3055\u304c\u5c0f\u3055\u3059\u304e\u308b\u3068\u8981\u4ef6\u3092\u6e80\u305f\u3055\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u5927\u304d\u3059\u304e\u308b\u3068\u30e1\u30e2\u30ea\u7a7a\u9593\u3092\u7121\u99c4\u306b\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        \u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3001\u52d5\u7684\u914d\u5217\u3092\u4f7f\u7528\u3057\u3066\u30ea\u30b9\u30c8\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306f\u914d\u5217\u306e\u5229\u70b9\u3092\u7d99\u627f\u3057\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c\u4e2d\u306b\u52d5\u7684\u306b\u62e1\u5f35\u3067\u304d\u307e\u3059\u3002

        \u5b9f\u969b\u3001\u591a\u304f\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u52d5\u7684\u914d\u5217\u3092\u4f7f\u7528\u3057\u3066\u30ea\u30b9\u30c8\u3092\u5b9f\u88c5\u3057\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001Python\u306elist\u3001Java\u306eArrayList\u3001C++\u306evector\u3001C#\u306eList\u306a\u3069\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u8b70\u8ad6\u3067\u306f\u3001\u300c\u30ea\u30b9\u30c8\u300d\u3068\u300c\u52d5\u7684\u914d\u5217\u300d\u3092\u540c\u7fa9\u306e\u6982\u5ff5\u3068\u3057\u3066\u6271\u3044\u307e\u3059\u3002

        "},{"location":"chapter_array_and_linkedlist/list/#431","title":"4.3.1 \u00a0 \u30ea\u30b9\u30c8\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/list/#1","title":"1. \u00a0 \u30ea\u30b9\u30c8\u306e\u521d\u671f\u5316","text":"

        \u901a\u5e38\u3001\u300c\u521d\u671f\u5024\u306a\u3057\u300d\u3068\u300c\u521d\u671f\u5024\u3042\u308a\u300d\u306e2\u3064\u306e\u521d\u671f\u5316\u65b9\u6cd5\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin list.py
        # \u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316\n# \u521d\u671f\u5024\u306a\u3057\nnums1: list[int] = []\n# \u521d\u671f\u5024\u3042\u308a\nnums: list[int] = [1, 3, 2, 5, 4]\n
        list.cpp
        /* \u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316 */\n// \u6ce8\u610f: C++\u3067\u306f\u3001vector\u304c\u3053\u3053\u3067\u8aac\u660e\u3055\u308c\u3066\u3044\u308bnums\u306b\u76f8\u5f53\u3057\u307e\u3059\n// \u521d\u671f\u5024\u306a\u3057\nvector<int> nums1;\n// \u521d\u671f\u5024\u3042\u308a\nvector<int> nums = { 1, 3, 2, 5, 4 };\n
        list.java
        /* \u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316 */\n// \u521d\u671f\u5024\u306a\u3057\nList<Integer> nums1 = new ArrayList<>();\n// \u521d\u671f\u5024\u3042\u308a\uff08\u8981\u7d20\u578b\u306fint[]\u306e\u30e9\u30c3\u30d1\u30fc\u30af\u30e9\u30b9Integer[]\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff09\nInteger[] numbers = new Integer[] { 1, 3, 2, 5, 4 };\nList<Integer> nums = new ArrayList<>(Arrays.asList(numbers));\n
        list.cs
        /* \u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316 */\n// \u521d\u671f\u5024\u306a\u3057\nList<int> nums1 = [];\n// \u521d\u671f\u5024\u3042\u308a\nint[] numbers = [1, 3, 2, 5, 4];\nList<int> nums = [.. numbers];\n
        list_test.go
        /* \u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316 */\n// \u521d\u671f\u5024\u306a\u3057\nnums1 := []int{}\n// \u521d\u671f\u5024\u3042\u308a\nnums := []int{1, 3, 2, 5, 4}\n
        list.swift
        /* \u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316 */\n// \u521d\u671f\u5024\u306a\u3057\nlet nums1: [Int] = []\n// \u521d\u671f\u5024\u3042\u308a\nvar nums = [1, 3, 2, 5, 4]\n
        list.js
        /* \u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316 */\n// \u521d\u671f\u5024\u306a\u3057\nconst nums1 = [];\n// \u521d\u671f\u5024\u3042\u308a\nconst nums = [1, 3, 2, 5, 4];\n
        list.ts
        /* \u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316 */\n// \u521d\u671f\u5024\u306a\u3057\nconst nums1: number[] = [];\n// \u521d\u671f\u5024\u3042\u308a\nconst nums: number[] = [1, 3, 2, 5, 4];\n
        list.dart
        /* \u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316 */\n// \u521d\u671f\u5024\u306a\u3057\nList<int> nums1 = [];\n// \u521d\u671f\u5024\u3042\u308a\nList<int> nums = [1, 3, 2, 5, 4];\n
        list.rs
        /* \u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316 */\n// \u521d\u671f\u5024\u306a\u3057\nlet nums1: Vec<i32> = Vec::new();\n// \u521d\u671f\u5024\u3042\u308a\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
        list.c
        // C\u306f\u7d44\u307f\u8fbc\u307f\u306e\u52d5\u7684\u914d\u5217\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        list.kt
        \n
        "},{"location":"chapter_array_and_linkedlist/list/#2","title":"2. \u00a0 \u8981\u7d20\u3078\u306e\u30a2\u30af\u30bb\u30b9","text":"

        \u30ea\u30b9\u30c8\u306f\u672c\u8cea\u7684\u306b\u914d\u5217\u3067\u3042\u308b\u305f\u3081\u3001\\(O(1)\\)\u6642\u9593\u3067\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\u3057\u66f4\u65b0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin list.py
        # \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\nnum: int = nums[1]  # \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\n\n# \u8981\u7d20\u3092\u66f4\u65b0\nnums[1] = 0    # \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u30920\u306b\u66f4\u65b0\n
        list.cpp
        /* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint num = nums[1];  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\n\n/* \u8981\u7d20\u3092\u66f4\u65b0 */\nnums[1] = 0;  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u30920\u306b\u66f4\u65b0\n
        list.java
        /* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint num = nums.get(1);  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\n\n/* \u8981\u7d20\u3092\u66f4\u65b0 */\nnums.set(1, 0);  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u30920\u306b\u66f4\u65b0\n
        list.cs
        /* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint num = nums[1];  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\n\n/* \u8981\u7d20\u3092\u66f4\u65b0 */\nnums[1] = 0;  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u30920\u306b\u66f4\u65b0\n
        list_test.go
        /* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nnum := nums[1]  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\n\n/* \u8981\u7d20\u3092\u66f4\u65b0 */\nnums[1] = 0     // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u30920\u306b\u66f4\u65b0\n
        list.swift
        /* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nlet num = nums[1] // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\n\n/* \u8981\u7d20\u3092\u66f4\u65b0 */\nnums[1] = 0 // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u30920\u306b\u66f4\u65b0\n
        list.js
        /* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nconst num = nums[1];  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\n\n/* \u8981\u7d20\u3092\u66f4\u65b0 */\nnums[1] = 0;  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u30920\u306b\u66f4\u65b0\n
        list.ts
        /* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nconst num: number = nums[1];  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\n\n/* \u8981\u7d20\u3092\u66f4\u65b0 */\nnums[1] = 0;  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u30920\u306b\u66f4\u65b0\n
        list.dart
        /* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint num = nums[1];  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\n\n/* \u8981\u7d20\u3092\u66f4\u65b0 */\nnums[1] = 0;  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u30920\u306b\u66f4\u65b0\n
        list.rs
        /* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nlet num: i32 = nums[1];  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\n/* \u8981\u7d20\u3092\u66f4\u65b0 */\nnums[1] = 0;             // \u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306e\u8981\u7d20\u30920\u306b\u66f4\u65b0\n
        list.c
        // C\u306f\u7d44\u307f\u8fbc\u307f\u306e\u52d5\u7684\u914d\u5217\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        list.kt
        \n
        "},{"location":"chapter_array_and_linkedlist/list/#3","title":"3. \u00a0 \u8981\u7d20\u306e\u633f\u5165\u3068\u524a\u9664","text":"

        \u914d\u5217\u3068\u6bd4\u8f03\u3057\u3066\u3001\u30ea\u30b9\u30c8\u306f\u8981\u7d20\u306e\u8ffd\u52a0\u3068\u524a\u9664\u306b\u304a\u3044\u3066\u3088\u308a\u67d4\u8edf\u6027\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002\u30ea\u30b9\u30c8\u306e\u672b\u5c3e\u3078\u306e\u8981\u7d20\u8ffd\u52a0\u306f\\(O(1)\\)\u64cd\u4f5c\u3067\u3059\u304c\u3001\u30ea\u30b9\u30c8\u306e\u4ed6\u306e\u5834\u6240\u3067\u306e\u8981\u7d20\u306e\u633f\u5165\u3068\u524a\u9664\u306e\u52b9\u7387\u306f\u914d\u5217\u3068\u540c\u3058\u307e\u307e\u3067\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin list.py
        # \u30ea\u30b9\u30c8\u3092\u30af\u30ea\u30a2\nnums.clear()\n\n# \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n# \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165\nnums.insert(3, 6)  # \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306b\u6570\u50246\u3092\u633f\u5165\n\n# \u8981\u7d20\u3092\u524a\u9664\nnums.pop(3)        # \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306e\u8981\u7d20\u3092\u524a\u9664\n
        list.cpp
        /* \u30ea\u30b9\u30c8\u3092\u30af\u30ea\u30a2 */\nnums.clear();\n\n/* \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 */\nnums.push_back(1);\nnums.push_back(3);\nnums.push_back(2);\nnums.push_back(5);\nnums.push_back(4);\n\n/* \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165 */\nnums.insert(nums.begin() + 3, 6);  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306b\u6570\u50246\u3092\u633f\u5165\n\n/* \u8981\u7d20\u3092\u524a\u9664 */\nnums.erase(nums.begin() + 3);      // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306e\u8981\u7d20\u3092\u524a\u9664\n
        list.java
        /* \u30ea\u30b9\u30c8\u3092\u30af\u30ea\u30a2 */\nnums.clear();\n\n/* \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165 */\nnums.add(3, 6);  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306b\u6570\u50246\u3092\u633f\u5165\n\n/* \u8981\u7d20\u3092\u524a\u9664 */\nnums.remove(3);  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306e\u8981\u7d20\u3092\u524a\u9664\n
        list.cs
        /* \u30ea\u30b9\u30c8\u3092\u30af\u30ea\u30a2 */\nnums.Clear();\n\n/* \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 */\nnums.Add(1);\nnums.Add(3);\nnums.Add(2);\nnums.Add(5);\nnums.Add(4);\n\n/* \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165 */\nnums.Insert(3, 6);\n\n/* \u8981\u7d20\u3092\u524a\u9664 */\nnums.RemoveAt(3);\n
        list_test.go
        /* \u30ea\u30b9\u30c8\u3092\u30af\u30ea\u30a2 */\nnums = nil\n\n/* \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 */\nnums = append(nums, 1)\nnums = append(nums, 3)\nnums = append(nums, 2)\nnums = append(nums, 5)\nnums = append(nums, 4)\n\n/* \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165 */\nnums = append(nums[:3], append([]int{6}, nums[3:]...)...) // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306b\u6570\u50246\u3092\u633f\u5165\n\n/* \u8981\u7d20\u3092\u524a\u9664 */\nnums = append(nums[:3], nums[4:]...) // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306e\u8981\u7d20\u3092\u524a\u9664\n
        list.swift
        /* \u30ea\u30b9\u30c8\u3092\u30af\u30ea\u30a2 */\nnums.removeAll()\n\n/* \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 */\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n/* \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165 */\nnums.insert(6, at: 3) // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306b\u6570\u50246\u3092\u633f\u5165\n\n/* \u8981\u7d20\u3092\u524a\u9664 */\nnums.remove(at: 3) // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306e\u8981\u7d20\u3092\u524a\u9664\n
        list.js
        /* \u30ea\u30b9\u30c8\u3092\u30af\u30ea\u30a2 */\nnums.length = 0;\n\n/* \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165 */\nnums.splice(3, 0, 6);\n\n/* \u8981\u7d20\u3092\u524a\u9664 */\nnums.splice(3, 1);\n
        list.ts
        /* \u30ea\u30b9\u30c8\u3092\u30af\u30ea\u30a2 */\nnums.length = 0;\n\n/* \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165 */\nnums.splice(3, 0, 6);\n\n/* \u8981\u7d20\u3092\u524a\u9664 */\nnums.splice(3, 1);\n
        list.dart
        /* \u30ea\u30b9\u30c8\u3092\u30af\u30ea\u30a2 */\nnums.clear();\n\n/* \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165 */\nnums.insert(3, 6); // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306b\u6570\u50246\u3092\u633f\u5165\n\n/* \u8981\u7d20\u3092\u524a\u9664 */\nnums.removeAt(3); // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306e\u8981\u7d20\u3092\u524a\u9664\n
        list.rs
        /* \u30ea\u30b9\u30c8\u3092\u30af\u30ea\u30a2 */\nnums.clear();\n\n/* \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165 */\nnums.insert(3, 6);  // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306b\u6570\u50246\u3092\u633f\u5165\n\n/* \u8981\u7d20\u3092\u524a\u9664 */\nnums.remove(3);    // \u30a4\u30f3\u30c7\u30c3\u30af\u30b93\u306e\u8981\u7d20\u3092\u524a\u9664\n
        list.c
        // C\u306f\u7d44\u307f\u8fbc\u307f\u306e\u52d5\u7684\u914d\u5217\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        list.kt
        \n
        "},{"location":"chapter_array_and_linkedlist/list/#4","title":"4. \u00a0 \u30ea\u30b9\u30c8\u306e\u53cd\u5fa9","text":"

        \u914d\u5217\u3068\u540c\u69d8\u306b\u3001\u30ea\u30b9\u30c8\u306f\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u4f7f\u7528\u3057\u3066\u53cd\u5fa9\u3059\u308b\u3053\u3068\u3082\u3001\u5404\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin list.py
        # \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u30ea\u30b9\u30c8\u3092\u53cd\u5fa9\ncount = 0\nfor i in range(len(nums)):\n    count += nums[i]\n\n# \u30ea\u30b9\u30c8\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9\nfor num in nums:\n    count += num\n
        list.cpp
        /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u30ea\u30b9\u30c8\u3092\u53cd\u5fa9 */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\n    count += nums[i];\n}\n\n/* \u30ea\u30b9\u30c8\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9 */\ncount = 0;\nfor (int num : nums) {\n    count += num;\n}\n
        list.java
        /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u30ea\u30b9\u30c8\u3092\u53cd\u5fa9 */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\n    count += nums.get(i);\n}\n\n/* \u30ea\u30b9\u30c8\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9 */\nfor (int num : nums) {\n    count += num;\n}\n
        list.cs
        /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u30ea\u30b9\u30c8\u3092\u53cd\u5fa9 */\nint count = 0;\nfor (int i = 0; i < nums.Count; i++) {\n    count += nums[i];\n}\n\n/* \u30ea\u30b9\u30c8\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9 */\ncount = 0;\nforeach (int num in nums) {\n    count += num;\n}\n
        list_test.go
        /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u30ea\u30b9\u30c8\u3092\u53cd\u5fa9 */\ncount := 0\nfor i := 0; i < len(nums); i++ {\n    count += nums[i]\n}\n\n/* \u30ea\u30b9\u30c8\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9 */\ncount = 0\nfor _, num := range nums {\n    count += num\n}\n
        list.swift
        /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u30ea\u30b9\u30c8\u3092\u53cd\u5fa9 */\nvar count = 0\nfor i in nums.indices {\n    count += nums[i]\n}\n\n/* \u30ea\u30b9\u30c8\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9 */\ncount = 0\nfor num in nums {\n    count += num\n}\n
        list.js
        /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u30ea\u30b9\u30c8\u3092\u53cd\u5fa9 */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\n    count += nums[i];\n}\n\n/* \u30ea\u30b9\u30c8\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9 */\ncount = 0;\nfor (const num of nums) {\n    count += num;\n}\n
        list.ts
        /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u30ea\u30b9\u30c8\u3092\u53cd\u5fa9 */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\n    count += nums[i];\n}\n\n/* \u30ea\u30b9\u30c8\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9 */\ncount = 0;\nfor (const num of nums) {\n    count += num;\n}\n
        list.dart
        /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u30ea\u30b9\u30c8\u3092\u53cd\u5fa9 */\nint count = 0;\nfor (var i = 0; i < nums.length; i++) {\n    count += nums[i];\n}\n\n/* \u30ea\u30b9\u30c8\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9 */\ncount = 0;\nfor (var num in nums) {\n    count += num;\n}\n
        list.rs
        // \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u30ea\u30b9\u30c8\u3092\u53cd\u5fa9\nlet mut _count = 0;\nfor i in 0..nums.len() {\n    _count += nums[i];\n}\n\n// \u30ea\u30b9\u30c8\u8981\u7d20\u3092\u76f4\u63a5\u53cd\u5fa9\n_count = 0;\nfor num in &nums {\n    _count += num;\n}\n
        list.c
        // C\u306f\u7d44\u307f\u8fbc\u307f\u306e\u52d5\u7684\u914d\u5217\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        list.kt
        \n
        "},{"location":"chapter_array_and_linkedlist/list/#5","title":"5. \u00a0 \u30ea\u30b9\u30c8\u306e\u9023\u7d50","text":"

        \u65b0\u3057\u3044\u30ea\u30b9\u30c8nums1\u304c\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001\u305d\u308c\u3092\u5143\u306e\u30ea\u30b9\u30c8\u306e\u672b\u5c3e\u306b\u8ffd\u52a0\u3067\u304d\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin list.py
        # 2\u3064\u306e\u30ea\u30b9\u30c8\u3092\u9023\u7d50\nnums1: list[int] = [6, 8, 7, 10, 9]\nnums += nums1  # nums1\u3092nums\u306e\u672b\u5c3e\u306b\u9023\u7d50\n
        list.cpp
        /* 2\u3064\u306e\u30ea\u30b9\u30c8\u3092\u9023\u7d50 */\nvector<int> nums1 = { 6, 8, 7, 10, 9 };\n// nums1\u3092nums\u306e\u672b\u5c3e\u306b\u9023\u7d50\nnums.insert(nums.end(), nums1.begin(), nums1.end());\n
        list.java
        /* 2\u3064\u306e\u30ea\u30b9\u30c8\u3092\u9023\u7d50 */\nList<Integer> nums1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));\nnums.addAll(nums1);  // nums1\u3092nums\u306e\u672b\u5c3e\u306b\u9023\u7d50\n
        list.cs
        /* 2\u3064\u306e\u30ea\u30b9\u30c8\u3092\u9023\u7d50 */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.AddRange(nums1);  // nums1\u3092nums\u306e\u672b\u5c3e\u306b\u9023\u7d50\n
        list_test.go
        /* 2\u3064\u306e\u30ea\u30b9\u30c8\u3092\u9023\u7d50 */\nnums1 := []int{6, 8, 7, 10, 9}\nnums = append(nums, nums1...)  // nums1\u3092nums\u306e\u672b\u5c3e\u306b\u9023\u7d50\n
        list.swift
        /* 2\u3064\u306e\u30ea\u30b9\u30c8\u3092\u9023\u7d50 */\nlet nums1 = [6, 8, 7, 10, 9]\nnums.append(contentsOf: nums1) // nums1\u3092nums\u306e\u672b\u5c3e\u306b\u9023\u7d50\n
        list.js
        /* 2\u3064\u306e\u30ea\u30b9\u30c8\u3092\u9023\u7d50 */\nconst nums1 = [6, 8, 7, 10, 9];\nnums.push(...nums1);  // nums1\u3092nums\u306e\u672b\u5c3e\u306b\u9023\u7d50\n
        list.ts
        /* 2\u3064\u306e\u30ea\u30b9\u30c8\u3092\u9023\u7d50 */\nconst nums1: number[] = [6, 8, 7, 10, 9];\nnums.push(...nums1);  // nums1\u3092nums\u306e\u672b\u5c3e\u306b\u9023\u7d50\n
        list.dart
        /* 2\u3064\u306e\u30ea\u30b9\u30c8\u3092\u9023\u7d50 */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.addAll(nums1);  // nums1\u3092nums\u306e\u672b\u5c3e\u306b\u9023\u7d50\n
        list.rs
        /* 2\u3064\u306e\u30ea\u30b9\u30c8\u3092\u9023\u7d50 */\nlet nums1: Vec<i32> = vec![6, 8, 7, 10, 9];\nnums.extend(nums1);\n
        list.c
        // C\u306f\u7d44\u307f\u8fbc\u307f\u306e\u52d5\u7684\u914d\u5217\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        list.kt
        \n
        "},{"location":"chapter_array_and_linkedlist/list/#6","title":"6. \u00a0 \u30ea\u30b9\u30c8\u306e\u30bd\u30fc\u30c8","text":"

        \u30ea\u30b9\u30c8\u304c\u30bd\u30fc\u30c8\u3055\u308c\u308b\u3068\u3001\u300c\u4e8c\u5206\u63a2\u7d22\u300d\u3084\u300c\u53cc\u30dd\u30a4\u30f3\u30bf\u300d\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306a\u3069\u3001\u914d\u5217\u95a2\u9023\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u3067\u3088\u304f\u4f7f\u7528\u3055\u308c\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin list.py
        # \u30ea\u30b9\u30c8\u3092\u30bd\u30fc\u30c8\nnums.sort()  # \u30bd\u30fc\u30c8\u5f8c\u3001\u30ea\u30b9\u30c8\u8981\u7d20\u306f\u6607\u9806\u306b\u306a\u308a\u307e\u3059\n
        list.cpp
        /* \u30ea\u30b9\u30c8\u3092\u30bd\u30fc\u30c8 */\nsort(nums.begin(), nums.end());  // \u30bd\u30fc\u30c8\u5f8c\u3001\u30ea\u30b9\u30c8\u8981\u7d20\u306f\u6607\u9806\u306b\u306a\u308a\u307e\u3059\n
        list.java
        /* \u30ea\u30b9\u30c8\u3092\u30bd\u30fc\u30c8 */\nCollections.sort(nums);  // \u30bd\u30fc\u30c8\u5f8c\u3001\u30ea\u30b9\u30c8\u8981\u7d20\u306f\u6607\u9806\u306b\u306a\u308a\u307e\u3059\n
        list.cs
        /* \u30ea\u30b9\u30c8\u3092\u30bd\u30fc\u30c8 */\nnums.Sort(); // \u30bd\u30fc\u30c8\u5f8c\u3001\u30ea\u30b9\u30c8\u8981\u7d20\u306f\u6607\u9806\u306b\u306a\u308a\u307e\u3059\n
        list_test.go
        /* \u30ea\u30b9\u30c8\u3092\u30bd\u30fc\u30c8 */\nsort.Ints(nums)  // \u30bd\u30fc\u30c8\u5f8c\u3001\u30ea\u30b9\u30c8\u8981\u7d20\u306f\u6607\u9806\u306b\u306a\u308a\u307e\u3059\n
        list.swift
        /* \u30ea\u30b9\u30c8\u3092\u30bd\u30fc\u30c8 */\nnums.sort() // \u30bd\u30fc\u30c8\u5f8c\u3001\u30ea\u30b9\u30c8\u8981\u7d20\u306f\u6607\u9806\u306b\u306a\u308a\u307e\u3059\n
        list.js
        /* \u30ea\u30b9\u30c8\u3092\u30bd\u30fc\u30c8 */\nnums.sort((a, b) => a - b);  // \u30bd\u30fc\u30c8\u5f8c\u3001\u30ea\u30b9\u30c8\u8981\u7d20\u306f\u6607\u9806\u306b\u306a\u308a\u307e\u3059\n
        list.ts
        /* \u30ea\u30b9\u30c8\u3092\u30bd\u30fc\u30c8 */\nnums.sort((a, b) => a - b);  // \u30bd\u30fc\u30c8\u5f8c\u3001\u30ea\u30b9\u30c8\u8981\u7d20\u306f\u6607\u9806\u306b\u306a\u308a\u307e\u3059\n
        list.dart
        /* \u30ea\u30b9\u30c8\u3092\u30bd\u30fc\u30c8 */\nnums.sort(); // \u30bd\u30fc\u30c8\u5f8c\u3001\u30ea\u30b9\u30c8\u8981\u7d20\u306f\u6607\u9806\u306b\u306a\u308a\u307e\u3059\n
        list.rs
        /* \u30ea\u30b9\u30c8\u3092\u30bd\u30fc\u30c8 */\nnums.sort(); // \u30bd\u30fc\u30c8\u5f8c\u3001\u30ea\u30b9\u30c8\u8981\u7d20\u306f\u6607\u9806\u306b\u306a\u308a\u307e\u3059\n
        list.c
        // C\u306f\u7d44\u307f\u8fbc\u307f\u306e\u52d5\u7684\u914d\u5217\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        list.kt
        \n
        "},{"location":"chapter_array_and_linkedlist/list/#432","title":"4.3.2 \u00a0 \u30ea\u30b9\u30c8\u306e\u5b9f\u88c5","text":"

        \u591a\u304f\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u306f\u3001Java\u3001C++\u3001Python\u306a\u3069\u3092\u542b\u3080\u7d44\u307f\u8fbc\u307f\u30ea\u30b9\u30c8\u304c\u4ed8\u5c5e\u3057\u3066\u3044\u307e\u3059\u3002\u305d\u308c\u3089\u306e\u5b9f\u88c5\u306f\u3001\u521d\u671f\u5bb9\u91cf\u3084\u62e1\u5f35\u4fc2\u6570\u306a\u3069\u306e\u69d8\u3005\u306a\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u614e\u91cd\u306b\u8003\u616e\u3057\u305f\u8a2d\u5b9a\u3067\u3001\u8907\u96d1\u306b\u306a\u308a\u304c\u3061\u3067\u3059\u3002\u8208\u5473\u306e\u3042\u308b\u8aad\u8005\u306f\u3001\u3055\u3089\u306a\u308b\u5b66\u7fd2\u306e\u305f\u3081\u306b\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u8abf\u3079\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u30ea\u30b9\u30c8\u304c\u3069\u306e\u3088\u3046\u306b\u52d5\u4f5c\u3059\u308b\u304b\u306e\u7406\u89e3\u3092\u6df1\u3081\u308b\u305f\u3081\u306b\u30013\u3064\u306e\u91cd\u8981\u306a\u8a2d\u8a08\u5074\u9762\u306b\u7126\u70b9\u3092\u5f53\u3066\u3066\u3001\u7c21\u7565\u5316\u3055\u308c\u305f\u30ea\u30b9\u30c8\u306e\u5b9f\u88c5\u3092\u8a66\u307f\u307e\u3059\uff1a

        • \u521d\u671f\u5bb9\u91cf\uff1a\u914d\u5217\u306b\u5408\u7406\u7684\u306a\u521d\u671f\u5bb9\u91cf\u3092\u9078\u629e\u3057\u307e\u3059\u3002\u3053\u306e\u4f8b\u3067\u306f\u3001\u521d\u671f\u5bb9\u91cf\u3068\u3057\u306610\u3092\u9078\u629e\u3057\u307e\u3059\u3002
        • \u30b5\u30a4\u30ba\u8a18\u9332\uff1a\u30ea\u30b9\u30c8\u5185\u306e\u73fe\u5728\u306e\u8981\u7d20\u6570\u3092\u8a18\u9332\u3059\u308b\u5909\u6570size\u3092\u5ba3\u8a00\u3057\u3001\u8981\u7d20\u306e\u633f\u5165\u3068\u524a\u9664\u3067\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u306b\u66f4\u65b0\u3057\u307e\u3059\u3002\u3053\u306e\u5909\u6570\u306b\u3088\u308a\u3001\u30ea\u30b9\u30c8\u306e\u672b\u5c3e\u3092\u7279\u5b9a\u3057\u3001\u62e1\u5f35\u304c\u5fc5\u8981\u304b\u3069\u3046\u304b\u3092\u5224\u65ad\u3067\u304d\u307e\u3059\u3002
        • \u62e1\u5f35\u30e1\u30ab\u30cb\u30ba\u30e0\uff1a\u8981\u7d20\u633f\u5165\u6642\u306b\u30ea\u30b9\u30c8\u304c\u6e80\u676f\u306b\u9054\u3057\u305f\u5834\u5408\u3001\u62e1\u5f35\u30d7\u30ed\u30bb\u30b9\u304c\u5fc5\u8981\u3067\u3059\u3002\u3053\u308c\u306b\u306f\u62e1\u5f35\u4fc2\u6570\u306b\u57fa\u3065\u3044\u3066\u3088\u308a\u5927\u304d\u306a\u914d\u5217\u3092\u4f5c\u6210\u3057\u3001\u73fe\u5728\u306e\u914d\u5217\u304b\u3089\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u65b0\u3057\u3044\u914d\u5217\u306b\u8ee2\u9001\u3059\u308b\u3053\u3068\u304c\u542b\u307e\u308c\u307e\u3059\u3002\u3053\u306e\u4f8b\u3067\u306f\u3001\u62e1\u5f35\u306e\u305f\u3073\u306b\u914d\u5217\u30b5\u30a4\u30ba\u30922\u500d\u306b\u3059\u308b\u3053\u3068\u3092\u898f\u5b9a\u3057\u307e\u3059\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_list.py
        class MyList:\n    \"\"\"\u30ea\u30b9\u30c8\u30af\u30e9\u30b9\"\"\"\n\n    def __init__(self):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        self._capacity: int = 10  # \u30ea\u30b9\u30c8\u306e\u5bb9\u91cf\n        self._arr: list[int] = [0] * self._capacity  # \u914d\u5217\uff08\u30ea\u30b9\u30c8\u8981\u7d20\u3092\u683c\u7d0d\uff09\n        self._size: int = 0  # \u30ea\u30b9\u30c8\u306e\u9577\u3055\uff08\u73fe\u5728\u306e\u8981\u7d20\u6570\uff09\n        self._extend_ratio: int = 2  # \u5404\u30ea\u30b9\u30c8\u62e1\u5f35\u306e\u500d\u6570\n\n    def size(self) -> int:\n        \"\"\"\u30ea\u30b9\u30c8\u306e\u9577\u3055\uff08\u73fe\u5728\u306e\u8981\u7d20\u6570\uff09\u3092\u53d6\u5f97\"\"\"\n        return self._size\n\n    def capacity(self) -> int:\n        \"\"\"\u30ea\u30b9\u30c8\u306e\u5bb9\u91cf\u3092\u53d6\u5f97\"\"\"\n        return self._capacity\n\n    def get(self, index: int) -> int:\n        \"\"\"\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\"\"\"\n        # \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u4f8b\u5916\u3092\u30b9\u30ed\u30fc\n        if index < 0 or index >= self._size:\n            raise IndexError(\"Index out of bounds\")\n        return self._arr[index]\n\n    def set(self, num: int, index: int):\n        \"\"\"\u8981\u7d20\u3092\u66f4\u65b0\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"Index out of bounds\")\n        self._arr[index] = num\n\n    def add(self, num: int):\n        \"\"\"\u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0\"\"\"\n        # \u8981\u7d20\u6570\u304c\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u62e1\u5f35\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u30c8\u30ea\u30ac\u30fc\n        if self.size() == self.capacity():\n            self.extend_capacity()\n        self._arr[self._size] = num\n        self._size += 1\n\n    def insert(self, num: int, index: int):\n        \"\"\"\u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"Index out of bounds\")\n        # \u8981\u7d20\u6570\u304c\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u62e1\u5f35\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u30c8\u30ea\u30ac\u30fc\n        if self._size == self.capacity():\n            self.extend_capacity()\n        # \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 index \u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u5f8c\u308d\u306b\u79fb\u52d5\n        for j in range(self._size - 1, index - 1, -1):\n            self._arr[j + 1] = self._arr[j]\n        self._arr[index] = num\n        # \u8981\u7d20\u6570\u3092\u66f4\u65b0\n        self._size += 1\n\n    def remove(self, index: int) -> int:\n        \"\"\"\u8981\u7d20\u3092\u524a\u9664\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"Index out of bounds\")\n        num = self._arr[index]\n        # \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 index \u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u524d\u306b\u79fb\u52d5\n        for j in range(index, self._size - 1):\n            self._arr[j] = self._arr[j + 1]\n        # \u8981\u7d20\u6570\u3092\u66f4\u65b0\n        self._size -= 1\n        # \u524a\u9664\u3055\u308c\u305f\u8981\u7d20\u3092\u8fd4\u3059\n        return num\n\n    def extend_capacity(self):\n        \"\"\"\u30ea\u30b9\u30c8\u3092\u62e1\u5f35\"\"\"\n        # \u5143\u306e\u914d\u5217\u306e _extend_ratio \u500d\u306e\u9577\u3055\u306e\u65b0\u3057\u3044\u914d\u5217\u3092\u4f5c\u6210\u3057\u3001\u5143\u306e\u914d\u5217\u3092\u65b0\u3057\u3044\u914d\u5217\u306b\u30b3\u30d4\u30fc\n        self._arr = self._arr + [0] * self.capacity() * (self._extend_ratio - 1)\n        # \u30ea\u30b9\u30c8\u306e\u5bb9\u91cf\u3092\u66f4\u65b0\n        self._capacity = len(self._arr)\n\n    def to_array(self) -> list[int]:\n        \"\"\"\u6709\u52b9\u306a\u9577\u3055\u306e\u30ea\u30b9\u30c8\u3092\u8fd4\u3059\"\"\"\n        return self._arr[: self._size]\n
        my_list.cpp
        /* \u30ea\u30b9\u30c8\u30af\u30e9\u30b9 */\nclass MyList {\n  private:\n    int *arr;             // \u914d\u5217\uff08\u30ea\u30b9\u30c8\u8981\u7d20\u3092\u683c\u7d0d\uff09\n    int arrCapacity = 10; // \u30ea\u30b9\u30c8\u306e\u5bb9\u91cf\n    int arrSize = 0;      // \u30ea\u30b9\u30c8\u306e\u9577\u3055\uff08\u73fe\u5728\u306e\u8981\u7d20\u6570\uff09\n    int extendRatio = 2;   // \u30ea\u30b9\u30c8\u62e1\u5f35\u6642\u306e\u500d\u7387\n\n  public:\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    MyList() {\n        arr = new int[arrCapacity];\n    }\n\n    /* \u30c7\u30b9\u30c8\u30e9\u30af\u30bf */\n    ~MyList() {\n        delete[] arr;\n    }\n\n    /* \u30ea\u30b9\u30c8\u306e\u9577\u3055\u3092\u53d6\u5f97\uff08\u73fe\u5728\u306e\u8981\u7d20\u6570\uff09*/\n    int size() {\n        return arrSize;\n    }\n\n    /* \u30ea\u30b9\u30c8\u306e\u5bb9\u91cf\u3092\u53d6\u5f97 */\n    int capacity() {\n        return arrCapacity;\n    }\n\n    /* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    int get(int index) {\n        // \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001\u4f8b\u5916\u3092\u30b9\u30ed\u30fc\uff08\u4ee5\u4e0b\u540c\u69d8\uff09\n        if (index < 0 || index >= size())\n            throw out_of_range(\"Index out of bounds\");\n        return arr[index];\n    }\n\n    /* \u8981\u7d20\u3092\u66f4\u65b0 */\n    void set(int index, int num) {\n        if (index < 0 || index >= size())\n            throw out_of_range(\"Index out of bounds\");\n        arr[index] = num;\n    }\n\n    /* \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 */\n    void add(int num) {\n        // \u8981\u7d20\u6570\u304c\u5bb9\u91cf\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u62e1\u5f35\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u30c8\u30ea\u30ac\u30fc\n        if (size() == capacity())\n            extendCapacity();\n        arr[size()] = num;\n        // \u8981\u7d20\u6570\u3092\u66f4\u65b0\n        arrSize++;\n    }\n\n    /* \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165 */\n    void insert(int index, int num) {\n        if (index < 0 || index >= size())\n            throw out_of_range(\"Index out of bounds\");\n        // \u8981\u7d20\u6570\u304c\u5bb9\u91cf\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u62e1\u5f35\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u30c8\u30ea\u30ac\u30fc\n        if (size() == capacity())\n            extendCapacity();\n        // `index`\u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u5f8c\u308d\u306b\u79fb\u52d5\n        for (int j = size() - 1; j >= index; j--) {\n            arr[j + 1] = arr[j];\n        }\n        arr[index] = num;\n        // \u8981\u7d20\u6570\u3092\u66f4\u65b0\n        arrSize++;\n    }\n\n    /* \u8981\u7d20\u3092\u524a\u9664 */\n    int remove(int index) {\n        if (index < 0 || index >= size())\n            throw out_of_range(\"Index out of bounds\");\n        int num = arr[index];\n        // `index`\u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u524d\u306b\u79fb\u52d5\n        for (int j = index; j < size() - 1; j++) {\n            arr[j] = arr[j + 1];\n        }\n        // \u8981\u7d20\u6570\u3092\u66f4\u65b0\n        arrSize--;\n        // \u524a\u9664\u3055\u308c\u305f\u8981\u7d20\u3092\u8fd4\u5374\n        return num;\n    }\n\n    /* \u30ea\u30b9\u30c8\u3092\u62e1\u5f35 */\n    void extendCapacity() {\n        // \u5143\u306e\u914d\u5217\u306eextendRatio\u500d\u306e\u9577\u3055\u3067\u65b0\u3057\u3044\u914d\u5217\u3092\u4f5c\u6210\n        int newCapacity = capacity() * extendRatio;\n        int *tmp = arr;\n        arr = new int[newCapacity];\n        // \u5143\u306e\u914d\u5217\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u65b0\u3057\u3044\u914d\u5217\u306b\u30b3\u30d4\u30fc\n        for (int i = 0; i < size(); i++) {\n            arr[i] = tmp[i];\n        }\n        // \u30e1\u30e2\u30ea\u3092\u89e3\u653e\n        delete[] tmp;\n        arrCapacity = newCapacity;\n    }\n\n    /* \u30ea\u30b9\u30c8\u3092Vector\u306b\u5909\u63db\u3057\u3066\u5370\u5237\u7528\u306b\u4f7f\u7528 */\n    vector<int> toVector() {\n        // \u6709\u52b9\u306a\u9577\u3055\u7bc4\u56f2\u5185\u306e\u8981\u7d20\u306e\u307f\u3092\u5909\u63db\n        vector<int> vec(size());\n        for (int i = 0; i < size(); i++) {\n            vec[i] = arr[i];\n        }\n        return vec;\n    }\n};\n
        my_list.java
        /* \u30ea\u30b9\u30c8\u30af\u30e9\u30b9 */\nclass MyList {\n    private int[] arr; // \u914d\u5217\uff08\u30ea\u30b9\u30c8\u8981\u7d20\u3092\u683c\u7d0d\uff09\n    private int capacity = 10; // \u30ea\u30b9\u30c8\u5bb9\u91cf\n    private int size = 0; // \u30ea\u30b9\u30c8\u9577\uff08\u73fe\u5728\u306e\u8981\u7d20\u6570\uff09\n    private int extendRatio = 2; // \u30ea\u30b9\u30c8\u306e\u5404\u62e1\u5f35\u500d\u7387\n\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    public MyList() {\n        arr = new int[capacity];\n    }\n\n    /* \u30ea\u30b9\u30c8\u9577\u3092\u53d6\u5f97\uff08\u73fe\u5728\u306e\u8981\u7d20\u6570\uff09 */\n    public int size() {\n        return size;\n    }\n\n    /* \u30ea\u30b9\u30c8\u5bb9\u91cf\u3092\u53d6\u5f97 */\n    public int capacity() {\n        return capacity;\n    }\n\n    /* \u8981\u7d20\u3078\u306e\u30a2\u30af\u30bb\u30b9 */\n    public int get(int index) {\n        // \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u4f8b\u5916\u3092\u30b9\u30ed\u30fc\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u3067\u3059\");\n        return arr[index];\n    }\n\n    /* \u8981\u7d20\u306e\u66f4\u65b0 */\n    public void set(int index, int num) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u3067\u3059\");\n        arr[index] = num;\n    }\n\n    /* \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 */\n    public void add(int num) {\n        // \u8981\u7d20\u6570\u304c\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u62e1\u5f35\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u5b9f\u884c\n        if (size == capacity())\n            extendCapacity();\n        arr[size] = num;\n        // \u8981\u7d20\u6570\u3092\u66f4\u65b0\n        size++;\n    }\n\n    /* \u4e2d\u9593\u306b\u8981\u7d20\u3092\u633f\u5165 */\n    public void insert(int index, int num) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u3067\u3059\");\n        // \u8981\u7d20\u6570\u304c\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u62e1\u5f35\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u5b9f\u884c\n        if (size == capacity())\n            extendCapacity();\n        // `index` \u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u5f8c\u308d\u306b\u79fb\u52d5\n        for (int j = size - 1; j >= index; j--) {\n            arr[j + 1] = arr[j];\n        }\n        arr[index] = num;\n        // \u8981\u7d20\u6570\u3092\u66f4\u65b0\n        size++;\n    }\n\n    /* \u8981\u7d20\u306e\u524a\u9664 */\n    public int remove(int index) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u3067\u3059\");\n        int num = arr[index];\n        // `index` \u3088\u308a\u5f8c\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u30921\u3064\u524d\u306b\u79fb\u52d5\n        for (int j = index; j < size - 1; j++) {\n            arr[j] = arr[j + 1];\n        }\n        // \u8981\u7d20\u6570\u3092\u66f4\u65b0\n        size--;\n        // \u524a\u9664\u3055\u308c\u305f\u8981\u7d20\u3092\u8fd4\u3059\n        return num;\n    }\n\n    /* \u30ea\u30b9\u30c8\u3092\u62e1\u5f35 */\n    public void extendCapacity() {\n        // \u5143\u306e\u914d\u5217\u306e\u9577\u3055\u3092 extendRatio \u500d\u3057\u305f\u65b0\u3057\u3044\u914d\u5217\u3092\u4f5c\u6210\u3057\u3001\u5143\u306e\u914d\u5217\u3092\u65b0\u3057\u3044\u914d\u5217\u306b\u30b3\u30d4\u30fc\n        arr = Arrays.copyOf(arr, capacity() * extendRatio);\n        // \u30ea\u30b9\u30c8\u5bb9\u91cf\u3092\u66f4\u65b0\n        capacity = arr.length;\n    }\n\n    /* \u30ea\u30b9\u30c8\u3092\u914d\u5217\u306b\u5909\u63db */\n    public int[] toArray() {\n        int size = size();\n        // \u6709\u52b9\u306a\u9577\u3055\u7bc4\u56f2\u5185\u306e\u8981\u7d20\u306e\u307f\u3092\u5909\u63db\n        int[] arr = new int[size];\n        for (int i = 0; i < size; i++) {\n            arr[i] = get(i);\n        }\n        return arr;\n    }\n}\n
        my_list.cs
        [class]{MyList}-[func]{}\n
        my_list.go
        [class]{myList}-[func]{}\n
        my_list.swift
        [class]{MyList}-[func]{}\n
        my_list.js
        [class]{MyList}-[func]{}\n
        my_list.ts
        [class]{MyList}-[func]{}\n
        my_list.dart
        [class]{MyList}-[func]{}\n
        my_list.rs
        [class]{MyList}-[func]{}\n
        my_list.c
        [class]{MyList}-[func]{}\n
        my_list.kt
        [class]{MyList}-[func]{}\n
        my_list.rb
        [class]{MyList}-[func]{}\n
        "},{"location":"chapter_array_and_linkedlist/ram_and_cache/","title":"4.4 \u00a0 \u30e1\u30e2\u30ea\u3068\u30ad\u30e3\u30c3\u30b7\u30e5 *","text":"

        \u3053\u306e\u7ae0\u306e\u6700\u521d\u306e2\u3064\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u300c\u9023\u7d9a\u683c\u7d0d\u300d\u3068\u300c\u5206\u6563\u683c\u7d0d\u300d\u3092\u305d\u308c\u305e\u308c\u8868\u73fe\u3059\u308b2\u3064\u306e\u57fa\u672c\u7684\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3042\u308b\u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u63a2\u7a76\u3057\u307e\u3057\u305f\u3002

        \u5b9f\u969b\u3001\u7269\u7406\u69cb\u9020\u306f\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u30e1\u30e2\u30ea\u3068\u30ad\u30e3\u30c3\u30b7\u30e5\u3092\u3069\u306e\u7a0b\u5ea6\u52b9\u7387\u7684\u306b\u5229\u7528\u3059\u308b\u304b\u3092\u5927\u304d\u304f\u6c7a\u5b9a\u3057\u3001\u3053\u308c\u304c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5168\u4f53\u7684\u306a\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306b\u5f71\u97ff\u3092\u4e0e\u3048\u307e\u3059\u3002

        "},{"location":"chapter_array_and_linkedlist/ram_and_cache/#441","title":"4.4.1 \u00a0 \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u8a18\u61b6\u88c5\u7f6e","text":"

        \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306b\u306f3\u7a2e\u985e\u306e\u8a18\u61b6\u88c5\u7f6e\u304c\u3042\u308a\u307e\u3059\uff1a\u30cf\u30fc\u30c9\u30c7\u30a3\u30b9\u30af\u3001\u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\u30e1\u30e2\u30ea\uff08RAM\uff09\u3001\u304a\u3088\u3073\u30ad\u30e3\u30c3\u30b7\u30e5\u30e1\u30e2\u30ea\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u8868\u306f\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30b7\u30b9\u30c6\u30e0\u306b\u304a\u3051\u308b\u305d\u308c\u305e\u308c\u306e\u5f79\u5272\u3068\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u7279\u6027\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u8868 4-2 \u00a0 \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u8a18\u61b6\u88c5\u7f6e

        \u30cf\u30fc\u30c9\u30c7\u30a3\u30b9\u30af \u30e1\u30e2\u30ea \u30ad\u30e3\u30c3\u30b7\u30e5 \u7528\u9014 OS\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u3001\u30d5\u30a1\u30a4\u30eb\u306a\u3069\u306e\u30c7\u30fc\u30bf\u306e\u9577\u671f\u4fdd\u5b58 \u73fe\u5728\u5b9f\u884c\u4e2d\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3068\u51e6\u7406\u4e2d\u306e\u30c7\u30fc\u30bf\u306e\u4e00\u6642\u4fdd\u5b58 \u983b\u7e41\u306b\u30a2\u30af\u30bb\u30b9\u3055\u308c\u308b\u30c7\u30fc\u30bf\u3068\u547d\u4ee4\u3092\u4fdd\u5b58\u3057\u3001CPU\u306e\u30e1\u30e2\u30ea\u3078\u306e\u30a2\u30af\u30bb\u30b9\u6570\u3092\u524a\u6e1b \u63ee\u767a\u6027 \u96fb\u6e90\u30aa\u30d5\u5f8c\u3082\u30c7\u30fc\u30bf\u306f\u5931\u308f\u308c\u306a\u3044 \u96fb\u6e90\u30aa\u30d5\u5f8c\u306b\u30c7\u30fc\u30bf\u306f\u5931\u308f\u308c\u308b \u96fb\u6e90\u30aa\u30d5\u5f8c\u306b\u30c7\u30fc\u30bf\u306f\u5931\u308f\u308c\u308b \u5bb9\u91cf \u3088\u308a\u5927\u304d\u3044\u3001TB\u30ec\u30d9\u30eb \u3088\u308a\u5c0f\u3055\u3044\u3001GB\u30ec\u30d9\u30eb \u975e\u5e38\u306b\u5c0f\u3055\u3044\u3001MB\u30ec\u30d9\u30eb \u901f\u5ea6 \u3088\u308a\u9045\u3044\u3001\u6570\u767e\u304b\u3089\u6570\u5343MB/s \u3088\u308a\u9ad8\u901f\u3001\u6570\u5341GB/s \u975e\u5e38\u306b\u9ad8\u901f\u3001\u6570\u5341\u304b\u3089\u6570\u767eGB/s \u4fa1\u683c\uff08USD\uff09 \u3088\u308a\u5b89\u4fa1\u3001\u6570\u30bb\u30f3\u30c8/GB \u3088\u308a\u9ad8\u4fa1\u3001\u6570\u30c9\u30eb/GB \u975e\u5e38\u306b\u9ad8\u4fa1\u3001CPU\u3068\u4e00\u7dd2\u306b\u4fa1\u683c\u8a2d\u5b9a

        \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u8a18\u61b6\u30b7\u30b9\u30c6\u30e0\u306f\u3001\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u30d4\u30e9\u30df\u30c3\u30c9\u3068\u3057\u3066\u8996\u899a\u5316\u3067\u304d\u307e\u3059\u3002\u30d4\u30e9\u30df\u30c3\u30c9\u306e\u4e0a\u90e8\u306b\u3042\u308b\u8a18\u61b6\u88c5\u7f6e\u307b\u3069\u9ad8\u901f\u3067\u3001\u5bb9\u91cf\u304c\u5c0f\u3055\u304f\u3001\u3088\u308a\u9ad8\u4fa1\u3067\u3059\u3002\u3053\u306e\u30de\u30eb\u30c1\u30ec\u30d9\u30eb\u8a2d\u8a08\u306f\u5076\u7136\u3067\u306f\u306a\u304f\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u79d1\u5b66\u8005\u3068\u30a8\u30f3\u30b8\u30cb\u30a2\u306b\u3088\u308b\u614e\u91cd\u306a\u691c\u8a0e\u306e\u7d50\u679c\u3067\u3059\u3002

        • \u30cf\u30fc\u30c9\u30c7\u30a3\u30b9\u30af\u3092\u30e1\u30e2\u30ea\u306b\u7f6e\u304d\u63db\u3048\u308b\u306e\u306f\u56f0\u96e3\u3067\u3059\u3002\u7b2c\u4e00\u306b\u3001\u30e1\u30e2\u30ea\u5185\u306e\u30c7\u30fc\u30bf\u306f\u96fb\u6e90\u30aa\u30d5\u5f8c\u306b\u5931\u308f\u308c\u308b\u305f\u3081\u3001\u9577\u671f\u30c7\u30fc\u30bf\u4fdd\u5b58\u306b\u306f\u9069\u3057\u3066\u3044\u307e\u305b\u3093\u3002\u7b2c\u4e8c\u306b\u3001\u30e1\u30e2\u30ea\u306f\u30cf\u30fc\u30c9\u30c7\u30a3\u30b9\u30af\u3088\u308a\u3082\u5927\u5e45\u306b\u9ad8\u4fa1\u3067\u3001\u6d88\u8cbb\u8005\u5e02\u5834\u3067\u306e\u5e83\u7bc4\u56f2\u306a\u4f7f\u7528\u306e\u5b9f\u73fe\u53ef\u80fd\u6027\u3092\u5236\u9650\u3057\u3066\u3044\u307e\u3059\u3002
        • \u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u5927\u5bb9\u91cf\u3068\u9ad8\u901f\u306e\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u306b\u76f4\u9762\u3057\u3066\u3044\u307e\u3059\u3002L1\u3001L2\u3001L3\u30ad\u30e3\u30c3\u30b7\u30e5\u306e\u5bb9\u91cf\u304c\u5897\u52a0\u3059\u308b\u306b\u3064\u308c\u3066\u3001\u305d\u306e\u7269\u7406\u30b5\u30a4\u30ba\u304c\u5927\u304d\u304f\u306a\u308a\u3001CPU\u30b3\u30a2\u304b\u3089\u306e\u8ddd\u96e2\u304c\u5897\u52a0\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u30c7\u30fc\u30bf\u8ee2\u9001\u6642\u9593\u304c\u9577\u304f\u306a\u308a\u3001\u30a2\u30af\u30bb\u30b9\u9045\u5ef6\u304c\u9ad8\u304f\u306a\u308a\u307e\u3059\u3002\u73fe\u5728\u306e\u6280\u8853\u3067\u306f\u3001\u30de\u30eb\u30c1\u30ec\u30d9\u30eb\u30ad\u30e3\u30c3\u30b7\u30e5\u69cb\u9020\u304c\u5bb9\u91cf\u3001\u901f\u5ea6\u3001\u30b3\u30b9\u30c8\u306e\u9593\u306e\u6700\u9069\u306a\u30d0\u30e9\u30f3\u30b9\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002

        \u56f3 4-9 \u00a0 \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u8a18\u61b6\u30b7\u30b9\u30c6\u30e0

        Tip

        \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u8a18\u61b6\u968e\u5c64\u306f\u3001\u901f\u5ea6\u3001\u5bb9\u91cf\u3001\u30b3\u30b9\u30c8\u306e\u9593\u306e\u614e\u91cd\u306a\u30d0\u30e9\u30f3\u30b9\u3092\u53cd\u6620\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u30bf\u30a4\u30d7\u306e\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u306f\u69d8\u3005\u306a\u696d\u754c\u3067\u4e00\u822c\u7684\u3067\u3042\u308a\u3001\u5229\u76ca\u3068\u5236\u9650\u306e\u9593\u306e\u6700\u9069\u306a\u30d0\u30e9\u30f3\u30b9\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002

        \u5168\u4f53\u7684\u306b\u3001\u30cf\u30fc\u30c9\u30c7\u30a3\u30b9\u30af\u306f\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u306e\u9577\u671f\u4fdd\u5b58\u3092\u63d0\u4f9b\u3057\u3001\u30e1\u30e2\u30ea\u306f\u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c\u4e2d\u306b\u51e6\u7406\u3055\u308c\u308b\u30c7\u30fc\u30bf\u306e\u4e00\u6642\u4fdd\u5b58\u3068\u3057\u3066\u6a5f\u80fd\u3057\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u983b\u7e41\u306b\u30a2\u30af\u30bb\u30b9\u3055\u308c\u308b\u30c7\u30fc\u30bf\u3068\u547d\u4ee4\u3092\u4fdd\u5b58\u3057\u3066\u5b9f\u884c\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002\u305d\u308c\u3089\u306f\u4e00\u7dd2\u306b\u306a\u3063\u3066\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30b7\u30b9\u30c6\u30e0\u306e\u52b9\u7387\u7684\u306a\u52d5\u4f5c\u3092\u4fdd\u8a3c\u3057\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c\u4e2d\u3001\u30c7\u30fc\u30bf\u306f\u30cf\u30fc\u30c9\u30c7\u30a3\u30b9\u30af\u304b\u3089\u30e1\u30e2\u30ea\u306b\u8aad\u307f\u8fbc\u307e\u308c\u3001CPU\u8a08\u7b97\u304c\u884c\u308f\u308c\u307e\u3059\u3002CPU\u306e\u62e1\u5f35\u3068\u3057\u3066\u6a5f\u80fd\u3059\u308b\u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u3001**\u30e1\u30e2\u30ea\u304b\u3089\u30a4\u30f3\u30c6\u30ea\u30b8\u30a7\u30f3\u30c8\u306b\u30c7\u30fc\u30bf\u3092\u5148\u8aad\u307f**\u3057\u3001CPU\u306e\u3088\u308a\u9ad8\u901f\u306a\u30c7\u30fc\u30bf\u30a2\u30af\u30bb\u30b9\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c\u52b9\u7387\u304c\u5927\u5e45\u306b\u5411\u4e0a\u3057\u3001\u4f4e\u901f\u306a\u30e1\u30e2\u30ea\u3078\u306e\u4f9d\u5b58\u304c\u6e1b\u5c11\u3057\u307e\u3059\u3002

        \u56f3 4-10 \u00a0 \u30cf\u30fc\u30c9\u30c7\u30a3\u30b9\u30af\u3001\u30e1\u30e2\u30ea\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u9593\u306e\u30c7\u30fc\u30bf\u30d5\u30ed\u30fc

        "},{"location":"chapter_array_and_linkedlist/ram_and_cache/#442","title":"4.4.2 \u00a0 \u30c7\u30fc\u30bf\u69cb\u9020\u306e\u30e1\u30e2\u30ea\u52b9\u7387","text":"

        \u30e1\u30e2\u30ea\u7a7a\u9593\u5229\u7528\u306e\u89b3\u70b9\u304b\u3089\u3001\u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u306f\u305d\u308c\u305e\u308c\u5229\u70b9\u3068\u5236\u9650\u304c\u3042\u308a\u307e\u3059\u3002

        \u4e00\u65b9\u3067\u3001**\u30e1\u30e2\u30ea\u306f\u9650\u3089\u308c\u3066\u304a\u308a\u3001\u8907\u6570\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u9593\u3067\u5171\u6709\u3067\u304d\u306a\u3044**\u305f\u3081\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u306e\u7a7a\u9593\u4f7f\u7528\u306e\u6700\u9069\u5316\u306f\u91cd\u8981\u3067\u3059\u3002\u914d\u5217\u306f\u8981\u7d20\u304c\u5bc6\u63a5\u306b\u30d1\u30c3\u30af\u3055\u308c\u3066\u304a\u308a\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u3088\u3046\u306b\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u306e\u305f\u3081\u306e\u8ffd\u52a0\u30e1\u30e2\u30ea\u3092\u5fc5\u8981\u3068\u3057\u306a\u3044\u305f\u3081\u3001\u7a7a\u9593\u52b9\u7387\u7684\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u914d\u5217\u306f\u9023\u7d9a\u3057\u305f\u30e1\u30e2\u30ea\u30d6\u30ed\u30c3\u30af\u3092\u4e8b\u524d\u306b\u5272\u308a\u5f53\u3066\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u5272\u308a\u5f53\u3066\u3089\u308c\u305f\u7a7a\u9593\u304c\u5b9f\u969b\u306e\u5fc5\u8981\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u7121\u99c4\u306b\u3064\u306a\u304c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u914d\u5217\u306e\u62e1\u5f35\u3082\u8ffd\u52a0\u306e\u6642\u9593\u3068\u7a7a\u9593\u306e\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u3092\u4f34\u3044\u307e\u3059\u3002\u5bfe\u7167\u7684\u306b\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u5404\u30ce\u30fc\u30c9\u306b\u5bfe\u3057\u3066\u30e1\u30e2\u30ea\u3092\u52d5\u7684\u306b\u5272\u308a\u5f53\u3066\u30fb\u89e3\u653e\u3057\u3001\u30dd\u30a4\u30f3\u30bf\u306e\u305f\u3081\u306e\u8ffd\u52a0\u30e1\u30e2\u30ea\u306e\u30b3\u30b9\u30c8\u3067\u3088\u308a\u5927\u304d\u306a\u67d4\u8edf\u6027\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002

        \u4e00\u65b9\u3067\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c\u4e2d\u3001\u7e70\u308a\u8fd4\u3055\u308c\u308b\u30e1\u30e2\u30ea\u306e\u5272\u308a\u5f53\u3066\u3068\u89e3\u653e\u306f\u30e1\u30e2\u30ea\u306e\u65ad\u7247\u5316\u3092\u5897\u52a0\u3055\u305b\u3001\u30e1\u30e2\u30ea\u5229\u7528\u52b9\u7387\u3092\u4f4e\u4e0b\u3055\u305b\u307e\u3059\u3002\u914d\u5217\u306f\u9023\u7d9a\u8a18\u61b6\u65b9\u5f0f\u306b\u3088\u308a\u3001\u30e1\u30e2\u30ea\u65ad\u7247\u5316\u3092\u5f15\u304d\u8d77\u3053\u3059\u53ef\u80fd\u6027\u304c\u6bd4\u8f03\u7684\u4f4e\u3044\u3067\u3059\u3002\u5bfe\u7167\u7684\u306b\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u8981\u7d20\u3092\u975e\u9023\u7d9a\u306e\u5834\u6240\u306b\u4fdd\u5b58\u3057\u3001\u983b\u7e41\u306a\u633f\u5165\u3068\u524a\u9664\u306f\u30e1\u30e2\u30ea\u65ad\u7247\u5316\u3092\u60aa\u5316\u3055\u305b\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_array_and_linkedlist/ram_and_cache/#443","title":"4.4.3 \u00a0 \u30c7\u30fc\u30bf\u69cb\u9020\u306e\u30ad\u30e3\u30c3\u30b7\u30e5\u52b9\u7387","text":"

        \u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u30e1\u30e2\u30ea\u3088\u308a\u3082\u7a7a\u9593\u5bb9\u91cf\u304c\u306f\u308b\u304b\u306b\u5c0f\u3055\u3044\u3067\u3059\u304c\u3001\u306f\u308b\u304b\u306b\u9ad8\u901f\u3067\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c\u901f\u5ea6\u306b\u304a\u3044\u3066\u91cd\u8981\u306a\u5f79\u5272\u3092\u679c\u305f\u3057\u307e\u3059\u3002\u9650\u3089\u308c\u305f\u5bb9\u91cf\u306e\u305f\u3081\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u983b\u7e41\u306b\u30a2\u30af\u30bb\u30b9\u3055\u308c\u308b\u30c7\u30fc\u30bf\u306e\u30b5\u30d6\u30bb\u30c3\u30c8\u306e\u307f\u3092\u4fdd\u5b58\u3067\u304d\u307e\u3059\u3002CPU\u304c\u30ad\u30e3\u30c3\u30b7\u30e5\u306b\u5b58\u5728\u3057\u306a\u3044\u30c7\u30fc\u30bf\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u30df\u30b9\u304c\u767a\u751f\u3057\u3001CPU\u306f\u4f4e\u901f\u306a\u30e1\u30e2\u30ea\u304b\u3089\u5fc5\u8981\u306a\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306b\u5f71\u97ff\u3092\u4e0e\u3048\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        \u660e\u3089\u304b\u306b\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u30df\u30b9\u304c\u5c11\u306a\u3044\u307b\u3069\u3001CPU\u306e\u30c7\u30fc\u30bf\u8aad\u307f\u66f8\u304d\u52b9\u7387\u304c\u9ad8\u304f\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u5411\u4e0a\u3057\u307e\u3059\u3002CPU\u304c\u30ad\u30e3\u30c3\u30b7\u30e5\u304b\u3089\u30c7\u30fc\u30bf\u3092\u6b63\u5e38\u306b\u53d6\u5f97\u3059\u308b\u5272\u5408\u306f\u30ad\u30e3\u30c3\u30b7\u30e5\u30d2\u30c3\u30c8\u7387\u3068\u547c\u3070\u308c\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u52b9\u7387\u3092\u6e2c\u5b9a\u3059\u308b\u305f\u3081\u306b\u3088\u304f\u4f7f\u7528\u3055\u308c\u308b\u6307\u6a19\u3067\u3059\u3002

        \u3088\u308a\u9ad8\u3044\u52b9\u7387\u3092\u9054\u6210\u3059\u308b\u305f\u3081\u306b\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u4ee5\u4e0b\u306e\u30c7\u30fc\u30bf\u30ed\u30fc\u30c9\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u63a1\u7528\u3057\u307e\u3059\u3002

        • \u30ad\u30e3\u30c3\u30b7\u30e5\u30e9\u30a4\u30f3\uff1a\u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u500b\u3005\u306e\u30d0\u30a4\u30c8\u3067\u306f\u306a\u304f\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u30e9\u30a4\u30f3\u3068\u547c\u3070\u308c\u308b\u5358\u4f4d\u3067\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58\u30fb\u30ed\u30fc\u30c9\u3057\u3066\u52d5\u4f5c\u3057\u307e\u3059\u3002\u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\u3001\u4e00\u5ea6\u306b\u3088\u308a\u5927\u304d\u306a\u30c7\u30fc\u30bf\u30d6\u30ed\u30c3\u30af\u3092\u8ee2\u9001\u3059\u308b\u3053\u3068\u3067\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002
        • \u5148\u8aad\u307f\u30e1\u30ab\u30cb\u30ba\u30e0\uff1a\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u30c7\u30fc\u30bf\u30a2\u30af\u30bb\u30b9\u30d1\u30bf\u30fc\u30f3\uff08\u4f8b\uff1a\u9023\u7d9a\u307e\u305f\u306f\u56fa\u5b9a\u30b9\u30c8\u30e9\u30a4\u30c9\u30a2\u30af\u30bb\u30b9\uff09\u3092\u4e88\u6e2c\u3057\u3001\u3053\u308c\u3089\u306e\u30d1\u30bf\u30fc\u30f3\u306b\u57fa\u3065\u3044\u3066\u30c7\u30fc\u30bf\u3092\u30ad\u30e3\u30c3\u30b7\u30e5\u306b\u5148\u8aad\u307f\u3057\u3066\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u30d2\u30c3\u30c8\u7387\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002
        • \u7a7a\u9593\u7684\u5c40\u6240\u6027\uff1a\u7279\u5b9a\u306e\u30c7\u30fc\u30bf\u304c\u30a2\u30af\u30bb\u30b9\u3055\u308c\u308b\u3068\u3001\u8fd1\u304f\u306e\u30c7\u30fc\u30bf\u3082\u307e\u3082\u306a\u304f\u30a2\u30af\u30bb\u30b9\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u3092\u6d3b\u7528\u3059\u308b\u305f\u3081\u306b\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u8981\u6c42\u3055\u308c\u305f\u30c7\u30fc\u30bf\u3068\u4e00\u7dd2\u306b\u96a3\u63a5\u3059\u308b\u30c7\u30fc\u30bf\u3092\u30ed\u30fc\u30c9\u3057\u3001\u30d2\u30c3\u30c8\u7387\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002
        • \u6642\u9593\u7684\u5c40\u6240\u6027\uff1a\u30c7\u30fc\u30bf\u304c\u30a2\u30af\u30bb\u30b9\u3055\u308c\u305f\u5834\u5408\u3001\u8fd1\u3044\u5c06\u6765\u306b\u518d\u3073\u30a2\u30af\u30bb\u30b9\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u3053\u306e\u539f\u7406\u3092\u4f7f\u7528\u3057\u3066\u3001\u6700\u8fd1\u30a2\u30af\u30bb\u30b9\u3055\u308c\u305f\u30c7\u30fc\u30bf\u3092\u4fdd\u6301\u3057\u3066\u30d2\u30c3\u30c8\u7387\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002

        \u5b9f\u969b\u3001\u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u7570\u306a\u308b\u30ad\u30e3\u30c3\u30b7\u30e5\u5229\u7528\u52b9\u7387\u3092\u6301\u3061\u3001\u3053\u308c\u306f\u4e3b\u306b\u4ee5\u4e0b\u306e\u5074\u9762\u306b\u53cd\u6620\u3055\u308c\u307e\u3059\u3002

        • \u5360\u6709\u7a7a\u9593\uff1a\u9023\u7d50\u30ea\u30b9\u30c8\u8981\u7d20\u306f\u914d\u5217\u8981\u7d20\u3088\u308a\u3082\u591a\u304f\u306e\u7a7a\u9593\u3092\u5360\u6709\u3059\u308b\u305f\u3081\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u306b\u4fdd\u6301\u3055\u308c\u308b\u6709\u52b9\u30c7\u30fc\u30bf\u304c\u5c11\u306a\u304f\u306a\u308a\u307e\u3059\u3002
        • \u30ad\u30e3\u30c3\u30b7\u30e5\u30e9\u30a4\u30f3\uff1a\u9023\u7d50\u30ea\u30b9\u30c8\u30c7\u30fc\u30bf\u306f\u30e1\u30e2\u30ea\u5168\u4f53\u306b\u6563\u5728\u3057\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u300c\u884c\u5358\u4f4d\u3067\u30ed\u30fc\u30c9\u300d\u3055\u308c\u308b\u305f\u3081\u3001\u30ed\u30fc\u30c9\u3055\u308c\u308b\u7121\u52b9\u30c7\u30fc\u30bf\u306e\u5272\u5408\u304c\u9ad8\u304f\u306a\u308a\u307e\u3059\u3002
        • \u5148\u8aad\u307f\u30e1\u30ab\u30cb\u30ba\u30e0\uff1a\u914d\u5217\u306e\u30c7\u30fc\u30bf\u30a2\u30af\u30bb\u30b9\u30d1\u30bf\u30fc\u30f3\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u3088\u308a\u3082\u300c\u4e88\u6e2c\u53ef\u80fd\u300d\u3067\u3001\u3064\u307e\u308a\u30b7\u30b9\u30c6\u30e0\u304c\u3053\u308c\u304b\u3089\u30ed\u30fc\u30c9\u3055\u308c\u308b\u30c7\u30fc\u30bf\u3092\u63a8\u6e2c\u3057\u3084\u3059\u3044\u3067\u3059\u3002
        • \u7a7a\u9593\u7684\u5c40\u6240\u6027\uff1a\u914d\u5217\u306f\u9023\u7d9a\u3057\u305f\u30e1\u30e2\u30ea\u7a7a\u9593\u306b\u4fdd\u5b58\u3055\u308c\u308b\u305f\u3081\u3001\u30ed\u30fc\u30c9\u3055\u308c\u3066\u3044\u308b\u30c7\u30fc\u30bf\u306e\u8fd1\u304f\u306e\u30c7\u30fc\u30bf\u304c\u307e\u3082\u306a\u304f\u30a2\u30af\u30bb\u30b9\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u9ad8\u304f\u306a\u308a\u307e\u3059\u3002

        \u5168\u4f53\u7684\u306b\u3001\u914d\u5217\u306f\u3088\u308a\u9ad8\u3044\u30ad\u30e3\u30c3\u30b7\u30e5\u30d2\u30c3\u30c8\u7387\u3092\u6301\u3061\u3001\u4e00\u822c\u7684\u306b\u9023\u7d50\u30ea\u30b9\u30c8\u3088\u308a\u3082\u64cd\u4f5c\u52b9\u7387\u304c\u9ad8\u3044\u3067\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u914d\u5217\u306b\u57fa\u3065\u304f\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u306e\u89e3\u6c7a\u306b\u304a\u3044\u3066\u4eba\u6c17\u304c\u3042\u308a\u307e\u3059\u3002

        **\u9ad8\u3044\u30ad\u30e3\u30c3\u30b7\u30e5\u52b9\u7387\u304c\u914d\u5217\u304c\u5e38\u306b\u9023\u7d50\u30ea\u30b9\u30c8\u3088\u308a\u512a\u308c\u3066\u3044\u308b\u3068\u3044\u3046\u610f\u5473\u3067\u306f\u306a\u3044**\u3053\u3068\u306b\u6ce8\u610f\u3059\u3079\u304d\u3067\u3059\u3002\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u9078\u629e\u306f\u7279\u5b9a\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u8981\u4ef6\u306b\u4f9d\u5b58\u3059\u3079\u304d\u3067\u3059\u3002\u4f8b\u3048\u3070\u3001\u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u4e21\u65b9\u304c\u300c\u30b9\u30bf\u30c3\u30af\u300d\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\u304c\uff08\u6b21\u7ae0\u3067\u8a73\u7d30\u8aac\u660e\uff09\u3001\u305d\u308c\u3089\u306f\u7570\u306a\u308b\u30b7\u30ca\u30ea\u30aa\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002

        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u3067\u306f\u3001\u3088\u308a\u9ad8\u3044\u64cd\u4f5c\u52b9\u7387\u3068\u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\u6a5f\u80fd\u3092\u63d0\u4f9b\u3059\u308b\u305f\u3081\u3001\u914d\u5217\u306b\u57fa\u3065\u304f\u30b9\u30bf\u30c3\u30af\u3092\u9078\u629e\u3059\u308b\u50be\u5411\u304c\u3042\u308a\u307e\u3059\u3002\u552f\u4e00\u306e\u30b3\u30b9\u30c8\u306f\u914d\u5217\u306b\u5bfe\u3057\u3066\u4e00\u5b9a\u91cf\u306e\u30e1\u30e2\u30ea\u7a7a\u9593\u3092\u4e8b\u524d\u306b\u5272\u308a\u5f53\u3066\u308b\u5fc5\u8981\u304c\u3042\u308b\u3053\u3068\u3067\u3059\u3002
        • \u30c7\u30fc\u30bf\u91cf\u304c\u975e\u5e38\u306b\u5927\u304d\u304f\u3001\u9ad8\u5ea6\u306b\u52d5\u7684\u3067\u3001\u30b9\u30bf\u30c3\u30af\u306e\u4e88\u60f3\u30b5\u30a4\u30ba\u3092\u63a8\u5b9a\u3059\u308b\u306e\u304c\u56f0\u96e3\u306a\u5834\u5408\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u30b9\u30bf\u30c3\u30af\u304c\u3088\u308a\u826f\u3044\u9078\u629e\u3067\u3059\u3002\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u30e1\u30e2\u30ea\u306e\u7570\u306a\u308b\u90e8\u5206\u306b\u5206\u6563\u3067\u304d\u3001\u914d\u5217\u62e1\u5f35\u306e\u8ffd\u52a0\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u3092\u56de\u907f\u3067\u304d\u307e\u3059\u3002
        "},{"location":"chapter_array_and_linkedlist/summary/","title":"4.5 \u00a0 \u307e\u3068\u3081","text":""},{"location":"chapter_array_and_linkedlist/summary/#1","title":"1. \u00a0 \u91cd\u8981\u306a\u5fa9\u7fd2","text":"
        • \u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u306f2\u3064\u306e\u57fa\u672c\u7684\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3042\u308a\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30e1\u30e2\u30ea\u306b\u304a\u3051\u308b2\u3064\u306e\u683c\u7d0d\u65b9\u6cd5\u3092\u8868\u3057\u3066\u3044\u307e\u3059\uff1a\u9023\u7d9a\u7a7a\u9593\u683c\u7d0d\u3068\u975e\u9023\u7d9a\u7a7a\u9593\u683c\u7d0d\u3067\u3059\u3002\u305d\u308c\u3089\u306e\u7279\u6027\u306f\u4e92\u3044\u306b\u88dc\u5b8c\u3057\u5408\u3044\u307e\u3059\u3002
        • \u914d\u5217\u306f\u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3001\u4f7f\u7528\u3059\u308b\u30e1\u30e2\u30ea\u304c\u5c11\u306a\u3044\u4e00\u65b9\u3067\u3001\u8981\u7d20\u306e\u633f\u5165\u3068\u524a\u9664\u306f\u975e\u52b9\u7387\u7684\u3067\u3001\u521d\u671f\u5316\u5f8c\u306e\u9577\u3055\u304c\u56fa\u5b9a\u3055\u308c\u3066\u3044\u307e\u3059\u3002
        • \u9023\u7d50\u30ea\u30b9\u30c8\u306f\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u306e\u5909\u66f4\u306b\u3088\u3063\u3066\u52b9\u7387\u7684\u306a\u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664\u3092\u5b9f\u88c5\u3057\u3001\u9577\u3055\u3092\u67d4\u8edf\u306b\u8abf\u6574\u3067\u304d\u307e\u3059\u304c\u3001\u30ce\u30fc\u30c9\u30a2\u30af\u30bb\u30b9\u52b9\u7387\u304c\u4f4e\u304f\u3001\u3088\u308a\u591a\u304f\u306e\u30e1\u30e2\u30ea\u3092\u6d88\u8cbb\u3057\u307e\u3059\u3002
        • \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u4e00\u822c\u7684\u306a\u7a2e\u985e\u306b\u306f\u3001\u5358\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u5faa\u74b0\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u304c\u3042\u308a\u3001\u305d\u308c\u305e\u308c\u306b\u72ec\u81ea\u306e\u5fdc\u7528\u30b7\u30ca\u30ea\u30aa\u304c\u3042\u308a\u307e\u3059\u3002
        • \u30ea\u30b9\u30c8\u306f\u8981\u7d20\u306e\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3067\u3001\u8ffd\u52a0\u3001\u524a\u9664\u3001\u5909\u66f4\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3001\u901a\u5e38\u306f\u52d5\u7684\u914d\u5217\u306b\u57fa\u3065\u3044\u3066\u5b9f\u88c5\u3055\u308c\u3001\u914d\u5217\u306e\u5229\u70b9\u3092\u4fdd\u6301\u3057\u306a\u304c\u3089\u67d4\u8edf\u306a\u9577\u3055\u8abf\u6574\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002
        • \u30ea\u30b9\u30c8\u306e\u51fa\u73fe\u306b\u3088\u308a\u914d\u5217\u306e\u5b9f\u7528\u6027\u304c\u5927\u5e45\u306b\u5411\u4e0a\u3057\u307e\u3057\u305f\u304c\u3001\u4e00\u90e8\u306e\u30e1\u30e2\u30ea\u7a7a\u9593\u306e\u7121\u99c4\u306b\u3064\u306a\u304c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        • \u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c\u4e2d\u3001\u30c7\u30fc\u30bf\u306f\u4e3b\u306b\u30e1\u30e2\u30ea\u306b\u683c\u7d0d\u3055\u308c\u307e\u3059\u3002\u914d\u5217\u306f\u3088\u308a\u9ad8\u3044\u30e1\u30e2\u30ea\u7a7a\u9593\u52b9\u7387\u3092\u63d0\u4f9b\u3057\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u30e1\u30e2\u30ea\u4f7f\u7528\u306b\u304a\u3044\u3066\u3088\u308a\u67d4\u8edf\u3067\u3059\u3002
        • \u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u30e9\u30a4\u30f3\u3001\u5148\u8aad\u307f\u3001\u7a7a\u9593\u7684\u5c40\u6240\u6027\u3001\u6642\u9593\u7684\u5c40\u6240\u6027\u306a\u3069\u306e\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u901a\u3058\u3066CPU\u306b\u9ad8\u901f\u30c7\u30fc\u30bf\u30a2\u30af\u30bb\u30b9\u3092\u63d0\u4f9b\u3057\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c\u52b9\u7387\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002
        • \u3088\u308a\u9ad8\u3044\u30ad\u30e3\u30c3\u30b7\u30e5\u30d2\u30c3\u30c8\u7387\u306b\u3088\u308a\u3001\u914d\u5217\u306f\u4e00\u822c\u7684\u306b\u9023\u7d50\u30ea\u30b9\u30c8\u3088\u308a\u3082\u52b9\u7387\u7684\u3067\u3059\u3002\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u9078\u629e\u3059\u308b\u969b\u306f\u3001\u7279\u5b9a\u306e\u30cb\u30fc\u30ba\u3068\u30b7\u30ca\u30ea\u30aa\u306b\u57fa\u3065\u3044\u3066\u9069\u5207\u306a\u9078\u629e\u3092\u3059\u3079\u304d\u3067\u3059\u3002
        "},{"location":"chapter_array_and_linkedlist/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u914d\u5217\u3092\u30b9\u30bf\u30c3\u30af\u306b\u683c\u7d0d\u3059\u308b\u304b\u30d2\u30fc\u30d7\u306b\u683c\u7d0d\u3059\u308b\u304b\u306f\u3001\u6642\u9593\u3068\u7a7a\u9593\u52b9\u7387\u306b\u5f71\u97ff\u3057\u307e\u3059\u304b\uff1f

        \u30b9\u30bf\u30c3\u30af\u3068\u30d2\u30fc\u30d7\u306e\u4e21\u65b9\u306b\u683c\u7d0d\u3055\u308c\u308b\u914d\u5217\u306f\u9023\u7d9a\u3057\u305f\u30e1\u30e2\u30ea\u7a7a\u9593\u306b\u683c\u7d0d\u3055\u308c\u3001\u30c7\u30fc\u30bf\u64cd\u4f5c\u52b9\u7387\u306f\u672c\u8cea\u7684\u306b\u540c\u3058\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u30b9\u30bf\u30c3\u30af\u3068\u30d2\u30fc\u30d7\u306b\u306f\u72ec\u81ea\u306e\u7279\u6027\u304c\u3042\u308a\u3001\u4ee5\u4e0b\u306e\u9055\u3044\u304c\u751f\u3058\u307e\u3059\u3002

        1. \u5272\u308a\u5f53\u3066\u3068\u89e3\u653e\u52b9\u7387\uff1a\u30b9\u30bf\u30c3\u30af\u306f\u3088\u308a\u5c0f\u3055\u306a\u30e1\u30e2\u30ea\u30d6\u30ed\u30c3\u30af\u3067\u3001\u30b3\u30f3\u30d1\u30a4\u30e9\u306b\u3088\u3063\u3066\u81ea\u52d5\u7684\u306b\u5272\u308a\u5f53\u3066\u3089\u308c\u307e\u3059\u3002\u30d2\u30fc\u30d7\u30e1\u30e2\u30ea\u306f\u6bd4\u8f03\u7684\u5927\u304d\u304f\u3001\u30b3\u30fc\u30c9\u3067\u52d5\u7684\u306b\u5272\u308a\u5f53\u3066\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u65ad\u7247\u5316\u3057\u3084\u3059\u3044\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30d2\u30fc\u30d7\u3067\u306e\u5272\u308a\u5f53\u3066\u3068\u89e3\u653e\u64cd\u4f5c\u306f\u4e00\u822c\u7684\u306b\u30b9\u30bf\u30c3\u30af\u3088\u308a\u3082\u9045\u304f\u306a\u308a\u307e\u3059\u3002
        2. \u30b5\u30a4\u30ba\u5236\u9650\uff1a\u30b9\u30bf\u30c3\u30af\u30e1\u30e2\u30ea\u306f\u6bd4\u8f03\u7684\u5c0f\u3055\u304f\u3001\u30d2\u30fc\u30d7\u30b5\u30a4\u30ba\u306f\u4e00\u822c\u7684\u306b\u5229\u7528\u53ef\u80fd\u306a\u30e1\u30e2\u30ea\u306b\u3088\u3063\u3066\u5236\u9650\u3055\u308c\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30d2\u30fc\u30d7\u306f\u5927\u304d\u306a\u914d\u5217\u306e\u683c\u7d0d\u306b\u3088\u308a\u9069\u3057\u3066\u3044\u307e\u3059\u3002
        3. \u67d4\u8edf\u6027\uff1a\u30b9\u30bf\u30c3\u30af\u4e0a\u306e\u914d\u5217\u306e\u30b5\u30a4\u30ba\u306f\u30b3\u30f3\u30d1\u30a4\u30eb\u6642\u306b\u6c7a\u5b9a\u3055\u308c\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u30d2\u30fc\u30d7\u4e0a\u306e\u914d\u5217\u306e\u30b5\u30a4\u30ba\u306f\u5b9f\u884c\u6642\u306b\u52d5\u7684\u306b\u6c7a\u5b9a\u3067\u304d\u307e\u3059\u3002

        Q\uff1a\u306a\u305c\u914d\u5217\u306f\u540c\u3058\u578b\u306e\u8981\u7d20\u3092\u5fc5\u8981\u3068\u3057\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u540c\u3058\u578b\u306e\u8981\u7d20\u3092\u5f37\u8abf\u3057\u306a\u3044\u306e\u3067\u3059\u304b\uff1f

        \u9023\u7d50\u30ea\u30b9\u30c8\u306f\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u306b\u3088\u3063\u3066\u63a5\u7d9a\u3055\u308c\u305f\u30ce\u30fc\u30c9\u3067\u69cb\u6210\u3055\u308c\u3001\u5404\u30ce\u30fc\u30c9\u306fint\u3001double\u3001string\u3001object\u306a\u3069\u3001\u7570\u306a\u308b\u578b\u306e\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3067\u304d\u307e\u3059\u3002

        \u5bfe\u7167\u7684\u306b\u3001\u914d\u5217\u8981\u7d20\u306f\u540c\u3058\u578b\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u3053\u308c\u306b\u3088\u308a\u5bfe\u5fdc\u3059\u308b\u8981\u7d20\u4f4d\u7f6e\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u305f\u3081\u306e\u30aa\u30d5\u30bb\u30c3\u30c8\u3092\u8a08\u7b97\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001int\u3068long\u578b\u306e\u4e21\u65b9\u3092\u542b\u3080\u914d\u5217\u3067\u3001\u5358\u4e00\u8981\u7d20\u304c\u305d\u308c\u305e\u308c4\u30d0\u30a4\u30c8\u30688\u30d0\u30a4\u30c8\u3092\u5360\u6709\u3059\u308b\u5834\u5408\u3001\u914d\u5217\u306b2\u3064\u306e\u7570\u306a\u308b\u9577\u3055\u306e\u8981\u7d20\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u4ee5\u4e0b\u306e\u5f0f\u3092\u4f7f\u7528\u3057\u3066\u30aa\u30d5\u30bb\u30c3\u30c8\u3092\u8a08\u7b97\u3067\u304d\u307e\u305b\u3093\u3002

        # \u8981\u7d20\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9 = \u914d\u5217\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9 + \u8981\u7d20\u9577 * \u8981\u7d20\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\n

        Q\uff1a\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3057\u305f\u5f8c\u3001P.next\u3092None\u306b\u8a2d\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u304b\uff1f

        P.next\u3092\u5909\u66f4\u3057\u306a\u304f\u3066\u3082\u554f\u984c\u3042\u308a\u307e\u305b\u3093\u3002\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u89b3\u70b9\u304b\u3089\u3001\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u304b\u3089\u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u307e\u3067\u306e\u5de1\u56de\u3067P\u306b\u906d\u9047\u3059\u308b\u3053\u3068\u306f\u3082\u3046\u3042\u308a\u307e\u305b\u3093\u3002\u3053\u308c\u306f\u3001\u30ce\u30fc\u30c9P\u304c\u30ea\u30b9\u30c8\u304b\u3089\u52b9\u679c\u7684\u306b\u524a\u9664\u3055\u308c\u305f\u3053\u3068\u3092\u610f\u5473\u3057\u3001P\u304c\u6307\u3059\u5834\u6240\u306f\u3082\u306f\u3084\u30ea\u30b9\u30c8\u306b\u5f71\u97ff\u3057\u307e\u305b\u3093\u3002

        \u30ac\u30d9\u30fc\u30b8\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u89b3\u70b9\u304b\u3089\u3001Java\u3001Python\u3001Go\u306a\u3069\u306e\u81ea\u52d5\u30ac\u30d9\u30fc\u30b8\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u6301\u3064\u8a00\u8a9e\u3067\u306f\u3001\u30ce\u30fc\u30c9P\u304c\u53ce\u96c6\u3055\u308c\u308b\u304b\u3069\u3046\u304b\u306f\u3001\u305d\u308c\u3092\u6307\u3059\u53c2\u7167\u304c\u307e\u3060\u3042\u308b\u304b\u3069\u3046\u304b\u306b\u4f9d\u5b58\u3057\u3001P.next\u306e\u5024\u306b\u306f\u4f9d\u5b58\u3057\u307e\u305b\u3093\u3002C\u3084C++\u306a\u3069\u306e\u8a00\u8a9e\u3067\u306f\u3001\u30ce\u30fc\u30c9\u306e\u30e1\u30e2\u30ea\u3092\u624b\u52d5\u3067\u89e3\u653e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        Q\uff1a\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u306f\u3001\u633f\u5165\u3068\u524a\u9664\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306fO(1)\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u633f\u5165\u3084\u524a\u9664\u524d\u306e\u8981\u7d20\u691c\u7d22\u306b\u306fO(n)\u6642\u9593\u304c\u304b\u304b\u308b\u306e\u3067\u3001\u306a\u305c\u6642\u9593\u8a08\u7b97\u91cf\u306fO(n)\u3067\u306f\u306a\u3044\u306e\u3067\u3059\u304b\uff1f

        \u8981\u7d20\u3092\u6700\u521d\u306b\u691c\u7d22\u3057\u3066\u304b\u3089\u524a\u9664\u3059\u308b\u5834\u5408\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\u78ba\u304b\u306bO(n)\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u633f\u5165\u3068\u524a\u9664\u306b\u304a\u3051\u308bO(1)\u306e\u5229\u70b9\u306f\u4ed6\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u305f\u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u5b9f\u88c5\u3067\u306f\u3001\u5e38\u306b\u30d8\u30c3\u30c9\u3068\u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u3092\u6307\u3059\u30dd\u30a4\u30f3\u30bf\u3092\u7dad\u6301\u3057\u3001\u5404\u633f\u5165\u3068\u524a\u9664\u64cd\u4f5c\u3092O(1)\u306b\u3057\u307e\u3059\u3002

        Q\uff1a\u300c\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u5b9a\u7fa9\u3068\u683c\u7d0d\u65b9\u6cd5\u300d\u306e\u56f3\u3067\u3001\u8584\u9752\u8272\u306e\u683c\u7d0d\u30ce\u30fc\u30c9\u306f\u5358\u4e00\u306e\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u3092\u5360\u6709\u3057\u307e\u3059\u304b\u3001\u305d\u308c\u3068\u3082\u30ce\u30fc\u30c9\u5024\u3068\u534a\u5206\u3092\u5171\u6709\u3057\u307e\u3059\u304b\uff1f

        \u56f3\u306f\u5358\u306a\u308b\u5b9a\u6027\u7684\u306a\u8868\u73fe\u3067\u3042\u308a\u3001\u5b9a\u91cf\u7684\u5206\u6790\u306f\u7279\u5b9a\u306e\u72b6\u6cc1\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002

        • \u7570\u306a\u308b\u578b\u306e\u30ce\u30fc\u30c9\u5024\u306f\u7570\u306a\u308b\u91cf\u306e\u7a7a\u9593\u3092\u5360\u6709\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001int\u3001long\u3001double\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3067\u3059\u3002
        • \u30dd\u30a4\u30f3\u30bf\u5909\u6570\u306b\u3088\u3063\u3066\u5360\u6709\u3055\u308c\u308b\u30e1\u30e2\u30ea\u7a7a\u9593\u306f\u3001\u4f7f\u7528\u3055\u308c\u308b\u30aa\u30da\u30ec\u30fc\u30c6\u30a3\u30f3\u30b0\u30b7\u30b9\u30c6\u30e0\u3068\u30b3\u30f3\u30d1\u30a4\u30eb\u74b0\u5883\u306b\u4f9d\u5b58\u3057\u3001\u901a\u5e388\u30d0\u30a4\u30c8\u307e\u305f\u306f4\u30d0\u30a4\u30c8\u3067\u3059\u3002

        Q\uff1a\u30ea\u30b9\u30c8\u306e\u672b\u5c3e\u3078\u306e\u8981\u7d20\u8ffd\u52a0\u306f\u5e38\u306bO(1)\u3067\u3059\u304b\uff1f

        \u8981\u7d20\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3067\u30ea\u30b9\u30c8\u9577\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30ea\u30b9\u30c8\u306f\u6700\u521d\u306b\u62e1\u5f35\u3055\u308c\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u30b7\u30b9\u30c6\u30e0\u306f\u65b0\u3057\u3044\u30e1\u30e2\u30ea\u30d6\u30ed\u30c3\u30af\u3092\u8981\u6c42\u3057\u3001\u5143\u306e\u30ea\u30b9\u30c8\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u79fb\u52d5\u3059\u308b\u305f\u3081\u3001\u3053\u306e\u5834\u5408\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306fO(n)\u306b\u306a\u308a\u307e\u3059\u3002

        Q\uff1a\u300c\u30ea\u30b9\u30c8\u306e\u51fa\u73fe\u306b\u3088\u308a\u914d\u5217\u306e\u5b9f\u7528\u6027\u304c\u5927\u5e45\u306b\u5411\u4e0a\u3057\u307e\u3057\u305f\u304c\u3001\u4e00\u90e8\u306e\u30e1\u30e2\u30ea\u7a7a\u9593\u306e\u7121\u99c4\u306b\u3064\u306a\u304c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u300d\u3068\u3044\u3046\u6587\u306f\u3001\u5bb9\u91cf\u3001\u9577\u3055\u3001\u62e1\u5f35\u4fc2\u6570\u306a\u3069\u306e\u8ffd\u52a0\u5909\u6570\u306b\u3088\u3063\u3066\u5360\u6709\u3055\u308c\u308b\u30e1\u30e2\u30ea\u3092\u6307\u3057\u3066\u3044\u307e\u3059\u304b\uff1f

        \u3053\u3053\u3067\u306e\u7a7a\u9593\u306e\u7121\u99c4\u306f\u4e3b\u306b2\u3064\u306e\u5074\u9762\u3092\u6307\u3057\u307e\u3059\uff1a\u4e00\u65b9\u3067\u3001\u30ea\u30b9\u30c8\u306f\u521d\u671f\u9577\u3067\u8a2d\u5b9a\u3055\u308c\u307e\u3059\u304c\u3001\u5e38\u306b\u5fc5\u8981\u3068\u306f\u9650\u308a\u307e\u305b\u3093\u3002\u4ed6\u65b9\u3067\u3001\u983b\u7e41\u306a\u62e1\u5f35\u3092\u9632\u3050\u305f\u3081\u3001\u62e1\u5f35\u306f\u901a\u5e38\\(\\times 1.5\\)\u306a\u3069\u306e\u4fc2\u6570\u3067\u4e57\u7b97\u3055\u308c\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u591a\u304f\u306e\u7a7a\u304d\u30b9\u30ed\u30c3\u30c8\u304c\u751f\u307e\u308c\u3001\u901a\u5e38\u306f\u5b8c\u5168\u306b\u57cb\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u305b\u3093\u3002

        Q\uff1aPython\u3067n = [1, 2, 3]\u3092\u521d\u671f\u5316\u3057\u305f\u5f8c\u3001\u3053\u308c\u30893\u3064\u306e\u8981\u7d20\u306e\u30a2\u30c9\u30ec\u30b9\u306f\u9023\u7d9a\u3057\u3066\u3044\u307e\u3059\u304c\u3001m = [2, 1, 3]\u3092\u521d\u671f\u5316\u3059\u308b\u3068\u3001\u5404\u8981\u7d20\u306eid\u306f\u9023\u7d9a\u3057\u3066\u3044\u306a\u3044\u304cn\u306e\u3082\u306e\u3068\u540c\u4e00\u3067\u3059\u3002\u3053\u308c\u3089\u306e\u8981\u7d20\u306e\u30a2\u30c9\u30ec\u30b9\u304c\u9023\u7d9a\u3057\u3066\u3044\u306a\u3044\u5834\u5408\u3001m\u306f\u307e\u3060\u914d\u5217\u3067\u3059\u304b\uff1f

        \u30ea\u30b9\u30c8\u8981\u7d20\u3092\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9n = [n1, n2, n3, n4, n5]\u306b\u7f6e\u304d\u63db\u3048\u308b\u5834\u5408\u3001\u3053\u308c\u30895\u3064\u306e\u30ce\u30fc\u30c9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3082\u901a\u5e38\u30e1\u30e2\u30ea\u5168\u4f53\u306b\u5206\u6563\u3057\u3066\u3044\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u30ea\u30b9\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u4e0e\u3048\u3089\u308c\u308c\u3070\u3001O(1)\u6642\u9593\u3067\u30ce\u30fc\u30c9\u306e\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u3001\u5bfe\u5fdc\u3059\u308b\u30ce\u30fc\u30c9\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u914d\u5217\u304c\u30ce\u30fc\u30c9\u81ea\u4f53\u3067\u306f\u306a\u304f\u3001\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u3067\u3059\u3002

        \u591a\u304f\u306e\u8a00\u8a9e\u3068\u306f\u7570\u306a\u308a\u3001Python\u3067\u306f\u6570\u5024\u3082\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066\u30e9\u30c3\u30d7\u3055\u308c\u3001\u30ea\u30b9\u30c8\u306f\u6570\u5024\u81ea\u4f53\u3067\u306f\u306a\u304f\u3001\u3053\u308c\u3089\u306e\u6570\u5024\u3078\u306e\u53c2\u7167\u3092\u683c\u7d0d\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u30012\u3064\u306e\u914d\u5217\u306e\u540c\u3058\u6570\u5024\u304c\u540c\u3058id\u3092\u6301\u3061\u3001\u3053\u308c\u3089\u306e\u6570\u5024\u306e\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u306f\u9023\u7d9a\u3067\u3042\u308b\u5fc5\u8981\u304c\u306a\u3044\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002

        Q\uff1aC++ STL\u306estd::list\u306f\u3059\u3067\u306b\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u5b9f\u88c5\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u4e00\u90e8\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u66f8\u7c4d\u3067\u306f\u76f4\u63a5\u4f7f\u7528\u3057\u3066\u3044\u306a\u3044\u3088\u3046\u3067\u3059\u3002\u4f55\u304b\u5236\u9650\u304c\u3042\u308a\u307e\u3059\u304b\uff1f

        \u4e00\u65b9\u3067\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u5b9f\u88c5\u3059\u308b\u969b\u306f\u914d\u5217\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3092\u597d\u307f\u3001\u5fc5\u8981\u306a\u5834\u5408\u306e\u307f\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u4e3b\u306b2\u3064\u306e\u7406\u7531\u304c\u3042\u308a\u307e\u3059\u3002

        • \u7a7a\u9593\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\uff1a\u5404\u8981\u7d20\u306b2\u3064\u306e\u8ffd\u52a0\u30dd\u30a4\u30f3\u30bf\uff08\u524d\u306e\u8981\u7d20\u7528\u3068\u6b21\u306e\u8981\u7d20\u7528\uff09\u304c\u5fc5\u8981\u306a\u305f\u3081\u3001std::list\u306f\u901a\u5e38std::vector\u3088\u308a\u3082\u591a\u304f\u306e\u7a7a\u9593\u3092\u5360\u6709\u3057\u307e\u3059\u3002
        • \u30ad\u30e3\u30c3\u30b7\u30e5\u975e\u53cb\u597d\u7684\uff1a\u30c7\u30fc\u30bf\u304c\u9023\u7d9a\u3057\u3066\u683c\u7d0d\u3055\u308c\u3066\u3044\u306a\u3044\u305f\u3081\u3001std::list\u306f\u30ad\u30e3\u30c3\u30b7\u30e5\u5229\u7528\u7387\u304c\u4f4e\u304f\u306a\u308a\u307e\u3059\u3002\u4e00\u822c\u7684\u306b\u3001std::vector\u306e\u65b9\u304c\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u512a\u308c\u3066\u3044\u307e\u3059\u3002

        \u4ed6\u65b9\u3067\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u4e3b\u306b\u4e8c\u5206\u6728\u3068\u30b0\u30e9\u30d5\u306b\u5fc5\u8981\u3067\u3059\u3002\u30b9\u30bf\u30c3\u30af\u3068\u30ad\u30e5\u30fc\u306f\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u306f\u306a\u304f\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306estack\u3068queue\u30af\u30e9\u30b9\u3092\u4f7f\u7528\u3057\u3066\u5b9f\u88c5\u3055\u308c\u308b\u3053\u3068\u304c\u591a\u3044\u3067\u3059\u3002

        Q\uff1a\u30ea\u30b9\u30c8res = [0] * self.size()\u3092\u521d\u671f\u5316\u3059\u308b\u3068\u3001res\u306e\u5404\u8981\u7d20\u306f\u540c\u3058\u30a2\u30c9\u30ec\u30b9\u3092\u53c2\u7167\u3057\u307e\u3059\u304b\uff1f

        \u3044\u3044\u3048\u3002\u3057\u304b\u3057\u3001\u3053\u306e\u554f\u984c\u306f\u4e8c\u6b21\u5143\u914d\u5217\u3067\u767a\u751f\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u4e8c\u6b21\u5143\u30ea\u30b9\u30c8res = [[0]] * self.size()\u3092\u521d\u671f\u5316\u3059\u308b\u3068\u3001\u540c\u3058\u30ea\u30b9\u30c8[0]\u3092\u8907\u6570\u56de\u53c2\u7167\u3059\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002

        Q\uff1a\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3059\u308b\u969b\u3001\u305d\u306e\u5f8c\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\u3092\u65ad\u3064\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u304b\uff1f

        \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff08\u554f\u984c\u89e3\u6c7a\uff09\u306e\u89b3\u70b9\u304b\u3089\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u30ed\u30b8\u30c3\u30af\u304c\u6b63\u3057\u3044\u9650\u308a\u3001\u30ea\u30f3\u30af\u3092\u65ad\u305f\u306a\u304f\u3066\u3082\u554f\u984c\u3042\u308a\u307e\u305b\u3093\u3002\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u89b3\u70b9\u304b\u3089\u3001\u30ea\u30f3\u30af\u3092\u65ad\u3064\u65b9\u304c\u5b89\u5168\u3067\u8ad6\u7406\u7684\u306b\u660e\u78ba\u3067\u3059\u3002\u30ea\u30f3\u30af\u3092\u65ad\u305f\u305a\u3001\u524a\u9664\u3055\u308c\u305f\u30ce\u30fc\u30c9\u304c\u9069\u5207\u306b\u30ea\u30b5\u30a4\u30af\u30eb\u3055\u308c\u306a\u3044\u5834\u5408\u3001\u5f8c\u7d9a\u30ce\u30fc\u30c9\u306e\u30e1\u30e2\u30ea\u306e\u30ea\u30b5\u30a4\u30af\u30eb\u306b\u5f71\u97ff\u3092\u4e0e\u3048\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_backtracking/","title":"\u7b2c 13 \u7ae0 \u00a0 \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0","text":"

        Abstract

        \u8ff7\u8def\u306e\u63a2\u691c\u5bb6\u306e\u3088\u3046\u306b\u3001\u79c1\u305f\u3061\u306f\u524d\u9032\u3059\u308b\u9053\u3067\u969c\u5bb3\u306b\u906d\u9047\u3059\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002

        \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306e\u529b\u306f\u3001\u79c1\u305f\u3061\u306b\u65b0\u3057\u304f\u59cb\u3081\u308b\u3053\u3068\u3001\u8a66\u3057\u7d9a\u3051\u308b\u3053\u3068\u3001\u305d\u3057\u3066\u6700\u7d42\u7684\u306b\u5149\u3078\u306e\u51fa\u53e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002

        "},{"location":"chapter_backtracking/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 13.1 \u00a0 \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0
        • 13.2 \u00a0 \u9806\u5217\u554f\u984c
        • 13.3 \u00a0 \u90e8\u5206\u96c6\u5408\u548c\u554f\u984c
        • 13.4 \u00a0 N\u30af\u30a4\u30fc\u30f3\u554f\u984c
        • 13.5 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_backtracking/backtracking_algorithm/","title":"13.1 \u00a0 \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0","text":"

        \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u5168\u6570\u63a2\u7d22\u306b\u3088\u3063\u3066\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u65b9\u6cd5\u3067\u3059\u3002\u305d\u306e\u6838\u5fc3\u6982\u5ff5\u306f\u3001\u521d\u671f\u72b6\u614b\u304b\u3089\u958b\u59cb\u3057\u3066\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u89e3\u3092\u7dcf\u5f53\u305f\u308a\u3067\u63a2\u7d22\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u6b63\u3057\u3044\u3082\u306e\u3092\u8a18\u9332\u3057\u3001\u89e3\u304c\u898b\u3064\u304b\u308b\u304b\u3001\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u89e3\u304c\u8a66\u3055\u308c\u305f\u304c\u89e3\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002

        \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306f\u901a\u5e38\u300c\u6df1\u3055\u512a\u5148\u63a2\u7d22\u300d\u3092\u4f7f\u7528\u3057\u3066\u89e3\u7a7a\u9593\u3092\u8d70\u67fb\u3057\u307e\u3059\u3002\u300c\u4e8c\u5206\u6728\u300d\u306e\u7ae0\u3067\u3001\u524d\u9806\u3001\u4e2d\u9806\u3001\u5f8c\u9806\u8d70\u67fb\u306f\u3059\u3079\u3066\u6df1\u3055\u512a\u5148\u63a2\u7d22\u3067\u3042\u308b\u3053\u3068\u3092\u8ff0\u3079\u307e\u3057\u305f\u3002\u6b21\u306b\u3001\u524d\u9806\u8d70\u67fb\u3092\u4f7f\u7528\u3057\u3066\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u554f\u984c\u3092\u89e3\u6c7a\u3057\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52d5\u4f5c\u3092\u6bb5\u968e\u7684\u306b\u7406\u89e3\u3057\u3066\u3044\u304d\u307e\u3059\u3002

        \u4f8b1

        \u4e8c\u5206\u6728\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u5024\u304c \\(7\\) \u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3092\u691c\u7d22\u3057\u3066\u8a18\u9332\u3057\u3001\u30ea\u30b9\u30c8\u3067\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3001\u3053\u306e\u6728\u3092\u524d\u9806\u3067\u8d70\u67fb\u3057\u3001\u73fe\u5728\u306e\u30ce\u30fc\u30c9\u306e\u5024\u304c \\(7\\) \u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002\u305d\u3046\u3067\u3042\u308c\u3070\u3001\u30ce\u30fc\u30c9\u306e\u5024\u3092\u7d50\u679c\u30ea\u30b9\u30c8 res \u306b\u8ffd\u52a0\u3057\u307e\u3059\u3002\u30d7\u30ed\u30bb\u30b9\u306f\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_i_compact.py
        def pre_order(root: TreeNode):\n    \"\"\"\u524d\u9806\u8d70\u67fb\uff1a\u4f8b\u4e00\"\"\"\n    if root is None:\n        return\n    if root.val == 7:\n        # \u89e3\u3092\u8a18\u9332\n        res.append(root)\n    pre_order(root.left)\n    pre_order(root.right)\n
        preorder_traversal_i_compact.cpp
        /* \u524d\u9806\u8d70\u67fb\uff1a\u4f8b\uff11 */\nvoid preOrder(TreeNode *root) {\n    if (root == nullptr) {\n        return;\n    }\n    if (root->val == 7) {\n        // \u89e3\u3092\u8a18\u9332\n        res.push_back(root);\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n}\n
        preorder_traversal_i_compact.java
        /* \u524d\u9806\u8d70\u67fb\uff1a\u4f8b\uff11 */\nvoid preOrder(TreeNode root) {\n    if (root == null) {\n        return;\n    }\n    if (root.val == 7) {\n        // \u89e3\u3092\u8a18\u9332\n        res.add(root);\n    }\n    preOrder(root.left);\n    preOrder(root.right);\n}\n
        preorder_traversal_i_compact.cs
        [class]{preorder_traversal_i_compact}-[func]{PreOrder}\n
        preorder_traversal_i_compact.go
        [class]{}-[func]{preOrderI}\n
        preorder_traversal_i_compact.swift
        [class]{}-[func]{preOrder}\n
        preorder_traversal_i_compact.js
        [class]{}-[func]{preOrder}\n
        preorder_traversal_i_compact.ts
        [class]{}-[func]{preOrder}\n
        preorder_traversal_i_compact.dart
        [class]{}-[func]{preOrder}\n
        preorder_traversal_i_compact.rs
        [class]{}-[func]{pre_order}\n
        preorder_traversal_i_compact.c
        [class]{}-[func]{preOrder}\n
        preorder_traversal_i_compact.kt
        [class]{}-[func]{preOrder}\n
        preorder_traversal_i_compact.rb
        [class]{}-[func]{pre_order}\n

        \u56f3 13-1 \u00a0 \u524d\u9806\u8d70\u67fb\u3067\u306e\u30ce\u30fc\u30c9\u691c\u7d22

        "},{"location":"chapter_backtracking/backtracking_algorithm/#1311","title":"13.1.1 \u00a0 \u8a66\u884c\u3068\u5f8c\u9000","text":"

        \u89e3\u7a7a\u9593\u3092\u63a2\u7d22\u3059\u308b\u969b\u306b\u300c\u8a66\u884c\u300d\u3068\u300c\u5f8c\u9000\u300d\u6226\u7565\u3092\u4f7f\u7528\u3059\u308b\u305f\u3081\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u547c\u3070\u308c\u307e\u3059\u3002\u63a2\u7d22\u4e2d\u3001\u6e80\u8db3\u306e\u3044\u304f\u89e3\u3092\u5f97\u308b\u305f\u3081\u306b\u3082\u306f\u3084\u9032\u3081\u306a\u3044\u72b6\u614b\u306b\u906d\u9047\u3059\u308b\u305f\u3073\u306b\u3001\u524d\u306e\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3066\u524d\u306e\u72b6\u614b\u306b\u623b\u308a\u3001\u6b21\u306e\u8a66\u884c\u306e\u305f\u3081\u306b\u4ed6\u306e\u53ef\u80fd\u306a\u9078\u629e\u3092\u9078\u629e\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002

        \u4f8b1\u3067\u306f\u3001\u5404\u30ce\u30fc\u30c9\u306e\u8a2a\u554f\u304c\u300c\u8a66\u884c\u300d\u3092\u958b\u59cb\u3057\u307e\u3059\u3002\u305d\u3057\u3066\u8449\u30ce\u30fc\u30c9\u3092\u901a\u904e\u3059\u308b\u304b\u3001return \u6587\u3067\u89aa\u30ce\u30fc\u30c9\u306b\u623b\u308b\u3053\u3068\u304c\u300c\u5f8c\u9000\u300d\u3092\u793a\u5506\u3057\u307e\u3059\u3002

        \u5f8c\u9000\u306f\u5358\u306b\u95a2\u6570\u306e\u623b\u308a\u5024\u3067\u306f\u306a\u3044\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4f8b1\u306e\u554f\u984c\u3092\u5c11\u3057\u62e1\u5f35\u3057\u3066\u3001\u305d\u308c\u304c\u4f55\u3092\u610f\u5473\u3059\u308b\u304b\u3092\u8aac\u660e\u3057\u307e\u3059\u3002

        \u4f8b2

        \u4e8c\u5206\u6728\u3067\u3001\u5024\u304c \\(7\\) \u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3092\u691c\u7d22\u3057\u3001\u3059\u3079\u3066\u306e\u30de\u30c3\u30c1\u30f3\u30b0\u30ce\u30fc\u30c9\u306b\u3064\u3044\u3066\u3001\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u304b\u3089\u305d\u306e\u30ce\u30fc\u30c9\u307e\u3067\u306e\u30d1\u30b9\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u4f8b1\u306e\u30b3\u30fc\u30c9\u306b\u57fa\u3065\u3044\u3066\u3001\u8a2a\u554f\u3057\u305f\u30ce\u30fc\u30c9\u30d1\u30b9\u3092\u8a18\u9332\u3059\u308b\u305f\u3081\u306b path \u3068\u3044\u3046\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u5024\u304c \\(7\\) \u306e\u30ce\u30fc\u30c9\u306b\u5230\u9054\u3059\u308b\u3068\u3001path \u3092\u30b3\u30d4\u30fc\u3057\u3066\u7d50\u679c\u30ea\u30b9\u30c8 res \u306b\u8ffd\u52a0\u3057\u307e\u3059\u3002\u8d70\u67fb\u5f8c\u3001res \u306b\u306f\u3059\u3079\u3066\u306e\u89e3\u304c\u4fdd\u6301\u3055\u308c\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_ii_compact.py
        def pre_order(root: TreeNode):\n    \"\"\"\u524d\u9806\u8d70\u67fb\uff1a\u4f8b\u4e8c\"\"\"\n    if root is None:\n        return\n    # \u8a66\u884c\n    path.append(root)\n    if root.val == 7:\n        # \u89e3\u3092\u8a18\u9332\n        res.append(list(path))\n    pre_order(root.left)\n    pre_order(root.right)\n    # \u64a4\u56de\n    path.pop()\n
        preorder_traversal_ii_compact.cpp
        /* \u524d\u9806\u8d70\u67fb\uff1a\u4f8b\uff12 */\nvoid preOrder(TreeNode *root) {\n    if (root == nullptr) {\n        return;\n    }\n    // \u8a66\u884c\n    path.push_back(root);\n    if (root->val == 7) {\n        // \u89e3\u3092\u8a18\u9332\n        res.push_back(path);\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // \u56de\u9000\n    path.pop_back();\n}\n
        preorder_traversal_ii_compact.java
        /* \u524d\u9806\u8d70\u67fb\uff1a\u4f8b\uff12 */\nvoid preOrder(TreeNode root) {\n    if (root == null) {\n        return;\n    }\n    // \u8a66\u884c\n    path.add(root);\n    if (root.val == 7) {\n        // \u89e3\u3092\u8a18\u9332\n        res.add(new ArrayList<>(path));\n    }\n    preOrder(root.left);\n    preOrder(root.right);\n    // \u56de\u9000\n    path.remove(path.size() - 1);\n}\n
        preorder_traversal_ii_compact.cs
        [class]{preorder_traversal_ii_compact}-[func]{PreOrder}\n
        preorder_traversal_ii_compact.go
        [class]{}-[func]{preOrderII}\n
        preorder_traversal_ii_compact.swift
        [class]{}-[func]{preOrder}\n
        preorder_traversal_ii_compact.js
        [class]{}-[func]{preOrder}\n
        preorder_traversal_ii_compact.ts
        [class]{}-[func]{preOrder}\n
        preorder_traversal_ii_compact.dart
        [class]{}-[func]{preOrder}\n
        preorder_traversal_ii_compact.rs
        [class]{}-[func]{pre_order}\n
        preorder_traversal_ii_compact.c
        [class]{}-[func]{preOrder}\n
        preorder_traversal_ii_compact.kt
        [class]{}-[func]{preOrder}\n
        preorder_traversal_ii_compact.rb
        [class]{}-[func]{pre_order}\n

        \u5404\u300c\u8a66\u884c\u300d\u3067\u3001\u73fe\u5728\u306e\u30ce\u30fc\u30c9\u3092 path \u306b\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3067\u30d1\u30b9\u3092\u8a18\u9332\u3057\u307e\u3059\u3002\u300c\u5f8c\u9000\u300d\u304c\u5fc5\u8981\u306a\u3068\u304d\u306f\u3044\u3064\u3067\u3082\u3001path \u304b\u3089\u30ce\u30fc\u30c9\u3092\u30dd\u30c3\u30d7\u3057\u3066**\u3053\u306e\u5931\u6557\u3057\u305f\u8a66\u884c\u524d\u306e\u72b6\u614b\u3092\u5fa9\u5143\u3057\u307e\u3059**\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u30d7\u30ed\u30bb\u30b9\u3092\u89b3\u5bdf\u3059\u308b\u3053\u3068\u3067\u3001\u8a66\u884c\u306f\u300c\u524d\u9032\u300d\u306e\u3088\u3046\u3067\u3001\u5f8c\u9000\u306f\u300c\u5143\u306b\u623b\u3059\u300d\u306e\u3088\u3046\u3067\u3059\u3002\u5f8c\u8005\u306e\u30da\u30a2\u306f\u3001\u5bfe\u5fdc\u3059\u308b\u3082\u306e\u306b\u5bfe\u3059\u308b\u9006\u64cd\u4f5c\u3068\u898b\u306a\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        <1><2><3><4><5><6><7><8><9><10><11>

        \u56f3 13-2 \u00a0 \u8a66\u884c\u3068\u5f8c\u9000

        "},{"location":"chapter_backtracking/backtracking_algorithm/#1312","title":"13.1.2 \u00a0 \u526a\u5b9a","text":"

        \u8907\u96d1\u306a\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u554f\u984c\u306f\u901a\u5e381\u3064\u4ee5\u4e0a\u306e\u5236\u7d04\u3092\u542b\u307f\u3001\u3053\u308c\u3089\u306f\u300c\u526a\u5b9a\u300d\u306b\u3088\u304f\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002

        \u4f8b3

        \u4e8c\u5206\u6728\u3067\u3001\u5024\u304c \\(7\\) \u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3092\u691c\u7d22\u3057\u3001\u30eb\u30fc\u30c8\u304b\u3089\u3053\u308c\u3089\u306e\u30ce\u30fc\u30c9\u307e\u3067\u306e\u30d1\u30b9\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u305f\u3060\u3057\u3001\u30d1\u30b9\u306b\u306f\u5024\u304c \\(3\\) \u306e\u30ce\u30fc\u30c9\u3092\u542b\u307e\u306a\u3044\u3068\u3044\u3046\u5236\u9650\u304c\u3042\u308a\u307e\u3059\u3002

        \u4e0a\u8a18\u306e\u5236\u7d04\u3092\u6e80\u305f\u3059\u305f\u3081\u306b\u3001\u526a\u5b9a\u64cd\u4f5c\u3092\u8ffd\u52a0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff1a\u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u4e2d\u306b\u3001\u5024\u304c \\(3\\) \u306e\u30ce\u30fc\u30c9\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u305d\u306e\u30d1\u30b9\u3092\u901a\u3058\u3066\u3055\u3089\u306b\u691c\u7d22\u3059\u308b\u3053\u3068\u3092\u5373\u5ea7\u306b\u4e2d\u6b62\u3057\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_iii_compact.py
        def pre_order(root: TreeNode):\n    \"\"\"\u524d\u9806\u8d70\u67fb\uff1a\u4f8b\u4e09\"\"\"\n    # \u679d\u5208\u308a\n    if root is None or root.val == 3:\n        return\n    # \u8a66\u884c\n    path.append(root)\n    if root.val == 7:\n        # \u89e3\u3092\u8a18\u9332\n        res.append(list(path))\n    pre_order(root.left)\n    pre_order(root.right)\n    # \u64a4\u56de\n    path.pop()\n
        preorder_traversal_iii_compact.cpp
        /* \u524d\u9806\u8d70\u67fb\uff1a\u4f8b\uff13 */\nvoid preOrder(TreeNode *root) {\n    // \u526a\u5b9a\n    if (root == nullptr || root->val == 3) {\n        return;\n    }\n    // \u8a66\u884c\n    path.push_back(root);\n    if (root->val == 7) {\n        // \u89e3\u3092\u8a18\u9332\n        res.push_back(path);\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // \u56de\u9000\n    path.pop_back();\n}\n
        preorder_traversal_iii_compact.java
        /* \u524d\u9806\u8d70\u67fb\uff1a\u4f8b\uff13 */\nvoid preOrder(TreeNode root) {\n    // \u526a\u5b9a\n    if (root == null || root.val == 3) {\n        return;\n    }\n    // \u8a66\u884c\n    path.add(root);\n    if (root.val == 7) {\n        // \u89e3\u3092\u8a18\u9332\n        res.add(new ArrayList<>(path));\n    }\n    preOrder(root.left);\n    preOrder(root.right);\n    // \u56de\u9000\n    path.remove(path.size() - 1);\n}\n
        preorder_traversal_iii_compact.cs
        [class]{preorder_traversal_iii_compact}-[func]{PreOrder}\n
        preorder_traversal_iii_compact.go
        [class]{}-[func]{preOrderIII}\n
        preorder_traversal_iii_compact.swift
        [class]{}-[func]{preOrder}\n
        preorder_traversal_iii_compact.js
        [class]{}-[func]{preOrder}\n
        preorder_traversal_iii_compact.ts
        [class]{}-[func]{preOrder}\n
        preorder_traversal_iii_compact.dart
        [class]{}-[func]{preOrder}\n
        preorder_traversal_iii_compact.rs
        [class]{}-[func]{pre_order}\n
        preorder_traversal_iii_compact.c
        [class]{}-[func]{preOrder}\n
        preorder_traversal_iii_compact.kt
        [class]{}-[func]{preOrder}\n
        preorder_traversal_iii_compact.rb
        [class]{}-[func]{pre_order}\n

        \u300c\u526a\u5b9a\u300d\u306f\u975e\u5e38\u306b\u751f\u304d\u751f\u304d\u3068\u3057\u305f\u540d\u8a5e\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u3067\u3001\u5236\u7d04\u3092\u6e80\u305f\u3055\u306a\u3044\u691c\u7d22\u5206\u5c90\u3092\u300c\u5207\u308a\u53d6\u308a\u300d\u307e\u3059\u3002\u3055\u3089\u306a\u308b\u4e0d\u8981\u306a\u8a66\u884c\u3092\u907f\u3051\u3001\u691c\u7d22\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002

        \u56f3 13-3 \u00a0 \u5236\u7d04\u306b\u57fa\u3065\u304f\u526a\u5b9a

        "},{"location":"chapter_backtracking/backtracking_algorithm/#1313","title":"13.1.3 \u00a0 \u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u30b3\u30fc\u30c9","text":"

        \u4eca\u5ea6\u306f\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u304b\u3089\u300c\u8a66\u884c\u3001\u5f8c\u9000\u3001\u526a\u5b9a\u300d\u306e\u4e3b\u8981\u306a\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u62bd\u51fa\u3057\u3066\u3001\u30b3\u30fc\u30c9\u306e\u6c4e\u7528\u6027\u3092\u5411\u4e0a\u3055\u305b\u3066\u307f\u307e\u3057\u3087\u3046\u3002

        \u4ee5\u4e0b\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u30b3\u30fc\u30c9\u3067\u306f\u3001state \u306f\u554f\u984c\u306e\u73fe\u5728\u306e\u72b6\u614b\u3092\u8868\u3057\u3001choices \u306f\u73fe\u5728\u306e\u72b6\u614b\u3067\u5229\u7528\u53ef\u80fd\u306a\u9078\u629e\u80a2\u3092\u8868\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        def backtrack(state: State, choices: list[choice], res: list[state]):\n    \"\"\"\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\"\"\"\n    # \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if is_solution(state):\n        # \u89e3\u3092\u8a18\u9332\n        record_solution(state, res)\n        # \u691c\u7d22\u3092\u505c\u6b62\n        return\n    # \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    for choice in choices:\n        # \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if is_valid(state, choice):\n            # \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            make_choice(state, choice)\n            backtrack(state, choices, res)\n            # \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            undo_choice(state, choice)\n
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af */\nvoid backtrack(State *state, vector<Choice *> &choices, vector<State *> &res) {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if (isSolution(state)) {\n        // \u89e3\u3092\u8a18\u9332\n        recordSolution(state, res);\n        // \u691c\u7d22\u3092\u505c\u6b62\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    for (Choice choice : choices) {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if (isValid(state, choice)) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            undoChoice(state, choice);\n        }\n    }\n}\n
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af */\nvoid backtrack(State state, List<Choice> choices, List<State> res) {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if (isSolution(state)) {\n        // \u89e3\u3092\u8a18\u9332\n        recordSolution(state, res);\n        // \u691c\u7d22\u3092\u505c\u6b62\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    for (Choice choice : choices) {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if (isValid(state, choice)) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            undoChoice(state, choice);\n        }\n    }\n}\n
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af */\nvoid Backtrack(State state, List<Choice> choices, List<State> res) {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if (IsSolution(state)) {\n        // \u89e3\u3092\u8a18\u9332\n        RecordSolution(state, res);\n        // \u691c\u7d22\u3092\u505c\u6b62\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    foreach (Choice choice in choices) {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if (IsValid(state, choice)) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            MakeChoice(state, choice);\n            Backtrack(state, choices, res);\n            // \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            UndoChoice(state, choice);\n        }\n    }\n}\n
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af */\nfunc backtrack(state *State, choices []Choice, res *[]State) {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if isSolution(state) {\n        // \u89e3\u3092\u8a18\u9332\n        recordSolution(state, res)\n        // \u691c\u7d22\u3092\u505c\u6b62\n        return\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    for _, choice := range choices {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if isValid(state, choice) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            makeChoice(state, choice)\n            backtrack(state, choices, res)\n            // \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            undoChoice(state, choice)\n        }\n    }\n}\n
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af */\nfunc backtrack(state: inout State, choices: [Choice], res: inout [State]) {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if isSolution(state: state) {\n        // \u89e3\u3092\u8a18\u9332\n        recordSolution(state: state, res: &res)\n        // \u691c\u7d22\u3092\u505c\u6b62\n        return\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    for choice in choices {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if isValid(state: state, choice: choice) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            makeChoice(state: &state, choice: choice)\n            backtrack(state: &state, choices: choices, res: &res)\n            // \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            undoChoice(state: &state, choice: choice)\n        }\n    }\n}\n
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af */\nfunction backtrack(state, choices, res) {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if (isSolution(state)) {\n        // \u89e3\u3092\u8a18\u9332\n        recordSolution(state, res);\n        // \u691c\u7d22\u3092\u505c\u6b62\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    for (let choice of choices) {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if (isValid(state, choice)) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            undoChoice(state, choice);\n        }\n    }\n}\n
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af */\nfunction backtrack(state: State, choices: Choice[], res: State[]): void {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if (isSolution(state)) {\n        // \u89e3\u3092\u8a18\u9332\n        recordSolution(state, res);\n        // \u691c\u7d22\u3092\u505c\u6b62\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    for (let choice of choices) {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if (isValid(state, choice)) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            undoChoice(state, choice);\n        }\n    }\n}\n
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af */\nvoid backtrack(State state, List<Choice>, List<State> res) {\n  // \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n  if (isSolution(state)) {\n    // \u89e3\u3092\u8a18\u9332\n    recordSolution(state, res);\n    // \u691c\u7d22\u3092\u505c\u6b62\n    return;\n  }\n  // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n  for (Choice choice in choices) {\n    // \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if (isValid(state, choice)) {\n      // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n      makeChoice(state, choice);\n      backtrack(state, choices, res);\n      // \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n      undoChoice(state, choice);\n    }\n  }\n}\n
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af */\nfn backtrack(state: &mut State, choices: &Vec<Choice>, res: &mut Vec<State>) {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if is_solution(state) {\n        // \u89e3\u3092\u8a18\u9332\n        record_solution(state, res);\n        // \u691c\u7d22\u3092\u505c\u6b62\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    for choice in choices {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if is_valid(state, choice) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            make_choice(state, choice);\n            backtrack(state, choices, res);\n            // \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            undo_choice(state, choice);\n        }\n    }\n}\n
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af */\nvoid backtrack(State *state, Choice *choices, int numChoices, State *res, int numRes) {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if (isSolution(state)) {\n        // \u89e3\u3092\u8a18\u9332\n        recordSolution(state, res, numRes);\n        // \u691c\u7d22\u3092\u505c\u6b62\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    for (int i = 0; i < numChoices; i++) {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if (isValid(state, &choices[i])) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            makeChoice(state, &choices[i]);\n            backtrack(state, choices, numChoices, res, numRes);\n            // \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            undoChoice(state, &choices[i]);\n        }\n    }\n}\n
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af */\nfun backtrack(state: State?, choices: List<Choice?>, res: List<State?>?) {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if (isSolution(state)) {\n        // \u89e3\u3092\u8a18\u9332\n        recordSolution(state, res)\n        // \u691c\u7d22\u3092\u505c\u6b62\n        return\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    for (choice in choices) {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if (isValid(state, choice)) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            makeChoice(state, choice)\n            backtrack(state, choices, res)\n            // \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            undoChoice(state, choice)\n        }\n    }\n}\n
        ### \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af ###\ndef backtrack(state, choices, res)\n    # \u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n    if is_solution?(state)\n        # \u89e3\u3092\u8a18\u9332\n        record_solution(state, res)\n        return\n    end\n\n    # \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u53cd\u5fa9\n    for choice in choices\n        # \u526a\u5b9a\uff1a\u9078\u629e\u80a2\u304c\u6709\u52b9\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n        if is_valid?(state, choice)\n            # \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            make_choice(state, choice)\n            backtrack(state, choices, res)\n            # \u5f8c\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u623b\u3059\n            undo_choice(state, choice)\n        end\n    end\nend\n

        \u6b21\u306b\u3001\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u30b3\u30fc\u30c9\u306b\u57fa\u3065\u3044\u3066\u4f8b\u984c 3 \u3092\u89e3\u304d\u307e\u3059\u3002\u72b6\u614b state \u306f\u30ce\u30fc\u30c9\u306e\u8d70\u67fb\u7d4c\u8def\u3092\u8868\u3057\u3001\u9078\u629e\u80a2 choices \u306f\u73fe\u5728\u30ce\u30fc\u30c9\u306e\u5de6\u5b50\u30ce\u30fc\u30c9\u3068\u53f3\u5b50\u30ce\u30fc\u30c9\u3001\u7d50\u679c res \u306f\u7d4c\u8def\u30ea\u30b9\u30c8\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_iii_template.py
        def is_solution(state: list[TreeNode]) -> bool:\n    \"\"\"\u73fe\u5728\u306e\u72b6\u614b\u304c\u89e3\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\"\"\"\n    return state and state[-1].val == 7\n\ndef record_solution(state: list[TreeNode], res: list[list[TreeNode]]):\n    \"\"\"\u89e3\u3092\u8a18\u9332\"\"\"\n    res.append(list(state))\n\ndef is_valid(state: list[TreeNode], choice: TreeNode) -> bool:\n    \"\"\"\u73fe\u5728\u306e\u72b6\u614b\u4e0b\u3067\u9078\u629e\u304c\u5408\u6cd5\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\"\"\"\n    return choice is not None and choice.val != 3\n\ndef make_choice(state: list[TreeNode], choice: TreeNode):\n    \"\"\"\u72b6\u614b\u3092\u66f4\u65b0\"\"\"\n    state.append(choice)\n\ndef undo_choice(state: list[TreeNode], choice: TreeNode):\n    \"\"\"\u72b6\u614b\u3092\u5fa9\u5143\"\"\"\n    state.pop()\n\ndef backtrack(\n    state: list[TreeNode], choices: list[TreeNode], res: list[list[TreeNode]]\n):\n    \"\"\"\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u4f8b\u4e09\"\"\"\n    # \u89e3\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\n    if is_solution(state):\n        # \u89e3\u3092\u8a18\u9332\n        record_solution(state, res)\n    # \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for choice in choices:\n        # \u679d\u5208\u308a\uff1a\u9078\u629e\u304c\u5408\u6cd5\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\n        if is_valid(state, choice):\n            # \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            make_choice(state, choice)\n            # \u6b21\u306e\u9078\u629e\u30e9\u30a6\u30f3\u30c9\u306b\u9032\u3080\n            backtrack(state, [choice.left, choice.right], res)\n            # \u64a4\u56de\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n            undo_choice(state, choice)\n
        preorder_traversal_iii_template.cpp
        /* \u73fe\u5728\u306e\u72b6\u614b\u304c\u89e3\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\nbool isSolution(vector<TreeNode *> &state) {\n    return !state.empty() && state.back()->val == 7;\n}\n\n/* \u89e3\u3092\u8a18\u9332 */\nvoid recordSolution(vector<TreeNode *> &state, vector<vector<TreeNode *>> &res) {\n    res.push_back(state);\n}\n\n/* \u73fe\u5728\u306e\u72b6\u614b\u4e0b\u3067\u9078\u629e\u304c\u5408\u6cd5\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\nbool isValid(vector<TreeNode *> &state, TreeNode *choice) {\n    return choice != nullptr && choice->val != 3;\n}\n\n/* \u72b6\u614b\u3092\u66f4\u65b0 */\nvoid makeChoice(vector<TreeNode *> &state, TreeNode *choice) {\n    state.push_back(choice);\n}\n\n/* \u72b6\u614b\u3092\u5fa9\u5143 */\nvoid undoChoice(vector<TreeNode *> &state, TreeNode *choice) {\n    state.pop_back();\n}\n\n/* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u4f8b\uff13 */\nvoid backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<vector<TreeNode *>> &res) {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\n    if (isSolution(state)) {\n        // \u89e3\u3092\u8a18\u9332\n        recordSolution(state, res);\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for (TreeNode *choice : choices) {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u304c\u5408\u6cd5\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\n        if (isValid(state, choice)) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            makeChoice(state, choice);\n            // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n            vector<TreeNode *> nextChoices{choice->left, choice->right};\n            backtrack(state, nextChoices, res);\n            // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n            undoChoice(state, choice);\n        }\n    }\n}\n
        preorder_traversal_iii_template.java
        /* \u73fe\u5728\u306e\u72b6\u614b\u304c\u89e3\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\nboolean isSolution(List<TreeNode> state) {\n    return !state.isEmpty() && state.get(state.size() - 1).val == 7;\n}\n\n/* \u89e3\u3092\u8a18\u9332 */\nvoid recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n    res.add(new ArrayList<>(state));\n}\n\n/* \u73fe\u5728\u306e\u72b6\u614b\u4e0b\u3067\u9078\u629e\u304c\u5408\u6cd5\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\nboolean isValid(List<TreeNode> state, TreeNode choice) {\n    return choice != null && choice.val != 3;\n}\n\n/* \u72b6\u614b\u3092\u66f4\u65b0 */\nvoid makeChoice(List<TreeNode> state, TreeNode choice) {\n    state.add(choice);\n}\n\n/* \u72b6\u614b\u3092\u5fa9\u5143 */\nvoid undoChoice(List<TreeNode> state, TreeNode choice) {\n    state.remove(state.size() - 1);\n}\n\n/* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u4f8b\uff13 */\nvoid backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {\n    // \u89e3\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\n    if (isSolution(state)) {\n        // \u89e3\u3092\u8a18\u9332\n        recordSolution(state, res);\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for (TreeNode choice : choices) {\n        // \u526a\u5b9a\uff1a\u9078\u629e\u304c\u5408\u6cd5\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\n        if (isValid(state, choice)) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            makeChoice(state, choice);\n            // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n            backtrack(state, Arrays.asList(choice.left, choice.right), res);\n            // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n            undoChoice(state, choice);\n        }\n    }\n}\n
        preorder_traversal_iii_template.cs
        [class]{preorder_traversal_iii_template}-[func]{IsSolution}\n\n[class]{preorder_traversal_iii_template}-[func]{RecordSolution}\n\n[class]{preorder_traversal_iii_template}-[func]{IsValid}\n\n[class]{preorder_traversal_iii_template}-[func]{MakeChoice}\n\n[class]{preorder_traversal_iii_template}-[func]{UndoChoice}\n\n[class]{preorder_traversal_iii_template}-[func]{Backtrack}\n
        preorder_traversal_iii_template.go
        [class]{}-[func]{isSolution}\n\n[class]{}-[func]{recordSolution}\n\n[class]{}-[func]{isValid}\n\n[class]{}-[func]{makeChoice}\n\n[class]{}-[func]{undoChoice}\n\n[class]{}-[func]{backtrackIII}\n
        preorder_traversal_iii_template.swift
        [class]{}-[func]{isSolution}\n\n[class]{}-[func]{recordSolution}\n\n[class]{}-[func]{isValid}\n\n[class]{}-[func]{makeChoice}\n\n[class]{}-[func]{undoChoice}\n\n[class]{}-[func]{backtrack}\n
        preorder_traversal_iii_template.js
        [class]{}-[func]{isSolution}\n\n[class]{}-[func]{recordSolution}\n\n[class]{}-[func]{isValid}\n\n[class]{}-[func]{makeChoice}\n\n[class]{}-[func]{undoChoice}\n\n[class]{}-[func]{backtrack}\n
        preorder_traversal_iii_template.ts
        [class]{}-[func]{isSolution}\n\n[class]{}-[func]{recordSolution}\n\n[class]{}-[func]{isValid}\n\n[class]{}-[func]{makeChoice}\n\n[class]{}-[func]{undoChoice}\n\n[class]{}-[func]{backtrack}\n
        preorder_traversal_iii_template.dart
        [class]{}-[func]{isSolution}\n\n[class]{}-[func]{recordSolution}\n\n[class]{}-[func]{isValid}\n\n[class]{}-[func]{makeChoice}\n\n[class]{}-[func]{undoChoice}\n\n[class]{}-[func]{backtrack}\n
        preorder_traversal_iii_template.rs
        [class]{}-[func]{is_solution}\n\n[class]{}-[func]{record_solution}\n\n[class]{}-[func]{is_valid}\n\n[class]{}-[func]{make_choice}\n\n[class]{}-[func]{undo_choice}\n\n[class]{}-[func]{backtrack}\n
        preorder_traversal_iii_template.c
        [class]{}-[func]{isSolution}\n\n[class]{}-[func]{recordSolution}\n\n[class]{}-[func]{isValid}\n\n[class]{}-[func]{makeChoice}\n\n[class]{}-[func]{undoChoice}\n\n[class]{}-[func]{backtrack}\n
        preorder_traversal_iii_template.kt
        [class]{}-[func]{isSolution}\n\n[class]{}-[func]{recordSolution}\n\n[class]{}-[func]{isValid}\n\n[class]{}-[func]{makeChoice}\n\n[class]{}-[func]{undoChoice}\n\n[class]{}-[func]{backtrack}\n
        preorder_traversal_iii_template.rb
        [class]{}-[func]{is_solution}\n\n[class]{}-[func]{record_solution}\n\n[class]{}-[func]{is_valid}\n\n[class]{}-[func]{make_choice}\n\n[class]{}-[func]{undo_choice}\n\n[class]{}-[func]{backtrack}\n

        \u554f\u984c\u6587\u306e\u610f\u5473\u306b\u5f93\u3044\u3001\u5024\u304c \\(7\\) \u306e\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u305f\u5f8c\u3082\u63a2\u7d22\u3092\u7d9a\u3051\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u89e3\u3092\u8a18\u9332\u3057\u305f\u5f8c\u306e return \u6587\u3092\u524a\u9664\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u6b21\u306e\u56f3\u306f\u3001return \u6587\u3092\u4fdd\u6301\u3059\u308b\u5834\u5408\u3068\u524a\u9664\u3059\u308b\u5834\u5408\u306e\u63a2\u7d22\u904e\u7a0b\u306e\u6bd4\u8f03\u3067\u3059\u3002

        \u56f3 13-4 \u00a0 return\u3092\u4fdd\u6301\u3059\u308b\u5834\u5408\u3068\u524a\u9664\u3059\u308b\u5834\u5408\u306e\u63a2\u7d22\u904e\u7a0b\u306e\u6bd4\u8f03

        \u524d\u9806\u8d70\u67fb\u306b\u57fa\u3065\u304f\u30b3\u30fc\u30c9\u5b9f\u88c5\u3068\u6bd4\u3079\u308b\u3068\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306b\u57fa\u3065\u304f\u5b9f\u88c5\u306f\u3084\u3084\u5197\u9577\u306b\u898b\u3048\u307e\u3059\u304c\u3001\u6c4e\u7528\u6027\u306f\u3088\u308a\u9ad8\u3044\u3067\u3059\u3002\u5b9f\u969b\u3001\u591a\u304f\u306e\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u554f\u984c\u306f\u3053\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306e\u4e0b\u3067\u89e3\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u5177\u4f53\u7684\u306a\u554f\u984c\u306b\u5fdc\u3058\u3066 state \u3068 choices \u3092\u5b9a\u7fa9\u3057\u3001\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u5185\u306e\u5404\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u88c5\u3059\u308c\u3070\u3088\u3044\u306e\u3067\u3059\u3002

        "},{"location":"chapter_backtracking/backtracking_algorithm/#1314","title":"13.1.4 \u00a0 \u3088\u304f\u4f7f\u308f\u308c\u308b\u7528\u8a9e","text":"

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u3092\u3088\u308a\u660e\u78ba\u306b\u5206\u6790\u3059\u308b\u305f\u3081\u306b\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3088\u304f\u4f7f\u308f\u308c\u308b\u7528\u8a9e\u306e\u610f\u5473\u3092\u307e\u3068\u3081\u3001\u4f8b\u984c 3 \u306e\u5bfe\u5fdc\u4f8b\u3092\u4ee5\u4e0b\u306e\u8868\u306b\u793a\u3057\u307e\u3059\u3002

        \u8868 13-1 \u00a0 \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3088\u304f\u4f7f\u308f\u308c\u308b\u7528\u8a9e

        \u540d\u79f0 \u5b9a\u7fa9 \u4f8b\u984c 3 \u89e3\uff08solution\uff09 \u89e3\u306f\u554f\u984c\u306e\u7279\u5b9a\u6761\u4ef6\u3092\u6e80\u305f\u3059\u7b54\u3048\u3067\u3042\u308a\u30011 \u3064\u307e\u305f\u306f\u8907\u6570\u5b58\u5728\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b \u6839\u30ce\u30fc\u30c9\u304b\u3089\u30ce\u30fc\u30c9 \\(7\\) \u307e\u3067\u306e\u5236\u7d04\u6761\u4ef6\u3092\u6e80\u305f\u3059\u3059\u3079\u3066\u306e\u7d4c\u8def \u5236\u7d04\u6761\u4ef6\uff08constraint\uff09 \u5236\u7d04\u6761\u4ef6\u306f\u3001\u89e3\u306e\u5b9f\u73fe\u53ef\u80fd\u6027\u3092\u5236\u9650\u3059\u308b\u6761\u4ef6\u3067\u3042\u308a\u3001\u901a\u5e38\u306f\u679d\u5208\u308a\u306b\u4f7f\u7528\u3055\u308c\u308b \u7d4c\u8def\u306b\u30ce\u30fc\u30c9 \\(3\\) \u3092\u542b\u307e\u306a\u3044 \u72b6\u614b\uff08state\uff09 \u72b6\u614b\u306f\u3001\u3042\u308b\u6642\u70b9\u3067\u306e\u554f\u984c\u306e\u72b6\u6cc1\u3092\u8868\u3057\u3001\u3053\u308c\u307e\u3067\u306b\u884c\u3063\u305f\u9078\u629e\u3092\u542b\u3080 \u73fe\u5728\u8a2a\u554f\u3057\u305f\u30ce\u30fc\u30c9\u7d4c\u8def\u3001\u3059\u306a\u308f\u3061 path \u30ce\u30fc\u30c9\u30ea\u30b9\u30c8 \u8a66\u884c\uff08attempt\uff09 \u8a66\u884c\u306f\u3001\u5229\u7528\u53ef\u80fd\u306a\u9078\u629e\u80a2\u306b\u57fa\u3065\u3044\u3066\u89e3\u7a7a\u9593\u3092\u63a2\u7d22\u3059\u308b\u904e\u7a0b\u3067\u3042\u308a\u3001\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\u3057\u3001\u89e3\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\u3059\u308b \u5de6\uff08\u53f3\uff09\u5b50\u30ce\u30fc\u30c9\u3092\u518d\u5e30\u7684\u306b\u8a2a\u554f\u3057\u3001\u30ce\u30fc\u30c9\u3092 path \u306b\u8ffd\u52a0\u3057\u3001\u30ce\u30fc\u30c9\u306e\u5024\u304c \\(7\\) \u304b\u3092\u78ba\u8a8d\u3059\u308b \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30af\uff08backtracking\uff09 \u5236\u7d04\u6761\u4ef6\u3092\u6e80\u305f\u3055\u306a\u3044\u72b6\u614b\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u4ee5\u524d\u306e\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3066\u524d\u306e\u72b6\u614b\u306b\u623b\u308b\u3053\u3068 \u8449\u30ce\u30fc\u30c9\u3092\u8d8a\u3048\u305f\u3068\u304d\u3001\u63a2\u7d22\u7d42\u4e86\u3001\u5024\u304c \\(3\\) \u306e\u30ce\u30fc\u30c9\u306b\u906d\u9047\u3057\u305f\u3068\u304d\u63a2\u7d22\u3092\u7d42\u4e86\u3057\u3001\u95a2\u6570\u304c\u623b\u308b \u679d\u5208\u308a\uff08pruning\uff09 \u554f\u984c\u306e\u7279\u6027\u3084\u5236\u7d04\u6761\u4ef6\u306b\u57fa\u3065\u304d\u3001\u7121\u610f\u5473\u306a\u63a2\u7d22\u7d4c\u8def\u3092\u907f\u3051\u308b\u65b9\u6cd5\u3067\u3042\u308a\u3001\u63a2\u7d22\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u308b \u5024\u304c \\(3\\) \u306e\u30ce\u30fc\u30c9\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u305d\u308c\u4ee5\u4e0a\u63a2\u7d22\u3057\u306a\u3044

        Tip

        \u554f\u984c\u3001\u89e3\u3001\u72b6\u614b\u306a\u3069\u306e\u6982\u5ff5\u306f\u4e00\u822c\u7684\u306a\u3082\u306e\u3067\u3042\u308a\u3001\u5206\u5272\u7d71\u6cbb\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3001\u52d5\u7684\u8a08\u753b\u6cd5\u3001\u8caa\u6b32\u6cd5\u306a\u3069\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u3082\u95a2\u4fc2\u3057\u307e\u3059\u3002

        "},{"location":"chapter_backtracking/backtracking_algorithm/#1315","title":"13.1.5 \u00a0 \u9577\u6240\u3068\u9650\u754c","text":"

        \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u672c\u8cea\u7684\u306b\u6df1\u3055\u512a\u5148\u63a2\u7d22\uff08DFS\uff09\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u4e00\u7a2e\u3067\u3042\u308a\u3001\u6761\u4ef6\u3092\u6e80\u305f\u3059\u89e3\u3092\u898b\u3064\u3051\u308b\u307e\u3067\u3042\u3089\u3086\u308b\u53ef\u80fd\u306a\u89e3\u3092\u8a66\u307f\u307e\u3059\u3002\u3053\u306e\u65b9\u6cd5\u306e\u5229\u70b9\u306f\u3001\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u89e3\u3092\u898b\u3064\u3051\u3089\u308c\u308b\u70b9\u3067\u3042\u308a\u3001\u9069\u5207\u306a\u679d\u5208\u308a\u3092\u884c\u3048\u3070\u52b9\u7387\u304c\u9ad8\u3044\u3053\u3068\u3067\u3059\u3002

        \u3057\u304b\u3057\u3001\u5927\u898f\u6a21\u307e\u305f\u306f\u8907\u96d1\u306a\u554f\u984c\u3092\u6271\u3046\u5834\u5408\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u52b9\u7387\u306f\u8a31\u5bb9\u3067\u304d\u306a\u3044\u307b\u3069\u4f4e\u4e0b\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        • \u6642\u9593\uff1a\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u901a\u5e38\u3001\u72b6\u614b\u7a7a\u9593\u306e\u3059\u3079\u3066\u306e\u53ef\u80fd\u6027\u3092\u63a2\u7d22\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\u6307\u6570\u30aa\u30fc\u30c0\u30fc\u307e\u305f\u306f\u968e\u4e57\u30aa\u30fc\u30c0\u30fc\u306b\u9054\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        • \u7a7a\u9593\uff1a\u518d\u5e30\u547c\u3073\u51fa\u3057\u4e2d\u306b\u73fe\u5728\u306e\u72b6\u614b\uff08\u4f8b\uff1a\u7d4c\u8def\u3001\u679d\u5208\u308a\u7528\u306e\u88dc\u52a9\u5909\u6570\u306a\u3069\uff09\u3092\u4fdd\u5b58\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u6df1\u3055\u304c\u5927\u304d\u3044\u5834\u5408\u3001\u7a7a\u9593\u306e\u4f7f\u7528\u91cf\u304c\u5897\u52a0\u3057\u307e\u3059\u3002

        \u305d\u308c\u3067\u3082\u306a\u304a\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u7279\u5b9a\u306e\u63a2\u7d22\u554f\u984c\u3084\u5236\u7d04\u6e80\u8db3\u554f\u984c\u306e\u6700\u826f\u306e\u89e3\u6cd5\u3067\u3042\u308b\u3053\u3068\u304c\u591a\u3044\u3067\u3059\u3002\u3053\u308c\u3089\u306e\u554f\u984c\u3067\u306f\u3001\u3069\u306e\u9078\u629e\u304c\u6709\u52b9\u306a\u89e3\u3092\u751f\u6210\u3059\u308b\u304b\u3092\u4e88\u6e2c\u3067\u304d\u306a\u3044\u305f\u3081\u3001\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u9078\u629e\u3092\u8a66\u3059\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u3088\u3046\u306a\u5834\u5408\u3001**\u52b9\u7387\u306e\u6700\u9069\u5316\u304c\u9375**\u3068\u306a\u308a\u307e\u3059\u3002\u4e00\u822c\u7684\u306a\u6700\u9069\u5316\u624b\u6cd5\u306f\u6b21\u306e 2 \u3064\u3067\u3059\u3002

        • \u679d\u5208\u308a\uff1a\u89e3\u3092\u751f\u6210\u3057\u306a\u3044\u3053\u3068\u304c\u78ba\u5b9f\u306a\u7d4c\u8def\u3092\u907f\u3051\u308b\u3053\u3068\u3067\u3001\u6642\u9593\u3068\u7a7a\u9593\u3092\u7bc0\u7d04\u3057\u307e\u3059\u3002
        • \u30d2\u30e5\u30fc\u30ea\u30b9\u30c6\u30a3\u30c3\u30af\u63a2\u7d22\uff1a\u63a2\u7d22\u4e2d\u306b\u6226\u7565\u3084\u8a55\u4fa1\u5024\u3092\u5c0e\u5165\u3057\u3001\u6709\u52b9\u306a\u89e3\u3092\u751f\u6210\u3059\u308b\u53ef\u80fd\u6027\u304c\u9ad8\u3044\u7d4c\u8def\u3092\u512a\u5148\u7684\u306b\u63a2\u7d22\u3057\u307e\u3059\u3002
        "},{"location":"chapter_backtracking/backtracking_algorithm/#1316","title":"13.1.6 \u00a0 \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306e\u5178\u578b\u7684\u306a\u4f8b\u984c","text":"

        \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u591a\u304f\u306e\u63a2\u7d22\u554f\u984c\u3001\u5236\u7d04\u6e80\u8db3\u554f\u984c\u3001\u7d44\u5408\u305b\u6700\u9069\u5316\u554f\u984c\u3092\u89e3\u304f\u306e\u306b\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002

        \u63a2\u7d22\u554f\u984c\uff1a\u3053\u306e\u7a2e\u306e\u554f\u984c\u306e\u76ee\u6a19\u306f\u3001\u7279\u5b9a\u306e\u6761\u4ef6\u3092\u6e80\u305f\u3059\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3067\u3059\u3002

        • \u5168\u9806\u5217\u554f\u984c\uff1a\u4e0e\u3048\u3089\u308c\u305f\u96c6\u5408\u306e\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u9806\u5217\u3092\u6c42\u3081\u308b\u3002
        • \u90e8\u5206\u548c\u554f\u984c\uff1a\u4e0e\u3048\u3089\u308c\u305f\u96c6\u5408\u3068\u76ee\u6a19\u548c\u306b\u5bfe\u3057\u3066\u3001\u548c\u304c\u76ee\u6a19\u5024\u306b\u306a\u308b\u3059\u3079\u3066\u306e\u90e8\u5206\u96c6\u5408\u3092\u6c42\u3081\u308b\u3002
        • \u30cf\u30ce\u30a4\u306e\u5854\uff1a3 \u672c\u306e\u67f1\u3068\u7570\u306a\u308b\u30b5\u30a4\u30ba\u306e\u5186\u76e4\u304c\u3042\u308a\u3001\u3059\u3079\u3066\u306e\u5186\u76e4\u3092 1 \u672c\u306e\u67f1\u304b\u3089\u5225\u306e\u67f1\u306b\u79fb\u3059\u30021 \u56de\u306b 1 \u679a\u3057\u304b\u52d5\u304b\u305b\u305a\u3001\u5927\u304d\u306a\u5186\u76e4\u3092\u5c0f\u3055\u3044\u5186\u76e4\u306e\u4e0a\u306b\u7f6e\u304f\u3053\u3068\u306f\u3067\u304d\u306a\u3044\u3002

        \u5236\u7d04\u6e80\u8db3\u554f\u984c\uff1a\u3053\u306e\u7a2e\u306e\u554f\u984c\u306e\u76ee\u6a19\u306f\u3001\u3059\u3079\u3066\u306e\u5236\u7d04\u6761\u4ef6\u3092\u6e80\u305f\u3059\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3067\u3059\u3002

        • \\(n\\) \u30af\u30a4\u30fc\u30f3\u554f\u984c\uff1a\\(n imes n\\) \u306e\u30c1\u30a7\u30b9\u76e4\u306b \\(n\\) \u500b\u306e\u30af\u30a4\u30fc\u30f3\u3092\u914d\u7f6e\u3057\u3001\u4e92\u3044\u306b\u653b\u6483\u3057\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u3002
        • \u6570\u72ec\uff1a\\(9 imes 9\\) \u306e\u30b0\u30ea\u30c3\u30c9\u306b\u6570\u5b57 \\(1\\) ~ \\(9\\) \u3092\u5165\u529b\u3057\u3001\u5404\u884c\u3001\u5217\u3001\\(3 imes 3\\) \u306e\u30b5\u30d6\u30b0\u30ea\u30c3\u30c9\u306b\u91cd\u8907\u304c\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u3002
        • \u30b0\u30e9\u30d5\u5f69\u8272\u554f\u984c\uff1a\u4e0e\u3048\u3089\u308c\u305f\u7121\u5411\u30b0\u30e9\u30d5\u306b\u5bfe\u3057\u3001\u96a3\u63a5\u9802\u70b9\u304c\u7570\u306a\u308b\u8272\u306b\u306a\u308b\u3088\u3046\u306b\u6700\u5c0f\u9650\u306e\u8272\u3067\u5f69\u8272\u3059\u308b\u3002

        \u7d44\u5408\u305b\u6700\u9069\u5316\u554f\u984c\uff1a\u3053\u306e\u7a2e\u306e\u554f\u984c\u306e\u76ee\u6a19\u306f\u3001\u7d44\u5408\u305b\u7a7a\u9593\u5185\u3067\u7279\u5b9a\u306e\u6761\u4ef6\u3092\u6e80\u305f\u3059\u6700\u9069\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3067\u3059\u3002

        • 0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\uff1a\u4e0e\u3048\u3089\u308c\u305f\u7269\u54c1\u7fa4\u3068\u30d0\u30c3\u30af\u30d1\u30c3\u30af\u304c\u3042\u308a\u3001\u5404\u7269\u54c1\u306b\u306f\u4fa1\u5024\u3068\u91cd\u3055\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u3002\u30d0\u30c3\u30af\u30d1\u30c3\u30af\u306e\u5bb9\u91cf\u5236\u9650\u5185\u3067\u3001\u7dcf\u4fa1\u5024\u3092\u6700\u5927\u5316\u3059\u308b\u7269\u54c1\u306e\u9078\u629e\u3092\u6c42\u3081\u308b\u3002
        • \u65c5\u884c\u30bb\u30fc\u30eb\u30b9\u30de\u30f3\u554f\u984c\uff1a\u30b0\u30e9\u30d5\u4e0a\u3067\u30011 \u3064\u306e\u70b9\u304b\u3089\u51fa\u767a\u3057\u3001\u3059\u3079\u3066\u306e\u4ed6\u306e\u70b9\u3092 1 \u56de\u305a\u3064\u8a2a\u554f\u3057\u3066\u51fa\u767a\u70b9\u306b\u623b\u308b\u6700\u77ed\u7d4c\u8def\u3092\u6c42\u3081\u308b\u3002
        • \u6700\u5927\u30af\u30ea\u30fc\u30af\u554f\u984c\uff1a\u4e0e\u3048\u3089\u308c\u305f\u7121\u5411\u30b0\u30e9\u30d5\u306e\u4e2d\u3067\u3001\u4efb\u610f\u306e 2 \u9802\u70b9\u9593\u306b\u8fba\u304c\u5b58\u5728\u3059\u308b\u6700\u5927\u306e\u5b8c\u5168\u90e8\u5206\u30b0\u30e9\u30d5\u3092\u898b\u3064\u3051\u308b\u3002

        \u6ce8\u610f\u3059\u3079\u304d\u306f\u3001\u591a\u304f\u306e\u7d44\u5408\u305b\u6700\u9069\u5316\u554f\u984c\u306b\u5bfe\u3057\u3066\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u304c\u6700\u9069\u89e3\u6cd5\u3067\u306f\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002

        • 0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306f\u3001\u6642\u9593\u52b9\u7387\u3092\u9ad8\u3081\u308b\u305f\u3081\u306b\u52d5\u7684\u8a08\u753b\u6cd5\u304c\u3088\u304f\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        • \u65c5\u884c\u30bb\u30fc\u30eb\u30b9\u30de\u30f3\u554f\u984c\u306f\u6709\u540d\u306a NP-Hard \u554f\u984c\u3067\u3042\u308a\u3001\u907a\u4f1d\u7684\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3084\u30a2\u30f3\u30c8\u30b3\u30ed\u30cb\u30fc\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306a\u3069\u306e\u624b\u6cd5\u304c\u3088\u304f\u4f7f\u308f\u308c\u307e\u3059\u3002
        • \u6700\u5927\u30af\u30ea\u30fc\u30af\u554f\u984c\u306f\u30b0\u30e9\u30d5\u7406\u8ad6\u306e\u53e4\u5178\u7684\u306a\u554f\u984c\u3067\u3042\u308a\u3001\u8caa\u6b32\u6cd5\u306a\u3069\u306e\u30d2\u30e5\u30fc\u30ea\u30b9\u30c6\u30a3\u30c3\u30af\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u89e3\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
        "},{"location":"chapter_backtracking/n_queens_problem/","title":"13.4 \u00a0 N\u30af\u30a4\u30fc\u30f3\u554f\u984c","text":"

        Question

        \u30c1\u30a7\u30b9\u306e\u30eb\u30fc\u30eb\u306b\u3088\u308b\u3068\u3001\u30af\u30a4\u30fc\u30f3\u306f\u540c\u3058\u884c\u3001\u5217\u3001\u307e\u305f\u306f\u5bfe\u89d2\u7dda\u4e0a\u306e\u99d2\u3092\u653b\u6483\u3067\u304d\u307e\u3059\u3002\\(n\\) \u500b\u306e\u30af\u30a4\u30fc\u30f3\u3068 \\(n \\times n\\) \u306e\u30c1\u30a7\u30b9\u30dc\u30fc\u30c9\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u30012\u3064\u306e\u30af\u30a4\u30fc\u30f3\u304c\u4e92\u3044\u306b\u653b\u6483\u3067\u304d\u306a\u3044\u914d\u7f6e\u3092\u898b\u3064\u3051\u3066\u304f\u3060\u3055\u3044\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\\(n = 4\\) \u306e\u5834\u5408\u30012\u3064\u306e\u89e3\u304c\u3042\u308a\u307e\u3059\u3002\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u89b3\u70b9\u304b\u3089\u3001\\(n \\times n\\) \u306e\u30c1\u30a7\u30b9\u30dc\u30fc\u30c9\u306b\u306f \\(n^2\\) \u500b\u306e\u30de\u30b9\u304c\u3042\u308a\u3001\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u9078\u629e\u80a2 choices \u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u30c1\u30a7\u30b9\u30dc\u30fc\u30c9\u306e\u72b6\u614b state \u306f\u3001\u5404\u30af\u30a4\u30fc\u30f3\u304c\u914d\u7f6e\u3055\u308c\u308b\u306b\u3064\u308c\u3066\u7d99\u7d9a\u7684\u306b\u5909\u5316\u3057\u307e\u3059\u3002

        \u56f3 13-15 \u00a0 4\u30af\u30a4\u30fc\u30f3\u554f\u984c\u306e\u89e3

        \u4ee5\u4e0b\u306e\u56f3\u306f\u3001\u3053\u306e\u554f\u984c\u306e3\u3064\u306e\u5236\u7d04\u3092\u793a\u3057\u3066\u3044\u307e\u3059\uff1a\u8907\u6570\u306e\u30af\u30a4\u30fc\u30f3\u306f\u540c\u3058\u884c\u3001\u5217\u3001\u307e\u305f\u306f\u5bfe\u89d2\u7dda\u3092\u5360\u6709\u3067\u304d\u307e\u305b\u3093\u3002\u5bfe\u89d2\u7dda\u306f\u4e3b\u5bfe\u89d2\u7dda \\ \u3068\u526f\u5bfe\u89d2\u7dda / \u306b\u5206\u304b\u308c\u308b\u3053\u3068\u306b\u6ce8\u610f\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002

        \u56f3 13-16 \u00a0 N\u30af\u30a4\u30fc\u30f3\u554f\u984c\u306e\u5236\u7d04

        "},{"location":"chapter_backtracking/n_queens_problem/#1","title":"1. \u00a0 \u884c\u3054\u3068\u306e\u914d\u7f6e\u6226\u7565","text":"

        \u30af\u30a4\u30fc\u30f3\u306e\u6570\u304c\u30c1\u30a7\u30b9\u30dc\u30fc\u30c9\u306e\u884c\u6570\u3068\u7b49\u3057\u304f\u3001\u3069\u3061\u3089\u3082 \\(n\\) \u3067\u3042\u308b\u305f\u3081\u3001**\u30c1\u30a7\u30b9\u30dc\u30fc\u30c9\u306e\u5404\u884c\u306b\u306f1\u3064\u306e\u30af\u30a4\u30fc\u30f3\u306e\u307f\u304c\u914d\u7f6e\u3067\u304d\u308b\u3053\u3068\u304c**\u5bb9\u6613\u306b\u7d50\u8ad6\u4ed8\u3051\u3089\u308c\u307e\u3059\u3002

        \u3053\u308c\u306f\u3001\u884c\u3054\u3068\u306e\u914d\u7f6e\u6226\u7565\u3092\u63a1\u7528\u3067\u304d\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\uff1a\u6700\u521d\u306e\u884c\u304b\u3089\u958b\u59cb\u3057\u3066\u3001\u6700\u5f8c\u306e\u884c\u306b\u5230\u9054\u3059\u308b\u307e\u3067\u884c\u3054\u3068\u306b1\u3064\u306e\u30af\u30a4\u30fc\u30f3\u3092\u914d\u7f6e\u3057\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306f\u30014\u30af\u30a4\u30fc\u30f3\u554f\u984c\u306e\u884c\u3054\u3068\u306e\u914d\u7f6e\u30d7\u30ed\u30bb\u30b9\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u30b9\u30da\u30fc\u30b9\u306e\u5236\u9650\u306b\u3088\u308a\u3001\u56f3\u306f\u6700\u521d\u306e\u884c\u306e1\u3064\u306e\u691c\u7d22\u5206\u5c90\u306e\u307f\u3092\u5c55\u958b\u3057\u3001\u5217\u3068\u5bfe\u89d2\u7dda\u306e\u5236\u7d04\u3092\u6e80\u305f\u3055\u306a\u3044\u914d\u7f6e\u3092\u526a\u5b9a\u3057\u307e\u3059\u3002

        \u56f3 13-17 \u00a0 \u884c\u3054\u3068\u306e\u914d\u7f6e\u6226\u7565

        \u672c\u8cea\u7684\u306b\u3001\u884c\u3054\u3068\u306e\u914d\u7f6e\u6226\u7565\u306f\u526a\u5b9a\u95a2\u6570\u3068\u3057\u3066\u6a5f\u80fd\u3057\u3001\u540c\u3058\u884c\u306b\u8907\u6570\u306e\u30af\u30a4\u30fc\u30f3\u3092\u914d\u7f6e\u3059\u308b\u3059\u3079\u3066\u306e\u691c\u7d22\u5206\u5c90\u3092\u9664\u53bb\u3057\u307e\u3059\u3002

        "},{"location":"chapter_backtracking/n_queens_problem/#2","title":"2. \u00a0 \u5217\u3068\u5bfe\u89d2\u7dda\u306e\u526a\u5b9a","text":"

        \u5217\u306e\u5236\u7d04\u3092\u6e80\u305f\u3059\u305f\u3081\u306b\u3001\u9577\u3055 \\(n\\) \u306e\u30d6\u30fc\u30eb\u914d\u5217 cols \u3092\u4f7f\u7528\u3057\u3066\u3001\u5404\u5217\u306b\u30af\u30a4\u30fc\u30f3\u304c\u5360\u6709\u3055\u308c\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3092\u8ffd\u8de1\u3067\u304d\u307e\u3059\u3002\u5404\u914d\u7f6e\u6c7a\u5b9a\u306e\u524d\u306b\u3001cols \u3092\u4f7f\u7528\u3057\u3066\u3059\u3067\u306b\u30af\u30a4\u30fc\u30f3\u304c\u3042\u308b\u5217\u3092\u526a\u5b9a\u3057\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u4e2d\u306b\u52d5\u7684\u306b\u66f4\u65b0\u3055\u308c\u307e\u3059\u3002

        Tip

        \u884c\u5217\u306e\u539f\u70b9\u306f\u5de6\u4e0a\u9685\u306b\u3042\u308a\u3001\u884c\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u4e0a\u304b\u3089\u4e0b\u306b\u5897\u52a0\u3057\u3001\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u5de6\u304b\u3089\u53f3\u306b\u5897\u52a0\u3059\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u5bfe\u89d2\u7dda\u306e\u5236\u7d04\u306f\u3069\u3046\u3067\u3057\u3087\u3046\u304b\uff1f\u30c1\u30a7\u30b9\u30dc\u30fc\u30c9\u4e0a\u306e\u7279\u5b9a\u306e\u30bb\u30eb\u306e\u884c\u3068\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092 \\((row, col)\\) \u3068\u3057\u307e\u3059\u3002\u7279\u5b9a\u306e\u4e3b\u5bfe\u89d2\u7dda\u3092\u9078\u629e\u3059\u308b\u3053\u3068\u3067\u3001\u305d\u306e\u5bfe\u89d2\u7dda\u4e0a\u306e\u3059\u3079\u3066\u306e\u30bb\u30eb\u3067\u5dee \\(row - col\\) \u304c\u540c\u3058\u3067\u3042\u308b\u3053\u3068\u306b\u6c17\u4ed8\u304d\u307e\u3059\u3002\u3064\u307e\u308a\u3001\\(row - col\\) \u306f\u4e3b\u5bfe\u89d2\u7dda\u4e0a\u3067\u5b9a\u6570\u5024\u3067\u3059\u3002

        \u8a00\u3044\u63db\u3048\u308b\u3068\u30012\u3064\u306e\u30bb\u30eb\u304c \\(row_1 - col_1 = row_2 - col_2\\) \u3092\u6e80\u305f\u3059\u5834\u5408\u3001\u305d\u308c\u3089\u306f\u78ba\u5b9f\u306b\u540c\u3058\u4e3b\u5bfe\u89d2\u7dda\u4e0a\u306b\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u30d1\u30bf\u30fc\u30f3\u3092\u4f7f\u7528\u3057\u3066\u3001\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u914d\u5217 diags1 \u3092\u5229\u7528\u3057\u3066\u3001\u30af\u30a4\u30fc\u30f3\u304c\u4e3b\u5bfe\u89d2\u7dda\u4e0a\u306b\u3042\u308b\u304b\u3069\u3046\u304b\u3092\u8ffd\u8de1\u3067\u304d\u307e\u3059\u3002

        \u540c\u69d8\u306b\u3001\\(row + col\\) \u306e\u548c\u306f\u526f\u5bfe\u89d2\u7dda\u4e0a\u306e\u3059\u3079\u3066\u306e\u30bb\u30eb\u3067\u5b9a\u6570\u5024\u3067\u3059\u3002\u914d\u5217 diags2 \u3092\u4f7f\u7528\u3057\u3066\u526f\u5bfe\u89d2\u7dda\u306e\u5236\u7d04\u3082\u51e6\u7406\u3067\u304d\u307e\u3059\u3002

        \u56f3 13-18 \u00a0 \u5217\u3068\u5bfe\u89d2\u7dda\u306e\u5236\u7d04\u306e\u51e6\u7406

        "},{"location":"chapter_backtracking/n_queens_problem/#3","title":"3. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        \\(n\\) \u6b21\u5143\u306e\u6b63\u65b9\u884c\u5217\u3067\u306f\u3001\\(row - col\\) \u306e\u7bc4\u56f2\u306f \\([-n + 1, n - 1]\\) \u3067\u3001\\(row + col\\) \u306e\u7bc4\u56f2\u306f \\([0, 2n - 2]\\) \u3067\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u4e3b\u5bfe\u89d2\u7dda\u3068\u526f\u5bfe\u89d2\u7dda\u306e\u6570\u306f\u3069\u3061\u3089\u3082 \\(2n - 1\\) \u3067\u3001\u914d\u5217 diags1 \u3068 diags2 \u306e\u9577\u3055\u306f \\(2n - 1\\) \u3067\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby n_queens.py
        def backtrack(\n    row: int,\n    n: int,\n    state: list[list[str]],\n    res: list[list[list[str]]],\n    cols: list[bool],\n    diags1: list[bool],\n    diags2: list[bool],\n):\n    \"\"\"\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1an \u30af\u30a4\u30fc\u30f3\"\"\"\n    # \u3059\u3079\u3066\u306e\u884c\u304c\u914d\u7f6e\u3055\u308c\u305f\u3089\u3001\u89e3\u3092\u8a18\u9332\n    if row == n:\n        res.append([list(row) for row in state])\n        return\n    # \u3059\u3079\u3066\u306e\u5217\u3092\u8d70\u67fb\n    for col in range(n):\n        # \u30bb\u30eb\u306b\u5bfe\u5fdc\u3059\u308b\u4e3b\u5bfe\u89d2\u7dda\u3068\u526f\u5bfe\u89d2\u7dda\u3092\u8a08\u7b97\n        diag1 = row - col + n - 1\n        diag2 = row + col\n        # \u679d\u5208\u308a\uff1a\u30bb\u30eb\u306e\u5217\u3001\u4e3b\u5bfe\u89d2\u7dda\u3001\u526f\u5bfe\u89d2\u7dda\u306b\u30af\u30a4\u30fc\u30f3\u3092\u914d\u7f6e\u3057\u306a\u3044\n        if not cols[col] and not diags1[diag1] and not diags2[diag2]:\n            # \u8a66\u884c\uff1a\u30bb\u30eb\u306b\u30af\u30a4\u30fc\u30f3\u3092\u914d\u7f6e\n            state[row][col] = \"Q\"\n            cols[col] = diags1[diag1] = diags2[diag2] = True\n            # \u6b21\u306e\u884c\u3092\u914d\u7f6e\n            backtrack(row + 1, n, state, res, cols, diags1, diags2)\n            # \u64a4\u56de\uff1a\u30bb\u30eb\u3092\u7a7a\u306e\u30b9\u30dd\u30c3\u30c8\u306b\u5fa9\u5143\n            state[row][col] = \"#\"\n            cols[col] = diags1[diag1] = diags2[diag2] = False\n\ndef n_queens(n: int) -> list[list[list[str]]]:\n    \"\"\"n \u30af\u30a4\u30fc\u30f3\u3092\u89e3\u304f\"\"\"\n    # n*n \u30b5\u30a4\u30ba\u306e\u30c1\u30a7\u30b9\u30dc\u30fc\u30c9\u3092\u521d\u671f\u5316\u3001'Q' \u306f\u30af\u30a4\u30fc\u30f3\u3092\u8868\u3057\u3001'#' \u306f\u7a7a\u306e\u30b9\u30dd\u30c3\u30c8\u3092\u8868\u3059\n    state = [[\"#\" for _ in range(n)] for _ in range(n)]\n    cols = [False] * n  # \u30af\u30a4\u30fc\u30f3\u304c\u3042\u308b\u5217\u3092\u8a18\u9332\n    diags1 = [False] * (2 * n - 1)  # \u30af\u30a4\u30fc\u30f3\u304c\u3042\u308b\u4e3b\u5bfe\u89d2\u7dda\u3092\u8a18\u9332\n    diags2 = [False] * (2 * n - 1)  # \u30af\u30a4\u30fc\u30f3\u304c\u3042\u308b\u526f\u5bfe\u89d2\u7dda\u3092\u8a18\u9332\n    res = []\n    backtrack(0, n, state, res, cols, diags1, diags2)\n\n    return res\n
        n_queens.cpp
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1an \u30af\u30a4\u30fc\u30f3 */\nvoid backtrack(int row, int n, vector<vector<string>> &state, vector<vector<vector<string>>> &res, vector<bool> &cols,\n               vector<bool> &diags1, vector<bool> &diags2) {\n    // \u3059\u3079\u3066\u306e\u884c\u304c\u914d\u7f6e\u3055\u308c\u305f\u3089\u3001\u89e3\u3092\u8a18\u9332\n    if (row == n) {\n        res.push_back(state);\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u5217\u3092\u8d70\u67fb\n    for (int col = 0; col < n; col++) {\n        // \u30bb\u30eb\u306b\u5bfe\u5fdc\u3059\u308b\u4e3b\u5bfe\u89d2\u7dda\u3068\u526f\u5bfe\u89d2\u7dda\u3092\u8a08\u7b97\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // \u526a\u5b9a\uff1a\u30bb\u30eb\u306e\u5217\u3001\u4e3b\u5bfe\u89d2\u7dda\u3001\u526f\u5bfe\u89d2\u7dda\u306b\u30af\u30a4\u30fc\u30f3\u3092\u914d\u7f6e\u3059\u308b\u3053\u3068\u3092\u8a31\u53ef\u3057\u306a\u3044\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u8a66\u884c\uff1a\u30bb\u30eb\u306b\u30af\u30a4\u30fc\u30f3\u3092\u914d\u7f6e\n            state[row][col] = \"Q\";\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // \u6b21\u306e\u884c\u3092\u914d\u7f6e\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // \u56de\u9000\uff1a\u30bb\u30eb\u3092\u7a7a\u306e\u30b9\u30dd\u30c3\u30c8\u306b\u5fa9\u5143\n            state[row][col] = \"#\";\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* n \u30af\u30a4\u30fc\u30f3\u3092\u89e3\u304f */\nvector<vector<vector<string>>> nQueens(int n) {\n    // n*n \u30b5\u30a4\u30ba\u306e\u30c1\u30a7\u30b9\u30dc\u30fc\u30c9\u3092\u521d\u671f\u5316\u3001'Q' \u306f\u30af\u30a4\u30fc\u30f3\u3092\u8868\u3057\u3001'#' \u306f\u7a7a\u306e\u30b9\u30dd\u30c3\u30c8\u3092\u8868\u3059\n    vector<vector<string>> state(n, vector<string>(n, \"#\"));\n    vector<bool> cols(n, false);           // \u30af\u30a4\u30fc\u30f3\u306e\u3042\u308b\u5217\u3092\u8a18\u9332\n    vector<bool> diags1(2 * n - 1, false); // \u30af\u30a4\u30fc\u30f3\u306e\u3042\u308b\u4e3b\u5bfe\u89d2\u7dda\u3092\u8a18\u9332\n    vector<bool> diags2(2 * n - 1, false); // \u30af\u30a4\u30fc\u30f3\u306e\u3042\u308b\u526f\u5bfe\u89d2\u7dda\u3092\u8a18\u9332\n    vector<vector<vector<string>>> res;\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n\n    return res;\n}\n
        n_queens.java
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1an \u30af\u30a4\u30fc\u30f3 */\nvoid backtrack(int row, int n, List<List<String>> state, List<List<List<String>>> res,\n        boolean[] cols, boolean[] diags1, boolean[] diags2) {\n    // \u3059\u3079\u3066\u306e\u884c\u304c\u914d\u7f6e\u3055\u308c\u305f\u3089\u3001\u89e3\u3092\u8a18\u9332\n    if (row == n) {\n        List<List<String>> copyState = new ArrayList<>();\n        for (List<String> sRow : state) {\n            copyState.add(new ArrayList<>(sRow));\n        }\n        res.add(copyState);\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u5217\u3092\u8d70\u67fb\n    for (int col = 0; col < n; col++) {\n        // \u30bb\u30eb\u306b\u5bfe\u5fdc\u3059\u308b\u4e3b\u5bfe\u89d2\u7dda\u3068\u526f\u5bfe\u89d2\u7dda\u3092\u8a08\u7b97\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // \u526a\u5b9a\uff1a\u30bb\u30eb\u306e\u5217\u3001\u4e3b\u5bfe\u89d2\u7dda\u3001\u526f\u5bfe\u89d2\u7dda\u306b\u30af\u30a4\u30fc\u30f3\u3092\u914d\u7f6e\u3059\u308b\u3053\u3068\u3092\u8a31\u53ef\u3057\u306a\u3044\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u8a66\u884c\uff1a\u30bb\u30eb\u306b\u30af\u30a4\u30fc\u30f3\u3092\u914d\u7f6e\n            state.get(row).set(col, \"Q\");\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // \u6b21\u306e\u884c\u3092\u914d\u7f6e\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // \u56de\u9000\uff1a\u30bb\u30eb\u3092\u7a7a\u306e\u30b9\u30dd\u30c3\u30c8\u306b\u5fa9\u5143\n            state.get(row).set(col, \"#\");\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* n \u30af\u30a4\u30fc\u30f3\u3092\u89e3\u304f */\nList<List<List<String>>> nQueens(int n) {\n    // n*n \u30b5\u30a4\u30ba\u306e\u30c1\u30a7\u30b9\u30dc\u30fc\u30c9\u3092\u521d\u671f\u5316\u3001'Q' \u306f\u30af\u30a4\u30fc\u30f3\u3092\u8868\u3057\u3001'#' \u306f\u7a7a\u306e\u30b9\u30dd\u30c3\u30c8\u3092\u8868\u3059\n    List<List<String>> state = new ArrayList<>();\n    for (int i = 0; i < n; i++) {\n        List<String> row = new ArrayList<>();\n        for (int j = 0; j < n; j++) {\n            row.add(\"#\");\n        }\n        state.add(row);\n    }\n    boolean[] cols = new boolean[n]; // \u30af\u30a4\u30fc\u30f3\u306e\u3042\u308b\u5217\u3092\u8a18\u9332\n    boolean[] diags1 = new boolean[2 * n - 1]; // \u30af\u30a4\u30fc\u30f3\u306e\u3042\u308b\u4e3b\u5bfe\u89d2\u7dda\u3092\u8a18\u9332\n    boolean[] diags2 = new boolean[2 * n - 1]; // \u30af\u30a4\u30fc\u30f3\u306e\u3042\u308b\u526f\u5bfe\u89d2\u7dda\u3092\u8a18\u9332\n    List<List<List<String>>> res = new ArrayList<>();\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n\n    return res;\n}\n
        n_queens.cs
        [class]{n_queens}-[func]{Backtrack}\n\n[class]{n_queens}-[func]{NQueens}\n
        n_queens.go
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{nQueens}\n
        n_queens.swift
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{nQueens}\n
        n_queens.js
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{nQueens}\n
        n_queens.ts
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{nQueens}\n
        n_queens.dart
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{nQueens}\n
        n_queens.rs
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{n_queens}\n
        n_queens.c
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{nQueens}\n
        n_queens.kt
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{nQueens}\n
        n_queens.rb
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{n_queens}\n

        \\(n\\) \u500b\u306e\u30af\u30a4\u30fc\u30f3\u3092\u884c\u3054\u3068\u306b\u914d\u7f6e\u3057\u3001\u5217\u306e\u5236\u7d04\u3092\u8003\u616e\u3057\u3066\u3001\u6700\u521d\u306e\u884c\u304b\u3089\u6700\u5f8c\u306e\u884c\u307e\u3067\u3001\\(n\\)\u3001\\(n-1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u306e\u9078\u629e\u80a2\u304c\u3042\u308a\u3001\\(O(n!)\\) \u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u89e3\u3092\u8a18\u9332\u3059\u308b\u969b\u3001\u884c\u5217 state \u3092\u30b3\u30d4\u30fc\u3057\u3066 res \u306b\u8ffd\u52a0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u30b3\u30d4\u30fc\u64cd\u4f5c\u306f \\(O(n^2)\\) \u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u5168\u4f53\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n! \\cdot n^2)\\) \u3067\u3059\u3002\u5b9f\u969b\u306b\u306f\u3001\u5bfe\u89d2\u7dda\u5236\u7d04\u306b\u57fa\u3065\u304f\u526a\u5b9a\u306b\u3088\u308a\u691c\u7d22\u7a7a\u9593\u3092\u5927\u5e45\u306b\u524a\u6e1b\u3067\u304d\u308b\u305f\u3081\u3001\u591a\u304f\u306e\u5834\u5408\u3001\u691c\u7d22\u52b9\u7387\u306f\u4e0a\u8a18\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3088\u308a\u3082\u512a\u308c\u3066\u3044\u307e\u3059\u3002

        \u914d\u5217 state \u306f \\(O(n^2)\\) \u7a7a\u9593\u3092\u4f7f\u7528\u3057\u3001\u914d\u5217 cols\u3001diags1\u3001diags2 \u306f\u305d\u308c\u305e\u308c \\(O(n)\\) \u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u6700\u5927\u518d\u5e30\u6df1\u5ea6\u306f \\(n\\) \u3067\u3001\\(O(n)\\) \u306e\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(n^2)\\) \u3067\u3059\u3002

        "},{"location":"chapter_backtracking/permutations_problem/","title":"13.2 \u00a0 \u9806\u5217\u554f\u984c","text":"

        \u9806\u5217\u554f\u984c\u306f\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5178\u578b\u7684\u306a\u5fdc\u7528\u3067\u3059\u3002\u3053\u308c\u306f\u3001\u914d\u5217\u3084\u6587\u5b57\u5217\u306a\u3069\u306e\u4e0e\u3048\u3089\u308c\u305f\u96c6\u5408\u304b\u3089\u8981\u7d20\u306e\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u914d\u7f6e\uff08\u9806\u5217\uff09\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3092\u542b\u307f\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u8868\u306f\u3001\u5165\u529b\u914d\u5217\u3068\u305d\u306e\u5bfe\u5fdc\u3059\u308b\u9806\u5217\u3092\u542b\u3080\u3044\u304f\u3064\u304b\u306e\u4f8b\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u8868 13-2 \u00a0 \u9806\u5217\u306e\u4f8b

        \u5165\u529b\u914d\u5217 \u9806\u5217 \\([1]\\) \\([1]\\) \\([1, 2]\\) \\([1, 2], [2, 1]\\) \\([1, 2, 3]\\) \\([1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]\\)"},{"location":"chapter_backtracking/permutations_problem/#1321","title":"13.2.1 \u00a0 \u91cd\u8907\u8981\u7d20\u304c\u306a\u3044\u5834\u5408","text":"

        Question

        \u91cd\u8907\u8981\u7d20\u306e\u306a\u3044\u6574\u6570\u914d\u5217\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u9806\u5217\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306e\u89b3\u70b9\u304b\u3089\u3001\u9806\u5217\u3092\u751f\u6210\u3059\u308b\u30d7\u30ed\u30bb\u30b9\u3092\u4e00\u9023\u306e\u9078\u629e\u3068\u3057\u3066\u898b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 \u5165\u529b\u914d\u5217\u304c \\([1, 2, 3]\\) \u3060\u3068\u3057\u307e\u3059\u3002\u6700\u521d\u306b \\(1\\) \u3092\u9078\u629e\u3057\u3001\u6b21\u306b \\(3\\)\u3001\u6700\u5f8c\u306b \\(2\\) \u3092\u9078\u629e\u3059\u308b\u3068\u3001\u9806\u5217 \\([1, 3, 2]\\) \u304c\u5f97\u3089\u308c\u307e\u3059\u3002\u300c\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u300d\u306f\u524d\u306e\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3066\u3001\u4ee3\u66ff\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u63a2\u7d22\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

        \u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306e\u89b3\u70b9\u304b\u3089\u3001\u5019\u88dc\u96c6\u5408 choices \u306f\u5165\u529b\u914d\u5217\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u3067\u69cb\u6210\u3055\u308c\u3001state \u306f\u3053\u308c\u307e\u3067\u306b\u9078\u629e\u3055\u308c\u305f\u8981\u7d20\u3092\u4fdd\u6301\u3057\u307e\u3059\u3002\u5404\u8981\u7d20\u306f\u4e00\u5ea6\u3060\u3051\u9078\u629e\u3067\u304d\u308b\u305f\u3081\u3001state \u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u306f\u4e00\u610f\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u3092\u518d\u5e30\u6728\u306b\u5c55\u958b\u3067\u304d\u307e\u3059\u3002\u5404\u30ce\u30fc\u30c9\u306f\u73fe\u5728\u306e state \u3092\u8868\u3057\u307e\u3059\u3002\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u304b\u3089\u958b\u59cb\u3057\u3066\u30013\u56de\u306e\u9078\u629e\u306e\u5f8c\u3001\u8449\u30ce\u30fc\u30c9\u306b\u5230\u9054\u3057\u307e\u3059\u2014\u305d\u308c\u305e\u308c\u304c\u9806\u5217\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002

        \u56f3 13-5 \u00a0 \u9806\u5217\u306e\u518d\u5e30\u6728

        "},{"location":"chapter_backtracking/permutations_problem/#1","title":"1. \u00a0 \u91cd\u8907\u9078\u629e\u306e\u526a\u5b9a","text":"

        \u5404\u8981\u7d20\u304c\u4e00\u5ea6\u3060\u3051\u9078\u629e\u3055\u308c\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3059\u308b\u305f\u3081\u306b\u3001\u30d6\u30fc\u30eb\u914d\u5217 selected \u3092\u5c0e\u5165\u3057\u307e\u3059\u3002\u3053\u3053\u3067 selected[i] \u306f choices[i] \u304c\u9078\u629e\u3055\u308c\u305f\u304b\u3069\u3046\u304b\u3092\u793a\u3057\u307e\u3059\u3002\u6b21\u306b\u3001\u3053\u306e\u914d\u5217\u306b\u57fa\u3065\u3044\u3066\u526a\u5b9a\u30b9\u30c6\u30c3\u30d7\u3092\u5b9f\u884c\u3057\u307e\u3059\uff1a

        • choice[i] \u3092\u9078\u629e\u3057\u305f\u5f8c\u3001selected[i] \u3092 \\(\\text{True}\\) \u306b\u8a2d\u5b9a\u3057\u3066\u9078\u629e\u3055\u308c\u305f\u3068\u30de\u30fc\u30af\u3057\u307e\u3059\u3002
        • choices \u3092\u53cd\u5fa9\u51e6\u7406\u3059\u308b\u969b\u3001\u9078\u629e\u3055\u308c\u305f\u3068\u30de\u30fc\u30af\u3055\u308c\u305f\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u30b9\u30ad\u30c3\u30d7\u3057\u307e\u3059\uff08\u3064\u307e\u308a\u3001\u305d\u308c\u3089\u306e\u5206\u5c90\u3092\u526a\u5b9a\u3057\u307e\u3059\uff09\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u6700\u521d\u306e\u30e9\u30a6\u30f3\u30c9\u30671\u3092\u9078\u629e\u3057\u30012\u756a\u76ee\u306e\u30e9\u30a6\u30f3\u30c9\u30673\u3092\u9078\u629e\u3057\u3001\u6700\u5f8c\u306e\u30e9\u30a6\u30f3\u30c9\u30672\u3092\u9078\u629e\u3059\u308b\u3068\u3057\u307e\u3059\u30022\u756a\u76ee\u306e\u30e9\u30a6\u30f3\u30c9\u3067\u8981\u7d201\u306e\u5206\u5c90\u3068\u30013\u756a\u76ee\u306e\u30e9\u30a6\u30f3\u30c9\u3067\u8981\u7d201\u30683\u306e\u5206\u5c90\u3092\u526a\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u56f3 13-6 \u00a0 \u9806\u5217\u306e\u526a\u5b9a\u4f8b

        \u56f3\u304b\u3089\u3001\u3053\u306e\u526a\u5b9a\u30d7\u30ed\u30bb\u30b9\u304c\u691c\u7d22\u7a7a\u9593\u3092 \\(O(n^n)\\) \u304b\u3089 \\(O(n!)\\) \u306b\u524a\u6e1b\u3059\u308b\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002

        "},{"location":"chapter_backtracking/permutations_problem/#2","title":"2. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        \u3053\u306e\u7406\u89e3\u306b\u3088\u308a\u3001\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u30b3\u30fc\u30c9\u306e\u300c\u7a7a\u6b04\u3092\u57cb\u3081\u308b\u300d\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u5168\u4f53\u306e\u30b3\u30fc\u30c9\u3092\u7c21\u6f54\u306b\u4fdd\u3064\u305f\u3081\u3001\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306e\u5404\u90e8\u5206\u3092\u500b\u5225\u306b\u5b9f\u88c5\u305b\u305a\u3001\u4ee3\u308f\u308a\u306b backtrack() \u95a2\u6570\u3067\u3059\u3079\u3066\u3092\u5c55\u958b\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby permutations_i.py
        def backtrack(\n    state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]\n):\n    \"\"\"\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u9806\u5217 I\"\"\"\n    # \u72b6\u614b\u306e\u9577\u3055\u304c\u8981\u7d20\u6570\u3068\u7b49\u3057\u3044\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\n    if len(state) == len(choices):\n        res.append(list(state))\n        return\n    # \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for i, choice in enumerate(choices):\n        # \u679d\u5208\u308a\uff1a\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3092\u8a31\u53ef\u3057\u306a\u3044\n        if not selected[i]:\n            # \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            selected[i] = True\n            state.append(choice)\n            # \u6b21\u306e\u9078\u629e\u30e9\u30a6\u30f3\u30c9\u306b\u9032\u3080\n            backtrack(state, choices, selected, res)\n            # \u64a4\u56de\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n            selected[i] = False\n            state.pop()\n\ndef permutations_i(nums: list[int]) -> list[list[int]]:\n    \"\"\"\u9806\u5217 I\"\"\"\n    res = []\n    backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\n    return res\n
        permutations_i.cpp
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u9806\u5217 I */\nvoid backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &selected, vector<vector<int>> &res) {\n    // \u72b6\u614b\u306e\u9577\u3055\u304c\u8981\u7d20\u6570\u3068\u7b49\u3057\u304f\u306a\u3063\u305f\u3089\u3001\u89e3\u3092\u8a18\u9332\n    if (state.size() == choices.size()) {\n        res.push_back(state);\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for (int i = 0; i < choices.size(); i++) {\n        int choice = choices[i];\n        // \u526a\u5b9a\uff1a\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3092\u8a31\u53ef\u3057\u306a\u3044\n        if (!selected[i]) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            selected[i] = true;\n            state.push_back(choice);\n            // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n            selected[i] = false;\n            state.pop_back();\n        }\n    }\n}\n\n/* \u9806\u5217 I */\nvector<vector<int>> permutationsI(vector<int> nums) {\n    vector<int> state;\n    vector<bool> selected(nums.size(), false);\n    vector<vector<int>> res;\n    backtrack(state, nums, selected, res);\n    return res;\n}\n
        permutations_i.java
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u9806\u5217 I */\nvoid backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {\n    // \u72b6\u614b\u306e\u9577\u3055\u304c\u8981\u7d20\u6570\u3068\u7b49\u3057\u304f\u306a\u3063\u305f\u3089\u3001\u89e3\u3092\u8a18\u9332\n    if (state.size() == choices.length) {\n        res.add(new ArrayList<Integer>(state));\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for (int i = 0; i < choices.length; i++) {\n        int choice = choices[i];\n        // \u526a\u5b9a\uff1a\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3092\u8a31\u53ef\u3057\u306a\u3044\n        if (!selected[i]) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            selected[i] = true;\n            state.add(choice);\n            // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n            selected[i] = false;\n            state.remove(state.size() - 1);\n        }\n    }\n}\n\n/* \u9806\u5217 I */\nList<List<Integer>> permutationsI(int[] nums) {\n    List<List<Integer>> res = new ArrayList<List<Integer>>();\n    backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);\n    return res;\n}\n
        permutations_i.cs
        [class]{permutations_i}-[func]{Backtrack}\n\n[class]{permutations_i}-[func]{PermutationsI}\n
        permutations_i.go
        [class]{}-[func]{backtrackI}\n\n[class]{}-[func]{permutationsI}\n
        permutations_i.swift
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsI}\n
        permutations_i.js
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsI}\n
        permutations_i.ts
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsI}\n
        permutations_i.dart
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsI}\n
        permutations_i.rs
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutations_i}\n
        permutations_i.c
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsI}\n
        permutations_i.kt
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsI}\n
        permutations_i.rb
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutations_i}\n
        "},{"location":"chapter_backtracking/permutations_problem/#1322","title":"13.2.2 \u00a0 \u91cd\u8907\u8981\u7d20\u3092\u8003\u616e\u3059\u308b\u5834\u5408","text":"

        Question

        **\u91cd\u8907\u8981\u7d20\u3092\u542b\u3080\u53ef\u80fd\u6027\u306e\u3042\u308b**\u6574\u6570\u914d\u5217\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u3059\u3079\u3066\u306e\u4e00\u610f\u306e\u9806\u5217\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u5165\u529b\u914d\u5217\u304c \\([1, 1, 2]\\) \u3060\u3068\u3057\u307e\u3059\u30022\u3064\u306e\u540c\u4e00\u8981\u7d20 \\(1\\) \u3092\u533a\u5225\u3059\u308b\u305f\u3081\u306b\u30012\u756a\u76ee\u3092 \\(\\hat{1}\\) \u3068\u30e9\u30d9\u30eb\u4ed8\u3051\u3057\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u3053\u306e\u65b9\u6cd5\u3067\u751f\u6210\u3055\u308c\u308b\u9806\u5217\u306e\u534a\u5206\u306f\u91cd\u8907\u3067\u3059\uff1a

        \u56f3 13-7 \u00a0 \u91cd\u8907\u9806\u5217

        \u3067\u306f\u3001\u3053\u308c\u3089\u306e\u91cd\u8907\u9806\u5217\u3092\u3069\u306e\u3088\u3046\u306b\u9664\u53bb\u3067\u304d\u308b\u3067\u3057\u3087\u3046\u304b\uff1f\u4e00\u3064\u306e\u76f4\u63a5\u7684\u306a\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\u3001\u3059\u3079\u3066\u306e\u9806\u5217\u3092\u751f\u6210\u3057\u305f\u5f8c\u306b\u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8\u3092\u4f7f\u7528\u3057\u3066\u91cd\u8907\u3092\u9664\u53bb\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u3053\u308c\u306f\u3042\u307e\u308a\u512a\u96c5\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u91cd\u8907\u3092\u751f\u6210\u3059\u308b\u5206\u5c90\u306f\u672c\u6765\u4e0d\u8981\u3067\u3042\u308a\u3001\u4e8b\u524d\u306b\u526a\u5b9a\u3055\u308c\u308b\u3079\u304d\u3060\u304b\u3089\u3067\u3059\u3001\u3053\u308c\u306b\u3088\u308a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u304c\u5411\u4e0a\u3057\u307e\u3059\u3002

        "},{"location":"chapter_backtracking/permutations_problem/#1_1","title":"1. \u00a0 \u7b49\u5024\u8981\u7d20\u306e\u526a\u5b9a","text":"

        \u4ee5\u4e0b\u306e\u56f3\u3092\u898b\u308b\u3068\u3001\u6700\u521d\u306e\u30e9\u30a6\u30f3\u30c9\u3067 \\(1\\) \u307e\u305f\u306f \\(\\hat{1}\\) \u3092\u9078\u629e\u3059\u308b\u3068\u540c\u3058\u9806\u5217\u306b\u3064\u306a\u304c\u308b\u305f\u3081\u3001\\(\\hat{1}\\) \u3092\u526a\u5b9a\u3057\u307e\u3059\u3002

        \u540c\u69d8\u306b\u3001\u6700\u521d\u306e\u30e9\u30a6\u30f3\u30c9\u3067 \\(2\\) \u3092\u9078\u629e\u3057\u305f\u5f8c\u30012\u756a\u76ee\u306e\u30e9\u30a6\u30f3\u30c9\u3067 \\(1\\) \u307e\u305f\u306f \\(\\hat{1}\\) \u3092\u9078\u629e\u3057\u3066\u3082\u91cd\u8907\u5206\u5c90\u306b\u3064\u306a\u304c\u308b\u305f\u3081\u3001\u305d\u306e\u6642\u3082 \\(\\hat{1}\\) \u3092\u526a\u5b9a\u3057\u307e\u3059\u3002

        \u672c\u8cea\u7684\u306b\u3001\u79c1\u305f\u3061\u306e\u76ee\u6a19\u306f\u3001\u8907\u6570\u306e\u540c\u4e00\u8981\u7d20\u304c\u9078\u629e\u306e\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u4e00\u5ea6\u3060\u3051\u9078\u629e\u3055\u308c\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3059\u308b\u3053\u3068\u3067\u3059\u3002

        \u56f3 13-8 \u00a0 \u91cd\u8907\u9806\u5217\u306e\u526a\u5b9a

        "},{"location":"chapter_backtracking/permutations_problem/#2_1","title":"2. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        \u524d\u306e\u554f\u984c\u306e\u30b3\u30fc\u30c9\u306b\u57fa\u3065\u3044\u3066\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8 duplicated \u3092\u5c0e\u5165\u3057\u307e\u3059\u3002\u3053\u306e\u30bb\u30c3\u30c8\u306f\u3001\u3059\u3067\u306b\u8a66\u884c\u3057\u305f\u8981\u7d20\u3092\u8ffd\u8de1\u3057\u3001\u91cd\u8907\u3092\u526a\u5b9a\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby permutations_ii.py
        def backtrack(\n    state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]\n):\n    \"\"\"\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u9806\u5217 II\"\"\"\n    # \u72b6\u614b\u306e\u9577\u3055\u304c\u8981\u7d20\u6570\u3068\u7b49\u3057\u3044\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\n    if len(state) == len(choices):\n        res.append(list(state))\n        return\n    # \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    duplicated = set[int]()\n    for i, choice in enumerate(choices):\n        # \u679d\u5208\u308a\uff1a\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3092\u8a31\u53ef\u305b\u305a\u3001\u7b49\u3057\u3044\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3082\u8a31\u53ef\u3057\u306a\u3044\n        if not selected[i] and choice not in duplicated:\n            # \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            duplicated.add(choice)  # \u9078\u629e\u3055\u308c\u305f\u8981\u7d20\u5024\u3092\u8a18\u9332\n            selected[i] = True\n            state.append(choice)\n            # \u6b21\u306e\u9078\u629e\u30e9\u30a6\u30f3\u30c9\u306b\u9032\u3080\n            backtrack(state, choices, selected, res)\n            # \u64a4\u56de\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n            selected[i] = False\n            state.pop()\n\ndef permutations_ii(nums: list[int]) -> list[list[int]]:\n    \"\"\"\u9806\u5217 II\"\"\"\n    res = []\n    backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\n    return res\n
        permutations_ii.cpp
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u9806\u5217 II */\nvoid backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &selected, vector<vector<int>> &res) {\n    // \u72b6\u614b\u306e\u9577\u3055\u304c\u8981\u7d20\u6570\u3068\u7b49\u3057\u304f\u306a\u3063\u305f\u3089\u3001\u89e3\u3092\u8a18\u9332\n    if (state.size() == choices.size()) {\n        res.push_back(state);\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    unordered_set<int> duplicated;\n    for (int i = 0; i < choices.size(); i++) {\n        int choice = choices[i];\n        // \u526a\u5b9a\uff1a\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3092\u8a31\u53ef\u305b\u305a\u3001\u7b49\u3057\u3044\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3082\u8a31\u53ef\u3057\u306a\u3044\n        if (!selected[i] && duplicated.find(choice) == duplicated.end()) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            duplicated.emplace(choice); // \u9078\u629e\u3055\u308c\u305f\u8981\u7d20\u5024\u3092\u8a18\u9332\n            selected[i] = true;\n            state.push_back(choice);\n            // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n            selected[i] = false;\n            state.pop_back();\n        }\n    }\n}\n\n/* \u9806\u5217 II */\nvector<vector<int>> permutationsII(vector<int> nums) {\n    vector<int> state;\n    vector<bool> selected(nums.size(), false);\n    vector<vector<int>> res;\n    backtrack(state, nums, selected, res);\n    return res;\n}\n
        permutations_ii.java
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u9806\u5217 II */\nvoid backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {\n    // \u72b6\u614b\u306e\u9577\u3055\u304c\u8981\u7d20\u6570\u3068\u7b49\u3057\u304f\u306a\u3063\u305f\u3089\u3001\u89e3\u3092\u8a18\u9332\n    if (state.size() == choices.length) {\n        res.add(new ArrayList<Integer>(state));\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    Set<Integer> duplicated = new HashSet<Integer>();\n    for (int i = 0; i < choices.length; i++) {\n        int choice = choices[i];\n        // \u526a\u5b9a\uff1a\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3092\u8a31\u53ef\u305b\u305a\u3001\u7b49\u3057\u3044\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3082\u8a31\u53ef\u3057\u306a\u3044\n        if (!selected[i] && !duplicated.contains(choice)) {\n            // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n            duplicated.add(choice); // \u9078\u629e\u3055\u308c\u305f\u8981\u7d20\u5024\u3092\u8a18\u9332\n            selected[i] = true;\n            state.add(choice);\n            // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n            selected[i] = false;\n            state.remove(state.size() - 1);\n        }\n    }\n}\n\n/* \u9806\u5217 II */\nList<List<Integer>> permutationsII(int[] nums) {\n    List<List<Integer>> res = new ArrayList<List<Integer>>();\n    backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);\n    return res;\n}\n
        permutations_ii.cs
        [class]{permutations_ii}-[func]{Backtrack}\n\n[class]{permutations_ii}-[func]{PermutationsII}\n
        permutations_ii.go
        [class]{}-[func]{backtrackII}\n\n[class]{}-[func]{permutationsII}\n
        permutations_ii.swift
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsII}\n
        permutations_ii.js
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsII}\n
        permutations_ii.ts
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsII}\n
        permutations_ii.dart
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsII}\n
        permutations_ii.rs
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutations_ii}\n
        permutations_ii.c
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsII}\n
        permutations_ii.kt
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsII}\n
        permutations_ii.rb
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutations_ii}\n

        \u3059\u3079\u3066\u306e\u8981\u7d20\u304c\u7570\u306a\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\\(n\\) \u500b\u306e\u8981\u7d20\u306e\u9806\u5217\u306f \\(n!\\) \uff08\u968e\u4e57\uff09\u500b\u3042\u308a\u307e\u3059\u3002\u5404\u7d50\u679c\u3092\u8a18\u9332\u3059\u308b\u306b\u306f\u9577\u3055 \\(n\\) \u306e\u30ea\u30b9\u30c8\u3092\u30b3\u30d4\u30fc\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u3053\u308c\u306b\u306f \\(O(n)\\) \u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u7dcf\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n!n)\\) \u3067\u3059\u3002

        \u6700\u5927\u518d\u5e30\u6df1\u5ea6\u306f \\(n\\) \u3067\u3001\\(O(n)\\) \u306e\u30b9\u30bf\u30c3\u30af\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002selected \u914d\u5217\u3082 \\(O(n)\\) \u7a7a\u9593\u304c\u5fc5\u8981\u3067\u3059\u3002\u4e00\u5ea6\u306b\u6700\u5927 \\(n\\) \u500b\u306e\u500b\u5225\u306e duplicated \u30bb\u30c3\u30c8\u304c\u5b58\u5728\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3001\u305d\u308c\u3089\u306f\u96c6\u5408\u7684\u306b \\(O(n^2)\\) \u7a7a\u9593\u3092\u5360\u6709\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(n^2)\\) \u3067\u3059\u3002

        "},{"location":"chapter_backtracking/permutations_problem/#3-2","title":"3. \u00a0 2\u3064\u306e\u526a\u5b9a\u65b9\u6cd5\u306e\u6bd4\u8f03","text":"

        selected \u3068 duplicated \u306f\u3069\u3061\u3089\u3082\u526a\u5b9a\u30e1\u30ab\u30cb\u30ba\u30e0\u3068\u3057\u3066\u6a5f\u80fd\u3057\u307e\u3059\u304c\u3001\u7570\u306a\u308b\u554f\u984c\u3092\u30bf\u30fc\u30b2\u30c3\u30c8\u306b\u3057\u3066\u3044\u307e\u3059\uff1a

        • \u91cd\u8907\u9078\u629e\u306e\u526a\u5b9a\uff08selected \u7d4c\u7531\uff09\uff1a\u691c\u7d22\u5168\u4f53\u306b\u5358\u4e00\u306e selected \u914d\u5217\u304c\u3042\u308a\u3001\u73fe\u5728\u306e\u72b6\u614b\u306b\u3059\u3067\u306b\u3042\u308b\u8981\u7d20\u3092\u793a\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u540c\u3058\u8981\u7d20\u304c state \u306b\u8907\u6570\u56de\u73fe\u308c\u308b\u3053\u3068\u3092\u9632\u304e\u307e\u3059\u3002
        • \u7b49\u5024\u8981\u7d20\u306e\u526a\u5b9a\uff08duplicated \u7d4c\u7531\uff09\uff1abacktrack \u95a2\u6570\u306e\u5404\u547c\u3073\u51fa\u3057\u306f\u72ec\u81ea\u306e duplicated \u30bb\u30c3\u30c8\u3092\u4f7f\u7528\u3057\u3001\u305d\u306e\u7279\u5b9a\u306e\u53cd\u5fa9\uff08for \u30eb\u30fc\u30d7\uff09\u3067\u3059\u3067\u306b\u9078\u629e\u3055\u308c\u305f\u8981\u7d20\u3092\u8a18\u9332\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u7b49\u3057\u3044\u8981\u7d20\u304c\u9078\u629e\u306e\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u4e00\u5ea6\u3060\u3051\u9078\u629e\u3055\u308c\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3057\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306f\u3001\u3053\u308c\u30892\u3064\u306e\u526a\u5b9a\u6226\u7565\u306e\u7bc4\u56f2\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u6728\u306e\u5404\u30ce\u30fc\u30c9\u306f\u9078\u629e\u3092\u8868\u3057\u307e\u3059\u3002\u30eb\u30fc\u30c8\u304b\u3089\u4efb\u610f\u306e\u8449\u3078\u306e\u7d4c\u8def\u306f\u30011\u3064\u306e\u5b8c\u5168\u306a\u9806\u5217\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002

        \u56f3 13-9 \u00a0 2\u3064\u306e\u526a\u5b9a\u6761\u4ef6\u306e\u7bc4\u56f2

        "},{"location":"chapter_backtracking/subset_sum_problem/","title":"13.3 \u00a0 \u90e8\u5206\u96c6\u5408\u548c\u554f\u984c","text":""},{"location":"chapter_backtracking/subset_sum_problem/#1331","title":"13.3.1 \u00a0 \u91cd\u8907\u8981\u7d20\u304c\u306a\u3044\u5834\u5408","text":"

        Question

        \u6b63\u306e\u6574\u6570\u306e\u914d\u5217 nums \u3068\u30bf\u30fc\u30b2\u30c3\u30c8\u6b63\u6574\u6570 target \u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u7d44\u307f\u5408\u308f\u305b\u5185\u306e\u8981\u7d20\u306e\u548c\u304c target \u306b\u7b49\u3057\u304f\u306a\u308b\u3088\u3046\u306a\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u7d44\u307f\u5408\u308f\u305b\u3092\u898b\u3064\u3051\u3066\u304f\u3060\u3055\u3044\u3002\u4e0e\u3048\u3089\u308c\u305f\u914d\u5217\u306b\u306f\u91cd\u8907\u8981\u7d20\u304c\u306a\u304f\u3001\u5404\u8981\u7d20\u306f\u8907\u6570\u56de\u9078\u629e\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u7d44\u307f\u5408\u308f\u305b\u3092\u91cd\u8907\u3059\u308b\u7d44\u307f\u5408\u308f\u305b\u3092\u542b\u307e\u306a\u3044\u30ea\u30b9\u30c8\u3068\u3057\u3066\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u4f8b\u3048\u3070\u3001\u5165\u529b\u96c6\u5408 \\(\\{3, 4, 5\\}\\) \u3068\u30bf\u30fc\u30b2\u30c3\u30c8\u6574\u6570 \\(9\\) \u306e\u5834\u5408\u3001\u89e3\u306f \\(\\{3, 3, 3\\}, \\{4, 5\\}\\) \u3067\u3059\u3002\u4ee5\u4e0b\u306e2\u70b9\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        • \u5165\u529b\u96c6\u5408\u306e\u8981\u7d20\u306f\u7121\u5236\u9650\u306b\u9078\u629e\u3067\u304d\u307e\u3059\u3002
        • \u90e8\u5206\u96c6\u5408\u306f\u8981\u7d20\u306e\u9806\u5e8f\u3092\u533a\u5225\u3057\u307e\u305b\u3093\u3002\u4f8b\u3048\u3070 \\(\\{4, 5\\}\\) \u3068 \\(\\{5, 4\\}\\) \u306f\u540c\u3058\u90e8\u5206\u96c6\u5408\u3067\u3059\u3002
        "},{"location":"chapter_backtracking/subset_sum_problem/#1","title":"1. \u00a0 \u9806\u5217\u89e3\u6cd5\u306e\u53c2\u8003","text":"

        \u9806\u5217\u554f\u984c\u3068\u540c\u69d8\u306b\u3001\u90e8\u5206\u96c6\u5408\u306e\u751f\u6210\u3092\u4e00\u9023\u306e\u9078\u629e\u3068\u3057\u3066\u60f3\u50cf\u3067\u304d\u3001\u9078\u629e\u30d7\u30ed\u30bb\u30b9\u4e2d\u306b\u300c\u8981\u7d20\u548c\u300d\u3092\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u3067\u66f4\u65b0\u3067\u304d\u307e\u3059\u3002\u8981\u7d20\u548c\u304c target \u306b\u7b49\u3057\u304f\u306a\u3063\u305f\u3068\u304d\u3001\u90e8\u5206\u96c6\u5408\u3092\u7d50\u679c\u30ea\u30b9\u30c8\u306b\u8a18\u9332\u3057\u307e\u3059\u3002

        \u9806\u5217\u554f\u984c\u3068\u306f\u7570\u306a\u308a\u3001\u3053\u306e\u554f\u984c\u3067\u306f\u8981\u7d20\u306f\u7121\u5236\u9650\u306b\u9078\u629e\u3067\u304d\u308b\u305f\u3081\u3001\u8981\u7d20\u304c\u9078\u629e\u3055\u308c\u305f\u304b\u3069\u3046\u304b\u3092\u8a18\u9332\u3059\u308b\u305f\u3081\u306e selected \u30d6\u30fc\u30eb\u914d\u5217\u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u305b\u3093\u3002\u9806\u5217\u30b3\u30fc\u30c9\u306b\u8efd\u5fae\u306a\u4fee\u6b63\u3092\u52a0\u3048\u3066\u3001\u6700\u521d\u306b\u554f\u984c\u3092\u89e3\u6c7a\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby subset_sum_i_naive.py
        def backtrack(\n    state: list[int],\n    target: int,\n    total: int,\n    choices: list[int],\n    res: list[list[int]],\n):\n    \"\"\"\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c I\"\"\"\n    # \u90e8\u5206\u96c6\u5408\u306e\u548c\u304c target \u3068\u7b49\u3057\u3044\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\n    if total == target:\n        res.append(list(state))\n        return\n    # \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for i in range(len(choices)):\n        # \u679d\u5208\u308a\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c\u304c target \u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u305d\u306e\u9078\u629e\u3092\u30b9\u30ad\u30c3\u30d7\n        if total + choices[i] > target:\n            continue\n        # \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u8981\u7d20\u3068 total \u3092\u66f4\u65b0\n        state.append(choices[i])\n        # \u6b21\u306e\u9078\u629e\u30e9\u30a6\u30f3\u30c9\u306b\u9032\u3080\n        backtrack(state, target, total + choices[i], choices, res)\n        # \u64a4\u56de\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n        state.pop()\n\ndef subset_sum_i_naive(nums: list[int], target: int) -> list[list[int]]:\n    \"\"\"\u90e8\u5206\u96c6\u5408\u306e\u548c I \u3092\u89e3\u304f\uff08\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u3092\u542b\u3080\uff09\"\"\"\n    state = []  # \u72b6\u614b\uff08\u90e8\u5206\u96c6\u5408\uff09\n    total = 0  # \u90e8\u5206\u96c6\u5408\u306e\u548c\n    res = []  # \u7d50\u679c\u30ea\u30b9\u30c8\uff08\u90e8\u5206\u96c6\u5408\u30ea\u30b9\u30c8\uff09\n    backtrack(state, target, total, nums, res)\n    return res\n
        subset_sum_i_naive.cpp
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u90e8\u5206\u96c6\u5408\u548c I */\nvoid backtrack(vector<int> &state, int target, int total, vector<int> &choices, vector<vector<int>> &res) {\n    // \u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3068\u7b49\u3057\u3044\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\n    if (total == target) {\n        res.push_back(state);\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for (int i = 0; i < choices.size(); i++) {\n        // \u526a\u5b9a\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u305d\u306e\u9078\u629e\u3092\u30b9\u30ad\u30c3\u30d7\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u8981\u7d20\u3068total\u3092\u66f4\u65b0\n        state.push_back(choices[i]);\n        // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n        backtrack(state, target, total + choices[i], choices, res);\n        // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n        state.pop_back();\n    }\n}\n\n/* \u90e8\u5206\u96c6\u5408\u548c I \u3092\u89e3\u304f\uff08\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u3092\u542b\u3080\uff09 */\nvector<vector<int>> subsetSumINaive(vector<int> nums, int target) {\n    vector<int> state;       // \u72b6\u614b\uff08\u90e8\u5206\u96c6\u5408\uff09\n    int total = 0;           // \u90e8\u5206\u96c6\u5408\u306e\u548c\n    vector<vector<int>> res; // \u7d50\u679c\u30ea\u30b9\u30c8\uff08\u90e8\u5206\u96c6\u5408\u30ea\u30b9\u30c8\uff09\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
        subset_sum_i_naive.java
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u90e8\u5206\u96c6\u5408\u548c I */\nvoid backtrack(List<Integer> state, int target, int total, int[] choices, List<List<Integer>> res) {\n    // \u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3068\u7b49\u3057\u3044\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\n    if (total == target) {\n        res.add(new ArrayList<>(state));\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for (int i = 0; i < choices.length; i++) {\n        // \u526a\u5b9a\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u305d\u306e\u9078\u629e\u3092\u30b9\u30ad\u30c3\u30d7\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u8981\u7d20\u3068total\u3092\u66f4\u65b0\n        state.add(choices[i]);\n        // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n        backtrack(state, target, total + choices[i], choices, res);\n        // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n        state.remove(state.size() - 1);\n    }\n}\n\n/* \u90e8\u5206\u96c6\u5408\u548c I \u3092\u89e3\u304f\uff08\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u3092\u542b\u3080\uff09 */\nList<List<Integer>> subsetSumINaive(int[] nums, int target) {\n    List<Integer> state = new ArrayList<>(); // \u72b6\u614b\uff08\u90e8\u5206\u96c6\u5408\uff09\n    int total = 0; // \u90e8\u5206\u96c6\u5408\u306e\u548c\n    List<List<Integer>> res = new ArrayList<>(); // \u7d50\u679c\u30ea\u30b9\u30c8\uff08\u90e8\u5206\u96c6\u5408\u30ea\u30b9\u30c8\uff09\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
        subset_sum_i_naive.cs
        [class]{subset_sum_i_naive}-[func]{Backtrack}\n\n[class]{subset_sum_i_naive}-[func]{SubsetSumINaive}\n
        subset_sum_i_naive.go
        [class]{}-[func]{backtrackSubsetSumINaive}\n\n[class]{}-[func]{subsetSumINaive}\n
        subset_sum_i_naive.swift
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumINaive}\n
        subset_sum_i_naive.js
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumINaive}\n
        subset_sum_i_naive.ts
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumINaive}\n
        subset_sum_i_naive.dart
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumINaive}\n
        subset_sum_i_naive.rs
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subset_sum_i_naive}\n
        subset_sum_i_naive.c
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumINaive}\n
        subset_sum_i_naive.kt
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumINaive}\n
        subset_sum_i_naive.rb
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subset_sum_i_naive}\n

        \u914d\u5217 \\([3, 4, 5]\\) \u3068\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20 \\(9\\) \u3092\u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u306b\u5165\u529b\u3059\u308b\u3068\u3001\u7d50\u679c \\([3, 3, 3], [4, 5], [5, 4]\\) \u304c\u5f97\u3089\u308c\u307e\u3059\u3002\u548c\u304c \\(9\\) \u306e\u3059\u3079\u3066\u306e\u90e8\u5206\u96c6\u5408\u3092\u6b63\u5e38\u306b\u898b\u3064\u3051\u307e\u3057\u305f\u304c\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408 \\([4, 5]\\) \u3068 \\([5, 4]\\) \u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002

        \u3053\u308c\u306f\u3001\u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u304c\u9078\u629e\u306e\u9806\u5e8f\u3092\u533a\u5225\u3059\u308b\u305f\u3081\u3067\u3059\u304c\u3001\u90e8\u5206\u96c6\u5408\u306f\u9078\u629e\u9806\u5e8f\u3092\u533a\u5225\u3057\u307e\u305b\u3093\u3002\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\\(5\\) \u306e\u524d\u306b \\(4\\) \u3092\u9078\u629e\u3059\u308b\u3053\u3068\u3068 \\(4\\) \u306e\u524d\u306b \\(5\\) \u3092\u9078\u629e\u3059\u308b\u3053\u3068\u306f\u7570\u306a\u308b\u5206\u5c90\u3067\u3059\u304c\u3001\u540c\u3058\u90e8\u5206\u96c6\u5408\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002

        \u56f3 13-10 \u00a0 \u90e8\u5206\u96c6\u5408\u306e\u691c\u7d22\u3068\u5883\u754c\u5916\u306e\u526a\u5b9a

        \u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u3092\u9664\u53bb\u3059\u308b\u305f\u3081\u306b\u3001\u76f4\u63a5\u7684\u306a\u30a2\u30a4\u30c7\u30a2\u306f\u7d50\u679c\u30ea\u30b9\u30c8\u3092\u91cd\u8907\u9664\u53bb\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u3053\u306e\u65b9\u6cd5\u306f2\u3064\u306e\u7406\u7531\u3067\u975e\u5e38\u306b\u975e\u52b9\u7387\u7684\u3067\u3059\u3002

        • \u914d\u5217\u8981\u7d20\u304c\u591a\u3044\u5834\u5408\u3001\u7279\u306b target \u304c\u5927\u304d\u3044\u5834\u5408\u3001\u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u3067\u5927\u91cf\u306e\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u304c\u751f\u6210\u3055\u308c\u307e\u3059\u3002
        • \u90e8\u5206\u96c6\u5408\uff08\u914d\u5217\uff09\u306e\u5dee\u7570\u3092\u6bd4\u8f03\u3059\u308b\u3053\u3068\u306f\u975e\u5e38\u306b\u6642\u9593\u304c\u304b\u304b\u308a\u3001\u307e\u305a\u914d\u5217\u3092\u30bd\u30fc\u30c8\u3057\u3001\u6b21\u306b\u914d\u5217\u306e\u5404\u8981\u7d20\u306e\u5dee\u7570\u3092\u6bd4\u8f03\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        "},{"location":"chapter_backtracking/subset_sum_problem/#2","title":"2. \u00a0 \u91cd\u8907\u90e8\u5206\u96c6\u5408\u306e\u526a\u5b9a","text":"

        \u526a\u5b9a\u3092\u901a\u3058\u3066\u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u4e2d\u306b\u91cd\u8907\u9664\u53bb\u3092\u691c\u8a0e\u3057\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\u7570\u306a\u308b\u9806\u5e8f\u3067\u914d\u5217\u8981\u7d20\u3092\u9078\u629e\u3059\u308b\u3068\u304d\u306b\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u304c\u751f\u6210\u3055\u308c\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u72b6\u6cc1\u3067\u3059\u3002

        1. \u6700\u521d\u306e\u30e9\u30a6\u30f3\u30c9\u3067 \\(3\\) \u3092\u9078\u629e\u3057\u30012\u756a\u76ee\u306e\u30e9\u30a6\u30f3\u30c9\u3067 \\(4\\) \u3092\u9078\u629e\u3059\u308b\u3068\u3001\u3053\u308c\u30892\u3064\u306e\u8981\u7d20\u3092\u542b\u3080\u3059\u3079\u3066\u306e\u90e8\u5206\u96c6\u5408\u304c\u751f\u6210\u3055\u308c\u3001\\([3, 4, \\dots]\\) \u3068\u8868\u8a18\u3055\u308c\u307e\u3059\u3002
        2. \u5f8c\u3067\u3001\u6700\u521d\u306e\u30e9\u30a6\u30f3\u30c9\u3067 \\(4\\) \u304c\u9078\u629e\u3055\u308c\u305f\u3068\u304d\u30012\u756a\u76ee\u306e\u30e9\u30a6\u30f3\u30c9\u306f \\(3\\) \u3092\u30b9\u30ad\u30c3\u30d7\u3059\u3079\u304d\u3067\u3059\u3002\u3053\u306e\u9078\u629e\u306b\u3088\u3063\u3066\u751f\u6210\u3055\u308c\u308b\u90e8\u5206\u96c6\u5408 \\([4, 3, \\dots]\\) \u306f\u30b9\u30c6\u30c3\u30d7 1. \u306e\u90e8\u5206\u96c6\u5408\u3068\u5b8c\u5168\u306b\u91cd\u8907\u3059\u308b\u304b\u3089\u3067\u3059\u3002

        \u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u3067\u306f\u3001\u5404\u5c64\u306e\u9078\u629e\u304c\u5de6\u304b\u3089\u53f3\u306b\u4e00\u3064\u305a\u3064\u8a66\u884c\u3055\u308c\u308b\u305f\u3081\u3001\u53f3\u5074\u306e\u5206\u5c90\u307b\u3069\u3088\u308a\u591a\u304f\u526a\u5b9a\u3055\u308c\u307e\u3059\u3002

        1. \u6700\u521d\u306e2\u30e9\u30a6\u30f3\u30c9\u3067 \\(3\\) \u3068 \\(5\\) \u3092\u9078\u629e\u3057\u3001\u90e8\u5206\u96c6\u5408 \\([3, 5, \\dots]\\) \u3092\u751f\u6210\u3057\u307e\u3059\u3002
        2. \u6700\u521d\u306e2\u30e9\u30a6\u30f3\u30c9\u3067 \\(4\\) \u3068 \\(5\\) \u3092\u9078\u629e\u3057\u3001\u90e8\u5206\u96c6\u5408 \\([4, 5, \\dots]\\) \u3092\u751f\u6210\u3057\u307e\u3059\u3002
        3. \u6700\u521d\u306e\u30e9\u30a6\u30f3\u30c9\u3067 \\(5\\) \u304c\u9078\u629e\u3055\u308c\u305f\u5834\u5408\u30012\u756a\u76ee\u306e\u30e9\u30a6\u30f3\u30c9\u306f \\(3\\) \u3068 \\(4\\) \u3092\u30b9\u30ad\u30c3\u30d7\u3059\u3079\u304d\u3067\u3059\u3002\u90e8\u5206\u96c6\u5408 \\([5, 3, \\dots]\\) \u3068 \\([5, 4, \\dots]\\) \u306f\u30b9\u30c6\u30c3\u30d7 1. \u3068 2. \u3067\u8a18\u8ff0\u3055\u308c\u305f\u90e8\u5206\u96c6\u5408\u3068\u5b8c\u5168\u306b\u91cd\u8907\u3059\u308b\u304b\u3089\u3067\u3059\u3002

        \u56f3 13-11 \u00a0 \u7570\u306a\u308b\u9078\u629e\u9806\u5e8f\u306b\u3088\u308b\u91cd\u8907\u90e8\u5206\u96c6\u5408

        \u8981\u7d04\u3059\u308b\u3068\u3001\u5165\u529b\u914d\u5217 \\([x_1, x_2, \\dots, x_n]\\) \u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u3067\u306e\u9078\u629e\u30b7\u30fc\u30b1\u30f3\u30b9\u306f \\([x_{i_1}, x_{i_2}, \\dots, x_{i_m}]\\) \u3067\u3042\u308b\u3079\u304d\u3067\u3001\\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\) \u3092\u6e80\u305f\u3059\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u6761\u4ef6\u3092\u6e80\u305f\u3055\u306a\u3044\u9078\u629e\u30b7\u30fc\u30b1\u30f3\u30b9\u306f\u91cd\u8907\u3092\u5f15\u304d\u8d77\u3053\u3057\u3001\u526a\u5b9a\u3055\u308c\u308b\u3079\u304d\u3067\u3059\u3002

        "},{"location":"chapter_backtracking/subset_sum_problem/#3","title":"3. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        \u3053\u306e\u526a\u5b9a\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306b\u3001\u5909\u6570 start \u3092\u521d\u671f\u5316\u3057\u3001\u3053\u308c\u306f\u8d70\u67fb\u306e\u958b\u59cb\u70b9\u3092\u793a\u3057\u307e\u3059\u3002\u9078\u629e \\(x_{i}\\) \u3092\u884c\u3063\u305f\u5f8c\u3001\u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u3092\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 \\(i\\) \u304b\u3089\u958b\u59cb\u3059\u308b\u3088\u3046\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u9078\u629e\u30b7\u30fc\u30b1\u30f3\u30b9\u304c \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\) \u3092\u6e80\u305f\u3059\u3053\u3068\u304c\u4fdd\u8a3c\u3055\u308c\u3001\u90e8\u5206\u96c6\u5408\u306e\u4e00\u610f\u6027\u304c\u4fdd\u8a3c\u3055\u308c\u307e\u3059\u3002

        \u3055\u3089\u306b\u3001\u30b3\u30fc\u30c9\u306b\u4ee5\u4e0b\u306e2\u3064\u306e\u6700\u9069\u5316\u3092\u884c\u3044\u307e\u3057\u305f\u3002

        • \u691c\u7d22\u3092\u958b\u59cb\u3059\u308b\u524d\u306b\u3001\u914d\u5217 nums \u3092\u30bd\u30fc\u30c8\u3057\u307e\u3059\u3002\u3059\u3079\u3066\u306e\u9078\u629e\u306e\u8d70\u67fb\u3067\u3001\u90e8\u5206\u96c6\u5408\u548c\u304c target \u3092\u8d85\u3048\u305f\u3068\u304d\u306b\u30eb\u30fc\u30d7\u3092\u76f4\u63a5\u7d42\u4e86\u3057\u307e\u3059\u3002\u5f8c\u7d9a\u306e\u8981\u7d20\u306f\u3088\u308a\u5927\u304d\u304f\u3001\u305d\u308c\u3089\u306e\u90e8\u5206\u96c6\u5408\u548c\u306f\u78ba\u5b9f\u306b target \u3092\u8d85\u3048\u308b\u304b\u3089\u3067\u3059\u3002
        • \u8981\u7d20\u548c\u5909\u6570 total \u3092\u9664\u53bb\u3057\u3001target \u306b\u5bfe\u3057\u3066\u6e1b\u7b97\u3092\u5b9f\u884c\u3057\u3066\u8981\u7d20\u548c\u3092\u30ab\u30a6\u30f3\u30c8\u3057\u307e\u3059\u3002target \u304c \\(0\\) \u306b\u7b49\u3057\u304f\u306a\u3063\u305f\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\u3057\u307e\u3059\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby subset_sum_i.py
        def backtrack(\n    state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n    \"\"\"\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c I\"\"\"\n    # \u90e8\u5206\u96c6\u5408\u306e\u548c\u304c target \u3068\u7b49\u3057\u3044\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\n    if target == 0:\n        res.append(list(state))\n        return\n    # \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    # \u679d\u5208\u308a\u4e8c\uff1astart \u304b\u3089\u8d70\u67fb\u3092\u958b\u59cb\u3057\u3066\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u306e\u751f\u6210\u3092\u907f\u3051\u308b\n    for i in range(start, len(choices)):\n        # \u679d\u5208\u308a\u4e00\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c\u304c target \u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u76f4\u3061\u306b\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\n        # \u3053\u308c\u306f\u914d\u5217\u304c\u30bd\u30fc\u30c8\u3055\u308c\u3066\u304a\u308a\u3001\u5f8c\u306e\u8981\u7d20\u304c\u3088\u308a\u5927\u304d\u3044\u305f\u3081\u3001\u90e8\u5206\u96c6\u5408\u306e\u548c\u306f\u5fc5\u305a target \u3092\u8d85\u3048\u308b\u305f\u3081\n        if target - choices[i] < 0:\n            break\n        # \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001target\u3001start \u3092\u66f4\u65b0\n        state.append(choices[i])\n        # \u6b21\u306e\u9078\u629e\u30e9\u30a6\u30f3\u30c9\u306b\u9032\u3080\n        backtrack(state, target - choices[i], choices, i, res)\n        # \u64a4\u56de\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n        state.pop()\n\ndef subset_sum_i(nums: list[int], target: int) -> list[list[int]]:\n    \"\"\"\u90e8\u5206\u96c6\u5408\u306e\u548c I \u3092\u89e3\u304f\"\"\"\n    state = []  # \u72b6\u614b\uff08\u90e8\u5206\u96c6\u5408\uff09\n    nums.sort()  # nums \u3092\u30bd\u30fc\u30c8\n    start = 0  # \u8d70\u67fb\u306e\u958b\u59cb\u70b9\n    res = []  # \u7d50\u679c\u30ea\u30b9\u30c8\uff08\u90e8\u5206\u96c6\u5408\u30ea\u30b9\u30c8\uff09\n    backtrack(state, target, nums, start, res)\n    return res\n
        subset_sum_i.cpp
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u90e8\u5206\u96c6\u5408\u548c I */\nvoid backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {\n    // \u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3068\u7b49\u3057\u3044\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\n    if (target == 0) {\n        res.push_back(state);\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    // \u526a\u5b9a\u4e8c\uff1astart\u304b\u3089\u8d70\u67fb\u3092\u958b\u59cb\u3057\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u306e\u751f\u6210\u3092\u56de\u907f\n    for (int i = start; i < choices.size(); i++) {\n        // \u526a\u5b9a\u4e00\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u5373\u5ea7\u306b\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\n        // \u914d\u5217\u304c\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u5f8c\u306e\u8981\u7d20\u306f\u3055\u3089\u306b\u5927\u304d\u304f\u3001\u90e8\u5206\u96c6\u5408\u306e\u548c\u306f\u5fc5\u305atarget\u3092\u8d85\u3048\u308b\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001target\u3001start\u3092\u66f4\u65b0\n        state.push_back(choices[i]);\n        // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n        backtrack(state, target - choices[i], choices, i, res);\n        // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n        state.pop_back();\n    }\n}\n\n/* \u90e8\u5206\u96c6\u5408\u548c I \u3092\u89e3\u304f */\nvector<vector<int>> subsetSumI(vector<int> nums, int target) {\n    vector<int> state;              // \u72b6\u614b\uff08\u90e8\u5206\u96c6\u5408\uff09\n    sort(nums.begin(), nums.end()); // nums \u3092\u30bd\u30fc\u30c8\n    int start = 0;                  // \u8d70\u67fb\u306e\u958b\u59cb\u70b9\n    vector<vector<int>> res;        // \u7d50\u679c\u30ea\u30b9\u30c8\uff08\u90e8\u5206\u96c6\u5408\u30ea\u30b9\u30c8\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_i.java
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u90e8\u5206\u96c6\u5408\u548c I */\nvoid backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {\n    // \u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3068\u7b49\u3057\u3044\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\n    if (target == 0) {\n        res.add(new ArrayList<>(state));\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    // \u526a\u5b9a\u4e8c\uff1astart\u304b\u3089\u8d70\u67fb\u3092\u958b\u59cb\u3057\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u306e\u751f\u6210\u3092\u56de\u907f\n    for (int i = start; i < choices.length; i++) {\n        // \u526a\u5b9a\u4e00\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u5373\u5ea7\u306b\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\n        // \u914d\u5217\u304c\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u5f8c\u306e\u8981\u7d20\u306f\u3055\u3089\u306b\u5927\u304d\u304f\u3001\u90e8\u5206\u96c6\u5408\u306e\u548c\u306f\u5fc5\u305atarget\u3092\u8d85\u3048\u308b\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001target\u3001start\u3092\u66f4\u65b0\n        state.add(choices[i]);\n        // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n        backtrack(state, target - choices[i], choices, i, res);\n        // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n        state.remove(state.size() - 1);\n    }\n}\n\n/* \u90e8\u5206\u96c6\u5408\u548c I \u3092\u89e3\u304f */\nList<List<Integer>> subsetSumI(int[] nums, int target) {\n    List<Integer> state = new ArrayList<>(); // \u72b6\u614b\uff08\u90e8\u5206\u96c6\u5408\uff09\n    Arrays.sort(nums); // nums \u3092\u30bd\u30fc\u30c8\n    int start = 0; // \u8d70\u67fb\u306e\u958b\u59cb\u70b9\n    List<List<Integer>> res = new ArrayList<>(); // \u7d50\u679c\u30ea\u30b9\u30c8\uff08\u90e8\u5206\u96c6\u5408\u30ea\u30b9\u30c8\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_i.cs
        [class]{subset_sum_i}-[func]{Backtrack}\n\n[class]{subset_sum_i}-[func]{SubsetSumI}\n
        subset_sum_i.go
        [class]{}-[func]{backtrackSubsetSumI}\n\n[class]{}-[func]{subsetSumI}\n
        subset_sum_i.swift
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumI}\n
        subset_sum_i.js
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumI}\n
        subset_sum_i.ts
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumI}\n
        subset_sum_i.dart
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumI}\n
        subset_sum_i.rs
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subset_sum_i}\n
        subset_sum_i.c
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumI}\n
        subset_sum_i.kt
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumI}\n
        subset_sum_i.rb
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subset_sum_i}\n

        \u4ee5\u4e0b\u306e\u56f3\u306f\u3001\u914d\u5217 \\([3, 4, 5]\\) \u3068\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20 \\(9\\) \u3092\u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u306b\u5165\u529b\u3057\u305f\u5f8c\u306e\u5168\u4f53\u7684\u306a\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 13-12 \u00a0 \u90e8\u5206\u96c6\u5408\u548c I \u306e\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9

        "},{"location":"chapter_backtracking/subset_sum_problem/#1332","title":"13.3.2 \u00a0 \u91cd\u8907\u8981\u7d20\u304c\u3042\u308b\u5834\u5408\u3092\u8003\u616e","text":"

        Question

        \u6b63\u306e\u6574\u6570\u306e\u914d\u5217 nums \u3068\u30bf\u30fc\u30b2\u30c3\u30c8\u6b63\u6574\u6570 target \u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u7d44\u307f\u5408\u308f\u305b\u5185\u306e\u8981\u7d20\u306e\u548c\u304c target \u306b\u7b49\u3057\u304f\u306a\u308b\u3088\u3046\u306a\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u7d44\u307f\u5408\u308f\u305b\u3092\u898b\u3064\u3051\u3066\u304f\u3060\u3055\u3044\u3002\u4e0e\u3048\u3089\u308c\u305f\u914d\u5217\u306b\u306f\u91cd\u8907\u8981\u7d20\u304c\u542b\u307e\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u5404\u8981\u7d20\u306f\u4e00\u5ea6\u3060\u3051\u9078\u629e\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u7d44\u307f\u5408\u308f\u305b\u3092\u91cd\u8907\u3059\u308b\u7d44\u307f\u5408\u308f\u305b\u3092\u542b\u307e\u306a\u3044\u30ea\u30b9\u30c8\u3068\u3057\u3066\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u524d\u306e\u554f\u984c\u3068\u6bd4\u8f03\u3057\u3066\u3001\u3053\u306e\u554f\u984c\u306e\u5165\u529b\u914d\u5217\u306b\u306f\u91cd\u8907\u8981\u7d20\u304c\u542b\u307e\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u65b0\u3057\u3044\u554f\u984c\u304c\u5c0e\u5165\u3055\u308c\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u914d\u5217 \\([4, \\hat{4}, 5]\\) \u3068\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20 \\(9\\) \u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u65e2\u5b58\u306e\u30b3\u30fc\u30c9\u306e\u51fa\u529b\u7d50\u679c\u306f \\([4, 5], [\\hat{4}, 5]\\) \u3068\u306a\u308a\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u304c\u751f\u6210\u3055\u308c\u307e\u3059\u3002

        \u3053\u306e\u91cd\u8907\u306e\u7406\u7531\u306f\u3001\u7279\u5b9a\u306e\u30e9\u30a6\u30f3\u30c9\u3067\u7b49\u3057\u3044\u8981\u7d20\u304c\u8907\u6570\u56de\u9078\u629e\u3055\u308c\u308b\u3053\u3068\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u3067\u306f\u3001\u6700\u521d\u306e\u30e9\u30a6\u30f3\u30c9\u306b3\u3064\u306e\u9078\u629e\u80a2\u304c\u3042\u308a\u3001\u305d\u306e\u3046\u30612\u3064\u304c \\(4\\) \u3067\u3042\u308a\u30012\u3064\u306e\u91cd\u8907\u3059\u308b\u691c\u7d22\u5206\u5c90\u3092\u751f\u6210\u3057\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u3092\u51fa\u529b\u3057\u307e\u3059\u3002\u540c\u69d8\u306b\u30012\u756a\u76ee\u306e\u30e9\u30a6\u30f3\u30c9\u306e2\u3064\u306e \\(4\\) \u3082\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u3092\u751f\u6210\u3057\u307e\u3059\u3002

        \u56f3 13-13 \u00a0 \u7b49\u3057\u3044\u8981\u7d20\u306b\u3088\u308b\u91cd\u8907\u90e8\u5206\u96c6\u5408

        "},{"location":"chapter_backtracking/subset_sum_problem/#1_1","title":"1. \u00a0 \u7b49\u5024\u8981\u7d20\u306e\u526a\u5b9a","text":"

        \u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3001\u7b49\u3057\u3044\u8981\u7d20\u304c\u30e9\u30a6\u30f3\u30c9\u3054\u3068\u306b\u4e00\u5ea6\u3060\u3051\u9078\u629e\u3055\u308c\u308b\u3088\u3046\u306b\u5236\u9650\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u5b9f\u88c5\u306f\u975e\u5e38\u306b\u5de7\u5999\u3067\u3059\uff1a\u914d\u5217\u304c\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u7b49\u3057\u3044\u8981\u7d20\u306f\u96a3\u63a5\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u7279\u5b9a\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u3067\u3001\u73fe\u5728\u306e\u8981\u7d20\u304c\u305d\u306e\u5de6\u5074\u306e\u8981\u7d20\u3068\u7b49\u3057\u3044\u5834\u5408\u3001\u305d\u308c\u306f\u3059\u3067\u306b\u9078\u629e\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u305f\u3081\u3001\u73fe\u5728\u306e\u8981\u7d20\u3092\u76f4\u63a5\u30b9\u30ad\u30c3\u30d7\u3057\u307e\u3059\u3002

        \u540c\u6642\u306b\u3001\u3053\u306e\u554f\u984c\u3067\u306f\u5404\u914d\u5217\u8981\u7d20\u306f\u4e00\u5ea6\u3060\u3051\u9078\u629e\u3067\u304d\u308b\u3068\u898f\u5b9a\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u5e78\u3044\u3001\u5909\u6570 start \u3092\u4f7f\u7528\u3057\u3066\u3053\u306e\u5236\u7d04\u3082\u6e80\u305f\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a\u9078\u629e \\(x_{i}\\) \u3092\u884c\u3063\u305f\u5f8c\u3001\u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u3092\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 \\(i + 1\\) \u304b\u3089\u524d\u65b9\u306b\u958b\u59cb\u3059\u308b\u3088\u3046\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u304c\u9664\u53bb\u3055\u308c\u308b\u3060\u3051\u3067\u306a\u304f\u3001\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3082\u56de\u907f\u3055\u308c\u307e\u3059\u3002

        "},{"location":"chapter_backtracking/subset_sum_problem/#2_1","title":"2. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby subset_sum_ii.py
        def backtrack(\n    state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n    \"\"\"\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c II\"\"\"\n    # \u90e8\u5206\u96c6\u5408\u306e\u548c\u304c target \u3068\u7b49\u3057\u3044\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\n    if target == 0:\n        res.append(list(state))\n        return\n    # \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    # \u679d\u5208\u308a\u4e8c\uff1astart \u304b\u3089\u8d70\u67fb\u3092\u958b\u59cb\u3057\u3066\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u306e\u751f\u6210\u3092\u907f\u3051\u308b\n    # \u679d\u5208\u308a\u4e09\uff1astart \u304b\u3089\u8d70\u67fb\u3092\u958b\u59cb\u3057\u3066\u540c\u3058\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3092\u907f\u3051\u308b\n    for i in range(start, len(choices)):\n        # \u679d\u5208\u308a\u4e00\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c\u304c target \u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u76f4\u3061\u306b\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\n        # \u3053\u308c\u306f\u914d\u5217\u304c\u30bd\u30fc\u30c8\u3055\u308c\u3066\u304a\u308a\u3001\u5f8c\u306e\u8981\u7d20\u304c\u3088\u308a\u5927\u304d\u3044\u305f\u3081\u3001\u90e8\u5206\u96c6\u5408\u306e\u548c\u306f\u5fc5\u305a target \u3092\u8d85\u3048\u308b\u305f\u3081\n        if target - choices[i] < 0:\n            break\n        # \u679d\u5208\u308a\u56db\uff1a\u8981\u7d20\u304c\u5de6\u306e\u8981\u7d20\u3068\u7b49\u3057\u3044\u5834\u5408\u3001\u691c\u7d22\u5206\u5c90\u304c\u91cd\u8907\u3057\u3066\u3044\u308b\u3053\u3068\u3092\u793a\u3059\u305f\u3081\u3001\u30b9\u30ad\u30c3\u30d7\n        if i > start and choices[i] == choices[i - 1]:\n            continue\n        # \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001target\u3001start \u3092\u66f4\u65b0\n        state.append(choices[i])\n        # \u6b21\u306e\u9078\u629e\u30e9\u30a6\u30f3\u30c9\u306b\u9032\u3080\n        backtrack(state, target - choices[i], choices, i + 1, res)\n        # \u64a4\u56de\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n        state.pop()\n\ndef subset_sum_ii(nums: list[int], target: int) -> list[list[int]]:\n    \"\"\"\u90e8\u5206\u96c6\u5408\u306e\u548c II \u3092\u89e3\u304f\"\"\"\n    state = []  # \u72b6\u614b\uff08\u90e8\u5206\u96c6\u5408\uff09\n    nums.sort()  # nums \u3092\u30bd\u30fc\u30c8\n    start = 0  # \u8d70\u67fb\u306e\u958b\u59cb\u70b9\n    res = []  # \u7d50\u679c\u30ea\u30b9\u30c8\uff08\u90e8\u5206\u96c6\u5408\u30ea\u30b9\u30c8\uff09\n    backtrack(state, target, nums, start, res)\n    return res\n
        subset_sum_ii.cpp
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u90e8\u5206\u96c6\u5408\u548c II */\nvoid backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {\n    // \u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3068\u7b49\u3057\u3044\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\n    if (target == 0) {\n        res.push_back(state);\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    // \u526a\u5b9a\u4e8c\uff1astart\u304b\u3089\u8d70\u67fb\u3092\u958b\u59cb\u3057\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u306e\u751f\u6210\u3092\u56de\u907f\n    // \u526a\u5b9a\u4e09\uff1astart\u304b\u3089\u8d70\u67fb\u3092\u958b\u59cb\u3057\u3001\u540c\u3058\u8981\u7d20\u306e\u7e70\u308a\u8fd4\u3057\u9078\u629e\u3092\u56de\u907f\n    for (int i = start; i < choices.size(); i++) {\n        // \u526a\u5b9a\u4e00\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u5373\u5ea7\u306b\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\n        // \u914d\u5217\u304c\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u5f8c\u306e\u8981\u7d20\u306f\u3055\u3089\u306b\u5927\u304d\u304f\u3001\u90e8\u5206\u96c6\u5408\u306e\u548c\u306f\u5fc5\u305atarget\u3092\u8d85\u3048\u308b\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u526a\u5b9a\u56db\uff1a\u8981\u7d20\u304c\u5de6\u306e\u8981\u7d20\u3068\u7b49\u3057\u3044\u5834\u5408\u3001\u691c\u7d22\u30d6\u30e9\u30f3\u30c1\u306e\u91cd\u8907\u3092\u793a\u3059\u306e\u3067\u30b9\u30ad\u30c3\u30d7\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001target\u3001start\u3092\u66f4\u65b0\n        state.push_back(choices[i]);\n        // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n        state.pop_back();\n    }\n}\n\n/* \u90e8\u5206\u96c6\u5408\u548c II \u3092\u89e3\u304f */\nvector<vector<int>> subsetSumII(vector<int> nums, int target) {\n    vector<int> state;              // \u72b6\u614b\uff08\u90e8\u5206\u96c6\u5408\uff09\n    sort(nums.begin(), nums.end()); // nums \u3092\u30bd\u30fc\u30c8\n    int start = 0;                  // \u8d70\u67fb\u306e\u958b\u59cb\u70b9\n    vector<vector<int>> res;        // \u7d50\u679c\u30ea\u30b9\u30c8\uff08\u90e8\u5206\u96c6\u5408\u30ea\u30b9\u30c8\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_ii.java
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u90e8\u5206\u96c6\u5408\u548c II */\nvoid backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {\n    // \u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3068\u7b49\u3057\u3044\u3068\u304d\u3001\u89e3\u3092\u8a18\u9332\n    if (target == 0) {\n        res.add(new ArrayList<>(state));\n        return;\n    }\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    // \u526a\u5b9a\u4e8c\uff1astart\u304b\u3089\u8d70\u67fb\u3092\u958b\u59cb\u3057\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u306e\u751f\u6210\u3092\u56de\u907f\n    // \u526a\u5b9a\u4e09\uff1astart\u304b\u3089\u8d70\u67fb\u3092\u958b\u59cb\u3057\u3001\u540c\u3058\u8981\u7d20\u306e\u7e70\u308a\u8fd4\u3057\u9078\u629e\u3092\u56de\u907f\n    for (int i = start; i < choices.length; i++) {\n        // \u526a\u5b9a\u4e00\uff1a\u90e8\u5206\u96c6\u5408\u306e\u548c\u304ctarget\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u5373\u5ea7\u306b\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\n        // \u914d\u5217\u304c\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u5f8c\u306e\u8981\u7d20\u306f\u3055\u3089\u306b\u5927\u304d\u304f\u3001\u90e8\u5206\u96c6\u5408\u306e\u548c\u306f\u5fc5\u305atarget\u3092\u8d85\u3048\u308b\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u526a\u5b9a\u56db\uff1a\u8981\u7d20\u304c\u5de6\u306e\u8981\u7d20\u3068\u7b49\u3057\u3044\u5834\u5408\u3001\u691c\u7d22\u30d6\u30e9\u30f3\u30c1\u306e\u91cd\u8907\u3092\u793a\u3059\u306e\u3067\u30b9\u30ad\u30c3\u30d7\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001target\u3001start\u3092\u66f4\u65b0\n        state.add(choices[i]);\n        // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u306b\u9032\u3080\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // \u56de\u9000\uff1a\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u3001\u524d\u306e\u72b6\u614b\u306b\u5fa9\u5143\n        state.remove(state.size() - 1);\n    }\n}\n\n/* \u90e8\u5206\u96c6\u5408\u548c II \u3092\u89e3\u304f */\nList<List<Integer>> subsetSumII(int[] nums, int target) {\n    List<Integer> state = new ArrayList<>(); // \u72b6\u614b\uff08\u90e8\u5206\u96c6\u5408\uff09\n    Arrays.sort(nums); // nums \u3092\u30bd\u30fc\u30c8\n    int start = 0; // \u8d70\u67fb\u306e\u958b\u59cb\u70b9\n    List<List<Integer>> res = new ArrayList<>(); // \u7d50\u679c\u30ea\u30b9\u30c8\uff08\u90e8\u5206\u96c6\u5408\u30ea\u30b9\u30c8\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_ii.cs
        [class]{subset_sum_ii}-[func]{Backtrack}\n\n[class]{subset_sum_ii}-[func]{SubsetSumII}\n
        subset_sum_ii.go
        [class]{}-[func]{backtrackSubsetSumII}\n\n[class]{}-[func]{subsetSumII}\n
        subset_sum_ii.swift
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumII}\n
        subset_sum_ii.js
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumII}\n
        subset_sum_ii.ts
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumII}\n
        subset_sum_ii.dart
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumII}\n
        subset_sum_ii.rs
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subset_sum_ii}\n
        subset_sum_ii.c
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumII}\n
        subset_sum_ii.kt
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumII}\n
        subset_sum_ii.rb
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{subset_sum_ii}\n

        \u4ee5\u4e0b\u306e\u56f3\u306f\u3001\u914d\u5217 \\([4, 4, 5]\\) \u3068\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20 \\(9\\) \u306e\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9\u3092\u793a\u3057\u30014\u7a2e\u985e\u306e\u526a\u5b9a\u64cd\u4f5c\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002\u56f3\u3068\u30b3\u30fc\u30c9\u306e\u30b3\u30e1\u30f3\u30c8\u3092\u7d44\u307f\u5408\u308f\u305b\u3066\u3001\u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u5168\u4f53\u3068\u5404\u7a2e\u985e\u306e\u526a\u5b9a\u64cd\u4f5c\u306e\u52d5\u4f5c\u3092\u7406\u89e3\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u56f3 13-14 \u00a0 \u90e8\u5206\u96c6\u5408\u548c II \u306e\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9

        "},{"location":"chapter_backtracking/summary/","title":"13.5 \u00a0 \u307e\u3068\u3081","text":""},{"location":"chapter_backtracking/summary/#1","title":"1. \u00a0 \u91cd\u8981\u306a\u5fa9\u7fd2","text":"
        • \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u672c\u8cea\u306f\u5168\u6570\u63a2\u7d22\u3067\u3059\u3002\u89e3\u7a7a\u9593\u306e\u6df1\u3055\u512a\u5148\u8d70\u67fb\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u3067\u6761\u4ef6\u3092\u6e80\u305f\u3059\u89e3\u3092\u6c42\u3081\u307e\u3059\u3002\u691c\u7d22\u4e2d\u306b\u6e80\u8db3\u306e\u3044\u304f\u89e3\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001\u305d\u308c\u3092\u8a18\u9332\u3057\u3001\u3059\u3079\u3066\u306e\u89e3\u304c\u898b\u3064\u304b\u308b\u304b\u8d70\u67fb\u304c\u5b8c\u4e86\u3059\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002
        • \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u306b\u306f\u8a66\u884c\u3068\u5f8c\u9000\u304c\u542b\u307e\u308c\u307e\u3059\u3002\u6df1\u3055\u512a\u5148\u63a2\u7d22\u3092\u4f7f\u7528\u3057\u3066\u69d8\u3005\u306a\u9078\u629e\u3092\u63a2\u7d22\u3057\u3001\u9078\u629e\u304c\u5236\u7d04\u3092\u6e80\u305f\u3055\u306a\u3044\u5834\u5408\u3001\u524d\u306e\u9078\u629e\u3092\u53d6\u308a\u6d88\u3057\u307e\u3059\u3002\u305d\u3057\u3066\u524d\u306e\u72b6\u614b\u306b\u623b\u3063\u3066\u4ed6\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u8a66\u3057\u7d9a\u3051\u307e\u3059\u3002\u8a66\u884c\u3068\u5f8c\u9000\u306f\u53cd\u5bfe\u65b9\u5411\u306e\u64cd\u4f5c\u3067\u3059\u3002
        • \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u554f\u984c\u306b\u306f\u901a\u5e38\u8907\u6570\u306e\u5236\u7d04\u304c\u542b\u307e\u308c\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u5236\u7d04\u306f\u526a\u5b9a\u64cd\u4f5c\u3092\u5b9f\u884c\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\u526a\u5b9a\u306f\u4e0d\u8981\u306a\u691c\u7d22\u5206\u5c90\u3092\u4e8b\u524d\u306b\u7d42\u4e86\u3057\u3001\u691c\u7d22\u52b9\u7387\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
        • \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u4e3b\u306b\u691c\u7d22\u554f\u984c\u3068\u5236\u7d04\u6e80\u8db3\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u7d44\u307f\u5408\u308f\u305b\u6700\u9069\u5316\u554f\u984c\u306f\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u3066\u89e3\u6c7a\u3067\u304d\u307e\u3059\u304c\u3001\u591a\u304f\u306e\u5834\u5408\u3001\u3088\u308a\u52b9\u7387\u7684\u307e\u305f\u306f\u52b9\u679c\u7684\u306a\u89e3\u6c7a\u65b9\u6cd5\u304c\u5229\u7528\u53ef\u80fd\u3067\u3059\u3002
        • \u9806\u5217\u554f\u984c\u306f\u3001\u4e0e\u3048\u3089\u308c\u305f\u96c6\u5408\u306e\u8981\u7d20\u306e\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u9806\u5217\u3092\u691c\u7d22\u3059\u308b\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u307e\u3059\u3002\u5404\u8981\u7d20\u304c\u9078\u629e\u3055\u308c\u305f\u304b\u3069\u3046\u304b\u3092\u8a18\u9332\u3059\u308b\u305f\u3081\u306b\u914d\u5217\u3092\u4f7f\u7528\u3057\u3001\u540c\u3058\u8981\u7d20\u306e\u91cd\u8907\u9078\u629e\u3092\u907f\u3051\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u5404\u8981\u7d20\u304c\u4e00\u5ea6\u3060\u3051\u9078\u629e\u3055\u308c\u308b\u3053\u3068\u304c\u4fdd\u8a3c\u3055\u308c\u307e\u3059\u3002
        • \u9806\u5217\u554f\u984c\u3067\u306f\u3001\u96c6\u5408\u306b\u91cd\u8907\u8981\u7d20\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u6700\u7d42\u7d50\u679c\u306b\u91cd\u8907\u9806\u5217\u304c\u542b\u307e\u308c\u307e\u3059\u3002\u540c\u4e00\u8981\u7d20\u304c\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u4e00\u5ea6\u3060\u3051\u9078\u629e\u3067\u304d\u308b\u3088\u3046\u306b\u5236\u9650\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u3053\u308c\u306f\u901a\u5e38\u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8\u3092\u4f7f\u7528\u3057\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002
        • \u90e8\u5206\u96c6\u5408\u548c\u554f\u984c\u306f\u3001\u4e0e\u3048\u3089\u308c\u305f\u96c6\u5408\u3067\u30bf\u30fc\u30b2\u30c3\u30c8\u5024\u306b\u5408\u8a08\u3059\u308b\u5168\u3066\u306e\u90e8\u5206\u96c6\u5408\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u307e\u3059\u3002\u96c6\u5408\u306f\u8981\u7d20\u306e\u9806\u5e8f\u3092\u533a\u5225\u3057\u307e\u305b\u3093\u304c\u3001\u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u3067\u306f\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u304c\u751f\u6210\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u7570\u306a\u308b\u8981\u7d20\u9806\u5e8f\u3092\u72ec\u7279\u306e\u30d1\u30b9\u3068\u3057\u3066\u63a2\u7d22\u3059\u308b\u305f\u3081\u306b\u767a\u751f\u3057\u307e\u3059\u3002\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306e\u524d\u306b\u3001\u30c7\u30fc\u30bf\u3092\u30bd\u30fc\u30c8\u3057\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u8d70\u67fb\u306e\u958b\u59cb\u70b9\u3092\u793a\u3059\u5909\u6570\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u96c6\u5408\u3092\u751f\u6210\u3059\u308b\u691c\u7d22\u5206\u5c90\u3092\u526a\u5b9a\u3067\u304d\u307e\u3059\u3002
        • \u90e8\u5206\u96c6\u5408\u548c\u554f\u984c\u3067\u306f\u3001\u914d\u5217\u5185\u306e\u7b49\u3057\u3044\u8981\u7d20\u306f\u91cd\u8907\u96c6\u5408\u3092\u751f\u6210\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u914d\u5217\u304c\u3059\u3067\u306b\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u308b\u3068\u3044\u3046\u524d\u63d0\u6761\u4ef6\u3092\u4f7f\u7528\u3057\u3066\u3001\u96a3\u63a5\u3059\u308b\u8981\u7d20\u304c\u7b49\u3057\u3044\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\u3059\u308b\u3053\u3068\u3067\u526a\u5b9a\u3092\u884c\u3044\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u7b49\u3057\u3044\u8981\u7d20\u304c\u30e9\u30a6\u30f3\u30c9\u3054\u3068\u306b\u4e00\u5ea6\u3060\u3051\u9078\u629e\u3055\u308c\u308b\u3053\u3068\u304c\u4fdd\u8a3c\u3055\u308c\u307e\u3059\u3002
        • \\(n\\) \u30af\u30a4\u30fc\u30f3\u554f\u984c\u306f\u30012\u3064\u306e\u30af\u30a4\u30fc\u30f3\u304c\u4e92\u3044\u306b\u653b\u6483\u3067\u304d\u306a\u3044\u3088\u3046\u306b \\(n \\times n\\) \u306e\u30c1\u30a7\u30b9\u30dc\u30fc\u30c9\u306b \\(n\\) \u500b\u306e\u30af\u30a4\u30fc\u30f3\u3092\u914d\u7f6e\u3059\u308b\u65b9\u6848\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u307e\u3059\u3002\u554f\u984c\u306e\u5236\u7d04\u306b\u306f\u884c\u5236\u7d04\u3001\u5217\u5236\u7d04\u3001\u304a\u3088\u3073\u4e3b\u5bfe\u89d2\u7dda\u3068\u526f\u5bfe\u89d2\u7dda\u306e\u5236\u7d04\u304c\u542b\u307e\u308c\u307e\u3059\u3002\u884c\u5236\u7d04\u3092\u6e80\u305f\u3059\u305f\u3081\u306b\u3001\u884c\u3054\u3068\u306b1\u3064\u306e\u30af\u30a4\u30fc\u30f3\u3092\u914d\u7f6e\u3059\u308b\u6226\u7565\u3092\u63a1\u7528\u3057\u3001\u5404\u884c\u306b1\u3064\u306e\u30af\u30a4\u30fc\u30f3\u304c\u914d\u7f6e\u3055\u308c\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3057\u307e\u3059\u3002
        • \u5217\u5236\u7d04\u3068\u5bfe\u89d2\u7dda\u5236\u7d04\u306e\u51e6\u7406\u306f\u4f3c\u3066\u3044\u307e\u3059\u3002\u5217\u5236\u7d04\u306b\u3064\u3044\u3066\u306f\u3001\u5404\u5217\u306b\u30af\u30a4\u30fc\u30f3\u304c\u3042\u308b\u304b\u3069\u3046\u304b\u3092\u8a18\u9332\u3059\u308b\u914d\u5217\u3092\u4f7f\u7528\u3057\u3001\u9078\u629e\u3055\u308c\u305f\u30bb\u30eb\u304c\u5408\u6cd5\u304b\u3069\u3046\u304b\u3092\u793a\u3057\u307e\u3059\u3002\u5bfe\u89d2\u7dda\u5236\u7d04\u306b\u3064\u3044\u3066\u306f\u30012\u3064\u306e\u914d\u5217\u3092\u4f7f\u7528\u3057\u3066\u4e3b\u5bfe\u89d2\u7dda\u3068\u526f\u5bfe\u89d2\u7dda\u306b\u305d\u308c\u305e\u308c\u30af\u30a4\u30fc\u30f3\u306e\u5b58\u5728\u3092\u8a18\u9332\u3057\u307e\u3059\u3002\u8ab2\u984c\u306f\u3001\u540c\u3058\u4e3b\u5bfe\u89d2\u7dda\u307e\u305f\u306f\u526f\u5bfe\u89d2\u7dda\u4e0a\u306e\u30bb\u30eb\u306e\u884c\u3068\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u9593\u306e\u95a2\u4fc2\u3092\u6c7a\u5b9a\u3059\u308b\u3053\u3068\u3067\u3059\u3002
        "},{"location":"chapter_backtracking/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q: \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3068\u518d\u5e30\u306e\u95a2\u4fc2\u3092\u3069\u306e\u3088\u3046\u306b\u7406\u89e3\u3059\u308c\u3070\u3088\u3044\u3067\u3059\u304b\uff1f

        \u5168\u4f53\u7684\u306b\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306f\u300c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u6226\u7565\u300d\u3067\u3042\u308a\u3001\u518d\u5e30\u306f\u3088\u308a\u300c\u30c4\u30fc\u30eb\u300d\u3067\u3059\u3002

        • \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u901a\u5e38\u518d\u5e30\u306b\u57fa\u3065\u3044\u3066\u3044\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306f\u518d\u5e30\u306e\u5fdc\u7528\u30b7\u30ca\u30ea\u30aa\u306e\u4e00\u3064\u3067\u3042\u308a\u3001\u7279\u306b\u691c\u7d22\u554f\u984c\u306b\u304a\u3044\u3066\u3067\u3059\u3002
        • \u518d\u5e30\u306e\u69cb\u9020\u306f\u300c\u90e8\u5206\u554f\u984c\u5206\u89e3\u300d\u306e\u554f\u984c\u89e3\u6c7a\u30d1\u30e9\u30c0\u30a4\u30e0\u3092\u53cd\u6620\u3057\u307e\u3059\u3002\u5206\u5272\u7d71\u6cbb\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\uff08\u30e1\u30e2\u5316\u518d\u5e30\uff09\u3092\u542b\u3080\u554f\u984c\u306e\u89e3\u6c7a\u3067\u3088\u304f\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        "},{"location":"chapter_computational_complexity/","title":"\u7b2c 2 \u7ae0 \u00a0 \u8907\u96d1\u5ea6\u89e3\u6790","text":"

        Abstract

        \u8907\u96d1\u5ea6\u89e3\u6790\u306f\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5e83\u5927\u306a\u5b87\u5b99\u306b\u304a\u3051\u308b\u6642\u7a7a\u306e\u30ca\u30d3\u30b2\u30fc\u30bf\u30fc\u306e\u3088\u3046\u306a\u3082\u306e\u3067\u3059\u3002

        \u6642\u9593\u3068\u7a7a\u9593\u306e\u6b21\u5143\u3092\u3088\u308a\u6df1\u304f\u63a2\u6c42\u3057\u3001\u3088\u308a\u512a\u96c5\u306a\u89e3\u6c7a\u7b56\u3092\u6c42\u3081\u308b\u305f\u3081\u306e\u30ac\u30a4\u30c9\u3068\u306a\u308a\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 2.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u52b9\u7387\u8a55\u4fa1
        • 2.2 \u00a0 \u53cd\u5fa9\u3068\u518d\u5e30
        • 2.3 \u00a0 \u6642\u9593\u8a08\u7b97\u91cf
        • 2.4 \u00a0 \u7a7a\u9593\u8a08\u7b97\u91cf
        • 2.5 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_computational_complexity/iteration_and_recursion/","title":"2.2 \u00a0 \u53cd\u5fa9\u3068\u518d\u5e30","text":"

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u304a\u3044\u3066\u3001\u30bf\u30b9\u30af\u306e\u7e70\u308a\u8fd4\u3057\u5b9f\u884c\u306f\u975e\u5e38\u306b\u4e00\u822c\u7684\u3067\u3042\u308a\u3001\u8907\u96d1\u5ea6\u306e\u5206\u6790\u3068\u5bc6\u63a5\u306b\u95a2\u4fc2\u3057\u3066\u3044\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u6642\u9593\u8a08\u7b97\u91cf\u3068\u7a7a\u9593\u8a08\u7b97\u91cf\u306e\u6982\u5ff5\u3092\u8a73\u3057\u304f\u5b66\u3076\u524d\u306b\u3001\u307e\u305a\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3067\u7e70\u308a\u8fd4\u3057\u30bf\u30b9\u30af\u3092\u5b9f\u88c5\u3059\u308b\u65b9\u6cd5\u3092\u63a2\u7a76\u3057\u307e\u3057\u3087\u3046\u3002\u3053\u308c\u306b\u306f\u30012\u3064\u306e\u57fa\u672c\u7684\u306a\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u5236\u5fa1\u69cb\u9020\u3067\u3042\u308b\u53cd\u5fa9\u3068\u518d\u5e30\u306e\u7406\u89e3\u304c\u542b\u307e\u308c\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#221","title":"2.2.1 \u00a0 \u53cd\u5fa9","text":"

        \u53cd\u5fa9\u306f\u3001\u30bf\u30b9\u30af\u3092\u7e70\u308a\u8fd4\u3057\u5b9f\u884c\u3059\u308b\u305f\u3081\u306e\u5236\u5fa1\u69cb\u9020\u3067\u3059\u3002\u53cd\u5fa9\u3067\u306f\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u7279\u5b9a\u306e\u6761\u4ef6\u304c\u6e80\u305f\u3055\u308c\u3066\u3044\u308b\u9650\u308a\u30b3\u30fc\u30c9\u30d6\u30ed\u30c3\u30af\u3092\u7e70\u308a\u8fd4\u3057\u5b9f\u884c\u3057\u3001\u3053\u306e\u6761\u4ef6\u304c\u6e80\u305f\u3055\u308c\u306a\u304f\u306a\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#1-for","title":"1. \u00a0 for\u30eb\u30fc\u30d7","text":"

        for\u30eb\u30fc\u30d7\u306f\u53cd\u5fa9\u306e\u6700\u3082\u4e00\u822c\u7684\u306a\u5f62\u5f0f\u306e1\u3064\u3067\u3042\u308a\u3001\u53cd\u5fa9\u56de\u6570\u304c\u4e8b\u524d\u306b\u5206\u304b\u3063\u3066\u3044\u308b\u5834\u5408\u306b\u7279\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u95a2\u6570\u306ffor\u30eb\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u3066\\(1 + 2 + \\dots + n\\)\u306e\u5408\u8a08\u3092\u5b9f\u884c\u3057\u3001\u5408\u8a08\u3092\u5909\u6570res\u306b\u683c\u7d0d\u3057\u307e\u3059\u3002Python\u3067\u306f\u3001range(a, b)\u306fa\u3092\u542b\u307fb\u3092\u9664\u304f\u533a\u9593\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3064\u307e\u308a\u3001\\(a\\)\u304b\u3089\\(b\u22121\\)\u307e\u3067\u306e\u7bc4\u56f2\u3067\u53cd\u5fa9\u3057\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
        def for_loop(n: int) -> int:\n    \"\"\"for\u30eb\u30fc\u30d7\"\"\"\n    res = 0\n    # 1, 2, ..., n-1, n \u306e\u5408\u8a08\u3092\u30eb\u30fc\u30d7\n    for i in range(1, n + 1):\n        res += i\n    return res\n
        iteration.cpp
        /* for \u30eb\u30fc\u30d7 */\nint forLoop(int n) {\n    int res = 0;\n    // 1, 2, ..., n-1, n \u306e\u5408\u8a08\u3092\u30eb\u30fc\u30d7\u8a08\u7b97\n    for (int i = 1; i <= n; ++i) {\n        res += i;\n    }\n    return res;\n}\n
        iteration.java
        /* for \u30eb\u30fc\u30d7 */\nint forLoop(int n) {\n    int res = 0;\n    // 1, 2, ..., n-1, n \u306e\u5408\u8a08\u3092\u30eb\u30fc\u30d7\u8a08\u7b97\n    for (int i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
        iteration.cs
        [class]{iteration}-[func]{ForLoop}\n
        iteration.go
        [class]{}-[func]{forLoop}\n
        iteration.swift
        [class]{}-[func]{forLoop}\n
        iteration.js
        [class]{}-[func]{forLoop}\n
        iteration.ts
        [class]{}-[func]{forLoop}\n
        iteration.dart
        [class]{}-[func]{forLoop}\n
        iteration.rs
        [class]{}-[func]{for_loop}\n
        iteration.c
        [class]{}-[func]{forLoop}\n
        iteration.kt
        [class]{}-[func]{forLoop}\n
        iteration.rb
        [class]{}-[func]{for_loop}\n

        \u4ee5\u4e0b\u306e\u56f3\u306f\u3053\u306e\u5408\u8a08\u95a2\u6570\u3092\u8868\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 2-1 \u00a0 Flowchart of the sum function

        \u3053\u306e\u5408\u8a08\u95a2\u6570\u3067\u306e\u64cd\u4f5c\u6570\u306f\u5165\u529b\u30c7\u30fc\u30bf\u306e\u30b5\u30a4\u30ba\\(n\\)\u306b\u6bd4\u4f8b\u3059\u308b\u3001\u3064\u307e\u308a\u7dda\u5f62\u95a2\u4fc2\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u300c\u7dda\u5f62\u95a2\u4fc2\u300d\u3053\u305d\u304c\u6642\u9593\u8a08\u7b97\u91cf\u304c\u8a18\u8ff0\u3059\u308b\u3082\u306e\u3067\u3059\u3002\u3053\u306e\u30c8\u30d4\u30c3\u30af\u306b\u3064\u3044\u3066\u306f\u6b21\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u8a73\u3057\u304f\u8aac\u660e\u3057\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#2-while","title":"2. \u00a0 while\u30eb\u30fc\u30d7","text":"

        for\u30eb\u30fc\u30d7\u3068\u540c\u69d8\u306b\u3001while\u30eb\u30fc\u30d7\u306f\u53cd\u5fa9\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306e\u3082\u30461\u3064\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u3067\u3059\u3002while\u30eb\u30fc\u30d7\u3067\u306f\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u5404\u53cd\u5fa9\u306e\u958b\u59cb\u6642\u306b\u6761\u4ef6\u3092\u30c1\u30a7\u30c3\u30af\u3057\u3001\u6761\u4ef6\u304c\u771f\u306e\u5834\u5408\u306f\u5b9f\u884c\u3092\u7d99\u7d9a\u3057\u3001\u305d\u3046\u3067\u306a\u3051\u308c\u3070\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\u3057\u307e\u3059\u3002

        \u4ee5\u4e0b\u3067\u306fwhile\u30eb\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u3066\u5408\u8a08\\(1 + 2 + \\dots + n\\)\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
        def while_loop(n: int) -> int:\n    \"\"\"while\u30eb\u30fc\u30d7\"\"\"\n    res = 0\n    i = 1  # \u6761\u4ef6\u5909\u6570\u3092\u521d\u671f\u5316\n    # 1, 2, ..., n-1, n \u306e\u5408\u8a08\u3092\u30eb\u30fc\u30d7\n    while i <= n:\n        res += i\n        i += 1  # \u6761\u4ef6\u5909\u6570\u3092\u66f4\u65b0\n    return res\n
        iteration.cpp
        /* while \u30eb\u30fc\u30d7 */\nint whileLoop(int n) {\n    int res = 0;\n    int i = 1; // \u6761\u4ef6\u5909\u6570\u3092\u521d\u671f\u5316\n    // 1, 2, ..., n-1, n \u306e\u5408\u8a08\u3092\u30eb\u30fc\u30d7\u8a08\u7b97\n    while (i <= n) {\n        res += i;\n        i++; // \u6761\u4ef6\u5909\u6570\u3092\u66f4\u65b0\n    }\n    return res;\n}\n
        iteration.java
        /* while \u30eb\u30fc\u30d7 */\nint whileLoop(int n) {\n    int res = 0;\n    int i = 1; // \u6761\u4ef6\u5909\u6570\u3092\u521d\u671f\u5316\n    // 1, 2, ..., n-1, n \u306e\u5408\u8a08\u3092\u30eb\u30fc\u30d7\u8a08\u7b97\n    while (i <= n) {\n        res += i;\n        i++; // \u6761\u4ef6\u5909\u6570\u3092\u66f4\u65b0\n    }\n    return res;\n}\n
        iteration.cs
        [class]{iteration}-[func]{WhileLoop}\n
        iteration.go
        [class]{}-[func]{whileLoop}\n
        iteration.swift
        [class]{}-[func]{whileLoop}\n
        iteration.js
        [class]{}-[func]{whileLoop}\n
        iteration.ts
        [class]{}-[func]{whileLoop}\n
        iteration.dart
        [class]{}-[func]{whileLoop}\n
        iteration.rs
        [class]{}-[func]{while_loop}\n
        iteration.c
        [class]{}-[func]{whileLoop}\n
        iteration.kt
        [class]{}-[func]{whileLoop}\n
        iteration.rb
        [class]{}-[func]{while_loop}\n

        while\u30eb\u30fc\u30d7\u306ffor\u30eb\u30fc\u30d7\u3088\u308a\u3082\u67d4\u8edf\u6027\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002\u7279\u306b\u3001\u6761\u4ef6\u5909\u6570\u306e\u30ab\u30b9\u30bf\u30e0\u521d\u671f\u5316\u3068\u5404\u30b9\u30c6\u30c3\u30d7\u3067\u306e\u5909\u66f4\u304c\u53ef\u80fd\u3067\u3059\u3002

        \u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3001\u6761\u4ef6\u5909\u6570\\(i\\)\u304c\u5404\u30e9\u30a6\u30f3\u30c9\u30672\u56de\u66f4\u65b0\u3055\u308c\u307e\u3059\u304c\u3001\u3053\u308c\u306ffor\u30eb\u30fc\u30d7\u3067\u306f\u5b9f\u88c5\u304c\u4e0d\u4fbf\u3067\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
        def while_loop_ii(n: int) -> int:\n    \"\"\"while\u30eb\u30fc\u30d7\uff082\u3064\u306e\u66f4\u65b0\uff09\"\"\"\n    res = 0\n    i = 1  # \u6761\u4ef6\u5909\u6570\u3092\u521d\u671f\u5316\n    # 1, 4, 10, ... \u306e\u5408\u8a08\u3092\u30eb\u30fc\u30d7\n    while i <= n:\n        res += i\n        # \u6761\u4ef6\u5909\u6570\u3092\u66f4\u65b0\n        i += 1\n        i *= 2\n    return res\n
        iteration.cpp
        /* while \u30eb\u30fc\u30d7\uff082\u3064\u306e\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n    int res = 0;\n    int i = 1; // \u6761\u4ef6\u5909\u6570\u3092\u521d\u671f\u5316\n    // 1, 4, 10, ... \u306e\u5408\u8a08\u3092\u30eb\u30fc\u30d7\u8a08\u7b97\n    while (i <= n) {\n        res += i;\n        // \u6761\u4ef6\u5909\u6570\u3092\u66f4\u65b0\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
        iteration.java
        /* while \u30eb\u30fc\u30d7\uff082\u3064\u306e\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n    int res = 0;\n    int i = 1; // \u6761\u4ef6\u5909\u6570\u3092\u521d\u671f\u5316\n    // 1, 4, 10, ... \u306e\u5408\u8a08\u3092\u30eb\u30fc\u30d7\u8a08\u7b97\n    while (i <= n) {\n        res += i;\n        // \u6761\u4ef6\u5909\u6570\u3092\u66f4\u65b0\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
        iteration.cs
        [class]{iteration}-[func]{WhileLoopII}\n
        iteration.go
        [class]{}-[func]{whileLoopII}\n
        iteration.swift
        [class]{}-[func]{whileLoopII}\n
        iteration.js
        [class]{}-[func]{whileLoopII}\n
        iteration.ts
        [class]{}-[func]{whileLoopII}\n
        iteration.dart
        [class]{}-[func]{whileLoopII}\n
        iteration.rs
        [class]{}-[func]{while_loop_ii}\n
        iteration.c
        [class]{}-[func]{whileLoopII}\n
        iteration.kt
        [class]{}-[func]{whileLoopII}\n
        iteration.rb
        [class]{}-[func]{while_loop_ii}\n

        \u5168\u4f53\u7684\u306b\u3001for\u30eb\u30fc\u30d7\u306f\u3088\u308a\u7c21\u6f54\u3067\u3001while\u30eb\u30fc\u30d7\u306f\u3088\u308a\u67d4\u8edf\u3067\u3059\u3002\u3069\u3061\u3089\u3082\u53cd\u5fa9\u69cb\u9020\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002\u3069\u3061\u3089\u3092\u4f7f\u7528\u3059\u308b\u304b\u306f\u3001\u554f\u984c\u306e\u5177\u4f53\u7684\u306a\u8981\u4ef6\u306b\u57fa\u3065\u3044\u3066\u6c7a\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#3","title":"3. \u00a0 \u30cd\u30b9\u30c8\u3057\u305f\u30eb\u30fc\u30d7","text":"

        1\u3064\u306e\u30eb\u30fc\u30d7\u69cb\u9020\u3092\u5225\u306e\u30eb\u30fc\u30d7\u69cb\u9020\u5185\u306b\u30cd\u30b9\u30c8\u3067\u304d\u307e\u3059\u3002\u4ee5\u4e0b\u306ffor\u30eb\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u305f\u4f8b\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
        def nested_for_loop(n: int) -> str:\n    \"\"\"\u4e8c\u91cdfor\u30eb\u30fc\u30d7\"\"\"\n    res = \"\"\n    # i = 1, 2, ..., n-1, n \u3092\u30eb\u30fc\u30d7\n    for i in range(1, n + 1):\n        # j = 1, 2, ..., n-1, n \u3092\u30eb\u30fc\u30d7\n        for j in range(1, n + 1):\n            res += f\"({i}, {j}), \"\n    return res\n
        iteration.cpp
        /* 2\u91cd for \u30eb\u30fc\u30d7 */\nstring nestedForLoop(int n) {\n    ostringstream res;\n    // \u30eb\u30fc\u30d7 i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; ++i) {\n        // \u30eb\u30fc\u30d7 j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; ++j) {\n            res << \"(\" << i << \", \" << j << \"), \";\n        }\n    }\n    return res.str();\n}\n
        iteration.java
        /* 2\u91cd for \u30eb\u30fc\u30d7 */\nString nestedForLoop(int n) {\n    StringBuilder res = new StringBuilder();\n    // \u30eb\u30fc\u30d7 i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        // \u30eb\u30fc\u30d7 j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; j++) {\n            res.append(\"(\" + i + \", \" + j + \"), \");\n        }\n    }\n    return res.toString();\n}\n
        iteration.cs
        [class]{iteration}-[func]{NestedForLoop}\n
        iteration.go
        [class]{}-[func]{nestedForLoop}\n
        iteration.swift
        [class]{}-[func]{nestedForLoop}\n
        iteration.js
        [class]{}-[func]{nestedForLoop}\n
        iteration.ts
        [class]{}-[func]{nestedForLoop}\n
        iteration.dart
        [class]{}-[func]{nestedForLoop}\n
        iteration.rs
        [class]{}-[func]{nested_for_loop}\n
        iteration.c
        [class]{}-[func]{nestedForLoop}\n
        iteration.kt
        [class]{}-[func]{nestedForLoop}\n
        iteration.rb
        [class]{}-[func]{nested_for_loop}\n

        \u4ee5\u4e0b\u306e\u56f3\u306f\u3053\u306e\u30cd\u30b9\u30c8\u3057\u305f\u30eb\u30fc\u30d7\u3092\u8868\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 2-2 \u00a0 Flowchart of the nested loop

        \u3053\u306e\u3088\u3046\u306a\u5834\u5408\u3001\u95a2\u6570\u306e\u64cd\u4f5c\u6570\u306f\\(n^2\\)\u306b\u6bd4\u4f8b\u3057\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u6642\u9593\u3068\u5165\u529b\u30c7\u30fc\u30bf\u306e\u30b5\u30a4\u30ba\\(n\\)\u306b\u306f\u300c\u4e8c\u6b21\u95a2\u4fc2\u300d\u304c\u3042\u308a\u307e\u3059\u3002

        \u3055\u3089\u306b\u30cd\u30b9\u30c8\u3057\u305f\u30eb\u30fc\u30d7\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3067\u8907\u96d1\u5ea6\u3092\u9ad8\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u5404\u30ec\u30d9\u30eb\u306e\u30cd\u30b9\u30c8\u306f\u4e8b\u5b9f\u4e0a\u300c\u6b21\u5143\u3092\u5897\u52a0\u300d\u3055\u305b\u3001\u6642\u9593\u8a08\u7b97\u91cf\u3092\u300c\u4e09\u6b21\u300d\u3001\u300c\u56db\u6b21\u300d\u306a\u3069\u306b\u5f15\u304d\u4e0a\u3052\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#222","title":"2.2.2 \u00a0 \u518d\u5e30","text":"

        \u518d\u5e30\u306f\u3001\u95a2\u6570\u304c\u81ea\u5206\u81ea\u8eab\u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u3067\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u6226\u7565\u3067\u3059\u3002\u4e3b\u306b2\u3064\u306e\u30d5\u30a7\u30fc\u30ba\u304c\u542b\u307e\u308c\u307e\u3059\uff1a

        1. \u547c\u3073\u51fa\u3057: \u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u81ea\u5206\u81ea\u8eab\u3092\u7e70\u308a\u8fd4\u3057\u547c\u3073\u51fa\u3057\u3001\u3057\u3070\u3057\u3070\u3088\u308a\u5c0f\u3055\u3044\u307e\u305f\u306f\u3088\u308a\u5358\u7d14\u306a\u5f15\u6570\u3067\u3001\u300c\u7d42\u4e86\u6761\u4ef6\u300d\u306b\u5411\u304b\u3063\u3066\u9032\u307f\u307e\u3059\u3002
        2. \u8fd4\u5374: \u300c\u7d42\u4e86\u6761\u4ef6\u300d\u304c\u30c8\u30ea\u30ac\u30fc\u3055\u308c\u308b\u3068\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u6700\u3082\u6df1\u3044\u518d\u5e30\u95a2\u6570\u304b\u3089\u8fd4\u308a\u59cb\u3081\u3001\u5404\u30ec\u30a4\u30e4\u30fc\u306e\u7d50\u679c\u3092\u96c6\u7d04\u3057\u307e\u3059\u3002

        \u5b9f\u88c5\u306e\u89b3\u70b9\u304b\u3089\u3001\u518d\u5e30\u30b3\u30fc\u30c9\u306f\u4e3b\u306b3\u3064\u306e\u8981\u7d20\u3092\u542b\u307f\u307e\u3059\u3002

        1. \u7d42\u4e86\u6761\u4ef6: \u300c\u547c\u3073\u51fa\u3057\u300d\u304b\u3089\u300c\u8fd4\u5374\u300d\u306b\u3044\u3064\u5207\u308a\u66ff\u3048\u308b\u304b\u3092\u6c7a\u5b9a\u3057\u307e\u3059\u3002
        2. \u518d\u5e30\u547c\u3073\u51fa\u3057: \u300c\u547c\u3073\u51fa\u3057\u300d\u306b\u5bfe\u5fdc\u3057\u3001\u95a2\u6570\u304c\u81ea\u5206\u81ea\u8eab\u3092\u547c\u3073\u51fa\u3057\u3001\u901a\u5e38\u306f\u3088\u308a\u5c0f\u3055\u3044\u307e\u305f\u306f\u3088\u308a\u5358\u7d14\u5316\u3055\u308c\u305f\u30d1\u30e9\u30e1\u30fc\u30bf\u3067\u884c\u3044\u307e\u3059\u3002
        3. \u7d50\u679c\u306e\u8fd4\u5374: \u300c\u8fd4\u5374\u300d\u306b\u5bfe\u5fdc\u3057\u3001\u73fe\u5728\u306e\u518d\u5e30\u30ec\u30d9\u30eb\u306e\u7d50\u679c\u304c\u524d\u306e\u30ec\u30a4\u30e4\u30fc\u306b\u8fd4\u3055\u308c\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u89b3\u5bdf\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u5358\u7d14\u306b\u95a2\u6570recur(n)\u3092\u547c\u3073\u51fa\u3059\u3060\u3051\u3067\\(1 + 2 + \\dots + n\\)\u306e\u5408\u8a08\u3092\u8a08\u7b97\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
        def recur(n: int) -> int:\n    \"\"\"\u518d\u5e30\"\"\"\n    # \u7d42\u4e86\u6761\u4ef6\n    if n == 1:\n        return 1\n    # \u518d\u5e30\uff1a\u518d\u5e30\u547c\u3073\u51fa\u3057\n    res = recur(n - 1)\n    # \u5fa9\u5e30\uff1a\u7d50\u679c\u3092\u8fd4\u3059\n    return n + res\n
        recursion.cpp
        /* \u518d\u5e30 */\nint recur(int n) {\n    // \u7d42\u4e86\u6761\u4ef6\n    if (n == 1)\n        return 1;\n    // \u518d\u5e30\uff1a\u518d\u5e30\u547c\u3073\u51fa\u3057\n    int res = recur(n - 1);\n    // \u623b\u308a\u5024\uff1a\u7d50\u679c\u3092\u8fd4\u3059\n    return n + res;\n}\n
        recursion.java
        /* \u518d\u5e30 */\nint recur(int n) {\n    // \u7d42\u4e86\u6761\u4ef6\n    if (n == 1)\n        return 1;\n    // \u518d\u5e30\uff1a\u518d\u5e30\u547c\u3073\u51fa\u3057\n    int res = recur(n - 1);\n    // \u623b\u308a\u5024\uff1a\u7d50\u679c\u3092\u8fd4\u3059\n    return n + res;\n}\n
        recursion.cs
        [class]{recursion}-[func]{Recur}\n
        recursion.go
        [class]{}-[func]{recur}\n
        recursion.swift
        [class]{}-[func]{recur}\n
        recursion.js
        [class]{}-[func]{recur}\n
        recursion.ts
        [class]{}-[func]{recur}\n
        recursion.dart
        [class]{}-[func]{recur}\n
        recursion.rs
        [class]{}-[func]{recur}\n
        recursion.c
        [class]{}-[func]{recur}\n
        recursion.kt
        [class]{}-[func]{recur}\n
        recursion.rb
        [class]{}-[func]{recur}\n

        \u4ee5\u4e0b\u306e\u56f3\u306f\u3053\u306e\u95a2\u6570\u306e\u518d\u5e30\u30d7\u30ed\u30bb\u30b9\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 2-3 \u00a0 Recursive process of the sum function

        \u53cd\u5fa9\u3068\u518d\u5e30\u306f\u8a08\u7b97\u306e\u89b3\u70b9\u304b\u3089\u540c\u3058\u7d50\u679c\u3092\u9054\u6210\u3067\u304d\u307e\u3059\u304c\u3001\u305d\u308c\u3089\u306f\u601d\u8003\u3068\u554f\u984c\u89e3\u6c7a\u306e\u5168\u304f\u7570\u306a\u308b\u30d1\u30e9\u30c0\u30a4\u30e0\u3092\u8868\u3057\u307e\u3059\u3002

        • \u53cd\u5fa9: \u300c\u30dc\u30c8\u30e0\u30a2\u30c3\u30d7\u300d\u3067\u554f\u984c\u3092\u89e3\u6c7a\u3057\u307e\u3059\u3002\u6700\u3082\u57fa\u672c\u7684\u306a\u30b9\u30c6\u30c3\u30d7\u304b\u3089\u59cb\u307e\u308a\u3001\u30bf\u30b9\u30af\u304c\u5b8c\u4e86\u3059\u308b\u307e\u3067\u3053\u308c\u3089\u306e\u30b9\u30c6\u30c3\u30d7\u3092\u7e70\u308a\u8fd4\u3057\u8ffd\u52a0\u307e\u305f\u306f\u7d2f\u7a4d\u3057\u307e\u3059\u3002
        • \u518d\u5e30: \u300c\u30c8\u30c3\u30d7\u30c0\u30a6\u30f3\u300d\u3067\u554f\u984c\u3092\u89e3\u6c7a\u3057\u307e\u3059\u3002\u5143\u306e\u554f\u984c\u3092\u3088\u308a\u5c0f\u3055\u306a\u30b5\u30d6\u554f\u984c\u306b\u5206\u89e3\u3057\u3001\u5404\u30b5\u30d6\u554f\u984c\u306f\u5143\u306e\u554f\u984c\u3068\u540c\u3058\u5f62\u5f0f\u3092\u6301\u3061\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u30b5\u30d6\u554f\u984c\u306f\u3001\u89e3\u304c\u5206\u304b\u3063\u3066\u3044\u308b\u30d9\u30fc\u30b9\u30b1\u30fc\u30b9\u3067\u505c\u6b62\u3059\u308b\u307e\u3067\u3001\u3055\u3089\u306b\u5c0f\u3055\u306a\u30b5\u30d6\u554f\u984c\u306b\u5206\u89e3\u3055\u308c\u307e\u3059\u3002

        \u5148\u307b\u3069\u306e\u5408\u8a08\u95a2\u6570\u306e\u4f8b\u3092\u53d6\u3063\u3066\u307f\u307e\u3057\u3087\u3046\u3002\\(f(n) = 1 + 2 + \\dots + n\\)\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u307e\u3059\u3002

        • \u53cd\u5fa9: \u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u3067\u306f\u3001\u30eb\u30fc\u30d7\u5185\u3067\u5408\u8a08\u30d7\u30ed\u30bb\u30b9\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\u3057\u307e\u3059\u3002\\(1\\)\u304b\u3089\u59cb\u307e\u308a\\(n\\)\u307e\u3067\u6a2a\u65ad\u3057\u3001\u5404\u53cd\u5fa9\u3067\u5408\u8a08\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3066\u6700\u7d42\u7684\u306b\\(f(n)\\)\u3092\u8a08\u7b97\u3057\u307e\u3059\u3002
        • \u518d\u5e30: \u3053\u3053\u3067\u306f\u3001\u554f\u984c\u306f\u30b5\u30d6\u554f\u984c\u306b\u5206\u89e3\u3055\u308c\u307e\u3059\uff1a\\(f(n) = n + f(n-1)\\)\u3002\u3053\u306e\u5206\u89e3\u306f\u3001\u30d9\u30fc\u30b9\u30b1\u30fc\u30b9\u306e\\(f(1) = 1\\)\u306b\u5230\u9054\u3059\u308b\u307e\u3067\u518d\u5e30\u7684\u306b\u7d9a\u304d\u3001\u305d\u3053\u3067\u518d\u5e30\u304c\u7d42\u4e86\u3057\u307e\u3059\u3002
        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#1","title":"1. \u00a0 \u547c\u3073\u51fa\u3057\u30b9\u30bf\u30c3\u30af","text":"

        \u518d\u5e30\u95a2\u6570\u304c\u81ea\u5206\u81ea\u8eab\u3092\u547c\u3073\u51fa\u3059\u305f\u3073\u306b\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u65b0\u3057\u304f\u958b\u59cb\u3055\u308c\u305f\u95a2\u6570\u306b\u30e1\u30e2\u30ea\u3092\u5272\u308a\u5f53\u3066\u3066\u3001\u30ed\u30fc\u30ab\u30eb\u5909\u6570\u3001\u623b\u308a\u30a2\u30c9\u30ec\u30b9\u3001\u305d\u306e\u4ed6\u306e\u95a2\u9023\u60c5\u5831\u3092\u683c\u7d0d\u3057\u307e\u3059\u3002\u3053\u308c\u306f2\u3064\u306e\u4e3b\u8981\u306a\u7d50\u679c\u3092\u3082\u305f\u3089\u3057\u307e\u3059\u3002

        • \u95a2\u6570\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30c7\u30fc\u30bf\u306f\u300c\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u300d\u3068\u547c\u3070\u308c\u308b\u30e1\u30e2\u30ea\u9818\u57df\u306b\u683c\u7d0d\u3055\u308c\u3001\u95a2\u6570\u304c\u8fd4\u3055\u308c\u305f\u5f8c\u306b\u306e\u307f\u89e3\u653e\u3055\u308c\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u518d\u5e30\u306f\u4e00\u822c\u7684\u306b\u53cd\u5fa9\u3088\u308a\u3082\u591a\u304f\u306e\u30e1\u30e2\u30ea\u7a7a\u9593\u3092\u6d88\u8cbb\u3057\u307e\u3059\u3002
        • \u518d\u5e30\u547c\u3073\u51fa\u3057\u306f\u8ffd\u52a0\u306e\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u3092\u5c0e\u5165\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u518d\u5e30\u306f\u901a\u5e38\u30eb\u30fc\u30d7\u3088\u308a\u3082\u6642\u9593\u52b9\u7387\u304c\u52a3\u308a\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u3001\u7d42\u4e86\u6761\u4ef6\u304c\u30c8\u30ea\u30ac\u30fc\u3055\u308c\u308b\u524d\u306b\\(n\\)\u500b\u306e\u672a\u8fd4\u5374\u306e\u518d\u5e30\u95a2\u6570\u304c\u3042\u308a\u3001\u518d\u5e30\u306e\u6df1\u3055\u304c\\(n\\)\u3067\u3042\u308b\u3053\u3068\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 2-4 \u00a0 Recursion call depth

        \u5b9f\u969b\u306b\u306f\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u8a31\u53ef\u3055\u308c\u308b\u518d\u5e30\u306e\u6df1\u3055\u306f\u901a\u5e38\u5236\u9650\u3055\u308c\u3066\u304a\u308a\u3001\u904e\u5ea6\u306b\u6df1\u3044\u518d\u5e30\u306f\u30b9\u30bf\u30c3\u30af\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u30a8\u30e9\u30fc\u3092\u5f15\u304d\u8d77\u3053\u3059\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#2","title":"2. \u00a0 \u672b\u5c3e\u518d\u5e30","text":"

        \u8208\u5473\u6df1\u3044\u3053\u3068\u306b\u3001\u95a2\u6570\u304c\u8fd4\u3059\u76f4\u524d\u306e\u6700\u5f8c\u306e\u30b9\u30c6\u30c3\u30d7\u3068\u3057\u3066\u518d\u5e30\u547c\u3073\u51fa\u3057\u3092\u5b9f\u884c\u3059\u308b\u5834\u5408\u3001\u30b3\u30f3\u30d1\u30a4\u30e9\u307e\u305f\u306f\u30a4\u30f3\u30bf\u30fc\u30d7\u30ea\u30bf\u30fc\u306b\u3088\u3063\u3066\u53cd\u5fa9\u3068\u540c\u3058\u7a7a\u9593\u52b9\u7387\u306b\u306a\u308b\u3088\u3046\u306b\u6700\u9069\u5316\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u30b7\u30ca\u30ea\u30aa\u306f\u672b\u5c3e\u518d\u5e30\u3068\u3057\u3066\u77e5\u3089\u308c\u3066\u3044\u307e\u3059\u3002

        • \u901a\u5e38\u306e\u518d\u5e30: \u6a19\u6e96\u7684\u306a\u518d\u5e30\u3067\u306f\u3001\u95a2\u6570\u304c\u524d\u306e\u30ec\u30d9\u30eb\u306b\u623b\u3063\u305f\u3068\u304d\u3001\u3055\u3089\u306b\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3057\u7d9a\u3051\u308b\u305f\u3081\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u524d\u306e\u547c\u3073\u51fa\u3057\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u4fdd\u5b58\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u672b\u5c3e\u518d\u5e30: \u3053\u3053\u3067\u306f\u3001\u518d\u5e30\u547c\u3073\u51fa\u3057\u306f\u95a2\u6570\u304c\u8fd4\u3059\u524d\u306e\u6700\u7d42\u64cd\u4f5c\u3067\u3059\u3002\u3053\u308c\u306f\u3001\u524d\u306e\u30ec\u30d9\u30eb\u306b\u623b\u3063\u305f\u969b\u306b\u3001\u3055\u3089\u306a\u308b\u30a2\u30af\u30b7\u30e7\u30f3\u304c\u5fc5\u8981\u306a\u3044\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u305f\u3081\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u524d\u306e\u30ec\u30d9\u30eb\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u4fdd\u5b58\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u305b\u3093\u3002

        \u4f8b\u3048\u3070\u3001\\(1 + 2 + \\dots + n\\)\u306e\u8a08\u7b97\u3067\u306f\u3001\u7d50\u679c\u5909\u6570res\u3092\u95a2\u6570\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u306b\u3059\u308b\u3053\u3068\u3067\u3001\u672b\u5c3e\u518d\u5e30\u3092\u5b9f\u73fe\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
        def tail_recur(n, res):\n    \"\"\"\u672b\u5c3e\u518d\u5e30\"\"\"\n    # \u7d42\u4e86\u6761\u4ef6\n    if n == 0:\n        return res\n    # \u672b\u5c3e\u518d\u5e30\u547c\u3073\u51fa\u3057\n    return tail_recur(n - 1, res + n)\n
        recursion.cpp
        /* \u672b\u5c3e\u518d\u5e30 */\nint tailRecur(int n, int res) {\n    // \u7d42\u4e86\u6761\u4ef6\n    if (n == 0)\n        return res;\n    // \u672b\u5c3e\u518d\u5e30\u547c\u3073\u51fa\u3057\n    return tailRecur(n - 1, res + n);\n}\n
        recursion.java
        /* \u672b\u5c3e\u518d\u5e30 */\nint tailRecur(int n, int res) {\n    // \u7d42\u4e86\u6761\u4ef6\n    if (n == 0)\n        return res;\n    // \u672b\u5c3e\u518d\u5e30\u547c\u3073\u51fa\u3057\n    return tailRecur(n - 1, res + n);\n}\n
        recursion.cs
        [class]{recursion}-[func]{TailRecur}\n
        recursion.go
        [class]{}-[func]{tailRecur}\n
        recursion.swift
        [class]{}-[func]{tailRecur}\n
        recursion.js
        [class]{}-[func]{tailRecur}\n
        recursion.ts
        [class]{}-[func]{tailRecur}\n
        recursion.dart
        [class]{}-[func]{tailRecur}\n
        recursion.rs
        [class]{}-[func]{tail_recur}\n
        recursion.c
        [class]{}-[func]{tailRecur}\n
        recursion.kt
        [class]{}-[func]{tailRecur}\n
        recursion.rb
        [class]{}-[func]{tail_recur}\n

        \u672b\u5c3e\u518d\u5e30\u306e\u5b9f\u884c\u30d7\u30ed\u30bb\u30b9\u306f\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u901a\u5e38\u306e\u518d\u5e30\u3068\u672b\u5c3e\u518d\u5e30\u3092\u6bd4\u8f03\u3059\u308b\u3068\u3001\u5408\u8a08\u64cd\u4f5c\u306e\u30dd\u30a4\u30f3\u30c8\u304c\u7570\u306a\u308a\u307e\u3059\u3002

        • \u901a\u5e38\u306e\u518d\u5e30: \u5408\u8a08\u64cd\u4f5c\u306f\u300c\u8fd4\u5374\u300d\u30d5\u30a7\u30fc\u30ba\u3067\u767a\u751f\u3057\u3001\u5404\u30ec\u30a4\u30e4\u30fc\u304c\u8fd4\u3063\u305f\u5f8c\u306b\u3082\u3046\u4e00\u5ea6\u5408\u8a08\u304c\u5fc5\u8981\u3067\u3059\u3002
        • \u672b\u5c3e\u518d\u5e30: \u5408\u8a08\u64cd\u4f5c\u306f\u300c\u547c\u3073\u51fa\u3057\u300d\u30d5\u30a7\u30fc\u30ba\u3067\u767a\u751f\u3057\u3001\u300c\u8fd4\u5374\u300d\u30d5\u30a7\u30fc\u30ba\u306f\u5404\u30ec\u30a4\u30e4\u30fc\u3092\u901a\u3058\u3066\u8fd4\u3059\u3060\u3051\u3067\u3059\u3002

        \u56f3 2-5 \u00a0 Tail recursion process

        Tip

        \u591a\u304f\u306e\u30b3\u30f3\u30d1\u30a4\u30e9\u3084\u30a4\u30f3\u30bf\u30fc\u30d7\u30ea\u30bf\u30fc\u306f\u672b\u5c3e\u518d\u5e30\u6700\u9069\u5316\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u306a\u3044\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4f8b\u3048\u3070\u3001Python\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u672b\u5c3e\u518d\u5e30\u6700\u9069\u5316\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u306a\u3044\u305f\u3081\u3001\u95a2\u6570\u304c\u672b\u5c3e\u518d\u5e30\u306e\u5f62\u5f0f\u3067\u3042\u3063\u3066\u3082\u3001\u30b9\u30bf\u30c3\u30af\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u306e\u554f\u984c\u306b\u906d\u9047\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#3_1","title":"3. \u00a0 \u518d\u5e30\u6728","text":"

        \u300c\u5206\u5272\u7d71\u6cbb\u300d\u306b\u95a2\u9023\u3059\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u6271\u3046\u969b\u3001\u518d\u5e30\u306f\u53cd\u5fa9\u3088\u308a\u3082\u3057\u3070\u3057\u3070\u3088\u308a\u76f4\u611f\u7684\u306a\u30a2\u30d7\u30ed\u30fc\u30c1\u3068\u3088\u308a\u8aad\u307f\u3084\u3059\u3044\u30b3\u30fc\u30c9\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002\u300c\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u300d\u3092\u4f8b\u306b\u53d6\u3063\u3066\u307f\u307e\u3057\u3087\u3046\u3002

        Question

        \u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\\(0, 1, 1, 2, 3, 5, 8, 13, \\dots\\)\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u6570\u5217\u306e\\(n\\)\u756a\u76ee\u306e\u6570\u3092\u6c42\u3081\u306a\u3055\u3044\u3002

        \u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u306e\\(n\\)\u756a\u76ee\u306e\u6570\u3092\\(f(n)\\)\u3068\u3059\u308b\u3068\u30012\u3064\u306e\u7d50\u8ad6\u3092\u7c21\u5358\u306b\u5c0e\u304d\u51fa\u305b\u307e\u3059\uff1a

        • \u6570\u5217\u306e\u6700\u521d\u306e2\u3064\u306e\u6570\u306f\\(f(1) = 0\\)\u3068\\(f(2) = 1\\)\u3067\u3059\u3002
        • \u6570\u5217\u306e\u5404\u6570\u306f\u524d\u306e2\u3064\u306e\u6570\u306e\u5408\u8a08\u3067\u3059\u3002\u3064\u307e\u308a\u3001\\(f(n) = f(n - 1) + f(n - 2)\\)\u3067\u3059\u3002

        \u518d\u5e30\u95a2\u4fc2\u3092\u4f7f\u7528\u3057\u3001\u6700\u521d\u306e2\u3064\u306e\u6570\u3092\u7d42\u4e86\u6761\u4ef6\u3068\u3057\u3066\u8003\u616e\u3059\u308b\u3068\u3001\u518d\u5e30\u30b3\u30fc\u30c9\u3092\u66f8\u3051\u307e\u3059\u3002fib(n)\u3092\u547c\u3073\u51fa\u3059\u3068\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u306e\\(n\\)\u756a\u76ee\u306e\u6570\u304c\u5f97\u3089\u308c\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
        def fib(n: int) -> int:\n    \"\"\"\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\uff1a\u518d\u5e30\"\"\"\n    # \u7d42\u4e86\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if n == 1 or n == 2:\n        return n - 1\n    # \u518d\u5e30\u547c\u3073\u51fa\u3057 f(n) = f(n-1) + f(n-2)\n    res = fib(n - 1) + fib(n - 2)\n    # \u7d50\u679c f(n) \u3092\u8fd4\u3059\n    return res\n
        recursion.cpp
        /* \u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\uff1a\u518d\u5e30 */\nint fib(int n) {\n    // \u7d42\u4e86\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // \u518d\u5e30\u547c\u3073\u51fa\u3057 f(n) = f(n-1) + f(n-2)\n    int res = fib(n - 1) + fib(n - 2);\n    // \u7d50\u679c f(n) \u3092\u8fd4\u3059\n    return res;\n}\n
        recursion.java
        /* \u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\uff1a\u518d\u5e30 */\nint fib(int n) {\n    // \u7d42\u4e86\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // \u518d\u5e30\u547c\u3073\u51fa\u3057 f(n) = f(n-1) + f(n-2)\n    int res = fib(n - 1) + fib(n - 2);\n    // \u7d50\u679c f(n) \u3092\u8fd4\u3059\n    return res;\n}\n
        recursion.cs
        [class]{recursion}-[func]{Fib}\n
        recursion.go
        [class]{}-[func]{fib}\n
        recursion.swift
        [class]{}-[func]{fib}\n
        recursion.js
        [class]{}-[func]{fib}\n
        recursion.ts
        [class]{}-[func]{fib}\n
        recursion.dart
        [class]{}-[func]{fib}\n
        recursion.rs
        [class]{}-[func]{fib}\n
        recursion.c
        [class]{}-[func]{fib}\n
        recursion.kt
        [class]{}-[func]{fib}\n
        recursion.rb
        [class]{}-[func]{fib}\n

        \u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\u305d\u308c\u81ea\u4f53\u306e\u4e2d\u30672\u3064\u306e\u95a2\u6570\u3092\u518d\u5e30\u7684\u306b\u547c\u3073\u51fa\u3057\u3066\u3044\u308b\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002\u3064\u307e\u308a\u30011\u56de\u306e\u547c\u3073\u51fa\u3057\u30672\u3064\u306e\u5206\u5c90\u547c\u3073\u51fa\u3057\u304c\u751f\u6210\u3055\u308c\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u3001\u3053\u306e\u7d99\u7d9a\u7684\u306a\u518d\u5e30\u547c\u3073\u51fa\u3057\u306f\u6700\u7d42\u7684\u306b\u6df1\u3055\\(n\\)\u306e\u518d\u5e30\u6728\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002

        \u56f3 2-6 \u00a0 Fibonacci sequence recursion tree

        \u57fa\u672c\u7684\u306b\u3001\u518d\u5e30\u306f\u300c\u554f\u984c\u3092\u3088\u308a\u5c0f\u3055\u306a\u30b5\u30d6\u554f\u984c\u306b\u5206\u89e3\u3059\u308b\u300d\u30d1\u30e9\u30c0\u30a4\u30e0\u3092\u4f53\u73fe\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306f\u91cd\u8981\u3067\u3059\u3002

        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u89b3\u70b9\u304b\u3089\u3001\u63a2\u7d22\u3001\u30bd\u30fc\u30c8\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3001\u5206\u5272\u7d71\u6cbb\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306a\u3069\u306e\u591a\u304f\u306e\u91cd\u8981\u306a\u6226\u7565\u306f\u3001\u76f4\u63a5\u7684\u307e\u305f\u306f\u9593\u63a5\u7684\u306b\u3053\u306e\u601d\u8003\u65b9\u6cd5\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002
        • \u30c7\u30fc\u30bf\u69cb\u9020\u306e\u89b3\u70b9\u304b\u3089\u3001\u518d\u5e30\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u6728\u3001\u30b0\u30e9\u30d5\u3092\u6271\u3046\u306e\u306b\u81ea\u7136\u306b\u9069\u3057\u3066\u304a\u308a\u3001\u3053\u308c\u3089\u306f\u5206\u5272\u7d71\u6cbb\u30a2\u30d7\u30ed\u30fc\u30c1\u3092\u4f7f\u7528\u3057\u305f\u5206\u6790\u306b\u9069\u3057\u3066\u3044\u308b\u305f\u3081\u3067\u3059\u3002
        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#223","title":"2.2.3 \u00a0 \u6bd4\u8f03","text":"

        \u4e0a\u8a18\u306e\u5185\u5bb9\u3092\u307e\u3068\u3081\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u8868\u306f\u5b9f\u88c5\u3001\u6027\u80fd\u3001\u9069\u7528\u6027\u306e\u89b3\u70b9\u304b\u3089\u53cd\u5fa9\u3068\u518d\u5e30\u306e\u9055\u3044\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u8868: \u53cd\u5fa9\u3068\u518d\u5e30\u306e\u7279\u6027\u306e\u6bd4\u8f03

        \u53cd\u5fa9 \u518d\u5e30 \u30a2\u30d7\u30ed\u30fc\u30c1 \u30eb\u30fc\u30d7\u69cb\u9020 \u95a2\u6570\u304c\u81ea\u5206\u81ea\u8eab\u3092\u547c\u3073\u51fa\u3059 \u6642\u9593\u52b9\u7387 \u4e00\u822c\u7684\u306b\u3088\u308a\u9ad8\u3044\u52b9\u7387\u3001\u95a2\u6570\u547c\u3073\u51fa\u3057\u306e\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u306a\u3057 \u5404\u95a2\u6570\u547c\u3073\u51fa\u3057\u304c\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u3092\u751f\u6210 \u30e1\u30e2\u30ea\u4f7f\u7528\u91cf \u901a\u5e38\u306f\u56fa\u5b9a\u30b5\u30a4\u30ba\u306e\u30e1\u30e2\u30ea\u7a7a\u9593\u3092\u4f7f\u7528 \u7d2f\u7a4d\u7684\u306a\u95a2\u6570\u547c\u3073\u51fa\u3057\u304c\u5927\u91cf\u306e\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u4f7f\u7528\u3059\u308b\u53ef\u80fd\u6027 \u9069\u7528\u53ef\u80fd\u306a\u554f\u984c \u5358\u7d14\u306a\u30eb\u30fc\u30d7\u30bf\u30b9\u30af\u306b\u9069\u3057\u3066\u3044\u308b\u3001\u76f4\u611f\u7684\u3067\u8aad\u307f\u3084\u3059\u3044\u30b3\u30fc\u30c9 \u554f\u984c\u306e\u5206\u89e3\u306b\u9069\u3057\u3066\u3044\u308b\uff08\u6728\u3001\u30b0\u30e9\u30d5\u3001\u5206\u5272\u7d71\u6cbb\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306a\u3069\uff09\u3001\u7c21\u6f54\u3067\u660e\u78ba\u306a\u30b3\u30fc\u30c9\u69cb\u9020

        Tip

        \u4ee5\u4e0b\u306e\u5185\u5bb9\u304c\u7406\u89e3\u3057\u306b\u304f\u3044\u5834\u5408\u306f\u3001\u300c\u30b9\u30bf\u30c3\u30af\u300d\u306e\u7ae0\u3092\u8aad\u3093\u3060\u5f8c\u306b\u518d\u3073\u8a2a\u308c\u308b\u3053\u3068\u3092\u691c\u8a0e\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u305d\u308c\u3067\u306f\u3001\u53cd\u5fa9\u3068\u518d\u5e30\u306e\u672c\u8cea\u7684\u306a\u95a2\u9023\u306f\u4f55\u3067\u3057\u3087\u3046\u304b\uff1f\u4e0a\u8a18\u306e\u518d\u5e30\u95a2\u6570\u3092\u4f8b\u306b\u53d6\u308b\u3068\u3001\u5408\u8a08\u64cd\u4f5c\u306f\u518d\u5e30\u306e\u300c\u8fd4\u5374\u300d\u30d5\u30a7\u30fc\u30ba\u3067\u767a\u751f\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u6700\u521d\u306b\u547c\u3073\u51fa\u3055\u308c\u305f\u95a2\u6570\u304c\u6700\u5f8c\u306b\u5408\u8a08\u64cd\u4f5c\u3092\u5b8c\u4e86\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u3001\u30b9\u30bf\u30c3\u30af\u306e\u300c\u5f8c\u5165\u308c\u5148\u51fa\u3057\u300d\u539f\u7406\u3092\u53cd\u6620\u3057\u3066\u3044\u307e\u3059\u3002

        \u300c\u547c\u3073\u51fa\u3057\u30b9\u30bf\u30c3\u30af\u300d\u3084\u300c\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u300d\u306a\u3069\u306e\u518d\u5e30\u7528\u8a9e\u306f\u3001\u518d\u5e30\u3068\u30b9\u30bf\u30c3\u30af\u306e\u5bc6\u63a5\u306a\u95a2\u4fc2\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        1. \u547c\u3073\u51fa\u3057: \u95a2\u6570\u304c\u547c\u3073\u51fa\u3055\u308c\u308b\u3068\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u300c\u547c\u3073\u51fa\u3057\u30b9\u30bf\u30c3\u30af\u300d\u4e0a\u306b\u305d\u306e\u95a2\u6570\u7528\u306e\u65b0\u3057\u3044\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u3092\u5272\u308a\u5f53\u3066\u3001\u30ed\u30fc\u30ab\u30eb\u5909\u6570\u3001\u30d1\u30e9\u30e1\u30fc\u30bf\u3001\u623b\u308a\u30a2\u30c9\u30ec\u30b9\u3001\u305d\u306e\u4ed6\u306e\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3057\u307e\u3059\u3002
        2. \u8fd4\u5374: \u95a2\u6570\u304c\u5b9f\u884c\u3092\u5b8c\u4e86\u3057\u3066\u8fd4\u308b\u3068\u3001\u5bfe\u5fdc\u3059\u308b\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u304c\u300c\u547c\u3073\u51fa\u3057\u30b9\u30bf\u30c3\u30af\u300d\u304b\u3089\u524a\u9664\u3055\u308c\u3001\u524d\u306e\u95a2\u6570\u306e\u5b9f\u884c\u74b0\u5883\u304c\u5fa9\u5143\u3055\u308c\u307e\u3059\u3002

        \u3057\u305f\u304c\u3063\u3066\u3001\u660e\u793a\u7684\u306a\u30b9\u30bf\u30c3\u30af\u3092\u4f7f\u7528\u3057\u3066\u547c\u3073\u51fa\u3057\u30b9\u30bf\u30c3\u30af\u306e\u52d5\u4f5c\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u518d\u5e30\u3092\u53cd\u5fa9\u5f62\u5f0f\u306b\u5909\u63db\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
        def for_loop_recur(n: int) -> int:\n    \"\"\"\u53cd\u5fa9\u3067\u518d\u5e30\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\"\"\"\n    # \u660e\u793a\u7684\u306a\u30b9\u30bf\u30c3\u30af\u3092\u4f7f\u7528\u3057\u3066\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u30b9\u30bf\u30c3\u30af\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\n    stack = []\n    res = 0\n    # \u518d\u5e30\uff1a\u518d\u5e30\u547c\u3073\u51fa\u3057\n    for i in range(n, 0, -1):\n        # \u300c\u30b9\u30bf\u30c3\u30af\u3078\u306e\u30d7\u30c3\u30b7\u30e5\u300d\u3067\u300c\u518d\u5e30\u300d\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\n        stack.append(i)\n    # \u5fa9\u5e30\uff1a\u7d50\u679c\u3092\u8fd4\u3059\n    while stack:\n        # \u300c\u30b9\u30bf\u30c3\u30af\u304b\u3089\u306e\u30dd\u30c3\u30d7\u300d\u3067\u300c\u5fa9\u5e30\u300d\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\n        res += stack.pop()\n    # res = 1+2+3+...+n\n    return res\n
        recursion.cpp
        /* \u53cd\u5fa9\u3067\u518d\u5e30\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8 */\nint forLoopRecur(int n) {\n    // \u660e\u793a\u7684\u306a\u30b9\u30bf\u30c3\u30af\u3092\u4f7f\u7528\u3057\u3066\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u30b9\u30bf\u30c3\u30af\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\n    stack<int> stack;\n    int res = 0;\n    // \u518d\u5e30\uff1a\u518d\u5e30\u547c\u3073\u51fa\u3057\n    for (int i = n; i > 0; i--) {\n        // \u300c\u30b9\u30bf\u30c3\u30af\u3078\u306e\u30d7\u30c3\u30b7\u30e5\u300d\u3067\u300c\u518d\u5e30\u300d\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\n        stack.push(i);\n    }\n    // \u623b\u308a\u5024\uff1a\u7d50\u679c\u3092\u8fd4\u3059\n    while (!stack.empty()) {\n        // \u300c\u30b9\u30bf\u30c3\u30af\u304b\u3089\u306e\u30dd\u30c3\u30d7\u300d\u3067\u300c\u623b\u308a\u5024\u300d\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\n        res += stack.top();\n        stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
        recursion.java
        /* \u53cd\u5fa9\u3067\u518d\u5e30\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8 */\nint forLoopRecur(int n) {\n    // \u660e\u793a\u7684\u306a\u30b9\u30bf\u30c3\u30af\u3092\u4f7f\u7528\u3057\u3066\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u30b9\u30bf\u30c3\u30af\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\n    Stack<Integer> stack = new Stack<>();\n    int res = 0;\n    // \u518d\u5e30\uff1a\u518d\u5e30\u547c\u3073\u51fa\u3057\n    for (int i = n; i > 0; i--) {\n        // \u300c\u30b9\u30bf\u30c3\u30af\u3078\u306e\u30d7\u30c3\u30b7\u30e5\u300d\u3067\u300c\u518d\u5e30\u300d\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\n        stack.push(i);\n    }\n    // \u623b\u308a\u5024\uff1a\u7d50\u679c\u3092\u8fd4\u3059\n    while (!stack.isEmpty()) {\n        // \u300c\u30b9\u30bf\u30c3\u30af\u304b\u3089\u306e\u30dd\u30c3\u30d7\u300d\u3067\u300c\u623b\u308a\u5024\u300d\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\n        res += stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
        recursion.cs
        [class]{recursion}-[func]{ForLoopRecur}\n
        recursion.go
        [class]{}-[func]{forLoopRecur}\n
        recursion.swift
        [class]{}-[func]{forLoopRecur}\n
        recursion.js
        [class]{}-[func]{forLoopRecur}\n
        recursion.ts
        [class]{}-[func]{forLoopRecur}\n
        recursion.dart
        [class]{}-[func]{forLoopRecur}\n
        recursion.rs
        [class]{}-[func]{for_loop_recur}\n
        recursion.c
        [class]{}-[func]{forLoopRecur}\n
        recursion.kt
        [class]{}-[func]{forLoopRecur}\n
        recursion.rb
        [class]{}-[func]{for_loop_recur}\n

        \u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\u518d\u5e30\u304c\u53cd\u5fa9\u306b\u5909\u63db\u3055\u308c\u305f\u3068\u304d\u3001\u30b3\u30fc\u30c9\u306f\u3088\u308a\u8907\u96d1\u306b\u306a\u308a\u307e\u3059\u3002\u53cd\u5fa9\u3068\u518d\u5e30\u306f\u3057\u3070\u3057\u3070\u76f8\u4e92\u306b\u5909\u63db\u3067\u304d\u307e\u3059\u304c\u30012\u3064\u306e\u7406\u7531\u3067\u305d\u3046\u3059\u308b\u3053\u3068\u304c\u5e38\u306b\u63a8\u5968\u3055\u308c\u308b\u308f\u3051\u3067\u306f\u3042\u308a\u307e\u305b\u3093\uff1a

        • \u5909\u63db\u3055\u308c\u305f\u30b3\u30fc\u30c9\u306f\u7406\u89e3\u304c\u3088\u308a\u56f0\u96e3\u306b\u306a\u308a\u3001\u8aad\u307f\u306b\u304f\u304f\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        • \u4e00\u90e8\u306e\u8907\u96d1\u306a\u554f\u984c\u3067\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u306e\u547c\u3073\u51fa\u3057\u30b9\u30bf\u30c3\u30af\u306e\u52d5\u4f5c\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\u3059\u308b\u3053\u3068\u306f\u975e\u5e38\u306b\u56f0\u96e3\u3067\u3059\u3002

        \u7d50\u8ad6\u3068\u3057\u3066\u3001\u53cd\u5fa9\u307e\u305f\u306f\u518d\u5e30\u3092\u9078\u629e\u3059\u308b\u304b\u306f\u554f\u984c\u306e\u5177\u4f53\u7684\u306a\u6027\u8cea\u306b\u3088\u308a\u307e\u3059\u3002\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306e\u5b9f\u8df5\u3067\u306f\u3001\u4e21\u65b9\u306e\u9577\u6240\u3068\u77ed\u6240\u3092\u6bd4\u8f03\u691c\u8a0e\u3057\u3001\u624b\u5143\u306e\u72b6\u6cc1\u306b\u6700\u3082\u9069\u3057\u305f\u30a2\u30d7\u30ed\u30fc\u30c1\u3092\u9078\u629e\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002

        "},{"location":"chapter_computational_complexity/performance_evaluation/","title":"2.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u8a55\u4fa1","text":"

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u8a2d\u8a08\u306b\u304a\u3044\u3066\u3001\u79c1\u305f\u3061\u306f\u9806\u5e8f\u306b\u5f93\u3063\u3066\u4ee5\u4e0b\u306e2\u3064\u306e\u76ee\u6a19\u3092\u8ffd\u6c42\u3057\u307e\u3059\u3002

        1. \u554f\u984c\u306e\u89e3\u6c7a\u7b56\u3092\u898b\u3064\u3051\u308b: \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u6307\u5b9a\u3055\u308c\u305f\u5165\u529b\u7bc4\u56f2\u5185\u3067\u78ba\u5b9f\u306b\u6b63\u3057\u3044\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3079\u304d\u3067\u3059\u3002
        2. \u6700\u9069\u89e3\u3092\u6c42\u3081\u308b: \u540c\u3058\u554f\u984c\u306b\u5bfe\u3057\u3066\u8907\u6570\u306e\u89e3\u6c7a\u7b56\u304c\u5b58\u5728\u3059\u308b\u5834\u5408\u304c\u3042\u308a\u3001\u79c1\u305f\u3061\u306f\u53ef\u80fd\u306a\u9650\u308a\u6700\u3082\u52b9\u7387\u7684\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3092\u76ee\u6307\u3057\u307e\u3059\u3002

        \u3064\u307e\u308a\u3001\u554f\u984c\u3092\u89e3\u6c7a\u3067\u304d\u308b\u3053\u3068\u3092\u524d\u63d0\u3068\u3057\u3066\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u304c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u8a55\u4fa1\u3059\u308b\u4e3b\u8981\u306a\u57fa\u6e96\u3068\u306a\u3063\u3066\u304a\u308a\u3001\u3053\u308c\u306b\u306f\u4ee5\u4e0b\u306e2\u3064\u306e\u6b21\u5143\u304c\u542b\u307e\u308c\u307e\u3059\u3002

        • \u6642\u9593\u52b9\u7387: \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u5b9f\u884c\u3055\u308c\u308b\u901f\u5ea6\u3002
        • \u7a7a\u9593\u52b9\u7387: \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u5360\u6709\u3059\u308b\u30e1\u30e2\u30ea\u7a7a\u9593\u306e\u30b5\u30a4\u30ba\u3002

        \u8981\u3059\u308b\u306b\u3001\u79c1\u305f\u3061\u306e\u76ee\u6a19\u306f\u3001\u9ad8\u901f\u3067\u30e1\u30e2\u30ea\u52b9\u7387\u306e\u826f\u3044\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u8a2d\u8a08\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u52b9\u679c\u7684\u306b\u8a55\u4fa1\u3059\u308b\u3053\u3068\u306f\u91cd\u8981\u3067\u3059\u3002\u306a\u305c\u306a\u3089\u3001\u305d\u3046\u3059\u308b\u3053\u3068\u3067\u521d\u3081\u3066\u69d8\u3005\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u6bd4\u8f03\u3057\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u8a2d\u8a08\u3068\u6700\u9069\u5316\u30d7\u30ed\u30bb\u30b9\u3092\u5c0e\u304f\u3053\u3068\u304c\u3067\u304d\u308b\u304b\u3089\u3067\u3059\u3002

        \u52b9\u7387\u8a55\u4fa1\u306b\u306f\u4e3b\u306b2\u3064\u306e\u65b9\u6cd5\u304c\u3042\u308a\u307e\u3059\uff1a\u5b9f\u969b\u306e\u30c6\u30b9\u30c8\u3068\u7406\u8ad6\u7684\u63a8\u5b9a\u3067\u3059\u3002

        "},{"location":"chapter_computational_complexity/performance_evaluation/#211","title":"2.1.1 \u00a0 \u5b9f\u969b\u306e\u30c6\u30b9\u30c8","text":"

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u3068B\u304c\u3042\u308a\u3001\u3069\u3061\u3089\u3082\u540c\u3058\u554f\u984c\u3092\u89e3\u6c7a\u3067\u304d\u3001\u305d\u308c\u3089\u306e\u52b9\u7387\u3092\u6bd4\u8f03\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3068\u3057\u307e\u3059\u3002\u6700\u3082\u76f4\u63a5\u7684\u306a\u65b9\u6cd5\u306f\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u3092\u4f7f\u7528\u3057\u3066\u3053\u308c\u30892\u3064\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u5b9f\u884c\u3057\u3001\u5b9f\u884c\u6642\u9593\u3068\u30e1\u30e2\u30ea\u4f7f\u7528\u91cf\u3092\u76e3\u8996\u30fb\u8a18\u9332\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u3053\u306e\u8a55\u4fa1\u65b9\u6cd5\u306f\u5b9f\u969b\u306e\u72b6\u6cc1\u3092\u53cd\u6620\u3057\u307e\u3059\u304c\u3001\u5927\u304d\u306a\u5236\u9650\u304c\u3042\u308a\u307e\u3059\u3002

        \u4e00\u65b9\u3067\u3001\u30c6\u30b9\u30c8\u74b0\u5883\u304b\u3089\u306e\u5e72\u6e09\u3092\u6392\u9664\u3059\u308b\u3053\u3068\u306f\u56f0\u96e3\u3067\u3059\u3002\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u69cb\u6210\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6027\u80fd\u306b\u5f71\u97ff\u3092\u4e0e\u3048\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u4e26\u5217\u5ea6\u306e\u9ad8\u3044\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u30de\u30eb\u30c1\u30b3\u30a2CPU\u3067\u306e\u5b9f\u884c\u306b\u3088\u308a\u9069\u3057\u3066\u3044\u307e\u3059\u3057\u3001\u96c6\u7d04\u7684\u306a\u30e1\u30e2\u30ea\u64cd\u4f5c\u3092\u542b\u3080\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u9ad8\u6027\u80fd\u30e1\u30e2\u30ea\u3067\u3088\u308a\u826f\u3044\u6027\u80fd\u3092\u767a\u63ee\u3057\u307e\u3059\u3002\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u30c6\u30b9\u30c8\u7d50\u679c\u306f\u3001\u7570\u306a\u308b\u30de\u30b7\u30f3\u9593\u3067\u5909\u308f\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u5e73\u5747\u52b9\u7387\u3092\u8a08\u7b97\u3059\u308b\u305f\u3081\u306b\u8907\u6570\u306e\u30de\u30b7\u30f3\u3067\u30c6\u30b9\u30c8\u3059\u308b\u3053\u3068\u304c\u5b9f\u7528\u7684\u3067\u306a\u3044\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

        \u4e00\u65b9\u3067\u3001\u5b8c\u5168\u306a\u30c6\u30b9\u30c8\u3092\u5b9f\u65bd\u3059\u308b\u3053\u3068\u306f\u975e\u5e38\u306b\u30ea\u30bd\u30fc\u30b9\u96c6\u7d04\u7684\u3067\u3059\u3002\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u306f\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\u306b\u3088\u3063\u3066\u5909\u308f\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u30c7\u30fc\u30bf\u91cf\u304c\u5c11\u306a\u3044\u5834\u5408\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u304cB\u3088\u308a\u901f\u304f\u5b9f\u884c\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u30c7\u30fc\u30bf\u91cf\u304c\u591a\u3044\u5834\u5408\u306f\u30c6\u30b9\u30c8\u7d50\u679c\u304c\u9006\u306b\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u8aac\u5f97\u529b\u306e\u3042\u308b\u7d50\u8ad6\u3092\u5c0e\u304f\u305f\u3081\u306b\u306f\u3001\u5e45\u5e83\u3044\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\u3092\u30c6\u30b9\u30c8\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u3053\u308c\u306b\u306f\u904e\u5ea6\u306a\u8a08\u7b97\u30ea\u30bd\u30fc\u30b9\u304c\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/performance_evaluation/#212","title":"2.1.2 \u00a0 \u7406\u8ad6\u7684\u63a8\u5b9a","text":"

        \u5b9f\u969b\u306e\u30c6\u30b9\u30c8\u306e\u5927\u304d\u306a\u5236\u9650\u306b\u3088\u308a\u3001\u8a08\u7b97\u306e\u307f\u3067\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u8a55\u4fa1\u3059\u308b\u3053\u3068\u3092\u691c\u8a0e\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u63a8\u5b9a\u65b9\u6cd5\u306f\u6f38\u8fd1\u7684\u8907\u96d1\u5ea6\u89e3\u6790\u3001\u307e\u305f\u306f\u5358\u306b\u8907\u96d1\u5ea6\u89e3\u6790\u3068\u3057\u3066\u77e5\u3089\u308c\u3066\u3044\u307e\u3059\u3002

        \u8907\u96d1\u5ea6\u89e3\u6790\u306f\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u306b\u5fc5\u8981\u306a\u6642\u9593\u3068\u7a7a\u9593\u30ea\u30bd\u30fc\u30b9\u3068\u5165\u529b\u30c7\u30fc\u30bf\u306e\u30b5\u30a4\u30ba\u3068\u306e\u95a2\u4fc2\u3092\u53cd\u6620\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u5165\u529b\u30c7\u30fc\u30bf\u306e\u30b5\u30a4\u30ba\u304c\u5897\u52a0\u3059\u308b\u306b\u3064\u308c\u3066\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u5fc5\u8981\u306a\u6642\u9593\u3068\u7a7a\u9593\u306e\u5897\u52a0\u50be\u5411\u3092\u8a18\u8ff0\u3057\u307e\u3059\u3002\u3053\u306e\u5b9a\u7fa9\u306f\u8907\u96d1\u306b\u805e\u3053\u3048\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u3088\u308a\u826f\u304f\u7406\u89e3\u3059\u308b\u305f\u3081\u306b3\u3064\u306e\u91cd\u8981\u306a\u30dd\u30a4\u30f3\u30c8\u306b\u5206\u89e3\u3067\u304d\u307e\u3059\u3002

        • \u300c\u6642\u9593\u3068\u7a7a\u9593\u30ea\u30bd\u30fc\u30b9\u300d\u306f\u3001\u305d\u308c\u305e\u308c\u6642\u9593\u8a08\u7b97\u91cf\u3068\u7a7a\u9593\u8a08\u7b97\u91cf\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002
        • \u300c\u5165\u529b\u30c7\u30fc\u30bf\u306e\u30b5\u30a4\u30ba\u304c\u5897\u52a0\u3059\u308b\u306b\u3064\u308c\u3066\u300d\u306f\u3001\u8907\u96d1\u5ea6\u304c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3068\u5165\u529b\u30c7\u30fc\u30bf\u91cf\u3068\u306e\u95a2\u4fc2\u3092\u53cd\u6620\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002
        • \u300c\u6642\u9593\u3068\u7a7a\u9593\u306e\u5897\u52a0\u50be\u5411\u300d\u306f\u3001\u8907\u96d1\u5ea6\u89e3\u6790\u304c\u5b9f\u884c\u6642\u9593\u3084\u5360\u6709\u7a7a\u9593\u306e\u5177\u4f53\u7684\u306a\u5024\u3067\u306f\u306a\u304f\u3001\u6642\u9593\u3084\u7a7a\u9593\u304c\u5897\u52a0\u3059\u308b\u300c\u7387\u300d\u306b\u7126\u70b9\u3092\u5f53\u3066\u308b\u3053\u3068\u3092\u793a\u3057\u307e\u3059\u3002

        \u8907\u96d1\u5ea6\u89e3\u6790\u306f\u5b9f\u969b\u306e\u30c6\u30b9\u30c8\u65b9\u6cd5\u306e\u6b20\u70b9\u3092\u514b\u670d\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u4ee5\u4e0b\u306e\u5074\u9762\u3067\u53cd\u6620\u3055\u308c\u307e\u3059\uff1a

        • \u5b9f\u969b\u306b\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u306a\u3044\u305f\u3081\u3001\u3088\u308a\u74b0\u5883\u306b\u512a\u3057\u304f\u3001\u30a8\u30cd\u30eb\u30ae\u30fc\u52b9\u7387\u304c\u826f\u3044\u3067\u3059\u3002
        • \u30c6\u30b9\u30c8\u74b0\u5883\u306b\u4f9d\u5b58\u305b\u305a\u3001\u3059\u3079\u3066\u306e\u52d5\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u306b\u9069\u7528\u3067\u304d\u307e\u3059\u3002
        • \u7570\u306a\u308b\u30c7\u30fc\u30bf\u91cf\u3067\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u53cd\u6620\u3067\u304d\u3001\u7279\u306b\u5927\u91cf\u30c7\u30fc\u30bf\u3067\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6027\u80fd\u3092\u793a\u3057\u307e\u3059\u3002

        Tip

        \u8907\u96d1\u5ea6\u306e\u6982\u5ff5\u306b\u3064\u3044\u3066\u307e\u3060\u6df7\u4e71\u3057\u3066\u3044\u308b\u5834\u5408\u3067\u3082\u3001\u5fc3\u914d\u3057\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002\u4ee5\u964d\u306e\u7ae0\u3067\u8a73\u3057\u304f\u53d6\u308a\u4e0a\u3052\u307e\u3059\u3002

        \u8907\u96d1\u5ea6\u89e3\u6790\u306f\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u8a55\u4fa1\u3059\u308b\u300c\u3082\u306e\u3055\u3057\u300d\u3092\u63d0\u4f9b\u3057\u3001\u5b9f\u884c\u306b\u5fc5\u8981\u306a\u6642\u9593\u3068\u7a7a\u9593\u30ea\u30bd\u30fc\u30b9\u3092\u6e2c\u5b9a\u3057\u3001\u7570\u306a\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u6bd4\u8f03\u3059\u308b\u3053\u3068\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002

        \u8907\u96d1\u5ea6\u306f\u6570\u5b66\u7684\u6982\u5ff5\u3067\u3042\u308a\u3001\u521d\u5fc3\u8005\u306b\u306f\u62bd\u8c61\u7684\u3067\u56f0\u96e3\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u3053\u306e\u89b3\u70b9\u304b\u3089\u3001\u8907\u96d1\u5ea6\u89e3\u6790\u306f\u6700\u521d\u306b\u7d39\u4ecb\u3059\u308b\u306e\u306b\u6700\u3082\u9069\u3057\u305f\u30c8\u30d4\u30c3\u30af\u3067\u306f\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u3057\u304b\u3057\u3001\u7279\u5b9a\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u3084\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7279\u6027\u306b\u3064\u3044\u3066\u8b70\u8ad6\u3059\u308b\u3068\u304d\u3001\u305d\u306e\u901f\u5ea6\u3068\u7a7a\u9593\u4f7f\u7528\u91cf\u3092\u5206\u6790\u3059\u308b\u3053\u3068\u3092\u907f\u3051\u308b\u306e\u306f\u56f0\u96e3\u3067\u3059\u3002

        \u8981\u7d04\u3059\u308b\u3068\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u6df1\u304f\u5165\u308b\u524d\u306b\u8907\u96d1\u5ea6\u89e3\u6790\u306e\u57fa\u672c\u7684\u306a\u7406\u89e3\u3092\u8eab\u306b\u3064\u3051\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u7c21\u5358\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u8907\u96d1\u5ea6\u89e3\u6790\u3092\u5b9f\u884c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/space_complexity/","title":"2.4 \u00a0 \u7a7a\u9593\u8a08\u7b97\u91cf","text":"

        \u7a7a\u9593\u8a08\u7b97\u91cf\u306f\u3001\u30c7\u30fc\u30bf\u91cf\u304c\u5897\u52a0\u3059\u308b\u306b\u3064\u308c\u3066\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u5360\u6709\u3059\u308b\u30e1\u30e2\u30ea\u7a7a\u9593\u306e\u5897\u52a0\u50be\u5411\u3092\u6e2c\u5b9a\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u3053\u306e\u6982\u5ff5\u306f\u6642\u9593\u8a08\u7b97\u91cf\u3068\u975e\u5e38\u306b\u4f3c\u3066\u3044\u307e\u3059\u304c\u3001\u300c\u5b9f\u884c\u6642\u9593\u300d\u304c\u300c\u5360\u6709\u30e1\u30e2\u30ea\u7a7a\u9593\u300d\u306b\u7f6e\u304d\u63db\u3048\u3089\u308c\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/space_complexity/#241","title":"2.4.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u95a2\u9023\u3059\u308b\u7a7a\u9593","text":"

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u5b9f\u884c\u4e2d\u306b\u4f7f\u7528\u3059\u308b\u30e1\u30e2\u30ea\u7a7a\u9593\u306b\u306f\u3001\u4e3b\u306b\u4ee5\u4e0b\u306e\u7a2e\u985e\u304c\u3042\u308a\u307e\u3059\u3002

        • \u5165\u529b\u7a7a\u9593: \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5165\u529b\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        • \u4e00\u6642\u7a7a\u9593: \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u4e2d\u306b\u5909\u6570\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3001\u95a2\u6570\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3001\u305d\u306e\u4ed6\u306e\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        • \u51fa\u529b\u7a7a\u9593: \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u51fa\u529b\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002

        \u4e00\u822c\u7684\u306b\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306e\u7d71\u8a08\u7bc4\u56f2\u306b\u306f\u300c\u4e00\u6642\u7a7a\u9593\u300d\u3068\u300c\u51fa\u529b\u7a7a\u9593\u300d\u306e\u4e21\u65b9\u304c\u542b\u307e\u308c\u307e\u3059\u3002

        \u4e00\u6642\u7a7a\u9593\u306f\u3055\u3089\u306b3\u3064\u306e\u90e8\u5206\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        • \u4e00\u6642\u30c7\u30fc\u30bf: \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u4e2d\u306b\u69d8\u3005\u306a\u5b9a\u6570\u3001\u5909\u6570\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306a\u3069\u3092\u4fdd\u5b58\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        • \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593: \u547c\u3073\u51fa\u3055\u308c\u305f\u95a2\u6570\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u30b7\u30b9\u30c6\u30e0\u306f\u95a2\u6570\u304c\u547c\u3073\u51fa\u3055\u308c\u308b\u305f\u3073\u306b\u30b9\u30bf\u30c3\u30af\u306e\u9802\u4e0a\u306b\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u3092\u4f5c\u6210\u3057\u3001\u95a2\u6570\u304c\u8fd4\u3055\u308c\u305f\u5f8c\u306b\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u89e3\u653e\u3057\u307e\u3059\u3002
        • \u547d\u4ee4\u7a7a\u9593: \u30b3\u30f3\u30d1\u30a4\u30eb\u6e08\u307f\u30d7\u30ed\u30b0\u30e9\u30e0\u547d\u4ee4\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u3001\u5b9f\u969b\u306e\u7d71\u8a08\u3067\u306f\u901a\u5e38\u7121\u8996\u3067\u304d\u307e\u3059\u3002

        \u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u3092\u5206\u6790\u3059\u308b\u969b\u3001\u901a\u5e38\u306f\u4e00\u6642\u30c7\u30fc\u30bf\u3001\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3001\u51fa\u529b\u30c7\u30fc\u30bf\u3092\u30ab\u30a6\u30f3\u30c8\u3057\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        \u56f3 2-15 \u00a0 Space types used in algorithms

        \u95a2\u9023\u3059\u308b\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin
        class Node:\n    \"\"\"\u30af\u30e9\u30b9\"\"\"\n    def __init__(self, x: int):\n        self.val: int = x               # \u30ce\u30fc\u30c9\u5024\n        self.next: Node | None = None   # \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n\ndef function() -> int:\n    \"\"\"\u95a2\u6570\"\"\"\n    # \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c...\n    return 0\n\ndef algorithm(n) -> int:    # \u5165\u529b\u30c7\u30fc\u30bf\n    A = 0                   # \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5b9a\u6570\u3001\u901a\u5e38\u5927\u6587\u5b57\uff09\n    b = 0                   # \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5909\u6570\uff09\n    node = Node(0)          # \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\n    c = function()          # \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\uff08\u95a2\u6570\u547c\u3073\u51fa\u3057\uff09\n    return A + b + c        # \u51fa\u529b\u30c7\u30fc\u30bf\n
        /* \u69cb\u9020\u4f53 */\nstruct Node {\n    int val;\n    Node *next;\n    Node(int x) : val(x), next(nullptr) {}\n};\n\n/* \u95a2\u6570 */\nint func() {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c...\n    return 0;\n}\n\nint algorithm(int n) {          // \u5165\u529b\u30c7\u30fc\u30bf\n    const int a = 0;            // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5b9a\u6570\uff09\n    int b = 0;                  // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5909\u6570\uff09\n    Node* node = new Node(0);   // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\n    int c = func();             // \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\uff08\u95a2\u6570\u547c\u3073\u51fa\u3057\uff09\n    return a + b + c;           // \u51fa\u529b\u30c7\u30fc\u30bf\n}\n
        /* \u30af\u30e9\u30b9 */\nclass Node {\n    int val;\n    Node next;\n    Node(int x) { val = x; }\n}\n\n/* \u95a2\u6570 */\nint function() {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c...\n    return 0;\n}\n\nint algorithm(int n) {          // \u5165\u529b\u30c7\u30fc\u30bf\n    final int a = 0;            // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5b9a\u6570\uff09\n    int b = 0;                  // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5909\u6570\uff09\n    Node node = new Node(0);    // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\n    int c = function();         // \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\uff08\u95a2\u6570\u547c\u3073\u51fa\u3057\uff09\n    return a + b + c;           // \u51fa\u529b\u30c7\u30fc\u30bf\n}\n
        /* \u30af\u30e9\u30b9 */\nclass Node {\n    int val;\n    Node next;\n    Node(int x) { val = x; }\n}\n\n/* \u95a2\u6570 */\nint Function() {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c...\n    return 0;\n}\n\nint Algorithm(int n) {  // \u5165\u529b\u30c7\u30fc\u30bf\n    const int a = 0;    // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5b9a\u6570\uff09\n    int b = 0;          // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5909\u6570\uff09\n    Node node = new(0); // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\n    int c = Function(); // \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\uff08\u95a2\u6570\u547c\u3073\u51fa\u3057\uff09\n    return a + b + c;   // \u51fa\u529b\u30c7\u30fc\u30bf\n}\n
        /* \u69cb\u9020\u4f53 */\ntype node struct {\n    val  int\n    next *node\n}\n\n/* \u30ce\u30fc\u30c9\u69cb\u9020\u4f53\u3092\u4f5c\u6210 */\nfunc newNode(val int) *node {\n    return &node{val: val}\n}\n\n/* \u95a2\u6570 */\nfunc function() int {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c...\n    return 0\n}\n\nfunc algorithm(n int) int { // \u5165\u529b\u30c7\u30fc\u30bf\n    const a = 0             // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5b9a\u6570\uff09\n    b := 0                  // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5909\u6570\uff09\n    newNode(0)              // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\n    c := function()         // \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\uff08\u95a2\u6570\u547c\u3073\u51fa\u3057\uff09\n    return a + b + c        // \u51fa\u529b\u30c7\u30fc\u30bf\n}\n
        /* \u30af\u30e9\u30b9 */\nclass Node {\n    var val: Int\n    var next: Node?\n\n    init(x: Int) {\n        val = x\n    }\n}\n\n/* \u95a2\u6570 */\nfunc function() -> Int {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c...\n    return 0\n}\n\nfunc algorithm(n: Int) -> Int { // \u5165\u529b\u30c7\u30fc\u30bf\n    let a = 0                   // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5b9a\u6570\uff09\n    var b = 0                   // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5909\u6570\uff09\n    let node = Node(x: 0)       // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\n    let c = function()          // \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\uff08\u95a2\u6570\u547c\u3073\u51fa\u3057\uff09\n    return a + b + c            // \u51fa\u529b\u30c7\u30fc\u30bf\n}\n
        /* \u30af\u30e9\u30b9 */\nclass Node {\n    val;\n    next;\n    constructor(val) {\n        this.val = val === undefined ? 0 : val; // \u30ce\u30fc\u30c9\u5024\n        this.next = null;                       // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    }\n}\n\n/* \u95a2\u6570 */\nfunction constFunc() {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0;\n}\n\nfunction algorithm(n) {         // \u5165\u529b\u30c7\u30fc\u30bf\n    const a = 0;                // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5b9a\u6570\uff09\n    let b = 0;                  // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5909\u6570\uff09\n    const node = new Node(0);   // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\n    const c = constFunc();      // \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\uff08\u95a2\u6570\u547c\u3073\u51fa\u3057\uff09\n    return a + b + c;           // \u51fa\u529b\u30c7\u30fc\u30bf\n}\n
        /* \u30af\u30e9\u30b9 */\nclass Node {\n    val: number;\n    next: Node | null;\n    constructor(val?: number) {\n        this.val = val === undefined ? 0 : val; // \u30ce\u30fc\u30c9\u5024\n        this.next = null;                       // \u6b21\u306e\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    }\n}\n\n/* \u95a2\u6570 */\nfunction constFunc(): number {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0;\n}\n\nfunction algorithm(n: number): number { // \u5165\u529b\u30c7\u30fc\u30bf\n    const a = 0;                        // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5b9a\u6570\uff09\n    let b = 0;                          // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5909\u6570\uff09\n    const node = new Node(0);           // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\n    const c = constFunc();              // \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\uff08\u95a2\u6570\u547c\u3073\u51fa\u3057\uff09\n    return a + b + c;                   // \u51fa\u529b\u30c7\u30fc\u30bf\n}\n
        /* \u30af\u30e9\u30b9 */\nclass Node {\n  int val;\n  Node next;\n  Node(this.val, [this.next]);\n}\n\n/* \u95a2\u6570 */\nint function() {\n  // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c...\n  return 0;\n}\n\nint algorithm(int n) {  // \u5165\u529b\u30c7\u30fc\u30bf\n  const int a = 0;      // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5b9a\u6570\uff09\n  int b = 0;            // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5909\u6570\uff09\n  Node node = Node(0);  // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\n  int c = function();   // \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\uff08\u95a2\u6570\u547c\u3073\u51fa\u3057\uff09\n  return a + b + c;     // \u51fa\u529b\u30c7\u30fc\u30bf\n}\n
        use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u69cb\u9020\u4f53 */\nstruct Node {\n    val: i32,\n    next: Option<Rc<RefCell<Node>>>,\n}\n\n/* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\nimpl Node {\n    fn new(val: i32) -> Self {\n        Self { val: val, next: None }\n    }\n}\n\n/* \u95a2\u6570 */\nfn function() -> i32 {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c...\n    return 0;\n}\n\nfn algorithm(n: i32) -> i32 {   // \u5165\u529b\u30c7\u30fc\u30bf\n    const a: i32 = 0;           // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5b9a\u6570\uff09\n    let mut b = 0;              // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5909\u6570\uff09\n    let node = Node::new(0);    // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\n    let c = function();         // \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\uff08\u95a2\u6570\u547c\u3073\u51fa\u3057\uff09\n    return a + b + c;           // \u51fa\u529b\u30c7\u30fc\u30bf\n}\n
        /* \u95a2\u6570 */\nint func() {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c...\n    return 0;\n}\n\nint algorithm(int n) {  // \u5165\u529b\u30c7\u30fc\u30bf\n    const int a = 0;    // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5b9a\u6570\uff09\n    int b = 0;          // \u4e00\u6642\u30c7\u30fc\u30bf\uff08\u5909\u6570\uff09\n    int c = func();     // \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\uff08\u95a2\u6570\u547c\u3073\u51fa\u3057\uff09\n    return a + b + c;   // \u51fa\u529b\u30c7\u30fc\u30bf\n}\n
        \n
        "},{"location":"chapter_computational_complexity/space_complexity/#242","title":"2.4.2 \u00a0 \u8a08\u7b97\u65b9\u6cd5","text":"

        \u7a7a\u9593\u8a08\u7b97\u91cf\u3092\u8a08\u7b97\u3059\u308b\u65b9\u6cd5\u306f\u6642\u9593\u8a08\u7b97\u91cf\u3068\u307b\u307c\u540c\u69d8\u3067\u3001\u7d71\u8a08\u5bfe\u8c61\u3092\u300c\u64cd\u4f5c\u6570\u300d\u304b\u3089\u300c\u4f7f\u7528\u7a7a\u9593\u306e\u30b5\u30a4\u30ba\u300d\u306b\u5909\u66f4\u3059\u308b\u3060\u3051\u3067\u3059\u3002

        \u3057\u304b\u3057\u3001\u6642\u9593\u8a08\u7b97\u91cf\u3068\u306f\u7570\u306a\u308a\u3001\u901a\u5e38\u306f\u6700\u60aa\u30b1\u30fc\u30b9\u7a7a\u9593\u8a08\u7b97\u91cf\u306e\u307f\u306b\u7126\u70b9\u3092\u5f53\u3066\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u30e1\u30e2\u30ea\u7a7a\u9593\u304c\u30cf\u30fc\u30c9\u8981\u4ef6\u3067\u3042\u308a\u3001\u3059\u3079\u3066\u306e\u5165\u529b\u30c7\u30fc\u30bf\u306e\u4e0b\u3067\u5341\u5206\u306a\u30e1\u30e2\u30ea\u7a7a\u9593\u304c\u78ba\u4fdd\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3067\u3059\u3002

        \u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u6700\u60aa\u30b1\u30fc\u30b9\u7a7a\u9593\u8a08\u7b97\u91cf\u306e\u300c\u6700\u60aa\u30b1\u30fc\u30b9\u300d\u3068\u3044\u3046\u7528\u8a9e\u306b\u306f2\u3064\u306e\u610f\u5473\u304c\u3042\u308a\u307e\u3059\u3002

        1. \u6700\u60aa\u306e\u5165\u529b\u30c7\u30fc\u30bf\u306b\u57fa\u3065\u304f: \\(n < 10\\)\u306e\u5834\u5408\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u3067\u3059\u304c\u3001\\(n > 10\\)\u306e\u5834\u5408\u3001\u521d\u671f\u5316\u3055\u308c\u305f\u914d\u5217nums\u304c\\(O(n)\\)\u306e\u7a7a\u9593\u3092\u5360\u6709\u3059\u308b\u305f\u3081\u3001\u6700\u60aa\u30b1\u30fc\u30b9\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3059\u3002
        2. \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u4e2d\u306b\u4f7f\u7528\u3055\u308c\u308b\u30d4\u30fc\u30af\u30e1\u30e2\u30ea\u306b\u57fa\u3065\u304f: \u4f8b\u3048\u3070\u3001\u6700\u5f8c\u306e\u884c\u3092\u5b9f\u884c\u3059\u308b\u524d\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\\(O(1)\\)\u306e\u7a7a\u9593\u3092\u5360\u6709\u3057\u307e\u3059\u3002\u914d\u5217nums\u3092\u521d\u671f\u5316\u3059\u308b\u969b\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\\(O(n)\\)\u306e\u7a7a\u9593\u3092\u5360\u6709\u3059\u308b\u305f\u3081\u3001\u6700\u60aa\u30b1\u30fc\u30b9\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3059\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin
        def algorithm(n: int):\n    a = 0               # O(1)\n    b = [0] * 10000     # O(1)\n    if n > 10:\n        nums = [0] * n  # O(n)\n
        void algorithm(int n) {\n    int a = 0;               // O(1)\n    vector<int> b(10000);    // O(1)\n    if (n > 10)\n        vector<int> nums(n); // O(n)\n}\n
        void algorithm(int n) {\n    int a = 0;                   // O(1)\n    int[] b = new int[10000];    // O(1)\n    if (n > 10)\n        int[] nums = new int[n]; // O(n)\n}\n
        void Algorithm(int n) {\n    int a = 0;                   // O(1)\n    int[] b = new int[10000];    // O(1)\n    if (n > 10) {\n        int[] nums = new int[n]; // O(n)\n    }\n}\n
        func algorithm(n int) {\n    a := 0                      // O(1)\n    b := make([]int, 10000)     // O(1)\n    var nums []int\n    if n > 10 {\n        nums := make([]int, n)  // O(n)\n    }\n    fmt.Println(a, b, nums)\n}\n
        func algorithm(n: Int) {\n    let a = 0 // O(1)\n    let b = Array(repeating: 0, count: 10000) // O(1)\n    if n > 10 {\n        let nums = Array(repeating: 0, count: n) // O(n)\n    }\n}\n
        function algorithm(n) {\n    const a = 0;                   // O(1)\n    const b = new Array(10000);    // O(1)\n    if (n > 10) {\n        const nums = new Array(n); // O(n)\n    }\n}\n
        function algorithm(n: number): void {\n    const a = 0;                   // O(1)\n    const b = new Array(10000);    // O(1)\n    if (n > 10) {\n        const nums = new Array(n); // O(n)\n    }\n}\n
        void algorithm(int n) {\n  int a = 0;                            // O(1)\n  List<int> b = List.filled(10000, 0);  // O(1)\n  if (n > 10) {\n    List<int> nums = List.filled(n, 0); // O(n)\n  }\n}\n
        fn algorithm(n: i32) {\n    let a = 0;                           // O(1)\n    let b = [0; 10000];                  // O(1)\n    if n > 10 {\n        let nums = vec![0; n as usize];  // O(n)\n    }\n}\n
        void algorithm(int n) {\n    int a = 0;               // O(1)\n    int b[10000];            // O(1)\n    if (n > 10)\n        int nums[n] = {0};   // O(n)\n}\n
        \n

        \u518d\u5e30\u95a2\u6570\u3067\u306f\u3001\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u8003\u616e\u306b\u5165\u308c\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin
        def function() -> int:\n    # \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0\n\ndef loop(n: int):\n    \"\"\"\u30eb\u30fc\u30d7 O(1)\"\"\"\n    for _ in range(n):\n        function()\n\ndef recur(n: int):\n    \"\"\"\u518d\u5e30 O(n)\"\"\"\n    if n == 1:\n        return\n    return recur(n - 1)\n
        int func() {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0;\n}\n/* \u30b5\u30a4\u30af\u30eb O(1) */\nvoid loop(int n) {\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n/* \u518d\u5e30 O(n) */\nvoid recur(int n) {\n    if (n == 1) return;\n    recur(n - 1);\n}\n
        int function() {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0;\n}\n/* \u30b5\u30a4\u30af\u30eb O(1) */\nvoid loop(int n) {\n    for (int i = 0; i < n; i++) {\n        function();\n    }\n}\n/* \u518d\u5e30 O(n) */\nvoid recur(int n) {\n    if (n == 1) return;\n    recur(n - 1);\n}\n
        int Function() {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0;\n}\n/* \u30b5\u30a4\u30af\u30eb O(1) */\nvoid Loop(int n) {\n    for (int i = 0; i < n; i++) {\n        Function();\n    }\n}\n/* \u518d\u5e30 O(n) */\nint Recur(int n) {\n    if (n == 1) return 1;\n    return Recur(n - 1);\n}\n
        func function() int {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0\n}\n\n/* \u30b5\u30a4\u30af\u30eb O(1) */\nfunc loop(n int) {\n    for i := 0; i < n; i++ {\n        function()\n    }\n}\n\n/* \u518d\u5e30 O(n) */\nfunc recur(n int) {\n    if n == 1 {\n        return\n    }\n    recur(n - 1)\n}\n
        @discardableResult\nfunc function() -> Int {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0\n}\n\n/* \u30b5\u30a4\u30af\u30eb O(1) */\nfunc loop(n: Int) {\n    for _ in 0 ..< n {\n        function()\n    }\n}\n\n/* \u518d\u5e30 O(n) */\nfunc recur(n: Int) {\n    if n == 1 {\n        return\n    }\n    recur(n: n - 1)\n}\n
        function constFunc() {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0;\n}\n/* \u30b5\u30a4\u30af\u30eb O(1) */\nfunction loop(n) {\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n/* \u518d\u5e30 O(n) */\nfunction recur(n) {\n    if (n === 1) return;\n    return recur(n - 1);\n}\n
        function constFunc(): number {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0;\n}\n/* \u30b5\u30a4\u30af\u30eb O(1) */\nfunction loop(n: number): void {\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n/* \u518d\u5e30 O(n) */\nfunction recur(n: number): void {\n    if (n === 1) return;\n    return recur(n - 1);\n}\n
        int function() {\n  // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n  return 0;\n}\n/* \u30b5\u30a4\u30af\u30eb O(1) */\nvoid loop(int n) {\n  for (int i = 0; i < n; i++) {\n    function();\n  }\n}\n/* \u518d\u5e30 O(n) */\nvoid recur(int n) {\n  if (n == 1) return;\n  recur(n - 1);\n}\n
        fn function() -> i32 {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0;\n}\n/* \u30b5\u30a4\u30af\u30eb O(1) */\nfn loop(n: i32) {\n    for i in 0..n {\n        function();\n    }\n}\n/* \u518d\u5e30 O(n) */\nvoid recur(n: i32) {\n    if n == 1 {\n        return;\n    }\n    recur(n - 1);\n}\n
        int func() {\n    // \u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0;\n}\n/* \u30b5\u30a4\u30af\u30eb O(1) */\nvoid loop(int n) {\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n/* \u518d\u5e30 O(n) */\nvoid recur(int n) {\n    if (n == 1) return;\n    recur(n - 1);\n}\n
        \n

        loop()\u95a2\u6570\u3068recur()\u95a2\u6570\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\u4e21\u65b9\u3068\u3082\\(O(n)\\)\u3067\u3059\u304c\u3001\u305d\u308c\u3089\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\u7570\u306a\u308a\u307e\u3059\u3002

        • loop()\u95a2\u6570\u306f\u30eb\u30fc\u30d7\u5185\u3067function()\u3092\\(n\\)\u56de\u547c\u3073\u51fa\u3057\u3001\u5404\u53cd\u5fa9\u306efunction()\u306f\u8fd4\u3063\u3066\u305d\u306e\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u89e3\u653e\u3059\u308b\u305f\u3081\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u306e\u307e\u307e\u3067\u3059\u3002
        • \u518d\u5e30\u95a2\u6570recur()\u306f\u5b9f\u884c\u4e2d\u306b\\(n\\)\u500b\u306e\u672a\u8fd4\u5374\u306erecur()\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u540c\u6642\u306b\u5b58\u5728\u3059\u308b\u305f\u3081\u3001\\(O(n)\\)\u306e\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u5360\u6709\u3057\u307e\u3059\u3002
        "},{"location":"chapter_computational_complexity/space_complexity/#243","title":"2.4.3 \u00a0 \u4e00\u822c\u7684\u306a\u7a2e\u985e","text":"

        \u5165\u529b\u30c7\u30fc\u30bf\u306e\u30b5\u30a4\u30ba\u3092\\(n\\)\u3068\u3059\u308b\u3068\u3001\u4e0b\u56f3\u306f\u4e00\u822c\u7684\u306a\u7a7a\u9593\u8a08\u7b97\u91cf\u306e\u7a2e\u985e\u3092\u793a\u3057\u3066\u3044\u307e\u3059\uff08\u4f4e\u3044\u3082\u306e\u304b\u3089\u9ad8\u3044\u3082\u306e\u3078\u3068\u4e26\u3079\u3089\u308c\u3066\u3044\u307e\u3059\uff09\u3002

        \\[ \\begin{aligned} & O(1) < O(\\log n) < O(n) < O(n^2) < O(2^n) \\newline & \\text{\u5b9a\u6570} < \\text{\u5bfe\u6570} < \\text{\u7dda\u5f62} < \\text{\u4e8c\u6b21} < \\text{\u6307\u6570} \\end{aligned} \\]

        \u56f3 2-16 \u00a0 Common types of space complexity

        "},{"location":"chapter_computational_complexity/space_complexity/#1-o1","title":"1. \u00a0 \u5b9a\u6570\u30aa\u30fc\u30c0\u30fc \\(O(1)\\)","text":"

        \u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\u306f\u3001\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\\(n\\)\u3068\u306f\u7121\u95a2\u4fc2\u306a\u5b9a\u6570\u3001\u5909\u6570\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u4e00\u822c\u7684\u3067\u3059\u3002

        \u30eb\u30fc\u30d7\u3067\u5909\u6570\u3092\u521d\u671f\u5316\u3057\u305f\u308a\u95a2\u6570\u3092\u547c\u3073\u51fa\u3057\u305f\u308a\u3059\u308b\u305f\u3081\u306b\u5360\u6709\u3055\u308c\u308b\u30e1\u30e2\u30ea\u306f\u3001\u6b21\u306e\u30b5\u30a4\u30af\u30eb\u306b\u5165\u308b\u969b\u306b\u89e3\u653e\u3055\u308c\u3001\u7a7a\u9593\u4e0a\u3067\u7d2f\u7a4d\u3055\u308c\u306a\u3044\u305f\u3081\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u306e\u307e\u307e\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def function() -> int:\n    \"\"\"\u95a2\u6570\"\"\"\n    # \u4f55\u3089\u304b\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0\n\ndef constant(n: int):\n    \"\"\"\u5b9a\u6570\u8907\u96d1\u5ea6\"\"\"\n    # \u5b9a\u6570\u3001\u5909\u6570\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f O(1) \u306e\u30b9\u30da\u30fc\u30b9\u3092\u5360\u6709\n    a = 0\n    nums = [0] * 10000\n    node = ListNode(0)\n    # \u30eb\u30fc\u30d7\u5185\u306e\u5909\u6570\u306f O(1) \u306e\u30b9\u30da\u30fc\u30b9\u3092\u5360\u6709\n    for _ in range(n):\n        c = 0\n    # \u30eb\u30fc\u30d7\u5185\u306e\u95a2\u6570\u306f O(1) \u306e\u30b9\u30da\u30fc\u30b9\u3092\u5360\u6709\n    for _ in range(n):\n        function()\n
        space_complexity.cpp
        /* \u95a2\u6570 */\nint func() {\n    // \u4f55\u3089\u304b\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0;\n}\n\n/* \u5b9a\u6570\u8a08\u7b97\u91cf */\nvoid constant(int n) {\n    // \u5b9a\u6570\u3001\u5909\u6570\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f O(1) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    const int a = 0;\n    int b = 0;\n    vector<int> nums(10000);\n    ListNode node(0);\n    // \u30eb\u30fc\u30d7\u5185\u306e\u5909\u6570\u306f O(1) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // \u30eb\u30fc\u30d7\u5185\u306e\u95a2\u6570\u306f O(1) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n
        space_complexity.java
        /* \u95a2\u6570 */\nint function() {\n    // \u4f55\u3089\u304b\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\n    return 0;\n}\n\n/* \u5b9a\u6570\u8a08\u7b97\u91cf */\nvoid constant(int n) {\n    // \u5b9a\u6570\u3001\u5909\u6570\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f O(1) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    final int a = 0;\n    int b = 0;\n    int[] nums = new int[10000];\n    ListNode node = new ListNode(0);\n    // \u30eb\u30fc\u30d7\u5185\u306e\u5909\u6570\u306f O(1) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // \u30eb\u30fc\u30d7\u5185\u306e\u95a2\u6570\u306f O(1) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    for (int i = 0; i < n; i++) {\n        function();\n    }\n}\n
        space_complexity.cs
        [class]{space_complexity}-[func]{Function}\n\n[class]{space_complexity}-[func]{Constant}\n
        space_complexity.go
        [class]{}-[func]{function}\n\n[class]{}-[func]{spaceConstant}\n
        space_complexity.swift
        [class]{}-[func]{function}\n\n[class]{}-[func]{constant}\n
        space_complexity.js
        [class]{}-[func]{constFunc}\n\n[class]{}-[func]{constant}\n
        space_complexity.ts
        [class]{}-[func]{constFunc}\n\n[class]{}-[func]{constant}\n
        space_complexity.dart
        [class]{}-[func]{function}\n\n[class]{}-[func]{constant}\n
        space_complexity.rs
        [class]{}-[func]{function}\n\n[class]{}-[func]{constant}\n
        space_complexity.c
        [class]{}-[func]{func}\n\n[class]{}-[func]{constant}\n
        space_complexity.kt
        [class]{}-[func]{function}\n\n[class]{}-[func]{constant}\n
        space_complexity.rb
        [class]{}-[func]{function}\n\n[class]{}-[func]{constant}\n
        "},{"location":"chapter_computational_complexity/space_complexity/#2-on","title":"2. \u00a0 \u7dda\u5f62\u30aa\u30fc\u30c0\u30fc \\(O(n)\\)","text":"

        \u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\u306f\u914d\u5217\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u30b9\u30bf\u30c3\u30af\u3001\u30ad\u30e5\u30fc\u306a\u3069\u3067\u4e00\u822c\u7684\u3067\u3001\u8981\u7d20\u6570\u306f\\(n\\)\u306b\u6bd4\u4f8b\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def linear(n: int):\n    \"\"\"\u7dda\u5f62\u8907\u96d1\u5ea6\"\"\"\n    # \u9577\u3055 n \u306e\u30ea\u30b9\u30c8\u306f O(n) \u306e\u30b9\u30da\u30fc\u30b9\u3092\u5360\u6709\n    nums = [0] * n\n    # \u9577\u3055 n \u306e\u30cf\u30c3\u30b7\u30e5\u30de\u30c3\u30d7\u306f O(n) \u306e\u30b9\u30da\u30fc\u30b9\u3092\u5360\u6709\n    hmap = dict[int, str]()\n    for i in range(n):\n        hmap[i] = str(i)\n
        space_complexity.cpp
        /* \u7dda\u5f62\u8a08\u7b97\u91cf */\nvoid linear(int n) {\n    // \u9577\u3055 n \u306e\u914d\u5217\u306f O(n) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    vector<int> nums(n);\n    // \u9577\u3055 n \u306e\u30ea\u30b9\u30c8\u306f O(n) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    vector<ListNode> nodes;\n    for (int i = 0; i < n; i++) {\n        nodes.push_back(ListNode(i));\n    }\n    // \u9577\u3055 n \u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306f O(n) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    unordered_map<int, string> map;\n    for (int i = 0; i < n; i++) {\n        map[i] = to_string(i);\n    }\n}\n
        space_complexity.java
        /* \u7dda\u5f62\u8a08\u7b97\u91cf */\nvoid linear(int n) {\n    // \u9577\u3055 n \u306e\u914d\u5217\u306f O(n) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    int[] nums = new int[n];\n    // \u9577\u3055 n \u306e\u30ea\u30b9\u30c8\u306f O(n) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    List<ListNode> nodes = new ArrayList<>();\n    for (int i = 0; i < n; i++) {\n        nodes.add(new ListNode(i));\n    }\n    // \u9577\u3055 n \u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306f O(n) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    Map<Integer, String> map = new HashMap<>();\n    for (int i = 0; i < n; i++) {\n        map.put(i, String.valueOf(i));\n    }\n}\n
        space_complexity.cs
        [class]{space_complexity}-[func]{Linear}\n
        space_complexity.go
        [class]{}-[func]{spaceLinear}\n
        space_complexity.swift
        [class]{}-[func]{linear}\n
        space_complexity.js
        [class]{}-[func]{linear}\n
        space_complexity.ts
        [class]{}-[func]{linear}\n
        space_complexity.dart
        [class]{}-[func]{linear}\n
        space_complexity.rs
        [class]{}-[func]{linear}\n
        space_complexity.c
        [class]{HashTable}-[func]{}\n\n[class]{}-[func]{linear}\n
        space_complexity.kt
        [class]{}-[func]{linear}\n
        space_complexity.rb
        [class]{}-[func]{linear}\n

        \u4e0b\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u3001\u3053\u306e\u95a2\u6570\u306e\u518d\u5e30\u6df1\u5ea6\u306f\\(n\\)\u3067\u3001\\(n\\)\u500b\u306e\u672a\u8fd4\u5374\u306elinear_recur()\u95a2\u6570\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u3042\u308a\u3001\\(O(n)\\)\u30b5\u30a4\u30ba\u306e\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def linear_recur(n: int):\n    \"\"\"\u7dda\u5f62\u8907\u96d1\u5ea6\uff08\u518d\u5e30\u5b9f\u88c5\uff09\"\"\"\n    print(\"\u518d\u5e30 n =\", n)\n    if n == 1:\n        return\n    linear_recur(n - 1)\n
        space_complexity.cpp
        /* \u7dda\u5f62\u8a08\u7b97\u91cf\uff08\u518d\u5e30\u5b9f\u88c5\uff09 */\nvoid linearRecur(int n) {\n    cout << \"\u518d\u5e30 n = \" << n << endl;\n    if (n == 1)\n        return;\n    linearRecur(n - 1);\n}\n
        space_complexity.java
        /* \u7dda\u5f62\u8a08\u7b97\u91cf\uff08\u518d\u5e30\u5b9f\u88c5\uff09 */\nvoid linearRecur(int n) {\n    System.out.println(\"\u518d\u5e30 n = \" + n);\n    if (n == 1)\n        return;\n    linearRecur(n - 1);\n}\n
        space_complexity.cs
        [class]{space_complexity}-[func]{LinearRecur}\n
        space_complexity.go
        [class]{}-[func]{spaceLinearRecur}\n
        space_complexity.swift
        [class]{}-[func]{linearRecur}\n
        space_complexity.js
        [class]{}-[func]{linearRecur}\n
        space_complexity.ts
        [class]{}-[func]{linearRecur}\n
        space_complexity.dart
        [class]{}-[func]{linearRecur}\n
        space_complexity.rs
        [class]{}-[func]{linear_recur}\n
        space_complexity.c
        [class]{}-[func]{linearRecur}\n
        space_complexity.kt
        [class]{}-[func]{linearRecur}\n
        space_complexity.rb
        [class]{}-[func]{linear_recur}\n

        \u56f3 2-17 \u00a0 Recursive function generating linear order space complexity

        "},{"location":"chapter_computational_complexity/space_complexity/#3-on2","title":"3. \u00a0 \u4e8c\u6b21\u30aa\u30fc\u30c0\u30fc \\(O(n^2)\\)","text":"

        \u4e8c\u6b21\u30aa\u30fc\u30c0\u30fc\u306f\u884c\u5217\u3084\u30b0\u30e9\u30d5\u3067\u4e00\u822c\u7684\u3067\u3001\u8981\u7d20\u6570\u306f\\(n\\)\u306e\u4e8c\u4e57\u306b\u6bd4\u4f8b\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def quadratic(n: int):\n    \"\"\"\u5e73\u65b9\u8907\u96d1\u5ea6\"\"\"\n    # \u4e8c\u6b21\u5143\u30ea\u30b9\u30c8\u306f O(n^2) \u306e\u30b9\u30da\u30fc\u30b9\u3092\u5360\u6709\n    num_matrix = [[0] * n for _ in range(n)]\n
        space_complexity.cpp
        /* \u4e8c\u6b21\u8a08\u7b97\u91cf */\nvoid quadratic(int n) {\n    // \u4e8c\u6b21\u5143\u30ea\u30b9\u30c8\u306f O(n^2) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    vector<vector<int>> numMatrix;\n    for (int i = 0; i < n; i++) {\n        vector<int> tmp;\n        for (int j = 0; j < n; j++) {\n            tmp.push_back(0);\n        }\n        numMatrix.push_back(tmp);\n    }\n}\n
        space_complexity.java
        /* \u4e8c\u6b21\u8a08\u7b97\u91cf */\nvoid quadratic(int n) {\n    // \u884c\u5217\u306f O(n^2) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    int[][] numMatrix = new int[n][n];\n    // \u4e8c\u6b21\u5143\u30ea\u30b9\u30c8\u306f O(n^2) \u7a7a\u9593\u3092\u5360\u3081\u308b\n    List<List<Integer>> numList = new ArrayList<>();\n    for (int i = 0; i < n; i++) {\n        List<Integer> tmp = new ArrayList<>();\n        for (int j = 0; j < n; j++) {\n            tmp.add(0);\n        }\n        numList.add(tmp);\n    }\n}\n
        space_complexity.cs
        [class]{space_complexity}-[func]{Quadratic}\n
        space_complexity.go
        [class]{}-[func]{spaceQuadratic}\n
        space_complexity.swift
        [class]{}-[func]{quadratic}\n
        space_complexity.js
        [class]{}-[func]{quadratic}\n
        space_complexity.ts
        [class]{}-[func]{quadratic}\n
        space_complexity.dart
        [class]{}-[func]{quadratic}\n
        space_complexity.rs
        [class]{}-[func]{quadratic}\n
        space_complexity.c
        [class]{}-[func]{quadratic}\n
        space_complexity.kt
        [class]{}-[func]{quadratic}\n
        space_complexity.rb
        [class]{}-[func]{quadratic}\n

        \u4e0b\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u3001\u3053\u306e\u95a2\u6570\u306e\u518d\u5e30\u6df1\u5ea6\u306f\\(n\\)\u3067\u3001\u5404\u518d\u5e30\u547c\u3073\u51fa\u3057\u3067\u9577\u3055\\(n\\)\u3001\\(n-1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\)\u306e\u914d\u5217\u304c\u521d\u671f\u5316\u3055\u308c\u3001\u5e73\u5747\\(n/2\\)\u3068\u306a\u308a\u3001\u5168\u4f53\u3068\u3057\u3066\\(O(n^2)\\)\u306e\u7a7a\u9593\u3092\u5360\u6709\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def quadratic_recur(n: int) -> int:\n    \"\"\"\u5e73\u65b9\u8907\u96d1\u5ea6\uff08\u518d\u5e30\u5b9f\u88c5\uff09\"\"\"\n    if n <= 0:\n        return 0\n    nums = [0] * n\n    print(f\"\u518d\u5e30 n = {n} \u306e\u4e2d\u3067\u914d\u5217\u306e\u9577\u3055 = {len(nums)}\")\n    return quadratic_recur(n - 1)\n
        space_complexity.cpp
        /* \u4e8c\u6b21\u8a08\u7b97\u91cf\uff08\u518d\u5e30\u5b9f\u88c5\uff09 */\nint quadraticRecur(int n) {\n    if (n <= 0)\n        return 0;\n    vector<int> nums(n);\n    cout << \"\u518d\u5e30 n = \" << n << \", nums \u306e\u9577\u3055 = \" << nums.size() << endl;\n    return quadraticRecur(n - 1);\n}\n
        space_complexity.java
        /* \u4e8c\u6b21\u8a08\u7b97\u91cf\uff08\u518d\u5e30\u5b9f\u88c5\uff09 */\nint quadraticRecur(int n) {\n    if (n <= 0)\n        return 0;\n    // \u914d\u5217 nums \u306e\u9577\u3055 = n, n-1, ..., 2, 1\n    int[] nums = new int[n];\n    System.out.println(\"\u518d\u5e30 n = \" + n + \" \u306e nums \u306e\u9577\u3055 = \" + nums.length);\n    return quadraticRecur(n - 1);\n}\n
        space_complexity.cs
        [class]{space_complexity}-[func]{QuadraticRecur}\n
        space_complexity.go
        [class]{}-[func]{spaceQuadraticRecur}\n
        space_complexity.swift
        [class]{}-[func]{quadraticRecur}\n
        space_complexity.js
        [class]{}-[func]{quadraticRecur}\n
        space_complexity.ts
        [class]{}-[func]{quadraticRecur}\n
        space_complexity.dart
        [class]{}-[func]{quadraticRecur}\n
        space_complexity.rs
        [class]{}-[func]{quadratic_recur}\n
        space_complexity.c
        [class]{}-[func]{quadraticRecur}\n
        space_complexity.kt
        [class]{}-[func]{quadraticRecur}\n
        space_complexity.rb
        [class]{}-[func]{quadratic_recur}\n

        \u56f3 2-18 \u00a0 Recursive function generating quadratic order space complexity

        "},{"location":"chapter_computational_complexity/space_complexity/#4-o2n","title":"4. \u00a0 \u6307\u6570\u30aa\u30fc\u30c0\u30fc \\(O(2^n)\\)","text":"

        \u6307\u6570\u30aa\u30fc\u30c0\u30fc\u306f\u4e8c\u5206\u6728\u3067\u4e00\u822c\u7684\u3067\u3059\u3002\u4e0b\u56f3\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\\(n\\)\u30ec\u30d9\u30eb\u306e\u300c\u5b8c\u5168\u4e8c\u5206\u6728\u300d\u306f\\(2^n - 1\\)\u500b\u306e\u30ce\u30fc\u30c9\u3092\u6301\u3061\u3001\\(O(2^n)\\)\u306e\u7a7a\u9593\u3092\u5360\u6709\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def build_tree(n: int) -> TreeNode | None:\n    \"\"\"\u6307\u6570\u8907\u96d1\u5ea6\uff08\u5b8c\u5168\u4e8c\u5206\u6728\u306e\u69cb\u7bc9\uff09\"\"\"\n    if n == 0:\n        return None\n    root = TreeNode(0)\n    root.left = build_tree(n - 1)\n    root.right = build_tree(n - 1)\n    return root\n
        space_complexity.cpp
        /* \u6307\u6570\u8a08\u7b97\u91cf\uff08\u5b8c\u5168\u4e8c\u5206\u6728\u306e\u69cb\u7bc9\uff09 */\nTreeNode *buildTree(int n) {\n    if (n == 0)\n        return nullptr;\n    TreeNode *root = new TreeNode(0);\n    root->left = buildTree(n - 1);\n    root->right = buildTree(n - 1);\n    return root;\n}\n
        space_complexity.java
        /* \u6307\u6570\u8a08\u7b97\u91cf\uff08\u5b8c\u5168\u4e8c\u5206\u6728\u306e\u69cb\u7bc9\uff09 */\nTreeNode buildTree(int n) {\n    if (n == 0)\n        return null;\n    TreeNode root = new TreeNode(0);\n    root.left = buildTree(n - 1);\n    root.right = buildTree(n - 1);\n    return root;\n}\n
        space_complexity.cs
        [class]{space_complexity}-[func]{BuildTree}\n
        space_complexity.go
        [class]{}-[func]{buildTree}\n
        space_complexity.swift
        [class]{}-[func]{buildTree}\n
        space_complexity.js
        [class]{}-[func]{buildTree}\n
        space_complexity.ts
        [class]{}-[func]{buildTree}\n
        space_complexity.dart
        [class]{}-[func]{buildTree}\n
        space_complexity.rs
        [class]{}-[func]{build_tree}\n
        space_complexity.c
        [class]{}-[func]{buildTree}\n
        space_complexity.kt
        [class]{}-[func]{buildTree}\n
        space_complexity.rb
        [class]{}-[func]{build_tree}\n

        \u56f3 2-19 \u00a0 Full binary tree generating exponential order space complexity

        "},{"location":"chapter_computational_complexity/space_complexity/#5-olog-n","title":"5. \u00a0 \u5bfe\u6570\u30aa\u30fc\u30c0\u30fc \\(O(\\log n)\\)","text":"

        \u5bfe\u6570\u30aa\u30fc\u30c0\u30fc\u306f\u5206\u5272\u7d71\u6cbb\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u4e00\u822c\u7684\u3067\u3059\u3002\u4f8b\u3048\u3070\u3001\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u3067\u306f\u3001\u9577\u3055\\(n\\)\u306e\u914d\u5217\u304c\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u518d\u5e30\u7684\u306b\u534a\u5206\u306b\u5206\u5272\u3055\u308c\u3001\u9ad8\u3055\\(\\log n\\)\u306e\u518d\u5e30\u6728\u3092\u5f62\u6210\u3057\u3001\\(O(\\log n)\\)\u306e\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        \u5225\u306e\u4f8b\u306f\u3001\u6570\u5024\u3092\u6587\u5b57\u5217\u306b\u5909\u63db\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u6b63\u306e\u6574\u6570\\(n\\)\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u305d\u306e\u6841\u6570\u306f\\(\\log_{10} n + 1\\)\u3067\u3001\u6587\u5b57\u5217\u306e\u9577\u3055\u306b\u5bfe\u5fdc\u3059\u308b\u305f\u3081\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(\\log_{10} n + 1) = O(\\log n)\\)\u3067\u3059\u3002

        "},{"location":"chapter_computational_complexity/space_complexity/#244","title":"2.4.4 \u00a0 \u6642\u9593\u3068\u7a7a\u9593\u306e\u30d0\u30e9\u30f3\u30b9","text":"

        \u7406\u60f3\u7684\u306b\u306f\u3001\u6642\u9593\u8a08\u7b97\u91cf\u3068\u7a7a\u9593\u8a08\u7b97\u91cf\u306e\u4e21\u65b9\u304c\u6700\u9069\u3067\u3042\u308b\u3053\u3068\u3092\u76ee\u6307\u3057\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u5b9f\u969b\u306b\u306f\u4e21\u65b9\u3092\u540c\u6642\u306b\u6700\u9069\u5316\u3059\u308b\u3053\u3068\u306f\u3057\u3070\u3057\u3070\u56f0\u96e3\u3067\u3059\u3002

        \u6642\u9593\u8a08\u7b97\u91cf\u3092\u4e0b\u3052\u308b\u3053\u3068\u306f\u901a\u5e38\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306e\u5897\u52a0\u3092\u4ee3\u511f\u3068\u3057\u3001\u305d\u306e\u9006\u3082\u540c\u69d8\u3067\u3059\u3002\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u901f\u5ea6\u3092\u5411\u4e0a\u3055\u305b\u308b\u305f\u3081\u306b\u30e1\u30e2\u30ea\u7a7a\u9593\u3092\u72a0\u7272\u306b\u3059\u308b\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\u300c\u6642\u7a7a\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u300d\u3068\u3057\u3066\u77e5\u3089\u308c\u3001\u305d\u306e\u9006\u306f\u300c\u7a7a\u6642\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u300d\u3068\u3057\u3066\u77e5\u3089\u308c\u3066\u3044\u307e\u3059\u3002

        \u9078\u629e\u306f\u3001\u3069\u3061\u3089\u306e\u5074\u9762\u3092\u3088\u308a\u91cd\u8996\u3059\u308b\u304b\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u6642\u9593\u306f\u7a7a\u9593\u3088\u308a\u3082\u8cb4\u91cd\u3067\u3042\u308b\u305f\u3081\u3001\u300c\u6642\u7a7a\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u300d\u304c\u3088\u308a\u4e00\u822c\u7684\u306a\u6226\u7565\u3067\u3059\u3002\u3082\u3061\u308d\u3093\u3001\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u6271\u3046\u969b\u306f\u7a7a\u9593\u8a08\u7b97\u91cf\u3092\u5236\u5fa1\u3059\u308b\u3053\u3068\u3082\u975e\u5e38\u306b\u91cd\u8981\u3067\u3059\u3002

        "},{"location":"chapter_computational_complexity/summary/","title":"2.5 \u00a0 \u307e\u3068\u3081","text":""},{"location":"chapter_computational_complexity/summary/#1","title":"1. \u00a0 \u91cd\u8981\u306a\u30ec\u30d3\u30e5\u30fc","text":"

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u52b9\u7387\u8a55\u4fa1

        • \u6642\u9593\u52b9\u7387\u3068\u7a7a\u9593\u52b9\u7387\u306f\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u512a\u52a3\u3092\u8a55\u4fa1\u3059\u308b2\u3064\u306e\u4e3b\u8981\u306a\u57fa\u6e96\u3067\u3059\u3002
        • \u5b9f\u969b\u306e\u30c6\u30b9\u30c8\u306b\u3088\u3063\u3066\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u8a55\u4fa1\u3067\u304d\u307e\u3059\u304c\u3001\u30c6\u30b9\u30c8\u74b0\u5883\u306e\u5f71\u97ff\u3092\u6392\u9664\u3059\u308b\u3053\u3068\u306f\u56f0\u96e3\u3067\u3001\u5927\u91cf\u306e\u8a08\u7b97\u30ea\u30bd\u30fc\u30b9\u3092\u6d88\u8cbb\u3057\u307e\u3059\u3002
        • \u8907\u96d1\u5ea6\u5206\u6790\u306f\u5b9f\u969b\u306e\u30c6\u30b9\u30c8\u306e\u6b20\u70b9\u3092\u514b\u670d\u3067\u304d\u307e\u3059\u3002\u305d\u306e\u7d50\u679c\u306f\u3059\u3079\u3066\u306e\u52d5\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u306b\u9069\u7528\u3067\u304d\u3001\u7570\u306a\u308b\u30c7\u30fc\u30bf\u30b9\u30b1\u30fc\u30eb\u3067\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u660e\u3089\u304b\u306b\u3067\u304d\u307e\u3059\u3002

        \u6642\u9593\u8a08\u7b97\u91cf

        • \u6642\u9593\u8a08\u7b97\u91cf\u306f\u3001\u30c7\u30fc\u30bf\u91cf\u306e\u5897\u52a0\u306b\u4f34\u3046\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u6642\u9593\u306e\u50be\u5411\u3092\u6e2c\u5b9a\u3057\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u52b9\u679c\u7684\u306b\u8a55\u4fa1\u3057\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u5165\u529b\u30c7\u30fc\u30bf\u91cf\u304c\u5c11\u306a\u3044\u5834\u5408\u3084\u6642\u9593\u8a08\u7b97\u91cf\u304c\u540c\u3058\u5834\u5408\u306a\u3069\u3001\u7279\u5b9a\u306e\u30b1\u30fc\u30b9\u3067\u306f\u5931\u6557\u3059\u308b\u3053\u3068\u304c\u3042\u308a\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u6b63\u78ba\u306b\u6bd4\u8f03\u3059\u308b\u3053\u3068\u304c\u56f0\u96e3\u306b\u306a\u308a\u307e\u3059\u3002
        • \u6700\u60aa\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\u306f\u30d3\u30c3\u30b0\\(O\\)\u8a18\u6cd5\u3092\u4f7f\u7528\u3057\u3066\u8868\u8a18\u3055\u308c\u3001\u6f38\u8fd1\u4e0a\u9650\u3092\u8868\u3057\u3001\\(n\\)\u304c\u7121\u9650\u5927\u306b\u8fd1\u3065\u304f\u306b\u3064\u308c\u3066\u306e\u64cd\u4f5c\u6570\\(T(n)\\)\u306e\u5897\u52a0\u30ec\u30d9\u30eb\u3092\u53cd\u6620\u3057\u307e\u3059\u3002
        • \u6642\u9593\u8a08\u7b97\u91cf\u306e\u8a08\u7b97\u306b\u306f2\u3064\u306e\u30b9\u30c6\u30c3\u30d7\u304c\u542b\u307e\u308c\u307e\u3059\uff1a\u307e\u305a\u64cd\u4f5c\u6570\u3092\u30ab\u30a6\u30f3\u30c8\u3057\u3001\u6b21\u306b\u6f38\u8fd1\u4e0a\u9650\u3092\u6c7a\u5b9a\u3057\u307e\u3059\u3002
        • \u4e00\u822c\u7684\u306a\u6642\u9593\u8a08\u7b97\u91cf\u306f\u3001\u4f4e\u3044\u3082\u306e\u304b\u3089\u9ad8\u3044\u3082\u306e\u3078\u3068\u4e26\u3079\u308b\u3068\u3001\\(O(1)\\)\u3001\\(O(\\log n)\\)\u3001\\(O(n)\\)\u3001\\(O(n \\log n)\\)\u3001\\(O(n^2)\\)\u3001\\(O(2^n)\\)\u3001\\(O(n!)\\)\u306a\u3069\u304c\u542b\u307e\u308c\u307e\u3059\u3002
        • \u4e00\u90e8\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\u56fa\u5b9a\u3055\u308c\u3066\u304a\u3089\u305a\u3001\u5165\u529b\u30c7\u30fc\u30bf\u306e\u5206\u5e03\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002\u6642\u9593\u8a08\u7b97\u91cf\u306f\u6700\u60aa\u3001\u6700\u826f\u3001\u5e73\u5747\u306e\u30b1\u30fc\u30b9\u306b\u5206\u3051\u3089\u308c\u307e\u3059\u3002\u6700\u826f\u30b1\u30fc\u30b9\u306f\u3001\u5165\u529b\u30c7\u30fc\u30bf\u304c\u6700\u826f\u30b1\u30fc\u30b9\u3092\u9054\u6210\u3059\u308b\u305f\u3081\u306b\u53b3\u683c\u306a\u6761\u4ef6\u3092\u6e80\u305f\u3059\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u307b\u3068\u3093\u3069\u4f7f\u7528\u3055\u308c\u307e\u305b\u3093\u3002
        • \u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf\u306f\u3001\u30e9\u30f3\u30c0\u30e0\u30c7\u30fc\u30bf\u5165\u529b\u4e0b\u3067\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u53cd\u6620\u3057\u3001\u5b9f\u969b\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6027\u80fd\u306b\u5bc6\u63a5\u306b\u985e\u4f3c\u3057\u3066\u3044\u307e\u3059\u3002\u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf\u306e\u8a08\u7b97\u306b\u306f\u3001\u5165\u529b\u30c7\u30fc\u30bf\u306e\u5206\u5e03\u3068\u305d\u306e\u5f8c\u306e\u6570\u5b66\u7684\u671f\u5f85\u5024\u3092\u8003\u616e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u7a7a\u9593\u8a08\u7b97\u91cf

        • \u7a7a\u9593\u8a08\u7b97\u91cf\u306f\u3001\u6642\u9593\u8a08\u7b97\u91cf\u3068\u540c\u69d8\u306b\u3001\u30c7\u30fc\u30bf\u91cf\u306e\u5897\u52a0\u306b\u4f34\u3046\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u5360\u6709\u3059\u308b\u30e1\u30e2\u30ea\u7a7a\u9593\u306e\u50be\u5411\u3092\u6e2c\u5b9a\u3057\u307e\u3059\u3002
        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u4e2d\u306b\u4f7f\u7528\u3055\u308c\u308b\u95a2\u9023\u30e1\u30e2\u30ea\u7a7a\u9593\u306f\u3001\u5165\u529b\u7a7a\u9593\u3001\u4e00\u6642\u7a7a\u9593\u3001\u51fa\u529b\u7a7a\u9593\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u4e00\u822c\u7684\u306b\u3001\u5165\u529b\u7a7a\u9593\u306f\u7a7a\u9593\u8a08\u7b97\u91cf\u306e\u8a08\u7b97\u306b\u542b\u307e\u308c\u307e\u305b\u3093\u3002\u4e00\u6642\u7a7a\u9593\u306f\u4e00\u6642\u30c7\u30fc\u30bf\u3001\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3001\u547d\u4ee4\u7a7a\u9593\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u306f\u901a\u5e38\u3001\u518d\u5e30\u95a2\u6570\u3067\u306e\u307f\u7a7a\u9593\u8a08\u7b97\u91cf\u306b\u5f71\u97ff\u3057\u307e\u3059\u3002
        • \u901a\u5e38\u306f\u6700\u60aa\u30b1\u30fc\u30b9\u7a7a\u9593\u8a08\u7b97\u91cf\u306e\u307f\u306b\u7126\u70b9\u3092\u5f53\u3066\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u6700\u60aa\u306e\u5165\u529b\u30c7\u30fc\u30bf\u3068\u64cd\u4f5c\u306e\u6700\u60aa\u306e\u77ac\u9593\u3067\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u3092\u8a08\u7b97\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002
        • \u4e00\u822c\u7684\u306a\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\u3001\u4f4e\u3044\u3082\u306e\u304b\u3089\u9ad8\u3044\u3082\u306e\u3078\u3068\u4e26\u3079\u308b\u3068\u3001\\(O(1)\\)\u3001\\(O(\\log n)\\)\u3001\\(O(n)\\)\u3001\\(O(n^2)\\)\u3001\\(O(2^n)\\)\u306a\u3069\u304c\u542b\u307e\u308c\u307e\u3059\u3002
        "},{"location":"chapter_computational_complexity/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q: \u672b\u5c3e\u518d\u5e30\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u3067\u3059\u304b\uff1f

        \u7406\u8ad6\u7684\u306b\u306f\u3001\u672b\u5c3e\u518d\u5e30\u95a2\u6570\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u306b\u6700\u9069\u5316\u3067\u304d\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u307b\u3068\u3093\u3069\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\uff08Java\u3001Python\u3001C++\u3001Go\u3001C#\u306a\u3069\uff09\u306f\u672b\u5c3e\u518d\u5e30\u306e\u81ea\u52d5\u6700\u9069\u5316\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u306a\u3044\u305f\u3081\u3001\u4e00\u822c\u7684\u306b\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3068\u8003\u3048\u3089\u308c\u3066\u3044\u307e\u3059\u3002

        Q: \u300c\u95a2\u6570\u300d\u3068\u300c\u30e1\u30bd\u30c3\u30c9\u300d\u3068\u3044\u3046\u7528\u8a9e\u306e\u9055\u3044\u306f\u4f55\u3067\u3059\u304b\uff1f

        \u95a2\u6570\u306f\u72ec\u7acb\u3057\u3066\u5b9f\u884c\u3067\u304d\u3001\u3059\u3079\u3066\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u660e\u793a\u7684\u306b\u6e21\u3055\u308c\u307e\u3059\u3002\u30e1\u30bd\u30c3\u30c9\u306f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u95a2\u9023\u4ed8\u3051\u3089\u308c\u3001\u305d\u308c\u3092\u547c\u3073\u51fa\u3059\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u6697\u9ed9\u7684\u306b\u6e21\u3055\u308c\u3001\u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5185\u306b\u542b\u307e\u308c\u308b\u30c7\u30fc\u30bf\u3092\u64cd\u4f5c\u3067\u304d\u307e\u3059\u3002

        \u4e00\u822c\u7684\u306a\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u304b\u3089\u306e\u4f8b\u3092\u3044\u304f\u3064\u304b\u793a\u3057\u307e\u3059\uff1a

        • C\u306f\u624b\u7d9a\u304d\u578b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u306e\u6982\u5ff5\u304c\u306a\u3044\u305f\u3081\u3001\u95a2\u6570\u306e\u307f\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u69cb\u9020\u4f53\uff08struct\uff09\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u3067\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\u3067\u304d\u3001\u3053\u308c\u3089\u306e\u69cb\u9020\u4f53\u306b\u95a2\u9023\u4ed8\u3051\u3089\u308c\u305f\u95a2\u6570\u306f\u4ed6\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u30e1\u30bd\u30c3\u30c9\u3068\u540c\u7b49\u3067\u3059\u3002
        • Java\u3068C#\u306f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u3001\u30b3\u30fc\u30c9\u30d6\u30ed\u30c3\u30af\uff08\u30e1\u30bd\u30c3\u30c9\uff09\u306f\u901a\u5e38\u30af\u30e9\u30b9\u306e\u4e00\u90e8\u3067\u3059\u3002\u9759\u7684\u30e1\u30bd\u30c3\u30c9\u306f\u30af\u30e9\u30b9\u306b\u30d0\u30a4\u30f3\u30c9\u3055\u308c\u3001\u7279\u5b9a\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5909\u6570\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u306a\u3044\u305f\u3081\u3001\u95a2\u6570\u306e\u3088\u3046\u306b\u52d5\u4f5c\u3057\u307e\u3059\u3002
        • C++\u3068Python\u306f\u624b\u7d9a\u304d\u578b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\uff08\u95a2\u6570\uff09\u3068\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\uff08\u30e1\u30bd\u30c3\u30c9\uff09\u306e\u4e21\u65b9\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u307e\u3059\u3002

        Q: \u300c\u7a7a\u9593\u8a08\u7b97\u91cf\u306e\u4e00\u822c\u7684\u306a\u7a2e\u985e\u300d\u306e\u56f3\u306f\u3001\u5360\u6709\u7a7a\u9593\u306e\u7d76\u5bfe\u30b5\u30a4\u30ba\u3092\u53cd\u6620\u3057\u3066\u3044\u307e\u3059\u304b\uff1f

        \u3044\u3044\u3048\u3001\u56f3\u306f\u7a7a\u9593\u8a08\u7b97\u91cf\u3092\u793a\u3057\u3066\u304a\u308a\u3001\u3053\u308c\u306f\u5897\u52a0\u50be\u5411\u3092\u53cd\u6620\u3059\u308b\u3082\u306e\u3067\u3042\u308a\u3001\u5360\u6709\u7a7a\u9593\u306e\u7d76\u5bfe\u30b5\u30a4\u30ba\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002

        \\(n = 8\\)\u3092\u53d6\u308b\u3068\u3001\u5404\u66f2\u7dda\u306e\u5024\u304c\u305d\u306e\u95a2\u6570\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u306a\u3044\u3053\u3068\u306b\u6c17\u3065\u304f\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u3053\u308c\u306f\u3001\u5404\u66f2\u7dda\u306b\u5b9a\u6570\u9805\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u305f\u3081\u3067\u3001\u5024\u306e\u7bc4\u56f2\u3092\u8996\u899a\u7684\u306b\u5feb\u9069\u306a\u7bc4\u56f2\u306b\u5727\u7e2e\u3059\u308b\u3053\u3068\u3092\u610f\u56f3\u3057\u3066\u3044\u307e\u3059\u3002

        \u5b9f\u969b\u306b\u306f\u3001\u901a\u5e38\u306f\u5404\u30e1\u30bd\u30c3\u30c9\u306e\u300c\u5b9a\u6570\u9805\u300d\u8907\u96d1\u5ea6\u3092\u77e5\u3089\u306a\u3044\u305f\u3081\u3001\u8907\u96d1\u5ea6\u306e\u307f\u306b\u57fa\u3065\u3044\u3066\\(n = 8\\)\u306e\u6700\u826f\u30bd\u30ea\u30e5\u30fc\u30b7\u30e7\u30f3\u3092\u9078\u629e\u3059\u308b\u3053\u3068\u306f\u4e00\u822c\u7684\u306b\u4e0d\u53ef\u80fd\u3067\u3059\u3002\u3057\u304b\u3057\u3001\\(n = 8^5\\)\u306e\u5834\u5408\u3001\u5897\u52a0\u50be\u5411\u304c\u652f\u914d\u7684\u306b\u306a\u308b\u305f\u3081\u3001\u9078\u629e\u304c\u306f\u308b\u304b\u306b\u5bb9\u6613\u306b\u306a\u308a\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/","title":"2.3 \u00a0 \u6642\u9593\u8a08\u7b97\u91cf","text":"

        \u5b9f\u884c\u6642\u9593\u306f\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u76f4\u611f\u7684\u306b\u8a55\u4fa1\u3067\u304d\u307e\u3059\u3002\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u6642\u9593\u3092\u6b63\u78ba\u306b\u63a8\u5b9a\u3059\u308b\u306b\u306f\u3069\u3046\u3059\u308c\u3070\u3088\u3044\u3067\u3057\u3087\u3046\u304b\uff1f

        1. \u5b9f\u884c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u306e\u6c7a\u5b9a: \u3053\u308c\u306b\u306f\u3001\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u69cb\u6210\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3001\u30b7\u30b9\u30c6\u30e0\u74b0\u5883\u306a\u3069\u304c\u542b\u307e\u308c\u3001\u3053\u308c\u3089\u3059\u3079\u3066\u304c\u30b3\u30fc\u30c9\u306e\u5b9f\u884c\u52b9\u7387\u306b\u5f71\u97ff\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        2. \u69d8\u3005\u306a\u8a08\u7b97\u64cd\u4f5c\u306e\u5b9f\u884c\u6642\u9593\u306e\u8a55\u4fa1: \u4f8b\u3048\u3070\u3001\u52a0\u7b97\u64cd\u4f5c+\u306f1 ns\u3001\u4e57\u7b97\u64cd\u4f5c*\u306f10 ns\u3001\u5370\u5237\u64cd\u4f5cprint()\u306f5 ns\u306a\u3069\u304b\u304b\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        3. \u30b3\u30fc\u30c9\u5185\u306e\u3059\u3079\u3066\u306e\u8a08\u7b97\u64cd\u4f5c\u3092\u30ab\u30a6\u30f3\u30c8: \u3053\u308c\u3089\u3059\u3079\u3066\u306e\u64cd\u4f5c\u306e\u5b9f\u884c\u6642\u9593\u3092\u5408\u8a08\u3059\u308b\u3068\u3001\u7dcf\u5b9f\u884c\u6642\u9593\u304c\u5f97\u3089\u308c\u307e\u3059\u3002

        \u4f8b\u3048\u3070\u3001\u5165\u529b\u30b5\u30a4\u30ba\u304c\\(n\\)\u306e\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin
        # \u7279\u5b9a\u306e\u64cd\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4e0b\u3067\ndef algorithm(n: int):\n    a = 2      # 1 ns\n    a = a + 1  # 1 ns\n    a = a * 2  # 10 ns\n    # n\u56de\u30eb\u30fc\u30d7\n    for _ in range(n):  # 1 ns\n        print(0)        # 5 ns\n
        // \u7279\u5b9a\u306e\u64cd\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4e0b\u3067\nvoid algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // n\u56de\u30eb\u30fc\u30d7\n    for (int i = 0; i < n; i++) {  // 1 ns, \u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b\n        cout << 0 << endl;         // 5 ns\n    }\n}\n
        // \u7279\u5b9a\u306e\u64cd\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4e0b\u3067\nvoid algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // n\u56de\u30eb\u30fc\u30d7\n    for (int i = 0; i < n; i++) {  // 1 ns, \u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b\n        System.out.println(0);     // 5 ns\n    }\n}\n
        // \u7279\u5b9a\u306e\u64cd\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4e0b\u3067\nvoid Algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // n\u56de\u30eb\u30fc\u30d7\n    for (int i = 0; i < n; i++) {  // 1 ns, \u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b\n        Console.WriteLine(0);      // 5 ns\n    }\n}\n
        // \u7279\u5b9a\u306e\u64cd\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4e0b\u3067\nfunc algorithm(n int) {\n    a := 2     // 1 ns\n    a = a + 1  // 1 ns\n    a = a * 2  // 10 ns\n    // n\u56de\u30eb\u30fc\u30d7\n    for i := 0; i < n; i++ {  // 1 ns\n        fmt.Println(a)        // 5 ns\n    }\n}\n
        // \u7279\u5b9a\u306e\u64cd\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4e0b\u3067\nfunc algorithm(n: Int) {\n    var a = 2 // 1 ns\n    a = a + 1 // 1 ns\n    a = a * 2 // 10 ns\n    // n\u56de\u30eb\u30fc\u30d7\n    for _ in 0 ..< n { // 1 ns\n        print(0) // 5 ns\n    }\n}\n
        // \u7279\u5b9a\u306e\u64cd\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4e0b\u3067\nfunction algorithm(n) {\n    var a = 2; // 1 ns\n    a = a + 1; // 1 ns\n    a = a * 2; // 10 ns\n    // n\u56de\u30eb\u30fc\u30d7\n    for(let i = 0; i < n; i++) { // 1 ns, \u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b\n        console.log(0); // 5 ns\n    }\n}\n
        // \u7279\u5b9a\u306e\u64cd\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4e0b\u3067\nfunction algorithm(n: number): void {\n    var a: number = 2; // 1 ns\n    a = a + 1; // 1 ns\n    a = a * 2; // 10 ns\n    // n\u56de\u30eb\u30fc\u30d7\n    for(let i = 0; i < n; i++) { // 1 ns, \u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b\n        console.log(0); // 5 ns\n    }\n}\n
        // \u7279\u5b9a\u306e\u64cd\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4e0b\u3067\nvoid algorithm(int n) {\n  int a = 2; // 1 ns\n  a = a + 1; // 1 ns\n  a = a * 2; // 10 ns\n  // n\u56de\u30eb\u30fc\u30d7\n  for (int i = 0; i < n; i++) { // 1 ns, \u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b\n    print(0); // 5 ns\n  }\n}\n
        // \u7279\u5b9a\u306e\u64cd\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4e0b\u3067\nfn algorithm(n: i32) {\n    let mut a = 2;      // 1 ns\n    a = a + 1;          // 1 ns\n    a = a * 2;          // 10 ns\n    // n\u56de\u30eb\u30fc\u30d7\n    for _ in 0..n {     // \u6bce\u56dei++\u30671 ns\n        println!(\"{}\", 0);  // 5 ns\n    }\n}\n
        // \u7279\u5b9a\u306e\u64cd\u4f5c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4e0b\u3067\nvoid algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // n\u56de\u30eb\u30fc\u30d7\n    for (int i = 0; i < n; i++) {   // 1 ns, \u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b\n        printf(\"%d\", 0);            // 5 ns\n    }\n}\n
        \n

        \u4e0a\u8a18\u306e\u65b9\u6cd5\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u6642\u9593\u306f\\((6n + 12)\\) ns\u3068\u3057\u3066\u8a08\u7b97\u3067\u304d\u307e\u3059\uff1a

        \\[ 1 + 1 + 10 + (1 + 5) \\times n = 6n + 12 \\]

        \u3057\u304b\u3057\u3001\u5b9f\u969b\u306b\u306f\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u6642\u9593\u3092\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u3053\u3068\u306f\u5b9f\u7528\u7684\u3067\u3082\u5408\u7406\u7684\u3067\u3082\u3042\u308a\u307e\u305b\u3093\u3002\u7b2c\u4e00\u306b\u3001\u63a8\u5b9a\u6642\u9593\u3092\u5b9f\u884c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u306b\u7d50\u3073\u4ed8\u3051\u305f\u304f\u3042\u308a\u307e\u305b\u3093\u3002\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u69d8\u3005\u306a\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u3067\u5b9f\u884c\u3055\u308c\u308b\u5fc5\u8981\u304c\u3042\u308b\u304b\u3089\u3067\u3059\u3002\u7b2c\u4e8c\u306b\u3001\u5404\u7a2e\u64cd\u4f5c\u306e\u5b9f\u884c\u6642\u9593\u3092\u77e5\u308b\u3053\u3068\u306f\u56f0\u96e3\u3067\u3042\u308a\u3001\u63a8\u5b9a\u30d7\u30ed\u30bb\u30b9\u3092\u96e3\u3057\u304f\u3057\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#231","title":"2.3.1 \u00a0 \u6642\u9593\u5897\u52a0\u50be\u5411\u306e\u8a55\u4fa1","text":"

        \u6642\u9593\u8a08\u7b97\u91cf\u5206\u6790\u306f\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u6642\u9593\u3092\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u306e\u3067\u306f\u306a\u304f\u3001**\u30c7\u30fc\u30bf\u91cf\u304c\u5897\u52a0\u3059\u308b\u306b\u3064\u308c\u3066\u306e\u5b9f\u884c\u6642\u9593\u306e\u5897\u52a0\u50be\u5411**\u3092\u5206\u6790\u3057\u307e\u3059\u3002

        \u3053\u306e\u300c\u6642\u9593\u5897\u52a0\u50be\u5411\u300d\u306e\u6982\u5ff5\u3092\u4f8b\u3067\u7406\u89e3\u3057\u307e\u3057\u3087\u3046\u3002\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\u3092\\(n\\)\u3068\u3057\u30013\u3064\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u3001B\u3001C\u3092\u8003\u3048\u3066\u307f\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin
        # \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\ndef algorithm_A(n: int):\n    print(0)\n# \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\ndef algorithm_B(n: int):\n    for _ in range(n):\n        print(0)\n# \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\ndef algorithm_C(n: int):\n    for _ in range(1000000):\n        print(0)\n
        // \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nvoid algorithm_A(int n) {\n    cout << 0 << endl;\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\nvoid algorithm_B(int n) {\n    for (int i = 0; i < n; i++) {\n        cout << 0 << endl;\n    }\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nvoid algorithm_C(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        cout << 0 << endl;\n    }\n}\n
        // \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nvoid algorithm_A(int n) {\n    System.out.println(0);\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\nvoid algorithm_B(int n) {\n    for (int i = 0; i < n; i++) {\n        System.out.println(0);\n    }\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nvoid algorithm_C(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        System.out.println(0);\n    }\n}\n
        // \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nvoid AlgorithmA(int n) {\n    Console.WriteLine(0);\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\nvoid AlgorithmB(int n) {\n    for (int i = 0; i < n; i++) {\n        Console.WriteLine(0);\n    }\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nvoid AlgorithmC(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        Console.WriteLine(0);\n    }\n}\n
        // \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nfunc algorithm_A(n int) {\n    fmt.Println(0)\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\nfunc algorithm_B(n int) {\n    for i := 0; i < n; i++ {\n        fmt.Println(0)\n    }\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nfunc algorithm_C(n int) {\n    for i := 0; i < 1000000; i++ {\n        fmt.Println(0)\n    }\n}\n
        // \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nfunc algorithmA(n: Int) {\n    print(0)\n}\n\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\nfunc algorithmB(n: Int) {\n    for _ in 0 ..< n {\n        print(0)\n    }\n}\n\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nfunc algorithmC(n: Int) {\n    for _ in 0 ..< 1_000_000 {\n        print(0)\n    }\n}\n
        // \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nfunction algorithm_A(n) {\n    console.log(0);\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\nfunction algorithm_B(n) {\n    for (let i = 0; i < n; i++) {\n        console.log(0);\n    }\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nfunction algorithm_C(n) {\n    for (let i = 0; i < 1000000; i++) {\n        console.log(0);\n    }\n}\n
        // \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nfunction algorithm_A(n: number): void {\n    console.log(0);\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\nfunction algorithm_B(n: number): void {\n    for (let i = 0; i < n; i++) {\n        console.log(0);\n    }\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nfunction algorithm_C(n: number): void {\n    for (let i = 0; i < 1000000; i++) {\n        console.log(0);\n    }\n}\n
        // \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nvoid algorithmA(int n) {\n  print(0);\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\nvoid algorithmB(int n) {\n  for (int i = 0; i < n; i++) {\n    print(0);\n  }\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nvoid algorithmC(int n) {\n  for (int i = 0; i < 1000000; i++) {\n    print(0);\n  }\n}\n
        // \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nfn algorithm_A(n: i32) {\n    println!(\"{}\", 0);\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\nfn algorithm_B(n: i32) {\n    for _ in 0..n {\n        println!(\"{}\", 0);\n    }\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nfn algorithm_C(n: i32) {\n    for _ in 0..1000000 {\n        println!(\"{}\", 0);\n    }\n}\n
        // \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nvoid algorithm_A(int n) {\n    printf(\"%d\", 0);\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\nvoid algorithm_B(int n) {\n    for (int i = 0; i < n; i++) {\n        printf(\"%d\", 0);\n    }\n}\n// \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306e\u6642\u9593\u8a08\u7b97\u91cf\uff1a\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\nvoid algorithm_C(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        printf(\"%d\", 0);\n    }\n}\n
        \n

        \u4e0b\u56f3\u306f\u3053\u308c\u30893\u3064\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u306b\u306f1\u3064\u306e\u5370\u5237\u64cd\u4f5c\u306e\u307f\u304c\u3042\u308a\u3001\u305d\u306e\u5b9f\u884c\u6642\u9593\u306f\\(n\\)\u3068\u3068\u3082\u306b\u5897\u52a0\u3057\u307e\u305b\u3093\u3002\u305d\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\u300c\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\u300d\u3068\u8003\u3048\u3089\u308c\u307e\u3059\u3002
        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306b\u306f\\(n\\)\u56de\u30eb\u30fc\u30d7\u3059\u308b\u5370\u5237\u64cd\u4f5c\u304c\u3042\u308a\u3001\u305d\u306e\u5b9f\u884c\u6642\u9593\u306f\\(n\\)\u3068\u7dda\u5f62\u306b\u5897\u52a0\u3057\u307e\u3059\u3002\u305d\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\u300c\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\u300d\u3067\u3059\u3002
        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0C\u306b\u306f1,000,000\u56de\u30eb\u30fc\u30d7\u3059\u308b\u5370\u5237\u64cd\u4f5c\u304c\u3042\u308a\u307e\u3059\u3002\u6642\u9593\u306f\u304b\u304b\u308a\u307e\u3059\u304c\u3001\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\\(n\\)\u3068\u306f\u7121\u95a2\u4fc2\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001C\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306fA\u3068\u540c\u3058\u300c\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\u300d\u3067\u3059\u3002

        \u56f3 2-7 \u00a0 Time growth trend of algorithms a, b, and c

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u6642\u9593\u3092\u76f4\u63a5\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u3053\u3068\u3068\u6bd4\u8f03\u3057\u3066\u3001\u6642\u9593\u8a08\u7b97\u91cf\u5206\u6790\u306e\u7279\u5fb4\u306f\u4f55\u3067\u3057\u3087\u3046\u304b\uff1f

        • \u6642\u9593\u8a08\u7b97\u91cf\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u52b9\u679c\u7684\u306b\u8a55\u4fa1\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306f\u7dda\u5f62\u306b\u5897\u52a0\u3059\u308b\u5b9f\u884c\u6642\u9593\u3092\u6301\u3061\u3001\\(n > 1\\)\u306e\u6642\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u3088\u308a\u9045\u304f\u3001\\(n > 1,000,000\\)\u306e\u6642\u306fC\u3088\u308a\u9045\u304f\u306a\u308a\u307e\u3059\u3002\u5b9f\u969b\u3001\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\\(n\\)\u304c\u5341\u5206\u306b\u5927\u304d\u3044\u9650\u308a\u3001\u300c\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\u300d\u8907\u96d1\u5ea6\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u5e38\u306b\u300c\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\u300d\u3088\u308a\u3082\u512a\u308c\u3066\u304a\u308a\u3001\u6642\u9593\u5897\u52a0\u50be\u5411\u306e\u672c\u8cea\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002
        • \u6642\u9593\u8a08\u7b97\u91cf\u5206\u6790\u306f\u3088\u308a\u76f4\u611f\u7684\u3067\u3059\u3002\u660e\u3089\u304b\u306b\u3001\u5b9f\u884c\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u3068\u8a08\u7b97\u64cd\u4f5c\u306e\u7a2e\u985e\u306f\u5b9f\u884c\u6642\u9593\u5897\u52a0\u306e\u50be\u5411\u3068\u306f\u7121\u95a2\u4fc2\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u6642\u9593\u8a08\u7b97\u91cf\u5206\u6790\u3067\u306f\u3001\u3059\u3079\u3066\u306e\u8a08\u7b97\u64cd\u4f5c\u306e\u5b9f\u884c\u6642\u9593\u3092\u540c\u3058\u300c\u5358\u4f4d\u6642\u9593\u300d\u3068\u3057\u3066\u6271\u3046\u3053\u3068\u304c\u3067\u304d\u3001\u300c\u8a08\u7b97\u64cd\u4f5c\u5b9f\u884c\u6642\u9593\u30ab\u30a6\u30f3\u30c8\u300d\u3092\u300c\u8a08\u7b97\u64cd\u4f5c\u30ab\u30a6\u30f3\u30c8\u300d\u306b\u5358\u7d14\u5316\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u63a8\u5b9a\u306e\u8907\u96d1\u3055\u304c\u5927\u5e45\u306b\u8efd\u6e1b\u3055\u308c\u307e\u3059\u3002
        • \u6642\u9593\u8a08\u7b97\u91cf\u306b\u306f\u5236\u9650\u304c\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0A\u3068C\u306f\u540c\u3058\u6642\u9593\u8a08\u7b97\u91cf\u3092\u6301\u3061\u307e\u3059\u304c\u3001\u5b9f\u969b\u306e\u5b9f\u884c\u6642\u9593\u306f\u5927\u304d\u304f\u7570\u306a\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u540c\u69d8\u306b\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0B\u306fC\u3088\u308a\u3082\u9ad8\u3044\u6642\u9593\u8a08\u7b97\u91cf\u3092\u6301\u3061\u307e\u3059\u304c\u3001\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\\(n\\)\u304c\u5c0f\u3055\u3044\u5834\u5408\u306f\u660e\u3089\u304b\u306b\u512a\u308c\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u5834\u5408\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306e\u307f\u306b\u57fa\u3065\u3044\u3066\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u5224\u65ad\u3059\u308b\u3053\u3068\u306f\u56f0\u96e3\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u3053\u308c\u3089\u306e\u554f\u984c\u306b\u3082\u304b\u304b\u308f\u3089\u305a\u3001\u8907\u96d1\u5ea6\u5206\u6790\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u8a55\u4fa1\u3059\u308b\u305f\u3081\u306e\u6700\u3082\u52b9\u679c\u7684\u3067\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u308b\u65b9\u6cd5\u3067\u3059\u3002
        "},{"location":"chapter_computational_complexity/time_complexity/#232","title":"2.3.2 \u00a0 \u6f38\u8fd1\u4e0a\u9650","text":"

        \u5165\u529b\u30b5\u30a4\u30ba\u304c\\(n\\)\u306e\u95a2\u6570\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin
        def algorithm(n: int):\n    a = 1      # +1\n    a = a + 1  # +1\n    a = a * 2  # +1\n    # n\u56de\u30eb\u30fc\u30d7\n    for i in range(n):  # +1\n        print(0)        # +1\n
        void algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // n\u56de\u30eb\u30fc\u30d7\n    for (int i = 0; i < n; i++) { // +1 (\u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b)\n        cout << 0 << endl;    // +1\n    }\n}\n
        void algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // n\u56de\u30eb\u30fc\u30d7\n    for (int i = 0; i < n; i++) { // +1 (\u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b)\n        System.out.println(0);    // +1\n    }\n}\n
        void Algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // n\u56de\u30eb\u30fc\u30d7\n    for (int i = 0; i < n; i++) {   // +1 (\u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b)\n        Console.WriteLine(0);   // +1\n    }\n}\n
        func algorithm(n int) {\n    a := 1      // +1\n    a = a + 1   // +1\n    a = a * 2   // +1\n    // n\u56de\u30eb\u30fc\u30d7\n    for i := 0; i < n; i++ {   // +1\n        fmt.Println(a)         // +1\n    }\n}\n
        func algorithm(n: Int) {\n    var a = 1 // +1\n    a = a + 1 // +1\n    a = a * 2 // +1\n    // n\u56de\u30eb\u30fc\u30d7\n    for _ in 0 ..< n { // +1\n        print(0) // +1\n    }\n}\n
        function algorithm(n) {\n    var a = 1; // +1\n    a += 1; // +1\n    a *= 2; // +1\n    // n\u56de\u30eb\u30fc\u30d7\n    for(let i = 0; i < n; i++){ // +1 (\u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b)\n        console.log(0); // +1\n    }\n}\n
        function algorithm(n: number): void{\n    var a: number = 1; // +1\n    a += 1; // +1\n    a *= 2; // +1\n    // n\u56de\u30eb\u30fc\u30d7\n    for(let i = 0; i < n; i++){ // +1 (\u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b)\n        console.log(0); // +1\n    }\n}\n
        void algorithm(int n) {\n  int a = 1; // +1\n  a = a + 1; // +1\n  a = a * 2; // +1\n  // n\u56de\u30eb\u30fc\u30d7\n  for (int i = 0; i < n; i++) { // +1 (\u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b)\n    print(0); // +1\n  }\n}\n
        fn algorithm(n: i32) {\n    let mut a = 1;   // +1\n    a = a + 1;      // +1\n    a = a * 2;      // +1\n\n    // n\u56de\u30eb\u30fc\u30d7\n    for _ in 0..n { // +1 (\u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b)\n        println!(\"{}\", 0); // +1\n    }\n}\n
        void algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // n\u56de\u30eb\u30fc\u30d7\n    for (int i = 0; i < n; i++) {   // +1 (\u6bce\u56dei++\u304c\u5b9f\u884c\u3055\u308c\u308b)\n        printf(\"%d\", 0);            // +1\n    }\n}\n
        \n

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u64cd\u4f5c\u6570\u3092\u5165\u529b\u30b5\u30a4\u30ba\\(n\\)\u306e\u95a2\u6570\u3068\u3057\u3066\u8868\u3059\u95a2\u6570\u3092\\(T(n)\\)\u3068\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u4f8b\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\uff1a

        \\[ T(n) = 3 + 2n \\]

        \\(T(n)\\)\u306f\u7dda\u5f62\u95a2\u6570\u3067\u3042\u308b\u305f\u3081\u3001\u305d\u306e\u5897\u52a0\u50be\u5411\u306f\u7dda\u5f62\u3067\u3042\u308a\u3001\u3057\u305f\u304c\u3063\u3066\u3001\u305d\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\u3067\u3001\\(O(n)\\)\u3068\u8868\u8a18\u3055\u308c\u307e\u3059\u3002\u3053\u306e\u6570\u5b66\u8a18\u6cd5\u306f\u30d3\u30c3\u30b0O\u8a18\u6cd5\u3068\u3057\u3066\u77e5\u3089\u308c\u3001\u95a2\u6570\\(T(n)\\)\u306e\u6f38\u8fd1\u4e0a\u9650\u3092\u8868\u3057\u307e\u3059\u3002

        \u672c\u8cea\u7684\u306b\u3001\u6642\u9593\u8a08\u7b97\u91cf\u5206\u6790\u306f\u300c\u64cd\u4f5c\u6570\\(T(n)\\)\u300d\u306e\u6f38\u8fd1\u4e0a\u9650\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3067\u3059\u3002\u305d\u308c\u306b\u306f\u6b63\u78ba\u306a\u6570\u5b66\u7684\u5b9a\u7fa9\u304c\u3042\u308a\u307e\u3059\u3002

        \u6f38\u8fd1\u4e0a\u9650

        \u3059\u3079\u3066\u306e\\(n > n_0\\)\u306b\u5bfe\u3057\u3066\\(T(n) \\leq c \\cdot f(n)\\)\u3068\u306a\u308b\u3088\u3046\u306a\u6b63\u306e\u5b9f\u6570\\(c\\)\u3068\\(n_0\\)\u304c\u5b58\u5728\u3059\u308b\u5834\u5408\u3001\\(f(n)\\)\u306f\\(T(n)\\)\u306e\u6f38\u8fd1\u4e0a\u9650\u3068\u307f\u306a\u3055\u308c\u3001\\(T(n) = O(f(n))\\)\u3068\u8868\u8a18\u3055\u308c\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u3001\u6f38\u8fd1\u4e0a\u9650\u306e\u8a08\u7b97\u3067\u306f\u3001\\(n\\)\u304c\u7121\u9650\u5927\u306b\u8fd1\u3065\u304f\u306b\u3064\u308c\u3066\u3001\\(T(n)\\)\u3068\\(f(n)\\)\u304c\u540c\u3058\u5897\u52a0\u30aa\u30fc\u30c0\u30fc\u3092\u6301\u3061\u3001\u5b9a\u6570\u56e0\u5b50\\(c\\)\u306e\u307f\u304c\u7570\u306a\u308b\u3088\u3046\u306a\u95a2\u6570\\(f(n)\\)\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u542b\u307e\u308c\u307e\u3059\u3002

        \u56f3 2-8 \u00a0 Asymptotic upper bound of a function

        "},{"location":"chapter_computational_complexity/time_complexity/#233","title":"2.3.3 \u00a0 \u8a08\u7b97\u65b9\u6cd5","text":"

        \u6f38\u8fd1\u4e0a\u9650\u306e\u6982\u5ff5\u306f\u6570\u5b66\u7684\u306b\u6fc3\u5bc6\u306b\u898b\u3048\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u4eca\u3059\u3050\u5b8c\u5168\u306b\u7406\u89e3\u3059\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u307e\u305a\u8a08\u7b97\u65b9\u6cd5\u3092\u7406\u89e3\u3057\u3001\u6642\u9593\u3092\u304b\u3051\u3066\u7df4\u7fd2\u3057\u7406\u89e3\u3057\u307e\u3057\u3087\u3046\u3002

        \\(f(n)\\)\u304c\u6c7a\u307e\u308c\u3070\u3001\u6642\u9593\u8a08\u7b97\u91cf\\(O(f(n))\\)\u304c\u5f97\u3089\u308c\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u6f38\u8fd1\u4e0a\u9650\\(f(n)\\)\u3092\u3069\u306e\u3088\u3046\u306b\u6c7a\u5b9a\u3059\u308b\u306e\u3067\u3057\u3087\u3046\u304b\uff1f\u3053\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u306f\u4e00\u822c\u7684\u306b2\u3064\u306e\u30b9\u30c6\u30c3\u30d7\u304c\u542b\u307e\u308c\u307e\u3059\uff1a\u64cd\u4f5c\u6570\u306e\u30ab\u30a6\u30f3\u30c8\u3068\u6f38\u8fd1\u4e0a\u9650\u306e\u6c7a\u5b9a\u3067\u3059\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#1-1","title":"1. \u00a0 \u30b9\u30c6\u30c3\u30d71: \u64cd\u4f5c\u6570\u306e\u30ab\u30a6\u30f3\u30c8","text":"

        \u3053\u306e\u30b9\u30c6\u30c3\u30d7\u3067\u306f\u3001\u30b3\u30fc\u30c9\u3092\u884c\u3054\u3068\u306b\u78ba\u8a8d\u3057\u307e\u3059\u3002\u3057\u304b\u3057\u3001\\(c \\cdot f(n)\\)\u306e\u5b9a\u6570\\(c\\)\u306e\u5b58\u5728\u306b\u3088\u308a\u3001\\(T(n)\\)\u306e\u3059\u3079\u3066\u306e\u4fc2\u6570\u3068\u5b9a\u6570\u9805\u306f\u7121\u8996\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u539f\u7406\u306b\u3088\u308a\u3001\u64cd\u4f5c\u3092\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u969b\u306e\u7c21\u7565\u5316\u6280\u6cd5\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002

        1. \\(T(n)\\)\u306e\u5b9a\u6570\u9805\u3092\u7121\u8996\u3057\u307e\u3059\u3002\u3053\u308c\u3089\u306f\\(n\\)\u3068\u306f\u7121\u95a2\u4fc2\u3067\u3042\u308b\u305f\u3081\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306b\u5f71\u97ff\u3057\u307e\u305b\u3093\u3002
        2. \u3059\u3079\u3066\u306e\u4fc2\u6570\u3092\u7701\u7565\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\\(2n\\)\u3001\\(5n + 1\\)\u56de\u306a\u3069\u306e\u30eb\u30fc\u30d7\u306f\u3001\\(n\\)\u306e\u524d\u306e\u4fc2\u6570\u304c\u6642\u9593\u8a08\u7b97\u91cf\u306b\u5f71\u97ff\u3057\u306a\u3044\u305f\u3081\u3001\\(n\\)\u56de\u306b\u7c21\u7565\u5316\u3067\u304d\u307e\u3059\u3002
        3. \u30cd\u30b9\u30c8\u3057\u305f\u30eb\u30fc\u30d7\u306b\u306f\u4e57\u7b97\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u7dcf\u64cd\u4f5c\u6570\u306f\u5404\u30eb\u30fc\u30d7\u306e\u64cd\u4f5c\u6570\u306e\u7a4d\u3067\u3042\u308a\u3001\u30dd\u30a4\u30f3\u30c81\u30682\u306e\u7c21\u7565\u5316\u6280\u6cd5\u3092\u5404\u30eb\u30fc\u30d7\u30ec\u30d9\u30eb\u306b\u9069\u7528\u3057\u307e\u3059\u3002

        \u95a2\u6570\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u3053\u308c\u3089\u306e\u6280\u6cd5\u3092\u4f7f\u7528\u3057\u3066\u64cd\u4f5c\u3092\u30ab\u30a6\u30f3\u30c8\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin
        def algorithm(n: int):\n    a = 1      # +0 (\u6280\u6cd51)\n    a = a + n  # +0 (\u6280\u6cd51)\n    # +n (\u6280\u6cd52)\n    for i in range(5 * n + 1):\n        print(0)\n    # +n*n (\u6280\u6cd53)\n    for i in range(2 * n):\n        for j in range(n + 1):\n            print(0)\n
        void algorithm(int n) {\n    int a = 1;  // +0 (\u6280\u6cd51)\n    a = a + n;  // +0 (\u6280\u6cd51)\n    // +n (\u6280\u6cd52)\n    for (int i = 0; i < 5 * n + 1; i++) {\n        cout << 0 << endl;\n    }\n    // +n*n (\u6280\u6cd53)\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            cout << 0 << endl;\n        }\n    }\n}\n
        void algorithm(int n) {\n    int a = 1;  // +0 (\u6280\u6cd51)\n    a = a + n;  // +0 (\u6280\u6cd51)\n    // +n (\u6280\u6cd52)\n    for (int i = 0; i < 5 * n + 1; i++) {\n        System.out.println(0);\n    }\n    // +n*n (\u6280\u6cd53)\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            System.out.println(0);\n        }\n    }\n}\n
        void Algorithm(int n) {\n    int a = 1;  // +0 (\u6280\u6cd51)\n    a = a + n;  // +0 (\u6280\u6cd51)\n    // +n (\u6280\u6cd52)\n    for (int i = 0; i < 5 * n + 1; i++) {\n        Console.WriteLine(0);\n    }\n    // +n*n (\u6280\u6cd53)\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            Console.WriteLine(0);\n        }\n    }\n}\n
        func algorithm(n int) {\n    a := 1     // +0 (\u6280\u6cd51)\n    a = a + n  // +0 (\u6280\u6cd51)\n    // +n (\u6280\u6cd52)\n    for i := 0; i < 5 * n + 1; i++ {\n        fmt.Println(0)\n    }\n    // +n*n (\u6280\u6cd53)\n    for i := 0; i < 2 * n; i++ {\n        for j := 0; j < n + 1; j++ {\n            fmt.Println(0)\n        }\n    }\n}\n
        func algorithm(n: Int) {\n    var a = 1 // +0 (\u6280\u6cd51)\n    a = a + n // +0 (\u6280\u6cd51)\n    // +n (\u6280\u6cd52)\n    for _ in 0 ..< (5 * n + 1) {\n        print(0)\n    }\n    // +n*n (\u6280\u6cd53)\n    for _ in 0 ..< (2 * n) {\n        for _ in 0 ..< (n + 1) {\n            print(0)\n        }\n    }\n}\n
        function algorithm(n) {\n    let a = 1;  // +0 (\u6280\u6cd51)\n    a = a + n;  // +0 (\u6280\u6cd51)\n    // +n (\u6280\u6cd52)\n    for (let i = 0; i < 5 * n + 1; i++) {\n        console.log(0);\n    }\n    // +n*n (\u6280\u6cd53)\n    for (let i = 0; i < 2 * n; i++) {\n        for (let j = 0; j < n + 1; j++) {\n            console.log(0);\n        }\n    }\n}\n
        function algorithm(n: number): void {\n    let a = 1;  // +0 (\u6280\u6cd51)\n    a = a + n;  // +0 (\u6280\u6cd51)\n    // +n (\u6280\u6cd52)\n    for (let i = 0; i < 5 * n + 1; i++) {\n        console.log(0);\n    }\n    // +n*n (\u6280\u6cd53)\n    for (let i = 0; i < 2 * n; i++) {\n        for (let j = 0; j < n + 1; j++) {\n            console.log(0);\n        }\n    }\n}\n
        void algorithm(int n) {\n  int a = 1; // +0 (\u6280\u6cd51)\n  a = a + n; // +0 (\u6280\u6cd51)\n  // +n (\u6280\u6cd52)\n  for (int i = 0; i < 5 * n + 1; i++) {\n    print(0);\n  }\n  // +n*n (\u6280\u6cd53)\n  for (int i = 0; i < 2 * n; i++) {\n    for (int j = 0; j < n + 1; j++) {\n      print(0);\n    }\n  }\n}\n
        fn algorithm(n: i32) {\n    let mut a = 1;     // +0 (\u6280\u6cd51)\n    a = a + n;        // +0 (\u6280\u6cd51)\n\n    // +n (\u6280\u6cd52)\n    for i in 0..(5 * n + 1) {\n        println!(\"{}\", 0);\n    }\n\n    // +n*n (\u6280\u6cd53)\n    for i in 0..(2 * n) {\n        for j in 0..(n + 1) {\n            println!(\"{}\", 0);\n        }\n    }\n}\n
        void algorithm(int n) {\n    int a = 1;  // +0 (\u6280\u6cd51)\n    a = a + n;  // +0 (\u6280\u6cd51)\n    // +n (\u6280\u6cd52)\n    for (int i = 0; i < 5 * n + 1; i++) {\n        printf(\"%d\", 0);\n    }\n    // +n*n (\u6280\u6cd53)\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            printf(\"%d\", 0);\n        }\n    }\n}\n
        \n

        \u4ee5\u4e0b\u306e\u5f0f\u306f\u3001\u7c21\u7565\u5316\u524d\u5f8c\u306e\u30ab\u30a6\u30f3\u30c8\u7d50\u679c\u3092\u793a\u3057\u3066\u304a\u308a\u3001\u3069\u3061\u3089\u3082\\(O(n^2)\\)\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306b\u5c0e\u304d\u307e\u3059\uff1a

        \\[ \\begin{aligned} T(n) & = 2n(n + 1) + (5n + 1) + 2 & \\text{\u5b8c\u5168\u30ab\u30a6\u30f3\u30c8 (-.-|||)} \\newline & = 2n^2 + 7n + 3 \\newline T(n) & = n^2 + n & \\text{\u7c21\u7565\u5316\u30ab\u30a6\u30f3\u30c8 (o.O)} \\end{aligned} \\]"},{"location":"chapter_computational_complexity/time_complexity/#2-2","title":"2. \u00a0 \u30b9\u30c6\u30c3\u30d72: \u6f38\u8fd1\u4e0a\u9650\u306e\u6c7a\u5b9a","text":"

        \u6642\u9593\u8a08\u7b97\u91cf\u306f\\(T(n)\\)\u306e\u6700\u9ad8\u6b21\u9805\u306b\u3088\u3063\u3066\u6c7a\u5b9a\u3055\u308c\u307e\u3059\u3002\u3053\u308c\u306f\u3001\\(n\\)\u304c\u7121\u9650\u5927\u306b\u8fd1\u3065\u304f\u306b\u3064\u308c\u3066\u3001\u6700\u9ad8\u6b21\u9805\u304c\u652f\u914d\u7684\u306b\u306a\u308a\u3001\u4ed6\u306e\u9805\u306e\u5f71\u97ff\u306f\u7121\u8996\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u305f\u3081\u3067\u3059\u3002

        \u4ee5\u4e0b\u306e\u8868\u306f\u3001\u7570\u306a\u308b\u64cd\u4f5c\u30ab\u30a6\u30f3\u30c8\u3068\u305d\u308c\u306b\u5bfe\u5fdc\u3059\u308b\u6642\u9593\u8a08\u7b97\u91cf\u306e\u4f8b\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u4fc2\u6570\u304c\u5897\u52a0\u30aa\u30fc\u30c0\u30fc\u3092\u5909\u66f4\u3067\u304d\u306a\u3044\u3053\u3068\u3092\u5f37\u8abf\u3059\u308b\u305f\u3081\u306b\u3001\u8a87\u5f35\u3055\u308c\u305f\u5024\u304c\u4f7f\u7528\u3055\u308c\u3066\u3044\u307e\u3059\u3002\\(n\\)\u304c\u975e\u5e38\u306b\u5927\u304d\u304f\u306a\u308b\u3068\u3001\u3053\u308c\u3089\u306e\u5b9a\u6570\u306f\u91cd\u8981\u3067\u306a\u304f\u306a\u308a\u307e\u3059\u3002

        \u8868: \u7570\u306a\u308b\u64cd\u4f5c\u30ab\u30a6\u30f3\u30c8\u306b\u5bfe\u3059\u308b\u6642\u9593\u8a08\u7b97\u91cf

        \u64cd\u4f5c\u30ab\u30a6\u30f3\u30c8 \\(T(n)\\) \u6642\u9593\u8a08\u7b97\u91cf \\(O(f(n))\\) \\(100000\\) \\(O(1)\\) \\(3n + 2\\) \\(O(n)\\) \\(2n^2 + 3n + 2\\) \\(O(n^2)\\) \\(n^3 + 10000n^2\\) \\(O(n^3)\\) \\(2^n + 10000n^{10000}\\) \\(O(2^n)\\)"},{"location":"chapter_computational_complexity/time_complexity/#234","title":"2.3.4 \u00a0 \u4e00\u822c\u7684\u306a\u6642\u9593\u8a08\u7b97\u91cf\u306e\u7a2e\u985e","text":"

        \u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\u3092\\(n\\)\u3068\u3057\u307e\u3057\u3087\u3046\u3002\u4e00\u822c\u7684\u306a\u6642\u9593\u8a08\u7b97\u91cf\u306e\u7a2e\u985e\u3092\u4e0b\u56f3\u306b\u793a\u3057\u3001\u4f4e\u3044\u3082\u306e\u304b\u3089\u9ad8\u3044\u3082\u306e\u3078\u3068\u4e26\u3079\u3066\u3044\u307e\u3059\uff1a

        \\[ \\begin{aligned} & O(1) < O(\\log n) < O(n) < O(n \\log n) < O(n^2) < O(2^n) < O(n!) \\newline & \\text{\u5b9a\u6570} < \\text{\u5bfe\u6570} < \\text{\u7dda\u5f62} < \\text{\u7dda\u5f62\u5bfe\u6570} < \\text{\u4e8c\u6b21} < \\text{\u6307\u6570} < \\text{\u968e\u4e57} \\end{aligned} \\]

        \u56f3 2-9 \u00a0 Common types of time complexity

        "},{"location":"chapter_computational_complexity/time_complexity/#1-o1","title":"1. \u00a0 \u5b9a\u6570\u30aa\u30fc\u30c0\u30fc \\(O(1)\\)","text":"

        \u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\u306f\u3001\u64cd\u4f5c\u6570\u304c\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\\(n\\)\u3068\u306f\u7121\u95a2\u4fc2\u3067\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u95a2\u6570\u3067\u306f\u3001\u64cd\u4f5c\u6570size\u304c\u5927\u304d\u3044\u5834\u5408\u3067\u3082\u3001\\(n\\)\u3068\u306f\u7121\u95a2\u4fc2\u3067\u3042\u308b\u305f\u3081\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u306e\u307e\u307e\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def constant(n: int) -> int:\n    \"\"\"\u5b9a\u6570\u8907\u96d1\u5ea6\"\"\"\n    count = 0\n    size = 100000\n    for _ in range(size):\n        count += 1\n    return count\n
        time_complexity.cpp
        /* \u5b9a\u6570\u8a08\u7b97\u91cf */\nint constant(int n) {\n    int count = 0;\n    int size = 100000;\n    for (int i = 0; i < size; i++)\n        count++;\n    return count;\n}\n
        time_complexity.java
        /* \u5b9a\u6570\u8a08\u7b97\u91cf */\nint constant(int n) {\n    int count = 0;\n    int size = 100000;\n    for (int i = 0; i < size; i++)\n        count++;\n    return count;\n}\n
        time_complexity.cs
        [class]{time_complexity}-[func]{Constant}\n
        time_complexity.go
        [class]{}-[func]{constant}\n
        time_complexity.swift
        [class]{}-[func]{constant}\n
        time_complexity.js
        [class]{}-[func]{constant}\n
        time_complexity.ts
        [class]{}-[func]{constant}\n
        time_complexity.dart
        [class]{}-[func]{constant}\n
        time_complexity.rs
        [class]{}-[func]{constant}\n
        time_complexity.c
        [class]{}-[func]{constant}\n
        time_complexity.kt
        [class]{}-[func]{constant}\n
        time_complexity.rb
        [class]{}-[func]{constant}\n
        "},{"location":"chapter_computational_complexity/time_complexity/#2-on","title":"2. \u00a0 \u7dda\u5f62\u30aa\u30fc\u30c0\u30fc \\(O(n)\\)","text":"

        \u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\u306f\u3001\u64cd\u4f5c\u6570\u304c\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\\(n\\)\u3068\u7dda\u5f62\u306b\u5897\u52a0\u3059\u308b\u3053\u3068\u3092\u793a\u3057\u307e\u3059\u3002\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\u306f\u4e00\u822c\u7684\u306b\u5358\u4e00\u30eb\u30fc\u30d7\u69cb\u9020\u3067\u73fe\u308c\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def linear(n: int) -> int:\n    \"\"\"\u7dda\u5f62\u8907\u96d1\u5ea6\"\"\"\n    count = 0\n    for _ in range(n):\n        count += 1\n    return count\n
        time_complexity.cpp
        /* \u7dda\u5f62\u8a08\u7b97\u91cf */\nint linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++)\n        count++;\n    return count;\n}\n
        time_complexity.java
        /* \u7dda\u5f62\u8a08\u7b97\u91cf */\nint linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++)\n        count++;\n    return count;\n}\n
        time_complexity.cs
        [class]{time_complexity}-[func]{Linear}\n
        time_complexity.go
        [class]{}-[func]{linear}\n
        time_complexity.swift
        [class]{}-[func]{linear}\n
        time_complexity.js
        [class]{}-[func]{linear}\n
        time_complexity.ts
        [class]{}-[func]{linear}\n
        time_complexity.dart
        [class]{}-[func]{linear}\n
        time_complexity.rs
        [class]{}-[func]{linear}\n
        time_complexity.c
        [class]{}-[func]{linear}\n
        time_complexity.kt
        [class]{}-[func]{linear}\n
        time_complexity.rb
        [class]{}-[func]{linear}\n

        \u914d\u5217\u306e\u8d70\u67fb\u3084\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u8d70\u67fb\u306a\u3069\u306e\u64cd\u4f5c\u306f\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(n)\\)\u3067\u3001\\(n\\)\u306f\u914d\u5217\u307e\u305f\u306f\u30ea\u30b9\u30c8\u306e\u9577\u3055\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def array_traversal(nums: list[int]) -> int:\n    \"\"\"\u7dda\u5f62\u8907\u96d1\u5ea6\uff08\u914d\u5217\u306e\u8d70\u67fb\uff09\"\"\"\n    count = 0\n    # \u30eb\u30fc\u30d7\u56de\u6570\u306f\u914d\u5217\u306e\u9577\u3055\u306b\u6bd4\u4f8b\u3059\u308b\n    for num in nums:\n        count += 1\n    return count\n
        time_complexity.cpp
        /* \u7dda\u5f62\u8a08\u7b97\u91cf\uff08\u914d\u5217\u306e\u8d70\u67fb\uff09 */\nint arrayTraversal(vector<int> &nums) {\n    int count = 0;\n    // \u30eb\u30fc\u30d7\u56de\u6570\u306f\u914d\u5217\u306e\u9577\u3055\u306b\u6bd4\u4f8b\n    for (int num : nums) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u7dda\u5f62\u8a08\u7b97\u91cf\uff08\u914d\u5217\u306e\u8d70\u67fb\uff09 */\nint arrayTraversal(int[] nums) {\n    int count = 0;\n    // \u30eb\u30fc\u30d7\u56de\u6570\u306f\u914d\u5217\u306e\u9577\u3055\u306b\u6bd4\u4f8b\n    for (int num : nums) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.cs
        [class]{time_complexity}-[func]{ArrayTraversal}\n
        time_complexity.go
        [class]{}-[func]{arrayTraversal}\n
        time_complexity.swift
        [class]{}-[func]{arrayTraversal}\n
        time_complexity.js
        [class]{}-[func]{arrayTraversal}\n
        time_complexity.ts
        [class]{}-[func]{arrayTraversal}\n
        time_complexity.dart
        [class]{}-[func]{arrayTraversal}\n
        time_complexity.rs
        [class]{}-[func]{array_traversal}\n
        time_complexity.c
        [class]{}-[func]{arrayTraversal}\n
        time_complexity.kt
        [class]{}-[func]{arrayTraversal}\n
        time_complexity.rb
        [class]{}-[func]{array_traversal}\n

        \u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\\(n\\)\u306f\u5165\u529b\u30c7\u30fc\u30bf\u306e\u7a2e\u985e\u306b\u57fa\u3065\u3044\u3066\u6c7a\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u6700\u521d\u306e\u4f8b\u3067\u306f\u3001\\(n\\)\u306f\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\u3092\u8868\u3057\u30012\u756a\u76ee\u306e\u4f8b\u3067\u306f\u3001\u914d\u5217\u306e\u9577\u3055\\(n\\)\u304c\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\u3067\u3059\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#3-on2","title":"3. \u00a0 \u4e8c\u6b21\u30aa\u30fc\u30c0\u30fc \\(O(n^2)\\)","text":"

        \u4e8c\u6b21\u30aa\u30fc\u30c0\u30fc\u306f\u3001\u64cd\u4f5c\u6570\u304c\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\\(n\\)\u306e\u4e8c\u4e57\u306b\u6bd4\u4f8b\u3057\u3066\u5897\u52a0\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u4e8c\u6b21\u30aa\u30fc\u30c0\u30fc\u306f\u901a\u5e38\u30cd\u30b9\u30c8\u3057\u305f\u30eb\u30fc\u30d7\u3067\u73fe\u308c\u3001\u5916\u5074\u3068\u5185\u5074\u306e\u30eb\u30fc\u30d7\u306e\u4e21\u65b9\u304c\u6642\u9593\u8a08\u7b97\u91cf\\(O(n)\\)\u3092\u6301\u3061\u3001\u5168\u4f53\u306e\u8907\u96d1\u5ea6\u306f\\(O(n^2)\\)\u306b\u306a\u308a\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def quadratic(n: int) -> int:\n    \"\"\"\u4e8c\u6b21\u8907\u96d1\u5ea6\"\"\"\n    count = 0\n    # \u30eb\u30fc\u30d7\u56de\u6570\u306f\u30c7\u30fc\u30bf\u30b5\u30a4\u30ban\u306e\u4e8c\u4e57\u306b\u6bd4\u4f8b\u3059\u308b\n    for i in range(n):\n        for j in range(n):\n            count += 1\n    return count\n
        time_complexity.cpp
        /* \u4e8c\u6b21\u8a08\u7b97\u91cf */\nint quadratic(int n) {\n    int count = 0;\n    // \u30eb\u30fc\u30d7\u56de\u6570\u306f\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba n \u306e\u4e8c\u4e57\u306b\u6bd4\u4f8b\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u4e8c\u6b21\u8a08\u7b97\u91cf */\nint quadratic(int n) {\n    int count = 0;\n    // \u30eb\u30fc\u30d7\u56de\u6570\u306f\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba n \u306e\u4e8c\u4e57\u306b\u6bd4\u4f8b\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
        time_complexity.cs
        [class]{time_complexity}-[func]{Quadratic}\n
        time_complexity.go
        [class]{}-[func]{quadratic}\n
        time_complexity.swift
        [class]{}-[func]{quadratic}\n
        time_complexity.js
        [class]{}-[func]{quadratic}\n
        time_complexity.ts
        [class]{}-[func]{quadratic}\n
        time_complexity.dart
        [class]{}-[func]{quadratic}\n
        time_complexity.rs
        [class]{}-[func]{quadratic}\n
        time_complexity.c
        [class]{}-[func]{quadratic}\n
        time_complexity.kt
        [class]{}-[func]{quadratic}\n
        time_complexity.rb
        [class]{}-[func]{quadratic}\n

        \u4e0b\u56f3\u306f\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\u3001\u7dda\u5f62\u30aa\u30fc\u30c0\u30fc\u3001\u4e8c\u6b21\u30aa\u30fc\u30c0\u30fc\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3092\u6bd4\u8f03\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 2-10 \u00a0 Constant, linear, and quadratic order time complexities

        \u4f8b\u3048\u3070\u3001\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u3067\u306f\u3001\u5916\u5074\u306e\u30eb\u30fc\u30d7\u304c\\(n - 1\\)\u56de\u5b9f\u884c\u3055\u308c\u3001\u5185\u5074\u306e\u30eb\u30fc\u30d7\u304c\\(n-1\\)\u3001\\(n-2\\)\u3001...\u3001\\(2\\)\u3001\\(1\\)\u56de\u5b9f\u884c\u3055\u308c\u3001\u5e73\u5747\\(n / 2\\)\u56de\u3068\u306a\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O((n - 1) n / 2) = O(n^2)\\)\u306b\u306a\u308a\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def bubble_sort(nums: list[int]) -> int:\n    \"\"\"\u4e8c\u6b21\u8907\u96d1\u5ea6\uff08\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\uff09\"\"\"\n    count = 0  # \u30ab\u30a6\u30f3\u30bf\n    # \u5916\u5074\u306e\u30eb\u30fc\u30d7: \u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f [0, i]\n    for i in range(len(nums) - 1, 0, -1):\n        # \u5185\u5074\u306e\u30eb\u30fc\u30d7: \u672a\u30bd\u30fc\u30c8\u7bc4\u56f2 [0, i] \u306e\u6700\u5927\u8981\u7d20\u3092\u53f3\u7aef\u306b\u30b9\u30ef\u30c3\u30d7\n        for j in range(i):\n            if nums[j] > nums[j + 1]:\n                # nums[j] \u3068 nums[j + 1] \u3092\u30b9\u30ef\u30c3\u30d7\n                tmp: int = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = tmp\n                count += 3  # \u8981\u7d20\u306e\u30b9\u30ef\u30c3\u30d7\u306f3\u3064\u306e\u500b\u5225\u64cd\u4f5c\u3092\u542b\u3080\n    return count\n
        time_complexity.cpp
        /* \u4e8c\u6b21\u8a08\u7b97\u91cf\uff08\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\uff09 */\nint bubbleSort(vector<int> &nums) {\n    int count = 0; // \u30ab\u30a6\u30f3\u30bf\u30fc\n    // \u5916\u5074\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f [0, i]\n    for (int i = nums.size() - 1; i > 0; i--) {\n        // \u5185\u5074\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2 [0, i] \u306e\u6700\u5927\u8981\u7d20\u3092\u7bc4\u56f2\u306e\u53f3\u7aef\u306b\u30b9\u30ef\u30c3\u30d7\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // nums[j] \u3068 nums[j + 1] \u3092\u30b9\u30ef\u30c3\u30d7\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // \u8981\u7d20\u306e\u30b9\u30ef\u30c3\u30d7\u306b\u306f3\u3064\u306e\u500b\u5225\u64cd\u4f5c\u304c\u542b\u307e\u308c\u308b\n            }\n        }\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u4e8c\u6b21\u8a08\u7b97\u91cf\uff08\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\uff09 */\nint bubbleSort(int[] nums) {\n    int count = 0; // \u30ab\u30a6\u30f3\u30bf\u30fc\n    // \u5916\u5074\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f [0, i]\n    for (int i = nums.length - 1; i > 0; i--) {\n        // \u5185\u5074\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2 [0, i] \u306e\u6700\u5927\u8981\u7d20\u3092\u7bc4\u56f2\u306e\u53f3\u7aef\u306b\u30b9\u30ef\u30c3\u30d7\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // nums[j] \u3068 nums[j + 1] \u3092\u30b9\u30ef\u30c3\u30d7\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // \u8981\u7d20\u306e\u30b9\u30ef\u30c3\u30d7\u306b\u306f3\u3064\u306e\u500b\u5225\u64cd\u4f5c\u304c\u542b\u307e\u308c\u308b\n            }\n        }\n    }\n    return count;\n}\n
        time_complexity.cs
        [class]{time_complexity}-[func]{BubbleSort}\n
        time_complexity.go
        [class]{}-[func]{bubbleSort}\n
        time_complexity.swift
        [class]{}-[func]{bubbleSort}\n
        time_complexity.js
        [class]{}-[func]{bubbleSort}\n
        time_complexity.ts
        [class]{}-[func]{bubbleSort}\n
        time_complexity.dart
        [class]{}-[func]{bubbleSort}\n
        time_complexity.rs
        [class]{}-[func]{bubble_sort}\n
        time_complexity.c
        [class]{}-[func]{bubbleSort}\n
        time_complexity.kt
        [class]{}-[func]{bubbleSort}\n
        time_complexity.rb
        [class]{}-[func]{bubble_sort}\n
        "},{"location":"chapter_computational_complexity/time_complexity/#4-o2n","title":"4. \u00a0 \u6307\u6570\u30aa\u30fc\u30c0\u30fc \\(O(2^n)\\)","text":"

        \u751f\u7269\u5b66\u7684\u300c\u7d30\u80de\u5206\u88c2\u300d\u306f\u6307\u6570\u30aa\u30fc\u30c0\u30fc\u5897\u52a0\u306e\u5178\u578b\u4f8b\u3067\u3059\uff1a1\u3064\u306e\u7d30\u80de\u304b\u3089\u59cb\u307e\u308a\u30011\u56de\u306e\u5206\u88c2\u5f8c\u306b2\u3064\u30012\u56de\u306e\u5206\u88c2\u5f8c\u306b4\u3064\u3068\u306a\u308a\u3001\\(n\\)\u56de\u306e\u5206\u88c2\u5f8c\u306b\\(2^n\\)\u500b\u306e\u7d30\u80de\u306b\u306a\u308a\u307e\u3059\u3002

        \u4e0b\u56f3\u3068\u30b3\u30fc\u30c9\u306f\u7d30\u80de\u5206\u88c2\u30d7\u30ed\u30bb\u30b9\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\u3057\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(2^n)\\)\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def exponential(n: int) -> int:\n    \"\"\"\u6307\u6570\u8907\u96d1\u5ea6\uff08\u30eb\u30fc\u30d7\u5b9f\u88c5\uff09\"\"\"\n    count = 0\n    base = 1\n    # \u30bb\u30eb\u306f\u6bce\u56de2\u3064\u306b\u5206\u88c2\u3057\u30011, 2, 4, 8, ..., 2^(n-1) \u306e\u6570\u5217\u3092\u5f62\u6210\u3059\u308b\n    for _ in range(n):\n        for _ in range(base):\n            count += 1\n        base *= 2\n    # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count\n
        time_complexity.cpp
        /* \u6307\u6570\u8a08\u7b97\u91cf\uff08\u30eb\u30fc\u30d7\u5b9f\u88c5\uff09 */\nint exponential(int n) {\n    int count = 0, base = 1;\n    // \u30bb\u30eb\u306f\u6bce\u30e9\u30a6\u30f3\u30c92\u3064\u306b\u5206\u88c2\u3057\u3001\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1) \u3092\u5f62\u6210\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
        time_complexity.java
        /* \u6307\u6570\u8a08\u7b97\u91cf\uff08\u30eb\u30fc\u30d7\u5b9f\u88c5\uff09 */\nint exponential(int n) {\n    int count = 0, base = 1;\n    // \u30bb\u30eb\u306f\u6bce\u30e9\u30a6\u30f3\u30c92\u3064\u306b\u5206\u88c2\u3057\u3001\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1) \u3092\u5f62\u6210\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
        time_complexity.cs
        [class]{time_complexity}-[func]{Exponential}\n
        time_complexity.go
        [class]{}-[func]{exponential}\n
        time_complexity.swift
        [class]{}-[func]{exponential}\n
        time_complexity.js
        [class]{}-[func]{exponential}\n
        time_complexity.ts
        [class]{}-[func]{exponential}\n
        time_complexity.dart
        [class]{}-[func]{exponential}\n
        time_complexity.rs
        [class]{}-[func]{exponential}\n
        time_complexity.c
        [class]{}-[func]{exponential}\n
        time_complexity.kt
        [class]{}-[func]{exponential}\n
        time_complexity.rb
        [class]{}-[func]{exponential}\n

        \u56f3 2-11 \u00a0 Exponential order time complexity

        \u5b9f\u969b\u306b\u306f\u3001\u6307\u6570\u30aa\u30fc\u30c0\u30fc\u306f\u518d\u5e30\u95a2\u6570\u3067\u3088\u304f\u73fe\u308c\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3001\u518d\u5e30\u7684\u306b2\u3064\u306e\u534a\u5206\u306b\u5206\u5272\u3057\u3001\\(n\\)\u56de\u306e\u5206\u5272\u5f8c\u306b\u505c\u6b62\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def exp_recur(n: int) -> int:\n    \"\"\"\u6307\u6570\u8907\u96d1\u5ea6\uff08\u518d\u5e30\u5b9f\u88c5\uff09\"\"\"\n    if n == 1:\n        return 1\n    return exp_recur(n - 1) + exp_recur(n - 1) + 1\n
        time_complexity.cpp
        /* \u6307\u6570\u8a08\u7b97\u91cf\uff08\u518d\u5e30\u5b9f\u88c5\uff09 */\nint expRecur(int n) {\n    if (n == 1)\n        return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
        time_complexity.java
        /* \u6307\u6570\u8a08\u7b97\u91cf\uff08\u518d\u5e30\u5b9f\u88c5\uff09 */\nint expRecur(int n) {\n    if (n == 1)\n        return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
        time_complexity.cs
        [class]{time_complexity}-[func]{ExpRecur}\n
        time_complexity.go
        [class]{}-[func]{expRecur}\n
        time_complexity.swift
        [class]{}-[func]{expRecur}\n
        time_complexity.js
        [class]{}-[func]{expRecur}\n
        time_complexity.ts
        [class]{}-[func]{expRecur}\n
        time_complexity.dart
        [class]{}-[func]{expRecur}\n
        time_complexity.rs
        [class]{}-[func]{exp_recur}\n
        time_complexity.c
        [class]{}-[func]{expRecur}\n
        time_complexity.kt
        [class]{}-[func]{expRecur}\n
        time_complexity.rb
        [class]{}-[func]{exp_recur}\n

        \u6307\u6570\u30aa\u30fc\u30c0\u30fc\u306e\u5897\u52a0\u306f\u6975\u3081\u3066\u6025\u901f\u3067\u3001\u5168\u6570\u63a2\u7d22\u6cd5\uff08\u30d6\u30eb\u30fc\u30c8\u30d5\u30a9\u30fc\u30b9\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306a\u3069\uff09\u3067\u3088\u304f\u898b\u3089\u308c\u307e\u3059\u3002\u5927\u898f\u6a21\u554f\u984c\u3067\u306f\u3001\u6307\u6570\u30aa\u30fc\u30c0\u30fc\u306f\u53d7\u3051\u5165\u308c\u3089\u308c\u305a\u3001\u3057\u3070\u3057\u3070\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3084\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u89e3\u6c7a\u7b56\u3068\u3057\u3066\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#5-olog-n","title":"5. \u00a0 \u5bfe\u6570\u30aa\u30fc\u30c0\u30fc \\(O(\\log n)\\)","text":"

        \u6307\u6570\u30aa\u30fc\u30c0\u30fc\u3068\u306f\u5bfe\u7167\u7684\u306b\u3001\u5bfe\u6570\u30aa\u30fc\u30c0\u30fc\u306f\u300c\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u30b5\u30a4\u30ba\u304c\u534a\u5206\u306b\u306a\u308b\u300d\u72b6\u6cc1\u3092\u53cd\u6620\u3057\u307e\u3059\u3002\u5165\u529b\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\u304c\\(n\\)\u306e\u5834\u5408\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u30b5\u30a4\u30ba\u304c\u534a\u5206\u306b\u306a\u308b\u305f\u3081\u3001\u53cd\u5fa9\u56de\u6570\u306f\\(\\log_2 n\\)\u3067\u3001\u3053\u308c\u306f\\(2^n\\)\u306e\u9006\u95a2\u6570\u3067\u3059\u3002

        \u4e0b\u56f3\u3068\u30b3\u30fc\u30c9\u306f\u300c\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u534a\u5206\u306b\u3059\u308b\u300d\u30d7\u30ed\u30bb\u30b9\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\u3057\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(\\log_2 n)\\)\u3067\u3001\u4e00\u822c\u7684\u306b\\(O(\\log n)\\)\u3068\u7701\u7565\u3055\u308c\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def logarithmic(n: int) -> int:\n    \"\"\"\u5bfe\u6570\u8907\u96d1\u5ea6\uff08\u30eb\u30fc\u30d7\u5b9f\u88c5\uff09\"\"\"\n    count = 0\n    while n > 1:\n        n = n / 2\n        count += 1\n    return count\n
        time_complexity.cpp
        /* \u5bfe\u6570\u8a08\u7b97\u91cf\uff08\u30eb\u30fc\u30d7\u5b9f\u88c5\uff09 */\nint logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u5bfe\u6570\u8a08\u7b97\u91cf\uff08\u30eb\u30fc\u30d7\u5b9f\u88c5\uff09 */\nint logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
        time_complexity.cs
        [class]{time_complexity}-[func]{Logarithmic}\n
        time_complexity.go
        [class]{}-[func]{logarithmic}\n
        time_complexity.swift
        [class]{}-[func]{logarithmic}\n
        time_complexity.js
        [class]{}-[func]{logarithmic}\n
        time_complexity.ts
        [class]{}-[func]{logarithmic}\n
        time_complexity.dart
        [class]{}-[func]{logarithmic}\n
        time_complexity.rs
        [class]{}-[func]{logarithmic}\n
        time_complexity.c
        [class]{}-[func]{logarithmic}\n
        time_complexity.kt
        [class]{}-[func]{logarithmic}\n
        time_complexity.rb
        [class]{}-[func]{logarithmic}\n

        \u56f3 2-12 \u00a0 Logarithmic order time complexity

        \u6307\u6570\u30aa\u30fc\u30c0\u30fc\u3068\u540c\u69d8\u306b\u3001\u5bfe\u6570\u30aa\u30fc\u30c0\u30fc\u3082\u518d\u5e30\u95a2\u6570\u3067\u983b\u7e41\u306b\u73fe\u308c\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u306f\u9ad8\u3055\\(\\log_2 n\\)\u306e\u518d\u5e30\u6728\u3092\u5f62\u6210\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def log_recur(n: int) -> int:\n    \"\"\"\u5bfe\u6570\u8907\u96d1\u5ea6\uff08\u518d\u5e30\u5b9f\u88c5\uff09\"\"\"\n    if n <= 1:\n        return 0\n    return log_recur(n / 2) + 1\n
        time_complexity.cpp
        /* \u5bfe\u6570\u8a08\u7b97\u91cf\uff08\u518d\u5e30\u5b9f\u88c5\uff09 */\nint logRecur(int n) {\n    if (n <= 1)\n        return 0;\n    return logRecur(n / 2) + 1;\n}\n
        time_complexity.java
        /* \u5bfe\u6570\u8a08\u7b97\u91cf\uff08\u518d\u5e30\u5b9f\u88c5\uff09 */\nint logRecur(int n) {\n    if (n <= 1)\n        return 0;\n    return logRecur(n / 2) + 1;\n}\n
        time_complexity.cs
        [class]{time_complexity}-[func]{LogRecur}\n
        time_complexity.go
        [class]{}-[func]{logRecur}\n
        time_complexity.swift
        [class]{}-[func]{logRecur}\n
        time_complexity.js
        [class]{}-[func]{logRecur}\n
        time_complexity.ts
        [class]{}-[func]{logRecur}\n
        time_complexity.dart
        [class]{}-[func]{logRecur}\n
        time_complexity.rs
        [class]{}-[func]{log_recur}\n
        time_complexity.c
        [class]{}-[func]{logRecur}\n
        time_complexity.kt
        [class]{}-[func]{logRecur}\n
        time_complexity.rb
        [class]{}-[func]{log_recur}\n

        \u5bfe\u6570\u30aa\u30fc\u30c0\u30fc\u306f\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306b\u57fa\u3065\u304f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5178\u578b\u3067\u3001\u300c\u591a\u304f\u306b\u5206\u5272\u300d\u3068\u300c\u8907\u96d1\u306a\u554f\u984c\u3092\u5358\u7d14\u5316\u300d\u3059\u308b\u30a2\u30d7\u30ed\u30fc\u30c1\u3092\u4f53\u73fe\u3057\u3066\u3044\u307e\u3059\u3002\u5897\u52a0\u304c\u9045\u304f\u3001\u5b9a\u6570\u30aa\u30fc\u30c0\u30fc\u306e\u6b21\u306b\u6700\u3082\u7406\u60f3\u7684\u306a\u6642\u9593\u8a08\u7b97\u91cf\u3067\u3059\u3002

        \\(O(\\log n)\\)\u306e\u5e95\u306f\u4f55\u3067\u3059\u304b\uff1f

        \u6280\u8853\u7684\u306b\u306f\u3001\u300c\\(m\\)\u306b\u5206\u5272\u300d\u306f\u6642\u9593\u8a08\u7b97\u91cf\\(O(\\log_m n)\\)\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002\u5bfe\u6570\u306e\u5e95\u5909\u66f4\u516c\u5f0f\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u7570\u306a\u308b\u5bfe\u6570\u8907\u96d1\u5ea6\u3092\u7b49\u4fa1\u306b\u3067\u304d\u307e\u3059\uff1a

        \\[ O(\\log_m n) = O(\\log_k n / \\log_k m) = O(\\log_k n) \\]

        \u3053\u308c\u306f\u3001\u5e95\\(m\\)\u3092\u8907\u96d1\u5ea6\u306b\u5f71\u97ff\u3092\u4e0e\u3048\u308b\u3053\u3068\u306a\u304f\u5909\u66f4\u3067\u304d\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u3057\u3070\u3057\u3070\u5e95\\(m\\)\u3092\u7701\u7565\u3057\u3001\u5358\u306b\u5bfe\u6570\u30aa\u30fc\u30c0\u30fc\u3092\\(O(\\log n)\\)\u3068\u8868\u8a18\u3057\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#6-on-log-n","title":"6. \u00a0 \u7dda\u5f62\u5bfe\u6570\u30aa\u30fc\u30c0\u30fc \\(O(n \\log n)\\)","text":"

        \u7dda\u5f62\u5bfe\u6570\u30aa\u30fc\u30c0\u30fc\u306f\u30cd\u30b9\u30c8\u3057\u305f\u30eb\u30fc\u30d7\u3067\u3088\u304f\u73fe\u308c\u30012\u3064\u306e\u30eb\u30fc\u30d7\u306e\u8907\u96d1\u5ea6\u304c\u305d\u308c\u305e\u308c\\(O(\\log n)\\)\u3068\\(O(n)\\)\u3067\u3059\u3002\u95a2\u9023\u3059\u308b\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def linear_log_recur(n: int) -> int:\n    \"\"\"\u7dda\u5f62\u5bfe\u6570\u8907\u96d1\u5ea6\"\"\"\n    if n <= 1:\n        return 1\n    count: int = linear_log_recur(n // 2) + linear_log_recur(n // 2)\n    for _ in range(n):\n        count += 1\n    return count\n
        time_complexity.cpp
        /* \u7dda\u5f62\u5bfe\u6570\u8a08\u7b97\u91cf */\nint linearLogRecur(int n) {\n    if (n <= 1)\n        return 1;\n    int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u7dda\u5f62\u5bfe\u6570\u8a08\u7b97\u91cf */\nint linearLogRecur(int n) {\n    if (n <= 1)\n        return 1;\n    int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.cs
        [class]{time_complexity}-[func]{LinearLogRecur}\n
        time_complexity.go
        [class]{}-[func]{linearLogRecur}\n
        time_complexity.swift
        [class]{}-[func]{linearLogRecur}\n
        time_complexity.js
        [class]{}-[func]{linearLogRecur}\n
        time_complexity.ts
        [class]{}-[func]{linearLogRecur}\n
        time_complexity.dart
        [class]{}-[func]{linearLogRecur}\n
        time_complexity.rs
        [class]{}-[func]{linear_log_recur}\n
        time_complexity.c
        [class]{}-[func]{linearLogRecur}\n
        time_complexity.kt
        [class]{}-[func]{linearLogRecur}\n
        time_complexity.rb
        [class]{}-[func]{linear_log_recur}\n

        \u4e0b\u56f3\u306f\u7dda\u5f62\u5bfe\u6570\u30aa\u30fc\u30c0\u30fc\u304c\u3069\u306e\u3088\u3046\u306b\u751f\u6210\u3055\u308c\u308b\u304b\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u4e8c\u5206\u6728\u306e\u5404\u30ec\u30d9\u30eb\u306b\u306f\\(n\\)\u500b\u306e\u64cd\u4f5c\u304c\u3042\u308a\u3001\u6728\u306b\u306f\\(\\log_2 n + 1\\)\u30ec\u30d9\u30eb\u304c\u3042\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n \\log n)\\)\u306b\u306a\u308a\u307e\u3059\u3002

        \u56f3 2-13 \u00a0 Linear-logarithmic order time complexity

        \u4e3b\u6d41\u306e\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u901a\u5e38\\(O(n \\log n)\\)\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3092\u6301\u3061\u3001\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u3001\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u3001\u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\u306a\u3069\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#7-on","title":"7. \u00a0 \u968e\u4e57\u30aa\u30fc\u30c0\u30fc \\(O(n!)\\)","text":"

        \u968e\u4e57\u30aa\u30fc\u30c0\u30fc\u306f\u300c\u5168\u9806\u5217\u300d\u306e\u6570\u5b66\u554f\u984c\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002\\(n\\)\u500b\u306e\u7570\u306a\u308b\u8981\u7d20\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u53ef\u80fd\u306a\u9806\u5217\u306e\u7dcf\u6570\u306f\uff1a

        \\[ n! = n \\times (n - 1) \\times (n - 2) \\times \\dots \\times 2 \\times 1 \\]

        \u968e\u4e57\u306f\u901a\u5e38\u518d\u5e30\u3092\u4f7f\u7528\u3057\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3068\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u3001\u7b2c1\u30ec\u30d9\u30eb\u306f\\(n\\)\u500b\u306e\u5206\u5c90\u306b\u5206\u5272\u3055\u308c\u3001\u7b2c2\u30ec\u30d9\u30eb\u306f\\(n - 1\\)\u500b\u306e\u5206\u5c90\u306b\u5206\u5272\u3055\u308c\u3001\u7b2c\\(n\\)\u30ec\u30d9\u30eb\u5f8c\u306b\u505c\u6b62\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def factorial_recur(n: int) -> int:\n    \"\"\"\u968e\u4e57\u8907\u96d1\u5ea6\uff08\u518d\u5e30\u5b9f\u88c5\uff09\"\"\"\n    if n == 0:\n        return 1\n    count = 0\n    # 1\u3064\u304b\u3089n\u306b\u5206\u5c90\n    for _ in range(n):\n        count += factorial_recur(n - 1)\n    return count\n
        time_complexity.cpp
        /* \u968e\u4e57\u8a08\u7b97\u91cf\uff08\u518d\u5e30\u5b9f\u88c5\uff09 */\nint factorialRecur(int n) {\n    if (n == 0)\n        return 1;\n    int count = 0;\n    // 1\u304b\u3089 n \u306b\u5206\u88c2\n    for (int i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u968e\u4e57\u8a08\u7b97\u91cf\uff08\u518d\u5e30\u5b9f\u88c5\uff09 */\nint factorialRecur(int n) {\n    if (n == 0)\n        return 1;\n    int count = 0;\n    // 1\u304b\u3089 n \u306b\u5206\u88c2\n    for (int i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
        time_complexity.cs
        [class]{time_complexity}-[func]{FactorialRecur}\n
        time_complexity.go
        [class]{}-[func]{factorialRecur}\n
        time_complexity.swift
        [class]{}-[func]{factorialRecur}\n
        time_complexity.js
        [class]{}-[func]{factorialRecur}\n
        time_complexity.ts
        [class]{}-[func]{factorialRecur}\n
        time_complexity.dart
        [class]{}-[func]{factorialRecur}\n
        time_complexity.rs
        [class]{}-[func]{factorial_recur}\n
        time_complexity.c
        [class]{}-[func]{factorialRecur}\n
        time_complexity.kt
        [class]{}-[func]{factorialRecur}\n
        time_complexity.rb
        [class]{}-[func]{factorial_recur}\n

        \u56f3 2-14 \u00a0 Factorial order time complexity

        \u968e\u4e57\u30aa\u30fc\u30c0\u30fc\u306f\u6307\u6570\u30aa\u30fc\u30c0\u30fc\u3088\u308a\u3082\u3055\u3089\u306b\u901f\u304f\u5897\u52a0\u3059\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3088\u308a\u5927\u304d\u306a\\(n\\)\u5024\u3067\u306f\u53d7\u3051\u5165\u308c\u3089\u308c\u307e\u305b\u3093\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#235","title":"2.3.5 \u00a0 \u6700\u60aa\u3001\u6700\u826f\u3001\u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf","text":"

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6642\u9593\u52b9\u7387\u306f\u56fa\u5b9a\u3055\u308c\u3066\u3044\u306a\u3044\u3053\u3068\u304c\u591a\u304f\u3001\u5165\u529b\u30c7\u30fc\u30bf\u306e\u5206\u5e03\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002\u9577\u3055\\(n\\)\u306e\u914d\u5217nums\u304c\u3042\u308a\u3001\\(1\\)\u304b\u3089\\(n\\)\u307e\u3067\u306e\u6570\u3067\u69cb\u6210\u3055\u308c\u3001\u305d\u308c\u305e\u308c\u304c\u4e00\u5ea6\u3060\u3051\u73fe\u308c\u307e\u3059\u304c\u3001\u30e9\u30f3\u30c0\u30e0\u306b\u30b7\u30e3\u30c3\u30d5\u30eb\u3055\u308c\u305f\u9806\u5e8f\u3067\u3042\u308b\u3068\u3057\u307e\u3059\u3002\u30bf\u30b9\u30af\u306f\u8981\u7d20\\(1\\)\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\u3053\u3068\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u7d50\u8ad6\u3092\u5c0e\u3051\u307e\u3059\uff1a

        • nums = [?, ?, ..., 1]\u306e\u5834\u5408\u3001\u3064\u307e\u308a\u6700\u5f8c\u306e\u8981\u7d20\u304c\\(1\\)\u306e\u5834\u5408\u3001\u914d\u5217\u306e\u5b8c\u5168\u306a\u8d70\u67fb\u304c\u5fc5\u8981\u3067\u3001\u6700\u60aa\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\\(O(n)\\)\u3092\u9054\u6210\u3057\u307e\u3059\u3002
        • nums = [1, ?, ?, ...]\u306e\u5834\u5408\u3001\u3064\u307e\u308a\u6700\u521d\u306e\u8981\u7d20\u304c\\(1\\)\u306e\u5834\u5408\u3001\u914d\u5217\u306e\u9577\u3055\u306b\u95a2\u4fc2\u306a\u304f\u3001\u3055\u3089\u306a\u308b\u8d70\u67fb\u306f\u4e0d\u8981\u3067\u3001\u6700\u826f\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\\(\\Omega(1)\\)\u3092\u9054\u6210\u3057\u307e\u3059\u3002

        \u300c\u6700\u60aa\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\u300d\u306f\u6f38\u8fd1\u4e0a\u9650\u306b\u5bfe\u5fdc\u3057\u3001\u5927\u304d\u306a\\(O\\)\u8a18\u6cd5\u3067\u8868\u3055\u308c\u307e\u3059\u3002\u5bfe\u5fdc\u3057\u3066\u3001\u300c\u6700\u826f\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\u300d\u306f\u6f38\u8fd1\u4e0b\u9650\u306b\u5bfe\u5fdc\u3057\u3001\\(\\Omega\\)\u3067\u8868\u3055\u308c\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby worst_best_time_complexity.py
        def random_numbers(n: int) -> list[int]:\n    \"\"\"\u8981\u7d20 1, 2, ..., n \u3092\u542b\u3080\u914d\u5217\u3092\u751f\u6210\u3001\u9806\u5e8f\u306f\u30b7\u30e3\u30c3\u30d5\u30eb\"\"\"\n    # \u914d\u5217 nums = 1, 2, 3, ..., n \u3092\u751f\u6210\n    nums = [i for i in range(1, n + 1)]\n    # \u914d\u5217\u8981\u7d20\u3092\u30e9\u30f3\u30c0\u30e0\u306b\u30b7\u30e3\u30c3\u30d5\u30eb\n    random.shuffle(nums)\n    return nums\n\ndef find_one(nums: list[int]) -> int:\n    \"\"\"\u914d\u5217 nums \u3067\u6570\u5024 1 \u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22\"\"\"\n    for i in range(len(nums)):\n        # \u8981\u7d20 1 \u304c\u914d\u5217\u306e\u6700\u521d\u306b\u3042\u308b\u5834\u5408\u3001\u6700\u826f\u6642\u9593\u8a08\u7b97\u91cf O(1) \u3092\u9054\u6210\n        # \u8981\u7d20 1 \u304c\u914d\u5217\u306e\u6700\u5f8c\u306b\u3042\u308b\u5834\u5408\u3001\u6700\u60aa\u6642\u9593\u8a08\u7b97\u91cf O(n) \u3092\u9054\u6210\n        if nums[i] == 1:\n            return i\n    return -1\n
        worst_best_time_complexity.cpp
        /* \u8981\u7d20 {1, 2, ..., n} \u3092\u30e9\u30f3\u30c0\u30e0\u306b\u30b7\u30e3\u30c3\u30d5\u30eb\u3057\u305f\u914d\u5217\u3092\u751f\u6210 */\nvector<int> randomNumbers(int n) {\n    vector<int> nums(n);\n    // \u914d\u5217 nums = { 1, 2, 3, ..., n } \u3092\u751f\u6210\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // \u30b7\u30b9\u30c6\u30e0\u6642\u523b\u3092\u4f7f\u7528\u3057\u3066\u30e9\u30f3\u30c0\u30e0\u30b7\u30fc\u30c9\u3092\u751f\u6210\n    unsigned seed = chrono::system_clock::now().time_since_epoch().count();\n    // \u914d\u5217\u8981\u7d20\u3092\u30e9\u30f3\u30c0\u30e0\u306b\u30b7\u30e3\u30c3\u30d5\u30eb\n    shuffle(nums.begin(), nums.end(), default_random_engine(seed));\n    return nums;\n}\n\n/* \u914d\u5217 nums \u3067\u6570\u50241\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u898b\u3064\u3051\u308b */\nint findOne(vector<int> &nums) {\n    for (int i = 0; i < nums.size(); i++) {\n        // \u8981\u7d201\u304c\u914d\u5217\u306e\u5148\u982d\u306b\u3042\u308b\u5834\u5408\u3001\u6700\u826f\u6642\u9593\u8a08\u7b97\u91cf O(1) \u3092\u9054\u6210\n        // \u8981\u7d201\u304c\u914d\u5217\u306e\u672b\u5c3e\u306b\u3042\u308b\u5834\u5408\u3001\u6700\u60aa\u6642\u9593\u8a08\u7b97\u91cf O(n) \u3092\u9054\u6210\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
        worst_best_time_complexity.java
        /* \u8981\u7d20 {1, 2, ..., n} \u3092\u30e9\u30f3\u30c0\u30e0\u306b\u30b7\u30e3\u30c3\u30d5\u30eb\u3057\u305f\u914d\u5217\u3092\u751f\u6210 */\nint[] randomNumbers(int n) {\n    Integer[] nums = new Integer[n];\n    // \u914d\u5217 nums = { 1, 2, 3, ..., n } \u3092\u751f\u6210\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // \u914d\u5217\u8981\u7d20\u3092\u30e9\u30f3\u30c0\u30e0\u306b\u30b7\u30e3\u30c3\u30d5\u30eb\n    Collections.shuffle(Arrays.asList(nums));\n    // Integer[] -> int[]\n    int[] res = new int[n];\n    for (int i = 0; i < n; i++) {\n        res[i] = nums[i];\n    }\n    return res;\n}\n\n/* \u914d\u5217 nums \u3067\u6570\u50241\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u898b\u3064\u3051\u308b */\nint findOne(int[] nums) {\n    for (int i = 0; i < nums.length; i++) {\n        // \u8981\u7d201\u304c\u914d\u5217\u306e\u5148\u982d\u306b\u3042\u308b\u5834\u5408\u3001\u6700\u826f\u6642\u9593\u8a08\u7b97\u91cf O(1) \u3092\u9054\u6210\n        // \u8981\u7d201\u304c\u914d\u5217\u306e\u672b\u5c3e\u306b\u3042\u308b\u5834\u5408\u3001\u6700\u60aa\u6642\u9593\u8a08\u7b97\u91cf O(n) \u3092\u9054\u6210\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
        worst_best_time_complexity.cs
        [class]{worst_best_time_complexity}-[func]{RandomNumbers}\n\n[class]{worst_best_time_complexity}-[func]{FindOne}\n
        worst_best_time_complexity.go
        [class]{}-[func]{randomNumbers}\n\n[class]{}-[func]{findOne}\n
        worst_best_time_complexity.swift
        [class]{}-[func]{randomNumbers}\n\n[class]{}-[func]{findOne}\n
        worst_best_time_complexity.js
        [class]{}-[func]{randomNumbers}\n\n[class]{}-[func]{findOne}\n
        worst_best_time_complexity.ts
        [class]{}-[func]{randomNumbers}\n\n[class]{}-[func]{findOne}\n
        worst_best_time_complexity.dart
        [class]{}-[func]{randomNumbers}\n\n[class]{}-[func]{findOne}\n
        worst_best_time_complexity.rs
        [class]{}-[func]{random_numbers}\n\n[class]{}-[func]{find_one}\n
        worst_best_time_complexity.c
        [class]{}-[func]{randomNumbers}\n\n[class]{}-[func]{findOne}\n
        worst_best_time_complexity.kt
        [class]{}-[func]{randomNumbers}\n\n[class]{}-[func]{findOne}\n
        worst_best_time_complexity.rb
        [class]{}-[func]{random_numbers}\n\n[class]{}-[func]{find_one}\n

        \u6700\u826f\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\u306f\u5b9f\u969b\u306b\u306f\u307b\u3068\u3093\u3069\u4f7f\u7528\u3055\u308c\u306a\u3044\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u901a\u5e38\u306f\u975e\u5e38\u306b\u4f4e\u3044\u78ba\u7387\u3067\u306e\u307f\u9054\u6210\u53ef\u80fd\u3067\u3001\u8aa4\u89e3\u3092\u62db\u304f\u53ef\u80fd\u6027\u304c\u3042\u308b\u304b\u3089\u3067\u3059\u3002\u6700\u60aa\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\u306f\u3088\u308a\u5b9f\u7528\u7684\u3067\u3001\u52b9\u7387\u306e\u5b89\u5168\u5024\u3092\u63d0\u4f9b\u3057\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u81ea\u4fe1\u3092\u6301\u3063\u3066\u4f7f\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002

        \u4e0a\u8a18\u306e\u4f8b\u304b\u3089\u3001\u6700\u60aa\u30b1\u30fc\u30b9\u3068\u6700\u826f\u30b1\u30fc\u30b9\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\u4e21\u65b9\u3068\u3082\u300c\u7279\u6b8a\u306a\u30c7\u30fc\u30bf\u5206\u5e03\u300d\u4e0b\u3067\u306e\u307f\u767a\u751f\u3057\u3001\u767a\u751f\u78ba\u7387\u304c\u5c0f\u3055\u304f\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u52b9\u7387\u3092\u6b63\u78ba\u306b\u53cd\u6620\u3057\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308b\u3053\u3068\u304c\u660e\u3089\u304b\u3067\u3059\u3002\u5bfe\u7167\u7684\u306b\u3001\u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf\u306f\u30e9\u30f3\u30c0\u30e0\u5165\u529b\u30c7\u30fc\u30bf\u4e0b\u3067\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u53cd\u6620\u3067\u304d\u3001\\(\\Theta\\)\u8a18\u6cd5\u3067\u8868\u3055\u308c\u307e\u3059\u3002

        \u4e00\u90e8\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u306f\u3001\u30e9\u30f3\u30c0\u30e0\u30c7\u30fc\u30bf\u5206\u5e03\u4e0b\u3067\u306e\u5e73\u5747\u30b1\u30fc\u30b9\u3092\u7c21\u5358\u306b\u63a8\u5b9a\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u524d\u8ff0\u306e\u4f8b\u3067\u306f\u3001\u5165\u529b\u914d\u5217\u304c\u30b7\u30e3\u30c3\u30d5\u30eb\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u8981\u7d20\\(1\\)\u304c\u4efb\u610f\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u73fe\u308c\u308b\u78ba\u7387\u306f\u7b49\u3057\u3044\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5e73\u5747\u30eb\u30fc\u30d7\u6570\u306f\u914d\u5217\u9577\u3055\u306e\u534a\u5206\\(n / 2\\)\u3067\u3001\u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(\\Theta(n / 2) = \\Theta(n)\\)\u3067\u3059\u3002

        \u3057\u304b\u3057\u3001\u3088\u308a\u8907\u96d1\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf\u3092\u8a08\u7b97\u3059\u308b\u3053\u3068\u306f\u975e\u5e38\u306b\u56f0\u96e3\u3067\u3059\u3002\u30c7\u30fc\u30bf\u5206\u5e03\u4e0b\u3067\u306e\u5168\u4f53\u7684\u306a\u6570\u5b66\u7684\u671f\u5f85\u5024\u3092\u5206\u6790\u3059\u308b\u3053\u3068\u304c\u56f0\u96e3\u3060\u304b\u3089\u3067\u3059\u3002\u305d\u306e\u3088\u3046\u306a\u5834\u5408\u3001\u901a\u5e38\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u5224\u65ad\u3059\u308b\u57fa\u6e96\u3068\u3057\u3066\u6700\u60aa\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        \\(\\Theta\\)\u8a18\u53f7\u306f\u306a\u305c\u307b\u3068\u3093\u3069\u898b\u3089\u308c\u306a\u3044\u306e\u3067\u3059\u304b\uff1f

        \u304a\u305d\u3089\u304f\\(O\\)\u8a18\u6cd5\u304c\u3088\u308a\u4e00\u822c\u7684\u306b\u8a71\u3055\u308c\u308b\u305f\u3081\u3001\u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf\u3092\u8868\u3059\u305f\u3081\u306b\u3088\u304f\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u53b3\u5bc6\u306b\u8a00\u3048\u3070\u3001\u3053\u306e\u5b9f\u8df5\u306f\u6b63\u78ba\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u3053\u306e\u672c\u3084\u4ed6\u306e\u8cc7\u6599\u3067\u300c\u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf\\(O(n)\\)\u300d\u306e\u3088\u3046\u306a\u8868\u73fe\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u306f\u3001\u76f4\u63a5\\(\\Theta(n)\\)\u3068\u3057\u3066\u7406\u89e3\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        "},{"location":"chapter_data_structure/","title":"\u7b2c 3 \u7ae0 \u00a0 \u30c7\u30fc\u30bf\u69cb\u9020","text":"

        Abstract

        \u30c7\u30fc\u30bf\u69cb\u9020\u306f\u5805\u7262\u3067\u591a\u69d8\u306a\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3068\u3057\u3066\u6a5f\u80fd\u3057\u307e\u3059\u3002

        \u30c7\u30fc\u30bf\u306e\u6574\u7136\u3068\u3057\u305f\u7d44\u7e54\u5316\u306e\u305f\u3081\u306e\u8a2d\u8a08\u56f3\u3092\u63d0\u4f9b\u3057\u3001\u305d\u306e\u4e0a\u3067\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u751f\u304d\u751f\u304d\u3068\u52d5\u304d\u51fa\u3057\u307e\u3059\u3002

        "},{"location":"chapter_data_structure/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 3.1 \u00a0 \u30c7\u30fc\u30bf\u69cb\u9020\u306e\u5206\u985e
        • 3.2 \u00a0 \u57fa\u672c\u30c7\u30fc\u30bf\u578b
        • 3.3 \u00a0 \u6570\u5024\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0 *
        • 3.4 \u00a0 \u6587\u5b57\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0 *
        • 3.5 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_data_structure/basic_data_types/","title":"3.2 \u00a0 \u57fa\u672c\u30c7\u30fc\u30bf\u578b","text":"

        \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u5185\u306e\u30c7\u30fc\u30bf\u306b\u3064\u3044\u3066\u8003\u3048\u308b\u969b\u3001\u30c6\u30ad\u30b9\u30c8\u3001\u753b\u50cf\u3001\u52d5\u753b\u3001\u97f3\u58f0\u30013D\u30e2\u30c7\u30eb\u306a\u3069\u3001\u69d8\u3005\u306a\u5f62\u5f0f\u304c\u601d\u3044\u6d6e\u304b\u3073\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u7d44\u7e54\u7684\u306a\u5f62\u5f0f\u306f\u7570\u306a\u308a\u307e\u3059\u304c\u3001\u3059\u3079\u3066\u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u304b\u3089\u69cb\u6210\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        **\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3068\u306f\u3001CPU\u304c\u76f4\u63a5\u64cd\u4f5c\u3067\u304d\u308b\u3082\u306e**\u3067\u3042\u308a\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u76f4\u63a5\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u4e3b\u306b\u4ee5\u4e0b\u304c\u542b\u307e\u308c\u307e\u3059\u3002

        • \u6574\u6570\u578b\uff1abyte\u3001short\u3001int\u3001long
        • \u6d6e\u52d5\u5c0f\u6570\u70b9\u578b\uff1afloat\u3001double\u3001\u5c0f\u6570\u3092\u8868\u73fe\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528
        • \u6587\u5b57\u578b\uff1achar\u3001\u69d8\u3005\u306a\u8a00\u8a9e\u306e\u6587\u5b57\u3001\u53e5\u8aad\u70b9\u3001\u3055\u3089\u306b\u306f\u7d75\u6587\u5b57\u3092\u8868\u73fe\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528
        • \u30d6\u30fc\u30eb\u578b\uff1abool\u3001\u300c\u306f\u3044\u300d\u307e\u305f\u306f\u300c\u3044\u3044\u3048\u300d\u306e\u5224\u65ad\u3092\u8868\u73fe\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528

        \u57fa\u672c\u30c7\u30fc\u30bf\u578b\u306f\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u5185\u3067\u4e8c\u9032\u5f62\u5f0f\u3067\u683c\u7d0d\u3055\u308c\u307e\u3059\u30021\u3064\u306e\u4e8c\u9032\u6841\u306f1\u30d3\u30c3\u30c8\u3067\u3059\u3002\u307b\u3068\u3093\u3069\u306e\u73fe\u4ee3\u7684\u306a\u30aa\u30da\u30ec\u30fc\u30c6\u30a3\u30f3\u30b0\u30b7\u30b9\u30c6\u30e0\u3067\u306f\u30011\u30d0\u30a4\u30c8\u306f8\u30d3\u30c3\u30c8\u3067\u69cb\u6210\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        \u57fa\u672c\u30c7\u30fc\u30bf\u578b\u306e\u5024\u306e\u7bc4\u56f2\u306f\u3001\u305d\u308c\u3089\u304c\u5360\u3081\u308b\u7a7a\u9593\u306e\u30b5\u30a4\u30ba\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002\u4ee5\u4e0b\u3067\u306f\u3001Java\u3092\u4f8b\u306b\u8aac\u660e\u3057\u307e\u3059\u3002

        • \u6574\u6570\u578bbyte\u306f1\u30d0\u30a4\u30c8 = 8\u30d3\u30c3\u30c8\u3092\u5360\u3081\u3001\\(2^8\\)\u500b\u306e\u6570\u5024\u3092\u8868\u73fe\u3067\u304d\u307e\u3059\u3002
        • \u6574\u6570\u578bint\u306f4\u30d0\u30a4\u30c8 = 32\u30d3\u30c3\u30c8\u3092\u5360\u3081\u3001\\(2^{32}\\)\u500b\u306e\u6570\u5024\u3092\u8868\u73fe\u3067\u304d\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u8868\u306f\u3001Java\u306b\u304a\u3051\u308b\u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u304c\u5360\u3081\u308b\u7a7a\u9593\u3001\u5024\u306e\u7bc4\u56f2\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u8868\u3092\u6697\u8a18\u3059\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\u304c\u3001\u4e00\u822c\u7684\u306a\u7406\u89e3\u3092\u6301\u3061\u3001\u5fc5\u8981\u6642\u306b\u53c2\u7167\u3059\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002

        \u8868 3-1 \u00a0 \u57fa\u672c\u30c7\u30fc\u30bf\u578b\u304c\u5360\u3081\u308b\u7a7a\u9593\u3068\u5024\u306e\u7bc4\u56f2

        \u578b \u30b7\u30f3\u30dc\u30eb \u5360\u6709\u7a7a\u9593 \u6700\u5c0f\u5024 \u6700\u5927\u5024 \u30c7\u30d5\u30a9\u30eb\u30c8\u5024 \u6574\u6570 byte 1\u30d0\u30a4\u30c8 \\(-2^7\\) (\\(-128\\)) \\(2^7 - 1\\) (\\(127\\)) 0 short 2\u30d0\u30a4\u30c8 \\(-2^{15}\\) \\(2^{15} - 1\\) 0 int 4\u30d0\u30a4\u30c8 \\(-2^{31}\\) \\(2^{31} - 1\\) 0 long 8\u30d0\u30a4\u30c8 \\(-2^{63}\\) \\(2^{63} - 1\\) 0 \u6d6e\u52d5\u5c0f\u6570\u70b9 float 4\u30d0\u30a4\u30c8 \\(1.175 \\times 10^{-38}\\) \\(3.403 \\times 10^{38}\\) \\(0.0\\text{f}\\) double 8\u30d0\u30a4\u30c8 \\(2.225 \\times 10^{-308}\\) \\(1.798 \\times 10^{308}\\) 0.0 \u6587\u5b57 char 2\u30d0\u30a4\u30c8 0 \\(2^{16} - 1\\) 0 \u30d6\u30fc\u30eb bool 1\u30d0\u30a4\u30c8 \\(\\text{false}\\) \\(\\text{true}\\) \\(\\text{false}\\)

        \u4e0a\u8a18\u306e\u8868\u306fJava\u306e\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u306b\u7279\u6709\u3067\u3042\u308b\u3053\u3068\u306b\u3054\u6ce8\u610f\u304f\u3060\u3055\u3044\u3002\u3059\u3079\u3066\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u306f\u72ec\u81ea\u306e\u30c7\u30fc\u30bf\u578b\u5b9a\u7fa9\u304c\u3042\u308a\u3001\u5360\u6709\u7a7a\u9593\u3001\u5024\u306e\u7bc4\u56f2\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u304c\u7570\u306a\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002

        • Python\u3067\u306f\u3001\u6574\u6570\u578bint\u306f\u4efb\u610f\u306e\u30b5\u30a4\u30ba\u306b\u306a\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u5229\u7528\u53ef\u80fd\u306a\u30e1\u30e2\u30ea\u306b\u3088\u3063\u3066\u306e\u307f\u5236\u9650\u3055\u308c\u307e\u3059\u3002\u6d6e\u52d5\u5c0f\u6570\u70b9float\u306f\u500d\u7cbe\u5ea664\u30d3\u30c3\u30c8\u3067\u3059\u3002char\u578b\u306f\u5b58\u5728\u305b\u305a\u3001\u5358\u4e00\u6587\u5b57\u306f\u5b9f\u969b\u306b\u306f\u9577\u30551\u306e\u6587\u5b57\u5217str\u3067\u3059\u3002
        • C\u304a\u3088\u3073C++\u3067\u306f\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u306e\u30b5\u30a4\u30ba\u304c\u6307\u5b9a\u3055\u308c\u3066\u304a\u3089\u305a\u3001\u5b9f\u88c5\u3068\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u306b\u3088\u3063\u3066\u7570\u306a\u308a\u307e\u3059\u3002\u4e0a\u8a18\u306e\u8868\u306fLP64\u30c7\u30fc\u30bf\u30e2\u30c7\u30eb\u306b\u5f93\u3063\u3066\u304a\u308a\u3001Linux\u3084macOS\u3092\u542b\u3080Unix 64\u30d3\u30c3\u30c8\u30aa\u30da\u30ec\u30fc\u30c6\u30a3\u30f3\u30b0\u30b7\u30b9\u30c6\u30e0\u3067\u4f7f\u7528\u3055\u308c\u3066\u3044\u307e\u3059\u3002
        • C\u304a\u3088\u3073C++\u306b\u304a\u3051\u308bchar\u306e\u30b5\u30a4\u30ba\u306f1\u30d0\u30a4\u30c8\u3067\u3059\u304c\u3001\u307b\u3068\u3093\u3069\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u306f\u3001\u7279\u5b9a\u306e\u6587\u5b57\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u65b9\u6cd5\u306b\u4f9d\u5b58\u3057\u3001\u8a73\u7d30\u306f\u300c\u6587\u5b57\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u300d\u306e\u7ae0\u3067\u8aac\u660e\u3055\u308c\u3066\u3044\u307e\u3059\u3002
        • \u30d6\u30fc\u30eb\u5024\u306e\u8868\u73fe\u306b\u306f1\u30d3\u30c3\u30c8\uff080\u307e\u305f\u306f1\uff09\u306e\u307f\u304c\u5fc5\u8981\u3067\u3059\u304c\u3001\u901a\u5e38\u306f\u30e1\u30e2\u30ea\u5185\u306b1\u30d0\u30a4\u30c8\u3068\u3057\u3066\u683c\u7d0d\u3055\u308c\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u73fe\u4ee3\u306e\u30b3\u30f3\u30d4\u30e5\u30fc\u30bfCPU\u304c\u901a\u5e381\u30d0\u30a4\u30c8\u3092\u6700\u5c0f\u306e\u30a2\u30c9\u30ec\u30b9\u53ef\u80fd\u306a\u30e1\u30e2\u30ea\u5358\u4f4d\u3068\u3057\u3066\u4f7f\u7528\u3059\u308b\u305f\u3081\u3067\u3059\u3002

        \u3067\u306f\u3001\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3068\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u95a2\u4fc2\u306f\u4f55\u3067\u3057\u3087\u3046\u304b\uff1f\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u306f\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u5185\u3067\u30c7\u30fc\u30bf\u3092\u7d44\u7e54\u5316\u3057\u683c\u7d0d\u3059\u308b\u65b9\u6cd5\u3067\u3042\u308b\u3053\u3068\u3092\u77e5\u3063\u3066\u3044\u307e\u3059\u3002\u3053\u3053\u3067\u306e\u7126\u70b9\u306f\u300c\u30c7\u30fc\u30bf\u300d\u3067\u306f\u306a\u304f\u300c\u69cb\u9020\u300d\u3067\u3059\u3002

        \u300c\u6570\u5024\u306e\u5217\u300d\u3092\u8868\u73fe\u3057\u305f\u3044\u5834\u5408\u3001\u81ea\u7136\u306b\u914d\u5217\u306e\u4f7f\u7528\u3092\u8003\u3048\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u914d\u5217\u306e\u7dda\u5f62\u69cb\u9020\u304c\u6570\u5024\u306e\u96a3\u63a5\u6027\u3068\u9806\u5e8f\u6027\u3092\u8868\u73fe\u3067\u304d\u308b\u305f\u3081\u3067\u3059\u304c\u3001\u683c\u7d0d\u3055\u308c\u308b\u5185\u5bb9\u304c\u6574\u6570int\u3001\u5c0f\u6570float\u3001\u6587\u5b57char\u306e\u3044\u305a\u308c\u3067\u3042\u3063\u3066\u3082\u3001\u300c\u30c7\u30fc\u30bf\u69cb\u9020\u300d\u3068\u306f\u7121\u95a2\u4fc2\u3067\u3059\u3002

        \u8a00\u3044\u63db\u3048\u308b\u3068\u3001\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u306f\u30c7\u30fc\u30bf\u306e\u300c\u5185\u5bb9\u578b\u300d\u3092\u63d0\u4f9b\u3057\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u30c7\u30fc\u30bf\u306e\u300c\u7d44\u7e54\u5316\u65b9\u6cd5\u300d\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3001\u540c\u3058\u30c7\u30fc\u30bf\u69cb\u9020\uff08\u914d\u5217\uff09\u3092\u4f7f\u7528\u3057\u3066\u3001int\u3001float\u3001char\u3001bool\u306a\u3069\u306e\u7570\u306a\u308b\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3092\u683c\u7d0d\u3057\u8868\u73fe\u3057\u3066\u3044\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin
        # \u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u3092\u521d\u671f\u5316\nnumbers: list[int] = [0] * 5\ndecimals: list[float] = [0.0] * 5\n# Python\u306e\u6587\u5b57\u306f\u5b9f\u969b\u306b\u306f\u9577\u30551\u306e\u6587\u5b57\u5217\ncharacters: list[str] = ['0'] * 5\nbools: list[bool] = [False] * 5\n# Python\u306e\u30ea\u30b9\u30c8\u306f\u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3068\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u53c2\u7167\u3092\u81ea\u7531\u306b\u683c\u7d0d\u53ef\u80fd\ndata = [0, 0.0, 'a', False, ListNode(0)]\n
        // \u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u3092\u521d\u671f\u5316\nint numbers[5];\nfloat decimals[5];\nchar characters[5];\nbool bools[5];\n
        // \u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u3092\u521d\u671f\u5316\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nboolean[] bools = new boolean[5];\n
        // \u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u3092\u521d\u671f\u5316\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nbool[] bools = new bool[5];\n
        // \u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u3092\u521d\u671f\u5316\nvar numbers = [5]int{}\nvar decimals = [5]float64{}\nvar characters = [5]byte{}\nvar bools = [5]bool{}\n
        // \u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u3092\u521d\u671f\u5316\nlet numbers = Array(repeating: 0, count: 5)\nlet decimals = Array(repeating: 0.0, count: 5)\nlet characters: [Character] = Array(repeating: \"a\", count: 5)\nlet bools = Array(repeating: false, count: 5)\n
        // JavaScript\u306e\u914d\u5217\u306f\u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3068\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u81ea\u7531\u306b\u683c\u7d0d\u53ef\u80fd\nconst array = [0, 0.0, 'a', false];\n
        // \u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u3092\u521d\u671f\u5316\nconst numbers: number[] = [];\nconst characters: string[] = [];\nconst bools: boolean[] = [];\n
        // \u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u3092\u521d\u671f\u5316\nList<int> numbers = List.filled(5, 0);\nList<double> decimals = List.filled(5, 0.0);\nList<String> characters = List.filled(5, 'a');\nList<bool> bools = List.filled(5, false);\n
        // \u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u3092\u521d\u671f\u5316\nlet numbers: Vec<i32> = vec![0; 5];\nlet decimals: Vec<f32> = vec![0.0, 5];\nlet characters: Vec<char> = vec!['0'; 5];\nlet bools: Vec<bool> = vec![false; 5];\n
        // \u69d8\u3005\u306a\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u3092\u521d\u671f\u5316\nint numbers[10];\nfloat decimals[10];\nchar characters[10];\nbool bools[10];\n
        \n
        "},{"location":"chapter_data_structure/character_encoding/","title":"3.4 \u00a0 \u6587\u5b57\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0 *","text":"

        \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30b7\u30b9\u30c6\u30e0\u3067\u306f\u3001\u3059\u3079\u3066\u306e\u30c7\u30fc\u30bf\u304c\u4e8c\u9032\u5f62\u5f0f\u3067\u683c\u7d0d\u3055\u308c\u3001char\u3082\u4f8b\u5916\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u6587\u5b57\u3092\u8868\u73fe\u3059\u308b\u305f\u3081\u306b\u3001\u5404\u6587\u5b57\u3068\u4e8c\u9032\u6570\u306e\u4e00\u5bfe\u4e00\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u3092\u5b9a\u7fa9\u3059\u308b\u300c\u6587\u5b57\u30bb\u30c3\u30c8\u300d\u3092\u958b\u767a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u6587\u5b57\u30bb\u30c3\u30c8\u304c\u3042\u308c\u3070\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306f\u8868\u3092\u53c2\u7167\u3057\u3066\u4e8c\u9032\u6570\u3092\u6587\u5b57\u306b\u5909\u63db\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_data_structure/character_encoding/#341-ascii","title":"3.4.1 \u00a0 ASCII\u6587\u5b57\u30bb\u30c3\u30c8","text":"

        ASCII\u30b3\u30fc\u30c9\u306f\u6700\u3082\u521d\u671f\u306e\u6587\u5b57\u30bb\u30c3\u30c8\u306e\u4e00\u3064\u3067\u3001\u6b63\u5f0f\u306b\u306fAmerican Standard Code for Information Interchange\u3068\u3057\u3066\u77e5\u3089\u308c\u3066\u3044\u307e\u3059\u30027\u3064\u306e\u4e8c\u9032\u6841\uff081\u30d0\u30a4\u30c8\u306e\u4e0b\u4f4d7\u30d3\u30c3\u30c8\uff09\u3092\u4f7f\u7528\u3057\u3066\u6587\u5b57\u3092\u8868\u73fe\u3057\u3001\u6700\u5927128\u7a2e\u985e\u306e\u7570\u306a\u308b\u6587\u5b57\u3092\u8868\u73fe\u3067\u304d\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001ASCII\u306b\u306f\u82f1\u8a9e\u306e\u5927\u6587\u5b57\u3068\u5c0f\u6587\u5b57\u30010\u301c9\u306e\u6570\u5b57\u3001\u69d8\u3005\u306a\u53e5\u8aad\u70b9\u3001\u7279\u5b9a\u306e\u5236\u5fa1\u6587\u5b57\uff08\u6539\u884c\u3084\u30bf\u30d6\u306a\u3069\uff09\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002

        \u56f3 3-6 \u00a0 ASCII\u30b3\u30fc\u30c9

        \u3057\u304b\u3057\u3001ASCII\u306f\u82f1\u8a9e\u306e\u6587\u5b57\u306e\u307f\u3092\u8868\u73fe\u3067\u304d\u307e\u3059\u3002\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u30b0\u30ed\u30fc\u30d0\u30eb\u5316\u306b\u4f34\u3044\u3001\u3088\u308a\u591a\u304f\u306e\u8a00\u8a9e\u3092\u8868\u73fe\u3059\u308b\u305f\u3081\u306bEASCII\u3068\u547c\u3070\u308c\u308b\u6587\u5b57\u30bb\u30c3\u30c8\u304c\u958b\u767a\u3055\u308c\u307e\u3057\u305f\u3002ASCII\u306e7\u30d3\u30c3\u30c8\u69cb\u9020\u304b\u30898\u30d3\u30c3\u30c8\u306b\u62e1\u5f35\u3057\u3001256\u6587\u5b57\u306e\u8868\u73fe\u3092\u53ef\u80fd\u306b\u3057\u307e\u3057\u305f\u3002

        \u4e16\u754c\u7684\u306b\u3001\u69d8\u3005\u306a\u5730\u57df\u56fa\u6709\u306eEASCII\u6587\u5b57\u30bb\u30c3\u30c8\u304c\u5c0e\u5165\u3055\u308c\u307e\u3057\u305f\u3002\u3053\u308c\u3089\u306e\u30bb\u30c3\u30c8\u306e\u6700\u521d\u306e128\u6587\u5b57\u306fASCII\u3068\u4e00\u81f4\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u6b8b\u308a\u306e128\u6587\u5b57\u306f\u7570\u306a\u308b\u8a00\u8a9e\u306e\u8981\u4ef6\u306b\u5bfe\u5fdc\u3059\u308b\u305f\u3081\u306b\u7570\u306a\u3063\u3066\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_data_structure/character_encoding/#342-gbk","title":"3.4.2 \u00a0 GBK\u6587\u5b57\u30bb\u30c3\u30c8","text":"

        \u5f8c\u306b\u3001**EASCII\u3067\u3082\u591a\u304f\u306e\u8a00\u8a9e\u306e\u6587\u5b57\u8981\u4ef6\u3092\u6e80\u305f\u3059\u3053\u3068\u304c\u3067\u304d\u306a\u3044**\u3053\u3068\u304c\u5224\u660e\u3057\u307e\u3057\u305f\u3002\u4f8b\u3048\u3070\u3001\u4e2d\u56fd\u8a9e\u306b\u306f\u7d0410\u4e07\u306e\u6f22\u5b57\u304c\u3042\u308a\u3001\u305d\u306e\u3046\u3061\u6570\u5343\u304c\u5b9a\u671f\u7684\u306b\u4f7f\u7528\u3055\u308c\u3066\u3044\u307e\u3059\u30021980\u5e74\u3001\u4e2d\u56fd\u6a19\u6e96\u5316\u59d4\u54e1\u4f1a\u306f6763\u306e\u4e2d\u56fd\u8a9e\u6587\u5b57\u3092\u542b\u3080GB2312\u6587\u5b57\u30bb\u30c3\u30c8\u3092\u767a\u8868\u3057\u3001\u4e2d\u56fd\u8a9e\u306e\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u51e6\u7406\u30cb\u30fc\u30ba\u3092\u672c\u8cea\u7684\u306b\u6e80\u305f\u3057\u307e\u3057\u305f\u3002

        \u3057\u304b\u3057\u3001GB2312\u306f\u4e00\u90e8\u306e\u7a00\u5c11\u6587\u5b57\u3084\u7e41\u4f53\u5b57\u3092\u51e6\u7406\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002GBK\u6587\u5b57\u30bb\u30c3\u30c8\u306fGB2312\u3092\u62e1\u5f35\u3057\u300121886\u306e\u4e2d\u56fd\u8a9e\u6587\u5b57\u3092\u542b\u3093\u3067\u3044\u307e\u3059\u3002GBK\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u30b9\u30ad\u30fc\u30e0\u3067\u306f\u3001ASCII\u6587\u5b57\u306f1\u30d0\u30a4\u30c8\u3067\u8868\u73fe\u3055\u308c\u3001\u4e2d\u56fd\u8a9e\u6587\u5b57\u306f2\u30d0\u30a4\u30c8\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        "},{"location":"chapter_data_structure/character_encoding/#343-unicode","title":"3.4.3 \u00a0 Unicode\u6587\u5b57\u30bb\u30c3\u30c8","text":"

        \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u6280\u8853\u306e\u6025\u901f\u306a\u767a\u5c55\u3068\u591a\u6570\u306e\u6587\u5b57\u30bb\u30c3\u30c8\u304a\u3088\u3073\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u6a19\u6e96\u306b\u3088\u308a\u3001\u6570\u591a\u304f\u306e\u554f\u984c\u304c\u767a\u751f\u3057\u307e\u3057\u305f\u3002\u4e00\u65b9\u3067\u306f\u3001\u3053\u308c\u3089\u306e\u6587\u5b57\u30bb\u30c3\u30c8\u306f\u4e00\u822c\u7684\u306b\u7279\u5b9a\u306e\u8a00\u8a9e\u306e\u6587\u5b57\u306e\u307f\u3092\u5b9a\u7fa9\u3057\u3001\u591a\u8a00\u8a9e\u74b0\u5883\u3067\u306f\u9069\u5207\u306b\u6a5f\u80fd\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002\u4ed6\u65b9\u3067\u306f\u3001\u540c\u3058\u8a00\u8a9e\u306b\u5bfe\u3059\u308b\u8907\u6570\u306e\u6587\u5b57\u30bb\u30c3\u30c8\u6a19\u6e96\u306e\u5b58\u5728\u306b\u3088\u308a\u3001\u7570\u306a\u308b\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u6a19\u6e96\u3092\u4f7f\u7528\u3059\u308b\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u9593\u3067\u60c5\u5831\u4ea4\u63db\u3092\u884c\u3046\u969b\u306b\u6587\u5b57\u5316\u3051\u304c\u767a\u751f\u3057\u307e\u3057\u305f\u3002

        \u5f53\u6642\u306e\u7814\u7a76\u8005\u305f\u3061\u306f\u8003\u3048\u307e\u3057\u305f\uff1a\u4e16\u754c\u306e\u3059\u3079\u3066\u306e\u8a00\u8a9e\u3068\u8a18\u53f7\u3092\u542b\u3080\u5305\u62ec\u7684\u306a\u6587\u5b57\u30bb\u30c3\u30c8\u304c\u958b\u767a\u3055\u308c\u308c\u3070\u3001\u8a00\u8a9e\u6a2a\u65ad\u74b0\u5883\u3068\u6587\u5b57\u5316\u3051\u306b\u95a2\u9023\u3059\u308b\u554f\u984c\u3092\u89e3\u6c7a\u3067\u304d\u308b\u306e\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046\u304b\uff1f \u3053\u306e\u30a2\u30a4\u30c7\u30a2\u306b\u30a4\u30f3\u30b9\u30d1\u30a4\u30a2\u3055\u308c\u3066\u3001\u5e83\u7bc4\u56f2\u306a\u6587\u5b57\u30bb\u30c3\u30c8\u3067\u3042\u308bUnicode\u304c\u8a95\u751f\u3057\u307e\u3057\u305f\u3002

        Unicode\u306f\u4e2d\u56fd\u8a9e\u3067\u300c\u7edf\u4e00\u7801\u300d\uff08\u7d71\u4e00\u30b3\u30fc\u30c9\uff09\u3068\u547c\u3070\u308c\u3001\u7406\u8ad6\u7684\u306b100\u4e07\u6587\u5b57\u4ee5\u4e0a\u3092\u53ce\u5bb9\u3067\u304d\u307e\u3059\u3002\u4e16\u754c\u4e2d\u306e\u3059\u3079\u3066\u306e\u6587\u5b57\u3092\u5358\u4e00\u306e\u30bb\u30c3\u30c8\u306b\u7d44\u307f\u8fbc\u307f\u3001\u69d8\u3005\u306a\u8a00\u8a9e\u306e\u51e6\u7406\u3068\u8868\u793a\u306e\u305f\u3081\u306e\u6c4e\u7528\u6587\u5b57\u30bb\u30c3\u30c8\u3092\u63d0\u4f9b\u3057\u3001\u7570\u306a\u308b\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u6a19\u6e96\u306b\u3088\u308b\u6587\u5b57\u5316\u3051\u306e\u554f\u984c\u3092\u6e1b\u3089\u3059\u3053\u3068\u3092\u76ee\u6307\u3057\u3066\u3044\u307e\u3059\u3002

        1991\u5e74\u306e\u30ea\u30ea\u30fc\u30b9\u4ee5\u6765\u3001Unicode\u306f\u65b0\u3057\u3044\u8a00\u8a9e\u3068\u6587\u5b57\u3092\u542b\u3080\u3088\u3046\u7d99\u7d9a\u7684\u306b\u62e1\u5f35\u3055\u308c\u3066\u3044\u307e\u3059\u30022022\u5e749\u6708\u73fe\u5728\u3001Unicode\u306b\u306f149,186\u6587\u5b57\u304c\u542b\u307e\u308c\u3066\u304a\u308a\u3001\u69d8\u3005\u306a\u8a00\u8a9e\u306e\u6587\u5b57\u3001\u8a18\u53f7\u3001\u3055\u3089\u306b\u306f\u7d75\u6587\u5b57\u3082\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002\u5e83\u5927\u306aUnicode\u6587\u5b57\u30bb\u30c3\u30c8\u3067\u306f\u3001\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u308b\u6587\u5b57\u306f2\u30d0\u30a4\u30c8\u3092\u5360\u6709\u3057\u3001\u4e00\u90e8\u306e\u7a00\u5c11\u306a\u6587\u5b57\u306f3\u30d0\u30a4\u30c8\u307e\u305f\u306f4\u30d0\u30a4\u30c8\u3092\u5360\u6709\u3059\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002

        Unicode\u306f\u5404\u6587\u5b57\u306b\u6570\u5024\uff08\u300c\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u300d\u3068\u547c\u3070\u308c\u308b\uff09\u3092\u5272\u308a\u5f53\u3066\u308b\u6c4e\u7528\u6587\u5b57\u30bb\u30c3\u30c8\u3067\u3059\u304c\u3001\u3053\u308c\u3089\u306e\u6587\u5b57\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u304c\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30b7\u30b9\u30c6\u30e0\u306b\u3069\u306e\u3088\u3046\u306b\u683c\u7d0d\u3055\u308c\u308b\u3079\u304d\u304b\u306f\u6307\u5b9a\u3057\u3066\u3044\u307e\u305b\u3093\u3002\u7591\u554f\u304c\u751f\u3058\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\uff1a\u30b7\u30b9\u30c6\u30e0\u306f\u30c6\u30ad\u30b9\u30c8\u5185\u306e\u7570\u306a\u308b\u9577\u3055\u306eUnicode\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u3092\u3069\u306e\u3088\u3046\u306b\u89e3\u91c8\u3059\u308b\u306e\u3067\u3057\u3087\u3046\u304b\uff1f\u4f8b\u3048\u3070\u30012\u30d0\u30a4\u30c8\u306e\u30b3\u30fc\u30c9\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u305d\u308c\u304c\u5358\u4e00\u306e2\u30d0\u30a4\u30c8\u6587\u5b57\u3092\u8868\u3059\u306e\u304b\u30012\u3064\u306e1\u30d0\u30a4\u30c8\u6587\u5b57\u3092\u8868\u3059\u306e\u304b\u3092\u3069\u306e\u3088\u3046\u306b\u5224\u65ad\u3059\u308b\u306e\u3067\u3057\u3087\u3046\u304b\uff1f

        \u3053\u306e\u554f\u984c\u306b\u5bfe\u3059\u308b\u7c21\u5358\u306a\u89e3\u6c7a\u7b56\u306f\u3001\u3059\u3079\u3066\u306e\u6587\u5b57\u3092\u7b49\u9577\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3068\u3057\u3066\u683c\u7d0d\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u300cHello\u300d\u306e\u5404\u6587\u5b57\u306f1\u30d0\u30a4\u30c8\u3092\u5360\u6709\u3057\u3001\u300c\u7b97\u6cd5\u300d\uff08\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff09\u306e\u5404\u6587\u5b57\u306f2\u30d0\u30a4\u30c8\u3092\u5360\u6709\u3057\u307e\u3059\u3002\u4e0a\u4f4d\u30d3\u30c3\u30c8\u3092\u30bc\u30ed\u3067\u57cb\u3081\u308b\u3053\u3068\u3067\u3001\u300cHello \u7b97\u6cd5\u300d\u306e\u3059\u3079\u3066\u306e\u6587\u5b57\u30922\u30d0\u30a4\u30c8\u3068\u3057\u3066\u30a8\u30f3\u30b3\u30fc\u30c9\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u65b9\u6cd5\u306b\u3088\u308a\u3001\u30b7\u30b9\u30c6\u30e0\u306f2\u30d0\u30a4\u30c8\u3054\u3068\u306b\u6587\u5b57\u3092\u89e3\u91c8\u3057\u3001\u30d5\u30ec\u30fc\u30ba\u306e\u5185\u5bb9\u3092\u5fa9\u5143\u3067\u304d\u307e\u3059\u3002

        \u56f3 3-7 \u00a0 Unicode\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u4f8b

        \u3057\u304b\u3057\u3001ASCII\u304c\u793a\u3057\u305f\u3088\u3046\u306b\u3001\u82f1\u8a9e\u306e\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306b\u306f1\u30d0\u30a4\u30c8\u306e\u307f\u304c\u5fc5\u8981\u3067\u3059\u3002\u4e0a\u8a18\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u82f1\u8a9e\u30c6\u30ad\u30b9\u30c8\u304c\u5360\u6709\u3059\u308b\u7a7a\u9593\u304cASCII\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3068\u6bd4\u8f03\u3057\u30662\u500d\u306b\u306a\u308a\u3001\u30e1\u30e2\u30ea\u7a7a\u9593\u306e\u7121\u99c4\u306b\u306a\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u3088\u308a\u52b9\u7387\u7684\u306aUnicode\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u65b9\u6cd5\u304c\u5fc5\u8981\u3067\u3059\u3002

        "},{"location":"chapter_data_structure/character_encoding/#344-utf-8","title":"3.4.4 \u00a0 UTF-8\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0","text":"

        \u73fe\u5728\u3001UTF-8\u306f\u56fd\u969b\u7684\u306b\u6700\u3082\u5e83\u304f\u4f7f\u7528\u3055\u308c\u3066\u3044\u308bUnicode\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u65b9\u6cd5\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002**\u3053\u308c\u306f\u53ef\u5909\u9577\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0**\u3067\u3001\u6587\u5b57\u306e\u8907\u96d1\u3055\u306b\u5fdc\u3058\u30661\u301c4\u30d0\u30a4\u30c8\u3092\u4f7f\u7528\u3057\u3066\u6587\u5b57\u3092\u8868\u73fe\u3057\u307e\u3059\u3002ASCII\u6587\u5b57\u306f1\u30d0\u30a4\u30c8\u306e\u307f\u304c\u5fc5\u8981\u3067\u3001\u30e9\u30c6\u30f3\u6587\u5b57\u3068\u30ae\u30ea\u30b7\u30e3\u6587\u5b57\u306f2\u30d0\u30a4\u30c8\u3001\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u308b\u4e2d\u56fd\u8a9e\u6587\u5b57\u306f3\u30d0\u30a4\u30c8\u3001\u305d\u306e\u4ed6\u306e\u7a00\u5c11\u306a\u6587\u5b57\u306f4\u30d0\u30a4\u30c8\u304c\u5fc5\u8981\u3067\u3059\u3002

        UTF-8\u306e\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u898f\u5247\u306f\u8907\u96d1\u3067\u306f\u306a\u304f\u30012\u3064\u306e\u30b1\u30fc\u30b9\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a

        • 1\u30d0\u30a4\u30c8\u6587\u5b57\u306e\u5834\u5408\u3001\u6700\u4e0a\u4f4d\u30d3\u30c3\u30c8\u3092\\(0\\)\u306b\u8a2d\u5b9a\u3057\u3001\u6b8b\u308a\u306e7\u30d3\u30c3\u30c8\u3092Unicode\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002\u6ce8\u76ee\u3059\u3079\u304d\u306f\u3001ASCII\u6587\u5b57\u304cUnicode\u30bb\u30c3\u30c8\u306e\u6700\u521d\u306e128\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u3092\u5360\u6709\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u3053\u308c\u306f**UTF-8\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u304cASCII\u3068\u5f8c\u65b9\u4e92\u63db\u6027\u304c\u3042\u308b**\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u3001UTF-8\u3092\u4f7f\u7528\u3057\u3066\u53e4\u3044ASCII\u30c6\u30ad\u30b9\u30c8\u3092\u89e3\u6790\u3067\u304d\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002
        • \u9577\u3055\\(n\\)\u30d0\u30a4\u30c8\u306e\u6587\u5b57\uff08\\(n > 1\\)\uff09\u306e\u5834\u5408\u3001\u6700\u521d\u306e\u30d0\u30a4\u30c8\u306e\u6700\u4e0a\u4f4d\\(n\\)\u30d3\u30c3\u30c8\u3092\\(1\\)\u306b\u8a2d\u5b9a\u3057\u3001\\((n + 1)^{\\text{th}}\\)\u30d3\u30c3\u30c8\u3092\\(0\\)\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u30022\u756a\u76ee\u306e\u30d0\u30a4\u30c8\u304b\u3089\u3001\u5404\u30d0\u30a4\u30c8\u306e\u6700\u4e0a\u4f4d2\u30d3\u30c3\u30c8\u3092\\(10\\)\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002\u6b8b\u308a\u306e\u30d3\u30c3\u30c8\u306fUnicode\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u3092\u57cb\u3081\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306f\u300cHello\u7b97\u6cd5\u300d\u306eUTF-8\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u6700\u4e0a\u4f4d\\(n\\)\u30d3\u30c3\u30c8\u304c\\(1\\)\u306b\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u6700\u4e0a\u4f4d\u30d3\u30c3\u30c8\u3067\\(1\\)\u306b\u8a2d\u5b9a\u3055\u308c\u305f\u30d3\u30c3\u30c8\u6570\u3092\u6570\u3048\u308b\u3053\u3068\u3067\u6587\u5b57\u306e\u9577\u3055\u3092\\(n\\)\u3068\u3057\u3066\u6c7a\u5b9a\u3067\u304d\u308b\u3053\u3068\u304c\u89b3\u5bdf\u3067\u304d\u307e\u3059\u3002

        \u3057\u304b\u3057\u3001\u306a\u305c\u6b8b\u308a\u306e\u30d0\u30a4\u30c8\u306e\u6700\u4e0a\u4f4d2\u30d3\u30c3\u30c8\u3092\\(10\\)\u306b\u8a2d\u5b9a\u3059\u308b\u306e\u3067\u3057\u3087\u3046\u304b\uff1f\u5b9f\u969b\u3001\u3053\u306e\\(10\\)\u306f\u4e00\u7a2e\u306e\u30c1\u30a7\u30c3\u30af\u30b5\u30e0\u3068\u3057\u3066\u6a5f\u80fd\u3057\u307e\u3059\u3002\u30b7\u30b9\u30c6\u30e0\u304c\u9593\u9055\u3063\u305f\u30d0\u30a4\u30c8\u304b\u3089\u30c6\u30ad\u30b9\u30c8\u306e\u89e3\u6790\u3092\u958b\u59cb\u3057\u305f\u5834\u5408\u3001\u30d0\u30a4\u30c8\u306e\u5148\u982d\u306e\\(10\\)\u306b\u3088\u308a\u30b7\u30b9\u30c6\u30e0\u306f\u7570\u5e38\u3092\u8fc5\u901f\u306b\u691c\u51fa\u3067\u304d\u307e\u3059\u3002

        \\(10\\)\u3092\u30c1\u30a7\u30c3\u30af\u30b5\u30e0\u3068\u3057\u3066\u4f7f\u7528\u3059\u308b\u7406\u7531\u306f\u3001UTF-8\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u898f\u5247\u306e\u4e0b\u3067\u306f\u3001\u6587\u5b57\u306e\u6700\u4e0a\u4f4d2\u30d3\u30c3\u30c8\u304c\\(10\\)\u306b\u306a\u308b\u3053\u3068\u306f\u4e0d\u53ef\u80fd\u3060\u304b\u3089\u3067\u3059\u3002\u3053\u308c\u306f\u77db\u76fe\u306b\u3088\u308a\u8a3c\u660e\u3067\u304d\u307e\u3059\uff1a\u6587\u5b57\u306e\u6700\u4e0a\u4f4d2\u30d3\u30c3\u30c8\u304c\\(10\\)\u306e\u5834\u5408\u3001\u6587\u5b57\u306e\u9577\u3055\u304c\\(1\\)\u3067\u3042\u308b\u3053\u3068\u3092\u793a\u3057\u3001\u3053\u308c\u306fASCII\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002\u3057\u304b\u3057\u3001ASCII\u6587\u5b57\u306e\u6700\u4e0a\u4f4d\u30d3\u30c3\u30c8\u306f\\(0\\)\u3067\u3042\u308b\u3079\u304d\u3067\u3001\u3053\u308c\u306f\u4eee\u5b9a\u3068\u77db\u76fe\u3057\u307e\u3059\u3002

        \u56f3 3-8 \u00a0 UTF-8\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u4f8b

        UTF-8\u4ee5\u5916\u306b\u3082\u3001\u4ed6\u306e\u4e00\u822c\u7684\u306a\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u65b9\u6cd5\u306b\u306f\u4ee5\u4e0b\u304c\u3042\u308a\u307e\u3059\uff1a

        • UTF-16\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\uff1a2\u307e\u305f\u306f4\u30d0\u30a4\u30c8\u3092\u4f7f\u7528\u3057\u3066\u6587\u5b57\u3092\u8868\u73fe\u3057\u307e\u3059\u3002\u3059\u3079\u3066\u306eASCII\u6587\u5b57\u3068\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u308b\u975e\u82f1\u8a9e\u6587\u5b57\u306f2\u30d0\u30a4\u30c8\u3067\u8868\u73fe\u3055\u308c\u3001\u5c11\u6570\u306e\u6587\u5b57\u306f4\u30d0\u30a4\u30c8\u304c\u5fc5\u8981\u3067\u3059\u30022\u30d0\u30a4\u30c8\u6587\u5b57\u306e\u5834\u5408\u3001UTF-16\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306fUnicode\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u3068\u7b49\u3057\u304f\u306a\u308a\u307e\u3059\u3002
        • UTF-32\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\uff1a\u3059\u3079\u3066\u306e\u6587\u5b57\u304c4\u30d0\u30a4\u30c8\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u3001UTF-32\u304cUTF-8\u3084UTF-16\u3088\u308a\u3082\u591a\u304f\u306e\u7a7a\u9593\u3092\u5360\u6709\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u3001\u7279\u306bASCII\u6587\u5b57\u306e\u5272\u5408\u304c\u9ad8\u3044\u30c6\u30ad\u30b9\u30c8\u3067\u306f\u9855\u8457\u3067\u3059\u3002

        \u30b9\u30c8\u30ec\u30fc\u30b8\u7a7a\u9593\u306e\u89b3\u70b9\u304b\u3089\u3001UTF-8\u3092\u4f7f\u7528\u3057\u3066\u82f1\u8a9e\u6587\u5b57\u3092\u8868\u73fe\u3059\u308b\u3053\u3068\u306f1\u30d0\u30a4\u30c8\u306e\u307f\u304c\u5fc5\u8981\u306a\u305f\u3081\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3059\u3002UTF-16\u3092\u4f7f\u7528\u3057\u3066\u4e00\u90e8\u306e\u975e\u82f1\u8a9e\u6587\u5b57\uff08\u4e2d\u56fd\u8a9e\u306a\u3069\uff09\u3092\u30a8\u30f3\u30b3\u30fc\u30c9\u3059\u308b\u3053\u3068\u306f\u30012\u30d0\u30a4\u30c8\u306e\u307f\u304c\u5fc5\u8981\u306a\u305f\u3081\u3088\u308a\u52b9\u7387\u7684\u306b\u306a\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u4e00\u65b9\u3001UTF-8\u3067\u306f3\u30d0\u30a4\u30c8\u304c\u5fc5\u8981\u306b\u306a\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002

        \u4e92\u63db\u6027\u306e\u89b3\u70b9\u304b\u3089\u3001UTF-8\u306f\u6700\u3082\u6c4e\u7528\u6027\u304c\u3042\u308a\u3001\u591a\u304f\u306e\u30c4\u30fc\u30eb\u3068\u30e9\u30a4\u30d6\u30e9\u30ea\u304cUTF-8\u3092\u512a\u5148\u7684\u306b\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_data_structure/character_encoding/#345","title":"3.4.5 \u00a0 \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u304a\u3051\u308b\u6587\u5b57\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0","text":"

        \u6b74\u53f2\u7684\u306b\u3001\u591a\u304f\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306f\u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c\u4e2d\u306e\u6587\u5b57\u5217\u51e6\u7406\u306bUTF-16\u3084UTF-32\u306a\u3069\u306e\u56fa\u5b9a\u9577\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u5229\u7528\u3057\u3066\u3044\u307e\u3057\u305f\u3002\u3053\u308c\u306b\u3088\u308a\u6587\u5b57\u5217\u3092\u914d\u5217\u3068\u3057\u3066\u51e6\u7406\u3067\u304d\u3001\u3044\u304f\u3064\u304b\u306e\u5229\u70b9\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\uff1aUTF-16\u3067\u30a8\u30f3\u30b3\u30fc\u30c9\u3055\u308c\u305f\u6587\u5b57\u5217\u306f\u7c21\u5358\u306b\u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\u3067\u304d\u307e\u3059\u3002\u53ef\u5909\u9577\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3067\u3042\u308bUTF-8\u306e\u5834\u5408\u3001\\(i^{th}\\)\u6587\u5b57\u306e\u4f4d\u7f6e\u3092\u7279\u5b9a\u3059\u308b\u306b\u306f\u6587\u5b57\u5217\u306e\u958b\u59cb\u304b\u3089\\(i^{th}\\)\u4f4d\u7f6e\u307e\u3067\u8d70\u67fb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\\(O(n)\\)\u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002
        • \u6587\u5b57\u6570\u30ab\u30a6\u30f3\u30c8\uff1a\u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\u3068\u540c\u69d8\u306b\u3001UTF-16\u3067\u30a8\u30f3\u30b3\u30fc\u30c9\u3055\u308c\u305f\u6587\u5b57\u5217\u306e\u6587\u5b57\u6570\u3092\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u3053\u3068\u306f\\(O(1)\\)\u64cd\u4f5c\u3067\u3059\u3002\u3057\u304b\u3057\u3001UTF-8\u3067\u30a8\u30f3\u30b3\u30fc\u30c9\u3055\u308c\u305f\u6587\u5b57\u5217\u306e\u6587\u5b57\u6570\u3092\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u306b\u306f\u6587\u5b57\u5217\u5168\u4f53\u3092\u8d70\u67fb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u6587\u5b57\u5217\u64cd\u4f5c\uff1a\u5206\u5272\u3001\u9023\u7d50\u3001\u633f\u5165\u3001\u524a\u9664\u306a\u3069\u306e\u591a\u304f\u306e\u6587\u5b57\u5217\u64cd\u4f5c\u306f\u3001UTF-16\u3067\u30a8\u30f3\u30b3\u30fc\u30c9\u3055\u308c\u305f\u6587\u5b57\u5217\u3067\u7c21\u5358\u3067\u3059\u3002\u3053\u308c\u3089\u306e\u64cd\u4f5c\u306f\u4e00\u822c\u7684\u306b\u3001UTF-8\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306e\u6709\u52b9\u6027\u3092\u78ba\u4fdd\u3059\u308b\u305f\u3081\u306bUTF-8\u3067\u30a8\u30f3\u30b3\u30fc\u30c9\u3055\u308c\u305f\u6587\u5b57\u5217\u3067\u8ffd\u52a0\u306e\u8a08\u7b97\u304c\u5fc5\u8981\u3067\u3059\u3002

        \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u304a\u3051\u308b\u6587\u5b57\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u30b9\u30ad\u30fc\u30e0\u306e\u8a2d\u8a08\u306f\u3001\u69d8\u3005\u306a\u8981\u56e0\u3092\u542b\u3080\u8208\u5473\u6df1\u3044\u30c8\u30d4\u30c3\u30af\u3067\u3059\uff1a

        • Java\u306eString\u578b\u306fUTF-16\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u3001\u5404\u6587\u5b57\u304c2\u30d0\u30a4\u30c8\u3092\u5360\u6709\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u300116\u30d3\u30c3\u30c8\u304c\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u6587\u5b57\u3092\u8868\u73fe\u3059\u308b\u306e\u306b\u5341\u5206\u3067\u3042\u308b\u3068\u3044\u3046\u521d\u671f\u306e\u4fe1\u5ff5\u306b\u57fa\u3065\u3044\u3066\u304a\u308a\u3001\u5f8c\u306b\u9593\u9055\u3044\u3067\u3042\u308b\u3053\u3068\u304c\u8a3c\u660e\u3055\u308c\u307e\u3057\u305f\u3002Unicode\u6a19\u6e96\u304c16\u30d3\u30c3\u30c8\u3092\u8d85\u3048\u3066\u62e1\u5f35\u3055\u308c\u308b\u3068\u3001Java\u306e\u6587\u5b57\u306f\u300c\u30b5\u30ed\u30b2\u30fc\u30c8\u30da\u30a2\u300d\u3068\u3057\u3066\u77e5\u3089\u308c\u308b16\u30d3\u30c3\u30c8\u5024\u306e\u30da\u30a2\u3067\u8868\u73fe\u3055\u308c\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002
        • JavaScript\u3068TypeScript\u306f\u3001Java\u3068\u540c\u69d8\u306e\u7406\u7531\u3067UTF-16\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002JavaScript\u304c1995\u5e74\u306bNetscape\u306b\u3088\u3063\u3066\u6700\u521d\u306b\u5c0e\u5165\u3055\u308c\u305f\u3068\u304d\u3001Unicode\u306f\u307e\u3060\u521d\u671f\u6bb5\u968e\u306b\u3042\u308a\u300116\u30d3\u30c3\u30c8\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306f\u3059\u3079\u3066\u306eUnicode\u6587\u5b57\u3092\u8868\u73fe\u3059\u308b\u306e\u306b\u5341\u5206\u3067\u3057\u305f\u3002
        • C#\u306fUTF-16\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u3001\u3053\u308c\u306f\u4e3b\u306bMicrosoft\u306b\u3088\u3063\u3066\u8a2d\u8a08\u3055\u308c\u305f.NET\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u3001\u304a\u3088\u3073\u591a\u304f\u306eMicrosoft\u6280\u8853\uff08Windows\u30aa\u30da\u30ec\u30fc\u30c6\u30a3\u30f3\u30b0\u30b7\u30b9\u30c6\u30e0\u3092\u542b\u3080\uff09\u304cUTF-16\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u5e83\u7bc4\u56f2\u306b\u4f7f\u7528\u3057\u3066\u3044\u308b\u305f\u3081\u3067\u3059\u3002

        \u6587\u5b57\u6570\u306e\u904e\u5c0f\u8a55\u4fa1\u306b\u3088\u308a\u3001\u3053\u308c\u3089\u306e\u8a00\u8a9e\u306f16\u30d3\u30c3\u30c8\u3092\u8d85\u3048\u308bUnicode\u6587\u5b57\u3092\u8868\u73fe\u3059\u308b\u305f\u3081\u306b\u300c\u30b5\u30ed\u30b2\u30fc\u30c8\u30da\u30a2\u300d\u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3057\u305f\u3002\u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u306b\u306f\u6b20\u70b9\u304c\u3042\u308a\u307e\u3059\uff1a\u30b5\u30ed\u30b2\u30fc\u30c8\u30da\u30a2\u3092\u542b\u3080\u6587\u5b57\u5217\u306f2\u30d0\u30a4\u30c8\u307e\u305f\u306f4\u30d0\u30a4\u30c8\u3092\u5360\u6709\u3059\u308b\u6587\u5b57\u3092\u6301\u3064\u5834\u5408\u304c\u3042\u308a\u3001\u56fa\u5b9a\u9577\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306e\u5229\u70b9\u3092\u5931\u3044\u307e\u3059\u3002\u3055\u3089\u306b\u3001\u30b5\u30ed\u30b2\u30fc\u30c8\u30da\u30a2\u306e\u51e6\u7406\u306f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306b\u8907\u96d1\u3055\u3068\u30c7\u30d0\u30c3\u30b0\u306e\u56f0\u96e3\u3055\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002

        \u3053\u308c\u3089\u306e\u8ab2\u984c\u306b\u5bfe\u51e6\u3059\u308b\u305f\u3081\u3001\u4e00\u90e8\u306e\u8a00\u8a9e\u306f\u4ee3\u66ff\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u6226\u7565\u3092\u63a1\u7528\u3057\u3066\u3044\u307e\u3059\uff1a

        • Python\u306estr\u578b\u306f\u3001\u6587\u5b57\u306e\u30b9\u30c8\u30ec\u30fc\u30b8\u9577\u304c\u6587\u5b57\u5217\u5185\u306e\u6700\u5927\u306eUnicode\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u306b\u4f9d\u5b58\u3059\u308b\u67d4\u8edf\u306a\u8868\u73fe\u3067Unicode\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u3059\u3079\u3066\u306e\u6587\u5b57\u304cASCII\u306e\u5834\u5408\u3001\u5404\u6587\u5b57\u306f1\u30d0\u30a4\u30c8\u3092\u5360\u6709\u3057\u3001\u57fa\u672c\u591a\u8a00\u8a9e\u9762\uff08BMP\uff09\u5185\u306e\u6587\u5b57\u306f2\u30d0\u30a4\u30c8\u3001BMP\u3092\u8d85\u3048\u308b\u6587\u5b57\u306f4\u30d0\u30a4\u30c8\u3092\u5360\u6709\u3057\u307e\u3059\u3002
        • Go\u306estring\u578b\u306f\u5185\u90e8\u7684\u306bUTF-8\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002Go\u306f\u500b\u5225\u306eUnicode\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u3092\u8868\u73fe\u3059\u308b\u305f\u3081\u306erune\u578b\u3082\u63d0\u4f9b\u3057\u307e\u3059\u3002
        • Rust\u306estr\u3068String\u578b\u306f\u5185\u90e8\u7684\u306bUTF-8\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002Rust\u306f\u500b\u5225\u306eUnicode\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u7528\u306echar\u578b\u3082\u63d0\u4f9b\u3057\u307e\u3059\u3002

        \u4e0a\u8a18\u306e\u8b70\u8ad6\u306f\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u306e\u6587\u5b57\u5217\u306e\u683c\u7d0d\u65b9\u6cd5\u306b\u95a2\u3059\u308b\u3082\u306e\u3067\u3042\u308a\u3001**\u30d5\u30a1\u30a4\u30eb\u3067\u306e\u6587\u5b57\u5217\u306e\u683c\u7d0d\u65b9\u6cd5\u3084\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u4e0a\u3067\u306e\u9001\u4fe1\u65b9\u6cd5\u3068\u306f\u7570\u306a\u308b**\u3053\u3068\u306b\u6ce8\u610f\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002\u30d5\u30a1\u30a4\u30eb\u30b9\u30c8\u30ec\u30fc\u30b8\u3084\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u9001\u4fe1\u3067\u306f\u3001\u6587\u5b57\u5217\u306f\u901a\u5e38\u3001\u6700\u9069\u306a\u4e92\u63db\u6027\u3068\u7a7a\u9593\u52b9\u7387\u306e\u305f\u3081\u306bUTF-8\u5f62\u5f0f\u3067\u30a8\u30f3\u30b3\u30fc\u30c9\u3055\u308c\u307e\u3059\u3002

        "},{"location":"chapter_data_structure/classification_of_data_structure/","title":"3.1 \u00a0 \u30c7\u30fc\u30bf\u69cb\u9020\u306e\u5206\u985e","text":"

        \u4e00\u822c\u7684\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u306f\u3001\u914d\u5217\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u30b9\u30bf\u30c3\u30af\u3001\u30ad\u30e5\u30fc\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3001\u6728\u3001\u30d2\u30fc\u30d7\u3001\u30b0\u30e9\u30d5\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u3089\u306f\u300c\u8ad6\u7406\u69cb\u9020\u300d\u3068\u300c\u7269\u7406\u69cb\u9020\u300d\u306b\u5206\u985e\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_data_structure/classification_of_data_structure/#311","title":"3.1.1 \u00a0 \u8ad6\u7406\u69cb\u9020\uff1a\u7dda\u5f62\u3068\u975e\u7dda\u5f62","text":"

        \u8ad6\u7406\u69cb\u9020\u306f\u30c7\u30fc\u30bf\u8981\u7d20\u9593\u306e\u8ad6\u7406\u7684\u95a2\u4fc2\u3092\u660e\u3089\u304b\u306b\u3057\u307e\u3059\u3002\u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u306f\u3001\u30c7\u30fc\u30bf\u306f\u7279\u5b9a\u306e\u9806\u5e8f\u3067\u914d\u7f6e\u3055\u308c\u3001\u30c7\u30fc\u30bf\u9593\u306e\u7dda\u5f62\u95a2\u4fc2\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u4e00\u65b9\u3001\u6728\u3067\u306f\u3001\u30c7\u30fc\u30bf\u306f\u4e0a\u304b\u3089\u4e0b\u3078\u968e\u5c64\u7684\u306b\u914d\u7f6e\u3055\u308c\u3001\u300c\u7956\u5148\u300d\u3068\u300c\u5b50\u5b6b\u300d\u9593\u306e\u6d3e\u751f\u95a2\u4fc2\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u305d\u3057\u3066\u3001\u30b0\u30e9\u30d5\u306f\u30ce\u30fc\u30c9\u3068\u30a8\u30c3\u30b8\u304b\u3089\u69cb\u6210\u3055\u308c\u3001\u8907\u96d1\u306a\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u95a2\u4fc2\u3092\u53cd\u6620\u3057\u3066\u3044\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u3001\u8ad6\u7406\u69cb\u9020\u306f\u300c\u7dda\u5f62\u300d\u3068\u300c\u975e\u7dda\u5f62\u300d\u306e2\u3064\u306e\u4e3b\u8981\u30ab\u30c6\u30b4\u30ea\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u7dda\u5f62\u69cb\u9020\u306f\u3088\u308a\u76f4\u611f\u7684\u3067\u3001\u30c7\u30fc\u30bf\u304c\u8ad6\u7406\u95a2\u4fc2\u306b\u304a\u3044\u3066\u7dda\u5f62\u306b\u914d\u7f6e\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u975e\u7dda\u5f62\u69cb\u9020\u306f\u3001\u9006\u306b\u975e\u7dda\u5f62\u306b\u914d\u7f6e\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        • \u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\uff1a\u914d\u5217\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u30b9\u30bf\u30c3\u30af\u3001\u30ad\u30e5\u30fc\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3002\u8981\u7d20\u304c\u4e00\u5bfe\u4e00\u306e\u9806\u6b21\u95a2\u4fc2\u3092\u6301\u3061\u307e\u3059\u3002
        • \u975e\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\uff1a\u6728\u3001\u30d2\u30fc\u30d7\u3001\u30b0\u30e9\u30d5\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3002

        \u975e\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u3001\u3055\u3089\u306b\u6728\u69cb\u9020\u3068\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u69cb\u9020\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        • \u6728\u69cb\u9020\uff1a\u6728\u3001\u30d2\u30fc\u30d7\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3002\u8981\u7d20\u304c\u4e00\u5bfe\u591a\u306e\u95a2\u4fc2\u3092\u6301\u3061\u307e\u3059\u3002
        • \u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u69cb\u9020\uff1a\u30b0\u30e9\u30d5\u3002\u8981\u7d20\u304c\u591a\u5bfe\u591a\u306e\u95a2\u4fc2\u3092\u6301\u3061\u307e\u3059\u3002

        \u56f3 3-1 \u00a0 Linear and non-linear data structures

        "},{"location":"chapter_data_structure/classification_of_data_structure/#312","title":"3.1.2 \u00a0 \u7269\u7406\u69cb\u9020\uff1a\u9023\u7d9a\u3068\u5206\u6563","text":"

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u4e2d\u3001\u51e6\u7406\u3055\u308c\u308b\u30c7\u30fc\u30bf\u306f\u30e1\u30e2\u30ea\u306b\u683c\u7d0d\u3055\u308c\u307e\u3059\u3002\u4e0b\u56f3\u306f\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u30e1\u30e2\u30ea\u30b9\u30c6\u30a3\u30c3\u30af\u3092\u793a\u3057\u3066\u304a\u308a\u3001\u5404\u9ed2\u3044\u6b63\u65b9\u5f62\u306f\u7269\u7406\u30e1\u30e2\u30ea\u7a7a\u9593\u3067\u3059\u3002\u30e1\u30e2\u30ea\u3092\u5de8\u5927\u306aExcel\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u3068\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u5404\u30bb\u30eb\u306f\u4e00\u5b9a\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3067\u304d\u307e\u3059\u3002

        \u30b7\u30b9\u30c6\u30e0\u306f\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u306b\u3088\u3063\u3066\u76ee\u6a19\u4f4d\u7f6e\u306e\u30c7\u30fc\u30bf\u306b\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306f\u7279\u5b9a\u306e\u30eb\u30fc\u30eb\u306b\u5f93\u3063\u3066\u8868\u306e\u5404\u30bb\u30eb\u306b\u4e00\u610f\u306e\u8b58\u5225\u5b50\u3092\u5272\u308a\u5f53\u3066\u3001\u5404\u30e1\u30e2\u30ea\u7a7a\u9593\u304c\u4e00\u610f\u306e\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u3092\u6301\u3064\u3053\u3068\u3092\u4fdd\u8a3c\u3057\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u30a2\u30c9\u30ec\u30b9\u306b\u3088\u308a\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u30e1\u30e2\u30ea\u306b\u683c\u7d0d\u3055\u308c\u305f\u30c7\u30fc\u30bf\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u307e\u3059\u3002

        \u56f3 3-2 \u00a0 Memory stick, memory spaces, memory addresses

        Tip

        \u30e1\u30e2\u30ea\u3092Excel\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u306b\u6bd4\u8f03\u3059\u308b\u3053\u3068\u306f\u7c21\u7565\u5316\u3055\u308c\u305f\u985e\u63a8\u3067\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u30e1\u30e2\u30ea\u306e\u5b9f\u969b\u306e\u52d5\u4f5c\u30e1\u30ab\u30cb\u30ba\u30e0\u306f\u3088\u308a\u8907\u96d1\u3067\u3001\u30a2\u30c9\u30ec\u30b9\u7a7a\u9593\u3001\u30e1\u30e2\u30ea\u7ba1\u7406\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u30e1\u30ab\u30cb\u30ba\u30e0\u3001\u4eee\u60f3\u30e1\u30e2\u30ea\u3001\u7269\u7406\u30e1\u30e2\u30ea\u306a\u3069\u306e\u6982\u5ff5\u304c\u95a2\u4fc2\u3057\u3066\u3044\u307e\u3059\u3002

        \u30e1\u30e2\u30ea\u306f\u3059\u3079\u3066\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u5171\u6709\u30ea\u30bd\u30fc\u30b9\u3067\u3059\u3002\u3042\u308b\u30e1\u30e2\u30ea\u30d6\u30ed\u30c3\u30af\u304c1\u3064\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u3088\u3063\u3066\u5360\u6709\u3055\u308c\u308b\u3068\u3001\u4ed6\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u540c\u6642\u306b\u4f7f\u7528\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30e1\u30e2\u30ea\u30ea\u30bd\u30fc\u30b9\u306f\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u8a2d\u8a08\u306b\u304a\u3051\u308b\u91cd\u8981\u306a\u8003\u616e\u4e8b\u9805\u3067\u3059\u3002\u4f8b\u3048\u3070\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u30d4\u30fc\u30af\u30e1\u30e2\u30ea\u4f7f\u7528\u91cf\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u306e\u6b8b\u308a\u7a7a\u304d\u30e1\u30e2\u30ea\u3092\u8d85\u3048\u3066\u306f\u3044\u3051\u307e\u305b\u3093\u3002\u9023\u7d9a\u3057\u305f\u30e1\u30e2\u30ea\u30d6\u30ed\u30c3\u30af\u304c\u4e0d\u8db3\u3057\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u975e\u9023\u7d9a\u30e1\u30e2\u30ea\u30d6\u30ed\u30c3\u30af\u306b\u683c\u7d0d\u3067\u304d\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u9078\u629e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u3001\u7269\u7406\u69cb\u9020\u306f\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30e1\u30e2\u30ea\u306b\u304a\u3051\u308b\u30c7\u30fc\u30bf\u306e\u683c\u7d0d\u65b9\u6cd5\u3092\u53cd\u6620\u3057\u3001\u9023\u7d9a\u7a7a\u9593\u683c\u7d0d\uff08\u914d\u5217\uff09\u3068\u975e\u9023\u7d9a\u7a7a\u9593\u683c\u7d0d\uff08\u9023\u7d50\u30ea\u30b9\u30c8\uff09\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u30022\u3064\u306e\u30bf\u30a4\u30d7\u306e\u7269\u7406\u69cb\u9020\u306f\u3001\u6642\u9593\u52b9\u7387\u3068\u7a7a\u9593\u52b9\u7387\u306e\u89b3\u70b9\u3067\u88dc\u5b8c\u7684\u306a\u7279\u6027\u3092\u793a\u3057\u307e\u3059\u3002

        \u56f3 3-3 \u00a0 Contiguous space storage and dispersed space storage

        \u3059\u3079\u3066\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u914d\u5217\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u307e\u305f\u306f\u305d\u306e\u7d44\u307f\u5408\u308f\u305b\u306b\u57fa\u3065\u3044\u3066\u5b9f\u88c5\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4f8b\u3048\u3070\u3001\u30b9\u30bf\u30c3\u30af\u3068\u30ad\u30e5\u30fc\u306f\u914d\u5217\u307e\u305f\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u3069\u3061\u3089\u3067\u3082\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u5b9f\u88c5\u306b\u306f\u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u4e21\u65b9\u304c\u95a2\u4fc2\u3059\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002

        • \u914d\u5217\u30d9\u30fc\u30b9\u306e\u5b9f\u88c5\uff1a\u30b9\u30bf\u30c3\u30af\u3001\u30ad\u30e5\u30fc\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3001\u6728\u3001\u30d2\u30fc\u30d7\u3001\u30b0\u30e9\u30d5\u3001\u884c\u5217\u3001\u30c6\u30f3\u30bd\u30eb\uff08\u6b21\u5143\\(\\geq 3\\)\u306e\u914d\u5217\uff09\u3002
        • \u9023\u7d50\u30ea\u30b9\u30c8\u30d9\u30fc\u30b9\u306e\u5b9f\u88c5\uff1a\u30b9\u30bf\u30c3\u30af\u3001\u30ad\u30e5\u30fc\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3001\u6728\u3001\u30d2\u30fc\u30d7\u3001\u30b0\u30e9\u30d5\u306a\u3069\u3002

        \u914d\u5217\u306b\u57fa\u3065\u3044\u3066\u5b9f\u88c5\u3055\u308c\u305f\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u300c\u9759\u7684\u30c7\u30fc\u30bf\u69cb\u9020\u300d\u3068\u3082\u547c\u3070\u308c\u3001\u521d\u671f\u5316\u5f8c\u306b\u9577\u3055\u3092\u5909\u66f4\u3067\u304d\u306a\u3044\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u9006\u306b\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u3044\u305f\u3082\u306e\u306f\u300c\u52d5\u7684\u30c7\u30fc\u30bf\u69cb\u9020\u300d\u3068\u547c\u3070\u308c\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c\u4e2d\u306b\u30b5\u30a4\u30ba\u3092\u8abf\u6574\u3067\u304d\u307e\u3059\u3002

        Tip

        \u7269\u7406\u69cb\u9020\u3092\u7406\u89e3\u3059\u308b\u306e\u304c\u56f0\u96e3\u306a\u5834\u5408\u306f\u3001\u6b21\u306e\u7ae0\u300c\u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u300d\u3092\u8aad\u3093\u3067\u304b\u3089\u3001\u3053\u306e\u7bc0\u306b\u623b\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002

        "},{"location":"chapter_data_structure/number_encoding/","title":"3.3 \u00a0 \u6570\u5024\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0 *","text":"

        Tip

        \u672c\u66f8\u3067\u306f\u3001\u30a2\u30b9\u30bf\u30ea\u30b9\u30af\u300c*\u300d\u304c\u4ed8\u3044\u305f\u7ae0\u306f\u4efb\u610f\u8aad\u66f8\u3067\u3059\u3002\u6642\u9593\u304c\u4e0d\u8db3\u3057\u3066\u3044\u308b\u5834\u5408\u3084\u96e3\u3057\u3044\u3068\u611f\u3058\u308b\u5834\u5408\u306f\u3001\u6700\u521d\u306f\u3053\u308c\u3089\u3092\u30b9\u30ad\u30c3\u30d7\u3057\u3066\u3001\u5fc5\u9808\u306e\u7ae0\u3092\u5b8c\u4e86\u3057\u305f\u5f8c\u306b\u623b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_data_structure/number_encoding/#331","title":"3.3.1 \u00a0 \u6574\u6570\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0","text":"

        \u524d\u306e\u7bc0\u306e\u8868\u3067\u3001\u3059\u3079\u3066\u306e\u6574\u6570\u578b\u306f\u6b63\u306e\u6570\u3088\u308a\u30821\u3064\u591a\u3044\u8ca0\u306e\u6570\u3092\u8868\u73fe\u3067\u304d\u308b\u3053\u3068\u3092\u89b3\u5bdf\u3057\u307e\u3057\u305f\u3002\u4f8b\u3048\u3070\u3001byte\u306e\u7bc4\u56f2\u306f\\([-128, 127]\\)\u3067\u3059\u3002\u3053\u306e\u73fe\u8c61\u306f\u76f4\u611f\u306b\u53cd\u3059\u308b\u3088\u3046\u306b\u898b\u3048\u3001\u305d\u306e\u6839\u672c\u7684\u306a\u7406\u7531\u306b\u306f\u7b26\u53f7\u7d76\u5bfe\u5024\u30011\u306e\u88dc\u6570\u30012\u306e\u88dc\u6570\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306e\u77e5\u8b58\u304c\u95a2\u4e0e\u3057\u3066\u3044\u307e\u3059\u3002

        \u307e\u305a\u91cd\u8981\u306a\u3053\u3068\u306f\u3001**\u6570\u5024\u306f\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u5185\u30672\u306e\u88dc\u6570\u5f62\u5f0f\u3067\u683c\u7d0d\u3055\u308c\u308b**\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002\u306a\u305c\u305d\u3046\u306a\u306e\u304b\u3092\u5206\u6790\u3059\u308b\u524d\u306b\u3001\u3053\u308c\u30893\u3064\u306e\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u65b9\u6cd5\u3092\u5b9a\u7fa9\u3057\u307e\u3057\u3087\u3046\uff1a

        • \u7b26\u53f7\u7d76\u5bfe\u5024\uff1a\u6570\u5024\u306e\u4e8c\u9032\u8868\u73fe\u306e\u6700\u4e0a\u4f4d\u30d3\u30c3\u30c8\u3092\u7b26\u53f7\u30d3\u30c3\u30c8\u3068\u3057\u3001\\(0\\)\u306f\u6b63\u306e\u6570\u3001\\(1\\)\u306f\u8ca0\u306e\u6570\u3092\u8868\u3057\u307e\u3059\u3002\u6b8b\u308a\u306e\u30d3\u30c3\u30c8\u306f\u6570\u5024\u306e\u5024\u3092\u8868\u3057\u307e\u3059\u3002
        • 1\u306e\u88dc\u6570\uff1a\u6b63\u306e\u6570\u306e1\u306e\u88dc\u6570\u306f\u7b26\u53f7\u7d76\u5bfe\u5024\u3068\u540c\u3058\u3067\u3059\u3002\u8ca0\u306e\u6570\u306e\u5834\u5408\u3001\u7b26\u53f7\u30d3\u30c3\u30c8\u4ee5\u5916\u306e\u3059\u3079\u3066\u306e\u30d3\u30c3\u30c8\u3092\u53cd\u8ee2\u3057\u3066\u5f97\u3089\u308c\u307e\u3059\u3002
        • 2\u306e\u88dc\u6570\uff1a\u6b63\u306e\u6570\u306e2\u306e\u88dc\u6570\u306f\u7b26\u53f7\u7d76\u5bfe\u5024\u3068\u540c\u3058\u3067\u3059\u3002\u8ca0\u306e\u6570\u306e\u5834\u5408\u3001\u305d\u306e1\u306e\u88dc\u6570\u306b\\(1\\)\u3092\u52a0\u3048\u3066\u5f97\u3089\u308c\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306f\u3001\u7b26\u53f7\u7d76\u5bfe\u5024\u30011\u306e\u88dc\u6570\u30012\u306e\u88dc\u6570\u9593\u306e\u5909\u63db\u3092\u793a\u3057\u3066\u3044\u307e\u3059\uff1a

        \u56f3 3-4 \u00a0 \u7b26\u53f7\u7d76\u5bfe\u5024\u30011\u306e\u88dc\u6570\u30012\u306e\u88dc\u6570\u9593\u306e\u5909\u63db

        \u7b26\u53f7\u7d76\u5bfe\u5024\u306f\u6700\u3082\u76f4\u611f\u7684\u3067\u3059\u304c\u3001\u5236\u9650\u304c\u3042\u308a\u307e\u3059\u3002\u4e00\u3064\u306b\u306f\u3001\u7b26\u53f7\u7d76\u5bfe\u5024\u306e\u8ca0\u306e\u6570\u306f\u8a08\u7b97\u3067\u76f4\u63a5\u4f7f\u7528\u3067\u304d\u307e\u305b\u3093\u3002\u4f8b\u3048\u3070\u3001\u7b26\u53f7\u7d76\u5bfe\u5024\u3067\\(1 + (-2)\\)\u3092\u8a08\u7b97\u3059\u308b\u3068\\(-3\\)\u306b\u306a\u308a\u3001\u3053\u308c\u306f\u6b63\u3057\u304f\u3042\u308a\u307e\u305b\u3093\u3002

        \\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 + 1000 \\; 0010 \\newline & = 1000 \\; 0011 \\newline & \\rightarrow -3 \\end{aligned} \\]

        \u3053\u306e\u554f\u984c\u306b\u5bfe\u51e6\u3059\u308b\u305f\u3081\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306f1\u306e\u88dc\u6570\u3092\u5c0e\u5165\u3057\u307e\u3057\u305f\u30021\u306e\u88dc\u6570\u306b\u5909\u63db\u3057\u3066\\(1 + (-2)\\)\u3092\u8a08\u7b97\u3057\u3001\u7d50\u679c\u3092\u7b26\u53f7\u7d76\u5bfe\u5024\u306b\u623b\u3059\u3068\u3001\u6b63\u3057\u3044\u7d50\u679c\\(-1\\)\u304c\u5f97\u3089\u308c\u307e\u3059\u3002

        \\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 \\; \\text{(\u7b26\u53f7\u7d76\u5bfe\u5024)} + 1000 \\; 0010 \\; \\text{(\u7b26\u53f7\u7d76\u5bfe\u5024)} \\newline & = 0000 \\; 0001 \\; \\text{(1\u306e\u88dc\u6570)} + 1111 \\; 1101 \\; \\text{(1\u306e\u88dc\u6570)} \\newline & = 1111 \\; 1110 \\; \\text{(1\u306e\u88dc\u6570)} \\newline & = 1000 \\; 0001 \\; \\text{(\u7b26\u53f7\u7d76\u5bfe\u5024)} \\newline & \\rightarrow -1 \\end{aligned} \\]

        \u307e\u305f\u3001\u7b26\u53f7\u7d76\u5bfe\u5024\u3067\u306f0\u306b2\u3064\u306e\u8868\u73fe\u304c\u3042\u308a\u307e\u3059\uff1a\\(+0\\)\u3068\\(-0\\)\u3067\u3059\u3002\u3053\u308c\u306f0\u306b\u5bfe\u3057\u30662\u3064\u306e\u7570\u306a\u308b\u4e8c\u9032\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u304c\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u3001\u66d6\u6627\u3055\u3092\u5f15\u304d\u8d77\u3053\u3059\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u6761\u4ef6\u30c1\u30a7\u30c3\u30af\u3067\u6b63\u3068\u8ca0\u306e0\u3092\u533a\u5225\u3057\u306a\u3044\u3068\u3001\u6b63\u3057\u304f\u306a\u3044\u7d50\u679c\u306b\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u66d6\u6627\u3055\u306b\u5bfe\u51e6\u3059\u308b\u306b\u306f\u8ffd\u52a0\u306e\u30c1\u30a7\u30c3\u30af\u304c\u5fc5\u8981\u3067\u3001\u8a08\u7b97\u52b9\u7387\u304c\u4f4e\u4e0b\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        \\[ \\begin{aligned} +0 & \\rightarrow 0000 \\; 0000 \\newline -0 & \\rightarrow 1000 \\; 0000 \\end{aligned} \\]

        \u7b26\u53f7\u7d76\u5bfe\u5024\u3068\u540c\u69d8\u306b\u30011\u306e\u88dc\u6570\u3082\u6b63\u3068\u8ca0\u306e0\u306e\u66d6\u6627\u3055\u306b\u60a9\u307e\u3055\u308c\u307e\u3059\u3002\u305d\u306e\u305f\u3081\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306f\u3055\u3089\u306b2\u306e\u88dc\u6570\u3092\u5c0e\u5165\u3057\u307e\u3057\u305f\u3002\u7b26\u53f7\u7d76\u5bfe\u5024\u30011\u306e\u88dc\u6570\u30012\u306e\u88dc\u6570\u306b\u304a\u3051\u308b\u8ca0\u306e0\u306e\u5909\u63db\u904e\u7a0b\u3092\u89b3\u5bdf\u3057\u3066\u307f\u307e\u3057\u3087\u3046\uff1a

        \\[ \\begin{aligned} -0 \\rightarrow \\; & 1000 \\; 0000 \\; \\text{(\u7b26\u53f7\u7d76\u5bfe\u5024)} \\newline = \\; & 1111 \\; 1111 \\; \\text{(1\u306e\u88dc\u6570)} \\newline = 1 \\; & 0000 \\; 0000 \\; \\text{(2\u306e\u88dc\u6570)} \\newline \\end{aligned} \\]

        \u8ca0\u306e0\u306e1\u306e\u88dc\u6570\u306b\\(1\\)\u3092\u52a0\u3048\u308b\u3068\u6841\u4e0a\u304c\u308a\u304c\u767a\u751f\u3057\u307e\u3059\u304c\u3001byte\u306e\u9577\u3055\u306f8\u30d3\u30c3\u30c8\u306e\u307f\u306e\u305f\u3081\u30019\u756a\u76ee\u306e\u30d3\u30c3\u30c8\u3078\u306e\u6841\u4e0a\u304c\u308a\\(1\\)\u306f\u7834\u68c4\u3055\u308c\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001**\u8ca0\u306e0\u306e2\u306e\u88dc\u6570\u306f\\(0000 \\; 0000\\)**\u3067\u3001\u6b63\u306e0\u3068\u540c\u3058\u306b\u306a\u308a\u3001\u66d6\u6627\u3055\u304c\u89e3\u6c7a\u3055\u308c\u307e\u3059\u3002

        \u6700\u5f8c\u306e\u8b0e\u306f\u3001byte\u306e\\([-128, 127]\\)\u306e\u7bc4\u56f2\u3067\u3001\u8ffd\u52a0\u306e\u8ca0\u306e\u6570\\(-128\\)\u304c\u3042\u308b\u3053\u3068\u3067\u3059\u3002\\([-127, +127]\\)\u306e\u533a\u9593\u3067\u306f\u3001\u3059\u3079\u3066\u306e\u6574\u6570\u306b\u5bfe\u5fdc\u3059\u308b\u7b26\u53f7\u7d76\u5bfe\u5024\u30011\u306e\u88dc\u6570\u30012\u306e\u88dc\u6570\u304c\u3042\u308a\u3001\u76f8\u4e92\u5909\u63db\u304c\u53ef\u80fd\u3067\u3042\u308b\u3053\u3068\u3092\u89b3\u5bdf\u3057\u307e\u3059\u3002

        \u3057\u304b\u3057\u30012\u306e\u88dc\u6570\\(1000 \\; 0000\\)\u306f\u5bfe\u5fdc\u3059\u308b\u7b26\u53f7\u7d76\u5bfe\u5024\u3092\u6301\u305f\u306a\u3044\u4f8b\u5916\u3067\u3059\u3002\u5909\u63db\u65b9\u6cd5\u306b\u3088\u308b\u3068\u3001\u305d\u306e\u7b26\u53f7\u7d76\u5bfe\u5024\u306f\\(0000 \\; 0000\\)\u3067\u30010\u3092\u793a\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u77db\u76fe\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u306a\u305c\u306a\u3089\u3001\u305d\u306e2\u306e\u88dc\u6570\u306f\u81ea\u5206\u81ea\u8eab\u3092\u8868\u3059\u3079\u304d\u3060\u304b\u3089\u3067\u3059\u3002\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306f\u3001\u3053\u306e\u7279\u5225\u306a2\u306e\u88dc\u6570\\(1000 \\; 0000\\)\u3092\\(-128\\)\u3092\u8868\u3059\u3082\u306e\u3068\u3057\u3066\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002\u5b9f\u969b\u30012\u306e\u88dc\u6570\u3067\u306e\\((-1) + (-127)\\)\u306e\u8a08\u7b97\u7d50\u679c\u306f\\(-128\\)\u306b\u306a\u308a\u307e\u3059\u3002

        \\[ \\begin{aligned} & (-127) + (-1) \\newline & \\rightarrow 1111 \\; 1111 \\; \\text{(\u7b26\u53f7\u7d76\u5bfe\u5024)} + 1000 \\; 0001 \\; \\text{(\u7b26\u53f7\u7d76\u5bfe\u5024)} \\newline & = 1000 \\; 0000 \\; \\text{(1\u306e\u88dc\u6570)} + 1111 \\; 1110 \\; \\text{(1\u306e\u88dc\u6570)} \\newline & = 1000 \\; 0001 \\; \\text{(2\u306e\u88dc\u6570)} + 1111 \\; 1111 \\; \\text{(2\u306e\u88dc\u6570)} \\newline & = 1000 \\; 0000 \\; \\text{(2\u306e\u88dc\u6570)} \\newline & \\rightarrow -128 \\end{aligned} \\]

        \u304a\u6c17\u3065\u304d\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u3053\u308c\u3089\u306e\u8a08\u7b97\u306f\u3059\u3079\u3066\u52a0\u7b97\u3067\u3042\u308a\u3001\u91cd\u8981\u306a\u4e8b\u5b9f\u3092\u793a\u5506\u3057\u3066\u3044\u307e\u3059\uff1a\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u5185\u90e8\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u56de\u8def\u306f\u4e3b\u306b\u52a0\u7b97\u6f14\u7b97\u3092\u4e2d\u5fc3\u306b\u8a2d\u8a08\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u52a0\u7b97\u304c\u4e57\u7b97\u3001\u9664\u7b97\u3001\u6e1b\u7b97\u306a\u3069\u306e\u4ed6\u306e\u6f14\u7b97\u3068\u6bd4\u8f03\u3057\u3066\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u3067\u5b9f\u88c5\u3057\u3084\u3059\u304f\u3001\u4e26\u5217\u5316\u304c\u5bb9\u6613\u3067\u9ad8\u901f\u8a08\u7b97\u304c\u53ef\u80fd\u3060\u304b\u3089\u3067\u3059\u3002

        \u3053\u308c\u306f\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u304c\u52a0\u7b97\u306e\u307f\u3092\u5b9f\u884c\u3067\u304d\u308b\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u3082\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u52a0\u7b97\u3068\u57fa\u672c\u7684\u306a\u8ad6\u7406\u6f14\u7b97\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306f\u69d8\u3005\u306a\u4ed6\u306e\u6570\u5b66\u6f14\u7b97\u3092\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u6e1b\u7b97\\(a - b\\)\u306f\\(a + (-b)\\)\u306b\u5909\u63db\u3067\u304d\u3001\u4e57\u7b97\u3068\u9664\u7b97\u306f\u8907\u6570\u306e\u52a0\u7b97\u307e\u305f\u306f\u6e1b\u7b97\u306b\u5909\u63db\u3067\u304d\u307e\u3059\u3002

        \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30672\u306e\u88dc\u6570\u3092\u4f7f\u7528\u3059\u308b\u7406\u7531\u3092\u307e\u3068\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a2\u306e\u88dc\u6570\u8868\u73fe\u306b\u3088\u308a\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306f\u540c\u3058\u56de\u8def\u3068\u6f14\u7b97\u3092\u4f7f\u7528\u3057\u3066\u6b63\u3068\u8ca0\u306e\u6570\u306e\u52a0\u7b97\u3092\u51e6\u7406\u3067\u304d\u3001\u6e1b\u7b97\u7528\u306e\u7279\u5225\u306a\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u56de\u8def\u306e\u5fc5\u8981\u6027\u3092\u6392\u9664\u3057\u3001\u6b63\u3068\u8ca0\u306e0\u306e\u66d6\u6627\u3055\u3092\u56de\u907f\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u8a2d\u8a08\u304c\u5927\u5e45\u306b\u7c21\u7d20\u5316\u3055\u308c\u3001\u8a08\u7b97\u52b9\u7387\u304c\u5411\u4e0a\u3057\u307e\u3059\u3002

        2\u306e\u88dc\u6570\u306e\u8a2d\u8a08\u306f\u975e\u5e38\u306b\u5de7\u5999\u3067\u3001\u30b9\u30da\u30fc\u30b9\u306e\u5236\u7d04\u306b\u3088\u308a\u3001\u3053\u3053\u3067\u505c\u6b62\u3057\u307e\u3059\u3002\u8208\u5473\u306e\u3042\u308b\u8aad\u8005\u306f\u3055\u3089\u306b\u63a2\u6c42\u3059\u308b\u3053\u3068\u3092\u5968\u52b1\u3057\u307e\u3059\u3002

        "},{"location":"chapter_data_structure/number_encoding/#332","title":"3.3.2 \u00a0 \u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0","text":"

        \u8208\u5473\u6df1\u3044\u3053\u3068\u306b\u6c17\u3065\u3044\u305f\u304b\u3082\u3057\u308c\u307e\u305b\u3093\uff1a\u540c\u30584\u30d0\u30a4\u30c8\u306e\u9577\u3055\u306b\u3082\u304b\u304b\u308f\u3089\u305a\u3001\u306a\u305cfloat\u306fint\u3068\u6bd4\u8f03\u3057\u3066\u306f\u308b\u304b\u306b\u5927\u304d\u3044\u5024\u306e\u7bc4\u56f2\u3092\u6301\u3064\u306e\u3067\u3057\u3087\u3046\u304b\uff1f\u3053\u308c\u306f\u76f4\u611f\u306b\u53cd\u3059\u308b\u3088\u3046\u306b\u898b\u3048\u307e\u3059\u3002float\u306f\u5206\u6570\u3092\u8868\u73fe\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u7bc4\u56f2\u304c\u7e2e\u5c0f\u3059\u308b\u3068\u4e88\u60f3\u3055\u308c\u308b\u304b\u3089\u3067\u3059\u3002

        \u5b9f\u969b\u3001\u3053\u308c\u306f\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\uff08float\uff09\u3067\u4f7f\u7528\u3055\u308c\u308b\u7570\u306a\u308b\u8868\u73fe\u65b9\u6cd5\u306b\u3088\u308b\u3082\u306e\u3067\u3059\u300232\u30d3\u30c3\u30c8\u306e\u4e8c\u9032\u6570\u3092\u6b21\u306e\u3088\u3046\u306b\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\uff1a

        \\[ b_{31} b_{30} b_{29} \\ldots b_2 b_1 b_0 \\]

        IEEE 754\u6a19\u6e96\u306b\u3088\u308b\u3068\u300132\u30d3\u30c3\u30c8\u306efloat\u306f\u6b21\u306e3\u3064\u306e\u90e8\u5206\u3067\u69cb\u6210\u3055\u308c\u307e\u3059\uff1a

        • \u7b26\u53f7\u30d3\u30c3\u30c8\\(\\mathrm{S}\\)\uff1a1\u30d3\u30c3\u30c8\u3092\u5360\u6709\u3057\u3001\\(b_{31}\\)\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002
        • \u6307\u6570\u30d3\u30c3\u30c8\\(\\mathrm{E}\\)\uff1a8\u30d3\u30c3\u30c8\u3092\u5360\u6709\u3057\u3001\\(b_{30} b_{29} \\ldots b_{23}\\)\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002
        • \u4eee\u6570\u30d3\u30c3\u30c8\\(\\mathrm{N}\\)\uff1a23\u30d3\u30c3\u30c8\u3092\u5360\u6709\u3057\u3001\\(b_{22} b_{21} \\ldots b_0\\)\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002

        \u4e8c\u9032float\u6570\u306e\u5024\u306f\u6b21\u306e\u3088\u3046\u306b\u8a08\u7b97\u3055\u308c\u307e\u3059\uff1a

        \\[ \\text{val} = (-1)^{b_{31}} \\times 2^{\\left(b_{30} b_{29} \\ldots b_{23}\\right)_2 - 127} \\times \\left(1 . b_{22} b_{21} \\ldots b_0\\right)_2 \\]

        \u5341\u9032\u516c\u5f0f\u306b\u5909\u63db\u3059\u308b\u3068\u3001\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\uff1a

        \\[ \\text{val} = (-1)^{\\mathrm{S}} \\times 2^{\\mathrm{E} - 127} \\times (1 + \\mathrm{N}) \\]

        \u5404\u6210\u5206\u306e\u7bc4\u56f2\u306f\uff1a

        \\[ \\begin{aligned} \\mathrm{S} \\in & \\{ 0, 1\\}, \\quad \\mathrm{E} \\in \\{ 1, 2, \\dots, 254 \\} \\newline (1 + \\mathrm{N}) = & (1 + \\sum_{i=1}^{23} b_{23-i} \\times 2^{-i}) \\subset [1, 2 - 2^{-23}] \\end{aligned} \\]

        \u56f3 3-5 \u00a0 IEEE 754\u6a19\u6e96\u3067\u306e\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u306e\u8a08\u7b97\u4f8b

        \u4e0a\u306e\u56f3\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\u4f8b\u306e\u30c7\u30fc\u30bf\\(\\mathrm{S} = 0\\)\u3001\\(\\mathrm{E} = 124\\)\u3001\\(\\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\\)\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\uff1a

        \\[ \\text{val} = (-1)^0 \\times 2^{124 - 127} \\times (1 + 0.375) = 0.171875 \\]

        \u3053\u308c\u3067\u6700\u521d\u306e\u8cea\u554f\u306b\u7b54\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1afloat\u306e\u8868\u73fe\u306b\u306f\u6307\u6570\u30d3\u30c3\u30c8\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u305f\u3081\u3001int\u3088\u308a\u3082\u306f\u308b\u304b\u306b\u5927\u304d\u3044\u7bc4\u56f2\u3092\u6301\u3061\u307e\u3059\u3002\u4e0a\u8a18\u306e\u8a08\u7b97\u306b\u57fa\u3065\u304f\u3068\u3001float\u3067\u8868\u73fe\u53ef\u80fd\u306a\u6700\u5927\u6b63\u306e\u6570\u306f\u7d04\\(2^{254 - 127} \\times (2 - 2^{-23}) \\approx 3.4 \\times 10^{38}\\)\u3067\u3001\u6700\u5c0f\u8ca0\u306e\u6570\u306f\u7b26\u53f7\u30d3\u30c3\u30c8\u3092\u5207\u308a\u66ff\u3048\u308b\u3053\u3068\u3067\u5f97\u3089\u308c\u307e\u3059\u3002

        \u3057\u304b\u3057\u3001float\u306e\u62e1\u5f35\u3055\u308c\u305f\u7bc4\u56f2\u306e\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u306f\u7cbe\u5ea6\u306e\u72a0\u7272\u3067\u3059\u3002\u6574\u6570\u578bint\u306f32\u30d3\u30c3\u30c8\u3059\u3079\u3066\u3092\u6570\u5024\u8868\u73fe\u306b\u4f7f\u7528\u3057\u3001\u5024\u306f\u5747\u7b49\u306b\u5206\u5e03\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u6307\u6570\u30d3\u30c3\u30c8\u306e\u305f\u3081\u3001float\u306e\u5024\u304c\u5927\u304d\u3044\u307b\u3069\u3001\u96a3\u63a5\u3059\u308b\u6570\u5024\u9593\u306e\u5dee\u304c\u5927\u304d\u304f\u306a\u308a\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u8868\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u6307\u6570\u30d3\u30c3\u30c8\\(\\mathrm{E} = 0\\)\u3068\\(\\mathrm{E} = 255\\)\u306f\u7279\u5225\u306a\u610f\u5473\u3092\u6301\u3061\u30010\u3001\u7121\u9650\u5927\u3001\\(\\mathrm{NaN}\\)\u306a\u3069\u3092\u8868\u73fe\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002

        \u8868 3-2 \u00a0 \u6307\u6570\u30d3\u30c3\u30c8\u306e\u610f\u5473

        \u6307\u6570\u30d3\u30c3\u30c8E \u4eee\u6570\u30d3\u30c3\u30c8\\(\\mathrm{N} = 0\\) \u4eee\u6570\u30d3\u30c3\u30c8\\(\\mathrm{N} \\ne 0\\) \u8a08\u7b97\u516c\u5f0f \\(0\\) \\(\\pm 0\\) \u975e\u6b63\u898f\u5316\u6570 \\((-1)^{\\mathrm{S}} \\times 2^{-126} \\times (0.\\mathrm{N})\\) \\(1, 2, \\dots, 254\\) \u6b63\u898f\u5316\u6570 \u6b63\u898f\u5316\u6570 \\((-1)^{\\mathrm{S}} \\times 2^{(\\mathrm{E} -127)} \\times (1.\\mathrm{N})\\) \\(255\\) \\(\\pm \\infty\\) \\(\\mathrm{NaN}\\)

        \u975e\u6b63\u898f\u5316\u6570\u306f\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u306e\u7cbe\u5ea6\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u306f\u6ce8\u76ee\u306b\u5024\u3057\u307e\u3059\u3002\u6700\u5c0f\u306e\u6b63\u306e\u6b63\u898f\u5316\u6570\u306f\\(2^{-126}\\)\u3067\u3001\u6700\u5c0f\u306e\u6b63\u306e\u975e\u6b63\u898f\u5316\u6570\u306f\\(2^{-126} \\times 2^{-23}\\)\u3067\u3059\u3002

        \u500d\u7cbe\u5ea6double\u3082float\u3068\u540c\u69d8\u306e\u8868\u73fe\u65b9\u6cd5\u3092\u4f7f\u7528\u3057\u307e\u3059\u304c\u3001\u7c21\u6f54\u3055\u306e\u305f\u3081\u3053\u3053\u3067\u306f\u8a73\u8ff0\u3057\u307e\u305b\u3093\u3002

        "},{"location":"chapter_data_structure/summary/","title":"3.5 \u00a0 \u307e\u3068\u3081","text":""},{"location":"chapter_data_structure/summary/#1","title":"1. \u00a0 \u91cd\u8981\u306a\u30dd\u30a4\u30f3\u30c8","text":"
        • \u30c7\u30fc\u30bf\u69cb\u9020\u306f\u8ad6\u7406\u69cb\u9020\u3068\u7269\u7406\u69cb\u9020\u306e2\u3064\u306e\u89b3\u70b9\u304b\u3089\u5206\u985e\u3067\u304d\u307e\u3059\u3002\u8ad6\u7406\u69cb\u9020\u306f\u30c7\u30fc\u30bf\u9593\u306e\u8ad6\u7406\u7684\u95a2\u4fc2\u3092\u8a18\u8ff0\u3057\u3001\u7269\u7406\u69cb\u9020\u306f\u30c7\u30fc\u30bf\u304c\u30e1\u30e2\u30ea\u306b\u3069\u306e\u3088\u3046\u306b\u683c\u7d0d\u3055\u308c\u308b\u304b\u3092\u8a18\u8ff0\u3057\u307e\u3059\u3002
        • \u3088\u304f\u4f7f\u7528\u3055\u308c\u308b\u8ad6\u7406\u69cb\u9020\u306b\u306f\u3001\u7dda\u5f62\u69cb\u9020\u3001\u6728\u3001\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u304c\u3042\u308a\u307e\u3059\u3002\u901a\u5e38\u3001\u8ad6\u7406\u69cb\u9020\u306b\u57fa\u3065\u3044\u3066\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u7dda\u5f62\uff08\u914d\u5217\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u30b9\u30bf\u30c3\u30af\u3001\u30ad\u30e5\u30fc\uff09\u3068\u975e\u7dda\u5f62\uff08\u6728\u3001\u30b0\u30e9\u30d5\u3001\u30d2\u30fc\u30d7\uff09\u306b\u5206\u3051\u307e\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u5b9f\u88c5\u306f\u7dda\u5f62\u3068\u975e\u7dda\u5f62\u306e\u4e21\u65b9\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u542b\u3080\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002
        • \u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u5b9f\u884c\u4e2d\u306e\u969b\u3001\u30c7\u30fc\u30bf\u306f\u30e1\u30e2\u30ea\u306b\u683c\u7d0d\u3055\u308c\u307e\u3059\u3002\u5404\u30e1\u30e2\u30ea\u7a7a\u9593\u306b\u306f\u5bfe\u5fdc\u3059\u308b\u30a2\u30c9\u30ec\u30b9\u304c\u3042\u308a\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u3053\u308c\u3089\u306e\u30a2\u30c9\u30ec\u30b9\u3092\u901a\u3058\u3066\u30c7\u30fc\u30bf\u306b\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002
        • \u7269\u7406\u69cb\u9020\u306f\u9023\u7d9a\u7a7a\u9593\u683c\u7d0d\uff08\u914d\u5217\uff09\u3068\u96e2\u6563\u7a7a\u9593\u683c\u7d0d\uff08\u9023\u7d50\u30ea\u30b9\u30c8\uff09\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3059\u3079\u3066\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u914d\u5217\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u307e\u305f\u306f\u305d\u306e\u4e21\u65b9\u306e\u7d44\u307f\u5408\u308f\u305b\u3092\u4f7f\u7528\u3057\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002
        • \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u306b\u306f\u3001\u6574\u6570\uff08byte\u3001short\u3001int\u3001long\uff09\u3001\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\uff08float\u3001double\uff09\u3001\u6587\u5b57\uff08char\uff09\u3001\u30d6\u30fc\u30eb\u5024\uff08bool\uff09\u304c\u542b\u307e\u308c\u307e\u3059\u3002\u30c7\u30fc\u30bf\u578b\u306e\u5024\u306e\u7bc4\u56f2\u306f\u3001\u305d\u306e\u30b5\u30a4\u30ba\u3068\u8868\u73fe\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002
        • \u7b26\u53f7\u7d76\u5bfe\u5024\u30011\u306e\u88dc\u6570\u30012\u306e\u88dc\u6570\u306f\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u3067\u6574\u6570\u3092\u30a8\u30f3\u30b3\u30fc\u30c9\u3059\u308b3\u3064\u306e\u65b9\u6cd5\u3067\u3042\u308a\u3001\u76f8\u4e92\u306b\u5909\u63db\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u7b26\u53f7\u7d76\u5bfe\u5024\u306e\u6700\u4e0a\u4f4d\u30d3\u30c3\u30c8\u306f\u7b26\u53f7\u30d3\u30c3\u30c8\u3067\u3001\u6b8b\u308a\u306e\u30d3\u30c3\u30c8\u306f\u6570\u5024\u306e\u5024\u3092\u8868\u3057\u307e\u3059\u3002
        • \u6574\u6570\u306f\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30672\u306e\u88dc\u6570\u306b\u3088\u3063\u3066\u30a8\u30f3\u30b3\u30fc\u30c9\u3055\u308c\u307e\u3059\u3002\u3053\u306e\u8868\u73fe\u306e\u5229\u70b9\u306b\u306f\u3001(i)\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u304c\u6b63\u3068\u8ca0\u306e\u6574\u6570\u306e\u52a0\u7b97\u3092\u7d71\u4e00\u3067\u304d\u308b\u3001(ii)\u6e1b\u7b97\u7528\u306e\u7279\u5225\u306a\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u56de\u8def\u3092\u8a2d\u8a08\u3059\u308b\u5fc5\u8981\u304c\u306a\u3044\u3001(iii)\u6b63\u3068\u8ca0\u306e0\u306e\u66d6\u6627\u3055\u304c\u306a\u3044\u3001\u304c\u3042\u308a\u307e\u3059\u3002
        • \u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u306e\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306f\u30011\u3064\u306e\u7b26\u53f7\u30d3\u30c3\u30c8\u30018\u3064\u306e\u6307\u6570\u30d3\u30c3\u30c8\u300123\u306e\u4eee\u6570\u30d3\u30c3\u30c8\u3067\u69cb\u6210\u3055\u308c\u307e\u3059\u3002\u6307\u6570\u30d3\u30c3\u30c8\u306e\u305f\u3081\u3001\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u306e\u7bc4\u56f2\u306f\u6574\u6570\u3088\u308a\u3082\u306f\u308b\u304b\u306b\u5927\u304d\u304f\u306a\u308a\u307e\u3059\u304c\u3001\u7cbe\u5ea6\u3092\u72a0\u7272\u306b\u3057\u307e\u3059\u3002
        • ASCII\u306f\u6700\u521d\u671f\u306e\u82f1\u8a9e\u6587\u5b57\u30bb\u30c3\u30c8\u3067\u30011\u30d0\u30a4\u30c8\u306e\u9577\u3055\u3067\u8a08127\u6587\u5b57\u3067\u3059\u3002GBK\u306f\u4eba\u6c17\u306e\u3042\u308b\u4e2d\u56fd\u8a9e\u6587\u5b57\u30bb\u30c3\u30c8\u3067\u30012\u4e07\u6587\u5b57\u4ee5\u4e0a\u306e\u4e2d\u56fd\u8a9e\u6587\u5b57\u3092\u542b\u307f\u307e\u3059\u3002Unicode\u306f\u4e16\u754c\u306e\u69d8\u3005\u306a\u8a00\u8a9e\u306e\u6587\u5b57\u3092\u542b\u3080\u5b8c\u5168\u306a\u6587\u5b57\u30bb\u30c3\u30c8\u6a19\u6e96\u3092\u63d0\u4f9b\u3059\u308b\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u3001\u6587\u5b57\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u65b9\u6cd5\u306e\u4e0d\u4e00\u81f4\u306b\u3088\u308b\u6587\u5b57\u5316\u3051\u554f\u984c\u3092\u89e3\u6c7a\u3057\u307e\u3059\u3002
        • UTF-8\u306f\u6700\u3082\u4eba\u6c17\u304c\u3042\u308a\u4e00\u822c\u7684\u306aUnicode\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u65b9\u6cd5\u3067\u3059\u3002\u3053\u308c\u306f\u53ef\u5909\u9577\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u65b9\u6cd5\u3067\u3001\u512a\u308c\u305f\u62e1\u5f35\u6027\u3068\u7a7a\u9593\u52b9\u7387\u3092\u6301\u3061\u307e\u3059\u3002UTF-16\u3068UTF-32\u306f\u56fa\u5b9a\u9577\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u65b9\u6cd5\u3067\u3059\u3002\u4e2d\u56fd\u8a9e\u6587\u5b57\u3092\u30a8\u30f3\u30b3\u30fc\u30c9\u3059\u308b\u969b\u3001UTF-16\u306fUTF-8\u3088\u308a\u3082\u5c11\u306a\u3044\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002Java\u3084C#\u306a\u3069\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067UTF-16\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        "},{"location":"chapter_data_structure/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q: \u306a\u305c\u30cf\u30c3\u30b7\u30e5\u8868\u306f\u7dda\u5f62\u3068\u975e\u7dda\u5f62\u306e\u4e21\u65b9\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u542b\u3080\u306e\u3067\u3059\u304b\uff1f

        \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u57fa\u790e\u69cb\u9020\u306f\u914d\u5217\u3067\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3001\u300c\u30c1\u30a7\u30a4\u30f3\u6cd5\u300d\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\uff08\u5f8c\u306e\u7bc0\u300c\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u300d\u3067\u8aac\u660e\uff09\uff1a\u914d\u5217\u306e\u5404\u30d0\u30b1\u30c3\u30c8\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u6307\u3057\u3001\u305d\u306e\u9577\u3055\u304c\u7279\u5b9a\u306e\u95be\u5024\u3088\u308a\u5927\u304d\u304f\u306a\u308b\u3068\u6728\uff08\u901a\u5e38\u306f\u8d64\u9ed2\u6728\uff09\u306b\u5909\u63db\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002 \u683c\u7d0d\u306e\u89b3\u70b9\u304b\u3089\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u57fa\u790e\u69cb\u9020\u306f\u914d\u5217\u3067\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u306b\u306f\u5024\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u307e\u305f\u306f\u6728\u304c\u542b\u307e\u308c\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306f\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\uff08\u914d\u5217\u3001\u9023\u7d50\u30ea\u30b9\u30c8\uff09\u3068\u975e\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\uff08\u6728\uff09\u306e\u4e21\u65b9\u3092\u542b\u3080\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002

        Q: char\u578b\u306e\u9577\u3055\u306f1\u30d0\u30a4\u30c8\u3067\u3059\u304b\uff1f

        char\u578b\u306e\u9577\u3055\u306f\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u65b9\u6cd5\u306b\u3088\u3063\u3066\u6c7a\u307e\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001Java\u3001JavaScript\u3001TypeScript\u3001C#\u306f\u3059\u3079\u3066UTF-16\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\uff08Unicode\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u3092\u4fdd\u5b58\u3059\u308b\u305f\u3081\uff09\u3092\u4f7f\u7528\u3059\u308b\u305f\u3081\u3001char\u578b\u306e\u9577\u3055\u306f2\u30d0\u30a4\u30c8\u3067\u3059\u3002

        Q: \u914d\u5217\u30d9\u30fc\u30b9\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u300c\u9759\u7684\u30c7\u30fc\u30bf\u69cb\u9020\u300d\u3068\u547c\u3076\u3053\u3068\u306b\u66d6\u6627\u3055\u306f\u3042\u308a\u307e\u305b\u3093\u304b\uff1f\u30b9\u30bf\u30c3\u30af\u3082\u30d7\u30c3\u30b7\u30e5\u3084\u30dd\u30c3\u30d7\u306a\u3069\u306e\u300c\u52d5\u7684\u300d\u64cd\u4f5c\u3092\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002

        \u30b9\u30bf\u30c3\u30af\u306f\u52d5\u7684\u306a\u30c7\u30fc\u30bf\u64cd\u4f5c\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\u304c\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u4f9d\u7136\u3068\u3057\u3066\u300c\u9759\u7684\u300d\u3067\u3059\uff08\u9577\u3055\u304c\u56fa\u5b9a\uff09\u3002\u914d\u5217\u30d9\u30fc\u30b9\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u52d5\u7684\u306b\u8981\u7d20\u3092\u8ffd\u52a0\u307e\u305f\u306f\u524a\u9664\u3067\u304d\u307e\u3059\u304c\u3001\u305d\u306e\u5bb9\u91cf\u306f\u56fa\u5b9a\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u30b9\u30bf\u30c3\u30af\u30b5\u30a4\u30ba\u304c\u4e8b\u524d\u306b\u5272\u308a\u5f53\u3066\u3089\u308c\u305f\u30b5\u30a4\u30ba\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u53e4\u3044\u914d\u5217\u306f\u65b0\u3057\u304f\u4f5c\u6210\u3055\u308c\u305f\u3088\u308a\u5927\u304d\u306a\u914d\u5217\u306b\u30b3\u30d4\u30fc\u3055\u308c\u307e\u3059\u3002

        Q: \u30b9\u30bf\u30c3\u30af\uff08\u30ad\u30e5\u30fc\uff09\u3092\u69cb\u7bc9\u3059\u308b\u969b\u3001\u305d\u306e\u30b5\u30a4\u30ba\u304c\u6307\u5b9a\u3055\u308c\u3066\u3044\u306a\u3044\u306e\u306b\u3001\u306a\u305c\u300c\u9759\u7684\u30c7\u30fc\u30bf\u69cb\u9020\u300d\u306a\u306e\u3067\u3059\u304b\uff1f

        \u9ad8\u7d1a\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u306f\u3001\u30b9\u30bf\u30c3\u30af\uff08\u30ad\u30e5\u30fc\uff09\u306e\u521d\u671f\u5bb9\u91cf\u3092\u624b\u52d5\u3067\u6307\u5b9a\u3059\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u3053\u306e\u30bf\u30b9\u30af\u306f\u30af\u30e9\u30b9\u5185\u3067\u81ea\u52d5\u7684\u306b\u5b8c\u4e86\u3055\u308c\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001Java\u306eArrayList\u306e\u521d\u671f\u5bb9\u91cf\u306f\u901a\u5e3810\u3067\u3059\u3002\u3055\u3089\u306b\u3001\u62e1\u5f35\u64cd\u4f5c\u3082\u81ea\u52d5\u7684\u306b\u5b8c\u4e86\u3055\u308c\u307e\u3059\u3002\u8a73\u7d30\u306b\u3064\u3044\u3066\u306f\u3001\u5f8c\u7d9a\u306e\u300c\u30ea\u30b9\u30c8\u300d\u306e\u7ae0\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        Q: \u7b26\u53f7\u7d76\u5bfe\u5024\u30922\u306e\u88dc\u6570\u306b\u5909\u63db\u3059\u308b\u65b9\u6cd5\u306f\u300c\u6700\u521d\u306b\u5426\u5b9a\u3057\u3066\u304b\u30891\u3092\u52a0\u3048\u308b\u300d\u3067\u3059\u306e\u3067\u30012\u306e\u88dc\u6570\u3092\u7b26\u53f7\u7d76\u5bfe\u5024\u306b\u5909\u63db\u3059\u308b\u3053\u3068\u306f\u305d\u306e\u9006\u64cd\u4f5c\u300c\u6700\u521d\u306b1\u3092\u6e1b\u7b97\u3057\u3066\u304b\u3089\u5426\u5b9a\u3059\u308b\u300d\u3067\u3042\u308b\u3079\u304d\u3067\u3059\u3002 \u3057\u304b\u3057\u30012\u306e\u88dc\u6570\u3082\u300c\u6700\u521d\u306b\u5426\u5b9a\u3057\u3066\u304b\u30891\u3092\u52a0\u3048\u308b\u300d\u3092\u901a\u3058\u3066\u7b26\u53f7\u7d76\u5bfe\u5024\u306b\u5909\u63db\u3067\u304d\u307e\u3059\u3002\u306a\u305c\u3067\u3059\u304b\uff1f

        A: \u3053\u308c\u306f\u3001\u7b26\u53f7\u7d76\u5bfe\u5024\u30682\u306e\u88dc\u6570\u9593\u306e\u76f8\u4e92\u5909\u63db\u304c\u300c\u88dc\u6570\u300d\u306e\u8a08\u7b97\u3068\u7b49\u4fa1\u3060\u304b\u3089\u3067\u3059\u3002\u307e\u305a\u88dc\u6570\u3092\u5b9a\u7fa9\u3057\u307e\u3059\uff1a\\(a + b = c\\)\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\\(a\\)\u306f\\(b\\)\u306e\\(c\\)\u306b\u5bfe\u3059\u308b\u88dc\u6570\u3068\u8a00\u3044\u3001\u9006\u306b\\(b\\)\u306f\\(a\\)\u306e\\(c\\)\u306b\u5bfe\u3059\u308b\u88dc\u6570\u3068\u8a00\u3044\u307e\u3059\u3002

        \u9577\u3055\\(n = 4\\)\u306e\u4e8c\u9032\u6570\\(0010\\)\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u3053\u306e\u6570\u304c\u7b26\u53f7\u7d76\u5bfe\u5024\uff08\u7b26\u53f7\u30d3\u30c3\u30c8\u3092\u7121\u8996\uff09\u306e\u5834\u5408\u3001\u305d\u306e2\u306e\u88dc\u6570\u306f\u300c\u6700\u521d\u306b\u5426\u5b9a\u3057\u3066\u304b\u30891\u3092\u52a0\u3048\u308b\u300d\u3053\u3068\u3067\u5f97\u3089\u308c\u307e\u3059\uff1a

        \\[ 0010 \\rightarrow 1101 \\rightarrow 1110 \\]

        \u7b26\u53f7\u7d76\u5bfe\u5024\u30682\u306e\u88dc\u6570\u306e\u548c\u304c\\(0010 + 1110 = 10000\\)\u3067\u3042\u308b\u3053\u3068\u3092\u89b3\u5bdf\u3057\u307e\u3059\u3002\u3064\u307e\u308a\u30012\u306e\u88dc\u6570\\(1110\\)\u306f\u7b26\u53f7\u7d76\u5bfe\u5024\\(0010\\)\u306e\\(10000\\)\u306b\u5bfe\u3059\u308b\u300c\u88dc\u6570\u300d\u3067\u3059\u3002\u3053\u308c\u306f\u3001\u4e0a\u8a18\u306e\u300c\u6700\u521d\u306b\u5426\u5b9a\u3057\u3066\u304b\u30891\u3092\u52a0\u3048\u308b\u300d\u304c\\(10000\\)\u306b\u5bfe\u3059\u308b\u88dc\u6570\u306e\u8a08\u7b97\u3068\u7b49\u4fa1\u3067\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

        \u3067\u306f\u3001\\(1110\\)\u306e\\(10000\\)\u306b\u5bfe\u3059\u308b\u300c\u88dc\u6570\u300d\u306f\u4f55\u3067\u3057\u3087\u3046\u304b\uff1f\u300c\u6700\u521d\u306b\u5426\u5b9a\u3057\u3066\u304b\u30891\u3092\u52a0\u3048\u308b\u300d\u3053\u3068\u3067\u8a08\u7b97\u3067\u304d\u307e\u3059\uff1a

        \\[ 1110 \\rightarrow 0001 \\rightarrow 0010 \\]

        \u8a00\u3044\u63db\u3048\u308b\u3068\u3001\u7b26\u53f7\u7d76\u5bfe\u5024\u30682\u306e\u88dc\u6570\u306f\u4e92\u3044\u306b\\(10000\\)\u306b\u5bfe\u3059\u308b\u300c\u88dc\u6570\u300d\u3067\u3042\u308b\u305f\u3081\u3001\u300c\u7b26\u53f7\u7d76\u5bfe\u5024\u304b\u30892\u306e\u88dc\u6570\u300d\u3068\u300c2\u306e\u88dc\u6570\u304b\u3089\u7b26\u53f7\u7d76\u5bfe\u5024\u300d\u306f\u540c\u3058\u64cd\u4f5c\uff08\u6700\u521d\u306b\u5426\u5b9a\u3057\u3066\u304b\u30891\u3092\u52a0\u3048\u308b\uff09\u3067\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002

        \u3082\u3061\u308d\u3093\u3001\u300c\u6700\u521d\u306b\u5426\u5b9a\u3057\u3066\u304b\u30891\u3092\u52a0\u3048\u308b\u300d\u306e\u9006\u64cd\u4f5c\u3092\u4f7f\u7528\u3057\u30662\u306e\u88dc\u6570\\(1110\\)\u306e\u7b26\u53f7\u7d76\u5bfe\u5024\u3092\u6c42\u3081\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u300c\u6700\u521d\u306b1\u3092\u6e1b\u7b97\u3057\u3066\u304b\u3089\u5426\u5b9a\u3059\u308b\u300d\uff1a

        \\[ 1110 \\rightarrow 1101 \\rightarrow 0010 \\]

        \u8981\u7d04\u3059\u308b\u3068\u3001\u300c\u6700\u521d\u306b\u5426\u5b9a\u3057\u3066\u304b\u30891\u3092\u52a0\u3048\u308b\u300d\u3068\u300c\u6700\u521d\u306b1\u3092\u6e1b\u7b97\u3057\u3066\u304b\u3089\u5426\u5b9a\u3059\u308b\u300d\u306f\u4e21\u65b9\u3068\u3082\\(10000\\)\u306b\u5bfe\u3059\u308b\u88dc\u6570\u3092\u8a08\u7b97\u3057\u3066\u304a\u308a\u3001\u7b49\u4fa1\u3067\u3059\u3002

        \u672c\u8cea\u7684\u306b\u3001\u300c\u5426\u5b9a\u300d\u64cd\u4f5c\u306f\u5b9f\u969b\u306b\u306f\\(1111\\)\u306b\u5bfe\u3059\u308b\u88dc\u6570\u3092\u6c42\u3081\u308b\u3053\u3068\u3067\u3059\uff08\u7b26\u53f7\u7d76\u5bfe\u5024 + 1\u306e\u88dc\u6570 = 1111\u304c\u5e38\u306b\u6210\u308a\u7acb\u3064\u305f\u3081\uff09\u3002\u305d\u3057\u30661\u306e\u88dc\u6570\u306b1\u3092\u52a0\u3048\u308b\u3053\u3068\u306f\\(10000\\)\u306b\u5bfe\u3059\u308b2\u306e\u88dc\u6570\u3068\u7b49\u3057\u304f\u306a\u308a\u307e\u3059\u3002

        \u4e0a\u8a18\u3067\u306f\\(n = 4\\)\u3092\u4f8b\u306b\u53d6\u308a\u307e\u3057\u305f\u304c\u3001\u4efb\u610f\u306e\u6841\u6570\u306e\u4efb\u610f\u306e\u4e8c\u9032\u6570\u306b\u4e00\u822c\u5316\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_divide_and_conquer/","title":"\u7b2c 12 \u7ae0 \u00a0 \u5206\u5272\u7d71\u6cbb","text":"

        Abstract

        \u56f0\u96e3\u306a\u554f\u984c\u306f\u5c64\u3092\u91cd\u306d\u3066\u5206\u89e3\u3055\u308c\u3001\u5404\u5206\u89e3\u306b\u3088\u3063\u3066\u3088\u308a\u5358\u7d14\u306b\u306a\u308a\u307e\u3059\u3002

        \u5206\u5272\u7d71\u6cbb\u306f\u6df1\u3044\u771f\u7406\u3092\u660e\u3089\u304b\u306b\u3057\u307e\u3059\uff1a\u5358\u7d14\u3055\u304b\u3089\u59cb\u3081\u308c\u3070\u3001\u8907\u96d1\u3055\u306f\u89e3\u6c7a\u3055\u308c\u308b\u3002

        "},{"location":"chapter_divide_and_conquer/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 12.1 \u00a0 \u5206\u5272\u7d71\u6cbb\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0
        • 12.2 \u00a0 \u5206\u5272\u7d71\u6cbb\u63a2\u7d22\u6226\u7565
        • 12.3 \u00a0 \u4e8c\u5206\u6728\u69cb\u7bc9\u554f\u984c
        • 12.4 \u00a0 \u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c
        • 12.5 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_divide_and_conquer/binary_search_recur/","title":"12.2 \u00a0 \u5206\u5272\u7d71\u6cbb\u691c\u7d22\u6226\u7565","text":"

        \u79c1\u305f\u3061\u306f\u691c\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u4e3b\u306b2\u3064\u306e\u30ab\u30c6\u30b4\u30ea\u306b\u5206\u985e\u3055\u308c\u308b\u3053\u3068\u3092\u5b66\u3073\u307e\u3057\u305f\u3002

        • \u7dcf\u5f53\u305f\u308a\u691c\u7d22\uff1a\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u8d70\u67fb\u3059\u308b\u3053\u3068\u3067\u5b9f\u88c5\u3055\u308c\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u3067\u3059\u3002
        • \u9069\u5fdc\u691c\u7d22\uff1a\u72ec\u7279\u306a\u30c7\u30fc\u30bf\u7d44\u7e54\u5f62\u5f0f\u3084\u4e8b\u524d\u60c5\u5831\u3092\u5229\u7528\u3057\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(\\log n)\\) \u307e\u305f\u306f \\(O(1)\\) \u306b\u9054\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u5b9f\u969b\u3001\u6642\u9593\u8a08\u7b97\u91cf\u304c \\(O(\\log n)\\) \u306e\u691c\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u901a\u5e38\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306b\u57fa\u3065\u3044\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u4e8c\u5206\u63a2\u7d22\u3084\u6728\u306a\u3069\u3067\u3059\u3002

        • \u4e8c\u5206\u63a2\u7d22\u306e\u5404\u30b9\u30c6\u30c3\u30d7\u306f\u3001\u554f\u984c\uff08\u914d\u5217\u5185\u3067\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u3092\u691c\u7d22\u3059\u308b\uff09\u3092\u3088\u308a\u5c0f\u3055\u306a\u554f\u984c\uff08\u914d\u5217\u306e\u534a\u5206\u3067\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u3092\u691c\u7d22\u3059\u308b\uff09\u306b\u5206\u5272\u3057\u3001\u914d\u5217\u304c\u7a7a\u306b\u306a\u308b\u304b\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u304c\u898b\u3064\u304b\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002
        • \u6728\u306f\u5206\u5272\u7d71\u6cbb\u306e\u30a2\u30a4\u30c7\u30a2\u3092\u8868\u73fe\u3057\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u3001AVL\u6728\u3001\u30d2\u30fc\u30d7\u306a\u3069\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u306f\u3001\u69d8\u3005\u306a\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(\\log n)\\) \u3067\u3059\u3002

        \u4e8c\u5206\u63a2\u7d22\u306e\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002

        • \u554f\u984c\u3092\u5206\u5272\u3067\u304d\u308b\uff1a\u4e8c\u5206\u63a2\u7d22\u306f\u5143\u306e\u554f\u984c\uff08\u914d\u5217\u5185\u3067\u306e\u691c\u7d22\uff09\u3092\u90e8\u5206\u554f\u984c\uff08\u914d\u5217\u306e\u534a\u5206\u3067\u306e\u691c\u7d22\uff09\u306b\u518d\u5e30\u7684\u306b\u5206\u5272\u3057\u3001\u4e2d\u9593\u8981\u7d20\u3068\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u3092\u6bd4\u8f03\u3059\u308b\u3053\u3068\u3067\u5b9f\u73fe\u3055\u308c\u307e\u3059\u3002
        • \u90e8\u5206\u554f\u984c\u306f\u72ec\u7acb\u3057\u3066\u3044\u308b\uff1a\u4e8c\u5206\u63a2\u7d22\u3067\u306f\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u4e00\u3064\u306e\u90e8\u5206\u554f\u984c\u3092\u51e6\u7406\u3057\u3001\u4ed6\u306e\u90e8\u5206\u554f\u984c\u306b\u5f71\u97ff\u3055\u308c\u307e\u305b\u3093\u3002
        • \u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u30de\u30fc\u30b8\u3059\u308b\u5fc5\u8981\u304c\u306a\u3044\uff1a\u4e8c\u5206\u63a2\u7d22\u306f\u7279\u5b9a\u306e\u8981\u7d20\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u30de\u30fc\u30b8\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u305b\u3093\u3002\u90e8\u5206\u554f\u984c\u304c\u89e3\u6c7a\u3055\u308c\u308b\u3068\u3001\u5143\u306e\u554f\u984c\u3082\u89e3\u6c7a\u3055\u308c\u307e\u3059\u3002

        \u5206\u5272\u7d71\u6cbb\u306f\u691c\u7d22\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u306a\u305c\u306a\u3089\u3001\u7dcf\u5f53\u305f\u308a\u691c\u7d22\u306f\u30e9\u30a6\u30f3\u30c9\u3054\u3068\u306b1\u3064\u306e\u9078\u629e\u80a2\u3057\u304b\u9664\u53bb\u3067\u304d\u307e\u305b\u3093\u304c\u3001\u5206\u5272\u7d71\u6cbb\u306f\u9078\u629e\u80a2\u306e\u534a\u5206\u3092\u9664\u53bb\u3067\u304d\u308b\u304b\u3089\u3067\u3059\u3002

        "},{"location":"chapter_divide_and_conquer/binary_search_recur/#1","title":"1. \u00a0 \u5206\u5272\u7d71\u6cbb\u306b\u57fa\u3065\u304f\u4e8c\u5206\u63a2\u7d22\u306e\u5b9f\u88c5","text":"

        \u524d\u306e\u7ae0\u3067\u306f\u3001\u4e8c\u5206\u63a2\u7d22\u306f\u53cd\u5fa9\u306b\u57fa\u3065\u3044\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3057\u305f\u3002\u4eca\u5ea6\u306f\u3001\u5206\u5272\u7d71\u6cbb\uff08\u518d\u5e30\uff09\u306b\u57fa\u3065\u3044\u3066\u5b9f\u88c5\u3057\u307e\u3059\u3002

        Question

        \u9577\u3055 \\(n\\) \u306e\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u914d\u5217 nums \u304c\u4e0e\u3048\u3089\u308c\u3001\u3059\u3079\u3066\u306e\u8981\u7d20\u304c\u4e00\u610f\u3067\u3042\u308b\u5834\u5408\u3001\u8981\u7d20 target \u3092\u898b\u3064\u3051\u3066\u304f\u3060\u3055\u3044\u3002

        \u5206\u5272\u7d71\u6cbb\u306e\u89b3\u70b9\u304b\u3089\u3001\u691c\u7d22\u533a\u9593 \\([i, j]\\) \u306b\u5bfe\u5fdc\u3059\u308b\u90e8\u5206\u554f\u984c\u3092 \\(f(i, j)\\) \u3068\u8868\u3057\u307e\u3059\u3002

        \u5143\u306e\u554f\u984c \\(f(0, n-1)\\) \u304b\u3089\u958b\u59cb\u3057\u3066\u3001\u4ee5\u4e0b\u306e\u30b9\u30c6\u30c3\u30d7\u3067\u4e8c\u5206\u63a2\u7d22\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002

        1. \u691c\u7d22\u533a\u9593 \\([i, j]\\) \u306e\u4e2d\u70b9 \\(m\\) \u3092\u8a08\u7b97\u3057\u3001\u305d\u308c\u3092\u4f7f\u7528\u3057\u3066\u691c\u7d22\u533a\u9593\u306e\u534a\u5206\u3092\u9664\u53bb\u3057\u307e\u3059\u3002
        2. \u534a\u5206\u306e\u30b5\u30a4\u30ba\u306b\u7e2e\u5c0f\u3055\u308c\u305f\u90e8\u5206\u554f\u984c\u3092\u518d\u5e30\u7684\u306b\u89e3\u6c7a\u3057\u307e\u3059\u3002\u3053\u308c\u306f \\(f(i, m-1)\\) \u307e\u305f\u306f \\(f(m+1, j)\\) \u306b\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        3. target \u304c\u898b\u3064\u304b\u308b\u304b\u533a\u9593\u304c\u7a7a\u306b\u306a\u3063\u3066\u30ea\u30bf\u30fc\u30f3\u3059\u308b\u307e\u3067\u3001\u30b9\u30c6\u30c3\u30d7 1. \u3068 2. \u3092\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306f\u3001\u914d\u5217\u5185\u3067\u8981\u7d20 \\(6\\) \u3092\u63a2\u3059\u4e8c\u5206\u63a2\u7d22\u306e\u5206\u5272\u7d71\u6cbb\u904e\u7a0b\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 12-4 \u00a0 \u4e8c\u5206\u63a2\u7d22\u306e\u5206\u5272\u7d71\u6cbb\u904e\u7a0b

        \u5b9f\u88c5\u30b3\u30fc\u30c9\u3067\u306f\u3001\u554f\u984c \\(f(i, j)\\) \u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u518d\u5e30\u95a2\u6570 dfs() \u3092\u5ba3\u8a00\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_recur.py
        def dfs(nums: list[int], target: int, i: int, j: int) -> int:\n    \"\"\"\u4e8c\u5206\u63a2\u7d22\uff1a\u554f\u984c f(i, j)\"\"\"\n    # \u533a\u9593\u304c\u7a7a\u306e\u5834\u5408\u3001\u5bfe\u8c61\u8981\u7d20\u304c\u306a\u3044\u3053\u3068\u3092\u793a\u3059\u305f\u3081\u3001-1 \u3092\u8fd4\u3059\n    if i > j:\n        return -1\n    # \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 m \u3092\u8a08\u7b97\n    m = (i + j) // 2\n    if nums[m] < target:\n        # \u518d\u5e30\u90e8\u5206\u554f\u984c f(m+1, j)\n        return dfs(nums, target, m + 1, j)\n    elif nums[m] > target:\n        # \u518d\u5e30\u90e8\u5206\u554f\u984c f(i, m-1)\n        return dfs(nums, target, i, m - 1)\n    else:\n        # \u5bfe\u8c61\u8981\u7d20\u3092\u767a\u898b\u3057\u305f\u305f\u3081\u3001\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n        return m\n\ndef binary_search(nums: list[int], target: int) -> int:\n    \"\"\"\u4e8c\u5206\u63a2\u7d22\"\"\"\n    n = len(nums)\n    # \u554f\u984c f(0, n-1) \u3092\u89e3\u304f\n    return dfs(nums, target, 0, n - 1)\n
        binary_search_recur.cpp
        /* \u4e8c\u5206\u63a2\u7d22\uff1a\u554f\u984c f(i, j) */\nint dfs(vector<int> &nums, int target, int i, int j) {\n    // \u533a\u9593\u304c\u7a7a\u306e\u5834\u5408\u3001\u5bfe\u8c61\u8981\u7d20\u304c\u5b58\u5728\u3057\u306a\u3044\u3053\u3068\u3092\u793a\u3059\u305f\u3081\u3001-1 \u3092\u8fd4\u3059\n    if (i > j) {\n        return -1;\n    }\n    // \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 m \u3092\u8a08\u7b97\n    int m = i + (j - i) / 2;\n    if (nums[m] < target) {\n        // \u518d\u5e30\u7684\u306a\u90e8\u5206\u554f\u984c f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // \u518d\u5e30\u7684\u306a\u90e8\u5206\u554f\u984c f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // \u5bfe\u8c61\u8981\u7d20\u304c\u898b\u3064\u304b\u3063\u305f\u305f\u3081\u3001\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n        return m;\n    }\n}\n\n/* \u4e8c\u5206\u63a2\u7d22 */\nint binarySearch(vector<int> &nums, int target) {\n    int n = nums.size();\n    // \u554f\u984c f(0, n-1) \u3092\u89e3\u304f\n    return dfs(nums, target, 0, n - 1);\n}\n
        binary_search_recur.java
        /* \u4e8c\u5206\u63a2\u7d22\uff1a\u554f\u984c f(i, j) */\nint dfs(int[] nums, int target, int i, int j) {\n    // \u533a\u9593\u304c\u7a7a\u306e\u5834\u5408\u3001\u5bfe\u8c61\u8981\u7d20\u304c\u5b58\u5728\u3057\u306a\u3044\u3053\u3068\u3092\u793a\u3059\u305f\u3081\u3001-1 \u3092\u8fd4\u3059\n    if (i > j) {\n        return -1;\n    }\n    // \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 m \u3092\u8a08\u7b97\n    int m = i + (j - i) / 2;\n    if (nums[m] < target) {\n        // \u518d\u5e30\u7684\u306a\u90e8\u5206\u554f\u984c f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // \u518d\u5e30\u7684\u306a\u90e8\u5206\u554f\u984c f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // \u5bfe\u8c61\u8981\u7d20\u304c\u898b\u3064\u304b\u3063\u305f\u305f\u3081\u3001\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n        return m;\n    }\n}\n\n/* \u4e8c\u5206\u63a2\u7d22 */\nint binarySearch(int[] nums, int target) {\n    int n = nums.length;\n    // \u554f\u984c f(0, n-1) \u3092\u89e3\u304f\n    return dfs(nums, target, 0, n - 1);\n}\n
        binary_search_recur.cs
        [class]{binary_search_recur}-[func]{DFS}\n\n[class]{binary_search_recur}-[func]{BinarySearch}\n
        binary_search_recur.go
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{binarySearch}\n
        binary_search_recur.swift
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{binarySearch}\n
        binary_search_recur.js
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{binarySearch}\n
        binary_search_recur.ts
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{binarySearch}\n
        binary_search_recur.dart
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{binarySearch}\n
        binary_search_recur.rs
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{binary_search}\n
        binary_search_recur.c
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{binarySearch}\n
        binary_search_recur.kt
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{binarySearch}\n
        binary_search_recur.rb
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{binary_search}\n
        "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/","title":"12.3 \u00a0 \u4e8c\u5206\u6728\u69cb\u7bc9\u554f\u984c","text":"

        Question

        \u4e8c\u5206\u6728\u306e\u524d\u9806\u8d70\u67fb preorder \u30b7\u30fc\u30b1\u30f3\u30b9\u3068\u4e2d\u9806\u8d70\u67fb inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u4e8c\u5206\u6728\u3092\u69cb\u7bc9\u3057\u3066\u305d\u306e\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4e8c\u5206\u6728\u306b\u91cd\u8907\u3059\u308b\u30ce\u30fc\u30c9\u5024\u304c\u306a\u3044\u3068\u4eee\u5b9a\u3057\u307e\u3059\uff08\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\uff09\u3002

        \u56f3 12-5 \u00a0 \u4e8c\u5206\u6728\u69cb\u7bc9\u306e\u30b5\u30f3\u30d7\u30eb\u30c7\u30fc\u30bf

        "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#1","title":"1. \u00a0 \u5206\u5272\u7d71\u6cbb\u554f\u984c\u304b\u3069\u3046\u304b\u306e\u5224\u5b9a","text":"

        preorder \u3068 inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u304b\u3089\u4e8c\u5206\u6728\u3092\u69cb\u7bc9\u3059\u308b\u5143\u306e\u554f\u984c\u306f\u3001\u5178\u578b\u7684\u306a\u5206\u5272\u7d71\u6cbb\u554f\u984c\u3067\u3059\u3002

        • \u554f\u984c\u3092\u5206\u89e3\u3067\u304d\u308b\uff1a\u5206\u5272\u7d71\u6cbb\u306e\u89b3\u70b9\u304b\u3089\u3001\u5143\u306e\u554f\u984c\u30922\u3064\u306e\u90e8\u5206\u554f\u984c\uff08\u5de6\u306e\u90e8\u5206\u6728\u306e\u69cb\u7bc9\u3068\u53f3\u306e\u90e8\u5206\u6728\u306e\u69cb\u7bc9\uff09\u3068\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\u3068\u3044\u30461\u3064\u306e\u64cd\u4f5c\u306b\u5206\u5272\u3067\u304d\u307e\u3059\u3002\u5404\u90e8\u5206\u6728\uff08\u90e8\u5206\u554f\u984c\uff09\u306b\u3064\u3044\u3066\u3001\u540c\u3058\u30a2\u30d7\u30ed\u30fc\u30c1\u3092\u7d99\u7d9a\u7684\u306b\u9069\u7528\u3057\u3001\u3088\u308a\u5c0f\u3055\u306a\u90e8\u5206\u6728\uff08\u90e8\u5206\u554f\u984c\uff09\u306b\u5206\u5272\u3057\u3001\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\uff08\u7a7a\u306e\u90e8\u5206\u6728\uff09\u306b\u5230\u9054\u3059\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002
        • \u90e8\u5206\u554f\u984c\u306f\u72ec\u7acb\u3057\u3066\u3044\u308b\uff1a\u5de6\u3068\u53f3\u306e\u90e8\u5206\u6728\u306f\u91cd\u8907\u3057\u307e\u305b\u3093\u3002\u5de6\u306e\u90e8\u5206\u6728\u3092\u69cb\u7bc9\u3059\u308b\u969b\u3001\u5de6\u306e\u90e8\u5206\u6728\u306b\u5bfe\u5fdc\u3059\u308b\u4e2d\u9806\u8d70\u67fb\u3068\u524d\u9806\u8d70\u67fb\u306e\u30bb\u30b0\u30e1\u30f3\u30c8\u306e\u307f\u304c\u5fc5\u8981\u3067\u3059\u3002\u53f3\u306e\u90e8\u5206\u6728\u306b\u3082\u540c\u3058\u30a2\u30d7\u30ed\u30fc\u30c1\u304c\u9069\u7528\u3055\u308c\u307e\u3059\u3002
        • \u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u308b\uff1a\u5de6\u3068\u53f3\u306e\u90e8\u5206\u6728\uff08\u90e8\u5206\u554f\u984c\u306e\u89e3\uff09\u3092\u69cb\u7bc9\u3057\u305f\u3089\u3001\u305d\u308c\u3089\u3092\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u306b\u63a5\u7d9a\u3057\u3066\u5143\u306e\u554f\u984c\u306e\u89e3\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002
        "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#2","title":"2. \u00a0 \u90e8\u5206\u6728\u306e\u5206\u5272\u65b9\u6cd5","text":"

        \u4e0a\u8a18\u306e\u5206\u6790\u306b\u57fa\u3065\u3044\u3066\u3001\u3053\u306e\u554f\u984c\u306f\u5206\u5272\u7d71\u6cbb\u3092\u4f7f\u7528\u3057\u3066\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u524d\u9806\u8d70\u67fb preorder \u30b7\u30fc\u30b1\u30f3\u30b9\u3068\u4e2d\u9806\u8d70\u67fb inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u4f7f\u7528\u3057\u3066\u5de6\u3068\u53f3\u306e\u90e8\u5206\u6728\u3092\u3069\u306e\u3088\u3046\u306b\u5206\u5272\u3059\u308c\u3070\u3088\u3044\u3067\u3057\u3087\u3046\u304b\uff1f

        \u5b9a\u7fa9\u306b\u3088\u308a\u3001preorder \u3068 inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u306e\u4e21\u65b9\u30923\u3064\u306e\u90e8\u5206\u306b\u5206\u5272\u3067\u304d\u307e\u3059\uff1a

        • \u524d\u9806\u8d70\u67fb\uff1a[ \u30eb\u30fc\u30c8 | \u5de6\u306e\u90e8\u5206\u6728 | \u53f3\u306e\u90e8\u5206\u6728 ]\u3002\u4f8b\u3048\u3070\u3001\u56f3\u3067\u306f\u3001\u6728\u306f [ 3 | 9 | 2 1 7 ] \u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002
        • \u4e2d\u9806\u8d70\u67fb\uff1a[ \u5de6\u306e\u90e8\u5206\u6728 | \u30eb\u30fc\u30c8 | \u53f3\u306e\u90e8\u5206\u6728 ]\u3002\u4f8b\u3048\u3070\u3001\u56f3\u3067\u306f\u3001\u6728\u306f [ 9 | 3 | 1 2 7 ] \u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002

        \u524d\u306e\u56f3\u306e\u30c7\u30fc\u30bf\u3092\u4f7f\u7528\u3057\u3066\u3001\u6b21\u306e\u56f3\u306b\u793a\u3059\u30b9\u30c6\u30c3\u30d7\u306b\u5f93\u3063\u3066\u5206\u5272\u7d50\u679c\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\uff1a

        1. \u524d\u9806\u8d70\u67fb\u306e\u6700\u521d\u306e\u8981\u7d203\u304c\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u306e\u5024\u3067\u3059\u3002
        2. inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u5185\u3067\u30eb\u30fc\u30c8\u30ce\u30fc\u30c93\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u898b\u3064\u3051\u3001\u3053\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u4f7f\u7528\u3057\u3066 inorder \u3092 [ 9 | 3 \uff5c 1 2 7 ] \u306b\u5206\u5272\u3057\u307e\u3059\u3002
        3. inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u306e\u5206\u5272\u306b\u5f93\u3063\u3066\u3001\u5de6\u3068\u53f3\u306e\u90e8\u5206\u6728\u304c\u305d\u308c\u305e\u308c1\u500b\u30683\u500b\u306e\u30ce\u30fc\u30c9\u3092\u542b\u3080\u3053\u3068\u304c\u7c21\u5358\u306b\u6c7a\u5b9a\u3067\u304d\u308b\u305f\u3081\u3001preorder \u30b7\u30fc\u30b1\u30f3\u30b9\u3092 [ 3 | 9 | 2 1 7 ] \u306b\u5bfe\u5fdc\u3057\u3066\u5206\u5272\u3067\u304d\u307e\u3059\u3002

        \u56f3 12-6 \u00a0 \u524d\u9806\u8d70\u67fb\u3068\u4e2d\u9806\u8d70\u67fb\u3067\u306e\u90e8\u5206\u6728\u306e\u5206\u5272

        "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#3","title":"3. \u00a0 \u5909\u6570\u306b\u57fa\u3065\u304f\u90e8\u5206\u6728\u7bc4\u56f2\u306e\u8a18\u8ff0","text":"

        \u4e0a\u8a18\u306e\u5206\u5272\u65b9\u6cd5\u306b\u57fa\u3065\u3044\u3066\u3001preorder \u3068 inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u306b\u304a\u3051\u308b\u30eb\u30fc\u30c8\u3001\u5de6\u306e\u90e8\u5206\u6728\u3001\u53f3\u306e\u90e8\u5206\u6728\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2\u3092\u53d6\u5f97\u3057\u307e\u3057\u305f\u3002\u3053\u308c\u3089\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2\u3092\u8a18\u8ff0\u3059\u308b\u305f\u3081\u306b\u3001\u3044\u304f\u3064\u304b\u306e\u30dd\u30a4\u30f3\u30bf\u5909\u6570\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        • \u73fe\u5728\u306e\u6728\u306e\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u306e preorder \u30b7\u30fc\u30b1\u30f3\u30b9\u3067\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092 \\(i\\) \u3068\u3057\u307e\u3059\u3002
        • \u73fe\u5728\u306e\u6728\u306e\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u306e inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u3067\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092 \\(m\\) \u3068\u3057\u307e\u3059\u3002
        • \u73fe\u5728\u306e\u6728\u306e inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u3067\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2\u3092 \\([l, r]\\) \u3068\u3057\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u8868\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u3053\u308c\u3089\u306e\u5909\u6570\u306f preorder \u30b7\u30fc\u30b1\u30f3\u30b9\u3067\u306e\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3068 inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u3067\u306e\u90e8\u5206\u6728\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2\u3092\u8868\u3057\u307e\u3059\u3002

        \u8868 12-1 \u00a0 \u524d\u9806\u8d70\u67fb\u3068\u4e2d\u9806\u8d70\u67fb\u3067\u306e\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3068\u90e8\u5206\u6728\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9

        preorder \u3067\u306e\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 inorder \u3067\u306e\u90e8\u5206\u6728\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2 \u73fe\u5728\u306e\u6728 \\(i\\) \\([l, r]\\) \u5de6\u306e\u90e8\u5206\u6728 \\(i + 1\\) \\([l, m-1]\\) \u53f3\u306e\u90e8\u5206\u6728 \\(i + 1 + (m - l)\\) \\([m+1, r]\\)

        \u53f3\u306e\u90e8\u5206\u6728\u306e\u30eb\u30fc\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e \\((m-l)\\) \u306f\u300c\u5de6\u306e\u90e8\u5206\u6728\u306e\u30ce\u30fc\u30c9\u6570\u300d\u3092\u8868\u3059\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3088\u308a\u660e\u78ba\u306a\u7406\u89e3\u306e\u305f\u3081\u306b\u3001\u4ee5\u4e0b\u306e\u56f3\u3092\u53c2\u7167\u3059\u308b\u3053\u3068\u304c\u5f79\u7acb\u3064\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002

        \u56f3 12-7 \u00a0 \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3068\u5de6\u53f3\u306e\u90e8\u5206\u6728\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9

        "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#4","title":"4. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        \\(m\\) \u306e\u554f\u3044\u5408\u308f\u305b\u306e\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u308b\u305f\u3081\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb hmap \u3092\u4f7f\u7528\u3057\u3066 inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u306e\u8981\u7d20\u304b\u3089\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3078\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u3092\u683c\u7d0d\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby build_tree.py
        def dfs(\n    preorder: list[int],\n    inorder_map: dict[int, int],\n    i: int,\n    l: int,\n    r: int,\n) -> TreeNode | None:\n    \"\"\"\u4e8c\u5206\u6728\u306e\u69cb\u7bc9\uff1a\u5206\u5272\u7d71\u6cbb\"\"\"\n    # \u90e8\u5206\u6728\u306e\u533a\u9593\u304c\u7a7a\u306e\u3068\u304d\u7d42\u4e86\n    if r - l < 0:\n        return None\n    # \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\n    root = TreeNode(preorder[i])\n    # m \u3092\u30af\u30a8\u30ea\u3057\u3066\u5de6\u90e8\u5206\u6728\u3068\u53f3\u90e8\u5206\u6728\u3092\u5206\u5272\n    m = inorder_map[preorder[i]]\n    # \u90e8\u5206\u554f\u984c\uff1a\u5de6\u90e8\u5206\u6728\u3092\u69cb\u7bc9\n    root.left = dfs(preorder, inorder_map, i + 1, l, m - 1)\n    # \u90e8\u5206\u554f\u984c\uff1a\u53f3\u90e8\u5206\u6728\u3092\u69cb\u7bc9\n    root.right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r)\n    # \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u8fd4\u3059\n    return root\n\ndef build_tree(preorder: list[int], inorder: list[int]) -> TreeNode | None:\n    \"\"\"\u4e8c\u5206\u6728\u3092\u69cb\u7bc9\"\"\"\n    # \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3001\u4e2d\u9806\u8d70\u67fb\u306e\u8981\u7d20\u304b\u3089\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3078\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u3092\u4fdd\u5b58\n    inorder_map = {val: i for i, val in enumerate(inorder)}\n    root = dfs(preorder, inorder_map, 0, 0, len(inorder) - 1)\n    return root\n
        build_tree.cpp
        /* \u4e8c\u5206\u6728\u306e\u69cb\u7bc9\uff1a\u5206\u5272\u7d71\u6cbb */\nTreeNode *dfs(vector<int> &preorder, unordered_map<int, int> &inorderMap, int i, int l, int r) {\n    // \u90e8\u5206\u6728\u306e\u533a\u9593\u304c\u7a7a\u306e\u5834\u5408\u306b\u7d42\u4e86\n    if (r - l < 0)\n        return NULL;\n    // \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\n    TreeNode *root = new TreeNode(preorder[i]);\n    // m \u3092\u554f\u3044\u5408\u308f\u305b\u3066\u5de6\u53f3\u306e\u90e8\u5206\u6728\u3092\u5206\u5272\n    int m = inorderMap[preorder[i]];\n    // \u90e8\u5206\u554f\u984c\uff1a\u5de6\u306e\u90e8\u5206\u6728\u3092\u69cb\u7bc9\n    root->left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // \u90e8\u5206\u554f\u984c\uff1a\u53f3\u306e\u90e8\u5206\u6728\u3092\u69cb\u7bc9\n    root->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u8fd4\u3059\n    return root;\n}\n\n/* \u4e8c\u5206\u6728\u306e\u69cb\u7bc9 */\nTreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {\n    // \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3057\u3001\u4e2d\u9593\u9806\u5e8f\u306e\u8981\u7d20\u304b\u3089\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3078\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u3092\u683c\u7d0d\n    unordered_map<int, int> inorderMap;\n    for (int i = 0; i < inorder.size(); i++) {\n        inorderMap[inorder[i]] = i;\n    }\n    TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorder.size() - 1);\n    return root;\n}\n
        build_tree.java
        /* \u4e8c\u5206\u6728\u306e\u69cb\u7bc9\uff1a\u5206\u5272\u7d71\u6cbb */\nTreeNode dfs(int[] preorder, Map<Integer, Integer> inorderMap, int i, int l, int r) {\n    // \u90e8\u5206\u6728\u306e\u533a\u9593\u304c\u7a7a\u306e\u5834\u5408\u306b\u7d42\u4e86\n    if (r - l < 0)\n        return null;\n    // \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\n    TreeNode root = new TreeNode(preorder[i]);\n    // m \u3092\u554f\u3044\u5408\u308f\u305b\u3066\u5de6\u53f3\u306e\u90e8\u5206\u6728\u3092\u5206\u5272\n    int m = inorderMap.get(preorder[i]);\n    // \u90e8\u5206\u554f\u984c\uff1a\u5de6\u306e\u90e8\u5206\u6728\u3092\u69cb\u7bc9\n    root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // \u90e8\u5206\u554f\u984c\uff1a\u53f3\u306e\u90e8\u5206\u6728\u3092\u69cb\u7bc9\n    root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u8fd4\u3059\n    return root;\n}\n\n/* \u4e8c\u5206\u6728\u306e\u69cb\u7bc9 */\nTreeNode buildTree(int[] preorder, int[] inorder) {\n    // \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3057\u3001\u4e2d\u9593\u9806\u5e8f\u306e\u8981\u7d20\u304b\u3089\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3078\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u3092\u683c\u7d0d\n    Map<Integer, Integer> inorderMap = new HashMap<>();\n    for (int i = 0; i < inorder.length; i++) {\n        inorderMap.put(inorder[i], i);\n    }\n    TreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n    return root;\n}\n
        build_tree.cs
        [class]{build_tree}-[func]{DFS}\n\n[class]{build_tree}-[func]{BuildTree}\n
        build_tree.go
        [class]{}-[func]{dfsBuildTree}\n\n[class]{}-[func]{buildTree}\n
        build_tree.swift
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{buildTree}\n
        build_tree.js
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{buildTree}\n
        build_tree.ts
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{buildTree}\n
        build_tree.dart
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{buildTree}\n
        build_tree.rs
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{build_tree}\n
        build_tree.c
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{buildTree}\n
        build_tree.kt
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{buildTree}\n
        build_tree.rb
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{build_tree}\n

        \u4ee5\u4e0b\u306e\u56f3\u306f\u3001\u4e8c\u5206\u6728\u3092\u69cb\u7bc9\u3059\u308b\u518d\u5e30\u904e\u7a0b\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u5404\u30ce\u30fc\u30c9\u306f\u518d\u5e30\u306e\u300c\u4e0b\u964d\u300d\u6bb5\u968e\u3067\u4f5c\u6210\u3055\u308c\u3001\u5404\u30a8\u30c3\u30b8\uff08\u53c2\u7167\uff09\u306f\u300c\u4e0a\u6607\u300d\u6bb5\u968e\u3067\u5f62\u6210\u3055\u308c\u307e\u3059\u3002

        <1><2><3><4><5><6><7><8><9>

        \u56f3 12-8 \u00a0 \u4e8c\u5206\u6728\u69cb\u7bc9\u306e\u518d\u5e30\u904e\u7a0b

        \u5404\u518d\u5e30\u95a2\u6570\u306e preorder \u3068 inorder \u30b7\u30fc\u30b1\u30f3\u30b9\u306e\u5206\u5272\u306f\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        \u56f3 12-9 \u00a0 \u5404\u518d\u5e30\u95a2\u6570\u3067\u306e\u5206\u5272

        \u4e8c\u5206\u6728\u304c \\(n\\) \u500b\u306e\u30ce\u30fc\u30c9\u3092\u6301\u3064\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u5404\u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\uff08\u518d\u5e30\u95a2\u6570 dfs() \u306e\u547c\u3073\u51fa\u3057\uff09\u306b\u306f \\(O(1)\\) \u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u5168\u4f53\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u3067\u3059\u3002

        \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306f inorder \u8981\u7d20\u304b\u3089\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3078\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u3001\\(O(n)\\) \u30b9\u30da\u30fc\u30b9\u304c\u5fc5\u8981\u3067\u3059\u3002\u6700\u60aa\u306e\u5834\u5408\u3001\u4e8c\u5206\u6728\u304c\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u9000\u5316\u3059\u308b\u3068\u3001\u518d\u5e30\u306e\u6df1\u3055\u306f \\(n\\) \u306b\u9054\u3057\u3001\\(O(n)\\) \u306e\u30b9\u30bf\u30c3\u30af\u30b9\u30da\u30fc\u30b9\u3092\u6d88\u8cbb\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u5168\u4f53\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u3067\u3059\u3002

        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/","title":"12.1 \u00a0 \u5206\u5272\u7d71\u6cbb\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0","text":"

        \u5206\u5272\u7d71\u6cbb\u306f\u91cd\u8981\u3067\u4eba\u6c17\u306e\u3042\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u6226\u7565\u3067\u3059\u3002\u540d\u524d\u304c\u793a\u3059\u3088\u3046\u306b\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u901a\u5e38\u518d\u5e30\u7684\u306b\u5b9f\u88c5\u3055\u308c\u3001\u300c\u5206\u5272\u300d\u3068\u300c\u7d71\u6cbb\u300d\u306e2\u3064\u306e\u30b9\u30c6\u30c3\u30d7\u304b\u3089\u69cb\u6210\u3055\u308c\u307e\u3059\u3002

        1. \u5206\u5272\uff08\u5206\u5272\u6bb5\u968e\uff09\uff1a\u5143\u306e\u554f\u984c\u3092\u518d\u5e30\u7684\u306b2\u3064\u4ee5\u4e0a\u306e\u5c0f\u3055\u306a\u90e8\u5206\u554f\u984c\u306b\u5206\u89e3\u3057\u3001\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306b\u5230\u9054\u3059\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002
        2. \u7d71\u6cbb\uff08\u30de\u30fc\u30b8\u6bb5\u968e\uff09\uff1a\u89e3\u6c7a\u65b9\u6cd5\u304c\u65e2\u77e5\u306e\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u304b\u3089\u958b\u59cb\u3057\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u30dc\u30c8\u30e0\u30a2\u30c3\u30d7\u65b9\u5f0f\u3067\u30de\u30fc\u30b8\u3057\u3066\u5143\u306e\u554f\u984c\u306e\u89e3\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u300c\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u300d\u306f\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306e\u5178\u578b\u7684\u306a\u5fdc\u7528\u306e\u4e00\u3064\u3067\u3059\u3002

        1. \u5206\u5272\uff1a\u5143\u306e\u914d\u5217\uff08\u5143\u306e\u554f\u984c\uff09\u3092\u518d\u5e30\u7684\u306b2\u3064\u306e\u526f\u914d\u5217\uff08\u90e8\u5206\u554f\u984c\uff09\u306b\u5206\u5272\u3057\u3001\u526f\u914d\u5217\u304c1\u3064\u306e\u8981\u7d20\u306e\u307f\u306b\u306a\u308b\u307e\u3067\uff08\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\uff09\u7d9a\u3051\u307e\u3059\u3002
        2. \u7d71\u6cbb\uff1a\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u526f\u914d\u5217\uff08\u90e8\u5206\u554f\u984c\u306e\u89e3\uff09\u3092\u30dc\u30c8\u30e0\u30a2\u30c3\u30d7\u3067\u30de\u30fc\u30b8\u3057\u3066\u3001\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u5143\u306e\u914d\u5217\uff08\u5143\u306e\u554f\u984c\u306e\u89e3\uff09\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002

        \u56f3 12-1 \u00a0 \u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u306e\u5206\u5272\u7d71\u6cbb\u6226\u7565

        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1211","title":"12.1.1 \u00a0 \u5206\u5272\u7d71\u6cbb\u554f\u984c\u3092\u7279\u5b9a\u3059\u308b\u65b9\u6cd5","text":"

        \u554f\u984c\u304c\u5206\u5272\u7d71\u6cbb\u89e3\u6c7a\u306b\u9069\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u306f\u3001\u901a\u5e38\u4ee5\u4e0b\u306e\u57fa\u6e96\u306b\u57fa\u3065\u3044\u3066\u6c7a\u5b9a\u3067\u304d\u307e\u3059\u3002

        1. \u554f\u984c\u3092\u3088\u308a\u5c0f\u3055\u306a\u3082\u306e\u306b\u5206\u89e3\u3067\u304d\u308b\uff1a\u5143\u306e\u554f\u984c\u3092\u3088\u308a\u5c0f\u3055\u304f\u985e\u4f3c\u3057\u305f\u90e8\u5206\u554f\u984c\u306b\u5206\u5272\u3067\u304d\u3001\u305d\u306e\u3088\u3046\u306a\u904e\u7a0b\u3092\u540c\u3058\u65b9\u6cd5\u3067\u518d\u5e30\u7684\u306b\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002
        2. \u90e8\u5206\u554f\u984c\u306f\u72ec\u7acb\u3057\u3066\u3044\u308b\uff1a\u90e8\u5206\u554f\u984c\u9593\u306b\u91cd\u8907\u304c\u306a\u304f\u3001\u72ec\u7acb\u3057\u3066\u304a\u308a\u3001\u500b\u5225\u306b\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002
        3. \u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u30de\u30fc\u30b8\u3067\u304d\u308b\uff1a\u5143\u306e\u554f\u984c\u306e\u89e3\u306f\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u5c0e\u51fa\u3055\u308c\u307e\u3059\u3002

        \u660e\u3089\u304b\u306b\u3001\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u306f\u3053\u308c\u30893\u3064\u306e\u57fa\u6e96\u3092\u6e80\u305f\u3057\u3066\u3044\u307e\u3059\u3002

        1. \u554f\u984c\u3092\u3088\u308a\u5c0f\u3055\u306a\u3082\u306e\u306b\u5206\u89e3\u3067\u304d\u308b\uff1a\u914d\u5217\uff08\u5143\u306e\u554f\u984c\uff09\u3092\u518d\u5e30\u7684\u306b2\u3064\u306e\u526f\u914d\u5217\uff08\u90e8\u5206\u554f\u984c\uff09\u306b\u5206\u5272\u3057\u307e\u3059\u3002
        2. \u90e8\u5206\u554f\u984c\u306f\u72ec\u7acb\u3057\u3066\u3044\u308b\uff1a\u5404\u526f\u914d\u5217\u306f\u72ec\u7acb\u3057\u3066\u30bd\u30fc\u30c8\u3067\u304d\u307e\u3059\uff08\u90e8\u5206\u554f\u984c\u306f\u72ec\u7acb\u3057\u3066\u89e3\u6c7a\u3067\u304d\u307e\u3059\uff09\u3002
        3. \u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u30de\u30fc\u30b8\u3067\u304d\u308b\uff1a2\u3064\u306e\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u526f\u914d\u5217\uff08\u90e8\u5206\u554f\u984c\u306e\u89e3\uff09\u30921\u3064\u306e\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u914d\u5217\uff08\u5143\u306e\u554f\u984c\u306e\u89e3\uff09\u306b\u30de\u30fc\u30b8\u3067\u304d\u307e\u3059\u3002
        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1212","title":"12.1.2 \u00a0 \u5206\u5272\u7d71\u6cbb\u306b\u3088\u308b\u52b9\u7387\u306e\u5411\u4e0a","text":"

        \u5206\u5272\u7d71\u6cbb\u6226\u7565\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u3092\u52b9\u679c\u7684\u306b\u89e3\u6c7a\u3059\u308b\u3060\u3051\u3067\u306a\u304f\u3001\u3057\u3070\u3057\u3070\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u306f\u3001\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u3001\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u3001\u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\u306f\u3001\u5206\u5272\u7d71\u6cbb\u6226\u7565\u3092\u9069\u7528\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u9078\u629e\u30bd\u30fc\u30c8\u3001\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u3001\u633f\u5165\u30bd\u30fc\u30c8\u3088\u308a\u3082\u9ad8\u901f\u3067\u3059\u3002

        \u79c1\u305f\u3061\u306e\u5fc3\u306b\u306f\u7591\u554f\u304c\u3042\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\uff1a\u306a\u305c\u5206\u5272\u7d71\u6cbb\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u305d\u306e\u6839\u672c\u7684\u306a\u8ad6\u7406\u306f\u4f55\u3067\u3059\u304b\uff1f \u3064\u307e\u308a\u3001\u554f\u984c\u3092\u90e8\u5206\u554f\u984c\u306b\u5206\u89e3\u3057\u3001\u305d\u308c\u3089\u3092\u89e3\u6c7a\u3057\u3001\u305d\u308c\u3089\u306e\u89e3\u3092\u7d44\u307f\u5408\u308f\u305b\u3066\u5143\u306e\u554f\u984c\u306b\u5bfe\u51e6\u3059\u308b\u3053\u3068\u304c\u3001\u5143\u306e\u554f\u984c\u3092\u76f4\u63a5\u89e3\u6c7a\u3059\u308b\u3088\u308a\u3082\u52b9\u7387\u7684\u3067\u3042\u308b\u7406\u7531\u306f\u4f55\u3067\u3059\u304b\uff1f\u3053\u306e\u8cea\u554f\u306f2\u3064\u306e\u5074\u9762\u304b\u3089\u5206\u6790\u3067\u304d\u307e\u3059\uff1a\u64cd\u4f5c\u6570\u3068\u4e26\u5217\u8a08\u7b97\u3002

        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1","title":"1. \u00a0 \u64cd\u4f5c\u6570\u306e\u6700\u9069\u5316","text":"

        \u300c\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u300d\u3092\u4f8b\u306b\u3068\u308b\u3068\u3001\u9577\u3055 \\(n\\) \u306e\u914d\u5217\u3092\u51e6\u7406\u3059\u308b\u306e\u306b \\(O(n^2)\\) \u6642\u9593\u304c\u5fc5\u8981\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u914d\u5217\u3092\u4e2d\u70b9\u304b\u30892\u3064\u306e\u526f\u914d\u5217\u306b\u5206\u5272\u3059\u308b\u3068\u3057\u307e\u3059\u3002\u305d\u306e\u3088\u3046\u306a\u5206\u5272\u306b\u306f \\(O(n)\\) \u6642\u9593\u304c\u5fc5\u8981\u3067\u3059\u3002\u5404\u526f\u914d\u5217\u306e\u30bd\u30fc\u30c8\u306b\u306f \\(O((n / 2)^2)\\) \u6642\u9593\u304c\u5fc5\u8981\u3067\u3059\u3002\u305d\u3057\u30662\u3064\u306e\u526f\u914d\u5217\u306e\u30de\u30fc\u30b8\u306b\u306f \\(O(n)\\) \u6642\u9593\u304c\u5fc5\u8981\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u5168\u4f53\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\uff1a

        \\[ O(n + (\\frac{n}{2})^2 \\times 2 + n) = O(\\frac{n^2}{2} + 2n) \\]

        \u56f3 12-2 \u00a0 \u914d\u5217\u5206\u5272\u524d\u5f8c\u306e\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8

        \u4ee5\u4e0b\u306e\u4e0d\u7b49\u5f0f\u3092\u8a08\u7b97\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u5de6\u5074\u306f\u5206\u5272\u524d\u306e\u7dcf\u64cd\u4f5c\u6570\u3092\u8868\u3057\u3001\u53f3\u5074\u306f\u5206\u5272\u5f8c\u306e\u7dcf\u64cd\u4f5c\u6570\u3092\u305d\u308c\u305e\u308c\u8868\u3057\u307e\u3059\uff1a

        \\[ \\begin{aligned} n^2 & > \\frac{n^2}{2} + 2n \\newline n^2 - \\frac{n^2}{2} - 2n & > 0 \\newline n(n - 4) & > 0 \\end{aligned} \\]

        \u3053\u308c\u306f \\(n > 4\\) \u306e\u5834\u5408\u3001\u5206\u5272\u5f8c\u306e\u64cd\u4f5c\u6570\u304c\u5c11\u306a\u304f\u3001\u3088\u308a\u826f\u3044\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306b\u3064\u306a\u304c\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u5206\u5272\u5f8c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\u4f9d\u7136\u3068\u3057\u3066\u4e8c\u6b21 \\(O(n^2)\\) \u3067\u3059\u304c\u3001\u8a08\u7b97\u91cf\u306e\u5b9a\u6570\u4fc2\u6570\u304c\u6e1b\u5c11\u3057\u3066\u3044\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u3055\u3089\u306b\u9032\u3080\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u526f\u914d\u5217\u3092\u305d\u306e\u4e2d\u70b9\u304b\u3089\u3055\u3089\u306b2\u3064\u306e\u526f\u914d\u5217\u306b\u5206\u5272\u3057\u7d9a\u3051\u3066\u3001\u526f\u914d\u5217\u304c1\u3064\u306e\u8981\u7d20\u306e\u307f\u306b\u306a\u308b\u307e\u3067\u7d9a\u3051\u305f\u3089\u3069\u3046\u3067\u3057\u3087\u3046\u304b\uff1f \u3053\u306e\u30a2\u30a4\u30c7\u30a2\u306f\u5b9f\u969b\u306b\u306f\u300c\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u300d\u3067\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n \\log n)\\) \u3067\u3059\u3002

        \u5c11\u3057\u9055\u3046\u3053\u3068\u3092\u8a66\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u30022\u3064\u3067\u306f\u306a\u304f\u3001\u3088\u308a\u591a\u304f\u306e\u5206\u5272\u306b\u5206\u5272\u3057\u305f\u3089\u3069\u3046\u3067\u3057\u3087\u3046\u304b\uff1f \u4f8b\u3048\u3070\u3001\u5143\u306e\u914d\u5217\u3092 \\(k\\) \u500b\u306e\u526f\u914d\u5217\u306b\u5747\u7b49\u306b\u5206\u5272\u3057\u307e\u3059\u304b\uff1f\u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\u300c\u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u300d\u3068\u975e\u5e38\u306b\u4f3c\u3066\u304a\u308a\u3001\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u306e\u30bd\u30fc\u30c8\u306b\u975e\u5e38\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u7406\u8ad6\u7684\u306b\u306f\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n + k)\\) \u306b\u9054\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#2","title":"2. \u00a0 \u4e26\u5217\u8a08\u7b97\u306b\u3088\u308b\u6700\u9069\u5316","text":"

        \u5206\u5272\u7d71\u6cbb\u306b\u3088\u3063\u3066\u751f\u6210\u3055\u308c\u308b\u90e8\u5206\u554f\u984c\u306f\u4e92\u3044\u306b\u72ec\u7acb\u3057\u3066\u3044\u308b\u3053\u3068\u304c\u5206\u304b\u3063\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u305d\u308c\u3089\u3092\u4e26\u5217\u3067\u89e3\u6c7a\u3067\u304d\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002 \u305d\u306e\u7d50\u679c\u3001\u5206\u5272\u7d71\u6cbb\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3092\u6e1b\u3089\u3059\u3060\u3051\u3067\u306a\u304f\u3001\u73fe\u4ee3\u306e\u30aa\u30da\u30ec\u30fc\u30c6\u30a3\u30f3\u30b0\u30b7\u30b9\u30c6\u30e0\u306b\u3088\u308b\u4e26\u5217\u6700\u9069\u5316\u3082\u4fc3\u9032\u3057\u307e\u3059\u3002

        \u4e26\u5217\u6700\u9069\u5316\u306f\u3001\u8907\u6570\u306e\u30b3\u30a2\u3084\u30d7\u30ed\u30bb\u30c3\u30b5\u3092\u6301\u3064\u74b0\u5883\u3067\u7279\u306b\u52b9\u679c\u7684\u3067\u3059\u3002\u30b7\u30b9\u30c6\u30e0\u304c\u8907\u6570\u306e\u90e8\u5206\u554f\u984c\u3092\u540c\u6642\u306b\u51e6\u7406\u3067\u304d\u308b\u305f\u3081\u3001\u8a08\u7b97\u30ea\u30bd\u30fc\u30b9\u3092\u5b8c\u5168\u306b\u6d3b\u7528\u3057\u3001\u5168\u4f53\u7684\u306a\u5b9f\u884c\u6642\u9593\u304c\u5927\u5e45\u306b\u77ed\u7e2e\u3055\u308c\u307e\u3059\u3002

        \u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u300c\u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u300d\u3067\u306f\u3001\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u69d8\u3005\u306a\u30d0\u30b1\u30c3\u30c8\u306b\u5747\u7b49\u306b\u5206\u89e3\u3057\u307e\u3059\u3002\u5404\u30d0\u30b1\u30c3\u30c8\u306e\u30bd\u30fc\u30c8\u4f5c\u696d\u306f\u3001\u5229\u7528\u53ef\u80fd\u306a\u8a08\u7b97\u30e6\u30cb\u30c3\u30c8\u306b\u5272\u308a\u5f53\u3066\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3059\u3079\u3066\u306e\u4f5c\u696d\u304c\u5b8c\u4e86\u3059\u308b\u3068\u3001\u3059\u3079\u3066\u306e\u30bd\u30fc\u30c8\u3055\u308c\u305f\u30d0\u30b1\u30c3\u30c8\u304c\u30de\u30fc\u30b8\u3055\u308c\u3066\u6700\u7d42\u7d50\u679c\u304c\u751f\u6210\u3055\u308c\u307e\u3059\u3002

        \u56f3 12-3 \u00a0 \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306e\u4e26\u5217\u8a08\u7b97

        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1213","title":"12.1.3 \u00a0 \u5206\u5272\u7d71\u6cbb\u306e\u4e00\u822c\u7684\u306a\u5fdc\u7528","text":"

        \u5206\u5272\u7d71\u6cbb\u306f\u591a\u304f\u306e\u53e4\u5178\u7684\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002

        • \u6700\u8fd1\u70b9\u5bfe\u306e\u767a\u898b\uff1a\u3053\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u70b9\u306e\u96c6\u5408\u30922\u3064\u306e\u534a\u5206\u306b\u5206\u5272\u3059\u308b\u3053\u3068\u3067\u52d5\u4f5c\u3057\u307e\u3059\u3002\u305d\u3057\u3066\u5404\u534a\u5206\u3067\u518d\u5e30\u7684\u306b\u6700\u8fd1\u70b9\u5bfe\u3092\u898b\u3064\u3051\u307e\u3059\u3002\u6700\u5f8c\u306b\u30012\u3064\u306e\u534a\u5206\u306b\u307e\u305f\u304c\u308b\u30da\u30a2\u3092\u8003\u616e\u3057\u3066\u3001\u5168\u4f53\u306e\u6700\u8fd1\u70b9\u5bfe\u3092\u898b\u3064\u3051\u307e\u3059\u3002
        • \u5927\u6574\u6570\u306e\u4e57\u7b97\uff1a\u4e00\u3064\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306fKaratsuba\u3068\u547c\u3070\u308c\u307e\u3059\u3002\u5927\u6574\u6570\u306e\u4e57\u7b97\u3092\u3044\u304f\u3064\u304b\u306e\u5c0f\u3055\u306a\u6574\u6570\u306e\u4e57\u7b97\u3068\u52a0\u7b97\u306b\u5206\u89e3\u3057\u307e\u3059\u3002
        • \u884c\u5217\u306e\u4e57\u7b97\uff1a\u4e00\u4f8b\u306fStrassen\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3059\u3002\u5927\u304d\u306a\u884c\u5217\u306e\u4e57\u7b97\u3092\u8907\u6570\u306e\u5c0f\u3055\u306a\u884c\u5217\u306e\u4e57\u7b97\u3068\u52a0\u7b97\u306b\u5206\u89e3\u3057\u307e\u3059\u3002
        • \u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c\uff1a\u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c\u306f\u518d\u5e30\u7684\u306b\u89e3\u6c7a\u3067\u304d\u3001\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306e\u5178\u578b\u7684\u306a\u5fdc\u7528\u3067\u3059\u3002
        • \u8ee2\u5012\u5bfe\u306e\u89e3\u6c7a\uff1a\u30b7\u30fc\u30b1\u30f3\u30b9\u3067\u3001\u524d\u306e\u6570\u304c\u5f8c\u306e\u6570\u3088\u308a\u5927\u304d\u3044\u5834\u5408\u3001\u3053\u308c\u30892\u3064\u306e\u6570\u306f\u8ee2\u5012\u5bfe\u3092\u69cb\u6210\u3057\u307e\u3059\u3002\u8ee2\u5012\u5bfe\u554f\u984c\u306e\u89e3\u6c7a\u306f\u3001\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u306e\u52a9\u3051\u3092\u501f\u308a\u3066\u3001\u5206\u5272\u7d71\u6cbb\u306e\u30a2\u30a4\u30c7\u30a2\u3092\u5229\u7528\u3067\u304d\u307e\u3059\u3002

        \u5206\u5272\u7d71\u6cbb\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u8a2d\u8a08\u306b\u3082\u5e83\u304f\u5fdc\u7528\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        • \u4e8c\u5206\u63a2\u7d22\uff1a\u4e8c\u5206\u63a2\u7d22\u306f\u3001\u30bd\u30fc\u30c8\u6e08\u307f\u914d\u5217\u3092\u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304b\u30892\u3064\u306e\u534a\u5206\u306b\u5206\u5272\u3057\u307e\u3059\u3002\u305d\u3057\u3066\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u5024\u3068\u4e2d\u9593\u8981\u7d20\u5024\u306e\u6bd4\u8f03\u7d50\u679c\u306b\u57fa\u3065\u3044\u3066\u3001\u4e00\u65b9\u306e\u534a\u5206\u304c\u7834\u68c4\u3055\u308c\u307e\u3059\u3002\u540c\u3058\u30d7\u30ed\u30bb\u30b9\u3067\u6b8b\u308a\u306e\u534a\u5206\u3067\u691c\u7d22\u304c\u7d9a\u884c\u3055\u308c\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u304c\u898b\u3064\u304b\u308b\u304b\u6b8b\u308a\u306e\u8981\u7d20\u304c\u306a\u304f\u306a\u308b\u307e\u3067\u7d9a\u304d\u307e\u3059\u3002
        • \u30de\u30fc\u30b8\u30bd\u30fc\u30c8\uff1a\u3053\u306e\u7bc0\u306e\u5192\u982d\u3067\u3059\u3067\u306b\u7d39\u4ecb\u3057\u305f\u305f\u3081\u3001\u3055\u3089\u306a\u308b\u8a73\u8ff0\u306f\u4e0d\u8981\u3067\u3059\u3002
        • \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\uff1a\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306f\u30d4\u30dc\u30c3\u30c8\u5024\u3092\u9078\u629e\u3057\u3066\u914d\u5217\u30922\u3064\u306e\u526f\u914d\u5217\u306b\u5206\u5272\u3057\u3001\u4e00\u65b9\u306f\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5c0f\u3055\u3044\u8981\u7d20\u3001\u3082\u3046\u4e00\u65b9\u306f\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5927\u304d\u3044\u8981\u7d20\u3092\u6301\u3061\u307e\u3059\u3002\u3053\u306e\u30d7\u30ed\u30bb\u30b9\u306f\u3001\u3053\u308c\u30892\u3064\u306e\u526f\u914d\u5217\u306e\u305d\u308c\u305e\u308c\u306b\u5bfe\u3057\u3066\u30011\u3064\u306e\u8981\u7d20\u306e\u307f\u3092\u4fdd\u6301\u3059\u308b\u307e\u3067\u7d9a\u304d\u307e\u3059\u3002
        • \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\uff1a\u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306e\u57fa\u672c\u7684\u306a\u30a2\u30a4\u30c7\u30a2\u306f\u3001\u30c7\u30fc\u30bf\u3092\u8907\u6570\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u6563\u3055\u305b\u308b\u3053\u3068\u3067\u3059\u3002\u5404\u30d0\u30b1\u30c3\u30c8\u5185\u306e\u8981\u7d20\u3092\u30bd\u30fc\u30c8\u3057\u305f\u5f8c\u3001\u30d0\u30b1\u30c3\u30c8\u304b\u3089\u9806\u5e8f\u3088\u304f\u8981\u7d20\u3092\u53d6\u5f97\u3057\u3066\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u914d\u5217\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002
        • \u6728\uff1a\u4f8b\u3048\u3070\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u3001AVL\u6728\u3001\u8d64\u9ed2\u6728\u3001B\u6728\u3001B+\u6728\u306a\u3069\u3002\u305d\u306e\u64cd\u4f5c\uff08\u691c\u7d22\u3001\u633f\u5165\u3001\u524a\u9664\uff09\u306f\u3059\u3079\u3066\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306e\u5fdc\u7528\u3068\u898b\u306a\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
        • \u30d2\u30fc\u30d7\uff1a\u30d2\u30fc\u30d7\u306f\u7279\u5225\u306a\u30bf\u30a4\u30d7\u306e\u5b8c\u5168\u4e8c\u5206\u6728\u3067\u3059\u3002\u305d\u306e\u69d8\u3005\u306a\u64cd\u4f5c\uff08\u633f\u5165\u3001\u524a\u9664\u3001\u30d2\u30fc\u30d7\u5316\uff09\u306f\u3001\u5b9f\u969b\u306b\u5206\u5272\u7d71\u6cbb\u306e\u30a2\u30a4\u30c7\u30a2\u3092\u542b\u610f\u3057\u3066\u3044\u307e\u3059\u3002
        • \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\uff1a\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306f\u76f4\u63a5\u5206\u5272\u7d71\u6cbb\u3092\u9069\u7528\u3057\u307e\u305b\u3093\u304c\u3001\u4e00\u90e8\u306e\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u89e3\u6c7a\u30bd\u30ea\u30e5\u30fc\u30b7\u30e7\u30f3\u306f\u9593\u63a5\u7684\u306b\u3053\u306e\u6226\u7565\u3092\u9069\u7528\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u30c1\u30a7\u30a4\u30f3\u6cd5\u306e\u9577\u3044\u30ea\u30b9\u30c8\u306f\u3001\u30af\u30a8\u30ea\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u308b\u305f\u3081\u306b\u8d64\u9ed2\u6728\u306b\u5909\u63db\u3055\u308c\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002

        **\u5206\u5272\u7d71\u6cbb\u306f\u5de7\u5999\u306b\u6d78\u900f\u3059\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30a2\u30a4\u30c7\u30a2**\u3067\u3042\u308a\u3001\u69d8\u3005\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u7d44\u307f\u8fbc\u307e\u308c\u3066\u3044\u308b\u3053\u3068\u304c\u5206\u304b\u308a\u307e\u3059\u3002

        "},{"location":"chapter_divide_and_conquer/hanota_problem/","title":"12.4 \u00a0 \u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c","text":"

        \u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u3068\u4e8c\u5206\u6728\u69cb\u7bc9\u306e\u4e21\u65b9\u3067\u3001\u5143\u306e\u554f\u984c\u30922\u3064\u306e\u90e8\u5206\u554f\u984c\u306b\u5206\u89e3\u3057\u3001\u305d\u308c\u305e\u308c\u304c\u5143\u306e\u554f\u984c\u306e\u30b5\u30a4\u30ba\u306e\u534a\u5206\u3067\u3057\u305f\u3002\u3057\u304b\u3057\u3001\u30cf\u30ce\u30a4\u306e\u5854\u3067\u306f\u3001\u7570\u306a\u308b\u5206\u89e3\u6226\u7565\u3092\u63a1\u7528\u3057\u307e\u3059\u3002

        Question

        3\u3064\u306e\u67f1\u304c\u3042\u308a\u3001\u305d\u308c\u305e\u308c A\u3001B\u3001C \u3068\u8868\u8a18\u3055\u308c\u307e\u3059\u3002\u6700\u521d\u3001\u67f1 A \u306b\u306f \\(n\\) \u679a\u306e\u5186\u76e4\u304c\u3042\u308a\u3001\u4e0a\u304b\u3089\u4e0b\u306b\u5411\u304b\u3063\u3066\u6607\u9806\u306e\u30b5\u30a4\u30ba\u3067\u914d\u7f6e\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u79c1\u305f\u3061\u306e\u30bf\u30b9\u30af\u306f\u3001\u3053\u308c\u3089\u306e \\(n\\) \u679a\u306e\u5186\u76e4\u3092\u67f1 C \u306b\u79fb\u52d5\u3057\u3001\u5143\u306e\u9806\u5e8f\u3092\u7dad\u6301\u3059\u308b\u3053\u3068\u3067\u3059\uff08\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\uff09\u3002\u79fb\u52d5\u4e2d\u306b\u306f\u4ee5\u4e0b\u306e\u30eb\u30fc\u30eb\u304c\u9069\u7528\u3055\u308c\u307e\u3059\uff1a

        1. \u5186\u76e4\u306f\u67f1\u306e\u4e0a\u90e8\u304b\u3089\u306e\u307f\u53d6\u308a\u9664\u304f\u3053\u3068\u304c\u3067\u304d\u3001\u5225\u306e\u67f1\u306e\u4e0a\u90e8\u306b\u7f6e\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        2. \u4e00\u5ea6\u306b\u79fb\u52d5\u3067\u304d\u308b\u306e\u306f1\u679a\u306e\u5186\u76e4\u306e\u307f\u3067\u3059\u3002
        3. \u5c0f\u3055\u3044\u5186\u76e4\u306f\u5e38\u306b\u5927\u304d\u3044\u5186\u76e4\u306e\u4e0a\u306b\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u56f3 12-10 \u00a0 \u30cf\u30ce\u30a4\u306e\u5854\u306e\u4f8b

        \u30b5\u30a4\u30ba \\(i\\) \u306e\u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c\u3092 \\(f(i)\\) \u3068\u8868\u8a18\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\\(f(3)\\) \u306f3\u679a\u306e\u5186\u76e4\u3092\u67f1 A \u304b\u3089\u67f1 C \u306b\u79fb\u52d5\u3059\u308b\u3053\u3068\u3092\u8868\u3057\u307e\u3059\u3002

        "},{"location":"chapter_divide_and_conquer/hanota_problem/#1","title":"1. \u00a0 \u57fa\u672c\u30b1\u30fc\u30b9\u3092\u8003\u3048\u308b","text":"

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u554f\u984c \\(f(1)\\)\uff08\u5186\u76e4\u304c1\u679a\u306e\u307f\uff09\u306b\u3064\u3044\u3066\u306f\u3001A \u304b\u3089 C \u306b\u76f4\u63a5\u79fb\u52d5\u3067\u304d\u307e\u3059\u3002

        <1><2>

        \u56f3 12-11 \u00a0 \u30b5\u30a4\u30ba1\u306e\u554f\u984c\u306e\u89e3

        \\(f(2)\\)\uff08\u5186\u76e4\u304c2\u679a\uff09\u306b\u3064\u3044\u3066\u306f\u3001**\u67f1 B \u306e\u52a9\u3051\u3092\u501f\u308a\u3066\u5c0f\u3055\u3044\u5186\u76e4\u3092\u5927\u304d\u3044\u5186\u76e4\u306e\u4e0a\u306b\u4fdd\u3064**\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\uff1a

        1. \u307e\u305a\u3001\u5c0f\u3055\u3044\u5186\u76e4\u3092 A \u304b\u3089 B \u306b\u79fb\u52d5\u3057\u307e\u3059\u3002
        2. \u6b21\u306b\u3001\u5927\u304d\u3044\u5186\u76e4\u3092 A \u304b\u3089 C \u306b\u79fb\u52d5\u3057\u307e\u3059\u3002
        3. \u6700\u5f8c\u306b\u3001\u5c0f\u3055\u3044\u5186\u76e4\u3092 B \u304b\u3089 C \u306b\u79fb\u52d5\u3057\u307e\u3059\u3002
        <1><2><3><4>

        \u56f3 12-12 \u00a0 \u30b5\u30a4\u30ba2\u306e\u554f\u984c\u306e\u89e3

        \\(f(2)\\) \u3092\u89e3\u6c7a\u3059\u308b\u904e\u7a0b\u306f\u6b21\u306e\u3088\u3046\u306b\u8981\u7d04\u3067\u304d\u307e\u3059\uff1aB \u306e\u52a9\u3051\u3092\u501f\u308a\u30662\u679a\u306e\u5186\u76e4\u3092 A \u304b\u3089 C \u306b\u79fb\u52d5\u3059\u308b\u3002\u3053\u3053\u3067\u3001C \u3092\u30bf\u30fc\u30b2\u30c3\u30c8\u67f1\u3001B \u3092\u30d0\u30c3\u30d5\u30a1\u67f1\u3068\u547c\u3073\u307e\u3059\u3002

        "},{"location":"chapter_divide_and_conquer/hanota_problem/#2","title":"2. \u00a0 \u90e8\u5206\u554f\u984c\u306e\u5206\u89e3","text":"

        \u554f\u984c \\(f(3)\\)\uff08\u3064\u307e\u308a\u3001\u5186\u76e4\u304c3\u679a\u306e\u5834\u5408\uff09\u306b\u3064\u3044\u3066\u306f\u3001\u72b6\u6cc1\u304c\u3084\u3084\u8907\u96d1\u306b\u306a\u308a\u307e\u3059\u3002

        \u3059\u3067\u306b \\(f(1)\\) \u3068 \\(f(2)\\) \u306e\u89e3\u304c\u5206\u304b\u3063\u3066\u3044\u308b\u306e\u3067\u3001\u5206\u5272\u7d71\u6cbb\u306e\u89b3\u70b9\u3092\u63a1\u7528\u3057\u3001A \u306e\u4e0a\u306e2\u679a\u306e\u5186\u76e4\u30921\u3064\u306e\u5358\u4f4d\u3068\u3057\u3066\u6271\u3044\u3001\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u30b9\u30c6\u30c3\u30d7\u3092\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u30013\u679a\u306e\u5186\u76e4\u3092 A \u304b\u3089 C \u306b\u6b63\u5e38\u306b\u79fb\u52d5\u3067\u304d\u307e\u3059\u3002

        1. B \u3092\u30bf\u30fc\u30b2\u30c3\u30c8\u67f1\u3001C \u3092\u30d0\u30c3\u30d5\u30a1\u67f1\u3068\u3057\u3066\u30012\u679a\u306e\u5186\u76e4\u3092 A \u304b\u3089 B \u306b\u79fb\u52d5\u3057\u307e\u3059\u3002
        2. \u6b8b\u308a\u306e\u5186\u76e4\u3092 A \u304b\u3089\u76f4\u63a5 C \u306b\u79fb\u52d5\u3057\u307e\u3059\u3002
        3. C \u3092\u30bf\u30fc\u30b2\u30c3\u30c8\u67f1\u3001A \u3092\u30d0\u30c3\u30d5\u30a1\u67f1\u3068\u3057\u3066\u30012\u679a\u306e\u5186\u76e4\u3092 B \u304b\u3089 C \u306b\u79fb\u52d5\u3057\u307e\u3059\u3002
        <1><2><3><4>

        \u56f3 12-13 \u00a0 \u30b5\u30a4\u30ba3\u306e\u554f\u984c\u306e\u89e3

        \u672c\u8cea\u7684\u306b\u3001\\(f(3)\\) \u30922\u3064\u306e \\(f(2)\\) \u90e8\u5206\u554f\u984c\u30681\u3064\u306e \\(f(1)\\) \u90e8\u5206\u554f\u984c\u306b\u5206\u89e3\u3057\u307e\u3059\u3002\u3053\u308c\u30893\u3064\u306e\u90e8\u5206\u554f\u984c\u3092\u9806\u6b21\u89e3\u6c7a\u3059\u308b\u3053\u3068\u3067\u3001\u5143\u306e\u554f\u984c\u304c\u89e3\u6c7a\u3055\u308c\u3001\u90e8\u5206\u554f\u984c\u304c\u72ec\u7acb\u3057\u3066\u304a\u308a\u3001\u305d\u308c\u3089\u306e\u89e3\u3092\u30de\u30fc\u30b8\u3067\u304d\u308b\u3053\u3068\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u3053\u3053\u304b\u3089\u3001\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u30cf\u30ce\u30a4\u306e\u5854\u306e\u5206\u5272\u7d71\u6cbb\u6226\u7565\u3092\u8981\u7d04\u3067\u304d\u307e\u3059\u3002\u5143\u306e\u554f\u984c \\(f(n)\\) \u30922\u3064\u306e\u90e8\u5206\u554f\u984c \\(f(n-1)\\) \u30681\u3064\u306e\u90e8\u5206\u554f\u984c \\(f(1)\\) \u306b\u5206\u5272\u3057\u3001\u4ee5\u4e0b\u306e\u9806\u5e8f\u3067\u3053\u308c\u30893\u3064\u306e\u90e8\u5206\u554f\u984c\u3092\u89e3\u6c7a\u3057\u307e\u3059\uff1a

        1. C \u3092\u30d0\u30c3\u30d5\u30a1\u3068\u3057\u3066\u4f7f\u7528\u3057\u3001\\(n-1\\) \u679a\u306e\u5186\u76e4\u3092 A \u304b\u3089 B \u306b\u79fb\u52d5\u3057\u307e\u3059\u3002
        2. \u6b8b\u308a\u306e\u5186\u76e4\u3092 A \u304b\u3089\u76f4\u63a5 C \u306b\u79fb\u52d5\u3057\u307e\u3059\u3002
        3. A \u3092\u30d0\u30c3\u30d5\u30a1\u3068\u3057\u3066\u4f7f\u7528\u3057\u3001\\(n-1\\) \u679a\u306e\u5186\u76e4\u3092 B \u304b\u3089 C \u306b\u79fb\u52d5\u3057\u307e\u3059\u3002

        \u5404 \\(f(n-1)\\) \u90e8\u5206\u554f\u984c\u306b\u3064\u3044\u3066\u3001\u540c\u3058\u518d\u5e30\u5206\u5272\u3092\u9069\u7528\u3067\u304d\u3001\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c \\(f(1)\\) \u306b\u5230\u9054\u3059\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002\\(f(1)\\) \u306f\u5358\u4e00\u306e\u79fb\u52d5\u306e\u307f\u304c\u5fc5\u8981\u3067\u3042\u308b\u3053\u3068\u304c\u3059\u3067\u306b\u5206\u304b\u3063\u3066\u3044\u308b\u305f\u3081\u3001\u89e3\u6c7a\u3059\u308b\u306e\u306f\u7c21\u5358\u3067\u3059\u3002

        \u56f3 12-14 \u00a0 \u30cf\u30ce\u30a4\u306e\u5854\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306e\u5206\u5272\u7d71\u6cbb\u6226\u7565

        "},{"location":"chapter_divide_and_conquer/hanota_problem/#3","title":"3. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        \u30b3\u30fc\u30c9\u3067\u306f\u3001\u518d\u5e30\u95a2\u6570 dfs(i, src, buf, tar) \u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u67f1 src \u304b\u3089\u4e0a\u306e \\(i\\) \u679a\u306e\u5186\u76e4\u3092\u67f1 tar \u306b\u79fb\u52d5\u3057\u3001\u67f1 buf \u3092\u30d0\u30c3\u30d5\u30a1\u3068\u3057\u3066\u4f7f\u7528\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hanota.py
        def move(src: list[int], tar: list[int]):\n    \"\"\"\u5186\u76e4\u3092\u79fb\u52d5\"\"\"\n    # src \u306e\u4e0a\u304b\u3089\u5186\u76e4\u3092\u53d6\u308a\u51fa\u3059\n    pan = src.pop()\n    # \u5186\u76e4\u3092 tar \u306e\u4e0a\u306b\u7f6e\u304f\n    tar.append(pan)\n\ndef dfs(i: int, src: list[int], buf: list[int], tar: list[int]):\n    \"\"\"\u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c f(i) \u3092\u89e3\u304f\"\"\"\n    # src \u306b\u5186\u76e4\u304c 1 \u3064\u3060\u3051\u6b8b\u3063\u3066\u3044\u308b\u5834\u5408\u3001\u305d\u308c\u3092 tar \u306b\u79fb\u52d5\n    if i == 1:\n        move(src, tar)\n        return\n    # \u90e8\u5206\u554f\u984c f(i-1)\uff1atar \u306e\u52a9\u3051\u3092\u501f\u308a\u3066 src \u306e\u4e0a\u306e i-1 \u500b\u306e\u5186\u76e4\u3092 buf \u306b\u79fb\u52d5\n    dfs(i - 1, src, tar, buf)\n    # \u90e8\u5206\u554f\u984c f(1)\uff1a\u6b8b\u308a\u306e 1 \u500b\u306e\u5186\u76e4\u3092 src \u304b\u3089 tar \u306b\u79fb\u52d5\n    move(src, tar)\n    # \u90e8\u5206\u554f\u984c f(i-1)\uff1asrc \u306e\u52a9\u3051\u3092\u501f\u308a\u3066 buf \u306e\u4e0a\u306e i-1 \u500b\u306e\u5186\u76e4\u3092 tar \u306b\u79fb\u52d5\n    dfs(i - 1, buf, src, tar)\n\ndef solve_hanota(A: list[int], B: list[int], C: list[int]):\n    \"\"\"\u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c\u3092\u89e3\u304f\"\"\"\n    n = len(A)\n    # B \u306e\u52a9\u3051\u3092\u501f\u308a\u3066 A \u306e\u4e0a\u306e n \u500b\u306e\u5186\u76e4\u3092 C \u306b\u79fb\u52d5\n    dfs(n, A, B, C)\n
        hanota.cpp
        /* \u5186\u76e4\u3092\u79fb\u52d5 */\nvoid move(vector<int> &src, vector<int> &tar) {\n    // src \u306e\u6700\u4e0a\u90e8\u304b\u3089\u5186\u76e4\u3092\u53d6\u308a\u51fa\u3059\n    int pan = src.back();\n    src.pop_back();\n    // \u5186\u76e4\u3092 tar \u306e\u6700\u4e0a\u90e8\u306b\u914d\u7f6e\n    tar.push_back(pan);\n}\n\n/* \u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c f(i) \u3092\u89e3\u304f */\nvoid dfs(int i, vector<int> &src, vector<int> &buf, vector<int> &tar) {\n    // src \u306b\u5186\u76e4\u304c1\u3064\u3060\u3051\u6b8b\u3063\u3066\u3044\u308b\u5834\u5408\u3001\u305d\u308c\u3092 tar \u306b\u79fb\u52d5\n    if (i == 1) {\n        move(src, tar);\n        return;\n    }\n    // \u90e8\u5206\u554f\u984c f(i-1)\uff1atar \u306e\u52a9\u3051\u3092\u501f\u308a\u3066\u3001\u4e0a\u4f4d i-1 \u500b\u306e\u5186\u76e4\u3092 src \u304b\u3089 buf \u306b\u79fb\u52d5\n    dfs(i - 1, src, tar, buf);\n    // \u90e8\u5206\u554f\u984c f(1)\uff1a\u6b8b\u308a\u306e1\u3064\u306e\u5186\u76e4\u3092 src \u304b\u3089 tar \u306b\u79fb\u52d5\n    move(src, tar);\n    // \u90e8\u5206\u554f\u984c f(i-1)\uff1asrc \u306e\u52a9\u3051\u3092\u501f\u308a\u3066\u3001\u4e0a\u4f4d i-1 \u500b\u306e\u5186\u76e4\u3092 buf \u304b\u3089 tar \u306b\u79fb\u52d5\n    dfs(i - 1, buf, src, tar);\n}\n\n/* \u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c\u3092\u89e3\u304f */\nvoid solveHanota(vector<int> &A, vector<int> &B, vector<int> &C) {\n    int n = A.size();\n    // B \u306e\u52a9\u3051\u3092\u501f\u308a\u3066\u3001\u4e0a\u4f4d n \u500b\u306e\u5186\u76e4\u3092 A \u304b\u3089 C \u306b\u79fb\u52d5\n    dfs(n, A, B, C);\n}\n
        hanota.java
        /* \u5186\u76e4\u3092\u79fb\u52d5 */\nvoid move(List<Integer> src, List<Integer> tar) {\n    // src \u306e\u6700\u4e0a\u90e8\u304b\u3089\u5186\u76e4\u3092\u53d6\u308a\u51fa\u3059\n    Integer pan = src.remove(src.size() - 1);\n    // \u5186\u76e4\u3092 tar \u306e\u6700\u4e0a\u90e8\u306b\u914d\u7f6e\n    tar.add(pan);\n}\n\n/* \u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c f(i) \u3092\u89e3\u304f */\nvoid dfs(int i, List<Integer> src, List<Integer> buf, List<Integer> tar) {\n    // src \u306b\u5186\u76e4\u304c1\u3064\u3060\u3051\u6b8b\u3063\u3066\u3044\u308b\u5834\u5408\u3001\u305d\u308c\u3092 tar \u306b\u79fb\u52d5\n    if (i == 1) {\n        move(src, tar);\n        return;\n    }\n    // \u90e8\u5206\u554f\u984c f(i-1)\uff1atar \u306e\u52a9\u3051\u3092\u501f\u308a\u3066\u3001\u4e0a\u4f4d i-1 \u500b\u306e\u5186\u76e4\u3092 src \u304b\u3089 buf \u306b\u79fb\u52d5\n    dfs(i - 1, src, tar, buf);\n    // \u90e8\u5206\u554f\u984c f(1)\uff1a\u6b8b\u308a\u306e1\u3064\u306e\u5186\u76e4\u3092 src \u304b\u3089 tar \u306b\u79fb\u52d5\n    move(src, tar);\n    // \u90e8\u5206\u554f\u984c f(i-1)\uff1asrc \u306e\u52a9\u3051\u3092\u501f\u308a\u3066\u3001\u4e0a\u4f4d i-1 \u500b\u306e\u5186\u76e4\u3092 buf \u304b\u3089 tar \u306b\u79fb\u52d5\n    dfs(i - 1, buf, src, tar);\n}\n\n/* \u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c\u3092\u89e3\u304f */\nvoid solveHanota(List<Integer> A, List<Integer> B, List<Integer> C) {\n    int n = A.size();\n    // B \u306e\u52a9\u3051\u3092\u501f\u308a\u3066\u3001\u4e0a\u4f4d n \u500b\u306e\u5186\u76e4\u3092 A \u304b\u3089 C \u306b\u79fb\u52d5\n    dfs(n, A, B, C);\n}\n
        hanota.cs
        [class]{hanota}-[func]{Move}\n\n[class]{hanota}-[func]{DFS}\n\n[class]{hanota}-[func]{SolveHanota}\n
        hanota.go
        [class]{}-[func]{move}\n\n[class]{}-[func]{dfsHanota}\n\n[class]{}-[func]{solveHanota}\n
        hanota.swift
        [class]{}-[func]{move}\n\n[class]{}-[func]{dfs}\n\n[class]{}-[func]{solveHanota}\n
        hanota.js
        [class]{}-[func]{move}\n\n[class]{}-[func]{dfs}\n\n[class]{}-[func]{solveHanota}\n
        hanota.ts
        [class]{}-[func]{move}\n\n[class]{}-[func]{dfs}\n\n[class]{}-[func]{solveHanota}\n
        hanota.dart
        [class]{}-[func]{move}\n\n[class]{}-[func]{dfs}\n\n[class]{}-[func]{solveHanota}\n
        hanota.rs
        [class]{}-[func]{move_pan}\n\n[class]{}-[func]{dfs}\n\n[class]{}-[func]{solve_hanota}\n
        hanota.c
        [class]{}-[func]{move}\n\n[class]{}-[func]{dfs}\n\n[class]{}-[func]{solveHanota}\n
        hanota.kt
        [class]{}-[func]{move}\n\n[class]{}-[func]{dfs}\n\n[class]{}-[func]{solveHanota}\n
        hanota.rb
        [class]{}-[func]{move}\n\n[class]{}-[func]{dfs}\n\n[class]{}-[func]{solve_hanota}\n

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c\u306f\u9ad8\u3055 \\(n\\) \u306e\u518d\u5e30\u6728\u3068\u3057\u3066\u8996\u899a\u5316\u3067\u304d\u307e\u3059\u3002\u5404\u30ce\u30fc\u30c9\u306f\u90e8\u5206\u554f\u984c\u3092\u8868\u3057\u3001dfs() \u306e\u547c\u3073\u51fa\u3057\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(2^n)\\)\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u3067\u3059\u3002

        \u56f3 12-15 \u00a0 \u30cf\u30ce\u30a4\u306e\u5854\u306e\u518d\u5e30\u6728

        Quote

        \u30cf\u30ce\u30a4\u306e\u5854\u306f\u53e4\u4ee3\u306e\u4f1d\u8aac\u306b\u7531\u6765\u3057\u307e\u3059\u3002\u53e4\u4ee3\u30a4\u30f3\u30c9\u306e\u5bfa\u9662\u3067\u3001\u50e7\u4fb6\u305f\u3061\u306f3\u672c\u306e\u9ad8\u3044\u30c0\u30a4\u30e4\u30e2\u30f3\u30c9\u306e\u67f1\u3068\u3001\u7570\u306a\u308b\u30b5\u30a4\u30ba\u306e \\(64\\) \u679a\u306e\u91d1\u306e\u5186\u76e4\u3092\u6301\u3063\u3066\u3044\u307e\u3057\u305f\u3002\u5f7c\u3089\u306f\u3001\u6700\u5f8c\u306e\u5186\u76e4\u304c\u6b63\u3057\u304f\u7f6e\u304b\u308c\u305f\u3068\u304d\u3001\u4e16\u754c\u304c\u7d42\u308f\u308b\u3068\u4fe1\u3058\u3066\u3044\u307e\u3057\u305f\u3002

        \u3057\u304b\u3057\u3001\u50e7\u4fb6\u305f\u3061\u304c1\u79d2\u306b1\u679a\u306e\u5186\u76e4\u3092\u79fb\u52d5\u3057\u305f\u3068\u3057\u3066\u3082\u3001\u7d04 \\(2^{64} \\approx 1.84\u00d710^{19}\\) \u2014\u7d045850\u5104\u5e74\u2014\u304b\u304b\u308a\u3001\u5b87\u5b99\u306e\u5e74\u9f62\u306e\u73fe\u5728\u306e\u63a8\u5b9a\u3092\u306f\u308b\u304b\u306b\u8d85\u3048\u3066\u3044\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u3053\u306e\u4f1d\u8aac\u304c\u771f\u5b9f\u3067\u3042\u308c\u3070\u3001\u4e16\u754c\u306e\u7d42\u308f\u308a\u306b\u3064\u3044\u3066\u5fc3\u914d\u3059\u308b\u5fc5\u8981\u306f\u304a\u305d\u3089\u304f\u306a\u3044\u3067\u3057\u3087\u3046\u3002

        "},{"location":"chapter_divide_and_conquer/summary/","title":"12.5 \u00a0 \u307e\u3068\u3081","text":"
        • \u5206\u5272\u7d71\u6cbb\u306f\u4e00\u822c\u7684\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u8a2d\u8a08\u6226\u7565\u3067\u3001\u5206\u5272\uff08\u5206\u5272\uff09\u3068\u7d71\u6cbb\uff08\u30de\u30fc\u30b8\uff09\u306e2\u3064\u306e\u6bb5\u968e\u304b\u3089\u69cb\u6210\u3055\u308c\u3001\u4e00\u822c\u7684\u306b\u518d\u5e30\u3092\u4f7f\u7528\u3057\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002
        • \u554f\u984c\u304c\u5206\u5272\u7d71\u6cbb\u30a2\u30d7\u30ed\u30fc\u30c1\u306b\u9069\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3092\u5224\u65ad\u3059\u308b\u305f\u3081\u306b\u3001\u554f\u984c\u304c\u5206\u89e3\u53ef\u80fd\u304b\u3069\u3046\u304b\u3001\u90e8\u5206\u554f\u984c\u304c\u72ec\u7acb\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3001\u90e8\u5206\u554f\u984c\u3092\u30de\u30fc\u30b8\u3067\u304d\u308b\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002
        • \u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u306f\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306e\u5178\u578b\u7684\u306a\u4f8b\u3067\u3059\u3002\u914d\u5217\u3092\u518d\u5e30\u7684\u306b2\u3064\u306e\u7b49\u3057\u3044\u9577\u3055\u306e\u526f\u914d\u5217\u306b\u5206\u5272\u3057\u30011\u3064\u306e\u8981\u7d20\u306e\u307f\u304c\u6b8b\u308b\u307e\u3067\u7d9a\u3051\u3001\u6b21\u306b\u3053\u308c\u3089\u306e\u526f\u914d\u5217\u3092\u5c64\u3054\u3068\u306b\u30de\u30fc\u30b8\u3057\u3066\u30bd\u30fc\u30c8\u3092\u5b8c\u4e86\u3057\u307e\u3059\u3002
        • \u5206\u5272\u7d71\u6cbb\u6226\u7565\u306e\u5c0e\u5165\u306f\u3001\u3057\u3070\u3057\u3070\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002\u4e00\u65b9\u3067\u306f\u64cd\u4f5c\u6570\u3092\u6e1b\u3089\u3057\u3001\u4ed6\u65b9\u3067\u306f\u5206\u5272\u5f8c\u306e\u30b7\u30b9\u30c6\u30e0\u306e\u4e26\u5217\u6700\u9069\u5316\u3092\u4fc3\u9032\u3057\u307e\u3059\u3002
        • \u5206\u5272\u7d71\u6cbb\u306f\u591a\u6570\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u306b\u9069\u7528\u3067\u304d\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u8a2d\u8a08\u3067\u5e83\u304f\u4f7f\u7528\u3055\u308c\u3001\u591a\u304f\u306e\u30b7\u30ca\u30ea\u30aa\u306b\u73fe\u308c\u307e\u3059\u3002
        • \u7dcf\u5f53\u305f\u308a\u691c\u7d22\u3068\u6bd4\u8f03\u3057\u3066\u3001\u9069\u5fdc\u691c\u7d22\u306f\u3088\u308a\u52b9\u7387\u7684\u3067\u3059\u3002\u6642\u9593\u8a08\u7b97\u91cf\u304c \\(O(\\log n)\\) \u306e\u691c\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u901a\u5e38\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306b\u57fa\u3065\u3044\u3066\u3044\u307e\u3059\u3002
        • \u4e8c\u5206\u63a2\u7d22\u306f\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306e\u3082\u3046\u4e00\u3064\u306e\u53e4\u5178\u7684\u306a\u5fdc\u7528\u3067\u3059\u3002\u90e8\u5206\u554f\u984c\u306e\u89e3\u306e\u30de\u30fc\u30b8\u3092\u542b\u307e\u305a\u3001\u518d\u5e30\u7684\u306a\u5206\u5272\u7d71\u6cbb\u30a2\u30d7\u30ed\u30fc\u30c1\u3067\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002
        • \u4e8c\u5206\u6728\u69cb\u7bc9\u554f\u984c\u3067\u306f\u3001\u6728\u306e\u69cb\u7bc9\uff08\u5143\u306e\u554f\u984c\uff09\u3092\u5de6\u306e\u90e8\u5206\u6728\u3068\u53f3\u306e\u90e8\u5206\u6728\u306e\u69cb\u7bc9\uff08\u90e8\u5206\u554f\u984c\uff09\u306b\u5206\u5272\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306f\u524d\u9806\u8d70\u67fb\u3068\u4e2d\u9806\u8d70\u67fb\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2\u3092\u5206\u5272\u3059\u308b\u3053\u3068\u3067\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002
        • \u30cf\u30ce\u30a4\u306e\u5854\u554f\u984c\u3067\u306f\u3001\u30b5\u30a4\u30ba \\(n\\) \u306e\u554f\u984c\u3092\u30b5\u30a4\u30ba \\(n-1\\) \u306e2\u3064\u306e\u90e8\u5206\u554f\u984c\u3068\u30b5\u30a4\u30ba \\(1\\) \u306e1\u3064\u306e\u90e8\u5206\u554f\u984c\u306b\u5206\u89e3\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u30893\u3064\u306e\u90e8\u5206\u554f\u984c\u3092\u9806\u6b21\u89e3\u6c7a\u3059\u308b\u3053\u3068\u3067\u3001\u5143\u306e\u554f\u984c\u304c\u89e3\u6c7a\u3055\u308c\u307e\u3059\u3002
        "},{"location":"chapter_dynamic_programming/","title":"\u7b2c 14 \u7ae0 \u00a0 \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0","text":"

        Abstract

        \u5ddd\u304c\u6d41\u308c\u3066\u6d77\u306b\u6ce8\u3050\u3088\u3046\u306b\u3001

        \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u5c0f\u3055\u306a\u554f\u984c\u306e\u89e3\u3092\u7e54\u308a\u5408\u308f\u305b\u3066\u3001\u3088\u308a\u5927\u304d\u306a\u554f\u984c\u306e\u89e3\u3078\u3068\u5c0e\u304d\u307e\u3059\u3002\u4e00\u6b69\u4e00\u6b69\u9032\u3093\u3067\u3001\u6700\u7d42\u7684\u306a\u7b54\u3048\u304c\u5f85\u3064\u5f7c\u5cb8\u3078\u3068\u5411\u304b\u3044\u307e\u3059\u3002

        "},{"location":"chapter_dynamic_programming/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 14.1 \u00a0 \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u5165\u9580
        • 14.2 \u00a0 DP\u554f\u984c\u306e\u7279\u6027
        • 14.3 \u00a0 DP\u554f\u984c\u89e3\u6c7a\u30a2\u30d7\u30ed\u30fc\u30c1
        • 14.4 \u00a0 0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c
        • 14.5 \u00a0 \u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c
        • 14.6 \u00a0 \u7de8\u96c6\u8ddd\u96e2\u554f\u984c
        • 14.7 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_dynamic_programming/dp_problem_features/","title":"14.2 \u00a0 \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u306e\u7279\u5fb4","text":"

        \u524d\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u304c\u554f\u984c\u3092\u90e8\u5206\u554f\u984c\u306b\u5206\u89e3\u3059\u308b\u3053\u3068\u3067\u5143\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u65b9\u6cd5\u3092\u5b66\u3073\u307e\u3057\u305f\u3002\u5b9f\u969b\u3001\u90e8\u5206\u554f\u984c\u306e\u5206\u89e3\u306f\u4e00\u822c\u7684\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30a2\u30d7\u30ed\u30fc\u30c1\u3067\u3042\u308a\u3001\u5206\u5272\u7d71\u6cbb\u6cd5\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3067\u306f\u7570\u306a\u308b\u91cd\u70b9\u304c\u3042\u308a\u307e\u3059\u3002

        • \u5206\u5272\u7d71\u6cbb\u6cd5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u5143\u306e\u554f\u984c\u3092\u8907\u6570\u306e\u72ec\u7acb\u3057\u305f\u90e8\u5206\u554f\u984c\u306b\u518d\u5e30\u7684\u306b\u5206\u5272\u3057\u3001\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306b\u5230\u9054\u3059\u308b\u307e\u3067\u7d9a\u3051\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u6642\u306b\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u7d44\u307f\u5408\u308f\u305b\u3066\u6700\u7d42\u7684\u306b\u5143\u306e\u554f\u984c\u306e\u89e3\u3092\u5f97\u307e\u3059\u3002
        • \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3082\u554f\u984c\u3092\u518d\u5e30\u7684\u306b\u5206\u89e3\u3057\u307e\u3059\u304c\u3001\u5206\u5272\u7d71\u6cbb\u6cd5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u306e\u4e3b\u306a\u9055\u3044\u306f\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306e\u90e8\u5206\u554f\u984c\u304c\u76f8\u4e92\u4f9d\u5b58\u7684\u3067\u3042\u308a\u3001\u5206\u89e3\u30d7\u30ed\u30bb\u30b9\u4e2d\u306b\u591a\u304f\u306e\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u304c\u73fe\u308c\u308b\u3053\u3068\u3067\u3059\u3002
        • \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u8a66\u884c\u932f\u8aa4\u306b\u3088\u3063\u3066\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u89e3\u3092\u7db2\u7f85\u3057\u3001\u679d\u5208\u308a\u306b\u3088\u3063\u3066\u4e0d\u5fc5\u8981\u306a\u63a2\u7d22\u5206\u5c90\u3092\u907f\u3051\u307e\u3059\u3002\u5143\u306e\u554f\u984c\u306e\u89e3\u306f\u4e00\u9023\u306e\u6c7a\u5b9a\u30b9\u30c6\u30c3\u30d7\u304b\u3089\u69cb\u6210\u3055\u308c\u3001\u5404\u6c7a\u5b9a\u30b9\u30c6\u30c3\u30d7\u524d\u306e\u5404\u90e8\u5206\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u90e8\u5206\u554f\u984c\u3068\u3057\u3066\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u5b9f\u969b\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u6700\u9069\u5316\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3088\u304f\u4f7f\u7528\u3055\u308c\u3001\u3053\u308c\u3089\u306f\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u3092\u542b\u3080\u3060\u3051\u3067\u306a\u304f\u3001\u4ed6\u306b2\u3064\u306e\u4e3b\u8981\u306a\u7279\u5fb4\u304c\u3042\u308a\u307e\u3059\uff1a\u6700\u9069\u90e8\u5206\u69cb\u9020\u3068\u7121\u8a18\u61b6\u6027\u3067\u3059\u3002

        "},{"location":"chapter_dynamic_programming/dp_problem_features/#1421","title":"14.2.1 \u00a0 \u6700\u9069\u90e8\u5206\u69cb\u9020","text":"

        \u968e\u6bb5\u767b\u308a\u554f\u984c\u3092\u5c11\u3057\u4fee\u6b63\u3057\u3066\u3001\u6700\u9069\u90e8\u5206\u69cb\u9020\u306e\u6982\u5ff5\u3092\u5b9f\u8a3c\u3059\u308b\u306e\u306b\u3088\u308a\u9069\u3057\u305f\u3082\u306e\u306b\u3057\u307e\u3059\u3002

        \u968e\u6bb5\u767b\u308a\u306e\u6700\u5c0f\u30b3\u30b9\u30c8

        \u968e\u6bb5\u304c\u3042\u308a\u3001\u4e00\u5ea6\u306b1\u6bb5\u307e\u305f\u306f2\u6bb5\u4e0a\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u968e\u6bb5\u306e\u5404\u6bb5\u306b\u306f\u305d\u306e\u6bb5\u3067\u652f\u6255\u3046\u5fc5\u8981\u304c\u3042\u308b\u30b3\u30b9\u30c8\u3092\u8868\u3059\u975e\u8ca0\u306e\u6574\u6570\u304c\u3042\u308a\u307e\u3059\u3002\u975e\u8ca0\u306e\u6574\u6570\u914d\u5217 \\(cost\\) \u304c\u4e0e\u3048\u3089\u308c\u3001\\(cost[i]\\) \u306f \\(i\\) \u6bb5\u76ee\u3067\u652f\u6255\u3046\u5fc5\u8981\u304c\u3042\u308b\u30b3\u30b9\u30c8\u3092\u8868\u3057\u3001\\(cost[0]\\) \u306f\u5730\u9762\uff08\u958b\u59cb\u70b9\uff09\u3067\u3059\u3002\u9802\u4e0a\u306b\u5230\u9054\u3059\u308b\u305f\u3081\u306b\u5fc5\u8981\u306a\u6700\u5c0f\u30b3\u30b9\u30c8\u306f\u4f55\u3067\u3059\u304b\uff1f

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u30011\u6bb5\u76ee\u30012\u6bb5\u76ee\u30013\u6bb5\u76ee\u306e\u30b3\u30b9\u30c8\u304c\u305d\u308c\u305e\u308c \\(1\\)\u3001\\(10\\)\u3001\\(1\\) \u306e\u5834\u5408\u3001\u5730\u9762\u304b\u30893\u6bb5\u76ee\u306b\u767b\u308b\u6700\u5c0f\u30b3\u30b9\u30c8\u306f \\(2\\) \u3067\u3059\u3002

        \u56f3 14-6 \u00a0 3\u6bb5\u76ee\u306b\u767b\u308b\u6700\u5c0f\u30b3\u30b9\u30c8

        \\(dp[i]\\) \u3092 \\(i\\) \u6bb5\u76ee\u306b\u767b\u308b\u7d2f\u7a4d\u30b3\u30b9\u30c8\u3068\u3057\u307e\u3059\u3002\\(i\\) \u6bb5\u76ee\u306f \\(i-1\\) \u6bb5\u76ee\u307e\u305f\u306f \\(i-2\\) \u6bb5\u76ee\u304b\u3089\u306e\u307f\u6765\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u305f\u3081\u3001\\(dp[i]\\) \u306f \\(dp[i-1] + cost[i]\\) \u307e\u305f\u306f \\(dp[i-2] + cost[i]\\) \u306e\u3044\u305a\u308c\u304b\u3057\u304b\u3042\u308a\u3048\u307e\u305b\u3093\u3002\u30b3\u30b9\u30c8\u3092\u6700\u5c0f\u5316\u3059\u308b\u305f\u3081\u306b\u30012\u3064\u306e\u3046\u3061\u5c0f\u3055\u3044\u65b9\u3092\u9078\u629e\u3059\u3079\u304d\u3067\u3059\uff1a

        \\[ dp[i] = \\min(dp[i-1], dp[i-2]) + cost[i] \\]

        \u3053\u308c\u306b\u3088\u308a\u6700\u9069\u90e8\u5206\u69cb\u9020\u306e\u610f\u5473\u304c\u308f\u304b\u308a\u307e\u3059\uff1a\u5143\u306e\u554f\u984c\u306e\u6700\u9069\u89e3\u306f\u90e8\u5206\u554f\u984c\u306e\u6700\u9069\u89e3\u304b\u3089\u69cb\u7bc9\u3055\u308c\u308b\u3002

        \u3053\u306e\u554f\u984c\u306f\u660e\u3089\u304b\u306b\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u6301\u3063\u3066\u3044\u307e\u3059\uff1a2\u3064\u306e\u90e8\u5206\u554f\u984c \\(dp[i-1]\\) \u3068 \\(dp[i-2]\\) \u306e\u6700\u9069\u89e3\u304b\u3089\u3088\u308a\u826f\u3044\u65b9\u3092\u9078\u629e\u3057\u3001\u305d\u308c\u3092\u4f7f\u7528\u3057\u3066\u5143\u306e\u554f\u984c \\(dp[i]\\) \u306e\u6700\u9069\u89e3\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002

        \u3067\u306f\u3001\u524d\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u306e\u968e\u6bb5\u767b\u308a\u554f\u984c\u306f\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u6301\u3063\u3066\u3044\u308b\u3067\u3057\u3087\u3046\u304b\uff1f\u305d\u306e\u76ee\u6a19\u306f\u89e3\u306e\u6570\u3092\u6c42\u3081\u308b\u3053\u3068\u3067\u3001\u3053\u308c\u306f\u6570\u3048\u4e0a\u3052\u554f\u984c\u306e\u3088\u3046\u3067\u3059\u304c\u3001\u5225\u306e\u65b9\u6cd5\u3067\u5c0b\u306d\u3066\u307f\u307e\u3057\u3087\u3046\uff1a\u300c\u89e3\u306e\u6700\u5927\u6570\u3092\u6c42\u3081\u308b\u300d\u3002\u9a5a\u304f\u3053\u3068\u306b\u3001**\u554f\u984c\u304c\u5909\u308f\u3063\u305f\u306b\u3082\u304b\u304b\u308f\u3089\u305a\u3001\u6700\u9069\u90e8\u5206\u69cb\u9020\u304c\u73fe\u308c\u305f**\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\uff1a\\(n\\) \u6bb5\u76ee\u3067\u306e\u89e3\u306e\u6700\u5927\u6570\u306f\u3001\\(n-1\\) \u6bb5\u76ee\u3068 \\(n-2\\) \u6bb5\u76ee\u3067\u306e\u89e3\u306e\u6700\u5927\u6570\u306e\u548c\u306b\u7b49\u3057\u3044\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u6700\u9069\u90e8\u5206\u69cb\u9020\u306e\u89e3\u91c8\u306f\u975e\u5e38\u306b\u67d4\u8edf\u3067\u3001\u7570\u306a\u308b\u554f\u984c\u3067\u306f\u7570\u306a\u308b\u610f\u5473\u3092\u6301\u3061\u307e\u3059\u3002

        \u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3068\u521d\u671f\u72b6\u614b \\(dp[1] = cost[1]\\) \u304a\u3088\u3073 \\(dp[2] = cost[2]\\) \u306b\u5f93\u3063\u3066\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30b3\u30fc\u30c9\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_cost_climbing_stairs_dp.py
        def min_cost_climbing_stairs_dp(cost: list[int]) -> int:\n    \"\"\"\u6700\u5c0f\u30b3\u30b9\u30c8\u968e\u6bb5\u767b\u308a\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n = len(cost) - 1\n    if n == 1 or n == 2:\n        return cost[n]\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    dp = [0] * (n + 1)\n    # \u521d\u671f\u72b6\u614b\uff1a\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4e8b\u524d\u8a2d\u5b9a\n    dp[1], dp[2] = cost[1], cost[2]\n    # \u72b6\u614b\u9077\u79fb\uff1a\u5c0f\u3055\u3044\u90e8\u5206\u554f\u984c\u304b\u3089\u5927\u304d\u3044\u90e8\u5206\u554f\u984c\u3092\u6bb5\u968e\u7684\u306b\u89e3\u304f\n    for i in range(3, n + 1):\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n    return dp[n]\n
        min_cost_climbing_stairs_dp.cpp
        /* \u6700\u5c0f\u30b3\u30b9\u30c8\u968e\u6bb5\u767b\u308a\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint minCostClimbingStairsDP(vector<int> &cost) {\n    int n = cost.size() - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3057\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    vector<int> dp(n + 1);\n    // \u521d\u671f\u72b6\u614b\uff1a\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4e8b\u524d\u8a2d\u5b9a\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u72b6\u614b\u9077\u79fb\uff1a\u5c0f\u3055\u306a\u554f\u984c\u304b\u3089\u5927\u304d\u306a\u90e8\u5206\u554f\u984c\u3092\u6bb5\u968e\u7684\u306b\u89e3\u304f\n    for (int i = 3; i <= n; i++) {\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
        min_cost_climbing_stairs_dp.java
        /* \u6700\u5c0f\u30b3\u30b9\u30c8\u968e\u6bb5\u767b\u308a\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint minCostClimbingStairsDP(int[] cost) {\n    int n = cost.length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3057\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    int[] dp = new int[n + 1];\n    // \u521d\u671f\u72b6\u614b\uff1a\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4e8b\u524d\u8a2d\u5b9a\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u72b6\u614b\u9077\u79fb\uff1a\u5c0f\u3055\u306a\u554f\u984c\u304b\u3089\u5927\u304d\u306a\u90e8\u5206\u554f\u984c\u3092\u6bb5\u968e\u7684\u306b\u89e3\u304f\n    for (int i = 3; i <= n; i++) {\n        dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
        min_cost_climbing_stairs_dp.cs
        [class]{min_cost_climbing_stairs_dp}-[func]{MinCostClimbingStairsDP}\n
        min_cost_climbing_stairs_dp.go
        [class]{}-[func]{minCostClimbingStairsDP}\n
        min_cost_climbing_stairs_dp.swift
        [class]{}-[func]{minCostClimbingStairsDP}\n
        min_cost_climbing_stairs_dp.js
        [class]{}-[func]{minCostClimbingStairsDP}\n
        min_cost_climbing_stairs_dp.ts
        [class]{}-[func]{minCostClimbingStairsDP}\n
        min_cost_climbing_stairs_dp.dart
        [class]{}-[func]{minCostClimbingStairsDP}\n
        min_cost_climbing_stairs_dp.rs
        [class]{}-[func]{min_cost_climbing_stairs_dp}\n
        min_cost_climbing_stairs_dp.c
        [class]{}-[func]{minCostClimbingStairsDP}\n
        min_cost_climbing_stairs_dp.kt
        [class]{}-[func]{minCostClimbingStairsDP}\n
        min_cost_climbing_stairs_dp.rb
        [class]{}-[func]{min_cost_climbing_stairs_dp}\n

        \u4e0b\u306e\u56f3\u306f\u4e0a\u8a18\u30b3\u30fc\u30c9\u306e\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 14-7 \u00a0 \u968e\u6bb5\u767b\u308a\u306e\u6700\u5c0f\u30b3\u30b9\u30c8\u306e\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9

        \u3053\u306e\u554f\u984c\u3082\u7a7a\u9593\u6700\u9069\u5316\u304c\u53ef\u80fd\u3067\u30011\u6b21\u5143\u30920\u306b\u5727\u7e2e\u3057\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u3092 \\(O(n)\\) \u304b\u3089 \\(O(1)\\) \u306b\u524a\u6e1b\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_cost_climbing_stairs_dp.py
        def min_cost_climbing_stairs_dp_comp(cost: list[int]) -> int:\n    \"\"\"\u6700\u5c0f\u30b3\u30b9\u30c8\u968e\u6bb5\u767b\u308a\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n = len(cost) - 1\n    if n == 1 or n == 2:\n        return cost[n]\n    a, b = cost[1], cost[2]\n    for i in range(3, n + 1):\n        a, b = b, min(a, b) + cost[i]\n    return b\n
        min_cost_climbing_stairs_dp.cpp
        /* \u6700\u5c0f\u30b3\u30b9\u30c8\u968e\u6bb5\u767b\u308a\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint minCostClimbingStairsDPComp(vector<int> &cost) {\n    int n = cost.size() - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
        min_cost_climbing_stairs_dp.java
        /* \u6700\u5c0f\u30b3\u30b9\u30c8\u968e\u6bb5\u767b\u308a\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint minCostClimbingStairsDPComp(int[] cost) {\n    int n = cost.length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = Math.min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
        min_cost_climbing_stairs_dp.cs
        [class]{min_cost_climbing_stairs_dp}-[func]{MinCostClimbingStairsDPComp}\n
        min_cost_climbing_stairs_dp.go
        [class]{}-[func]{minCostClimbingStairsDPComp}\n
        min_cost_climbing_stairs_dp.swift
        [class]{}-[func]{minCostClimbingStairsDPComp}\n
        min_cost_climbing_stairs_dp.js
        [class]{}-[func]{minCostClimbingStairsDPComp}\n
        min_cost_climbing_stairs_dp.ts
        [class]{}-[func]{minCostClimbingStairsDPComp}\n
        min_cost_climbing_stairs_dp.dart
        [class]{}-[func]{minCostClimbingStairsDPComp}\n
        min_cost_climbing_stairs_dp.rs
        [class]{}-[func]{min_cost_climbing_stairs_dp_comp}\n
        min_cost_climbing_stairs_dp.c
        [class]{}-[func]{minCostClimbingStairsDPComp}\n
        min_cost_climbing_stairs_dp.kt
        [class]{}-[func]{minCostClimbingStairsDPComp}\n
        min_cost_climbing_stairs_dp.rb
        [class]{}-[func]{min_cost_climbing_stairs_dp_comp}\n
        "},{"location":"chapter_dynamic_programming/dp_problem_features/#1422","title":"14.2.2 \u00a0 \u7121\u8a18\u61b6\u6027","text":"

        \u7121\u8a18\u61b6\u6027\u306f\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u304c\u554f\u984c\u89e3\u6c7a\u306b\u52b9\u679c\u7684\u3067\u3042\u308b\u3053\u3068\u3092\u53ef\u80fd\u306b\u3059\u308b\u91cd\u8981\u306a\u7279\u5fb4\u306e1\u3064\u3067\u3059\u3002\u305d\u306e\u5b9a\u7fa9\u306f\uff1a\u7279\u5b9a\u306e\u72b6\u614b\u304c\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001\u305d\u306e\u5c06\u6765\u306e\u767a\u5c55\u306f\u73fe\u5728\u306e\u72b6\u614b\u306e\u307f\u306b\u95a2\u9023\u3057\u3001\u904e\u53bb\u306b\u7d4c\u9a13\u3057\u305f\u3059\u3079\u3066\u306e\u72b6\u614b\u3068\u306f\u7121\u95a2\u4fc2\u3067\u3042\u308b\u3002

        \u968e\u6bb5\u767b\u308a\u554f\u984c\u3092\u4f8b\u306b\u53d6\u308b\u3068\u3001\u72b6\u614b \\(i\\) \u304c\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001\u305d\u308c\u306f\u72b6\u614b \\(i+1\\) \u3068 \\(i+2\\) \u306b\u767a\u5c55\u3057\u3001\u305d\u308c\u305e\u308c1\u6bb5\u30b8\u30e3\u30f3\u30d7\u30682\u6bb5\u30b8\u30e3\u30f3\u30d7\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002\u3053\u308c\u30892\u3064\u306e\u9078\u629e\u3092\u3059\u308b\u3068\u304d\u3001\u72b6\u614b \\(i\\) \u3088\u308a\u524d\u306e\u72b6\u614b\u3092\u8003\u616e\u3059\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u306a\u305c\u306a\u3089\u3001\u305d\u308c\u3089\u306f\u72b6\u614b \\(i\\) \u306e\u5c06\u6765\u306b\u5f71\u97ff\u3057\u306a\u3044\u304b\u3089\u3067\u3059\u3002

        \u3057\u304b\u3057\u3001\u968e\u6bb5\u767b\u308a\u554f\u984c\u306b\u5236\u7d04\u3092\u8ffd\u52a0\u3059\u308b\u3068\u3001\u72b6\u6cc1\u304c\u5909\u308f\u308a\u307e\u3059\u3002

        \u5236\u7d04\u4ed8\u304d\u968e\u6bb5\u767b\u308a

        \\(n\\) \u6bb5\u306e\u968e\u6bb5\u304c\u3042\u308a\u3001\u6bce\u56de1\u6bb5\u307e\u305f\u306f2\u6bb5\u4e0a\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u304c\u30011\u6bb5\u30922\u56de\u9023\u7d9a\u3067\u30b8\u30e3\u30f3\u30d7\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002\u9802\u4e0a\u306b\u767b\u308b\u65b9\u6cd5\u306f\u4f55\u901a\u308a\u3042\u308a\u307e\u3059\u304b\uff1f

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u30013\u6bb5\u76ee\u306b\u767b\u308b\u5b9f\u884c\u53ef\u80fd\u306a\u9078\u629e\u80a2\u306f2\u3064\u3060\u3051\u3067\u30011\u6bb5\u30923\u56de\u9023\u7d9a\u3067\u30b8\u30e3\u30f3\u30d7\u3059\u308b\u9078\u629e\u80a2\u306f\u5236\u7d04\u6761\u4ef6\u3092\u6e80\u305f\u3055\u306a\u3044\u305f\u3081\u7834\u68c4\u3055\u308c\u307e\u3059\u3002

        \u56f3 14-8 \u00a0 \u5236\u7d04\u4ed8\u304d\u30673\u6bb5\u76ee\u306b\u767b\u308b\u5b9f\u884c\u53ef\u80fd\u306a\u9078\u629e\u80a2\u306e\u6570

        \u3053\u306e\u554f\u984c\u3067\u306f\u3001\u524d\u56de\u304c1\u6bb5\u30b8\u30e3\u30f3\u30d7\u3060\u3063\u305f\u5834\u5408\u3001\u6b21\u56de\u306f\u5fc5\u305a2\u6bb5\u30b8\u30e3\u30f3\u30d7\u3067\u306a\u3051\u308c\u3070\u306a\u308a\u307e\u305b\u3093\u3002\u3053\u308c\u306f**\u6b21\u306e\u30b9\u30c6\u30c3\u30d7\u306e\u9078\u629e\u304c\u73fe\u5728\u306e\u72b6\u614b\uff08\u73fe\u5728\u306e\u968e\u6bb5\u6bb5\u6570\uff09\u3060\u3051\u3067\u306f\u72ec\u7acb\u3057\u3066\u6c7a\u5b9a\u3067\u304d\u305a\u3001\u524d\u306e\u72b6\u614b\uff08\u524d\u56de\u306e\u968e\u6bb5\u6bb5\u6570\uff09\u306b\u3082\u4f9d\u5b58\u3059\u308b**\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

        \u3053\u306e\u554f\u984c\u304c\u3082\u306f\u3084\u7121\u8a18\u61b6\u6027\u3092\u6e80\u305f\u3055\u305a\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f \\(dp[i] = dp[i-1] + dp[i-2]\\) \u3082\u5931\u6557\u3059\u308b\u3053\u3068\u306f\u5bb9\u6613\u306b\u308f\u304b\u308a\u307e\u3059\u3002\u306a\u305c\u306a\u3089 \\(dp[i-1]\\) \u306f\u4eca\u56de\u306e1\u6bb5\u30b8\u30e3\u30f3\u30d7\u3092\u8868\u3057\u307e\u3059\u304c\u3001\u591a\u304f\u306e\u300c\u524d\u56de\u304c1\u6bb5\u30b8\u30e3\u30f3\u30d7\u3060\u3063\u305f\u300d\u9078\u629e\u80a2\u3092\u542b\u3093\u3067\u304a\u308a\u3001\u5236\u7d04\u3092\u6e80\u305f\u3059\u305f\u3081\u306b\u306f\u3053\u308c\u3089\u3092\u76f4\u63a5 \\(dp[i]\\) \u306b\u542b\u3081\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002

        \u3053\u306e\u305f\u3081\u3001\u72b6\u614b\u5b9a\u7fa9\u3092\u62e1\u5f35\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff1a\u72b6\u614b \\([i, j]\\) \u306f \\(i\\) \u6bb5\u76ee\u306b\u3044\u3066\u3001\u524d\u56de\u304c \\(j\\) \u6bb5\u30b8\u30e3\u30f3\u30d7\u3060\u3063\u305f\u3053\u3068\u3092\u8868\u3059\u3002\u3053\u3053\u3067 \\(j \\in \\{1, 2\\}\\) \u3067\u3059\u3002\u3053\u306e\u72b6\u614b\u5b9a\u7fa9\u306f\u524d\u56de\u304c1\u6bb5\u30b8\u30e3\u30f3\u30d7\u3060\u3063\u305f\u304b2\u6bb5\u30b8\u30e3\u30f3\u30d7\u3060\u3063\u305f\u304b\u3092\u52b9\u679c\u7684\u306b\u533a\u5225\u3057\u3001\u73fe\u5728\u306e\u72b6\u614b\u304c\u3069\u3053\u304b\u3089\u6765\u305f\u304b\u3092\u9069\u5207\u306b\u5224\u65ad\u3067\u304d\u307e\u3059\u3002

        • \u524d\u56de\u304c1\u6bb5\u30b8\u30e3\u30f3\u30d7\u3060\u3063\u305f\u5834\u5408\u3001\u524d\u3005\u56de\u306f\u5fc5\u305a2\u6bb5\u30b8\u30e3\u30f3\u30d7\u3092\u9078\u629e\u3057\u3066\u3044\u305f\u306f\u305a\u3067\u3059\u3002\u3064\u307e\u308a\u3001\\(dp[i, 1]\\) \u306f \\(dp[i-1, 2]\\) \u304b\u3089\u306e\u307f\u9077\u79fb\u3067\u304d\u307e\u3059\u3002
        • \u524d\u56de\u304c2\u6bb5\u30b8\u30e3\u30f3\u30d7\u3060\u3063\u305f\u5834\u5408\u3001\u524d\u3005\u56de\u306f1\u6bb5\u30b8\u30e3\u30f3\u30d7\u307e\u305f\u306f2\u6bb5\u30b8\u30e3\u30f3\u30d7\u3092\u9078\u629e\u3067\u304d\u307e\u3057\u305f\u3002\u3064\u307e\u308a\u3001\\(dp[i, 2]\\) \u306f \\(dp[i-2, 1]\\) \u307e\u305f\u306f \\(dp[i-2, 2]\\) \u304b\u3089\u9077\u79fb\u3067\u304d\u307e\u3059\u3002

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\\(dp[i, j]\\) \u306f\u72b6\u614b \\([i, j]\\) \u306e\u89e3\u306e\u6570\u3092\u8868\u3057\u307e\u3059\u3002\u3053\u306e\u6642\u70b9\u3067\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u306f\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\uff1a

        \\[ \\begin{cases} dp[i, 1] = dp[i-1, 2] \\\\ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2] \\end{cases} \\]

        \u56f3 14-9 \u00a0 \u5236\u7d04\u3092\u8003\u616e\u3057\u305f\u518d\u5e30\u95a2\u4fc2

        \u6700\u7d42\u7684\u306b\u3001\\(dp[n, 1] + dp[n, 2]\\) \u3092\u8fd4\u305b\u3070\u3088\u304f\u3001\u3053\u306e2\u3064\u306e\u5408\u8a08\u304c \\(n\\) \u6bb5\u76ee\u306b\u767b\u308b\u89e3\u306e\u7dcf\u6570\u3092\u8868\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_constraint_dp.py
        def climbing_stairs_constraint_dp(n: int) -> int:\n    \"\"\"\u5236\u7d04\u4ed8\u304d\u968e\u6bb5\u767b\u308a\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    if n == 1 or n == 2:\n        return 1\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    dp = [[0] * 3 for _ in range(n + 1)]\n    # \u521d\u671f\u72b6\u614b\uff1a\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4e8b\u524d\u8a2d\u5b9a\n    dp[1][1], dp[1][2] = 1, 0\n    dp[2][1], dp[2][2] = 0, 1\n    # \u72b6\u614b\u9077\u79fb\uff1a\u5c0f\u3055\u3044\u90e8\u5206\u554f\u984c\u304b\u3089\u5927\u304d\u3044\u90e8\u5206\u554f\u984c\u3092\u6bb5\u968e\u7684\u306b\u89e3\u304f\n    for i in range(3, n + 1):\n        dp[i][1] = dp[i - 1][2]\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n    return dp[n][1] + dp[n][2]\n
        climbing_stairs_constraint_dp.cpp
        /* \u5236\u7d04\u4ed8\u304d\u968e\u6bb5\u767b\u308a\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint climbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3057\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    vector<vector<int>> dp(n + 1, vector<int>(3, 0));\n    // \u521d\u671f\u72b6\u614b\uff1a\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4e8b\u524d\u8a2d\u5b9a\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // \u72b6\u614b\u9077\u79fb\uff1a\u5c0f\u3055\u306a\u554f\u984c\u304b\u3089\u5927\u304d\u306a\u90e8\u5206\u554f\u984c\u3092\u6bb5\u968e\u7684\u306b\u89e3\u304f\n    for (int i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
        climbing_stairs_constraint_dp.java
        /* \u5236\u7d04\u4ed8\u304d\u968e\u6bb5\u767b\u308a\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint climbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3057\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    int[][] dp = new int[n + 1][3];\n    // \u521d\u671f\u72b6\u614b\uff1a\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4e8b\u524d\u8a2d\u5b9a\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // \u72b6\u614b\u9077\u79fb\uff1a\u5c0f\u3055\u306a\u554f\u984c\u304b\u3089\u5927\u304d\u306a\u90e8\u5206\u554f\u984c\u3092\u6bb5\u968e\u7684\u306b\u89e3\u304f\n    for (int i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
        climbing_stairs_constraint_dp.cs
        [class]{climbing_stairs_constraint_dp}-[func]{ClimbingStairsConstraintDP}\n
        climbing_stairs_constraint_dp.go
        [class]{}-[func]{climbingStairsConstraintDP}\n
        climbing_stairs_constraint_dp.swift
        [class]{}-[func]{climbingStairsConstraintDP}\n
        climbing_stairs_constraint_dp.js
        [class]{}-[func]{climbingStairsConstraintDP}\n
        climbing_stairs_constraint_dp.ts
        [class]{}-[func]{climbingStairsConstraintDP}\n
        climbing_stairs_constraint_dp.dart
        [class]{}-[func]{climbingStairsConstraintDP}\n
        climbing_stairs_constraint_dp.rs
        [class]{}-[func]{climbing_stairs_constraint_dp}\n
        climbing_stairs_constraint_dp.c
        [class]{}-[func]{climbingStairsConstraintDP}\n
        climbing_stairs_constraint_dp.kt
        [class]{}-[func]{climbingStairsConstraintDP}\n
        climbing_stairs_constraint_dp.rb
        [class]{}-[func]{climbing_stairs_constraint_dp}\n

        \u4e0a\u8a18\u306e\u30b1\u30fc\u30b9\u3067\u306f\u3001\u524d\u306e\u72b6\u614b\u306e\u307f\u3092\u8003\u616e\u3059\u308c\u3070\u3088\u3044\u305f\u3081\u3001\u72b6\u614b\u5b9a\u7fa9\u3092\u62e1\u5f35\u3059\u308b\u3053\u3068\u3067\u4f9d\u7136\u3068\u3057\u3066\u7121\u8a18\u61b6\u6027\u3092\u6e80\u305f\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u4e00\u90e8\u306e\u554f\u984c\u3067\u306f\u975e\u5e38\u306b\u6df1\u523b\u306a\u300c\u72b6\u614b\u52b9\u679c\u300d\u304c\u3042\u308a\u307e\u3059\u3002

        \u969c\u5bb3\u7269\u751f\u6210\u4ed8\u304d\u968e\u6bb5\u767b\u308a

        \\(n\\) \u6bb5\u306e\u968e\u6bb5\u304c\u3042\u308a\u3001\u6bce\u56de1\u6bb5\u307e\u305f\u306f2\u6bb5\u4e0a\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002**\\(i\\) \u6bb5\u76ee\u306b\u767b\u3063\u305f\u3068\u304d\u3001\u30b7\u30b9\u30c6\u30e0\u304c\u81ea\u52d5\u7684\u306b \\(2i\\) \u6bb5\u76ee\u306b\u969c\u5bb3\u7269\u3092\u7f6e\u304d\u3001\u305d\u306e\u5f8c\u306e\u3059\u3079\u3066\u306e\u30e9\u30a6\u30f3\u30c9\u3067 \\(2i\\) \u6bb5\u76ee\u306b\u30b8\u30e3\u30f3\u30d7\u3059\u308b\u3053\u3068\u304c\u7981\u6b62\u3055\u308c\u308b**\u3068\u898f\u5b9a\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u6700\u521d\u306e2\u30e9\u30a6\u30f3\u30c9\u30672\u6bb5\u76ee\u30683\u6bb5\u76ee\u306b\u30b8\u30e3\u30f3\u30d7\u3057\u305f\u5834\u5408\u3001\u305d\u306e\u5f8c\u306f4\u6bb5\u76ee\u30686\u6bb5\u76ee\u306b\u30b8\u30e3\u30f3\u30d7\u3067\u304d\u307e\u305b\u3093\u3002\u9802\u4e0a\u306b\u767b\u308b\u65b9\u6cd5\u306f\u4f55\u901a\u308a\u3042\u308a\u307e\u3059\u304b\uff1f

        \u3053\u306e\u554f\u984c\u3067\u306f\u3001\u6b21\u306e\u30b8\u30e3\u30f3\u30d7\u306f\u3059\u3079\u3066\u306e\u904e\u53bb\u306e\u72b6\u614b\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002\u5404\u30b8\u30e3\u30f3\u30d7\u304c\u3088\u308a\u9ad8\u3044\u6bb5\u306b\u969c\u5bb3\u7269\u3092\u7f6e\u304d\u3001\u5c06\u6765\u306e\u30b8\u30e3\u30f3\u30d7\u306b\u5f71\u97ff\u3059\u308b\u304b\u3089\u3067\u3059\u3002\u3053\u306e\u3088\u3046\u306a\u554f\u984c\u3067\u306f\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u3057\u3070\u3057\u3070\u89e3\u6c7a\u306b\u82e6\u52b4\u3057\u307e\u3059\u3002

        \u5b9f\u969b\u3001\u591a\u304f\u306e\u8907\u96d1\u306a\u7d44\u307f\u5408\u308f\u305b\u6700\u9069\u5316\u554f\u984c\uff08\u5de1\u56de\u30bb\u30fc\u30eb\u30b9\u30de\u30f3\u554f\u984c\u306a\u3069\uff09\u306f\u7121\u8a18\u61b6\u6027\u3092\u6e80\u305f\u3057\u307e\u305b\u3093\u3002\u3053\u306e\u3088\u3046\u306a\u554f\u984c\u306b\u5bfe\u3057\u3066\u306f\u3001\u901a\u5e38\u3001\u30d2\u30e5\u30fc\u30ea\u30b9\u30c6\u30a3\u30c3\u30af\u63a2\u7d22\u3001\u907a\u4f1d\u7684\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3001\u5f37\u5316\u5b66\u7fd2\u306a\u3069\u306e\u4ed6\u306e\u65b9\u6cd5\u3092\u9078\u629e\u3057\u3066\u3001\u9650\u3089\u308c\u305f\u6642\u9593\u5185\u306b\u4f7f\u7528\u53ef\u80fd\u306a\u5c40\u6240\u6700\u9069\u89e3\u3092\u5f97\u307e\u3059\u3002

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/","title":"14.3 \u00a0 \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u89e3\u6c7a\u30a2\u30d7\u30ed\u30fc\u30c1","text":"

        \u524d\u306e2\u3064\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u306e\u4e3b\u8981\u306a\u7279\u5fb4\u3092\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002\u6b21\u306b\u3001\u3088\u308a\u5b9f\u7528\u7684\u306a2\u3064\u306e\u554f\u984c\u3092\u4e00\u7dd2\u306b\u63a2\u7d22\u3057\u307e\u3057\u3087\u3046\u3002

        1. \u554f\u984c\u304c\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u304b\u3069\u3046\u304b\u3092\u3069\u306e\u3088\u3046\u306b\u5224\u65ad\u3059\u308b\u304b\uff1f
        2. \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u5b8c\u5168\u306a\u30b9\u30c6\u30c3\u30d7\u306f\u4f55\u304b\uff1f
        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1431","title":"14.3.1 \u00a0 \u554f\u984c\u306e\u5224\u5b9a","text":"

        \u4e00\u822c\u7684\u306b\u8a00\u3048\u3070\u3001\u554f\u984c\u304c\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u3001\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u542b\u307f\u3001\u7121\u8a18\u61b6\u6027\u3092\u793a\u3059\u5834\u5408\u3001\u901a\u5e38\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u89e3\u6cd5\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u554f\u984c\u306e\u8aac\u660e\u304b\u3089\u76f4\u63a5\u3053\u308c\u3089\u306e\u7279\u5fb4\u3092\u62bd\u51fa\u3059\u308b\u3053\u3068\u306f\u3057\u3070\u3057\u3070\u56f0\u96e3\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u901a\u5e38\u306f\u6761\u4ef6\u3092\u7de9\u548c\u3057\u3001**\u307e\u305a\u554f\u984c\u304c\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\uff08\u5168\u63a2\u7d22\uff09\u3092\u4f7f\u7528\u3057\u305f\u89e3\u6c7a\u306b\u9069\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3092\u89b3\u5bdf**\u3057\u307e\u3059\u3002

        **\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306b\u9069\u3057\u305f\u554f\u984c\u306f\u901a\u5e38\u300c\u6c7a\u5b9a\u6728\u30e2\u30c7\u30eb\u300d\u306b\u9069\u5408**\u3057\u3001\u3053\u308c\u306f\u6728\u69cb\u9020\u3092\u4f7f\u7528\u3057\u3066\u8a18\u8ff0\u3067\u304d\u3001\u5404\u30ce\u30fc\u30c9\u306f\u6c7a\u5b9a\u3092\u8868\u3057\u3001\u5404\u30d1\u30b9\u306f\u6c7a\u5b9a\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u8868\u3057\u307e\u3059\u3002

        \u8a00\u3044\u63db\u3048\u308b\u3068\u3001\u554f\u984c\u304c\u660e\u793a\u7684\u306a\u6c7a\u5b9a\u6982\u5ff5\u3092\u542b\u307f\u3001\u89e3\u304c\u4e00\u9023\u306e\u6c7a\u5b9a\u3092\u901a\u3058\u3066\u751f\u6210\u3055\u308c\u308b\u5834\u5408\u3001\u305d\u308c\u306f\u6c7a\u5b9a\u6728\u30e2\u30c7\u30eb\u306b\u9069\u5408\u3057\u3001\u901a\u5e38\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u3066\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002

        \u3053\u306e\u57fa\u790e\u306e\u4e0a\u3067\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u3092\u5224\u5b9a\u3059\u308b\u305f\u3081\u306e\u300c\u30dc\u30fc\u30ca\u30b9\u30dd\u30a4\u30f3\u30c8\u300d\u304c\u3042\u308a\u307e\u3059\u3002

        • \u554f\u984c\u306b\u6700\u5927\u5316\uff08\u6700\u5c0f\u5316\uff09\u307e\u305f\u306f\u6700\u3082\uff08\u6700\u3082\u5c11\u306a\u3044\uff09\u6700\u9069\u306a\u89e3\u3092\u898b\u3064\u3051\u308b\u3068\u3044\u3046\u8a18\u8ff0\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u3002
        • \u554f\u984c\u306e\u72b6\u614b\u304c\u30ea\u30b9\u30c8\u3001\u591a\u6b21\u5143\u884c\u5217\u3001\u307e\u305f\u306f\u6728\u3092\u4f7f\u7528\u3057\u3066\u8868\u73fe\u3067\u304d\u3001\u72b6\u614b\u304c\u305d\u306e\u5468\u56f2\u306e\u72b6\u614b\u3068\u518d\u5e30\u95a2\u4fc2\u3092\u6301\u3063\u3066\u3044\u308b\u3002

        \u5bfe\u5fdc\u3057\u3066\u3001\u300c\u30da\u30ca\u30eb\u30c6\u30a3\u30dd\u30a4\u30f3\u30c8\u300d\u3082\u3042\u308a\u307e\u3059\u3002

        • \u554f\u984c\u306e\u76ee\u6a19\u306f\u6700\u9069\u89e3\u3060\u3051\u3067\u306a\u304f\u3001\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3067\u3042\u308b\u3002
        • \u554f\u984c\u306e\u8aac\u660e\u306b\u9806\u5217\u3068\u7d44\u307f\u5408\u308f\u305b\u306e\u660e\u3089\u304b\u306a\u7279\u5fb4\u304c\u3042\u308a\u3001\u7279\u5b9a\u306e\u8907\u6570\u306e\u89e3\u3092\u8fd4\u3059\u5fc5\u8981\u304c\u3042\u308b\u3002

        \u554f\u984c\u304c\u6c7a\u5b9a\u6728\u30e2\u30c7\u30eb\u306b\u9069\u5408\u3057\u3001\u6bd4\u8f03\u7684\u660e\u3089\u304b\u306a\u300c\u30dc\u30fc\u30ca\u30b9\u30dd\u30a4\u30f3\u30c8\u300d\u3092\u6301\u3064\u5834\u5408\u3001\u305d\u308c\u304c\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u3067\u3042\u308b\u3068\u4eee\u5b9a\u3057\u3001\u89e3\u6c7a\u30d7\u30ed\u30bb\u30b9\u4e2d\u306b\u691c\u8a3c\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1432","title":"14.3.2 \u00a0 \u554f\u984c\u89e3\u6c7a\u30b9\u30c6\u30c3\u30d7","text":"

        \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u89e3\u6c7a\u30d7\u30ed\u30bb\u30b9\u306f\u554f\u984c\u306e\u6027\u8cea\u3068\u96e3\u6613\u5ea6\u306b\u3088\u3063\u3066\u7570\u306a\u308a\u307e\u3059\u304c\u3001\u4e00\u822c\u7684\u306b\u6b21\u306e\u30b9\u30c6\u30c3\u30d7\u306b\u5f93\u3044\u307e\u3059\uff1a\u6c7a\u5b9a\u306e\u8a18\u8ff0\u3001\u72b6\u614b\u306e\u5b9a\u7fa9\u3001\\(dp\\) \u30c6\u30fc\u30d6\u30eb\u306e\u78ba\u7acb\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u306e\u5c0e\u51fa\u3001\u5883\u754c\u6761\u4ef6\u306e\u6c7a\u5b9a\u306a\u3069\u3002

        \u554f\u984c\u89e3\u6c7a\u30b9\u30c6\u30c3\u30d7\u3092\u3088\u308a\u5177\u4f53\u7684\u306b\u8aac\u660e\u3059\u308b\u305f\u3081\u306b\u3001\u53e4\u5178\u7684\u306a\u554f\u984c\u300c\u6700\u5c0f\u7d4c\u8def\u548c\u300d\u3092\u4f8b\u3068\u3057\u3066\u4f7f\u7528\u3057\u307e\u3059\u3002

        Question

        \\(n \\times m\\) \u306e\u4e8c\u6b21\u5143\u30b0\u30ea\u30c3\u30c9 grid \u304c\u4e0e\u3048\u3089\u308c\u3001\u30b0\u30ea\u30c3\u30c9\u306e\u5404\u30bb\u30eb\u306b\u306f\u8ca0\u3067\u306a\u3044\u6574\u6570\u304c\u542b\u307e\u308c\u3001\u305d\u306e\u30bb\u30eb\u306e\u30b3\u30b9\u30c8\u3092\u8868\u3057\u307e\u3059\u3002\u30ed\u30dc\u30c3\u30c8\u306f\u5de6\u4e0a\u306e\u30bb\u30eb\u304b\u3089\u59cb\u307e\u308a\u3001\u5404\u30b9\u30c6\u30c3\u30d7\u3067\u4e0b\u307e\u305f\u306f\u53f3\u306b\u306e\u307f\u79fb\u52d5\u3067\u304d\u3001\u53f3\u4e0b\u306e\u30bb\u30eb\u306b\u5230\u9054\u3059\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002\u5de6\u4e0a\u304b\u3089\u53f3\u4e0b\u3078\u306e\u6700\u5c0f\u7d4c\u8def\u548c\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u4e0b\u306e\u56f3\u306f\u4f8b\u3092\u793a\u3057\u3066\u304a\u308a\u3001\u4e0e\u3048\u3089\u308c\u305f\u30b0\u30ea\u30c3\u30c9\u306e\u6700\u5c0f\u7d4c\u8def\u548c\u306f \\(13\\) \u3067\u3059\u3002

        \u56f3 14-10 \u00a0 \u6700\u5c0f\u7d4c\u8def\u548c\u306e\u4f8b\u30c7\u30fc\u30bf

        \u7b2c1\u30b9\u30c6\u30c3\u30d7\uff1a\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u6c7a\u5b9a\u3092\u8003\u3048\u3001\u72b6\u614b\u3092\u5b9a\u7fa9\u3057\u3001\u305d\u308c\u306b\u3088\u308a \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u3092\u5f97\u308b

        \u3053\u306e\u554f\u984c\u306e\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u6c7a\u5b9a\u306f\u3001\u73fe\u5728\u306e\u30bb\u30eb\u304b\u3089\u4e0b\u307e\u305f\u306f\u53f3\u306b1\u30b9\u30c6\u30c3\u30d7\u79fb\u52d5\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u73fe\u5728\u306e\u30bb\u30eb\u306e\u884c\u3068\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c \\([i, j]\\) \u3067\u3042\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u4e0b\u307e\u305f\u306f\u53f3\u306b\u79fb\u52d5\u3057\u305f\u5f8c\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f \\([i+1, j]\\) \u307e\u305f\u306f \\([i, j+1]\\) \u306b\u306a\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u72b6\u614b\u306b\u306f2\u3064\u306e\u5909\u6570\u304c\u542b\u307e\u308c\u308b\u3079\u304d\u3067\u3059\uff1a\u884c\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3068\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3001\\([i, j]\\) \u3068\u8868\u8a18\u3055\u308c\u307e\u3059\u3002

        \u72b6\u614b \\([i, j]\\) \u306f\u90e8\u5206\u554f\u984c\u306b\u5bfe\u5fdc\u3057\u307e\u3059\uff1a\u958b\u59cb\u70b9 \\([0, 0]\\) \u304b\u3089 \\([i, j]\\) \u3078\u306e\u6700\u5c0f\u7d4c\u8def\u548c\u3001\\(dp[i, j]\\) \u3068\u8868\u8a18\u3055\u308c\u307e\u3059\u3002

        \u3053\u306e\u3088\u3046\u306b\u3057\u3066\u3001\u4e0b\u306e\u56f3\u306b\u793a\u3059\u4e8c\u6b21\u5143 \\(dp\\) \u884c\u5217\u3092\u5f97\u307e\u3059\u3002\u305d\u306e\u30b5\u30a4\u30ba\u306f\u5165\u529b\u30b0\u30ea\u30c3\u30c9 \\(grid\\) \u3068\u540c\u3058\u3067\u3059\u3002

        \u56f3 14-11 \u00a0 \u72b6\u614b\u5b9a\u7fa9\u3068DP\u30c6\u30fc\u30d6\u30eb

        Note

        \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3068\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306f\u6c7a\u5b9a\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3068\u3057\u3066\u8a18\u8ff0\u3067\u304d\u3001\u72b6\u614b\u306f\u3059\u3079\u3066\u306e\u6c7a\u5b9a\u5909\u6570\u304b\u3089\u69cb\u6210\u3055\u308c\u307e\u3059\u3002\u554f\u984c\u89e3\u6c7a\u306e\u9032\u884c\u3092\u8a18\u8ff0\u3059\u308b\u3059\u3079\u3066\u306e\u5909\u6570\u3092\u542b\u3080\u3079\u304d\u3067\u3001\u6b21\u306e\u72b6\u614b\u3092\u5c0e\u51fa\u3059\u308b\u306e\u306b\u5341\u5206\u306a\u60c5\u5831\u3092\u542b\u3093\u3067\u3044\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u5404\u72b6\u614b\u306f\u90e8\u5206\u554f\u984c\u306b\u5bfe\u5fdc\u3057\u3001\u3059\u3079\u3066\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4fdd\u5b58\u3059\u308b\u305f\u3081\u306e \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002\u72b6\u614b\u306e\u5404\u72ec\u7acb\u5909\u6570\u306f \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u306e\u6b21\u5143\u3067\u3059\u3002\u672c\u8cea\u7684\u306b\u3001\\(dp\\) \u30c6\u30fc\u30d6\u30eb\u306f\u72b6\u614b\u3068\u90e8\u5206\u554f\u984c\u306e\u89e3\u306e\u9593\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u3067\u3059\u3002

        \u7b2c2\u30b9\u30c6\u30c3\u30d7\uff1a\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u7279\u5b9a\u3057\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3092\u5c0e\u51fa\u3059\u308b

        \u72b6\u614b \\([i, j]\\) \u306b\u3064\u3044\u3066\u3001\u305d\u308c\u306f\u4e0a\u306e\u30bb\u30eb \\([i-1, j]\\) \u307e\u305f\u306f\u5de6\u306e\u30bb\u30eb \\([i, j-1]\\) \u304b\u3089\u306e\u307f\u5c0e\u51fa\u3067\u304d\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u6700\u9069\u90e8\u5206\u69cb\u9020\u306f\uff1a\\([i, j]\\) \u306b\u5230\u9054\u3059\u308b\u6700\u5c0f\u7d4c\u8def\u548c\u306f\u3001\\([i, j-1]\\) \u3068 \\([i-1, j]\\) \u306e\u6700\u5c0f\u7d4c\u8def\u548c\u306e\u5c0f\u3055\u3044\u65b9\u306b\u3088\u3063\u3066\u6c7a\u5b9a\u3055\u308c\u307e\u3059\u3002

        \u4e0a\u8a18\u306e\u5206\u6790\u306b\u57fa\u3065\u3044\u3066\u3001\u4e0b\u306e\u56f3\u306b\u793a\u3059\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3092\u5c0e\u51fa\u3067\u304d\u307e\u3059\uff1a

        \\[ dp[i, j] = \\min(dp[i-1, j], dp[i, j-1]) + grid[i, j] \\]

        \u56f3 14-12 \u00a0 \u6700\u9069\u90e8\u5206\u69cb\u9020\u3068\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f

        Note

        \u5b9a\u7fa9\u3055\u308c\u305f \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u306b\u57fa\u3065\u3044\u3066\u3001\u5143\u306e\u554f\u984c\u3068\u90e8\u5206\u554f\u984c\u306e\u95a2\u4fc2\u3092\u8003\u3048\u3001\u90e8\u5206\u554f\u984c\u306e\u6700\u9069\u89e3\u304b\u3089\u5143\u306e\u554f\u984c\u306e\u6700\u9069\u89e3\u3092\u3069\u306e\u3088\u3046\u306b\u69cb\u7bc9\u3059\u308b\u304b\u3001\u3064\u307e\u308a\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u898b\u3064\u3051\u307e\u3059\u3002

        \u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u7279\u5b9a\u3057\u305f\u3089\u3001\u305d\u308c\u3092\u4f7f\u7528\u3057\u3066\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3092\u69cb\u7bc9\u3067\u304d\u307e\u3059\u3002

        \u7b2c3\u30b9\u30c6\u30c3\u30d7\uff1a\u5883\u754c\u6761\u4ef6\u3068\u72b6\u614b\u9077\u79fb\u9806\u5e8f\u3092\u6c7a\u5b9a\u3059\u308b

        \u3053\u306e\u554f\u984c\u3067\u306f\u3001\u6700\u521d\u306e\u884c\u306e\u72b6\u614b\u306f\u5de6\u306e\u72b6\u614b\u304b\u3089\u306e\u307f\u6765\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u6700\u521d\u306e\u5217\u306e\u72b6\u614b\u306f\u4e0a\u306e\u72b6\u614b\u304b\u3089\u306e\u307f\u6765\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u305f\u3081\u3001\u6700\u521d\u306e\u884c \\(i = 0\\) \u3068\u6700\u521d\u306e\u5217 \\(j = 0\\) \u304c\u5883\u754c\u6761\u4ef6\u3067\u3059\u3002

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5404\u30bb\u30eb\u306f\u5de6\u306e\u30bb\u30eb\u3068\u4e0a\u306e\u30bb\u30eb\u304b\u3089\u5c0e\u51fa\u3055\u308c\u308b\u305f\u3081\u3001\u30eb\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u3066\u884c\u5217\u3092\u8d70\u67fb\u3057\u3001\u5916\u5074\u306e\u30eb\u30fc\u30d7\u306f\u884c\u3092\u53cd\u5fa9\u3057\u3001\u5185\u5074\u306e\u30eb\u30fc\u30d7\u306f\u5217\u3092\u53cd\u5fa9\u3057\u307e\u3059\u3002

        \u56f3 14-13 \u00a0 \u5883\u754c\u6761\u4ef6\u3068\u72b6\u614b\u9077\u79fb\u9806\u5e8f

        Note

        \u5883\u754c\u6761\u4ef6\u306f\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3067 \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u3001\u63a2\u7d22\u3067\u306f\u679d\u5208\u308a\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002

        \u72b6\u614b\u9077\u79fb\u9806\u5e8f\u306e\u6838\u5fc3\u306f\u3001\u73fe\u5728\u306e\u554f\u984c\u306e\u89e3\u3092\u8a08\u7b97\u3059\u308b\u3068\u304d\u3001\u305d\u308c\u304c\u4f9d\u5b58\u3059\u308b\u3059\u3079\u3066\u306e\u5c0f\u3055\u306a\u90e8\u5206\u554f\u984c\u304c\u65e2\u306b\u6b63\u3057\u304f\u8a08\u7b97\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u4fdd\u3059\u308b\u3053\u3068\u3067\u3059\u3002

        \u4e0a\u8a18\u306e\u5206\u6790\u306b\u57fa\u3065\u3044\u3066\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30b3\u30fc\u30c9\u3092\u76f4\u63a5\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u90e8\u5206\u554f\u984c\u306e\u5206\u89e3\u306f\u30c8\u30c3\u30d7\u30c0\u30a6\u30f3\u30a2\u30d7\u30ed\u30fc\u30c1\u3067\u3042\u308b\u305f\u3081\u3001\u300c\u529b\u4efb\u305b\u63a2\u7d22 \u2192 \u30e1\u30e2\u5316\u63a2\u7d22 \u2192 \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u300d\u306e\u9806\u5e8f\u3067\u5b9f\u88c5\u3059\u308b\u3053\u3068\u304c\u7fd2\u6163\u7684\u306a\u601d\u8003\u306b\u3088\u308a\u9069\u5408\u3057\u307e\u3059\u3002

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1-1","title":"1. \u00a0 \u65b9\u6cd51\uff1a\u529b\u4efb\u305b\u63a2\u7d22","text":"

        \u72b6\u614b \\([i, j]\\) \u304b\u3089\u63a2\u7d22\u3092\u958b\u59cb\u3057\u3001\u305d\u308c\u3092\u5e38\u306b\u3088\u308a\u5c0f\u3055\u306a\u72b6\u614b \\([i-1, j]\\) \u3068 \\([i, j-1]\\) \u306b\u5206\u89e3\u3057\u307e\u3059\u3002\u518d\u5e30\u95a2\u6570\u306b\u306f\u4ee5\u4e0b\u306e\u8981\u7d20\u304c\u542b\u307e\u308c\u307e\u3059\u3002

        • \u518d\u5e30\u30d1\u30e9\u30e1\u30fc\u30bf\uff1a\u72b6\u614b \\([i, j]\\)\u3002
        • \u623b\u308a\u5024\uff1a\\([0, 0]\\) \u304b\u3089 \\([i, j]\\) \u3078\u306e\u6700\u5c0f\u7d4c\u8def\u548c \\(dp[i, j]\\)\u3002
        • \u7d42\u4e86\u6761\u4ef6\uff1a\\(i = 0\\) \u304b\u3064 \\(j = 0\\) \u306e\u3068\u304d\u3001\u30b3\u30b9\u30c8 \\(grid[0, 0]\\) \u3092\u8fd4\u3059\u3002
        • \u679d\u5208\u308a\uff1a\\(i < 0\\) \u307e\u305f\u306f \\(j < 0\\) \u3067\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u3068\u304d\u3001\u30b3\u30b9\u30c8 \\(+\\infty\\) \u3092\u8fd4\u3057\u3001\u5b9f\u884c\u4e0d\u53ef\u80fd\u6027\u3092\u8868\u3059\u3002

        \u5b9f\u88c5\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
        def min_path_sum_dfs(grid: list[list[int]], i: int, j: int) -> int:\n    \"\"\"\u6700\u5c0f\u30d1\u30b9\u548c\uff1a\u30d6\u30eb\u30fc\u30c8\u30d5\u30a9\u30fc\u30b9\u63a2\u7d22\"\"\"\n    # \u5de6\u4e0a\u306e\u30bb\u30eb\u306e\u5834\u5408\u3001\u63a2\u7d22\u3092\u7d42\u4e86\n    if i == 0 and j == 0:\n        return grid[0][0]\n    # \u884c\u307e\u305f\u306f\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001+\u221e \u30b3\u30b9\u30c8\u3092\u8fd4\u3059\n    if i < 0 or j < 0:\n        return inf\n    # \u5de6\u4e0a\u304b\u3089 (i-1, j) \u3068 (i, j-1) \u3078\u306e\u6700\u5c0f\u30d1\u30b9\u30b3\u30b9\u30c8\u3092\u8a08\u7b97\n    up = min_path_sum_dfs(grid, i - 1, j)\n    left = min_path_sum_dfs(grid, i, j - 1)\n    # \u5de6\u4e0a\u304b\u3089 (i, j) \u3078\u306e\u6700\u5c0f\u30d1\u30b9\u30b3\u30b9\u30c8\u3092\u8fd4\u3059\n    return min(left, up) + grid[i][j]\n
        min_path_sum.cpp
        /* \u6700\u5c0f\u30d1\u30b9\u548c\uff1a\u30d6\u30eb\u30fc\u30c8\u30d5\u30a9\u30fc\u30b9\u63a2\u7d22 */\nint minPathSumDFS(vector<vector<int>> &grid, int i, int j) {\n    // \u5de6\u4e0a\u306e\u30bb\u30eb\u306e\u5834\u5408\u3001\u63a2\u7d22\u3092\u7d42\u4e86\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u884c\u307e\u305f\u306f\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001+\u221e \u306e\u30b3\u30b9\u30c8\u3092\u8fd4\u3059\n    if (i < 0 || j < 0) {\n        return INT_MAX;\n    }\n    // \u5de6\u4e0a\u304b\u3089 (i-1, j) \u3068 (i, j-1) \u3078\u306e\u6700\u5c0f\u30d1\u30b9\u30b3\u30b9\u30c8\u3092\u8a08\u7b97\n    int up = minPathSumDFS(grid, i - 1, j);\n    int left = minPathSumDFS(grid, i, j - 1);\n    // \u5de6\u4e0a\u304b\u3089 (i, j) \u3078\u306e\u6700\u5c0f\u30d1\u30b9\u30b3\u30b9\u30c8\u3092\u8fd4\u3059\n    return min(left, up) + grid[i][j];\n}\n
        min_path_sum.java
        /* \u6700\u5c0f\u30d1\u30b9\u548c\uff1a\u30d6\u30eb\u30fc\u30c8\u30d5\u30a9\u30fc\u30b9\u63a2\u7d22 */\nint minPathSumDFS(int[][] grid, int i, int j) {\n    // \u5de6\u4e0a\u306e\u30bb\u30eb\u306e\u5834\u5408\u3001\u63a2\u7d22\u3092\u7d42\u4e86\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u884c\u307e\u305f\u306f\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001+\u221e \u306e\u30b3\u30b9\u30c8\u3092\u8fd4\u3059\n    if (i < 0 || j < 0) {\n        return Integer.MAX_VALUE;\n    }\n    // \u5de6\u4e0a\u304b\u3089 (i-1, j) \u3068 (i, j-1) \u3078\u306e\u6700\u5c0f\u30d1\u30b9\u30b3\u30b9\u30c8\u3092\u8a08\u7b97\n    int up = minPathSumDFS(grid, i - 1, j);\n    int left = minPathSumDFS(grid, i, j - 1);\n    // \u5de6\u4e0a\u304b\u3089 (i, j) \u3078\u306e\u6700\u5c0f\u30d1\u30b9\u30b3\u30b9\u30c8\u3092\u8fd4\u3059\n    return Math.min(left, up) + grid[i][j];\n}\n
        min_path_sum.cs
        [class]{min_path_sum}-[func]{MinPathSumDFS}\n
        min_path_sum.go
        [class]{}-[func]{minPathSumDFS}\n
        min_path_sum.swift
        [class]{}-[func]{minPathSumDFS}\n
        min_path_sum.js
        [class]{}-[func]{minPathSumDFS}\n
        min_path_sum.ts
        [class]{}-[func]{minPathSumDFS}\n
        min_path_sum.dart
        [class]{}-[func]{minPathSumDFS}\n
        min_path_sum.rs
        [class]{}-[func]{min_path_sum_dfs}\n
        min_path_sum.c
        [class]{}-[func]{minPathSumDFS}\n
        min_path_sum.kt
        [class]{}-[func]{minPathSumDFS}\n
        min_path_sum.rb
        [class]{}-[func]{min_path_sum_dfs}\n

        \u4e0b\u306e\u56f3\u306f \\(dp[2, 1]\\) \u3092\u6839\u3068\u3059\u308b\u518d\u5e30\u6728\u3092\u793a\u3057\u3066\u304a\u308a\u3001\u3044\u304f\u3064\u304b\u306e\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u3092\u542b\u307f\u3001\u305d\u306e\u6570\u306f\u30b0\u30ea\u30c3\u30c9 grid \u306e\u30b5\u30a4\u30ba\u304c\u5897\u52a0\u3059\u308b\u3068\u6025\u6fc0\u306b\u5897\u52a0\u3057\u307e\u3059\u3002

        \u672c\u8cea\u7684\u306b\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u306e\u7406\u7531\u306f\uff1a**\u5de6\u4e0a\u9685\u304b\u3089\u7279\u5b9a\u306e\u30bb\u30eb\u306b\u5230\u9054\u3059\u308b\u8907\u6570\u306e\u30d1\u30b9\u304c\u5b58\u5728\u3059\u308b**\u3053\u3068\u3067\u3059\u3002

        \u56f3 14-14 \u00a0 \u529b\u4efb\u305b\u63a2\u7d22\u306e\u518d\u5e30\u6728

        \u5404\u72b6\u614b\u306b\u306f\u4e0b\u3068\u53f3\u306e2\u3064\u306e\u9078\u629e\u304c\u3042\u308b\u305f\u3081\u3001\u5de6\u4e0a\u9685\u304b\u3089\u53f3\u4e0b\u9685\u307e\u3067\u306e\u7dcf\u30b9\u30c6\u30c3\u30d7\u6570\u306f \\(m + n - 2\\) \u3067\u3001\u6700\u60aa\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(2^{m + n})\\) \u3067\u3059\u3002\u3053\u306e\u8a08\u7b97\u65b9\u6cd5\u306f\u30b0\u30ea\u30c3\u30c9\u30a8\u30c3\u30b8\u8fd1\u304f\u306e\u72b6\u6cc1\u3092\u8003\u616e\u3057\u3066\u3044\u306a\u3044\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u30a8\u30c3\u30b8\u306b\u5230\u9054\u3057\u305f\u3068\u304d\u3001\u9078\u629e\u80a2\u304c1\u3064\u3057\u304b\u6b8b\u3089\u306a\u3044\u305f\u3081\u3001\u5b9f\u969b\u306e\u30d1\u30b9\u6570\u306f\u3088\u308a\u5c11\u306a\u304f\u306a\u308a\u307e\u3059\u3002

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#2-2","title":"2. \u00a0 \u65b9\u6cd52\uff1a\u30e1\u30e2\u5316\u63a2\u7d22","text":"

        \u30b0\u30ea\u30c3\u30c9 grid \u3068\u540c\u3058\u30b5\u30a4\u30ba\u306e\u30e1\u30e2\u30ea\u30b9\u30c8 mem \u3092\u5c0e\u5165\u3057\u3001\u69d8\u3005\u306a\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u8a18\u9332\u3057\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u3092\u679d\u5208\u308a\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
        def min_path_sum_dfs_mem(\n    grid: list[list[int]], mem: list[list[int]], i: int, j: int\n) -> int:\n    \"\"\"\u6700\u5c0f\u30d1\u30b9\u548c\uff1a\u8a18\u61b6\u5316\u63a2\u7d22\"\"\"\n    # \u5de6\u4e0a\u306e\u30bb\u30eb\u306e\u5834\u5408\u3001\u63a2\u7d22\u3092\u7d42\u4e86\n    if i == 0 and j == 0:\n        return grid[0][0]\n    # \u884c\u307e\u305f\u306f\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001+\u221e \u30b3\u30b9\u30c8\u3092\u8fd4\u3059\n    if i < 0 or j < 0:\n        return inf\n    # \u8a18\u9332\u304c\u3042\u308b\u5834\u5408\u3001\u305d\u308c\u3092\u8fd4\u3059\n    if mem[i][j] != -1:\n        return mem[i][j]\n    # \u5de6\u3068\u4e0a\u306e\u30bb\u30eb\u304b\u3089\u306e\u6700\u5c0f\u30d1\u30b9\u30b3\u30b9\u30c8\n    up = min_path_sum_dfs_mem(grid, mem, i - 1, j)\n    left = min_path_sum_dfs_mem(grid, mem, i, j - 1)\n    # \u5de6\u4e0a\u304b\u3089 (i, j) \u3078\u306e\u6700\u5c0f\u30d1\u30b9\u30b3\u30b9\u30c8\u3092\u8a18\u9332\u3057\u3066\u8fd4\u3059\n    mem[i][j] = min(left, up) + grid[i][j]\n    return mem[i][j]\n
        min_path_sum.cpp
        [class]{}-[func]{minPathSumDFSMem}\n
        min_path_sum.java
        /* \u6700\u5c0f\u30d1\u30b9\u548c\uff1a\u30e1\u30e2\u5316\u63a2\u7d22 */\nint minPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n    // \u5de6\u4e0a\u306e\u30bb\u30eb\u306e\u5834\u5408\u3001\u63a2\u7d22\u3092\u7d42\u4e86\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u884c\u307e\u305f\u306f\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001+\u221e \u306e\u30b3\u30b9\u30c8\u3092\u8fd4\u3059\n    if (i < 0 || j < 0) {\n        return Integer.MAX_VALUE;\n    }\n    // \u8a18\u9332\u304c\u3042\u308b\u5834\u5408\u3001\u305d\u308c\u3092\u8fd4\u3059\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // \u5de6\u3068\u4e0a\u306e\u30bb\u30eb\u304b\u3089\u306e\u6700\u5c0f\u30d1\u30b9\u30b3\u30b9\u30c8\n    int up = minPathSumDFSMem(grid, mem, i - 1, j);\n    int left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // \u5de6\u4e0a\u304b\u3089 (i, j) \u3078\u306e\u6700\u5c0f\u30d1\u30b9\u30b3\u30b9\u30c8\u3092\u8a18\u9332\u3057\u3066\u8fd4\u3059\n    mem[i][j] = Math.min(left, up) + grid[i][j];\n    return mem[i][j];\n}\n
        min_path_sum.cs
        [class]{min_path_sum}-[func]{MinPathSumDFSMem}\n
        min_path_sum.go
        [class]{}-[func]{minPathSumDFSMem}\n
        min_path_sum.swift
        [class]{}-[func]{minPathSumDFSMem}\n
        min_path_sum.js
        [class]{}-[func]{minPathSumDFSMem}\n
        min_path_sum.ts
        [class]{}-[func]{minPathSumDFSMem}\n
        min_path_sum.dart
        [class]{}-[func]{minPathSumDFSMem}\n
        min_path_sum.rs
        [class]{}-[func]{min_path_sum_dfs_mem}\n
        min_path_sum.c
        [class]{}-[func]{minPathSumDFSMem}\n
        min_path_sum.kt
        [class]{}-[func]{minPathSumDFSMem}\n
        min_path_sum.rb
        [class]{}-[func]{min_path_sum_dfs_mem}\n

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30e1\u30e2\u5316\u3092\u5c0e\u5165\u3057\u305f\u5f8c\u3001\u3059\u3079\u3066\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u306f\u4e00\u5ea6\u3060\u3051\u8a08\u7b97\u3055\u308c\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\u72b6\u614b\u306e\u7dcf\u6570\u3001\u3064\u307e\u308a\u30b0\u30ea\u30c3\u30c9\u30b5\u30a4\u30ba \\(O(nm)\\) \u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002

        \u56f3 14-15 \u00a0 \u30e1\u30e2\u5316\u63a2\u7d22\u306e\u518d\u5e30\u6728

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#3-3","title":"3. \u00a0 \u65b9\u6cd53\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0","text":"

        \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u89e3\u6cd5\u3092\u53cd\u5fa9\u7684\u306b\u5b9f\u88c5\u3057\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
        def min_path_sum_dp(grid: list[list[int]]) -> int:\n    \"\"\"\u6700\u5c0f\u30d1\u30b9\u548c\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n, m = len(grid), len(grid[0])\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    dp = [[0] * m for _ in range(n)]\n    dp[0][0] = grid[0][0]\n    # \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\n    for j in range(1, m):\n        dp[0][j] = dp[0][j - 1] + grid[0][j]\n    # \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u5217\n    for i in range(1, n):\n        dp[i][0] = dp[i - 1][0] + grid[i][0]\n    # \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\u3068\u5217\n    for i in range(1, n):\n        for j in range(1, m):\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n    return dp[n - 1][m - 1]\n
        min_path_sum.cpp
        /* \u6700\u5c0f\u30d1\u30b9\u548c\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint minPathSumDP(vector<vector<int>> &grid) {\n    int n = grid.size(), m = grid[0].size();\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    vector<vector<int>> dp(n, vector<int>(m));\n    dp[0][0] = grid[0][0];\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\n    for (int j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u5217\n    for (int i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\u3068\u5217\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
        min_path_sum.java
        /* \u6700\u5c0f\u30d1\u30b9\u548c\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint minPathSumDP(int[][] grid) {\n    int n = grid.length, m = grid[0].length;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    int[][] dp = new int[n][m];\n    dp[0][0] = grid[0][0];\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\n    for (int j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u5217\n    for (int i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\u3068\u5217\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
        min_path_sum.cs
        [class]{min_path_sum}-[func]{MinPathSumDP}\n
        min_path_sum.go
        [class]{}-[func]{minPathSumDP}\n
        min_path_sum.swift
        [class]{}-[func]{minPathSumDP}\n
        min_path_sum.js
        [class]{}-[func]{minPathSumDP}\n
        min_path_sum.ts
        [class]{}-[func]{minPathSumDP}\n
        min_path_sum.dart
        [class]{}-[func]{minPathSumDP}\n
        min_path_sum.rs
        [class]{}-[func]{min_path_sum_dp}\n
        min_path_sum.c
        [class]{}-[func]{minPathSumDP}\n
        min_path_sum.kt
        [class]{}-[func]{minPathSumDP}\n
        min_path_sum.rb
        [class]{}-[func]{min_path_sum_dp}\n

        \u4e0b\u306e\u56f3\u306f\u6700\u5c0f\u7d4c\u8def\u548c\u306e\u72b6\u614b\u9077\u79fb\u30d7\u30ed\u30bb\u30b9\u3092\u793a\u3057\u3001\u30b0\u30ea\u30c3\u30c9\u5168\u4f53\u3092\u8d70\u67fb\u3059\u308b\u305f\u3081\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(nm)\\) \u3067\u3059\u3002

        \u914d\u5217 dp \u306e\u30b5\u30a4\u30ba\u306f \\(n \\times m\\) \u3067\u3042\u308b\u305f\u3081\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(nm)\\) \u3067\u3059\u3002

        <1><2><3><4><5><6><7><8><9><10><11><12>

        \u56f3 14-16 \u00a0 \u6700\u5c0f\u7d4c\u8def\u548c\u306e\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#4","title":"4. \u00a0 \u7a7a\u9593\u6700\u9069\u5316","text":"

        \u5404\u30bb\u30eb\u306f\u5de6\u3068\u4e0a\u306e\u30bb\u30eb\u306e\u307f\u306b\u95a2\u9023\u3059\u308b\u305f\u3081\u3001\u5358\u4e00\u884c\u914d\u5217\u3092\u4f7f\u7528\u3057\u3066 \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002

        \u914d\u5217 dp \u306f1\u884c\u306e\u72b6\u614b\u306e\u307f\u3092\u8868\u73fe\u3067\u304d\u308b\u305f\u3081\u3001\u6700\u521d\u306e\u5217\u306e\u72b6\u614b\u3092\u4e8b\u524d\u306b\u521d\u671f\u5316\u3067\u304d\u305a\u3001\u5404\u884c\u3092\u8d70\u67fb\u3059\u308b\u3068\u304d\u306b\u66f4\u65b0\u3059\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
        def min_path_sum_dp_comp(grid: list[list[int]]) -> int:\n    \"\"\"\u6700\u5c0f\u30d1\u30b9\u548c\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n, m = len(grid), len(grid[0])\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    dp = [0] * m\n    # \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\n    dp[0] = grid[0][0]\n    for j in range(1, m):\n        dp[j] = dp[j - 1] + grid[0][j]\n    # \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\n    for i in range(1, n):\n        # \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u5217\n        dp[0] = dp[0] + grid[i][0]\n        # \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u5217\n        for j in range(1, m):\n            dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n    return dp[m - 1]\n
        min_path_sum.cpp
        [class]{}-[func]{minPathSumDPComp}\n
        min_path_sum.java
        /* \u6700\u5c0f\u30d1\u30b9\u548c\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint minPathSumDPComp(int[][] grid) {\n    int n = grid.length, m = grid[0].length;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    int[] dp = new int[m];\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\n    dp[0] = grid[0][0];\n    for (int j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\n    for (int i = 1; i < n; i++) {\n        // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u5217\n        dp[0] = dp[0] + grid[i][0];\n        // \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u5217\n        for (int j = 1; j < m; j++) {\n            dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
        min_path_sum.cs
        [class]{min_path_sum}-[func]{MinPathSumDPComp}\n
        min_path_sum.go
        [class]{}-[func]{minPathSumDPComp}\n
        min_path_sum.swift
        [class]{}-[func]{minPathSumDPComp}\n
        min_path_sum.js
        [class]{}-[func]{minPathSumDPComp}\n
        min_path_sum.ts
        [class]{}-[func]{minPathSumDPComp}\n
        min_path_sum.dart
        [class]{}-[func]{minPathSumDPComp}\n
        min_path_sum.rs
        [class]{}-[func]{min_path_sum_dp_comp}\n
        min_path_sum.c
        [class]{}-[func]{minPathSumDPComp}\n
        min_path_sum.kt
        [class]{}-[func]{minPathSumDPComp}\n
        min_path_sum.rb
        [class]{}-[func]{min_path_sum_dp_comp}\n
        "},{"location":"chapter_dynamic_programming/edit_distance_problem/","title":"14.6 \u00a0 \u7de8\u96c6\u8ddd\u96e2\u554f\u984c","text":"

        \u7de8\u96c6\u8ddd\u96e2\u306f\u3001\u30ec\u30fc\u30d9\u30f3\u30b7\u30e5\u30bf\u30a4\u30f3\u8ddd\u96e2\u3068\u3082\u547c\u3070\u308c\u3001\u4e00\u3064\u306e\u6587\u5b57\u5217\u3092\u5225\u306e\u6587\u5b57\u5217\u306b\u5909\u63db\u3059\u308b\u305f\u3081\u306b\u5fc5\u8981\u306a\u6700\u5c0f\u4fee\u6b63\u56de\u6570\u3092\u6307\u3057\u3001\u60c5\u5831\u691c\u7d22\u3084\u81ea\u7136\u8a00\u8a9e\u51e6\u7406\u30672\u3064\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u9593\u306e\u985e\u4f3c\u5ea6\u3092\u6e2c\u5b9a\u3059\u308b\u305f\u3081\u306b\u3088\u304f\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002

        Question

        2\u3064\u306e\u6587\u5b57\u5217 \\(s\\) \u3068 \\(t\\) \u304c\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001\\(s\\) \u3092 \\(t\\) \u306b\u5909\u63db\u3059\u308b\u305f\u3081\u306b\u5fc5\u8981\u306a\u6700\u5c0f\u7de8\u96c6\u56de\u6570\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u6587\u5b57\u5217\u306b\u5bfe\u3057\u30663\u7a2e\u985e\u306e\u7de8\u96c6\u3092\u5b9f\u884c\u3067\u304d\u307e\u3059\uff1a\u6587\u5b57\u306e\u633f\u5165\u3001\u6587\u5b57\u306e\u524a\u9664\u3001\u307e\u305f\u306f\u6587\u5b57\u3092\u4ed6\u306e\u4efb\u610f\u306e\u6587\u5b57\u306b\u7f6e\u63db\u3002

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001kitten \u3092 sitting \u306b\u5909\u63db\u3059\u308b\u306b\u306f3\u56de\u306e\u7de8\u96c6\u304c\u5fc5\u8981\u3067\u30012\u56de\u306e\u7f6e\u63db\u30681\u56de\u306e\u633f\u5165\u3092\u542b\u307f\u307e\u3059\u3002hello \u3092 algo \u306b\u5909\u63db\u3059\u308b\u306b\u306f3\u30b9\u30c6\u30c3\u30d7\u304c\u5fc5\u8981\u3067\u30012\u56de\u306e\u7f6e\u63db\u30681\u56de\u306e\u524a\u9664\u3092\u542b\u307f\u307e\u3059\u3002

        \u56f3 14-27 \u00a0 \u7de8\u96c6\u8ddd\u96e2\u306e\u4f8b\u30c7\u30fc\u30bf

        \u7de8\u96c6\u8ddd\u96e2\u554f\u984c\u306f\u6c7a\u5b9a\u6728\u30e2\u30c7\u30eb\u3067\u81ea\u7136\u306b\u8aac\u660e\u3067\u304d\u307e\u3059\u3002\u6587\u5b57\u5217\u306f\u6728\u306e\u30ce\u30fc\u30c9\u306b\u5bfe\u5fdc\u3057\u30011\u30e9\u30a6\u30f3\u30c9\u306e\u6c7a\u5b9a\uff08\u7de8\u96c6\u64cd\u4f5c\uff09\u306f\u6728\u306e\u30a8\u30c3\u30b8\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u64cd\u4f5c\u306b\u5236\u9650\u304c\u306a\u3044\u5834\u5408\u3001\u5404\u30ce\u30fc\u30c9\u306f\u591a\u304f\u306e\u30a8\u30c3\u30b8\u3092\u5c0e\u51fa\u3067\u304d\u3001\u305d\u308c\u305e\u308c\u304c1\u3064\u306e\u64cd\u4f5c\u306b\u5bfe\u5fdc\u3059\u308b\u305f\u3081\u3001hello \u3092 algo \u306b\u5909\u63db\u3059\u308b\u53ef\u80fd\u306a\u7d4c\u8def\u306f\u591a\u6570\u3042\u308a\u307e\u3059\u3002

        \u6c7a\u5b9a\u6728\u306e\u89b3\u70b9\u304b\u3089\u3001\u3053\u306e\u554f\u984c\u306e\u76ee\u6a19\u306f\u3001\u30ce\u30fc\u30c9 hello \u3068\u30ce\u30fc\u30c9 algo \u306e\u9593\u306e\u6700\u77ed\u7d4c\u8def\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3067\u3059\u3002

        \u56f3 14-28 \u00a0 \u6c7a\u5b9a\u6728\u30e2\u30c7\u30eb\u306b\u57fa\u3065\u3044\u3066\u8868\u73fe\u3055\u308c\u305f\u7de8\u96c6\u8ddd\u96e2\u554f\u984c

        "},{"location":"chapter_dynamic_programming/edit_distance_problem/#1","title":"1. \u00a0 \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30a2\u30d7\u30ed\u30fc\u30c1","text":"

        \u30b9\u30c6\u30c3\u30d71\uff1a\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u6c7a\u5b9a\u3092\u8003\u3048\u3001\u72b6\u614b\u3092\u5b9a\u7fa9\u3057\u3001\u305d\u308c\u306b\u3088\u308a \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u3092\u5f97\u308b

        \u5404\u30e9\u30a6\u30f3\u30c9\u306e\u6c7a\u5b9a\u306f\u3001\u6587\u5b57\u5217 \\(s\\) \u306b\u5bfe\u3057\u30661\u3064\u306e\u7de8\u96c6\u64cd\u4f5c\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u3092\u542b\u307f\u307e\u3059\u3002

        \u7de8\u96c6\u30d7\u30ed\u30bb\u30b9\u4e2d\u306b\u554f\u984c\u306e\u30b5\u30a4\u30ba\u3092\u6bb5\u968e\u7684\u306b\u7e2e\u5c0f\u3059\u308b\u3053\u3068\u3092\u76ee\u6307\u3057\u3001\u3053\u308c\u306b\u3088\u308a\u90e8\u5206\u554f\u984c\u3092\u69cb\u7bc9\u3067\u304d\u307e\u3059\u3002\u6587\u5b57\u5217 \\(s\\) \u3068 \\(t\\) \u306e\u9577\u3055\u3092\u305d\u308c\u305e\u308c \\(n\\) \u3068 \\(m\\) \u3068\u3057\u307e\u3059\u3002\u307e\u305a\u3001\u4e21\u65b9\u306e\u6587\u5b57\u5217\u306e\u672b\u5c3e\u6587\u5b57 \\(s[n-1]\\) \u3068 \\(t[m-1]\\) \u3092\u8003\u616e\u3057\u307e\u3059\u3002

        • \\(s[n-1]\\) \u3068 \\(t[m-1]\\) \u304c\u540c\u3058\u5834\u5408\u3001\u305d\u308c\u3089\u3092\u30b9\u30ad\u30c3\u30d7\u3057\u3066\u76f4\u63a5 \\(s[n-2]\\) \u3068 \\(t[m-2]\\) \u3092\u8003\u616e\u3067\u304d\u307e\u3059\u3002
        • \\(s[n-1]\\) \u3068 \\(t[m-1]\\) \u304c\u7570\u306a\u308b\u5834\u5408\u3001\\(s\\) \u306b\u5bfe\u3057\u30661\u3064\u306e\u7de8\u96c6\uff08\u633f\u5165\u3001\u524a\u9664\u3001\u7f6e\u63db\uff09\u3092\u5b9f\u884c\u3057\u3066\u30012\u3064\u306e\u6587\u5b57\u5217\u306e\u672b\u5c3e\u6587\u5b57\u3092\u4e00\u81f4\u3055\u305b\u3001\u305d\u308c\u3089\u3092\u30b9\u30ad\u30c3\u30d7\u3057\u3066\u3088\u308a\u5c0f\u898f\u6a21\u306a\u554f\u984c\u3092\u8003\u616e\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u3057\u305f\u304c\u3063\u3066\u3001\u6587\u5b57\u5217 \\(s\\) \u3067\u306e\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u6c7a\u5b9a\uff08\u7de8\u96c6\u64cd\u4f5c\uff09\u306f\u3001\\(s\\) \u3068 \\(t\\) \u3067\u30de\u30c3\u30c1\u3055\u308c\u308b\u6b8b\u308a\u306e\u6587\u5b57\u3092\u5909\u66f4\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u72b6\u614b\u306f \\(s\\) \u3068 \\(t\\) \u3067\u73fe\u5728\u8003\u616e\u3055\u308c\u3066\u3044\u308b \\(i\\) \u756a\u76ee\u3068 \\(j\\) \u756a\u76ee\u306e\u6587\u5b57\u3067\u3042\u308a\u3001\\([i, j]\\) \u3068\u8868\u8a18\u3055\u308c\u307e\u3059\u3002

        \u72b6\u614b \\([i, j]\\) \u306f\u90e8\u5206\u554f\u984c\u306b\u5bfe\u5fdc\u3057\u307e\u3059\uff1a\\(s\\) \u306e\u6700\u521d\u306e \\(i\\) \u6587\u5b57\u3092 \\(t\\) \u306e\u6700\u521d\u306e \\(j\\) \u6587\u5b57\u306b\u5909\u66f4\u3059\u308b\u305f\u3081\u306b\u5fc5\u8981\u306a\u6700\u5c0f\u7de8\u96c6\u56de\u6570\u3002

        \u3053\u308c\u304b\u3089\u3001\u30b5\u30a4\u30ba \\((i+1) \\times (j+1)\\) \u306e\u4e8c\u6b21\u5143 \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u3092\u5f97\u307e\u3059\u3002

        \u30b9\u30c6\u30c3\u30d72\uff1a\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u7279\u5b9a\u3057\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3092\u5c0e\u51fa\u3059\u308b

        \u90e8\u5206\u554f\u984c \\(dp[i, j]\\) \u3092\u8003\u616e\u3059\u308b\u3068\u3001\u3053\u308c\u306b\u5bfe\u5fdc\u3059\u308b2\u3064\u306e\u6587\u5b57\u5217\u306e\u672b\u5c3e\u6587\u5b57\u306f \\(s[i-1]\\) \u3068 \\(t[j-1]\\) \u3067\u3042\u308a\u3001\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b3\u3064\u306e\u30b7\u30ca\u30ea\u30aa\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        1. \\(s[i-1]\\) \u306e\u5f8c\u306b \\(t[j-1]\\) \u3092\u8ffd\u52a0\u3059\u308b\u3068\u3001\u6b8b\u308a\u306e\u90e8\u5206\u554f\u984c\u306f \\(dp[i, j-1]\\) \u3067\u3059\u3002
        2. \\(s[i-1]\\) \u3092\u524a\u9664\u3059\u308b\u3068\u3001\u6b8b\u308a\u306e\u90e8\u5206\u554f\u984c\u306f \\(dp[i-1, j]\\) \u3067\u3059\u3002
        3. \\(s[i-1]\\) \u3092 \\(t[j-1]\\) \u306b\u7f6e\u63db\u3059\u308b\u3068\u3001\u6b8b\u308a\u306e\u90e8\u5206\u554f\u984c\u306f \\(dp[i-1, j-1]\\) \u3067\u3059\u3002

        \u56f3 14-29 \u00a0 \u7de8\u96c6\u8ddd\u96e2\u306e\u72b6\u614b\u9077\u79fb

        \u4e0a\u8a18\u306e\u5206\u6790\u306b\u57fa\u3065\u3044\u3066\u3001\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u6c7a\u5b9a\u3067\u304d\u307e\u3059\uff1a\\(dp[i, j]\\) \u306e\u6700\u5c0f\u7de8\u96c6\u56de\u6570\u306f\u3001\\(dp[i, j-1]\\)\u3001\\(dp[i-1, j]\\)\u3001\\(dp[i-1, j-1]\\) \u306e\u4e2d\u306e\u6700\u5c0f\u5024\u306b\u7de8\u96c6\u30b9\u30c6\u30c3\u30d7 \\(1\\) \u3092\u52a0\u3048\u305f\u3082\u306e\u3067\u3059\u3002\u5bfe\u5fdc\u3059\u308b\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u306f\uff1a

        \\[ dp[i, j] = \\min(dp[i, j-1], dp[i-1, j], dp[i-1, j-1]) + 1 \\]

        \u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\\(s[i-1]\\) \u3068 \\(t[j-1]\\) \u304c\u540c\u3058\u5834\u5408\u3001\u73fe\u5728\u306e\u6587\u5b57\u306b\u5bfe\u3057\u3066\u7de8\u96c6\u306f\u5fc5\u8981\u3042\u308a\u307e\u305b\u3093\u3002\u3053\u306e\u5834\u5408\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u306f\uff1a

        \\[ dp[i, j] = dp[i-1, j-1] \\]

        \u30b9\u30c6\u30c3\u30d73\uff1a\u5883\u754c\u6761\u4ef6\u3068\u72b6\u614b\u9077\u79fb\u306e\u9806\u5e8f\u3092\u6c7a\u5b9a\u3059\u308b

        \u4e21\u65b9\u306e\u6587\u5b57\u5217\u304c\u7a7a\u306e\u5834\u5408\u3001\u7de8\u96c6\u56de\u6570\u306f \\(0\\) \u3067\u3059\u3002\u3064\u307e\u308a\u3001\\(dp[0, 0] = 0\\) \u3067\u3059\u3002\\(s\\) \u304c\u7a7a\u3067 \\(t\\) \u304c\u7a7a\u3067\u306a\u3044\u5834\u5408\u3001\u6700\u5c0f\u7de8\u96c6\u56de\u6570\u306f \\(t\\) \u306e\u9577\u3055\u306b\u7b49\u3057\u304f\u3001\u3064\u307e\u308a\u6700\u521d\u306e\u884c \\(dp[0, j] = j\\) \u3067\u3059\u3002\\(s\\) \u304c\u7a7a\u3067\u306a\u304f \\(t\\) \u304c\u7a7a\u306e\u5834\u5408\u3001\u6700\u5c0f\u7de8\u96c6\u56de\u6570\u306f \\(s\\) \u306e\u9577\u3055\u306b\u7b49\u3057\u304f\u3001\u3064\u307e\u308a\u6700\u521d\u306e\u5217 \\(dp[i, 0] = i\\) \u3067\u3059\u3002

        \u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\\(dp[i, j]\\) \u306e\u89e3\u6c7a\u306f\u5de6\u3001\u4e0a\u3001\u5de6\u4e0a\u306e\u89e3\u306b\u4f9d\u5b58\u3059\u308b\u305f\u3081\u3001\u4e8c\u91cd\u30eb\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u3066\u6b63\u3057\u3044\u9806\u5e8f\u3067 \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u5168\u4f53\u3092\u8d70\u67fb\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_dynamic_programming/edit_distance_problem/#2","title":"2. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby edit_distance.py
        def edit_distance_dp(s: str, t: str) -> int:\n    \"\"\"\u7de8\u96c6\u8ddd\u96e2\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n, m = len(s), len(t)\n    dp = [[0] * (m + 1) for _ in range(n + 1)]\n    # \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\u3068\u6700\u521d\u306e\u5217\n    for i in range(1, n + 1):\n        dp[i][0] = i\n    for j in range(1, m + 1):\n        dp[0][j] = j\n    # \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\u3068\u5217\n    for i in range(1, n + 1):\n        for j in range(1, m + 1):\n            if s[i - 1] == t[j - 1]:\n                # 2 \u3064\u306e\u6587\u5b57\u304c\u7b49\u3057\u3044\u5834\u5408\u3001\u3053\u308c\u3089 2 \u3064\u306e\u6587\u5b57\u3092\u30b9\u30ad\u30c3\u30d7\n                dp[i][j] = dp[i - 1][j - 1]\n            else:\n                # \u6700\u5c0f\u7de8\u96c6\u6570 = 3 \u3064\u306e\u64cd\u4f5c\uff08\u633f\u5165\u3001\u524a\u9664\u3001\u7f6e\u63db\uff09\u304b\u3089\u306e\u6700\u5c0f\u7de8\u96c6\u6570 + 1\n                dp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1\n    return dp[n][m]\n
        edit_distance.cpp
        /* \u7de8\u96c6\u8ddd\u96e2\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint editDistanceDP(string s, string t) {\n    int n = s.length(), m = t.length();\n    vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\u3068\u6700\u521d\u306e\u5217\n    for (int i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\u3068\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s[i - 1] == t[j - 1]) {\n                // 2\u3064\u306e\u6587\u5b57\u304c\u7b49\u3057\u3044\u5834\u5408\u3001\u3053\u308c\u30892\u3064\u306e\u6587\u5b57\u3092\u30b9\u30ad\u30c3\u30d7\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // \u6700\u5c0f\u7de8\u96c6\u6570 = 3\u3064\u306e\u64cd\u4f5c\uff08\u633f\u5165\u3001\u524a\u9664\u3001\u7f6e\u63db\uff09\u304b\u3089\u306e\u6700\u5c0f\u7de8\u96c6\u6570 + 1\n                dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
        edit_distance.java
        /* \u7de8\u96c6\u8ddd\u96e2\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint editDistanceDP(String s, String t) {\n    int n = s.length(), m = t.length();\n    int[][] dp = new int[n + 1][m + 1];\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\u3068\u6700\u521d\u306e\u5217\n    for (int i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\u3068\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s.charAt(i - 1) == t.charAt(j - 1)) {\n                // 2\u3064\u306e\u6587\u5b57\u304c\u7b49\u3057\u3044\u5834\u5408\u3001\u3053\u308c\u30892\u3064\u306e\u6587\u5b57\u3092\u30b9\u30ad\u30c3\u30d7\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // \u6700\u5c0f\u7de8\u96c6\u6570 = 3\u3064\u306e\u64cd\u4f5c\uff08\u633f\u5165\u3001\u524a\u9664\u3001\u7f6e\u63db\uff09\u304b\u3089\u306e\u6700\u5c0f\u7de8\u96c6\u6570 + 1\n                dp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
        edit_distance.cs
        [class]{edit_distance}-[func]{EditDistanceDP}\n
        edit_distance.go
        [class]{}-[func]{editDistanceDP}\n
        edit_distance.swift
        [class]{}-[func]{editDistanceDP}\n
        edit_distance.js
        [class]{}-[func]{editDistanceDP}\n
        edit_distance.ts
        [class]{}-[func]{editDistanceDP}\n
        edit_distance.dart
        [class]{}-[func]{editDistanceDP}\n
        edit_distance.rs
        [class]{}-[func]{edit_distance_dp}\n
        edit_distance.c
        [class]{}-[func]{editDistanceDP}\n
        edit_distance.kt
        [class]{}-[func]{editDistanceDP}\n
        edit_distance.rb
        [class]{}-[func]{edit_distance_dp}\n

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u7de8\u96c6\u8ddd\u96e2\u554f\u984c\u306e\u72b6\u614b\u9077\u79fb\u30d7\u30ed\u30bb\u30b9\u306f\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3068\u975e\u5e38\u306b\u4f3c\u3066\u304a\u308a\u3001\u4e8c\u6b21\u5143\u30b0\u30ea\u30c3\u30c9\u3092\u57cb\u3081\u308b\u3053\u3068\u3068\u898b\u306a\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        <1><2><3><4><5><6><7><8><9><10><11><12><13><14><15>

        \u56f3 14-30 \u00a0 \u7de8\u96c6\u8ddd\u96e2\u306e\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9

        "},{"location":"chapter_dynamic_programming/edit_distance_problem/#3","title":"3. \u00a0 \u7a7a\u9593\u6700\u9069\u5316","text":"

        \\(dp[i, j]\\) \u306f\u4e0a\u306e \\(dp[i-1, j]\\)\u3001\u5de6\u306e \\(dp[i, j-1]\\)\u3001\u5de6\u4e0a\u306e \\(dp[i-1, j-1]\\) \u306e\u89e3\u304b\u3089\u5c0e\u51fa\u3055\u308c\u3001\u76f4\u63a5\u8d70\u67fb\u3067\u306f\u5de6\u4e0a\u306e\u89e3 \\(dp[i-1, j-1]\\) \u304c\u5931\u308f\u308c\u3001\u9006\u8d70\u67fb\u3067\u306f\u4e8b\u524d\u306b \\(dp[i, j-1]\\) \u3092\u69cb\u7bc9\u3067\u304d\u306a\u3044\u305f\u3081\u3001\u3069\u3061\u3089\u306e\u8d70\u67fb\u9806\u5e8f\u3082\u5b9f\u884c\u53ef\u80fd\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002

        \u3053\u306e\u7406\u7531\u3067\u3001\u5909\u6570 leftup \u3092\u4f7f\u7528\u3057\u3066\u5de6\u4e0a\u306e \\(dp[i-1, j-1]\\) \u304b\u3089\u306e\u89e3\u3092\u4e00\u6642\u7684\u306b\u4fdd\u5b58\u3057\u3001\u5de6\u3068\u4e0a\u306e\u89e3\u306e\u307f\u3092\u8003\u616e\u3059\u308c\u3070\u3088\u304f\u306a\u308a\u307e\u3059\u3002\u3053\u306e\u72b6\u6cc1\u306f\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3068\u4f3c\u3066\u304a\u308a\u3001\u76f4\u63a5\u8d70\u67fb\u304c\u53ef\u80fd\u3067\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby edit_distance.py
        def edit_distance_dp_comp(s: str, t: str) -> int:\n    \"\"\"\u7de8\u96c6\u8ddd\u96e2\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n, m = len(s), len(t)\n    dp = [0] * (m + 1)\n    # \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\n    for j in range(1, m + 1):\n        dp[j] = j\n    # \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\n    for i in range(1, n + 1):\n        # \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u5217\n        leftup = dp[0]  # dp[i-1, j-1] \u3092\u4e00\u6642\u7684\u306b\u4fdd\u5b58\n        dp[0] += 1\n        # \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u5217\n        for j in range(1, m + 1):\n            temp = dp[j]\n            if s[i - 1] == t[j - 1]:\n                # 2 \u3064\u306e\u6587\u5b57\u304c\u7b49\u3057\u3044\u5834\u5408\u3001\u3053\u308c\u3089 2 \u3064\u306e\u6587\u5b57\u3092\u30b9\u30ad\u30c3\u30d7\n                dp[j] = leftup\n            else:\n                # \u6700\u5c0f\u7de8\u96c6\u6570 = 3 \u3064\u306e\u64cd\u4f5c\uff08\u633f\u5165\u3001\u524a\u9664\u3001\u7f6e\u63db\uff09\u304b\u3089\u306e\u6700\u5c0f\u7de8\u96c6\u6570 + 1\n                dp[j] = min(dp[j - 1], dp[j], leftup) + 1\n            leftup = temp  # \u6b21\u306e dp[i-1, j-1] \u306e\u305f\u3081\u306b\u66f4\u65b0\n    return dp[m]\n
        edit_distance.cpp
        [class]{}-[func]{editDistanceDPComp}\n
        edit_distance.java
        /* \u7de8\u96c6\u8ddd\u96e2\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint editDistanceDPComp(String s, String t) {\n    int n = s.length(), m = t.length();\n    int[] dp = new int[m + 1];\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\n    for (int j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\n    for (int i = 1; i <= n; i++) {\n        // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u5217\n        int leftup = dp[0]; // dp[i-1, j-1] \u3092\u4e00\u6642\u7684\u306b\u683c\u7d0d\n        dp[0] = i;\n        // \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u5217\n        for (int j = 1; j <= m; j++) {\n            int temp = dp[j];\n            if (s.charAt(i - 1) == t.charAt(j - 1)) {\n                // 2\u3064\u306e\u6587\u5b57\u304c\u7b49\u3057\u3044\u5834\u5408\u3001\u3053\u308c\u30892\u3064\u306e\u6587\u5b57\u3092\u30b9\u30ad\u30c3\u30d7\n                dp[j] = leftup;\n            } else {\n                // \u6700\u5c0f\u7de8\u96c6\u6570 = 3\u3064\u306e\u64cd\u4f5c\uff08\u633f\u5165\u3001\u524a\u9664\u3001\u7f6e\u63db\uff09\u304b\u3089\u306e\u6700\u5c0f\u7de8\u96c6\u6570 + 1\n                dp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // \u6b21\u306e\u30e9\u30a6\u30f3\u30c9\u306e dp[i-1, j-1] \u306e\u305f\u3081\u306b\u66f4\u65b0\n        }\n    }\n    return dp[m];\n}\n
        edit_distance.cs
        [class]{edit_distance}-[func]{EditDistanceDPComp}\n
        edit_distance.go
        [class]{}-[func]{editDistanceDPComp}\n
        edit_distance.swift
        [class]{}-[func]{editDistanceDPComp}\n
        edit_distance.js
        [class]{}-[func]{editDistanceDPComp}\n
        edit_distance.ts
        [class]{}-[func]{editDistanceDPComp}\n
        edit_distance.dart
        [class]{}-[func]{editDistanceDPComp}\n
        edit_distance.rs
        [class]{}-[func]{edit_distance_dp_comp}\n
        edit_distance.c
        [class]{}-[func]{editDistanceDPComp}\n
        edit_distance.kt
        [class]{}-[func]{editDistanceDPComp}\n
        edit_distance.rb
        [class]{}-[func]{edit_distance_dp_comp}\n
        "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/","title":"14.1 \u00a0 \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306e\u7d39\u4ecb","text":"

        \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u91cd\u8981\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d1\u30e9\u30c0\u30a4\u30e0\u3067\u3042\u308a\u3001\u554f\u984c\u3092\u4e00\u9023\u306e\u5c0f\u3055\u306a\u90e8\u5206\u554f\u984c\u306b\u5206\u89e3\u3057\u3001\u3053\u308c\u3089\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4fdd\u5b58\u3059\u308b\u3053\u3068\u3067\u5197\u9577\u306a\u8a08\u7b97\u3092\u907f\u3051\u3001\u6642\u9593\u52b9\u7387\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002

        \u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u53e4\u5178\u7684\u306a\u554f\u984c\u304b\u3089\u59cb\u3081\u3066\u3001\u307e\u305a\u529b\u4efb\u305b\u306e\u63a2\u7d22\u6cd5\u306b\u3088\u308b\u89e3\u6cd5\u3092\u63d0\u793a\u3057\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u3092\u7279\u5b9a\u3057\u3066\u304b\u3089\u3001\u3088\u308a\u52b9\u7387\u7684\u306a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u89e3\u6cd5\u3092\u6bb5\u968e\u7684\u306b\u5c0e\u51fa\u3057\u307e\u3059\u3002

        \u968e\u6bb5\u767b\u308a

        \\(n\\) \u6bb5\u306e\u968e\u6bb5\u304c\u3042\u308a\u3001\u4e00\u5ea6\u306b \\(1\\) \u6bb5\u307e\u305f\u306f \\(2\\) \u6bb5\u4e0a\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u9802\u4e0a\u306b\u5230\u9054\u3059\u308b\u65b9\u6cd5\u306f\u4f55\u901a\u308a\u3042\u308a\u307e\u3059\u304b\uff1f

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\\(3\\) \u6bb5\u306e\u968e\u6bb5\u306e\u9802\u4e0a\u306b\u5230\u9054\u3059\u308b\u65b9\u6cd5\u306f \\(3\\) \u901a\u308a\u3042\u308a\u307e\u3059\u3002

        \u56f3 14-1 \u00a0 3\u6bb5\u76ee\u306b\u5230\u9054\u3059\u308b\u65b9\u6cd5\u306e\u6570

        \u3053\u306e\u554f\u984c\u306f**\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3092\u7528\u3044\u3066\u3059\u3079\u3066\u306e\u53ef\u80fd\u6027\u3092\u7db2\u7f85**\u3059\u308b\u3053\u3068\u3067\u65b9\u6cd5\u306e\u6570\u3092\u8a08\u7b97\u3059\u308b\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u3066\u3044\u307e\u3059\u3002\u5177\u4f53\u7684\u306b\u306f\u3001\u968e\u6bb5\u767b\u308a\u306e\u554f\u984c\u3092\u591a\u6bb5\u968e\u9078\u629e\u30d7\u30ed\u30bb\u30b9\u3068\u3057\u3066\u8003\u3048\u307e\u3059\uff1a\u5730\u9762\u304b\u3089\u59cb\u3081\u3066\u3001\u6bce\u56de \\(1\\) \u6bb5\u307e\u305f\u306f \\(2\\) \u6bb5\u4e0a\u308b\u304b\u3092\u9078\u629e\u3057\u3001\u968e\u6bb5\u306e\u9802\u4e0a\u306b\u5230\u9054\u3057\u305f\u3089\u65b9\u6cd5\u306e\u6570\u3092\u30ab\u30a6\u30f3\u30c8\u3057\u3001\u9802\u4e0a\u3092\u8d85\u3048\u305f\u5834\u5408\u306f\u30d7\u30eb\u30fc\u30cb\u30f3\u30b0\uff08\u679d\u5208\u308a\uff09\u3092\u884c\u3044\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_backtrack.py
        def backtrack(choices: list[int], state: int, n: int, res: list[int]) -> int:\n    \"\"\"\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\"\"\"\n    # n \u6bb5\u76ee\u306b\u767b\u3063\u305f\u3068\u304d\u3001\u89e3\u306e\u6570\u306b 1 \u3092\u52a0\u3048\u308b\n    if state == n:\n        res[0] += 1\n    # \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for choice in choices:\n        # \u679d\u5208\u308a\uff1an \u6bb5\u3092\u8d85\u3048\u3066\u767b\u308b\u3053\u3068\u3092\u8a31\u53ef\u3057\u306a\u3044\n        if state + choice > n:\n            continue\n        # \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n        backtrack(choices, state + choice, n, res)\n        # \u64a4\u56de\n\ndef climbing_stairs_backtrack(n: int) -> int:\n    \"\"\"\u968e\u6bb5\u767b\u308a\uff1a\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\"\"\"\n    choices = [1, 2]  # 1 \u6bb5\u307e\u305f\u306f 2 \u6bb5\u767b\u308b\u3053\u3068\u3092\u9078\u629e\u53ef\u80fd\n    state = 0  # 0 \u6bb5\u76ee\u304b\u3089\u767b\u308a\u59cb\u3081\u308b\n    res = [0]  # res[0] \u3092\u4f7f\u7528\u3057\u3066\u89e3\u306e\u6570\u3092\u8a18\u9332\n    backtrack(choices, state, n, res)\n    return res[0]\n
        climbing_stairs_backtrack.cpp
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0 */\nvoid backtrack(vector<int> &choices, int state, int n, vector<int> &res) {\n    // n\u6bb5\u76ee\u306b\u5230\u9054\u3057\u305f\u3068\u304d\u3001\u89e3\u306e\u6570\u306b1\u3092\u52a0\u3048\u308b\n    if (state == n)\n        res[0]++;\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for (auto &choice : choices) {\n        // \u526a\u5b9a\uff1an\u6bb5\u3092\u8d85\u3048\u3066\u767b\u308b\u3053\u3068\u3092\u8a31\u53ef\u3057\u306a\u3044\n        if (state + choice > n)\n            continue;\n        // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n        backtrack(choices, state + choice, n, res);\n        // \u64a4\u56de\n    }\n}\n\n/* \u968e\u6bb5\u767b\u308a\uff1a\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0 */\nint climbingStairsBacktrack(int n) {\n    vector<int> choices = {1, 2}; // 1\u6bb5\u307e\u305f\u306f2\u6bb5\u767b\u308b\u3053\u3068\u3092\u9078\u629e\u53ef\u80fd\n    int state = 0;                // 0\u6bb5\u76ee\u304b\u3089\u767b\u308a\u59cb\u3081\u308b\n    vector<int> res = {0};        // res[0] \u3092\u4f7f\u7528\u3057\u3066\u89e3\u306e\u6570\u3092\u8a18\u9332\n    backtrack(choices, state, n, res);\n    return res[0];\n}\n
        climbing_stairs_backtrack.java
        /* \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0 */\nvoid backtrack(List<Integer> choices, int state, int n, List<Integer> res) {\n    // n\u6bb5\u76ee\u306b\u5230\u9054\u3057\u305f\u3068\u304d\u3001\u89e3\u306e\u6570\u306b1\u3092\u52a0\u3048\u308b\n    if (state == n)\n        res.set(0, res.get(0) + 1);\n    // \u3059\u3079\u3066\u306e\u9078\u629e\u80a2\u3092\u8d70\u67fb\n    for (Integer choice : choices) {\n        // \u526a\u5b9a\uff1an\u6bb5\u3092\u8d85\u3048\u3066\u767b\u308b\u3053\u3068\u3092\u8a31\u53ef\u3057\u306a\u3044\n        if (state + choice > n)\n            continue;\n        // \u8a66\u884c\uff1a\u9078\u629e\u3092\u884c\u3044\u3001\u72b6\u614b\u3092\u66f4\u65b0\n        backtrack(choices, state + choice, n, res);\n        // \u64a4\u56de\n    }\n}\n\n/* \u968e\u6bb5\u767b\u308a\uff1a\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0 */\nint climbingStairsBacktrack(int n) {\n    List<Integer> choices = Arrays.asList(1, 2); // 1\u6bb5\u307e\u305f\u306f2\u6bb5\u767b\u308b\u3053\u3068\u3092\u9078\u629e\u53ef\u80fd\n    int state = 0; // 0\u6bb5\u76ee\u304b\u3089\u767b\u308a\u59cb\u3081\u308b\n    List<Integer> res = new ArrayList<>();\n    res.add(0); // res[0] \u3092\u4f7f\u7528\u3057\u3066\u89e3\u306e\u6570\u3092\u8a18\u9332\n    backtrack(choices, state, n, res);\n    return res.get(0);\n}\n
        climbing_stairs_backtrack.cs
        [class]{climbing_stairs_backtrack}-[func]{Backtrack}\n\n[class]{climbing_stairs_backtrack}-[func]{ClimbingStairsBacktrack}\n
        climbing_stairs_backtrack.go
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{climbingStairsBacktrack}\n
        climbing_stairs_backtrack.swift
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{climbingStairsBacktrack}\n
        climbing_stairs_backtrack.js
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{climbingStairsBacktrack}\n
        climbing_stairs_backtrack.ts
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{climbingStairsBacktrack}\n
        climbing_stairs_backtrack.dart
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{climbingStairsBacktrack}\n
        climbing_stairs_backtrack.rs
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{climbing_stairs_backtrack}\n
        climbing_stairs_backtrack.c
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{climbingStairsBacktrack}\n
        climbing_stairs_backtrack.kt
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{climbingStairsBacktrack}\n
        climbing_stairs_backtrack.rb
        [class]{}-[func]{backtrack}\n\n[class]{}-[func]{climbing_stairs_backtrack}\n
        "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1411-1","title":"14.1.1 \u00a0 \u65b9\u6cd51\uff1a\u529b\u4efb\u305b\u63a2\u7d22","text":"

        \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u554f\u984c\u3092\u660e\u793a\u7684\u306b\u90e8\u5206\u554f\u984c\u306b\u5206\u89e3\u3057\u307e\u305b\u3093\u3002\u4ee3\u308f\u308a\u306b\u3001\u554f\u984c\u3092\u4e00\u9023\u306e\u6c7a\u5b9a\u30b9\u30c6\u30c3\u30d7\u3068\u3057\u3066\u6271\u3044\u3001\u8a66\u884c\u3068\u679d\u5208\u308a\u3092\u901a\u3058\u3066\u3059\u3079\u3066\u306e\u53ef\u80fd\u6027\u3092\u63a2\u7d22\u3057\u307e\u3059\u3002

        \u3053\u306e\u554f\u984c\u3092\u5206\u89e3\u30a2\u30d7\u30ed\u30fc\u30c1\u3092\u4f7f\u3063\u3066\u5206\u6790\u3067\u304d\u307e\u3059\u3002\\(dp[i]\\) \u3092 \\(i\\) \u6bb5\u76ee\u306b\u5230\u9054\u3059\u308b\u65b9\u6cd5\u306e\u6570\u3068\u3057\u307e\u3059\u3002\u3053\u306e\u5834\u5408\u3001\\(dp[i]\\) \u304c\u5143\u306e\u554f\u984c\u3067\u3042\u308a\u3001\u305d\u306e\u90e8\u5206\u554f\u984c\u306f\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\uff1a

        \\[ dp[i-1], dp[i-2], \\dots, dp[2], dp[1] \\]

        \u5404\u79fb\u52d5\u306f \\(1\\) \u6bb5\u307e\u305f\u306f \\(2\\) \u6bb5\u3057\u304b\u9032\u3081\u306a\u3044\u305f\u3081\u3001\\(i\\) \u6bb5\u76ee\u306b\u7acb\u3063\u3066\u3044\u308b\u3068\u304d\u3001\u524d\u306e\u30b9\u30c6\u30c3\u30d7\u306f \\(i-1\\) \u6bb5\u76ee\u307e\u305f\u306f \\(i-2\\) \u6bb5\u76ee\u306e\u3044\u305a\u308c\u304b\u306b\u3044\u305f\u306f\u305a\u3067\u3059\u3002\u3064\u307e\u308a\u3001\\(i\\) \u6bb5\u76ee\u306b\u306f \\(i-1\\) \u6bb5\u76ee\u307e\u305f\u306f \\(i-2\\) \u6bb5\u76ee\u304b\u3089\u3057\u304b\u5230\u9054\u3067\u304d\u307e\u305b\u3093\u3002

        \u3053\u308c\u306b\u3088\u308a\u91cd\u8981\u306a\u7d50\u8ad6\u304c\u5f97\u3089\u308c\u307e\u3059\uff1a\\(i-1\\) \u6bb5\u76ee\u306b\u5230\u9054\u3059\u308b\u65b9\u6cd5\u306e\u6570\u306b \\(i-2\\) \u6bb5\u76ee\u306b\u5230\u9054\u3059\u308b\u65b9\u6cd5\u306e\u6570\u3092\u52a0\u3048\u305f\u3082\u306e\u304c\u3001\\(i\\) \u6bb5\u76ee\u306b\u5230\u9054\u3059\u308b\u65b9\u6cd5\u306e\u6570\u306b\u7b49\u3057\u3044\u3002\u5f0f\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        \\[ dp[i] = dp[i-1] + dp[i-2] \\]

        \u3053\u308c\u306f\u3001\u968e\u6bb5\u767b\u308a\u554f\u984c\u306b\u304a\u3044\u3066\u90e8\u5206\u554f\u984c\u9593\u306b\u518d\u5e30\u95a2\u4fc2\u304c\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u3001\u5143\u306e\u554f\u984c\u306e\u89e3\u306f\u90e8\u5206\u554f\u984c\u306e\u89e3\u304b\u3089\u69cb\u7bc9\u3067\u304d\u307e\u3059\u3002\u4e0b\u306e\u56f3\u306f\u3053\u306e\u518d\u5e30\u95a2\u4fc2\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 14-2 \u00a0 \u89e3\u306e\u6570\u306e\u518d\u5e30\u95a2\u4fc2

        \u518d\u5e30\u5f0f\u306b\u5f93\u3063\u3066\u529b\u4efb\u305b\u63a2\u7d22\u89e3\u6cd5\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\\(dp[n]\\) \u304b\u3089\u59cb\u3081\u3066\u3001**\u3088\u308a\u5927\u304d\u306a\u554f\u984c\u3092\u518d\u5e30\u7684\u306b2\u3064\u306e\u5c0f\u3055\u306a\u90e8\u5206\u554f\u984c\u306e\u548c\u306b\u5206\u89e3**\u3057\u3001\u89e3\u304c\u65e2\u77e5\u306e\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c \\(dp[1]\\) \u3068 \\(dp[2]\\) \u306b\u5230\u9054\u3059\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002\\(dp[1] = 1\\) \u3068 \\(dp[2] = 2\\) \u3067\u3001\u305d\u308c\u305e\u308c1\u6bb5\u76ee\u30682\u6bb5\u76ee\u306b\u767b\u308b\u65b9\u6cd5\u304c \\(1\\) \u901a\u308a\u3068 \\(2\\) \u901a\u308a\u3042\u308b\u3053\u3068\u3092\u8868\u3057\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\u6a19\u6e96\u7684\u306a\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30b3\u30fc\u30c9\u3068\u540c\u69d8\u306b\u6df1\u3055\u512a\u5148\u63a2\u7d22\u306b\u5c5e\u3057\u307e\u3059\u304c\u3001\u3088\u308a\u7c21\u6f54\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dfs.py
        def dfs(i: int) -> int:\n    \"\"\"\u63a2\u7d22\"\"\"\n    # \u65e2\u77e5\u306e dp[1] \u3068 dp[2] \u306f\u3001\u305d\u308c\u3089\u3092\u8fd4\u3059\n    if i == 1 or i == 2:\n        return i\n    # dp[i] = dp[i-1] + dp[i-2]\n    count = dfs(i - 1) + dfs(i - 2)\n    return count\n\ndef climbing_stairs_dfs(n: int) -> int:\n    \"\"\"\u968e\u6bb5\u767b\u308a\uff1a\u63a2\u7d22\"\"\"\n    return dfs(n)\n
        climbing_stairs_dfs.cpp
        /* \u63a2\u7d22 */\nint dfs(int i) {\n    // \u65e2\u77e5\u306e dp[1] \u3068 dp[2] \u3092\u8fd4\u3059\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* \u968e\u6bb5\u767b\u308a\uff1a\u63a2\u7d22 */\nint climbingStairsDFS(int n) {\n    return dfs(n);\n}\n
        climbing_stairs_dfs.java
        /* \u63a2\u7d22 */\nint dfs(int i) {\n    // \u65e2\u77e5\u306e dp[1] \u3068 dp[2] \u3092\u8fd4\u3059\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* \u968e\u6bb5\u767b\u308a\uff1a\u63a2\u7d22 */\nint climbingStairsDFS(int n) {\n    return dfs(n);\n}\n
        climbing_stairs_dfs.cs
        [class]{climbing_stairs_dfs}-[func]{DFS}\n\n[class]{climbing_stairs_dfs}-[func]{ClimbingStairsDFS}\n
        climbing_stairs_dfs.go
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFS}\n
        climbing_stairs_dfs.swift
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFS}\n
        climbing_stairs_dfs.js
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFS}\n
        climbing_stairs_dfs.ts
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFS}\n
        climbing_stairs_dfs.dart
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFS}\n
        climbing_stairs_dfs.rs
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbing_stairs_dfs}\n
        climbing_stairs_dfs.c
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFS}\n
        climbing_stairs_dfs.kt
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFS}\n
        climbing_stairs_dfs.rb
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbing_stairs_dfs}\n

        \u4e0b\u306e\u56f3\u306f\u529b\u4efb\u305b\u63a2\u7d22\u306b\u3088\u3063\u3066\u5f62\u6210\u3055\u308c\u308b\u518d\u5e30\u6728\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u554f\u984c \\(dp[n]\\) \u306b\u3064\u3044\u3066\u3001\u305d\u306e\u518d\u5e30\u6728\u306e\u6df1\u3055\u306f \\(n\\) \u3067\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(2^n)\\) \u3067\u3059\u3002\u3053\u306e\u6307\u6570\u7684\u5897\u52a0\u306b\u3088\u308a\u3001\\(n\\) \u304c\u5927\u304d\u3044\u3068\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u5b9f\u884c\u304c\u306f\u308b\u304b\u306b\u9045\u304f\u306a\u308a\u3001\u9577\u3044\u5f85\u6a5f\u6642\u9593\u304c\u751f\u3058\u307e\u3059\u3002

        \u56f3 14-3 \u00a0 \u968e\u6bb5\u767b\u308a\u306e\u518d\u5e30\u6728

        \u4e0a\u306e\u56f3\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001**\u6307\u6570\u6642\u9593\u8a08\u7b97\u91cf\u306f\u300c\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u300d\u306b\u3088\u3063\u3066\u5f15\u304d\u8d77\u3053\u3055\u308c\u308b**\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\\(dp[9]\\) \u306f \\(dp[8]\\) \u3068 \\(dp[7]\\) \u306b\u5206\u89e3\u3055\u308c\u3001\\(dp[8]\\) \u306f\u3055\u3089\u306b \\(dp[7]\\) \u3068 \\(dp[6]\\) \u306b\u5206\u89e3\u3055\u308c\u3001\u4e21\u65b9\u3068\u3082\u90e8\u5206\u554f\u984c \\(dp[7]\\) \u3092\u542b\u3093\u3067\u3044\u307e\u3059\u3002

        \u3053\u306e\u3088\u3046\u306b\u3001\u90e8\u5206\u554f\u984c\u306b\u306f\u3055\u3089\u306b\u5c0f\u3055\u306a\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u304c\u542b\u307e\u308c\u3001\u3053\u308c\u306f\u7121\u9650\u306b\u7d9a\u304d\u307e\u3059\u3002\u8a08\u7b97\u30ea\u30bd\u30fc\u30b9\u306e\u5927\u90e8\u5206\u304c\u3053\u308c\u3089\u306e\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u306b\u6d6a\u8cbb\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1412-2","title":"14.1.2 \u00a0 \u65b9\u6cd52\uff1a\u30e1\u30e2\u5316\u63a2\u7d22","text":"

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u308b\u305f\u3081\u3001**\u3059\u3079\u3066\u306e\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u3092\u4e00\u5ea6\u3060\u3051\u8a08\u7b97\u3057\u305f\u3044**\u3068\u8003\u3048\u307e\u3059\u3002\u3053\u306e\u76ee\u7684\u306e\u305f\u3081\u3001\u5404\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u8a18\u9332\u3059\u308b\u914d\u5217 mem \u3092\u5ba3\u8a00\u3057\u3001\u63a2\u7d22\u30d7\u30ed\u30bb\u30b9\u4e2d\u306b\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u3092\u679d\u5208\u308a\u3057\u307e\u3059\u3002

        1. \\(dp[i]\\) \u304c\u521d\u3081\u3066\u8a08\u7b97\u3055\u308c\u308b\u3068\u304d\u3001\u5f8c\u3067\u4f7f\u7528\u3059\u308b\u305f\u3081\u306b mem[i] \u306b\u8a18\u9332\u3057\u307e\u3059\u3002
        2. \\(dp[i]\\) \u3092\u518d\u5ea6\u8a08\u7b97\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3068\u304d\u3001mem[i] \u304b\u3089\u76f4\u63a5\u7d50\u679c\u3092\u53d6\u5f97\u3067\u304d\u3001\u305d\u306e\u90e8\u5206\u554f\u984c\u306e\u5197\u9577\u306a\u8a08\u7b97\u3092\u907f\u3051\u3089\u308c\u307e\u3059\u3002

        \u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dfs_mem.py
        def dfs(i: int, mem: list[int]) -> int:\n    \"\"\"\u8a18\u61b6\u5316\u63a2\u7d22\"\"\"\n    # \u65e2\u77e5\u306e dp[1] \u3068 dp[2] \u306f\u3001\u305d\u308c\u3089\u3092\u8fd4\u3059\n    if i == 1 or i == 2:\n        return i\n    # dp[i] \u306e\u8a18\u9332\u304c\u3042\u308b\u5834\u5408\u3001\u305d\u308c\u3092\u8fd4\u3059\n    if mem[i] != -1:\n        return mem[i]\n    # dp[i] = dp[i-1] + dp[i-2]\n    count = dfs(i - 1, mem) + dfs(i - 2, mem)\n    # dp[i] \u3092\u8a18\u9332\n    mem[i] = count\n    return count\n\ndef climbing_stairs_dfs_mem(n: int) -> int:\n    \"\"\"\u968e\u6bb5\u767b\u308a\uff1a\u8a18\u61b6\u5316\u63a2\u7d22\"\"\"\n    # mem[i] \u306f i \u6bb5\u76ee\u306b\u767b\u308b\u89e3\u306e\u7dcf\u6570\u3092\u8a18\u9332\u3001-1 \u306f\u8a18\u9332\u306a\u3057\u3092\u610f\u5473\u3059\u308b\n    mem = [-1] * (n + 1)\n    return dfs(n, mem)\n
        climbing_stairs_dfs_mem.cpp
        /* \u30e1\u30e2\u5316\u63a2\u7d22 */\nint dfs(int i, vector<int> &mem) {\n    // \u65e2\u77e5\u306e dp[1] \u3068 dp[2] \u3092\u8fd4\u3059\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] \u306e\u8a18\u9332\u304c\u3042\u308b\u5834\u5408\u3001\u305d\u308c\u3092\u8fd4\u3059\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // dp[i] \u3092\u8a18\u9332\n    mem[i] = count;\n    return count;\n}\n\n/* \u968e\u6bb5\u767b\u308a\uff1a\u30e1\u30e2\u5316\u63a2\u7d22 */\nint climbingStairsDFSMem(int n) {\n    // mem[i] \u306f i \u6bb5\u76ee\u306b\u767b\u308b\u7dcf\u89e3\u6570\u3092\u8a18\u9332\u3001-1 \u306f\u8a18\u9332\u306a\u3057\u3092\u610f\u5473\u3059\u308b\n    vector<int> mem(n + 1, -1);\n    return dfs(n, mem);\n}\n
        climbing_stairs_dfs_mem.java
        /* \u30e1\u30e2\u5316\u63a2\u7d22 */\nint dfs(int i, int[] mem) {\n    // \u65e2\u77e5\u306e dp[1] \u3068 dp[2] \u3092\u8fd4\u3059\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] \u306e\u8a18\u9332\u304c\u3042\u308b\u5834\u5408\u3001\u305d\u308c\u3092\u8fd4\u3059\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // dp[i] \u3092\u8a18\u9332\n    mem[i] = count;\n    return count;\n}\n\n/* \u968e\u6bb5\u767b\u308a\uff1a\u30e1\u30e2\u5316\u63a2\u7d22 */\nint climbingStairsDFSMem(int n) {\n    // mem[i] \u306f i \u6bb5\u76ee\u306b\u767b\u308b\u7dcf\u89e3\u6570\u3092\u8a18\u9332\u3001-1 \u306f\u8a18\u9332\u306a\u3057\u3092\u610f\u5473\u3059\u308b\n    int[] mem = new int[n + 1];\n    Arrays.fill(mem, -1);\n    return dfs(n, mem);\n}\n
        climbing_stairs_dfs_mem.cs
        [class]{climbing_stairs_dfs_mem}-[func]{DFS}\n\n[class]{climbing_stairs_dfs_mem}-[func]{ClimbingStairsDFSMem}\n
        climbing_stairs_dfs_mem.go
        [class]{}-[func]{dfsMem}\n\n[class]{}-[func]{climbingStairsDFSMem}\n
        climbing_stairs_dfs_mem.swift
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFSMem}\n
        climbing_stairs_dfs_mem.js
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFSMem}\n
        climbing_stairs_dfs_mem.ts
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFSMem}\n
        climbing_stairs_dfs_mem.dart
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFSMem}\n
        climbing_stairs_dfs_mem.rs
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbing_stairs_dfs_mem}\n
        climbing_stairs_dfs_mem.c
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFSMem}\n
        climbing_stairs_dfs_mem.kt
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbingStairsDFSMem}\n
        climbing_stairs_dfs_mem.rb
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{climbing_stairs_dfs_mem}\n

        \u4e0b\u306e\u56f3\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001**\u30e1\u30e2\u5316\u5f8c\u3001\u3059\u3079\u3066\u306e\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u306f\u4e00\u5ea6\u3060\u3051\u8a08\u7b97\u3055\u308c\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u3092 \\(O(n)\\) \u306b\u6700\u9069\u5316**\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u5927\u5e45\u306a\u6539\u5584\u3067\u3059\u3002

        \u56f3 14-4 \u00a0 \u30e1\u30e2\u5316\u63a2\u7d22\u306b\u3088\u308b\u518d\u5e30\u6728

        "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1413-3","title":"14.1.3 \u00a0 \u65b9\u6cd53\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0","text":"

        **\u30e1\u30e2\u5316\u63a2\u7d22\u306f\u300c\u30c8\u30c3\u30d7\u30c0\u30a6\u30f3\u300d\u65b9\u5f0f**\u3067\u3059\uff1a\u5143\u306e\u554f\u984c\uff08\u6839\u30ce\u30fc\u30c9\uff09\u304b\u3089\u59cb\u3081\u3066\u3001\u3088\u308a\u5927\u304d\u306a\u90e8\u5206\u554f\u984c\u3092\u3088\u308a\u5c0f\u3055\u306a\u3082\u306e\u306b\u518d\u5e30\u7684\u306b\u5206\u89e3\u3057\u3001\u6700\u5c0f\u306e\u65e2\u77e5\u306e\u90e8\u5206\u554f\u984c\uff08\u8449\u30ce\u30fc\u30c9\uff09\u306e\u89e3\u306b\u5230\u9054\u3059\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002\u305d\u306e\u5f8c\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u306b\u3088\u308a\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u53ce\u96c6\u3057\u3001\u5143\u306e\u554f\u984c\u306e\u89e3\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002

        \u4e00\u65b9\u3001**\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u300c\u30dc\u30c8\u30e0\u30a2\u30c3\u30d7\u300d\u65b9\u5f0f**\u3067\u3059\uff1a\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u304b\u3089\u59cb\u3081\u3066\u3001\u5143\u306e\u554f\u984c\u304c\u89e3\u6c7a\u3055\u308c\u308b\u307e\u3067\u3001\u3088\u308a\u5927\u304d\u306a\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u53cd\u5fa9\u7684\u306b\u69cb\u7bc9\u3057\u307e\u3059\u3002

        \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3092\u5fc5\u8981\u3068\u3057\u306a\u3044\u305f\u3081\u3001\u30eb\u30fc\u30d7\u3092\u4f7f\u3063\u305f\u53cd\u5fa9\u306e\u307f\u304c\u5fc5\u8981\u3067\u3001\u518d\u5e30\u306f\u4e0d\u8981\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3001\u914d\u5217 dp \u3092\u521d\u671f\u5316\u3057\u3066\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4fdd\u5b58\u3057\u3001\u30e1\u30e2\u5316\u63a2\u7d22\u306e\u914d\u5217 mem \u3068\u540c\u3058\u8a18\u9332\u6a5f\u80fd\u3092\u679c\u305f\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dp.py
        def climbing_stairs_dp(n: int) -> int:\n    \"\"\"\u968e\u6bb5\u767b\u308a\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    if n == 1 or n == 2:\n        return n\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u4f7f\u7528\n    dp = [0] * (n + 1)\n    # \u521d\u671f\u72b6\u614b\uff1a\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4e8b\u524d\u8a2d\u5b9a\n    dp[1], dp[2] = 1, 2\n    # \u72b6\u614b\u9077\u79fb\uff1a\u5c0f\u3055\u3044\u90e8\u5206\u554f\u984c\u304b\u3089\u5927\u304d\u3044\u90e8\u5206\u554f\u984c\u3092\u6bb5\u968e\u7684\u306b\u89e3\u304f\n    for i in range(3, n + 1):\n        dp[i] = dp[i - 1] + dp[i - 2]\n    return dp[n]\n
        climbing_stairs_dp.cpp
        /* \u968e\u6bb5\u767b\u308a\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint climbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3057\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    vector<int> dp(n + 1);\n    // \u521d\u671f\u72b6\u614b\uff1a\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4e8b\u524d\u8a2d\u5b9a\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u72b6\u614b\u9077\u79fb\uff1a\u5c0f\u3055\u306a\u554f\u984c\u304b\u3089\u5927\u304d\u306a\u90e8\u5206\u554f\u984c\u3092\u6bb5\u968e\u7684\u306b\u89e3\u304f\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
        climbing_stairs_dp.java
        /* \u968e\u6bb5\u767b\u308a\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint climbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3057\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    int[] dp = new int[n + 1];\n    // \u521d\u671f\u72b6\u614b\uff1a\u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4e8b\u524d\u8a2d\u5b9a\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u72b6\u614b\u9077\u79fb\uff1a\u5c0f\u3055\u306a\u554f\u984c\u304b\u3089\u5927\u304d\u306a\u90e8\u5206\u554f\u984c\u3092\u6bb5\u968e\u7684\u306b\u89e3\u304f\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
        climbing_stairs_dp.cs
        [class]{climbing_stairs_dp}-[func]{ClimbingStairsDP}\n
        climbing_stairs_dp.go
        [class]{}-[func]{climbingStairsDP}\n
        climbing_stairs_dp.swift
        [class]{}-[func]{climbingStairsDP}\n
        climbing_stairs_dp.js
        [class]{}-[func]{climbingStairsDP}\n
        climbing_stairs_dp.ts
        [class]{}-[func]{climbingStairsDP}\n
        climbing_stairs_dp.dart
        [class]{}-[func]{climbingStairsDP}\n
        climbing_stairs_dp.rs
        [class]{}-[func]{climbing_stairs_dp}\n
        climbing_stairs_dp.c
        [class]{}-[func]{climbingStairsDP}\n
        climbing_stairs_dp.kt
        [class]{}-[func]{climbingStairsDP}\n
        climbing_stairs_dp.rb
        [class]{}-[func]{climbing_stairs_dp}\n

        \u4e0b\u306e\u56f3\u306f\u4e0a\u8a18\u30b3\u30fc\u30c9\u306e\u5b9f\u884c\u30d7\u30ed\u30bb\u30b9\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 14-5 \u00a0 \u968e\u6bb5\u767b\u308a\u306e\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9

        \u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u540c\u69d8\u306b\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3082\u300c\u72b6\u614b\u300d\u306e\u6982\u5ff5\u3092\u4f7f\u7528\u3057\u3066\u554f\u984c\u89e3\u6c7a\u306e\u7279\u5b9a\u306e\u6bb5\u968e\u3092\u8868\u73fe\u3057\u3001\u5404\u72b6\u614b\u306f\u90e8\u5206\u554f\u984c\u3068\u305d\u306e\u5c40\u6240\u6700\u9069\u89e3\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u968e\u6bb5\u767b\u308a\u554f\u984c\u306e\u72b6\u614b\u306f\u73fe\u5728\u306e\u30b9\u30c6\u30c3\u30d7\u756a\u53f7 \\(i\\) \u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u307e\u3059\u3002

        \u4e0a\u8a18\u306e\u5185\u5bb9\u306b\u57fa\u3065\u3044\u3066\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3067\u3088\u304f\u4f7f\u7528\u3055\u308c\u308b\u7528\u8a9e\u3092\u307e\u3068\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        • \u914d\u5217 dp \u306fDP\u30c6\u30fc\u30d6\u30eb\u3068\u547c\u3070\u308c\u3001\\(dp[i]\\) \u306f\u72b6\u614b \\(i\\) \u306b\u5bfe\u5fdc\u3059\u308b\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u8868\u3057\u307e\u3059\u3002
        • \u6700\u5c0f\u306e\u90e8\u5206\u554f\u984c\uff08\u30b9\u30c6\u30c3\u30d7 \\(1\\) \u3068 \\(2\\)\uff09\u306b\u5bfe\u5fdc\u3059\u308b\u72b6\u614b\u306f\u521d\u671f\u72b6\u614b\u3068\u547c\u3070\u308c\u307e\u3059\u3002
        • \u518d\u5e30\u5f0f \\(dp[i] = dp[i-1] + dp[i-2]\\) \u306f\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3068\u547c\u3070\u308c\u307e\u3059\u3002
        "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1414","title":"14.1.4 \u00a0 \u7a7a\u9593\u6700\u9069\u5316","text":"

        \u6ce8\u610f\u6df1\u3044\u8aad\u8005\u306f**\\(dp[i]\\) \u306f \\(dp[i-1]\\) \u3068 \\(dp[i-2]\\) \u306e\u307f\u306b\u95a2\u9023\u3059\u308b\u305f\u3081\u3001\u3059\u3079\u3066\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4fdd\u5b58\u3059\u308b\u305f\u3081\u306b\u914d\u5217 dp \u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u306a\u3044**\u3053\u3068\u306b\u6c17\u3065\u304f\u3067\u3057\u3087\u3046\u3002\u5358\u306b2\u3064\u306e\u5909\u6570\u3092\u4f7f\u3063\u3066\u53cd\u5fa9\u7684\u306b\u9032\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dp.py
        def climbing_stairs_dp_comp(n: int) -> int:\n    \"\"\"\u968e\u6bb5\u767b\u308a\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    if n == 1 or n == 2:\n        return n\n    a, b = 1, 2\n    for _ in range(3, n + 1):\n        a, b = b, a + b\n    return b\n
        climbing_stairs_dp.cpp
        /* \u968e\u6bb5\u767b\u308a\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint climbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
        climbing_stairs_dp.java
        /* \u968e\u6bb5\u767b\u308a\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint climbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
        climbing_stairs_dp.cs
        [class]{climbing_stairs_dp}-[func]{ClimbingStairsDPComp}\n
        climbing_stairs_dp.go
        [class]{}-[func]{climbingStairsDPComp}\n
        climbing_stairs_dp.swift
        [class]{}-[func]{climbingStairsDPComp}\n
        climbing_stairs_dp.js
        [class]{}-[func]{climbingStairsDPComp}\n
        climbing_stairs_dp.ts
        [class]{}-[func]{climbingStairsDPComp}\n
        climbing_stairs_dp.dart
        [class]{}-[func]{climbingStairsDPComp}\n
        climbing_stairs_dp.rs
        [class]{}-[func]{climbing_stairs_dp_comp}\n
        climbing_stairs_dp.c
        [class]{}-[func]{climbingStairsDPComp}\n
        climbing_stairs_dp.kt
        [class]{}-[func]{climbingStairsDPComp}\n
        climbing_stairs_dp.rb
        [class]{}-[func]{climbing_stairs_dp_comp}\n

        \u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\u914d\u5217 dp \u304c\u5360\u6709\u3057\u3066\u3044\u305f\u7a7a\u9593\u304c\u524a\u9664\u3055\u308c\u308b\u305f\u3081\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u304b\u3089 \\(O(1)\\) \u306b\u524a\u6e1b\u3055\u308c\u307e\u3059\u3002

        \u591a\u304f\u306e\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u3067\u306f\u3001\u73fe\u5728\u306e\u72b6\u614b\u306f\u9650\u3089\u308c\u305f\u6570\u306e\u524d\u306e\u72b6\u614b\u306e\u307f\u306b\u4f9d\u5b58\u3059\u308b\u305f\u3081\u3001\u5fc5\u8981\u306a\u72b6\u614b\u306e\u307f\u3092\u4fdd\u6301\u3057\u3001\u300c\u6b21\u5143\u524a\u6e1b\u300d\u306b\u3088\u3063\u3066\u30e1\u30e2\u30ea\u7a7a\u9593\u3092\u7bc0\u7d04\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u7a7a\u9593\u6700\u9069\u5316\u6280\u8853\u306f\u300c\u30ed\u30fc\u30ea\u30f3\u30b0\u5909\u6570\u300d\u307e\u305f\u306f\u300c\u30ed\u30fc\u30ea\u30f3\u30b0\u914d\u5217\u300d\u3068\u3057\u3066\u77e5\u3089\u308c\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_dynamic_programming/knapsack_problem/","title":"14.4 \u00a0 0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c","text":"

        \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306f\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306e\u512a\u308c\u305f\u5165\u9580\u554f\u984c\u3067\u3042\u308a\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3067\u6700\u3082\u4e00\u822c\u7684\u306a\u554f\u984c\u30bf\u30a4\u30d7\u3067\u3059\u30020-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3001\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3001\u8907\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306a\u3069\u3001\u591a\u304f\u306e\u5909\u7a2e\u304c\u3042\u308a\u307e\u3059\u3002

        \u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u307e\u305a\u6700\u3082\u4e00\u822c\u7684\u306a0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3092\u89e3\u6c7a\u3057\u307e\u3059\u3002

        Question

        \\(n\\) \u500b\u306e\u30a2\u30a4\u30c6\u30e0\u304c\u4e0e\u3048\u3089\u308c\u3001\\(i\\) \u756a\u76ee\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u91cd\u91cf\u306f \\(wgt[i-1]\\) \u3067\u5024\u306f \\(val[i-1]\\) \u3067\u3059\u3002\u5bb9\u91cf\u304c \\(cap\\) \u306e\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u304c\u3042\u308a\u307e\u3059\u3002\u5404\u30a2\u30a4\u30c6\u30e0\u306f1\u56de\u306e\u307f\u9078\u629e\u3067\u304d\u307e\u3059\u3002\u5bb9\u91cf\u5236\u9650\u4e0b\u3067\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u30a2\u30a4\u30c6\u30e0\u306e\u6700\u5927\u5024\u306f\u4f55\u3067\u3059\u304b\uff1f

        \u4e0b\u306e\u56f3\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\u30a2\u30a4\u30c6\u30e0\u756a\u53f7 \\(i\\) \u306f1\u304b\u3089\u6570\u3048\u59cb\u3081\u3001\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f0\u304b\u3089\u59cb\u307e\u308b\u305f\u3081\u3001\u30a2\u30a4\u30c6\u30e0 \\(i\\) \u306e\u91cd\u91cf\u306f \\(wgt[i-1]\\) \u306b\u5bfe\u5fdc\u3057\u3001\u5024\u306f \\(val[i-1]\\) \u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002

        \u56f3 14-17 \u00a0 0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u4f8b\u30c7\u30fc\u30bf

        0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3092 \\(n\\) \u30e9\u30a6\u30f3\u30c9\u306e\u6c7a\u5b9a\u304b\u3089\u69cb\u6210\u3055\u308c\u308b\u30d7\u30ed\u30bb\u30b9\u3068\u3057\u3066\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u5404\u30a2\u30a4\u30c6\u30e0\u306b\u3064\u3044\u3066\u5165\u308c\u306a\u3044\u3001\u307e\u305f\u306f\u5165\u308c\u308b\u3068\u3044\u30462\u3064\u306e\u6c7a\u5b9a\u304c\u3042\u308a\u3001\u3057\u305f\u304c\u3063\u3066\u554f\u984c\u306f\u6c7a\u5b9a\u6728\u30e2\u30c7\u30eb\u306b\u9069\u5408\u3057\u307e\u3059\u3002

        \u3053\u306e\u554f\u984c\u306e\u76ee\u7684\u306f\u300c\u9650\u3089\u308c\u305f\u5bb9\u91cf\u306e\u4e0b\u3067\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u30a2\u30a4\u30c6\u30e0\u306e\u5024\u3092\u6700\u5927\u5316\u3059\u308b\u300d\u3053\u3068\u3067\u3042\u308a\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u3067\u3042\u308b\u53ef\u80fd\u6027\u304c\u9ad8\u3044\u3067\u3059\u3002

        \u7b2c1\u30b9\u30c6\u30c3\u30d7\uff1a\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u6c7a\u5b9a\u3092\u8003\u3048\u3001\u72b6\u614b\u3092\u5b9a\u7fa9\u3057\u3001\u305d\u308c\u306b\u3088\u308a \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u3092\u5f97\u308b

        \u5404\u30a2\u30a4\u30c6\u30e0\u306b\u3064\u3044\u3066\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u306a\u3051\u308c\u3070\u5bb9\u91cf\u306f\u5909\u308f\u3089\u305a\u3001\u5165\u308c\u308c\u3070\u5bb9\u91cf\u306f\u6e1b\u5c11\u3057\u307e\u3059\u3002\u3053\u308c\u304b\u3089\u72b6\u614b\u5b9a\u7fa9\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a\u73fe\u5728\u306e\u30a2\u30a4\u30c6\u30e0\u756a\u53f7 \\(i\\) \u3068\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf \\(c\\)\u3001\\([i, c]\\) \u3068\u8868\u8a18\u3055\u308c\u307e\u3059\u3002

        \u72b6\u614b \\([i, c]\\) \u306f\u90e8\u5206\u554f\u984c\u306b\u5bfe\u5fdc\u3057\u307e\u3059\uff1a\u5bb9\u91cf \\(c\\) \u306e\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u3067\u306e\u6700\u521d\u306e \\(i\\) \u500b\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u6700\u5927\u5024\u3001\\(dp[i, c]\\) \u3068\u8868\u8a18\u3055\u308c\u307e\u3059\u3002

        \u63a2\u3057\u3066\u3044\u308b\u89e3\u306f \\(dp[n, cap]\\) \u3067\u3042\u308b\u305f\u3081\u3001\u30b5\u30a4\u30ba \\((n+1) \\times (cap+1)\\) \u306e\u4e8c\u6b21\u5143 \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u304c\u5fc5\u8981\u3067\u3059\u3002

        \u7b2c2\u30b9\u30c6\u30c3\u30d7\uff1a\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u7279\u5b9a\u3057\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3092\u5c0e\u51fa\u3059\u308b

        \u30a2\u30a4\u30c6\u30e0 \\(i\\) \u306e\u6c7a\u5b9a\u3092\u884c\u3063\u305f\u5f8c\u3001\u6b8b\u308b\u306e\u306f\u6700\u521d\u306e \\(i-1\\) \u500b\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u6c7a\u5b9a\u306e\u90e8\u5206\u554f\u984c\u3067\u3042\u308a\u3001\u3053\u308c\u306f2\u3064\u306e\u30b1\u30fc\u30b9\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        • \u30a2\u30a4\u30c6\u30e0 \\(i\\) \u3092\u5165\u308c\u306a\u3044\uff1a\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf\u306f\u5909\u308f\u3089\u305a\u3001\u72b6\u614b\u306f \\([i-1, c]\\) \u306b\u5909\u308f\u308a\u307e\u3059\u3002
        • \u30a2\u30a4\u30c6\u30e0 \\(i\\) \u3092\u5165\u308c\u308b\uff1a\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf\u306f \\(wgt[i-1]\\) \u3060\u3051\u6e1b\u5c11\u3057\u3001\u5024\u306f \\(val[i-1]\\) \u3060\u3051\u5897\u52a0\u3057\u3001\u72b6\u614b\u306f \\([i-1, c-wgt[i-1]]\\) \u306b\u5909\u308f\u308a\u307e\u3059\u3002

        \u4e0a\u8a18\u306e\u5206\u6790\u306b\u3088\u308a\u3001\u3053\u306e\u554f\u984c\u306e\u6700\u9069\u90e8\u5206\u69cb\u9020\u304c\u660e\u3089\u304b\u306b\u306a\u308a\u307e\u3059\uff1a\u6700\u5927\u5024 \\(dp[i, c]\\) \u306f\u3001\u30a2\u30a4\u30c6\u30e0 \\(i\\) \u3092\u5165\u308c\u306a\u3044\u65b9\u6848\u3068\u30a2\u30a4\u30c6\u30e0 \\(i\\) \u3092\u5165\u308c\u308b\u65b9\u6848\u306e2\u3064\u306e\u3046\u3061\u3001\u3088\u308a\u5927\u304d\u306a\u5024\u306b\u7b49\u3057\u3044\u3002\u3053\u308c\u304b\u3089\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3092\u5c0e\u51fa\u3067\u304d\u307e\u3059\uff1a

        \\[ dp[i, c] = \\max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1]) \\]

        \u73fe\u5728\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u91cd\u91cf \\(wgt[i - 1]\\) \u304c\u6b8b\u308a\u306e\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf \\(c\\) \u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u552f\u4e00\u306e\u9078\u629e\u80a2\u306f\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u306a\u3044\u3053\u3068\u3067\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002

        \u7b2c3\u30b9\u30c6\u30c3\u30d7\uff1a\u5883\u754c\u6761\u4ef6\u3068\u72b6\u614b\u9077\u79fb\u306e\u9806\u5e8f\u3092\u6c7a\u5b9a\u3059\u308b

        \u30a2\u30a4\u30c6\u30e0\u304c\u306a\u3044\u5834\u5408\u307e\u305f\u306f\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf\u304c \\(0\\) \u306e\u5834\u5408\u3001\u6700\u5927\u5024\u306f \\(0\\) \u3067\u3059\u3002\u3064\u307e\u308a\u3001\u6700\u521d\u306e\u5217 \\(dp[i, 0]\\) \u3068\u6700\u521d\u306e\u884c \\(dp[0, c]\\) \u306f\u3069\u3061\u3089\u3082 \\(0\\) \u306b\u7b49\u3057\u3044\u3067\u3059\u3002

        \u73fe\u5728\u306e\u72b6\u614b \\([i, c]\\) \u306f\u76f4\u63a5\u4e0a\u306e\u72b6\u614b \\([i-1, c]\\) \u3068\u5de6\u4e0a\u306e\u72b6\u614b \\([i-1, c-wgt[i-1]]\\) \u304b\u3089\u9077\u79fb\u3059\u308b\u305f\u3081\u30012\u5c64\u306e\u30eb\u30fc\u30d7\u3092\u901a\u3058\u3066 \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u5168\u4f53\u3092\u9806\u5e8f\u901a\u308a\u306b\u8d70\u67fb\u3057\u307e\u3059\u3002

        \u4e0a\u8a18\u306e\u5206\u6790\u306b\u5f93\u3063\u3066\u3001\u6b21\u306b\u529b\u4efb\u305b\u63a2\u7d22\u3001\u30e1\u30e2\u5316\u63a2\u7d22\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306e\u9806\u5e8f\u3067\u89e3\u6cd5\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002

        "},{"location":"chapter_dynamic_programming/knapsack_problem/#1-1","title":"1. \u00a0 \u65b9\u6cd51\uff1a\u529b\u4efb\u305b\u63a2\u7d22","text":"

        \u63a2\u7d22\u30b3\u30fc\u30c9\u306b\u306f\u4ee5\u4e0b\u306e\u8981\u7d20\u304c\u542b\u307e\u308c\u307e\u3059\u3002

        • \u518d\u5e30\u30d1\u30e9\u30e1\u30fc\u30bf\uff1a\u72b6\u614b \\([i, c]\\)\u3002
        • \u623b\u308a\u5024\uff1a\u90e8\u5206\u554f\u984c \\(dp[i, c]\\) \u306e\u89e3\u3002
        • \u7d42\u4e86\u6761\u4ef6\uff1a\u30a2\u30a4\u30c6\u30e0\u756a\u53f7\u304c\u7bc4\u56f2\u5916 \\(i = 0\\) \u307e\u305f\u306f\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u6b8b\u308a\u5bb9\u91cf\u304c \\(0\\) \u306e\u3068\u304d\u3001\u518d\u5e30\u3092\u7d42\u4e86\u3057\u5024 \\(0\\) \u3092\u8fd4\u3059\u3002
        • \u679d\u5208\u308a\uff1a\u73fe\u5728\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u91cd\u91cf\u304c\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u6b8b\u308a\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u552f\u4e00\u306e\u9078\u629e\u80a2\u306f\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u306a\u3044\u3053\u3068\u3067\u3059\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
        def knapsack_dfs(wgt: list[int], val: list[int], i: int, c: int) -> int:\n    \"\"\"0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u30d6\u30eb\u30fc\u30c8\u30d5\u30a9\u30fc\u30b9\u63a2\u7d22\"\"\"\n    # \u3059\u3079\u3066\u306e\u30a2\u30a4\u30c6\u30e0\u304c\u9078\u629e\u3055\u308c\u305f\u304b\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u6b8b\u308a\u5bb9\u91cf\u304c\u306a\u3044\u5834\u5408\u3001\u5024 0 \u3092\u8fd4\u3059\n    if i == 0 or c == 0:\n        return 0\n    # \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u306a\u3044\u3053\u3068\u3057\u304b\u9078\u629e\u3067\u304d\u306a\u3044\n    if wgt[i - 1] > c:\n        return knapsack_dfs(wgt, val, i - 1, c)\n    # \u30a2\u30a4\u30c6\u30e0 i \u3092\u5165\u308c\u306a\u3044\u306e\u3068\u5165\u308c\u308b\u306e\u3068\u306e\u6700\u5927\u5024\u3092\u8a08\u7b97\n    no = knapsack_dfs(wgt, val, i - 1, c)\n    yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]\n    # 2 \u3064\u306e\u9078\u629e\u80a2\u306e\u3046\u3061\u5927\u304d\u3044\u5024\u3092\u8fd4\u3059\n    return max(no, yes)\n
        knapsack.cpp
        /* 0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u30d6\u30eb\u30fc\u30c8\u30d5\u30a9\u30fc\u30b9\u63a2\u7d22 */\nint knapsackDFS(vector<int> &wgt, vector<int> &val, int i, int c) {\n    // \u3059\u3079\u3066\u306e\u30a2\u30a4\u30c6\u30e0\u304c\u9078\u629e\u3055\u308c\u305f\u304b\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u6b8b\u308a\u5bb9\u91cf\u304c\u306a\u3044\u5834\u5408\u3001\u5024 0 \u3092\u8fd4\u3059\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u306a\u3044\u3053\u3068\u3057\u304b\u9078\u629e\u3067\u304d\u306a\u3044\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // \u30a2\u30a4\u30c6\u30e0 i \u3092\u5165\u308c\u306a\u3044\u5834\u5408\u3068\u5165\u308c\u308b\u5834\u5408\u306e\u6700\u5927\u5024\u3092\u8a08\u7b97\n    int no = knapsackDFS(wgt, val, i - 1, c);\n    int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // 2\u3064\u306e\u9078\u629e\u80a2\u306e\u3088\u308a\u5927\u304d\u3044\u5024\u3092\u8fd4\u3059\n    return max(no, yes);\n}\n
        knapsack.java
        /* 0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u30d6\u30eb\u30fc\u30c8\u30d5\u30a9\u30fc\u30b9\u63a2\u7d22 */\nint knapsackDFS(int[] wgt, int[] val, int i, int c) {\n    // \u3059\u3079\u3066\u306e\u30a2\u30a4\u30c6\u30e0\u304c\u9078\u629e\u3055\u308c\u305f\u304b\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u6b8b\u308a\u5bb9\u91cf\u304c\u306a\u3044\u5834\u5408\u3001\u5024 0 \u3092\u8fd4\u3059\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u306a\u3044\u3053\u3068\u3057\u304b\u9078\u629e\u3067\u304d\u306a\u3044\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // \u30a2\u30a4\u30c6\u30e0 i \u3092\u5165\u308c\u306a\u3044\u5834\u5408\u3068\u5165\u308c\u308b\u5834\u5408\u306e\u6700\u5927\u5024\u3092\u8a08\u7b97\n    int no = knapsackDFS(wgt, val, i - 1, c);\n    int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // 2\u3064\u306e\u9078\u629e\u80a2\u306e\u3088\u308a\u5927\u304d\u3044\u5024\u3092\u8fd4\u3059\n    return Math.max(no, yes);\n}\n
        knapsack.cs
        [class]{knapsack}-[func]{KnapsackDFS}\n
        knapsack.go
        [class]{}-[func]{knapsackDFS}\n
        knapsack.swift
        [class]{}-[func]{knapsackDFS}\n
        knapsack.js
        [class]{}-[func]{knapsackDFS}\n
        knapsack.ts
        [class]{}-[func]{knapsackDFS}\n
        knapsack.dart
        [class]{}-[func]{knapsackDFS}\n
        knapsack.rs
        [class]{}-[func]{knapsack_dfs}\n
        knapsack.c
        [class]{}-[func]{knapsackDFS}\n
        knapsack.kt
        [class]{}-[func]{knapsackDFS}\n
        knapsack.rb
        [class]{}-[func]{knapsack_dfs}\n

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5404\u30a2\u30a4\u30c6\u30e0\u306f\u9078\u629e\u3057\u306a\u3044\u3068\u9078\u629e\u3059\u308b\u3068\u3044\u30462\u3064\u306e\u63a2\u7d22\u5206\u5c90\u3092\u751f\u6210\u3059\u308b\u305f\u3081\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(2^n)\\) \u3067\u3059\u3002

        \u518d\u5e30\u6728\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\\(dp[1, 10]\\) \u306a\u3069\u306e\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u304c\u3042\u308b\u3053\u3068\u304c\u5bb9\u6613\u306b\u308f\u304b\u308a\u307e\u3059\u3002\u30a2\u30a4\u30c6\u30e0\u304c\u591a\u304f\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf\u304c\u5927\u304d\u3044\u5834\u5408\u3001\u7279\u306b\u540c\u3058\u91cd\u91cf\u306e\u30a2\u30a4\u30c6\u30e0\u304c\u591a\u3044\u5834\u5408\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u306e\u6570\u306f\u5927\u5e45\u306b\u5897\u52a0\u3057\u307e\u3059\u3002

        \u56f3 14-18 \u00a0 0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u529b\u4efb\u305b\u63a2\u7d22\u518d\u5e30\u6728

        "},{"location":"chapter_dynamic_programming/knapsack_problem/#2-2","title":"2. \u00a0 \u65b9\u6cd52\uff1a\u30e1\u30e2\u5316\u63a2\u7d22","text":"

        \u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u304c\u4e00\u5ea6\u3060\u3051\u8a08\u7b97\u3055\u308c\u308b\u3053\u3068\u3092\u78ba\u4fdd\u3059\u308b\u305f\u3081\u306b\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u8a18\u9332\u3059\u308b\u30e1\u30e2\u5316\u30ea\u30b9\u30c8 mem \u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u3053\u3053\u3067 mem[i][c] \u306f \\(dp[i, c]\\) \u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002

        \u30e1\u30e2\u5316\u3092\u5c0e\u5165\u3057\u305f\u5f8c\u3001**\u6642\u9593\u8a08\u7b97\u91cf\u306f\u90e8\u5206\u554f\u984c\u306e\u6570\u306b\u4f9d\u5b58**\u3057\u3001\\(O(n \\times cap)\\) \u306b\u306a\u308a\u307e\u3059\u3002\u5b9f\u88c5\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
        def knapsack_dfs_mem(\n    wgt: list[int], val: list[int], mem: list[list[int]], i: int, c: int\n) -> int:\n    \"\"\"0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u8a18\u61b6\u5316\u63a2\u7d22\"\"\"\n    # \u3059\u3079\u3066\u306e\u30a2\u30a4\u30c6\u30e0\u304c\u9078\u629e\u3055\u308c\u305f\u304b\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u6b8b\u308a\u5bb9\u91cf\u304c\u306a\u3044\u5834\u5408\u3001\u5024 0 \u3092\u8fd4\u3059\n    if i == 0 or c == 0:\n        return 0\n    # \u8a18\u9332\u304c\u3042\u308b\u5834\u5408\u3001\u305d\u308c\u3092\u8fd4\u3059\n    if mem[i][c] != -1:\n        return mem[i][c]\n    # \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u306a\u3044\u3053\u3068\u3057\u304b\u9078\u629e\u3067\u304d\u306a\u3044\n    if wgt[i - 1] > c:\n        return knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n    # \u30a2\u30a4\u30c6\u30e0 i \u3092\u5165\u308c\u306a\u3044\u306e\u3068\u5165\u308c\u308b\u306e\u3068\u306e\u6700\u5927\u5024\u3092\u8a08\u7b97\n    no = knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n    yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1]\n    # 2 \u3064\u306e\u9078\u629e\u80a2\u306e\u3046\u3061\u5927\u304d\u3044\u5024\u3092\u8a18\u9332\u3057\u3066\u8fd4\u3059\n    mem[i][c] = max(no, yes)\n    return mem[i][c]\n
        knapsack.cpp
        [class]{}-[func]{knapsackDFSMem}\n
        knapsack.java
        /* 0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u30e1\u30e2\u5316\u63a2\u7d22 */\nint knapsackDFSMem(int[] wgt, int[] val, int[][] mem, int i, int c) {\n    // \u3059\u3079\u3066\u306e\u30a2\u30a4\u30c6\u30e0\u304c\u9078\u629e\u3055\u308c\u305f\u304b\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u6b8b\u308a\u5bb9\u91cf\u304c\u306a\u3044\u5834\u5408\u3001\u5024 0 \u3092\u8fd4\u3059\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u8a18\u9332\u304c\u3042\u308b\u5834\u5408\u3001\u305d\u308c\u3092\u8fd4\u3059\n    if (mem[i][c] != -1) {\n        return mem[i][c];\n    }\n    // \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u306a\u3044\u3053\u3068\u3057\u304b\u9078\u629e\u3067\u304d\u306a\u3044\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, mem, i - 1, c);\n    }\n    // \u30a2\u30a4\u30c6\u30e0 i \u3092\u5165\u308c\u306a\u3044\u5834\u5408\u3068\u5165\u308c\u308b\u5834\u5408\u306e\u6700\u5927\u5024\u3092\u8a08\u7b97\n    int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n    int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // 2\u3064\u306e\u9078\u629e\u80a2\u306e\u3088\u308a\u5927\u304d\u3044\u5024\u3092\u8a18\u9332\u3057\u3066\u8fd4\u3059\n    mem[i][c] = Math.max(no, yes);\n    return mem[i][c];\n}\n
        knapsack.cs
        [class]{knapsack}-[func]{KnapsackDFSMem}\n
        knapsack.go
        [class]{}-[func]{knapsackDFSMem}\n
        knapsack.swift
        [class]{}-[func]{knapsackDFSMem}\n
        knapsack.js
        [class]{}-[func]{knapsackDFSMem}\n
        knapsack.ts
        [class]{}-[func]{knapsackDFSMem}\n
        knapsack.dart
        [class]{}-[func]{knapsackDFSMem}\n
        knapsack.rs
        [class]{}-[func]{knapsack_dfs_mem}\n
        knapsack.c
        [class]{}-[func]{knapsackDFSMem}\n
        knapsack.kt
        [class]{}-[func]{knapsackDFSMem}\n
        knapsack.rb
        [class]{}-[func]{knapsack_dfs_mem}\n

        \u4e0b\u306e\u56f3\u306f\u30e1\u30e2\u5316\u63a2\u7d22\u3067\u679d\u5208\u308a\u3055\u308c\u308b\u63a2\u7d22\u5206\u5c90\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 14-19 \u00a0 0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u30e1\u30e2\u5316\u63a2\u7d22\u518d\u5e30\u6728

        "},{"location":"chapter_dynamic_programming/knapsack_problem/#3-3","title":"3. \u00a0 \u65b9\u6cd53\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0","text":"

        \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u672c\u8cea\u7684\u306b\u72b6\u614b\u9077\u79fb\u4e2d\u306b \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u3092\u57cb\u3081\u308b\u3053\u3068\u3092\u542b\u307f\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
        def knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n = len(wgt)\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    dp = [[0] * (cap + 1) for _ in range(n + 1)]\n    # \u72b6\u614b\u9077\u79fb\n    for i in range(1, n + 1):\n        for c in range(1, cap + 1):\n            if wgt[i - 1] > c:\n                # \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][c] = dp[i - 1][c]\n            else:\n                # \u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\u306e\u3068\u9078\u629e\u3059\u308b\u306e\u3068\u3067\u5927\u304d\u3044\u5024\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\n    return dp[n][cap]\n
        knapsack.cpp
        /* 0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint knapsackDP(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5927\u304d\u3044\u5024\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        knapsack.java
        /* 0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint knapsackDP(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    int[][] dp = new int[n + 1][cap + 1];\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5927\u304d\u3044\u5024\n                dp[i][c] = Math.max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        knapsack.cs
        [class]{knapsack}-[func]{KnapsackDP}\n
        knapsack.go
        [class]{}-[func]{knapsackDP}\n
        knapsack.swift
        [class]{}-[func]{knapsackDP}\n
        knapsack.js
        [class]{}-[func]{knapsackDP}\n
        knapsack.ts
        [class]{}-[func]{knapsackDP}\n
        knapsack.dart
        [class]{}-[func]{knapsackDP}\n
        knapsack.rs
        [class]{}-[func]{knapsack_dp}\n
        knapsack.c
        [class]{}-[func]{knapsackDP}\n
        knapsack.kt
        [class]{}-[func]{knapsackDP}\n
        knapsack.rb
        [class]{}-[func]{knapsack_dp}\n

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u6642\u9593\u8a08\u7b97\u91cf\u3068\u7a7a\u9593\u8a08\u7b97\u91cf\u306e\u4e21\u65b9\u304c\u914d\u5217 dp \u306e\u30b5\u30a4\u30ba\u3001\u3064\u307e\u308a \\(O(n \\times cap)\\) \u306b\u3088\u3063\u3066\u6c7a\u5b9a\u3055\u308c\u307e\u3059\u3002

        <1><2><3><4><5><6><7><8><9><10><11><12><13><14>

        \u56f3 14-20 \u00a0 0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9

        "},{"location":"chapter_dynamic_programming/knapsack_problem/#4","title":"4. \u00a0 \u7a7a\u9593\u6700\u9069\u5316","text":"

        \u5404\u72b6\u614b\u306f\u4e0a\u306e\u884c\u306e\u72b6\u614b\u306e\u307f\u306b\u95a2\u9023\u3059\u308b\u305f\u3081\u30012\u3064\u306e\u914d\u5217\u3092\u4f7f\u7528\u3057\u3066\u30ed\u30fc\u30ea\u30f3\u30b0\u524d\u9032\u3055\u305b\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u3092 \\(O(n^2)\\) \u304b\u3089 \\(O(n)\\) \u306b\u524a\u6e1b\u3067\u304d\u307e\u3059\u3002

        \u3055\u3089\u306b\u8003\u3048\u3066\u307f\u308b\u3068\u30011\u3064\u306e\u914d\u5217\u3060\u3051\u3067\u7a7a\u9593\u6700\u9069\u5316\u3092\u9054\u6210\u3067\u304d\u308b\u3067\u3057\u3087\u3046\u304b\uff1f\u5404\u72b6\u614b\u304c\u76f4\u63a5\u4e0a\u306e\u30bb\u30eb\u307e\u305f\u306f\u5de6\u4e0a\u306e\u30bb\u30eb\u304b\u3089\u9077\u79fb\u3059\u308b\u3053\u3068\u304c\u89b3\u5bdf\u3067\u304d\u307e\u3059\u3002\u914d\u5217\u304c1\u3064\u3057\u304b\u306a\u3044\u5834\u5408\u3001\\(i\\) \u884c\u76ee\u306e\u8d70\u67fb\u3092\u958b\u59cb\u3059\u308b\u3068\u304d\u3001\u305d\u306e\u914d\u5217\u306f\u307e\u3060 \\(i-1\\) \u884c\u76ee\u306e\u72b6\u614b\u3092\u4fdd\u5b58\u3057\u3066\u3044\u307e\u3059\u3002

        • \u901a\u5e38\u306e\u9806\u5e8f\u3067\u8d70\u67fb\u3059\u308b\u5834\u5408\u3001\\(dp[i, j]\\) \u306b\u8d70\u67fb\u3057\u305f\u3068\u304d\u3001\u5de6\u4e0a\u306e \\(dp[i-1, 1]\\) \uff5e \\(dp[i-1, j-1]\\) \u306e\u5024\u304c\u3059\u3067\u306b\u4e0a\u66f8\u304d\u3055\u308c\u3066\u3044\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u6b63\u3057\u3044\u72b6\u614b\u9077\u79fb\u7d50\u679c\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u305b\u3093\u3002
        • \u9006\u9806\u3067\u8d70\u67fb\u3059\u308b\u5834\u5408\u3001\u4e0a\u66f8\u304d\u554f\u984c\u306f\u306a\u304f\u3001\u72b6\u614b\u9077\u79fb\u3092\u6b63\u3057\u304f\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002

        \u4e0b\u306e\u56f3\u306f\u5358\u4e00\u914d\u5217\u3067\u306e \\(i = 1\\) \u884c\u76ee\u304b\u3089 \\(i = 2\\) \u884c\u76ee\u3078\u306e\u9077\u79fb\u30d7\u30ed\u30bb\u30b9\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u901a\u5e38\u9806\u5e8f\u8d70\u67fb\u3068\u9006\u9806\u8d70\u67fb\u306e\u9055\u3044\u306b\u3064\u3044\u3066\u8003\u3048\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002

        <1><2><3><4><5><6>

        \u56f3 14-21 \u00a0 0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9

        \u30b3\u30fc\u30c9\u5b9f\u88c5\u3067\u306f\u3001\u914d\u5217 dp \u306e\u6700\u521d\u306e\u6b21\u5143 \\(i\\) \u3092\u524a\u9664\u3057\u3001\u5185\u5074\u306e\u30eb\u30fc\u30d7\u3092\u9006\u8d70\u67fb\u306b\u5909\u66f4\u3059\u308b\u3060\u3051\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
        def knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n = len(wgt)\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    dp = [0] * (cap + 1)\n    # \u72b6\u614b\u9077\u79fb\n    for i in range(1, n + 1):\n        # \u9006\u9806\u3067\u8d70\u67fb\n        for c in range(cap, 0, -1):\n            if wgt[i - 1] > c:\n                # \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[c] = dp[c]\n            else:\n                # \u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\u306e\u3068\u9078\u629e\u3059\u308b\u306e\u3068\u3067\u5927\u304d\u3044\u5024\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n    return dp[cap]\n
        knapsack.cpp
        /* 0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint knapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    vector<int> dp(cap + 1, 0);\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        // \u9006\u9806\u3067\u8d70\u67fb\n        for (int c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5927\u304d\u3044\u5024\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        knapsack.java
        /* 0-1 \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint knapsackDPComp(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    int[] dp = new int[cap + 1];\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        // \u9006\u9806\u3067\u8d70\u67fb\n        for (int c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5927\u304d\u3044\u5024\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        knapsack.cs
        [class]{knapsack}-[func]{KnapsackDPComp}\n
        knapsack.go
        [class]{}-[func]{knapsackDPComp}\n
        knapsack.swift
        [class]{}-[func]{knapsackDPComp}\n
        knapsack.js
        [class]{}-[func]{knapsackDPComp}\n
        knapsack.ts
        [class]{}-[func]{knapsackDPComp}\n
        knapsack.dart
        [class]{}-[func]{knapsackDPComp}\n
        knapsack.rs
        [class]{}-[func]{knapsack_dp_comp}\n
        knapsack.c
        [class]{}-[func]{knapsackDPComp}\n
        knapsack.kt
        [class]{}-[func]{knapsackDPComp}\n
        knapsack.rb
        [class]{}-[func]{knapsack_dp_comp}\n
        "},{"location":"chapter_dynamic_programming/summary/","title":"14.7 \u00a0 \u307e\u3068\u3081","text":"
        • \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u554f\u984c\u3092\u5206\u89e3\u3057\u3001\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4fdd\u5b58\u3059\u308b\u3053\u3068\u3067\u5197\u9577\u306a\u8a08\u7b97\u3092\u907f\u3051\u3001\u8a08\u7b97\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002
        • \u6642\u9593\u3092\u8003\u616e\u3057\u306a\u3051\u308c\u3070\u3001\u3059\u3079\u3066\u306e\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u306f\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\uff08\u529b\u4efb\u305b\u63a2\u7d22\uff09\u3092\u4f7f\u7528\u3057\u3066\u89e3\u6c7a\u3067\u304d\u307e\u3059\u304c\u3001\u518d\u5e30\u6728\u306b\u306f\u591a\u304f\u306e\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u304c\u3042\u308a\u3001\u52b9\u7387\u304c\u975e\u5e38\u306b\u4f4e\u304f\u306a\u308a\u307e\u3059\u3002\u8a18\u61b6\u5316\u30ea\u30b9\u30c8\u3092\u5c0e\u5165\u3059\u308b\u3053\u3068\u3067\u3001\u8a08\u7b97\u3055\u308c\u305f\u3059\u3079\u3066\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4fdd\u5b58\u3057\u3001\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u304c\u4e00\u5ea6\u3060\u3051\u8a08\u7b97\u3055\u308c\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3067\u304d\u307e\u3059\u3002
        • \u8a18\u61b6\u5316\u63a2\u7d22\u306f\u30c8\u30c3\u30d7\u30c0\u30a6\u30f3\u306e\u518d\u5e30\u89e3\u6cd5\u3067\u3042\u308a\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u30dc\u30c8\u30e0\u30a2\u30c3\u30d7\u306e\u53cd\u5fa9\u30a2\u30d7\u30ed\u30fc\u30c1\u306b\u5bfe\u5fdc\u3057\u3001\u300c\u8868\u3092\u57cb\u3081\u308b\u300d\u3053\u3068\u306b\u4f3c\u3066\u3044\u307e\u3059\u3002\u73fe\u5728\u306e\u72b6\u614b\u306f\u7279\u5b9a\u306e\u5c40\u6240\u72b6\u614b\u306e\u307f\u306b\u4f9d\u5b58\u3059\u308b\u305f\u3081\u3001dp\u30c6\u30fc\u30d6\u30eb\u306e1\u6b21\u5143\u3092\u524a\u9664\u3057\u3066\u7a7a\u9593\u8a08\u7b97\u91cf\u3092\u524a\u6e1b\u3067\u304d\u307e\u3059\u3002
        • \u90e8\u5206\u554f\u984c\u306e\u5206\u89e3\u306f\u6c4e\u7528\u7684\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30a2\u30d7\u30ed\u30fc\u30c1\u3067\u3042\u308a\u3001\u5206\u5272\u7d71\u6cbb\u6cd5\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3067\u7279\u5fb4\u304c\u7570\u306a\u308a\u307e\u3059\u3002
        • \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u306b\u306f3\u3064\u306e\u4e3b\u8981\u306a\u7279\u5fb4\u304c\u3042\u308a\u307e\u3059\uff1a\u91cd\u8907\u3059\u308b\u90e8\u5206\u554f\u984c\u3001\u6700\u9069\u90e8\u5206\u69cb\u9020\u3001\u7121\u8a18\u61b6\u6027\u3002
        • \u5143\u306e\u554f\u984c\u306e\u6700\u9069\u89e3\u304c\u305d\u306e\u90e8\u5206\u554f\u984c\u306e\u6700\u9069\u89e3\u304b\u3089\u69cb\u7bc9\u3067\u304d\u308b\u5834\u5408\u3001\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u6301\u3061\u307e\u3059\u3002
        • \u7121\u8a18\u61b6\u6027\u3068\u306f\u3001\u72b6\u614b\u306e\u5c06\u6765\u306e\u767a\u5c55\u304c\u73fe\u5728\u306e\u72b6\u614b\u306e\u307f\u306b\u4f9d\u5b58\u3057\u3001\u904e\u53bb\u306b\u7d4c\u9a13\u3057\u305f\u3059\u3079\u3066\u306e\u72b6\u614b\u306b\u4f9d\u5b58\u3057\u306a\u3044\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u591a\u304f\u306e\u7d44\u307f\u5408\u308f\u305b\u6700\u9069\u5316\u554f\u984c\u306b\u306f\u3053\u306e\u7279\u6027\u304c\u306a\u304f\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u3066\u8fc5\u901f\u306b\u89e3\u6c7a\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002

        \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c

        • \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306f\u6700\u3082\u5178\u578b\u7684\u306a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u306e1\u3064\u3067\u30010-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u3001\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u3001\u8907\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306a\u3069\u306e\u5909\u7a2e\u304c\u3042\u308a\u307e\u3059\u3002
        • 0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u72b6\u614b\u5b9a\u7fa9\u306f\u3001\u6700\u521d\u306e \\(i\\) \u500b\u306e\u30a2\u30a4\u30c6\u30e0\u3092\u542b\u3080\u5bb9\u91cf \\(c\\) \u306e\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u3067\u306e\u6700\u5927\u5024\u3067\u3059\u3002\u30a2\u30a4\u30c6\u30e0\u3092\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u306a\u3044\u307e\u305f\u306f\u5165\u308c\u308b\u3068\u3044\u3046\u6c7a\u5b9a\u306b\u57fa\u3065\u3044\u3066\u3001\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u7279\u5b9a\u3057\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3092\u69cb\u7bc9\u3067\u304d\u307e\u3059\u3002\u7a7a\u9593\u6700\u9069\u5316\u3067\u306f\u3001\u5404\u72b6\u614b\u304c\u76f4\u63a5\u4e0a\u3068\u5de6\u4e0a\u306e\u72b6\u614b\u306b\u4f9d\u5b58\u3059\u308b\u305f\u3081\u3001\u5de6\u4e0a\u306e\u72b6\u614b\u306e\u4e0a\u66f8\u304d\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u30ea\u30b9\u30c8\u3092\u9006\u9806\u3067\u8d70\u67fb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3067\u306f\u3001\u5404\u7a2e\u985e\u306e\u30a2\u30a4\u30c6\u30e0\u3092\u9078\u629e\u3067\u304d\u308b\u6570\u306b\u5236\u9650\u304c\u306a\u3044\u305f\u3081\u3001\u30a2\u30a4\u30c6\u30e0\u3092\u542b\u3081\u308b\u72b6\u614b\u9077\u79fb\u306f0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u3068\u7570\u306a\u308a\u307e\u3059\u3002\u72b6\u614b\u304c\u76f4\u63a5\u4e0a\u3068\u5de6\u306e\u72b6\u614b\u306b\u4f9d\u5b58\u3059\u308b\u305f\u3081\u3001\u7a7a\u9593\u6700\u9069\u5316\u3067\u306f\u524d\u65b9\u8d70\u67fb\u3092\u542b\u3081\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u306f\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u5909\u7a2e\u3067\u3001\u300c\u6700\u5927\u300d\u5024\u3092\u6c42\u3081\u308b\u3053\u3068\u304b\u3089\u300c\u6700\u5c0f\u300d\u30b3\u30a4\u30f3\u6570\u3092\u6c42\u3081\u308b\u3053\u3068\u306b\u5909\u308f\u308a\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u306f \\(\\max()\\) \u3092 \\(\\min()\\) \u306b\u5909\u66f4\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u5bb9\u91cf\u3092\u300c\u8d85\u3048\u306a\u3044\u300d\u3053\u3068\u3092\u8ffd\u6c42\u3059\u308b\u3053\u3068\u304b\u3089\u3001\u6b63\u78ba\u306b\u76ee\u6a19\u91d1\u984d\u3092\u6c42\u3081\u308b\u3053\u3068\u306b\u5909\u308f\u308a\u3001\u300c\u76ee\u6a19\u91d1\u984d\u3092\u69cb\u6210\u3067\u304d\u306a\u3044\u300d\u7121\u52b9\u89e3\u3092\u8868\u3059\u305f\u3081\u306b \\(amt + 1\\) \u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984cII\u306f\u300c\u6700\u5c0f\u30b3\u30a4\u30f3\u6570\u300d\u3092\u6c42\u3081\u308b\u3053\u3068\u304b\u3089\u300c\u30b3\u30a4\u30f3\u306e\u7d44\u307f\u5408\u308f\u305b\u6570\u300d\u3092\u6c42\u3081\u308b\u3053\u3068\u306b\u5909\u308f\u308a\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3092 \\(\\min()\\) \u304b\u3089\u548c\u7b97\u6f14\u7b97\u5b50\u306b\u5909\u66f4\u3057\u307e\u3059\u3002

        \u7de8\u96c6\u8ddd\u96e2\u554f\u984c

        • \u7de8\u96c6\u8ddd\u96e2\uff08\u30ec\u30fc\u30d9\u30f3\u30b7\u30e5\u30bf\u30a4\u30f3\u8ddd\u96e2\uff09\u306f2\u3064\u306e\u6587\u5b57\u5217\u9593\u306e\u985e\u4f3c\u5ea6\u3092\u6e2c\u5b9a\u3057\u3001\u4e00\u3064\u306e\u6587\u5b57\u5217\u3092\u5225\u306e\u6587\u5b57\u5217\u306b\u5909\u66f4\u3059\u308b\u305f\u3081\u306b\u5fc5\u8981\u306a\u6700\u5c0f\u7de8\u96c6\u30b9\u30c6\u30c3\u30d7\u6570\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u3001\u7de8\u96c6\u64cd\u4f5c\u306b\u306f\u8ffd\u52a0\u3001\u524a\u9664\u3001\u7f6e\u63db\u304c\u542b\u307e\u308c\u307e\u3059\u3002
        • \u7de8\u96c6\u8ddd\u96e2\u554f\u984c\u306e\u72b6\u614b\u5b9a\u7fa9\u306f\u3001\\(s\\) \u306e\u6700\u521d\u306e \\(i\\) \u6587\u5b57\u3092 \\(t\\) \u306e\u6700\u521d\u306e \\(j\\) \u6587\u5b57\u306b\u5909\u66f4\u3059\u308b\u305f\u3081\u306b\u5fc5\u8981\u306a\u6700\u5c0f\u7de8\u96c6\u30b9\u30c6\u30c3\u30d7\u6570\u3067\u3059\u3002\\(s[i] \\ne t[j]\\) \u306e\u5834\u5408\u3001\u8ffd\u52a0\u3001\u524a\u9664\u3001\u7f6e\u63db\u306e3\u3064\u306e\u6c7a\u5b9a\u304c\u3042\u308a\u3001\u305d\u308c\u305e\u308c\u306b\u5bfe\u5fdc\u3059\u308b\u6b8b\u4f59\u90e8\u5206\u554f\u984c\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u304b\u3089\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u7279\u5b9a\u3057\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3092\u69cb\u7bc9\u3067\u304d\u307e\u3059\u3002\\(s[i] = t[j]\\) \u306e\u5834\u5408\u3001\u73fe\u5728\u306e\u6587\u5b57\u306e\u7de8\u96c6\u306f\u5fc5\u8981\u3042\u308a\u307e\u305b\u3093\u3002
        • \u7de8\u96c6\u8ddd\u96e2\u3067\u306f\u3001\u72b6\u614b\u304c\u76f4\u63a5\u4e0a\u3001\u5de6\u3001\u5de6\u4e0a\u306e\u72b6\u614b\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u7a7a\u9593\u6700\u9069\u5316\u5f8c\u3001\u524d\u65b9\u8d70\u67fb\u3082\u9006\u8d70\u67fb\u3082\u6b63\u3057\u304f\u72b6\u614b\u9077\u79fb\u3092\u5b9f\u884c\u3067\u304d\u307e\u305b\u3093\u3002\u3053\u308c\u306b\u5bfe\u51e6\u3059\u308b\u305f\u3081\u3001\u5909\u6570\u3092\u4f7f\u7528\u3057\u3066\u5de6\u4e0a\u306e\u72b6\u614b\u3092\u4e00\u6642\u7684\u306b\u4fdd\u5b58\u3057\u3001\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u72b6\u6cc1\u3068\u540c\u7b49\u306b\u3057\u3001\u7a7a\u9593\u6700\u9069\u5316\u5f8c\u306b\u524d\u65b9\u8d70\u67fb\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002
        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/","title":"14.5 \u00a0 \u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c","text":"

        \u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u307e\u305a\u5225\u306e\u4e00\u822c\u7684\u306a\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3067\u3042\u308b\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u3092\u89e3\u6c7a\u3057\u3001\u6b21\u306b\u305d\u306e\u7279\u6b8a\u30b1\u30fc\u30b9\u3067\u3042\u308b\u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u3092\u63a2\u7d22\u3057\u307e\u3059\u3002

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1451","title":"14.5.1 \u00a0 \u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c","text":"

        Question

        \\(n\\) \u500b\u306e\u30a2\u30a4\u30c6\u30e0\u304c\u4e0e\u3048\u3089\u308c\u3001\\(i\\) \u756a\u76ee\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u91cd\u91cf\u306f \\(wgt[i-1]\\) \u3067\u5024\u306f \\(val[i-1]\\) \u3067\u3059\u3002\u5bb9\u91cf\u304c \\(cap\\) \u306e\u30d0\u30c3\u30af\u30d1\u30c3\u30af\u304c\u3042\u308a\u307e\u3059\u3002\u5404\u30a2\u30a4\u30c6\u30e0\u306f\u8907\u6570\u56de\u9078\u629e\u3067\u304d\u307e\u3059\u3002\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u3053\u3068\u306a\u304f\u30d0\u30c3\u30af\u30d1\u30c3\u30af\u306b\u5165\u308c\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u30a2\u30a4\u30c6\u30e0\u306e\u6700\u5927\u5024\u306f\u4f55\u3067\u3059\u304b\uff1f\u4ee5\u4e0b\u306e\u4f8b\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u56f3 14-22 \u00a0 \u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u4f8b\u30c7\u30fc\u30bf

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1","title":"1. \u00a0 \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30a2\u30d7\u30ed\u30fc\u30c1","text":"

        \u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306f0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3068\u975e\u5e38\u306b\u4f3c\u3066\u304a\u308a\u3001\u552f\u4e00\u306e\u9055\u3044\u306f\u30a2\u30a4\u30c6\u30e0\u3092\u9078\u629e\u3067\u304d\u308b\u56de\u6570\u306b\u5236\u9650\u304c\u306a\u3044\u3053\u3068\u3067\u3059\u3002

        • 0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3067\u306f\u3001\u5404\u30a2\u30a4\u30c6\u30e0\u306f1\u3064\u3057\u304b\u306a\u3044\u305f\u3081\u3001\u30a2\u30a4\u30c6\u30e0 \\(i\\) \u3092\u30d0\u30c3\u30af\u30d1\u30c3\u30af\u306b\u5165\u308c\u305f\u5f8c\u306f\u3001\u524d\u306e \\(i-1\\) \u500b\u306e\u30a2\u30a4\u30c6\u30e0\u304b\u3089\u306e\u307f\u9078\u629e\u3067\u304d\u307e\u3059\u3002
        • \u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3067\u306f\u3001\u5404\u30a2\u30a4\u30c6\u30e0\u306e\u6570\u91cf\u306f\u7121\u5236\u9650\u3067\u3042\u308b\u305f\u3081\u3001\u30a2\u30a4\u30c6\u30e0 \\(i\\) \u3092\u30d0\u30c3\u30af\u30d1\u30c3\u30af\u306b\u5165\u308c\u305f\u5f8c\u3082\u3001\u524d\u306e \\(i\\) \u500b\u306e\u30a2\u30a4\u30c6\u30e0\u304b\u3089\u5f15\u304d\u7d9a\u304d\u9078\u629e\u3067\u304d\u307e\u3059\u3002

        \u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u30eb\u30fc\u30eb\u306e\u4e0b\u3067\u3001\u72b6\u614b \\([i, c]\\) \u306f2\u3064\u306e\u65b9\u6cd5\u3067\u5909\u5316\u3067\u304d\u307e\u3059\u3002

        • \u30a2\u30a4\u30c6\u30e0 \\(i\\) \u3092\u5165\u308c\u306a\u3044\uff1a0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3068\u540c\u69d8\u306b\u3001\\([i-1, c]\\) \u306b\u9077\u79fb\u3057\u307e\u3059\u3002
        • \u30a2\u30a4\u30c6\u30e0 \\(i\\) \u3092\u5165\u308c\u308b\uff1a0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3068\u306f\u7570\u306a\u308a\u3001\\([i, c-wgt[i-1]]\\) \u306b\u9077\u79fb\u3057\u307e\u3059\u3002

        \u3057\u305f\u304c\u3063\u3066\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u306f\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\uff1a

        \\[ dp[i, c] = \\max(dp[i-1, c], dp[i, c - wgt[i-1]] + val[i-1]) \\]"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2","title":"2. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        2\u3064\u306e\u554f\u984c\u306e\u30b3\u30fc\u30c9\u3092\u6bd4\u8f03\u3059\u308b\u3068\u3001\u72b6\u614b\u9077\u79fb\u304c \\(i-1\\) \u304b\u3089 \\(i\\) \u306b\u5909\u308f\u308a\u3001\u6b8b\u308a\u306f\u5b8c\u5168\u306b\u540c\u4e00\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby unbounded_knapsack.py
        def unbounded_knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"\u5b8c\u5168\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n = len(wgt)\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    dp = [[0] * (cap + 1) for _ in range(n + 1)]\n    # \u72b6\u614b\u9077\u79fb\n    for i in range(1, n + 1):\n        for c in range(1, cap + 1):\n            if wgt[i - 1] > c:\n                # \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][c] = dp[i - 1][c]\n            else:\n                # \u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\u306e\u3068\u9078\u629e\u3059\u308b\u306e\u3068\u3067\u5927\u304d\u3044\u5024\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\n    return dp[n][cap]\n
        unbounded_knapsack.cpp
        /* \u5b8c\u5168\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint unboundedKnapsackDP(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5927\u304d\u3044\u5024\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        unbounded_knapsack.java
        /* \u5b8c\u5168\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint unboundedKnapsackDP(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    int[][] dp = new int[n + 1][cap + 1];\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5927\u304d\u3044\u5024\n                dp[i][c] = Math.max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        unbounded_knapsack.cs
        [class]{unbounded_knapsack}-[func]{UnboundedKnapsackDP}\n
        unbounded_knapsack.go
        [class]{}-[func]{unboundedKnapsackDP}\n
        unbounded_knapsack.swift
        [class]{}-[func]{unboundedKnapsackDP}\n
        unbounded_knapsack.js
        [class]{}-[func]{unboundedKnapsackDP}\n
        unbounded_knapsack.ts
        [class]{}-[func]{unboundedKnapsackDP}\n
        unbounded_knapsack.dart
        [class]{}-[func]{unboundedKnapsackDP}\n
        unbounded_knapsack.rs
        [class]{}-[func]{unbounded_knapsack_dp}\n
        unbounded_knapsack.c
        [class]{}-[func]{unboundedKnapsackDP}\n
        unbounded_knapsack.kt
        [class]{}-[func]{unboundedKnapsackDP}\n
        unbounded_knapsack.rb
        [class]{}-[func]{unbounded_knapsack_dp}\n
        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3","title":"3. \u00a0 \u7a7a\u9593\u6700\u9069\u5316","text":"

        \u73fe\u5728\u306e\u72b6\u614b\u306f\u5de6\u3068\u4e0a\u306e\u72b6\u614b\u304b\u3089\u6765\u308b\u305f\u3081\u3001\u7a7a\u9593\u6700\u9069\u5316\u89e3\u6cd5\u306f \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u306e\u5404\u884c\u306b\u5bfe\u3057\u3066\u524d\u65b9\u8d70\u67fb\u3092\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u3053\u306e\u8d70\u67fb\u9806\u5e8f\u306f0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u5834\u5408\u3068\u306f\u9006\u3067\u3059\u3002\u9055\u3044\u3092\u7406\u89e3\u3059\u308b\u305f\u3081\u306b\u4e0b\u306e\u56f3\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        <1><2><3><4><5><6>

        \u56f3 14-23 \u00a0 \u7a7a\u9593\u6700\u9069\u5316\u5f8c\u306e\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9

        \u30b3\u30fc\u30c9\u5b9f\u88c5\u306f\u975e\u5e38\u306b\u7c21\u5358\u3067\u3001\u914d\u5217 dp \u306e\u6700\u521d\u306e\u6b21\u5143\u3092\u524a\u9664\u3059\u308b\u3060\u3051\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby unbounded_knapsack.py
        def unbounded_knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"\u5b8c\u5168\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n = len(wgt)\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    dp = [0] * (cap + 1)\n    # \u72b6\u614b\u9077\u79fb\n    for i in range(1, n + 1):\n        # \u9806\u5e8f\u3067\u8d70\u67fb\n        for c in range(1, cap + 1):\n            if wgt[i - 1] > c:\n                # \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[c] = dp[c]\n            else:\n                # \u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\u306e\u3068\u9078\u629e\u3059\u308b\u306e\u3068\u3067\u5927\u304d\u3044\u5024\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n    return dp[cap]\n
        unbounded_knapsack.cpp
        /* \u5b8c\u5168\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint unboundedKnapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    vector<int> dp(cap + 1, 0);\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[c] = dp[c];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5927\u304d\u3044\u5024\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        unbounded_knapsack.java
        /* \u5b8c\u5168\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint unboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    int[] dp = new int[cap + 1];\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[c] = dp[c];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u30a2\u30a4\u30c6\u30e0 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5927\u304d\u3044\u5024\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        unbounded_knapsack.cs
        [class]{unbounded_knapsack}-[func]{UnboundedKnapsackDPComp}\n
        unbounded_knapsack.go
        [class]{}-[func]{unboundedKnapsackDPComp}\n
        unbounded_knapsack.swift
        [class]{}-[func]{unboundedKnapsackDPComp}\n
        unbounded_knapsack.js
        [class]{}-[func]{unboundedKnapsackDPComp}\n
        unbounded_knapsack.ts
        [class]{}-[func]{unboundedKnapsackDPComp}\n
        unbounded_knapsack.dart
        [class]{}-[func]{unboundedKnapsackDPComp}\n
        unbounded_knapsack.rs
        [class]{}-[func]{unbounded_knapsack_dp_comp}\n
        unbounded_knapsack.c
        [class]{}-[func]{unboundedKnapsackDPComp}\n
        unbounded_knapsack.kt
        [class]{}-[func]{unboundedKnapsackDPComp}\n
        unbounded_knapsack.rb
        [class]{}-[func]{unbounded_knapsack_dp_comp}\n
        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1452","title":"14.5.2 \u00a0 \u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c","text":"

        \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306f\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u554f\u984c\u306e\u5927\u304d\u306a\u30af\u30e9\u30b9\u306e\u4ee3\u8868\u3067\u3042\u308a\u3001\u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u306a\u3069\u591a\u304f\u306e\u5909\u7a2e\u304c\u3042\u308a\u307e\u3059\u3002

        Question

        \\(n\\) \u7a2e\u985e\u306e\u30b3\u30a4\u30f3\u304c\u4e0e\u3048\u3089\u308c\u3001\\(i\\) \u756a\u76ee\u306e\u7a2e\u985e\u306e\u30b3\u30a4\u30f3\u306e\u984d\u9762\u306f \\(coins[i - 1]\\) \u3067\u3001\u76ee\u6a19\u91d1\u984d\u306f \\(amt\\) \u3067\u3059\u3002\u5404\u7a2e\u985e\u306e\u30b3\u30a4\u30f3\u306f\u8907\u6570\u56de\u9078\u629e\u3067\u304d\u307e\u3059\u3002\u76ee\u6a19\u91d1\u984d\u3092\u69cb\u6210\u3059\u308b\u306e\u306b\u5fc5\u8981\u306a\u6700\u5c0f\u30b3\u30a4\u30f3\u6570\u306f\u4f55\u3067\u3059\u304b\uff1f\u76ee\u6a19\u91d1\u984d\u3092\u69cb\u6210\u3067\u304d\u306a\u3044\u5834\u5408\u306f \\(-1\\) \u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4ee5\u4e0b\u306e\u4f8b\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u56f3 14-24 \u00a0 \u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u306e\u4f8b\u30c7\u30fc\u30bf

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1_1","title":"1. \u00a0 \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30a2\u30d7\u30ed\u30fc\u30c1","text":"

        \u30b3\u30a4\u30f3\u4ea4\u63db\u306f\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u7279\u6b8a\u30b1\u30fc\u30b9\u3068\u898b\u306a\u3059\u3053\u3068\u304c\u3067\u304d\u3001\u4ee5\u4e0b\u306e\u985e\u4f3c\u70b9\u3068\u76f8\u9055\u70b9\u3092\u5171\u6709\u3057\u3066\u3044\u307e\u3059\u3002

        • 2\u3064\u306e\u554f\u984c\u306f\u4e92\u3044\u306b\u5909\u63db\u3067\u304d\u307e\u3059\uff1a\u300c\u30a2\u30a4\u30c6\u30e0\u300d\u306f\u300c\u30b3\u30a4\u30f3\u300d\u306b\u5bfe\u5fdc\u3057\u3001\u300c\u30a2\u30a4\u30c6\u30e0\u306e\u91cd\u91cf\u300d\u306f\u300c\u30b3\u30a4\u30f3\u306e\u984d\u9762\u300d\u306b\u5bfe\u5fdc\u3057\u3001\u300c\u30d0\u30c3\u30af\u30d1\u30c3\u30af\u306e\u5bb9\u91cf\u300d\u306f\u300c\u76ee\u6a19\u91d1\u984d\u300d\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002
        • \u6700\u9069\u5316\u76ee\u6a19\u306f\u9006\u3067\u3059\uff1a\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306f\u30a2\u30a4\u30c6\u30e0\u306e\u5024\u3092\u6700\u5927\u5316\u3059\u308b\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u3001\u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u306f\u30b3\u30a4\u30f3\u306e\u6570\u3092\u6700\u5c0f\u5316\u3059\u308b\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u307e\u3059\u3002
        • \u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306f\u30d0\u30c3\u30af\u30d1\u30c3\u30af\u5bb9\u91cf\u3092\u300c\u8d85\u3048\u306a\u3044\u300d\u89e3\u3092\u6c42\u3081\u3001\u30b3\u30a4\u30f3\u4ea4\u63db\u306f\u76ee\u6a19\u91d1\u984d\u3092\u300c\u6b63\u78ba\u306b\u300d\u69cb\u6210\u3059\u308b\u89e3\u3092\u6c42\u3081\u307e\u3059\u3002

        \u7b2c1\u30b9\u30c6\u30c3\u30d7\uff1a\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u610f\u601d\u6c7a\u5b9a\u3092\u8003\u3048\u3001\u72b6\u614b\u3092\u5b9a\u7fa9\u3057\u3001\u305d\u308c\u306b\u3088\u308a \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u3092\u5c0e\u51fa\u3059\u308b

        \u72b6\u614b \\([i, a]\\) \u306f\u90e8\u5206\u554f\u984c\u306b\u5bfe\u5fdc\u3057\u307e\u3059\uff1a\u6700\u521d\u306e \\(i\\) \u7a2e\u985e\u306e\u30b3\u30a4\u30f3\u3092\u4f7f\u7528\u3057\u3066\u91d1\u984d \\(a\\) \u3092\u69cb\u6210\u3067\u304d\u308b\u6700\u5c0f\u30b3\u30a4\u30f3\u6570\u3001\\(dp[i, a]\\) \u3068\u8868\u8a18\u3055\u308c\u307e\u3059\u3002

        \u4e8c\u6b21\u5143 \\(dp\\) \u30c6\u30fc\u30d6\u30eb\u306e\u30b5\u30a4\u30ba\u306f \\((n+1) \\times (amt+1)\\) \u3067\u3059\u3002

        \u7b2c2\u30b9\u30c6\u30c3\u30d7\uff1a\u6700\u9069\u90e8\u5206\u69cb\u9020\u3092\u7279\u5b9a\u3057\u3001\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u3092\u5c0e\u51fa\u3059\u308b

        \u3053\u306e\u554f\u984c\u306f\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u306e2\u3064\u306e\u5074\u9762\u3067\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3068\u7570\u306a\u308a\u307e\u3059\u3002

        • \u3053\u306e\u554f\u984c\u306f\u6700\u5c0f\u5024\u3092\u6c42\u3081\u308b\u305f\u3081\u3001\u6f14\u7b97\u5b50 \\(\\max()\\) \u3092 \\(\\min()\\) \u306b\u5909\u66f4\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u6700\u9069\u5316\u306f\u30b3\u30a4\u30f3\u306e\u6570\u306b\u7126\u70b9\u3092\u5f53\u3066\u3066\u3044\u308b\u305f\u3081\u3001\u30b3\u30a4\u30f3\u304c\u9078\u629e\u3055\u308c\u305f\u3068\u304d\u306b\u5358\u7d14\u306b \\(+1\\) \u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002
        \\[ dp[i, a] = \\min(dp[i-1, a], dp[i, a - coins[i-1]] + 1) \\]

        \u7b2c3\u30b9\u30c6\u30c3\u30d7\uff1a\u5883\u754c\u6761\u4ef6\u3068\u72b6\u614b\u9077\u79fb\u9806\u5e8f\u3092\u5b9a\u7fa9\u3059\u308b

        \u76ee\u6a19\u91d1\u984d\u304c \\(0\\) \u306e\u5834\u5408\u3001\u305d\u308c\u3092\u69cb\u6210\u3059\u308b\u306e\u306b\u5fc5\u8981\u306a\u6700\u5c0f\u30b3\u30a4\u30f3\u6570\u306f \\(0\\) \u3067\u3042\u308b\u305f\u3081\u3001\u6700\u521d\u306e\u5217\u306e\u3059\u3079\u3066\u306e \\(dp[i, 0]\\) \u306f \\(0\\) \u3067\u3059\u3002

        \u30b3\u30a4\u30f3\u304c\u306a\u3044\u5834\u5408\u3001**\u4efb\u610f\u306e\u91d1\u984d >0 \u3092\u69cb\u6210\u3059\u308b\u3053\u3068\u306f\u4e0d\u53ef\u80fd**\u3067\u3042\u308a\u3001\u3053\u308c\u306f\u7121\u52b9\u306a\u89e3\u3067\u3059\u3002\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u306e \\(\\min()\\) \u95a2\u6570\u304c\u7121\u52b9\u306a\u89e3\u3092\u8a8d\u8b58\u3057\u3066\u30d5\u30a3\u30eb\u30bf\u30ea\u30f3\u30b0\u3067\u304d\u308b\u3088\u3046\u306b\u3001\\(+\\infty\\) \u3092\u4f7f\u7528\u3057\u3066\u305d\u308c\u3089\u3092\u8868\u73fe\u3059\u308b\u3053\u3068\u3092\u691c\u8a0e\u3057\u3001\u3064\u307e\u308a\u6700\u521d\u306e\u884c\u306e\u3059\u3079\u3066\u306e \\(dp[0, a]\\) \u3092 \\(+\\infty\\) \u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2_1","title":"2. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        \u307b\u3068\u3093\u3069\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306f \\(+\\infty\\) \u5909\u6570\u3092\u63d0\u4f9b\u3057\u3066\u304a\u3089\u305a\u3001\u6574\u6570 int \u306e\u6700\u5927\u5024\u306e\u307f\u3092\u4ee3\u66ff\u3068\u3057\u3066\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u304c\u767a\u751f\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\uff1a\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u306e \\(+1\\) \u6f14\u7b97\u304c\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        \u3053\u306e\u7406\u7531\u3067\u3001\u6570\u5024 \\(amt + 1\\) \u3092\u4f7f\u7528\u3057\u3066\u7121\u52b9\u306a\u89e3\u3092\u8868\u3057\u307e\u3059\u3002\u306a\u305c\u306a\u3089\u3001\\(amt\\) \u3092\u69cb\u6210\u3059\u308b\u306e\u306b\u5fc5\u8981\u306a\u6700\u5927\u30b3\u30a4\u30f3\u6570\u306f\u6700\u5927\u3067\u3082 \\(amt\\) \u3060\u304b\u3089\u3067\u3059\u3002\u7d50\u679c\u3092\u8fd4\u3059\u524d\u306b\u3001\\(dp[n, amt]\\) \u304c \\(amt + 1\\) \u306b\u7b49\u3057\u3044\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\u3057\u3001\u305d\u3046\u3067\u3042\u308c\u3070 \\(-1\\) \u3092\u8fd4\u3057\u3001\u76ee\u6a19\u91d1\u984d\u3092\u69cb\u6210\u3067\u304d\u306a\u3044\u3053\u3068\u3092\u793a\u3057\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change.py
        def coin_change_dp(coins: list[int], amt: int) -> int:\n    \"\"\"\u786c\u8ca8\u4ea4\u63db\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n = len(coins)\n    MAX = amt + 1\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    dp = [[0] * (amt + 1) for _ in range(n + 1)]\n    # \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\u3068\u6700\u521d\u306e\u5217\n    for a in range(1, amt + 1):\n        dp[0][a] = MAX\n    # \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\u3068\u5217\n    for i in range(1, n + 1):\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][a] = dp[i - 1][a]\n            else:\n                # \u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\u306e\u3068\u9078\u629e\u3059\u308b\u306e\u3068\u3067\u5c0f\u3055\u3044\u5024\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n    return dp[n][amt] if dp[n][amt] != MAX else -1\n
        coin_change.cpp
        /* \u786c\u8ca8\u4e21\u66ff\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint coinChangeDP(vector<int> &coins, int amt) {\n    int n = coins.size();\n    int MAX = amt + 1;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    vector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\u3068\u6700\u521d\u306e\u5217\n    for (int a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\u3068\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u786c\u8ca8 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5c0f\u3055\u3044\u5024\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
        coin_change.java
        /* \u786c\u8ca8\u4e21\u66ff\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint coinChangeDP(int[] coins, int amt) {\n    int n = coins.length;\n    int MAX = amt + 1;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    int[][] dp = new int[n + 1][amt + 1];\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6700\u521d\u306e\u884c\u3068\u6700\u521d\u306e\u5217\n    for (int a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // \u72b6\u614b\u9077\u79fb\uff1a\u6b8b\u308a\u306e\u884c\u3068\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u786c\u8ca8 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5c0f\u3055\u3044\u5024\n                dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
        coin_change.cs
        [class]{coin_change}-[func]{CoinChangeDP}\n
        coin_change.go
        [class]{}-[func]{coinChangeDP}\n
        coin_change.swift
        [class]{}-[func]{coinChangeDP}\n
        coin_change.js
        [class]{}-[func]{coinChangeDP}\n
        coin_change.ts
        [class]{}-[func]{coinChangeDP}\n
        coin_change.dart
        [class]{}-[func]{coinChangeDP}\n
        coin_change.rs
        [class]{}-[func]{coin_change_dp}\n
        coin_change.c
        [class]{}-[func]{coinChangeDP}\n
        coin_change.kt
        [class]{}-[func]{coinChangeDP}\n
        coin_change.rb
        [class]{}-[func]{coin_change_dp}\n

        \u4e0b\u306e\u56f3\u306f\u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u306e\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9\u3092\u793a\u3057\u3066\u304a\u308a\u3001\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3068\u975e\u5e38\u306b\u4f3c\u3066\u3044\u307e\u3059\u3002

        <1><2><3><4><5><6><7><8><9><10><11><12><13><14><15>

        \u56f3 14-25 \u00a0 \u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u306e\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3_1","title":"3. \u00a0 \u7a7a\u9593\u6700\u9069\u5316","text":"

        \u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u306e\u7a7a\u9593\u6700\u9069\u5316\u306f\u7121\u5236\u9650\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3068\u540c\u3058\u65b9\u6cd5\u3067\u51e6\u7406\u3055\u308c\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change.py
        def coin_change_dp_comp(coins: list[int], amt: int) -> int:\n    \"\"\"\u786c\u8ca8\u4ea4\u63db\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n = len(coins)\n    MAX = amt + 1\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    dp = [MAX] * (amt + 1)\n    dp[0] = 0\n    # \u72b6\u614b\u9077\u79fb\n    for i in range(1, n + 1):\n        # \u9806\u5e8f\u3067\u8d70\u67fb\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[a] = dp[a]\n            else:\n                # \u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\u306e\u3068\u9078\u629e\u3059\u308b\u306e\u3068\u3067\u5c0f\u3055\u3044\u5024\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n    return dp[amt] if dp[amt] != MAX else -1\n
        coin_change.cpp
        /* \u786c\u8ca8\u4e21\u66ff\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint coinChangeDPComp(vector<int> &coins, int amt) {\n    int n = coins.size();\n    int MAX = amt + 1;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    vector<int> dp(amt + 1, MAX);\n    dp[0] = 0;\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[a] = dp[a];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u786c\u8ca8 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5c0f\u3055\u3044\u5024\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1;\n}\n
        coin_change.java
        /* \u786c\u8ca8\u4e21\u66ff\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint coinChangeDPComp(int[] coins, int amt) {\n    int n = coins.length;\n    int MAX = amt + 1;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    int[] dp = new int[amt + 1];\n    Arrays.fill(dp, MAX);\n    dp[0] = 0;\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[a] = dp[a];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u786c\u8ca8 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e\u3088\u308a\u5c0f\u3055\u3044\u5024\n                dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1;\n}\n
        coin_change.cs
        [class]{coin_change}-[func]{CoinChangeDPComp}\n
        coin_change.go
        [class]{}-[func]{coinChangeDPComp}\n
        coin_change.swift
        [class]{}-[func]{coinChangeDPComp}\n
        coin_change.js
        [class]{}-[func]{coinChangeDPComp}\n
        coin_change.ts
        [class]{}-[func]{coinChangeDPComp}\n
        coin_change.dart
        [class]{}-[func]{coinChangeDPComp}\n
        coin_change.rs
        [class]{}-[func]{coin_change_dp_comp}\n
        coin_change.c
        [class]{}-[func]{coinChangeDPComp}\n
        coin_change.kt
        [class]{}-[func]{coinChangeDPComp}\n
        coin_change.rb
        [class]{}-[func]{coin_change_dp_comp}\n
        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1453-ii","title":"14.5.3 \u00a0 \u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984cII","text":"

        Question

        \\(n\\) \u7a2e\u985e\u306e\u30b3\u30a4\u30f3\u304c\u4e0e\u3048\u3089\u308c\u3001\\(i\\) \u756a\u76ee\u306e\u7a2e\u985e\u306e\u30b3\u30a4\u30f3\u306e\u984d\u9762\u306f \\(coins[i - 1]\\) \u3067\u3001\u76ee\u6a19\u91d1\u984d\u306f \\(amt\\) \u3067\u3059\u3002\u5404\u7a2e\u985e\u306e\u30b3\u30a4\u30f3\u306f\u8907\u6570\u56de\u9078\u629e\u3067\u304d\u3001\u76ee\u6a19\u91d1\u984d\u3092\u69cb\u6210\u3067\u304d\u308b\u30b3\u30a4\u30f3\u306e\u7d44\u307f\u5408\u308f\u305b\u306f\u4f55\u901a\u308a\u3042\u308a\u307e\u3059\u304b\u3002\u4ee5\u4e0b\u306e\u4f8b\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u56f3 14-26 \u00a0 \u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984cII\u306e\u4f8b\u30c7\u30fc\u30bf

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1_2","title":"1. \u00a0 \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30a2\u30d7\u30ed\u30fc\u30c1","text":"

        \u524d\u306e\u554f\u984c\u3068\u6bd4\u8f03\u3057\u3066\u3001\u3053\u306e\u554f\u984c\u306e\u76ee\u6a19\u306f\u7d44\u307f\u5408\u308f\u305b\u306e\u6570\u3092\u6c7a\u5b9a\u3059\u308b\u3053\u3068\u3067\u3042\u308b\u305f\u3081\u3001\u90e8\u5206\u554f\u984c\u306f\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\uff1a\u6700\u521d\u306e \\(i\\) \u7a2e\u985e\u306e\u30b3\u30a4\u30f3\u3092\u4f7f\u7528\u3057\u3066\u91d1\u984d \\(a\\) \u3092\u69cb\u6210\u3067\u304d\u308b\u7d44\u307f\u5408\u308f\u305b\u306e\u6570\u3002\\(dp\\) \u30c6\u30fc\u30d6\u30eb\u306f\u30b5\u30a4\u30ba \\((n+1) \\times (amt + 1)\\) \u306e\u4e8c\u6b21\u5143\u884c\u5217\u306e\u307e\u307e\u6b8b\u308a\u307e\u3059\u3002

        \u73fe\u5728\u306e\u72b6\u614b\u306e\u7d44\u307f\u5408\u308f\u305b\u6570\u306f\u3001\u73fe\u5728\u306e\u30b3\u30a4\u30f3\u3092\u9078\u629e\u3057\u306a\u3044\u7d44\u307f\u5408\u308f\u305b\u3068\u73fe\u5728\u306e\u30b3\u30a4\u30f3\u3092\u9078\u629e\u3059\u308b\u7d44\u307f\u5408\u308f\u305b\u306e\u5408\u8a08\u3067\u3059\u3002\u72b6\u614b\u9077\u79fb\u65b9\u7a0b\u5f0f\u306f\uff1a

        \\[ dp[i, a] = dp[i-1, a] + dp[i, a - coins[i-1]] \\]

        \u76ee\u6a19\u91d1\u984d\u304c \\(0\\) \u306e\u5834\u5408\u3001\u76ee\u6a19\u91d1\u984d\u3092\u69cb\u6210\u3059\u308b\u306e\u306b\u30b3\u30a4\u30f3\u306f\u5fc5\u8981\u306a\u3044\u305f\u3081\u3001\u6700\u521d\u306e\u5217\u306e\u3059\u3079\u3066\u306e \\(dp[i, 0]\\) \u306f \\(1\\) \u306b\u521d\u671f\u5316\u3055\u308c\u308b\u3079\u304d\u3067\u3059\u3002\u30b3\u30a4\u30f3\u304c\u306a\u3044\u5834\u5408\u3001\u4efb\u610f\u306e\u91d1\u984d >0 \u3092\u69cb\u6210\u3059\u308b\u3053\u3068\u306f\u4e0d\u53ef\u80fd\u3067\u3042\u308b\u305f\u3081\u3001\u6700\u521d\u306e\u884c\u306e\u3059\u3079\u3066\u306e \\(dp[0, a]\\) \u306f \\(0\\) \u306b\u8a2d\u5b9a\u3055\u308c\u308b\u3079\u304d\u3067\u3059\u3002

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2_2","title":"2. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change_ii.py
        def coin_change_ii_dp(coins: list[int], amt: int) -> int:\n    \"\"\"\u786c\u8ca8\u4ea4\u63db II\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n = len(coins)\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    dp = [[0] * (amt + 1) for _ in range(n + 1)]\n    # \u6700\u521d\u306e\u5217\u3092\u521d\u671f\u5316\n    for i in range(n + 1):\n        dp[i][0] = 1\n    # \u72b6\u614b\u9077\u79fb\n    for i in range(1, n + 1):\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][a] = dp[i - 1][a]\n            else:\n                # \u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\u306e\u3068\u9078\u629e\u3059\u308b\u306e\u3068\u306e\u4e21\u65b9\u306e\u9078\u629e\u80a2\u306e\u548c\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n    return dp[n][amt]\n
        coin_change_ii.cpp
        /* \u786c\u8ca8\u4e21\u66ff II\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint coinChangeIIDP(vector<int> &coins, int amt) {\n    int n = coins.size();\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    vector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n    // \u6700\u521d\u306e\u5217\u3092\u521d\u671f\u5316\n    for (int i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u786c\u8ca8 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e2\u3064\u306e\u9078\u629e\u80a2\u306e\u5408\u8a08\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
        coin_change_ii.java
        /* \u786c\u8ca8\u4e21\u66ff II\uff1a\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint coinChangeIIDP(int[] coins, int amt) {\n    int n = coins.length;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    int[][] dp = new int[n + 1][amt + 1];\n    // \u6700\u521d\u306e\u5217\u3092\u521d\u671f\u5316\n    for (int i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u786c\u8ca8 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e2\u3064\u306e\u9078\u629e\u80a2\u306e\u5408\u8a08\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
        coin_change_ii.cs
        [class]{coin_change_ii}-[func]{CoinChangeIIDP}\n
        coin_change_ii.go
        [class]{}-[func]{coinChangeIIDP}\n
        coin_change_ii.swift
        [class]{}-[func]{coinChangeIIDP}\n
        coin_change_ii.js
        [class]{}-[func]{coinChangeIIDP}\n
        coin_change_ii.ts
        [class]{}-[func]{coinChangeIIDP}\n
        coin_change_ii.dart
        [class]{}-[func]{coinChangeIIDP}\n
        coin_change_ii.rs
        [class]{}-[func]{coin_change_ii_dp}\n
        coin_change_ii.c
        [class]{}-[func]{coinChangeIIDP}\n
        coin_change_ii.kt
        [class]{}-[func]{coinChangeIIDP}\n
        coin_change_ii.rb
        [class]{}-[func]{coin_change_ii_dp}\n
        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3_2","title":"3. \u00a0 \u7a7a\u9593\u6700\u9069\u5316","text":"

        \u7a7a\u9593\u6700\u9069\u5316\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\u540c\u3058\u3067\u3001\u30b3\u30a4\u30f3\u306e\u6b21\u5143\u3092\u524a\u9664\u3059\u308b\u3060\u3051\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change_ii.py
        def coin_change_ii_dp_comp(coins: list[int], amt: int) -> int:\n    \"\"\"\u786c\u8ca8\u4ea4\u63db II\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\"\"\"\n    n = len(coins)\n    # dp \u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    dp = [0] * (amt + 1)\n    dp[0] = 1\n    # \u72b6\u614b\u9077\u79fb\n    for i in range(1, n + 1):\n        # \u9806\u5e8f\u3067\u8d70\u67fb\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[a] = dp[a]\n            else:\n                # \u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\u306e\u3068\u9078\u629e\u3059\u308b\u306e\u3068\u306e\u4e21\u65b9\u306e\u9078\u629e\u80a2\u306e\u548c\n                dp[a] = dp[a] + dp[a - coins[i - 1]]\n    return dp[amt]\n
        coin_change_ii.cpp
        /* \u786c\u8ca8\u4e21\u66ff II\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint coinChangeIIDPComp(vector<int> &coins, int amt) {\n    int n = coins.size();\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    vector<int> dp(amt + 1, 0);\n    dp[0] = 1;\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[a] = dp[a];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u786c\u8ca8 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e2\u3064\u306e\u9078\u629e\u80a2\u306e\u5408\u8a08\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
        coin_change_ii.java
        /* \u786c\u8ca8\u4e21\u66ff II\uff1a\u7a7a\u9593\u6700\u9069\u5316\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 */\nint coinChangeIIDPComp(int[] coins, int amt) {\n    int n = coins.length;\n    // DP\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n    int[] dp = new int[amt + 1];\n    dp[0] = 1;\n    // \u72b6\u614b\u9077\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u76ee\u6a19\u91d1\u984d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u786c\u8ca8 i \u3092\u9078\u629e\u3057\u306a\u3044\n                dp[a] = dp[a];\n            } else {\n                // \u9078\u629e\u3057\u306a\u3044\u5834\u5408\u3068\u786c\u8ca8 i \u3092\u9078\u629e\u3059\u308b\u5834\u5408\u306e2\u3064\u306e\u9078\u629e\u80a2\u306e\u5408\u8a08\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
        coin_change_ii.cs
        [class]{coin_change_ii}-[func]{CoinChangeIIDPComp}\n
        coin_change_ii.go
        [class]{}-[func]{coinChangeIIDPComp}\n
        coin_change_ii.swift
        [class]{}-[func]{coinChangeIIDPComp}\n
        coin_change_ii.js
        [class]{}-[func]{coinChangeIIDPComp}\n
        coin_change_ii.ts
        [class]{}-[func]{coinChangeIIDPComp}\n
        coin_change_ii.dart
        [class]{}-[func]{coinChangeIIDPComp}\n
        coin_change_ii.rs
        [class]{}-[func]{coin_change_ii_dp_comp}\n
        coin_change_ii.c
        [class]{}-[func]{coinChangeIIDPComp}\n
        coin_change_ii.kt
        [class]{}-[func]{coinChangeIIDPComp}\n
        coin_change_ii.rb
        [class]{}-[func]{coin_change_ii_dp_comp}\n
        "},{"location":"chapter_graph/","title":"\u7b2c 9 \u7ae0 \u00a0 \u30b0\u30e9\u30d5","text":"

        Abstract

        \u4eba\u751f\u306e\u65c5\u8def\u306b\u304a\u3044\u3066\u3001\u79c1\u305f\u3061\u306e\u4e00\u4eba\u4e00\u4eba\u306f\u30ce\u30fc\u30c9\u3067\u3042\u308a\u3001\u7121\u6570\u306e\u898b\u3048\u306a\u3044\u8fba\u3067\u7d50\u3070\u308c\u3066\u3044\u307e\u3059\u3002

        \u4e00\u3064\u4e00\u3064\u306e\u51fa\u4f1a\u3044\u3068\u5225\u308c\u304c\u3001\u3053\u306e\u5e83\u5927\u306a\u4eba\u751f\u306e\u30b0\u30e9\u30d5\u306b\u72ec\u7279\u306e\u5370\u3092\u6b8b\u3057\u3066\u3044\u304d\u307e\u3059\u3002

        "},{"location":"chapter_graph/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 9.1 \u00a0 \u30b0\u30e9\u30d5
        • 9.2 \u00a0 \u30b0\u30e9\u30d5\u306e\u57fa\u672c\u64cd\u4f5c
        • 9.3 \u00a0 \u30b0\u30e9\u30d5\u306e\u8d70\u67fb
        • 9.4 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_graph/graph/","title":"9.1 \u00a0 \u30b0\u30e9\u30d5","text":"

        \u30b0\u30e9\u30d5\u306f\u975e\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u4e00\u7a2e\u3067\u3001\u9802\u70b9\u3068\u8fba\u3067\u69cb\u6210\u3055\u308c\u307e\u3059\u3002\u30b0\u30e9\u30d5\\(G\\)\u306f\u3001\u9802\u70b9\u306e\u96c6\u5408\\(V\\)\u3068\u8fba\u306e\u96c6\u5408\\(E\\)\u306e\u7d44\u307f\u5408\u308f\u305b\u3068\u3057\u3066\u62bd\u8c61\u7684\u306b\u8868\u73fe\u3067\u304d\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u4f8b\u306f\u30015\u3064\u306e\u9802\u70b9\u30687\u3064\u306e\u8fba\u3092\u542b\u3080\u30b0\u30e9\u30d5\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \\[ \\begin{aligned} V & = \\{ 1, 2, 3, 4, 5 \\} \\newline E & = \\{ (1,2), (1,3), (1,5), (2,3), (2,4), (2,5), (4,5) \\} \\newline G & = \\{ V, E \\} \\newline \\end{aligned} \\]

        \u9802\u70b9\u3092\u30ce\u30fc\u30c9\u3001\u8fba\u3092\u30ce\u30fc\u30c9\u3092\u63a5\u7d9a\u3059\u308b\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3068\u898b\u306a\u3059\u3068\u3001\u30b0\u30e9\u30d5\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u304b\u3089\u62e1\u5f35\u3055\u308c\u305f\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u3057\u3066\u898b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u7dda\u5f62\u95a2\u4fc2\uff08\u9023\u7d50\u30ea\u30b9\u30c8\uff09\u3084\u5206\u5272\u7d71\u6cbb\u95a2\u4fc2\uff08\u6728\uff09\u3068\u6bd4\u8f03\u3057\u3066\u3001\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u95a2\u4fc2\uff08\u30b0\u30e9\u30d5\uff09\u306f\u81ea\u7531\u5ea6\u304c\u9ad8\u3044\u305f\u3081\u3001\u3088\u308a\u8907\u96d1\u3067\u3059\u3002

        \u56f3 9-1 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u3001\u6728\u3001\u30b0\u30e9\u30d5\u306e\u95a2\u4fc2

        "},{"location":"chapter_graph/graph/#911","title":"9.1.1 \u00a0 \u30b0\u30e9\u30d5\u306e\u4e00\u822c\u7684\u306a\u7a2e\u985e\u3068\u7528\u8a9e","text":"

        \u30b0\u30e9\u30d5\u306f\u3001\u8fba\u306b\u65b9\u5411\u304c\u3042\u308b\u304b\u3069\u3046\u304b\u306b\u3088\u3063\u3066\u7121\u5411\u30b0\u30e9\u30d5\u3068\u6709\u5411\u30b0\u30e9\u30d5\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff08\u4e0b\u56f3\u53c2\u7167\uff09\u3002

        • \u7121\u5411\u30b0\u30e9\u30d5\u3067\u306f\u3001\u8fba\u306f2\u3064\u306e\u9802\u70b9\u9593\u306e\u300c\u53cc\u65b9\u5411\u300d\u63a5\u7d9a\u3092\u8868\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001Facebook\u306e\u300c\u53cb\u9054\u300d\u95a2\u4fc2\u3067\u3059\u3002
        • \u6709\u5411\u30b0\u30e9\u30d5\u3067\u306f\u3001\u8fba\u306b\u65b9\u5411\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u8fba\\(A \\rightarrow B\\)\u3068\\(A \\leftarrow B\\)\u306f\u4e92\u3044\u306b\u72ec\u7acb\u3057\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001Instagram\u3084TikTok\u306e\u300c\u30d5\u30a9\u30ed\u30fc\u300d\u3068\u300c\u30d5\u30a9\u30ed\u30ef\u30fc\u300d\u306e\u95a2\u4fc2\u3067\u3059\u3002

        \u56f3 9-2 \u00a0 \u6709\u5411\u30b0\u30e9\u30d5\u3068\u7121\u5411\u30b0\u30e9\u30d5

        \u3059\u3079\u3066\u306e\u9802\u70b9\u304c\u63a5\u7d9a\u3055\u308c\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u306b\u3088\u3063\u3066\u3001\u30b0\u30e9\u30d5\u306f\u9023\u7d50\u30b0\u30e9\u30d5\u3068\u975e\u9023\u7d50\u30b0\u30e9\u30d5\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff08\u4e0b\u56f3\u53c2\u7167\uff09\u3002

        • \u9023\u7d50\u30b0\u30e9\u30d5\u3067\u306f\u3001\u4efb\u610f\u306e\u9802\u70b9\u304b\u3089\u958b\u59cb\u3057\u3066\u4ed6\u306e\u4efb\u610f\u306e\u9802\u70b9\u306b\u5230\u9054\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002
        • \u975e\u9023\u7d50\u30b0\u30e9\u30d5\u3067\u306f\u3001\u4efb\u610f\u306e\u958b\u59cb\u9802\u70b9\u304b\u3089\u5230\u9054\u3067\u304d\u306a\u3044\u9802\u70b9\u304c\u5c11\u306a\u304f\u3068\u30821\u3064\u5b58\u5728\u3057\u307e\u3059\u3002

        \u56f3 9-3 \u00a0 \u9023\u7d50\u30b0\u30e9\u30d5\u3068\u975e\u9023\u7d50\u30b0\u30e9\u30d5

        \u8fba\u306b\u91cd\u307f\u5909\u6570\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u3001\u305d\u306e\u7d50\u679c\u3068\u3057\u3066\u91cd\u307f\u4ed8\u304d\u30b0\u30e9\u30d5\u304c\u751f\u307e\u308c\u307e\u3059\uff08\u4e0b\u56f3\u53c2\u7167\uff09\u3002\u4f8b\u3048\u3070\u3001Instagram\u3067\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u304c\u3042\u306a\u305f\u3068\u4ed6\u306e\u30e6\u30fc\u30b6\u30fc\u3068\u306e\u9593\u306e\u76f8\u4e92\u4f5c\u7528\u30ec\u30d9\u30eb\uff08\u3044\u3044\u306d\u3001\u95b2\u89a7\u3001\u30b3\u30e1\u30f3\u30c8\u306a\u3069\uff09\u306b\u3088\u3063\u3066\u30d5\u30a9\u30ed\u30ef\u30fc\u3068\u30d5\u30a9\u30ed\u30fc\u4e2d\u306e\u30ea\u30b9\u30c8\u3092\u30bd\u30fc\u30c8\u3057\u307e\u3059\u3002\u3053\u306e\u3088\u3046\u306a\u76f8\u4e92\u4f5c\u7528\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306f\u91cd\u307f\u4ed8\u304d\u30b0\u30e9\u30d5\u3067\u8868\u73fe\u3067\u304d\u307e\u3059\u3002

        \u56f3 9-4 \u00a0 \u91cd\u307f\u4ed8\u304d\u30b0\u30e9\u30d5\u3068\u91cd\u307f\u306a\u3057\u30b0\u30e9\u30d5

        \u30b0\u30e9\u30d5\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u308b\u7528\u8a9e\u304c\u3042\u308a\u307e\u3059\u3002

        • \u96a3\u63a5\uff1a2\u3064\u306e\u9802\u70b9\u3092\u63a5\u7d9a\u3059\u308b\u8fba\u304c\u3042\u308b\u5834\u5408\u3001\u3053\u308c\u30892\u3064\u306e\u9802\u70b9\u306f\u300c\u96a3\u63a5\u300d\u3057\u3066\u3044\u308b\u3068\u8a00\u308f\u308c\u307e\u3059\u3002\u4e0a\u56f3\u3067\u306f\u3001\u9802\u70b91\u306e\u96a3\u63a5\u9802\u70b9\u306f\u9802\u70b92\u30013\u30015\u3067\u3059\u3002
        • \u30d1\u30b9\uff1a\u9802\u70b9A\u304b\u3089\u9802\u70b9B\u307e\u3067\u306b\u901a\u904e\u3059\u308b\u8fba\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u3001A\u304b\u3089B\u3078\u306e\u30d1\u30b9\u3068\u547c\u3073\u307e\u3059\u3002\u4e0a\u56f3\u3067\u306f\u3001\u8fba\u306e\u30b7\u30fc\u30b1\u30f3\u30b91-5-2-4\u306f\u9802\u70b91\u304b\u3089\u9802\u70b94\u3078\u306e\u30d1\u30b9\u3067\u3059\u3002
        • \u6b21\u6570\uff1a\u9802\u70b9\u304c\u6301\u3064\u8fba\u306e\u6570\u3067\u3059\u3002\u6709\u5411\u30b0\u30e9\u30d5\u306e\u5834\u5408\u3001\u5165\u6b21\u6570\u306f\u305d\u306e\u9802\u70b9\u3092\u6307\u3059\u8fba\u306e\u6570\u3001\u51fa\u6b21\u6570\u306f\u305d\u306e\u9802\u70b9\u304b\u3089\u51fa\u308b\u8fba\u306e\u6570\u3092\u6307\u3057\u307e\u3059\u3002
        "},{"location":"chapter_graph/graph/#912","title":"9.1.2 \u00a0 \u30b0\u30e9\u30d5\u306e\u8868\u73fe","text":"

        \u30b0\u30e9\u30d5\u306e\u4e00\u822c\u7684\u306a\u8868\u73fe\u306b\u306f\u300c\u96a3\u63a5\u884c\u5217\u300d\u3068\u300c\u96a3\u63a5\u30ea\u30b9\u30c8\u300d\u304c\u3042\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u4f8b\u3067\u306f\u7121\u5411\u30b0\u30e9\u30d5\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        "},{"location":"chapter_graph/graph/#1","title":"1. \u00a0 \u96a3\u63a5\u884c\u5217","text":"

        \u30b0\u30e9\u30d5\u306e\u9802\u70b9\u6570\u3092\\(n\\)\u3068\u3059\u308b\u3068\u3001\u96a3\u63a5\u884c\u5217\u306f\\(n \\times n\\)\u306e\u884c\u5217\u3092\u4f7f\u7528\u3057\u3066\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3057\u307e\u3059\u3002\u5404\u884c\uff08\u5217\uff09\u306f\u9802\u70b9\u3092\u8868\u3057\u3001\u884c\u5217\u8981\u7d20\u306f\u8fba\u3092\u8868\u3057\u30012\u3064\u306e\u9802\u70b9\u9593\u306b\u8fba\u304c\u3042\u308b\u304b\u3069\u3046\u304b\u3092\\(1\\)\u307e\u305f\u306f\\(0\\)\u3067\u793a\u3057\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u96a3\u63a5\u884c\u5217\u3092\\(M\\)\u3001\u9802\u70b9\u306e\u30ea\u30b9\u30c8\u3092\\(V\\)\u3068\u3059\u308b\u3068\u3001\u884c\u5217\u8981\u7d20\\(M[i, j] = 1\\)\u306f\u9802\u70b9\\(V[i]\\)\u3068\u9802\u70b9\\(V[j]\\)\u306e\u9593\u306b\u8fba\u304c\u3042\u308b\u3053\u3068\u3092\u793a\u3057\u3001\u9006\u306b\\(M[i, j] = 0\\)\u306f2\u3064\u306e\u9802\u70b9\u9593\u306b\u8fba\u304c\u306a\u3044\u3053\u3068\u3092\u793a\u3057\u307e\u3059\u3002

        \u56f3 9-5 \u00a0 \u96a3\u63a5\u884c\u5217\u306b\u3088\u308b\u30b0\u30e9\u30d5\u306e\u8868\u73fe

        \u96a3\u63a5\u884c\u5217\u306b\u306f\u4ee5\u4e0b\u306e\u7279\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        • \u9802\u70b9\u306f\u81ea\u5206\u81ea\u8eab\u306b\u63a5\u7d9a\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u306a\u3044\u305f\u3081\u3001\u96a3\u63a5\u884c\u5217\u306e\u4e3b\u5bfe\u89d2\u7dda\u4e0a\u306e\u8981\u7d20\u306f\u610f\u5473\u304c\u3042\u308a\u307e\u305b\u3093\u3002
        • \u7121\u5411\u30b0\u30e9\u30d5\u306e\u5834\u5408\u3001\u4e21\u65b9\u5411\u306e\u8fba\u306f\u7b49\u4fa1\u3067\u3042\u308b\u305f\u3081\u3001\u96a3\u63a5\u884c\u5217\u306f\u4e3b\u5bfe\u89d2\u7dda\u306b\u95a2\u3057\u3066\u5bfe\u79f0\u3067\u3059\u3002
        • \u96a3\u63a5\u884c\u5217\u306e\u8981\u7d20\u3092\\(1\\)\u3068\\(0\\)\u304b\u3089\u91cd\u307f\u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u3067\u3001\u91cd\u307f\u4ed8\u304d\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3067\u304d\u307e\u3059\u3002

        \u96a3\u63a5\u884c\u5217\u3067\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3059\u308b\u5834\u5408\u3001\u884c\u5217\u8981\u7d20\u306b\u76f4\u63a5\u30a2\u30af\u30bb\u30b9\u3057\u3066\u8fba\u3092\u53d6\u5f97\u3067\u304d\u308b\u305f\u3081\u3001\u8ffd\u52a0\u3001\u524a\u9664\u3001\u691c\u7d22\u3001\u5909\u66f4\u306e\u64cd\u4f5c\u304c\u52b9\u7387\u7684\u3067\u3001\u3059\u3079\u3066\u6642\u9593\u8a08\u7b97\u91cf\\(O(1)\\)\u3067\u3059\u3002\u305f\u3060\u3057\u3001\u884c\u5217\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(n^2)\\)\u3067\u3001\u3088\u308a\u591a\u304f\u306e\u30e1\u30e2\u30ea\u3092\u6d88\u8cbb\u3057\u307e\u3059\u3002

        "},{"location":"chapter_graph/graph/#2","title":"2. \u00a0 \u96a3\u63a5\u30ea\u30b9\u30c8","text":"

        \u96a3\u63a5\u30ea\u30b9\u30c8\u306f\\(n\\)\u500b\u306e\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3057\u3001\u5404\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u306f\u9802\u70b9\u3092\u8868\u3057\u307e\u3059\u3002\\(i\\)\u756a\u76ee\u306e\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u9802\u70b9\\(i\\)\u306b\u5bfe\u5fdc\u3057\u3001\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\uff08\u305d\u306e\u9802\u70b9\u306b\u63a5\u7d9a\u3055\u308c\u305f\u9802\u70b9\uff09\u3092\u542b\u307f\u307e\u3059\u3002\u4e0b\u56f3\u306f\u96a3\u63a5\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u683c\u7d0d\u3055\u308c\u305f\u30b0\u30e9\u30d5\u306e\u4f8b\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 9-6 \u00a0 \u96a3\u63a5\u30ea\u30b9\u30c8\u306b\u3088\u308b\u30b0\u30e9\u30d5\u306e\u8868\u73fe

        \u96a3\u63a5\u30ea\u30b9\u30c8\u306f\u5b9f\u969b\u306e\u8fba\u306e\u307f\u3092\u683c\u7d0d\u3057\u3001\u8fba\u306e\u7dcf\u6570\u306f\\(n^2\\)\u3088\u308a\u3082\u306f\u308b\u304b\u306b\u5c11\u306a\u3044\u3053\u3068\u304c\u591a\u304f\u3001\u3088\u308a\u7a7a\u9593\u52b9\u7387\u7684\u3067\u3059\u3002\u305f\u3060\u3057\u3001\u96a3\u63a5\u30ea\u30b9\u30c8\u3067\u8fba\u3092\u898b\u3064\u3051\u308b\u306b\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8d70\u67fb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u305d\u306e\u6642\u9593\u52b9\u7387\u306f\u96a3\u63a5\u884c\u5217\u307b\u3069\u826f\u304f\u3042\u308a\u307e\u305b\u3093\u3002

        \u4e0a\u56f3\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\u96a3\u63a5\u30ea\u30b9\u30c8\u306e\u69cb\u9020\u306f\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306e\u300c\u30c1\u30a7\u30a4\u30f3\u6cd5\u300d\u3068\u975e\u5e38\u306b\u4f3c\u3066\u3044\u308b\u305f\u3081\u3001\u540c\u69d8\u306e\u65b9\u6cd5\u3092\u4f7f\u7528\u3057\u3066\u52b9\u7387\u3092\u6700\u9069\u5316\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u304c\u9577\u3044\u5834\u5408\u3001\u305d\u308c\u3092AVL\u6728\u3084\u8d64\u9ed2\u6728\u306b\u5909\u63db\u3057\u3066\u3001\u6642\u9593\u52b9\u7387\u3092\\(O(n)\\)\u304b\u3089\\(O(\\log n)\\)\u306b\u6700\u9069\u5316\u3067\u304d\u307e\u3059\u3002\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306b\u5909\u63db\u3059\u308b\u3053\u3068\u3067\u3001\u6642\u9593\u8a08\u7b97\u91cf\u3092\\(O(1)\\)\u306b\u524a\u6e1b\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_graph/graph/#913","title":"9.1.3 \u00a0 \u30b0\u30e9\u30d5\u306e\u4e00\u822c\u7684\u306a\u5fdc\u7528","text":"

        \u4e0b\u8868\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u591a\u304f\u306e\u73fe\u5b9f\u4e16\u754c\u306e\u30b7\u30b9\u30c6\u30e0\u306f\u30b0\u30e9\u30d5\u3067\u30e2\u30c7\u30eb\u5316\u3067\u304d\u3001\u5bfe\u5fdc\u3059\u308b\u554f\u984c\u306f\u30b0\u30e9\u30d5\u8a08\u7b97\u554f\u984c\u306b\u524a\u6e1b\u3067\u304d\u307e\u3059\u3002

        \u8868 9-1 \u00a0 \u73fe\u5b9f\u751f\u6d3b\u306e\u4e00\u822c\u7684\u306a\u30b0\u30e9\u30d5

        \u9802\u70b9 \u8fba \u30b0\u30e9\u30d5\u8a08\u7b97\u554f\u984c \u30bd\u30fc\u30b7\u30e3\u30eb\u30cd\u30c3\u30c8\u30ef\u30fc\u30af \u30e6\u30fc\u30b6\u30fc \u30d5\u30a9\u30ed\u30fc / \u30d5\u30a9\u30ed\u30ef\u30fc\u95a2\u4fc2 \u6f5c\u5728\u7684\u30d5\u30a9\u30ed\u30fc\u63a8\u85a6 \u5730\u4e0b\u9244\u8def\u7dda \u99c5 \u99c5\u9593\u306e\u63a5\u7d9a\u6027 \u6700\u77ed\u30eb\u30fc\u30c8\u63a8\u85a6 \u592a\u967d\u7cfb \u5929\u4f53 \u5929\u4f53\u9593\u306e\u91cd\u529b \u60d1\u661f\u8ecc\u9053\u8a08\u7b97"},{"location":"chapter_graph/graph_operations/","title":"9.2 \u00a0 \u30b0\u30e9\u30d5\u306e\u57fa\u672c\u64cd\u4f5c","text":"

        \u30b0\u30e9\u30d5\u306e\u57fa\u672c\u64cd\u4f5c\u306f\u300c\u8fba\u300d\u306b\u5bfe\u3059\u308b\u64cd\u4f5c\u3068\u300c\u9802\u70b9\u300d\u306b\u5bfe\u3059\u308b\u64cd\u4f5c\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u300c\u96a3\u63a5\u884c\u5217\u300d\u3068\u300c\u96a3\u63a5\u30ea\u30b9\u30c8\u300d\u306e2\u3064\u306e\u8868\u73fe\u65b9\u6cd5\u306e\u4e0b\u3067\u306f\u3001\u5b9f\u88c5\u304c\u7570\u306a\u308a\u307e\u3059\u3002

        "},{"location":"chapter_graph/graph_operations/#921","title":"9.2.1 \u00a0 \u96a3\u63a5\u884c\u5217\u306b\u57fa\u3065\u304f\u5b9f\u88c5","text":"

        \\(n\\)\u500b\u306e\u9802\u70b9\u3092\u6301\u3064\u7121\u5411\u30b0\u30e9\u30d5\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u3055\u307e\u3056\u307e\u306a\u64cd\u4f5c\u306f\u4e0b\u56f3\u306e\u3088\u3046\u306b\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002

        • \u8fba\u306e\u8ffd\u52a0\u307e\u305f\u306f\u524a\u9664\uff1a\u96a3\u63a5\u884c\u5217\u5185\u306e\u6307\u5b9a\u3055\u308c\u305f\u8fba\u3092\u76f4\u63a5\u5909\u66f4\u3057\u3001\\(O(1)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u7121\u5411\u30b0\u30e9\u30d5\u3067\u3042\u308b\u305f\u3081\u3001\u4e21\u65b9\u5411\u306e\u8fba\u3092\u540c\u6642\u306b\u66f4\u65b0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u9802\u70b9\u306e\u8ffd\u52a0\uff1a\u96a3\u63a5\u884c\u5217\u306e\u672b\u5c3e\u306b\u884c\u3068\u5217\u3092\u8ffd\u52a0\u3057\u3001\u3059\u3079\u3066\\(0\\)\u3067\u57cb\u3081\u307e\u3059\u3002\\(O(n)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u9802\u70b9\u306e\u524a\u9664\uff1a\u96a3\u63a5\u884c\u5217\u5185\u306e\u884c\u3068\u5217\u3092\u524a\u9664\u3057\u307e\u3059\u3002\u6700\u60aa\u306e\u5834\u5408\u306f\u6700\u521d\u306e\u884c\u3068\u5217\u304c\u524a\u9664\u3055\u308c\u308b\u3068\u304d\u3067\u3001\\((n-1)^2\\)\u500b\u306e\u8981\u7d20\u3092\u300c\u4e0a\u3068\u5de6\u306b\u79fb\u52d5\u300d\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\\(O(n^2)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u521d\u671f\u5316\uff1a\\(n\\)\u500b\u306e\u9802\u70b9\u3092\u6e21\u3057\u3001\u9577\u3055\\(n\\)\u306e\u9802\u70b9\u30ea\u30b9\u30c8vertices\u3092\u521d\u671f\u5316\u3057\u3001\\(O(n)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\\(n \\times n\\)\u30b5\u30a4\u30ba\u306e\u96a3\u63a5\u884c\u5217adjMat\u3092\u521d\u671f\u5316\u3057\u3001\\(O(n^2)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        \u96a3\u63a5\u884c\u5217\u306e\u521d\u671f\u5316\u8fba\u306e\u8ffd\u52a0\u8fba\u306e\u524a\u9664\u9802\u70b9\u306e\u8ffd\u52a0\u9802\u70b9\u306e\u524a\u9664

        \u56f3 9-7 \u00a0 \u96a3\u63a5\u884c\u5217\u3067\u306e\u521d\u671f\u5316\u3001\u8fba\u306e\u8ffd\u52a0\u3068\u524a\u9664\u3001\u9802\u70b9\u306e\u8ffd\u52a0\u3068\u524a\u9664

        \u4ee5\u4e0b\u306f\u96a3\u63a5\u884c\u5217\u3092\u4f7f\u7528\u3057\u3066\u8868\u73fe\u3055\u308c\u305f\u30b0\u30e9\u30d5\u306e\u5b9f\u88c5\u30b3\u30fc\u30c9\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_adjacency_matrix.py
        class GraphAdjMat:\n    \"\"\"\u96a3\u63a5\u884c\u5217\u306b\u57fa\u3065\u304f\u7121\u5411\u30b0\u30e9\u30d5\u30af\u30e9\u30b9\"\"\"\n\n    def __init__(self, vertices: list[int], edges: list[list[int]]):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        # \u9802\u70b9\u30ea\u30b9\u30c8\u3001\u8981\u7d20\u306f\u300c\u9802\u70b9\u5024\u300d\u3092\u8868\u3057\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u300c\u9802\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u3092\u8868\u3059\n        self.vertices: list[int] = []\n        # \u96a3\u63a5\u884c\u5217\u3001\u884c\u3068\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u300c\u9802\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u306b\u5bfe\u5fdc\n        self.adj_mat: list[list[int]] = []\n        # \u9802\u70b9\u3092\u8ffd\u52a0\n        for val in vertices:\n            self.add_vertex(val)\n        # \u8fba\u3092\u8ffd\u52a0\n        # edges\u8981\u7d20\u306f\u9802\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8868\u3059\n        for e in edges:\n            self.add_edge(e[0], e[1])\n\n    def size(self) -> int:\n        \"\"\"\u9802\u70b9\u6570\u3092\u53d6\u5f97\"\"\"\n        return len(self.vertices)\n\n    def add_vertex(self, val: int):\n        \"\"\"\u9802\u70b9\u3092\u8ffd\u52a0\"\"\"\n        n = self.size()\n        # \u9802\u70b9\u30ea\u30b9\u30c8\u306b\u65b0\u3057\u3044\u9802\u70b9\u5024\u3092\u8ffd\u52a0\n        self.vertices.append(val)\n        # \u96a3\u63a5\u884c\u5217\u306b\u884c\u3092\u8ffd\u52a0\n        new_row = [0] * n\n        self.adj_mat.append(new_row)\n        # \u96a3\u63a5\u884c\u5217\u306b\u5217\u3092\u8ffd\u52a0\n        for row in self.adj_mat:\n            row.append(0)\n\n    def remove_vertex(self, index: int):\n        \"\"\"\u9802\u70b9\u3092\u524a\u9664\"\"\"\n        if index >= self.size():\n            raise IndexError()\n        # \u9802\u70b9\u30ea\u30b9\u30c8\u304b\u3089`index`\u306e\u9802\u70b9\u3092\u524a\u9664\n        self.vertices.pop(index)\n        # \u96a3\u63a5\u884c\u5217\u304b\u3089`index`\u306e\u884c\u3092\u524a\u9664\n        self.adj_mat.pop(index)\n        # \u96a3\u63a5\u884c\u5217\u304b\u3089`index`\u306e\u5217\u3092\u524a\u9664\n        for row in self.adj_mat:\n            row.pop(index)\n\n    def add_edge(self, i: int, j: int):\n        \"\"\"\u8fba\u3092\u8ffd\u52a0\"\"\"\n        # \u30d1\u30e9\u30e1\u30fc\u30bfi\u3001j\u306f\u9802\u70b9\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5bfe\u5fdc\n        # \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u7bc4\u56f2\u5916\u3068\u7b49\u4fa1\u6027\u3092\u51e6\u7406\n        if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\n            raise IndexError()\n        # \u7121\u5411\u30b0\u30e9\u30d5\u3067\u306f\u3001\u96a3\u63a5\u884c\u5217\u306f\u4e3b\u5bfe\u89d2\u7dda\u306b\u3064\u3044\u3066\u5bfe\u79f0\u3001\u3059\u306a\u308f\u3061 (i, j) == (j, i) \u3092\u6e80\u305f\u3059\n        self.adj_mat[i][j] = 1\n        self.adj_mat[j][i] = 1\n\n    def remove_edge(self, i: int, j: int):\n        \"\"\"\u8fba\u3092\u524a\u9664\"\"\"\n        # \u30d1\u30e9\u30e1\u30fc\u30bfi\u3001j\u306f\u9802\u70b9\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5bfe\u5fdc\n        # \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u7bc4\u56f2\u5916\u3068\u7b49\u4fa1\u6027\u3092\u51e6\u7406\n        if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\n            raise IndexError()\n        self.adj_mat[i][j] = 0\n        self.adj_mat[j][i] = 0\n\n    def print(self):\n        \"\"\"\u96a3\u63a5\u884c\u5217\u3092\u51fa\u529b\"\"\"\n        print(\"\u9802\u70b9\u30ea\u30b9\u30c8 =\", self.vertices)\n        print(\"\u96a3\u63a5\u884c\u5217 =\")\n        print_matrix(self.adj_mat)\n
        graph_adjacency_matrix.cpp
        /* \u96a3\u63a5\u884c\u5217\u306b\u57fa\u3065\u304f\u7121\u5411\u30b0\u30e9\u30d5\u30af\u30e9\u30b9 */\nclass GraphAdjMat {\n    vector<int> vertices;       // \u9802\u70b9\u30ea\u30b9\u30c8\u3001\u8981\u7d20\u306f\u300c\u9802\u70b9\u5024\u300d\u3092\u8868\u3057\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u300c\u9802\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u3092\u8868\u3059\n    vector<vector<int>> adjMat; // \u96a3\u63a5\u884c\u5217\u3001\u884c\u3068\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u300c\u9802\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u306b\u5bfe\u5fdc\n\n  public:\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    GraphAdjMat(const vector<int> &vertices, const vector<vector<int>> &edges) {\n        // \u9802\u70b9\u3092\u8ffd\u52a0\n        for (int val : vertices) {\n            addVertex(val);\n        }\n        // \u8fba\u3092\u8ffd\u52a0\n        // \u8fba\u306e\u8981\u7d20\u306f\u9802\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8868\u3059\n        for (const vector<int> &edge : edges) {\n            addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u9802\u70b9\u6570\u3092\u53d6\u5f97 */\n    int size() const {\n        return vertices.size();\n    }\n\n    /* \u9802\u70b9\u3092\u8ffd\u52a0 */\n    void addVertex(int val) {\n        int n = size();\n        // \u9802\u70b9\u30ea\u30b9\u30c8\u306b\u65b0\u3057\u3044\u9802\u70b9\u5024\u3092\u8ffd\u52a0\n        vertices.push_back(val);\n        // \u96a3\u63a5\u884c\u5217\u306b\u884c\u3092\u8ffd\u52a0\n        adjMat.emplace_back(vector<int>(n, 0));\n        // \u96a3\u63a5\u884c\u5217\u306b\u5217\u3092\u8ffd\u52a0\n        for (vector<int> &row : adjMat) {\n            row.push_back(0);\n        }\n    }\n\n    /* \u9802\u70b9\u3092\u524a\u9664 */\n    void removeVertex(int index) {\n        if (index >= size()) {\n            throw out_of_range(\"Vertex does not exist\");\n        }\n        // \u9802\u70b9\u30ea\u30b9\u30c8\u304b\u3089`index`\u306e\u9802\u70b9\u3092\u524a\u9664\n        vertices.erase(vertices.begin() + index);\n        // \u96a3\u63a5\u884c\u5217\u304b\u3089`index`\u306e\u884c\u3092\u524a\u9664\n        adjMat.erase(adjMat.begin() + index);\n        // \u96a3\u63a5\u884c\u5217\u304b\u3089`index`\u306e\u5217\u3092\u524a\u9664\n        for (vector<int> &row : adjMat) {\n            row.erase(row.begin() + index);\n        }\n    }\n\n    /* \u8fba\u3092\u8ffd\u52a0 */\n    // \u30d1\u30e9\u30e1\u30fc\u30bfi\u3001j\u306f\u9802\u70b9\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5bfe\u5fdc\n    void addEdge(int i, int j) {\n        // \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2\u5916\u3068\u7b49\u4fa1\u6027\u3092\u51e6\u7406\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n            throw out_of_range(\"Vertex does not exist\");\n        }\n        // \u7121\u5411\u30b0\u30e9\u30d5\u3067\u306f\u3001\u96a3\u63a5\u884c\u5217\u306f\u4e3b\u5bfe\u89d2\u7dda\u306b\u3064\u3044\u3066\u5bfe\u79f0\u3001\u5373\u3061(i, j) == (j, i)\u3092\u6e80\u305f\u3059\n        adjMat[i][j] = 1;\n        adjMat[j][i] = 1;\n    }\n\n    /* \u8fba\u3092\u524a\u9664 */\n    // \u30d1\u30e9\u30e1\u30fc\u30bfi\u3001j\u306f\u9802\u70b9\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5bfe\u5fdc\n    void removeEdge(int i, int j) {\n        // \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2\u5916\u3068\u7b49\u4fa1\u6027\u3092\u51e6\u7406\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n            throw out_of_range(\"Vertex does not exist\");\n        }\n        adjMat[i][j] = 0;\n        adjMat[j][i] = 0;\n    }\n\n    /* \u96a3\u63a5\u884c\u5217\u3092\u5370\u5237 */\n    void print() {\n        cout << \"\u9802\u70b9\u30ea\u30b9\u30c8 = \";\n        printVector(vertices);\n        cout << \"\u96a3\u63a5\u884c\u5217 =\" << endl;\n        printVectorMatrix(adjMat);\n    }\n};\n
        graph_adjacency_matrix.java
        /* \u96a3\u63a5\u884c\u5217\u306b\u57fa\u3065\u304f\u7121\u5411\u30b0\u30e9\u30d5\u30af\u30e9\u30b9 */\nclass GraphAdjMat {\n    List<Integer> vertices; // \u9802\u70b9\u30ea\u30b9\u30c8\u3001\u8981\u7d20\u306f\u300c\u9802\u70b9\u5024\u300d\u3092\u8868\u3057\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u300c\u9802\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u3092\u8868\u3059\n    List<List<Integer>> adjMat; // \u96a3\u63a5\u884c\u5217\u3001\u884c\u3068\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u300c\u9802\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u306b\u5bfe\u5fdc\n\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    public GraphAdjMat(int[] vertices, int[][] edges) {\n        this.vertices = new ArrayList<>();\n        this.adjMat = new ArrayList<>();\n        // \u9802\u70b9\u3092\u8ffd\u52a0\n        for (int val : vertices) {\n            addVertex(val);\n        }\n        // \u8fba\u3092\u8ffd\u52a0\n        // \u8fba\u306e\u8981\u7d20\u306f\u9802\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8868\u3059\n        for (int[] e : edges) {\n            addEdge(e[0], e[1]);\n        }\n    }\n\n    /* \u9802\u70b9\u6570\u3092\u53d6\u5f97 */\n    public int size() {\n        return vertices.size();\n    }\n\n    /* \u9802\u70b9\u3092\u8ffd\u52a0 */\n    public void addVertex(int val) {\n        int n = size();\n        // \u9802\u70b9\u30ea\u30b9\u30c8\u306b\u65b0\u3057\u3044\u9802\u70b9\u5024\u3092\u8ffd\u52a0\n        vertices.add(val);\n        // \u96a3\u63a5\u884c\u5217\u306b\u884c\u3092\u8ffd\u52a0\n        List<Integer> newRow = new ArrayList<>(n);\n        for (int j = 0; j < n; j++) {\n            newRow.add(0);\n        }\n        adjMat.add(newRow);\n        // \u96a3\u63a5\u884c\u5217\u306b\u5217\u3092\u8ffd\u52a0\n        for (List<Integer> row : adjMat) {\n            row.add(0);\n        }\n    }\n\n    /* \u9802\u70b9\u3092\u524a\u9664 */\n    public void removeVertex(int index) {\n        if (index >= size())\n            throw new IndexOutOfBoundsException();\n        // \u9802\u70b9\u30ea\u30b9\u30c8\u304b\u3089 `index` \u306e\u9802\u70b9\u3092\u524a\u9664\n        vertices.remove(index);\n        // \u96a3\u63a5\u884c\u5217\u304b\u3089 `index` \u306e\u884c\u3092\u524a\u9664\n        adjMat.remove(index);\n        // \u96a3\u63a5\u884c\u5217\u304b\u3089 `index` \u306e\u5217\u3092\u524a\u9664\n        for (List<Integer> row : adjMat) {\n            row.remove(index);\n        }\n    }\n\n    /* \u8fba\u3092\u8ffd\u52a0 */\n    // \u30d1\u30e9\u30e1\u30fc\u30bf i\u3001j \u306f\u9802\u70b9\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5bfe\u5fdc\n    public void addEdge(int i, int j) {\n        // \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u7bc4\u56f2\u5916\u3068\u7b49\u4fa1\u6027\u3092\u51e6\u7406\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw new IndexOutOfBoundsException();\n        // \u7121\u5411\u30b0\u30e9\u30d5\u3067\u306f\u3001\u96a3\u63a5\u884c\u5217\u306f\u4e3b\u5bfe\u89d2\u7dda\u306b\u3064\u3044\u3066\u5bfe\u79f0\u3001\u3059\u306a\u308f\u3061 (i, j) == (j, i) \u3092\u6e80\u305f\u3059\n        adjMat.get(i).set(j, 1);\n        adjMat.get(j).set(i, 1);\n    }\n\n    /* \u8fba\u3092\u524a\u9664 */\n    // \u30d1\u30e9\u30e1\u30fc\u30bf i\u3001j \u306f\u9802\u70b9\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5bfe\u5fdc\n    public void removeEdge(int i, int j) {\n        // \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u7bc4\u56f2\u5916\u3068\u7b49\u4fa1\u6027\u3092\u51e6\u7406\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw new IndexOutOfBoundsException();\n        adjMat.get(i).set(j, 0);\n        adjMat.get(j).set(i, 0);\n    }\n\n    /* \u96a3\u63a5\u884c\u5217\u3092\u51fa\u529b */\n    public void print() {\n        System.out.print(\"\u9802\u70b9\u30ea\u30b9\u30c8 = \");\n        System.out.println(vertices);\n        System.out.println(\"\u96a3\u63a5\u884c\u5217 =\");\n        PrintUtil.printMatrix(adjMat);\n    }\n}\n
        graph_adjacency_matrix.cs
        [class]{GraphAdjMat}-[func]{}\n
        graph_adjacency_matrix.go
        [class]{graphAdjMat}-[func]{}\n
        graph_adjacency_matrix.swift
        [class]{GraphAdjMat}-[func]{}\n
        graph_adjacency_matrix.js
        [class]{GraphAdjMat}-[func]{}\n
        graph_adjacency_matrix.ts
        [class]{GraphAdjMat}-[func]{}\n
        graph_adjacency_matrix.dart
        [class]{GraphAdjMat}-[func]{}\n
        graph_adjacency_matrix.rs
        [class]{GraphAdjMat}-[func]{}\n
        graph_adjacency_matrix.c
        [class]{GraphAdjMat}-[func]{}\n
        graph_adjacency_matrix.kt
        [class]{GraphAdjMat}-[func]{}\n
        graph_adjacency_matrix.rb
        [class]{GraphAdjMat}-[func]{}\n
        "},{"location":"chapter_graph/graph_operations/#922","title":"9.2.2 \u00a0 \u96a3\u63a5\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u5b9f\u88c5","text":"

        \u7dcf\u8a08\\(n\\)\u500b\u306e\u9802\u70b9\u3068\\(m\\)\u500b\u306e\u8fba\u3092\u6301\u3064\u7121\u5411\u30b0\u30e9\u30d5\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u3055\u307e\u3056\u307e\u306a\u64cd\u4f5c\u306f\u4e0b\u56f3\u306e\u3088\u3046\u306b\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002

        • \u8fba\u306e\u8ffd\u52a0\uff1a\u5bfe\u5fdc\u3059\u308b\u9802\u70b9\u306e\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u672b\u5c3e\u306b\u8fba\u3092\u8ffd\u52a0\u3059\u308b\u3060\u3051\u3067\u3001\\(O(1)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u7121\u5411\u30b0\u30e9\u30d5\u3067\u3042\u308b\u305f\u3081\u3001\u4e21\u65b9\u5411\u306b\u540c\u6642\u306b\u8fba\u3092\u8ffd\u52a0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u8fba\u306e\u524a\u9664\uff1a\u5bfe\u5fdc\u3059\u308b\u9802\u70b9\u306e\u9023\u7d50\u30ea\u30b9\u30c8\u5185\u3067\u6307\u5b9a\u3055\u308c\u305f\u8fba\u3092\u898b\u3064\u3051\u3066\u524a\u9664\u3057\u3001\\(O(m)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u7121\u5411\u30b0\u30e9\u30d5\u3067\u306f\u3001\u4e21\u65b9\u5411\u306e\u8fba\u3092\u540c\u6642\u306b\u524a\u9664\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u9802\u70b9\u306e\u8ffd\u52a0\uff1a\u96a3\u63a5\u30ea\u30b9\u30c8\u306b\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8ffd\u52a0\u3057\u3001\u65b0\u3057\u3044\u9802\u70b9\u3092\u30ea\u30b9\u30c8\u306e\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u306b\u3057\u3001\\(O(1)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u9802\u70b9\u306e\u524a\u9664\uff1a\u96a3\u63a5\u30ea\u30b9\u30c8\u5168\u4f53\u3092\u8d70\u67fb\u3057\u3001\u6307\u5b9a\u3055\u308c\u305f\u9802\u70b9\u3092\u542b\u3080\u3059\u3079\u3066\u306e\u8fba\u3092\u524a\u9664\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\\(O(n + m)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u521d\u671f\u5316\uff1a\u96a3\u63a5\u30ea\u30b9\u30c8\u306b\\(n\\)\u500b\u306e\u9802\u70b9\u3068\\(2m\\)\u500b\u306e\u8fba\u3092\u4f5c\u6210\u3057\u3001\\(O(n + m)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        \u96a3\u63a5\u30ea\u30b9\u30c8\u306e\u521d\u671f\u5316\u8fba\u306e\u8ffd\u52a0\u8fba\u306e\u524a\u9664\u9802\u70b9\u306e\u8ffd\u52a0\u9802\u70b9\u306e\u524a\u9664

        \u56f3 9-8 \u00a0 \u96a3\u63a5\u30ea\u30b9\u30c8\u3067\u306e\u521d\u671f\u5316\u3001\u8fba\u306e\u8ffd\u52a0\u3068\u524a\u9664\u3001\u9802\u70b9\u306e\u8ffd\u52a0\u3068\u524a\u9664

        \u4ee5\u4e0b\u306f\u96a3\u63a5\u30ea\u30b9\u30c8\u306e\u30b3\u30fc\u30c9\u5b9f\u88c5\u3067\u3059\u3002\u4e0a\u56f3\u3068\u6bd4\u8f03\u3057\u3066\u3001\u5b9f\u969b\u306e\u30b3\u30fc\u30c9\u306b\u306f\u4ee5\u4e0b\u306e\u9055\u3044\u304c\u3042\u308a\u307e\u3059\u3002

        • \u9802\u70b9\u306e\u8ffd\u52a0\u3068\u524a\u9664\u306e\u4fbf\u5b9c\u3001\u304a\u3088\u3073\u30b3\u30fc\u30c9\u306e\u7c21\u7d20\u5316\u306e\u305f\u3081\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u4ee3\u308f\u308a\u306b\u30ea\u30b9\u30c8\uff08\u52d5\u7684\u914d\u5217\uff09\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u4f7f\u7528\u3057\u3066\u96a3\u63a5\u30ea\u30b9\u30c8\u3092\u683c\u7d0d\u3057\u3001key\u304c\u9802\u70b9\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3001value\u304c\u305d\u306e\u9802\u70b9\u306e\u96a3\u63a5\u9802\u70b9\u306e\u30ea\u30b9\u30c8\uff08\u9023\u7d50\u30ea\u30b9\u30c8\uff09\u3067\u3059\u3002

        \u3055\u3089\u306b\u3001\u96a3\u63a5\u30ea\u30b9\u30c8\u3067\u9802\u70b9\u3092\u8868\u73fe\u3059\u308b\u305f\u3081\u306bVertex\u30af\u30e9\u30b9\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u305d\u306e\u7406\u7531\u306f\uff1a\u96a3\u63a5\u884c\u5217\u306e\u3088\u3046\u306b\u30ea\u30b9\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u4f7f\u7528\u3057\u3066\u7570\u306a\u308b\u9802\u70b9\u3092\u533a\u5225\u3059\u308b\u5834\u5408\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(i\\)\u306e\u9802\u70b9\u3092\u524a\u9664\u3057\u305f\u3044\u5834\u5408\u3001\u96a3\u63a5\u30ea\u30b9\u30c8\u5168\u4f53\u3092\u8d70\u67fb\u3057\u3001\\(i\\)\u3088\u308a\u5927\u304d\u3044\u3059\u3079\u3066\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u30921\u3064\u305a\u3064\u6e1b\u5c11\u3055\u305b\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u3053\u308c\u306f\u975e\u5e38\u306b\u975e\u52b9\u7387\u7684\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u5404\u9802\u70b9\u304c\u4e00\u610f\u306eVertex\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3067\u3042\u308b\u5834\u5408\u3001\u9802\u70b9\u3092\u524a\u9664\u3057\u3066\u3082\u4ed6\u306e\u9802\u70b9\u306b\u5909\u66f4\u3092\u52a0\u3048\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u305b\u3093\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_adjacency_list.py
        class GraphAdjList:\n    \"\"\"\u96a3\u63a5\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u7121\u5411\u30b0\u30e9\u30d5\u30af\u30e9\u30b9\"\"\"\n\n    def __init__(self, edges: list[list[Vertex]]):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        # \u96a3\u63a5\u30ea\u30b9\u30c8\u3001\u30ad\u30fc: \u9802\u70b9\u3001\u5024: \u305d\u306e\u9802\u70b9\u306e\u96a3\u63a5\u3059\u308b\u5168\u9802\u70b9\n        self.adj_list = dict[Vertex, list[Vertex]]()\n        # \u3059\u3079\u3066\u306e\u9802\u70b9\u3068\u8fba\u3092\u8ffd\u52a0\n        for edge in edges:\n            self.add_vertex(edge[0])\n            self.add_vertex(edge[1])\n            self.add_edge(edge[0], edge[1])\n\n    def size(self) -> int:\n        \"\"\"\u9802\u70b9\u6570\u3092\u53d6\u5f97\"\"\"\n        return len(self.adj_list)\n\n    def add_edge(self, vet1: Vertex, vet2: Vertex):\n        \"\"\"\u8fba\u3092\u8ffd\u52a0\"\"\"\n        if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n            raise ValueError()\n        # \u8fba vet1 - vet2 \u3092\u8ffd\u52a0\n        self.adj_list[vet1].append(vet2)\n        self.adj_list[vet2].append(vet1)\n\n    def remove_edge(self, vet1: Vertex, vet2: Vertex):\n        \"\"\"\u8fba\u3092\u524a\u9664\"\"\"\n        if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n            raise ValueError()\n        # \u8fba vet1 - vet2 \u3092\u524a\u9664\n        self.adj_list[vet1].remove(vet2)\n        self.adj_list[vet2].remove(vet1)\n\n    def add_vertex(self, vet: Vertex):\n        \"\"\"\u9802\u70b9\u3092\u8ffd\u52a0\"\"\"\n        if vet in self.adj_list:\n            return\n        # \u96a3\u63a5\u30ea\u30b9\u30c8\u306b\u65b0\u3057\u3044\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8ffd\u52a0\n        self.adj_list[vet] = []\n\n    def remove_vertex(self, vet: Vertex):\n        \"\"\"\u9802\u70b9\u3092\u524a\u9664\"\"\"\n        if vet not in self.adj_list:\n            raise ValueError()\n        # \u96a3\u63a5\u30ea\u30b9\u30c8\u304b\u3089\u9802\u70b9vet\u306b\u5bfe\u5fdc\u3059\u308b\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u524a\u9664\n        self.adj_list.pop(vet)\n        # \u4ed6\u306e\u9802\u70b9\u306e\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8d70\u67fb\u3057\u3001vet\u3092\u542b\u3080\u3059\u3079\u3066\u306e\u8fba\u3092\u524a\u9664\n        for vertex in self.adj_list:\n            if vet in self.adj_list[vertex]:\n                self.adj_list[vertex].remove(vet)\n\n    def print(self):\n        \"\"\"\u96a3\u63a5\u30ea\u30b9\u30c8\u3092\u51fa\u529b\"\"\"\n        print(\"\u96a3\u63a5\u30ea\u30b9\u30c8 =\")\n        for vertex in self.adj_list:\n            tmp = [v.val for v in self.adj_list[vertex]]\n            print(f\"{vertex.val}: {tmp},\")\n
        graph_adjacency_list.cpp
        /* \u96a3\u63a5\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u7121\u5411\u30b0\u30e9\u30d5\u30af\u30e9\u30b9 */\nclass GraphAdjList {\n  public:\n    // \u96a3\u63a5\u30ea\u30b9\u30c8\u3001\u30ad\u30fc\uff1a\u9802\u70b9\u3001\u5024\uff1a\u305d\u306e\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\n    unordered_map<Vertex *, vector<Vertex *>> adjList;\n\n    /* \u30d9\u30af\u30bf\u30fc\u304b\u3089\u6307\u5b9a\u3055\u308c\u305f\u30ce\u30fc\u30c9\u3092\u524a\u9664 */\n    void remove(vector<Vertex *> &vec, Vertex *vet) {\n        for (int i = 0; i < vec.size(); i++) {\n            if (vec[i] == vet) {\n                vec.erase(vec.begin() + i);\n                break;\n            }\n        }\n    }\n\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    GraphAdjList(const vector<vector<Vertex *>> &edges) {\n        // \u3059\u3079\u3066\u306e\u9802\u70b9\u3068\u8fba\u3092\u8ffd\u52a0\n        for (const vector<Vertex *> &edge : edges) {\n            addVertex(edge[0]);\n            addVertex(edge[1]);\n            addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u9802\u70b9\u6570\u3092\u53d6\u5f97 */\n    int size() {\n        return adjList.size();\n    }\n\n    /* \u8fba\u3092\u8ffd\u52a0 */\n    void addEdge(Vertex *vet1, Vertex *vet2) {\n        if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n            throw invalid_argument(\"Vertex does not exist\");\n        // \u8fba vet1 - vet2 \u3092\u8ffd\u52a0\n        adjList[vet1].push_back(vet2);\n        adjList[vet2].push_back(vet1);\n    }\n\n    /* \u8fba\u3092\u524a\u9664 */\n    void removeEdge(Vertex *vet1, Vertex *vet2) {\n        if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n            throw invalid_argument(\"Vertex does not exist\");\n        // \u8fba vet1 - vet2 \u3092\u524a\u9664\n        remove(adjList[vet1], vet2);\n        remove(adjList[vet2], vet1);\n    }\n\n    /* \u9802\u70b9\u3092\u8ffd\u52a0 */\n    void addVertex(Vertex *vet) {\n        if (adjList.count(vet))\n            return;\n        // \u96a3\u63a5\u30ea\u30b9\u30c8\u306b\u65b0\u3057\u3044\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8ffd\u52a0\n        adjList[vet] = vector<Vertex *>();\n    }\n\n    /* \u9802\u70b9\u3092\u524a\u9664 */\n    void removeVertex(Vertex *vet) {\n        if (!adjList.count(vet))\n            throw invalid_argument(\"Vertex does not exist\");\n        // \u96a3\u63a5\u30ea\u30b9\u30c8\u304b\u3089\u9802\u70b9vet\u306b\u5bfe\u5fdc\u3059\u308b\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u524a\u9664\n        adjList.erase(vet);\n        // \u4ed6\u306e\u9802\u70b9\u306e\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8d70\u67fb\u3057\u3001vet\u3092\u542b\u3080\u3059\u3079\u3066\u306e\u8fba\u3092\u524a\u9664\n        for (auto &adj : adjList) {\n            remove(adj.second, vet);\n        }\n    }\n\n    /* \u96a3\u63a5\u30ea\u30b9\u30c8\u3092\u5370\u5237 */\n    void print() {\n        cout << \"\u96a3\u63a5\u30ea\u30b9\u30c8 =\" << endl;\n        for (auto &adj : adjList) {\n            const auto &key = adj.first;\n            const auto &vec = adj.second;\n            cout << key->val << \": \";\n            printVector(vetsToVals(vec));\n        }\n    }\n};\n
        graph_adjacency_list.java
        /* \u96a3\u63a5\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u7121\u5411\u30b0\u30e9\u30d5\u30af\u30e9\u30b9 */\nclass GraphAdjList {\n    // \u96a3\u63a5\u30ea\u30b9\u30c8\u3001\u30ad\u30fc: \u9802\u70b9\u3001\u5024: \u305d\u306e\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\n    Map<Vertex, List<Vertex>> adjList;\n\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    public GraphAdjList(Vertex[][] edges) {\n        this.adjList = new HashMap<>();\n        // \u3059\u3079\u3066\u306e\u9802\u70b9\u3068\u8fba\u3092\u8ffd\u52a0\n        for (Vertex[] edge : edges) {\n            addVertex(edge[0]);\n            addVertex(edge[1]);\n            addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u9802\u70b9\u6570\u3092\u53d6\u5f97 */\n    public int size() {\n        return adjList.size();\n    }\n\n    /* \u8fba\u3092\u8ffd\u52a0 */\n    public void addEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw new IllegalArgumentException();\n        // \u8fba vet1 - vet2 \u3092\u8ffd\u52a0\n        adjList.get(vet1).add(vet2);\n        adjList.get(vet2).add(vet1);\n    }\n\n    /* \u8fba\u3092\u524a\u9664 */\n    public void removeEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw new IllegalArgumentException();\n        // \u8fba vet1 - vet2 \u3092\u524a\u9664\n        adjList.get(vet1).remove(vet2);\n        adjList.get(vet2).remove(vet1);\n    }\n\n    /* \u9802\u70b9\u3092\u8ffd\u52a0 */\n    public void addVertex(Vertex vet) {\n        if (adjList.containsKey(vet))\n            return;\n        // \u96a3\u63a5\u30ea\u30b9\u30c8\u306b\u65b0\u3057\u3044\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8ffd\u52a0\n        adjList.put(vet, new ArrayList<>());\n    }\n\n    /* \u9802\u70b9\u3092\u524a\u9664 */\n    public void removeVertex(Vertex vet) {\n        if (!adjList.containsKey(vet))\n            throw new IllegalArgumentException();\n        // \u96a3\u63a5\u30ea\u30b9\u30c8\u304b\u3089\u9802\u70b9 vet \u306b\u5bfe\u5fdc\u3059\u308b\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u524a\u9664\n        adjList.remove(vet);\n        // \u4ed6\u306e\u9802\u70b9\u306e\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8d70\u67fb\u3057\u3001vet \u3092\u542b\u3080\u3059\u3079\u3066\u306e\u8fba\u3092\u524a\u9664\n        for (List<Vertex> list : adjList.values()) {\n            list.remove(vet);\n        }\n    }\n\n    /* \u96a3\u63a5\u30ea\u30b9\u30c8\u3092\u51fa\u529b */\n    public void print() {\n        System.out.println(\"\u96a3\u63a5\u30ea\u30b9\u30c8 =\");\n        for (Map.Entry<Vertex, List<Vertex>> pair : adjList.entrySet()) {\n            List<Integer> tmp = new ArrayList<>();\n            for (Vertex vertex : pair.getValue())\n                tmp.add(vertex.val);\n            System.out.println(pair.getKey().val + \": \" + tmp + \",\");\n        }\n    }\n}\n
        graph_adjacency_list.cs
        [class]{GraphAdjList}-[func]{}\n
        graph_adjacency_list.go
        [class]{graphAdjList}-[func]{}\n
        graph_adjacency_list.swift
        [class]{GraphAdjList}-[func]{}\n
        graph_adjacency_list.js
        [class]{GraphAdjList}-[func]{}\n
        graph_adjacency_list.ts
        [class]{GraphAdjList}-[func]{}\n
        graph_adjacency_list.dart
        [class]{GraphAdjList}-[func]{}\n
        graph_adjacency_list.rs
        [class]{GraphAdjList}-[func]{}\n
        graph_adjacency_list.c
        [class]{AdjListNode}-[func]{}\n\n[class]{GraphAdjList}-[func]{}\n
        graph_adjacency_list.kt
        [class]{GraphAdjList}-[func]{}\n
        graph_adjacency_list.rb
        [class]{GraphAdjList}-[func]{}\n
        "},{"location":"chapter_graph/graph_operations/#923","title":"9.2.3 \u00a0 \u52b9\u7387\u306e\u6bd4\u8f03","text":"

        \u30b0\u30e9\u30d5\u306b\\(n\\)\u500b\u306e\u9802\u70b9\u3068\\(m\\)\u500b\u306e\u8fba\u304c\u3042\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u4e0b\u8868\u306f\u96a3\u63a5\u884c\u5217\u3068\u96a3\u63a5\u30ea\u30b9\u30c8\u306e\u6642\u9593\u52b9\u7387\u3068\u7a7a\u9593\u52b9\u7387\u3092\u6bd4\u8f03\u3057\u3066\u3044\u307e\u3059\u3002

        \u8868 9-2 \u00a0 \u96a3\u63a5\u884c\u5217\u3068\u96a3\u63a5\u30ea\u30b9\u30c8\u306e\u6bd4\u8f03

        \u96a3\u63a5\u884c\u5217 \u96a3\u63a5\u30ea\u30b9\u30c8\uff08\u9023\u7d50\u30ea\u30b9\u30c8\uff09 \u96a3\u63a5\u30ea\u30b9\u30c8\uff08\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\uff09 \u96a3\u63a5\u6027\u306e\u5224\u5b9a \\(O(1)\\) \\(O(m)\\) \\(O(1)\\) \u8fba\u306e\u8ffd\u52a0 \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u8fba\u306e\u524a\u9664 \\(O(1)\\) \\(O(m)\\) \\(O(1)\\) \u9802\u70b9\u306e\u8ffd\u52a0 \\(O(n)\\) \\(O(1)\\) \\(O(1)\\) \u9802\u70b9\u306e\u524a\u9664 \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n)\\) \u30e1\u30e2\u30ea\u7a7a\u9593\u4f7f\u7528\u91cf \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n + m)\\)

        \u4e0a\u8868\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\u96a3\u63a5\u30ea\u30b9\u30c8\uff08\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\uff09\u304c\u6700\u9ad8\u306e\u6642\u9593\u52b9\u7387\u3068\u7a7a\u9593\u52b9\u7387\u3092\u6301\u3063\u3066\u3044\u308b\u3088\u3046\u306b\u898b\u3048\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u5b9f\u969b\u306b\u306f\u3001\u96a3\u63a5\u884c\u5217\u3067\u306e\u8fba\u306b\u5bfe\u3059\u308b\u64cd\u4f5c\u304c\u3088\u308a\u52b9\u7387\u7684\u3067\u3001\u5358\u4e00\u306e\u914d\u5217\u30a2\u30af\u30bb\u30b9\u307e\u305f\u306f\u4ee3\u5165\u64cd\u4f5c\u306e\u307f\u304c\u5fc5\u8981\u3067\u3059\u3002\u5168\u4f53\u7684\u306b\u3001\u96a3\u63a5\u884c\u5217\u306f\u300c\u7a7a\u9593\u3068\u6642\u9593\u306e\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u300d\u306e\u539f\u5247\u3092\u4f8b\u793a\u3057\u3001\u96a3\u63a5\u30ea\u30b9\u30c8\u306f\u300c\u6642\u9593\u3068\u7a7a\u9593\u306e\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u300d\u3092\u4f8b\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_graph/graph_traversal/","title":"9.3 \u00a0 \u30b0\u30e9\u30d5\u8d70\u67fb","text":"

        \u6728\u306f\u300c\u4e00\u5bfe\u591a\u300d\u306e\u95a2\u4fc2\u3092\u8868\u73fe\u3057\u3001\u30b0\u30e9\u30d5\u306f\u3088\u308a\u9ad8\u3044\u81ea\u7531\u5ea6\u3092\u6301\u3061\u3001\u4efb\u610f\u306e\u300c\u591a\u5bfe\u591a\u300d\u306e\u95a2\u4fc2\u3092\u8868\u73fe\u3067\u304d\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u6728\u3092\u30b0\u30e9\u30d5\u306e\u7279\u5225\u306a\u30b1\u30fc\u30b9\u3068\u898b\u306a\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u660e\u3089\u304b\u306b\u3001\u6728\u306e\u8d70\u67fb\u64cd\u4f5c\u3082\u30b0\u30e9\u30d5\u8d70\u67fb\u64cd\u4f5c\u306e\u7279\u5225\u306a\u30b1\u30fc\u30b9\u3067\u3059\u3002

        \u30b0\u30e9\u30d5\u3068\u6728\u306e\u4e21\u65b9\u3067\u3001\u8d70\u67fb\u64cd\u4f5c\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306b\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5fdc\u7528\u304c\u5fc5\u8981\u3067\u3059\u3002\u30b0\u30e9\u30d5\u8d70\u67fb\u306f2\u3064\u306e\u30bf\u30a4\u30d7\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a\u5e45\u512a\u5148\u63a2\u7d22\uff08BFS\uff09\u3068\u6df1\u3055\u512a\u5148\u63a2\u7d22\uff08DFS\uff09\u3067\u3059\u3002

        "},{"location":"chapter_graph/graph_traversal/#931","title":"9.3.1 \u00a0 \u5e45\u512a\u5148\u63a2\u7d22","text":"

        \u5e45\u512a\u5148\u63a2\u7d22\u306f\u8fd1\u304f\u304b\u3089\u9060\u304f\u3078\u306e\u8d70\u67fb\u65b9\u6cd5\u3067\u3001\u3042\u308b\u9802\u70b9\u304b\u3089\u958b\u59cb\u3057\u3001\u5e38\u306b\u6700\u3082\u8fd1\u3044\u9802\u70b9\u3092\u512a\u5148\u7684\u306b\u8a2a\u554f\u3057\u3001\u5c64\u3054\u3068\u306b\u5916\u5074\u306b\u5c55\u958b\u3057\u3066\u3044\u304d\u307e\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5de6\u4e0a\u306e\u9802\u70b9\u304b\u3089\u958b\u59cb\u3057\u3001\u307e\u305a\u305d\u306e\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u8d70\u67fb\u3057\u3001\u6b21\u306b\u6b21\u306e\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u8d70\u67fb\u3057\u3001\u4ee5\u4e0b\u540c\u69d8\u306b\u3001\u3059\u3079\u3066\u306e\u9802\u70b9\u304c\u8a2a\u554f\u3055\u308c\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002

        \u56f3 9-9 \u00a0 \u30b0\u30e9\u30d5\u306e\u5e45\u512a\u5148\u8d70\u67fb

        "},{"location":"chapter_graph/graph_traversal/#1","title":"1. \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u88c5","text":"

        BFS\u306f\u901a\u5e38\u30ad\u30e5\u30fc\u306e\u52a9\u3051\u3092\u501f\u308a\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\uff08\u4e0b\u8a18\u306e\u30b3\u30fc\u30c9\u53c2\u7167\uff09\u3002\u30ad\u30e5\u30fc\u306f\u300c\u5148\u5165\u5148\u51fa\u300d\u3067\u3001\u3053\u308c\u306f\u300c\u8fd1\u304f\u304b\u3089\u9060\u304f\u3078\u300d\u8d70\u67fb\u3059\u308bBFS\u306e\u8003\u3048\u65b9\u3068\u4e00\u81f4\u3057\u307e\u3059\u3002

        1. \u958b\u59cb\u9802\u70b9startVet\u3092\u30ad\u30e5\u30fc\u306b\u8ffd\u52a0\u3057\u3001\u30eb\u30fc\u30d7\u3092\u958b\u59cb\u3057\u307e\u3059\u3002
        2. \u30eb\u30fc\u30d7\u306e\u5404\u53cd\u5fa9\u3067\u3001\u30ad\u30e5\u30fc\u306e\u5148\u982d\u306e\u9802\u70b9\u3092\u30dd\u30c3\u30d7\u3057\u3001\u305d\u308c\u3092\u8a2a\u554f\u6e08\u307f\u3068\u3057\u3066\u8a18\u9332\u3057\u3001\u6b21\u306b\u305d\u306e\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u30ad\u30e5\u30fc\u306e\u672b\u5c3e\u306b\u8ffd\u52a0\u3057\u307e\u3059\u3002
        3. \u3059\u3079\u3066\u306e\u9802\u70b9\u304c\u8a2a\u554f\u3055\u308c\u308b\u307e\u3067\u624b\u98062.\u3092\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002

        \u9802\u70b9\u306e\u518d\u8a2a\u554f\u3092\u9632\u3050\u305f\u3081\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8visited\u3092\u4f7f\u7528\u3057\u3066\u3069\u306e\u30ce\u30fc\u30c9\u304c\u8a2a\u554f\u3055\u308c\u305f\u304b\u3092\u8a18\u9332\u3057\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_bfs.py
        def graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n    \"\"\"\u5e45\u512a\u5148\u8d70\u67fb\"\"\"\n    # \u96a3\u63a5\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3057\u3001\u6307\u5b9a\u3055\u308c\u305f\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u53d6\u5f97\n    # \u9802\u70b9\u8d70\u67fb\u30b7\u30fc\u30b1\u30f3\u30b9\n    res = []\n    # \u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8\u3001\u8a2a\u554f\u6e08\u307f\u9802\u70b9\u3092\u8a18\u9332\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    visited = set[Vertex]([start_vet])\n    # BFS\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u308b\u30ad\u30e5\u30fc\n    que = deque[Vertex]([start_vet])\n    # \u9802\u70b9vet\u304b\u3089\u958b\u59cb\u3057\u3001\u3059\u3079\u3066\u306e\u9802\u70b9\u304c\u8a2a\u554f\u3055\u308c\u308b\u307e\u3067\u30eb\u30fc\u30d7\n    while len(que) > 0:\n        vet = que.popleft()  # \u30ad\u30e5\u30fc\u306e\u5148\u982d\u306e\u9802\u70b9\u3092\u30c7\u30ad\u30e5\u30fc\n        res.append(vet)  # \u8a2a\u554f\u6e08\u307f\u9802\u70b9\u3092\u8a18\u9332\n        # \u305d\u306e\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u8d70\u67fb\n        for adj_vet in graph.adj_list[vet]:\n            if adj_vet in visited:\n                continue  # \u65e2\u306b\u8a2a\u554f\u6e08\u307f\u306e\u9802\u70b9\u3092\u30b9\u30ad\u30c3\u30d7\n            que.append(adj_vet)  # \u672a\u8a2a\u554f\u306e\u9802\u70b9\u306e\u307f\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\n            visited.add(adj_vet)  # \u9802\u70b9\u3092\u8a2a\u554f\u6e08\u307f\u3068\u3057\u3066\u30de\u30fc\u30af\n    # \u9802\u70b9\u8d70\u67fb\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u8fd4\u3059\n    return res\n
        graph_bfs.cpp
        /* \u5e45\u512a\u5148\u8d70\u67fb */\n// \u96a3\u63a5\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3057\u3001\u6307\u5b9a\u3055\u308c\u305f\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u53d6\u5f97\nvector<Vertex *> graphBFS(GraphAdjList &graph, Vertex *startVet) {\n    // \u9802\u70b9\u8d70\u67fb\u9806\u5e8f\n    vector<Vertex *> res;\n    // \u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8\u3001\u8a2a\u554f\u6e08\u307f\u9802\u70b9\u3092\u8a18\u9332\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    unordered_set<Vertex *> visited = {startVet};\n    // BFS\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u308b\u30ad\u30e5\u30fc\n    queue<Vertex *> que;\n    que.push(startVet);\n    // \u9802\u70b9vet\u304b\u3089\u958b\u59cb\u3057\u3001\u3059\u3079\u3066\u306e\u9802\u70b9\u304c\u8a2a\u554f\u3055\u308c\u308b\u307e\u3067\u30eb\u30fc\u30d7\n    while (!que.empty()) {\n        Vertex *vet = que.front();\n        que.pop();          // \u30ad\u30e5\u30fc\u306e\u5148\u982d\u306e\u9802\u70b9\u3092\u30c7\u30ad\u30e5\u30fc\n        res.push_back(vet); // \u8a2a\u554f\u6e08\u307f\u9802\u70b9\u3092\u8a18\u9332\n        // \u305d\u306e\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u8d70\u67fb\n        for (auto adjVet : graph.adjList[vet]) {\n            if (visited.count(adjVet))\n                continue;            // \u3059\u3067\u306b\u8a2a\u554f\u6e08\u307f\u306e\u9802\u70b9\u3092\u30b9\u30ad\u30c3\u30d7\n            que.push(adjVet);        // \u672a\u8a2a\u554f\u306e\u9802\u70b9\u306e\u307f\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\n            visited.emplace(adjVet); // \u9802\u70b9\u3092\u8a2a\u554f\u6e08\u307f\u3068\u3057\u3066\u30de\u30fc\u30af\n        }\n    }\n    // \u9802\u70b9\u8d70\u67fb\u9806\u5e8f\u3092\u8fd4\u3059\n    return res;\n}\n
        graph_bfs.java
        /* \u5e45\u512a\u5148\u8d70\u67fb */\n// \u96a3\u63a5\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3057\u3001\u6307\u5b9a\u3057\u305f\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u53d6\u5f97\nList<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {\n    // \u9802\u70b9\u8d70\u67fb\u9806\u5e8f\n    List<Vertex> res = new ArrayList<>();\n    // \u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8\u3001\u8a2a\u554f\u6e08\u307f\u306e\u9802\u70b9\u3092\u8a18\u9332\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    Set<Vertex> visited = new HashSet<>();\n    visited.add(startVet);\n    // BFS \u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3059\u308b\u30ad\u30e5\u30fc\n    Queue<Vertex> que = new LinkedList<>();\n    que.offer(startVet);\n    // \u9802\u70b9 vet \u304b\u3089\u958b\u59cb\u3057\u3001\u3059\u3079\u3066\u306e\u9802\u70b9\u304c\u8a2a\u554f\u3055\u308c\u308b\u307e\u3067\u30eb\u30fc\u30d7\n    while (!que.isEmpty()) {\n        Vertex vet = que.poll(); // \u30ad\u30e5\u30fc\u306e\u5148\u982d\u306e\u9802\u70b9\u3092\u30c7\u30ad\u30e5\u30fc\n        res.add(vet);            // \u8a2a\u554f\u3057\u305f\u9802\u70b9\u3092\u8a18\u9332\n        // \u305d\u306e\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u8d70\u67fb\n        for (Vertex adjVet : graph.adjList.get(vet)) {\n            if (visited.contains(adjVet))\n                continue;        // \u3059\u3067\u306b\u8a2a\u554f\u6e08\u307f\u306e\u9802\u70b9\u3092\u30b9\u30ad\u30c3\u30d7\n            que.offer(adjVet);   // \u672a\u8a2a\u554f\u306e\u9802\u70b9\u306e\u307f\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\n            visited.add(adjVet); // \u9802\u70b9\u3092\u8a2a\u554f\u6e08\u307f\u3068\u3057\u3066\u30de\u30fc\u30af\n        }\n    }\n    // \u9802\u70b9\u8d70\u67fb\u9806\u5e8f\u3092\u8fd4\u3059\n    return res;\n}\n
        graph_bfs.cs
        [class]{graph_bfs}-[func]{GraphBFS}\n
        graph_bfs.go
        [class]{}-[func]{graphBFS}\n
        graph_bfs.swift
        [class]{}-[func]{graphBFS}\n
        graph_bfs.js
        [class]{}-[func]{graphBFS}\n
        graph_bfs.ts
        [class]{}-[func]{graphBFS}\n
        graph_bfs.dart
        [class]{}-[func]{graphBFS}\n
        graph_bfs.rs
        [class]{}-[func]{graph_bfs}\n
        graph_bfs.c
        [class]{Queue}-[func]{}\n\n[class]{}-[func]{isVisited}\n\n[class]{}-[func]{graphBFS}\n
        graph_bfs.kt
        [class]{}-[func]{graphBFS}\n
        graph_bfs.rb
        [class]{}-[func]{graph_bfs}\n

        \u30b3\u30fc\u30c9\u306f\u6bd4\u8f03\u7684\u62bd\u8c61\u7684\u3067\u3059\u304c\u3001\u4e0b\u56f3\u3068\u6bd4\u8f03\u3059\u308b\u3053\u3068\u3067\u3088\u308a\u826f\u304f\u7406\u89e3\u3067\u304d\u307e\u3059\u3002

        <1><2><3><4><5><6><7><8><9><10><11>

        \u56f3 9-10 \u00a0 \u30b0\u30e9\u30d5\u306e\u5e45\u512a\u5148\u63a2\u7d22\u306e\u624b\u9806

        \u5e45\u512a\u5148\u8d70\u67fb\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u306f\u4e00\u610f\u3067\u3059\u304b\uff1f

        \u4e00\u610f\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u5e45\u512a\u5148\u8d70\u67fb\u306f\u300c\u8fd1\u304f\u304b\u3089\u9060\u304f\u300d\u306e\u9806\u5e8f\u3067\u8d70\u67fb\u3059\u308b\u3053\u3068\u306e\u307f\u3092\u8981\u6c42\u3057\u3001\u540c\u3058\u8ddd\u96e2\u306e\u9802\u70b9\u306e\u8d70\u67fb\u9806\u5e8f\u306f\u4efb\u610f\u306b\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u4e0a\u56f3\u3067\u306f\u3001\u9802\u70b9\\(1\\)\u3068\\(3\\)\u306e\u8a2a\u554f\u9806\u5e8f\u3092\u4ea4\u63db\u3067\u304d\u307e\u3059\u3057\u3001\u9802\u70b9\\(2\\)\u3001\\(4\\)\u3001\\(6\\)\u306e\u9806\u5e8f\u3082\u540c\u69d8\u3067\u3059\u3002

        "},{"location":"chapter_graph/graph_traversal/#2","title":"2. \u00a0 \u8a08\u7b97\u91cf\u5206\u6790","text":"

        \u6642\u9593\u8a08\u7b97\u91cf\uff1a\u3059\u3079\u3066\u306e\u9802\u70b9\u304c\u4e00\u5ea6\u305a\u3064\u30a8\u30f3\u30ad\u30e5\u30fc\u304a\u3088\u3073\u30c7\u30ad\u30e5\u30fc\u3055\u308c\u3001\\(O(|V|)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u96a3\u63a5\u9802\u70b9\u3092\u8d70\u67fb\u3059\u308b\u904e\u7a0b\u3067\u3001\u7121\u5411\u30b0\u30e9\u30d5\u3067\u3042\u308b\u305f\u3081\u3001\u3059\u3079\u3066\u306e\u8fba\u304c\\(2\\)\u56de\u8a2a\u554f\u3055\u308c\u3001\\(O(2|E|)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u5168\u4f53\u3067\\(O(|V| + |E|)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        \u7a7a\u9593\u8a08\u7b97\u91cf\uff1a\u30ea\u30b9\u30c8res\u3001\u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8visited\u3001\u30ad\u30e5\u30fcque\u306e\u6700\u5927\u9802\u70b9\u6570\u306f\\(|V|\\)\u3067\u3001\\(O(|V|)\\)\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        "},{"location":"chapter_graph/graph_traversal/#932","title":"9.3.2 \u00a0 \u6df1\u3055\u512a\u5148\u63a2\u7d22","text":"

        \u6df1\u3055\u512a\u5148\u63a2\u7d22\u306f\u53ef\u80fd\u306a\u9650\u308a\u9060\u304f\u307e\u3067\u884c\u304d\u3001\u305d\u308c\u4ee5\u4e0a\u306e\u30d1\u30b9\u304c\u306a\u3044\u5834\u5408\u306b\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30af\u3059\u308b\u8d70\u67fb\u65b9\u6cd5\u3067\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5de6\u4e0a\u306e\u9802\u70b9\u304b\u3089\u958b\u59cb\u3057\u3001\u305d\u308c\u4ee5\u4e0a\u306e\u30d1\u30b9\u304c\u306a\u304f\u306a\u308b\u307e\u3067\u73fe\u5728\u306e\u9802\u70b9\u306e\u3044\u305a\u308c\u304b\u306e\u96a3\u63a5\u9802\u70b9\u3092\u8a2a\u554f\u3057\u3001\u6b21\u306b\u623b\u3063\u3066\u7d9a\u884c\u3057\u3001\u3059\u3079\u3066\u306e\u9802\u70b9\u304c\u8d70\u67fb\u3055\u308c\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002

        \u56f3 9-11 \u00a0 \u30b0\u30e9\u30d5\u306e\u6df1\u3055\u512a\u5148\u8d70\u67fb

        "},{"location":"chapter_graph/graph_traversal/#1_1","title":"1. \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u88c5","text":"

        \u3053\u306e\u300c\u53ef\u80fd\u306a\u9650\u308a\u9060\u304f\u307e\u3067\u884c\u3063\u3066\u304b\u3089\u623b\u308b\u300d\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d1\u30e9\u30c0\u30a4\u30e0\u306f\u901a\u5e38\u518d\u5e30\u306b\u57fa\u3065\u3044\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002\u5e45\u512a\u5148\u63a2\u7d22\u3068\u540c\u69d8\u306b\u3001\u6df1\u3055\u512a\u5148\u63a2\u7d22\u3067\u3082\u3001\u518d\u8a2a\u554f\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u8a2a\u554f\u6e08\u307f\u9802\u70b9\u3092\u8a18\u9332\u3059\u308b\u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8visited\u306e\u52a9\u3051\u304c\u5fc5\u8981\u3067\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_dfs.py
        def dfs(graph: GraphAdjList, visited: set[Vertex], res: list[Vertex], vet: Vertex):\n    \"\"\"\u6df1\u3055\u512a\u5148\u8d70\u67fb\u306e\u30d8\u30eb\u30d1\u30fc\u95a2\u6570\"\"\"\n    res.append(vet)  # \u8a2a\u554f\u6e08\u307f\u9802\u70b9\u3092\u8a18\u9332\n    visited.add(vet)  # \u9802\u70b9\u3092\u8a2a\u554f\u6e08\u307f\u3068\u3057\u3066\u30de\u30fc\u30af\n    # \u305d\u306e\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u8d70\u67fb\n    for adjVet in graph.adj_list[vet]:\n        if adjVet in visited:\n            continue  # \u65e2\u306b\u8a2a\u554f\u6e08\u307f\u306e\u9802\u70b9\u3092\u30b9\u30ad\u30c3\u30d7\n        # \u96a3\u63a5\u9802\u70b9\u3092\u518d\u5e30\u7684\u306b\u8a2a\u554f\n        dfs(graph, visited, res, adjVet)\n\ndef graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n    \"\"\"\u6df1\u3055\u512a\u5148\u8d70\u67fb\"\"\"\n    # \u96a3\u63a5\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3057\u3001\u6307\u5b9a\u3055\u308c\u305f\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u53d6\u5f97\n    # \u9802\u70b9\u8d70\u67fb\u30b7\u30fc\u30b1\u30f3\u30b9\n    res = []\n    # \u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8\u3001\u8a2a\u554f\u6e08\u307f\u9802\u70b9\u3092\u8a18\u9332\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    visited = set[Vertex]()\n    dfs(graph, visited, res, start_vet)\n    return res\n
        graph_dfs.cpp
        /* \u6df1\u3055\u512a\u5148\u8d70\u67fb\u30d8\u30eb\u30d1\u30fc\u95a2\u6570 */\nvoid dfs(GraphAdjList &graph, unordered_set<Vertex *> &visited, vector<Vertex *> &res, Vertex *vet) {\n    res.push_back(vet);   // \u8a2a\u554f\u6e08\u307f\u9802\u70b9\u3092\u8a18\u9332\n    visited.emplace(vet); // \u9802\u70b9\u3092\u8a2a\u554f\u6e08\u307f\u3068\u3057\u3066\u30de\u30fc\u30af\n    // \u305d\u306e\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u8d70\u67fb\n    for (Vertex *adjVet : graph.adjList[vet]) {\n        if (visited.count(adjVet))\n            continue; // \u3059\u3067\u306b\u8a2a\u554f\u6e08\u307f\u306e\u9802\u70b9\u3092\u30b9\u30ad\u30c3\u30d7\n        // \u96a3\u63a5\u9802\u70b9\u3092\u518d\u5e30\u7684\u306b\u8a2a\u554f\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* \u6df1\u3055\u512a\u5148\u8d70\u67fb */\n// \u96a3\u63a5\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3057\u3001\u6307\u5b9a\u3055\u308c\u305f\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u53d6\u5f97\nvector<Vertex *> graphDFS(GraphAdjList &graph, Vertex *startVet) {\n    // \u9802\u70b9\u8d70\u67fb\u9806\u5e8f\n    vector<Vertex *> res;\n    // \u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8\u3001\u8a2a\u554f\u6e08\u307f\u9802\u70b9\u3092\u8a18\u9332\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    unordered_set<Vertex *> visited;\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
        graph_dfs.java
        /* \u6df1\u3055\u512a\u5148\u8d70\u67fb\u306e\u88dc\u52a9\u95a2\u6570 */\nvoid dfs(GraphAdjList graph, Set<Vertex> visited, List<Vertex> res, Vertex vet) {\n    res.add(vet);     // \u8a2a\u554f\u3057\u305f\u9802\u70b9\u3092\u8a18\u9332\n    visited.add(vet); // \u9802\u70b9\u3092\u8a2a\u554f\u6e08\u307f\u3068\u3057\u3066\u30de\u30fc\u30af\n    // \u305d\u306e\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u8d70\u67fb\n    for (Vertex adjVet : graph.adjList.get(vet)) {\n        if (visited.contains(adjVet))\n            continue; // \u3059\u3067\u306b\u8a2a\u554f\u6e08\u307f\u306e\u9802\u70b9\u3092\u30b9\u30ad\u30c3\u30d7\n        // \u96a3\u63a5\u9802\u70b9\u3092\u518d\u5e30\u7684\u306b\u8a2a\u554f\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* \u6df1\u3055\u512a\u5148\u8d70\u67fb */\n// \u96a3\u63a5\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3057\u3001\u6307\u5b9a\u3057\u305f\u9802\u70b9\u306e\u3059\u3079\u3066\u306e\u96a3\u63a5\u9802\u70b9\u3092\u53d6\u5f97\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n    // \u9802\u70b9\u8d70\u67fb\u9806\u5e8f\n    List<Vertex> res = new ArrayList<>();\n    // \u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8\u3001\u8a2a\u554f\u6e08\u307f\u306e\u9802\u70b9\u3092\u8a18\u9332\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    Set<Vertex> visited = new HashSet<>();\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
        graph_dfs.cs
        [class]{graph_dfs}-[func]{DFS}\n\n[class]{graph_dfs}-[func]{GraphDFS}\n
        graph_dfs.go
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{graphDFS}\n
        graph_dfs.swift
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{graphDFS}\n
        graph_dfs.js
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{graphDFS}\n
        graph_dfs.ts
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{graphDFS}\n
        graph_dfs.dart
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{graphDFS}\n
        graph_dfs.rs
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{graph_dfs}\n
        graph_dfs.c
        [class]{}-[func]{isVisited}\n\n[class]{}-[func]{dfs}\n\n[class]{}-[func]{graphDFS}\n
        graph_dfs.kt
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{graphDFS}\n
        graph_dfs.rb
        [class]{}-[func]{dfs}\n\n[class]{}-[func]{graph_dfs}\n

        \u6df1\u3055\u512a\u5148\u63a2\u7d22\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d7\u30ed\u30bb\u30b9\u3092\u4e0b\u56f3\u306b\u793a\u3057\u307e\u3059\u3002

        • \u7834\u7dda\u306f\u4e0b\u5411\u304d\u306e\u518d\u5e30\u3092\u8868\u3057\u3001\u65b0\u3057\u3044\u9802\u70b9\u3092\u8a2a\u554f\u3059\u308b\u305f\u3081\u306b\u65b0\u3057\u3044\u518d\u5e30\u30e1\u30bd\u30c3\u30c9\u304c\u958b\u59cb\u3055\u308c\u305f\u3053\u3068\u3092\u793a\u3057\u307e\u3059\u3002
        • \u66f2\u7dda\u306e\u7834\u7dda\u306f\u4e0a\u5411\u304d\u306e\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30af\u3092\u8868\u3057\u3001\u3053\u306e\u518d\u5e30\u30e1\u30bd\u30c3\u30c9\u304c\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u304c\u958b\u59cb\u3055\u308c\u305f\u4f4d\u7f6e\u306b\u623b\u3063\u305f\u3053\u3068\u3092\u793a\u3057\u307e\u3059\u3002

        \u7406\u89e3\u3092\u6df1\u3081\u308b\u305f\u3081\u3001\u4e0b\u56f3\u3068\u30b3\u30fc\u30c9\u3092\u7d44\u307f\u5408\u308f\u305b\u3066\u3001DFS\u30d7\u30ed\u30bb\u30b9\u5168\u4f53\u3092\u982d\u306e\u4e2d\u3067\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\uff08\u307e\u305f\u306f\u63cf\u753b\uff09\u3059\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002\u5404\u518d\u5e30\u30e1\u30bd\u30c3\u30c9\u304c\u3044\u3064\u958b\u59cb\u3055\u308c\u3001\u3044\u3064\u623b\u308b\u304b\u3092\u542b\u3081\u3066\u3067\u3059\u3002

        <1><2><3><4><5><6><7><8><9><10><11>

        \u56f3 9-12 \u00a0 \u30b0\u30e9\u30d5\u306e\u6df1\u3055\u512a\u5148\u63a2\u7d22\u306e\u624b\u9806

        \u6df1\u3055\u512a\u5148\u8d70\u67fb\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u306f\u4e00\u610f\u3067\u3059\u304b\uff1f

        \u5e45\u512a\u5148\u8d70\u67fb\u3068\u540c\u69d8\u306b\u3001\u6df1\u3055\u512a\u5148\u8d70\u67fb\u30b7\u30fc\u30b1\u30f3\u30b9\u306e\u9806\u5e8f\u3082\u4e00\u610f\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u3042\u308b\u9802\u70b9\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u3069\u306e\u65b9\u5411\u3092\u6700\u521d\u306b\u63a2\u7d22\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u3002\u3064\u307e\u308a\u3001\u96a3\u63a5\u9802\u70b9\u306e\u9806\u5e8f\u306f\u4efb\u610f\u306b\u30b7\u30e3\u30c3\u30d5\u30eb\u3067\u304d\u307e\u3059\u304c\u3001\u3059\u3079\u3066\u6df1\u3055\u512a\u5148\u8d70\u67fb\u306e\u4e00\u90e8\u3067\u3059\u3002

        \u6728\u306e\u8d70\u67fb\u3092\u4f8b\u306b\u53d6\u308b\u3068\u3001\u300c\u6839 \\(\\rightarrow\\) \u5de6 \\(\\rightarrow\\) \u53f3\u300d\u3001\u300c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u300d\u3001\u300c\u5de6 \\(\\rightarrow\\) \u53f3 \\(\\rightarrow\\) \u6839\u300d\u306f\u3001\u305d\u308c\u305e\u308c\u524d\u9806\u3001\u4e2d\u9806\u3001\u5f8c\u9806\u8d70\u67fb\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002\u3053\u308c\u3089\u306f3\u3064\u306e\u7570\u306a\u308b\u8d70\u67fb\u512a\u5148\u5ea6\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u304c\u30013\u3064\u3059\u3079\u3066\u304c\u6df1\u3055\u512a\u5148\u8d70\u67fb\u3068\u898b\u306a\u3055\u308c\u307e\u3059\u3002

        "},{"location":"chapter_graph/graph_traversal/#2_1","title":"2. \u00a0 \u8a08\u7b97\u91cf\u5206\u6790","text":"

        \u6642\u9593\u8a08\u7b97\u91cf\uff1a\u3059\u3079\u3066\u306e\u9802\u70b9\u304c\u4e00\u5ea6\u8a2a\u554f\u3055\u308c\u3001\\(O(|V|)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u3059\u3079\u3066\u306e\u8fba\u304c2\u56de\u8a2a\u554f\u3055\u308c\u3001\\(O(2|E|)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u5168\u4f53\u3067\\(O(|V| + |E|)\\)\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        \u7a7a\u9593\u8a08\u7b97\u91cf\uff1a\u30ea\u30b9\u30c8res\u3001\u30cf\u30c3\u30b7\u30e5\u30bb\u30c3\u30c8visited\u306e\u6700\u5927\u9802\u70b9\u6570\u306f\\(|V|\\)\u3067\u3001\u6700\u5927\u518d\u5e30\u6df1\u5ea6\u306f\\(|V|\\)\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\\(O(|V|)\\)\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        "},{"location":"chapter_graph/summary/","title":"9.4 \u00a0 \u307e\u3068\u3081","text":""},{"location":"chapter_graph/summary/#1","title":"1. \u00a0 \u91cd\u8981\u306a\u5fa9\u7fd2","text":"
        • \u30b0\u30e9\u30d5\u306f\u9802\u70b9\u3068\u8fba\u3067\u69cb\u6210\u3055\u308c\u307e\u3059\u3002\u9802\u70b9\u306e\u96c6\u5408\u3068\u8fba\u306e\u96c6\u5408\u3068\u3057\u3066\u8a18\u8ff0\u3067\u304d\u307e\u3059\u3002
        • \u7dda\u5f62\u95a2\u4fc2\uff08\u9023\u7d50\u30ea\u30b9\u30c8\u306a\u3069\uff09\u3084\u968e\u5c64\u95a2\u4fc2\uff08\u6728\u306a\u3069\uff09\u3068\u6bd4\u8f03\u3057\u3066\u3001\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u95a2\u4fc2\uff08\u30b0\u30e9\u30d5\uff09\u306f\u3088\u308a\u5927\u304d\u306a\u67d4\u8edf\u6027\u3092\u63d0\u4f9b\u3057\u3001\u3088\u308a\u8907\u96d1\u306b\u306a\u308a\u307e\u3059\u3002
        • \u6709\u5411\u30b0\u30e9\u30d5\u3067\u306f\u3001\u8fba\u306b\u65b9\u5411\u304c\u3042\u308a\u307e\u3059\u3002\u9023\u7d50\u30b0\u30e9\u30d5\u3067\u306f\u3001\u4efb\u610f\u306e\u9802\u70b9\u304b\u3089\u4ed6\u306e\u4efb\u610f\u306e\u9802\u70b9\u306b\u5230\u9054\u3067\u304d\u307e\u3059\u3002\u91cd\u307f\u4ed8\u304d\u30b0\u30e9\u30d5\u3067\u306f\u3001\u5404\u8fba\u306b\u95a2\u9023\u3059\u308b\u91cd\u307f\u5909\u6570\u304c\u3042\u308a\u307e\u3059\u3002
        • \u96a3\u63a5\u884c\u5217\u306f\u3001\u884c\u5217\uff082\u6b21\u5143\u914d\u5217\uff09\u3092\u4f7f\u7528\u3057\u3066\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3059\u308b\u65b9\u6cd5\u3067\u3059\u3002\u884c\u3068\u5217\u306f\u9802\u70b9\u3092\u8868\u3057\u307e\u3059\u3002\u884c\u5217\u8981\u7d20\u306e\u5024\u306f\u30012\u3064\u306e\u9802\u70b9\u9593\u306b\u8fba\u304c\u3042\u308b\u304b\u3069\u3046\u304b\u3092\u793a\u3057\u3001\u8fba\u304c\u3042\u308b\u5834\u5408\u306f\\(1\\)\u3001\u306a\u3044\u5834\u5408\u306f\\(0\\)\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u96a3\u63a5\u884c\u5217\u306f\u8fba\u306e\u8ffd\u52a0\u3001\u524a\u9664\u3001\u30c1\u30a7\u30c3\u30af\u306a\u3069\u306e\u64cd\u4f5c\u306b\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3059\u304c\u3001\u3088\u308a\u591a\u304f\u306e\u30b9\u30da\u30fc\u30b9\u304c\u5fc5\u8981\u3067\u3059\u3002
        • \u96a3\u63a5\u30ea\u30b9\u30c8\u306f\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u96c6\u5408\u3092\u4f7f\u7528\u3057\u3066\u30b0\u30e9\u30d5\u3092\u8868\u73fe\u3059\u308b\u3082\u3046\u4e00\u3064\u306e\u4e00\u822c\u7684\u306a\u65b9\u6cd5\u3067\u3059\u3002\u30b0\u30e9\u30d5\u5185\u306e\u5404\u9802\u70b9\u306b\u306f\u3001\u305d\u306e\u96a3\u63a5\u3059\u308b\u3059\u3079\u3066\u306e\u9802\u70b9\u3092\u542b\u3080\u30ea\u30b9\u30c8\u304c\u3042\u308a\u307e\u3059\u3002\\(i\\)\u756a\u76ee\u306e\u30ea\u30b9\u30c8\u306f\u9802\u70b9\\(i\\)\u3092\u8868\u3057\u307e\u3059\u3002\u96a3\u63a5\u30ea\u30b9\u30c8\u306f\u96a3\u63a5\u884c\u5217\u3068\u6bd4\u8f03\u3057\u3066\u3088\u308a\u5c11\u306a\u3044\u30b9\u30da\u30fc\u30b9\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u8fba\u3092\u898b\u3064\u3051\u308b\u305f\u3081\u306b\u30ea\u30b9\u30c8\u3092\u8d70\u67fb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u6642\u9593\u52b9\u7387\u306f\u4f4e\u304f\u306a\u308a\u307e\u3059\u3002
        • \u96a3\u63a5\u30ea\u30b9\u30c8\u306e\u9023\u7d50\u30ea\u30b9\u30c8\u304c\u5341\u5206\u306b\u9577\u304f\u306a\u3063\u305f\u3068\u304d\u3001\u30eb\u30c3\u30af\u30a2\u30c3\u30d7\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u308b\u305f\u3081\u306b\u8d64\u9ed2\u6728\u3084\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306b\u5909\u63db\u3067\u304d\u307e\u3059\u3002
        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u8a2d\u8a08\u306e\u89b3\u70b9\u304b\u3089\u3001\u96a3\u63a5\u884c\u5217\u306f\u300c\u7a7a\u9593\u3068\u6642\u9593\u306e\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u300d\u306e\u6982\u5ff5\u3092\u53cd\u6620\u3057\u3001\u96a3\u63a5\u30ea\u30b9\u30c8\u306f\u300c\u6642\u9593\u3068\u7a7a\u9593\u306e\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u300d\u3092\u53cd\u6620\u3057\u307e\u3059\u3002
        • \u30b0\u30e9\u30d5\u306f\u3001\u30bd\u30fc\u30b7\u30e3\u30eb\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u3084\u5730\u4e0b\u9244\u8def\u7dda\u306a\u3069\u3001\u3055\u307e\u3056\u307e\u306a\u73fe\u5b9f\u4e16\u754c\u306e\u30b7\u30b9\u30c6\u30e0\u3092\u30e2\u30c7\u30eb\u5316\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002
        • \u6728\u306f\u30b0\u30e9\u30d5\u306e\u7279\u5225\u306a\u30b1\u30fc\u30b9\u3067\u3042\u308a\u3001\u6728\u306e\u8d70\u67fb\u3082\u30b0\u30e9\u30d5\u8d70\u67fb\u306e\u7279\u5225\u306a\u30b1\u30fc\u30b9\u3067\u3059\u3002
        • \u30b0\u30e9\u30d5\u306e\u5e45\u512a\u5148\u8d70\u67fb\u306f\u3001\u8fd1\u304f\u304b\u3089\u9060\u304f\u3078\u3068\u5c64\u3054\u3068\u306b\u62e1\u5f35\u3059\u308b\u63a2\u7d22\u65b9\u6cd5\u3067\u3001\u901a\u5e38\u30ad\u30e5\u30fc\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u30b0\u30e9\u30d5\u306e\u6df1\u3055\u512a\u5148\u8d70\u67fb\u306f\u3001\u305d\u308c\u4ee5\u4e0a\u306e\u30d1\u30b9\u304c\u306a\u3044\u5834\u5408\u306b\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30af\u3059\u308b\u524d\u306b\u3001\u307e\u305a\u7d42\u7aef\u306b\u5230\u9054\u3059\u308b\u3053\u3068\u3092\u512a\u5148\u3059\u308b\u63a2\u7d22\u65b9\u6cd5\u3067\u3059\u3002\u3057\u3070\u3057\u3070\u518d\u5e30\u3092\u4f7f\u7528\u3057\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002
        "},{"location":"chapter_graph/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q: \u30d1\u30b9\u306f\u9802\u70b9\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u307e\u3059\u304b\u3001\u305d\u308c\u3068\u3082\u8fba\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u307e\u3059\u304b\uff1f

        \u30b0\u30e9\u30d5\u7406\u8ad6\u3067\u306f\u3001\u30b0\u30e9\u30d5\u5185\u306e\u30d1\u30b9\u306f\u9802\u70b9\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u7d50\u3076\u6709\u9650\u307e\u305f\u306f\u7121\u9650\u306e\u8fba\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3067\u3059\u3002

        \u3053\u306e\u6587\u66f8\u3067\u306f\u3001\u30d1\u30b9\u306f\u9802\u70b9\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3067\u306f\u306a\u304f\u3001\u8fba\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3068\u8003\u3048\u3089\u308c\u307e\u3059\u3002\u3053\u308c\u306f\u30012\u3064\u306e\u9802\u70b9\u3092\u7d50\u3076\u8907\u6570\u306e\u8fba\u304c\u3042\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u305d\u306e\u5834\u5408\u5404\u8fba\u304c\u30d1\u30b9\u306b\u5bfe\u5fdc\u3059\u308b\u305f\u3081\u3067\u3059\u3002

        Q: \u975e\u9023\u7d50\u30b0\u30e9\u30d5\u3067\u306f\u3001\u8d70\u67fb\u3067\u304d\u306a\u3044\u70b9\u304c\u3042\u308a\u307e\u3059\u304b\uff1f

        \u975e\u9023\u7d50\u30b0\u30e9\u30d5\u3067\u306f\u3001\u7279\u5b9a\u306e\u70b9\u304b\u3089\u5230\u9054\u3067\u304d\u306a\u3044\u9802\u70b9\u304c\u5c11\u306a\u304f\u3068\u30821\u3064\u3042\u308a\u307e\u3059\u3002\u975e\u9023\u7d50\u30b0\u30e9\u30d5\u3092\u8d70\u67fb\u3059\u308b\u306b\u306f\u3001\u30b0\u30e9\u30d5\u306e\u3059\u3079\u3066\u306e\u9023\u7d50\u6210\u5206\u3092\u8d70\u67fb\u3059\u308b\u305f\u3081\u306b\u8907\u6570\u306e\u958b\u59cb\u70b9\u3092\u8a2d\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        Q: \u96a3\u63a5\u30ea\u30b9\u30c8\u3067\u3001\u300c\u305d\u306e\u9802\u70b9\u306b\u63a5\u7d9a\u3055\u308c\u305f\u3059\u3079\u3066\u306e\u9802\u70b9\u300d\u306e\u9806\u5e8f\u306f\u91cd\u8981\u3067\u3059\u304b\uff1f

        \u4efb\u610f\u306e\u9806\u5e8f\u3067\u69cb\u3044\u307e\u305b\u3093\u3002\u305f\u3060\u3057\u3001\u5b9f\u969b\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u306f\u3001\u9802\u70b9\u304c\u8ffd\u52a0\u3055\u308c\u305f\u9806\u5e8f\u3084\u9802\u70b9\u5024\u306e\u9806\u5e8f\u306a\u3069\u3001\u7279\u5b9a\u306e\u30eb\u30fc\u30eb\u306b\u5f93\u3063\u3066\u305d\u308c\u3089\u3092\u30bd\u30fc\u30c8\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u7279\u5b9a\u306e\u6975\u5024\u3092\u6301\u3064\u9802\u70b9\u3092\u7d20\u65e9\u304f\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_greedy/","title":"\u7b2c 15 \u7ae0 \u00a0 \u8caa\u6b32\u6cd5","text":"

        Abstract

        \u3072\u307e\u308f\u308a\u306f\u592a\u967d\u306e\u65b9\u3092\u5411\u304d\u3001\u5e38\u306b\u81ea\u5206\u306b\u3068\u3063\u3066\u6700\u5927\u306e\u6210\u9577\u3092\u6c42\u3081\u307e\u3059\u3002

        \u8caa\u6b32\u306a\u6226\u7565\u306f\u3001\u4e00\u9023\u306e\u5358\u7d14\u306a\u9078\u629e\u3092\u901a\u3058\u3066\u3001\u6bb5\u968e\u7684\u306b\u6700\u826f\u306e\u7b54\u3048\u3078\u3068\u5c0e\u304d\u307e\u3059\u3002

        "},{"location":"chapter_greedy/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 15.1 \u00a0 \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0
        • 15.2 \u00a0 \u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c
        • 15.3 \u00a0 \u6700\u5927\u5bb9\u91cf\u554f\u984c
        • 15.4 \u00a0 \u6700\u5927\u7a4d\u5207\u65ad\u554f\u984c
        • 15.5 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_greedy/fractional_knapsack_problem/","title":"15.2 \u00a0 \u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c","text":"

        Question

        \\(n\\) \u500b\u306e\u30a2\u30a4\u30c6\u30e0\u304c\u4e0e\u3048\u3089\u308c\u3001\\(i\\) \u756a\u76ee\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u91cd\u91cf\u306f \\(wgt[i-1]\\) \u3067\u5024\u306f \\(val[i-1]\\) \u3067\u3059\u3002\u5bb9\u91cf\u304c \\(cap\\) \u306e\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u304c\u3042\u308a\u307e\u3059\u3002\u5404\u30a2\u30a4\u30c6\u30e0\u306f1\u56de\u306e\u307f\u9078\u629e\u3067\u304d\u307e\u3059\u304c\u3001\u30a2\u30a4\u30c6\u30e0\u306e\u4e00\u90e8\u3092\u9078\u629e\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u305d\u306e\u5024\u306f\u9078\u629e\u3055\u308c\u305f\u91cd\u91cf\u306e\u5272\u5408\u306b\u57fa\u3065\u3044\u3066\u8a08\u7b97\u3055\u308c\u307e\u3059\u3002\u9650\u3089\u308c\u305f\u5bb9\u91cf\u306e\u4e0b\u3067\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5185\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u6700\u5927\u5024\u306f\u4f55\u3067\u3059\u304b\uff1f\u4f8b\u3092\u4e0b\u306e\u56f3\u306b\u793a\u3057\u307e\u3059\u3002

        \u56f3 15-3 \u00a0 \u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u4f8b\u30c7\u30fc\u30bf

        \u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306f\u5168\u4f53\u7684\u306b0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u3068\u975e\u5e38\u306b\u4f3c\u3066\u304a\u308a\u3001\u73fe\u5728\u306e\u30a2\u30a4\u30c6\u30e0 \\(i\\) \u3068\u5bb9\u91cf \\(c\\) \u3092\u542b\u307f\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u9650\u3089\u308c\u305f\u5bb9\u91cf\u5185\u3067\u5024\u3092\u6700\u5927\u5316\u3059\u308b\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u3066\u3044\u307e\u3059\u3002

        \u9055\u3044\u306f\u3001\u3053\u306e\u554f\u984c\u3067\u306f\u30a2\u30a4\u30c6\u30e0\u306e\u4e00\u90e8\u306e\u307f\u3092\u9078\u629e\u3067\u304d\u308b\u3053\u3068\u3067\u3059\u3002\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30a2\u30a4\u30c6\u30e0\u3092\u4efb\u610f\u306b\u5206\u5272\u3057\u3001\u91cd\u91cf\u306e\u5272\u5408\u306b\u57fa\u3065\u3044\u3066\u5bfe\u5fdc\u3059\u308b\u5024\u3092\u8a08\u7b97\u3067\u304d\u307e\u3059\u3002

        1. \u30a2\u30a4\u30c6\u30e0 \\(i\\) \u306b\u3064\u3044\u3066\u3001\u305d\u306e\u5358\u4f4d\u91cd\u91cf\u3042\u305f\u308a\u306e\u5024\u306f \\(val[i-1] / wgt[i-1]\\) \u3067\u3001\u5358\u4f4d\u5024\u3068\u547c\u3070\u308c\u307e\u3059\u3002
        2. \u91cd\u91cf \\(w\\) \u306e\u30a2\u30a4\u30c6\u30e0 \\(i\\) \u306e\u4e00\u90e8\u3092\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u308b\u3068\u3059\u308b\u3068\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u8ffd\u52a0\u3055\u308c\u308b\u5024\u306f \\(w \\times val[i-1] / wgt[i-1]\\) \u3067\u3059\u3002

        \u56f3 15-4 \u00a0 \u30a2\u30a4\u30c6\u30e0\u306e\u5358\u4f4d\u91cd\u91cf\u3042\u305f\u308a\u306e\u5024

        "},{"location":"chapter_greedy/fractional_knapsack_problem/#1","title":"1. \u00a0 \u8caa\u6b32\u6226\u7565\u306e\u6c7a\u5b9a","text":"

        \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u5185\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u7dcf\u5024\u3092\u6700\u5927\u5316\u3059\u308b\u3053\u3068\u306f\u3001\u672c\u8cea\u7684\u306b\u5358\u4f4d\u91cd\u91cf\u3042\u305f\u308a\u306e\u5024\u3092\u6700\u5927\u5316\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u3053\u308c\u304b\u3089\u3001\u4e0b\u306e\u56f3\u306b\u793a\u3059\u8caa\u6b32\u6226\u7565\u3092\u5c0e\u51fa\u3067\u304d\u307e\u3059\u3002

        1. \u30a2\u30a4\u30c6\u30e0\u3092\u5358\u4f4d\u5024\u306e\u9ad8\u3044\u9806\u304b\u3089\u4f4e\u3044\u9806\u306b\u30bd\u30fc\u30c8\u3057\u307e\u3059\u3002
        2. \u3059\u3079\u3066\u306e\u30a2\u30a4\u30c6\u30e0\u3092\u53cd\u5fa9\u3057\u3001**\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u6700\u3082\u9ad8\u3044\u5358\u4f4d\u5024\u3092\u6301\u3064\u30a2\u30a4\u30c6\u30e0\u3092\u8caa\u6b32\u306b\u9078\u629e**\u3057\u307e\u3059\u3002
        3. \u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306e\u6b8b\u308a\u5bb9\u91cf\u304c\u4e0d\u5341\u5206\u306a\u5834\u5408\u3001\u73fe\u5728\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u4e00\u90e8\u3092\u4f7f\u7528\u3057\u3066\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u3092\u6e80\u305f\u3057\u307e\u3059\u3002

        \u56f3 15-5 \u00a0 \u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u8caa\u6b32\u6226\u7565

        "},{"location":"chapter_greedy/fractional_knapsack_problem/#2","title":"2. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        \u30a2\u30a4\u30c6\u30e0\u3092\u5358\u4f4d\u5024\u3067\u30bd\u30fc\u30c8\u3059\u308b\u305f\u3081\u306b Item \u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3057\u307e\u3057\u305f\u3002\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u304c\u6e80\u305f\u3055\u308c\u308b\u307e\u3067\u30eb\u30fc\u30d7\u3057\u3066\u8caa\u6b32\u306a\u9078\u629e\u3092\u884c\u3044\u3001\u305d\u306e\u5f8c\u7d42\u4e86\u3057\u3066\u89e3\u3092\u8fd4\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby fractional_knapsack.py
        class Item:\n    \"\"\"\u30a2\u30a4\u30c6\u30e0\"\"\"\n\n    def __init__(self, w: int, v: int):\n        self.w = w  # \u30a2\u30a4\u30c6\u30e0\u306e\u91cd\u91cf\n        self.v = v  # \u30a2\u30a4\u30c6\u30e0\u306e\u4fa1\u5024\n\ndef fractional_knapsack(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"\u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u8caa\u6b32\u6cd5\"\"\"\n    # \u30a2\u30a4\u30c6\u30e0\u30ea\u30b9\u30c8\u3092\u4f5c\u6210\u30012 \u3064\u306e\u5c5e\u6027\u3092\u542b\u3080\uff1a\u91cd\u91cf\u3001\u4fa1\u5024\n    items = [Item(w, v) for w, v in zip(wgt, val)]\n    # \u5358\u4f4d\u4fa1\u5024 item.v / item.w \u3067\u9ad8\u3044\u9806\u306b\u30bd\u30fc\u30c8\n    items.sort(key=lambda item: item.v / item.w, reverse=True)\n    # \u8caa\u6b32\u9078\u629e\u3092\u30eb\u30fc\u30d7\n    res = 0\n    for item in items:\n        if item.w <= cap:\n            # \u6b8b\u308a\u5bb9\u91cf\u304c\u5341\u5206\u306a\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0\u5168\u4f53\u3092\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u308b\n            res += item.v\n            cap -= item.w\n        else:\n            # \u6b8b\u308a\u5bb9\u91cf\u304c\u4e0d\u5341\u5206\u306a\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0\u306e\u4e00\u90e8\u3092\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u308b\n            res += (item.v / item.w) * cap\n            # \u6b8b\u308a\u5bb9\u91cf\u304c\u306a\u304f\u306a\u3063\u305f\u305f\u3081\u3001\u30eb\u30fc\u30d7\u3092\u4e2d\u65ad\n            break\n    return res\n
        fractional_knapsack.cpp
        /* \u30a2\u30a4\u30c6\u30e0 */\nclass Item {\n  public:\n    int w; // \u30a2\u30a4\u30c6\u30e0\u306e\u91cd\u91cf\n    int v; // \u30a2\u30a4\u30c6\u30e0\u306e\u4fa1\u5024\n\n    Item(int w, int v) : w(w), v(v) {\n    }\n};\n\n/* \u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u8caa\u6b32\u6cd5 */\ndouble fractionalKnapsack(vector<int> &wgt, vector<int> &val, int cap) {\n    // \u30a2\u30a4\u30c6\u30e0\u30ea\u30b9\u30c8\u3092\u4f5c\u6210\u30012\u3064\u306e\u5c5e\u6027\u3092\u542b\u3080\uff1a\u91cd\u91cf\u3001\u4fa1\u5024\n    vector<Item> items;\n    for (int i = 0; i < wgt.size(); i++) {\n        items.push_back(Item(wgt[i], val[i]));\n    }\n    // \u5358\u4f4d\u4fa1\u5024 item.v / item.w \u3067\u9ad8\u3044\u9806\u306b\u30bd\u30fc\u30c8\n    sort(items.begin(), items.end(), [](Item &a, Item &b) { return (double)a.v / a.w > (double)b.v / b.w; });\n    // \u8caa\u6b32\u9078\u629e\u3092\u30eb\u30fc\u30d7\n    double res = 0;\n    for (auto &item : items) {\n        if (item.w <= cap) {\n            // \u6b8b\u308a\u5bb9\u91cf\u304c\u5341\u5206\u306a\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0\u5168\u4f53\u3092\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u308b\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // \u6b8b\u308a\u5bb9\u91cf\u304c\u4e0d\u5341\u5206\u306a\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0\u306e\u4e00\u90e8\u3092\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u308b\n            res += (double)item.v / item.w * cap;\n            // \u6b8b\u308a\u5bb9\u91cf\u304c\u306a\u304f\u306a\u3063\u305f\u305f\u3081\u3001\u30eb\u30fc\u30d7\u3092\u4e2d\u65ad\n            break;\n        }\n    }\n    return res;\n}\n
        fractional_knapsack.java
        /* \u30a2\u30a4\u30c6\u30e0 */\nclass Item {\n    int w; // \u30a2\u30a4\u30c6\u30e0\u306e\u91cd\u91cf\n    int v; // \u30a2\u30a4\u30c6\u30e0\u306e\u4fa1\u5024\n\n    public Item(int w, int v) {\n        this.w = w;\n        this.v = v;\n    }\n}\n\n/* \u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\uff1a\u8caa\u6b32\u6cd5 */\ndouble fractionalKnapsack(int[] wgt, int[] val, int cap) {\n    // \u30a2\u30a4\u30c6\u30e0\u30ea\u30b9\u30c8\u3092\u4f5c\u6210\u30012\u3064\u306e\u5c5e\u6027\u3092\u542b\u3080\uff1a\u91cd\u91cf\u3001\u4fa1\u5024\n    Item[] items = new Item[wgt.length];\n    for (int i = 0; i < wgt.length; i++) {\n        items[i] = new Item(wgt[i], val[i]);\n    }\n    // \u5358\u4f4d\u4fa1\u5024 item.v / item.w \u3067\u9ad8\u3044\u9806\u306b\u30bd\u30fc\u30c8\n    Arrays.sort(items, Comparator.comparingDouble(item -> -((double) item.v / item.w)));\n    // \u8caa\u6b32\u9078\u629e\u3092\u30eb\u30fc\u30d7\n    double res = 0;\n    for (Item item : items) {\n        if (item.w <= cap) {\n            // \u6b8b\u308a\u5bb9\u91cf\u304c\u5341\u5206\u306a\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0\u5168\u4f53\u3092\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u308b\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // \u6b8b\u308a\u5bb9\u91cf\u304c\u4e0d\u5341\u5206\u306a\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0\u306e\u4e00\u90e8\u3092\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u306b\u5165\u308c\u308b\n            res += (double) item.v / item.w * cap;\n            // \u6b8b\u308a\u5bb9\u91cf\u304c\u306a\u304f\u306a\u3063\u305f\u305f\u3081\u3001\u30eb\u30fc\u30d7\u3092\u4e2d\u65ad\n            break;\n        }\n    }\n    return res;\n}\n
        fractional_knapsack.cs
        [class]{Item}-[func]{}\n\n[class]{fractional_knapsack}-[func]{FractionalKnapsack}\n
        fractional_knapsack.go
        [class]{Item}-[func]{}\n\n[class]{}-[func]{fractionalKnapsack}\n
        fractional_knapsack.swift
        [class]{Item}-[func]{}\n\n[class]{}-[func]{fractionalKnapsack}\n
        fractional_knapsack.js
        [class]{Item}-[func]{}\n\n[class]{}-[func]{fractionalKnapsack}\n
        fractional_knapsack.ts
        [class]{Item}-[func]{}\n\n[class]{}-[func]{fractionalKnapsack}\n
        fractional_knapsack.dart
        [class]{Item}-[func]{}\n\n[class]{}-[func]{fractionalKnapsack}\n
        fractional_knapsack.rs
        [class]{Item}-[func]{}\n\n[class]{}-[func]{fractional_knapsack}\n
        fractional_knapsack.c
        [class]{Item}-[func]{}\n\n[class]{}-[func]{fractionalKnapsack}\n
        fractional_knapsack.kt
        [class]{Item}-[func]{}\n\n[class]{}-[func]{fractionalKnapsack}\n
        fractional_knapsack.rb
        [class]{Item}-[func]{}\n\n[class]{}-[func]{fractional_knapsack}\n

        \u30bd\u30fc\u30c8\u4ee5\u5916\u306b\u3001\u6700\u60aa\u306e\u5834\u5408\u3001\u30a2\u30a4\u30c6\u30e0\u306e\u30ea\u30b9\u30c8\u5168\u4f53\u3092\u8d70\u67fb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u3067\u3059\u3002\u3053\u3053\u3067 \\(n\\) \u306f\u30a2\u30a4\u30c6\u30e0\u306e\u6570\u3067\u3059\u3002

        Item \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30ea\u30b9\u30c8\u304c\u521d\u671f\u5316\u3055\u308c\u308b\u305f\u3081\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u3067\u3059\u3002

        "},{"location":"chapter_greedy/fractional_knapsack_problem/#3","title":"3. \u00a0 \u6b63\u3057\u3055\u306e\u8a3c\u660e","text":"

        \u80cc\u7406\u6cd5\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u30a2\u30a4\u30c6\u30e0 \\(x\\) \u304c\u6700\u9ad8\u306e\u5358\u4f4d\u5024\u3092\u6301\u3061\u3001\u3042\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u6700\u5927\u5024 res \u3092\u751f\u6210\u3059\u308b\u304c\u3001\u89e3\u306b\u30a2\u30a4\u30c6\u30e0 \\(x\\) \u304c\u542b\u307e\u308c\u3066\u3044\u306a\u3044\u3068\u4eee\u5b9a\u3057\u307e\u3059\u3002

        \u4eca\u3001\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u304b\u3089\u4efb\u610f\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u5358\u4f4d\u91cd\u91cf\u3092\u53d6\u308a\u9664\u304d\u3001\u30a2\u30a4\u30c6\u30e0 \\(x\\) \u306e\u5358\u4f4d\u91cd\u91cf\u3067\u7f6e\u304d\u63db\u3048\u307e\u3059\u3002\u30a2\u30a4\u30c6\u30e0 \\(x\\) \u306e\u5358\u4f4d\u5024\u304c\u6700\u9ad8\u3067\u3042\u308b\u305f\u3081\u3001\u7f6e\u304d\u63db\u3048\u5f8c\u306e\u7dcf\u5024\u306f\u78ba\u5b9f\u306b res \u3088\u308a\u5927\u304d\u304f\u306a\u308a\u307e\u3059\u3002\u3053\u308c\u306f res \u304c\u6700\u9069\u89e3\u3067\u3042\u308b\u3068\u3044\u3046\u4eee\u5b9a\u3068\u77db\u76fe\u3057\u3001\u6700\u9069\u89e3\u306b\u306f\u5fc5\u305a\u30a2\u30a4\u30c6\u30e0 \\(x\\) \u304c\u542b\u307e\u308c\u308b\u3053\u3068\u3092\u8a3c\u660e\u3057\u307e\u3059\u3002

        \u3053\u306e\u89e3\u306e\u4ed6\u306e\u30a2\u30a4\u30c6\u30e0\u306b\u3064\u3044\u3066\u3082\u3001\u4e0a\u8a18\u306e\u77db\u76fe\u3092\u69cb\u7bc9\u3067\u304d\u307e\u3059\u3002\u5168\u4f53\u7684\u306b\u3001**\u5358\u4f4d\u5024\u304c\u3088\u308a\u5927\u304d\u3044\u30a2\u30a4\u30c6\u30e0\u306f\u5e38\u306b\u3088\u308a\u826f\u3044\u9078\u629e**\u3067\u3042\u308a\u3001\u8caa\u6b32\u6226\u7565\u304c\u52b9\u679c\u7684\u3067\u3042\u308b\u3053\u3068\u3092\u8a3c\u660e\u3057\u307e\u3059\u3002

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30a2\u30a4\u30c6\u30e0\u306e\u91cd\u91cf\u3068\u5358\u4f4d\u5024\u3092\u305d\u308c\u305e\u308c\u4e8c\u6b21\u5143\u30c1\u30e3\u30fc\u30c8\u306e\u6a2a\u8ef8\u3068\u7e26\u8ef8\u3068\u898b\u306a\u3059\u3068\u3001\u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306f\u300c\u9650\u3089\u308c\u305f\u6a2a\u8ef8\u7bc4\u56f2\u5185\u3067\u56f2\u307e\u308c\u308b\u6700\u5927\u9762\u7a4d\u3092\u6c42\u3081\u308b\u300d\u3053\u3068\u306b\u5909\u63db\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u985e\u63a8\u306f\u3001\u5e7e\u4f55\u5b66\u7684\u89b3\u70b9\u304b\u3089\u8caa\u6b32\u6226\u7565\u306e\u52b9\u679c\u3092\u7406\u89e3\u3059\u308b\u306e\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002

        \u56f3 15-6 \u00a0 \u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306e\u5e7e\u4f55\u5b66\u7684\u8868\u73fe

        "},{"location":"chapter_greedy/greedy_algorithm/","title":"15.1 \u00a0 \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0","text":"

        \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u6700\u9069\u5316\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306e\u4e00\u822c\u7684\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3001\u57fa\u672c\u7684\u306b\u554f\u984c\u306e\u5404\u610f\u601d\u6c7a\u5b9a\u6bb5\u968e\u3067\u6700\u3082\u826f\u3044\u9078\u629e\u3092\u3059\u308b\u3053\u3068\u3001\u3064\u307e\u308a\u5c40\u6240\u7684\u306b\u6700\u9069\u306a\u6c7a\u5b9a\u3092\u8caa\u6b32\u306b\u884c\u3044\u3001\u30b0\u30ed\u30fc\u30d0\u30eb\u306b\u6700\u9069\u306a\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3092\u671b\u307f\u307e\u3059\u3002\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u7c21\u6f54\u3067\u52b9\u7387\u7684\u3067\u3042\u308a\u3001\u591a\u304f\u306e\u5b9f\u7528\u7684\u306a\u554f\u984c\u3067\u5e83\u304f\u4f7f\u7528\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u3001\u3069\u3061\u3089\u3082\u6700\u9069\u5316\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3088\u304f\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u4e21\u8005\u306f\u6700\u9069\u90e8\u5206\u69cb\u9020\u306e\u6027\u8cea\u306b\u4f9d\u5b58\u3059\u308b\u306a\u3069\u3001\u3044\u304f\u3064\u304b\u306e\u985e\u4f3c\u70b9\u3092\u5171\u6709\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u52d5\u4f5c\u65b9\u6cd5\u304c\u7570\u306a\u308a\u307e\u3059\u3002

        • \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u73fe\u5728\u306e\u6c7a\u5b9a\u6bb5\u968e\u3067\u3059\u3079\u3066\u306e\u4ee5\u524d\u306e\u6c7a\u5b9a\u3092\u8003\u616e\u3057\u3001\u904e\u53bb\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u4f7f\u7528\u3057\u3066\u73fe\u5728\u306e\u90e8\u5206\u554f\u984c\u306e\u89e3\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002
        • \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u904e\u53bb\u306e\u6c7a\u5b9a\u3092\u8003\u616e\u305b\u305a\u3001\u4ee3\u308f\u308a\u306b\u8caa\u6b32\u306a\u9078\u629e\u3092\u7d9a\u3051\u3001\u554f\u984c\u304c\u89e3\u6c7a\u3055\u308c\u308b\u307e\u3067\u554f\u984c\u306e\u7bc4\u56f2\u3092\u7d99\u7d9a\u7684\u306b\u72ed\u3081\u307e\u3059\u3002

        \u307e\u305a\u3001\u300c\u5b8c\u5168\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u300d\u306e\u7ae0\u3067\u7d39\u4ecb\u3055\u308c\u305f\u300c\u30b3\u30a4\u30f3\u4ea4\u63db\u300d\u306e\u4f8b\u3092\u901a\u3058\u3066\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52d5\u4f5c\u539f\u7406\u3092\u7406\u89e3\u3057\u307e\u3057\u3087\u3046\u3002\u3059\u3067\u306b\u3088\u304f\u77e5\u3063\u3066\u3044\u308b\u3068\u601d\u3044\u307e\u3059\u3002

        Question

        \\(n\\) \u7a2e\u985e\u306e\u30b3\u30a4\u30f3\u304c\u4e0e\u3048\u3089\u308c\u3001\\(i\\) \u756a\u76ee\u306e\u7a2e\u985e\u306e\u30b3\u30a4\u30f3\u306e\u984d\u9762\u306f \\(coins[i - 1]\\) \u3067\u3001\u76ee\u6a19\u91d1\u984d\u306f \\(amt\\) \u3067\u3059\u3002\u5404\u7a2e\u985e\u306e\u30b3\u30a4\u30f3\u306f\u7121\u5236\u9650\u306b\u5229\u7528\u53ef\u80fd\u3067\u3001\u76ee\u6a19\u91d1\u984d\u3092\u69cb\u6210\u3059\u308b\u306e\u306b\u5fc5\u8981\u306a\u6700\u5c0f\u30b3\u30a4\u30f3\u6570\u306f\u4f55\u3067\u3059\u304b\uff1f\u76ee\u6a19\u91d1\u984d\u3092\u69cb\u6210\u3067\u304d\u306a\u3044\u5834\u5408\u306f \\(-1\\) \u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u3053\u306e\u554f\u984c\u3067\u63a1\u7528\u3055\u308c\u308b\u8caa\u6b32\u6226\u7565\u3092\u4e0b\u306e\u56f3\u306b\u793a\u3057\u307e\u3059\u3002\u76ee\u6a19\u91d1\u984d\u304c\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001**\u305d\u308c\u306b\u6700\u3082\u8fd1\u304f\u3001\u305d\u308c\u3092\u8d85\u3048\u306a\u3044\u30b3\u30a4\u30f3\u3092\u8caa\u6b32\u306b\u9078\u629e**\u3057\u3001\u76ee\u6a19\u91d1\u984d\u304c\u6e80\u305f\u3055\u308c\u308b\u307e\u3067\u3053\u306e\u30b9\u30c6\u30c3\u30d7\u3092\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002

        \u56f3 15-1 \u00a0 \u30b3\u30a4\u30f3\u4ea4\u63db\u306e\u8caa\u6b32\u6226\u7565

        \u5b9f\u88c5\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change_greedy.py
        def coin_change_greedy(coins: list[int], amt: int) -> int:\n    \"\"\"\u786c\u8ca8\u4ea4\u63db\uff1a\u8caa\u6b32\u6cd5\"\"\"\n    # coins \u30ea\u30b9\u30c8\u304c\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u308b\u3068\u4eee\u5b9a\n    i = len(coins) - 1\n    count = 0\n    # \u6b8b\u308a\u91d1\u984d\u304c\u306a\u304f\u306a\u308b\u307e\u3067\u8caa\u6b32\u9078\u629e\u3092\u30eb\u30fc\u30d7\n    while amt > 0:\n        # \u6b8b\u308a\u91d1\u984d\u306b\u6700\u3082\u8fd1\u304f\u3001\u305d\u308c\u3088\u308a\u5c0f\u3055\u3044\u786c\u8ca8\u3092\u898b\u3064\u3051\u308b\n        while i > 0 and coins[i] > amt:\n            i -= 1\n        # coins[i] \u3092\u9078\u629e\n        amt -= coins[i]\n        count += 1\n    # \u5b9f\u884c\u53ef\u80fd\u306a\u89e3\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3001-1 \u3092\u8fd4\u3059\n    return count if amt == 0 else -1\n
        coin_change_greedy.cpp
        /* \u786c\u8ca8\u4e21\u66ff\uff1a\u8caa\u6b32\u6cd5 */\nint coinChangeGreedy(vector<int> &coins, int amt) {\n    // \u786c\u8ca8\u30ea\u30b9\u30c8\u304c\u9806\u5e8f\u4ed8\u3051\u3055\u308c\u3066\u3044\u308b\u3068\u4eee\u5b9a\n    int i = coins.size() - 1;\n    int count = 0;\n    // \u6b8b\u308a\u91d1\u984d\u304c\u306a\u304f\u306a\u308b\u307e\u3067\u8caa\u6b32\u9078\u629e\u3092\u30eb\u30fc\u30d7\n    while (amt > 0) {\n        // \u6b8b\u308a\u91d1\u984d\u306b\u8fd1\u304f\u3001\u305d\u308c\u4ee5\u4e0b\u306e\u6700\u5c0f\u786c\u8ca8\u3092\u898b\u3064\u3051\u308b\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // coins[i] \u3092\u9078\u629e\n        amt -= coins[i];\n        count++;\n    }\n    // \u5b9f\u884c\u53ef\u80fd\u306a\u89e3\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3001-1 \u3092\u8fd4\u3059\n    return amt == 0 ? count : -1;\n}\n
        coin_change_greedy.java
        /* \u786c\u8ca8\u4e21\u66ff\uff1a\u8caa\u6b32\u6cd5 */\nint coinChangeGreedy(int[] coins, int amt) {\n    // \u786c\u8ca8\u30ea\u30b9\u30c8\u304c\u9806\u5e8f\u4ed8\u3051\u3055\u308c\u3066\u3044\u308b\u3068\u4eee\u5b9a\n    int i = coins.length - 1;\n    int count = 0;\n    // \u6b8b\u308a\u91d1\u984d\u304c\u306a\u304f\u306a\u308b\u307e\u3067\u8caa\u6b32\u9078\u629e\u3092\u30eb\u30fc\u30d7\n    while (amt > 0) {\n        // \u6b8b\u308a\u91d1\u984d\u306b\u8fd1\u304f\u3001\u305d\u308c\u4ee5\u4e0b\u306e\u6700\u5c0f\u786c\u8ca8\u3092\u898b\u3064\u3051\u308b\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // coins[i] \u3092\u9078\u629e\n        amt -= coins[i];\n        count++;\n    }\n    // \u5b9f\u884c\u53ef\u80fd\u306a\u89e3\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3001-1 \u3092\u8fd4\u3059\n    return amt == 0 ? count : -1;\n}\n
        coin_change_greedy.cs
        [class]{coin_change_greedy}-[func]{CoinChangeGreedy}\n
        coin_change_greedy.go
        [class]{}-[func]{coinChangeGreedy}\n
        coin_change_greedy.swift
        [class]{}-[func]{coinChangeGreedy}\n
        coin_change_greedy.js
        [class]{}-[func]{coinChangeGreedy}\n
        coin_change_greedy.ts
        [class]{}-[func]{coinChangeGreedy}\n
        coin_change_greedy.dart
        [class]{}-[func]{coinChangeGreedy}\n
        coin_change_greedy.rs
        [class]{}-[func]{coin_change_greedy}\n
        coin_change_greedy.c
        [class]{}-[func]{coinChangeGreedy}\n
        coin_change_greedy.kt
        [class]{}-[func]{coinChangeGreedy}\n
        coin_change_greedy.rb
        [class]{}-[func]{coin_change_greedy}\n

        \u611f\u5606\u3059\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\uff1a\u306a\u3093\u3066\u7c21\u6f54\u306a\u3093\u3060\uff01\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u7d0410\u884c\u306e\u30b3\u30fc\u30c9\u3067\u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u3092\u89e3\u6c7a\u3057\u307e\u3059\u3002

        "},{"location":"chapter_greedy/greedy_algorithm/#1511","title":"15.1.1 \u00a0 \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5229\u70b9\u3068\u5236\u9650","text":"

        \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u76f4\u63a5\u7684\u3067\u5b9f\u88c5\u304c\u7c21\u5358\u3067\u3042\u308b\u3060\u3051\u3067\u306a\u304f\u3001\u901a\u5e38\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3082\u3042\u308a\u307e\u3059\u3002\u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u3067\u3001\u6700\u5c0f\u306e\u30b3\u30a4\u30f3\u984d\u9762\u3092 \\(\\min(coins)\\) \u3068\u3059\u308b\u3068\u3001\u8caa\u6b32\u306a\u9078\u629e\u30eb\u30fc\u30d7\u306f\u6700\u5927 \\(amt / \\min(coins)\\) \u56de\u5b9f\u884c\u3055\u308c\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(amt / \\min(coins))\\) \u306b\u306a\u308a\u307e\u3059\u3002\u3053\u308c\u306f\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u89e3\u6cd5\u306e\u6642\u9593\u8a08\u7b97\u91cf \\(O(n \\times amt)\\) \u3088\u308a\u3082\u4e00\u6841\u5c0f\u3055\u3044\u3067\u3059\u3002

        \u3057\u304b\u3057\u3001\u4e00\u90e8\u306e\u30b3\u30a4\u30f3\u984d\u9762\u306e\u7d44\u307f\u5408\u308f\u305b\u3067\u306f\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u6700\u9069\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u305b\u3093\u3002\u4e0b\u306e\u56f3\u306f2\u3064\u306e\u4f8b\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        • \u6b63\u306e\u4f8b \\(coins = [1, 5, 10, 20, 50, 100]\\)\uff1a\u3053\u306e\u30b3\u30a4\u30f3\u306e\u7d44\u307f\u5408\u308f\u305b\u3067\u306f\u3001\u4efb\u610f\u306e \\(amt\\) \u306b\u5bfe\u3057\u3066\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u6700\u9069\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
        • \u8ca0\u306e\u4f8b \\(coins = [1, 20, 50]\\)\uff1a\\(amt = 60\\) \u3068\u3059\u308b\u3068\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u7d44\u307f\u5408\u308f\u305b \\(50 + 1 \\times 10\\) \u3057\u304b\u898b\u3064\u3051\u3089\u308c\u305a\u3001\u5408\u8a0811\u679a\u306e\u30b3\u30a4\u30f3\u3067\u3059\u304c\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u6700\u9069\u89e3 \\(20 + 20 + 20\\) \u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u30013\u679a\u306e\u30b3\u30a4\u30f3\u306e\u307f\u304c\u5fc5\u8981\u3067\u3059\u3002
        • \u8ca0\u306e\u4f8b \\(coins = [1, 49, 50]\\)\uff1a\\(amt = 98\\) \u3068\u3059\u308b\u3068\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u7d44\u307f\u5408\u308f\u305b \\(50 + 1 \\times 48\\) \u3057\u304b\u898b\u3064\u3051\u3089\u308c\u305a\u3001\u5408\u8a0849\u679a\u306e\u30b3\u30a4\u30f3\u3067\u3059\u304c\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306f\u6700\u9069\u89e3 \\(49 + 49\\) \u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u30012\u679a\u306e\u30b3\u30a4\u30f3\u306e\u307f\u304c\u5fc5\u8981\u3067\u3059\u3002

        \u56f3 15-2 \u00a0 \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u6700\u9069\u89e3\u3092\u898b\u3064\u3051\u3089\u308c\u306a\u3044\u4f8b

        \u3053\u308c\u306f\u3001\u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u306b\u304a\u3044\u3066\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u30b0\u30ed\u30fc\u30d0\u30eb\u306b\u6700\u9069\u306a\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3067\u304d\u305a\u3001\u975e\u5e38\u306b\u60aa\u3044\u89e3\u3092\u898b\u3064\u3051\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306e\u65b9\u304c\u9069\u3057\u3066\u3044\u307e\u3059\u3002

        \u4e00\u822c\u7684\u306b\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u9069\u7528\u6027\u306f2\u3064\u306e\u30ab\u30c6\u30b4\u30ea\u306b\u5206\u985e\u3055\u308c\u307e\u3059\u3002

        1. \u6700\u9069\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u4fdd\u8a3c\u3055\u308c\u308b\uff1a\u3053\u308c\u3089\u306e\u5834\u5408\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3057\u3070\u3057\u3070\u6700\u826f\u306e\u9078\u629e\u3067\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3084\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3088\u308a\u3082\u52b9\u7387\u7684\u3067\u3042\u308b\u50be\u5411\u304c\u3042\u308a\u307e\u3059\u3002
        2. \u6e96\u6700\u9069\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u308b\uff1a\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3053\u3053\u3067\u3082\u9069\u7528\u53ef\u80fd\u3067\u3059\u3002\u591a\u304f\u306e\u8907\u96d1\u306a\u554f\u984c\u3067\u306f\u3001\u30b0\u30ed\u30fc\u30d0\u30eb\u6700\u9069\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u306f\u975e\u5e38\u306b\u56f0\u96e3\u3067\u3042\u308a\u3001\u9ad8\u52b9\u7387\u306e\u6e96\u6700\u9069\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3082\u975e\u5e38\u306b\u4fa1\u5024\u304c\u3042\u308a\u307e\u3059\u3002
        "},{"location":"chapter_greedy/greedy_algorithm/#1512","title":"15.1.2 \u00a0 \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7279\u5fb4","text":"

        \u305d\u308c\u3067\u306f\u3001\u3069\u306e\u3088\u3046\u306a\u554f\u984c\u304c\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u89e3\u6c7a\u3059\u308b\u306e\u306b\u9069\u3057\u3066\u3044\u308b\u306e\u3067\u3057\u3087\u3046\u304b\uff1f\u8a00\u3044\u63db\u3048\u308c\u3070\u3001\u3069\u306e\u3088\u3046\u306a\u6761\u4ef6\u4e0b\u3067\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u6700\u9069\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3067\u304d\u308b\u306e\u3067\u3057\u3087\u3046\u304b\uff1f

        \u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3068\u6bd4\u8f03\u3057\u3066\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3088\u308a\u53b3\u3057\u3044\u4f7f\u7528\u6761\u4ef6\u3092\u6301\u3061\u3001\u4e3b\u306b\u554f\u984c\u306e2\u3064\u306e\u6027\u8cea\u306b\u7126\u70b9\u3092\u5f53\u3066\u3066\u3044\u307e\u3059\u3002

        • \u8caa\u6b32\u9078\u629e\u6027\uff1a\u5c40\u6240\u7684\u306b\u6700\u9069\u306a\u9078\u629e\u304c\u5e38\u306b\u30b0\u30ed\u30fc\u30d0\u30eb\u306b\u6700\u9069\u306a\u89e3\u306b\u5c0e\u304f\u3053\u3068\u304c\u3067\u304d\u308b\u5834\u5408\u306e\u307f\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u6700\u9069\u89e3\u3092\u5f97\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3067\u304d\u307e\u3059\u3002
        • \u6700\u9069\u90e8\u5206\u69cb\u9020\uff1a\u5143\u306e\u554f\u984c\u306e\u6700\u9069\u89e3\u306f\u305d\u306e\u90e8\u5206\u554f\u984c\u306e\u6700\u9069\u89e3\u3092\u542b\u307f\u307e\u3059\u3002

        \u6700\u9069\u90e8\u5206\u69cb\u9020\u306f\u300c\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u300d\u306e\u7ae0\u3067\u3059\u3067\u306b\u7d39\u4ecb\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u3053\u3053\u3067\u306f\u3053\u308c\u4ee5\u4e0a\u8b70\u8ad6\u3057\u307e\u305b\u3093\u3002\u4e00\u90e8\u306e\u554f\u984c\u306b\u306f\u660e\u3089\u304b\u306a\u6700\u9069\u90e8\u5206\u69cb\u9020\u304c\u3042\u308a\u307e\u305b\u3093\u304c\u3001\u305d\u308c\u3067\u3082\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u4f7f\u7528\u3057\u3066\u89e3\u6c7a\u3067\u304d\u308b\u3053\u3068\u306b\u6ce8\u610f\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002

        \u4e3b\u306b\u8caa\u6b32\u9078\u629e\u6027\u3092\u6c7a\u5b9a\u3059\u308b\u65b9\u6cd5\u3092\u63a2\u7d22\u3057\u307e\u3059\u3002\u305d\u306e\u8a18\u8ff0\u306f\u5358\u7d14\u306b\u898b\u3048\u307e\u3059\u304c\u3001\u5b9f\u969b\u306b\u306f\u3001\u591a\u304f\u306e\u554f\u984c\u306e\u8caa\u6b32\u9078\u629e\u6027\u3092\u8a3c\u660e\u3059\u308b\u3053\u3068\u306f\u5bb9\u6613\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002

        \u4f8b\u3048\u3070\u3001\u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u3067\u306f\u3001\u8caa\u6b32\u9078\u629e\u6027\u3092\u53cd\u8a3c\u3059\u308b\u305f\u3081\u306b\u53cd\u4f8b\u3092\u7c21\u5358\u306b\u6319\u3052\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u304c\u3001\u305d\u308c\u3092\u8a3c\u660e\u3059\u308b\u3053\u3068\u306f\u306f\u308b\u304b\u306b\u56f0\u96e3\u3067\u3059\u3002**\u30b3\u30a4\u30f3\u306e\u7d44\u307f\u5408\u308f\u305b\u304c\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u4f7f\u7528\u3057\u3066\u89e3\u6c7a\u3067\u304d\u308b\u305f\u3081\u306b\u306f\u3001\u3069\u306e\u3088\u3046\u306a\u6761\u4ef6\u3092\u6e80\u305f\u3059\u5fc5\u8981\u304c\u3042\u308b\u304b**\u3068\u5c0b\u306d\u3089\u308c\u305f\u5834\u5408\u3001\u53b3\u5bc6\u306a\u6570\u5b66\u7684\u8a3c\u660e\u3092\u63d0\u4f9b\u3059\u308b\u3053\u3068\u304c\u56f0\u96e3\u3067\u3042\u308b\u305f\u3081\u3001\u3057\u3070\u3057\u3070\u76f4\u611f\u3084\u4f8b\u306b\u983c\u3063\u3066\u66d6\u6627\u306a\u7b54\u3048\u3092\u63d0\u4f9b\u3057\u306a\u3051\u308c\u3070\u306a\u308a\u307e\u305b\u3093\u3002

        Quote

        \u3042\u308b\u8ad6\u6587\u3067\u306f\u3001\u30b3\u30a4\u30f3\u306e\u7d44\u307f\u5408\u308f\u305b\u304c\u4efb\u610f\u306e\u91d1\u984d\u306b\u5bfe\u3057\u3066\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u4f7f\u7528\u3057\u3066\u6700\u9069\u89e3\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\u3059\u308b\u305f\u3081\u306e\u6642\u9593\u8a08\u7b97\u91cf \\(O(n^3)\\) \u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u63d0\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        Pearson, D. A polynomial-time algorithm for the change-making problem[J]. Operations Research Letters, 2005, 33(3): 231-234.

        "},{"location":"chapter_greedy/greedy_algorithm/#1513","title":"15.1.3 \u00a0 \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u3088\u308b\u554f\u984c\u89e3\u6c7a\u306e\u30b9\u30c6\u30c3\u30d7","text":"

        \u8caa\u6b32\u554f\u984c\u306e\u554f\u984c\u89e3\u6c7a\u30d7\u30ed\u30bb\u30b9\u306f\u3001\u4e00\u822c\u7684\u306b\u4ee5\u4e0b\u306e3\u3064\u306e\u30b9\u30c6\u30c3\u30d7\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        1. \u554f\u984c\u5206\u6790\uff1a\u554f\u984c\u306e\u7279\u5fb4\u3092\u6574\u7406\u3057\u7406\u89e3\u3059\u308b\u3002\u72b6\u614b\u5b9a\u7fa9\u3001\u6700\u9069\u5316\u76ee\u6a19\u3001\u5236\u7d04\u306a\u3069\u3092\u542b\u307f\u307e\u3059\u3002\u3053\u306e\u30b9\u30c6\u30c3\u30d7\u306f\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3084\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3067\u3082\u95a2\u4e0e\u3057\u307e\u3059\u3002
        2. \u8caa\u6b32\u6226\u7565\u306e\u6c7a\u5b9a\uff1a\u5404\u30b9\u30c6\u30c3\u30d7\u3067\u8caa\u6b32\u306a\u9078\u629e\u3092\u3059\u308b\u65b9\u6cd5\u3092\u6c7a\u5b9a\u3059\u308b\u3002\u3053\u306e\u6226\u7565\u306f\u5404\u30b9\u30c6\u30c3\u30d7\u3067\u554f\u984c\u306e\u898f\u6a21\u3092\u7e2e\u5c0f\u3057\u3001\u6700\u7d42\u7684\u306b\u554f\u984c\u5168\u4f53\u3092\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002
        3. \u6b63\u78ba\u6027\u306e\u8a3c\u660e\uff1a\u901a\u5e38\u3001\u554f\u984c\u304c\u8caa\u6b32\u9078\u629e\u6027\u3068\u6700\u9069\u90e8\u5206\u69cb\u9020\u306e\u4e21\u65b9\u3092\u6301\u3064\u3053\u3068\u3092\u8a3c\u660e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u30b9\u30c6\u30c3\u30d7\u306b\u306f\u3001\u5e30\u7d0d\u6cd5\u3084\u80cc\u7406\u6cd5\u306a\u3069\u306e\u6570\u5b66\u7684\u8a3c\u660e\u304c\u5fc5\u8981\u306a\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002

        \u8caa\u6b32\u6226\u7565\u306e\u6c7a\u5b9a\u306f\u554f\u984c\u89e3\u6c7a\u306e\u6838\u5fc3\u30b9\u30c6\u30c3\u30d7\u3067\u3059\u304c\u3001\u5b9f\u88c5\u306f\u5bb9\u6613\u3067\u306f\u306a\u3044\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u4e3b\u306a\u7406\u7531\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002

        • \u7570\u306a\u308b\u554f\u984c\u9593\u3067\u8caa\u6b32\u6226\u7565\u306f\u5927\u304d\u304f\u7570\u306a\u308b\u3002\u591a\u304f\u306e\u554f\u984c\u3067\u306f\u3001\u8caa\u6b32\u6226\u7565\u306f\u304b\u306a\u308a\u76f4\u63a5\u7684\u3067\u3001\u4e00\u822c\u7684\u306a\u601d\u8003\u3068\u8a66\u884c\u3092\u901a\u3058\u3066\u601d\u3044\u3064\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u4e00\u90e8\u306e\u8907\u96d1\u306a\u554f\u984c\u3067\u306f\u3001\u8caa\u6b32\u6226\u7565\u306f\u975e\u5e38\u306b\u898b\u3064\u3051\u306b\u304f\u304f\u3001\u3053\u308c\u306f\u500b\u4eba\u306e\u554f\u984c\u89e3\u6c7a\u7d4c\u9a13\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u80fd\u529b\u306e\u771f\u306e\u30c6\u30b9\u30c8\u3067\u3059\u3002
        • \u4e00\u90e8\u306e\u8caa\u6b32\u6226\u7565\u306f\u975e\u5e38\u306b\u8aa4\u89e3\u3092\u62db\u304f\u3002\u81ea\u4fe1\u3092\u6301\u3063\u3066\u8caa\u6b32\u6226\u7565\u3092\u8a2d\u8a08\u3057\u3001\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u30c6\u30b9\u30c8\u306b\u63d0\u51fa\u3057\u305f\u3068\u304d\u3001\u4e00\u90e8\u306e\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u304c\u901a\u3089\u306a\u3044\u53ef\u80fd\u6027\u304c\u9ad8\u3044\u3067\u3059\u3002\u3053\u308c\u306f\u8a2d\u8a08\u3055\u308c\u305f\u8caa\u6b32\u6226\u7565\u304c\u300c\u90e8\u5206\u7684\u306b\u6b63\u3057\u3044\u300d\u3060\u3051\u3067\u3042\u308b\u305f\u3081\u3067\u3001\u4e0a\u8a18\u306e\u30b3\u30a4\u30f3\u4ea4\u63db\u306e\u4f8b\u3067\u8aac\u660e\u3057\u305f\u901a\u308a\u3067\u3059\u3002

        \u6b63\u78ba\u6027\u3092\u78ba\u4fdd\u3059\u308b\u305f\u3081\u306b\u3001\u8caa\u6b32\u6226\u7565\u306b\u5bfe\u3057\u3066\u53b3\u5bc6\u306a\u6570\u5b66\u7684\u8a3c\u660e\u3092\u63d0\u4f9b\u3059\u3079\u304d\u3067\u3001\u901a\u5e38\u306f\u80cc\u7406\u6cd5\u3084\u6570\u5b66\u7684\u5e30\u7d0d\u6cd5\u3092\u542b\u307f\u307e\u3059\u3002

        \u3057\u304b\u3057\u3001\u6b63\u78ba\u6027\u3092\u8a3c\u660e\u3059\u308b\u3053\u3068\u306f\u5bb9\u6613\u306a\u4f5c\u696d\u3067\u306f\u306a\u3044\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u9014\u65b9\u306b\u66ae\u308c\u305f\u5834\u5408\u3001\u901a\u5e38\u306f\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u306b\u57fa\u3065\u3044\u3066\u30b3\u30fc\u30c9\u3092\u30c7\u30d0\u30c3\u30b0\u3057\u3001\u8caa\u6b32\u6226\u7565\u3092\u6bb5\u968e\u7684\u306b\u4fee\u6b63\u3057\u691c\u8a3c\u3059\u308b\u3053\u3068\u3092\u9078\u629e\u3057\u307e\u3059\u3002

        "},{"location":"chapter_greedy/greedy_algorithm/#1514","title":"15.1.4 \u00a0 \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u89e3\u6c7a\u3055\u308c\u308b\u5178\u578b\u7684\u306a\u554f\u984c","text":"

        \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u8caa\u6b32\u9078\u629e\u3068\u6700\u9069\u90e8\u5206\u69cb\u9020\u306e\u6027\u8cea\u3092\u6e80\u305f\u3059\u6700\u9069\u5316\u554f\u984c\u306b\u3088\u304f\u9069\u7528\u3055\u308c\u307e\u3059\u3002\u4ee5\u4e0b\u306f\u5178\u578b\u7684\u306a\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u306e\u3044\u304f\u3064\u304b\u3067\u3059\u3002

        • \u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\uff1a\u4e00\u90e8\u306e\u30b3\u30a4\u30f3\u306e\u7d44\u307f\u5408\u308f\u305b\u3067\u306f\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u5e38\u306b\u6700\u9069\u89e3\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002
        • \u533a\u9593\u30b9\u30b1\u30b8\u30e5\u30fc\u30ea\u30f3\u30b0\u554f\u984c\uff1a\u3044\u304f\u3064\u304b\u306e\u30bf\u30b9\u30af\u304c\u3042\u308a\u3001\u305d\u308c\u305e\u308c\u304c\u4e00\u5b9a\u671f\u9593\u306b\u308f\u305f\u3063\u3066\u884c\u308f\u308c\u308b\u3068\u3057\u307e\u3059\u3002\u76ee\u6a19\u306f\u3067\u304d\u308b\u3060\u3051\u591a\u304f\u306e\u30bf\u30b9\u30af\u3092\u5b8c\u4e86\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u5e38\u306b\u6700\u3082\u65e9\u304f\u7d42\u4e86\u3059\u308b\u30bf\u30b9\u30af\u3092\u9078\u629e\u3059\u308b\u3068\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u6700\u9069\u89e3\u3092\u9054\u6210\u3067\u304d\u307e\u3059\u3002
        • \u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\uff1a\u30a2\u30a4\u30c6\u30e0\u306e\u30bb\u30c3\u30c8\u3068\u904b\u642c\u5bb9\u91cf\u304c\u4e0e\u3048\u3089\u308c\u3001\u76ee\u6a19\u306f\u7dcf\u91cd\u91cf\u304c\u904b\u642c\u5bb9\u91cf\u3092\u8d85\u3048\u305a\u3001\u7dcf\u4fa1\u5024\u304c\u6700\u5927\u5316\u3055\u308c\u308b\u3088\u3046\u306a\u30a2\u30a4\u30c6\u30e0\u306e\u30bb\u30c3\u30c8\u3092\u9078\u629e\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u5e38\u306b\u6700\u9ad8\u306e\u4fa1\u5024\u5bfe\u91cd\u91cf\u6bd4\uff08\u4fa1\u5024/\u91cd\u91cf\uff09\u306e\u30a2\u30a4\u30c6\u30e0\u3092\u9078\u629e\u3059\u308b\u3068\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u4e00\u90e8\u306e\u30b1\u30fc\u30b9\u3067\u6700\u9069\u89e3\u3092\u9054\u6210\u3067\u304d\u307e\u3059\u3002
        • \u682a\u5f0f\u53d6\u5f15\u554f\u984c\uff1a\u682a\u4fa1\u306e\u5c65\u6b74\u306e\u30bb\u30c3\u30c8\u304c\u4e0e\u3048\u3089\u308c\u3001\u8907\u6570\u56de\u306e\u53d6\u5f15\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u304c\u3001\u3059\u3067\u306b\u682a\u5f0f\u3092\u6240\u6709\u3057\u3066\u3044\u308b\u5834\u5408\u306f\u58f2\u5374\u5f8c\u3067\u306a\u3044\u3068\u518d\u5ea6\u8cfc\u5165\u3067\u304d\u307e\u305b\u3093\u3002\u76ee\u6a19\u306f\u6700\u5927\u5229\u76ca\u3092\u9054\u6210\u3059\u308b\u3053\u3068\u3067\u3059\u3002
        • \u30cf\u30d5\u30de\u30f3\u7b26\u53f7\u5316\uff1a\u30cf\u30d5\u30de\u30f3\u7b26\u53f7\u5316\u306f\u7121\u640d\u5931\u30c7\u30fc\u30bf\u5727\u7e2e\u306b\u4f7f\u7528\u3055\u308c\u308b\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3059\u3002\u30cf\u30d5\u30de\u30f3\u6728\u3092\u69cb\u7bc9\u3059\u308b\u3053\u3068\u306b\u3088\u308a\u3001\u5e38\u306b\u6700\u4f4e\u983b\u5ea6\u306e2\u3064\u306e\u30ce\u30fc\u30c9\u3092\u7d71\u5408\u3057\u3001\u6700\u5c0f\u91cd\u307f\u4ed8\u304d\u30d1\u30b9\u9577\uff08\u7b26\u53f7\u5316\u9577\uff09\u306e\u30cf\u30d5\u30de\u30f3\u6728\u3092\u751f\u6210\u3057\u307e\u3059\u3002
        • \u30c0\u30a4\u30af\u30b9\u30c8\u30e9\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff1a\u3053\u308c\u306f\u4e0e\u3048\u3089\u308c\u305f\u30bd\u30fc\u30b9\u9802\u70b9\u304b\u3089\u4ed6\u306e\u3059\u3079\u3066\u306e\u9802\u70b9\u3078\u306e\u6700\u77ed\u7d4c\u8def\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306e\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3059\u3002
        "},{"location":"chapter_greedy/max_capacity_problem/","title":"15.3 \u00a0 \u6700\u5927\u5bb9\u91cf\u554f\u984c","text":"

        Question

        \u914d\u5217 \\(ht\\) \u3092\u5165\u529b\u3057\u307e\u3059\u3002\u5404\u8981\u7d20\u306f\u5782\u76f4\u4ed5\u5207\u308a\u306e\u9ad8\u3055\u3092\u8868\u3057\u307e\u3059\u3002\u914d\u5217\u5185\u306e\u4efb\u610f\u306e2\u3064\u306e\u4ed5\u5207\u308a\u3068\u3001\u305d\u308c\u3089\u306e\u9593\u306e\u30b9\u30da\u30fc\u30b9\u306b\u3088\u3063\u3066\u30b3\u30f3\u30c6\u30ca\u3092\u5f62\u6210\u3067\u304d\u307e\u3059\u3002

        \u30b3\u30f3\u30c6\u30ca\u306e\u5bb9\u91cf\u306f\u9ad8\u3055\u3068\u5e45\u306e\u7a4d\uff08\u9762\u7a4d\uff09\u3067\u3001\u9ad8\u3055\u306f\u77ed\u3044\u65b9\u306e\u4ed5\u5207\u308a\u306b\u3088\u3063\u3066\u6c7a\u5b9a\u3055\u308c\u3001\u5e45\u306f2\u3064\u306e\u4ed5\u5207\u308a\u306e\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u5dee\u3067\u3059\u3002

        \u30b3\u30f3\u30c6\u30ca\u306e\u5bb9\u91cf\u3092\u6700\u5927\u5316\u3059\u308b2\u3064\u306e\u4ed5\u5207\u308a\u3092\u914d\u5217\u304b\u3089\u9078\u629e\u3057\u3001\u3053\u306e\u6700\u5927\u5bb9\u91cf\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4f8b\u3092\u4e0b\u306e\u56f3\u306b\u793a\u3057\u307e\u3059\u3002

        \u56f3 15-7 \u00a0 \u6700\u5927\u5bb9\u91cf\u554f\u984c\u306e\u4f8b\u30c7\u30fc\u30bf

        \u30b3\u30f3\u30c6\u30ca\u306f\u4efb\u610f\u306e2\u3064\u306e\u4ed5\u5207\u308a\u306b\u3088\u3063\u3066\u5f62\u6210\u3055\u308c\u308b\u305f\u3081\u3001\u3053\u306e\u554f\u984c\u306e\u72b6\u614b\u306f2\u3064\u306e\u4ed5\u5207\u308a\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u8868\u73fe\u3055\u308c\u3001\\([i, j]\\) \u3068\u8868\u8a18\u3055\u308c\u307e\u3059\u3002

        \u554f\u984c\u306e\u8a18\u8ff0\u306b\u3088\u308c\u3070\u3001\u5bb9\u91cf\u306f\u9ad8\u3055\u3068\u5e45\u306e\u7a4d\u306b\u7b49\u3057\u304f\u3001\u9ad8\u3055\u306f\u77ed\u3044\u65b9\u306e\u4ed5\u5207\u308a\u306b\u3088\u3063\u3066\u6c7a\u5b9a\u3055\u308c\u3001\u5e45\u306f2\u3064\u306e\u4ed5\u5207\u308a\u306e\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u5dee\u3067\u3059\u3002\u5bb9\u91cf \\(cap[i, j]\\) \u306e\u5f0f\u306f\uff1a

        \\[ cap[i, j] = \\min(ht[i], ht[j]) \\times (j - i) \\]

        \u914d\u5217\u306e\u9577\u3055\u3092 \\(n\\) \u3068\u4eee\u5b9a\u3059\u308b\u3068\u30012\u3064\u306e\u4ed5\u5207\u308a\u306e\u7d44\u307f\u5408\u308f\u305b\u6570\uff08\u72b6\u614b\u306e\u7dcf\u6570\uff09\u306f \\(C_n^2 = \\frac{n(n - 1)}{2}\\) \u3067\u3059\u3002\u6700\u3082\u76f4\u63a5\u7684\u306a\u30a2\u30d7\u30ed\u30fc\u30c1\u306f**\u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u72b6\u614b\u3092\u5217\u6319\u3059\u308b**\u3053\u3068\u3067\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n^2)\\) \u306b\u306a\u308a\u307e\u3059\u3002

        "},{"location":"chapter_greedy/max_capacity_problem/#1","title":"1. \u00a0 \u8caa\u6b32\u6226\u7565\u306e\u6c7a\u5b9a","text":"

        \u3053\u306e\u554f\u984c\u306b\u306f\u3088\u308a\u52b9\u7387\u7684\u306a\u89e3\u6cd5\u304c\u3042\u308a\u307e\u3059\u3002\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 \\(i < j\\) \u304b\u3064\u9ad8\u3055 \\(ht[i] < ht[j]\\) \u306e\u72b6\u614b \\([i, j]\\) \u3092\u9078\u629e\u3057\u307e\u3059\u3002\u3064\u307e\u308a\u3001\\(i\\) \u306f\u77ed\u3044\u4ed5\u5207\u308a\u3001\\(j\\) \u306f\u9ad8\u3044\u4ed5\u5207\u308a\u3067\u3059\u3002

        \u56f3 15-8 \u00a0 \u521d\u671f\u72b6\u614b

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u9ad8\u3044\u4ed5\u5207\u308a \\(j\\) \u3092\u77ed\u3044\u4ed5\u5207\u308a \\(i\\) \u306b\u8fd1\u3065\u3051\u3066\u79fb\u52d5\u3059\u308b\u3068\u3001\u5bb9\u91cf\u306f\u78ba\u5b9f\u306b\u6e1b\u5c11\u3057\u307e\u3059\u3002

        \u3053\u308c\u306f\u3001\u9ad8\u3044\u4ed5\u5207\u308a \\(j\\) \u3092\u79fb\u52d5\u3059\u308b\u3068\u3001\u5e45 \\(j-i\\) \u304c\u78ba\u5b9f\u306b\u6e1b\u5c11\u3059\u308b\u305f\u3081\u3067\u3059\u3002\u9ad8\u3055\u306f\u77ed\u3044\u4ed5\u5207\u308a\u306b\u3088\u3063\u3066\u6c7a\u5b9a\u3055\u308c\u308b\u305f\u3081\u3001\u9ad8\u3055\u306f\u540c\u3058\u307e\u307e\uff08\\(i\\) \u304c\u77ed\u3044\u4ed5\u5207\u308a\u306e\u307e\u307e\uff09\u304b\u6e1b\u5c11\uff08\u79fb\u52d5\u3057\u305f \\(j\\) \u304c\u77ed\u3044\u4ed5\u5207\u308a\u306b\u306a\u308b\uff09\u3057\u304b\u3042\u308a\u307e\u305b\u3093\u3002

        \u56f3 15-9 \u00a0 \u9ad8\u3044\u4ed5\u5207\u308a\u3092\u5185\u5074\u306b\u79fb\u52d5\u3057\u305f\u5f8c\u306e\u72b6\u614b

        \u9006\u306b\u3001\u77ed\u3044\u4ed5\u5207\u308a \\(i\\) \u3092\u5185\u5074\u306b\u79fb\u52d5\u3059\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u306e\u307f\u5bb9\u91cf\u3092\u5897\u52a0\u3055\u305b\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002\u5e45\u306f\u78ba\u5b9f\u306b\u6e1b\u5c11\u3057\u307e\u3059\u304c\u3001\u9ad8\u3055\u304c\u5897\u52a0\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\uff08\u79fb\u52d5\u3057\u305f\u77ed\u3044\u4ed5\u5207\u308a \\(i\\) \u304c\u9ad8\u304f\u306a\u308b\u5834\u5408\uff09\u3002\u4f8b\u3048\u3070\u3001\u4e0b\u306e\u56f3\u3067\u306f\u3001\u77ed\u3044\u4ed5\u5207\u308a\u3092\u79fb\u52d5\u3057\u305f\u5f8c\u306b\u9762\u7a4d\u304c\u5897\u52a0\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 15-10 \u00a0 \u77ed\u3044\u4ed5\u5207\u308a\u3092\u5185\u5074\u306b\u79fb\u52d5\u3057\u305f\u5f8c\u306e\u72b6\u614b

        \u3053\u308c\u306b\u3088\u308a\u3001\u3053\u306e\u554f\u984c\u306e\u8caa\u6b32\u6226\u7565\u304c\u5c0e\u304b\u308c\u307e\u3059\uff1a\u30b3\u30f3\u30c6\u30ca\u306e\u4e21\u7aef\u306b2\u3064\u306e\u30dd\u30a4\u30f3\u30bf\u3092\u521d\u671f\u5316\u3057\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u77ed\u3044\u4ed5\u5207\u308a\u306b\u5bfe\u5fdc\u3059\u308b\u30dd\u30a4\u30f3\u30bf\u3092\u5185\u5074\u306b\u79fb\u52d5\u3057\u30012\u3064\u306e\u30dd\u30a4\u30f3\u30bf\u304c\u51fa\u4f1a\u3046\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002

        \u4e0b\u306e\u56f3\u306f\u8caa\u6b32\u6226\u7565\u306e\u5b9f\u884c\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        1. \u6700\u521d\u306b\u3001\u30dd\u30a4\u30f3\u30bf \\(i\\) \u3068 \\(j\\) \u304c\u914d\u5217\u306e\u4e21\u7aef\u306b\u914d\u7f6e\u3055\u308c\u307e\u3059\u3002
        2. \u73fe\u5728\u306e\u72b6\u614b\u306e\u5bb9\u91cf \\(cap[i, j]\\) \u3092\u8a08\u7b97\u3057\u3001\u6700\u5927\u5bb9\u91cf\u3092\u66f4\u65b0\u3057\u307e\u3059\u3002
        3. \u4ed5\u5207\u308a \\(i\\) \u3068 \\(j\\) \u306e\u9ad8\u3055\u3092\u6bd4\u8f03\u3057\u3001\u77ed\u3044\u4ed5\u5207\u308a\u30921\u30b9\u30c6\u30c3\u30d7\u5185\u5074\u306b\u79fb\u52d5\u3057\u307e\u3059\u3002
        4. \\(i\\) \u3068 \\(j\\) \u304c\u51fa\u4f1a\u3046\u307e\u3067\u30b9\u30c6\u30c3\u30d7 2. \u3068 3. \u3092\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002
        <1><2><3><4><5><6><7><8><9>

        \u56f3 15-11 \u00a0 \u6700\u5927\u5bb9\u91cf\u554f\u984c\u306e\u8caa\u6b32\u30d7\u30ed\u30bb\u30b9

        "},{"location":"chapter_greedy/max_capacity_problem/#2","title":"2. \u00a0 \u5b9f\u88c5","text":"

        \u30b3\u30fc\u30c9\u306f\u6700\u5927 \\(n\\) \u56de\u30eb\u30fc\u30d7\u3059\u308b\u305f\u3081\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u3067\u3059\u3002

        \u5909\u6570 \\(i\\)\u3001\\(j\\)\u3001\\(res\\) \u306f\u4e00\u5b9a\u91cf\u306e\u8ffd\u52a0\u30b9\u30da\u30fc\u30b9\u3092\u4f7f\u7528\u3059\u308b\u305f\u3081\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(1)\\) \u3067\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby max_capacity.py
        def max_capacity(ht: list[int]) -> int:\n    \"\"\"\u6700\u5927\u5bb9\u91cf\uff1a\u8caa\u6b32\u6cd5\"\"\"\n    # i\u3001j \u3092\u521d\u671f\u5316\u3001\u914d\u5217\u306e\u4e21\u7aef\u3067\u5206\u5272\u3055\u305b\u308b\n    i, j = 0, len(ht) - 1\n    # \u521d\u671f\u6700\u5927\u5bb9\u91cf\u306f 0\n    res = 0\n    # 2 \u3064\u306e\u677f\u304c\u51fa\u4f1a\u3046\u307e\u3067\u8caa\u6b32\u9078\u629e\u3092\u30eb\u30fc\u30d7\n    while i < j:\n        # \u6700\u5927\u5bb9\u91cf\u3092\u66f4\u65b0\n        cap = min(ht[i], ht[j]) * (j - i)\n        res = max(res, cap)\n        # \u77ed\u3044\u677f\u3092\u5185\u5074\u306b\u79fb\u52d5\n        if ht[i] < ht[j]:\n            i += 1\n        else:\n            j -= 1\n    return res\n
        max_capacity.cpp
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8caa\u6b32\u6cd5 */\nint maxCapacity(vector<int> &ht) {\n    // i\u3001j \u3092\u521d\u671f\u5316\u3057\u3001\u914d\u5217\u306e\u4e21\u7aef\u3067\u5206\u5272\u3055\u305b\u308b\n    int i = 0, j = ht.size() - 1;\n    // \u521d\u671f\u6700\u5927\u5bb9\u91cf\u306f 0\n    int res = 0;\n    // 2\u3064\u306e\u677f\u304c\u51fa\u4f1a\u3046\u307e\u3067\u8caa\u6b32\u9078\u629e\u3092\u30eb\u30fc\u30d7\n    while (i < j) {\n        // \u6700\u5927\u5bb9\u91cf\u3092\u66f4\u65b0\n        int cap = min(ht[i], ht[j]) * (j - i);\n        res = max(res, cap);\n        // \u3088\u308a\u77ed\u3044\u677f\u3092\u5185\u5074\u306b\u79fb\u52d5\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
        max_capacity.java
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8caa\u6b32\u6cd5 */\nint maxCapacity(int[] ht) {\n    // i\u3001j \u3092\u521d\u671f\u5316\u3057\u3001\u914d\u5217\u306e\u4e21\u7aef\u3067\u5206\u5272\u3055\u305b\u308b\n    int i = 0, j = ht.length - 1;\n    // \u521d\u671f\u6700\u5927\u5bb9\u91cf\u306f 0\n    int res = 0;\n    // 2\u3064\u306e\u677f\u304c\u51fa\u4f1a\u3046\u307e\u3067\u8caa\u6b32\u9078\u629e\u3092\u30eb\u30fc\u30d7\n    while (i < j) {\n        // \u6700\u5927\u5bb9\u91cf\u3092\u66f4\u65b0\n        int cap = Math.min(ht[i], ht[j]) * (j - i);\n        res = Math.max(res, cap);\n        // \u3088\u308a\u77ed\u3044\u677f\u3092\u5185\u5074\u306b\u79fb\u52d5\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
        max_capacity.cs
        [class]{max_capacity}-[func]{MaxCapacity}\n
        max_capacity.go
        [class]{}-[func]{maxCapacity}\n
        max_capacity.swift
        [class]{}-[func]{maxCapacity}\n
        max_capacity.js
        [class]{}-[func]{maxCapacity}\n
        max_capacity.ts
        [class]{}-[func]{maxCapacity}\n
        max_capacity.dart
        [class]{}-[func]{maxCapacity}\n
        max_capacity.rs
        [class]{}-[func]{max_capacity}\n
        max_capacity.c
        [class]{}-[func]{maxCapacity}\n
        max_capacity.kt
        [class]{}-[func]{maxCapacity}\n
        max_capacity.rb
        [class]{}-[func]{max_capacity}\n
        "},{"location":"chapter_greedy/max_capacity_problem/#3","title":"3. \u00a0 \u6b63\u3057\u3055\u306e\u8a3c\u660e","text":"

        \u8caa\u6b32\u6cd5\u304c\u5217\u6319\u3088\u308a\u3082\u9ad8\u901f\u3067\u3042\u308b\u7406\u7531\u306f\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u8caa\u6b32\u9078\u629e\u304c\u4e00\u90e8\u306e\u72b6\u614b\u3092\u300c\u30b9\u30ad\u30c3\u30d7\u300d\u3059\u308b\u304b\u3089\u3067\u3059\u3002

        \u4f8b\u3048\u3070\u3001\\(i\\) \u304c\u77ed\u3044\u4ed5\u5207\u308a\u3067 \\(j\\) \u304c\u9ad8\u3044\u4ed5\u5207\u308a\u3067\u3042\u308b\u72b6\u614b \\(cap[i, j]\\) \u306e\u4e0b\u3067\u3001\u77ed\u3044\u4ed5\u5207\u308a \\(i\\) \u3092\u8caa\u6b32\u306b1\u30b9\u30c6\u30c3\u30d7\u5185\u5074\u306b\u79fb\u52d5\u3059\u308b\u3068\u3001\u4e0b\u306e\u56f3\u306b\u793a\u3059\u300c\u30b9\u30ad\u30c3\u30d7\u3055\u308c\u305f\u300d\u72b6\u614b\u306b\u3064\u306a\u304c\u308a\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u3053\u308c\u3089\u306e\u72b6\u614b\u306e\u5bb9\u91cf\u3092\u5f8c\u3067\u691c\u8a3c\u3067\u304d\u306a\u3044\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

        \\[ cap[i, i+1], cap[i, i+2], \\dots, cap[i, j-2], cap[i, j-1] \\]

        \u56f3 15-12 \u00a0 \u77ed\u3044\u4ed5\u5207\u308a\u306e\u79fb\u52d5\u306b\u3088\u3063\u3066\u30b9\u30ad\u30c3\u30d7\u3055\u308c\u308b\u72b6\u614b

        \u89b3\u5bdf\u3059\u308b\u3068\u3001\u3053\u308c\u3089\u306e\u30b9\u30ad\u30c3\u30d7\u3055\u308c\u305f\u72b6\u614b\u306f\u5b9f\u969b\u306b\u306f\u9ad8\u3044\u4ed5\u5207\u308a \\(j\\) \u304c\u5185\u5074\u306b\u79fb\u52d5\u3057\u305f\u3059\u3079\u3066\u306e\u72b6\u614b**\u3067\u3059\u3002\u9ad8\u3044\u4ed5\u5207\u308a\u3092\u5185\u5074\u306b\u79fb\u52d5\u3059\u308b\u3068\u5bb9\u91cf\u304c\u78ba\u5b9f\u306b\u6e1b\u5c11\u3059\u308b\u3053\u3068\u3092\u3059\u3067\u306b\u8a3c\u660e\u3057\u307e\u3057\u305f\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30b9\u30ad\u30c3\u30d7\u3055\u308c\u305f\u72b6\u614b\u306f\u6700\u9069\u89e3\u3067\u3042\u308b\u53ef\u80fd\u6027\u304c\u306a\u304f\u3001**\u305d\u308c\u3089\u3092\u30b9\u30ad\u30c3\u30d7\u3057\u3066\u3082\u6700\u9069\u89e3\u3092\u898b\u9003\u3059\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u3002

        \u5206\u6790\u306b\u3088\u308a\u3001\u77ed\u3044\u4ed5\u5207\u308a\u3092\u79fb\u52d5\u3059\u308b\u64cd\u4f5c\u306f\u300c\u5b89\u5168\u300d\u3067\u3042\u308a\u3001\u8caa\u6b32\u6226\u7565\u304c\u52b9\u679c\u7684\u3067\u3042\u308b\u3053\u3068\u304c\u793a\u3055\u308c\u307e\u3059\u3002

        "},{"location":"chapter_greedy/max_product_cutting_problem/","title":"15.4 \u00a0 \u6700\u5927\u7a4d\u5207\u65ad\u554f\u984c","text":"

        Question

        \u6b63\u306e\u6574\u6570 \\(n\\) \u304c\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001\u305d\u308c\u3092\u5408\u8a08\u304c \\(n\\) \u306b\u306a\u308b\u5c11\u306a\u304f\u3068\u30822\u3064\u306e\u6b63\u306e\u6574\u6570\u306b\u5206\u5272\u3057\u3001\u3053\u308c\u3089\u306e\u6574\u6570\u306e\u6700\u5927\u7a4d\u3092\u6c42\u3081\u3066\u304f\u3060\u3055\u3044\u3002\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3068\u304a\u308a\u3067\u3059\u3002

        \u56f3 15-13 \u00a0 \u6700\u5927\u7a4d\u5207\u65ad\u554f\u984c\u306e\u5b9a\u7fa9

        \\(n\\) \u3092 \\(m\\) \u500b\u306e\u6574\u6570\u56e0\u5b50\u306b\u5206\u5272\u3059\u308b\u3068\u4eee\u5b9a\u3057\u3001\\(i\\) \u756a\u76ee\u306e\u56e0\u5b50\u3092 \\(n_i\\) \u3068\u8868\u8a18\u3059\u308b\u3068\u3001

        \\[ n = \\sum_{i=1}^{m}n_i \\]

        \u3053\u306e\u554f\u984c\u306e\u76ee\u6a19\u306f\u3001\u3059\u3079\u3066\u306e\u6574\u6570\u56e0\u5b50\u306e\u6700\u5927\u7a4d\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3067\u3059\u3002\u3059\u306a\u308f\u3061\u3001

        \\[ \\max(\\prod_{i=1}^{m}n_i) \\]

        \u8003\u616e\u3059\u3079\u304d\u70b9\uff1a\u5206\u5272\u6570 \\(m\\) \u306f\u3069\u306e\u7a0b\u5ea6\u5927\u304d\u304f\u3059\u3079\u304d\u304b\u3001\u5404 \\(n_i\\) \u306f\u4f55\u3067\u3042\u308b\u3079\u304d\u304b\uff1f

        "},{"location":"chapter_greedy/max_product_cutting_problem/#1","title":"1. \u00a0 \u8caa\u6b32\u6226\u7565\u306e\u6c7a\u5b9a","text":"

        \u7d4c\u9a13\u7684\u306b\u30012\u3064\u306e\u6574\u6570\u306e\u7a4d\u306f\u591a\u304f\u306e\u5834\u5408\u305d\u306e\u548c\u3088\u308a\u3082\u5927\u304d\u304f\u306a\u308a\u307e\u3059\u3002\\(n\\) \u304b\u3089\u56e0\u5b50 \\(2\\) \u3092\u5206\u5272\u3059\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u305d\u306e\u7a4d\u306f \\(2(n-2)\\) \u3067\u3059\u3002\u3053\u306e\u7a4d\u3092 \\(n\\) \u3068\u6bd4\u8f03\u3057\u307e\u3059\uff1a

        \\[ \\begin{aligned} 2(n-2) & \\geq n \\newline 2n - n - 4 & \\geq 0 \\newline n & \\geq 4 \\end{aligned} \\]

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\\(n \\geq 4\\) \u306e\u3068\u304d\u3001\\(2\\) \u3092\u5206\u5272\u3059\u308b\u3068\u7a4d\u304c\u5897\u52a0\u3057\u307e\u3059\u3002\u3053\u308c\u306f4\u4ee5\u4e0a\u306e\u6574\u6570\u3092\u5206\u5272\u3059\u3079\u304d\u3067\u3042\u308b\u3053\u3068\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u8caa\u6b32\u6226\u75651\uff1a\u5206\u5272\u30b9\u30ad\u30fc\u30e0\u304c \\(\\geq 4\\) \u306e\u56e0\u5b50\u3092\u542b\u3080\u5834\u5408\u3001\u305d\u308c\u3089\u306f\u3055\u3089\u306b\u5206\u5272\u3055\u308c\u308b\u3079\u304d\u3067\u3059\u3002\u6700\u7d42\u7684\u306a\u5206\u5272\u306f\u56e0\u5b50 \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u306e\u307f\u3092\u542b\u3080\u3079\u304d\u3067\u3059\u3002

        \u56f3 15-14 \u00a0 \u5206\u5272\u306b\u3088\u308b\u7a4d\u306e\u5897\u52a0

        \u6b21\u306b\u3001\u3069\u306e\u56e0\u5b50\u304c\u6700\u9069\u304b\u3092\u8003\u616e\u3057\u307e\u3059\u3002\u56e0\u5b50 \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u306e\u4e2d\u3067\u3001\u660e\u3089\u304b\u306b \\(1\\) \u304c\u6700\u60aa\u3067\u3059\u3002\\(1 \\times (n-1) < n\\) \u304c\u5e38\u306b\u6210\u308a\u7acb\u3064\u305f\u3081\u3001\\(1\\) \u3092\u5206\u5272\u3059\u308b\u3068\u5b9f\u969b\u306b\u7a4d\u304c\u6e1b\u5c11\u3057\u307e\u3059\u3002

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\\(n = 6\\) \u306e\u3068\u304d\u3001\\(3 \\times 3 > 2 \\times 2 \\times 2\\) \u3067\u3059\u3002\u3053\u308c\u306f \\(3\\) \u3092\u5206\u5272\u3059\u308b\u65b9\u304c \\(2\\) \u3092\u5206\u5272\u3059\u308b\u3088\u308a\u3082\u826f\u3044\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

        \u8caa\u6b32\u6226\u75652\uff1a\u5206\u5272\u30b9\u30ad\u30fc\u30e0\u306b\u306f\u6700\u5927\u30672\u3064\u306e \\(2\\) \u304c\u3042\u308b\u3079\u304d\u3067\u3059\u30023\u3064\u306e \\(2\\) \u306f\u5e38\u306b2\u3064\u306e \\(3\\) \u306b\u7f6e\u304d\u63db\u3048\u3066\u3088\u308a\u9ad8\u3044\u7a4d\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u304b\u3089\u3067\u3059\u3002

        \u56f3 15-15 \u00a0 \u6700\u9069\u306a\u5206\u5272\u56e0\u5b50

        \u4e0a\u8a18\u304b\u3089\u3001\u4ee5\u4e0b\u306e\u8caa\u6b32\u6226\u7565\u3092\u5c0e\u51fa\u3067\u304d\u307e\u3059\u3002

        1. \u5165\u529b\u6574\u6570 \\(n\\) \u306b\u3064\u3044\u3066\u3001\u4f59\u308a\u304c \\(0\\)\u3001\\(1\\)\u3001\u307e\u305f\u306f \\(2\\) \u306b\u306a\u308b\u307e\u3067\u56e0\u5b50 \\(3\\) \u3092\u7d99\u7d9a\u7684\u306b\u5206\u5272\u3057\u307e\u3059\u3002
        2. \u4f59\u308a\u304c \\(0\\) \u306e\u5834\u5408\u3001\\(n\\) \u304c \\(3\\) \u306e\u500d\u6570\u3067\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u305f\u3081\u3001\u305d\u308c\u4ee5\u4e0a\u306e\u884c\u52d5\u306f\u53d6\u308a\u307e\u305b\u3093\u3002
        3. \u4f59\u308a\u304c \\(2\\) \u306e\u5834\u5408\u3001\u3055\u3089\u306b\u5206\u5272\u3092\u7d9a\u3051\u305a\u3001\u305d\u306e\u307e\u307e\u4fdd\u6301\u3057\u307e\u3059\u3002
        4. \u4f59\u308a\u304c \\(1\\) \u306e\u5834\u5408\u3001\\(2 \\times 2 > 1 \\times 3\\) \u3067\u3042\u308b\u305f\u3081\u3001\u6700\u5f8c\u306e \\(3\\) \u3092 \\(2\\) \u306b\u7f6e\u304d\u63db\u3048\u308b\u3079\u304d\u3067\u3059\u3002
        "},{"location":"chapter_greedy/max_product_cutting_problem/#2","title":"2. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        \u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u6574\u6570\u3092\u5206\u5272\u3059\u308b\u305f\u3081\u306b\u30eb\u30fc\u30d7\u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u306f\u306a\u304f\u3001\u5e8a\u9664\u7b97\u6f14\u7b97\u3092\u4f7f\u7528\u3057\u3066 \\(3\\) \u306e\u6570 \\(a\\) \u3092\u53d6\u5f97\u3057\u3001\u5270\u4f59\u6f14\u7b97\u3092\u4f7f\u7528\u3057\u3066\u4f59\u308a \\(b\\) \u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\uff1a

        \\[ n = 3a + b \\]

        \\(n \\leq 3\\) \u306e\u5883\u754c\u30b1\u30fc\u30b9\u3067\u306f\u3001\\(1\\) \u3092\u5206\u5272\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u7a4d\u306f \\(1 \\times (n - 1)\\) \u3067\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby max_product_cutting.py
        def max_product_cutting(n: int) -> int:\n    \"\"\"\u5207\u65ad\u306e\u6700\u5927\u7a4d\uff1a\u8caa\u6b32\u6cd5\"\"\"\n    # n <= 3 \u306e\u5834\u5408\u30011 \u3092\u5207\u308a\u51fa\u3059\u5fc5\u8981\u304c\u3042\u308b\n    if n <= 3:\n        return 1 * (n - 1)\n    # \u8caa\u6b32\u7684\u306b 3 \u3092\u5207\u308a\u51fa\u3059\u3001a \u306f 3 \u306e\u500b\u6570\u3001b \u306f\u4f59\u308a\n    a, b = n // 3, n % 3\n    if b == 1:\n        # \u4f59\u308a\u304c 1 \u306e\u5834\u5408\u30011 * 3 \u306e\u30da\u30a2\u3092 2 * 2 \u306b\u5909\u63db\n        return int(math.pow(3, a - 1)) * 2 * 2\n    if b == 2:\n        # \u4f59\u308a\u304c 2 \u306e\u5834\u5408\u3001\u4f55\u3082\u3057\u306a\u3044\n        return int(math.pow(3, a)) * 2\n    # \u4f59\u308a\u304c 0 \u306e\u5834\u5408\u3001\u4f55\u3082\u3057\u306a\u3044\n    return int(math.pow(3, a))\n
        max_product_cutting.cpp
        /* \u6700\u5927\u7a4d\u5207\u65ad\uff1a\u8caa\u6b32\u6cd5 */\nint maxProductCutting(int n) {\n    // n <= 3 \u306e\u5834\u5408\u30011 \u3092\u5207\u308a\u51fa\u3059\u5fc5\u8981\u304c\u3042\u308b\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // \u8caa\u6b32\u306b 3 \u3092\u5207\u308a\u51fa\u3059\u3002a \u306f 3 \u306e\u500b\u6570\u3001b \u306f\u4f59\u308a\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // \u4f59\u308a\u304c 1 \u306e\u5834\u5408\u30011 * 3 \u306e\u30da\u30a2\u3092 2 * 2 \u306b\u5909\u63db\n        return (int)pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // \u4f59\u308a\u304c 2 \u306e\u5834\u5408\u3001\u4f55\u3082\u3057\u306a\u3044\n        return (int)pow(3, a) * 2;\n    }\n    // \u4f59\u308a\u304c 0 \u306e\u5834\u5408\u3001\u4f55\u3082\u3057\u306a\u3044\n    return (int)pow(3, a);\n}\n
        max_product_cutting.java
        /* \u6700\u5927\u7a4d\u5207\u65ad\uff1a\u8caa\u6b32\u6cd5 */\nint maxProductCutting(int n) {\n    // n <= 3 \u306e\u5834\u5408\u30011 \u3092\u5207\u308a\u51fa\u3059\u5fc5\u8981\u304c\u3042\u308b\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // \u8caa\u6b32\u306b 3 \u3092\u5207\u308a\u51fa\u3059\u3002a \u306f 3 \u306e\u500b\u6570\u3001b \u306f\u4f59\u308a\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // \u4f59\u308a\u304c 1 \u306e\u5834\u5408\u30011 * 3 \u306e\u30da\u30a2\u3092 2 * 2 \u306b\u5909\u63db\n        return (int) Math.pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // \u4f59\u308a\u304c 2 \u306e\u5834\u5408\u3001\u4f55\u3082\u3057\u306a\u3044\n        return (int) Math.pow(3, a) * 2;\n    }\n    // \u4f59\u308a\u304c 0 \u306e\u5834\u5408\u3001\u4f55\u3082\u3057\u306a\u3044\n    return (int) Math.pow(3, a);\n}\n
        max_product_cutting.cs
        [class]{max_product_cutting}-[func]{MaxProductCutting}\n
        max_product_cutting.go
        [class]{}-[func]{maxProductCutting}\n
        max_product_cutting.swift
        [class]{}-[func]{maxProductCutting}\n
        max_product_cutting.js
        [class]{}-[func]{maxProductCutting}\n
        max_product_cutting.ts
        [class]{}-[func]{maxProductCutting}\n
        max_product_cutting.dart
        [class]{}-[func]{maxProductCutting}\n
        max_product_cutting.rs
        [class]{}-[func]{max_product_cutting}\n
        max_product_cutting.c
        [class]{}-[func]{maxProductCutting}\n
        max_product_cutting.kt
        [class]{}-[func]{maxProductCutting}\n
        max_product_cutting.rb
        [class]{}-[func]{max_product_cutting}\n

        \u56f3 15-16 \u00a0 \u5207\u65ad\u5f8c\u306e\u6700\u5927\u7a4d\u306e\u8a08\u7b97\u65b9\u6cd5

        \u6642\u9593\u8a08\u7b97\u91cf\u306f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u3079\u304d\u4e57\u6f14\u7b97\u306e\u5b9f\u88c5\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002Python\u3067\u306f\u3001\u3088\u304f\u4f7f\u7528\u3055\u308c\u308b\u3079\u304d\u4e57\u8a08\u7b97\u95a2\u6570\u306f3\u7a2e\u985e\u3042\u308a\u307e\u3059\uff1a

        • \u6f14\u7b97\u5b50 ** \u3068\u95a2\u6570 pow() \u306e\u4e21\u65b9\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(\\log\u2061 a)\\) \u3067\u3059\u3002
        • math.pow() \u95a2\u6570\u306f\u5185\u90e8\u3067C\u8a00\u8a9e\u30e9\u30a4\u30d6\u30e9\u30ea\u306e pow() \u95a2\u6570\u3092\u547c\u3073\u51fa\u3057\u3001\u6d6e\u52d5\u5c0f\u6570\u70b9\u3079\u304d\u4e57\u3092\u5b9f\u884c\u3057\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(1)\\) \u3067\u3059\u3002

        \u5909\u6570 \\(a\\) \u3068 \\(b\\) \u306f\u4e00\u5b9a\u30b5\u30a4\u30ba\u306e\u8ffd\u52a0\u30b9\u30da\u30fc\u30b9\u3092\u4f7f\u7528\u3059\u308b\u305f\u3081\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(1)\\) \u3067\u3059\u3002

        "},{"location":"chapter_greedy/max_product_cutting_problem/#3","title":"3. \u00a0 \u6b63\u3057\u3055\u306e\u8a3c\u660e","text":"

        \u80cc\u7406\u6cd5\u3092\u4f7f\u7528\u3057\u3001\\(n \\geq 3\\) \u306e\u30b1\u30fc\u30b9\u306e\u307f\u3092\u5206\u6790\u3057\u307e\u3059\u3002

        1. \u3059\u3079\u3066\u306e\u56e0\u5b50 \\(\\leq 3\\)\uff1a\u6700\u9069\u5206\u5272\u30b9\u30ad\u30fc\u30e0\u304c\u56e0\u5b50 \\(x \\geq 4\\) \u3092\u542b\u3080\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u305d\u308c\u3092\u78ba\u5b9f\u306b \\(2(x-2)\\) \u306b\u3055\u3089\u306b\u5206\u5272\u3067\u304d\u3001\u3088\u308a\u5927\u304d\u306a\u7a4d\u3092\u5f97\u3089\u308c\u307e\u3059\u3002\u3053\u308c\u306f\u4eee\u5b9a\u3068\u77db\u76fe\u3057\u307e\u3059\u3002
        2. \u5206\u5272\u30b9\u30ad\u30fc\u30e0\u306b \\(1\\) \u304c\u542b\u307e\u308c\u306a\u3044\uff1a\u6700\u9069\u5206\u5272\u30b9\u30ad\u30fc\u30e0\u304c\u56e0\u5b50 \\(1\\) \u3092\u542b\u3080\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u305d\u308c\u3092\u78ba\u5b9f\u306b\u5225\u306e\u56e0\u5b50\u3068\u7d50\u5408\u3057\u3066\u3088\u308a\u5927\u304d\u306a\u7a4d\u3092\u5f97\u3089\u308c\u307e\u3059\u3002\u3053\u308c\u306f\u4eee\u5b9a\u3068\u77db\u76fe\u3057\u307e\u3059\u3002
        3. \u5206\u5272\u30b9\u30ad\u30fc\u30e0\u306b\u306f\u6700\u5927\u30672\u3064\u306e \\(2\\) \u304c\u542b\u307e\u308c\u308b\uff1a\u6700\u9069\u5206\u5272\u30b9\u30ad\u30fc\u30e0\u304c3\u3064\u306e \\(2\\) \u3092\u542b\u3080\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u305d\u308c\u3089\u3092\u78ba\u5b9f\u306b2\u3064\u306e \\(3\\) \u306b\u7f6e\u304d\u63db\u3048\u3066\u3001\u3088\u308a\u9ad8\u3044\u7a4d\u3092\u9054\u6210\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306f\u4eee\u5b9a\u3068\u77db\u76fe\u3057\u307e\u3059\u3002
        "},{"location":"chapter_greedy/summary/","title":"15.5 \u00a0 \u307e\u3068\u3081","text":"
        • \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u6700\u9069\u5316\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3088\u304f\u4f7f\u7528\u3055\u308c\u3001\u539f\u7406\u306f\u5404\u6c7a\u5b9a\u6bb5\u968e\u3067\u5c40\u6240\u7684\u306b\u6700\u9069\u306a\u6c7a\u5b9a\u3092\u884c\u3044\u3001\u30b0\u30ed\u30fc\u30d0\u30eb\u306b\u6700\u9069\u306a\u89e3\u3092\u9054\u6210\u3059\u308b\u3053\u3068\u3067\u3059\u3002
        • \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u8caa\u6b32\u306a\u9078\u629e\u3092\u6b21\u3005\u3068\u53cd\u5fa9\u7684\u306b\u884c\u3044\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u554f\u984c\u3092\u3088\u308a\u5c0f\u3055\u306a\u90e8\u5206\u554f\u984c\u306b\u5909\u63db\u3057\u3001\u554f\u984c\u304c\u89e3\u6c7a\u3055\u308c\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002
        • \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u5b9f\u88c5\u304c\u7c21\u5358\u306a\u3060\u3051\u3067\u306a\u304f\u3001\u554f\u984c\u89e3\u6c7a\u52b9\u7387\u3082\u9ad8\u3044\u3067\u3059\u3002\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3068\u6bd4\u8f03\u3057\u3066\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u4e00\u822c\u7684\u306b\u3088\u308a\u4f4e\u3044\u6642\u9593\u8a08\u7b97\u91cf\u3092\u6301\u3061\u307e\u3059\u3002
        • \u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u306b\u304a\u3044\u3066\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u7279\u5b9a\u306e\u30b3\u30a4\u30f3\u306e\u7d44\u307f\u5408\u308f\u305b\u306b\u5bfe\u3057\u3066\u6700\u9069\u89e3\u3092\u4fdd\u8a3c\u3067\u304d\u307e\u3059\u304c\u3001\u4ed6\u306e\u7d44\u307f\u5408\u308f\u305b\u3067\u306f\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u975e\u5e38\u306b\u60aa\u3044\u89e3\u3092\u898b\u3064\u3051\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        • \u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u89e3\u6cd5\u306b\u9069\u3057\u305f\u554f\u984c\u306f2\u3064\u306e\u4e3b\u8981\u306a\u6027\u8cea\u3092\u6301\u3061\u307e\u3059\uff1a\u8caa\u6b32\u9078\u629e\u6027\u3068\u6700\u9069\u90e8\u5206\u69cb\u9020\u3002\u8caa\u6b32\u9078\u629e\u6027\u306f\u8caa\u6b32\u6226\u7565\u306e\u52b9\u679c\u3092\u8868\u3057\u307e\u3059\u3002
        • \u4e00\u90e8\u306e\u8907\u96d1\u306a\u554f\u984c\u3067\u306f\u3001\u8caa\u6b32\u9078\u629e\u6027\u3092\u8a3c\u660e\u3059\u308b\u3053\u3068\u306f\u7c21\u5358\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u9006\u306b\u3001\u7121\u52b9\u6027\u3092\u8a3c\u660e\u3059\u308b\u3053\u3068\u306f\u3057\u3070\u3057\u3070\u3088\u308a\u5bb9\u6613\u3067\u3001\u30b3\u30a4\u30f3\u4ea4\u63db\u554f\u984c\u306a\u3069\u304c\u305d\u306e\u4f8b\u3067\u3059\u3002
        • \u8caa\u6b32\u554f\u984c\u306e\u89e3\u6c7a\u306f\u4e3b\u306b3\u3064\u306e\u30b9\u30c6\u30c3\u30d7\u304b\u3089\u69cb\u6210\u3055\u308c\u307e\u3059\uff1a\u554f\u984c\u5206\u6790\u3001\u8caa\u6b32\u6226\u7565\u306e\u6c7a\u5b9a\u3001\u6b63\u3057\u3055\u306e\u8a3c\u660e\u3002\u3053\u306e\u3046\u3061\u3001\u8caa\u6b32\u6226\u7565\u306e\u6c7a\u5b9a\u304c\u91cd\u8981\u306a\u30b9\u30c6\u30c3\u30d7\u3067\u3042\u308a\u3001\u6b63\u3057\u3055\u306e\u8a3c\u660e\u304c\u3057\u3070\u3057\u3070\u6311\u6226\u3068\u306a\u308a\u307e\u3059\u3002
        • \u5206\u6570\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306f0-1\u30ca\u30c3\u30d7\u30b5\u30c3\u30af\u554f\u984c\u306b\u57fa\u3065\u3044\u3066\u30a2\u30a4\u30c6\u30e0\u306e\u4e00\u90e8\u306e\u9078\u629e\u3092\u53ef\u80fd\u306b\u3057\u3001\u3057\u305f\u304c\u3063\u3066\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u4f7f\u7528\u3057\u3066\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002\u8caa\u6b32\u6226\u7565\u306e\u6b63\u3057\u3055\u306f\u80cc\u7406\u6cd5\u306b\u3088\u3063\u3066\u8a3c\u660e\u3067\u304d\u307e\u3059\u3002
        • \u6700\u5927\u5bb9\u91cf\u554f\u984c\u306f\u5168\u63a2\u7d22\u6cd5\u3067\u89e3\u6c7a\u3067\u304d\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n^2)\\) \u3067\u3059\u3002\u8caa\u6b32\u6226\u7565\u3092\u8a2d\u8a08\u3059\u308b\u3053\u3068\u3067\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u77ed\u3044\u677f\u3092\u5185\u5074\u306b\u79fb\u52d5\u3057\u3001\u6642\u9593\u8a08\u7b97\u91cf\u3092 \\(O(n)\\) \u306b\u6700\u9069\u5316\u3057\u307e\u3059\u3002
        • \u5207\u65ad\u5f8c\u306e\u6700\u5927\u7a4d\u554f\u984c\u306b\u304a\u3044\u3066\u30012\u3064\u306e\u8caa\u6b32\u6226\u7565\u3092\u5c0e\u51fa\u3057\u307e\u3059\uff1a\\(\\geq 4\\) \u306e\u6574\u6570\u306f\u7d99\u7d9a\u7684\u306b\u5207\u65ad\u3055\u308c\u308b\u3079\u304d\u3067\u3001\u6700\u9069\u306a\u5207\u65ad\u56e0\u5b50\u306f \\(3\\) \u3067\u3059\u3002\u30b3\u30fc\u30c9\u306b\u306f\u3079\u304d\u4e57\u6f14\u7b97\u304c\u542b\u307e\u308c\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\u3079\u304d\u4e57\u6f14\u7b97\u306e\u5b9f\u88c5\u65b9\u6cd5\u306b\u4f9d\u5b58\u3057\u3001\u4e00\u822c\u7684\u306b \\(O(1)\\) \u307e\u305f\u306f \\(O(\\log n)\\) \u3067\u3059\u3002
        "},{"location":"chapter_hashing/","title":"\u7b2c 6 \u7ae0 \u00a0 \u30cf\u30c3\u30b7\u30e5\u8868","text":"

        Abstract

        \u30b3\u30f3\u30d4\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0\u306e\u4e16\u754c\u306b\u304a\u3044\u3066\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306f\u8ce2\u3044\u53f8\u66f8\u306e\u3088\u3046\u306a\u3082\u306e\u3067\u3059\u3002

        \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u756a\u53f7\u306e\u8a08\u7b97\u65b9\u6cd5\u3092\u7406\u89e3\u3057\u3001\u76ee\u7684\u306e\u672c\u3092\u8fc5\u901f\u306b\u53d6\u5f97\u3059\u308b\u3053\u3068\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002

        "},{"location":"chapter_hashing/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 6.1 \u00a0 \u30cf\u30c3\u30b7\u30e5\u8868
        • 6.2 \u00a0 \u30cf\u30c3\u30b7\u30e5\u885d\u7a81
        • 6.3 \u00a0 \u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0
        • 6.4 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_hashing/hash_algorithm/","title":"6.3 \u00a0 \u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0","text":"

        \u524d\u306e2\u3064\u306e\u7bc0\u3067\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u52d5\u4f5c\u539f\u7406\u3068\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u51e6\u7406\u3059\u308b\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002\u3057\u304b\u3057\u3001\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u3068\u9023\u9396\u6cd5\u306f\u3069\u3061\u3089\u3082**\u885d\u7a81\u304c\u767a\u751f\u3057\u305f\u969b\u306b\u30cf\u30c3\u30b7\u30e5\u8868\u304c\u6b63\u5e38\u306b\u6a5f\u80fd\u3059\u308b\u3053\u3068\u306e\u307f\u3092\u4fdd\u8a3c\u3067\u304d\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306e\u767a\u751f\u983b\u5ea6\u3092\u6e1b\u3089\u3059\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093**\u3002

        \u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u304c\u3042\u307e\u308a\u306b\u3082\u983b\u7e41\u306b\u767a\u751f\u3059\u308b\u3068\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u6027\u80fd\u306f\u5287\u7684\u306b\u60aa\u5316\u3057\u307e\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u9023\u9396\u6cd5\u30cf\u30c3\u30b7\u30e5\u8868\u3067\u306f\u3001\u7406\u60f3\u7684\u306a\u30b1\u30fc\u30b9\u3067\u306f\u30ad\u30fc\u5024\u30da\u30a2\u304c\u30d0\u30b1\u30c3\u30c8\u9593\u306b\u5747\u7b49\u306b\u5206\u6563\u3055\u308c\u3001\u6700\u9069\u306a\u30af\u30a8\u30ea\u52b9\u7387\u3092\u5b9f\u73fe\u3057\u307e\u3059\u3002\u6700\u60aa\u306e\u30b1\u30fc\u30b9\u3067\u306f\u3001\u3059\u3079\u3066\u306e\u30ad\u30fc\u5024\u30da\u30a2\u304c\u540c\u3058\u30d0\u30b1\u30c3\u30c8\u306b\u683c\u7d0d\u3055\u308c\u3001\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(n)\\)\u306b\u60aa\u5316\u3057\u307e\u3059\u3002

        \u56f3 6-8 \u00a0 \u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306e\u7406\u60f3\u7684\u304a\u3088\u3073\u6700\u60aa\u306e\u30b1\u30fc\u30b9

        \u30ad\u30fc\u5024\u30da\u30a2\u306e\u5206\u5e03\u306f\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306b\u3088\u3063\u3066\u6c7a\u5b9a\u3055\u308c\u307e\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u8a08\u7b97\u30b9\u30c6\u30c3\u30d7\u3092\u601d\u3044\u51fa\u3059\u3068\u3001\u307e\u305a\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u8a08\u7b97\u3057\u3001\u6b21\u306b\u914d\u5217\u9577\u3067\u5270\u4f59\u3092\u53d6\u308a\u307e\u3059\uff1a

        index = hash(key) % capacity\n

        \u4e0a\u8a18\u306e\u5f0f\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u5bb9\u91cfcapacity\u304c\u56fa\u5b9a\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0hash()\u304c\u51fa\u529b\u5024\u3092\u6c7a\u5b9a\u3057\u3001\u305d\u308c\u306b\u3088\u3063\u3066\u30cf\u30c3\u30b7\u30e5\u8868\u306b\u304a\u3051\u308b\u30ad\u30fc\u5024\u30da\u30a2\u306e\u5206\u5e03\u3092\u6c7a\u5b9a\u3057\u307e\u3059\u3002

        \u3053\u308c\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306e\u78ba\u7387\u3092\u6e1b\u3089\u3059\u305f\u3081\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0hash()\u306e\u8a2d\u8a08\u306b\u7126\u70b9\u3092\u5f53\u3066\u308b\u3079\u304d\u3067\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

        "},{"location":"chapter_hashing/hash_algorithm/#631","title":"6.3.1 \u00a0 \u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u76ee\u6a19","text":"

        \u300c\u9ad8\u901f\u3067\u5b89\u5b9a\u3057\u305f\u300d\u30cf\u30c3\u30b7\u30e5\u8868\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u5b9f\u73fe\u3059\u308b\u305f\u3081\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u4ee5\u4e0b\u306e\u7279\u6027\u3092\u6301\u3064\u3079\u304d\u3067\u3059\uff1a

        • \u6c7a\u5b9a\u6027: \u540c\u3058\u5165\u529b\u306b\u5bfe\u3057\u3066\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u5e38\u306b\u540c\u3058\u51fa\u529b\u3092\u751f\u6210\u3059\u308b\u3079\u304d\u3067\u3059\u3002\u305d\u3046\u3067\u306a\u3051\u308c\u3070\u30cf\u30c3\u30b7\u30e5\u8868\u306f\u4fe1\u983c\u3067\u304d\u307e\u305b\u3093\u3002
        • \u9ad8\u52b9\u7387: \u30cf\u30c3\u30b7\u30e5\u5024\u3092\u8a08\u7b97\u3059\u308b\u30d7\u30ed\u30bb\u30b9\u306f\u5341\u5206\u306b\u9ad8\u901f\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u8a08\u7b97\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u304c\u5c0f\u3055\u3044\u307b\u3069\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306f\u3088\u308a\u5b9f\u7528\u7684\u306b\u306a\u308a\u307e\u3059\u3002
        • \u5747\u7b49\u5206\u6563: \u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u30ad\u30fc\u5024\u30da\u30a2\u304c\u30cf\u30c3\u30b7\u30e5\u8868\u306b\u5747\u7b49\u306b\u5206\u6563\u3055\u308c\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3059\u308b\u3079\u304d\u3067\u3059\u3002\u5206\u6563\u304c\u5747\u7b49\u3067\u3042\u308b\u307b\u3069\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306e\u78ba\u7387\u306f\u4f4e\u304f\u306a\u308a\u307e\u3059\u3002

        \u5b9f\u969b\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u5b9f\u88c5\u3060\u3051\u3067\u306a\u304f\u3001\u4ed6\u306e\u5206\u91ce\u3067\u3082\u5e83\u304f\u5fdc\u7528\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        • \u30d1\u30b9\u30ef\u30fc\u30c9\u4fdd\u5b58: \u30e6\u30fc\u30b6\u30fc\u30d1\u30b9\u30ef\u30fc\u30c9\u306e\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u3092\u4fdd\u8b77\u3059\u308b\u305f\u3081\u306b\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u901a\u5e38\u5e73\u6587\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u4fdd\u5b58\u305b\u305a\u3001\u30d1\u30b9\u30ef\u30fc\u30c9\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u4fdd\u5b58\u3057\u307e\u3059\u3002\u30e6\u30fc\u30b6\u30fc\u304c\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u5165\u529b\u3059\u308b\u3068\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u5165\u529b\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u8a08\u7b97\u3057\u3001\u4fdd\u5b58\u3055\u308c\u3066\u3044\u308b\u30cf\u30c3\u30b7\u30e5\u5024\u3068\u6bd4\u8f03\u3057\u307e\u3059\u3002\u4e00\u81f4\u3059\u308c\u3070\u3001\u30d1\u30b9\u30ef\u30fc\u30c9\u306f\u6b63\u3057\u3044\u3068\u898b\u306a\u3055\u308c\u307e\u3059\u3002
        • \u30c7\u30fc\u30bf\u6574\u5408\u6027\u30c1\u30a7\u30c3\u30af: \u30c7\u30fc\u30bf\u9001\u4fe1\u8005\u306f\u30c7\u30fc\u30bf\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u8a08\u7b97\u3057\u3066\u4e00\u7dd2\u306b\u9001\u4fe1\u3067\u304d\u307e\u3059\u3002\u53d7\u4fe1\u8005\u306f\u53d7\u4fe1\u3057\u305f\u30c7\u30fc\u30bf\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u518d\u8a08\u7b97\u3057\u3001\u53d7\u4fe1\u3057\u305f\u30cf\u30c3\u30b7\u30e5\u5024\u3068\u6bd4\u8f03\u3067\u304d\u307e\u3059\u3002\u4e00\u81f4\u3059\u308c\u3070\u3001\u30c7\u30fc\u30bf\u306f\u5b8c\u5168\u3067\u3042\u308b\u3068\u898b\u306a\u3055\u308c\u307e\u3059\u3002

        \u6697\u53f7\u5316\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u5024\u304b\u3089\u5143\u306e\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u63a8\u6e2c\u3059\u308b\u306a\u3069\u306e\u9006\u884c\u5206\u6790\u3092\u9632\u3050\u305f\u3081\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3088\u308a\u9ad8\u3044\u30ec\u30d9\u30eb\u306e\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u6a5f\u80fd\u304c\u5fc5\u8981\u3067\u3059\u3002

        • \u4e00\u65b9\u5411\u6027: \u30cf\u30c3\u30b7\u30e5\u5024\u304b\u3089\u5165\u529b\u30c7\u30fc\u30bf\u306b\u95a2\u3059\u308b\u60c5\u5831\u3092\u63a8\u6e2c\u3059\u308b\u3053\u3068\u306f\u4e0d\u53ef\u80fd\u3067\u3042\u308b\u3079\u304d\u3067\u3059\u3002
        • \u885d\u7a81\u8010\u6027: \u540c\u3058\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u751f\u6210\u3059\u308b2\u3064\u306e\u7570\u306a\u308b\u5165\u529b\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u306f\u6975\u3081\u3066\u56f0\u96e3\u3067\u3042\u308b\u3079\u304d\u3067\u3059\u3002
        • \u96ea\u5d29\u52b9\u679c: \u5165\u529b\u306e\u5c0f\u3055\u306a\u5909\u66f4\u306f\u3001\u51fa\u529b\u306b\u5927\u304d\u304f\u4e88\u6e2c\u4e0d\u53ef\u80fd\u306a\u5909\u5316\u3092\u3082\u305f\u3089\u3059\u3079\u304d\u3067\u3059\u3002

        **\u300c\u5747\u7b49\u5206\u6563\u300d\u3068\u300c\u885d\u7a81\u8010\u6027\u300d\u306f2\u3064\u306e\u5225\u3005\u306e\u6982\u5ff5**\u3067\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u5747\u7b49\u5206\u6563\u3092\u6e80\u305f\u3057\u3066\u3082\u3001\u5fc5\u305a\u3057\u3082\u885d\u7a81\u8010\u6027\u304c\u3042\u308b\u3068\u306f\u9650\u308a\u307e\u305b\u3093\u3002\u4f8b\u3048\u3070\u3001\u30e9\u30f3\u30c0\u30e0\u306a\u5165\u529bkey\u306e\u4e0b\u3067\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570key % 100\u306f\u5747\u7b49\u306b\u5206\u6563\u3055\u308c\u305f\u51fa\u529b\u3092\u751f\u6210\u3067\u304d\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u3053\u306e\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u904e\u5ea6\u306b\u30b7\u30f3\u30d7\u30eb\u3067\u3001\u4e0b\u4e8c\u6841\u304c\u540c\u3058\u3059\u3079\u3066\u306ekey\u306f\u540c\u3058\u51fa\u529b\u3092\u6301\u3064\u305f\u3081\u3001\u30cf\u30c3\u30b7\u30e5\u5024\u304b\u3089\u4f7f\u7528\u53ef\u80fd\u306akey\u3092\u7c21\u5358\u306b\u63a8\u6e2c\u3067\u304d\u3001\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u7834\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_hashing/hash_algorithm/#632","title":"6.3.2 \u00a0 \u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u8a2d\u8a08","text":"

        \u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u8a2d\u8a08\u306f\u591a\u304f\u306e\u8981\u56e0\u3092\u8003\u616e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u8907\u96d1\u306a\u554f\u984c\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u8981\u6c42\u304c\u5c11\u306a\u3044\u4e00\u90e8\u306e\u30b7\u30ca\u30ea\u30aa\u3067\u306f\u3001\u3044\u304f\u3064\u304b\u306e\u7c21\u5358\u306a\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u8a2d\u8a08\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002

        • \u52a0\u7b97\u30cf\u30c3\u30b7\u30e5: \u5165\u529b\u306e\u5404\u6587\u5b57\u306eASCII\u30b3\u30fc\u30c9\u3092\u5408\u8a08\u3057\u3001\u5408\u8a08\u3092\u30cf\u30c3\u30b7\u30e5\u5024\u3068\u3057\u3066\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u4e57\u7b97\u30cf\u30c3\u30b7\u30e5: \u4e57\u7b97\u306e\u975e\u76f8\u95a2\u6027\u3092\u5229\u7528\u3057\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u5b9a\u6570\u3092\u4e57\u7b97\u3057\u3001\u5404\u6587\u5b57\u306eASCII\u30b3\u30fc\u30c9\u3092\u30cf\u30c3\u30b7\u30e5\u5024\u306b\u7d2f\u7a4d\u3057\u307e\u3059\u3002
        • XOR\u30cf\u30c3\u30b7\u30e5: \u5165\u529b\u30c7\u30fc\u30bf\u306e\u5404\u8981\u7d20\u3092XOR\u3059\u308b\u3053\u3068\u3067\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u7d2f\u7a4d\u3057\u307e\u3059\u3002
        • \u56de\u8ee2\u30cf\u30c3\u30b7\u30e5: \u5404\u6587\u5b57\u306eASCII\u30b3\u30fc\u30c9\u3092\u30cf\u30c3\u30b7\u30e5\u5024\u306b\u7d2f\u7a4d\u3057\u3001\u5404\u7d2f\u7a4d\u524d\u306b\u30cf\u30c3\u30b7\u30e5\u5024\u306b\u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby simple_hash.py
        def add_hash(key: str) -> int:\n    \"\"\"\u52a0\u6cd5\u30cf\u30c3\u30b7\u30e5\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash += ord(c)\n    return hash % modulus\n\ndef mul_hash(key: str) -> int:\n    \"\"\"\u4e57\u6cd5\u30cf\u30c3\u30b7\u30e5\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash = 31 * hash + ord(c)\n    return hash % modulus\n\ndef xor_hash(key: str) -> int:\n    \"\"\"XOR\u30cf\u30c3\u30b7\u30e5\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash ^= ord(c)\n    return hash % modulus\n\ndef rot_hash(key: str) -> int:\n    \"\"\"\u56de\u8ee2\u30cf\u30c3\u30b7\u30e5\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash = (hash << 4) ^ (hash >> 28) ^ ord(c)\n    return hash % modulus\n
        simple_hash.cpp
        /* \u52a0\u7b97\u30cf\u30c3\u30b7\u30e5 */\nint addHash(string key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash = (hash + (int)c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* \u4e57\u7b97\u30cf\u30c3\u30b7\u30e5 */\nint mulHash(string key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash = (31 * hash + (int)c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* XOR\u30cf\u30c3\u30b7\u30e5 */\nint xorHash(string key) {\n    int hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash ^= (int)c;\n    }\n    return hash & MODULUS;\n}\n\n/* \u56de\u8ee2\u30cf\u30c3\u30b7\u30e5 */\nint rotHash(string key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ (int)c) % MODULUS;\n    }\n    return (int)hash;\n}\n
        simple_hash.java
        /* \u52a0\u7b97\u30cf\u30c3\u30b7\u30e5 */\nint addHash(String key) {\n    long hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash = (hash + (int) c) % MODULUS;\n    }\n    return (int) hash;\n}\n\n/* \u4e57\u7b97\u30cf\u30c3\u30b7\u30e5 */\nint mulHash(String key) {\n    long hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash = (31 * hash + (int) c) % MODULUS;\n    }\n    return (int) hash;\n}\n\n/* XOR\u30cf\u30c3\u30b7\u30e5 */\nint xorHash(String key) {\n    int hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash ^= (int) c;\n    }\n    return hash & MODULUS;\n}\n\n/* \u56de\u8ee2\u30cf\u30c3\u30b7\u30e5 */\nint rotHash(String key) {\n    long hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ (int) c) % MODULUS;\n    }\n    return (int) hash;\n}\n
        simple_hash.cs
        [class]{simple_hash}-[func]{AddHash}\n\n[class]{simple_hash}-[func]{MulHash}\n\n[class]{simple_hash}-[func]{XorHash}\n\n[class]{simple_hash}-[func]{RotHash}\n
        simple_hash.go
        [class]{}-[func]{addHash}\n\n[class]{}-[func]{mulHash}\n\n[class]{}-[func]{xorHash}\n\n[class]{}-[func]{rotHash}\n
        simple_hash.swift
        [class]{}-[func]{addHash}\n\n[class]{}-[func]{mulHash}\n\n[class]{}-[func]{xorHash}\n\n[class]{}-[func]{rotHash}\n
        simple_hash.js
        [class]{}-[func]{addHash}\n\n[class]{}-[func]{mulHash}\n\n[class]{}-[func]{xorHash}\n\n[class]{}-[func]{rotHash}\n
        simple_hash.ts
        [class]{}-[func]{addHash}\n\n[class]{}-[func]{mulHash}\n\n[class]{}-[func]{xorHash}\n\n[class]{}-[func]{rotHash}\n
        simple_hash.dart
        [class]{}-[func]{addHash}\n\n[class]{}-[func]{mulHash}\n\n[class]{}-[func]{xorHash}\n\n[class]{}-[func]{rotHash}\n
        simple_hash.rs
        [class]{}-[func]{add_hash}\n\n[class]{}-[func]{mul_hash}\n\n[class]{}-[func]{xor_hash}\n\n[class]{}-[func]{rot_hash}\n
        simple_hash.c
        [class]{}-[func]{addHash}\n\n[class]{}-[func]{mulHash}\n\n[class]{}-[func]{xorHash}\n\n[class]{}-[func]{rotHash}\n
        simple_hash.kt
        [class]{}-[func]{addHash}\n\n[class]{}-[func]{mulHash}\n\n[class]{}-[func]{xorHash}\n\n[class]{}-[func]{rotHash}\n
        simple_hash.rb
        [class]{}-[func]{add_hash}\n\n[class]{}-[func]{mul_hash}\n\n[class]{}-[func]{xor_hash}\n\n[class]{}-[func]{rot_hash}\n

        \u5404\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6700\u5f8c\u306e\u30b9\u30c6\u30c3\u30d7\u304c\u5927\u304d\u306a\u7d20\u6570\\(1000000007\\)\u306e\u5270\u4f59\u3092\u53d6\u308b\u3053\u3068\u3067\u3001\u30cf\u30c3\u30b7\u30e5\u5024\u304c\u9069\u5207\u306a\u7bc4\u56f2\u5185\u306b\u3042\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3057\u3066\u3044\u308b\u3053\u3068\u304c\u89b3\u5bdf\u3055\u308c\u307e\u3059\u3002\u306a\u305c\u7d20\u6570\u306e\u5270\u4f59\u3092\u53d6\u308b\u3053\u3068\u304c\u5f37\u8abf\u3055\u308c\u308b\u306e\u304b\u3001\u307e\u305f\u306f\u5408\u6210\u6570\u306e\u5270\u4f59\u3092\u53d6\u308b\u3053\u3068\u306e\u6b20\u70b9\u306f\u4f55\u304b\u3092\u8003\u3048\u308b\u4fa1\u5024\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u306f\u8208\u5473\u6df1\u3044\u8cea\u554f\u3067\u3059\u3002

        \u7d50\u8ad6\u3068\u3057\u3066\uff1a\u5927\u304d\u306a\u7d20\u6570\u3092\u5270\u4f59\u3068\u3057\u3066\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30cf\u30c3\u30b7\u30e5\u5024\u306e\u5747\u7b49\u5206\u6563\u3092\u6700\u5927\u5316\u3067\u304d\u307e\u3059\u3002\u7d20\u6570\u306f\u4ed6\u306e\u6570\u3068\u5171\u901a\u56e0\u5b50\u3092\u6301\u305f\u306a\u3044\u305f\u3081\u3001\u5270\u4f59\u6f14\u7b97\u306b\u3088\u3063\u3066\u5f15\u304d\u8d77\u3053\u3055\u308c\u308b\u5468\u671f\u7684\u30d1\u30bf\u30fc\u30f3\u3092\u6e1b\u3089\u3057\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u56de\u907f\u3067\u304d\u307e\u3059\u3002

        \u4f8b\u3048\u3070\u3001\u5408\u6210\u6570\\(9\\)\u3092\u5270\u4f59\u3068\u3057\u3066\u9078\u629e\u3059\u308b\u3068\u3057\u307e\u3059\u3002\u3053\u308c\u306f\\(3\\)\u3067\u5272\u308a\u5207\u308c\u308b\u305f\u3081\u3001\\(3\\)\u3067\u5272\u308a\u5207\u308c\u308b\u3059\u3079\u3066\u306ekey\u306f\u30cf\u30c3\u30b7\u30e5\u5024\\(0\\)\u3001\\(3\\)\u3001\\(6\\)\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3055\u308c\u307e\u3059\u3002

        \\[ \\begin{aligned} \\text{modulus} & = 9 \\newline \\text{key} & = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\dots \\} \\newline \\text{hash} & = \\{ 0, 3, 6, 0, 3, 6, 0, 3, 6, 0, 3, 6,\\dots \\} \\end{aligned} \\]

        \u5165\u529bkey\u304c\u305f\u307e\u305f\u307e\u3053\u306e\u7a2e\u306e\u7b49\u5dee\u6570\u5217\u5206\u5e03\u3092\u6301\u3064\u5834\u5408\u3001\u30cf\u30c3\u30b7\u30e5\u5024\u304c\u30af\u30e9\u30b9\u30bf\u30fc\u3057\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u60aa\u5316\u3055\u305b\u307e\u3059\u3002\u4eca\u5ea6\u306fmodulus\u3092\u7d20\u6570\\(13\\)\u306b\u7f6e\u304d\u63db\u3048\u308b\u3068\u3057\u307e\u3059\u3002key\u3068modulus\u306e\u9593\u306b\u5171\u901a\u56e0\u5b50\u304c\u306a\u3044\u305f\u3081\u3001\u51fa\u529b\u30cf\u30c3\u30b7\u30e5\u5024\u306e\u5747\u7b49\u6027\u304c\u5927\u5e45\u306b\u6539\u5584\u3055\u308c\u307e\u3059\u3002

        \\[ \\begin{aligned} \\text{modulus} & = 13 \\newline \\text{key} & = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\dots \\} \\newline \\text{hash} & = \\{ 0, 3, 6, 9, 12, 2, 5, 8, 11, 1, 4, 7, \\dots \\} \\end{aligned} \\]

        key\u304c\u30e9\u30f3\u30c0\u30e0\u3067\u5747\u7b49\u306b\u5206\u6563\u3055\u308c\u308b\u3053\u3068\u304c\u4fdd\u8a3c\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u5270\u4f59\u3068\u3057\u3066\u7d20\u6570\u307e\u305f\u306f\u5408\u6210\u6570\u3092\u9078\u629e\u3057\u3066\u3082\u3001\u4e21\u65b9\u3068\u3082\u5747\u7b49\u306b\u5206\u6563\u3055\u308c\u305f\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u751f\u6210\u3067\u304d\u308b\u3053\u3068\u306f\u6ce8\u76ee\u306b\u5024\u3057\u307e\u3059\u3002\u3057\u304b\u3057\u3001key\u306e\u5206\u5e03\u306b\u3042\u308b\u7a2e\u306e\u5468\u671f\u6027\u304c\u3042\u308b\u5834\u5408\u3001\u5408\u6210\u6570\u306e\u5270\u4f59\u306f\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u3092\u5f15\u304d\u8d77\u3053\u3057\u3084\u3059\u304f\u306a\u308a\u307e\u3059\u3002

        \u8981\u7d04\u3059\u308b\u3068\u3001\u901a\u5e38\u306f\u7d20\u6570\u3092\u5270\u4f59\u3068\u3057\u3066\u9078\u629e\u3057\u3001\u3053\u306e\u7d20\u6570\u306f\u5468\u671f\u7684\u30d1\u30bf\u30fc\u30f3\u3092\u53ef\u80fd\u306a\u9650\u308a\u6392\u9664\u3057\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5805\u7262\u6027\u3092\u5411\u4e0a\u3055\u305b\u308b\u305f\u3081\u306b\u5341\u5206\u5927\u304d\u304f\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_hashing/hash_algorithm/#633","title":"6.3.3 \u00a0 \u4e00\u822c\u7684\u306a\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0","text":"

        \u4e0a\u8a18\u3067\u8a00\u53ca\u3057\u305f\u7c21\u5358\u306a\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u304b\u306a\u308a\u300c\u8106\u5f31\u300d\u3067\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u8a2d\u8a08\u76ee\u6a19\u304b\u3089\u7a0b\u9060\u3044\u3053\u3068\u306f\u96e3\u3057\u304f\u3042\u308a\u307e\u305b\u3093\u3002\u4f8b\u3048\u3070\u3001\u52a0\u7b97\u3068XOR\u306f\u4ea4\u63db\u6cd5\u5247\u306b\u5f93\u3046\u305f\u3081\u3001\u52a0\u7b97\u30cf\u30c3\u30b7\u30e5\u3068XOR\u30cf\u30c3\u30b7\u30e5\u306f\u540c\u3058\u5185\u5bb9\u3060\u304c\u9806\u5e8f\u304c\u7570\u306a\u308b\u6587\u5b57\u5217\u3092\u533a\u5225\u3067\u304d\u305a\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u60aa\u5316\u3055\u305b\u3001\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u554f\u984c\u3092\u5f15\u304d\u8d77\u3053\u3059\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        \u5b9f\u969b\u306b\u306f\u3001\u901a\u5e38MD5\u3001SHA-1\u3001SHA-2\u3001SHA-3\u306a\u3069\u306e\u6a19\u6e96\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u3053\u308c\u3089\u306f\u4efb\u610f\u306e\u9577\u3055\u306e\u5165\u529b\u30c7\u30fc\u30bf\u3092\u56fa\u5b9a\u9577\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3067\u304d\u307e\u3059\u3002

        \u904e\u53bb1\u4e16\u7d00\u306b\u308f\u305f\u3063\u3066\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u7d99\u7d9a\u7684\u306a\u30a2\u30c3\u30d7\u30b0\u30ec\u30fc\u30c9\u3068\u6700\u9069\u5316\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u3042\u308a\u307e\u3057\u305f\u3002\u4e00\u90e8\u306e\u7814\u7a76\u8005\u306f\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6027\u80fd\u5411\u4e0a\u306b\u52aa\u3081\u3001\u30cf\u30c3\u30ab\u30fc\u3092\u542b\u3080\u4ed6\u306e\u4eba\u3005\u306f\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u554f\u984c\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u306b\u5c02\u5ff5\u3057\u3066\u3044\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u8868\u306f\u3001\u5b9f\u7528\u7684\u306a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u308b\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        • MD5\u3068SHA-1\u306f\u8907\u6570\u56de\u653b\u6483\u306b\u6210\u529f\u3057\u3066\u304a\u308a\u3001\u3055\u307e\u3056\u307e\u306a\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u653e\u68c4\u3055\u308c\u3066\u3044\u307e\u3059\u3002
        • SHA-2\u30b7\u30ea\u30fc\u30ba\u3001\u7279\u306bSHA-256\u306f\u3001\u73fe\u5728\u6700\u3082\u5b89\u5168\u306a\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e1\u3064\u3067\u3001\u6210\u529f\u3057\u305f\u653b\u6483\u306f\u5831\u544a\u3055\u308c\u3066\u304a\u3089\u305a\u3001\u3055\u307e\u3056\u307e\u306a\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3068\u30d7\u30ed\u30c8\u30b3\u30eb\u3067\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u3066\u3044\u307e\u3059\u3002
        • SHA-3\u306fSHA-2\u3068\u6bd4\u8f03\u3057\u3066\u5b9f\u88c5\u30b3\u30b9\u30c8\u304c\u4f4e\u304f\u3001\u8a08\u7b97\u52b9\u7387\u304c\u9ad8\u3044\u3067\u3059\u304c\u3001\u73fe\u5728\u306e\u4f7f\u7528\u7bc4\u56f2\u306fSHA-2\u30b7\u30ea\u30fc\u30ba\u307b\u3069\u5e83\u7bc4\u56f2\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002

        \u8868 6-2 \u00a0 \u4e00\u822c\u7684\u306a\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0

        MD5 SHA-1 SHA-2 SHA-3 \u30ea\u30ea\u30fc\u30b9\u5e74 1992 1995 2002 2008 \u51fa\u529b\u9577 128 bit 160 bit 256/512 bit 224/256/384/512 bit \u30cf\u30c3\u30b7\u30e5\u885d\u7a81 \u983b\u7e41 \u983b\u7e41 \u307e\u308c \u307e\u308c \u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u30ec\u30d9\u30eb \u4f4e\u3001\u653b\u6483\u306b\u6210\u529f\u3057\u3066\u3044\u308b \u4f4e\u3001\u653b\u6483\u306b\u6210\u529f\u3057\u3066\u3044\u308b \u9ad8 \u9ad8 \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3 \u653e\u68c4\u3001\u30c7\u30fc\u30bf\u6574\u5408\u6027\u30c1\u30a7\u30c3\u30af\u306b\u307e\u3060\u4f7f\u7528 \u653e\u68c4 \u6697\u53f7\u901a\u8ca8\u53d6\u5f15\u691c\u8a3c\u3001\u30c7\u30b8\u30bf\u30eb\u7f72\u540d\u306a\u3069 SHA-2\u306e\u4ee3\u66ff\u3068\u3057\u3066\u4f7f\u7528\u53ef\u80fd"},{"location":"chapter_hashing/hash_algorithm/#_1","title":"\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u304a\u3051\u308b\u30cf\u30c3\u30b7\u30e5\u5024","text":"

        \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30ad\u30fc\u306f\u6574\u6570\u3001\u5c0f\u6570\u3001\u6587\u5b57\u5217\u306a\u3069\u306e\u3055\u307e\u3056\u307e\u306a\u30c7\u30fc\u30bf\u578b\u306b\u3067\u304d\u308b\u3053\u3068\u3092\u77e5\u3063\u3066\u3044\u307e\u3059\u3002\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306f\u901a\u5e38\u3001\u3053\u308c\u3089\u306e\u30c7\u30fc\u30bf\u578b\u306b\u5bfe\u3057\u3066\u7d44\u307f\u8fbc\u307f\u306e\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u63d0\u4f9b\u3057\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97\u3057\u307e\u3059\u3002Python\u3092\u4f8b\u306b\u53d6\u308b\u3068\u3001hash()\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u3055\u307e\u3056\u307e\u306a\u30c7\u30fc\u30bf\u578b\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u8a08\u7b97\u3067\u304d\u307e\u3059\u3002

        • \u6574\u6570\u3068\u30d6\u30fc\u30eb\u5024\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f\u3001\u305d\u308c\u3089\u81ea\u8eab\u306e\u5024\u3067\u3059\u3002
        • \u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u3068\u6587\u5b57\u5217\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306e\u8a08\u7b97\u306f\u3088\u308a\u8907\u96d1\u3067\u3001\u8208\u5473\u306e\u3042\u308b\u8aad\u8005\u306f\u81ea\u5206\u3067\u7814\u7a76\u3059\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002
        • \u30bf\u30d7\u30eb\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f\u3001\u305d\u306e\u5404\u8981\u7d20\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306e\u7d44\u307f\u5408\u308f\u305b\u3067\u3001\u5358\u4e00\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306b\u306a\u308a\u307e\u3059\u3002
        • \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f\u3001\u305d\u306e\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u306b\u57fa\u3065\u3044\u3066\u751f\u6210\u3055\u308c\u307e\u3059\u3002\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30cf\u30c3\u30b7\u30e5\u30e1\u30bd\u30c3\u30c9\u3092\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\u3059\u308b\u3053\u3068\u3067\u3001\u5185\u5bb9\u306b\u57fa\u3065\u3044\u3066\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u751f\u6210\u3067\u304d\u307e\u3059\u3002

        Tip

        \u7570\u306a\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u304a\u3051\u308b\u7d44\u307f\u8fbc\u307f\u30cf\u30c3\u30b7\u30e5\u5024\u8a08\u7b97\u95a2\u6570\u306e\u5b9a\u7fa9\u3068\u65b9\u6cd5\u306f\u7570\u306a\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin built_in_hash.py
        num = 3\nhash_num = hash(num)\n# \u6574\u65703\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f3\n\nbol = True\nhash_bol = hash(bol)\n# \u30d6\u30fc\u30eb\u5024True\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f1\n\ndec = 3.14159\nhash_dec = hash(dec)\n# \u5c0f\u65703.14159\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f326484311674566659\n\nstr = \"Hello \u7b97\u6cd5\"\nhash_str = hash(str)\n# \u6587\u5b57\u5217\"Hello \u7b97\u6cd5\"\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f4617003410720528961\n\ntup = (12836, \"\u5c0f\u54c8\")\nhash_tup = hash(tup)\n# \u30bf\u30d7\u30eb(12836, '\u5c0f\u54c8')\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f1029005403108185979\n\nobj = ListNode(0)\nhash_obj = hash(obj)\n# ListNode\u30aa\u30d6\u30b8\u30a7\u30af\u30c80x1058fd810\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f274267521\n
        built_in_hash.cpp
        int num = 3;\nsize_t hashNum = hash<int>()(num);\n// \u6574\u65703\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f3\n\nbool bol = true;\nsize_t hashBol = hash<bool>()(bol);\n// \u30d6\u30fc\u30eb\u50241\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f1\n\ndouble dec = 3.14159;\nsize_t hashDec = hash<double>()(dec);\n// \u5c0f\u65703.14159\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f4614256650576692846\n\nstring str = \"Hello \u7b97\u6cd5\";\nsize_t hashStr = hash<string>()(str);\n// \u6587\u5b57\u5217\"Hello \u7b97\u6cd5\"\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f15466937326284535026\n\n// C++\u3067\u306f\u3001\u7d44\u307f\u8fbc\u307fstd::hash()\u306f\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306e\u307f\u3092\u63d0\u4f9b\n// \u914d\u5217\u3068\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f\u5225\u9014\u5b9f\u88c5\u304c\u5fc5\u8981\n
        built_in_hash.java
        int num = 3;\nint hashNum = Integer.hashCode(num);\n// \u6574\u65703\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f3\n\nboolean bol = true;\nint hashBol = Boolean.hashCode(bol);\n// \u30d6\u30fc\u30eb\u5024true\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f1231\n\ndouble dec = 3.14159;\nint hashDec = Double.hashCode(dec);\n// \u5c0f\u65703.14159\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f-1340954729\n\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode();\n// \u6587\u5b57\u5217\"Hello \u7b97\u6cd5\"\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f-727081396\n\nObject[] arr = { 12836, \"\u5c0f\u54c8\" };\nint hashTup = Arrays.hashCode(arr);\n// \u914d\u5217[12836, \u5c0f\u54c8]\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f1151158\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode();\n// ListNode\u30aa\u30d6\u30b8\u30a7\u30af\u30c8utils.ListNode@7dc5e7b4\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f2110121908\n
        built_in_hash.cs
        int num = 3;\nint hashNum = num.GetHashCode();\n// \u6574\u65703\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f3;\n\nbool bol = true;\nint hashBol = bol.GetHashCode();\n// \u30d6\u30fc\u30eb\u5024true\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f1;\n\ndouble dec = 3.14159;\nint hashDec = dec.GetHashCode();\n// \u5c0f\u65703.14159\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f-1340954729;\n\nstring str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.GetHashCode();\n// \u6587\u5b57\u5217\"Hello \u7b97\u6cd5\"\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f-586107568;\n\nobject[] arr = [12836, \"\u5c0f\u54c8\"];\nint hashTup = arr.GetHashCode();\n// \u914d\u5217[12836, \u5c0f\u54c8]\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f42931033;\n\nListNode obj = new(0);\nint hashObj = obj.GetHashCode();\n// ListNode\u30aa\u30d6\u30b8\u30a7\u30af\u30c80\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f39053774;\n
        built_in_hash.go
        // Go\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30cf\u30c3\u30b7\u30e5\u30b3\u30fc\u30c9\u95a2\u6570\u304c\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u305b\u3093\n
        built_in_hash.swift
        let num = 3\nlet hashNum = num.hashValue\n// \u6574\u65703\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f9047044699613009734\n\nlet bol = true\nlet hashBol = bol.hashValue\n// \u30d6\u30fc\u30eb\u5024true\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f-4431640247352757451\n\nlet dec = 3.14159\nlet hashDec = dec.hashValue\n// \u5c0f\u65703.14159\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f-2465384235396674631\n\nlet str = \"Hello \u7b97\u6cd5\"\nlet hashStr = str.hashValue\n// \u6587\u5b57\u5217\"Hello \u7b97\u6cd5\"\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f-7850626797806988787\n\nlet arr = [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")]\nlet hashTup = arr.hashValue\n// \u914d\u5217[AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")]\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f-2308633508154532996\n\nlet obj = ListNode(x: 0)\nlet hashObj = obj.hashValue\n// ListNode\u30aa\u30d6\u30b8\u30a7\u30af\u30c8utils.ListNode\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f-2434780518035996159\n
        built_in_hash.js
        // JavaScript\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30cf\u30c3\u30b7\u30e5\u30b3\u30fc\u30c9\u95a2\u6570\u304c\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u305b\u3093\n
        built_in_hash.ts
        // TypeScript\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30cf\u30c3\u30b7\u30e5\u30b3\u30fc\u30c9\u95a2\u6570\u304c\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u305b\u3093\n
        built_in_hash.dart
        int num = 3;\nint hashNum = num.hashCode;\n// \u6574\u65703\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f34803\n\nbool bol = true;\nint hashBol = bol.hashCode;\n// \u30d6\u30fc\u30eb\u5024true\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f1231\n\ndouble dec = 3.14159;\nint hashDec = dec.hashCode;\n// \u5c0f\u65703.14159\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f2570631074981783\n\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode;\n// \u6587\u5b57\u5217\"Hello \u7b97\u6cd5\"\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f468167534\n\nList arr = [12836, \"\u5c0f\u54c8\"];\nint hashArr = arr.hashCode;\n// \u914d\u5217[12836, \u5c0f\u54c8]\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f976512528\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode;\n// ListNode\u30aa\u30d6\u30b8\u30a7\u30af\u30c8Instance of 'ListNode'\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f1033450432\n
        built_in_hash.rs
        use std::collections::hash_map::DefaultHasher;\nuse std::hash::{Hash, Hasher};\n\nlet num = 3;\nlet mut num_hasher = DefaultHasher::new();\nnum.hash(&mut num_hasher);\nlet hash_num = num_hasher.finish();\n// \u6574\u65703\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f568126464209439262\n\nlet bol = true;\nlet mut bol_hasher = DefaultHasher::new();\nbol.hash(&mut bol_hasher);\nlet hash_bol = bol_hasher.finish();\n// \u30d6\u30fc\u30eb\u5024true\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f4952851536318644461\n\nlet dec: f32 = 3.14159;\nlet mut dec_hasher = DefaultHasher::new();\ndec.to_bits().hash(&mut dec_hasher);\nlet hash_dec = dec_hasher.finish();\n// \u5c0f\u65703.14159\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f2566941990314602357\n\nlet str = \"Hello \u7b97\u6cd5\";\nlet mut str_hasher = DefaultHasher::new();\nstr.hash(&mut str_hasher);\nlet hash_str = str_hasher.finish();\n// \u6587\u5b57\u5217\"Hello \u7b97\u6cd5\"\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f16092673739211250988\n\nlet arr = (&12836, &\"\u5c0f\u54c8\");\nlet mut tup_hasher = DefaultHasher::new();\narr.hash(&mut tup_hasher);\nlet hash_tup = tup_hasher.finish();\n// \u30bf\u30d7\u30eb(12836, \"\u5c0f\u54c8\")\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f1885128010422702749\n\nlet node = ListNode::new(42);\nlet mut hasher = DefaultHasher::new();\nnode.borrow().val.hash(&mut hasher);\nlet hash = hasher.finish();\n// ListNode\u30aa\u30d6\u30b8\u30a7\u30af\u30c8RefCell { value: ListNode { val: 42, next: None } }\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u306f15387811073369036852\n
        built_in_hash.c
        // C\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30cf\u30c3\u30b7\u30e5\u30b3\u30fc\u30c9\u95a2\u6570\u304c\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u305b\u3093\n
        built_in_hash.kt
        \n

        \u591a\u304f\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u306f\u3001\u4e0d\u5909\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u307f\u304c\u30cf\u30c3\u30b7\u30e5\u8868\u306ekey\u3068\u3057\u3066\u6a5f\u80fd\u3067\u304d\u307e\u3059\u3002\u30ea\u30b9\u30c8\uff08\u52d5\u7684\u914d\u5217\uff09\u3092key\u3068\u3057\u3066\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001\u30ea\u30b9\u30c8\u306e\u5185\u5bb9\u304c\u5909\u66f4\u3055\u308c\u308b\u3068\u3001\u305d\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u3082\u5909\u66f4\u3055\u308c\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3067\u5143\u306evalue\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u306a\u304f\u306a\u308a\u307e\u3059\u3002

        \u30ab\u30b9\u30bf\u30e0\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff08\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u306a\u3069\uff09\u306e\u30e1\u30f3\u30d0\u30fc\u5909\u6570\u306f\u53ef\u5909\u3067\u3059\u304c\u3001\u30cf\u30c3\u30b7\u30e5\u53ef\u80fd\u3067\u3059\u3002\u3053\u308c\u306f\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30cf\u30c3\u30b7\u30e5\u5024\u304c\u901a\u5e38\u305d\u306e\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u306b\u57fa\u3065\u3044\u3066\u751f\u6210\u3055\u308c\u308b\u305f\u3081\u3067\u3059\u3002\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5185\u5bb9\u304c\u5909\u66f4\u3055\u308c\u3066\u3082\u3001\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u306f\u540c\u3058\u307e\u307e\u306a\u306e\u3067\u3001\u30cf\u30c3\u30b7\u30e5\u5024\u306f\u5909\u66f4\u3055\u308c\u307e\u305b\u3093\u3002

        \u7570\u306a\u308b\u30b3\u30f3\u30bd\u30fc\u30eb\u3067\u51fa\u529b\u3055\u308c\u308b\u30cf\u30c3\u30b7\u30e5\u5024\u304c\u7570\u306a\u308b\u3053\u3068\u306b\u6c17\u3065\u3044\u305f\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u3053\u308c\u306f\u3001Python\u30a4\u30f3\u30bf\u30fc\u30d7\u30ea\u30bf\u30fc\u304c\u8d77\u52d5\u3059\u308b\u305f\u3073\u306b\u6587\u5b57\u5217\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306b\u30e9\u30f3\u30c0\u30e0\u30bd\u30eb\u30c8\u3092\u8ffd\u52a0\u3059\u308b\u305f\u3081\u3067\u3059\u3002\u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u306fHashDoS\u653b\u6483\u3092\u52b9\u679c\u7684\u306b\u9632\u304e\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002

        "},{"location":"chapter_hashing/hash_collision/","title":"6.2 \u00a0 \u30cf\u30c3\u30b7\u30e5\u885d\u7a81","text":"

        \u524d\u7bc0\u3067\u8ff0\u3079\u305f\u3088\u3046\u306b\u3001**\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u5165\u529b\u7a7a\u9593\u306f\u51fa\u529b\u7a7a\u9593\u3088\u308a\u3082\u306f\u308b\u304b\u306b\u5927\u304d\u3044**\u305f\u3081\u3001\u7406\u8ad6\u7684\u306b\u306f\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306f\u907f\u3051\u3089\u308c\u307e\u305b\u3093\u3002\u4f8b\u3048\u3070\u3001\u5165\u529b\u7a7a\u9593\u304c\u3059\u3079\u3066\u306e\u6574\u6570\u3067\u3001\u51fa\u529b\u7a7a\u9593\u304c\u914d\u5217\u5bb9\u91cf\u306e\u30b5\u30a4\u30ba\u306e\u5834\u5408\u3001\u8907\u6570\u306e\u6574\u6570\u304c\u5fc5\u7136\u7684\u306b\u540c\u3058\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3055\u308c\u307e\u3059\u3002

        \u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306f\u8aa4\u3063\u305f\u30af\u30a8\u30ea\u7d50\u679c\u306b\u3064\u306a\u304c\u308a\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u4f7f\u3044\u3084\u3059\u3055\u306b\u6df1\u523b\u306a\u5f71\u97ff\u3092\u4e0e\u3048\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u554f\u984c\u306b\u5bfe\u51e6\u3059\u308b\u305f\u3081\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u304c\u767a\u751f\u3059\u308b\u305f\u3073\u306b\u3001\u885d\u7a81\u304c\u6d88\u3048\u308b\u307e\u3067\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30ea\u30b5\u30a4\u30ba\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002\u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\u975e\u5e38\u306b\u30b7\u30f3\u30d7\u30eb\u3067\u76f4\u63a5\u7684\u3067\u3042\u308a\u3001\u3046\u307e\u304f\u6a5f\u80fd\u3057\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u30c6\u30fc\u30d6\u30eb\u306e\u62e1\u5f35\u306b\u306f\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u79fb\u884c\u3068\u30cf\u30c3\u30b7\u30e5\u30b3\u30fc\u30c9\u306e\u518d\u8a08\u7b97\u304c\u542b\u307e\u308c\u3001\u3053\u308c\u3089\u306f\u9ad8\u30b3\u30b9\u30c8\u3067\u3042\u308b\u305f\u3081\u3001\u975e\u5e38\u306b\u975e\u52b9\u7387\u7684\u306b\u898b\u3048\u307e\u3059\u3002\u52b9\u7387\u3092\u5411\u4e0a\u3055\u305b\u308b\u305f\u3081\u306b\u3001\u4ee5\u4e0b\u306e\u6226\u7565\u3092\u63a1\u7528\u3067\u304d\u307e\u3059\uff1a

        1. **\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u304c\u767a\u751f\u3057\u305f\u5834\u5408\u3067\u3082\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u306e\u691c\u7d22\u304c\u9069\u5207\u306b\u6a5f\u80fd\u3059\u308b**\u3088\u3046\u306b\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u6539\u5584\u3059\u308b\u3002
        2. \u6df1\u523b\u306a\u885d\u7a81\u304c\u89b3\u5bdf\u3055\u308c\u3001\u5fc5\u8981\u306b\u306a\u308b\u524d\u306b\u3001\u62e1\u5f35\u306f\u6700\u5f8c\u306e\u624b\u6bb5\u3068\u3059\u308b\u3002

        \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u69cb\u9020\u3092\u6539\u5584\u3059\u308b\u4e3b\u306a\u65b9\u6cd5\u306f2\u3064\u3042\u308a\u307e\u3059\uff1a\u300c\u9023\u9396\u6cd5\u300d\u3068\u300c\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u300d\u3067\u3059\u3002

        "},{"location":"chapter_hashing/hash_collision/#621","title":"6.2.1 \u00a0 \u9023\u9396\u6cd5","text":"

        \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u8868\u3067\u306f\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u306f1\u3064\u306e\u30ad\u30fc\u5024\u30da\u30a2\u306e\u307f\u3092\u683c\u7d0d\u3067\u304d\u307e\u3059\u3002\u9023\u9396\u6cd5\u306f\u5358\u4e00\u306e\u8981\u7d20\u3092\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u5909\u63db\u3057\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u3068\u3057\u3066\u6271\u3044\u3001\u885d\u7a81\u3059\u308b\u3059\u3079\u3066\u306e\u30ad\u30fc\u5024\u30da\u30a2\u3092\u540c\u3058\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u683c\u7d0d\u3057\u307e\u3059\u3002\u4e0b\u56f3\u306f\u9023\u9396\u6cd5\u3092\u4f7f\u7528\u3057\u305f\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u4f8b\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 6-5 \u00a0 \u9023\u9396\u6cd5\u30cf\u30c3\u30b7\u30e5\u8868

        \u9023\u9396\u6cd5\u3067\u5b9f\u88c5\u3055\u308c\u305f\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u64cd\u4f5c\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5909\u66f4\u3055\u308c\u307e\u3059\uff1a

        • \u8981\u7d20\u306e\u30af\u30a8\u30ea: key\u3092\u5165\u529b\u3057\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u3092\u901a\u3057\u3066\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\u3057\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u306b\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8d70\u67fb\u3057\u3066\u30ad\u30fc\u3092\u6bd4\u8f03\u3057\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u30ad\u30fc\u5024\u30da\u30a2\u3092\u898b\u3064\u3051\u307e\u3059\u3002
        • \u8981\u7d20\u306e\u8ffd\u52a0: \u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u3092\u901a\u3057\u3066\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3001\u30ce\u30fc\u30c9\uff08\u30ad\u30fc\u5024\u30da\u30a2\uff09\u3092\u30ea\u30b9\u30c8\u306b\u8ffd\u52a0\u3057\u307e\u3059\u3002
        • \u8981\u7d20\u306e\u524a\u9664: \u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u7d50\u679c\u306b\u57fa\u3065\u3044\u3066\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30d8\u30c3\u30c9\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8d70\u67fb\u3057\u3066\u30bf\u30fc\u30b2\u30c3\u30c8\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u3066\u524a\u9664\u3057\u307e\u3059\u3002

        \u9023\u9396\u6cd5\u306b\u306f\u4ee5\u4e0b\u306e\u5236\u9650\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u7a7a\u9593\u4f7f\u7528\u91cf\u306e\u5897\u52a0: \u9023\u7d50\u30ea\u30b9\u30c8\u306b\u306f\u30ce\u30fc\u30c9\u30dd\u30a4\u30f3\u30bf\u304c\u542b\u307e\u308c\u3066\u304a\u308a\u3001\u914d\u5217\u3088\u308a\u3082\u591a\u304f\u306e\u30e1\u30e2\u30ea\u7a7a\u9593\u3092\u6d88\u8cbb\u3057\u307e\u3059\u3002
        • \u30af\u30a8\u30ea\u52b9\u7387\u306e\u4f4e\u4e0b: \u5bfe\u5fdc\u3059\u308b\u8981\u7d20\u3092\u898b\u3064\u3051\u308b\u305f\u3081\u306b\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u7dda\u5f62\u8d70\u67fb\u304c\u5fc5\u8981\u306b\u306a\u308b\u305f\u3081\u3067\u3059\u3002

        \u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u306f\u9023\u9396\u6cd5\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u7c21\u5358\u306a\u5b9f\u88c5\u3092\u63d0\u4f9b\u3057\u3001\u6ce8\u610f\u3059\u3079\u304d2\u3064\u306e\u70b9\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u7c21\u5358\u306b\u3059\u308b\u305f\u3081\u306b\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u4ee3\u308f\u308a\u306b\u30ea\u30b9\u30c8\uff08\u52d5\u7684\u914d\u5217\uff09\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u3053\u306e\u8a2d\u5b9a\u3067\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u8868\uff08\u914d\u5217\uff09\u306f\u8907\u6570\u306e\u30d0\u30b1\u30c3\u30c8\u3092\u542b\u307f\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u306f\u30ea\u30b9\u30c8\u3067\u3059\u3002
        • \u3053\u306e\u5b9f\u88c5\u306b\u306f\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30ea\u30b5\u30a4\u30ba\u30e1\u30bd\u30c3\u30c9\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002\u8ca0\u8377\u7387\u304c\\(\\frac{2}{3}\\)\u3092\u8d85\u3048\u308b\u3068\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3092\u5143\u306e\u30b5\u30a4\u30ba\u306e2\u500d\u306b\u62e1\u5f35\u3057\u307e\u3059\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map_chaining.py
        class HashMapChaining:\n    \"\"\"\u30c1\u30a7\u30fc\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\"\"\"\n\n    def __init__(self):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        self.size = 0  # \u30ad\u30fc\u5024\u30da\u30a2\u306e\u6570\n        self.capacity = 4  # \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306e\u5bb9\u91cf\n        self.load_thres = 2.0 / 3.0  # \u62e1\u5f35\u3092\u30c8\u30ea\u30ac\u30fc\u3059\u308b\u8ca0\u8377\u7387\u306e\u95be\u5024\n        self.extend_ratio = 2  # \u62e1\u5f35\u306e\u500d\u6570\n        self.buckets = [[] for _ in range(self.capacity)]  # \u30d0\u30b1\u30c3\u30c8\u914d\u5217\n\n    def hash_func(self, key: int) -> int:\n        \"\"\"\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\"\"\"\n        return key % self.capacity\n\n    def load_factor(self) -> float:\n        \"\"\"\u8ca0\u8377\u7387\"\"\"\n        return self.size / self.capacity\n\n    def get(self, key: int) -> str | None:\n        \"\"\"\u7167\u4f1a\u64cd\u4f5c\"\"\"\n        index = self.hash_func(key)\n        bucket = self.buckets[index]\n        # \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3057\u3001\u30ad\u30fc\u304c\u898b\u3064\u304b\u308c\u3070\u5bfe\u5fdc\u3059\u308b val \u3092\u8fd4\u3059\n        for pair in bucket:\n            if pair.key == key:\n                return pair.val\n        # \u30ad\u30fc\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3001None \u3092\u8fd4\u3059\n        return None\n\n    def put(self, key: int, val: str):\n        \"\"\"\u8ffd\u52a0\u64cd\u4f5c\"\"\"\n        # \u8ca0\u8377\u7387\u304c\u95be\u5024\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u62e1\u5f35\u3092\u5b9f\u884c\n        if self.load_factor() > self.load_thres:\n            self.extend()\n        index = self.hash_func(key)\n        bucket = self.buckets[index]\n        # \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3057\u3001\u6307\u5b9a\u3055\u308c\u305f\u30ad\u30fc\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u5bfe\u5fdc\u3059\u308b val \u3092\u66f4\u65b0\u3057\u3066\u8fd4\u3059\n        for pair in bucket:\n            if pair.key == key:\n                pair.val = val\n                return\n        # \u30ad\u30fc\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u672b\u5c3e\u306b\u8ffd\u52a0\n        pair = Pair(key, val)\n        bucket.append(pair)\n        self.size += 1\n\n    def remove(self, key: int):\n        \"\"\"\u524a\u9664\u64cd\u4f5c\"\"\"\n        index = self.hash_func(key)\n        bucket = self.buckets[index]\n        # \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3057\u3001\u305d\u306e\u4e2d\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2\u3092\u524a\u9664\n        for pair in bucket:\n            if pair.key == key:\n                bucket.remove(pair)\n                self.size -= 1\n                break\n\n    def extend(self):\n        \"\"\"\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u62e1\u5f35\"\"\"\n        # \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u4e00\u6642\u7684\u306b\u4fdd\u5b58\n        buckets = self.buckets\n        # \u62e1\u5f35\u3055\u308c\u305f\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n        self.capacity *= self.extend_ratio\n        self.buckets = [[] for _ in range(self.capacity)]\n        self.size = 0\n        # \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306b\u30ad\u30fc\u5024\u30da\u30a2\u3092\u79fb\u52d5\n        for bucket in buckets:\n            for pair in bucket:\n                self.put(pair.key, pair.val)\n\n    def print(self):\n        \"\"\"\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u51fa\u529b\"\"\"\n        for bucket in self.buckets:\n            res = []\n            for pair in bucket:\n                res.append(str(pair.key) + \" -> \" + pair.val)\n            print(res)\n
        hash_map_chaining.cpp
        /* \u30c1\u30a7\u30a4\u30f3\u6cd5\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb */\nclass HashMapChaining {\n  private:\n    int size;                       // \u30ad\u30fc\u5024\u30da\u30a2\u306e\u6570\n    int capacity;                   // \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306e\u5bb9\u91cf\n    double loadThres;               // \u62e1\u5f35\u3092\u30c8\u30ea\u30ac\u30fc\u3059\u308b\u8ca0\u8377\u7387\u306e\u95be\u5024\n    int extendRatio;                // \u62e1\u5f35\u500d\u7387\n    vector<vector<Pair *>> buckets; // \u30d0\u30b1\u30c3\u30c8\u914d\u5217\n\n  public:\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    HashMapChaining() : size(0), capacity(4), loadThres(2.0 / 3.0), extendRatio(2) {\n        buckets.resize(capacity);\n    }\n\n    /* \u30c7\u30b9\u30c8\u30e9\u30af\u30bf */\n    ~HashMapChaining() {\n        for (auto &bucket : buckets) {\n            for (Pair *pair : bucket) {\n                // \u30e1\u30e2\u30ea\u3092\u89e3\u653e\n                delete pair;\n            }\n        }\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u95a2\u6570 */\n    int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u8ca0\u8377\u7387 */\n    double loadFactor() {\n        return (double)size / (double)capacity;\n    }\n\n    /* \u30af\u30a8\u30ea\u64cd\u4f5c */\n    string get(int key) {\n        int index = hashFunc(key);\n        // \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3001\u30ad\u30fc\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001\u5bfe\u5fdc\u3059\u308bval\u3092\u8fd4\u5374\n        for (Pair *pair : buckets[index]) {\n            if (pair->key == key) {\n                return pair->val;\n            }\n        }\n        // \u30ad\u30fc\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3001\u7a7a\u6587\u5b57\u5217\u3092\u8fd4\u5374\n        return \"\";\n    }\n\n    /* \u8ffd\u52a0\u64cd\u4f5c */\n    void put(int key, string val) {\n        // \u8ca0\u8377\u7387\u304c\u95be\u5024\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u62e1\u5f35\u3092\u5b9f\u884c\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        int index = hashFunc(key);\n        // \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3001\u6307\u5b9a\u30ad\u30fc\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u5bfe\u5fdc\u3059\u308bval\u3092\u66f4\u65b0\u3057\u3066\u8fd4\u5374\n        for (Pair *pair : buckets[index]) {\n            if (pair->key == key) {\n                pair->val = val;\n                return;\n            }\n        }\n        // \u30ad\u30fc\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u672b\u5c3e\u306b\u8ffd\u52a0\n        buckets[index].push_back(new Pair(key, val));\n        size++;\n    }\n\n    /* \u524a\u9664\u64cd\u4f5c */\n    void remove(int key) {\n        int index = hashFunc(key);\n        auto &bucket = buckets[index];\n        // \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u524a\u9664\n        for (int i = 0; i < bucket.size(); i++) {\n            if (bucket[i]->key == key) {\n                Pair *tmp = bucket[i];\n                bucket.erase(bucket.begin() + i); // \u30ad\u30fc\u5024\u30da\u30a2\u3092\u524a\u9664\n                delete tmp;                       // \u30e1\u30e2\u30ea\u3092\u89e3\u653e\n                size--;\n                return;\n            }\n        }\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u62e1\u5f35 */\n    void extend() {\n        // \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u4e00\u6642\u4fdd\u5b58\n        vector<vector<Pair *>> bucketsTmp = buckets;\n        // \u62e1\u5f35\u3055\u308c\u305f\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n        capacity *= extendRatio;\n        buckets.clear();\n        buckets.resize(capacity);\n        size = 0;\n        // \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306b\u30ad\u30fc\u5024\u30da\u30a2\u3092\u79fb\u52d5\n        for (auto &bucket : bucketsTmp) {\n            for (Pair *pair : bucket) {\n                put(pair->key, pair->val);\n                // \u30e1\u30e2\u30ea\u3092\u89e3\u653e\n                delete pair;\n            }\n        }\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u5370\u5237 */\n    void print() {\n        for (auto &bucket : buckets) {\n            cout << \"[\";\n            for (Pair *pair : bucket) {\n                cout << pair->key << \" -> \" << pair->val << \", \";\n            }\n            cout << \"]\\n\";\n        }\n    }\n};\n
        hash_map_chaining.java
        /* \u30c1\u30a7\u30a4\u30f3\u6cd5\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb */\nclass HashMapChaining {\n    int size; // \u30ad\u30fc\u5024\u30da\u30a2\u306e\u6570\n    int capacity; // \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306e\u5bb9\u91cf\n    double loadThres; // \u62e1\u5f35\u3092\u30c8\u30ea\u30ac\u30fc\u3059\u308b\u8ca0\u8377\u7387\u306e\u95be\u5024\n    int extendRatio; // \u62e1\u5f35\u500d\u7387\n    List<List<Pair>> buckets; // \u30d0\u30b1\u30c3\u30c8\u914d\u5217\n\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    public HashMapChaining() {\n        size = 0;\n        capacity = 4;\n        loadThres = 2.0 / 3.0;\n        extendRatio = 2;\n        buckets = new ArrayList<>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.add(new ArrayList<>());\n        }\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u95a2\u6570 */\n    int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u8ca0\u8377\u7387 */\n    double loadFactor() {\n        return (double) size / capacity;\n    }\n\n    /* \u30af\u30a8\u30ea\u64cd\u4f5c */\n    String get(int key) {\n        int index = hashFunc(key);\n        List<Pair> bucket = buckets.get(index);\n        // \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3001\u30ad\u30fc\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u5bfe\u5fdc\u3059\u308bval\u3092\u8fd4\u3059\n        for (Pair pair : bucket) {\n            if (pair.key == key) {\n                return pair.val;\n            }\n        }\n        // \u30ad\u30fc\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3001null\u3092\u8fd4\u3059\n        return null;\n    }\n\n    /* \u8ffd\u52a0\u64cd\u4f5c */\n    void put(int key, String val) {\n        // \u8ca0\u8377\u7387\u304c\u95be\u5024\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u62e1\u5f35\u3092\u5b9f\u884c\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        int index = hashFunc(key);\n        List<Pair> bucket = buckets.get(index);\n        // \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3001\u6307\u5b9a\u3057\u305f\u30ad\u30fc\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u5bfe\u5fdc\u3059\u308bval\u3092\u66f4\u65b0\u3057\u3066\u623b\u308b\n        for (Pair pair : bucket) {\n            if (pair.key == key) {\n                pair.val = val;\n                return;\n            }\n        }\n        // \u30ad\u30fc\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u672b\u5c3e\u306b\u8ffd\u52a0\n        Pair pair = new Pair(key, val);\n        bucket.add(pair);\n        size++;\n    }\n\n    /* \u524a\u9664\u64cd\u4f5c */\n    void remove(int key) {\n        int index = hashFunc(key);\n        List<Pair> bucket = buckets.get(index);\n        // \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3001\u305d\u306e\u4e2d\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2\u3092\u524a\u9664\n        for (Pair pair : bucket) {\n            if (pair.key == key) {\n                bucket.remove(pair);\n                size--;\n                break;\n            }\n        }\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u62e1\u5f35 */\n    void extend() {\n        // \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u4e00\u6642\u7684\u306b\u4fdd\u5b58\n        List<List<Pair>> bucketsTmp = buckets;\n        // \u62e1\u5f35\u3055\u308c\u305f\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n        capacity *= extendRatio;\n        buckets = new ArrayList<>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.add(new ArrayList<>());\n        }\n        size = 0;\n        // \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306b\u30ad\u30fc\u5024\u30da\u30a2\u3092\u79fb\u52d5\n        for (List<Pair> bucket : bucketsTmp) {\n            for (Pair pair : bucket) {\n                put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u5370\u5237 */\n    void print() {\n        for (List<Pair> bucket : buckets) {\n            List<String> res = new ArrayList<>();\n            for (Pair pair : bucket) {\n                res.add(pair.key + \" -> \" + pair.val);\n            }\n            System.out.println(res);\n        }\n    }\n}\n
        hash_map_chaining.cs
        [class]{HashMapChaining}-[func]{}\n
        hash_map_chaining.go
        [class]{hashMapChaining}-[func]{}\n
        hash_map_chaining.swift
        [class]{HashMapChaining}-[func]{}\n
        hash_map_chaining.js
        [class]{HashMapChaining}-[func]{}\n
        hash_map_chaining.ts
        [class]{HashMapChaining}-[func]{}\n
        hash_map_chaining.dart
        [class]{HashMapChaining}-[func]{}\n
        hash_map_chaining.rs
        [class]{HashMapChaining}-[func]{}\n
        hash_map_chaining.c
        [class]{Node}-[func]{}\n\n[class]{HashMapChaining}-[func]{}\n
        hash_map_chaining.kt
        [class]{HashMapChaining}-[func]{}\n
        hash_map_chaining.rb
        [class]{HashMapChaining}-[func]{}\n

        \u9023\u7d50\u30ea\u30b9\u30c8\u304c\u975e\u5e38\u306b\u9577\u3044\u5834\u5408\u3001\u30af\u30a8\u30ea\u52b9\u7387\\(O(n)\\)\u304c\u60aa\u3044\u3053\u3068\u306f\u6ce8\u76ee\u306b\u5024\u3057\u307e\u3059\u3002\u3053\u306e\u5834\u5408\u3001\u30ea\u30b9\u30c8\u3092\u300cAVL\u6728\u300d\u307e\u305f\u306f\u300c\u8d64\u9ed2\u6728\u300d\u306b\u5909\u63db\u3057\u3066\u3001\u30af\u30a8\u30ea\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3092\\(O(\\log n)\\)\u306b\u6700\u9069\u5316\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_hashing/hash_collision/#622","title":"6.2.2 \u00a0 \u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5","text":"

        \u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u306f\u8ffd\u52a0\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u5c0e\u5165\u305b\u305a\u3001\u4ee3\u308f\u308a\u306b\u300c\u8907\u6570\u56de\u30d7\u30ed\u30fc\u30d6\u300d\u3092\u901a\u3057\u3066\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u51e6\u7406\u3057\u307e\u3059\u3002\u30d7\u30ed\u30fc\u30d6\u65b9\u6cd5\u306b\u306f\u4e3b\u306b\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u3001\u4e8c\u6b21\u30d7\u30ed\u30fc\u30d6\u3001\u4e8c\u91cd\u30cf\u30c3\u30b7\u30e5\u304c\u3042\u308a\u307e\u3059\u3002

        \u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u3092\u4f8b\u306b\u3057\u3066\u3001\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u7d39\u4ecb\u3057\u307e\u3057\u3087\u3046\u3002

        "},{"location":"chapter_hashing/hash_collision/#1","title":"1. \u00a0 \u7dda\u5f62\u30d7\u30ed\u30fc\u30d6","text":"

        \u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u306f\u56fa\u5b9a\u30b9\u30c6\u30c3\u30d7\u306e\u7dda\u5f62\u691c\u7d22\u3092\u30d7\u30ed\u30fc\u30d6\u306b\u4f7f\u7528\u3057\u3001\u901a\u5e38\u306e\u30cf\u30c3\u30b7\u30e5\u8868\u3068\u306f\u7570\u306a\u308a\u307e\u3059\u3002

        • \u8981\u7d20\u306e\u633f\u5165: \u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97\u3057\u307e\u3059\u3002\u30d0\u30b1\u30c3\u30c8\u306b\u65e2\u306b\u8981\u7d20\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u885d\u7a81\u4f4d\u7f6e\u304b\u3089\u7dda\u5f62\u306b\u524d\u65b9\u306b\u8d70\u67fb\u3057\uff08\u901a\u5e38\u30b9\u30c6\u30c3\u30d7\u30b5\u30a4\u30ba\u306f\\(1\\)\uff09\u3001\u7a7a\u306e\u30d0\u30b1\u30c3\u30c8\u304c\u898b\u3064\u304b\u308b\u307e\u3067\u9032\u307f\u3001\u8981\u7d20\u3092\u633f\u5165\u3057\u307e\u3059\u3002
        • \u8981\u7d20\u306e\u691c\u7d22: \u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u540c\u3058\u30b9\u30c6\u30c3\u30d7\u30b5\u30a4\u30ba\u3092\u4f7f\u7528\u3057\u3066\u7dda\u5f62\u306b\u524d\u65b9\u306b\u8d70\u67fb\u3057\u3001\u5bfe\u5fdc\u3059\u308b\u8981\u7d20\u304c\u898b\u3064\u304b\u3063\u305f\u3089value\u3092\u8fd4\u3057\u307e\u3059\u3002\u7a7a\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u304c\u30cf\u30c3\u30b7\u30e5\u8868\u306b\u306a\u3044\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u305f\u3081\u3001None\u3092\u8fd4\u3057\u307e\u3059\u3002

        \u4e0b\u56f3\u306f\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\uff08\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\uff09\u30cf\u30c3\u30b7\u30e5\u8868\u306b\u304a\u3051\u308b\u30ad\u30fc\u5024\u30da\u30a2\u306e\u5206\u5e03\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306b\u3088\u308b\u3068\u3001\u4e0b\u4e8c\u6841\u304c\u540c\u3058\u30ad\u30fc\u306f\u540c\u3058\u30d0\u30b1\u30c3\u30c8\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3055\u308c\u307e\u3059\u3002\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u3092\u901a\u3057\u3066\u3001\u305d\u308c\u3089\u306f\u305d\u306e\u30d0\u30b1\u30c3\u30c8\u3068\u305d\u306e\u4e0b\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u9806\u6b21\u683c\u7d0d\u3055\u308c\u307e\u3059\u3002

        \u56f3 6-6 \u00a0 \u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\uff08\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\uff09\u30cf\u30c3\u30b7\u30e5\u8868\u306b\u304a\u3051\u308b\u30ad\u30fc\u5024\u30da\u30a2\u306e\u5206\u5e03

        \u3057\u304b\u3057\u3001\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u306f\u300c\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u300d\u3092\u4f5c\u308a\u3084\u3059\u3044\u50be\u5411\u304c\u3042\u308a\u307e\u3059\u3002\u5177\u4f53\u7684\u306b\u306f\u3001\u914d\u5217\u5185\u306e\u9023\u7d9a\u7684\u306b\u5360\u6709\u3055\u308c\u305f\u4f4d\u7f6e\u304c\u9577\u3044\u307b\u3069\u3001\u3053\u308c\u3089\u306e\u9023\u7d9a\u3057\u305f\u4f4d\u7f6e\u3067\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u304c\u767a\u751f\u3059\u308b\u78ba\u7387\u304c\u9ad8\u304f\u306a\u308a\u3001\u305d\u306e\u4f4d\u7f6e\u3067\u306e\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u306e\u6210\u9577\u3092\u3055\u3089\u306b\u4fc3\u9032\u3057\u3001\u60aa\u5faa\u74b0\u3092\u5f62\u6210\u3057\u3001\u6700\u7d42\u7684\u306b\u633f\u5165\u3001\u524a\u9664\u3001\u30af\u30a8\u30ea\u3001\u66f4\u65b0\u64cd\u4f5c\u306e\u52b9\u7387\u4f4e\u4e0b\u306b\u3064\u306a\u304c\u308a\u307e\u3059\u3002

        **\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u30cf\u30c3\u30b7\u30e5\u8868\u3067\u306f\u8981\u7d20\u3092\u76f4\u63a5\u524a\u9664\u3067\u304d\u306a\u3044**\u3053\u3068\u306b\u6ce8\u610f\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002\u8981\u7d20\u3092\u524a\u9664\u3059\u308b\u3068\u3001\u914d\u5217\u306b\u7a7a\u306e\u30d0\u30b1\u30c3\u30c8None\u304c\u4f5c\u6210\u3055\u308c\u307e\u3059\u3002\u8981\u7d20\u3092\u691c\u7d22\u3059\u308b\u969b\u3001\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u304c\u3053\u306e\u7a7a\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u906d\u9047\u3059\u308b\u3068\u623b\u3063\u3066\u3057\u307e\u3044\u3001\u3053\u306e\u30d0\u30b1\u30c3\u30c8\u306e\u4e0b\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u306a\u304f\u306a\u308a\u307e\u3059\u3002\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u3053\u308c\u3089\u306e\u8981\u7d20\u304c\u5b58\u5728\u3057\u306a\u3044\u3068\u8aa4\u3063\u3066\u4eee\u5b9a\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3068\u304a\u308a\u3067\u3059\u3002

        \u56f3 6-7 \u00a0 \u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u3067\u306e\u524a\u9664\u306b\u3088\u308b\u30af\u30a8\u30ea\u554f\u984c

        \u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3001\u9045\u5ef6\u524a\u9664\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u63a1\u7528\u3067\u304d\u307e\u3059\uff1a\u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u8981\u7d20\u3092\u76f4\u63a5\u524a\u9664\u3059\u308b\u4ee3\u308f\u308a\u306b\u3001\u5b9a\u6570TOMBSTONE\u3092\u4f7f\u7528\u3057\u3066\u30d0\u30b1\u30c3\u30c8\u3092\u30de\u30fc\u30af\u3057\u307e\u3059\u3002\u3053\u306e\u30e1\u30ab\u30cb\u30ba\u30e0\u3067\u306f\u3001None\u3068TOMBSTONE\u306e\u4e21\u65b9\u304c\u7a7a\u306e\u30d0\u30b1\u30c3\u30c8\u3092\u8868\u3057\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u4fdd\u6301\u3067\u304d\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u304cTOMBSTONE\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u305d\u306e\u4e0b\u306b\u307e\u3060\u30ad\u30fc\u5024\u30da\u30a2\u304c\u3042\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3001\u8d70\u67fb\u3092\u7d9a\u3051\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u3057\u304b\u3057\u3001\u9045\u5ef6\u524a\u9664\u306f\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u6027\u80fd\u52a3\u5316\u3092\u52a0\u901f\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u524a\u9664\u64cd\u4f5c\u306e\u305f\u3073\u306b\u524a\u9664\u30de\u30fc\u30af\u304c\u751f\u6210\u3055\u308c\u3001TOMBSTONE\u304c\u5897\u52a0\u3059\u308b\u3068\u3001\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u304c\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u3092\u898b\u3064\u3051\u308b\u305f\u3081\u306b\u8907\u6570\u306eTOMBSTONE\u3092\u30b9\u30ad\u30c3\u30d7\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3001\u691c\u7d22\u6642\u9593\u3082\u5897\u52a0\u3057\u307e\u3059\u3002

        \u3053\u308c\u306b\u5bfe\u51e6\u3059\u308b\u305f\u3081\u306b\u3001\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u4e2d\u306b\u6700\u521d\u306b\u906d\u9047\u3057\u305fTOMBSTONE\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a18\u9332\u3057\u3001\u691c\u7d22\u3055\u308c\u305f\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u3068\u305d\u306eTOMBSTONE\u306e\u4f4d\u7f6e\u3092\u4ea4\u63db\u3059\u308b\u3053\u3068\u3092\u691c\u8a0e\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3053\u308c\u3092\u884c\u3046\u5229\u70b9\u306f\u3001\u8981\u7d20\u304c\u30af\u30a8\u30ea\u307e\u305f\u306f\u8ffd\u52a0\u3055\u308c\u308b\u305f\u3073\u306b\u3001\u8981\u7d20\u304c\u305d\u306e\u7406\u60f3\u7684\u306a\u4f4d\u7f6e\uff08\u30d7\u30ed\u30fc\u30d6\u306e\u958b\u59cb\u70b9\uff09\u306b\u3088\u308a\u8fd1\u3044\u30d0\u30b1\u30c3\u30c8\u306b\u79fb\u52d5\u3055\u308c\u3001\u30af\u30a8\u30ea\u52b9\u7387\u304c\u6700\u9069\u5316\u3055\u308c\u308b\u3053\u3068\u3067\u3059\u3002

        \u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u306f\u3001\u9045\u5ef6\u524a\u9664\u3092\u4f7f\u7528\u3057\u305f\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\uff08\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\uff09\u30cf\u30c3\u30b7\u30e5\u8868\u3092\u5b9f\u88c5\u3057\u3066\u3044\u307e\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u7a7a\u9593\u3092\u3088\u308a\u6709\u52b9\u306b\u6d3b\u7528\u3059\u308b\u305f\u3081\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3092\u300c\u5faa\u74b0\u914d\u5217\u300d\u3068\u3057\u3066\u6271\u3044\u307e\u3059\u3002\u914d\u5217\u306e\u7d42\u308f\u308a\u3092\u8d85\u3048\u308b\u3068\u3001\u6700\u521d\u306b\u623b\u3063\u3066\u8d70\u67fb\u3092\u7d9a\u3051\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map_open_addressing.py
        class HashMapOpenAddressing:\n    \"\"\"\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\"\"\"\n\n    def __init__(self):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        self.size = 0  # \u30ad\u30fc\u5024\u30da\u30a2\u306e\u6570\n        self.capacity = 4  # \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306e\u5bb9\u91cf\n        self.load_thres = 2.0 / 3.0  # \u62e1\u5f35\u3092\u30c8\u30ea\u30ac\u30fc\u3059\u308b\u8ca0\u8377\u7387\u306e\u95be\u5024\n        self.extend_ratio = 2  # \u62e1\u5f35\u306e\u500d\u6570\n        self.buckets: list[Pair | None] = [None] * self.capacity  # \u30d0\u30b1\u30c3\u30c8\u914d\u5217\n        self.TOMBSTONE = Pair(-1, \"-1\")  # \u524a\u9664\u30de\u30fc\u30af\n\n    def hash_func(self, key: int) -> int:\n        \"\"\"\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\"\"\"\n        return key % self.capacity\n\n    def load_factor(self) -> float:\n        \"\"\"\u8ca0\u8377\u7387\"\"\"\n        return self.size / self.capacity\n\n    def find_bucket(self, key: int) -> int:\n        \"\"\"key \u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22\"\"\"\n        index = self.hash_func(key)\n        first_tombstone = -1\n        # \u7dda\u5f62\u63a2\u67fb\u3001\u7a7a\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u906d\u9047\u3057\u305f\u3089\u30d6\u30ec\u30fc\u30af\n        while self.buckets[index] is not None:\n            # \u30ad\u30fc\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n            if self.buckets[index].key == key:\n                # \u524a\u9664\u30de\u30fc\u30af\u304c\u4ee5\u524d\u306b\u906d\u9047\u3057\u3066\u3044\u305f\u5834\u5408\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u79fb\u52d5\n                if first_tombstone != -1:\n                    self.buckets[first_tombstone] = self.buckets[index]\n                    self.buckets[index] = self.TOMBSTONE\n                    return first_tombstone  # \u79fb\u52d5\u3055\u308c\u305f\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n                return index  # \u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n            # \u6700\u521d\u306b\u906d\u9047\u3057\u305f\u524a\u9664\u30de\u30fc\u30af\u3092\u8a18\u9332\n            if first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE:\n                first_tombstone = index\n            # \u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97\u3001\u672b\u5c3e\u3092\u8d85\u3048\u305f\u5834\u5408\u306f\u5148\u982d\u306b\u623b\u308b\n            index = (index + 1) % self.capacity\n        # \u30ad\u30fc\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001\u633f\u5165\u30dd\u30a4\u30f3\u30c8\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n        return index if first_tombstone == -1 else first_tombstone\n\n    def get(self, key: int) -> str:\n        \"\"\"\u7167\u4f1a\u64cd\u4f5c\"\"\"\n        # key \u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22\n        index = self.find_bucket(key)\n        # \u30ad\u30fc\u5024\u30da\u30a2\u304c\u898b\u3064\u304b\u308c\u3070\u3001\u5bfe\u5fdc\u3059\u308b val \u3092\u8fd4\u3059\n        if self.buckets[index] not in [None, self.TOMBSTONE]:\n            return self.buckets[index].val\n        # \u30ad\u30fc\u5024\u30da\u30a2\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001None \u3092\u8fd4\u3059\n        return None\n\n    def put(self, key: int, val: str):\n        \"\"\"\u8ffd\u52a0\u64cd\u4f5c\"\"\"\n        # \u8ca0\u8377\u7387\u304c\u95be\u5024\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u62e1\u5f35\u3092\u5b9f\u884c\n        if self.load_factor() > self.load_thres:\n            self.extend()\n        # key \u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22\n        index = self.find_bucket(key)\n        # \u30ad\u30fc\u5024\u30da\u30a2\u304c\u898b\u3064\u304b\u308c\u3070\u3001val \u3092\u4e0a\u66f8\u304d\u3057\u3066\u8fd4\u3059\n        if self.buckets[index] not in [None, self.TOMBSTONE]:\n            self.buckets[index].val = val\n            return\n        # \u30ad\u30fc\u5024\u30da\u30a2\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u8ffd\u52a0\n        self.buckets[index] = Pair(key, val)\n        self.size += 1\n\n    def remove(self, key: int):\n        \"\"\"\u524a\u9664\u64cd\u4f5c\"\"\"\n        # key \u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22\n        index = self.find_bucket(key)\n        # \u30ad\u30fc\u5024\u30da\u30a2\u304c\u898b\u3064\u304b\u308c\u3070\u3001\u524a\u9664\u30de\u30fc\u30af\u3067\u8986\u3046\n        if self.buckets[index] not in [None, self.TOMBSTONE]:\n            self.buckets[index] = self.TOMBSTONE\n            self.size -= 1\n\n    def extend(self):\n        \"\"\"\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u62e1\u5f35\"\"\"\n        # \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u4e00\u6642\u7684\u306b\u4fdd\u5b58\n        buckets_tmp = self.buckets\n        # \u62e1\u5f35\u3055\u308c\u305f\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n        self.capacity *= self.extend_ratio\n        self.buckets = [None] * self.capacity\n        self.size = 0\n        # \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306b\u30ad\u30fc\u5024\u30da\u30a2\u3092\u79fb\u52d5\n        for pair in buckets_tmp:\n            if pair not in [None, self.TOMBSTONE]:\n                self.put(pair.key, pair.val)\n\n    def print(self):\n        \"\"\"\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u51fa\u529b\"\"\"\n        for pair in self.buckets:\n            if pair is None:\n                print(\"None\")\n            elif pair is self.TOMBSTONE:\n                print(\"TOMBSTONE\")\n            else:\n                print(pair.key, \"->\", pair.val)\n
        hash_map_open_addressing.cpp
        /* \u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb */\nclass HashMapOpenAddressing {\n  private:\n    int size;                             // \u30ad\u30fc\u5024\u30da\u30a2\u306e\u6570\n    int capacity = 4;                     // \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306e\u5bb9\u91cf\n    const double loadThres = 2.0 / 3.0;     // \u62e1\u5f35\u3092\u30c8\u30ea\u30ac\u30fc\u3059\u308b\u8ca0\u8377\u7387\u306e\u95be\u5024\n    const int extendRatio = 2;            // \u62e1\u5f35\u500d\u7387\n    vector<Pair *> buckets;               // \u30d0\u30b1\u30c3\u30c8\u914d\u5217\n    Pair *TOMBSTONE = new Pair(-1, \"-1\"); // \u524a\u9664\u30de\u30fc\u30af\n\n  public:\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    HashMapOpenAddressing() : size(0), buckets(capacity, nullptr) {\n    }\n\n    /* \u30c7\u30b9\u30c8\u30e9\u30af\u30bf */\n    ~HashMapOpenAddressing() {\n        for (Pair *pair : buckets) {\n            if (pair != nullptr && pair != TOMBSTONE) {\n                delete pair;\n            }\n        }\n        delete TOMBSTONE;\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u95a2\u6570 */\n    int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u8ca0\u8377\u7387 */\n    double loadFactor() {\n        return (double)size / capacity;\n    }\n\n    /* key\u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22 */\n    int findBucket(int key) {\n        int index = hashFunc(key);\n        int firstTombstone = -1;\n        // \u7dda\u5f62\u63a2\u67fb\u3001\u7a7a\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u906d\u9047\u3057\u305f\u3089\u4e2d\u65ad\n        while (buckets[index] != nullptr) {\n            // key\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u5374\n            if (buckets[index]->key == key) {\n                // \u4ee5\u524d\u306b\u524a\u9664\u30de\u30fc\u30af\u306b\u906d\u9047\u3057\u3066\u3044\u305f\u5834\u5408\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u79fb\u52d5\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index];\n                    buckets[index] = TOMBSTONE;\n                    return firstTombstone; // \u79fb\u52d5\u3055\u308c\u305f\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u5374\n                }\n                return index; // \u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u5374\n            }\n            // \u6700\u521d\u306b\u906d\u9047\u3057\u305f\u524a\u9664\u30de\u30fc\u30af\u3092\u8a18\u9332\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index;\n            }\n            // \u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97\u3001\u672b\u5c3e\u3092\u8d85\u3048\u305f\u5834\u5408\u306f\u5148\u982d\u306b\u623b\u308b\n            index = (index + 1) % capacity;\n        }\n        // key\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001\u633f\u5165\u30dd\u30a4\u30f3\u30c8\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u5374\n        return firstTombstone == -1 ? index : firstTombstone;\n    }\n\n    /* \u30af\u30a8\u30ea\u64cd\u4f5c */\n    string get(int key) {\n        // key\u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22\n        int index = findBucket(key);\n        // \u30ad\u30fc\u5024\u30da\u30a2\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001\u5bfe\u5fdc\u3059\u308bval\u3092\u8fd4\u5374\n        if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n            return buckets[index]->val;\n        }\n        // \u30ad\u30fc\u5024\u30da\u30a2\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001\u7a7a\u6587\u5b57\u5217\u3092\u8fd4\u5374\n        return \"\";\n    }\n\n    /* \u8ffd\u52a0\u64cd\u4f5c */\n    void put(int key, string val) {\n        // \u8ca0\u8377\u7387\u304c\u95be\u5024\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u62e1\u5f35\u3092\u5b9f\u884c\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        // key\u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22\n        int index = findBucket(key);\n        // \u30ad\u30fc\u5024\u30da\u30a2\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001val\u3092\u4e0a\u66f8\u304d\u3057\u3066\u8fd4\u5374\n        if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n            buckets[index]->val = val;\n            return;\n        }\n        // \u30ad\u30fc\u5024\u30da\u30a2\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u8ffd\u52a0\n        buckets[index] = new Pair(key, val);\n        size++;\n    }\n\n    /* \u524a\u9664\u64cd\u4f5c */\n    void remove(int key) {\n        // key\u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22\n        int index = findBucket(key);\n        // \u30ad\u30fc\u5024\u30da\u30a2\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001\u524a\u9664\u30de\u30fc\u30af\u3067\u8986\u3046\n        if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n            delete buckets[index];\n            buckets[index] = TOMBSTONE;\n            size--;\n        }\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u62e1\u5f35 */\n    void extend() {\n        // \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u4e00\u6642\u4fdd\u5b58\n        vector<Pair *> bucketsTmp = buckets;\n        // \u62e1\u5f35\u3055\u308c\u305f\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n        capacity *= extendRatio;\n        buckets = vector<Pair *>(capacity, nullptr);\n        size = 0;\n        // \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306b\u30ad\u30fc\u5024\u30da\u30a2\u3092\u79fb\u52d5\n        for (Pair *pair : bucketsTmp) {\n            if (pair != nullptr && pair != TOMBSTONE) {\n                put(pair->key, pair->val);\n                delete pair;\n            }\n        }\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u5370\u5237 */\n    void print() {\n        for (Pair *pair : buckets) {\n            if (pair == nullptr) {\n                cout << \"nullptr\" << endl;\n            } else if (pair == TOMBSTONE) {\n                cout << \"TOMBSTONE\" << endl;\n            } else {\n                cout << pair->key << \" -> \" << pair->val << endl;\n            }\n        }\n    }\n};\n
        hash_map_open_addressing.java
        /* \u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb */\nclass HashMapOpenAddressing {\n    private int size; // \u30ad\u30fc\u5024\u30da\u30a2\u306e\u6570\n    private int capacity = 4; // \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306e\u5bb9\u91cf\n    private final double loadThres = 2.0 / 3.0; // \u62e1\u5f35\u3092\u30c8\u30ea\u30ac\u30fc\u3059\u308b\u8ca0\u8377\u7387\u306e\u95be\u5024\n    private final int extendRatio = 2; // \u62e1\u5f35\u500d\u7387\n    private Pair[] buckets; // \u30d0\u30b1\u30c3\u30c8\u914d\u5217\n    private final Pair TOMBSTONE = new Pair(-1, \"-1\"); // \u524a\u9664\u30de\u30fc\u30af\n\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    public HashMapOpenAddressing() {\n        size = 0;\n        buckets = new Pair[capacity];\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u95a2\u6570 */\n    private int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u8ca0\u8377\u7387 */\n    private double loadFactor() {\n        return (double) size / capacity;\n    }\n\n    /* key\u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22 */\n    private int findBucket(int key) {\n        int index = hashFunc(key);\n        int firstTombstone = -1;\n        // \u7dda\u5f62\u63a2\u67fb\u3001\u7a7a\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u906d\u9047\u3057\u305f\u3089\u7d42\u4e86\n        while (buckets[index] != null) {\n            // key\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n            if (buckets[index].key == key) {\n                // \u4ee5\u524d\u306b\u524a\u9664\u30de\u30fc\u30af\u306b\u906d\u9047\u3057\u3066\u3044\u305f\u5834\u5408\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u79fb\u52d5\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index];\n                    buckets[index] = TOMBSTONE;\n                    return firstTombstone; // \u79fb\u52d5\u5f8c\u306e\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n                }\n                return index; // \u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n            }\n            // \u6700\u521d\u306b\u906d\u9047\u3057\u305f\u524a\u9664\u30de\u30fc\u30af\u3092\u8a18\u9332\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index;\n            }\n            // \u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97\u3001\u672b\u5c3e\u3092\u8d85\u3048\u305f\u5834\u5408\u306f\u5148\u982d\u306b\u623b\u308b\n            index = (index + 1) % capacity;\n        }\n        // key\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001\u633f\u5165\u30dd\u30a4\u30f3\u30c8\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n        return firstTombstone == -1 ? index : firstTombstone;\n    }\n\n    /* \u30af\u30a8\u30ea\u64cd\u4f5c */\n    public String get(int key) {\n        // key\u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22\n        int index = findBucket(key);\n        // \u30ad\u30fc\u5024\u30da\u30a2\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001\u5bfe\u5fdc\u3059\u308bval\u3092\u8fd4\u3059\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            return buckets[index].val;\n        }\n        // \u30ad\u30fc\u5024\u30da\u30a2\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001null\u3092\u8fd4\u3059\n        return null;\n    }\n\n    /* \u8ffd\u52a0\u64cd\u4f5c */\n    public void put(int key, String val) {\n        // \u8ca0\u8377\u7387\u304c\u95be\u5024\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u62e1\u5f35\u3092\u5b9f\u884c\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        // key\u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22\n        int index = findBucket(key);\n        // \u30ad\u30fc\u5024\u30da\u30a2\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001val\u3092\u4e0a\u66f8\u304d\u3057\u3066\u623b\u308b\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index].val = val;\n            return;\n        }\n        // \u30ad\u30fc\u5024\u30da\u30a2\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001\u30ad\u30fc\u5024\u30da\u30a2\u3092\u8ffd\u52a0\n        buckets[index] = new Pair(key, val);\n        size++;\n    }\n\n    /* \u524a\u9664\u64cd\u4f5c */\n    public void remove(int key) {\n        // key\u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u7d22\n        int index = findBucket(key);\n        // \u30ad\u30fc\u5024\u30da\u30a2\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001\u524a\u9664\u30de\u30fc\u30af\u3067\u8986\u3046\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index] = TOMBSTONE;\n            size--;\n        }\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u62e1\u5f35 */\n    private void extend() {\n        // \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u4e00\u6642\u7684\u306b\u4fdd\u5b58\n        Pair[] bucketsTmp = buckets;\n        // \u62e1\u5f35\u3055\u308c\u305f\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\n        capacity *= extendRatio;\n        buckets = new Pair[capacity];\n        size = 0;\n        // \u5143\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u65b0\u3057\u3044\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306b\u30ad\u30fc\u5024\u30da\u30a2\u3092\u79fb\u52d5\n        for (Pair pair : bucketsTmp) {\n            if (pair != null && pair != TOMBSTONE) {\n                put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u5370\u5237 */\n    public void print() {\n        for (Pair pair : buckets) {\n            if (pair == null) {\n                System.out.println(\"null\");\n            } else if (pair == TOMBSTONE) {\n                System.out.println(\"TOMBSTONE\");\n            } else {\n                System.out.println(pair.key + \" -> \" + pair.val);\n            }\n        }\n    }\n}\n
        hash_map_open_addressing.cs
        [class]{HashMapOpenAddressing}-[func]{}\n
        hash_map_open_addressing.go
        [class]{hashMapOpenAddressing}-[func]{}\n
        hash_map_open_addressing.swift
        [class]{HashMapOpenAddressing}-[func]{}\n
        hash_map_open_addressing.js
        [class]{HashMapOpenAddressing}-[func]{}\n
        hash_map_open_addressing.ts
        [class]{HashMapOpenAddressing}-[func]{}\n
        hash_map_open_addressing.dart
        [class]{HashMapOpenAddressing}-[func]{}\n
        hash_map_open_addressing.rs
        [class]{HashMapOpenAddressing}-[func]{}\n
        hash_map_open_addressing.c
        [class]{HashMapOpenAddressing}-[func]{}\n
        hash_map_open_addressing.kt
        [class]{HashMapOpenAddressing}-[func]{}\n
        hash_map_open_addressing.rb
        [class]{HashMapOpenAddressing}-[func]{}\n
        "},{"location":"chapter_hashing/hash_collision/#2","title":"2. \u00a0 \u4e8c\u6b21\u30d7\u30ed\u30fc\u30d6","text":"

        \u4e8c\u6b21\u30d7\u30ed\u30fc\u30d6\u306f\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u306b\u4f3c\u3066\u304a\u308a\u3001\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u306e\u4e00\u822c\u7684\u306a\u6226\u7565\u306e1\u3064\u3067\u3059\u3002\u885d\u7a81\u304c\u767a\u751f\u3057\u305f\u5834\u5408\u3001\u4e8c\u6b21\u30d7\u30ed\u30fc\u30d6\u306f\u5358\u7d14\u306b\u56fa\u5b9a\u30b9\u30c6\u30c3\u30d7\u6570\u3092\u30b9\u30ad\u30c3\u30d7\u3059\u308b\u306e\u3067\u306f\u306a\u304f\u3001\u300c\u30d7\u30ed\u30fc\u30d6\u56de\u6570\u306e\u4e8c\u4e57\u300d\u306b\u7b49\u3057\u3044\u30b9\u30c6\u30c3\u30d7\u6570\u3001\u3064\u307e\u308a\\(1, 4, 9, \\dots\\)\u30b9\u30c6\u30c3\u30d7\u3092\u30b9\u30ad\u30c3\u30d7\u3057\u307e\u3059\u3002

        \u4e8c\u6b21\u30d7\u30ed\u30fc\u30d6\u306b\u306f\u4ee5\u4e0b\u306e\u5229\u70b9\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u4e8c\u6b21\u30d7\u30ed\u30fc\u30d6\u306f\u3001\u30d7\u30ed\u30fc\u30d6\u56de\u6570\u306e\u4e8c\u4e57\u306e\u8ddd\u96e2\u3092\u30b9\u30ad\u30c3\u30d7\u3059\u308b\u3053\u3068\u3067\u3001\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u306e\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u52b9\u679c\u3092\u8efd\u6e1b\u3057\u3088\u3046\u3068\u3057\u307e\u3059\u3002
        • \u4e8c\u6b21\u30d7\u30ed\u30fc\u30d6\u306f\u3088\u308a\u5927\u304d\u306a\u8ddd\u96e2\u3092\u30b9\u30ad\u30c3\u30d7\u3057\u3066\u7a7a\u306e\u4f4d\u7f6e\u3092\u898b\u3064\u3051\u3001\u30c7\u30fc\u30bf\u3092\u3088\u308a\u5747\u7b49\u306b\u5206\u6563\u3059\u308b\u306e\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002

        \u3057\u304b\u3057\u3001\u4e8c\u6b21\u30d7\u30ed\u30fc\u30d6\u306f\u5b8c\u74a7\u3067\u306f\u3042\u308a\u307e\u305b\u3093\uff1a

        • \u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u306f\u4f9d\u7136\u3068\u3057\u3066\u5b58\u5728\u3057\u3001\u3064\u307e\u308a\u4e00\u90e8\u306e\u4f4d\u7f6e\u306f\u4ed6\u306e\u4f4d\u7f6e\u3088\u308a\u3082\u5360\u6709\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u9ad8\u3044\u3067\u3059\u3002
        • \u4e8c\u4e57\u306e\u6210\u9577\u306b\u3088\u308a\u3001\u4e8c\u6b21\u30d7\u30ed\u30fc\u30d6\u306f\u30cf\u30c3\u30b7\u30e5\u8868\u5168\u4f53\u3092\u30d7\u30ed\u30fc\u30d6\u3067\u304d\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306b\u7a7a\u306e\u30d0\u30b1\u30c3\u30c8\u304c\u3042\u3063\u3066\u3082\u3001\u4e8c\u6b21\u30d7\u30ed\u30fc\u30d6\u304c\u30a2\u30af\u30bb\u30b9\u3067\u304d\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        "},{"location":"chapter_hashing/hash_collision/#3","title":"3. \u00a0 \u4e8c\u91cd\u30cf\u30c3\u30b7\u30e5","text":"

        \u540d\u524d\u304c\u793a\u3059\u3088\u3046\u306b\u3001\u4e8c\u91cd\u30cf\u30c3\u30b7\u30e5\u6cd5\u306f\u8907\u6570\u306e\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\\(f_1(x)\\)\u3001\\(f_2(x)\\)\u3001\\(f_3(x)\\)\u3001\\(\\dots\\)\u3092\u30d7\u30ed\u30fc\u30d6\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002

        • \u8981\u7d20\u306e\u633f\u5165: \u30cf\u30c3\u30b7\u30e5\u95a2\u6570\\(f_1(x)\\)\u304c\u885d\u7a81\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\\(f_2(x)\\)\u3092\u8a66\u3057\u3001\u4ee5\u4e0b\u540c\u69d8\u306b\u3001\u7a7a\u306e\u4f4d\u7f6e\u304c\u898b\u3064\u304b\u3063\u3066\u8981\u7d20\u304c\u633f\u5165\u3055\u308c\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002
        • \u8981\u7d20\u306e\u691c\u7d22: \u540c\u3058\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u9806\u5e8f\u3067\u691c\u7d22\u3057\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u304c\u898b\u3064\u304b\u3063\u3066\u8fd4\u3055\u308c\u308b\u307e\u3067\u3001\u307e\u305f\u306f\u7a7a\u306e\u4f4d\u7f6e\u306b\u906d\u9047\u3059\u308b\u304b\u3059\u3079\u3066\u306e\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u304c\u8a66\u3055\u308c\u308b\u307e\u3067\u7d9a\u3051\u3001\u8981\u7d20\u304c\u30cf\u30c3\u30b7\u30e5\u8868\u306b\u306a\u3044\u3053\u3068\u3092\u793a\u3057\u3001None\u3092\u8fd4\u3057\u307e\u3059\u3002

        \u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u3068\u6bd4\u8f03\u3057\u3066\u3001\u4e8c\u91cd\u30cf\u30c3\u30b7\u30e5\u6cd5\u306f\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u304c\u8d77\u3053\u308a\u306b\u304f\u3044\u3067\u3059\u304c\u3001\u8907\u6570\u306e\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306f\u8ffd\u52a0\u306e\u8a08\u7b97\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u3092\u5c0e\u5165\u3057\u307e\u3059\u3002

        Tip

        \u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\uff08\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u3001\u4e8c\u6b21\u30d7\u30ed\u30fc\u30d6\u3001\u4e8c\u91cd\u30cf\u30c3\u30b7\u30e5\uff09\u30cf\u30c3\u30b7\u30e5\u8868\u306f\u3059\u3079\u3066\u300c\u8981\u7d20\u3092\u76f4\u63a5\u524a\u9664\u3067\u304d\u306a\u3044\u300d\u3068\u3044\u3046\u554f\u984c\u304c\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        "},{"location":"chapter_hashing/hash_collision/#623","title":"6.2.3 \u00a0 \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u9078\u629e","text":"

        \u7570\u306a\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306f\u7570\u306a\u308b\u30cf\u30c3\u30b7\u30e5\u8868\u5b9f\u88c5\u6226\u7565\u3092\u63a1\u7528\u3057\u3066\u3044\u307e\u3059\u3002\u4ee5\u4e0b\u306b\u3044\u304f\u3064\u304b\u306e\u4f8b\u3092\u793a\u3057\u307e\u3059\uff1a

        • Python\u306f\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002dict\u8f9e\u66f8\u306f\u30d7\u30ed\u30fc\u30d6\u306b\u7591\u4f3c\u4e71\u6570\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • Java\u306f\u9023\u9396\u6cd5\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002JDK 1.8\u4ee5\u964d\u3001HashMap\u306e\u914d\u5217\u9577\u304c64\u306b\u9054\u3057\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u9577\u3055\u304c8\u306b\u9054\u3059\u308b\u3068\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u691c\u7d22\u6027\u80fd\u3092\u5411\u4e0a\u3055\u305b\u308b\u305f\u3081\u306b\u8d64\u9ed2\u6728\u306b\u5909\u63db\u3055\u308c\u307e\u3059\u3002
        • Go\u306f\u9023\u9396\u6cd5\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002Go\u306f\u5404\u30d0\u30b1\u30c3\u30c8\u304c\u6700\u59278\u3064\u306e\u30ad\u30fc\u5024\u30da\u30a2\u3092\u683c\u7d0d\u3067\u304d\u308b\u3053\u3068\u3092\u898f\u5b9a\u3057\u3001\u5bb9\u91cf\u3092\u8d85\u3048\u305f\u5834\u5408\u306f\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u30d0\u30b1\u30c3\u30c8\u304c\u9023\u7d50\u3055\u308c\u307e\u3059\u3002\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u30d0\u30b1\u30c3\u30c8\u304c\u591a\u3059\u304e\u308b\u5834\u5408\u3001\u6027\u80fd\u3092\u78ba\u4fdd\u3059\u308b\u305f\u3081\u306b\u7279\u5225\u306a\u7b49\u5bb9\u91cf\u30ea\u30b5\u30a4\u30ba\u64cd\u4f5c\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002
        "},{"location":"chapter_hashing/hash_map/","title":"6.1 \u00a0 \u30cf\u30c3\u30b7\u30e5\u8868","text":"

        \u30cf\u30c3\u30b7\u30e5\u8868\u306f\u30cf\u30c3\u30b7\u30e5\u30de\u30c3\u30d7\u3068\u3082\u547c\u3070\u308c\u3001\u30ad\u30fc\u3068\u5024\u306e\u9593\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u3092\u78ba\u7acb\u3057\u3001\u52b9\u7387\u7684\u306a\u8981\u7d20\u306e\u53d6\u5f97\u3092\u53ef\u80fd\u306b\u3059\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3059\u3002\u5177\u4f53\u7684\u306b\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306bkey\u3092\u5165\u529b\u3059\u308b\u3068\u3001\\(O(1)\\)\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3067\u5bfe\u5fdc\u3059\u308bvalue\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\\(n\\)\u4eba\u306e\u5b66\u751f\u304c\u3044\u3066\u3001\u5404\u5b66\u751f\u306b\u306f\u300c\u540d\u524d\u300d\u3068\u300c\u5b66\u7c4d\u756a\u53f7\u300d\u306e2\u3064\u306e\u30c7\u30fc\u30bf\u30d5\u30a3\u30fc\u30eb\u30c9\u304c\u3042\u308b\u3068\u3057\u307e\u3059\u3002\u5b66\u7c4d\u756a\u53f7\u3092\u5165\u529b\u3068\u3057\u3066\u5bfe\u5fdc\u3059\u308b\u540d\u524d\u3092\u8fd4\u3059\u30af\u30a8\u30ea\u6a5f\u80fd\u3092\u5b9f\u88c5\u3057\u305f\u3044\u5834\u5408\u3001\u4e0b\u56f3\u306b\u793a\u3059\u30cf\u30c3\u30b7\u30e5\u8868\u3092\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002

        \u56f3 6-1 \u00a0 \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u62bd\u8c61\u7684\u306a\u8868\u73fe

        \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u52a0\u3048\u3066\u3001\u914d\u5217\u3084\u9023\u7d50\u30ea\u30b9\u30c8\u3082\u30af\u30a8\u30ea\u6a5f\u80fd\u306e\u5b9f\u88c5\u306b\u4f7f\u7528\u3067\u304d\u307e\u3059\u304c\u3001\u6642\u9593\u8a08\u7b97\u91cf\u304c\u7570\u306a\u308a\u307e\u3059\u3002\u52b9\u7387\u306f\u4ee5\u4e0b\u306e\u8868\u3067\u6bd4\u8f03\u3055\u308c\u3066\u3044\u307e\u3059\uff1a

        • \u8981\u7d20\u306e\u633f\u5165: \u914d\u5217\uff08\u307e\u305f\u306f\u9023\u7d50\u30ea\u30b9\u30c8\uff09\u306e\u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0\u3059\u308b\u3060\u3051\u3067\u3059\u3002\u3053\u306e\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u3067\u3059\u3002
        • \u8981\u7d20\u306e\u691c\u7d22: \u914d\u5217\uff08\u307e\u305f\u306f\u9023\u7d50\u30ea\u30b9\u30c8\uff09\u304c\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u306a\u3044\u305f\u3081\u3001\u8981\u7d20\u3092\u691c\u7d22\u3059\u308b\u306b\u306f\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u8d70\u67fb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3059\u3002
        • \u8981\u7d20\u306e\u524a\u9664: \u8981\u7d20\u3092\u524a\u9664\u3059\u308b\u306b\u306f\u3001\u307e\u305a\u305d\u306e\u8981\u7d20\u3092\u898b\u3064\u3051\u3066\u304b\u3089\u3001\u914d\u5217\uff08\u307e\u305f\u306f\u9023\u7d50\u30ea\u30b9\u30c8\uff09\u304b\u3089\u524a\u9664\u3057\u307e\u3059\u3002\u3053\u306e\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3059\u3002

        \u8868 6-1 \u00a0 \u4e00\u822c\u7684\u306a\u64cd\u4f5c\u306e\u6642\u9593\u52b9\u7387\u306e\u6bd4\u8f03

        \u914d\u5217 \u9023\u7d50\u30ea\u30b9\u30c8 \u30cf\u30c3\u30b7\u30e5\u8868 \u8981\u7d20\u306e\u691c\u7d22 \\(O(n)\\) \\(O(n)\\) \\(O(1)\\) \u8981\u7d20\u306e\u633f\u5165 \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u8981\u7d20\u306e\u524a\u9664 \\(O(n)\\) \\(O(n)\\) \\(O(1)\\)

        \u89b3\u5bdf\u3055\u308c\u308b\u3088\u3046\u306b\u3001**\u30cf\u30c3\u30b7\u30e5\u8868\u306b\u304a\u3051\u308b\u64cd\u4f5c\uff08\u633f\u5165\u3001\u524a\u9664\u3001\u691c\u7d22\u3001\u5909\u66f4\uff09\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)**\u3067\u3001\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3059\u3002

        "},{"location":"chapter_hashing/hash_map/#611","title":"6.1.1 \u00a0 \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c","text":"

        \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c\u306b\u306f\u3001\u521d\u671f\u5316\u3001\u30af\u30a8\u30ea\u3001\u30ad\u30fc\u5024\u30da\u30a2\u306e\u8ffd\u52a0\u3001\u30ad\u30fc\u5024\u30da\u30a2\u306e\u524a\u9664\u304c\u3042\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306f\u30b3\u30fc\u30c9\u4f8b\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin hash_map.py
        # \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u521d\u671f\u5316\nhmap: dict = {}\n\n# \u8ffd\u52a0\u64cd\u4f5c\n# \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u8ffd\u52a0\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u5570\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9e2d\"\n\n# \u30af\u30a8\u30ea\u64cd\u4f5c\n# \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u3092\u5165\u529b\u3057\u3001\u5024\u3092\u53d6\u5f97\nname: str = hmap[15937]\n\n# \u524a\u9664\u64cd\u4f5c\n# \u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u524a\u9664\nhmap.pop(10583)\n
        hash_map.cpp
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u521d\u671f\u5316 */\nunordered_map<int, string> map;\n\n/* \u8ffd\u52a0\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u8ffd\u52a0\nmap[12836] = \"\u5c0f\u54c8\";\nmap[15937] = \"\u5c0f\u5570\";\nmap[16750] = \"\u5c0f\u7b97\";\nmap[13276] = \"\u5c0f\u6cd5\";\nmap[10583] = \"\u5c0f\u9e2d\";\n\n/* \u30af\u30a8\u30ea\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u3092\u5165\u529b\u3057\u3001\u5024\u3092\u53d6\u5f97\nstring name = map[15937];\n\n/* \u524a\u9664\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u524a\u9664\nmap.erase(10583);\n
        hash_map.java
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u521d\u671f\u5316 */\nMap<Integer, String> map = new HashMap<>();\n\n/* \u8ffd\u52a0\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u8ffd\u52a0\nmap.put(12836, \"\u5c0f\u54c8\");\nmap.put(15937, \"\u5c0f\u5570\");\nmap.put(16750, \"\u5c0f\u7b97\");\nmap.put(13276, \"\u5c0f\u6cd5\");\nmap.put(10583, \"\u5c0f\u9e2d\");\n\n/* \u30af\u30a8\u30ea\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u3092\u5165\u529b\u3057\u3001\u5024\u3092\u53d6\u5f97\nString name = map.get(15937);\n\n/* \u524a\u9664\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u524a\u9664\nmap.remove(10583);\n
        hash_map.cs
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u521d\u671f\u5316 */\nDictionary<int, string> map = new() {\n    /* \u8ffd\u52a0\u64cd\u4f5c */\n    // \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u8ffd\u52a0\n    { 12836, \"\u5c0f\u54c8\" },\n    { 15937, \"\u5c0f\u5570\" },\n    { 16750, \"\u5c0f\u7b97\" },\n    { 13276, \"\u5c0f\u6cd5\" },\n    { 10583, \"\u5c0f\u9e2d\" }\n};\n\n/* \u30af\u30a8\u30ea\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u3092\u5165\u529b\u3057\u3001\u5024\u3092\u53d6\u5f97\nstring name = map[15937];\n\n/* \u524a\u9664\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u524a\u9664\nmap.Remove(10583);\n
        hash_map_test.go
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u521d\u671f\u5316 */\nhmap := make(map[int]string)\n\n/* \u8ffd\u52a0\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u8ffd\u52a0\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u5570\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9e2d\"\n\n/* \u30af\u30a8\u30ea\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u3092\u5165\u529b\u3057\u3001\u5024\u3092\u53d6\u5f97\nname := hmap[15937]\n\n/* \u524a\u9664\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u524a\u9664\ndelete(hmap, 10583)\n
        hash_map.swift
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u521d\u671f\u5316 */\nvar map: [Int: String] = [:]\n\n/* \u8ffd\u52a0\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u8ffd\u52a0\nmap[12836] = \"\u5c0f\u54c8\"\nmap[15937] = \"\u5c0f\u5570\"\nmap[16750] = \"\u5c0f\u7b97\"\nmap[13276] = \"\u5c0f\u6cd5\"\nmap[10583] = \"\u5c0f\u9e2d\"\n\n/* \u30af\u30a8\u30ea\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u3092\u5165\u529b\u3057\u3001\u5024\u3092\u53d6\u5f97\nlet name = map[15937]!\n\n/* \u524a\u9664\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u524a\u9664\nmap.removeValue(forKey: 10583)\n
        hash_map.js
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u521d\u671f\u5316 */\nconst map = new Map();\n/* \u8ffd\u52a0\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u8ffd\u52a0\nmap.set(12836, '\u5c0f\u54c8');\nmap.set(15937, '\u5c0f\u5570');\nmap.set(16750, '\u5c0f\u7b97');\nmap.set(13276, '\u5c0f\u6cd5');\nmap.set(10583, '\u5c0f\u9e2d');\n\n/* \u30af\u30a8\u30ea\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u3092\u5165\u529b\u3057\u3001\u5024\u3092\u53d6\u5f97\nlet name = map.get(15937);\n\n/* \u524a\u9664\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u524a\u9664\nmap.delete(10583);\n
        hash_map.ts
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u521d\u671f\u5316 */\nconst map = new Map<number, string>();\n/* \u8ffd\u52a0\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u8ffd\u52a0\nmap.set(12836, '\u5c0f\u54c8');\nmap.set(15937, '\u5c0f\u5570');\nmap.set(16750, '\u5c0f\u7b97');\nmap.set(13276, '\u5c0f\u6cd5');\nmap.set(10583, '\u5c0f\u9e2d');\nconsole.info('\\n\u8ffd\u52a0\u5f8c\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306f\\nKey -> Value');\nconsole.info(map);\n\n/* \u30af\u30a8\u30ea\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u3092\u5165\u529b\u3057\u3001\u5024\u3092\u53d6\u5f97\nlet name = map.get(15937);\nconsole.info('\\n\u5b66\u7c4d\u756a\u53f715937\u3092\u5165\u529b\u3001\u540d\u524d\u3092\u554f\u3044\u5408\u308f\u305b ' + name);\n\n/* \u524a\u9664\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u524a\u9664\nmap.delete(10583);\nconsole.info('\\n10583\u3092\u524a\u9664\u5f8c\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306f\\nKey -> Value');\nconsole.info(map);\n
        hash_map.dart
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u521d\u671f\u5316 */\nMap<int, String> map = {};\n\n/* \u8ffd\u52a0\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u8ffd\u52a0\nmap[12836] = \"\u5c0f\u54c8\";\nmap[15937] = \"\u5c0f\u5570\";\nmap[16750] = \"\u5c0f\u7b97\";\nmap[13276] = \"\u5c0f\u6cd5\";\nmap[10583] = \"\u5c0f\u9e2d\";\n\n/* \u30af\u30a8\u30ea\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u3092\u5165\u529b\u3057\u3001\u5024\u3092\u53d6\u5f97\nString name = map[15937];\n\n/* \u524a\u9664\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u524a\u9664\nmap.remove(10583);\n
        hash_map.rs
        use std::collections::HashMap;\n\n/* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u521d\u671f\u5316 */\nlet mut map: HashMap<i32, String> = HashMap::new();\n\n/* \u8ffd\u52a0\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u8ffd\u52a0\nmap.insert(12836, \"\u5c0f\u54c8\".to_string());\nmap.insert(15937, \"\u5c0f\u5570\".to_string());\nmap.insert(16750, \"\u5c0f\u7b97\".to_string());\nmap.insert(13279, \"\u5c0f\u6cd5\".to_string());\nmap.insert(10583, \"\u5c0f\u9e2d\".to_string());\n\n/* \u30af\u30a8\u30ea\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u306b\u30ad\u30fc\u3092\u5165\u529b\u3057\u3001\u5024\u3092\u53d6\u5f97\nlet _name: Option<&String> = map.get(&15937);\n\n/* \u524a\u9664\u64cd\u4f5c */\n// \u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u30ad\u30fc\u5024\u30da\u30a2 (key, value) \u3092\u524a\u9664\nlet _removed_value: Option<String> = map.remove(&10583);\n
        hash_map.c
        // C\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30cf\u30c3\u30b7\u30e5\u8868\u304c\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u305b\u3093\n
        hash_map.kt
        \n

        \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u8d70\u67fb\u3059\u308b\u4e00\u822c\u7684\u306a\u65b9\u6cd5\u306f3\u3064\u3042\u308a\u307e\u3059\uff1a\u30ad\u30fc\u5024\u30da\u30a2\u306e\u8d70\u67fb\u3001\u30ad\u30fc\u306e\u8d70\u67fb\u3001\u5024\u306e\u8d70\u67fb\u3002\u4ee5\u4e0b\u306f\u30b3\u30fc\u30c9\u4f8b\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin hash_map.py
        # \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u8d70\u67fb\n# \u30ad\u30fc\u5024\u30da\u30a2 key->value \u3092\u8d70\u67fb\nfor key, value in hmap.items():\n    print(key, \"->\", value)\n# \u30ad\u30fc\u306e\u307f\u3092\u8d70\u67fb\nfor key in hmap.keys():\n    print(key)\n# \u5024\u306e\u307f\u3092\u8d70\u67fb\nfor value in hmap.values():\n    print(value)\n
        hash_map.cpp
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u8d70\u67fb */\n// \u30ad\u30fc\u5024\u30da\u30a2 key->value \u3092\u8d70\u67fb\nfor (auto kv: map) {\n    cout << kv.first << \" -> \" << kv.second << endl;\n}\n// \u30a4\u30c6\u30ec\u30fc\u30bf\u3092\u4f7f\u7528\u3057\u3066\u30ad\u30fc\u5024\u30da\u30a2 key->value \u3092\u8d70\u67fb\nfor (auto iter = map.begin(); iter != map.end(); iter++) {\n    cout << iter->first << \"->\" << iter->second << endl;\n}\n
        hash_map.java
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u8d70\u67fb */\n// \u30ad\u30fc\u5024\u30da\u30a2 key->value \u3092\u8d70\u67fb\nfor (Map.Entry<Integer, String> kv: map.entrySet()) {\n    System.out.println(kv.getKey() + \" -> \" + kv.getValue());\n}\n// \u30ad\u30fc\u306e\u307f\u3092\u8d70\u67fb\nfor (int key: map.keySet()) {\n    System.out.println(key);\n}\n// \u5024\u306e\u307f\u3092\u8d70\u67fb\nfor (String val: map.values()) {\n    System.out.println(val);\n}\n
        hash_map.cs
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u8d70\u67fb */\n// \u30ad\u30fc\u5024\u30da\u30a2 Key->Value \u3092\u8d70\u67fb\nforeach (var kv in map) {\n    Console.WriteLine(kv.Key + \" -> \" + kv.Value);\n}\n// \u30ad\u30fc\u306e\u307f\u3092\u8d70\u67fb\nforeach (int key in map.Keys) {\n    Console.WriteLine(key);\n}\n// \u5024\u306e\u307f\u3092\u8d70\u67fb\nforeach (string val in map.Values) {\n    Console.WriteLine(val);\n}\n
        hash_map_test.go
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u8d70\u67fb */\n// \u30ad\u30fc\u5024\u30da\u30a2 key->value \u3092\u8d70\u67fb\nfor key, value := range hmap {\n    fmt.Println(key, \"->\", value)\n}\n// \u30ad\u30fc\u306e\u307f\u3092\u8d70\u67fb\nfor key := range hmap {\n    fmt.Println(key)\n}\n// \u5024\u306e\u307f\u3092\u8d70\u67fb\nfor _, value := range hmap {\n    fmt.Println(value)\n}\n
        hash_map.swift
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u8d70\u67fb */\n// \u30ad\u30fc\u5024\u30da\u30a2 Key->Value \u3092\u8d70\u67fb\nfor (key, value) in map {\n    print(\"\\(key) -> \\(value)\")\n}\n// \u30ad\u30fc\u306e\u307f\u3092\u8d70\u67fb\nfor key in map.keys {\n    print(key)\n}\n// \u5024\u306e\u307f\u3092\u8d70\u67fb\nfor value in map.values {\n    print(value)\n}\n
        hash_map.js
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u8d70\u67fb */\nconsole.info('\\n\u30ad\u30fc\u5024\u30da\u30a2 Key->Value \u3092\u8d70\u67fb');\nfor (const [k, v] of map.entries()) {\n    console.info(k + ' -> ' + v);\n}\nconsole.info('\\n\u30ad\u30fc\u306e\u307f\u3092\u8d70\u67fb Key');\nfor (const k of map.keys()) {\n    console.info(k);\n}\nconsole.info('\\n\u5024\u306e\u307f\u3092\u8d70\u67fb Value');\nfor (const v of map.values()) {\n    console.info(v);\n}\n
        hash_map.ts
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u8d70\u67fb */\nconsole.info('\\n\u30ad\u30fc\u5024\u30da\u30a2 Key->Value \u3092\u8d70\u67fb');\nfor (const [k, v] of map.entries()) {\n    console.info(k + ' -> ' + v);\n}\nconsole.info('\\n\u30ad\u30fc\u306e\u307f\u3092\u8d70\u67fb Key');\nfor (const k of map.keys()) {\n    console.info(k);\n}\nconsole.info('\\n\u5024\u306e\u307f\u3092\u8d70\u67fb Value');\nfor (const v of map.values()) {\n    console.info(v);\n}\n
        hash_map.dart
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u8d70\u67fb */\n// \u30ad\u30fc\u5024\u30da\u30a2 Key->Value \u3092\u8d70\u67fb\nmap.forEach((key, value) {\nprint('$key -> $value');\n});\n\n// \u30ad\u30fc\u306e\u307f\u3092\u8d70\u67fb Key\nmap.keys.forEach((key) {\nprint(key);\n});\n\n// \u5024\u306e\u307f\u3092\u8d70\u67fb Value\nmap.values.forEach((value) {\nprint(value);\n});\n
        hash_map.rs
        /* \u30cf\u30c3\u30b7\u30e5\u8868\u3092\u8d70\u67fb */\n// \u30ad\u30fc\u5024\u30da\u30a2 Key->Value \u3092\u8d70\u67fb\nfor (key, value) in &map {\n    println!(\"{key} -> {value}\");\n}\n\n// \u30ad\u30fc\u306e\u307f\u3092\u8d70\u67fb Key\nfor key in map.keys() {\n    println!(\"{key}\");\n}\n\n// \u5024\u306e\u307f\u3092\u8d70\u67fb Value\nfor value in map.values() {\n    println!(\"{value}\");\n}\n
        hash_map.c
        // C\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30cf\u30c3\u30b7\u30e5\u8868\u304c\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u305b\u3093\n
        hash_map.kt
        \n
        "},{"location":"chapter_hashing/hash_map/#612","title":"6.1.2 \u00a0 \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u7c21\u5358\u306a\u5b9f\u88c5","text":"

        \u307e\u305a\u3001\u6700\u3082\u7c21\u5358\u306a\u30b1\u30fc\u30b9\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\uff1a\u914d\u5217\u306e\u307f\u3092\u4f7f\u3063\u3066\u30cf\u30c3\u30b7\u30e5\u8868\u3092\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3002\u30cf\u30c3\u30b7\u30e5\u8868\u306b\u304a\u3044\u3066\u3001\u914d\u5217\u306e\u5404\u7a7a\u304d\u30b9\u30ed\u30c3\u30c8\u306f\u30d0\u30b1\u30c3\u30c8\u3068\u547c\u3070\u308c\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u306f\u30ad\u30fc\u5024\u30da\u30a2\u3092\u683c\u7d0d\u3067\u304d\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30af\u30a8\u30ea\u64cd\u4f5c\u306fkey\u306b\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u3092\u898b\u3064\u3051\u3001\u305d\u3053\u304b\u3089value\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002

        \u3067\u306f\u3001key\u306b\u57fa\u3065\u3044\u3066\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u3092\u3069\u306e\u3088\u3046\u306b\u7279\u5b9a\u3059\u308b\u306e\u3067\u3057\u3087\u3046\u304b\uff1f\u3053\u308c\u306f\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306b\u3088\u3063\u3066\u5b9f\u73fe\u3055\u308c\u307e\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u5f79\u5272\u306f\u3001\u3088\u308a\u5927\u304d\u306a\u5165\u529b\u7a7a\u9593\u3092\u3088\u308a\u5c0f\u3055\u306a\u51fa\u529b\u7a7a\u9593\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u8868\u3067\u306f\u3001\u5165\u529b\u7a7a\u9593\u306f\u3059\u3079\u3066\u306e\u30ad\u30fc\u3067\u69cb\u6210\u3055\u308c\u3001\u51fa\u529b\u7a7a\u9593\u306f\u3059\u3079\u3066\u306e\u30d0\u30b1\u30c3\u30c8\uff08\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\uff09\u3067\u69cb\u6210\u3055\u308c\u307e\u3059\u3002\u3064\u307e\u308a\u3001key\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u5bfe\u5fdc\u3059\u308b\u30ad\u30fc\u5024\u30da\u30a2\u306e\u914d\u5217\u5185\u306e\u683c\u7d0d\u4f4d\u7f6e\u3092\u6c7a\u5b9a\u3067\u304d\u307e\u3059\u3002

        \u4e0e\u3048\u3089\u308c\u305fkey\u306b\u5bfe\u3057\u3066\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u8a08\u7b97\u306f2\u3064\u306e\u30b9\u30c6\u30c3\u30d7\u3067\u69cb\u6210\u3055\u308c\u307e\u3059\uff1a

        1. \u7279\u5b9a\u306e\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0hash()\u3092\u4f7f\u7528\u3057\u3066\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u8a08\u7b97\u3057\u307e\u3059\u3002
        2. \u30cf\u30c3\u30b7\u30e5\u5024\u3092\u30d0\u30b1\u30c3\u30c8\u6570\uff08\u914d\u5217\u9577\uff09capacity\u3067\u5270\u4f59\u3092\u53d6\u308a\u3001\u30ad\u30fc\u306b\u5bfe\u5fdc\u3059\u308b\u914d\u5217index\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002
        index = hash(key) % capacity\n

        \u305d\u306e\u5f8c\u3001index\u3092\u4f7f\u7528\u3057\u3066\u30cf\u30c3\u30b7\u30e5\u8868\u5185\u306e\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3001value\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002

        \u914d\u5217\u9577\u304ccapacity = 100\u3067\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304chash(key) = key\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u3068\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306fkey % 100\u3068\u3057\u3066\u8868\u73fe\u3067\u304d\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u56f3\u306f\u3001key\u3092\u5b66\u7c4d\u756a\u53f7\u3001value\u3092\u540d\u524d\u3068\u3057\u3066\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u52d5\u4f5c\u539f\u7406\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 6-2 \u00a0 \u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u52d5\u4f5c\u539f\u7406

        \u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u306f\u7c21\u5358\u306a\u30cf\u30c3\u30b7\u30e5\u8868\u3092\u5b9f\u88c5\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u3053\u3067\u306f\u3001key\u3068value\u3092Pair\u30af\u30e9\u30b9\u306b\u30ab\u30d7\u30bb\u30eb\u5316\u3057\u3066\u30ad\u30fc\u5024\u30da\u30a2\u3092\u8868\u73fe\u3057\u3066\u3044\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_hash_map.py
        class Pair:\n    \"\"\"\u30ad\u30fc\u5024\u30da\u30a2\"\"\"\n\n    def __init__(self, key: int, val: str):\n        self.key = key\n        self.val = val\n\nclass ArrayHashMap:\n    \"\"\"\u914d\u5217\u5b9f\u88c5\u306b\u57fa\u3065\u304f\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\"\"\"\n\n    def __init__(self):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        # 100\u500b\u306e\u30d0\u30b1\u30c3\u30c8\u3092\u542b\u3080\u914d\u5217\u3092\u521d\u671f\u5316\n        self.buckets: list[Pair | None] = [None] * 100\n\n    def hash_func(self, key: int) -> int:\n        \"\"\"\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\"\"\"\n        index = key % 100\n        return index\n\n    def get(self, key: int) -> str:\n        \"\"\"\u7167\u4f1a\u64cd\u4f5c\"\"\"\n        index: int = self.hash_func(key)\n        pair: Pair = self.buckets[index]\n        if pair is None:\n            return None\n        return pair.val\n\n    def put(self, key: int, val: str):\n        \"\"\"\u8ffd\u52a0\u64cd\u4f5c\"\"\"\n        pair = Pair(key, val)\n        index: int = self.hash_func(key)\n        self.buckets[index] = pair\n\n    def remove(self, key: int):\n        \"\"\"\u524a\u9664\u64cd\u4f5c\"\"\"\n        index: int = self.hash_func(key)\n        # None \u306b\u8a2d\u5b9a\u3057\u3001\u524a\u9664\u3092\u8868\u73fe\n        self.buckets[index] = None\n\n    def entry_set(self) -> list[Pair]:\n        \"\"\"\u3059\u3079\u3066\u306e\u30ad\u30fc\u5024\u30da\u30a2\u3092\u53d6\u5f97\"\"\"\n        result: list[Pair] = []\n        for pair in self.buckets:\n            if pair is not None:\n                result.append(pair)\n        return result\n\n    def key_set(self) -> list[int]:\n        \"\"\"\u3059\u3079\u3066\u306e\u30ad\u30fc\u3092\u53d6\u5f97\"\"\"\n        result = []\n        for pair in self.buckets:\n            if pair is not None:\n                result.append(pair.key)\n        return result\n\n    def value_set(self) -> list[str]:\n        \"\"\"\u3059\u3079\u3066\u306e\u5024\u3092\u53d6\u5f97\"\"\"\n        result = []\n        for pair in self.buckets:\n            if pair is not None:\n                result.append(pair.val)\n        return result\n\n    def print(self):\n        \"\"\"\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u51fa\u529b\"\"\"\n        for pair in self.buckets:\n            if pair is not None:\n                print(pair.key, \"->\", pair.val)\n
        array_hash_map.cpp
        /* \u30ad\u30fc\u5024\u30da\u30a2 */\nstruct Pair {\n  public:\n    int key;\n    string val;\n    Pair(int key, string val) {\n        this->key = key;\n        this->val = val;\n    }\n};\n\n/* \u914d\u5217\u5b9f\u88c5\u306b\u57fa\u3065\u304f\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb */\nclass ArrayHashMap {\n  private:\n    vector<Pair *> buckets;\n\n  public:\n    ArrayHashMap() {\n        // \u914d\u5217\u3092\u521d\u671f\u5316\u3001100\u500b\u306e\u30d0\u30b1\u30c3\u30c8\u3092\u542b\u3080\n        buckets = vector<Pair *>(100);\n    }\n\n    ~ArrayHashMap() {\n        // \u30e1\u30e2\u30ea\u3092\u89e3\u653e\n        for (const auto &bucket : buckets) {\n            delete bucket;\n        }\n        buckets.clear();\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u95a2\u6570 */\n    int hashFunc(int key) {\n        int index = key % 100;\n        return index;\n    }\n\n    /* \u30af\u30a8\u30ea\u64cd\u4f5c */\n    string get(int key) {\n        int index = hashFunc(key);\n        Pair *pair = buckets[index];\n        if (pair == nullptr)\n            return \"\";\n        return pair->val;\n    }\n\n    /* \u8ffd\u52a0\u64cd\u4f5c */\n    void put(int key, string val) {\n        Pair *pair = new Pair(key, val);\n        int index = hashFunc(key);\n        buckets[index] = pair;\n    }\n\n    /* \u524a\u9664\u64cd\u4f5c */\n    void remove(int key) {\n        int index = hashFunc(key);\n        // \u30e1\u30e2\u30ea\u3092\u89e3\u653e\u3057\u3066nullptr\u306b\u8a2d\u5b9a\n        delete buckets[index];\n        buckets[index] = nullptr;\n    }\n\n    /* \u3059\u3079\u3066\u306e\u30ad\u30fc\u5024\u30da\u30a2\u3092\u53d6\u5f97 */\n    vector<Pair *> pairSet() {\n        vector<Pair *> pairSet;\n        for (Pair *pair : buckets) {\n            if (pair != nullptr) {\n                pairSet.push_back(pair);\n            }\n        }\n        return pairSet;\n    }\n\n    /* \u3059\u3079\u3066\u306e\u30ad\u30fc\u3092\u53d6\u5f97 */\n    vector<int> keySet() {\n        vector<int> keySet;\n        for (Pair *pair : buckets) {\n            if (pair != nullptr) {\n                keySet.push_back(pair->key);\n            }\n        }\n        return keySet;\n    }\n\n    /* \u3059\u3079\u3066\u306e\u5024\u3092\u53d6\u5f97 */\n    vector<string> valueSet() {\n        vector<string> valueSet;\n        for (Pair *pair : buckets) {\n            if (pair != nullptr) {\n                valueSet.push_back(pair->val);\n            }\n        }\n        return valueSet;\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u5370\u5237 */\n    void print() {\n        for (Pair *kv : pairSet()) {\n            cout << kv->key << \" -> \" << kv->val << endl;\n        }\n    }\n};\n
        array_hash_map.java
        /* \u30ad\u30fc\u5024\u30da\u30a2 */\nclass Pair {\n    public int key;\n    public String val;\n\n    public Pair(int key, String val) {\n        this.key = key;\n        this.val = val;\n    }\n}\n\n/* \u914d\u5217\u5b9f\u88c5\u306b\u57fa\u3065\u304f\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb */\nclass ArrayHashMap {\n    private List<Pair> buckets;\n\n    public ArrayHashMap() {\n        // 100\u500b\u306e\u30d0\u30b1\u30c3\u30c8\u3092\u542b\u3080\u914d\u5217\u3092\u521d\u671f\u5316\n        buckets = new ArrayList<>();\n        for (int i = 0; i < 100; i++) {\n            buckets.add(null);\n        }\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u95a2\u6570 */\n    private int hashFunc(int key) {\n        int index = key % 100;\n        return index;\n    }\n\n    /* \u30af\u30a8\u30ea\u64cd\u4f5c */\n    public String get(int key) {\n        int index = hashFunc(key);\n        Pair pair = buckets.get(index);\n        if (pair == null)\n            return null;\n        return pair.val;\n    }\n\n    /* \u8ffd\u52a0\u64cd\u4f5c */\n    public void put(int key, String val) {\n        Pair pair = new Pair(key, val);\n        int index = hashFunc(key);\n        buckets.set(index, pair);\n    }\n\n    /* \u524a\u9664\u64cd\u4f5c */\n    public void remove(int key) {\n        int index = hashFunc(key);\n        // null\u306b\u8a2d\u5b9a\u3057\u3066\u524a\u9664\u3092\u793a\u3059\n        buckets.set(index, null);\n    }\n\n    /* \u3059\u3079\u3066\u306e\u30ad\u30fc\u5024\u30da\u30a2\u3092\u53d6\u5f97 */\n    public List<Pair> pairSet() {\n        List<Pair> pairSet = new ArrayList<>();\n        for (Pair pair : buckets) {\n            if (pair != null)\n                pairSet.add(pair);\n        }\n        return pairSet;\n    }\n\n    /* \u3059\u3079\u3066\u306e\u30ad\u30fc\u3092\u53d6\u5f97 */\n    public List<Integer> keySet() {\n        List<Integer> keySet = new ArrayList<>();\n        for (Pair pair : buckets) {\n            if (pair != null)\n                keySet.add(pair.key);\n        }\n        return keySet;\n    }\n\n    /* \u3059\u3079\u3066\u306e\u5024\u3092\u53d6\u5f97 */\n    public List<String> valueSet() {\n        List<String> valueSet = new ArrayList<>();\n        for (Pair pair : buckets) {\n            if (pair != null)\n                valueSet.add(pair.val);\n        }\n        return valueSet;\n    }\n\n    /* \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u5370\u5237 */\n    public void print() {\n        for (Pair kv : pairSet()) {\n            System.out.println(kv.key + \" -> \" + kv.val);\n        }\n    }\n}\n
        array_hash_map.cs
        [class]{Pair}-[func]{}\n\n[class]{ArrayHashMap}-[func]{}\n
        array_hash_map.go
        [class]{pair}-[func]{}\n\n[class]{arrayHashMap}-[func]{}\n
        array_hash_map.swift
        [file]{utils/pair.swift}-[class]{Pair}-[func]{}\n\n[class]{ArrayHashMap}-[func]{}\n
        array_hash_map.js
        [class]{Pair}-[func]{}\n\n[class]{ArrayHashMap}-[func]{}\n
        array_hash_map.ts
        [class]{Pair}-[func]{}\n\n[class]{ArrayHashMap}-[func]{}\n
        array_hash_map.dart
        [class]{Pair}-[func]{}\n\n[class]{ArrayHashMap}-[func]{}\n
        array_hash_map.rs
        [class]{Pair}-[func]{}\n\n[class]{ArrayHashMap}-[func]{}\n
        array_hash_map.c
        [class]{Pair}-[func]{}\n\n[class]{ArrayHashMap}-[func]{}\n
        array_hash_map.kt
        [class]{Pair}-[func]{}\n\n[class]{ArrayHashMap}-[func]{}\n
        array_hash_map.rb
        [class]{Pair}-[func]{}\n\n[class]{ArrayHashMap}-[func]{}\n
        "},{"location":"chapter_hashing/hash_map/#613","title":"6.1.3 \u00a0 \u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3068\u30ea\u30b5\u30a4\u30ba","text":"

        \u672c\u8cea\u7684\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u5f79\u5272\u306f\u3001\u3059\u3079\u3066\u306e\u30ad\u30fc\u306e\u5165\u529b\u7a7a\u9593\u5168\u4f53\u3092\u3001\u3059\u3079\u3066\u306e\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u51fa\u529b\u7a7a\u9593\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u5165\u529b\u7a7a\u9593\u306f\u51fa\u529b\u7a7a\u9593\u3088\u308a\u3082\u306f\u308b\u304b\u306b\u5927\u304d\u3044\u3053\u3068\u304c\u3088\u304f\u3042\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u7406\u8ad6\u7684\u306b\u306f\u3001\u300c\u8907\u6570\u306e\u5165\u529b\u304c\u540c\u3058\u51fa\u529b\u306b\u5bfe\u5fdc\u3059\u308b\u300d\u30b1\u30fc\u30b9\u304c\u5e38\u306b\u5b58\u5728\u3057\u307e\u3059\u3002

        \u4e0a\u8a18\u306e\u4f8b\u3067\u306f\u3001\u4e0e\u3048\u3089\u308c\u305f\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u3067\u3001\u5165\u529bkey\u306e\u4e0b\u4e8c\u6841\u304c\u540c\u3058\u5834\u5408\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306f\u540c\u3058\u51fa\u529b\u3092\u751f\u6210\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u5b66\u7c4d\u756a\u53f712836\u306820336\u306e2\u4eba\u306e\u5b66\u751f\u3092\u30af\u30a8\u30ea\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\uff1a

        12836 % 100 = 36\n20336 % 100 = 36\n

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u4e21\u65b9\u306e\u5b66\u7c4d\u756a\u53f7\u304c\u540c\u3058\u540d\u524d\u3092\u6307\u3057\u3066\u304a\u308a\u3001\u3053\u308c\u306f\u660e\u3089\u304b\u306b\u9593\u9055\u3063\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u8907\u6570\u306e\u5165\u529b\u304c\u540c\u3058\u51fa\u529b\u306b\u5bfe\u5fdc\u3059\u308b\u72b6\u6cc1\u3092\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3068\u547c\u3073\u307e\u3059\u3002

        \u56f3 6-3 \u00a0 \u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306e\u4f8b

        \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u5bb9\u91cf\\(n\\)\u304c\u5897\u52a0\u3059\u308b\u306b\u3064\u308c\u3066\u3001\u8907\u6570\u306e\u30ad\u30fc\u304c\u540c\u3058\u30d0\u30b1\u30c3\u30c8\u306b\u5272\u308a\u5f53\u3066\u3089\u308c\u308b\u78ba\u7387\u304c\u6e1b\u5c11\u3057\u3001\u885d\u7a81\u304c\u5c11\u306a\u304f\u306a\u308b\u3053\u3068\u306f\u7406\u89e3\u3057\u3084\u3059\u3044\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u3092\u30ea\u30b5\u30a4\u30ba\u3059\u308b\u3053\u3068\u3067\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u6e1b\u3089\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30ea\u30b5\u30a4\u30ba\u524d\u306f\u3001\u30ad\u30fc\u5024\u30da\u30a2(136, A)\u3068(236, D)\u304c\u885d\u7a81\u3057\u3066\u3044\u307e\u3057\u305f\u3002\u3057\u304b\u3057\u3001\u30ea\u30b5\u30a4\u30ba\u5f8c\u306f\u885d\u7a81\u304c\u89e3\u6c7a\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        \u56f3 6-4 \u00a0 \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30ea\u30b5\u30a4\u30ba

        \u914d\u5217\u306e\u62e1\u5f35\u3068\u540c\u69d8\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30ea\u30b5\u30a4\u30ba\u306b\u306f\u3059\u3079\u3066\u306e\u30ad\u30fc\u5024\u30da\u30a2\u3092\u5143\u306e\u30cf\u30c3\u30b7\u30e5\u8868\u304b\u3089\u65b0\u3057\u3044\u3082\u306e\u306b\u79fb\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002\u3055\u3089\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306ecapacity\u304c\u5909\u66f4\u3055\u308c\u308b\u305f\u3081\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u3059\u3079\u3066\u306e\u30ad\u30fc\u5024\u30da\u30a2\u306e\u683c\u7d0d\u4f4d\u7f6e\u3092\u518d\u8a08\u7b97\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u30ea\u30b5\u30a4\u30ba\u30d7\u30ed\u30bb\u30b9\u306e\u8a08\u7b97\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u304c\u3055\u3089\u306b\u5897\u52a0\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306f\u983b\u7e41\u306a\u30ea\u30b5\u30a4\u30ba\u3092\u9632\u3050\u305f\u3081\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306b\u5341\u5206\u5927\u304d\u306a\u5bb9\u91cf\u3092\u5272\u308a\u5f53\u3066\u308b\u3053\u3068\u304c\u3088\u304f\u3042\u308a\u307e\u3059\u3002

        \u8ca0\u8377\u7387\u306f\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u91cd\u8981\u306a\u6982\u5ff5\u3067\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u8868\u5185\u306e\u8981\u7d20\u6570\u3068\u30d0\u30b1\u30c3\u30c8\u6570\u306e\u6bd4\u7387\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u307e\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306e\u6df1\u523b\u5ea6\u3092\u6e2c\u5b9a\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u3001\u3057\u3070\u3057\u3070\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30ea\u30b5\u30a4\u30ba\u306e\u30c8\u30ea\u30ac\u30fc\u3068\u3057\u3066\u3082\u6a5f\u80fd\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001Java\u3067\u306f\u3001\u8ca0\u8377\u7387\u304c\\(0.75\\)\u3092\u8d85\u3048\u308b\u3068\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u30cf\u30c3\u30b7\u30e5\u8868\u3092\u5143\u306e\u30b5\u30a4\u30ba\u306e2\u500d\u306b\u30ea\u30b5\u30a4\u30ba\u3057\u307e\u3059\u3002

        "},{"location":"chapter_hashing/summary/","title":"6.4 \u00a0 \u307e\u3068\u3081","text":""},{"location":"chapter_hashing/summary/#1","title":"1. \u00a0 \u91cd\u8981\u306a\u30dd\u30a4\u30f3\u30c8","text":"
        • \u5165\u529bkey\u304c\u4e0e\u3048\u3089\u308c\u308b\u3068\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306f\\(O(1)\\)\u306e\u6642\u9593\u3067\u5bfe\u5fdc\u3059\u308bvalue\u3092\u53d6\u5f97\u3067\u304d\u3001\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3059\u3002
        • \u4e00\u822c\u7684\u306a\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u64cd\u4f5c\u306b\u306f\u3001\u30af\u30a8\u30ea\u3001\u30ad\u30fc\u5024\u30da\u30a2\u306e\u8ffd\u52a0\u3001\u30ad\u30fc\u5024\u30da\u30a2\u306e\u524a\u9664\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u8d70\u67fb\u304c\u3042\u308a\u307e\u3059\u3002
        • \u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306fkey\u3092\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3057\u3001\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3066value\u3092\u53d6\u5f97\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002
        • 2\u3064\u306e\u7570\u306a\u308b\u30ad\u30fc\u304c\u30cf\u30c3\u30b7\u30e5\u5316\u5f8c\u306b\u540c\u3058\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u306a\u308b\u5834\u5408\u304c\u3042\u308a\u3001\u8aa4\u3063\u305f\u30af\u30a8\u30ea\u7d50\u679c\u306b\u3064\u306a\u304c\u308a\u307e\u3059\u3002\u3053\u306e\u73fe\u8c61\u306f\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3068\u3057\u3066\u77e5\u3089\u308c\u3066\u3044\u307e\u3059\u3002
        • \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u5bb9\u91cf\u304c\u5927\u304d\u3044\u307b\u3069\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306e\u78ba\u7387\u306f\u4f4e\u304f\u306a\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30ea\u30b5\u30a4\u30ba\u306f\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u7de9\u548c\u3067\u304d\u307e\u3059\u3002\u914d\u5217\u306e\u30ea\u30b5\u30a4\u30ba\u3068\u540c\u69d8\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30ea\u30b5\u30a4\u30ba\u306f\u30b3\u30b9\u30c8\u304c\u9ad8\u3044\u3067\u3059\u3002
        • \u8981\u7d20\u6570\u3092\u30d0\u30b1\u30c3\u30c8\u6570\u3067\u5272\u3063\u305f\u8ca0\u8377\u7387\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306e\u6df1\u523b\u5ea6\u3092\u53cd\u6620\u3057\u3001\u3057\u3070\u3057\u3070\u30cf\u30c3\u30b7\u30e5\u8868\u30ea\u30b5\u30a4\u30ba\u306e\u30c8\u30ea\u30ac\u30fc\u6761\u4ef6\u3068\u3057\u3066\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        • \u9023\u9396\u6cd5\u306f\u5404\u8981\u7d20\u3092\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u5909\u63db\u3057\u3001\u885d\u7a81\u3059\u308b\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u540c\u3058\u30ea\u30b9\u30c8\u306b\u683c\u7d0d\u3059\u308b\u3053\u3068\u3067\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u306b\u5bfe\u51e6\u3057\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u904e\u5ea6\u306b\u9577\u3044\u30ea\u30b9\u30c8\u306f\u30af\u30a8\u30ea\u52b9\u7387\u3092\u4f4e\u4e0b\u3055\u305b\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u30ea\u30b9\u30c8\u3092\u8d64\u9ed2\u6728\u306b\u5909\u63db\u3059\u308b\u3053\u3068\u3067\u6539\u5584\u3067\u304d\u307e\u3059\u3002
        • \u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u306f\u8907\u6570\u56de\u306e\u30d7\u30ed\u30fc\u30d6\u3092\u901a\u3057\u3066\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u51e6\u7406\u3057\u307e\u3059\u3002\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u306f\u56fa\u5b9a\u30b9\u30c6\u30c3\u30d7\u30b5\u30a4\u30ba\u3092\u4f7f\u7528\u3057\u307e\u3059\u304c\u3001\u8981\u7d20\u3092\u524a\u9664\u3067\u304d\u305a\u3001\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u3092\u8d77\u3053\u3057\u3084\u3059\u3044\u50be\u5411\u304c\u3042\u308a\u307e\u3059\u3002\u591a\u91cd\u30cf\u30c3\u30b7\u30e5\u306f\u30d7\u30ed\u30fc\u30d6\u306b\u8907\u6570\u306e\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3001\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u3068\u6bd4\u8f03\u3057\u3066\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u3092\u6e1b\u3089\u3057\u307e\u3059\u304c\u3001\u8a08\u7b97\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u304c\u5897\u52a0\u3057\u307e\u3059\u3002
        • \u7570\u306a\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306f\u3055\u307e\u3056\u307e\u306a\u30cf\u30c3\u30b7\u30e5\u8868\u5b9f\u88c5\u3092\u63a1\u7528\u3057\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001Java\u306eHashMap\u306f\u9023\u9396\u6cd5\u3092\u4f7f\u7528\u3057\u3001Python\u306edict\u306f\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u3092\u63a1\u7528\u3057\u3066\u3044\u307e\u3059\u3002
        • \u30cf\u30c3\u30b7\u30e5\u8868\u3067\u306f\u3001\u6c7a\u5b9a\u6027\u3001\u9ad8\u52b9\u7387\u3001\u5747\u7b49\u5206\u6563\u3092\u6301\u3064\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u671b\u307e\u308c\u307e\u3059\u3002\u6697\u53f7\u5316\u3067\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u885d\u7a81\u8010\u6027\u3068\u96ea\u5d29\u52b9\u679c\u3082\u6301\u3064\u3079\u304d\u3067\u3059\u3002
        • \u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u901a\u5e38\u3001\u30cf\u30c3\u30b7\u30e5\u5024\u306e\u5747\u7b49\u5206\u6563\u3092\u4fdd\u8a3c\u3057\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u6e1b\u3089\u3059\u305f\u3081\u306b\u3001\u5927\u304d\u306a\u7d20\u6570\u3092\u5270\u4f59\u3068\u3057\u3066\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u4e00\u822c\u7684\u306a\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u306f\u3001MD5\u3001SHA-1\u3001SHA-2\u3001SHA-3\u304c\u3042\u308a\u307e\u3059\u3002MD5\u306f\u30d5\u30a1\u30a4\u30eb\u6574\u5408\u6027\u30c1\u30a7\u30c3\u30af\u306b\u3088\u304f\u4f7f\u7528\u3055\u308c\u3001SHA-2\u306f\u5b89\u5168\u306a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3068\u30d7\u30ed\u30c8\u30b3\u30eb\u3067\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        • \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306f\u901a\u5e38\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97\u3059\u308b\u305f\u3081\u306b\u3001\u30c7\u30fc\u30bf\u578b\u306b\u5bfe\u3057\u3066\u7d44\u307f\u8fbc\u307f\u306e\u30cf\u30c3\u30b7\u30e5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002\u4e00\u822c\u7684\u306b\u3001\u4e0d\u5909\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u307f\u304c\u30cf\u30c3\u30b7\u30e5\u53ef\u80fd\u3067\u3059\u3002
        "},{"location":"chapter_hashing/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q: \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(n)\\)\u306b\u60aa\u5316\u3059\u308b\u306e\u306f\u3044\u3064\u3067\u3059\u304b\uff1f

        \u30cf\u30c3\u30b7\u30e5\u8868\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u304c\u6df1\u523b\u306a\u5834\u5408\u306b\\(O(n)\\)\u306b\u60aa\u5316\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u304c\u9069\u5207\u306b\u8a2d\u8a08\u3055\u308c\u3001\u5bb9\u91cf\u304c\u9069\u5207\u306b\u8a2d\u5b9a\u3055\u308c\u3001\u885d\u7a81\u304c\u5747\u7b49\u306b\u5206\u6563\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u3067\u3059\u3002\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u7d44\u307f\u8fbc\u307f\u30cf\u30c3\u30b7\u30e5\u8868\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001\u901a\u5e38\u306f\u6642\u9593\u8a08\u7b97\u91cf\u3092\\(O(1)\\)\u3068\u8003\u3048\u307e\u3059\u3002

        Q: \u306a\u305c\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\\(f(x) = x\\)\u3092\u4f7f\u7528\u3057\u306a\u3044\u306e\u3067\u3059\u304b\uff1f\u3053\u308c\u306a\u3089\u885d\u7a81\u3092\u6392\u9664\u3067\u304d\u307e\u3059\u3002

        \u30cf\u30c3\u30b7\u30e5\u95a2\u6570\\(f(x) = x\\)\u3067\u306f\u3001\u5404\u8981\u7d20\u304c\u4e00\u610f\u306e\u30d0\u30b1\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5bfe\u5fdc\u3057\u3001\u3053\u308c\u306f\u914d\u5217\u3068\u540c\u7b49\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u5165\u529b\u7a7a\u9593\u306f\u901a\u5e38\u51fa\u529b\u7a7a\u9593\uff08\u914d\u5217\u9577\uff09\u3088\u308a\u3082\u306f\u308b\u304b\u306b\u5927\u304d\u3044\u305f\u3081\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u6700\u5f8c\u306e\u30b9\u30c6\u30c3\u30d7\u306f\u914d\u5217\u9577\u306e\u5270\u4f59\u3092\u53d6\u308b\u3053\u3068\u304c\u3088\u304f\u3042\u308a\u307e\u3059\u3002\u8a00\u3044\u63db\u3048\u308b\u3068\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u76ee\u6a19\u306f\u3001\\(O(1)\\)\u306e\u30af\u30a8\u30ea\u52b9\u7387\u3092\u63d0\u4f9b\u3057\u306a\u304c\u3089\u3001\u3088\u308a\u5927\u304d\u306a\u72b6\u614b\u7a7a\u9593\u3092\u3088\u308a\u5c0f\u3055\u306a\u3082\u306e\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3059\u308b\u3053\u3068\u3067\u3059\u3002

        Q: \u30cf\u30c3\u30b7\u30e5\u8868\u304c\u3053\u308c\u3089\u306e\u69cb\u9020\u3092\u4f7f\u3063\u3066\u5b9f\u88c5\u3055\u308c\u3066\u3044\u308b\u306b\u3082\u304b\u304b\u308f\u3089\u305a\u3001\u306a\u305c\u914d\u5217\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u4e8c\u5206\u6728\u3088\u308a\u3082\u52b9\u7387\u7684\u306b\u306a\u308c\u308b\u306e\u3067\u3059\u304b\uff1f

        \u307e\u305a\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306f\u6642\u9593\u52b9\u7387\u304c\u9ad8\u3044\u3067\u3059\u304c\u3001\u7a7a\u9593\u52b9\u7387\u306f\u4f4e\u3044\u3067\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30e1\u30e2\u30ea\u306e\u5927\u90e8\u5206\u306f\u672a\u4f7f\u7528\u306e\u307e\u307e\u3067\u3059\u3002

        \u6b21\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306f\u7279\u5b9a\u306e\u30e6\u30fc\u30b9\u30b1\u30fc\u30b9\u3067\u306e\u307f\u6642\u9593\u52b9\u7387\u304c\u9ad8\u3044\u3067\u3059\u3002\u914d\u5217\u3084\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u540c\u3058\u6642\u9593\u8a08\u7b97\u91cf\u3067\u6a5f\u80fd\u3092\u5b9f\u88c5\u3067\u304d\u308b\u5834\u5408\u3001\u901a\u5e38\u306f\u30cf\u30c3\u30b7\u30e5\u8868\u3092\u4f7f\u7528\u3059\u308b\u3088\u308a\u3082\u9ad8\u901f\u3067\u3059\u3002\u3053\u308c\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u8a08\u7b97\u304c\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u3092\u767a\u751f\u3055\u305b\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306e\u5b9a\u6570\u56e0\u5b50\u304c\u5927\u304d\u304f\u306a\u308b\u305f\u3081\u3067\u3059\u3002

        \u6700\u5f8c\u306b\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\u60aa\u5316\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u9023\u9396\u6cd5\u3067\u306f\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3084\u8d64\u9ed2\u6728\u3067\u691c\u7d22\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3001\u3053\u308c\u306f\u4f9d\u7136\u3068\u3057\u3066\\(O(n)\\)\u6642\u9593\u306b\u60aa\u5316\u3059\u308b\u30ea\u30b9\u30af\u304c\u3042\u308a\u307e\u3059\u3002

        Q: \u591a\u91cd\u30cf\u30c3\u30b7\u30e5\u306b\u3082\u8981\u7d20\u3092\u76f4\u63a5\u524a\u9664\u3067\u304d\u306a\u3044\u3068\u3044\u3046\u6b20\u9665\u304c\u3042\u308a\u307e\u3059\u304b\uff1f\u524a\u9664\u3068\u3057\u3066\u30de\u30fc\u30af\u3055\u308c\u305f\u7a7a\u9593\u306f\u518d\u5229\u7528\u3067\u304d\u307e\u3059\u304b\uff1f

        \u591a\u91cd\u30cf\u30c3\u30b7\u30e5\u306f\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u306e\u4e00\u5f62\u614b\u3067\u3042\u308a\u3001\u3059\u3079\u3066\u306e\u30aa\u30fc\u30d7\u30f3\u30a2\u30c9\u30ec\u30b9\u6cd5\u306b\u306f\u8981\u7d20\u3092\u76f4\u63a5\u524a\u9664\u3067\u304d\u306a\u3044\u3068\u3044\u3046\u6b20\u70b9\u304c\u3042\u308a\u307e\u3059\u3002\u8981\u7d20\u3092\u524a\u9664\u6e08\u307f\u3068\u3057\u3066\u30de\u30fc\u30af\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u30de\u30fc\u30af\u3055\u308c\u305f\u7a7a\u9593\u306f\u518d\u5229\u7528\u3067\u304d\u307e\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u8868\u306b\u65b0\u3057\u3044\u8981\u7d20\u3092\u633f\u5165\u3059\u308b\u969b\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u304c\u524a\u9664\u6e08\u307f\u3068\u3057\u3066\u30de\u30fc\u30af\u3055\u308c\u305f\u4f4d\u7f6e\u3092\u6307\u3057\u3066\u3044\u308b\u5834\u5408\u3001\u305d\u306e\u4f4d\u7f6e\u306f\u65b0\u3057\u3044\u8981\u7d20\u306b\u3088\u3063\u3066\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30d7\u30ed\u30fc\u30d6\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u7dad\u6301\u3057\u306a\u304c\u3089\u3001\u7a7a\u9593\u306e\u52b9\u7387\u7684\u306a\u4f7f\u7528\u304c\u4fdd\u8a3c\u3055\u308c\u307e\u3059\u3002

        Q: \u306a\u305c\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u306e\u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u4e2d\u306b\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u304c\u767a\u751f\u3059\u308b\u306e\u3067\u3059\u304b\uff1f

        \u691c\u7d22\u30d7\u30ed\u30bb\u30b9\u4e2d\u3001\u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306f\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u3068\u30ad\u30fc\u5024\u30da\u30a2\u3092\u6307\u3057\u307e\u3059\u3002key\u304c\u4e00\u81f4\u3057\u306a\u3044\u5834\u5408\u3001\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u793a\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u7dda\u5f62\u30d7\u30ed\u30fc\u30d6\u306f\u6b63\u3057\u3044\u30ad\u30fc\u5024\u30da\u30a2\u304c\u898b\u3064\u304b\u308b\u304b\u691c\u7d22\u304c\u5931\u6557\u3059\u308b\u307e\u3067\u3001\u4e8b\u524d\u306b\u6c7a\u3081\u3089\u308c\u305f\u30b9\u30c6\u30c3\u30d7\u30b5\u30a4\u30ba\u3067\u4e0b\u65b9\u5411\u306b\u691c\u7d22\u3057\u307e\u3059\u3002

        Q: \u306a\u305c\u30cf\u30c3\u30b7\u30e5\u8868\u306e\u30ea\u30b5\u30a4\u30ba\u304c\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u3092\u7de9\u548c\u3067\u304d\u308b\u306e\u3067\u3059\u304b\uff1f

        \u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u306e\u6700\u5f8c\u306e\u30b9\u30c6\u30c3\u30d7\u306f\u3001\u51fa\u529b\u3092\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2\u5185\u306b\u4fdd\u3064\u305f\u3081\u306b\u3001\u914d\u5217\u9577\\(n\\)\u306e\u5270\u4f59\u3092\u53d6\u308b\u3053\u3068\u304c\u3088\u304f\u3042\u308a\u307e\u3059\u3002\u30ea\u30b5\u30a4\u30ba\u6642\u3001\u914d\u5217\u9577\\(n\\)\u304c\u5909\u5316\u3057\u3001\u30ad\u30fc\u306b\u5bfe\u5fdc\u3059\u308b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3082\u5909\u5316\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u4ee5\u524d\u306b\u540c\u3058\u30d0\u30b1\u30c3\u30c8\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3055\u308c\u3066\u3044\u305f\u30ad\u30fc\u304c\u3001\u30ea\u30b5\u30a4\u30ba\u5f8c\u306b\u8907\u6570\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u6563\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u305d\u308c\u306b\u3088\u3063\u3066\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u304c\u7de9\u548c\u3055\u308c\u307e\u3059\u3002

        "},{"location":"chapter_heap/","title":"\u7b2c 8 \u7ae0 \u00a0 \u30d2\u30fc\u30d7","text":"

        Abstract

        \u30d2\u30fc\u30d7\u306f\u5c71\u3068\u305d\u306e\u967a\u3057\u3044\u5cf0\u306e\u3088\u3046\u306b\u3001\u5c64\u3092\u306a\u3057\u3066\u8d77\u4f0f\u3057\u3001\u305d\u308c\u305e\u308c\u304c\u72ec\u7279\u306e\u5f62\u3092\u6301\u3063\u3066\u3044\u307e\u3059\u3002

        \u5404\u5c71\u306e\u9802\u306f\u6563\u3089\u3070\u3063\u305f\u9ad8\u3055\u3067\u4e0a\u4e0b\u3057\u307e\u3059\u304c\u3001\u6700\u3082\u9ad8\u3044\u3082\u306e\u304c\u5e38\u306b\u6700\u521d\u306b\u6ce8\u76ee\u3092\u96c6\u3081\u307e\u3059\u3002

        "},{"location":"chapter_heap/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 8.1 \u00a0 \u30d2\u30fc\u30d7
        • 8.2 \u00a0 \u30d2\u30fc\u30d7\u306e\u69cb\u7bc9
        • 8.3 \u00a0 Top-k\u554f\u984c
        • 8.4 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_heap/build_heap/","title":"8.2 \u00a0 \u30d2\u30fc\u30d7\u69cb\u7bc9\u64cd\u4f5c","text":"

        \u5834\u5408\u306b\u3088\u3063\u3066\u306f\u3001\u30ea\u30b9\u30c8\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u4f7f\u7528\u3057\u3066\u30d2\u30fc\u30d7\u3092\u69cb\u7bc9\u3057\u305f\u3044\u3053\u3068\u304c\u3042\u308a\u3001\u3053\u306e\u30d7\u30ed\u30bb\u30b9\u306f\u300c\u30d2\u30fc\u30d7\u69cb\u7bc9\u64cd\u4f5c\u300d\u3068\u3057\u3066\u77e5\u3089\u308c\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_heap/build_heap/#821","title":"8.2.1 \u00a0 \u30d2\u30fc\u30d7\u633f\u5165\u64cd\u4f5c\u306b\u3088\u308b\u5b9f\u88c5","text":"

        \u307e\u305a\u3001\u7a7a\u306e\u30d2\u30fc\u30d7\u3092\u4f5c\u6210\u3057\u3001\u6b21\u306b\u30ea\u30b9\u30c8\u3092\u53cd\u5fa9\u51e6\u7406\u3057\u3066\u3001\u5404\u8981\u7d20\u306b\u5bfe\u3057\u3066\u9806\u756a\u306b\u300c\u30d2\u30fc\u30d7\u633f\u5165\u64cd\u4f5c\u300d\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306e\u672b\u5c3e\u306b\u8ffd\u52a0\u3057\u3001\u6b21\u306b\u4e0b\u304b\u3089\u4e0a\u306b\u300c\u30d2\u30fc\u30d7\u5316\u300d\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

        \u30d2\u30fc\u30d7\u306b\u8981\u7d20\u304c\u8ffd\u52a0\u3055\u308c\u308b\u305f\u3073\u306b\u3001\u30d2\u30fc\u30d7\u306e\u9577\u3055\u306f1\u3064\u305a\u3064\u5897\u52a0\u3057\u307e\u3059\u3002\u30ce\u30fc\u30c9\u306f\u4e8c\u5206\u6728\u306b\u4e0a\u304b\u3089\u4e0b\u306b\u8ffd\u52a0\u3055\u308c\u308b\u305f\u3081\u3001\u30d2\u30fc\u30d7\u306f\u300c\u4e0a\u304b\u3089\u4e0b\u306b\u300d\u69cb\u7bc9\u3055\u308c\u307e\u3059\u3002

        \u8981\u7d20\u6570\u3092\\(n\\)\u3068\u3059\u308b\u3068\u3001\u5404\u8981\u7d20\u306e\u633f\u5165\u64cd\u4f5c\u306f\\(O(\\log{n})\\)\u6642\u9593\u304b\u304b\u308b\u305f\u3081\u3001\u3053\u306e\u30d2\u30fc\u30d7\u69cb\u7bc9\u65b9\u6cd5\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n \\log n)\\)\u3067\u3059\u3002

        "},{"location":"chapter_heap/build_heap/#822","title":"8.2.2 \u00a0 \u8d70\u67fb\u306b\u3088\u308b\u30d2\u30fc\u30d7\u5316\u306e\u5b9f\u88c5","text":"

        \u5b9f\u969b\u306b\u306f\u30012\u3064\u306e\u30b9\u30c6\u30c3\u30d7\u3067\u3088\u308a\u52b9\u7387\u7684\u306a\u30d2\u30fc\u30d7\u69cb\u7bc9\u65b9\u6cd5\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002

        1. \u30ea\u30b9\u30c8\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u305d\u306e\u307e\u307e\u30d2\u30fc\u30d7\u306b\u8ffd\u52a0\u3057\u307e\u3059\u3002\u3053\u306e\u6642\u70b9\u3067\u306f\u3001\u30d2\u30fc\u30d7\u306e\u6027\u8cea\u306f\u307e\u3060\u6e80\u305f\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002
        2. \u30d2\u30fc\u30d7\u3092\u9006\u9806\uff08\u30ec\u30d9\u30eb\u9806\u8d70\u67fb\u306e\u9006\uff09\u3067\u8d70\u67fb\u3057\u3001\u5404\u975e\u8449\u30ce\u30fc\u30c9\u306b\u5bfe\u3057\u3066\u300c\u4e0a\u304b\u3089\u4e0b\u306e\u30d2\u30fc\u30d7\u5316\u300d\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002

        \u30ce\u30fc\u30c9\u3092\u30d2\u30fc\u30d7\u5316\u3057\u305f\u5f8c\u3001\u305d\u306e\u30ce\u30fc\u30c9\u3092\u6839\u3068\u3059\u308b\u90e8\u5206\u6728\u306f\u6709\u52b9\u306a\u90e8\u5206\u30d2\u30fc\u30d7\u306b\u306a\u308a\u307e\u3059\u3002\u8d70\u67fb\u304c\u9006\u9806\u3067\u3042\u308b\u305f\u3081\u3001\u30d2\u30fc\u30d7\u306f\u300c\u4e0b\u304b\u3089\u4e0a\u306b\u300d\u69cb\u7bc9\u3055\u308c\u307e\u3059\u3002

        \u9006\u8d70\u67fb\u3092\u9078\u629e\u3059\u308b\u7406\u7531\u306f\u3001\u73fe\u5728\u306e\u30ce\u30fc\u30c9\u306e\u4e0b\u306e\u90e8\u5206\u6728\u304c\u3059\u3067\u306b\u6709\u52b9\u306a\u90e8\u5206\u30d2\u30fc\u30d7\u3067\u3042\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3057\u3001\u73fe\u5728\u306e\u30ce\u30fc\u30c9\u306e\u30d2\u30fc\u30d7\u5316\u3092\u52b9\u679c\u7684\u306b\u3059\u308b\u305f\u3081\u3067\u3059\u3002

        \u8a00\u53ca\u3059\u308b\u4fa1\u5024\u304c\u3042\u308b\u306e\u306f\u3001**\u8449\u30ce\u30fc\u30c9\u306f\u5b50\u3092\u6301\u305f\u306a\u3044\u305f\u3081\u3001\u81ea\u7136\u306b\u6709\u52b9\u306a\u90e8\u5206\u30d2\u30fc\u30d7\u3092\u5f62\u6210\u3057\u3001\u30d2\u30fc\u30d7\u5316\u3059\u308b\u5fc5\u8981\u304c\u306a\u3044**\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u6700\u5f8c\u306e\u975e\u8449\u30ce\u30fc\u30c9\u306f\u6700\u5f8c\u306e\u30ce\u30fc\u30c9\u306e\u89aa\u3067\u3059\u3002\u305d\u3053\u304b\u3089\u958b\u59cb\u3057\u3066\u9006\u9806\u306b\u8d70\u67fb\u3057\u3066\u30d2\u30fc\u30d7\u5316\u3092\u5b9f\u884c\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
        def __init__(self, nums: list[int]):\n    \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3001\u5165\u529b\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u3044\u3066\u30d2\u30fc\u30d7\u3092\u69cb\u7bc9\"\"\"\n    # \u3059\u3079\u3066\u306e\u30ea\u30b9\u30c8\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u8ffd\u52a0\n    self.max_heap = nums\n    # \u8449\u4ee5\u5916\u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3092\u30d2\u30fc\u30d7\u5316\n    for i in range(self.parent(self.size() - 1), -1, -1):\n        self.sift_down(i)\n
        my_heap.cpp
        /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3001\u5165\u529b\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u3044\u3066\u30d2\u30fc\u30d7\u3092\u69cb\u7bc9 */\nMaxHeap(vector<int> nums) {\n    // \u3059\u3079\u3066\u306e\u30ea\u30b9\u30c8\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u8ffd\u52a0\n    maxHeap = nums;\n    // \u8449\u4ee5\u5916\u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3092\u30d2\u30fc\u30d7\u5316\n    for (int i = parent(size() - 1); i >= 0; i--) {\n        siftDown(i);\n    }\n}\n
        my_heap.java
        /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3001\u5165\u529b\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u3044\u3066\u30d2\u30fc\u30d7\u3092\u69cb\u7bc9 */\nMaxHeap(List<Integer> nums) {\n    // \u3059\u3079\u3066\u306e\u30ea\u30b9\u30c8\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u8ffd\u52a0\n    maxHeap = new ArrayList<>(nums);\n    // \u8449\u3092\u9664\u304f\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3092\u30d2\u30fc\u30d7\u5316\n    for (int i = parent(size() - 1); i >= 0; i--) {\n        siftDown(i);\n    }\n}\n
        my_heap.cs
        [class]{MaxHeap}-[func]{MaxHeap}\n
        my_heap.go
        [class]{maxHeap}-[func]{newMaxHeap}\n
        my_heap.swift
        [class]{MaxHeap}-[func]{init}\n
        my_heap.js
        [class]{MaxHeap}-[func]{constructor}\n
        my_heap.ts
        [class]{MaxHeap}-[func]{constructor}\n
        my_heap.dart
        [class]{MaxHeap}-[func]{MaxHeap}\n
        my_heap.rs
        [class]{MaxHeap}-[func]{new}\n
        my_heap.c
        [class]{MaxHeap}-[func]{newMaxHeap}\n
        my_heap.kt
        [class]{MaxHeap}-[func]{}\n
        my_heap.rb
        [class]{MaxHeap}-[func]{initialize}\n
        "},{"location":"chapter_heap/build_heap/#823","title":"8.2.3 \u00a0 \u8a08\u7b97\u91cf\u5206\u6790","text":"

        \u6b21\u306b\u3001\u3053\u306e\u7b2c2\u306e\u30d2\u30fc\u30d7\u69cb\u7bc9\u65b9\u6cd5\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3092\u8a08\u7b97\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002

        • \u5b8c\u5099\u4e8c\u5206\u6728\u306e\u30ce\u30fc\u30c9\u6570\u3092\\(n\\)\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u8449\u30ce\u30fc\u30c9\u306e\u6570\u306f\\((n + 1) / 2\\)\u3067\u3059\u3002\u3053\u3053\u3067\\(/\\) \u306f\u6574\u6570\u9664\u7b97\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30d2\u30fc\u30d7\u5316\u304c\u5fc5\u8981\u306a\u30ce\u30fc\u30c9\u306e\u6570\u306f\\((n - 1) / 2\\)\u3067\u3059\u3002
        • \u300c\u4e0a\u304b\u3089\u4e0b\u306e\u30d2\u30fc\u30d7\u5316\u300d\u306e\u30d7\u30ed\u30bb\u30b9\u3067\u306f\u3001\u5404\u30ce\u30fc\u30c9\u306f\u6700\u5927\u3067\u8449\u30ce\u30fc\u30c9\u307e\u3067\u30d2\u30fc\u30d7\u5316\u3055\u308c\u308b\u305f\u3081\u3001\u6700\u5927\u53cd\u5fa9\u56de\u6570\u306f\u4e8c\u5206\u6728\u306e\u9ad8\u3055\\(\\log n\\)\u3067\u3059\u3002

        \u3053\u306e2\u3064\u3092\u639b\u3051\u5408\u308f\u305b\u308b\u3068\u3001\u30d2\u30fc\u30d7\u69cb\u7bc9\u30d7\u30ed\u30bb\u30b9\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n \\log n)\\)\u3068\u306a\u308a\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u3053\u306e\u63a8\u5b9a\u306f\u6b63\u78ba\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u4e8c\u5206\u6728\u306e\u4e0b\u4f4d\u30ec\u30d9\u30eb\u306b\u306f\u4e0a\u4f4d\u3088\u308a\u3082\u306f\u308b\u304b\u306b\u591a\u304f\u306e\u30ce\u30fc\u30c9\u304c\u3042\u308b\u3068\u3044\u3046\u6027\u8cea\u3092\u8003\u616e\u3057\u3066\u3044\u306a\u3044\u304b\u3089\u3067\u3059\u3002

        \u3088\u308a\u6b63\u78ba\u306a\u8a08\u7b97\u3092\u884c\u3044\u307e\u3057\u3087\u3046\u3002\u8a08\u7b97\u3092\u7c21\u7d20\u5316\u3059\u308b\u305f\u3081\u3001\\(n\\)\u500b\u306e\u30ce\u30fc\u30c9\u3068\u9ad8\u3055\\(h\\)\u3092\u6301\u3064\u300c\u5b8c\u5168\u4e8c\u5206\u6728\u300d\u3092\u4eee\u5b9a\u3057\u307e\u3059\u3002\u3053\u306e\u4eee\u5b9a\u306f\u7d50\u679c\u306e\u6b63\u78ba\u6027\u306b\u5f71\u97ff\u3057\u307e\u305b\u3093\u3002

        \u56f3 8-5 \u00a0 \u5b8c\u5168\u4e8c\u5206\u6728\u306e\u5404\u30ec\u30d9\u30eb\u306e\u30ce\u30fc\u30c9\u6570

        \u4e0a\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30ce\u30fc\u30c9\u304c\u300c\u4e0a\u304b\u3089\u4e0b\u306b\u30d2\u30fc\u30d7\u5316\u3055\u308c\u308b\u300d\u6700\u5927\u53cd\u5fa9\u56de\u6570\u306f\u3001\u305d\u306e\u30ce\u30fc\u30c9\u304b\u3089\u8449\u30ce\u30fc\u30c9\u307e\u3067\u306e\u8ddd\u96e2\u3068\u7b49\u3057\u304f\u3001\u3053\u308c\u306f\u6b63\u78ba\u306b\u300c\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u300d\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u5404\u30ec\u30d9\u30eb\u3067\u300c\u30ce\u30fc\u30c9\u6570\u00d7\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u300d\u3092\u5408\u8a08\u3057\u3066\u3001**\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u306e\u7dcf\u30d2\u30fc\u30d7\u5316\u53cd\u5fa9\u56de\u6570\u3092\u5f97\u308b**\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \\[ T(h) = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{(h-1)}\\times1 \\]

        \u4e0a\u8a18\u306e\u65b9\u7a0b\u5f0f\u3092\u7c21\u7d20\u5316\u3059\u308b\u305f\u3081\u306b\u3001\u9ad8\u6821\u306e\u6570\u5217\u306e\u77e5\u8b58\u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u307e\u305a\\(T(h)\\)\u306b\\(2\\)\u3092\u639b\u3051\u3066\u4ee5\u4e0b\u3092\u5f97\u307e\u3059\uff1a

        \\[ \\begin{aligned} T(h) & = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{h-1}\\times1 \\newline 2T(h) & = 2^1h + 2^2(h-1) + 2^3(h-2) + \\dots + 2^h\\times1 \\newline \\end{aligned} \\]

        \u5909\u4f4d\u6cd5\u3092\u4f7f\u7528\u3057\u3066\\(2T(h)\\)\u304b\u3089\\(T(h)\\)\u3092\u6e1b\u7b97\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u3092\u5f97\u307e\u3059\uff1a

        \\[ 2T(h) - T(h) = T(h) = -2^0h + 2^1 + 2^2 + \\dots + 2^{h-1} + 2^h \\]

        \u65b9\u7a0b\u5f0f\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\\(T(h)\\)\u306f\u7b49\u6bd4\u6570\u5217\u3067\u3042\u308a\u3001\u548c\u306e\u516c\u5f0f\u3092\u4f7f\u7528\u3057\u3066\u76f4\u63a5\u8a08\u7b97\u3067\u304d\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\u4ee5\u4e0b\u306b\u306a\u308a\u307e\u3059\uff1a

        \\[ \\begin{aligned} T(h) & = 2 \\frac{1 - 2^h}{1 - 2} - h \\newline & = 2^{h+1} - h - 2 \\newline & = O(2^h) \\end{aligned} \\]

        \u3055\u3089\u306b\u3001\u9ad8\u3055\\(h\\)\u306e\u5b8c\u5168\u4e8c\u5206\u6728\u306f\\(n = 2^{h+1} - 1\\)\u500b\u306e\u30ce\u30fc\u30c9\u3092\u6301\u3064\u305f\u3081\u3001\u8a08\u7b97\u91cf\u306f\\(O(2^h) = O(n)\\)\u3067\u3059\u3002\u3053\u306e\u8a08\u7b97\u306f\u3001**\u30ea\u30b9\u30c8\u3092\u5165\u529b\u3057\u3066\u30d2\u30fc\u30d7\u3092\u69cb\u7bc9\u3059\u308b\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(n)\\)\u3067\u3042\u308a\u3001\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3042\u308b**\u3053\u3068\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_heap/heap/","title":"8.1 \u00a0 \u30d2\u30fc\u30d7","text":"

        \u30d2\u30fc\u30d7\u306f\u7279\u5b9a\u306e\u6761\u4ef6\u3092\u6e80\u305f\u3059\u5b8c\u5099\u4e8c\u5206\u6728\u3067\u3001\u4e3b\u306b\u6b21\u306e2\u3064\u306e\u30bf\u30a4\u30d7\u306b\u5206\u985e\u3055\u308c\u307e\u3059\uff08\u4e0b\u56f3\u53c2\u7167\uff09\u3002

        • \u6700\u5c0f\u30d2\u30fc\u30d7\uff1a\u4efb\u610f\u306e\u30ce\u30fc\u30c9\u306e\u5024 \\(\\leq\\) \u305d\u306e\u5b50\u30ce\u30fc\u30c9\u306e\u5024\u3002
        • \u6700\u5927\u30d2\u30fc\u30d7\uff1a\u4efb\u610f\u306e\u30ce\u30fc\u30c9\u306e\u5024 \\(\\geq\\) \u305d\u306e\u5b50\u30ce\u30fc\u30c9\u306e\u5024\u3002

        \u56f3 8-1 \u00a0 \u6700\u5c0f\u30d2\u30fc\u30d7\u3068\u6700\u5927\u30d2\u30fc\u30d7

        \u5b8c\u5099\u4e8c\u5206\u6728\u306e\u7279\u5225\u306a\u30b1\u30fc\u30b9\u3068\u3057\u3066\u3001\u30d2\u30fc\u30d7\u306b\u306f\u4ee5\u4e0b\u306e\u7279\u6027\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u6700\u4e0b\u4f4d\u5c64\u306e\u30ce\u30fc\u30c9\u306f\u5de6\u304b\u3089\u53f3\u306b\u57cb\u3081\u3089\u308c\u3001\u4ed6\u306e\u5c64\u306e\u30ce\u30fc\u30c9\u306f\u5b8c\u5168\u306b\u57cb\u3081\u3089\u308c\u3066\u3044\u307e\u3059\u3002
        • \u4e8c\u5206\u6728\u306e\u6839\u30ce\u30fc\u30c9\u3092\u30d2\u30fc\u30d7\u306e\u300c\u5148\u982d\u300d\u3068\u547c\u3073\u3001\u6700\u3082\u53f3\u4e0b\u306e\u30ce\u30fc\u30c9\u3092\u30d2\u30fc\u30d7\u306e\u300c\u672b\u5c3e\u300d\u3068\u547c\u3073\u307e\u3059\u3002
        • \u6700\u5927\u30d2\u30fc\u30d7\uff08\u6700\u5c0f\u30d2\u30fc\u30d7\uff09\u306e\u5834\u5408\u3001\u5148\u982d\u8981\u7d20\uff08\u6839\uff09\u306e\u5024\u306f\u3059\u3079\u3066\u306e\u8981\u7d20\u306e\u4e2d\u3067\u6700\u5927\uff08\u6700\u5c0f\uff09\u3067\u3059\u3002
        "},{"location":"chapter_heap/heap/#811","title":"8.1.1 \u00a0 \u30d2\u30fc\u30d7\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c","text":"

        \u591a\u304f\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u304c\u512a\u5148\u5ea6\u30ad\u30e5\u30fc\u3092\u63d0\u4f9b\u3057\u3066\u3044\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3053\u308c\u306f\u512a\u5148\u5ea6\u4ed8\u304d\u30bd\u30fc\u30c8\u3092\u6301\u3064\u30ad\u30e5\u30fc\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u308b\u62bd\u8c61\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3059\u3002

        \u5b9f\u969b\u306b\u306f\u3001\u30d2\u30fc\u30d7\u306f\u512a\u5148\u5ea6\u30ad\u30e5\u30fc\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306b\u3088\u304f\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u6700\u5927\u30d2\u30fc\u30d7\u306f\u3001\u8981\u7d20\u304c\u964d\u9806\u3067\u30c7\u30ad\u30e5\u30fc\u3055\u308c\u308b\u512a\u5148\u5ea6\u30ad\u30e5\u30fc\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002\u4f7f\u7528\u306e\u89b3\u70b9\u304b\u3089\u3001\u300c\u512a\u5148\u5ea6\u30ad\u30e5\u30fc\u300d\u3068\u300c\u30d2\u30fc\u30d7\u300d\u3092\u540c\u7b49\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u3053\u306e\u672c\u3067\u306f\u4e21\u8005\u3092\u7279\u5225\u306b\u533a\u5225\u305b\u305a\u3001\u7d71\u4e00\u3057\u3066\u300c\u30d2\u30fc\u30d7\u300d\u3068\u547c\u3073\u307e\u3059\u3002

        \u30d2\u30fc\u30d7\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c\u3092\u4e0b\u8868\u306b\u793a\u3057\u307e\u3059\u3002\u30e1\u30bd\u30c3\u30c9\u540d\u306f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u3088\u3063\u3066\u7570\u306a\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002

        \u8868 8-1 \u00a0 \u30d2\u30fc\u30d7\u64cd\u4f5c\u306e\u52b9\u7387

        \u30e1\u30bd\u30c3\u30c9\u540d \u8aac\u660e \u6642\u9593\u8a08\u7b97\u91cf push() \u30d2\u30fc\u30d7\u306b\u8981\u7d20\u3092\u8ffd\u52a0 \\(O(\\log n)\\) pop() \u30d2\u30fc\u30d7\u304b\u3089\u5148\u982d\u8981\u7d20\u3092\u524a\u9664 \\(O(\\log n)\\) peek() \u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\uff08\u6700\u5927/\u6700\u5c0f\u30d2\u30fc\u30d7\u306e\u5834\u5408\u3001\u6700\u5927/\u6700\u5c0f\u5024\uff09 \\(O(1)\\) size() \u30d2\u30fc\u30d7\u5185\u306e\u8981\u7d20\u6570\u3092\u53d6\u5f97 \\(O(1)\\) isEmpty() \u30d2\u30fc\u30d7\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af \\(O(1)\\)

        \u5b9f\u969b\u306b\u306f\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u3088\u3063\u3066\u63d0\u4f9b\u3055\u308c\u308b\u30d2\u30fc\u30d7\u30af\u30e9\u30b9\uff08\u307e\u305f\u306f\u512a\u5148\u5ea6\u30ad\u30e5\u30fc\u30af\u30e9\u30b9\uff09\u3092\u76f4\u63a5\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002

        \u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u300c\u6607\u9806\u300d\u3068\u300c\u964d\u9806\u300d\u304c\u3042\u308b\u3088\u3046\u306b\u3001flag\u3092\u8a2d\u5b9a\u3059\u308b\u304bComparator\u3092\u5909\u66f4\u3059\u308b\u3053\u3068\u3067\u300c\u6700\u5c0f\u30d2\u30fc\u30d7\u300d\u3068\u300c\u6700\u5927\u30d2\u30fc\u30d7\u300d\u3092\u5207\u308a\u66ff\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby heap.py
        # \u6700\u5c0f\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\nmin_heap, flag = [], 1\n# \u6700\u5927\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\nmax_heap, flag = [], -1\n\n# Python\u306eheapq\u30e2\u30b8\u30e5\u30fc\u30eb\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u6700\u5c0f\u30d2\u30fc\u30d7\u3092\u5b9f\u88c5\n# \u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u30d7\u30c3\u30b7\u30e5\u3059\u308b\u524d\u306b\u8ca0\u306e\u5024\u306b\u3059\u308b\u3053\u3068\u3067\u3001\u9806\u5e8f\u3092\u53cd\u8ee2\u3055\u305b\u3001\u6700\u5927\u30d2\u30fc\u30d7\u3092\u5b9f\u88c5\n# \u3053\u306e\u4f8b\u3067\u306f\u3001flag = 1\u306f\u6700\u5c0f\u30d2\u30fc\u30d7\u306b\u5bfe\u5fdc\u3057\u3001flag = -1\u306f\u6700\u5927\u30d2\u30fc\u30d7\u306b\u5bfe\u5fdc\n\n# \u30d2\u30fc\u30d7\u306b\u8981\u7d20\u3092\u30d7\u30c3\u30b7\u30e5\nheapq.heappush(max_heap, flag * 1)\nheapq.heappush(max_heap, flag * 3)\nheapq.heappush(max_heap, flag * 2)\nheapq.heappush(max_heap, flag * 5)\nheapq.heappush(max_heap, flag * 4)\n\n# \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u53d6\u5f97\npeek: int = flag * max_heap[0] # 5\n\n# \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u30dd\u30c3\u30d7\n# \u30dd\u30c3\u30d7\u3055\u308c\u305f\u8981\u7d20\u306f\u964d\u9806\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u5f62\u6210\nval = flag * heapq.heappop(max_heap) # 5\nval = flag * heapq.heappop(max_heap) # 4\nval = flag * heapq.heappop(max_heap) # 3\nval = flag * heapq.heappop(max_heap) # 2\nval = flag * heapq.heappop(max_heap) # 1\n\n# \u30d2\u30fc\u30d7\u306e\u30b5\u30a4\u30ba\u3092\u53d6\u5f97\nsize: int = len(max_heap)\n\n# \u30d2\u30fc\u30d7\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\nis_empty: bool = not max_heap\n\n# \u30ea\u30b9\u30c8\u304b\u3089\u30d2\u30fc\u30d7\u3092\u4f5c\u6210\nmin_heap: list[int] = [1, 3, 2, 5, 4]\nheapq.heapify(min_heap)\n
        heap.cpp
        /* \u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316 */\n// \u6700\u5c0f\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\npriority_queue<int, vector<int>, greater<int>> minHeap;\n// \u6700\u5927\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\npriority_queue<int, vector<int>, less<int>> maxHeap;\n\n/* \u30d2\u30fc\u30d7\u306b\u8981\u7d20\u3092\u30d7\u30c3\u30b7\u30e5 */\nmaxHeap.push(1);\nmaxHeap.push(3);\nmaxHeap.push(2);\nmaxHeap.push(5);\nmaxHeap.push(4);\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u53d6\u5f97 */\nint peek = maxHeap.top(); // 5\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\n// \u30dd\u30c3\u30d7\u3055\u308c\u305f\u8981\u7d20\u306f\u964d\u9806\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u5f62\u6210\nmaxHeap.pop(); // 5\nmaxHeap.pop(); // 4\nmaxHeap.pop(); // 3\nmaxHeap.pop(); // 2\nmaxHeap.pop(); // 1\n\n/* \u30d2\u30fc\u30d7\u306e\u30b5\u30a4\u30ba\u3092\u53d6\u5f97 */\nint size = maxHeap.size();\n\n/* \u30d2\u30fc\u30d7\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af */\nbool isEmpty = maxHeap.empty();\n\n/* \u30ea\u30b9\u30c8\u304b\u3089\u30d2\u30fc\u30d7\u3092\u4f5c\u6210 */\nvector<int> input{1, 3, 2, 5, 4};\npriority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());\n
        heap.java
        /* \u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316 */\n// \u6700\u5c0f\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\nQueue<Integer> minHeap = new PriorityQueue<>();\n// \u6700\u5927\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\uff08\u30e9\u30e0\u30c0\u5f0f\u3067Comparator\u3092\u5909\u66f4\u3059\u308b\u3060\u3051\uff09\nQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);\n\n/* \u30d2\u30fc\u30d7\u306b\u8981\u7d20\u3092\u30d7\u30c3\u30b7\u30e5 */\nmaxHeap.offer(1);\nmaxHeap.offer(3);\nmaxHeap.offer(2);\nmaxHeap.offer(5);\nmaxHeap.offer(4);\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u53d6\u5f97 */\nint peek = maxHeap.peek(); // 5\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\n// \u30dd\u30c3\u30d7\u3055\u308c\u305f\u8981\u7d20\u306f\u964d\u9806\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u5f62\u6210\npeek = maxHeap.poll(); // 5\npeek = maxHeap.poll(); // 4\npeek = maxHeap.poll(); // 3\npeek = maxHeap.poll(); // 2\npeek = maxHeap.poll(); // 1\n\n/* \u30d2\u30fc\u30d7\u306e\u30b5\u30a4\u30ba\u3092\u53d6\u5f97 */\nint size = maxHeap.size();\n\n/* \u30d2\u30fc\u30d7\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af */\nboolean isEmpty = maxHeap.isEmpty();\n\n/* \u30ea\u30b9\u30c8\u304b\u3089\u30d2\u30fc\u30d7\u3092\u4f5c\u6210 */\nminHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));\n
        heap.cs
        /* \u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316 */\n// \u6700\u5c0f\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\nPriorityQueue<int, int> minHeap = new();\n// \u6700\u5927\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\uff08\u30e9\u30e0\u30c0\u5f0f\u3067Comparator\u3092\u5909\u66f4\u3059\u308b\u3060\u3051\uff09\nPriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y - x));\n\n/* \u30d2\u30fc\u30d7\u306b\u8981\u7d20\u3092\u30d7\u30c3\u30b7\u30e5 */\nmaxHeap.Enqueue(1, 1);\nmaxHeap.Enqueue(3, 3);\nmaxHeap.Enqueue(2, 2);\nmaxHeap.Enqueue(5, 5);\nmaxHeap.Enqueue(4, 4);\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u53d6\u5f97 */\nint peek = maxHeap.Peek();//5\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\n// \u30dd\u30c3\u30d7\u3055\u308c\u305f\u8981\u7d20\u306f\u964d\u9806\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u5f62\u6210\npeek = maxHeap.Dequeue();  // 5\npeek = maxHeap.Dequeue();  // 4\npeek = maxHeap.Dequeue();  // 3\npeek = maxHeap.Dequeue();  // 2\npeek = maxHeap.Dequeue();  // 1\n\n/* \u30d2\u30fc\u30d7\u306e\u30b5\u30a4\u30ba\u3092\u53d6\u5f97 */\nint size = maxHeap.Count;\n\n/* \u30d2\u30fc\u30d7\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af */\nbool isEmpty = maxHeap.Count == 0;\n\n/* \u30ea\u30b9\u30c8\u304b\u3089\u30d2\u30fc\u30d7\u3092\u4f5c\u6210 */\nminHeap = new PriorityQueue<int, int>([(1, 1), (3, 3), (2, 2), (5, 5), (4, 4)]);\n
        heap.go
        // Go\u3067\u306f\u3001heap.Interface\u3092\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3067\u6574\u6570\u306e\u6700\u5927\u30d2\u30fc\u30d7\u3092\u69cb\u7bc9\u3067\u304d\u307e\u3059\n// heap.Interface\u3092\u5b9f\u88c5\u3059\u308b\u306b\u306f\u3001sort.Interface\u3082\u5b9f\u88c5\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\ntype intHeap []any\n\n// heap.Interface\u306ePush\u30e1\u30bd\u30c3\u30c9\u3001\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u30d7\u30c3\u30b7\u30e5\nfunc (h *intHeap) Push(x any) {\n    // Push\u3068Pop\u306e\u4e21\u65b9\u3067\u30dd\u30a4\u30f3\u30bf\u30ec\u30b7\u30fc\u30d0\u30fc\u3092\u4f7f\u7528\n    // \u30b9\u30e9\u30a4\u30b9\u306e\u8981\u7d20\u3092\u8abf\u6574\u3059\u308b\u3060\u3051\u3067\u306a\u304f\u3001\u305d\u306e\u9577\u3055\u3082\u5909\u66f4\u3059\u308b\u305f\u3081\n    *h = append(*h, x.(int))\n}\n\n// heap.Interface\u306ePop\u30e1\u30bd\u30c3\u30c9\u3001\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u524a\u9664\nfunc (h *intHeap) Pop() any {\n    // \u30d2\u30fc\u30d7\u304b\u3089\u30dd\u30c3\u30d7\u3059\u308b\u8981\u7d20\u306f\u672b\u5c3e\u306b\u683c\u7d0d\n    last := (*h)[len(*h)-1]\n    *h = (*h)[:len(*h)-1]\n    return last\n}\n\n// sort.Interface\u306eLen\u30e1\u30bd\u30c3\u30c9\nfunc (h *intHeap) Len() int {\n    return len(*h)\n}\n\n// sort.Interface\u306eLess\u30e1\u30bd\u30c3\u30c9\nfunc (h *intHeap) Less(i, j int) bool {\n    // \u6700\u5c0f\u30d2\u30fc\u30d7\u3092\u5b9f\u88c5\u3057\u305f\u3044\u5834\u5408\u306f\u3001\u3053\u308c\u3092\u5c0f\u306a\u308a\u6bd4\u8f03\u306b\u5909\u66f4\n    return (*h)[i].(int) > (*h)[j].(int)\n}\n\n// sort.Interface\u306eSwap\u30e1\u30bd\u30c3\u30c9\nfunc (h *intHeap) Swap(i, j int) {\n    (*h)[i], (*h)[j] = (*h)[j], (*h)[i]\n}\n\n// Top \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u53d6\u5f97\nfunc (h *intHeap) Top() any {\n    return (*h)[0]\n}\n\n/* \u30c9\u30e9\u30a4\u30d0\u30fc\u30b3\u30fc\u30c9 */\nfunc TestHeap(t *testing.T) {\n    /* \u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316 */\n    // \u6700\u5927\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\n    maxHeap := &intHeap{}\n    heap.Init(maxHeap)\n    /* \u30d2\u30fc\u30d7\u306b\u8981\u7d20\u3092\u30d7\u30c3\u30b7\u30e5 */\n    // heap.Interface\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u3066\u8981\u7d20\u3092\u8ffd\u52a0\n    heap.Push(maxHeap, 1)\n    heap.Push(maxHeap, 3)\n    heap.Push(maxHeap, 2)\n    heap.Push(maxHeap, 4)\n    heap.Push(maxHeap, 5)\n\n    /* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u53d6\u5f97 */\n    top := maxHeap.Top()\n    fmt.Printf(\"\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u306f %d\\n\", top)\n\n    /* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\n    // heap.Interface\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u3066\u8981\u7d20\u3092\u524a\u9664\n    heap.Pop(maxHeap) // 5\n    heap.Pop(maxHeap) // 4\n    heap.Pop(maxHeap) // 3\n    heap.Pop(maxHeap) // 2\n    heap.Pop(maxHeap) // 1\n\n    /* \u30d2\u30fc\u30d7\u306e\u30b5\u30a4\u30ba\u3092\u53d6\u5f97 */\n    size := len(*maxHeap)\n    fmt.Printf(\"\u30d2\u30fc\u30d7\u5185\u306e\u8981\u7d20\u6570\u306f %d\\n\", size)\n\n    /* \u30d2\u30fc\u30d7\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af */\n    isEmpty := len(*maxHeap) == 0\n    fmt.Printf(\"\u30d2\u30fc\u30d7\u306f\u7a7a\u3067\u3059\u304b\uff1f %t\\n\", isEmpty)\n}\n
        heap.swift
        /* \u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316 */\n// Swift\u306eHeap\u578b\u306f\u6700\u5927\u30d2\u30fc\u30d7\u3068\u6700\u5c0f\u30d2\u30fc\u30d7\u306e\u4e21\u65b9\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3001swift-collections\u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u5fc5\u8981\nvar heap = Heap<Int>()\n\n/* \u30d2\u30fc\u30d7\u306b\u8981\u7d20\u3092\u30d7\u30c3\u30b7\u30e5 */\nheap.insert(1)\nheap.insert(3)\nheap.insert(2)\nheap.insert(5)\nheap.insert(4)\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u53d6\u5f97 */\nvar peek = heap.max()!\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\npeek = heap.removeMax() // 5\npeek = heap.removeMax() // 4\npeek = heap.removeMax() // 3\npeek = heap.removeMax() // 2\npeek = heap.removeMax() // 1\n\n/* \u30d2\u30fc\u30d7\u306e\u30b5\u30a4\u30ba\u3092\u53d6\u5f97 */\nlet size = heap.count\n\n/* \u30d2\u30fc\u30d7\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af */\nlet isEmpty = heap.isEmpty\n\n/* \u30ea\u30b9\u30c8\u304b\u3089\u30d2\u30fc\u30d7\u3092\u4f5c\u6210 */\nlet heap2 = Heap([1, 3, 2, 5, 4])\n
        heap.js
        // JavaScript\u306f\u7d44\u307f\u8fbc\u307f\u306eHeap\u30af\u30e9\u30b9\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        heap.ts
        // TypeScript\u306f\u7d44\u307f\u8fbc\u307f\u306eHeap\u30af\u30e9\u30b9\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        heap.dart
        // Dart\u306f\u7d44\u307f\u8fbc\u307f\u306eHeap\u30af\u30e9\u30b9\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        heap.rs
        use std::collections::BinaryHeap;\nuse std::cmp::Reverse;\n\n/* \u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316 */\n// \u6700\u5c0f\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\nlet mut min_heap = BinaryHeap::<Reverse<i32>>::new();\n// \u6700\u5927\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\nlet mut max_heap = BinaryHeap::new();\n\n/* \u30d2\u30fc\u30d7\u306b\u8981\u7d20\u3092\u30d7\u30c3\u30b7\u30e5 */\nmax_heap.push(1);\nmax_heap.push(3);\nmax_heap.push(2);\nmax_heap.push(5);\nmax_heap.push(4);\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u53d6\u5f97 */\nlet peek = max_heap.peek().unwrap();  // 5\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\n// \u30dd\u30c3\u30d7\u3055\u308c\u305f\u8981\u7d20\u306f\u964d\u9806\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u5f62\u6210\nlet peek = max_heap.pop().unwrap();   // 5\nlet peek = max_heap.pop().unwrap();   // 4\nlet peek = max_heap.pop().unwrap();   // 3\nlet peek = max_heap.pop().unwrap();   // 2\nlet peek = max_heap.pop().unwrap();   // 1\n\n/* \u30d2\u30fc\u30d7\u306e\u30b5\u30a4\u30ba\u3092\u53d6\u5f97 */\nlet size = max_heap.len();\n\n/* \u30d2\u30fc\u30d7\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af */\nlet is_empty = max_heap.is_empty();\n\n/* \u30ea\u30b9\u30c8\u304b\u3089\u30d2\u30fc\u30d7\u3092\u4f5c\u6210 */\nlet min_heap = BinaryHeap::from(vec![Reverse(1), Reverse(3), Reverse(2), Reverse(5), Reverse(4)]);\n
        heap.c
        // C\u306f\u7d44\u307f\u8fbc\u307f\u306eHeap\u30af\u30e9\u30b9\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        heap.kt
        /* \u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316 */\n// \u6700\u5c0f\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\nvar minHeap = PriorityQueue<Int>()\n// \u6700\u5927\u30d2\u30fc\u30d7\u306e\u521d\u671f\u5316\uff08\u30e9\u30e0\u30c0\u5f0f\u3067Comparator\u3092\u5909\u66f4\u3059\u308b\u3060\u3051\uff09\nval maxHeap = PriorityQueue { a: Int, b: Int -> b - a }\n\n/* \u30d2\u30fc\u30d7\u306b\u8981\u7d20\u3092\u30d7\u30c3\u30b7\u30e5 */\nmaxHeap.offer(1)\nmaxHeap.offer(3)\nmaxHeap.offer(2)\nmaxHeap.offer(5)\nmaxHeap.offer(4)\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u53d6\u5f97 */\nvar peek = maxHeap.peek() // 5\n\n/* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\n// \u30dd\u30c3\u30d7\u3055\u308c\u305f\u8981\u7d20\u306f\u964d\u9806\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u5f62\u6210\npeek = maxHeap.poll() // 5\npeek = maxHeap.poll() // 4\npeek = maxHeap.poll() // 3\npeek = maxHeap.poll() // 2\npeek = maxHeap.poll() // 1\n\n/* \u30d2\u30fc\u30d7\u306e\u30b5\u30a4\u30ba\u3092\u53d6\u5f97 */\nval size = maxHeap.size\n\n/* \u30d2\u30fc\u30d7\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af */\nval isEmpty = maxHeap.isEmpty()\n\n/* \u30ea\u30b9\u30c8\u304b\u3089\u30d2\u30fc\u30d7\u3092\u4f5c\u6210 */\nminHeap = PriorityQueue(mutableListOf(1, 3, 2, 5, 4))\n
        heap.rb
        \n
        "},{"location":"chapter_heap/heap/#812","title":"8.1.2 \u00a0 \u30d2\u30fc\u30d7\u306e\u5b9f\u88c5","text":"

        \u4ee5\u4e0b\u306e\u5b9f\u88c5\u306f\u6700\u5927\u30d2\u30fc\u30d7\u3067\u3059\u3002\u6700\u5c0f\u30d2\u30fc\u30d7\u306b\u5909\u63db\u3059\u308b\u306b\u306f\u3001\u3059\u3079\u3066\u306e\u30b5\u30a4\u30ba\u8ad6\u7406\u6bd4\u8f03\u3092\u53cd\u8ee2\u3055\u305b\u308b\u3060\u3051\u3067\u3059\uff08\u4f8b\u3048\u3070\u3001\\(\\geq\\)\u3092\\(\\leq\\)\u306b\u7f6e\u304d\u63db\u3048\u308b\uff09\u3002\u8208\u5473\u306e\u3042\u308b\u8aad\u8005\u306f\u81ea\u5206\u3067\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002

        "},{"location":"chapter_heap/heap/#1","title":"1. \u00a0 \u30d2\u30fc\u30d7\u306e\u683c\u7d0d\u3068\u8868\u73fe","text":"

        \u300c\u4e8c\u5206\u6728\u300d\u306e\u7bc0\u3067\u8ff0\u3079\u305f\u3088\u3046\u306b\u3001\u5b8c\u5099\u4e8c\u5206\u6728\u306f\u914d\u5217\u8868\u73fe\u306b\u975e\u5e38\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u30d2\u30fc\u30d7\u306f\u5b8c\u5099\u4e8c\u5206\u6728\u306e\u4e00\u7a2e\u306a\u306e\u3067\u3001\u914d\u5217\u3092\u4f7f\u7528\u3057\u3066\u30d2\u30fc\u30d7\u3092\u683c\u7d0d\u3057\u307e\u3059\u3002

        \u914d\u5217\u3092\u4f7f\u7528\u3057\u3066\u4e8c\u5206\u6728\u3092\u8868\u73fe\u3059\u308b\u5834\u5408\u3001\u8981\u7d20\u306f\u30ce\u30fc\u30c9\u5024\u3092\u8868\u3057\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u4e8c\u5206\u6728\u5185\u306e\u30ce\u30fc\u30c9\u4f4d\u7f6e\u3092\u8868\u3057\u307e\u3059\u3002\u30ce\u30fc\u30c9\u30dd\u30a4\u30f3\u30bf\u306f\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u30de\u30c3\u30d4\u30f3\u30b0\u516c\u5f0f\u3092\u901a\u3058\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(i\\)\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u305d\u306e\u5de6\u306e\u5b50\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\\(2i + 1\\)\u3001\u53f3\u306e\u5b50\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\\(2i + 2\\)\u3001\u89aa\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\\((i - 1) / 2\\)\uff08\u5e8a\u9664\u7b97\uff09\u3067\u3059\u3002\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001null\u30ce\u30fc\u30c9\u307e\u305f\u306f\u30ce\u30fc\u30c9\u304c\u5b58\u5728\u3057\u306a\u3044\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

        \u56f3 8-2 \u00a0 \u30d2\u30fc\u30d7\u306e\u8868\u73fe\u3068\u683c\u7d0d

        \u5f8c\u3067\u4fbf\u5229\u306b\u4f7f\u7528\u3059\u308b\u305f\u3081\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u30de\u30c3\u30d4\u30f3\u30b0\u516c\u5f0f\u3092\u95a2\u6570\u306b\u30ab\u30d7\u30bb\u30eb\u5316\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
        def left(self, i: int) -> int:\n    \"\"\"\u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\"\"\"\n    return 2 * i + 1\n\ndef right(self, i: int) -> int:\n    \"\"\"\u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\"\"\"\n    return 2 * i + 2\n\ndef parent(self, i: int) -> int:\n    \"\"\"\u89aa\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\"\"\"\n    return (i - 1) // 2  # \u6574\u6570\u9664\u7b97\u3067\u5207\u308a\u4e0b\u3052\n
        my_heap.cpp
        /* \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\nint left(int i) {\n    return 2 * i + 1;\n}\n\n/* \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\nint right(int i) {\n    return 2 * i + 2;\n}\n\n/* \u89aa\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\nint parent(int i) {\n    return (i - 1) / 2; // \u6574\u6570\u9664\u7b97\u3067\u5207\u308a\u4e0b\u3052\n}\n
        my_heap.java
        /* \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\nint left(int i) {\n    return 2 * i + 1;\n}\n\n/* \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\nint right(int i) {\n    return 2 * i + 2;\n}\n\n/* \u89aa\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\nint parent(int i) {\n    return (i - 1) / 2; // \u6574\u6570\u9664\u7b97\u3067\u5207\u308a\u4e0b\u3052\n}\n
        my_heap.cs
        [class]{MaxHeap}-[func]{Left}\n\n[class]{MaxHeap}-[func]{Right}\n\n[class]{MaxHeap}-[func]{Parent}\n
        my_heap.go
        [class]{maxHeap}-[func]{left}\n\n[class]{maxHeap}-[func]{right}\n\n[class]{maxHeap}-[func]{parent}\n
        my_heap.swift
        [class]{MaxHeap}-[func]{left}\n\n[class]{MaxHeap}-[func]{right}\n\n[class]{MaxHeap}-[func]{parent}\n
        my_heap.js
        [class]{MaxHeap}-[func]{left}\n\n[class]{MaxHeap}-[func]{right}\n\n[class]{MaxHeap}-[func]{parent}\n
        my_heap.ts
        [class]{MaxHeap}-[func]{left}\n\n[class]{MaxHeap}-[func]{right}\n\n[class]{MaxHeap}-[func]{parent}\n
        my_heap.dart
        [class]{MaxHeap}-[func]{_left}\n\n[class]{MaxHeap}-[func]{_right}\n\n[class]{MaxHeap}-[func]{_parent}\n
        my_heap.rs
        [class]{MaxHeap}-[func]{left}\n\n[class]{MaxHeap}-[func]{right}\n\n[class]{MaxHeap}-[func]{parent}\n
        my_heap.c
        [class]{MaxHeap}-[func]{left}\n\n[class]{MaxHeap}-[func]{right}\n\n[class]{MaxHeap}-[func]{parent}\n
        my_heap.kt
        [class]{MaxHeap}-[func]{left}\n\n[class]{MaxHeap}-[func]{right}\n\n[class]{MaxHeap}-[func]{parent}\n
        my_heap.rb
        [class]{MaxHeap}-[func]{left}\n\n[class]{MaxHeap}-[func]{right}\n\n[class]{MaxHeap}-[func]{parent}\n
        "},{"location":"chapter_heap/heap/#2","title":"2. \u00a0 \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3078\u306e\u30a2\u30af\u30bb\u30b9","text":"

        \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u306f\u4e8c\u5206\u6728\u306e\u6839\u30ce\u30fc\u30c9\u3067\u3001\u30ea\u30b9\u30c8\u306e\u6700\u521d\u306e\u8981\u7d20\u3067\u3082\u3042\u308a\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
        def peek(self) -> int:\n    \"\"\"\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\"\"\"\n    return self.max_heap[0]\n
        my_heap.cpp
        /* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint peek() {\n    return maxHeap[0];\n}\n
        my_heap.java
        /* \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint peek() {\n    return maxHeap.get(0);\n}\n
        my_heap.cs
        [class]{MaxHeap}-[func]{Peek}\n
        my_heap.go
        [class]{maxHeap}-[func]{peek}\n
        my_heap.swift
        [class]{MaxHeap}-[func]{peek}\n
        my_heap.js
        [class]{MaxHeap}-[func]{peek}\n
        my_heap.ts
        [class]{MaxHeap}-[func]{peek}\n
        my_heap.dart
        [class]{MaxHeap}-[func]{peek}\n
        my_heap.rs
        [class]{MaxHeap}-[func]{peek}\n
        my_heap.c
        [class]{MaxHeap}-[func]{peek}\n
        my_heap.kt
        [class]{MaxHeap}-[func]{peek}\n
        my_heap.rb
        [class]{MaxHeap}-[func]{peek}\n
        "},{"location":"chapter_heap/heap/#3","title":"3. \u00a0 \u30d2\u30fc\u30d7\u3078\u306e\u8981\u7d20\u633f\u5165","text":"

        \u8981\u7d20val\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u307e\u305a\u305d\u308c\u3092\u30d2\u30fc\u30d7\u306e\u5e95\u306b\u8ffd\u52a0\u3057\u307e\u3059\u3002\u8ffd\u52a0\u5f8c\u3001val\u304c\u30d2\u30fc\u30d7\u5185\u306e\u4ed6\u306e\u8981\u7d20\u3088\u308a\u5927\u304d\u3044\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3001\u30d2\u30fc\u30d7\u306e\u5b8c\u5168\u6027\u304c\u640d\u306a\u308f\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u633f\u5165\u3055\u308c\u305f\u30ce\u30fc\u30c9\u304b\u3089\u6839\u30ce\u30fc\u30c9\u307e\u3067\u306e\u30d1\u30b9\u3092\u4fee\u5fa9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u64cd\u4f5c\u306f\u30d2\u30fc\u30d7\u5316\u3068\u547c\u3070\u308c\u307e\u3059\u3002

        \u633f\u5165\u3055\u308c\u305f\u30ce\u30fc\u30c9\u304b\u3089\u958b\u59cb\u3057\u3066\u3001\u4e0b\u304b\u3089\u4e0a\u306b\u30d2\u30fc\u30d7\u5316\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u633f\u5165\u3055\u308c\u305f\u30ce\u30fc\u30c9\u306e\u5024\u3092\u305d\u306e\u89aa\u30ce\u30fc\u30c9\u3068\u6bd4\u8f03\u3057\u3001\u633f\u5165\u3055\u308c\u305f\u30ce\u30fc\u30c9\u304c\u5927\u304d\u3044\u5834\u5408\u306f\u305d\u308c\u3089\u3092\u4ea4\u63db\u3057\u307e\u3059\u3002\u6b21\u306b\u3053\u306e\u64cd\u4f5c\u3092\u7d9a\u884c\u3057\u3001\u6839\u306b\u5230\u9054\u3059\u308b\u304b\u3001\u4ea4\u63db\u304c\u4e0d\u8981\u306a\u30ce\u30fc\u30c9\u306b\u906d\u9047\u3059\u308b\u307e\u3067\u3001\u4e0b\u304b\u3089\u4e0a\u306b\u30d2\u30fc\u30d7\u5185\u306e\u5404\u30ce\u30fc\u30c9\u3092\u4fee\u5fa9\u3057\u307e\u3059\u3002

        <1><2><3><4><5><6><7><8><9>

        \u56f3 8-3 \u00a0 \u30d2\u30fc\u30d7\u3078\u306e\u8981\u7d20\u633f\u5165\u306e\u624b\u9806

        \u7dcf\u30ce\u30fc\u30c9\u6570\u3092\\(n\\)\u3068\u3059\u308b\u3068\u3001\u6728\u306e\u9ad8\u3055\u306f\\(O(\\log n)\\)\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30d2\u30fc\u30d7\u5316\u64cd\u4f5c\u306e\u30eb\u30fc\u30d7\u53cd\u5fa9\u56de\u6570\u306f\u6700\u5927\\(O(\\log n)\\)\u3067\u3001\u8981\u7d20\u633f\u5165\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(\\log n)\\)\u306b\u306a\u308a\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
        def push(self, val: int):\n    \"\"\"\u30d2\u30fc\u30d7\u306b\u8981\u7d20\u3092\u30d7\u30c3\u30b7\u30e5\"\"\"\n    # \u30ce\u30fc\u30c9\u3092\u8ffd\u52a0\n    self.max_heap.append(val)\n    # \u4e0b\u304b\u3089\u4e0a\u3078\u30d2\u30fc\u30d7\u5316\n    self.sift_up(self.size() - 1)\n\ndef sift_up(self, i: int):\n    \"\"\"\u30ce\u30fc\u30c9i\u304b\u3089\u958b\u59cb\u3057\u3066\u3001\u4e0b\u304b\u3089\u4e0a\u3078\u30d2\u30fc\u30d7\u5316\"\"\"\n    while True:\n        # \u30ce\u30fc\u30c9i\u306e\u89aa\u30ce\u30fc\u30c9\u3092\u53d6\u5f97\n        p = self.parent(i)\n        # \u300c\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u8d8a\u3048\u308b\u300d\u307e\u305f\u306f\u300c\u30ce\u30fc\u30c9\u304c\u4fee\u5fa9\u4e0d\u8981\u300d\u306e\u5834\u5408\u3001\u30d2\u30fc\u30d7\u5316\u3092\u7d42\u4e86\n        if p < 0 or self.max_heap[i] <= self.max_heap[p]:\n            break\n        # 2\u3064\u306e\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\n        self.swap(i, p)\n        # \u4e0a\u5411\u304d\u306e\u30eb\u30fc\u30d7\u30d2\u30fc\u30d7\u5316\n        i = p\n
        my_heap.cpp
        /* \u30d2\u30fc\u30d7\u306b\u8981\u7d20\u3092\u30d7\u30c3\u30b7\u30e5 */\nvoid push(int val) {\n    // \u30ce\u30fc\u30c9\u3092\u8ffd\u52a0\n    maxHeap.push_back(val);\n    // \u4e0b\u304b\u3089\u4e0a\u3078\u30d2\u30fc\u30d7\u5316\n    siftUp(size() - 1);\n}\n\n/* \u30ce\u30fc\u30c9i\u304b\u3089\u4e0a\u5411\u304d\u306b\u30d2\u30fc\u30d7\u5316\u3092\u958b\u59cb */\nvoid siftUp(int i) {\n    while (true) {\n        // \u30ce\u30fc\u30c9i\u306e\u89aa\u30ce\u30fc\u30c9\u3092\u53d6\u5f97\n        int p = parent(i);\n        // \u300c\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u8d85\u3048\u308b\u300d\u307e\u305f\u306f\u300c\u30ce\u30fc\u30c9\u304c\u4fee\u5fa9\u4e0d\u8981\u300d\u306e\u5834\u5408\u3001\u30d2\u30fc\u30d7\u5316\u3092\u7d42\u4e86\n        if (p < 0 || maxHeap[i] <= maxHeap[p])\n            break;\n        // 2\u3064\u306e\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\n        swap(maxHeap[i], maxHeap[p]);\n        // \u4e0a\u5411\u304d\u306b\u30eb\u30fc\u30d7\u3057\u3066\u30d2\u30fc\u30d7\u5316\n        i = p;\n    }\n}\n
        my_heap.java
        /* \u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u30d7\u30c3\u30b7\u30e5 */\nvoid push(int val) {\n    // \u30ce\u30fc\u30c9\u3092\u8ffd\u52a0\n    maxHeap.add(val);\n    // \u4e0b\u304b\u3089\u4e0a\u3078\u30d2\u30fc\u30d7\u5316\n    siftUp(size() - 1);\n}\n\n/* \u30ce\u30fc\u30c9 i \u304b\u3089\u4e0a\u5411\u304d\u306b\u30d2\u30fc\u30d7\u5316\u3092\u958b\u59cb */\nvoid siftUp(int i) {\n    while (true) {\n        // \u30ce\u30fc\u30c9 i \u306e\u89aa\u30ce\u30fc\u30c9\u3092\u53d6\u5f97\n        int p = parent(i);\n        // \u300c\u6839\u30ce\u30fc\u30c9\u3092\u8d8a\u3048\u308b\u300d\u307e\u305f\u306f\u300c\u30ce\u30fc\u30c9\u304c\u4fee\u5fa9\u4e0d\u8981\u300d\u306e\u5834\u5408\u3001\u30d2\u30fc\u30d7\u5316\u3092\u7d42\u4e86\n        if (p < 0 || maxHeap.get(i) <= maxHeap.get(p))\n            break;\n        // 2\u3064\u306e\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\n        swap(i, p);\n        // \u4e0a\u5411\u304d\u306b\u30d2\u30fc\u30d7\u5316\u3092\u30eb\u30fc\u30d7\n        i = p;\n    }\n}\n
        my_heap.cs
        [class]{MaxHeap}-[func]{Push}\n\n[class]{MaxHeap}-[func]{SiftUp}\n
        my_heap.go
        [class]{maxHeap}-[func]{push}\n\n[class]{maxHeap}-[func]{siftUp}\n
        my_heap.swift
        [class]{MaxHeap}-[func]{push}\n\n[class]{MaxHeap}-[func]{siftUp}\n
        my_heap.js
        [class]{MaxHeap}-[func]{push}\n\n[class]{MaxHeap}-[func]{siftUp}\n
        my_heap.ts
        [class]{MaxHeap}-[func]{push}\n\n[class]{MaxHeap}-[func]{siftUp}\n
        my_heap.dart
        [class]{MaxHeap}-[func]{push}\n\n[class]{MaxHeap}-[func]{siftUp}\n
        my_heap.rs
        [class]{MaxHeap}-[func]{push}\n\n[class]{MaxHeap}-[func]{sift_up}\n
        my_heap.c
        [class]{MaxHeap}-[func]{push}\n\n[class]{MaxHeap}-[func]{siftUp}\n
        my_heap.kt
        [class]{MaxHeap}-[func]{push}\n\n[class]{MaxHeap}-[func]{siftUp}\n
        my_heap.rb
        [class]{MaxHeap}-[func]{push}\n\n[class]{MaxHeap}-[func]{sift_up}\n
        "},{"location":"chapter_heap/heap/#4","title":"4. \u00a0 \u30d2\u30fc\u30d7\u304b\u3089\u306e\u5148\u982d\u8981\u7d20\u524a\u9664","text":"

        \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u306f\u4e8c\u5206\u6728\u306e\u6839\u30ce\u30fc\u30c9\u3001\u3064\u307e\u308a\u30ea\u30b9\u30c8\u306e\u6700\u521d\u306e\u8981\u7d20\u3067\u3059\u3002\u30ea\u30b9\u30c8\u304b\u3089\u6700\u521d\u306e\u8981\u7d20\u3092\u76f4\u63a5\u524a\u9664\u3059\u308b\u3068\u3001\u4e8c\u5206\u6728\u5185\u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u5909\u66f4\u3055\u308c\u3001\u5f8c\u7d9a\u306e\u4fee\u5fa9\u306b\u30d2\u30fc\u30d7\u5316\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u56f0\u96e3\u306b\u306a\u308a\u307e\u3059\u3002\u8981\u7d20\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u5909\u66f4\u3092\u6700\u5c0f\u9650\u306b\u6291\u3048\u308b\u305f\u3081\u3001\u6b21\u306e\u624b\u9806\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        1. \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3068\u5e95\u306e\u8981\u7d20\u3092\u4ea4\u63db\u3057\u307e\u3059\uff08\u6839\u30ce\u30fc\u30c9\u3068\u6700\u3082\u53f3\u306e\u8449\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\uff09\u3002
        2. \u4ea4\u63db\u5f8c\u3001\u30ea\u30b9\u30c8\u304b\u3089\u30d2\u30fc\u30d7\u306e\u5e95\u3092\u524a\u9664\u3057\u307e\u3059\uff08\u4ea4\u63db\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u5b9f\u969b\u306b\u306f\u5143\u306e\u5148\u982d\u8981\u7d20\u304c\u524a\u9664\u3055\u308c\u308b\uff09\u3002
        3. \u6839\u30ce\u30fc\u30c9\u304b\u3089\u958b\u59cb\u3057\u3066\u3001\u4e0a\u304b\u3089\u4e0b\u306b\u30d2\u30fc\u30d7\u5316\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u300c\u4e0a\u304b\u3089\u4e0b\u306e\u30d2\u30fc\u30d7\u5316\u300d\u306e\u65b9\u5411\u306f\u300c\u4e0b\u304b\u3089\u4e0a\u306e\u30d2\u30fc\u30d7\u5316\u300d\u3068\u53cd\u5bfe\u3067\u3059\u3002\u6839\u30ce\u30fc\u30c9\u306e\u5024\u3092\u305d\u306e2\u3064\u306e\u5b50\u3068\u6bd4\u8f03\u3057\u3001\u6700\u5927\u306e\u5b50\u3068\u4ea4\u63db\u3057\u307e\u3059\u3002\u6b21\u306b\u3001\u8449\u30ce\u30fc\u30c9\u306b\u5230\u9054\u3059\u308b\u304b\u3001\u4ea4\u63db\u304c\u4e0d\u8981\u306a\u30ce\u30fc\u30c9\u306b\u906d\u9047\u3059\u308b\u307e\u3067\u3001\u3053\u306e\u64cd\u4f5c\u3092\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002

        <1><2><3><4><5><6><7><8><9><10>

        \u56f3 8-4 \u00a0 \u30d2\u30fc\u30d7\u304b\u3089\u306e\u5148\u982d\u8981\u7d20\u524a\u9664\u306e\u624b\u9806

        \u8981\u7d20\u633f\u5165\u64cd\u4f5c\u3068\u540c\u69d8\u306b\u3001\u5148\u982d\u8981\u7d20\u524a\u9664\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3082\\(O(\\log n)\\)\u3067\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
        def pop(self) -> int:\n    \"\"\"\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u304b\u3089\u51fa\u3059\"\"\"\n    # \u7a7a\u306e\u51e6\u7406\n    if self.is_empty():\n        raise IndexError(\"Heap is empty\")\n    # \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3068\u6700\u53f3\u7aef\u306e\u8449\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\uff08\u6700\u521d\u306e\u8981\u7d20\u3068\u6700\u5f8c\u306e\u8981\u7d20\u3092\u4ea4\u63db\uff09\n    self.swap(0, self.size() - 1)\n    # \u30ce\u30fc\u30c9\u3092\u524a\u9664\n    val = self.max_heap.pop()\n    # \u4e0a\u304b\u3089\u4e0b\u3078\u30d2\u30fc\u30d7\u5316\n    self.sift_down(0)\n    # \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u8fd4\u3059\n    return val\n\ndef sift_down(self, i: int):\n    \"\"\"\u30ce\u30fc\u30c9i\u304b\u3089\u958b\u59cb\u3057\u3066\u3001\u4e0a\u304b\u3089\u4e0b\u3078\u30d2\u30fc\u30d7\u5316\"\"\"\n    while True:\n        # i\u3001l\u3001r\u306e\u4e2d\u3067\u6700\u5927\u306e\u30ce\u30fc\u30c9\u3092\u6c7a\u5b9a\u3057\u3001ma\u3068\u3059\u308b\n        l, r, ma = self.left(i), self.right(i), i\n        if l < self.size() and self.max_heap[l] > self.max_heap[ma]:\n            ma = l\n        if r < self.size() and self.max_heap[r] > self.max_heap[ma]:\n            ma = r\n        # \u30ce\u30fc\u30c9i\u304c\u6700\u5927\u307e\u305f\u306f\u30a4\u30f3\u30c7\u30c3\u30af\u30b9l\u3001r\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001\u3055\u3089\u306a\u308b\u30d2\u30fc\u30d7\u5316\u306f\u4e0d\u8981\u3001\u30d6\u30ec\u30fc\u30af\n        if ma == i:\n            break\n        # 2\u3064\u306e\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\n        self.swap(i, ma)\n        # \u4e0b\u5411\u304d\u306e\u30eb\u30fc\u30d7\u30d2\u30fc\u30d7\u5316\n        i = ma\n
        my_heap.cpp
        /* \u8981\u7d20\u304c\u30d2\u30fc\u30d7\u304b\u3089\u9000\u51fa */\nvoid pop() {\n    // \u7a7a\u306e\u51e6\u7406\n    if (isEmpty()) {\n        throw out_of_range(\"Heap is empty\");\n    }\n    // \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u6700\u3082\u53f3\u306e\u8449\u30ce\u30fc\u30c9\u3068\u4ea4\u63db\uff08\u6700\u521d\u306e\u8981\u7d20\u3068\u6700\u5f8c\u306e\u8981\u7d20\u3092\u4ea4\u63db\uff09\n    swap(maxHeap[0], maxHeap[size() - 1]);\n    // \u30ce\u30fc\u30c9\u3092\u524a\u9664\n    maxHeap.pop_back();\n    // \u4e0a\u304b\u3089\u4e0b\u3078\u30d2\u30fc\u30d7\u5316\n    siftDown(0);\n}\n\n/* \u30ce\u30fc\u30c9i\u304b\u3089\u4e0b\u5411\u304d\u306b\u30d2\u30fc\u30d7\u5316\u3092\u958b\u59cb */\nvoid siftDown(int i) {\n    while (true) {\n        // i\u3001l\u3001r\u306e\u4e2d\u3067\u6700\u5927\u306e\u30ce\u30fc\u30c9\u3092\u6c7a\u5b9a\u3057\u3001ma\u3068\u3057\u3066\u8a18\u9332\n        int l = left(i), r = right(i), ma = i;\n        if (l < size() && maxHeap[l] > maxHeap[ma])\n            ma = l;\n        if (r < size() && maxHeap[r] > maxHeap[ma])\n            ma = r;\n        // \u30ce\u30fc\u30c9i\u304c\u6700\u5927\u3001\u307e\u305f\u306f\u30a4\u30f3\u30c7\u30c3\u30af\u30b9l\u3001r\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001\u3053\u308c\u4ee5\u4e0a\u306e\u30d2\u30fc\u30d7\u5316\u306f\u4e0d\u8981\u3001\u30d6\u30ec\u30fc\u30af\n        if (ma == i)\n            break;\n        swap(maxHeap[i], maxHeap[ma]);\n        // \u4e0b\u5411\u304d\u306b\u30eb\u30fc\u30d7\u3057\u3066\u30d2\u30fc\u30d7\u5316\n        i = ma;\n    }\n}\n
        my_heap.java
        /* \u8981\u7d20\u304c\u30d2\u30fc\u30d7\u304b\u3089\u9000\u51fa */\nint pop() {\n    // \u7a7a\u306e\u51e6\u7406\n    if (isEmpty())\n        throw new IndexOutOfBoundsException();\n    // \u6839\u30ce\u30fc\u30c9\u3092\u6700\u3082\u53f3\u306e\u8449\u30ce\u30fc\u30c9\u3068\u4ea4\u63db\uff08\u6700\u521d\u306e\u8981\u7d20\u3092\u6700\u5f8c\u306e\u8981\u7d20\u3068\u4ea4\u63db\uff09\n    swap(0, size() - 1);\n    // \u30ce\u30fc\u30c9\u3092\u524a\u9664\n    int val = maxHeap.remove(size() - 1);\n    // \u4e0a\u304b\u3089\u4e0b\u3078\u30d2\u30fc\u30d7\u5316\n    siftDown(0);\n    // \u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u8fd4\u3059\n    return val;\n}\n\n/* \u30ce\u30fc\u30c9 i \u304b\u3089\u4e0b\u5411\u304d\u306b\u30d2\u30fc\u30d7\u5316\u3092\u958b\u59cb */\nvoid siftDown(int i) {\n    while (true) {\n        // i\u3001l\u3001r \u306e\u4e2d\u3067\u6700\u5927\u306e\u30ce\u30fc\u30c9\u3092\u6c7a\u5b9a\u3057\u3001ma \u3068\u3059\u308b\n        int l = left(i), r = right(i), ma = i;\n        if (l < size() && maxHeap.get(l) > maxHeap.get(ma))\n            ma = l;\n        if (r < size() && maxHeap.get(r) > maxHeap.get(ma))\n            ma = r;\n        // \u30ce\u30fc\u30c9 i \u304c\u6700\u5927\u306e\u5834\u5408\u3001\u307e\u305f\u306f\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 l\u3001r \u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001\u3055\u3089\u306a\u308b\u30d2\u30fc\u30d7\u5316\u306f\u4e0d\u8981\u3001\u7d42\u4e86\n        if (ma == i)\n            break;\n        // 2\u3064\u306e\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\n        swap(i, ma);\n        // \u4e0b\u5411\u304d\u306b\u30d2\u30fc\u30d7\u5316\u3092\u30eb\u30fc\u30d7\n        i = ma;\n    }\n}\n
        my_heap.cs
        [class]{MaxHeap}-[func]{Pop}\n\n[class]{MaxHeap}-[func]{SiftDown}\n
        my_heap.go
        [class]{maxHeap}-[func]{pop}\n\n[class]{maxHeap}-[func]{siftDown}\n
        my_heap.swift
        [class]{MaxHeap}-[func]{pop}\n\n[class]{MaxHeap}-[func]{siftDown}\n
        my_heap.js
        [class]{MaxHeap}-[func]{pop}\n\n[class]{MaxHeap}-[func]{siftDown}\n
        my_heap.ts
        [class]{MaxHeap}-[func]{pop}\n\n[class]{MaxHeap}-[func]{siftDown}\n
        my_heap.dart
        [class]{MaxHeap}-[func]{pop}\n\n[class]{MaxHeap}-[func]{siftDown}\n
        my_heap.rs
        [class]{MaxHeap}-[func]{pop}\n\n[class]{MaxHeap}-[func]{sift_down}\n
        my_heap.c
        [class]{MaxHeap}-[func]{pop}\n\n[class]{MaxHeap}-[func]{siftDown}\n
        my_heap.kt
        [class]{MaxHeap}-[func]{pop}\n\n[class]{MaxHeap}-[func]{siftDown}\n
        my_heap.rb
        [class]{MaxHeap}-[func]{pop}\n\n[class]{MaxHeap}-[func]{sift_down}\n
        "},{"location":"chapter_heap/heap/#813","title":"8.1.3 \u00a0 \u30d2\u30fc\u30d7\u306e\u4e00\u822c\u7684\u306a\u5fdc\u7528","text":"
        • \u512a\u5148\u5ea6\u30ad\u30e5\u30fc\uff1a\u30d2\u30fc\u30d7\u306f\u512a\u5148\u5ea6\u30ad\u30e5\u30fc\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306e\u597d\u307e\u3057\u3044\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3001\u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c\u3068\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\u306e\u4e21\u65b9\u306e\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(\\log n)\\)\u3001\u30ad\u30e5\u30fc\u69cb\u7bc9\u306e\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(n)\\)\u3067\u3001\u3059\u3079\u3066\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3059\u3002
        • \u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\uff1a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u305d\u308c\u3089\u304b\u3089\u30d2\u30fc\u30d7\u3092\u4f5c\u6210\u3057\u3001\u6b21\u306b\u8981\u7d20\u524a\u9664\u64cd\u4f5c\u3092\u7d99\u7d9a\u7684\u306b\u5b9f\u884c\u3057\u3066\u9806\u5e8f\u4ed8\u3051\u3055\u308c\u305f\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\u3092\u5b9f\u88c5\u3059\u308b\u3088\u308a\u6d17\u7df4\u3055\u308c\u305f\u65b9\u6cd5\u304c\u3042\u308a\u3001\u300c\u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\u300d\u306e\u7ae0\u3067\u8aac\u660e\u3055\u308c\u3066\u3044\u307e\u3059\u3002
        • \u6700\u5927\\(k\\)\u500b\u306e\u8981\u7d20\u306e\u767a\u898b\uff1a\u3053\u308c\u306f\u53e4\u5178\u7684\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u3067\u3042\u308a\u3001\u4e00\u822c\u7684\u306a\u4f7f\u7528\u4f8b\u3067\u3082\u3042\u308a\u307e\u3059\u3002Weibo\u30db\u30c3\u30c8\u691c\u7d22\u306e\u30c8\u30c3\u30d710\u30db\u30c3\u30c8\u30cb\u30e5\u30fc\u30b9\u306e\u9078\u629e\u3084\u3001\u30c8\u30c3\u30d710\u306e\u58f2\u308c\u7b4b\u5546\u54c1\u306e\u9078\u629e\u306a\u3069\u3067\u3059\u3002
        "},{"location":"chapter_heap/summary/","title":"8.4 \u00a0 \u307e\u3068\u3081","text":""},{"location":"chapter_heap/summary/#1","title":"1. \u00a0 \u91cd\u8981\u306a\u5fa9\u7fd2","text":"
        • \u30d2\u30fc\u30d7\u306f\u5b8c\u5099\u4e8c\u5206\u6728\u3067\u3001\u305d\u306e\u69cb\u7bc9\u6027\u8cea\u306b\u57fa\u3065\u3044\u3066\u6700\u5927\u30d2\u30fc\u30d7\u307e\u305f\u306f\u6700\u5c0f\u30d2\u30fc\u30d7\u306b\u5206\u985e\u3067\u304d\u307e\u3059\u3002\u6700\u5927\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u306f\u6700\u5927\u3067\u3001\u6700\u5c0f\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u306f\u6700\u5c0f\u3067\u3059\u3002
        • \u512a\u5148\u5ea6\u30ad\u30e5\u30fc\u306f\u3001\u30c7\u30ad\u30e5\u30fc\u306e\u512a\u5148\u5ea6\u3092\u6301\u3064\u30ad\u30e5\u30fc\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u3001\u901a\u5e38\u30d2\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002
        • \u30d2\u30fc\u30d7\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c\u3068\u305d\u308c\u306b\u5bfe\u5fdc\u3059\u308b\u6642\u9593\u8a08\u7b97\u91cf\u306b\u306f\u4ee5\u4e0b\u304c\u3042\u308a\u307e\u3059\uff1a\u30d2\u30fc\u30d7\u3078\u306e\u8981\u7d20\u633f\u5165\\(O(\\log n)\\)\u3001\u30d2\u30fc\u30d7\u304b\u3089\u306e\u5148\u982d\u8981\u7d20\u524a\u9664\\(O(\\log n)\\)\u3001\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3078\u306e\u30a2\u30af\u30bb\u30b9\\(O(1)\\)\u3002
        • \u5b8c\u5099\u4e8c\u5206\u6728\u306f\u914d\u5217\u3067\u8868\u73fe\u3059\u308b\u306e\u306b\u9069\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u30d2\u30fc\u30d7\u306f\u4e00\u822c\u7684\u306b\u914d\u5217\u3092\u4f7f\u7528\u3057\u3066\u683c\u7d0d\u3055\u308c\u307e\u3059\u3002
        • \u30d2\u30fc\u30d7\u5316\u64cd\u4f5c\u306f\u30d2\u30fc\u30d7\u306e\u6027\u8cea\u3092\u7dad\u6301\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u3001\u30d2\u30fc\u30d7\u306e\u633f\u5165\u64cd\u4f5c\u3068\u524a\u9664\u64cd\u4f5c\u306e\u4e21\u65b9\u3067\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        • \\(n\\)\u500b\u306e\u8981\u7d20\u304c\u5165\u529b\u3068\u3057\u3066\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u306e\u30d2\u30fc\u30d7\u69cb\u7bc9\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u306b\u6700\u9069\u5316\u3067\u304d\u3001\u3053\u308c\u306f\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3059\u3002
        • Top-k\u306f\u53e4\u5178\u7684\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u3067\u3001\u30d2\u30fc\u30d7\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u4f7f\u7528\u3057\u3066\u52b9\u7387\u7684\u306b\u89e3\u6c7a\u3067\u304d\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n \\log k)\\)\u3067\u3059\u3002
        "},{"location":"chapter_heap/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q: \u30c7\u30fc\u30bf\u69cb\u9020\u306e\u300c\u30d2\u30fc\u30d7\u300d\u3068\u30e1\u30e2\u30ea\u7ba1\u7406\u306e\u300c\u30d2\u30fc\u30d7\u300d\u306f\u540c\u3058\u6982\u5ff5\u3067\u3059\u304b\uff1f

        \u3053\u306e2\u3064\u306f\u3001\u3069\u3061\u3089\u3082\u300c\u30d2\u30fc\u30d7\u300d\u3068\u547c\u3070\u308c\u307e\u3059\u304c\u3001\u540c\u3058\u6982\u5ff5\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30b7\u30b9\u30c6\u30e0\u30e1\u30e2\u30ea\u306e\u30d2\u30fc\u30d7\u306f\u52d5\u7684\u30e1\u30e2\u30ea\u5272\u308a\u5f53\u3066\u306e\u4e00\u90e8\u3067\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u5b9f\u884c\u4e2d\u306b\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3084\u914d\u5217\u306a\u3069\u306e\u8907\u96d1\u306a\u69cb\u9020\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u3001\u4e00\u5b9a\u91cf\u306e\u30d2\u30fc\u30d7\u30e1\u30e2\u30ea\u3092\u8981\u6c42\u3067\u304d\u307e\u3059\u3002\u5272\u308a\u5f53\u3066\u3089\u308c\u305f\u30c7\u30fc\u30bf\u304c\u4e0d\u8981\u306b\u306a\u3063\u305f\u3068\u304d\u306f\u3001\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u3092\u9632\u3050\u305f\u3081\u306b\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u3053\u306e\u30e1\u30e2\u30ea\u3092\u89e3\u653e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u30b9\u30bf\u30c3\u30af\u30e1\u30e2\u30ea\u3068\u6bd4\u8f03\u3057\u3066\u3001\u30d2\u30fc\u30d7\u30e1\u30e2\u30ea\u306e\u7ba1\u7406\u3068\u4f7f\u7528\u306b\u306f\u3088\u308a\u591a\u304f\u306e\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3001\u4e0d\u9069\u5207\u306a\u4f7f\u7528\u306f\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u3084\u30c0\u30f3\u30b0\u30ea\u30f3\u30b0\u30dd\u30a4\u30f3\u30bf\u306b\u3064\u306a\u304c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_heap/top_k/","title":"8.3 \u00a0 Top-k\u554f\u984c","text":"

        Question

        \u9577\u3055\\(n\\)\u306e\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u3066\u3044\u306a\u3044\u914d\u5217nums\u304c\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001\u914d\u5217\u5185\u306e\u6700\u5927\\(k\\)\u500b\u306e\u8981\u7d20\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u3053\u306e\u554f\u984c\u306b\u3064\u3044\u3066\u3001\u307e\u305a2\u3064\u306e\u76f4\u63a5\u7684\u306a\u89e3\u6cd5\u3092\u7d39\u4ecb\u3057\u3001\u6b21\u306b\u3088\u308a\u52b9\u7387\u7684\u306a\u30d2\u30fc\u30d7\u30d9\u30fc\u30b9\u306e\u65b9\u6cd5\u3092\u8aac\u660e\u3057\u307e\u3059\u3002

        "},{"location":"chapter_heap/top_k/#831-1","title":"8.3.1 \u00a0 \u65b9\u6cd51\uff1a\u53cd\u5fa9\u9078\u629e","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\\(k\\)\u56de\u306e\u53cd\u5fa9\u3092\u5b9f\u884c\u3057\u3001\u5404\u56de\u3067\\(1\\)\u756a\u76ee\u3001\\(2\\)\u756a\u76ee\u3001\\(\\dots\\)\u3001\\(k\\)\u756a\u76ee\u306b\u5927\u304d\u3044\u8981\u7d20\u3092\u62bd\u51fa\u3067\u304d\u307e\u3059\u3002\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(nk)\\)\u3067\u3059\u3002

        \u3053\u306e\u65b9\u6cd5\u306f\\(k \\ll n\\)\u306e\u5834\u5408\u306b\u306e\u307f\u9069\u3057\u3066\u3044\u307e\u3059\u3002\\(k\\)\u304c\\(n\\)\u306b\u8fd1\u3044\u5834\u5408\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n^2)\\)\u306b\u8fd1\u3065\u304d\u3001\u975e\u5e38\u306b\u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002

        \u56f3 8-6 \u00a0 \u6700\u5927k\u500b\u306e\u8981\u7d20\u3092\u53cd\u5fa9\u7684\u306b\u898b\u3064\u3051\u308b

        Tip

        \\(k = n\\)\u306e\u5834\u5408\u3001\u5b8c\u5168\u306b\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u3053\u308c\u306f\u300c\u9078\u629e\u30bd\u30fc\u30c8\u300d\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u540c\u7b49\u3067\u3059\u3002

        "},{"location":"chapter_heap/top_k/#832-2","title":"8.3.2 \u00a0 \u65b9\u6cd52\uff1a\u30bd\u30fc\u30c8","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u307e\u305a\u914d\u5217nums\u3092\u30bd\u30fc\u30c8\u3057\u3001\u6b21\u306b\u6700\u5f8c\u306e\\(k\\)\u500b\u306e\u8981\u7d20\u3092\u8fd4\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n \\log n)\\)\u3067\u3059\u3002

        \u660e\u3089\u304b\u306b\u3001\u3053\u306e\u65b9\u6cd5\u306f\u30bf\u30b9\u30af\u3092\u300c\u3084\u308a\u3059\u304e\u300d\u3066\u3044\u307e\u3059\u3002\u6700\u5927\\(k\\)\u500b\u306e\u8981\u7d20\u3092\u898b\u3064\u3051\u308b\u3060\u3051\u3067\u3088\u304f\u3001\u4ed6\u306e\u8981\u7d20\u3092\u30bd\u30fc\u30c8\u3059\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\u3002

        \u56f3 8-7 \u00a0 \u30bd\u30fc\u30c8\u306b\u3088\u308b\u6700\u5927k\u500b\u306e\u8981\u7d20\u306e\u767a\u898b

        "},{"location":"chapter_heap/top_k/#833-3","title":"8.3.3 \u00a0 \u65b9\u6cd53\uff1a\u30d2\u30fc\u30d7","text":"

        \u4ee5\u4e0b\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30d2\u30fc\u30d7\u306b\u57fa\u3065\u3044\u3066Top-k\u554f\u984c\u3092\u3088\u308a\u52b9\u7387\u7684\u306b\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002

        1. \u6700\u5c0f\u30d2\u30fc\u30d7\u3092\u521d\u671f\u5316\u3057\u307e\u3059\u3002\u5148\u982d\u8981\u7d20\u304c\u6700\u5c0f\u306b\u306a\u308a\u307e\u3059\u3002
        2. \u307e\u305a\u3001\u914d\u5217\u306e\u6700\u521d\u306e\\(k\\)\u500b\u306e\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u633f\u5165\u3057\u307e\u3059\u3002
        3. \\(k + 1\\)\u756a\u76ee\u306e\u8981\u7d20\u304b\u3089\u958b\u59cb\u3057\u3001\u73fe\u5728\u306e\u8981\u7d20\u304c\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3088\u308a\u5927\u304d\u3044\u5834\u5408\u3001\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u524a\u9664\u3057\u3001\u73fe\u5728\u306e\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u633f\u5165\u3057\u307e\u3059\u3002
        4. \u8d70\u67fb\u3092\u5b8c\u4e86\u3057\u305f\u5f8c\u3001\u30d2\u30fc\u30d7\u306b\u306f\u6700\u5927\\(k\\)\u500b\u306e\u8981\u7d20\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002
        <1><2><3><4><5><6><7><8><9>

        \u56f3 8-8 \u00a0 \u30d2\u30fc\u30d7\u306b\u57fa\u3065\u304f\u6700\u5927k\u500b\u306e\u8981\u7d20\u306e\u767a\u898b

        \u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby top_k.py
        def top_k_heap(nums: list[int], k: int) -> list[int]:\n    \"\"\"\u30d2\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u5185\u306e\u6700\u5927k\u500b\u306e\u8981\u7d20\u3092\u898b\u3064\u3051\u308b\"\"\"\n    # \u6700\u5c0f\u30d2\u30fc\u30d7\u3092\u521d\u671f\u5316\n    heap = []\n    # \u914d\u5217\u306e\u6700\u521d\u306ek\u500b\u306e\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u5165\u529b\n    for i in range(k):\n        heapq.heappush(heap, nums[i])\n    # k+1\u756a\u76ee\u306e\u8981\u7d20\u304b\u3089\u3001\u30d2\u30fc\u30d7\u306e\u9577\u3055\u3092k\u306b\u4fdd\u3064\n    for i in range(k, len(nums)):\n        # \u73fe\u5728\u306e\u8981\u7d20\u304c\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3088\u308a\u5927\u304d\u3044\u5834\u5408\u3001\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u524a\u9664\u3057\u3001\u73fe\u5728\u306e\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u5165\u529b\n        if nums[i] > heap[0]:\n            heapq.heappop(heap)\n            heapq.heappush(heap, nums[i])\n    return heap\n
        top_k.cpp
        /* \u30d2\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u5185\u306e\u6700\u5927k\u500b\u306e\u8981\u7d20\u3092\u898b\u3064\u3051\u308b */\npriority_queue<int, vector<int>, greater<int>> topKHeap(vector<int> &nums, int k) {\n    // \u6700\u5c0f\u30d2\u30fc\u30d7\u3092\u521d\u671f\u5316\n    priority_queue<int, vector<int>, greater<int>> heap;\n    // \u914d\u5217\u306e\u6700\u521d\u306ek\u500b\u306e\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u5165\u529b\n    for (int i = 0; i < k; i++) {\n        heap.push(nums[i]);\n    }\n    // k+1\u756a\u76ee\u306e\u8981\u7d20\u304b\u3089\u3001\u30d2\u30fc\u30d7\u306e\u9577\u3055\u3092k\u306b\u4fdd\u3064\n    for (int i = k; i < nums.size(); i++) {\n        // \u73fe\u5728\u306e\u8981\u7d20\u304c\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3088\u308a\u5927\u304d\u3044\u5834\u5408\u3001\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u524a\u9664\u3057\u3001\u73fe\u5728\u306e\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u5165\u529b\n        if (nums[i] > heap.top()) {\n            heap.pop();\n            heap.push(nums[i]);\n        }\n    }\n    return heap;\n}\n
        top_k.java
        /* \u30d2\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u5185\u306e\u6700\u5927 k \u500b\u306e\u8981\u7d20\u3092\u691c\u7d22 */\nQueue<Integer> topKHeap(int[] nums, int k) {\n    // \u6700\u5c0f\u30d2\u30fc\u30d7\u3092\u521d\u671f\u5316\n    Queue<Integer> heap = new PriorityQueue<Integer>();\n    // \u914d\u5217\u306e\u6700\u521d\u306e k \u500b\u306e\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u5165\u529b\n    for (int i = 0; i < k; i++) {\n        heap.offer(nums[i]);\n    }\n    // k+1 \u756a\u76ee\u306e\u8981\u7d20\u304b\u3089\u3001\u30d2\u30fc\u30d7\u306e\u9577\u3055\u3092 k \u306b\u4fdd\u3064\n    for (int i = k; i < nums.length; i++) {\n        // \u73fe\u5728\u306e\u8981\u7d20\u304c\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3088\u308a\u5927\u304d\u3044\u5834\u5408\u3001\u30d2\u30fc\u30d7\u306e\u5148\u982d\u8981\u7d20\u3092\u524a\u9664\u3057\u3001\u73fe\u5728\u306e\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u306b\u5165\u529b\n        if (nums[i] > heap.peek()) {\n            heap.poll();\n            heap.offer(nums[i]);\n        }\n    }\n    return heap;\n}\n
        top_k.cs
        [class]{top_k}-[func]{TopKHeap}\n
        top_k.go
        [class]{}-[func]{topKHeap}\n
        top_k.swift
        [class]{}-[func]{topKHeap}\n
        top_k.js
        [class]{}-[func]{pushMinHeap}\n\n[class]{}-[func]{popMinHeap}\n\n[class]{}-[func]{peekMinHeap}\n\n[class]{}-[func]{getMinHeap}\n\n[class]{}-[func]{topKHeap}\n
        top_k.ts
        [class]{}-[func]{pushMinHeap}\n\n[class]{}-[func]{popMinHeap}\n\n[class]{}-[func]{peekMinHeap}\n\n[class]{}-[func]{getMinHeap}\n\n[class]{}-[func]{topKHeap}\n
        top_k.dart
        [class]{}-[func]{topKHeap}\n
        top_k.rs
        [class]{}-[func]{top_k_heap}\n
        top_k.c
        [class]{}-[func]{pushMinHeap}\n\n[class]{}-[func]{popMinHeap}\n\n[class]{}-[func]{peekMinHeap}\n\n[class]{}-[func]{getMinHeap}\n\n[class]{}-[func]{topKHeap}\n
        top_k.kt
        [class]{}-[func]{topKHeap}\n
        top_k.rb
        [class]{}-[func]{top_k_heap}\n

        \u5408\u8a08\\(n\\)\u56de\u306e\u30d2\u30fc\u30d7\u633f\u5165\u3068\u524a\u9664\u304c\u5b9f\u884c\u3055\u308c\u3001\u6700\u5927\u30d2\u30fc\u30d7\u30b5\u30a4\u30ba\u304c\\(k\\)\u3067\u3042\u308b\u305f\u3081\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n \\log k)\\)\u3067\u3059\u3002\u3053\u306e\u65b9\u6cd5\u306f\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3001\\(k\\)\u304c\u5c0f\u3055\u3044\u5834\u5408\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u306b\u8fd1\u3065\u304d\u3001\\(k\\)\u304c\u5927\u304d\u3044\u5834\u5408\u3067\u3082\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n \\log n)\\)\u3092\u8d85\u3048\u307e\u305b\u3093\u3002

        \u3055\u3089\u306b\u3001\u3053\u306e\u65b9\u6cd5\u306f\u52d5\u7684\u30c7\u30fc\u30bf\u30b9\u30c8\u30ea\u30fc\u30e0\u306e\u30b7\u30ca\u30ea\u30aa\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u30c7\u30fc\u30bf\u3092\u7d99\u7d9a\u7684\u306b\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3067\u3001\u30d2\u30fc\u30d7\u5185\u306e\u8981\u7d20\u3092\u7dad\u6301\u3057\u3001\u6700\u5927\\(k\\)\u500b\u306e\u8981\u7d20\u306e\u52d5\u7684\u66f4\u65b0\u3092\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_hello_algo/","title":"\u306f\u3058\u3081\u306b","text":"

        \u6570\u5e74\u524d\u3001\u79c1\u306fLeetCode\u3067\u300c\u5263\u6307Offer\u300d\u306e\u554f\u984c\u89e3\u7b54\u3092\u5171\u6709\u3057\u3001\u591a\u304f\u306e\u8aad\u8005\u304b\u3089\u52b1\u307e\u3057\u3068\u30b5\u30dd\u30fc\u30c8\u3092\u53d7\u3051\u307e\u3057\u305f\u3002\u8aad\u8005\u3068\u306e\u3084\u308a\u53d6\u308a\u306e\u4e2d\u3067\u3001\u6700\u3082\u3088\u304f\u805e\u304b\u308c\u305f\u8cea\u554f\u306f\u300c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52c9\u5f37\u3092\u3069\u3046\u59cb\u3081\u305f\u3089\u3088\u3044\u304b\u300d\u3067\u3057\u305f\u3002\u6b21\u7b2c\u306b\u3001\u79c1\u306f\u3053\u306e\u8cea\u554f\u306b\u5f37\u3044\u95a2\u5fc3\u3092\u62b1\u304f\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002

        \u554f\u984c\u3092\u76f4\u63a5\u89e3\u304f\u3053\u3068\u304c\u6700\u3082\u4eba\u6c17\u306e\u3042\u308b\u65b9\u6cd5\u306e\u3088\u3046\u3067\u3059\u3002\u3053\u308c\u306f\u30b7\u30f3\u30d7\u30eb\u3067\u76f4\u63a5\u7684\u3067\u52b9\u679c\u7684\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u554f\u984c\u89e3\u6c7a\u306f\u30de\u30a4\u30f3\u30b9\u30a4\u30fc\u30d1\u30fc\u3092\u30d7\u30ec\u30a4\u3059\u308b\u3088\u3046\u306a\u3082\u306e\u3067\u3059\u3002\u81ea\u5b66\u81ea\u7fd2\u306e\u80fd\u529b\u304c\u9ad8\u3044\u4eba\u306f\u3001\u5730\u96f7\u3092\u4e00\u3064\u305a\u3064\u56de\u907f\u3057\u3066\u3044\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u304c\u3001\u3057\u3063\u304b\u308a\u3068\u3057\u305f\u57fa\u790e\u304c\u306a\u3044\u4eba\u306f\u3001\u4f55\u5ea6\u3082\u3064\u307e\u305a\u3044\u3066\u632b\u6298\u3057\u306a\u304c\u3089\u5f8c\u9000\u3059\u308b\u3053\u3068\u306b\u306a\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u6559\u79d1\u66f8\u3092\u8aad\u3080\u3053\u3068\u3082\u4e00\u822c\u7684\u306a\u65b9\u6cd5\u3067\u3059\u304c\u3001\u5c31\u8077\u6d3b\u52d5\u4e2d\u306e\u4eba\u306b\u3068\u3063\u3066\u3001\u5352\u696d\u8ad6\u6587\u306e\u57f7\u7b46\u3001\u5c65\u6b74\u66f8\u306e\u63d0\u51fa\u3001\u7b46\u8a18\u8a66\u9a13\u3084\u9762\u63a5\u306e\u6e96\u5099\u304c\u65e2\u306b\u30a8\u30cd\u30eb\u30ae\u30fc\u306e\u5927\u90e8\u5206\u3092\u6d88\u8cbb\u3057\u3066\u304a\u308a\u3001\u5206\u539a\u3044\u672c\u3092\u8aad\u3080\u3053\u3068\u306f\u3057\u3070\u3057\u3070\u56f0\u96e3\u306a\u6311\u6226\u3068\u306a\u308a\u307e\u3059\u3002

        \u3082\u3057\u3042\u306a\u305f\u304c\u540c\u69d8\u306e\u60a9\u307f\u3092\u62b1\u3048\u3066\u3044\u308b\u306a\u3089\u3001\u3053\u306e\u672c\u304c\u3042\u306a\u305f\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u3066\u5e78\u904b\u3067\u3059\u3002\u3053\u306e\u672c\u306f\u3001\u3053\u306e\u8cea\u554f\u306b\u5bfe\u3059\u308b\u79c1\u306e\u7b54\u3048\u3067\u3059\u3002\u3053\u308c\u304c\u6700\u826f\u306e\u89e3\u6c7a\u7b56\u3067\u306f\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u5c11\u306a\u304f\u3068\u3082\u7a4d\u6975\u7684\u306a\u8a66\u307f\u3067\u3059\u3002\u3053\u306e\u672c\u304c\u3042\u306a\u305f\u306b\u76f4\u63a5\u5185\u5b9a\u3092\u3082\u305f\u3089\u3059\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u304c\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u300c\u77e5\u8b58\u5730\u56f3\u300d\u3092\u63a2\u7d22\u3059\u308b\u624b\u5f15\u304d\u3068\u306a\u308a\u3001\u3055\u307e\u3056\u307e\u306a\u300c\u5730\u96f7\u300d\u306e\u5f62\u3001\u5927\u304d\u3055\u3001\u4f4d\u7f6e\u3092\u7406\u89e3\u3057\u3001\u3055\u307e\u3056\u307e\u306a\u300c\u5730\u96f7\u9664\u53bb\u65b9\u6cd5\u300d\u3092\u30de\u30b9\u30bf\u30fc\u3067\u304d\u308b\u3088\u3046\u304a\u624b\u4f1d\u3044\u3057\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u30b9\u30ad\u30eb\u304c\u3042\u308c\u3070\u3001\u3088\u308a\u5feb\u9069\u306b\u554f\u984c\u3092\u89e3\u304d\u3001\u6587\u732e\u3092\u8aad\u3080\u3053\u3068\u304c\u3067\u304d\u3001\u5f90\u3005\u306b\u77e5\u8b58\u4f53\u7cfb\u3092\u69cb\u7bc9\u3067\u304d\u308b\u3068\u4fe1\u3058\u3066\u3044\u307e\u3059\u3002

        \u79c1\u306f\u3001\u30d5\u30a1\u30a4\u30f3\u30de\u30f3\u6559\u6388\u306e\u8a00\u8449\u306b\u6df1\u304f\u540c\u611f\u3057\u307e\u3059\u3002\u300c\u77e5\u8b58\u306f\u7121\u6599\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u6ce8\u610f\u3092\u6255\u308f\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u306e\u3067\u3059\u3002\u300d\u3053\u306e\u610f\u5473\u3067\u3001\u3053\u306e\u672c\u306f\u5b8c\u5168\u306b\u300c\u7121\u6599\u300d\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u3053\u306e\u672c\u306b\u5bfe\u3059\u308b\u3042\u306a\u305f\u306e\u8cb4\u91cd\u306a\u300c\u6ce8\u610f\u300d\u306b\u5fdc\u3048\u308b\u305f\u3081\u306b\u3001\u79c1\u306f\u6700\u5584\u3092\u5c3d\u304f\u3057\u3001\u6700\u5927\u306e\u300c\u6ce8\u610f\u300d\u3092\u6255\u3063\u3066\u3053\u306e\u672c\u3092\u66f8\u304d\u307e\u3059\u3002

        \u81ea\u5206\u306e\u9650\u754c\u3092\u8a8d\u8b58\u3057\u3066\u304a\u308a\u3001\u3053\u306e\u672c\u306e\u5185\u5bb9\u304c\u6642\u9593\u3092\u304b\u3051\u3066\u6d17\u7df4\u3055\u308c\u305f\u306b\u3082\u304b\u304b\u308f\u3089\u305a\u3001\u9593\u9055\u3044\u306f\u78ba\u5b9f\u306b\u6b8b\u3063\u3066\u3044\u308b\u3053\u3068\u3092\u7406\u89e3\u3057\u3066\u3044\u307e\u3059\u3002\u5148\u751f\u65b9\u3084\u5b66\u751f\u306e\u7686\u69d8\u304b\u3089\u306e\u6279\u8a55\u3068\u8a02\u6b63\u3092\u5fc3\u304b\u3089\u6b53\u8fce\u3044\u305f\u3057\u307e\u3059\u3002

        Hello, Algo!

        \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u51fa\u73fe\u306f\u4e16\u754c\u306b\u5927\u304d\u306a\u5909\u5316\u3092\u3082\u305f\u3089\u3057\u307e\u3057\u305f\u3002\u9ad8\u901f\u306a\u8a08\u7b97\u80fd\u529b\u3068\u512a\u308c\u305f\u30d7\u30ed\u30b0\u30e9\u30e0\u53ef\u80fd\u6027\u306b\u3088\u308a\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u5b9f\u884c\u3057\u30c7\u30fc\u30bf\u3092\u51e6\u7406\u3059\u308b\u305f\u3081\u306e\u7406\u60f3\u7684\u306a\u5a92\u4f53\u3068\u306a\u308a\u307e\u3057\u305f\u3002\u30d3\u30c7\u30aa\u30b2\u30fc\u30e0\u306e\u30ea\u30a2\u30eb\u306a\u30b0\u30e9\u30d5\u30a3\u30c3\u30af\u30b9\u3001\u81ea\u52d5\u904b\u8ee2\u306e\u77e5\u7684\u306a\u5224\u65ad\u3001AlphaGo\u306e\u898b\u4e8b\u306a\u56f2\u7881\u30b2\u30fc\u30e0\u3001ChatGPT\u306e\u81ea\u7136\u306a\u5bfe\u8a71\u306a\u3069\u3001\u3053\u308c\u3089\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306f\u3059\u3079\u3066\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u4e0a\u3067\u52d5\u4f5c\u3059\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7cbe\u5de7\u306a\u5b9f\u6f14\u3067\u3059\u3002

        \u5b9f\u969b\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u51fa\u73fe\u4ee5\u524d\u304b\u3089\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u4e16\u754c\u306e\u81f3\u308b\u6240\u306b\u5b58\u5728\u3057\u3066\u3044\u307e\u3057\u305f\u3002\u521d\u671f\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u6bd4\u8f03\u7684\u30b7\u30f3\u30d7\u30eb\u3067\u3001\u53e4\u4ee3\u306e\u8a08\u6570\u65b9\u6cd5\u3084\u9053\u5177\u4f5c\u308a\u306e\u624b\u9806\u306a\u3069\u304c\u3042\u308a\u307e\u3057\u305f\u3002\u6587\u660e\u304c\u9032\u6b69\u3059\u308b\u306b\u3064\u308c\u3066\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3088\u308a\u6d17\u7df4\u3055\u308c\u8907\u96d1\u306b\u306a\u308a\u307e\u3057\u305f\u3002\u8077\u4eba\u306e\u7cbe\u5de7\u306a\u6280\u8853\u304b\u3089\u3001\u751f\u7523\u529b\u3092\u89e3\u653e\u3059\u308b\u5de5\u696d\u88fd\u54c1\u3001\u5b87\u5b99\u3092\u652f\u914d\u3059\u308b\u79d1\u5b66\u6cd5\u5247\u307e\u3067\u3001\u307b\u307c\u3059\u3079\u3066\u306e\u5e73\u51e1\u307e\u305f\u306f\u9a5a\u7570\u7684\u306a\u3053\u3068\u306e\u80cc\u5f8c\u306b\u306f\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5de7\u5999\u306a\u601d\u8003\u304c\u3042\u308a\u307e\u3059\u3002

        \u540c\u69d8\u306b\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u81f3\u308b\u6240\u306b\u3042\u308a\u307e\u3059\u3002\u30bd\u30fc\u30b7\u30e3\u30eb\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u304b\u3089\u5730\u4e0b\u9244\u8def\u7dda\u307e\u3067\u3001\u591a\u304f\u306e\u30b7\u30b9\u30c6\u30e0\u306f\u300c\u30b0\u30e9\u30d5\u300d\u3068\u3057\u3066\u30e2\u30c7\u30eb\u5316\u3067\u304d\u307e\u3059\u3002\u56fd\u304b\u3089\u5bb6\u65cf\u307e\u3067\u3001\u793e\u4f1a\u7d44\u7e54\u306e\u4e3b\u8981\u306a\u5f62\u614b\u306f\u300c\u6728\u300d\u306e\u7279\u5fb4\u3092\u793a\u3057\u307e\u3059\u3002\u51ac\u670d\u306f\u300c\u30b9\u30bf\u30c3\u30af\u300d\u306e\u3088\u3046\u3067\u3001\u6700\u521d\u306b\u7740\u305f\u3082\u306e\u304c\u6700\u5f8c\u306b\u8131\u304c\u308c\u307e\u3059\u3002\u30d0\u30c9\u30df\u30f3\u30c8\u30f3\u306e\u30b7\u30e3\u30c8\u30eb\u7b52\u306f\u300c\u30ad\u30e5\u30fc\u300d\u306b\u4f3c\u3066\u304a\u308a\u3001\u4e00\u65b9\u306e\u7aef\u3067\u633f\u5165\u3057\u3001\u3082\u3046\u4e00\u65b9\u306e\u7aef\u3067\u53d6\u308a\u51fa\u3057\u307e\u3059\u3002\u8f9e\u66f8\u306f\u300c\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u300d\u306e\u3088\u3046\u3067\u3001\u76ee\u6a19\u30a8\u30f3\u30c8\u30ea\u3092\u7d20\u65e9\u304f\u691c\u7d22\u3067\u304d\u307e\u3059\u3002

        \u3053\u306e\u672c\u306f\u3001\u660e\u78ba\u3067\u7406\u89e3\u3057\u3084\u3059\u3044\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u56f3\u89e3\u3068\u5b9f\u884c\u53ef\u80fd\u306a\u30b3\u30fc\u30c9\u4f8b\u3092\u901a\u3058\u3066\u3001\u8aad\u8005\u304c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u6838\u5fc3\u6982\u5ff5\u3092\u7406\u89e3\u3057\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3092\u901a\u3058\u3066\u305d\u308c\u3089\u3092\u5b9f\u88c5\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u3053\u3068\u3092\u76ee\u6307\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u57fa\u76e4\u306e\u4e0a\u3067\u3001\u3053\u306e\u672c\u306f\u8907\u96d1\u306a\u4e16\u754c\u306b\u304a\u3051\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u751f\u304d\u751f\u304d\u3068\u3057\u305f\u73fe\u308c\u3092\u660e\u3089\u304b\u306b\u3057\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7f8e\u3057\u3055\u3092\u793a\u3059\u3053\u3068\u306b\u52aa\u3081\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u672c\u304c\u3042\u306a\u305f\u306e\u304a\u5f79\u306b\u7acb\u3066\u308b\u3053\u3068\u3092\u9858\u3063\u3066\u3044\u307e\u3059\uff01

        "},{"location":"chapter_introduction/","title":"\u7b2c 1 \u7ae0 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u306e\u51fa\u4f1a\u3044","text":"

        Abstract

        \u512a\u96c5\u306a\u4e59\u5973\u304c\u8e0a\u308a\u307e\u3059\u3002\u30c7\u30fc\u30bf\u3068\u7d61\u307f\u5408\u3044\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u30e1\u30ed\u30c7\u30a3\u30fc\u306b\u5408\u308f\u305b\u3066\u30b9\u30ab\u30fc\u30c8\u3092\u306a\u3073\u304b\u305b\u306a\u304c\u3089\u3002

        \u5f7c\u5973\u304c\u3042\u306a\u305f\u3092\u30c0\u30f3\u30b9\u306b\u8a98\u3044\u307e\u3059\u3002\u5f7c\u5973\u306e\u30b9\u30c6\u30c3\u30d7\u306b\u5f93\u3063\u3066\u3001\u8ad6\u7406\u3068\u7f8e\u306b\u6e80\u3061\u305f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u4e16\u754c\u306b\u5165\u308a\u307e\u3057\u3087\u3046\u3002

        "},{"location":"chapter_introduction/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 1.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3069\u3053\u306b\u3067\u3082\u3042\u308b
        • 1.2 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u306f\u4f55\u304b
        • 1.3 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_introduction/algorithms_are_everywhere/","title":"1.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u81f3\u308b\u6240\u306b\u3042\u308b","text":"

        \u300c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u300d\u3068\u3044\u3046\u8a00\u8449\u3092\u805e\u304f\u3068\u3001\u81ea\u7136\u306b\u6570\u5b66\u3092\u601d\u3044\u6d6e\u304b\u3079\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u591a\u304f\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u8907\u96d1\u306a\u6570\u5b66\u3092\u542b\u307e\u305a\u3001\u57fa\u672c\u7684\u306a\u8ad6\u7406\u306b\u3088\u308a\u591a\u304f\u4f9d\u5b58\u3057\u3066\u304a\u308a\u3001\u3053\u308c\u306f\u79c1\u305f\u3061\u306e\u65e5\u5e38\u751f\u6d3b\u306e\u81f3\u308b\u6240\u3067\u898b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u3064\u3044\u3066\u6b63\u5f0f\u306b\u8b70\u8ad6\u3092\u59cb\u3081\u308b\u524d\u306b\u3001\u8208\u5473\u6df1\u3044\u4e8b\u5b9f\u3092\u5171\u6709\u3059\u308b\u4fa1\u5024\u304c\u3042\u308a\u307e\u3059\u3002\u3042\u306a\u305f\u306f\u7121\u610f\u8b58\u306e\u3046\u3061\u306b\u591a\u304f\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u5b66\u3073\u3001\u65e5\u5e38\u751f\u6d3b\u3067\u305d\u308c\u3089\u3092\u5fdc\u7528\u3059\u308b\u3053\u3068\u306b\u6163\u308c\u3066\u3044\u307e\u3059\u3002\u3053\u3053\u3067\u3001\u3053\u306e\u70b9\u3092\u8a3c\u660e\u3059\u308b\u305f\u3081\u306b\u3044\u304f\u3064\u304b\u306e\u5177\u4f53\u7684\u306a\u4f8b\u3092\u6319\u3052\u307e\u3059\u3002

        \u4f8b1\uff1a\u8f9e\u66f8\u306e\u5f15\u304d\u65b9\u3002\u82f1\u8a9e\u306e\u8f9e\u66f8\u3067\u306f\u3001\u5358\u8a9e\u304c\u30a2\u30eb\u30d5\u30a1\u30d9\u30c3\u30c8\u9806\u306b\u4e26\u3093\u3067\u3044\u307e\u3059\u3002\\(r\\)\u3067\u59cb\u307e\u308b\u5358\u8a9e\u3092\u63a2\u3057\u3066\u3044\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u901a\u5e38\u306f\u4ee5\u4e0b\u306e\u65b9\u6cd5\u3067\u884c\u3044\u307e\u3059\uff1a

        1. \u8f9e\u66f8\u3092\u5927\u4f53\u534a\u5206\u3050\u3089\u3044\u306e\u3068\u3053\u308d\u3067\u958b\u304d\u3001\u305d\u306e\u30da\u30fc\u30b8\u306e\u6700\u521d\u306e\u8a9e\u5f59\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\\(m\\)\u3067\u59cb\u307e\u308b\u6587\u5b57\u3060\u3068\u3057\u307e\u3057\u3087\u3046\u3002
        2. \\(r\\)\u306f\u30a2\u30eb\u30d5\u30a1\u30d9\u30c3\u30c8\u3067\\(m\\)\u306e\u5f8c\u306b\u6765\u308b\u306e\u3067\u3001\u524d\u534a\u3092\u7121\u8996\u3057\u3066\u3001\u63a2\u7d22\u7a7a\u9593\u3092\u5f8c\u534a\u306b\u7d5e\u308a\u307e\u3059\u3002
        3. \\(r\\)\u3067\u59cb\u307e\u308b\u5358\u8a9e\u3092\u898b\u3064\u3051\u308b\u307e\u3067\u3001\u30b9\u30c6\u30c3\u30d71.\u30682.\u3092\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002
        <1><2><3><4><5>

        \u56f3 1-1 \u00a0 \u8f9e\u66f8\u3092\u5f15\u304f\u904e\u7a0b

        \u8f9e\u66f8\u3092\u5f15\u304f\u3053\u3068\u306f\u3001\u5c0f\u5b66\u751f\u306b\u3068\u3063\u3066\u5fc5\u9808\u306e\u30b9\u30ad\u30eb\u3067\u3059\u304c\u3001\u5b9f\u969b\u306b\u306f\u6709\u540d\u306a\u300c\u4e8c\u5206\u63a2\u7d22\u300d\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3059\u3002\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u89b3\u70b9\u304b\u3089\u3001\u8f9e\u66f8\u3092\u30bd\u30fc\u30c8\u3055\u308c\u305f\u300c\u914d\u5217\u300d\u3068\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u89b3\u70b9\u304b\u3089\u3001\u8f9e\u66f8\u3067\u5358\u8a9e\u3092\u63a2\u3059\u305f\u3081\u306b\u53d6\u3089\u308c\u308b\u4e00\u9023\u306e\u884c\u52d5\u306f\u3001\u300c\u4e8c\u5206\u63a2\u7d22\u300d\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u898b\u306a\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u4f8b2\uff1a\u30c8\u30e9\u30f3\u30d7\u306e\u6574\u7406\u3002\u30c8\u30e9\u30f3\u30d7\u3092\u30d7\u30ec\u30a4\u3059\u308b\u3068\u304d\u3001\u624b\u672d\u3092\u6607\u9806\u306b\u4e26\u3079\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u904e\u7a0b\u3067\u793a\u3055\u308c\u307e\u3059\u3002

        1. \u30c8\u30e9\u30f3\u30d7\u3092\u300c\u6574\u5217\u6e08\u307f\u300d\u3068\u300c\u672a\u6574\u5217\u300d\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u306b\u5206\u3051\u307e\u3059\u3002\u6700\u521d\u306f\u4e00\u756a\u5de6\u306e\u30ab\u30fc\u30c9\u304c\u65e2\u306b\u6574\u5217\u3057\u3066\u3044\u308b\u3068\u4eee\u5b9a\u3057\u307e\u3059\u3002
        2. \u672a\u6574\u5217\u30bb\u30af\u30b7\u30e7\u30f3\u304b\u3089\u30ab\u30fc\u30c9\u30921\u679a\u53d6\u308a\u51fa\u3057\u3001\u6574\u5217\u6e08\u307f\u30bb\u30af\u30b7\u30e7\u30f3\u306e\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u633f\u5165\u3057\u307e\u3059\u3002\u3053\u306e\u5f8c\u3001\u5de6\u7aef\u306e2\u679a\u306e\u30ab\u30fc\u30c9\u304c\u6574\u5217\u3057\u307e\u3059\u3002
        3. \u3059\u3079\u3066\u306e\u30ab\u30fc\u30c9\u304c\u6574\u5217\u3059\u308b\u307e\u3067\u3001\u30b9\u30c6\u30c3\u30d72\u3092\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002

        \u56f3 1-2 \u00a0 \u30c8\u30e9\u30f3\u30d7\u306e\u6574\u7406\u904e\u7a0b

        \u4e0a\u8a18\u306e\u30c8\u30e9\u30f3\u30d7\u3092\u6574\u7406\u3059\u308b\u65b9\u6cd5\u306f\u3001\u5b9f\u8cea\u7684\u306b\u300c\u633f\u5165\u30bd\u30fc\u30c8\u300d\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3042\u308a\u3001\u5c0f\u3055\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306b\u5bfe\u3057\u3066\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3059\u3002\u591a\u304f\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u30bd\u30fc\u30c8\u95a2\u6570\u306b\u306f\u633f\u5165\u30bd\u30fc\u30c8\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002

        \u4f8b3\uff1a\u304a\u91e3\u308a\u306e\u8a08\u7b97\u3002\u30b9\u30fc\u30d1\u30fc\u30de\u30fc\u30b1\u30c3\u30c8\u3067\\(69\\)\u306e\u8cb7\u3044\u7269\u3092\u3057\u305f\u3068\u4eee\u5b9a\u3057\u307e\u3059\u3002\u30ec\u30b8\u4fc2\u306b\\(100\\)\u3092\u6e21\u3059\u3068\u3001\\(31\\)\u306e\u304a\u91e3\u308a\u3092\u63d0\u4f9b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u904e\u7a0b\u306f\u4ee5\u4e0b\u306e\u56f3\u3067\u660e\u78ba\u306b\u7406\u89e3\u3067\u304d\u307e\u3059\u3002

        1. \u9078\u629e\u80a2\u306f\\(31\\)\u4ee5\u4e0b\u306e\u4fa1\u5024\u306e\u3042\u308b\u901a\u8ca8\u3067\u3001\\(1\\)\u3001\\(5\\)\u3001\\(10\\)\u3001\\(20\\)\u304c\u542b\u307e\u308c\u307e\u3059\u3002
        2. \u9078\u629e\u80a2\u304b\u3089\u6700\u5927\u306e\\(20\\)\u3092\u53d6\u308a\u51fa\u3057\u3001\\(31 - 20 = 11\\)\u304c\u6b8b\u308a\u307e\u3059\u3002
        3. \u6b8b\u308a\u306e\u9078\u629e\u80a2\u304b\u3089\u6700\u5927\u306e\\(10\\)\u3092\u53d6\u308a\u51fa\u3057\u3001\\(11 - 10 = 1\\)\u304c\u6b8b\u308a\u307e\u3059\u3002
        4. \u6b8b\u308a\u306e\u9078\u629e\u80a2\u304b\u3089\u6700\u5927\u306e\\(1\\)\u3092\u53d6\u308a\u51fa\u3057\u3001\\(1 - 1 = 0\\)\u304c\u6b8b\u308a\u307e\u3059\u3002
        5. \u304a\u91e3\u308a\u306e\u8a08\u7b97\u304c\u5b8c\u4e86\u3057\u3001\u89e3\u7b54\u306f\\(20 + 10 + 1 = 31\\)\u3067\u3059\u3002

        \u56f3 1-3 \u00a0 \u304a\u91e3\u308a\u306e\u8a08\u7b97\u904e\u7a0b

        \u8a18\u8ff0\u3055\u308c\u305f\u30b9\u30c6\u30c3\u30d7\u3067\u306f\u3001\u5229\u7528\u53ef\u80fd\u306a\u6700\u5927\u306e\u984d\u9762\u3092\u4f7f\u7528\u3057\u3066\u5404\u6bb5\u968e\u3067\u6700\u826f\u306e\u9078\u629e\u80a2\u3092\u9078\u3076\u3053\u3068\u3067\u3001\u52b9\u679c\u7684\u306a\u304a\u91e3\u308a\u8a08\u7b97\u6226\u7565\u306b\u3064\u306a\u304c\u308a\u307e\u3059\u3002\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u89b3\u70b9\u304b\u3089\u3001\u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\u300c\u8caa\u6b32\u300d\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u3057\u3066\u77e5\u3089\u308c\u3066\u3044\u307e\u3059\u3002

        \u6599\u7406\u306e\u6e96\u5099\u304b\u3089\u5b87\u5b99\u65c5\u884c\u307e\u3067\u3001\u307b\u307c\u3059\u3079\u3066\u306e\u554f\u984c\u89e3\u6c7a\u306b\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u95a2\u308f\u3063\u3066\u3044\u307e\u3059\u3002\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u51fa\u73fe\u306b\u3088\u308a\u3001\u30e1\u30e2\u30ea\u306b\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u683c\u7d0d\u3057\u3001CPU\u3068GPU\u3092\u547c\u3073\u51fa\u3057\u3066\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u5b9f\u884c\u3059\u308b\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002\u3053\u306e\u3088\u3046\u306b\u3057\u3066\u3001\u73fe\u5b9f\u4e16\u754c\u306e\u554f\u984c\u3092\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306b\u79fb\u3057\u3001\u3088\u308a\u52b9\u7387\u7684\u306a\u65b9\u6cd5\u3067\u3055\u307e\u3056\u307e\u306a\u8907\u96d1\u306a\u554f\u984c\u3092\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002

        Tip

        \u30c7\u30fc\u30bf\u69cb\u9020\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3001\u914d\u5217\u3001\u4e8c\u5206\u63a2\u7d22\u306a\u3069\u306e\u6982\u5ff5\u306b\u3064\u3044\u3066\u307e\u3060\u6df7\u4e71\u3057\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u8aad\u307f\u7d9a\u3051\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002\u3053\u306e\u672c\u306f\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7406\u89e3\u306e\u9818\u57df\u3078\u3068\u512a\u3057\u304f\u5c0e\u3044\u3066\u304f\u308c\u308b\u3067\u3057\u3087\u3046\u3002

        "},{"location":"chapter_introduction/summary/","title":"1.3 \u00a0 \u307e\u3068\u3081","text":"
        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u65e5\u5e38\u751f\u6d3b\u306b\u3042\u308a\u3075\u308c\u3066\u304a\u308a\u3001\u601d\u3063\u3066\u3044\u308b\u307b\u3069\u30a2\u30af\u30bb\u30b9\u3057\u306b\u304f\u304f\u8907\u96d1\u306a\u3082\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u5b9f\u969b\u3001\u79c1\u305f\u3061\u306f\u65e2\u306b\u7121\u610f\u8b58\u306e\u3046\u3061\u306b\u591a\u304f\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u5b66\u3073\u3001\u751f\u6d3b\u306e\u69d8\u3005\u306a\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002
        • \u8f9e\u66f8\u3067\u5358\u8a9e\u3092\u5f15\u304f\u539f\u7406\u306f\u4e8c\u5206\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u4e00\u81f4\u3057\u3066\u3044\u307e\u3059\u3002\u4e8c\u5206\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u5206\u5272\u7d71\u6cbb\u3068\u3044\u3046\u91cd\u8981\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u6982\u5ff5\u3092\u4f53\u73fe\u3057\u3066\u3044\u307e\u3059\u3002
        • \u30c8\u30e9\u30f3\u30d7\u3092\u6574\u7406\u3059\u308b\u904e\u7a0b\u306f\u633f\u5165\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u975e\u5e38\u306b\u4f3c\u3066\u3044\u307e\u3059\u3002\u633f\u5165\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u5c0f\u3055\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306e\u30bd\u30fc\u30c8\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002
        • \u901a\u8ca8\u3067\u304a\u91e3\u308a\u3092\u8a08\u7b97\u3059\u308b\u30b9\u30c6\u30c3\u30d7\u306f\u672c\u8cea\u7684\u306b\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u5f93\u3063\u3066\u304a\u308a\u3001\u5404\u30b9\u30c6\u30c3\u30d7\u3067\u305d\u306e\u6642\u70b9\u3067\u306e\u6700\u826f\u306e\u9078\u629e\u3092\u3057\u307e\u3059\u3002
        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u6709\u9650\u6642\u9593\u5185\u3067\u7279\u5b9a\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306e\u6bb5\u968e\u7684\u306a\u6307\u793a\u306e\u30bb\u30c3\u30c8\u3067\u3059\u304c\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u3067\u306e\u30c7\u30fc\u30bf\u306e\u7d44\u7e54\u5316\u3068\u4fdd\u5b58\u65b9\u6cd5\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002
        • \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u5bc6\u63a5\u306b\u95a2\u9023\u3057\u3066\u3044\u307e\u3059\u3002\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u57fa\u790e\u3067\u3042\u308a\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u6a5f\u80fd\u3092\u6d3b\u7528\u3059\u308b\u30b9\u30c6\u30fc\u30b8\u3067\u3059\u3002
        • \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u30d6\u30ed\u30c3\u30af\u306e\u7d44\u307f\u7acb\u3066\u306b\u4f8b\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u30d6\u30ed\u30c3\u30af\u306f\u30c7\u30fc\u30bf\u3092\u8868\u3057\u3001\u30d6\u30ed\u30c3\u30af\u306e\u5f62\u72b6\u3068\u63a5\u7d9a\u65b9\u6cd5\u306f\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u8868\u3057\u3001\u30d6\u30ed\u30c3\u30af\u3092\u7d44\u307f\u7acb\u3066\u308b\u30b9\u30c6\u30c3\u30d7\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002
        "},{"location":"chapter_introduction/summary/#1-q-a","title":"1. \u00a0 Q & A","text":"

        Q\uff1a\u30d7\u30ed\u30b0\u30e9\u30de\u30fc\u3068\u3057\u3066\u3001\u65e5\u5e38\u306e\u4ed5\u4e8b\u3067\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u624b\u52d5\u3067\u5b9f\u88c5\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3053\u3068\u306f\u3081\u3063\u305f\u306b\u3042\u308a\u307e\u305b\u3093\u3002\u6700\u3082\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u65e2\u306b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3068\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u7d44\u307f\u8fbc\u307e\u308c\u3066\u304a\u308a\u3001\u3059\u3050\u306b\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u79c1\u305f\u3061\u304c\u4ed5\u4e8b\u3067\u906d\u9047\u3059\u308b\u554f\u984c\u304c\u3001\u30ab\u30b9\u30bf\u30e0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u8a2d\u8a08\u3092\u5fc5\u8981\u3068\u3059\u308b\u8907\u96d1\u3055\u306e\u30ec\u30d9\u30eb\u306b\u307e\u3060\u9054\u3057\u3066\u3044\u306a\u3044\u3053\u3068\u3092\u793a\u5506\u3057\u3066\u3044\u307e\u3059\u304b\uff1f

        \u7279\u5b9a\u306e\u4ed5\u4e8b\u30b9\u30ad\u30eb\u304c\u6b66\u8853\u306e\u300c\u6280\u300d\u306e\u3088\u3046\u306a\u3082\u306e\u3060\u3068\u3059\u308c\u3070\u3001\u57fa\u790e\u79d1\u76ee\u306f\u300c\u5185\u529f\u300d\u306e\u3088\u3046\u306a\u3082\u306e\u3067\u3059\u3002

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff08\u304a\u3088\u3073\u305d\u306e\u4ed6\u306e\u57fa\u790e\u79d1\u76ee\uff09\u3092\u5b66\u3076\u610f\u7fa9\u306f\u3001\u5fc5\u305a\u3057\u3082\u4ed5\u4e8b\u3067\u305d\u308c\u3089\u3092\u4e00\u304b\u3089\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3067\u306f\u306a\u304f\u3001\u6982\u5ff5\u306e\u78ba\u56fa\u305f\u308b\u7406\u89e3\u306b\u57fa\u3065\u3044\u3066\u3001\u3088\u308a\u5c02\u9580\u7684\u306a\u610f\u601d\u6c7a\u5b9a\u3068\u554f\u984c\u89e3\u6c7a\u3092\u53ef\u80fd\u306b\u3057\u3001\u305d\u308c\u306b\u3088\u3063\u3066\u4ed5\u4e8b\u306e\u5168\u4f53\u7684\u306a\u8cea\u3092\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u3060\u3068\u79c1\u306f\u4fe1\u3058\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u3059\u3079\u3066\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30bd\u30fc\u30c8\u95a2\u6570\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u5b66\u3093\u3067\u3044\u306a\u3044\u5834\u5408\u3001\u3069\u3093\u306a\u30c7\u30fc\u30bf\u304c\u4e0e\u3048\u3089\u308c\u3066\u3082\u3001\u3053\u306e\u30bd\u30fc\u30c8\u95a2\u6570\u306b\u6e21\u3059\u3060\u3051\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u30b9\u30e0\u30fc\u30ba\u306b\u52d5\u4f5c\u3057\u3001\u826f\u3044\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u793a\u3057\u3001\u554f\u984c\u304c\u306a\u3044\u3088\u3046\u306b\u898b\u3048\u307e\u3059\u3002
        • \u3057\u304b\u3057\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u5b66\u3093\u3060\u3053\u3068\u304c\u3042\u308c\u3070\u3001\u7d44\u307f\u8fbc\u307f\u306e\u30bd\u30fc\u30c8\u95a2\u6570\u306e\u6642\u9593\u8907\u96d1\u5ea6\u306f\u901a\u5e38\\(O(n \\log n)\\)\u3067\u3042\u308b\u3053\u3068\u3092\u7406\u89e3\u3057\u3066\u3044\u307e\u3059\u3002\u3055\u3089\u306b\u3001\u30c7\u30fc\u30bf\u304c\u56fa\u5b9a\u6841\u6570\u306e\u6574\u6570\uff08\u5b66\u751fID\u306a\u3069\uff09\u3067\u69cb\u6210\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u57fa\u6570\u30bd\u30fc\u30c8\u306e\u3088\u3046\u306a\u3088\u308a\u52b9\u7387\u7684\u306a\u30a2\u30d7\u30ed\u30fc\u30c1\u3092\u9069\u7528\u3067\u304d\u3001\u6642\u9593\u8907\u96d1\u5ea6\u3092O(nk)\u306b\u524a\u6e1b\u3067\u304d\u307e\u3059\u3002\u3053\u3053\u3067k\u306f\u6841\u6570\u3067\u3059\u3002\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u51e6\u7406\u3059\u308b\u969b\u3001\u7bc0\u7d04\u3055\u308c\u305f\u6642\u9593\u306f\u91cd\u8981\u306a\u4fa1\u5024\u306b\u5909\u308f\u308a\u307e\u3059 \u2014 \u30b3\u30b9\u30c8\u306e\u524a\u6e1b\u3001\u30e6\u30fc\u30b6\u30fc\u30a8\u30af\u30b9\u30da\u30ea\u30a8\u30f3\u30b9\u306e\u5411\u4e0a\u3001\u30b7\u30b9\u30c6\u30e0\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306e\u5411\u4e0a\u3002

        \u30a8\u30f3\u30b8\u30cb\u30a2\u30ea\u30f3\u30b0\u3067\u306f\u3001\u591a\u304f\u306e\u554f\u984c\u3092\u6700\u9069\u306b\u89e3\u6c7a\u3059\u308b\u3053\u3068\u306f\u56f0\u96e3\u3067\u3059\u3002\u307b\u3068\u3093\u3069\u306f\u300c\u6e96\u6700\u9069\u300d\u89e3\u6c7a\u7b56\u3067\u5bfe\u51e6\u3055\u308c\u307e\u3059\u3002\u554f\u984c\u306e\u96e3\u3057\u3055\u306f\u3001\u305d\u306e\u56fa\u6709\u306e\u8907\u96d1\u3055\u3060\u3051\u3067\u306a\u304f\u3001\u305d\u308c\u306b\u53d6\u308a\u7d44\u3080\u4eba\u306e\u77e5\u8b58\u3068\u7d4c\u9a13\u306b\u3082\u4f9d\u5b58\u3057\u307e\u3059\u3002\u5c02\u9580\u77e5\u8b58\u3068\u7d4c\u9a13\u304c\u6df1\u3044\u307b\u3069\u3001\u5206\u6790\u304c\u3088\u308a\u5fb9\u5e95\u7684\u306b\u306a\u308a\u3001\u554f\u984c\u3092\u3088\u308a\u512a\u96c5\u306b\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_introduction/what_is_dsa/","title":"1.2 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u306f\u4f55\u304b","text":""},{"location":"chapter_introduction/what_is_dsa/#121","title":"1.2.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9a\u7fa9","text":"

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u6709\u9650\u6642\u9593\u5185\u3067\u7279\u5b9a\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306e\u4e00\u9023\u306e\u6307\u793a\u307e\u305f\u306f\u30b9\u30c6\u30c3\u30d7\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u7279\u5fb4\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u554f\u984c\u304c\u660e\u78ba\u306b\u5b9a\u7fa9\u3055\u308c\u3066\u304a\u308a\u3001\u5165\u529b\u3068\u51fa\u529b\u306e\u660e\u78ba\u306a\u5b9a\u7fa9\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002
        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u5b9f\u884c\u53ef\u80fd\u3067\u3001\u6709\u9650\u306e\u56de\u6570\u306e\u30b9\u30c6\u30c3\u30d7\u3001\u6642\u9593\u3001\u30e1\u30e2\u30ea\u7a7a\u9593\u5185\u3067\u5b8c\u4e86\u3067\u304d\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002
        • \u5404\u30b9\u30c6\u30c3\u30d7\u306b\u306f\u660e\u78ba\u306a\u610f\u5473\u304c\u3042\u308a\u307e\u3059\u3002\u540c\u3058\u5165\u529b\u3068\u6761\u4ef6\u306e\u4e0b\u3067\u51fa\u529b\u306f\u4e00\u8cab\u3057\u3066\u540c\u3058\u3067\u3059\u3002
        "},{"location":"chapter_introduction/what_is_dsa/#122","title":"1.2.2 \u00a0 \u30c7\u30fc\u30bf\u69cb\u9020\u306e\u5b9a\u7fa9","text":"

        \u30c7\u30fc\u30bf\u69cb\u9020\u306f\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u5185\u3067\u30c7\u30fc\u30bf\u3092\u7d44\u7e54\u3057\u4fdd\u5b58\u3059\u308b\u65b9\u6cd5\u3067\u3001\u4ee5\u4e0b\u306e\u8a2d\u8a08\u76ee\u6a19\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u30e1\u30e2\u30ea\u3092\u7bc0\u7d04\u3059\u308b\u305f\u3081\u306b\u7a7a\u9593\u5360\u6709\u3092\u6700\u5c0f\u5316\u3059\u308b\u3002
        • \u30c7\u30fc\u30bf\u64cd\u4f5c\u3092\u53ef\u80fd\u306a\u9650\u308a\u9ad8\u901f\u306b\u3057\u3001\u30c7\u30fc\u30bf\u306e\u30a2\u30af\u30bb\u30b9\u3001\u8ffd\u52a0\u3001\u524a\u9664\u3001\u66f4\u65b0\u306a\u3069\u3092\u30ab\u30d0\u30fc\u3059\u308b\u3002
        • \u52b9\u7387\u7684\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u5b9f\u884c\u3092\u53ef\u80fd\u306b\u3059\u308b\u305f\u3081\u306b\u3001\u7c21\u6f54\u306a\u30c7\u30fc\u30bf\u8868\u73fe\u3068\u8ad6\u7406\u60c5\u5831\u3092\u63d0\u4f9b\u3059\u308b\u3002

        \u30c7\u30fc\u30bf\u69cb\u9020\u306e\u8a2d\u8a08\u306f\u30d0\u30e9\u30f3\u30b9\u3092\u53d6\u308b\u884c\u70ba\u3067\u3042\u308a\u3001\u3057\u3070\u3057\u3070\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u304c\u5fc5\u8981\u3067\u3059\u3002\u4e00\u3064\u306e\u5074\u9762\u3092\u6539\u5584\u3057\u305f\u3044\u5834\u5408\u3001\u3057\u3070\u3057\u3070\u5225\u306e\u5074\u9762\u3067\u59a5\u5354\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306f2\u3064\u306e\u4f8b\u3067\u3059\uff1a

        • \u914d\u5217\u3068\u6bd4\u8f03\u3057\u3066\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u30c7\u30fc\u30bf\u306e\u8ffd\u52a0\u3068\u524a\u9664\u306b\u304a\u3044\u3066\u3088\u308a\u4fbf\u5229\u3067\u3059\u304c\u3001\u30c7\u30fc\u30bf\u30a2\u30af\u30bb\u30b9\u901f\u5ea6\u3092\u72a0\u7272\u306b\u3057\u307e\u3059\u3002
        • \u9023\u7d50\u30ea\u30b9\u30c8\u3068\u6bd4\u8f03\u3057\u3066\u3001\u30b0\u30e9\u30d5\u306f\u3088\u308a\u8c4a\u5bcc\u306a\u8ad6\u7406\u60c5\u5831\u3092\u63d0\u4f9b\u3057\u307e\u3059\u304c\u3001\u3088\u308a\u591a\u304f\u306e\u30e1\u30e2\u30ea\u7a7a\u9593\u304c\u5fc5\u8981\u3067\u3059\u3002
        "},{"location":"chapter_introduction/what_is_dsa/#123","title":"1.2.3 \u00a0 \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u95a2\u4fc2","text":"

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u9ad8\u5ea6\u306b\u95a2\u9023\u3057\u3001\u5bc6\u63a5\u306b\u7d71\u5408\u3055\u308c\u3066\u304a\u308a\u3001\u5177\u4f53\u7684\u306b\u306f\u4ee5\u4e0b\u306e3\u3064\u306e\u5074\u9762\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u30c7\u30fc\u30bf\u69cb\u9020\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u57fa\u790e\u3067\u3059\u3002\u69cb\u9020\u5316\u3055\u308c\u305f\u30c7\u30fc\u30bf\u4fdd\u5b58\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u305f\u3081\u306e\u30c7\u30fc\u30bf\u64cd\u4f5c\u65b9\u6cd5\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002
        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u6d3b\u529b\u3092\u6ce8\u5165\u3057\u307e\u3059\u3002\u30c7\u30fc\u30bf\u69cb\u9020\u3060\u3051\u3067\u306f\u30c7\u30fc\u30bf\u60c5\u5831\u3092\u4fdd\u5b58\u3059\u308b\u3060\u3051\u3067\u3059\u3002\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5fdc\u7528\u306b\u3088\u3063\u3066\u3001\u7279\u5b9a\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002
        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u7570\u306a\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u57fa\u3065\u3044\u3066\u5b9f\u88c5\u3067\u304d\u308b\u3053\u3068\u304c\u591a\u3044\u3067\u3059\u304c\u3001\u5b9f\u884c\u52b9\u7387\u306f\u5927\u304d\u304f\u7570\u306a\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002\u9069\u5207\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u9078\u629e\u3059\u308b\u3053\u3068\u304c\u9375\u3067\u3059\u3002

        \u56f3 1-4 \u00a0 \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u95a2\u4fc2

        \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30d6\u30ed\u30c3\u30af\u306e\u30bb\u30c3\u30c8\u306b\u4f8b\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u30d6\u30ed\u30c3\u30af\u30bb\u30c3\u30c8\u306b\u306f\u591a\u6570\u306e\u30d4\u30fc\u30b9\u304c\u542b\u307e\u308c\u3001\u8a73\u7d30\u306a\u7d44\u307f\u7acb\u3066\u8aac\u660e\u66f8\u304c\u4ed8\u3044\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u8aac\u660e\u66f8\u306b\u6bb5\u968e\u7684\u306b\u5f93\u3046\u3053\u3068\u3067\u3001\u8907\u96d1\u306a\u30d6\u30ed\u30c3\u30af\u30e2\u30c7\u30eb\u3092\u69cb\u7bc9\u3067\u304d\u307e\u3059\u3002

        \u56f3 1-5 \u00a0 \u30d6\u30ed\u30c3\u30af\u306e\u7d44\u307f\u7acb\u3066

        \u4e21\u8005\u306e\u8a73\u7d30\u306a\u5bfe\u5fdc\u95a2\u4fc2\u306f\u4ee5\u4e0b\u306e\u8868\u306b\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        \u8868 1-1 \u00a0 \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u30d6\u30ed\u30c3\u30af\u3068\u6bd4\u8f03

        \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0 \u30d6\u30ed\u30c3\u30af \u5165\u529b\u30c7\u30fc\u30bf \u672a\u7d44\u307f\u7acb\u3066\u306e\u30d6\u30ed\u30c3\u30af \u30c7\u30fc\u30bf\u69cb\u9020 \u30d6\u30ed\u30c3\u30af\u306e\u7d44\u7e54\u3001\u5f62\u72b6\u3001\u30b5\u30a4\u30ba\u3001\u63a5\u7d9a\u306a\u3069\u3092\u542b\u3080 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0 \u30d6\u30ed\u30c3\u30af\u3092\u671b\u307e\u3057\u3044\u5f62\u72b6\u306b\u7d44\u307f\u7acb\u3066\u308b\u4e00\u9023\u306e\u30b9\u30c6\u30c3\u30d7 \u51fa\u529b\u30c7\u30fc\u30bf \u5b8c\u6210\u3057\u305f\u30d6\u30ed\u30c3\u30af\u30e2\u30c7\u30eb

        \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u304b\u3089\u72ec\u7acb\u3057\u3066\u3044\u308b\u3053\u3068\u306f\u6ce8\u76ee\u306b\u5024\u3057\u307e\u3059\u3002\u3053\u306e\u7406\u7531\u306b\u3088\u308a\u3001\u3053\u306e\u672c\u306f\u8907\u6570\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u306e\u5b9f\u88c5\u3092\u63d0\u4f9b\u3067\u304d\u307e\u3059\u3002

        \u6163\u7fd2\u7684\u306a\u7565\u8a9e

        \u5b9f\u751f\u6d3b\u306e\u8b70\u8ad6\u3067\u306f\u3001\u300c\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u300d\u3092\u5358\u7d14\u306b\u300c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u300d\u3068\u547c\u3076\u3053\u3068\u304c\u3088\u304f\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u3088\u304f\u77e5\u3089\u308c\u305fLeetCode\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u306f\u3001\u5b9f\u969b\u306b\u306f\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u4e21\u65b9\u306e\u77e5\u8b58\u3092\u30c6\u30b9\u30c8\u3057\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_preface/","title":"\u7b2c 0 \u7ae0 \u00a0 \u5e8f\u6587","text":"

        Abstract

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u7f8e\u3057\u3044\u4ea4\u97ff\u66f2\u306e\u3088\u3046\u3067\u3001\u30b3\u30fc\u30c9\u306e\u4e00\u884c\u4e00\u884c\u304c\u30ea\u30ba\u30e0\u306e\u3088\u3046\u306b\u6d41\u308c\u3066\u3044\u307e\u3059\u3002

        \u3053\u306e\u672c\u304c\u3042\u306a\u305f\u306e\u5fc3\u306e\u4e2d\u3067\u9759\u304b\u306b\u97ff\u304d\u3001\u72ec\u7279\u3067\u6df1\u3044\u65cb\u5f8b\u3092\u6b8b\u3059\u3053\u3068\u3092\u9858\u3063\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_preface/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 0.1 \u00a0 \u3053\u306e\u672c\u306b\u3064\u3044\u3066
        • 0.2 \u00a0 \u672c\u66f8\u306e\u4f7f\u3044\u65b9
        • 0.3 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_preface/about_the_book/","title":"0.1 \u00a0 \u3053\u306e\u672c\u306b\u3064\u3044\u3066","text":"

        \u3053\u306e\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306f\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u95a2\u3059\u308b\u7121\u6599\u3067\u521d\u5fc3\u8005\u306b\u3084\u3055\u3057\u3044\u30af\u30e9\u30c3\u30b7\u30e5\u30b3\u30fc\u30b9\u306e\u4f5c\u6210\u3092\u76ee\u6307\u3057\u3066\u3044\u307e\u3059\u3002

        • \u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u4ed8\u304d\u306e\u56f3\u89e3\u3001\u7406\u89e3\u3057\u3084\u3059\u3044\u5185\u5bb9\u3001\u6ed1\u3089\u304b\u306a\u5b66\u7fd2\u66f2\u7dda\u306b\u3088\u308a\u3001\u521d\u5fc3\u8005\u304c\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u300c\u77e5\u8b58\u30de\u30c3\u30d7\u300d\u3092\u63a2\u7d22\u3059\u308b\u306e\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002
        • \u30ef\u30f3\u30af\u30ea\u30c3\u30af\u3067\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3067\u304d\u308b\u305f\u3081\u3001\u8aad\u8005\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30b9\u30ad\u30eb\u306e\u5411\u4e0a\u3068\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u52d5\u4f5c\u539f\u7406\u304a\u3088\u3073\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u57fa\u790e\u5b9f\u88c5\u306e\u7406\u89e3\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002
        • \u6559\u3048\u308b\u3053\u3068\u306b\u3088\u308b\u5b66\u7fd2\u3092\u4fc3\u9032\u3057\u3001\u8cea\u554f\u3084\u6d1e\u5bdf\u306e\u5171\u6709\u3092\u81ea\u7531\u306b\u884c\u3063\u3066\u304f\u3060\u3055\u3044\u3002\u8b70\u8ad6\u3092\u901a\u3058\u3066\u4e00\u7dd2\u306b\u6210\u9577\u3057\u307e\u3057\u3087\u3046\u3002
        "},{"location":"chapter_preface/about_the_book/#011","title":"0.1.1 \u00a0 \u5bfe\u8c61\u8aad\u8005","text":"

        \u3082\u3057\u3042\u306a\u305f\u304c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u89e6\u308c\u305f\u3070\u304b\u308a\u3067\u7d4c\u9a13\u304c\u9650\u3089\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u307e\u305f\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3042\u308b\u7a0b\u5ea6\u306e\u7d4c\u9a13\u3092\u7a4d\u3093\u3067\u3044\u3066\u3082\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u3064\u3044\u3066\u66d6\u6627\u306a\u7406\u89e3\u3057\u304b\u306a\u304f\u3001\u5e38\u306b\u300c\u5206\u304b\u3063\u305f\u300d\u3068\u300c\u3046\u30fc\u3093\u300d\u306e\u9593\u3092\u884c\u304d\u6765\u3057\u3066\u3044\u308b\u5834\u5408\u3001\u3053\u306e\u672c\u306f\u3042\u306a\u305f\u306e\u305f\u3081\u306e\u3082\u306e\u3067\u3059\uff01

        \u3059\u3067\u306b\u3042\u308b\u7a0b\u5ea6\u306e\u554f\u984c\u89e3\u6c7a\u7d4c\u9a13\u3092\u7a4d\u3093\u3067\u304a\u308a\u3001\u307b\u3068\u3093\u3069\u306e\u30bf\u30a4\u30d7\u306e\u554f\u984c\u306b\u7cbe\u901a\u3057\u3066\u3044\u308b\u5834\u5408\u3001\u3053\u306e\u672c\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u77e5\u8b58\u4f53\u7cfb\u3092\u5fa9\u7fd2\u3057\u6574\u7406\u3059\u308b\u306e\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002\u30ea\u30dd\u30b8\u30c8\u30ea\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306f\u300c\u554f\u984c\u89e3\u6c7a\u30c4\u30fc\u30eb\u30ad\u30c3\u30c8\u300d\u3084\u300c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30c1\u30fc\u30c8\u30b7\u30fc\u30c8\u300d\u3068\u3057\u3066\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002

        \u3082\u3057\u3042\u306a\u305f\u304c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5c02\u9580\u5bb6\u3067\u3042\u308c\u3070\u3001\u8cb4\u91cd\u306a\u63d0\u6848\u3092\u3044\u305f\u3060\u304f\u304b\u3001\u53c2\u52a0\u3057\u3066\u5354\u529b\u3057\u3066\u3044\u305f\u3060\u3051\u308c\u3070\u3068\u601d\u3044\u307e\u3059\u3002

        \u524d\u63d0\u6761\u4ef6

        \u5c11\u306a\u304f\u3068\u3082\u4e00\u3064\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u7c21\u5358\u306a\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u8aad\u3080\u3053\u3068\u304c\u3067\u304d\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_preface/about_the_book/#012","title":"0.1.2 \u00a0 \u5185\u5bb9\u69cb\u6210","text":"

        \u672c\u66f8\u306e\u4e3b\u306a\u5185\u5bb9\u3092\u4e0b\u56f3\u306b\u793a\u3057\u307e\u3059\u3002

        • \u8a08\u7b97\u91cf\u89e3\u6790: \u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u8a55\u4fa1\u3059\u308b\u5074\u9762\u3068\u65b9\u6cd5\u3092\u63a2\u6c42\u3057\u307e\u3059\u3002\u6642\u9593\u8a08\u7b97\u91cf\u3068\u7a7a\u9593\u8a08\u7b97\u91cf\u3092\u5c0e\u51fa\u3059\u308b\u65b9\u6cd5\u3001\u304a\u3088\u3073\u4e00\u822c\u7684\u306a\u30bf\u30a4\u30d7\u3068\u4f8b\u3092\u6271\u3044\u307e\u3059\u3002
        • \u30c7\u30fc\u30bf\u69cb\u9020: \u57fa\u672c\u7684\u306a\u30c7\u30fc\u30bf\u578b\u3001\u5206\u985e\u65b9\u6cd5\u3001\u5b9a\u7fa9\u3001\u9577\u6240\u3068\u77ed\u6240\u3001\u4e00\u822c\u7684\u306a\u64cd\u4f5c\u3001\u30bf\u30a4\u30d7\u3001\u5fdc\u7528\u3001\u304a\u3088\u3073\u914d\u5217\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u30b9\u30bf\u30c3\u30af\u3001\u30ad\u30e5\u30fc\u3001\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3001\u6728\u3001\u30d2\u30fc\u30d7\u3001\u30b0\u30e9\u30d5\u306a\u3069\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u5b9f\u88c5\u65b9\u6cd5\u306b\u7126\u70b9\u3092\u5f53\u3066\u307e\u3059\u3002
        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0: \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u5b9a\u7fa9\u3057\u3001\u305d\u306e\u9577\u6240\u3068\u77ed\u6240\u3001\u52b9\u7387\u6027\u3001\u5fdc\u7528\u30b7\u30ca\u30ea\u30aa\u3001\u554f\u984c\u89e3\u6c7a\u30b9\u30c6\u30c3\u30d7\u306b\u3064\u3044\u3066\u8b70\u8ad6\u3057\u3001\u691c\u7d22\u3001\u30bd\u30fc\u30c8\u3001\u5206\u5272\u7d71\u6cbb\u3001\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3001\u52d5\u7684\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3001\u8caa\u6b32\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306a\u3069\u3001\u3055\u307e\u3056\u307e\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u30b5\u30f3\u30d7\u30eb\u554f\u984c\u3092\u542b\u307f\u307e\u3059\u3002

        \u56f3 0-1 \u00a0 \u672c\u66f8\u306e\u4e3b\u306a\u5185\u5bb9

        "},{"location":"chapter_preface/about_the_book/#013","title":"0.1.3 \u00a0 \u8b1d\u8f9e","text":"

        \u672c\u66f8\u306f\u3001\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u30b3\u30df\u30e5\u30cb\u30c6\u30a3\u306e\u591a\u6570\u306e\u8ca2\u732e\u8005\u306b\u3088\u308b\u5171\u540c\u306e\u52aa\u529b\u306e\u3082\u3068\u3001\u7d99\u7d9a\u7684\u306b\u6539\u5584\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u6642\u9593\u3068\u52b4\u529b\u3092\u6ce8\u3044\u3067\u57f7\u7b46\u306b\u643a\u308f\u3063\u3066\u304f\u3060\u3055\u3063\u305f\u3059\u3079\u3066\u306e\u65b9\u3005\u306b\u611f\u8b1d\u3057\u307e\u3059\u3002\u8ca2\u732e\u8005\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059\uff08GitHub \u306b\u3088\u308a\u81ea\u52d5\u751f\u6210\u3055\u308c\u305f\u9806\u5e8f\uff09\uff1akrahets\u3001coderonion\u3001Gonglja\u3001nuomi1\u3001Reanon\u3001justin-tse\u3001hpstory\u3001danielsss\u3001curtishd\u3001night-cruise\u3001S-N-O-R-L-A-X\u3001rongyi\u3001msk397\u3001gvenusleo\u3001khoaxuantu\u3001rivertwilight\u3001K3v123\u3001gyt95\u3001zhuoqinyue\u3001yuelinxin\u3001Zuoxun\u3001mingXta\u3001Phoenix0415\u3001FangYuan33\u3001GN-Yu\u3001longsizhuo\u3001IsChristina\u3001xBLACKICEx\u3001guowei-gong\u3001Cathay-Chen\u3001pengchzn\u3001QiLOL\u3001magentaqin\u3001hello-ikun\u3001JoseHung\u3001qualifier1024\u3001thomasq0\u3001sunshinesDL\u3001L-Super\u3001Guanngxu\u3001Transmigration-zhou\u3001WSL0809\u3001Slone123c\u3001lhxsm\u3001yuan0221\u3001what-is-me\u3001Shyam-Chen\u3001theNefelibatas\u3001longranger2\u3001codeberg-user\u3001xiongsp\u3001JeffersonHuang\u3001prinpal\u3001seven1240\u3001Wonderdch\u3001malone6\u3001xiaomiusa87\u3001gaofer\u3001bluebean-cloud\u3001a16su\u3001SamJin98\u3001hongyun-robot\u3001nanlei\u3001XiaChuerwu\u3001yd-j\u3001iron-irax\u3001mgisr\u3001steventimes\u3001junminhong\u3001heshuyue\u3001danny900714\u3001MolDuM\u3001Nigh\u3001Dr-XYZ\u3001XC-Zero\u3001reeswell\u3001PXG-XPG\u3001NI-SW\u3001Horbin-Magician\u3001Enlightenus\u3001YangXuanyi\u3001beatrix-chan\u3001DullSword\u3001xjr7670\u3001jiaxianhua\u3001qq909244296\u3001iStig\u3001boloboloda\u3001hts0000\u3001gledfish\u3001wenjianmin\u3001keshida\u3001kilikilikid\u3001lclc6\u3001lwbaptx\u3001linyejoe2\u3001liuxjerry\u3001llql1211\u3001fbigm\u3001echo1937\u3001szu17dmy\u3001dshlstarr\u3001Yucao-cy\u3001coderlef\u3001czruby\u3001bongbongbakudan\u3001beintentional\u3001ZongYangL\u3001ZhongYuuu\u3001ZhongGuanbin\u3001hezhizhen\u3001linzeyan\u3001ZJKung\u3001luluxia\u3001xb534\u3001ztkuaikuai\u3001yw-1021\u3001ElaBosak233\u3001baagod\u3001zhouLion\u3001yishangzhang\u3001yi427\u3001yanedie\u3001yabo083\u3001weibk\u3001wangwang105\u3001th1nk3r-ing\u3001tao363\u30014yDX3906\u3001syd168\u3001sslmj2020\u3001smilelsb\u3001siqyka\u3001selear\u3001sdshaoda\u3001Xi-Row\u3001popozhu\u3001nuquist19\u3001noobcodemaker\u3001XiaoK29\u3001chadyi\u3001lyl625760\u3001lucaswangdev\u30010130w\u3001shanghai-Jerry\u3001EJackYang\u3001Javesun99\u3001eltociear\u3001lipusheng\u3001KNChiu\u3001BlindTerran\u3001ShiMaRing\u3001lovelock\u3001FreddieLi\u3001FloranceYeh\u3001fanchenggang\u3001gltianwen\u3001goerll\u3001nedchu\u3001curly210102\u3001CuB3y0nd\u3001KraHsu\u3001CarrotDLaw\u3001youshaoXG\u3001bubble9um\u3001Asashishi\u3001Asa0oo0o0o\u3001fanenr\u3001eagleanurag\u3001akshiterate\u300152coder\u3001foursevenlove\u3001KorsChen\u3001GaochaoZhu\u3001hopkings2008\u3001yang-le\u3001realwujing\u3001Evilrabbit520\u3001Umer-Jahangir\u3001Turing-1024-Lee\u3001Suremotoo\u3001paoxiaomooo\u3001Chieko-Seren\u3001Allen-Scai\u3001ymmmas\u3001Risuntsy\u3001Richard-Zhang1019\u3001RafaelCaso\u3001qingpeng9802\u3001primexiao\u3001Urbaner3\u3001zhongfq\u3001nidhoggfgg\u3001MwumLi\u3001CreatorMetaSky\u3001martinx\u3001ZnYang2018\u3001hugtyftg\u3001logan-qiu\u3001psychelzh\u3001Keynman\u3001KeiichiKasai\u3001KawaiiAsh\u3002

        \u3053\u306e\u672c\u306e\u30b3\u30fc\u30c9\u30ec\u30d3\u30e5\u30fc\u4f5c\u696d\u306f\u3001coderonion, Gonglja, gvenusleo, hpstory, justin\u2010tse, khoaxuantu, krahets, night-cruise, nuomi1, Reanon and rongyi\uff08\u30a2\u30eb\u30d5\u30a1\u30d9\u30c3\u30c8\u9806\uff09\u306b\u3088\u3063\u3066\u5b8c\u4e86\u3055\u308c\u307e\u3057\u305f\u3002\u5f7c\u3089\u306e\u6642\u9593\u3068\u52aa\u529b\u306b\u611f\u8b1d\u3057\u3001\u69d8\u3005\u306a\u8a00\u8a9e\u3067\u306e\u30b3\u30fc\u30c9\u306e\u6a19\u6e96\u5316\u3068\u7d71\u4e00\u6027\u3092\u78ba\u4fdd\u3057\u3066\u304f\u3060\u3055\u3044\u307e\u3057\u305f\u3002

        \u672c\u66f8\u306e\u7e41\u4f53\u5b57\u4e2d\u56fd\u8a9e\u7248\u306f Shyam-Chen \u304a\u3088\u3073 Dr-XYZ \u306b\u3088\u3063\u3066\u30ec\u30d3\u30e5\u30fc\u3055\u308c\u3001\u82f1\u8a9e\u7248\u306f yuelinxin\u3001K3v123\u3001QiLOL\u3001Phoenix0415\u3001SamJin98\u3001yanedie\u3001RafaelCaso\u3001pengchzn\u3001thomasq0\u3001magentaqin \u306b\u3088\u3063\u3066\u30ec\u30d3\u30e5\u30fc\u3055\u308c\u3001\u65e5\u672c\u8a9e\u7248\u306f eltociear \u306b\u3088\u3063\u3066\u30ec\u30d3\u30e5\u30fc\u3055\u308c\u307e\u3057\u305f\u3002\u5f7c\u3089\u306e\u7d99\u7d9a\u7684\u306a\u8ca2\u732e\u306e\u304a\u304b\u3052\u3067\u3001\u672c\u66f8\u306f\u3088\u308a\u5e45\u5e83\u3044\u8aad\u8005\u5c64\u306b\u63d0\u4f9b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u3066\u3044\u307e\u3059\u3002\u3053\u3053\u306b\u6df1\u304f\u611f\u8b1d\u3044\u305f\u3057\u307e\u3059\u3002

        \u3053\u306e\u672c\u306e\u5236\u4f5c\u904e\u7a0b\u306b\u304a\u3044\u3066\u3001\u591a\u304f\u306e\u65b9\u3005\u304b\u3089\u8cb4\u91cd\u306a\u652f\u63f4\u3092\u3044\u305f\u3060\u304d\u307e\u3057\u305f\u3002\u3053\u308c\u3089\u306b\u9650\u5b9a\u3055\u308c\u307e\u305b\u3093\u304c\uff1a

        • \u4f1a\u793e\u3067\u306e\u30e1\u30f3\u30bf\u30fc\u3001\u674e\u7199\u535a\u58eb\u306b\u611f\u8b1d\u3057\u307e\u3059\u3002\u3042\u308b\u4f1a\u8a71\u3067\u300c\u65e9\u304f\u884c\u52d5\u3057\u308d\u300d\u3068\u52b1\u307e\u3057\u3066\u3044\u305f\u3060\u304d\u3001\u3053\u306e\u672c\u3092\u66f8\u304f\u6c7a\u610f\u3092\u56fa\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002
        • \u30ac\u30fc\u30eb\u30d5\u30ec\u30f3\u30c9\u306eBubble\u306b\u611f\u8b1d\u3057\u307e\u3059\u3002\u3053\u306e\u672c\u306e\u6700\u521d\u306e\u8aad\u8005\u3068\u3057\u3066\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u521d\u5fc3\u8005\u306e\u8996\u70b9\u304b\u3089\u591a\u304f\u306e\u8cb4\u91cd\u306a\u63d0\u6848\u3092\u3044\u305f\u3060\u304d\u3001\u3053\u306e\u672c\u3092\u521d\u5fc3\u8005\u306b\u3088\u308a\u9069\u3057\u305f\u3082\u306e\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u307e\u3057\u305f\u3002
        • Tengbao\u3001Qibao\u3001Feibao\u306b\u611f\u8b1d\u3057\u307e\u3059\u3002\u3053\u306e\u672c\u306e\u30af\u30ea\u30a8\u30a4\u30c6\u30a3\u30d6\u306a\u540d\u524d\u3092\u8003\u3048\u3066\u304f\u3060\u3055\u308a\u3001\u307f\u3093\u306a\u304c\u521d\u3081\u3066\u300cHello World!\u300d\u3092\u66f8\u3044\u305f\u6642\u306e\u7d20\u6674\u3089\u3057\u3044\u601d\u3044\u51fa\u3092\u547c\u3073\u8d77\u3053\u3057\u3066\u304f\u308c\u307e\u3057\u305f\u3002
        • Xiaoquan\u306b\u611f\u8b1d\u3057\u307e\u3059\u3002\u77e5\u7684\u8ca1\u7523\u306b\u95a2\u3059\u308b\u5c02\u9580\u7684\u306a\u652f\u63f4\u3092\u63d0\u4f9b\u3057\u3066\u304f\u3060\u3055\u308a\u3001\u3053\u306e\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u672c\u306e\u958b\u767a\u306b\u304a\u3044\u3066\u91cd\u8981\u306a\u5f79\u5272\u3092\u679c\u305f\u3057\u3066\u304f\u3060\u3055\u3044\u307e\u3057\u305f\u3002
        • Sutong\u306b\u611f\u8b1d\u3057\u307e\u3059\u3002\u3053\u306e\u672c\u306e\u7f8e\u3057\u3044\u30ab\u30d0\u30fc\u3068\u30ed\u30b4\u3092\u30c7\u30b6\u30a4\u30f3\u3057\u3066\u304f\u3060\u3055\u308a\u3001\u79c1\u306e\u8981\u6c42\u3067\u4f55\u5ea6\u3082\u4fee\u6b63\u3092\u8f9b\u62b1\u5f37\u304f\u884c\u3063\u3066\u304f\u3060\u3055\u3044\u307e\u3057\u305f\u3002
        • @squidfunk \u306b\u611f\u8b1d\u3057\u307e\u3059\u3002\u57f7\u7b46\u3068\u7d44\u7248\u306e\u63d0\u6848\u3001\u304a\u3088\u3073\u5f7c\u304c\u958b\u767a\u3057\u305f\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u30c6\u30fc\u30de Material-for-MkDocs \u3092\u63d0\u4f9b\u3057\u3066\u304f\u3060\u3055\u3044\u307e\u3057\u305f\u3002

        \u57f7\u7b46\u306e\u904e\u7a0b\u3067\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u95a2\u3059\u308b\u591a\u6570\u306e\u6559\u79d1\u66f8\u3084\u8a18\u4e8b\u3092\u6df1\u304f\u7814\u7a76\u3057\u307e\u3057\u305f\u3002\u3053\u308c\u3089\u306e\u4f5c\u54c1\u306f\u6a21\u7bc4\u7684\u306a\u30e2\u30c7\u30eb\u3068\u3057\u3066\u6a5f\u80fd\u3057\u3001\u3053\u306e\u672c\u306e\u5185\u5bb9\u306e\u6b63\u78ba\u6027\u3068\u54c1\u8cea\u3092\u78ba\u4fdd\u3057\u3066\u304f\u3060\u3055\u3044\u307e\u3057\u305f\u3002\u5148\u4eba\u306e\u65b9\u3005\u306e\u8cb4\u91cd\u306a\u8ca2\u732e\u306b\u611f\u8b1d\u3044\u305f\u3057\u307e\u3059\uff01

        \u3053\u306e\u672c\u306f\u3001\u7406\u8ad6\u3068\u5b9f\u8df5\u3092\u7d44\u307f\u5408\u308f\u305b\u305f\u5b66\u7fd2\u3092\u63d0\u5531\u3057\u3066\u304a\u308a\u3001\u3053\u306e\u70b9\u3067 \"Dive into Deep Learning\" \u304b\u3089\u30a4\u30f3\u30b9\u30d4\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u53d7\u3051\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u512a\u308c\u305f\u672c\u3092\u3059\u3079\u3066\u306e\u8aad\u8005\u306b\u5f37\u304f\u304a\u52e7\u3081\u3057\u307e\u3059\u3002

        \u7d99\u7d9a\u7684\u306a\u652f\u63f4\u3068\u52b1\u307e\u3057\u306b\u3088\u308a\u3001\u3053\u306e\u8208\u5473\u6df1\u3044\u4ed5\u4e8b\u3092\u3059\u308b\u3053\u3068\u3092\u53ef\u80fd\u306b\u3057\u3066\u304f\u3060\u3055\u3063\u305f\u4e21\u89aa\u306b\u5fc3\u304b\u3089\u611f\u8b1d\u3044\u305f\u3057\u307e\u3059\u3002

        "},{"location":"chapter_preface/suggestions/","title":"0.2 \u00a0 \u8aad\u307f\u65b9","text":"

        Tip

        \u6700\u826f\u306e\u8aad\u66f8\u4f53\u9a13\u306e\u305f\u3081\u306b\u3001\u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3092\u901a\u8aad\u3059\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002

        "},{"location":"chapter_preface/suggestions/#021","title":"0.2.1 \u00a0 \u8a18\u8ff0\u898f\u5247","text":"
        • \u30bf\u30a4\u30c8\u30eb\u306e\u5f8c\u306b\u300c*\u300d\u304c\u4ed8\u3044\u305f\u7ae0\u306f\u4efb\u610f\u3067\u3042\u308a\u3001\u6bd4\u8f03\u7684\u96e3\u6613\u5ea6\u306e\u9ad8\u3044\u5185\u5bb9\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002\u6642\u9593\u306b\u5236\u7d04\u304c\u3042\u308b\u5834\u5408\u306f\u3001\u3053\u308c\u3089\u3092\u30b9\u30ad\u30c3\u30d7\u3059\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002
        • \u6280\u8853\u7528\u8a9e\u306f\u592a\u5b57\uff08\u5370\u5237\u7248\u304a\u3088\u3073PDF\u7248\uff09\u307e\u305f\u306f\u4e0b\u7dda\uff08Web\u7248\uff09\u3067\u8868\u793a\u3055\u308c\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u914d\u5217\u306a\u3069\u3067\u3059\u3002\u6280\u8853\u6587\u66f8\u3092\u3088\u308a\u826f\u304f\u7406\u89e3\u3059\u308b\u305f\u3081\u306b\u3001\u3053\u308c\u3089\u306b\u6163\u308c\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002
        • **\u592a\u5b57\u306e\u30c6\u30ad\u30b9\u30c8**\u306f\u91cd\u8981\u306a\u5185\u5bb9\u3084\u8981\u7d04\u6587\u3092\u793a\u3057\u3001\u7279\u5225\u306a\u6ce8\u610f\u3092\u6255\u3046\u4fa1\u5024\u304c\u3042\u308a\u307e\u3059\u3002
        • \u7279\u5b9a\u306e\u610f\u5473\u3092\u6301\u3064\u5358\u8a9e\u3084\u53e5\u306f\u300c\u5f15\u7528\u7b26\u300d\u3067\u793a\u3055\u308c\u3001\u66d6\u6627\u3055\u3092\u907f\u3051\u307e\u3059\u3002
        • \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u9593\u3067\u4e00\u81f4\u3057\u306a\u3044\u7528\u8a9e\u306b\u3064\u3044\u3066\u306f\u3001\u3053\u306e\u672c\u306fPython\u306b\u5f93\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001null\u3092\u610f\u5473\u3059\u308b\u305f\u3081\u306bNone\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u3053\u306e\u672c\u306f\u3001\u3088\u308a\u30b3\u30f3\u30d1\u30af\u30c8\u306a\u30b3\u30f3\u30c6\u30f3\u30c4\u30ec\u30a4\u30a2\u30a6\u30c8\u3068\u5f15\u304d\u63db\u3048\u306b\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u30b3\u30e1\u30f3\u30c8\u898f\u7d04\u3092\u90e8\u5206\u7684\u306b\u7121\u8996\u3057\u3066\u3044\u307e\u3059\u3002\u30b3\u30e1\u30f3\u30c8\u306f\u4e3b\u306b3\u3064\u306e\u30bf\u30a4\u30d7\u3067\u69cb\u6210\u3055\u308c\u3066\u3044\u307e\u3059\uff1a\u30bf\u30a4\u30c8\u30eb\u30b3\u30e1\u30f3\u30c8\u3001\u5185\u5bb9\u30b3\u30e1\u30f3\u30c8\u3001\u8907\u6570\u884c\u30b3\u30e1\u30f3\u30c8\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin
        \"\"\"\u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8\"\"\"\n\n# \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n\"\"\"\n\u8907\u6570\u884c\n\u30b3\u30e1\u30f3\u30c8\n\"\"\"\n
        /* \u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8 */\n\n// \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n/**\n * \u8907\u6570\u884c\n * \u30b3\u30e1\u30f3\u30c8\n */\n
        /* \u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8 */\n\n// \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n/**\n * \u8907\u6570\u884c\n * \u30b3\u30e1\u30f3\u30c8\n */\n
        /* \u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8 */\n\n// \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n/**\n * \u8907\u6570\u884c\n * \u30b3\u30e1\u30f3\u30c8\n */\n
        /* \u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8 */\n\n// \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n/**\n * \u8907\u6570\u884c\n * \u30b3\u30e1\u30f3\u30c8\n */\n
        /* \u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8 */\n\n// \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n/**\n * \u8907\u6570\u884c\n * \u30b3\u30e1\u30f3\u30c8\n */\n
        /* \u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8 */\n\n// \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n/**\n * \u8907\u6570\u884c\n * \u30b3\u30e1\u30f3\u30c8\n */\n
        /* \u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8 */\n\n// \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n/**\n * \u8907\u6570\u884c\n * \u30b3\u30e1\u30f3\u30c8\n */\n
        /* \u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8 */\n\n// \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n/**\n * \u8907\u6570\u884c\n * \u30b3\u30e1\u30f3\u30c8\n */\n
        /* \u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8 */\n\n// \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n/**\n * \u8907\u6570\u884c\n * \u30b3\u30e1\u30f3\u30c8\n */\n
        /* \u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8 */\n\n// \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n/**\n * \u8907\u6570\u884c\n * \u30b3\u30e1\u30f3\u30c8\n */\n
        /* \u95a2\u6570\u3001\u30af\u30e9\u30b9\u3001\u30c6\u30b9\u30c8\u30b5\u30f3\u30d7\u30eb\u306a\u3069\u3092\u30e9\u30d9\u30eb\u4ed8\u3051\u3059\u308b\u305f\u3081\u306e\u30d8\u30c3\u30c0\u30fc\u30b3\u30e1\u30f3\u30c8 */\n\n// \u8a73\u7d30\u3092\u8aac\u660e\u3059\u308b\u305f\u3081\u306e\u30b3\u30e1\u30f3\u30c8\n\n/**\n * \u8907\u6570\u884c\n * \u30b3\u30e1\u30f3\u30c8\n */\n
        "},{"location":"chapter_preface/suggestions/#022","title":"0.2.2 \u00a0 \u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u56f3\u89e3\u306b\u3088\u308b\u52b9\u7387\u7684\u5b66\u7fd2","text":"

        \u30c6\u30ad\u30b9\u30c8\u3068\u6bd4\u8f03\u3057\u3066\u3001\u52d5\u753b\u3084\u753b\u50cf\u306f\u60c5\u5831\u5bc6\u5ea6\u304c\u9ad8\u304f\u3001\u3088\u308a\u69cb\u9020\u5316\u3055\u308c\u3066\u304a\u308a\u3001\u7406\u89e3\u3057\u3084\u3059\u304f\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u672c\u3067\u306f\u3001\u91cd\u8981\u3067\u96e3\u3057\u3044\u6982\u5ff5\u306f\u4e3b\u306b\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3068\u56f3\u89e3\u3092\u901a\u3058\u3066\u63d0\u793a\u3055\u308c\u3001\u30c6\u30ad\u30b9\u30c8\u306f\u8aac\u660e\u3068\u88dc\u8db3\u3068\u3057\u3066\u6a5f\u80fd\u3057\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306a\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3084\u56f3\u89e3\u306e\u3042\u308b\u5185\u5bb9\u306b\u906d\u9047\u3057\u305f\u5834\u5408\u3001\u56f3\u306e\u7406\u89e3\u3092\u512a\u5148\u3057\u3001\u30c6\u30ad\u30b9\u30c8\u3092\u88dc\u8db3\u3068\u3057\u3066\u3001\u4e21\u65b9\u3092\u7d71\u5408\u3057\u3066\u5305\u62ec\u7684\u306a\u7406\u89e3\u3092\u5f97\u3066\u304f\u3060\u3055\u3044\u3002

        \u56f3 0-2 \u00a0 \u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u56f3\u89e3\u306e\u4f8b

        "},{"location":"chapter_preface/suggestions/#023","title":"0.2.3 \u00a0 \u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u5b9f\u8df5\u306b\u3088\u308b\u7406\u89e3\u306e\u6df1\u5316","text":"

        \u3053\u306e\u672c\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306fGitHub\u30ea\u30dd\u30b8\u30c8\u30ea\u3067\u30db\u30b9\u30c8\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306b\u306f\u30c6\u30b9\u30c8\u4f8b\u304c\u4ed8\u5c5e\u3057\u3066\u304a\u308a\u3001\u30ef\u30f3\u30af\u30ea\u30c3\u30af\u3067\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002

        \u6642\u9593\u306b\u4f59\u88d5\u304c\u3042\u308b\u5834\u5408\u306f\u3001\u81ea\u5206\u3067\u30b3\u30fc\u30c9\u3092\u30bf\u30a4\u30d7\u3059\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002\u6642\u9593\u304c\u306a\u3044\u5834\u5408\u306f\u3001\u5c11\u306a\u304f\u3068\u3082\u3059\u3079\u3066\u306e\u30b3\u30fc\u30c9\u3092\u8aad\u3093\u3067\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u30b3\u30fc\u30c9\u3092\u8aad\u3080\u3060\u3051\u3068\u6bd4\u8f03\u3057\u3066\u3001\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3053\u3068\u306f\u591a\u304f\u306e\u5834\u5408\u3001\u3088\u308a\u591a\u304f\u306e\u5b66\u7fd2\u3092\u3082\u305f\u3089\u3057\u307e\u3059\u3002\u5b9f\u8df5\u306b\u3088\u308b\u5b66\u7fd2\u3053\u305d\u304c\u771f\u306e\u5b66\u7fd2\u65b9\u6cd5\u3067\u3059\u3002

        \u56f3 0-3 \u00a0 \u30b3\u30fc\u30c9\u5b9f\u884c\u4f8b

        \u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u305f\u3081\u306e\u8a2d\u5b9a\u306b\u306f\u3001\u4e3b\u306b3\u3064\u306e\u30b9\u30c6\u30c3\u30d7\u304c\u542b\u307e\u308c\u307e\u3059\u3002

        \u30b9\u30c6\u30c3\u30d71\uff1a\u30ed\u30fc\u30ab\u30eb\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u74b0\u5883\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3002\u4ed8\u9332\u306e\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u306b\u5f93\u3063\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u304b\u3001\u3059\u3067\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u306f\u3053\u306e\u30b9\u30c6\u30c3\u30d7\u3092\u30b9\u30ad\u30c3\u30d7\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u30b9\u30c6\u30c3\u30d72\uff1a\u30b3\u30fc\u30c9\u30ea\u30dd\u30b8\u30c8\u30ea\u3092\u30af\u30ed\u30fc\u30f3\u307e\u305f\u306f\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3002GitHub\u30ea\u30dd\u30b8\u30c8\u30ea\u3092\u8a2a\u554f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        Git\u304c\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u6b21\u306e\u30b3\u30de\u30f3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u30ea\u30dd\u30b8\u30c8\u30ea\u3092\u30af\u30ed\u30fc\u30f3\u3057\u307e\u3059\uff1a

        git clone https://github.com/krahets/hello-algo.git\n

        \u307e\u305f\u306f\u3001\u4e0b\u56f3\u306b\u793a\u3059\u5834\u6240\u306b\u3042\u308b\u300cDownload ZIP\u300d\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3057\u3066\u3001\u30b3\u30fc\u30c9\u3092\u5727\u7e2eZIP\u30d5\u30a1\u30a4\u30eb\u3068\u3057\u3066\u76f4\u63a5\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002\u305d\u306e\u5f8c\u3001\u30ed\u30fc\u30ab\u30eb\u3067\u5c55\u958b\u3059\u308b\u3060\u3051\u3067\u3059\u3002

        \u56f3 0-4 \u00a0 \u30ea\u30dd\u30b8\u30c8\u30ea\u306e\u30af\u30ed\u30fc\u30f3\u3068\u30b3\u30fc\u30c9\u306e\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9

        \u30b9\u30c6\u30c3\u30d73\uff1a\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u4e0a\u90e8\u306b\u30d5\u30a1\u30a4\u30eb\u540d\u304c\u8a18\u8f09\u3055\u308c\u305f\u30b3\u30fc\u30c9\u30d6\u30ed\u30c3\u30af\u306b\u3064\u3044\u3066\u306f\u3001\u30ea\u30dd\u30b8\u30c8\u30ea\u306ecodes\u30d5\u30a9\u30eb\u30c0\u3067\u5bfe\u5fdc\u3059\u308b\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u30d5\u30a1\u30a4\u30eb\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u30d5\u30a1\u30a4\u30eb\u306f\u30ef\u30f3\u30af\u30ea\u30c3\u30af\u3067\u5b9f\u884c\u3067\u304d\u3001\u4e0d\u8981\u306a\u30c7\u30d0\u30c3\u30b0\u6642\u9593\u3092\u7bc0\u7d04\u3057\u3001\u5b66\u7fd2\u306b\u96c6\u4e2d\u3067\u304d\u307e\u3059\u3002

        \u56f3 0-5 \u00a0 \u30b3\u30fc\u30c9\u30d6\u30ed\u30c3\u30af\u3068\u5bfe\u5fdc\u3059\u308b\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u30d5\u30a1\u30a4\u30eb

        "},{"location":"chapter_preface/suggestions/#024","title":"0.2.4 \u00a0 \u8b70\u8ad6\u306b\u3088\u308b\u5171\u540c\u5b66\u7fd2","text":"

        \u3053\u306e\u672c\u3092\u8aad\u3093\u3067\u3044\u308b\u9593\u3001\u5b66\u3079\u306a\u304b\u3063\u305f\u70b9\u3092\u98db\u3070\u3055\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002\u30b3\u30e1\u30f3\u30c8\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u6c17\u8efd\u306b\u8cea\u554f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u559c\u3093\u3067\u304a\u7b54\u3048\u3057\u3001\u901a\u5e382\u65e5\u4ee5\u5185\u306b\u56de\u7b54\u3067\u304d\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5404\u7ae0\u306e\u4e0b\u90e8\u306b\u30b3\u30e1\u30f3\u30c8\u30bb\u30af\u30b7\u30e7\u30f3\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u30b3\u30e1\u30f3\u30c8\u306b\u6ce8\u610f\u3092\u6255\u3046\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002\u4ed6\u306e\u4eba\u304c\u906d\u9047\u3057\u305f\u554f\u984c\u3092\u77e5\u308b\u3053\u3068\u3067\u3001\u77e5\u8b58\u306e\u30ae\u30e3\u30c3\u30d7\u3092\u7279\u5b9a\u3057\u3001\u3088\u308a\u6df1\u3044\u601d\u7d22\u3092\u4fc3\u3059\u3060\u3051\u3067\u306a\u304f\u3001\u4ef2\u9593\u306e\u8aad\u8005\u306e\u8cea\u554f\u306b\u7b54\u3048\u305f\u308a\u3001\u6d1e\u5bdf\u3092\u5171\u6709\u3057\u305f\u308a\u3001\u76f8\u4e92\u306e\u5411\u4e0a\u3092\u4fc3\u9032\u3057\u305f\u308a\u3059\u308b\u3053\u3068\u3067\u5bdb\u5927\u306b\u8ca2\u732e\u3059\u308b\u3053\u3068\u3082\u62db\u5f85\u3057\u307e\u3059\u3002

        \u56f3 0-6 \u00a0 \u30b3\u30e1\u30f3\u30c8\u30bb\u30af\u30b7\u30e7\u30f3\u306e\u4f8b

        "},{"location":"chapter_preface/suggestions/#025","title":"0.2.5 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u5b66\u7fd2\u30d1\u30b9","text":"

        \u5168\u4f53\u7684\u306b\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u30de\u30b9\u30bf\u30fc\u3059\u308b\u65c5\u306f3\u3064\u306e\u6bb5\u968e\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a

        1. \u6bb5\u968e1\uff1a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5165\u9580\u3002\u3055\u307e\u3056\u307e\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u7279\u6027\u3068\u4f7f\u7528\u6cd5\u306b\u6163\u308c\u3001\u7570\u306a\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u539f\u7406\u3001\u30d7\u30ed\u30bb\u30b9\u3001\u7528\u9014\u3001\u52b9\u7387\u306b\u3064\u3044\u3066\u5b66\u3076\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        2. \u6bb5\u968e2\uff1a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u306e\u7df4\u7fd2\u3002Sword for Offer\u3084LeetCode Hot 100\u306a\u3069\u306e\u4eba\u6c17\u306e\u3042\u308b\u554f\u984c\u304b\u3089\u59cb\u3081\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u3001\u5c11\u306a\u304f\u3068\u3082100\u554f\u3092\u84c4\u7a4d\u3057\u3066\u4e3b\u6d41\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u306b\u6163\u308c\u308b\u3053\u3068\u3067\u3059\u3002\u7df4\u7fd2\u3092\u59cb\u3081\u308b\u3068\u5fd8\u5374\u304c\u8ab2\u984c\u306b\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u3053\u308c\u306f\u6b63\u5e38\u306a\u3053\u3068\u3067\u3059\u306e\u3067\u3054\u5b89\u5fc3\u304f\u3060\u3055\u3044\u3002\u300c\u30a8\u30d3\u30f3\u30b0\u30cf\u30a6\u30b9\u306e\u5fd8\u5374\u66f2\u7dda\u300d\u306b\u5f93\u3063\u3066\u554f\u984c\u3092\u5fa9\u7fd2\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u901a\u5e383\u301c5\u56de\u306e\u53cd\u5fa9\u306e\u5f8c\u3001\u305d\u308c\u3089\u3092\u899a\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3067\u3057\u3087\u3046\u3002
        3. \u6bb5\u968e3\uff1a\u77e5\u8b58\u4f53\u7cfb\u306e\u69cb\u7bc9\u3002\u5b66\u7fd2\u306e\u9762\u3067\u306f\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30b3\u30e9\u30e0\u8a18\u4e8b\u3001\u89e3\u6cd5\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u6559\u79d1\u66f8\u3092\u8aad\u3093\u3067\u77e5\u8b58\u4f53\u7cfb\u3092\u7d99\u7d9a\u7684\u306b\u8c4a\u304b\u306b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u7df4\u7fd2\u306e\u9762\u3067\u306f\u3001\u30c8\u30d4\u30c3\u30af\u5225\u5206\u985e\u3001\u4e00\u3064\u306e\u554f\u984c\u306b\u5bfe\u3059\u308b\u8907\u6570\u306e\u89e3\u6cd5\u3001\u8907\u6570\u306e\u554f\u984c\u306b\u5bfe\u3059\u308b\u4e00\u3064\u306e\u89e3\u6cd5\u306a\u3069\u3001\u9ad8\u5ea6\u306a\u6226\u7565\u3092\u8a66\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u6226\u7565\u306b\u95a2\u3059\u308b\u6d1e\u5bdf\u306f\u3001\u3055\u307e\u3056\u307e\u306a\u30b3\u30df\u30e5\u30cb\u30c6\u30a3\u3067\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u3053\u306e\u672c\u306f\u4e3b\u306b\u300c\u6bb5\u968e1\u300d\u3092\u30ab\u30d0\u30fc\u3057\u3066\u304a\u308a\u3001\u6bb5\u968e2\u30683\u306b\u3088\u308a\u52b9\u7387\u7684\u306b\u53d6\u308a\u7d44\u3080\u306e\u306b\u5f79\u7acb\u3064\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 0-7 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u5b66\u7fd2\u30d1\u30b9

        "},{"location":"chapter_preface/summary/","title":"0.3 \u00a0 \u307e\u3068\u3081","text":"
        • \u3053\u306e\u672c\u306e\u4e3b\u306a\u8aad\u8005\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u521d\u5fc3\u8005\u3067\u3059\u3002\u3059\u3067\u306b\u57fa\u672c\u7684\u306a\u77e5\u8b58\u3092\u304a\u6301\u3061\u306e\u5834\u5408\u3001\u3053\u306e\u672c\u306f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u77e5\u8b58\u3092\u4f53\u7cfb\u7684\u306b\u5fa9\u7fd2\u3059\u308b\u306e\u306b\u5f79\u7acb\u3061\u3001\u3053\u306e\u672c\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306f\u300c\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u30c4\u30fc\u30eb\u30ad\u30c3\u30c8\u300d\u3068\u3057\u3066\u3082\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002
        • \u3053\u306e\u672c\u306f3\u3064\u306e\u4e3b\u8981\u306a\u30bb\u30af\u30b7\u30e7\u30f3\u3001\u8a08\u7b97\u91cf\u89e3\u6790\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u69cb\u6210\u3055\u308c\u3066\u304a\u308a\u3001\u3053\u306e\u5206\u91ce\u306e\u307b\u3068\u3093\u3069\u306e\u30c8\u30d4\u30c3\u30af\u3092\u30ab\u30d0\u30fc\u3057\u3066\u3044\u307e\u3059\u3002
        • \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u521d\u5fc3\u8005\u306b\u3068\u3063\u3066\u3001\u591a\u304f\u306e\u56de\u308a\u9053\u3084\u4e00\u822c\u7684\u306a\u843d\u3068\u3057\u7a74\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u3001\u521d\u671f\u6bb5\u968e\u3067\u5165\u9580\u66f8\u3092\u8aad\u3080\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002
        • \u672c\u66f8\u5185\u306e\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3068\u56f3\u306f\u901a\u5e38\u3001\u91cd\u8981\u306a\u30dd\u30a4\u30f3\u30c8\u3068\u96e3\u3057\u3044\u77e5\u8b58\u3092\u7d39\u4ecb\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u672c\u3092\u8aad\u3080\u969b\u306b\u306f\u3053\u308c\u3089\u306b\u3088\u308a\u591a\u304f\u306e\u6ce8\u610f\u3092\u6255\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u5b9f\u8df5\u306f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3092\u5b66\u3076\u6700\u826f\u306e\u65b9\u6cd5\u3067\u3059\u3002\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3057\u3001\u81ea\u5206\u3067\u30b3\u30fc\u30c9\u3092\u30bf\u30a4\u30d7\u3059\u308b\u3053\u3068\u3092\u5f37\u304f\u304a\u52e7\u3081\u3057\u307e\u3059\u3002
        • \u3053\u306e\u672c\u306eWeb\u7248\u306e\u5404\u7ae0\u306b\u306f\u8b70\u8ad6\u30bb\u30af\u30b7\u30e7\u30f3\u304c\u3042\u308a\u3001\u3044\u3064\u3067\u3082\u8cea\u554f\u3084\u6d1e\u5bdf\u3092\u5171\u6709\u3059\u308b\u3053\u3068\u3092\u6b53\u8fce\u3057\u307e\u3059\u3002
        "},{"location":"chapter_reference/","title":"\u53c2\u8003\u6587\u732e","text":"

        [1] Thomas H. Cormen, et al. Introduction to Algorithms (3rd Edition).

        [2] Aditya Bhargava. Grokking Algorithms: An Illustrated Guide for Programmers and Other Curious People (1st Edition).

        [3] Robert Sedgewick, et al. Algorithms (4th Edition).

        [4] Yan Weimin. Data Structures (C Language Version).

        [5] Deng Junhui. Data Structures (C++ Language Version, Third Edition).

        [6] Mark Allen Weiss, translated by Chen Yue. Data Structures and Algorithm Analysis in Java (Third Edition).

        [7] Cheng Jie. Speaking of Data Structures.

        [8] Wang Zheng. The Beauty of Data Structures and Algorithms.

        [9] Gayle Laakmann McDowell. Cracking the Coding Interview: 189 Programming Questions and Solutions (6th Edition).

        [10] Aston Zhang, et al. Dive into Deep Learning.

        "},{"location":"chapter_searching/","title":"\u7b2c 10 \u7ae0 \u00a0 \u63a2\u7d22","text":"

        Abstract

        \u63a2\u7d22\u306f\u672a\u77e5\u3078\u306e\u5192\u967a\u3067\u3059\u3002\u795e\u79d8\u7684\u306a\u7a7a\u9593\u306e\u9685\u3005\u307e\u3067\u5de1\u308b\u5fc5\u8981\u304c\u3042\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3057\u3001\u3042\u308b\u3044\u306f\u3059\u3050\u306b\u76ee\u6a19\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002

        \u3053\u306e\u767a\u898b\u306e\u65c5\u306b\u304a\u3044\u3066\u3001\u305d\u308c\u305e\u308c\u306e\u63a2\u67fb\u306f\u4e88\u671f\u3057\u306a\u3044\u7b54\u3048\u3067\u7d42\u308f\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002

        "},{"location":"chapter_searching/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 10.1 \u00a0 \u4e8c\u5206\u63a2\u7d22
        • 10.2 \u00a0 \u4e8c\u5206\u63a2\u7d22\u633f\u5165\u70b9
        • 10.3 \u00a0 \u4e8c\u5206\u63a2\u7d22\u306e\u5883\u754c
        • 10.4 \u00a0 \u30cf\u30c3\u30b7\u30e5\u6700\u9069\u5316\u6226\u7565
        • 10.5 \u00a0 \u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u518d\u8003
        • 10.6 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_searching/binary_search/","title":"10.1 \u00a0 \u4e8c\u5206\u63a2\u7d22","text":"

        \u4e8c\u5206\u63a2\u7d22\u306f\u5206\u5272\u7d71\u6cbb\u6226\u7565\u3092\u7528\u3044\u308b\u52b9\u7387\u7684\u306a\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3059\u3002\u914d\u5217\u5185\u306e\u8981\u7d20\u306e\u6574\u5217\u9806\u5e8f\u3092\u5229\u7528\u3057\u3001\u5404\u53cd\u5fa9\u3067\u63a2\u7d22\u533a\u9593\u3092\u534a\u5206\u306b\u6e1b\u3089\u3057\u306a\u304c\u3089\u3001\u76ee\u6a19\u8981\u7d20\u304c\u898b\u3064\u304b\u308b\u304b\u63a2\u7d22\u533a\u9593\u304c\u7a7a\u306b\u306a\u308b\u307e\u3067\u7d9a\u884c\u3057\u307e\u3059\u3002

        Question

        \u9577\u3055\\(n\\)\u306e\u914d\u5217nums\u304c\u4e0e\u3048\u3089\u308c\u3001\u8981\u7d20\u306f\u91cd\u8907\u306a\u3057\u3067\u6607\u9806\u306b\u914d\u5217\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u914d\u5217\u5185\u306e\u8981\u7d20target\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u898b\u3064\u3051\u3066\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u914d\u5217\u306b\u8981\u7d20\u304c\u542b\u307e\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u306f\\(-1\\)\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4f8b\u3092\u4e0b\u56f3\u306b\u793a\u3057\u307e\u3059\u3002

        \u56f3 10-1 \u00a0 Binary search example data

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u307e\u305a\\(i = 0\\)\u3068\\(j = n - 1\\)\u3067\u30dd\u30a4\u30f3\u30bf\u3092\u521d\u671f\u5316\u3057\u3001\u305d\u308c\u305e\u308c\u914d\u5217\u306e\u6700\u521d\u3068\u6700\u5f8c\u306e\u8981\u7d20\u3092\u6307\u3057\u307e\u3059\u3002\u3053\u308c\u3089\u306f\u307e\u305f\u5168\u4f53\u306e\u63a2\u7d22\u533a\u9593\\([0, n - 1]\\)\u3092\u8868\u3057\u307e\u3059\u3002\u89d2\u62ec\u5f27\u306f\u9589\u533a\u9593\u3092\u793a\u3057\u3001\u5883\u754c\u5024\u81ea\u8eab\u3082\u542b\u3080\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u305d\u3057\u3066\u3001\u4ee5\u4e0b\u306e2\u3064\u306e\u30b9\u30c6\u30c3\u30d7\u3092\u30eb\u30fc\u30d7\u3067\u5b9f\u884c\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        1. \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(m = \\lfloor {(i + j) / 2} \\rfloor\\)\u3092\u8a08\u7b97\u3057\u307e\u3059\u3002\u3053\u3053\u3067\\(\\lfloor \\: \\rfloor\\)\u306f\u5e8a\u95a2\u6570\u3092\u8868\u3057\u307e\u3059\u3002
        2. nums[m]\u3068target\u306e\u6bd4\u8f03\u306b\u57fa\u3065\u3044\u3066\u3001\u4ee5\u4e0b\u306e3\u3064\u306e\u30b1\u30fc\u30b9\u306e\u3046\u30611\u3064\u3092\u9078\u629e\u3057\u3066\u5b9f\u884c\u3057\u307e\u3059\u3002
          1. nums[m] < target\u306e\u5834\u5408\u3001target\u306f\u533a\u9593\\([m + 1, j]\\)\u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\u305f\u3081\u3001\\(i = m + 1\\)\u3068\u3057\u307e\u3059\u3002
          2. nums[m] > target\u306e\u5834\u5408\u3001target\u306f\u533a\u9593\\([i, m - 1]\\)\u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\u305f\u3081\u3001\\(j = m - 1\\)\u3068\u3057\u307e\u3059\u3002
          3. nums[m] = target\u306e\u5834\u5408\u3001target\u304c\u898b\u3064\u304b\u3063\u305f\u3053\u3068\u3092\u793a\u3059\u305f\u3081\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(m\\)\u3092\u8fd4\u3057\u307e\u3059\u3002

        \u914d\u5217\u306b\u76ee\u6a19\u8981\u7d20\u304c\u542b\u307e\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u3001\u63a2\u7d22\u533a\u9593\u306f\u6700\u7d42\u7684\u306b\u7a7a\u306b\u306a\u308a\u3001\\(-1\\)\u3092\u8fd4\u3057\u3066\u7d42\u4e86\u3057\u307e\u3059\u3002

        <1><2><3><4><5><6><7>

        \u56f3 10-2 \u00a0 Binary search process

        \\(i\\)\u3068\\(j\\)\u304c\u4e21\u65b9\u3068\u3082int\u578b\u3067\u3042\u308b\u305f\u3081\u3001**\\(i + j\\)\u306fint\u578b\u306e\u7bc4\u56f2\u3092\u8d85\u3048\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b**\u3053\u3068\u306f\u6ce8\u76ee\u306b\u5024\u3057\u307e\u3059\u3002\u5927\u304d\u306a\u6570\u306e\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u3092\u907f\u3051\u308b\u305f\u3081\u3001\u901a\u5e38\u306f\u5f0f\\(m = \\lfloor {i + (j - i) / 2} \\rfloor\\)\u3092\u4f7f\u7528\u3057\u3066\u4e2d\u70b9\u3092\u8a08\u7b97\u3057\u307e\u3059\u3002

        \u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search.py
        def binary_search(nums: list[int], target: int) -> int:\n    \"\"\"\u4e8c\u5206\u63a2\u7d22\uff08\u4e21\u7aef\u9589\u533a\u9593\uff09\"\"\"\n    # \u4e21\u7aef\u9589\u533a\u9593 [0, n-1] \u3092\u521d\u671f\u5316\u3001\u3059\u306a\u308f\u3061 i, j \u306f\u305d\u308c\u305e\u308c\u914d\u5217\u306e\u6700\u521d\u306e\u8981\u7d20\u3068\u6700\u5f8c\u306e\u8981\u7d20\u3092\u6307\u3059\n    i, j = 0, len(nums) - 1\n    # \u691c\u7d22\u533a\u9593\u304c\u7a7a\u306b\u306a\u308b\u307e\u3067\u30eb\u30fc\u30d7\uff08i > j \u306e\u3068\u304d\u7a7a\uff09\n    while i <= j:\n        # \u7406\u8ad6\u7684\u306b\u306f\u3001Python\u306e\u6570\u5024\u306f\u7121\u9650\u306b\u5927\u304d\u304f\u306a\u308b\u3053\u3068\u304c\u3067\u304d\u308b\uff08\u30e1\u30e2\u30ea\u30b5\u30a4\u30ba\u306b\u4f9d\u5b58\uff09\u305f\u3081\u3001\u5927\u304d\u306a\u6570\u306e\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u3092\u8003\u616e\u3059\u308b\u5fc5\u8981\u306f\u306a\u3044\n        m = i + (j - i) // 2  # \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 m \u3092\u8a08\u7b97\n        if nums[m] < target:\n            i = m + 1  # \u3053\u306e\u5834\u5408\u3001target \u306f\u533a\u9593 [m+1, j] \u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n        elif nums[m] > target:\n            j = m - 1  # \u3053\u306e\u5834\u5408\u3001target \u306f\u533a\u9593 [i, m-1] \u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n        else:\n            return m  # \u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u304c\u898b\u3064\u304b\u3063\u305f\u305f\u3081\u3001\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n    return -1  # \u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u305f\u3081\u3001-1 \u3092\u8fd4\u3059\n
        binary_search.cpp
        /* \u4e8c\u5206\u63a2\u7d22\uff08\u4e21\u7aef\u9589\u533a\u9593\uff09 */\nint binarySearch(vector<int> &nums, int target) {\n    // \u4e21\u7aef\u9589\u533a\u9593[0, n-1]\u3092\u521d\u671f\u5316\u3001\u3059\u306a\u308f\u3061i\u3001j\u306f\u305d\u308c\u305e\u308c\u914d\u5217\u306e\u6700\u521d\u306e\u8981\u7d20\u3068\u6700\u5f8c\u306e\u8981\u7d20\u3092\u6307\u3059\n    int i = 0, j = nums.size() - 1;\n    // \u63a2\u7d22\u533a\u9593\u304c\u7a7a\u306b\u306a\u308b\u307e\u3067\u30eb\u30fc\u30d7\uff08i > j\u306e\u6642\u7a7a\u306b\u306a\u308b\uff09\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9m\u3092\u8a08\u7b97\n        if (nums[m] < target)    // \u3053\u306e\u72b6\u6cc1\u306ftarget\u304c\u533a\u9593[m+1, j]\u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n            i = m + 1;\n        else if (nums[m] > target) // \u3053\u306e\u72b6\u6cc1\u306ftarget\u304c\u533a\u9593[i, m-1]\u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n            j = m - 1;\n        else // \u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u304c\u898b\u3064\u304b\u3063\u305f\u305f\u3081\u3001\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n            return m;\n    }\n    // \u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u305f\u3081\u3001-1\u3092\u8fd4\u3059\n    return -1;\n}\n
        binary_search.java
        /* \u4e8c\u5206\u63a2\u7d22\uff08\u4e21\u7aef\u9589\u533a\u9593\uff09 */\nint binarySearch(int[] nums, int target) {\n    // \u4e21\u7aef\u9589\u533a\u9593 [0, n-1] \u3092\u521d\u671f\u5316\u3001\u3059\u306a\u308f\u3061 i, j \u306f\u305d\u308c\u305e\u308c\u914d\u5217\u306e\u6700\u521d\u306e\u8981\u7d20\u3068\u6700\u5f8c\u306e\u8981\u7d20\u3092\u6307\u3059\n    int i = 0, j = nums.length - 1;\n    // \u63a2\u7d22\u533a\u9593\u304c\u7a7a\u306b\u306a\u308b\u307e\u3067\u30eb\u30fc\u30d7\uff08i > j \u306e\u3068\u304d\u7a7a\uff09\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 m \u3092\u8a08\u7b97\n        if (nums[m] < target) // \u3053\u306e\u72b6\u6cc1\u306f target \u304c\u533a\u9593 [m+1, j] \u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n            i = m + 1;\n        else if (nums[m] > target) // \u3053\u306e\u72b6\u6cc1\u306f target \u304c\u533a\u9593 [i, m-1] \u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n            j = m - 1;\n        else // \u76ee\u6a19\u8981\u7d20\u3092\u898b\u3064\u3051\u305f\u306e\u3067\u3001\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n            return m;\n    }\n    // \u76ee\u6a19\u8981\u7d20\u3092\u898b\u3064\u3051\u3089\u308c\u306a\u304b\u3063\u305f\u306e\u3067\u3001-1 \u3092\u8fd4\u3059\n    return -1;\n}\n
        binary_search.cs
        [class]{binary_search}-[func]{BinarySearch}\n
        binary_search.go
        [class]{}-[func]{binarySearch}\n
        binary_search.swift
        [class]{}-[func]{binarySearch}\n
        binary_search.js
        [class]{}-[func]{binarySearch}\n
        binary_search.ts
        [class]{}-[func]{binarySearch}\n
        binary_search.dart
        [class]{}-[func]{binarySearch}\n
        binary_search.rs
        [class]{}-[func]{binary_search}\n
        binary_search.c
        [class]{}-[func]{binarySearch}\n
        binary_search.kt
        [class]{}-[func]{binarySearch}\n
        binary_search.rb
        [class]{}-[func]{binary_search}\n

        \u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(\\log n)\\)\u3067\u3059\uff1a\u4e8c\u5206\u30eb\u30fc\u30d7\u306b\u304a\u3044\u3066\u3001\u533a\u9593\u306f\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u534a\u5206\u306b\u6e1b\u5c11\u3059\u308b\u305f\u3081\u3001\u53cd\u5fa9\u56de\u6570\u306f\\(\\log_2 n\\)\u3068\u306a\u308a\u307e\u3059\u3002

        \u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u3067\u3059\uff1a\u30dd\u30a4\u30f3\u30bf\\(i\\)\u3068\\(j\\)\u306f\u5b9a\u6570\u30b5\u30a4\u30ba\u306e\u7a7a\u9593\u3092\u5360\u6709\u3057\u307e\u3059\u3002

        "},{"location":"chapter_searching/binary_search/#1011","title":"10.1.1 \u00a0 \u533a\u9593\u8868\u73fe\u65b9\u6cd5","text":"

        \u4e0a\u8a18\u306e\u9589\u533a\u9593\u306e\u4ed6\u306b\u3001\u3082\u3046\u4e00\u3064\u306e\u4e00\u822c\u7684\u306a\u533a\u9593\u8868\u73fe\u306f\u300c\u5de6\u9589\u53f3\u958b\u300d\u533a\u9593\u3067\u3001\\([0, n)\\)\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u3001\u5de6\u5883\u754c\u306f\u81ea\u8eab\u3092\u542b\u307f\u3001\u53f3\u5883\u754c\u306f\u542b\u307f\u307e\u305b\u3093\u3002\u3053\u306e\u8868\u73fe\u3067\u306f\u3001\\(i = j\\)\u306e\u3068\u304d\u533a\u9593\\([i, j)\\)\u306f\u7a7a\u306b\u306a\u308a\u307e\u3059\u3002

        \u3053\u306e\u8868\u73fe\u306b\u57fa\u3065\u3044\u3066\u540c\u3058\u6a5f\u80fd\u3092\u6301\u3064\u4e8c\u5206\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search.py
        def binary_search_lcro(nums: list[int], target: int) -> int:\n    \"\"\"\u4e8c\u5206\u63a2\u7d22\uff08\u5de6\u9589\u53f3\u958b\u533a\u9593\uff09\"\"\"\n    # \u5de6\u9589\u53f3\u958b\u533a\u9593 [0, n) \u3092\u521d\u671f\u5316\u3001\u3059\u306a\u308f\u3061 i, j \u306f\u305d\u308c\u305e\u308c\u914d\u5217\u306e\u6700\u521d\u306e\u8981\u7d20\u3068\u6700\u5f8c\u306e\u8981\u7d20+1\u3092\u6307\u3059\n    i, j = 0, len(nums)\n    # \u691c\u7d22\u533a\u9593\u304c\u7a7a\u306b\u306a\u308b\u307e\u3067\u30eb\u30fc\u30d7\uff08i = j \u306e\u3068\u304d\u7a7a\uff09\n    while i < j:\n        m = i + (j - i) // 2  # \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 m \u3092\u8a08\u7b97\n        if nums[m] < target:\n            i = m + 1  # \u3053\u306e\u5834\u5408\u3001target \u306f\u533a\u9593 [m+1, j) \u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n        elif nums[m] > target:\n            j = m  # \u3053\u306e\u5834\u5408\u3001target \u306f\u533a\u9593 [i, m) \u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n        else:\n            return m  # \u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u304c\u898b\u3064\u304b\u3063\u305f\u305f\u3081\u3001\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n    return -1  # \u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u305f\u3081\u3001-1 \u3092\u8fd4\u3059\n
        binary_search.cpp
        /* \u4e8c\u5206\u63a2\u7d22\uff08\u5de6\u9589\u53f3\u958b\u533a\u9593\uff09 */\nint binarySearchLCRO(vector<int> &nums, int target) {\n    // \u5de6\u9589\u53f3\u958b\u533a\u9593[0, n)\u3092\u521d\u671f\u5316\u3001\u3059\u306a\u308f\u3061i\u3001j\u306f\u305d\u308c\u305e\u308c\u914d\u5217\u306e\u6700\u521d\u306e\u8981\u7d20\u3068\u6700\u5f8c\u306e\u8981\u7d20+1\u3092\u6307\u3059\n    int i = 0, j = nums.size();\n    // \u63a2\u7d22\u533a\u9593\u304c\u7a7a\u306b\u306a\u308b\u307e\u3067\u30eb\u30fc\u30d7\uff08i = j\u306e\u6642\u7a7a\u306b\u306a\u308b\uff09\n    while (i < j) {\n        int m = i + (j - i) / 2; // \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9m\u3092\u8a08\u7b97\n        if (nums[m] < target)    // \u3053\u306e\u72b6\u6cc1\u306ftarget\u304c\u533a\u9593[m+1, j)\u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n            i = m + 1;\n        else if (nums[m] > target) // \u3053\u306e\u72b6\u6cc1\u306ftarget\u304c\u533a\u9593[i, m)\u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n            j = m;\n        else // \u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u304c\u898b\u3064\u304b\u3063\u305f\u305f\u3081\u3001\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n            return m;\n    }\n    // \u30bf\u30fc\u30b2\u30c3\u30c8\u8981\u7d20\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u305f\u3081\u3001-1\u3092\u8fd4\u3059\n    return -1;\n}\n
        binary_search.java
        /* \u4e8c\u5206\u63a2\u7d22\uff08\u5de6\u9589\u53f3\u958b\u533a\u9593\uff09 */\nint binarySearchLCRO(int[] nums, int target) {\n    // \u5de6\u9589\u53f3\u958b\u533a\u9593 [0, n) \u3092\u521d\u671f\u5316\u3001\u3059\u306a\u308f\u3061 i, j \u306f\u305d\u308c\u305e\u308c\u914d\u5217\u306e\u6700\u521d\u306e\u8981\u7d20\u3068\u6700\u5f8c\u306e\u8981\u7d20+1\u3092\u6307\u3059\n    int i = 0, j = nums.length;\n    // \u63a2\u7d22\u533a\u9593\u304c\u7a7a\u306b\u306a\u308b\u307e\u3067\u30eb\u30fc\u30d7\uff08i = j \u306e\u3068\u304d\u7a7a\uff09\n    while (i < j) {\n        int m = i + (j - i) / 2; // \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 m \u3092\u8a08\u7b97\n        if (nums[m] < target) // \u3053\u306e\u72b6\u6cc1\u306f target \u304c\u533a\u9593 [m+1, j) \u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n            i = m + 1;\n        else if (nums[m] > target) // \u3053\u306e\u72b6\u6cc1\u306f target \u304c\u533a\u9593 [i, m) \u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\n            j = m;\n        else // \u76ee\u6a19\u8981\u7d20\u3092\u898b\u3064\u3051\u305f\u306e\u3067\u3001\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n            return m;\n    }\n    // \u76ee\u6a19\u8981\u7d20\u3092\u898b\u3064\u3051\u3089\u308c\u306a\u304b\u3063\u305f\u306e\u3067\u3001-1 \u3092\u8fd4\u3059\n    return -1;\n}\n
        binary_search.cs
        [class]{binary_search}-[func]{BinarySearchLCRO}\n
        binary_search.go
        [class]{}-[func]{binarySearchLCRO}\n
        binary_search.swift
        [class]{}-[func]{binarySearchLCRO}\n
        binary_search.js
        [class]{}-[func]{binarySearchLCRO}\n
        binary_search.ts
        [class]{}-[func]{binarySearchLCRO}\n
        binary_search.dart
        [class]{}-[func]{binarySearchLCRO}\n
        binary_search.rs
        [class]{}-[func]{binary_search_lcro}\n
        binary_search.c
        [class]{}-[func]{binarySearchLCRO}\n
        binary_search.kt
        [class]{}-[func]{binarySearchLCRO}\n
        binary_search.rb
        [class]{}-[func]{binary_search_lcro}\n

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u30012\u3064\u306e\u533a\u9593\u8868\u73fe\u30bf\u30a4\u30d7\u306b\u304a\u3044\u3066\u3001\u4e8c\u5206\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u521d\u671f\u5316\u3001\u30eb\u30fc\u30d7\u6761\u4ef6\u3001\u533a\u9593\u7e2e\u5c0f\u64cd\u4f5c\u304c\u7570\u306a\u308a\u307e\u3059\u3002

        \u300c\u9589\u533a\u9593\u300d\u8868\u73fe\u3067\u306f\u4e21\u65b9\u306e\u5883\u754c\u304c\u5305\u542b\u7684\u3067\u3042\u308b\u305f\u3081\u3001\u30dd\u30a4\u30f3\u30bf\\(i\\)\u3068\\(j\\)\u306b\u3088\u308b\u533a\u9593\u7e2e\u5c0f\u64cd\u4f5c\u3082\u5bfe\u79f0\u7684\u3067\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u306b\u304f\u304f\u306a\u308b\u305f\u3081\u3001\u4e00\u822c\u7684\u306b\u300c\u9589\u533a\u9593\u300d\u30a2\u30d7\u30ed\u30fc\u30c1\u306e\u4f7f\u7528\u304c\u63a8\u5968\u3055\u308c\u307e\u3059\u3002

        \u56f3 10-3 \u00a0 Two types of interval definitions

        "},{"location":"chapter_searching/binary_search/#1012","title":"10.1.2 \u00a0 \u5229\u70b9\u3068\u5236\u9650","text":"

        \u4e8c\u5206\u63a2\u7d22\u306f\u6642\u9593\u3068\u7a7a\u9593\u306e\u4e21\u65b9\u306e\u9762\u3067\u826f\u597d\u306a\u6027\u80fd\u3092\u793a\u3057\u307e\u3059\u3002

        • \u4e8c\u5206\u63a2\u7d22\u306f\u6642\u9593\u52b9\u7387\u304c\u826f\u3044\u3067\u3059\u3002\u5927\u304d\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3067\u306f\u3001\u5bfe\u6570\u6642\u9593\u8a08\u7b97\u91cf\u304c\u5927\u304d\u306a\u5229\u70b9\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u30b5\u30a4\u30ba\\(n = 2^{20}\\)\u306e\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u7dda\u5f62\u63a2\u7d22\u306f\\(2^{20} = 1048576\\)\u56de\u306e\u53cd\u5fa9\u304c\u5fc5\u8981\u3067\u3059\u304c\u3001\u4e8c\u5206\u63a2\u7d22\u306f\\(\\log_2 2^{20} = 20\\)\u56de\u306e\u30eb\u30fc\u30d7\u306e\u307f\u3067\u6e08\u307f\u307e\u3059\u3002
        • \u4e8c\u5206\u63a2\u7d22\u306b\u306f\u8ffd\u52a0\u306e\u7a7a\u9593\u304c\u5fc5\u8981\u3042\u308a\u307e\u305b\u3093\u3002\u8ffd\u52a0\u306e\u7a7a\u9593\u306b\u4f9d\u5b58\u3059\u308b\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff08\u30cf\u30c3\u30b7\u30e5\u63a2\u7d22\u306a\u3069\uff09\u3068\u6bd4\u8f03\u3057\u3066\u3001\u4e8c\u5206\u63a2\u7d22\u306f\u3088\u308a\u7a7a\u9593\u52b9\u7387\u7684\u3067\u3059\u3002

        \u3057\u304b\u3057\u3001\u4e8c\u5206\u63a2\u7d22\u306f\u4ee5\u4e0b\u306e\u61f8\u5ff5\u306b\u3088\u308a\u3001\u3059\u3079\u3066\u306e\u30b7\u30ca\u30ea\u30aa\u306b\u9069\u3057\u3066\u3044\u308b\u3068\u306f\u9650\u308a\u307e\u305b\u3093\u3002

        • \u4e8c\u5206\u63a2\u7d22\u306f\u30bd\u30fc\u30c8\u6e08\u307f\u30c7\u30fc\u30bf\u306b\u306e\u307f\u9069\u7528\u3067\u304d\u307e\u3059\u3002\u672a\u30bd\u30fc\u30c8\u306e\u30c7\u30fc\u30bf\u306f\u4e8c\u5206\u63a2\u7d22\u3092\u9069\u7528\u3059\u308b\u524d\u306b\u30bd\u30fc\u30c8\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u901a\u5e38\\(O(n \\log n)\\)\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3092\u6301\u3064\u305f\u3081\u3001\u3053\u308c\u306f\u4fa1\u5024\u304c\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u3053\u306e\u30b3\u30b9\u30c8\u306f\u7dda\u5f62\u63a2\u7d22\u3088\u308a\u3082\u9ad8\u304f\u3001\u4e8c\u5206\u63a2\u7d22\u81ea\u4f53\u306f\u8a00\u3046\u307e\u3067\u3082\u3042\u308a\u307e\u305b\u3093\u3002\u983b\u7e41\u306a\u633f\u5165\u304c\u3042\u308b\u30b7\u30ca\u30ea\u30aa\u3067\u306f\u3001\u914d\u5217\u3092\u9806\u5e8f\u306b\u4fdd\u3064\u30b3\u30b9\u30c8\u306f\u975e\u5e38\u306b\u9ad8\u304f\u3001\u7279\u5b9a\u306e\u4f4d\u7f6e\u306b\u65b0\u3057\u3044\u8981\u7d20\u3092\u633f\u5165\u3059\u308b\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3059\u3002
        • \u4e8c\u5206\u63a2\u7d22\u306f\u914d\u5217\u306e\u307f\u3092\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\u4e8c\u5206\u63a2\u7d22\u306b\u306f\u975e\u9023\u7d9a\uff08\u30b8\u30e3\u30f3\u30d7\uff09\u8981\u7d20\u30a2\u30af\u30bb\u30b9\u304c\u5fc5\u8981\u3067\u3001\u3053\u308c\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u306f\u975e\u52b9\u7387\u7684\u3067\u3059\u3002\u305d\u306e\u305f\u3081\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3084\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u30c7\u30fc\u30bf\u69cb\u9020\u306f\u3053\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u9069\u3057\u3066\u3044\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        • \u7dda\u5f62\u63a2\u7d22\u306f\u5c0f\u3055\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3067\u3088\u308a\u826f\u3044\u6027\u80fd\u3092\u793a\u3057\u307e\u3059\u3002\u7dda\u5f62\u63a2\u7d22\u3067\u306f\u5404\u53cd\u5fa9\u30671\u3064\u306e\u5224\u5b9a\u64cd\u4f5c\u306e\u307f\u304c\u5fc5\u8981\u3067\u3059\u304c\u3001\u4e8c\u5206\u63a2\u7d22\u3067\u306f1\u3064\u306e\u52a0\u7b97\u30011\u3064\u306e\u9664\u7b97\u30011\u3064\u304b\u30893\u3064\u306e\u5224\u5b9a\u64cd\u4f5c\u30011\u3064\u306e\u52a0\u7b97\uff08\u6e1b\u7b97\uff09\u3092\u542b\u307f\u3001\u5408\u8a084\u3064\u304b\u30896\u3064\u306e\u64cd\u4f5c\u304c\u5fc5\u8981\u3067\u3059\u3002\u305d\u306e\u305f\u3081\u3001\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\\(n\\)\u304c\u5c0f\u3055\u3044\u5834\u5408\u3001\u7dda\u5f62\u63a2\u7d22\u306f\u4e8c\u5206\u63a2\u7d22\u3088\u308a\u3082\u9ad8\u901f\u3067\u3059\u3002
        "},{"location":"chapter_searching/binary_search_edge/","title":"10.3 \u00a0 \u4e8c\u5206\u63a2\u7d22\u306e\u5883\u754c","text":""},{"location":"chapter_searching/binary_search_edge/#1031","title":"10.3.1 \u00a0 \u5de6\u5883\u754c\u3092\u898b\u3064\u3051\u308b","text":"

        Question

        \u91cd\u8907\u8981\u7d20\u3092\u542b\u3080\u53ef\u80fd\u6027\u304c\u3042\u308b\u9577\u3055\\(n\\)\u306e\u30bd\u30fc\u30c8\u6e08\u307f\u914d\u5217nums\u304c\u4e0e\u3048\u3089\u308c\u3001\u6700\u3082\u5de6\u306e\u8981\u7d20target\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u8981\u7d20\u304c\u914d\u5217\u306b\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u306f\u3001\\(-1\\)\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u633f\u5165\u4f4d\u7f6e\u306e\u4e8c\u5206\u63a2\u7d22\u65b9\u6cd5\u3092\u601d\u3044\u51fa\u3059\u3068\u3001\u63a2\u7d22\u5b8c\u4e86\u5f8c\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(i\\)\u306ftarget\u306e\u6700\u3082\u5de6\u306e\u51fa\u73fe\u3092\u6307\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u633f\u5165\u4f4d\u7f6e\u306e\u63a2\u7d22\u306f\u672c\u8cea\u7684\u306b\u6700\u3082\u5de6\u306etarget\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3068\u540c\u3058\u3067\u3059\u3002

        \u633f\u5165\u4f4d\u7f6e\u3092\u898b\u3064\u3051\u308b\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066target\u306e\u5de6\u5883\u754c\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u914d\u5217\u306btarget\u304c\u542b\u307e\u308c\u3066\u3044\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3053\u308c\u306f\u4ee5\u4e0b\u306e2\u3064\u306e\u7d50\u679c\u306b\u3064\u306a\u304c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u633f\u5165\u4f4d\u7f6e\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(i\\)\u304c\u7bc4\u56f2\u5916\u3067\u3059\u3002
        • \u8981\u7d20nums[i]\u304ctarget\u3068\u7b49\u3057\u304f\u3042\u308a\u307e\u305b\u3093\u3002

        \u3053\u308c\u3089\u306e\u5834\u5408\u3001\u5358\u306b\\(-1\\)\u3092\u8fd4\u3057\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_edge.py
        def binary_search_left_edge(nums: list[int], target: int) -> int:\n    \"\"\"\u6700\u5de6\u7aef\u306e\u30bf\u30fc\u30b2\u30c3\u30c8\u306e\u4e8c\u5206\u63a2\u7d22\"\"\"\n    # \u30bf\u30fc\u30b2\u30c3\u30c8\u306e\u633f\u5165\u4f4d\u7f6e\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3068\u540c\u7b49\n    i = binary_search_insertion(nums, target)\n    # \u30bf\u30fc\u30b2\u30c3\u30c8\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u5834\u5408\u3001-1 \u3092\u8fd4\u3059\n    if i == len(nums) or nums[i] != target:\n        return -1\n    # \u30bf\u30fc\u30b2\u30c3\u30c8\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i \u3092\u8fd4\u3059\n    return i\n
        binary_search_edge.cpp
        /* \u6700\u5de6\u306e\u30bf\u30fc\u30b2\u30c3\u30c8\u306e\u4e8c\u5206\u63a2\u7d22 */\nint binarySearchLeftEdge(vector<int> &nums, int target) {\n    // target\u306e\u633f\u5165\u30dd\u30a4\u30f3\u30c8\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3068\u7b49\u4fa1\n    int i = binarySearchInsertion(nums, target);\n    // target\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u305f\u3081\u3001-1\u3092\u8fd4\u3059\n    if (i == nums.size() || nums[i] != target) {\n        return -1;\n    }\n    // target\u304c\u898b\u3064\u304b\u3063\u305f\u305f\u3081\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9i\u3092\u8fd4\u3059\n    return i;\n}\n
        binary_search_edge.java
        /* \u6700\u3082\u5de6\u306e target \u3092\u4e8c\u5206\u63a2\u7d22 */\nint binarySearchLeftEdge(int[] nums, int target) {\n    // target \u306e\u633f\u5165\u70b9\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3068\u7b49\u4fa1\n    int i = binary_search_insertion.binarySearchInsertion(nums, target);\n    // target \u3092\u898b\u3064\u3051\u3089\u308c\u306a\u304b\u3063\u305f\u306e\u3067\u3001-1 \u3092\u8fd4\u3059\n    if (i == nums.length || nums[i] != target) {\n        return -1;\n    }\n    // target \u3092\u898b\u3064\u3051\u305f\u306e\u3067\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i \u3092\u8fd4\u3059\n    return i;\n}\n
        binary_search_edge.cs
        [class]{binary_search_edge}-[func]{BinarySearchLeftEdge}\n
        binary_search_edge.go
        [class]{}-[func]{binarySearchLeftEdge}\n
        binary_search_edge.swift
        [class]{}-[func]{binarySearchLeftEdge}\n
        binary_search_edge.js
        [class]{}-[func]{binarySearchLeftEdge}\n
        binary_search_edge.ts
        [class]{}-[func]{binarySearchLeftEdge}\n
        binary_search_edge.dart
        [class]{}-[func]{binarySearchLeftEdge}\n
        binary_search_edge.rs
        [class]{}-[func]{binary_search_left_edge}\n
        binary_search_edge.c
        [class]{}-[func]{binarySearchLeftEdge}\n
        binary_search_edge.kt
        [class]{}-[func]{binarySearchLeftEdge}\n
        binary_search_edge.rb
        [class]{}-[func]{binary_search_left_edge}\n
        "},{"location":"chapter_searching/binary_search_edge/#1032","title":"10.3.2 \u00a0 \u53f3\u5883\u754c\u3092\u898b\u3064\u3051\u308b","text":"

        target\u306e\u6700\u3082\u53f3\u306e\u51fa\u73fe\u3092\u3069\u306e\u3088\u3046\u306b\u898b\u3064\u3051\u308b\u3067\u3057\u3087\u3046\u304b\uff1f\u6700\u3082\u76f4\u63a5\u7684\u306a\u65b9\u6cd5\u306f\u3001nums[m] == target\u306e\u5834\u5408\u306b\u63a2\u7d22\u5883\u754c\u3092\u8abf\u6574\u3059\u308b\u65b9\u6cd5\u3092\u5909\u66f4\u3057\u3066\u3001\u5f93\u6765\u306e\u4e8c\u5206\u63a2\u7d22\u30ed\u30b8\u30c3\u30af\u3092\u4fee\u6b63\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u30b3\u30fc\u30c9\u306f\u3053\u3053\u3067\u306f\u7701\u7565\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u8208\u5473\u304c\u3042\u308b\u5834\u5408\u306f\u3001\u81ea\u5206\u3067\u30b3\u30fc\u30c9\u3092\u5b9f\u88c5\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002

        \u4ee5\u4e0b\u3067\u306f\u3001\u3055\u3089\u306b2\u3064\u306e\u5de7\u5999\u306a\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002

        "},{"location":"chapter_searching/binary_search_edge/#1","title":"1. \u00a0 \u5de6\u5883\u754c\u63a2\u7d22\u3092\u518d\u5229\u7528\u3059\u308b","text":"

        target\u306e\u6700\u3082\u53f3\u306e\u51fa\u73fe\u3092\u898b\u3064\u3051\u308b\u306b\u306f\u3001\u6700\u3082\u5de6\u306etarget\u3092\u898b\u3064\u3051\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u305f\u95a2\u6570\u3092\u518d\u5229\u7528\u3067\u304d\u307e\u3059\u3002\u5177\u4f53\u7684\u306b\u306f\u3001\u6700\u3082\u53f3\u306e\u30bf\u30fc\u30b2\u30c3\u30c8\u306e\u63a2\u7d22\u3092\u6700\u3082\u5de6\u306e\u30bf\u30fc\u30b2\u30c3\u30c8 + 1\u306e\u63a2\u7d22\u306b\u5909\u63db\u3057\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u63a2\u7d22\u5b8c\u4e86\u5f8c\u3001\u30dd\u30a4\u30f3\u30bf\\(i\\)\u306f\u6700\u3082\u5de6\u306etarget + 1\uff08\u5b58\u5728\u3059\u308b\u5834\u5408\uff09\u3092\u6307\u3057\u3001\u30dd\u30a4\u30f3\u30bf\\(j\\)\u306ftarget\u306e\u6700\u3082\u53f3\u306e\u51fa\u73fe\u3092\u6307\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\\(j\\)\u3092\u8fd4\u3059\u3053\u3068\u3067\u53f3\u5883\u754c\u304c\u5f97\u3089\u308c\u307e\u3059\u3002

        \u56f3 10-7 \u00a0 Transforming the search for the right boundary into the search for the left boundary

        \u8fd4\u3055\u308c\u308b\u633f\u5165\u4f4d\u7f6e\u306f\\(i\\)\u3067\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3057\u305f\u304c\u3063\u3066\u3001\\(j\\)\u3092\u5f97\u308b\u305f\u3081\u306b\u306f1\u3092\u5f15\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_edge.py
        def binary_search_right_edge(nums: list[int], target: int) -> int:\n    \"\"\"\u6700\u53f3\u7aef\u306e\u30bf\u30fc\u30b2\u30c3\u30c8\u306e\u4e8c\u5206\u63a2\u7d22\"\"\"\n    # \u6700\u5de6\u7aef\u306e\u30bf\u30fc\u30b2\u30c3\u30c8 + 1 \u3092\u898b\u3064\u3051\u308b\u3053\u3068\u306b\u5909\u63db\n    i = binary_search_insertion(nums, target + 1)\n    # j \u306f\u6700\u53f3\u7aef\u306e\u30bf\u30fc\u30b2\u30c3\u30c8\u3092\u6307\u3057\u3001i \u306f\u30bf\u30fc\u30b2\u30c3\u30c8\u3088\u308a\u5927\u304d\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u6307\u3059\n    j = i - 1\n    # \u30bf\u30fc\u30b2\u30c3\u30c8\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u5834\u5408\u3001-1 \u3092\u8fd4\u3059\n    if j == -1 or nums[j] != target:\n        return -1\n    # \u30bf\u30fc\u30b2\u30c3\u30c8\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 j \u3092\u8fd4\u3059\n    return j\n
        binary_search_edge.cpp
        /* \u6700\u53f3\u306e\u30bf\u30fc\u30b2\u30c3\u30c8\u306e\u4e8c\u5206\u63a2\u7d22 */\nint binarySearchRightEdge(vector<int> &nums, int target) {\n    // \u6700\u5de6\u306etarget + 1\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u306b\u5909\u63db\n    int i = binarySearchInsertion(nums, target + 1);\n    // j\u306f\u6700\u53f3\u306e\u30bf\u30fc\u30b2\u30c3\u30c8\u3092\u6307\u3057\u3001i\u306ftarget\u3088\u308a\u5927\u304d\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u6307\u3059\n    int j = i - 1;\n    // target\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u305f\u3081\u3001-1\u3092\u8fd4\u3059\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // target\u304c\u898b\u3064\u304b\u3063\u305f\u305f\u3081\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9j\u3092\u8fd4\u3059\n    return j;\n}\n
        binary_search_edge.java
        /* \u6700\u3082\u53f3\u306e target \u3092\u4e8c\u5206\u63a2\u7d22 */\nint binarySearchRightEdge(int[] nums, int target) {\n    // \u6700\u3082\u5de6\u306e target + 1 \u3092\u898b\u3064\u3051\u308b\u3053\u3068\u306b\u5909\u63db\n    int i = binary_search_insertion.binarySearchInsertion(nums, target + 1);\n    // j \u306f\u6700\u3082\u53f3\u306e target \u3092\u6307\u3057\u3001i \u306f target \u3088\u308a\u5927\u304d\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u6307\u3059\n    int j = i - 1;\n    // target \u3092\u898b\u3064\u3051\u3089\u308c\u306a\u304b\u3063\u305f\u306e\u3067\u3001-1 \u3092\u8fd4\u3059\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // target \u3092\u898b\u3064\u3051\u305f\u306e\u3067\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 j \u3092\u8fd4\u3059\n    return j;\n}\n
        binary_search_edge.cs
        [class]{binary_search_edge}-[func]{BinarySearchRightEdge}\n
        binary_search_edge.go
        [class]{}-[func]{binarySearchRightEdge}\n
        binary_search_edge.swift
        [class]{}-[func]{binarySearchRightEdge}\n
        binary_search_edge.js
        [class]{}-[func]{binarySearchRightEdge}\n
        binary_search_edge.ts
        [class]{}-[func]{binarySearchRightEdge}\n
        binary_search_edge.dart
        [class]{}-[func]{binarySearchRightEdge}\n
        binary_search_edge.rs
        [class]{}-[func]{binary_search_right_edge}\n
        binary_search_edge.c
        [class]{}-[func]{binarySearchRightEdge}\n
        binary_search_edge.kt
        [class]{}-[func]{binarySearchRightEdge}\n
        binary_search_edge.rb
        [class]{}-[func]{binary_search_right_edge}\n
        "},{"location":"chapter_searching/binary_search_edge/#2","title":"2. \u00a0 \u8981\u7d20\u63a2\u7d22\u306b\u5909\u63db\u3059\u308b","text":"

        \u914d\u5217\u306btarget\u304c\u542b\u307e\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u3001\\(i\\)\u3068\\(j\\)\u306f\u6700\u7d42\u7684\u306btarget\u3088\u308a\u5927\u304d\u3044\u6700\u521d\u306e\u8981\u7d20\u3068\u5c0f\u3055\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u305d\u308c\u305e\u308c\u6307\u3057\u307e\u3059\u3002

        \u3057\u305f\u304c\u3063\u3066\u3001\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u914d\u5217\u306b\u5b58\u5728\u3057\u306a\u3044\u8981\u7d20\u3092\u69cb\u7bc9\u3057\u3066\u3001\u5de6\u3068\u53f3\u306e\u5883\u754c\u3092\u63a2\u7d22\u3067\u304d\u307e\u3059\u3002

        • \u6700\u3082\u5de6\u306etarget\u3092\u898b\u3064\u3051\u308b\u306b\u306f\uff1atarget - 0.5\u3092\u63a2\u7d22\u3059\u308b\u3053\u3068\u306b\u5909\u63db\u3067\u304d\u3001\u30dd\u30a4\u30f3\u30bf\\(i\\)\u3092\u8fd4\u3057\u307e\u3059\u3002
        • \u6700\u3082\u53f3\u306etarget\u3092\u898b\u3064\u3051\u308b\u306b\u306f\uff1atarget + 0.5\u3092\u63a2\u7d22\u3059\u308b\u3053\u3068\u306b\u5909\u63db\u3067\u304d\u3001\u30dd\u30a4\u30f3\u30bf\\(j\\)\u3092\u8fd4\u3057\u307e\u3059\u3002

        \u56f3 10-8 \u00a0 Transforming the search for boundaries into the search for an element

        \u30b3\u30fc\u30c9\u306f\u3053\u3053\u3067\u306f\u7701\u7565\u3055\u308c\u3066\u3044\u307e\u3059\u304c\u3001\u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u306b\u3064\u3044\u3066\u6ce8\u610f\u3059\u3079\u304d2\u3064\u306e\u91cd\u8981\u306a\u70b9\u304c\u3042\u308a\u307e\u3059\u3002

        • \u4e0e\u3048\u3089\u308c\u305f\u914d\u5217nums\u306b\u306f\u5c0f\u6570\u304c\u542b\u307e\u308c\u3066\u3044\u306a\u3044\u305f\u3081\u3001\u7b49\u3057\u3044\u5834\u5408\u306e\u51e6\u7406\u306f\u5fc3\u914d\u3042\u308a\u307e\u305b\u3093\u3002
        • \u305f\u3060\u3057\u3001\u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u3067\u5c0f\u6570\u3092\u5c0e\u5165\u3059\u308b\u306b\u306f\u3001target\u5909\u6570\u3092\u6d6e\u52d5\u5c0f\u6570\u70b9\u578b\u306b\u5909\u66f4\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff08Python\u3067\u306f\u5909\u66f4\u306f\u4e0d\u8981\u3067\u3059\uff09\u3002
        "},{"location":"chapter_searching/binary_search_insertion/","title":"10.2 \u00a0 \u4e8c\u5206\u63a2\u7d22\u306b\u3088\u308b\u633f\u5165","text":"

        \u4e8c\u5206\u63a2\u7d22\u306f\u76ee\u6a19\u8981\u7d20\u3092\u63a2\u7d22\u3059\u308b\u3060\u3051\u3067\u306a\u304f\u3001\u76ee\u6a19\u8981\u7d20\u306e\u633f\u5165\u4f4d\u7f6e\u3092\u63a2\u7d22\u3059\u308b\u306a\u3069\u3001\u591a\u304f\u306e\u5909\u7a2e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3082\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002

        "},{"location":"chapter_searching/binary_search_insertion/#1021","title":"10.2.1 \u00a0 \u91cd\u8907\u8981\u7d20\u304c\u306a\u3044\u5834\u5408","text":"

        Question

        \u4e00\u610f\u306e\u8981\u7d20\u3092\u6301\u3064\u9577\u3055\\(n\\)\u306e\u30bd\u30fc\u30c8\u6e08\u307f\u914d\u5217nums\u3068\u8981\u7d20target\u304c\u4e0e\u3048\u3089\u308c\u3001\u30bd\u30fc\u30c8\u9806\u3092\u7dad\u6301\u3057\u306a\u304c\u3089target\u3092nums\u306b\u633f\u5165\u3057\u307e\u3059\u3002target\u304c\u914d\u5217\u306b\u3059\u3067\u306b\u5b58\u5728\u3059\u308b\u5834\u5408\u306f\u3001\u65e2\u5b58\u306e\u8981\u7d20\u306e\u5de6\u5074\u306b\u633f\u5165\u3057\u307e\u3059\u3002\u633f\u5165\u5f8c\u306e\u914d\u5217\u306b\u304a\u3051\u308btarget\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4e0b\u56f3\u306b\u793a\u3059\u4f8b\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u56f3 10-4 \u00a0 Example data for binary search insertion point

        \u524d\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u306e\u4e8c\u5206\u63a2\u7d22\u30b3\u30fc\u30c9\u3092\u518d\u5229\u7528\u3057\u305f\u3044\u5834\u5408\u3001\u4ee5\u4e0b\u306e2\u3064\u306e\u8cea\u554f\u306b\u7b54\u3048\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u8cea\u554f1\uff1a\u914d\u5217\u306b\u3059\u3067\u306btarget\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u633f\u5165\u4f4d\u7f6e\u306f\u65e2\u5b58\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u306a\u308a\u307e\u3059\u304b\uff1f

        target\u3092\u7b49\u3057\u3044\u8981\u7d20\u306e\u5de6\u5074\u306b\u633f\u5165\u3059\u308b\u3068\u3044\u3046\u8981\u4ef6\u306f\u3001\u65b0\u3057\u304f\u633f\u5165\u3055\u308c\u308btarget\u304c\u5143\u306etarget\u306e\u4f4d\u7f6e\u3092\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u914d\u5217\u306btarget\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u633f\u5165\u4f4d\u7f6e\u306f\u78ba\u304b\u306b\u305d\u306etarget\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u3059\u3002

        \u8cea\u554f2\uff1a\u914d\u5217\u306btarget\u304c\u542b\u307e\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u3001\u3069\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u633f\u5165\u3055\u308c\u307e\u3059\u304b\uff1f

        \u4e8c\u5206\u63a2\u7d22\u30d7\u30ed\u30bb\u30b9\u3092\u3055\u3089\u306b\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\uff1anums[m] < target\u306e\u3068\u304d\u3001\u30dd\u30a4\u30f3\u30bf\\(i\\)\u304c\u79fb\u52d5\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u30dd\u30a4\u30f3\u30bf\\(i\\)\u304ctarget\u4ee5\u4e0a\u306e\u8981\u7d20\u306b\u8fd1\u3065\u3044\u3066\u3044\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u540c\u69d8\u306b\u3001\u30dd\u30a4\u30f3\u30bf\\(j\\)\u306f\u5e38\u306btarget\u4ee5\u4e0b\u306e\u8981\u7d20\u306b\u8fd1\u3065\u3044\u3066\u3044\u307e\u3059\u3002

        \u3057\u305f\u304c\u3063\u3066\u3001\u4e8c\u5206\u306e\u7d42\u4e86\u6642\u306b\u306f\u78ba\u5b9f\u306b\uff1a\\(i\\)\u306ftarget\u3088\u308a\u5927\u304d\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u6307\u3057\u3001\\(j\\)\u306ftarget\u3088\u308a\u5c0f\u3055\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u6307\u3057\u307e\u3059\u3002\u914d\u5217\u306btarget\u304c\u542b\u307e\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u3001\u633f\u5165\u4f4d\u7f6e\u306f\\(i\\)\u3067\u3042\u308b\u3053\u3068\u306f\u660e\u3089\u304b\u3067\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_insertion.py
        def binary_search_insertion_simple(nums: list[int], target: int) -> int:\n    \"\"\"\u633f\u5165\u4f4d\u7f6e\u306e\u4e8c\u5206\u63a2\u7d22\uff08\u91cd\u8907\u8981\u7d20\u306a\u3057\uff09\"\"\"\n    i, j = 0, len(nums) - 1  # \u4e21\u7aef\u9589\u533a\u9593 [0, n-1] \u3092\u521d\u671f\u5316\n    while i <= j:\n        m = i + (j - i) // 2  # \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 m \u3092\u8a08\u7b97\n        if nums[m] < target:\n            i = m + 1  # \u30bf\u30fc\u30b2\u30c3\u30c8\u306f\u533a\u9593 [m+1, j] \u306b\u3042\u308b\n        elif nums[m] > target:\n            j = m - 1  # \u30bf\u30fc\u30b2\u30c3\u30c8\u306f\u533a\u9593 [i, m-1] \u306b\u3042\u308b\n        else:\n            return m  # \u30bf\u30fc\u30b2\u30c3\u30c8\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001\u633f\u5165\u4f4d\u7f6e m \u3092\u8fd4\u3059\n    # \u30bf\u30fc\u30b2\u30c3\u30c8\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u5834\u5408\u3001\u633f\u5165\u4f4d\u7f6e i \u3092\u8fd4\u3059\n    return i\n
        binary_search_insertion.cpp
        /* \u633f\u5165\u30dd\u30a4\u30f3\u30c8\u306e\u4e8c\u5206\u63a2\u7d22\uff08\u91cd\u8907\u8981\u7d20\u306a\u3057\uff09 */\nint binarySearchInsertionSimple(vector<int> &nums, int target) {\n    int i = 0, j = nums.size() - 1; // \u4e21\u7aef\u9589\u533a\u9593[0, n-1]\u3092\u521d\u671f\u5316\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9m\u3092\u8a08\u7b97\n        if (nums[m] < target) {\n            i = m + 1; // \u30bf\u30fc\u30b2\u30c3\u30c8\u306f\u533a\u9593[m+1, j]\u306b\u3042\u308b\n        } else if (nums[m] > target) {\n            j = m - 1; // \u30bf\u30fc\u30b2\u30c3\u30c8\u306f\u533a\u9593[i, m-1]\u306b\u3042\u308b\n        } else {\n            return m; // \u30bf\u30fc\u30b2\u30c3\u30c8\u304c\u898b\u3064\u304b\u3063\u305f\u305f\u3081\u3001\u633f\u5165\u30dd\u30a4\u30f3\u30c8m\u3092\u8fd4\u3059\n        }\n    }\n    // \u30bf\u30fc\u30b2\u30c3\u30c8\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u305f\u3081\u3001\u633f\u5165\u30dd\u30a4\u30f3\u30c8i\u3092\u8fd4\u3059\n    return i;\n}\n
        binary_search_insertion.java
        /* \u633f\u5165\u70b9\u306e\u4e8c\u5206\u63a2\u7d22\uff08\u91cd\u8907\u8981\u7d20\u306a\u3057\uff09 */\nint binarySearchInsertionSimple(int[] nums, int target) {\n    int i = 0, j = nums.length - 1; // \u4e21\u7aef\u9589\u533a\u9593 [0, n-1] \u3092\u521d\u671f\u5316\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 m \u3092\u8a08\u7b97\n        if (nums[m] < target) {\n            i = m + 1; // target \u306f\u533a\u9593 [m+1, j] \u306b\u3042\u308b\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u306f\u533a\u9593 [i, m-1] \u306b\u3042\u308b\n        } else {\n            return m; // target \u3092\u898b\u3064\u3051\u305f\u306e\u3067\u3001\u633f\u5165\u70b9 m \u3092\u8fd4\u3059\n        }\n    }\n    // target \u3092\u898b\u3064\u3051\u3089\u308c\u306a\u304b\u3063\u305f\u306e\u3067\u3001\u633f\u5165\u70b9 i \u3092\u8fd4\u3059\n    return i;\n}\n
        binary_search_insertion.cs
        [class]{binary_search_insertion}-[func]{BinarySearchInsertionSimple}\n
        binary_search_insertion.go
        [class]{}-[func]{binarySearchInsertionSimple}\n
        binary_search_insertion.swift
        [class]{}-[func]{binarySearchInsertionSimple}\n
        binary_search_insertion.js
        [class]{}-[func]{binarySearchInsertionSimple}\n
        binary_search_insertion.ts
        [class]{}-[func]{binarySearchInsertionSimple}\n
        binary_search_insertion.dart
        [class]{}-[func]{binarySearchInsertionSimple}\n
        binary_search_insertion.rs
        [class]{}-[func]{binary_search_insertion_simple}\n
        binary_search_insertion.c
        [class]{}-[func]{binarySearchInsertionSimple}\n
        binary_search_insertion.kt
        [class]{}-[func]{binarySearchInsertionSimple}\n
        binary_search_insertion.rb
        [class]{}-[func]{binary_search_insertion_simple}\n
        "},{"location":"chapter_searching/binary_search_insertion/#1022","title":"10.2.2 \u00a0 \u91cd\u8907\u8981\u7d20\u304c\u3042\u308b\u5834\u5408","text":"

        Question

        \u524d\u306e\u8cea\u554f\u306b\u57fa\u3065\u3044\u3066\u3001\u914d\u5217\u306b\u91cd\u8907\u8981\u7d20\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3068\u4eee\u5b9a\u3057\u3001\u4ed6\u306f\u3059\u3079\u3066\u540c\u3058\u3068\u3057\u307e\u3059\u3002

        \u914d\u5217\u306btarget\u306e\u8907\u6570\u306e\u51fa\u73fe\u304c\u3042\u308b\u5834\u5408\u3001\u901a\u5e38\u306e\u4e8c\u5206\u63a2\u7d22\u306ftarget\u306e1\u3064\u306e\u51fa\u73fe\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u307f\u3092\u8fd4\u3059\u3053\u3068\u304c\u3067\u304d\u3001\u305d\u306e\u4f4d\u7f6e\u306e\u5de6\u53f3\u306btarget\u306e\u51fa\u73fe\u304c\u3044\u304f\u3064\u3042\u308b\u304b\u3092\u7279\u5b9a\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002

        \u554f\u984c\u3067\u306f\u76ee\u6a19\u8981\u7d20\u3092\u6700\u3082\u5de6\u306e\u4f4d\u7f6e\u306b\u633f\u5165\u3059\u308b\u3053\u3068\u304c\u8981\u6c42\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u914d\u5217\u5185\u306e\u6700\u3082\u5de6\u306etarget\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u898b\u3064\u3051\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u6700\u521d\u306b\u4e0b\u56f3\u306b\u793a\u3059\u30b9\u30c6\u30c3\u30d7\u3092\u901a\u3057\u3066\u3053\u308c\u3092\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002

        1. \u4e8c\u5206\u63a2\u7d22\u3092\u5b9f\u884c\u3057\u3066target\u306e\u4efb\u610f\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3001\u4f8b\u3048\u3070\\(k\\)\u3092\u898b\u3064\u3051\u307e\u3059\u3002
        2. \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(k\\)\u304b\u3089\u958b\u59cb\u3057\u3066\u3001\u6700\u3082\u5de6\u306etarget\u306e\u51fa\u73fe\u304c\u898b\u3064\u304b\u308b\u307e\u3067\u5de6\u306b\u7dda\u5f62\u63a2\u7d22\u3092\u884c\u3044\u3001\u3053\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3057\u307e\u3059\u3002

        \u56f3 10-5 \u00a0 Linear search for the insertion point of duplicate elements

        \u3053\u306e\u65b9\u6cd5\u306f\u5b9f\u73fe\u53ef\u80fd\u3067\u3059\u304c\u3001\u7dda\u5f62\u63a2\u7d22\u3092\u542b\u3080\u305f\u3081\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3059\u3002\u3053\u306e\u65b9\u6cd5\u306f\u3001\u914d\u5217\u306b\u591a\u304f\u306e\u91cd\u8907\u3059\u308btarget\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u5834\u5408\u306b\u975e\u52b9\u7387\u3067\u3059\u3002

        \u4eca\u5ea6\u306f\u4e8c\u5206\u63a2\u7d22\u30b3\u30fc\u30c9\u3092\u62e1\u5f35\u3059\u308b\u3053\u3068\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5168\u4f53\u7684\u306a\u30d7\u30ed\u30bb\u30b9\u306f\u540c\u3058\u307e\u307e\u3067\u3059\u3002\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u3001\u307e\u305a\u4e2d\u9593\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(m\\)\u3092\u8a08\u7b97\u3057\u3001\u6b21\u306btarget\u3068nums[m]\u306e\u5024\u3092\u6bd4\u8f03\u3057\u3066\u3001\u4ee5\u4e0b\u306e\u30b1\u30fc\u30b9\u306b\u306a\u308a\u307e\u3059\u3002

        • nums[m] < target\u307e\u305f\u306fnums[m] > target\u306e\u3068\u304d\u3001\u3053\u308c\u306ftarget\u304c\u307e\u3060\u898b\u3064\u304b\u3063\u3066\u3044\u306a\u3044\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u305f\u3081\u3001\u901a\u5e38\u306e\u4e8c\u5206\u63a2\u7d22\u3092\u4f7f\u7528\u3057\u3066\u63a2\u7d22\u7bc4\u56f2\u3092\u72ed\u3081\u3001\u30dd\u30a4\u30f3\u30bf\\(i\\)\u3068\\(j\\)\u3092target\u306b\u8fd1\u3065\u3051\u307e\u3059\u3002
        • nums[m] == target\u306e\u3068\u304d\u3001\u3053\u308c\u306ftarget\u3088\u308a\u5c0f\u3055\u3044\u8981\u7d20\u304c\u7bc4\u56f2\\([i, m - 1]\\)\u306b\u3042\u308b\u3053\u3068\u3092\u793a\u3059\u305f\u3081\u3001\\(j = m - 1\\)\u3092\u4f7f\u7528\u3057\u3066\u7bc4\u56f2\u3092\u72ed\u3081\u3001\u30dd\u30a4\u30f3\u30bf\\(j\\)\u3092target\u3088\u308a\u5c0f\u3055\u3044\u8981\u7d20\u306b\u8fd1\u3065\u3051\u307e\u3059\u3002

        \u30eb\u30fc\u30d7\u5f8c\u3001\\(i\\)\u306f\u6700\u3082\u5de6\u306etarget\u3092\u6307\u3057\u3001\\(j\\)\u306ftarget\u3088\u308a\u5c0f\u3055\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u6307\u3059\u305f\u3081\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(i\\)\u304c\u633f\u5165\u4f4d\u7f6e\u3067\u3059\u3002

        <1><2><3><4><5><6><7><8>

        \u56f3 10-6 \u00a0 Steps for binary search insertion point of duplicate elements

        \u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u89b3\u5bdf\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u5206\u5c90nums[m] > target\u3068nums[m] == target\u306e\u64cd\u4f5c\u306f\u540c\u3058\u3067\u3042\u308b\u305f\u3081\u3001\u3053\u308c\u30892\u3064\u306e\u5206\u5c90\u3092\u30de\u30fc\u30b8\u3067\u304d\u307e\u3059\u3002

        \u305d\u308c\u3067\u3082\u3001\u30ed\u30b8\u30c3\u30af\u304c\u3088\u308a\u660e\u78ba\u306b\u306a\u308a\u3001\u53ef\u8aad\u6027\u304c\u5411\u4e0a\u3059\u308b\u305f\u3081\u3001\u6761\u4ef6\u3092\u5c55\u958b\u3057\u305f\u307e\u307e\u306b\u3057\u3066\u304a\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_insertion.py
        def binary_search_insertion(nums: list[int], target: int) -> int:\n    \"\"\"\u633f\u5165\u4f4d\u7f6e\u306e\u4e8c\u5206\u63a2\u7d22\uff08\u91cd\u8907\u8981\u7d20\u3042\u308a\uff09\"\"\"\n    i, j = 0, len(nums) - 1  # \u4e21\u7aef\u9589\u533a\u9593 [0, n-1] \u3092\u521d\u671f\u5316\n    while i <= j:\n        m = i + (j - i) // 2  # \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 m \u3092\u8a08\u7b97\n        if nums[m] < target:\n            i = m + 1  # \u30bf\u30fc\u30b2\u30c3\u30c8\u306f\u533a\u9593 [m+1, j] \u306b\u3042\u308b\n        elif nums[m] > target:\n            j = m - 1  # \u30bf\u30fc\u30b2\u30c3\u30c8\u306f\u533a\u9593 [i, m-1] \u306b\u3042\u308b\n        else:\n            j = m - 1  # \u30bf\u30fc\u30b2\u30c3\u30c8\u672a\u6e80\u306e\u6700\u521d\u306e\u8981\u7d20\u306f\u533a\u9593 [i, m-1] \u306b\u3042\u308b\n    # \u633f\u5165\u4f4d\u7f6e i \u3092\u8fd4\u3059\n    return i\n
        binary_search_insertion.cpp
        /* \u633f\u5165\u30dd\u30a4\u30f3\u30c8\u306e\u4e8c\u5206\u63a2\u7d22\uff08\u91cd\u8907\u8981\u7d20\u3042\u308a\uff09 */\nint binarySearchInsertion(vector<int> &nums, int target) {\n    int i = 0, j = nums.size() - 1; // \u4e21\u7aef\u9589\u533a\u9593[0, n-1]\u3092\u521d\u671f\u5316\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9m\u3092\u8a08\u7b97\n        if (nums[m] < target) {\n            i = m + 1; // \u30bf\u30fc\u30b2\u30c3\u30c8\u306f\u533a\u9593[m+1, j]\u306b\u3042\u308b\n        } else if (nums[m] > target) {\n            j = m - 1; // \u30bf\u30fc\u30b2\u30c3\u30c8\u306f\u533a\u9593[i, m-1]\u306b\u3042\u308b\n        } else {\n            j = m - 1; // \u30bf\u30fc\u30b2\u30c3\u30c8\u672a\u6e80\u306e\u6700\u521d\u306e\u8981\u7d20\u306f\u533a\u9593[i, m-1]\u306b\u3042\u308b\n        }\n    }\n    // \u633f\u5165\u30dd\u30a4\u30f3\u30c8i\u3092\u8fd4\u3059\n    return i;\n}\n
        binary_search_insertion.java
        /* \u633f\u5165\u70b9\u306e\u4e8c\u5206\u63a2\u7d22\uff08\u91cd\u8907\u8981\u7d20\u3042\u308a\uff09 */\nint binarySearchInsertion(int[] nums, int target) {\n    int i = 0, j = nums.length - 1; // \u4e21\u7aef\u9589\u533a\u9593 [0, n-1] \u3092\u521d\u671f\u5316\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u4e2d\u70b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 m \u3092\u8a08\u7b97\n        if (nums[m] < target) {\n            i = m + 1; // target \u306f\u533a\u9593 [m+1, j] \u306b\u3042\u308b\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u306f\u533a\u9593 [i, m-1] \u306b\u3042\u308b\n        } else {\n            j = m - 1; // target \u3088\u308a\u5c0f\u3055\u3044\u6700\u521d\u306e\u8981\u7d20\u306f\u533a\u9593 [i, m-1] \u306b\u3042\u308b\n        }\n    }\n    // \u633f\u5165\u70b9 i \u3092\u8fd4\u3059\n    return i;\n}\n
        binary_search_insertion.cs
        [class]{binary_search_insertion}-[func]{BinarySearchInsertion}\n
        binary_search_insertion.go
        [class]{}-[func]{binarySearchInsertion}\n
        binary_search_insertion.swift
        [class]{}-[func]{binarySearchInsertion}\n
        binary_search_insertion.js
        [class]{}-[func]{binarySearchInsertion}\n
        binary_search_insertion.ts
        [class]{}-[func]{binarySearchInsertion}\n
        binary_search_insertion.dart
        [class]{}-[func]{binarySearchInsertion}\n
        binary_search_insertion.rs
        [class]{}-[func]{binary_search_insertion}\n
        binary_search_insertion.c
        [class]{}-[func]{binarySearchInsertion}\n
        binary_search_insertion.kt
        [class]{}-[func]{binarySearchInsertion}\n
        binary_search_insertion.rb
        [class]{}-[func]{binary_search_insertion}\n

        Tip

        \u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u306e\u30b3\u30fc\u30c9\u306f\u300c\u9589\u533a\u9593\u300d\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002\u300c\u5de6\u9589\u53f3\u958b\u300d\u306b\u8208\u5473\u304c\u3042\u308b\u5834\u5408\u306f\u3001\u81ea\u5206\u3067\u30b3\u30fc\u30c9\u3092\u5b9f\u88c5\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002

        \u8981\u7d04\u3059\u308b\u3068\u3001\u4e8c\u5206\u63a2\u7d22\u306f\u672c\u8cea\u7684\u306b\u30dd\u30a4\u30f3\u30bf\\(i\\)\u3068\\(j\\)\u306e\u63a2\u7d22\u76ee\u6a19\u3092\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u3053\u308c\u3089\u306e\u76ee\u6a19\u306f\u7279\u5b9a\u306e\u8981\u7d20\uff08target\u306a\u3069\uff09\u307e\u305f\u306f\u8981\u7d20\u306e\u7bc4\u56f2\uff08target\u3088\u308a\u5c0f\u3055\u3044\u3082\u306e\u306a\u3069\uff09\u3067\u3042\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        \u4e8c\u5206\u63a2\u7d22\u306e\u9023\u7d9a\u30eb\u30fc\u30d7\u306b\u304a\u3044\u3066\u3001\u30dd\u30a4\u30f3\u30bf\\(i\\)\u3068\\(j\\)\u306f\u6bb5\u968e\u7684\u306b\u4e8b\u524d\u5b9a\u7fa9\u3055\u308c\u305f\u76ee\u6a19\u306b\u8fd1\u3065\u304d\u307e\u3059\u3002\u6700\u7d42\u7684\u306b\u3001\u305d\u308c\u3089\u306f\u7b54\u3048\u3092\u898b\u3064\u3051\u308b\u304b\u3001\u5883\u754c\u3092\u8d8a\u3048\u305f\u5f8c\u306b\u505c\u6b62\u3057\u307e\u3059\u3002

        "},{"location":"chapter_searching/replace_linear_by_hashing/","title":"10.4 \u00a0 \u30cf\u30c3\u30b7\u30e5\u6700\u9069\u5316\u6226\u7565","text":"

        \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u306b\u304a\u3044\u3066\u3001\u7dda\u5f62\u63a2\u7d22\u3092\u30cf\u30c3\u30b7\u30e5\u30d9\u30fc\u30b9\u306e\u63a2\u7d22\u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u3067\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3092\u524a\u6e1b\u3059\u308b\u3053\u3068\u304c\u3088\u304f\u3042\u308a\u307e\u3059\u3002\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u554f\u984c\u3092\u4f7f\u7528\u3057\u3066\u7406\u89e3\u3092\u6df1\u3081\u307e\u3057\u3087\u3046\u3002

        Question

        \u6574\u6570\u914d\u5217nums\u3068\u76ee\u6a19\u8981\u7d20target\u304c\u4e0e\u3048\u3089\u308c\u3001\u914d\u5217\u5185\u3067\u300c\u548c\u300d\u304ctarget\u306b\u7b49\u3057\u30442\u3064\u306e\u8981\u7d20\u3092\u63a2\u7d22\u3057\u3001\u305d\u308c\u3089\u306e\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4efb\u610f\u306e\u89e3\u304c\u53d7\u3051\u5165\u308c\u3089\u308c\u307e\u3059\u3002

        "},{"location":"chapter_searching/replace_linear_by_hashing/#1041","title":"10.4.1 \u00a0 \u7dda\u5f62\u63a2\u7d22\uff1a\u6642\u9593\u3092\u7a7a\u9593\u3068\u4ea4\u63db","text":"

        \u3059\u3079\u3066\u306e\u53ef\u80fd\u306a\u7d44\u307f\u5408\u308f\u305b\u3092\u76f4\u63a5\u6a2a\u65ad\u3059\u308b\u3053\u3068\u3092\u8003\u3048\u3066\u307f\u307e\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30cd\u30b9\u30c8\u3057\u305f\u30eb\u30fc\u30d7\u3092\u958b\u59cb\u3057\u3001\u5404\u53cd\u5fa9\u30672\u3064\u306e\u6574\u6570\u306e\u548c\u304ctarget\u306b\u7b49\u3057\u3044\u304b\u3069\u3046\u304b\u3092\u5224\u65ad\u3057\u307e\u3059\u3002\u305d\u3046\u3067\u3042\u308c\u3070\u3001\u305d\u308c\u3089\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3057\u307e\u3059\u3002

        \u56f3 10-9 \u00a0 Linear search solution for two-sum problem

        \u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby two_sum.py
        def two_sum_brute_force(nums: list[int], target: int) -> list[int]:\n    \"\"\"\u65b9\u6cd5\u4e00\uff1a\u30d6\u30eb\u30fc\u30c8\u30d5\u30a9\u30fc\u30b9\u5217\u6319\"\"\"\n    # \u4e8c\u91cd\u30eb\u30fc\u30d7\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f O(n^2)\n    for i in range(len(nums) - 1):\n        for j in range(i + 1, len(nums)):\n            if nums[i] + nums[j] == target:\n                return [i, j]\n    return []\n
        two_sum.cpp
        /* \u65b9\u6cd5\u4e00\uff1a\u30d6\u30eb\u30fc\u30c8\u30d5\u30a9\u30fc\u30b9\u5217\u6319 */\nvector<int> twoSumBruteForce(vector<int> &nums, int target) {\n    int size = nums.size();\n    // \u4e8c\u91cd\u30eb\u30fc\u30d7\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306fO(n^2)\n    for (int i = 0; i < size - 1; i++) {\n        for (int j = i + 1; j < size; j++) {\n            if (nums[i] + nums[j] == target)\n                return {i, j};\n        }\n    }\n    return {};\n}\n
        two_sum.java
        /* \u65b9\u6cd5\u4e00: \u66b4\u529b\u5217\u6319 */\nint[] twoSumBruteForce(int[] nums, int target) {\n    int size = nums.length;\n    // \u4e8c\u91cd\u30eb\u30fc\u30d7\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f O(n^2)\n    for (int i = 0; i < size - 1; i++) {\n        for (int j = i + 1; j < size; j++) {\n            if (nums[i] + nums[j] == target)\n                return new int[] { i, j };\n        }\n    }\n    return new int[0];\n}\n
        two_sum.cs
        [class]{two_sum}-[func]{TwoSumBruteForce}\n
        two_sum.go
        [class]{}-[func]{twoSumBruteForce}\n
        two_sum.swift
        [class]{}-[func]{twoSumBruteForce}\n
        two_sum.js
        [class]{}-[func]{twoSumBruteForce}\n
        two_sum.ts
        [class]{}-[func]{twoSumBruteForce}\n
        two_sum.dart
        [class]{}-[func]{twoSumBruteForce}\n
        two_sum.rs
        [class]{}-[func]{two_sum_brute_force}\n
        two_sum.c
        [class]{}-[func]{twoSumBruteForce}\n
        two_sum.kt
        [class]{}-[func]{twoSumBruteForce}\n
        two_sum.rb
        [class]{}-[func]{two_sum_brute_force}\n

        \u3053\u306e\u65b9\u6cd5\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n^2)\\)\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u3067\u3001\u5927\u5bb9\u91cf\u30c7\u30fc\u30bf\u3067\u306f\u975e\u5e38\u306b\u6642\u9593\u304c\u304b\u304b\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_searching/replace_linear_by_hashing/#1042","title":"10.4.2 \u00a0 \u30cf\u30c3\u30b7\u30e5\u63a2\u7d22\uff1a\u7a7a\u9593\u3092\u6642\u9593\u3068\u4ea4\u63db","text":"

        \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306e\u4f7f\u7528\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u30ad\u30fc\u3068\u5024\u306e\u30da\u30a2\u306f\u305d\u308c\u305e\u308c\u914d\u5217\u8981\u7d20\u3068\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u3059\u3002\u914d\u5217\u3092\u30eb\u30fc\u30d7\u3057\u3001\u5404\u53cd\u5fa9\u4e2d\u306b\u4e0b\u56f3\u306b\u793a\u3059\u30b9\u30c6\u30c3\u30d7\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002

        1. \u6570\u5024target - nums[i]\u304c\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306b\u3042\u308b\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002\u3042\u308b\u5834\u5408\u306f\u3001\u3053\u308c\u30892\u3064\u306e\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u76f4\u63a5\u8fd4\u3057\u307e\u3059\u3002
        2. \u30ad\u30fc\u3068\u5024\u306e\u30da\u30a2nums[i]\u3068\u30a4\u30f3\u30c7\u30c3\u30af\u30b9i\u3092\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306b\u8ffd\u52a0\u3057\u307e\u3059\u3002
        <1><2><3>

        \u56f3 10-10 \u00a0 Help hash table solve two-sum

        \u5b9f\u88c5\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306b\u793a\u3055\u308c\u3001\u5358\u4e00\u306e\u30eb\u30fc\u30d7\u306e\u307f\u304c\u5fc5\u8981\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby two_sum.py
        def two_sum_hash_table(nums: list[int], target: int) -> list[int]:\n    \"\"\"\u65b9\u6cd5\u4e8c\uff1a\u88dc\u52a9\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\"\"\"\n    # \u88dc\u52a9\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f O(n)\n    dic = {}\n    # \u5358\u4e00\u30eb\u30fc\u30d7\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f O(n)\n    for i in range(len(nums)):\n        if target - nums[i] in dic:\n            return [dic[target - nums[i]], i]\n        dic[nums[i]] = i\n    return []\n
        two_sum.cpp
        /* \u65b9\u6cd5\u4e8c\uff1a\u88dc\u52a9\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb */\nvector<int> twoSumHashTable(vector<int> &nums, int target) {\n    int size = nums.size();\n    // \u88dc\u52a9\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306fO(n)\n    unordered_map<int, int> dic;\n    // \u5358\u5c64\u30eb\u30fc\u30d7\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306fO(n)\n    for (int i = 0; i < size; i++) {\n        if (dic.find(target - nums[i]) != dic.end()) {\n            return {dic[target - nums[i]], i};\n        }\n        dic.emplace(nums[i], i);\n    }\n    return {};\n}\n
        two_sum.java
        /* \u65b9\u6cd5\u4e8c: \u88dc\u52a9\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb */\nint[] twoSumHashTable(int[] nums, int target) {\n    int size = nums.length;\n    // \u88dc\u52a9\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f O(n)\n    Map<Integer, Integer> dic = new HashMap<>();\n    // \u5358\u4e00\u5c64\u30eb\u30fc\u30d7\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f O(n)\n    for (int i = 0; i < size; i++) {\n        if (dic.containsKey(target - nums[i])) {\n            return new int[] { dic.get(target - nums[i]), i };\n        }\n        dic.put(nums[i], i);\n    }\n    return new int[0];\n}\n
        two_sum.cs
        [class]{two_sum}-[func]{TwoSumHashTable}\n
        two_sum.go
        [class]{}-[func]{twoSumHashTable}\n
        two_sum.swift
        [class]{}-[func]{twoSumHashTable}\n
        two_sum.js
        [class]{}-[func]{twoSumHashTable}\n
        two_sum.ts
        [class]{}-[func]{twoSumHashTable}\n
        two_sum.dart
        [class]{}-[func]{twoSumHashTable}\n
        two_sum.rs
        [class]{}-[func]{two_sum_hash_table}\n
        two_sum.c
        [class]{HashTable}-[func]{}\n\n[class]{}-[func]{twoSumHashTable}\n
        two_sum.kt
        [class]{}-[func]{twoSumHashTable}\n
        two_sum.rb
        [class]{}-[func]{two_sum_hash_table}\n

        \u3053\u306e\u65b9\u6cd5\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u63a2\u7d22\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u6642\u9593\u8a08\u7b97\u91cf\u3092\\(O(n^2)\\)\u304b\u3089\\(O(n)\\)\u306b\u524a\u6e1b\u3057\u3001\u5b9f\u884c\u6642\u52b9\u7387\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002

        \u8ffd\u52a0\u306e\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u7dad\u6301\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3059\u3002\u305d\u308c\u306b\u3082\u304b\u304b\u308f\u3089\u305a\u3001\u3053\u306e\u65b9\u6cd5\u306f\u5168\u4f53\u7684\u306b\u3088\u308a\u5747\u8861\u306e\u3068\u308c\u305f\u6642\u7a7a\u9593\u52b9\u7387\u3092\u6301\u3061\u3001\u3053\u306e\u554f\u984c\u306e\u6700\u9069\u89e3\u3068\u306a\u308a\u307e\u3059\u3002

        "},{"location":"chapter_searching/searching_algorithm_revisited/","title":"10.5 \u00a0 \u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u518d\u691c\u8a0e","text":"

        \u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff08\u691c\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff09\u306f\u3001\u914d\u5217\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u6728\u3001\u30b0\u30e9\u30d5\u306a\u3069\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u5185\u3067\u7279\u5b9a\u306e\u57fa\u6e96\u3092\u6e80\u305f\u30591\u3064\u4ee5\u4e0a\u306e\u8981\u7d20\u3092\u53d6\u5f97\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002

        \u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u305d\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u306b\u57fa\u3065\u3044\u3066\u4ee5\u4e0b\u306e2\u3064\u306e\u30ab\u30c6\u30b4\u30ea\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        • \u30c7\u30fc\u30bf\u69cb\u9020\u3092\u6a2a\u65ad\u3059\u308b\u3053\u3068\u3067\u76ee\u6a19\u8981\u7d20\u3092\u7279\u5b9a\u3059\u308b\uff1a\u914d\u5217\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3001\u6728\u3001\u30b0\u30e9\u30d5\u306e\u6a2a\u65ad\u306a\u3069\u3002
        • \u30c7\u30fc\u30bf\u306e\u7d44\u7e54\u69cb\u9020\u3084\u65e2\u5b58\u306e\u30c7\u30fc\u30bf\u3092\u4f7f\u7528\u3057\u3066\u52b9\u7387\u7684\u306a\u8981\u7d20\u63a2\u7d22\u3092\u5b9f\u73fe\u3059\u308b\uff1a\u4e8c\u5206\u63a2\u7d22\u3001\u30cf\u30c3\u30b7\u30e5\u63a2\u7d22\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u63a2\u7d22\u306a\u3069\u3002

        \u3053\u308c\u3089\u306e\u30c8\u30d4\u30c3\u30af\u306f\u524d\u306e\u7ae0\u3067\u7d39\u4ecb\u3055\u308c\u305f\u305f\u3081\u3001\u79c1\u305f\u3061\u306b\u306f\u99b4\u67d3\u307f\u306e\u306a\u3044\u3082\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u3088\u308a\u4f53\u7cfb\u7684\u306a\u89b3\u70b9\u304b\u3089\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u518d\u691c\u8a0e\u3057\u307e\u3059\u3002

        "},{"location":"chapter_searching/searching_algorithm_revisited/#1051","title":"10.5.1 \u00a0 \u7dcf\u5f53\u305f\u308a\u63a2\u7d22","text":"

        \u7dcf\u5f53\u305f\u308a\u63a2\u7d22\u306f\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u6a2a\u65ad\u3059\u308b\u3053\u3068\u3067\u76ee\u6a19\u8981\u7d20\u3092\u7279\u5b9a\u3057\u307e\u3059\u3002

        • \u300c\u7dda\u5f62\u63a2\u7d22\u300d\u306f\u914d\u5217\u3084\u9023\u7d50\u30ea\u30b9\u30c8\u306a\u3069\u306e\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u4e00\u7aef\u304b\u3089\u958b\u59cb\u3057\u3001\u76ee\u6a19\u8981\u7d20\u304c\u898b\u3064\u304b\u308b\u304b\u3001\u76ee\u6a19\u8981\u7d20\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u306a\u304f\u4ed6\u7aef\u306b\u5230\u9054\u3059\u308b\u307e\u3067\u3001\u5404\u8981\u7d20\u306b\u4e00\u3064\u305a\u3064\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002
        • \u300c\u5e45\u512a\u5148\u63a2\u7d22\u300d\u3068\u300c\u6df1\u3055\u512a\u5148\u63a2\u7d22\u300d\u306f\u3001\u30b0\u30e9\u30d5\u3068\u6728\u306e2\u3064\u306e\u6a2a\u65ad\u6226\u7565\u3067\u3059\u3002\u5e45\u512a\u5148\u63a2\u7d22\u306f\u521d\u671f\u30ce\u30fc\u30c9\u304b\u3089\u958b\u59cb\u3057\u3001\u5c64\u3054\u3068\uff08\u5de6\u304b\u3089\u53f3\u3078\uff09\u306b\u63a2\u7d22\u3057\u3001\u8fd1\u304f\u304b\u3089\u9060\u304f\u306e\u30ce\u30fc\u30c9\u306b\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002\u6df1\u3055\u512a\u5148\u63a2\u7d22\u306f\u521d\u671f\u30ce\u30fc\u30c9\u304b\u3089\u958b\u59cb\u3057\u3001\u30d1\u30b9\u306e\u7d42\u7aef\uff08\u4e0a\u304b\u3089\u4e0b\u3078\uff09\u307e\u3067\u8ffd\u8de1\u3057\u3001\u305d\u306e\u5f8c\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30af\u3057\u3066\u4ed6\u306e\u30d1\u30b9\u3092\u8a66\u3057\u3001\u30c7\u30fc\u30bf\u69cb\u9020\u5168\u4f53\u304c\u6a2a\u65ad\u3055\u308c\u308b\u307e\u3067\u7d9a\u884c\u3057\u307e\u3059\u3002

        \u7dcf\u5f53\u305f\u308a\u63a2\u7d22\u306e\u5229\u70b9\u306f\u3001\u305d\u306e\u5358\u7d14\u3055\u3068\u6c4e\u7528\u6027\u3067\u3042\u308a\u3001**\u30c7\u30fc\u30bf\u306e\u524d\u51e6\u7406\u3084\u8ffd\u52a0\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u52a9\u3051\u304c\u4e0d\u8981**\u3067\u3059\u3002

        \u305f\u3060\u3057\u3001**\u3053\u306e\u30bf\u30a4\u30d7\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)**\u3067\u3001\\(n\\)\u306f\u8981\u7d20\u6570\u3067\u3042\u308b\u305f\u3081\u3001\u5927\u898f\u6a21\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3067\u306f\u6027\u80fd\u304c\u60aa\u304f\u306a\u308a\u307e\u3059\u3002

        "},{"location":"chapter_searching/searching_algorithm_revisited/#1052","title":"10.5.2 \u00a0 \u9069\u5fdc\u7684\u63a2\u7d22","text":"

        \u9069\u5fdc\u7684\u63a2\u7d22\u306f\u3001\u30c7\u30fc\u30bf\u306e\u56fa\u6709\u306e\u6027\u8cea\uff08\u9806\u5e8f\u306a\u3069\uff09\u3092\u4f7f\u7528\u3057\u3066\u63a2\u7d22\u30d7\u30ed\u30bb\u30b9\u3092\u6700\u9069\u5316\u3057\u3001\u305d\u308c\u306b\u3088\u308a\u76ee\u6a19\u8981\u7d20\u3092\u3088\u308a\u52b9\u7387\u7684\u306b\u7279\u5b9a\u3057\u307e\u3059\u3002

        • \u300c\u4e8c\u5206\u63a2\u7d22\u300d\u306f\u30c7\u30fc\u30bf\u306e\u6574\u5217\u6027\u3092\u4f7f\u7528\u3057\u3066\u52b9\u7387\u7684\u306a\u63a2\u7d22\u3092\u5b9f\u73fe\u3057\u3001\u914d\u5217\u306b\u306e\u307f\u9069\u7528\u53ef\u80fd\u3067\u3059\u3002
        • \u300c\u30cf\u30c3\u30b7\u30e5\u63a2\u7d22\u300d\u306f\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u3092\u4f7f\u7528\u3057\u3066\u63a2\u7d22\u30c7\u30fc\u30bf\u3068\u76ee\u6a19\u30c7\u30fc\u30bf\u306e\u9593\u306b\u30ad\u30fc\u3068\u5024\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u3092\u78ba\u7acb\u3057\u3001\u305d\u308c\u306b\u3088\u308a\u30af\u30a8\u30ea\u64cd\u4f5c\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002
        • \u7279\u5b9a\u306e\u6728\u69cb\u9020\uff08\u4e8c\u5206\u63a2\u7d22\u6728\u306a\u3069\uff09\u3067\u306e\u300c\u6728\u63a2\u7d22\u300d\u306f\u3001\u30ce\u30fc\u30c9\u5024\u306e\u6bd4\u8f03\u306b\u57fa\u3065\u3044\u3066\u30ce\u30fc\u30c9\u3092\u8fc5\u901f\u306b\u9664\u5916\u3057\u3001\u305d\u308c\u306b\u3088\u308a\u76ee\u6a19\u8981\u7d20\u3092\u7279\u5b9a\u3057\u307e\u3059\u3002

        \u3053\u308c\u3089\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5229\u70b9\u306f\u9ad8\u52b9\u7387\u3067\u3042\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(\\log n)\\)\u307e\u305f\u306f\\(O(1)\\)\u306b\u307e\u3067\u9054\u3057\u307e\u3059\u3002

        \u305f\u3060\u3057\u3001\u3053\u308c\u3089\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u4f7f\u7528\u3059\u308b\u306b\u306f\u3001\u591a\u304f\u306e\u5834\u5408\u30c7\u30fc\u30bf\u306e\u524d\u51e6\u7406\u304c\u5fc5\u8981\u3067\u3059\u3002\u4f8b\u3048\u3070\u3001\u4e8c\u5206\u63a2\u7d22\u3067\u306f\u4e8b\u524d\u306b\u914d\u5217\u3092\u30bd\u30fc\u30c8\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u30cf\u30c3\u30b7\u30e5\u63a2\u7d22\u3068\u6728\u63a2\u7d22\u306e\u4e21\u65b9\u3067\u8ffd\u52a0\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u52a9\u3051\u304c\u5fc5\u8981\u3067\u3059\u3002\u3053\u308c\u3089\u306e\u69cb\u9020\u3092\u7dad\u6301\u3059\u308b\u3053\u3068\u3082\u3001\u6642\u9593\u3068\u7a7a\u9593\u306e\u9762\u3067\u3088\u308a\u591a\u304f\u306e\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u304c\u5fc5\u8981\u3067\u3059\u3002

        Tip

        \u9069\u5fdc\u7684\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u591a\u304f\u306e\u5834\u5408\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u547c\u3070\u308c\u3001\u4e3b\u306b\u7279\u5b9a\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u5185\u3067\u76ee\u6a19\u8981\u7d20\u3092\u8fc5\u901f\u306b\u53d6\u5f97\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002

        "},{"location":"chapter_searching/searching_algorithm_revisited/#1053","title":"10.5.3 \u00a0 \u63a2\u7d22\u65b9\u6cd5\u306e\u9078\u629e","text":"

        \u30b5\u30a4\u30ba\\(n\\)\u306e\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u7dda\u5f62\u63a2\u7d22\u3001\u4e8c\u5206\u63a2\u7d22\u3001\u6728\u63a2\u7d22\u3001\u30cf\u30c3\u30b7\u30e5\u63a2\u7d22\u3001\u307e\u305f\u306f\u305d\u306e\u4ed6\u306e\u65b9\u6cd5\u3092\u4f7f\u7528\u3057\u3066\u76ee\u6a19\u8981\u7d20\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u65b9\u6cd5\u306e\u52d5\u4f5c\u539f\u7406\u3092\u4e0b\u56f3\u306b\u793a\u3057\u307e\u3059\u3002

        \u56f3 10-11 \u00a0 Various search strategies

        \u524d\u8ff0\u306e\u65b9\u6cd5\u306e\u7279\u6027\u3068\u64cd\u4f5c\u52b9\u7387\u3092\u4ee5\u4e0b\u306e\u8868\u306b\u793a\u3057\u307e\u3059\u3002

        \u8868 10-1 \u00a0 \u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u52b9\u7387\u306e\u6bd4\u8f03

        \u7dda\u5f62\u63a2\u7d22 \u4e8c\u5206\u63a2\u7d22 \u6728\u63a2\u7d22 \u30cf\u30c3\u30b7\u30e5\u63a2\u7d22 \u8981\u7d20\u63a2\u7d22 \\(O(n)\\) \\(O(\\log n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u8981\u7d20\u633f\u5165 \\(O(1)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u8981\u7d20\u524a\u9664 \\(O(n)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u8ffd\u52a0\u7a7a\u9593 \\(O(1)\\) \\(O(1)\\) \\(O(n)\\) \\(O(n)\\) \u30c7\u30fc\u30bf\u524d\u51e6\u7406 / \u30bd\u30fc\u30c8 \\(O(n \\log n)\\) \u6728\u69cb\u7bc9 \\(O(n \\log n)\\) \u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u69cb\u7bc9 \\(O(n)\\) \u30c7\u30fc\u30bf\u9806\u5e8f\u6027 \u7121\u9806\u5e8f \u9806\u5e8f \u9806\u5e8f \u7121\u9806\u5e8f

        \u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u9078\u629e\u306f\u3001\u30c7\u30fc\u30bf\u91cf\u3001\u63a2\u7d22\u6027\u80fd\u8981\u4ef6\u3001\u30c7\u30fc\u30bf\u30af\u30a8\u30ea\u3068\u66f4\u65b0\u306e\u983b\u5ea6\u306a\u3069\u306b\u3082\u4f9d\u5b58\u3057\u307e\u3059\u3002

        \u7dda\u5f62\u63a2\u7d22

        • \u6c4e\u7528\u6027\u304c\u826f\u304f\u3001\u30c7\u30fc\u30bf\u524d\u51e6\u7406\u64cd\u4f5c\u304c\u4e0d\u8981\u3067\u3059\u3002\u30c7\u30fc\u30bf\u3092\u4e00\u5ea6\u3060\u3051\u30af\u30a8\u30ea\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u3001\u4ed6\u306e3\u3064\u306e\u65b9\u6cd5\u306e\u30c7\u30fc\u30bf\u524d\u51e6\u7406\u6642\u9593\u306f\u7dda\u5f62\u63a2\u7d22\u306e\u6642\u9593\u3088\u308a\u3082\u9577\u304f\u306a\u308a\u307e\u3059\u3002
        • \u5c0f\u5bb9\u91cf\u306e\u30c7\u30fc\u30bf\u306b\u9069\u3057\u3066\u304a\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u304c\u52b9\u7387\u306b\u4e0e\u3048\u308b\u5f71\u97ff\u306f\u5c0f\u3055\u3044\u3067\u3059\u3002
        • \u30c7\u30fc\u30bf\u66f4\u65b0\u304c\u975e\u5e38\u306b\u983b\u7e41\u306a\u30b7\u30ca\u30ea\u30aa\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u65b9\u6cd5\u306f\u30c7\u30fc\u30bf\u306e\u8ffd\u52a0\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u3092\u5fc5\u8981\u3068\u3057\u306a\u3044\u305f\u3081\u3067\u3059\u3002

        \u4e8c\u5206\u63a2\u7d22

        • \u3088\u308a\u5927\u304d\u306a\u30c7\u30fc\u30bf\u91cf\u306b\u9069\u3057\u3066\u304a\u308a\u3001\u5b89\u5b9a\u3057\u305f\u6027\u80fd\u3068\u6700\u60aa\u30b1\u30fc\u30b9\u306e\u6642\u9593\u8a08\u7b97\u91cf\\(O(\\log n)\\)\u3092\u6301\u3061\u307e\u3059\u3002
        • \u305f\u3060\u3057\u3001\u30c7\u30fc\u30bf\u91cf\u304c\u5927\u304d\u3059\u304e\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002\u914d\u5217\u306e\u4fdd\u5b58\u306b\u306f\u9023\u7d9a\u3057\u305f\u30e1\u30e2\u30ea\u7a7a\u9593\u304c\u5fc5\u8981\u3060\u304b\u3089\u3067\u3059\u3002
        • \u983b\u7e41\u306a\u8ffd\u52a0\u3068\u524a\u9664\u304c\u3042\u308b\u30b7\u30ca\u30ea\u30aa\u306b\u306f\u9069\u3057\u3066\u3044\u307e\u305b\u3093\u3002\u9806\u5e8f\u4ed8\u304d\u914d\u5217\u306e\u7dad\u6301\u306b\u591a\u304f\u306e\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u304c\u767a\u751f\u3059\u308b\u305f\u3081\u3067\u3059\u3002

        \u30cf\u30c3\u30b7\u30e5\u63a2\u7d22

        • \u9ad8\u901f\u30af\u30a8\u30ea\u6027\u80fd\u304c\u4e0d\u53ef\u6b20\u306a\u30b7\u30ca\u30ea\u30aa\u306b\u9069\u3057\u3066\u304a\u308a\u3001\u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u3067\u3059\u3002
        • \u9806\u5e8f\u4ed8\u304d\u30c7\u30fc\u30bf\u3084\u7bc4\u56f2\u63a2\u7d22\u304c\u5fc5\u8981\u306a\u30b7\u30ca\u30ea\u30aa\u306b\u306f\u9069\u3057\u3066\u3044\u307e\u305b\u3093\u3002\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306f\u30c7\u30fc\u30bf\u306e\u9806\u5e8f\u6027\u3092\u7dad\u6301\u3067\u304d\u306a\u3044\u305f\u3081\u3067\u3059\u3002
        • \u30cf\u30c3\u30b7\u30e5\u95a2\u6570\u3068\u30cf\u30c3\u30b7\u30e5\u885d\u7a81\u51e6\u7406\u6226\u7565\u3078\u306e\u4f9d\u5b58\u5ea6\u304c\u9ad8\u304f\u3001\u6027\u80fd\u52a3\u5316\u306e\u30ea\u30b9\u30af\u304c\u5927\u304d\u3044\u3067\u3059\u3002
        • \u904e\u5ea6\u306b\u5927\u5bb9\u91cf\u306e\u30c7\u30fc\u30bf\u306b\u306f\u9069\u3057\u3066\u3044\u307e\u305b\u3093\u3002\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306f\u885d\u7a81\u3092\u6700\u5c0f\u5316\u3057\u3001\u826f\u597d\u306a\u30af\u30a8\u30ea\u6027\u80fd\u3092\u63d0\u4f9b\u3059\u308b\u305f\u3081\u306b\u8ffd\u52a0\u306e\u7a7a\u9593\u304c\u5fc5\u8981\u3060\u304b\u3089\u3067\u3059\u3002

        \u6728\u63a2\u7d22

        • \u5927\u5bb9\u91cf\u30c7\u30fc\u30bf\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u6728\u30ce\u30fc\u30c9\u306f\u30e1\u30e2\u30ea\u5185\u306b\u5206\u6563\u3057\u3066\u4fdd\u5b58\u3055\u308c\u308b\u305f\u3081\u3067\u3059\u3002
        • \u9806\u5e8f\u4ed8\u304d\u30c7\u30fc\u30bf\u306e\u7dad\u6301\u3084\u7bc4\u56f2\u63a2\u7d22\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002
        • \u30ce\u30fc\u30c9\u306e\u7d99\u7d9a\u7684\u306a\u8ffd\u52a0\u3068\u524a\u9664\u306b\u3088\u308a\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u306f\u504f\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(n)\\)\u306b\u52a3\u5316\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        • AVL\u6728\u3084\u8d64\u9ed2\u6728\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001\u64cd\u4f5c\u306f\\(O(\\log n)\\)\u52b9\u7387\u3067\u5b89\u5b9a\u3057\u3066\u5b9f\u884c\u3067\u304d\u307e\u3059\u304c\u3001\u6728\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u7dad\u6301\u3059\u308b\u64cd\u4f5c\u306b\u3088\u308a\u8ffd\u52a0\u306e\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u304c\u8ffd\u52a0\u3055\u308c\u307e\u3059\u3002
        "},{"location":"chapter_searching/summary/","title":"10.6 \u00a0 \u307e\u3068\u3081","text":"
        • \u4e8c\u5206\u63a2\u7d22\u306f\u30c7\u30fc\u30bf\u306e\u9806\u5e8f\u306b\u4f9d\u5b58\u3057\u3001\u63a2\u7d22\u533a\u9593\u3092\u53cd\u5fa9\u7684\u306b\u534a\u5206\u306b\u3059\u308b\u3053\u3068\u3067\u63a2\u7d22\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002\u5165\u529b\u30c7\u30fc\u30bf\u304c\u30bd\u30fc\u30c8\u6e08\u307f\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u914d\u5217\u307e\u305f\u306f\u914d\u5217\u30d9\u30fc\u30b9\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u306e\u307f\u9069\u7528\u53ef\u80fd\u3067\u3059\u3002
        • \u7121\u9806\u5e8f\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u5185\u306e\u30a8\u30f3\u30c8\u30ea\u3092\u898b\u3064\u3051\u308b\u306b\u306f\u3001\u7dcf\u5f53\u305f\u308a\u63a2\u7d22\u304c\u5fc5\u8981\u306a\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u57fa\u3065\u3044\u3066\u7570\u306a\u308b\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u9069\u7528\u3067\u304d\u307e\u3059\uff1a\u7dda\u5f62\u63a2\u7d22\u306f\u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u9069\u3057\u3066\u304a\u308a\u3001\u5e45\u512a\u5148\u63a2\u7d22\uff08BFS\uff09\u3068\u6df1\u3055\u512a\u5148\u63a2\u7d22\uff08DFS\uff09\u306f\u30b0\u30e9\u30d5\u3068\u6728\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u975e\u5e38\u306b\u6c4e\u7528\u6027\u304c\u9ad8\u304f\u3001\u30c7\u30fc\u30bf\u306e\u524d\u51e6\u7406\u304c\u4e0d\u8981\u3067\u3059\u304c\u3001\\(O(n)\\)\u3068\u3044\u3046\u9ad8\u3044\u6642\u9593\u8a08\u7b97\u91cf\u3092\u6301\u3061\u307e\u3059\u3002
        • \u30cf\u30c3\u30b7\u30e5\u63a2\u7d22\u3001\u6728\u63a2\u7d22\u3001\u4e8c\u5206\u63a2\u7d22\u306f\u52b9\u7387\u7684\u306a\u63a2\u7d22\u65b9\u6cd5\u3067\u3001\u7279\u5b9a\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u5185\u3067\u76ee\u6a19\u8981\u7d20\u3092\u8fc5\u901f\u306b\u7279\u5b9a\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3001\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(\\log n)\\)\u307e\u305f\u306f\\(O(1)\\)\u306b\u307e\u3067\u9054\u3057\u307e\u3059\u304c\u3001\u901a\u5e38\u306f\u8ffd\u52a0\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u53ce\u5bb9\u3059\u308b\u305f\u3081\u306b\u8ffd\u52a0\u306e\u7a7a\u9593\u304c\u5fc5\u8981\u3067\u3059\u3002
        • \u5b9f\u969b\u306b\u306f\u3001\u30c7\u30fc\u30bf\u91cf\u3001\u63a2\u7d22\u6027\u80fd\u8981\u4ef6\u3001\u30c7\u30fc\u30bf\u30af\u30a8\u30ea\u3068\u66f4\u65b0\u983b\u5ea6\u306a\u3069\u306e\u8981\u56e0\u3092\u5206\u6790\u3057\u3066\u3001\u9069\u5207\u306a\u63a2\u7d22\u65b9\u6cd5\u3092\u9078\u629e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u7dda\u5f62\u63a2\u7d22\u306f\u5c0f\u3055\u306a\u30c7\u30fc\u30bf\u3084\u983b\u7e41\u306b\u66f4\u65b0\u3055\u308c\u308b\uff08\u5909\u52d5\u6027\u306e\u9ad8\u3044\uff09\u30c7\u30fc\u30bf\u306b\u7406\u60f3\u7684\u3067\u3059\u3002\u4e8c\u5206\u63a2\u7d22\u306f\u5927\u304d\u304f\u3066\u30bd\u30fc\u30c8\u6e08\u307f\u306e\u30c7\u30fc\u30bf\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u30cf\u30c3\u30b7\u30e5\u63a2\u7d22\u306f\u9ad8\u3044\u30af\u30a8\u30ea\u52b9\u7387\u304c\u5fc5\u8981\u3067\u7bc4\u56f2\u30af\u30a8\u30ea\u304c\u4e0d\u8981\u306a\u30c7\u30fc\u30bf\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u6728\u63a2\u7d22\u306f\u9806\u5e8f\u3092\u7dad\u6301\u3057\u3001\u7bc4\u56f2\u30af\u30a8\u30ea\u3092\u30b5\u30dd\u30fc\u30c8\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5927\u304d\u306a\u52d5\u7684\u30c7\u30fc\u30bf\u306b\u6700\u3082\u9069\u3057\u3066\u3044\u307e\u3059\u3002
        • \u7dda\u5f62\u63a2\u7d22\u3092\u30cf\u30c3\u30b7\u30e5\u63a2\u7d22\u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u306f\u3001\u5b9f\u884c\u6642\u6027\u80fd\u3092\u6700\u9069\u5316\u3059\u308b\u4e00\u822c\u7684\u306a\u6226\u7565\u3067\u3001\u6642\u9593\u8a08\u7b97\u91cf\u3092\\(O(n)\\)\u304b\u3089\\(O(1)\\)\u306b\u524a\u6e1b\u3057\u307e\u3059\u3002
        "},{"location":"chapter_sorting/","title":"\u7b2c 11 \u7ae0 \u00a0 \u30bd\u30fc\u30c8","text":"

        Abstract

        \u30bd\u30fc\u30c8\u306f\u6df7\u6c8c\u3092\u79e9\u5e8f\u306b\u5909\u3048\u308b\u9b54\u6cd5\u306e\u9375\u306e\u3088\u3046\u306a\u3082\u306e\u3067\u3001\u30c7\u30fc\u30bf\u3092\u3088\u308a\u52b9\u7387\u7684\u306b\u7406\u89e3\u3057\u51e6\u7406\u3059\u308b\u3053\u3068\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002

        \u5358\u7d14\u306a\u6607\u9806\u3067\u3042\u308d\u3046\u3068\u8907\u96d1\u306a\u30ab\u30c6\u30b4\u30ea\u914d\u5217\u3067\u3042\u308d\u3046\u3068\u3001\u30bd\u30fc\u30c8\u306f\u30c7\u30fc\u30bf\u306e\u8abf\u548c\u7f8e\u3092\u660e\u3089\u304b\u306b\u3057\u307e\u3059\u3002

        "},{"location":"chapter_sorting/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 11.1 \u00a0 \u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0
        • 11.2 \u00a0 \u9078\u629e\u30bd\u30fc\u30c8
        • 11.3 \u00a0 \u30d0\u30d6\u30eb\u30bd\u30fc\u30c8
        • 11.4 \u00a0 \u633f\u5165\u30bd\u30fc\u30c8
        • 11.5 \u00a0 \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8
        • 11.6 \u00a0 \u30de\u30fc\u30b8\u30bd\u30fc\u30c8
        • 11.7 \u00a0 \u30d2\u30fc\u30d7\u30bd\u30fc\u30c8
        • 11.8 \u00a0 \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8
        • 11.9 \u00a0 \u8a08\u6570\u30bd\u30fc\u30c8
        • 11.10 \u00a0 \u57fa\u6570\u30bd\u30fc\u30c8
        • 11.11 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_sorting/bubble_sort/","title":"11.3 \u00a0 \u30d0\u30d6\u30eb\u30bd\u30fc\u30c8","text":"

        \u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u306f\u3001\u96a3\u63a5\u3059\u308b\u8981\u7d20\u3092\u7d99\u7d9a\u7684\u306b\u6bd4\u8f03\u3057\u4ea4\u63db\u3059\u308b\u3053\u3068\u3067\u52d5\u4f5c\u3057\u307e\u3059\u3002\u3053\u306e\u30d7\u30ed\u30bb\u30b9\u306f\u6ce1\u304c\u5e95\u304b\u3089\u4e0a\u306b\u4e0a\u6607\u3059\u308b\u3088\u3046\u306a\u3082\u306e\u306a\u306e\u3067\u3001\u300c\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u300d\u3068\u540d\u4ed8\u3051\u3089\u308c\u307e\u3057\u305f\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30d0\u30d6\u30ea\u30f3\u30b0\u30d7\u30ed\u30bb\u30b9\u306f\u8981\u7d20\u4ea4\u63db\u3092\u4f7f\u7528\u3057\u3066\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\u3067\u304d\u307e\u3059\uff1a\u914d\u5217\u306e\u5de6\u7aef\u304b\u3089\u958b\u59cb\u3057\u3066\u53f3\u306b\u79fb\u52d5\u3057\u3001\u96a3\u63a5\u3059\u308b\u8981\u7d20\u306e\u5404\u30da\u30a2\u3092\u6bd4\u8f03\u3057\u307e\u3059\u3002\u5de6\u306e\u8981\u7d20\u304c\u53f3\u306e\u8981\u7d20\u3088\u308a\u5927\u304d\u3044\u5834\u5408\u306f\u3001\u305d\u308c\u3089\u3092\u4ea4\u63db\u3057\u307e\u3059\u3002\u6a2a\u65ad\u5f8c\u3001\u6700\u5927\u8981\u7d20\u306f\u914d\u5217\u306e\u53f3\u7aef\u306b\u30d0\u30d6\u30eb\u30a2\u30c3\u30d7\u3057\u307e\u3059\u3002

        <1><2><3><4><5><6><7>

        \u56f3 11-4 \u00a0 Simulating bubble process using element swap

        "},{"location":"chapter_sorting/bubble_sort/#1131","title":"11.3.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d7\u30ed\u30bb\u30b9","text":"

        \u914d\u5217\u306e\u9577\u3055\u3092\\(n\\)\u3068\u3057\u307e\u3059\u3002\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u306e\u30b9\u30c6\u30c3\u30d7\u306f\u4e0b\u56f3\u306b\u793a\u3055\u308c\u307e\u3059\uff1a

        1. \u307e\u305a\u3001\\(n\\)\u500b\u306e\u8981\u7d20\u306b\u5bfe\u3057\u30661\u56de\u306e\u300c\u30d0\u30d6\u30eb\u300d\u30d1\u30b9\u3092\u5b9f\u884c\u3057\u3001\u6700\u5927\u8981\u7d20\u3092\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u4ea4\u63db\u3057\u307e\u3059\u3002
        2. \u6b21\u306b\u3001\u6b8b\u308a\u306e\\(n - 1\\)\u500b\u306e\u8981\u7d20\u306b\u5bfe\u3057\u3066\u300c\u30d0\u30d6\u30eb\u300d\u30d1\u30b9\u3092\u5b9f\u884c\u3057\u30012\u756a\u76ee\u306b\u5927\u304d\u3044\u8981\u7d20\u3092\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u4ea4\u63db\u3057\u307e\u3059\u3002
        3. \u3053\u306e\u65b9\u6cd5\u3067\u7d9a\u884c\u3057\u307e\u3059\uff1b\\(n - 1\\)\u56de\u306e\u30d1\u30b9\u306e\u5f8c\u3001\u6700\u5927\\(n - 1\\)\u500b\u306e\u8981\u7d20\u304c\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u79fb\u52d5\u3055\u308c\u307e\u3059\u3002
        4. \u6b8b\u308a\u306e\u552f\u4e00\u306e\u8981\u7d20\u306f**\u5fc5\u305a**\u6700\u5c0f\u3067\u3042\u308b\u305f\u3081\u3001**\u3055\u3089\u306a\u308b**\u30bd\u30fc\u30c8\u306f\u5fc5\u8981\u3042\u308a\u307e\u305b\u3093\u3002\u3053\u306e\u6642\u70b9\u3067\u3001\u914d\u5217\u306f\u30bd\u30fc\u30c8\u3055\u308c\u307e\u3059\u3002

        \u56f3 11-5 \u00a0 Bubble sort process

        \u30b3\u30fc\u30c9\u4f8b\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby bubble_sort.py
        def bubble_sort(nums: list[int]):\n    \"\"\"\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\"\"\"\n    n = len(nums)\n    # \u5916\u5074\u306e\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f [0, i]\n    for i in range(n - 1, 0, -1):\n        # \u5185\u5074\u306e\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2 [0, i] \u306e\u6700\u5927\u8981\u7d20\u3092\u7bc4\u56f2\u306e\u53f3\u7aef\u306b\u79fb\u52d5\n        for j in range(i):\n            if nums[j] > nums[j + 1]:\n                # nums[j] \u3068 nums[j + 1] \u3092\u4ea4\u63db\n                nums[j], nums[j + 1] = nums[j + 1], nums[j]\n
        bubble_sort.cpp
        /* \u30d0\u30d6\u30eb\u30bd\u30fc\u30c8 */\nvoid bubbleSort(vector<int> &nums) {\n    // \u5916\u5074\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f[0, i]\n    for (int i = nums.size() - 1; i > 0; i--) {\n        // \u5185\u5074\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2[0, i]\u5185\u306e\u6700\u5927\u8981\u7d20\u3092\u7bc4\u56f2\u306e\u53f3\u7aef\u306b\u4ea4\u63db\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // nums[j]\u3068nums[j + 1]\u3092\u4ea4\u63db\n                // \u3053\u3053\u3067\u306fstd\u306eswap\u3092\u4f7f\u7528\n                swap(nums[j], nums[j + 1]);\n            }\n        }\n    }\n}\n
        bubble_sort.java
        /* \u30d0\u30d6\u30eb\u30bd\u30fc\u30c8 */\nvoid bubbleSort(int[] nums) {\n    // \u5916\u5074\u30eb\u30fc\u30d7: \u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f [0, i]\n    for (int i = nums.length - 1; i > 0; i--) {\n        // \u5185\u5074\u30eb\u30fc\u30d7: \u672a\u30bd\u30fc\u30c8\u7bc4\u56f2 [0, i] \u306e\u6700\u5927\u8981\u7d20\u3092\u7bc4\u56f2\u306e\u53f3\u7aef\u306b\u4ea4\u63db\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // nums[j] \u3068 nums[j + 1] \u3092\u4ea4\u63db\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n            }\n        }\n    }\n}\n
        bubble_sort.cs
        [class]{bubble_sort}-[func]{BubbleSort}\n
        bubble_sort.go
        [class]{}-[func]{bubbleSort}\n
        bubble_sort.swift
        [class]{}-[func]{bubbleSort}\n
        bubble_sort.js
        [class]{}-[func]{bubbleSort}\n
        bubble_sort.ts
        [class]{}-[func]{bubbleSort}\n
        bubble_sort.dart
        [class]{}-[func]{bubbleSort}\n
        bubble_sort.rs
        [class]{}-[func]{bubble_sort}\n
        bubble_sort.c
        [class]{}-[func]{bubbleSort}\n
        bubble_sort.kt
        [class]{}-[func]{bubbleSort}\n
        bubble_sort.rb
        [class]{}-[func]{bubble_sort}\n
        "},{"location":"chapter_sorting/bubble_sort/#1132","title":"11.3.2 \u00a0 \u52b9\u7387\u306e\u6700\u9069\u5316","text":"

        \u300c\u30d0\u30d6\u30ea\u30f3\u30b0\u300d\u306e\u30e9\u30a6\u30f3\u30c9\u4e2d\u306b\u4ea4\u63db\u304c\u767a\u751f\u3057\u306a\u3044\u5834\u5408\u3001\u914d\u5217\u306f\u3059\u3067\u306b\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u3059\u3050\u306b\u623b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u3092\u691c\u51fa\u3059\u308b\u305f\u3081\u306b\u3001flag\u5909\u6570\u3092\u8ffd\u52a0\u3067\u304d\u307e\u3059\uff1b\u30d1\u30b9\u3067\u4ea4\u63db\u304c\u884c\u308f\u308c\u306a\u3044\u5834\u5408\u306f\u3001\u30d5\u30e9\u30b0\u3092\u8a2d\u5b9a\u3057\u3066\u65e9\u671f\u306b\u623b\u308a\u307e\u3059\u3002

        \u3053\u306e\u6700\u9069\u5316\u304c\u3042\u3063\u3066\u3082\u3001\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u306e\u6700\u60aa\u6642\u9593\u8a08\u7b97\u91cf\u3068\u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n^2)\\)\u306e\u307e\u307e\u3067\u3059\u3002\u305f\u3060\u3057\u3001\u5165\u529b\u914d\u5217\u304c\u3059\u3067\u306b\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u6700\u826f\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u307e\u3067\u4f4e\u304f\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby bubble_sort.py
        def bubble_sort_with_flag(nums: list[int]):\n    \"\"\"\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\uff08\u30d5\u30e9\u30b0\u306b\u3088\u308b\u6700\u9069\u5316\uff09\"\"\"\n    n = len(nums)\n    # \u5916\u5074\u306e\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f [0, i]\n    for i in range(n - 1, 0, -1):\n        flag = False  # \u30d5\u30e9\u30b0\u3092\u521d\u671f\u5316\n        # \u5185\u5074\u306e\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2 [0, i] \u306e\u6700\u5927\u8981\u7d20\u3092\u7bc4\u56f2\u306e\u53f3\u7aef\u306b\u79fb\u52d5\n        for j in range(i):\n            if nums[j] > nums[j + 1]:\n                # nums[j] \u3068 nums[j + 1] \u3092\u4ea4\u63db\n                nums[j], nums[j + 1] = nums[j + 1], nums[j]\n                flag = True  # \u8981\u7d20\u3092\u4ea4\u63db\u3057\u305f\u3053\u3068\u3092\u8a18\u9332\n        if not flag:\n            break  # \u3053\u306e\u56de\u306e\u300c\u30d0\u30d6\u30ea\u30f3\u30b0\u300d\u3067\u8981\u7d20\u304c\u4ea4\u63db\u3055\u308c\u306a\u304b\u3063\u305f\u5834\u5408\u3001\u7d42\u4e86\n
        bubble_sort.cpp
        /* \u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\uff08\u30d5\u30e9\u30b0\u6700\u9069\u5316\u7248\uff09*/\nvoid bubbleSortWithFlag(vector<int> &nums) {\n    // \u5916\u5074\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f[0, i]\n    for (int i = nums.size() - 1; i > 0; i--) {\n        bool flag = false; // \u30d5\u30e9\u30b0\u3092\u521d\u671f\u5316\n        // \u5185\u5074\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2[0, i]\u5185\u306e\u6700\u5927\u8981\u7d20\u3092\u7bc4\u56f2\u306e\u53f3\u7aef\u306b\u4ea4\u63db\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // nums[j]\u3068nums[j + 1]\u3092\u4ea4\u63db\n                // \u3053\u3053\u3067\u306fstd\u306eswap\u3092\u4f7f\u7528\n                swap(nums[j], nums[j + 1]);\n                flag = true; // \u4ea4\u63db\u3055\u308c\u305f\u8981\u7d20\u3092\u8a18\u9332\n            }\n        }\n        if (!flag)\n            break; // \u3053\u306e\u56de\u306e\u300c\u30d0\u30d6\u30ea\u30f3\u30b0\u300d\u3067\u8981\u7d20\u304c\u4ea4\u63db\u3055\u308c\u306a\u304b\u3063\u305f\u5834\u5408\u3001\u7d42\u4e86\n    }\n}\n
        bubble_sort.java
        /* \u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\uff08\u30d5\u30e9\u30b0\u306b\u3088\u308b\u6700\u9069\u5316\uff09 */\nvoid bubbleSortWithFlag(int[] nums) {\n    // \u5916\u5074\u30eb\u30fc\u30d7: \u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f [0, i]\n    for (int i = nums.length - 1; i > 0; i--) {\n        boolean flag = false; // \u30d5\u30e9\u30b0\u3092\u521d\u671f\u5316\n        // \u5185\u5074\u30eb\u30fc\u30d7: \u672a\u30bd\u30fc\u30c8\u7bc4\u56f2 [0, i] \u306e\u6700\u5927\u8981\u7d20\u3092\u7bc4\u56f2\u306e\u53f3\u7aef\u306b\u4ea4\u63db\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // nums[j] \u3068 nums[j + 1] \u3092\u4ea4\u63db\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                flag = true; // \u4ea4\u63db\u3055\u308c\u305f\u8981\u7d20\u3092\u8a18\u9332\n            }\n        }\n        if (!flag)\n            break; // \u3053\u306e\u300c\u30d0\u30d6\u30ea\u30f3\u30b0\u300d\u30e9\u30a6\u30f3\u30c9\u3067\u8981\u7d20\u304c\u4ea4\u63db\u3055\u308c\u306a\u304b\u3063\u305f\u5834\u5408\u3001\u7d42\u4e86\n    }\n}\n
        bubble_sort.cs
        [class]{bubble_sort}-[func]{BubbleSortWithFlag}\n
        bubble_sort.go
        [class]{}-[func]{bubbleSortWithFlag}\n
        bubble_sort.swift
        [class]{}-[func]{bubbleSortWithFlag}\n
        bubble_sort.js
        [class]{}-[func]{bubbleSortWithFlag}\n
        bubble_sort.ts
        [class]{}-[func]{bubbleSortWithFlag}\n
        bubble_sort.dart
        [class]{}-[func]{bubbleSortWithFlag}\n
        bubble_sort.rs
        [class]{}-[func]{bubble_sort_with_flag}\n
        bubble_sort.c
        [class]{}-[func]{bubbleSortWithFlag}\n
        bubble_sort.kt
        [class]{}-[func]{bubbleSortWithFlag}\n
        bubble_sort.rb
        [class]{}-[func]{bubble_sort_with_flag}\n
        "},{"location":"chapter_sorting/bubble_sort/#1133","title":"11.3.3 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7279\u6027","text":"
        • \\(O(n^2)\\)\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3001\u9069\u5fdc\u30bd\u30fc\u30c8\u3002 \u5404\u300c\u30d0\u30d6\u30ea\u30f3\u30b0\u300d\u30e9\u30a6\u30f3\u30c9\u306f\u9577\u3055\\(n - 1\\)\u3001\\(n - 2\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\)\u306e\u914d\u5217\u30bb\u30b0\u30e1\u30f3\u30c8\u3092\u6a2a\u65ad\u3057\u3001\u5408\u8a08\u306f\\((n - 1) n / 2\\)\u3068\u306a\u308a\u307e\u3059\u3002flag\u6700\u9069\u5316\u306b\u3088\u308a\u3001\u914d\u5217\u304c\u3059\u3067\u306b\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u6700\u826f\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u306b\u9054\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        • \\(O(1)\\)\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u3001\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u30bd\u30fc\u30c8\u3002 \u30dd\u30a4\u30f3\u30bf\\(i\\)\u3068\\(j\\)\u306b\u3088\u3063\u3066\u5b9a\u6570\u91cf\u306e\u8ffd\u52a0\u7a7a\u9593\u306e\u307f\u304c\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        • \u5b89\u5b9a\u30bd\u30fc\u30c8\u3002 \u7b49\u3057\u3044\u8981\u7d20\u306f\u300c\u30d0\u30d6\u30ea\u30f3\u30b0\u300d\u4e2d\u306b\u4ea4\u63db\u3055\u308c\u306a\u3044\u305f\u3081\u3001\u5143\u306e\u9806\u5e8f\u304c\u4fdd\u6301\u3055\u308c\u3001\u3053\u308c\u306f\u5b89\u5b9a\u30bd\u30fc\u30c8\u306b\u306a\u308a\u307e\u3059\u3002
        "},{"location":"chapter_sorting/bucket_sort/","title":"11.8 \u00a0 \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8","text":"

        \u524d\u8ff0\u306e\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3059\u3079\u3066\u300c\u6bd4\u8f03\u30d9\u30fc\u30b9\u306e\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u300d\u3067\u3001\u5024\u3092\u6bd4\u8f03\u3059\u308b\u3053\u3068\u3067\u8981\u7d20\u3092\u30bd\u30fc\u30c8\u3057\u307e\u3059\u3002\u3053\u306e\u3088\u3046\u306a\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f \\(O(n \\log n)\\) \u3088\u308a\u826f\u3044\u6642\u9593\u8a08\u7b97\u91cf\u3092\u6301\u3064\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002\u6b21\u306b\u3001\u7dda\u5f62\u6642\u9593\u8a08\u7b97\u91cf\u3092\u9054\u6210\u3067\u304d\u308b\u3044\u304f\u3064\u304b\u306e\u300c\u975e\u6bd4\u8f03\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u300d\u306b\u3064\u3044\u3066\u8b70\u8ad6\u3057\u307e\u3059\u3002

        \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306f\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306e\u5178\u578b\u7684\u306a\u5fdc\u7528\u3067\u3059\u3002\u4e00\u9023\u306e\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u30d0\u30b1\u30c3\u30c8\u3092\u8a2d\u5b9a\u3057\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u304c\u30c7\u30fc\u30bf\u306e\u7bc4\u56f2\u3092\u542b\u307f\u3001\u5165\u529b\u30c7\u30fc\u30bf\u3092\u3053\u308c\u3089\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u5747\u7b49\u306b\u5206\u6563\u3055\u305b\u308b\u3053\u3068\u3067\u52d5\u4f5c\u3057\u307e\u3059\u3002\u305d\u3057\u3066\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u5185\u306e\u30c7\u30fc\u30bf\u3092\u500b\u5225\u306b\u30bd\u30fc\u30c8\u3057\u307e\u3059\u3002\u6700\u5f8c\u306b\u3001\u3059\u3079\u3066\u306e\u30d0\u30b1\u30c3\u30c8\u304b\u3089\u306e\u30bd\u30fc\u30c8\u6e08\u307f\u30c7\u30fc\u30bf\u3092\u9806\u6b21\u30de\u30fc\u30b8\u3057\u3066\u6700\u7d42\u7d50\u679c\u3092\u751f\u6210\u3057\u307e\u3059\u3002

        "},{"location":"chapter_sorting/bucket_sort/#1181","title":"11.8.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u904e\u7a0b","text":"

        \u9577\u3055 \\(n\\) \u306e\u914d\u5217\u3067\u3001\\([0, 1)\\) \u306e\u7bc4\u56f2\u306e\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u3092\u8003\u3048\u3066\u307f\u307e\u3059\u3002\u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306e\u904e\u7a0b\u306f\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        1. \\(k\\) \u500b\u306e\u30d0\u30b1\u30c3\u30c8\u3092\u521d\u671f\u5316\u3057\u3001\\(n\\) \u500b\u306e\u8981\u7d20\u3092\u3053\u308c\u3089\u306e \\(k\\) \u500b\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u6563\u3055\u305b\u307e\u3059\u3002
        2. \u5404\u30d0\u30b1\u30c3\u30c8\u3092\u500b\u5225\u306b\u30bd\u30fc\u30c8\u3057\u307e\u3059\uff08\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u7d44\u307f\u8fbc\u307f\u30bd\u30fc\u30c8\u95a2\u6570\u3092\u4f7f\u7528\uff09\u3002
        3. \u6700\u5c0f\u304b\u3089\u6700\u5927\u306e\u30d0\u30b1\u30c3\u30c8\u306e\u9806\u5e8f\u3067\u7d50\u679c\u3092\u30de\u30fc\u30b8\u3057\u307e\u3059\u3002

        \u56f3 11-13 \u00a0 \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u904e\u7a0b

        \u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby bucket_sort.py
        def bucket_sort(nums: list[float]):\n    \"\"\"\u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\"\"\"\n    # k = n/2 \u500b\u306e\u30d0\u30b1\u30c3\u30c8\u3092\u521d\u671f\u5316\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u306b\u5e73\u57472\u500b\u306e\u8981\u7d20\u3092\u914d\u7f6e\u3059\u308b\u3053\u3068\u3092\u671f\u5f85\n    k = len(nums) // 2\n    buckets = [[] for _ in range(k)]\n    # 1. \u914d\u5217\u8981\u7d20\u3092\u5404\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u6563\n    for num in nums:\n        # \u5165\u529b\u30c7\u30fc\u30bf\u7bc4\u56f2\u306f [0, 1)\u3001num * k \u3092\u4f7f\u7528\u3057\u3066\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2 [0, k-1] \u306b\u30de\u30c3\u30d4\u30f3\u30b0\n        i = int(num * k)\n        # num \u3092\u30d0\u30b1\u30c3\u30c8 i \u306b\u8ffd\u52a0\n        buckets[i].append(num)\n    # 2. \u5404\u30d0\u30b1\u30c3\u30c8\u3092\u30bd\u30fc\u30c8\n    for bucket in buckets:\n        # \u7d44\u307f\u8fbc\u307f\u30bd\u30fc\u30c8\u95a2\u6570\u3092\u4f7f\u7528\u3001\u4ed6\u306e\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u3082\u53ef\u80fd\n        bucket.sort()\n    # 3. \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3057\u3066\u7d50\u679c\u3092\u30de\u30fc\u30b8\n    i = 0\n    for bucket in buckets:\n        for num in bucket:\n            nums[i] = num\n            i += 1\n
        bucket_sort.cpp
        /* \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8 */\nvoid bucketSort(vector<float> &nums) {\n    // k = n/2\u500b\u306e\u30d0\u30b1\u30c3\u30c8\u3092\u521d\u671f\u5316\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u306b2\u3064\u306e\u8981\u7d20\u3092\u5272\u308a\u5f53\u3066\u308b\u3053\u3068\u3092\u671f\u5f85\n    int k = nums.size() / 2;\n    vector<vector<float>> buckets(k);\n    // 1. \u914d\u5217\u8981\u7d20\u3092\u5404\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u914d\n    for (float num : nums) {\n        // \u5165\u529b\u30c7\u30fc\u30bf\u7bc4\u56f2\u306f[0, 1)\u3001num * k\u3092\u4f7f\u7528\u3057\u3066\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2[0, k-1]\u306b\u30de\u30c3\u30d7\n        int i = num * k;\n        // bucket_idx\u30d0\u30b1\u30c3\u30c8\u306b\u6570\u5024\u3092\u8ffd\u52a0\n        buckets[i].push_back(num);\n    }\n    // 2. \u5404\u30d0\u30b1\u30c3\u30c8\u3092\u30bd\u30fc\u30c8\n    for (vector<float> &bucket : buckets) {\n        // \u7d44\u307f\u8fbc\u307f\u30bd\u30fc\u30c8\u95a2\u6570\u3092\u4f7f\u7528\u3001\u4ed6\u306e\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u3082\u53ef\u80fd\n        sort(bucket.begin(), bucket.end());\n    }\n    // 3. \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3057\u3066\u7d50\u679c\u3092\u30de\u30fc\u30b8\n    int i = 0;\n    for (vector<float> &bucket : buckets) {\n        for (float num : bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
        bucket_sort.java
        /* \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8 */\nvoid bucketSort(float[] nums) {\n    // k = n/2 \u500b\u306e\u30d0\u30b1\u30c3\u30c8\u3092\u521d\u671f\u5316\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u306b\u671f\u5f85\u3055\u308c\u308b\u8981\u7d20\u6570\u306f 2 \u500b\n    int k = nums.length / 2;\n    List<List<Float>> buckets = new ArrayList<>();\n    for (int i = 0; i < k; i++) {\n        buckets.add(new ArrayList<>());\n    }\n    // 1. \u914d\u5217\u8981\u7d20\u3092\u5404\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u6563\n    for (float num : nums) {\n        // \u5165\u529b\u30c7\u30fc\u30bf\u7bc4\u56f2\u306f [0, 1)\u3001num * k \u3092\u4f7f\u3063\u3066\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7bc4\u56f2 [0, k-1] \u306b\u30de\u30c3\u30d4\u30f3\u30b0\n        int i = (int) (num * k);\n        // num \u3092\u30d0\u30b1\u30c3\u30c8 i \u306b\u8ffd\u52a0\n        buckets.get(i).add(num);\n    }\n    // 2. \u5404\u30d0\u30b1\u30c3\u30c8\u3092\u30bd\u30fc\u30c8\n    for (List<Float> bucket : buckets) {\n        // \u7d44\u307f\u8fbc\u307f\u30bd\u30fc\u30c8\u95a2\u6570\u3092\u4f7f\u7528\u3001\u4ed6\u306e\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u3082\u53ef\u80fd\n        Collections.sort(bucket);\n    }\n    // 3. \u30d0\u30b1\u30c3\u30c8\u3092\u8d70\u67fb\u3057\u3066\u7d50\u679c\u3092\u30de\u30fc\u30b8\n    int i = 0;\n    for (List<Float> bucket : buckets) {\n        for (float num : bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
        bucket_sort.cs
        [class]{bucket_sort}-[func]{BucketSort}\n
        bucket_sort.go
        [class]{}-[func]{bucketSort}\n
        bucket_sort.swift
        [class]{}-[func]{bucketSort}\n
        bucket_sort.js
        [class]{}-[func]{bucketSort}\n
        bucket_sort.ts
        [class]{}-[func]{bucketSort}\n
        bucket_sort.dart
        [class]{}-[func]{bucketSort}\n
        bucket_sort.rs
        [class]{}-[func]{bucket_sort}\n
        bucket_sort.c
        [class]{}-[func]{bucketSort}\n
        bucket_sort.kt
        [class]{}-[func]{bucketSort}\n
        bucket_sort.rb
        [class]{}-[func]{bucket_sort}\n
        "},{"location":"chapter_sorting/bucket_sort/#1182","title":"11.8.2 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7279\u5fb4","text":"

        \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306f\u975e\u5e38\u306b\u5927\u304d\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306e\u51e6\u7406\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u5165\u529b\u30c7\u30fc\u30bf\u306b100\u4e07\u500b\u306e\u8981\u7d20\u304c\u542b\u307e\u308c\u3001\u30b7\u30b9\u30c6\u30e0\u30e1\u30e2\u30ea\u306e\u5236\u9650\u306b\u3088\u308a\u3059\u3079\u3066\u306e\u30c7\u30fc\u30bf\u3092\u540c\u6642\u306b\u30ed\u30fc\u30c9\u3067\u304d\u306a\u3044\u5834\u5408\u3001\u30c7\u30fc\u30bf\u30921,000\u500b\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u5272\u3057\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u3092\u500b\u5225\u306b\u30bd\u30fc\u30c8\u3057\u3066\u304b\u3089\u7d50\u679c\u3092\u30de\u30fc\u30b8\u3067\u304d\u307e\u3059\u3002

        • \u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n + k)\\)\uff1a\u8981\u7d20\u304c\u30d0\u30b1\u30c3\u30c8\u9593\u3067\u5747\u7b49\u306b\u5206\u6563\u3055\u308c\u3066\u3044\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u5185\u306e\u8981\u7d20\u6570\u306f \\(n/k\\) \u3067\u3059\u3002\u5358\u4e00\u306e\u30d0\u30b1\u30c3\u30c8\u306e\u30bd\u30fc\u30c8\u306b \\(O(n/k \\log(n/k))\\) \u6642\u9593\u304c\u304b\u304b\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u3059\u3079\u3066\u306e\u30d0\u30b1\u30c3\u30c8\u306e\u30bd\u30fc\u30c8\u306b \\(O(n \\log(n/k))\\) \u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002\u30d0\u30b1\u30c3\u30c8\u6570 \\(k\\) \u304c\u6bd4\u8f03\u7684\u5927\u304d\u3044\u3068\u304d\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u306b\u8fd1\u3065\u304d\u307e\u3059\u3002\u7d50\u679c\u306e\u30de\u30fc\u30b8\u306b\u306f\u3001\u3059\u3079\u3066\u306e\u30d0\u30b1\u30c3\u30c8\u3068\u8981\u7d20\u3092\u8d70\u67fb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\\(O(n + k)\\) \u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002\u6700\u60aa\u306e\u5834\u5408\u3001\u3059\u3079\u3066\u306e\u30c7\u30fc\u30bf\u304c\u5358\u4e00\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u6563\u3055\u308c\u3001\u305d\u306e\u30d0\u30b1\u30c3\u30c8\u306e\u30bd\u30fc\u30c8\u306b\u306f \\(O(n^2)\\) \u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002
        • \u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(n + k)\\)\u3001\u975e\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u30bd\u30fc\u30c8\uff1a\\(k\\) \u500b\u306e\u30d0\u30b1\u30c3\u30c8\u3068\u5408\u8a08 \\(n\\) \u500b\u306e\u8981\u7d20\u306e\u305f\u3081\u306e\u8ffd\u52a0\u30b9\u30da\u30fc\u30b9\u304c\u5fc5\u8981\u3067\u3059\u3002
        • \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u304c\u5b89\u5b9a\u304b\u3069\u3046\u304b\u306f\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u5185\u3067\u4f7f\u7528\u3055\u308c\u308b\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u5b89\u5b9a\u304b\u3069\u3046\u304b\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002
        "},{"location":"chapter_sorting/bucket_sort/#1183","title":"11.8.3 \u00a0 \u5747\u7b49\u5206\u6563\u3092\u9054\u6210\u3059\u308b\u65b9\u6cd5","text":"

        \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306e\u7406\u8ad6\u7684\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u306b\u9054\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u91cd\u8981\u306a\u3053\u3068\u306f\u3001\u3059\u3079\u3066\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u8981\u7d20\u3092\u5747\u7b49\u306b\u5206\u6563\u3055\u305b\u308b\u3053\u3068\u3067\u3059\u3002\u5b9f\u4e16\u754c\u306e\u30c7\u30fc\u30bf\u306f\u3057\u3070\u3057\u3070\u5747\u4e00\u306b\u5206\u6563\u3055\u308c\u3066\u3044\u306a\u3044\u304b\u3089\u3067\u3059\u3002\u4f8b\u3048\u3070\u3001eBay\u306e\u3059\u3079\u3066\u306e\u5546\u54c1\u3092\u4fa1\u683c\u7bc4\u56f2\u306710\u500b\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u5747\u7b49\u306b\u5206\u6563\u3055\u305b\u305f\u3044\u3068\u3057\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u5546\u54c1\u4fa1\u683c\u306e\u5206\u6563\u306f\u5747\u7b49\u3067\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001100\u30c9\u30eb\u672a\u6e80\u306e\u5546\u54c1\u304c\u591a\u304f\u3001500\u30c9\u30eb\u4ee5\u4e0a\u306e\u5546\u54c1\u304c\u5c11\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u4fa1\u683c\u7bc4\u56f2\u3092\u5747\u7b49\u306b10\u5206\u5272\u3059\u308b\u3068\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u306e\u5546\u54c1\u6570\u306e\u5dee\u304c\u5927\u304d\u304f\u306a\u308a\u307e\u3059\u3002

        \u5747\u7b49\u5206\u6563\u3092\u9054\u6210\u3059\u308b\u305f\u3081\u306b\u3001\u6700\u521d\u306b\u304a\u304a\u3088\u305d\u306e\u5883\u754c\u3092\u8a2d\u5b9a\u3057\u3066\u3001\u30c7\u30fc\u30bf\u30923\u3064\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u5927\u307e\u304b\u306b\u5206\u5272\u3067\u304d\u307e\u3059\u3002\u5206\u6563\u304c\u5b8c\u4e86\u3057\u305f\u5f8c\u3001\u3088\u308a\u591a\u304f\u306e\u30a2\u30a4\u30c6\u30e0\u3092\u6301\u3064\u30d0\u30b1\u30c3\u30c8\u3092\u3055\u3089\u306b3\u3064\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u5272\u3057\u3001\u3059\u3079\u3066\u306e\u30d0\u30b1\u30c3\u30c8\u306e\u8981\u7d20\u6570\u304c\u307b\u307c\u7b49\u3057\u304f\u306a\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u3053\u306e\u65b9\u6cd5\u306f\u672c\u8cea\u7684\u306b\u518d\u5e30\u6728\u3092\u69cb\u7bc9\u3057\u3001\u8449\u30ce\u30fc\u30c9\u306e\u8981\u7d20\u6570\u304c\u3067\u304d\u308b\u3060\u3051\u5747\u7b49\u306b\u306a\u308b\u3053\u3068\u3092\u76ee\u6307\u3057\u307e\u3059\u3002\u3082\u3061\u308d\u3093\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u30c7\u30fc\u30bf\u30923\u3064\u306e\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u5272\u3059\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093 - \u5206\u5272\u6226\u7565\u306f\u30c7\u30fc\u30bf\u306e\u72ec\u7279\u306a\u7279\u6027\u306b\u9069\u5fdc\u7684\u306b\u8abf\u6574\u3067\u304d\u307e\u3059\u3002

        \u56f3 11-14 \u00a0 \u30d0\u30b1\u30c3\u30c8\u306e\u518d\u5e30\u7684\u5206\u5272

        \u5546\u54c1\u4fa1\u683c\u306e\u78ba\u7387\u5206\u5e03\u3092\u4e8b\u524d\u306b\u77e5\u3063\u3066\u3044\u308b\u5834\u5408\u3001\u30c7\u30fc\u30bf\u306e\u78ba\u7387\u5206\u5e03\u306b\u57fa\u3065\u3044\u3066\u5404\u30d0\u30b1\u30c3\u30c8\u306e\u4fa1\u683c\u5883\u754c\u3092\u8a2d\u5b9a\u3067\u304d\u307e\u3059\u3002\u30c7\u30fc\u30bf\u5206\u5e03\u3092\u5177\u4f53\u7684\u306b\u8a08\u7b97\u3059\u308b\u5fc5\u8981\u306f\u5fc5\u305a\u3057\u3082\u306a\u304f\u3001\u4ee3\u308f\u308a\u306b\u78ba\u7387\u30e2\u30c7\u30eb\u3092\u4f7f\u7528\u3057\u3066\u30c7\u30fc\u30bf\u7279\u6027\u306b\u57fa\u3065\u3044\u3066\u8fd1\u4f3c\u3067\u304d\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5546\u54c1\u4fa1\u683c\u304c\u6b63\u898f\u5206\u5e03\u306b\u5f93\u3046\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u30d0\u30b1\u30c3\u30c8\u9593\u3067\u30a2\u30a4\u30c6\u30e0\u306e\u5206\u6563\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u53d6\u308b\u305f\u3081\u306b\u5408\u7406\u7684\u306a\u4fa1\u683c\u533a\u9593\u3092\u5b9a\u7fa9\u3067\u304d\u307e\u3059\u3002

        \u56f3 11-15 \u00a0 \u78ba\u7387\u5206\u5e03\u306b\u57fa\u3065\u304f\u30d0\u30b1\u30c3\u30c8\u5206\u5272

        "},{"location":"chapter_sorting/counting_sort/","title":"11.9 \u00a0 \u8a08\u6570\u30bd\u30fc\u30c8","text":"

        \u8a08\u6570\u30bd\u30fc\u30c8\u306f\u8981\u7d20\u306e\u6570\u3092\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u3053\u3068\u3067\u30bd\u30fc\u30c8\u3092\u5b9f\u73fe\u3057\u3001\u901a\u5e38\u306f\u6574\u6570\u914d\u5217\u306b\u9069\u7528\u3055\u308c\u307e\u3059\u3002

        "},{"location":"chapter_sorting/counting_sort/#1191","title":"11.9.1 \u00a0 \u7c21\u5358\u306a\u5b9f\u88c5","text":"

        \u7c21\u5358\u306a\u4f8b\u304b\u3089\u59cb\u3081\u307e\u3057\u3087\u3046\u3002\u9577\u3055 \\(n\\) \u306e\u914d\u5217 nums \u304c\u4e0e\u3048\u3089\u308c\u3001\u3059\u3079\u3066\u306e\u8981\u7d20\u304c\u300c\u975e\u8ca0\u6574\u6570\u300d\u3067\u3042\u308b\u5834\u5408\u3001\u8a08\u6570\u30bd\u30fc\u30c8\u306e\u5168\u4f53\u7684\u306a\u904e\u7a0b\u306f\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        1. \u914d\u5217\u3092\u8d70\u67fb\u3057\u3066\u6700\u5927\u6570\u3092\u898b\u3064\u3051\u3001\u305d\u308c\u3092 \\(m\\) \u3068\u3057\u3001\u9577\u3055 \\(m + 1\\) \u306e\u88dc\u52a9\u914d\u5217 counter \u3092\u4f5c\u6210\u3057\u307e\u3059\u3002
        2. counter \u3092\u4f7f\u7528\u3057\u3066 nums \u5185\u306e\u5404\u6570\u306e\u51fa\u73fe\u56de\u6570\u3092\u30ab\u30a6\u30f3\u30c8\u3057\u307e\u3059\u3002\u3053\u3053\u3067 counter[num] \u306f\u6570 num \u306e\u51fa\u73fe\u56de\u6570\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002\u30ab\u30a6\u30f3\u30c8\u65b9\u6cd5\u306f\u7c21\u5358\u3067\u3001nums \u3092\u8d70\u67fb\u3057\uff08\u73fe\u5728\u306e\u6570\u3092 num \u3068\u3059\u308b\uff09\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u3067 counter[num] \u3092 \\(1\\) \u5897\u3084\u3057\u307e\u3059\u3002
        3. counter \u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u81ea\u7136\u306b\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u3059\u3079\u3066\u306e\u6570\u306f\u672c\u8cea\u7684\u306b\u3059\u3067\u306b\u30bd\u30fc\u30c8\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u6b21\u306b\u3001counter \u3092\u8d70\u67fb\u3057\u3001\u51fa\u73fe\u9806\u306b nums \u3092\u6607\u9806\u3067\u57cb\u3081\u307e\u3059\u3002

        \u56f3 11-16 \u00a0 \u8a08\u6570\u30bd\u30fc\u30c8\u306e\u904e\u7a0b

        \u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby counting_sort.py
        def counting_sort_naive(nums: list[int]):\n    \"\"\"\u8a08\u6570\u30bd\u30fc\u30c8\"\"\"\n    # \u30b7\u30f3\u30d7\u30eb\u306a\u5b9f\u88c5\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30bd\u30fc\u30c8\u306b\u306f\u4f7f\u7528\u3067\u304d\u306a\u3044\n    # 1. \u914d\u5217\u5185\u306e\u6700\u5927\u8981\u7d20 m \u3092\u7d71\u8a08\n    m = 0\n    for num in nums:\n        m = max(m, num)\n    # 2. \u5404\u6570\u5b57\u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    # counter[num] \u306f num \u306e\u51fa\u73fe\u56de\u6570\u3092\u8868\u3059\n    counter = [0] * (m + 1)\n    for num in nums:\n        counter[num] += 1\n    # 3. counter \u3092\u8d70\u67fb\u3057\u3001\u5404\u8981\u7d20\u3092\u5143\u306e\u914d\u5217 nums \u306b\u57cb\u3081\u623b\u3057\n    i = 0\n    for num in range(m + 1):\n        for _ in range(counter[num]):\n            nums[i] = num\n            i += 1\n
        counting_sort.cpp
        /* \u30ab\u30a6\u30f3\u30c8\u30bd\u30fc\u30c8 */\n// \u7c21\u5358\u306a\u5b9f\u88c5\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30bd\u30fc\u30c8\u306b\u306f\u4f7f\u7528\u3067\u304d\u306a\u3044\nvoid countingSortNaive(vector<int> &nums) {\n    // 1. \u914d\u5217\u306e\u6700\u5927\u8981\u7d20m\u3092\u7d71\u8a08\n    int m = 0;\n    for (int num : nums) {\n        m = max(m, num);\n    }\n    // 2. \u5404\u6570\u5b57\u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    // counter[num]\u306fnum\u306e\u51fa\u73fe\u56de\u6570\u3092\u8868\u3059\n    vector<int> counter(m + 1, 0);\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. counter\u3092\u8d70\u67fb\u3057\u3001\u5404\u8981\u7d20\u3092\u5143\u306e\u914d\u5217nums\u306b\u623b\u3059\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
        counting_sort.java
        /* \u8a08\u6570\u30bd\u30fc\u30c8 */\n// \u7c21\u5358\u306a\u5b9f\u88c5\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30bd\u30fc\u30c8\u306b\u306f\u4f7f\u7528\u3067\u304d\u306a\u3044\nvoid countingSortNaive(int[] nums) {\n    // 1. \u914d\u5217\u306e\u6700\u5927\u8981\u7d20 m \u3092\u7d71\u8a08\n    int m = 0;\n    for (int num : nums) {\n        m = Math.max(m, num);\n    }\n    // 2. \u5404\u6570\u5b57\u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    // counter[num] \u306f num \u306e\u51fa\u73fe\u56de\u6570\u3092\u8868\u3059\n    int[] counter = new int[m + 1];\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. counter \u3092\u8d70\u67fb\u3057\u3001\u5404\u8981\u7d20\u3092\u5143\u306e\u914d\u5217 nums \u306b\u623b\u3059\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
        counting_sort.cs
        [class]{counting_sort}-[func]{CountingSortNaive}\n
        counting_sort.go
        [class]{}-[func]{countingSortNaive}\n
        counting_sort.swift
        [class]{}-[func]{countingSortNaive}\n
        counting_sort.js
        [class]{}-[func]{countingSortNaive}\n
        counting_sort.ts
        [class]{}-[func]{countingSortNaive}\n
        counting_sort.dart
        [class]{}-[func]{countingSortNaive}\n
        counting_sort.rs
        [class]{}-[func]{counting_sort_naive}\n
        counting_sort.c
        [class]{}-[func]{countingSortNaive}\n
        counting_sort.kt
        [class]{}-[func]{countingSortNaive}\n
        counting_sort.rb
        [class]{}-[func]{counting_sort_naive}\n

        \u8a08\u6570\u30bd\u30fc\u30c8\u3068\u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306e\u95a2\u4fc2

        \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306e\u89b3\u70b9\u304b\u3089\u3001\u8a08\u6570\u30bd\u30fc\u30c8\u306b\u304a\u3051\u308b\u8a08\u6570\u914d\u5217 counter \u306e\u5404\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u30d0\u30b1\u30c3\u30c8\u3068\u8003\u3048\u3001\u30ab\u30a6\u30f3\u30c8\u306e\u904e\u7a0b\u3092\u8981\u7d20\u3092\u5bfe\u5fdc\u3059\u308b\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u6563\u3055\u305b\u308b\u3053\u3068\u3068\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u672c\u8cea\u7684\u306b\u3001\u8a08\u6570\u30bd\u30fc\u30c8\u306f\u6574\u6570\u30c7\u30fc\u30bf\u306e\u305f\u3081\u306e\u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306e\u7279\u5225\u306a\u30b1\u30fc\u30b9\u3067\u3059\u3002

        "},{"location":"chapter_sorting/counting_sort/#1192","title":"11.9.2 \u00a0 \u5b8c\u5168\u306a\u5b9f\u88c5","text":"

        \u6ce8\u610f\u6df1\u3044\u8aad\u8005\u306f\u6c17\u4ed8\u304f\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u5165\u529b\u30c7\u30fc\u30bf\u304c\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5834\u5408\u3001\u4e0a\u8a18\u306e\u624b\u9806 3. \u306f\u7121\u52b9\u3067\u3059\u3002\u5165\u529b\u30c7\u30fc\u30bf\u304c\u5546\u54c1\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3001\u4fa1\u683c\uff08\u30af\u30e9\u30b9\u30e1\u30f3\u30d0\u5909\u6570\uff09\u3067\u5546\u54c1\u3092\u30bd\u30fc\u30c8\u3057\u305f\u3044\u3068\u3057\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u4e0a\u8a18\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u7d50\u679c\u3068\u3057\u3066\u30bd\u30fc\u30c8\u6e08\u307f\u306e\u4fa1\u683c\u306e\u307f\u3092\u63d0\u4f9b\u3067\u304d\u307e\u3059\u3002

        \u3067\u306f\u3001\u5143\u306e\u30c7\u30fc\u30bf\u306e\u30bd\u30fc\u30c8\u7d50\u679c\u3092\u3069\u306e\u3088\u3046\u306b\u53d6\u5f97\u3067\u304d\u308b\u3067\u3057\u3087\u3046\u304b\uff1f\u307e\u305a\u3001counter \u306e\u300c\u524d\u7f6e\u548c\u300d\u3092\u8a08\u7b97\u3057\u307e\u3059\u3002\u540d\u524d\u304c\u793a\u3059\u3088\u3046\u306b\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i \u3067\u306e\u524d\u7f6e\u548c prefix[i] \u306f\u3001\u914d\u5217\u306e\u6700\u521d\u306e i \u500b\u306e\u8981\u7d20\u306e\u548c\u306b\u7b49\u3057\u3044\u3067\u3059\uff1a

        \\[ \\text{prefix}[i] = \\sum_{j=0}^i \\text{counter[j]} \\]

        \u524d\u7f6e\u548c\u306b\u306f\u660e\u78ba\u306a\u610f\u5473\u304c\u3042\u308a\u307e\u3059\u3002prefix[num] - 1 \u306f\u7d50\u679c\u914d\u5217 res \u306b\u304a\u3051\u308b\u8981\u7d20 num \u306e\u6700\u5f8c\u306e\u51fa\u73fe\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8868\u3057\u307e\u3059\u3002\u3053\u306e\u60c5\u5831\u306f\u91cd\u8981\u3067\u3001\u5404\u8981\u7d20\u304c\u7d50\u679c\u914d\u5217\u306e\u3069\u3053\u306b\u73fe\u308c\u308b\u3079\u304d\u304b\u3092\u6559\u3048\u3066\u304f\u308c\u307e\u3059\u3002\u6b21\u306b\u3001\u5143\u306e\u914d\u5217 nums \u306e\u5404\u8981\u7d20 num \u3092\u9006\u9806\u3067\u8d70\u67fb\u3057\u3001\u5404\u53cd\u5fa9\u3067\u4ee5\u4e0b\u306e2\u3064\u306e\u624b\u9806\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002

        1. \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 prefix[num] - 1 \u3067\u914d\u5217 res \u306b num \u3092\u57cb\u3081\u307e\u3059\u3002
        2. \u524d\u7f6e\u548c prefix[num] \u3092 \\(1\\) \u6e1b\u3089\u3057\u3066\u3001num \u3092\u914d\u7f6e\u3059\u308b\u6b21\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002

        \u8d70\u67fb\u5f8c\u3001\u914d\u5217 res \u306b\u306f\u30bd\u30fc\u30c8\u3055\u308c\u305f\u7d50\u679c\u304c\u542b\u307e\u308c\u3001\u6700\u5f8c\u306b res \u304c\u5143\u306e\u914d\u5217 nums \u3092\u7f6e\u304d\u63db\u3048\u307e\u3059\u3002\u5b8c\u5168\u306a\u8a08\u6570\u30bd\u30fc\u30c8\u306e\u904e\u7a0b\u306f\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        <1><2><3><4><5><6><7><8>

        \u56f3 11-17 \u00a0 \u8a08\u6570\u30bd\u30fc\u30c8\u306e\u904e\u7a0b

        \u8a08\u6570\u30bd\u30fc\u30c8\u306e\u5b9f\u88c5\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby counting_sort.py
        def counting_sort(nums: list[int]):\n    \"\"\"\u8a08\u6570\u30bd\u30fc\u30c8\"\"\"\n    # \u5b8c\u5168\u306a\u5b9f\u88c5\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30bd\u30fc\u30c8\u304c\u53ef\u80fd\u3067\u3001\u5b89\u5b9a\u30bd\u30fc\u30c8\n    # 1. \u914d\u5217\u5185\u306e\u6700\u5927\u8981\u7d20 m \u3092\u7d71\u8a08\n    m = max(nums)\n    # 2. \u5404\u6570\u5b57\u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    # counter[num] \u306f num \u306e\u51fa\u73fe\u56de\u6570\u3092\u8868\u3059\n    counter = [0] * (m + 1)\n    for num in nums:\n        counter[num] += 1\n    # 3. counter \u306e\u524d\u7f6e\u548c\u3092\u8a08\u7b97\u3057\u3001\u300c\u51fa\u73fe\u56de\u6570\u300d\u3092\u300c\u672b\u5c3e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u306b\u5909\u63db\n    # counter[num]-1 \u306f res \u306b\u304a\u3044\u3066 num \u304c\u6700\u5f8c\u306b\u51fa\u73fe\u3059\u308b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\n    for i in range(m):\n        counter[i + 1] += counter[i]\n    # 4. nums \u3092\u9006\u9806\u306b\u8d70\u67fb\u3057\u3001\u5404\u8981\u7d20\u3092\u7d50\u679c\u914d\u5217 res \u306b\u914d\u7f6e\n    # \u7d50\u679c\u3092\u8a18\u9332\u3059\u308b\u305f\u3081\u306e\u914d\u5217 res \u3092\u521d\u671f\u5316\n    n = len(nums)\n    res = [0] * n\n    for i in range(n - 1, -1, -1):\n        num = nums[i]\n        res[counter[num] - 1] = num  # num \u3092\u5bfe\u5fdc\u3059\u308b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u914d\u7f6e\n        counter[num] -= 1  # \u524d\u7f6e\u548c\u30921\u6e1b\u3089\u3057\u3001num \u3092\u914d\u7f6e\u3059\u308b\u6b21\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\n    # \u7d50\u679c\u914d\u5217 res \u3092\u4f7f\u7528\u3057\u3066\u5143\u306e\u914d\u5217 nums \u3092\u4e0a\u66f8\u304d\n    for i in range(n):\n        nums[i] = res[i]\n
        counting_sort.cpp
        /* \u30ab\u30a6\u30f3\u30c8\u30bd\u30fc\u30c8 */\n// \u5b8c\u5168\u306a\u5b9f\u88c5\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30bd\u30fc\u30c8\u304c\u53ef\u80fd\u3067\u5b89\u5b9a\u30bd\u30fc\u30c8\nvoid countingSort(vector<int> &nums) {\n    // 1. \u914d\u5217\u306e\u6700\u5927\u8981\u7d20m\u3092\u7d71\u8a08\n    int m = 0;\n    for (int num : nums) {\n        m = max(m, num);\n    }\n    // 2. \u5404\u6570\u5b57\u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    // counter[num]\u306fnum\u306e\u51fa\u73fe\u56de\u6570\u3092\u8868\u3059\n    vector<int> counter(m + 1, 0);\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. counter\u306e\u524d\u7f00\u548c\u3092\u8a08\u7b97\u3057\u3001\u300c\u51fa\u73fe\u56de\u6570\u300d\u3092\u300c\u672b\u5c3e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u306b\u5909\u63db\n    // counter[num]-1\u306fnum\u304cres\u3067\u73fe\u308c\u308b\u6700\u5f8c\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. nums\u3092\u9006\u9806\u3067\u8d70\u67fb\u3057\u3001\u5404\u8981\u7d20\u3092\u7d50\u679c\u914d\u5217res\u306b\u914d\u7f6e\n    // \u7d50\u679c\u3092\u8a18\u9332\u3059\u308b\u914d\u5217res\u3092\u521d\u671f\u5316\n    int n = nums.size();\n    vector<int> res(n);\n    for (int i = n - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // num\u3092\u5bfe\u5fdc\u3059\u308b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u914d\u7f6e\n        counter[num]--;              // \u524d\u7f00\u548c\u30921\u6e1b\u3089\u3057\u3001num\u3092\u914d\u7f6e\u3059\u308b\u6b21\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\n    }\n    // \u7d50\u679c\u914d\u5217res\u3067\u5143\u306e\u914d\u5217nums\u3092\u4e0a\u66f8\u304d\n    nums = res;\n}\n
        counting_sort.java
        /* \u8a08\u6570\u30bd\u30fc\u30c8 */\n// \u5b8c\u5168\u306a\u5b9f\u88c5\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u30bd\u30fc\u30c8\u3067\u304d\u3001\u5b89\u5b9a\u30bd\u30fc\u30c8\nvoid countingSort(int[] nums) {\n    // 1. \u914d\u5217\u306e\u6700\u5927\u8981\u7d20 m \u3092\u7d71\u8a08\n    int m = 0;\n    for (int num : nums) {\n        m = Math.max(m, num);\n    }\n    // 2. \u5404\u6570\u5b57\u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    // counter[num] \u306f num \u306e\u51fa\u73fe\u56de\u6570\u3092\u8868\u3059\n    int[] counter = new int[m + 1];\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. counter \u306e\u7d2f\u7a4d\u548c\u3092\u8a08\u7b97\u3057\u3001\u300c\u51fa\u73fe\u56de\u6570\u300d\u3092\u300c\u5c3b\u5c3e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u306b\u5909\u63db\n    // counter[num]-1 \u306f res \u5185\u3067 num \u304c\u51fa\u73fe\u3059\u308b\u6700\u5f8c\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. nums \u3092\u9006\u9806\u306b\u8d70\u67fb\u3057\u3001\u5404\u8981\u7d20\u3092\u7d50\u679c\u914d\u5217 res \u306b\u914d\u7f6e\n    // \u7d50\u679c\u3092\u8a18\u9332\u3059\u308b\u914d\u5217 res \u3092\u521d\u671f\u5316\n    int n = nums.length;\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // num \u3092\u5bfe\u5fdc\u3059\u308b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u914d\u7f6e\n        counter[num]--; // \u7d2f\u7a4d\u548c\u3092 1 \u6e1b\u7b97\u3057\u3001num \u3092\u914d\u7f6e\u3059\u308b\u6b21\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\n    }\n    // \u7d50\u679c\u914d\u5217 res \u3092\u4f7f\u3063\u3066\u5143\u306e\u914d\u5217 nums \u3092\u4e0a\u66f8\u304d\n    for (int i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n
        counting_sort.cs
        [class]{counting_sort}-[func]{CountingSort}\n
        counting_sort.go
        [class]{}-[func]{countingSort}\n
        counting_sort.swift
        [class]{}-[func]{countingSort}\n
        counting_sort.js
        [class]{}-[func]{countingSort}\n
        counting_sort.ts
        [class]{}-[func]{countingSort}\n
        counting_sort.dart
        [class]{}-[func]{countingSort}\n
        counting_sort.rs
        [class]{}-[func]{counting_sort}\n
        counting_sort.c
        [class]{}-[func]{countingSort}\n
        counting_sort.kt
        [class]{}-[func]{countingSort}\n
        counting_sort.rb
        [class]{}-[func]{counting_sort}\n
        "},{"location":"chapter_sorting/counting_sort/#1193","title":"11.9.3 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7279\u5fb4","text":"
        • \u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n + m)\\)\u3001\u975e\u9069\u5fdc\u30bd\u30fc\u30c8\uff1anums \u3068 counter \u306e\u8d70\u67fb\u304c\u542b\u307e\u308c\u3001\u3069\u3061\u3089\u3082\u7dda\u5f62\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u4e00\u822c\u7684\u306b\u3001\\(n \\gg m\\) \u3067\u3042\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u306b\u8fd1\u3065\u304d\u307e\u3059\u3002
        • \u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(n + m)\\)\u3001\u975e\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u30bd\u30fc\u30c8\uff1a\u9577\u3055 \\(n\\) \u306e\u914d\u5217 res \u3068\u9577\u3055 \\(m\\) \u306e\u914d\u5217 counter \u3092\u305d\u308c\u305e\u308c\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u5b89\u5b9a\u30bd\u30fc\u30c8\uff1a\u8981\u7d20\u304c\u300c\u53f3\u304b\u3089\u5de6\u300d\u306e\u9806\u5e8f\u3067 res \u306b\u57cb\u3081\u3089\u308c\u308b\u305f\u3081\u3001nums \u306e\u8d70\u67fb\u3092\u9006\u9806\u306b\u3059\u308b\u3053\u3068\u3067\u3001\u7b49\u3057\u3044\u8981\u7d20\u9593\u306e\u76f8\u5bfe\u4f4d\u7f6e\u306e\u5909\u5316\u3092\u9632\u304e\u3001\u5b89\u5b9a\u3057\u305f\u30bd\u30fc\u30c8\u3092\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002\u5b9f\u969b\u3001nums \u3092\u9806\u756a\u306b\u8d70\u67fb\u3057\u3066\u3082\u6b63\u3057\u3044\u30bd\u30fc\u30c8\u7d50\u679c\u3092\u751f\u6210\u3067\u304d\u307e\u3059\u304c\u3001\u7d50\u679c\u306f\u4e0d\u5b89\u5b9a\u3067\u3059\u3002
        "},{"location":"chapter_sorting/counting_sort/#1194","title":"11.9.4 \u00a0 \u5236\u9650\u4e8b\u9805","text":"

        \u4eca\u307e\u3067\u306b\u3001\u8a08\u6570\u30bd\u30fc\u30c8\u306f\u975e\u5e38\u306b\u5de7\u5999\u3060\u3068\u611f\u3058\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u5358\u306b\u91cf\u3092\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u3060\u3051\u3067\u52b9\u7387\u7684\u306a\u30bd\u30fc\u30c8\u3092\u5b9f\u73fe\u3067\u304d\u308b\u304b\u3089\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u8a08\u6570\u30bd\u30fc\u30c8\u3092\u4f7f\u7528\u3059\u308b\u305f\u3081\u306e\u524d\u63d0\u6761\u4ef6\u306f\u6bd4\u8f03\u7684\u53b3\u3057\u3044\u3067\u3059\u3002

        \u8a08\u6570\u30bd\u30fc\u30c8\u306f\u975e\u8ca0\u6574\u6570\u306b\u306e\u307f\u9069\u7528\u3067\u304d\u307e\u3059\u3002\u4ed6\u306e\u30bf\u30a4\u30d7\u306e\u30c7\u30fc\u30bf\u306b\u9069\u7528\u3057\u305f\u3044\u5834\u5408\u3001\u3053\u308c\u3089\u306e\u30c7\u30fc\u30bf\u304c\u8981\u7d20\u306e\u5143\u306e\u9806\u5e8f\u3092\u5909\u66f4\u3059\u308b\u3053\u3068\u306a\u304f\u975e\u8ca0\u6574\u6570\u306b\u5909\u63db\u3067\u304d\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u8ca0\u306e\u6574\u6570\u3092\u542b\u3080\u914d\u5217\u306e\u5834\u5408\u3001\u6700\u521d\u306b\u3059\u3079\u3066\u306e\u6570\u306b\u5b9a\u6570\u3092\u52a0\u3048\u3066\u3001\u3059\u3079\u3066\u3092\u6b63\u306e\u6570\u306b\u5909\u63db\u3057\u3001\u30bd\u30fc\u30c8\u5b8c\u4e86\u5f8c\u306b\u5143\u306b\u623b\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u8a08\u6570\u30bd\u30fc\u30c8\u306f\u5024\u306e\u7bc4\u56f2\u304c\u5c0f\u3055\u3044\u5927\u304d\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u4e0a\u8a18\u306e\u4f8b\u3067\u306f\u3001\\(m\\) \u306f\u5927\u304d\u3059\u304e\u308b\u3079\u304d\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u305d\u3046\u3067\u306a\u3051\u308c\u3070\u3001\u3042\u307e\u308a\u306b\u3082\u591a\u304f\u306e\u30b9\u30da\u30fc\u30b9\u3092\u5360\u6709\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u3002\u305d\u3057\u3066 \\(n \\ll m\\) \u306e\u5834\u5408\u3001\u8a08\u6570\u30bd\u30fc\u30c8\u306f \\(O(m)\\) \u6642\u9593\u3092\u4f7f\u7528\u3057\u3001\\(O(n \\log n)\\) \u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3088\u308a\u9045\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_sorting/heap_sort/","title":"11.7 \u00a0 \u30d2\u30fc\u30d7\u30bd\u30fc\u30c8","text":"

        Tip

        \u3053\u306e\u7bc0\u3092\u8aad\u3080\u524d\u306b\u3001\u300c\u30d2\u30fc\u30d7\u300d\u306e\u7ae0\u3092\u5fc5\u305a\u5b8c\u4e86\u3055\u305b\u3066\u304f\u3060\u3055\u3044\u3002

        \u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\u306f\u3001\u30d2\u30fc\u30d7\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u57fa\u3065\u304f\u52b9\u7387\u7684\u306a\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3059\u3002\u3059\u3067\u306b\u5b66\u7fd2\u3057\u305f\u300c\u30d2\u30fc\u30d7\u306e\u69cb\u7bc9\u300d\u3068\u300c\u8981\u7d20\u306e\u62bd\u51fa\u300d\u64cd\u4f5c\u3092\u4f7f\u7528\u3057\u3066\u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002

        1. \u914d\u5217\u3092\u5165\u529b\u3057\u3001\u6700\u5c0f\u30d2\u30fc\u30d7\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002\u3053\u3053\u3067\u3001\u6700\u5c0f\u8981\u7d20\u304c\u30d2\u30fc\u30d7\u306e\u9802\u4e0a\u306b\u4f4d\u7f6e\u3057\u307e\u3059\u3002
        2. \u7d99\u7d9a\u7684\u306b\u62bd\u51fa\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3001\u62bd\u51fa\u3055\u308c\u305f\u8981\u7d20\u3092\u9806\u6b21\u8a18\u9332\u3057\u3066\u3001\u6700\u5c0f\u304b\u3089\u6700\u5927\u307e\u3067\u306e\u30bd\u30fc\u30c8\u6e08\u307f\u30ea\u30b9\u30c8\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002

        \u4e0a\u8a18\u306e\u65b9\u6cd5\u306f\u5b9f\u73fe\u53ef\u80fd\u3067\u3059\u304c\u3001\u30dd\u30c3\u30d7\u3055\u308c\u305f\u8981\u7d20\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306e\u8ffd\u52a0\u306e\u914d\u5217\u304c\u5fc5\u8981\u3067\u3001\u3084\u3084\u7a7a\u9593\u3092\u6d88\u8cbb\u3057\u307e\u3059\u3002\u5b9f\u969b\u306b\u306f\u3001\u901a\u5e38\u3001\u3088\u308a\u512a\u96c5\u306a\u5b9f\u88c5\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        "},{"location":"chapter_sorting/heap_sort/#1171","title":"11.7.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6d41\u308c","text":"

        \u914d\u5217\u306e\u9577\u3055\u3092 \\(n\\) \u3068\u3059\u308b\u3068\u3001\u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\u306e\u904e\u7a0b\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002

        1. \u914d\u5217\u3092\u5165\u529b\u3057\u3001\u6700\u5927\u30d2\u30fc\u30d7\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002\u3053\u306e\u624b\u9806\u306e\u5f8c\u3001\u6700\u5927\u8981\u7d20\u304c\u30d2\u30fc\u30d7\u306e\u9802\u4e0a\u306b\u4f4d\u7f6e\u3057\u307e\u3059\u3002
        2. \u30d2\u30fc\u30d7\u306e\u9802\u4e0a\u8981\u7d20\uff08\u6700\u521d\u306e\u8981\u7d20\uff09\u3068\u30d2\u30fc\u30d7\u306e\u5e95\u90e8\u8981\u7d20\uff08\u6700\u5f8c\u306e\u8981\u7d20\uff09\u3092\u4ea4\u63db\u3057\u307e\u3059\u3002\u3053\u306e\u4ea4\u63db\u306e\u5f8c\u3001\u30d2\u30fc\u30d7\u306e\u9577\u3055\u3092 \\(1\\) \u6e1b\u3089\u3057\u3001\u30bd\u30fc\u30c8\u6e08\u307f\u8981\u7d20\u306e\u6570\u3092 \\(1\\) \u5897\u3084\u3057\u307e\u3059\u3002
        3. \u30d2\u30fc\u30d7\u306e\u9802\u4e0a\u304b\u3089\u958b\u59cb\u3057\u3066\u3001\u4e0a\u304b\u3089\u4e0b\u3078\u306esift-down\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002sift-down\u306e\u5f8c\u3001\u30d2\u30fc\u30d7\u306e\u6027\u8cea\u304c\u5fa9\u5143\u3055\u308c\u307e\u3059\u3002
        4. \u624b\u9806 2. \u3068 3. \u3092\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002\\(n - 1\\) \u30e9\u30a6\u30f3\u30c9\u30eb\u30fc\u30d7\u3057\u3066\u3001\u914d\u5217\u306e\u30bd\u30fc\u30c8\u3092\u5b8c\u4e86\u3057\u307e\u3059\u3002

        Tip

        \u5b9f\u969b\u3001\u8981\u7d20\u62bd\u51fa\u64cd\u4f5c\u3082\u624b\u9806 2. \u3068 3. \u3092\u542b\u307f\u3001\u62bd\u51fa\u3055\u308c\u305f\u8981\u7d20\u3092\u30d2\u30fc\u30d7\u304b\u3089\u524a\u9664\u3059\u308b\u8ffd\u52a0\u306e\u624b\u9806\u304c\u3042\u308a\u307e\u3059\u3002

        <1><2><3><4><5><6><7><8><9><10><11><12>

        \u56f3 11-12 \u00a0 \u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\u306e\u904e\u7a0b

        \u30b3\u30fc\u30c9\u306e\u5b9f\u88c5\u3067\u306f\u3001\u300c\u30d2\u30fc\u30d7\u300d\u306e\u7ae0\u304b\u3089\u306esift-down\u95a2\u6570 sift_down() \u3092\u4f7f\u7528\u3057\u307e\u3057\u305f\u3002\u6700\u5927\u8981\u7d20\u304c\u62bd\u51fa\u3055\u308c\u308b\u306b\u3064\u308c\u3066\u30d2\u30fc\u30d7\u306e\u9577\u3055\u304c\u6e1b\u5c11\u3059\u308b\u305f\u3081\u3001sift_down() \u95a2\u6570\u306b\u9577\u3055\u30d1\u30e9\u30e1\u30fc\u30bf \\(n\\) \u3092\u8ffd\u52a0\u3057\u3066\u3001\u30d2\u30fc\u30d7\u306e\u73fe\u5728\u306e\u6709\u52b9\u9577\u3092\u6307\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby heap_sort.py
        def sift_down(nums: list[int], n: int, i: int):\n    \"\"\"\u30d2\u30fc\u30d7\u306e\u9577\u3055\u304c n\u3001\u30ce\u30fc\u30c9 i \u304b\u3089\u4e0a\u304b\u3089\u4e0b\u3078\u30d2\u30fc\u30d7\u5316\u3092\u958b\u59cb\"\"\"\n    while True:\n        # i\u3001l\u3001r \u306e\u4e2d\u3067\u6700\u5927\u306e\u30ce\u30fc\u30c9\u3092\u5224\u5b9a\u3057\u3001ma \u3068\u3059\u308b\n        l = 2 * i + 1\n        r = 2 * i + 2\n        ma = i\n        if l < n and nums[l] > nums[ma]:\n            ma = l\n        if r < n and nums[r] > nums[ma]:\n            ma = r\n        # \u30ce\u30fc\u30c9 i \u304c\u6700\u5927\u307e\u305f\u306f l\u3001r \u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001\u3055\u3089\u306a\u308b\u30d2\u30fc\u30d7\u5316\u306f\u4e0d\u8981\u3001\u30eb\u30fc\u30d7\u3092\u629c\u3051\u308b\n        if ma == i:\n            break\n        # 2\u3064\u306e\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\n        nums[i], nums[ma] = nums[ma], nums[i]\n        # \u4e0b\u5411\u304d\u306b\u30d2\u30fc\u30d7\u5316\u3092\u30eb\u30fc\u30d7\n        i = ma\n\ndef heap_sort(nums: list[int]):\n    \"\"\"\u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\"\"\"\n    # \u30d2\u30fc\u30d7\u69cb\u7bc9\u64cd\u4f5c\uff1a\u8449\u30ce\u30fc\u30c9\u4ee5\u5916\u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3092\u30d2\u30fc\u30d7\u5316\n    for i in range(len(nums) // 2 - 1, -1, -1):\n        sift_down(nums, len(nums), i)\n    # \u30d2\u30fc\u30d7\u304b\u3089\u6700\u5927\u8981\u7d20\u3092\u62bd\u51fa\u3057\u3001n-1 \u56de\u7e70\u308a\u8fd4\u3059\n    for i in range(len(nums) - 1, 0, -1):\n        # \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3068\u6700\u3082\u53f3\u306e\u8449\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\uff08\u6700\u521d\u306e\u8981\u7d20\u3068\u6700\u5f8c\u306e\u8981\u7d20\u3092\u4ea4\u63db\uff09\n        nums[0], nums[i] = nums[i], nums[0]\n        # \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u304b\u3089\u4e0a\u304b\u3089\u4e0b\u3078\u30d2\u30fc\u30d7\u5316\u3092\u958b\u59cb\n        sift_down(nums, i, 0)\n
        heap_sort.cpp
        /* \u30d2\u30fc\u30d7\u306e\u9577\u3055\u306fn\u3001\u30ce\u30fc\u30c9i\u304b\u3089\u4e0a\u304b\u3089\u4e0b\u3078\u30d2\u30fc\u30d7\u5316\u3092\u958b\u59cb */\nvoid siftDown(vector<int> &nums, int n, int i) {\n    while (true) {\n        // i\u3001l\u3001r \u306e\u4e2d\u3067\u6700\u5927\u306e\u30ce\u30fc\u30c9\u3092\u6c7a\u5b9a\u3057\u3001ma\u3068\u3057\u3066\u8a18\u9332\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // \u30ce\u30fc\u30c9i\u304c\u6700\u5927\u304b\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9l\u3001r\u304c\u5883\u754c\u5916\u306e\u5834\u5408\u3001\u305d\u308c\u4ee5\u4e0a\u306e\u30d2\u30fc\u30d7\u5316\u306f\u4e0d\u8981\u3067\u7d42\u4e86\n        if (ma == i) {\n            break;\n        }\n        // \u4e8c\u3064\u306e\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\n        swap(nums[i], nums[ma]);\n        // \u4e0b\u5411\u304d\u306b\u30d2\u30fc\u30d7\u5316\u3092\u30eb\u30fc\u30d7\n        i = ma;\n    }\n}\n\n/* \u30d2\u30fc\u30d7\u30bd\u30fc\u30c8 */\nvoid heapSort(vector<int> &nums) {\n    // \u30d2\u30fc\u30d7\u69cb\u7bc9\u64cd\u4f5c\uff1a\u8449\u4ee5\u5916\u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3092\u30d2\u30fc\u30d7\u5316\n    for (int i = nums.size() / 2 - 1; i >= 0; --i) {\n        siftDown(nums, nums.size(), i);\n    }\n    // \u30d2\u30fc\u30d7\u304b\u3089\u6700\u5927\u8981\u7d20\u3092\u62bd\u51fa\u3057\u3001n-1\u56de\u7e70\u308a\u8fd4\u3059\n    for (int i = nums.size() - 1; i > 0; --i) {\n        // \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u6700\u53f3\u8449\u30ce\u30fc\u30c9\u3068\u4ea4\u63db\uff08\u6700\u521d\u306e\u8981\u7d20\u3092\u6700\u5f8c\u306e\u8981\u7d20\u3068\u4ea4\u63db\uff09\n        swap(nums[0], nums[i]);\n        // \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u304b\u3089\u4e0a\u304b\u3089\u4e0b\u3078\u30d2\u30fc\u30d7\u5316\u3092\u958b\u59cb\n        siftDown(nums, i, 0);\n    }\n}\n
        heap_sort.java
        /* \u30d2\u30fc\u30d7\u306e\u9577\u3055\u306f n\u3001\u30ce\u30fc\u30c9 i \u304b\u3089\u4e0a\u304b\u3089\u4e0b\u3078\u30d2\u30fc\u30d7\u5316\u958b\u59cb */\nvoid siftDown(int[] nums, int n, int i) {\n    while (true) {\n        // i, l, r \u306e\u4e2d\u3067\u6700\u5927\u306e\u30ce\u30fc\u30c9\u3092\u5224\u5b9a\u3057\u3001ma \u3068\u3059\u308b\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // \u30ce\u30fc\u30c9 i \u304c\u6700\u5927\u3001\u307e\u305f\u306f\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 l, r \u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001\u3055\u3089\u306a\u308b\u30d2\u30fc\u30d7\u5316\u306f\u4e0d\u8981\u3001\u30d6\u30ec\u30fc\u30af\n        if (ma == i)\n            break;\n        // 2\u3064\u306e\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\n        int temp = nums[i];\n        nums[i] = nums[ma];\n        nums[ma] = temp;\n        // \u4e0b\u5411\u304d\u306b\u30d2\u30fc\u30d7\u5316\u3092\u30eb\u30fc\u30d7\n        i = ma;\n    }\n}\n\n/* \u30d2\u30fc\u30d7\u30bd\u30fc\u30c8 */\nvoid heapSort(int[] nums) {\n    // \u30d2\u30fc\u30d7\u69cb\u7bc9\u64cd\u4f5c: \u8449\u30ce\u30fc\u30c9\u4ee5\u5916\u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3092\u30d2\u30fc\u30d7\u5316\n    for (int i = nums.length / 2 - 1; i >= 0; i--) {\n        siftDown(nums, nums.length, i);\n    }\n    // \u30d2\u30fc\u30d7\u304b\u3089\u6700\u5927\u8981\u7d20\u3092\u62bd\u51fa\u3057\u3001n-1 \u56de\u7e70\u308a\u8fd4\u3057\n    for (int i = nums.length - 1; i > 0; i--) {\n        // \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3068\u6700\u3082\u53f3\u306e\u8449\u30ce\u30fc\u30c9\u3092\u4ea4\u63db\uff08\u6700\u521d\u306e\u8981\u7d20\u3068\u6700\u5f8c\u306e\u8981\u7d20\u3092\u4ea4\u63db\uff09\n        int tmp = nums[0];\n        nums[0] = nums[i];\n        nums[i] = tmp;\n        // \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u304b\u3089\u4e0a\u304b\u3089\u4e0b\u3078\u30d2\u30fc\u30d7\u5316\u958b\u59cb\n        siftDown(nums, i, 0);\n    }\n}\n
        heap_sort.cs
        [class]{heap_sort}-[func]{SiftDown}\n\n[class]{heap_sort}-[func]{HeapSort}\n
        heap_sort.go
        [class]{}-[func]{siftDown}\n\n[class]{}-[func]{heapSort}\n
        heap_sort.swift
        [class]{}-[func]{siftDown}\n\n[class]{}-[func]{heapSort}\n
        heap_sort.js
        [class]{}-[func]{siftDown}\n\n[class]{}-[func]{heapSort}\n
        heap_sort.ts
        [class]{}-[func]{siftDown}\n\n[class]{}-[func]{heapSort}\n
        heap_sort.dart
        [class]{}-[func]{siftDown}\n\n[class]{}-[func]{heapSort}\n
        heap_sort.rs
        [class]{}-[func]{sift_down}\n\n[class]{}-[func]{heap_sort}\n
        heap_sort.c
        [class]{}-[func]{siftDown}\n\n[class]{}-[func]{heapSort}\n
        heap_sort.kt
        [class]{}-[func]{siftDown}\n\n[class]{}-[func]{heapSort}\n
        heap_sort.rb
        [class]{}-[func]{sift_down}\n\n[class]{}-[func]{heap_sort}\n
        "},{"location":"chapter_sorting/heap_sort/#1172","title":"11.7.2 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7279\u5fb4","text":"
        • \u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n \\log n)\\)\u3001\u975e\u9069\u5fdc\u30bd\u30fc\u30c8\uff1a\u30d2\u30fc\u30d7\u306e\u69cb\u7bc9\u306f \\(O(n)\\) \u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u30d2\u30fc\u30d7\u304b\u3089\u6700\u5927\u8981\u7d20\u3092\u62bd\u51fa\u3059\u308b\u306b\u306f \\(O(\\log n)\\) \u6642\u9593\u304c\u304b\u304b\u308a\u3001\\(n - 1\\) \u30e9\u30a6\u30f3\u30c9\u30eb\u30fc\u30d7\u3057\u307e\u3059\u3002
        • \u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(1)\\)\u3001\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u30bd\u30fc\u30c8\uff1a\u3044\u304f\u3064\u304b\u306e\u30dd\u30a4\u30f3\u30bf\u5909\u6570\u304c \\(O(1)\\) \u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u8981\u7d20\u306e\u4ea4\u63db\u3068\u30d2\u30fc\u30d7\u5316\u64cd\u4f5c\u306f\u5143\u306e\u914d\u5217\u3067\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002
        • \u975e\u5b89\u5b9a\u30bd\u30fc\u30c8\uff1a\u30d2\u30fc\u30d7\u306e\u9802\u4e0a\u3068\u5e95\u90e8\u8981\u7d20\u306e\u4ea4\u63db\u4e2d\u306b\u3001\u7b49\u3057\u3044\u8981\u7d20\u306e\u76f8\u5bfe\u4f4d\u7f6e\u304c\u5909\u308f\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        "},{"location":"chapter_sorting/insertion_sort/","title":"11.4 \u00a0 \u633f\u5165\u30bd\u30fc\u30c8","text":"

        \u633f\u5165\u30bd\u30fc\u30c8\u306f\u3001\u30c8\u30e9\u30f3\u30d7\u306e\u30c7\u30c3\u30ad\u3092\u624b\u52d5\u3067\u30bd\u30fc\u30c8\u3059\u308b\u30d7\u30ed\u30bb\u30b9\u306b\u3088\u304f\u4f3c\u305f\u52d5\u4f5c\u3092\u3059\u308b\u30b7\u30f3\u30d7\u30eb\u306a\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3059\u3002

        \u5177\u4f53\u7684\u306b\u306f\u3001\u672a\u30bd\u30fc\u30c8\u533a\u9593\u304b\u3089\u30d9\u30fc\u30b9\u8981\u7d20\u3092\u9078\u629e\u3057\u3001\u305d\u306e\u5de6\u5074\u306e\u30bd\u30fc\u30c8\u6e08\u307f\u533a\u9593\u306e\u8981\u7d20\u3068\u6bd4\u8f03\u3057\u3066\u3001\u8981\u7d20\u3092\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u633f\u5165\u3057\u307e\u3059\u3002

        \u4e0b\u56f3\u306f\u3001\u8981\u7d20\u304c\u914d\u5217\u306b\u633f\u5165\u3055\u308c\u308b\u65b9\u6cd5\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u30d9\u30fc\u30b9\u8981\u7d20\u3092base\u3068\u3059\u308b\u3068\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304b\u3089base\u307e\u3067\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u53f3\u306b1\u3064\u305a\u3064\u30b7\u30d5\u30c8\u3057\u3001\u305d\u306e\u5f8cbase\u3092\u30bf\u30fc\u30b2\u30c3\u30c8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5272\u308a\u5f53\u3066\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u56f3 11-6 \u00a0 Single insertion operation

        "},{"location":"chapter_sorting/insertion_sort/#1141","title":"11.4.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d7\u30ed\u30bb\u30b9","text":"

        \u633f\u5165\u30bd\u30fc\u30c8\u306e\u5168\u4f53\u7684\u306a\u30d7\u30ed\u30bb\u30b9\u306f\u4e0b\u56f3\u306b\u793a\u3055\u308c\u307e\u3059\u3002

        1. \u914d\u5217\u306e\u6700\u521d\u306e\u8981\u7d20\u3092\u30bd\u30fc\u30c8\u6e08\u307f\u3068\u307f\u306a\u3057\u307e\u3059\u3002
        2. 2\u756a\u76ee\u306e\u8981\u7d20\u3092base\u3068\u3057\u3066\u9078\u629e\u3057\u3001\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u633f\u5165\u3057\u3066\u3001\u6700\u521d\u306e2\u3064\u306e\u8981\u7d20\u3092\u30bd\u30fc\u30c8\u6e08\u307f\u306b\u3057\u307e\u3059\u3002
        3. 3\u756a\u76ee\u306e\u8981\u7d20\u3092base\u3068\u3057\u3066\u9078\u629e\u3057\u3001\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u633f\u5165\u3057\u3066\u3001\u6700\u521d\u306e3\u3064\u306e\u8981\u7d20\u3092\u30bd\u30fc\u30c8\u6e08\u307f\u306b\u3057\u307e\u3059\u3002
        4. \u3053\u306e\u65b9\u6cd5\u3067\u7d9a\u884c\u3057\u3001\u6700\u5f8c\u306e\u53cd\u5fa9\u3067\u306f\u3001\u6700\u5f8c\u306e\u8981\u7d20\u3092base\u3068\u3057\u3066\u53d6\u308a\u3001\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u633f\u5165\u3057\u305f\u5f8c\u3001\u3059\u3079\u3066\u306e\u8981\u7d20\u304c\u30bd\u30fc\u30c8\u3055\u308c\u307e\u3059\u3002

        \u56f3 11-7 \u00a0 Insertion sort process

        \u30b3\u30fc\u30c9\u4f8b\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby insertion_sort.py
        def insertion_sort(nums: list[int]):\n    \"\"\"\u633f\u5165\u30bd\u30fc\u30c8\"\"\"\n    # \u5916\u5074\u306e\u30eb\u30fc\u30d7\uff1a\u30bd\u30fc\u30c8\u6e08\u307f\u7bc4\u56f2\u306f [0, i-1]\n    for i in range(1, len(nums)):\n        base = nums[i]\n        j = i - 1\n        # \u5185\u5074\u306e\u30eb\u30fc\u30d7\uff1abase \u3092\u30bd\u30fc\u30c8\u6e08\u307f\u7bc4\u56f2 [0, i-1] \u306e\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u633f\u5165\n        while j >= 0 and nums[j] > base:\n            nums[j + 1] = nums[j]  # nums[j] \u3092\u53f3\u306b1\u3064\u79fb\u52d5\n            j -= 1\n        nums[j + 1] = base  # base \u3092\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u4ee3\u5165\n
        insertion_sort.cpp
        /* \u633f\u5165\u30bd\u30fc\u30c8 */\nvoid insertionSort(vector<int> &nums) {\n    // \u5916\u5074\u30eb\u30fc\u30d7\uff1a\u30bd\u30fc\u30c8\u6e08\u307f\u7bc4\u56f2\u306f[0, i-1]\n    for (int i = 1; i < nums.size(); i++) {\n        int base = nums[i], j = i - 1;\n        // \u5185\u5074\u30eb\u30fc\u30d7\uff1abase\u3092\u30bd\u30fc\u30c8\u6e08\u307f\u7bc4\u56f2[0, i-1]\u5185\u306e\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u633f\u5165\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // nums[j]\u3092\u4e00\u3064\u53f3\u306b\u79fb\u52d5\n            j--;\n        }\n        nums[j + 1] = base; // base\u3092\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u4ee3\u5165\n    }\n}\n
        insertion_sort.java
        /* \u633f\u5165\u30bd\u30fc\u30c8 */\nvoid insertionSort(int[] nums) {\n    // \u5916\u5074\u30eb\u30fc\u30d7: \u30bd\u30fc\u30c8\u6e08\u307f\u7bc4\u56f2\u306f [0, i-1]\n    for (int i = 1; i < nums.length; i++) {\n        int base = nums[i], j = i - 1;\n        // \u5185\u5074\u30eb\u30fc\u30d7: base \u3092\u30bd\u30fc\u30c8\u6e08\u307f\u7bc4\u56f2 [0, i-1] \u306e\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u633f\u5165\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // nums[j] \u3092\u53f3\u306b1\u3064\u79fb\u52d5\n            j--;\n        }\n        nums[j + 1] = base;        // base \u3092\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u4ee3\u5165\n    }\n}\n
        insertion_sort.cs
        [class]{insertion_sort}-[func]{InsertionSort}\n
        insertion_sort.go
        [class]{}-[func]{insertionSort}\n
        insertion_sort.swift
        [class]{}-[func]{insertionSort}\n
        insertion_sort.js
        [class]{}-[func]{insertionSort}\n
        insertion_sort.ts
        [class]{}-[func]{insertionSort}\n
        insertion_sort.dart
        [class]{}-[func]{insertionSort}\n
        insertion_sort.rs
        [class]{}-[func]{insertion_sort}\n
        insertion_sort.c
        [class]{}-[func]{insertionSort}\n
        insertion_sort.kt
        [class]{}-[func]{insertionSort}\n
        insertion_sort.rb
        [class]{}-[func]{insertion_sort}\n
        "},{"location":"chapter_sorting/insertion_sort/#1142","title":"11.4.2 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7279\u6027","text":"
        • \u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n^2)\\)\u3001\u9069\u5fdc\u30bd\u30fc\u30c8\uff1a\u6700\u60aa\u306e\u5834\u5408\u3001\u5404\u633f\u5165\u64cd\u4f5c\u306b\u306f\\(n - 1\\)\u3001\\(n-2\\)\u3001...\u3001\\(2\\)\u3001\\(1\\)\u306e\u30eb\u30fc\u30d7\u304c\u5fc5\u8981\u3067\u3001\u5408\u8a08\u306f\\((n - 1) n / 2\\)\u3068\u306a\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n^2)\\)\u3067\u3059\u3002\u9806\u5e8f\u4ed8\u304d\u30c7\u30fc\u30bf\u306e\u5834\u5408\u3001\u633f\u5165\u64cd\u4f5c\u306f\u65e9\u671f\u306b\u7d42\u4e86\u3057\u307e\u3059\u3002\u5165\u529b\u914d\u5217\u304c\u5b8c\u5168\u306b\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u633f\u5165\u30bd\u30fc\u30c8\u306f\u6700\u826f\u6642\u9593\u8a08\u7b97\u91cf\\(O(n)\\)\u3092\u5b9f\u73fe\u3057\u307e\u3059\u3002
        • \u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u3001\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u30bd\u30fc\u30c8\uff1a\u30dd\u30a4\u30f3\u30bf\\(i\\)\u3068\\(j\\)\u306f\u5b9a\u6570\u91cf\u306e\u8ffd\u52a0\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u5b89\u5b9a\u30bd\u30fc\u30c8\uff1a\u633f\u5165\u64cd\u4f5c\u4e2d\u3001\u7b49\u3057\u3044\u8981\u7d20\u306e\u53f3\u5074\u306b\u8981\u7d20\u3092\u633f\u5165\u3057\u3001\u9806\u5e8f\u3092\u5909\u66f4\u3057\u307e\u305b\u3093\u3002
        "},{"location":"chapter_sorting/insertion_sort/#1143","title":"11.4.3 \u00a0 \u633f\u5165\u30bd\u30fc\u30c8\u306e\u5229\u70b9","text":"

        \u633f\u5165\u30bd\u30fc\u30c8\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n^2)\\)\u3067\u3001\u6b21\u306b\u5b66\u7fd2\u3059\u308b\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n \\log n)\\)\u3067\u3059\u3002\u633f\u5165\u30bd\u30fc\u30c8\u306f\u3088\u308a\u9ad8\u3044\u6642\u9593\u8a08\u7b97\u91cf\u3092\u6301\u3061\u307e\u3059\u304c\u3001\u5c0f\u3055\u306a\u5165\u529b\u30b5\u30a4\u30ba\u3067\u306f\u901a\u5e38\u3088\u308a\u9ad8\u901f\u3067\u3059\u3002

        \u3053\u306e\u7d50\u8ad6\u306f\u7dda\u5f62\u63a2\u7d22\u3068\u4e8c\u5206\u63a2\u7d22\u306e\u7d50\u8ad6\u3068\u4f3c\u3066\u3044\u307e\u3059\u3002\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(n \\log n)\\)\u3067\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306b\u57fa\u3065\u304f\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306a\u3069\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u591a\u304f\u306e\u5834\u5408\u3088\u308a\u591a\u304f\u306e\u5358\u4f4d\u64cd\u4f5c\u3092\u542b\u307f\u307e\u3059\u3002\u5c0f\u3055\u306a\u5165\u529b\u30b5\u30a4\u30ba\u3067\u306f\u3001\\(n^2\\)\u3068\\(n \\log n\\)\u306e\u6570\u5024\u306f\u8fd1\u304f\u3001\u8a08\u7b97\u91cf\u304c\u652f\u914d\u7684\u3067\u306a\u304f\u3001\u30e9\u30a6\u30f3\u30c9\u3042\u305f\u308a\u306e\u5358\u4f4d\u64cd\u4f5c\u6570\u304c\u6c7a\u5b9a\u7684\u306a\u5f79\u5272\u3092\u679c\u305f\u3057\u307e\u3059\u3002

        \u5b9f\u969b\u3001\u591a\u304f\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\uff08Java\u306a\u3069\uff09\u306f\u3001\u7d44\u307f\u8fbc\u307f\u30bd\u30fc\u30c8\u95a2\u6570\u5185\u3067\u633f\u5165\u30bd\u30fc\u30c8\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002\u4e00\u822c\u7684\u306a\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\uff1a\u9577\u3044\u914d\u5217\u306b\u5bfe\u3057\u3066\u306f\u3001\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306a\u3069\u306e\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306b\u57fa\u3065\u304f\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u4f7f\u7528\u3057\u3001\u77ed\u3044\u914d\u5217\u306b\u5bfe\u3057\u3066\u306f\u633f\u5165\u30bd\u30fc\u30c8\u3092\u76f4\u63a5\u4f7f\u7528\u3057\u307e\u3059\u3002

        \u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u3001\u9078\u629e\u30bd\u30fc\u30c8\u3001\u633f\u5165\u30bd\u30fc\u30c8\u306f\u3059\u3079\u3066\u6642\u9593\u8a08\u7b97\u91cf\\(O(n^2)\\)\u3092\u6301\u3061\u307e\u3059\u304c\u3001\u5b9f\u969b\u306b\u306f\u3001\u633f\u5165\u30bd\u30fc\u30c8\u306f\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u3084\u9078\u629e\u30bd\u30fc\u30c8\u3088\u308a\u3082\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u4e3b\u306a\u7406\u7531\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002

        • \u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u306f\u8981\u7d20\u4ea4\u63db\u306b\u57fa\u3065\u304d\u3001\u4e00\u6642\u5909\u6570\u306e\u4f7f\u7528\u304c\u5fc5\u8981\u3067\u30013\u3064\u306e\u5358\u4f4d\u64cd\u4f5c\u3092\u542b\u307f\u307e\u3059\uff1b\u633f\u5165\u30bd\u30fc\u30c8\u306f\u8981\u7d20\u4ee3\u5165\u306b\u57fa\u3065\u304d\u30011\u3064\u306e\u5358\u4f4d\u64cd\u4f5c\u306e\u307f\u304c\u5fc5\u8981\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u306e\u8a08\u7b97\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u306f\u4e00\u822c\u7684\u306b\u633f\u5165\u30bd\u30fc\u30c8\u3088\u308a\u3082\u9ad8\u304f\u306a\u308a\u307e\u3059\u3002
        • \u9078\u629e\u30bd\u30fc\u30c8\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\u5e38\u306b\\(O(n^2)\\)\u3067\u3059\u3002\u90e8\u5206\u7684\u306b\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u30c7\u30fc\u30bf\u306e\u30bb\u30c3\u30c8\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u633f\u5165\u30bd\u30fc\u30c8\u306f\u901a\u5e38\u9078\u629e\u30bd\u30fc\u30c8\u3088\u308a\u3082\u52b9\u7387\u7684\u3067\u3059\u3002
        • \u9078\u629e\u30bd\u30fc\u30c8\u306f\u4e0d\u5b89\u5b9a\u3067\u3001\u30de\u30eb\u30c1\u30ec\u30d9\u30eb\u30bd\u30fc\u30c8\u306b\u9069\u7528\u3067\u304d\u307e\u305b\u3093\u3002
        "},{"location":"chapter_sorting/merge_sort/","title":"11.6 \u00a0 \u30de\u30fc\u30b8\u30bd\u30fc\u30c8","text":"

        \u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u306f\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306b\u57fa\u3065\u304f\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3001\u4e0b\u56f3\u306b\u793a\u3059\u300c\u5206\u5272\u300d\u3068\u300c\u30de\u30fc\u30b8\u300d\u30d5\u30a7\u30fc\u30ba\u3092\u542b\u307f\u307e\u3059\u3002

        1. \u5206\u5272\u30d5\u30a7\u30fc\u30ba\uff1a\u4e2d\u70b9\u304b\u3089\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u5206\u5272\u3057\u3001\u9577\u3044\u914d\u5217\u306e\u30bd\u30fc\u30c8\u554f\u984c\u3092\u3088\u308a\u77ed\u3044\u914d\u5217\u306b\u5909\u63db\u3057\u307e\u3059\u3002
        2. \u30de\u30fc\u30b8\u30d5\u30a7\u30fc\u30ba\uff1a\u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c1\u306b\u306a\u3063\u305f\u3068\u304d\u306b\u5206\u5272\u3092\u505c\u6b62\u3057\u3001\u305d\u306e\u5f8c\u30de\u30fc\u30b8\u3092\u958b\u59cb\u3057\u307e\u3059\u30022\u3064\u306e\u77ed\u3044\u30bd\u30fc\u30c8\u6e08\u307f\u914d\u5217\u3092\u9023\u7d9a\u7684\u306b\u3088\u308a\u9577\u3044\u30bd\u30fc\u30c8\u6e08\u307f\u914d\u5217\u306b\u30de\u30fc\u30b8\u3057\u3001\u30d7\u30ed\u30bb\u30b9\u304c\u5b8c\u4e86\u3059\u308b\u307e\u3067\u7d9a\u884c\u3057\u307e\u3059\u3002

        \u56f3 11-10 \u00a0 The divide and merge phases of merge sort

        "},{"location":"chapter_sorting/merge_sort/#1161","title":"11.6.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30ef\u30fc\u30af\u30d5\u30ed\u30fc","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u300c\u5206\u5272\u30d5\u30a7\u30fc\u30ba\u300d\u306f\u4e2d\u70b9\u304b\u3089\u914d\u5217\u3092\u4e0a\u304b\u3089\u4e0b\u306b2\u3064\u306e\u30b5\u30d6\u914d\u5217\u306b\u518d\u5e30\u7684\u306b\u5206\u5272\u3057\u307e\u3059\u3002

        1. \u4e2d\u70b9mid\u3092\u8a08\u7b97\u3057\u3001\u5de6\u30b5\u30d6\u914d\u5217\uff08\u533a\u9593[left, mid]\uff09\u3068\u53f3\u30b5\u30d6\u914d\u5217\uff08\u533a\u9593[mid + 1, right]\uff09\u3092\u518d\u5e30\u7684\u306b\u5206\u5272\u3057\u307e\u3059\u3002
        2. \u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c1\u306b\u306a\u308b\u307e\u3067\u30b9\u30c6\u30c3\u30d71.\u3092\u518d\u5e30\u7684\u306b\u7d9a\u884c\u3057\u3001\u305d\u306e\u5f8c\u505c\u6b62\u3057\u307e\u3059\u3002

        \u300c\u30de\u30fc\u30b8\u30d5\u30a7\u30fc\u30ba\u300d\u306f\u5de6\u3068\u53f3\u306e\u30b5\u30d6\u914d\u5217\u3092\u4e0b\u304b\u3089\u4e0a\u306b\u30bd\u30fc\u30c8\u6e08\u307f\u914d\u5217\u306b\u7d50\u5408\u3057\u307e\u3059\u3002\u91cd\u8981\u306a\u306e\u306f\u3001\u30de\u30fc\u30b8\u304c\u9577\u30551\u306e\u30b5\u30d6\u914d\u5217\u304b\u3089\u958b\u59cb\u3055\u308c\u3001\u30de\u30fc\u30b8\u30d5\u30a7\u30fc\u30ba\u4e2d\u306b\u5404\u30b5\u30d6\u914d\u5217\u304c\u30bd\u30fc\u30c8\u3055\u308c\u308b\u3053\u3068\u3067\u3059\u3002

        <1><2><3><4><5><6><7><8><9><10>

        \u56f3 11-11 \u00a0 Merge sort process

        \u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u306e\u518d\u5e30\u9806\u5e8f\u306f\u4e8c\u5206\u6728\u306e\u5f8c\u9806\u6a2a\u65ad\u3068\u4e00\u81f4\u3059\u308b\u3053\u3068\u304c\u89b3\u5bdf\u3067\u304d\u307e\u3059\u3002

        • \u5f8c\u9806\u6a2a\u65ad\uff1a\u307e\u305a\u5de6\u306e\u30b5\u30d6\u30c4\u30ea\u30fc\u3092\u518d\u5e30\u7684\u306b\u6a2a\u65ad\u3057\u3001\u6b21\u306b\u53f3\u306e\u30b5\u30d6\u30c4\u30ea\u30fc\u3092\u6a2a\u65ad\u3057\u3001\u6700\u5f8c\u306b\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u51e6\u7406\u3057\u307e\u3059\u3002
        • \u30de\u30fc\u30b8\u30bd\u30fc\u30c8\uff1a\u307e\u305a\u5de6\u306e\u30b5\u30d6\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u51e6\u7406\u3057\u3001\u6b21\u306b\u53f3\u306e\u30b5\u30d6\u914d\u5217\u3092\u51e6\u7406\u3057\u3001\u6700\u5f8c\u306b\u30de\u30fc\u30b8\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002

        \u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u306e\u5b9f\u88c5\u306f\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u306b\u793a\u3055\u308c\u307e\u3059\u3002nums\u3067\u30de\u30fc\u30b8\u3055\u308c\u308b\u533a\u9593\u306f[left, right]\u3067\u3001tmp\u306e\u5bfe\u5fdc\u3059\u308b\u533a\u9593\u306f[0, right - left]\u3067\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby merge_sort.py
        def merge(nums: list[int], left: int, mid: int, right: int):\n    \"\"\"\u5de6\u30b5\u30d6\u914d\u5217\u3068\u53f3\u30b5\u30d6\u914d\u5217\u3092\u30de\u30fc\u30b8\"\"\"\n    # \u5de6\u30b5\u30d6\u914d\u5217\u533a\u9593\u306f [left, mid]\u3001\u53f3\u30b5\u30d6\u914d\u5217\u533a\u9593\u306f [mid+1, right]\n    # \u4e00\u6642\u914d\u5217 tmp \u3092\u4f5c\u6210\u3057\u3066\u30de\u30fc\u30b8\u7d50\u679c\u3092\u683c\u7d0d\n    tmp = [0] * (right - left + 1)\n    # \u5de6\u53f3\u30b5\u30d6\u914d\u5217\u306e\u958b\u59cb\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u521d\u671f\u5316\n    i, j, k = left, mid + 1, 0\n    # \u4e21\u65b9\u306e\u30b5\u30d6\u914d\u5217\u306b\u8981\u7d20\u304c\u6b8b\u3063\u3066\u3044\u308b\u9593\u3001\u3088\u308a\u5c0f\u3055\u3044\u8981\u7d20\u3092\u4e00\u6642\u914d\u5217\u306b\u30b3\u30d4\u30fc\n    while i <= mid and j <= right:\n        if nums[i] <= nums[j]:\n            tmp[k] = nums[i]\n            i += 1\n        else:\n            tmp[k] = nums[j]\n            j += 1\n        k += 1\n    # \u6b8b\u3063\u305f\u5de6\u53f3\u30b5\u30d6\u914d\u5217\u306e\u8981\u7d20\u3092\u4e00\u6642\u914d\u5217\u306b\u30b3\u30d4\u30fc\n    while i <= mid:\n        tmp[k] = nums[i]\n        i += 1\n        k += 1\n    while j <= right:\n        tmp[k] = nums[j]\n        j += 1\n        k += 1\n    # \u4e00\u6642\u914d\u5217 tmp \u306e\u8981\u7d20\u3092\u5143\u306e\u914d\u5217 nums \u306e\u5bfe\u5fdc\u3059\u308b\u533a\u9593\u306b\u30b3\u30d4\u30fc\u30d0\u30c3\u30af\n    for k in range(0, len(tmp)):\n        nums[left + k] = tmp[k]\n\ndef merge_sort(nums: list[int], left: int, right: int):\n    \"\"\"\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\"\"\"\n    # \u7d42\u4e86\u6761\u4ef6\n    if left >= right:\n        return  # \u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c1\u306e\u3068\u304d\u306b\u518d\u5e30\u3092\u7d42\u4e86\n    # \u5206\u5272\u6bb5\u968e\n    mid = left + (right - left) // 2  # \u4e2d\u70b9\u3092\u8a08\u7b97\n    merge_sort(nums, left, mid)  # \u5de6\u30b5\u30d6\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u51e6\u7406\n    merge_sort(nums, mid + 1, right)  # \u53f3\u30b5\u30d6\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u51e6\u7406\n    # \u30de\u30fc\u30b8\u6bb5\u968e\n    merge(nums, left, mid, right)\n
        merge_sort.cpp
        /* \u5de6\u30b5\u30d6\u914d\u5217\u3068\u53f3\u30b5\u30d6\u914d\u5217\u3092\u30de\u30fc\u30b8 */\nvoid merge(vector<int> &nums, int left, int mid, int right) {\n    // \u5de6\u30b5\u30d6\u914d\u5217\u306e\u533a\u9593\u306f[left, mid]\u3001\u53f3\u30b5\u30d6\u914d\u5217\u306e\u533a\u9593\u306f[mid+1, right]\n    // \u30de\u30fc\u30b8\u7d50\u679c\u3092\u4fdd\u5b58\u3059\u308b\u4e00\u6642\u914d\u5217tmp\u3092\u4f5c\u6210\n    vector<int> tmp(right - left + 1);\n    // \u5de6\u53f3\u30b5\u30d6\u914d\u5217\u306e\u958b\u59cb\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u521d\u671f\u5316\n    int i = left, j = mid + 1, k = 0;\n    // \u4e21\u30b5\u30d6\u914d\u5217\u306b\u8981\u7d20\u304c\u3042\u308b\u9593\u3001\u5c0f\u3055\u3044\u65b9\u306e\u8981\u7d20\u3092\u4e00\u6642\u914d\u5217\u306b\u30b3\u30d4\u30fc\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++];\n        else\n            tmp[k++] = nums[j++];\n    }\n    // \u5de6\u53f3\u30b5\u30d6\u914d\u5217\u306e\u6b8b\u308a\u306e\u8981\u7d20\u3092\u4e00\u6642\u914d\u5217\u306b\u30b3\u30d4\u30fc\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // \u4e00\u6642\u914d\u5217tmp\u306e\u8981\u7d20\u3092\u5143\u306e\u914d\u5217nums\u306e\u5bfe\u5fdc\u3059\u308b\u533a\u9593\u306b\u30b3\u30d4\u30fc\n    for (k = 0; k < tmp.size(); k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* \u30de\u30fc\u30b8\u30bd\u30fc\u30c8 */\nvoid mergeSort(vector<int> &nums, int left, int right) {\n    // \u7d42\u4e86\u6761\u4ef6\n    if (left >= right)\n        return; // \u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c1\u306e\u6642\u3001\u518d\u5e30\u3092\u7d42\u4e86\n    // \u5206\u5272\u6bb5\u968e\n    int mid = left + (right - left) / 2;    // \u4e2d\u70b9\u3092\u8a08\u7b97\n    mergeSort(nums, left, mid);      // \u5de6\u30b5\u30d6\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u51e6\u7406\n    mergeSort(nums, mid + 1, right); // \u53f3\u30b5\u30d6\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u51e6\u7406\n    // \u30de\u30fc\u30b8\u6bb5\u968e\n    merge(nums, left, mid, right);\n}\n
        merge_sort.java
        /* \u5de6\u90e8\u5206\u914d\u5217\u3068\u53f3\u90e8\u5206\u914d\u5217\u3092\u30de\u30fc\u30b8 */\nvoid merge(int[] nums, int left, int mid, int right) {\n    // \u5de6\u90e8\u5206\u914d\u5217\u533a\u9593\u306f [left, mid]\u3001\u53f3\u90e8\u5206\u914d\u5217\u533a\u9593\u306f [mid+1, right]\n    // \u4e00\u6642\u914d\u5217 tmp \u3092\u4f5c\u6210\u3057\u3066\u30de\u30fc\u30b8\u7d50\u679c\u3092\u683c\u7d0d\n    int[] tmp = new int[right - left + 1];\n    // \u5de6\u53f3\u90e8\u5206\u914d\u5217\u306e\u958b\u59cb\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u521d\u671f\u5316\n    int i = left, j = mid + 1, k = 0;\n    // \u4e21\u90e8\u5206\u914d\u5217\u306b\u307e\u3060\u8981\u7d20\u304c\u3042\u308b\u9593\u3001\u6bd4\u8f03\u3057\u3066\u3088\u308a\u5c0f\u3055\u3044\u8981\u7d20\u3092\u4e00\u6642\u914d\u5217\u306b\u30b3\u30d4\u30fc\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++];\n        else\n            tmp[k++] = nums[j++];\n    }\n    // \u5de6\u53f3\u90e8\u5206\u914d\u5217\u306e\u6b8b\u308a\u306e\u8981\u7d20\u3092\u4e00\u6642\u914d\u5217\u306b\u30b3\u30d4\u30fc\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // \u4e00\u6642\u914d\u5217 tmp \u306e\u8981\u7d20\u3092\u5143\u306e\u914d\u5217 nums \u306e\u5bfe\u5fdc\u3059\u308b\u533a\u9593\u306b\u30b3\u30d4\u30fc\u30d0\u30c3\u30af\n    for (k = 0; k < tmp.length; k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* \u30de\u30fc\u30b8\u30bd\u30fc\u30c8 */\nvoid mergeSort(int[] nums, int left, int right) {\n    // \u7d42\u4e86\u6761\u4ef6\n    if (left >= right)\n        return; // \u90e8\u5206\u914d\u5217\u306e\u9577\u3055\u304c 1 \u306e\u3068\u304d\u518d\u5e30\u3092\u7d42\u4e86\n    // \u5206\u5272\u6bb5\u968e\n    int mid = left + (right - left) / 2; // \u4e2d\u70b9\u3092\u8a08\u7b97\n    mergeSort(nums, left, mid); // \u5de6\u90e8\u5206\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u51e6\u7406\n    mergeSort(nums, mid + 1, right); // \u53f3\u90e8\u5206\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u51e6\u7406\n    // \u30de\u30fc\u30b8\u6bb5\u968e\n    merge(nums, left, mid, right);\n}\n
        merge_sort.cs
        [class]{merge_sort}-[func]{Merge}\n\n[class]{merge_sort}-[func]{MergeSort}\n
        merge_sort.go
        [class]{}-[func]{merge}\n\n[class]{}-[func]{mergeSort}\n
        merge_sort.swift
        [class]{}-[func]{merge}\n\n[class]{}-[func]{mergeSort}\n
        merge_sort.js
        [class]{}-[func]{merge}\n\n[class]{}-[func]{mergeSort}\n
        merge_sort.ts
        [class]{}-[func]{merge}\n\n[class]{}-[func]{mergeSort}\n
        merge_sort.dart
        [class]{}-[func]{merge}\n\n[class]{}-[func]{mergeSort}\n
        merge_sort.rs
        [class]{}-[func]{merge}\n\n[class]{}-[func]{merge_sort}\n
        merge_sort.c
        [class]{}-[func]{merge}\n\n[class]{}-[func]{mergeSort}\n
        merge_sort.kt
        [class]{}-[func]{merge}\n\n[class]{}-[func]{mergeSort}\n
        merge_sort.rb
        [class]{}-[func]{merge}\n\n[class]{}-[func]{merge_sort}\n
        "},{"location":"chapter_sorting/merge_sort/#1162","title":"11.6.2 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7279\u6027","text":"
        • \\(O(n \\log n)\\)\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3001\u975e\u9069\u5fdc\u30bd\u30fc\u30c8\uff1a\u5206\u5272\u306b\u3088\u308a\u9ad8\u3055\\(\\log n\\)\u306e\u518d\u5e30\u30c4\u30ea\u30fc\u304c\u4f5c\u6210\u3055\u308c\u3001\u5404\u5c64\u3067\u5408\u8a08\\(n\\)\u56de\u306e\u64cd\u4f5c\u3092\u30de\u30fc\u30b8\u3057\u3001\u5168\u4f53\u7684\u306a\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n \\log n)\\)\u3068\u306a\u308a\u307e\u3059\u3002
        • \\(O(n)\\)\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u3001\u975e\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u30bd\u30fc\u30c8\uff1a\u518d\u5e30\u6df1\u5ea6\u306f\\(\\log n\\)\u3067\u3001\\(O(\\log n)\\)\u306e\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u30de\u30fc\u30b8\u64cd\u4f5c\u306b\u306f\u88dc\u52a9\u914d\u5217\u304c\u5fc5\u8981\u3067\u3001\u8ffd\u52a0\u306e\\(O(n)\\)\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u5b89\u5b9a\u30bd\u30fc\u30c8\uff1a\u30de\u30fc\u30b8\u30d7\u30ed\u30bb\u30b9\u4e2d\u3001\u7b49\u3057\u3044\u8981\u7d20\u306e\u9806\u5e8f\u306f\u5909\u66f4\u3055\u308c\u307e\u305b\u3093\u3002
        "},{"location":"chapter_sorting/merge_sort/#1163","title":"11.6.3 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30bd\u30fc\u30c8","text":"

        \u9023\u7d50\u30ea\u30b9\u30c8\u306e\u5834\u5408\u3001\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u306f\u4ed6\u306e\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3088\u308a\u3082\u5927\u304d\u306a\u5229\u70b9\u304c\u3042\u308a\u307e\u3059\u3002\u9023\u7d50\u30ea\u30b9\u30c8\u30bd\u30fc\u30c8\u30bf\u30b9\u30af\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u3092\\(O(1)\\)\u306b\u6700\u9069\u5316\u3067\u304d\u307e\u3059\u3002

        • \u5206\u5272\u30d5\u30a7\u30fc\u30ba\uff1a\u300c\u518d\u5e30\u300d\u306e\u4ee3\u308f\u308a\u306b\u300c\u53cd\u5fa9\u300d\u3092\u4f7f\u7528\u3057\u3066\u9023\u7d50\u30ea\u30b9\u30c8\u5206\u5272\u4f5c\u696d\u3092\u5b9f\u884c\u3067\u304d\u308b\u305f\u3081\u3001\u518d\u5e30\u3067\u4f7f\u7528\u3055\u308c\u308b\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u7bc0\u7d04\u3067\u304d\u307e\u3059\u3002
        • \u30de\u30fc\u30b8\u30d5\u30a7\u30fc\u30ba\uff1a\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u306f\u3001\u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664\u64cd\u4f5c\u306f\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u5909\u66f4\u3059\u308b\u3053\u3068\u3067\u5b9f\u73fe\u3067\u304d\u308b\u305f\u3081\u3001\u30de\u30fc\u30b8\u30d5\u30a7\u30fc\u30ba\uff082\u3064\u306e\u77ed\u3044\u9806\u5e8f\u4ed8\u304d\u30ea\u30b9\u30c8\u30921\u3064\u306e\u9577\u3044\u9806\u5e8f\u4ed8\u304d\u30ea\u30b9\u30c8\u306b\u7d50\u5408\uff09\u4e2d\u306b\u8ffd\u52a0\u306e\u30ea\u30b9\u30c8\u3092\u4f5c\u6210\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u305b\u3093\u3002

        \u5b9f\u88c5\u306e\u8a73\u7d30\u306f\u6bd4\u8f03\u7684\u8907\u96d1\u3067\u3001\u8208\u5473\u306e\u3042\u308b\u8aad\u8005\u306f\u95a2\u9023\u8cc7\u6599\u3092\u53c2\u7167\u3057\u3066\u5b66\u7fd2\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        "},{"location":"chapter_sorting/quick_sort/","title":"11.5 \u00a0 \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8","text":"

        \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306f\u5206\u5272\u7d71\u6cbb\u6226\u7565\u306b\u57fa\u3065\u304f\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3001\u305d\u306e\u52b9\u7387\u6027\u3068\u5e45\u5e83\u3044\u5fdc\u7528\u3067\u77e5\u3089\u308c\u3066\u3044\u307e\u3059\u3002

        \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306e\u30b3\u30a2\u64cd\u4f5c\u306f\u300c\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u300d\u3067\u3001\u914d\u5217\u304b\u3089\u8981\u7d20\u3092\u300c\u30d4\u30dc\u30c3\u30c8\u300d\u3068\u3057\u3066\u9078\u629e\u3057\u3001\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5c0f\u3055\u3044\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u305d\u306e\u5de6\u5074\u306b\u79fb\u52d5\u3057\u3001\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5927\u304d\u3044\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u305d\u306e\u53f3\u5074\u306b\u79fb\u52d5\u3059\u308b\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u3066\u3044\u307e\u3059\u3002\u5177\u4f53\u7684\u306b\u3001\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u306e\u30d7\u30ed\u30bb\u30b9\u306f\u4e0b\u56f3\u306b\u793a\u3055\u308c\u307e\u3059\u3002

        1. \u914d\u5217\u306e\u6700\u3082\u5de6\u306e\u8981\u7d20\u3092\u30d4\u30dc\u30c3\u30c8\u3068\u3057\u3066\u9078\u629e\u3057\u30012\u3064\u306e\u30dd\u30a4\u30f3\u30bfi\u3068j\u3092\u521d\u671f\u5316\u3057\u3066\u914d\u5217\u306e\u4e21\u7aef\u3092\u305d\u308c\u305e\u308c\u6307\u3059\u3088\u3046\u306b\u3057\u307e\u3059\u3002
        2. \u5404\u30e9\u30a6\u30f3\u30c9\u3067i\uff08j\uff09\u3092\u4f7f\u7528\u3057\u3066\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5927\u304d\u3044\uff08\u5c0f\u3055\u3044\uff09\u6700\u521d\u306e\u8981\u7d20\u3092\u63a2\u7d22\u3057\u3001\u6b21\u306b\u3053\u308c\u30892\u3064\u306e\u8981\u7d20\u3092\u4ea4\u63db\u3059\u308b\u30eb\u30fc\u30d7\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002
        3. i\u3068j\u304c\u51fa\u4f1a\u3046\u307e\u3067\u30b9\u30c6\u30c3\u30d72.\u3092\u7e70\u308a\u8fd4\u3057\u3001\u6700\u5f8c\u306b\u30d4\u30dc\u30c3\u30c8\u30922\u3064\u306e\u30b5\u30d6\u914d\u5217\u306e\u5883\u754c\u306b\u4ea4\u63db\u3057\u307e\u3059\u3002
        <1><2><3><4><5><6><7><8><9>

        \u56f3 11-8 \u00a0 Pivot division process

        \u30d4\u30dc\u30c3\u30c8\u5206\u5272\u5f8c\u3001\u5143\u306e\u914d\u5217\u306f3\u3064\u306e\u90e8\u5206\u306b\u5206\u5272\u3055\u308c\u307e\u3059\uff1a\u5de6\u30b5\u30d6\u914d\u5217\u3001\u30d4\u30dc\u30c3\u30c8\u3001\u53f3\u30b5\u30d6\u914d\u5217\u3067\u3001\u300c\u5de6\u30b5\u30d6\u914d\u5217\u306e\u4efb\u610f\u306e\u8981\u7d20 \\(\\leq\\) \u30d4\u30dc\u30c3\u30c8 \\(\\leq\\) \u53f3\u30b5\u30d6\u914d\u5217\u306e\u4efb\u610f\u306e\u8981\u7d20\u300d\u3092\u6e80\u305f\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u3053\u308c\u30892\u3064\u306e\u30b5\u30d6\u914d\u5217\u306e\u307f\u3092\u30bd\u30fc\u30c8\u3059\u308c\u3070\u3088\u3044\u306e\u3067\u3059\u3002

        \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306e\u5206\u5272\u7d71\u6cbb\u6226\u7565

        \u30d4\u30dc\u30c3\u30c8\u5206\u5272\u306e\u672c\u8cea\u306f\u3001\u3088\u308a\u9577\u3044\u914d\u5217\u306e\u30bd\u30fc\u30c8\u554f\u984c\u3092\u3088\u308a\u77ed\u30442\u3064\u306e\u914d\u5217\u306b\u7c21\u7d20\u5316\u3059\u308b\u3053\u3068\u3067\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
        def partition(self, nums: list[int], left: int, right: int) -> int:\n    \"\"\"\u5206\u5272\"\"\"\n    # nums[left] \u3092\u30d4\u30dc\u30c3\u30c8\u3068\u3057\u3066\u4f7f\u7528\n    i, j = left, right\n    while i < j:\n        while i < j and nums[j] >= nums[left]:\n            j -= 1  # \u53f3\u304b\u3089\u5de6\u3078\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5c0f\u3055\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u63a2\u3059\n        while i < j and nums[i] <= nums[left]:\n            i += 1  # \u5de6\u304b\u3089\u53f3\u3078\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5927\u304d\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u63a2\u3059\n        # \u8981\u7d20\u3092\u4ea4\u63db\n        nums[i], nums[j] = nums[j], nums[i]\n    # \u30d4\u30dc\u30c3\u30c8\u30922\u3064\u306e\u30b5\u30d6\u914d\u5217\u306e\u5883\u754c\u306b\u4ea4\u63db\n    nums[i], nums[left] = nums[left], nums[i]\n    return i  # \u30d4\u30dc\u30c3\u30c8\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n
        quick_sort.cpp
        /* \u5206\u5272 */\nint partition(vector<int> &nums, int left, int right) {\n    // nums[left]\u3092\u30d4\u30dc\u30c3\u30c8\u3068\u3057\u3066\u4f7f\u7528\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--; // \u53f3\u304b\u3089\u5de6\u3078\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5c0f\u3055\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u691c\u7d22\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u5de6\u304b\u3089\u53f3\u3078\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5927\u304d\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u691c\u7d22\n        swap(nums, i, j); // \u3053\u308c\u3089\u4e8c\u3064\u306e\u8981\u7d20\u3092\u4ea4\u63db\n    }\n    swap(nums, i, left); // \u30d4\u30dc\u30c3\u30c8\u3092\u4e8c\u3064\u306e\u30b5\u30d6\u914d\u5217\u306e\u5883\u754c\u306b\u4ea4\u63db\n    return i;            // \u30d4\u30dc\u30c3\u30c8\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n}\n
        quick_sort.java
        /* \u8981\u7d20\u3092\u4ea4\u63db */\nvoid swap(int[] nums, int i, int j) {\n    int tmp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = tmp;\n}\n\n/* \u5206\u5272 */\nint partition(int[] nums, int left, int right) {\n    // nums[left] \u3092\u57fa\u6e96\u5024\u3068\u3057\u3066\u4f7f\u7528\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // \u53f3\u304b\u3089\u5de6\u3078\u3001\u57fa\u6e96\u5024\u3088\u308a\u5c0f\u3055\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u691c\u7d22\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u5de6\u304b\u3089\u53f3\u3078\u3001\u57fa\u6e96\u5024\u3088\u308a\u5927\u304d\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u691c\u7d22\n        swap(nums, i, j); // \u3053\u308c\u30892\u3064\u306e\u8981\u7d20\u3092\u4ea4\u63db\n    }\n    swap(nums, i, left);  // \u57fa\u6e96\u5024\u30922\u3064\u306e\u90e8\u5206\u914d\u5217\u306e\u5883\u754c\u306b\u4ea4\u63db\n    return i;             // \u57fa\u6e96\u5024\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n}\n
        quick_sort.cs
        [class]{quickSort}-[func]{Swap}\n\n[class]{quickSort}-[func]{Partition}\n
        quick_sort.go
        [class]{quickSort}-[func]{partition}\n
        quick_sort.swift
        [class]{}-[func]{partition}\n
        quick_sort.js
        [class]{QuickSort}-[func]{swap}\n\n[class]{QuickSort}-[func]{partition}\n
        quick_sort.ts
        [class]{QuickSort}-[func]{swap}\n\n[class]{QuickSort}-[func]{partition}\n
        quick_sort.dart
        [class]{QuickSort}-[func]{_swap}\n\n[class]{QuickSort}-[func]{_partition}\n
        quick_sort.rs
        [class]{QuickSort}-[func]{partition}\n
        quick_sort.c
        [class]{}-[func]{swap}\n\n[class]{}-[func]{partition}\n
        quick_sort.kt
        [class]{}-[func]{swap}\n\n[class]{}-[func]{partition}\n
        quick_sort.rb
        [class]{QuickSort}-[func]{partition}\n
        "},{"location":"chapter_sorting/quick_sort/#1151","title":"11.5.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u30d7\u30ed\u30bb\u30b9","text":"

        \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306e\u5168\u4f53\u7684\u306a\u30d7\u30ed\u30bb\u30b9\u306f\u4e0b\u56f3\u306b\u793a\u3055\u308c\u307e\u3059\u3002

        1. \u307e\u305a\u3001\u5143\u306e\u914d\u5217\u306b\u5bfe\u3057\u3066\u300c\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u300d\u3092\u5b9f\u884c\u3057\u3001\u672a\u30bd\u30fc\u30c8\u306e\u5de6\u3068\u53f3\u306e\u30b5\u30d6\u914d\u5217\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002
        2. \u6b21\u306b\u3001\u5de6\u3068\u53f3\u306e\u30b5\u30d6\u914d\u5217\u306b\u5bfe\u3057\u3066\u305d\u308c\u305e\u308c\u518d\u5e30\u7684\u306b\u300c\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u300d\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002
        3. \u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c1\u306b\u306a\u308b\u307e\u3067\u518d\u5e30\u3092\u7d9a\u3051\u3001\u914d\u5217\u5168\u4f53\u306e\u30bd\u30fc\u30c8\u3092\u5b8c\u4e86\u3057\u307e\u3059\u3002

        \u56f3 11-9 \u00a0 Quick sort process

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
        def quick_sort(self, nums: list[int], left: int, right: int):\n    \"\"\"\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\"\"\"\n    # \u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c1\u306e\u3068\u304d\u306b\u518d\u5e30\u3092\u7d42\u4e86\n    if left >= right:\n        return\n    # \u5206\u5272\n    pivot = self.partition(nums, left, right)\n    # \u5de6\u30b5\u30d6\u914d\u5217\u3068\u53f3\u30b5\u30d6\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u51e6\u7406\n    self.quick_sort(nums, left, pivot - 1)\n    self.quick_sort(nums, pivot + 1, right)\n
        quick_sort.cpp
        /* \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8 */\nvoid quickSort(vector<int> &nums, int left, int right) {\n    // \u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c1\u306e\u6642\u3001\u518d\u5e30\u3092\u7d42\u4e86\n    if (left >= right)\n        return;\n    // \u5206\u5272\n    int pivot = partition(nums, left, right);\n    // \u5de6\u30b5\u30d6\u914d\u5217\u3068\u53f3\u30b5\u30d6\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u51e6\u7406\n    quickSort(nums, left, pivot - 1);\n    quickSort(nums, pivot + 1, right);\n}\n
        quick_sort.java
        /* \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8 */\nvoid quickSort(int[] nums, int left, int right) {\n    // \u90e8\u5206\u914d\u5217\u306e\u9577\u3055\u304c 1 \u306e\u3068\u304d\u518d\u5e30\u3092\u7d42\u4e86\n    if (left >= right)\n        return;\n    // \u5206\u5272\n    int pivot = partition(nums, left, right);\n    // \u5de6\u90e8\u5206\u914d\u5217\u3068\u53f3\u90e8\u5206\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u51e6\u7406\n    quickSort(nums, left, pivot - 1);\n    quickSort(nums, pivot + 1, right);\n}\n
        quick_sort.cs
        [class]{quickSort}-[func]{QuickSort}\n
        quick_sort.go
        [class]{quickSort}-[func]{quickSort}\n
        quick_sort.swift
        [class]{}-[func]{quickSort}\n
        quick_sort.js
        [class]{QuickSort}-[func]{quickSort}\n
        quick_sort.ts
        [class]{QuickSort}-[func]{quickSort}\n
        quick_sort.dart
        [class]{QuickSort}-[func]{quickSort}\n
        quick_sort.rs
        [class]{QuickSort}-[func]{quick_sort}\n
        quick_sort.c
        [class]{}-[func]{quickSort}\n
        quick_sort.kt
        [class]{}-[func]{quickSort}\n
        quick_sort.rb
        [class]{QuickSort}-[func]{quick_sort}\n
        "},{"location":"chapter_sorting/quick_sort/#1152","title":"11.5.2 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7279\u5fb4","text":"
        • \\(O(n \\log n)\\)\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3001\u975e\u9069\u5fdc\u30bd\u30fc\u30c8\uff1a\u5e73\u5747\u7684\u306a\u30b1\u30fc\u30b9\u3067\u306f\u3001\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u306e\u518d\u5e30\u30ec\u30d9\u30eb\u306f\\(\\log n\\)\u3067\u3001\u30ec\u30d9\u30eb\u3042\u305f\u308a\u306e\u30eb\u30fc\u30d7\u306e\u7dcf\u6570\u306f\\(n\\)\u3067\u3042\u308a\u3001\u5168\u4f53\u3067\\(O(n \\log n)\\)\u306e\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u6700\u60aa\u306e\u5834\u5408\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u306f\u9577\u3055\\(n\\)\u306e\u914d\u5217\u3092\u9577\u3055\\(0\\)\u3068\\(n - 1\\)\u306e2\u3064\u306e\u30b5\u30d6\u914d\u5217\u306b\u5206\u5272\u3057\u3001\u518d\u5e30\u30ec\u30d9\u30eb\u6570\u304c\\(n\\)\u306b\u9054\u3059\u308b\u3068\u3001\u5404\u30ec\u30d9\u30eb\u306e\u30eb\u30fc\u30d7\u6570\u306f\\(n\\)\u3067\u3001\u4f7f\u7528\u3055\u308c\u308b\u7dcf\u6642\u9593\u306f\\(O(n^2)\\)\u3067\u3059\u3002
        • \\(O(n)\\)\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u3001\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u30bd\u30fc\u30c8\uff1a\u5165\u529b\u914d\u5217\u304c\u5b8c\u5168\u306b\u9006\u9806\u306e\u5834\u5408\u3001\u6700\u60aa\u306e\u518d\u5e30\u6df1\u5ea6\u306f\\(n\\)\u306b\u9054\u3057\u3001\\(O(n)\\)\u306e\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u30bd\u30fc\u30c8\u64cd\u4f5c\u306f\u8ffd\u52a0\u306e\u914d\u5217\u306e\u52a9\u3051\u306a\u3057\u306b\u5143\u306e\u914d\u5217\u3067\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002
        • \u975e\u5b89\u5b9a\u30bd\u30fc\u30c8\uff1a\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u306e\u6700\u7d42\u30b9\u30c6\u30c3\u30d7\u3067\u3001\u30d4\u30dc\u30c3\u30c8\u306f\u7b49\u3057\u3044\u8981\u7d20\u306e\u53f3\u5074\u306b\u4ea4\u63db\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
        "},{"location":"chapter_sorting/quick_sort/#1153","title":"11.5.3 \u00a0 \u306a\u305c\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306f\u9ad8\u901f\u306a\u306e\u304b","text":"

        \u540d\u524d\u304c\u793a\u3059\u3088\u3046\u306b\u3001\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306f\u52b9\u7387\u6027\u306e\u9762\u3067\u4e00\u5b9a\u306e\u5229\u70b9\u3092\u6301\u3064\u3079\u304d\u3067\u3059\u3002\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306e\u5e73\u5747\u6642\u9593\u8a08\u7b97\u91cf\u306f\u300c\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u300d\u3084\u300c\u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\u300d\u3068\u540c\u3058\u3067\u3059\u304c\u3001\u4ee5\u4e0b\u306e\u7406\u7531\u3067\u4e00\u822c\u7684\u306b\u3088\u308a\u52b9\u7387\u7684\u3067\u3059\u3002

        • \u6700\u60aa\u30b1\u30fc\u30b9\u30b7\u30ca\u30ea\u30aa\u306e\u4f4e\u3044\u78ba\u7387\uff1a\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306e\u6700\u60aa\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n^2)\\)\u3067\u3001\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u307b\u3069\u5b89\u5b9a\u3057\u3066\u3044\u307e\u305b\u3093\u304c\u3001\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306f\\(O(n \\log n)\\)\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3067\u52d5\u4f5c\u3067\u304d\u307e\u3059\u3002
        • \u9ad8\u3044\u30ad\u30e3\u30c3\u30b7\u30e5\u5229\u7528\u7387\uff1a\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u64cd\u4f5c\u4e2d\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u30b5\u30d6\u914d\u5217\u5168\u4f53\u3092\u30ad\u30e3\u30c3\u30b7\u30e5\u306b\u30ed\u30fc\u30c9\u3067\u304d\u308b\u305f\u3081\u3001\u8981\u7d20\u306b\u3088\u308a\u52b9\u7387\u7684\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u307e\u3059\u3002\u5bfe\u7167\u7684\u306b\u3001\u300c\u30d2\u30fc\u30d7\u30bd\u30fc\u30c8\u300d\u306a\u3069\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u8981\u7d20\u306b\u30b8\u30e3\u30f3\u30d7\u65b9\u5f0f\u3067\u30a2\u30af\u30bb\u30b9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u3053\u306e\u7279\u5fb4\u3092\u6b20\u3044\u3066\u3044\u307e\u3059\u3002
        • \u8a08\u7b97\u91cf\u306e\u5c0f\u3055\u306a\u5b9a\u6570\u4fc2\u6570\uff1a\u4e0a\u8a183\u3064\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u4e2d\u3067\u3001\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306f\u6bd4\u8f03\u3001\u4ee3\u5165\u3001\u4ea4\u63db\u306a\u3069\u306e\u64cd\u4f5c\u306e\u7dcf\u6570\u304c\u6700\u3082\u5c11\u306a\u3044\u3067\u3059\u3002\u3053\u308c\u306f\u300c\u633f\u5165\u30bd\u30fc\u30c8\u300d\u304c\u300c\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u300d\u3088\u308a\u3082\u9ad8\u901f\u306a\u7406\u7531\u3068\u4f3c\u3066\u3044\u307e\u3059\u3002
        "},{"location":"chapter_sorting/quick_sort/#1154","title":"11.5.4 \u00a0 \u30d4\u30dc\u30c3\u30c8\u6700\u9069\u5316","text":"

        \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306e\u6642\u9593\u52b9\u7387\u306f\u7279\u5b9a\u306e\u5165\u529b\u3067\u52a3\u5316\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u5165\u529b\u914d\u5217\u304c\u5b8c\u5168\u306b\u9006\u9806\u306e\u5834\u5408\u3001\u6700\u3082\u5de6\u306e\u8981\u7d20\u3092\u30d4\u30dc\u30c3\u30c8\u3068\u3057\u3066\u9078\u629e\u3059\u308b\u305f\u3081\u3001\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u5f8c\u3001\u30d4\u30dc\u30c3\u30c8\u306f\u914d\u5217\u306e\u53f3\u7aef\u306b\u4ea4\u63db\u3055\u308c\u3001\u5de6\u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c\\(n - 1\\)\u3001\u53f3\u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c\\(0\\)\u306b\u306a\u308a\u307e\u3059\u3002\u3053\u306e\u65b9\u6cd5\u3092\u7d9a\u3051\u308b\u3068\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u3067\u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c\\(0\\)\u306b\u306a\u308a\u3001\u5206\u5272\u7d71\u6cbb\u6226\u7565\u304c\u5931\u6557\u3057\u3001\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306f\u300c\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u300d\u306b\u4f3c\u305f\u5f62\u306b\u52a3\u5316\u3057\u307e\u3059\u3002

        \u3053\u306e\u72b6\u6cc1\u3092\u907f\u3051\u308b\u305f\u3081\u3001\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u3067\u30d4\u30dc\u30c3\u30c8\u9078\u629e\u6226\u7565\u3092\u6700\u9069\u5316\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u8981\u7d20\u3092\u30e9\u30f3\u30c0\u30e0\u306b\u9078\u629e\u3057\u3066\u30d4\u30dc\u30c3\u30c8\u3068\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u904b\u304c\u60aa\u304f\u3001\u4e00\u8cab\u3057\u3066\u6700\u9069\u3067\u306a\u3044\u30d4\u30dc\u30c3\u30c8\u3092\u9078\u629e\u3057\u305f\u5834\u5408\u3001\u52b9\u7387\u306f\u307e\u3060\u6e80\u8db3\u3067\u304d\u307e\u305b\u3093\u3002

        \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306f\u901a\u5e38\u300c\u7591\u4f3c\u4e71\u6570\u300d\u3092\u751f\u6210\u3059\u308b\u3053\u3068\u306b\u6ce8\u610f\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002\u7591\u4f3c\u4e71\u6570\u30b7\u30fc\u30b1\u30f3\u30b9\u306b\u5bfe\u3057\u3066\u7279\u5b9a\u306e\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u3092\u69cb\u7bc9\u3059\u308b\u3068\u3001\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306e\u52b9\u7387\u306f\u307e\u3060\u52a3\u5316\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        \u3055\u3089\u306a\u308b\u6539\u5584\u306e\u305f\u3081\u30013\u3064\u306e\u5019\u88dc\u8981\u7d20\uff08\u901a\u5e38\u306f\u914d\u5217\u306e\u6700\u521d\u3001\u6700\u5f8c\u3001\u4e2d\u70b9\u306e\u8981\u7d20\uff09\u3092\u9078\u629e\u3057\u3001**\u3053\u308c\u30893\u3064\u306e\u5019\u88dc\u8981\u7d20\u306e\u4e2d\u592e\u5024\u3092\u30d4\u30dc\u30c3\u30c8\u3068\u3057\u3066\u4f7f\u7528**\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u65b9\u6cd5\u3067\u3001\u30d4\u30dc\u30c3\u30c8\u304c\u300c\u5c0f\u3055\u3059\u304e\u305a\u5927\u304d\u3059\u304e\u306a\u3044\u300d\u78ba\u7387\u304c\u5927\u5e45\u306b\u5897\u52a0\u3057\u307e\u3059\u3002\u3082\u3061\u308d\u3093\u3001\u3055\u3089\u306b\u591a\u304f\u306e\u5019\u88dc\u8981\u7d20\u3092\u9078\u629e\u3057\u3066\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5805\u7262\u6027\u3092\u3055\u3089\u306b\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u65b9\u6cd5\u306b\u3088\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(n^2)\\)\u306b\u52a3\u5316\u3059\u308b\u78ba\u7387\u304c\u5927\u5e45\u306b\u524a\u6e1b\u3055\u308c\u307e\u3059\u3002

        \u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
        def median_three(self, nums: list[int], left: int, mid: int, right: int) -> int:\n    \"\"\"3\u3064\u306e\u5019\u88dc\u8981\u7d20\u306e\u4e2d\u592e\u5024\u3092\u9078\u629e\"\"\"\n    l, m, r = nums[left], nums[mid], nums[right]\n    if (l <= m <= r) or (r <= m <= l):\n        return mid  # m \u306f l \u3068 r \u306e\u9593\n    if (m <= l <= r) or (r <= l <= m):\n        return left  # l \u306f m \u3068 r \u306e\u9593\n    return right\n\ndef partition(self, nums: list[int], left: int, right: int) -> int:\n    \"\"\"\u5206\u5272\uff08\u4e09\u70b9\u4e2d\u592e\u5024\uff09\"\"\"\n    # nums[left] \u3092\u30d4\u30dc\u30c3\u30c8\u3068\u3057\u3066\u4f7f\u7528\n    med = self.median_three(nums, left, (left + right) // 2, right)\n    # \u4e2d\u592e\u5024\u3092\u914d\u5217\u306e\u6700\u5de6\u7aef\u306b\u4ea4\u63db\n    nums[left], nums[med] = nums[med], nums[left]\n    # nums[left] \u3092\u30d4\u30dc\u30c3\u30c8\u3068\u3057\u3066\u4f7f\u7528\n    i, j = left, right\n    while i < j:\n        while i < j and nums[j] >= nums[left]:\n            j -= 1  # \u53f3\u304b\u3089\u5de6\u3078\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5c0f\u3055\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u63a2\u3059\n        while i < j and nums[i] <= nums[left]:\n            i += 1  # \u5de6\u304b\u3089\u53f3\u3078\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5927\u304d\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u63a2\u3059\n        # \u8981\u7d20\u3092\u4ea4\u63db\n        nums[i], nums[j] = nums[j], nums[i]\n    # \u30d4\u30dc\u30c3\u30c8\u30922\u3064\u306e\u30b5\u30d6\u914d\u5217\u306e\u5883\u754c\u306b\u4ea4\u63db\n    nums[i], nums[left] = nums[left], nums[i]\n    return i  # \u30d4\u30dc\u30c3\u30c8\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n
        quick_sort.cpp
        /* \u4e09\u3064\u306e\u5019\u88dc\u8981\u7d20\u306e\u4e2d\u592e\u5024\u3092\u9078\u629e */\nint medianThree(vector<int> &nums, int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m\u306fl\u3068r\u306e\u9593\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l\u306fm\u3068r\u306e\u9593\n    return right;\n}\n\n/* \u5206\u5272\uff08\u4e09\u3064\u306e\u4e2d\u592e\u5024\uff09 */\nint partition(vector<int> &nums, int left, int right) {\n    // \u4e09\u3064\u306e\u5019\u88dc\u8981\u7d20\u306e\u4e2d\u592e\u5024\u3092\u9078\u629e\n    int med = medianThree(nums, left, (left + right) / 2, right);\n    // \u4e2d\u592e\u5024\u3092\u914d\u5217\u306e\u6700\u5de6\u4f4d\u7f6e\u306b\u4ea4\u63db\n    swap(nums, left, med);\n    // nums[left]\u3092\u30d4\u30dc\u30c3\u30c8\u3068\u3057\u3066\u4f7f\u7528\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--; // \u53f3\u304b\u3089\u5de6\u3078\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5c0f\u3055\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u691c\u7d22\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u5de6\u304b\u3089\u53f3\u3078\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5927\u304d\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u691c\u7d22\n        swap(nums, i, j); // \u3053\u308c\u3089\u4e8c\u3064\u306e\u8981\u7d20\u3092\u4ea4\u63db\n    }\n    swap(nums, i, left); // \u30d4\u30dc\u30c3\u30c8\u3092\u4e8c\u3064\u306e\u30b5\u30d6\u914d\u5217\u306e\u5883\u754c\u306b\u4ea4\u63db\n    return i;            // \u30d4\u30dc\u30c3\u30c8\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n}\n
        quick_sort.java
        /* 3\u3064\u306e\u5019\u88dc\u8981\u7d20\u306e\u4e2d\u592e\u5024\u3092\u9078\u629e */\nint medianThree(int[] nums, int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m \u306f l \u3068 r \u306e\u9593\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l \u306f m \u3068 r \u306e\u9593\n    return right;\n}\n\n/* \u5206\u5272\uff083\u3064\u306e\u4e2d\u592e\u5024\uff09 */\nint partition(int[] nums, int left, int right) {\n    // 3\u3064\u306e\u5019\u88dc\u8981\u7d20\u306e\u4e2d\u592e\u5024\u3092\u9078\u629e\n    int med = medianThree(nums, left, (left + right) / 2, right);\n    // \u4e2d\u592e\u5024\u3092\u914d\u5217\u306e\u6700\u5de6\u7aef\u306e\u4f4d\u7f6e\u306b\u4ea4\u63db\n    swap(nums, left, med);\n    // nums[left] \u3092\u57fa\u6e96\u5024\u3068\u3057\u3066\u4f7f\u7528\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // \u53f3\u304b\u3089\u5de6\u3078\u3001\u57fa\u6e96\u5024\u3088\u308a\u5c0f\u3055\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u691c\u7d22\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u5de6\u304b\u3089\u53f3\u3078\u3001\u57fa\u6e96\u5024\u3088\u308a\u5927\u304d\u3044\u6700\u521d\u306e\u8981\u7d20\u3092\u691c\u7d22\n        swap(nums, i, j); // \u3053\u308c\u30892\u3064\u306e\u8981\u7d20\u3092\u4ea4\u63db\n    }\n    swap(nums, i, left);  // \u57fa\u6e96\u5024\u30922\u3064\u306e\u90e8\u5206\u914d\u5217\u306e\u5883\u754c\u306b\u4ea4\u63db\n    return i;             // \u57fa\u6e96\u5024\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8fd4\u3059\n}\n
        quick_sort.cs
        [class]{QuickSortMedian}-[func]{MedianThree}\n\n[class]{QuickSortMedian}-[func]{Partition}\n
        quick_sort.go
        [class]{quickSortMedian}-[func]{medianThree}\n\n[class]{quickSortMedian}-[func]{partition}\n
        quick_sort.swift
        [class]{}-[func]{medianThree}\n\n[class]{}-[func]{partitionMedian}\n
        quick_sort.js
        [class]{QuickSortMedian}-[func]{medianThree}\n\n[class]{QuickSortMedian}-[func]{partition}\n
        quick_sort.ts
        [class]{QuickSortMedian}-[func]{medianThree}\n\n[class]{QuickSortMedian}-[func]{partition}\n
        quick_sort.dart
        [class]{QuickSortMedian}-[func]{_medianThree}\n\n[class]{QuickSortMedian}-[func]{_partition}\n
        quick_sort.rs
        [class]{QuickSortMedian}-[func]{median_three}\n\n[class]{QuickSortMedian}-[func]{partition}\n
        quick_sort.c
        [class]{}-[func]{medianThree}\n\n[class]{}-[func]{partitionMedian}\n
        quick_sort.kt
        [class]{}-[func]{medianThree}\n\n[class]{}-[func]{partitionMedian}\n
        quick_sort.rb
        [class]{QuickSortMedian}-[func]{median_three}\n\n[class]{QuickSortMedian}-[func]{partition}\n
        "},{"location":"chapter_sorting/quick_sort/#1155","title":"11.5.5 \u00a0 \u672b\u5c3e\u518d\u5e30\u6700\u9069\u5316","text":"

        \u7279\u5b9a\u306e\u5165\u529b\u3067\u306f\u3001\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306f\u3088\u308a\u591a\u304f\u306e\u7a7a\u9593\u3092\u5360\u6709\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u5b8c\u5168\u306b\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u5165\u529b\u914d\u5217\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u518d\u5e30\u3067\u306e\u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u3092\\(m\\)\u3068\u3057\u307e\u3059\u3002\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u30d4\u30dc\u30c3\u30c8\u5206\u5272\u3067\u3001\u9577\u3055\\(0\\)\u306e\u5de6\u30b5\u30d6\u914d\u5217\u3068\u9577\u3055\\(m - 1\\)\u306e\u53f3\u30b5\u30d6\u914d\u5217\u304c\u751f\u6210\u3055\u308c\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u518d\u5e30\u547c\u3073\u51fa\u3057\u3054\u3068\u306b\u554f\u984c\u30b5\u30a4\u30ba\u304c1\u3064\u306e\u8981\u7d20\u306e\u307f\u6e1b\u5c11\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u3001\u5404\u30ec\u30d9\u30eb\u306e\u518d\u5e30\u3067\u306e\u524a\u6e1b\u304c\u975e\u5e38\u306b\u5c0f\u3055\u304f\u306a\u308a\u307e\u3059\u3002 \u7d50\u679c\u3068\u3057\u3066\u3001\u518d\u5e30\u30c4\u30ea\u30fc\u306e\u9ad8\u3055\u306f\\(n \u2212 1\\)\u306b\u9054\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u3053\u308c\u306b\u306f\\(O(n)\\)\u306e\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u304c\u5fc5\u8981\u3067\u3059\u3002

        \u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u306e\u84c4\u7a4d\u3092\u9632\u3050\u305f\u3081\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u306e\u30d4\u30dc\u30c3\u30c8\u30bd\u30fc\u30c8\u5f8c\u306b2\u3064\u306e\u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u3092\u6bd4\u8f03\u3057\u3001**\u3088\u308a\u77ed\u3044\u30b5\u30d6\u914d\u5217\u306e\u307f\u3092\u518d\u5e30\u7684\u306b\u30bd\u30fc\u30c8**\u3067\u304d\u307e\u3059\u3002\u3088\u308a\u77ed\u3044\u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u306f\\(n / 2\\)\u3092\u8d85\u3048\u306a\u3044\u305f\u3081\u3001\u3053\u306e\u65b9\u6cd5\u306f\u518d\u5e30\u6df1\u5ea6\u304c\\(\\log n\\)\u3092\u8d85\u3048\u306a\u3044\u3053\u3068\u3092\u4fdd\u8a3c\u3057\u3001\u6700\u60aa\u7a7a\u9593\u8a08\u7b97\u91cf\u3092\\(O(\\log n)\\)\u306b\u6700\u9069\u5316\u3057\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
        def quick_sort(self, nums: list[int], left: int, right: int):\n    \"\"\"\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\uff08\u672b\u5c3e\u518d\u5e30\u6700\u9069\u5316\uff09\"\"\"\n    # \u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c1\u306e\u3068\u304d\u306b\u7d42\u4e86\n    while left < right:\n        # \u5206\u5272\u64cd\u4f5c\n        pivot = self.partition(nums, left, right)\n        # 2\u3064\u306e\u30b5\u30d6\u914d\u5217\u306e\u3046\u3061\u77ed\u3044\u65b9\u306b\u5bfe\u3057\u3066\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u3092\u5b9f\u884c\n        if pivot - left < right - pivot:\n            self.quick_sort(nums, left, pivot - 1)  # \u5de6\u30b5\u30d6\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u30bd\u30fc\u30c8\n            left = pivot + 1  # \u6b8b\u308a\u306e\u672a\u30bd\u30fc\u30c8\u533a\u9593\u306f [pivot + 1, right]\n        else:\n            self.quick_sort(nums, pivot + 1, right)  # \u53f3\u30b5\u30d6\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u30bd\u30fc\u30c8\n            right = pivot - 1  # \u6b8b\u308a\u306e\u672a\u30bd\u30fc\u30c8\u533a\u9593\u306f [left, pivot - 1]\n
        quick_sort.cpp
        /* \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\uff08\u672b\u5c3e\u518d\u5e30\u6700\u9069\u5316\uff09 */\nvoid quickSort(vector<int> &nums, int left, int right) {\n    // \u30b5\u30d6\u914d\u5217\u306e\u9577\u3055\u304c1\u306e\u6642\u7d42\u4e86\n    while (left < right) {\n        // \u5206\u5272\u64cd\u4f5c\n        int pivot = partition(nums, left, right);\n        // \u4e8c\u3064\u306e\u30b5\u30d6\u914d\u5217\u306e\u3046\u3061\u77ed\u3044\u65b9\u3067\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u3092\u5b9f\u884c\n        if (pivot - left < right - pivot) {\n            quickSort(nums, left, pivot - 1); // \u5de6\u30b5\u30d6\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u30bd\u30fc\u30c8\n            left = pivot + 1;                 // \u6b8b\u308a\u306e\u672a\u30bd\u30fc\u30c8\u533a\u9593\u306f[pivot + 1, right]\n        } else {\n            quickSort(nums, pivot + 1, right); // \u53f3\u30b5\u30d6\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u30bd\u30fc\u30c8\n            right = pivot - 1;                 // \u6b8b\u308a\u306e\u672a\u30bd\u30fc\u30c8\u533a\u9593\u306f[left, pivot - 1]\n        }\n    }\n}\n
        quick_sort.java
        /* \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\uff08\u672b\u5c3e\u518d\u5e30\u6700\u9069\u5316\uff09 */\nvoid quickSort(int[] nums, int left, int right) {\n    // \u90e8\u5206\u914d\u5217\u306e\u9577\u3055\u304c 1 \u306e\u3068\u304d\u7d42\u4e86\n    while (left < right) {\n        // \u5206\u5272\u64cd\u4f5c\n        int pivot = partition(nums, left, right);\n        // 2\u3064\u306e\u90e8\u5206\u914d\u5217\u306e\u3046\u3061\u77ed\u3044\u65b9\u306b\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u3092\u5b9f\u884c\n        if (pivot - left < right - pivot) {\n            quickSort(nums, left, pivot - 1); // \u5de6\u90e8\u5206\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u30bd\u30fc\u30c8\n            left = pivot + 1; // \u6b8b\u308a\u306e\u672a\u30bd\u30fc\u30c8\u533a\u9593\u306f [pivot + 1, right]\n        } else {\n            quickSort(nums, pivot + 1, right); // \u53f3\u90e8\u5206\u914d\u5217\u3092\u518d\u5e30\u7684\u306b\u30bd\u30fc\u30c8\n            right = pivot - 1; // \u6b8b\u308a\u306e\u672a\u30bd\u30fc\u30c8\u533a\u9593\u306f [left, pivot - 1]\n        }\n    }\n}\n
        quick_sort.cs
        [class]{QuickSortTailCall}-[func]{QuickSort}\n
        quick_sort.go
        [class]{quickSortTailCall}-[func]{quickSort}\n
        quick_sort.swift
        [class]{}-[func]{quickSortTailCall}\n
        quick_sort.js
        [class]{QuickSortTailCall}-[func]{quickSort}\n
        quick_sort.ts
        [class]{QuickSortTailCall}-[func]{quickSort}\n
        quick_sort.dart
        [class]{QuickSortTailCall}-[func]{quickSort}\n
        quick_sort.rs
        [class]{QuickSortTailCall}-[func]{quick_sort}\n
        quick_sort.c
        [class]{}-[func]{quickSortTailCall}\n
        quick_sort.kt
        [class]{}-[func]{quickSortTailCall}\n
        quick_sort.rb
        [class]{QuickSortTailCall}-[func]{quick_sort}\n
        "},{"location":"chapter_sorting/radix_sort/","title":"11.10 \u00a0 \u57fa\u6570\u30bd\u30fc\u30c8","text":"

        \u524d\u306e\u7bc0\u3067\u306f\u8a08\u6570\u30bd\u30fc\u30c8\u3092\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002\u3053\u308c\u306f\u3001\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba \\(n\\) \u304c\u5927\u304d\u3044\u304c\u30c7\u30fc\u30bf\u7bc4\u56f2 \\(m\\) \u304c\u5c0f\u3055\u3044\u30b7\u30ca\u30ea\u30aa\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\\(n = 10^6\\) \u306e\u5b66\u751fID\u3092\u30bd\u30fc\u30c8\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u5404ID\u304c \\(8\\) \u6841\u306e\u6570\u5b57\u3067\u3042\u308b\u3068\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u30c7\u30fc\u30bf\u7bc4\u56f2 \\(m = 10^8\\) \u304c\u975e\u5e38\u306b\u5927\u304d\u3044\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u3053\u306e\u5834\u5408\u3001\u8a08\u6570\u30bd\u30fc\u30c8\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u5927\u91cf\u306e\u30e1\u30e2\u30ea\u30b9\u30da\u30fc\u30b9\u304c\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059\u3002\u57fa\u6570\u30bd\u30fc\u30c8\u306f\u3053\u306e\u72b6\u6cc1\u3092\u56de\u907f\u3067\u304d\u307e\u3059\u3002

        \u57fa\u6570\u30bd\u30fc\u30c8\u306f\u8a08\u6570\u30bd\u30fc\u30c8\u3068\u540c\u3058\u6838\u5fc3\u6982\u5ff5\u3092\u5171\u6709\u3057\u3001\u8981\u7d20\u306e\u983b\u5ea6\u3092\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u3053\u3068\u3067\u30bd\u30fc\u30c8\u3057\u307e\u3059\u3002\u540c\u6642\u306b\u3001\u57fa\u6570\u30bd\u30fc\u30c8\u306f\u6570\u5b57\u306e\u6841\u9593\u306e\u6f38\u9032\u7684\u95a2\u4fc2\u3092\u5229\u7528\u3057\u3066\u3053\u308c\u3092\u57fa\u76e4\u3068\u3057\u3066\u3044\u307e\u3059\u3002\u6841\u3092\u4e00\u5ea6\u306b\u4e00\u3064\u305a\u3064\u51e6\u7406\u3057\u3066\u30bd\u30fc\u30c8\u3057\u3001\u6700\u7d42\u7684\u306a\u30bd\u30fc\u30c8\u9806\u5e8f\u3092\u9054\u6210\u3057\u307e\u3059\u3002

        "},{"location":"chapter_sorting/radix_sort/#11101","title":"11.10.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u904e\u7a0b","text":"

        \u5b66\u751fID\u30c7\u30fc\u30bf\u3092\u4f8b\u3068\u3057\u3066\u3001\u6700\u4e0b\u4f4d\u6841\u3092 \\(1\\) \u756a\u76ee\u3001\u6700\u4e0a\u4f4d\u6841\u3092 \\(8\\) \u756a\u76ee\u3068\u3059\u308b\u3068\u3001\u57fa\u6570\u30bd\u30fc\u30c8\u306e\u904e\u7a0b\u306f\u4ee5\u4e0b\u306e\u56f3\u306b\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        1. \u6841 \\(k = 1\\) \u3092\u521d\u671f\u5316\u3057\u307e\u3059\u3002
        2. \u5b66\u751fID\u306e \\(k\\) \u756a\u76ee\u306e\u6841\u306b\u5bfe\u3057\u3066\u300c\u8a08\u6570\u30bd\u30fc\u30c8\u300d\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002\u5b8c\u4e86\u5f8c\u3001\u30c7\u30fc\u30bf\u306f \\(k\\) \u756a\u76ee\u306e\u6841\u306b\u57fa\u3065\u3044\u3066\u6700\u5c0f\u304b\u3089\u6700\u5927\u307e\u3067\u30bd\u30fc\u30c8\u3055\u308c\u307e\u3059\u3002
        3. \\(k\\) \u3092 \\(1\\) \u5897\u3084\u3057\u3001\u624b\u9806 2. \u306b\u623b\u3063\u3066\u53cd\u5fa9\u3092\u7d9a\u3051\u3001\u3059\u3079\u3066\u306e\u6841\u304c\u30bd\u30fc\u30c8\u3055\u308c\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002\u3053\u306e\u6642\u70b9\u3067\u904e\u7a0b\u304c\u7d42\u4e86\u3057\u307e\u3059\u3002

        \u56f3 11-18 \u00a0 \u57fa\u6570\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u904e\u7a0b

        \u4ee5\u4e0b\u3001\u30b3\u30fc\u30c9\u5b9f\u88c5\u3092\u8a73\u3057\u304f\u898b\u3066\u307f\u307e\u3059\u3002\u57fa\u6570 \\(d\\) \u3067\u306e\u6570 \\(x\\) \u306b\u5bfe\u3057\u3066\u3001\u305d\u306e \\(k\\) \u756a\u76ee\u306e\u6841 \\(x_k\\) \u3092\u53d6\u5f97\u3059\u308b\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u8a08\u7b97\u5f0f\u3092\u4f7f\u7528\u3067\u304d\u307e\u3059\uff1a

        \\[ x_k = \\lfloor\\frac{x}{d^{k-1}}\\rfloor \\bmod d \\]

        \u3053\u3053\u3067 \\(\\lfloor a \\rfloor\\) \u306f\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570 \\(a\\) \u306e\u5207\u308a\u6368\u3066\u3092\u8868\u3057\u3001\\(\\bmod \\: d\\) \u306f \\(d\\) \u306b\u3088\u308b\u5270\u4f59\u3092\u8868\u3057\u307e\u3059\u3002\u5b66\u751fID\u30c7\u30fc\u30bf\u306e\u5834\u5408\u3001\\(d = 10\\) \u3067 \\(k \\in [1, 8]\\) \u3067\u3059\u3002

        \u3055\u3089\u306b\u3001\\(k\\) \u756a\u76ee\u306e\u6841\u306b\u57fa\u3065\u3044\u3066\u30bd\u30fc\u30c8\u3067\u304d\u308b\u3088\u3046\u306b\u3001\u8a08\u6570\u30bd\u30fc\u30c8\u306e\u30b3\u30fc\u30c9\u3092\u5c11\u3057\u4fee\u6b63\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby radix_sort.py
        def digit(num: int, exp: int) -> int:\n    \"\"\"\u8981\u7d20 num \u306e k \u756a\u76ee\u306e\u6841\u3092\u53d6\u5f97\u3001exp = 10^(k-1)\"\"\"\n    # k \u306e\u4ee3\u308f\u308a\u306b exp \u3092\u6e21\u3059\u3053\u3068\u3067\u3001\u3053\u3053\u3067\u30b3\u30b9\u30c8\u306e\u9ad8\u3044\u7d2f\u4e57\u8a08\u7b97\u3092\u907f\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u308b\n    return (num // exp) % 10\n\ndef counting_sort_digit(nums: list[int], exp: int):\n    \"\"\"\u8a08\u6570\u30bd\u30fc\u30c8\uff08nums \u306e k \u756a\u76ee\u306e\u6841\u306b\u57fa\u3065\u304f\uff09\"\"\"\n    # 10\u9032\u6570\u306e\u6841\u306e\u7bc4\u56f2\u306f 0~9\u3001\u3057\u305f\u304c\u3063\u3066\u9577\u305510\u306e\u30d0\u30b1\u30c3\u30c8\u914d\u5217\u304c\u5fc5\u8981\n    counter = [0] * 10\n    n = len(nums)\n    # \u6570\u5b57 0~9 \u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    for i in range(n):\n        d = digit(nums[i], exp)  # nums[i] \u306e k \u756a\u76ee\u306e\u6841\u3092\u53d6\u5f97\u3001d \u3068\u3059\u308b\n        counter[d] += 1  # \u6570\u5b57 d \u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    # \u524d\u7f6e\u548c\u3092\u8a08\u7b97\u3057\u3001\u300c\u51fa\u73fe\u56de\u6570\u300d\u3092\u300c\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u306b\u5909\u63db\n    for i in range(1, 10):\n        counter[i] += counter[i - 1]\n    # \u9006\u9806\u306b\u8d70\u67fb\u3057\u3001\u30d0\u30b1\u30c3\u30c8\u7d71\u8a08\u306b\u57fa\u3065\u3044\u3066\u5404\u8981\u7d20\u3092 res \u306b\u914d\u7f6e\n    res = [0] * n\n    for i in range(n - 1, -1, -1):\n        d = digit(nums[i], exp)\n        j = counter[d] - 1  # \u914d\u5217\u5185\u306e d \u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 j \u3092\u53d6\u5f97\n        res[j] = nums[i]  # \u73fe\u5728\u306e\u8981\u7d20\u3092\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 j \u306b\u914d\u7f6e\n        counter[d] -= 1  # d \u306e\u6570\u30921\u6e1b\u3089\u3059\n    # \u7d50\u679c\u3092\u4f7f\u7528\u3057\u3066\u5143\u306e\u914d\u5217 nums \u3092\u4e0a\u66f8\u304d\n    for i in range(n):\n        nums[i] = res[i]\n\ndef radix_sort(nums: list[int]):\n    \"\"\"\u57fa\u6570\u30bd\u30fc\u30c8\"\"\"\n    # \u914d\u5217\u306e\u6700\u5927\u8981\u7d20\u3092\u53d6\u5f97\u3057\u3001\u6700\u5927\u6841\u6570\u3092\u5224\u5b9a\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    m = max(nums)\n    # \u6700\u4e0b\u4f4d\u6841\u304b\u3089\u6700\u4e0a\u4f4d\u6841\u307e\u3067\u8d70\u67fb\n    exp = 1\n    while exp <= m:\n        # \u914d\u5217\u8981\u7d20\u306e k \u756a\u76ee\u306e\u6841\u306b\u5bfe\u3057\u3066\u8a08\u6570\u30bd\u30fc\u30c8\u3092\u5b9f\u884c\n        # k = 1 -> exp = 1\n        # k = 2 -> exp = 10\n        # \u3064\u307e\u308a\u3001exp = 10^(k-1)\n        counting_sort_digit(nums, exp)\n        exp *= 10\n
        radix_sort.cpp
        /* \u8981\u7d20num\u306ek\u756a\u76ee\u306e\u6841\u3092\u53d6\u5f97\u3001exp = 10^(k-1) */\nint digit(int num, int exp) {\n    // k\u306e\u4ee3\u308f\u308a\u306bexp\u3092\u6e21\u3059\u3053\u3068\u3067\u3001\u3053\u3053\u3067\u7e70\u308a\u8fd4\u3055\u308c\u308b\u9ad8\u4fa1\u306a\u51aa\u4e57\u8a08\u7b97\u3092\u907f\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u308b\n    return (num / exp) % 10;\n}\n\n/* \u30ab\u30a6\u30f3\u30c8\u30bd\u30fc\u30c8\uff08nums\u306ek\u756a\u76ee\u306e\u6841\u306b\u57fa\u3065\u304f\uff09 */\nvoid countingSortDigit(vector<int> &nums, int exp) {\n    // 10\u9032\u6570\u306e\u6841\u7bc4\u56f2\u306f0~9\u306a\u306e\u3067\u3001\u9577\u305510\u306e\u30d0\u30b1\u30c3\u30c8\u914d\u5217\u304c\u5fc5\u8981\n    vector<int> counter(10, 0);\n    int n = nums.size();\n    // \u6570\u5b570~9\u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    for (int i = 0; i < n; i++) {\n        int d = digit(nums[i], exp); // nums[i]\u306ek\u756a\u76ee\u306e\u6841\u3092\u53d6\u5f97\u3001d\u3068\u3057\u3066\u8a18\u9332\n        counter[d]++;                // \u6570\u5b57d\u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    }\n    // \u524d\u7f00\u548c\u3092\u8a08\u7b97\u3057\u3001\u300c\u51fa\u73fe\u56de\u6570\u300d\u3092\u300c\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u306b\u5909\u63db\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // \u9006\u9806\u3067\u8d70\u67fb\u3057\u3001\u30d0\u30b1\u30c3\u30c8\u7d71\u8a08\u306b\u57fa\u3065\u3044\u3066\u5404\u8981\u7d20\u3092res\u306b\u914d\u7f6e\n    vector<int> res(n, 0);\n    for (int i = n - 1; i >= 0; i--) {\n        int d = digit(nums[i], exp);\n        int j = counter[d] - 1; // d\u304c\u914d\u5217\u5185\u306b\u3042\u308b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9j\u3092\u53d6\u5f97\n        res[j] = nums[i];       // \u73fe\u5728\u306e\u8981\u7d20\u3092\u30a4\u30f3\u30c7\u30c3\u30af\u30b9j\u306b\u914d\u7f6e\n        counter[d]--;           // d\u306e\u30ab\u30a6\u30f3\u30c8\u30921\u6e1b\u3089\u3059\n    }\n    // \u7d50\u679c\u3067\u5143\u306e\u914d\u5217nums\u3092\u4e0a\u66f8\u304d\n    for (int i = 0; i < n; i++)\n        nums[i] = res[i];\n}\n\n/* \u57fa\u6570\u30bd\u30fc\u30c8 */\nvoid radixSort(vector<int> &nums) {\n    // \u914d\u5217\u306e\u6700\u5927\u8981\u7d20\u3092\u53d6\u5f97\u3001\u6700\u5927\u6841\u6570\u3092\u5224\u5b9a\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    int m = *max_element(nums.begin(), nums.end());\n    // \u6700\u4e0b\u4f4d\u6841\u304b\u3089\u6700\u4e0a\u4f4d\u6841\u307e\u3067\u8d70\u67fb\n    for (int exp = 1; exp <= m; exp *= 10)\n        // \u914d\u5217\u8981\u7d20\u306ek\u756a\u76ee\u306e\u6841\u3067\u30ab\u30a6\u30f3\u30c8\u30bd\u30fc\u30c8\u3092\u5b9f\u884c\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u3064\u307e\u308a\u3001exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n}\n
        radix_sort.java
        /* \u8981\u7d20 num \u306e k \u756a\u76ee\u306e\u6841\u3092\u53d6\u5f97\u3001exp = 10^(k-1) */\nint digit(int num, int exp) {\n    // k \u306e\u4ee3\u308f\u308a\u306b exp \u3092\u6e21\u3059\u3053\u3068\u3067\u3001\u3053\u3053\u3067\u30b3\u30b9\u30c8\u306e\u9ad8\u3044\u7d2f\u4e57\u8a08\u7b97\u306e\u7e70\u308a\u8fd4\u3057\u3092\u907f\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u308b\n    return (num / exp) % 10;\n}\n\n/* \u8a08\u6570\u30bd\u30fc\u30c8\uff08nums \u306e k \u756a\u76ee\u306e\u6841\u306b\u57fa\u3065\u304f\uff09 */\nvoid countingSortDigit(int[] nums, int exp) {\n    // 10\u9032\u6570\u306e\u6841\u306e\u7bc4\u56f2\u306f 0~9\u3001\u3057\u305f\u304c\u3063\u3066\u9577\u3055 10 \u306e\u30d0\u30b1\u30c3\u30c8\u914d\u5217\u304c\u5fc5\u8981\n    int[] counter = new int[10];\n    int n = nums.length;\n    // \u6841 0~9 \u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    for (int i = 0; i < n; i++) {\n        int d = digit(nums[i], exp); // nums[i] \u306e k \u756a\u76ee\u306e\u6841\u3092\u53d6\u5f97\u3001d \u3068\u3059\u308b\n        counter[d]++;                // \u6841 d \u306e\u51fa\u73fe\u56de\u6570\u3092\u7d71\u8a08\n    }\n    // \u7d2f\u7a4d\u548c\u3092\u8a08\u7b97\u3057\u3001\u300c\u51fa\u73fe\u56de\u6570\u300d\u3092\u300c\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u300d\u306b\u5909\u63db\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // \u9006\u9806\u306b\u8d70\u67fb\u3057\u3001\u30d0\u30b1\u30c3\u30c8\u7d71\u8a08\u306b\u57fa\u3065\u3044\u3066\u5404\u8981\u7d20\u3092 res \u306b\u914d\u7f6e\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int d = digit(nums[i], exp);\n        int j = counter[d] - 1; // \u914d\u5217\u5185\u3067\u306e d \u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 j \u3092\u53d6\u5f97\n        res[j] = nums[i];       // \u73fe\u5728\u306e\u8981\u7d20\u3092\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 j \u306b\u914d\u7f6e\n        counter[d]--;           // d \u306e\u30ab\u30a6\u30f3\u30c8\u3092 1 \u6e1b\u3089\u3059\n    }\n    // \u7d50\u679c\u3067\u5143\u306e\u914d\u5217 nums \u3092\u4e0a\u66f8\u304d\n    for (int i = 0; i < n; i++)\n        nums[i] = res[i];\n}\n\n/* \u57fa\u6570\u30bd\u30fc\u30c8 */\nvoid radixSort(int[] nums) {\n    // \u914d\u5217\u306e\u6700\u5927\u8981\u7d20\u3092\u53d6\u5f97\u3057\u3001\u6700\u5927\u6841\u6570\u3092\u5224\u5b9a\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\n    int m = Integer.MIN_VALUE;\n    for (int num : nums)\n        if (num > m)\n            m = num;\n    // \u6700\u4e0b\u4f4d\u6841\u304b\u3089\u6700\u4e0a\u4f4d\u6841\u307e\u3067\u8d70\u67fb\n    for (int exp = 1; exp <= m; exp *= 10) {\n        // \u914d\u5217\u8981\u7d20\u306e k \u756a\u76ee\u306e\u6841\u306b\u5bfe\u3057\u3066\u8a08\u6570\u30bd\u30fc\u30c8\u3092\u5b9f\u884c\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u3059\u306a\u308f\u3061 exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n    }\n}\n
        radix_sort.cs
        [class]{radix_sort}-[func]{Digit}\n\n[class]{radix_sort}-[func]{CountingSortDigit}\n\n[class]{radix_sort}-[func]{RadixSort}\n
        radix_sort.go
        [class]{}-[func]{digit}\n\n[class]{}-[func]{countingSortDigit}\n\n[class]{}-[func]{radixSort}\n
        radix_sort.swift
        [class]{}-[func]{digit}\n\n[class]{}-[func]{countingSortDigit}\n\n[class]{}-[func]{radixSort}\n
        radix_sort.js
        [class]{}-[func]{digit}\n\n[class]{}-[func]{countingSortDigit}\n\n[class]{}-[func]{radixSort}\n
        radix_sort.ts
        [class]{}-[func]{digit}\n\n[class]{}-[func]{countingSortDigit}\n\n[class]{}-[func]{radixSort}\n
        radix_sort.dart
        [class]{}-[func]{digit}\n\n[class]{}-[func]{countingSortDigit}\n\n[class]{}-[func]{radixSort}\n
        radix_sort.rs
        [class]{}-[func]{digit}\n\n[class]{}-[func]{counting_sort_digit}\n\n[class]{}-[func]{radix_sort}\n
        radix_sort.c
        [class]{}-[func]{digit}\n\n[class]{}-[func]{countingSortDigit}\n\n[class]{}-[func]{radixSort}\n
        radix_sort.kt
        [class]{}-[func]{digit}\n\n[class]{}-[func]{countingSortDigit}\n\n[class]{}-[func]{radixSort}\n
        radix_sort.rb
        [class]{}-[func]{digit}\n\n[class]{}-[func]{counting_sort_digit}\n\n[class]{}-[func]{radix_sort}\n

        \u306a\u305c\u6700\u4e0b\u4f4d\u6841\u304b\u3089\u958b\u59cb\u3059\u308b\u306e\u304b\uff1f

        \u9023\u7d9a\u3059\u308b\u30bd\u30fc\u30c8\u30e9\u30a6\u30f3\u30c9\u3067\u306f\u3001\u5f8c\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u7d50\u679c\u304c\u524d\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u7d50\u679c\u3092\u4e0a\u66f8\u304d\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u6700\u521d\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u7d50\u679c\u304c \\(a < b\\) \u3067\u30012\u756a\u76ee\u306e\u30e9\u30a6\u30f3\u30c9\u304c \\(a > b\\) \u306e\u5834\u5408\u30012\u756a\u76ee\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u7d50\u679c\u304c\u6700\u521d\u306e\u30e9\u30a6\u30f3\u30c9\u306e\u7d50\u679c\u3092\u7f6e\u304d\u63db\u3048\u307e\u3059\u3002\u4e0a\u4f4d\u6841\u306f\u4e0b\u4f4d\u6841\u3088\u308a\u512a\u5148\u3055\u308c\u308b\u305f\u3081\u3001\u4e0a\u4f4d\u6841\u306e\u524d\u306b\u4e0b\u4f4d\u6841\u3092\u30bd\u30fc\u30c8\u3059\u308b\u3053\u3068\u304c\u7406\u306b\u304b\u306a\u3063\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_sorting/radix_sort/#11102","title":"11.10.2 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7279\u5fb4","text":"

        \u8a08\u6570\u30bd\u30fc\u30c8\u3068\u6bd4\u8f03\u3057\u3066\u3001\u57fa\u6570\u30bd\u30fc\u30c8\u306f\u3088\u308a\u5927\u304d\u306a\u6570\u5024\u7bc4\u56f2\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u30c7\u30fc\u30bf\u304c\u56fa\u5b9a\u6841\u6570\u3067\u8868\u73fe\u3067\u304d\u3001\u6841\u6570\u304c\u3042\u307e\u308a\u5927\u304d\u304f\u306a\u3044\u3053\u3068\u3092\u524d\u63d0\u3068\u3057\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u306f\u6841\u6570 \\(k\\) \u304c\u5927\u304d\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf \\(O(nk) \\gg O(n^2)\\) \u306b\u3064\u306a\u304c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3001\u57fa\u6570\u30bd\u30fc\u30c8\u306b\u306f\u9069\u3057\u3066\u3044\u307e\u305b\u3093\u3002

        • \u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(nk)\\)\u3001\u975e\u9069\u5fdc\u30bd\u30fc\u30c8\uff1a\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\u3092 \\(n\\)\u3001\u30c7\u30fc\u30bf\u304c\u57fa\u6570 \\(d\\)\u3001\u6700\u5927\u6841\u6570\u3092 \\(k\\) \u3068\u3059\u308b\u3068\u3001\u5358\u4e00\u6841\u306e\u30bd\u30fc\u30c8\u306b\u306f \\(O(n + d)\\) \u6642\u9593\u304c\u304b\u304b\u308a\u3001\u3059\u3079\u3066\u306e \\(k\\) \u6841\u306e\u30bd\u30fc\u30c8\u306b\u306f \\(O((n + d)k)\\) \u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002\u4e00\u822c\u7684\u306b\u3001\\(d\\) \u3068 \\(k\\) \u306f\u3069\u3061\u3089\u3082\u6bd4\u8f03\u7684\u5c0f\u3055\u304f\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u306b\u8fd1\u3065\u304d\u307e\u3059\u3002
        • \u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(n + d)\\)\u3001\u975e\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u30bd\u30fc\u30c8\uff1a\u8a08\u6570\u30bd\u30fc\u30c8\u3068\u540c\u69d8\u306b\u3001\u57fa\u6570\u30bd\u30fc\u30c8\u306f\u9577\u3055 \\(n\\) \u3068 \\(d\\) \u306e\u914d\u5217 res \u3068 counter \u306b\u305d\u308c\u305e\u308c\u4f9d\u5b58\u3057\u307e\u3059\u3002
        • \u5b89\u5b9a\u30bd\u30fc\u30c8\uff1a\u8a08\u6570\u30bd\u30fc\u30c8\u304c\u5b89\u5b9a\u306a\u5834\u5408\u3001\u57fa\u6570\u30bd\u30fc\u30c8\u3082\u5b89\u5b9a\u3067\u3059\u3002\u8a08\u6570\u30bd\u30fc\u30c8\u304c\u4e0d\u5b89\u5b9a\u306a\u5834\u5408\u3001\u57fa\u6570\u30bd\u30fc\u30c8\u306f\u6b63\u3057\u3044\u30bd\u30fc\u30c8\u9806\u5e8f\u3092\u4fdd\u8a3c\u3067\u304d\u307e\u305b\u3093\u3002
        "},{"location":"chapter_sorting/selection_sort/","title":"11.2 \u00a0 \u9078\u629e\u30bd\u30fc\u30c8","text":"

        \u9078\u629e\u30bd\u30fc\u30c8\u306f\u975e\u5e38\u306b\u30b7\u30f3\u30d7\u30eb\u306a\u539f\u7406\u3067\u52d5\u4f5c\u3057\u307e\u3059\uff1a\u5404\u53cd\u5fa9\u3067\u672a\u30bd\u30fc\u30c8\u533a\u9593\u304b\u3089\u6700\u5c0f\u8981\u7d20\u3092\u9078\u629e\u3057\u3001\u30bd\u30fc\u30c8\u6e08\u307f\u30bb\u30af\u30b7\u30e7\u30f3\u306e\u672b\u5c3e\u306b\u79fb\u52d5\u3059\u308b\u30eb\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        \u914d\u5217\u306e\u9577\u3055\u3092\\(n\\)\u3068\u3059\u308b\u3068\u3001\u9078\u629e\u30bd\u30fc\u30c8\u306e\u30b9\u30c6\u30c3\u30d7\u306f\u4e0b\u56f3\u306b\u793a\u3055\u308c\u307e\u3059\u3002

        1. \u6700\u521d\u306b\u3001\u3059\u3079\u3066\u306e\u8981\u7d20\u306f\u672a\u30bd\u30fc\u30c8\u3067\u3001\u3064\u307e\u308a\u672a\u30bd\u30fc\u30c8\uff08\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\uff09\u533a\u9593\u306f\\([0, n-1]\\)\u3067\u3059\u3002
        2. \u533a\u9593\\([0, n-1]\\)\u306e\u6700\u5c0f\u8981\u7d20\u3092\u9078\u629e\u3057\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(0\\)\u306e\u8981\u7d20\u3068\u4ea4\u63db\u3057\u307e\u3059\u3002\u3053\u306e\u5f8c\u3001\u914d\u5217\u306e\u6700\u521d\u306e\u8981\u7d20\u304c\u30bd\u30fc\u30c8\u3055\u308c\u307e\u3059\u3002
        3. \u533a\u9593\\([1, n-1]\\)\u306e\u6700\u5c0f\u8981\u7d20\u3092\u9078\u629e\u3057\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\\(1\\)\u306e\u8981\u7d20\u3068\u4ea4\u63db\u3057\u307e\u3059\u3002\u3053\u306e\u5f8c\u3001\u914d\u5217\u306e\u6700\u521d\u306e2\u3064\u306e\u8981\u7d20\u304c\u30bd\u30fc\u30c8\u3055\u308c\u307e\u3059\u3002
        4. \u3053\u306e\u65b9\u6cd5\u3067\u7d9a\u884c\u3057\u307e\u3059\u3002\\(n - 1\\)\u30e9\u30a6\u30f3\u30c9\u306e\u9078\u629e\u3068\u4ea4\u63db\u306e\u5f8c\u3001\u6700\u521d\u306e\\(n - 1\\)\u500b\u306e\u8981\u7d20\u304c\u30bd\u30fc\u30c8\u3055\u308c\u307e\u3059\u3002
        5. \u6b8b\u308a\u306e\u552f\u4e00\u306e\u8981\u7d20\u306f\u7d50\u679c\u7684\u306b\u6700\u5927\u8981\u7d20\u3067\u3042\u308a\u3001\u30bd\u30fc\u30c8\u3059\u308b\u5fc5\u8981\u304c\u306a\u3044\u305f\u3081\u3001\u914d\u5217\u306f\u30bd\u30fc\u30c8\u3055\u308c\u307e\u3059\u3002
        <1><2><3><4><5><6><7><8><9><10><11>

        \u56f3 11-2 \u00a0 Selection sort process

        \u30b3\u30fc\u30c9\u3067\u306f\u3001\\(k\\)\u3092\u4f7f\u7528\u3057\u3066\u672a\u30bd\u30fc\u30c8\u533a\u9593\u5185\u306e\u6700\u5c0f\u8981\u7d20\u3092\u8a18\u9332\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby selection_sort.py
        def selection_sort(nums: list[int]):\n    \"\"\"\u9078\u629e\u30bd\u30fc\u30c8\"\"\"\n    n = len(nums)\n    # \u5916\u5074\u306e\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f [i, n-1]\n    for i in range(n - 1):\n        # \u5185\u5074\u306e\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u5185\u3067\u6700\u5c0f\u8981\u7d20\u3092\u898b\u3064\u3051\u308b\n        k = i\n        for j in range(i + 1, n):\n            if nums[j] < nums[k]:\n                k = j  # \u6700\u5c0f\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a18\u9332\n        # \u6700\u5c0f\u8981\u7d20\u3092\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306e\u5148\u982d\u8981\u7d20\u3068\u4ea4\u63db\n        nums[i], nums[k] = nums[k], nums[i]\n
        selection_sort.cpp
        /* \u9078\u629e\u30bd\u30fc\u30c8 */\nvoid selectionSort(vector<int> &nums) {\n    int n = nums.size();\n    // \u5916\u5074\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f[i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // \u5185\u5074\u30eb\u30fc\u30d7\uff1a\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u5185\u3067\u6700\u5c0f\u8981\u7d20\u3092\u898b\u3064\u3051\u308b\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // \u6700\u5c0f\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a18\u9332\n        }\n        // \u6700\u5c0f\u8981\u7d20\u3092\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306e\u6700\u521d\u306e\u8981\u7d20\u3068\u4ea4\u63db\n        swap(nums[i], nums[k]);\n    }\n}\n
        selection_sort.java
        /* \u9078\u629e\u30bd\u30fc\u30c8 */\nvoid selectionSort(int[] nums) {\n    int n = nums.length;\n    // \u5916\u5074\u30eb\u30fc\u30d7: \u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306f [i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // \u5185\u5074\u30eb\u30fc\u30d7: \u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u5185\u3067\u6700\u5c0f\u8981\u7d20\u3092\u898b\u3064\u3051\u308b\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // \u6700\u5c0f\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a18\u9332\n        }\n        // \u6700\u5c0f\u8981\u7d20\u3068\u672a\u30bd\u30fc\u30c8\u7bc4\u56f2\u306e\u6700\u521d\u306e\u8981\u7d20\u3092\u4ea4\u63db\n        int temp = nums[i];\n        nums[i] = nums[k];\n        nums[k] = temp;\n    }\n}\n
        selection_sort.cs
        [class]{selection_sort}-[func]{SelectionSort}\n
        selection_sort.go
        [class]{}-[func]{selectionSort}\n
        selection_sort.swift
        [class]{}-[func]{selectionSort}\n
        selection_sort.js
        [class]{}-[func]{selectionSort}\n
        selection_sort.ts
        [class]{}-[func]{selectionSort}\n
        selection_sort.dart
        [class]{}-[func]{selectionSort}\n
        selection_sort.rs
        [class]{}-[func]{selection_sort}\n
        selection_sort.c
        [class]{}-[func]{selectionSort}\n
        selection_sort.kt
        [class]{}-[func]{selectionSort}\n
        selection_sort.rb
        [class]{}-[func]{selection_sort}\n
        "},{"location":"chapter_sorting/selection_sort/#1121","title":"11.2.1 \u00a0 \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u7279\u6027","text":"
        • \\(O(n^2)\\)\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3001\u975e\u9069\u5fdc\u30bd\u30fc\u30c8\uff1a\u5916\u5074\u30eb\u30fc\u30d7\u306b\\(n - 1\\)\u56de\u306e\u53cd\u5fa9\u304c\u3042\u308a\u3001\u672a\u30bd\u30fc\u30c8\u30bb\u30af\u30b7\u30e7\u30f3\u306e\u9577\u3055\u306f\u6700\u521d\u306e\u53cd\u5fa9\u3067\\(n\\)\u304b\u3089\u59cb\u307e\u308a\u3001\u6700\u5f8c\u306e\u53cd\u5fa9\u3067\\(2\\)\u307e\u3067\u6e1b\u5c11\u3057\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u5404\u5916\u5074\u30eb\u30fc\u30d7\u53cd\u5fa9\u306b\u306f\u305d\u308c\u305e\u308c\\(n\\)\u3001\\(n - 1\\)\u3001\\(\\dots\\)\u3001\\(3\\)\u3001\\(2\\)\u56de\u306e\u5185\u5074\u30eb\u30fc\u30d7\u53cd\u5fa9\u304c\u542b\u307e\u308c\u3001\u5408\u8a08\u306f\\(\\frac{(n - 1)(n + 2)}{2}\\)\u3068\u306a\u308a\u307e\u3059\u3002
        • \\(O(1)\\)\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u3001\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u30bd\u30fc\u30c8\uff1a\u30dd\u30a4\u30f3\u30bf\\(i\\)\u3068\\(j\\)\u3067\u5b9a\u6570\u306e\u8ffd\u52a0\u7a7a\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u975e\u5b89\u5b9a\u30bd\u30fc\u30c8\uff1a\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u8981\u7d20nums[i]\u306f\u7b49\u3057\u3044\u8981\u7d20\u306e\u53f3\u5074\u306b\u4ea4\u63db\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u76f8\u5bfe\u9806\u5e8f\u304c\u5909\u308f\u308b\u539f\u56e0\u3068\u306a\u308a\u307e\u3059\u3002

        \u56f3 11-3 \u00a0 Selection sort instability example

        "},{"location":"chapter_sorting/sorting_algorithm/","title":"11.1 \u00a0 \u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0","text":"

        \u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3092\u7279\u5b9a\u306e\u9806\u5e8f\u3067\u914d\u5217\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u30c7\u30fc\u30bf\u306f\u901a\u5e38\u3001\u3088\u308a\u52b9\u7387\u7684\u306b\u63a2\u7d22\u3001\u5206\u6790\u3001\u51e6\u7406\u3067\u304d\u308b\u305f\u3081\u3001\u5e45\u5e83\u3044\u5fdc\u7528\u304c\u3042\u308a\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u30c7\u30fc\u30bf\u578b\u306f\u6574\u6570\u3001\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u3001\u6587\u5b57\u3001\u6587\u5b57\u5217\u306a\u3069\u3067\u3059\u3002\u30bd\u30fc\u30c8\u57fa\u6e96\u306f\u3001\u6570\u5024\u30b5\u30a4\u30ba\u3001\u6587\u5b57ASCII\u9806\u5e8f\u3001\u307e\u305f\u306f\u30ab\u30b9\u30bf\u30e0\u57fa\u6e96\u306a\u3069\u3001\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u8a2d\u5b9a\u3067\u304d\u307e\u3059\u3002

        \u56f3 11-1 \u00a0 Data types and comparator examples

        "},{"location":"chapter_sorting/sorting_algorithm/#1111","title":"11.1.1 \u00a0 \u8a55\u4fa1\u6b21\u5143","text":"

        \u5b9f\u884c\u52b9\u7387\uff1a\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6642\u9593\u8a08\u7b97\u91cf\u304c\u3067\u304d\u308b\u3060\u3051\u4f4e\u3044\u3053\u3068\u3092\u671f\u5f85\u3057\u3001\u5168\u4f53\u7684\u306a\u64cd\u4f5c\u6570\u3082\u5c11\u306a\u3044\u3053\u3068\uff08\u6642\u9593\u8a08\u7b97\u91cf\u306e\u5b9a\u6570\u9805\u3092\u4e0b\u3052\u308b\uff09\u3092\u671b\u307f\u307e\u3059\u3002\u5927\u5bb9\u91cf\u30c7\u30fc\u30bf\u3067\u306f\u3001\u5b9f\u884c\u52b9\u7387\u304c\u7279\u306b\u91cd\u8981\u3067\u3059\u3002

        \u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u6027\uff1a\u540d\u524d\u304c\u793a\u3059\u3068\u304a\u308a\u3001\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u30bd\u30fc\u30c8\u306f\u5143\u306e\u914d\u5217\u3092\u76f4\u63a5\u64cd\u4f5c\u3059\u308b\u3053\u3068\u3067\u5b9f\u73fe\u3055\u308c\u3001\u8ffd\u52a0\u306e\u30d8\u30eb\u30d1\u30fc\u914d\u5217\u304c\u4e0d\u8981\u3067\u3042\u308b\u305f\u3081\u3001\u30e1\u30e2\u30ea\u3092\u7bc0\u7d04\u3057\u307e\u3059\u3002\u4e00\u822c\u7684\u306b\u3001\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u30bd\u30fc\u30c8\u306f\u30c7\u30fc\u30bf\u79fb\u52d5\u64cd\u4f5c\u304c\u5c11\u306a\u304f\u3001\u9ad8\u901f\u3067\u3059\u3002

        \u5b89\u5b9a\u6027\uff1a\u5b89\u5b9a\u30bd\u30fc\u30c8\u306f\u3001\u30bd\u30fc\u30c8\u5f8c\u306b\u914d\u5217\u5185\u306e\u7b49\u3057\u3044\u8981\u7d20\u306e\u76f8\u5bfe\u9806\u5e8f\u304c\u5909\u308f\u3089\u306a\u3044\u3053\u3068\u3092\u4fdd\u8a3c\u3057\u307e\u3059\u3002

        \u5b89\u5b9a\u30bd\u30fc\u30c8\u306f\u3001\u30de\u30eb\u30c1\u30ad\u30fc\u30bd\u30fc\u30c8\u30b7\u30ca\u30ea\u30aa\u306b\u304a\u3044\u3066\u5fc5\u8981\u6761\u4ef6\u3067\u3059\u3002\u5b66\u751f\u60c5\u5831\u3092\u683c\u7d0d\u3059\u308b\u30c6\u30fc\u30d6\u30eb\u304c\u3042\u308a\u3001\u7b2c1\u5217\u3068\u7b2c2\u5217\u304c\u305d\u308c\u305e\u308c\u540d\u524d\u3068\u5e74\u9f62\u3067\u3042\u308b\u3068\u3057\u307e\u3059\u3002\u3053\u306e\u5834\u5408\u3001\u4e0d\u5b89\u5b9a\u30bd\u30fc\u30c8\u306f\u5165\u529b\u30c7\u30fc\u30bf\u306e\u9806\u5e8f\u3092\u5931\u3046\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\uff1a

        # \u5165\u529b\u30c7\u30fc\u30bf\u306f\u540d\u524d\u3067\u30bd\u30fc\u30c8\u6e08\u307f\n# (\u540d\u524d, \u5e74\u9f62)\n  ('A', 19)\n  ('B', 18)\n  ('C', 21)\n  ('D', 19)\n  ('E', 23)\n\n# \u4e0d\u5b89\u5b9a\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u4f7f\u7528\u3057\u3066\u30ea\u30b9\u30c8\u3092\u5e74\u9f62\u3067\u30bd\u30fc\u30c8\u3059\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\n# \u7d50\u679c\u306f('D', 19)\u3068('A', 19)\u306e\u76f8\u5bfe\u4f4d\u7f6e\u3092\u5909\u66f4\u3057\u3001\n# \u5165\u529b\u30c7\u30fc\u30bf\u304c\u540d\u524d\u3067\u30bd\u30fc\u30c8\u6e08\u307f\u3067\u3042\u308b\u3068\u3044\u3046\u6027\u8cea\u304c\u5931\u308f\u308c\u308b\n  ('B', 18)\n  ('D', 19)\n  ('A', 19)\n  ('C', 21)\n  ('E', 23)\n

        \u9069\u5fdc\u6027\uff1a\u9069\u5fdc\u30bd\u30fc\u30c8\u306f\u5165\u529b\u30c7\u30fc\u30bf\u5185\u306e\u65e2\u5b58\u306e\u9806\u5e8f\u60c5\u5831\u3092\u6d3b\u7528\u3057\u3066\u8a08\u7b97\u8ca0\u8377\u3092\u524a\u6e1b\u3057\u3001\u3088\u308a\u6700\u9069\u306a\u6642\u9593\u52b9\u7387\u3092\u5b9f\u73fe\u3057\u307e\u3059\u3002\u9069\u5fdc\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6700\u826f\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\u306f\u3001\u901a\u5e38\u5e73\u5747\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\u3088\u308a\u3082\u512a\u308c\u3066\u3044\u307e\u3059\u3002

        \u6bd4\u8f03\u30d9\u30fc\u30b9\u307e\u305f\u306f\u975e\u6bd4\u8f03\u30d9\u30fc\u30b9\uff1a\u6bd4\u8f03\u30d9\u30fc\u30b9\u30bd\u30fc\u30c8\u306f\u6bd4\u8f03\u6f14\u7b97\u5b50\uff08\\(<\\)\u3001\\(=\\)\u3001\\(>\\)\uff09\u306b\u4f9d\u5b58\u3057\u3066\u8981\u7d20\u306e\u76f8\u5bfe\u9806\u5e8f\u3092\u6c7a\u5b9a\u3057\u3001\u914d\u5217\u5168\u4f53\u3092\u30bd\u30fc\u30c8\u3057\u307e\u3059\u3002\u7406\u8ad6\u7684\u6700\u9069\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n \\log n)\\)\u3067\u3059\u3002\u4e00\u65b9\u3001\u975e\u6bd4\u8f03\u30bd\u30fc\u30c8\u306f\u6bd4\u8f03\u6f14\u7b97\u5b50\u3092\u4f7f\u7528\u305b\u305a\u3001\\(O(n)\\)\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3092\u5b9f\u73fe\u3067\u304d\u307e\u3059\u304c\u3001\u6c4e\u7528\u6027\u306f\u6bd4\u8f03\u7684\u52a3\u308a\u307e\u3059\u3002

        "},{"location":"chapter_sorting/sorting_algorithm/#1112","title":"11.1.2 \u00a0 \u7406\u60f3\u7684\u306a\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0","text":"

        \u9ad8\u901f\u5b9f\u884c\u3001\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u3001\u5b89\u5b9a\u3001\u9069\u5fdc\u3001\u6c4e\u7528\u3002\u660e\u3089\u304b\u306b\u3001\u3053\u308c\u3089\u306e\u3059\u3079\u3066\u306e\u7279\u5fb4\u3092\u7d44\u307f\u5408\u308f\u305b\u305f\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u4eca\u65e5\u307e\u3067\u898b\u3064\u304b\u3063\u3066\u3044\u307e\u305b\u3093\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u9078\u629e\u3059\u308b\u969b\u306f\u3001\u30c7\u30fc\u30bf\u306e\u7279\u5b9a\u306e\u7279\u5fb4\u3068\u554f\u984c\u306e\u8981\u4ef6\u306b\u57fa\u3065\u3044\u3066\u6c7a\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u6b21\u306b\u3001\u3055\u307e\u3056\u307e\u306a\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u4e00\u7dd2\u306b\u5b66\u3073\u3001\u4e0a\u8a18\u306e\u8a55\u4fa1\u6b21\u5143\u306b\u57fa\u3065\u3044\u3066\u305d\u308c\u305e\u308c\u306e\u5229\u70b9\u3068\u6b20\u70b9\u3092\u5206\u6790\u3057\u307e\u3059\u3002

        "},{"location":"chapter_sorting/summary/","title":"11.11 \u00a0 \u307e\u3068\u3081","text":""},{"location":"chapter_sorting/summary/#1","title":"1. \u00a0 \u91cd\u8981\u306a\u5fa9\u7fd2","text":"
        • \u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u306f\u96a3\u63a5\u3059\u308b\u8981\u7d20\u3092\u4ea4\u63db\u3059\u308b\u3053\u3068\u3067\u52d5\u4f5c\u3057\u307e\u3059\u3002\u30d5\u30e9\u30b0\u3092\u8ffd\u52a0\u3057\u3066\u65e9\u671f\u30ea\u30bf\u30fc\u30f3\u3092\u53ef\u80fd\u306b\u3059\u308b\u3053\u3068\u3067\u3001\u30d0\u30d6\u30eb\u30bd\u30fc\u30c8\u306e\u6700\u826f\u30b1\u30fc\u30b9\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3092 \\(O(n)\\) \u306b\u6700\u9069\u5316\u3067\u304d\u307e\u3059\u3002
        • \u633f\u5165\u30bd\u30fc\u30c8\u306f\u3001\u672a\u30bd\u30fc\u30c8\u533a\u9593\u304b\u3089\u8981\u7d20\u3092\u53d6\u308a\u51fa\u3057\u3066\u30bd\u30fc\u30c8\u6e08\u307f\u533a\u9593\u306e\u6b63\u3057\u3044\u4f4d\u7f6e\u306b\u633f\u5165\u3059\u308b\u3053\u3068\u3067\u5404\u30e9\u30a6\u30f3\u30c9\u3092\u30bd\u30fc\u30c8\u3057\u307e\u3059\u3002\u633f\u5165\u30bd\u30fc\u30c8\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n^2)\\) \u3067\u3059\u304c\u3001\u5358\u4f4d\u3042\u305f\u308a\u306e\u64cd\u4f5c\u304c\u6bd4\u8f03\u7684\u5c11\u306a\u3044\u305f\u3081\u3001\u5c11\u91cf\u306e\u30c7\u30fc\u30bf\u306e\u30bd\u30fc\u30c8\u3067\u306f\u975e\u5e38\u306b\u4eba\u6c17\u304c\u3042\u308a\u307e\u3059\u3002
        • \u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306f\u6b69\u54e8\u5206\u5272\u64cd\u4f5c\u306b\u57fa\u3065\u3044\u3066\u3044\u307e\u3059\u3002\u6b69\u54e8\u5206\u5272\u3067\u306f\u3001\u5e38\u306b\u6700\u60aa\u306e\u30d4\u30dc\u30c3\u30c8\u3092\u9078\u3076\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u304c \\(O(n^2)\\) \u306b\u52a3\u5316\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u4e2d\u592e\u5024\u3084\u30e9\u30f3\u30c0\u30e0\u30d4\u30dc\u30c3\u30c8\u3092\u5c0e\u5165\u3059\u308b\u3053\u3068\u3067\u3001\u305d\u306e\u3088\u3046\u306a\u52a3\u5316\u306e\u78ba\u7387\u3092\u6e1b\u3089\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u672b\u5c3e\u518d\u5e30\u306f\u518d\u5e30\u306e\u6df1\u3055\u3092\u52b9\u679c\u7684\u306b\u6e1b\u3089\u3057\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u3092 \\(O(\\log n)\\) \u306b\u6700\u9069\u5316\u3057\u307e\u3059\u3002
        • \u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u306b\u306f\u5206\u5272\u3068\u30de\u30fc\u30b8\u306e2\u3064\u306e\u6bb5\u968e\u304c\u3042\u308a\u3001\u901a\u5e38\u5206\u5272\u7d71\u6cbb\u6226\u7565\u3092\u4f53\u73fe\u3057\u3066\u3044\u307e\u3059\u3002\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u3067\u306f\u3001\u914d\u5217\u306e\u30bd\u30fc\u30c8\u306b\u306f\u88dc\u52a9\u914d\u5217\u306e\u4f5c\u6210\u304c\u5fc5\u8981\u3067\u3001\u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(n)\\) \u306b\u306a\u308a\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u30ea\u30b9\u30c8\u306e\u30bd\u30fc\u30c8\u306e\u7a7a\u9593\u8a08\u7b97\u91cf\u306f \\(O(1)\\) \u306b\u6700\u9069\u5316\u3067\u304d\u307e\u3059\u3002
        • \u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306f3\u3064\u306e\u624b\u9806\u304b\u3089\u69cb\u6210\u3055\u308c\u307e\u3059\uff1a\u30c7\u30fc\u30bf\u3092\u30d0\u30b1\u30c3\u30c8\u306b\u5206\u6563\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u5185\u3067\u306e\u30bd\u30fc\u30c8\u3001\u30d0\u30b1\u30c3\u30c8\u9806\u3067\u306e\u7d50\u679c\u306e\u30de\u30fc\u30b8\u3002\u3053\u308c\u3082\u5206\u5272\u7d71\u6cbb\u6226\u7565\u3092\u4f53\u73fe\u3057\u3001\u975e\u5e38\u306b\u5927\u304d\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306e\u9375\u306f\u30c7\u30fc\u30bf\u306e\u5747\u7b49\u5206\u6563\u3067\u3059\u3002
        • \u8a08\u6570\u30bd\u30fc\u30c8\u306f\u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306e\u5909\u5f62\u3067\u3001\u5404\u30c7\u30fc\u30bf\u30dd\u30a4\u30f3\u30c8\u306e\u51fa\u73fe\u56de\u6570\u3092\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u3053\u3068\u3067\u30bd\u30fc\u30c8\u3057\u307e\u3059\u3002\u8a08\u6570\u30bd\u30fc\u30c8\u306f\u9650\u3089\u308c\u305f\u7bc4\u56f2\u306e\u30c7\u30fc\u30bf\u3092\u6301\u3064\u5927\u304d\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306b\u9069\u3057\u3066\u304a\u308a\u3001\u30c7\u30fc\u30bf\u3092\u6b63\u306e\u6574\u6570\u306b\u5909\u63db\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u57fa\u6570\u30bd\u30fc\u30c8\u306f\u6841\u3054\u3068\u306b\u30bd\u30fc\u30c8\u3059\u308b\u3053\u3068\u3067\u30c7\u30fc\u30bf\u3092\u51e6\u7406\u3057\u3001\u30c7\u30fc\u30bf\u304c\u56fa\u5b9a\u9577\u306e\u6570\u5024\u3068\u3057\u3066\u8868\u73fe\u3055\u308c\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u5168\u4f53\u7684\u306b\u3001\u79c1\u305f\u3061\u306f\u9ad8\u52b9\u7387\u3001\u5b89\u5b9a\u6027\u3001\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u64cd\u4f5c\u3001\u9069\u5fdc\u6027\u3092\u6301\u3064\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u6c42\u3081\u3066\u3044\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u4ed6\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u3084\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u540c\u69d8\u306b\u3001\u3053\u308c\u3089\u3059\u3079\u3066\u306e\u6761\u4ef6\u3092\u540c\u6642\u306b\u6e80\u305f\u3059\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u5b58\u5728\u3057\u307e\u305b\u3093\u3002\u5b9f\u969b\u306e\u5fdc\u7528\u3067\u306f\u3001\u30c7\u30fc\u30bf\u306e\u7279\u6027\u306b\u57fa\u3065\u3044\u3066\u9069\u5207\u306a\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u9078\u629e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u4ee5\u4e0b\u306e\u56f3\u306f\u3001\u52b9\u7387\u6027\u3001\u5b89\u5b9a\u6027\u3001\u30a4\u30f3\u30d7\u30ec\u30fc\u30b9\u6027\u3001\u9069\u5fdc\u6027\u306e\u89b3\u70b9\u304b\u3089\u4e3b\u6d41\u306e\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u6bd4\u8f03\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 11-19 \u00a0 \u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u6bd4\u8f03

        "},{"location":"chapter_sorting/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q: \u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b89\u5b9a\u6027\u306f\u3044\u3064\u5fc5\u8981\u3067\u3059\u304b\uff1f

        \u5b9f\u969b\u306b\u306f\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u4e00\u3064\u306e\u5c5e\u6027\u306b\u57fa\u3065\u3044\u3066\u30bd\u30fc\u30c8\u3059\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u5b66\u751f\u306f\u540d\u524d\u3068\u8eab\u9577\u306e\u5c5e\u6027\u3092\u6301\u3061\u3001\u591a\u6bb5\u968e\u30bd\u30fc\u30c8\u3092\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3092\u76ee\u6307\u3057\u307e\u3059\uff1a\u6700\u521d\u306b\u540d\u524d\u3067 (A, 180) (B, 185) (C, 170) (D, 170) \u3092\u53d6\u5f97\u3057\u3001\u6b21\u306b\u8eab\u9577\u3067\u3002\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u4e0d\u5b89\u5b9a\u306a\u305f\u3081\u3001(D, 170) (C, 170) (A, 180) (B, 185) \u306b\u306a\u3063\u3066\u3057\u307e\u3046\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        \u5b66\u751fD\u3068C\u306e\u4f4d\u7f6e\u304c\u4ea4\u63db\u3055\u308c\u3001\u540d\u524d\u306e\u9806\u5e8f\u6027\u304c\u7834\u3089\u308c\u3066\u3044\u308b\u306e\u304c\u5206\u304b\u308a\u307e\u3059\u3002\u3053\u308c\u306f\u671b\u307e\u3057\u304f\u3042\u308a\u307e\u305b\u3093\u3002

        Q: \u6b69\u54e8\u5206\u5272\u3067\u306e\u300c\u53f3\u304b\u3089\u5de6\u3078\u306e\u691c\u7d22\u300d\u3068\u300c\u5de6\u304b\u3089\u53f3\u3078\u306e\u691c\u7d22\u300d\u306e\u9806\u5e8f\u3092\u4ea4\u63db\u3067\u304d\u307e\u3059\u304b\uff1f

        \u3044\u3044\u3048\u3001\u6700\u5de6\u8981\u7d20\u3092\u30d4\u30dc\u30c3\u30c8\u3068\u3057\u3066\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001\u6700\u521d\u306b\u300c\u53f3\u304b\u3089\u5de6\u3078\u306e\u691c\u7d22\u300d\u3092\u884c\u3044\u3001\u6b21\u306b\u300c\u5de6\u304b\u3089\u53f3\u3078\u306e\u691c\u7d22\u300d\u3092\u884c\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u7d50\u8ad6\u306f\u3084\u3084\u76f4\u89b3\u306b\u53cd\u3059\u308b\u306e\u3067\u3001\u7406\u7531\u3092\u5206\u6790\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002

        \u6b69\u54e8\u5206\u5272 partition() \u306e\u6700\u5f8c\u306e\u30b9\u30c6\u30c3\u30d7\u306f nums[left] \u3068 nums[i] \u3092\u4ea4\u63db\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u4ea4\u63db\u5f8c\u3001\u30d4\u30dc\u30c3\u30c8\u306e\u5de6\u5074\u306e\u8981\u7d20\u306f\u3059\u3079\u3066\u30d4\u30dc\u30c3\u30c8\u4ee5\u4e0b\u306b\u306a\u308a\u307e\u3059\u3002\u3053\u308c\u306b\u306f\u6700\u5f8c\u306e\u4ea4\u63db\u524d\u306b nums[left] >= nums[i] \u304c\u6210\u308a\u7acb\u3064\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u300c\u5de6\u304b\u3089\u53f3\u3078\u306e\u691c\u7d22\u300d\u3092\u6700\u521d\u306b\u884c\u3044\u3001\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5927\u304d\u3044\u8981\u7d20\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3001i == j \u3067\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\u3057\u3001nums[j] == nums[i] > nums[left] \u3068\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u6700\u7d42\u4ea4\u63db\u64cd\u4f5c\u306f\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5927\u304d\u3044\u8981\u7d20\u3092\u914d\u5217\u306e\u5de6\u7aef\u306b\u4ea4\u63db\u3057\u3001\u6b69\u54e8\u5206\u5272\u3092\u5931\u6557\u3055\u305b\u307e\u3059\u3002

        \u4f8b\u3048\u3070\u3001\u914d\u5217 [0, 0, 0, 0, 1] \u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u6700\u521d\u306b\u300c\u5de6\u304b\u3089\u53f3\u3078\u306e\u691c\u7d22\u300d\u3092\u884c\u3046\u3068\u3001\u6b69\u54e8\u5206\u5272\u5f8c\u306e\u914d\u5217\u306f [1, 0, 0, 0, 0] \u3068\u306a\u308a\u3001\u3053\u308c\u306f\u6b63\u3057\u304f\u3042\u308a\u307e\u305b\u3093\u3002

        \u3055\u3089\u306b\u8003\u3048\u308b\u3068\u3001nums[right] \u3092\u30d4\u30dc\u30c3\u30c8\u3068\u3057\u3066\u9078\u629e\u3059\u308b\u5834\u5408\u3001\u307e\u3063\u305f\u304f\u9006\u3067\u3001\u6700\u521d\u306b\u300c\u5de6\u304b\u3089\u53f3\u3078\u306e\u691c\u7d22\u300d\u3092\u884c\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        Q: \u672b\u5c3e\u518d\u5e30\u6700\u9069\u5316\u306b\u3064\u3044\u3066\u3001\u77ed\u3044\u914d\u5217\u3092\u9078\u629e\u3059\u308b\u3053\u3068\u3067\u518d\u5e30\u306e\u6df1\u3055\u304c \\(\\log n\\) \u3092\u8d85\u3048\u306a\u3044\u3053\u3068\u3092\u4fdd\u8a3c\u3059\u308b\u306e\u306f\u306a\u305c\u3067\u3059\u304b\uff1f

        \u518d\u5e30\u306e\u6df1\u3055\u306f\u73fe\u5728\u30ea\u30bf\u30fc\u30f3\u3057\u3066\u3044\u306a\u3044\u518d\u5e30\u30e1\u30bd\u30c3\u30c9\u306e\u6570\u3067\u3059\u3002\u6b69\u54e8\u5206\u5272\u306e\u5404\u30e9\u30a6\u30f3\u30c9\u306f\u5143\u306e\u914d\u5217\u30922\u3064\u306e\u526f\u914d\u5217\u306b\u5206\u5272\u3057\u307e\u3059\u3002\u672b\u5c3e\u518d\u5e30\u6700\u9069\u5316\u306b\u3088\u308a\u3001\u518d\u5e30\u7684\u306b\u7d9a\u884c\u3059\u308b\u526f\u914d\u5217\u306e\u9577\u3055\u306f\u6700\u5927\u3067\u3082\u5143\u306e\u914d\u5217\u9577\u306e\u534a\u5206\u3067\u3059\u3002\u6700\u60aa\u306e\u5834\u5408\u5e38\u306b\u9577\u3055\u3092\u534a\u5206\u306b\u3059\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u6700\u7d42\u7684\u306a\u518d\u5e30\u306e\u6df1\u3055\u306f \\(\\log n\\) \u306b\u306a\u308a\u307e\u3059\u3002

        \u5143\u306e\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u3092\u898b\u76f4\u3059\u3068\u3001\u3088\u308a\u5927\u304d\u306a\u914d\u5217\u3092\u7d99\u7d9a\u7684\u306b\u518d\u5e30\u51e6\u7406\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u6700\u60aa\u306e\u5834\u5408 \\(n\\)\u3001\\(n - 1\\)\u3001...\u3001\\(2\\)\u3001\\(1\\) \u3067\u3001\u518d\u5e30\u306e\u6df1\u3055\u306f \\(n\\) \u306b\u306a\u308a\u307e\u3059\u3002\u672b\u5c3e\u518d\u5e30\u6700\u9069\u5316\u306f\u3053\u306e\u30b7\u30ca\u30ea\u30aa\u3092\u56de\u907f\u3067\u304d\u307e\u3059\u3002

        Q: \u914d\u5217\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u304c\u7b49\u3057\u3044\u5834\u5408\u3001\u30af\u30a4\u30c3\u30af\u30bd\u30fc\u30c8\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n^2)\\) \u3067\u3059\u304b\uff1f\u3053\u306e\u52a3\u5316\u30b1\u30fc\u30b9\u3092\u3069\u3046\u51e6\u7406\u3059\u3079\u304d\u3067\u3059\u304b\uff1f

        \u306f\u3044\u3002\u3053\u306e\u72b6\u6cc1\u306b\u3064\u3044\u3066\u306f\u3001\u6b69\u54e8\u5206\u5272\u3092\u4f7f\u7528\u3057\u3066\u914d\u5217\u3092\u30d4\u30dc\u30c3\u30c8\u3088\u308a\u5c0f\u3055\u3044\u3001\u7b49\u3057\u3044\u3001\u5927\u304d\u3044\u306e3\u3064\u306e\u90e8\u5206\u306b\u5206\u5272\u3059\u308b\u3053\u3068\u3092\u691c\u8a0e\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u5c0f\u3055\u3044\u90e8\u5206\u3068\u5927\u304d\u3044\u90e8\u5206\u306e\u307f\u3092\u518d\u5e30\u7684\u306b\u9032\u3081\u307e\u3059\u3002\u3053\u306e\u65b9\u6cd5\u3067\u306f\u3001\u3059\u3079\u3066\u306e\u5165\u529b\u8981\u7d20\u304c\u7b49\u3057\u3044\u914d\u5217\u30921\u30e9\u30a6\u30f3\u30c9\u306e\u6b69\u54e8\u5206\u5272\u3060\u3051\u3067\u30bd\u30fc\u30c8\u3067\u304d\u307e\u3059\u3002

        Q: \u306a\u305c\u30d0\u30b1\u30c3\u30c8\u30bd\u30fc\u30c8\u306e\u6700\u60aa\u30b1\u30fc\u30b9\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n^2)\\) \u3067\u3059\u304b\uff1f

        \u6700\u60aa\u306e\u5834\u5408\u3001\u3059\u3079\u3066\u306e\u8981\u7d20\u304c\u540c\u3058\u30d0\u30b1\u30c3\u30c8\u306b\u914d\u7f6e\u3055\u308c\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u8981\u7d20\u3092\u30bd\u30fc\u30c8\u3059\u308b\u305f\u3081\u306b \\(O(n^2)\\) \u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f \\(O(n^2)\\) \u306b\u306a\u308a\u307e\u3059\u3002

        "},{"location":"chapter_stack_and_queue/","title":"\u7b2c 5 \u7ae0 \u00a0 \u30b9\u30bf\u30c3\u30af\u3068\u30ad\u30e5\u30fc","text":"

        Abstract

        \u30b9\u30bf\u30c3\u30af\u306f\u7a4d\u307f\u91cd\u306d\u3089\u308c\u305f\u732b\u306e\u3088\u3046\u306a\u3082\u306e\u3067\u3001\u30ad\u30e5\u30fc\u306f\u4e00\u5217\u306b\u4e26\u3093\u3060\u732b\u306e\u3088\u3046\u306a\u3082\u306e\u3067\u3059\u3002

        \u305d\u308c\u3089\u306f\u305d\u308c\u305e\u308c\u3001\u5f8c\u5165\u5148\u51fa\uff08LIFO\uff09\u3068\u5148\u5165\u5148\u51fa\uff08FIFO\uff09\u306e\u8ad6\u7406\u95a2\u4fc2\u3092\u8868\u3057\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_stack_and_queue/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 5.1 \u00a0 \u30b9\u30bf\u30c3\u30af
        • 5.2 \u00a0 \u30ad\u30e5\u30fc
        • 5.3 \u00a0 \u4e21\u7aef\u30ad\u30e5\u30fc
        • 5.4 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_stack_and_queue/deque/","title":"5.3 \u00a0 \u4e21\u7aef\u30ad\u30e5\u30fc","text":"

        \u30ad\u30e5\u30fc\u3067\u306f\u3001\u5148\u982d\u304b\u3089\u306e\u8981\u7d20\u306e\u524a\u9664\u3084\u672b\u5c3e\u3078\u306e\u8981\u7d20\u306e\u8ffd\u52a0\u306e\u307f\u304c\u53ef\u80fd\u3067\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u4e21\u7aef\u30ad\u30e5\u30fc\uff08deque\uff09\u306f\u3088\u308a\u67d4\u8edf\u6027\u3092\u63d0\u4f9b\u3057\u3001\u5148\u982d\u3068\u672b\u5c3e\u306e\u4e21\u65b9\u3067\u8981\u7d20\u306e\u8ffd\u52a0\u3084\u524a\u9664\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002

        \u56f3 5-7 \u00a0 \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u64cd\u4f5c

        "},{"location":"chapter_stack_and_queue/deque/#531","title":"5.3.1 \u00a0 \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c","text":"

        \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002\u5177\u4f53\u7684\u306a\u30e1\u30bd\u30c3\u30c9\u540d\u306f\u4f7f\u7528\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u3088\u3063\u3066\u7570\u306a\u308a\u307e\u3059\u3002

        \u8868 5-3 \u00a0 \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u64cd\u4f5c\u52b9\u7387

        \u30e1\u30bd\u30c3\u30c9\u540d \u8aac\u660e \u6642\u9593\u8a08\u7b97\u91cf pushFirst() \u5148\u982d\u306b\u8981\u7d20\u3092\u8ffd\u52a0 \\(O(1)\\) pushLast() \u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0 \\(O(1)\\) popFirst() \u5148\u982d\u8981\u7d20\u3092\u524a\u9664 \\(O(1)\\) popLast() \u672b\u5c3e\u8981\u7d20\u3092\u524a\u9664 \\(O(1)\\) peekFirst() \u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 \\(O(1)\\) peekLast() \u672b\u5c3e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 \\(O(1)\\)

        \u540c\u69d8\u306b\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u5b9f\u88c5\u3055\u308c\u305f\u4e21\u7aef\u30ad\u30e5\u30fc\u30af\u30e9\u30b9\u3092\u76f4\u63a5\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin deque.py
        from collections import deque\n\n# \u4e21\u7aef\u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316\ndeq: deque[int] = deque()\n\n# \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\ndeq.append(2)      # \u672b\u5c3e\u306b\u8ffd\u52a0\ndeq.append(5)\ndeq.append(4)\ndeq.appendleft(3)  # \u5148\u982d\u306b\u8ffd\u52a0\ndeq.appendleft(1)\n\n# \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\nfront: int = deq[0]  # \u5148\u982d\u8981\u7d20\nrear: int = deq[-1]  # \u672b\u5c3e\u8981\u7d20\n\n# \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\npop_front: int = deq.popleft()  # \u5148\u982d\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\npop_rear: int = deq.pop()       # \u672b\u5c3e\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\n\n# \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97\nsize: int = len(deq)\n\n# \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\nis_empty: bool = len(deq) == 0\n
        deque.cpp
        /* \u4e21\u7aef\u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\ndeque<int> deque;\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\ndeque.push_back(2);   // \u672b\u5c3e\u306b\u8ffd\u52a0\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3);  // \u5148\u982d\u306b\u8ffd\u52a0\ndeque.push_front(1);\n\n/* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint front = deque.front(); // \u5148\u982d\u8981\u7d20\nint back = deque.back();   // \u672b\u5c3e\u8981\u7d20\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\ndeque.pop_front();  // \u5148\u982d\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\ndeque.pop_back();   // \u672b\u5c3e\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = deque.size();\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d */\nbool empty = deque.empty();\n
        deque.java
        /* \u4e21\u7aef\u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\nDeque<Integer> deque = new LinkedList<>();\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\ndeque.offerLast(2);   // \u672b\u5c3e\u306b\u8ffd\u52a0\ndeque.offerLast(5);\ndeque.offerLast(4);\ndeque.offerFirst(3);  // \u5148\u982d\u306b\u8ffd\u52a0\ndeque.offerFirst(1);\n\n/* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint peekFirst = deque.peekFirst();  // \u5148\u982d\u8981\u7d20\nint peekLast = deque.peekLast();    // \u672b\u5c3e\u8981\u7d20\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\nint popFirst = deque.pollFirst();  // \u5148\u982d\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\nint popLast = deque.pollLast();    // \u672b\u5c3e\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = deque.size();\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d */\nboolean isEmpty = deque.isEmpty();\n
        deque.cs
        /* \u4e21\u7aef\u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// C#\u3067\u306f\u3001LinkedList\u3092\u4e21\u7aef\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\nLinkedList<int> deque = new();\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\ndeque.AddLast(2);   // \u672b\u5c3e\u306b\u8ffd\u52a0\ndeque.AddLast(5);\ndeque.AddLast(4);\ndeque.AddFirst(3);  // \u5148\u982d\u306b\u8ffd\u52a0\ndeque.AddFirst(1);\n\n/* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint peekFirst = deque.First.Value;  // \u5148\u982d\u8981\u7d20\nint peekLast = deque.Last.Value;    // \u672b\u5c3e\u8981\u7d20\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\ndeque.RemoveFirst();  // \u5148\u982d\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\ndeque.RemoveLast();   // \u672b\u5c3e\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = deque.Count;\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d */\nbool isEmpty = deque.Count == 0;\n
        deque_test.go
        /* \u4e21\u7aef\u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// Go\u3067\u306f\u3001list\u3092\u4e21\u7aef\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\ndeque := list.New()\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\ndeque.PushBack(2)      // \u672b\u5c3e\u306b\u8ffd\u52a0\ndeque.PushBack(5)\ndeque.PushBack(4)\ndeque.PushFront(3)     // \u5148\u982d\u306b\u8ffd\u52a0\ndeque.PushFront(1)\n\n/* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nfront := deque.Front() // \u5148\u982d\u8981\u7d20\nrear := deque.Back()   // \u672b\u5c3e\u8981\u7d20\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\ndeque.Remove(front)    // \u5148\u982d\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\ndeque.Remove(rear)     // \u672b\u5c3e\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nsize := deque.Len()\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d */\nisEmpty := deque.Len() == 0\n
        deque.swift
        /* \u4e21\u7aef\u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// Swift\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u4e21\u7aef\u30ad\u30e5\u30fc\u30af\u30e9\u30b9\u304c\u306a\u3044\u305f\u3081\u3001Array\u3092\u4e21\u7aef\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\nvar deque: [Int] = []\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\ndeque.append(2) // \u672b\u5c3e\u306b\u8ffd\u52a0\ndeque.append(5)\ndeque.append(4)\ndeque.insert(3, at: 0) // \u5148\u982d\u306b\u8ffd\u52a0\ndeque.insert(1, at: 0)\n\n/* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nlet peekFirst = deque.first! // \u5148\u982d\u8981\u7d20\nlet peekLast = deque.last!   // \u672b\u5c3e\u8981\u7d20\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\n// Array\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001popFirst\u306e\u8a08\u7b97\u91cf\u306fO(n)\nlet popFirst = deque.removeFirst() // \u5148\u982d\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\nlet popLast = deque.removeLast()   // \u672b\u5c3e\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nlet size = deque.count\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d */\nlet isEmpty = deque.isEmpty\n
        deque.js
        /* \u4e21\u7aef\u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// JavaScript\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u306a\u3044\u305f\u3081\u3001Array\u3092\u4e21\u7aef\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\nconst deque = [];\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// \u6ce8\u610f\uff1aunshift()\u306f\u914d\u5217\u306e\u305f\u3081\u6642\u9593\u8a08\u7b97\u91cf\u304cO(n)\ndeque.unshift(3);\ndeque.unshift(1);\n\n/* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nconst peekFirst = deque[0]; // \u5148\u982d\u8981\u7d20\nconst peekLast = deque[deque.length - 1]; // \u672b\u5c3e\u8981\u7d20\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\n// \u6ce8\u610f\uff1ashift()\u306f\u914d\u5217\u306e\u305f\u3081\u6642\u9593\u8a08\u7b97\u91cf\u304cO(n)\nconst popFront = deque.shift(); // \u5148\u982d\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\nconst popBack = deque.pop();    // \u672b\u5c3e\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nconst size = deque.length;\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d */\nconst isEmpty = size === 0;\n
        deque.ts
        /* \u4e21\u7aef\u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// TypeScript\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u306a\u3044\u305f\u3081\u3001Array\u3092\u4e21\u7aef\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\nconst deque: number[] = [];\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// \u6ce8\u610f\uff1aunshift()\u306f\u914d\u5217\u306e\u305f\u3081\u6642\u9593\u8a08\u7b97\u91cf\u304cO(n)\ndeque.unshift(3);\ndeque.unshift(1);\n\n/* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nconst peekFirst: number = deque[0]; // \u5148\u982d\u8981\u7d20\nconst peekLast: number = deque[deque.length - 1]; // \u672b\u5c3e\u8981\u7d20\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\n// \u6ce8\u610f\uff1ashift()\u306f\u914d\u5217\u306e\u305f\u3081\u6642\u9593\u8a08\u7b97\u91cf\u304cO(n)\nconst popFront: number = deque.shift() as number; // \u5148\u982d\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\nconst popBack: number = deque.pop() as number;    // \u672b\u5c3e\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nconst size: number = deque.length;\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d */\nconst isEmpty: boolean = size === 0;\n
        deque.dart
        /* \u4e21\u7aef\u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// Dart\u3067\u306f\u3001Queue\u304c\u4e21\u7aef\u30ad\u30e5\u30fc\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u308b\nQueue<int> deque = Queue<int>();\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\ndeque.addLast(2);  // \u672b\u5c3e\u306b\u8ffd\u52a0\ndeque.addLast(5);\ndeque.addLast(4);\ndeque.addFirst(3); // \u5148\u982d\u306b\u8ffd\u52a0\ndeque.addFirst(1);\n\n/* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint peekFirst = deque.first; // \u5148\u982d\u8981\u7d20\nint peekLast = deque.last;   // \u672b\u5c3e\u8981\u7d20\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\nint popFirst = deque.removeFirst(); // \u5148\u982d\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\nint popLast = deque.removeLast();   // \u672b\u5c3e\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = deque.length;\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d */\nbool isEmpty = deque.isEmpty;\n
        deque.rs
        /* \u4e21\u7aef\u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\nlet mut deque: VecDeque<u32> = VecDeque::new();\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\ndeque.push_back(2);  // \u672b\u5c3e\u306b\u8ffd\u52a0\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3); // \u5148\u982d\u306b\u8ffd\u52a0\ndeque.push_front(1);\n\n/* \u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nif let Some(front) = deque.front() { // \u5148\u982d\u8981\u7d20\n}\nif let Some(rear) = deque.back() {   // \u672b\u5c3e\u8981\u7d20\n}\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\nif let Some(pop_front) = deque.pop_front() { // \u5148\u982d\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\n}\nif let Some(pop_rear) = deque.pop_back() {   // \u672b\u5c3e\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\n}\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nlet size = deque.len();\n\n/* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d */\nlet is_empty = deque.is_empty();\n
        deque.c
        // C\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u305b\u3093\n
        deque.kt
        \n
        "},{"location":"chapter_stack_and_queue/deque/#532","title":"5.3.2 \u00a0 \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u5b9f\u88c5 *","text":"

        \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u5b9f\u88c5\u306f\u901a\u5e38\u306e\u30ad\u30e5\u30fc\u306e\u5b9f\u88c5\u3068\u4f3c\u3066\u304a\u308a\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u307e\u305f\u306f\u914d\u5217\u3092\u57fa\u76e4\u3068\u306a\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u3057\u3066\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_stack_and_queue/deque/#1","title":"1. \u00a0 \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u5b9f\u88c5","text":"

        \u524d\u7bc0\u3067\u3001\u901a\u5e38\u306e\u5358\u4e00\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u4f7f\u3063\u3066\u30ad\u30e5\u30fc\u3092\u5b9f\u88c5\u3057\u305f\u3053\u3068\u3092\u601d\u3044\u51fa\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3053\u308c\u306f\u5148\u982d\u304b\u3089\u306e\u524a\u9664\uff08\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\u306b\u5bfe\u5fdc\uff09\u3068\u672b\u5c3e\u3078\u306e\u65b0\u3057\u3044\u8981\u7d20\u306e\u8ffd\u52a0\uff08\u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c\u306b\u5bfe\u5fdc\uff09\u3092\u4fbf\u5229\u306b\u884c\u3048\u308b\u305f\u3081\u3067\u3057\u305f\u3002

        \u4e21\u7aef\u30ad\u30e5\u30fc\u3067\u306f\u3001\u5148\u982d\u3068\u672b\u5c3e\u306e\u4e21\u65b9\u3067\u30a8\u30f3\u30ad\u30e5\u30fc\u3068\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\u3092\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u4e21\u7aef\u30ad\u30e5\u30fc\u306f\u9006\u65b9\u5411\u306e\u64cd\u4f5c\u3082\u5b9f\u88c5\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u305f\u3081\u3001\u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u57fa\u76e4\u3068\u306a\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u3057\u3066\u300c\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u300d\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u5148\u982d\u30ce\u30fc\u30c9\u3068\u672b\u5c3e\u30ce\u30fc\u30c9\u3092\u305d\u308c\u305e\u308c\u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u524d\u7aef\u3068\u5f8c\u7aef\u3068\u3057\u3066\u6271\u3044\u3001\u4e21\u7aef\u3067\u306e\u30ce\u30fc\u30c9\u306e\u8ffd\u52a0\u3068\u524a\u9664\u6a5f\u80fd\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002

        LinkedListDequepushLast()pushFirst()popLast()popFirst()

        \u56f3 5-8 \u00a0 \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u3088\u308b\u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u30a8\u30f3\u30ad\u30e5\u30fc\u3068\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\u306e\u5b9f\u88c5

        \u5b9f\u88c5\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linkedlist_deque.py
        class ListNode:\n    \"\"\"\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\"\"\"\n\n    def __init__(self, val: int):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        self.val: int = val\n        self.next: ListNode | None = None  # \u5f8c\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n        self.prev: ListNode | None = None  # \u524d\u99c6\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n\nclass LinkedListDeque:\n    \"\"\"\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30d9\u30fc\u30b9\u306e\u53cc\u7aef\u30ad\u30e5\u30fc\u30af\u30e9\u30b9\"\"\"\n\n    def __init__(self):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        self._front: ListNode | None = None  # \u30d8\u30c3\u30c9\u30ce\u30fc\u30c9 front\n        self._rear: ListNode | None = None  # \u30c6\u30fc\u30eb\u30ce\u30fc\u30c9 rear\n        self._size: int = 0  # \u53cc\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\n\n    def size(self) -> int:\n        \"\"\"\u53cc\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u53cc\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\"\"\"\n        return self._size == 0\n\n    def push(self, num: int, is_front: bool):\n        \"\"\"\u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c\"\"\"\n        node = ListNode(num)\n        # \u30ea\u30b9\u30c8\u304c\u7a7a\u306e\u5834\u5408\u3001front \u3068 rear \u306e\u4e21\u65b9\u3092 node \u306b\u5411\u3051\u308b\n        if self.is_empty():\n            self._front = self._rear = node\n        # \u524d\u7aef\u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c\n        elif is_front:\n            # \u30ce\u30fc\u30c9\u3092\u30ea\u30b9\u30c8\u306e\u5148\u982d\u306b\u8ffd\u52a0\n            self._front.prev = node\n            node.next = self._front\n            self._front = node  # \u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u3092\u66f4\u65b0\n        # \u5f8c\u7aef\u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c\n        else:\n            # \u30ce\u30fc\u30c9\u3092\u30ea\u30b9\u30c8\u306e\u672b\u5c3e\u306b\u8ffd\u52a0\n            self._rear.next = node\n            node.prev = self._rear\n            self._rear = node  # \u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u3092\u66f4\u65b0\n        self._size += 1  # \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u66f4\u65b0\n\n    def push_first(self, num: int):\n        \"\"\"\u524d\u7aef\u30a8\u30f3\u30ad\u30e5\u30fc\"\"\"\n        self.push(num, True)\n\n    def push_last(self, num: int):\n        \"\"\"\u5f8c\u7aef\u30a8\u30f3\u30ad\u30e5\u30fc\"\"\"\n        self.push(num, False)\n\n    def pop(self, is_front: bool) -> int:\n        \"\"\"\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Double-ended queue is empty\")\n        # \u524d\u7aef\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\n        if is_front:\n            val: int = self._front.val  # \u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u306e\u5024\u3092\u4e00\u6642\u7684\u306b\u4fdd\u5b58\n            # \u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u3092\u524a\u9664\n            fnext: ListNode | None = self._front.next\n            if fnext is not None:\n                fnext.prev = None\n                self._front.next = None\n            self._front = fnext  # \u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u3092\u66f4\u65b0\n        # \u5f8c\u7aef\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\n        else:\n            val: int = self._rear.val  # \u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u306e\u5024\u3092\u4e00\u6642\u7684\u306b\u4fdd\u5b58\n            # \u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u3092\u524a\u9664\n            rprev: ListNode | None = self._rear.prev\n            if rprev is not None:\n                rprev.next = None\n                self._rear.prev = None\n            self._rear = rprev  # \u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u3092\u66f4\u65b0\n        self._size -= 1  # \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u66f4\u65b0\n        return val\n\n    def pop_first(self) -> int:\n        \"\"\"\u524d\u7aef\u30c7\u30ad\u30e5\u30fc\"\"\"\n        return self.pop(True)\n\n    def pop_last(self) -> int:\n        \"\"\"\u5f8c\u7aef\u30c7\u30ad\u30e5\u30fc\"\"\"\n        return self.pop(False)\n\n    def peek_first(self) -> int:\n        \"\"\"\u524d\u7aef\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Double-ended queue is empty\")\n        return self._front.val\n\n    def peek_last(self) -> int:\n        \"\"\"\u5f8c\u7aef\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Double-ended queue is empty\")\n        return self._rear.val\n\n    def to_array(self) -> list[int]:\n        \"\"\"\u51fa\u529b\u7528\u306e\u914d\u5217\u3092\u8fd4\u3059\"\"\"\n        node = self._front\n        res = [0] * self.size()\n        for i in range(self.size()):\n            res[i] = node.val\n            node = node.next\n        return res\n
        linkedlist_deque.cpp
        /* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9 */\nstruct DoublyListNode {\n    int val;              // \u30ce\u30fc\u30c9\u306e\u5024\n    DoublyListNode *next; // \u5f8c\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    DoublyListNode *prev; // \u524d\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {\n    }\n};\n\n/* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u4e21\u7aef\u30ad\u30e5\u30fc\u30af\u30e9\u30b9 */\nclass LinkedListDeque {\n  private:\n    DoublyListNode *front, *rear; // \u5148\u982d\u30ce\u30fc\u30c9front\u3001\u672b\u5c3e\u30ce\u30fc\u30c9rear\n    int queSize = 0;              // \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\n\n  public:\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    LinkedListDeque() : front(nullptr), rear(nullptr) {\n    }\n\n    /* \u30c7\u30b9\u30c8\u30e9\u30af\u30bf */\n    ~LinkedListDeque() {\n        // \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8d70\u67fb\u3001\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3001\u30e1\u30e2\u30ea\u3092\u89e3\u653e\n        DoublyListNode *pre, *cur = front;\n        while (cur != nullptr) {\n            pre = cur;\n            cur = cur->next;\n            delete pre;\n        }\n    }\n\n    /* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    int size() {\n        return queSize;\n    }\n\n    /* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    bool isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c */\n    void push(int num, bool isFront) {\n        DoublyListNode *node = new DoublyListNode(num);\n        // \u30ea\u30b9\u30c8\u304c\u7a7a\u306e\u5834\u5408\u3001front\u3068rear\u306e\u4e21\u65b9\u3092node\u306b\u5411\u3051\u308b\n        if (isEmpty())\n            front = rear = node;\n        // \u5148\u982d\u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c\n        else if (isFront) {\n            // \u30ce\u30fc\u30c9\u3092\u30ea\u30b9\u30c8\u306e\u5148\u982d\u306b\u8ffd\u52a0\n            front->prev = node;\n            node->next = front;\n            front = node; // \u5148\u982d\u30ce\u30fc\u30c9\u3092\u66f4\u65b0\n        // \u672b\u5c3e\u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c\n        } else {\n            // \u30ce\u30fc\u30c9\u3092\u30ea\u30b9\u30c8\u306e\u672b\u5c3e\u306b\u8ffd\u52a0\n            rear->next = node;\n            node->prev = rear;\n            rear = node; // \u672b\u5c3e\u30ce\u30fc\u30c9\u3092\u66f4\u65b0\n        }\n        queSize++; // \u30ad\u30e5\u30fc\u9577\u3092\u66f4\u65b0\n    }\n\n    /* \u5148\u982d\u30a8\u30f3\u30ad\u30e5\u30fc */\n    void pushFirst(int num) {\n        push(num, true);\n    }\n\n    /* \u672b\u5c3e\u30a8\u30f3\u30ad\u30e5\u30fc */\n    void pushLast(int num) {\n        push(num, false);\n    }\n\n    /* \u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c */\n    int pop(bool isFront) {\n        if (isEmpty())\n            throw out_of_range(\"Queue is empty\");\n        int val;\n        // \u5148\u982d\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\n        if (isFront) {\n            val = front->val; // \u5148\u982d\u30ce\u30fc\u30c9\u306e\u5024\u3092\u4e00\u6642\u4fdd\u5b58\n            // \u5148\u982d\u30ce\u30fc\u30c9\u3092\u524a\u9664\n            DoublyListNode *fNext = front->next;\n            if (fNext != nullptr) {\n                fNext->prev = nullptr;\n                front->next = nullptr;\n            }\n            delete front;\n            front = fNext; // \u5148\u982d\u30ce\u30fc\u30c9\u3092\u66f4\u65b0\n        // \u672b\u5c3e\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\n        } else {\n            val = rear->val; // \u672b\u5c3e\u30ce\u30fc\u30c9\u306e\u5024\u3092\u4e00\u6642\u4fdd\u5b58\n            // \u672b\u5c3e\u30ce\u30fc\u30c9\u3092\u524a\u9664\n            DoublyListNode *rPrev = rear->prev;\n            if (rPrev != nullptr) {\n                rPrev->next = nullptr;\n                rear->prev = nullptr;\n            }\n            delete rear;\n            rear = rPrev; // \u672b\u5c3e\u30ce\u30fc\u30c9\u3092\u66f4\u65b0\n        }\n        queSize--; // \u30ad\u30e5\u30fc\u9577\u3092\u66f4\u65b0\n        return val;\n    }\n\n    /* \u5148\u982d\u30c7\u30ad\u30e5\u30fc */\n    int popFirst() {\n        return pop(true);\n    }\n\n    /* \u672b\u5c3e\u30c7\u30ad\u30e5\u30fc */\n    int popLast() {\n        return pop(false);\n    }\n\n    /* \u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    int peekFirst() {\n        if (isEmpty())\n            throw out_of_range(\"Double-ended queue is empty\");\n        return front->val;\n    }\n\n    /* \u672b\u5c3e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    int peekLast() {\n        if (isEmpty())\n            throw out_of_range(\"Double-ended queue is empty\");\n        return rear->val;\n    }\n\n    /* \u5370\u5237\u7528\u306b\u914d\u5217\u3092\u8fd4\u5374 */\n    vector<int> toVector() {\n        DoublyListNode *node = front;\n        vector<int> res(size());\n        for (int i = 0; i < res.size(); i++) {\n            res[i] = node->val;\n            node = node->next;\n        }\n        return res;\n    }\n};\n
        linkedlist_deque.java
        /* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9 */\nclass ListNode {\n    int val; // \u30ce\u30fc\u30c9\u5024\n    ListNode next; // \u5f8c\u7d9a\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    ListNode prev; // \u524d\u4efb\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n\n    ListNode(int val) {\n        this.val = val;\n        prev = next = null;\n    }\n}\n\n/* \u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u4e21\u7aef\u30ad\u30e5\u30fc\u30af\u30e9\u30b9 */\nclass LinkedListDeque {\n    private ListNode front, rear; // \u5148\u982d\u30ce\u30fc\u30c9 front\u3001\u672b\u5c3e\u30ce\u30fc\u30c9 rear\n    private int queSize = 0; // \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\n\n    public LinkedListDeque() {\n        front = rear = null;\n    }\n\n    /* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    public int size() {\n        return queSize;\n    }\n\n    /* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c */\n    private void push(int num, boolean isFront) {\n        ListNode node = new ListNode(num);\n        // \u30ea\u30b9\u30c8\u304c\u7a7a\u306e\u5834\u5408\u3001front \u3068 rear \u306e\u4e21\u65b9\u3092 node \u306b\u6307\u3059\n        if (isEmpty())\n            front = rear = node;\n        // \u5148\u982d\u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c\n        else if (isFront) {\n            // node \u3092\u30ea\u30b9\u30c8\u306e\u5148\u982d\u306b\u8ffd\u52a0\n            front.prev = node;\n            node.next = front;\n            front = node; // front \u3092\u66f4\u65b0\n        // \u672b\u5c3e\u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c\n        } else {\n            // node \u3092\u30ea\u30b9\u30c8\u306e\u672b\u5c3e\u306b\u8ffd\u52a0\n            rear.next = node;\n            node.prev = rear;\n            rear = node; // rear \u3092\u66f4\u65b0\n        }\n        queSize++; // \u9577\u3055\u3092\u66f4\u65b0\n    }\n\n    /* \u5148\u982d\u30a8\u30f3\u30ad\u30e5\u30fc */\n    public void pushFirst(int num) {\n        push(num, true);\n    }\n\n    /* \u672b\u5c3e\u30a8\u30f3\u30ad\u30e5\u30fc */\n    public void pushLast(int num) {\n        push(num, false);\n    }\n\n    /* \u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c */\n    private int pop(boolean isFront) {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        int val;\n        // \u5148\u982d\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\n        if (isFront) {\n            val = front.val; // \u4e00\u6642\u7684\u306b\u5148\u982d\u30ce\u30fc\u30c9\u5024\u3092\u4fdd\u5b58\n            // \u6b21\u306e\u30ce\u30fc\u30c9\u3092\u524a\u9664\n            ListNode fNext = front.next;\n            if (fNext != null) {\n                fNext.prev = null;\n                front.next = null;\n            }\n            front = fNext; // front \u3092\u66f4\u65b0\n        // \u672b\u5c3e\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\n        } else {\n            val = rear.val; // \u4e00\u6642\u7684\u306b\u672b\u5c3e\u30ce\u30fc\u30c9\u5024\u3092\u4fdd\u5b58\n            // \u524d\u306e\u30ce\u30fc\u30c9\u3092\u524a\u9664\n            ListNode rPrev = rear.prev;\n            if (rPrev != null) {\n                rPrev.next = null;\n                rear.prev = null;\n            }\n            rear = rPrev; // rear \u3092\u66f4\u65b0\n        }\n        queSize--; // \u9577\u3055\u3092\u66f4\u65b0\n        return val;\n    }\n\n    /* \u5148\u982d\u30c7\u30ad\u30e5\u30fc */\n    public int popFirst() {\n        return pop(true);\n    }\n\n    /* \u672b\u5c3e\u30c7\u30ad\u30e5\u30fc */\n    public int popLast() {\n        return pop(false);\n    }\n\n    /* \u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    public int peekFirst() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return front.val;\n    }\n\n    /* \u672b\u5c3e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    public int peekLast() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return rear.val;\n    }\n\n    /* \u914d\u5217\u3092\u8fd4\u3059 */\n    public int[] toArray() {\n        ListNode node = front;\n        int[] res = new int[size()];\n        for (int i = 0; i < res.length; i++) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_deque.cs
        [class]{ListNode}-[func]{}\n\n[class]{LinkedListDeque}-[func]{}\n
        linkedlist_deque.go
        [class]{linkedListDeque}-[func]{}\n
        linkedlist_deque.swift
        [class]{ListNode}-[func]{}\n\n[class]{LinkedListDeque}-[func]{}\n
        linkedlist_deque.js
        [class]{ListNode}-[func]{}\n\n[class]{LinkedListDeque}-[func]{}\n
        linkedlist_deque.ts
        [class]{ListNode}-[func]{}\n\n[class]{LinkedListDeque}-[func]{}\n
        linkedlist_deque.dart
        [class]{ListNode}-[func]{}\n\n[class]{LinkedListDeque}-[func]{}\n
        linkedlist_deque.rs
        [class]{ListNode}-[func]{}\n\n[class]{LinkedListDeque}-[func]{}\n
        linkedlist_deque.c
        [class]{DoublyListNode}-[func]{}\n\n[class]{LinkedListDeque}-[func]{}\n
        linkedlist_deque.kt
        [class]{ListNode}-[func]{}\n\n[class]{LinkedListDeque}-[func]{}\n
        linkedlist_deque.rb
        [class]{ListNode}-[func]{}\n\n[class]{LinkedListDeque}-[func]{}\n
        "},{"location":"chapter_stack_and_queue/deque/#2","title":"2. \u00a0 \u914d\u5217\u306b\u57fa\u3065\u304f\u5b9f\u88c5","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u914d\u5217\u3067\u30ad\u30e5\u30fc\u3092\u5b9f\u88c5\u3059\u308b\u306e\u3068\u540c\u69d8\u306b\u3001\u5faa\u74b0\u914d\u5217\u3092\u4f7f\u3063\u3066\u4e21\u7aef\u30ad\u30e5\u30fc\u3092\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002

        ArrayDequepushLast()pushFirst()popLast()popFirst()

        \u56f3 5-9 \u00a0 \u914d\u5217\u306b\u3088\u308b\u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u30a8\u30f3\u30ad\u30e5\u30fc\u3068\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\u306e\u5b9f\u88c5

        \u5b9f\u88c5\u3067\u306f\u300c\u524d\u7aef\u30a8\u30f3\u30ad\u30e5\u30fc\u300d\u3068\u300c\u5f8c\u7aef\u30c7\u30ad\u30e5\u30fc\u300d\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u8ffd\u52a0\u3059\u308b\u3060\u3051\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_deque.py
        class ArrayDeque:\n    \"\"\"\u5faa\u74b0\u914d\u5217\u30d9\u30fc\u30b9\u306e\u53cc\u7aef\u30ad\u30e5\u30fc\u30af\u30e9\u30b9\"\"\"\n\n    def __init__(self, capacity: int):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        self._nums: list[int] = [0] * capacity\n        self._front: int = 0\n        self._size: int = 0\n\n    def capacity(self) -> int:\n        \"\"\"\u53cc\u7aef\u30ad\u30e5\u30fc\u306e\u5bb9\u91cf\u3092\u53d6\u5f97\"\"\"\n        return len(self._nums)\n\n    def size(self) -> int:\n        \"\"\"\u53cc\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u53cc\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\"\"\"\n        return self._size == 0\n\n    def index(self, i: int) -> int:\n        \"\"\"\u5faa\u74b0\u914d\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97\"\"\"\n        # \u30e2\u30b8\u30e5\u30ed\u6f14\u7b97\u306b\u3088\u3063\u3066\u5faa\u74b0\u914d\u5217\u3092\u5b9f\u88c5\n        # i \u304c\u914d\u5217\u306e\u672b\u5c3e\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u5148\u982d\u306b\u623b\u308b\n        # i \u304c\u914d\u5217\u306e\u5148\u982d\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u672b\u5c3e\u306b\u623b\u308b\n        return (i + self.capacity()) % self.capacity()\n\n    def push_first(self, num: int):\n        \"\"\"\u524d\u7aef\u30a8\u30f3\u30ad\u30e5\u30fc\"\"\"\n        if self._size == self.capacity():\n            print(\"\u53cc\u7aef\u30ad\u30e5\u30fc\u304c\u6e80\u676f\u3067\u3059\")\n            return\n        # \u30d5\u30ed\u30f3\u30c8\u30dd\u30a4\u30f3\u30bf\u3092\u5de6\u306b1\u3064\u79fb\u52d5\n        # \u30e2\u30b8\u30e5\u30ed\u6f14\u7b97\u306b\u3088\u3063\u3066\u30d5\u30ed\u30f3\u30c8\u304c\u914d\u5217\u306e\u5148\u982d\u3092\u8d85\u3048\u3066\u672b\u5c3e\u306b\u623b\u308b\u3053\u3068\u3092\u5b9f\u88c5\n        self._front = self.index(self._front - 1)\n        # num \u3092\u524d\u7aef\u306b\u8ffd\u52a0\n        self._nums[self._front] = num\n        self._size += 1\n\n    def push_last(self, num: int):\n        \"\"\"\u5f8c\u7aef\u30a8\u30f3\u30ad\u30e5\u30fc\"\"\"\n        if self._size == self.capacity():\n            print(\"\u53cc\u7aef\u30ad\u30e5\u30fc\u304c\u6e80\u676f\u3067\u3059\")\n            return\n        # \u30ea\u30a2\u30dd\u30a4\u30f3\u30bf\u3092\u8a08\u7b97\u3001\u30ea\u30a2\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 + 1 \u3092\u6307\u3059\n        rear = self.index(self._front + self._size)\n        # num \u3092\u5f8c\u7aef\u306b\u8ffd\u52a0\n        self._nums[rear] = num\n        self._size += 1\n\n    def pop_first(self) -> int:\n        \"\"\"\u524d\u7aef\u30c7\u30ad\u30e5\u30fc\"\"\"\n        num = self.peek_first()\n        # \u30d5\u30ed\u30f3\u30c8\u30dd\u30a4\u30f3\u30bf\u30921\u3064\u5f8c\u308d\u306b\u79fb\u52d5\n        self._front = self.index(self._front + 1)\n        self._size -= 1\n        return num\n\n    def pop_last(self) -> int:\n        \"\"\"\u5f8c\u7aef\u30c7\u30ad\u30e5\u30fc\"\"\"\n        num = self.peek_last()\n        self._size -= 1\n        return num\n\n    def peek_first(self) -> int:\n        \"\"\"\u524d\u7aef\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Double-ended queue is empty\")\n        return self._nums[self._front]\n\n    def peek_last(self) -> int:\n        \"\"\"\u5f8c\u7aef\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Double-ended queue is empty\")\n        # \u5f8c\u7aef\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97\n        last = self.index(self._front + self._size - 1)\n        return self._nums[last]\n\n    def to_array(self) -> list[int]:\n        \"\"\"\u51fa\u529b\u7528\u306e\u914d\u5217\u3092\u8fd4\u3059\"\"\"\n        # \u6709\u52b9\u306a\u9577\u3055\u7bc4\u56f2\u5185\u306e\u8981\u7d20\u306e\u307f\u3092\u5909\u63db\n        res = []\n        for i in range(self._size):\n            res.append(self._nums[self.index(self._front + i)])\n        return res\n
        array_deque.cpp
        /* \u5faa\u74b0\u914d\u5217\u306b\u57fa\u3065\u304f\u4e21\u7aef\u30ad\u30e5\u30fc\u30af\u30e9\u30b9 */\nclass ArrayDeque {\n  private:\n    vector<int> nums; // \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u8981\u7d20\u3092\u683c\u7d0d\u3059\u308b\u914d\u5217\n    int front;        // \u5148\u982d\u30dd\u30a4\u30f3\u30bf\u3001\u5148\u982d\u8981\u7d20\u3092\u6307\u3059\n    int queSize;      // \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\n\n  public:\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    ArrayDeque(int capacity) {\n        nums.resize(capacity);\n        front = queSize = 0;\n    }\n\n    /* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u5bb9\u91cf\u3092\u53d6\u5f97 */\n    int capacity() {\n        return nums.size();\n    }\n\n    /* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    int size() {\n        return queSize;\n    }\n\n    /* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    bool isEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u5faa\u74b0\u914d\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97 */\n    int index(int i) {\n        // \u5270\u4f59\u6f14\u7b97\u3067\u5faa\u74b0\u914d\u5217\u3092\u5b9f\u73fe\n        // i\u304c\u914d\u5217\u306e\u672b\u5c3e\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u5148\u982d\u306b\u623b\u308b\n        // i\u304c\u914d\u5217\u306e\u5148\u982d\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u672b\u5c3e\u306b\u623b\u308b\n        return (i + capacity()) % capacity();\n    }\n\n    /* \u5148\u982d\u30a8\u30f3\u30ad\u30e5\u30fc */\n    void pushFirst(int num) {\n        if (queSize == capacity()) {\n            cout << \"Double-ended queue is full\" << endl;\n            return;\n        }\n        // \u5148\u982d\u30dd\u30a4\u30f3\u30bf\u30921\u3064\u5de6\u306b\u79fb\u52d5\n        // \u5270\u4f59\u6f14\u7b97\u3067front\u304c\u914d\u5217\u306e\u5148\u982d\u3092\u8d8a\u3048\u3066\u672b\u5c3e\u306b\u623b\u308b\u3053\u3068\u3092\u5b9f\u73fe\n        front = index(front - 1);\n        // num\u3092\u5148\u982d\u306b\u8ffd\u52a0\n        nums[front] = num;\n        queSize++;\n    }\n\n    /* \u672b\u5c3e\u30a8\u30f3\u30ad\u30e5\u30fc */\n    void pushLast(int num) {\n        if (queSize == capacity()) {\n            cout << \"Double-ended queue is full\" << endl;\n            return;\n        }\n        // \u672b\u5c3e\u30dd\u30a4\u30f3\u30bf\u3092\u8a08\u7b97\u3001\u672b\u5c3e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 + 1\u3092\u6307\u3059\n        int rear = index(front + queSize);\n        // num\u3092\u672b\u5c3e\u306b\u8ffd\u52a0\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u5148\u982d\u30c7\u30ad\u30e5\u30fc */\n    int popFirst() {\n        int num = peekFirst();\n        // \u5148\u982d\u30dd\u30a4\u30f3\u30bf\u30921\u3064\u5f8c\u308d\u306b\u79fb\u52d5\n        front = index(front + 1);\n        queSize--;\n        return num;\n    }\n\n    /* \u672b\u5c3e\u30c7\u30ad\u30e5\u30fc */\n    int popLast() {\n        int num = peekLast();\n        queSize--;\n        return num;\n    }\n\n    /* \u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    int peekFirst() {\n        if (isEmpty())\n            throw out_of_range(\"Double-ended queue is empty\");\n        return nums[front];\n    }\n\n    /* \u672b\u5c3e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    int peekLast() {\n        if (isEmpty())\n            throw out_of_range(\"Double-ended queue is empty\");\n        // \u672b\u5c3e\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97\n        int last = index(front + queSize - 1);\n        return nums[last];\n    }\n\n    /* \u5370\u5237\u7528\u306b\u914d\u5217\u3092\u8fd4\u5374 */\n    vector<int> toVector() {\n        // \u6709\u52b9\u306a\u9577\u3055\u7bc4\u56f2\u5185\u306e\u8981\u7d20\u306e\u307f\u3092\u5909\u63db\n        vector<int> res(queSize);\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[index(j)];\n        }\n        return res;\n    }\n};\n
        array_deque.java
        /* \u5faa\u74b0\u914d\u5217\u306b\u57fa\u3065\u304f\u4e21\u7aef\u30ad\u30e5\u30fc\u30af\u30e9\u30b9 */\nclass ArrayDeque {\n    private int[] nums; // \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u8981\u7d20\u3092\u683c\u7d0d\u3059\u308b\u914d\u5217\n    private int front; // \u5148\u982d\u30dd\u30a4\u30f3\u30bf\u3001\u5148\u982d\u8981\u7d20\u3092\u6307\u3059\n    private int queSize; // \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\n\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    public ArrayDeque(int capacity) {\n        this.nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u5bb9\u91cf\u3092\u53d6\u5f97 */\n    public int capacity() {\n        return nums.length;\n    }\n\n    /* \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    public int size() {\n        return queSize;\n    }\n\n    /* \u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    public boolean isEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u5faa\u74b0\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97 */\n    private int index(int i) {\n        // \u30e2\u30b8\u30e5\u30ed\u6f14\u7b97\u306b\u3088\u308a\u5faa\u74b0\u914d\u5217\u3092\u5b9f\u88c5\n        // i \u304c\u914d\u5217\u306e\u672b\u5c3e\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u5148\u982d\u306b\u623b\u308b\n        // i \u304c\u914d\u5217\u306e\u5148\u982d\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u672b\u5c3e\u306b\u623b\u308b\n        return (i + capacity()) % capacity();\n    }\n\n    /* \u5148\u982d\u30a8\u30f3\u30ad\u30e5\u30fc */\n    public void pushFirst(int num) {\n        if (queSize == capacity()) {\n            System.out.println(\"\u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u6e80\u676f\u3067\u3059\");\n            return;\n        }\n        // \u5148\u982d\u30dd\u30a4\u30f3\u30bf\u3092\u5de6\u306b\u79fb\u52d5\u3057\u3001\u5883\u754c\u3092\u8d8a\u3048\u308b\u5834\u5408\u306f\u914d\u5217\u306e\u672b\u5c3e\u306b\u56de\u308b\n        front = index(front - 1);\n        // \u5148\u982d\u306b num \u3092\u8ffd\u52a0\n        nums[front] = num;\n        queSize++;\n    }\n\n    /* \u672b\u5c3e\u30a8\u30f3\u30ad\u30e5\u30fc */\n    public void pushLast(int num) {\n        if (queSize == capacity()) {\n            System.out.println(\"\u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u6e80\u676f\u3067\u3059\");\n            return;\n        }\n        // \u672b\u5c3e\u30dd\u30a4\u30f3\u30bf\u3092\u8a08\u7b97\u3057\u3001\u672b\u5c3e\u306b\u8981\u7d20\u3092\u8ffd\u52a0\n        int rear = index(front + queSize);\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u5148\u982d\u30c7\u30ad\u30e5\u30fc */\n    public int popFirst() {\n        int num = peekFirst();\n        // \u5148\u982d\u30dd\u30a4\u30f3\u30bf\u3092\u53f3\u306b\u79fb\u52d5\n        front = index(front + 1);\n        queSize--;\n        return num;\n    }\n\n    /* \u672b\u5c3e\u30c7\u30ad\u30e5\u30fc */\n    public int popLast() {\n        int num = peekLast();\n        queSize--;\n        return num;\n    }\n\n    /* \u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    public int peekFirst() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return nums[front];\n    }\n\n    /* \u672b\u5c3e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    public int peekLast() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        // \u672b\u5c3e\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a08\u7b97\n        int last = index(front + queSize - 1);\n        return nums[last];\n    }\n\n    /* \u914d\u5217\u3092\u8fd4\u3059 */\n    public int[] toArray() {\n        // front \u304b\u3089\u958b\u59cb\u3057\u3066 queSize \u500b\u306e\u8981\u7d20\u306e\u307f\u3092\u30b3\u30d4\u30fc\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[index(j)];\n        }\n        return res;\n    }\n}\n
        array_deque.cs
        [class]{ArrayDeque}-[func]{}\n
        array_deque.go
        [class]{arrayDeque}-[func]{}\n
        array_deque.swift
        [class]{ArrayDeque}-[func]{}\n
        array_deque.js
        [class]{ArrayDeque}-[func]{}\n
        array_deque.ts
        [class]{ArrayDeque}-[func]{}\n
        array_deque.dart
        [class]{ArrayDeque}-[func]{}\n
        array_deque.rs
        [class]{ArrayDeque}-[func]{}\n
        array_deque.c
        [class]{ArrayDeque}-[func]{}\n
        array_deque.kt
        [class]{ArrayDeque}-[func]{}\n
        array_deque.rb
        [class]{ArrayDeque}-[func]{}\n
        "},{"location":"chapter_stack_and_queue/deque/#533","title":"5.3.3 \u00a0 \u4e21\u7aef\u30ad\u30e5\u30fc\u306e\u5fdc\u7528","text":"

        \u4e21\u7aef\u30ad\u30e5\u30fc\u306f\u30b9\u30bf\u30c3\u30af\u3068\u30ad\u30e5\u30fc\u306e\u4e21\u65b9\u306e\u30ed\u30b8\u30c3\u30af\u3092\u7d44\u307f\u5408\u308f\u305b\u3066\u3044\u308b\u305f\u3081\u3001\u305d\u308c\u305e\u308c\u306e\u3059\u3079\u3066\u306e\u30e6\u30fc\u30b9\u30b1\u30fc\u30b9\u3092\u5b9f\u88c5\u3067\u304d\u3001\u3088\u308a\u5927\u304d\u306a\u67d4\u8edf\u6027\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002

        \u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u306e\u300c\u5143\u306b\u623b\u3059\u300d\u6a5f\u80fd\u306f\u901a\u5e38\u30b9\u30bf\u30c3\u30af\u3092\u4f7f\u3063\u3066\u5b9f\u88c5\u3055\u308c\u308b\u3053\u3068\u3092\u77e5\u3063\u3066\u3044\u307e\u3059\uff1a\u30b7\u30b9\u30c6\u30e0\u306f\u5404\u5909\u66f4\u64cd\u4f5c\u3092\u30b9\u30bf\u30c3\u30af\u306bpush\u3057\u3001\u6b21\u306bpop\u3057\u3066\u5143\u306b\u623b\u3059\u3053\u3068\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u30b7\u30b9\u30c6\u30e0\u30ea\u30bd\u30fc\u30b9\u306e\u5236\u9650\u3092\u8003\u616e\u3057\u3066\u3001\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u306f\u5143\u306b\u623b\u3059\u30b9\u30c6\u30c3\u30d7\u306e\u6570\u3092\u5236\u9650\u3059\u308b\u3053\u3068\u304c\u3088\u304f\u3042\u308a\u307e\u3059\uff08\u4f8b\u3048\u3070\u3001\u6700\u5f8c\u306e50\u30b9\u30c6\u30c3\u30d7\u306e\u307f\u3092\u8a31\u53ef\uff09\u3002\u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u304c50\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u306f\u30b9\u30bf\u30c3\u30af\u306e\u5e95\u90e8\uff08\u30ad\u30e5\u30fc\u306e\u524d\u7aef\uff09\u3067\u524a\u9664\u64cd\u4f5c\u3092\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u901a\u5e38\u306e\u30b9\u30bf\u30c3\u30af\u3067\u306f\u3053\u306e\u6a5f\u80fd\u3092\u5b9f\u884c\u3067\u304d\u306a\u3044\u305f\u3081\u3001\u4e21\u7aef\u30ad\u30e5\u30fc\u304c\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059\u3002\u300c\u5143\u306b\u623b\u3059\u300d\u306e\u30b3\u30a2\u30ed\u30b8\u30c3\u30af\u306f\u4f9d\u7136\u3068\u3057\u3066\u30b9\u30bf\u30c3\u30af\u306e\u5f8c\u5165\u308c\u5148\u51fa\u3057\u539f\u5247\u306b\u5f93\u3044\u307e\u3059\u304c\u3001\u4e21\u7aef\u30ad\u30e5\u30fc\u306f\u3088\u308a\u67d4\u8edf\u306b\u3044\u304f\u3064\u304b\u306e\u8ffd\u52a0\u30ed\u30b8\u30c3\u30af\u3092\u5b9f\u88c5\u3067\u304d\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        "},{"location":"chapter_stack_and_queue/queue/","title":"5.2 \u00a0 \u30ad\u30e5\u30fc","text":"

        \u30ad\u30e5\u30fc\u306f\u3001\u5148\u5165\u5148\u51fa\uff08FIFO\uff09\u30eb\u30fc\u30eb\u306b\u5f93\u3046\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3059\u3002\u540d\u524d\u304c\u793a\u3059\u3088\u3046\u306b\u3001\u30ad\u30e5\u30fc\u306f\u884c\u5217\u306e\u73fe\u8c61\u3092\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\u3057\u3001\u65b0\u53c2\u8005\u306f\u5217\u306e\u5f8c\u308d\u306b\u4e26\u3073\u3001\u524d\u306e\u4eba\u304c\u6700\u521d\u306b\u5217\u3092\u96e2\u308c\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30ad\u30e5\u30fc\u306e\u524d\u9762\u3092\u300c\u30d8\u30c3\u30c9\u300d\u3001\u5f8c\u9762\u3092\u300c\u30c6\u30fc\u30eb\u300d\u3068\u547c\u3073\u307e\u3059\u3002\u30ad\u30e5\u30fc\u306e\u5f8c\u308d\u306b\u8981\u7d20\u3092\u8ffd\u52a0\u3059\u308b\u64cd\u4f5c\u3092\u300c\u30a8\u30f3\u30ad\u30e5\u30fc\u300d\u3001\u524d\u304b\u3089\u8981\u7d20\u3092\u524a\u9664\u3059\u308b\u64cd\u4f5c\u3092\u300c\u30c7\u30ad\u30e5\u30fc\u300d\u3068\u547c\u3073\u307e\u3059\u3002

        \u56f3 5-4 \u00a0 \u30ad\u30e5\u30fc\u306e\u5148\u5165\u5148\u51fa\u30eb\u30fc\u30eb

        "},{"location":"chapter_stack_and_queue/queue/#521","title":"5.2.1 \u00a0 \u30ad\u30e5\u30fc\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c","text":"

        \u30ad\u30e5\u30fc\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c\u3092\u4e0b\u8868\u306b\u793a\u3057\u307e\u3059\u3002\u30e1\u30bd\u30c3\u30c9\u540d\u306f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u3088\u3063\u3066\u7570\u306a\u308b\u5834\u5408\u304c\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3053\u3053\u3067\u306f\u3001\u30b9\u30bf\u30c3\u30af\u3067\u4f7f\u7528\u3057\u305f\u306e\u3068\u540c\u3058\u547d\u540d\u898f\u5247\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        \u8868 5-2 \u00a0 \u30ad\u30e5\u30fc\u64cd\u4f5c\u306e\u52b9\u7387

        \u30e1\u30bd\u30c3\u30c9\u540d \u8aac\u660e \u6642\u9593\u8a08\u7b97\u91cf push() \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\u3001\u30c6\u30fc\u30eb\u306b\u8ffd\u52a0 \\(O(1)\\) pop() \u30d8\u30c3\u30c9\u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc \\(O(1)\\) peek() \u30d8\u30c3\u30c9\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 \\(O(1)\\)

        \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u7528\u610f\u3055\u308c\u3066\u3044\u308b\u30ad\u30e5\u30fc\u30af\u30e9\u30b9\u3092\u76f4\u63a5\u4f7f\u7528\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin queue.py
        from collections import deque\n\n# \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316\n# Python\u3067\u306f\u3001\u4e00\u822c\u7684\u306bdeque\u30af\u30e9\u30b9\u3092\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\u3057\u307e\u3059\n# queue.Queue()\u306f\u7d14\u7c8b\u306a\u30ad\u30e5\u30fc\u30af\u30e9\u30b9\u3067\u3059\u304c\u3001\u4f7f\u3044\u306b\u304f\u3044\u305f\u3081\u63a8\u5968\u3055\u308c\u307e\u305b\u3093\nque: deque[int] = deque()\n\n# \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\nque.append(1)\nque.append(3)\nque.append(2)\nque.append(5)\nque.append(4)\n\n# \u6700\u521d\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\nfront: int = que[0]\n\n# \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc\npop: int = que.popleft()\n\n# \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97\nsize: int = len(que)\n\n# \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af\nis_empty: bool = len(que) == 0\n
        queue.cpp
        /* \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\nqueue<int> queue;\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u6700\u521d\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint front = queue.front();\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\nqueue.pop();\n\n/* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = queue.size();\n\n/* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nbool empty = queue.empty();\n
        queue.java
        /* \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\nQueue<Integer> queue = new LinkedList<>();\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\nqueue.offer(1);\nqueue.offer(3);\nqueue.offer(2);\nqueue.offer(5);\nqueue.offer(4);\n\n/* \u6700\u521d\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint peek = queue.peek();\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\nint pop = queue.poll();\n\n/* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = queue.size();\n\n/* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nboolean isEmpty = queue.isEmpty();\n
        queue.cs
        /* \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\nQueue<int> queue = new();\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\nqueue.Enqueue(1);\nqueue.Enqueue(3);\nqueue.Enqueue(2);\nqueue.Enqueue(5);\nqueue.Enqueue(4);\n\n/* \u6700\u521d\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint peek = queue.Peek();\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\nint pop = queue.Dequeue();\n\n/* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = queue.Count;\n\n/* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nbool isEmpty = queue.Count == 0;\n
        queue_test.go
        /* \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// Go\u3067\u306f\u3001list\u3092\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\nqueue := list.New()\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\nqueue.PushBack(1)\nqueue.PushBack(3)\nqueue.PushBack(2)\nqueue.PushBack(5)\nqueue.PushBack(4)\n\n/* \u6700\u521d\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\npeek := queue.Front()\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\npop := queue.Front()\nqueue.Remove(pop)\n\n/* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nsize := queue.Len()\n\n/* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nisEmpty := queue.Len() == 0\n
        queue.swift
        /* \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// Swift\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30ad\u30e5\u30fc\u30af\u30e9\u30b9\u304c\u306a\u3044\u305f\u3081\u3001Array\u3092\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\nvar queue: [Int] = []\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\nqueue.append(1)\nqueue.append(3)\nqueue.append(2)\nqueue.append(5)\nqueue.append(4)\n\n/* \u6700\u521d\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nlet peek = queue.first!\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\n// \u914d\u5217\u306a\u306e\u3067\u3001removeFirst\u306e\u8a08\u7b97\u91cf\u306fO(n)\nlet pool = queue.removeFirst()\n\n/* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nlet size = queue.count\n\n/* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nlet isEmpty = queue.isEmpty\n
        queue.js
        /* \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// JavaScript\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30ad\u30e5\u30fc\u304c\u306a\u3044\u305f\u3081\u3001Array\u3092\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\nconst queue = [];\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u6700\u521d\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nconst peek = queue[0];\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\n// \u57fa\u790e\u69cb\u9020\u304c\u914d\u5217\u306a\u306e\u3067\u3001shift()\u30e1\u30bd\u30c3\u30c9\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306fO(n)\nconst pop = queue.shift();\n\n/* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nconst size = queue.length;\n\n/* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nconst empty = queue.length === 0;\n
        queue.ts
        /* \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// TypeScript\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30ad\u30e5\u30fc\u304c\u306a\u3044\u305f\u3081\u3001Array\u3092\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\nconst queue: number[] = [];\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u6700\u521d\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nconst peek = queue[0];\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\n// \u57fa\u790e\u69cb\u9020\u304c\u914d\u5217\u306a\u306e\u3067\u3001shift()\u30e1\u30bd\u30c3\u30c9\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306fO(n)\nconst pop = queue.shift();\n\n/* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nconst size = queue.length;\n\n/* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nconst empty = queue.length === 0;\n
        queue.dart
        /* \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// Dart\u306eQueue\u30af\u30e9\u30b9\u306f\u53cc\u65b9\u5411\u30ad\u30e5\u30fc\u3067\u3059\u304c\u3001\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\u3067\u304d\u307e\u3059\nQueue<int> queue = Queue();\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\nqueue.add(1);\nqueue.add(3);\nqueue.add(2);\nqueue.add(5);\nqueue.add(4);\n\n/* \u6700\u521d\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint peek = queue.first;\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\nint pop = queue.removeFirst();\n\n/* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = queue.length;\n\n/* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nbool isEmpty = queue.isEmpty;\n
        queue.rs
        /* \u53cc\u65b9\u5411\u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316 */\n// Rust\u3067\u306f\u3001\u53cc\u65b9\u5411\u30ad\u30e5\u30fc\u3092\u901a\u5e38\u306e\u30ad\u30e5\u30fc\u3068\u3057\u3066\u4f7f\u7528\nlet mut deque: VecDeque<u32> = VecDeque::new();\n\n/* \u8981\u7d20\u3092\u30a8\u30f3\u30ad\u30e5\u30fc */\ndeque.push_back(1);\ndeque.push_back(3);\ndeque.push_back(2);\ndeque.push_back(5);\ndeque.push_back(4);\n\n/* \u6700\u521d\u306e\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nif let Some(front) = deque.front() {\n}\n\n/* \u8981\u7d20\u3092\u30c7\u30ad\u30e5\u30fc */\nif let Some(pop) = deque.pop_front() {\n}\n\n/* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nlet size = deque.len();\n\n/* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nlet is_empty = deque.is_empty();\n
        queue.c
        // C\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30ad\u30e5\u30fc\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        queue.kt
        \n
        "},{"location":"chapter_stack_and_queue/queue/#522","title":"5.2.2 \u00a0 \u30ad\u30e5\u30fc\u306e\u5b9f\u88c5","text":"

        \u30ad\u30e5\u30fc\u3092\u5b9f\u88c5\u3059\u308b\u306b\u306f\u3001\u4e00\u65b9\u306e\u7aef\u3067\u8981\u7d20\u3092\u8ffd\u52a0\u3057\u3001\u3082\u3046\u4e00\u65b9\u306e\u7aef\u3067\u8981\u7d20\u3092\u524a\u9664\u3067\u304d\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u304c\u5fc5\u8981\u3067\u3059\u3002\u9023\u7d50\u30ea\u30b9\u30c8\u3068\u914d\u5217\u306e\u4e21\u65b9\u304c\u3053\u306e\u8981\u4ef6\u3092\u6e80\u305f\u3057\u307e\u3059\u3002

        "},{"location":"chapter_stack_and_queue/queue/#1","title":"1. \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u30d9\u30fc\u30b9\u306e\u5b9f\u88c5","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u300c\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u300d\u3068\u300c\u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u300d\u3092\u305d\u308c\u305e\u308c\u30ad\u30e5\u30fc\u306e\u300c\u30d5\u30ed\u30f3\u30c8\u300d\u3068\u300c\u30ea\u30a2\u300d\u3068\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u30ce\u30fc\u30c9\u306f\u5f8c\u308d\u3067\u306e\u307f\u8ffd\u52a0\u3067\u304d\u3001\u524d\u3067\u306e\u307f\u524a\u9664\u3067\u304d\u308b\u3088\u3046\u306b\u898f\u5b9a\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        LinkedListQueuepush()pop()

        \u56f3 5-5 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u306b\u3088\u308b\u30ad\u30e5\u30fc\u5b9f\u88c5\u306e\u30a8\u30f3\u30ad\u30e5\u30fc\u3068\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c

        \u4ee5\u4e0b\u306f\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u30ad\u30e5\u30fc\u3092\u5b9f\u88c5\u3059\u308b\u30b3\u30fc\u30c9\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linkedlist_queue.py
        class LinkedListQueue:\n    \"\"\"\u9023\u7d50\u30ea\u30b9\u30c8\u30d9\u30fc\u30b9\u306e\u30ad\u30e5\u30fc\u30af\u30e9\u30b9\"\"\"\n\n    def __init__(self):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        self._front: ListNode | None = None  # \u30d8\u30c3\u30c9\u30ce\u30fc\u30c9 front\n        self._rear: ListNode | None = None  # \u30c6\u30fc\u30eb\u30ce\u30fc\u30c9 rear\n        self._size: int = 0\n\n    def size(self) -> int:\n        \"\"\"\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\"\"\"\n        return self._size == 0\n\n    def push(self, num: int):\n        \"\"\"\u30a8\u30f3\u30ad\u30e5\u30fc\"\"\"\n        # \u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u306e\u5f8c\u308d\u306b num \u3092\u8ffd\u52a0\n        node = ListNode(num)\n        # \u30ad\u30e5\u30fc\u304c\u7a7a\u306e\u5834\u5408\u3001\u30d8\u30c3\u30c9\u3068\u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u306e\u4e21\u65b9\u3092\u305d\u306e\u30ce\u30fc\u30c9\u306b\u5411\u3051\u308b\n        if self._front is None:\n            self._front = node\n            self._rear = node\n        # \u30ad\u30e5\u30fc\u304c\u7a7a\u3067\u306a\u3044\u5834\u5408\u3001\u305d\u306e\u30ce\u30fc\u30c9\u3092\u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u306e\u5f8c\u308d\u306b\u8ffd\u52a0\n        else:\n            self._rear.next = node\n            self._rear = node\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"\u30c7\u30ad\u30e5\u30fc\"\"\"\n        num = self.peek()\n        # \u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u3092\u524a\u9664\n        self._front = self._front.next\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"\u30d5\u30ed\u30f3\u30c8\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Queue is empty\")\n        return self._front.val\n\n    def to_list(self) -> list[int]:\n        \"\"\"\u51fa\u529b\u7528\u306e\u30ea\u30b9\u30c8\u306b\u5909\u63db\"\"\"\n        queue = []\n        temp = self._front\n        while temp:\n            queue.append(temp.val)\n            temp = temp.next\n        return queue\n
        linkedlist_queue.cpp
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u30ad\u30e5\u30fc\u30af\u30e9\u30b9 */\nclass LinkedListQueue {\n  private:\n    ListNode *front, *rear; // \u5148\u982d\u30ce\u30fc\u30c9front\u3001\u672b\u5c3e\u30ce\u30fc\u30c9rear\n    int queSize;\n\n  public:\n    LinkedListQueue() {\n        front = nullptr;\n        rear = nullptr;\n        queSize = 0;\n    }\n\n    ~LinkedListQueue() {\n        // \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8d70\u67fb\u3001\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3001\u30e1\u30e2\u30ea\u3092\u89e3\u653e\n        freeMemoryLinkedList(front);\n    }\n\n    /* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    int size() {\n        return queSize;\n    }\n\n    /* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    bool isEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u30a8\u30f3\u30ad\u30e5\u30fc */\n    void push(int num) {\n        // \u672b\u5c3e\u30ce\u30fc\u30c9\u306e\u5f8c\u308d\u306bnum\u3092\u8ffd\u52a0\n        ListNode *node = new ListNode(num);\n        // \u30ad\u30e5\u30fc\u304c\u7a7a\u306e\u5834\u5408\u3001\u5148\u982d\u3068\u672b\u5c3e\u30ce\u30fc\u30c9\u306e\u4e21\u65b9\u3092\u305d\u306e\u30ce\u30fc\u30c9\u306b\u5411\u3051\u308b\n        if (front == nullptr) {\n            front = node;\n            rear = node;\n        }\n        // \u30ad\u30e5\u30fc\u304c\u7a7a\u3067\u306a\u3044\u5834\u5408\u3001\u305d\u306e\u30ce\u30fc\u30c9\u3092\u672b\u5c3e\u30ce\u30fc\u30c9\u306e\u5f8c\u308d\u306b\u8ffd\u52a0\n        else {\n            rear->next = node;\n            rear = node;\n        }\n        queSize++;\n    }\n\n    /* \u30c7\u30ad\u30e5\u30fc */\n    int pop() {\n        int num = peek();\n        // \u5148\u982d\u30ce\u30fc\u30c9\u3092\u524a\u9664\n        ListNode *tmp = front;\n        front = front->next;\n        // \u30e1\u30e2\u30ea\u3092\u89e3\u653e\n        delete tmp;\n        queSize--;\n        return num;\n    }\n\n    /* \u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    int peek() {\n        if (size() == 0)\n            throw out_of_range(\"Queue is empty\");\n        return front->val;\n    }\n\n    /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092Vector\u306b\u5909\u63db\u3057\u3066\u8fd4\u5374 */\n    vector<int> toVector() {\n        ListNode *node = front;\n        vector<int> res(size());\n        for (int i = 0; i < res.size(); i++) {\n            res[i] = node->val;\n            node = node->next;\n        }\n        return res;\n    }\n};\n
        linkedlist_queue.java
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u30ad\u30e5\u30fc\u30af\u30e9\u30b9 */\nclass LinkedListQueue {\n    private ListNode front, rear; // \u5148\u982d\u30ce\u30fc\u30c9 front\u3001\u672b\u5c3e\u30ce\u30fc\u30c9 rear\n    private int queSize = 0;\n\n    public LinkedListQueue() {\n        front = null;\n        rear = null;\n    }\n\n    /* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    public int size() {\n        return queSize;\n    }\n\n    /* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u30a8\u30f3\u30ad\u30e5\u30fc */\n    public void push(int num) {\n        // \u672b\u5c3e\u30ce\u30fc\u30c9\u306e\u5f8c\u308d\u306b num \u3092\u8ffd\u52a0\n        ListNode node = new ListNode(num);\n        // \u30ad\u30e5\u30fc\u304c\u7a7a\u306e\u5834\u5408\u3001\u5148\u982d\u3068\u672b\u5c3e\u30ce\u30fc\u30c9\u306e\u4e21\u65b9\u3092\u305d\u306e\u30ce\u30fc\u30c9\u306b\u30dd\u30a4\u30f3\u30c8\n        if (front == null) {\n            front = node;\n            rear = node;\n        // \u30ad\u30e5\u30fc\u304c\u7a7a\u3067\u306a\u3044\u5834\u5408\u3001\u305d\u306e\u30ce\u30fc\u30c9\u3092\u672b\u5c3e\u30ce\u30fc\u30c9\u306e\u5f8c\u308d\u306b\u8ffd\u52a0\n        } else {\n            rear.next = node;\n            rear = node;\n        }\n        queSize++;\n    }\n\n    /* \u30c7\u30ad\u30e5\u30fc */\n    public int pop() {\n        int num = peek();\n        // \u5148\u982d\u30ce\u30fc\u30c9\u3092\u524a\u9664\n        front = front.next;\n        queSize--;\n        return num;\n    }\n\n    /* \u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return front.val;\n    }\n\n    /* \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u914d\u5217\u306b\u5909\u63db\u3057\u3066\u8fd4\u3059 */\n    public int[] toArray() {\n        ListNode node = front;\n        int[] res = new int[size()];\n        for (int i = 0; i < res.length; i++) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_queue.cs
        [class]{LinkedListQueue}-[func]{}\n
        linkedlist_queue.go
        [class]{linkedListQueue}-[func]{}\n
        linkedlist_queue.swift
        [class]{LinkedListQueue}-[func]{}\n
        linkedlist_queue.js
        [class]{LinkedListQueue}-[func]{}\n
        linkedlist_queue.ts
        [class]{LinkedListQueue}-[func]{}\n
        linkedlist_queue.dart
        [class]{LinkedListQueue}-[func]{}\n
        linkedlist_queue.rs
        [class]{LinkedListQueue}-[func]{}\n
        linkedlist_queue.c
        [class]{LinkedListQueue}-[func]{}\n
        linkedlist_queue.kt
        [class]{LinkedListQueue}-[func]{}\n
        linkedlist_queue.rb
        [class]{LinkedListQueue}-[func]{}\n
        "},{"location":"chapter_stack_and_queue/queue/#2","title":"2. \u00a0 \u914d\u5217\u30d9\u30fc\u30b9\u306e\u5b9f\u88c5","text":"

        \u914d\u5217\u306e\u6700\u521d\u306e\u8981\u7d20\u3092\u524a\u9664\u3059\u308b\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u3067\u3001\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\u304c\u975e\u52b9\u7387\u306b\u306a\u308a\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u3053\u306e\u554f\u984c\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5de7\u5999\u306b\u56de\u907f\u3067\u304d\u307e\u3059\u3002

        \u5909\u6570front\u3092\u4f7f\u7528\u3057\u3066\u30d5\u30ed\u30f3\u30c8\u8981\u7d20\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u793a\u3057\u3001\u5909\u6570size\u3092\u7dad\u6301\u3057\u3066\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u8a18\u9332\u3057\u307e\u3059\u3002rear = front + size\u3092\u5b9a\u7fa9\u3057\u3001\u3053\u308c\u306f\u30c6\u30fc\u30eb\u8981\u7d20\u306e\u76f4\u5f8c\u306e\u4f4d\u7f6e\u3092\u6307\u3057\u307e\u3059\u3002

        \u3053\u306e\u8a2d\u8a08\u306b\u3088\u308a\u3001\u914d\u5217\u5185\u306e\u8981\u7d20\u306e\u6709\u52b9\u306a\u9593\u9694\u306f[front, rear - 1]\u3067\u3059\u3002\u5404\u64cd\u4f5c\u306e\u5b9f\u88c5\u65b9\u6cd5\u3092\u4e0b\u56f3\u306b\u793a\u3057\u307e\u3059\u3002

        • \u30a8\u30f3\u30ad\u30e5\u30fc\u64cd\u4f5c\uff1a\u5165\u529b\u8981\u7d20\u3092rear\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5272\u308a\u5f53\u3066\u3001size\u30921\u5897\u52a0\u3055\u305b\u307e\u3059\u3002
        • \u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\uff1a\u5358\u306bfront\u30921\u5897\u52a0\u3055\u305b\u3001size\u30921\u6e1b\u5c11\u3055\u305b\u307e\u3059\u3002

        \u30a8\u30f3\u30ad\u30e5\u30fc\u3068\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\u306f\u4e21\u65b9\u3068\u3082\u5358\u4e00\u306e\u64cd\u4f5c\u306e\u307f\u3092\u5fc5\u8981\u3068\u3057\u3001\u305d\u308c\u305e\u308c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(1)\\)\u3067\u3059\u3002

        ArrayQueuepush()pop()

        \u56f3 5-6 \u00a0 \u914d\u5217\u306b\u3088\u308b\u30ad\u30e5\u30fc\u5b9f\u88c5\u306e\u30a8\u30f3\u30ad\u30e5\u30fc\u3068\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c

        \u554f\u984c\u306b\u6c17\u3065\u304f\u304b\u3082\u3057\u308c\u307e\u305b\u3093\uff1a\u30a8\u30f3\u30ad\u30e5\u30fc\u3068\u30c7\u30ad\u30e5\u30fc\u64cd\u4f5c\u304c\u7d99\u7d9a\u7684\u306b\u5b9f\u884c\u3055\u308c\u308b\u3068\u3001front\u3068rear\u306e\u4e21\u65b9\u304c\u53f3\u306b\u79fb\u52d5\u3057\u3001\u6700\u7d42\u7684\u306b\u914d\u5217\u306e\u672b\u5c3e\u306b\u5230\u9054\u3057\u3066\u305d\u308c\u4ee5\u4e0a\u79fb\u52d5\u3067\u304d\u306a\u304f\u306a\u308a\u307e\u3059\u3002\u3053\u308c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3001\u914d\u5217\u3092\u300c\u5faa\u74b0\u914d\u5217\u300d\u3068\u3057\u3066\u6271\u3044\u3001\u914d\u5217\u306e\u672b\u5c3e\u3092\u5148\u982d\u306b\u63a5\u7d9a\u3057\u307e\u3059\u3002

        \u5faa\u74b0\u914d\u5217\u3067\u306f\u3001front\u307e\u305f\u306frear\u304c\u672b\u5c3e\u306b\u5230\u9054\u3059\u308b\u3068\u3001\u914d\u5217\u306e\u5148\u982d\u306b\u30eb\u30fc\u30d7\u30d0\u30c3\u30af\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u5faa\u74b0\u30d1\u30bf\u30fc\u30f3\u306f\u3001\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u306b\u793a\u3059\u3088\u3046\u306b\u300c\u5270\u4f59\u6f14\u7b97\u300d\u3067\u5b9f\u73fe\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_queue.py
        class ArrayQueue:\n    \"\"\"\u5faa\u74b0\u914d\u5217\u30d9\u30fc\u30b9\u306e\u30ad\u30e5\u30fc\u30af\u30e9\u30b9\"\"\"\n\n    def __init__(self, size: int):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        self._nums: list[int] = [0] * size  # \u30ad\u30e5\u30fc\u8981\u7d20\u3092\u683c\u7d0d\u3059\u308b\u914d\u5217\n        self._front: int = 0  # \u30d5\u30ed\u30f3\u30c8\u30dd\u30a4\u30f3\u30bf\u3001\u30d5\u30ed\u30f3\u30c8\u8981\u7d20\u3092\u6307\u3059\n        self._size: int = 0  # \u30ad\u30e5\u30fc\u306e\u9577\u3055\n\n    def capacity(self) -> int:\n        \"\"\"\u30ad\u30e5\u30fc\u306e\u5bb9\u91cf\u3092\u53d6\u5f97\"\"\"\n        return len(self._nums)\n\n    def size(self) -> int:\n        \"\"\"\u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\"\"\"\n        return self._size == 0\n\n    def push(self, num: int):\n        \"\"\"\u30a8\u30f3\u30ad\u30e5\u30fc\"\"\"\n        if self._size == self.capacity():\n            raise IndexError(\"Queue is full\")\n        # \u30ea\u30a2\u30dd\u30a4\u30f3\u30bf\u3092\u8a08\u7b97\u3001\u30ea\u30a2\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 + 1 \u3092\u6307\u3059\n        # \u30e2\u30b8\u30e5\u30ed\u6f14\u7b97\u3092\u4f7f\u7528\u3057\u3066\u30ea\u30a2\u30dd\u30a4\u30f3\u30bf\u3092\u914d\u5217\u306e\u672b\u5c3e\u304b\u3089\u5148\u982d\u306b\u623b\u3059\n        rear: int = (self._front + self._size) % self.capacity()\n        # num \u3092\u30ea\u30a2\u306b\u8ffd\u52a0\n        self._nums[rear] = num\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"\u30c7\u30ad\u30e5\u30fc\"\"\"\n        num: int = self.peek()\n        # \u30d5\u30ed\u30f3\u30c8\u30dd\u30a4\u30f3\u30bf\u30921\u3064\u5f8c\u308d\u306b\u79fb\u52d5\u3001\u672b\u5c3e\u3092\u8d85\u3048\u305f\u5834\u5408\u306f\u914d\u5217\u306e\u5148\u982d\u306b\u623b\u308b\n        self._front = (self._front + 1) % self.capacity()\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"\u30d5\u30ed\u30f3\u30c8\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Queue is empty\")\n        return self._nums[self._front]\n\n    def to_list(self) -> list[int]:\n        \"\"\"\u51fa\u529b\u7528\u306e\u914d\u5217\u3092\u8fd4\u3059\"\"\"\n        res = [0] * self.size()\n        j: int = self._front\n        for i in range(self.size()):\n            res[i] = self._nums[(j % self.capacity())]\n            j += 1\n        return res\n
        array_queue.cpp
        /* \u5faa\u74b0\u914d\u5217\u306b\u57fa\u3065\u304f\u30ad\u30e5\u30fc\u30af\u30e9\u30b9 */\nclass ArrayQueue {\n  private:\n    int *nums;       // \u30ad\u30e5\u30fc\u8981\u7d20\u3092\u683c\u7d0d\u3059\u308b\u914d\u5217\n    int front;       // \u5148\u982d\u30dd\u30a4\u30f3\u30bf\u3001\u5148\u982d\u8981\u7d20\u3092\u6307\u3059\n    int queSize;     // \u30ad\u30e5\u30fc\u306e\u9577\u3055\n    int queCapacity; // \u30ad\u30e5\u30fc\u306e\u5bb9\u91cf\n\n  public:\n    ArrayQueue(int capacity) {\n        // \u914d\u5217\u3092\u521d\u671f\u5316\n        nums = new int[capacity];\n        queCapacity = capacity;\n        front = queSize = 0;\n    }\n\n    ~ArrayQueue() {\n        delete[] nums;\n    }\n\n    /* \u30ad\u30e5\u30fc\u306e\u5bb9\u91cf\u3092\u53d6\u5f97 */\n    int capacity() {\n        return queCapacity;\n    }\n\n    /* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    int size() {\n        return queSize;\n    }\n\n    /* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    bool isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u30a8\u30f3\u30ad\u30e5\u30fc */\n    void push(int num) {\n        if (queSize == queCapacity) {\n            cout << \"Queue is full\" << endl;\n            return;\n        }\n        // \u672b\u5c3e\u30dd\u30a4\u30f3\u30bf\u3092\u8a08\u7b97\u3001\u672b\u5c3e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 + 1\u3092\u6307\u3059\n        // \u5270\u4f59\u6f14\u7b97\u3092\u4f7f\u7528\u3057\u3066\u672b\u5c3e\u30dd\u30a4\u30f3\u30bf\u304c\u914d\u5217\u306e\u672b\u5c3e\u304b\u3089\u5148\u982d\u306b\u623b\u308b\u3088\u3046\u306b\u30e9\u30c3\u30d7\n        int rear = (front + queSize) % queCapacity;\n        // num\u3092\u672b\u5c3e\u306b\u8ffd\u52a0\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u30c7\u30ad\u30e5\u30fc */\n    int pop() {\n        int num = peek();\n        // \u5148\u982d\u30dd\u30a4\u30f3\u30bf\u30921\u3064\u5f8c\u308d\u306b\u79fb\u52d5\u3001\u672b\u5c3e\u3092\u8d85\u3048\u305f\u5834\u5408\u306f\u914d\u5217\u306e\u5148\u982d\u306b\u623b\u308b\n        front = (front + 1) % queCapacity;\n        queSize--;\n        return num;\n    }\n\n    /* \u5148\u982d\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    int peek() {\n        if (isEmpty())\n            throw out_of_range(\"Queue is empty\");\n        return nums[front];\n    }\n\n    /* \u914d\u5217\u3092Vector\u306b\u5909\u63db\u3057\u3066\u8fd4\u5374 */\n    vector<int> toVector() {\n        // \u6709\u52b9\u306a\u9577\u3055\u7bc4\u56f2\u5185\u306e\u8981\u7d20\u306e\u307f\u3092\u5909\u63db\n        vector<int> arr(queSize);\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            arr[i] = nums[j % queCapacity];\n        }\n        return arr;\n    }\n};\n
        array_queue.java
        /* \u914d\u5217\u306b\u57fa\u3065\u304f\u30ad\u30e5\u30fc\u30af\u30e9\u30b9 */\nclass ArrayQueue {\n    private int[] nums; // \u8981\u7d20\u3092\u683c\u7d0d\u3059\u308b\u914d\u5217\n    private int front; // \u30ad\u30e5\u30fc\u30d8\u30c3\u30c9\u30dd\u30a4\u30f3\u30bf\u3001\u6700\u521d\u306e\u8981\u7d20\u3092\u6307\u3059\n    private int queSize; // \u30ad\u30e5\u30fc\u306e\u9577\u3055\n\n    public ArrayQueue(int capacity) {\n        nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* \u30ad\u30e5\u30fc\u306e\u5bb9\u91cf\u3092\u53d6\u5f97 */\n    public int capacity() {\n        return nums.length;\n    }\n\n    /* \u30ad\u30e5\u30fc\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    public int size() {\n        return queSize;\n    }\n\n    /* \u30ad\u30e5\u30fc\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    public boolean isEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u30a8\u30f3\u30ad\u30e5\u30fc */\n    public void push(int num) {\n        if (queSize == capacity()) {\n            System.out.println(\"\u30ad\u30e5\u30fc\u304c\u6e80\u676f\u3067\u3059\");\n            return;\n        }\n        // \u30ea\u30a2\u30dd\u30a4\u30f3\u30bf\u3092\u8a08\u7b97\uff1afront + queSize\n        // \u30e2\u30b8\u30e5\u30ed\u64cd\u4f5c\u306b\u3088\u308a rear \u304c\u914d\u5217\u306e\u9577\u3055\u3092\u8d85\u3048\u308b\u3053\u3068\u3092\u56de\u907f\n        int rear = (front + queSize) % capacity();\n        // \u8981\u7d20\u3092\u30ad\u30e5\u30fc\u30ea\u30a2\u306b\u8ffd\u52a0\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u30c7\u30ad\u30e5\u30fc */\n    public int pop() {\n        int num = peek();\n        // \u30ad\u30e5\u30fc\u30d8\u30c3\u30c9\u30dd\u30a4\u30f3\u30bf\u3092\u5f8c\u308d\u306b1\u3064\u79fb\u52d5\u3001\u30e2\u30b8\u30e5\u30ed\u64cd\u4f5c\u306b\u3088\u308a\u7bc4\u56f2\u3092\u8d85\u3048\u308b\u3053\u3068\u3092\u56de\u907f\n        front = (front + 1) % capacity();\n        queSize--;\n        return num;\n    }\n\n    /* \u30ad\u30e5\u30fc\u30d8\u30c3\u30c9\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return nums[front];\n    }\n\n    /* \u914d\u5217\u3092\u8fd4\u3059 */\n    public int[] toArray() {\n        // front \u304b\u3089\u958b\u59cb\u3057\u3066 queSize \u500b\u306e\u8981\u7d20\u306e\u307f\u3092\u30b3\u30d4\u30fc\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[j % capacity()];\n        }\n        return res;\n    }\n}\n
        array_queue.cs
        [class]{ArrayQueue}-[func]{}\n
        array_queue.go
        [class]{arrayQueue}-[func]{}\n
        array_queue.swift
        [class]{ArrayQueue}-[func]{}\n
        array_queue.js
        [class]{ArrayQueue}-[func]{}\n
        array_queue.ts
        [class]{ArrayQueue}-[func]{}\n
        array_queue.dart
        [class]{ArrayQueue}-[func]{}\n
        array_queue.rs
        [class]{ArrayQueue}-[func]{}\n
        array_queue.c
        [class]{ArrayQueue}-[func]{}\n
        array_queue.kt
        [class]{ArrayQueue}-[func]{}\n
        array_queue.rb
        [class]{ArrayQueue}-[func]{}\n

        \u4e0a\u8a18\u306e\u30ad\u30e5\u30fc\u5b9f\u88c5\u306b\u306f\u307e\u3060\u5236\u9650\u304c\u3042\u308a\u307e\u3059\uff1a\u9577\u3055\u304c\u56fa\u5b9a\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u3053\u306e\u554f\u984c\u306f\u89e3\u6c7a\u304c\u56f0\u96e3\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u914d\u5217\u3092\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u81ea\u52d5\u62e1\u5f35\u3067\u304d\u308b\u52d5\u7684\u914d\u5217\u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u8208\u5473\u306e\u3042\u308b\u8aad\u8005\u306f\u81ea\u5206\u3067\u5b9f\u88c5\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002

        2\u3064\u306e\u5b9f\u88c5\u306e\u6bd4\u8f03\u306f\u30b9\u30bf\u30c3\u30af\u306e\u5834\u5408\u3068\u4e00\u8cab\u3057\u3066\u304a\u308a\u3001\u3053\u3053\u3067\u306f\u7e70\u308a\u8fd4\u3057\u307e\u305b\u3093\u3002

        "},{"location":"chapter_stack_and_queue/queue/#523","title":"5.2.3 \u00a0 \u30ad\u30e5\u30fc\u306e\u5178\u578b\u7684\u306a\u5fdc\u7528","text":"
        • Amazon\u306e\u6ce8\u6587\uff1a\u8cb7\u3044\u7269\u5ba2\u304c\u6ce8\u6587\u3092\u884c\u3063\u305f\u5f8c\u3001\u3053\u308c\u3089\u306e\u6ce8\u6587\u306f\u30ad\u30e5\u30fc\u306b\u53c2\u52a0\u3057\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u9806\u756a\u306b\u51e6\u7406\u3057\u307e\u3059\u3002\u72ec\u8eab\u306e\u65e5\u306a\u3069\u306e\u30a4\u30d9\u30f3\u30c8\u4e2d\u306f\u3001\u77ed\u6642\u9593\u3067\u5927\u91cf\u306e\u6ce8\u6587\u304c\u751f\u6210\u3055\u308c\u3001\u9ad8\u3044\u540c\u6642\u5b9f\u884c\u6027\u304c\u30a8\u30f3\u30b8\u30cb\u30a2\u306b\u3068\u3063\u3066\u91cd\u8981\u306a\u8ab2\u984c\u3068\u306a\u308a\u307e\u3059\u3002
        • \u69d8\u3005\u306aToDo\u30ea\u30b9\u30c8\uff1a\u300c\u5148\u7740\u9806\u300d\u6a5f\u80fd\u304c\u5fc5\u8981\u306a\u30b7\u30ca\u30ea\u30aa\u3001\u4f8b\u3048\u3070\u30d7\u30ea\u30f3\u30bf\u30fc\u306e\u30bf\u30b9\u30af\u30ad\u30e5\u30fc\u3084\u30ec\u30b9\u30c8\u30e9\u30f3\u306e\u914d\u9054\u30ad\u30e5\u30fc\u306a\u3069\u3001\u30ad\u30e5\u30fc\u3067\u51e6\u7406\u9806\u5e8f\u3092\u52b9\u679c\u7684\u306b\u7dad\u6301\u3067\u304d\u307e\u3059\u3002
        "},{"location":"chapter_stack_and_queue/stack/","title":"5.1 \u00a0 \u30b9\u30bf\u30c3\u30af","text":"

        \u30b9\u30bf\u30c3\u30af\u306f\u3001\u5f8c\u5165\u5148\u51fa\uff08LIFO\uff09\u306e\u539f\u5247\u306b\u5f93\u3046\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3059\u3002

        \u30b9\u30bf\u30c3\u30af\u3092\u30c6\u30fc\u30d6\u30eb\u4e0a\u306e\u76bf\u306e\u5c71\u306b\u4f8b\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u5e95\u306e\u76bf\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u306b\u306f\u3001\u307e\u305a\u4e0a\u306e\u76bf\u3092\u53d6\u308a\u9664\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u76bf\u3092\u69d8\u3005\u306a\u7a2e\u985e\u306e\u8981\u7d20\uff08\u6574\u6570\u3001\u6587\u5b57\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306a\u3069\uff09\u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u3067\u3001\u30b9\u30bf\u30c3\u30af\u3068\u547c\u3070\u308c\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u8981\u7d20\u306e\u5c71\u306e\u4e0a\u90e8\u3092\u300c\u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u300d\u3001\u4e0b\u90e8\u3092\u300c\u30b9\u30bf\u30c3\u30af\u306e\u30dc\u30c8\u30e0\u300d\u3068\u547c\u3073\u307e\u3059\u3002\u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u306b\u8981\u7d20\u3092\u8ffd\u52a0\u3059\u308b\u64cd\u4f5c\u3092\u300c\u30d7\u30c3\u30b7\u30e5\u300d\u3001\u30c8\u30c3\u30d7\u8981\u7d20\u3092\u524a\u9664\u3059\u308b\u64cd\u4f5c\u3092\u300c\u30dd\u30c3\u30d7\u300d\u3068\u547c\u3073\u307e\u3059\u3002

        \u56f3 5-1 \u00a0 \u30b9\u30bf\u30c3\u30af\u306e\u5f8c\u5165\u5148\u51fa\u30eb\u30fc\u30eb

        "},{"location":"chapter_stack_and_queue/stack/#511","title":"5.1.1 \u00a0 \u30b9\u30bf\u30c3\u30af\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c","text":"

        \u30b9\u30bf\u30c3\u30af\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c\u3092\u4e0b\u8868\u306b\u793a\u3057\u307e\u3059\u3002\u5177\u4f53\u7684\u306a\u30e1\u30bd\u30c3\u30c9\u540d\u306f\u4f7f\u7528\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u3088\u3063\u3066\u7570\u306a\u308a\u307e\u3059\u3002\u3053\u3053\u3067\u306f\u3001\u4f8b\u3068\u3057\u3066push()\u3001pop()\u3001peek()\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        \u8868 5-1 \u00a0 \u30b9\u30bf\u30c3\u30af\u64cd\u4f5c\u306e\u52b9\u7387

        \u30e1\u30bd\u30c3\u30c9 \u8aac\u660e \u6642\u9593\u8a08\u7b97\u91cf push() \u8981\u7d20\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5\uff08\u30c8\u30c3\u30d7\u306b\u8ffd\u52a0\uff09 \\(O(1)\\) pop() \u30b9\u30bf\u30c3\u30af\u304b\u3089\u30c8\u30c3\u30d7\u8981\u7d20\u3092\u30dd\u30c3\u30d7 \\(O(1)\\) peek() \u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 \\(O(1)\\)

        \u901a\u5e38\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u7d44\u307f\u8fbc\u307e\u308c\u3066\u3044\u308b\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9\u3092\u76f4\u63a5\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u4e00\u90e8\u306e\u8a00\u8a9e\u3067\u306f\u5177\u4f53\u7684\u306b\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9\u3092\u63d0\u4f9b\u3057\u3066\u3044\u306a\u3044\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u5834\u5408\u3001\u8a00\u8a9e\u306e\u300c\u914d\u5217\u300d\u307e\u305f\u306f\u300c\u9023\u7d50\u30ea\u30b9\u30c8\u300d\u3092\u30b9\u30bf\u30c3\u30af\u3068\u3057\u3066\u4f7f\u7528\u3057\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u30b9\u30bf\u30c3\u30af\u30ed\u30b8\u30c3\u30af\u306b\u95a2\u9023\u3057\u306a\u3044\u64cd\u4f5c\u3092\u7121\u8996\u3067\u304d\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlin stack.py
        # \u30b9\u30bf\u30c3\u30af\u3092\u521d\u671f\u5316\n# Python\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9\u304c\u306a\u3044\u305f\u3081\u3001list\u3092\u30b9\u30bf\u30c3\u30af\u3068\u3057\u3066\u4f7f\u7528\nstack: list[int] = []\n\n# \u8981\u7d20\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n# \u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\npeek: int = stack[-1]\n\n# \u30b9\u30bf\u30c3\u30af\u304b\u3089\u8981\u7d20\u3092\u30dd\u30c3\u30d7\npop: int = stack.pop()\n\n# \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97\nsize: int = len(stack)\n\n# \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af\nis_empty: bool = len(stack) == 0\n
        stack.cpp
        /* \u30b9\u30bf\u30c3\u30af\u3092\u521d\u671f\u5316 */\nstack<int> stack;\n\n/* \u8981\u7d20\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint top = stack.top();\n\n/* \u30b9\u30bf\u30c3\u30af\u304b\u3089\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\nstack.pop(); // \u623b\u308a\u5024\u306a\u3057\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = stack.size();\n\n/* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nbool empty = stack.empty();\n
        stack.java
        /* \u30b9\u30bf\u30c3\u30af\u3092\u521d\u671f\u5316 */\nStack<Integer> stack = new Stack<>();\n\n/* \u8981\u7d20\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint peek = stack.peek();\n\n/* \u30b9\u30bf\u30c3\u30af\u304b\u3089\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\nint pop = stack.pop();\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = stack.size();\n\n/* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nboolean isEmpty = stack.isEmpty();\n
        stack.cs
        /* \u30b9\u30bf\u30c3\u30af\u3092\u521d\u671f\u5316 */\nStack<int> stack = new();\n\n/* \u8981\u7d20\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5 */\nstack.Push(1);\nstack.Push(3);\nstack.Push(2);\nstack.Push(5);\nstack.Push(4);\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint peek = stack.Peek();\n\n/* \u30b9\u30bf\u30c3\u30af\u304b\u3089\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\nint pop = stack.Pop();\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = stack.Count;\n\n/* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nbool isEmpty = stack.Count == 0;\n
        stack_test.go
        /* \u30b9\u30bf\u30c3\u30af\u3092\u521d\u671f\u5316 */\n// Go\u3067\u306f\u3001Slice\u3092\u30b9\u30bf\u30c3\u30af\u3068\u3057\u3066\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u63a8\u5968\u3055\u308c\u307e\u3059\nvar stack []int\n\n/* \u8981\u7d20\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5 */\nstack = append(stack, 1)\nstack = append(stack, 3)\nstack = append(stack, 2)\nstack = append(stack, 5)\nstack = append(stack, 4)\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\npeek := stack[len(stack)-1]\n\n/* \u30b9\u30bf\u30c3\u30af\u304b\u3089\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\npop := stack[len(stack)-1]\nstack = stack[:len(stack)-1]\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nsize := len(stack)\n\n/* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nisEmpty := len(stack) == 0\n
        stack.swift
        /* \u30b9\u30bf\u30c3\u30af\u3092\u521d\u671f\u5316 */\n// Swift\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9\u304c\u306a\u3044\u305f\u3081\u3001Array\u3092\u30b9\u30bf\u30c3\u30af\u3068\u3057\u3066\u4f7f\u7528\nvar stack: [Int] = []\n\n/* \u8981\u7d20\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5 */\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nlet peek = stack.last!\n\n/* \u30b9\u30bf\u30c3\u30af\u304b\u3089\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\nlet pop = stack.removeLast()\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nlet size = stack.count\n\n/* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nlet isEmpty = stack.isEmpty\n
        stack.js
        /* \u30b9\u30bf\u30c3\u30af\u3092\u521d\u671f\u5316 */\n// JavaScript\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9\u304c\u306a\u3044\u305f\u3081\u3001Array\u3092\u30b9\u30bf\u30c3\u30af\u3068\u3057\u3066\u4f7f\u7528\nconst stack = [];\n\n/* \u8981\u7d20\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nconst peek = stack[stack.length-1];\n\n/* \u30b9\u30bf\u30c3\u30af\u304b\u3089\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\nconst pop = stack.pop();\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nconst size = stack.length;\n\n/* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nconst is_empty = stack.length === 0;\n
        stack.ts
        /* \u30b9\u30bf\u30c3\u30af\u3092\u521d\u671f\u5316 */\n// TypeScript\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9\u304c\u306a\u3044\u305f\u3081\u3001Array\u3092\u30b9\u30bf\u30c3\u30af\u3068\u3057\u3066\u4f7f\u7528\nconst stack: number[] = [];\n\n/* \u8981\u7d20\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nconst peek = stack[stack.length - 1];\n\n/* \u30b9\u30bf\u30c3\u30af\u304b\u3089\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\nconst pop = stack.pop();\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nconst size = stack.length;\n\n/* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nconst is_empty = stack.length === 0;\n
        stack.dart
        /* \u30b9\u30bf\u30c3\u30af\u3092\u521d\u671f\u5316 */\n// Dart\u306b\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9\u304c\u306a\u3044\u305f\u3081\u3001List\u3092\u30b9\u30bf\u30c3\u30af\u3068\u3057\u3066\u4f7f\u7528\nList<int> stack = [];\n\n/* \u8981\u7d20\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5 */\nstack.add(1);\nstack.add(3);\nstack.add(2);\nstack.add(5);\nstack.add(4);\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nint peek = stack.last;\n\n/* \u30b9\u30bf\u30c3\u30af\u304b\u3089\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\nint pop = stack.removeLast();\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nint size = stack.length;\n\n/* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nbool isEmpty = stack.isEmpty;\n
        stack.rs
        /* \u30b9\u30bf\u30c3\u30af\u3092\u521d\u671f\u5316 */\n// Vec\u3092\u30b9\u30bf\u30c3\u30af\u3068\u3057\u3066\u4f7f\u7528\nlet mut stack: Vec<i32> = Vec::new();\n\n/* \u8981\u7d20\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\nlet top = stack.last().unwrap();\n\n/* \u30b9\u30bf\u30c3\u30af\u304b\u3089\u8981\u7d20\u3092\u30dd\u30c3\u30d7 */\nlet pop = stack.pop().unwrap();\n\n/* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\nlet size = stack.len();\n\n/* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af */\nlet is_empty = stack.is_empty();\n
        stack.c
        // C\u306f\u7d44\u307f\u8fbc\u307f\u306e\u30b9\u30bf\u30c3\u30af\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\n
        stack.kt
        \n
        "},{"location":"chapter_stack_and_queue/stack/#512","title":"5.1.2 \u00a0 \u30b9\u30bf\u30c3\u30af\u306e\u5b9f\u88c5","text":"

        \u30b9\u30bf\u30c3\u30af\u304c\u3069\u306e\u3088\u3046\u306b\u52d5\u4f5c\u3059\u308b\u304b\u3092\u3088\u308a\u6df1\u304f\u7406\u89e3\u3059\u308b\u305f\u3081\u306b\u3001\u81ea\u5206\u3067\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9\u3092\u5b9f\u88c5\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002

        \u30b9\u30bf\u30c3\u30af\u306f\u5f8c\u5165\u5148\u51fa\u306e\u539f\u5247\u306b\u5f93\u3046\u305f\u3081\u3001\u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u3067\u306e\u307f\u8981\u7d20\u3092\u8ffd\u52a0\u307e\u305f\u306f\u524a\u9664\u3067\u304d\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u914d\u5217\u3068\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u4e21\u65b9\u306f\u4efb\u610f\u306e\u4f4d\u7f6e\u3067\u8981\u7d20\u3092\u8ffd\u52a0\u30fb\u524a\u9664\u3067\u304d\u308b\u305f\u3081\u3001\u30b9\u30bf\u30c3\u30af\u306f\u5236\u9650\u3055\u308c\u305f\u914d\u5217\u307e\u305f\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u3068\u898b\u306a\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u8a00\u3044\u63db\u3048\u308c\u3070\u3001\u914d\u5217\u3084\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u7279\u5b9a\u306e\u7121\u95a2\u4fc2\u306a\u64cd\u4f5c\u3092\u300c\u906e\u853d\u300d\u3057\u3066\u3001\u5916\u90e8\u306e\u52d5\u4f5c\u3092\u30b9\u30bf\u30c3\u30af\u306e\u7279\u6027\u306b\u5408\u308f\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_stack_and_queue/stack/#1","title":"1. \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u30d9\u30fc\u30b9\u306e\u5b9f\u88c5","text":"

        \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066\u30b9\u30bf\u30c3\u30af\u3092\u5b9f\u88c5\u3059\u308b\u5834\u5408\u3001\u30ea\u30b9\u30c8\u306e\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u3092\u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u3001\u30c6\u30fc\u30eb\u30ce\u30fc\u30c9\u3092\u30b9\u30bf\u30c3\u30af\u306e\u30dc\u30c8\u30e0\u3068\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30d7\u30c3\u30b7\u30e5\u64cd\u4f5c\u3067\u306f\u3001\u5358\u306b\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30d8\u30c3\u30c9\u306b\u8981\u7d20\u3092\u633f\u5165\u3057\u307e\u3059\u3002\u3053\u306e\u30ce\u30fc\u30c9\u633f\u5165\u65b9\u6cd5\u306f\u300c\u30d8\u30c3\u30c9\u633f\u5165\u300d\u3068\u3057\u3066\u77e5\u3089\u308c\u3066\u3044\u307e\u3059\u3002\u30dd\u30c3\u30d7\u64cd\u4f5c\u3067\u306f\u3001\u30ea\u30b9\u30c8\u304b\u3089\u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3059\u308b\u3060\u3051\u3067\u3059\u3002

        LinkedListStackpush()pop()

        \u56f3 5-2 \u00a0 \u9023\u7d50\u30ea\u30b9\u30c8\u306b\u3088\u308b\u30b9\u30bf\u30c3\u30af\u5b9f\u88c5\u306e\u30d7\u30c3\u30b7\u30e5\u3068\u30dd\u30c3\u30d7\u64cd\u4f5c

        \u4ee5\u4e0b\u306f\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u30b9\u30bf\u30c3\u30af\u5b9f\u88c5\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linkedlist_stack.py
        class LinkedListStack:\n    \"\"\"\u9023\u7d50\u30ea\u30b9\u30c8\u30d9\u30fc\u30b9\u306e\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9\"\"\"\n\n    def __init__(self):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        self._peek: ListNode | None = None\n        self._size: int = 0\n\n    def size(self) -> int:\n        \"\"\"\u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\"\"\"\n        return self._size == 0\n\n    def push(self, val: int):\n        \"\"\"\u30d7\u30c3\u30b7\u30e5\"\"\"\n        node = ListNode(val)\n        node.next = self._peek\n        self._peek = node\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"\u30dd\u30c3\u30d7\"\"\"\n        num = self.peek()\n        self._peek = self._peek.next\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"\u30b9\u30bf\u30c3\u30af\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Stack is empty\")\n        return self._peek.val\n\n    def to_list(self) -> list[int]:\n        \"\"\"\u51fa\u529b\u7528\u306e\u30ea\u30b9\u30c8\u306b\u5909\u63db\"\"\"\n        arr = []\n        node = self._peek\n        while node:\n            arr.append(node.val)\n            node = node.next\n        arr.reverse()\n        return arr\n
        linkedlist_stack.cpp
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9 */\nclass LinkedListStack {\n  private:\n    ListNode *stackTop; // \u5148\u982d\u30ce\u30fc\u30c9\u3092\u30b9\u30bf\u30c3\u30af\u30c8\u30c3\u30d7\u3068\u3057\u3066\u4f7f\u7528\n    int stkSize;        // \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\n\n  public:\n    LinkedListStack() {\n        stackTop = nullptr;\n        stkSize = 0;\n    }\n\n    ~LinkedListStack() {\n        // \u9023\u7d50\u30ea\u30b9\u30c8\u3092\u8d70\u67fb\u3001\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3001\u30e1\u30e2\u30ea\u3092\u89e3\u653e\n        freeMemoryLinkedList(stackTop);\n    }\n\n    /* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    int size() {\n        return stkSize;\n    }\n\n    /* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    bool isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u30d7\u30c3\u30b7\u30e5 */\n    void push(int num) {\n        ListNode *node = new ListNode(num);\n        node->next = stackTop;\n        stackTop = node;\n        stkSize++;\n    }\n\n    /* \u30dd\u30c3\u30d7 */\n    int pop() {\n        int num = top();\n        ListNode *tmp = stackTop;\n        stackTop = stackTop->next;\n        // \u30e1\u30e2\u30ea\u3092\u89e3\u653e\n        delete tmp;\n        stkSize--;\n        return num;\n    }\n\n    /* \u30b9\u30bf\u30c3\u30af\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    int top() {\n        if (isEmpty())\n            throw out_of_range(\"Stack is empty\");\n        return stackTop->val;\n    }\n\n    /* \u30ea\u30b9\u30c8\u3092\u914d\u5217\u306b\u5909\u63db\u3057\u3066\u8fd4\u5374 */\n    vector<int> toVector() {\n        ListNode *node = stackTop;\n        vector<int> res(size());\n        for (int i = res.size() - 1; i >= 0; i--) {\n            res[i] = node->val;\n            node = node->next;\n        }\n        return res;\n    }\n};\n
        linkedlist_stack.java
        /* \u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9 */\nclass LinkedListStack {\n    private ListNode stackPeek; // \u30d8\u30c3\u30c9\u30ce\u30fc\u30c9\u3092\u30b9\u30bf\u30c3\u30af\u30c8\u30c3\u30d7\u3068\u3057\u3066\u4f7f\u7528\n    private int stkSize = 0; // \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\n\n    public LinkedListStack() {\n        stackPeek = null;\n    }\n\n    /* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    public int size() {\n        return stkSize;\n    }\n\n    /* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u30d7\u30c3\u30b7\u30e5 */\n    public void push(int num) {\n        ListNode node = new ListNode(num);\n        node.next = stackPeek;\n        stackPeek = node;\n        stkSize++;\n    }\n\n    /* \u30dd\u30c3\u30d7 */\n    public int pop() {\n        int num = peek();\n        stackPeek = stackPeek.next;\n        stkSize--;\n        return num;\n    }\n\n    /* \u30b9\u30bf\u30c3\u30af\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return stackPeek.val;\n    }\n\n    /* List \u3092 Array \u306b\u5909\u63db\u3057\u3066\u8fd4\u3059 */\n    public int[] toArray() {\n        ListNode node = stackPeek;\n        int[] res = new int[size()];\n        for (int i = res.length - 1; i >= 0; i--) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_stack.cs
        [class]{LinkedListStack}-[func]{}\n
        linkedlist_stack.go
        [class]{linkedListStack}-[func]{}\n
        linkedlist_stack.swift
        [class]{LinkedListStack}-[func]{}\n
        linkedlist_stack.js
        [class]{LinkedListStack}-[func]{}\n
        linkedlist_stack.ts
        [class]{LinkedListStack}-[func]{}\n
        linkedlist_stack.dart
        [class]{LinkedListStack}-[func]{}\n
        linkedlist_stack.rs
        [class]{LinkedListStack}-[func]{}\n
        linkedlist_stack.c
        [class]{LinkedListStack}-[func]{}\n
        linkedlist_stack.kt
        [class]{LinkedListStack}-[func]{}\n
        linkedlist_stack.rb
        [class]{LinkedListStack}-[func]{}\n
        "},{"location":"chapter_stack_and_queue/stack/#2","title":"2. \u00a0 \u914d\u5217\u30d9\u30fc\u30b9\u306e\u5b9f\u88c5","text":"

        \u914d\u5217\u3092\u4f7f\u7528\u3057\u3066\u30b9\u30bf\u30c3\u30af\u3092\u5b9f\u88c5\u3059\u308b\u5834\u5408\u3001\u914d\u5217\u306e\u672b\u5c3e\u3092\u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u3068\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30d7\u30c3\u30b7\u30e5\u3068\u30dd\u30c3\u30d7\u64cd\u4f5c\u306f\u3001\u305d\u308c\u305e\u308c\u914d\u5217\u306e\u672b\u5c3e\u3067\u306e\u8981\u7d20\u306e\u8ffd\u52a0\u3068\u524a\u9664\u306b\u5bfe\u5fdc\u3057\u3001\u3069\u3061\u3089\u3082\u6642\u9593\u8a08\u7b97\u91cf\\(O(1)\\)\u3067\u3059\u3002

        ArrayStackpush()pop()

        \u56f3 5-3 \u00a0 \u914d\u5217\u306b\u3088\u308b\u30b9\u30bf\u30c3\u30af\u5b9f\u88c5\u306e\u30d7\u30c3\u30b7\u30e5\u3068\u30dd\u30c3\u30d7\u64cd\u4f5c

        \u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5\u3055\u308c\u308b\u8981\u7d20\u304c\u7d99\u7d9a\u7684\u306b\u5897\u52a0\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3001\u52d5\u7684\u914d\u5217\u3092\u4f7f\u7528\u3067\u304d\u3001\u914d\u5217\u62e1\u5f35\u3092\u81ea\u5206\u3067\u51e6\u7406\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u305b\u3093\u3002\u4ee5\u4e0b\u306f\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_stack.py
        class ArrayStack:\n    \"\"\"\u914d\u5217\u30d9\u30fc\u30b9\u306e\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9\"\"\"\n\n    def __init__(self):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        self._stack: list[int] = []\n\n    def size(self) -> int:\n        \"\"\"\u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97\"\"\"\n        return len(self._stack)\n\n    def is_empty(self) -> bool:\n        \"\"\"\u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\"\"\"\n        return self.size() == 0\n\n    def push(self, item: int):\n        \"\"\"\u30d7\u30c3\u30b7\u30e5\"\"\"\n        self._stack.append(item)\n\n    def pop(self) -> int:\n        \"\"\"\u30dd\u30c3\u30d7\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Stack is empty\")\n        return self._stack.pop()\n\n    def peek(self) -> int:\n        \"\"\"\u30b9\u30bf\u30c3\u30af\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Stack is empty\")\n        return self._stack[-1]\n\n    def to_list(self) -> list[int]:\n        \"\"\"\u51fa\u529b\u7528\u306e\u914d\u5217\u3092\u8fd4\u3059\"\"\"\n        return self._stack\n
        array_stack.cpp
        /* \u914d\u5217\u306b\u57fa\u3065\u304f\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9 */\nclass ArrayStack {\n  private:\n    vector<int> stack;\n\n  public:\n    /* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    int size() {\n        return stack.size();\n    }\n\n    /* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    bool isEmpty() {\n        return stack.size() == 0;\n    }\n\n    /* \u30d7\u30c3\u30b7\u30e5 */\n    void push(int num) {\n        stack.push_back(num);\n    }\n\n    /* \u30dd\u30c3\u30d7 */\n    int pop() {\n        int num = top();\n        stack.pop_back();\n        return num;\n    }\n\n    /* \u30b9\u30bf\u30c3\u30af\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    int top() {\n        if (isEmpty())\n            throw out_of_range(\"Stack is empty\");\n        return stack.back();\n    }\n\n    /* Vector\u3092\u8fd4\u5374 */\n    vector<int> toVector() {\n        return stack;\n    }\n};\n
        array_stack.java
        /* \u914d\u5217\u306b\u57fa\u3065\u304f\u30b9\u30bf\u30c3\u30af\u30af\u30e9\u30b9 */\nclass ArrayStack {\n    private ArrayList<Integer> stack;\n\n    public ArrayStack() {\n        // \u30ea\u30b9\u30c8\uff08\u52d5\u7684\u914d\u5217\uff09\u3092\u521d\u671f\u5316\n        stack = new ArrayList<>();\n    }\n\n    /* \u30b9\u30bf\u30c3\u30af\u306e\u9577\u3055\u3092\u53d6\u5f97 */\n    public int size() {\n        return stack.size();\n    }\n\n    /* \u30b9\u30bf\u30c3\u30af\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u30d7\u30c3\u30b7\u30e5 */\n    public void push(int num) {\n        stack.add(num);\n    }\n\n    /* \u30dd\u30c3\u30d7 */\n    public int pop() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return stack.remove(size() - 1);\n    }\n\n    /* \u30b9\u30bf\u30c3\u30af\u30c8\u30c3\u30d7\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9 */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return stack.get(size() - 1);\n    }\n\n    /* List \u3092 Array \u306b\u5909\u63db\u3057\u3066\u8fd4\u3059 */\n    public Object[] toArray() {\n        return stack.toArray();\n    }\n}\n
        array_stack.cs
        [class]{ArrayStack}-[func]{}\n
        array_stack.go
        [class]{arrayStack}-[func]{}\n
        array_stack.swift
        [class]{ArrayStack}-[func]{}\n
        array_stack.js
        [class]{ArrayStack}-[func]{}\n
        array_stack.ts
        [class]{ArrayStack}-[func]{}\n
        array_stack.dart
        [class]{ArrayStack}-[func]{}\n
        array_stack.rs
        [class]{ArrayStack}-[func]{}\n
        array_stack.c
        [class]{ArrayStack}-[func]{}\n
        array_stack.kt
        [class]{ArrayStack}-[func]{}\n
        array_stack.rb
        [class]{ArrayStack}-[func]{}\n
        "},{"location":"chapter_stack_and_queue/stack/#513-2","title":"5.1.3 \u00a0 2\u3064\u306e\u5b9f\u88c5\u306e\u6bd4\u8f03","text":"

        \u30b5\u30dd\u30fc\u30c8\u3055\u308c\u308b\u64cd\u4f5c

        \u4e21\u65b9\u306e\u5b9f\u88c5\u306f\u3001\u30b9\u30bf\u30c3\u30af\u3067\u5b9a\u7fa9\u3055\u308c\u305f\u3059\u3079\u3066\u306e\u64cd\u4f5c\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u307e\u3059\u3002\u914d\u5217\u5b9f\u88c5\u306f\u3055\u3089\u306b\u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u307e\u3059\u304c\u3001\u3053\u308c\u306f\u30b9\u30bf\u30c3\u30af\u5b9a\u7fa9\u306e\u7bc4\u56f2\u3092\u8d85\u3048\u3066\u304a\u308a\u3001\u4e00\u822c\u7684\u306b\u306f\u4f7f\u7528\u3055\u308c\u307e\u305b\u3093\u3002

        \u6642\u9593\u52b9\u7387

        \u914d\u5217\u30d9\u30fc\u30b9\u306e\u5b9f\u88c5\u3067\u306f\u3001\u30d7\u30c3\u30b7\u30e5\u3068\u30dd\u30c3\u30d7\u64cd\u4f5c\u306e\u4e21\u65b9\u304c\u4e8b\u524d\u306b\u5272\u308a\u5f53\u3066\u3089\u308c\u305f\u9023\u7d9a\u30e1\u30e2\u30ea\u3067\u767a\u751f\u3057\u3001\u826f\u597d\u306a\u30ad\u30e3\u30c3\u30b7\u30e5\u5c40\u6240\u6027\u304c\u3042\u308b\u305f\u3081\u52b9\u7387\u304c\u9ad8\u304f\u306a\u308a\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u30d7\u30c3\u30b7\u30e5\u64cd\u4f5c\u304c\u914d\u5217\u5bb9\u91cf\u3092\u8d85\u3048\u308b\u5834\u5408\u3001\u30ea\u30b5\u30a4\u30ba\u30e1\u30ab\u30cb\u30ba\u30e0\u304c\u30c8\u30ea\u30ac\u30fc\u3055\u308c\u3001\u305d\u306e\u30d7\u30c3\u30b7\u30e5\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u306b\u306a\u308a\u307e\u3059\u3002

        \u9023\u7d50\u30ea\u30b9\u30c8\u5b9f\u88c5\u3067\u306f\u3001\u30ea\u30b9\u30c8\u62e1\u5f35\u306f\u975e\u5e38\u306b\u67d4\u8edf\u3067\u3001\u914d\u5217\u62e1\u5f35\u306e\u3088\u3046\u306a\u52b9\u7387\u4f4e\u4e0b\u306e\u554f\u984c\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u3057\u304b\u3057\u3001\u30d7\u30c3\u30b7\u30e5\u64cd\u4f5c\u306b\u306f\u30ce\u30fc\u30c9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u521d\u671f\u5316\u3068\u30dd\u30a4\u30f3\u30bf\u306e\u5909\u66f4\u304c\u5fc5\u8981\u306a\u305f\u3081\u3001\u52b9\u7387\u306f\u6bd4\u8f03\u7684\u4f4e\u304f\u306a\u308a\u307e\u3059\u3002\u30d7\u30c3\u30b7\u30e5\u3055\u308c\u308b\u8981\u7d20\u304c\u3059\u3067\u306b\u30ce\u30fc\u30c9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5834\u5408\u3001\u521d\u671f\u5316\u30b9\u30c6\u30c3\u30d7\u3092\u30b9\u30ad\u30c3\u30d7\u3067\u304d\u3001\u52b9\u7387\u304c\u5411\u4e0a\u3057\u307e\u3059\u3002

        \u3057\u305f\u304c\u3063\u3066\u3001\u30d7\u30c3\u30b7\u30e5\u3068\u30dd\u30c3\u30d7\u64cd\u4f5c\u306e\u8981\u7d20\u304cint\u3084double\u306a\u3069\u306e\u57fa\u672c\u30c7\u30fc\u30bf\u578b\u306e\u5834\u5408\u3001\u4ee5\u4e0b\u306e\u7d50\u8ad6\u3092\u5c0e\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a

        • \u914d\u5217\u30d9\u30fc\u30b9\u306e\u30b9\u30bf\u30c3\u30af\u5b9f\u88c5\u306f\u62e1\u5f35\u6642\u306b\u52b9\u7387\u304c\u4f4e\u4e0b\u3057\u307e\u3059\u304c\u3001\u62e1\u5f35\u306f\u4f4e\u983b\u5ea6\u64cd\u4f5c\u3067\u3042\u308b\u305f\u3081\u3001\u5e73\u5747\u52b9\u7387\u306f\u9ad8\u304f\u306a\u308a\u307e\u3059\u3002
        • \u9023\u7d50\u30ea\u30b9\u30c8\u30d9\u30fc\u30b9\u306e\u30b9\u30bf\u30c3\u30af\u5b9f\u88c5\u306f\u3088\u308a\u5b89\u5b9a\u3057\u305f\u52b9\u7387\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002

        \u7a7a\u9593\u52b9\u7387

        \u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316\u3059\u308b\u969b\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u300c\u521d\u671f\u5bb9\u91cf\u300d\u3092\u5272\u308a\u5f53\u3066\u307e\u3059\u304c\u3001\u3053\u308c\u306f\u5b9f\u969b\u306e\u5fc5\u8981\u91cf\u3092\u8d85\u3048\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3055\u3089\u306b\u3001\u62e1\u5f35\u30e1\u30ab\u30cb\u30ba\u30e0\u306f\u901a\u5e38\u3001\u7279\u5b9a\u306e\u4fc2\u6570\uff082\u500d\u306a\u3069\uff09\u3067\u5bb9\u91cf\u3092\u5897\u52a0\u3055\u305b\u3001\u3053\u308c\u3082\u5b9f\u969b\u306e\u5fc5\u8981\u91cf\u3092\u8d85\u3048\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u914d\u5217\u30d9\u30fc\u30b9\u306e\u30b9\u30bf\u30c3\u30af\u306f\u4e00\u90e8\u306e\u7a7a\u9593\u3092\u7121\u99c4\u306b\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        \u3057\u304b\u3057\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u306f\u30dd\u30a4\u30f3\u30bf\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306e\u8ffd\u52a0\u7a7a\u9593\u304c\u5fc5\u8981\u306a\u305f\u3081\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u30ce\u30fc\u30c9\u304c\u5360\u6709\u3059\u308b\u7a7a\u9593\u306f\u6bd4\u8f03\u7684\u5927\u304d\u304f\u306a\u308a\u307e\u3059\u3002

        \u307e\u3068\u3081\u308b\u3068\u3001\u3069\u3061\u3089\u306e\u5b9f\u88c5\u304c\u3088\u308a\u30e1\u30e2\u30ea\u52b9\u7387\u7684\u304b\u3092\u5358\u7d14\u306b\u5224\u65ad\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002\u7279\u5b9a\u306e\u72b6\u6cc1\u306b\u57fa\u3065\u304f\u5206\u6790\u304c\u5fc5\u8981\u3067\u3059\u3002

        "},{"location":"chapter_stack_and_queue/stack/#514","title":"5.1.4 \u00a0 \u30b9\u30bf\u30c3\u30af\u306e\u5178\u578b\u7684\u306a\u5fdc\u7528","text":"
        • \u30d6\u30e9\u30a6\u30b6\u306e\u623b\u308b\u3068\u9032\u3080\u3001\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u306e\u5143\u306b\u623b\u3059\u3068\u3084\u308a\u76f4\u3057\u3002\u65b0\u3057\u3044Web\u30da\u30fc\u30b8\u3092\u958b\u304f\u305f\u3073\u306b\u3001\u30d6\u30e9\u30a6\u30b6\u306f\u524d\u306e\u30da\u30fc\u30b8\u3092\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5\u3057\u3001\u623b\u308b\u64cd\u4f5c\uff08\u672c\u8cea\u7684\u306b\u306f\u30dd\u30c3\u30d7\u64cd\u4f5c\uff09\u3092\u901a\u3058\u3066\u524d\u306e\u30da\u30fc\u30b8\u306b\u623b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u623b\u308b\u3068\u9032\u3080\u306e\u4e21\u65b9\u3092\u30b5\u30dd\u30fc\u30c8\u3059\u308b\u306b\u306f\u30012\u3064\u306e\u30b9\u30bf\u30c3\u30af\u304c\u9023\u643a\u3057\u3066\u52d5\u4f5c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
        • \u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u30e1\u30e2\u30ea\u7ba1\u7406\u3002\u95a2\u6570\u304c\u547c\u3073\u51fa\u3055\u308c\u308b\u305f\u3073\u306b\u3001\u30b7\u30b9\u30c6\u30e0\u306f\u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u306b\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u3092\u8ffd\u52a0\u3057\u3066\u95a2\u6570\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u60c5\u5831\u3092\u8a18\u9332\u3057\u307e\u3059\u3002\u518d\u5e30\u95a2\u6570\u3067\u306f\u3001\u4e0b\u65b9\u5411\u306e\u518d\u5e30\u30d5\u30a7\u30fc\u30ba\u306f\u30b9\u30bf\u30c3\u30af\u3078\u306e\u30d7\u30c3\u30b7\u30e5\u3092\u7d9a\u3051\u3001\u4e0a\u65b9\u5411\u306e\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u30d5\u30a7\u30fc\u30ba\u306f\u30b9\u30bf\u30c3\u30af\u304b\u3089\u306e\u30dd\u30c3\u30d7\u3092\u7d9a\u3051\u307e\u3059\u3002
        "},{"location":"chapter_stack_and_queue/summary/","title":"5.4 \u00a0 \u307e\u3068\u3081","text":""},{"location":"chapter_stack_and_queue/summary/#1","title":"1. \u00a0 \u91cd\u8981\u306a\u30dd\u30a4\u30f3\u30c8","text":"
        • \u30b9\u30bf\u30c3\u30af\u306f\u5f8c\u5165\u308c\u5148\u51fa\u3057\uff08LIFO\uff09\u306e\u539f\u5247\u306b\u5f93\u3046\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3001\u914d\u5217\u307e\u305f\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u4f7f\u3063\u3066\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002
        • \u6642\u9593\u52b9\u7387\u306e\u89b3\u70b9\u3067\u306f\u3001\u30b9\u30bf\u30c3\u30af\u306e\u914d\u5217\u5b9f\u88c5\u306e\u65b9\u304c\u5e73\u5747\u7684\u306a\u52b9\u7387\u304c\u9ad8\u3044\u3067\u3059\u3002\u305f\u3060\u3057\u3001\u62e1\u5f35\u6642\u306b\u306f\u5358\u4e00\u306e\u30d7\u30c3\u30b7\u30e5\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(n)\\)\u306b\u60aa\u5316\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u5bfe\u7167\u7684\u306b\u3001\u30b9\u30bf\u30c3\u30af\u306e\u9023\u7d50\u30ea\u30b9\u30c8\u5b9f\u88c5\u306f\u3088\u308a\u5b89\u5b9a\u3057\u305f\u52b9\u7387\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002
        • \u7a7a\u9593\u52b9\u7387\u306b\u95a2\u3057\u3066\u306f\u3001\u30b9\u30bf\u30c3\u30af\u306e\u914d\u5217\u5b9f\u88c5\u306f\u4e00\u5b9a\u7a0b\u5ea6\u306e\u7a7a\u9593\u306e\u7121\u99c4\u306b\u3064\u306a\u304c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30ce\u30fc\u30c9\u304c\u5360\u6709\u3059\u308b\u30e1\u30e2\u30ea\u7a7a\u9593\u306f\u4e00\u822c\u7684\u306b\u914d\u5217\u306e\u8981\u7d20\u3088\u308a\u3082\u5927\u304d\u3044\u3053\u3068\u306b\u6ce8\u610f\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002
        • \u30ad\u30e5\u30fc\u306f\u5148\u5165\u308c\u5148\u51fa\u3057\uff08FIFO\uff09\u306e\u539f\u5247\u306b\u5f93\u3046\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3001\u540c\u69d8\u306b\u914d\u5217\u307e\u305f\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u3092\u4f7f\u3063\u3066\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002\u30ad\u30e5\u30fc\u306e\u6642\u9593\u3068\u7a7a\u9593\u52b9\u7387\u306b\u95a2\u3059\u308b\u7d50\u8ad6\u306f\u3001\u30b9\u30bf\u30c3\u30af\u3068\u4f3c\u3066\u3044\u307e\u3059\u3002
        • \u4e21\u7aef\u30ad\u30e5\u30fc\uff08deque\uff09\u306f\u3088\u308a\u67d4\u8edf\u306a\u30ad\u30e5\u30fc\u306e\u7a2e\u985e\u3067\u3001\u4e21\u7aef\u3067\u306e\u8981\u7d20\u306e\u8ffd\u52a0\u3068\u524a\u9664\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002
        "},{"location":"chapter_stack_and_queue/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q: \u30d6\u30e9\u30a6\u30b6\u306e\u9032\u3080\u30fb\u623b\u308b\u6a5f\u80fd\u306f\u53cc\u65b9\u5411\u9023\u7d50\u30ea\u30b9\u30c8\u3067\u5b9f\u88c5\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3059\u304b\uff1f

        \u30d6\u30e9\u30a6\u30b6\u306e\u9032\u3080\u30fb\u623b\u308b\u30ca\u30d3\u30b2\u30fc\u30b7\u30e7\u30f3\u306f\u672c\u8cea\u7684\u306b\u300c\u30b9\u30bf\u30c3\u30af\u300d\u6982\u5ff5\u306e\u73fe\u308c\u3067\u3059\u3002\u30e6\u30fc\u30b6\u30fc\u304c\u65b0\u3057\u3044\u30da\u30fc\u30b8\u3092\u8a2a\u554f\u3059\u308b\u3068\u3001\u305d\u306e\u30da\u30fc\u30b8\u304c\u30b9\u30bf\u30c3\u30af\u306e\u5148\u982d\u306b\u8ffd\u52a0\u3055\u308c\u307e\u3059\u3002\u623b\u308b\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u3068\u3001\u30da\u30fc\u30b8\u304c\u30b9\u30bf\u30c3\u30af\u306e\u5148\u982d\u304b\u3089\u30dd\u30c3\u30d7\u3055\u308c\u307e\u3059\u3002\u4e21\u7aef\u30ad\u30e5\u30fc\uff08deque\uff09\u306f\u3001\u300c\u4e21\u7aef\u30ad\u30e5\u30fc\u300d\u306e\u7ae0\u3067\u8ff0\u3079\u305f\u3088\u3046\u306b\u3001\u3044\u304f\u3064\u304b\u306e\u8ffd\u52a0\u64cd\u4f5c\u3092\u4fbf\u5229\u306b\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002

        Q: \u30b9\u30bf\u30c3\u30af\u304b\u3089\u30dd\u30c3\u30d7\u3057\u305f\u5f8c\u3001\u30dd\u30c3\u30d7\u3055\u308c\u305f\u30ce\u30fc\u30c9\u306e\u30e1\u30e2\u30ea\u3092\u89e3\u653e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u304b\uff1f

        \u30dd\u30c3\u30d7\u3055\u308c\u305f\u30ce\u30fc\u30c9\u304c\u5f8c\u3067\u4f7f\u7528\u3055\u308c\u308b\u5834\u5408\u306f\u3001\u305d\u306e\u30e1\u30e2\u30ea\u3092\u89e3\u653e\u3059\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u81ea\u52d5\u30ac\u30d9\u30fc\u30b8\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3092\u6301\u3064Java\u3084Python\u306a\u3069\u306e\u8a00\u8a9e\u3067\u306f\u3001\u624b\u52d5\u306e\u30e1\u30e2\u30ea\u89e3\u653e\u306f\u5fc5\u8981\u3042\u308a\u307e\u305b\u3093\u3002C\u3084C++\u3067\u306f\u3001\u624b\u52d5\u306e\u30e1\u30e2\u30ea\u89e3\u653e\u304c\u5fc5\u8981\u3067\u3059\u3002

        Q: \u4e21\u7aef\u30ad\u30e5\u30fc\u306f2\u3064\u306e\u30b9\u30bf\u30c3\u30af\u3092\u7d50\u5408\u3057\u305f\u3082\u306e\u306e\u3088\u3046\u306b\u898b\u3048\u307e\u3059\u3002\u305d\u306e\u7528\u9014\u306f\u4f55\u3067\u3059\u304b\uff1f

        \u4e21\u7aef\u30ad\u30e5\u30fc\u306f\u3001\u30b9\u30bf\u30c3\u30af\u3068\u30ad\u30e5\u30fc\u306e\u7d44\u307f\u5408\u308f\u305b\u307e\u305f\u306f2\u3064\u306e\u30b9\u30bf\u30c3\u30af\u3092\u7d50\u5408\u3057\u305f\u3082\u306e\u3067\u3001\u30b9\u30bf\u30c3\u30af\u3068\u30ad\u30e5\u30fc\u306e\u4e21\u65b9\u306e\u30ed\u30b8\u30c3\u30af\u3092\u793a\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30b9\u30bf\u30c3\u30af\u3068\u30ad\u30e5\u30fc\u306e\u3059\u3079\u3066\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u5b9f\u88c5\u3067\u304d\u3001\u3088\u308a\u5927\u304d\u306a\u67d4\u8edf\u6027\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002

        Q: \u5143\u306b\u623b\u3059\u3068\u3084\u308a\u76f4\u3057\u306f\u5177\u4f53\u7684\u306b\u3069\u306e\u3088\u3046\u306b\u5b9f\u88c5\u3055\u308c\u308b\u306e\u3067\u3059\u304b\uff1f

        \u5143\u306b\u623b\u3059\u3068\u3084\u308a\u76f4\u3057\u306e\u64cd\u4f5c\u306f2\u3064\u306e\u30b9\u30bf\u30c3\u30af\u3092\u4f7f\u3063\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\uff1a\u5143\u306b\u623b\u3059\u7528\u306e\u30b9\u30bf\u30c3\u30afA\u3068\u3084\u308a\u76f4\u3057\u7528\u306e\u30b9\u30bf\u30c3\u30afB\u3067\u3059\u3002

        1. \u30e6\u30fc\u30b6\u30fc\u304c\u64cd\u4f5c\u3092\u5b9f\u884c\u3059\u308b\u305f\u3073\u306b\u3001\u305d\u308c\u304c\u30b9\u30bf\u30c3\u30afA\u306b\u30d7\u30c3\u30b7\u30e5\u3055\u308c\u3001\u30b9\u30bf\u30c3\u30afB\u304c\u30af\u30ea\u30a2\u3055\u308c\u307e\u3059\u3002
        2. \u30e6\u30fc\u30b6\u30fc\u304c\u300c\u5143\u306b\u623b\u3059\u300d\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001\u6700\u65b0\u306e\u64cd\u4f5c\u304c\u30b9\u30bf\u30c3\u30afA\u304b\u3089\u30dd\u30c3\u30d7\u3055\u308c\u3001\u30b9\u30bf\u30c3\u30afB\u306b\u30d7\u30c3\u30b7\u30e5\u3055\u308c\u307e\u3059\u3002
        3. \u30e6\u30fc\u30b6\u30fc\u304c\u300c\u3084\u308a\u76f4\u3057\u300d\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001\u6700\u65b0\u306e\u64cd\u4f5c\u304c\u30b9\u30bf\u30c3\u30afB\u304b\u3089\u30dd\u30c3\u30d7\u3055\u308c\u3001\u30b9\u30bf\u30c3\u30afA\u306b\u623b\u3055\u308c\u307e\u3059\u3002
        "},{"location":"chapter_tree/","title":"\u7b2c 7 \u7ae0 \u00a0 \u6728","text":"

        Abstract

        \u305d\u3073\u3048\u7acb\u3064\u6728\u306f\u6d3b\u529b\u306b\u6e80\u3061\u305f\u672c\u8cea\u3092\u653e\u3061\u3001\u6df1\u3044\u6839\u3068\u8c4a\u304b\u306a\u8449\u3092\u8a87\u308a\u306a\u304c\u3089\u3082\u3001\u305d\u306e\u679d\u306f\u758e\u3089\u306b\u6563\u3089\u3070\u308a\u3001\u5e7d\u7384\u306a\u96f0\u56f2\u6c17\u3092\u91b8\u3057\u51fa\u3057\u3066\u3044\u307e\u3059\u3002

        \u305d\u308c\u306f\u30c7\u30fc\u30bf\u306b\u304a\u3051\u308b\u5206\u5272\u7d71\u6cbb\u306e\u9bae\u3084\u304b\u306a\u5f62\u3092\u79c1\u305f\u3061\u306b\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        "},{"location":"chapter_tree/#_1","title":"\u7ae0\u306e\u5185\u5bb9","text":"
        • 7.1 \u00a0 \u4e8c\u5206\u6728
        • 7.2 \u00a0 \u4e8c\u5206\u6728\u306e\u8d70\u67fb
        • 7.3 \u00a0 \u6728\u306e\u914d\u5217\u8868\u73fe
        • 7.4 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728
        • 7.5 \u00a0 AVL\u6728 *
        • 7.6 \u00a0 \u307e\u3068\u3081
        "},{"location":"chapter_tree/array_representation_of_tree/","title":"7.3 \u00a0 \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe","text":"

        \u9023\u7d50\u30ea\u30b9\u30c8\u8868\u73fe\u3067\u306f\u3001\u4e8c\u5206\u6728\u306e\u683c\u7d0d\u5358\u4f4d\u306f\u30ce\u30fc\u30c9TreeNode\u3067\u3042\u308a\u3001\u30ce\u30fc\u30c9\u306f\u30dd\u30a4\u30f3\u30bf\u306b\u3088\u3063\u3066\u63a5\u7d9a\u3055\u308c\u307e\u3059\u3002\u9023\u7d50\u30ea\u30b9\u30c8\u8868\u73fe\u3067\u306e\u4e8c\u5206\u6728\u306e\u57fa\u672c\u64cd\u4f5c\u306b\u3064\u3044\u3066\u306f\u524d\u306e\u7bc0\u3067\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002

        \u3067\u306f\u3001\u914d\u5217\u3092\u4f7f\u3063\u3066\u4e8c\u5206\u6728\u3092\u8868\u73fe\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u308b\u3067\u3057\u3087\u3046\u304b\uff1f\u7b54\u3048\u306f\u30a4\u30a8\u30b9\u3067\u3059\u3002

        "},{"location":"chapter_tree/array_representation_of_tree/#731","title":"7.3.1 \u00a0 \u5b8c\u5168\u4e8c\u5206\u6728\u306e\u8868\u73fe","text":"

        \u307e\u305a\u7c21\u5358\u306a\u30b1\u30fc\u30b9\u304b\u3089\u5206\u6790\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u5b8c\u5168\u4e8c\u5206\u6728\u304c\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001\u30ec\u30d9\u30eb\u9806\u63a2\u7d22\u306e\u9806\u5e8f\u306b\u5f93\u3063\u3066\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3092\u914d\u5217\u306b\u683c\u7d0d\u3057\u3001\u5404\u30ce\u30fc\u30c9\u306f\u4e00\u610f\u306e\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002

        \u30ec\u30d9\u30eb\u9806\u63a2\u7d22\u306e\u7279\u6027\u306b\u57fa\u3065\u3044\u3066\u3001\u89aa\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3068\u305d\u306e\u5b50\u30ce\u30fc\u30c9\u306e\u9593\u306e\u300c\u30de\u30c3\u30d4\u30f3\u30b0\u516c\u5f0f\u300d\u3092\u5c0e\u304d\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\\(i\\)\u306e\u5834\u5408\u3001\u305d\u306e\u5de6\u306e\u5b50\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\\(2i + 1\\)\u3001\u53f3\u306e\u5b50\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\\(2i + 2\\)\u3067\u3059\u3002\u4e0b\u56f3\u306f\u3001\u3055\u307e\u3056\u307e\u306a\u30ce\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u9593\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u95a2\u4fc2\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 7-12 \u00a0 \u5b8c\u5168\u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe

        \u30de\u30c3\u30d4\u30f3\u30b0\u516c\u5f0f\u306f\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u30ce\u30fc\u30c9\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3068\u540c\u69d8\u306e\u5f79\u5272\u3092\u679c\u305f\u3057\u307e\u3059\u3002\u914d\u5217\u5185\u306e\u4efb\u610f\u306e\u30ce\u30fc\u30c9\u304c\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001\u30de\u30c3\u30d4\u30f3\u30b0\u516c\u5f0f\u3092\u4f7f\u7528\u3057\u3066\u305d\u306e\u5de6\uff08\u53f3\uff09\u306e\u5b50\u30ce\u30fc\u30c9\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u307e\u3059\u3002

        "},{"location":"chapter_tree/array_representation_of_tree/#732","title":"7.3.2 \u00a0 \u4efb\u610f\u306e\u4e8c\u5206\u6728\u306e\u8868\u73fe","text":"

        \u5b8c\u5168\u4e8c\u5206\u6728\u306f\u7279\u5225\u306a\u30b1\u30fc\u30b9\u3067\u3059\u3002\u4e8c\u5206\u6728\u306e\u4e2d\u9593\u30ec\u30d9\u30eb\u306b\u306f\u591a\u304f\u306eNone\u5024\u304c\u5b58\u5728\u3059\u308b\u3053\u3068\u304c\u3088\u304f\u3042\u308a\u307e\u3059\u3002\u30ec\u30d9\u30eb\u9806\u63a2\u7d22\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u306b\u306f\u3053\u308c\u3089\u306eNone\u5024\u304c\u542b\u307e\u308c\u306a\u3044\u305f\u3081\u3001\u3053\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3060\u3051\u306b\u4f9d\u5b58\u3057\u3066None\u5024\u306e\u6570\u3068\u5206\u5e03\u3092\u63a8\u6e2c\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002\u3064\u307e\u308a\u3001\u8907\u6570\u306e\u4e8c\u5206\u6728\u69cb\u9020\u304c\u540c\u3058\u30ec\u30d9\u30eb\u9806\u63a2\u7d22\u30b7\u30fc\u30b1\u30f3\u30b9\u3068\u4e00\u81f4\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5b8c\u5168\u3067\u306a\u3044\u4e8c\u5206\u6728\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u4e0a\u8a18\u306e\u914d\u5217\u8868\u73fe\u65b9\u6cd5\u306f\u5931\u6557\u3057\u307e\u3059\u3002

        \u56f3 7-13 \u00a0 \u30ec\u30d9\u30eb\u9806\u63a2\u7d22\u30b7\u30fc\u30b1\u30f3\u30b9\u304c\u8907\u6570\u306e\u4e8c\u5206\u6728\u306e\u53ef\u80fd\u6027\u306b\u5bfe\u5fdc

        \u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3001\u30ec\u30d9\u30eb\u9806\u63a2\u7d22\u30b7\u30fc\u30b1\u30f3\u30b9\u3067\u3059\u3079\u3066\u306eNone\u5024\u3092\u660e\u793a\u7684\u306b\u66f8\u304d\u51fa\u3059\u3053\u3068\u3092\u691c\u8a0e\u3067\u304d\u307e\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u3053\u306e\u51e6\u7406\u5f8c\u3001\u30ec\u30d9\u30eb\u9806\u63a2\u7d22\u30b7\u30fc\u30b1\u30f3\u30b9\u306f\u4e8c\u5206\u6728\u3092\u4e00\u610f\u306b\u8868\u73fe\u3067\u304d\u307e\u3059\u3002\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        # \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe\n# None\u3092\u4f7f\u7528\u3057\u3066\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u8868\u73fe\ntree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]\n
        /* \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe */\n// \u6700\u5927\u6574\u6570\u5024INT_MAX\u3092\u4f7f\u7528\u3057\u3066\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u30de\u30fc\u30af\nvector<int> tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
        /* \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe */\n// Integer\u30e9\u30c3\u30d1\u30fc\u30af\u30e9\u30b9\u3092\u4f7f\u7528\u3057\u3066null\u3067\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u30de\u30fc\u30af\nInteger[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };\n
        /* \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe */\n// nullable int (int?)\u3092\u4f7f\u7528\u3057\u3066null\u3067\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u30de\u30fc\u30af\nint?[] tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
        /* \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe */\n// any\u578b\u30b9\u30e9\u30a4\u30b9\u3092\u4f7f\u7528\u3057\u3066nil\u3067\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u30de\u30fc\u30af\ntree := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15}\n
        /* \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe */\n// optional Int (Int?)\u3092\u4f7f\u7528\u3057\u3066nil\u3067\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u30de\u30fc\u30af\nlet tree: [Int?] = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]\n
        /* \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe */\n// null\u3092\u4f7f\u7528\u3057\u3066\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u8868\u73fe\nlet tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
        /* \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe */\n// null\u3092\u4f7f\u7528\u3057\u3066\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u8868\u73fe\nlet tree: (number | null)[] = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
        /* \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe */\n// nullable int (int?)\u3092\u4f7f\u7528\u3057\u3066null\u3067\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u30de\u30fc\u30af\nList<int?> tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
        /* \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe */\n// None\u3092\u4f7f\u7528\u3057\u3066\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u30de\u30fc\u30af\nlet tree = [Some(1), Some(2), Some(3), Some(4), None, Some(6), Some(7), Some(8), Some(9), None, None, Some(12), None, None, Some(15)];\n
        /* \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe */\n// \u6700\u5927int\u5024\u3092\u4f7f\u7528\u3057\u3066\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u30de\u30fc\u30af\u3001\u3057\u305f\u304c\u3063\u3066\u30ce\u30fc\u30c9\u5024\u306fINT_MAX\u3067\u3042\u3063\u3066\u306f\u306a\u3089\u306a\u3044\nint tree[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
        /* \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe */\n// null\u3092\u4f7f\u7528\u3057\u3066\u7a7a\u306e\u30b9\u30ed\u30c3\u30c8\u3092\u8868\u73fe\nval tree = mutableListOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 )\n
        \n

        \u56f3 7-14 \u00a0 \u4efb\u610f\u306e\u7a2e\u985e\u306e\u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe

        \u6ce8\u76ee\u3059\u3079\u304d\u306f\u3001\u5b8c\u5099\u4e8c\u5206\u6728\u306f\u914d\u5217\u8868\u73fe\u306b\u975e\u5e38\u306b\u9069\u3057\u3066\u3044\u308b**\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002\u5b8c\u5099\u4e8c\u5206\u6728\u306e\u5b9a\u7fa9\u3092\u601d\u3044\u51fa\u3059\u3068\u3001None\u306f\u6700\u4e0b\u4f4d\u30ec\u30d9\u30eb\u3067\u306e\u307f\u3001\u304b\u3064\u53f3\u5074\u306b\u5411\u304b\u3063\u3066\u73fe\u308c\u307e\u3059\u3002**\u3064\u307e\u308a\u3001\u3059\u3079\u3066\u306eNone\u5024\u306f\u78ba\u5b9f\u306b\u30ec\u30d9\u30eb\u9806\u63a2\u7d22\u30b7\u30fc\u30b1\u30f3\u30b9\u306e\u6700\u5f8c\u306b\u73fe\u308c\u307e\u3059\u3002

        \u3053\u308c\u306f\u3001\u914d\u5217\u3092\u4f7f\u7528\u3057\u3066\u5b8c\u5099\u4e8c\u5206\u6728\u3092\u8868\u73fe\u3059\u308b\u969b\u3001\u3059\u3079\u3066\u306eNone\u5024\u306e\u683c\u7d0d\u3092\u7701\u7565\u3067\u304d\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u3001\u975e\u5e38\u306b\u4fbf\u5229\u3067\u3059\u3002\u4e0b\u56f3\u306b\u4f8b\u3092\u793a\u3057\u307e\u3059\u3002

        \u56f3 7-15 \u00a0 \u5b8c\u5099\u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe

        \u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u306f\u3001\u914d\u5217\u8868\u73fe\u306b\u57fa\u3065\u304f\u4e8c\u5206\u6728\u3092\u5b9f\u88c5\u3057\u3001\u6b21\u306e\u64cd\u4f5c\u3092\u542b\u307f\u307e\u3059\uff1a

        • \u30ce\u30fc\u30c9\u304c\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001\u305d\u306e\u5024\u3001\u5de6\uff08\u53f3\uff09\u306e\u5b50\u30ce\u30fc\u30c9\u3001\u304a\u3088\u3073\u89aa\u30ce\u30fc\u30c9\u3092\u53d6\u5f97\u3059\u308b\u3002
        • \u524d\u9806\u3001\u4e2d\u9806\u3001\u5f8c\u9806\u3001\u304a\u3088\u3073\u30ec\u30d9\u30eb\u9806\u63a2\u7d22\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u53d6\u5f97\u3059\u308b\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_binary_tree.py
        class ArrayBinaryTree:\n    \"\"\"\u914d\u5217\u30d9\u30fc\u30b9\u306e\u4e8c\u5206\u6728\u30af\u30e9\u30b9\"\"\"\n\n    def __init__(self, arr: list[int | None]):\n        \"\"\"\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\"\"\"\n        self._tree = list(arr)\n\n    def size(self):\n        \"\"\"\u30ea\u30b9\u30c8\u306e\u5bb9\u91cf\"\"\"\n        return len(self._tree)\n\n    def val(self, i: int) -> int | None:\n        \"\"\"\u30a4\u30f3\u30c7\u30c3\u30af\u30b9i\u306e\u30ce\u30fc\u30c9\u306e\u5024\u3092\u53d6\u5f97\"\"\"\n        # \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001None\u3092\u8fd4\u3057\u3001\u7a7a\u5e2d\u3092\u8868\u3059\n        if i < 0 or i >= self.size():\n            return None\n        return self._tree[i]\n\n    def left(self, i: int) -> int | None:\n        \"\"\"\u30a4\u30f3\u30c7\u30c3\u30af\u30b9i\u306e\u30ce\u30fc\u30c9\u306e\u5de6\u306e\u5b50\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\"\"\"\n        return 2 * i + 1\n\n    def right(self, i: int) -> int | None:\n        \"\"\"\u30a4\u30f3\u30c7\u30c3\u30af\u30b9i\u306e\u30ce\u30fc\u30c9\u306e\u53f3\u306e\u5b50\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\"\"\"\n        return 2 * i + 2\n\n    def parent(self, i: int) -> int | None:\n        \"\"\"\u30a4\u30f3\u30c7\u30c3\u30af\u30b9i\u306e\u30ce\u30fc\u30c9\u306e\u89aa\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\"\"\"\n        return (i - 1) // 2\n\n    def level_order(self) -> list[int]:\n        \"\"\"\u30ec\u30d9\u30eb\u9806\u8d70\u67fb\"\"\"\n        self.res = []\n        # \u914d\u5217\u3092\u8d70\u67fb\n        for i in range(self.size()):\n            if self.val(i) is not None:\n                self.res.append(self.val(i))\n        return self.res\n\n    def dfs(self, i: int, order: str):\n        \"\"\"\u6df1\u3055\u512a\u5148\u8d70\u67fb\"\"\"\n        if self.val(i) is None:\n            return\n        # \u524d\u9806\u8d70\u67fb\n        if order == \"pre\":\n            self.res.append(self.val(i))\n        self.dfs(self.left(i), order)\n        # \u4e2d\u9806\u8d70\u67fb\n        if order == \"in\":\n            self.res.append(self.val(i))\n        self.dfs(self.right(i), order)\n        # \u5f8c\u9806\u8d70\u67fb\n        if order == \"post\":\n            self.res.append(self.val(i))\n\n    def pre_order(self) -> list[int]:\n        \"\"\"\u524d\u9806\u8d70\u67fb\"\"\"\n        self.res = []\n        self.dfs(0, order=\"pre\")\n        return self.res\n\n    def in_order(self) -> list[int]:\n        \"\"\"\u4e2d\u9806\u8d70\u67fb\"\"\"\n        self.res = []\n        self.dfs(0, order=\"in\")\n        return self.res\n\n    def post_order(self) -> list[int]:\n        \"\"\"\u5f8c\u9806\u8d70\u67fb\"\"\"\n        self.res = []\n        self.dfs(0, order=\"post\")\n        return self.res\n
        array_binary_tree.cpp
        /* \u914d\u5217\u30d9\u30fc\u30b9\u306e\u4e8c\u5206\u6728\u30af\u30e9\u30b9 */\nclass ArrayBinaryTree {\n  public:\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    ArrayBinaryTree(vector<int> arr) {\n        tree = arr;\n    }\n\n    /* \u30ea\u30b9\u30c8\u306e\u5bb9\u91cf */\n    int size() {\n        return tree.size();\n    }\n\n    /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i \u306e\u30ce\u30fc\u30c9\u306e\u5024\u3092\u53d6\u5f97 */\n    int val(int i) {\n        // \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001INT_MAX \u3092\u8fd4\u3059\uff08null \u3092\u8868\u3059\uff09\n        if (i < 0 || i >= size())\n            return INT_MAX;\n        return tree[i];\n    }\n\n    /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i \u306e\u30ce\u30fc\u30c9\u306e\u5de6\u306e\u5b50\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\n    int left(int i) {\n        return 2 * i + 1;\n    }\n\n    /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i \u306e\u30ce\u30fc\u30c9\u306e\u53f3\u306e\u5b50\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\n    int right(int i) {\n        return 2 * i + 2;\n    }\n\n    /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i \u306e\u30ce\u30fc\u30c9\u306e\u89aa\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\n    int parent(int i) {\n        return (i - 1) / 2;\n    }\n\n    /* \u30ec\u30d9\u30eb\u9806\u8d70\u67fb */\n    vector<int> levelOrder() {\n        vector<int> res;\n        // \u914d\u5217\u3092\u8d70\u67fb\n        for (int i = 0; i < size(); i++) {\n            if (val(i) != INT_MAX)\n                res.push_back(val(i));\n        }\n        return res;\n    }\n\n    /* \u524d\u9806\u8d70\u67fb */\n    vector<int> preOrder() {\n        vector<int> res;\n        dfs(0, \"pre\", res);\n        return res;\n    }\n\n    /* \u4e2d\u9806\u8d70\u67fb */\n    vector<int> inOrder() {\n        vector<int> res;\n        dfs(0, \"in\", res);\n        return res;\n    }\n\n    /* \u5f8c\u9806\u8d70\u67fb */\n    vector<int> postOrder() {\n        vector<int> res;\n        dfs(0, \"post\", res);\n        return res;\n    }\n\n  private:\n    vector<int> tree;\n\n    /* \u6df1\u3055\u512a\u5148\u8d70\u67fb */\n    void dfs(int i, string order, vector<int> &res) {\n        // \u7a7a\u306e\u4f4d\u7f6e\u306e\u5834\u5408\u3001\u623b\u308b\n        if (val(i) == INT_MAX)\n            return;\n        // \u524d\u9806\u8d70\u67fb\n        if (order == \"pre\")\n            res.push_back(val(i));\n        dfs(left(i), order, res);\n        // \u4e2d\u9806\u8d70\u67fb\n        if (order == \"in\")\n            res.push_back(val(i));\n        dfs(right(i), order, res);\n        // \u5f8c\u9806\u8d70\u67fb\n        if (order == \"post\")\n            res.push_back(val(i));\n    }\n};\n
        array_binary_tree.java
        /* \u914d\u5217\u30d9\u30fc\u30b9\u306e\u4e8c\u5206\u6728\u30af\u30e9\u30b9 */\nclass ArrayBinaryTree {\n    private List<Integer> tree;\n\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    public ArrayBinaryTree(List<Integer> arr) {\n        tree = new ArrayList<>(arr);\n    }\n\n    /* \u30ea\u30b9\u30c8\u306e\u5bb9\u91cf */\n    public int size() {\n        return tree.size();\n    }\n\n    /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i \u306e\u30ce\u30fc\u30c9\u306e\u5024\u3092\u53d6\u5f97 */\n    public Integer val(int i) {\n        // \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7bc4\u56f2\u5916\u306e\u5834\u5408\u3001null \u3092\u8fd4\u3059\uff08\u7a7a\u306e\u4f4d\u7f6e\u3092\u8868\u3059\uff09\n        if (i < 0 || i >= size())\n            return null;\n        return tree.get(i);\n    }\n\n    /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i \u306e\u30ce\u30fc\u30c9\u306e\u5de6\u306e\u5b50\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\n    public Integer left(int i) {\n        return 2 * i + 1;\n    }\n\n    /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i \u306e\u30ce\u30fc\u30c9\u306e\u53f3\u306e\u5b50\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\n    public Integer right(int i) {\n        return 2 * i + 2;\n    }\n\n    /* \u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i \u306e\u30ce\u30fc\u30c9\u306e\u89aa\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97 */\n    public Integer parent(int i) {\n        return (i - 1) / 2;\n    }\n\n    /* \u30ec\u30d9\u30eb\u9806\u8d70\u67fb */\n    public List<Integer> levelOrder() {\n        List<Integer> res = new ArrayList<>();\n        // \u914d\u5217\u3092\u8d70\u67fb\n        for (int i = 0; i < size(); i++) {\n            if (val(i) != null)\n                res.add(val(i));\n        }\n        return res;\n    }\n\n    /* \u6df1\u3055\u512a\u5148\u8d70\u67fb */\n    private void dfs(Integer i, String order, List<Integer> res) {\n        // \u7a7a\u306e\u4f4d\u7f6e\u306e\u5834\u5408\u3001\u623b\u308b\n        if (val(i) == null)\n            return;\n        // \u524d\u9806\u8d70\u67fb\n        if (\"pre\".equals(order))\n            res.add(val(i));\n        dfs(left(i), order, res);\n        // \u4e2d\u9806\u8d70\u67fb\n        if (\"in\".equals(order))\n            res.add(val(i));\n        dfs(right(i), order, res);\n        // \u5f8c\u9806\u8d70\u67fb\n        if (\"post\".equals(order))\n            res.add(val(i));\n    }\n\n    /* \u524d\u9806\u8d70\u67fb */\n    public List<Integer> preOrder() {\n        List<Integer> res = new ArrayList<>();\n        dfs(0, \"pre\", res);\n        return res;\n    }\n\n    /* \u4e2d\u9806\u8d70\u67fb */\n    public List<Integer> inOrder() {\n        List<Integer> res = new ArrayList<>();\n        dfs(0, \"in\", res);\n        return res;\n    }\n\n    /* \u5f8c\u9806\u8d70\u67fb */\n    public List<Integer> postOrder() {\n        List<Integer> res = new ArrayList<>();\n        dfs(0, \"post\", res);\n        return res;\n    }\n}\n
        array_binary_tree.cs
        [class]{ArrayBinaryTree}-[func]{}\n
        array_binary_tree.go
        [class]{arrayBinaryTree}-[func]{}\n
        array_binary_tree.swift
        [class]{ArrayBinaryTree}-[func]{}\n
        array_binary_tree.js
        [class]{ArrayBinaryTree}-[func]{}\n
        array_binary_tree.ts
        [class]{ArrayBinaryTree}-[func]{}\n
        array_binary_tree.dart
        [class]{ArrayBinaryTree}-[func]{}\n
        array_binary_tree.rs
        [class]{ArrayBinaryTree}-[func]{}\n
        array_binary_tree.c
        [class]{ArrayBinaryTree}-[func]{}\n
        array_binary_tree.kt
        [class]{ArrayBinaryTree}-[func]{}\n
        array_binary_tree.rb
        [class]{ArrayBinaryTree}-[func]{}\n
        "},{"location":"chapter_tree/array_representation_of_tree/#733","title":"7.3.3 \u00a0 \u5229\u70b9\u3068\u5236\u9650","text":"

        \u4e8c\u5206\u6728\u306e\u914d\u5217\u8868\u73fe\u306b\u306f\u4ee5\u4e0b\u306e\u5229\u70b9\u304c\u3042\u308a\u307e\u3059\uff1a

        • \u914d\u5217\u306f\u9023\u7d9a\u3057\u305f\u30e1\u30e2\u30ea\u7a7a\u9593\u306b\u683c\u7d0d\u3055\u308c\u308b\u305f\u3081\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u30d5\u30ec\u30f3\u30c9\u30ea\u30fc\u3067\u3001\u3088\u308a\u9ad8\u901f\u306a\u30a2\u30af\u30bb\u30b9\u3068\u63a2\u7d22\u304c\u53ef\u80fd\u3067\u3059\u3002
        • \u30dd\u30a4\u30f3\u30bf\u3092\u683c\u7d0d\u3059\u308b\u5fc5\u8981\u304c\u306a\u3044\u305f\u3081\u3001\u30b9\u30da\u30fc\u30b9\u3092\u7bc0\u7d04\u3067\u304d\u307e\u3059\u3002
        • \u30ce\u30fc\u30c9\u3078\u306e\u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\u304c\u53ef\u80fd\u3067\u3059\u3002

        \u3057\u304b\u3057\u3001\u914d\u5217\u8868\u73fe\u306b\u306f\u3044\u304f\u3064\u304b\u306e\u5236\u9650\u3082\u3042\u308a\u307e\u3059\uff1a

        • \u914d\u5217\u683c\u7d0d\u306b\u306f\u9023\u7d9a\u3057\u305f\u30e1\u30e2\u30ea\u7a7a\u9593\u304c\u5fc5\u8981\u306a\u305f\u3081\u3001\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u6301\u3064\u6728\u306e\u683c\u7d0d\u306b\u306f\u9069\u3057\u3066\u3044\u307e\u305b\u3093\u3002
        • \u30ce\u30fc\u30c9\u306e\u8ffd\u52a0\u3084\u524a\u9664\u306b\u306f\u914d\u5217\u306e\u633f\u5165\u3084\u524a\u9664\u64cd\u4f5c\u304c\u5fc5\u8981\u3067\u3001\u52b9\u7387\u304c\u4f4e\u304f\u306a\u308a\u307e\u3059\u3002
        • \u4e8c\u5206\u6728\u306b\u591a\u304f\u306eNone\u5024\u304c\u3042\u308b\u5834\u5408\u3001\u914d\u5217\u306b\u542b\u307e\u308c\u308b\u30ce\u30fc\u30c9\u30c7\u30fc\u30bf\u306e\u5272\u5408\u304c\u4f4e\u304f\u306a\u308a\u3001\u7a7a\u9593\u5229\u7528\u7387\u304c\u4f4e\u4e0b\u3057\u307e\u3059\u3002
        "},{"location":"chapter_tree/avl_tree/","title":"7.5 \u00a0 AVL\u6728 *","text":"

        \u300c\u4e8c\u5206\u63a2\u7d22\u6728\u300d\u306e\u7bc0\u3067\u306f\u3001\u8907\u6570\u306e\u633f\u5165\u3068\u524a\u9664\u306e\u5f8c\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u304c\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u9000\u5316\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3053\u3068\u3092\u8ff0\u3079\u307e\u3057\u305f\u3002\u3053\u306e\u3088\u3046\u306a\u5834\u5408\u3001\u3059\u3079\u3066\u306e\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(\\log n)\\)\u304b\u3089\\(O(n)\\)\u306b\u60aa\u5316\u3057\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u30012\u3064\u306e\u30ce\u30fc\u30c9\u524a\u9664\u64cd\u4f5c\u306e\u5f8c\u3001\u3053\u306e\u4e8c\u5206\u63a2\u7d22\u6728\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u9000\u5316\u3057\u307e\u3059\u3002

        \u56f3 7-24 \u00a0 \u30ce\u30fc\u30c9\u524a\u9664\u5f8c\u306eAVL\u6728\u306e\u9000\u5316

        \u4f8b\u3048\u3070\u3001\u4e0b\u56f3\u306b\u793a\u3059\u5b8c\u5168\u4e8c\u5206\u6728\u3067\u306f\u30012\u3064\u306e\u30ce\u30fc\u30c9\u3092\u633f\u5165\u3057\u305f\u5f8c\u3001\u6728\u304c\u5de6\u306b\u5927\u304d\u304f\u50be\u304d\u3001\u691c\u7d22\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3082\u60aa\u5316\u3057\u307e\u3059\u3002

        \u56f3 7-25 \u00a0 \u30ce\u30fc\u30c9\u633f\u5165\u5f8c\u306eAVL\u6728\u306e\u9000\u5316

        1962\u5e74\u3001G. M. Adelson-Velsky\u3068E. M. Landis\u304c\u8ad6\u6587\u300cAn algorithm for the organization of information\u300d\u3067AVL\u6728\u3092\u63d0\u6848\u3057\u307e\u3057\u305f\u3002\u3053\u306e\u8ad6\u6587\u3067\u306f\u3001\u30ce\u30fc\u30c9\u306e\u7d99\u7d9a\u7684\u306a\u8ffd\u52a0\u3068\u524a\u9664\u306e\u5f8c\u3082AVL\u6728\u304c\u9000\u5316\u3057\u306a\u3044\u3053\u3068\u3092\u4fdd\u8a3c\u3059\u308b\u4e00\u9023\u306e\u64cd\u4f5c\u306b\u3064\u3044\u3066\u8a73\u8ff0\u3057\u3001\u3055\u307e\u3056\u307e\u306a\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3092\\(O(\\log n)\\)\u30ec\u30d9\u30eb\u306b\u7dad\u6301\u3057\u307e\u3057\u305f\u3002\u3064\u307e\u308a\u3001\u983b\u7e41\u306a\u8ffd\u52a0\u3001\u524a\u9664\u3001\u691c\u7d22\u3001\u5909\u66f4\u304c\u5fc5\u8981\u306a\u30b7\u30ca\u30ea\u30aa\u3067\u3001AVL\u6728\u306f\u5e38\u306b\u52b9\u7387\u7684\u306a\u30c7\u30fc\u30bf\u64cd\u4f5c\u6027\u80fd\u3092\u7dad\u6301\u3067\u304d\u3001\u5927\u304d\u306a\u5fdc\u7528\u4fa1\u5024\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_tree/avl_tree/#751-avl","title":"7.5.1 \u00a0 AVL\u6728\u306e\u4e00\u822c\u7684\u306a\u7528\u8a9e","text":"

        AVL\u6728\u306f\u4e8c\u5206\u63a2\u7d22\u6728\u3067\u3042\u308a\u304b\u3064\u5e73\u8861\u4e8c\u5206\u6728\u3067\u3082\u3042\u308a\u3001\u3053\u308c\u30892\u3064\u306e\u7a2e\u985e\u306e\u4e8c\u5206\u6728\u306e\u3059\u3079\u3066\u306e\u6027\u8cea\u3092\u6e80\u305f\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u5e73\u8861\u4e8c\u5206\u63a2\u7d22\u6728\u3067\u3059\u3002

        "},{"location":"chapter_tree/avl_tree/#1","title":"1. \u00a0 \u30ce\u30fc\u30c9\u306e\u9ad8\u3055","text":"

        AVL\u6728\u306b\u95a2\u9023\u3059\u308b\u64cd\u4f5c\u3067\u306f\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u53d6\u5f97\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u30ce\u30fc\u30c9\u30af\u30e9\u30b9\u306bheight\u5909\u6570\u3092\u8ffd\u52a0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        class TreeNode:\n    \"\"\"AVL\u6728\u30ce\u30fc\u30c9\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val                 # \u30ce\u30fc\u30c9\u5024\n        self.height: int = 0                # \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n        self.left: TreeNode | None = None   # \u5de6\u306e\u5b50\u3078\u306e\u53c2\u7167\n        self.right: TreeNode | None = None  # \u53f3\u306e\u5b50\u3078\u306e\u53c2\u7167\n
        /* AVL\u6728\u30ce\u30fc\u30c9 */\nstruct TreeNode {\n    int val{};          // \u30ce\u30fc\u30c9\u5024\n    int height = 0;     // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n    TreeNode *left{};   // \u5de6\u306e\u5b50\n    TreeNode *right{};  // \u53f3\u306e\u5b50\n    TreeNode() = default;\n    explicit TreeNode(int x) : val(x){}\n};\n
        /* AVL\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode {\n    public int val;        // \u30ce\u30fc\u30c9\u5024\n    public int height;     // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n    public TreeNode left;  // \u5de6\u306e\u5b50\n    public TreeNode right; // \u53f3\u306e\u5b50\n    public TreeNode(int x) { val = x; }\n}\n
        /* AVL\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode(int? x) {\n    public int? val = x;    // \u30ce\u30fc\u30c9\u5024\n    public int height;      // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n    public TreeNode? left;  // \u5de6\u306e\u5b50\u3078\u306e\u53c2\u7167\n    public TreeNode? right; // \u53f3\u306e\u5b50\u3078\u306e\u53c2\u7167\n}\n
        /* AVL\u6728\u30ce\u30fc\u30c9 */\ntype TreeNode struct {\n    Val    int       // \u30ce\u30fc\u30c9\u5024\n    Height int       // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n    Left   *TreeNode // \u5de6\u306e\u5b50\u3078\u306e\u53c2\u7167\n    Right  *TreeNode // \u53f3\u306e\u5b50\u3078\u306e\u53c2\u7167\n}\n
        /* AVL\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode {\n    var val: Int // \u30ce\u30fc\u30c9\u5024\n    var height: Int // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n    var left: TreeNode? // \u5de6\u306e\u5b50\n    var right: TreeNode? // \u53f3\u306e\u5b50\n\n    init(x: Int) {\n        val = x\n        height = 0\n    }\n}\n
        /* AVL\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode {\n    val; // \u30ce\u30fc\u30c9\u5024\n    height; // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n    left; // \u5de6\u306e\u5b50\u30dd\u30a4\u30f3\u30bf\n    right; // \u53f3\u306e\u5b50\u30dd\u30a4\u30f3\u30bf\n    constructor(val, left, right, height) {\n        this.val = val === undefined ? 0 : val;\n        this.height = height === undefined ? 0 : height;\n        this.left = left === undefined ? null : left;\n        this.right = right === undefined ? null : right;\n    }\n}\n
        /* AVL\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode {\n    val: number;            // \u30ce\u30fc\u30c9\u5024\n    height: number;         // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n    left: TreeNode | null;  // \u5de6\u306e\u5b50\u30dd\u30a4\u30f3\u30bf\n    right: TreeNode | null; // \u53f3\u306e\u5b50\u30dd\u30a4\u30f3\u30bf\n    constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {\n        this.val = val === undefined ? 0 : val;\n        this.height = height === undefined ? 0 : height;\n        this.left = left === undefined ? null : left;\n        this.right = right === undefined ? null : right;\n    }\n}\n
        /* AVL\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode {\n  int val;         // \u30ce\u30fc\u30c9\u5024\n  int height;      // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n  TreeNode? left;  // \u5de6\u306e\u5b50\n  TreeNode? right; // \u53f3\u306e\u5b50\n  TreeNode(this.val, [this.height = 0, this.left, this.right]);\n}\n
        use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* AVL\u6728\u30ce\u30fc\u30c9 */\nstruct TreeNode {\n    val: i32,                               // \u30ce\u30fc\u30c9\u5024\n    height: i32,                            // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n    left: Option<Rc<RefCell<TreeNode>>>,    // \u5de6\u306e\u5b50\n    right: Option<Rc<RefCell<TreeNode>>>,   // \u53f3\u306e\u5b50\n}\n\nimpl TreeNode {\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    fn new(val: i32) -> Rc<RefCell<Self>> {\n        Rc::new(RefCell::new(Self {\n            val,\n            height: 0,\n            left: None,\n            right: None\n        }))\n    }\n}\n
        /* AVL\u6728\u30ce\u30fc\u30c9 */\nTreeNode struct TreeNode {\n    int val;\n    int height;\n    struct TreeNode *left;\n    struct TreeNode *right;\n} TreeNode;\n\n/* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\nTreeNode *newTreeNode(int val) {\n    TreeNode *node;\n\n    node = (TreeNode *)malloc(sizeof(TreeNode));\n    node->val = val;\n    node->height = 0;\n    node->left = NULL;\n    node->right = NULL;\n    return node;\n}\n
        /* AVL\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode(val _val: Int) {  // \u30ce\u30fc\u30c9\u5024\n    val height: Int = 0          // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n    val left: TreeNode? = null   // \u5de6\u306e\u5b50\n    val right: TreeNode? = null  // \u53f3\u306e\u5b50\n}\n
        \n

        \u300c\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u300d\u3068\u306f\u3001\u305d\u306e\u30ce\u30fc\u30c9\u304b\u3089\u6700\u3082\u9060\u3044\u8449\u30ce\u30fc\u30c9\u307e\u3067\u306e\u8ddd\u96e2\u3001\u3064\u307e\u308a\u901a\u904e\u3059\u308b\u300c\u8fba\u300d\u306e\u6570\u3092\u6307\u3057\u307e\u3059\u3002\u91cd\u8981\u306a\u306e\u306f\u3001\u8449\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u306f\\(0\\)\u3067\u3001null\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u306f\\(-1\\)\u3067\u3042\u308b\u3053\u3068\u3067\u3059\u3002\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u53d6\u5f97\u3057\u3001\u66f4\u65b0\u3059\u308b\u305f\u3081\u306e2\u3064\u306e\u30e6\u30fc\u30c6\u30a3\u30ea\u30c6\u30a3\u95a2\u6570\u3092\u4f5c\u6210\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def height(self, node: TreeNode | None) -> int:\n    \"\"\"\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u53d6\u5f97\"\"\"\n    # \u7a7a\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u306f-1\u3001\u8449\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u306f0\n    if node is not None:\n        return node.height\n    return -1\n\ndef update_height(self, node: TreeNode | None):\n    \"\"\"\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\"\"\"\n    # \u30ce\u30fc\u30c9\u306e\u9ad8\u3055 = \u6700\u3082\u9ad8\u3044\u90e8\u5206\u6728\u306e\u9ad8\u3055 + 1\n    node.height = max([self.height(node.left), self.height(node.right)]) + 1\n
        avl_tree.cpp
        /* \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u53d6\u5f97 */\nint height(TreeNode *node) {\n    // \u7a7a\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u306f-1\u3001\u8449\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u306f0\n    return node == nullptr ? -1 : node->height;\n}\n\n/* \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0 */\nvoid updateHeight(TreeNode *node) {\n    // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055 = \u6700\u3082\u9ad8\u3044\u90e8\u5206\u6728\u306e\u9ad8\u3055 + 1\n    node->height = max(height(node->left), height(node->right)) + 1;\n}\n
        avl_tree.java
        /* \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u53d6\u5f97 */\nint height(TreeNode node) {\n    // \u7a7a\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u306f -1\u3001\u8449\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u306f 0\n    return node == null ? -1 : node.height;\n}\n\n/* \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0 */\nvoid updateHeight(TreeNode node) {\n    // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u306f\u6700\u3082\u9ad8\u3044\u90e8\u5206\u6728\u306e\u9ad8\u3055 + 1\n    node.height = Math.max(height(node.left), height(node.right)) + 1;\n}\n
        avl_tree.cs
        [class]{AVLTree}-[func]{Height}\n\n[class]{AVLTree}-[func]{UpdateHeight}\n
        avl_tree.go
        [class]{aVLTree}-[func]{height}\n\n[class]{aVLTree}-[func]{updateHeight}\n
        avl_tree.swift
        [class]{AVLTree}-[func]{height}\n\n[class]{AVLTree}-[func]{updateHeight}\n
        avl_tree.js
        [class]{AVLTree}-[func]{height}\n\n[class]{AVLTree}-[func]{updateHeight}\n
        avl_tree.ts
        [class]{AVLTree}-[func]{height}\n\n[class]{AVLTree}-[func]{updateHeight}\n
        avl_tree.dart
        [class]{AVLTree}-[func]{height}\n\n[class]{AVLTree}-[func]{updateHeight}\n
        avl_tree.rs
        [class]{AVLTree}-[func]{height}\n\n[class]{AVLTree}-[func]{update_height}\n
        avl_tree.c
        [class]{}-[func]{height}\n\n[class]{}-[func]{updateHeight}\n
        avl_tree.kt
        [class]{AVLTree}-[func]{height}\n\n[class]{AVLTree}-[func]{updateHeight}\n
        avl_tree.rb
        [class]{AVLTree}-[func]{height}\n\n[class]{AVLTree}-[func]{update_height}\n
        "},{"location":"chapter_tree/avl_tree/#2","title":"2. \u00a0 \u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50","text":"

        \u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50\u306f\u3001\u305d\u306e\u30ce\u30fc\u30c9\u306e\u5de6\u90e8\u5206\u6728\u306e\u9ad8\u3055\u304b\u3089\u53f3\u90e8\u5206\u6728\u306e\u9ad8\u3055\u3092\u5f15\u3044\u305f\u5024\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u3001null\u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50\u306f\\(0\\)\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u307e\u3059\u3002\u5f8c\u3067\u4f7f\u3044\u3084\u3059\u304f\u3059\u308b\u305f\u3081\u3001\u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50\u3092\u53d6\u5f97\u3059\u308b\u6a5f\u80fd\u3082\u95a2\u6570\u306b\u30ab\u30d7\u30bb\u30eb\u5316\u3057\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def balance_factor(self, node: TreeNode | None) -> int:\n    \"\"\"\u30d0\u30e9\u30f3\u30b9\u56e0\u5b50\u3092\u53d6\u5f97\"\"\"\n    # \u7a7a\u30ce\u30fc\u30c9\u306e\u30d0\u30e9\u30f3\u30b9\u56e0\u5b50\u306f0\n    if node is None:\n        return 0\n    # \u30ce\u30fc\u30c9\u306e\u30d0\u30e9\u30f3\u30b9\u56e0\u5b50 = \u5de6\u90e8\u5206\u6728\u306e\u9ad8\u3055 - \u53f3\u90e8\u5206\u6728\u306e\u9ad8\u3055\n    return self.height(node.left) - self.height(node.right)\n
        avl_tree.cpp
        /* \u5e73\u8861\u56e0\u5b50\u3092\u53d6\u5f97 */\nint balanceFactor(TreeNode *node) {\n    // \u7a7a\u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50\u306f0\n    if (node == nullptr)\n        return 0;\n    // \u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50 = \u5de6\u90e8\u5206\u6728\u306e\u9ad8\u3055 - \u53f3\u90e8\u5206\u6728\u306e\u9ad8\u3055\n    return height(node->left) - height(node->right);\n}\n
        avl_tree.java
        /* \u5e73\u8861\u56e0\u5b50\u3092\u53d6\u5f97 */\nint balanceFactor(TreeNode node) {\n    // \u7a7a\u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50\u306f 0\n    if (node == null)\n        return 0;\n    // \u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50 = \u5de6\u90e8\u5206\u6728\u306e\u9ad8\u3055 - \u53f3\u90e8\u5206\u6728\u306e\u9ad8\u3055\n    return height(node.left) - height(node.right);\n}\n
        avl_tree.cs
        [class]{AVLTree}-[func]{BalanceFactor}\n
        avl_tree.go
        [class]{aVLTree}-[func]{balanceFactor}\n
        avl_tree.swift
        [class]{AVLTree}-[func]{balanceFactor}\n
        avl_tree.js
        [class]{AVLTree}-[func]{balanceFactor}\n
        avl_tree.ts
        [class]{AVLTree}-[func]{balanceFactor}\n
        avl_tree.dart
        [class]{AVLTree}-[func]{balanceFactor}\n
        avl_tree.rs
        [class]{AVLTree}-[func]{balance_factor}\n
        avl_tree.c
        [class]{}-[func]{balanceFactor}\n
        avl_tree.kt
        [class]{AVLTree}-[func]{balanceFactor}\n
        avl_tree.rb
        [class]{AVLTree}-[func]{balance_factor}\n

        Tip

        \u5e73\u8861\u56e0\u5b50\u3092\\(f\\)\u3068\u3059\u308b\u3068\u3001AVL\u6728\u306e\u4efb\u610f\u306e\u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50\u306f\\(-1 \\le f \\le 1\\)\u3092\u6e80\u305f\u3057\u307e\u3059\u3002

        "},{"location":"chapter_tree/avl_tree/#752-avl","title":"7.5.2 \u00a0 AVL\u6728\u306e\u56de\u8ee2","text":"

        AVL\u6728\u306e\u7279\u5fb4\u7684\u306a\u6a5f\u80fd\u306f\u300c\u56de\u8ee2\u300d\u64cd\u4f5c\u3067\u3001\u3053\u308c\u306f\u4e8c\u5206\u6728\u306e\u4e2d\u9806\u63a2\u7d22\u30b7\u30fc\u30b1\u30f3\u30b9\u306b\u5f71\u97ff\u3092\u4e0e\u3048\u308b\u3053\u3068\u306a\u304f\u3001\u4e0d\u5e73\u8861\u306a\u30ce\u30fc\u30c9\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u56de\u5fa9\u3067\u304d\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u56de\u8ee2\u64cd\u4f5c\u306f\u300c\u4e8c\u5206\u63a2\u7d22\u6728\u300d\u306e\u6027\u8cea\u3092\u7dad\u6301\u3057\u306a\u304c\u3089\u3001\u6728\u3092\u300c\u5e73\u8861\u4e8c\u5206\u6728\u300d\u306b\u623b\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u7d76\u5bfe\u5e73\u8861\u56e0\u5b50\u304c\\(> 1\\)\u306e\u30ce\u30fc\u30c9\u3092\u300c\u4e0d\u5e73\u8861\u30ce\u30fc\u30c9\u300d\u3068\u547c\u3073\u307e\u3059\u3002\u4e0d\u5e73\u8861\u306e\u30bf\u30a4\u30d7\u306b\u5fdc\u3058\u3066\u30014\u7a2e\u985e\u306e\u56de\u8ee2\u304c\u3042\u308a\u307e\u3059\uff1a\u53f3\u56de\u8ee2\u3001\u5de6\u56de\u8ee2\u3001\u53f3\u5de6\u56de\u8ee2\u3001\u5de6\u53f3\u56de\u8ee2\u3067\u3059\u3002\u4ee5\u4e0b\u3001\u3053\u308c\u3089\u306e\u56de\u8ee2\u64cd\u4f5c\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u8aac\u660e\u3057\u307e\u3059\u3002

        "},{"location":"chapter_tree/avl_tree/#1_1","title":"1. \u00a0 \u53f3\u56de\u8ee2","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u4e8c\u5206\u6728\u3067\u4e0b\u304b\u3089\u4e0a\u3078\u306e\u6700\u521d\u306e\u4e0d\u5e73\u8861\u30ce\u30fc\u30c9\u306f\u300c\u30ce\u30fc\u30c93\u300d\u3067\u3059\u3002\u3053\u306e\u4e0d\u5e73\u8861\u30ce\u30fc\u30c9\u3092\u6839\u3068\u3059\u308b\u90e8\u5206\u6728\u306b\u7126\u70b9\u3092\u5f53\u3066\u3001\u3053\u308c\u3092node\u3068\u3057\u3001\u305d\u306e\u5de6\u306e\u5b50\u3092child\u3068\u3057\u3066\u3001\u300c\u53f3\u56de\u8ee2\u300d\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002\u53f3\u56de\u8ee2\u5f8c\u3001\u90e8\u5206\u6728\u306f\u518d\u3073\u30d0\u30e9\u30f3\u30b9\u304c\u53d6\u308c\u3001\u540c\u6642\u306b\u4e8c\u5206\u63a2\u7d22\u6728\u306e\u6027\u8cea\u3082\u7dad\u6301\u3055\u308c\u307e\u3059\u3002

        <1><2><3><4>

        \u56f3 7-26 \u00a0 \u53f3\u56de\u8ee2\u306e\u624b\u9806

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001child\u30ce\u30fc\u30c9\u306b\u53f3\u306e\u5b50\uff08grand_child\u3068\u8868\u8a18\uff09\u304c\u3042\u308b\u5834\u5408\u3001\u53f3\u56de\u8ee2\u3067\u624b\u9806\u3092\u8ffd\u52a0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff1agrand_child\u3092node\u306e\u5de6\u306e\u5b50\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002

        \u56f3 7-27 \u00a0 grand_child\u304c\u3042\u308b\u53f3\u56de\u8ee2

        \u300c\u53f3\u56de\u8ee2\u300d\u306f\u6bd4\u55a9\u7684\u306a\u7528\u8a9e\u3067\u3001\u5b9f\u969b\u306b\u306f\u30ce\u30fc\u30c9\u30dd\u30a4\u30f3\u30bf\u3092\u5909\u66f4\u3059\u308b\u3053\u3068\u3067\u5b9f\u73fe\u3055\u308c\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3067\u793a\u3055\u308c\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def right_rotate(self, node: TreeNode | None) -> TreeNode | None:\n    \"\"\"\u53f3\u56de\u8ee2\u64cd\u4f5c\"\"\"\n    child = node.left\n    grand_child = child.right\n    # child\u3092\u4e2d\u5fc3\u306bnode\u3092\u53f3\u306b\u56de\u8ee2\n    child.right = node\n    node.left = grand_child\n    # \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    self.update_height(node)\n    self.update_height(child)\n    # \u56de\u8ee2\u5f8c\u306e\u90e8\u5206\u6728\u306e\u30eb\u30fc\u30c8\u3092\u8fd4\u3059\n    return child\n
        avl_tree.cpp
        /* \u53f3\u56de\u8ee2\u64cd\u4f5c */\nTreeNode *rightRotate(TreeNode *node) {\n    TreeNode *child = node->left;\n    TreeNode *grandChild = child->right;\n    // child\u3092\u4e2d\u5fc3\u306bnode\u3092\u53f3\u306b\u56de\u8ee2\n    child->right = node;\n    node->left = grandChild;\n    // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    updateHeight(node);\n    updateHeight(child);\n    // \u56de\u8ee2\u5f8c\u306e\u90e8\u5206\u6728\u306e\u30eb\u30fc\u30c8\u3092\u8fd4\u3059\n    return child;\n}\n
        avl_tree.java
        /* \u53f3\u56de\u8ee2\u64cd\u4f5c */\nTreeNode rightRotate(TreeNode node) {\n    TreeNode child = node.left;\n    TreeNode grandChild = child.right;\n    // child \u3092\u8ef8\u3068\u3057\u3066 node \u3092\u53f3\u306b\u56de\u8ee2\n    child.right = node;\n    node.left = grandChild;\n    // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    updateHeight(node);\n    updateHeight(child);\n    // \u56de\u8ee2\u5f8c\u306e\u90e8\u5206\u6728\u306e\u6839\u3092\u8fd4\u3059\n    return child;\n}\n
        avl_tree.cs
        [class]{AVLTree}-[func]{RightRotate}\n
        avl_tree.go
        [class]{aVLTree}-[func]{rightRotate}\n
        avl_tree.swift
        [class]{AVLTree}-[func]{rightRotate}\n
        avl_tree.js
        [class]{AVLTree}-[func]{rightRotate}\n
        avl_tree.ts
        [class]{AVLTree}-[func]{rightRotate}\n
        avl_tree.dart
        [class]{AVLTree}-[func]{rightRotate}\n
        avl_tree.rs
        [class]{AVLTree}-[func]{right_rotate}\n
        avl_tree.c
        [class]{}-[func]{rightRotate}\n
        avl_tree.kt
        [class]{AVLTree}-[func]{rightRotate}\n
        avl_tree.rb
        [class]{AVLTree}-[func]{right_rotate}\n
        "},{"location":"chapter_tree/avl_tree/#2_1","title":"2. \u00a0 \u5de6\u56de\u8ee2","text":"

        \u5bfe\u5fdc\u3057\u3066\u3001\u4e0a\u8a18\u306e\u4e0d\u5e73\u8861\u4e8c\u5206\u6728\u306e\u300c\u93e1\u50cf\u300d\u3092\u8003\u616e\u3059\u308b\u3068\u3001\u4e0b\u56f3\u306b\u793a\u3059\u300c\u5de6\u56de\u8ee2\u300d\u64cd\u4f5c\u3092\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u56f3 7-28 \u00a0 \u5de6\u56de\u8ee2\u64cd\u4f5c

        \u540c\u69d8\u306b\u3001\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001child\u30ce\u30fc\u30c9\u306b\u5de6\u306e\u5b50\uff08grand_child\u3068\u8868\u8a18\uff09\u304c\u3042\u308b\u5834\u5408\u3001\u5de6\u56de\u8ee2\u3067\u624b\u9806\u3092\u8ffd\u52a0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff1agrand_child\u3092node\u306e\u53f3\u306e\u5b50\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002

        \u56f3 7-29 \u00a0 grand_child\u304c\u3042\u308b\u5de6\u56de\u8ee2

        **\u53f3\u56de\u8ee2\u3068\u5de6\u56de\u8ee2\u306e\u64cd\u4f5c\u306f\u8ad6\u7406\u7684\u306b\u5bfe\u79f0\u3067\u3042\u308a\u30012\u3064\u306e\u5bfe\u79f0\u7684\u306a\u4e0d\u5e73\u8861\u30bf\u30a4\u30d7\u3092\u89e3\u6c7a\u3057\u307e\u3059**\u3053\u3068\u304c\u89b3\u5bdf\u3067\u304d\u307e\u3059\u3002\u5bfe\u79f0\u6027\u306b\u57fa\u3065\u3044\u3066\u3001\u53f3\u56de\u8ee2\u306e\u5b9f\u88c5\u30b3\u30fc\u30c9\u3067\u3059\u3079\u3066\u306eleft\u3092right\u306b\u3001\u3059\u3079\u3066\u306eright\u3092left\u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u3067\u3001\u5de6\u56de\u8ee2\u306e\u5b9f\u88c5\u30b3\u30fc\u30c9\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def left_rotate(self, node: TreeNode | None) -> TreeNode | None:\n    \"\"\"\u5de6\u56de\u8ee2\u64cd\u4f5c\"\"\"\n    child = node.right\n    grand_child = child.left\n    # child\u3092\u4e2d\u5fc3\u306bnode\u3092\u5de6\u306b\u56de\u8ee2\n    child.left = node\n    node.right = grand_child\n    # \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    self.update_height(node)\n    self.update_height(child)\n    # \u56de\u8ee2\u5f8c\u306e\u90e8\u5206\u6728\u306e\u30eb\u30fc\u30c8\u3092\u8fd4\u3059\n    return child\n
        avl_tree.cpp
        /* \u5de6\u56de\u8ee2\u64cd\u4f5c */\nTreeNode *leftRotate(TreeNode *node) {\n    TreeNode *child = node->right;\n    TreeNode *grandChild = child->left;\n    // child\u3092\u4e2d\u5fc3\u306bnode\u3092\u5de6\u306b\u56de\u8ee2\n    child->left = node;\n    node->right = grandChild;\n    // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    updateHeight(node);\n    updateHeight(child);\n    // \u56de\u8ee2\u5f8c\u306e\u90e8\u5206\u6728\u306e\u30eb\u30fc\u30c8\u3092\u8fd4\u3059\n    return child;\n}\n
        avl_tree.java
        /* \u5de6\u56de\u8ee2\u64cd\u4f5c */\nTreeNode leftRotate(TreeNode node) {\n    TreeNode child = node.right;\n    TreeNode grandChild = child.left;\n    // child \u3092\u8ef8\u3068\u3057\u3066 node \u3092\u5de6\u306b\u56de\u8ee2\n    child.left = node;\n    node.right = grandChild;\n    // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    updateHeight(node);\n    updateHeight(child);\n    // \u56de\u8ee2\u5f8c\u306e\u90e8\u5206\u6728\u306e\u6839\u3092\u8fd4\u3059\n    return child;\n}\n
        avl_tree.cs
        [class]{AVLTree}-[func]{LeftRotate}\n
        avl_tree.go
        [class]{aVLTree}-[func]{leftRotate}\n
        avl_tree.swift
        [class]{AVLTree}-[func]{leftRotate}\n
        avl_tree.js
        [class]{AVLTree}-[func]{leftRotate}\n
        avl_tree.ts
        [class]{AVLTree}-[func]{leftRotate}\n
        avl_tree.dart
        [class]{AVLTree}-[func]{leftRotate}\n
        avl_tree.rs
        [class]{AVLTree}-[func]{left_rotate}\n
        avl_tree.c
        [class]{}-[func]{leftRotate}\n
        avl_tree.kt
        [class]{AVLTree}-[func]{leftRotate}\n
        avl_tree.rb
        [class]{AVLTree}-[func]{left_rotate}\n
        "},{"location":"chapter_tree/avl_tree/#3","title":"3. \u00a0 \u5de6\u53f3\u56de\u8ee2","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u4e0d\u5e73\u8861\u30ce\u30fc\u30c93\u306e\u5834\u5408\u3001\u5de6\u56de\u8ee2\u307e\u305f\u306f\u53f3\u56de\u8ee2\u306e\u3044\u305a\u308c\u304b\u3060\u3051\u3067\u306f\u90e8\u5206\u6728\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u56de\u5fa9\u3067\u304d\u307e\u305b\u3093\u3002\u3053\u306e\u5834\u5408\u3001\u307e\u305achild\u306b\u5bfe\u3057\u3066\u300c\u5de6\u56de\u8ee2\u300d\u3092\u5b9f\u884c\u3057\u3001\u6b21\u306bnode\u306b\u5bfe\u3057\u3066\u300c\u53f3\u56de\u8ee2\u300d\u3092\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u56f3 7-30 \u00a0 \u5de6\u53f3\u56de\u8ee2

        "},{"location":"chapter_tree/avl_tree/#4","title":"4. \u00a0 \u53f3\u5de6\u56de\u8ee2","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u4e0a\u8a18\u306e\u4e0d\u5e73\u8861\u4e8c\u5206\u6728\u306e\u93e1\u50cf\u30b1\u30fc\u30b9\u3067\u306f\u3001\u307e\u305achild\u306b\u5bfe\u3057\u3066\u300c\u53f3\u56de\u8ee2\u300d\u3092\u5b9f\u884c\u3057\u3001\u6b21\u306bnode\u306b\u5bfe\u3057\u3066\u300c\u5de6\u56de\u8ee2\u300d\u3092\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        \u56f3 7-31 \u00a0 \u53f3\u5de6\u56de\u8ee2

        "},{"location":"chapter_tree/avl_tree/#5","title":"5. \u00a0 \u56de\u8ee2\u306e\u9078\u629e","text":"

        \u4e0b\u56f3\u306b\u793a\u30594\u7a2e\u985e\u306e\u4e0d\u5e73\u8861\u306f\u3001\u305d\u308c\u305e\u308c\u4e0a\u8a18\u3067\u8aac\u660e\u3057\u305f\u30b1\u30fc\u30b9\u306b\u5bfe\u5fdc\u3057\u3001\u53f3\u56de\u8ee2\u3001\u5de6\u53f3\u56de\u8ee2\u3001\u53f3\u5de6\u56de\u8ee2\u3001\u5de6\u56de\u8ee2\u304c\u5fc5\u8981\u3067\u3059\u3002

        \u56f3 7-32 \u00a0 AVL\u6728\u306e4\u3064\u306e\u56de\u8ee2\u30b1\u30fc\u30b9

        \u4e0b\u8868\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u4e0d\u5e73\u8861\u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50\u3068\u305d\u306e\u9ad8\u3044\u5074\u306e\u5b50\u306e\u5e73\u8861\u56e0\u5b50\u306e\u7b26\u53f7\u3092\u5224\u65ad\u3059\u308b\u3053\u3068\u3067\u3001\u4e0d\u5e73\u8861\u30ce\u30fc\u30c9\u304c\u4e0a\u8a18\u306e\u3069\u306e\u30b1\u30fc\u30b9\u306b\u5c5e\u3059\u308b\u304b\u3092\u6c7a\u5b9a\u3057\u307e\u3059\u3002

        \u8868 7-3 \u00a0 4\u3064\u306e\u56de\u8ee2\u30b1\u30fc\u30b9\u306e\u9078\u629e\u6761\u4ef6

        \u4e0d\u5e73\u8861\u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50 \u5b50\u30ce\u30fc\u30c9\u306e\u5e73\u8861\u56e0\u5b50 \u4f7f\u7528\u3059\u308b\u56de\u8ee2\u65b9\u6cd5 \\(> 1\\)\uff08\u5de6\u306b\u50be\u3044\u305f\u6728\uff09 \\(\\geq 0\\) \u53f3\u56de\u8ee2 \\(> 1\\)\uff08\u5de6\u306b\u50be\u3044\u305f\u6728\uff09 \\(<0\\) \u5de6\u56de\u8ee2\u3057\u3066\u304b\u3089\u53f3\u56de\u8ee2 \\(< -1\\)\uff08\u53f3\u306b\u50be\u3044\u305f\u6728\uff09 \\(\\leq 0\\) \u5de6\u56de\u8ee2 \\(< -1\\)\uff08\u53f3\u306b\u50be\u3044\u305f\u6728\uff09 \\(>0\\) \u53f3\u56de\u8ee2\u3057\u3066\u304b\u3089\u5de6\u56de\u8ee2

        \u4fbf\u5b9c\u4e0a\u3001\u56de\u8ee2\u64cd\u4f5c\u3092\u95a2\u6570\u306b\u30ab\u30d7\u30bb\u30eb\u5316\u3057\u307e\u3059\u3002\u3053\u306e\u95a2\u6570\u306b\u3088\u308a\u3001\u3055\u307e\u3056\u307e\u306a\u7a2e\u985e\u306e\u4e0d\u5e73\u8861\u306b\u5bfe\u3057\u3066\u56de\u8ee2\u3092\u5b9f\u884c\u3057\u3001\u4e0d\u5e73\u8861\u30ce\u30fc\u30c9\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u56de\u5fa9\u3067\u304d\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def rotate(self, node: TreeNode | None) -> TreeNode | None:\n    \"\"\"\u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3066\u90e8\u5206\u6728\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u5fa9\u5143\"\"\"\n    # node\u306e\u30d0\u30e9\u30f3\u30b9\u56e0\u5b50\u3092\u53d6\u5f97\n    balance_factor = self.balance_factor(node)\n    # \u5de6\u504f\u308a\u6728\n    if balance_factor > 1:\n        if self.balance_factor(node.left) >= 0:\n            # \u53f3\u56de\u8ee2\n            return self.right_rotate(node)\n        else:\n            # \u5de6\u56de\u8ee2\u3057\u3066\u304b\u3089\u53f3\u56de\u8ee2\n            node.left = self.left_rotate(node.left)\n            return self.right_rotate(node)\n    # \u53f3\u504f\u308a\u6728\n    elif balance_factor < -1:\n        if self.balance_factor(node.right) <= 0:\n            # \u5de6\u56de\u8ee2\n            return self.left_rotate(node)\n        else:\n            # \u53f3\u56de\u8ee2\u3057\u3066\u304b\u3089\u5de6\u56de\u8ee2\n            node.right = self.right_rotate(node.right)\n            return self.left_rotate(node)\n    # \u30d0\u30e9\u30f3\u30b9\u306e\u53d6\u308c\u305f\u6728\u3001\u56de\u8ee2\u4e0d\u8981\u3001\u623b\u308b\n    return node\n
        avl_tree.cpp
        /* \u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3066\u90e8\u5206\u6728\u306e\u5e73\u8861\u3092\u56de\u5fa9 */\nTreeNode *rotate(TreeNode *node) {\n    // node\u306e\u5e73\u8861\u56e0\u5b50\u3092\u53d6\u5f97\n    int _balanceFactor = balanceFactor(node);\n    // \u5de6\u306b\u50be\u3044\u305f\u6728\n    if (_balanceFactor > 1) {\n        if (balanceFactor(node->left) >= 0) {\n            // \u53f3\u56de\u8ee2\n            return rightRotate(node);\n        } else {\n            // \u5148\u306b\u5de6\u56de\u8ee2\u3001\u305d\u306e\u5f8c\u53f3\u56de\u8ee2\n            node->left = leftRotate(node->left);\n            return rightRotate(node);\n        }\n    }\n    // \u53f3\u306b\u50be\u3044\u305f\u6728\n    if (_balanceFactor < -1) {\n        if (balanceFactor(node->right) <= 0) {\n            // \u5de6\u56de\u8ee2\n            return leftRotate(node);\n        } else {\n            // \u5148\u306b\u53f3\u56de\u8ee2\u3001\u305d\u306e\u5f8c\u5de6\u56de\u8ee2\n            node->right = rightRotate(node->right);\n            return leftRotate(node);\n        }\n    }\n    // \u5e73\u8861\u306a\u6728\u3001\u56de\u8ee2\u4e0d\u8981\u3001\u305d\u306e\u307e\u307e\u623b\u308b\n    return node;\n}\n
        avl_tree.java
        /* \u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3066\u90e8\u5206\u6728\u306e\u5e73\u8861\u3092\u56de\u5fa9 */\nTreeNode rotate(TreeNode node) {\n    // node \u306e\u5e73\u8861\u56e0\u5b50\u3092\u53d6\u5f97\n    int balanceFactor = balanceFactor(node);\n    // \u5de6\u50be\u659c\u306e\u6728\n    if (balanceFactor > 1) {\n        if (balanceFactor(node.left) >= 0) {\n            // \u53f3\u56de\u8ee2\n            return rightRotate(node);\n        } else {\n            // \u5148\u306b\u5de6\u56de\u8ee2\u3001\u305d\u306e\u5f8c\u53f3\u56de\u8ee2\n            node.left = leftRotate(node.left);\n            return rightRotate(node);\n        }\n    }\n    // \u53f3\u50be\u659c\u306e\u6728\n    if (balanceFactor < -1) {\n        if (balanceFactor(node.right) <= 0) {\n            // \u5de6\u56de\u8ee2\n            return leftRotate(node);\n        } else {\n            // \u5148\u306b\u53f3\u56de\u8ee2\u3001\u305d\u306e\u5f8c\u5de6\u56de\u8ee2\n            node.right = rightRotate(node.right);\n            return leftRotate(node);\n        }\n    }\n    // \u5e73\u8861\u6728\u3001\u56de\u8ee2\u306f\u4e0d\u8981\u3001\u623b\u308b\n    return node;\n}\n
        avl_tree.cs
        [class]{AVLTree}-[func]{Rotate}\n
        avl_tree.go
        [class]{aVLTree}-[func]{rotate}\n
        avl_tree.swift
        [class]{AVLTree}-[func]{rotate}\n
        avl_tree.js
        [class]{AVLTree}-[func]{rotate}\n
        avl_tree.ts
        [class]{AVLTree}-[func]{rotate}\n
        avl_tree.dart
        [class]{AVLTree}-[func]{rotate}\n
        avl_tree.rs
        [class]{AVLTree}-[func]{rotate}\n
        avl_tree.c
        [class]{}-[func]{rotate}\n
        avl_tree.kt
        [class]{AVLTree}-[func]{rotate}\n
        avl_tree.rb
        [class]{AVLTree}-[func]{rotate}\n
        "},{"location":"chapter_tree/avl_tree/#753-avl","title":"7.5.3 \u00a0 AVL\u6728\u306e\u4e00\u822c\u7684\u306a\u64cd\u4f5c","text":""},{"location":"chapter_tree/avl_tree/#1_2","title":"1. \u00a0 \u30ce\u30fc\u30c9\u306e\u633f\u5165","text":"

        AVL\u6728\u306e\u30ce\u30fc\u30c9\u633f\u5165\u64cd\u4f5c\u306f\u4e8c\u5206\u63a2\u7d22\u6728\u306e\u305d\u308c\u3068\u4f3c\u3066\u3044\u307e\u3059\u3002\u552f\u4e00\u306e\u9055\u3044\u306f\u3001AVL\u6728\u3067\u30ce\u30fc\u30c9\u3092\u633f\u5165\u3057\u305f\u5f8c\u3001\u305d\u306e\u30ce\u30fc\u30c9\u304b\u3089\u6839\u30ce\u30fc\u30c9\u307e\u3067\u306e\u30d1\u30b9\u4e0a\u306b\u4e00\u9023\u306e\u4e0d\u5e73\u8861\u30ce\u30fc\u30c9\u304c\u73fe\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3053\u3068\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u3053\u306e\u30ce\u30fc\u30c9\u304b\u3089\u59cb\u3081\u3066\u4e0a\u5411\u304d\u306b\u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3001\u3059\u3079\u3066\u306e\u4e0d\u5e73\u8861\u30ce\u30fc\u30c9\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u56de\u5fa9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def insert(self, val):\n    \"\"\"\u30ce\u30fc\u30c9\u3092\u633f\u5165\"\"\"\n    self._root = self.insert_helper(self._root, val)\n\ndef insert_helper(self, node: TreeNode | None, val: int) -> TreeNode:\n    \"\"\"\u518d\u5e30\u7684\u306b\u30ce\u30fc\u30c9\u3092\u633f\u5165\uff08\u30d8\u30eb\u30d1\u30fc\u30e1\u30bd\u30c3\u30c9\uff09\"\"\"\n    if node is None:\n        return TreeNode(val)\n    # 1. \u633f\u5165\u4f4d\u7f6e\u3092\u898b\u3064\u3051\u3066\u30ce\u30fc\u30c9\u3092\u633f\u5165\n    if val < node.val:\n        node.left = self.insert_helper(node.left, val)\n    elif val > node.val:\n        node.right = self.insert_helper(node.right, val)\n    else:\n        # \u91cd\u8907\u30ce\u30fc\u30c9\u306f\u633f\u5165\u3057\u306a\u3044\u3001\u623b\u308b\n        return node\n    # \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    self.update_height(node)\n    # 2. \u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3066\u90e8\u5206\u6728\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u5fa9\u5143\n    return self.rotate(node)\n
        avl_tree.cpp
        /* \u30ce\u30fc\u30c9\u3092\u633f\u5165 */\nvoid insert(int val) {\n    root = insertHelper(root, val);\n}\n\n/* \u30ce\u30fc\u30c9\u3092\u518d\u5e30\u7684\u306b\u633f\u5165\uff08\u30d8\u30eb\u30d1\u30fc\u30e1\u30bd\u30c3\u30c9\uff09 */\nTreeNode *insertHelper(TreeNode *node, int val) {\n    if (node == nullptr)\n        return new TreeNode(val);\n    /* 1. \u633f\u5165\u4f4d\u7f6e\u3092\u898b\u3064\u3051\u3066\u30ce\u30fc\u30c9\u3092\u633f\u5165 */\n    if (val < node->val)\n        node->left = insertHelper(node->left, val);\n    else if (val > node->val)\n        node->right = insertHelper(node->right, val);\n    else\n        return node;    // \u91cd\u8907\u30ce\u30fc\u30c9\u306f\u633f\u5165\u3057\u306a\u3044\u3001\u305d\u306e\u307e\u307e\u623b\u308b\n    updateHeight(node); // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    /* 2. \u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3066\u90e8\u5206\u6728\u306e\u5e73\u8861\u3092\u56de\u5fa9 */\n    node = rotate(node);\n    // \u90e8\u5206\u6728\u306e\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u8fd4\u3059\n    return node;\n}\n
        avl_tree.java
        /* \u30ce\u30fc\u30c9\u3092\u633f\u5165 */\nvoid insert(int val) {\n    root = insertHelper(root, val);\n}\n\n/* \u518d\u5e30\u7684\u306b\u30ce\u30fc\u30c9\u3092\u633f\u5165\uff08\u88dc\u52a9\u30e1\u30bd\u30c3\u30c9\uff09 */\nTreeNode insertHelper(TreeNode node, int val) {\n    if (node == null)\n        return new TreeNode(val);\n    /* 1. \u633f\u5165\u4f4d\u7f6e\u3092\u898b\u3064\u3051\u3066\u30ce\u30fc\u30c9\u3092\u633f\u5165 */\n    if (val < node.val)\n        node.left = insertHelper(node.left, val);\n    else if (val > node.val)\n        node.right = insertHelper(node.right, val);\n    else\n        return node; // \u91cd\u8907\u30ce\u30fc\u30c9\u306f\u633f\u5165\u3057\u306a\u3044\u3001\u623b\u308b\n    updateHeight(node); // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    /* 2. \u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3066\u90e8\u5206\u6728\u306e\u5e73\u8861\u3092\u56de\u5fa9 */\n    node = rotate(node);\n    // \u90e8\u5206\u6728\u306e\u6839\u30ce\u30fc\u30c9\u3092\u8fd4\u3059\n    return node;\n}\n
        avl_tree.cs
        [class]{AVLTree}-[func]{Insert}\n\n[class]{AVLTree}-[func]{InsertHelper}\n
        avl_tree.go
        [class]{aVLTree}-[func]{insert}\n\n[class]{aVLTree}-[func]{insertHelper}\n
        avl_tree.swift
        [class]{AVLTree}-[func]{insert}\n\n[class]{AVLTree}-[func]{insertHelper}\n
        avl_tree.js
        [class]{AVLTree}-[func]{insert}\n\n[class]{AVLTree}-[func]{insertHelper}\n
        avl_tree.ts
        [class]{AVLTree}-[func]{insert}\n\n[class]{AVLTree}-[func]{insertHelper}\n
        avl_tree.dart
        [class]{AVLTree}-[func]{insert}\n\n[class]{AVLTree}-[func]{insertHelper}\n
        avl_tree.rs
        [class]{AVLTree}-[func]{insert}\n\n[class]{AVLTree}-[func]{insert_helper}\n
        avl_tree.c
        [class]{AVLTree}-[func]{insert}\n\n[class]{}-[func]{insertHelper}\n
        avl_tree.kt
        [class]{AVLTree}-[func]{insert}\n\n[class]{AVLTree}-[func]{insertHelper}\n
        avl_tree.rb
        [class]{AVLTree}-[func]{insert}\n\n[class]{AVLTree}-[func]{insert_helper}\n
        "},{"location":"chapter_tree/avl_tree/#2_2","title":"2. \u00a0 \u30ce\u30fc\u30c9\u306e\u524a\u9664","text":"

        \u540c\u69d8\u306b\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u3067\u306e\u30ce\u30fc\u30c9\u524a\u9664\u65b9\u6cd5\u306b\u57fa\u3065\u3044\u3066\u3001\u4e0b\u304b\u3089\u4e0a\u3078\u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3066\u3059\u3079\u3066\u306e\u4e0d\u5e73\u8861\u30ce\u30fc\u30c9\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u56de\u5fa9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def remove(self, val: int):\n    \"\"\"\u30ce\u30fc\u30c9\u3092\u524a\u9664\"\"\"\n    self._root = self.remove_helper(self._root, val)\n\ndef remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None:\n    \"\"\"\u518d\u5e30\u7684\u306b\u30ce\u30fc\u30c9\u3092\u524a\u9664\uff08\u30d8\u30eb\u30d1\u30fc\u30e1\u30bd\u30c3\u30c9\uff09\"\"\"\n    if node is None:\n        return None\n    # 1. \u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u3066\u524a\u9664\n    if val < node.val:\n        node.left = self.remove_helper(node.left, val)\n    elif val > node.val:\n        node.right = self.remove_helper(node.right, val)\n    else:\n        if node.left is None or node.right is None:\n            child = node.left or node.right\n            # \u5b50\u30ce\u30fc\u30c9\u6570 = 0\u3001\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3057\u3066\u623b\u308b\n            if child is None:\n                return None\n            # \u5b50\u30ce\u30fc\u30c9\u6570 = 1\u3001\u30ce\u30fc\u30c9\u3092\u524a\u9664\n            else:\n                node = child\n        else:\n            # \u5b50\u30ce\u30fc\u30c9\u6570 = 2\u3001\u4e2d\u9806\u8d70\u67fb\u306e\u6b21\u306e\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3057\u3001\u305d\u308c\u3067\u73fe\u5728\u306e\u30ce\u30fc\u30c9\u3092\u7f6e\u304d\u63db\u3048\n            temp = node.right\n            while temp.left is not None:\n                temp = temp.left\n            node.right = self.remove_helper(node.right, temp.val)\n            node.val = temp.val\n    # \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    self.update_height(node)\n    # 2. \u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3066\u90e8\u5206\u6728\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u5fa9\u5143\n    return self.rotate(node)\n
        avl_tree.cpp
        /* \u30ce\u30fc\u30c9\u3092\u524a\u9664 */\nvoid remove(int val) {\n    root = removeHelper(root, val);\n}\n\n/* \u30ce\u30fc\u30c9\u3092\u518d\u5e30\u7684\u306b\u524a\u9664\uff08\u30d8\u30eb\u30d1\u30fc\u30e1\u30bd\u30c3\u30c9\uff09 */\nTreeNode *removeHelper(TreeNode *node, int val) {\n    if (node == nullptr)\n        return nullptr;\n    /* 1. \u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u3066\u524a\u9664 */\n    if (val < node->val)\n        node->left = removeHelper(node->left, val);\n    else if (val > node->val)\n        node->right = removeHelper(node->right, val);\n    else {\n        if (node->left == nullptr || node->right == nullptr) {\n            TreeNode *child = node->left != nullptr ? node->left : node->right;\n            // \u5b50\u30ce\u30fc\u30c9\u6570 = 0\u3001\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3057\u3066\u623b\u308b\n            if (child == nullptr) {\n                delete node;\n                return nullptr;\n            }\n            // \u5b50\u30ce\u30fc\u30c9\u6570 = 1\u3001\u30ce\u30fc\u30c9\u3092\u524a\u9664\n            else {\n                delete node;\n                node = child;\n            }\n        } else {\n            // \u5b50\u30ce\u30fc\u30c9\u6570 = 2\u3001\u4e2d\u9806\u8d70\u67fb\u306e\u6b21\u306e\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3057\u3001\u73fe\u5728\u306e\u30ce\u30fc\u30c9\u3068\u7f6e\u304d\u63db\u3048\u308b\n            TreeNode *temp = node->right;\n            while (temp->left != nullptr) {\n                temp = temp->left;\n            }\n            int tempVal = temp->val;\n            node->right = removeHelper(node->right, temp->val);\n            node->val = tempVal;\n        }\n    }\n    updateHeight(node); // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    /* 2. \u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3066\u90e8\u5206\u6728\u306e\u5e73\u8861\u3092\u56de\u5fa9 */\n    node = rotate(node);\n    // \u90e8\u5206\u6728\u306e\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u8fd4\u3059\n    return node;\n}\n
        avl_tree.java
        /* \u30ce\u30fc\u30c9\u3092\u524a\u9664 */\nvoid remove(int val) {\n    root = removeHelper(root, val);\n}\n\n/* \u518d\u5e30\u7684\u306b\u30ce\u30fc\u30c9\u3092\u524a\u9664\uff08\u88dc\u52a9\u30e1\u30bd\u30c3\u30c9\uff09 */\nTreeNode removeHelper(TreeNode node, int val) {\n    if (node == null)\n        return null;\n    /* 1. \u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u3066\u524a\u9664 */\n    if (val < node.val)\n        node.left = removeHelper(node.left, val);\n    else if (val > node.val)\n        node.right = removeHelper(node.right, val);\n    else {\n        if (node.left == null || node.right == null) {\n            TreeNode child = node.left != null ? node.left : node.right;\n            // \u5b50\u30ce\u30fc\u30c9\u6570 = 0\u3001\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3057\u3066\u623b\u308b\n            if (child == null)\n                return null;\n            // \u5b50\u30ce\u30fc\u30c9\u6570 = 1\u3001\u30ce\u30fc\u30c9\u3092\u524a\u9664\n            else\n                node = child;\n        } else {\n            // \u5b50\u30ce\u30fc\u30c9\u6570 = 2\u3001\u4e2d\u9806\u8d70\u67fb\u306e\u6b21\u306e\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3057\u3001\u73fe\u5728\u306e\u30ce\u30fc\u30c9\u3092\u305d\u308c\u3067\u7f6e\u304d\u63db\u3048\u308b\n            TreeNode temp = node.right;\n            while (temp.left != null) {\n                temp = temp.left;\n            }\n            node.right = removeHelper(node.right, temp.val);\n            node.val = temp.val;\n        }\n    }\n    updateHeight(node); // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u3092\u66f4\u65b0\n    /* 2. \u56de\u8ee2\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u3066\u90e8\u5206\u6728\u306e\u5e73\u8861\u3092\u56de\u5fa9 */\n    node = rotate(node);\n    // \u90e8\u5206\u6728\u306e\u6839\u30ce\u30fc\u30c9\u3092\u8fd4\u3059\n    return node;\n}\n
        avl_tree.cs
        [class]{AVLTree}-[func]{Remove}\n\n[class]{AVLTree}-[func]{RemoveHelper}\n
        avl_tree.go
        [class]{aVLTree}-[func]{remove}\n\n[class]{aVLTree}-[func]{removeHelper}\n
        avl_tree.swift
        [class]{AVLTree}-[func]{remove}\n\n[class]{AVLTree}-[func]{removeHelper}\n
        avl_tree.js
        [class]{AVLTree}-[func]{remove}\n\n[class]{AVLTree}-[func]{removeHelper}\n
        avl_tree.ts
        [class]{AVLTree}-[func]{remove}\n\n[class]{AVLTree}-[func]{removeHelper}\n
        avl_tree.dart
        [class]{AVLTree}-[func]{remove}\n\n[class]{AVLTree}-[func]{removeHelper}\n
        avl_tree.rs
        [class]{AVLTree}-[func]{remove}\n\n[class]{AVLTree}-[func]{remove_helper}\n
        avl_tree.c
        [class]{AVLTree}-[func]{removeItem}\n\n[class]{}-[func]{removeHelper}\n
        avl_tree.kt
        [class]{AVLTree}-[func]{remove}\n\n[class]{AVLTree}-[func]{removeHelper}\n
        avl_tree.rb
        [class]{AVLTree}-[func]{remove}\n\n[class]{AVLTree}-[func]{remove_helper}\n
        "},{"location":"chapter_tree/avl_tree/#3_1","title":"3. \u00a0 \u30ce\u30fc\u30c9\u306e\u691c\u7d22","text":"

        AVL\u6728\u3067\u306e\u30ce\u30fc\u30c9\u691c\u7d22\u64cd\u4f5c\u306f\u4e8c\u5206\u63a2\u7d22\u6728\u306e\u305d\u308c\u3068\u4e00\u81f4\u3057\u3066\u304a\u308a\u3001\u3053\u3053\u3067\u306f\u8a73\u8ff0\u3057\u307e\u305b\u3093\u3002

        "},{"location":"chapter_tree/avl_tree/#754-avl","title":"7.5.4 \u00a0 AVL\u6728\u306e\u5178\u578b\u7684\u306a\u5fdc\u7528","text":"
        • \u5927\u91cf\u306e\u30c7\u30fc\u30bf\u306e\u6574\u7406\u3068\u683c\u7d0d\u306b\u4f7f\u7528\u3055\u308c\u3001\u691c\u7d22\u983b\u5ea6\u304c\u9ad8\u304f\u3001\u633f\u5165\u3068\u524a\u9664\u306e\u983b\u5ea6\u304c\u4f4e\u3044\u30b7\u30ca\u30ea\u30aa\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002
        • \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u30b7\u30b9\u30c6\u30e0\u306e\u69cb\u7bc9\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        • \u8d64\u9ed2\u6728\u3082\u4e00\u822c\u7684\u306a\u5e73\u8861\u4e8c\u5206\u63a2\u7d22\u6728\u306e\u4e00\u7a2e\u3067\u3059\u3002AVL\u6728\u3068\u6bd4\u8f03\u3057\u3066\u3001\u8d64\u9ed2\u6728\u306f\u3088\u308a\u7de9\u3044\u5e73\u8861\u6761\u4ef6\u3092\u6301\u3061\u3001\u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664\u306b\u304b\u304b\u308b\u56de\u8ee2\u6570\u304c\u5c11\u306a\u304f\u3001\u30ce\u30fc\u30c9\u306e\u8ffd\u52a0\u3068\u524a\u9664\u64cd\u4f5c\u306e\u5e73\u5747\u52b9\u7387\u304c\u9ad8\u304f\u306a\u308a\u307e\u3059\u3002
        "},{"location":"chapter_tree/binary_search_tree/","title":"7.4 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u306f\u4ee5\u4e0b\u306e\u6761\u4ef6\u3092\u6e80\u305f\u3057\u307e\u3059\u3002

        1. \u6839\u30ce\u30fc\u30c9\u306b\u3064\u3044\u3066\u3001\u5de6\u90e8\u5206\u6728\u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u306e\u5024 \\(<\\) \u6839\u30ce\u30fc\u30c9\u306e\u5024 \\(<\\) \u53f3\u90e8\u5206\u6728\u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u306e\u5024\u3002
        2. \u4efb\u610f\u306e\u30ce\u30fc\u30c9\u306e\u5de6\u3068\u53f3\u306e\u90e8\u5206\u6728\u3082\u4e8c\u5206\u63a2\u7d22\u6728\u3067\u3059\u3002\u3064\u307e\u308a\u3001\u6761\u4ef61.\u3082\u6e80\u305f\u3057\u307e\u3059\u3002

        \u56f3 7-16 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728

        "},{"location":"chapter_tree/binary_search_tree/#741","title":"7.4.1 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728\u306e\u64cd\u4f5c","text":"

        \u4e8c\u5206\u63a2\u7d22\u6728\u3092\u30af\u30e9\u30b9BinarySearchTree\u3068\u3057\u3066\u30ab\u30d7\u30bb\u30eb\u5316\u3057\u3001\u6728\u306e\u6839\u30ce\u30fc\u30c9\u3092\u6307\u3059\u30e1\u30f3\u30d0\u30fc\u5909\u6570root\u3092\u5ba3\u8a00\u3057\u307e\u3059\u3002

        "},{"location":"chapter_tree/binary_search_tree/#1","title":"1. \u00a0 \u30ce\u30fc\u30c9\u306e\u691c\u7d22","text":"

        \u30bf\u30fc\u30b2\u30c3\u30c8\u30ce\u30fc\u30c9\u5024num\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u306e\u6027\u8cea\u306b\u5f93\u3063\u3066\u691c\u7d22\u3067\u304d\u307e\u3059\u3002\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30ce\u30fc\u30c9cur\u3092\u5ba3\u8a00\u3057\u3001\u4e8c\u5206\u6728\u306e\u6839\u30ce\u30fc\u30c9root\u304b\u3089\u958b\u59cb\u3057\u3001\u30ce\u30fc\u30c9\u5024cur.val\u3068num\u306e\u30b5\u30a4\u30ba\u3092\u6bd4\u8f03\u3059\u308b\u30eb\u30fc\u30d7\u3092\u884c\u3044\u307e\u3059\u3002

        • cur.val < num\u306e\u5834\u5408\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u30ce\u30fc\u30c9\u306fcur\u306e\u53f3\u90e8\u5206\u6728\u306b\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u305f\u3081\u3001cur = cur.right\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002
        • cur.val > num\u306e\u5834\u5408\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u30ce\u30fc\u30c9\u306fcur\u306e\u5de6\u90e8\u5206\u6728\u306b\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u305f\u3081\u3001cur = cur.left\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002
        • cur.val = num\u306e\u5834\u5408\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u30ce\u30fc\u30c9\u304c\u898b\u3064\u304b\u3063\u305f\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u305f\u3081\u3001\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\u3057\u3066\u30ce\u30fc\u30c9\u3092\u8fd4\u3057\u307e\u3059\u3002
        <1><2><3><4>

        \u56f3 7-17 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728\u3067\u306e\u30ce\u30fc\u30c9\u691c\u7d22\u4f8b

        \u4e8c\u5206\u63a2\u7d22\u6728\u3067\u306e\u691c\u7d22\u64cd\u4f5c\u306f\u4e8c\u5206\u63a2\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u540c\u3058\u539f\u7406\u3067\u52d5\u4f5c\u3057\u3001\u5404\u30e9\u30a6\u30f3\u30c9\u3067\u30b1\u30fc\u30b9\u306e\u534a\u5206\u3092\u6392\u9664\u3057\u307e\u3059\u3002\u30eb\u30fc\u30d7\u6570\u306f\u6700\u5927\u3067\u4e8c\u5206\u6728\u306e\u9ad8\u3055\u3067\u3059\u3002\u4e8c\u5206\u6728\u304c\u5e73\u8861\u3057\u3066\u3044\u308b\u5834\u5408\u3001\\(O(\\log n)\\)\u306e\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u30b3\u30fc\u30c9\u4f8b\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_tree.py
        def search(self, num: int) -> TreeNode | None:\n    \"\"\"\u30ce\u30fc\u30c9\u3092\u63a2\u7d22\"\"\"\n    cur = self._root\n    # \u30eb\u30fc\u30d7\u3067\u63a2\u7d22\u3001\u8449\u30ce\u30fc\u30c9\u3092\u901a\u904e\u3057\u305f\u5f8c\u306b\u30d6\u30ec\u30fc\u30af\n    while cur is not None:\n        # \u30bf\u30fc\u30b2\u30c3\u30c8\u30ce\u30fc\u30c9\u306fcur\u306e\u53f3\u90e8\u5206\u6728\u306b\u3042\u308b\n        if cur.val < num:\n            cur = cur.right\n        # \u30bf\u30fc\u30b2\u30c3\u30c8\u30ce\u30fc\u30c9\u306fcur\u306e\u5de6\u90e8\u5206\u6728\u306b\u3042\u308b\n        elif cur.val > num:\n            cur = cur.left\n        # \u30bf\u30fc\u30b2\u30c3\u30c8\u30ce\u30fc\u30c9\u3092\u767a\u898b\u3001\u30eb\u30fc\u30d7\u3092\u30d6\u30ec\u30fc\u30af\n        else:\n            break\n    return cur\n
        binary_search_tree.cpp
        /* \u30ce\u30fc\u30c9\u3092\u691c\u7d22 */\nTreeNode *search(int num) {\n    TreeNode *cur = root;\n    // \u30eb\u30fc\u30d7\u3067\u691c\u7d22\u3001\u8449\u30ce\u30fc\u30c9\u3092\u901a\u308a\u904e\u304e\u305f\u3089\u7d42\u4e86\n    while (cur != nullptr) {\n        // \u76ee\u6a19\u30ce\u30fc\u30c9\u306fcur\u306e\u53f3\u90e8\u5206\u6728\u306b\u3042\u308b\n        if (cur->val < num)\n            cur = cur->right;\n        // \u76ee\u6a19\u30ce\u30fc\u30c9\u306fcur\u306e\u5de6\u90e8\u5206\u6728\u306b\u3042\u308b\n        else if (cur->val > num)\n            cur = cur->left;\n        // \u76ee\u6a19\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u305f\u3001\u30eb\u30fc\u30d7\u3092\u629c\u3051\u308b\n        else\n            break;\n    }\n    // \u76ee\u6a19\u30ce\u30fc\u30c9\u3092\u8fd4\u3059\n    return cur;\n}\n
        binary_search_tree.java
        /* \u30ce\u30fc\u30c9\u3092\u691c\u7d22 */\nTreeNode search(int num) {\n    TreeNode cur = root;\n    // \u30eb\u30fc\u30d7\u3067\u691c\u7d22\u3001\u8449\u30ce\u30fc\u30c9\u3092\u901a\u904e\u5f8c\u306b\u7d42\u4e86\n    while (cur != null) {\n        // \u5bfe\u8c61\u30ce\u30fc\u30c9\u306f cur \u306e\u53f3\u90e8\u5206\u6728\u306b\u3042\u308b\n        if (cur.val < num)\n            cur = cur.right;\n        // \u5bfe\u8c61\u30ce\u30fc\u30c9\u306f cur \u306e\u5de6\u90e8\u5206\u6728\u306b\u3042\u308b\n        else if (cur.val > num)\n            cur = cur.left;\n        // \u5bfe\u8c61\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u305f\u3001\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\n        else\n            break;\n    }\n    // \u5bfe\u8c61\u30ce\u30fc\u30c9\u3092\u8fd4\u3059\n    return cur;\n}\n
        binary_search_tree.cs
        [class]{BinarySearchTree}-[func]{Search}\n
        binary_search_tree.go
        [class]{binarySearchTree}-[func]{search}\n
        binary_search_tree.swift
        [class]{BinarySearchTree}-[func]{search}\n
        binary_search_tree.js
        [class]{BinarySearchTree}-[func]{search}\n
        binary_search_tree.ts
        [class]{BinarySearchTree}-[func]{search}\n
        binary_search_tree.dart
        [class]{BinarySearchTree}-[func]{search}\n
        binary_search_tree.rs
        [class]{BinarySearchTree}-[func]{search}\n
        binary_search_tree.c
        [class]{BinarySearchTree}-[func]{search}\n
        binary_search_tree.kt
        [class]{BinarySearchTree}-[func]{search}\n
        binary_search_tree.rb
        [class]{BinarySearchTree}-[func]{search}\n
        "},{"location":"chapter_tree/binary_search_tree/#2","title":"2. \u00a0 \u30ce\u30fc\u30c9\u306e\u633f\u5165","text":"

        \u633f\u5165\u3059\u308b\u8981\u7d20num\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u306e\u6027\u8cea\u300c\u5de6\u90e8\u5206\u6728 < \u6839\u30ce\u30fc\u30c9 < \u53f3\u90e8\u5206\u6728\u300d\u3092\u7dad\u6301\u3059\u308b\u305f\u3081\u3001\u633f\u5165\u64cd\u4f5c\u306f\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u9032\u884c\u3057\u307e\u3059\u3002

        1. \u633f\u5165\u4f4d\u7f6e\u3092\u898b\u3064\u3051\u308b: \u691c\u7d22\u64cd\u4f5c\u3068\u540c\u69d8\u306b\u3001\u6839\u30ce\u30fc\u30c9\u304b\u3089\u958b\u59cb\u3057\u3001\u73fe\u5728\u306e\u30ce\u30fc\u30c9\u5024\u3068num\u306e\u30b5\u30a4\u30ba\u95a2\u4fc2\u306b\u5f93\u3063\u3066\u4e0b\u5411\u304d\u306b\u30eb\u30fc\u30d7\u3057\u3001\u8449\u30ce\u30fc\u30c9\u3092\u901a\u904e\uff08None\u306b\u8d70\u67fb\uff09\u3059\u308b\u307e\u3067\u3001\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\u3057\u307e\u3059\u3002
        2. \u3053\u306e\u4f4d\u7f6e\u306b\u30ce\u30fc\u30c9\u3092\u633f\u5165: \u30ce\u30fc\u30c9num\u3092\u521d\u671f\u5316\u3057\u3001None\u304c\u3042\u3063\u305f\u5834\u6240\u306b\u914d\u7f6e\u3057\u307e\u3059\u3002

        \u56f3 7-18 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728\u3078\u306e\u30ce\u30fc\u30c9\u633f\u5165

        \u30b3\u30fc\u30c9\u5b9f\u88c5\u3067\u306f\u3001\u4ee5\u4e0b\u306e2\u70b9\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        • \u4e8c\u5206\u63a2\u7d22\u6728\u306f\u91cd\u8907\u30ce\u30fc\u30c9\u306e\u5b58\u5728\u3092\u8a31\u53ef\u3057\u307e\u305b\u3093\u3002\u305d\u3046\u3067\u306a\u3051\u308c\u3070\u3001\u305d\u306e\u5b9a\u7fa9\u306b\u9055\u53cd\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u633f\u5165\u3059\u308b\u30ce\u30fc\u30c9\u304c\u65e2\u306b\u6728\u306b\u5b58\u5728\u3059\u308b\u5834\u5408\u3001\u633f\u5165\u306f\u5b9f\u884c\u3055\u308c\u305a\u3001\u30ce\u30fc\u30c9\u306f\u76f4\u63a5\u623b\u308a\u307e\u3059\u3002
        • \u633f\u5165\u64cd\u4f5c\u3092\u5b9f\u884c\u3059\u308b\u306b\u306f\u3001\u524d\u306e\u30eb\u30fc\u30d7\u304b\u3089\u306e\u30ce\u30fc\u30c9\u3092\u4fdd\u5b58\u3059\u308b\u305f\u3081\u306b\u30ce\u30fc\u30c9pre\u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u3088\u3046\u306b\u3057\u3066\u3001None\u306b\u8d70\u67fb\u3057\u305f\u3068\u304d\u306b\u3001\u305d\u306e\u89aa\u30ce\u30fc\u30c9\u3092\u53d6\u5f97\u3067\u304d\u3001\u30ce\u30fc\u30c9\u633f\u5165\u64cd\u4f5c\u3092\u5b8c\u4e86\u3067\u304d\u307e\u3059\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_tree.py
        def insert(self, num: int):\n    \"\"\"\u30ce\u30fc\u30c9\u3092\u633f\u5165\"\"\"\n    # \u6728\u304c\u7a7a\u306e\u5834\u5408\u3001\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\n    if self._root is None:\n        self._root = TreeNode(num)\n        return\n    # \u30eb\u30fc\u30d7\u3067\u63a2\u7d22\u3001\u8449\u30ce\u30fc\u30c9\u3092\u901a\u904e\u3057\u305f\u5f8c\u306b\u30d6\u30ec\u30fc\u30af\n    cur, pre = self._root, None\n    while cur is not None:\n        # \u91cd\u8907\u30ce\u30fc\u30c9\u3092\u767a\u898b\u3057\u305f\u305f\u3081\u3001\u623b\u308b\n        if cur.val == num:\n            return\n        pre = cur\n        # \u633f\u5165\u4f4d\u7f6e\u306fcur\u306e\u53f3\u90e8\u5206\u6728\u306b\u3042\u308b\n        if cur.val < num:\n            cur = cur.right\n        # \u633f\u5165\u4f4d\u7f6e\u306fcur\u306e\u5de6\u90e8\u5206\u6728\u306b\u3042\u308b\n        else:\n            cur = cur.left\n    # \u30ce\u30fc\u30c9\u3092\u633f\u5165\n    node = TreeNode(num)\n    if pre.val < num:\n        pre.right = node\n    else:\n        pre.left = node\n
        binary_search_tree.cpp
        /* \u30ce\u30fc\u30c9\u3092\u633f\u5165 */\nvoid insert(int num) {\n    // \u6728\u304c\u7a7a\u306e\u5834\u5408\u3001\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\n    if (root == nullptr) {\n        root = new TreeNode(num);\n        return;\n    }\n    TreeNode *cur = root, *pre = nullptr;\n    // \u30eb\u30fc\u30d7\u3067\u691c\u7d22\u3001\u8449\u30ce\u30fc\u30c9\u3092\u901a\u308a\u904e\u304e\u305f\u3089\u7d42\u4e86\n    while (cur != nullptr) {\n        // \u91cd\u8907\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u305f\u5834\u5408\u3001\u623b\u308b\n        if (cur->val == num)\n            return;\n        pre = cur;\n        // \u633f\u5165\u4f4d\u7f6e\u306fcur\u306e\u53f3\u90e8\u5206\u6728\u306b\u3042\u308b\n        if (cur->val < num)\n            cur = cur->right;\n        // \u633f\u5165\u4f4d\u7f6e\u306fcur\u306e\u5de6\u90e8\u5206\u6728\u306b\u3042\u308b\n        else\n            cur = cur->left;\n    }\n    // \u30ce\u30fc\u30c9\u3092\u633f\u5165\n    TreeNode *node = new TreeNode(num);\n    if (pre->val < num)\n        pre->right = node;\n    else\n        pre->left = node;\n}\n
        binary_search_tree.java
        /* \u30ce\u30fc\u30c9\u3092\u633f\u5165 */\nvoid insert(int num) {\n    // \u6728\u304c\u7a7a\u306e\u5834\u5408\u3001\u6839\u30ce\u30fc\u30c9\u3092\u521d\u671f\u5316\n    if (root == null) {\n        root = new TreeNode(num);\n        return;\n    }\n    TreeNode cur = root, pre = null;\n    // \u30eb\u30fc\u30d7\u3067\u691c\u7d22\u3001\u8449\u30ce\u30fc\u30c9\u3092\u901a\u904e\u5f8c\u306b\u7d42\u4e86\n    while (cur != null) {\n        // \u91cd\u8907\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u305f\u5834\u5408\u3001\u623b\u308b\n        if (cur.val == num)\n            return;\n        pre = cur;\n        // \u633f\u5165\u4f4d\u7f6e\u306f cur \u306e\u53f3\u90e8\u5206\u6728\u306b\u3042\u308b\n        if (cur.val < num)\n            cur = cur.right;\n        // \u633f\u5165\u4f4d\u7f6e\u306f cur \u306e\u5de6\u90e8\u5206\u6728\u306b\u3042\u308b\n        else\n            cur = cur.left;\n    }\n    // \u30ce\u30fc\u30c9\u3092\u633f\u5165\n    TreeNode node = new TreeNode(num);\n    if (pre.val < num)\n        pre.right = node;\n    else\n        pre.left = node;\n}\n
        binary_search_tree.cs
        [class]{BinarySearchTree}-[func]{Insert}\n
        binary_search_tree.go
        [class]{binarySearchTree}-[func]{insert}\n
        binary_search_tree.swift
        [class]{BinarySearchTree}-[func]{insert}\n
        binary_search_tree.js
        [class]{BinarySearchTree}-[func]{insert}\n
        binary_search_tree.ts
        [class]{BinarySearchTree}-[func]{insert}\n
        binary_search_tree.dart
        [class]{BinarySearchTree}-[func]{insert}\n
        binary_search_tree.rs
        [class]{BinarySearchTree}-[func]{insert}\n
        binary_search_tree.c
        [class]{BinarySearchTree}-[func]{insert}\n
        binary_search_tree.kt
        [class]{BinarySearchTree}-[func]{insert}\n
        binary_search_tree.rb
        [class]{BinarySearchTree}-[func]{insert}\n

        \u30ce\u30fc\u30c9\u306e\u691c\u7d22\u3068\u540c\u69d8\u306b\u3001\u30ce\u30fc\u30c9\u306e\u633f\u5165\u306b\u306f\\(O(\\log n)\\)\u306e\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002

        "},{"location":"chapter_tree/binary_search_tree/#3","title":"3. \u00a0 \u30ce\u30fc\u30c9\u306e\u524a\u9664","text":"

        \u307e\u305a\u3001\u4e8c\u5206\u6728\u3067\u30bf\u30fc\u30b2\u30c3\u30c8\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u3001\u305d\u308c\u3092\u524a\u9664\u3057\u307e\u3059\u3002\u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u540c\u69d8\u306b\u3001\u524a\u9664\u64cd\u4f5c\u304c\u5b8c\u4e86\u3057\u305f\u5f8c\u3082\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u306e\u6027\u8cea\u300c\u5de6\u90e8\u5206\u6728 < \u6839\u30ce\u30fc\u30c9 < \u53f3\u90e8\u5206\u6728\u300d\u304c\u6e80\u305f\u3055\u308c\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u30ce\u30fc\u30c9\u306e\u5b50\u30ce\u30fc\u30c9\u6570\u306b\u57fa\u3065\u3044\u3066\u30010\u30011\u30012\u306e3\u3064\u306e\u30b1\u30fc\u30b9\u306b\u5206\u3051\u3001\u5bfe\u5fdc\u3059\u308b\u30ce\u30fc\u30c9\u524a\u9664\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u306e\u6b21\u6570\u304c\\(0\\)\u306e\u5834\u5408\u3001\u305d\u306e\u30ce\u30fc\u30c9\u306f\u8449\u30ce\u30fc\u30c9\u3067\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u3001\u76f4\u63a5\u524a\u9664\u3067\u304d\u307e\u3059\u3002

        \u56f3 7-19 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728\u3067\u306e\u30ce\u30fc\u30c9\u524a\u9664\uff08\u6b21\u65700\uff09

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u306e\u6b21\u6570\u304c\\(1\\)\u306e\u5834\u5408\u3001\u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u3092\u305d\u306e\u5b50\u30ce\u30fc\u30c9\u3067\u7f6e\u304d\u63db\u3048\u308b\u3060\u3051\u3067\u5341\u5206\u3067\u3059\u3002

        \u56f3 7-20 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728\u3067\u306e\u30ce\u30fc\u30c9\u524a\u9664\uff08\u6b21\u65701\uff09

        \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u306e\u6b21\u6570\u304c\\(2\\)\u306e\u5834\u5408\u3001\u76f4\u63a5\u524a\u9664\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u304c\u3001\u30ce\u30fc\u30c9\u3092\u4f7f\u7528\u3057\u3066\u7f6e\u304d\u63db\u3048\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4e8c\u5206\u63a2\u7d22\u6728\u306e\u6027\u8cea\u300c\u5de6\u90e8\u5206\u6728 \\(<\\) \u6839\u30ce\u30fc\u30c9 \\(<\\) \u53f3\u90e8\u5206\u6728\u300d\u3092\u7dad\u6301\u3059\u308b\u305f\u3081\u3001\u3053\u306e\u30ce\u30fc\u30c9\u306f\u53f3\u90e8\u5206\u6728\u306e\u6700\u5c0f\u30ce\u30fc\u30c9\u307e\u305f\u306f\u5de6\u90e8\u5206\u6728\u306e\u6700\u5927\u30ce\u30fc\u30c9\u306e\u3044\u305a\u308c\u304b\u3067\u3059\u3002

        \u53f3\u90e8\u5206\u6728\u306e\u6700\u5c0f\u30ce\u30fc\u30c9\uff08\u4e2d\u9806\u8d70\u67fb\u3067\u306e\u6b21\u306e\u30ce\u30fc\u30c9\uff09\u3092\u9078\u629e\u3059\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u524a\u9664\u64cd\u4f5c\u306f\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u9032\u884c\u3057\u307e\u3059\u3002

        1. \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u306e\u300c\u4e2d\u9806\u8d70\u67fb\u30b7\u30fc\u30b1\u30f3\u30b9\u300d\u3067\u306e\u6b21\u306e\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u3001tmp\u3068\u3057\u3066\u793a\u3057\u307e\u3059\u3002
        2. \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u306e\u5024\u3092tmp\u306e\u5024\u3067\u7f6e\u304d\u63db\u3048\u3001\u6728\u5185\u3067\u30ce\u30fc\u30c9tmp\u3092\u518d\u5e30\u7684\u306b\u524a\u9664\u3057\u307e\u3059\u3002
        <1><2><3><4>

        \u56f3 7-21 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728\u3067\u306e\u30ce\u30fc\u30c9\u524a\u9664\uff08\u6b21\u65702\uff09

        \u30ce\u30fc\u30c9\u3092\u524a\u9664\u3059\u308b\u64cd\u4f5c\u3082\\(O(\\log n)\\)\u306e\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u308b\u306e\u306b\\(O(\\log n)\\)\u306e\u6642\u9593\u304c\u5fc5\u8981\u3067\u3001\u4e2d\u9806\u8d70\u67fb\u306e\u5f8c\u7d99\u30ce\u30fc\u30c9\u3092\u53d6\u5f97\u3059\u308b\u306e\u306b\\(O(\\log n)\\)\u306e\u6642\u9593\u304c\u5fc5\u8981\u3067\u3059\u3002\u30b3\u30fc\u30c9\u4f8b\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_tree.py
        def remove(self, num: int):\n    \"\"\"\u30ce\u30fc\u30c9\u3092\u524a\u9664\"\"\"\n    # \u6728\u304c\u7a7a\u306e\u5834\u5408\u3001\u623b\u308b\n    if self._root is None:\n        return\n    # \u30eb\u30fc\u30d7\u3067\u63a2\u7d22\u3001\u8449\u30ce\u30fc\u30c9\u3092\u901a\u904e\u3057\u305f\u5f8c\u306b\u30d6\u30ec\u30fc\u30af\n    cur, pre = self._root, None\n    while cur is not None:\n        # \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u3092\u767a\u898b\u3001\u30eb\u30fc\u30d7\u3092\u30d6\u30ec\u30fc\u30af\n        if cur.val == num:\n            break\n        pre = cur\n        # \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u306fcur\u306e\u53f3\u90e8\u5206\u6728\u306b\u3042\u308b\n        if cur.val < num:\n            cur = cur.right\n        # \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u306fcur\u306e\u5de6\u90e8\u5206\u6728\u306b\u3042\u308b\n        else:\n            cur = cur.left\n    # \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001\u623b\u308b\n    if cur is None:\n        return\n\n    # \u5b50\u30ce\u30fc\u30c9\u6570 = 0 \u307e\u305f\u306f 1\n    if cur.left is None or cur.right is None:\n        # \u5b50\u30ce\u30fc\u30c9\u6570 = 0/1\u306e\u5834\u5408\u3001child = null/\u305d\u306e\u5b50\u30ce\u30fc\u30c9\n        child = cur.left or cur.right\n        # \u30ce\u30fc\u30c9cur\u3092\u524a\u9664\n        if cur != self._root:\n            if pre.left == cur:\n                pre.left = child\n            else:\n                pre.right = child\n        else:\n            # \u524a\u9664\u3055\u308c\u308b\u30ce\u30fc\u30c9\u304c\u30eb\u30fc\u30c8\u306e\u5834\u5408\u3001\u30eb\u30fc\u30c8\u3092\u518d\u5272\u308a\u5f53\u3066\n            self._root = child\n    # \u5b50\u30ce\u30fc\u30c9\u6570 = 2\n    else:\n        # cur\u306e\u4e2d\u9806\u8d70\u67fb\u306e\u6b21\u306e\u30ce\u30fc\u30c9\u3092\u53d6\u5f97\n        tmp: TreeNode = cur.right\n        while tmp.left is not None:\n            tmp = tmp.left\n        # \u518d\u5e30\u7684\u306b\u30ce\u30fc\u30c9tmp\u3092\u524a\u9664\n        self.remove(tmp.val)\n        # cur\u3092tmp\u3067\u7f6e\u304d\u63db\u3048\n        cur.val = tmp.val\n
        binary_search_tree.cpp
        /* \u30ce\u30fc\u30c9\u3092\u524a\u9664 */\nvoid remove(int num) {\n    // \u6728\u304c\u7a7a\u306e\u5834\u5408\u3001\u623b\u308b\n    if (root == nullptr)\n        return;\n    TreeNode *cur = root, *pre = nullptr;\n    // \u30eb\u30fc\u30d7\u3067\u691c\u7d22\u3001\u8449\u30ce\u30fc\u30c9\u3092\u901a\u308a\u904e\u304e\u305f\u3089\u7d42\u4e86\n    while (cur != nullptr) {\n        // \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u305f\u3001\u30eb\u30fc\u30d7\u3092\u629c\u3051\u308b\n        if (cur->val == num)\n            break;\n        pre = cur;\n        // \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u306fcur\u306e\u53f3\u90e8\u5206\u6728\u306b\u3042\u308b\n        if (cur->val < num)\n            cur = cur->right;\n        // \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u306fcur\u306e\u5de6\u90e8\u5206\u6728\u306b\u3042\u308b\n        else\n            cur = cur->left;\n    }\n    // \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u304c\u306a\u3044\u5834\u5408\u3001\u623b\u308b\n    if (cur == nullptr)\n        return;\n    // \u5b50\u30ce\u30fc\u30c9\u6570 = 0 \u307e\u305f\u306f 1\n    if (cur->left == nullptr || cur->right == nullptr) {\n        // \u5b50\u30ce\u30fc\u30c9\u6570 = 0 / 1\u306e\u5834\u5408\u3001child = nullptr / \u305d\u306e\u5b50\u30ce\u30fc\u30c9\n        TreeNode *child = cur->left != nullptr ? cur->left : cur->right;\n        // \u30ce\u30fc\u30c9cur\u3092\u524a\u9664\n        if (cur != root) {\n            if (pre->left == cur)\n                pre->left = child;\n            else\n                pre->right = child;\n        } else {\n            // \u524a\u9664\u3055\u308c\u308b\u30ce\u30fc\u30c9\u304c\u30eb\u30fc\u30c8\u306e\u5834\u5408\u3001\u30eb\u30fc\u30c8\u3092\u518d\u5272\u308a\u5f53\u3066\n            root = child;\n        }\n        // \u30e1\u30e2\u30ea\u3092\u89e3\u653e\n        delete cur;\n    }\n    // \u5b50\u30ce\u30fc\u30c9\u6570 = 2\n    else {\n        // cur\u306e\u4e2d\u9806\u8d70\u67fb\u306e\u6b21\u306e\u30ce\u30fc\u30c9\u3092\u53d6\u5f97\n        TreeNode *tmp = cur->right;\n        while (tmp->left != nullptr) {\n            tmp = tmp->left;\n        }\n        int tmpVal = tmp->val;\n        // \u30ce\u30fc\u30c9tmp\u3092\u518d\u5e30\u7684\u306b\u524a\u9664\n        remove(tmp->val);\n        // cur\u3092tmp\u3067\u7f6e\u304d\u63db\u3048\n        cur->val = tmpVal;\n    }\n}\n
        binary_search_tree.java
        /* \u30ce\u30fc\u30c9\u3092\u524a\u9664 */\nvoid remove(int num) {\n    // \u6728\u304c\u7a7a\u306e\u5834\u5408\u3001\u623b\u308b\n    if (root == null)\n        return;\n    TreeNode cur = root, pre = null;\n    // \u30eb\u30fc\u30d7\u3067\u691c\u7d22\u3001\u8449\u30ce\u30fc\u30c9\u3092\u901a\u904e\u5f8c\u306b\u7d42\u4e86\n    while (cur != null) {\n        // \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u3092\u898b\u3064\u3051\u305f\u3001\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\n        if (cur.val == num)\n            break;\n        pre = cur;\n        // \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u306f cur \u306e\u53f3\u90e8\u5206\u6728\u306b\u3042\u308b\n        if (cur.val < num)\n            cur = cur.right;\n        // \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u306f cur \u306e\u5de6\u90e8\u5206\u6728\u306b\u3042\u308b\n        else\n            cur = cur.left;\n    }\n    // \u524a\u9664\u3059\u308b\u30ce\u30fc\u30c9\u304c\u306a\u3044\u5834\u5408\u3001\u623b\u308b\n    if (cur == null)\n        return;\n    // \u5b50\u30ce\u30fc\u30c9\u6570 = 0 \u307e\u305f\u306f 1\n    if (cur.left == null || cur.right == null) {\n        // \u5b50\u30ce\u30fc\u30c9\u6570 = 0/1 \u306e\u5834\u5408\u3001child = null/\u305d\u306e\u5b50\u30ce\u30fc\u30c9\n        TreeNode child = cur.left != null ? cur.left : cur.right;\n        // \u30ce\u30fc\u30c9 cur \u3092\u524a\u9664\n        if (cur != root) {\n            if (pre.left == cur)\n                pre.left = child;\n            else\n                pre.right = child;\n        } else {\n            // \u524a\u9664\u3055\u308c\u308b\u30ce\u30fc\u30c9\u304c\u6839\u306e\u5834\u5408\u3001\u6839\u3092\u518d\u5272\u308a\u5f53\u3066\n            root = child;\n        }\n    }\n    // \u5b50\u30ce\u30fc\u30c9\u6570 = 2\n    else {\n        // cur \u306e\u4e2d\u9806\u8d70\u67fb\u306e\u6b21\u306e\u30ce\u30fc\u30c9\u3092\u53d6\u5f97\n        TreeNode tmp = cur.right;\n        while (tmp.left != null) {\n            tmp = tmp.left;\n        }\n        // \u518d\u5e30\u7684\u306b\u30ce\u30fc\u30c9 tmp \u3092\u524a\u9664\n        remove(tmp.val);\n        // cur \u3092 tmp \u3067\u7f6e\u304d\u63db\u3048\u308b\n        cur.val = tmp.val;\n    }\n}\n
        binary_search_tree.cs
        [class]{BinarySearchTree}-[func]{Remove}\n
        binary_search_tree.go
        [class]{binarySearchTree}-[func]{remove}\n
        binary_search_tree.swift
        [class]{BinarySearchTree}-[func]{remove}\n
        binary_search_tree.js
        [class]{BinarySearchTree}-[func]{remove}\n
        binary_search_tree.ts
        [class]{BinarySearchTree}-[func]{remove}\n
        binary_search_tree.dart
        [class]{BinarySearchTree}-[func]{remove}\n
        binary_search_tree.rs
        [class]{BinarySearchTree}-[func]{remove}\n
        binary_search_tree.c
        [class]{BinarySearchTree}-[func]{removeItem}\n
        binary_search_tree.kt
        [class]{BinarySearchTree}-[func]{remove}\n
        binary_search_tree.rb
        [class]{BinarySearchTree}-[func]{remove}\n
        "},{"location":"chapter_tree/binary_search_tree/#4","title":"4. \u00a0 \u4e2d\u9806\u8d70\u67fb\u306f\u9806\u5e8f\u4ed8\u3051\u3055\u308c\u3066\u3044\u308b","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u4e8c\u5206\u6728\u306e\u4e2d\u9806\u8d70\u67fb\u306f\u300c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u300d\u306e\u8d70\u67fb\u9806\u5e8f\u306b\u5f93\u3044\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u306f\u300c\u5de6\u5b50\u30ce\u30fc\u30c9 \\(<\\) \u6839\u30ce\u30fc\u30c9 \\(<\\) \u53f3\u5b50\u30ce\u30fc\u30c9\u300d\u306e\u30b5\u30a4\u30ba\u95a2\u4fc2\u3092\u6e80\u305f\u3057\u307e\u3059\u3002

        \u3053\u308c\u306f\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u3067\u4e2d\u9806\u8d70\u67fb\u3092\u5b9f\u884c\u3059\u308b\u3068\u304d\u306b\u3001\u5e38\u306b\u6b21\u306b\u5c0f\u3055\u3044\u30ce\u30fc\u30c9\u304c\u6700\u521d\u306b\u8d70\u67fb\u3055\u308c\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u3001\u91cd\u8981\u306a\u6027\u8cea\u306b\u3064\u306a\u304c\u308a\u307e\u3059\uff1a\u4e8c\u5206\u63a2\u7d22\u6728\u306e\u4e2d\u9806\u8d70\u67fb\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u306f\u6607\u9806\u3067\u3059\u3002

        \u4e2d\u9806\u8d70\u67fb\u306e\u6607\u9806\u6027\u8cea\u3092\u4f7f\u7528\u3057\u3066\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u3067\u9806\u5e8f\u4ed8\u3051\u3055\u308c\u305f\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u306b\u306f\\(O(n)\\)\u306e\u6642\u9593\u306e\u307f\u304c\u5fc5\u8981\u3067\u3001\u8ffd\u52a0\u306e\u30bd\u30fc\u30c8\u64cd\u4f5c\u306f\u4e0d\u8981\u3067\u3042\u308a\u3001\u975e\u5e38\u306b\u52b9\u7387\u7684\u3067\u3059\u3002

        \u56f3 7-22 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728\u306e\u4e2d\u9806\u8d70\u67fb\u30b7\u30fc\u30b1\u30f3\u30b9

        "},{"location":"chapter_tree/binary_search_tree/#742","title":"7.4.2 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728\u306e\u52b9\u7387","text":"

        \u30c7\u30fc\u30bf\u306e\u30bb\u30c3\u30c8\u304c\u4e0e\u3048\u3089\u308c\u305f\u5834\u5408\u3001\u914d\u5217\u307e\u305f\u306f\u4e8c\u5206\u63a2\u7d22\u6728\u3092\u4f7f\u7528\u3057\u3066\u683c\u7d0d\u3059\u308b\u3053\u3068\u3092\u691c\u8a0e\u3057\u307e\u3059\u3002\u4e0b\u306e\u8868\u3092\u89b3\u5bdf\u3059\u308b\u3068\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u306e\u3059\u3079\u3066\u306e\u64cd\u4f5c\u306f\u5bfe\u6570\u6642\u9593\u8a08\u7b97\u91cf\u3092\u6301\u3061\u3001\u5b89\u5b9a\u3057\u3066\u52b9\u7387\u7684\u3067\u3059\u3002\u914d\u5217\u306f\u3001\u983b\u7e41\u306a\u8ffd\u52a0\u3068\u691c\u7d22\u3084\u524a\u9664\u306e\u983b\u5ea6\u304c\u5c11\u306a\u3044\u30b7\u30ca\u30ea\u30aa\u3067\u306e\u307f\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u3088\u308a\u3082\u52b9\u7387\u7684\u3067\u3059\u3002

        \u8868 7-2 \u00a0 \u914d\u5217\u3068\u63a2\u7d22\u6728\u306e\u52b9\u7387\u6bd4\u8f03

        \u672a\u30bd\u30fc\u30c8\u914d\u5217 \u4e8c\u5206\u63a2\u7d22\u6728 \u8981\u7d20\u306e\u691c\u7d22 \\(O(n)\\) \\(O(\\log n)\\) \u8981\u7d20\u306e\u633f\u5165 \\(O(1)\\) \\(O(\\log n)\\) \u8981\u7d20\u306e\u524a\u9664 \\(O(n)\\) \\(O(\\log n)\\)

        \u7406\u60f3\u7684\u306b\u306f\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u306f\u300c\u5e73\u8861\u300d\u3057\u3066\u304a\u308a\u3001\u4efb\u610f\u306e\u30ce\u30fc\u30c9\u3092\\(\\log n\\)\u30eb\u30fc\u30d7\u5185\u3067\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        \u3057\u304b\u3057\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u3067\u7d99\u7d9a\u7684\u306b\u30ce\u30fc\u30c9\u3092\u633f\u5165\u304a\u3088\u3073\u524a\u9664\u3059\u308b\u3068\u3001\u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u9000\u5316\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u3055\u307e\u3056\u307e\u306a\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u3082\\(O(n)\\)\u306b\u60aa\u5316\u3057\u307e\u3059\u3002

        \u56f3 7-23 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728\u306e\u9000\u5316

        "},{"location":"chapter_tree/binary_search_tree/#743","title":"7.4.3 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728\u306e\u4e00\u822c\u7684\u306a\u5fdc\u7528","text":"
        • \u30b7\u30b9\u30c6\u30e0\u3067\u306e\u591a\u30ec\u30d9\u30eb\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3068\u3057\u3066\u4f7f\u7528\u3055\u308c\u3001\u52b9\u7387\u7684\u306a\u691c\u7d22\u3001\u633f\u5165\u3001\u524a\u9664\u64cd\u4f5c\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002
        • \u7279\u5b9a\u306e\u691c\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u57fa\u76e4\u3068\u306a\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u3057\u3066\u6a5f\u80fd\u3057\u307e\u3059\u3002
        • \u30c7\u30fc\u30bf\u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u683c\u7d0d\u3057\u3066\u3001\u305d\u306e\u9806\u5e8f\u4ed8\u3051\u3055\u308c\u305f\u72b6\u614b\u3092\u7dad\u6301\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        "},{"location":"chapter_tree/binary_tree/","title":"7.1 \u00a0 \u4e8c\u5206\u6728","text":"

        \u4e8c\u5206\u6728\u306f\u3001\u7956\u5148\u3068\u5b50\u5b6b\u306e\u9593\u306e\u968e\u5c64\u95a2\u4fc2\u3092\u8868\u73fe\u3057\u3001\u300c\u4e8c\u3064\u306b\u5206\u5272\u3059\u308b\u300d\u5206\u5272\u7d71\u6cbb\u6cd5\u306e\u8ad6\u7406\u3092\u4f53\u73fe\u3059\u308b\u975e\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3059\u3002\u9023\u7d50\u30ea\u30b9\u30c8\u3068\u540c\u69d8\u306b\u3001\u4e8c\u5206\u6728\u306e\u57fa\u672c\u5358\u4f4d\u306f\u30ce\u30fc\u30c9\u3067\u3042\u308a\u3001\u5404\u30ce\u30fc\u30c9\u306f\u5024\u3001\u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\u3001\u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\u3092\u542b\u307f\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        class TreeNode:\n    \"\"\"\u4e8c\u5206\u6728\u30ce\u30fc\u30c9\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val                # \u30ce\u30fc\u30c9\u5024\n        self.left: TreeNode | None = None  # \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n        self.right: TreeNode | None = None # \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n
        /* \u4e8c\u5206\u6728\u30ce\u30fc\u30c9 */\nstruct TreeNode {\n    int val;          // \u30ce\u30fc\u30c9\u5024\n    TreeNode *left;   // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    TreeNode *right;  // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n};\n
        /* \u4e8c\u5206\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode {\n    int val;         // \u30ce\u30fc\u30c9\u5024\n    TreeNode left;   // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    TreeNode right;  // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    TreeNode(int x) { val = x; }\n}\n
        /* \u4e8c\u5206\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode(int? x) {\n    public int? val = x;    // \u30ce\u30fc\u30c9\u5024\n    public TreeNode? left;  // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    public TreeNode? right; // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n}\n
        /* \u4e8c\u5206\u6728\u30ce\u30fc\u30c9 */\ntype TreeNode struct {\n    Val   int\n    Left  *TreeNode\n    Right *TreeNode\n}\n/* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\nfunc NewTreeNode(v int) *TreeNode {\n    return &TreeNode{\n        Left:  nil, // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n        Right: nil, // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n        Val:   v,   // \u30ce\u30fc\u30c9\u5024\n    }\n}\n
        /* \u4e8c\u5206\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode {\n    var val: Int // \u30ce\u30fc\u30c9\u5024\n    var left: TreeNode? // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    var right: TreeNode? // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n\n    init(x: Int) {\n        val = x\n    }\n}\n
        /* \u4e8c\u5206\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode {\n    val; // \u30ce\u30fc\u30c9\u5024\n    left; // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    right; // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    constructor(val, left, right) {\n        this.val = val === undefined ? 0 : val;\n        this.left = left === undefined ? null : left;\n        this.right = right === undefined ? null : right;\n    }\n}\n
        /* \u4e8c\u5206\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode {\n    val: number;\n    left: TreeNode | null;\n    right: TreeNode | null;\n\n    constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {\n        this.val = val === undefined ? 0 : val; // \u30ce\u30fc\u30c9\u5024\n        this.left = left === undefined ? null : left; // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n        this.right = right === undefined ? null : right; // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    }\n}\n
        /* \u4e8c\u5206\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode {\n  int val;         // \u30ce\u30fc\u30c9\u5024\n  TreeNode? left;  // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n  TreeNode? right; // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n  TreeNode(this.val, [this.left, this.right]);\n}\n
        use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u4e8c\u5206\u6728\u30ce\u30fc\u30c9 */\nstruct TreeNode {\n    val: i32,                               // \u30ce\u30fc\u30c9\u5024\n    left: Option<Rc<RefCell<TreeNode>>>,    // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    right: Option<Rc<RefCell<TreeNode>>>,   // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n}\n\nimpl TreeNode {\n    /* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\n    fn new(val: i32) -> Rc<RefCell<Self>> {\n        Rc::new(RefCell::new(Self {\n            val,\n            left: None,\n            right: None\n        }))\n    }\n}\n
        /* \u4e8c\u5206\u6728\u30ce\u30fc\u30c9 */\ntypedef struct TreeNode {\n    int val;                // \u30ce\u30fc\u30c9\u5024\n    int height;             // \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\n    struct TreeNode *left;  // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n    struct TreeNode *right; // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u30dd\u30a4\u30f3\u30bf\n} TreeNode;\n\n/* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf */\nTreeNode *newTreeNode(int val) {\n    TreeNode *node;\n\n    node = (TreeNode *)malloc(sizeof(TreeNode));\n    node->val = val;\n    node->height = 0;\n    node->left = NULL;\n    node->right = NULL;\n    return node;\n}\n
        /* \u4e8c\u5206\u6728\u30ce\u30fc\u30c9 */\nclass TreeNode(val _val: Int) {  // \u30ce\u30fc\u30c9\u5024\n    val left: TreeNode? = null   // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n    val right: TreeNode? = null  // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3078\u306e\u53c2\u7167\n}\n
        \n

        \u5404\u30ce\u30fc\u30c9\u306f2\u3064\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u6301\u3061\u3001\u305d\u308c\u305e\u308c\u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3068\u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3092\u6307\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u30ce\u30fc\u30c9\u306f\u3001\u3053\u308c\u30892\u3064\u306e\u5b50\u30ce\u30fc\u30c9\u306e\u89aa\u30ce\u30fc\u30c9\u3068\u547c\u3070\u308c\u307e\u3059\u3002\u4e8c\u5206\u6728\u306e\u30ce\u30fc\u30c9\u304c\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001\u3053\u306e\u30ce\u30fc\u30c9\u306e\u5de6\u306e\u5b50\u3068\u305d\u306e\u4e0b\u306b\u3042\u308b\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3067\u5f62\u6210\u3055\u308c\u308b\u6728\u3092\u3001\u3053\u306e\u30ce\u30fc\u30c9\u306e\u5de6\u90e8\u5206\u6728\u3068\u547c\u3073\u307e\u3059\u3002\u540c\u69d8\u306b\u3001\u53f3\u90e8\u5206\u6728\u3082\u5b9a\u7fa9\u3067\u304d\u307e\u3059\u3002

        \u4e8c\u5206\u6728\u3067\u306f\u3001\u8449\u30ce\u30fc\u30c9\u3092\u9664\u3044\u3066\u3001\u4ed6\u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u306f\u5b50\u30ce\u30fc\u30c9\u3068\u7a7a\u3067\u306a\u3044\u90e8\u5206\u6728\u3092\u542b\u307f\u307e\u3059\u3002 \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u300c\u30ce\u30fc\u30c92\u300d\u3092\u89aa\u30ce\u30fc\u30c9\u3068\u3057\u3066\u898b\u308b\u3068\u3001\u305d\u306e\u5de6\u3068\u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u306f\u305d\u308c\u305e\u308c\u300c\u30ce\u30fc\u30c94\u300d\u3068\u300c\u30ce\u30fc\u30c95\u300d\u3067\u3059\u3002\u5de6\u90e8\u5206\u6728\u306f\u300c\u30ce\u30fc\u30c94\u300d\u3068\u305d\u306e\u4e0b\u306b\u3042\u308b\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3067\u5f62\u6210\u3055\u308c\u3001\u53f3\u90e8\u5206\u6728\u306f\u300c\u30ce\u30fc\u30c95\u300d\u3068\u305d\u306e\u4e0b\u306b\u3042\u308b\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u3067\u5f62\u6210\u3055\u308c\u307e\u3059\u3002

        \u56f3 7-1 \u00a0 \u89aa\u30ce\u30fc\u30c9\u3001\u5b50\u30ce\u30fc\u30c9\u3001\u90e8\u5206\u6728

        "},{"location":"chapter_tree/binary_tree/#711","title":"7.1.1 \u00a0 \u4e8c\u5206\u6728\u306e\u4e00\u822c\u7684\u306a\u7528\u8a9e","text":"

        \u4e8c\u5206\u6728\u3067\u3088\u304f\u4f7f\u7528\u3055\u308c\u308b\u7528\u8a9e\u3092\u4e0b\u56f3\u306b\u793a\u3057\u307e\u3059\u3002

        • \u6839\u30ce\u30fc\u30c9\uff1a\u4e8c\u5206\u6728\u306e\u6700\u4e0a\u4f4d\u30ec\u30d9\u30eb\u306b\u3042\u308b\u30ce\u30fc\u30c9\u3067\u3001\u89aa\u30ce\u30fc\u30c9\u3092\u6301\u3061\u307e\u305b\u3093\u3002
        • \u8449\u30ce\u30fc\u30c9\uff1a\u5b50\u30ce\u30fc\u30c9\u3092\u6301\u305f\u306a\u3044\u30ce\u30fc\u30c9\u3067\u3001\u4e21\u65b9\u306e\u30dd\u30a4\u30f3\u30bf\u304cNone\u3092\u6307\u3057\u3066\u3044\u307e\u3059\u3002
        • \u8fba\uff1a2\u3064\u306e\u30ce\u30fc\u30c9\u3092\u7d50\u3076\u7dda\u5206\u3067\u3001\u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u8868\u73fe\u3057\u307e\u3059\u3002
        • \u30ce\u30fc\u30c9\u306e\u30ec\u30d9\u30eb\uff1a\u4e0a\u304b\u3089\u4e0b\u306b\u5411\u304b\u3063\u3066\u5897\u52a0\u3057\u3001\u6839\u30ce\u30fc\u30c9\u304c\u30ec\u30d9\u30eb1\u3067\u3059\u3002
        • \u30ce\u30fc\u30c9\u306e\u6b21\u6570\uff1a\u30ce\u30fc\u30c9\u304c\u6301\u3064\u5b50\u30ce\u30fc\u30c9\u306e\u6570\u3067\u3059\u3002\u4e8c\u5206\u6728\u3067\u306f\u3001\u6b21\u6570\u306f0\u30011\u3001\u307e\u305f\u306f2\u306b\u306a\u308a\u307e\u3059\u3002
        • \u4e8c\u5206\u6728\u306e\u9ad8\u3055\uff1a\u6839\u30ce\u30fc\u30c9\u304b\u3089\u6700\u3082\u9060\u3044\u8449\u30ce\u30fc\u30c9\u307e\u3067\u306e\u8fba\u306e\u6570\u3067\u3059\u3002
        • \u30ce\u30fc\u30c9\u306e\u6df1\u3055\uff1a\u6839\u30ce\u30fc\u30c9\u304b\u3089\u305d\u306e\u30ce\u30fc\u30c9\u307e\u3067\u306e\u8fba\u306e\u6570\u3067\u3059\u3002
        • \u30ce\u30fc\u30c9\u306e\u9ad8\u3055\uff1a\u6700\u3082\u9060\u3044\u8449\u30ce\u30fc\u30c9\u304b\u3089\u305d\u306e\u30ce\u30fc\u30c9\u307e\u3067\u306e\u8fba\u306e\u6570\u3067\u3059\u3002

        \u56f3 7-2 \u00a0 \u4e8c\u5206\u6728\u306e\u4e00\u822c\u7684\u306a\u7528\u8a9e

        Tip

        \u300c\u9ad8\u3055\u300d\u3068\u300c\u6df1\u3055\u300d\u306f\u901a\u5e38\u300c\u901a\u904e\u3059\u308b\u8fba\u306e\u6570\u300d\u3068\u3057\u3066\u5b9a\u7fa9\u3057\u307e\u3059\u304c\u3001\u4e00\u90e8\u306e\u554f\u984c\u3084\u6559\u79d1\u66f8\u3067\u306f\u300c\u901a\u904e\u3059\u308b\u30ce\u30fc\u30c9\u306e\u6570\u300d\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u5834\u5408\u3001\u9ad8\u3055\u3068\u6df1\u3055\u306e\u4e21\u65b9\u30921\u3060\u3051\u5897\u3084\u3059\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_tree/binary_tree/#712","title":"7.1.2 \u00a0 \u4e8c\u5206\u6728\u306e\u57fa\u672c\u64cd\u4f5c","text":""},{"location":"chapter_tree/binary_tree/#1","title":"1. \u00a0 \u4e8c\u5206\u6728\u306e\u521d\u671f\u5316","text":"

        \u9023\u7d50\u30ea\u30b9\u30c8\u3068\u540c\u69d8\u306b\u3001\u4e8c\u5206\u6728\u306e\u521d\u671f\u5316\u3067\u306f\u3001\u307e\u305a\u30ce\u30fc\u30c9\u3092\u4f5c\u6210\u3057\u3001\u6b21\u306b\u305d\u308c\u3089\u306e\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u78ba\u7acb\u3057\u307e\u3059\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree.py
        # \u4e8c\u5206\u6728\u306e\u521d\u671f\u5316\n# \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nn1 = TreeNode(val=1)\nn2 = TreeNode(val=2)\nn3 = TreeNode(val=3)\nn4 = TreeNode(val=4)\nn5 = TreeNode(val=5)\n# \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
        binary_tree.cpp
        /* \u4e8c\u5206\u6728\u306e\u521d\u671f\u5316 */\n// \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nTreeNode* n1 = new TreeNode(1);\nTreeNode* n2 = new TreeNode(2);\nTreeNode* n3 = new TreeNode(3);\nTreeNode* n4 = new TreeNode(4);\nTreeNode* n5 = new TreeNode(5);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
        binary_tree.java
        // \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
        binary_tree.cs
        /* \u4e8c\u5206\u6728\u306e\u521d\u671f\u5316 */\n// \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nTreeNode n1 = new(1);\nTreeNode n2 = new(2);\nTreeNode n3 = new(3);\nTreeNode n4 = new(4);\nTreeNode n5 = new(5);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
        binary_tree.go
        /* \u4e8c\u5206\u6728\u306e\u521d\u671f\u5316 */\n// \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nn1 := NewTreeNode(1)\nn2 := NewTreeNode(2)\nn3 := NewTreeNode(3)\nn4 := NewTreeNode(4)\nn5 := NewTreeNode(5)\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1.Left = n2\nn1.Right = n3\nn2.Left = n4\nn2.Right = n5\n
        binary_tree.swift
        // \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nlet n1 = TreeNode(x: 1)\nlet n2 = TreeNode(x: 2)\nlet n3 = TreeNode(x: 3)\nlet n4 = TreeNode(x: 4)\nlet n5 = TreeNode(x: 5)\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
        binary_tree.js
        /* \u4e8c\u5206\u6728\u306e\u521d\u671f\u5316 */\n// \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nlet n1 = new TreeNode(1),\n    n2 = new TreeNode(2),\n    n3 = new TreeNode(3),\n    n4 = new TreeNode(4),\n    n5 = new TreeNode(5);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
        binary_tree.ts
        /* \u4e8c\u5206\u6728\u306e\u521d\u671f\u5316 */\n// \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nlet n1 = new TreeNode(1),\n    n2 = new TreeNode(2),\n    n3 = new TreeNode(3),\n    n4 = new TreeNode(4),\n    n5 = new TreeNode(5);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
        binary_tree.dart
        /* \u4e8c\u5206\u6728\u306e\u521d\u671f\u5316 */\n// \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
        binary_tree.rs
        // \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nlet n1 = TreeNode::new(1);\nlet n2 = TreeNode::new(2);\nlet n3 = TreeNode::new(3);\nlet n4 = TreeNode::new(4);\nlet n5 = TreeNode::new(5);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1.borrow_mut().left = Some(n2.clone());\nn1.borrow_mut().right = Some(n3);\nn2.borrow_mut().left = Some(n4);\nn2.borrow_mut().right = Some(n5);\n
        binary_tree.c
        /* \u4e8c\u5206\u6728\u306e\u521d\u671f\u5316 */\n// \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nTreeNode *n1 = newTreeNode(1);\nTreeNode *n2 = newTreeNode(2);\nTreeNode *n3 = newTreeNode(3);\nTreeNode *n4 = newTreeNode(4);\nTreeNode *n5 = newTreeNode(5);\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
        binary_tree.kt
        // \u30ce\u30fc\u30c9\u306e\u521d\u671f\u5316\nval n1 = TreeNode(1)\nval n2 = TreeNode(2)\nval n3 = TreeNode(3)\nval n4 = TreeNode(4)\nval n5 = TreeNode(5)\n// \u30ce\u30fc\u30c9\u9593\u306e\u53c2\u7167\uff08\u30dd\u30a4\u30f3\u30bf\uff09\u3092\u7d50\u3076\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
        binary_tree.rb
        \n
        "},{"location":"chapter_tree/binary_tree/#2","title":"2. \u00a0 \u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664","text":"

        \u9023\u7d50\u30ea\u30b9\u30c8\u3068\u540c\u69d8\u306b\u3001\u4e8c\u5206\u6728\u3067\u306e\u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664\u306f\u30dd\u30a4\u30f3\u30bf\u3092\u5909\u66f4\u3059\u308b\u3053\u3068\u3067\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002\u4e0b\u56f3\u306b\u4f8b\u3092\u793a\u3057\u307e\u3059\u3002

        \u56f3 7-3 \u00a0 \u4e8c\u5206\u6728\u3067\u306e\u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree.py
        # \u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664\np = TreeNode(0)\n# n1 -> n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\nn1.left = p\np.left = n2\n# \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1.left = n2\n
        binary_tree.cpp
        /* \u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664 */\nTreeNode* P = new TreeNode(0);\n// n1\u3068n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\nn1->left = P;\nP->left = n2;\n// \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1->left = n2;\n
        binary_tree.java
        TreeNode P = new TreeNode(0);\n// n1\u3068n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\nn1.left = P;\nP.left = n2;\n// \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1.left = n2;\n
        binary_tree.cs
        /* \u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664 */\nTreeNode P = new(0);\n// n1\u3068n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\nn1.left = P;\nP.left = n2;\n// \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1.left = n2;\n
        binary_tree.go
        /* \u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664 */\n// n1\u3068n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\np := NewTreeNode(0)\nn1.Left = p\np.Left = n2\n// \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1.Left = n2\n
        binary_tree.swift
        let P = TreeNode(x: 0)\n// n1\u3068n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\nn1.left = P\nP.left = n2\n// \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1.left = n2\n
        binary_tree.js
        /* \u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664 */\nlet P = new TreeNode(0);\n// n1\u3068n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\nn1.left = P;\nP.left = n2;\n// \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1.left = n2;\n
        binary_tree.ts
        /* \u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664 */\nconst P = new TreeNode(0);\n// n1\u3068n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\nn1.left = P;\nP.left = n2;\n// \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1.left = n2;\n
        binary_tree.dart
        /* \u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664 */\nTreeNode P = new TreeNode(0);\n// n1\u3068n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\nn1.left = P;\nP.left = n2;\n// \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1.left = n2;\n
        binary_tree.rs
        let p = TreeNode::new(0);\n// n1\u3068n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\nn1.borrow_mut().left = Some(p.clone());\np.borrow_mut().left = Some(n2.clone());\n// \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1.borrow_mut().left = Some(n2);\n
        binary_tree.c
        /* \u30ce\u30fc\u30c9\u306e\u633f\u5165\u3068\u524a\u9664 */\nTreeNode *P = newTreeNode(0);\n// n1\u3068n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\nn1->left = P;\nP->left = n2;\n// \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1->left = n2;\n
        binary_tree.kt
        val P = TreeNode(0)\n// n1\u3068n2\u306e\u9593\u306b\u30ce\u30fc\u30c9P\u3092\u633f\u5165\nn1.left = P\nP.left = n2\n// \u30ce\u30fc\u30c9P\u3092\u524a\u9664\nn1.left = n2\n
        binary_tree.rb
        \n

        Tip

        \u30ce\u30fc\u30c9\u306e\u633f\u5165\u306f\u4e8c\u5206\u6728\u306e\u5143\u306e\u8ad6\u7406\u69cb\u9020\u3092\u5909\u66f4\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u30ce\u30fc\u30c9\u306e\u524a\u9664\u306f\u901a\u5e38\u305d\u306e\u30ce\u30fc\u30c9\u3068\u305d\u306e\u3059\u3079\u3066\u306e\u90e8\u5206\u6728\u3092\u524a\u9664\u3059\u308b\u3053\u3068\u306b\u306a\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u4e8c\u5206\u6728\u3067\u306f\u3001\u633f\u5165\u3068\u524a\u9664\u306f\u901a\u5e38\u4e00\u9023\u306e\u64cd\u4f5c\u3092\u901a\u3058\u3066\u5b9f\u884c\u3055\u308c\u3001\u610f\u5473\u306e\u3042\u308b\u7d50\u679c\u3092\u5f97\u307e\u3059\u3002

        "},{"location":"chapter_tree/binary_tree/#713","title":"7.1.3 \u00a0 \u4e8c\u5206\u6728\u306e\u4e00\u822c\u7684\u306a\u7a2e\u985e","text":""},{"location":"chapter_tree/binary_tree/#1_1","title":"1. \u00a0 \u5b8c\u5168\u4e8c\u5206\u6728","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5b8c\u5168\u4e8c\u5206\u6728\u3067\u306f\u3001\u3059\u3079\u3066\u306e\u30ec\u30d9\u30eb\u304c\u30ce\u30fc\u30c9\u3067\u5b8c\u5168\u306b\u57cb\u3081\u3089\u308c\u3066\u3044\u307e\u3059\u3002\u5b8c\u5168\u4e8c\u5206\u6728\u3067\u306f\u3001\u8449\u30ce\u30fc\u30c9\u306e\u6b21\u6570\u306f\\(0\\)\u3067\u3001\u4ed6\u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u306e\u6b21\u6570\u306f\\(2\\)\u3067\u3059\u3002\u30ce\u30fc\u30c9\u306e\u7dcf\u6570\u306f\\(2^{h+1} - 1\\)\u3068\u3057\u3066\u8a08\u7b97\u3067\u304d\u3001\u3053\u3053\u3067\\(h\\)\u306f\u6728\u306e\u9ad8\u3055\u3067\u3059\u3002\u3053\u308c\u306f\u6a19\u6e96\u7684\u306a\u6307\u6570\u95a2\u4fc2\u3092\u793a\u3057\u3001\u81ea\u7136\u754c\u306e\u7d30\u80de\u5206\u88c2\u306e\u4e00\u822c\u7684\u306a\u73fe\u8c61\u3092\u53cd\u6620\u3057\u3066\u3044\u307e\u3059\u3002

        Tip

        \u4e2d\u56fd\u8a9e\u570f\u3067\u306f\u3001\u5b8c\u5168\u4e8c\u5206\u6728\u306f\u3057\u3070\u3057\u3070\u6e80\u4e8c\u5206\u6728\u3068\u547c\u3070\u308c\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u56f3 7-4 \u00a0 \u5b8c\u5168\u4e8c\u5206\u6728

        "},{"location":"chapter_tree/binary_tree/#2_1","title":"2. \u00a0 \u5b8c\u5099\u4e8c\u5206\u6728","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5b8c\u5099\u4e8c\u5206\u6728\u306f\u3001\u6700\u4e0b\u4f4d\u30ec\u30d9\u30eb\u306e\u307f\u304c\u5b8c\u5168\u306b\u57cb\u3081\u3089\u308c\u3066\u3044\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308b\u4e8c\u5206\u6728\u3067\u3001\u6700\u4e0b\u4f4d\u30ec\u30d9\u30eb\u306e\u30ce\u30fc\u30c9\u306f\u5de6\u304b\u3089\u53f3\u306b\u9023\u7d9a\u3057\u3066\u57cb\u3081\u3089\u308c\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u5b8c\u5168\u4e8c\u5206\u6728\u306f\u5b8c\u5099\u4e8c\u5206\u6728\u3067\u3082\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u56f3 7-5 \u00a0 \u5b8c\u5099\u4e8c\u5206\u6728

        "},{"location":"chapter_tree/binary_tree/#3","title":"3. \u00a0 \u6e80\u4e8c\u5206\u6728","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u6e80\u4e8c\u5206\u6728\u3067\u306f\u3001\u8449\u30ce\u30fc\u30c9\u3092\u9664\u3044\u3066\u3001\u4ed6\u306e\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u304c2\u3064\u306e\u5b50\u30ce\u30fc\u30c9\u3092\u6301\u3061\u307e\u3059\u3002

        \u56f3 7-6 \u00a0 \u6e80\u4e8c\u5206\u6728

        "},{"location":"chapter_tree/binary_tree/#4","title":"4. \u00a0 \u5e73\u8861\u4e8c\u5206\u6728","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u5e73\u8861\u4e8c\u5206\u6728\u3067\u306f\u3001\u4efb\u610f\u306e\u30ce\u30fc\u30c9\u306e\u5de6\u3068\u53f3\u306e\u90e8\u5206\u6728\u306e\u9ad8\u3055\u306e\u7d76\u5bfe\u5dee\u304c1\u3092\u8d85\u3048\u307e\u305b\u3093\u3002

        \u56f3 7-7 \u00a0 \u5e73\u8861\u4e8c\u5206\u6728

        "},{"location":"chapter_tree/binary_tree/#714","title":"7.1.4 \u00a0 \u4e8c\u5206\u6728\u306e\u9000\u5316","text":"

        \u4e0b\u56f3\u306f\u3001\u4e8c\u5206\u6728\u306e\u7406\u60f3\u7684\u306a\u69cb\u9020\u3068\u9000\u5316\u3057\u305f\u69cb\u9020\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u4e8c\u5206\u6728\u306f\u3001\u3059\u3079\u3066\u306e\u30ec\u30d9\u30eb\u304c\u57cb\u3081\u3089\u308c\u3066\u3044\u308b\u3068\u304d\u306b\u300c\u5b8c\u5168\u4e8c\u5206\u6728\u300d\u306b\u306a\u308a\u3001\u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u304c\u4e00\u65b9\u306b\u504f\u3063\u3066\u3044\u308b\u3068\u304d\u306b\u300c\u9023\u7d50\u30ea\u30b9\u30c8\u300d\u306b\u9000\u5316\u3057\u307e\u3059\u3002

        • \u5b8c\u5168\u4e8c\u5206\u6728\u306f\u3001\u4e8c\u5206\u6728\u306e\u300c\u5206\u5272\u7d71\u6cbb\u6cd5\u300d\u306e\u5229\u70b9\u3092\u5341\u5206\u306b\u6d3b\u7528\u3067\u304d\u308b\u7406\u60f3\u7684\u306a\u30b7\u30ca\u30ea\u30aa\u3067\u3059\u3002
        • \u4e00\u65b9\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u5225\u306e\u6975\u7aef\u3092\u8868\u3057\u3001\u3059\u3079\u3066\u306e\u64cd\u4f5c\u304c\u7dda\u5f62\u306b\u306a\u308a\u3001\u6642\u9593\u8a08\u7b97\u91cf\u304c\\(O(n)\\)\u306b\u306a\u308a\u307e\u3059\u3002

        \u56f3 7-8 \u00a0 \u4e8c\u5206\u6728\u306e\u6700\u826f\u3068\u6700\u60aa\u306e\u69cb\u9020

        \u4e0b\u8868\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u6700\u826f\u3068\u6700\u60aa\u306e\u69cb\u9020\u3067\u306f\u3001\u4e8c\u5206\u6728\u306f\u8449\u30ce\u30fc\u30c9\u6570\u3001\u7dcf\u30ce\u30fc\u30c9\u6570\u3001\u9ad8\u3055\u306e\u6700\u5927\u5024\u307e\u305f\u306f\u6700\u5c0f\u5024\u3092\u9054\u6210\u3057\u307e\u3059\u3002

        \u8868 7-1 \u00a0 \u4e8c\u5206\u6728\u306e\u6700\u826f\u3068\u6700\u60aa\u306e\u69cb\u9020

        \u5b8c\u5168\u4e8c\u5206\u6728 \u9023\u7d50\u30ea\u30b9\u30c8 \u30ec\u30d9\u30eb\\(i\\)\u306e\u30ce\u30fc\u30c9\u6570 \\(2^{i-1}\\) \\(1\\) \u9ad8\u3055\\(h\\)\u306e\u6728\u306e\u8449\u30ce\u30fc\u30c9\u6570 \\(2^h\\) \\(1\\) \u9ad8\u3055\\(h\\)\u306e\u6728\u306e\u7dcf\u30ce\u30fc\u30c9\u6570 \\(2^{h+1} - 1\\) \\(h + 1\\) \u7dcf\u30ce\u30fc\u30c9\u6570\\(n\\)\u306e\u6728\u306e\u9ad8\u3055 \\(\\log_2 (n+1) - 1\\) \\(n - 1\\)"},{"location":"chapter_tree/binary_tree_traversal/","title":"7.2 \u00a0 \u4e8c\u5206\u6728\u306e\u8d70\u67fb","text":"

        \u7269\u7406\u7684\u69cb\u9020\u306e\u89b3\u70b9\u304b\u3089\u898b\u308b\u3068\u3001\u6728\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u57fa\u3065\u304f\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u305d\u306e\u8d70\u67fb\u65b9\u6cd5\u306f\u30dd\u30a4\u30f3\u30bf\u3092\u901a\u3057\u3066\u30ce\u30fc\u30c9\u306b\u4e00\u3064\u305a\u3064\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u3092\u542b\u307f\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u6728\u306f\u975e\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3042\u308b\u305f\u3081\u3001\u6728\u306e\u8d70\u67fb\u306f\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u8d70\u67fb\u3088\u308a\u3082\u8907\u96d1\u3067\u3001\u691c\u7d22\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u652f\u63f4\u304c\u5fc5\u8981\u3067\u3059\u3002

        \u4e8c\u5206\u6728\u306e\u4e00\u822c\u7684\u306a\u8d70\u67fb\u65b9\u6cd5\u306b\u306f\u3001\u30ec\u30d9\u30eb\u9806\u8d70\u67fb\u3001\u524d\u9806\u8d70\u67fb\u3001\u4e2d\u9806\u8d70\u67fb\u3001\u5f8c\u9806\u8d70\u67fb\u304c\u3042\u308a\u307e\u3059\u3002

        "},{"location":"chapter_tree/binary_tree_traversal/#721","title":"7.2.1 \u00a0 \u30ec\u30d9\u30eb\u9806\u8d70\u67fb","text":"

        \u4e0b\u56f3\u306b\u793a\u3059\u3088\u3046\u306b\u3001\u30ec\u30d9\u30eb\u9806\u8d70\u67fb\u306f\u4e8c\u5206\u6728\u3092\u4e0a\u304b\u3089\u4e0b\u3078\u3001\u5c64\u3054\u3068\u306b\u8d70\u67fb\u3057\u307e\u3059\u3002\u5404\u30ec\u30d9\u30eb\u5185\u3067\u306f\u3001\u5de6\u304b\u3089\u53f3\u3078\u30ce\u30fc\u30c9\u3092\u8a2a\u554f\u3057\u307e\u3059\u3002

        \u30ec\u30d9\u30eb\u9806\u8d70\u67fb\u306f\u672c\u8cea\u7684\u306b\u5e45\u512a\u5148\u8d70\u67fb\u306e\u4e00\u7a2e\u3067\u3001\u5e45\u512a\u5148\u63a2\u7d22\uff08BFS\uff09\u3068\u3082\u547c\u3070\u308c\u3001\u300c\u5468\u56f2\u306b\u5411\u304b\u3063\u3066\u5916\u5411\u304d\u306b\u62e1\u5f35\u3059\u308b\u300d\u5c64\u3054\u3068\u306e\u8d70\u67fb\u65b9\u6cd5\u3092\u4f53\u73fe\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 7-9 \u00a0 \u4e8c\u5206\u6728\u306e\u30ec\u30d9\u30eb\u9806\u8d70\u67fb

        "},{"location":"chapter_tree/binary_tree_traversal/#1","title":"1. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        \u5e45\u512a\u5148\u8d70\u67fb\u306f\u901a\u5e38\u300c\u30ad\u30e5\u30fc\u300d\u306e\u52a9\u3051\u3092\u501f\u308a\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002\u30ad\u30e5\u30fc\u306f\u300c\u5148\u5165\u308c\u5148\u51fa\u3057\u300d\u306e\u898f\u5247\u306b\u5f93\u3044\u3001\u5e45\u512a\u5148\u8d70\u67fb\u306f\u300c\u5c64\u3054\u3068\u306e\u9032\u884c\u300d\u898f\u5247\u306b\u5f93\u3044\u307e\u3059\u3002\u4e21\u8005\u306e\u57fa\u672c\u7684\u306a\u8003\u3048\u65b9\u306f\u4e00\u81f4\u3057\u3066\u3044\u307e\u3059\u3002\u5b9f\u88c5\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree_bfs.py
        def level_order(root: TreeNode | None) -> list[int]:\n    \"\"\"\u30ec\u30d9\u30eb\u9806\u8d70\u67fb\"\"\"\n    # \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316\u3057\u3001\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u8ffd\u52a0\n    queue: deque[TreeNode] = deque()\n    queue.append(root)\n    # \u8d70\u67fb\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u683c\u7d0d\u3059\u308b\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316\n    res = []\n    while queue:\n        node: TreeNode = queue.popleft()  # \u30ad\u30e5\u30fc\u304b\u3089\u30c7\u30ad\u30e5\u30fc\n        res.append(node.val)  # \u30ce\u30fc\u30c9\u306e\u5024\u3092\u4fdd\u5b58\n        if node.left is not None:\n            queue.append(node.left)  # \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\n        if node.right is not None:\n            queue.append(node.right)  # \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\n    return res\n
        binary_tree_bfs.cpp
        /* \u30ec\u30d9\u30eb\u9806\u8d70\u67fb */\nvector<int> levelOrder(TreeNode *root) {\n    // \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316\u3001\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u8ffd\u52a0\n    queue<TreeNode *> queue;\n    queue.push(root);\n    // \u8d70\u67fb\u9806\u5e8f\u3092\u4fdd\u5b58\u3059\u308b\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316\n    vector<int> vec;\n    while (!queue.empty()) {\n        TreeNode *node = queue.front();\n        queue.pop();              // \u30ad\u30e5\u30fc\u304b\u3089\u30c7\u30ad\u30e5\u30fc\n        vec.push_back(node->val); // \u30ce\u30fc\u30c9\u5024\u3092\u4fdd\u5b58\n        if (node->left != nullptr)\n            queue.push(node->left); // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\n        if (node->right != nullptr)\n            queue.push(node->right); // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\n    }\n    return vec;\n}\n
        binary_tree_bfs.java
        /* \u30ec\u30d9\u30eb\u9806\u8d70\u67fb */\nList<Integer> levelOrder(TreeNode root) {\n    // \u30ad\u30e5\u30fc\u3092\u521d\u671f\u5316\u3057\u3001\u6839\u30ce\u30fc\u30c9\u3092\u8ffd\u52a0\n    Queue<TreeNode> queue = new LinkedList<>();\n    queue.add(root);\n    // \u8d70\u67fb\u9806\u5e8f\u3092\u683c\u7d0d\u3059\u308b\u30ea\u30b9\u30c8\u3092\u521d\u671f\u5316\n    List<Integer> list = new ArrayList<>();\n    while (!queue.isEmpty()) {\n        TreeNode node = queue.poll(); // \u30ad\u30e5\u30fc\u306e\u30c7\u30ad\u30e5\u30fc\n        list.add(node.val);           // \u30ce\u30fc\u30c9\u306e\u5024\u3092\u4fdd\u5b58\n        if (node.left != null)\n            queue.offer(node.left);   // \u5de6\u306e\u5b50\u30ce\u30fc\u30c9\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\n        if (node.right != null)\n            queue.offer(node.right);  // \u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3092\u30a8\u30f3\u30ad\u30e5\u30fc\n    }\n    return list;\n}\n
        binary_tree_bfs.cs
        [class]{binary_tree_bfs}-[func]{LevelOrder}\n
        binary_tree_bfs.go
        [class]{}-[func]{levelOrder}\n
        binary_tree_bfs.swift
        [class]{}-[func]{levelOrder}\n
        binary_tree_bfs.js
        [class]{}-[func]{levelOrder}\n
        binary_tree_bfs.ts
        [class]{}-[func]{levelOrder}\n
        binary_tree_bfs.dart
        [class]{}-[func]{levelOrder}\n
        binary_tree_bfs.rs
        [class]{}-[func]{level_order}\n
        binary_tree_bfs.c
        [class]{}-[func]{levelOrder}\n
        binary_tree_bfs.kt
        [class]{}-[func]{levelOrder}\n
        binary_tree_bfs.rb
        [class]{}-[func]{level_order}\n
        "},{"location":"chapter_tree/binary_tree_traversal/#2","title":"2. \u00a0 \u8a08\u7b97\u91cf\u5206\u6790","text":"
        • \u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\): \u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u304c\u4e00\u5ea6\u305a\u3064\u8a2a\u554f\u3055\u308c\u3001\\(O(n)\\)\u306e\u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002\u3053\u3053\u3067\\(n\\)\u306f\u30ce\u30fc\u30c9\u6570\u3067\u3059\u3002
        • \u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\): \u6700\u60aa\u306e\u5834\u5408\u3001\u3064\u307e\u308a\u5b8c\u5168\u4e8c\u5206\u6728\u306e\u5834\u5408\u3001\u6700\u4e0b\u4f4d\u30ec\u30d9\u30eb\u306b\u8d70\u67fb\u3059\u308b\u524d\u306b\u3001\u30ad\u30e5\u30fc\u306f\u6700\u5927\\((n + 1) / 2\\)\u500b\u306e\u30ce\u30fc\u30c9\u3092\u540c\u6642\u306b\u542b\u3080\u3053\u3068\u304c\u3067\u304d\u3001\\(O(n)\\)\u306e\u7a7a\u9593\u3092\u5360\u6709\u3057\u307e\u3059\u3002
        "},{"location":"chapter_tree/binary_tree_traversal/#722","title":"7.2.2 \u00a0 \u524d\u9806\u3001\u4e2d\u9806\u3001\u5f8c\u9806\u8d70\u67fb","text":"

        \u5bfe\u5fdc\u3057\u3066\u3001\u524d\u9806\u3001\u4e2d\u9806\u3001\u5f8c\u9806\u8d70\u67fb\u306f\u3059\u3079\u3066\u6df1\u5ea6\u512a\u5148\u8d70\u67fb\u306b\u5c5e\u3057\u3001\u6df1\u5ea6\u512a\u5148\u63a2\u7d22\uff08DFS\uff09\u3068\u3082\u547c\u3070\u308c\u3001\u300c\u307e\u305a\u6700\u5f8c\u307e\u3067\u9032\u307f\u3001\u305d\u306e\u5f8c\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30af\u3057\u3066\u7d9a\u884c\u3059\u308b\u300d\u8d70\u67fb\u65b9\u6cd5\u3092\u4f53\u73fe\u3057\u3066\u3044\u307e\u3059\u3002

        \u4e0b\u56f3\u306f\u4e8c\u5206\u6728\u306b\u5bfe\u3057\u3066\u6df1\u5ea6\u512a\u5148\u8d70\u67fb\u3092\u5b9f\u884c\u3059\u308b\u52d5\u4f5c\u539f\u7406\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u6df1\u5ea6\u512a\u5148\u8d70\u67fb\u306f\u4e8c\u5206\u6728\u5168\u4f53\u3092\u300c\u6b69\u304d\u56de\u308b\u300d\u3088\u3046\u306a\u3082\u306e\u3067\u3001\u5404\u30ce\u30fc\u30c9\u30673\u3064\u306e\u4f4d\u7f6e\u306b\u906d\u9047\u3057\u3001\u305d\u308c\u3089\u306f\u524d\u9806\u3001\u4e2d\u9806\u3001\u5f8c\u9806\u8d70\u67fb\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u307e\u3059\u3002

        \u56f3 7-10 \u00a0 \u4e8c\u5206\u63a2\u7d22\u6728\u306e\u524d\u9806\u3001\u4e2d\u9806\u3001\u5f8c\u9806\u8d70\u67fb

        "},{"location":"chapter_tree/binary_tree_traversal/#1_1","title":"1. \u00a0 \u30b3\u30fc\u30c9\u5b9f\u88c5","text":"

        \u6df1\u5ea6\u512a\u5148\u63a2\u7d22\u306f\u901a\u5e38\u518d\u5e30\u306b\u57fa\u3065\u3044\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree_dfs.py
        def pre_order(root: TreeNode | None):\n    \"\"\"\u524d\u9806\u8d70\u67fb\"\"\"\n    if root is None:\n        return\n    # \u8a2a\u554f\u9806\u5e8f: \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9 -> \u5de6\u90e8\u5206\u6728 -> \u53f3\u90e8\u5206\u6728\n    res.append(root.val)\n    pre_order(root=root.left)\n    pre_order(root=root.right)\n\ndef in_order(root: TreeNode | None):\n    \"\"\"\u4e2d\u9806\u8d70\u67fb\"\"\"\n    if root is None:\n        return\n    # \u8a2a\u554f\u9806\u5e8f: \u5de6\u90e8\u5206\u6728 -> \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9 -> \u53f3\u90e8\u5206\u6728\n    in_order(root=root.left)\n    res.append(root.val)\n    in_order(root=root.right)\n\ndef post_order(root: TreeNode | None):\n    \"\"\"\u5f8c\u9806\u8d70\u67fb\"\"\"\n    if root is None:\n        return\n    # \u8a2a\u554f\u9806\u5e8f: \u5de6\u90e8\u5206\u6728 -> \u53f3\u90e8\u5206\u6728 -> \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\n    post_order(root=root.left)\n    post_order(root=root.right)\n    res.append(root.val)\n
        binary_tree_dfs.cpp
        /* \u524d\u9806\u8d70\u67fb */\nvoid preOrder(TreeNode *root) {\n    if (root == nullptr)\n        return;\n    // \u8a2a\u554f\u512a\u5148\u5ea6\uff1a\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9 -> \u5de6\u90e8\u5206\u6728 -> \u53f3\u90e8\u5206\u6728\n    vec.push_back(root->val);\n    preOrder(root->left);\n    preOrder(root->right);\n}\n\n/* \u4e2d\u9806\u8d70\u67fb */\nvoid inOrder(TreeNode *root) {\n    if (root == nullptr)\n        return;\n    // \u8a2a\u554f\u512a\u5148\u5ea6\uff1a\u5de6\u90e8\u5206\u6728 -> \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9 -> \u53f3\u90e8\u5206\u6728\n    inOrder(root->left);\n    vec.push_back(root->val);\n    inOrder(root->right);\n}\n\n/* \u5f8c\u9806\u8d70\u67fb */\nvoid postOrder(TreeNode *root) {\n    if (root == nullptr)\n        return;\n    // \u8a2a\u554f\u512a\u5148\u5ea6\uff1a\u5de6\u90e8\u5206\u6728 -> \u53f3\u90e8\u5206\u6728 -> \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\n    postOrder(root->left);\n    postOrder(root->right);\n    vec.push_back(root->val);\n}\n
        binary_tree_dfs.java
        /* \u524d\u9806\u8d70\u67fb */\nvoid preOrder(TreeNode root) {\n    if (root == null)\n        return;\n    // \u8a2a\u554f\u512a\u5148\u5ea6: \u6839\u30ce\u30fc\u30c9 -> \u5de6\u90e8\u5206\u6728 -> \u53f3\u90e8\u5206\u6728\n    list.add(root.val);\n    preOrder(root.left);\n    preOrder(root.right);\n}\n\n/* \u4e2d\u9806\u8d70\u67fb */\nvoid inOrder(TreeNode root) {\n    if (root == null)\n        return;\n    // \u8a2a\u554f\u512a\u5148\u5ea6: \u5de6\u90e8\u5206\u6728 -> \u6839\u30ce\u30fc\u30c9 -> \u53f3\u90e8\u5206\u6728\n    inOrder(root.left);\n    list.add(root.val);\n    inOrder(root.right);\n}\n\n/* \u5f8c\u9806\u8d70\u67fb */\nvoid postOrder(TreeNode root) {\n    if (root == null)\n        return;\n    // \u8a2a\u554f\u512a\u5148\u5ea6: \u5de6\u90e8\u5206\u6728 -> \u53f3\u90e8\u5206\u6728 -> \u6839\u30ce\u30fc\u30c9\n    postOrder(root.left);\n    postOrder(root.right);\n    list.add(root.val);\n}\n
        binary_tree_dfs.cs
        [class]{binary_tree_dfs}-[func]{PreOrder}\n\n[class]{binary_tree_dfs}-[func]{InOrder}\n\n[class]{binary_tree_dfs}-[func]{PostOrder}\n
        binary_tree_dfs.go
        [class]{}-[func]{preOrder}\n\n[class]{}-[func]{inOrder}\n\n[class]{}-[func]{postOrder}\n
        binary_tree_dfs.swift
        [class]{}-[func]{preOrder}\n\n[class]{}-[func]{inOrder}\n\n[class]{}-[func]{postOrder}\n
        binary_tree_dfs.js
        [class]{}-[func]{preOrder}\n\n[class]{}-[func]{inOrder}\n\n[class]{}-[func]{postOrder}\n
        binary_tree_dfs.ts
        [class]{}-[func]{preOrder}\n\n[class]{}-[func]{inOrder}\n\n[class]{}-[func]{postOrder}\n
        binary_tree_dfs.dart
        [class]{}-[func]{preOrder}\n\n[class]{}-[func]{inOrder}\n\n[class]{}-[func]{postOrder}\n
        binary_tree_dfs.rs
        [class]{}-[func]{pre_order}\n\n[class]{}-[func]{in_order}\n\n[class]{}-[func]{post_order}\n
        binary_tree_dfs.c
        [class]{}-[func]{preOrder}\n\n[class]{}-[func]{inOrder}\n\n[class]{}-[func]{postOrder}\n
        binary_tree_dfs.kt
        [class]{}-[func]{preOrder}\n\n[class]{}-[func]{inOrder}\n\n[class]{}-[func]{postOrder}\n
        binary_tree_dfs.rb
        [class]{}-[func]{pre_order}\n\n[class]{}-[func]{in_order}\n\n[class]{}-[func]{post_order}\n

        Tip

        \u6df1\u5ea6\u512a\u5148\u63a2\u7d22\u306f\u53cd\u5fa9\u306b\u57fa\u3065\u3044\u3066\u3082\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002\u8208\u5473\u306e\u3042\u308b\u8aad\u8005\u306f\u81ea\u5206\u3067\u5b66\u7fd2\u3057\u3066\u304f\u3060\u3055\u3044\u3002

        \u4e0b\u56f3\u306f\u4e8c\u5206\u6728\u306e\u524d\u9806\u8d70\u67fb\u306e\u518d\u5e30\u30d7\u30ed\u30bb\u30b9\u3092\u793a\u3057\u3066\u304a\u308a\u3001\u3053\u308c\u306f\u300c\u518d\u5e30\u300d\u3068\u300c\u5fa9\u5e30\u300d\u3068\u3044\u30462\u3064\u306e\u53cd\u5bfe\u306e\u90e8\u5206\u306b\u5206\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

        1. \u300c\u518d\u5e30\u300d\u306f\u65b0\u3057\u3044\u30e1\u30bd\u30c3\u30c9\u3092\u958b\u59cb\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u3053\u306e\u30d7\u30ed\u30bb\u30b9\u3067\u6b21\u306e\u30ce\u30fc\u30c9\u306b\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002
        2. \u300c\u5fa9\u5e30\u300d\u306f\u95a2\u6570\u304c\u623b\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u3001\u73fe\u5728\u306e\u30ce\u30fc\u30c9\u304c\u5b8c\u5168\u306b\u30a2\u30af\u30bb\u30b9\u3055\u308c\u305f\u3053\u3068\u3092\u793a\u3057\u307e\u3059\u3002
        <1><2><3><4><5><6><7><8><9><10><11>

        \u56f3 7-11 \u00a0 \u524d\u9806\u8d70\u67fb\u306e\u518d\u5e30\u30d7\u30ed\u30bb\u30b9

        "},{"location":"chapter_tree/binary_tree_traversal/#2_1","title":"2. \u00a0 \u8a08\u7b97\u91cf\u5206\u6790","text":"
        • \u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\): \u3059\u3079\u3066\u306e\u30ce\u30fc\u30c9\u304c\u4e00\u5ea6\u305a\u3064\u8a2a\u554f\u3055\u308c\u3001\\(O(n)\\)\u306e\u6642\u9593\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
        • \u7a7a\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\): \u6700\u60aa\u306e\u5834\u5408\u3001\u3064\u307e\u308a\u6728\u304c\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u9000\u5316\u3057\u305f\u5834\u5408\u3001\u518d\u5e30\u306e\u6df1\u3055\u306f\\(n\\)\u306b\u9054\u3057\u3001\u30b7\u30b9\u30c6\u30e0\u306f\\(O(n)\\)\u306e\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u7a7a\u9593\u3092\u5360\u6709\u3057\u307e\u3059\u3002
        "},{"location":"chapter_tree/summary/","title":"7.6 \u00a0 \u307e\u3068\u3081","text":""},{"location":"chapter_tree/summary/#1","title":"1. \u00a0 \u91cd\u8981\u306a\u30dd\u30a4\u30f3\u30c8","text":"
        • \u4e8c\u5206\u6728\u306f\u975e\u7dda\u5f62\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3001\u300c\u4e00\u3064\u3092\u4e8c\u3064\u306b\u5206\u3051\u308b\u300d\u5206\u5272\u7d71\u6cbb\u306e\u30ed\u30b8\u30c3\u30af\u3092\u53cd\u6620\u3057\u3066\u3044\u307e\u3059\u3002\u5404\u4e8c\u5206\u6728\u30ce\u30fc\u30c9\u306b\u306f\u5024\u30682\u3064\u306e\u30dd\u30a4\u30f3\u30bf\u304c\u542b\u307e\u308c\u3001\u305d\u308c\u305e\u308c\u5de6\u3068\u53f3\u306e\u5b50\u30ce\u30fc\u30c9\u3092\u6307\u3057\u307e\u3059\u3002
        • \u4e8c\u5206\u6728\u306e\u30ce\u30fc\u30c9\u306b\u3064\u3044\u3066\u3001\u305d\u306e\u5de6\uff08\u53f3\uff09\u5b50\u30ce\u30fc\u30c9\u3068\u305d\u306e\u4e0b\u306b\u5f62\u6210\u3055\u308c\u308b\u6728\u306f\u3001\u307e\u3068\u3081\u3066\u305d\u306e\u30ce\u30fc\u30c9\u306e\u5de6\uff08\u53f3\uff09\u90e8\u5206\u6728\u3068\u547c\u3070\u308c\u307e\u3059\u3002
        • \u4e8c\u5206\u6728\u306b\u95a2\u9023\u3059\u308b\u7528\u8a9e\u306b\u306f\u3001\u6839\u30ce\u30fc\u30c9\u3001\u8449\u30ce\u30fc\u30c9\u3001\u30ec\u30d9\u30eb\u3001\u6b21\u6570\u3001\u30a8\u30c3\u30b8\u3001\u9ad8\u3055\u3001\u6df1\u3055\u304c\u3042\u308a\u307e\u3059\u3002
        • \u4e8c\u5206\u6728\u306e\u521d\u671f\u5316\u3001\u30ce\u30fc\u30c9\u306e\u633f\u5165\u3001\u30ce\u30fc\u30c9\u306e\u524a\u9664\u306e\u64cd\u4f5c\u306f\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306e\u64cd\u4f5c\u3068\u4f3c\u3066\u3044\u307e\u3059\u3002
        • \u4e00\u822c\u7684\u306a\u4e8c\u5206\u6728\u306e\u7a2e\u985e\u306b\u306f\u3001\u5b8c\u5168\u4e8c\u5206\u6728\u3001\u5b8c\u5099\u4e8c\u5206\u6728\u3001\u6e80\u4e8c\u5206\u6728\u3001\u5e73\u8861\u4e8c\u5206\u6728\u304c\u3042\u308a\u307e\u3059\u3002\u5b8c\u5168\u4e8c\u5206\u6728\u306f\u7406\u60f3\u7684\u306a\u72b6\u614b\u3092\u8868\u3057\u3001\u9023\u7d50\u30ea\u30b9\u30c8\u306f\u9000\u5316\u5f8c\u306e\u6700\u60aa\u306e\u72b6\u614b\u3067\u3059\u3002
        • \u4e8c\u5206\u6728\u306f\u3001\u30ce\u30fc\u30c9\u5024\u3068\u7a7a\u304d\u30b9\u30ed\u30c3\u30c8\u3092\u30ec\u30d9\u30eb\u9806\u8d70\u67fb\u30b7\u30fc\u30b1\u30f3\u30b9\u3067\u914d\u7f6e\u3057\u3001\u89aa\u30ce\u30fc\u30c9\u3068\u5b50\u30ce\u30fc\u30c9\u9593\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u30de\u30c3\u30d4\u30f3\u30b0\u95a2\u4fc2\u306b\u57fa\u3065\u3044\u3066\u30dd\u30a4\u30f3\u30bf\u3092\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3067\u3001\u914d\u5217\u3092\u4f7f\u7528\u3057\u3066\u8868\u73fe\u3067\u304d\u307e\u3059\u3002
        • \u4e8c\u5206\u6728\u306e\u30ec\u30d9\u30eb\u9806\u8d70\u67fb\u306f\u5e45\u512a\u5148\u63a2\u7d22\u624b\u6cd5\u3067\u3001\u300c\u5186\u3092\u62e1\u5927\u3057\u306a\u304c\u3089\u300d\u306e\u5c64\u3054\u3068\u306e\u8d70\u67fb\u65b9\u5f0f\u3092\u53cd\u6620\u3057\u3066\u3044\u307e\u3059\u3002\u901a\u5e38\u306f\u30ad\u30e5\u30fc\u3092\u4f7f\u7528\u3057\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002
        • \u524d\u9806\u3001\u4e2d\u9806\u3001\u5f8c\u9806\u8d70\u67fb\u306f\u3059\u3079\u3066\u6df1\u5ea6\u512a\u5148\u63a2\u7d22\u624b\u6cd5\u3067\u3001\u300c\u307e\u305a\u6700\u5f8c\u307e\u3067\u884c\u304d\u3001\u305d\u306e\u5f8c\u30d0\u30c3\u30af\u30c8\u30e9\u30c3\u30af\u3057\u3066\u7d9a\u884c\u3059\u308b\u300d\u8d70\u67fb\u65b9\u5f0f\u3092\u53cd\u6620\u3057\u3066\u3044\u307e\u3059\u3002\u901a\u5e38\u306f\u518d\u5e30\u3092\u4f7f\u7528\u3057\u3066\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002
        • \u4e8c\u5206\u63a2\u7d22\u6728\u306f\u8981\u7d20\u691c\u7d22\u306e\u305f\u3081\u306e\u52b9\u7387\u7684\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3001\u691c\u7d22\u3001\u633f\u5165\u3001\u524a\u9664\u64cd\u4f5c\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\u3059\u3079\u3066\\(O(\\log n)\\)\u3067\u3059\u3002\u4e8c\u5206\u63a2\u7d22\u6728\u304c\u9023\u7d50\u30ea\u30b9\u30c8\u306b\u9000\u5316\u3059\u308b\u3068\u3001\u3053\u308c\u3089\u306e\u6642\u9593\u8a08\u7b97\u91cf\u306f\\(O(n)\\)\u306b\u60aa\u5316\u3057\u307e\u3059\u3002
        • AVL\u6728\u306f\u5e73\u8861\u4e8c\u5206\u63a2\u7d22\u6728\u3068\u3082\u547c\u3070\u308c\u3001\u56de\u8ee2\u64cd\u4f5c\u3092\u901a\u3057\u3066\u7d99\u7d9a\u7684\u306a\u30ce\u30fc\u30c9\u633f\u5165\u3068\u524a\u9664\u5f8c\u3082\u6728\u304c\u5e73\u8861\u3092\u4fdd\u3064\u3053\u3068\u3092\u4fdd\u8a3c\u3057\u307e\u3059\u3002
        • AVL\u6728\u306e\u56de\u8ee2\u64cd\u4f5c\u306b\u306f\u3001\u53f3\u56de\u8ee2\u3001\u5de6\u56de\u8ee2\u3001\u53f3\u5de6\u56de\u8ee2\u3001\u5de6\u53f3\u56de\u8ee2\u304c\u3042\u308a\u307e\u3059\u3002\u30ce\u30fc\u30c9\u306e\u633f\u5165\u307e\u305f\u306f\u524a\u9664\u5f8c\u3001AVL\u6728\u306f\u30dc\u30c8\u30e0\u30a2\u30c3\u30d7\u65b9\u5f0f\u3067\u3053\u308c\u3089\u306e\u56de\u8ee2\u3092\u5b9f\u884c\u3057\u3066\u81ea\u5df1\u5e73\u8861\u3092\u53d6\u308a\u307e\u3059\u3002
        "},{"location":"chapter_tree/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q: \u4e00\u3064\u306e\u30ce\u30fc\u30c9\u306e\u307f\u3092\u6301\u3064\u4e8c\u5206\u6728\u306b\u3064\u3044\u3066\u3001\u6728\u306e\u9ad8\u3055\u3068\u6839\u30ce\u30fc\u30c9\u306e\u6df1\u3055\u306e\u4e21\u65b9\u304c\\(0\\)\u3067\u3059\u304b\uff1f

        \u306f\u3044\u3001\u9ad8\u3055\u3068\u6df1\u3055\u306f\u901a\u5e38\u300c\u901a\u904e\u3057\u305f\u30a8\u30c3\u30b8\u306e\u6570\u300d\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u308b\u305f\u3081\u3067\u3059\u3002

        Q: \u4e8c\u5206\u6728\u306b\u304a\u3051\u308b\u633f\u5165\u3068\u524a\u9664\u306f\u4e00\u822c\u7684\u306b\u4e00\u9023\u306e\u64cd\u4f5c\u306b\u3088\u3063\u3066\u9054\u6210\u3055\u308c\u307e\u3059\u3002\u3053\u3053\u3067\u306e\u300c\u4e00\u9023\u306e\u64cd\u4f5c\u300d\u3068\u306f\u4f55\u3092\u6307\u3057\u307e\u3059\u304b\uff1f\u5b50\u30ce\u30fc\u30c9\u306e\u30ea\u30bd\u30fc\u30b9\u3092\u89e3\u653e\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u304b\uff1f

        \u4e8c\u5206\u63a2\u7d22\u6728\u3092\u4f8b\u306b\u53d6\u308b\u3068\u3001\u30ce\u30fc\u30c9\u3092\u524a\u9664\u3059\u308b\u64cd\u4f5c\u306f3\u3064\u306e\u7570\u306a\u308b\u30b7\u30ca\u30ea\u30aa\u3067\u51e6\u7406\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u305d\u308c\u305e\u308c\u8907\u6570\u30b9\u30c6\u30c3\u30d7\u306e\u30ce\u30fc\u30c9\u64cd\u4f5c\u304c\u5fc5\u8981\u3067\u3059\u3002

        Q: \u4e8c\u5206\u6728\u306eDFS\u8d70\u67fb\u3067\u524d\u9806\u3001\u4e2d\u9806\u3001\u5f8c\u9806\u306e3\u3064\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u304c\u3042\u308b\u306e\u306f\u306a\u305c\u3067\u3059\u304b\uff1f\u305d\u306e\u7528\u9014\u306f\u4f55\u3067\u3059\u304b\uff1f

        \u914d\u5217\u306e\u9806\u6b21\u304a\u3088\u3073\u9006\u9806\u8d70\u67fb\u3068\u540c\u69d8\u306b\u3001\u524d\u9806\u3001\u4e2d\u9806\u3001\u5f8c\u9806\u8d70\u67fb\u306f\u4e8c\u5206\u6728\u3092\u8d70\u67fb\u3059\u308b3\u3064\u306e\u65b9\u6cd5\u3067\u3042\u308a\u3001\u7279\u5b9a\u306e\u9806\u5e8f\u3067\u8d70\u67fb\u7d50\u679c\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u4e8c\u5206\u63a2\u7d22\u6728\u3067\u306f\u3001\u30ce\u30fc\u30c9\u30b5\u30a4\u30ba\u304c\u300c\u5de6\u5b50\u30ce\u30fc\u30c9\u5024 < \u6839\u30ce\u30fc\u30c9\u5024 < \u53f3\u5b50\u30ce\u30fc\u30c9\u5024\u300d\u3092\u6e80\u305f\u3059\u305f\u3081\u3001\u300c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u300d\u306e\u512a\u5148\u9806\u4f4d\u3067\u6728\u3092\u8d70\u67fb\u3059\u308b\u3053\u3068\u3067\u3001\u9806\u5e8f\u4ed8\u3051\u3089\u308c\u305f\u30ce\u30fc\u30c9\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002

        Q: \u4e0d\u5e73\u8861\u30ce\u30fc\u30c9node\u3001child\u3001grand_child\u9593\u306e\u95a2\u4fc2\u3092\u51e6\u7406\u3059\u308b\u53f3\u56de\u8ee2\u64cd\u4f5c\u306b\u304a\u3044\u3066\u3001\u53f3\u56de\u8ee2\u5f8c\u306bnode\u3068\u305d\u306e\u89aa\u30ce\u30fc\u30c9\u9593\u306e\u63a5\u7d9a\u3068node\u306e\u5143\u306e\u30ea\u30f3\u30af\u304c\u5931\u308f\u308c\u308b\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u304b\uff1f

        \u3053\u306e\u554f\u984c\u3092\u518d\u5e30\u7684\u306a\u89b3\u70b9\u304b\u3089\u898b\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002right_rotate(root)\u64cd\u4f5c\u306f\u90e8\u5206\u6728\u306e\u6839\u30ce\u30fc\u30c9\u3092\u6e21\u3057\u3001\u6700\u7d42\u7684\u306breturn child\u3067\u56de\u8ee2\u3055\u308c\u305f\u90e8\u5206\u6728\u306e\u6839\u30ce\u30fc\u30c9\u3092\u8fd4\u3057\u307e\u3059\u3002\u90e8\u5206\u6728\u306e\u6839\u30ce\u30fc\u30c9\u3068\u305d\u306e\u89aa\u30ce\u30fc\u30c9\u9593\u306e\u63a5\u7d9a\u306f\u3001\u3053\u306e\u95a2\u6570\u304c\u623b\u3063\u305f\u5f8c\u306b\u78ba\u7acb\u3055\u308c\u3001\u3053\u308c\u306f\u53f3\u56de\u8ee2\u64cd\u4f5c\u306e\u4fdd\u5b88\u7bc4\u56f2\u5916\u3067\u3059\u3002

        Q: C++\u3067\u306f\u3001\u95a2\u6570\u306fprivate\u3068public\u30bb\u30af\u30b7\u30e7\u30f3\u306b\u5206\u304b\u308c\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u306b\u306f\u3069\u306e\u3088\u3046\u306a\u8003\u616e\u4e8b\u9805\u304c\u3042\u308a\u307e\u3059\u304b\uff1f\u306a\u305cheight()\u95a2\u6570\u3068updateHeight()\u95a2\u6570\u304c\u305d\u308c\u305e\u308cpublic\u3068private\u306b\u914d\u7f6e\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3059\u304b\uff1f

        \u3053\u308c\u306f\u30e1\u30bd\u30c3\u30c9\u306e\u4f7f\u7528\u7bc4\u56f2\u306b\u3088\u308a\u307e\u3059\u3002\u30e1\u30bd\u30c3\u30c9\u304c\u30af\u30e9\u30b9\u5185\u3067\u306e\u307f\u4f7f\u7528\u3055\u308c\u308b\u5834\u5408\u3001private\u306b\u8a2d\u8a08\u3055\u308c\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u30e6\u30fc\u30b6\u30fc\u304c\u72ec\u81ea\u306bupdateHeight()\u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u306f\u610f\u5473\u304c\u3042\u308a\u307e\u305b\u3093\u3002\u3053\u308c\u306f\u633f\u5165\u307e\u305f\u306f\u524a\u9664\u64cd\u4f5c\u306e\u4e00\u30b9\u30c6\u30c3\u30d7\u306b\u904e\u304e\u306a\u3044\u304b\u3089\u3067\u3059\u3002\u3057\u304b\u3057\u3001height()\u306f\u30ce\u30fc\u30c9\u306e\u9ad8\u3055\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u305f\u3081\u306e\u3082\u306e\u3067\u3001vector.size()\u3068\u540c\u69d8\u3067\u3042\u308b\u305f\u3081\u3001\u4f7f\u7528\u306e\u305f\u3081\u306bpublic\u306b\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u307e\u3059\u3002

        Q: \u5165\u529b\u30c7\u30fc\u30bf\u306e\u30bb\u30c3\u30c8\u304b\u3089\u4e8c\u5206\u63a2\u7d22\u6728\u3092\u3069\u306e\u3088\u3046\u306b\u69cb\u7bc9\u3057\u307e\u3059\u304b\uff1f\u6839\u30ce\u30fc\u30c9\u306e\u9078\u629e\u306f\u975e\u5e38\u306b\u91cd\u8981\u3067\u3059\u304b\uff1f

        \u306f\u3044\u3001\u6728\u3092\u69cb\u7bc9\u3059\u308b\u65b9\u6cd5\u306f\u4e8c\u5206\u63a2\u7d22\u6728\u30b3\u30fc\u30c9\u306ebuild_tree()\u30e1\u30bd\u30c3\u30c9\u3067\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u6839\u30ce\u30fc\u30c9\u306e\u9078\u629e\u306b\u3064\u3044\u3066\u306f\u3001\u901a\u5e38\u5165\u529b\u30c7\u30fc\u30bf\u3092\u30bd\u30fc\u30c8\u3057\u3001\u4e2d\u592e\u306e\u8981\u7d20\u3092\u6839\u30ce\u30fc\u30c9\u3068\u3057\u3066\u9078\u629e\u3057\u3001\u518d\u5e30\u7684\u306b\u5de6\u3068\u53f3\u306e\u90e8\u5206\u6728\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002\u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\u6728\u306e\u5e73\u8861\u3092\u6700\u5927\u5316\u3057\u307e\u3059\u3002

        Q: Java\u3067\u306f\u3001\u6587\u5b57\u5217\u6bd4\u8f03\u306b\u5e38\u306bequals()\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u304b\uff1f

        Java\u3067\u306f\u3001\u30d7\u30ea\u30df\u30c6\u30a3\u30d6\u30c7\u30fc\u30bf\u578b\u306e\u5834\u5408\u3001==\u306f2\u3064\u306e\u5909\u6570\u306e\u5024\u304c\u7b49\u3057\u3044\u304b\u3069\u3046\u304b\u3092\u6bd4\u8f03\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u53c2\u7167\u578b\u306e\u5834\u5408\u30012\u3064\u306e\u30b7\u30f3\u30dc\u30eb\u306e\u52d5\u4f5c\u539f\u7406\u306f\u7570\u306a\u308a\u307e\u3059\u3002

        • ==: 2\u3064\u306e\u5909\u6570\u304c\u540c\u3058\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6307\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3001\u3064\u307e\u308a\u30e1\u30e2\u30ea\u5185\u306e\u4f4d\u7f6e\u304c\u540c\u3058\u304b\u3069\u3046\u304b\u3092\u6bd4\u8f03\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
        • equals(): 2\u3064\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5024\u304c\u7b49\u3057\u3044\u304b\u3069\u3046\u304b\u3092\u6bd4\u8f03\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002

        \u3057\u305f\u304c\u3063\u3066\u3001\u5024\u3092\u6bd4\u8f03\u3059\u308b\u306b\u306fequals()\u3092\u4f7f\u7528\u3059\u3079\u304d\u3067\u3059\u3002\u305f\u3060\u3057\u3001String a = \"hi\"; String b = \"hi\";\u3067\u521d\u671f\u5316\u3055\u308c\u305f\u6587\u5b57\u5217\u306f\u6587\u5b57\u5217\u5b9a\u6570\u30d7\u30fc\u30eb\u306b\u683c\u7d0d\u3055\u308c\u3001\u540c\u3058\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6307\u3059\u305f\u3081\u3001a == b\u30822\u3064\u306e\u6587\u5b57\u5217\u306e\u5185\u5bb9\u3092\u6bd4\u8f03\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002

        Q: \u6700\u4e0b\u4f4d\u30ec\u30d9\u30eb\u306b\u5230\u9054\u3059\u308b\u524d\u306b\u3001\u5e45\u512a\u5148\u8d70\u67fb\u3067\u30ad\u30e5\u30fc\u5185\u306e\u30ce\u30fc\u30c9\u6570\u306f\\(2^h\\)\u3067\u3059\u304b\uff1f

        \u306f\u3044\u3001\u4f8b\u3048\u3070\u9ad8\u3055\\(h = 2\\)\u306e\u6e80\u4e8c\u5206\u6728\u306f\u5408\u8a08\\(n = 7\\)\u500b\u306e\u30ce\u30fc\u30c9\u3092\u6301\u3061\u3001\u6700\u4e0b\u4f4d\u30ec\u30d9\u30eb\u306b\u306f\\(4 = 2^h = (n + 1) / 2\\)\u500b\u306e\u30ce\u30fc\u30c9\u304c\u3042\u308a\u307e\u3059\u3002

        "}]} \ No newline at end of file diff --git a/ja/stylesheets/extra.css b/ja/stylesheets/extra.css index 56e15f51a..8f95c3aeb 100644 --- a/ja/stylesheets/extra.css +++ b/ja/stylesheets/extra.css @@ -567,4 +567,4 @@ a:hover .text-button span { left: 50%; transform: translate(-50%, -50%); } -/*! update cache: 20260104155757 */ +/*! update cache: 20260104161456 */ diff --git a/javascripts/katex.js b/javascripts/katex.js index 136be59e2..1b4d04cda 100644 --- a/javascripts/katex.js +++ b/javascripts/katex.js @@ -8,4 +8,4 @@ document$.subscribe(({ body }) => { ], }); }); -/*! update cache: 20260104155727 */ +/*! update cache: 20260104161427 */ diff --git a/javascripts/mathjax.js b/javascripts/mathjax.js index b2367409b..ad76c4d73 100644 --- a/javascripts/mathjax.js +++ b/javascripts/mathjax.js @@ -15,4 +15,4 @@ window.MathJax = { document$.subscribe(() => { MathJax.typesetPromise(); }); -/*! update cache: 20260104155727 */ +/*! update cache: 20260104161427 */ diff --git a/javascripts/starfield.js b/javascripts/starfield.js index 30b2acb83..90175ceff 100644 --- a/javascripts/starfield.js +++ b/javascripts/starfield.js @@ -469,4 +469,4 @@ return Starfield; }); -/*! update cache: 20260104155727 */ +/*! update cache: 20260104161427 */ diff --git a/search/search_index.json b/search/search_index.json index fc9cfc505..75f591026 100644 --- a/search/search_index.json +++ b/search/search_index.json @@ -1 +1 @@ -{"config":{"lang":["en"],"separator":"[\\s\\u200b\\u3000\\-\u3001\u3002\uff0c\uff0e\uff1f\uff01\uff1b]+","pipeline":["stemmer"],"fields":{"title":{"boost":1000.0},"text":{"boost":1.0},"tags":{"boost":1000000.0}}},"docs":[{"location":"chapter_appendix/","title":"\u7b2c 16 \u7ae0 \u00a0 \u9644\u5f55","text":""},{"location":"chapter_appendix/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 16.1 \u00a0 \u7f16\u7a0b\u73af\u5883\u5b89\u88c5
        • 16.2 \u00a0 \u4e00\u8d77\u53c2\u4e0e\u521b\u4f5c
        • 16.3 \u00a0 \u672f\u8bed\u8868
        "},{"location":"chapter_appendix/contribution/","title":"16.2 \u00a0 \u4e00\u8d77\u53c2\u4e0e\u521b\u4f5c","text":"

        \u7531\u4e8e\u7b14\u8005\u80fd\u529b\u6709\u9650\uff0c\u4e66\u4e2d\u96be\u514d\u5b58\u5728\u4e00\u4e9b\u9057\u6f0f\u548c\u9519\u8bef\uff0c\u8bf7\u60a8\u8c05\u89e3\u3002\u5982\u679c\u60a8\u53d1\u73b0\u4e86\u7b14\u8bef\u3001\u94fe\u63a5\u5931\u6548\u3001\u5185\u5bb9\u7f3a\u5931\u3001\u6587\u5b57\u6b67\u4e49\u3001\u89e3\u91ca\u4e0d\u6e05\u6670\u6216\u884c\u6587\u7ed3\u6784\u4e0d\u5408\u7406\u7b49\u95ee\u9898\uff0c\u8bf7\u534f\u52a9\u6211\u4eec\u8fdb\u884c\u4fee\u6b63\uff0c\u4ee5\u7ed9\u8bfb\u8005\u63d0\u4f9b\u66f4\u4f18\u8d28\u7684\u5b66\u4e60\u8d44\u6e90\u3002

        \u6240\u6709\u64b0\u7a3f\u4eba\u7684 GitHub ID \u5c06\u5728\u672c\u4e66\u4ed3\u5e93\u3001\u7f51\u9875\u7248\u548c PDF \u7248\u7684\u4e3b\u9875\u4e0a\u8fdb\u884c\u5c55\u793a\uff0c\u4ee5\u611f\u8c22\u4ed6\u4eec\u5bf9\u5f00\u6e90\u793e\u533a\u7684\u65e0\u79c1\u5949\u732e\u3002

        \u5f00\u6e90\u7684\u9b45\u529b

        \u7eb8\u8d28\u56fe\u4e66\u7684\u4e24\u6b21\u5370\u5237\u7684\u95f4\u9694\u65f6\u95f4\u5f80\u5f80\u8f83\u4e45\uff0c\u5185\u5bb9\u66f4\u65b0\u975e\u5e38\u4e0d\u65b9\u4fbf\u3002

        \u800c\u5728\u672c\u5f00\u6e90\u4e66\u4e2d\uff0c\u5185\u5bb9\u66f4\u8fed\u7684\u65f6\u95f4\u88ab\u7f29\u77ed\u81f3\u6570\u65e5\u751a\u81f3\u51e0\u4e2a\u5c0f\u65f6\u3002

        "},{"location":"chapter_appendix/contribution/#1","title":"1. \u00a0 \u5185\u5bb9\u5fae\u8c03","text":"

        \u5982\u56fe 16-3 \u6240\u793a\uff0c\u6bcf\u4e2a\u9875\u9762\u7684\u53f3\u4e0a\u89d2\u90fd\u6709\u201c\u7f16\u8f91\u56fe\u6807\u201d\u3002\u60a8\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u4fee\u6539\u6587\u672c\u6216\u4ee3\u7801\u3002

        1. \u70b9\u51fb\u201c\u7f16\u8f91\u56fe\u6807\u201d\uff0c\u5982\u679c\u9047\u5230\u201c\u9700\u8981 Fork \u6b64\u4ed3\u5e93\u201d\u7684\u63d0\u793a\uff0c\u8bf7\u540c\u610f\u8be5\u64cd\u4f5c\u3002
        2. \u4fee\u6539 Markdown \u6e90\u6587\u4ef6\u5185\u5bb9\uff0c\u68c0\u67e5\u5185\u5bb9\u7684\u6b63\u786e\u6027\uff0c\u5e76\u5c3d\u91cf\u4fdd\u6301\u6392\u7248\u683c\u5f0f\u7684\u7edf\u4e00\u3002
        3. \u5728\u9875\u9762\u5e95\u90e8\u586b\u5199\u4fee\u6539\u8bf4\u660e\uff0c\u7136\u540e\u70b9\u51fb\u201cPropose file change\u201d\u6309\u94ae\u3002\u9875\u9762\u8df3\u8f6c\u540e\uff0c\u70b9\u51fb\u201cCreate pull request\u201d\u6309\u94ae\u5373\u53ef\u53d1\u8d77\u62c9\u53d6\u8bf7\u6c42\u3002

        \u56fe 16-3 \u00a0 \u9875\u9762\u7f16\u8f91\u6309\u952e

        \u56fe\u7247\u65e0\u6cd5\u76f4\u63a5\u4fee\u6539\uff0c\u9700\u8981\u901a\u8fc7\u65b0\u5efa Issue \u6216\u8bc4\u8bba\u7559\u8a00\u6765\u63cf\u8ff0\u95ee\u9898\uff0c\u6211\u4eec\u4f1a\u5c3d\u5feb\u91cd\u65b0\u7ed8\u5236\u5e76\u66ff\u6362\u56fe\u7247\u3002

        "},{"location":"chapter_appendix/contribution/#2","title":"2. \u00a0 \u5185\u5bb9\u521b\u4f5c","text":"

        \u5982\u679c\u60a8\u6709\u5174\u8da3\u53c2\u4e0e\u6b64\u5f00\u6e90\u9879\u76ee\uff0c\u5305\u62ec\u5c06\u4ee3\u7801\u7ffb\u8bd1\u6210\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u3001\u6269\u5c55\u6587\u7ae0\u5185\u5bb9\u7b49\uff0c\u90a3\u4e48\u9700\u8981\u5b9e\u65bd\u4ee5\u4e0b Pull Request \u5de5\u4f5c\u6d41\u7a0b\u3002

        1. \u767b\u5f55 GitHub \uff0c\u5c06\u672c\u4e66\u7684\u4ee3\u7801\u4ed3\u5e93 Fork \u5230\u4e2a\u4eba\u8d26\u53f7\u4e0b\u3002
        2. \u8fdb\u5165\u60a8\u7684 Fork \u4ed3\u5e93\u7f51\u9875\uff0c\u4f7f\u7528 git clone \u547d\u4ee4\u5c06\u4ed3\u5e93\u514b\u9686\u81f3\u672c\u5730\u3002
        3. \u5728\u672c\u5730\u8fdb\u884c\u5185\u5bb9\u521b\u4f5c\uff0c\u5e76\u8fdb\u884c\u5b8c\u6574\u6d4b\u8bd5\uff0c\u9a8c\u8bc1\u4ee3\u7801\u7684\u6b63\u786e\u6027\u3002
        4. \u5c06\u672c\u5730\u6240\u505a\u66f4\u6539 Commit \uff0c\u7136\u540e Push \u81f3\u8fdc\u7a0b\u4ed3\u5e93\u3002
        5. \u5237\u65b0\u4ed3\u5e93\u7f51\u9875\uff0c\u70b9\u51fb\u201cCreate pull request\u201d\u6309\u94ae\u5373\u53ef\u53d1\u8d77\u62c9\u53d6\u8bf7\u6c42\u3002
        "},{"location":"chapter_appendix/contribution/#3-docker","title":"3. \u00a0 Docker \u90e8\u7f72","text":"

        \u5728 hello-algo \u6839\u76ee\u5f55\u4e0b\uff0c\u6267\u884c\u4ee5\u4e0b Docker \u811a\u672c\uff0c\u5373\u53ef\u5728 http://localhost:8000 \u8bbf\u95ee\u672c\u9879\u76ee\uff1a

        docker-compose up -d\n

        \u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5373\u53ef\u5220\u9664\u90e8\u7f72\uff1a

        docker-compose down\n
        "},{"location":"chapter_appendix/installation/","title":"16.1 \u00a0 \u7f16\u7a0b\u73af\u5883\u5b89\u88c5","text":""},{"location":"chapter_appendix/installation/#1611-ide","title":"16.1.1 \u00a0 \u5b89\u88c5 IDE","text":"

        \u63a8\u8350\u4f7f\u7528\u5f00\u6e90\u3001\u8f7b\u91cf\u7684 VS Code \u4f5c\u4e3a\u672c\u5730\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u3002\u8bbf\u95ee VS Code \u5b98\u7f51\uff0c\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u9009\u62e9\u76f8\u5e94\u7248\u672c\u7684 VS Code \u8fdb\u884c\u4e0b\u8f7d\u548c\u5b89\u88c5\u3002

        \u56fe 16-1 \u00a0 \u4ece\u5b98\u7f51\u4e0b\u8f7d VS Code

        VS Code \u62e5\u6709\u5f3a\u5927\u7684\u6269\u5c55\u5305\u751f\u6001\u7cfb\u7edf\uff0c\u652f\u6301\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u7684\u8fd0\u884c\u548c\u8c03\u8bd5\u3002\u4ee5 Python \u4e3a\u4f8b\uff0c\u5b89\u88c5\u201cPython Extension Pack\u201d\u6269\u5c55\u5305\u4e4b\u540e\uff0c\u5373\u53ef\u8fdb\u884c Python \u4ee3\u7801\u8c03\u8bd5\u3002\u5b89\u88c5\u6b65\u9aa4\u5982\u56fe 16-2 \u6240\u793a\u3002

        \u56fe 16-2 \u00a0 \u5b89\u88c5 VS Code \u6269\u5c55\u5305

        "},{"location":"chapter_appendix/installation/#1612","title":"16.1.2 \u00a0 \u5b89\u88c5\u8bed\u8a00\u73af\u5883","text":""},{"location":"chapter_appendix/installation/#1-python","title":"1. \u00a0 Python \u73af\u5883","text":"
        1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Miniconda3 \uff0c\u9700\u8981 Python 3.10 \u6216\u66f4\u65b0\u7248\u672c\u3002
        2. \u5728 VS Code \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 python \uff0c\u5b89\u88c5 Python Extension Pack \u3002
        3. \uff08\u53ef\u9009\uff09\u5728\u547d\u4ee4\u884c\u8f93\u5165 pip install black \uff0c\u5b89\u88c5\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\u3002
        "},{"location":"chapter_appendix/installation/#2-cc","title":"2. \u00a0 C/C++ \u73af\u5883","text":"
        1. Windows \u7cfb\u7edf\u9700\u8981\u5b89\u88c5 MinGW\uff08\u914d\u7f6e\u6559\u7a0b\uff09\uff1bMacOS \u81ea\u5e26 Clang \uff0c\u65e0\u987b\u5b89\u88c5\u3002
        2. \u5728 VS Code \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 c++ \uff0c\u5b89\u88c5 C/C++ Extension Pack \u3002
        3. \uff08\u53ef\u9009\uff09\u6253\u5f00 Settings \u9875\u9762\uff0c\u641c\u7d22 Clang_format_fallback Style \u4ee3\u7801\u683c\u5f0f\u5316\u9009\u9879\uff0c\u8bbe\u7f6e\u4e3a { BasedOnStyle: Microsoft, BreakBeforeBraces: Attach } \u3002
        "},{"location":"chapter_appendix/installation/#3-java","title":"3. \u00a0 Java \u73af\u5883","text":"
        1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 OpenJDK\uff08\u7248\u672c\u9700\u6ee1\u8db3 > JDK 9\uff09\u3002
        2. \u5728 VS Code \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 java \uff0c\u5b89\u88c5 Extension Pack for Java \u3002
        "},{"location":"chapter_appendix/installation/#4-c","title":"4. \u00a0 C# \u73af\u5883","text":"
        1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 .Net 8.0 \u3002
        2. \u5728 VS Code \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 C# Dev Kit \uff0c\u5b89\u88c5 C# Dev Kit \uff08\u914d\u7f6e\u6559\u7a0b\uff09\u3002
        3. \u4e5f\u53ef\u4f7f\u7528 Visual Studio\uff08\u5b89\u88c5\u6559\u7a0b\uff09\u3002
        "},{"location":"chapter_appendix/installation/#5-go","title":"5. \u00a0 Go \u73af\u5883","text":"
        1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 go \u3002
        2. \u5728 VS Code \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 go \uff0c\u5b89\u88c5 Go \u3002
        3. \u6309\u5feb\u6377\u952e Ctrl + Shift + P \u547c\u51fa\u547d\u4ee4\u680f\uff0c\u8f93\u5165 go \uff0c\u9009\u62e9 Go: Install/Update Tools \uff0c\u5168\u90e8\u52fe\u9009\u5e76\u5b89\u88c5\u5373\u53ef\u3002
        "},{"location":"chapter_appendix/installation/#6-swift","title":"6. \u00a0 Swift \u73af\u5883","text":"
        1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Swift \u3002
        2. \u5728 VS Code \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 swift \uff0c\u5b89\u88c5 Swift for Visual Studio Code \u3002
        "},{"location":"chapter_appendix/installation/#7-javascript","title":"7. \u00a0 JavaScript \u73af\u5883","text":"
        1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Node.js \u3002
        2. \uff08\u53ef\u9009\uff09\u5728 VS Code \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 Prettier \uff0c\u5b89\u88c5\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\u3002
        "},{"location":"chapter_appendix/installation/#8-typescript","title":"8. \u00a0 TypeScript \u73af\u5883","text":"
        1. \u540c JavaScript \u73af\u5883\u5b89\u88c5\u6b65\u9aa4\u3002
        2. \u5b89\u88c5 TypeScript Execute (tsx) \u3002
        3. \u5728 VS Code \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 typescript \uff0c\u5b89\u88c5 Pretty TypeScript Errors \u3002
        "},{"location":"chapter_appendix/installation/#9-dart","title":"9. \u00a0 Dart \u73af\u5883","text":"
        1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Dart \u3002
        2. \u5728 VS Code \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 dart \uff0c\u5b89\u88c5 Dart \u3002
        "},{"location":"chapter_appendix/installation/#10-rust","title":"10. \u00a0 Rust \u73af\u5883","text":"
        1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Rust \u3002
        2. \u5728 VS Code \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 rust \uff0c\u5b89\u88c5 rust-analyzer \u3002
        "},{"location":"chapter_appendix/terminology/","title":"16.3 \u00a0 \u672f\u8bed\u8868","text":"

        \u8868 16-1 \u5217\u51fa\u4e86\u4e66\u4e2d\u51fa\u73b0\u7684\u91cd\u8981\u672f\u8bed\uff0c\u503c\u5f97\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\u3002

        • \u5efa\u8bae\u8bb0\u4f4f\u540d\u8bcd\u7684\u82f1\u6587\u53eb\u6cd5\uff0c\u4ee5\u4fbf\u9605\u8bfb\u82f1\u6587\u6587\u732e\u3002
        • \u90e8\u5206\u540d\u8bcd\u5728\u7b80\u4f53\u4e2d\u6587\u548c\u7e41\u4f53\u4e2d\u6587\u4e0b\u7684\u53eb\u6cd5\u4e0d\u540c\u3002

        \u8868 16-1 \u00a0 \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u91cd\u8981\u540d\u8bcd

        English \u7b80\u4f53\u4e2d\u6587 \u7e41\u4f53\u4e2d\u6587 algorithm \u7b97\u6cd5 \u6f14\u7b97\u6cd5 data structure \u6570\u636e\u7ed3\u6784 \u8cc7\u6599\u7d50\u69cb code \u4ee3\u7801 \u7a0b\u5f0f\u78bc file \u6587\u4ef6 \u6a94\u6848 function \u51fd\u6570 \u51fd\u5f0f method \u65b9\u6cd5 \u65b9\u6cd5 variable \u53d8\u91cf \u8b8a\u6578 asymptotic complexity analysis \u6e10\u8fd1\u590d\u6742\u5ea6\u5206\u6790 \u6f38\u8fd1\u8907\u96dc\u5ea6\u5206\u6790 time complexity \u65f6\u95f4\u590d\u6742\u5ea6 \u6642\u9593\u8907\u96dc\u5ea6 space complexity \u7a7a\u95f4\u590d\u6742\u5ea6 \u7a7a\u9593\u8907\u96dc\u5ea6 loop \u5faa\u73af \u8ff4\u5708 iteration \u8fed\u4ee3 \u8fed\u4ee3 recursion \u9012\u5f52 \u905e\u8ff4 tail recursion \u5c3e\u9012\u5f52 \u5c3e\u905e\u8ff4 recursion tree \u9012\u5f52\u6811 \u905e\u8ff4\u6a39 big-\\(O\\) notation \u5927 \\(O\\) \u8bb0\u53f7 \u5927 \\(O\\) \u8a18\u865f asymptotic upper bound \u6e10\u8fd1\u4e0a\u754c \u6f38\u8fd1\u4e0a\u754c sign-magnitude \u539f\u7801 \u539f\u78bc 1\u2019s complement \u53cd\u7801 \u4e00\u88dc\u6578 2\u2019s complement \u8865\u7801 \u4e8c\u88dc\u6578 array \u6570\u7ec4 \u9663\u5217 index \u7d22\u5f15 \u7d22\u5f15 linked list \u94fe\u8868 \u93c8\u7d50\u4e32\u5217 linked list node, list node \u94fe\u8868\u8282\u70b9 \u93c8\u7d50\u4e32\u5217\u7bc0\u9ede head node \u5934\u8282\u70b9 \u982d\u7bc0\u9ede tail node \u5c3e\u8282\u70b9 \u5c3e\u7bc0\u9ede list \u5217\u8868 \u4e32\u5217 dynamic array \u52a8\u6001\u6570\u7ec4 \u52d5\u614b\u9663\u5217 hard disk \u786c\u76d8 \u786c\u789f random-access memory (RAM) \u5185\u5b58 \u8a18\u61b6\u9ad4 cache memory \u7f13\u5b58 \u5feb\u53d6 cache miss \u7f13\u5b58\u672a\u547d\u4e2d \u5feb\u53d6\u672a\u547d\u4e2d cache hit rate \u7f13\u5b58\u547d\u4e2d\u7387 \u5feb\u53d6\u547d\u4e2d\u7387 stack \u6808 \u5806\u758a top of the stack \u6808\u9876 \u5806\u758a\u9802 bottom of the stack \u6808\u5e95 \u5806\u758a\u5e95 queue \u961f\u5217 \u4f47\u5217 double-ended queue \u53cc\u5411\u961f\u5217 \u96d9\u5411\u4f47\u5217 front of the queue \u961f\u9996 \u4f47\u5217\u9996 rear of the queue \u961f\u5c3e \u4f47\u5217\u5c3e hash table \u54c8\u5e0c\u8868 \u96dc\u6e4a\u8868 hash set \u54c8\u5e0c\u96c6\u5408 \u96dc\u6e4a\u96c6\u5408 bucket \u6876 \u6876 hash function \u54c8\u5e0c\u51fd\u6570 \u96dc\u6e4a\u51fd\u5f0f hash collision \u54c8\u5e0c\u51b2\u7a81 \u96dc\u6e4a\u885d\u7a81 load factor \u8d1f\u8f7d\u56e0\u5b50 \u8ca0\u8f09\u56e0\u5b50 separate chaining \u94fe\u5f0f\u5730\u5740 \u93c8\u7d50\u4f4d\u5740 open addressing \u5f00\u653e\u5bfb\u5740 \u958b\u653e\u5b9a\u5740 linear probing \u7ebf\u6027\u63a2\u6d4b \u7dda\u6027\u63a2\u67e5 lazy deletion \u61d2\u5220\u9664 \u61f6\u522a\u9664 binary tree \u4e8c\u53c9\u6811 \u4e8c\u5143\u6a39 tree node \u6811\u8282\u70b9 \u6a39\u7bc0\u9ede left-child node \u5de6\u5b50\u8282\u70b9 \u5de6\u5b50\u7bc0\u9ede right-child node \u53f3\u5b50\u8282\u70b9 \u53f3\u5b50\u7bc0\u9ede parent node \u7236\u8282\u70b9 \u7236\u7bc0\u9ede left subtree \u5de6\u5b50\u6811 \u5de6\u5b50\u6a39 right subtree \u53f3\u5b50\u6811 \u53f3\u5b50\u6a39 root node \u6839\u8282\u70b9 \u6839\u7bc0\u9ede leaf node \u53f6\u8282\u70b9 \u8449\u7bc0\u9ede edge \u8fb9 \u908a level \u5c42 \u5c64 degree \u5ea6 \u5ea6 height \u9ad8\u5ea6 \u9ad8\u5ea6 depth \u6df1\u5ea6 \u6df1\u5ea6 perfect binary tree \u5b8c\u7f8e\u4e8c\u53c9\u6811 \u5b8c\u7f8e\u4e8c\u5143\u6a39 complete binary tree \u5b8c\u5168\u4e8c\u53c9\u6811 \u5b8c\u5168\u4e8c\u5143\u6a39 full binary tree \u5b8c\u6ee1\u4e8c\u53c9\u6811 \u5b8c\u6eff\u4e8c\u5143\u6a39 balanced binary tree \u5e73\u8861\u4e8c\u53c9\u6811 \u5e73\u8861\u4e8c\u5143\u6a39 binary search tree \u4e8c\u53c9\u641c\u7d22\u6811 \u4e8c\u5143\u641c\u5c0b\u6a39 AVL tree AVL \u6811 AVL \u6a39 red-black tree \u7ea2\u9ed1\u6811 \u7d05\u9ed1\u6a39 level-order traversal \u5c42\u5e8f\u904d\u5386 \u5c64\u5e8f\u8d70\u8a2a breadth-first traversal \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 \u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a depth-first traversal \u6df1\u5ea6\u4f18\u5148\u904d\u5386 \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a binary search tree \u4e8c\u53c9\u641c\u7d22\u6811 \u4e8c\u5143\u641c\u5c0b\u6a39 balanced binary search tree \u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811 \u5e73\u8861\u4e8c\u5143\u641c\u5c0b\u6a39 balance factor \u5e73\u8861\u56e0\u5b50 \u5e73\u8861\u56e0\u5b50 heap \u5806 \u5806\u7a4d max heap \u5927\u9876\u5806 \u5927\u9802\u5806\u7a4d min heap \u5c0f\u9876\u5806 \u5c0f\u9802\u5806\u7a4d priority queue \u4f18\u5148\u961f\u5217 \u512a\u5148\u4f47\u5217 heapify \u5806\u5316 \u5806\u7a4d\u5316 top-\\(k\\) problem Top-\\(k\\) \u95ee\u9898 Top-\\(k\\) \u554f\u984c graph \u56fe \u5716 vertex \u9876\u70b9 \u9802\u9ede undirected graph \u65e0\u5411\u56fe \u7121\u5411\u5716 directed graph \u6709\u5411\u56fe \u6709\u5411\u5716 connected graph \u8fde\u901a\u56fe \u9023\u901a\u5716 disconnected graph \u975e\u8fde\u901a\u56fe \u975e\u9023\u901a\u5716 weighted graph \u6709\u6743\u56fe \u6709\u6b0a\u5716 adjacency \u90bb\u63a5 \u9130\u63a5 path \u8def\u5f84 \u8def\u5f91 in-degree \u5165\u5ea6 \u5165\u5ea6 out-degree \u51fa\u5ea6 \u51fa\u5ea6 adjacency matrix \u90bb\u63a5\u77e9\u9635 \u9130\u63a5\u77e9\u9663 adjacency list \u90bb\u63a5\u8868 \u9130\u63a5\u8868 breadth-first search \u5e7f\u5ea6\u4f18\u5148\u641c\u7d22 \u5ee3\u5ea6\u512a\u5148\u641c\u5c0b depth-first search \u6df1\u5ea6\u4f18\u5148\u641c\u7d22 \u6df1\u5ea6\u512a\u5148\u641c\u5c0b binary search \u4e8c\u5206\u67e5\u627e \u4e8c\u5206\u641c\u5c0b searching algorithm \u641c\u7d22\u7b97\u6cd5 \u641c\u5c0b\u6f14\u7b97\u6cd5 sorting algorithm \u6392\u5e8f\u7b97\u6cd5 \u6392\u5e8f\u6f14\u7b97\u6cd5 selection sort \u9009\u62e9\u6392\u5e8f \u9078\u64c7\u6392\u5e8f bubble sort \u5192\u6ce1\u6392\u5e8f \u6ce1\u6cab\u6392\u5e8f insertion sort \u63d2\u5165\u6392\u5e8f \u63d2\u5165\u6392\u5e8f quick sort \u5feb\u901f\u6392\u5e8f \u5feb\u901f\u6392\u5e8f merge sort \u5f52\u5e76\u6392\u5e8f \u5408\u4f75\u6392\u5e8f heap sort \u5806\u6392\u5e8f \u5806\u7a4d\u6392\u5e8f bucket sort \u6876\u6392\u5e8f \u6876\u6392\u5e8f counting sort \u8ba1\u6570\u6392\u5e8f \u8a08\u6578\u6392\u5e8f radix sort \u57fa\u6570\u6392\u5e8f \u57fa\u6578\u6392\u5e8f divide and conquer \u5206\u6cbb \u5206\u6cbb hanota problem \u6c49\u8bfa\u5854\u95ee\u9898 \u6cb3\u5167\u5854\u554f\u984c backtracking algorithm \u56de\u6eaf\u7b97\u6cd5 \u56de\u6eaf\u6f14\u7b97\u6cd5 constraint \u7ea6\u675f \u7d04\u675f solution \u89e3 \u89e3 state \u72b6\u6001 \u72c0\u614b pruning \u526a\u679d \u526a\u679d permutations problem \u5168\u6392\u5217\u95ee\u9898 \u5168\u6392\u5217\u554f\u984c subset-sum problem \u5b50\u96c6\u548c\u95ee\u9898 \u5b50\u96c6\u5408\u554f\u984c \\(n\\)-queens problem \\(n\\) \u7687\u540e\u95ee\u9898 \\(n\\) \u7687\u540e\u554f\u984c dynamic programming \u52a8\u6001\u89c4\u5212 \u52d5\u614b\u898f\u5283 initial state \u521d\u59cb\u72b6\u6001 \u521d\u59cb\u72c0\u614b state-transition equation \u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b \u72c0\u614b\u8f49\u79fb\u65b9\u7a0b knapsack problem \u80cc\u5305\u95ee\u9898 \u80cc\u5305\u554f\u984c edit distance problem \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898 \u7de8\u8f2f\u8ddd\u96e2\u554f\u984c greedy algorithm \u8d2a\u5fc3\u7b97\u6cd5 \u8caa\u5a6a\u6f14\u7b97\u6cd5"},{"location":"chapter_array_and_linkedlist/","title":"\u7b2c 4 \u7ae0 \u00a0 \u6570\u7ec4\u4e0e\u94fe\u8868","text":"

        Abstract

        \u6570\u636e\u7ed3\u6784\u7684\u4e16\u754c\u5982\u540c\u4e00\u5835\u539a\u5b9e\u7684\u7816\u5899\u3002

        \u6570\u7ec4\u7684\u7816\u5757\u6574\u9f50\u6392\u5217\uff0c\u9010\u4e2a\u7d27\u8d34\u3002\u94fe\u8868\u7684\u7816\u5757\u5206\u6563\u5404\u5904\uff0c\u8fde\u63a5\u7684\u85e4\u8513\u81ea\u7531\u5730\u7a7f\u68ad\u4e8e\u7816\u7f1d\u4e4b\u95f4\u3002

        "},{"location":"chapter_array_and_linkedlist/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 4.1 \u00a0 \u6570\u7ec4
        • 4.2 \u00a0 \u94fe\u8868
        • 4.3 \u00a0 \u5217\u8868
        • 4.4 \u00a0 \u5185\u5b58\u4e0e\u7f13\u5b58 *
        • 4.5 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_array_and_linkedlist/array/","title":"4.1 \u00a0 \u6570\u7ec4","text":"

        \u6570\u7ec4\uff08array\uff09\u662f\u4e00\u79cd\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u5176\u5c06\u76f8\u540c\u7c7b\u578b\u7684\u5143\u7d20\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\u3002\u6211\u4eec\u5c06\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e\u79f0\u4e3a\u8be5\u5143\u7d20\u7684\u7d22\u5f15\uff08index\uff09\u3002\u56fe 4-1 \u5c55\u793a\u4e86\u6570\u7ec4\u7684\u4e3b\u8981\u6982\u5ff5\u548c\u5b58\u50a8\u65b9\u5f0f\u3002

        \u56fe 4-1 \u00a0 \u6570\u7ec4\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f

        "},{"location":"chapter_array_and_linkedlist/array/#411","title":"4.1.1 \u00a0 \u6570\u7ec4\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/array/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u6570\u7ec4","text":"

        \u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u7528\u6570\u7ec4\u7684\u4e24\u79cd\u521d\u59cb\u5316\u65b9\u5f0f\uff1a\u65e0\u521d\u59cb\u503c\u3001\u7ed9\u5b9a\u521d\u59cb\u503c\u3002\u5728\u672a\u6307\u5b9a\u521d\u59cb\u503c\u7684\u60c5\u51b5\u4e0b\uff0c\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4f1a\u5c06\u6570\u7ec4\u5143\u7d20\u521d\u59cb\u5316\u4e3a \\(0\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        # \u521d\u59cb\u5316\u6570\u7ec4\narr: list[int] = [0] * 5  # [ 0, 0, 0, 0, 0 ]\nnums: list[int] = [1, 3, 2, 5, 4]\n
        array.cpp
        /* \u521d\u59cb\u5316\u6570\u7ec4 */\n// \u5b58\u50a8\u5728\u6808\u4e0a\nint arr[5];\nint nums[5] = { 1, 3, 2, 5, 4 };\n// \u5b58\u50a8\u5728\u5806\u4e0a\uff08\u9700\u8981\u624b\u52a8\u91ca\u653e\u7a7a\u95f4\uff09\nint* arr1 = new int[5];\nint* nums1 = new int[5] { 1, 3, 2, 5, 4 };\n
        array.java
        /* \u521d\u59cb\u5316\u6570\u7ec4 */\nint[] arr = new int[5]; // { 0, 0, 0, 0, 0 }\nint[] nums = { 1, 3, 2, 5, 4 };\n
        array.cs
        /* \u521d\u59cb\u5316\u6570\u7ec4 */\nint[] arr = new int[5]; // [ 0, 0, 0, 0, 0 ]\nint[] nums = [1, 3, 2, 5, 4];\n
        array.go
        /* \u521d\u59cb\u5316\u6570\u7ec4 */\nvar arr [5]int\n// \u5728 Go \u4e2d\uff0c\u6307\u5b9a\u957f\u5ea6\u65f6\uff08[5]int\uff09\u4e3a\u6570\u7ec4\uff0c\u4e0d\u6307\u5b9a\u957f\u5ea6\u65f6\uff08[]int\uff09\u4e3a\u5207\u7247\n// \u7531\u4e8e Go \u7684\u6570\u7ec4\u88ab\u8bbe\u8ba1\u4e3a\u5728\u7f16\u8bd1\u671f\u786e\u5b9a\u957f\u5ea6\uff0c\u56e0\u6b64\u53ea\u80fd\u4f7f\u7528\u5e38\u91cf\u6765\u6307\u5b9a\u957f\u5ea6\n// \u4e3a\u4e86\u65b9\u4fbf\u5b9e\u73b0\u6269\u5bb9 extend() \u65b9\u6cd5\uff0c\u4ee5\u4e0b\u5c06\u5207\u7247\uff08Slice\uff09\u770b\u4f5c\u6570\u7ec4\uff08Array\uff09\nnums := []int{1, 3, 2, 5, 4}\n
        array.swift
        /* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr = Array(repeating: 0, count: 5) // [0, 0, 0, 0, 0]\nlet nums = [1, 3, 2, 5, 4]\n
        array.js
        /* \u521d\u59cb\u5316\u6570\u7ec4 */\nvar arr = new Array(5).fill(0);\nvar nums = [1, 3, 2, 5, 4];\n
        array.ts
        /* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr: number[] = new Array(5).fill(0);\nlet nums: number[] = [1, 3, 2, 5, 4];\n
        array.dart
        /* \u521d\u59cb\u5316\u6570\u7ec4 */\nList<int> arr = List.filled(5, 0); // [0, 0, 0, 0, 0]\nList<int> nums = [1, 3, 2, 5, 4];\n
        array.rs
        /* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr: [i32; 5] = [0; 5]; // [0, 0, 0, 0, 0]\nlet slice: &[i32] = &[0; 5];\n// \u5728 Rust \u4e2d\uff0c\u6307\u5b9a\u957f\u5ea6\u65f6\uff08[i32; 5]\uff09\u4e3a\u6570\u7ec4\uff0c\u4e0d\u6307\u5b9a\u957f\u5ea6\u65f6\uff08&[i32]\uff09\u4e3a\u5207\u7247\n// \u7531\u4e8e Rust \u7684\u6570\u7ec4\u88ab\u8bbe\u8ba1\u4e3a\u5728\u7f16\u8bd1\u671f\u786e\u5b9a\u957f\u5ea6\uff0c\u56e0\u6b64\u53ea\u80fd\u4f7f\u7528\u5e38\u91cf\u6765\u6307\u5b9a\u957f\u5ea6\n// Vector \u662f Rust \u4e00\u822c\u60c5\u51b5\u4e0b\u7528\u4f5c\u52a8\u6001\u6570\u7ec4\u7684\u7c7b\u578b\n// \u4e3a\u4e86\u65b9\u4fbf\u5b9e\u73b0\u6269\u5bb9 extend() \u65b9\u6cd5\uff0c\u4ee5\u4e0b\u5c06 vector \u770b\u4f5c\u6570\u7ec4\uff08array\uff09\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
        array.c
        /* \u521d\u59cb\u5316\u6570\u7ec4 */\nint arr[5] = { 0 }; // { 0, 0, 0, 0, 0 }\nint nums[5] = { 1, 3, 2, 5, 4 };\n
        array.kt
        /* \u521d\u59cb\u5316\u6570\u7ec4 */\nvar arr = IntArray(5) // { 0, 0, 0, 0, 0 }\nvar nums = intArrayOf(1, 3, 2, 5, 4)\n
        array.rb
        # \u521d\u59cb\u5316\u6570\u7ec4\narr = Array.new(5, 0)\nnums = [1, 3, 2, 5, 4]\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/array/#2","title":"2. \u00a0 \u8bbf\u95ee\u5143\u7d20","text":"

        \u6570\u7ec4\u5143\u7d20\u88ab\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\uff0c\u8fd9\u610f\u5473\u7740\u8ba1\u7b97\u6570\u7ec4\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u975e\u5e38\u5bb9\u6613\u3002\u7ed9\u5b9a\u6570\u7ec4\u5185\u5b58\u5730\u5740\uff08\u9996\u5143\u7d20\u5185\u5b58\u5730\u5740\uff09\u548c\u67d0\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u56fe 4-2 \u6240\u793a\u7684\u516c\u5f0f\u8ba1\u7b97\u5f97\u5230\u8be5\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\uff0c\u4ece\u800c\u76f4\u63a5\u8bbf\u95ee\u8be5\u5143\u7d20\u3002

        \u56fe 4-2 \u00a0 \u6570\u7ec4\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u8ba1\u7b97

        \u89c2\u5bdf\u56fe 4-2 \uff0c\u6211\u4eec\u53d1\u73b0\u6570\u7ec4\u9996\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u4e3a \\(0\\) \uff0c\u8fd9\u4f3c\u4e4e\u6709\u4e9b\u53cd\u76f4\u89c9\uff0c\u56e0\u4e3a\u4ece \\(1\\) \u5f00\u59cb\u8ba1\u6570\u4f1a\u66f4\u81ea\u7136\u3002\u4f46\u4ece\u5730\u5740\u8ba1\u7b97\u516c\u5f0f\u7684\u89d2\u5ea6\u770b\uff0c\u7d22\u5f15\u672c\u8d28\u4e0a\u662f\u5185\u5b58\u5730\u5740\u7684\u504f\u79fb\u91cf\u3002\u9996\u4e2a\u5143\u7d20\u7684\u5730\u5740\u504f\u79fb\u91cf\u662f \\(0\\) \uff0c\u56e0\u6b64\u5b83\u7684\u7d22\u5f15\u4e3a \\(0\\) \u662f\u5408\u7406\u7684\u3002

        \u5728\u6570\u7ec4\u4e2d\u8bbf\u95ee\u5143\u7d20\u975e\u5e38\u9ad8\u6548\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u968f\u673a\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u5143\u7d20\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def random_access(nums: list[int]) -> int:\n    \"\"\"\u968f\u673a\u8bbf\u95ee\u5143\u7d20\"\"\"\n    # \u5728\u533a\u95f4 [0, len(nums)-1] \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n    random_index = random.randint(0, len(nums) - 1)\n    # \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n    random_num = nums[random_index]\n    return random_num\n
        array.cpp
        /* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n    // \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n    int randomIndex = rand() % size;\n    // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
        array.java
        /* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int[] nums) {\n    // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n    int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);\n    // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
        array.cs
        /* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint RandomAccess(int[] nums) {\n    Random random = new();\n    // \u5728\u533a\u95f4 [0, nums.Length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n    int randomIndex = random.Next(nums.Length);\n    // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
        array.go
        /* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunc randomAccess(nums []int) (randomNum int) {\n    // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n    randomIndex := rand.Intn(len(nums))\n    // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n    randomNum = nums[randomIndex]\n    return\n}\n
        array.swift
        /* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunc randomAccess(nums: [Int]) -> Int {\n    // \u5728\u533a\u95f4 [0, nums.count) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n    let randomIndex = nums.indices.randomElement()!\n    // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n    let randomNum = nums[randomIndex]\n    return randomNum\n}\n
        array.js
        /* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunction randomAccess(nums) {\n    // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n    const random_index = Math.floor(Math.random() * nums.length);\n    // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n    const random_num = nums[random_index];\n    return random_num;\n}\n
        array.ts
        /* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunction randomAccess(nums: number[]): number {\n    // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n    const random_index = Math.floor(Math.random() * nums.length);\n    // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n    const random_num = nums[random_index];\n    return random_num;\n}\n
        array.dart
        /* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(List<int> nums) {\n  // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n  int randomIndex = Random().nextInt(nums.length);\n  // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n  int randomNum = nums[randomIndex];\n  return randomNum;\n}\n
        array.rs
        /* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfn random_access(nums: &[i32]) -> i32 {\n    // \u5728\u533a\u95f4 [0, nums.len()) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n    let random_index = rand::thread_rng().gen_range(0..nums.len());\n    // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n    let random_num = nums[random_index];\n    random_num\n}\n
        array.c
        /* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n    // \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n    int randomIndex = rand() % size;\n    // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
        array.kt
        /* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfun randomAccess(nums: IntArray): Int {\n    // \u5728\u533a\u95f4 [0, nums.size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n    val randomIndex = ThreadLocalRandom.current().nextInt(0, nums.size)\n    // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n    val randomNum = nums[randomIndex]\n    return randomNum\n}\n
        array.rb
        ### \u968f\u673a\u8bbf\u95ee\u5143\u7d20 ###\ndef random_access(nums)\n  # \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n  random_index = Random.rand(0...nums.length)\n\n  # \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n  nums[random_index]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/array/#3","title":"3. \u00a0 \u63d2\u5165\u5143\u7d20","text":"

        \u6570\u7ec4\u5143\u7d20\u5728\u5185\u5b58\u4e2d\u662f\u201c\u7d27\u6328\u7740\u7684\u201d\uff0c\u5b83\u4eec\u4e4b\u95f4\u6ca1\u6709\u7a7a\u95f4\u518d\u5b58\u653e\u4efb\u4f55\u6570\u636e\u3002\u5982\u56fe 4-3 \u6240\u793a\uff0c\u5982\u679c\u60f3\u5728\u6570\u7ec4\u4e2d\u95f4\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\uff0c\u5219\u9700\u8981\u5c06\u8be5\u5143\u7d20\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u4e4b\u540e\u518d\u628a\u5143\u7d20\u8d4b\u503c\u7ed9\u8be5\u7d22\u5f15\u3002

        \u56fe 4-3 \u00a0 \u6570\u7ec4\u63d2\u5165\u5143\u7d20\u793a\u4f8b

        \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u56fa\u5b9a\u7684\uff0c\u56e0\u6b64\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\u5fc5\u5b9a\u4f1a\u5bfc\u81f4\u6570\u7ec4\u5c3e\u90e8\u5143\u7d20\u201c\u4e22\u5931\u201d\u3002\u6211\u4eec\u5c06\u8fd9\u4e2a\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u7559\u5728\u201c\u5217\u8868\u201d\u7ae0\u8282\u4e2d\u8ba8\u8bba\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def insert(nums: list[int], num: int, index: int):\n    \"\"\"\u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num\"\"\"\n    # \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for i in range(len(nums) - 1, index, -1):\n        nums[i] = nums[i - 1]\n    # \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n    nums[index] = num\n
        array.cpp
        /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int *nums, int size, int num, int index) {\n    // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for (int i = size - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n    nums[index] = num;\n}\n
        array.java
        /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int[] nums, int num, int index) {\n    // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for (int i = nums.length - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n    nums[index] = num;\n}\n
        array.cs
        /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid Insert(int[] nums, int num, int index) {\n    // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for (int i = nums.Length - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n    nums[index] = num;\n}\n
        array.go
        /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunc insert(nums []int, num int, index int) {\n    // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for i := len(nums) - 1; i > index; i-- {\n        nums[i] = nums[i-1]\n    }\n    // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n    nums[index] = num\n}\n
        array.swift
        /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunc insert(nums: inout [Int], num: Int, index: Int) {\n    // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for i in nums.indices.dropFirst(index).reversed() {\n        nums[i] = nums[i - 1]\n    }\n    // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n    nums[index] = num\n}\n
        array.js
        /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunction insert(nums, num, index) {\n    // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for (let i = nums.length - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n    nums[index] = num;\n}\n
        array.ts
        /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunction insert(nums: number[], num: number, index: number): void {\n    // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for (let i = nums.length - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n    nums[index] = num;\n}\n
        array.dart
        /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 _num */\nvoid insert(List<int> nums, int _num, int index) {\n  // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n  for (var i = nums.length - 1; i > index; i--) {\n    nums[i] = nums[i - 1];\n  }\n  // \u5c06 _num \u8d4b\u7ed9 index \u5904\u5143\u7d20\n  nums[index] = _num;\n}\n
        array.rs
        /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfn insert(nums: &mut [i32], num: i32, index: usize) {\n    // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for i in (index + 1..nums.len()).rev() {\n        nums[i] = nums[i - 1];\n    }\n    // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n    nums[index] = num;\n}\n
        array.c
        /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int *nums, int size, int num, int index) {\n    // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for (int i = size - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n    nums[index] = num;\n}\n
        array.kt
        /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfun insert(nums: IntArray, num: Int, index: Int) {\n    // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for (i in nums.size - 1 downTo index + 1) {\n        nums[i] = nums[i - 1]\n    }\n    // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n    nums[index] = num\n}\n
        array.rb
        ### \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num ###\ndef insert(nums, num, index)\n  # \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n  for i in (nums.length - 1).downto(index + 1)\n    nums[i] = nums[i - 1]\n  end\n\n  # \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n  nums[index] = num\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/array/#4","title":"4. \u00a0 \u5220\u9664\u5143\u7d20","text":"

        \u540c\u7406\uff0c\u5982\u56fe 4-4 \u6240\u793a\uff0c\u82e5\u60f3\u5220\u9664\u7d22\u5f15 \\(i\\) \u5904\u7684\u5143\u7d20\uff0c\u5219\u9700\u8981\u628a\u7d22\u5f15 \\(i\\) \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\u3002

        \u56fe 4-4 \u00a0 \u6570\u7ec4\u5220\u9664\u5143\u7d20\u793a\u4f8b

        \u8bf7\u6ce8\u610f\uff0c\u5220\u9664\u5143\u7d20\u5b8c\u6210\u540e\uff0c\u539f\u5148\u672b\u5c3e\u7684\u5143\u7d20\u53d8\u5f97\u201c\u65e0\u610f\u4e49\u201d\u4e86\uff0c\u6240\u4ee5\u6211\u4eec\u65e0\u987b\u7279\u610f\u53bb\u4fee\u6539\u5b83\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def remove(nums: list[int], index: int):\n    \"\"\"\u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20\"\"\"\n    # \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for i in range(index, len(nums) - 1):\n        nums[i] = nums[i + 1]\n
        array.cpp
        /* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid remove(int *nums, int size, int index) {\n    // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for (int i = index; i < size - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
        array.java
        /* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid remove(int[] nums, int index) {\n    // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for (int i = index; i < nums.length - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
        array.cs
        /* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid Remove(int[] nums, int index) {\n    // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for (int i = index; i < nums.Length - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
        array.go
        /* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunc remove(nums []int, index int) {\n    // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for i := index; i < len(nums)-1; i++ {\n        nums[i] = nums[i+1]\n    }\n}\n
        array.swift
        /* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunc remove(nums: inout [Int], index: Int) {\n    // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for i in nums.indices.dropFirst(index).dropLast() {\n        nums[i] = nums[i + 1]\n    }\n}\n
        array.js
        /* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunction remove(nums, index) {\n    // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for (let i = index; i < nums.length - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
        array.ts
        /* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunction remove(nums: number[], index: number): void {\n    // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for (let i = index; i < nums.length - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
        array.dart
        /* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid remove(List<int> nums, int index) {\n  // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n  for (var i = index; i < nums.length - 1; i++) {\n    nums[i] = nums[i + 1];\n  }\n}\n
        array.rs
        /* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfn remove(nums: &mut [i32], index: usize) {\n    // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for i in index..nums.len() - 1 {\n        nums[i] = nums[i + 1];\n    }\n}\n
        array.c
        /* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nvoid removeItem(int *nums, int size, int index) {\n    // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for (int i = index; i < size - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
        array.kt
        /* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfun remove(nums: IntArray, index: Int) {\n    // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for (i in index..<nums.size - 1) {\n        nums[i] = nums[i + 1]\n    }\n}\n
        array.rb
        ### \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 ###\ndef remove(nums, index)\n  # \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n  for i in index...(nums.length - 1)\n    nums[i] = nums[i + 1]\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u603b\u7684\u6765\u770b\uff0c\u6570\u7ec4\u7684\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u6709\u4ee5\u4e0b\u7f3a\u70b9\u3002

        • \u65f6\u95f4\u590d\u6742\u5ea6\u9ad8\uff1a\u6570\u7ec4\u7684\u63d2\u5165\u548c\u5220\u9664\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u6570\u7ec4\u957f\u5ea6\u3002
        • \u4e22\u5931\u5143\u7d20\uff1a\u7531\u4e8e\u6570\u7ec4\u7684\u957f\u5ea6\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u5728\u63d2\u5165\u5143\u7d20\u540e\uff0c\u8d85\u51fa\u6570\u7ec4\u957f\u5ea6\u8303\u56f4\u7684\u5143\u7d20\u4f1a\u4e22\u5931\u3002
        • \u5185\u5b58\u6d6a\u8d39\uff1a\u6211\u4eec\u53ef\u4ee5\u521d\u59cb\u5316\u4e00\u4e2a\u6bd4\u8f83\u957f\u7684\u6570\u7ec4\uff0c\u53ea\u7528\u524d\u9762\u4e00\u90e8\u5206\uff0c\u8fd9\u6837\u5728\u63d2\u5165\u6570\u636e\u65f6\uff0c\u4e22\u5931\u7684\u672b\u5c3e\u5143\u7d20\u90fd\u662f\u201c\u65e0\u610f\u4e49\u201d\u7684\uff0c\u4f46\u8fd9\u6837\u505a\u4f1a\u9020\u6210\u90e8\u5206\u5185\u5b58\u7a7a\u95f4\u6d6a\u8d39\u3002
        "},{"location":"chapter_array_and_linkedlist/array/#5","title":"5. \u00a0 \u904d\u5386\u6570\u7ec4","text":"

        \u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u65e2\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u904d\u5386\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def traverse(nums: list[int]):\n    \"\"\"\u904d\u5386\u6570\u7ec4\"\"\"\n    count = 0\n    # \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n    for i in range(len(nums)):\n        count += nums[i]\n    # \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n    for num in nums:\n        count += num\n    # \u540c\u65f6\u904d\u5386\u6570\u636e\u7d22\u5f15\u548c\u5143\u7d20\n    for i, num in enumerate(nums):\n        count += nums[i]\n        count += num\n
        array.cpp
        /* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int *nums, int size) {\n    int count = 0;\n    // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n    for (int i = 0; i < size; i++) {\n        count += nums[i];\n    }\n}\n
        array.java
        /* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int[] nums) {\n    int count = 0;\n    // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n    for (int i = 0; i < nums.length; i++) {\n        count += nums[i];\n    }\n    // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n    for (int num : nums) {\n        count += num;\n    }\n}\n
        array.cs
        /* \u904d\u5386\u6570\u7ec4 */\nvoid Traverse(int[] nums) {\n    int count = 0;\n    // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n    for (int i = 0; i < nums.Length; i++) {\n        count += nums[i];\n    }\n    // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n    foreach (int num in nums) {\n        count += num;\n    }\n}\n
        array.go
        /* \u904d\u5386\u6570\u7ec4 */\nfunc traverse(nums []int) {\n    count := 0\n    // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n    for i := 0; i < len(nums); i++ {\n        count += nums[i]\n    }\n    count = 0\n    // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n    for _, num := range nums {\n        count += num\n    }\n    // \u540c\u65f6\u904d\u5386\u6570\u636e\u7d22\u5f15\u548c\u5143\u7d20\n    for i, num := range nums {\n        count += nums[i]\n        count += num\n    }\n}\n
        array.swift
        /* \u904d\u5386\u6570\u7ec4 */\nfunc traverse(nums: [Int]) {\n    var count = 0\n    // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n    for i in nums.indices {\n        count += nums[i]\n    }\n    // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n    for num in nums {\n        count += num\n    }\n    // \u540c\u65f6\u904d\u5386\u6570\u636e\u7d22\u5f15\u548c\u5143\u7d20\n    for (i, num) in nums.enumerated() {\n        count += nums[i]\n        count += num\n    }\n}\n
        array.js
        /* \u904d\u5386\u6570\u7ec4 */\nfunction traverse(nums) {\n    let count = 0;\n    // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n    for (let i = 0; i < nums.length; i++) {\n        count += nums[i];\n    }\n    // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n    for (const num of nums) {\n        count += num;\n    }\n}\n
        array.ts
        /* \u904d\u5386\u6570\u7ec4 */\nfunction traverse(nums: number[]): void {\n    let count = 0;\n    // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n    for (let i = 0; i < nums.length; i++) {\n        count += nums[i];\n    }\n    // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n    for (const num of nums) {\n        count += num;\n    }\n}\n
        array.dart
        /* \u904d\u5386\u6570\u7ec4\u5143\u7d20 */\nvoid traverse(List<int> nums) {\n  int count = 0;\n  // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n  for (var i = 0; i < nums.length; i++) {\n    count += nums[i];\n  }\n  // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n  for (int _num in nums) {\n    count += _num;\n  }\n  // \u901a\u8fc7 forEach \u65b9\u6cd5\u904d\u5386\u6570\u7ec4\n  nums.forEach((_num) {\n    count += _num;\n  });\n}\n
        array.rs
        /* \u904d\u5386\u6570\u7ec4 */\nfn traverse(nums: &[i32]) {\n    let mut _count = 0;\n    // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n    for i in 0..nums.len() {\n        _count += nums[i];\n    }\n    // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n    _count = 0;\n    for &num in nums {\n        _count += num;\n    }\n}\n
        array.c
        /* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int *nums, int size) {\n    int count = 0;\n    // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n    for (int i = 0; i < size; i++) {\n        count += nums[i];\n    }\n}\n
        array.kt
        /* \u904d\u5386\u6570\u7ec4 */\nfun traverse(nums: IntArray) {\n    var count = 0\n    // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n    for (i in nums.indices) {\n        count += nums[i]\n    }\n    // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n    for (j in nums) {\n        count += j\n    }\n}\n
        array.rb
        ### \u904d\u5386\u6570\u7ec4 ###\ndef traverse(nums)\n  count = 0\n\n  # \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n  for i in 0...nums.length\n    count += nums[i]\n  end\n\n  # \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n  for num in nums\n    count += num\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/array/#6","title":"6. \u00a0 \u67e5\u627e\u5143\u7d20","text":"

        \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\u9700\u8981\u904d\u5386\u6570\u7ec4\uff0c\u6bcf\u8f6e\u5224\u65ad\u5143\u7d20\u503c\u662f\u5426\u5339\u914d\uff0c\u82e5\u5339\u914d\u5219\u8f93\u51fa\u5bf9\u5e94\u7d22\u5f15\u3002

        \u56e0\u4e3a\u6570\u7ec4\u662f\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u6240\u4ee5\u4e0a\u8ff0\u67e5\u627e\u64cd\u4f5c\u88ab\u79f0\u4e3a\u201c\u7ebf\u6027\u67e5\u627e\u201d\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def find(nums: list[int], target: int) -> int:\n    \"\"\"\u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\"\"\"\n    for i in range(len(nums)):\n        if nums[i] == target:\n            return i\n    return -1\n
        array.cpp
        /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int *nums, int size, int target) {\n    for (int i = 0; i < size; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
        array.java
        /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int[] nums, int target) {\n    for (int i = 0; i < nums.length; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
        array.cs
        /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint Find(int[] nums, int target) {\n    for (int i = 0; i < nums.Length; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
        array.go
        /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunc find(nums []int, target int) (index int) {\n    index = -1\n    for i := 0; i < len(nums); i++ {\n        if nums[i] == target {\n            index = i\n            break\n        }\n    }\n    return\n}\n
        array.swift
        /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunc find(nums: [Int], target: Int) -> Int {\n    for i in nums.indices {\n        if nums[i] == target {\n            return i\n        }\n    }\n    return -1\n}\n
        array.js
        /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunction find(nums, target) {\n    for (let i = 0; i < nums.length; i++) {\n        if (nums[i] === target) return i;\n    }\n    return -1;\n}\n
        array.ts
        /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunction find(nums: number[], target: number): number {\n    for (let i = 0; i < nums.length; i++) {\n        if (nums[i] === target) {\n            return i;\n        }\n    }\n    return -1;\n}\n
        array.dart
        /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(List<int> nums, int target) {\n  for (var i = 0; i < nums.length; i++) {\n    if (nums[i] == target) return i;\n  }\n  return -1;\n}\n
        array.rs
        /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfn find(nums: &[i32], target: i32) -> Option<usize> {\n    for i in 0..nums.len() {\n        if nums[i] == target {\n            return Some(i);\n        }\n    }\n    None\n}\n
        array.c
        /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int *nums, int size, int target) {\n    for (int i = 0; i < size; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
        array.kt
        /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfun find(nums: IntArray, target: Int): Int {\n    for (i in nums.indices) {\n        if (nums[i] == target)\n            return i\n    }\n    return -1\n}\n
        array.rb
        ### \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 ###\ndef find(nums, target)\n  for i in 0...nums.length\n    return i if nums[i] == target\n  end\n\n  -1\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/array/#7","title":"7. \u00a0 \u6269\u5bb9\u6570\u7ec4","text":"

        \u5728\u590d\u6742\u7684\u7cfb\u7edf\u73af\u5883\u4e2d\uff0c\u7a0b\u5e8f\u96be\u4ee5\u4fdd\u8bc1\u6570\u7ec4\u4e4b\u540e\u7684\u5185\u5b58\u7a7a\u95f4\u662f\u53ef\u7528\u7684\uff0c\u4ece\u800c\u65e0\u6cd5\u5b89\u5168\u5730\u6269\u5c55\u6570\u7ec4\u5bb9\u91cf\u3002\u56e0\u6b64\u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u4e0d\u53ef\u53d8\u7684\u3002

        \u5982\u679c\u6211\u4eec\u5e0c\u671b\u6269\u5bb9\u6570\u7ec4\uff0c\u5219\u9700\u91cd\u65b0\u5efa\u7acb\u4e00\u4e2a\u66f4\u5927\u7684\u6570\u7ec4\uff0c\u7136\u540e\u628a\u539f\u6570\u7ec4\u5143\u7d20\u4f9d\u6b21\u590d\u5236\u5230\u65b0\u6570\u7ec4\u3002\u8fd9\u662f\u4e00\u4e2a \\(O(n)\\) \u7684\u64cd\u4f5c\uff0c\u5728\u6570\u7ec4\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\u975e\u5e38\u8017\u65f6\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def extend(nums: list[int], enlarge: int) -> list[int]:\n    \"\"\"\u6269\u5c55\u6570\u7ec4\u957f\u5ea6\"\"\"\n    # \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n    res = [0] * (len(nums) + enlarge)\n    # \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    for i in range(len(nums)):\n        res[i] = nums[i]\n    # \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n    return res\n
        array.cpp
        /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint *extend(int *nums, int size, int enlarge) {\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n    int *res = new int[size + enlarge];\n    // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    for (int i = 0; i < size; i++) {\n        res[i] = nums[i];\n    }\n    // \u91ca\u653e\u5185\u5b58\n    delete[] nums;\n    // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n    return res;\n}\n
        array.java
        /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint[] extend(int[] nums, int enlarge) {\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n    int[] res = new int[nums.length + enlarge];\n    // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    for (int i = 0; i < nums.length; i++) {\n        res[i] = nums[i];\n    }\n    // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n    return res;\n}\n
        array.cs
        /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint[] Extend(int[] nums, int enlarge) {\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n    int[] res = new int[nums.Length + enlarge];\n    // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    for (int i = 0; i < nums.Length; i++) {\n        res[i] = nums[i];\n    }\n    // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n    return res;\n}\n
        array.go
        /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfunc extend(nums []int, enlarge int) []int {\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n    res := make([]int, len(nums)+enlarge)\n    // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    for i, num := range nums {\n        res[i] = num\n    }\n    // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n    return res\n}\n
        array.swift
        /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfunc extend(nums: [Int], enlarge: Int) -> [Int] {\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n    var res = Array(repeating: 0, count: nums.count + enlarge)\n    // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    for i in nums.indices {\n        res[i] = nums[i]\n    }\n    // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n    return res\n}\n
        array.js
        /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\n// \u8bf7\u6ce8\u610f\uff0cJavaScript \u7684 Array \u662f\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u6269\u5c55\n// \u4e3a\u4e86\u65b9\u4fbf\u5b66\u4e60\uff0c\u672c\u51fd\u6570\u5c06 Array \u770b\u4f5c\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6570\u7ec4\nfunction extend(nums, enlarge) {\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n    const res = new Array(nums.length + enlarge).fill(0);\n    // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    for (let i = 0; i < nums.length; i++) {\n        res[i] = nums[i];\n    }\n    // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n    return res;\n}\n
        array.ts
        /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\n// \u8bf7\u6ce8\u610f\uff0cTypeScript \u7684 Array \u662f\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u6269\u5c55\n// \u4e3a\u4e86\u65b9\u4fbf\u5b66\u4e60\uff0c\u672c\u51fd\u6570\u5c06 Array \u770b\u4f5c\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6570\u7ec4\nfunction extend(nums: number[], enlarge: number): number[] {\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n    const res = new Array(nums.length + enlarge).fill(0);\n    // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    for (let i = 0; i < nums.length; i++) {\n        res[i] = nums[i];\n    }\n    // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n    return res;\n}\n
        array.dart
        /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nList<int> extend(List<int> nums, int enlarge) {\n  // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n  List<int> res = List.filled(nums.length + enlarge, 0);\n  // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n  for (var i = 0; i < nums.length; i++) {\n    res[i] = nums[i];\n  }\n  // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n  return res;\n}\n
        array.rs
        /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfn extend(nums: &[i32], enlarge: usize) -> Vec<i32> {\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n    let mut res: Vec<i32> = vec![0; nums.len() + enlarge];\n    // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\n    res[0..nums.len()].copy_from_slice(nums);\n\n    // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n    res\n}\n
        array.c
        /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint *extend(int *nums, int size, int enlarge) {\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n    int *res = (int *)malloc(sizeof(int) * (size + enlarge));\n    // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    for (int i = 0; i < size; i++) {\n        res[i] = nums[i];\n    }\n    // \u521d\u59cb\u5316\u6269\u5c55\u540e\u7684\u7a7a\u95f4\n    for (int i = size; i < size + enlarge; i++) {\n        res[i] = 0;\n    }\n    // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n    return res;\n}\n
        array.kt
        /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfun extend(nums: IntArray, enlarge: Int): IntArray {\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n    val res = IntArray(nums.size + enlarge)\n    // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    for (i in nums.indices) {\n        res[i] = nums[i]\n    }\n    // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n    return res\n}\n
        array.rb
        ### \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 ###\n# \u8bf7\u6ce8\u610f\uff0cRuby \u7684 Array \u662f\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u6269\u5c55\n# \u4e3a\u4e86\u65b9\u4fbf\u5b66\u4e60\uff0c\u672c\u51fd\u6570\u5c06 Array \u770b\u4f5c\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6570\u7ec4\ndef extend(nums, enlarge)\n  # \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n  res = Array.new(nums.length + enlarge, 0)\n\n  # \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n  for i in 0...nums.length\n    res[i] = nums[i]\n  end\n\n  # \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/array/#412","title":"4.1.2 \u00a0 \u6570\u7ec4\u7684\u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"

        \u6570\u7ec4\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u5185\uff0c\u4e14\u5143\u7d20\u7c7b\u578b\u76f8\u540c\u3002\u8fd9\u79cd\u505a\u6cd5\u5305\u542b\u4e30\u5bcc\u7684\u5148\u9a8c\u4fe1\u606f\uff0c\u7cfb\u7edf\u53ef\u4ee5\u5229\u7528\u8fd9\u4e9b\u4fe1\u606f\u6765\u4f18\u5316\u6570\u636e\u7ed3\u6784\u7684\u64cd\u4f5c\u6548\u7387\u3002

        • \u7a7a\u95f4\u6548\u7387\u9ad8\uff1a\u6570\u7ec4\u4e3a\u6570\u636e\u5206\u914d\u4e86\u8fde\u7eed\u7684\u5185\u5b58\u5757\uff0c\u65e0\u987b\u989d\u5916\u7684\u7ed3\u6784\u5f00\u9500\u3002
        • \u652f\u6301\u968f\u673a\u8bbf\u95ee\uff1a\u6570\u7ec4\u5141\u8bb8\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u8bbf\u95ee\u4efb\u4f55\u5143\u7d20\u3002
        • \u7f13\u5b58\u5c40\u90e8\u6027\uff1a\u5f53\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u65f6\uff0c\u8ba1\u7b97\u673a\u4e0d\u4ec5\u4f1a\u52a0\u8f7d\u5b83\uff0c\u8fd8\u4f1a\u7f13\u5b58\u5176\u5468\u56f4\u7684\u5176\u4ed6\u6570\u636e\uff0c\u4ece\u800c\u501f\u52a9\u9ad8\u901f\u7f13\u5b58\u6765\u63d0\u5347\u540e\u7eed\u64cd\u4f5c\u7684\u6267\u884c\u901f\u5ea6\u3002

        \u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u662f\u4e00\u628a\u53cc\u5203\u5251\uff0c\u5176\u5b58\u5728\u4ee5\u4e0b\u5c40\u9650\u6027\u3002

        • \u63d2\u5165\u4e0e\u5220\u9664\u6548\u7387\u4f4e\uff1a\u5f53\u6570\u7ec4\u4e2d\u5143\u7d20\u8f83\u591a\u65f6\uff0c\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u9700\u8981\u79fb\u52a8\u5927\u91cf\u7684\u5143\u7d20\u3002
        • \u957f\u5ea6\u4e0d\u53ef\u53d8\uff1a\u6570\u7ec4\u5728\u521d\u59cb\u5316\u540e\u957f\u5ea6\u5c31\u56fa\u5b9a\u4e86\uff0c\u6269\u5bb9\u6570\u7ec4\u9700\u8981\u5c06\u6240\u6709\u6570\u636e\u590d\u5236\u5230\u65b0\u6570\u7ec4\uff0c\u5f00\u9500\u5f88\u5927\u3002
        • \u7a7a\u95f4\u6d6a\u8d39\uff1a\u5982\u679c\u6570\u7ec4\u5206\u914d\u7684\u5927\u5c0f\u8d85\u8fc7\u5b9e\u9645\u6240\u9700\uff0c\u90a3\u4e48\u591a\u4f59\u7684\u7a7a\u95f4\u5c31\u88ab\u6d6a\u8d39\u4e86\u3002
        "},{"location":"chapter_array_and_linkedlist/array/#413","title":"4.1.3 \u00a0 \u6570\u7ec4\u5178\u578b\u5e94\u7528","text":"

        \u6570\u7ec4\u662f\u4e00\u79cd\u57fa\u7840\u4e14\u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\uff0c\u65e2\u9891\u7e41\u5e94\u7528\u5728\u5404\u7c7b\u7b97\u6cd5\u4e4b\u4e2d\uff0c\u4e5f\u53ef\u7528\u4e8e\u5b9e\u73b0\u5404\u79cd\u590d\u6742\u6570\u636e\u7ed3\u6784\u3002

        • \u968f\u673a\u8bbf\u95ee\uff1a\u5982\u679c\u6211\u4eec\u60f3\u968f\u673a\u62bd\u53d6\u4e00\u4e9b\u6837\u672c\uff0c\u90a3\u4e48\u53ef\u4ee5\u7528\u6570\u7ec4\u5b58\u50a8\uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u968f\u673a\u5e8f\u5217\uff0c\u6839\u636e\u7d22\u5f15\u5b9e\u73b0\u968f\u673a\u62bd\u6837\u3002
        • \u6392\u5e8f\u548c\u641c\u7d22\uff1a\u6570\u7ec4\u662f\u6392\u5e8f\u548c\u641c\u7d22\u7b97\u6cd5\u6700\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u3001\u4e8c\u5206\u67e5\u627e\u7b49\u90fd\u4e3b\u8981\u5728\u6570\u7ec4\u4e0a\u8fdb\u884c\u3002
        • \u67e5\u627e\u8868\uff1a\u5f53\u9700\u8981\u5feb\u901f\u67e5\u627e\u4e00\u4e2a\u5143\u7d20\u6216\u5176\u5bf9\u5e94\u5173\u7cfb\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u4f5c\u4e3a\u67e5\u627e\u8868\u3002\u5047\u5982\u6211\u4eec\u60f3\u5b9e\u73b0\u5b57\u7b26\u5230 ASCII \u7801\u7684\u6620\u5c04\uff0c\u5219\u53ef\u4ee5\u5c06\u5b57\u7b26\u7684 ASCII \u7801\u503c\u4f5c\u4e3a\u7d22\u5f15\uff0c\u5bf9\u5e94\u7684\u5143\u7d20\u5b58\u653e\u5728\u6570\u7ec4\u4e2d\u7684\u5bf9\u5e94\u4f4d\u7f6e\u3002
        • \u673a\u5668\u5b66\u4e60\uff1a\u795e\u7ecf\u7f51\u7edc\u4e2d\u5927\u91cf\u4f7f\u7528\u4e86\u5411\u91cf\u3001\u77e9\u9635\u3001\u5f20\u91cf\u4e4b\u95f4\u7684\u7ebf\u6027\u4ee3\u6570\u8fd0\u7b97\uff0c\u8fd9\u4e9b\u6570\u636e\u90fd\u662f\u4ee5\u6570\u7ec4\u7684\u5f62\u5f0f\u6784\u5efa\u7684\u3002\u6570\u7ec4\u662f\u795e\u7ecf\u7f51\u7edc\u7f16\u7a0b\u4e2d\u6700\u5e38\u4f7f\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002
        • \u6570\u636e\u7ed3\u6784\u5b9e\u73b0\uff1a\u6570\u7ec4\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u5806\u3001\u56fe\u7b49\u6570\u636e\u7ed3\u6784\u3002\u4f8b\u5982\uff0c\u56fe\u7684\u90bb\u63a5\u77e9\u9635\u8868\u793a\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u3002
        "},{"location":"chapter_array_and_linkedlist/linked_list/","title":"4.2 \u00a0 \u94fe\u8868","text":"

        \u5185\u5b58\u7a7a\u95f4\u662f\u6240\u6709\u7a0b\u5e8f\u7684\u516c\u5171\u8d44\u6e90\uff0c\u5728\u4e00\u4e2a\u590d\u6742\u7684\u7cfb\u7edf\u8fd0\u884c\u73af\u5883\u4e0b\uff0c\u7a7a\u95f2\u7684\u5185\u5b58\u7a7a\u95f4\u53ef\u80fd\u6563\u843d\u5728\u5185\u5b58\u5404\u5904\u3002\u6211\u4eec\u77e5\u9053\uff0c\u5b58\u50a8\u6570\u7ec4\u7684\u5185\u5b58\u7a7a\u95f4\u5fc5\u987b\u662f\u8fde\u7eed\u7684\uff0c\u800c\u5f53\u6570\u7ec4\u975e\u5e38\u5927\u65f6\uff0c\u5185\u5b58\u53ef\u80fd\u65e0\u6cd5\u63d0\u4f9b\u5982\u6b64\u5927\u7684\u8fde\u7eed\u7a7a\u95f4\u3002\u6b64\u65f6\u94fe\u8868\u7684\u7075\u6d3b\u6027\u4f18\u52bf\u5c31\u4f53\u73b0\u51fa\u6765\u4e86\u3002

        \u94fe\u8868\uff08linked list\uff09\u662f\u4e00\u79cd\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u8282\u70b9\u5bf9\u8c61\uff0c\u5404\u4e2a\u8282\u70b9\u901a\u8fc7\u201c\u5f15\u7528\u201d\u76f8\u8fde\u63a5\u3002\u5f15\u7528\u8bb0\u5f55\u4e86\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u5185\u5b58\u5730\u5740\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u4ece\u5f53\u524d\u8282\u70b9\u8bbf\u95ee\u5230\u4e0b\u4e00\u4e2a\u8282\u70b9\u3002

        \u94fe\u8868\u7684\u8bbe\u8ba1\u4f7f\u5f97\u5404\u4e2a\u8282\u70b9\u53ef\u4ee5\u5206\u6563\u5b58\u50a8\u5728\u5185\u5b58\u5404\u5904\uff0c\u5b83\u4eec\u7684\u5185\u5b58\u5730\u5740\u65e0\u987b\u8fde\u7eed\u3002

        \u56fe 4-5 \u00a0 \u94fe\u8868\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f

        \u89c2\u5bdf\u56fe 4-5 \uff0c\u94fe\u8868\u7684\u7ec4\u6210\u5355\u4f4d\u662f\u8282\u70b9\uff08node\uff09\u5bf9\u8c61\u3002\u6bcf\u4e2a\u8282\u70b9\u90fd\u5305\u542b\u4e24\u9879\u6570\u636e\uff1a\u8282\u70b9\u7684\u201c\u503c\u201d\u548c\u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u201c\u5f15\u7528\u201d\u3002

        • \u94fe\u8868\u7684\u9996\u4e2a\u8282\u70b9\u88ab\u79f0\u4e3a\u201c\u5934\u8282\u70b9\u201d\uff0c\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u88ab\u79f0\u4e3a\u201c\u5c3e\u8282\u70b9\u201d\u3002
        • \u5c3e\u8282\u70b9\u6307\u5411\u7684\u662f\u201c\u7a7a\u201d\uff0c\u5b83\u5728 Java\u3001C++ \u548c Python \u4e2d\u5206\u522b\u88ab\u8bb0\u4e3a null\u3001nullptr \u548c None \u3002
        • \u5728 C\u3001C++\u3001Go \u548c Rust \u7b49\u652f\u6301\u6307\u9488\u7684\u8bed\u8a00\u4e2d\uff0c\u4e0a\u8ff0\u201c\u5f15\u7528\u201d\u5e94\u88ab\u66ff\u6362\u4e3a\u201c\u6307\u9488\u201d\u3002

        \u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u94fe\u8868\u8282\u70b9 ListNode \u9664\u4e86\u5305\u542b\u503c\uff0c\u8fd8\u9700\u989d\u5916\u4fdd\u5b58\u4e00\u4e2a\u5f15\u7528\uff08\u6307\u9488\uff09\u3002\u56e0\u6b64\u5728\u76f8\u540c\u6570\u636e\u91cf\u4e0b\uff0c\u94fe\u8868\u6bd4\u6570\u7ec4\u5360\u7528\u66f4\u591a\u7684\u5185\u5b58\u7a7a\u95f4\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        class ListNode:\n    \"\"\"\u94fe\u8868\u8282\u70b9\u7c7b\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val               # \u8282\u70b9\u503c\n        self.next: ListNode | None = None # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n
        /* \u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\n    int val;         // \u8282\u70b9\u503c\n    ListNode *next;  // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n    ListNode(int x) : val(x), next(nullptr) {}  // \u6784\u9020\u51fd\u6570\n};\n
        /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n    int val;        // \u8282\u70b9\u503c\n    ListNode next;  // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n    ListNode(int x) { val = x; }  // \u6784\u9020\u51fd\u6570\n}\n
        /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode(int x) {  //\u6784\u9020\u51fd\u6570\n    int val = x;         // \u8282\u70b9\u503c\n    ListNode? next;      // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n}\n
        /* \u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype ListNode struct {\n    Val  int       // \u8282\u70b9\u503c\n    Next *ListNode // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n}\n\n// NewListNode \u6784\u9020\u51fd\u6570\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u94fe\u8868\nfunc NewListNode(val int) *ListNode {\n    return &ListNode{\n        Val:  val,\n        Next: nil,\n    }\n}\n
        /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n    var val: Int // \u8282\u70b9\u503c\n    var next: ListNode? // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n\n    init(x: Int) { // \u6784\u9020\u51fd\u6570\n        val = x\n    }\n}\n
        /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n    constructor(val, next) {\n        this.val = (val === undefined ? 0 : val);       // \u8282\u70b9\u503c\n        this.next = (next === undefined ? null : next); // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n    }\n}\n
        /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n    val: number;\n    next: ListNode | null;\n    constructor(val?: number, next?: ListNode | null) {\n        this.val = val === undefined ? 0 : val;        // \u8282\u70b9\u503c\n        this.next = next === undefined ? null : next;  // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n    }\n}\n
        /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n  int val; // \u8282\u70b9\u503c\n  ListNode? next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n  ListNode(this.val, [this.next]); // \u6784\u9020\u51fd\u6570\n}\n
        use std::rc::Rc;\nuse std::cell::RefCell;\n/* \u94fe\u8868\u8282\u70b9\u7c7b */\n#[derive(Debug)]\nstruct ListNode {\n    val: i32, // \u8282\u70b9\u503c\n    next: Option<Rc<RefCell<ListNode>>>, // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n}\n
        /* \u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\ntypedef struct ListNode {\n    int val;               // \u8282\u70b9\u503c\n    struct ListNode *next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n} ListNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nListNode *newListNode(int val) {\n    ListNode *node;\n    node = (ListNode *) malloc(sizeof(ListNode));\n    node->val = val;\n    node->next = NULL;\n    return node;\n}\n
        /* \u94fe\u8868\u8282\u70b9\u7c7b */\n// \u6784\u9020\u65b9\u6cd5\nclass ListNode(x: Int) {\n    val _val: Int = x          // \u8282\u70b9\u503c\n    val next: ListNode? = null // \u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u5f15\u7528\n}\n
        # \u94fe\u8868\u8282\u70b9\u7c7b\nclass ListNode\n  attr_accessor :val  # \u8282\u70b9\u503c\n  attr_accessor :next # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n\n  def initialize(val=0, next_node=nil)\n    @val = val\n    @next = next_node\n  end\nend\n
        "},{"location":"chapter_array_and_linkedlist/linked_list/#421","title":"4.2.1 \u00a0 \u94fe\u8868\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/linked_list/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u94fe\u8868","text":"

        \u5efa\u7acb\u94fe\u8868\u5206\u4e3a\u4e24\u6b65\uff0c\u7b2c\u4e00\u6b65\u662f\u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\u5bf9\u8c61\uff0c\u7b2c\u4e8c\u6b65\u662f\u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\u5173\u7cfb\u3002\u521d\u59cb\u5316\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4ece\u94fe\u8868\u7684\u5934\u8282\u70b9\u51fa\u53d1\uff0c\u901a\u8fc7\u5f15\u7528\u6307\u5411 next \u4f9d\u6b21\u8bbf\u95ee\u6240\u6709\u8282\u70b9\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
        # \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4\n# \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nn0 = ListNode(1)\nn1 = ListNode(3)\nn2 = ListNode(2)\nn3 = ListNode(5)\nn4 = ListNode(4)\n# \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
        linked_list.cpp
        /* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode* n0 = new ListNode(1);\nListNode* n1 = new ListNode(3);\nListNode* n2 = new ListNode(2);\nListNode* n3 = new ListNode(5);\nListNode* n4 = new ListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
        linked_list.java
        /* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode n0 = new ListNode(1);\nListNode n1 = new ListNode(3);\nListNode n2 = new ListNode(2);\nListNode n3 = new ListNode(5);\nListNode n4 = new ListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
        linked_list.cs
        /* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode n0 = new(1);\nListNode n1 = new(3);\nListNode n2 = new(2);\nListNode n3 = new(5);\nListNode n4 = new(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
        linked_list.go
        /* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nn0 := NewListNode(1)\nn1 := NewListNode(3)\nn2 := NewListNode(2)\nn3 := NewListNode(5)\nn4 := NewListNode(4)\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.Next = n1\nn1.Next = n2\nn2.Next = n3\nn3.Next = n4\n
        linked_list.swift
        /* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nlet n0 = ListNode(x: 1)\nlet n1 = ListNode(x: 3)\nlet n2 = ListNode(x: 2)\nlet n3 = ListNode(x: 5)\nlet n4 = ListNode(x: 4)\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
        linked_list.js
        /* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
        linked_list.ts
        /* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
        linked_list.dart
        /* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\\\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode n0 = ListNode(1);\nListNode n1 = ListNode(3);\nListNode n2 = ListNode(2);\nListNode n3 = ListNode(5);\nListNode n4 = ListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
        linked_list.rs
        /* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nlet n0 = Rc::new(RefCell::new(ListNode { val: 1, next: None }));\nlet n1 = Rc::new(RefCell::new(ListNode { val: 3, next: None }));\nlet n2 = Rc::new(RefCell::new(ListNode { val: 2, next: None }));\nlet n3 = Rc::new(RefCell::new(ListNode { val: 5, next: None }));\nlet n4 = Rc::new(RefCell::new(ListNode { val: 4, next: None }));\n\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.borrow_mut().next = Some(n1.clone());\nn1.borrow_mut().next = Some(n2.clone());\nn2.borrow_mut().next = Some(n3.clone());\nn3.borrow_mut().next = Some(n4.clone());\n
        linked_list.c
        /* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode* n0 = newListNode(1);\nListNode* n1 = newListNode(3);\nListNode* n2 = newListNode(2);\nListNode* n3 = newListNode(5);\nListNode* n4 = newListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
        linked_list.kt
        /* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nval n0 = ListNode(1)\nval n1 = ListNode(3)\nval n2 = ListNode(2)\nval n3 = ListNode(5)\nval n4 = ListNode(4)\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
        linked_list.rb
        # \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4\n# \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nn0 = ListNode.new(1)\nn1 = ListNode.new(3)\nn2 = ListNode.new(2)\nn3 = ListNode.new(5)\nn4 = ListNode.new(4)\n# \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u6570\u7ec4\u6574\u4f53\u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u6bd4\u5982\u6570\u7ec4 nums \u5305\u542b\u5143\u7d20 nums[0] \u548c nums[1] \u7b49\uff0c\u800c\u94fe\u8868\u662f\u7531\u591a\u4e2a\u72ec\u7acb\u7684\u8282\u70b9\u5bf9\u8c61\u7ec4\u6210\u7684\u3002\u6211\u4eec\u901a\u5e38\u5c06\u5934\u8282\u70b9\u5f53\u4f5c\u94fe\u8868\u7684\u4ee3\u79f0\uff0c\u6bd4\u5982\u4ee5\u4e0a\u4ee3\u7801\u4e2d\u7684\u94fe\u8868\u53ef\u8bb0\u4f5c\u94fe\u8868 n0 \u3002

        "},{"location":"chapter_array_and_linkedlist/linked_list/#2","title":"2. \u00a0 \u63d2\u5165\u8282\u70b9","text":"

        \u5728\u94fe\u8868\u4e2d\u63d2\u5165\u8282\u70b9\u975e\u5e38\u5bb9\u6613\u3002\u5982\u56fe 4-6 \u6240\u793a\uff0c\u5047\u8bbe\u6211\u4eec\u60f3\u5728\u76f8\u90bb\u7684\u4e24\u4e2a\u8282\u70b9 n0 \u548c n1 \u4e4b\u95f4\u63d2\u5165\u4e00\u4e2a\u65b0\u8282\u70b9 P \uff0c\u5219\u53ea\u9700\u6539\u53d8\u4e24\u4e2a\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\u5373\u53ef\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002

        \u76f8\u6bd4\u4e4b\u4e0b\uff0c\u5728\u6570\u7ec4\u4e2d\u63d2\u5165\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5728\u5927\u6570\u636e\u91cf\u4e0b\u7684\u6548\u7387\u8f83\u4f4e\u3002

        \u56fe 4-6 \u00a0 \u94fe\u8868\u63d2\u5165\u8282\u70b9\u793a\u4f8b

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
        def insert(n0: ListNode, P: ListNode):\n    \"\"\"\u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P\"\"\"\n    n1 = n0.next\n    P.next = n1\n    n0.next = P\n
        linked_list.cpp
        /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode *n0, ListNode *P) {\n    ListNode *n1 = n0->next;\n    P->next = n1;\n    n0->next = P;\n}\n
        linked_list.java
        /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode n0, ListNode P) {\n    ListNode n1 = n0.next;\n    P.next = n1;\n    n0.next = P;\n}\n
        linked_list.cs
        /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid Insert(ListNode n0, ListNode P) {\n    ListNode? n1 = n0.next;\n    P.next = n1;\n    n0.next = P;\n}\n
        linked_list.go
        /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunc insertNode(n0 *ListNode, P *ListNode) {\n    n1 := n0.Next\n    P.Next = n1\n    n0.Next = P\n}\n
        linked_list.swift
        /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunc insert(n0: ListNode, P: ListNode) {\n    let n1 = n0.next\n    P.next = n1\n    n0.next = P\n}\n
        linked_list.js
        /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunction insert(n0, P) {\n    const n1 = n0.next;\n    P.next = n1;\n    n0.next = P;\n}\n
        linked_list.ts
        /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunction insert(n0: ListNode, P: ListNode): void {\n    const n1 = n0.next;\n    P.next = n1;\n    n0.next = P;\n}\n
        linked_list.dart
        /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode n0, ListNode P) {\n  ListNode? n1 = n0.next;\n  P.next = n1;\n  n0.next = P;\n}\n
        linked_list.rs
        /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\n#[allow(non_snake_case)]\npub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) {\n    let n1 = n0.borrow_mut().next.take();\n    P.borrow_mut().next = n1;\n    n0.borrow_mut().next = Some(P);\n}\n
        linked_list.c
        /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode *n0, ListNode *P) {\n    ListNode *n1 = n0->next;\n    P->next = n1;\n    n0->next = P;\n}\n
        linked_list.kt
        /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfun insert(n0: ListNode?, p: ListNode?) {\n    val n1 = n0?.next\n    p?.next = n1\n    n0?.next = p\n}\n
        linked_list.rb
        ### \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 _p ###\n# Ruby \u7684 `p` \u662f\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c `P` \u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528 `_p` \u4ee3\u66ff\ndef insert(n0, _p)\n  n1 = n0.next\n  _p.next = n1\n  n0.next = _p\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/linked_list/#3","title":"3. \u00a0 \u5220\u9664\u8282\u70b9","text":"

        \u5982\u56fe 4-7 \u6240\u793a\uff0c\u5728\u94fe\u8868\u4e2d\u5220\u9664\u8282\u70b9\u4e5f\u975e\u5e38\u65b9\u4fbf\uff0c\u53ea\u9700\u6539\u53d8\u4e00\u4e2a\u8282\u70b9\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\u5373\u53ef\u3002

        \u8bf7\u6ce8\u610f\uff0c\u5c3d\u7ba1\u5728\u5220\u9664\u64cd\u4f5c\u5b8c\u6210\u540e\u8282\u70b9 P \u4ecd\u7136\u6307\u5411 n1 \uff0c\u4f46\u5b9e\u9645\u4e0a\u904d\u5386\u6b64\u94fe\u8868\u5df2\u7ecf\u65e0\u6cd5\u8bbf\u95ee\u5230 P \uff0c\u8fd9\u610f\u5473\u7740 P \u5df2\u7ecf\u4e0d\u518d\u5c5e\u4e8e\u8be5\u94fe\u8868\u4e86\u3002

        \u56fe 4-7 \u00a0 \u94fe\u8868\u5220\u9664\u8282\u70b9

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
        def remove(n0: ListNode):\n    \"\"\"\u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9\"\"\"\n    if not n0.next:\n        return\n    # n0 -> P -> n1\n    P = n0.next\n    n1 = P.next\n    n0.next = n1\n
        linked_list.cpp
        /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode *n0) {\n    if (n0->next == nullptr)\n        return;\n    // n0 -> P -> n1\n    ListNode *P = n0->next;\n    ListNode *n1 = P->next;\n    n0->next = n1;\n    // \u91ca\u653e\u5185\u5b58\n    delete P;\n}\n
        linked_list.java
        /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode n0) {\n    if (n0.next == null)\n        return;\n    // n0 -> P -> n1\n    ListNode P = n0.next;\n    ListNode n1 = P.next;\n    n0.next = n1;\n}\n
        linked_list.cs
        /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid Remove(ListNode n0) {\n    if (n0.next == null)\n        return;\n    // n0 -> P -> n1\n    ListNode P = n0.next;\n    ListNode? n1 = P.next;\n    n0.next = n1;\n}\n
        linked_list.go
        /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunc removeItem(n0 *ListNode) {\n    if n0.Next == nil {\n        return\n    }\n    // n0 -> P -> n1\n    P := n0.Next\n    n1 := P.Next\n    n0.Next = n1\n}\n
        linked_list.swift
        /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunc remove(n0: ListNode) {\n    if n0.next == nil {\n        return\n    }\n    // n0 -> P -> n1\n    let P = n0.next\n    let n1 = P?.next\n    n0.next = n1\n}\n
        linked_list.js
        /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunction remove(n0) {\n    if (!n0.next) return;\n    // n0 -> P -> n1\n    const P = n0.next;\n    const n1 = P.next;\n    n0.next = n1;\n}\n
        linked_list.ts
        /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunction remove(n0: ListNode): void {\n    if (!n0.next) {\n        return;\n    }\n    // n0 -> P -> n1\n    const P = n0.next;\n    const n1 = P.next;\n    n0.next = n1;\n}\n
        linked_list.dart
        /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode n0) {\n  if (n0.next == null) return;\n  // n0 -> P -> n1\n  ListNode P = n0.next!;\n  ListNode? n1 = P.next;\n  n0.next = n1;\n}\n
        linked_list.rs
        /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\n#[allow(non_snake_case)]\npub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {\n    // n0 -> P -> n1\n    let P = n0.borrow_mut().next.take();\n    if let Some(node) = P {\n        let n1 = node.borrow_mut().next.take();\n        n0.borrow_mut().next = n1;\n    }\n}\n
        linked_list.c
        /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nvoid removeItem(ListNode *n0) {\n    if (!n0->next)\n        return;\n    // n0 -> P -> n1\n    ListNode *P = n0->next;\n    ListNode *n1 = P->next;\n    n0->next = n1;\n    // \u91ca\u653e\u5185\u5b58\n    free(P);\n}\n
        linked_list.kt
        /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfun remove(n0: ListNode?) {\n    if (n0?.next == null)\n        return\n    // n0 -> P -> n1\n    val p = n0.next\n    val n1 = p?.next\n    n0.next = n1\n}\n
        linked_list.rb
        ### \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 ###\ndef remove(n0)\n  return if n0.next.nil?\n\n  # n0 -> remove_node -> n1\n  remove_node = n0.next\n  n1 = remove_node.next\n  n0.next = n1\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/linked_list/#4","title":"4. \u00a0 \u8bbf\u95ee\u8282\u70b9","text":"

        \u5728\u94fe\u8868\u4e2d\u8bbf\u95ee\u8282\u70b9\u7684\u6548\u7387\u8f83\u4f4e\u3002\u5982\u4e0a\u4e00\u8282\u6240\u8ff0\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u4e0b\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u5143\u7d20\u3002\u94fe\u8868\u5219\u4e0d\u7136\uff0c\u7a0b\u5e8f\u9700\u8981\u4ece\u5934\u8282\u70b9\u51fa\u53d1\uff0c\u9010\u4e2a\u5411\u540e\u904d\u5386\uff0c\u76f4\u81f3\u627e\u5230\u76ee\u6807\u8282\u70b9\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8bbf\u95ee\u94fe\u8868\u7684\u7b2c \\(i\\) \u4e2a\u8282\u70b9\u9700\u8981\u5faa\u73af \\(i - 1\\) \u8f6e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
        def access(head: ListNode, index: int) -> ListNode | None:\n    \"\"\"\u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9\"\"\"\n    for _ in range(index):\n        if not head:\n            return None\n        head = head.next\n    return head\n
        linked_list.cpp
        /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode *access(ListNode *head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == nullptr)\n            return nullptr;\n        head = head->next;\n    }\n    return head;\n}\n
        linked_list.java
        /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode access(ListNode head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == null)\n            return null;\n        head = head.next;\n    }\n    return head;\n}\n
        linked_list.cs
        /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode? Access(ListNode? head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == null)\n            return null;\n        head = head.next;\n    }\n    return head;\n}\n
        linked_list.go
        /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunc access(head *ListNode, index int) *ListNode {\n    for i := 0; i < index; i++ {\n        if head == nil {\n            return nil\n        }\n        head = head.Next\n    }\n    return head\n}\n
        linked_list.swift
        /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunc access(head: ListNode, index: Int) -> ListNode? {\n    var head: ListNode? = head\n    for _ in 0 ..< index {\n        if head == nil {\n            return nil\n        }\n        head = head?.next\n    }\n    return head\n}\n
        linked_list.js
        /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunction access(head, index) {\n    for (let i = 0; i < index; i++) {\n        if (!head) {\n            return null;\n        }\n        head = head.next;\n    }\n    return head;\n}\n
        linked_list.ts
        /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunction access(head: ListNode | null, index: number): ListNode | null {\n    for (let i = 0; i < index; i++) {\n        if (!head) {\n            return null;\n        }\n        head = head.next;\n    }\n    return head;\n}\n
        linked_list.dart
        /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode? access(ListNode? head, int index) {\n  for (var i = 0; i < index; i++) {\n    if (head == null) return null;\n    head = head.next;\n  }\n  return head;\n}\n
        linked_list.rs
        /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\npub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Option<Rc<RefCell<ListNode<T>>>> {\n    fn dfs<T>(\n        head: Option<&Rc<RefCell<ListNode<T>>>>,\n        index: i32,\n    ) -> Option<Rc<RefCell<ListNode<T>>>> {\n        if index <= 0 {\n            return head.cloned();\n        }\n\n        if let Some(node) = head {\n            dfs(node.borrow().next.as_ref(), index - 1)\n        } else {\n            None\n        }\n    }\n\n    dfs(Some(head).as_ref(), index)\n}\n
        linked_list.c
        /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode *access(ListNode *head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == NULL)\n            return NULL;\n        head = head->next;\n    }\n    return head;\n}\n
        linked_list.kt
        /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfun access(head: ListNode?, index: Int): ListNode? {\n    var h = head\n    for (i in 0..<index) {\n        if (h == null)\n            return null\n        h = h.next\n    }\n    return h\n}\n
        linked_list.rb
        ### \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 ###\ndef access(head, index)\n  for i in 0...index\n    return nil if head.nil?\n    head = head.next\n  end\n\n  head\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/linked_list/#5","title":"5. \u00a0 \u67e5\u627e\u8282\u70b9","text":"

        \u904d\u5386\u94fe\u8868\uff0c\u67e5\u627e\u5176\u4e2d\u503c\u4e3a target \u7684\u8282\u70b9\uff0c\u8f93\u51fa\u8be5\u8282\u70b9\u5728\u94fe\u8868\u4e2d\u7684\u7d22\u5f15\u3002\u6b64\u8fc7\u7a0b\u4e5f\u5c5e\u4e8e\u7ebf\u6027\u67e5\u627e\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
        def find(head: ListNode, target: int) -> int:\n    \"\"\"\u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9\"\"\"\n    index = 0\n    while head:\n        if head.val == target:\n            return index\n        head = head.next\n        index += 1\n    return -1\n
        linked_list.cpp
        /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode *head, int target) {\n    int index = 0;\n    while (head != nullptr) {\n        if (head->val == target)\n            return index;\n        head = head->next;\n        index++;\n    }\n    return -1;\n}\n
        linked_list.java
        /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode head, int target) {\n    int index = 0;\n    while (head != null) {\n        if (head.val == target)\n            return index;\n        head = head.next;\n        index++;\n    }\n    return -1;\n}\n
        linked_list.cs
        /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint Find(ListNode? head, int target) {\n    int index = 0;\n    while (head != null) {\n        if (head.val == target)\n            return index;\n        head = head.next;\n        index++;\n    }\n    return -1;\n}\n
        linked_list.go
        /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunc findNode(head *ListNode, target int) int {\n    index := 0\n    for head != nil {\n        if head.Val == target {\n            return index\n        }\n        head = head.Next\n        index++\n    }\n    return -1\n}\n
        linked_list.swift
        /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunc find(head: ListNode, target: Int) -> Int {\n    var head: ListNode? = head\n    var index = 0\n    while head != nil {\n        if head?.val == target {\n            return index\n        }\n        head = head?.next\n        index += 1\n    }\n    return -1\n}\n
        linked_list.js
        /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunction find(head, target) {\n    let index = 0;\n    while (head !== null) {\n        if (head.val === target) {\n            return index;\n        }\n        head = head.next;\n        index += 1;\n    }\n    return -1;\n}\n
        linked_list.ts
        /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunction find(head: ListNode | null, target: number): number {\n    let index = 0;\n    while (head !== null) {\n        if (head.val === target) {\n            return index;\n        }\n        head = head.next;\n        index += 1;\n    }\n    return -1;\n}\n
        linked_list.dart
        /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode? head, int target) {\n  int index = 0;\n  while (head != null) {\n    if (head.val == target) {\n      return index;\n    }\n    head = head.next;\n    index++;\n  }\n  return -1;\n}\n
        linked_list.rs
        /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\npub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T) -> i32 {\n    fn find<T: PartialEq>(head: Option<&Rc<RefCell<ListNode<T>>>>, target: T, idx: i32) -> i32 {\n        if let Some(node) = head {\n            if node.borrow().val == target {\n                return idx;\n            }\n            return find(node.borrow().next.as_ref(), target, idx + 1);\n        } else {\n            -1\n        }\n    }\n\n    find(Some(head).as_ref(), target, 0)\n}\n
        linked_list.c
        /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode *head, int target) {\n    int index = 0;\n    while (head) {\n        if (head->val == target)\n            return index;\n        head = head->next;\n        index++;\n    }\n    return -1;\n}\n
        linked_list.kt
        /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfun find(head: ListNode?, target: Int): Int {\n    var index = 0\n    var h = head\n    while (h != null) {\n        if (h._val == target)\n            return index\n        h = h.next\n        index++\n    }\n    return -1\n}\n
        linked_list.rb
        ### \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 ###\ndef find(head, target)\n  index = 0\n  while head\n    return index if head.val == target\n    head = head.next\n    index += 1\n  end\n\n  -1\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/linked_list/#422-vs","title":"4.2.2 \u00a0 \u6570\u7ec4 vs. \u94fe\u8868","text":"

        \u8868 4-1 \u603b\u7ed3\u4e86\u6570\u7ec4\u548c\u94fe\u8868\u7684\u5404\u9879\u7279\u70b9\u5e76\u5bf9\u6bd4\u4e86\u64cd\u4f5c\u6548\u7387\u3002\u7531\u4e8e\u5b83\u4eec\u91c7\u7528\u4e24\u79cd\u76f8\u53cd\u7684\u5b58\u50a8\u7b56\u7565\uff0c\u56e0\u6b64\u5404\u79cd\u6027\u8d28\u548c\u64cd\u4f5c\u6548\u7387\u4e5f\u5448\u73b0\u5bf9\u7acb\u7684\u7279\u70b9\u3002

        \u8868 4-1 \u00a0 \u6570\u7ec4\u4e0e\u94fe\u8868\u7684\u6548\u7387\u5bf9\u6bd4

        \u6570\u7ec4 \u94fe\u8868 \u5b58\u50a8\u65b9\u5f0f \u8fde\u7eed\u5185\u5b58\u7a7a\u95f4 \u5206\u6563\u5185\u5b58\u7a7a\u95f4 \u5bb9\u91cf\u6269\u5c55 \u957f\u5ea6\u4e0d\u53ef\u53d8 \u53ef\u7075\u6d3b\u6269\u5c55 \u5185\u5b58\u6548\u7387 \u5143\u7d20\u5360\u7528\u5185\u5b58\u5c11\u3001\u4f46\u53ef\u80fd\u6d6a\u8d39\u7a7a\u95f4 \u5143\u7d20\u5360\u7528\u5185\u5b58\u591a \u8bbf\u95ee\u5143\u7d20 \\(O(1)\\) \\(O(n)\\) \u6dfb\u52a0\u5143\u7d20 \\(O(n)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(1)\\)"},{"location":"chapter_array_and_linkedlist/linked_list/#423","title":"4.2.3 \u00a0 \u5e38\u89c1\u94fe\u8868\u7c7b\u578b","text":"

        \u5982\u56fe 4-8 \u6240\u793a\uff0c\u5e38\u89c1\u7684\u94fe\u8868\u7c7b\u578b\u5305\u62ec\u4e09\u79cd\u3002

        • \u5355\u5411\u94fe\u8868\uff1a\u5373\u524d\u9762\u4ecb\u7ecd\u7684\u666e\u901a\u94fe\u8868\u3002\u5355\u5411\u94fe\u8868\u7684\u8282\u70b9\u5305\u542b\u503c\u548c\u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\u4e24\u9879\u6570\u636e\u3002\u6211\u4eec\u5c06\u9996\u4e2a\u8282\u70b9\u79f0\u4e3a\u5934\u8282\u70b9\uff0c\u5c06\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u79f0\u4e3a\u5c3e\u8282\u70b9\uff0c\u5c3e\u8282\u70b9\u6307\u5411\u7a7a None \u3002
        • \u73af\u5f62\u94fe\u8868\uff1a\u5982\u679c\u6211\u4eec\u4ee4\u5355\u5411\u94fe\u8868\u7684\u5c3e\u8282\u70b9\u6307\u5411\u5934\u8282\u70b9\uff08\u9996\u5c3e\u76f8\u63a5\uff09\uff0c\u5219\u5f97\u5230\u4e00\u4e2a\u73af\u5f62\u94fe\u8868\u3002\u5728\u73af\u5f62\u94fe\u8868\u4e2d\uff0c\u4efb\u610f\u8282\u70b9\u90fd\u53ef\u4ee5\u89c6\u4f5c\u5934\u8282\u70b9\u3002
        • \u53cc\u5411\u94fe\u8868\uff1a\u4e0e\u5355\u5411\u94fe\u8868\u76f8\u6bd4\uff0c\u53cc\u5411\u94fe\u8868\u8bb0\u5f55\u4e86\u4e24\u4e2a\u65b9\u5411\u7684\u5f15\u7528\u3002\u53cc\u5411\u94fe\u8868\u7684\u8282\u70b9\u5b9a\u4e49\u540c\u65f6\u5305\u542b\u6307\u5411\u540e\u7ee7\u8282\u70b9\uff08\u4e0b\u4e00\u4e2a\u8282\u70b9\uff09\u548c\u524d\u9a71\u8282\u70b9\uff08\u4e0a\u4e00\u4e2a\u8282\u70b9\uff09\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\u3002\u76f8\u8f83\u4e8e\u5355\u5411\u94fe\u8868\uff0c\u53cc\u5411\u94fe\u8868\u66f4\u5177\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u671d\u4e24\u4e2a\u65b9\u5411\u904d\u5386\u94fe\u8868\uff0c\u4f46\u76f8\u5e94\u5730\u4e5f\u9700\u8981\u5360\u7528\u66f4\u591a\u7684\u5185\u5b58\u7a7a\u95f4\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        class ListNode:\n    \"\"\"\u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val                # \u8282\u70b9\u503c\n        self.next: ListNode | None = None  # \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n        self.prev: ListNode | None = None  # \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\n    int val;         // \u8282\u70b9\u503c\n    ListNode *next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\n    ListNode *prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n    ListNode(int x) : val(x), next(nullptr), prev(nullptr) {}  // \u6784\u9020\u51fd\u6570\n};\n
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n    int val;        // \u8282\u70b9\u503c\n    ListNode next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n    ListNode prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n    ListNode(int x) { val = x; }  // \u6784\u9020\u51fd\u6570\n}\n
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode(int x) {  // \u6784\u9020\u51fd\u6570\n    int val = x;    // \u8282\u70b9\u503c\n    ListNode next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n    ListNode prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n}\n
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype DoublyListNode struct {\n    Val  int             // \u8282\u70b9\u503c\n    Next *DoublyListNode // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\n    Prev *DoublyListNode // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n}\n\n// NewDoublyListNode \u521d\u59cb\u5316\nfunc NewDoublyListNode(val int) *DoublyListNode {\n    return &DoublyListNode{\n        Val:  val,\n        Next: nil,\n        Prev: nil,\n    }\n}\n
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n    var val: Int // \u8282\u70b9\u503c\n    var next: ListNode? // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n    var prev: ListNode? // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n\n    init(x: Int) { // \u6784\u9020\u51fd\u6570\n        val = x\n    }\n}\n
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n    constructor(val, next, prev) {\n        this.val = val  ===  undefined ? 0 : val;        // \u8282\u70b9\u503c\n        this.next = next  ===  undefined ? null : next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n        this.prev = prev  ===  undefined ? null : prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n    }\n}\n
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n    val: number;\n    next: ListNode | null;\n    prev: ListNode | null;\n    constructor(val?: number, next?: ListNode | null, prev?: ListNode | null) {\n        this.val = val  ===  undefined ? 0 : val;        // \u8282\u70b9\u503c\n        this.next = next  ===  undefined ? null : next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n        this.prev = prev  ===  undefined ? null : prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n    }\n}\n
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n    int val;        // \u8282\u70b9\u503c\n    ListNode? next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n    ListNode? prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n    ListNode(this.val, [this.next, this.prev]);  // \u6784\u9020\u51fd\u6570\n}\n
        use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\u578b */\n#[derive(Debug)]\nstruct ListNode {\n    val: i32, // \u8282\u70b9\u503c\n    next: Option<Rc<RefCell<ListNode>>>, // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\n    prev: Option<Rc<RefCell<ListNode>>>, // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n}\n\n/* \u6784\u9020\u51fd\u6570 */\nimpl ListNode {\n    fn new(val: i32) -> Self {\n        ListNode {\n            val,\n            next: None,\n            prev: None,\n        }\n    }\n}\n
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\ntypedef struct ListNode {\n    int val;               // \u8282\u70b9\u503c\n    struct ListNode *next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\n    struct ListNode *prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n} ListNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nListNode *newListNode(int val) {\n    ListNode *node;\n    node = (ListNode *) malloc(sizeof(ListNode));\n    node->val = val;\n    node->next = NULL;\n    node->prev = NULL;\n    return node;\n}\n
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\n// \u6784\u9020\u65b9\u6cd5\nclass ListNode(x: Int) {\n    val _val: Int = x           // \u8282\u70b9\u503c\n    val next: ListNode? = null  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n    val prev: ListNode? = null  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n}\n
        # \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\nclass ListNode\n  attr_accessor :val    # \u8282\u70b9\u503c\n  attr_accessor :next   # \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n  attr_accessor :prev   # \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n\n  def initialize(val=0, next_node=nil, prev_node=nil)\n    @val = val\n    @next = next_node\n    @prev = prev_node\n  end\nend\n

        \u56fe 4-8 \u00a0 \u5e38\u89c1\u94fe\u8868\u79cd\u7c7b

        "},{"location":"chapter_array_and_linkedlist/linked_list/#424","title":"4.2.4 \u00a0 \u94fe\u8868\u5178\u578b\u5e94\u7528","text":"

        \u5355\u5411\u94fe\u8868\u901a\u5e38\u7528\u4e8e\u5b9e\u73b0\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u548c\u56fe\u7b49\u6570\u636e\u7ed3\u6784\u3002

        • \u6808\u4e0e\u961f\u5217\uff1a\u5f53\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u90fd\u5728\u94fe\u8868\u7684\u4e00\u7aef\u8fdb\u884c\u65f6\uff0c\u5b83\u8868\u73b0\u7684\u7279\u6027\u4e3a\u5148\u8fdb\u540e\u51fa\uff0c\u5bf9\u5e94\u6808\uff1b\u5f53\u63d2\u5165\u64cd\u4f5c\u5728\u94fe\u8868\u7684\u4e00\u7aef\u8fdb\u884c\uff0c\u5220\u9664\u64cd\u4f5c\u5728\u94fe\u8868\u7684\u53e6\u4e00\u7aef\u8fdb\u884c\uff0c\u5b83\u8868\u73b0\u7684\u7279\u6027\u4e3a\u5148\u8fdb\u5148\u51fa\uff0c\u5bf9\u5e94\u961f\u5217\u3002
        • \u54c8\u5e0c\u8868\uff1a\u94fe\u5f0f\u5730\u5740\u662f\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u7684\u4e3b\u6d41\u65b9\u6848\u4e4b\u4e00\uff0c\u5728\u8be5\u65b9\u6848\u4e2d\uff0c\u6240\u6709\u51b2\u7a81\u7684\u5143\u7d20\u90fd\u4f1a\u88ab\u653e\u5230\u4e00\u4e2a\u94fe\u8868\u4e2d\u3002
        • \u56fe\uff1a\u90bb\u63a5\u8868\u662f\u8868\u793a\u56fe\u7684\u4e00\u79cd\u5e38\u7528\u65b9\u5f0f\uff0c\u5176\u4e2d\u56fe\u7684\u6bcf\u4e2a\u9876\u70b9\u90fd\u4e0e\u4e00\u4e2a\u94fe\u8868\u76f8\u5173\u8054\uff0c\u94fe\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u4ee3\u8868\u4e0e\u8be5\u9876\u70b9\u76f8\u8fde\u7684\u5176\u4ed6\u9876\u70b9\u3002

        \u53cc\u5411\u94fe\u8868\u5e38\u7528\u4e8e\u9700\u8981\u5feb\u901f\u67e5\u627e\u524d\u4e00\u4e2a\u548c\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u573a\u666f\u3002

        • \u9ad8\u7ea7\u6570\u636e\u7ed3\u6784\uff1a\u6bd4\u5982\u5728\u7ea2\u9ed1\u6811\u3001B \u6811\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u8bbf\u95ee\u8282\u70b9\u7684\u7236\u8282\u70b9\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728\u8282\u70b9\u4e2d\u4fdd\u5b58\u4e00\u4e2a\u6307\u5411\u7236\u8282\u70b9\u7684\u5f15\u7528\u6765\u5b9e\u73b0\uff0c\u7c7b\u4f3c\u4e8e\u53cc\u5411\u94fe\u8868\u3002
        • \u6d4f\u89c8\u5668\u5386\u53f2\uff1a\u5728\u7f51\u9875\u6d4f\u89c8\u5668\u4e2d\uff0c\u5f53\u7528\u6237\u70b9\u51fb\u524d\u8fdb\u6216\u540e\u9000\u6309\u94ae\u65f6\uff0c\u6d4f\u89c8\u5668\u9700\u8981\u77e5\u9053\u7528\u6237\u8bbf\u95ee\u8fc7\u7684\u524d\u4e00\u4e2a\u548c\u540e\u4e00\u4e2a\u7f51\u9875\u3002\u53cc\u5411\u94fe\u8868\u7684\u7279\u6027\u4f7f\u5f97\u8fd9\u79cd\u64cd\u4f5c\u53d8\u5f97\u7b80\u5355\u3002
        • LRU \u7b97\u6cd5\uff1a\u5728\u7f13\u5b58\u6dd8\u6c70\uff08LRU\uff09\u7b97\u6cd5\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5feb\u901f\u627e\u5230\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\u6570\u636e\uff0c\u4ee5\u53ca\u652f\u6301\u5feb\u901f\u6dfb\u52a0\u548c\u5220\u9664\u8282\u70b9\u3002\u8fd9\u65f6\u5019\u4f7f\u7528\u53cc\u5411\u94fe\u8868\u5c31\u975e\u5e38\u5408\u9002\u3002

        \u73af\u5f62\u94fe\u8868\u5e38\u7528\u4e8e\u9700\u8981\u5468\u671f\u6027\u64cd\u4f5c\u7684\u573a\u666f\uff0c\u6bd4\u5982\u64cd\u4f5c\u7cfb\u7edf\u7684\u8d44\u6e90\u8c03\u5ea6\u3002

        • \u65f6\u95f4\u7247\u8f6e\u8f6c\u8c03\u5ea6\u7b97\u6cd5\uff1a\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u65f6\u95f4\u7247\u8f6e\u8f6c\u8c03\u5ea6\u7b97\u6cd5\u662f\u4e00\u79cd\u5e38\u89c1\u7684 CPU \u8c03\u5ea6\u7b97\u6cd5\uff0c\u5b83\u9700\u8981\u5bf9\u4e00\u7ec4\u8fdb\u7a0b\u8fdb\u884c\u5faa\u73af\u3002\u6bcf\u4e2a\u8fdb\u7a0b\u88ab\u8d4b\u4e88\u4e00\u4e2a\u65f6\u95f4\u7247\uff0c\u5f53\u65f6\u95f4\u7247\u7528\u5b8c\u65f6\uff0cCPU \u5c06\u5207\u6362\u5230\u4e0b\u4e00\u4e2a\u8fdb\u7a0b\u3002\u8fd9\u79cd\u5faa\u73af\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7\u73af\u5f62\u94fe\u8868\u6765\u5b9e\u73b0\u3002
        • \u6570\u636e\u7f13\u51b2\u533a\uff1a\u5728\u67d0\u4e9b\u6570\u636e\u7f13\u51b2\u533a\u7684\u5b9e\u73b0\u4e2d\uff0c\u4e5f\u53ef\u80fd\u4f1a\u4f7f\u7528\u73af\u5f62\u94fe\u8868\u3002\u6bd4\u5982\u5728\u97f3\u9891\u3001\u89c6\u9891\u64ad\u653e\u5668\u4e2d\uff0c\u6570\u636e\u6d41\u53ef\u80fd\u4f1a\u88ab\u5206\u6210\u591a\u4e2a\u7f13\u51b2\u5757\u5e76\u653e\u5165\u4e00\u4e2a\u73af\u5f62\u94fe\u8868\uff0c\u4ee5\u4fbf\u5b9e\u73b0\u65e0\u7f1d\u64ad\u653e\u3002
        "},{"location":"chapter_array_and_linkedlist/list/","title":"4.3 \u00a0 \u5217\u8868","text":"

        \u5217\u8868\uff08list\uff09\u662f\u4e00\u4e2a\u62bd\u8c61\u7684\u6570\u636e\u7ed3\u6784\u6982\u5ff5\uff0c\u5b83\u8868\u793a\u5143\u7d20\u7684\u6709\u5e8f\u96c6\u5408\uff0c\u652f\u6301\u5143\u7d20\u8bbf\u95ee\u3001\u4fee\u6539\u3001\u6dfb\u52a0\u3001\u5220\u9664\u548c\u904d\u5386\u7b49\u64cd\u4f5c\uff0c\u65e0\u987b\u4f7f\u7528\u8005\u8003\u8651\u5bb9\u91cf\u9650\u5236\u7684\u95ee\u9898\u3002\u5217\u8868\u53ef\u4ee5\u57fa\u4e8e\u94fe\u8868\u6216\u6570\u7ec4\u5b9e\u73b0\u3002

        • \u94fe\u8868\u5929\u7136\u53ef\u4ee5\u770b\u4f5c\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u652f\u6301\u5143\u7d20\u589e\u5220\u67e5\u6539\u64cd\u4f5c\uff0c\u5e76\u4e14\u53ef\u4ee5\u7075\u6d3b\u52a8\u6001\u6269\u5bb9\u3002
        • \u6570\u7ec4\u4e5f\u652f\u6301\u5143\u7d20\u589e\u5220\u67e5\u6539\uff0c\u4f46\u7531\u4e8e\u5176\u957f\u5ea6\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u53ea\u80fd\u770b\u4f5c\u4e00\u4e2a\u5177\u6709\u957f\u5ea6\u9650\u5236\u7684\u5217\u8868\u3002

        \u5f53\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u5217\u8868\u65f6\uff0c\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6027\u8d28\u4f1a\u5bfc\u81f4\u5217\u8868\u7684\u5b9e\u7528\u6027\u964d\u4f4e\u3002\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u901a\u5e38\u65e0\u6cd5\u4e8b\u5148\u786e\u5b9a\u9700\u8981\u5b58\u50a8\u591a\u5c11\u6570\u636e\uff0c\u4ece\u800c\u96be\u4ee5\u9009\u62e9\u5408\u9002\u7684\u5217\u8868\u957f\u5ea6\u3002\u82e5\u957f\u5ea6\u8fc7\u5c0f\uff0c\u5219\u5f88\u53ef\u80fd\u65e0\u6cd5\u6ee1\u8db3\u4f7f\u7528\u9700\u6c42\uff1b\u82e5\u957f\u5ea6\u8fc7\u5927\uff0c\u5219\u4f1a\u9020\u6210\u5185\u5b58\u7a7a\u95f4\u6d6a\u8d39\u3002

        \u4e3a\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\uff08dynamic array\uff09\u6765\u5b9e\u73b0\u5217\u8868\u3002\u5b83\u7ee7\u627f\u4e86\u6570\u7ec4\u7684\u5404\u9879\u4f18\u70b9\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u52a8\u6001\u6269\u5bb9\u3002

        \u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u5217\u8868\u662f\u57fa\u4e8e\u52a8\u6001\u6570\u7ec4\u5b9e\u73b0\u7684\uff0c\u4f8b\u5982 Python \u4e2d\u7684 list \u3001Java \u4e2d\u7684 ArrayList \u3001C++ \u4e2d\u7684 vector \u548c C# \u4e2d\u7684 List \u7b49\u3002\u5728\u63a5\u4e0b\u6765\u7684\u8ba8\u8bba\u4e2d\uff0c\u6211\u4eec\u5c06\u628a\u201c\u5217\u8868\u201d\u548c\u201c\u52a8\u6001\u6570\u7ec4\u201d\u89c6\u4e3a\u7b49\u540c\u7684\u6982\u5ff5\u3002

        "},{"location":"chapter_array_and_linkedlist/list/#431","title":"4.3.1 \u00a0 \u5217\u8868\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/list/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u5217\u8868","text":"

        \u6211\u4eec\u901a\u5e38\u4f7f\u7528\u201c\u65e0\u521d\u59cb\u503c\u201d\u548c\u201c\u6709\u521d\u59cb\u503c\u201d\u8fd9\u4e24\u79cd\u521d\u59cb\u5316\u65b9\u6cd5\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
        # \u521d\u59cb\u5316\u5217\u8868\n# \u65e0\u521d\u59cb\u503c\nnums1: list[int] = []\n# \u6709\u521d\u59cb\u503c\nnums: list[int] = [1, 3, 2, 5, 4]\n
        list.cpp
        /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u9700\u6ce8\u610f\uff0cC++ \u4e2d vector \u5373\u662f\u672c\u6587\u63cf\u8ff0\u7684 nums\n// \u65e0\u521d\u59cb\u503c\nvector<int> nums1;\n// \u6709\u521d\u59cb\u503c\nvector<int> nums = { 1, 3, 2, 5, 4 };\n
        list.java
        /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<Integer> nums1 = new ArrayList<>();\n// \u6709\u521d\u59cb\u503c\uff08\u6ce8\u610f\u6570\u7ec4\u7684\u5143\u7d20\u7c7b\u578b\u9700\u4e3a int[] \u7684\u5305\u88c5\u7c7b Integer[]\uff09\nInteger[] numbers = new Integer[] { 1, 3, 2, 5, 4 };\nList<Integer> nums = new ArrayList<>(Arrays.asList(numbers));\n
        list.cs
        /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<int> nums1 = [];\n// \u6709\u521d\u59cb\u503c\nint[] numbers = [1, 3, 2, 5, 4];\nList<int> nums = [.. numbers];\n
        list_test.go
        /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nnums1 := []int{}\n// \u6709\u521d\u59cb\u503c\nnums := []int{1, 3, 2, 5, 4}\n
        list.swift
        /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nlet nums1: [Int] = []\n// \u6709\u521d\u59cb\u503c\nvar nums = [1, 3, 2, 5, 4]\n
        list.js
        /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nconst nums1 = [];\n// \u6709\u521d\u59cb\u503c\nconst nums = [1, 3, 2, 5, 4];\n
        list.ts
        /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nconst nums1: number[] = [];\n// \u6709\u521d\u59cb\u503c\nconst nums: number[] = [1, 3, 2, 5, 4];\n
        list.dart
        /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<int> nums1 = [];\n// \u6709\u521d\u59cb\u503c\nList<int> nums = [1, 3, 2, 5, 4];\n
        list.rs
        /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nlet nums1: Vec<i32> = Vec::new();\n// \u6709\u521d\u59cb\u503c\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
        list.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
        list.kt
        /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nvar nums1 = listOf<Int>()\n// \u6709\u521d\u59cb\u503c\nvar numbers = arrayOf(1, 3, 2, 5, 4)\nvar nums = numbers.toMutableList()\n
        list.rb
        # \u521d\u59cb\u5316\u5217\u8868\n# \u65e0\u521d\u59cb\u503c\nnums1 = []\n# \u6709\u521d\u59cb\u503c\nnums = [1, 3, 2, 5, 4]\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/list/#2","title":"2. \u00a0 \u8bbf\u95ee\u5143\u7d20","text":"

        \u5217\u8868\u672c\u8d28\u4e0a\u662f\u6570\u7ec4\uff0c\u56e0\u6b64\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u8bbf\u95ee\u548c\u66f4\u65b0\u5143\u7d20\uff0c\u6548\u7387\u5f88\u9ad8\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
        # \u8bbf\u95ee\u5143\u7d20\nnum: int = nums[1]  # \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n# \u66f4\u65b0\u5143\u7d20\nnums[1] = 0    # \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        list.cpp
        /* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        list.java
        /* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums.get(1);  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums.set(1, 0);  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        list.cs
        /* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        list_test.go
        /* \u8bbf\u95ee\u5143\u7d20 */\nnum := nums[1]  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0     // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        list.swift
        /* \u8bbf\u95ee\u5143\u7d20 */\nlet num = nums[1] // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0 // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        list.js
        /* \u8bbf\u95ee\u5143\u7d20 */\nconst num = nums[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        list.ts
        /* \u8bbf\u95ee\u5143\u7d20 */\nconst num: number = nums[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        list.dart
        /* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        list.rs
        /* \u8bbf\u95ee\u5143\u7d20 */\nlet num: i32 = nums[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;             // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        list.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
        list.kt
        /* \u8bbf\u95ee\u5143\u7d20 */\nval num = nums[1]       // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0             // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        list.rb
        # \u8bbf\u95ee\u5143\u7d20\nnum = nums[1] # \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n# \u66f4\u65b0\u5143\u7d20\nnums[1] = 0 # \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/list/#3","title":"3. \u00a0 \u63d2\u5165\u4e0e\u5220\u9664\u5143\u7d20","text":"

        \u76f8\u8f83\u4e8e\u6570\u7ec4\uff0c\u5217\u8868\u53ef\u4ee5\u81ea\u7531\u5730\u6dfb\u52a0\u4e0e\u5220\u9664\u5143\u7d20\u3002\u5728\u5217\u8868\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff0c\u4f46\u63d2\u5165\u548c\u5220\u9664\u5143\u7d20\u7684\u6548\u7387\u4ecd\u4e0e\u6570\u7ec4\u76f8\u540c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
        # \u6e05\u7a7a\u5217\u8868\nnums.clear()\n\n# \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n# \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\nnums.insert(3, 6)  # \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n# \u5220\u9664\u5143\u7d20\nnums.pop(3)        # \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        list.cpp
        /* \u6e05\u7a7a\u5217\u8868 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.push_back(1);\nnums.push_back(3);\nnums.push_back(2);\nnums.push_back(5);\nnums.push_back(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.insert(nums.begin() + 3, 6);  // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.erase(nums.begin() + 3);      // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        list.java
        /* \u6e05\u7a7a\u5217\u8868 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.add(3, 6);  // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.remove(3);  // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        list.cs
        /* \u6e05\u7a7a\u5217\u8868 */\nnums.Clear();\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.Add(1);\nnums.Add(3);\nnums.Add(2);\nnums.Add(5);\nnums.Add(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.Insert(3, 6);  // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.RemoveAt(3);  // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        list_test.go
        /* \u6e05\u7a7a\u5217\u8868 */\nnums = nil\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums = append(nums, 1)\nnums = append(nums, 3)\nnums = append(nums, 2)\nnums = append(nums, 5)\nnums = append(nums, 4)\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums = append(nums[:3], append([]int{6}, nums[3:]...)...) // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums = append(nums[:3], nums[4:]...) // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        list.swift
        /* \u6e05\u7a7a\u5217\u8868 */\nnums.removeAll()\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.insert(6, at: 3) // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.remove(at: 3) // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        list.js
        /* \u6e05\u7a7a\u5217\u8868 */\nnums.length = 0;\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.splice(3, 0, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.splice(3, 1);  // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        list.ts
        /* \u6e05\u7a7a\u5217\u8868 */\nnums.length = 0;\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.splice(3, 0, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.splice(3, 1);  // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        list.dart
        /* \u6e05\u7a7a\u5217\u8868 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.insert(3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.removeAt(3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        list.rs
        /* \u6e05\u7a7a\u5217\u8868 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.insert(3, 6);  // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.remove(3);    // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        list.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
        list.kt
        /* \u6e05\u7a7a\u5217\u8868 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.add(3, 6);  // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.remove(3);  // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        list.rb
        # \u6e05\u7a7a\u5217\u8868\nnums.clear\n\n# \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\nnums << 1\nnums << 3\nnums << 2\nnums << 5\nnums << 4\n\n# \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\nnums.insert(3, 6) # \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n# \u5220\u9664\u5143\u7d20\nnums.delete_at(3) # \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/list/#4","title":"4. \u00a0 \u904d\u5386\u5217\u8868","text":"

        \u4e0e\u6570\u7ec4\u4e00\u6837\uff0c\u5217\u8868\u53ef\u4ee5\u6839\u636e\u7d22\u5f15\u904d\u5386\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u904d\u5386\u5404\u5143\u7d20\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
        # \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\ncount = 0\nfor i in range(len(nums)):\n    count += nums[i]\n\n# \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20\nfor num in nums:\n    count += num\n
        list.cpp
        /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\n    count += nums[i];\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (int num : nums) {\n    count += num;\n}\n
        list.java
        /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\n    count += nums.get(i);\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\nfor (int num : nums) {\n    count += num;\n}\n
        list.cs
        /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < nums.Count; i++) {\n    count += nums[i];\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nforeach (int num in nums) {\n    count += num;\n}\n
        list_test.go
        /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\ncount := 0\nfor i := 0; i < len(nums); i++ {\n    count += nums[i]\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0\nfor _, num := range nums {\n    count += num\n}\n
        list.swift
        /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nvar count = 0\nfor i in nums.indices {\n    count += nums[i]\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0\nfor num in nums {\n    count += num\n}\n
        list.js
        /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\n    count += nums[i];\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (const num of nums) {\n    count += num;\n}\n
        list.ts
        /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\n    count += nums[i];\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (const num of nums) {\n    count += num;\n}\n
        list.dart
        /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (var i = 0; i < nums.length; i++) {\n    count += nums[i];\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (var num in nums) {\n    count += num;\n}\n
        list.rs
        // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\nlet mut _count = 0;\nfor i in 0..nums.len() {\n    _count += nums[i];\n}\n\n// \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20\n_count = 0;\nfor num in &nums {\n    _count += num;\n}\n
        list.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
        list.kt
        /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nvar count = 0\nfor (i in nums.indices) {\n    count += nums[i]\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\nfor (num in nums) {\n    count += num\n}\n
        list.rb
        # \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\ncount = 0\nfor i in 0...nums.length\n    count += nums[i]\nend\n\n# \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20\ncount = 0\nfor num in nums\n    count += num\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/list/#5","title":"5. \u00a0 \u62fc\u63a5\u5217\u8868","text":"

        \u7ed9\u5b9a\u4e00\u4e2a\u65b0\u5217\u8868 nums1 \uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u62fc\u63a5\u5230\u539f\u5217\u8868\u7684\u5c3e\u90e8\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
        # \u62fc\u63a5\u4e24\u4e2a\u5217\u8868\nnums1: list[int] = [6, 8, 7, 10, 9]\nnums += nums1  # \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
        list.cpp
        /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nvector<int> nums1 = { 6, 8, 7, 10, 9 };\n// \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\nnums.insert(nums.end(), nums1.begin(), nums1.end());\n
        list.java
        /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<Integer> nums1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));\nnums.addAll(nums1);  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
        list.cs
        /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.AddRange(nums1);  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
        list_test.go
        /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nnums1 := []int{6, 8, 7, 10, 9}\nnums = append(nums, nums1...)  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
        list.swift
        /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nlet nums1 = [6, 8, 7, 10, 9]\nnums.append(contentsOf: nums1) // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
        list.js
        /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nconst nums1 = [6, 8, 7, 10, 9];\nnums.push(...nums1);  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
        list.ts
        /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nconst nums1: number[] = [6, 8, 7, 10, 9];\nnums.push(...nums1);  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
        list.dart
        /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.addAll(nums1);  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
        list.rs
        /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nlet nums1: Vec<i32> = vec![6, 8, 7, 10, 9];\nnums.extend(nums1);\n
        list.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
        list.kt
        /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nval nums1 = intArrayOf(6, 8, 7, 10, 9).toMutableList()\nnums.addAll(nums1)  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
        list.rb
        # \u62fc\u63a5\u4e24\u4e2a\u5217\u8868\nnums1 = [6, 8, 7, 10, 9]\nnums += nums1\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/list/#6","title":"6. \u00a0 \u6392\u5e8f\u5217\u8868","text":"

        \u5b8c\u6210\u5217\u8868\u6392\u5e8f\u540e\uff0c\u6211\u4eec\u4fbf\u53ef\u4ee5\u4f7f\u7528\u5728\u6570\u7ec4\u7c7b\u7b97\u6cd5\u9898\u4e2d\u7ecf\u5e38\u8003\u67e5\u7684\u201c\u4e8c\u5206\u67e5\u627e\u201d\u548c\u201c\u53cc\u6307\u9488\u201d\u7b97\u6cd5\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
        # \u6392\u5e8f\u5217\u8868\nnums.sort()  # \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        list.cpp
        /* \u6392\u5e8f\u5217\u8868 */\nsort(nums.begin(), nums.end());  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        list.java
        /* \u6392\u5e8f\u5217\u8868 */\nCollections.sort(nums);  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        list.cs
        /* \u6392\u5e8f\u5217\u8868 */\nnums.Sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        list_test.go
        /* \u6392\u5e8f\u5217\u8868 */\nsort.Ints(nums)  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        list.swift
        /* \u6392\u5e8f\u5217\u8868 */\nnums.sort() // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        list.js
        /* \u6392\u5e8f\u5217\u8868 */\nnums.sort((a, b) => a - b);  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        list.ts
        /* \u6392\u5e8f\u5217\u8868 */\nnums.sort((a, b) => a - b);  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        list.dart
        /* \u6392\u5e8f\u5217\u8868 */\nnums.sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        list.rs
        /* \u6392\u5e8f\u5217\u8868 */\nnums.sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        list.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
        list.kt
        /* \u6392\u5e8f\u5217\u8868 */\nnums.sort() // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        list.rb
        # \u6392\u5e8f\u5217\u8868\nnums = nums.sort { |a, b| a <=> b } # \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/list/#432","title":"4.3.2 \u00a0 \u5217\u8868\u5b9e\u73b0","text":"

        \u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u5185\u7f6e\u4e86\u5217\u8868\uff0c\u4f8b\u5982 Java\u3001C++\u3001Python \u7b49\u3002\u5b83\u4eec\u7684\u5b9e\u73b0\u6bd4\u8f83\u590d\u6742\uff0c\u5404\u4e2a\u53c2\u6570\u7684\u8bbe\u5b9a\u4e5f\u975e\u5e38\u8003\u7a76\uff0c\u4f8b\u5982\u521d\u59cb\u5bb9\u91cf\u3001\u6269\u5bb9\u500d\u6570\u7b49\u3002\u611f\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u67e5\u9605\u6e90\u7801\u8fdb\u884c\u5b66\u4e60\u3002

        \u4e3a\u4e86\u52a0\u6df1\u5bf9\u5217\u8868\u5de5\u4f5c\u539f\u7406\u7684\u7406\u89e3\uff0c\u6211\u4eec\u5c1d\u8bd5\u5b9e\u73b0\u4e00\u4e2a\u7b80\u6613\u7248\u5217\u8868\uff0c\u5305\u62ec\u4ee5\u4e0b\u4e09\u4e2a\u91cd\u70b9\u8bbe\u8ba1\u3002

        • \u521d\u59cb\u5bb9\u91cf\uff1a\u9009\u53d6\u4e00\u4e2a\u5408\u7406\u7684\u6570\u7ec4\u521d\u59cb\u5bb9\u91cf\u3002\u5728\u672c\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9009\u62e9 10 \u4f5c\u4e3a\u521d\u59cb\u5bb9\u91cf\u3002
        • \u6570\u91cf\u8bb0\u5f55\uff1a\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf size \uff0c\u7528\u4e8e\u8bb0\u5f55\u5217\u8868\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff0c\u5e76\u968f\u7740\u5143\u7d20\u63d2\u5165\u548c\u5220\u9664\u5b9e\u65f6\u66f4\u65b0\u3002\u6839\u636e\u6b64\u53d8\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4f4d\u5217\u8868\u5c3e\u90e8\uff0c\u4ee5\u53ca\u5224\u65ad\u662f\u5426\u9700\u8981\u6269\u5bb9\u3002
        • \u6269\u5bb9\u673a\u5236\uff1a\u82e5\u63d2\u5165\u5143\u7d20\u65f6\u5217\u8868\u5bb9\u91cf\u5df2\u6ee1\uff0c\u5219\u9700\u8981\u8fdb\u884c\u6269\u5bb9\u3002\u5148\u6839\u636e\u6269\u5bb9\u500d\u6570\u521b\u5efa\u4e00\u4e2a\u66f4\u5927\u7684\u6570\u7ec4\uff0c\u518d\u5c06\u5f53\u524d\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u4f9d\u6b21\u79fb\u52a8\u81f3\u65b0\u6570\u7ec4\u3002\u5728\u672c\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u89c4\u5b9a\u6bcf\u6b21\u5c06\u6570\u7ec4\u6269\u5bb9\u81f3\u4e4b\u524d\u7684 2 \u500d\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_list.py
        class MyList:\n    \"\"\"\u5217\u8868\u7c7b\"\"\"\n\n    def __init__(self):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        self._capacity: int = 10  # \u5217\u8868\u5bb9\u91cf\n        self._arr: list[int] = [0] * self._capacity  # \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n        self._size: int = 0  # \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n        self._extend_ratio: int = 2  # \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n    def size(self) -> int:\n        \"\"\"\u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\"\"\"\n        return self._size\n\n    def capacity(self) -> int:\n        \"\"\"\u83b7\u53d6\u5217\u8868\u5bb9\u91cf\"\"\"\n        return self._capacity\n\n    def get(self, index: int) -> int:\n        \"\"\"\u8bbf\u95ee\u5143\u7d20\"\"\"\n        # \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n        if index < 0 or index >= self._size:\n            raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n        return self._arr[index]\n\n    def set(self, num: int, index: int):\n        \"\"\"\u66f4\u65b0\u5143\u7d20\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n        self._arr[index] = num\n\n    def add(self, num: int):\n        \"\"\"\u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\"\"\"\n        # \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if self.size() == self.capacity():\n            self.extend_capacity()\n        self._arr[self._size] = num\n        self._size += 1\n\n    def insert(self, num: int, index: int):\n        \"\"\"\u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n        # \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if self._size == self.capacity():\n            self.extend_capacity()\n        # \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        for j in range(self._size - 1, index - 1, -1):\n            self._arr[j + 1] = self._arr[j]\n        self._arr[index] = num\n        # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        self._size += 1\n\n    def remove(self, index: int) -> int:\n        \"\"\"\u5220\u9664\u5143\u7d20\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n        num = self._arr[index]\n        # \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n        for j in range(index, self._size - 1):\n            self._arr[j] = self._arr[j + 1]\n        # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        self._size -= 1\n        # \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n        return num\n\n    def extend_capacity(self):\n        \"\"\"\u5217\u8868\u6269\u5bb9\"\"\"\n        # \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 _extend_ratio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n        self._arr = self._arr + [0] * self.capacity() * (self._extend_ratio - 1)\n        # \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n        self._capacity = len(self._arr)\n\n    def to_array(self) -> list[int]:\n        \"\"\"\u8fd4\u56de\u6709\u6548\u957f\u5ea6\u7684\u5217\u8868\"\"\"\n        return self._arr[: self._size]\n
        my_list.cpp
        /* \u5217\u8868\u7c7b */\nclass MyList {\n  private:\n    int *arr;             // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n    int arrCapacity = 10; // \u5217\u8868\u5bb9\u91cf\n    int arrSize = 0;      // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n    int extendRatio = 2;   // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n  public:\n    /* \u6784\u9020\u65b9\u6cd5 */\n    MyList() {\n        arr = new int[arrCapacity];\n    }\n\n    /* \u6790\u6784\u65b9\u6cd5 */\n    ~MyList() {\n        delete[] arr;\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n    int size() {\n        return arrSize;\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n    int capacity() {\n        return arrCapacity;\n    }\n\n    /* \u8bbf\u95ee\u5143\u7d20 */\n    int get(int index) {\n        // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n        if (index < 0 || index >= size())\n            throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n        return arr[index];\n    }\n\n    /* \u66f4\u65b0\u5143\u7d20 */\n    void set(int index, int num) {\n        if (index < 0 || index >= size())\n            throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n        arr[index] = num;\n    }\n\n    /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n    void add(int num) {\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if (size() == capacity())\n            extendCapacity();\n        arr[size()] = num;\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        arrSize++;\n    }\n\n    /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n    void insert(int index, int num) {\n        if (index < 0 || index >= size())\n            throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if (size() == capacity())\n            extendCapacity();\n        // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        for (int j = size() - 1; j >= index; j--) {\n            arr[j + 1] = arr[j];\n        }\n        arr[index] = num;\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        arrSize++;\n    }\n\n    /* \u5220\u9664\u5143\u7d20 */\n    int remove(int index) {\n        if (index < 0 || index >= size())\n            throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n        int num = arr[index];\n        // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n        for (int j = index; j < size() - 1; j++) {\n            arr[j] = arr[j + 1];\n        }\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        arrSize--;\n        // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n        return num;\n    }\n\n    /* \u5217\u8868\u6269\u5bb9 */\n    void extendCapacity() {\n        // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\n        int newCapacity = capacity() * extendRatio;\n        int *tmp = arr;\n        arr = new int[newCapacity];\n        // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n        for (int i = 0; i < size(); i++) {\n            arr[i] = tmp[i];\n        }\n        // \u91ca\u653e\u5185\u5b58\n        delete[] tmp;\n        arrCapacity = newCapacity;\n    }\n\n    /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a Vector \u7528\u4e8e\u6253\u5370 */\n    vector<int> toVector() {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        vector<int> vec(size());\n        for (int i = 0; i < size(); i++) {\n            vec[i] = arr[i];\n        }\n        return vec;\n    }\n};\n
        my_list.java
        /* \u5217\u8868\u7c7b */\nclass MyList {\n    private int[] arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n    private int capacity = 10; // \u5217\u8868\u5bb9\u91cf\n    private int size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n    private int extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public MyList() {\n        arr = new int[capacity];\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\n    public int size() {\n        return size;\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n    public int capacity() {\n        return capacity;\n    }\n\n    /* \u8bbf\u95ee\u5143\u7d20 */\n    public int get(int index) {\n        // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n        return arr[index];\n    }\n\n    /* \u66f4\u65b0\u5143\u7d20 */\n    public void set(int index, int num) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n        arr[index] = num;\n    }\n\n    /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n    public void add(int num) {\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if (size == capacity())\n            extendCapacity();\n        arr[size] = num;\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        size++;\n    }\n\n    /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n    public void insert(int index, int num) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if (size == capacity())\n            extendCapacity();\n        // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        for (int j = size - 1; j >= index; j--) {\n            arr[j + 1] = arr[j];\n        }\n        arr[index] = num;\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        size++;\n    }\n\n    /* \u5220\u9664\u5143\u7d20 */\n    public int remove(int index) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n        int num = arr[index];\n        // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n        for (int j = index; j < size - 1; j++) {\n            arr[j] = arr[j + 1];\n        }\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        size--;\n        // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n        return num;\n    }\n\n    /* \u5217\u8868\u6269\u5bb9 */\n    public void extendCapacity() {\n        // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n        arr = Arrays.copyOf(arr, capacity() * extendRatio);\n        // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n        capacity = arr.length;\n    }\n\n    /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n    public int[] toArray() {\n        int size = size();\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        int[] arr = new int[size];\n        for (int i = 0; i < size; i++) {\n            arr[i] = get(i);\n        }\n        return arr;\n    }\n}\n
        my_list.cs
        /* \u5217\u8868\u7c7b */\nclass MyList {\n    private int[] arr;           // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n    private int arrCapacity = 10;    // \u5217\u8868\u5bb9\u91cf\n    private int arrSize = 0;         // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n    private readonly int extendRatio = 2;  // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public MyList() {\n        arr = new int[arrCapacity];\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n    public int Size() {\n        return arrSize;\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n    public int Capacity() {\n        return arrCapacity;\n    }\n\n    /* \u8bbf\u95ee\u5143\u7d20 */\n    public int Get(int index) {\n        // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n        return arr[index];\n    }\n\n    /* \u66f4\u65b0\u5143\u7d20 */\n    public void Set(int index, int num) {\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n        arr[index] = num;\n    }\n\n    /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n    public void Add(int num) {\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if (arrSize == arrCapacity)\n            ExtendCapacity();\n        arr[arrSize] = num;\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        arrSize++;\n    }\n\n    /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n    public void Insert(int index, int num) {\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if (arrSize == arrCapacity)\n            ExtendCapacity();\n        // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        for (int j = arrSize - 1; j >= index; j--) {\n            arr[j + 1] = arr[j];\n        }\n        arr[index] = num;\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        arrSize++;\n    }\n\n    /* \u5220\u9664\u5143\u7d20 */\n    public int Remove(int index) {\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n        int num = arr[index];\n        // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n        for (int j = index; j < arrSize - 1; j++) {\n            arr[j] = arr[j + 1];\n        }\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        arrSize--;\n        // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n        return num;\n    }\n\n    /* \u5217\u8868\u6269\u5bb9 */\n    public void ExtendCapacity() {\n        // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a arrCapacity * extendRatio \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n        Array.Resize(ref arr, arrCapacity * extendRatio);\n        // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n        arrCapacity = arr.Length;\n    }\n\n    /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n    public int[] ToArray() {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        int[] arr = new int[arrSize];\n        for (int i = 0; i < arrSize; i++) {\n            arr[i] = Get(i);\n        }\n        return arr;\n    }\n}\n
        my_list.go
        /* \u5217\u8868\u7c7b */\ntype myList struct {\n    arrCapacity int\n    arr         []int\n    arrSize     int\n    extendRatio int\n}\n\n/* \u6784\u9020\u51fd\u6570 */\nfunc newMyList() *myList {\n    return &myList{\n        arrCapacity: 10,              // \u5217\u8868\u5bb9\u91cf\n        arr:         make([]int, 10), // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n        arrSize:     0,               // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n        extendRatio: 2,               // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n    }\n}\n\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\nfunc (l *myList) size() int {\n    return l.arrSize\n}\n\n/*  \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nfunc (l *myList) capacity() int {\n    return l.arrCapacity\n}\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nfunc (l *myList) get(index int) int {\n    // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n    if index < 0 || index >= l.arrSize {\n        panic(\"\u7d22\u5f15\u8d8a\u754c\")\n    }\n    return l.arr[index]\n}\n\n/* \u66f4\u65b0\u5143\u7d20 */\nfunc (l *myList) set(num, index int) {\n    if index < 0 || index >= l.arrSize {\n        panic(\"\u7d22\u5f15\u8d8a\u754c\")\n    }\n    l.arr[index] = num\n}\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nfunc (l *myList) add(num int) {\n    // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n    if l.arrSize == l.arrCapacity {\n        l.extendCapacity()\n    }\n    l.arr[l.arrSize] = num\n    // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n    l.arrSize++\n}\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nfunc (l *myList) insert(num, index int) {\n    if index < 0 || index >= l.arrSize {\n        panic(\"\u7d22\u5f15\u8d8a\u754c\")\n    }\n    // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n    if l.arrSize == l.arrCapacity {\n        l.extendCapacity()\n    }\n    // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for j := l.arrSize - 1; j >= index; j-- {\n        l.arr[j+1] = l.arr[j]\n    }\n    l.arr[index] = num\n    // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n    l.arrSize++\n}\n\n/* \u5220\u9664\u5143\u7d20 */\nfunc (l *myList) remove(index int) int {\n    if index < 0 || index >= l.arrSize {\n        panic(\"\u7d22\u5f15\u8d8a\u754c\")\n    }\n    num := l.arr[index]\n    // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for j := index; j < l.arrSize-1; j++ {\n        l.arr[j] = l.arr[j+1]\n    }\n    // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n    l.arrSize--\n    // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n    return num\n}\n\n/* \u5217\u8868\u6269\u5bb9 */\nfunc (l *myList) extendCapacity() {\n    // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    l.arr = append(l.arr, make([]int, l.arrCapacity*(l.extendRatio-1))...)\n    // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n    l.arrCapacity = len(l.arr)\n}\n\n/* \u8fd4\u56de\u6709\u6548\u957f\u5ea6\u7684\u5217\u8868 */\nfunc (l *myList) toArray() []int {\n    // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n    return l.arr[:l.arrSize]\n}\n
        my_list.swift
        /* \u5217\u8868\u7c7b */\nclass MyList {\n    private var arr: [Int] // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n    private var _capacity: Int // \u5217\u8868\u5bb9\u91cf\n    private var _size: Int // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n    private let extendRatio: Int // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    init() {\n        _capacity = 10\n        _size = 0\n        extendRatio = 2\n        arr = Array(repeating: 0, count: _capacity)\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n    func size() -> Int {\n        _size\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n    func capacity() -> Int {\n        _capacity\n    }\n\n    /* \u8bbf\u95ee\u5143\u7d20 */\n    func get(index: Int) -> Int {\n        // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u9519\u8bef\uff0c\u4e0b\u540c\n        if index < 0 || index >= size() {\n            fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n        }\n        return arr[index]\n    }\n\n    /* \u66f4\u65b0\u5143\u7d20 */\n    func set(index: Int, num: Int) {\n        if index < 0 || index >= size() {\n            fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n        }\n        arr[index] = num\n    }\n\n    /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n    func add(num: Int) {\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if size() == capacity() {\n            extendCapacity()\n        }\n        arr[size()] = num\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        _size += 1\n    }\n\n    /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n    func insert(index: Int, num: Int) {\n        if index < 0 || index >= size() {\n            fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n        }\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if size() == capacity() {\n            extendCapacity()\n        }\n        // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        for j in (index ..< size()).reversed() {\n            arr[j + 1] = arr[j]\n        }\n        arr[index] = num\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        _size += 1\n    }\n\n    /* \u5220\u9664\u5143\u7d20 */\n    @discardableResult\n    func remove(index: Int) -> Int {\n        if index < 0 || index >= size() {\n            fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n        }\n        let num = arr[index]\n        // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n        for j in index ..< (size() - 1) {\n            arr[j] = arr[j + 1]\n        }\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        _size -= 1\n        // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n        return num\n    }\n\n    /* \u5217\u8868\u6269\u5bb9 */\n    func extendCapacity() {\n        // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n        arr = arr + Array(repeating: 0, count: capacity() * (extendRatio - 1))\n        // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n        _capacity = arr.count\n    }\n\n    /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n    func toArray() -> [Int] {\n        Array(arr.prefix(size()))\n    }\n}\n
        my_list.js
        /* \u5217\u8868\u7c7b */\nclass MyList {\n    #arr = new Array(); // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n    #capacity = 10; // \u5217\u8868\u5bb9\u91cf\n    #size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n    #extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor() {\n        this.#arr = new Array(this.#capacity);\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n    size() {\n        return this.#size;\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n    capacity() {\n        return this.#capacity;\n    }\n\n    /* \u8bbf\u95ee\u5143\u7d20 */\n    get(index) {\n        // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n        if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n        return this.#arr[index];\n    }\n\n    /* \u66f4\u65b0\u5143\u7d20 */\n    set(index, num) {\n        if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n        this.#arr[index] = num;\n    }\n\n    /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n    add(num) {\n        // \u5982\u679c\u957f\u5ea6\u7b49\u4e8e\u5bb9\u91cf\uff0c\u5219\u9700\u8981\u6269\u5bb9\n        if (this.#size === this.#capacity) {\n            this.extendCapacity();\n        }\n        // \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u5c3e\u90e8\n        this.#arr[this.#size] = num;\n        this.#size++;\n    }\n\n    /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n    insert(index, num) {\n        if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if (this.#size === this.#capacity) {\n            this.extendCapacity();\n        }\n        // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        for (let j = this.#size - 1; j >= index; j--) {\n            this.#arr[j + 1] = this.#arr[j];\n        }\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        this.#arr[index] = num;\n        this.#size++;\n    }\n\n    /* \u5220\u9664\u5143\u7d20 */\n    remove(index) {\n        if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n        let num = this.#arr[index];\n        // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n        for (let j = index; j < this.#size - 1; j++) {\n            this.#arr[j] = this.#arr[j + 1];\n        }\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        this.#size--;\n        // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n        return num;\n    }\n\n    /* \u5217\u8868\u6269\u5bb9 */\n    extendCapacity() {\n        // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n        this.#arr = this.#arr.concat(\n            new Array(this.capacity() * (this.#extendRatio - 1))\n        );\n        // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n        this.#capacity = this.#arr.length;\n    }\n\n    /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n    toArray() {\n        let size = this.size();\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        const arr = new Array(size);\n        for (let i = 0; i < size; i++) {\n            arr[i] = this.get(i);\n        }\n        return arr;\n    }\n}\n
        my_list.ts
        /* \u5217\u8868\u7c7b */\nclass MyList {\n    private arr: Array<number>; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n    private _capacity: number = 10; // \u5217\u8868\u5bb9\u91cf\n    private _size: number = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n    private extendRatio: number = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor() {\n        this.arr = new Array(this._capacity);\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n    public size(): number {\n        return this._size;\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n    public capacity(): number {\n        return this._capacity;\n    }\n\n    /* \u8bbf\u95ee\u5143\u7d20 */\n    public get(index: number): number {\n        // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n        if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n        return this.arr[index];\n    }\n\n    /* \u66f4\u65b0\u5143\u7d20 */\n    public set(index: number, num: number): void {\n        if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n        this.arr[index] = num;\n    }\n\n    /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n    public add(num: number): void {\n        // \u5982\u679c\u957f\u5ea6\u7b49\u4e8e\u5bb9\u91cf\uff0c\u5219\u9700\u8981\u6269\u5bb9\n        if (this._size === this._capacity) this.extendCapacity();\n        // \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u5c3e\u90e8\n        this.arr[this._size] = num;\n        this._size++;\n    }\n\n    /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n    public insert(index: number, num: number): void {\n        if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if (this._size === this._capacity) {\n            this.extendCapacity();\n        }\n        // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        for (let j = this._size - 1; j >= index; j--) {\n            this.arr[j + 1] = this.arr[j];\n        }\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        this.arr[index] = num;\n        this._size++;\n    }\n\n    /* \u5220\u9664\u5143\u7d20 */\n    public remove(index: number): number {\n        if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n        let num = this.arr[index];\n        // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n        for (let j = index; j < this._size - 1; j++) {\n            this.arr[j] = this.arr[j + 1];\n        }\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        this._size--;\n        // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n        return num;\n    }\n\n    /* \u5217\u8868\u6269\u5bb9 */\n    public extendCapacity(): void {\n        // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a size \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n        this.arr = this.arr.concat(\n            new Array(this.capacity() * (this.extendRatio - 1))\n        );\n        // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n        this._capacity = this.arr.length;\n    }\n\n    /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n    public toArray(): number[] {\n        let size = this.size();\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        const arr = new Array(size);\n        for (let i = 0; i < size; i++) {\n            arr[i] = this.get(i);\n        }\n        return arr;\n    }\n}\n
        my_list.dart
        /* \u5217\u8868\u7c7b */\nclass MyList {\n  late List<int> _arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n  int _capacity = 10; // \u5217\u8868\u5bb9\u91cf\n  int _size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n  int _extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n  /* \u6784\u9020\u65b9\u6cd5 */\n  MyList() {\n    _arr = List.filled(_capacity, 0);\n  }\n\n  /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n  int size() => _size;\n\n  /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n  int capacity() => _capacity;\n\n  /* \u8bbf\u95ee\u5143\u7d20 */\n  int get(int index) {\n    if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n    return _arr[index];\n  }\n\n  /* \u66f4\u65b0\u5143\u7d20 */\n  void set(int index, int _num) {\n    if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n    _arr[index] = _num;\n  }\n\n  /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n  void add(int _num) {\n    // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n    if (_size == _capacity) extendCapacity();\n    _arr[_size] = _num;\n    // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n    _size++;\n  }\n\n  /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n  void insert(int index, int _num) {\n    if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n    // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n    if (_size == _capacity) extendCapacity();\n    // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for (var j = _size - 1; j >= index; j--) {\n      _arr[j + 1] = _arr[j];\n    }\n    _arr[index] = _num;\n    // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n    _size++;\n  }\n\n  /* \u5220\u9664\u5143\u7d20 */\n  int remove(int index) {\n    if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n    int _num = _arr[index];\n    // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for (var j = index; j < _size - 1; j++) {\n      _arr[j] = _arr[j + 1];\n    }\n    // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n    _size--;\n    // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n    return _num;\n  }\n\n  /* \u5217\u8868\u6269\u5bb9 */\n  void extendCapacity() {\n    // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 _extendRatio \u500d\u7684\u65b0\u6570\u7ec4\n    final _newNums = List.filled(_capacity * _extendRatio, 0);\n    // \u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    List.copyRange(_newNums, 0, _arr);\n    // \u66f4\u65b0 _arr \u7684\u5f15\u7528\n    _arr = _newNums;\n    // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n    _capacity = _arr.length;\n  }\n\n  /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n  List<int> toArray() {\n    List<int> arr = [];\n    for (var i = 0; i < _size; i++) {\n      arr.add(get(i));\n    }\n    return arr;\n  }\n}\n
        my_list.rs
        /* \u5217\u8868\u7c7b */\n#[allow(dead_code)]\nstruct MyList {\n    arr: Vec<i32>,       // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n    capacity: usize,     // \u5217\u8868\u5bb9\u91cf\n    size: usize,         // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n    extend_ratio: usize, // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n}\n\n#[allow(unused, unused_comparisons)]\nimpl MyList {\n    /* \u6784\u9020\u65b9\u6cd5 */\n    pub fn new(capacity: usize) -> Self {\n        let mut vec = vec![0; capacity];\n        Self {\n            arr: vec,\n            capacity,\n            size: 0,\n            extend_ratio: 2,\n        }\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n    pub fn size(&self) -> usize {\n        return self.size;\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n    pub fn capacity(&self) -> usize {\n        return self.capacity;\n    }\n\n    /* \u8bbf\u95ee\u5143\u7d20 */\n    pub fn get(&self, index: usize) -> i32 {\n        // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n        if index >= self.size {\n            panic!(\"\u7d22\u5f15\u8d8a\u754c\")\n        };\n        return self.arr[index];\n    }\n\n    /* \u66f4\u65b0\u5143\u7d20 */\n    pub fn set(&mut self, index: usize, num: i32) {\n        if index >= self.size {\n            panic!(\"\u7d22\u5f15\u8d8a\u754c\")\n        };\n        self.arr[index] = num;\n    }\n\n    /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n    pub fn add(&mut self, num: i32) {\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if self.size == self.capacity() {\n            self.extend_capacity();\n        }\n        self.arr[self.size] = num;\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        self.size += 1;\n    }\n\n    /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n    pub fn insert(&mut self, index: usize, num: i32) {\n        if index >= self.size() {\n            panic!(\"\u7d22\u5f15\u8d8a\u754c\")\n        };\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if self.size == self.capacity() {\n            self.extend_capacity();\n        }\n        // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        for j in (index..self.size).rev() {\n            self.arr[j + 1] = self.arr[j];\n        }\n        self.arr[index] = num;\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        self.size += 1;\n    }\n\n    /* \u5220\u9664\u5143\u7d20 */\n    pub fn remove(&mut self, index: usize) -> i32 {\n        if index >= self.size() {\n            panic!(\"\u7d22\u5f15\u8d8a\u754c\")\n        };\n        let num = self.arr[index];\n        // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n        for j in index..self.size - 1 {\n            self.arr[j] = self.arr[j + 1];\n        }\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        self.size -= 1;\n        // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n        return num;\n    }\n\n    /* \u5217\u8868\u6269\u5bb9 */\n    pub fn extend_capacity(&mut self) {\n        // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extend_ratio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n        let new_capacity = self.capacity * self.extend_ratio;\n        self.arr.resize(new_capacity, 0);\n        // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n        self.capacity = new_capacity;\n    }\n\n    /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n    pub fn to_array(&self) -> Vec<i32> {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        let mut arr = Vec::new();\n        for i in 0..self.size {\n            arr.push(self.get(i));\n        }\n        arr\n    }\n}\n
        my_list.c
        /* \u5217\u8868\u7c7b */\ntypedef struct {\n    int *arr;        // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n    int capacity;    // \u5217\u8868\u5bb9\u91cf\n    int size;        // \u5217\u8868\u5927\u5c0f\n    int extendRatio; // \u5217\u8868\u6bcf\u6b21\u6269\u5bb9\u7684\u500d\u6570\n} MyList;\n\n/* \u6784\u9020\u51fd\u6570 */\nMyList *newMyList() {\n    MyList *nums = malloc(sizeof(MyList));\n    nums->capacity = 10;\n    nums->arr = malloc(sizeof(int) * nums->capacity);\n    nums->size = 0;\n    nums->extendRatio = 2;\n    return nums;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delMyList(MyList *nums) {\n    free(nums->arr);\n    free(nums);\n}\n\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6 */\nint size(MyList *nums) {\n    return nums->size;\n}\n\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nint capacity(MyList *nums) {\n    return nums->capacity;\n}\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nint get(MyList *nums, int index) {\n    assert(index >= 0 && index < nums->size);\n    return nums->arr[index];\n}\n\n/* \u66f4\u65b0\u5143\u7d20 */\nvoid set(MyList *nums, int index, int num) {\n    assert(index >= 0 && index < nums->size);\n    nums->arr[index] = num;\n}\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nvoid add(MyList *nums, int num) {\n    if (size(nums) == capacity(nums)) {\n        extendCapacity(nums); // \u6269\u5bb9\n    }\n    nums->arr[size(nums)] = num;\n    nums->size++;\n}\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nvoid insert(MyList *nums, int index, int num) {\n    assert(index >= 0 && index < size(nums));\n    // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n    if (size(nums) == capacity(nums)) {\n        extendCapacity(nums); // \u6269\u5bb9\n    }\n    for (int i = size(nums); i > index; --i) {\n        nums->arr[i] = nums->arr[i - 1];\n    }\n    nums->arr[index] = num;\n    nums->size++;\n}\n\n/* \u5220\u9664\u5143\u7d20 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nint removeItem(MyList *nums, int index) {\n    assert(index >= 0 && index < size(nums));\n    int num = nums->arr[index];\n    for (int i = index; i < size(nums) - 1; i++) {\n        nums->arr[i] = nums->arr[i + 1];\n    }\n    nums->size--;\n    return num;\n}\n\n/* \u5217\u8868\u6269\u5bb9 */\nvoid extendCapacity(MyList *nums) {\n    // \u5148\u5206\u914d\u7a7a\u95f4\n    int newCapacity = capacity(nums) * nums->extendRatio;\n    int *extend = (int *)malloc(sizeof(int) * newCapacity);\n    int *temp = nums->arr;\n\n    // \u62f7\u8d1d\u65e7\u6570\u636e\u5230\u65b0\u6570\u636e\n    for (int i = 0; i < size(nums); i++)\n        extend[i] = nums->arr[i];\n\n    // \u91ca\u653e\u65e7\u6570\u636e\n    free(temp);\n\n    // \u66f4\u65b0\u65b0\u6570\u636e\n    nums->arr = extend;\n    nums->capacity = newCapacity;\n}\n\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a Array \u7528\u4e8e\u6253\u5370 */\nint *toArray(MyList *nums) {\n    return nums->arr;\n}\n
        my_list.kt
        /* \u5217\u8868\u7c7b */\nclass MyList {\n    private var arr: IntArray = intArrayOf() // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n    private var capacity: Int = 10 // \u5217\u8868\u5bb9\u91cf\n    private var size: Int = 0 // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n    private var extendRatio: Int = 2 // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    init {\n        arr = IntArray(capacity)\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\n    fun size(): Int {\n        return size\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n    fun capacity(): Int {\n        return capacity\n    }\n\n    /* \u8bbf\u95ee\u5143\u7d20 */\n    fun get(index: Int): Int {\n        // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n        if (index < 0 || index >= size)\n            throw IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\")\n        return arr[index]\n    }\n\n    /* \u66f4\u65b0\u5143\u7d20 */\n    fun set(index: Int, num: Int) {\n        if (index < 0 || index >= size)\n            throw IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\")\n        arr[index] = num\n    }\n\n    /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n    fun add(num: Int) {\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if (size == capacity())\n            extendCapacity()\n        arr[size] = num\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        size++\n    }\n\n    /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n    fun insert(index: Int, num: Int) {\n        if (index < 0 || index >= size)\n            throw IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\")\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if (size == capacity())\n            extendCapacity()\n        // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        for (j in size - 1 downTo index)\n            arr[j + 1] = arr[j]\n        arr[index] = num\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        size++\n    }\n\n    /* \u5220\u9664\u5143\u7d20 */\n    fun remove(index: Int): Int {\n        if (index < 0 || index >= size)\n            throw IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\")\n        val num = arr[index]\n        // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n        for (j in index..<size - 1)\n            arr[j] = arr[j + 1]\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        size--\n        // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n        return num\n    }\n\n    /* \u5217\u8868\u6269\u5bb9 */\n    fun extendCapacity() {\n        // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n        arr = arr.copyOf(capacity() * extendRatio)\n        // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n        capacity = arr.size\n    }\n\n    /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n    fun toArray(): IntArray {\n        val size = size()\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        val arr = IntArray(size)\n        for (i in 0..<size) {\n            arr[i] = get(i)\n        }\n        return arr\n    }\n}\n
        my_list.rb
        ### \u5217\u8868\u7c7b ###\nclass MyList\n  attr_reader :size       # \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n  attr_reader :capacity   # \u83b7\u53d6\u5217\u8868\u5bb9\u91cf\n\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize\n    @capacity = 10\n    @size = 0\n    @extend_ratio = 2\n    @arr = Array.new(capacity)\n  end\n\n  ### \u8bbf\u95ee\u5143\u7d20 ###\n  def get(index)\n    # \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n    raise IndexError, \"\u7d22\u5f15\u8d8a\u754c\" if index < 0 || index >= size\n    @arr[index]\n  end\n\n  ### \u8bbf\u95ee\u5143\u7d20 ###\n  def set(index, num)\n    raise IndexError, \"\u7d22\u5f15\u8d8a\u754c\" if index < 0 || index >= size\n    @arr[index] = num\n  end\n\n  ### \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 ###\n  def add(num)\n    # \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n    extend_capacity if size == capacity\n    @arr[size] = num\n\n    # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n    @size += 1\n  end\n\n  ### \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 ###\n  def insert(index, num)\n    raise IndexError, \"\u7d22\u5f15\u8d8a\u754c\" if index < 0 || index >= size\n\n    # \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n    extend_capacity if size == capacity\n\n    # \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for j in (size - 1).downto(index)\n      @arr[j + 1] = @arr[j]\n    end\n    @arr[index] = num\n\n    # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n    @size += 1\n  end\n\n  ### \u5220\u9664\u5143\u7d20 ###\n  def remove(index)\n    raise IndexError, \"\u7d22\u5f15\u8d8a\u754c\" if index < 0 || index >= size\n    num = @arr[index]\n\n    # \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for j in index...size\n      @arr[j] = @arr[j + 1]\n    end\n\n    # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n    @size -= 1\n\n    # \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n    num\n  end\n\n  ### \u5217\u8868\u6269\u5bb9 ###\n  def extend_capacity\n    # \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extend_ratio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    arr = @arr.dup + Array.new(capacity * (@extend_ratio - 1))\n    # \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n    @capacity = arr.length\n  end\n\n  ### \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 ###\n  def to_array\n    sz = size\n    # \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n    arr = Array.new(sz)\n    for i in 0...sz\n      arr[i] = get(i)\n    end\n    arr\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/ram_and_cache/","title":"4.4 \u00a0 \u5185\u5b58\u4e0e\u7f13\u5b58 *","text":"

        \u5728\u672c\u7ae0\u7684\u524d\u4e24\u8282\u4e2d\uff0c\u6211\u4eec\u63a2\u8ba8\u4e86\u6570\u7ec4\u548c\u94fe\u8868\u8fd9\u4e24\u79cd\u57fa\u7840\u4e14\u91cd\u8981\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u4eec\u5206\u522b\u4ee3\u8868\u4e86\u201c\u8fde\u7eed\u5b58\u50a8\u201d\u548c\u201c\u5206\u6563\u5b58\u50a8\u201d\u4e24\u79cd\u7269\u7406\u7ed3\u6784\u3002

        \u5b9e\u9645\u4e0a\uff0c\u7269\u7406\u7ed3\u6784\u5728\u5f88\u5927\u7a0b\u5ea6\u4e0a\u51b3\u5b9a\u4e86\u7a0b\u5e8f\u5bf9\u5185\u5b58\u548c\u7f13\u5b58\u7684\u4f7f\u7528\u6548\u7387\uff0c\u8fdb\u800c\u5f71\u54cd\u7b97\u6cd5\u7a0b\u5e8f\u7684\u6574\u4f53\u6027\u80fd\u3002

        "},{"location":"chapter_array_and_linkedlist/ram_and_cache/#441","title":"4.4.1 \u00a0 \u8ba1\u7b97\u673a\u5b58\u50a8\u8bbe\u5907","text":"

        \u8ba1\u7b97\u673a\u4e2d\u5305\u62ec\u4e09\u79cd\u7c7b\u578b\u7684\u5b58\u50a8\u8bbe\u5907\uff1a\u786c\u76d8\uff08hard disk\uff09\u3001\u5185\u5b58\uff08random-access memory, RAM\uff09\u3001\u7f13\u5b58\uff08cache memory\uff09\u3002\u8868 4-2 \u5c55\u793a\u4e86\u5b83\u4eec\u5728\u8ba1\u7b97\u673a\u7cfb\u7edf\u4e2d\u7684\u4e0d\u540c\u89d2\u8272\u548c\u6027\u80fd\u7279\u70b9\u3002

        \u8868 4-2 \u00a0 \u8ba1\u7b97\u673a\u7684\u5b58\u50a8\u8bbe\u5907

        \u786c\u76d8 \u5185\u5b58 \u7f13\u5b58 \u7528\u9014 \u957f\u671f\u5b58\u50a8\u6570\u636e\uff0c\u5305\u62ec\u64cd\u4f5c\u7cfb\u7edf\u3001\u7a0b\u5e8f\u3001\u6587\u4ef6\u7b49 \u4e34\u65f6\u5b58\u50a8\u5f53\u524d\u8fd0\u884c\u7684\u7a0b\u5e8f\u548c\u6b63\u5728\u5904\u7406\u7684\u6570\u636e \u5b58\u50a8\u7ecf\u5e38\u8bbf\u95ee\u7684\u6570\u636e\u548c\u6307\u4ee4\uff0c\u51cf\u5c11 CPU \u8bbf\u95ee\u5185\u5b58\u7684\u6b21\u6570 \u6613\u5931\u6027 \u65ad\u7535\u540e\u6570\u636e\u4e0d\u4f1a\u4e22\u5931 \u65ad\u7535\u540e\u6570\u636e\u4f1a\u4e22\u5931 \u65ad\u7535\u540e\u6570\u636e\u4f1a\u4e22\u5931 \u5bb9\u91cf \u8f83\u5927\uff0cTB \u7ea7\u522b \u8f83\u5c0f\uff0cGB \u7ea7\u522b \u975e\u5e38\u5c0f\uff0cMB \u7ea7\u522b \u901f\u5ea6 \u8f83\u6162\uff0c\u51e0\u767e\u5230\u51e0\u5343 MB/s \u8f83\u5feb\uff0c\u51e0\u5341 GB/s \u975e\u5e38\u5feb\uff0c\u51e0\u5341\u5230\u51e0\u767e GB/s \u4ef7\u683c\uff08\u4eba\u6c11\u5e01\uff09 \u8f83\u4fbf\u5b9c\uff0c\u51e0\u6bdb\u5230\u51e0\u5143 / GB \u8f83\u8d35\uff0c\u51e0\u5341\u5230\u51e0\u767e\u5143 / GB \u975e\u5e38\u8d35\uff0c\u968f CPU \u6253\u5305\u8ba1\u4ef7

        \u6211\u4eec\u53ef\u4ee5\u5c06\u8ba1\u7b97\u673a\u5b58\u50a8\u7cfb\u7edf\u60f3\u8c61\u4e3a\u56fe 4-9 \u6240\u793a\u7684\u91d1\u5b57\u5854\u7ed3\u6784\u3002\u8d8a\u9760\u8fd1\u91d1\u5b57\u5854\u9876\u7aef\u7684\u5b58\u50a8\u8bbe\u5907\u7684\u901f\u5ea6\u8d8a\u5feb\u3001\u5bb9\u91cf\u8d8a\u5c0f\u3001\u6210\u672c\u8d8a\u9ad8\u3002\u8fd9\u79cd\u591a\u5c42\u7ea7\u7684\u8bbe\u8ba1\u5e76\u975e\u5076\u7136\uff0c\u800c\u662f\u8ba1\u7b97\u673a\u79d1\u5b66\u5bb6\u548c\u5de5\u7a0b\u5e08\u4eec\u7ecf\u8fc7\u6df1\u601d\u719f\u8651\u7684\u7ed3\u679c\u3002

        • \u786c\u76d8\u96be\u4ee5\u88ab\u5185\u5b58\u53d6\u4ee3\u3002\u9996\u5148\uff0c\u5185\u5b58\u4e2d\u7684\u6570\u636e\u5728\u65ad\u7535\u540e\u4f1a\u4e22\u5931\uff0c\u56e0\u6b64\u5b83\u4e0d\u9002\u5408\u957f\u671f\u5b58\u50a8\u6570\u636e\uff1b\u5176\u6b21\uff0c\u5185\u5b58\u7684\u6210\u672c\u662f\u786c\u76d8\u7684\u51e0\u5341\u500d\uff0c\u8fd9\u4f7f\u5f97\u5b83\u96be\u4ee5\u5728\u6d88\u8d39\u8005\u5e02\u573a\u666e\u53ca\u3002
        • \u7f13\u5b58\u7684\u5927\u5bb9\u91cf\u548c\u9ad8\u901f\u5ea6\u96be\u4ee5\u517c\u5f97\u3002\u968f\u7740 L1\u3001L2\u3001L3 \u7f13\u5b58\u7684\u5bb9\u91cf\u9010\u6b65\u589e\u5927\uff0c\u5176\u7269\u7406\u5c3a\u5bf8\u4f1a\u53d8\u5927\uff0c\u4e0e CPU \u6838\u5fc3\u4e4b\u95f4\u7684\u7269\u7406\u8ddd\u79bb\u4f1a\u53d8\u8fdc\uff0c\u4ece\u800c\u5bfc\u81f4\u6570\u636e\u4f20\u8f93\u65f6\u95f4\u589e\u52a0\uff0c\u5143\u7d20\u8bbf\u95ee\u5ef6\u8fdf\u53d8\u9ad8\u3002\u5728\u5f53\u524d\u6280\u672f\u4e0b\uff0c\u591a\u5c42\u7ea7\u7684\u7f13\u5b58\u7ed3\u6784\u662f\u5bb9\u91cf\u3001\u901f\u5ea6\u548c\u6210\u672c\u4e4b\u95f4\u7684\u6700\u4f73\u5e73\u8861\u70b9\u3002

        \u56fe 4-9 \u00a0 \u8ba1\u7b97\u673a\u5b58\u50a8\u7cfb\u7edf

        Tip

        \u8ba1\u7b97\u673a\u7684\u5b58\u50a8\u5c42\u6b21\u7ed3\u6784\u4f53\u73b0\u4e86\u901f\u5ea6\u3001\u5bb9\u91cf\u548c\u6210\u672c\u4e09\u8005\u4e4b\u95f4\u7684\u7cbe\u5999\u5e73\u8861\u3002\u5b9e\u9645\u4e0a\uff0c\u8fd9\u79cd\u6743\u8861\u666e\u904d\u5b58\u5728\u4e8e\u6240\u6709\u5de5\u4e1a\u9886\u57df\uff0c\u5b83\u8981\u6c42\u6211\u4eec\u5728\u4e0d\u540c\u7684\u4f18\u52bf\u548c\u9650\u5236\u4e4b\u95f4\u627e\u5230\u6700\u4f73\u5e73\u8861\u70b9\u3002

        \u603b\u7684\u6765\u8bf4\uff0c\u786c\u76d8\u7528\u4e8e\u957f\u671f\u5b58\u50a8\u5927\u91cf\u6570\u636e\uff0c\u5185\u5b58\u7528\u4e8e\u4e34\u65f6\u5b58\u50a8\u7a0b\u5e8f\u8fd0\u884c\u4e2d\u6b63\u5728\u5904\u7406\u7684\u6570\u636e\uff0c\u800c\u7f13\u5b58\u5219\u7528\u4e8e\u5b58\u50a8\u7ecf\u5e38\u8bbf\u95ee\u7684\u6570\u636e\u548c\u6307\u4ee4\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u8fd0\u884c\u6548\u7387\u3002\u4e09\u8005\u5171\u540c\u534f\u4f5c\uff0c\u786e\u4fdd\u8ba1\u7b97\u673a\u7cfb\u7edf\u9ad8\u6548\u8fd0\u884c\u3002

        \u5982\u56fe 4-10 \u6240\u793a\uff0c\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u6570\u636e\u4f1a\u4ece\u786c\u76d8\u4e2d\u88ab\u8bfb\u53d6\u5230\u5185\u5b58\u4e2d\uff0c\u4f9b CPU \u8ba1\u7b97\u4f7f\u7528\u3002\u7f13\u5b58\u53ef\u4ee5\u770b\u4f5c CPU \u7684\u4e00\u90e8\u5206\uff0c\u5b83\u901a\u8fc7\u667a\u80fd\u5730\u4ece\u5185\u5b58\u52a0\u8f7d\u6570\u636e\uff0c\u7ed9 CPU \u63d0\u4f9b\u9ad8\u901f\u7684\u6570\u636e\u8bfb\u53d6\uff0c\u4ece\u800c\u663e\u8457\u63d0\u5347\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\uff0c\u51cf\u5c11\u5bf9\u8f83\u6162\u7684\u5185\u5b58\u7684\u4f9d\u8d56\u3002

        \u56fe 4-10 \u00a0 \u786c\u76d8\u3001\u5185\u5b58\u548c\u7f13\u5b58\u4e4b\u95f4\u7684\u6570\u636e\u6d41\u901a

        "},{"location":"chapter_array_and_linkedlist/ram_and_cache/#442","title":"4.4.2 \u00a0 \u6570\u636e\u7ed3\u6784\u7684\u5185\u5b58\u6548\u7387","text":"

        \u5728\u5185\u5b58\u7a7a\u95f4\u5229\u7528\u65b9\u9762\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u5404\u81ea\u5177\u6709\u4f18\u52bf\u548c\u5c40\u9650\u6027\u3002

        \u4e00\u65b9\u9762\uff0c\u5185\u5b58\u662f\u6709\u9650\u7684\uff0c\u4e14\u540c\u4e00\u5757\u5185\u5b58\u4e0d\u80fd\u88ab\u591a\u4e2a\u7a0b\u5e8f\u5171\u4eab\uff0c\u56e0\u6b64\u6211\u4eec\u5e0c\u671b\u6570\u636e\u7ed3\u6784\u80fd\u591f\u5c3d\u53ef\u80fd\u9ad8\u6548\u5730\u5229\u7528\u7a7a\u95f4\u3002\u6570\u7ec4\u7684\u5143\u7d20\u7d27\u5bc6\u6392\u5217\uff0c\u4e0d\u9700\u8981\u989d\u5916\u7684\u7a7a\u95f4\u6765\u5b58\u50a8\u94fe\u8868\u8282\u70b9\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\uff0c\u56e0\u6b64\u7a7a\u95f4\u6548\u7387\u66f4\u9ad8\u3002\u7136\u800c\uff0c\u6570\u7ec4\u9700\u8981\u4e00\u6b21\u6027\u5206\u914d\u8db3\u591f\u7684\u8fde\u7eed\u5185\u5b58\u7a7a\u95f4\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6d6a\u8d39\uff0c\u6570\u7ec4\u6269\u5bb9\u4e5f\u9700\u8981\u989d\u5916\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u6210\u672c\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u94fe\u8868\u4ee5\u201c\u8282\u70b9\u201d\u4e3a\u5355\u4f4d\u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d\u548c\u56de\u6536\uff0c\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002

        \u53e6\u4e00\u65b9\u9762\uff0c\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u968f\u7740\u53cd\u590d\u7533\u8bf7\u4e0e\u91ca\u653e\u5185\u5b58\uff0c\u7a7a\u95f2\u5185\u5b58\u7684\u788e\u7247\u5316\u7a0b\u5ea6\u4f1a\u8d8a\u6765\u8d8a\u9ad8\uff0c\u4ece\u800c\u5bfc\u81f4\u5185\u5b58\u7684\u5229\u7528\u6548\u7387\u964d\u4f4e\u3002\u6570\u7ec4\u7531\u4e8e\u5176\u8fde\u7eed\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u76f8\u5bf9\u4e0d\u5bb9\u6613\u5bfc\u81f4\u5185\u5b58\u788e\u7247\u5316\u3002\u76f8\u53cd\uff0c\u94fe\u8868\u7684\u5143\u7d20\u662f\u5206\u6563\u5b58\u50a8\u7684\uff0c\u5728\u9891\u7e41\u7684\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u4e2d\uff0c\u66f4\u5bb9\u6613\u5bfc\u81f4\u5185\u5b58\u788e\u7247\u5316\u3002

        "},{"location":"chapter_array_and_linkedlist/ram_and_cache/#443","title":"4.4.3 \u00a0 \u6570\u636e\u7ed3\u6784\u7684\u7f13\u5b58\u6548\u7387","text":"

        \u7f13\u5b58\u867d\u7136\u5728\u7a7a\u95f4\u5bb9\u91cf\u4e0a\u8fdc\u5c0f\u4e8e\u5185\u5b58\uff0c\u4f46\u5b83\u6bd4\u5185\u5b58\u5feb\u5f97\u591a\uff0c\u5728\u7a0b\u5e8f\u6267\u884c\u901f\u5ea6\u4e0a\u8d77\u7740\u81f3\u5173\u91cd\u8981\u7684\u4f5c\u7528\u3002\u7531\u4e8e\u7f13\u5b58\u7684\u5bb9\u91cf\u6709\u9650\uff0c\u53ea\u80fd\u5b58\u50a8\u4e00\u5c0f\u90e8\u5206\u9891\u7e41\u8bbf\u95ee\u7684\u6570\u636e\uff0c\u56e0\u6b64\u5f53 CPU \u5c1d\u8bd5\u8bbf\u95ee\u7684\u6570\u636e\u4e0d\u5728\u7f13\u5b58\u4e2d\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u7f13\u5b58\u672a\u547d\u4e2d\uff08cache miss\uff09\uff0c\u6b64\u65f6 CPU \u4e0d\u5f97\u4e0d\u4ece\u901f\u5ea6\u8f83\u6162\u7684\u5185\u5b58\u4e2d\u52a0\u8f7d\u6240\u9700\u6570\u636e\u3002

        \u663e\u7136\uff0c\u201c\u7f13\u5b58\u672a\u547d\u4e2d\u201d\u8d8a\u5c11\uff0cCPU \u8bfb\u5199\u6570\u636e\u7684\u6548\u7387\u5c31\u8d8a\u9ad8\uff0c\u7a0b\u5e8f\u6027\u80fd\u4e5f\u5c31\u8d8a\u597d\u3002\u6211\u4eec\u5c06 CPU \u4ece\u7f13\u5b58\u4e2d\u6210\u529f\u83b7\u53d6\u6570\u636e\u7684\u6bd4\u4f8b\u79f0\u4e3a\u7f13\u5b58\u547d\u4e2d\u7387\uff08cache hit rate\uff09\uff0c\u8fd9\u4e2a\u6307\u6807\u901a\u5e38\u7528\u6765\u8861\u91cf\u7f13\u5b58\u6548\u7387\u3002

        \u4e3a\u4e86\u5c3d\u53ef\u80fd\u8fbe\u5230\u66f4\u9ad8\u7684\u6548\u7387\uff0c\u7f13\u5b58\u4f1a\u91c7\u53d6\u4ee5\u4e0b\u6570\u636e\u52a0\u8f7d\u673a\u5236\u3002

        • \u7f13\u5b58\u884c\uff1a\u7f13\u5b58\u4e0d\u662f\u5355\u4e2a\u5b57\u8282\u5730\u5b58\u50a8\u4e0e\u52a0\u8f7d\u6570\u636e\uff0c\u800c\u662f\u4ee5\u7f13\u5b58\u884c\u4e3a\u5355\u4f4d\u3002\u76f8\u6bd4\u4e8e\u5355\u4e2a\u5b57\u8282\u7684\u4f20\u8f93\uff0c\u7f13\u5b58\u884c\u7684\u4f20\u8f93\u5f62\u5f0f\u66f4\u52a0\u9ad8\u6548\u3002
        • \u9884\u53d6\u673a\u5236\uff1a\u5904\u7406\u5668\u4f1a\u5c1d\u8bd5\u9884\u6d4b\u6570\u636e\u8bbf\u95ee\u6a21\u5f0f\uff08\u4f8b\u5982\u987a\u5e8f\u8bbf\u95ee\u3001\u56fa\u5b9a\u6b65\u957f\u8df3\u8dc3\u8bbf\u95ee\u7b49\uff09\uff0c\u5e76\u6839\u636e\u7279\u5b9a\u6a21\u5f0f\u5c06\u6570\u636e\u52a0\u8f7d\u81f3\u7f13\u5b58\u4e4b\u4e2d\uff0c\u4ece\u800c\u63d0\u5347\u547d\u4e2d\u7387\u3002
        • \u7a7a\u95f4\u5c40\u90e8\u6027\uff1a\u5982\u679c\u4e00\u4e2a\u6570\u636e\u88ab\u8bbf\u95ee\uff0c\u90a3\u4e48\u5b83\u9644\u8fd1\u7684\u6570\u636e\u53ef\u80fd\u8fd1\u671f\u4e5f\u4f1a\u88ab\u8bbf\u95ee\u3002\u56e0\u6b64\uff0c\u7f13\u5b58\u5728\u52a0\u8f7d\u67d0\u4e00\u6570\u636e\u65f6\uff0c\u4e5f\u4f1a\u52a0\u8f7d\u5176\u9644\u8fd1\u7684\u6570\u636e\uff0c\u4ee5\u63d0\u9ad8\u547d\u4e2d\u7387\u3002
        • \u65f6\u95f4\u5c40\u90e8\u6027\uff1a\u5982\u679c\u4e00\u4e2a\u6570\u636e\u88ab\u8bbf\u95ee\uff0c\u90a3\u4e48\u5b83\u5728\u4e0d\u4e45\u7684\u5c06\u6765\u5f88\u53ef\u80fd\u518d\u6b21\u88ab\u8bbf\u95ee\u3002\u7f13\u5b58\u5229\u7528\u8fd9\u4e00\u539f\u7406\uff0c\u901a\u8fc7\u4fdd\u7559\u6700\u8fd1\u8bbf\u95ee\u8fc7\u7684\u6570\u636e\u6765\u63d0\u9ad8\u547d\u4e2d\u7387\u3002

        \u5b9e\u9645\u4e0a\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u5bf9\u7f13\u5b58\u7684\u5229\u7528\u6548\u7387\u662f\u4e0d\u540c\u7684\uff0c\u4e3b\u8981\u4f53\u73b0\u5728\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u3002

        • \u5360\u7528\u7a7a\u95f4\uff1a\u94fe\u8868\u5143\u7d20\u6bd4\u6570\u7ec4\u5143\u7d20\u5360\u7528\u7a7a\u95f4\u66f4\u591a\uff0c\u5bfc\u81f4\u7f13\u5b58\u4e2d\u5bb9\u7eb3\u7684\u6709\u6548\u6570\u636e\u91cf\u66f4\u5c11\u3002
        • \u7f13\u5b58\u884c\uff1a\u94fe\u8868\u6570\u636e\u5206\u6563\u5728\u5185\u5b58\u5404\u5904\uff0c\u800c\u7f13\u5b58\u662f\u201c\u6309\u884c\u52a0\u8f7d\u201d\u7684\uff0c\u56e0\u6b64\u52a0\u8f7d\u5230\u65e0\u6548\u6570\u636e\u7684\u6bd4\u4f8b\u66f4\u9ad8\u3002
        • \u9884\u53d6\u673a\u5236\uff1a\u6570\u7ec4\u6bd4\u94fe\u8868\u7684\u6570\u636e\u8bbf\u95ee\u6a21\u5f0f\u66f4\u5177\u201c\u53ef\u9884\u6d4b\u6027\u201d\uff0c\u5373\u7cfb\u7edf\u66f4\u5bb9\u6613\u731c\u51fa\u5373\u5c06\u88ab\u52a0\u8f7d\u7684\u6570\u636e\u3002
        • \u7a7a\u95f4\u5c40\u90e8\u6027\uff1a\u6570\u7ec4\u88ab\u5b58\u50a8\u5728\u96c6\u4e2d\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\uff0c\u56e0\u6b64\u88ab\u52a0\u8f7d\u6570\u636e\u9644\u8fd1\u7684\u6570\u636e\u66f4\u6709\u53ef\u80fd\u5373\u5c06\u88ab\u8bbf\u95ee\u3002

        \u603b\u4f53\u800c\u8a00\uff0c\u6570\u7ec4\u5177\u6709\u66f4\u9ad8\u7684\u7f13\u5b58\u547d\u4e2d\u7387\uff0c\u56e0\u6b64\u5b83\u5728\u64cd\u4f5c\u6548\u7387\u4e0a\u901a\u5e38\u4f18\u4e8e\u94fe\u8868\u3002\u8fd9\u4f7f\u5f97\u5728\u89e3\u51b3\u7b97\u6cd5\u95ee\u9898\u65f6\uff0c\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u5f80\u5f80\u66f4\u53d7\u6b22\u8fce\u3002

        \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u9ad8\u7f13\u5b58\u6548\u7387\u5e76\u4e0d\u610f\u5473\u7740\u6570\u7ec4\u5728\u6240\u6709\u60c5\u51b5\u4e0b\u90fd\u4f18\u4e8e\u94fe\u8868\u3002\u5b9e\u9645\u5e94\u7528\u4e2d\u9009\u62e9\u54ea\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u6765\u51b3\u5b9a\u3002\u4f8b\u5982\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u90fd\u53ef\u4ee5\u5b9e\u73b0\u201c\u6808\u201d\u6570\u636e\u7ed3\u6784\uff08\u4e0b\u4e00\u7ae0\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\uff09\uff0c\u4f46\u5b83\u4eec\u9002\u7528\u4e8e\u4e0d\u540c\u573a\u666f\u3002

        • \u5728\u505a\u7b97\u6cd5\u9898\u65f6\uff0c\u6211\u4eec\u4f1a\u503e\u5411\u4e8e\u9009\u62e9\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u64cd\u4f5c\u6548\u7387\u548c\u968f\u673a\u8bbf\u95ee\u7684\u80fd\u529b\uff0c\u4ee3\u4ef7\u4ec5\u662f\u9700\u8981\u9884\u5148\u4e3a\u6570\u7ec4\u5206\u914d\u4e00\u5b9a\u7684\u5185\u5b58\u7a7a\u95f4\u3002
        • \u5982\u679c\u6570\u636e\u91cf\u975e\u5e38\u5927\u3001\u52a8\u6001\u6027\u5f88\u9ad8\u3001\u6808\u7684\u9884\u671f\u5927\u5c0f\u96be\u4ee5\u4f30\u8ba1\uff0c\u90a3\u4e48\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\u66f4\u52a0\u5408\u9002\u3002\u94fe\u8868\u80fd\u591f\u5c06\u5927\u91cf\u6570\u636e\u5206\u6563\u5b58\u50a8\u4e8e\u5185\u5b58\u7684\u4e0d\u540c\u90e8\u5206\uff0c\u5e76\u4e14\u907f\u514d\u4e86\u6570\u7ec4\u6269\u5bb9\u4ea7\u751f\u7684\u989d\u5916\u5f00\u9500\u3002
        "},{"location":"chapter_array_and_linkedlist/summary/","title":"4.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_array_and_linkedlist/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u6570\u7ec4\u548c\u94fe\u8868\u662f\u4e24\u79cd\u57fa\u672c\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5206\u522b\u4ee3\u8868\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u4e24\u79cd\u5b58\u50a8\u65b9\u5f0f\uff1a\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u548c\u5206\u6563\u7a7a\u95f4\u5b58\u50a8\u3002\u4e24\u8005\u7684\u7279\u70b9\u5448\u73b0\u51fa\u4e92\u8865\u7684\u7279\u6027\u3002
        • \u6570\u7ec4\u652f\u6301\u968f\u673a\u8bbf\u95ee\u3001\u5360\u7528\u5185\u5b58\u8f83\u5c11\uff1b\u4f46\u63d2\u5165\u548c\u5220\u9664\u5143\u7d20\u6548\u7387\u4f4e\uff0c\u4e14\u521d\u59cb\u5316\u540e\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002
        • \u94fe\u8868\u901a\u8fc7\u66f4\u6539\u5f15\u7528\uff08\u6307\u9488\uff09\u5b9e\u73b0\u9ad8\u6548\u7684\u8282\u70b9\u63d2\u5165\u4e0e\u5220\u9664\uff0c\u4e14\u53ef\u4ee5\u7075\u6d3b\u8c03\u6574\u957f\u5ea6\uff1b\u4f46\u8282\u70b9\u8bbf\u95ee\u6548\u7387\u4f4e\u3001\u5360\u7528\u5185\u5b58\u8f83\u591a\u3002\u5e38\u89c1\u7684\u94fe\u8868\u7c7b\u578b\u5305\u62ec\u5355\u5411\u94fe\u8868\u3001\u73af\u5f62\u94fe\u8868\u3001\u53cc\u5411\u94fe\u8868\u3002
        • \u5217\u8868\u662f\u4e00\u79cd\u652f\u6301\u589e\u5220\u67e5\u6539\u7684\u5143\u7d20\u6709\u5e8f\u96c6\u5408\uff0c\u901a\u5e38\u57fa\u4e8e\u52a8\u6001\u6570\u7ec4\u5b9e\u73b0\u3002\u5b83\u4fdd\u7559\u4e86\u6570\u7ec4\u7684\u4f18\u52bf\uff0c\u540c\u65f6\u53ef\u4ee5\u7075\u6d3b\u8c03\u6574\u957f\u5ea6\u3002
        • \u5217\u8868\u7684\u51fa\u73b0\u5927\u5e45\u63d0\u9ad8\u4e86\u6570\u7ec4\u7684\u5b9e\u7528\u6027\uff0c\u4f46\u53ef\u80fd\u5bfc\u81f4\u90e8\u5206\u5185\u5b58\u7a7a\u95f4\u6d6a\u8d39\u3002
        • \u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u6570\u636e\u4e3b\u8981\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u3002\u6570\u7ec4\u53ef\u63d0\u4f9b\u66f4\u9ad8\u7684\u5185\u5b58\u7a7a\u95f4\u6548\u7387\uff0c\u800c\u94fe\u8868\u5219\u5728\u5185\u5b58\u4f7f\u7528\u4e0a\u66f4\u52a0\u7075\u6d3b\u3002
        • \u7f13\u5b58\u901a\u8fc7\u7f13\u5b58\u884c\u3001\u9884\u53d6\u673a\u5236\u4ee5\u53ca\u7a7a\u95f4\u5c40\u90e8\u6027\u548c\u65f6\u95f4\u5c40\u90e8\u6027\u7b49\u6570\u636e\u52a0\u8f7d\u673a\u5236\uff0c\u4e3a CPU \u63d0\u4f9b\u5feb\u901f\u6570\u636e\u8bbf\u95ee\uff0c\u663e\u8457\u63d0\u5347\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002
        • \u7531\u4e8e\u6570\u7ec4\u5177\u6709\u66f4\u9ad8\u7684\u7f13\u5b58\u547d\u4e2d\u7387\uff0c\u56e0\u6b64\u5b83\u901a\u5e38\u6bd4\u94fe\u8868\u66f4\u9ad8\u6548\u3002\u5728\u9009\u62e9\u6570\u636e\u7ed3\u6784\u65f6\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u573a\u666f\u505a\u51fa\u6070\u5f53\u9009\u62e9\u3002
        "},{"location":"chapter_array_and_linkedlist/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u6570\u7ec4\u5b58\u50a8\u5728\u6808\u4e0a\u548c\u5b58\u50a8\u5728\u5806\u4e0a\uff0c\u5bf9\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u662f\u5426\u6709\u5f71\u54cd\uff1f

        \u5b58\u50a8\u5728\u6808\u4e0a\u548c\u5806\u4e0a\u7684\u6570\u7ec4\u90fd\u88ab\u5b58\u50a8\u5728\u8fde\u7eed\u5185\u5b58\u7a7a\u95f4\u5185\uff0c\u6570\u636e\u64cd\u4f5c\u6548\u7387\u57fa\u672c\u4e00\u81f4\u3002\u7136\u800c\uff0c\u6808\u548c\u5806\u5177\u6709\u5404\u81ea\u7684\u7279\u70b9\uff0c\u4ece\u800c\u5bfc\u81f4\u4ee5\u4e0b\u4e0d\u540c\u70b9\u3002

        1. \u5206\u914d\u548c\u91ca\u653e\u6548\u7387\uff1a\u6808\u662f\u4e00\u5757\u8f83\u5c0f\u7684\u5185\u5b58\uff0c\u5206\u914d\u7531\u7f16\u8bd1\u5668\u81ea\u52a8\u5b8c\u6210\uff1b\u800c\u5806\u5185\u5b58\u76f8\u5bf9\u66f4\u5927\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u52a8\u6001\u5206\u914d\uff0c\u66f4\u5bb9\u6613\u788e\u7247\u5316\u3002\u56e0\u6b64\uff0c\u5806\u4e0a\u7684\u5206\u914d\u548c\u91ca\u653e\u64cd\u4f5c\u901a\u5e38\u6bd4\u6808\u4e0a\u7684\u6162\u3002
        2. \u5927\u5c0f\u9650\u5236\uff1a\u6808\u5185\u5b58\u76f8\u5bf9\u8f83\u5c0f\uff0c\u5806\u7684\u5927\u5c0f\u4e00\u822c\u53d7\u9650\u4e8e\u53ef\u7528\u5185\u5b58\u3002\u56e0\u6b64\u5806\u66f4\u52a0\u9002\u5408\u5b58\u50a8\u5927\u578b\u6570\u7ec4\u3002
        3. \u7075\u6d3b\u6027\uff1a\u6808\u4e0a\u7684\u6570\u7ec4\u7684\u5927\u5c0f\u9700\u8981\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\uff0c\u800c\u5806\u4e0a\u7684\u6570\u7ec4\u7684\u5927\u5c0f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u786e\u5b9a\u3002

        Q\uff1a\u4e3a\u4ec0\u4e48\u6570\u7ec4\u8981\u6c42\u76f8\u540c\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u800c\u5728\u94fe\u8868\u4e2d\u5374\u6ca1\u6709\u5f3a\u8c03\u76f8\u540c\u7c7b\u578b\u5462\uff1f

        \u94fe\u8868\u7531\u8282\u70b9\u7ec4\u6210\uff0c\u8282\u70b9\u4e4b\u95f4\u901a\u8fc7\u5f15\u7528\uff08\u6307\u9488\uff09\u8fde\u63a5\uff0c\u5404\u4e2a\u8282\u70b9\u53ef\u4ee5\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u4f8b\u5982 int\u3001double\u3001string\u3001object \u7b49\u3002

        \u76f8\u5bf9\u5730\uff0c\u6570\u7ec4\u5143\u7d20\u5219\u5fc5\u987b\u662f\u76f8\u540c\u7c7b\u578b\u7684\uff0c\u8fd9\u6837\u624d\u80fd\u901a\u8fc7\u8ba1\u7b97\u504f\u79fb\u91cf\u6765\u83b7\u53d6\u5bf9\u5e94\u5143\u7d20\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c\u6570\u7ec4\u540c\u65f6\u5305\u542b int \u548c long \u4e24\u79cd\u7c7b\u578b\uff0c\u5355\u4e2a\u5143\u7d20\u5206\u522b\u5360\u7528 4 \u5b57\u8282\u548c 8 \u5b57\u8282 \uff0c\u6b64\u65f6\u5c31\u4e0d\u80fd\u7528\u4ee5\u4e0b\u516c\u5f0f\u8ba1\u7b97\u504f\u79fb\u91cf\u4e86\uff0c\u56e0\u4e3a\u6570\u7ec4\u4e2d\u5305\u542b\u4e86\u4e24\u79cd\u201c\u5143\u7d20\u957f\u5ea6\u201d\u3002

        # \u5143\u7d20\u5185\u5b58\u5730\u5740 = \u6570\u7ec4\u5185\u5b58\u5730\u5740\uff08\u9996\u5143\u7d20\u5185\u5b58\u5730\u5740\uff09 + \u5143\u7d20\u957f\u5ea6 * \u5143\u7d20\u7d22\u5f15\n

        Q\uff1a\u5220\u9664\u8282\u70b9 P \u540e\uff0c\u662f\u5426\u9700\u8981\u628a P.next \u8bbe\u4e3a None \u5462\uff1f

        \u4e0d\u4fee\u6539 P.next \u4e5f\u53ef\u4ee5\u3002\u4ece\u8be5\u94fe\u8868\u7684\u89d2\u5ea6\u770b\uff0c\u4ece\u5934\u8282\u70b9\u904d\u5386\u5230\u5c3e\u8282\u70b9\u5df2\u7ecf\u4e0d\u4f1a\u9047\u5230 P \u4e86\u3002\u8fd9\u610f\u5473\u7740\u8282\u70b9 P \u5df2\u7ecf\u4ece\u94fe\u8868\u4e2d\u5220\u9664\u4e86\uff0c\u6b64\u65f6\u8282\u70b9 P \u6307\u5411\u54ea\u91cc\u90fd\u4e0d\u4f1a\u5bf9\u8be5\u94fe\u8868\u4ea7\u751f\u5f71\u54cd\u3002

        \u4ece\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\uff08\u505a\u9898\uff09\u7684\u89d2\u5ea6\u770b\uff0c\u4e0d\u65ad\u5f00\u6ca1\u6709\u5173\u7cfb\uff0c\u53ea\u8981\u4fdd\u8bc1\u7a0b\u5e8f\u7684\u903b\u8f91\u662f\u6b63\u786e\u7684\u5c31\u884c\u3002\u4ece\u6807\u51c6\u5e93\u7684\u89d2\u5ea6\u770b\uff0c\u65ad\u5f00\u66f4\u52a0\u5b89\u5168\u3001\u903b\u8f91\u66f4\u52a0\u6e05\u6670\u3002\u5982\u679c\u4e0d\u65ad\u5f00\uff0c\u5047\u8bbe\u88ab\u5220\u9664\u8282\u70b9\u672a\u88ab\u6b63\u5e38\u56de\u6536\uff0c\u90a3\u4e48\u5b83\u4f1a\u5f71\u54cd\u540e\u7ee7\u8282\u70b9\u7684\u5185\u5b58\u56de\u6536\u3002

        Q\uff1a\u5728\u94fe\u8868\u4e2d\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u3002\u4f46\u662f\u589e\u5220\u4e4b\u524d\u90fd\u9700\u8981 \\(O(n)\\) \u7684\u65f6\u95f4\u67e5\u627e\u5143\u7d20\uff0c\u90a3\u4e3a\u4ec0\u4e48\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u662f \\(O(n)\\) \u5462\uff1f

        \u5982\u679c\u662f\u5148\u67e5\u627e\u5143\u7d20\u3001\u518d\u5220\u9664\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u786e\u5b9e\u662f \\(O(n)\\) \u3002\u7136\u800c\uff0c\u94fe\u8868\u7684 \\(O(1)\\) \u589e\u5220\u7684\u4f18\u52bf\u53ef\u4ee5\u5728\u5176\u4ed6\u5e94\u7528\u4e0a\u5f97\u5230\u4f53\u73b0\u3002\u4f8b\u5982\uff0c\u53cc\u5411\u961f\u5217\u9002\u5408\u4f7f\u7528\u94fe\u8868\u5b9e\u73b0\uff0c\u6211\u4eec\u7ef4\u62a4\u4e00\u4e2a\u6307\u9488\u53d8\u91cf\u59cb\u7ec8\u6307\u5411\u5934\u8282\u70b9\u3001\u5c3e\u8282\u70b9\uff0c\u6bcf\u6b21\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u90fd\u662f \\(O(1)\\) \u3002

        Q\uff1a\u56fe\u201c\u94fe\u8868\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f\u201d\u4e2d\uff0c\u6d45\u84dd\u8272\u7684\u5b58\u50a8\u8282\u70b9\u6307\u9488\u662f\u5360\u7528\u4e00\u5757\u5185\u5b58\u5730\u5740\u5417\uff1f\u8fd8\u662f\u548c\u8282\u70b9\u503c\u5404\u5360\u4e00\u534a\u5462\uff1f

        \u8be5\u793a\u610f\u56fe\u53ea\u662f\u5b9a\u6027\u8868\u793a\uff0c\u5b9a\u91cf\u8868\u793a\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u5206\u6790\u3002

        • \u4e0d\u540c\u7c7b\u578b\u7684\u8282\u70b9\u503c\u5360\u7528\u7684\u7a7a\u95f4\u662f\u4e0d\u540c\u7684\uff0c\u6bd4\u5982 int\u3001long\u3001double \u548c\u5b9e\u4f8b\u5bf9\u8c61\u7b49\u3002
        • \u6307\u9488\u53d8\u91cf\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u5927\u5c0f\u6839\u636e\u6240\u4f7f\u7528\u7684\u64cd\u4f5c\u7cfb\u7edf\u53ca\u7f16\u8bd1\u73af\u5883\u800c\u5b9a\uff0c\u5927\u591a\u4e3a 8 \u5b57\u8282\u6216 4 \u5b57\u8282\u3002

        Q\uff1a\u5728\u5217\u8868\u672b\u5c3e\u6dfb\u52a0\u5143\u7d20\u662f\u5426\u65f6\u65f6\u523b\u523b\u90fd\u4e3a \\(O(1)\\) \uff1f

        \u5982\u679c\u6dfb\u52a0\u5143\u7d20\u65f6\u8d85\u51fa\u5217\u8868\u957f\u5ea6\uff0c\u5219\u9700\u8981\u5148\u6269\u5bb9\u5217\u8868\u518d\u6dfb\u52a0\u3002\u7cfb\u7edf\u4f1a\u7533\u8bf7\u4e00\u5757\u65b0\u7684\u5185\u5b58\uff0c\u5e76\u5c06\u539f\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\u642c\u8fd0\u8fc7\u53bb\uff0c\u8fd9\u65f6\u5019\u65f6\u95f4\u590d\u6742\u5ea6\u5c31\u4f1a\u662f \\(O(n)\\) \u3002

        Q\uff1a\u201c\u5217\u8868\u7684\u51fa\u73b0\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u6570\u7ec4\u7684\u5b9e\u7528\u6027\uff0c\u4f46\u53ef\u80fd\u5bfc\u81f4\u90e8\u5206\u5185\u5b58\u7a7a\u95f4\u6d6a\u8d39\u201d\uff0c\u8fd9\u91cc\u7684\u7a7a\u95f4\u6d6a\u8d39\u662f\u6307\u989d\u5916\u589e\u52a0\u7684\u53d8\u91cf\u5982\u5bb9\u91cf\u3001\u957f\u5ea6\u3001\u6269\u5bb9\u500d\u6570\u6240\u5360\u7684\u5185\u5b58\u5417\uff1f

        \u8fd9\u91cc\u7684\u7a7a\u95f4\u6d6a\u8d39\u4e3b\u8981\u6709\u4e24\u65b9\u9762\u542b\u4e49\uff1a\u4e00\u65b9\u9762\uff0c\u5217\u8868\u90fd\u4f1a\u8bbe\u5b9a\u4e00\u4e2a\u521d\u59cb\u957f\u5ea6\uff0c\u6211\u4eec\u4e0d\u4e00\u5b9a\u9700\u8981\u7528\u8fd9\u4e48\u591a\uff1b\u53e6\u4e00\u65b9\u9762\uff0c\u4e3a\u4e86\u9632\u6b62\u9891\u7e41\u6269\u5bb9\uff0c\u6269\u5bb9\u4e00\u822c\u4f1a\u4e58\u4ee5\u4e00\u4e2a\u7cfb\u6570\uff0c\u6bd4\u5982 \\(\\times 1.5\\) \u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4e5f\u4f1a\u51fa\u73b0\u5f88\u591a\u7a7a\u4f4d\uff0c\u6211\u4eec\u901a\u5e38\u4e0d\u80fd\u5b8c\u5168\u586b\u6ee1\u5b83\u4eec\u3002

        Q\uff1a\u5728 Python \u4e2d\u521d\u59cb\u5316 n = [1, 2, 3] \u540e\uff0c\u8fd9 3 \u4e2a\u5143\u7d20\u7684\u5730\u5740\u662f\u76f8\u8fde\u7684\uff0c\u4f46\u662f\u521d\u59cb\u5316 m = [2, 1, 3] \u4f1a\u53d1\u73b0\u5b83\u4eec\u6bcf\u4e2a\u5143\u7d20\u7684 id \u5e76\u4e0d\u662f\u8fde\u7eed\u7684\uff0c\u800c\u662f\u5206\u522b\u8ddf n \u4e2d\u7684\u76f8\u540c\u3002\u8fd9\u4e9b\u5143\u7d20\u7684\u5730\u5740\u4e0d\u8fde\u7eed\uff0c\u90a3\u4e48 m \u8fd8\u662f\u6570\u7ec4\u5417\uff1f

        \u5047\u5982\u628a\u5217\u8868\u5143\u7d20\u6362\u6210\u94fe\u8868\u8282\u70b9 n = [n1, n2, n3, n4, n5] \uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u8fd9 5 \u4e2a\u8282\u70b9\u5bf9\u8c61\u4e5f\u5206\u6563\u5b58\u50a8\u5728\u5185\u5b58\u5404\u5904\u3002\u7136\u800c\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u5217\u8868\u7d22\u5f15\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u83b7\u53d6\u8282\u70b9\u5185\u5b58\u5730\u5740\uff0c\u4ece\u800c\u8bbf\u95ee\u5230\u5bf9\u5e94\u7684\u8282\u70b9\u3002\u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u4e2d\u5b58\u50a8\u7684\u662f\u8282\u70b9\u7684\u5f15\u7528\uff0c\u800c\u975e\u8282\u70b9\u672c\u8eab\u3002

        \u4e0e\u8bb8\u591a\u8bed\u8a00\u4e0d\u540c\uff0cPython \u4e2d\u7684\u6570\u5b57\u4e5f\u88ab\u5305\u88c5\u4e3a\u5bf9\u8c61\uff0c\u5217\u8868\u4e2d\u5b58\u50a8\u7684\u4e0d\u662f\u6570\u5b57\u672c\u8eab\uff0c\u800c\u662f\u5bf9\u6570\u5b57\u7684\u5f15\u7528\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4f1a\u53d1\u73b0\u4e24\u4e2a\u6570\u7ec4\u4e2d\u7684\u76f8\u540c\u6570\u5b57\u62e5\u6709\u540c\u4e00\u4e2a id \uff0c\u5e76\u4e14\u8fd9\u4e9b\u6570\u5b57\u7684\u5185\u5b58\u5730\u5740\u65e0\u987b\u8fde\u7eed\u3002

        Q\uff1aC++ STL \u91cc\u9762\u7684 std::list \u5df2\u7ecf\u5b9e\u73b0\u4e86\u53cc\u5411\u94fe\u8868\uff0c\u4f46\u597d\u50cf\u4e00\u4e9b\u7b97\u6cd5\u4e66\u4e0a\u4e0d\u600e\u4e48\u76f4\u63a5\u4f7f\u7528\u5b83\uff0c\u662f\u4e0d\u662f\u56e0\u4e3a\u6709\u4ec0\u4e48\u5c40\u9650\u6027\u5462\uff1f

        \u4e00\u65b9\u9762\uff0c\u6211\u4eec\u5f80\u5f80\u66f4\u9752\u7750\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u7b97\u6cd5\uff0c\u800c\u53ea\u5728\u5fc5\u8981\u65f6\u624d\u4f7f\u7528\u94fe\u8868\uff0c\u4e3b\u8981\u6709\u4e24\u4e2a\u539f\u56e0\u3002

        • \u7a7a\u95f4\u5f00\u9500\uff1a\u7531\u4e8e\u6bcf\u4e2a\u5143\u7d20\u9700\u8981\u4e24\u4e2a\u989d\u5916\u7684\u6307\u9488\uff08\u4e00\u4e2a\u7528\u4e8e\u524d\u4e00\u4e2a\u5143\u7d20\uff0c\u4e00\u4e2a\u7528\u4e8e\u540e\u4e00\u4e2a\u5143\u7d20\uff09\uff0c\u6240\u4ee5 std::list \u901a\u5e38\u6bd4 std::vector \u66f4\u5360\u7528\u7a7a\u95f4\u3002
        • \u7f13\u5b58\u4e0d\u53cb\u597d\uff1a\u7531\u4e8e\u6570\u636e\u4e0d\u662f\u8fde\u7eed\u5b58\u653e\u7684\uff0c\u56e0\u6b64 std::list \u5bf9\u7f13\u5b58\u7684\u5229\u7528\u7387\u8f83\u4f4e\u3002\u4e00\u822c\u60c5\u51b5\u4e0b\uff0cstd::vector \u7684\u6027\u80fd\u4f1a\u66f4\u597d\u3002

        \u53e6\u4e00\u65b9\u9762\uff0c\u5fc5\u8981\u4f7f\u7528\u94fe\u8868\u7684\u60c5\u51b5\u4e3b\u8981\u662f\u4e8c\u53c9\u6811\u548c\u56fe\u3002\u6808\u548c\u961f\u5217\u5f80\u5f80\u4f1a\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u7684 stack \u548c queue \uff0c\u800c\u975e\u94fe\u8868\u3002

        Q\uff1a\u64cd\u4f5c res = [[0]] * n \u751f\u6210\u4e86\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\uff0c\u5176\u4e2d\u6bcf\u4e00\u4e2a [0] \u90fd\u662f\u72ec\u7acb\u7684\u5417\uff1f

        \u4e0d\u662f\u72ec\u7acb\u7684\u3002\u6b64\u4e8c\u7ef4\u5217\u8868\u4e2d\uff0c\u6240\u6709\u7684 [0] \u5b9e\u9645\u4e0a\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u5982\u679c\u6211\u4eec\u4fee\u6539\u5176\u4e2d\u4e00\u4e2a\u5143\u7d20\uff0c\u4f1a\u53d1\u73b0\u6240\u6709\u7684\u5bf9\u5e94\u5143\u7d20\u90fd\u4f1a\u968f\u4e4b\u6539\u53d8\u3002

        \u5982\u679c\u5e0c\u671b\u4e8c\u7ef4\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a [0] \u90fd\u662f\u72ec\u7acb\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528 res = [[0] for _ in range(n)] \u6765\u5b9e\u73b0\u3002\u8fd9\u79cd\u65b9\u5f0f\u7684\u539f\u7406\u662f\u521d\u59cb\u5316\u4e86 \\(n\\) \u4e2a\u72ec\u7acb\u7684 [0] \u5217\u8868\u5bf9\u8c61\u3002

        Q\uff1a\u64cd\u4f5c res = [0] * n \u751f\u6210\u4e86\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u6bcf\u4e00\u4e2a\u6574\u6570 0 \u90fd\u662f\u72ec\u7acb\u7684\u5417\uff1f

        \u5728\u8be5\u5217\u8868\u4e2d\uff0c\u6240\u6709\u6574\u6570 0 \u90fd\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u8fd9\u662f\u56e0\u4e3a Python \u5bf9\u5c0f\u6574\u6570\uff08\u901a\u5e38\u662f -5 \u5230 256\uff09\u91c7\u7528\u4e86\u7f13\u5b58\u6c60\u673a\u5236\uff0c\u4ee5\u4fbf\u6700\u5927\u5316\u5bf9\u8c61\u590d\u7528\uff0c\u4ece\u800c\u63d0\u5347\u6027\u80fd\u3002

        \u867d\u7136\u5b83\u4eec\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4f46\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u72ec\u7acb\u4fee\u6539\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u8fd9\u662f\u56e0\u4e3a Python \u7684\u6574\u6570\u662f\u201c\u4e0d\u53ef\u53d8\u5bf9\u8c61\u201d\u3002\u5f53\u6211\u4eec\u4fee\u6539\u67d0\u4e2a\u5143\u7d20\u65f6\uff0c\u5b9e\u9645\u4e0a\u662f\u5207\u6362\u4e3a\u53e6\u4e00\u4e2a\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u800c\u4e0d\u662f\u6539\u53d8\u539f\u6709\u5bf9\u8c61\u672c\u8eab\u3002

        \u7136\u800c\uff0c\u5f53\u5217\u8868\u5143\u7d20\u662f\u201c\u53ef\u53d8\u5bf9\u8c61\u201d\u65f6\uff08\u4f8b\u5982\u5217\u8868\u3001\u5b57\u5178\u6216\u7c7b\u5b9e\u4f8b\u7b49\uff09\uff0c\u4fee\u6539\u67d0\u4e2a\u5143\u7d20\u4f1a\u76f4\u63a5\u6539\u53d8\u8be5\u5bf9\u8c61\u672c\u8eab\uff0c\u6240\u6709\u5f15\u7528\u8be5\u5bf9\u8c61\u7684\u5143\u7d20\u90fd\u4f1a\u4ea7\u751f\u76f8\u540c\u53d8\u5316\u3002

        "},{"location":"chapter_backtracking/","title":"\u7b2c 13 \u7ae0 \u00a0 \u56de\u6eaf","text":"

        Abstract

        \u6211\u4eec\u5982\u540c\u8ff7\u5bab\u4e2d\u7684\u63a2\u7d22\u8005\uff0c\u5728\u524d\u8fdb\u7684\u9053\u8def\u4e0a\u53ef\u80fd\u4f1a\u9047\u5230\u56f0\u96be\u3002

        \u56de\u6eaf\u7684\u529b\u91cf\u8ba9\u6211\u4eec\u80fd\u591f\u91cd\u65b0\u5f00\u59cb\uff0c\u4e0d\u65ad\u5c1d\u8bd5\uff0c\u6700\u7ec8\u627e\u5230\u901a\u5f80\u5149\u660e\u7684\u51fa\u53e3\u3002

        "},{"location":"chapter_backtracking/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 13.1 \u00a0 \u56de\u6eaf\u7b97\u6cd5
        • 13.2 \u00a0 \u5168\u6392\u5217\u95ee\u9898
        • 13.3 \u00a0 \u5b50\u96c6\u548c\u95ee\u9898
        • 13.4 \u00a0 N \u7687\u540e\u95ee\u9898
        • 13.5 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_backtracking/backtracking_algorithm/","title":"13.1 \u00a0 \u56de\u6eaf\u7b97\u6cd5","text":"

        \u56de\u6eaf\u7b97\u6cd5\uff08backtracking algorithm\uff09\u662f\u4e00\u79cd\u901a\u8fc7\u7a77\u4e3e\u6765\u89e3\u51b3\u95ee\u9898\u7684\u65b9\u6cd5\uff0c\u5b83\u7684\u6838\u5fc3\u601d\u60f3\u662f\u4ece\u4e00\u4e2a\u521d\u59cb\u72b6\u6001\u51fa\u53d1\uff0c\u66b4\u529b\u641c\u7d22\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5f53\u9047\u5230\u6b63\u786e\u7684\u89e3\u5219\u5c06\u5176\u8bb0\u5f55\uff0c\u76f4\u5230\u627e\u5230\u89e3\u6216\u8005\u5c1d\u8bd5\u4e86\u6240\u6709\u53ef\u80fd\u7684\u9009\u62e9\u90fd\u65e0\u6cd5\u627e\u5230\u89e3\u4e3a\u6b62\u3002

        \u56de\u6eaf\u7b97\u6cd5\u901a\u5e38\u91c7\u7528\u201c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u201d\u6765\u904d\u5386\u89e3\u7a7a\u95f4\u3002\u5728\u201c\u4e8c\u53c9\u6811\u201d\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u63d0\u5230\u524d\u5e8f\u3001\u4e2d\u5e8f\u548c\u540e\u5e8f\u904d\u5386\u90fd\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5229\u7528\u524d\u5e8f\u904d\u5386\u6784\u9020\u4e00\u4e2a\u56de\u6eaf\u95ee\u9898\uff0c\u9010\u6b65\u4e86\u89e3\u56de\u6eaf\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u3002

        \u4f8b\u9898\u4e00

        \u7ed9\u5b9a\u4e00\u68f5\u4e8c\u53c9\u6811\uff0c\u641c\u7d22\u5e76\u8bb0\u5f55\u6240\u6709\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\uff0c\u8bf7\u8fd4\u56de\u8282\u70b9\u5217\u8868\u3002

        \u5bf9\u4e8e\u6b64\u9898\uff0c\u6211\u4eec\u524d\u5e8f\u904d\u5386\u8fd9\u68f5\u6811\uff0c\u5e76\u5224\u65ad\u5f53\u524d\u8282\u70b9\u7684\u503c\u662f\u5426\u4e3a \\(7\\) \uff0c\u82e5\u662f\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u7684\u503c\u52a0\u5165\u7ed3\u679c\u5217\u8868 res \u4e4b\u4e2d\u3002\u76f8\u5173\u8fc7\u7a0b\u5b9e\u73b0\u5982\u56fe 13-1 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_i_compact.py
        def pre_order(root: TreeNode):\n    \"\"\"\u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00\"\"\"\n    if root is None:\n        return\n    if root.val == 7:\n        # \u8bb0\u5f55\u89e3\n        res.append(root)\n    pre_order(root.left)\n    pre_order(root.right)\n
        preorder_traversal_i_compact.cpp
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode *root) {\n    if (root == nullptr) {\n        return;\n    }\n    if (root->val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res.push_back(root);\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n}\n
        preorder_traversal_i_compact.java
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode root) {\n    if (root == null) {\n        return;\n    }\n    if (root.val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res.add(root);\n    }\n    preOrder(root.left);\n    preOrder(root.right);\n}\n
        preorder_traversal_i_compact.cs
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid PreOrder(TreeNode? root) {\n    if (root == null) {\n        return;\n    }\n    if (root.val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res.Add(root);\n    }\n    PreOrder(root.left);\n    PreOrder(root.right);\n}\n
        preorder_traversal_i_compact.go
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunc preOrderI(root *TreeNode, res *[]*TreeNode) {\n    if root == nil {\n        return\n    }\n    if (root.Val).(int) == 7 {\n        // \u8bb0\u5f55\u89e3\n        *res = append(*res, root)\n    }\n    preOrderI(root.Left, res)\n    preOrderI(root.Right, res)\n}\n
        preorder_traversal_i_compact.swift
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunc preOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    if root.val == 7 {\n        // \u8bb0\u5f55\u89e3\n        res.append(root)\n    }\n    preOrder(root: root.left)\n    preOrder(root: root.right)\n}\n
        preorder_traversal_i_compact.js
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunction preOrder(root, res) {\n    if (root === null) {\n        return;\n    }\n    if (root.val === 7) {\n        // \u8bb0\u5f55\u89e3\n        res.push(root);\n    }\n    preOrder(root.left, res);\n    preOrder(root.right, res);\n}\n
        preorder_traversal_i_compact.ts
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunction preOrder(root: TreeNode | null, res: TreeNode[]): void {\n    if (root === null) {\n        return;\n    }\n    if (root.val === 7) {\n        // \u8bb0\u5f55\u89e3\n        res.push(root);\n    }\n    preOrder(root.left, res);\n    preOrder(root.right, res);\n}\n
        preorder_traversal_i_compact.dart
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode? root, List<TreeNode> res) {\n  if (root == null) {\n    return;\n  }\n  if (root.val == 7) {\n    // \u8bb0\u5f55\u89e3\n    res.add(root);\n  }\n  preOrder(root.left, res);\n  preOrder(root.right, res);\n}\n
        preorder_traversal_i_compact.rs
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfn pre_order(res: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<&Rc<RefCell<TreeNode>>>) {\n    if root.is_none() {\n        return;\n    }\n    if let Some(node) = root {\n        if node.borrow().val == 7 {\n            // \u8bb0\u5f55\u89e3\n            res.push(node.clone());\n        }\n        pre_order(res, node.borrow().left.as_ref());\n        pre_order(res, node.borrow().right.as_ref());\n    }\n}\n
        preorder_traversal_i_compact.c
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode *root) {\n    if (root == NULL) {\n        return;\n    }\n    if (root->val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res[resSize++] = root;\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n}\n
        preorder_traversal_i_compact.kt
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfun preOrder(root: TreeNode?) {\n    if (root == null) {\n        return\n    }\n    if (root._val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res!!.add(root)\n    }\n    preOrder(root.left)\n    preOrder(root.right)\n}\n
        preorder_traversal_i_compact.rb
        ### \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 ###\ndef pre_order(root)\n  return unless root\n\n  # \u8bb0\u5f55\u89e3\n  $res << root if root.val == 7\n\n  pre_order(root.left)\n  pre_order(root.right)\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 13-1 \u00a0 \u5728\u524d\u5e8f\u904d\u5386\u4e2d\u641c\u7d22\u8282\u70b9

        "},{"location":"chapter_backtracking/backtracking_algorithm/#1311","title":"13.1.1 \u00a0 \u5c1d\u8bd5\u4e0e\u56de\u9000","text":"

        \u4e4b\u6240\u4ee5\u79f0\u4e4b\u4e3a\u56de\u6eaf\u7b97\u6cd5\uff0c\u662f\u56e0\u4e3a\u8be5\u7b97\u6cd5\u5728\u641c\u7d22\u89e3\u7a7a\u95f4\u65f6\u4f1a\u91c7\u7528\u201c\u5c1d\u8bd5\u201d\u4e0e\u201c\u56de\u9000\u201d\u7684\u7b56\u7565\u3002\u5f53\u7b97\u6cd5\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u9047\u5230\u67d0\u4e2a\u72b6\u6001\u65e0\u6cd5\u7ee7\u7eed\u524d\u8fdb\u6216\u65e0\u6cd5\u5f97\u5230\u6ee1\u8db3\u6761\u4ef6\u7684\u89e3\u65f6\uff0c\u5b83\u4f1a\u64a4\u9500\u4e0a\u4e00\u6b65\u7684\u9009\u62e9\uff0c\u9000\u56de\u5230\u4e4b\u524d\u7684\u72b6\u6001\uff0c\u5e76\u5c1d\u8bd5\u5176\u4ed6\u53ef\u80fd\u7684\u9009\u62e9\u3002

        \u5bf9\u4e8e\u4f8b\u9898\u4e00\uff0c\u8bbf\u95ee\u6bcf\u4e2a\u8282\u70b9\u90fd\u4ee3\u8868\u4e00\u6b21\u201c\u5c1d\u8bd5\u201d\uff0c\u800c\u8d8a\u8fc7\u53f6\u8282\u70b9\u6216\u8fd4\u56de\u7236\u8282\u70b9\u7684 return \u5219\u8868\u793a\u201c\u56de\u9000\u201d\u3002

        \u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u56de\u9000\u5e76\u4e0d\u4ec5\u4ec5\u5305\u62ec\u51fd\u6570\u8fd4\u56de\u3002\u4e3a\u89e3\u91ca\u8fd9\u4e00\u70b9\uff0c\u6211\u4eec\u5bf9\u4f8b\u9898\u4e00\u7a0d\u4f5c\u62d3\u5c55\u3002

        \u4f8b\u9898\u4e8c

        \u5728\u4e8c\u53c9\u6811\u4e2d\u641c\u7d22\u6240\u6709\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\uff0c\u8bf7\u8fd4\u56de\u6839\u8282\u70b9\u5230\u8fd9\u4e9b\u8282\u70b9\u7684\u8def\u5f84\u3002

        \u5728\u4f8b\u9898\u4e00\u4ee3\u7801\u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u5217\u8868 path \u8bb0\u5f55\u8bbf\u95ee\u8fc7\u7684\u8282\u70b9\u8def\u5f84\u3002\u5f53\u8bbf\u95ee\u5230\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\u65f6\uff0c\u5219\u590d\u5236 path \u5e76\u6dfb\u52a0\u8fdb\u7ed3\u679c\u5217\u8868 res \u3002\u904d\u5386\u5b8c\u6210\u540e\uff0cres \u4e2d\u4fdd\u5b58\u7684\u5c31\u662f\u6240\u6709\u7684\u89e3\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_ii_compact.py
        def pre_order(root: TreeNode):\n    \"\"\"\u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c\"\"\"\n    if root is None:\n        return\n    # \u5c1d\u8bd5\n    path.append(root)\n    if root.val == 7:\n        # \u8bb0\u5f55\u89e3\n        res.append(list(path))\n    pre_order(root.left)\n    pre_order(root.right)\n    # \u56de\u9000\n    path.pop()\n
        preorder_traversal_ii_compact.cpp
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode *root) {\n    if (root == nullptr) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path.push_back(root);\n    if (root->val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res.push_back(path);\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // \u56de\u9000\n    path.pop_back();\n}\n
        preorder_traversal_ii_compact.java
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode root) {\n    if (root == null) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path.add(root);\n    if (root.val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res.add(new ArrayList<>(path));\n    }\n    preOrder(root.left);\n    preOrder(root.right);\n    // \u56de\u9000\n    path.remove(path.size() - 1);\n}\n
        preorder_traversal_ii_compact.cs
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid PreOrder(TreeNode? root) {\n    if (root == null) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path.Add(root);\n    if (root.val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res.Add(new List<TreeNode>(path));\n    }\n    PreOrder(root.left);\n    PreOrder(root.right);\n    // \u56de\u9000\n    path.RemoveAt(path.Count - 1);\n}\n
        preorder_traversal_ii_compact.go
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunc preOrderII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {\n    if root == nil {\n        return\n    }\n    // \u5c1d\u8bd5\n    *path = append(*path, root)\n    if root.Val.(int) == 7 {\n        // \u8bb0\u5f55\u89e3\n        *res = append(*res, append([]*TreeNode{}, *path...))\n    }\n    preOrderII(root.Left, res, path)\n    preOrderII(root.Right, res, path)\n    // \u56de\u9000\n    *path = (*path)[:len(*path)-1]\n}\n
        preorder_traversal_ii_compact.swift
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunc preOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    // \u5c1d\u8bd5\n    path.append(root)\n    if root.val == 7 {\n        // \u8bb0\u5f55\u89e3\n        res.append(path)\n    }\n    preOrder(root: root.left)\n    preOrder(root: root.right)\n    // \u56de\u9000\n    path.removeLast()\n}\n
        preorder_traversal_ii_compact.js
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunction preOrder(root, path, res) {\n    if (root === null) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path.push(root);\n    if (root.val === 7) {\n        // \u8bb0\u5f55\u89e3\n        res.push([...path]);\n    }\n    preOrder(root.left, path, res);\n    preOrder(root.right, path, res);\n    // \u56de\u9000\n    path.pop();\n}\n
        preorder_traversal_ii_compact.ts
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunction preOrder(\n    root: TreeNode | null,\n    path: TreeNode[],\n    res: TreeNode[][]\n): void {\n    if (root === null) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path.push(root);\n    if (root.val === 7) {\n        // \u8bb0\u5f55\u89e3\n        res.push([...path]);\n    }\n    preOrder(root.left, path, res);\n    preOrder(root.right, path, res);\n    // \u56de\u9000\n    path.pop();\n}\n
        preorder_traversal_ii_compact.dart
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(\n  TreeNode? root,\n  List<TreeNode> path,\n  List<List<TreeNode>> res,\n) {\n  if (root == null) {\n    return;\n  }\n\n  // \u5c1d\u8bd5\n  path.add(root);\n  if (root.val == 7) {\n    // \u8bb0\u5f55\u89e3\n    res.add(List.from(path));\n  }\n  preOrder(root.left, path, res);\n  preOrder(root.right, path, res);\n  // \u56de\u9000\n  path.removeLast();\n}\n
        preorder_traversal_ii_compact.rs
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfn pre_order(\n    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,\n    path: &mut Vec<Rc<RefCell<TreeNode>>>,\n    root: Option<&Rc<RefCell<TreeNode>>>,\n) {\n    if root.is_none() {\n        return;\n    }\n    if let Some(node) = root {\n        // \u5c1d\u8bd5\n        path.push(node.clone());\n        if node.borrow().val == 7 {\n            // \u8bb0\u5f55\u89e3\n            res.push(path.clone());\n        }\n        pre_order(res, path, node.borrow().left.as_ref());\n        pre_order(res, path, node.borrow().right.as_ref());\n        // \u56de\u9000\n        path.pop();\n    }\n}\n
        preorder_traversal_ii_compact.c
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode *root) {\n    if (root == NULL) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path[pathSize++] = root;\n    if (root->val == 7) {\n        // \u8bb0\u5f55\u89e3\n        for (int i = 0; i < pathSize; ++i) {\n            res[resSize][i] = path[i];\n        }\n        resSize++;\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // \u56de\u9000\n    pathSize--;\n}\n
        preorder_traversal_ii_compact.kt
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfun preOrder(root: TreeNode?) {\n    if (root == null) {\n        return\n    }\n    // \u5c1d\u8bd5\n    path!!.add(root)\n    if (root._val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res!!.add(path!!.toMutableList())\n    }\n    preOrder(root.left)\n    preOrder(root.right)\n    // \u56de\u9000\n    path!!.removeAt(path!!.size - 1)\n}\n
        preorder_traversal_ii_compact.rb
        ### \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c ###\ndef pre_order(root)\n  return unless root\n\n  # \u5c1d\u8bd5\n  $path << root\n\n  # \u8bb0\u5f55\u89e3\n  $res << $path.dup if root.val == 7\n\n  pre_order(root.left)\n  pre_order(root.right)\n\n  # \u56de\u9000\n  $path.pop\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5728\u6bcf\u6b21\u201c\u5c1d\u8bd5\u201d\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5c06\u5f53\u524d\u8282\u70b9\u6dfb\u52a0\u8fdb path \u6765\u8bb0\u5f55\u8def\u5f84\uff1b\u800c\u5728\u201c\u56de\u9000\u201d\u524d\uff0c\u6211\u4eec\u9700\u8981\u5c06\u8be5\u8282\u70b9\u4ece path \u4e2d\u5f39\u51fa\uff0c\u4ee5\u6062\u590d\u672c\u6b21\u5c1d\u8bd5\u4e4b\u524d\u7684\u72b6\u6001\u3002

        \u89c2\u5bdf\u56fe 13-2 \u6240\u793a\u7684\u8fc7\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5c1d\u8bd5\u548c\u56de\u9000\u7406\u89e3\u4e3a\u201c\u524d\u8fdb\u201d\u4e0e\u201c\u64a4\u9500\u201d\uff0c\u4e24\u4e2a\u64cd\u4f5c\u4e92\u4e3a\u9006\u5411\u3002

        <1><2><3><4><5><6><7><8><9><10><11>

        \u56fe 13-2 \u00a0 \u5c1d\u8bd5\u4e0e\u56de\u9000

        "},{"location":"chapter_backtracking/backtracking_algorithm/#1312","title":"13.1.2 \u00a0 \u526a\u679d","text":"

        \u590d\u6742\u7684\u56de\u6eaf\u95ee\u9898\u901a\u5e38\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a\u7ea6\u675f\u6761\u4ef6\uff0c\u7ea6\u675f\u6761\u4ef6\u901a\u5e38\u53ef\u7528\u4e8e\u201c\u526a\u679d\u201d\u3002

        \u4f8b\u9898\u4e09

        \u5728\u4e8c\u53c9\u6811\u4e2d\u641c\u7d22\u6240\u6709\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\uff0c\u8bf7\u8fd4\u56de\u6839\u8282\u70b9\u5230\u8fd9\u4e9b\u8282\u70b9\u7684\u8def\u5f84\uff0c\u5e76\u8981\u6c42\u8def\u5f84\u4e2d\u4e0d\u5305\u542b\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u3002

        \u4e3a\u4e86\u6ee1\u8db3\u4ee5\u4e0a\u7ea6\u675f\u6761\u4ef6\uff0c\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u526a\u679d\u64cd\u4f5c\uff1a\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u82e5\u9047\u5230\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\uff0c\u5219\u63d0\u524d\u8fd4\u56de\uff0c\u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_iii_compact.py
        def pre_order(root: TreeNode):\n    \"\"\"\u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09\"\"\"\n    # \u526a\u679d\n    if root is None or root.val == 3:\n        return\n    # \u5c1d\u8bd5\n    path.append(root)\n    if root.val == 7:\n        # \u8bb0\u5f55\u89e3\n        res.append(list(path))\n    pre_order(root.left)\n    pre_order(root.right)\n    # \u56de\u9000\n    path.pop()\n
        preorder_traversal_iii_compact.cpp
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode *root) {\n    // \u526a\u679d\n    if (root == nullptr || root->val == 3) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path.push_back(root);\n    if (root->val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res.push_back(path);\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // \u56de\u9000\n    path.pop_back();\n}\n
        preorder_traversal_iii_compact.java
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode root) {\n    // \u526a\u679d\n    if (root == null || root.val == 3) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path.add(root);\n    if (root.val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res.add(new ArrayList<>(path));\n    }\n    preOrder(root.left);\n    preOrder(root.right);\n    // \u56de\u9000\n    path.remove(path.size() - 1);\n}\n
        preorder_traversal_iii_compact.cs
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid PreOrder(TreeNode? root) {\n    // \u526a\u679d\n    if (root == null || root.val == 3) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path.Add(root);\n    if (root.val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res.Add(new List<TreeNode>(path));\n    }\n    PreOrder(root.left);\n    PreOrder(root.right);\n    // \u56de\u9000\n    path.RemoveAt(path.Count - 1);\n}\n
        preorder_traversal_iii_compact.go
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunc preOrderIII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {\n    // \u526a\u679d\n    if root == nil || root.Val == 3 {\n        return\n    }\n    // \u5c1d\u8bd5\n    *path = append(*path, root)\n    if root.Val.(int) == 7 {\n        // \u8bb0\u5f55\u89e3\n        *res = append(*res, append([]*TreeNode{}, *path...))\n    }\n    preOrderIII(root.Left, res, path)\n    preOrderIII(root.Right, res, path)\n    // \u56de\u9000\n    *path = (*path)[:len(*path)-1]\n}\n
        preorder_traversal_iii_compact.swift
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunc preOrder(root: TreeNode?) {\n    // \u526a\u679d\n    guard let root = root, root.val != 3 else {\n        return\n    }\n    // \u5c1d\u8bd5\n    path.append(root)\n    if root.val == 7 {\n        // \u8bb0\u5f55\u89e3\n        res.append(path)\n    }\n    preOrder(root: root.left)\n    preOrder(root: root.right)\n    // \u56de\u9000\n    path.removeLast()\n}\n
        preorder_traversal_iii_compact.js
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunction preOrder(root, path, res) {\n    // \u526a\u679d\n    if (root === null || root.val === 3) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path.push(root);\n    if (root.val === 7) {\n        // \u8bb0\u5f55\u89e3\n        res.push([...path]);\n    }\n    preOrder(root.left, path, res);\n    preOrder(root.right, path, res);\n    // \u56de\u9000\n    path.pop();\n}\n
        preorder_traversal_iii_compact.ts
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunction preOrder(\n    root: TreeNode | null,\n    path: TreeNode[],\n    res: TreeNode[][]\n): void {\n    // \u526a\u679d\n    if (root === null || root.val === 3) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path.push(root);\n    if (root.val === 7) {\n        // \u8bb0\u5f55\u89e3\n        res.push([...path]);\n    }\n    preOrder(root.left, path, res);\n    preOrder(root.right, path, res);\n    // \u56de\u9000\n    path.pop();\n}\n
        preorder_traversal_iii_compact.dart
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(\n  TreeNode? root,\n  List<TreeNode> path,\n  List<List<TreeNode>> res,\n) {\n  if (root == null || root.val == 3) {\n    return;\n  }\n\n  // \u5c1d\u8bd5\n  path.add(root);\n  if (root.val == 7) {\n    // \u8bb0\u5f55\u89e3\n    res.add(List.from(path));\n  }\n  preOrder(root.left, path, res);\n  preOrder(root.right, path, res);\n  // \u56de\u9000\n  path.removeLast();\n}\n
        preorder_traversal_iii_compact.rs
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfn pre_order(\n    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,\n    path: &mut Vec<Rc<RefCell<TreeNode>>>,\n    root: Option<&Rc<RefCell<TreeNode>>>,\n) {\n    // \u526a\u679d\n    if root.is_none() || root.as_ref().unwrap().borrow().val == 3 {\n        return;\n    }\n    if let Some(node) = root {\n        // \u5c1d\u8bd5\n        path.push(node.clone());\n        if node.borrow().val == 7 {\n            // \u8bb0\u5f55\u89e3\n            res.push(path.clone());\n        }\n        pre_order(res, path, node.borrow().left.as_ref());\n        pre_order(res, path, node.borrow().right.as_ref());\n        // \u56de\u9000\n        path.pop();\n    }\n}\n
        preorder_traversal_iii_compact.c
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode *root) {\n    // \u526a\u679d\n    if (root == NULL || root->val == 3) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path[pathSize++] = root;\n    if (root->val == 7) {\n        // \u8bb0\u5f55\u89e3\n        for (int i = 0; i < pathSize; i++) {\n            res[resSize][i] = path[i];\n        }\n        resSize++;\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // \u56de\u9000\n    pathSize--;\n}\n
        preorder_traversal_iii_compact.kt
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfun preOrder(root: TreeNode?) {\n    // \u526a\u679d\n    if (root == null || root._val == 3) {\n        return\n    }\n    // \u5c1d\u8bd5\n    path!!.add(root)\n    if (root._val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res!!.add(path!!.toMutableList())\n    }\n    preOrder(root.left)\n    preOrder(root.right)\n    // \u56de\u9000\n    path!!.removeAt(path!!.size - 1)\n}\n
        preorder_traversal_iii_compact.rb
        ### \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 ###\ndef pre_order(root)\n  # \u526a\u679d\n  return if !root || root.val == 3\n\n  # \u5c1d\u8bd5\n  $path.append(root)\n\n  # \u8bb0\u5f55\u89e3\n  $res << $path.dup if root.val == 7\n\n  pre_order(root.left)\n  pre_order(root.right)\n\n  # \u56de\u9000\n  $path.pop\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u201c\u526a\u679d\u201d\u662f\u4e00\u4e2a\u975e\u5e38\u5f62\u8c61\u7684\u540d\u8bcd\u3002\u5982\u56fe 13-3 \u6240\u793a\uff0c\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u201c\u526a\u6389\u201d\u4e86\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u641c\u7d22\u5206\u652f\uff0c\u907f\u514d\u8bb8\u591a\u65e0\u610f\u4e49\u7684\u5c1d\u8bd5\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u641c\u7d22\u6548\u7387\u3002

        \u56fe 13-3 \u00a0 \u6839\u636e\u7ea6\u675f\u6761\u4ef6\u526a\u679d

        "},{"location":"chapter_backtracking/backtracking_algorithm/#1313","title":"13.1.3 \u00a0 \u6846\u67b6\u4ee3\u7801","text":"

        \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c1d\u8bd5\u5c06\u56de\u6eaf\u7684\u201c\u5c1d\u8bd5\u3001\u56de\u9000\u3001\u526a\u679d\u201d\u7684\u4e3b\u4f53\u6846\u67b6\u63d0\u70bc\u51fa\u6765\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u901a\u7528\u6027\u3002

        \u5728\u4ee5\u4e0b\u6846\u67b6\u4ee3\u7801\u4e2d\uff0cstate \u8868\u793a\u95ee\u9898\u7684\u5f53\u524d\u72b6\u6001\uff0cchoices \u8868\u793a\u5f53\u524d\u72b6\u6001\u4e0b\u53ef\u4ee5\u505a\u51fa\u7684\u9009\u62e9\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        def backtrack(state: State, choices: list[choice], res: list[state]):\n    \"\"\"\u56de\u6eaf\u7b97\u6cd5\u6846\u67b6\"\"\"\n    # \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if is_solution(state):\n        # \u8bb0\u5f55\u89e3\n        record_solution(state, res)\n        # \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n        return\n    # \u904d\u5386\u6240\u6709\u9009\u62e9\n    for choice in choices:\n        # \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if is_valid(state, choice):\n            # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            make_choice(state, choice)\n            backtrack(state, choices, res)\n            # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undo_choice(state, choice)\n
        /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State *state, vector<Choice *> &choices, vector<State *> &res) {\n    // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if (isSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res);\n        // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (Choice choice : choices) {\n        // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state, choice);\n        }\n    }\n}\n
        /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State state, List<Choice> choices, List<State> res) {\n    // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if (isSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res);\n        // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (Choice choice : choices) {\n        // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state, choice);\n        }\n    }\n}\n
        /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid Backtrack(State state, List<Choice> choices, List<State> res) {\n    // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if (IsSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        RecordSolution(state, res);\n        // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    foreach (Choice choice in choices) {\n        // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (IsValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            MakeChoice(state, choice);\n            Backtrack(state, choices, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            UndoChoice(state, choice);\n        }\n    }\n}\n
        /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunc backtrack(state *State, choices []Choice, res *[]State) {\n    // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if isSolution(state) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res)\n        // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for _, choice := range choices {\n        // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if isValid(state, choice) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice)\n            backtrack(state, choices, res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state, choice)\n        }\n    }\n}\n
        /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunc backtrack(state: inout State, choices: [Choice], res: inout [State]) {\n    // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if isSolution(state: state) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state: state, res: &res)\n        // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for choice in choices {\n        // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if isValid(state: state, choice: choice) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state: &state, choice: choice)\n            backtrack(state: &state, choices: choices, res: &res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state: &state, choice: choice)\n        }\n    }\n}\n
        /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunction backtrack(state, choices, res) {\n    // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if (isSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res);\n        // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (let choice of choices) {\n        // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state, choice);\n        }\n    }\n}\n
        /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunction backtrack(state: State, choices: Choice[], res: State[]): void {\n    // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if (isSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res);\n        // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (let choice of choices) {\n        // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state, choice);\n        }\n    }\n}\n
        /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State state, List<Choice>, List<State> res) {\n  // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n  if (isSolution(state)) {\n    // \u8bb0\u5f55\u89e3\n    recordSolution(state, res);\n    // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n    return;\n  }\n  // \u904d\u5386\u6240\u6709\u9009\u62e9\n  for (Choice choice in choices) {\n    // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n    if (isValid(state, choice)) {\n      // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n      makeChoice(state, choice);\n      backtrack(state, choices, res);\n      // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n      undoChoice(state, choice);\n    }\n  }\n}\n
        /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfn backtrack(state: &mut State, choices: &Vec<Choice>, res: &mut Vec<State>) {\n    // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if is_solution(state) {\n        // \u8bb0\u5f55\u89e3\n        record_solution(state, res);\n        // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for choice in choices {\n        // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if is_valid(state, choice) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            make_choice(state, choice);\n            backtrack(state, choices, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undo_choice(state, choice);\n        }\n    }\n}\n
        /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State *state, Choice *choices, int numChoices, State *res, int numRes) {\n    // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if (isSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res, numRes);\n        // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (int i = 0; i < numChoices; i++) {\n        // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(state, &choices[i])) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, &choices[i]);\n            backtrack(state, choices, numChoices, res, numRes);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state, &choices[i]);\n        }\n    }\n}\n
        /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfun backtrack(state: State?, choices: List<Choice?>, res: List<State?>?) {\n    // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if (isSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res)\n        // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (choice in choices) {\n        // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice)\n            backtrack(state, choices, res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state, choice)\n        }\n    }\n}\n
        ### \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 ###\ndef backtrack(state, choices, res)\n    # \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if is_solution?(state)\n        # \u8bb0\u5f55\u89e3\n        record_solution(state, res)\n        return\n    end\n\n    # \u904d\u5386\u6240\u6709\u9009\u62e9\n    for choice in choices\n        # \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if is_valid?(state, choice)\n            # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            make_choice(state, choice)\n            backtrack(state, choices, res)\n            # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undo_choice(state, choice)\n        end\n    end\nend\n

        \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u57fa\u4e8e\u6846\u67b6\u4ee3\u7801\u6765\u89e3\u51b3\u4f8b\u9898\u4e09\u3002\u72b6\u6001 state \u4e3a\u8282\u70b9\u904d\u5386\u8def\u5f84\uff0c\u9009\u62e9 choices \u4e3a\u5f53\u524d\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\uff0c\u7ed3\u679c res \u662f\u8def\u5f84\u5217\u8868\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_iii_template.py
        def is_solution(state: list[TreeNode]) -> bool:\n    \"\"\"\u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3\"\"\"\n    return state and state[-1].val == 7\n\ndef record_solution(state: list[TreeNode], res: list[list[TreeNode]]):\n    \"\"\"\u8bb0\u5f55\u89e3\"\"\"\n    res.append(list(state))\n\ndef is_valid(state: list[TreeNode], choice: TreeNode) -> bool:\n    \"\"\"\u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5\"\"\"\n    return choice is not None and choice.val != 3\n\ndef make_choice(state: list[TreeNode], choice: TreeNode):\n    \"\"\"\u66f4\u65b0\u72b6\u6001\"\"\"\n    state.append(choice)\n\ndef undo_choice(state: list[TreeNode], choice: TreeNode):\n    \"\"\"\u6062\u590d\u72b6\u6001\"\"\"\n    state.pop()\n\ndef backtrack(\n    state: list[TreeNode], choices: list[TreeNode], res: list[list[TreeNode]]\n):\n    \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09\"\"\"\n    # \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n    if is_solution(state):\n        # \u8bb0\u5f55\u89e3\n        record_solution(state, res)\n    # \u904d\u5386\u6240\u6709\u9009\u62e9\n    for choice in choices:\n        # \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if is_valid(state, choice):\n            # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            make_choice(state, choice)\n            # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, [choice.left, choice.right], res)\n            # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undo_choice(state, choice)\n
        preorder_traversal_iii_template.cpp
        /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(vector<TreeNode *> &state) {\n    return !state.empty() && state.back()->val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(vector<TreeNode *> &state, vector<vector<TreeNode *>> &res) {\n    res.push_back(state);\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nbool isValid(vector<TreeNode *> &state, TreeNode *choice) {\n    return choice != nullptr && choice->val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(vector<TreeNode *> &state, TreeNode *choice) {\n    state.push_back(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(vector<TreeNode *> &state, TreeNode *choice) {\n    state.pop_back();\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<vector<TreeNode *>> &res) {\n    // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n    if (isSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res);\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (TreeNode *choice : choices) {\n        // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            vector<TreeNode *> nextChoices{choice->left, choice->right};\n            backtrack(state, nextChoices, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state, choice);\n        }\n    }\n}\n
        preorder_traversal_iii_template.java
        /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nboolean isSolution(List<TreeNode> state) {\n    return !state.isEmpty() && state.get(state.size() - 1).val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n    res.add(new ArrayList<>(state));\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nboolean isValid(List<TreeNode> state, TreeNode choice) {\n    return choice != null && choice.val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(List<TreeNode> state, TreeNode choice) {\n    state.add(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(List<TreeNode> state, TreeNode choice) {\n    state.remove(state.size() - 1);\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {\n    // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n    if (isSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res);\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (TreeNode choice : choices) {\n        // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, Arrays.asList(choice.left, choice.right), res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state, choice);\n        }\n    }\n}\n
        preorder_traversal_iii_template.cs
        /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool IsSolution(List<TreeNode> state) {\n    return state.Count != 0 && state[^1].val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nvoid RecordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n    res.Add(new List<TreeNode>(state));\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nbool IsValid(List<TreeNode> state, TreeNode choice) {\n    return choice != null && choice.val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid MakeChoice(List<TreeNode> state, TreeNode choice) {\n    state.Add(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nvoid UndoChoice(List<TreeNode> state, TreeNode choice) {\n    state.RemoveAt(state.Count - 1);\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid Backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {\n    // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n    if (IsSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        RecordSolution(state, res);\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    foreach (TreeNode choice in choices) {\n        // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (IsValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            MakeChoice(state, choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            Backtrack(state, [choice.left!, choice.right!], res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            UndoChoice(state, choice);\n        }\n    }\n}\n
        preorder_traversal_iii_template.go
        /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunc isSolution(state *[]*TreeNode) bool {\n    return len(*state) != 0 && (*state)[len(*state)-1].Val == 7\n}\n\n/* \u8bb0\u5f55\u89e3 */\nfunc recordSolution(state *[]*TreeNode, res *[][]*TreeNode) {\n    *res = append(*res, append([]*TreeNode{}, *state...))\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfunc isValid(state *[]*TreeNode, choice *TreeNode) bool {\n    return choice != nil && choice.Val != 3\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nfunc makeChoice(state *[]*TreeNode, choice *TreeNode) {\n    *state = append(*state, choice)\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nfunc undoChoice(state *[]*TreeNode, choice *TreeNode) {\n    *state = (*state)[:len(*state)-1]\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunc backtrackIII(state *[]*TreeNode, choices *[]*TreeNode, res *[][]*TreeNode) {\n    // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n    if isSolution(state) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res)\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for _, choice := range *choices {\n        // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if isValid(state, choice) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice)\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            temp := make([]*TreeNode, 0)\n            temp = append(temp, choice.Left, choice.Right)\n            backtrackIII(state, &temp, res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state, choice)\n        }\n    }\n}\n
        preorder_traversal_iii_template.swift
        /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunc isSolution(state: [TreeNode]) -> Bool {\n    !state.isEmpty && state.last!.val == 7\n}\n\n/* \u8bb0\u5f55\u89e3 */\nfunc recordSolution(state: [TreeNode], res: inout [[TreeNode]]) {\n    res.append(state)\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfunc isValid(state: [TreeNode], choice: TreeNode?) -> Bool {\n    choice != nil && choice!.val != 3\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nfunc makeChoice(state: inout [TreeNode], choice: TreeNode) {\n    state.append(choice)\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nfunc undoChoice(state: inout [TreeNode], choice: TreeNode) {\n    state.removeLast()\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunc backtrack(state: inout [TreeNode], choices: [TreeNode], res: inout [[TreeNode]]) {\n    // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n    if isSolution(state: state) {\n        recordSolution(state: state, res: &res)\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for choice in choices {\n        // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if isValid(state: state, choice: choice) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state: &state, choice: choice)\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state: &state, choices: [choice.left, choice.right].compactMap { $0 }, res: &res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state: &state, choice: choice)\n        }\n    }\n}\n
        preorder_traversal_iii_template.js
        /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunction isSolution(state) {\n    return state && state[state.length - 1]?.val === 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nfunction recordSolution(state, res) {\n    res.push([...state]);\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfunction isValid(state, choice) {\n    return choice !== null && choice.val !== 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nfunction makeChoice(state, choice) {\n    state.push(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nfunction undoChoice(state) {\n    state.pop();\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunction backtrack(state, choices, res) {\n    // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n    if (isSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res);\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (const choice of choices) {\n        // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, [choice.left, choice.right], res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state);\n        }\n    }\n}\n
        preorder_traversal_iii_template.ts
        /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunction isSolution(state: TreeNode[]): boolean {\n    return state && state[state.length - 1]?.val === 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nfunction recordSolution(state: TreeNode[], res: TreeNode[][]): void {\n    res.push([...state]);\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfunction isValid(state: TreeNode[], choice: TreeNode): boolean {\n    return choice !== null && choice.val !== 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nfunction makeChoice(state: TreeNode[], choice: TreeNode): void {\n    state.push(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nfunction undoChoice(state: TreeNode[]): void {\n    state.pop();\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunction backtrack(\n    state: TreeNode[],\n    choices: TreeNode[],\n    res: TreeNode[][]\n): void {\n    // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n    if (isSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res);\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (const choice of choices) {\n        // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, [choice.left, choice.right], res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state);\n        }\n    }\n}\n
        preorder_traversal_iii_template.dart
        /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(List<TreeNode> state) {\n  return state.isNotEmpty && state.last.val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n  res.add(List.from(state));\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nbool isValid(List<TreeNode> state, TreeNode? choice) {\n  return choice != null && choice.val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(List<TreeNode> state, TreeNode? choice) {\n  state.add(choice!);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(List<TreeNode> state, TreeNode? choice) {\n  state.removeLast();\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(\n  List<TreeNode> state,\n  List<TreeNode?> choices,\n  List<List<TreeNode>> res,\n) {\n  // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n  if (isSolution(state)) {\n    // \u8bb0\u5f55\u89e3\n    recordSolution(state, res);\n  }\n  // \u904d\u5386\u6240\u6709\u9009\u62e9\n  for (TreeNode? choice in choices) {\n    // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n    if (isValid(state, choice)) {\n      // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n      makeChoice(state, choice);\n      // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n      backtrack(state, [choice!.left, choice.right], res);\n      // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n      undoChoice(state, choice);\n    }\n  }\n}\n
        preorder_traversal_iii_template.rs
        /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfn is_solution(state: &mut Vec<Rc<RefCell<TreeNode>>>) -> bool {\n    return !state.is_empty() && state.last().unwrap().borrow().val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nfn record_solution(\n    state: &mut Vec<Rc<RefCell<TreeNode>>>,\n    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,\n) {\n    res.push(state.clone());\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfn is_valid(_: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Option<&Rc<RefCell<TreeNode>>>) -> bool {\n    return choice.is_some() && choice.unwrap().borrow().val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nfn make_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Rc<RefCell<TreeNode>>) {\n    state.push(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nfn undo_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, _: Rc<RefCell<TreeNode>>) {\n    state.pop();\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfn backtrack(\n    state: &mut Vec<Rc<RefCell<TreeNode>>>,\n    choices: &Vec<Option<&Rc<RefCell<TreeNode>>>>,\n    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,\n) {\n    // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n    if is_solution(state) {\n        // \u8bb0\u5f55\u89e3\n        record_solution(state, res);\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for &choice in choices.iter() {\n        // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if is_valid(state, choice) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            make_choice(state, choice.unwrap().clone());\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(\n                state,\n                &vec![\n                    choice.unwrap().borrow().left.as_ref(),\n                    choice.unwrap().borrow().right.as_ref(),\n                ],\n                res,\n            );\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undo_choice(state, choice.unwrap().clone());\n        }\n    }\n}\n
        preorder_traversal_iii_template.c
        /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(void) {\n    return pathSize > 0 && path[pathSize - 1]->val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(void) {\n    for (int i = 0; i < pathSize; i++) {\n        res[resSize][i] = path[i];\n    }\n    resSize++;\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nbool isValid(TreeNode *choice) {\n    return choice != NULL && choice->val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(TreeNode *choice) {\n    path[pathSize++] = choice;\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(void) {\n    pathSize--;\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(TreeNode *choices[2]) {\n    // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n    if (isSolution()) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution();\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (int i = 0; i < 2; i++) {\n        TreeNode *choice = choices[i];\n        // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            TreeNode *nextChoices[2] = {choice->left, choice->right};\n            backtrack(nextChoices);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice();\n        }\n    }\n}\n
        preorder_traversal_iii_template.kt
        /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfun isSolution(state: MutableList<TreeNode?>): Boolean {\n    return state.isNotEmpty() && state[state.size - 1]?._val == 7\n}\n\n/* \u8bb0\u5f55\u89e3 */\nfun recordSolution(state: MutableList<TreeNode?>?, res: MutableList<MutableList<TreeNode?>?>) {\n    res.add(state!!.toMutableList())\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfun isValid(state: MutableList<TreeNode?>?, choice: TreeNode?): Boolean {\n    return choice != null && choice._val != 3\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nfun makeChoice(state: MutableList<TreeNode?>, choice: TreeNode?) {\n    state.add(choice)\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nfun undoChoice(state: MutableList<TreeNode?>, choice: TreeNode?) {\n    state.removeLast()\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfun backtrack(\n    state: MutableList<TreeNode?>,\n    choices: MutableList<TreeNode?>,\n    res: MutableList<MutableList<TreeNode?>?>\n) {\n    // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n    if (isSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res)\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (choice in choices) {\n        // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice)\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, mutableListOf(choice!!.left, choice.right), res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state, choice)\n        }\n    }\n}\n
        preorder_traversal_iii_template.rb
        ### \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 ###\ndef is_solution?(state)\n  !state.empty? && state.last.val == 7\nend\n\n### \u8bb0\u5f55\u89e3 ###\ndef record_solution(state, res)\n  res << state.dup\nend\n\n### \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 ###\ndef is_valid?(state, choice)\n  choice && choice.val != 3\nend\n\n### \u66f4\u65b0\u72b6\u6001 ###\ndef make_choice(state, choice)\n  state << choice\nend\n\n### \u6062\u590d\u72b6\u6001 ###\ndef undo_choice(state, choice)\n  state.pop\nend\n\n### \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 ###\ndef backtrack(state, choices, res)\n  # \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n  record_solution(state, res) if is_solution?(state)\n\n  # \u904d\u5386\u6240\u6709\u9009\u62e9\n  for choice in choices\n    # \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n    if is_valid?(state, choice)\n      # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n      make_choice(state, choice)\n      # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n      backtrack(state, [choice.left, choice.right], res)\n      # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n      undo_choice(state, choice)\n    end\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u6839\u636e\u9898\u610f\uff0c\u6211\u4eec\u5728\u627e\u5230\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\u540e\u5e94\u8be5\u7ee7\u7eed\u641c\u7d22\uff0c\u56e0\u6b64\u9700\u8981\u5c06\u8bb0\u5f55\u89e3\u4e4b\u540e\u7684 return \u8bed\u53e5\u5220\u9664\u3002\u56fe 13-4 \u5bf9\u6bd4\u4e86\u4fdd\u7559\u6216\u5220\u9664 return \u8bed\u53e5\u7684\u641c\u7d22\u8fc7\u7a0b\u3002

        \u56fe 13-4 \u00a0 \u4fdd\u7559\u4e0e\u5220\u9664 return \u7684\u641c\u7d22\u8fc7\u7a0b\u5bf9\u6bd4

        \u76f8\u6bd4\u57fa\u4e8e\u524d\u5e8f\u904d\u5386\u7684\u4ee3\u7801\u5b9e\u73b0\uff0c\u57fa\u4e8e\u56de\u6eaf\u7b97\u6cd5\u6846\u67b6\u7684\u4ee3\u7801\u5b9e\u73b0\u867d\u7136\u663e\u5f97\u5570\u5506\uff0c\u4f46\u901a\u7528\u6027\u66f4\u597d\u3002\u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u56de\u6eaf\u95ee\u9898\u53ef\u4ee5\u5728\u8be5\u6846\u67b6\u4e0b\u89e3\u51b3\u3002\u6211\u4eec\u53ea\u9700\u6839\u636e\u5177\u4f53\u95ee\u9898\u6765\u5b9a\u4e49 state \u548c choices \uff0c\u5e76\u5b9e\u73b0\u6846\u67b6\u4e2d\u7684\u5404\u4e2a\u65b9\u6cd5\u5373\u53ef\u3002

        "},{"location":"chapter_backtracking/backtracking_algorithm/#1314","title":"13.1.4 \u00a0 \u5e38\u7528\u672f\u8bed","text":"

        \u4e3a\u4e86\u66f4\u6e05\u6670\u5730\u5206\u6790\u7b97\u6cd5\u95ee\u9898\uff0c\u6211\u4eec\u603b\u7ed3\u4e00\u4e0b\u56de\u6eaf\u7b97\u6cd5\u4e2d\u5e38\u7528\u672f\u8bed\u7684\u542b\u4e49\uff0c\u5e76\u5bf9\u7167\u4f8b\u9898\u4e09\u7ed9\u51fa\u5bf9\u5e94\u793a\u4f8b\uff0c\u5982\u8868 13-1 \u6240\u793a\u3002

        \u8868 13-1 \u00a0 \u5e38\u89c1\u7684\u56de\u6eaf\u7b97\u6cd5\u672f\u8bed

        \u540d\u8bcd \u5b9a\u4e49 \u4f8b\u9898\u4e09 \u89e3\uff08solution\uff09 \u89e3\u662f\u6ee1\u8db3\u95ee\u9898\u7279\u5b9a\u6761\u4ef6\u7684\u7b54\u6848\uff0c\u53ef\u80fd\u6709\u4e00\u4e2a\u6216\u591a\u4e2a \u6839\u8282\u70b9\u5230\u8282\u70b9 \\(7\\) \u7684\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u6240\u6709\u8def\u5f84 \u7ea6\u675f\u6761\u4ef6\uff08constraint\uff09 \u7ea6\u675f\u6761\u4ef6\u662f\u95ee\u9898\u4e2d\u9650\u5236\u89e3\u7684\u53ef\u884c\u6027\u7684\u6761\u4ef6\uff0c\u901a\u5e38\u7528\u4e8e\u526a\u679d \u8def\u5f84\u4e2d\u4e0d\u5305\u542b\u8282\u70b9 \\(3\\) \u72b6\u6001\uff08state\uff09 \u72b6\u6001\u8868\u793a\u95ee\u9898\u5728\u67d0\u4e00\u65f6\u523b\u7684\u60c5\u51b5\uff0c\u5305\u62ec\u5df2\u7ecf\u505a\u51fa\u7684\u9009\u62e9 \u5f53\u524d\u5df2\u8bbf\u95ee\u7684\u8282\u70b9\u8def\u5f84\uff0c\u5373 path \u8282\u70b9\u5217\u8868 \u5c1d\u8bd5\uff08attempt\uff09 \u5c1d\u8bd5\u662f\u6839\u636e\u53ef\u7528\u9009\u62e9\u6765\u63a2\u7d22\u89e3\u7a7a\u95f4\u7684\u8fc7\u7a0b\uff0c\u5305\u62ec\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\uff0c\u68c0\u67e5\u662f\u5426\u4e3a\u89e3 \u9012\u5f52\u8bbf\u95ee\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\uff0c\u5c06\u8282\u70b9\u6dfb\u52a0\u8fdb path \uff0c\u5224\u65ad\u8282\u70b9\u7684\u503c\u662f\u5426\u4e3a \\(7\\) \u56de\u9000\uff08backtracking\uff09 \u56de\u9000\u6307\u9047\u5230\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u72b6\u6001\u65f6\uff0c\u64a4\u9500\u524d\u9762\u505a\u51fa\u7684\u9009\u62e9\uff0c\u56de\u5230\u4e0a\u4e00\u4e2a\u72b6\u6001 \u5f53\u8d8a\u8fc7\u53f6\u8282\u70b9\u3001\u7ed3\u675f\u8282\u70b9\u8bbf\u95ee\u3001\u9047\u5230\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u65f6\u7ec8\u6b62\u641c\u7d22\uff0c\u51fd\u6570\u8fd4\u56de \u526a\u679d\uff08pruning\uff09 \u526a\u679d\u662f\u6839\u636e\u95ee\u9898\u7279\u6027\u548c\u7ea6\u675f\u6761\u4ef6\u907f\u514d\u65e0\u610f\u4e49\u7684\u641c\u7d22\u8def\u5f84\u7684\u65b9\u6cd5\uff0c\u53ef\u63d0\u9ad8\u641c\u7d22\u6548\u7387 \u5f53\u9047\u5230\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u65f6\uff0c\u5219\u4e0d\u518d\u7ee7\u7eed\u641c\u7d22

        Tip

        \u95ee\u9898\u3001\u89e3\u3001\u72b6\u6001\u7b49\u6982\u5ff5\u662f\u901a\u7528\u7684\uff0c\u5728\u5206\u6cbb\u3001\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\u3001\u8d2a\u5fc3\u7b49\u7b97\u6cd5\u4e2d\u90fd\u6709\u6d89\u53ca\u3002

        "},{"location":"chapter_backtracking/backtracking_algorithm/#1315","title":"13.1.5 \u00a0 \u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"

        \u56de\u6eaf\u7b97\u6cd5\u672c\u8d28\u4e0a\u662f\u4e00\u79cd\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u7b97\u6cd5\uff0c\u5b83\u5c1d\u8bd5\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\u76f4\u5230\u627e\u5230\u6ee1\u8db3\u6761\u4ef6\u7684\u89e3\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u80fd\u591f\u627e\u5230\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u800c\u4e14\u5728\u5408\u7406\u7684\u526a\u679d\u64cd\u4f5c\u4e0b\uff0c\u5177\u6709\u5f88\u9ad8\u7684\u6548\u7387\u3002

        \u7136\u800c\uff0c\u5728\u5904\u7406\u5927\u89c4\u6a21\u6216\u8005\u590d\u6742\u95ee\u9898\u65f6\uff0c\u56de\u6eaf\u7b97\u6cd5\u7684\u8fd0\u884c\u6548\u7387\u53ef\u80fd\u96be\u4ee5\u63a5\u53d7\u3002

        • \u65f6\u95f4\uff1a\u56de\u6eaf\u7b97\u6cd5\u901a\u5e38\u9700\u8981\u904d\u5386\u72b6\u6001\u7a7a\u95f4\u7684\u6240\u6709\u53ef\u80fd\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u8fbe\u5230\u6307\u6570\u9636\u6216\u9636\u4e58\u9636\u3002
        • \u7a7a\u95f4\uff1a\u5728\u9012\u5f52\u8c03\u7528\u4e2d\u9700\u8981\u4fdd\u5b58\u5f53\u524d\u7684\u72b6\u6001\uff08\u4f8b\u5982\u8def\u5f84\u3001\u7528\u4e8e\u526a\u679d\u7684\u8f85\u52a9\u53d8\u91cf\u7b49\uff09\uff0c\u5f53\u6df1\u5ea6\u5f88\u5927\u65f6\uff0c\u7a7a\u95f4\u9700\u6c42\u53ef\u80fd\u4f1a\u53d8\u5f97\u5f88\u5927\u3002

        \u5373\u4fbf\u5982\u6b64\uff0c\u56de\u6eaf\u7b97\u6cd5\u4ecd\u7136\u662f\u67d0\u4e9b\u641c\u7d22\u95ee\u9898\u548c\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\u7684\u6700\u4f73\u89e3\u51b3\u65b9\u6848\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u95ee\u9898\uff0c\u7531\u4e8e\u65e0\u6cd5\u9884\u6d4b\u54ea\u4e9b\u9009\u62e9\u53ef\u751f\u6210\u6709\u6548\u7684\u89e3\uff0c\u56e0\u6b64\u6211\u4eec\u5fc5\u987b\u5bf9\u6240\u6709\u53ef\u80fd\u7684\u9009\u62e9\u8fdb\u884c\u904d\u5386\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5173\u952e\u662f\u5982\u4f55\u4f18\u5316\u6548\u7387\uff0c\u5e38\u89c1\u7684\u6548\u7387\u4f18\u5316\u65b9\u6cd5\u6709\u4e24\u79cd\u3002

        • \u526a\u679d\uff1a\u907f\u514d\u641c\u7d22\u90a3\u4e9b\u80af\u5b9a\u4e0d\u4f1a\u4ea7\u751f\u89e3\u7684\u8def\u5f84\uff0c\u4ece\u800c\u8282\u7701\u65f6\u95f4\u548c\u7a7a\u95f4\u3002
        • \u542f\u53d1\u5f0f\u641c\u7d22\uff1a\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u5f15\u5165\u4e00\u4e9b\u7b56\u7565\u6216\u8005\u4f30\u8ba1\u503c\uff0c\u4ece\u800c\u4f18\u5148\u641c\u7d22\u6700\u6709\u53ef\u80fd\u4ea7\u751f\u6709\u6548\u89e3\u7684\u8def\u5f84\u3002
        "},{"location":"chapter_backtracking/backtracking_algorithm/#1316","title":"13.1.6 \u00a0 \u56de\u6eaf\u5178\u578b\u4f8b\u9898","text":"

        \u56de\u6eaf\u7b97\u6cd5\u53ef\u7528\u4e8e\u89e3\u51b3\u8bb8\u591a\u641c\u7d22\u95ee\u9898\u3001\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\u548c\u7ec4\u5408\u4f18\u5316\u95ee\u9898\u3002

        \u641c\u7d22\u95ee\u9898\uff1a\u8fd9\u7c7b\u95ee\u9898\u7684\u76ee\u6807\u662f\u627e\u5230\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u89e3\u51b3\u65b9\u6848\u3002

        • \u5168\u6392\u5217\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u96c6\u5408\uff0c\u6c42\u51fa\u5176\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u7ec4\u5408\u3002
        • \u5b50\u96c6\u548c\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u96c6\u5408\u548c\u4e00\u4e2a\u76ee\u6807\u548c\uff0c\u627e\u5230\u96c6\u5408\u4e2d\u6240\u6709\u548c\u4e3a\u76ee\u6807\u548c\u7684\u5b50\u96c6\u3002
        • \u6c49\u8bfa\u5854\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e09\u6839\u67f1\u5b50\u548c\u4e00\u7cfb\u5217\u5927\u5c0f\u4e0d\u540c\u7684\u5706\u76d8\uff0c\u8981\u6c42\u5c06\u6240\u6709\u5706\u76d8\u4ece\u4e00\u6839\u67f1\u5b50\u79fb\u52a8\u5230\u53e6\u4e00\u6839\u67f1\u5b50\uff0c\u6bcf\u6b21\u53ea\u80fd\u79fb\u52a8\u4e00\u4e2a\u5706\u76d8\uff0c\u4e14\u4e0d\u80fd\u5c06\u5927\u5706\u76d8\u653e\u5728\u5c0f\u5706\u76d8\u4e0a\u3002

        \u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\uff1a\u8fd9\u7c7b\u95ee\u9898\u7684\u76ee\u6807\u662f\u627e\u5230\u6ee1\u8db3\u6240\u6709\u7ea6\u675f\u6761\u4ef6\u7684\u89e3\u3002

        • \\(n\\) \u7687\u540e\uff1a\u5728 \\(n \\times n\\) \u7684\u68cb\u76d8\u4e0a\u653e\u7f6e \\(n\\) \u4e2a\u7687\u540e\uff0c\u4f7f\u5f97\u5b83\u4eec\u4e92\u4e0d\u653b\u51fb\u3002
        • \u6570\u72ec\uff1a\u5728 \\(9 \\times 9\\) \u7684\u7f51\u683c\u4e2d\u586b\u5165\u6570\u5b57 \\(1\\) ~ \\(9\\) \uff0c\u4f7f\u5f97\u6bcf\u884c\u3001\u6bcf\u5217\u548c\u6bcf\u4e2a \\(3 \\times 3\\) \u5b50\u7f51\u683c\u4e2d\u7684\u6570\u5b57\u4e0d\u91cd\u590d\u3002
        • \u56fe\u7740\u8272\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u65e0\u5411\u56fe\uff0c\u7528\u6700\u5c11\u7684\u989c\u8272\u7ed9\u56fe\u7684\u6bcf\u4e2a\u9876\u70b9\u7740\u8272\uff0c\u4f7f\u5f97\u76f8\u90bb\u9876\u70b9\u989c\u8272\u4e0d\u540c\u3002

        \u7ec4\u5408\u4f18\u5316\u95ee\u9898\uff1a\u8fd9\u7c7b\u95ee\u9898\u7684\u76ee\u6807\u662f\u5728\u4e00\u4e2a\u7ec4\u5408\u7a7a\u95f4\u4e2d\u627e\u5230\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u7684\u6700\u4f18\u89e3\u3002

        • 0-1 \u80cc\u5305\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u7ec4\u7269\u54c1\u548c\u4e00\u4e2a\u80cc\u5305\uff0c\u6bcf\u4e2a\u7269\u54c1\u6709\u4e00\u5b9a\u7684\u4ef7\u503c\u548c\u91cd\u91cf\uff0c\u8981\u6c42\u5728\u80cc\u5305\u5bb9\u91cf\u9650\u5236\u5185\uff0c\u9009\u62e9\u7269\u54c1\u4f7f\u5f97\u603b\u4ef7\u503c\u6700\u5927\u3002
        • \u65c5\u884c\u5546\u95ee\u9898\uff1a\u5728\u4e00\u4e2a\u56fe\u4e2d\uff0c\u4ece\u4e00\u4e2a\u70b9\u51fa\u53d1\uff0c\u8bbf\u95ee\u6240\u6709\u5176\u4ed6\u70b9\u6070\u597d\u4e00\u6b21\u540e\u8fd4\u56de\u8d77\u70b9\uff0c\u6c42\u6700\u77ed\u8def\u5f84\u3002
        • \u6700\u5927\u56e2\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u65e0\u5411\u56fe\uff0c\u627e\u5230\u6700\u5927\u7684\u5b8c\u5168\u5b50\u56fe\uff0c\u5373\u5b50\u56fe\u4e2d\u7684\u4efb\u610f\u4e24\u4e2a\u9876\u70b9\u4e4b\u95f4\u90fd\u6709\u8fb9\u76f8\u8fde\u3002

        \u8bf7\u6ce8\u610f\uff0c\u5bf9\u4e8e\u8bb8\u591a\u7ec4\u5408\u4f18\u5316\u95ee\u9898\uff0c\u56de\u6eaf\u4e0d\u662f\u6700\u4f18\u89e3\u51b3\u65b9\u6848\u3002

        • 0-1 \u80cc\u5305\u95ee\u9898\u901a\u5e38\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u89e3\u51b3\uff0c\u4ee5\u8fbe\u5230\u66f4\u9ad8\u7684\u65f6\u95f4\u6548\u7387\u3002
        • \u65c5\u884c\u5546\u662f\u4e00\u4e2a\u8457\u540d\u7684 NP-Hard \u95ee\u9898\uff0c\u5e38\u7528\u89e3\u6cd5\u6709\u9057\u4f20\u7b97\u6cd5\u548c\u8681\u7fa4\u7b97\u6cd5\u7b49\u3002
        • \u6700\u5927\u56e2\u95ee\u9898\u662f\u56fe\u8bba\u4e2d\u7684\u4e00\u4e2a\u7ecf\u5178\u95ee\u9898\uff0c\u53ef\u7528\u8d2a\u5fc3\u7b97\u6cd5\u7b49\u542f\u53d1\u5f0f\u7b97\u6cd5\u6765\u89e3\u51b3\u3002
        "},{"location":"chapter_backtracking/n_queens_problem/","title":"13.4 \u00a0 n \u7687\u540e\u95ee\u9898","text":"

        Question

        \u6839\u636e\u56fd\u9645\u8c61\u68cb\u7684\u89c4\u5219\uff0c\u7687\u540e\u53ef\u4ee5\u653b\u51fb\u4e0e\u540c\u5904\u4e00\u884c\u3001\u4e00\u5217\u6216\u4e00\u6761\u659c\u7ebf\u4e0a\u7684\u68cb\u5b50\u3002\u7ed9\u5b9a \\(n\\) \u4e2a\u7687\u540e\u548c\u4e00\u4e2a \\(n \\times n\\) \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5bfb\u627e\u4f7f\u5f97\u6240\u6709\u7687\u540e\u4e4b\u95f4\u65e0\u6cd5\u76f8\u4e92\u653b\u51fb\u7684\u6446\u653e\u65b9\u6848\u3002

        \u5982\u56fe 13-15 \u6240\u793a\uff0c\u5f53 \\(n = 4\\) \u65f6\uff0c\u5171\u53ef\u4ee5\u627e\u5230\u4e24\u4e2a\u89e3\u3002\u4ece\u56de\u6eaf\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\\(n \\times n\\) \u5927\u5c0f\u7684\u68cb\u76d8\u5171\u6709 \\(n^2\\) \u4e2a\u683c\u5b50\uff0c\u7ed9\u51fa\u4e86\u6240\u6709\u7684\u9009\u62e9 choices \u3002\u5728\u9010\u4e2a\u653e\u7f6e\u7687\u540e\u7684\u8fc7\u7a0b\u4e2d\uff0c\u68cb\u76d8\u72b6\u6001\u5728\u4e0d\u65ad\u5730\u53d8\u5316\uff0c\u6bcf\u4e2a\u65f6\u523b\u7684\u68cb\u76d8\u5c31\u662f\u72b6\u6001 state \u3002

        \u56fe 13-15 \u00a0 4 \u7687\u540e\u95ee\u9898\u7684\u89e3

        \u56fe 13-16 \u5c55\u793a\u4e86\u672c\u9898\u7684\u4e09\u4e2a\u7ea6\u675f\u6761\u4ef6\uff1a\u591a\u4e2a\u7687\u540e\u4e0d\u80fd\u5728\u540c\u4e00\u884c\u3001\u540c\u4e00\u5217\u3001\u540c\u4e00\u6761\u5bf9\u89d2\u7ebf\u4e0a\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5bf9\u89d2\u7ebf\u5206\u4e3a\u4e3b\u5bf9\u89d2\u7ebf \\ \u548c\u6b21\u5bf9\u89d2\u7ebf / \u4e24\u79cd\u3002

        \u56fe 13-16 \u00a0 n \u7687\u540e\u95ee\u9898\u7684\u7ea6\u675f\u6761\u4ef6

        "},{"location":"chapter_backtracking/n_queens_problem/#1","title":"1. \u00a0 \u9010\u884c\u653e\u7f6e\u7b56\u7565","text":"

        \u7687\u540e\u7684\u6570\u91cf\u548c\u68cb\u76d8\u7684\u884c\u6570\u90fd\u4e3a \\(n\\) \uff0c\u56e0\u6b64\u6211\u4eec\u5bb9\u6613\u5f97\u5230\u4e00\u4e2a\u63a8\u8bba\uff1a\u68cb\u76d8\u6bcf\u884c\u90fd\u5141\u8bb8\u4e14\u53ea\u5141\u8bb8\u653e\u7f6e\u4e00\u4e2a\u7687\u540e\u3002

        \u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u53d6\u9010\u884c\u653e\u7f6e\u7b56\u7565\uff1a\u4ece\u7b2c\u4e00\u884c\u5f00\u59cb\uff0c\u5728\u6bcf\u884c\u653e\u7f6e\u4e00\u4e2a\u7687\u540e\uff0c\u76f4\u81f3\u6700\u540e\u4e00\u884c\u7ed3\u675f\u3002

        \u56fe 13-17 \u6240\u793a\u4e3a 4 \u7687\u540e\u95ee\u9898\u7684\u9010\u884c\u653e\u7f6e\u8fc7\u7a0b\u3002\u53d7\u753b\u5e45\u9650\u5236\uff0c\u56fe 13-17 \u4ec5\u5c55\u5f00\u4e86\u7b2c\u4e00\u884c\u7684\u5176\u4e2d\u4e00\u4e2a\u641c\u7d22\u5206\u652f\uff0c\u5e76\u4e14\u5c06\u4e0d\u6ee1\u8db3\u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u65b9\u6848\u90fd\u8fdb\u884c\u4e86\u526a\u679d\u3002

        \u56fe 13-17 \u00a0 \u9010\u884c\u653e\u7f6e\u7b56\u7565

        \u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u9010\u884c\u653e\u7f6e\u7b56\u7565\u8d77\u5230\u4e86\u526a\u679d\u7684\u4f5c\u7528\uff0c\u5b83\u907f\u514d\u4e86\u540c\u4e00\u884c\u51fa\u73b0\u591a\u4e2a\u7687\u540e\u7684\u6240\u6709\u641c\u7d22\u5206\u652f\u3002

        "},{"location":"chapter_backtracking/n_queens_problem/#2","title":"2. \u00a0 \u5217\u4e0e\u5bf9\u89d2\u7ebf\u526a\u679d","text":"

        \u4e3a\u4e86\u6ee1\u8db3\u5217\u7ea6\u675f\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u5e03\u5c14\u578b\u6570\u7ec4 cols \u8bb0\u5f55\u6bcf\u4e00\u5217\u662f\u5426\u6709\u7687\u540e\u3002\u5728\u6bcf\u6b21\u51b3\u5b9a\u653e\u7f6e\u524d\uff0c\u6211\u4eec\u901a\u8fc7 cols \u5c06\u5df2\u6709\u7687\u540e\u7684\u5217\u8fdb\u884c\u526a\u679d\uff0c\u5e76\u5728\u56de\u6eaf\u4e2d\u52a8\u6001\u66f4\u65b0 cols \u7684\u72b6\u6001\u3002

        Tip

        \u8bf7\u6ce8\u610f\uff0c\u77e9\u9635\u7684\u8d77\u70b9\u4f4d\u4e8e\u5de6\u4e0a\u89d2\uff0c\u5176\u4e2d\u884c\u7d22\u5f15\u4ece\u4e0a\u5230\u4e0b\u589e\u52a0\uff0c\u5217\u7d22\u5f15\u4ece\u5de6\u5230\u53f3\u589e\u52a0\u3002

        \u90a3\u4e48\uff0c\u5982\u4f55\u5904\u7406\u5bf9\u89d2\u7ebf\u7ea6\u675f\u5462\uff1f\u8bbe\u68cb\u76d8\u4e2d\u67d0\u4e2a\u683c\u5b50\u7684\u884c\u5217\u7d22\u5f15\u4e3a \\((row, col)\\) \uff0c\u9009\u5b9a\u77e9\u9635\u4e2d\u7684\u67d0\u6761\u4e3b\u5bf9\u89d2\u7ebf\uff0c\u6211\u4eec\u53d1\u73b0\u8be5\u5bf9\u89d2\u7ebf\u4e0a\u6240\u6709\u683c\u5b50\u7684\u884c\u7d22\u5f15\u51cf\u5217\u7d22\u5f15\u90fd\u76f8\u7b49\uff0c\u5373\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u6240\u6709\u683c\u5b50\u7684 \\(row - col\\) \u4e3a\u6052\u5b9a\u503c\u3002

        \u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4e24\u4e2a\u683c\u5b50\u6ee1\u8db3 \\(row_1 - col_1 = row_2 - col_2\\) \uff0c\u5219\u5b83\u4eec\u4e00\u5b9a\u5904\u5728\u540c\u4e00\u6761\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u3002\u5229\u7528\u8be5\u89c4\u5f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u501f\u52a9\u56fe 13-18 \u6240\u793a\u7684\u6570\u7ec4 diags1 \u8bb0\u5f55\u6bcf\u6761\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\u3002

        \u540c\u7406\uff0c\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u7684\u6240\u6709\u683c\u5b50\u7684 \\(row + col\\) \u662f\u6052\u5b9a\u503c\u3002\u6211\u4eec\u540c\u6837\u4e5f\u53ef\u4ee5\u501f\u52a9\u6570\u7ec4 diags2 \u6765\u5904\u7406\u6b21\u5bf9\u89d2\u7ebf\u7ea6\u675f\u3002

        \u56fe 13-18 \u00a0 \u5904\u7406\u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f

        "},{"location":"chapter_backtracking/n_queens_problem/#3","title":"3. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u8bf7\u6ce8\u610f\uff0c\\(n\\) \u7ef4\u65b9\u9635\u4e2d \\(row - col\\) \u7684\u8303\u56f4\u662f \\([-n + 1, n - 1]\\) \uff0c\\(row + col\\) \u7684\u8303\u56f4\u662f \\([0, 2n - 2]\\) \uff0c\u6240\u4ee5\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\u7684\u6570\u91cf\u90fd\u4e3a \\(2n - 1\\) \uff0c\u5373\u6570\u7ec4 diags1 \u548c diags2 \u7684\u957f\u5ea6\u90fd\u4e3a \\(2n - 1\\) \u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby n_queens.py
        def backtrack(\n    row: int,\n    n: int,\n    state: list[list[str]],\n    res: list[list[list[str]]],\n    cols: list[bool],\n    diags1: list[bool],\n    diags2: list[bool],\n):\n    \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e\"\"\"\n    # \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if row == n:\n        res.append([list(row) for row in state])\n        return\n    # \u904d\u5386\u6240\u6709\u5217\n    for col in range(n):\n        # \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n        diag1 = row - col + n - 1\n        diag2 = row + col\n        # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n        if not cols[col] and not diags1[diag1] and not diags2[diag2]:\n            # \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n            state[row][col] = \"Q\"\n            cols[col] = diags1[diag1] = diags2[diag2] = True\n            # \u653e\u7f6e\u4e0b\u4e00\u884c\n            backtrack(row + 1, n, state, res, cols, diags1, diags2)\n            # \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n            state[row][col] = \"#\"\n            cols[col] = diags1[diag1] = diags2[diag2] = False\n\ndef n_queens(n: int) -> list[list[list[str]]]:\n    \"\"\"\u6c42\u89e3 n \u7687\u540e\"\"\"\n    # \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n    state = [[\"#\" for _ in range(n)] for _ in range(n)]\n    cols = [False] * n  # \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n    diags1 = [False] * (2 * n - 1)  # \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    diags2 = [False] * (2 * n - 1)  # \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    res = []\n    backtrack(0, n, state, res, cols, diags1, diags2)\n\n    return res\n
        n_queens.cpp
        /* \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e */\nvoid backtrack(int row, int n, vector<vector<string>> &state, vector<vector<vector<string>>> &res, vector<bool> &cols,\n               vector<bool> &diags1, vector<bool> &diags2) {\n    // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (row == n) {\n        res.push_back(state);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u5217\n    for (int col = 0; col < n; col++) {\n        // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n            state[row][col] = \"Q\";\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // \u653e\u7f6e\u4e0b\u4e00\u884c\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n            state[row][col] = \"#\";\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nvector<vector<vector<string>>> nQueens(int n) {\n    // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n    vector<vector<string>> state(n, vector<string>(n, \"#\"));\n    vector<bool> cols(n, false);           // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n    vector<bool> diags1(2 * n - 1, false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    vector<bool> diags2(2 * n - 1, false); // \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    vector<vector<vector<string>>> res;\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n\n    return res;\n}\n
        n_queens.java
        /* \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e */\nvoid backtrack(int row, int n, List<List<String>> state, List<List<List<String>>> res,\n        boolean[] cols, boolean[] diags1, boolean[] diags2) {\n    // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (row == n) {\n        List<List<String>> copyState = new ArrayList<>();\n        for (List<String> sRow : state) {\n            copyState.add(new ArrayList<>(sRow));\n        }\n        res.add(copyState);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u5217\n    for (int col = 0; col < n; col++) {\n        // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n            state.get(row).set(col, \"Q\");\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // \u653e\u7f6e\u4e0b\u4e00\u884c\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n            state.get(row).set(col, \"#\");\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nList<List<List<String>>> nQueens(int n) {\n    // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n    List<List<String>> state = new ArrayList<>();\n    for (int i = 0; i < n; i++) {\n        List<String> row = new ArrayList<>();\n        for (int j = 0; j < n; j++) {\n            row.add(\"#\");\n        }\n        state.add(row);\n    }\n    boolean[] cols = new boolean[n]; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n    boolean[] diags1 = new boolean[2 * n - 1]; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    boolean[] diags2 = new boolean[2 * n - 1]; // \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    List<List<List<String>>> res = new ArrayList<>();\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n\n    return res;\n}\n
        n_queens.cs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e */\nvoid Backtrack(int row, int n, List<List<string>> state, List<List<List<string>>> res,\n        bool[] cols, bool[] diags1, bool[] diags2) {\n    // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (row == n) {\n        List<List<string>> copyState = [];\n        foreach (List<string> sRow in state) {\n            copyState.Add(new List<string>(sRow));\n        }\n        res.Add(copyState);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u5217\n    for (int col = 0; col < n; col++) {\n        // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n            state[row][col] = \"Q\";\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // \u653e\u7f6e\u4e0b\u4e00\u884c\n            Backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n            state[row][col] = \"#\";\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nList<List<List<string>>> NQueens(int n) {\n    // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n    List<List<string>> state = [];\n    for (int i = 0; i < n; i++) {\n        List<string> row = [];\n        for (int j = 0; j < n; j++) {\n            row.Add(\"#\");\n        }\n        state.Add(row);\n    }\n    bool[] cols = new bool[n]; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n    bool[] diags1 = new bool[2 * n - 1]; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    bool[] diags2 = new bool[2 * n - 1]; // \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    List<List<List<string>>> res = [];\n\n    Backtrack(0, n, state, res, cols, diags1, diags2);\n\n    return res;\n}\n
        n_queens.go
        /* \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e */\nfunc backtrack(row, n int, state *[][]string, res *[][][]string, cols, diags1, diags2 *[]bool) {\n    // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if row == n {\n        newState := make([][]string, len(*state))\n        for i, _ := range newState {\n            newState[i] = make([]string, len((*state)[0]))\n            copy(newState[i], (*state)[i])\n\n        }\n        *res = append(*res, newState)\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u5217\n    for col := 0; col < n; col++ {\n        // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n        diag1 := row - col + n - 1\n        diag2 := row + col\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n        if !(*cols)[col] && !(*diags1)[diag1] && !(*diags2)[diag2] {\n            // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n            (*state)[row][col] = \"Q\"\n            (*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = true, true, true\n            // \u653e\u7f6e\u4e0b\u4e00\u884c\n            backtrack(row+1, n, state, res, cols, diags1, diags2)\n            // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n            (*state)[row][col] = \"#\"\n            (*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = false, false, false\n        }\n    }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nfunc nQueens(n int) [][][]string {\n    // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n    state := make([][]string, n)\n    for i := 0; i < n; i++ {\n        row := make([]string, n)\n        for i := 0; i < n; i++ {\n            row[i] = \"#\"\n        }\n        state[i] = row\n    }\n    // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n    cols := make([]bool, n)\n    diags1 := make([]bool, 2*n-1)\n    diags2 := make([]bool, 2*n-1)\n    res := make([][][]string, 0)\n    backtrack(0, n, &state, &res, &cols, &diags1, &diags2)\n    return res\n}\n
        n_queens.swift
        /* \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e */\nfunc backtrack(row: Int, n: Int, state: inout [[String]], res: inout [[[String]]], cols: inout [Bool], diags1: inout [Bool], diags2: inout [Bool]) {\n    // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if row == n {\n        res.append(state)\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u5217\n    for col in 0 ..< n {\n        // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n        let diag1 = row - col + n - 1\n        let diag2 = row + col\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n        if !cols[col] && !diags1[diag1] && !diags2[diag2] {\n            // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n            state[row][col] = \"Q\"\n            cols[col] = true\n            diags1[diag1] = true\n            diags2[diag2] = true\n            // \u653e\u7f6e\u4e0b\u4e00\u884c\n            backtrack(row: row + 1, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)\n            // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n            state[row][col] = \"#\"\n            cols[col] = false\n            diags1[diag1] = false\n            diags2[diag2] = false\n        }\n    }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nfunc nQueens(n: Int) -> [[[String]]] {\n    // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n    var state = Array(repeating: Array(repeating: \"#\", count: n), count: n)\n    var cols = Array(repeating: false, count: n) // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n    var diags1 = Array(repeating: false, count: 2 * n - 1) // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    var diags2 = Array(repeating: false, count: 2 * n - 1) // \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    var res: [[[String]]] = []\n\n    backtrack(row: 0, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)\n\n    return res\n}\n
        n_queens.js
        /* \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e */\nfunction backtrack(row, n, state, res, cols, diags1, diags2) {\n    // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (row === n) {\n        res.push(state.map((row) => row.slice()));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u5217\n    for (let col = 0; col < n; col++) {\n        // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n        const diag1 = row - col + n - 1;\n        const diag2 = row + col;\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n            state[row][col] = 'Q';\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // \u653e\u7f6e\u4e0b\u4e00\u884c\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n            state[row][col] = '#';\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nfunction nQueens(n) {\n    // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n    const state = Array.from({ length: n }, () => Array(n).fill('#'));\n    const cols = Array(n).fill(false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n    const diags1 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    const diags2 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    const res = [];\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n    return res;\n}\n
        n_queens.ts
        /* \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e */\nfunction backtrack(\n    row: number,\n    n: number,\n    state: string[][],\n    res: string[][][],\n    cols: boolean[],\n    diags1: boolean[],\n    diags2: boolean[]\n): void {\n    // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (row === n) {\n        res.push(state.map((row) => row.slice()));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u5217\n    for (let col = 0; col < n; col++) {\n        // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n        const diag1 = row - col + n - 1;\n        const diag2 = row + col;\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n            state[row][col] = 'Q';\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // \u653e\u7f6e\u4e0b\u4e00\u884c\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n            state[row][col] = '#';\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nfunction nQueens(n: number): string[][][] {\n    // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n    const state = Array.from({ length: n }, () => Array(n).fill('#'));\n    const cols = Array(n).fill(false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n    const diags1 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    const diags2 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    const res: string[][][] = [];\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n    return res;\n}\n
        n_queens.dart
        /* \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e */\nvoid backtrack(\n  int row,\n  int n,\n  List<List<String>> state,\n  List<List<List<String>>> res,\n  List<bool> cols,\n  List<bool> diags1,\n  List<bool> diags2,\n) {\n  // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n  if (row == n) {\n    List<List<String>> copyState = [];\n    for (List<String> sRow in state) {\n      copyState.add(List.from(sRow));\n    }\n    res.add(copyState);\n    return;\n  }\n  // \u904d\u5386\u6240\u6709\u5217\n  for (int col = 0; col < n; col++) {\n    // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n    int diag1 = row - col + n - 1;\n    int diag2 = row + col;\n    // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n    if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n      // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n      state[row][col] = \"Q\";\n      cols[col] = true;\n      diags1[diag1] = true;\n      diags2[diag2] = true;\n      // \u653e\u7f6e\u4e0b\u4e00\u884c\n      backtrack(row + 1, n, state, res, cols, diags1, diags2);\n      // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n      state[row][col] = \"#\";\n      cols[col] = false;\n      diags1[diag1] = false;\n      diags2[diag2] = false;\n    }\n  }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nList<List<List<String>>> nQueens(int n) {\n  // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n  List<List<String>> state = List.generate(n, (index) => List.filled(n, \"#\"));\n  List<bool> cols = List.filled(n, false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n  List<bool> diags1 = List.filled(2 * n - 1, false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n  List<bool> diags2 = List.filled(2 * n - 1, false); // \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n  List<List<List<String>>> res = [];\n\n  backtrack(0, n, state, res, cols, diags1, diags2);\n\n  return res;\n}\n
        n_queens.rs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e */\nfn backtrack(\n    row: usize,\n    n: usize,\n    state: &mut Vec<Vec<String>>,\n    res: &mut Vec<Vec<Vec<String>>>,\n    cols: &mut [bool],\n    diags1: &mut [bool],\n    diags2: &mut [bool],\n) {\n    // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if row == n {\n        res.push(state.clone());\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u5217\n    for col in 0..n {\n        // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n        let diag1 = row + n - 1 - col;\n        let diag2 = row + col;\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n        if !cols[col] && !diags1[diag1] && !diags2[diag2] {\n            // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n            state[row][col] = \"Q\".into();\n            (cols[col], diags1[diag1], diags2[diag2]) = (true, true, true);\n            // \u653e\u7f6e\u4e0b\u4e00\u884c\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n            state[row][col] = \"#\".into();\n            (cols[col], diags1[diag1], diags2[diag2]) = (false, false, false);\n        }\n    }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nfn n_queens(n: usize) -> Vec<Vec<Vec<String>>> {\n    // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n    let mut state: Vec<Vec<String>> = vec![vec![\"#\".to_string(); n]; n];\n    let mut cols = vec![false; n]; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n    let mut diags1 = vec![false; 2 * n - 1]; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    let mut diags2 = vec![false; 2 * n - 1]; // \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    let mut res: Vec<Vec<Vec<String>>> = Vec::new();\n\n    backtrack(\n        0,\n        n,\n        &mut state,\n        &mut res,\n        &mut cols,\n        &mut diags1,\n        &mut diags2,\n    );\n\n    res\n}\n
        n_queens.c
        /* \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e */\nvoid backtrack(int row, int n, char state[MAX_SIZE][MAX_SIZE], char ***res, int *resSize, bool cols[MAX_SIZE],\n               bool diags1[2 * MAX_SIZE - 1], bool diags2[2 * MAX_SIZE - 1]) {\n    // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (row == n) {\n        res[*resSize] = (char **)malloc(sizeof(char *) * n);\n        for (int i = 0; i < n; ++i) {\n            res[*resSize][i] = (char *)malloc(sizeof(char) * (n + 1));\n            strcpy(res[*resSize][i], state[i]);\n        }\n        (*resSize)++;\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u5217\n    for (int col = 0; col < n; col++) {\n        // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n            state[row][col] = 'Q';\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // \u653e\u7f6e\u4e0b\u4e00\u884c\n            backtrack(row + 1, n, state, res, resSize, cols, diags1, diags2);\n            // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n            state[row][col] = '#';\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nchar ***nQueens(int n, int *returnSize) {\n    char state[MAX_SIZE][MAX_SIZE];\n    // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n    for (int i = 0; i < n; ++i) {\n        for (int j = 0; j < n; ++j) {\n            state[i][j] = '#';\n        }\n        state[i][n] = '\\0';\n    }\n    bool cols[MAX_SIZE] = {false};           // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n    bool diags1[2 * MAX_SIZE - 1] = {false}; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    bool diags2[2 * MAX_SIZE - 1] = {false}; // \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n\n    char ***res = (char ***)malloc(sizeof(char **) * MAX_SIZE);\n    *returnSize = 0;\n    backtrack(0, n, state, res, returnSize, cols, diags1, diags2);\n    return res;\n}\n
        n_queens.kt
        /* \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e */\nfun backtrack(\n    row: Int,\n    n: Int,\n    state: MutableList<MutableList<String>>,\n    res: MutableList<MutableList<MutableList<String>>?>,\n    cols: BooleanArray,\n    diags1: BooleanArray,\n    diags2: BooleanArray\n) {\n    // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (row == n) {\n        val copyState = mutableListOf<MutableList<String>>()\n        for (sRow in state) {\n            copyState.add(sRow.toMutableList())\n        }\n        res.add(copyState)\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u5217\n    for (col in 0..<n) {\n        // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n        val diag1 = row - col + n - 1\n        val diag2 = row + col\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n            state[row][col] = \"Q\"\n            diags2[diag2] = true\n            diags1[diag1] = diags2[diag2]\n            cols[col] = diags1[diag1]\n            // \u653e\u7f6e\u4e0b\u4e00\u884c\n            backtrack(row + 1, n, state, res, cols, diags1, diags2)\n            // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n            state[row][col] = \"#\"\n            diags2[diag2] = false\n            diags1[diag1] = diags2[diag2]\n            cols[col] = diags1[diag1]\n        }\n    }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nfun nQueens(n: Int): MutableList<MutableList<MutableList<String>>?> {\n    // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n    val state = mutableListOf<MutableList<String>>()\n    for (i in 0..<n) {\n        val row = mutableListOf<String>()\n        for (j in 0..<n) {\n            row.add(\"#\")\n        }\n        state.add(row)\n    }\n    val cols = BooleanArray(n) // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n    val diags1 = BooleanArray(2 * n - 1) // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    val diags2 = BooleanArray(2 * n - 1) // \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    val res = mutableListOf<MutableList<MutableList<String>>?>()\n\n    backtrack(0, n, state, res, cols, diags1, diags2)\n\n    return res\n}\n
        n_queens.rb
        ### \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e ###\ndef backtrack(row, n, state, res, cols, diags1, diags2)\n  # \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n  if row == n\n    res << state.map { |row| row.dup }\n    return\n  end\n\n  # \u904d\u5386\u6240\u6709\u5217\n  for col in 0...n\n    # \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n    diag1 = row - col + n - 1\n    diag2 = row + col\n    # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n    if !cols[col] && !diags1[diag1] && !diags2[diag2]\n      # \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n      state[row][col] = \"Q\"\n      cols[col] = diags1[diag1] = diags2[diag2] = true\n      # \u653e\u7f6e\u4e0b\u4e00\u884c\n      backtrack(row + 1, n, state, res, cols, diags1, diags2)\n      # \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n      state[row][col] = \"#\"\n      cols[col] = diags1[diag1] = diags2[diag2] = false\n    end\n  end\nend\n\n### \u6c42\u89e3 n \u7687\u540e ###\ndef n_queens(n)\n  # \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n  state = Array.new(n) { Array.new(n, \"#\") }\n  cols = Array.new(n, false) # \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n  diags1 = Array.new(2 * n - 1, false) # \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n  diags2 = Array.new(2 * n - 1, false) # \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n  res = []\n  backtrack(0, n, state, res, cols, diags1, diags2)\n\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u9010\u884c\u653e\u7f6e \\(n\\) \u6b21\uff0c\u8003\u8651\u5217\u7ea6\u675f\uff0c\u5219\u4ece\u7b2c\u4e00\u884c\u5230\u6700\u540e\u4e00\u884c\u5206\u522b\u6709 \\(n\\)\u3001\\(n-1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u4e2a\u9009\u62e9\uff0c\u4f7f\u7528 \\(O(n!)\\) \u65f6\u95f4\u3002\u5f53\u8bb0\u5f55\u89e3\u65f6\uff0c\u9700\u8981\u590d\u5236\u77e9\u9635 state \u5e76\u6dfb\u52a0\u8fdb res \uff0c\u590d\u5236\u64cd\u4f5c\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002\u56e0\u6b64\uff0c\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n! \\cdot n^2)\\) \u3002\u5b9e\u9645\u4e0a\uff0c\u6839\u636e\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u526a\u679d\u4e5f\u80fd\u591f\u5927\u5e45\u7f29\u5c0f\u641c\u7d22\u7a7a\u95f4\uff0c\u56e0\u800c\u641c\u7d22\u6548\u7387\u5f80\u5f80\u4f18\u4e8e\u4ee5\u4e0a\u65f6\u95f4\u590d\u6742\u5ea6\u3002

        \u6570\u7ec4 state \u4f7f\u7528 \\(O(n^2)\\) \u7a7a\u95f4\uff0c\u6570\u7ec4 cols\u3001diags1 \u548c diags2 \u7686\u4f7f\u7528 \\(O(n)\\) \u7a7a\u95f4\u3002\u6700\u5927\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002

        "},{"location":"chapter_backtracking/permutations_problem/","title":"13.2 \u00a0 \u5168\u6392\u5217\u95ee\u9898","text":"

        \u5168\u6392\u5217\u95ee\u9898\u662f\u56de\u6eaf\u7b97\u6cd5\u7684\u4e00\u4e2a\u5178\u578b\u5e94\u7528\u3002\u5b83\u7684\u5b9a\u4e49\u662f\u5728\u7ed9\u5b9a\u4e00\u4e2a\u96c6\u5408\uff08\u5982\u4e00\u4e2a\u6570\u7ec4\u6216\u5b57\u7b26\u4e32\uff09\u7684\u60c5\u51b5\u4e0b\uff0c\u627e\u51fa\u5176\u4e2d\u5143\u7d20\u7684\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u3002

        \u8868 13-2 \u5217\u4e3e\u4e86\u51e0\u4e2a\u793a\u4f8b\u6570\u636e\uff0c\u5305\u62ec\u8f93\u5165\u6570\u7ec4\u548c\u5bf9\u5e94\u7684\u6240\u6709\u6392\u5217\u3002

        \u8868 13-2 \u00a0 \u5168\u6392\u5217\u793a\u4f8b

        \u8f93\u5165\u6570\u7ec4 \u6240\u6709\u6392\u5217 \\([1]\\) \\([1]\\) \\([1, 2]\\) \\([1, 2], [2, 1]\\) \\([1, 2, 3]\\) \\([1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]\\)"},{"location":"chapter_backtracking/permutations_problem/#1321","title":"13.2.1 \u00a0 \u65e0\u76f8\u7b49\u5143\u7d20\u7684\u60c5\u51b5","text":"

        Question

        \u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u5176\u4e2d\u4e0d\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u8fd4\u56de\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u3002

        \u4ece\u56de\u6eaf\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u751f\u6210\u6392\u5217\u7684\u8fc7\u7a0b\u60f3\u8c61\u6210\u4e00\u7cfb\u5217\u9009\u62e9\u7684\u7ed3\u679c\u3002\u5047\u8bbe\u8f93\u5165\u6570\u7ec4\u4e3a \\([1, 2, 3]\\) \uff0c\u5982\u679c\u6211\u4eec\u5148\u9009\u62e9 \\(1\\) \uff0c\u518d\u9009\u62e9 \\(3\\) \uff0c\u6700\u540e\u9009\u62e9 \\(2\\) \uff0c\u5219\u83b7\u5f97\u6392\u5217 \\([1, 3, 2]\\) \u3002\u56de\u9000\u8868\u793a\u64a4\u9500\u4e00\u4e2a\u9009\u62e9\uff0c\u4e4b\u540e\u7ee7\u7eed\u5c1d\u8bd5\u5176\u4ed6\u9009\u62e9\u3002

        \u4ece\u56de\u6eaf\u4ee3\u7801\u7684\u89d2\u5ea6\u770b\uff0c\u5019\u9009\u96c6\u5408 choices \u662f\u8f93\u5165\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u72b6\u6001 state \u662f\u76f4\u81f3\u76ee\u524d\u5df2\u88ab\u9009\u62e9\u7684\u5143\u7d20\u3002\u8bf7\u6ce8\u610f\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ea\u5141\u8bb8\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u56e0\u6b64 state \u4e2d\u7684\u6240\u6709\u5143\u7d20\u90fd\u5e94\u8be5\u662f\u552f\u4e00\u7684\u3002

        \u5982\u56fe 13-5 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u641c\u7d22\u8fc7\u7a0b\u5c55\u5f00\u6210\u4e00\u68f5\u9012\u5f52\u6811\uff0c\u6811\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u5f53\u524d\u72b6\u6001 state \u3002\u4ece\u6839\u8282\u70b9\u5f00\u59cb\uff0c\u7ecf\u8fc7\u4e09\u8f6e\u9009\u62e9\u540e\u5230\u8fbe\u53f6\u8282\u70b9\uff0c\u6bcf\u4e2a\u53f6\u8282\u70b9\u90fd\u5bf9\u5e94\u4e00\u4e2a\u6392\u5217\u3002

        \u56fe 13-5 \u00a0 \u5168\u6392\u5217\u7684\u9012\u5f52\u6811

        "},{"location":"chapter_backtracking/permutations_problem/#1","title":"1. \u00a0 \u91cd\u590d\u9009\u62e9\u526a\u679d","text":"

        \u4e3a\u4e86\u5b9e\u73b0\u6bcf\u4e2a\u5143\u7d20\u53ea\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u6211\u4eec\u8003\u8651\u5f15\u5165\u4e00\u4e2a\u5e03\u5c14\u578b\u6570\u7ec4 selected \uff0c\u5176\u4e2d selected[i] \u8868\u793a choices[i] \u662f\u5426\u5df2\u88ab\u9009\u62e9\uff0c\u5e76\u57fa\u4e8e\u5b83\u5b9e\u73b0\u4ee5\u4e0b\u526a\u679d\u64cd\u4f5c\u3002

        • \u5728\u505a\u51fa\u9009\u62e9 choice[i] \u540e\uff0c\u6211\u4eec\u5c31\u5c06 selected[i] \u8d4b\u503c\u4e3a \\(\\text{True}\\) \uff0c\u4ee3\u8868\u5b83\u5df2\u88ab\u9009\u62e9\u3002
        • \u904d\u5386\u9009\u62e9\u5217\u8868 choices \u65f6\uff0c\u8df3\u8fc7\u6240\u6709\u5df2\u88ab\u9009\u62e9\u7684\u8282\u70b9\uff0c\u5373\u526a\u679d\u3002

        \u5982\u56fe 13-6 \u6240\u793a\uff0c\u5047\u8bbe\u6211\u4eec\u7b2c\u4e00\u8f6e\u9009\u62e9 1 \uff0c\u7b2c\u4e8c\u8f6e\u9009\u62e9 3 \uff0c\u7b2c\u4e09\u8f6e\u9009\u62e9 2 \uff0c\u5219\u9700\u8981\u5728\u7b2c\u4e8c\u8f6e\u526a\u6389\u5143\u7d20 1 \u7684\u5206\u652f\uff0c\u5728\u7b2c\u4e09\u8f6e\u526a\u6389\u5143\u7d20 1 \u548c\u5143\u7d20 3 \u7684\u5206\u652f\u3002

        \u56fe 13-6 \u00a0 \u5168\u6392\u5217\u526a\u679d\u793a\u4f8b

        \u89c2\u5bdf\u56fe 13-6 \u53d1\u73b0\uff0c\u8be5\u526a\u679d\u64cd\u4f5c\u5c06\u641c\u7d22\u7a7a\u95f4\u5927\u5c0f\u4ece \\(O(n^n)\\) \u51cf\u5c0f\u81f3 \\(O(n!)\\) \u3002

        "},{"location":"chapter_backtracking/permutations_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u60f3\u6e05\u695a\u4ee5\u4e0a\u4fe1\u606f\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u6846\u67b6\u4ee3\u7801\u4e2d\u505a\u201c\u5b8c\u5f62\u586b\u7a7a\u201d\u4e86\u3002\u4e3a\u4e86\u7f29\u77ed\u6574\u4f53\u4ee3\u7801\uff0c\u6211\u4eec\u4e0d\u5355\u72ec\u5b9e\u73b0\u6846\u67b6\u4ee3\u7801\u4e2d\u7684\u5404\u4e2a\u51fd\u6570\uff0c\u800c\u662f\u5c06\u5b83\u4eec\u5c55\u5f00\u5728 backtrack() \u51fd\u6570\u4e2d\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby permutations_i.py
        def backtrack(\n    state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]\n):\n    \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I\"\"\"\n    # \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if len(state) == len(choices):\n        res.append(list(state))\n        return\n    # \u904d\u5386\u6240\u6709\u9009\u62e9\n    for i, choice in enumerate(choices):\n        # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n        if not selected[i]:\n            # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            selected[i] = True\n            state.append(choice)\n            # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res)\n            # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = False\n            state.pop()\n\ndef permutations_i(nums: list[int]) -> list[list[int]]:\n    \"\"\"\u5168\u6392\u5217 I\"\"\"\n    res = []\n    backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\n    return res\n
        permutations_i.cpp
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &selected, vector<vector<int>> &res) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.size() == choices.size()) {\n        res.push_back(state);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (int i = 0; i < choices.size(); i++) {\n        int choice = choices[i];\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n        if (!selected[i]) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            selected[i] = true;\n            state.push_back(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.pop_back();\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 I */\nvector<vector<int>> permutationsI(vector<int> nums) {\n    vector<int> state;\n    vector<bool> selected(nums.size(), false);\n    vector<vector<int>> res;\n    backtrack(state, nums, selected, res);\n    return res;\n}\n
        permutations_i.java
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.size() == choices.length) {\n        res.add(new ArrayList<Integer>(state));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (int i = 0; i < choices.length; i++) {\n        int choice = choices[i];\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n        if (!selected[i]) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            selected[i] = true;\n            state.add(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.remove(state.size() - 1);\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 I */\nList<List<Integer>> permutationsI(int[] nums) {\n    List<List<Integer>> res = new ArrayList<List<Integer>>();\n    backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);\n    return res;\n}\n
        permutations_i.cs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.Count == choices.Length) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (int i = 0; i < choices.Length; i++) {\n        int choice = choices[i];\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n        if (!selected[i]) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            selected[i] = true;\n            state.Add(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            Backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.RemoveAt(state.Count - 1);\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 I */\nList<List<int>> PermutationsI(int[] nums) {\n    List<List<int>> res = [];\n    Backtrack([], nums, new bool[nums.Length], res);\n    return res;\n}\n
        permutations_i.go
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunc backtrackI(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if len(*state) == len(*choices) {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for i := 0; i < len(*choices); i++ {\n        choice := (*choices)[i]\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n        if !(*selected)[i] {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            (*selected)[i] = true\n            *state = append(*state, choice)\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrackI(state, choices, selected, res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            (*selected)[i] = false\n            *state = (*state)[:len(*state)-1]\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 I */\nfunc permutationsI(nums []int) [][]int {\n    res := make([][]int, 0)\n    state := make([]int, 0)\n    selected := make([]bool, len(nums))\n    backtrackI(&state, &nums, &selected, &res)\n    return res\n}\n
        permutations_i.swift
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunc backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if state.count == choices.count {\n        res.append(state)\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (i, choice) in choices.enumerated() {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n        if !selected[i] {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            selected[i] = true\n            state.append(choice)\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state: &state, choices: choices, selected: &selected, res: &res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false\n            state.removeLast()\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 I */\nfunc permutationsI(nums: [Int]) -> [[Int]] {\n    var state: [Int] = []\n    var selected = Array(repeating: false, count: nums.count)\n    var res: [[Int]] = []\n    backtrack(state: &state, choices: nums, selected: &selected, res: &res)\n    return res\n}\n
        permutations_i.js
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunction backtrack(state, choices, selected, res) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.length === choices.length) {\n        res.push([...state]);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    choices.forEach((choice, i) => {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n        if (!selected[i]) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            selected[i] = true;\n            state.push(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.pop();\n        }\n    });\n}\n\n/* \u5168\u6392\u5217 I */\nfunction permutationsI(nums) {\n    const res = [];\n    backtrack([], nums, Array(nums.length).fill(false), res);\n    return res;\n}\n
        permutations_i.ts
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunction backtrack(\n    state: number[],\n    choices: number[],\n    selected: boolean[],\n    res: number[][]\n): void {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.length === choices.length) {\n        res.push([...state]);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    choices.forEach((choice, i) => {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n        if (!selected[i]) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            selected[i] = true;\n            state.push(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.pop();\n        }\n    });\n}\n\n/* \u5168\u6392\u5217 I */\nfunction permutationsI(nums: number[]): number[][] {\n    const res: number[][] = [];\n    backtrack([], nums, Array(nums.length).fill(false), res);\n    return res;\n}\n
        permutations_i.dart
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(\n  List<int> state,\n  List<int> choices,\n  List<bool> selected,\n  List<List<int>> res,\n) {\n  // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n  if (state.length == choices.length) {\n    res.add(List.from(state));\n    return;\n  }\n  // \u904d\u5386\u6240\u6709\u9009\u62e9\n  for (int i = 0; i < choices.length; i++) {\n    int choice = choices[i];\n    // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n    if (!selected[i]) {\n      // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n      selected[i] = true;\n      state.add(choice);\n      // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n      backtrack(state, choices, selected, res);\n      // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n      selected[i] = false;\n      state.removeLast();\n    }\n  }\n}\n\n/* \u5168\u6392\u5217 I */\nList<List<int>> permutationsI(List<int> nums) {\n  List<List<int>> res = [];\n  backtrack([], nums, List.filled(nums.length, false), res);\n  return res;\n}\n
        permutations_i.rs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if state.len() == choices.len() {\n        res.push(state);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for i in 0..choices.len() {\n        let choice = choices[i];\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n        if !selected[i] {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            selected[i] = true;\n            state.push(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state.clone(), choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.pop();\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 I */\nfn permutations_i(nums: &mut [i32]) -> Vec<Vec<i32>> {\n    let mut res = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    backtrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);\n    res\n}\n
        permutations_i.c
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(int *state, int stateSize, int *choices, int choicesSize, bool *selected, int **res, int *resSize) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (stateSize == choicesSize) {\n        res[*resSize] = (int *)malloc(choicesSize * sizeof(int));\n        for (int i = 0; i < choicesSize; i++) {\n            res[*resSize][i] = state[i];\n        }\n        (*resSize)++;\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (int i = 0; i < choicesSize; i++) {\n        int choice = choices[i];\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n        if (!selected[i]) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            selected[i] = true;\n            state[stateSize] = choice;\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, stateSize + 1, choices, choicesSize, selected, res, resSize);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 I */\nint **permutationsI(int *nums, int numsSize, int *returnSize) {\n    int *state = (int *)malloc(numsSize * sizeof(int));\n    bool *selected = (bool *)malloc(numsSize * sizeof(bool));\n    for (int i = 0; i < numsSize; i++) {\n        selected[i] = false;\n    }\n    int **res = (int **)malloc(MAX_SIZE * sizeof(int *));\n    *returnSize = 0;\n\n    backtrack(state, 0, nums, numsSize, selected, res, returnSize);\n\n    free(state);\n    free(selected);\n\n    return res;\n}\n
        permutations_i.kt
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfun backtrack(\n    state: MutableList<Int>,\n    choices: IntArray,\n    selected: BooleanArray,\n    res: MutableList<MutableList<Int>?>\n) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.size == choices.size) {\n        res.add(state.toMutableList())\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (i in choices.indices) {\n        val choice = choices[i]\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n        if (!selected[i]) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            selected[i] = true\n            state.add(choice)\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false\n            state.removeAt(state.size - 1)\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 I */\nfun permutationsI(nums: IntArray): MutableList<MutableList<Int>?> {\n    val res = mutableListOf<MutableList<Int>?>()\n    backtrack(mutableListOf(), nums, BooleanArray(nums.size), res)\n    return res\n}\n
        permutations_i.rb
        ### \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I ###\ndef backtrack(state, choices, selected, res)\n  # \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n  if state.length == choices.length\n    res << state.dup\n    return\n  end\n\n  # \u904d\u5386\u6240\u6709\u9009\u62e9\n  choices.each_with_index do |choice, i|\n    # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n    unless selected[i]\n      # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n      selected[i] = true\n      state << choice\n      # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n      backtrack(state, choices, selected, res)\n      # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n      selected[i] = false\n      state.pop\n    end\n  end\nend\n\n### \u5168\u6392\u5217 I ###\ndef permutations_i(nums)\n  res = []\n  backtrack([], nums, Array.new(nums.length, false), res)\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_backtracking/permutations_problem/#1322","title":"13.2.2 \u00a0 \u8003\u8651\u76f8\u7b49\u5143\u7d20\u7684\u60c5\u51b5","text":"

        Question

        \u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u8fd4\u56de\u6240\u6709\u4e0d\u91cd\u590d\u7684\u6392\u5217\u3002

        \u5047\u8bbe\u8f93\u5165\u6570\u7ec4\u4e3a \\([1, 1, 2]\\) \u3002\u4e3a\u4e86\u65b9\u4fbf\u533a\u5206\u4e24\u4e2a\u91cd\u590d\u5143\u7d20 \\(1\\) \uff0c\u6211\u4eec\u5c06\u7b2c\u4e8c\u4e2a \\(1\\) \u8bb0\u4e3a \\(\\hat{1}\\) \u3002

        \u5982\u56fe 13-7 \u6240\u793a\uff0c\u4e0a\u8ff0\u65b9\u6cd5\u751f\u6210\u7684\u6392\u5217\u6709\u4e00\u534a\u662f\u91cd\u590d\u7684\u3002

        \u56fe 13-7 \u00a0 \u91cd\u590d\u6392\u5217

        \u90a3\u4e48\u5982\u4f55\u53bb\u9664\u91cd\u590d\u7684\u6392\u5217\u5462\uff1f\u6700\u76f4\u63a5\u5730\uff0c\u8003\u8651\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u96c6\u5408\uff0c\u76f4\u63a5\u5bf9\u6392\u5217\u7ed3\u679c\u8fdb\u884c\u53bb\u91cd\u3002\u7136\u800c\u8fd9\u6837\u505a\u4e0d\u591f\u4f18\u96c5\uff0c\u56e0\u4e3a\u751f\u6210\u91cd\u590d\u6392\u5217\u7684\u641c\u7d22\u5206\u652f\u6ca1\u6709\u5fc5\u8981\uff0c\u5e94\u5f53\u63d0\u524d\u8bc6\u522b\u5e76\u526a\u679d\uff0c\u8fd9\u6837\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u5347\u7b97\u6cd5\u6548\u7387\u3002

        "},{"location":"chapter_backtracking/permutations_problem/#1_1","title":"1. \u00a0 \u76f8\u7b49\u5143\u7d20\u526a\u679d","text":"

        \u89c2\u5bdf\u56fe 13-8 \uff0c\u5728\u7b2c\u4e00\u8f6e\u4e2d\uff0c\u9009\u62e9 \\(1\\) \u6216\u9009\u62e9 \\(\\hat{1}\\) \u662f\u7b49\u4ef7\u7684\uff0c\u5728\u8fd9\u4e24\u4e2a\u9009\u62e9\u4e4b\u4e0b\u751f\u6210\u7684\u6240\u6709\u6392\u5217\u90fd\u662f\u91cd\u590d\u7684\u3002\u56e0\u6b64\u5e94\u8be5\u628a \\(\\hat{1}\\) \u526a\u679d\u3002

        \u540c\u7406\uff0c\u5728\u7b2c\u4e00\u8f6e\u9009\u62e9 \\(2\\) \u4e4b\u540e\uff0c\u7b2c\u4e8c\u8f6e\u9009\u62e9\u4e2d\u7684 \\(1\\) \u548c \\(\\hat{1}\\) \u4e5f\u4f1a\u4ea7\u751f\u91cd\u590d\u5206\u652f\uff0c\u56e0\u6b64\u4e5f\u5e94\u5c06\u7b2c\u4e8c\u8f6e\u7684 \\(\\hat{1}\\) \u526a\u679d\u3002

        \u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u6211\u4eec\u7684\u76ee\u6807\u662f\u5728\u67d0\u4e00\u8f6e\u9009\u62e9\u4e2d\uff0c\u4fdd\u8bc1\u591a\u4e2a\u76f8\u7b49\u7684\u5143\u7d20\u4ec5\u88ab\u9009\u62e9\u4e00\u6b21\u3002

        \u56fe 13-8 \u00a0 \u91cd\u590d\u6392\u5217\u526a\u679d

        "},{"location":"chapter_backtracking/permutations_problem/#2_1","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u5728\u4e0a\u4e00\u9898\u7684\u4ee3\u7801\u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u8003\u8651\u5728\u6bcf\u4e00\u8f6e\u9009\u62e9\u4e2d\u5f00\u542f\u4e00\u4e2a\u54c8\u5e0c\u96c6\u5408 duplicated \uff0c\u7528\u4e8e\u8bb0\u5f55\u8be5\u8f6e\u4e2d\u5df2\u7ecf\u5c1d\u8bd5\u8fc7\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u91cd\u590d\u5143\u7d20\u526a\u679d\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby permutations_ii.py
        def backtrack(\n    state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]\n):\n    \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II\"\"\"\n    # \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if len(state) == len(choices):\n        res.append(list(state))\n        return\n    # \u904d\u5386\u6240\u6709\u9009\u62e9\n    duplicated = set[int]()\n    for i, choice in enumerate(choices):\n        # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n        if not selected[i] and choice not in duplicated:\n            # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            duplicated.add(choice)  # \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n            selected[i] = True\n            state.append(choice)\n            # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res)\n            # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = False\n            state.pop()\n\ndef permutations_ii(nums: list[int]) -> list[list[int]]:\n    \"\"\"\u5168\u6392\u5217 II\"\"\"\n    res = []\n    backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\n    return res\n
        permutations_ii.cpp
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &selected, vector<vector<int>> &res) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.size() == choices.size()) {\n        res.push_back(state);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    unordered_set<int> duplicated;\n    for (int i = 0; i < choices.size(); i++) {\n        int choice = choices[i];\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n        if (!selected[i] && duplicated.find(choice) == duplicated.end()) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            duplicated.emplace(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n            selected[i] = true;\n            state.push_back(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.pop_back();\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 II */\nvector<vector<int>> permutationsII(vector<int> nums) {\n    vector<int> state;\n    vector<bool> selected(nums.size(), false);\n    vector<vector<int>> res;\n    backtrack(state, nums, selected, res);\n    return res;\n}\n
        permutations_ii.java
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.size() == choices.length) {\n        res.add(new ArrayList<Integer>(state));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    Set<Integer> duplicated = new HashSet<Integer>();\n    for (int i = 0; i < choices.length; i++) {\n        int choice = choices[i];\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n        if (!selected[i] && !duplicated.contains(choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            duplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n            selected[i] = true;\n            state.add(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.remove(state.size() - 1);\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 II */\nList<List<Integer>> permutationsII(int[] nums) {\n    List<List<Integer>> res = new ArrayList<List<Integer>>();\n    backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);\n    return res;\n}\n
        permutations_ii.cs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.Count == choices.Length) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    HashSet<int> duplicated = [];\n    for (int i = 0; i < choices.Length; i++) {\n        int choice = choices[i];\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n        if (!selected[i] && !duplicated.Contains(choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            duplicated.Add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n            selected[i] = true;\n            state.Add(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            Backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.RemoveAt(state.Count - 1);\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 II */\nList<List<int>> PermutationsII(int[] nums) {\n    List<List<int>> res = [];\n    Backtrack([], nums, new bool[nums.Length], res);\n    return res;\n}\n
        permutations_ii.go
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunc backtrackII(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if len(*state) == len(*choices) {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    duplicated := make(map[int]struct{}, 0)\n    for i := 0; i < len(*choices); i++ {\n        choice := (*choices)[i]\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n        if _, ok := duplicated[choice]; !ok && !(*selected)[i] {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n            duplicated[choice] = struct{}{}\n            (*selected)[i] = true\n            *state = append(*state, choice)\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrackII(state, choices, selected, res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            (*selected)[i] = false\n            *state = (*state)[:len(*state)-1]\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 II */\nfunc permutationsII(nums []int) [][]int {\n    res := make([][]int, 0)\n    state := make([]int, 0)\n    selected := make([]bool, len(nums))\n    backtrackII(&state, &nums, &selected, &res)\n    return res\n}\n
        permutations_ii.swift
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunc backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if state.count == choices.count {\n        res.append(state)\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    var duplicated: Set<Int> = []\n    for (i, choice) in choices.enumerated() {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n        if !selected[i], !duplicated.contains(choice) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            duplicated.insert(choice) // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n            selected[i] = true\n            state.append(choice)\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state: &state, choices: choices, selected: &selected, res: &res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false\n            state.removeLast()\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 II */\nfunc permutationsII(nums: [Int]) -> [[Int]] {\n    var state: [Int] = []\n    var selected = Array(repeating: false, count: nums.count)\n    var res: [[Int]] = []\n    backtrack(state: &state, choices: nums, selected: &selected, res: &res)\n    return res\n}\n
        permutations_ii.js
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunction backtrack(state, choices, selected, res) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.length === choices.length) {\n        res.push([...state]);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    const duplicated = new Set();\n    choices.forEach((choice, i) => {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n        if (!selected[i] && !duplicated.has(choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            duplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n            selected[i] = true;\n            state.push(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.pop();\n        }\n    });\n}\n\n/* \u5168\u6392\u5217 II */\nfunction permutationsII(nums) {\n    const res = [];\n    backtrack([], nums, Array(nums.length).fill(false), res);\n    return res;\n}\n
        permutations_ii.ts
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunction backtrack(\n    state: number[],\n    choices: number[],\n    selected: boolean[],\n    res: number[][]\n): void {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.length === choices.length) {\n        res.push([...state]);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    const duplicated = new Set();\n    choices.forEach((choice, i) => {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n        if (!selected[i] && !duplicated.has(choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            duplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n            selected[i] = true;\n            state.push(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.pop();\n        }\n    });\n}\n\n/* \u5168\u6392\u5217 II */\nfunction permutationsII(nums: number[]): number[][] {\n    const res: number[][] = [];\n    backtrack([], nums, Array(nums.length).fill(false), res);\n    return res;\n}\n
        permutations_ii.dart
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(\n  List<int> state,\n  List<int> choices,\n  List<bool> selected,\n  List<List<int>> res,\n) {\n  // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n  if (state.length == choices.length) {\n    res.add(List.from(state));\n    return;\n  }\n  // \u904d\u5386\u6240\u6709\u9009\u62e9\n  Set<int> duplicated = {};\n  for (int i = 0; i < choices.length; i++) {\n    int choice = choices[i];\n    // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n    if (!selected[i] && !duplicated.contains(choice)) {\n      // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n      duplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n      selected[i] = true;\n      state.add(choice);\n      // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n      backtrack(state, choices, selected, res);\n      // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n      selected[i] = false;\n      state.removeLast();\n    }\n  }\n}\n\n/* \u5168\u6392\u5217 II */\nList<List<int>> permutationsII(List<int> nums) {\n  List<List<int>> res = [];\n  backtrack([], nums, List.filled(nums.length, false), res);\n  return res;\n}\n
        permutations_ii.rs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if state.len() == choices.len() {\n        res.push(state);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    let mut duplicated = HashSet::<i32>::new();\n    for i in 0..choices.len() {\n        let choice = choices[i];\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n        if !selected[i] && !duplicated.contains(&choice) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            duplicated.insert(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n            selected[i] = true;\n            state.push(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state.clone(), choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.pop();\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 II */\nfn permutations_ii(nums: &mut [i32]) -> Vec<Vec<i32>> {\n    let mut res = Vec::new();\n    backtrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);\n    res\n}\n
        permutations_ii.c
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(int *state, int stateSize, int *choices, int choicesSize, bool *selected, int **res, int *resSize) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (stateSize == choicesSize) {\n        res[*resSize] = (int *)malloc(choicesSize * sizeof(int));\n        for (int i = 0; i < choicesSize; i++) {\n            res[*resSize][i] = state[i];\n        }\n        (*resSize)++;\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    bool duplicated[MAX_SIZE] = {false};\n    for (int i = 0; i < choicesSize; i++) {\n        int choice = choices[i];\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n        if (!selected[i] && !duplicated[choice]) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            duplicated[choice] = true; // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n            selected[i] = true;\n            state[stateSize] = choice;\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, stateSize + 1, choices, choicesSize, selected, res, resSize);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 II */\nint **permutationsII(int *nums, int numsSize, int *returnSize) {\n    int *state = (int *)malloc(numsSize * sizeof(int));\n    bool *selected = (bool *)malloc(numsSize * sizeof(bool));\n    for (int i = 0; i < numsSize; i++) {\n        selected[i] = false;\n    }\n    int **res = (int **)malloc(MAX_SIZE * sizeof(int *));\n    *returnSize = 0;\n\n    backtrack(state, 0, nums, numsSize, selected, res, returnSize);\n\n    free(state);\n    free(selected);\n\n    return res;\n}\n
        permutations_ii.kt
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfun backtrack(\n    state: MutableList<Int>,\n    choices: IntArray,\n    selected: BooleanArray,\n    res: MutableList<MutableList<Int>?>\n) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.size == choices.size) {\n        res.add(state.toMutableList())\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    val duplicated = HashSet<Int>()\n    for (i in choices.indices) {\n        val choice = choices[i]\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n        if (!selected[i] && !duplicated.contains(choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            duplicated.add(choice) // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n            selected[i] = true\n            state.add(choice)\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false\n            state.removeAt(state.size - 1)\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 II */\nfun permutationsII(nums: IntArray): MutableList<MutableList<Int>?> {\n    val res = mutableListOf<MutableList<Int>?>()\n    backtrack(mutableListOf(), nums, BooleanArray(nums.size), res)\n    return res\n}\n
        permutations_ii.rb
        ### \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II ###\ndef backtrack(state, choices, selected, res)\n  # \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n  if state.length == choices.length\n    res << state.dup\n    return\n  end\n\n  # \u904d\u5386\u6240\u6709\u9009\u62e9\n  duplicated = Set.new\n  choices.each_with_index do |choice, i|\n    # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n    if !selected[i] && !duplicated.include?(choice)\n      # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n      duplicated.add(choice)\n      selected[i] = true\n      state << choice\n      # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n      backtrack(state, choices, selected, res)\n      # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n      selected[i] = false\n      state.pop\n    end\n  end\nend\n\n### \u5168\u6392\u5217 II ###\ndef permutations_ii(nums)\n  res = []\n  backtrack([], nums, Array.new(nums.length, false), res)\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5047\u8bbe\u5143\u7d20\u4e24\u4e24\u4e4b\u95f4\u4e92\u4e0d\u76f8\u540c\uff0c\u5219 \\(n\\) \u4e2a\u5143\u7d20\u5171\u6709 \\(n!\\) \u79cd\u6392\u5217\uff08\u9636\u4e58\uff09\uff1b\u5728\u8bb0\u5f55\u7ed3\u679c\u65f6\uff0c\u9700\u8981\u590d\u5236\u957f\u5ea6\u4e3a \\(n\\) \u7684\u5217\u8868\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n!n)\\) \u3002

        \u6700\u5927\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002selected \u4f7f\u7528 \\(O(n)\\) \u7a7a\u95f4\u3002\u540c\u4e00\u65f6\u523b\u6700\u591a\u5171\u6709 \\(n\\) \u4e2a duplicated \uff0c\u4f7f\u7528 \\(O(n^2)\\) \u7a7a\u95f4\u3002\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002

        "},{"location":"chapter_backtracking/permutations_problem/#3","title":"3. \u00a0 \u4e24\u79cd\u526a\u679d\u5bf9\u6bd4","text":"

        \u8bf7\u6ce8\u610f\uff0c\u867d\u7136 selected \u548c duplicated \u90fd\u7528\u4e8e\u526a\u679d\uff0c\u4f46\u4e24\u8005\u7684\u76ee\u6807\u4e0d\u540c\u3002

        • \u91cd\u590d\u9009\u62e9\u526a\u679d\uff1a\u6574\u4e2a\u641c\u7d22\u8fc7\u7a0b\u4e2d\u53ea\u6709\u4e00\u4e2a selected \u3002\u5b83\u8bb0\u5f55\u7684\u662f\u5f53\u524d\u72b6\u6001\u4e2d\u5305\u542b\u54ea\u4e9b\u5143\u7d20\uff0c\u5176\u4f5c\u7528\u662f\u907f\u514d\u67d0\u4e2a\u5143\u7d20\u5728 state \u4e2d\u91cd\u590d\u51fa\u73b0\u3002
        • \u76f8\u7b49\u5143\u7d20\u526a\u679d\uff1a\u6bcf\u8f6e\u9009\u62e9\uff08\u6bcf\u4e2a\u8c03\u7528\u7684 backtrack \u51fd\u6570\uff09\u90fd\u5305\u542b\u4e00\u4e2a duplicated \u3002\u5b83\u8bb0\u5f55\u7684\u662f\u5728\u672c\u8f6e\u904d\u5386\uff08for \u5faa\u73af\uff09\u4e2d\u54ea\u4e9b\u5143\u7d20\u5df2\u88ab\u9009\u62e9\u8fc7\uff0c\u5176\u4f5c\u7528\u662f\u4fdd\u8bc1\u76f8\u7b49\u5143\u7d20\u53ea\u88ab\u9009\u62e9\u4e00\u6b21\u3002

        \u56fe 13-9 \u5c55\u793a\u4e86\u4e24\u4e2a\u526a\u679d\u6761\u4ef6\u7684\u751f\u6548\u8303\u56f4\u3002\u6ce8\u610f\uff0c\u6811\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u9009\u62e9\uff0c\u4ece\u6839\u8282\u70b9\u5230\u53f6\u8282\u70b9\u7684\u8def\u5f84\u4e0a\u7684\u5404\u4e2a\u8282\u70b9\u6784\u6210\u4e00\u4e2a\u6392\u5217\u3002

        \u56fe 13-9 \u00a0 \u4e24\u79cd\u526a\u679d\u6761\u4ef6\u7684\u4f5c\u7528\u8303\u56f4

        "},{"location":"chapter_backtracking/subset_sum_problem/","title":"13.3 \u00a0 \u5b50\u96c6\u548c\u95ee\u9898","text":""},{"location":"chapter_backtracking/subset_sum_problem/#1331","title":"13.3.1 \u00a0 \u65e0\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"

        Question

        \u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4 nums \u548c\u4e00\u4e2a\u76ee\u6807\u6b63\u6574\u6570 target \uff0c\u8bf7\u627e\u51fa\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\uff0c\u4f7f\u5f97\u7ec4\u5408\u4e2d\u7684\u5143\u7d20\u548c\u7b49\u4e8e target \u3002\u7ed9\u5b9a\u6570\u7ec4\u65e0\u91cd\u590d\u5143\u7d20\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ef\u4ee5\u88ab\u9009\u53d6\u591a\u6b21\u3002\u8bf7\u4ee5\u5217\u8868\u5f62\u5f0f\u8fd4\u56de\u8fd9\u4e9b\u7ec4\u5408\uff0c\u5217\u8868\u4e2d\u4e0d\u5e94\u5305\u542b\u91cd\u590d\u7ec4\u5408\u3002

        \u4f8b\u5982\uff0c\u8f93\u5165\u96c6\u5408 \\(\\{3, 4, 5\\}\\) \u548c\u76ee\u6807\u6574\u6570 \\(9\\) \uff0c\u89e3\u4e3a \\(\\{3, 3, 3\\}, \\{4, 5\\}\\) \u3002\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u4e24\u70b9\u3002

        • \u8f93\u5165\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u88ab\u65e0\u9650\u6b21\u91cd\u590d\u9009\u53d6\u3002
        • \u5b50\u96c6\u4e0d\u533a\u5206\u5143\u7d20\u987a\u5e8f\uff0c\u6bd4\u5982 \\(\\{4, 5\\}\\) \u548c \\(\\{5, 4\\}\\) \u662f\u540c\u4e00\u4e2a\u5b50\u96c6\u3002
        "},{"location":"chapter_backtracking/subset_sum_problem/#1","title":"1. \u00a0 \u53c2\u8003\u5168\u6392\u5217\u89e3\u6cd5","text":"

        \u7c7b\u4f3c\u4e8e\u5168\u6392\u5217\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u5b50\u96c6\u7684\u751f\u6210\u8fc7\u7a0b\u60f3\u8c61\u6210\u4e00\u7cfb\u5217\u9009\u62e9\u7684\u7ed3\u679c\uff0c\u5e76\u5728\u9009\u62e9\u8fc7\u7a0b\u4e2d\u5b9e\u65f6\u66f4\u65b0\u201c\u5143\u7d20\u548c\u201d\uff0c\u5f53\u5143\u7d20\u548c\u7b49\u4e8e target \u65f6\uff0c\u5c31\u5c06\u5b50\u96c6\u8bb0\u5f55\u81f3\u7ed3\u679c\u5217\u8868\u3002

        \u800c\u4e0e\u5168\u6392\u5217\u95ee\u9898\u4e0d\u540c\u7684\u662f\uff0c\u672c\u9898\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u88ab\u65e0\u9650\u6b21\u9009\u53d6\uff0c\u56e0\u6b64\u65e0\u987b\u501f\u52a9 selected \u5e03\u5c14\u5217\u8868\u6765\u8bb0\u5f55\u5143\u7d20\u662f\u5426\u5df2\u88ab\u9009\u62e9\u3002\u6211\u4eec\u53ef\u4ee5\u5bf9\u5168\u6392\u5217\u4ee3\u7801\u8fdb\u884c\u5c0f\u5e45\u4fee\u6539\uff0c\u521d\u6b65\u5f97\u5230\u89e3\u9898\u4ee3\u7801\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby subset_sum_i_naive.py
        def backtrack(\n    state: list[int],\n    target: int,\n    total: int,\n    choices: list[int],\n    res: list[list[int]],\n):\n    \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I\"\"\"\n    # \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if total == target:\n        res.append(list(state))\n        return\n    # \u904d\u5386\u6240\u6709\u9009\u62e9\n    for i in range(len(choices)):\n        # \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n        if total + choices[i] > target:\n            continue\n        # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n        state.append(choices[i])\n        # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target, total + choices[i], choices, res)\n        # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop()\n\ndef subset_sum_i_naive(nums: list[int], target: int) -> list[list[int]]:\n    \"\"\"\u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09\"\"\"\n    state = []  # \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    total = 0  # \u5b50\u96c6\u548c\n    res = []  # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, total, nums, res)\n    return res\n
        subset_sum_i_naive.cpp
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(vector<int> &state, int target, int total, vector<int> &choices, vector<vector<int>> &res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (total == target) {\n        res.push_back(state);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (size_t i = 0; i < choices.size(); i++) {\n        // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n        state.push_back(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target, total + choices[i], choices, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop_back();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nvector<vector<int>> subsetSumINaive(vector<int> &nums, int target) {\n    vector<int> state;       // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    int total = 0;           // \u5b50\u96c6\u548c\n    vector<vector<int>> res; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
        subset_sum_i_naive.java
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(List<Integer> state, int target, int total, int[] choices, List<List<Integer>> res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (total == target) {\n        res.add(new ArrayList<>(state));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (int i = 0; i < choices.length; i++) {\n        // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n        state.add(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target, total + choices[i], choices, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.remove(state.size() - 1);\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nList<List<Integer>> subsetSumINaive(int[] nums, int target) {\n    List<Integer> state = new ArrayList<>(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    int total = 0; // \u5b50\u96c6\u548c\n    List<List<Integer>> res = new ArrayList<>(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
        subset_sum_i_naive.cs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid Backtrack(List<int> state, int target, int total, int[] choices, List<List<int>> res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (total == target) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (int i = 0; i < choices.Length; i++) {\n        // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n        state.Add(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        Backtrack(state, target, total + choices[i], choices, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.RemoveAt(state.Count - 1);\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nList<List<int>> SubsetSumINaive(int[] nums, int target) {\n    List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    int total = 0; // \u5b50\u96c6\u548c\n    List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    Backtrack(state, target, total, nums, res);\n    return res;\n}\n
        subset_sum_i_naive.go
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrackSubsetSumINaive(total, target int, state, choices *[]int, res *[][]int) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if target == total {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for i := 0; i < len(*choices); i++ {\n        // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n        if total+(*choices)[i] > target {\n            continue\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n        *state = append(*state, (*choices)[i])\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrackSubsetSumINaive(total+(*choices)[i], target, state, choices, res)\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        *state = (*state)[:len(*state)-1]\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunc subsetSumINaive(nums []int, target int) [][]int {\n    state := make([]int, 0) // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    total := 0              // \u5b50\u96c6\u548c\n    res := make([][]int, 0) // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrackSubsetSumINaive(total, target, &state, &nums, &res)\n    return res\n}\n
        subset_sum_i_naive.swift
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrack(state: inout [Int], target: Int, total: Int, choices: [Int], res: inout [[Int]]) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if total == target {\n        res.append(state)\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for i in choices.indices {\n        // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n        if total + choices[i] > target {\n            continue\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n        state.append(choices[i])\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state: &state, target: target, total: total + choices[i], choices: choices, res: &res)\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.removeLast()\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunc subsetSumINaive(nums: [Int], target: Int) -> [[Int]] {\n    var state: [Int] = [] // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    let total = 0 // \u5b50\u96c6\u548c\n    var res: [[Int]] = [] // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state: &state, target: target, total: total, choices: nums, res: &res)\n    return res\n}\n
        subset_sum_i_naive.js
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(state, target, total, choices, res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (total === target) {\n        res.push([...state]);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (let i = 0; i < choices.length; i++) {\n        // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n        state.push(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target, total + choices[i], choices, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunction subsetSumINaive(nums, target) {\n    const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    const total = 0; // \u5b50\u96c6\u548c\n    const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
        subset_sum_i_naive.ts
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(\n    state: number[],\n    target: number,\n    total: number,\n    choices: number[],\n    res: number[][]\n): void {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (total === target) {\n        res.push([...state]);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (let i = 0; i < choices.length; i++) {\n        // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n        state.push(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target, total + choices[i], choices, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunction subsetSumINaive(nums: number[], target: number): number[][] {\n    const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    const total = 0; // \u5b50\u96c6\u548c\n    const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
        subset_sum_i_naive.dart
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(\n  List<int> state,\n  int target,\n  int total,\n  List<int> choices,\n  List<List<int>> res,\n) {\n  // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n  if (total == target) {\n    res.add(List.from(state));\n    return;\n  }\n  // \u904d\u5386\u6240\u6709\u9009\u62e9\n  for (int i = 0; i < choices.length; i++) {\n    // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n    if (total + choices[i] > target) {\n      continue;\n    }\n    // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n    state.add(choices[i]);\n    // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n    backtrack(state, target, total + choices[i], choices, res);\n    // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n    state.removeLast();\n  }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nList<List<int>> subsetSumINaive(List<int> nums, int target) {\n  List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n  int total = 0; // \u5143\u7d20\u548c\n  List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n  backtrack(state, target, total, nums, res);\n  return res;\n}\n
        subset_sum_i_naive.rs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfn backtrack(\n    state: &mut Vec<i32>,\n    target: i32,\n    total: i32,\n    choices: &[i32],\n    res: &mut Vec<Vec<i32>>,\n) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if total == target {\n        res.push(state.clone());\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for i in 0..choices.len() {\n        // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n        if total + choices[i] > target {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n        state.push(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target, total + choices[i], choices, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfn subset_sum_i_naive(nums: &[i32], target: i32) -> Vec<Vec<i32>> {\n    let mut state = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    let total = 0; // \u5b50\u96c6\u548c\n    let mut res = Vec::new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(&mut state, target, total, nums, &mut res);\n    res\n}\n
        subset_sum_i_naive.c
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(int target, int total, int *choices, int choicesSize) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (total == target) {\n        for (int i = 0; i < stateSize; i++) {\n            res[resSize][i] = state[i];\n        }\n        resColSizes[resSize++] = stateSize;\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (int i = 0; i < choicesSize; i++) {\n        // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n        state[stateSize++] = choices[i];\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(target, total + choices[i], choices, choicesSize);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        stateSize--;\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nvoid subsetSumINaive(int *nums, int numsSize, int target) {\n    resSize = 0; // \u521d\u59cb\u5316\u89e3\u7684\u6570\u91cf\u4e3a0\n    backtrack(target, 0, nums, numsSize);\n}\n
        subset_sum_i_naive.kt
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfun backtrack(\n    state: MutableList<Int>,\n    target: Int,\n    total: Int,\n    choices: IntArray,\n    res: MutableList<MutableList<Int>?>\n) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (total == target) {\n        res.add(state.toMutableList())\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (i in choices.indices) {\n        // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n        if (total + choices[i] > target) {\n            continue\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n        state.add(choices[i])\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target, total + choices[i], choices, res)\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.removeAt(state.size - 1)\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfun subsetSumINaive(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {\n    val state = mutableListOf<Int>() // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    val total = 0 // \u5b50\u96c6\u548c\n    val res = mutableListOf<MutableList<Int>?>() // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, total, nums, res)\n    return res\n}\n
        subset_sum_i_naive.rb
        ### \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I ###\ndef backtrack(state, target, total, choices, res)\n  # \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n  if total == target\n    res << state.dup\n    return\n  end\n\n  # \u904d\u5386\u6240\u6709\u9009\u62e9\n  for i in 0...choices.length\n    # \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n    next if total + choices[i] > target\n    # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n    state << choices[i]\n    # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n    backtrack(state, target, total + choices[i], choices, res)\n    # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n    state.pop\n  end\nend\n\n### \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09###\ndef subset_sum_i_naive(nums, target)\n  state = [] # \u72b6\u6001\uff08\u5b50\u96c6\uff09\n  total = 0 # \u5b50\u96c6\u548c\n  res = [] # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n  backtrack(state, target, total, nums, res)\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5411\u4ee5\u4e0a\u4ee3\u7801\u8f93\u5165\u6570\u7ec4 \\([3, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a \\([3, 3, 3], [4, 5], [5, 4]\\) \u3002\u867d\u7136\u6210\u529f\u627e\u51fa\u4e86\u6240\u6709\u548c\u4e3a \\(9\\) \u7684\u5b50\u96c6\uff0c\u4f46\u5176\u4e2d\u5b58\u5728\u91cd\u590d\u7684\u5b50\u96c6 \\([4, 5]\\) \u548c \\([5, 4]\\) \u3002

        \u8fd9\u662f\u56e0\u4e3a\u641c\u7d22\u8fc7\u7a0b\u662f\u533a\u5206\u9009\u62e9\u987a\u5e8f\u7684\uff0c\u7136\u800c\u5b50\u96c6\u4e0d\u533a\u5206\u9009\u62e9\u987a\u5e8f\u3002\u5982\u56fe 13-10 \u6240\u793a\uff0c\u5148\u9009 \\(4\\) \u540e\u9009 \\(5\\) \u4e0e\u5148\u9009 \\(5\\) \u540e\u9009 \\(4\\) \u662f\u4e0d\u540c\u7684\u5206\u652f\uff0c\u4f46\u5bf9\u5e94\u540c\u4e00\u4e2a\u5b50\u96c6\u3002

        \u56fe 13-10 \u00a0 \u5b50\u96c6\u641c\u7d22\u4e0e\u8d8a\u754c\u526a\u679d

        \u4e3a\u4e86\u53bb\u9664\u91cd\u590d\u5b50\u96c6\uff0c\u4e00\u79cd\u76f4\u63a5\u7684\u601d\u8def\u662f\u5bf9\u7ed3\u679c\u5217\u8868\u8fdb\u884c\u53bb\u91cd\u3002\u4f46\u8fd9\u4e2a\u65b9\u6cd5\u6548\u7387\u5f88\u4f4e\uff0c\u6709\u4e24\u65b9\u9762\u539f\u56e0\u3002

        • \u5f53\u6570\u7ec4\u5143\u7d20\u8f83\u591a\uff0c\u5c24\u5176\u662f\u5f53 target \u8f83\u5927\u65f6\uff0c\u641c\u7d22\u8fc7\u7a0b\u4f1a\u4ea7\u751f\u5927\u91cf\u7684\u91cd\u590d\u5b50\u96c6\u3002
        • \u6bd4\u8f83\u5b50\u96c6\uff08\u6570\u7ec4\uff09\u7684\u5f02\u540c\u975e\u5e38\u8017\u65f6\uff0c\u9700\u8981\u5148\u6392\u5e8f\u6570\u7ec4\uff0c\u518d\u6bd4\u8f83\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u5f02\u540c\u3002
        "},{"location":"chapter_backtracking/subset_sum_problem/#2","title":"2. \u00a0 \u91cd\u590d\u5b50\u96c6\u526a\u679d","text":"

        \u6211\u4eec\u8003\u8651\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u901a\u8fc7\u526a\u679d\u8fdb\u884c\u53bb\u91cd\u3002\u89c2\u5bdf\u56fe 13-11 \uff0c\u91cd\u590d\u5b50\u96c6\u662f\u5728\u4ee5\u4e0d\u540c\u987a\u5e8f\u9009\u62e9\u6570\u7ec4\u5143\u7d20\u65f6\u4ea7\u751f\u7684\uff0c\u4f8b\u5982\u4ee5\u4e0b\u60c5\u51b5\u3002

        1. \u5f53\u7b2c\u4e00\u8f6e\u548c\u7b2c\u4e8c\u8f6e\u5206\u522b\u9009\u62e9 \\(3\\) \u548c \\(4\\) \u65f6\uff0c\u4f1a\u751f\u6210\u5305\u542b\u8fd9\u4e24\u4e2a\u5143\u7d20\u7684\u6240\u6709\u5b50\u96c6\uff0c\u8bb0\u4e3a \\([3, 4, \\dots]\\) \u3002
        2. \u4e4b\u540e\uff0c\u5f53\u7b2c\u4e00\u8f6e\u9009\u62e9 \\(4\\) \u65f6\uff0c\u5219\u7b2c\u4e8c\u8f6e\u5e94\u8be5\u8df3\u8fc7 \\(3\\) \uff0c\u56e0\u4e3a\u8be5\u9009\u62e9\u4ea7\u751f\u7684\u5b50\u96c6 \\([4, 3, \\dots]\\) \u548c\u7b2c 1. \u6b65\u4e2d\u751f\u6210\u7684\u5b50\u96c6\u5b8c\u5168\u91cd\u590d\u3002

        \u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u6bcf\u4e00\u5c42\u7684\u9009\u62e9\u90fd\u662f\u4ece\u5de6\u5230\u53f3\u88ab\u9010\u4e2a\u5c1d\u8bd5\u7684\uff0c\u56e0\u6b64\u8d8a\u9760\u53f3\u7684\u5206\u652f\u88ab\u526a\u6389\u7684\u8d8a\u591a\u3002

        1. \u524d\u4e24\u8f6e\u9009\u62e9 \\(3\\) \u548c \\(5\\) \uff0c\u751f\u6210\u5b50\u96c6 \\([3, 5, \\dots]\\) \u3002
        2. \u524d\u4e24\u8f6e\u9009\u62e9 \\(4\\) \u548c \\(5\\) \uff0c\u751f\u6210\u5b50\u96c6 \\([4, 5, \\dots]\\) \u3002
        3. \u82e5\u7b2c\u4e00\u8f6e\u9009\u62e9 \\(5\\) \uff0c\u5219\u7b2c\u4e8c\u8f6e\u5e94\u8be5\u8df3\u8fc7 \\(3\\) \u548c \\(4\\) \uff0c\u56e0\u4e3a\u5b50\u96c6 \\([5, 3, \\dots]\\) \u548c \\([5, 4, \\dots]\\) \u4e0e\u7b2c 1. \u6b65\u548c\u7b2c 2. \u6b65\u4e2d\u63cf\u8ff0\u7684\u5b50\u96c6\u5b8c\u5168\u91cd\u590d\u3002

        \u56fe 13-11 \u00a0 \u4e0d\u540c\u9009\u62e9\u987a\u5e8f\u5bfc\u81f4\u7684\u91cd\u590d\u5b50\u96c6

        \u603b\u7ed3\u6765\u770b\uff0c\u7ed9\u5b9a\u8f93\u5165\u6570\u7ec4 \\([x_1, x_2, \\dots, x_n]\\) \uff0c\u8bbe\u641c\u7d22\u8fc7\u7a0b\u4e2d\u7684\u9009\u62e9\u5e8f\u5217\u4e3a \\([x_{i_1}, x_{i_2}, \\dots, x_{i_m}]\\) \uff0c\u5219\u8be5\u9009\u62e9\u5e8f\u5217\u9700\u8981\u6ee1\u8db3 \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\) \uff0c\u4e0d\u6ee1\u8db3\u8be5\u6761\u4ef6\u7684\u9009\u62e9\u5e8f\u5217\u90fd\u4f1a\u9020\u6210\u91cd\u590d\uff0c\u5e94\u5f53\u526a\u679d\u3002

        "},{"location":"chapter_backtracking/subset_sum_problem/#3","title":"3. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u4e3a\u5b9e\u73b0\u8be5\u526a\u679d\uff0c\u6211\u4eec\u521d\u59cb\u5316\u53d8\u91cf start \uff0c\u7528\u4e8e\u6307\u793a\u904d\u5386\u8d77\u59cb\u70b9\u3002\u5f53\u505a\u51fa\u9009\u62e9 \\(x_{i}\\) \u540e\uff0c\u8bbe\u5b9a\u4e0b\u4e00\u8f6e\u4ece\u7d22\u5f15 \\(i\\) \u5f00\u59cb\u904d\u5386\u3002\u8fd9\u6837\u505a\u5c31\u53ef\u4ee5\u8ba9\u9009\u62e9\u5e8f\u5217\u6ee1\u8db3 \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\) \uff0c\u4ece\u800c\u4fdd\u8bc1\u5b50\u96c6\u552f\u4e00\u3002

        \u9664\u6b64\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u5bf9\u4ee3\u7801\u8fdb\u884c\u4e86\u4ee5\u4e0b\u4e24\u9879\u4f18\u5316\u3002

        • \u5728\u5f00\u542f\u641c\u7d22\u524d\uff0c\u5148\u5c06\u6570\u7ec4 nums \u6392\u5e8f\u3002\u5728\u904d\u5386\u6240\u6709\u9009\u62e9\u65f6\uff0c\u5f53\u5b50\u96c6\u548c\u8d85\u8fc7 target \u65f6\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\uff0c\u56e0\u4e3a\u540e\u8fb9\u7684\u5143\u7d20\u66f4\u5927\uff0c\u5176\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target \u3002
        • \u7701\u53bb\u5143\u7d20\u548c\u53d8\u91cf total \uff0c\u901a\u8fc7\u5728 target \u4e0a\u6267\u884c\u51cf\u6cd5\u6765\u7edf\u8ba1\u5143\u7d20\u548c\uff0c\u5f53 target \u7b49\u4e8e \\(0\\) \u65f6\u8bb0\u5f55\u89e3\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby subset_sum_i.py
        def backtrack(\n    state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n    \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I\"\"\"\n    # \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if target == 0:\n        res.append(list(state))\n        return\n    # \u904d\u5386\u6240\u6709\u9009\u62e9\n    # \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    for i in range(start, len(choices)):\n        # \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        # \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if target - choices[i] < 0:\n            break\n        # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.append(choices[i])\n        # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i, res)\n        # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop()\n\ndef subset_sum_i(nums: list[int], target: int) -> list[list[int]]:\n    \"\"\"\u6c42\u89e3\u5b50\u96c6\u548c I\"\"\"\n    state = []  # \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    nums.sort()  # \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    start = 0  # \u904d\u5386\u8d77\u59cb\u70b9\n    res = []  # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res)\n    return res\n
        subset_sum_i.cpp
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target == 0) {\n        res.push_back(state);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    for (int i = start; i < choices.size(); i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.push_back(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop_back();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nvector<vector<int>> subsetSumI(vector<int> &nums, int target) {\n    vector<int> state;              // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    sort(nums.begin(), nums.end()); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    int start = 0;                  // \u904d\u5386\u8d77\u59cb\u70b9\n    vector<vector<int>> res;        // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_i.java
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target == 0) {\n        res.add(new ArrayList<>(state));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    for (int i = start; i < choices.length; i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.add(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.remove(state.size() - 1);\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList<List<Integer>> subsetSumI(int[] nums, int target) {\n    List<Integer> state = new ArrayList<>(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    Arrays.sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n    List<List<Integer>> res = new ArrayList<>(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_i.cs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target == 0) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    for (int i = start; i < choices.Length; i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.Add(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        Backtrack(state, target - choices[i], choices, i, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.RemoveAt(state.Count - 1);\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList<List<int>> SubsetSumI(int[] nums, int target) {\n    List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    Array.Sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n    List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    Backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_i.go
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrackSubsetSumI(start, target int, state, choices *[]int, res *[][]int) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if target == 0 {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    for i := start; i < len(*choices); i++ {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if target-(*choices)[i] < 0 {\n            break\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        *state = append(*state, (*choices)[i])\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrackSubsetSumI(i, target-(*choices)[i], state, choices, res)\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        *state = (*state)[:len(*state)-1]\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunc subsetSumI(nums []int, target int) [][]int {\n    state := make([]int, 0) // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    sort.Ints(nums)         // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    start := 0              // \u904d\u5386\u8d77\u59cb\u70b9\n    res := make([][]int, 0) // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrackSubsetSumI(start, target, &state, &nums, &res)\n    return res\n}\n
        subset_sum_i.swift
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if target == 0 {\n        res.append(state)\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    for i in choices.indices.dropFirst(start) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if target - choices[i] < 0 {\n            break\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.append(choices[i])\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state: &state, target: target - choices[i], choices: choices, start: i, res: &res)\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.removeLast()\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunc subsetSumI(nums: [Int], target: Int) -> [[Int]] {\n    var state: [Int] = [] // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    let nums = nums.sorted() // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    let start = 0 // \u904d\u5386\u8d77\u59cb\u70b9\n    var res: [[Int]] = [] // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state: &state, target: target, choices: nums, start: start, res: &res)\n    return res\n}\n
        subset_sum_i.js
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(state, target, choices, start, res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target === 0) {\n        res.push([...state]);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    for (let i = start; i < choices.length; i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.push(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunction subsetSumI(nums, target) {\n    const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    nums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    const start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n    const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_i.ts
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(\n    state: number[],\n    target: number,\n    choices: number[],\n    start: number,\n    res: number[][]\n): void {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target === 0) {\n        res.push([...state]);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    for (let i = start; i < choices.length; i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.push(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunction subsetSumI(nums: number[], target: number): number[][] {\n    const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    nums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    const start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n    const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_i.dart
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(\n  List<int> state,\n  int target,\n  List<int> choices,\n  int start,\n  List<List<int>> res,\n) {\n  // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n  if (target == 0) {\n    res.add(List.from(state));\n    return;\n  }\n  // \u904d\u5386\u6240\u6709\u9009\u62e9\n  // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n  for (int i = start; i < choices.length; i++) {\n    // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n    // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n    if (target - choices[i] < 0) {\n      break;\n    }\n    // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n    state.add(choices[i]);\n    // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n    backtrack(state, target - choices[i], choices, i, res);\n    // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n    state.removeLast();\n  }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList<List<int>> subsetSumI(List<int> nums, int target) {\n  List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n  nums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n  int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n  List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n  backtrack(state, target, nums, start, res);\n  return res;\n}\n
        subset_sum_i.rs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfn backtrack(\n    state: &mut Vec<i32>,\n    target: i32,\n    choices: &[i32],\n    start: usize,\n    res: &mut Vec<Vec<i32>>,\n) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if target == 0 {\n        res.push(state.clone());\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    for i in start..choices.len() {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if target - choices[i] < 0 {\n            break;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.push(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfn subset_sum_i(nums: &mut [i32], target: i32) -> Vec<Vec<i32>> {\n    let mut state = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    nums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    let start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n    let mut res = Vec::new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(&mut state, target, nums, start, &mut res);\n    res\n}\n
        subset_sum_i.c
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(int target, int *choices, int choicesSize, int start) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target == 0) {\n        for (int i = 0; i < stateSize; ++i) {\n            res[resSize][i] = state[i];\n        }\n        resColSizes[resSize++] = stateSize;\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    for (int i = start; i < choicesSize; i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state[stateSize] = choices[i];\n        stateSize++;\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(target - choices[i], choices, choicesSize, i);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        stateSize--;\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nvoid subsetSumI(int *nums, int numsSize, int target) {\n    qsort(nums, numsSize, sizeof(int), cmp); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    int start = 0;                           // \u904d\u5386\u8d77\u59cb\u70b9\n    backtrack(target, nums, numsSize, start);\n}\n
        subset_sum_i.kt
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfun backtrack(\n    state: MutableList<Int>,\n    target: Int,\n    choices: IntArray,\n    start: Int,\n    res: MutableList<MutableList<Int>?>\n) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target == 0) {\n        res.add(state.toMutableList())\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    for (i in start..<choices.size) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.add(choices[i])\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i, res)\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.removeAt(state.size - 1)\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfun subsetSumI(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {\n    val state = mutableListOf<Int>() // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    nums.sort() // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    val start = 0 // \u904d\u5386\u8d77\u59cb\u70b9\n    val res = mutableListOf<MutableList<Int>?>() // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res)\n    return res\n}\n
        subset_sum_i.rb
        ### \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I ###\ndef backtrack(state, target, choices, start, res)\n  # \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n  if target.zero?\n    res << state.dup\n    return\n  end\n  # \u904d\u5386\u6240\u6709\u9009\u62e9\n  # \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n  for i in start...choices.length\n    # \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n    # \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n    break if target - choices[i] < 0\n    # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n    state << choices[i]\n    # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n    backtrack(state, target - choices[i], choices, i, res)\n    # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n    state.pop\n  end\nend\n\n### \u6c42\u89e3\u5b50\u96c6\u548c I ###\ndef subset_sum_i(nums, target)\n  state = [] # \u72b6\u6001\uff08\u5b50\u96c6\uff09\n  nums.sort! # \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n  start = 0 # \u904d\u5386\u8d77\u59cb\u70b9\n  res = [] # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n  backtrack(state, target, nums, start, res)\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 13-12 \u6240\u793a\u4e3a\u5c06\u6570\u7ec4 \\([3, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \u8f93\u5165\u4ee5\u4e0a\u4ee3\u7801\u540e\u7684\u6574\u4f53\u56de\u6eaf\u8fc7\u7a0b\u3002

        \u56fe 13-12 \u00a0 \u5b50\u96c6\u548c I \u56de\u6eaf\u8fc7\u7a0b

        "},{"location":"chapter_backtracking/subset_sum_problem/#1332","title":"13.3.2 \u00a0 \u8003\u8651\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"

        Question

        \u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4 nums \u548c\u4e00\u4e2a\u76ee\u6807\u6b63\u6574\u6570 target \uff0c\u8bf7\u627e\u51fa\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\uff0c\u4f7f\u5f97\u7ec4\u5408\u4e2d\u7684\u5143\u7d20\u548c\u7b49\u4e8e target \u3002\u7ed9\u5b9a\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ea\u53ef\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u8bf7\u4ee5\u5217\u8868\u5f62\u5f0f\u8fd4\u56de\u8fd9\u4e9b\u7ec4\u5408\uff0c\u5217\u8868\u4e2d\u4e0d\u5e94\u5305\u542b\u91cd\u590d\u7ec4\u5408\u3002

        \u76f8\u6bd4\u4e8e\u4e0a\u9898\uff0c\u672c\u9898\u7684\u8f93\u5165\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u8fd9\u5f15\u5165\u4e86\u65b0\u7684\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u7ed9\u5b9a\u6570\u7ec4 \\([4, \\hat{4}, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \uff0c\u5219\u73b0\u6709\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c\u4e3a \\([4, 5], [\\hat{4}, 5]\\) \uff0c\u51fa\u73b0\u4e86\u91cd\u590d\u5b50\u96c6\u3002

        \u9020\u6210\u8fd9\u79cd\u91cd\u590d\u7684\u539f\u56e0\u662f\u76f8\u7b49\u5143\u7d20\u5728\u67d0\u8f6e\u4e2d\u88ab\u591a\u6b21\u9009\u62e9\u3002\u5728\u56fe 13-13 \u4e2d\uff0c\u7b2c\u4e00\u8f6e\u5171\u6709\u4e09\u4e2a\u9009\u62e9\uff0c\u5176\u4e2d\u4e24\u4e2a\u90fd\u4e3a \\(4\\) \uff0c\u4f1a\u4ea7\u751f\u4e24\u4e2a\u91cd\u590d\u7684\u641c\u7d22\u5206\u652f\uff0c\u4ece\u800c\u8f93\u51fa\u91cd\u590d\u5b50\u96c6\uff1b\u540c\u7406\uff0c\u7b2c\u4e8c\u8f6e\u7684\u4e24\u4e2a \\(4\\) \u4e5f\u4f1a\u4ea7\u751f\u91cd\u590d\u5b50\u96c6\u3002

        \u56fe 13-13 \u00a0 \u76f8\u7b49\u5143\u7d20\u5bfc\u81f4\u7684\u91cd\u590d\u5b50\u96c6

        "},{"location":"chapter_backtracking/subset_sum_problem/#1_1","title":"1. \u00a0 \u76f8\u7b49\u5143\u7d20\u526a\u679d","text":"

        \u4e3a\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u9700\u8981\u9650\u5236\u76f8\u7b49\u5143\u7d20\u5728\u6bcf\u4e00\u8f6e\u4e2d\u53ea\u80fd\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u5b9e\u73b0\u65b9\u5f0f\u6bd4\u8f83\u5de7\u5999\uff1a\u7531\u4e8e\u6570\u7ec4\u662f\u5df2\u6392\u5e8f\u7684\uff0c\u56e0\u6b64\u76f8\u7b49\u5143\u7d20\u90fd\u662f\u76f8\u90bb\u7684\u3002\u8fd9\u610f\u5473\u7740\u5728\u67d0\u8f6e\u9009\u62e9\u4e2d\uff0c\u82e5\u5f53\u524d\u5143\u7d20\u4e0e\u5176\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u5219\u8bf4\u660e\u5b83\u5df2\u7ecf\u88ab\u9009\u62e9\u8fc7\uff0c\u56e0\u6b64\u76f4\u63a5\u8df3\u8fc7\u5f53\u524d\u5143\u7d20\u3002

        \u4e0e\u6b64\u540c\u65f6\uff0c\u672c\u9898\u89c4\u5b9a\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u53ea\u80fd\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5229\u7528\u53d8\u91cf start \u6765\u6ee1\u8db3\u8be5\u7ea6\u675f\uff1a\u5f53\u505a\u51fa\u9009\u62e9 \\(x_{i}\\) \u540e\uff0c\u8bbe\u5b9a\u4e0b\u4e00\u8f6e\u4ece\u7d22\u5f15 \\(i + 1\\) \u5f00\u59cb\u5411\u540e\u904d\u5386\u3002\u8fd9\u6837\u65e2\u80fd\u53bb\u9664\u91cd\u590d\u5b50\u96c6\uff0c\u4e5f\u80fd\u907f\u514d\u91cd\u590d\u9009\u62e9\u5143\u7d20\u3002

        "},{"location":"chapter_backtracking/subset_sum_problem/#2_1","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby subset_sum_ii.py
        def backtrack(\n    state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n    \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II\"\"\"\n    # \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if target == 0:\n        res.append(list(state))\n        return\n    # \u904d\u5386\u6240\u6709\u9009\u62e9\n    # \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    # \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n    for i in range(start, len(choices)):\n        # \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        # \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if target - choices[i] < 0:\n            break\n        # \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if i > start and choices[i] == choices[i - 1]:\n            continue\n        # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.append(choices[i])\n        # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i + 1, res)\n        # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop()\n\ndef subset_sum_ii(nums: list[int], target: int) -> list[list[int]]:\n    \"\"\"\u6c42\u89e3\u5b50\u96c6\u548c II\"\"\"\n    state = []  # \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    nums.sort()  # \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    start = 0  # \u904d\u5386\u8d77\u59cb\u70b9\n    res = []  # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res)\n    return res\n
        subset_sum_ii.cpp
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target == 0) {\n        res.push_back(state);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n    for (int i = start; i < choices.size(); i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.push_back(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop_back();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nvector<vector<int>> subsetSumII(vector<int> &nums, int target) {\n    vector<int> state;              // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    sort(nums.begin(), nums.end()); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    int start = 0;                  // \u904d\u5386\u8d77\u59cb\u70b9\n    vector<vector<int>> res;        // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_ii.java
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target == 0) {\n        res.add(new ArrayList<>(state));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n    for (int i = start; i < choices.length; i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.add(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.remove(state.size() - 1);\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList<List<Integer>> subsetSumII(int[] nums, int target) {\n    List<Integer> state = new ArrayList<>(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    Arrays.sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n    List<List<Integer>> res = new ArrayList<>(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_ii.cs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target == 0) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n    for (int i = start; i < choices.Length; i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.Add(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        Backtrack(state, target - choices[i], choices, i + 1, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.RemoveAt(state.Count - 1);\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList<List<int>> SubsetSumII(int[] nums, int target) {\n    List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    Array.Sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n    List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    Backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_ii.go
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunc backtrackSubsetSumII(start, target int, state, choices *[]int, res *[][]int) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if target == 0 {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n    for i := start; i < len(*choices); i++ {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if target-(*choices)[i] < 0 {\n            break\n        }\n        // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if i > start && (*choices)[i] == (*choices)[i-1] {\n            continue\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        *state = append(*state, (*choices)[i])\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrackSubsetSumII(i+1, target-(*choices)[i], state, choices, res)\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        *state = (*state)[:len(*state)-1]\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunc subsetSumII(nums []int, target int) [][]int {\n    state := make([]int, 0) // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    sort.Ints(nums)         // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    start := 0              // \u904d\u5386\u8d77\u59cb\u70b9\n    res := make([][]int, 0) // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrackSubsetSumII(start, target, &state, &nums, &res)\n    return res\n}\n
        subset_sum_ii.swift
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunc backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if target == 0 {\n        res.append(state)\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n    for i in choices.indices.dropFirst(start) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if target - choices[i] < 0 {\n            break\n        }\n        // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if i > start, choices[i] == choices[i - 1] {\n            continue\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.append(choices[i])\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state: &state, target: target - choices[i], choices: choices, start: i + 1, res: &res)\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.removeLast()\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunc subsetSumII(nums: [Int], target: Int) -> [[Int]] {\n    var state: [Int] = [] // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    let nums = nums.sorted() // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    let start = 0 // \u904d\u5386\u8d77\u59cb\u70b9\n    var res: [[Int]] = [] // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state: &state, target: target, choices: nums, start: start, res: &res)\n    return res\n}\n
        subset_sum_ii.js
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunction backtrack(state, target, choices, start, res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target === 0) {\n        res.push([...state]);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n    for (let i = start; i < choices.length; i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if (i > start && choices[i] === choices[i - 1]) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.push(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunction subsetSumII(nums, target) {\n    const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    nums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    const start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n    const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_ii.ts
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunction backtrack(\n    state: number[],\n    target: number,\n    choices: number[],\n    start: number,\n    res: number[][]\n): void {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target === 0) {\n        res.push([...state]);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n    for (let i = start; i < choices.length; i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if (i > start && choices[i] === choices[i - 1]) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.push(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunction subsetSumII(nums: number[], target: number): number[][] {\n    const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    nums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    const start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n    const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_ii.dart
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(\n  List<int> state,\n  int target,\n  List<int> choices,\n  int start,\n  List<List<int>> res,\n) {\n  // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n  if (target == 0) {\n    res.add(List.from(state));\n    return;\n  }\n  // \u904d\u5386\u6240\u6709\u9009\u62e9\n  // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n  // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n  for (int i = start; i < choices.length; i++) {\n    // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n    // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n    if (target - choices[i] < 0) {\n      break;\n    }\n    // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n    if (i > start && choices[i] == choices[i - 1]) {\n      continue;\n    }\n    // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n    state.add(choices[i]);\n    // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n    backtrack(state, target - choices[i], choices, i + 1, res);\n    // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n    state.removeLast();\n  }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList<List<int>> subsetSumII(List<int> nums, int target) {\n  List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n  nums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n  int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n  List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n  backtrack(state, target, nums, start, res);\n  return res;\n}\n
        subset_sum_ii.rs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfn backtrack(\n    state: &mut Vec<i32>,\n    target: i32,\n    choices: &[i32],\n    start: usize,\n    res: &mut Vec<Vec<i32>>,\n) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if target == 0 {\n        res.push(state.clone());\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n    for i in start..choices.len() {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if target - choices[i] < 0 {\n            break;\n        }\n        // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if i > start && choices[i] == choices[i - 1] {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.push(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfn subset_sum_ii(nums: &mut [i32], target: i32) -> Vec<Vec<i32>> {\n    let mut state = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    nums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    let start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n    let mut res = Vec::new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(&mut state, target, nums, start, &mut res);\n    res\n}\n
        subset_sum_ii.c
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(int target, int *choices, int choicesSize, int start) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target == 0) {\n        for (int i = 0; i < stateSize; i++) {\n            res[resSize][i] = state[i];\n        }\n        resColSizes[resSize++] = stateSize;\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n    for (int i = start; i < choicesSize; i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\n        if (target - choices[i] < 0) {\n            continue;\n        }\n        // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state[stateSize] = choices[i];\n        stateSize++;\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(target - choices[i], choices, choicesSize, i + 1);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        stateSize--;\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nvoid subsetSumII(int *nums, int numsSize, int target) {\n    // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    qsort(nums, numsSize, sizeof(int), cmp);\n    // \u5f00\u59cb\u56de\u6eaf\n    backtrack(target, nums, numsSize, 0);\n}\n
        subset_sum_ii.kt
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfun backtrack(\n    state: MutableList<Int>,\n    target: Int,\n    choices: IntArray,\n    start: Int,\n    res: MutableList<MutableList<Int>?>\n) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target == 0) {\n        res.add(state.toMutableList())\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n    for (i in start..<choices.size) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break\n        }\n        // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.add(choices[i])\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i + 1, res)\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.removeAt(state.size - 1)\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfun subsetSumII(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {\n    val state = mutableListOf<Int>() // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    nums.sort() // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    val start = 0 // \u904d\u5386\u8d77\u59cb\u70b9\n    val res = mutableListOf<MutableList<Int>?>() // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res)\n    return res\n}\n
        subset_sum_ii.rb
        ### \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II ###\ndef backtrack(state, target, choices, start, res)\n  # \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n  if target.zero?\n    res << state.dup\n    return\n  end\n\n  # \u904d\u5386\u6240\u6709\u9009\u62e9\n  # \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n  # \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n  for i in start...choices.length\n    # \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n    # \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n    break if target - choices[i] < 0\n    # \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n    next if i > start && choices[i] == choices[i - 1]\n    # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n    state << choices[i]\n    # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n    backtrack(state, target - choices[i], choices, i + 1, res)\n    # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n    state.pop\n  end\nend\n\n### \u6c42\u89e3\u5b50\u96c6\u548c II ###\ndef subset_sum_ii(nums, target)\n  state = [] # \u72b6\u6001\uff08\u5b50\u96c6\uff09\n  nums.sort! # \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n  start = 0 # \u904d\u5386\u8d77\u59cb\u70b9\n  res = [] # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n  backtrack(state, target, nums, start, res)\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 13-14 \u5c55\u793a\u4e86\u6570\u7ec4 \\([4, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \u7684\u56de\u6eaf\u8fc7\u7a0b\uff0c\u5171\u5305\u542b\u56db\u79cd\u526a\u679d\u64cd\u4f5c\u3002\u8bf7\u4f60\u5c06\u56fe\u793a\u4e0e\u4ee3\u7801\u6ce8\u91ca\u76f8\u7ed3\u5408\uff0c\u7406\u89e3\u6574\u4e2a\u641c\u7d22\u8fc7\u7a0b\uff0c\u4ee5\u53ca\u6bcf\u79cd\u526a\u679d\u64cd\u4f5c\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002

        \u56fe 13-14 \u00a0 \u5b50\u96c6\u548c II \u56de\u6eaf\u8fc7\u7a0b

        "},{"location":"chapter_backtracking/summary/","title":"13.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_backtracking/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u56de\u6eaf\u7b97\u6cd5\u672c\u8d28\u662f\u7a77\u4e3e\u6cd5\uff0c\u901a\u8fc7\u5bf9\u89e3\u7a7a\u95f4\u8fdb\u884c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u6765\u5bfb\u627e\u7b26\u5408\u6761\u4ef6\u7684\u89e3\u3002\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u9047\u5230\u6ee1\u8db3\u6761\u4ef6\u7684\u89e3\u5219\u8bb0\u5f55\uff0c\u76f4\u81f3\u627e\u5230\u6240\u6709\u89e3\u6216\u904d\u5386\u5b8c\u6210\u540e\u7ed3\u675f\u3002
        • \u56de\u6eaf\u7b97\u6cd5\u7684\u641c\u7d22\u8fc7\u7a0b\u5305\u62ec\u5c1d\u8bd5\u4e0e\u56de\u9000\u4e24\u4e2a\u90e8\u5206\u3002\u5b83\u901a\u8fc7\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u6765\u5c1d\u8bd5\u5404\u79cd\u9009\u62e9\uff0c\u5f53\u9047\u5230\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u60c5\u51b5\u65f6\uff0c\u5219\u64a4\u9500\u4e0a\u4e00\u6b65\u7684\u9009\u62e9\uff0c\u9000\u56de\u5230\u4e4b\u524d\u7684\u72b6\u6001\uff0c\u5e76\u7ee7\u7eed\u5c1d\u8bd5\u5176\u4ed6\u9009\u62e9\u3002\u5c1d\u8bd5\u4e0e\u56de\u9000\u662f\u4e24\u4e2a\u65b9\u5411\u76f8\u53cd\u7684\u64cd\u4f5c\u3002
        • \u56de\u6eaf\u95ee\u9898\u901a\u5e38\u5305\u542b\u591a\u4e2a\u7ea6\u675f\u6761\u4ef6\uff0c\u5b83\u4eec\u53ef\u7528\u4e8e\u5b9e\u73b0\u526a\u679d\u64cd\u4f5c\u3002\u526a\u679d\u53ef\u4ee5\u63d0\u524d\u7ed3\u675f\u4e0d\u5fc5\u8981\u7684\u641c\u7d22\u5206\u652f\uff0c\u5927\u5e45\u63d0\u5347\u641c\u7d22\u6548\u7387\u3002
        • \u56de\u6eaf\u7b97\u6cd5\u4e3b\u8981\u53ef\u7528\u4e8e\u89e3\u51b3\u641c\u7d22\u95ee\u9898\u548c\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\u3002\u7ec4\u5408\u4f18\u5316\u95ee\u9898\u867d\u7136\u53ef\u4ee5\u7528\u56de\u6eaf\u7b97\u6cd5\u89e3\u51b3\uff0c\u4f46\u5f80\u5f80\u5b58\u5728\u6548\u7387\u66f4\u9ad8\u6216\u6548\u679c\u66f4\u597d\u7684\u89e3\u6cd5\u3002
        • \u5168\u6392\u5217\u95ee\u9898\u65e8\u5728\u641c\u7d22\u7ed9\u5b9a\u96c6\u5408\u5143\u7d20\u7684\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u3002\u6211\u4eec\u501f\u52a9\u4e00\u4e2a\u6570\u7ec4\u6765\u8bb0\u5f55\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u88ab\u9009\u62e9\uff0c\u526a\u6389\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\u7684\u641c\u7d22\u5206\u652f\uff0c\u786e\u4fdd\u6bcf\u4e2a\u5143\u7d20\u53ea\u88ab\u9009\u62e9\u4e00\u6b21\u3002
        • \u5728\u5168\u6392\u5217\u95ee\u9898\u4e2d\uff0c\u5982\u679c\u96c6\u5408\u4e2d\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff0c\u5219\u6700\u7ec8\u7ed3\u679c\u4f1a\u51fa\u73b0\u91cd\u590d\u6392\u5217\u3002\u6211\u4eec\u9700\u8981\u7ea6\u675f\u76f8\u7b49\u5143\u7d20\u5728\u6bcf\u8f6e\u4e2d\u53ea\u80fd\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u8fd9\u901a\u5e38\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u96c6\u5408\u6765\u5b9e\u73b0\u3002
        • \u5b50\u96c6\u548c\u95ee\u9898\u7684\u76ee\u6807\u662f\u5728\u7ed9\u5b9a\u96c6\u5408\u4e2d\u627e\u5230\u548c\u4e3a\u76ee\u6807\u503c\u7684\u6240\u6709\u5b50\u96c6\u3002\u96c6\u5408\u4e0d\u533a\u5206\u5143\u7d20\u987a\u5e8f\uff0c\u800c\u641c\u7d22\u8fc7\u7a0b\u4f1a\u8f93\u51fa\u6240\u6709\u987a\u5e8f\u7684\u7ed3\u679c\uff0c\u4ea7\u751f\u91cd\u590d\u5b50\u96c6\u3002\u6211\u4eec\u5728\u56de\u6eaf\u524d\u5c06\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u8bbe\u7f6e\u4e00\u4e2a\u53d8\u91cf\u6765\u6307\u793a\u6bcf\u4e00\u8f6e\u7684\u904d\u5386\u8d77\u59cb\u70b9\uff0c\u4ece\u800c\u5c06\u751f\u6210\u91cd\u590d\u5b50\u96c6\u7684\u641c\u7d22\u5206\u652f\u8fdb\u884c\u526a\u679d\u3002
        • \u5bf9\u4e8e\u5b50\u96c6\u548c\u95ee\u9898\uff0c\u6570\u7ec4\u4e2d\u7684\u76f8\u7b49\u5143\u7d20\u4f1a\u4ea7\u751f\u91cd\u590d\u96c6\u5408\u3002\u6211\u4eec\u5229\u7528\u6570\u7ec4\u5df2\u6392\u5e8f\u7684\u524d\u7f6e\u6761\u4ef6\uff0c\u901a\u8fc7\u5224\u65ad\u76f8\u90bb\u5143\u7d20\u662f\u5426\u76f8\u7b49\u5b9e\u73b0\u526a\u679d\uff0c\u4ece\u800c\u786e\u4fdd\u76f8\u7b49\u5143\u7d20\u5728\u6bcf\u8f6e\u4e2d\u53ea\u80fd\u88ab\u9009\u4e2d\u4e00\u6b21\u3002
        • \\(n\\) \u7687\u540e\u95ee\u9898\u65e8\u5728\u5bfb\u627e\u5c06 \\(n\\) \u4e2a\u7687\u540e\u653e\u7f6e\u5230 \\(n \\times n\\) \u5c3a\u5bf8\u68cb\u76d8\u4e0a\u7684\u65b9\u6848\uff0c\u8981\u6c42\u6240\u6709\u7687\u540e\u4e24\u4e24\u4e4b\u95f4\u65e0\u6cd5\u653b\u51fb\u5bf9\u65b9\u3002\u8be5\u95ee\u9898\u7684\u7ea6\u675f\u6761\u4ef6\u6709\u884c\u7ea6\u675f\u3001\u5217\u7ea6\u675f\u3001\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\u7ea6\u675f\u3002\u4e3a\u6ee1\u8db3\u884c\u7ea6\u675f\uff0c\u6211\u4eec\u91c7\u7528\u6309\u884c\u653e\u7f6e\u7684\u7b56\u7565\uff0c\u4fdd\u8bc1\u6bcf\u4e00\u884c\u653e\u7f6e\u4e00\u4e2a\u7687\u540e\u3002
        • \u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u5904\u7406\u65b9\u5f0f\u7c7b\u4f3c\u3002\u5bf9\u4e8e\u5217\u7ea6\u675f\uff0c\u6211\u4eec\u5229\u7528\u4e00\u4e2a\u6570\u7ec4\u6765\u8bb0\u5f55\u6bcf\u4e00\u5217\u662f\u5426\u6709\u7687\u540e\uff0c\u4ece\u800c\u6307\u793a\u9009\u4e2d\u7684\u683c\u5b50\u662f\u5426\u5408\u6cd5\u3002\u5bf9\u4e8e\u5bf9\u89d2\u7ebf\u7ea6\u675f\uff0c\u6211\u4eec\u501f\u52a9\u4e24\u4e2a\u6570\u7ec4\u6765\u5206\u522b\u8bb0\u5f55\u8be5\u4e3b\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u5b58\u5728\u7687\u540e\uff1b\u96be\u70b9\u5728\u4e8e\u627e\u51fa\u5904\u5728\u540c\u4e00\u4e3b\uff08\u526f\uff09\u5bf9\u89d2\u7ebf\u4e0a\u7684\u683c\u5b50\u6240\u6ee1\u8db3\u7684\u884c\u5217\u7d22\u5f15\u89c4\u5f8b\u3002
        "},{"location":"chapter_backtracking/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u600e\u4e48\u7406\u89e3\u56de\u6eaf\u548c\u9012\u5f52\u7684\u5173\u7cfb\uff1f

        \u603b\u7684\u6765\u770b\uff0c\u56de\u6eaf\u662f\u4e00\u79cd\u201c\u7b97\u6cd5\u7b56\u7565\u201d\uff0c\u800c\u9012\u5f52\u66f4\u50cf\u662f\u4e00\u4e2a\u201c\u5de5\u5177\u201d\u3002

        • \u56de\u6eaf\u7b97\u6cd5\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\u3002\u7136\u800c\uff0c\u56de\u6eaf\u662f\u9012\u5f52\u7684\u5e94\u7528\u573a\u666f\u4e4b\u4e00\uff0c\u662f\u9012\u5f52\u5728\u641c\u7d22\u95ee\u9898\u4e2d\u7684\u5e94\u7528\u3002
        • \u9012\u5f52\u7684\u7ed3\u6784\u4f53\u73b0\u4e86\u201c\u5b50\u95ee\u9898\u5206\u89e3\u201d\u7684\u89e3\u9898\u8303\u5f0f\uff0c\u5e38\u7528\u4e8e\u89e3\u51b3\u5206\u6cbb\u3001\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\uff08\u8bb0\u5fc6\u5316\u9012\u5f52\uff09\u7b49\u95ee\u9898\u3002
        "},{"location":"chapter_computational_complexity/","title":"\u7b2c 2 \u7ae0 \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"

        Abstract

        \u590d\u6742\u5ea6\u5206\u6790\u72b9\u5982\u6d69\u701a\u7684\u7b97\u6cd5\u5b87\u5b99\u4e2d\u7684\u65f6\u7a7a\u5411\u5bfc\u3002

        \u5b83\u5e26\u9886\u6211\u4eec\u5728\u65f6\u95f4\u4e0e\u7a7a\u95f4\u8fd9\u4e24\u4e2a\u7ef4\u5ea6\u4e0a\u6df1\u5165\u63a2\u7d22\uff0c\u5bfb\u627e\u66f4\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\u3002

        "},{"location":"chapter_computational_complexity/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 2.1 \u00a0 \u7b97\u6cd5\u6548\u7387\u8bc4\u4f30
        • 2.2 \u00a0 \u8fed\u4ee3\u4e0e\u9012\u5f52
        • 2.3 \u00a0 \u65f6\u95f4\u590d\u6742\u5ea6
        • 2.4 \u00a0 \u7a7a\u95f4\u590d\u6742\u5ea6
        • 2.5 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_computational_complexity/iteration_and_recursion/","title":"2.2 \u00a0 \u8fed\u4ee3\u4e0e\u9012\u5f52","text":"

        \u5728\u7b97\u6cd5\u4e2d\uff0c\u91cd\u590d\u6267\u884c\u67d0\u4e2a\u4efb\u52a1\u662f\u5f88\u5e38\u89c1\u7684\uff0c\u5b83\u4e0e\u590d\u6742\u5ea6\u5206\u6790\u606f\u606f\u76f8\u5173\u3002\u56e0\u6b64\uff0c\u5728\u4ecb\u7ecd\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u6765\u4e86\u89e3\u5982\u4f55\u5728\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u91cd\u590d\u6267\u884c\u4efb\u52a1\uff0c\u5373\u4e24\u79cd\u57fa\u672c\u7684\u7a0b\u5e8f\u63a7\u5236\u7ed3\u6784\uff1a\u8fed\u4ee3\u3001\u9012\u5f52\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#221","title":"2.2.1 \u00a0 \u8fed\u4ee3","text":"

        \u8fed\u4ee3\uff08iteration\uff09\u662f\u4e00\u79cd\u91cd\u590d\u6267\u884c\u67d0\u4e2a\u4efb\u52a1\u7684\u63a7\u5236\u7ed3\u6784\u3002\u5728\u8fed\u4ee3\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u5728\u6ee1\u8db3\u4e00\u5b9a\u7684\u6761\u4ef6\u4e0b\u91cd\u590d\u6267\u884c\u67d0\u6bb5\u4ee3\u7801\uff0c\u76f4\u5230\u8fd9\u4e2a\u6761\u4ef6\u4e0d\u518d\u6ee1\u8db3\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#1-for","title":"1. \u00a0 for \u5faa\u73af","text":"

        for \u5faa\u73af\u662f\u6700\u5e38\u89c1\u7684\u8fed\u4ee3\u5f62\u5f0f\u4e4b\u4e00\uff0c\u9002\u5408\u5728\u9884\u5148\u77e5\u9053\u8fed\u4ee3\u6b21\u6570\u65f6\u4f7f\u7528\u3002

        \u4ee5\u4e0b\u51fd\u6570\u57fa\u4e8e for \u5faa\u73af\u5b9e\u73b0\u4e86\u6c42\u548c \\(1 + 2 + \\dots + n\\) \uff0c\u6c42\u548c\u7ed3\u679c\u4f7f\u7528\u53d8\u91cf res \u8bb0\u5f55\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cPython \u4e2d range(a, b) \u5bf9\u5e94\u7684\u533a\u95f4\u662f\u201c\u5de6\u95ed\u53f3\u5f00\u201d\u7684\uff0c\u5bf9\u5e94\u7684\u904d\u5386\u8303\u56f4\u4e3a \\(a, a + 1, \\dots, b-1\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
        def for_loop(n: int) -> int:\n    \"\"\"for \u5faa\u73af\"\"\"\n    res = 0\n    # \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for i in range(1, n + 1):\n        res += i\n    return res\n
        iteration.cpp
        /* for \u5faa\u73af */\nint forLoop(int n) {\n    int res = 0;\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; ++i) {\n        res += i;\n    }\n    return res;\n}\n
        iteration.java
        /* for \u5faa\u73af */\nint forLoop(int n) {\n    int res = 0;\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
        iteration.cs
        /* for \u5faa\u73af */\nint ForLoop(int n) {\n    int res = 0;\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
        iteration.go
        /* for \u5faa\u73af */\nfunc forLoop(n int) int {\n    res := 0\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for i := 1; i <= n; i++ {\n        res += i\n    }\n    return res\n}\n
        iteration.swift
        /* for \u5faa\u73af */\nfunc forLoop(n: Int) -> Int {\n    var res = 0\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for i in 1 ... n {\n        res += i\n    }\n    return res\n}\n
        iteration.js
        /* for \u5faa\u73af */\nfunction forLoop(n) {\n    let res = 0;\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for (let i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
        iteration.ts
        /* for \u5faa\u73af */\nfunction forLoop(n: number): number {\n    let res = 0;\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for (let i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
        iteration.dart
        /* for \u5faa\u73af */\nint forLoop(int n) {\n  int res = 0;\n  // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n  for (int i = 1; i <= n; i++) {\n    res += i;\n  }\n  return res;\n}\n
        iteration.rs
        /* for \u5faa\u73af */\nfn for_loop(n: i32) -> i32 {\n    let mut res = 0;\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for i in 1..=n {\n        res += i;\n    }\n    res\n}\n
        iteration.c
        /* for \u5faa\u73af */\nint forLoop(int n) {\n    int res = 0;\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
        iteration.kt
        /* for \u5faa\u73af */\nfun forLoop(n: Int): Int {\n    var res = 0\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for (i in 1..n) {\n        res += i\n    }\n    return res\n}\n
        iteration.rb
        ### for \u5faa\u73af ###\ndef for_loop(n)\n  res = 0\n\n  # \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n  for i in 1..n\n    res += i\n  end\n\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 2-1 \u662f\u8be5\u6c42\u548c\u51fd\u6570\u7684\u6d41\u7a0b\u6846\u56fe\u3002

        \u56fe 2-1 \u00a0 \u6c42\u548c\u51fd\u6570\u7684\u6d41\u7a0b\u6846\u56fe

        \u6b64\u6c42\u548c\u51fd\u6570\u7684\u64cd\u4f5c\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u6210\u6b63\u6bd4\uff0c\u6216\u8005\u8bf4\u6210\u201c\u7ebf\u6027\u5173\u7cfb\u201d\u3002\u5b9e\u9645\u4e0a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u63cf\u8ff0\u7684\u5c31\u662f\u8fd9\u4e2a\u201c\u7ebf\u6027\u5173\u7cfb\u201d\u3002\u76f8\u5173\u5185\u5bb9\u5c06\u4f1a\u5728\u4e0b\u4e00\u8282\u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#2-while","title":"2. \u00a0 while \u5faa\u73af","text":"

        \u4e0e for \u5faa\u73af\u7c7b\u4f3c\uff0cwhile \u5faa\u73af\u4e5f\u662f\u4e00\u79cd\u5b9e\u73b0\u8fed\u4ee3\u7684\u65b9\u6cd5\u3002\u5728 while \u5faa\u73af\u4e2d\uff0c\u7a0b\u5e8f\u6bcf\u8f6e\u90fd\u4f1a\u5148\u68c0\u67e5\u6761\u4ef6\uff0c\u5982\u679c\u6761\u4ef6\u4e3a\u771f\uff0c\u5219\u7ee7\u7eed\u6267\u884c\uff0c\u5426\u5219\u5c31\u7ed3\u675f\u5faa\u73af\u3002

        \u4e0b\u9762\u6211\u4eec\u7528 while \u5faa\u73af\u6765\u5b9e\u73b0\u6c42\u548c \\(1 + 2 + \\dots + n\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
        def while_loop(n: int) -> int:\n    \"\"\"while \u5faa\u73af\"\"\"\n    res = 0\n    i = 1  # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    # \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    while i <= n:\n        res += i\n        i += 1  # \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    return res\n
        iteration.cpp
        /* while \u5faa\u73af */\nint whileLoop(int n) {\n    int res = 0;\n    int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    }\n    return res;\n}\n
        iteration.java
        /* while \u5faa\u73af */\nint whileLoop(int n) {\n    int res = 0;\n    int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    }\n    return res;\n}\n
        iteration.cs
        /* while \u5faa\u73af */\nint WhileLoop(int n) {\n    int res = 0;\n    int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i += 1; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    }\n    return res;\n}\n
        iteration.go
        /* while \u5faa\u73af */\nfunc whileLoop(n int) int {\n    res := 0\n    // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    i := 1\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for i <= n {\n        res += i\n        // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i++\n    }\n    return res\n}\n
        iteration.swift
        /* while \u5faa\u73af */\nfunc whileLoop(n: Int) -> Int {\n    var res = 0\n    var i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    while i <= n {\n        res += i\n        i += 1 // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    }\n    return res\n}\n
        iteration.js
        /* while \u5faa\u73af */\nfunction whileLoop(n) {\n    let res = 0;\n    let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    }\n    return res;\n}\n
        iteration.ts
        /* while \u5faa\u73af */\nfunction whileLoop(n: number): number {\n    let res = 0;\n    let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    }\n    return res;\n}\n
        iteration.dart
        /* while \u5faa\u73af */\nint whileLoop(int n) {\n  int res = 0;\n  int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n  // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n  while (i <= n) {\n    res += i;\n    i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n  }\n  return res;\n}\n
        iteration.rs
        /* while \u5faa\u73af */\nfn while_loop(n: i32) -> i32 {\n    let mut res = 0;\n    let mut i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    while i <= n {\n        res += i;\n        i += 1; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    }\n    res\n}\n
        iteration.c
        /* while \u5faa\u73af */\nint whileLoop(int n) {\n    int res = 0;\n    int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    }\n    return res;\n}\n
        iteration.kt
        /* while \u5faa\u73af */\nfun whileLoop(n: Int): Int {\n    var res = 0\n    var i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i\n        i++ // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    }\n    return res\n}\n
        iteration.rb
        ### while \u5faa\u73af ###\ndef while_loop(n)\n  res = 0\n  i = 1 # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n\n  # \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n  while i <= n\n    res += i\n    i += 1 # \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n  end\n\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        while \u5faa\u73af\u6bd4 for \u5faa\u73af\u7684\u81ea\u7531\u5ea6\u66f4\u9ad8\u3002\u5728 while \u5faa\u73af\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u81ea\u7531\u5730\u8bbe\u8ba1\u6761\u4ef6\u53d8\u91cf\u7684\u521d\u59cb\u5316\u548c\u66f4\u65b0\u6b65\u9aa4\u3002

        \u4f8b\u5982\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u6761\u4ef6\u53d8\u91cf \\(i\\) \u6bcf\u8f6e\u8fdb\u884c\u4e24\u6b21\u66f4\u65b0\uff0c\u8fd9\u79cd\u60c5\u51b5\u5c31\u4e0d\u592a\u65b9\u4fbf\u7528 for \u5faa\u73af\u5b9e\u73b0\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
        def while_loop_ii(n: int) -> int:\n    \"\"\"while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09\"\"\"\n    res = 0\n    i = 1  # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    # \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n    while i <= n:\n        res += i\n        # \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i += 1\n        i *= 2\n    return res\n
        iteration.cpp
        /* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n    int res = 0;\n    int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
        iteration.java
        /* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n    int res = 0;\n    int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
        iteration.cs
        /* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint WhileLoopII(int n) {\n    int res = 0;\n    int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i += 1; \n        i *= 2;\n    }\n    return res;\n}\n
        iteration.go
        /* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunc whileLoopII(n int) int {\n    res := 0\n    // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    i := 1\n    // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n    for i <= n {\n        res += i\n        // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i++\n        i *= 2\n    }\n    return res\n}\n
        iteration.swift
        /* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunc whileLoopII(n: Int) -> Int {\n    var res = 0\n    var i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n    while i <= n {\n        res += i\n        // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i += 1\n        i *= 2\n    }\n    return res\n}\n
        iteration.js
        /* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunction whileLoopII(n) {\n    let res = 0;\n    let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
        iteration.ts
        /* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunction whileLoopII(n: number): number {\n    let res = 0;\n    let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
        iteration.dart
        /* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n  int res = 0;\n  int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n  // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n  while (i <= n) {\n    res += i;\n    // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    i++;\n    i *= 2;\n  }\n  return res;\n}\n
        iteration.rs
        /* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfn while_loop_ii(n: i32) -> i32 {\n    let mut res = 0;\n    let mut i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n\n    // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n    while i <= n {\n        res += i;\n        // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i += 1;\n        i *= 2;\n    }\n    res\n}\n
        iteration.c
        /* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n    int res = 0;\n    int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
        iteration.kt
        /* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfun whileLoopII(n: Int): Int {\n    var res = 0\n    var i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n    while (i <= n) {\n        res += i\n        // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i++\n        i *= 2\n    }\n    return res\n}\n
        iteration.rb
        ### while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09###\ndef while_loop_ii(n)\n  res = 0\n  i = 1 # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n\n  # \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n  while i <= n\n    res += i\n    # \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    i += 1\n    i *= 2\n  end\n\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u603b\u7684\u6765\u8bf4\uff0cfor \u5faa\u73af\u7684\u4ee3\u7801\u66f4\u52a0\u7d27\u51d1\uff0cwhile \u5faa\u73af\u66f4\u52a0\u7075\u6d3b\uff0c\u4e24\u8005\u90fd\u53ef\u4ee5\u5b9e\u73b0\u8fed\u4ee3\u7ed3\u6784\u3002\u9009\u62e9\u4f7f\u7528\u54ea\u4e00\u4e2a\u5e94\u8be5\u6839\u636e\u7279\u5b9a\u95ee\u9898\u7684\u9700\u6c42\u6765\u51b3\u5b9a\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#3","title":"3. \u00a0 \u5d4c\u5957\u5faa\u73af","text":"

        \u6211\u4eec\u53ef\u4ee5\u5728\u4e00\u4e2a\u5faa\u73af\u7ed3\u6784\u5185\u5d4c\u5957\u53e6\u4e00\u4e2a\u5faa\u73af\u7ed3\u6784\uff0c\u4e0b\u9762\u4ee5 for \u5faa\u73af\u4e3a\u4f8b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
        def nested_for_loop(n: int) -> str:\n    \"\"\"\u53cc\u5c42 for \u5faa\u73af\"\"\"\n    res = \"\"\n    # \u5faa\u73af i = 1, 2, ..., n-1, n\n    for i in range(1, n + 1):\n        # \u5faa\u73af j = 1, 2, ..., n-1, n\n        for j in range(1, n + 1):\n            res += f\"({i}, {j}), \"\n    return res\n
        iteration.cpp
        /* \u53cc\u5c42 for \u5faa\u73af */\nstring nestedForLoop(int n) {\n    ostringstream res;\n    // \u5faa\u73af i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; ++i) {\n        // \u5faa\u73af j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; ++j) {\n            res << \"(\" << i << \", \" << j << \"), \";\n        }\n    }\n    return res.str();\n}\n
        iteration.java
        /* \u53cc\u5c42 for \u5faa\u73af */\nString nestedForLoop(int n) {\n    StringBuilder res = new StringBuilder();\n    // \u5faa\u73af i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        // \u5faa\u73af j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; j++) {\n            res.append(\"(\" + i + \", \" + j + \"), \");\n        }\n    }\n    return res.toString();\n}\n
        iteration.cs
        /* \u53cc\u5c42 for \u5faa\u73af */\nstring NestedForLoop(int n) {\n    StringBuilder res = new();\n    // \u5faa\u73af i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        // \u5faa\u73af j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; j++) {\n            res.Append($\"({i}, {j}), \");\n        }\n    }\n    return res.ToString();\n}\n
        iteration.go
        /* \u53cc\u5c42 for \u5faa\u73af */\nfunc nestedForLoop(n int) string {\n    res := \"\"\n    // \u5faa\u73af i = 1, 2, ..., n-1, n\n    for i := 1; i <= n; i++ {\n        for j := 1; j <= n; j++ {\n            // \u5faa\u73af j = 1, 2, ..., n-1, n\n            res += fmt.Sprintf(\"(%d, %d), \", i, j)\n        }\n    }\n    return res\n}\n
        iteration.swift
        /* \u53cc\u5c42 for \u5faa\u73af */\nfunc nestedForLoop(n: Int) -> String {\n    var res = \"\"\n    // \u5faa\u73af i = 1, 2, ..., n-1, n\n    for i in 1 ... n {\n        // \u5faa\u73af j = 1, 2, ..., n-1, n\n        for j in 1 ... n {\n            res.append(\"(\\(i), \\(j)), \")\n        }\n    }\n    return res\n}\n
        iteration.js
        /* \u53cc\u5c42 for \u5faa\u73af */\nfunction nestedForLoop(n) {\n    let res = '';\n    // \u5faa\u73af i = 1, 2, ..., n-1, n\n    for (let i = 1; i <= n; i++) {\n        // \u5faa\u73af j = 1, 2, ..., n-1, n\n        for (let j = 1; j <= n; j++) {\n            res += `(${i}, ${j}), `;\n        }\n    }\n    return res;\n}\n
        iteration.ts
        /* \u53cc\u5c42 for \u5faa\u73af */\nfunction nestedForLoop(n: number): string {\n    let res = '';\n    // \u5faa\u73af i = 1, 2, ..., n-1, n\n    for (let i = 1; i <= n; i++) {\n        // \u5faa\u73af j = 1, 2, ..., n-1, n\n        for (let j = 1; j <= n; j++) {\n            res += `(${i}, ${j}), `;\n        }\n    }\n    return res;\n}\n
        iteration.dart
        /* \u53cc\u5c42 for \u5faa\u73af */\nString nestedForLoop(int n) {\n  String res = \"\";\n  // \u5faa\u73af i = 1, 2, ..., n-1, n\n  for (int i = 1; i <= n; i++) {\n    // \u5faa\u73af j = 1, 2, ..., n-1, n\n    for (int j = 1; j <= n; j++) {\n      res += \"($i, $j), \";\n    }\n  }\n  return res;\n}\n
        iteration.rs
        /* \u53cc\u5c42 for \u5faa\u73af */\nfn nested_for_loop(n: i32) -> String {\n    let mut res = vec![];\n    // \u5faa\u73af i = 1, 2, ..., n-1, n\n    for i in 1..=n {\n        // \u5faa\u73af j = 1, 2, ..., n-1, n\n        for j in 1..=n {\n            res.push(format!(\"({}, {}), \", i, j));\n        }\n    }\n    res.join(\"\")\n}\n
        iteration.c
        /* \u53cc\u5c42 for \u5faa\u73af */\nchar *nestedForLoop(int n) {\n    // n * n \u4e3a\u5bf9\u5e94\u70b9\u6570\u91cf\uff0c\"(i, j), \" \u5bf9\u5e94\u5b57\u7b26\u4e32\u957f\u6700\u5927\u4e3a 6+10*2\uff0c\u52a0\u4e0a\u6700\u540e\u4e00\u4e2a\u7a7a\u5b57\u7b26 \\0 \u7684\u989d\u5916\u7a7a\u95f4\n    int size = n * n * 26 + 1;\n    char *res = malloc(size * sizeof(char));\n    // \u5faa\u73af i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        // \u5faa\u73af j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; j++) {\n            char tmp[26];\n            snprintf(tmp, sizeof(tmp), \"(%d, %d), \", i, j);\n            strncat(res, tmp, size - strlen(res) - 1);\n        }\n    }\n    return res;\n}\n
        iteration.kt
        /* \u53cc\u5c42 for \u5faa\u73af */\nfun nestedForLoop(n: Int): String {\n    val res = StringBuilder()\n    // \u5faa\u73af i = 1, 2, ..., n-1, n\n    for (i in 1..n) {\n        // \u5faa\u73af j = 1, 2, ..., n-1, n\n        for (j in 1..n) {\n            res.append(\" ($i, $j), \")\n        }\n    }\n    return res.toString()\n}\n
        iteration.rb
        ### \u53cc\u5c42 for \u5faa\u73af ###\ndef nested_for_loop(n)\n  res = \"\"\n\n  # \u5faa\u73af i = 1, 2, ..., n-1, n\n  for i in 1..n\n    # \u5faa\u73af j = 1, 2, ..., n-1, n\n    for j in 1..n\n      res += \"(#{i}, #{j}), \"\n    end\n  end\n\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 2-2 \u662f\u8be5\u5d4c\u5957\u5faa\u73af\u7684\u6d41\u7a0b\u6846\u56fe\u3002

        \u56fe 2-2 \u00a0 \u5d4c\u5957\u5faa\u73af\u7684\u6d41\u7a0b\u6846\u56fe

        \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570\u7684\u64cd\u4f5c\u6570\u91cf\u4e0e \\(n^2\\) \u6210\u6b63\u6bd4\uff0c\u6216\u8005\u8bf4\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u548c\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u6210\u201c\u5e73\u65b9\u5173\u7cfb\u201d\u3002

        \u6211\u4eec\u53ef\u4ee5\u7ee7\u7eed\u6dfb\u52a0\u5d4c\u5957\u5faa\u73af\uff0c\u6bcf\u4e00\u6b21\u5d4c\u5957\u90fd\u662f\u4e00\u6b21\u201c\u5347\u7ef4\u201d\uff0c\u5c06\u4f1a\u4f7f\u65f6\u95f4\u590d\u6742\u5ea6\u63d0\u9ad8\u81f3\u201c\u7acb\u65b9\u5173\u7cfb\u201d\u201c\u56db\u6b21\u65b9\u5173\u7cfb\u201d\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#222","title":"2.2.2 \u00a0 \u9012\u5f52","text":"

        \u9012\u5f52\uff08recursion\uff09\u662f\u4e00\u79cd\u7b97\u6cd5\u7b56\u7565\uff0c\u901a\u8fc7\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u6765\u89e3\u51b3\u95ee\u9898\u3002\u5b83\u4e3b\u8981\u5305\u542b\u4e24\u4e2a\u9636\u6bb5\u3002

        1. \u9012\uff1a\u7a0b\u5e8f\u4e0d\u65ad\u6df1\u5165\u5730\u8c03\u7528\u81ea\u8eab\uff0c\u901a\u5e38\u4f20\u5165\u66f4\u5c0f\u6216\u66f4\u7b80\u5316\u7684\u53c2\u6570\uff0c\u76f4\u5230\u8fbe\u5230\u201c\u7ec8\u6b62\u6761\u4ef6\u201d\u3002
        2. \u5f52\uff1a\u89e6\u53d1\u201c\u7ec8\u6b62\u6761\u4ef6\u201d\u540e\uff0c\u7a0b\u5e8f\u4ece\u6700\u6df1\u5c42\u7684\u9012\u5f52\u51fd\u6570\u5f00\u59cb\u9010\u5c42\u8fd4\u56de\uff0c\u6c47\u805a\u6bcf\u4e00\u5c42\u7684\u7ed3\u679c\u3002

        \u800c\u4ece\u5b9e\u73b0\u7684\u89d2\u5ea6\u770b\uff0c\u9012\u5f52\u4ee3\u7801\u4e3b\u8981\u5305\u542b\u4e09\u4e2a\u8981\u7d20\u3002

        1. \u7ec8\u6b62\u6761\u4ef6\uff1a\u7528\u4e8e\u51b3\u5b9a\u4ec0\u4e48\u65f6\u5019\u7531\u201c\u9012\u201d\u8f6c\u201c\u5f52\u201d\u3002
        2. \u9012\u5f52\u8c03\u7528\uff1a\u5bf9\u5e94\u201c\u9012\u201d\uff0c\u51fd\u6570\u8c03\u7528\u81ea\u8eab\uff0c\u901a\u5e38\u8f93\u5165\u66f4\u5c0f\u6216\u66f4\u7b80\u5316\u7684\u53c2\u6570\u3002
        3. \u8fd4\u56de\u7ed3\u679c\uff1a\u5bf9\u5e94\u201c\u5f52\u201d\uff0c\u5c06\u5f53\u524d\u9012\u5f52\u5c42\u7ea7\u7684\u7ed3\u679c\u8fd4\u56de\u81f3\u4e0a\u4e00\u5c42\u3002

        \u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u6211\u4eec\u53ea\u9700\u8c03\u7528\u51fd\u6570 recur(n) \uff0c\u5c31\u53ef\u4ee5\u5b8c\u6210 \\(1 + 2 + \\dots + n\\) \u7684\u8ba1\u7b97\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
        def recur(n: int) -> int:\n    \"\"\"\u9012\u5f52\"\"\"\n    # \u7ec8\u6b62\u6761\u4ef6\n    if n == 1:\n        return 1\n    # \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    res = recur(n - 1)\n    # \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    return n + res\n
        recursion.cpp
        /* \u9012\u5f52 */\nint recur(int n) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n == 1)\n        return 1;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    int res = recur(n - 1);\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    return n + res;\n}\n
        recursion.java
        /* \u9012\u5f52 */\nint recur(int n) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n == 1)\n        return 1;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    int res = recur(n - 1);\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    return n + res;\n}\n
        recursion.cs
        /* \u9012\u5f52 */\nint Recur(int n) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n == 1)\n        return 1;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    int res = Recur(n - 1);\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    return n + res;\n}\n
        recursion.go
        /* \u9012\u5f52 */\nfunc recur(n int) int {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if n == 1 {\n        return 1\n    }\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    res := recur(n - 1)\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    return n + res\n}\n
        recursion.swift
        /* \u9012\u5f52 */\nfunc recur(n: Int) -> Int {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if n == 1 {\n        return 1\n    }\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    let res = recur(n: n - 1)\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    return n + res\n}\n
        recursion.js
        /* \u9012\u5f52 */\nfunction recur(n) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n === 1) return 1;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    const res = recur(n - 1);\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    return n + res;\n}\n
        recursion.ts
        /* \u9012\u5f52 */\nfunction recur(n: number): number {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n === 1) return 1;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    const res = recur(n - 1);\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    return n + res;\n}\n
        recursion.dart
        /* \u9012\u5f52 */\nint recur(int n) {\n  // \u7ec8\u6b62\u6761\u4ef6\n  if (n == 1) return 1;\n  // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n  int res = recur(n - 1);\n  // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n  return n + res;\n}\n
        recursion.rs
        /* \u9012\u5f52 */\nfn recur(n: i32) -> i32 {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if n == 1 {\n        return 1;\n    }\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    let res = recur(n - 1);\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    n + res\n}\n
        recursion.c
        /* \u9012\u5f52 */\nint recur(int n) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n == 1)\n        return 1;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    int res = recur(n - 1);\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    return n + res;\n}\n
        recursion.kt
        /* \u9012\u5f52 */\nfun recur(n: Int): Int {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n == 1)\n        return 1\n    // \u9012: \u9012\u5f52\u8c03\u7528\n    val res = recur(n - 1)\n    // \u5f52: \u8fd4\u56de\u7ed3\u679c\n    return n + res\n}\n
        recursion.rb
        ### \u9012\u5f52 ###\ndef recur(n)\n  # \u7ec8\u6b62\u6761\u4ef6\n  return 1 if n == 1\n  # \u9012\uff1a\u9012\u5f52\u8c03\u7528\n  res = recur(n - 1)\n  # \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n  n + res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 2-3 \u5c55\u793a\u4e86\u8be5\u51fd\u6570\u7684\u9012\u5f52\u8fc7\u7a0b\u3002

        \u56fe 2-3 \u00a0 \u6c42\u548c\u51fd\u6570\u7684\u9012\u5f52\u8fc7\u7a0b

        \u867d\u7136\u4ece\u8ba1\u7b97\u89d2\u5ea6\u770b\uff0c\u8fed\u4ee3\u4e0e\u9012\u5f52\u53ef\u4ee5\u5f97\u5230\u76f8\u540c\u7684\u7ed3\u679c\uff0c\u4f46\u5b83\u4eec\u4ee3\u8868\u4e86\u4e24\u79cd\u5b8c\u5168\u4e0d\u540c\u7684\u601d\u8003\u548c\u89e3\u51b3\u95ee\u9898\u7684\u8303\u5f0f\u3002

        • \u8fed\u4ee3\uff1a\u201c\u81ea\u4e0b\u800c\u4e0a\u201d\u5730\u89e3\u51b3\u95ee\u9898\u3002\u4ece\u6700\u57fa\u7840\u7684\u6b65\u9aa4\u5f00\u59cb\uff0c\u7136\u540e\u4e0d\u65ad\u91cd\u590d\u6216\u7d2f\u52a0\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u76f4\u5230\u4efb\u52a1\u5b8c\u6210\u3002
        • \u9012\u5f52\uff1a\u201c\u81ea\u4e0a\u800c\u4e0b\u201d\u5730\u89e3\u51b3\u95ee\u9898\u3002\u5c06\u539f\u95ee\u9898\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u8fd9\u4e9b\u5b50\u95ee\u9898\u548c\u539f\u95ee\u9898\u5177\u6709\u76f8\u540c\u7684\u5f62\u5f0f\u3002\u63a5\u4e0b\u6765\u5c06\u5b50\u95ee\u9898\u7ee7\u7eed\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u76f4\u5230\u57fa\u672c\u60c5\u51b5\u65f6\u505c\u6b62\uff08\u57fa\u672c\u60c5\u51b5\u7684\u89e3\u662f\u5df2\u77e5\u7684\uff09\u3002

        \u4ee5\u4e0a\u8ff0\u6c42\u548c\u51fd\u6570\u4e3a\u4f8b\uff0c\u8bbe\u95ee\u9898 \\(f(n) = 1 + 2 + \\dots + n\\) \u3002

        • \u8fed\u4ee3\uff1a\u5728\u5faa\u73af\u4e2d\u6a21\u62df\u6c42\u548c\u8fc7\u7a0b\uff0c\u4ece \\(1\\) \u904d\u5386\u5230 \\(n\\) \uff0c\u6bcf\u8f6e\u6267\u884c\u6c42\u548c\u64cd\u4f5c\uff0c\u5373\u53ef\u6c42\u5f97 \\(f(n)\\) \u3002
        • \u9012\u5f52\uff1a\u5c06\u95ee\u9898\u5206\u89e3\u4e3a\u5b50\u95ee\u9898 \\(f(n) = n + f(n-1)\\) \uff0c\u4e0d\u65ad\uff08\u9012\u5f52\u5730\uff09\u5206\u89e3\u4e0b\u53bb\uff0c\u76f4\u81f3\u57fa\u672c\u60c5\u51b5 \\(f(1) = 1\\) \u65f6\u7ec8\u6b62\u3002
        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#1","title":"1. \u00a0 \u8c03\u7528\u6808","text":"

        \u9012\u5f52\u51fd\u6570\u6bcf\u6b21\u8c03\u7528\u81ea\u8eab\u65f6\uff0c\u7cfb\u7edf\u90fd\u4f1a\u4e3a\u65b0\u5f00\u542f\u7684\u51fd\u6570\u5206\u914d\u5185\u5b58\uff0c\u4ee5\u5b58\u50a8\u5c40\u90e8\u53d8\u91cf\u3001\u8c03\u7528\u5730\u5740\u548c\u5176\u4ed6\u4fe1\u606f\u7b49\u3002\u8fd9\u5c06\u5bfc\u81f4\u4e24\u65b9\u9762\u7684\u7ed3\u679c\u3002

        • \u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u6570\u636e\u90fd\u5b58\u50a8\u5728\u79f0\u4e3a\u201c\u6808\u5e27\u7a7a\u95f4\u201d\u7684\u5185\u5b58\u533a\u57df\u4e2d\uff0c\u76f4\u81f3\u51fd\u6570\u8fd4\u56de\u540e\u624d\u4f1a\u88ab\u91ca\u653e\u3002\u56e0\u6b64\uff0c\u9012\u5f52\u901a\u5e38\u6bd4\u8fed\u4ee3\u66f4\u52a0\u8017\u8d39\u5185\u5b58\u7a7a\u95f4\u3002
        • \u9012\u5f52\u8c03\u7528\u51fd\u6570\u4f1a\u4ea7\u751f\u989d\u5916\u7684\u5f00\u9500\u3002\u56e0\u6b64\u9012\u5f52\u901a\u5e38\u6bd4\u5faa\u73af\u7684\u65f6\u95f4\u6548\u7387\u66f4\u4f4e\u3002

        \u5982\u56fe 2-4 \u6240\u793a\uff0c\u5728\u89e6\u53d1\u7ec8\u6b62\u6761\u4ef6\u524d\uff0c\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684\u9012\u5f52\u51fd\u6570\uff0c\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \u3002

        \u56fe 2-4 \u00a0 \u9012\u5f52\u8c03\u7528\u6df1\u5ea6

        \u5728\u5b9e\u9645\u4e2d\uff0c\u7f16\u7a0b\u8bed\u8a00\u5141\u8bb8\u7684\u9012\u5f52\u6df1\u5ea6\u901a\u5e38\u662f\u6709\u9650\u7684\uff0c\u8fc7\u6df1\u7684\u9012\u5f52\u53ef\u80fd\u5bfc\u81f4\u6808\u6ea2\u51fa\u9519\u8bef\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#2","title":"2. \u00a0 \u5c3e\u9012\u5f52","text":"

        \u6709\u8da3\u7684\u662f\uff0c\u5982\u679c\u51fd\u6570\u5728\u8fd4\u56de\u524d\u7684\u6700\u540e\u4e00\u6b65\u624d\u8fdb\u884c\u9012\u5f52\u8c03\u7528\uff0c\u5219\u8be5\u51fd\u6570\u53ef\u4ee5\u88ab\u7f16\u8bd1\u5668\u6216\u89e3\u91ca\u5668\u4f18\u5316\uff0c\u4f7f\u5176\u5728\u7a7a\u95f4\u6548\u7387\u4e0a\u4e0e\u8fed\u4ee3\u76f8\u5f53\u3002\u8fd9\u79cd\u60c5\u51b5\u88ab\u79f0\u4e3a\u5c3e\u9012\u5f52\uff08tail recursion\uff09\u3002

        • \u666e\u901a\u9012\u5f52\uff1a\u5f53\u51fd\u6570\u8fd4\u56de\u5230\u4e0a\u4e00\u5c42\u7ea7\u7684\u51fd\u6570\u540e\uff0c\u9700\u8981\u7ee7\u7eed\u6267\u884c\u4ee3\u7801\uff0c\u56e0\u6b64\u7cfb\u7edf\u9700\u8981\u4fdd\u5b58\u4e0a\u4e00\u5c42\u8c03\u7528\u7684\u4e0a\u4e0b\u6587\u3002
        • \u5c3e\u9012\u5f52\uff1a\u9012\u5f52\u8c03\u7528\u662f\u51fd\u6570\u8fd4\u56de\u524d\u7684\u6700\u540e\u4e00\u4e2a\u64cd\u4f5c\uff0c\u8fd9\u610f\u5473\u7740\u51fd\u6570\u8fd4\u56de\u5230\u4e0a\u4e00\u5c42\u7ea7\u540e\uff0c\u65e0\u987b\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\uff0c\u56e0\u6b64\u7cfb\u7edf\u65e0\u987b\u4fdd\u5b58\u4e0a\u4e00\u5c42\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u3002

        \u4ee5\u8ba1\u7b97 \\(1 + 2 + \\dots + n\\) \u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u7ed3\u679c\u53d8\u91cf res \u8bbe\u4e3a\u51fd\u6570\u53c2\u6570\uff0c\u4ece\u800c\u5b9e\u73b0\u5c3e\u9012\u5f52\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
        def tail_recur(n, res):\n    \"\"\"\u5c3e\u9012\u5f52\"\"\"\n    # \u7ec8\u6b62\u6761\u4ef6\n    if n == 0:\n        return res\n    # \u5c3e\u9012\u5f52\u8c03\u7528\n    return tail_recur(n - 1, res + n)\n
        recursion.cpp
        /* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n == 0)\n        return res;\n    // \u5c3e\u9012\u5f52\u8c03\u7528\n    return tailRecur(n - 1, res + n);\n}\n
        recursion.java
        /* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n == 0)\n        return res;\n    // \u5c3e\u9012\u5f52\u8c03\u7528\n    return tailRecur(n - 1, res + n);\n}\n
        recursion.cs
        /* \u5c3e\u9012\u5f52 */\nint TailRecur(int n, int res) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n == 0)\n        return res;\n    // \u5c3e\u9012\u5f52\u8c03\u7528\n    return TailRecur(n - 1, res + n);\n}\n
        recursion.go
        /* \u5c3e\u9012\u5f52 */\nfunc tailRecur(n int, res int) int {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if n == 0 {\n        return res\n    }\n    // \u5c3e\u9012\u5f52\u8c03\u7528\n    return tailRecur(n-1, res+n)\n}\n
        recursion.swift
        /* \u5c3e\u9012\u5f52 */\nfunc tailRecur(n: Int, res: Int) -> Int {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if n == 0 {\n        return res\n    }\n    // \u5c3e\u9012\u5f52\u8c03\u7528\n    return tailRecur(n: n - 1, res: res + n)\n}\n
        recursion.js
        /* \u5c3e\u9012\u5f52 */\nfunction tailRecur(n, res) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n === 0) return res;\n    // \u5c3e\u9012\u5f52\u8c03\u7528\n    return tailRecur(n - 1, res + n);\n}\n
        recursion.ts
        /* \u5c3e\u9012\u5f52 */\nfunction tailRecur(n: number, res: number): number {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n === 0) return res;\n    // \u5c3e\u9012\u5f52\u8c03\u7528\n    return tailRecur(n - 1, res + n);\n}\n
        recursion.dart
        /* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n  // \u7ec8\u6b62\u6761\u4ef6\n  if (n == 0) return res;\n  // \u5c3e\u9012\u5f52\u8c03\u7528\n  return tailRecur(n - 1, res + n);\n}\n
        recursion.rs
        /* \u5c3e\u9012\u5f52 */\nfn tail_recur(n: i32, res: i32) -> i32 {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if n == 0 {\n        return res;\n    }\n    // \u5c3e\u9012\u5f52\u8c03\u7528\n    tail_recur(n - 1, res + n)\n}\n
        recursion.c
        /* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n == 0)\n        return res;\n    // \u5c3e\u9012\u5f52\u8c03\u7528\n    return tailRecur(n - 1, res + n);\n}\n
        recursion.kt
        /* \u5c3e\u9012\u5f52 */\ntailrec fun tailRecur(n: Int, res: Int): Int {\n    // \u6dfb\u52a0 tailrec \u5173\u952e\u8bcd\uff0c\u4ee5\u5f00\u542f\u5c3e\u9012\u5f52\u4f18\u5316\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n == 0)\n        return res\n    // \u5c3e\u9012\u5f52\u8c03\u7528\n    return tailRecur(n - 1, res + n)\n}\n
        recursion.rb
        ### \u5c3e\u9012\u5f52 ###\ndef tail_recur(n, res)\n  # \u7ec8\u6b62\u6761\u4ef6\n  return res if n == 0\n  # \u5c3e\u9012\u5f52\u8c03\u7528\n  tail_recur(n - 1, res + n)\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5c3e\u9012\u5f52\u7684\u6267\u884c\u8fc7\u7a0b\u5982\u56fe 2-5 \u6240\u793a\u3002\u5bf9\u6bd4\u666e\u901a\u9012\u5f52\u548c\u5c3e\u9012\u5f52\uff0c\u4e24\u8005\u7684\u6c42\u548c\u64cd\u4f5c\u7684\u6267\u884c\u70b9\u662f\u4e0d\u540c\u7684\u3002

        • \u666e\u901a\u9012\u5f52\uff1a\u6c42\u548c\u64cd\u4f5c\u662f\u5728\u201c\u5f52\u201d\u7684\u8fc7\u7a0b\u4e2d\u6267\u884c\u7684\uff0c\u6bcf\u5c42\u8fd4\u56de\u540e\u90fd\u8981\u518d\u6267\u884c\u4e00\u6b21\u6c42\u548c\u64cd\u4f5c\u3002
        • \u5c3e\u9012\u5f52\uff1a\u6c42\u548c\u64cd\u4f5c\u662f\u5728\u201c\u9012\u201d\u7684\u8fc7\u7a0b\u4e2d\u6267\u884c\u7684\uff0c\u201c\u5f52\u201d\u7684\u8fc7\u7a0b\u53ea\u9700\u5c42\u5c42\u8fd4\u56de\u3002

        \u56fe 2-5 \u00a0 \u5c3e\u9012\u5f52\u8fc7\u7a0b

        Tip

        \u8bf7\u6ce8\u610f\uff0c\u8bb8\u591a\u7f16\u8bd1\u5668\u6216\u89e3\u91ca\u5668\u5e76\u4e0d\u652f\u6301\u5c3e\u9012\u5f52\u4f18\u5316\u3002\u4f8b\u5982\uff0cPython \u9ed8\u8ba4\u4e0d\u652f\u6301\u5c3e\u9012\u5f52\u4f18\u5316\uff0c\u56e0\u6b64\u5373\u4f7f\u51fd\u6570\u662f\u5c3e\u9012\u5f52\u5f62\u5f0f\uff0c\u4ecd\u7136\u53ef\u80fd\u4f1a\u9047\u5230\u6808\u6ea2\u51fa\u95ee\u9898\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#3_1","title":"3. \u00a0 \u9012\u5f52\u6811","text":"

        \u5f53\u5904\u7406\u4e0e\u201c\u5206\u6cbb\u201d\u76f8\u5173\u7684\u7b97\u6cd5\u95ee\u9898\u65f6\uff0c\u9012\u5f52\u5f80\u5f80\u6bd4\u8fed\u4ee3\u7684\u601d\u8def\u66f4\u52a0\u76f4\u89c2\u3001\u4ee3\u7801\u66f4\u52a0\u6613\u8bfb\u3002\u4ee5\u201c\u6590\u6ce2\u90a3\u5951\u6570\u5217\u201d\u4e3a\u4f8b\u3002

        Question

        \u7ed9\u5b9a\u4e00\u4e2a\u6590\u6ce2\u90a3\u5951\u6570\u5217 \\(0, 1, 1, 2, 3, 5, 8, 13, \\dots\\) \uff0c\u6c42\u8be5\u6570\u5217\u7684\u7b2c \\(n\\) \u4e2a\u6570\u5b57\u3002

        \u8bbe\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u7b2c \\(n\\) \u4e2a\u6570\u5b57\u4e3a \\(f(n)\\) \uff0c\u6613\u5f97\u4e24\u4e2a\u7ed3\u8bba\u3002

        • \u6570\u5217\u7684\u524d\u4e24\u4e2a\u6570\u5b57\u4e3a \\(f(1) = 0\\) \u548c \\(f(2) = 1\\) \u3002
        • \u6570\u5217\u4e2d\u7684\u6bcf\u4e2a\u6570\u5b57\u662f\u524d\u4e24\u4e2a\u6570\u5b57\u7684\u548c\uff0c\u5373 \\(f(n) = f(n - 1) + f(n - 2)\\) \u3002

        \u6309\u7167\u9012\u63a8\u5173\u7cfb\u8fdb\u884c\u9012\u5f52\u8c03\u7528\uff0c\u5c06\u524d\u4e24\u4e2a\u6570\u5b57\u4f5c\u4e3a\u7ec8\u6b62\u6761\u4ef6\uff0c\u4fbf\u53ef\u5199\u51fa\u9012\u5f52\u4ee3\u7801\u3002\u8c03\u7528 fib(n) \u5373\u53ef\u5f97\u5230\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u7b2c \\(n\\) \u4e2a\u6570\u5b57\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
        def fib(n: int) -> int:\n    \"\"\"\u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52\"\"\"\n    # \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if n == 1 or n == 2:\n        return n - 1\n    # \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n    res = fib(n - 1) + fib(n - 2)\n    # \u8fd4\u56de\u7ed3\u679c f(n)\n    return res\n
        recursion.cpp
        /* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n    // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n    int res = fib(n - 1) + fib(n - 2);\n    // \u8fd4\u56de\u7ed3\u679c f(n)\n    return res;\n}\n
        recursion.java
        /* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n    // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n    int res = fib(n - 1) + fib(n - 2);\n    // \u8fd4\u56de\u7ed3\u679c f(n)\n    return res;\n}\n
        recursion.cs
        /* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint Fib(int n) {\n    // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n    int res = Fib(n - 1) + Fib(n - 2);\n    // \u8fd4\u56de\u7ed3\u679c f(n)\n    return res;\n}\n
        recursion.go
        /* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunc fib(n int) int {\n    // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if n == 1 || n == 2 {\n        return n - 1\n    }\n    // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n    res := fib(n-1) + fib(n-2)\n    // \u8fd4\u56de\u7ed3\u679c f(n)\n    return res\n}\n
        recursion.swift
        /* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunc fib(n: Int) -> Int {\n    // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if n == 1 || n == 2 {\n        return n - 1\n    }\n    // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n    let res = fib(n: n - 1) + fib(n: n - 2)\n    // \u8fd4\u56de\u7ed3\u679c f(n)\n    return res\n}\n
        recursion.js
        /* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunction fib(n) {\n    // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if (n === 1 || n === 2) return n - 1;\n    // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n    const res = fib(n - 1) + fib(n - 2);\n    // \u8fd4\u56de\u7ed3\u679c f(n)\n    return res;\n}\n
        recursion.ts
        /* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunction fib(n: number): number {\n    // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if (n === 1 || n === 2) return n - 1;\n    // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n    const res = fib(n - 1) + fib(n - 2);\n    // \u8fd4\u56de\u7ed3\u679c f(n)\n    return res;\n}\n
        recursion.dart
        /* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n  // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n  if (n == 1 || n == 2) return n - 1;\n  // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n  int res = fib(n - 1) + fib(n - 2);\n  // \u8fd4\u56de\u7ed3\u679c f(n)\n  return res;\n}\n
        recursion.rs
        /* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfn fib(n: i32) -> i32 {\n    // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if n == 1 || n == 2 {\n        return n - 1;\n    }\n    // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n    let res = fib(n - 1) + fib(n - 2);\n    // \u8fd4\u56de\u7ed3\u679c\n    res\n}\n
        recursion.c
        /* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n    // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n    int res = fib(n - 1) + fib(n - 2);\n    // \u8fd4\u56de\u7ed3\u679c f(n)\n    return res;\n}\n
        recursion.kt
        /* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfun fib(n: Int): Int {\n    // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1\n    // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n    val res = fib(n - 1) + fib(n - 2)\n    // \u8fd4\u56de\u7ed3\u679c f(n)\n    return res\n}\n
        recursion.rb
        ### \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 ###\ndef fib(n)\n  # \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n  return n - 1 if n == 1 || n == 2\n  # \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n  res = fib(n - 1) + fib(n - 2)\n  # \u8fd4\u56de\u7ed3\u679c f(n)\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u89c2\u5bdf\u4ee5\u4e0a\u4ee3\u7801\uff0c\u6211\u4eec\u5728\u51fd\u6570\u5185\u9012\u5f52\u8c03\u7528\u4e86\u4e24\u4e2a\u51fd\u6570\uff0c\u8fd9\u610f\u5473\u7740\u4ece\u4e00\u4e2a\u8c03\u7528\u4ea7\u751f\u4e86\u4e24\u4e2a\u8c03\u7528\u5206\u652f\u3002\u5982\u56fe 2-6 \u6240\u793a\uff0c\u8fd9\u6837\u4e0d\u65ad\u9012\u5f52\u8c03\u7528\u4e0b\u53bb\uff0c\u6700\u7ec8\u5c06\u4ea7\u751f\u4e00\u68f5\u5c42\u6570\u4e3a \\(n\\) \u7684\u9012\u5f52\u6811\uff08recursion tree\uff09\u3002

        \u56fe 2-6 \u00a0 \u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u9012\u5f52\u6811

        \u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u9012\u5f52\u4f53\u73b0\u4e86\u201c\u5c06\u95ee\u9898\u5206\u89e3\u4e3a\u66f4\u5c0f\u5b50\u95ee\u9898\u201d\u7684\u601d\u7ef4\u8303\u5f0f\uff0c\u8fd9\u79cd\u5206\u6cbb\u7b56\u7565\u81f3\u5173\u91cd\u8981\u3002

        • \u4ece\u7b97\u6cd5\u89d2\u5ea6\u770b\uff0c\u641c\u7d22\u3001\u6392\u5e8f\u3001\u56de\u6eaf\u3001\u5206\u6cbb\u3001\u52a8\u6001\u89c4\u5212\u7b49\u8bb8\u591a\u91cd\u8981\u7b97\u6cd5\u7b56\u7565\u76f4\u63a5\u6216\u95f4\u63a5\u5730\u5e94\u7528\u4e86\u8fd9\u79cd\u601d\u7ef4\u65b9\u5f0f\u3002
        • \u4ece\u6570\u636e\u7ed3\u6784\u89d2\u5ea6\u770b\uff0c\u9012\u5f52\u5929\u7136\u9002\u5408\u5904\u7406\u94fe\u8868\u3001\u6811\u548c\u56fe\u7684\u76f8\u5173\u95ee\u9898\uff0c\u56e0\u4e3a\u5b83\u4eec\u975e\u5e38\u9002\u5408\u7528\u5206\u6cbb\u601d\u60f3\u8fdb\u884c\u5206\u6790\u3002
        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#223","title":"2.2.3 \u00a0 \u4e24\u8005\u5bf9\u6bd4","text":"

        \u603b\u7ed3\u4ee5\u4e0a\u5185\u5bb9\uff0c\u5982\u8868 2-1 \u6240\u793a\uff0c\u8fed\u4ee3\u548c\u9012\u5f52\u5728\u5b9e\u73b0\u3001\u6027\u80fd\u548c\u9002\u7528\u6027\u4e0a\u6709\u6240\u4e0d\u540c\u3002

        \u8868 2-1 \u00a0 \u8fed\u4ee3\u4e0e\u9012\u5f52\u7279\u70b9\u5bf9\u6bd4

        \u8fed\u4ee3 \u9012\u5f52 \u5b9e\u73b0\u65b9\u5f0f \u5faa\u73af\u7ed3\u6784 \u51fd\u6570\u8c03\u7528\u81ea\u8eab \u65f6\u95f4\u6548\u7387 \u6548\u7387\u901a\u5e38\u8f83\u9ad8\uff0c\u65e0\u51fd\u6570\u8c03\u7528\u5f00\u9500 \u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u90fd\u4f1a\u4ea7\u751f\u5f00\u9500 \u5185\u5b58\u4f7f\u7528 \u901a\u5e38\u4f7f\u7528\u56fa\u5b9a\u5927\u5c0f\u7684\u5185\u5b58\u7a7a\u95f4 \u7d2f\u79ef\u51fd\u6570\u8c03\u7528\u53ef\u80fd\u4f7f\u7528\u5927\u91cf\u7684\u6808\u5e27\u7a7a\u95f4 \u9002\u7528\u95ee\u9898 \u9002\u7528\u4e8e\u7b80\u5355\u5faa\u73af\u4efb\u52a1\uff0c\u4ee3\u7801\u76f4\u89c2\u3001\u53ef\u8bfb\u6027\u597d \u9002\u7528\u4e8e\u5b50\u95ee\u9898\u5206\u89e3\uff0c\u5982\u6811\u3001\u56fe\u3001\u5206\u6cbb\u3001\u56de\u6eaf\u7b49\uff0c\u4ee3\u7801\u7ed3\u6784\u7b80\u6d01\u3001\u6e05\u6670

        Tip

        \u5982\u679c\u611f\u89c9\u4ee5\u4e0b\u5185\u5bb9\u7406\u89e3\u56f0\u96be\uff0c\u53ef\u4ee5\u5728\u8bfb\u5b8c\u201c\u6808\u201d\u7ae0\u8282\u540e\u518d\u6765\u590d\u4e60\u3002

        \u90a3\u4e48\uff0c\u8fed\u4ee3\u548c\u9012\u5f52\u5177\u6709\u4ec0\u4e48\u5185\u5728\u8054\u7cfb\u5462\uff1f\u4ee5\u4e0a\u8ff0\u9012\u5f52\u51fd\u6570\u4e3a\u4f8b\uff0c\u6c42\u548c\u64cd\u4f5c\u5728\u9012\u5f52\u7684\u201c\u5f52\u201d\u9636\u6bb5\u8fdb\u884c\u3002\u8fd9\u610f\u5473\u7740\u6700\u521d\u88ab\u8c03\u7528\u7684\u51fd\u6570\u5b9e\u9645\u4e0a\u662f\u6700\u540e\u5b8c\u6210\u5176\u6c42\u548c\u64cd\u4f5c\u7684\uff0c\u8fd9\u79cd\u5de5\u4f5c\u673a\u5236\u4e0e\u6808\u7684\u201c\u5148\u5165\u540e\u51fa\u201d\u539f\u5219\u5f02\u66f2\u540c\u5de5\u3002

        \u4e8b\u5b9e\u4e0a\uff0c\u201c\u8c03\u7528\u6808\u201d\u548c\u201c\u6808\u5e27\u7a7a\u95f4\u201d\u8fd9\u7c7b\u9012\u5f52\u672f\u8bed\u5df2\u7ecf\u6697\u793a\u4e86\u9012\u5f52\u4e0e\u6808\u4e4b\u95f4\u7684\u5bc6\u5207\u5173\u7cfb\u3002

        1. \u9012\uff1a\u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u7cfb\u7edf\u4f1a\u5728\u201c\u8c03\u7528\u6808\u201d\u4e0a\u4e3a\u8be5\u51fd\u6570\u5206\u914d\u65b0\u7684\u6808\u5e27\uff0c\u7528\u4e8e\u5b58\u50a8\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\u3001\u53c2\u6570\u3001\u8fd4\u56de\u5730\u5740\u7b49\u6570\u636e\u3002
        2. \u5f52\uff1a\u5f53\u51fd\u6570\u5b8c\u6210\u6267\u884c\u5e76\u8fd4\u56de\u65f6\uff0c\u5bf9\u5e94\u7684\u6808\u5e27\u4f1a\u88ab\u4ece\u201c\u8c03\u7528\u6808\u201d\u4e0a\u79fb\u9664\uff0c\u6062\u590d\u4e4b\u524d\u51fd\u6570\u7684\u6267\u884c\u73af\u5883\u3002

        \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u8c03\u7528\u6808\u7684\u884c\u4e3a\uff0c\u4ece\u800c\u5c06\u9012\u5f52\u8f6c\u5316\u4e3a\u8fed\u4ee3\u5f62\u5f0f\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
        def for_loop_recur(n: int) -> int:\n    \"\"\"\u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52\"\"\"\n    # \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n    stack = []\n    res = 0\n    # \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    for i in range(n, 0, -1):\n        # \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n        stack.append(i)\n    # \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    while stack:\n        # \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n        res += stack.pop()\n    # res = 1+2+3+...+n\n    return res\n
        recursion.cpp
        /* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n    // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n    stack<int> stack;\n    int res = 0;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    for (int i = n; i > 0; i--) {\n        // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n        stack.push(i);\n    }\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    while (!stack.empty()) {\n        // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n        res += stack.top();\n        stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
        recursion.java
        /* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n    // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n    Stack<Integer> stack = new Stack<>();\n    int res = 0;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    for (int i = n; i > 0; i--) {\n        // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n        stack.push(i);\n    }\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    while (!stack.isEmpty()) {\n        // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n        res += stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
        recursion.cs
        /* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint ForLoopRecur(int n) {\n    // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n    Stack<int> stack = new();\n    int res = 0;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    for (int i = n; i > 0; i--) {\n        // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n        stack.Push(i);\n    }\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    while (stack.Count > 0) {\n        // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n        res += stack.Pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
        recursion.go
        /* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunc forLoopRecur(n int) int {\n    // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n    stack := list.New()\n    res := 0\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    for i := n; i > 0; i-- {\n        // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n        stack.PushBack(i)\n    }\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    for stack.Len() != 0 {\n        // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n        res += stack.Back().Value.(int)\n        stack.Remove(stack.Back())\n    }\n    // res = 1+2+3+...+n\n    return res\n}\n
        recursion.swift
        /* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunc forLoopRecur(n: Int) -> Int {\n    // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n    var stack: [Int] = []\n    var res = 0\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    for i in (1 ... n).reversed() {\n        // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n        stack.append(i)\n    }\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    while !stack.isEmpty {\n        // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n        res += stack.removeLast()\n    }\n    // res = 1+2+3+...+n\n    return res\n}\n
        recursion.js
        /* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunction forLoopRecur(n) {\n    // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n    const stack = [];\n    let res = 0;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    for (let i = n; i > 0; i--) {\n        // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n        stack.push(i);\n    }\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    while (stack.length) {\n        // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n        res += stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
        recursion.ts
        /* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunction forLoopRecur(n: number): number {\n    // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808 \n    const stack: number[] = [];\n    let res: number = 0;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    for (let i = n; i > 0; i--) {\n        // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n        stack.push(i);\n    }\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    while (stack.length) {\n        // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n        res += stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
        recursion.dart
        /* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n  // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n  List<int> stack = [];\n  int res = 0;\n  // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n  for (int i = n; i > 0; i--) {\n    // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n    stack.add(i);\n  }\n  // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n  while (!stack.isEmpty) {\n    // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n    res += stack.removeLast();\n  }\n  // res = 1+2+3+...+n\n  return res;\n}\n
        recursion.rs
        /* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfn for_loop_recur(n: i32) -> i32 {\n    // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n    let mut stack = Vec::new();\n    let mut res = 0;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    for i in (1..=n).rev() {\n        // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n        stack.push(i);\n    }\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    while !stack.is_empty() {\n        // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n        res += stack.pop().unwrap();\n    }\n    // res = 1+2+3+...+n\n    res\n}\n
        recursion.c
        /* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n    int stack[1000]; // \u501f\u52a9\u4e00\u4e2a\u5927\u6570\u7ec4\u6765\u6a21\u62df\u6808\n    int top = -1;    // \u6808\u9876\u7d22\u5f15\n    int res = 0;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    for (int i = n; i > 0; i--) {\n        // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n        stack[1 + top++] = i;\n    }\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    while (top >= 0) {\n        // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n        res += stack[top--];\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
        recursion.kt
        /* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfun forLoopRecur(n: Int): Int {\n    // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n    val stack = Stack<Int>()\n    var res = 0\n    // \u9012: \u9012\u5f52\u8c03\u7528\n    for (i in n downTo 0) {\n        // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n        stack.push(i)\n    }\n    // \u5f52: \u8fd4\u56de\u7ed3\u679c\n    while (stack.isNotEmpty()) {\n        // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n        res += stack.pop()\n    }\n    // res = 1+2+3+...+n\n    return res\n}\n
        recursion.rb
        ### \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 ###\ndef for_loop_recur(n)\n  # \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n  stack = []\n  res = 0\n\n  # \u9012\uff1a\u9012\u5f52\u8c03\u7528\n  for i in n.downto(0)\n    # \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n    stack << i\n  end\n  # \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n  while !stack.empty?\n    res += stack.pop\n  end\n\n  # res = 1+2+3+...+n\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u89c2\u5bdf\u4ee5\u4e0a\u4ee3\u7801\uff0c\u5f53\u9012\u5f52\u8f6c\u5316\u4e3a\u8fed\u4ee3\u540e\uff0c\u4ee3\u7801\u53d8\u5f97\u66f4\u52a0\u590d\u6742\u4e86\u3002\u5c3d\u7ba1\u8fed\u4ee3\u548c\u9012\u5f52\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4e92\u76f8\u8f6c\u5316\uff0c\u4f46\u4e0d\u4e00\u5b9a\u503c\u5f97\u8fd9\u6837\u505a\uff0c\u6709\u4ee5\u4e0b\u4e24\u70b9\u539f\u56e0\u3002

        • \u8f6c\u5316\u540e\u7684\u4ee3\u7801\u53ef\u80fd\u66f4\u52a0\u96be\u4ee5\u7406\u89e3\uff0c\u53ef\u8bfb\u6027\u66f4\u5dee\u3002
        • \u5bf9\u4e8e\u67d0\u4e9b\u590d\u6742\u95ee\u9898\uff0c\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\u7684\u884c\u4e3a\u53ef\u80fd\u975e\u5e38\u56f0\u96be\u3002

        \u603b\u4e4b\uff0c\u9009\u62e9\u8fed\u4ee3\u8fd8\u662f\u9012\u5f52\u53d6\u51b3\u4e8e\u7279\u5b9a\u95ee\u9898\u7684\u6027\u8d28\u3002\u5728\u7f16\u7a0b\u5b9e\u8df5\u4e2d\uff0c\u6743\u8861\u4e24\u8005\u7684\u4f18\u52a3\u5e76\u6839\u636e\u60c5\u5883\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u81f3\u5173\u91cd\u8981\u3002

        "},{"location":"chapter_computational_complexity/performance_evaluation/","title":"2.1 \u00a0 \u7b97\u6cd5\u6548\u7387\u8bc4\u4f30","text":"

        \u5728\u7b97\u6cd5\u8bbe\u8ba1\u4e2d\uff0c\u6211\u4eec\u5148\u540e\u8ffd\u6c42\u4ee5\u4e0b\u4e24\u4e2a\u5c42\u9762\u7684\u76ee\u6807\u3002

        1. \u627e\u5230\u95ee\u9898\u89e3\u6cd5\uff1a\u7b97\u6cd5\u9700\u8981\u5728\u89c4\u5b9a\u7684\u8f93\u5165\u8303\u56f4\u5185\u53ef\u9760\u5730\u6c42\u5f97\u95ee\u9898\u7684\u6b63\u786e\u89e3\u3002
        2. \u5bfb\u6c42\u6700\u4f18\u89e3\u6cd5\uff1a\u540c\u4e00\u4e2a\u95ee\u9898\u53ef\u80fd\u5b58\u5728\u591a\u79cd\u89e3\u6cd5\uff0c\u6211\u4eec\u5e0c\u671b\u627e\u5230\u5c3d\u53ef\u80fd\u9ad8\u6548\u7684\u7b97\u6cd5\u3002

        \u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u80fd\u591f\u89e3\u51b3\u95ee\u9898\u7684\u524d\u63d0\u4e0b\uff0c\u7b97\u6cd5\u6548\u7387\u5df2\u6210\u4e3a\u8861\u91cf\u7b97\u6cd5\u4f18\u52a3\u7684\u4e3b\u8981\u8bc4\u4ef7\u6307\u6807\uff0c\u5b83\u5305\u62ec\u4ee5\u4e0b\u4e24\u4e2a\u7ef4\u5ea6\u3002

        • \u65f6\u95f4\u6548\u7387\uff1a\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u7684\u957f\u77ed\u3002
        • \u7a7a\u95f4\u6548\u7387\uff1a\u7b97\u6cd5\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u7684\u5927\u5c0f\u3002

        \u7b80\u800c\u8a00\u4e4b\uff0c\u6211\u4eec\u7684\u76ee\u6807\u662f\u8bbe\u8ba1\u201c\u65e2\u5feb\u53c8\u7701\u201d\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u3002\u800c\u6709\u6548\u5730\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u53ea\u6709\u8fd9\u6837\uff0c\u6211\u4eec\u624d\u80fd\u5c06\u5404\u79cd\u7b97\u6cd5\u8fdb\u884c\u5bf9\u6bd4\uff0c\u8fdb\u800c\u6307\u5bfc\u7b97\u6cd5\u8bbe\u8ba1\u4e0e\u4f18\u5316\u8fc7\u7a0b\u3002

        \u6548\u7387\u8bc4\u4f30\u65b9\u6cd5\u4e3b\u8981\u5206\u4e3a\u4e24\u79cd\uff1a\u5b9e\u9645\u6d4b\u8bd5\u3001\u7406\u8bba\u4f30\u7b97\u3002

        "},{"location":"chapter_computational_complexity/performance_evaluation/#211","title":"2.1.1 \u00a0 \u5b9e\u9645\u6d4b\u8bd5","text":"

        \u5047\u8bbe\u6211\u4eec\u73b0\u5728\u6709\u7b97\u6cd5 A \u548c\u7b97\u6cd5 B \uff0c\u5b83\u4eec\u90fd\u80fd\u89e3\u51b3\u540c\u4e00\u95ee\u9898\uff0c\u73b0\u5728\u9700\u8981\u5bf9\u6bd4\u8fd9\u4e24\u4e2a\u7b97\u6cd5\u7684\u6548\u7387\u3002\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u627e\u4e00\u53f0\u8ba1\u7b97\u673a\uff0c\u8fd0\u884c\u8fd9\u4e24\u4e2a\u7b97\u6cd5\uff0c\u5e76\u76d1\u63a7\u8bb0\u5f55\u5b83\u4eec\u7684\u8fd0\u884c\u65f6\u95f4\u548c\u5185\u5b58\u5360\u7528\u60c5\u51b5\u3002\u8fd9\u79cd\u8bc4\u4f30\u65b9\u5f0f\u80fd\u591f\u53cd\u6620\u771f\u5b9e\u60c5\u51b5\uff0c\u4f46\u4e5f\u5b58\u5728\u8f83\u5927\u7684\u5c40\u9650\u6027\u3002

        \u4e00\u65b9\u9762\uff0c\u96be\u4ee5\u6392\u9664\u6d4b\u8bd5\u73af\u5883\u7684\u5e72\u6270\u56e0\u7d20\u3002\u786c\u4ef6\u914d\u7f6e\u4f1a\u5f71\u54cd\u7b97\u6cd5\u7684\u6027\u80fd\u8868\u73b0\u3002\u6bd4\u5982\u4e00\u4e2a\u7b97\u6cd5\u7684\u5e76\u884c\u5ea6\u8f83\u9ad8\uff0c\u90a3\u4e48\u5b83\u5c31\u66f4\u9002\u5408\u5728\u591a\u6838 CPU \u4e0a\u8fd0\u884c\uff0c\u4e00\u4e2a\u7b97\u6cd5\u7684\u5185\u5b58\u64cd\u4f5c\u5bc6\u96c6\uff0c\u90a3\u4e48\u5b83\u5728\u9ad8\u6027\u80fd\u5185\u5b58\u4e0a\u7684\u8868\u73b0\u5c31\u4f1a\u66f4\u597d\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u7b97\u6cd5\u5728\u4e0d\u540c\u7684\u673a\u5668\u4e0a\u7684\u6d4b\u8bd5\u7ed3\u679c\u53ef\u80fd\u662f\u4e0d\u4e00\u81f4\u7684\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u9700\u8981\u5728\u5404\u79cd\u673a\u5668\u4e0a\u8fdb\u884c\u6d4b\u8bd5\uff0c\u7edf\u8ba1\u5e73\u5747\u6548\u7387\uff0c\u800c\u8fd9\u662f\u4e0d\u73b0\u5b9e\u7684\u3002

        \u53e6\u4e00\u65b9\u9762\uff0c\u5c55\u5f00\u5b8c\u6574\u6d4b\u8bd5\u975e\u5e38\u8017\u8d39\u8d44\u6e90\u3002\u968f\u7740\u8f93\u5165\u6570\u636e\u91cf\u7684\u53d8\u5316\uff0c\u7b97\u6cd5\u4f1a\u8868\u73b0\u51fa\u4e0d\u540c\u7684\u6548\u7387\u3002\u4f8b\u5982\uff0c\u5728\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5c0f\u65f6\uff0c\u7b97\u6cd5 A \u7684\u8fd0\u884c\u65f6\u95f4\u6bd4\u7b97\u6cd5 B \u77ed\uff1b\u800c\u5728\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5927\u65f6\uff0c\u6d4b\u8bd5\u7ed3\u679c\u53ef\u80fd\u6070\u6070\u76f8\u53cd\u3002\u56e0\u6b64\uff0c\u4e3a\u4e86\u5f97\u5230\u6709\u8bf4\u670d\u529b\u7684\u7ed3\u8bba\uff0c\u6211\u4eec\u9700\u8981\u6d4b\u8bd5\u5404\u79cd\u89c4\u6a21\u7684\u8f93\u5165\u6570\u636e\uff0c\u800c\u8fd9\u9700\u8981\u8017\u8d39\u5927\u91cf\u7684\u8ba1\u7b97\u8d44\u6e90\u3002

        "},{"location":"chapter_computational_complexity/performance_evaluation/#212","title":"2.1.2 \u00a0 \u7406\u8bba\u4f30\u7b97","text":"

        \u7531\u4e8e\u5b9e\u9645\u6d4b\u8bd5\u5177\u6709\u8f83\u5927\u7684\u5c40\u9650\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u4ec5\u901a\u8fc7\u4e00\u4e9b\u8ba1\u7b97\u6765\u8bc4\u4f30\u7b97\u6cd5\u7684\u6548\u7387\u3002\u8fd9\u79cd\u4f30\u7b97\u65b9\u6cd5\u88ab\u79f0\u4e3a\u6e10\u8fd1\u590d\u6742\u5ea6\u5206\u6790\uff08asymptotic complexity analysis\uff09\uff0c\u7b80\u79f0\u590d\u6742\u5ea6\u5206\u6790\u3002

        \u590d\u6742\u5ea6\u5206\u6790\u80fd\u591f\u4f53\u73b0\u7b97\u6cd5\u8fd0\u884c\u6240\u9700\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u8d44\u6e90\u4e0e\u8f93\u5165\u6570\u636e\u89c4\u6a21\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u5b83\u63cf\u8ff0\u4e86\u968f\u7740\u8f93\u5165\u6570\u636e\u89c4\u6a21\u7684\u589e\u52a0\uff0c\u7b97\u6cd5\u6267\u884c\u6240\u9700\u65f6\u95f4\u548c\u7a7a\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u3002\u8fd9\u4e2a\u5b9a\u4e49\u6709\u4e9b\u62d7\u53e3\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u5206\u4e3a\u4e09\u4e2a\u91cd\u70b9\u6765\u7406\u89e3\u3002

        • \u201c\u65f6\u95f4\u548c\u7a7a\u95f4\u8d44\u6e90\u201d\u5206\u522b\u5bf9\u5e94\u65f6\u95f4\u590d\u6742\u5ea6\uff08time complexity\uff09\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\uff08space complexity\uff09\u3002
        • \u201c\u968f\u7740\u8f93\u5165\u6570\u636e\u89c4\u6a21\u7684\u589e\u52a0\u201d\u610f\u5473\u7740\u590d\u6742\u5ea6\u53cd\u6620\u4e86\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u4e0e\u8f93\u5165\u6570\u636e\u89c4\u6a21\u4e4b\u95f4\u7684\u5173\u7cfb\u3002
        • \u201c\u65f6\u95f4\u548c\u7a7a\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u201d\u8868\u793a\u590d\u6742\u5ea6\u5206\u6790\u5173\u6ce8\u7684\u4e0d\u662f\u8fd0\u884c\u65f6\u95f4\u6216\u5360\u7528\u7a7a\u95f4\u7684\u5177\u4f53\u503c\uff0c\u800c\u662f\u65f6\u95f4\u6216\u7a7a\u95f4\u589e\u957f\u7684\u201c\u5feb\u6162\u201d\u3002

        \u590d\u6742\u5ea6\u5206\u6790\u514b\u670d\u4e86\u5b9e\u9645\u6d4b\u8bd5\u65b9\u6cd5\u7684\u5f0a\u7aef\uff0c\u4f53\u73b0\u5728\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u3002

        • \u5b83\u65e0\u9700\u5b9e\u9645\u8fd0\u884c\u4ee3\u7801\uff0c\u66f4\u52a0\u7eff\u8272\u8282\u80fd\u3002
        • \u5b83\u72ec\u7acb\u4e8e\u6d4b\u8bd5\u73af\u5883\uff0c\u5206\u6790\u7ed3\u679c\u9002\u7528\u4e8e\u6240\u6709\u8fd0\u884c\u5e73\u53f0\u3002
        • \u5b83\u53ef\u4ee5\u4f53\u73b0\u4e0d\u540c\u6570\u636e\u91cf\u4e0b\u7684\u7b97\u6cd5\u6548\u7387\uff0c\u5c24\u5176\u662f\u5728\u5927\u6570\u636e\u91cf\u4e0b\u7684\u7b97\u6cd5\u6027\u80fd\u3002

        Tip

        \u5982\u679c\u4f60\u4ecd\u5bf9\u590d\u6742\u5ea6\u7684\u6982\u5ff5\u611f\u5230\u56f0\u60d1\uff0c\u65e0\u987b\u62c5\u5fc3\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7ae0\u8282\u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u3002

        \u590d\u6742\u5ea6\u5206\u6790\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u628a\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\u7684\u201c\u6807\u5c3a\u201d\uff0c\u4f7f\u6211\u4eec\u53ef\u4ee5\u8861\u91cf\u6267\u884c\u67d0\u4e2a\u7b97\u6cd5\u6240\u9700\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u8d44\u6e90\uff0c\u5bf9\u6bd4\u4e0d\u540c\u7b97\u6cd5\u4e4b\u95f4\u7684\u6548\u7387\u3002

        \u590d\u6742\u5ea6\u662f\u4e2a\u6570\u5b66\u6982\u5ff5\uff0c\u5bf9\u4e8e\u521d\u5b66\u8005\u53ef\u80fd\u6bd4\u8f83\u62bd\u8c61\uff0c\u5b66\u4e60\u96be\u5ea6\u76f8\u5bf9\u8f83\u9ad8\u3002\u4ece\u8fd9\u4e2a\u89d2\u5ea6\u770b\uff0c\u590d\u6742\u5ea6\u5206\u6790\u53ef\u80fd\u4e0d\u592a\u9002\u5408\u4f5c\u4e3a\u6700\u5148\u4ecb\u7ecd\u7684\u5185\u5bb9\u3002\u7136\u800c\uff0c\u5f53\u6211\u4eec\u8ba8\u8bba\u67d0\u4e2a\u6570\u636e\u7ed3\u6784\u6216\u7b97\u6cd5\u7684\u7279\u70b9\u65f6\uff0c\u96be\u4ee5\u907f\u514d\u8981\u5206\u6790\u5176\u8fd0\u884c\u901f\u5ea6\u548c\u7a7a\u95f4\u4f7f\u7528\u60c5\u51b5\u3002

        \u7efc\u4e0a\u6240\u8ff0\uff0c\u5efa\u8bae\u4f60\u5728\u6df1\u5165\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u4e4b\u524d\uff0c\u5148\u5bf9\u590d\u6742\u5ea6\u5206\u6790\u5efa\u7acb\u521d\u6b65\u7684\u4e86\u89e3\uff0c\u4ee5\u4fbf\u80fd\u591f\u5b8c\u6210\u7b80\u5355\u7b97\u6cd5\u7684\u590d\u6742\u5ea6\u5206\u6790\u3002

        "},{"location":"chapter_computational_complexity/space_complexity/","title":"2.4 \u00a0 \u7a7a\u95f4\u590d\u6742\u5ea6","text":"

        \u7a7a\u95f4\u590d\u6742\u5ea6\uff08space complexity\uff09\u7528\u4e8e\u8861\u91cf\u7b97\u6cd5\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u968f\u7740\u6570\u636e\u91cf\u53d8\u5927\u65f6\u7684\u589e\u957f\u8d8b\u52bf\u3002\u8fd9\u4e2a\u6982\u5ff5\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u975e\u5e38\u7c7b\u4f3c\uff0c\u53ea\u9700\u5c06\u201c\u8fd0\u884c\u65f6\u95f4\u201d\u66ff\u6362\u4e3a\u201c\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u201d\u3002

        "},{"location":"chapter_computational_complexity/space_complexity/#241","title":"2.4.1 \u00a0 \u7b97\u6cd5\u76f8\u5173\u7a7a\u95f4","text":"

        \u7b97\u6cd5\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\u3002

        • \u8f93\u5165\u7a7a\u95f4\uff1a\u7528\u4e8e\u5b58\u50a8\u7b97\u6cd5\u7684\u8f93\u5165\u6570\u636e\u3002
        • \u6682\u5b58\u7a7a\u95f4\uff1a\u7528\u4e8e\u5b58\u50a8\u7b97\u6cd5\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u53d8\u91cf\u3001\u5bf9\u8c61\u3001\u51fd\u6570\u4e0a\u4e0b\u6587\u7b49\u6570\u636e\u3002
        • \u8f93\u51fa\u7a7a\u95f4\uff1a\u7528\u4e8e\u5b58\u50a8\u7b97\u6cd5\u7684\u8f93\u51fa\u6570\u636e\u3002

        \u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u7edf\u8ba1\u8303\u56f4\u662f\u201c\u6682\u5b58\u7a7a\u95f4\u201d\u52a0\u4e0a\u201c\u8f93\u51fa\u7a7a\u95f4\u201d\u3002

        \u6682\u5b58\u7a7a\u95f4\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5212\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\u3002

        • \u6682\u5b58\u6570\u636e\uff1a\u7528\u4e8e\u4fdd\u5b58\u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u5404\u79cd\u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u7b49\u3002
        • \u6808\u5e27\u7a7a\u95f4\uff1a\u7528\u4e8e\u4fdd\u5b58\u8c03\u7528\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u6570\u636e\u3002\u7cfb\u7edf\u5728\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u65f6\u90fd\u4f1a\u5728\u6808\u9876\u90e8\u521b\u5efa\u4e00\u4e2a\u6808\u5e27\uff0c\u51fd\u6570\u8fd4\u56de\u540e\uff0c\u6808\u5e27\u7a7a\u95f4\u4f1a\u88ab\u91ca\u653e\u3002
        • \u6307\u4ee4\u7a7a\u95f4\uff1a\u7528\u4e8e\u4fdd\u5b58\u7f16\u8bd1\u540e\u7684\u7a0b\u5e8f\u6307\u4ee4\uff0c\u5728\u5b9e\u9645\u7edf\u8ba1\u4e2d\u901a\u5e38\u5ffd\u7565\u4e0d\u8ba1\u3002

        \u5728\u5206\u6790\u4e00\u6bb5\u7a0b\u5e8f\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u7edf\u8ba1\u6682\u5b58\u6570\u636e\u3001\u6808\u5e27\u7a7a\u95f4\u548c\u8f93\u51fa\u6570\u636e\u4e09\u90e8\u5206\uff0c\u5982\u56fe 2-15 \u6240\u793a\u3002

        \u56fe 2-15 \u00a0 \u7b97\u6cd5\u4f7f\u7528\u7684\u76f8\u5173\u7a7a\u95f4

        \u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        class Node:\n    \"\"\"\u7c7b\"\"\"\n    def __init__(self, x: int):\n        self.val: int = x              # \u8282\u70b9\u503c\n        self.next: Node | None = None  # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n\ndef function() -> int:\n    \"\"\"\u51fd\u6570\"\"\"\n    # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n    return 0\n\ndef algorithm(n) -> int:  # \u8f93\u5165\u6570\u636e\n    A = 0                 # \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff0c\u4e00\u822c\u7528\u5927\u5199\u5b57\u6bcd\u8868\u793a\uff09\n    b = 0                 # \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    node = Node(0)        # \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n    c = function()        # \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    return A + b + c      # \u8f93\u51fa\u6570\u636e\n
        /* \u7ed3\u6784\u4f53 */\nstruct Node {\n    int val;\n    Node *next;\n    Node(int x) : val(x), next(nullptr) {}\n};\n\n/* \u51fd\u6570 */\nint func() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n    return 0;\n}\n\nint algorithm(int n) {        // \u8f93\u5165\u6570\u636e\n    const int a = 0;          // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n    int b = 0;                // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    Node* node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n    int c = func();           // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    return a + b + c;         // \u8f93\u51fa\u6570\u636e\n}\n
        /* \u7c7b */\nclass Node {\n    int val;\n    Node next;\n    Node(int x) { val = x; }\n}\n\n/* \u51fd\u6570 */\nint function() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n    return 0;\n}\n\nint algorithm(int n) {        // \u8f93\u5165\u6570\u636e\n    final int a = 0;          // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n    int b = 0;                // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    Node node = new Node(0);  // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n    int c = function();       // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    return a + b + c;         // \u8f93\u51fa\u6570\u636e\n}\n
        /* \u7c7b */\nclass Node(int x) {\n    int val = x;\n    Node next;\n}\n\n/* \u51fd\u6570 */\nint Function() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n    return 0;\n}\n\nint Algorithm(int n) {        // \u8f93\u5165\u6570\u636e\n    const int a = 0;          // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n    int b = 0;                // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    Node node = new(0);       // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n    int c = Function();       // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    return a + b + c;         // \u8f93\u51fa\u6570\u636e\n}\n
        /* \u7ed3\u6784\u4f53 */\ntype node struct {\n    val  int\n    next *node\n}\n\n/* \u521b\u5efa node \u7ed3\u6784\u4f53  */\nfunc newNode(val int) *node {\n    return &node{val: val}\n}\n\n/* \u51fd\u6570 */\nfunc function() int {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n    return 0\n}\n\nfunc algorithm(n int) int { // \u8f93\u5165\u6570\u636e\n    const a = 0             // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n    b := 0                  // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    newNode(0)              // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n    c := function()         // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    return a + b + c        // \u8f93\u51fa\u6570\u636e\n}\n
        /* \u7c7b */\nclass Node {\n    var val: Int\n    var next: Node?\n\n    init(x: Int) {\n        val = x\n    }\n}\n\n/* \u51fd\u6570 */\nfunc function() -> Int {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n    return 0\n}\n\nfunc algorithm(n: Int) -> Int { // \u8f93\u5165\u6570\u636e\n    let a = 0             // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n    var b = 0             // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    let node = Node(x: 0) // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n    let c = function()    // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    return a + b + c      // \u8f93\u51fa\u6570\u636e\n}\n
        /* \u7c7b */\nclass Node {\n    val;\n    next;\n    constructor(val) {\n        this.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\n        this.next = null;                       // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n    }\n}\n\n/* \u51fd\u6570 */\nfunction constFunc() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n\nfunction algorithm(n) {       // \u8f93\u5165\u6570\u636e\n    const a = 0;              // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n    let b = 0;                // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    const node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n    const c = constFunc();    // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    return a + b + c;         // \u8f93\u51fa\u6570\u636e\n}\n
        /* \u7c7b */\nclass Node {\n    val: number;\n    next: Node | null;\n    constructor(val?: number) {\n        this.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\n        this.next = null;                       // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n    }\n}\n\n/* \u51fd\u6570 */\nfunction constFunc(): number {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n\nfunction algorithm(n: number): number { // \u8f93\u5165\u6570\u636e\n    const a = 0;                        // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n    let b = 0;                          // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    const node = new Node(0);           // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n    const c = constFunc();              // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    return a + b + c;                   // \u8f93\u51fa\u6570\u636e\n}\n
        /* \u7c7b */\nclass Node {\n  int val;\n  Node next;\n  Node(this.val, [this.next]);\n}\n\n/* \u51fd\u6570 */\nint function() {\n  // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n  return 0;\n}\n\nint algorithm(int n) {  // \u8f93\u5165\u6570\u636e\n  const int a = 0;      // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n  int b = 0;            // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n  Node node = Node(0);  // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n  int c = function();   // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n  return a + b + c;     // \u8f93\u51fa\u6570\u636e\n}\n
        use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u7ed3\u6784\u4f53 */\nstruct Node {\n    val: i32,\n    next: Option<Rc<RefCell<Node>>>,\n}\n\n/* \u521b\u5efa Node \u7ed3\u6784\u4f53 */\nimpl Node {\n    fn new(val: i32) -> Self {\n        Self { val: val, next: None }\n    }\n}\n\n/* \u51fd\u6570 */\nfn function() -> i32 {      \n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n    return 0;\n}\n\nfn algorithm(n: i32) -> i32 {       // \u8f93\u5165\u6570\u636e\n    const a: i32 = 0;               // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n    let mut b = 0;                  // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    let node = Node::new(0);        // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n    let c = function();             // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    return a + b + c;               // \u8f93\u51fa\u6570\u636e\n}\n
        /* \u51fd\u6570 */\nint func() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n    return 0;\n}\n\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\n    const int a = 0;   // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n    int b = 0;         // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    int c = func();    // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    return a + b + c;  // \u8f93\u51fa\u6570\u636e\n}\n
        /* \u7c7b */\nclass Node(var _val: Int) {\n    var next: Node? = null\n}\n\n/* \u51fd\u6570 */\nfun function(): Int {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n    return 0\n}\n\nfun algorithm(n: Int): Int { // \u8f93\u5165\u6570\u636e\n    val a = 0                // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n    var b = 0                // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    val node = Node(0)       // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n    val c = function()       // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    return a + b + c         // \u8f93\u51fa\u6570\u636e\n}\n
        ### \u7c7b ###\nclass Node\n    attr_accessor :val      # \u8282\u70b9\u503c\n    attr_accessor :next     # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n\n    def initialize(x)\n        @val = x\n    end\nend\n\n### \u51fd\u6570 ###\ndef function\n    # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n    0\nend\n\n### \u7b97\u6cd5 ###\ndef algorithm(n)        # \u8f93\u5165\u6570\u636e\n    a = 0               # \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n    b = 0               # \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    node = Node.new(0)  # \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n    c = function        # \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    a + b + c           # \u8f93\u51fa\u6570\u636e\nend\n
        "},{"location":"chapter_computational_complexity/space_complexity/#242","title":"2.4.2 \u00a0 \u63a8\u7b97\u65b9\u6cd5","text":"

        \u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u5927\u81f4\u76f8\u540c\uff0c\u53ea\u9700\u5c06\u7edf\u8ba1\u5bf9\u8c61\u4ece\u201c\u64cd\u4f5c\u6570\u91cf\u201d\u8f6c\u4e3a\u201c\u4f7f\u7528\u7a7a\u95f4\u5927\u5c0f\u201d\u3002

        \u800c\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u540c\u7684\u662f\uff0c\u6211\u4eec\u901a\u5e38\u53ea\u5173\u6ce8\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u3002\u8fd9\u662f\u56e0\u4e3a\u5185\u5b58\u7a7a\u95f4\u662f\u4e00\u9879\u786c\u6027\u8981\u6c42\uff0c\u6211\u4eec\u5fc5\u987b\u786e\u4fdd\u5728\u6240\u6709\u8f93\u5165\u6570\u636e\u4e0b\u90fd\u6709\u8db3\u591f\u7684\u5185\u5b58\u7a7a\u95f4\u9884\u7559\u3002

        \u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e2d\u7684\u201c\u6700\u5dee\u201d\u6709\u4e24\u5c42\u542b\u4e49\u3002

        1. \u4ee5\u6700\u5dee\u8f93\u5165\u6570\u636e\u4e3a\u51c6\uff1a\u5f53 \\(n < 10\\) \u65f6\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff1b\u4f46\u5f53 \\(n > 10\\) \u65f6\uff0c\u521d\u59cb\u5316\u7684\u6570\u7ec4 nums \u5360\u7528 \\(O(n)\\) \u7a7a\u95f4\uff0c\u56e0\u6b64\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002
        2. \u4ee5\u7b97\u6cd5\u8fd0\u884c\u4e2d\u7684\u5cf0\u503c\u5185\u5b58\u4e3a\u51c6\uff1a\u4f8b\u5982\uff0c\u7a0b\u5e8f\u5728\u6267\u884c\u6700\u540e\u4e00\u884c\u4e4b\u524d\uff0c\u5360\u7528 \\(O(1)\\) \u7a7a\u95f4\uff1b\u5f53\u521d\u59cb\u5316\u6570\u7ec4 nums \u65f6\uff0c\u7a0b\u5e8f\u5360\u7528 \\(O(n)\\) \u7a7a\u95f4\uff0c\u56e0\u6b64\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        def algorithm(n: int):\n    a = 0               # O(1)\n    b = [0] * 10000     # O(1)\n    if n > 10:\n        nums = [0] * n  # O(n)\n
        void algorithm(int n) {\n    int a = 0;               // O(1)\n    vector<int> b(10000);    // O(1)\n    if (n > 10)\n        vector<int> nums(n); // O(n)\n}\n
        void algorithm(int n) {\n    int a = 0;                   // O(1)\n    int[] b = new int[10000];    // O(1)\n    if (n > 10)\n        int[] nums = new int[n]; // O(n)\n}\n
        void Algorithm(int n) {\n    int a = 0;                   // O(1)\n    int[] b = new int[10000];    // O(1)\n    if (n > 10) {\n        int[] nums = new int[n]; // O(n)\n    }\n}\n
        func algorithm(n int) {\n    a := 0                      // O(1)\n    b := make([]int, 10000)     // O(1)\n    var nums []int\n    if n > 10 {\n        nums := make([]int, n)  // O(n)\n    }\n    fmt.Println(a, b, nums)\n}\n
        func algorithm(n: Int) {\n    let a = 0 // O(1)\n    let b = Array(repeating: 0, count: 10000) // O(1)\n    if n > 10 {\n        let nums = Array(repeating: 0, count: n) // O(n)\n    }\n}\n
        function algorithm(n) {\n    const a = 0;                   // O(1)\n    const b = new Array(10000);    // O(1)\n    if (n > 10) {\n        const nums = new Array(n); // O(n)\n    }\n}\n
        function algorithm(n: number): void {\n    const a = 0;                   // O(1)\n    const b = new Array(10000);    // O(1)\n    if (n > 10) {\n        const nums = new Array(n); // O(n)\n    }\n}\n
        void algorithm(int n) {\n  int a = 0;                            // O(1)\n  List<int> b = List.filled(10000, 0);  // O(1)\n  if (n > 10) {\n    List<int> nums = List.filled(n, 0); // O(n)\n  }\n}\n
        fn algorithm(n: i32) {\n    let a = 0;                              // O(1)\n    let b = [0; 10000];                     // O(1)\n    if n > 10 {\n        let nums = vec![0; n as usize];     // O(n)\n    }\n}\n
        void algorithm(int n) {\n    int a = 0;               // O(1)\n    int b[10000];            // O(1)\n    if (n > 10)\n        int nums[n] = {0};   // O(n)\n}\n
        fun algorithm(n: Int) {\n    val a = 0                    // O(1)\n    val b = IntArray(10000)      // O(1)\n    if (n > 10) {\n        val nums = IntArray(n)   // O(n)\n    }\n}\n
        def algorithm(n)\n    a = 0                           # O(1)\n    b = Array.new(10000)            # O(1)\n    nums = Array.new(n) if n > 10   # O(n)\nend\n

        \u5728\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u7edf\u8ba1\u6808\u5e27\u7a7a\u95f4\u3002\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        def function() -> int:\n    # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0\n\ndef loop(n: int):\n    \"\"\"\u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1)\"\"\"\n    for _ in range(n):\n        function()\n\ndef recur(n: int):\n    \"\"\"\u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\"\"\"\n    if n == 1:\n        return\n    return recur(n - 1)\n
        int func() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n/* \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) */\nvoid loop(int n) {\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n/* \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) */\nvoid recur(int n) {\n    if (n == 1) return;\n    recur(n - 1);\n}\n
        int function() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n/* \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) */\nvoid loop(int n) {\n    for (int i = 0; i < n; i++) {\n        function();\n    }\n}\n/* \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) */\nvoid recur(int n) {\n    if (n == 1) return;\n    recur(n - 1);\n}\n
        int Function() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n/* \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) */\nvoid Loop(int n) {\n    for (int i = 0; i < n; i++) {\n        Function();\n    }\n}\n/* \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) */\nint Recur(int n) {\n    if (n == 1) return 1;\n    return Recur(n - 1);\n}\n
        func function() int {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0\n}\n\n/* \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) */\nfunc loop(n int) {\n    for i := 0; i < n; i++ {\n        function()\n    }\n}\n\n/* \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) */\nfunc recur(n int) {\n    if n == 1 {\n        return\n    }\n    recur(n - 1)\n}\n
        @discardableResult\nfunc function() -> Int {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0\n}\n\n/* \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) */\nfunc loop(n: Int) {\n    for _ in 0 ..< n {\n        function()\n    }\n}\n\n/* \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) */\nfunc recur(n: Int) {\n    if n == 1 {\n        return\n    }\n    recur(n: n - 1)\n}\n
        function constFunc() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n/* \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) */\nfunction loop(n) {\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n/* \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) */\nfunction recur(n) {\n    if (n === 1) return;\n    return recur(n - 1);\n}\n
        function constFunc(): number {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n/* \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) */\nfunction loop(n: number): void {\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n/* \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) */\nfunction recur(n: number): void {\n    if (n === 1) return;\n    return recur(n - 1);\n}\n
        int function() {\n  // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n  return 0;\n}\n/* \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) */\nvoid loop(int n) {\n  for (int i = 0; i < n; i++) {\n    function();\n  }\n}\n/* \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) */\nvoid recur(int n) {\n  if (n == 1) return;\n  recur(n - 1);\n}\n
        fn function() -> i32 {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n/* \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) */\nfn loop(n: i32) {\n    for i in 0..n {\n        function();\n    }\n}\n/* \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) */\nfn recur(n: i32) {\n    if n == 1 {\n        return;\n    }\n    recur(n - 1);\n}\n
        int func() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n/* \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) */\nvoid loop(int n) {\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n/* \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) */\nvoid recur(int n) {\n    if (n == 1) return;\n    recur(n - 1);\n}\n
        fun function(): Int {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0\n}\n/* \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) */\nfun loop(n: Int) {\n    for (i in 0..<n) {\n        function()\n    }\n}\n/* \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) */\nfun recur(n: Int) {\n    if (n == 1) return\n    return recur(n - 1)\n}\n
        def function\n    # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    0\nend\n\n### \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) ###\ndef loop(n)\n    (0...n).each { function }\nend\n\n### \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) ###\ndef recur(n)\n    return if n == 1\n    recur(n - 1)\nend\n

        \u51fd\u6570 loop() \u548c recur() \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n)\\) \uff0c\u4f46\u7a7a\u95f4\u590d\u6742\u5ea6\u4e0d\u540c\u3002

        • \u51fd\u6570 loop() \u5728\u5faa\u73af\u4e2d\u8c03\u7528\u4e86 \\(n\\) \u6b21 function() \uff0c\u6bcf\u8f6e\u4e2d\u7684 function() \u90fd\u8fd4\u56de\u5e76\u91ca\u653e\u4e86\u6808\u5e27\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \u3002
        • \u9012\u5f52\u51fd\u6570 recur() \u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u4f1a\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684 recur() \uff0c\u4ece\u800c\u5360\u7528 \\(O(n)\\) \u7684\u6808\u5e27\u7a7a\u95f4\u3002
        "},{"location":"chapter_computational_complexity/space_complexity/#243","title":"2.4.3 \u00a0 \u5e38\u89c1\u7c7b\u578b","text":"

        \u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u56fe 2-16 \u5c55\u793a\u4e86\u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\uff08\u4ece\u4f4e\u5230\u9ad8\u6392\u5217\uff09\u3002

        \\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n^2) < O(2^n) \\newline \\text{\u5e38\u6570\u9636} < \\text{\u5bf9\u6570\u9636} < \\text{\u7ebf\u6027\u9636} < \\text{\u5e73\u65b9\u9636} < \\text{\u6307\u6570\u9636} \\end{aligned} \\]

        \u56fe 2-16 \u00a0 \u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b

        "},{"location":"chapter_computational_complexity/space_complexity/#1-o1","title":"1. \u00a0 \u5e38\u6570\u9636 \\(O(1)\\)","text":"

        \u5e38\u6570\u9636\u5e38\u89c1\u4e8e\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\u7684\u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u3002

        \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u5faa\u73af\u4e2d\u521d\u59cb\u5316\u53d8\u91cf\u6216\u8c03\u7528\u51fd\u6570\u800c\u5360\u7528\u7684\u5185\u5b58\uff0c\u5728\u8fdb\u5165\u4e0b\u4e00\u5faa\u73af\u540e\u5c31\u4f1a\u88ab\u91ca\u653e\uff0c\u56e0\u6b64\u4e0d\u4f1a\u7d2f\u79ef\u5360\u7528\u7a7a\u95f4\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def function() -> int:\n    \"\"\"\u51fd\u6570\"\"\"\n    # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0\n\ndef constant(n: int):\n    \"\"\"\u5e38\u6570\u9636\"\"\"\n    # \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n    a = 0\n    nums = [0] * 10000\n    node = ListNode(0)\n    # \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n    for _ in range(n):\n        c = 0\n    # \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n    for _ in range(n):\n        function()\n
        space_complexity.cpp
        /* \u51fd\u6570 */\nint func() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n    // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n    const int a = 0;\n    int b = 0;\n    vector<int> nums(10000);\n    ListNode node(0);\n    // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n
        space_complexity.java
        /* \u51fd\u6570 */\nint function() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n    // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n    final int a = 0;\n    int b = 0;\n    int[] nums = new int[10000];\n    ListNode node = new ListNode(0);\n    // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n    for (int i = 0; i < n; i++) {\n        function();\n    }\n}\n
        space_complexity.cs
        /* \u51fd\u6570 */\nint Function() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid Constant(int n) {\n    // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n    int a = 0;\n    int b = 0;\n    int[] nums = new int[10000];\n    ListNode node = new(0);\n    // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n    for (int i = 0; i < n; i++) {\n        Function();\n    }\n}\n
        space_complexity.go
        /* \u51fd\u6570 */\nfunc function() int {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n    return 0\n}\n\n/* \u5e38\u6570\u9636 */\nfunc spaceConstant(n int) {\n    // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n    const a = 0\n    b := 0\n    nums := make([]int, 10000)\n    node := newNode(0)\n    // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n    var c int\n    for i := 0; i < n; i++ {\n        c = 0\n    }\n    // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n    for i := 0; i < n; i++ {\n        function()\n    }\n    b += 0\n    c += 0\n    nums[0] = 0\n    node.val = 0\n}\n
        space_complexity.swift
        /* \u51fd\u6570 */\n@discardableResult\nfunc function() -> Int {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0\n}\n\n/* \u5e38\u6570\u9636 */\nfunc constant(n: Int) {\n    // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n    let a = 0\n    var b = 0\n    let nums = Array(repeating: 0, count: 10000)\n    let node = ListNode(x: 0)\n    // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n    for _ in 0 ..< n {\n        let c = 0\n    }\n    // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n    for _ in 0 ..< n {\n        function()\n    }\n}\n
        space_complexity.js
        /* \u51fd\u6570 */\nfunction constFunc() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nfunction constant(n) {\n    // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n    const a = 0;\n    const b = 0;\n    const nums = new Array(10000);\n    const node = new ListNode(0);\n    // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n    for (let i = 0; i < n; i++) {\n        const c = 0;\n    }\n    // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n
        space_complexity.ts
        /* \u51fd\u6570 */\nfunction constFunc(): number {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nfunction constant(n: number): void {\n    // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n    const a = 0;\n    const b = 0;\n    const nums = new Array(10000);\n    const node = new ListNode(0);\n    // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n    for (let i = 0; i < n; i++) {\n        const c = 0;\n    }\n    // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n
        space_complexity.dart
        /* \u51fd\u6570 */\nint function() {\n  // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n  return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n  // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n  final int a = 0;\n  int b = 0;\n  List<int> nums = List.filled(10000, 0);\n  ListNode node = ListNode(0);\n  // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n  for (var i = 0; i < n; i++) {\n    int c = 0;\n  }\n  // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n  for (var i = 0; i < n; i++) {\n    function();\n  }\n}\n
        space_complexity.rs
        /* \u51fd\u6570 */\nfn function() -> i32 {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n\n/* \u5e38\u6570\u9636 */\n#[allow(unused)]\nfn constant(n: i32) {\n    // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n    const A: i32 = 0;\n    let b = 0;\n    let nums = vec![0; 10000];\n    let node = ListNode::new(0);\n    // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n    for i in 0..n {\n        let c = 0;\n    }\n    // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n    for i in 0..n {\n        function();\n    }\n}\n
        space_complexity.c
        /* \u51fd\u6570 */\nint func() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n    // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n    const int a = 0;\n    int b = 0;\n    int nums[1000];\n    ListNode *node = newListNode(0);\n    free(node);\n    // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n
        space_complexity.kt
        /* \u51fd\u6570 */\nfun function(): Int {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0\n}\n\n/* \u5e38\u6570\u9636 */\nfun constant(n: Int) {\n    // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n    val a = 0\n    var b = 0\n    val nums = Array(10000) { 0 }\n    val node = ListNode(0)\n    // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n    for (i in 0..<n) {\n        val c = 0\n    }\n    // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n    for (i in 0..<n) {\n        function()\n    }\n}\n
        space_complexity.rb
        ### \u51fd\u6570 ###\ndef function\n  # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n  0\nend\n\n### \u5e38\u6570\u9636 ###\ndef constant(n)\n  # \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n  a = 0\n  nums = [0] * 10000\n  node = ListNode.new\n\n  # \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n  (0...n).each { c = 0 }\n  # \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n  (0...n).each { function }\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_computational_complexity/space_complexity/#2-on","title":"2. \u00a0 \u7ebf\u6027\u9636 \\(O(n)\\)","text":"

        \u7ebf\u6027\u9636\u5e38\u89c1\u4e8e\u5143\u7d20\u6570\u91cf\u4e0e \\(n\\) \u6210\u6b63\u6bd4\u7684\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u7b49\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def linear(n: int):\n    \"\"\"\u7ebf\u6027\u9636\"\"\"\n    # \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    nums = [0] * n\n    # \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    hmap = dict[int, str]()\n    for i in range(n):\n        hmap[i] = str(i)\n
        space_complexity.cpp
        /* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n    // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n    vector<int> nums(n);\n    // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    vector<ListNode> nodes;\n    for (int i = 0; i < n; i++) {\n        nodes.push_back(ListNode(i));\n    }\n    // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    unordered_map<int, string> map;\n    for (int i = 0; i < n; i++) {\n        map[i] = to_string(i);\n    }\n}\n
        space_complexity.java
        /* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n    // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n    int[] nums = new int[n];\n    // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    List<ListNode> nodes = new ArrayList<>();\n    for (int i = 0; i < n; i++) {\n        nodes.add(new ListNode(i));\n    }\n    // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    Map<Integer, String> map = new HashMap<>();\n    for (int i = 0; i < n; i++) {\n        map.put(i, String.valueOf(i));\n    }\n}\n
        space_complexity.cs
        /* \u7ebf\u6027\u9636 */\nvoid Linear(int n) {\n    // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n    int[] nums = new int[n];\n    // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    List<ListNode> nodes = [];\n    for (int i = 0; i < n; i++) {\n        nodes.Add(new ListNode(i));\n    }\n    // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    Dictionary<int, string> map = [];\n    for (int i = 0; i < n; i++) {\n        map.Add(i, i.ToString());\n    }\n}\n
        space_complexity.go
        /* \u7ebf\u6027\u9636 */\nfunc spaceLinear(n int) {\n    // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n    _ = make([]int, n)\n    // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    var nodes []*node\n    for i := 0; i < n; i++ {\n        nodes = append(nodes, newNode(i))\n    }\n    // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    m := make(map[int]string, n)\n    for i := 0; i < n; i++ {\n        m[i] = strconv.Itoa(i)\n    }\n}\n
        space_complexity.swift
        /* \u7ebf\u6027\u9636 */\nfunc linear(n: Int) {\n    // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n    let nums = Array(repeating: 0, count: n)\n    // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    let nodes = (0 ..< n).map { ListNode(x: $0) }\n    // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    let map = Dictionary(uniqueKeysWithValues: (0 ..< n).map { ($0, \"\\($0)\") })\n}\n
        space_complexity.js
        /* \u7ebf\u6027\u9636 */\nfunction linear(n) {\n    // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n    const nums = new Array(n);\n    // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    const nodes = [];\n    for (let i = 0; i < n; i++) {\n        nodes.push(new ListNode(i));\n    }\n    // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    const map = new Map();\n    for (let i = 0; i < n; i++) {\n        map.set(i, i.toString());\n    }\n}\n
        space_complexity.ts
        /* \u7ebf\u6027\u9636 */\nfunction linear(n: number): void {\n    // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n    const nums = new Array(n);\n    // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    const nodes: ListNode[] = [];\n    for (let i = 0; i < n; i++) {\n        nodes.push(new ListNode(i));\n    }\n    // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    const map = new Map();\n    for (let i = 0; i < n; i++) {\n        map.set(i, i.toString());\n    }\n}\n
        space_complexity.dart
        /* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n  // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n  List<int> nums = List.filled(n, 0);\n  // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n  List<ListNode> nodes = [];\n  for (var i = 0; i < n; i++) {\n    nodes.add(ListNode(i));\n  }\n  // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n  Map<int, String> map = HashMap();\n  for (var i = 0; i < n; i++) {\n    map.putIfAbsent(i, () => i.toString());\n  }\n}\n
        space_complexity.rs
        /* \u7ebf\u6027\u9636 */\n#[allow(unused)]\nfn linear(n: i32) {\n    // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n    let mut nums = vec![0; n as usize];\n    // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    let mut nodes = Vec::new();\n    for i in 0..n {\n        nodes.push(ListNode::new(i))\n    }\n    // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    let mut map = HashMap::new();\n    for i in 0..n {\n        map.insert(i, i.to_string());\n    }\n}\n
        space_complexity.c
        /* \u54c8\u5e0c\u8868 */\ntypedef struct {\n    int key;\n    int val;\n    UT_hash_handle hh; // \u57fa\u4e8e uthash.h \u5b9e\u73b0\n} HashTable;\n\n/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n    // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n    int *nums = malloc(sizeof(int) * n);\n    free(nums);\n\n    // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    ListNode **nodes = malloc(sizeof(ListNode *) * n);\n    for (int i = 0; i < n; i++) {\n        nodes[i] = newListNode(i);\n    }\n    // \u5185\u5b58\u91ca\u653e\n    for (int i = 0; i < n; i++) {\n        free(nodes[i]);\n    }\n    free(nodes);\n\n    // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    HashTable *h = NULL;\n    for (int i = 0; i < n; i++) {\n        HashTable *tmp = malloc(sizeof(HashTable));\n        tmp->key = i;\n        tmp->val = i;\n        HASH_ADD_INT(h, key, tmp);\n    }\n\n    // \u5185\u5b58\u91ca\u653e\n    HashTable *curr, *tmp;\n    HASH_ITER(hh, h, curr, tmp) {\n        HASH_DEL(h, curr);\n        free(curr);\n    }\n}\n
        space_complexity.kt
        /* \u7ebf\u6027\u9636 */\nfun linear(n: Int) {\n    // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n    val nums = Array(n) { 0 }\n    // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    val nodes = mutableListOf<ListNode>()\n    for (i in 0..<n) {\n        nodes.add(ListNode(i))\n    }\n    // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    val map = mutableMapOf<Int, String>()\n    for (i in 0..<n) {\n        map[i] = i.toString()\n    }\n}\n
        space_complexity.rb
        ### \u7ebf\u6027\u9636 ###\ndef linear(n)\n  # \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n  nums = Array.new(n, 0)\n\n  # \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n  hmap = {}\n  for i in 0...n\n    hmap[i] = i.to_s\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5982\u56fe 2-17 \u6240\u793a\uff0c\u6b64\u51fd\u6570\u7684\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u5373\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684 linear_recur() \u51fd\u6570\uff0c\u4f7f\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def linear_recur(n: int):\n    \"\"\"\u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n    print(\"\u9012\u5f52 n =\", n)\n    if n == 1:\n        return\n    linear_recur(n - 1)\n
        space_complexity.cpp
        /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n    cout << \"\u9012\u5f52 n = \" << n << endl;\n    if (n == 1)\n        return;\n    linearRecur(n - 1);\n}\n
        space_complexity.java
        /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n    System.out.println(\"\u9012\u5f52 n = \" + n);\n    if (n == 1)\n        return;\n    linearRecur(n - 1);\n}\n
        space_complexity.cs
        /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid LinearRecur(int n) {\n    Console.WriteLine(\"\u9012\u5f52 n = \" + n);\n    if (n == 1) return;\n    LinearRecur(n - 1);\n}\n
        space_complexity.go
        /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc spaceLinearRecur(n int) {\n    fmt.Println(\"\u9012\u5f52 n =\", n)\n    if n == 1 {\n        return\n    }\n    spaceLinearRecur(n - 1)\n}\n
        space_complexity.swift
        /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc linearRecur(n: Int) {\n    print(\"\u9012\u5f52 n = \\(n)\")\n    if n == 1 {\n        return\n    }\n    linearRecur(n: n - 1)\n}\n
        space_complexity.js
        /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction linearRecur(n) {\n    console.log(`\u9012\u5f52 n = ${n}`);\n    if (n === 1) return;\n    linearRecur(n - 1);\n}\n
        space_complexity.ts
        /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction linearRecur(n: number): void {\n    console.log(`\u9012\u5f52 n = ${n}`);\n    if (n === 1) return;\n    linearRecur(n - 1);\n}\n
        space_complexity.dart
        /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n  print('\u9012\u5f52 n = $n');\n  if (n == 1) return;\n  linearRecur(n - 1);\n}\n
        space_complexity.rs
        /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn linear_recur(n: i32) {\n    println!(\"\u9012\u5f52 n = {}\", n);\n    if n == 1 {\n        return;\n    };\n    linear_recur(n - 1);\n}\n
        space_complexity.c
        /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n    printf(\"\u9012\u5f52 n = %d\\r\\n\", n);\n    if (n == 1)\n        return;\n    linearRecur(n - 1);\n}\n
        space_complexity.kt
        /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfun linearRecur(n: Int) {\n    println(\"\u9012\u5f52 n = $n\")\n    if (n == 1)\n        return\n    linearRecur(n - 1)\n}\n
        space_complexity.rb
        ### \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09###\ndef linear_recur(n)\n  puts \"\u9012\u5f52 n = #{n}\"\n  return if n == 1\n  linear_recur(n - 1)\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 2-17 \u00a0 \u9012\u5f52\u51fd\u6570\u4ea7\u751f\u7684\u7ebf\u6027\u9636\u7a7a\u95f4\u590d\u6742\u5ea6

        "},{"location":"chapter_computational_complexity/space_complexity/#3-on2","title":"3. \u00a0 \u5e73\u65b9\u9636 \\(O(n^2)\\)","text":"

        \u5e73\u65b9\u9636\u5e38\u89c1\u4e8e\u77e9\u9635\u548c\u56fe\uff0c\u5143\u7d20\u6570\u91cf\u4e0e \\(n\\) \u6210\u5e73\u65b9\u5173\u7cfb\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def quadratic(n: int):\n    \"\"\"\u5e73\u65b9\u9636\"\"\"\n    # \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n    num_matrix = [[0] * n for _ in range(n)]\n
        space_complexity.cpp
        /* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n    // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n    vector<vector<int>> numMatrix;\n    for (int i = 0; i < n; i++) {\n        vector<int> tmp;\n        for (int j = 0; j < n; j++) {\n            tmp.push_back(0);\n        }\n        numMatrix.push_back(tmp);\n    }\n}\n
        space_complexity.java
        /* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n    // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n    int[][] numMatrix = new int[n][n];\n    // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n    List<List<Integer>> numList = new ArrayList<>();\n    for (int i = 0; i < n; i++) {\n        List<Integer> tmp = new ArrayList<>();\n        for (int j = 0; j < n; j++) {\n            tmp.add(0);\n        }\n        numList.add(tmp);\n    }\n}\n
        space_complexity.cs
        /* \u5e73\u65b9\u9636 */\nvoid Quadratic(int n) {\n    // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n    int[,] numMatrix = new int[n, n];\n    // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n    List<List<int>> numList = [];\n    for (int i = 0; i < n; i++) {\n        List<int> tmp = [];\n        for (int j = 0; j < n; j++) {\n            tmp.Add(0);\n        }\n        numList.Add(tmp);\n    }\n}\n
        space_complexity.go
        /* \u5e73\u65b9\u9636 */\nfunc spaceQuadratic(n int) {\n    // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n    numMatrix := make([][]int, n)\n    for i := 0; i < n; i++ {\n        numMatrix[i] = make([]int, n)\n    }\n}\n
        space_complexity.swift
        /* \u5e73\u65b9\u9636 */\nfunc quadratic(n: Int) {\n    // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n    let numList = Array(repeating: Array(repeating: 0, count: n), count: n)\n}\n
        space_complexity.js
        /* \u5e73\u65b9\u9636 */\nfunction quadratic(n) {\n    // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n    const numMatrix = Array(n)\n        .fill(null)\n        .map(() => Array(n).fill(null));\n    // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n    const numList = [];\n    for (let i = 0; i < n; i++) {\n        const tmp = [];\n        for (let j = 0; j < n; j++) {\n            tmp.push(0);\n        }\n        numList.push(tmp);\n    }\n}\n
        space_complexity.ts
        /* \u5e73\u65b9\u9636 */\nfunction quadratic(n: number): void {\n    // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n    const numMatrix = Array(n)\n        .fill(null)\n        .map(() => Array(n).fill(null));\n    // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n    const numList = [];\n    for (let i = 0; i < n; i++) {\n        const tmp = [];\n        for (let j = 0; j < n; j++) {\n            tmp.push(0);\n        }\n        numList.push(tmp);\n    }\n}\n
        space_complexity.dart
        /* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n  // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n  List<List<int>> numMatrix = List.generate(n, (_) => List.filled(n, 0));\n  // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n  List<List<int>> numList = [];\n  for (var i = 0; i < n; i++) {\n    List<int> tmp = [];\n    for (int j = 0; j < n; j++) {\n      tmp.add(0);\n    }\n    numList.add(tmp);\n  }\n}\n
        space_complexity.rs
        /* \u5e73\u65b9\u9636 */\n#[allow(unused)]\nfn quadratic(n: i32) {\n    // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n    let num_matrix = vec![vec![0; n as usize]; n as usize];\n    // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n    let mut num_list = Vec::new();\n    for i in 0..n {\n        let mut tmp = Vec::new();\n        for j in 0..n {\n            tmp.push(0);\n        }\n        num_list.push(tmp);\n    }\n}\n
        space_complexity.c
        /* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n    // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n    int **numMatrix = malloc(sizeof(int *) * n);\n    for (int i = 0; i < n; i++) {\n        int *tmp = malloc(sizeof(int) * n);\n        for (int j = 0; j < n; j++) {\n            tmp[j] = 0;\n        }\n        numMatrix[i] = tmp;\n    }\n\n    // \u5185\u5b58\u91ca\u653e\n    for (int i = 0; i < n; i++) {\n        free(numMatrix[i]);\n    }\n    free(numMatrix);\n}\n
        space_complexity.kt
        /* \u5e73\u65b9\u9636 */\nfun quadratic(n: Int) {\n    // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n    val numMatrix = arrayOfNulls<Array<Int>?>(n)\n    // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n    val numList = mutableListOf<MutableList<Int>>()\n    for (i in 0..<n) {\n        val tmp = mutableListOf<Int>()\n        for (j in 0..<n) {\n            tmp.add(0)\n        }\n        numList.add(tmp)\n    }\n}\n
        space_complexity.rb
        ### \u5e73\u65b9\u9636 ###\ndef quadratic(n)\n  # \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n  Array.new(n) { Array.new(n, 0) }\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5982\u56fe 2-18 \u6240\u793a\uff0c\u8be5\u51fd\u6570\u7684\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u5728\u6bcf\u4e2a\u9012\u5f52\u51fd\u6570\u4e2d\u90fd\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u6570\u7ec4\uff0c\u957f\u5ea6\u5206\u522b\u4e3a \\(n\\)\u3001\\(n-1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \uff0c\u5e73\u5747\u957f\u5ea6\u4e3a \\(n / 2\\) \uff0c\u56e0\u6b64\u603b\u4f53\u5360\u7528 \\(O(n^2)\\) \u7a7a\u95f4\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def quadratic_recur(n: int) -> int:\n    \"\"\"\u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n    if n <= 0:\n        return 0\n    # \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n    nums = [0] * n\n    return quadratic_recur(n - 1)\n
        space_complexity.cpp
        /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n    if (n <= 0)\n        return 0;\n    vector<int> nums(n);\n    cout << \"\u9012\u5f52 n = \" << n << \" \u4e2d\u7684 nums \u957f\u5ea6 = \" << nums.size() << endl;\n    return quadraticRecur(n - 1);\n}\n
        space_complexity.java
        /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n    if (n <= 0)\n        return 0;\n    // \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n    int[] nums = new int[n];\n    System.out.println(\"\u9012\u5f52 n = \" + n + \" \u4e2d\u7684 nums \u957f\u5ea6 = \" + nums.length);\n    return quadraticRecur(n - 1);\n}\n
        space_complexity.cs
        /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint QuadraticRecur(int n) {\n    if (n <= 0) return 0;\n    int[] nums = new int[n];\n    Console.WriteLine(\"\u9012\u5f52 n = \" + n + \" \u4e2d\u7684 nums \u957f\u5ea6 = \" + nums.Length);\n    return QuadraticRecur(n - 1);\n}\n
        space_complexity.go
        /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc spaceQuadraticRecur(n int) int {\n    if n <= 0 {\n        return 0\n    }\n    nums := make([]int, n)\n    fmt.Printf(\"\u9012\u5f52 n = %d \u4e2d\u7684 nums \u957f\u5ea6 = %d \\n\", n, len(nums))\n    return spaceQuadraticRecur(n - 1)\n}\n
        space_complexity.swift
        /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\n@discardableResult\nfunc quadraticRecur(n: Int) -> Int {\n    if n <= 0 {\n        return 0\n    }\n    // \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n    let nums = Array(repeating: 0, count: n)\n    print(\"\u9012\u5f52 n = \\(n) \u4e2d\u7684 nums \u957f\u5ea6 = \\(nums.count)\")\n    return quadraticRecur(n: n - 1)\n}\n
        space_complexity.js
        /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction quadraticRecur(n) {\n    if (n <= 0) return 0;\n    const nums = new Array(n);\n    console.log(`\u9012\u5f52 n = ${n} \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}`);\n    return quadraticRecur(n - 1);\n}\n
        space_complexity.ts
        /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction quadraticRecur(n: number): number {\n    if (n <= 0) return 0;\n    const nums = new Array(n);\n    console.log(`\u9012\u5f52 n = ${n} \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}`);\n    return quadraticRecur(n - 1);\n}\n
        space_complexity.dart
        /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n  if (n <= 0) return 0;\n  List<int> nums = List.filled(n, 0);\n  print('\u9012\u5f52 n = $n \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}');\n  return quadraticRecur(n - 1);\n}\n
        space_complexity.rs
        /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn quadratic_recur(n: i32) -> i32 {\n    if n <= 0 {\n        return 0;\n    };\n    // \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n    let nums = vec![0; n as usize];\n    println!(\"\u9012\u5f52 n = {} \u4e2d\u7684 nums \u957f\u5ea6 = {}\", n, nums.len());\n    return quadratic_recur(n - 1);\n}\n
        space_complexity.c
        /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n    if (n <= 0)\n        return 0;\n    int *nums = malloc(sizeof(int) * n);\n    printf(\"\u9012\u5f52 n = %d \u4e2d\u7684 nums \u957f\u5ea6 = %d\\r\\n\", n, n);\n    int res = quadraticRecur(n - 1);\n    free(nums);\n    return res;\n}\n
        space_complexity.kt
        /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\ntailrec fun quadraticRecur(n: Int): Int {\n    if (n <= 0)\n        return 0\n    // \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n    val nums = Array(n) { 0 }\n    println(\"\u9012\u5f52 n = $n \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.size}\")\n    return quadraticRecur(n - 1)\n}\n
        space_complexity.rb
        ### \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09###\ndef quadratic_recur(n)\n  return 0 unless n > 0\n\n  # \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n  nums = Array.new(n, 0)\n  quadratic_recur(n - 1)\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 2-18 \u00a0 \u9012\u5f52\u51fd\u6570\u4ea7\u751f\u7684\u5e73\u65b9\u9636\u7a7a\u95f4\u590d\u6742\u5ea6

        "},{"location":"chapter_computational_complexity/space_complexity/#4-o2n","title":"4. \u00a0 \u6307\u6570\u9636 \\(O(2^n)\\)","text":"

        \u6307\u6570\u9636\u5e38\u89c1\u4e8e\u4e8c\u53c9\u6811\u3002\u89c2\u5bdf\u56fe 2-19 \uff0c\u5c42\u6570\u4e3a \\(n\\) \u7684\u201c\u6ee1\u4e8c\u53c9\u6811\u201d\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(2^n - 1\\) \uff0c\u5360\u7528 \\(O(2^n)\\) \u7a7a\u95f4\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def build_tree(n: int) -> TreeNode | None:\n    \"\"\"\u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09\"\"\"\n    if n == 0:\n        return None\n    root = TreeNode(0)\n    root.left = build_tree(n - 1)\n    root.right = build_tree(n - 1)\n    return root\n
        space_complexity.cpp
        /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode *buildTree(int n) {\n    if (n == 0)\n        return nullptr;\n    TreeNode *root = new TreeNode(0);\n    root->left = buildTree(n - 1);\n    root->right = buildTree(n - 1);\n    return root;\n}\n
        space_complexity.java
        /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode buildTree(int n) {\n    if (n == 0)\n        return null;\n    TreeNode root = new TreeNode(0);\n    root.left = buildTree(n - 1);\n    root.right = buildTree(n - 1);\n    return root;\n}\n
        space_complexity.cs
        /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode? BuildTree(int n) {\n    if (n == 0) return null;\n    TreeNode root = new(0) {\n        left = BuildTree(n - 1),\n        right = BuildTree(n - 1)\n    };\n    return root;\n}\n
        space_complexity.go
        /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunc buildTree(n int) *TreeNode {\n    if n == 0 {\n        return nil\n    }\n    root := NewTreeNode(0)\n    root.Left = buildTree(n - 1)\n    root.Right = buildTree(n - 1)\n    return root\n}\n
        space_complexity.swift
        /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunc buildTree(n: Int) -> TreeNode? {\n    if n == 0 {\n        return nil\n    }\n    let root = TreeNode(x: 0)\n    root.left = buildTree(n: n - 1)\n    root.right = buildTree(n: n - 1)\n    return root\n}\n
        space_complexity.js
        /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunction buildTree(n) {\n    if (n === 0) return null;\n    const root = new TreeNode(0);\n    root.left = buildTree(n - 1);\n    root.right = buildTree(n - 1);\n    return root;\n}\n
        space_complexity.ts
        /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunction buildTree(n: number): TreeNode | null {\n    if (n === 0) return null;\n    const root = new TreeNode(0);\n    root.left = buildTree(n - 1);\n    root.right = buildTree(n - 1);\n    return root;\n}\n
        space_complexity.dart
        /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode? buildTree(int n) {\n  if (n == 0) return null;\n  TreeNode root = TreeNode(0);\n  root.left = buildTree(n - 1);\n  root.right = buildTree(n - 1);\n  return root;\n}\n
        space_complexity.rs
        /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfn build_tree(n: i32) -> Option<Rc<RefCell<TreeNode>>> {\n    if n == 0 {\n        return None;\n    };\n    let root = TreeNode::new(0);\n    root.borrow_mut().left = build_tree(n - 1);\n    root.borrow_mut().right = build_tree(n - 1);\n    return Some(root);\n}\n
        space_complexity.c
        /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode *buildTree(int n) {\n    if (n == 0)\n        return NULL;\n    TreeNode *root = newTreeNode(0);\n    root->left = buildTree(n - 1);\n    root->right = buildTree(n - 1);\n    return root;\n}\n
        space_complexity.kt
        /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfun buildTree(n: Int): TreeNode? {\n    if (n == 0)\n        return null\n    val root = TreeNode(0)\n    root.left = buildTree(n - 1)\n    root.right = buildTree(n - 1)\n    return root\n}\n
        space_complexity.rb
        ### \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09###\ndef build_tree(n)\n  return if n == 0\n\n  TreeNode.new.tap do |root|\n    root.left = build_tree(n - 1)\n    root.right = build_tree(n - 1)\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 2-19 \u00a0 \u6ee1\u4e8c\u53c9\u6811\u4ea7\u751f\u7684\u6307\u6570\u9636\u7a7a\u95f4\u590d\u6742\u5ea6

        "},{"location":"chapter_computational_complexity/space_complexity/#5-olog-n","title":"5. \u00a0 \u5bf9\u6570\u9636 \\(O(\\log n)\\)","text":"

        \u5bf9\u6570\u9636\u5e38\u89c1\u4e8e\u5206\u6cbb\u7b97\u6cd5\u3002\u4f8b\u5982\u5f52\u5e76\u6392\u5e8f\uff0c\u8f93\u5165\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\uff0c\u6bcf\u8f6e\u9012\u5f52\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5904\u5212\u5206\u4e3a\u4e24\u534a\uff0c\u5f62\u6210\u9ad8\u5ea6\u4e3a \\(\\log n\\) \u7684\u9012\u5f52\u6811\uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u6808\u5e27\u7a7a\u95f4\u3002

        \u518d\u4f8b\u5982\u5c06\u6570\u5b57\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\uff0c\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570 \\(n\\) \uff0c\u5b83\u7684\u4f4d\u6570\u4e3a \\(\\lfloor \\log_{10} n \\rfloor + 1\\) \uff0c\u5373\u5bf9\u5e94\u5b57\u7b26\u4e32\u957f\u5ea6\u4e3a \\(\\lfloor \\log_{10} n \\rfloor + 1\\) \uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log_{10} n + 1) = O(\\log n)\\) \u3002

        "},{"location":"chapter_computational_complexity/space_complexity/#244","title":"2.4.4 \u00a0 \u6743\u8861\u65f6\u95f4\u4e0e\u7a7a\u95f4","text":"

        \u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u90fd\u80fd\u8fbe\u5230\u6700\u4f18\u3002\u7136\u800c\u5728\u5b9e\u9645\u60c5\u51b5\u4e2d\uff0c\u540c\u65f6\u4f18\u5316\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u975e\u5e38\u56f0\u96be\u3002

        \u964d\u4f4e\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u9700\u8981\u4ee5\u63d0\u5347\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a\u4ee3\u4ef7\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u6211\u4eec\u5c06\u727a\u7272\u5185\u5b58\u7a7a\u95f4\u6765\u63d0\u5347\u7b97\u6cd5\u8fd0\u884c\u901f\u5ea6\u7684\u601d\u8def\u79f0\u4e3a\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\uff1b\u53cd\u4e4b\uff0c\u5219\u79f0\u4e3a\u201c\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4\u201d\u3002

        \u9009\u62e9\u54ea\u79cd\u601d\u8def\u53d6\u51b3\u4e8e\u6211\u4eec\u66f4\u770b\u91cd\u54ea\u4e2a\u65b9\u9762\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u65f6\u95f4\u6bd4\u7a7a\u95f4\u66f4\u5b9d\u8d35\uff0c\u56e0\u6b64\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\u901a\u5e38\u662f\u66f4\u5e38\u7528\u7684\u7b56\u7565\u3002\u5f53\u7136\uff0c\u5728\u6570\u636e\u91cf\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u63a7\u5236\u7a7a\u95f4\u590d\u6742\u5ea6\u4e5f\u975e\u5e38\u91cd\u8981\u3002

        "},{"location":"chapter_computational_complexity/summary/","title":"2.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_computational_complexity/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"

        \u7b97\u6cd5\u6548\u7387\u8bc4\u4f30

        • \u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u662f\u8861\u91cf\u7b97\u6cd5\u4f18\u52a3\u7684\u4e24\u4e2a\u4e3b\u8981\u8bc4\u4ef7\u6307\u6807\u3002
        • \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b9e\u9645\u6d4b\u8bd5\u6765\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\uff0c\u4f46\u96be\u4ee5\u6d88\u9664\u6d4b\u8bd5\u73af\u5883\u7684\u5f71\u54cd\uff0c\u4e14\u4f1a\u8017\u8d39\u5927\u91cf\u8ba1\u7b97\u8d44\u6e90\u3002
        • \u590d\u6742\u5ea6\u5206\u6790\u53ef\u4ee5\u6d88\u9664\u5b9e\u9645\u6d4b\u8bd5\u7684\u5f0a\u7aef\uff0c\u5206\u6790\u7ed3\u679c\u9002\u7528\u4e8e\u6240\u6709\u8fd0\u884c\u5e73\u53f0\uff0c\u5e76\u4e14\u80fd\u591f\u63ed\u793a\u7b97\u6cd5\u5728\u4e0d\u540c\u6570\u636e\u89c4\u6a21\u4e0b\u7684\u6548\u7387\u3002

        \u65f6\u95f4\u590d\u6742\u5ea6

        • \u65f6\u95f4\u590d\u6742\u5ea6\u7528\u4e8e\u8861\u91cf\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u968f\u6570\u636e\u91cf\u589e\u957f\u7684\u8d8b\u52bf\uff0c\u53ef\u4ee5\u6709\u6548\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u5931\u6548\uff0c\u5982\u5728\u8f93\u5165\u7684\u6570\u636e\u91cf\u8f83\u5c0f\u6216\u65f6\u95f4\u590d\u6742\u5ea6\u76f8\u540c\u65f6\uff0c\u65e0\u6cd5\u7cbe\u786e\u5bf9\u6bd4\u7b97\u6cd5\u6548\u7387\u7684\u4f18\u52a3\u3002
        • \u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4f7f\u7528\u5927 \\(O\\) \u7b26\u53f7\u8868\u793a\uff0c\u5bf9\u5e94\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c\uff0c\u53cd\u6620\u5f53 \\(n\\) \u8d8b\u5411\u6b63\u65e0\u7a77\u65f6\uff0c\u64cd\u4f5c\u6570\u91cf \\(T(n)\\) \u7684\u589e\u957f\u7ea7\u522b\u3002
        • \u63a8\u7b97\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u4e3a\u4e24\u6b65\uff0c\u9996\u5148\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf\uff0c\u7136\u540e\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c\u3002
        • \u5e38\u89c1\u65f6\u95f4\u590d\u6742\u5ea6\u4ece\u4f4e\u5230\u9ad8\u6392\u5217\u6709 \\(O(1)\\)\u3001\\(O(\\log n)\\)\u3001\\(O(n)\\)\u3001\\(O(n \\log n)\\)\u3001\\(O(n^2)\\)\u3001\\(O(2^n)\\) \u548c \\(O(n!)\\) \u7b49\u3002
        • \u67d0\u4e9b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u975e\u56fa\u5b9a\uff0c\u800c\u662f\u4e0e\u8f93\u5165\u6570\u636e\u7684\u5206\u5e03\u6709\u5173\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u4e3a\u6700\u5dee\u3001\u6700\u4f73\u3001\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u51e0\u4e4e\u4e0d\u7528\uff0c\u56e0\u4e3a\u8f93\u5165\u6570\u636e\u4e00\u822c\u9700\u8981\u6ee1\u8db3\u4e25\u683c\u6761\u4ef6\u624d\u80fd\u8fbe\u5230\u6700\u4f73\u60c5\u51b5\u3002
        • \u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u53cd\u6620\u7b97\u6cd5\u5728\u968f\u673a\u6570\u636e\u8f93\u5165\u4e0b\u7684\u8fd0\u884c\u6548\u7387\uff0c\u6700\u63a5\u8fd1\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u7b97\u6cd5\u6027\u80fd\u3002\u8ba1\u7b97\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u9700\u8981\u7edf\u8ba1\u8f93\u5165\u6570\u636e\u5206\u5e03\u4ee5\u53ca\u7efc\u5408\u540e\u7684\u6570\u5b66\u671f\u671b\u3002

        \u7a7a\u95f4\u590d\u6742\u5ea6

        • \u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u4f5c\u7528\u7c7b\u4f3c\u4e8e\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u7528\u4e8e\u8861\u91cf\u7b97\u6cd5\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u968f\u6570\u636e\u91cf\u589e\u957f\u7684\u8d8b\u52bf\u3002
        • \u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u76f8\u5173\u5185\u5b58\u7a7a\u95f4\u53ef\u5206\u4e3a\u8f93\u5165\u7a7a\u95f4\u3001\u6682\u5b58\u7a7a\u95f4\u3001\u8f93\u51fa\u7a7a\u95f4\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u8f93\u5165\u7a7a\u95f4\u4e0d\u7eb3\u5165\u7a7a\u95f4\u590d\u6742\u5ea6\u8ba1\u7b97\u3002\u6682\u5b58\u7a7a\u95f4\u53ef\u5206\u4e3a\u6682\u5b58\u6570\u636e\u3001\u6808\u5e27\u7a7a\u95f4\u548c\u6307\u4ee4\u7a7a\u95f4\uff0c\u5176\u4e2d\u6808\u5e27\u7a7a\u95f4\u901a\u5e38\u4ec5\u5728\u9012\u5f52\u51fd\u6570\u4e2d\u5f71\u54cd\u7a7a\u95f4\u590d\u6742\u5ea6\u3002
        • \u6211\u4eec\u901a\u5e38\u53ea\u5173\u6ce8\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u5373\u7edf\u8ba1\u7b97\u6cd5\u5728\u6700\u5dee\u8f93\u5165\u6570\u636e\u548c\u6700\u5dee\u8fd0\u884c\u65f6\u523b\u4e0b\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u3002
        • \u5e38\u89c1\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece\u4f4e\u5230\u9ad8\u6392\u5217\u6709 \\(O(1)\\)\u3001\\(O(\\log n)\\)\u3001\\(O(n)\\)\u3001\\(O(n^2)\\) \u548c \\(O(2^n)\\) \u7b49\u3002
        "},{"location":"chapter_computational_complexity/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u5c3e\u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u5417\uff1f

        \u7406\u8bba\u4e0a\uff0c\u5c3e\u9012\u5f52\u51fd\u6570\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f18\u5316\u81f3 \\(O(1)\\) \u3002\u4e0d\u8fc7\u7edd\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\uff08\u4f8b\u5982 Java\u3001Python\u3001C++\u3001Go\u3001C# \u7b49\uff09\u4e0d\u652f\u6301\u81ea\u52a8\u4f18\u5316\u5c3e\u9012\u5f52\uff0c\u56e0\u6b64\u901a\u5e38\u8ba4\u4e3a\u7a7a\u95f4\u590d\u6742\u5ea6\u662f \\(O(n)\\) \u3002

        Q\uff1a\u51fd\u6570\u548c\u65b9\u6cd5\u8fd9\u4e24\u4e2a\u672f\u8bed\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f

        \u51fd\u6570\uff08function\uff09\u53ef\u4ee5\u88ab\u72ec\u7acb\u6267\u884c\uff0c\u6240\u6709\u53c2\u6570\u90fd\u4ee5\u663e\u5f0f\u4f20\u9012\u3002\u65b9\u6cd5\uff08method\uff09\u4e0e\u4e00\u4e2a\u5bf9\u8c61\u5173\u8054\uff0c\u88ab\u9690\u5f0f\u4f20\u9012\u7ed9\u8c03\u7528\u5b83\u7684\u5bf9\u8c61\uff0c\u80fd\u591f\u5bf9\u7c7b\u7684\u5b9e\u4f8b\u4e2d\u5305\u542b\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002

        \u4e0b\u9762\u4ee5\u51e0\u79cd\u5e38\u89c1\u7684\u7f16\u7a0b\u8bed\u8a00\u4e3a\u4f8b\u6765\u8bf4\u660e\u3002

        • C \u8bed\u8a00\u662f\u8fc7\u7a0b\u5f0f\u7f16\u7a0b\u8bed\u8a00\uff0c\u6ca1\u6709\u9762\u5411\u5bf9\u8c61\u7684\u6982\u5ff5\uff0c\u6240\u4ee5\u53ea\u6709\u51fd\u6570\u3002\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u7ed3\u6784\u4f53\uff08struct\uff09\u6765\u6a21\u62df\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff0c\u4e0e\u7ed3\u6784\u4f53\u76f8\u5173\u8054\u7684\u51fd\u6570\u5c31\u76f8\u5f53\u4e8e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u65b9\u6cd5\u3002
        • Java \u548c C# \u662f\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u4ee3\u7801\u5757\uff08\u65b9\u6cd5\uff09\u901a\u5e38\u4f5c\u4e3a\u67d0\u4e2a\u7c7b\u7684\u4e00\u90e8\u5206\u3002\u9759\u6001\u65b9\u6cd5\u7684\u884c\u4e3a\u7c7b\u4f3c\u4e8e\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u88ab\u7ed1\u5b9a\u5728\u7c7b\u4e0a\uff0c\u4e0d\u80fd\u8bbf\u95ee\u7279\u5b9a\u7684\u5b9e\u4f8b\u53d8\u91cf\u3002
        • C++ \u548c Python \u65e2\u652f\u6301\u8fc7\u7a0b\u5f0f\u7f16\u7a0b\uff08\u51fd\u6570\uff09\uff0c\u4e5f\u652f\u6301\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08\u65b9\u6cd5\uff09\u3002

        Q\uff1a\u56fe\u89e3\u201c\u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\u201d\u53cd\u6620\u7684\u662f\u5426\u662f\u5360\u7528\u7a7a\u95f4\u7684\u7edd\u5bf9\u5927\u5c0f\uff1f

        \u4e0d\u662f\uff0c\u8be5\u56fe\u5c55\u793a\u7684\u662f\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u5176\u53cd\u6620\u7684\u662f\u589e\u957f\u8d8b\u52bf\uff0c\u800c\u4e0d\u662f\u5360\u7528\u7a7a\u95f4\u7684\u7edd\u5bf9\u5927\u5c0f\u3002

        \u5047\u8bbe\u53d6 \\(n = 8\\) \uff0c\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\u6bcf\u6761\u66f2\u7ebf\u7684\u503c\u4e0e\u51fd\u6570\u5bf9\u5e94\u4e0d\u4e0a\u3002\u8fd9\u662f\u56e0\u4e3a\u6bcf\u6761\u66f2\u7ebf\u90fd\u5305\u542b\u4e00\u4e2a\u5e38\u6570\u9879\uff0c\u7528\u4e8e\u5c06\u53d6\u503c\u8303\u56f4\u538b\u7f29\u5230\u4e00\u4e2a\u89c6\u89c9\u8212\u9002\u7684\u8303\u56f4\u5185\u3002

        \u5728\u5b9e\u9645\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u901a\u5e38\u4e0d\u77e5\u9053\u6bcf\u4e2a\u65b9\u6cd5\u7684\u201c\u5e38\u6570\u9879\u201d\u590d\u6742\u5ea6\u662f\u591a\u5c11\uff0c\u6240\u4ee5\u4e00\u822c\u65e0\u6cd5\u4ec5\u51ed\u590d\u6742\u5ea6\u6765\u9009\u62e9 \\(n = 8\\) \u4e4b\u4e0b\u7684\u6700\u4f18\u89e3\u6cd5\u3002\u4f46\u5bf9\u4e8e \\(n = 8^5\\) \u5c31\u5f88\u597d\u9009\u4e86\uff0c\u8fd9\u65f6\u589e\u957f\u8d8b\u52bf\u5df2\u7ecf\u5360\u4e3b\u5bfc\u4e86\u3002

        Q \u662f\u5426\u5b58\u5728\u6839\u636e\u5b9e\u9645\u4f7f\u7528\u573a\u666f\uff0c\u9009\u62e9\u727a\u7272\u65f6\u95f4\uff08\u6216\u7a7a\u95f4\uff09\u6765\u8bbe\u8ba1\u7b97\u6cd5\u7684\u60c5\u51b5\uff1f

        \u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5927\u90e8\u5206\u60c5\u51b5\u4f1a\u9009\u62e9\u727a\u7272\u7a7a\u95f4\u6362\u65f6\u95f4\u3002\u4f8b\u5982\u6570\u636e\u5e93\u7d22\u5f15\uff0c\u6211\u4eec\u901a\u5e38\u9009\u62e9\u5efa\u7acb B+ \u6811\u6216\u54c8\u5e0c\u7d22\u5f15\uff0c\u5360\u7528\u5927\u91cf\u5185\u5b58\u7a7a\u95f4\uff0c\u4ee5\u6362\u53d6 \\(O(\\log n)\\) \u751a\u81f3 \\(O(1)\\) \u7684\u9ad8\u6548\u67e5\u8be2\u3002

        \u5728\u7a7a\u95f4\u8d44\u6e90\u5b9d\u8d35\u7684\u573a\u666f\uff0c\u4e5f\u4f1a\u9009\u62e9\u727a\u7272\u65f6\u95f4\u6362\u7a7a\u95f4\u3002\u4f8b\u5982\u5728\u5d4c\u5165\u5f0f\u5f00\u53d1\u4e2d\uff0c\u8bbe\u5907\u5185\u5b58\u5f88\u5b9d\u8d35\uff0c\u5de5\u7a0b\u5e08\u53ef\u80fd\u4f1a\u653e\u5f03\u4f7f\u7528\u54c8\u5e0c\u8868\uff0c\u9009\u62e9\u4f7f\u7528\u6570\u7ec4\u987a\u5e8f\u67e5\u627e\uff0c\u4ee5\u8282\u7701\u5185\u5b58\u5360\u7528\uff0c\u4ee3\u4ef7\u662f\u67e5\u627e\u53d8\u6162\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/","title":"2.3 \u00a0 \u65f6\u95f4\u590d\u6742\u5ea6","text":"

        \u8fd0\u884c\u65f6\u95f4\u53ef\u4ee5\u76f4\u89c2\u4e14\u51c6\u786e\u5730\u53cd\u6620\u7b97\u6cd5\u7684\u6548\u7387\u3002\u5982\u679c\u6211\u4eec\u60f3\u51c6\u786e\u9884\u4f30\u4e00\u6bb5\u4ee3\u7801\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u5e94\u8be5\u5982\u4f55\u64cd\u4f5c\u5462\uff1f

        1. \u786e\u5b9a\u8fd0\u884c\u5e73\u53f0\uff0c\u5305\u62ec\u786c\u4ef6\u914d\u7f6e\u3001\u7f16\u7a0b\u8bed\u8a00\u3001\u7cfb\u7edf\u73af\u5883\u7b49\uff0c\u8fd9\u4e9b\u56e0\u7d20\u90fd\u4f1a\u5f71\u54cd\u4ee3\u7801\u7684\u8fd0\u884c\u6548\u7387\u3002
        2. \u8bc4\u4f30\u5404\u79cd\u8ba1\u7b97\u64cd\u4f5c\u6240\u9700\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u4f8b\u5982\u52a0\u6cd5\u64cd\u4f5c + \u9700\u8981 1 ns \uff0c\u4e58\u6cd5\u64cd\u4f5c * \u9700\u8981 10 ns \uff0c\u6253\u5370\u64cd\u4f5c print() \u9700\u8981 5 ns \u7b49\u3002
        3. \u7edf\u8ba1\u4ee3\u7801\u4e2d\u6240\u6709\u7684\u8ba1\u7b97\u64cd\u4f5c\uff0c\u5e76\u5c06\u6240\u6709\u64cd\u4f5c\u7684\u6267\u884c\u65f6\u95f4\u6c42\u548c\uff0c\u4ece\u800c\u5f97\u5230\u8fd0\u884c\u65f6\u95f4\u3002

        \u4f8b\u5982\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        # \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\ndef algorithm(n: int):\n    a = 2      # 1 ns\n    a = a + 1  # 1 ns\n    a = a * 2  # 10 ns\n    # \u5faa\u73af n \u6b21\n    for _ in range(n):  # 1 ns\n        print(0)        # 5 ns\n
        // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // \u5faa\u73af n \u6b21\n    for (int i = 0; i < n; i++) {  // 1 ns\n        cout << 0 << endl;         // 5 ns\n    }\n}\n
        // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // \u5faa\u73af n \u6b21\n    for (int i = 0; i < n; i++) {  // 1 ns\n        System.out.println(0);     // 5 ns\n    }\n}\n
        // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid Algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // \u5faa\u73af n \u6b21\n    for (int i = 0; i < n; i++) {  // 1 ns\n        Console.WriteLine(0);      // 5 ns\n    }\n}\n
        // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunc algorithm(n int) {\n    a := 2     // 1 ns\n    a = a + 1  // 1 ns\n    a = a * 2  // 10 ns\n    // \u5faa\u73af n \u6b21\n    for i := 0; i < n; i++ {  // 1 ns\n        fmt.Println(a)        // 5 ns\n    }\n}\n
        // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunc algorithm(n: Int) {\n    var a = 2 // 1 ns\n    a = a + 1 // 1 ns\n    a = a * 2 // 10 ns\n    // \u5faa\u73af n \u6b21\n    for _ in 0 ..< n { // 1 ns\n        print(0) // 5 ns\n    }\n}\n
        // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunction algorithm(n) {\n    var a = 2; // 1 ns\n    a = a + 1; // 1 ns\n    a = a * 2; // 10 ns\n    // \u5faa\u73af n \u6b21\n    for(let i = 0; i < n; i++) { // 1 ns\n        console.log(0); // 5 ns\n    }\n}\n
        // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunction algorithm(n: number): void {\n    var a: number = 2; // 1 ns\n    a = a + 1; // 1 ns\n    a = a * 2; // 10 ns\n    // \u5faa\u73af n \u6b21\n    for(let i = 0; i < n; i++) { // 1 ns\n        console.log(0); // 5 ns\n    }\n}\n
        // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\n  int a = 2; // 1 ns\n  a = a + 1; // 1 ns\n  a = a * 2; // 10 ns\n  // \u5faa\u73af n \u6b21\n  for (int i = 0; i < n; i++) { // 1 ns\n    print(0); // 5 ns\n  }\n}\n
        // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfn algorithm(n: i32) {\n    let mut a = 2;      // 1 ns\n    a = a + 1;          // 1 ns\n    a = a * 2;          // 10 ns\n    // \u5faa\u73af n \u6b21\n    for _ in 0..n {     // 1 ns\n        println!(\"{}\", 0);  // 5 ns\n    }\n}\n
        // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // \u5faa\u73af n \u6b21\n    for (int i = 0; i < n; i++) {   // 1 ns\n        printf(\"%d\", 0);            // 5 ns\n    }\n}\n
        // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfun algorithm(n: Int) {\n    var a = 2 // 1 ns\n    a = a + 1 // 1 ns\n    a = a * 2 // 10 ns\n    // \u5faa\u73af n \u6b21\n    for (i in 0..<n) {  // 1 ns\n        println(0)      // 5 ns\n    }\n}\n
        # \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\ndef algorithm(n)\n    a = 2       # 1 ns\n    a = a + 1   # 1 ns\n    a = a * 2   # 10 ns\n    # \u5faa\u73af n \u6b21\n    (0...n).each do # 1 ns\n        puts 0      # 5 ns\n    end\nend\n

        \u6839\u636e\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5f97\u5230\u7b97\u6cd5\u7684\u8fd0\u884c\u65f6\u95f4\u4e3a \\((6n + 12)\\) ns \uff1a

        \\[ 1 + 1 + 10 + (1 + 5) \\times n = 6n + 12 \\]

        \u4f46\u5b9e\u9645\u4e0a\uff0c\u7edf\u8ba1\u7b97\u6cd5\u7684\u8fd0\u884c\u65f6\u95f4\u65e2\u4e0d\u5408\u7406\u4e5f\u4e0d\u73b0\u5b9e\u3002\u9996\u5148\uff0c\u6211\u4eec\u4e0d\u5e0c\u671b\u5c06\u9884\u4f30\u65f6\u95f4\u548c\u8fd0\u884c\u5e73\u53f0\u7ed1\u5b9a\uff0c\u56e0\u4e3a\u7b97\u6cd5\u9700\u8981\u5728\u5404\u79cd\u4e0d\u540c\u7684\u5e73\u53f0\u4e0a\u8fd0\u884c\u3002\u5176\u6b21\uff0c\u6211\u4eec\u5f88\u96be\u83b7\u77e5\u6bcf\u79cd\u64cd\u4f5c\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u8fd9\u7ed9\u9884\u4f30\u8fc7\u7a0b\u5e26\u6765\u4e86\u6781\u5927\u7684\u96be\u5ea6\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#231","title":"2.3.1 \u00a0 \u7edf\u8ba1\u65f6\u95f4\u589e\u957f\u8d8b\u52bf","text":"

        \u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u7edf\u8ba1\u7684\u4e0d\u662f\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\uff0c\u800c\u662f\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u968f\u7740\u6570\u636e\u91cf\u53d8\u5927\u65f6\u7684\u589e\u957f\u8d8b\u52bf\u3002

        \u201c\u65f6\u95f4\u589e\u957f\u8d8b\u52bf\u201d\u8fd9\u4e2a\u6982\u5ff5\u6bd4\u8f83\u62bd\u8c61\uff0c\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u6765\u52a0\u4ee5\u7406\u89e3\u3002\u5047\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u7ed9\u5b9a\u4e09\u4e2a\u7b97\u6cd5 A\u3001B \u548c C \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        # \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_A(n: int):\n    print(0)\n# \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\ndef algorithm_B(n: int):\n    for _ in range(n):\n        print(0)\n# \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_C(n: int):\n    for _ in range(1000000):\n        print(0)\n
        // \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\n    cout << 0 << endl;\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\n    for (int i = 0; i < n; i++) {\n        cout << 0 << endl;\n    }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        cout << 0 << endl;\n    }\n}\n
        // \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\n    System.out.println(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\n    for (int i = 0; i < n; i++) {\n        System.out.println(0);\n    }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        System.out.println(0);\n    }\n}\n
        // \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid AlgorithmA(int n) {\n    Console.WriteLine(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid AlgorithmB(int n) {\n    for (int i = 0; i < n; i++) {\n        Console.WriteLine(0);\n    }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid AlgorithmC(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        Console.WriteLine(0);\n    }\n}\n
        // \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithm_A(n int) {\n    fmt.Println(0)\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunc algorithm_B(n int) {\n    for i := 0; i < n; i++ {\n        fmt.Println(0)\n    }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithm_C(n int) {\n    for i := 0; i < 1000000; i++ {\n        fmt.Println(0)\n    }\n}\n
        // \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithmA(n: Int) {\n    print(0)\n}\n\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunc algorithmB(n: Int) {\n    for _ in 0 ..< n {\n        print(0)\n    }\n}\n\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithmC(n: Int) {\n    for _ in 0 ..< 1_000_000 {\n        print(0)\n    }\n}\n
        // \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_A(n) {\n    console.log(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunction algorithm_B(n) {\n    for (let i = 0; i < n; i++) {\n        console.log(0);\n    }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_C(n) {\n    for (let i = 0; i < 1000000; i++) {\n        console.log(0);\n    }\n}\n
        // \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_A(n: number): void {\n    console.log(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunction algorithm_B(n: number): void {\n    for (let i = 0; i < n; i++) {\n        console.log(0);\n    }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_C(n: number): void {\n    for (let i = 0; i < 1000000; i++) {\n        console.log(0);\n    }\n}\n
        // \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithmA(int n) {\n  print(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithmB(int n) {\n  for (int i = 0; i < n; i++) {\n    print(0);\n  }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithmC(int n) {\n  for (int i = 0; i < 1000000; i++) {\n    print(0);\n  }\n}\n
        // \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfn algorithm_A(n: i32) {\n    println!(\"{}\", 0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfn algorithm_B(n: i32) {\n    for _ in 0..n {\n        println!(\"{}\", 0);\n    }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfn algorithm_C(n: i32) {\n    for _ in 0..1000000 {\n        println!(\"{}\", 0);\n    }\n}\n
        // \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\n    printf(\"%d\", 0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\n    for (int i = 0; i < n; i++) {\n        printf(\"%d\", 0);\n    }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        printf(\"%d\", 0);\n    }\n}\n
        // \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfun algoritm_A(n: Int) {\n    println(0)\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfun algorithm_B(n: Int) {\n    for (i in 0..<n){\n        println(0)\n    }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfun algorithm_C(n: Int) {\n    for (i in 0..<1000000) {\n        println(0)\n    }\n}\n
        # \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_A(n)\n    puts 0\nend\n\n# \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\ndef algorithm_B(n)\n    (0...n).each { puts 0 }\nend\n\n# \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_C(n)\n    (0...1_000_000).each { puts 0 }\nend\n

        \u56fe 2-7 \u5c55\u793a\u4e86\u4ee5\u4e0a\u4e09\u4e2a\u7b97\u6cd5\u51fd\u6570\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002

        • \u7b97\u6cd5 A \u53ea\u6709 \\(1\\) \u4e2a\u6253\u5370\u64cd\u4f5c\uff0c\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u4e0d\u968f\u7740 \\(n\\) \u589e\u5927\u800c\u589e\u957f\u3002\u6211\u4eec\u79f0\u6b64\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\u201c\u5e38\u6570\u9636\u201d\u3002
        • \u7b97\u6cd5 B \u4e2d\u7684\u6253\u5370\u64cd\u4f5c\u9700\u8981\u5faa\u73af \\(n\\) \u6b21\uff0c\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u968f\u7740 \\(n\\) \u589e\u5927\u5448\u7ebf\u6027\u589e\u957f\u3002\u6b64\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u88ab\u79f0\u4e3a\u201c\u7ebf\u6027\u9636\u201d\u3002
        • \u7b97\u6cd5 C \u4e2d\u7684\u6253\u5370\u64cd\u4f5c\u9700\u8981\u5faa\u73af \\(1000000\\) \u6b21\uff0c\u867d\u7136\u8fd0\u884c\u65f6\u95f4\u5f88\u957f\uff0c\u4f46\u5b83\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\u3002\u56e0\u6b64 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u548c A \u76f8\u540c\uff0c\u4ecd\u4e3a\u201c\u5e38\u6570\u9636\u201d\u3002

        \u56fe 2-7 \u00a0 \u7b97\u6cd5 A\u3001B \u548c C \u7684\u65f6\u95f4\u589e\u957f\u8d8b\u52bf

        \u76f8\u8f83\u4e8e\u76f4\u63a5\u7edf\u8ba1\u7b97\u6cd5\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u6709\u54ea\u4e9b\u7279\u70b9\u5462\uff1f

        • \u65f6\u95f4\u590d\u6742\u5ea6\u80fd\u591f\u6709\u6548\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\u3002\u4f8b\u5982\uff0c\u7b97\u6cd5 B \u7684\u8fd0\u884c\u65f6\u95f4\u5448\u7ebf\u6027\u589e\u957f\uff0c\u5728 \\(n > 1\\) \u65f6\u6bd4\u7b97\u6cd5 A \u66f4\u6162\uff0c\u5728 \\(n > 1000000\\) \u65f6\u6bd4\u7b97\u6cd5 C \u66f4\u6162\u3002\u4e8b\u5b9e\u4e0a\uff0c\u53ea\u8981\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u8db3\u591f\u5927\uff0c\u590d\u6742\u5ea6\u4e3a\u201c\u5e38\u6570\u9636\u201d\u7684\u7b97\u6cd5\u4e00\u5b9a\u4f18\u4e8e\u201c\u7ebf\u6027\u9636\u201d\u7684\u7b97\u6cd5\uff0c\u8fd9\u6b63\u662f\u65f6\u95f4\u589e\u957f\u8d8b\u52bf\u7684\u542b\u4e49\u3002
        • \u65f6\u95f4\u590d\u6742\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u66f4\u7b80\u4fbf\u3002\u663e\u7136\uff0c\u8fd0\u884c\u5e73\u53f0\u548c\u8ba1\u7b97\u64cd\u4f5c\u7c7b\u578b\u90fd\u4e0e\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u65e0\u5173\u3002\u56e0\u6b64\u5728\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u5c06\u6240\u6709\u8ba1\u7b97\u64cd\u4f5c\u7684\u6267\u884c\u65f6\u95f4\u89c6\u4e3a\u76f8\u540c\u7684\u201c\u5355\u4f4d\u65f6\u95f4\u201d\uff0c\u4ece\u800c\u5c06\u201c\u8ba1\u7b97\u64cd\u4f5c\u8fd0\u884c\u65f6\u95f4\u7edf\u8ba1\u201d\u7b80\u5316\u4e3a\u201c\u8ba1\u7b97\u64cd\u4f5c\u6570\u91cf\u7edf\u8ba1\u201d\uff0c\u8fd9\u6837\u4e00\u6765\u4f30\u7b97\u96be\u5ea6\u5c31\u5927\u5927\u964d\u4f4e\u4e86\u3002
        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u5b58\u5728\u4e00\u5b9a\u7684\u5c40\u9650\u6027\u3002\u4f8b\u5982\uff0c\u5c3d\u7ba1\u7b97\u6cd5 A \u548c C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u76f8\u540c\uff0c\u4f46\u5b9e\u9645\u8fd0\u884c\u65f6\u95f4\u5dee\u522b\u5f88\u5927\u3002\u540c\u6837\uff0c\u5c3d\u7ba1\u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u6bd4 C \u9ad8\uff0c\u4f46\u5728\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u8f83\u5c0f\u65f6\uff0c\u7b97\u6cd5 B \u660e\u663e\u4f18\u4e8e\u7b97\u6cd5 C \u3002\u5bf9\u4e8e\u6b64\u7c7b\u60c5\u51b5\uff0c\u6211\u4eec\u65f6\u5e38\u96be\u4ee5\u4ec5\u51ed\u65f6\u95f4\u590d\u6742\u5ea6\u5224\u65ad\u7b97\u6cd5\u6548\u7387\u7684\u9ad8\u4f4e\u3002\u5f53\u7136\uff0c\u5c3d\u7ba1\u5b58\u5728\u4e0a\u8ff0\u95ee\u9898\uff0c\u590d\u6742\u5ea6\u5206\u6790\u4ecd\u7136\u662f\u8bc4\u5224\u7b97\u6cd5\u6548\u7387\u6700\u6709\u6548\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\u3002
        "},{"location":"chapter_computational_complexity/time_complexity/#232","title":"2.3.2 \u00a0 \u51fd\u6570\u6e10\u8fd1\u4e0a\u754c","text":"

        \u7ed9\u5b9a\u4e00\u4e2a\u8f93\u5165\u5927\u5c0f\u4e3a \\(n\\) \u7684\u51fd\u6570\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        def algorithm(n: int):\n    a = 1      # +1\n    a = a + 1  # +1\n    a = a * 2  # +1\n    # \u5faa\u73af n \u6b21\n    for i in range(n):  # +1\n        print(0)        # +1\n
        void algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // \u5faa\u73af n \u6b21\n    for (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n        cout << 0 << endl;    // +1\n    }\n}\n
        void algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // \u5faa\u73af n \u6b21\n    for (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n        System.out.println(0);    // +1\n    }\n}\n
        void Algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // \u5faa\u73af n \u6b21\n    for (int i = 0; i < n; i++) {   // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n        Console.WriteLine(0);   // +1\n    }\n}\n
        func algorithm(n int) {\n    a := 1      // +1\n    a = a + 1   // +1\n    a = a * 2   // +1\n    // \u5faa\u73af n \u6b21\n    for i := 0; i < n; i++ {   // +1\n        fmt.Println(a)         // +1\n    }\n}\n
        func algorithm(n: Int) {\n    var a = 1 // +1\n    a = a + 1 // +1\n    a = a * 2 // +1\n    // \u5faa\u73af n \u6b21\n    for _ in 0 ..< n { // +1\n        print(0) // +1\n    }\n}\n
        function algorithm(n) {\n    var a = 1; // +1\n    a += 1; // +1\n    a *= 2; // +1\n    // \u5faa\u73af n \u6b21\n    for(let i = 0; i < n; i++){ // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n        console.log(0); // +1\n    }\n}\n
        function algorithm(n: number): void{\n    var a: number = 1; // +1\n    a += 1; // +1\n    a *= 2; // +1\n    // \u5faa\u73af n \u6b21\n    for(let i = 0; i < n; i++){ // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n        console.log(0); // +1\n    }\n}\n
        void algorithm(int n) {\n  int a = 1; // +1\n  a = a + 1; // +1\n  a = a * 2; // +1\n  // \u5faa\u73af n \u6b21\n  for (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n    print(0); // +1\n  }\n}\n
        fn algorithm(n: i32) {\n    let mut a = 1;   // +1\n    a = a + 1;      // +1\n    a = a * 2;      // +1\n\n    // \u5faa\u73af n \u6b21\n    for _ in 0..n { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n        println!(\"{}\", 0); // +1\n    }\n}\n
        void algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // \u5faa\u73af n \u6b21\n    for (int i = 0; i < n; i++) {   // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n        printf(\"%d\", 0);            // +1\n    }\n}\n
        fun algorithm(n: Int) {\n    var a = 1 // +1\n    a = a + 1 // +1\n    a = a * 2 // +1\n    // \u5faa\u73af n \u6b21\n    for (i in 0..<n) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n        println(0) // +1\n    }\n}\n
        def algorithm(n)\n    a = 1       # +1\n    a = a + 1   # +1\n    a = a * 2   # +1\n    # \u5faa\u73af n \u6b21\n    (0...n).each do # +1\n        puts 0      # +1\n    end\nend\n

        \u8bbe\u7b97\u6cd5\u7684\u64cd\u4f5c\u6570\u91cf\u662f\u4e00\u4e2a\u5173\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u7684\u51fd\u6570\uff0c\u8bb0\u4e3a \\(T(n)\\) \uff0c\u5219\u4ee5\u4e0a\u51fd\u6570\u7684\u64cd\u4f5c\u6570\u91cf\u4e3a\uff1a

        \\[ T(n) = 3 + 2n \\]

        \\(T(n)\\) \u662f\u4e00\u6b21\u51fd\u6570\uff0c\u8bf4\u660e\u5176\u8fd0\u884c\u65f6\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u662f\u7ebf\u6027\u7684\uff0c\u56e0\u6b64\u5b83\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f\u7ebf\u6027\u9636\u3002

        \u6211\u4eec\u5c06\u7ebf\u6027\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8bb0\u4e3a \\(O(n)\\) \uff0c\u8fd9\u4e2a\u6570\u5b66\u7b26\u53f7\u79f0\u4e3a\u5927 \\(O\\) \u8bb0\u53f7\uff08big-\\(O\\) notation\uff09\uff0c\u8868\u793a\u51fd\u6570 \\(T(n)\\) \u7684\u6e10\u8fd1\u4e0a\u754c\uff08asymptotic upper bound\uff09\u3002

        \u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u672c\u8d28\u4e0a\u662f\u8ba1\u7b97\u201c\u64cd\u4f5c\u6570\u91cf \\(T(n)\\)\u201d\u7684\u6e10\u8fd1\u4e0a\u754c\uff0c\u5b83\u5177\u6709\u660e\u786e\u7684\u6570\u5b66\u5b9a\u4e49\u3002

        \u51fd\u6570\u6e10\u8fd1\u4e0a\u754c

        \u82e5\u5b58\u5728\u6b63\u5b9e\u6570 \\(c\\) \u548c\u5b9e\u6570 \\(n_0\\) \uff0c\u4f7f\u5f97\u5bf9\u4e8e\u6240\u6709\u7684 \\(n > n_0\\) \uff0c\u5747\u6709 \\(T(n) \\leq c \\cdot f(n)\\) \uff0c\u5219\u53ef\u8ba4\u4e3a \\(f(n)\\) \u7ed9\u51fa\u4e86 \\(T(n)\\) \u7684\u4e00\u4e2a\u6e10\u8fd1\u4e0a\u754c\uff0c\u8bb0\u4e3a \\(T(n) = O(f(n))\\) \u3002

        \u5982\u56fe 2-8 \u6240\u793a\uff0c\u8ba1\u7b97\u6e10\u8fd1\u4e0a\u754c\u5c31\u662f\u5bfb\u627e\u4e00\u4e2a\u51fd\u6570 \\(f(n)\\) \uff0c\u4f7f\u5f97\u5f53 \\(n\\) \u8d8b\u5411\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\\(T(n)\\) \u548c \\(f(n)\\) \u5904\u4e8e\u76f8\u540c\u7684\u589e\u957f\u7ea7\u522b\uff0c\u4ec5\u76f8\u5dee\u4e00\u4e2a\u5e38\u6570\u7cfb\u6570 \\(c\\)\u3002

        \u56fe 2-8 \u00a0 \u51fd\u6570\u7684\u6e10\u8fd1\u4e0a\u754c

        "},{"location":"chapter_computational_complexity/time_complexity/#233","title":"2.3.3 \u00a0 \u63a8\u7b97\u65b9\u6cd5","text":"

        \u6e10\u8fd1\u4e0a\u754c\u7684\u6570\u5b66\u5473\u513f\u6709\u70b9\u91cd\uff0c\u5982\u679c\u4f60\u611f\u89c9\u6ca1\u6709\u5b8c\u5168\u7406\u89e3\uff0c\u4e5f\u65e0\u987b\u62c5\u5fc3\u3002\u6211\u4eec\u53ef\u4ee5\u5148\u638c\u63e1\u63a8\u7b97\u65b9\u6cd5\uff0c\u5728\u4e0d\u65ad\u7684\u5b9e\u8df5\u4e2d\uff0c\u5c31\u53ef\u4ee5\u9010\u6e10\u9886\u609f\u5176\u6570\u5b66\u610f\u4e49\u3002

        \u6839\u636e\u5b9a\u4e49\uff0c\u786e\u5b9a \\(f(n)\\) \u4e4b\u540e\uff0c\u6211\u4eec\u4fbf\u53ef\u5f97\u5230\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(f(n))\\) \u3002\u90a3\u4e48\u5982\u4f55\u786e\u5b9a\u6e10\u8fd1\u4e0a\u754c \\(f(n)\\) \u5462\uff1f\u603b\u4f53\u5206\u4e3a\u4e24\u6b65\uff1a\u9996\u5148\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf\uff0c\u7136\u540e\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#1","title":"1. \u00a0 \u7b2c\u4e00\u6b65\uff1a\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf","text":"

        \u9488\u5bf9\u4ee3\u7801\uff0c\u9010\u884c\u4ece\u4e0a\u5230\u4e0b\u8ba1\u7b97\u5373\u53ef\u3002\u7136\u800c\uff0c\u7531\u4e8e\u4e0a\u8ff0 \\(c \\cdot f(n)\\) \u4e2d\u7684\u5e38\u6570\u7cfb\u6570 \\(c\\) \u53ef\u4ee5\u53d6\u4efb\u610f\u5927\u5c0f\uff0c\u56e0\u6b64\u64cd\u4f5c\u6570\u91cf \\(T(n)\\) \u4e2d\u7684\u5404\u79cd\u7cfb\u6570\u3001\u5e38\u6570\u9879\u90fd\u53ef\u4ee5\u5ffd\u7565\u3002\u6839\u636e\u6b64\u539f\u5219\uff0c\u53ef\u4ee5\u603b\u7ed3\u51fa\u4ee5\u4e0b\u8ba1\u6570\u7b80\u5316\u6280\u5de7\u3002

        1. \u5ffd\u7565 \\(T(n)\\) \u4e2d\u7684\u5e38\u6570\u3002\u56e0\u4e3a\u5b83\u4eec\u90fd\u4e0e \\(n\\) \u65e0\u5173\uff0c\u6240\u4ee5\u5bf9\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u4ea7\u751f\u5f71\u54cd\u3002
        2. \u7701\u7565\u6240\u6709\u7cfb\u6570\u3002\u4f8b\u5982\uff0c\u5faa\u73af \\(2n\\) \u6b21\u3001\\(5n + 1\\) \u6b21\u7b49\uff0c\u90fd\u53ef\u4ee5\u7b80\u5316\u8bb0\u4e3a \\(n\\) \u6b21\uff0c\u56e0\u4e3a \\(n\\) \u524d\u9762\u7684\u7cfb\u6570\u5bf9\u65f6\u95f4\u590d\u6742\u5ea6\u6ca1\u6709\u5f71\u54cd\u3002
        3. \u5faa\u73af\u5d4c\u5957\u65f6\u4f7f\u7528\u4e58\u6cd5\u3002\u603b\u64cd\u4f5c\u6570\u91cf\u7b49\u4e8e\u5916\u5c42\u5faa\u73af\u548c\u5185\u5c42\u5faa\u73af\u64cd\u4f5c\u6570\u91cf\u4e4b\u79ef\uff0c\u6bcf\u4e00\u5c42\u5faa\u73af\u4f9d\u7136\u53ef\u4ee5\u5206\u522b\u5957\u7528\u7b2c 1. \u70b9\u548c\u7b2c 2. \u70b9\u7684\u6280\u5de7\u3002

        \u7ed9\u5b9a\u4e00\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4e0a\u8ff0\u6280\u5de7\u6765\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        def algorithm(n: int):\n    a = 1      # +0\uff08\u6280\u5de7 1\uff09\n    a = a + n  # +0\uff08\u6280\u5de7 1\uff09\n    # +n\uff08\u6280\u5de7 2\uff09\n    for i in range(5 * n + 1):\n        print(0)\n    # +n*n\uff08\u6280\u5de7 3\uff09\n    for i in range(2 * n):\n        for j in range(n + 1):\n            print(0)\n
        void algorithm(int n) {\n    int a = 1;  // +0\uff08\u6280\u5de7 1\uff09\n    a = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n    // +n\uff08\u6280\u5de7 2\uff09\n    for (int i = 0; i < 5 * n + 1; i++) {\n        cout << 0 << endl;\n    }\n    // +n*n\uff08\u6280\u5de7 3\uff09\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            cout << 0 << endl;\n        }\n    }\n}\n
        void algorithm(int n) {\n    int a = 1;  // +0\uff08\u6280\u5de7 1\uff09\n    a = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n    // +n\uff08\u6280\u5de7 2\uff09\n    for (int i = 0; i < 5 * n + 1; i++) {\n        System.out.println(0);\n    }\n    // +n*n\uff08\u6280\u5de7 3\uff09\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            System.out.println(0);\n        }\n    }\n}\n
        void Algorithm(int n) {\n    int a = 1;  // +0\uff08\u6280\u5de7 1\uff09\n    a = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n    // +n\uff08\u6280\u5de7 2\uff09\n    for (int i = 0; i < 5 * n + 1; i++) {\n        Console.WriteLine(0);\n    }\n    // +n*n\uff08\u6280\u5de7 3\uff09\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            Console.WriteLine(0);\n        }\n    }\n}\n
        func algorithm(n int) {\n    a := 1     // +0\uff08\u6280\u5de7 1\uff09\n    a = a + n  // +0\uff08\u6280\u5de7 1\uff09\n    // +n\uff08\u6280\u5de7 2\uff09\n    for i := 0; i < 5 * n + 1; i++ {\n        fmt.Println(0)\n    }\n    // +n*n\uff08\u6280\u5de7 3\uff09\n    for i := 0; i < 2 * n; i++ {\n        for j := 0; j < n + 1; j++ {\n            fmt.Println(0)\n        }\n    }\n}\n
        func algorithm(n: Int) {\n    var a = 1 // +0\uff08\u6280\u5de7 1\uff09\n    a = a + n // +0\uff08\u6280\u5de7 1\uff09\n    // +n\uff08\u6280\u5de7 2\uff09\n    for _ in 0 ..< (5 * n + 1) {\n        print(0)\n    }\n    // +n*n\uff08\u6280\u5de7 3\uff09\n    for _ in 0 ..< (2 * n) {\n        for _ in 0 ..< (n + 1) {\n            print(0)\n        }\n    }\n}\n
        function algorithm(n) {\n    let a = 1;  // +0\uff08\u6280\u5de7 1\uff09\n    a = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n    // +n\uff08\u6280\u5de7 2\uff09\n    for (let i = 0; i < 5 * n + 1; i++) {\n        console.log(0);\n    }\n    // +n*n\uff08\u6280\u5de7 3\uff09\n    for (let i = 0; i < 2 * n; i++) {\n        for (let j = 0; j < n + 1; j++) {\n            console.log(0);\n        }\n    }\n}\n
        function algorithm(n: number): void {\n    let a = 1;  // +0\uff08\u6280\u5de7 1\uff09\n    a = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n    // +n\uff08\u6280\u5de7 2\uff09\n    for (let i = 0; i < 5 * n + 1; i++) {\n        console.log(0);\n    }\n    // +n*n\uff08\u6280\u5de7 3\uff09\n    for (let i = 0; i < 2 * n; i++) {\n        for (let j = 0; j < n + 1; j++) {\n            console.log(0);\n        }\n    }\n}\n
        void algorithm(int n) {\n  int a = 1; // +0\uff08\u6280\u5de7 1\uff09\n  a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n  // +n\uff08\u6280\u5de7 2\uff09\n  for (int i = 0; i < 5 * n + 1; i++) {\n    print(0);\n  }\n  // +n*n\uff08\u6280\u5de7 3\uff09\n  for (int i = 0; i < 2 * n; i++) {\n    for (int j = 0; j < n + 1; j++) {\n      print(0);\n    }\n  }\n}\n
        fn algorithm(n: i32) {\n    let mut a = 1;     // +0\uff08\u6280\u5de7 1\uff09\n    a = a + n;        // +0\uff08\u6280\u5de7 1\uff09\n\n    // +n\uff08\u6280\u5de7 2\uff09\n    for i in 0..(5 * n + 1) {\n        println!(\"{}\", 0);\n    }\n\n    // +n*n\uff08\u6280\u5de7 3\uff09\n    for i in 0..(2 * n) {\n        for j in 0..(n + 1) {\n            println!(\"{}\", 0);\n        }\n    }\n}\n
        void algorithm(int n) {\n    int a = 1;  // +0\uff08\u6280\u5de7 1\uff09\n    a = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n    // +n\uff08\u6280\u5de7 2\uff09\n    for (int i = 0; i < 5 * n + 1; i++) {\n        printf(\"%d\", 0);\n    }\n    // +n*n\uff08\u6280\u5de7 3\uff09\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            printf(\"%d\", 0);\n        }\n    }\n}\n
        fun algorithm(n: Int) {\n    var a = 1   // +0\uff08\u6280\u5de7 1\uff09\n    a = a + n   // +0\uff08\u6280\u5de7 1\uff09\n    // +n\uff08\u6280\u5de7 2\uff09\n    for (i in 0..<5 * n + 1) {\n        println(0)\n    }\n    // +n*n\uff08\u6280\u5de7 3\uff09\n    for (i in 0..<2 * n) {\n        for (j in 0..<n + 1) {\n            println(0)\n        }\n    }\n}\n
        def algorithm(n)\n    a = 1       # +0\uff08\u6280\u5de7 1\uff09\n    a = a + n   # +0\uff08\u6280\u5de7 1\uff09\n    # +n\uff08\u6280\u5de7 2\uff09\n    (0...(5 * n + 1)).each do { puts 0 }\n    # +n*n\uff08\u6280\u5de7 3\uff09\n    (0...(2 * n)).each do\n        (0...(n + 1)).each do { puts 0 }\n    end\nend\n

        \u4ee5\u4e0b\u516c\u5f0f\u5c55\u793a\u4e86\u4f7f\u7528\u4e0a\u8ff0\u6280\u5de7\u524d\u540e\u7684\u7edf\u8ba1\u7ed3\u679c\uff0c\u4e24\u8005\u63a8\u7b97\u51fa\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n^2)\\) \u3002

        \\[ \\begin{aligned} T(n) & = 2n(n + 1) + (5n + 1) + 2 & \\text{\u5b8c\u6574\u7edf\u8ba1 (-.-|||)} \\newline & = 2n^2 + 7n + 3 \\newline T(n) & = n^2 + n & \\text{\u5077\u61d2\u7edf\u8ba1 (o.O)} \\end{aligned} \\]"},{"location":"chapter_computational_complexity/time_complexity/#2","title":"2. \u00a0 \u7b2c\u4e8c\u6b65\uff1a\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c","text":"

        \u65f6\u95f4\u590d\u6742\u5ea6\u7531 \\(T(n)\\) \u4e2d\u6700\u9ad8\u9636\u7684\u9879\u6765\u51b3\u5b9a\u3002\u8fd9\u662f\u56e0\u4e3a\u5728 \\(n\\) \u8d8b\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\u6700\u9ad8\u9636\u7684\u9879\u5c06\u53d1\u6325\u4e3b\u5bfc\u4f5c\u7528\uff0c\u5176\u4ed6\u9879\u7684\u5f71\u54cd\u90fd\u53ef\u4ee5\u5ffd\u7565\u3002

        \u8868 2-2 \u5c55\u793a\u4e86\u4e00\u4e9b\u4f8b\u5b50\uff0c\u5176\u4e2d\u4e00\u4e9b\u5938\u5f20\u7684\u503c\u662f\u4e3a\u4e86\u5f3a\u8c03\u201c\u7cfb\u6570\u65e0\u6cd5\u64bc\u52a8\u9636\u6570\u201d\u8fd9\u4e00\u7ed3\u8bba\u3002\u5f53 \\(n\\) \u8d8b\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\u8fd9\u4e9b\u5e38\u6570\u53d8\u5f97\u65e0\u8db3\u8f7b\u91cd\u3002

        \u8868 2-2 \u00a0 \u4e0d\u540c\u64cd\u4f5c\u6570\u91cf\u5bf9\u5e94\u7684\u65f6\u95f4\u590d\u6742\u5ea6

        \u64cd\u4f5c\u6570\u91cf \\(T(n)\\) \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(f(n))\\) \\(100000\\) \\(O(1)\\) \\(3n + 2\\) \\(O(n)\\) \\(2n^2 + 3n + 2\\) \\(O(n^2)\\) \\(n^3 + 10000n^2\\) \\(O(n^3)\\) \\(2^n + 10000n^{10000}\\) \\(O(2^n)\\)"},{"location":"chapter_computational_complexity/time_complexity/#234","title":"2.3.4 \u00a0 \u5e38\u89c1\u7c7b\u578b","text":"

        \u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u5e38\u89c1\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\u5982\u56fe 2-9 \u6240\u793a\uff08\u6309\u7167\u4ece\u4f4e\u5230\u9ad8\u7684\u987a\u5e8f\u6392\u5217\uff09\u3002

        \\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n \\log n) < O(n^2) < O(2^n) < O(n!) \\newline \\text{\u5e38\u6570\u9636} < \\text{\u5bf9\u6570\u9636} < \\text{\u7ebf\u6027\u9636} < \\text{\u7ebf\u6027\u5bf9\u6570\u9636} < \\text{\u5e73\u65b9\u9636} < \\text{\u6307\u6570\u9636} < \\text{\u9636\u4e58\u9636} \\end{aligned} \\]

        \u56fe 2-9 \u00a0 \u5e38\u89c1\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7c7b\u578b

        "},{"location":"chapter_computational_complexity/time_complexity/#1-o1","title":"1. \u00a0 \u5e38\u6570\u9636 \\(O(1)\\)","text":"

        \u5e38\u6570\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\uff0c\u5373\u4e0d\u968f\u7740 \\(n\\) \u7684\u53d8\u5316\u800c\u53d8\u5316\u3002

        \u5728\u4ee5\u4e0b\u51fd\u6570\u4e2d\uff0c\u5c3d\u7ba1\u64cd\u4f5c\u6570\u91cf size \u53ef\u80fd\u5f88\u5927\uff0c\u4f46\u7531\u4e8e\u5176\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def constant(n: int) -> int:\n    \"\"\"\u5e38\u6570\u9636\"\"\"\n    count = 0\n    size = 100000\n    for _ in range(size):\n        count += 1\n    return count\n
        time_complexity.cpp
        /* \u5e38\u6570\u9636 */\nint constant(int n) {\n    int count = 0;\n    int size = 100000;\n    for (int i = 0; i < size; i++)\n        count++;\n    return count;\n}\n
        time_complexity.java
        /* \u5e38\u6570\u9636 */\nint constant(int n) {\n    int count = 0;\n    int size = 100000;\n    for (int i = 0; i < size; i++)\n        count++;\n    return count;\n}\n
        time_complexity.cs
        /* \u5e38\u6570\u9636 */\nint Constant(int n) {\n    int count = 0;\n    int size = 100000;\n    for (int i = 0; i < size; i++)\n        count++;\n    return count;\n}\n
        time_complexity.go
        /* \u5e38\u6570\u9636 */\nfunc constant(n int) int {\n    count := 0\n    size := 100000\n    for i := 0; i < size; i++ {\n        count++\n    }\n    return count\n}\n
        time_complexity.swift
        /* \u5e38\u6570\u9636 */\nfunc constant(n: Int) -> Int {\n    var count = 0\n    let size = 100_000\n    for _ in 0 ..< size {\n        count += 1\n    }\n    return count\n}\n
        time_complexity.js
        /* \u5e38\u6570\u9636 */\nfunction constant(n) {\n    let count = 0;\n    const size = 100000;\n    for (let i = 0; i < size; i++) count++;\n    return count;\n}\n
        time_complexity.ts
        /* \u5e38\u6570\u9636 */\nfunction constant(n: number): number {\n    let count = 0;\n    const size = 100000;\n    for (let i = 0; i < size; i++) count++;\n    return count;\n}\n
        time_complexity.dart
        /* \u5e38\u6570\u9636 */\nint constant(int n) {\n  int count = 0;\n  int size = 100000;\n  for (var i = 0; i < size; i++) {\n    count++;\n  }\n  return count;\n}\n
        time_complexity.rs
        /* \u5e38\u6570\u9636 */\nfn constant(n: i32) -> i32 {\n    _ = n;\n    let mut count = 0;\n    let size = 100_000;\n    for _ in 0..size {\n        count += 1;\n    }\n    count\n}\n
        time_complexity.c
        /* \u5e38\u6570\u9636 */\nint constant(int n) {\n    int count = 0;\n    int size = 100000;\n    int i = 0;\n    for (int i = 0; i < size; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.kt
        /* \u5e38\u6570\u9636 */\nfun constant(n: Int): Int {\n    var count = 0\n    val size = 100000\n    for (i in 0..<size)\n        count++\n    return count\n}\n
        time_complexity.rb
        ### \u5e38\u6570\u9636 ###\ndef constant(n)\n  count = 0\n  size = 100000\n\n  (0...size).each { count += 1 }\n\n  count\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_computational_complexity/time_complexity/#2-on","title":"2. \u00a0 \u7ebf\u6027\u9636 \\(O(n)\\)","text":"

        \u7ebf\u6027\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u76f8\u5bf9\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u4ee5\u7ebf\u6027\u7ea7\u522b\u589e\u957f\u3002\u7ebf\u6027\u9636\u901a\u5e38\u51fa\u73b0\u5728\u5355\u5c42\u5faa\u73af\u4e2d\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def linear(n: int) -> int:\n    \"\"\"\u7ebf\u6027\u9636\"\"\"\n    count = 0\n    for _ in range(n):\n        count += 1\n    return count\n
        time_complexity.cpp
        /* \u7ebf\u6027\u9636 */\nint linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++)\n        count++;\n    return count;\n}\n
        time_complexity.java
        /* \u7ebf\u6027\u9636 */\nint linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++)\n        count++;\n    return count;\n}\n
        time_complexity.cs
        /* \u7ebf\u6027\u9636 */\nint Linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++)\n        count++;\n    return count;\n}\n
        time_complexity.go
        /* \u7ebf\u6027\u9636 */\nfunc linear(n int) int {\n    count := 0\n    for i := 0; i < n; i++ {\n        count++\n    }\n    return count\n}\n
        time_complexity.swift
        /* \u7ebf\u6027\u9636 */\nfunc linear(n: Int) -> Int {\n    var count = 0\n    for _ in 0 ..< n {\n        count += 1\n    }\n    return count\n}\n
        time_complexity.js
        /* \u7ebf\u6027\u9636 */\nfunction linear(n) {\n    let count = 0;\n    for (let i = 0; i < n; i++) count++;\n    return count;\n}\n
        time_complexity.ts
        /* \u7ebf\u6027\u9636 */\nfunction linear(n: number): number {\n    let count = 0;\n    for (let i = 0; i < n; i++) count++;\n    return count;\n}\n
        time_complexity.dart
        /* \u7ebf\u6027\u9636 */\nint linear(int n) {\n  int count = 0;\n  for (var i = 0; i < n; i++) {\n    count++;\n  }\n  return count;\n}\n
        time_complexity.rs
        /* \u7ebf\u6027\u9636 */\nfn linear(n: i32) -> i32 {\n    let mut count = 0;\n    for _ in 0..n {\n        count += 1;\n    }\n    count\n}\n
        time_complexity.c
        /* \u7ebf\u6027\u9636 */\nint linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.kt
        /* \u7ebf\u6027\u9636 */\nfun linear(n: Int): Int {\n    var count = 0\n    for (i in 0..<n)\n        count++\n    return count\n}\n
        time_complexity.rb
        ### \u7ebf\u6027\u9636 ###\ndef linear(n)\n  count = 0\n  (0...n).each { count += 1 }\n  count\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u904d\u5386\u6570\u7ec4\u548c\u904d\u5386\u94fe\u8868\u7b49\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u6570\u7ec4\u6216\u94fe\u8868\u7684\u957f\u5ea6\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def array_traversal(nums: list[int]) -> int:\n    \"\"\"\u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09\"\"\"\n    count = 0\n    # \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n    for num in nums:\n        count += 1\n    return count\n
        time_complexity.cpp
        /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(vector<int> &nums) {\n    int count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n    for (int num : nums) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int[] nums) {\n    int count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n    for (int num : nums) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.cs
        /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint ArrayTraversal(int[] nums) {\n    int count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n    foreach (int num in nums) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.go
        /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunc arrayTraversal(nums []int) int {\n    count := 0\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n    for range nums {\n        count++\n    }\n    return count\n}\n
        time_complexity.swift
        /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunc arrayTraversal(nums: [Int]) -> Int {\n    var count = 0\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n    for _ in nums {\n        count += 1\n    }\n    return count\n}\n
        time_complexity.js
        /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunction arrayTraversal(nums) {\n    let count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n    for (let i = 0; i < nums.length; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.ts
        /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunction arrayTraversal(nums: number[]): number {\n    let count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n    for (let i = 0; i < nums.length; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.dart
        /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(List<int> nums) {\n  int count = 0;\n  // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n  for (var _num in nums) {\n    count++;\n  }\n  return count;\n}\n
        time_complexity.rs
        /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfn array_traversal(nums: &[i32]) -> i32 {\n    let mut count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n    for _ in nums {\n        count += 1;\n    }\n    count\n}\n
        time_complexity.c
        /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int *nums, int n) {\n    int count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.kt
        /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfun arrayTraversal(nums: IntArray): Int {\n    var count = 0\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n    for (num in nums) {\n        count++\n    }\n    return count\n}\n
        time_complexity.rb
        ### \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09###\ndef array_traversal(nums)\n  count = 0\n\n  # \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n  for num in nums\n    count += 1\n  end\n\n  count\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u9700\u6839\u636e\u8f93\u5165\u6570\u636e\u7684\u7c7b\u578b\u6765\u5177\u4f53\u786e\u5b9a\u3002\u6bd4\u5982\u5728\u7b2c\u4e00\u4e2a\u793a\u4f8b\u4e2d\uff0c\u53d8\u91cf \\(n\\) \u4e3a\u8f93\u5165\u6570\u636e\u5927\u5c0f\uff1b\u5728\u7b2c\u4e8c\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6570\u7ec4\u957f\u5ea6 \\(n\\) \u4e3a\u6570\u636e\u5927\u5c0f\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#3-on2","title":"3. \u00a0 \u5e73\u65b9\u9636 \\(O(n^2)\\)","text":"

        \u5e73\u65b9\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u76f8\u5bf9\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u4ee5\u5e73\u65b9\u7ea7\u522b\u589e\u957f\u3002\u5e73\u65b9\u9636\u901a\u5e38\u51fa\u73b0\u5728\u5d4c\u5957\u5faa\u73af\u4e2d\uff0c\u5916\u5c42\u5faa\u73af\u548c\u5185\u5c42\u5faa\u73af\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n)\\) \uff0c\u56e0\u6b64\u603b\u4f53\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def quadratic(n: int) -> int:\n    \"\"\"\u5e73\u65b9\u9636\"\"\"\n    count = 0\n    # \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n    for i in range(n):\n        for j in range(n):\n            count += 1\n    return count\n
        time_complexity.cpp
        /* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n    int count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n    int count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
        time_complexity.cs
        /* \u5e73\u65b9\u9636 */\nint Quadratic(int n) {\n    int count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
        time_complexity.go
        /* \u5e73\u65b9\u9636 */\nfunc quadratic(n int) int {\n    count := 0\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n    for i := 0; i < n; i++ {\n        for j := 0; j < n; j++ {\n            count++\n        }\n    }\n    return count\n}\n
        time_complexity.swift
        /* \u5e73\u65b9\u9636 */\nfunc quadratic(n: Int) -> Int {\n    var count = 0\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n    for _ in 0 ..< n {\n        for _ in 0 ..< n {\n            count += 1\n        }\n    }\n    return count\n}\n
        time_complexity.js
        /* \u5e73\u65b9\u9636 */\nfunction quadratic(n) {\n    let count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n    for (let i = 0; i < n; i++) {\n        for (let j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
        time_complexity.ts
        /* \u5e73\u65b9\u9636 */\nfunction quadratic(n: number): number {\n    let count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n    for (let i = 0; i < n; i++) {\n        for (let j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
        time_complexity.dart
        /* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n  int count = 0;\n  // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n  for (int i = 0; i < n; i++) {\n    for (int j = 0; j < n; j++) {\n      count++;\n    }\n  }\n  return count;\n}\n
        time_complexity.rs
        /* \u5e73\u65b9\u9636 */\nfn quadratic(n: i32) -> i32 {\n    let mut count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n    for _ in 0..n {\n        for _ in 0..n {\n            count += 1;\n        }\n    }\n    count\n}\n
        time_complexity.c
        /* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n    int count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
        time_complexity.kt
        /* \u5e73\u65b9\u9636 */\nfun quadratic(n: Int): Int {\n    var count = 0\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n    for (i in 0..<n) {\n        for (j in 0..<n) {\n            count++\n        }\n    }\n    return count\n}\n
        time_complexity.rb
        ### \u5e73\u65b9\u9636 ###\ndef quadratic(n)\n  count = 0\n\n  # \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n  for i in 0...n\n    for j in 0...n\n      count += 1\n    end\n  end\n\n  count\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 2-10 \u5bf9\u6bd4\u4e86\u5e38\u6570\u9636\u3001\u7ebf\u6027\u9636\u548c\u5e73\u65b9\u9636\u4e09\u79cd\u65f6\u95f4\u590d\u6742\u5ea6\u3002

        \u56fe 2-10 \u00a0 \u5e38\u6570\u9636\u3001\u7ebf\u6027\u9636\u548c\u5e73\u65b9\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

        \u4ee5\u5192\u6ce1\u6392\u5e8f\u4e3a\u4f8b\uff0c\u5916\u5c42\u5faa\u73af\u6267\u884c \\(n - 1\\) \u6b21\uff0c\u5185\u5c42\u5faa\u73af\u6267\u884c \\(n-1\\)\u3001\\(n-2\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u6b21\uff0c\u5e73\u5747\u4e3a \\(n / 2\\) \u6b21\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O((n - 1) n / 2) = O(n^2)\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def bubble_sort(nums: list[int]) -> int:\n    \"\"\"\u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09\"\"\"\n    count = 0  # \u8ba1\u6570\u5668\n    # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i in range(len(nums) - 1, 0, -1):\n        # \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for j in range(i):\n            if nums[j] > nums[j + 1]:\n                # \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                tmp: int = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = tmp\n                count += 3  # \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n    return count\n
        time_complexity.cpp
        /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(vector<int> &nums) {\n    int count = 0; // \u8ba1\u6570\u5668\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = nums.size() - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n            }\n        }\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int[] nums) {\n    int count = 0; // \u8ba1\u6570\u5668\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = nums.length - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n            }\n        }\n    }\n    return count;\n}\n
        time_complexity.cs
        /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint BubbleSort(int[] nums) {\n    int count = 0;  // \u8ba1\u6570\u5668\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = nums.Length - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n                count += 3;  // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n            }\n        }\n    }\n    return count;\n}\n
        time_complexity.go
        /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunc bubbleSort(nums []int) int {\n    count := 0 // \u8ba1\u6570\u5668\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i := len(nums) - 1; i > 0; i-- {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for j := 0; j < i; j++ {\n            if nums[j] > nums[j+1] {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                tmp := nums[j]\n                nums[j] = nums[j+1]\n                nums[j+1] = tmp\n                count += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n            }\n        }\n    }\n    return count\n}\n
        time_complexity.swift
        /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunc bubbleSort(nums: inout [Int]) -> Int {\n    var count = 0 // \u8ba1\u6570\u5668\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i in nums.indices.dropFirst().reversed() {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for j in 0 ..< i {\n            if nums[j] > nums[j + 1] {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                let tmp = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = tmp\n                count += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n            }\n        }\n    }\n    return count\n}\n
        time_complexity.js
        /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunction bubbleSort(nums) {\n    let count = 0; // \u8ba1\u6570\u5668\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n            }\n        }\n    }\n    return count;\n}\n
        time_complexity.ts
        /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunction bubbleSort(nums: number[]): number {\n    let count = 0; // \u8ba1\u6570\u5668\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n            }\n        }\n    }\n    return count;\n}\n
        time_complexity.dart
        /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(List<int> nums) {\n  int count = 0; // \u8ba1\u6570\u5668\n  // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n  for (var i = nums.length - 1; i > 0; i--) {\n    // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n    for (var j = 0; j < i; j++) {\n      if (nums[j] > nums[j + 1]) {\n        // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n        int tmp = nums[j];\n        nums[j] = nums[j + 1];\n        nums[j + 1] = tmp;\n        count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n      }\n    }\n  }\n  return count;\n}\n
        time_complexity.rs
        /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfn bubble_sort(nums: &mut [i32]) -> i32 {\n    let mut count = 0; // \u8ba1\u6570\u5668\n\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i in (1..nums.len()).rev() {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for j in 0..i {\n            if nums[j] > nums[j + 1] {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n            }\n        }\n    }\n    count\n}\n
        time_complexity.c
        /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int *nums, int n) {\n    int count = 0; // \u8ba1\u6570\u5668\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = n - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n            }\n        }\n    }\n    return count;\n}\n
        time_complexity.kt
        /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfun bubbleSort(nums: IntArray): Int {\n    var count = 0 // \u8ba1\u6570\u5668\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (i in nums.size - 1 downTo 1) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (j in 0..<i) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                val temp = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = temp\n                count += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n            }\n        }\n    }\n    return count\n}\n
        time_complexity.rb
        ### \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09###\ndef bubble_sort(nums)\n  count = 0  # \u8ba1\u6570\u5668\n\n  # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n  for i in (nums.length - 1).downto(0)\n    # \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n    for j in 0...i\n      if nums[j] > nums[j + 1]\n        # \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n        tmp = nums[j]\n        nums[j] = nums[j + 1]\n        nums[j + 1] = tmp\n        count += 3 # \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n      end\n    end\n  end\n\n  count\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_computational_complexity/time_complexity/#4-o2n","title":"4. \u00a0 \u6307\u6570\u9636 \\(O(2^n)\\)","text":"

        \u751f\u7269\u5b66\u7684\u201c\u7ec6\u80de\u5206\u88c2\u201d\u662f\u6307\u6570\u9636\u589e\u957f\u7684\u5178\u578b\u4f8b\u5b50\uff1a\u521d\u59cb\u72b6\u6001\u4e3a \\(1\\) \u4e2a\u7ec6\u80de\uff0c\u5206\u88c2\u4e00\u8f6e\u540e\u53d8\u4e3a \\(2\\) \u4e2a\uff0c\u5206\u88c2\u4e24\u8f6e\u540e\u53d8\u4e3a \\(4\\) \u4e2a\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u5206\u88c2 \\(n\\) \u8f6e\u540e\u6709 \\(2^n\\) \u4e2a\u7ec6\u80de\u3002

        \u56fe 2-11 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6a21\u62df\u4e86\u7ec6\u80de\u5206\u88c2\u7684\u8fc7\u7a0b\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002\u8bf7\u6ce8\u610f\uff0c\u8f93\u5165 \\(n\\) \u8868\u793a\u5206\u88c2\u8f6e\u6570\uff0c\u8fd4\u56de\u503c count \u8868\u793a\u603b\u5206\u88c2\u6b21\u6570\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def exponential(n: int) -> int:\n    \"\"\"\u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\"\"\"\n    count = 0\n    base = 1\n    # \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n    for _ in range(n):\n        for _ in range(base):\n            count += 1\n        base *= 2\n    # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count\n
        time_complexity.cpp
        /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n    int count = 0, base = 1;\n    // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
        time_complexity.java
        /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n    int count = 0, base = 1;\n    // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
        time_complexity.cs
        /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint Exponential(int n) {\n    int count = 0, bas = 1;\n    // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < bas; j++) {\n            count++;\n        }\n        bas *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
        time_complexity.go
        /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09*/\nfunc exponential(n int) int {\n    count, base := 0, 1\n    // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n    for i := 0; i < n; i++ {\n        for j := 0; j < base; j++ {\n            count++\n        }\n        base *= 2\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count\n}\n
        time_complexity.swift
        /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunc exponential(n: Int) -> Int {\n    var count = 0\n    var base = 1\n    // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n    for _ in 0 ..< n {\n        for _ in 0 ..< base {\n            count += 1\n        }\n        base *= 2\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count\n}\n
        time_complexity.js
        /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction exponential(n) {\n    let count = 0,\n        base = 1;\n    // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n    for (let i = 0; i < n; i++) {\n        for (let j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
        time_complexity.ts
        /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction exponential(n: number): number {\n    let count = 0,\n        base = 1;\n    // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n    for (let i = 0; i < n; i++) {\n        for (let j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
        time_complexity.dart
        /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n  int count = 0, base = 1;\n  // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n  for (var i = 0; i < n; i++) {\n    for (var j = 0; j < base; j++) {\n      count++;\n    }\n    base *= 2;\n  }\n  // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n  return count;\n}\n
        time_complexity.rs
        /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfn exponential(n: i32) -> i32 {\n    let mut count = 0;\n    let mut base = 1;\n    // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n    for _ in 0..n {\n        for _ in 0..base {\n            count += 1\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    count\n}\n
        time_complexity.c
        /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n    int count = 0;\n    int bas = 1;\n    // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < bas; j++) {\n            count++;\n        }\n        bas *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
        time_complexity.kt
        /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfun exponential(n: Int): Int {\n    var count = 0\n    var base = 1\n    // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n    for (i in 0..<n) {\n        for (j in 0..<base) {\n            count++\n        }\n        base *= 2\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count\n}\n
        time_complexity.rb
        ### \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09###\ndef exponential(n)\n  count, base = 0, 1\n\n  # \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n  (0...n).each do\n    (0...base).each { count += 1 }\n    base *= 2\n  end\n\n  # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n  count\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 2-11 \u00a0 \u6307\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

        \u5728\u5b9e\u9645\u7b97\u6cd5\u4e2d\uff0c\u6307\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u9012\u5f52\u51fd\u6570\u4e2d\u3002\u4f8b\u5982\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u5176\u9012\u5f52\u5730\u4e00\u5206\u4e3a\u4e8c\uff0c\u7ecf\u8fc7 \\(n\\) \u6b21\u5206\u88c2\u540e\u505c\u6b62\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def exp_recur(n: int) -> int:\n    \"\"\"\u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n    if n == 1:\n        return 1\n    return exp_recur(n - 1) + exp_recur(n - 1) + 1\n
        time_complexity.cpp
        /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n    if (n == 1)\n        return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
        time_complexity.java
        /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n    if (n == 1)\n        return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
        time_complexity.cs
        /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint ExpRecur(int n) {\n    if (n == 1) return 1;\n    return ExpRecur(n - 1) + ExpRecur(n - 1) + 1;\n}\n
        time_complexity.go
        /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09*/\nfunc expRecur(n int) int {\n    if n == 1 {\n        return 1\n    }\n    return expRecur(n-1) + expRecur(n-1) + 1\n}\n
        time_complexity.swift
        /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc expRecur(n: Int) -> Int {\n    if n == 1 {\n        return 1\n    }\n    return expRecur(n: n - 1) + expRecur(n: n - 1) + 1\n}\n
        time_complexity.js
        /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction expRecur(n) {\n    if (n === 1) return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
        time_complexity.ts
        /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction expRecur(n: number): number {\n    if (n === 1) return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
        time_complexity.dart
        /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n  if (n == 1) return 1;\n  return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
        time_complexity.rs
        /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn exp_recur(n: i32) -> i32 {\n    if n == 1 {\n        return 1;\n    }\n    exp_recur(n - 1) + exp_recur(n - 1) + 1\n}\n
        time_complexity.c
        /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n    if (n == 1)\n        return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
        time_complexity.kt
        /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfun expRecur(n: Int): Int {\n    if (n == 1) {\n        return 1\n    }\n    return expRecur(n - 1) + expRecur(n - 1) + 1\n}\n
        time_complexity.rb
        ### \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09###\ndef exp_recur(n)\n  return 1 if n == 1\n  exp_recur(n - 1) + exp_recur(n - 1) + 1\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u6307\u6570\u9636\u589e\u957f\u975e\u5e38\u8fc5\u901f\uff0c\u5728\u7a77\u4e3e\u6cd5\uff08\u66b4\u529b\u641c\u7d22\u3001\u56de\u6eaf\u7b49\uff09\u4e2d\u6bd4\u8f83\u5e38\u89c1\u3002\u5bf9\u4e8e\u6570\u636e\u89c4\u6a21\u8f83\u5927\u7684\u95ee\u9898\uff0c\u6307\u6570\u9636\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u6216\u8d2a\u5fc3\u7b97\u6cd5\u7b49\u6765\u89e3\u51b3\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#5-olog-n","title":"5. \u00a0 \u5bf9\u6570\u9636 \\(O(\\log n)\\)","text":"

        \u4e0e\u6307\u6570\u9636\u76f8\u53cd\uff0c\u5bf9\u6570\u9636\u53cd\u6620\u4e86\u201c\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\u201d\u7684\u60c5\u51b5\u3002\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u7531\u4e8e\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\uff0c\u56e0\u6b64\u5faa\u73af\u6b21\u6570\u662f \\(\\log_2 n\\) \uff0c\u5373 \\(2^n\\) \u7684\u53cd\u51fd\u6570\u3002

        \u56fe 2-12 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6a21\u62df\u4e86\u201c\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\u201d\u7684\u8fc7\u7a0b\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log_2 n)\\) \uff0c\u7b80\u8bb0\u4e3a \\(O(\\log n)\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def logarithmic(n: int) -> int:\n    \"\"\"\u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\"\"\"\n    count = 0\n    while n > 1:\n        n = n / 2\n        count += 1\n    return count\n
        time_complexity.cpp
        /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
        time_complexity.cs
        /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint Logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n /= 2;\n        count++;\n    }\n    return count;\n}\n
        time_complexity.go
        /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09*/\nfunc logarithmic(n int) int {\n    count := 0\n    for n > 1 {\n        n = n / 2\n        count++\n    }\n    return count\n}\n
        time_complexity.swift
        /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunc logarithmic(n: Int) -> Int {\n    var count = 0\n    var n = n\n    while n > 1 {\n        n = n / 2\n        count += 1\n    }\n    return count\n}\n
        time_complexity.js
        /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction logarithmic(n) {\n    let count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
        time_complexity.ts
        /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction logarithmic(n: number): number {\n    let count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
        time_complexity.dart
        /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(int n) {\n  int count = 0;\n  while (n > 1) {\n    n = n ~/ 2;\n    count++;\n  }\n  return count;\n}\n
        time_complexity.rs
        /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfn logarithmic(mut n: i32) -> i32 {\n    let mut count = 0;\n    while n > 1 {\n        n = n / 2;\n        count += 1;\n    }\n    count\n}\n
        time_complexity.c
        /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
        time_complexity.kt
        /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfun logarithmic(n: Int): Int {\n    var n1 = n\n    var count = 0\n    while (n1 > 1) {\n        n1 /= 2\n        count++\n    }\n    return count\n}\n
        time_complexity.rb
        ### \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09###\ndef logarithmic(n)\n  count = 0\n\n  while n > 1\n    n /= 2\n    count += 1\n  end\n\n  count\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 2-12 \u00a0 \u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

        \u4e0e\u6307\u6570\u9636\u7c7b\u4f3c\uff0c\u5bf9\u6570\u9636\u4e5f\u5e38\u51fa\u73b0\u4e8e\u9012\u5f52\u51fd\u6570\u4e2d\u3002\u4ee5\u4e0b\u4ee3\u7801\u5f62\u6210\u4e86\u4e00\u68f5\u9ad8\u5ea6\u4e3a \\(\\log_2 n\\) \u7684\u9012\u5f52\u6811\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def log_recur(n: int) -> int:\n    \"\"\"\u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n    if n <= 1:\n        return 0\n    return log_recur(n / 2) + 1\n
        time_complexity.cpp
        /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(int n) {\n    if (n <= 1)\n        return 0;\n    return logRecur(n / 2) + 1;\n}\n
        time_complexity.java
        /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(int n) {\n    if (n <= 1)\n        return 0;\n    return logRecur(n / 2) + 1;\n}\n
        time_complexity.cs
        /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint LogRecur(int n) {\n    if (n <= 1) return 0;\n    return LogRecur(n / 2) + 1;\n}\n
        time_complexity.go
        /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09*/\nfunc logRecur(n int) int {\n    if n <= 1 {\n        return 0\n    }\n    return logRecur(n/2) + 1\n}\n
        time_complexity.swift
        /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc logRecur(n: Int) -> Int {\n    if n <= 1 {\n        return 0\n    }\n    return logRecur(n: n / 2) + 1\n}\n
        time_complexity.js
        /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction logRecur(n) {\n    if (n <= 1) return 0;\n    return logRecur(n / 2) + 1;\n}\n
        time_complexity.ts
        /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction logRecur(n: number): number {\n    if (n <= 1) return 0;\n    return logRecur(n / 2) + 1;\n}\n
        time_complexity.dart
        /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(int n) {\n  if (n <= 1) return 0;\n  return logRecur(n ~/ 2) + 1;\n}\n
        time_complexity.rs
        /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn log_recur(n: i32) -> i32 {\n    if n <= 1 {\n        return 0;\n    }\n    log_recur(n / 2) + 1\n}\n
        time_complexity.c
        /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(int n) {\n    if (n <= 1)\n        return 0;\n    return logRecur(n / 2) + 1;\n}\n
        time_complexity.kt
        /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfun logRecur(n: Int): Int {\n    if (n <= 1)\n        return 0\n    return logRecur(n / 2) + 1\n}\n
        time_complexity.rb
        ### \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09###\ndef log_recur(n)\n  return 0 unless n > 1\n  log_recur(n / 2) + 1\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5bf9\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u7b97\u6cd5\u4e2d\uff0c\u4f53\u73b0\u4e86\u201c\u4e00\u5206\u4e3a\u591a\u201d\u548c\u201c\u5316\u7e41\u4e3a\u7b80\u201d\u7684\u7b97\u6cd5\u601d\u60f3\u3002\u5b83\u589e\u957f\u7f13\u6162\uff0c\u662f\u4ec5\u6b21\u4e8e\u5e38\u6570\u9636\u7684\u7406\u60f3\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002

        \\(O(\\log n)\\) \u7684\u5e95\u6570\u662f\u591a\u5c11\uff1f

        \u51c6\u786e\u6765\u8bf4\uff0c\u201c\u4e00\u5206\u4e3a \\(m\\)\u201d\u5bf9\u5e94\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(\\log_m n)\\) \u3002\u800c\u901a\u8fc7\u5bf9\u6570\u6362\u5e95\u516c\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u5177\u6709\u4e0d\u540c\u5e95\u6570\u3001\u76f8\u7b49\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a

        \\[ O(\\log_m n) = O(\\log_k n / \\log_k m) = O(\\log_k n) \\]

        \u4e5f\u5c31\u662f\u8bf4\uff0c\u5e95\u6570 \\(m\\) \u53ef\u4ee5\u5728\u4e0d\u5f71\u54cd\u590d\u6742\u5ea6\u7684\u524d\u63d0\u4e0b\u8f6c\u6362\u3002\u56e0\u6b64\u6211\u4eec\u901a\u5e38\u4f1a\u7701\u7565\u5e95\u6570 \\(m\\) \uff0c\u5c06\u5bf9\u6570\u9636\u76f4\u63a5\u8bb0\u4e3a \\(O(\\log n)\\) \u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#6-on-log-n","title":"6. \u00a0 \u7ebf\u6027\u5bf9\u6570\u9636 \\(O(n \\log n)\\)","text":"

        \u7ebf\u6027\u5bf9\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u5d4c\u5957\u5faa\u73af\u4e2d\uff0c\u4e24\u5c42\u5faa\u73af\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u522b\u4e3a \\(O(\\log n)\\) \u548c \\(O(n)\\) \u3002\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def linear_log_recur(n: int) -> int:\n    \"\"\"\u7ebf\u6027\u5bf9\u6570\u9636\"\"\"\n    if n <= 1:\n        return 1\n    # \u4e00\u5206\u4e3a\u4e8c\uff0c\u5b50\u95ee\u9898\u7684\u89c4\u6a21\u51cf\u5c0f\u4e00\u534a\n    count = linear_log_recur(n // 2) + linear_log_recur(n // 2)\n    # \u5f53\u524d\u5b50\u95ee\u9898\u5305\u542b n \u4e2a\u64cd\u4f5c\n    for _ in range(n):\n        count += 1\n    return count\n
        time_complexity.cpp
        /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(int n) {\n    if (n <= 1)\n        return 1;\n    int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(int n) {\n    if (n <= 1)\n        return 1;\n    int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.cs
        /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint LinearLogRecur(int n) {\n    if (n <= 1) return 1;\n    int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.go
        /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunc linearLogRecur(n int) int {\n    if n <= 1 {\n        return 1\n    }\n    count := linearLogRecur(n/2) + linearLogRecur(n/2)\n    for i := 0; i < n; i++ {\n        count++\n    }\n    return count\n}\n
        time_complexity.swift
        /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunc linearLogRecur(n: Int) -> Int {\n    if n <= 1 {\n        return 1\n    }\n    var count = linearLogRecur(n: n / 2) + linearLogRecur(n: n / 2)\n    for _ in stride(from: 0, to: n, by: 1) {\n        count += 1\n    }\n    return count\n}\n
        time_complexity.js
        /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunction linearLogRecur(n) {\n    if (n <= 1) return 1;\n    let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (let i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.ts
        /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunction linearLogRecur(n: number): number {\n    if (n <= 1) return 1;\n    let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (let i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.dart
        /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(int n) {\n  if (n <= 1) return 1;\n  int count = linearLogRecur(n ~/ 2) + linearLogRecur(n ~/ 2);\n  for (var i = 0; i < n; i++) {\n    count++;\n  }\n  return count;\n}\n
        time_complexity.rs
        /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfn linear_log_recur(n: i32) -> i32 {\n    if n <= 1 {\n        return 1;\n    }\n    let mut count = linear_log_recur(n / 2) + linear_log_recur(n / 2);\n    for _ in 0..n {\n        count += 1;\n    }\n    return count;\n}\n
        time_complexity.c
        /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(int n) {\n    if (n <= 1)\n        return 1;\n    int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.kt
        /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfun linearLogRecur(n: Int): Int {\n    if (n <= 1)\n        return 1\n    var count = linearLogRecur(n / 2) + linearLogRecur(n / 2)\n    for (i in 0..<n) {\n        count++\n    }\n    return count\n}\n
        time_complexity.rb
        ### \u7ebf\u6027\u5bf9\u6570\u9636 ###\ndef linear_log_recur(n)\n  return 1 unless n > 1\n\n  count = linear_log_recur(n / 2) + linear_log_recur(n / 2)\n  (0...n).each { count += 1 }\n\n  count\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 2-13 \u5c55\u793a\u4e86\u7ebf\u6027\u5bf9\u6570\u9636\u7684\u751f\u6210\u65b9\u5f0f\u3002\u4e8c\u53c9\u6811\u7684\u6bcf\u4e00\u5c42\u7684\u64cd\u4f5c\u603b\u6570\u90fd\u4e3a \\(n\\) \uff0c\u6811\u5171\u6709 \\(\\log_2 n + 1\\) \u5c42\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002

        \u56fe 2-13 \u00a0 \u7ebf\u6027\u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

        \u4e3b\u6d41\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3a \\(O(n \\log n)\\) \uff0c\u4f8b\u5982\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u3001\u5806\u6392\u5e8f\u7b49\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#7-on","title":"7. \u00a0 \u9636\u4e58\u9636 \\(O(n!)\\)","text":"

        \u9636\u4e58\u9636\u5bf9\u5e94\u6570\u5b66\u4e0a\u7684\u201c\u5168\u6392\u5217\u201d\u95ee\u9898\u3002\u7ed9\u5b9a \\(n\\) \u4e2a\u4e92\u4e0d\u91cd\u590d\u7684\u5143\u7d20\uff0c\u6c42\u5176\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u65b9\u6848\uff0c\u65b9\u6848\u6570\u91cf\u4e3a\uff1a

        \\[ n! = n \\times (n - 1) \\times (n - 2) \\times \\dots \\times 2 \\times 1 \\]

        \u9636\u4e58\u901a\u5e38\u4f7f\u7528\u9012\u5f52\u5b9e\u73b0\u3002\u5982\u56fe 2-14 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u7b2c\u4e00\u5c42\u5206\u88c2\u51fa \\(n\\) \u4e2a\uff0c\u7b2c\u4e8c\u5c42\u5206\u88c2\u51fa \\(n - 1\\) \u4e2a\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u7b2c \\(n\\) \u5c42\u65f6\u505c\u6b62\u5206\u88c2\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def factorial_recur(n: int) -> int:\n    \"\"\"\u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n    if n == 0:\n        return 1\n    count = 0\n    # \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n    for _ in range(n):\n        count += factorial_recur(n - 1)\n    return count\n
        time_complexity.cpp
        /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n    if (n == 0)\n        return 1;\n    int count = 0;\n    // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n    for (int i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n    if (n == 0)\n        return 1;\n    int count = 0;\n    // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n    for (int i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
        time_complexity.cs
        /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint FactorialRecur(int n) {\n    if (n == 0) return 1;\n    int count = 0;\n    // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n    for (int i = 0; i < n; i++) {\n        count += FactorialRecur(n - 1);\n    }\n    return count;\n}\n
        time_complexity.go
        /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc factorialRecur(n int) int {\n    if n == 0 {\n        return 1\n    }\n    count := 0\n    // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n    for i := 0; i < n; i++ {\n        count += factorialRecur(n - 1)\n    }\n    return count\n}\n
        time_complexity.swift
        /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc factorialRecur(n: Int) -> Int {\n    if n == 0 {\n        return 1\n    }\n    var count = 0\n    // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n    for _ in 0 ..< n {\n        count += factorialRecur(n: n - 1)\n    }\n    return count\n}\n
        time_complexity.js
        /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction factorialRecur(n) {\n    if (n === 0) return 1;\n    let count = 0;\n    // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n    for (let i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
        time_complexity.ts
        /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction factorialRecur(n: number): number {\n    if (n === 0) return 1;\n    let count = 0;\n    // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n    for (let i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
        time_complexity.dart
        /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n  if (n == 0) return 1;\n  int count = 0;\n  // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n  for (var i = 0; i < n; i++) {\n    count += factorialRecur(n - 1);\n  }\n  return count;\n}\n
        time_complexity.rs
        /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn factorial_recur(n: i32) -> i32 {\n    if n == 0 {\n        return 1;\n    }\n    let mut count = 0;\n    // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n    for _ in 0..n {\n        count += factorial_recur(n - 1);\n    }\n    count\n}\n
        time_complexity.c
        /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n    if (n == 0)\n        return 1;\n    int count = 0;\n    for (int i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
        time_complexity.kt
        /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfun factorialRecur(n: Int): Int {\n    if (n == 0)\n        return 1\n    var count = 0\n    // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n    for (i in 0..<n) {\n        count += factorialRecur(n - 1)\n    }\n    return count\n}\n
        time_complexity.rb
        ### \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09###\ndef factorial_recur(n)\n  return 1 if n == 0\n\n  count = 0\n  # \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n  (0...n).each { count += factorial_recur(n - 1) }\n\n  count\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 2-14 \u00a0 \u9636\u4e58\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

        \u8bf7\u6ce8\u610f\uff0c\u56e0\u4e3a\u5f53 \\(n \\geq 4\\) \u65f6\u6052\u6709 \\(n! > 2^n\\) \uff0c\u6240\u4ee5\u9636\u4e58\u9636\u6bd4\u6307\u6570\u9636\u589e\u957f\u5f97\u66f4\u5feb\uff0c\u5728 \\(n\\) \u8f83\u5927\u65f6\u4e5f\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#235","title":"2.3.5 \u00a0 \u6700\u5dee\u3001\u6700\u4f73\u3001\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6","text":"

        \u7b97\u6cd5\u7684\u65f6\u95f4\u6548\u7387\u5f80\u5f80\u4e0d\u662f\u56fa\u5b9a\u7684\uff0c\u800c\u662f\u4e0e\u8f93\u5165\u6570\u636e\u7684\u5206\u5e03\u6709\u5173\u3002\u5047\u8bbe\u8f93\u5165\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4 nums \uff0c\u5176\u4e2d nums \u7531\u4ece \\(1\\) \u81f3 \\(n\\) \u7684\u6570\u5b57\u7ec4\u6210\uff0c\u6bcf\u4e2a\u6570\u5b57\u53ea\u51fa\u73b0\u4e00\u6b21\uff1b\u4f46\u5143\u7d20\u987a\u5e8f\u662f\u968f\u673a\u6253\u4e71\u7684\uff0c\u4efb\u52a1\u76ee\u6807\u662f\u8fd4\u56de\u5143\u7d20 \\(1\\) \u7684\u7d22\u5f15\u3002\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\u3002

        • \u5f53 nums = [?, ?, ..., 1] \uff0c\u5373\u5f53\u672b\u5c3e\u5143\u7d20\u662f \\(1\\) \u65f6\uff0c\u9700\u8981\u5b8c\u6574\u904d\u5386\u6570\u7ec4\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3002
        • \u5f53 nums = [1, ?, ?, ...] \uff0c\u5373\u5f53\u9996\u4e2a\u5143\u7d20\u4e3a \\(1\\) \u65f6\uff0c\u65e0\u8bba\u6570\u7ec4\u591a\u957f\u90fd\u4e0d\u9700\u8981\u7ee7\u7eed\u904d\u5386\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 \\(\\Omega(1)\\) \u3002

        \u201c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u201d\u5bf9\u5e94\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c\uff0c\u4f7f\u7528\u5927 \\(O\\) \u8bb0\u53f7\u8868\u793a\u3002\u76f8\u5e94\u5730\uff0c\u201c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u201d\u5bf9\u5e94\u51fd\u6570\u6e10\u8fd1\u4e0b\u754c\uff0c\u7528 \\(\\Omega\\) \u8bb0\u53f7\u8868\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby worst_best_time_complexity.py
        def random_numbers(n: int) -> list[int]:\n    \"\"\"\u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a: 1, 2, ..., n \uff0c\u987a\u5e8f\u88ab\u6253\u4e71\"\"\"\n    # \u751f\u6210\u6570\u7ec4 nums =: 1, 2, 3, ..., n\n    nums = [i for i in range(1, n + 1)]\n    # \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n    random.shuffle(nums)\n    return nums\n\ndef find_one(nums: list[int]) -> int:\n    \"\"\"\u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15\"\"\"\n    for i in range(len(nums)):\n        # \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n        # \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n        if nums[i] == 1:\n            return i\n    return -1\n
        worst_best_time_complexity.cpp
        /* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nvector<int> randomNumbers(int n) {\n    vector<int> nums(n);\n    // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // \u4f7f\u7528\u7cfb\u7edf\u65f6\u95f4\u751f\u6210\u968f\u673a\u79cd\u5b50\n    unsigned seed = chrono::system_clock::now().time_since_epoch().count();\n    // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n    shuffle(nums.begin(), nums.end(), default_random_engine(seed));\n    return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(vector<int> &nums) {\n    for (int i = 0; i < nums.size(); i++) {\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
        worst_best_time_complexity.java
        /* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint[] randomNumbers(int n) {\n    Integer[] nums = new Integer[n];\n    // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n    Collections.shuffle(Arrays.asList(nums));\n    // Integer[] -> int[]\n    int[] res = new int[n];\n    for (int i = 0; i < n; i++) {\n        res[i] = nums[i];\n    }\n    return res;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int[] nums) {\n    for (int i = 0; i < nums.length; i++) {\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
        worst_best_time_complexity.cs
        /* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint[] RandomNumbers(int n) {\n    int[] nums = new int[n];\n    // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n\n    // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n    for (int i = 0; i < nums.Length; i++) {\n        int index = new Random().Next(i, nums.Length);\n        (nums[i], nums[index]) = (nums[index], nums[i]);\n    }\n    return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint FindOne(int[] nums) {\n    for (int i = 0; i < nums.Length; i++) {\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
        worst_best_time_complexity.go
        /* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunc randomNumbers(n int) []int {\n    nums := make([]int, n)\n    // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n    for i := 0; i < n; i++ {\n        nums[i] = i + 1\n    }\n    // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n    rand.Shuffle(len(nums), func(i, j int) {\n        nums[i], nums[j] = nums[j], nums[i]\n    })\n    return nums\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunc findOne(nums []int) int {\n    for i := 0; i < len(nums); i++ {\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n        if nums[i] == 1 {\n            return i\n        }\n    }\n    return -1\n}\n
        worst_best_time_complexity.swift
        /* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunc randomNumbers(n: Int) -> [Int] {\n    // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n    var nums = Array(1 ... n)\n    // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n    nums.shuffle()\n    return nums\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunc findOne(nums: [Int]) -> Int {\n    for i in nums.indices {\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n        if nums[i] == 1 {\n            return i\n        }\n    }\n    return -1\n}\n
        worst_best_time_complexity.js
        /* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunction randomNumbers(n) {\n    const nums = Array(n);\n    // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n    for (let i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n    for (let i = 0; i < n; i++) {\n        const r = Math.floor(Math.random() * (i + 1));\n        const temp = nums[i];\n        nums[i] = nums[r];\n        nums[r] = temp;\n    }\n    return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunction findOne(nums) {\n    for (let i = 0; i < nums.length; i++) {\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n        if (nums[i] === 1) {\n            return i;\n        }\n    }\n    return -1;\n}\n
        worst_best_time_complexity.ts
        /* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunction randomNumbers(n: number): number[] {\n    const nums = Array(n);\n    // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n    for (let i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n    for (let i = 0; i < n; i++) {\n        const r = Math.floor(Math.random() * (i + 1));\n        const temp = nums[i];\n        nums[i] = nums[r];\n        nums[r] = temp;\n    }\n    return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunction findOne(nums: number[]): number {\n    for (let i = 0; i < nums.length; i++) {\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n        if (nums[i] === 1) {\n            return i;\n        }\n    }\n    return -1;\n}\n
        worst_best_time_complexity.dart
        /* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nList<int> randomNumbers(int n) {\n  final nums = List.filled(n, 0);\n  // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n  for (var i = 0; i < n; i++) {\n    nums[i] = i + 1;\n  }\n  // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n  nums.shuffle();\n\n  return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(List<int> nums) {\n  for (var i = 0; i < nums.length; i++) {\n    // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n    // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n    if (nums[i] == 1) return i;\n  }\n\n  return -1;\n}\n
        worst_best_time_complexity.rs
        /* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfn random_numbers(n: i32) -> Vec<i32> {\n    // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n    let mut nums = (1..=n).collect::<Vec<i32>>();\n    // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n    nums.shuffle(&mut thread_rng());\n    nums\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfn find_one(nums: &[i32]) -> Option<usize> {\n    for i in 0..nums.len() {\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n        if nums[i] == 1 {\n            return Some(i);\n        }\n    }\n    None\n}\n
        worst_best_time_complexity.c
        /* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint *randomNumbers(int n) {\n    // \u5206\u914d\u5806\u533a\u5185\u5b58\uff08\u521b\u5efa\u4e00\u7ef4\u53ef\u53d8\u957f\u6570\u7ec4\uff1a\u6570\u7ec4\u4e2d\u5143\u7d20\u6570\u91cf\u4e3a n \uff0c\u5143\u7d20\u7c7b\u578b\u4e3a int \uff09\n    int *nums = (int *)malloc(n * sizeof(int));\n    // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n    for (int i = n - 1; i > 0; i--) {\n        int j = rand() % (i + 1);\n        int temp = nums[i];\n        nums[i] = nums[j];\n        nums[j] = temp;\n    }\n    return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int *nums, int n) {\n    for (int i = 0; i < n; i++) {\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
        worst_best_time_complexity.kt
        /* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfun randomNumbers(n: Int): Array<Int?> {\n    val nums = IntArray(n)\n    // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n    for (i in 0..<n) {\n        nums[i] = i + 1\n    }\n    // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n    nums.shuffle()\n    val res = arrayOfNulls<Int>(n)\n    for (i in 0..<n) {\n        res[i] = nums[i]\n    }\n    return res\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfun findOne(nums: Array<Int?>): Int {\n    for (i in nums.indices) {\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n        if (nums[i] == 1)\n            return i\n    }\n    return -1\n}\n
        worst_best_time_complexity.rb
        ### \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a: 1, 2, ..., n \uff0c\u987a\u5e8f\u88ab\u6253\u4e71 ###\ndef random_numbers(n)\n  # \u751f\u6210\u6570\u7ec4 nums =: 1, 2, 3, ..., n\n  nums = Array.new(n) { |i| i + 1 }\n  # \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n  nums.shuffle!\nend\n\n### \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 ###\ndef find_one(nums)\n  for i in 0...nums.length\n    # \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n    # \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n    return i if nums[i] == 1\n  end\n\n  -1\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6211\u4eec\u5728\u5b9e\u9645\u4e2d\u5f88\u5c11\u4f7f\u7528\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u56e0\u4e3a\u901a\u5e38\u53ea\u6709\u5728\u5f88\u5c0f\u6982\u7387\u4e0b\u624d\u80fd\u8fbe\u5230\uff0c\u53ef\u80fd\u4f1a\u5e26\u6765\u4e00\u5b9a\u7684\u8bef\u5bfc\u6027\u3002\u800c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u66f4\u4e3a\u5b9e\u7528\uff0c\u56e0\u4e3a\u5b83\u7ed9\u51fa\u4e86\u4e00\u4e2a\u6548\u7387\u5b89\u5168\u503c\uff0c\u8ba9\u6211\u4eec\u53ef\u4ee5\u653e\u5fc3\u5730\u4f7f\u7528\u7b97\u6cd5\u3002

        \u4ece\u4e0a\u8ff0\u793a\u4f8b\u53ef\u4ee5\u770b\u51fa\uff0c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u53ea\u51fa\u73b0\u4e8e\u201c\u7279\u6b8a\u7684\u6570\u636e\u5206\u5e03\u201d\uff0c\u8fd9\u4e9b\u60c5\u51b5\u7684\u51fa\u73b0\u6982\u7387\u53ef\u80fd\u5f88\u5c0f\uff0c\u5e76\u4e0d\u80fd\u771f\u5b9e\u5730\u53cd\u6620\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f53\u73b0\u7b97\u6cd5\u5728\u968f\u673a\u8f93\u5165\u6570\u636e\u4e0b\u7684\u8fd0\u884c\u6548\u7387\uff0c\u7528 \\(\\Theta\\) \u8bb0\u53f7\u6765\u8868\u793a\u3002

        \u5bf9\u4e8e\u90e8\u5206\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u63a8\u7b97\u51fa\u968f\u673a\u6570\u636e\u5206\u5e03\u4e0b\u7684\u5e73\u5747\u60c5\u51b5\u3002\u6bd4\u5982\u4e0a\u8ff0\u793a\u4f8b\uff0c\u7531\u4e8e\u8f93\u5165\u6570\u7ec4\u662f\u88ab\u6253\u4e71\u7684\uff0c\u56e0\u6b64\u5143\u7d20 \\(1\\) \u51fa\u73b0\u5728\u4efb\u610f\u7d22\u5f15\u7684\u6982\u7387\u90fd\u662f\u76f8\u7b49\u7684\uff0c\u90a3\u4e48\u7b97\u6cd5\u7684\u5e73\u5747\u5faa\u73af\u6b21\u6570\u5c31\u662f\u6570\u7ec4\u957f\u5ea6\u7684\u4e00\u534a \\(n / 2\\) \uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(\\Theta(n / 2) = \\Theta(n)\\) \u3002

        \u4f46\u5bf9\u4e8e\u8f83\u4e3a\u590d\u6742\u7684\u7b97\u6cd5\uff0c\u8ba1\u7b97\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u5f80\u5f80\u6bd4\u8f83\u56f0\u96be\uff0c\u56e0\u4e3a\u5f88\u96be\u5206\u6790\u51fa\u5728\u6570\u636e\u5206\u5e03\u4e0b\u7684\u6574\u4f53\u6570\u5b66\u671f\u671b\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4f5c\u4e3a\u7b97\u6cd5\u6548\u7387\u7684\u8bc4\u5224\u6807\u51c6\u3002

        \u4e3a\u4ec0\u4e48\u5f88\u5c11\u770b\u5230 \\(\\Theta\\) \u7b26\u53f7\uff1f

        \u53ef\u80fd\u7531\u4e8e \\(O\\) \u7b26\u53f7\u8fc7\u4e8e\u6717\u6717\u4e0a\u53e3\uff0c\u56e0\u6b64\u6211\u4eec\u5e38\u5e38\u4f7f\u7528\u5b83\u6765\u8868\u793a\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u4f46\u4ece\u4e25\u683c\u610f\u4e49\u4e0a\u8bb2\uff0c\u8fd9\u79cd\u505a\u6cd5\u5e76\u4e0d\u89c4\u8303\u3002\u5728\u672c\u4e66\u548c\u5176\u4ed6\u8d44\u6599\u4e2d\uff0c\u82e5\u9047\u5230\u7c7b\u4f3c\u201c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\)\u201d\u7684\u8868\u8ff0\uff0c\u8bf7\u5c06\u5176\u76f4\u63a5\u7406\u89e3\u4e3a \\(\\Theta(n)\\) \u3002

        "},{"location":"chapter_data_structure/","title":"\u7b2c 3 \u7ae0 \u00a0 \u6570\u636e\u7ed3\u6784","text":"

        Abstract

        \u6570\u636e\u7ed3\u6784\u5982\u540c\u4e00\u526f\u7a33\u56fa\u800c\u591a\u6837\u7684\u6846\u67b6\u3002

        \u5b83\u4e3a\u6570\u636e\u7684\u6709\u5e8f\u7ec4\u7ec7\u63d0\u4f9b\u4e86\u84dd\u56fe\uff0c\u7b97\u6cd5\u5f97\u4ee5\u5728\u6b64\u57fa\u7840\u4e0a\u751f\u52a8\u8d77\u6765\u3002

        "},{"location":"chapter_data_structure/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 3.1 \u00a0 \u6570\u636e\u7ed3\u6784\u5206\u7c7b
        • 3.2 \u00a0 \u57fa\u672c\u6570\u636e\u7c7b\u578b
        • 3.3 \u00a0 \u6570\u5b57\u7f16\u7801 *
        • 3.4 \u00a0 \u5b57\u7b26\u7f16\u7801 *
        • 3.5 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_data_structure/basic_data_types/","title":"3.2 \u00a0 \u57fa\u672c\u6570\u636e\u7c7b\u578b","text":"

        \u5f53\u8c08\u53ca\u8ba1\u7b97\u673a\u4e2d\u7684\u6570\u636e\u65f6\uff0c\u6211\u4eec\u4f1a\u60f3\u5230\u6587\u672c\u3001\u56fe\u7247\u3001\u89c6\u9891\u3001\u8bed\u97f3\u30013D \u6a21\u578b\u7b49\u5404\u79cd\u5f62\u5f0f\u3002\u5c3d\u7ba1\u8fd9\u4e9b\u6570\u636e\u7684\u7ec4\u7ec7\u5f62\u5f0f\u5404\u5f02\uff0c\u4f46\u5b83\u4eec\u90fd\u7531\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6784\u6210\u3002

        \u57fa\u672c\u6570\u636e\u7c7b\u578b\u662f CPU \u53ef\u4ee5\u76f4\u63a5\u8fdb\u884c\u8fd0\u7b97\u7684\u7c7b\u578b\uff0c\u5728\u7b97\u6cd5\u4e2d\u76f4\u63a5\u88ab\u4f7f\u7528\uff0c\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\u3002

        • \u6574\u6570\u7c7b\u578b byte\u3001short\u3001int\u3001long \u3002
        • \u6d6e\u70b9\u6570\u7c7b\u578b float\u3001double \uff0c\u7528\u4e8e\u8868\u793a\u5c0f\u6570\u3002
        • \u5b57\u7b26\u7c7b\u578b char \uff0c\u7528\u4e8e\u8868\u793a\u5404\u79cd\u8bed\u8a00\u7684\u5b57\u6bcd\u3001\u6807\u70b9\u7b26\u53f7\u751a\u81f3\u8868\u60c5\u7b26\u53f7\u7b49\u3002
        • \u5e03\u5c14\u7c7b\u578b bool \uff0c\u7528\u4e8e\u8868\u793a\u201c\u662f\u201d\u4e0e\u201c\u5426\u201d\u5224\u65ad\u3002

        \u57fa\u672c\u6570\u636e\u7c7b\u578b\u4ee5\u4e8c\u8fdb\u5236\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u4e2d\u3002\u4e00\u4e2a\u4e8c\u8fdb\u5236\u4f4d\u5373\u4e3a \\(1\\) \u6bd4\u7279\u3002\u5728\u7edd\u5927\u591a\u6570\u73b0\u4ee3\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\\(1\\) \u5b57\u8282\uff08byte\uff09\u7531 \\(8\\) \u6bd4\u7279\uff08bit\uff09\u7ec4\u6210\u3002

        \u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u53d6\u503c\u8303\u56f4\u53d6\u51b3\u4e8e\u5176\u5360\u7528\u7684\u7a7a\u95f4\u5927\u5c0f\u3002\u4e0b\u9762\u4ee5 Java \u4e3a\u4f8b\u3002

        • \u6574\u6570\u7c7b\u578b byte \u5360\u7528 \\(1\\) \u5b57\u8282 = \\(8\\) \u6bd4\u7279 \uff0c\u53ef\u4ee5\u8868\u793a \\(2^{8}\\) \u4e2a\u6570\u5b57\u3002
        • \u6574\u6570\u7c7b\u578b int \u5360\u7528 \\(4\\) \u5b57\u8282 = \\(32\\) \u6bd4\u7279 \uff0c\u53ef\u4ee5\u8868\u793a \\(2^{32}\\) \u4e2a\u6570\u5b57\u3002

        \u8868 3-1 \u5217\u4e3e\u4e86 Java \u4e2d\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u5360\u7528\u7a7a\u95f4\u3001\u53d6\u503c\u8303\u56f4\u548c\u9ed8\u8ba4\u503c\u3002\u6b64\u8868\u683c\u65e0\u987b\u6b7b\u8bb0\u786c\u80cc\uff0c\u5927\u81f4\u7406\u89e3\u5373\u53ef\uff0c\u9700\u8981\u65f6\u53ef\u4ee5\u901a\u8fc7\u67e5\u8868\u6765\u56de\u5fc6\u3002

        \u8868 3-1 \u00a0 \u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u5360\u7528\u7a7a\u95f4\u548c\u53d6\u503c\u8303\u56f4

        \u7c7b\u578b \u7b26\u53f7 \u5360\u7528\u7a7a\u95f4 \u6700\u5c0f\u503c \u6700\u5927\u503c \u9ed8\u8ba4\u503c \u6574\u6570 byte 1 \u5b57\u8282 \\(-2^7\\) (\\(-128\\)) \\(2^7 - 1\\) (\\(127\\)) \\(0\\) short 2 \u5b57\u8282 \\(-2^{15}\\) \\(2^{15} - 1\\) \\(0\\) int 4 \u5b57\u8282 \\(-2^{31}\\) \\(2^{31} - 1\\) \\(0\\) long 8 \u5b57\u8282 \\(-2^{63}\\) \\(2^{63} - 1\\) \\(0\\) \u6d6e\u70b9\u6570 float 4 \u5b57\u8282 \\(1.175 \\times 10^{-38}\\) \\(3.403 \\times 10^{38}\\) \\(0.0\\text{f}\\) double 8 \u5b57\u8282 \\(2.225 \\times 10^{-308}\\) \\(1.798 \\times 10^{308}\\) \\(0.0\\) \u5b57\u7b26 char 2 \u5b57\u8282 \\(0\\) \\(2^{16} - 1\\) \\(0\\) \u5e03\u5c14 bool 1 \u5b57\u8282 \\(\\text{false}\\) \\(\\text{true}\\) \\(\\text{false}\\)

        \u8bf7\u6ce8\u610f\uff0c\u8868 3-1 \u9488\u5bf9\u7684\u662f Java \u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u60c5\u51b5\u3002\u6bcf\u79cd\u7f16\u7a0b\u8bed\u8a00\u90fd\u6709\u5404\u81ea\u7684\u6570\u636e\u7c7b\u578b\u5b9a\u4e49\uff0c\u5b83\u4eec\u7684\u5360\u7528\u7a7a\u95f4\u3001\u53d6\u503c\u8303\u56f4\u548c\u9ed8\u8ba4\u503c\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002

        • \u5728 Python \u4e2d\uff0c\u6574\u6570\u7c7b\u578b int \u53ef\u4ee5\u662f\u4efb\u610f\u5927\u5c0f\uff0c\u53ea\u53d7\u9650\u4e8e\u53ef\u7528\u5185\u5b58\uff1b\u6d6e\u70b9\u6570 float \u662f\u53cc\u7cbe\u5ea6 64 \u4f4d\uff1b\u6ca1\u6709 char \u7c7b\u578b\uff0c\u5355\u4e2a\u5b57\u7b26\u5b9e\u9645\u4e0a\u662f\u957f\u5ea6\u4e3a 1 \u7684\u5b57\u7b26\u4e32 str \u3002
        • C \u548c C++ \u672a\u660e\u786e\u89c4\u5b9a\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u5927\u5c0f\uff0c\u800c\u56e0\u5b9e\u73b0\u548c\u5e73\u53f0\u5404\u5f02\u3002\u8868 3-1 \u9075\u5faa LP64 \u6570\u636e\u6a21\u578b\uff0c\u5176\u7528\u4e8e\u5305\u62ec Linux \u548c macOS \u5728\u5185\u7684 Unix 64 \u4f4d\u64cd\u4f5c\u7cfb\u7edf\u3002
        • \u5b57\u7b26 char \u7684\u5927\u5c0f\u5728 C \u548c C++ \u4e2d\u4e3a 1 \u5b57\u8282\uff0c\u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\u53d6\u51b3\u4e8e\u7279\u5b9a\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u6cd5\uff0c\u8be6\u89c1\u201c\u5b57\u7b26\u7f16\u7801\u201d\u7ae0\u8282\u3002
        • \u5373\u4f7f\u8868\u793a\u5e03\u5c14\u91cf\u4ec5\u9700 1 \u4f4d\uff08\\(0\\) \u6216 \\(1\\)\uff09\uff0c\u5b83\u5728\u5185\u5b58\u4e2d\u901a\u5e38\u4e5f\u5b58\u50a8\u4e3a 1 \u5b57\u8282\u3002\u8fd9\u662f\u56e0\u4e3a\u73b0\u4ee3\u8ba1\u7b97\u673a CPU \u901a\u5e38\u5c06 1 \u5b57\u8282\u4f5c\u4e3a\u6700\u5c0f\u5bfb\u5740\u5185\u5b58\u5355\u5143\u3002

        \u90a3\u4e48\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e0e\u6570\u636e\u7ed3\u6784\u4e4b\u95f4\u6709\u4ec0\u4e48\u8054\u7cfb\u5462\uff1f\u6211\u4eec\u77e5\u9053\uff0c\u6570\u636e\u7ed3\u6784\u662f\u5728\u8ba1\u7b97\u673a\u4e2d\u7ec4\u7ec7\u4e0e\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\u3002\u8fd9\u53e5\u8bdd\u7684\u4e3b\u8bed\u662f\u201c\u7ed3\u6784\u201d\u800c\u975e\u201c\u6570\u636e\u201d\u3002

        \u5982\u679c\u60f3\u8868\u793a\u201c\u4e00\u6392\u6570\u5b57\u201d\uff0c\u6211\u4eec\u81ea\u7136\u4f1a\u60f3\u5230\u4f7f\u7528\u6570\u7ec4\u3002\u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u7684\u7ebf\u6027\u7ed3\u6784\u53ef\u4ee5\u8868\u793a\u6570\u5b57\u7684\u76f8\u90bb\u5173\u7cfb\u548c\u987a\u5e8f\u5173\u7cfb\uff0c\u4f46\u81f3\u4e8e\u5b58\u50a8\u7684\u5185\u5bb9\u662f\u6574\u6570 int\u3001\u5c0f\u6570 float \u8fd8\u662f\u5b57\u7b26 char \uff0c\u5219\u4e0e\u201c\u6570\u636e\u7ed3\u6784\u201d\u65e0\u5173\u3002

        \u6362\u53e5\u8bdd\u8bf4\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u4e86\u6570\u636e\u7684\u201c\u5185\u5bb9\u7c7b\u578b\u201d\uff0c\u800c\u6570\u636e\u7ed3\u6784\u63d0\u4f9b\u4e86\u6570\u636e\u7684\u201c\u7ec4\u7ec7\u65b9\u5f0f\u201d\u3002\u4f8b\u5982\u4ee5\u4e0b\u4ee3\u7801\uff0c\u6211\u4eec\u7528\u76f8\u540c\u7684\u6570\u636e\u7ed3\u6784\uff08\u6570\u7ec4\uff09\u6765\u5b58\u50a8\u4e0e\u8868\u793a\u4e0d\u540c\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec int\u3001float\u3001char\u3001bool \u7b49\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        # \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nnumbers: list[int] = [0] * 5\ndecimals: list[float] = [0.0] * 5\n# Python \u7684\u5b57\u7b26\u5b9e\u9645\u4e0a\u662f\u957f\u5ea6\u4e3a 1 \u7684\u5b57\u7b26\u4e32\ncharacters: list[str] = ['0'] * 5\nbools: list[bool] = [False] * 5\n# Python \u7684\u5217\u8868\u53ef\u4ee5\u81ea\u7531\u5b58\u50a8\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u8c61\u5f15\u7528\ndata = [0, 0.0, 'a', False, ListNode(0)]\n
        // \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint numbers[5];\nfloat decimals[5];\nchar characters[5];\nbool bools[5];\n
        // \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nboolean[] bools = new boolean[5];\n
        // \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nbool[] bools = new bool[5];\n
        // \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nvar numbers = [5]int{}\nvar decimals = [5]float64{}\nvar characters = [5]byte{}\nvar bools = [5]bool{}\n
        // \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nlet numbers = Array(repeating: 0, count: 5)\nlet decimals = Array(repeating: 0.0, count: 5)\nlet characters: [Character] = Array(repeating: \"a\", count: 5)\nlet bools = Array(repeating: false, count: 5)\n
        // JavaScript \u7684\u6570\u7ec4\u53ef\u4ee5\u81ea\u7531\u5b58\u50a8\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u8c61\nconst array = [0, 0.0, 'a', false];\n
        // \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nconst numbers: number[] = [];\nconst characters: string[] = [];\nconst bools: boolean[] = [];\n
        // \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nList<int> numbers = List.filled(5, 0);\nList<double> decimals = List.filled(5, 0.0);\nList<String> characters = List.filled(5, 'a');\nList<bool> bools = List.filled(5, false);\n
        // \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nlet numbers: Vec<i32> = vec![0; 5];\nlet decimals: Vec<f32> = vec![0.0; 5];\nlet characters: Vec<char> = vec!['0'; 5];\nlet bools: Vec<bool> = vec![false; 5];\n
        // \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint numbers[10];\nfloat decimals[10];\nchar characters[10];\nbool bools[10];\n
        // \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nval numbers = IntArray(5)\nval decinals = FloatArray(5)\nval characters = CharArray(5)\nval bools = BooleanArray(5)\n
        # Ruby \u7684\u5217\u8868\u53ef\u4ee5\u81ea\u7531\u5b58\u50a8\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u8c61\u5f15\u7528\ndata = [0, 0.0, 'a', false, ListNode(0)]\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_data_structure/character_encoding/","title":"3.4 \u00a0 \u5b57\u7b26\u7f16\u7801 *","text":"

        \u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u6240\u6709\u6570\u636e\u90fd\u662f\u4ee5\u4e8c\u8fdb\u5236\u6570\u7684\u5f62\u5f0f\u5b58\u50a8\u7684\uff0c\u5b57\u7b26 char \u4e5f\u4e0d\u4f8b\u5916\u3002\u4e3a\u4e86\u8868\u793a\u5b57\u7b26\uff0c\u6211\u4eec\u9700\u8981\u5efa\u7acb\u4e00\u5957\u201c\u5b57\u7b26\u96c6\u201d\uff0c\u89c4\u5b9a\u6bcf\u4e2a\u5b57\u7b26\u548c\u4e8c\u8fdb\u5236\u6570\u4e4b\u95f4\u7684\u4e00\u4e00\u5bf9\u5e94\u5173\u7cfb\u3002\u6709\u4e86\u5b57\u7b26\u96c6\u4e4b\u540e\uff0c\u8ba1\u7b97\u673a\u5c31\u53ef\u4ee5\u901a\u8fc7\u67e5\u8868\u5b8c\u6210\u4e8c\u8fdb\u5236\u6570\u5230\u5b57\u7b26\u7684\u8f6c\u6362\u3002

        "},{"location":"chapter_data_structure/character_encoding/#341-ascii","title":"3.4.1 \u00a0 ASCII \u5b57\u7b26\u96c6","text":"

        ASCII \u7801\u662f\u6700\u65e9\u51fa\u73b0\u7684\u5b57\u7b26\u96c6\uff0c\u5176\u5168\u79f0\u4e3a American Standard Code for Information Interchange\uff08\u7f8e\u56fd\u6807\u51c6\u4fe1\u606f\u4ea4\u6362\u4ee3\u7801\uff09\u3002\u5b83\u4f7f\u7528 7 \u4f4d\u4e8c\u8fdb\u5236\u6570\uff08\u4e00\u4e2a\u5b57\u8282\u7684\u4f4e 7 \u4f4d\uff09\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff0c\u6700\u591a\u80fd\u591f\u8868\u793a 128 \u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\u3002\u5982\u56fe 3-6 \u6240\u793a\uff0cASCII \u7801\u5305\u62ec\u82f1\u6587\u5b57\u6bcd\u7684\u5927\u5c0f\u5199\u3001\u6570\u5b57 0 ~ 9\u3001\u4e00\u4e9b\u6807\u70b9\u7b26\u53f7\uff0c\u4ee5\u53ca\u4e00\u4e9b\u63a7\u5236\u5b57\u7b26\uff08\u5982\u6362\u884c\u7b26\u548c\u5236\u8868\u7b26\uff09\u3002

        \u56fe 3-6 \u00a0 ASCII \u7801

        \u7136\u800c\uff0cASCII \u7801\u4ec5\u80fd\u591f\u8868\u793a\u82f1\u6587\u3002\u968f\u7740\u8ba1\u7b97\u673a\u7684\u5168\u7403\u5316\uff0c\u8bde\u751f\u4e86\u4e00\u79cd\u80fd\u591f\u8868\u793a\u66f4\u591a\u8bed\u8a00\u7684 EASCII \u5b57\u7b26\u96c6\u3002\u5b83\u5728 ASCII \u7684 7 \u4f4d\u57fa\u7840\u4e0a\u6269\u5c55\u5230 8 \u4f4d\uff0c\u80fd\u591f\u8868\u793a 256 \u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\u3002

        \u5728\u4e16\u754c\u8303\u56f4\u5185\uff0c\u9646\u7eed\u51fa\u73b0\u4e86\u4e00\u6279\u9002\u7528\u4e8e\u4e0d\u540c\u5730\u533a\u7684 EASCII \u5b57\u7b26\u96c6\u3002\u8fd9\u4e9b\u5b57\u7b26\u96c6\u7684\u524d 128 \u4e2a\u5b57\u7b26\u7edf\u4e00\u4e3a ASCII \u7801\uff0c\u540e 128 \u4e2a\u5b57\u7b26\u5b9a\u4e49\u4e0d\u540c\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u8bed\u8a00\u7684\u9700\u6c42\u3002

        "},{"location":"chapter_data_structure/character_encoding/#342-gbk","title":"3.4.2 \u00a0 GBK \u5b57\u7b26\u96c6","text":"

        \u540e\u6765\u4eba\u4eec\u53d1\u73b0\uff0cEASCII \u7801\u4ecd\u7136\u65e0\u6cd5\u6ee1\u8db3\u8bb8\u591a\u8bed\u8a00\u7684\u5b57\u7b26\u6570\u91cf\u8981\u6c42\u3002\u6bd4\u5982\u6c49\u5b57\u6709\u8fd1\u5341\u4e07\u4e2a\uff0c\u5149\u65e5\u5e38\u4f7f\u7528\u7684\u5c31\u6709\u51e0\u5343\u4e2a\u3002\u4e2d\u56fd\u56fd\u5bb6\u6807\u51c6\u603b\u5c40\u4e8e 1980 \u5e74\u53d1\u5e03\u4e86 GB2312 \u5b57\u7b26\u96c6\uff0c\u5176\u6536\u5f55\u4e86 6763 \u4e2a\u6c49\u5b57\uff0c\u57fa\u672c\u6ee1\u8db3\u4e86\u6c49\u5b57\u7684\u8ba1\u7b97\u673a\u5904\u7406\u9700\u8981\u3002

        \u7136\u800c\uff0cGB2312 \u65e0\u6cd5\u5904\u7406\u90e8\u5206\u7f55\u89c1\u5b57\u548c\u7e41\u4f53\u5b57\u3002GBK \u5b57\u7b26\u96c6\u662f\u5728 GB2312 \u7684\u57fa\u7840\u4e0a\u6269\u5c55\u5f97\u5230\u7684\uff0c\u5b83\u5171\u6536\u5f55\u4e86 21886 \u4e2a\u6c49\u5b57\u3002\u5728 GBK \u7684\u7f16\u7801\u65b9\u6848\u4e2d\uff0cASCII \u5b57\u7b26\u4f7f\u7528\u4e00\u4e2a\u5b57\u8282\u8868\u793a\uff0c\u6c49\u5b57\u4f7f\u7528\u4e24\u4e2a\u5b57\u8282\u8868\u793a\u3002

        "},{"location":"chapter_data_structure/character_encoding/#343-unicode","title":"3.4.3 \u00a0 Unicode \u5b57\u7b26\u96c6","text":"

        \u968f\u7740\u8ba1\u7b97\u673a\u6280\u672f\u7684\u84ec\u52c3\u53d1\u5c55\uff0c\u5b57\u7b26\u96c6\u4e0e\u7f16\u7801\u6807\u51c6\u767e\u82b1\u9f50\u653e\uff0c\u800c\u8fd9\u5e26\u6765\u4e86\u8bb8\u591a\u95ee\u9898\u3002\u4e00\u65b9\u9762\uff0c\u8fd9\u4e9b\u5b57\u7b26\u96c6\u4e00\u822c\u53ea\u5b9a\u4e49\u4e86\u7279\u5b9a\u8bed\u8a00\u7684\u5b57\u7b26\uff0c\u65e0\u6cd5\u5728\u591a\u8bed\u8a00\u73af\u5883\u4e0b\u6b63\u5e38\u5de5\u4f5c\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u540c\u4e00\u79cd\u8bed\u8a00\u5b58\u5728\u591a\u79cd\u5b57\u7b26\u96c6\u6807\u51c6\uff0c\u5982\u679c\u4e24\u53f0\u8ba1\u7b97\u673a\u4f7f\u7528\u7684\u662f\u4e0d\u540c\u7684\u7f16\u7801\u6807\u51c6\uff0c\u5219\u5728\u4fe1\u606f\u4f20\u9012\u65f6\u5c31\u4f1a\u51fa\u73b0\u4e71\u7801\u3002

        \u90a3\u4e2a\u65f6\u4ee3\u7684\u7814\u7a76\u4eba\u5458\u5c31\u5728\u60f3\uff1a\u5982\u679c\u63a8\u51fa\u4e00\u4e2a\u8db3\u591f\u5b8c\u6574\u7684\u5b57\u7b26\u96c6\uff0c\u5c06\u4e16\u754c\u8303\u56f4\u5185\u7684\u6240\u6709\u8bed\u8a00\u548c\u7b26\u53f7\u90fd\u6536\u5f55\u5176\u4e2d\uff0c\u4e0d\u5c31\u53ef\u4ee5\u89e3\u51b3\u8de8\u8bed\u8a00\u73af\u5883\u548c\u4e71\u7801\u95ee\u9898\u4e86\u5417\uff1f\u5728\u8fd9\u79cd\u60f3\u6cd5\u7684\u9a71\u52a8\u4e0b\uff0c\u4e00\u4e2a\u5927\u800c\u5168\u7684\u5b57\u7b26\u96c6 Unicode \u5e94\u8fd0\u800c\u751f\u3002

        Unicode \u7684\u4e2d\u6587\u540d\u79f0\u4e3a\u201c\u7edf\u4e00\u7801\u201d\uff0c\u7406\u8bba\u4e0a\u80fd\u5bb9\u7eb3 100 \u591a\u4e07\u4e2a\u5b57\u7b26\u3002\u5b83\u81f4\u529b\u4e8e\u5c06\u5168\u7403\u8303\u56f4\u5185\u7684\u5b57\u7b26\u7eb3\u5165\u7edf\u4e00\u7684\u5b57\u7b26\u96c6\u4e4b\u4e2d\uff0c\u63d0\u4f9b\u4e00\u79cd\u901a\u7528\u7684\u5b57\u7b26\u96c6\u6765\u5904\u7406\u548c\u663e\u793a\u5404\u79cd\u8bed\u8a00\u6587\u5b57\uff0c\u51cf\u5c11\u56e0\u4e3a\u7f16\u7801\u6807\u51c6\u4e0d\u540c\u800c\u4ea7\u751f\u7684\u4e71\u7801\u95ee\u9898\u3002

        \u81ea 1991 \u5e74\u53d1\u5e03\u4ee5\u6765\uff0cUnicode \u4e0d\u65ad\u6269\u5145\u65b0\u7684\u8bed\u8a00\u4e0e\u5b57\u7b26\u3002\u622a\u81f3 2022 \u5e74 9 \u6708\uff0cUnicode \u5df2\u7ecf\u5305\u542b 149186 \u4e2a\u5b57\u7b26\uff0c\u5305\u62ec\u5404\u79cd\u8bed\u8a00\u7684\u5b57\u7b26\u3001\u7b26\u53f7\u751a\u81f3\u8868\u60c5\u7b26\u53f7\u7b49\u3002\u5728\u5e9e\u5927\u7684 Unicode \u5b57\u7b26\u96c6\u4e2d\uff0c\u5e38\u7528\u7684\u5b57\u7b26\u5360\u7528 2 \u5b57\u8282\uff0c\u6709\u4e9b\u751f\u50fb\u7684\u5b57\u7b26\u5360\u7528 3 \u5b57\u8282\u751a\u81f3 4 \u5b57\u8282\u3002

        Unicode \u662f\u4e00\u79cd\u901a\u7528\u5b57\u7b26\u96c6\uff0c\u672c\u8d28\u4e0a\u662f\u7ed9\u6bcf\u4e2a\u5b57\u7b26\u5206\u914d\u4e00\u4e2a\u7f16\u53f7\uff08\u79f0\u4e3a\u201c\u7801\u70b9\u201d\uff09\uff0c\u4f46\u5b83\u5e76\u6ca1\u6709\u89c4\u5b9a\u5728\u8ba1\u7b97\u673a\u4e2d\u5982\u4f55\u5b58\u50a8\u8fd9\u4e9b\u5b57\u7b26\u7801\u70b9\u3002\u6211\u4eec\u4e0d\u7981\u4f1a\u95ee\uff1a\u5f53\u591a\u79cd\u957f\u5ea6\u7684 Unicode \u7801\u70b9\u540c\u65f6\u51fa\u73b0\u5728\u4e00\u4e2a\u6587\u672c\u4e2d\u65f6\uff0c\u7cfb\u7edf\u5982\u4f55\u89e3\u6790\u5b57\u7b26\uff1f\u4f8b\u5982\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a 2 \u5b57\u8282\u7684\u7f16\u7801\uff0c\u7cfb\u7edf\u5982\u4f55\u786e\u8ba4\u5b83\u662f\u4e00\u4e2a 2 \u5b57\u8282\u7684\u5b57\u7b26\u8fd8\u662f\u4e24\u4e2a 1 \u5b57\u8282\u7684\u5b57\u7b26\uff1f

        \u5bf9\u4e8e\u4ee5\u4e0a\u95ee\u9898\uff0c\u4e00\u79cd\u76f4\u63a5\u7684\u89e3\u51b3\u65b9\u6848\u662f\u5c06\u6240\u6709\u5b57\u7b26\u5b58\u50a8\u4e3a\u7b49\u957f\u7684\u7f16\u7801\u3002\u5982\u56fe 3-7 \u6240\u793a\uff0c\u201cHello\u201d\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 1 \u5b57\u8282\uff0c\u201c\u7b97\u6cd5\u201d\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u5b57\u8282\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u9ad8\u4f4d\u586b 0 \u5c06\u201cHello \u7b97\u6cd5\u201d\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u90fd\u7f16\u7801\u4e3a 2 \u5b57\u8282\u957f\u5ea6\u3002\u8fd9\u6837\u7cfb\u7edf\u5c31\u53ef\u4ee5\u6bcf\u9694 2 \u5b57\u8282\u89e3\u6790\u4e00\u4e2a\u5b57\u7b26\uff0c\u6062\u590d\u8fd9\u4e2a\u77ed\u8bed\u7684\u5185\u5bb9\u4e86\u3002

        \u56fe 3-7 \u00a0 Unicode \u7f16\u7801\u793a\u4f8b

        \u7136\u800c ASCII \u7801\u5df2\u7ecf\u5411\u6211\u4eec\u8bc1\u660e\uff0c\u7f16\u7801\u82f1\u6587\u53ea\u9700 1 \u5b57\u8282\u3002\u82e5\u91c7\u7528\u4e0a\u8ff0\u65b9\u6848\uff0c\u82f1\u6587\u6587\u672c\u5360\u7528\u7a7a\u95f4\u7684\u5927\u5c0f\u5c06\u4f1a\u662f ASCII \u7f16\u7801\u4e0b\u7684\u4e24\u500d\uff0c\u975e\u5e38\u6d6a\u8d39\u5185\u5b58\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4e00\u79cd\u66f4\u52a0\u9ad8\u6548\u7684 Unicode \u7f16\u7801\u65b9\u6cd5\u3002

        "},{"location":"chapter_data_structure/character_encoding/#344-utf-8","title":"3.4.4 \u00a0 UTF-8 \u7f16\u7801","text":"

        \u76ee\u524d\uff0cUTF-8 \u5df2\u6210\u4e3a\u56fd\u9645\u4e0a\u4f7f\u7528\u6700\u5e7f\u6cdb\u7684 Unicode \u7f16\u7801\u65b9\u6cd5\u3002\u5b83\u662f\u4e00\u79cd\u53ef\u53d8\u957f\u5ea6\u7684\u7f16\u7801\uff0c\u4f7f\u7528 1 \u5230 4 \u5b57\u8282\u6765\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff0c\u6839\u636e\u5b57\u7b26\u7684\u590d\u6742\u6027\u800c\u53d8\u3002ASCII \u5b57\u7b26\u53ea\u9700 1 \u5b57\u8282\uff0c\u62c9\u4e01\u5b57\u6bcd\u548c\u5e0c\u814a\u5b57\u6bcd\u9700\u8981 2 \u5b57\u8282\uff0c\u5e38\u7528\u7684\u4e2d\u6587\u5b57\u7b26\u9700\u8981 3 \u5b57\u8282\uff0c\u5176\u4ed6\u7684\u4e00\u4e9b\u751f\u50fb\u5b57\u7b26\u9700\u8981 4 \u5b57\u8282\u3002

        UTF-8 \u7684\u7f16\u7801\u89c4\u5219\u5e76\u4e0d\u590d\u6742\uff0c\u5206\u4e3a\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\u3002

        • \u5bf9\u4e8e\u957f\u5ea6\u4e3a 1 \u5b57\u8282\u7684\u5b57\u7b26\uff0c\u5c06\u6700\u9ad8\u4f4d\u8bbe\u7f6e\u4e3a \\(0\\) \uff0c\u5176\u4f59 7 \u4f4d\u8bbe\u7f6e\u4e3a Unicode \u7801\u70b9\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0cASCII \u5b57\u7b26\u5728 Unicode \u5b57\u7b26\u96c6\u4e2d\u5360\u636e\u4e86\u524d 128 \u4e2a\u7801\u70b9\u3002\u4e5f\u5c31\u662f\u8bf4\uff0cUTF-8 \u7f16\u7801\u53ef\u4ee5\u5411\u4e0b\u517c\u5bb9 ASCII \u7801\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 UTF-8 \u6765\u89e3\u6790\u5e74\u4ee3\u4e45\u8fdc\u7684 ASCII \u7801\u6587\u672c\u3002
        • \u5bf9\u4e8e\u957f\u5ea6\u4e3a \\(n\\) \u5b57\u8282\u7684\u5b57\u7b26\uff08\u5176\u4e2d \\(n > 1\\)\uff09\uff0c\u5c06\u9996\u4e2a\u5b57\u8282\u7684\u9ad8 \\(n\\) \u4f4d\u90fd\u8bbe\u7f6e\u4e3a \\(1\\) \uff0c\u7b2c \\(n + 1\\) \u4f4d\u8bbe\u7f6e\u4e3a \\(0\\) \uff1b\u4ece\u7b2c\u4e8c\u4e2a\u5b57\u8282\u5f00\u59cb\uff0c\u5c06\u6bcf\u4e2a\u5b57\u8282\u7684\u9ad8 2 \u4f4d\u90fd\u8bbe\u7f6e\u4e3a \\(10\\) \uff1b\u5176\u4f59\u6240\u6709\u4f4d\u7528\u4e8e\u586b\u5145\u5b57\u7b26\u7684 Unicode \u7801\u70b9\u3002

        \u56fe 3-8 \u5c55\u793a\u4e86\u201cHello\u7b97\u6cd5\u201d\u5bf9\u5e94\u7684 UTF-8 \u7f16\u7801\u3002\u89c2\u5bdf\u53d1\u73b0\uff0c\u7531\u4e8e\u6700\u9ad8 \\(n\\) \u4f4d\u90fd\u8bbe\u7f6e\u4e3a \\(1\\) \uff0c\u56e0\u6b64\u7cfb\u7edf\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u6700\u9ad8\u4f4d \\(1\\) \u7684\u4e2a\u6570\u6765\u89e3\u6790\u51fa\u5b57\u7b26\u7684\u957f\u5ea6\u4e3a \\(n\\) \u3002

        \u4f46\u4e3a\u4ec0\u4e48\u8981\u5c06\u5176\u4f59\u6240\u6709\u5b57\u8282\u7684\u9ad8 2 \u4f4d\u90fd\u8bbe\u7f6e\u4e3a \\(10\\) \u5462\uff1f\u5b9e\u9645\u4e0a\uff0c\u8fd9\u4e2a \\(10\\) \u80fd\u591f\u8d77\u5230\u6821\u9a8c\u7b26\u7684\u4f5c\u7528\u3002\u5047\u8bbe\u7cfb\u7edf\u4ece\u4e00\u4e2a\u9519\u8bef\u7684\u5b57\u8282\u5f00\u59cb\u89e3\u6790\u6587\u672c\uff0c\u5b57\u8282\u5934\u90e8\u7684 \\(10\\) \u80fd\u591f\u5e2e\u52a9\u7cfb\u7edf\u5feb\u901f\u5224\u65ad\u51fa\u5f02\u5e38\u3002

        \u4e4b\u6240\u4ee5\u5c06 \\(10\\) \u5f53\u4f5c\u6821\u9a8c\u7b26\uff0c\u662f\u56e0\u4e3a\u5728 UTF-8 \u7f16\u7801\u89c4\u5219\u4e0b\uff0c\u4e0d\u53ef\u80fd\u6709\u5b57\u7b26\u7684\u6700\u9ad8\u4e24\u4f4d\u662f \\(10\\) \u3002\u8fd9\u4e2a\u7ed3\u8bba\u53ef\u4ee5\u7528\u53cd\u8bc1\u6cd5\u6765\u8bc1\u660e\uff1a\u5047\u8bbe\u4e00\u4e2a\u5b57\u7b26\u7684\u6700\u9ad8\u4e24\u4f4d\u662f \\(10\\) \uff0c\u8bf4\u660e\u8be5\u5b57\u7b26\u7684\u957f\u5ea6\u4e3a \\(1\\) \uff0c\u5bf9\u5e94 ASCII \u7801\u3002\u800c ASCII \u7801\u7684\u6700\u9ad8\u4f4d\u5e94\u8be5\u662f \\(0\\) \uff0c\u4e0e\u5047\u8bbe\u77db\u76fe\u3002

        \u56fe 3-8 \u00a0 UTF-8 \u7f16\u7801\u793a\u4f8b

        \u9664\u4e86 UTF-8 \u4e4b\u5916\uff0c\u5e38\u89c1\u7684\u7f16\u7801\u65b9\u5f0f\u8fd8\u5305\u62ec\u4ee5\u4e0b\u4e24\u79cd\u3002

        • UTF-16 \u7f16\u7801\uff1a\u4f7f\u7528 2 \u6216 4 \u5b57\u8282\u6765\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\u3002\u6240\u6709\u7684 ASCII \u5b57\u7b26\u548c\u5e38\u7528\u7684\u975e\u82f1\u6587\u5b57\u7b26\uff0c\u90fd\u7528 2 \u5b57\u8282\u8868\u793a\uff1b\u5c11\u6570\u5b57\u7b26\u9700\u8981\u7528\u5230 4 \u5b57\u8282\u8868\u793a\u3002\u5bf9\u4e8e 2 \u5b57\u8282\u7684\u5b57\u7b26\uff0cUTF-16 \u7f16\u7801\u4e0e Unicode \u7801\u70b9\u76f8\u7b49\u3002
        • UTF-32 \u7f16\u7801\uff1a\u6bcf\u4e2a\u5b57\u7b26\u90fd\u4f7f\u7528 4 \u5b57\u8282\u3002\u8fd9\u610f\u5473\u7740 UTF-32 \u6bd4 UTF-8 \u548c UTF-16 \u66f4\u5360\u7528\u7a7a\u95f4\uff0c\u7279\u522b\u662f\u5bf9\u4e8e ASCII \u5b57\u7b26\u5360\u6bd4\u8f83\u9ad8\u7684\u6587\u672c\u3002

        \u4ece\u5b58\u50a8\u7a7a\u95f4\u5360\u7528\u7684\u89d2\u5ea6\u770b\uff0c\u4f7f\u7528 UTF-8 \u8868\u793a\u82f1\u6587\u5b57\u7b26\u975e\u5e38\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u4ec5\u9700 1 \u5b57\u8282\uff1b\u4f7f\u7528 UTF-16 \u7f16\u7801\u67d0\u4e9b\u975e\u82f1\u6587\u5b57\u7b26\uff08\u4f8b\u5982\u4e2d\u6587\uff09\u4f1a\u66f4\u52a0\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u4ec5\u9700 2 \u5b57\u8282\uff0c\u800c UTF-8 \u53ef\u80fd\u9700\u8981 3 \u5b57\u8282\u3002

        \u4ece\u517c\u5bb9\u6027\u7684\u89d2\u5ea6\u770b\uff0cUTF-8 \u7684\u901a\u7528\u6027\u6700\u4f73\uff0c\u8bb8\u591a\u5de5\u5177\u548c\u5e93\u4f18\u5148\u652f\u6301 UTF-8 \u3002

        "},{"location":"chapter_data_structure/character_encoding/#345","title":"3.4.5 \u00a0 \u7f16\u7a0b\u8bed\u8a00\u7684\u5b57\u7b26\u7f16\u7801","text":"

        \u5bf9\u4e8e\u4ee5\u5f80\u7684\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\uff0c\u7a0b\u5e8f\u8fd0\u884c\u4e2d\u7684\u5b57\u7b26\u4e32\u90fd\u91c7\u7528 UTF-16 \u6216 UTF-32 \u8fd9\u7c7b\u7b49\u957f\u7f16\u7801\u3002\u5728\u7b49\u957f\u7f16\u7801\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u770b\u4f5c\u6570\u7ec4\u6765\u5904\u7406\uff0c\u8fd9\u79cd\u505a\u6cd5\u5177\u6709\u4ee5\u4e0b\u4f18\u70b9\u3002

        • \u968f\u673a\u8bbf\u95ee\uff1aUTF-16 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u8fdb\u884c\u968f\u673a\u8bbf\u95ee\u3002UTF-8 \u662f\u4e00\u79cd\u53d8\u957f\u7f16\u7801\uff0c\u8981\u60f3\u627e\u5230\u7b2c \\(i\\) \u4e2a\u5b57\u7b26\uff0c\u6211\u4eec\u9700\u8981\u4ece\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u5904\u904d\u5386\u5230\u7b2c \\(i\\) \u4e2a\u5b57\u7b26\uff0c\u8fd9\u9700\u8981 \\(O(n)\\) \u7684\u65f6\u95f4\u3002
        • \u5b57\u7b26\u8ba1\u6570\uff1a\u4e0e\u968f\u673a\u8bbf\u95ee\u7c7b\u4f3c\uff0c\u8ba1\u7b97 UTF-16 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u4e5f\u662f \\(O(1)\\) \u7684\u64cd\u4f5c\u3002\u4f46\u662f\uff0c\u8ba1\u7b97 UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u9700\u8981\u904d\u5386\u6574\u4e2a\u5b57\u7b26\u4e32\u3002
        • \u5b57\u7b26\u4e32\u64cd\u4f5c\uff1a\u5728 UTF-16 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u4e0a\uff0c\u5f88\u591a\u5b57\u7b26\u4e32\u64cd\u4f5c\uff08\u5982\u5206\u5272\u3001\u8fde\u63a5\u3001\u63d2\u5165\u3001\u5220\u9664\u7b49\uff09\u66f4\u5bb9\u6613\u8fdb\u884c\u3002\u5728 UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u4e0a\uff0c\u8fdb\u884c\u8fd9\u4e9b\u64cd\u4f5c\u901a\u5e38\u9700\u8981\u989d\u5916\u7684\u8ba1\u7b97\uff0c\u4ee5\u786e\u4fdd\u4e0d\u4f1a\u4ea7\u751f\u65e0\u6548\u7684 UTF-8 \u7f16\u7801\u3002

        \u5b9e\u9645\u4e0a\uff0c\u7f16\u7a0b\u8bed\u8a00\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u6848\u8bbe\u8ba1\u662f\u4e00\u4e2a\u5f88\u6709\u8da3\u7684\u8bdd\u9898\uff0c\u6d89\u53ca\u8bb8\u591a\u56e0\u7d20\u3002

        • Java \u7684 String \u7c7b\u578b\u4f7f\u7528 UTF-16 \u7f16\u7801\uff0c\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u5b57\u8282\u3002\u8fd9\u662f\u56e0\u4e3a Java \u8bed\u8a00\u8bbe\u8ba1\u4e4b\u521d\uff0c\u4eba\u4eec\u8ba4\u4e3a 16 \u4f4d\u8db3\u4ee5\u8868\u793a\u6240\u6709\u53ef\u80fd\u7684\u5b57\u7b26\u3002\u7136\u800c\uff0c\u8fd9\u662f\u4e00\u4e2a\u4e0d\u6b63\u786e\u7684\u5224\u65ad\u3002\u540e\u6765 Unicode \u89c4\u8303\u6269\u5c55\u5230\u4e86\u8d85\u8fc7 16 \u4f4d\uff0c\u6240\u4ee5 Java \u4e2d\u7684\u5b57\u7b26\u73b0\u5728\u53ef\u80fd\u7531\u4e00\u5bf9 16 \u4f4d\u7684\u503c\uff08\u79f0\u4e3a\u201c\u4ee3\u7406\u5bf9\u201d\uff09\u8868\u793a\u3002
        • JavaScript \u548c TypeScript \u7684\u5b57\u7b26\u4e32\u4f7f\u7528 UTF-16 \u7f16\u7801\u7684\u539f\u56e0\u4e0e Java \u7c7b\u4f3c\u3002\u5f53 1995 \u5e74 Netscape \u516c\u53f8\u9996\u6b21\u63a8\u51fa JavaScript \u8bed\u8a00\u65f6\uff0cUnicode \u8fd8\u5904\u4e8e\u53d1\u5c55\u65e9\u671f\uff0c\u90a3\u65f6\u5019\u4f7f\u7528 16 \u4f4d\u7684\u7f16\u7801\u5c31\u8db3\u4ee5\u8868\u793a\u6240\u6709\u7684 Unicode \u5b57\u7b26\u4e86\u3002
        • C# \u4f7f\u7528 UTF-16 \u7f16\u7801\uff0c\u4e3b\u8981\u662f\u56e0\u4e3a .NET \u5e73\u53f0\u662f\u7531 Microsoft \u8bbe\u8ba1\u7684\uff0c\u800c Microsoft \u7684\u5f88\u591a\u6280\u672f\uff08\u5305\u62ec Windows \u64cd\u4f5c\u7cfb\u7edf\uff09\u90fd\u5e7f\u6cdb\u4f7f\u7528 UTF-16 \u7f16\u7801\u3002

        \u7531\u4e8e\u4ee5\u4e0a\u7f16\u7a0b\u8bed\u8a00\u5bf9\u5b57\u7b26\u6570\u91cf\u7684\u4f4e\u4f30\uff0c\u5b83\u4eec\u4e0d\u5f97\u4e0d\u91c7\u53d6\u201c\u4ee3\u7406\u5bf9\u201d\u7684\u65b9\u5f0f\u6765\u8868\u793a\u8d85\u8fc7 16 \u4f4d\u957f\u5ea6\u7684 Unicode \u5b57\u7b26\u3002\u8fd9\u662f\u4e00\u4e2a\u4e0d\u5f97\u5df2\u4e3a\u4e4b\u7684\u65e0\u5948\u4e4b\u4e3e\u3002\u4e00\u65b9\u9762\uff0c\u5305\u542b\u4ee3\u7406\u5bf9\u7684\u5b57\u7b26\u4e32\u4e2d\uff0c\u4e00\u4e2a\u5b57\u7b26\u53ef\u80fd\u5360\u7528 2 \u5b57\u8282\u6216 4 \u5b57\u8282\uff0c\u4ece\u800c\u4e27\u5931\u4e86\u7b49\u957f\u7f16\u7801\u7684\u4f18\u52bf\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u5904\u7406\u4ee3\u7406\u5bf9\u9700\u8981\u989d\u5916\u589e\u52a0\u4ee3\u7801\uff0c\u8fd9\u63d0\u9ad8\u4e86\u7f16\u7a0b\u7684\u590d\u6742\u6027\u548c\u8c03\u8bd5\u96be\u5ea6\u3002

        \u51fa\u4e8e\u4ee5\u4e0a\u539f\u56e0\uff0c\u90e8\u5206\u7f16\u7a0b\u8bed\u8a00\u63d0\u51fa\u4e86\u4e00\u4e9b\u4e0d\u540c\u7684\u7f16\u7801\u65b9\u6848\u3002

        • Python \u4e2d\u7684 str \u4f7f\u7528 Unicode \u7f16\u7801\uff0c\u5e76\u91c7\u7528\u4e00\u79cd\u7075\u6d3b\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u5b58\u50a8\u7684\u5b57\u7b26\u957f\u5ea6\u53d6\u51b3\u4e8e\u5b57\u7b26\u4e32\u4e2d\u6700\u5927\u7684 Unicode \u7801\u70b9\u3002\u82e5\u5b57\u7b26\u4e32\u4e2d\u5168\u90e8\u662f ASCII \u5b57\u7b26\uff0c\u5219\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 1 \u5b57\u8282\uff1b\u5982\u679c\u6709\u5b57\u7b26\u8d85\u51fa\u4e86 ASCII \u8303\u56f4\uff0c\u4f46\u5168\u90e8\u5728\u57fa\u672c\u591a\u8bed\u8a00\u5e73\u9762\uff08BMP\uff09\u5185\uff0c\u5219\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u5b57\u8282\uff1b\u5982\u679c\u6709\u8d85\u51fa BMP \u7684\u5b57\u7b26\uff0c\u5219\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 4 \u5b57\u8282\u3002
        • Go \u8bed\u8a00\u7684 string \u7c7b\u578b\u5728\u5185\u90e8\u4f7f\u7528 UTF-8 \u7f16\u7801\u3002Go \u8bed\u8a00\u8fd8\u63d0\u4f9b\u4e86 rune \u7c7b\u578b\uff0c\u5b83\u7528\u4e8e\u8868\u793a\u5355\u4e2a Unicode \u7801\u70b9\u3002
        • Rust \u8bed\u8a00\u7684 str \u548c String \u7c7b\u578b\u5728\u5185\u90e8\u4f7f\u7528 UTF-8 \u7f16\u7801\u3002Rust \u4e5f\u63d0\u4f9b\u4e86 char \u7c7b\u578b\uff0c\u7528\u4e8e\u8868\u793a\u5355\u4e2a Unicode \u7801\u70b9\u3002

        \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4ee5\u4e0a\u8ba8\u8bba\u7684\u90fd\u662f\u5b57\u7b26\u4e32\u5728\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u8fd9\u548c\u5b57\u7b26\u4e32\u5982\u4f55\u5728\u6587\u4ef6\u4e2d\u5b58\u50a8\u6216\u5728\u7f51\u7edc\u4e2d\u4f20\u8f93\u662f\u4e0d\u540c\u7684\u95ee\u9898\u3002\u5728\u6587\u4ef6\u5b58\u50a8\u6216\u7f51\u7edc\u4f20\u8f93\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a UTF-8 \u683c\u5f0f\uff0c\u4ee5\u8fbe\u5230\u6700\u4f18\u7684\u517c\u5bb9\u6027\u548c\u7a7a\u95f4\u6548\u7387\u3002

        "},{"location":"chapter_data_structure/classification_of_data_structure/","title":"3.1 \u00a0 \u6570\u636e\u7ed3\u6784\u5206\u7c7b","text":"

        \u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\u5305\u62ec\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\uff0c\u5b83\u4eec\u53ef\u4ee5\u4ece\u201c\u903b\u8f91\u7ed3\u6784\u201d\u548c\u201c\u7269\u7406\u7ed3\u6784\u201d\u4e24\u4e2a\u7ef4\u5ea6\u8fdb\u884c\u5206\u7c7b\u3002

        "},{"location":"chapter_data_structure/classification_of_data_structure/#311","title":"3.1.1 \u00a0 \u903b\u8f91\u7ed3\u6784\uff1a\u7ebf\u6027\u4e0e\u975e\u7ebf\u6027","text":"

        \u903b\u8f91\u7ed3\u6784\u63ed\u793a\u4e86\u6570\u636e\u5143\u7d20\u4e4b\u95f4\u7684\u903b\u8f91\u5173\u7cfb\u3002\u5728\u6570\u7ec4\u548c\u94fe\u8868\u4e2d\uff0c\u6570\u636e\u6309\u7167\u4e00\u5b9a\u987a\u5e8f\u6392\u5217\uff0c\u4f53\u73b0\u4e86\u6570\u636e\u4e4b\u95f4\u7684\u7ebf\u6027\u5173\u7cfb\uff1b\u800c\u5728\u6811\u4e2d\uff0c\u6570\u636e\u4ece\u9876\u90e8\u5411\u4e0b\u6309\u5c42\u6b21\u6392\u5217\uff0c\u8868\u73b0\u51fa\u201c\u7956\u5148\u201d\u4e0e\u201c\u540e\u4ee3\u201d\u4e4b\u95f4\u7684\u6d3e\u751f\u5173\u7cfb\uff1b\u56fe\u5219\u7531\u8282\u70b9\u548c\u8fb9\u6784\u6210\uff0c\u53cd\u6620\u4e86\u590d\u6742\u7684\u7f51\u7edc\u5173\u7cfb\u3002

        \u5982\u56fe 3-1 \u6240\u793a\uff0c\u903b\u8f91\u7ed3\u6784\u53ef\u5206\u4e3a\u201c\u7ebf\u6027\u201d\u548c\u201c\u975e\u7ebf\u6027\u201d\u4e24\u5927\u7c7b\u3002\u7ebf\u6027\u7ed3\u6784\u6bd4\u8f83\u76f4\u89c2\uff0c\u6307\u6570\u636e\u5728\u903b\u8f91\u5173\u7cfb\u4e0a\u5448\u7ebf\u6027\u6392\u5217\uff1b\u975e\u7ebf\u6027\u7ed3\u6784\u5219\u76f8\u53cd\uff0c\u5448\u975e\u7ebf\u6027\u6392\u5217\u3002

        • \u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff1a\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\uff0c\u5143\u7d20\u4e4b\u95f4\u662f\u4e00\u5bf9\u4e00\u7684\u987a\u5e8f\u5173\u7cfb\u3002
        • \u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff1a\u6811\u3001\u5806\u3001\u56fe\u3001\u54c8\u5e0c\u8868\u3002

        \u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5212\u5206\u4e3a\u6811\u5f62\u7ed3\u6784\u548c\u7f51\u72b6\u7ed3\u6784\u3002

        • \u6811\u5f62\u7ed3\u6784\uff1a\u6811\u3001\u5806\u3001\u54c8\u5e0c\u8868\uff0c\u5143\u7d20\u4e4b\u95f4\u662f\u4e00\u5bf9\u591a\u7684\u5173\u7cfb\u3002
        • \u7f51\u72b6\u7ed3\u6784\uff1a\u56fe\uff0c\u5143\u7d20\u4e4b\u95f4\u662f\u591a\u5bf9\u591a\u7684\u5173\u7cfb\u3002

        \u56fe 3-1 \u00a0 \u7ebf\u6027\u6570\u636e\u7ed3\u6784\u4e0e\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784

        "},{"location":"chapter_data_structure/classification_of_data_structure/#312","title":"3.1.2 \u00a0 \u7269\u7406\u7ed3\u6784\uff1a\u8fde\u7eed\u4e0e\u5206\u6563","text":"

        \u5f53\u7b97\u6cd5\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u6b63\u5728\u5904\u7406\u7684\u6570\u636e\u4e3b\u8981\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u3002\u56fe 3-2 \u5c55\u793a\u4e86\u4e00\u4e2a\u8ba1\u7b97\u673a\u5185\u5b58\u6761\uff0c\u5176\u4e2d\u6bcf\u4e2a\u9ed1\u8272\u65b9\u5757\u90fd\u5305\u542b\u4e00\u5757\u5185\u5b58\u7a7a\u95f4\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u5185\u5b58\u60f3\u8c61\u6210\u4e00\u4e2a\u5de8\u5927\u7684 Excel \u8868\u683c\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5355\u5143\u683c\u90fd\u53ef\u4ee5\u5b58\u50a8\u4e00\u5b9a\u5927\u5c0f\u7684\u6570\u636e\u3002

        \u7cfb\u7edf\u901a\u8fc7\u5185\u5b58\u5730\u5740\u6765\u8bbf\u95ee\u76ee\u6807\u4f4d\u7f6e\u7684\u6570\u636e\u3002\u5982\u56fe 3-2 \u6240\u793a\uff0c\u8ba1\u7b97\u673a\u6839\u636e\u7279\u5b9a\u89c4\u5219\u4e3a\u8868\u683c\u4e2d\u7684\u6bcf\u4e2a\u5355\u5143\u683c\u5206\u914d\u7f16\u53f7\uff0c\u786e\u4fdd\u6bcf\u4e2a\u5185\u5b58\u7a7a\u95f4\u90fd\u6709\u552f\u4e00\u7684\u5185\u5b58\u5730\u5740\u3002\u6709\u4e86\u8fd9\u4e9b\u5730\u5740\uff0c\u7a0b\u5e8f\u4fbf\u53ef\u4ee5\u8bbf\u95ee\u5185\u5b58\u4e2d\u7684\u6570\u636e\u3002

        \u56fe 3-2 \u00a0 \u5185\u5b58\u6761\u3001\u5185\u5b58\u7a7a\u95f4\u3001\u5185\u5b58\u5730\u5740

        Tip

        \u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u5c06\u5185\u5b58\u6bd4\u4f5c Excel \u8868\u683c\u662f\u4e00\u4e2a\u7b80\u5316\u7684\u7c7b\u6bd4\uff0c\u5b9e\u9645\u5185\u5b58\u7684\u5de5\u4f5c\u673a\u5236\u6bd4\u8f83\u590d\u6742\uff0c\u6d89\u53ca\u5730\u5740\u7a7a\u95f4\u3001\u5185\u5b58\u7ba1\u7406\u3001\u7f13\u5b58\u673a\u5236\u3001\u865a\u62df\u5185\u5b58\u548c\u7269\u7406\u5185\u5b58\u7b49\u6982\u5ff5\u3002

        \u5185\u5b58\u662f\u6240\u6709\u7a0b\u5e8f\u7684\u5171\u4eab\u8d44\u6e90\uff0c\u5f53\u67d0\u5757\u5185\u5b58\u88ab\u67d0\u4e2a\u7a0b\u5e8f\u5360\u7528\u65f6\uff0c\u5219\u901a\u5e38\u65e0\u6cd5\u88ab\u5176\u4ed6\u7a0b\u5e8f\u540c\u65f6\u4f7f\u7528\u4e86\u3002\u56e0\u6b64\u5728\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u8bbe\u8ba1\u4e2d\uff0c\u5185\u5b58\u8d44\u6e90\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u6bd4\u5982\uff0c\u7b97\u6cd5\u6240\u5360\u7528\u7684\u5185\u5b58\u5cf0\u503c\u4e0d\u5e94\u8d85\u8fc7\u7cfb\u7edf\u5269\u4f59\u7a7a\u95f2\u5185\u5b58\uff1b\u5982\u679c\u7f3a\u5c11\u8fde\u7eed\u5927\u5757\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u90a3\u4e48\u6240\u9009\u7528\u7684\u6570\u636e\u7ed3\u6784\u5fc5\u987b\u80fd\u591f\u5b58\u50a8\u5728\u5206\u6563\u7684\u5185\u5b58\u7a7a\u95f4\u5185\u3002

        \u5982\u56fe 3-3 \u6240\u793a\uff0c\u7269\u7406\u7ed3\u6784\u53cd\u6620\u4e86\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u53ef\u5206\u4e3a\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\uff08\u6570\u7ec4\uff09\u548c\u5206\u6563\u7a7a\u95f4\u5b58\u50a8\uff08\u94fe\u8868\uff09\u3002\u7269\u7406\u7ed3\u6784\u4ece\u5e95\u5c42\u51b3\u5b9a\u4e86\u6570\u636e\u7684\u8bbf\u95ee\u3001\u66f4\u65b0\u3001\u589e\u5220\u7b49\u64cd\u4f5c\u65b9\u6cd5\uff0c\u4e24\u79cd\u7269\u7406\u7ed3\u6784\u5728\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u65b9\u9762\u5448\u73b0\u51fa\u4e92\u8865\u7684\u7279\u70b9\u3002

        \u56fe 3-3 \u00a0 \u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u4e0e\u5206\u6563\u7a7a\u95f4\u5b58\u50a8

        \u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6240\u6709\u6570\u636e\u7ed3\u6784\u90fd\u662f\u57fa\u4e8e\u6570\u7ec4\u3001\u94fe\u8868\u6216\u4e8c\u8005\u7684\u7ec4\u5408\u5b9e\u73b0\u7684\u3002\u4f8b\u5982\uff0c\u6808\u548c\u961f\u5217\u65e2\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u94fe\u8868\u5b9e\u73b0\uff1b\u800c\u54c8\u5e0c\u8868\u7684\u5b9e\u73b0\u53ef\u80fd\u540c\u65f6\u5305\u542b\u6570\u7ec4\u548c\u94fe\u8868\u3002

        • \u57fa\u4e8e\u6570\u7ec4\u53ef\u5b9e\u73b0\uff1a\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\u3001\u77e9\u9635\u3001\u5f20\u91cf\uff08\u7ef4\u5ea6 \\(\\geq 3\\) \u7684\u6570\u7ec4\uff09\u7b49\u3002
        • \u57fa\u4e8e\u94fe\u8868\u53ef\u5b9e\u73b0\uff1a\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\u7b49\u3002

        \u94fe\u8868\u5728\u521d\u59cb\u5316\u540e\uff0c\u4ecd\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u5bf9\u5176\u957f\u5ea6\u8fdb\u884c\u8c03\u6574\uff0c\u56e0\u6b64\u4e5f\u79f0\u201c\u52a8\u6001\u6570\u636e\u7ed3\u6784\u201d\u3002\u6570\u7ec4\u5728\u521d\u59cb\u5316\u540e\u957f\u5ea6\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u4e5f\u79f0\u201c\u9759\u6001\u6570\u636e\u7ed3\u6784\u201d\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6570\u7ec4\u53ef\u901a\u8fc7\u91cd\u65b0\u5206\u914d\u5185\u5b58\u5b9e\u73b0\u957f\u5ea6\u53d8\u5316\uff0c\u4ece\u800c\u5177\u5907\u4e00\u5b9a\u7684\u201c\u52a8\u6001\u6027\u201d\u3002

        Tip

        \u5982\u679c\u4f60\u611f\u89c9\u7269\u7406\u7ed3\u6784\u7406\u89e3\u8d77\u6765\u6709\u56f0\u96be\uff0c\u5efa\u8bae\u5148\u9605\u8bfb\u4e0b\u4e00\u7ae0\uff0c\u7136\u540e\u518d\u56de\u987e\u672c\u8282\u5185\u5bb9\u3002

        "},{"location":"chapter_data_structure/number_encoding/","title":"3.3 \u00a0 \u6570\u5b57\u7f16\u7801 *","text":"

        Tip

        \u5728\u672c\u4e66\u4e2d\uff0c\u6807\u9898\u5e26\u6709 * \u7b26\u53f7\u7684\u662f\u9009\u8bfb\u7ae0\u8282\u3002\u5982\u679c\u4f60\u65f6\u95f4\u6709\u9650\u6216\u611f\u5230\u7406\u89e3\u56f0\u96be\uff0c\u53ef\u4ee5\u5148\u8df3\u8fc7\uff0c\u7b49\u5b66\u5b8c\u5fc5\u8bfb\u7ae0\u8282\u540e\u518d\u5355\u72ec\u653b\u514b\u3002

        "},{"location":"chapter_data_structure/number_encoding/#331","title":"3.3.1 \u00a0 \u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801","text":"

        \u5728\u4e0a\u4e00\u8282\u7684\u8868\u683c\u4e2d\u6211\u4eec\u53d1\u73b0\uff0c\u6240\u6709\u6574\u6570\u7c7b\u578b\u80fd\u591f\u8868\u793a\u7684\u8d1f\u6570\u90fd\u6bd4\u6b63\u6570\u591a\u4e00\u4e2a\uff0c\u4f8b\u5982 byte \u7684\u53d6\u503c\u8303\u56f4\u662f \\([-128, 127]\\) \u3002\u8fd9\u4e2a\u73b0\u8c61\u6bd4\u8f83\u53cd\u76f4\u89c9\uff0c\u5b83\u7684\u5185\u5728\u539f\u56e0\u6d89\u53ca\u539f\u7801\u3001\u53cd\u7801\u3001\u8865\u7801\u7684\u76f8\u5173\u77e5\u8bc6\u3002

        \u9996\u5148\u9700\u8981\u6307\u51fa\uff0c\u6570\u5b57\u662f\u4ee5\u201c\u8865\u7801\u201d\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u4e2d\u7684\u3002\u5728\u5206\u6790\u8fd9\u6837\u505a\u7684\u539f\u56e0\u4e4b\u524d\uff0c\u9996\u5148\u7ed9\u51fa\u4e09\u8005\u7684\u5b9a\u4e49\u3002

        • \u539f\u7801\uff1a\u6211\u4eec\u5c06\u6570\u5b57\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u7684\u6700\u9ad8\u4f4d\u89c6\u4e3a\u7b26\u53f7\u4f4d\uff0c\u5176\u4e2d \\(0\\) \u8868\u793a\u6b63\u6570\uff0c\\(1\\) \u8868\u793a\u8d1f\u6570\uff0c\u5176\u4f59\u4f4d\u8868\u793a\u6570\u5b57\u7684\u503c\u3002
        • \u53cd\u7801\uff1a\u6b63\u6570\u7684\u53cd\u7801\u4e0e\u5176\u539f\u7801\u76f8\u540c\uff0c\u8d1f\u6570\u7684\u53cd\u7801\u662f\u5bf9\u5176\u539f\u7801\u9664\u7b26\u53f7\u4f4d\u5916\u7684\u6240\u6709\u4f4d\u53d6\u53cd\u3002
        • \u8865\u7801\uff1a\u6b63\u6570\u7684\u8865\u7801\u4e0e\u5176\u539f\u7801\u76f8\u540c\uff0c\u8d1f\u6570\u7684\u8865\u7801\u662f\u5728\u5176\u53cd\u7801\u7684\u57fa\u7840\u4e0a\u52a0 \\(1\\) \u3002

        \u56fe 3-4 \u5c55\u793a\u4e86\u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801\u4e4b\u95f4\u7684\u8f6c\u6362\u65b9\u6cd5\u3002

        \u56fe 3-4 \u00a0 \u539f\u7801\u3001\u53cd\u7801\u4e0e\u8865\u7801\u4e4b\u95f4\u7684\u76f8\u4e92\u8f6c\u6362

        \u539f\u7801\uff08sign-magnitude\uff09\u867d\u7136\u6700\u76f4\u89c2\uff0c\u4f46\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027\u3002\u4e00\u65b9\u9762\uff0c\u8d1f\u6570\u7684\u539f\u7801\u4e0d\u80fd\u76f4\u63a5\u7528\u4e8e\u8fd0\u7b97\u3002\u4f8b\u5982\u5728\u539f\u7801\u4e0b\u8ba1\u7b97 \\(1 + (-2)\\) \uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f \\(-3\\) \uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u5bf9\u7684\u3002

        \\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 + 1000 \\; 0010 \\newline & = 1000 \\; 0011 \\newline & \\rightarrow -3 \\end{aligned} \\]

        \u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u8ba1\u7b97\u673a\u5f15\u5165\u4e86\u53cd\u7801\uff081's complement\uff09\u3002\u5982\u679c\u6211\u4eec\u5148\u5c06\u539f\u7801\u8f6c\u6362\u4e3a\u53cd\u7801\uff0c\u5e76\u5728\u53cd\u7801\u4e0b\u8ba1\u7b97 \\(1 + (-2)\\) \uff0c\u6700\u540e\u5c06\u7ed3\u679c\u4ece\u53cd\u7801\u8f6c\u6362\u56de\u539f\u7801\uff0c\u5219\u53ef\u5f97\u5230\u6b63\u786e\u7ed3\u679c \\(-1\\) \u3002

        \\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 \\; \\text{(\u539f\u7801)} + 1000 \\; 0010 \\; \\text{(\u539f\u7801)} \\newline & = 0000 \\; 0001 \\; \\text{(\u53cd\u7801)} + 1111 \\; 1101 \\; \\text{(\u53cd\u7801)} \\newline & = 1111 \\; 1110 \\; \\text{(\u53cd\u7801)} \\newline & = 1000 \\; 0001 \\; \\text{(\u539f\u7801)} \\newline & \\rightarrow -1 \\end{aligned} \\]

        \u53e6\u4e00\u65b9\u9762\uff0c\u6570\u5b57\u96f6\u7684\u539f\u7801\u6709 \\(+0\\) \u548c \\(-0\\) \u4e24\u79cd\u8868\u793a\u65b9\u5f0f\u3002\u8fd9\u610f\u5473\u7740\u6570\u5b57\u96f6\u5bf9\u5e94\u4e24\u4e2a\u4e0d\u540c\u7684\u4e8c\u8fdb\u5236\u7f16\u7801\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5e26\u6765\u6b67\u4e49\u3002\u6bd4\u5982\u5728\u6761\u4ef6\u5224\u65ad\u4e2d\uff0c\u5982\u679c\u6ca1\u6709\u533a\u5206\u6b63\u96f6\u548c\u8d1f\u96f6\uff0c\u5219\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5224\u65ad\u7ed3\u679c\u51fa\u9519\u3002\u800c\u5982\u679c\u6211\u4eec\u60f3\u5904\u7406\u6b63\u96f6\u548c\u8d1f\u96f6\u6b67\u4e49\uff0c\u5219\u9700\u8981\u5f15\u5165\u989d\u5916\u7684\u5224\u65ad\u64cd\u4f5c\uff0c\u8fd9\u53ef\u80fd\u4f1a\u964d\u4f4e\u8ba1\u7b97\u673a\u7684\u8fd0\u7b97\u6548\u7387\u3002

        \\[ \\begin{aligned} +0 & \\rightarrow 0000 \\; 0000 \\newline -0 & \\rightarrow 1000 \\; 0000 \\end{aligned} \\]

        \u4e0e\u539f\u7801\u4e00\u6837\uff0c\u53cd\u7801\u4e5f\u5b58\u5728\u6b63\u8d1f\u96f6\u6b67\u4e49\u95ee\u9898\uff0c\u56e0\u6b64\u8ba1\u7b97\u673a\u8fdb\u4e00\u6b65\u5f15\u5165\u4e86\u8865\u7801\uff082's complement\uff09\u3002\u6211\u4eec\u5148\u6765\u89c2\u5bdf\u4e00\u4e0b\u8d1f\u96f6\u7684\u539f\u7801\u3001\u53cd\u7801\u3001\u8865\u7801\u7684\u8f6c\u6362\u8fc7\u7a0b\uff1a

        \\[ \\begin{aligned} -0 \\rightarrow \\; & 1000 \\; 0000 \\; \\text{(\u539f\u7801)} \\newline = \\; & 1111 \\; 1111 \\; \\text{(\u53cd\u7801)} \\newline = 1 \\; & 0000 \\; 0000 \\; \\text{(\u8865\u7801)} \\newline \\end{aligned} \\]

        \u5728\u8d1f\u96f6\u7684\u53cd\u7801\u57fa\u7840\u4e0a\u52a0 \\(1\\) \u4f1a\u4ea7\u751f\u8fdb\u4f4d\uff0c\u4f46 byte \u7c7b\u578b\u7684\u957f\u5ea6\u53ea\u6709 8 \u4f4d\uff0c\u56e0\u6b64\u6ea2\u51fa\u5230\u7b2c 9 \u4f4d\u7684 \\(1\\) \u4f1a\u88ab\u820d\u5f03\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8d1f\u96f6\u7684\u8865\u7801\u4e3a \\(0000 \\; 0000\\) \uff0c\u4e0e\u6b63\u96f6\u7684\u8865\u7801\u76f8\u540c\u3002\u8fd9\u610f\u5473\u7740\u5728\u8865\u7801\u8868\u793a\u4e2d\u53ea\u5b58\u5728\u4e00\u4e2a\u96f6\uff0c\u6b63\u8d1f\u96f6\u6b67\u4e49\u4ece\u800c\u5f97\u5230\u89e3\u51b3\u3002

        \u8fd8\u5269\u6700\u540e\u4e00\u4e2a\u7591\u60d1\uff1abyte \u7c7b\u578b\u7684\u53d6\u503c\u8303\u56f4\u662f \\([-128, 127]\\) \uff0c\u591a\u51fa\u6765\u7684\u4e00\u4e2a\u8d1f\u6570 \\(-128\\) \u662f\u5982\u4f55\u5f97\u5230\u7684\u5462\uff1f\u6211\u4eec\u6ce8\u610f\u5230\uff0c\u533a\u95f4 \\([-127, +127]\\) \u5185\u7684\u6240\u6709\u6574\u6570\u90fd\u6709\u5bf9\u5e94\u7684\u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801\uff0c\u5e76\u4e14\u539f\u7801\u548c\u8865\u7801\u4e4b\u95f4\u53ef\u4ee5\u4e92\u76f8\u8f6c\u6362\u3002

        \u7136\u800c\uff0c\u8865\u7801 \\(1000 \\; 0000\\) \u662f\u4e00\u4e2a\u4f8b\u5916\uff0c\u5b83\u5e76\u6ca1\u6709\u5bf9\u5e94\u7684\u539f\u7801\u3002\u6839\u636e\u8f6c\u6362\u65b9\u6cd5\uff0c\u6211\u4eec\u5f97\u5230\u8be5\u8865\u7801\u7684\u539f\u7801\u4e3a \\(0000 \\; 0000\\) \u3002\u8fd9\u663e\u7136\u662f\u77db\u76fe\u7684\uff0c\u56e0\u4e3a\u8be5\u539f\u7801\u8868\u793a\u6570\u5b57 \\(0\\) \uff0c\u5b83\u7684\u8865\u7801\u5e94\u8be5\u662f\u81ea\u8eab\u3002\u8ba1\u7b97\u673a\u89c4\u5b9a\u8fd9\u4e2a\u7279\u6b8a\u7684\u8865\u7801 \\(1000 \\; 0000\\) \u4ee3\u8868 \\(-128\\) \u3002\u5b9e\u9645\u4e0a\uff0c\\((-1) + (-127)\\) \u5728\u8865\u7801\u4e0b\u7684\u8ba1\u7b97\u7ed3\u679c\u5c31\u662f \\(-128\\) \u3002

        \\[ \\begin{aligned} & (-127) + (-1) \\newline & \\rightarrow 1111 \\; 1111 \\; \\text{(\u539f\u7801)} + 1000 \\; 0001 \\; \\text{(\u539f\u7801)} \\newline & = 1000 \\; 0000 \\; \\text{(\u53cd\u7801)} + 1111 \\; 1110 \\; \\text{(\u53cd\u7801)} \\newline & = 1000 \\; 0001 \\; \\text{(\u8865\u7801)} + 1111 \\; 1111 \\; \\text{(\u8865\u7801)} \\newline & = 1000 \\; 0000 \\; \\text{(\u8865\u7801)} \\newline & \\rightarrow -128 \\end{aligned} \\]

        \u4f60\u53ef\u80fd\u5df2\u7ecf\u53d1\u73b0\u4e86\uff0c\u4e0a\u8ff0\u6240\u6709\u8ba1\u7b97\u90fd\u662f\u52a0\u6cd5\u8fd0\u7b97\u3002\u8fd9\u6697\u793a\u7740\u4e00\u4e2a\u91cd\u8981\u4e8b\u5b9e\uff1a\u8ba1\u7b97\u673a\u5185\u90e8\u7684\u786c\u4ef6\u7535\u8def\u4e3b\u8981\u662f\u57fa\u4e8e\u52a0\u6cd5\u8fd0\u7b97\u8bbe\u8ba1\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u52a0\u6cd5\u8fd0\u7b97\u76f8\u5bf9\u4e8e\u5176\u4ed6\u8fd0\u7b97\uff08\u6bd4\u5982\u4e58\u6cd5\u3001\u9664\u6cd5\u548c\u51cf\u6cd5\uff09\u6765\u8bf4\uff0c\u786c\u4ef6\u5b9e\u73b0\u8d77\u6765\u66f4\u7b80\u5355\uff0c\u66f4\u5bb9\u6613\u8fdb\u884c\u5e76\u884c\u5316\u5904\u7406\uff0c\u8fd0\u7b97\u901f\u5ea6\u66f4\u5feb\u3002

        \u8bf7\u6ce8\u610f\uff0c\u8fd9\u5e76\u4e0d\u610f\u5473\u7740\u8ba1\u7b97\u673a\u53ea\u80fd\u505a\u52a0\u6cd5\u3002\u901a\u8fc7\u5c06\u52a0\u6cd5\u4e0e\u4e00\u4e9b\u57fa\u672c\u903b\u8f91\u8fd0\u7b97\u7ed3\u5408\uff0c\u8ba1\u7b97\u673a\u80fd\u591f\u5b9e\u73b0\u5404\u79cd\u5176\u4ed6\u7684\u6570\u5b66\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u51cf\u6cd5 \\(a - b\\) \u53ef\u4ee5\u8f6c\u6362\u4e3a\u8ba1\u7b97\u52a0\u6cd5 \\(a + (-b)\\) \uff1b\u8ba1\u7b97\u4e58\u6cd5\u548c\u9664\u6cd5\u53ef\u4ee5\u8f6c\u6362\u4e3a\u8ba1\u7b97\u591a\u6b21\u52a0\u6cd5\u6216\u51cf\u6cd5\u3002

        \u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u603b\u7ed3\u51fa\u8ba1\u7b97\u673a\u4f7f\u7528\u8865\u7801\u7684\u539f\u56e0\uff1a\u57fa\u4e8e\u8865\u7801\u8868\u793a\uff0c\u8ba1\u7b97\u673a\u53ef\u4ee5\u7528\u540c\u6837\u7684\u7535\u8def\u548c\u64cd\u4f5c\u6765\u5904\u7406\u6b63\u6570\u548c\u8d1f\u6570\u7684\u52a0\u6cd5\uff0c\u4e0d\u9700\u8981\u8bbe\u8ba1\u7279\u6b8a\u7684\u786c\u4ef6\u7535\u8def\u6765\u5904\u7406\u51cf\u6cd5\uff0c\u5e76\u4e14\u65e0\u987b\u7279\u522b\u5904\u7406\u6b63\u8d1f\u96f6\u7684\u6b67\u4e49\u95ee\u9898\u3002\u8fd9\u5927\u5927\u7b80\u5316\u4e86\u786c\u4ef6\u8bbe\u8ba1\uff0c\u63d0\u9ad8\u4e86\u8fd0\u7b97\u6548\u7387\u3002

        \u8865\u7801\u7684\u8bbe\u8ba1\u975e\u5e38\u7cbe\u5999\uff0c\u56e0\u7bc7\u5e45\u5173\u7cfb\u6211\u4eec\u5c31\u5148\u4ecb\u7ecd\u5230\u8fd9\u91cc\uff0c\u5efa\u8bae\u6709\u5174\u8da3\u7684\u8bfb\u8005\u8fdb\u4e00\u6b65\u6df1\u5165\u4e86\u89e3\u3002

        "},{"location":"chapter_data_structure/number_encoding/#332","title":"3.3.2 \u00a0 \u6d6e\u70b9\u6570\u7f16\u7801","text":"

        \u7ec6\u5fc3\u7684\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\uff1aint \u548c float \u957f\u5ea6\u76f8\u540c\uff0c\u90fd\u662f 4 \u5b57\u8282 \uff0c\u4f46\u4e3a\u4ec0\u4e48 float \u7684\u53d6\u503c\u8303\u56f4\u8fdc\u5927\u4e8e int \uff1f\u8fd9\u975e\u5e38\u53cd\u76f4\u89c9\uff0c\u56e0\u4e3a\u6309\u7406\u8bf4 float \u9700\u8981\u8868\u793a\u5c0f\u6570\uff0c\u53d6\u503c\u8303\u56f4\u5e94\u8be5\u53d8\u5c0f\u624d\u5bf9\u3002

        \u5b9e\u9645\u4e0a\uff0c\u8fd9\u662f\u56e0\u4e3a\u6d6e\u70b9\u6570 float \u91c7\u7528\u4e86\u4e0d\u540c\u7684\u8868\u793a\u65b9\u5f0f\u3002\u8bb0\u4e00\u4e2a 32 \u6bd4\u7279\u957f\u5ea6\u7684\u4e8c\u8fdb\u5236\u6570\u4e3a\uff1a

        \\[ b_{31} b_{30} b_{29} \\ldots b_2 b_1 b_0 \\]

        \u6839\u636e IEEE 754 \u6807\u51c6\uff0c32-bit \u957f\u5ea6\u7684 float \u7531\u4ee5\u4e0b\u4e09\u4e2a\u90e8\u5206\u6784\u6210\u3002

        • \u7b26\u53f7\u4f4d \\(\\mathrm{S}\\) \uff1a\u5360 1 \u4f4d \uff0c\u5bf9\u5e94 \\(b_{31}\\) \u3002
        • \u6307\u6570\u4f4d \\(\\mathrm{E}\\) \uff1a\u5360 8 \u4f4d \uff0c\u5bf9\u5e94 \\(b_{30} b_{29} \\ldots b_{23}\\) \u3002
        • \u5206\u6570\u4f4d \\(\\mathrm{N}\\) \uff1a\u5360 23 \u4f4d \uff0c\u5bf9\u5e94 \\(b_{22} b_{21} \\ldots b_0\\) \u3002

        \u4e8c\u8fdb\u5236\u6570 float \u5bf9\u5e94\u503c\u7684\u8ba1\u7b97\u65b9\u6cd5\u4e3a\uff1a

        \\[ \\text {val} = (-1)^{b_{31}} \\times 2^{\\left(b_{30} b_{29} \\ldots b_{23}\\right)_2-127} \\times\\left(1 . b_{22} b_{21} \\ldots b_0\\right)_2 \\]

        \u8f6c\u5316\u5230\u5341\u8fdb\u5236\u4e0b\u7684\u8ba1\u7b97\u516c\u5f0f\u4e3a\uff1a

        \\[ \\text {val}=(-1)^{\\mathrm{S}} \\times 2^{\\mathrm{E} -127} \\times (1 + \\mathrm{N}) \\]

        \u5176\u4e2d\u5404\u9879\u7684\u53d6\u503c\u8303\u56f4\u4e3a\uff1a

        \\[ \\begin{aligned} \\mathrm{S} \\in & \\{ 0, 1\\}, \\quad \\mathrm{E} \\in \\{ 1, 2, \\dots, 254 \\} \\newline (1 + \\mathrm{N}) = & (1 + \\sum_{i=1}^{23} b_{23-i} 2^{-i}) \\subset [1, 2 - 2^{-23}] \\end{aligned} \\]

        \u56fe 3-5 \u00a0 IEEE 754 \u6807\u51c6\u4e0b\u7684 float \u7684\u8ba1\u7b97\u793a\u4f8b

        \u89c2\u5bdf\u56fe 3-5 \uff0c\u7ed9\u5b9a\u4e00\u4e2a\u793a\u4f8b\u6570\u636e \\(\\mathrm{S} = 0\\) \uff0c \\(\\mathrm{E} = 124\\) \uff0c\\(\\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\\) \uff0c\u5219\u6709\uff1a

        \\[ \\text { val } = (-1)^0 \\times 2^{124 - 127} \\times (1 + 0.375) = 0.171875 \\]

        \u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u56de\u7b54\u6700\u521d\u7684\u95ee\u9898\uff1afloat \u7684\u8868\u793a\u65b9\u5f0f\u5305\u542b\u6307\u6570\u4f4d\uff0c\u5bfc\u81f4\u5176\u53d6\u503c\u8303\u56f4\u8fdc\u5927\u4e8e int \u3002\u6839\u636e\u4ee5\u4e0a\u8ba1\u7b97\uff0cfloat \u53ef\u8868\u793a\u7684\u6700\u5927\u6b63\u6570\u4e3a \\(2^{254 - 127} \\times (2 - 2^{-23}) \\approx 3.4 \\times 10^{38}\\) \uff0c\u5207\u6362\u7b26\u53f7\u4f4d\u4fbf\u53ef\u5f97\u5230\u6700\u5c0f\u8d1f\u6570\u3002

        \u5c3d\u7ba1\u6d6e\u70b9\u6570 float \u6269\u5c55\u4e86\u53d6\u503c\u8303\u56f4\uff0c\u4f46\u5176\u526f\u4f5c\u7528\u662f\u727a\u7272\u4e86\u7cbe\u5ea6\u3002\u6574\u6570\u7c7b\u578b int \u5c06\u5168\u90e8 32 \u6bd4\u7279\u7528\u4e8e\u8868\u793a\u6570\u5b57\uff0c\u6570\u5b57\u662f\u5747\u5300\u5206\u5e03\u7684\uff1b\u800c\u7531\u4e8e\u6307\u6570\u4f4d\u7684\u5b58\u5728\uff0c\u6d6e\u70b9\u6570 float \u7684\u6570\u503c\u8d8a\u5927\uff0c\u76f8\u90bb\u4e24\u4e2a\u6570\u5b57\u4e4b\u95f4\u7684\u5dee\u503c\u5c31\u4f1a\u8d8b\u5411\u8d8a\u5927\u3002

        \u5982\u8868 3-2 \u6240\u793a\uff0c\u6307\u6570\u4f4d \\(\\mathrm{E} = 0\\) \u548c \\(\\mathrm{E} = 255\\) \u5177\u6709\u7279\u6b8a\u542b\u4e49\uff0c\u7528\u4e8e\u8868\u793a\u96f6\u3001\u65e0\u7a77\u5927\u3001\\(\\mathrm{NaN}\\) \u7b49\u3002

        \u8868 3-2 \u00a0 \u6307\u6570\u4f4d\u542b\u4e49

        \u6307\u6570\u4f4d E \u5206\u6570\u4f4d \\(\\mathrm{N} = 0\\) \u5206\u6570\u4f4d \\(\\mathrm{N} \\ne 0\\) \u8ba1\u7b97\u516c\u5f0f \\(0\\) \\(\\pm 0\\) \u6b21\u6b63\u89c4\u6570 \\((-1)^{\\mathrm{S}} \\times 2^{-126} \\times (0.\\mathrm{N})\\) \\(1, 2, \\dots, 254\\) \u6b63\u89c4\u6570 \u6b63\u89c4\u6570 \\((-1)^{\\mathrm{S}} \\times 2^{(\\mathrm{E} -127)} \\times (1.\\mathrm{N})\\) \\(255\\) \\(\\pm \\infty\\) \\(\\mathrm{NaN}\\)

        \u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6b21\u6b63\u89c4\u6570\u663e\u8457\u63d0\u5347\u4e86\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u3002\u6700\u5c0f\u6b63\u6b63\u89c4\u6570\u4e3a \\(2^{-126}\\) \uff0c\u6700\u5c0f\u6b63\u6b21\u6b63\u89c4\u6570\u4e3a \\(2^{-126} \\times 2^{-23}\\) \u3002

        \u53cc\u7cbe\u5ea6 double \u4e5f\u91c7\u7528\u7c7b\u4f3c\u4e8e float \u7684\u8868\u793a\u65b9\u6cd5\uff0c\u5728\u6b64\u4e0d\u505a\u8d58\u8ff0\u3002

        "},{"location":"chapter_data_structure/summary/","title":"3.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_data_structure/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u4ece\u903b\u8f91\u7ed3\u6784\u548c\u7269\u7406\u7ed3\u6784\u4e24\u4e2a\u89d2\u5ea6\u8fdb\u884c\u5206\u7c7b\u3002\u903b\u8f91\u7ed3\u6784\u63cf\u8ff0\u4e86\u6570\u636e\u5143\u7d20\u4e4b\u95f4\u7684\u903b\u8f91\u5173\u7cfb\uff0c\u800c\u7269\u7406\u7ed3\u6784\u63cf\u8ff0\u4e86\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u65b9\u5f0f\u3002
        • \u5e38\u89c1\u7684\u903b\u8f91\u7ed3\u6784\u5305\u62ec\u7ebf\u6027\u3001\u6811\u72b6\u548c\u7f51\u72b6\u7b49\u3002\u901a\u5e38\u6211\u4eec\u6839\u636e\u903b\u8f91\u7ed3\u6784\u5c06\u6570\u636e\u7ed3\u6784\u5206\u4e3a\u7ebf\u6027\uff08\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\uff09\u548c\u975e\u7ebf\u6027\uff08\u6811\u3001\u56fe\u3001\u5806\uff09\u4e24\u79cd\u3002\u54c8\u5e0c\u8868\u7684\u5b9e\u73b0\u53ef\u80fd\u540c\u65f6\u5305\u542b\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u548c\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002
        • \u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u6570\u636e\u88ab\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u3002\u6bcf\u4e2a\u5185\u5b58\u7a7a\u95f4\u90fd\u62e5\u6709\u5bf9\u5e94\u7684\u5185\u5b58\u5730\u5740\uff0c\u7a0b\u5e8f\u901a\u8fc7\u8fd9\u4e9b\u5185\u5b58\u5730\u5740\u8bbf\u95ee\u6570\u636e\u3002
        • \u7269\u7406\u7ed3\u6784\u4e3b\u8981\u5206\u4e3a\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\uff08\u6570\u7ec4\uff09\u548c\u5206\u6563\u7a7a\u95f4\u5b58\u50a8\uff08\u94fe\u8868\uff09\u3002\u6240\u6709\u6570\u636e\u7ed3\u6784\u90fd\u662f\u7531\u6570\u7ec4\u3001\u94fe\u8868\u6216\u4e24\u8005\u7684\u7ec4\u5408\u5b9e\u73b0\u7684\u3002
        • \u8ba1\u7b97\u673a\u4e2d\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u5305\u62ec\u6574\u6570 byte\u3001short\u3001int\u3001long \uff0c\u6d6e\u70b9\u6570 float\u3001double \uff0c\u5b57\u7b26 char \u548c\u5e03\u5c14 bool \u3002\u5b83\u4eec\u7684\u53d6\u503c\u8303\u56f4\u53d6\u51b3\u4e8e\u5360\u7528\u7a7a\u95f4\u5927\u5c0f\u548c\u8868\u793a\u65b9\u5f0f\u3002
        • \u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801\u662f\u5728\u8ba1\u7b97\u673a\u4e2d\u7f16\u7801\u6570\u5b57\u7684\u4e09\u79cd\u65b9\u6cd5\uff0c\u5b83\u4eec\u4e4b\u95f4\u53ef\u4ee5\u76f8\u4e92\u8f6c\u6362\u3002\u6574\u6570\u7684\u539f\u7801\u7684\u6700\u9ad8\u4f4d\u662f\u7b26\u53f7\u4f4d\uff0c\u5176\u4f59\u4f4d\u662f\u6570\u5b57\u7684\u503c\u3002
        • \u6574\u6570\u5728\u8ba1\u7b97\u673a\u4e2d\u662f\u4ee5\u8865\u7801\u7684\u5f62\u5f0f\u5b58\u50a8\u7684\u3002\u5728\u8865\u7801\u8868\u793a\u4e0b\uff0c\u8ba1\u7b97\u673a\u53ef\u4ee5\u5bf9\u6b63\u6570\u548c\u8d1f\u6570\u7684\u52a0\u6cd5\u4e00\u89c6\u540c\u4ec1\uff0c\u4e0d\u9700\u8981\u4e3a\u51cf\u6cd5\u64cd\u4f5c\u5355\u72ec\u8bbe\u8ba1\u7279\u6b8a\u7684\u786c\u4ef6\u7535\u8def\uff0c\u5e76\u4e14\u4e0d\u5b58\u5728\u6b63\u8d1f\u96f6\u6b67\u4e49\u7684\u95ee\u9898\u3002
        • \u6d6e\u70b9\u6570\u7684\u7f16\u7801\u7531 1 \u4f4d\u7b26\u53f7\u4f4d\u30018 \u4f4d\u6307\u6570\u4f4d\u548c 23 \u4f4d\u5206\u6570\u4f4d\u6784\u6210\u3002\u7531\u4e8e\u5b58\u5728\u6307\u6570\u4f4d\uff0c\u56e0\u6b64\u6d6e\u70b9\u6570\u7684\u53d6\u503c\u8303\u56f4\u8fdc\u5927\u4e8e\u6574\u6570\uff0c\u4ee3\u4ef7\u662f\u727a\u7272\u4e86\u7cbe\u5ea6\u3002
        • ASCII \u7801\u662f\u6700\u65e9\u51fa\u73b0\u7684\u82f1\u6587\u5b57\u7b26\u96c6\uff0c\u957f\u5ea6\u4e3a 1 \u5b57\u8282\uff0c\u5171\u6536\u5f55 127 \u4e2a\u5b57\u7b26\u3002GBK \u5b57\u7b26\u96c6\u662f\u5e38\u7528\u7684\u4e2d\u6587\u5b57\u7b26\u96c6\uff0c\u5171\u6536\u5f55\u4e24\u4e07\u591a\u4e2a\u6c49\u5b57\u3002Unicode \u81f4\u529b\u4e8e\u63d0\u4f9b\u4e00\u4e2a\u5b8c\u6574\u7684\u5b57\u7b26\u96c6\u6807\u51c6\uff0c\u6536\u5f55\u4e16\u754c\u4e0a\u5404\u79cd\u8bed\u8a00\u7684\u5b57\u7b26\uff0c\u4ece\u800c\u89e3\u51b3\u7531\u4e8e\u5b57\u7b26\u7f16\u7801\u65b9\u6cd5\u4e0d\u4e00\u81f4\u800c\u5bfc\u81f4\u7684\u4e71\u7801\u95ee\u9898\u3002
        • UTF-8 \u662f\u6700\u53d7\u6b22\u8fce\u7684 Unicode \u7f16\u7801\u65b9\u6cd5\uff0c\u901a\u7528\u6027\u975e\u5e38\u597d\u3002\u5b83\u662f\u4e00\u79cd\u53d8\u957f\u7684\u7f16\u7801\u65b9\u6cd5\uff0c\u5177\u6709\u5f88\u597d\u7684\u6269\u5c55\u6027\uff0c\u6709\u6548\u63d0\u5347\u4e86\u5b58\u50a8\u7a7a\u95f4\u7684\u4f7f\u7528\u6548\u7387\u3002UTF-16 \u548c UTF-32 \u662f\u7b49\u957f\u7684\u7f16\u7801\u65b9\u6cd5\u3002\u5728\u7f16\u7801\u4e2d\u6587\u65f6\uff0cUTF-16 \u5360\u7528\u7684\u7a7a\u95f4\u6bd4 UTF-8 \u66f4\u5c0f\u3002Java \u548c C# \u7b49\u7f16\u7a0b\u8bed\u8a00\u9ed8\u8ba4\u4f7f\u7528 UTF-16 \u7f16\u7801\u3002
        "},{"location":"chapter_data_structure/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u4e3a\u4ec0\u4e48\u54c8\u5e0c\u8868\u540c\u65f6\u5305\u542b\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u548c\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff1f

        \u54c8\u5e0c\u8868\u5e95\u5c42\u662f\u6570\u7ec4\uff0c\u800c\u4e3a\u4e86\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u4f7f\u7528\u201c\u94fe\u5f0f\u5730\u5740\u201d\uff08\u540e\u7eed\u201c\u54c8\u5e0c\u51b2\u7a81\u201d\u7ae0\u8282\u4f1a\u8bb2\uff09\uff1a\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u6876\u6307\u5411\u4e00\u4e2a\u94fe\u8868\uff0c\u5f53\u94fe\u8868\u957f\u5ea6\u8d85\u8fc7\u4e00\u5b9a\u9608\u503c\u65f6\uff0c\u53c8\u53ef\u80fd\u88ab\u8f6c\u5316\u4e3a\u6811\uff08\u901a\u5e38\u4e3a\u7ea2\u9ed1\u6811\uff09\u3002

        \u4ece\u5b58\u50a8\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u54c8\u5e0c\u8868\u7684\u5e95\u5c42\u662f\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e00\u4e2a\u6876\u69fd\u4f4d\u53ef\u80fd\u5305\u542b\u4e00\u4e2a\u503c\uff0c\u4e5f\u53ef\u80fd\u5305\u542b\u4e00\u4e2a\u94fe\u8868\u6216\u4e00\u68f5\u6811\u3002\u56e0\u6b64\uff0c\u54c8\u5e0c\u8868\u53ef\u80fd\u540c\u65f6\u5305\u542b\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff08\u6570\u7ec4\u3001\u94fe\u8868\uff09\u548c\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff08\u6811\uff09\u3002

        Q\uff1achar \u7c7b\u578b\u7684\u957f\u5ea6\u662f 1 \u5b57\u8282\u5417\uff1f

        char \u7c7b\u578b\u7684\u957f\u5ea6\u7531\u7f16\u7a0b\u8bed\u8a00\u91c7\u7528\u7684\u7f16\u7801\u65b9\u6cd5\u51b3\u5b9a\u3002\u4f8b\u5982\uff0cJava\u3001JavaScript\u3001TypeScript\u3001C# \u90fd\u91c7\u7528 UTF-16 \u7f16\u7801\uff08\u4fdd\u5b58 Unicode \u7801\u70b9\uff09\uff0c\u56e0\u6b64 char \u7c7b\u578b\u7684\u957f\u5ea6\u4e3a 2 \u5b57\u8282\u3002

        Q\uff1a\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u4e5f\u79f0\u201c\u9759\u6001\u6570\u636e\u7ed3\u6784\u201d \u662f\u5426\u6709\u6b67\u4e49\uff1f\u6808\u4e5f\u53ef\u4ee5\u8fdb\u884c\u51fa\u6808\u548c\u5165\u6808\u7b49\u64cd\u4f5c\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u90fd\u662f\u201c\u52a8\u6001\u201d\u7684\u3002

        \u6808\u786e\u5b9e\u53ef\u4ee5\u5b9e\u73b0\u52a8\u6001\u7684\u6570\u636e\u64cd\u4f5c\uff0c\u4f46\u6570\u636e\u7ed3\u6784\u4ecd\u7136\u662f\u201c\u9759\u6001\u201d\uff08\u957f\u5ea6\u4e0d\u53ef\u53d8\uff09\u7684\u3002\u5c3d\u7ba1\u57fa\u4e8e\u6570\u7ec4\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u52a8\u6001\u5730\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\uff0c\u4f46\u5b83\u4eec\u7684\u5bb9\u91cf\u662f\u56fa\u5b9a\u7684\u3002\u5982\u679c\u6570\u636e\u91cf\u8d85\u51fa\u4e86\u9884\u5206\u914d\u7684\u5927\u5c0f\uff0c\u5c31\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u66f4\u5927\u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u65e7\u6570\u7ec4\u7684\u5185\u5bb9\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002

        Q\uff1a\u5728\u6784\u5efa\u6808\uff08\u961f\u5217\uff09\u7684\u65f6\u5019\uff0c\u672a\u6307\u5b9a\u5b83\u7684\u5927\u5c0f\uff0c\u4e3a\u4ec0\u4e48\u5b83\u4eec\u662f\u201c\u9759\u6001\u6570\u636e\u7ed3\u6784\u201d\u5462\uff1f

        \u5728\u9ad8\u7ea7\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u65e0\u987b\u4eba\u5de5\u6307\u5b9a\u6808\uff08\u961f\u5217\uff09\u7684\u521d\u59cb\u5bb9\u91cf\uff0c\u8fd9\u4e2a\u5de5\u4f5c\u7531\u7c7b\u5185\u90e8\u81ea\u52a8\u5b8c\u6210\u3002\u4f8b\u5982\uff0cJava \u7684 ArrayList \u7684\u521d\u59cb\u5bb9\u91cf\u901a\u5e38\u4e3a 10\u3002\u53e6\u5916\uff0c\u6269\u5bb9\u64cd\u4f5c\u4e5f\u662f\u81ea\u52a8\u5b9e\u73b0\u7684\u3002\u8be6\u89c1\u540e\u7eed\u7684\u201c\u5217\u8868\u201d\u7ae0\u8282\u3002

        Q\uff1a\u539f\u7801\u8f6c\u8865\u7801\u7684\u65b9\u6cd5\u662f\u201c\u5148\u53d6\u53cd\u540e\u52a0 1\u201d\uff0c\u90a3\u4e48\u8865\u7801\u8f6c\u539f\u7801\u5e94\u8be5\u662f\u9006\u8fd0\u7b97\u201c\u5148\u51cf 1 \u540e\u53d6\u53cd\u201d\uff0c\u800c\u8865\u7801\u8f6c\u539f\u7801\u4e5f\u4e00\u6837\u53ef\u4ee5\u901a\u8fc7\u201c\u5148\u53d6\u53cd\u540e\u52a0 1\u201d\u5f97\u5230\uff0c\u8fd9\u662f\u4e3a\u4ec0\u4e48\u5462\uff1f

        \u8fd9\u662f\u56e0\u4e3a\u539f\u7801\u548c\u8865\u7801\u7684\u76f8\u4e92\u8f6c\u6362\u5b9e\u9645\u4e0a\u662f\u8ba1\u7b97\u201c\u8865\u6570\u201d\u7684\u8fc7\u7a0b\u3002\u6211\u4eec\u5148\u7ed9\u51fa\u8865\u6570\u7684\u5b9a\u4e49\uff1a\u5047\u8bbe \\(a + b = c\\) \uff0c\u90a3\u4e48\u6211\u4eec\u79f0 \\(a\\) \u662f \\(b\\) \u5230 \\(c\\) \u7684\u8865\u6570\uff0c\u53cd\u4e4b\u4e5f\u79f0 \\(b\\) \u662f \\(a\\) \u5230 \\(c\\) \u7684\u8865\u6570\u3002

        \u7ed9\u5b9a\u4e00\u4e2a \\(n = 4\\) \u4f4d\u957f\u5ea6\u7684\u4e8c\u8fdb\u5236\u6570 \\(0010\\) \uff0c\u5982\u679c\u5c06\u8fd9\u4e2a\u6570\u5b57\u770b\u4f5c\u539f\u7801\uff08\u4e0d\u8003\u8651\u7b26\u53f7\u4f4d\uff09\uff0c\u90a3\u4e48\u5b83\u7684\u8865\u7801\u9700\u901a\u8fc7\u201c\u5148\u53d6\u53cd\u540e\u52a0 1\u201d\u5f97\u5230\uff1a

        \\[ 0010 \\rightarrow 1101 \\rightarrow 1110 \\]

        \u6211\u4eec\u4f1a\u53d1\u73b0\uff0c\u539f\u7801\u548c\u8865\u7801\u7684\u548c\u662f \\(0010 + 1110 = 10000\\) \uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u8865\u7801 \\(1110\\) \u662f\u539f\u7801 \\(0010\\) \u5230 \\(10000\\) \u7684\u201c\u8865\u6570\u201d\u3002\u8fd9\u610f\u5473\u7740\u4e0a\u8ff0\u201c\u5148\u53d6\u53cd\u540e\u52a0 1\u201d\u5b9e\u9645\u4e0a\u662f\u8ba1\u7b97\u5230 \\(10000\\) \u7684\u8865\u6570\u7684\u8fc7\u7a0b\u3002

        \u90a3\u4e48\uff0c\u8865\u7801 \\(1110\\) \u5230 \\(10000\\) \u7684\u201c\u8865\u6570\u201d\u662f\u591a\u5c11\u5462\uff1f\u6211\u4eec\u4f9d\u7136\u53ef\u4ee5\u7528\u201c\u5148\u53d6\u53cd\u540e\u52a0 1\u201d\u5f97\u5230\u5b83\uff1a

        \\[ 1110 \\rightarrow 0001 \\rightarrow 0010 \\]

        \u6362\u53e5\u8bdd\u8bf4\uff0c\u539f\u7801\u548c\u8865\u7801\u4e92\u4e3a\u5bf9\u65b9\u5230 \\(10000\\) \u7684\u201c\u8865\u6570\u201d\uff0c\u56e0\u6b64\u201c\u539f\u7801\u8f6c\u8865\u7801\u201d\u548c\u201c\u8865\u7801\u8f6c\u539f\u7801\u201d\u53ef\u4ee5\u7528\u76f8\u540c\u7684\u64cd\u4f5c\uff08\u5148\u53d6\u53cd\u540e\u52a0 1 \uff09\u5b9e\u73b0\u3002

        \u5f53\u7136\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u7528\u9006\u8fd0\u7b97\u6765\u6c42\u8865\u7801 \\(1110\\) \u7684\u539f\u7801\uff0c\u5373\u201c\u5148\u51cf 1 \u540e\u53d6\u53cd\u201d\uff1a

        \\[ 1110 \\rightarrow 1101 \\rightarrow 0010 \\]

        \u603b\u7ed3\u6765\u770b\uff0c\u201c\u5148\u53d6\u53cd\u540e\u52a0 1\u201d\u548c\u201c\u5148\u51cf 1 \u540e\u53d6\u53cd\u201d\u8fd9\u4e24\u79cd\u8fd0\u7b97\u90fd\u662f\u5728\u8ba1\u7b97\u5230 \\(10000\\) \u7684\u8865\u6570\uff0c\u5b83\u4eec\u662f\u7b49\u4ef7\u7684\u3002

        \u672c\u8d28\u4e0a\u770b\uff0c\u201c\u53d6\u53cd\u201d\u64cd\u4f5c\u5b9e\u9645\u4e0a\u662f\u6c42\u5230 \\(1111\\) \u7684\u8865\u6570\uff08\u56e0\u4e3a\u6052\u6709 \u539f\u7801 + \u53cd\u7801 = 1111\uff09\uff1b\u800c\u5728\u53cd\u7801\u57fa\u7840\u4e0a\u518d\u52a0 1 \u5f97\u5230\u7684\u8865\u7801\uff0c\u5c31\u662f\u5230 \\(10000\\) \u7684\u8865\u6570\u3002

        \u4e0a\u8ff0\u4ee5 \\(n = 4\\) \u4e3a\u4f8b\uff0c\u5176\u53ef\u88ab\u63a8\u5e7f\u81f3\u4efb\u610f\u4f4d\u6570\u7684\u4e8c\u8fdb\u5236\u6570\u3002

        "},{"location":"chapter_divide_and_conquer/","title":"\u7b2c 12 \u7ae0 \u00a0 \u5206\u6cbb","text":"

        Abstract

        \u96be\u9898\u88ab\u9010\u5c42\u62c6\u89e3\uff0c\u6bcf\u4e00\u6b21\u7684\u62c6\u89e3\u90fd\u4f7f\u5b83\u53d8\u5f97\u66f4\u4e3a\u7b80\u5355\u3002

        \u5206\u800c\u6cbb\u4e4b\u63ed\u793a\u4e86\u4e00\u4e2a\u91cd\u8981\u7684\u4e8b\u5b9e\uff1a\u4ece\u7b80\u5355\u505a\u8d77\uff0c\u4e00\u5207\u90fd\u4e0d\u518d\u590d\u6742\u3002

        "},{"location":"chapter_divide_and_conquer/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 12.1 \u00a0 \u5206\u6cbb\u7b97\u6cd5
        • 12.2 \u00a0 \u5206\u6cbb\u641c\u7d22\u7b56\u7565
        • 12.3 \u00a0 \u6784\u5efa\u6811\u95ee\u9898
        • 12.4 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898
        • 12.5 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_divide_and_conquer/binary_search_recur/","title":"12.2 \u00a0 \u5206\u6cbb\u641c\u7d22\u7b56\u7565","text":"

        \u6211\u4eec\u5df2\u7ecf\u5b66\u8fc7\uff0c\u641c\u7d22\u7b97\u6cd5\u5206\u4e3a\u4e24\u5927\u7c7b\u3002

        • \u66b4\u529b\u641c\u7d22\uff1a\u5b83\u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784\u5b9e\u73b0\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002
        • \u81ea\u9002\u5e94\u641c\u7d22\uff1a\u5b83\u5229\u7528\u7279\u6709\u7684\u6570\u636e\u7ec4\u7ec7\u5f62\u5f0f\u6216\u5148\u9a8c\u4fe1\u606f\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe\u5230 \\(O(\\log n)\\) \u751a\u81f3 \\(O(1)\\) \u3002

        \u5b9e\u9645\u4e0a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u7684\u641c\u7d22\u7b97\u6cd5\u901a\u5e38\u662f\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u5b9e\u73b0\u7684\uff0c\u4f8b\u5982\u4e8c\u5206\u67e5\u627e\u548c\u6811\u3002

        • \u4e8c\u5206\u67e5\u627e\u7684\u6bcf\u4e00\u6b65\u90fd\u5c06\u95ee\u9898\uff08\u5728\u6570\u7ec4\u4e2d\u641c\u7d22\u76ee\u6807\u5143\u7d20\uff09\u5206\u89e3\u4e3a\u4e00\u4e2a\u5c0f\u95ee\u9898\uff08\u5728\u6570\u7ec4\u7684\u4e00\u534a\u4e2d\u641c\u7d22\u76ee\u6807\u5143\u7d20\uff09\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4e00\u76f4\u6301\u7eed\u5230\u6570\u7ec4\u4e3a\u7a7a\u6216\u627e\u5230\u76ee\u6807\u5143\u7d20\u4e3a\u6b62\u3002
        • \u6811\u662f\u5206\u6cbb\u601d\u60f3\u7684\u4ee3\u8868\uff0c\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u3001AVL \u6811\u3001\u5806\u7b49\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u5404\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7686\u4e3a \\(O(\\log n)\\) \u3002

        \u4e8c\u5206\u67e5\u627e\u7684\u5206\u6cbb\u7b56\u7565\u5982\u4e0b\u6240\u793a\u3002

        • \u95ee\u9898\u53ef\u4ee5\u5206\u89e3\uff1a\u4e8c\u5206\u67e5\u627e\u9012\u5f52\u5730\u5c06\u539f\u95ee\u9898\uff08\u5728\u6570\u7ec4\u4e2d\u8fdb\u884c\u67e5\u627e\uff09\u5206\u89e3\u4e3a\u5b50\u95ee\u9898\uff08\u5728\u6570\u7ec4\u7684\u4e00\u534a\u4e2d\u8fdb\u884c\u67e5\u627e\uff09\uff0c\u8fd9\u662f\u901a\u8fc7\u6bd4\u8f83\u4e2d\u95f4\u5143\u7d20\u548c\u76ee\u6807\u5143\u7d20\u6765\u5b9e\u73b0\u7684\u3002
        • \u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff1a\u5728\u4e8c\u5206\u67e5\u627e\u4e2d\uff0c\u6bcf\u8f6e\u53ea\u5904\u7406\u4e00\u4e2a\u5b50\u95ee\u9898\uff0c\u5b83\u4e0d\u53d7\u5176\u4ed6\u5b50\u95ee\u9898\u7684\u5f71\u54cd\u3002
        • \u5b50\u95ee\u9898\u7684\u89e3\u65e0\u987b\u5408\u5e76\uff1a\u4e8c\u5206\u67e5\u627e\u65e8\u5728\u67e5\u627e\u4e00\u4e2a\u7279\u5b9a\u5143\u7d20\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u8fdb\u884c\u5408\u5e76\u3002\u5f53\u5b50\u95ee\u9898\u5f97\u5230\u89e3\u51b3\u65f6\uff0c\u539f\u95ee\u9898\u4e5f\u4f1a\u540c\u65f6\u5f97\u5230\u89e3\u51b3\u3002

        \u5206\u6cbb\u80fd\u591f\u63d0\u5347\u641c\u7d22\u6548\u7387\uff0c\u672c\u8d28\u4e0a\u662f\u56e0\u4e3a\u66b4\u529b\u641c\u7d22\u6bcf\u8f6e\u53ea\u80fd\u6392\u9664\u4e00\u4e2a\u9009\u9879\uff0c\u800c\u5206\u6cbb\u641c\u7d22\u6bcf\u8f6e\u53ef\u4ee5\u6392\u9664\u4e00\u534a\u9009\u9879\u3002

        "},{"location":"chapter_divide_and_conquer/binary_search_recur/#1","title":"1. \u00a0 \u57fa\u4e8e\u5206\u6cbb\u5b9e\u73b0\u4e8c\u5206\u67e5\u627e","text":"

        \u5728\u4e4b\u524d\u7684\u7ae0\u8282\u4e2d\uff0c\u4e8c\u5206\u67e5\u627e\u662f\u57fa\u4e8e\u9012\u63a8\uff08\u8fed\u4ee3\uff09\u5b9e\u73b0\u7684\u3002\u73b0\u5728\u6211\u4eec\u57fa\u4e8e\u5206\u6cbb\uff08\u9012\u5f52\uff09\u6765\u5b9e\u73b0\u5b83\u3002

        Question

        \u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6709\u5e8f\u6570\u7ec4 nums \uff0c\u5176\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u662f\u552f\u4e00\u7684\uff0c\u8bf7\u67e5\u627e\u5143\u7d20 target \u3002

        \u4ece\u5206\u6cbb\u89d2\u5ea6\uff0c\u6211\u4eec\u5c06\u641c\u7d22\u533a\u95f4 \\([i, j]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u8bb0\u4e3a \\(f(i, j)\\) \u3002

        \u4ee5\u539f\u95ee\u9898 \\(f(0, n-1)\\) \u4e3a\u8d77\u59cb\u70b9\uff0c\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u4e8c\u5206\u67e5\u627e\u3002

        1. \u8ba1\u7b97\u641c\u7d22\u533a\u95f4 \\([i, j]\\) \u7684\u4e2d\u70b9 \\(m\\) \uff0c\u6839\u636e\u5b83\u6392\u9664\u4e00\u534a\u641c\u7d22\u533a\u95f4\u3002
        2. \u9012\u5f52\u6c42\u89e3\u89c4\u6a21\u51cf\u5c0f\u4e00\u534a\u7684\u5b50\u95ee\u9898\uff0c\u53ef\u80fd\u4e3a \\(f(i, m-1)\\) \u6216 \\(f(m+1, j)\\) \u3002
        3. \u5faa\u73af\u7b2c 1. \u6b65\u548c\u7b2c 2. \u6b65\uff0c\u76f4\u81f3\u627e\u5230 target \u6216\u533a\u95f4\u4e3a\u7a7a\u65f6\u8fd4\u56de\u3002

        \u56fe 12-4 \u5c55\u793a\u4e86\u5728\u6570\u7ec4\u4e2d\u4e8c\u5206\u67e5\u627e\u5143\u7d20 \\(6\\) \u7684\u5206\u6cbb\u8fc7\u7a0b\u3002

        \u56fe 12-4 \u00a0 \u4e8c\u5206\u67e5\u627e\u7684\u5206\u6cbb\u8fc7\u7a0b

        \u5728\u5b9e\u73b0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 dfs() \u6765\u6c42\u89e3\u95ee\u9898 \\(f(i, j)\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_recur.py
        def dfs(nums: list[int], target: int, i: int, j: int) -> int:\n    \"\"\"\u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j)\"\"\"\n    # \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n    if i > j:\n        return -1\n    # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    m = (i + j) // 2\n    if nums[m] < target:\n        # \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n        return dfs(nums, target, m + 1, j)\n    elif nums[m] > target:\n        # \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n        return dfs(nums, target, i, m - 1)\n    else:\n        # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        return m\n\ndef binary_search(nums: list[int], target: int) -> int:\n    \"\"\"\u4e8c\u5206\u67e5\u627e\"\"\"\n    n = len(nums)\n    # \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n    return dfs(nums, target, 0, n - 1)\n
        binary_search_recur.cpp
        /* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(vector<int> &nums, int target, int i, int j) {\n    // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n    if (i > j) {\n        return -1;\n    }\n    // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    int m = (i + j) / 2;\n    if (nums[m] < target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        return m;\n    }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(vector<int> &nums, int target) {\n    int n = nums.size();\n    // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
        binary_search_recur.java
        /* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(int[] nums, int target, int i, int j) {\n    // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n    if (i > j) {\n        return -1;\n    }\n    // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    int m = (i + j) / 2;\n    if (nums[m] < target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        return m;\n    }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(int[] nums, int target) {\n    int n = nums.length;\n    // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
        binary_search_recur.cs
        /* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint DFS(int[] nums, int target, int i, int j) {\n    // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n    if (i > j) {\n        return -1;\n    }\n    // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    int m = (i + j) / 2;\n    if (nums[m] < target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n        return DFS(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n        return DFS(nums, target, i, m - 1);\n    } else {\n        // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        return m;\n    }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nint BinarySearch(int[] nums, int target) {\n    int n = nums.Length;\n    // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n    return DFS(nums, target, 0, n - 1);\n}\n
        binary_search_recur.go
        /* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunc dfs(nums []int, target, i, j int) int {\n    // \u5982\u679c\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u6ca1\u6709\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n    if i > j {\n        return -1\n    }\n    //    \u8ba1\u7b97\u7d22\u5f15\u4e2d\u70b9\n    m := i + ((j - i) >> 1)\n    //\u5224\u65ad\u4e2d\u70b9\u4e0e\u76ee\u6807\u5143\u7d20\u5927\u5c0f\n    if nums[m] < target {\n        // \u5c0f\u4e8e\u5219\u9012\u5f52\u53f3\u534a\u6570\u7ec4\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n        return dfs(nums, target, m+1, j)\n    } else if nums[m] > target {\n        // \u5927\u4e8e\u5219\u9012\u5f52\u5de6\u534a\u6570\u7ec4\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n        return dfs(nums, target, i, m-1)\n    } else {\n        // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        return m\n    }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nfunc binarySearch(nums []int, target int) int {\n    n := len(nums)\n    return dfs(nums, target, 0, n-1)\n}\n
        binary_search_recur.swift
        /* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunc dfs(nums: [Int], target: Int, i: Int, j: Int) -> Int {\n    // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n    if i > j {\n        return -1\n    }\n    // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    let m = (i + j) / 2\n    if nums[m] < target {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n        return dfs(nums: nums, target: target, i: m + 1, j: j)\n    } else if nums[m] > target {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n        return dfs(nums: nums, target: target, i: i, j: m - 1)\n    } else {\n        // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        return m\n    }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nfunc binarySearch(nums: [Int], target: Int) -> Int {\n    // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n    dfs(nums: nums, target: target, i: nums.startIndex, j: nums.endIndex - 1)\n}\n
        binary_search_recur.js
        /* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunction dfs(nums, target, i, j) {\n    // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n    if (i > j) {\n        return -1;\n    }\n    // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    const m = i + ((j - i) >> 1);\n    if (nums[m] < target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        return m;\n    }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nfunction binarySearch(nums, target) {\n    const n = nums.length;\n    // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
        binary_search_recur.ts
        /* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunction dfs(nums: number[], target: number, i: number, j: number): number {\n    // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n    if (i > j) {\n        return -1;\n    }\n    // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    const m = i + ((j - i) >> 1);\n    if (nums[m] < target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        return m;\n    }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nfunction binarySearch(nums: number[], target: number): number {\n    const n = nums.length;\n    // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
        binary_search_recur.dart
        /* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(List<int> nums, int target, int i, int j) {\n  // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n  if (i > j) {\n    return -1;\n  }\n  // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n  int m = (i + j) ~/ 2;\n  if (nums[m] < target) {\n    // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n    return dfs(nums, target, m + 1, j);\n  } else if (nums[m] > target) {\n    // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n    return dfs(nums, target, i, m - 1);\n  } else {\n    // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n    return m;\n  }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(List<int> nums, int target) {\n  int n = nums.length;\n  // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n  return dfs(nums, target, 0, n - 1);\n}\n
        binary_search_recur.rs
        /* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfn dfs(nums: &[i32], target: i32, i: i32, j: i32) -> i32 {\n    // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n    if i > j {\n        return -1;\n    }\n    let m: i32 = i + (j - i) / 2;\n    if nums[m as usize] < target {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if nums[m as usize] > target {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        return m;\n    }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nfn binary_search(nums: &[i32], target: i32) -> i32 {\n    let n = nums.len() as i32;\n    // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n    dfs(nums, target, 0, n - 1)\n}\n
        binary_search_recur.c
        /* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(int nums[], int target, int i, int j) {\n    // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n    if (i > j) {\n        return -1;\n    }\n    // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    int m = (i + j) / 2;\n    if (nums[m] < target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        return m;\n    }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(int nums[], int target, int numsSize) {\n    int n = numsSize;\n    // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
        binary_search_recur.kt
        /* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfun dfs(\n    nums: IntArray,\n    target: Int,\n    i: Int,\n    j: Int\n): Int {\n    // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n    if (i > j) {\n        return -1\n    }\n    // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    val m = (i + j) / 2\n    return if (nums[m] < target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n        dfs(nums, target, m + 1, j)\n    } else if (nums[m] > target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n        dfs(nums, target, i, m - 1)\n    } else {\n        // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        m\n    }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nfun binarySearch(nums: IntArray, target: Int): Int {\n    val n = nums.size\n    // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n    return dfs(nums, target, 0, n - 1)\n}\n
        binary_search_recur.rb
        ### \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) ###\ndef dfs(nums, target, i, j)\n  # \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n  return -1 if i > j\n\n  # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n  m = (i + j) / 2\n\n  if nums[m] < target\n    # \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n    return dfs(nums, target, m + 1, j)\n  elsif nums[m] > target\n    # \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n    return dfs(nums, target, i, m - 1)\n  else\n    # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n    return m\n  end\nend\n\n### \u4e8c\u5206\u67e5\u627e ###\ndef binary_search(nums, target)\n  n = nums.length\n  # \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n  dfs(nums, target, 0, n - 1)\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/","title":"12.3 \u00a0 \u6784\u5efa\u4e8c\u53c9\u6811\u95ee\u9898","text":"

        Question

        \u7ed9\u5b9a\u4e00\u68f5\u4e8c\u53c9\u6811\u7684\u524d\u5e8f\u904d\u5386 preorder \u548c\u4e2d\u5e8f\u904d\u5386 inorder \uff0c\u8bf7\u4ece\u4e2d\u6784\u5efa\u4e8c\u53c9\u6811\uff0c\u8fd4\u56de\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\u3002\u5047\u8bbe\u4e8c\u53c9\u6811\u4e2d\u6ca1\u6709\u503c\u91cd\u590d\u7684\u8282\u70b9\uff08\u5982\u56fe 12-5 \u6240\u793a\uff09\u3002

        \u56fe 12-5 \u00a0 \u6784\u5efa\u4e8c\u53c9\u6811\u7684\u793a\u4f8b\u6570\u636e

        "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#1","title":"1. \u00a0 \u5224\u65ad\u662f\u5426\u4e3a\u5206\u6cbb\u95ee\u9898","text":"

        \u539f\u95ee\u9898\u5b9a\u4e49\u4e3a\u4ece preorder \u548c inorder \u6784\u5efa\u4e8c\u53c9\u6811\uff0c\u662f\u4e00\u4e2a\u5178\u578b\u7684\u5206\u6cbb\u95ee\u9898\u3002

        • \u95ee\u9898\u53ef\u4ee5\u5206\u89e3\uff1a\u4ece\u5206\u6cbb\u7684\u89d2\u5ea6\u5207\u5165\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u539f\u95ee\u9898\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\u3001\u6784\u5efa\u53f3\u5b50\u6811\uff0c\u52a0\u4e0a\u4e00\u6b65\u64cd\u4f5c\uff1a\u521d\u59cb\u5316\u6839\u8282\u70b9\u3002\u800c\u5bf9\u4e8e\u6bcf\u68f5\u5b50\u6811\uff08\u5b50\u95ee\u9898\uff09\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u590d\u7528\u4ee5\u4e0a\u5212\u5206\u65b9\u6cd5\uff0c\u5c06\u5176\u5212\u5206\u4e3a\u66f4\u5c0f\u7684\u5b50\u6811\uff08\u5b50\u95ee\u9898\uff09\uff0c\u76f4\u81f3\u8fbe\u5230\u6700\u5c0f\u5b50\u95ee\u9898\uff08\u7a7a\u5b50\u6811\uff09\u65f6\u7ec8\u6b62\u3002
        • \u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff1a\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u662f\u76f8\u4e92\u72ec\u7acb\u7684\uff0c\u5b83\u4eec\u4e4b\u95f4\u6ca1\u6709\u4ea4\u96c6\u3002\u5728\u6784\u5efa\u5de6\u5b50\u6811\u65f6\uff0c\u6211\u4eec\u53ea\u9700\u5173\u6ce8\u4e2d\u5e8f\u904d\u5386\u548c\u524d\u5e8f\u904d\u5386\u4e2d\u4e0e\u5de6\u5b50\u6811\u5bf9\u5e94\u7684\u90e8\u5206\u3002\u53f3\u5b50\u6811\u540c\u7406\u3002
        • \u5b50\u95ee\u9898\u7684\u89e3\u53ef\u4ee5\u5408\u5e76\uff1a\u4e00\u65e6\u5f97\u5230\u4e86\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\uff08\u5b50\u95ee\u9898\u7684\u89e3\uff09\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u5b83\u4eec\u94fe\u63a5\u5230\u6839\u8282\u70b9\u4e0a\uff0c\u5f97\u5230\u539f\u95ee\u9898\u7684\u89e3\u3002
        "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#2","title":"2. \u00a0 \u5982\u4f55\u5212\u5206\u5b50\u6811","text":"

        \u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u8fd9\u9053\u9898\u53ef\u4ee5\u4f7f\u7528\u5206\u6cbb\u6765\u6c42\u89e3\uff0c\u4f46\u5982\u4f55\u901a\u8fc7\u524d\u5e8f\u904d\u5386 preorder \u548c\u4e2d\u5e8f\u904d\u5386 inorder \u6765\u5212\u5206\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u5462\uff1f

        \u6839\u636e\u5b9a\u4e49\uff0cpreorder \u548c inorder \u90fd\u53ef\u4ee5\u5212\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\u3002

        • \u524d\u5e8f\u904d\u5386\uff1a[ \u6839\u8282\u70b9 | \u5de6\u5b50\u6811 | \u53f3\u5b50\u6811 ] \uff0c\u4f8b\u5982\u56fe 12-5 \u7684\u6811\u5bf9\u5e94 [ 3 | 9 | 2 1 7 ] \u3002
        • \u4e2d\u5e8f\u904d\u5386\uff1a[ \u5de6\u5b50\u6811 | \u6839\u8282\u70b9 \uff5c \u53f3\u5b50\u6811 ] \uff0c\u4f8b\u5982\u56fe 12-5 \u7684\u6811\u5bf9\u5e94 [ 9 | 3 | 1 2 7 ] \u3002

        \u4ee5\u4e0a\u56fe\u6570\u636e\u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u56fe 12-6 \u6240\u793a\u7684\u6b65\u9aa4\u5f97\u5230\u5212\u5206\u7ed3\u679c\u3002

        1. \u524d\u5e8f\u904d\u5386\u7684\u9996\u5143\u7d20 3 \u662f\u6839\u8282\u70b9\u7684\u503c\u3002
        2. \u67e5\u627e\u6839\u8282\u70b9 3 \u5728 inorder \u4e2d\u7684\u7d22\u5f15\uff0c\u5229\u7528\u8be5\u7d22\u5f15\u53ef\u5c06 inorder \u5212\u5206\u4e3a [ 9 | 3 \uff5c 1 2 7 ] \u3002
        3. \u6839\u636e inorder \u7684\u5212\u5206\u7ed3\u679c\uff0c\u6613\u5f97\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u7684\u8282\u70b9\u6570\u91cf\u5206\u522b\u4e3a 1 \u548c 3 \uff0c\u4ece\u800c\u53ef\u5c06 preorder \u5212\u5206\u4e3a [ 3 | 9 | 2 1 7 ] \u3002

        \u56fe 12-6 \u00a0 \u5728\u524d\u5e8f\u904d\u5386\u548c\u4e2d\u5e8f\u904d\u5386\u4e2d\u5212\u5206\u5b50\u6811

        "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#3","title":"3. \u00a0 \u57fa\u4e8e\u53d8\u91cf\u63cf\u8ff0\u5b50\u6811\u533a\u95f4","text":"

        \u6839\u636e\u4ee5\u4e0a\u5212\u5206\u65b9\u6cd5\uff0c\u6211\u4eec\u5df2\u7ecf\u5f97\u5230\u6839\u8282\u70b9\u3001\u5de6\u5b50\u6811\u3001\u53f3\u5b50\u6811\u5728 preorder \u548c inorder \u4e2d\u7684\u7d22\u5f15\u533a\u95f4\u3002\u800c\u4e3a\u4e86\u63cf\u8ff0\u8fd9\u4e9b\u7d22\u5f15\u533a\u95f4\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u51e0\u4e2a\u6307\u9488\u53d8\u91cf\u3002

        • \u5c06\u5f53\u524d\u6811\u7684\u6839\u8282\u70b9\u5728 preorder \u4e2d\u7684\u7d22\u5f15\u8bb0\u4e3a \\(i\\) \u3002
        • \u5c06\u5f53\u524d\u6811\u7684\u6839\u8282\u70b9\u5728 inorder \u4e2d\u7684\u7d22\u5f15\u8bb0\u4e3a \\(m\\) \u3002
        • \u5c06\u5f53\u524d\u6811\u5728 inorder \u4e2d\u7684\u7d22\u5f15\u533a\u95f4\u8bb0\u4e3a \\([l, r]\\) \u3002

        \u5982\u8868 12-1 \u6240\u793a\uff0c\u901a\u8fc7\u4ee5\u4e0a\u53d8\u91cf\u5373\u53ef\u8868\u793a\u6839\u8282\u70b9\u5728 preorder \u4e2d\u7684\u7d22\u5f15\uff0c\u4ee5\u53ca\u5b50\u6811\u5728 inorder \u4e2d\u7684\u7d22\u5f15\u533a\u95f4\u3002

        \u8868 12-1 \u00a0 \u6839\u8282\u70b9\u548c\u5b50\u6811\u5728\u524d\u5e8f\u904d\u5386\u548c\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u7d22\u5f15

        \u6839\u8282\u70b9\u5728 preorder \u4e2d\u7684\u7d22\u5f15 \u5b50\u6811\u5728 inorder \u4e2d\u7684\u7d22\u5f15\u533a\u95f4 \u5f53\u524d\u6811 \\(i\\) \\([l, r]\\) \u5de6\u5b50\u6811 \\(i + 1\\) \\([l, m-1]\\) \u53f3\u5b50\u6811 \\(i + 1 + (m - l)\\) \\([m+1, r]\\)

        \u8bf7\u6ce8\u610f\uff0c\u53f3\u5b50\u6811\u6839\u8282\u70b9\u7d22\u5f15\u4e2d\u7684 \\((m-l)\\) \u7684\u542b\u4e49\u662f\u201c\u5de6\u5b50\u6811\u7684\u8282\u70b9\u6570\u91cf\u201d\uff0c\u5efa\u8bae\u7ed3\u5408\u56fe 12-7 \u7406\u89e3\u3002

        \u56fe 12-7 \u00a0 \u6839\u8282\u70b9\u548c\u5de6\u53f3\u5b50\u6811\u7684\u7d22\u5f15\u533a\u95f4\u8868\u793a

        "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#4","title":"4. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u4e3a\u4e86\u63d0\u5347\u67e5\u8be2 \\(m\\) \u7684\u6548\u7387\uff0c\u6211\u4eec\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868 hmap \u6765\u5b58\u50a8\u6570\u7ec4 inorder \u4e2d\u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby build_tree.py
        def dfs(\n    preorder: list[int],\n    inorder_map: dict[int, int],\n    i: int,\n    l: int,\n    r: int,\n) -> TreeNode | None:\n    \"\"\"\u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb\"\"\"\n    # \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n    if r - l < 0:\n        return None\n    # \u521d\u59cb\u5316\u6839\u8282\u70b9\n    root = TreeNode(preorder[i])\n    # \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n    m = inorder_map[preorder[i]]\n    # \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n    root.left = dfs(preorder, inorder_map, i + 1, l, m - 1)\n    # \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n    root.right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r)\n    # \u8fd4\u56de\u6839\u8282\u70b9\n    return root\n\ndef build_tree(preorder: list[int], inorder: list[int]) -> TreeNode | None:\n    \"\"\"\u6784\u5efa\u4e8c\u53c9\u6811\"\"\"\n    # \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n    inorder_map = {val: i for i, val in enumerate(inorder)}\n    root = dfs(preorder, inorder_map, 0, 0, len(inorder) - 1)\n    return root\n
        build_tree.cpp
        /* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode *dfs(vector<int> &preorder, unordered_map<int, int> &inorderMap, int i, int l, int r) {\n    // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n    if (r - l < 0)\n        return NULL;\n    // \u521d\u59cb\u5316\u6839\u8282\u70b9\n    TreeNode *root = new TreeNode(preorder[i]);\n    // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n    int m = inorderMap[preorder[i]];\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n    root->left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n    root->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // \u8fd4\u56de\u6839\u8282\u70b9\n    return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {\n    // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n    unordered_map<int, int> inorderMap;\n    for (int i = 0; i < inorder.size(); i++) {\n        inorderMap[inorder[i]] = i;\n    }\n    TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorder.size() - 1);\n    return root;\n}\n
        build_tree.java
        /* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode dfs(int[] preorder, Map<Integer, Integer> inorderMap, int i, int l, int r) {\n    // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n    if (r - l < 0)\n        return null;\n    // \u521d\u59cb\u5316\u6839\u8282\u70b9\n    TreeNode root = new TreeNode(preorder[i]);\n    // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n    int m = inorderMap.get(preorder[i]);\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n    root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n    root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // \u8fd4\u56de\u6839\u8282\u70b9\n    return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode buildTree(int[] preorder, int[] inorder) {\n    // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n    Map<Integer, Integer> inorderMap = new HashMap<>();\n    for (int i = 0; i < inorder.length; i++) {\n        inorderMap.put(inorder[i], i);\n    }\n    TreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n    return root;\n}\n
        build_tree.cs
        /* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode? DFS(int[] preorder, Dictionary<int, int> inorderMap, int i, int l, int r) {\n    // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n    if (r - l < 0)\n        return null;\n    // \u521d\u59cb\u5316\u6839\u8282\u70b9\n    TreeNode root = new(preorder[i]);\n    // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n    int m = inorderMap[preorder[i]];\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n    root.left = DFS(preorder, inorderMap, i + 1, l, m - 1);\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n    root.right = DFS(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // \u8fd4\u56de\u6839\u8282\u70b9\n    return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode? BuildTree(int[] preorder, int[] inorder) {\n    // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n    Dictionary<int, int> inorderMap = [];\n    for (int i = 0; i < inorder.Length; i++) {\n        inorderMap.TryAdd(inorder[i], i);\n    }\n    TreeNode? root = DFS(preorder, inorderMap, 0, 0, inorder.Length - 1);\n    return root;\n}\n
        build_tree.go
        /* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunc dfsBuildTree(preorder []int, inorderMap map[int]int, i, l, r int) *TreeNode {\n    // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n    if r-l < 0 {\n        return nil\n    }\n    // \u521d\u59cb\u5316\u6839\u8282\u70b9\n    root := NewTreeNode(preorder[i])\n    // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n    m := inorderMap[preorder[i]]\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n    root.Left = dfsBuildTree(preorder, inorderMap, i+1, l, m-1)\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n    root.Right = dfsBuildTree(preorder, inorderMap, i+1+m-l, m+1, r)\n    // \u8fd4\u56de\u6839\u8282\u70b9\n    return root\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunc buildTree(preorder, inorder []int) *TreeNode {\n    // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n    inorderMap := make(map[int]int, len(inorder))\n    for i := 0; i < len(inorder); i++ {\n        inorderMap[inorder[i]] = i\n    }\n\n    root := dfsBuildTree(preorder, inorderMap, 0, 0, len(inorder)-1)\n    return root\n}\n
        build_tree.swift
        /* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunc dfs(preorder: [Int], inorderMap: [Int: Int], i: Int, l: Int, r: Int) -> TreeNode? {\n    // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n    if r - l < 0 {\n        return nil\n    }\n    // \u521d\u59cb\u5316\u6839\u8282\u70b9\n    let root = TreeNode(x: preorder[i])\n    // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n    let m = inorderMap[preorder[i]]!\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n    root.left = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1, l: l, r: m - 1)\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n    root.right = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1 + m - l, l: m + 1, r: r)\n    // \u8fd4\u56de\u6839\u8282\u70b9\n    return root\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunc buildTree(preorder: [Int], inorder: [Int]) -> TreeNode? {\n    // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n    let inorderMap = inorder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }\n    return dfs(preorder: preorder, inorderMap: inorderMap, i: inorder.startIndex, l: inorder.startIndex, r: inorder.endIndex - 1)\n}\n
        build_tree.js
        /* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunction dfs(preorder, inorderMap, i, l, r) {\n    // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n    if (r - l < 0) return null;\n    // \u521d\u59cb\u5316\u6839\u8282\u70b9\n    const root = new TreeNode(preorder[i]);\n    // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n    const m = inorderMap.get(preorder[i]);\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n    root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n    root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // \u8fd4\u56de\u6839\u8282\u70b9\n    return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunction buildTree(preorder, inorder) {\n    // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n    let inorderMap = new Map();\n    for (let i = 0; i < inorder.length; i++) {\n        inorderMap.set(inorder[i], i);\n    }\n    const root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n    return root;\n}\n
        build_tree.ts
        /* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunction dfs(\n    preorder: number[],\n    inorderMap: Map<number, number>,\n    i: number,\n    l: number,\n    r: number\n): TreeNode | null {\n    // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n    if (r - l < 0) return null;\n    // \u521d\u59cb\u5316\u6839\u8282\u70b9\n    const root: TreeNode = new TreeNode(preorder[i]);\n    // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n    const m = inorderMap.get(preorder[i]);\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n    root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n    root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // \u8fd4\u56de\u6839\u8282\u70b9\n    return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunction buildTree(preorder: number[], inorder: number[]): TreeNode | null {\n    // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n    let inorderMap = new Map<number, number>();\n    for (let i = 0; i < inorder.length; i++) {\n        inorderMap.set(inorder[i], i);\n    }\n    const root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n    return root;\n}\n
        build_tree.dart
        /* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode? dfs(\n  List<int> preorder,\n  Map<int, int> inorderMap,\n  int i,\n  int l,\n  int r,\n) {\n  // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n  if (r - l < 0) {\n    return null;\n  }\n  // \u521d\u59cb\u5316\u6839\u8282\u70b9\n  TreeNode? root = TreeNode(preorder[i]);\n  // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n  int m = inorderMap[preorder[i]]!;\n  // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n  root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n  // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n  root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n  // \u8fd4\u56de\u6839\u8282\u70b9\n  return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode? buildTree(List<int> preorder, List<int> inorder) {\n  // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n  Map<int, int> inorderMap = {};\n  for (int i = 0; i < inorder.length; i++) {\n    inorderMap[inorder[i]] = i;\n  }\n  TreeNode? root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n  return root;\n}\n
        build_tree.rs
        /* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfn dfs(\n    preorder: &[i32],\n    inorder_map: &HashMap<i32, i32>,\n    i: i32,\n    l: i32,\n    r: i32,\n) -> Option<Rc<RefCell<TreeNode>>> {\n    // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n    if r - l < 0 {\n        return None;\n    }\n    // \u521d\u59cb\u5316\u6839\u8282\u70b9\n    let root = TreeNode::new(preorder[i as usize]);\n    // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n    let m = inorder_map.get(&preorder[i as usize]).unwrap();\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n    root.borrow_mut().left = dfs(preorder, inorder_map, i + 1, l, m - 1);\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n    root.borrow_mut().right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r);\n    // \u8fd4\u56de\u6839\u8282\u70b9\n    Some(root)\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfn build_tree(preorder: &[i32], inorder: &[i32]) -> Option<Rc<RefCell<TreeNode>>> {\n    // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n    let mut inorder_map: HashMap<i32, i32> = HashMap::new();\n    for i in 0..inorder.len() {\n        inorder_map.insert(inorder[i], i as i32);\n    }\n    let root = dfs(preorder, &inorder_map, 0, 0, inorder.len() as i32 - 1);\n    root\n}\n
        build_tree.c
        /* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode *dfs(int *preorder, int *inorderMap, int i, int l, int r, int size) {\n    // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n    if (r - l < 0)\n        return NULL;\n    // \u521d\u59cb\u5316\u6839\u8282\u70b9\n    TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));\n    root->val = preorder[i];\n    root->left = NULL;\n    root->right = NULL;\n    // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n    int m = inorderMap[preorder[i]];\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n    root->left = dfs(preorder, inorderMap, i + 1, l, m - 1, size);\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n    root->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r, size);\n    // \u8fd4\u56de\u6839\u8282\u70b9\n    return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode *buildTree(int *preorder, int preorderSize, int *inorder, int inorderSize) {\n    // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n    int *inorderMap = (int *)malloc(sizeof(int) * MAX_SIZE);\n    for (int i = 0; i < inorderSize; i++) {\n        inorderMap[inorder[i]] = i;\n    }\n    TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorderSize - 1, inorderSize);\n    free(inorderMap);\n    return root;\n}\n
        build_tree.kt
        /* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfun dfs(\n    preorder: IntArray,\n    inorderMap: Map<Int?, Int?>,\n    i: Int,\n    l: Int,\n    r: Int\n): TreeNode? {\n    // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n    if (r - l < 0) return null\n    // \u521d\u59cb\u5316\u6839\u8282\u70b9\n    val root = TreeNode(preorder[i])\n    // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n    val m = inorderMap[preorder[i]]!!\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n    root.left = dfs(preorder, inorderMap, i + 1, l, m - 1)\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n    root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r)\n    // \u8fd4\u56de\u6839\u8282\u70b9\n    return root\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfun buildTree(preorder: IntArray, inorder: IntArray): TreeNode? {\n    // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n    val inorderMap = HashMap<Int?, Int?>()\n    for (i in inorder.indices) {\n        inorderMap[inorder[i]] = i\n    }\n    val root = dfs(preorder, inorderMap, 0, 0, inorder.size - 1)\n    return root\n}\n
        build_tree.rb
        ### \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb ###\ndef dfs(preorder, inorder_map, i, l, r)\n  # \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n  return if r - l < 0\n\n  # \u521d\u59cb\u5316\u6839\u8282\u70b9\n  root = TreeNode.new(preorder[i])\n  # \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n  m = inorder_map[preorder[i]]\n  # \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n  root.left = dfs(preorder, inorder_map, i + 1, l, m - 1)\n  # \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n  root.right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r)\n\n  # \u8fd4\u56de\u6839\u8282\u70b9\n  root\nend\n\n### \u6784\u5efa\u4e8c\u53c9\u6811 ###\ndef build_tree(preorder, inorder)\n  # \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n  inorder_map = {}\n  inorder.each_with_index { |val, i| inorder_map[val] = i }\n  dfs(preorder, inorder_map, 0, 0, inorder.length - 1)\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 12-8 \u5c55\u793a\u4e86\u6784\u5efa\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b\uff0c\u5404\u4e2a\u8282\u70b9\u662f\u5728\u5411\u4e0b\u201c\u9012\u201d\u7684\u8fc7\u7a0b\u4e2d\u5efa\u7acb\u7684\uff0c\u800c\u5404\u6761\u8fb9\uff08\u5f15\u7528\uff09\u662f\u5728\u5411\u4e0a\u201c\u5f52\u201d\u7684\u8fc7\u7a0b\u4e2d\u5efa\u7acb\u7684\u3002

        <1><2><3><4><5><6><7><8><9>

        \u56fe 12-8 \u00a0 \u6784\u5efa\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b

        \u6bcf\u4e2a\u9012\u5f52\u51fd\u6570\u5185\u7684\u524d\u5e8f\u904d\u5386 preorder \u548c\u4e2d\u5e8f\u904d\u5386 inorder \u7684\u5212\u5206\u7ed3\u679c\u5982\u56fe 12-9 \u6240\u793a\u3002

        \u56fe 12-9 \u00a0 \u6bcf\u4e2a\u9012\u5f52\u51fd\u6570\u4e2d\u7684\u5212\u5206\u7ed3\u679c

        \u8bbe\u6811\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u521d\u59cb\u5316\u6bcf\u4e00\u4e2a\u8282\u70b9\uff08\u6267\u884c\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 dfs() \uff09\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002\u56e0\u6b64\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

        \u54c8\u5e0c\u8868\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u5373\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u94fe\u8868\u65f6\uff0c\u9012\u5f52\u6df1\u5ea6\u8fbe\u5230 \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u7684\u6808\u5e27\u7a7a\u95f4\u3002\u56e0\u6b64\u603b\u4f53\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/","title":"12.1 \u00a0 \u5206\u6cbb\u7b97\u6cd5","text":"

        \u5206\u6cbb\uff08divide and conquer\uff09\uff0c\u5168\u79f0\u5206\u800c\u6cbb\u4e4b\uff0c\u662f\u4e00\u79cd\u975e\u5e38\u91cd\u8981\u4e14\u5e38\u89c1\u7684\u7b97\u6cd5\u7b56\u7565\u3002\u5206\u6cbb\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\uff0c\u5305\u62ec\u201c\u5206\u201d\u548c\u201c\u6cbb\u201d\u4e24\u4e2a\u6b65\u9aa4\u3002

        1. \u5206\uff08\u5212\u5206\u9636\u6bb5\uff09\uff1a\u9012\u5f52\u5730\u5c06\u539f\u95ee\u9898\u5206\u89e3\u4e3a\u4e24\u4e2a\u6216\u591a\u4e2a\u5b50\u95ee\u9898\uff0c\u76f4\u81f3\u5230\u8fbe\u6700\u5c0f\u5b50\u95ee\u9898\u65f6\u7ec8\u6b62\u3002
        2. \u6cbb\uff08\u5408\u5e76\u9636\u6bb5\uff09\uff1a\u4ece\u5df2\u77e5\u89e3\u7684\u6700\u5c0f\u5b50\u95ee\u9898\u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5730\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u8fdb\u884c\u5408\u5e76\uff0c\u4ece\u800c\u6784\u5efa\u51fa\u539f\u95ee\u9898\u7684\u89e3\u3002

        \u5982\u56fe 12-1 \u6240\u793a\uff0c\u201c\u5f52\u5e76\u6392\u5e8f\u201d\u662f\u5206\u6cbb\u7b56\u7565\u7684\u5178\u578b\u5e94\u7528\u4e4b\u4e00\u3002

        1. \u5206\uff1a\u9012\u5f52\u5730\u5c06\u539f\u6570\u7ec4\uff08\u539f\u95ee\u9898\uff09\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff08\u5b50\u95ee\u9898\uff09\uff0c\u76f4\u5230\u5b50\u6570\u7ec4\u53ea\u5269\u4e00\u4e2a\u5143\u7d20\uff08\u6700\u5c0f\u5b50\u95ee\u9898\uff09\u3002
        2. \u6cbb\uff1a\u4ece\u5e95\u81f3\u9876\u5730\u5c06\u6709\u5e8f\u7684\u5b50\u6570\u7ec4\uff08\u5b50\u95ee\u9898\u7684\u89e3\uff09\u8fdb\u884c\u5408\u5e76\uff0c\u4ece\u800c\u5f97\u5230\u6709\u5e8f\u7684\u539f\u6570\u7ec4\uff08\u539f\u95ee\u9898\u7684\u89e3\uff09\u3002

        \u56fe 12-1 \u00a0 \u5f52\u5e76\u6392\u5e8f\u7684\u5206\u6cbb\u7b56\u7565

        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1211","title":"12.1.1 \u00a0 \u5982\u4f55\u5224\u65ad\u5206\u6cbb\u95ee\u9898","text":"

        \u4e00\u4e2a\u95ee\u9898\u662f\u5426\u9002\u5408\u4f7f\u7528\u5206\u6cbb\u89e3\u51b3\uff0c\u901a\u5e38\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u51e0\u4e2a\u5224\u65ad\u4f9d\u636e\u3002

        1. \u95ee\u9898\u53ef\u4ee5\u5206\u89e3\uff1a\u539f\u95ee\u9898\u53ef\u4ee5\u5206\u89e3\u6210\u89c4\u6a21\u66f4\u5c0f\u3001\u7c7b\u4f3c\u7684\u5b50\u95ee\u9898\uff0c\u4ee5\u53ca\u80fd\u591f\u4ee5\u76f8\u540c\u65b9\u5f0f\u9012\u5f52\u5730\u8fdb\u884c\u5212\u5206\u3002
        2. \u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff1a\u5b50\u95ee\u9898\u4e4b\u95f4\u6ca1\u6709\u91cd\u53e0\uff0c\u4e92\u4e0d\u4f9d\u8d56\uff0c\u53ef\u4ee5\u72ec\u7acb\u89e3\u51b3\u3002
        3. \u5b50\u95ee\u9898\u7684\u89e3\u53ef\u4ee5\u5408\u5e76\uff1a\u539f\u95ee\u9898\u7684\u89e3\u901a\u8fc7\u5408\u5e76\u5b50\u95ee\u9898\u7684\u89e3\u5f97\u6765\u3002

        \u663e\u7136\uff0c\u5f52\u5e76\u6392\u5e8f\u6ee1\u8db3\u4ee5\u4e0a\u4e09\u4e2a\u5224\u65ad\u4f9d\u636e\u3002

        1. \u95ee\u9898\u53ef\u4ee5\u5206\u89e3\uff1a\u9012\u5f52\u5730\u5c06\u6570\u7ec4\uff08\u539f\u95ee\u9898\uff09\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff08\u5b50\u95ee\u9898\uff09\u3002
        2. \u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff1a\u6bcf\u4e2a\u5b50\u6570\u7ec4\u90fd\u53ef\u4ee5\u72ec\u7acb\u5730\u8fdb\u884c\u6392\u5e8f\uff08\u5b50\u95ee\u9898\u53ef\u4ee5\u72ec\u7acb\u8fdb\u884c\u6c42\u89e3\uff09\u3002
        3. \u5b50\u95ee\u9898\u7684\u89e3\u53ef\u4ee5\u5408\u5e76\uff1a\u4e24\u4e2a\u6709\u5e8f\u5b50\u6570\u7ec4\uff08\u5b50\u95ee\u9898\u7684\u89e3\uff09\u53ef\u4ee5\u5408\u5e76\u4e3a\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4\uff08\u539f\u95ee\u9898\u7684\u89e3\uff09\u3002
        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1212","title":"12.1.2 \u00a0 \u901a\u8fc7\u5206\u6cbb\u63d0\u5347\u6548\u7387","text":"

        \u5206\u6cbb\u4e0d\u4ec5\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u51b3\u7b97\u6cd5\u95ee\u9898\uff0c\u5f80\u5f80\u8fd8\u53ef\u4ee5\u63d0\u5347\u7b97\u6cd5\u6548\u7387\u3002\u5728\u6392\u5e8f\u7b97\u6cd5\u4e2d\uff0c\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u3001\u5806\u6392\u5e8f\u76f8\u8f83\u4e8e\u9009\u62e9\u3001\u5192\u6ce1\u3001\u63d2\u5165\u6392\u5e8f\u66f4\u5feb\uff0c\u5c31\u662f\u56e0\u4e3a\u5b83\u4eec\u5e94\u7528\u4e86\u5206\u6cbb\u7b56\u7565\u3002

        \u90a3\u4e48\uff0c\u6211\u4eec\u4e0d\u7981\u53d1\u95ee\uff1a\u4e3a\u4ec0\u4e48\u5206\u6cbb\u53ef\u4ee5\u63d0\u5347\u7b97\u6cd5\u6548\u7387\uff0c\u5176\u5e95\u5c42\u903b\u8f91\u662f\u4ec0\u4e48\uff1f\u6362\u53e5\u8bdd\u8bf4\uff0c\u5c06\u5927\u95ee\u9898\u5206\u89e3\u4e3a\u591a\u4e2a\u5b50\u95ee\u9898\u3001\u89e3\u51b3\u5b50\u95ee\u9898\u3001\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u5408\u5e76\u4e3a\u539f\u95ee\u9898\u7684\u89e3\uff0c\u8fd9\u51e0\u6b65\u7684\u6548\u7387\u4e3a\u4ec0\u4e48\u6bd4\u76f4\u63a5\u89e3\u51b3\u539f\u95ee\u9898\u7684\u6548\u7387\u66f4\u9ad8\uff1f\u8fd9\u4e2a\u95ee\u9898\u53ef\u4ee5\u4ece\u64cd\u4f5c\u6570\u91cf\u548c\u5e76\u884c\u8ba1\u7b97\u4e24\u65b9\u9762\u6765\u8ba8\u8bba\u3002

        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1","title":"1. \u00a0 \u64cd\u4f5c\u6570\u91cf\u4f18\u5316","text":"

        \u4ee5\u201c\u5192\u6ce1\u6392\u5e8f\u201d\u4e3a\u4f8b\uff0c\u5176\u5904\u7406\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\u9700\u8981 \\(O(n^2)\\) \u65f6\u95f4\u3002\u5047\u8bbe\u6211\u4eec\u6309\u7167\u56fe 12-2 \u6240\u793a\u7684\u65b9\u5f0f\uff0c\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5904\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u5219\u5212\u5206\u9700\u8981 \\(O(n)\\) \u65f6\u95f4\uff0c\u6392\u5e8f\u6bcf\u4e2a\u5b50\u6570\u7ec4\u9700\u8981 \\(O((n / 2)^2)\\) \u65f6\u95f4\uff0c\u5408\u5e76\u4e24\u4e2a\u5b50\u6570\u7ec4\u9700\u8981 \\(O(n)\\) \u65f6\u95f4\uff0c\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\uff1a

        \\[ O(n + (\\frac{n}{2})^2 \\times 2 + n) = O(\\frac{n^2}{2} + 2n) \\]

        \u56fe 12-2 \u00a0 \u5212\u5206\u6570\u7ec4\u524d\u540e\u7684\u5192\u6ce1\u6392\u5e8f

        \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8ba1\u7b97\u4ee5\u4e0b\u4e0d\u7b49\u5f0f\uff0c\u5176\u5de6\u8fb9\u548c\u53f3\u8fb9\u5206\u522b\u4e3a\u5212\u5206\u524d\u548c\u5212\u5206\u540e\u7684\u64cd\u4f5c\u603b\u6570\uff1a

        \\[ \\begin{aligned} n^2 & > \\frac{n^2}{2} + 2n \\newline n^2 - \\frac{n^2}{2} - 2n & > 0 \\newline n(n - 4) & > 0 \\end{aligned} \\]

        \u8fd9\u610f\u5473\u7740\u5f53 \\(n > 4\\) \u65f6\uff0c\u5212\u5206\u540e\u7684\u64cd\u4f5c\u6570\u91cf\u66f4\u5c11\uff0c\u6392\u5e8f\u6548\u7387\u5e94\u8be5\u66f4\u9ad8\u3002\u8bf7\u6ce8\u610f\uff0c\u5212\u5206\u540e\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u7136\u662f\u5e73\u65b9\u9636 \\(O(n^2)\\) \uff0c\u53ea\u662f\u590d\u6742\u5ea6\u4e2d\u7684\u5e38\u6570\u9879\u53d8\u5c0f\u4e86\u3002

        \u8fdb\u4e00\u6b65\u60f3\uff0c\u5982\u679c\u6211\u4eec\u628a\u5b50\u6570\u7ec4\u4e0d\u65ad\u5730\u518d\u4ece\u4e2d\u70b9\u5904\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u53ea\u5269\u4e00\u4e2a\u5143\u7d20\u65f6\u505c\u6b62\u5212\u5206\u5462\uff1f\u8fd9\u79cd\u601d\u8def\u5b9e\u9645\u4e0a\u5c31\u662f\u201c\u5f52\u5e76\u6392\u5e8f\u201d\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002

        \u518d\u601d\u8003\uff0c\u5982\u679c\u6211\u4eec\u591a\u8bbe\u7f6e\u51e0\u4e2a\u5212\u5206\u70b9\uff0c\u5c06\u539f\u6570\u7ec4\u5e73\u5747\u5212\u5206\u4e3a \\(k\\) \u4e2a\u5b50\u6570\u7ec4\u5462\uff1f\u8fd9\u79cd\u60c5\u51b5\u4e0e\u201c\u6876\u6392\u5e8f\u201d\u975e\u5e38\u7c7b\u4f3c\uff0c\u5b83\u975e\u5e38\u9002\u5408\u6392\u5e8f\u6d77\u91cf\u6570\u636e\uff0c\u7406\u8bba\u4e0a\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u8fbe\u5230 \\(O(n + k)\\) \u3002

        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#2","title":"2. \u00a0 \u5e76\u884c\u8ba1\u7b97\u4f18\u5316","text":"

        \u6211\u4eec\u77e5\u9053\uff0c\u5206\u6cbb\u751f\u6210\u7684\u5b50\u95ee\u9898\u662f\u76f8\u4e92\u72ec\u7acb\u7684\uff0c\u56e0\u6b64\u901a\u5e38\u53ef\u4ee5\u5e76\u884c\u89e3\u51b3\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5206\u6cbb\u4e0d\u4ec5\u53ef\u4ee5\u964d\u4f4e\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u8fd8\u6709\u5229\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u7684\u5e76\u884c\u4f18\u5316\u3002

        \u5e76\u884c\u4f18\u5316\u5728\u591a\u6838\u6216\u591a\u5904\u7406\u5668\u7684\u73af\u5883\u4e2d\u5c24\u5176\u6709\u6548\uff0c\u56e0\u4e3a\u7cfb\u7edf\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5b50\u95ee\u9898\uff0c\u66f4\u52a0\u5145\u5206\u5730\u5229\u7528\u8ba1\u7b97\u8d44\u6e90\uff0c\u4ece\u800c\u663e\u8457\u51cf\u5c11\u603b\u4f53\u7684\u8fd0\u884c\u65f6\u95f4\u3002

        \u6bd4\u5982\u5728\u56fe 12-3 \u6240\u793a\u7684\u201c\u6876\u6392\u5e8f\u201d\u4e2d\uff0c\u6211\u4eec\u5c06\u6d77\u91cf\u7684\u6570\u636e\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\uff0c\u5219\u53ef\u5c06\u6240\u6709\u6876\u7684\u6392\u5e8f\u4efb\u52a1\u5206\u6563\u5230\u5404\u4e2a\u8ba1\u7b97\u5355\u5143\uff0c\u5b8c\u6210\u540e\u518d\u5408\u5e76\u7ed3\u679c\u3002

        \u56fe 12-3 \u00a0 \u6876\u6392\u5e8f\u7684\u5e76\u884c\u8ba1\u7b97

        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1213","title":"12.1.3 \u00a0 \u5206\u6cbb\u5e38\u89c1\u5e94\u7528","text":"

        \u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u53ef\u4ee5\u7528\u6765\u89e3\u51b3\u8bb8\u591a\u7ecf\u5178\u7b97\u6cd5\u95ee\u9898\u3002

        • \u5bfb\u627e\u6700\u8fd1\u70b9\u5bf9\uff1a\u8be5\u7b97\u6cd5\u9996\u5148\u5c06\u70b9\u96c6\u5206\u6210\u4e24\u90e8\u5206\uff0c\u7136\u540e\u5206\u522b\u627e\u51fa\u4e24\u90e8\u5206\u4e2d\u7684\u6700\u8fd1\u70b9\u5bf9\uff0c\u6700\u540e\u627e\u51fa\u8de8\u8d8a\u4e24\u90e8\u5206\u7684\u6700\u8fd1\u70b9\u5bf9\u3002
        • \u5927\u6574\u6570\u4e58\u6cd5\uff1a\u4f8b\u5982 Karatsuba \u7b97\u6cd5\uff0c\u5b83\u5c06\u5927\u6574\u6570\u4e58\u6cd5\u5206\u89e3\u4e3a\u51e0\u4e2a\u8f83\u5c0f\u7684\u6574\u6570\u7684\u4e58\u6cd5\u548c\u52a0\u6cd5\u3002
        • \u77e9\u9635\u4e58\u6cd5\uff1a\u4f8b\u5982 Strassen \u7b97\u6cd5\uff0c\u5b83\u5c06\u5927\u77e9\u9635\u4e58\u6cd5\u5206\u89e3\u4e3a\u591a\u4e2a\u5c0f\u77e9\u9635\u7684\u4e58\u6cd5\u548c\u52a0\u6cd5\u3002
        • \u6c49\u8bfa\u5854\u95ee\u9898\uff1a\u6c49\u8bfa\u5854\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u89e3\u51b3\uff0c\u8fd9\u662f\u5178\u578b\u7684\u5206\u6cbb\u7b56\u7565\u5e94\u7528\u3002
        • \u6c42\u89e3\u9006\u5e8f\u5bf9\uff1a\u5728\u4e00\u4e2a\u5e8f\u5217\u4e2d\uff0c\u5982\u679c\u524d\u9762\u7684\u6570\u5b57\u5927\u4e8e\u540e\u9762\u7684\u6570\u5b57\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u6570\u5b57\u6784\u6210\u4e00\u4e2a\u9006\u5e8f\u5bf9\u3002\u6c42\u89e3\u9006\u5e8f\u5bf9\u95ee\u9898\u53ef\u4ee5\u5229\u7528\u5206\u6cbb\u7684\u601d\u60f3\uff0c\u501f\u52a9\u5f52\u5e76\u6392\u5e8f\u8fdb\u884c\u6c42\u89e3\u3002

        \u53e6\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u5728\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u7684\u8bbe\u8ba1\u4e2d\u5e94\u7528\u5f97\u975e\u5e38\u5e7f\u6cdb\u3002

        • \u4e8c\u5206\u67e5\u627e\uff1a\u4e8c\u5206\u67e5\u627e\u662f\u5c06\u6709\u5e8f\u6570\u7ec4\u4ece\u4e2d\u70b9\u7d22\u5f15\u5904\u5206\u4e3a\u4e24\u90e8\u5206\uff0c\u7136\u540e\u6839\u636e\u76ee\u6807\u503c\u4e0e\u4e2d\u95f4\u5143\u7d20\u503c\u6bd4\u8f83\u7ed3\u679c\uff0c\u51b3\u5b9a\u6392\u9664\u54ea\u4e00\u534a\u533a\u95f4\uff0c\u5e76\u5728\u5269\u4f59\u533a\u95f4\u6267\u884c\u76f8\u540c\u7684\u4e8c\u5206\u64cd\u4f5c\u3002
        • \u5f52\u5e76\u6392\u5e8f\uff1a\u672c\u8282\u5f00\u5934\u5df2\u4ecb\u7ecd\uff0c\u4e0d\u518d\u8d58\u8ff0\u3002
        • \u5feb\u901f\u6392\u5e8f\uff1a\u5feb\u901f\u6392\u5e8f\u662f\u9009\u53d6\u4e00\u4e2a\u57fa\u51c6\u503c\uff0c\u7136\u540e\u628a\u6570\u7ec4\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u4e00\u4e2a\u5b50\u6570\u7ec4\u7684\u5143\u7d20\u6bd4\u57fa\u51c6\u503c\u5c0f\uff0c\u53e6\u4e00\u5b50\u6570\u7ec4\u7684\u5143\u7d20\u6bd4\u57fa\u51c6\u503c\u5927\uff0c\u518d\u5bf9\u8fd9\u4e24\u90e8\u5206\u8fdb\u884c\u76f8\u540c\u7684\u5212\u5206\u64cd\u4f5c\uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u53ea\u5269\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002
        • \u6876\u6392\u5e8f\uff1a\u6876\u6392\u5e8f\u7684\u57fa\u672c\u601d\u60f3\u662f\u5c06\u6570\u636e\u5206\u6563\u5230\u591a\u4e2a\u6876\uff0c\u7136\u540e\u5bf9\u6bcf\u4e2a\u6876\u5185\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\uff0c\u6700\u540e\u5c06\u5404\u4e2a\u6876\u7684\u5143\u7d20\u4f9d\u6b21\u53d6\u51fa\uff0c\u4ece\u800c\u5f97\u5230\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4\u3002
        • \u6811\uff1a\u4f8b\u5982\u4e8c\u53c9\u641c\u7d22\u6811\u3001AVL \u6811\u3001\u7ea2\u9ed1\u6811\u3001B \u6811\u3001B+ \u6811\u7b49\uff0c\u5b83\u4eec\u7684\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u7b49\u64cd\u4f5c\u90fd\u53ef\u4ee5\u89c6\u4e3a\u5206\u6cbb\u7b56\u7565\u7684\u5e94\u7528\u3002
        • \u5806\uff1a\u5806\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u5176\u5404\u79cd\u64cd\u4f5c\uff0c\u5982\u63d2\u5165\u3001\u5220\u9664\u548c\u5806\u5316\uff0c\u5b9e\u9645\u4e0a\u90fd\u9690\u542b\u4e86\u5206\u6cbb\u7684\u601d\u60f3\u3002
        • \u54c8\u5e0c\u8868\uff1a\u867d\u7136\u54c8\u5e0c\u8868\u5e76\u4e0d\u76f4\u63a5\u5e94\u7528\u5206\u6cbb\uff0c\u4f46\u67d0\u4e9b\u54c8\u5e0c\u51b2\u7a81\u89e3\u51b3\u65b9\u6848\u95f4\u63a5\u5e94\u7528\u4e86\u5206\u6cbb\u7b56\u7565\uff0c\u4f8b\u5982\uff0c\u94fe\u5f0f\u5730\u5740\u4e2d\u7684\u957f\u94fe\u8868\u4f1a\u88ab\u8f6c\u5316\u4e3a\u7ea2\u9ed1\u6811\uff0c\u4ee5\u63d0\u5347\u67e5\u8be2\u6548\u7387\u3002

        \u53ef\u4ee5\u770b\u51fa\uff0c\u5206\u6cbb\u662f\u4e00\u79cd\u201c\u6da6\u7269\u7ec6\u65e0\u58f0\u201d\u7684\u7b97\u6cd5\u601d\u60f3\uff0c\u9690\u542b\u5728\u5404\u79cd\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784\u4e4b\u4e2d\u3002

        "},{"location":"chapter_divide_and_conquer/hanota_problem/","title":"12.4 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898","text":"

        \u5728\u5f52\u5e76\u6392\u5e8f\u548c\u6784\u5efa\u4e8c\u53c9\u6811\u4e2d\uff0c\u6211\u4eec\u90fd\u662f\u5c06\u539f\u95ee\u9898\u5206\u89e3\u4e3a\u4e24\u4e2a\u89c4\u6a21\u4e3a\u539f\u95ee\u9898\u4e00\u534a\u7684\u5b50\u95ee\u9898\u3002\u7136\u800c\u5bf9\u4e8e\u6c49\u8bfa\u5854\u95ee\u9898\uff0c\u6211\u4eec\u91c7\u7528\u4e0d\u540c\u7684\u5206\u89e3\u7b56\u7565\u3002

        Question

        \u7ed9\u5b9a\u4e09\u6839\u67f1\u5b50\uff0c\u8bb0\u4e3a A\u3001B \u548c C \u3002\u8d77\u59cb\u72b6\u6001\u4e0b\uff0c\u67f1\u5b50 A \u4e0a\u5957\u7740 \\(n\\) \u4e2a\u5706\u76d8\uff0c\u5b83\u4eec\u4ece\u4e0a\u5230\u4e0b\u6309\u7167\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u6392\u5217\u3002\u6211\u4eec\u7684\u4efb\u52a1\u662f\u8981\u628a\u8fd9 \\(n\\) \u4e2a\u5706\u76d8\u79fb\u5230\u67f1\u5b50 C \u4e0a\uff0c\u5e76\u4fdd\u6301\u5b83\u4eec\u7684\u539f\u6709\u987a\u5e8f\u4e0d\u53d8\uff08\u5982\u56fe 12-10 \u6240\u793a\uff09\u3002\u5728\u79fb\u52a8\u5706\u76d8\u7684\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u9075\u5b88\u4ee5\u4e0b\u89c4\u5219\u3002

        1. \u5706\u76d8\u53ea\u80fd\u4ece\u4e00\u6839\u67f1\u5b50\u9876\u90e8\u62ff\u51fa\uff0c\u4ece\u53e6\u4e00\u6839\u67f1\u5b50\u9876\u90e8\u653e\u5165\u3002
        2. \u6bcf\u6b21\u53ea\u80fd\u79fb\u52a8\u4e00\u4e2a\u5706\u76d8\u3002
        3. \u5c0f\u5706\u76d8\u5fc5\u987b\u65f6\u523b\u4f4d\u4e8e\u5927\u5706\u76d8\u4e4b\u4e0a\u3002

        \u56fe 12-10 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898\u793a\u4f8b

        \u6211\u4eec\u5c06\u89c4\u6a21\u4e3a \\(i\\) \u7684\u6c49\u8bfa\u5854\u95ee\u9898\u8bb0\u4f5c \\(f(i)\\) \u3002\u4f8b\u5982 \\(f(3)\\) \u4ee3\u8868\u5c06 \\(3\\) \u4e2a\u5706\u76d8\u4ece A \u79fb\u52a8\u81f3 C \u7684\u6c49\u8bfa\u5854\u95ee\u9898\u3002

        "},{"location":"chapter_divide_and_conquer/hanota_problem/#1","title":"1. \u00a0 \u8003\u8651\u57fa\u672c\u60c5\u51b5","text":"

        \u5982\u56fe 12-11 \u6240\u793a\uff0c\u5bf9\u4e8e\u95ee\u9898 \\(f(1)\\) \uff0c\u5373\u5f53\u53ea\u6709\u4e00\u4e2a\u5706\u76d8\u65f6\uff0c\u6211\u4eec\u5c06\u5b83\u76f4\u63a5\u4ece A \u79fb\u52a8\u81f3 C \u5373\u53ef\u3002

        <1><2>

        \u56fe 12-11 \u00a0 \u89c4\u6a21\u4e3a 1 \u7684\u95ee\u9898\u7684\u89e3

        \u5982\u56fe 12-12 \u6240\u793a\uff0c\u5bf9\u4e8e\u95ee\u9898 \\(f(2)\\) \uff0c\u5373\u5f53\u6709\u4e24\u4e2a\u5706\u76d8\u65f6\uff0c\u7531\u4e8e\u8981\u65f6\u523b\u6ee1\u8db3\u5c0f\u5706\u76d8\u5728\u5927\u5706\u76d8\u4e4b\u4e0a\uff0c\u56e0\u6b64\u9700\u8981\u501f\u52a9 B \u6765\u5b8c\u6210\u79fb\u52a8\u3002

        1. \u5148\u5c06\u4e0a\u9762\u7684\u5c0f\u5706\u76d8\u4ece A \u79fb\u81f3 B \u3002
        2. \u518d\u5c06\u5927\u5706\u76d8\u4ece A \u79fb\u81f3 C \u3002
        3. \u6700\u540e\u5c06\u5c0f\u5706\u76d8\u4ece B \u79fb\u81f3 C \u3002
        <1><2><3><4>

        \u56fe 12-12 \u00a0 \u89c4\u6a21\u4e3a 2 \u7684\u95ee\u9898\u7684\u89e3

        \u89e3\u51b3\u95ee\u9898 \\(f(2)\\) \u7684\u8fc7\u7a0b\u53ef\u603b\u7ed3\u4e3a\uff1a\u5c06\u4e24\u4e2a\u5706\u76d8\u501f\u52a9 B \u4ece A \u79fb\u81f3 C \u3002\u5176\u4e2d\uff0cC \u79f0\u4e3a\u76ee\u6807\u67f1\u3001B \u79f0\u4e3a\u7f13\u51b2\u67f1\u3002

        "},{"location":"chapter_divide_and_conquer/hanota_problem/#2","title":"2. \u00a0 \u5b50\u95ee\u9898\u5206\u89e3","text":"

        \u5bf9\u4e8e\u95ee\u9898 \\(f(3)\\) \uff0c\u5373\u5f53\u6709\u4e09\u4e2a\u5706\u76d8\u65f6\uff0c\u60c5\u51b5\u53d8\u5f97\u7a0d\u5fae\u590d\u6742\u4e86\u4e00\u4e9b\u3002

        \u56e0\u4e3a\u5df2\u77e5 \\(f(1)\\) \u548c \\(f(2)\\) \u7684\u89e3\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ece\u5206\u6cbb\u89d2\u5ea6\u601d\u8003\uff0c\u5c06 A \u9876\u90e8\u7684\u4e24\u4e2a\u5706\u76d8\u770b\u4f5c\u4e00\u4e2a\u6574\u4f53\uff0c\u6267\u884c\u56fe 12-13 \u6240\u793a\u7684\u6b65\u9aa4\u3002\u8fd9\u6837\u4e09\u4e2a\u5706\u76d8\u5c31\u88ab\u987a\u5229\u5730\u4ece A \u79fb\u81f3 C \u4e86\u3002

        1. \u4ee4 B \u4e3a\u76ee\u6807\u67f1\u3001C \u4e3a\u7f13\u51b2\u67f1\uff0c\u5c06\u4e24\u4e2a\u5706\u76d8\u4ece A \u79fb\u81f3 B \u3002
        2. \u5c06 A \u4e2d\u5269\u4f59\u7684\u4e00\u4e2a\u5706\u76d8\u4ece A \u76f4\u63a5\u79fb\u52a8\u81f3 C \u3002
        3. \u4ee4 C \u4e3a\u76ee\u6807\u67f1\u3001A \u4e3a\u7f13\u51b2\u67f1\uff0c\u5c06\u4e24\u4e2a\u5706\u76d8\u4ece B \u79fb\u81f3 C \u3002
        <1><2><3><4>

        \u56fe 12-13 \u00a0 \u89c4\u6a21\u4e3a 3 \u7684\u95ee\u9898\u7684\u89e3

        \u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u6211\u4eec\u5c06\u95ee\u9898 \\(f(3)\\) \u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u95ee\u9898 \\(f(2)\\) \u548c\u4e00\u4e2a\u5b50\u95ee\u9898 \\(f(1)\\) \u3002\u6309\u987a\u5e8f\u89e3\u51b3\u8fd9\u4e09\u4e2a\u5b50\u95ee\u9898\u4e4b\u540e\uff0c\u539f\u95ee\u9898\u968f\u4e4b\u5f97\u5230\u89e3\u51b3\u3002\u8fd9\u8bf4\u660e\u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff0c\u800c\u4e14\u89e3\u53ef\u4ee5\u5408\u5e76\u3002

        \u81f3\u6b64\uff0c\u6211\u4eec\u53ef\u603b\u7ed3\u51fa\u56fe 12-14 \u6240\u793a\u7684\u89e3\u51b3\u6c49\u8bfa\u5854\u95ee\u9898\u7684\u5206\u6cbb\u7b56\u7565\uff1a\u5c06\u539f\u95ee\u9898 \\(f(n)\\) \u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u95ee\u9898 \\(f(n-1)\\) \u548c\u4e00\u4e2a\u5b50\u95ee\u9898 \\(f(1)\\) \uff0c\u5e76\u6309\u7167\u4ee5\u4e0b\u987a\u5e8f\u89e3\u51b3\u8fd9\u4e09\u4e2a\u5b50\u95ee\u9898\u3002

        1. \u5c06 \\(n-1\\) \u4e2a\u5706\u76d8\u501f\u52a9 C \u4ece A \u79fb\u81f3 B \u3002
        2. \u5c06\u5269\u4f59 \\(1\\) \u4e2a\u5706\u76d8\u4ece A \u76f4\u63a5\u79fb\u81f3 C \u3002
        3. \u5c06 \\(n-1\\) \u4e2a\u5706\u76d8\u501f\u52a9 A \u4ece B \u79fb\u81f3 C \u3002

        \u5bf9\u4e8e\u8fd9\u4e24\u4e2a\u5b50\u95ee\u9898 \\(f(n-1)\\) \uff0c\u53ef\u4ee5\u901a\u8fc7\u76f8\u540c\u7684\u65b9\u5f0f\u8fdb\u884c\u9012\u5f52\u5212\u5206\uff0c\u76f4\u81f3\u8fbe\u5230\u6700\u5c0f\u5b50\u95ee\u9898 \\(f(1)\\) \u3002\u800c \\(f(1)\\) \u7684\u89e3\u662f\u5df2\u77e5\u7684\uff0c\u53ea\u9700\u4e00\u6b21\u79fb\u52a8\u64cd\u4f5c\u5373\u53ef\u3002

        \u56fe 12-14 \u00a0 \u89e3\u51b3\u6c49\u8bfa\u5854\u95ee\u9898\u7684\u5206\u6cbb\u7b56\u7565

        "},{"location":"chapter_divide_and_conquer/hanota_problem/#3","title":"3. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u5728\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 dfs(i, src, buf, tar) \uff0c\u5b83\u7684\u4f5c\u7528\u662f\u5c06\u67f1 src \u9876\u90e8\u7684 \\(i\\) \u4e2a\u5706\u76d8\u501f\u52a9\u7f13\u51b2\u67f1 buf \u79fb\u52a8\u81f3\u76ee\u6807\u67f1 tar \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hanota.py
        def move(src: list[int], tar: list[int]):\n    \"\"\"\u79fb\u52a8\u4e00\u4e2a\u5706\u76d8\"\"\"\n    # \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n    pan = src.pop()\n    # \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n    tar.append(pan)\n\ndef dfs(i: int, src: list[int], buf: list[int], tar: list[int]):\n    \"\"\"\u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i)\"\"\"\n    # \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n    if i == 1:\n        move(src, tar)\n        return\n    # \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n    dfs(i - 1, src, tar, buf)\n    # \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n    move(src, tar)\n    # \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n    dfs(i - 1, buf, src, tar)\n\ndef solve_hanota(A: list[int], B: list[int], C: list[int]):\n    \"\"\"\u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898\"\"\"\n    n = len(A)\n    # \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n    dfs(n, A, B, C)\n
        hanota.cpp
        /* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(vector<int> &src, vector<int> &tar) {\n    // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n    int pan = src.back();\n    src.pop_back();\n    // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n    tar.push_back(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nvoid dfs(int i, vector<int> &src, vector<int> &buf, vector<int> &tar) {\n    // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n    if (i == 1) {\n        move(src, tar);\n        return;\n    }\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n    dfs(i - 1, src, tar, buf);\n    // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n    move(src, tar);\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n    dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nvoid solveHanota(vector<int> &A, vector<int> &B, vector<int> &C) {\n    int n = A.size();\n    // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n    dfs(n, A, B, C);\n}\n
        hanota.java
        /* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(List<Integer> src, List<Integer> tar) {\n    // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n    Integer pan = src.remove(src.size() - 1);\n    // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n    tar.add(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nvoid dfs(int i, List<Integer> src, List<Integer> buf, List<Integer> tar) {\n    // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n    if (i == 1) {\n        move(src, tar);\n        return;\n    }\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n    dfs(i - 1, src, tar, buf);\n    // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n    move(src, tar);\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n    dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nvoid solveHanota(List<Integer> A, List<Integer> B, List<Integer> C) {\n    int n = A.size();\n    // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n    dfs(n, A, B, C);\n}\n
        hanota.cs
        /* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid Move(List<int> src, List<int> tar) {\n    // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n    int pan = src[^1];\n    src.RemoveAt(src.Count - 1);\n    // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n    tar.Add(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nvoid DFS(int i, List<int> src, List<int> buf, List<int> tar) {\n    // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n    if (i == 1) {\n        Move(src, tar);\n        return;\n    }\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n    DFS(i - 1, src, tar, buf);\n    // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n    Move(src, tar);\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n    DFS(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nvoid SolveHanota(List<int> A, List<int> B, List<int> C) {\n    int n = A.Count;\n    // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n    DFS(n, A, B, C);\n}\n
        hanota.go
        /* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunc move(src, tar *list.List) {\n    // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n    pan := src.Back()\n    // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n    tar.PushBack(pan.Value)\n    // \u79fb\u9664 src \u9876\u90e8\u5706\u76d8\n    src.Remove(pan)\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nfunc dfsHanota(i int, src, buf, tar *list.List) {\n    // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n    if i == 1 {\n        move(src, tar)\n        return\n    }\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n    dfsHanota(i-1, src, tar, buf)\n    // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n    move(src, tar)\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n    dfsHanota(i-1, buf, src, tar)\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nfunc solveHanota(A, B, C *list.List) {\n    n := A.Len()\n    // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n    dfsHanota(n, A, B, C)\n}\n
        hanota.swift
        /* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunc move(src: inout [Int], tar: inout [Int]) {\n    // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n    let pan = src.popLast()!\n    // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n    tar.append(pan)\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nfunc dfs(i: Int, src: inout [Int], buf: inout [Int], tar: inout [Int]) {\n    // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n    if i == 1 {\n        move(src: &src, tar: &tar)\n        return\n    }\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n    dfs(i: i - 1, src: &src, buf: &tar, tar: &buf)\n    // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n    move(src: &src, tar: &tar)\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n    dfs(i: i - 1, src: &buf, buf: &src, tar: &tar)\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nfunc solveHanota(A: inout [Int], B: inout [Int], C: inout [Int]) {\n    let n = A.count\n    // \u5217\u8868\u5c3e\u90e8\u662f\u67f1\u5b50\u9876\u90e8\n    // \u5c06 src \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n    dfs(i: n, src: &A, buf: &B, tar: &C)\n}\n
        hanota.js
        /* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunction move(src, tar) {\n    // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n    const pan = src.pop();\n    // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n    tar.push(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nfunction dfs(i, src, buf, tar) {\n    // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n    if (i === 1) {\n        move(src, tar);\n        return;\n    }\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n    dfs(i - 1, src, tar, buf);\n    // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n    move(src, tar);\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n    dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nfunction solveHanota(A, B, C) {\n    const n = A.length;\n    // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n    dfs(n, A, B, C);\n}\n
        hanota.ts
        /* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunction move(src: number[], tar: number[]): void {\n    // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n    const pan = src.pop();\n    // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n    tar.push(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nfunction dfs(i: number, src: number[], buf: number[], tar: number[]): void {\n    // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n    if (i === 1) {\n        move(src, tar);\n        return;\n    }\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n    dfs(i - 1, src, tar, buf);\n    // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n    move(src, tar);\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n    dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nfunction solveHanota(A: number[], B: number[], C: number[]): void {\n    const n = A.length;\n    // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n    dfs(n, A, B, C);\n}\n
        hanota.dart
        /* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(List<int> src, List<int> tar) {\n  // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n  int pan = src.removeLast();\n  // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n  tar.add(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nvoid dfs(int i, List<int> src, List<int> buf, List<int> tar) {\n  // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n  if (i == 1) {\n    move(src, tar);\n    return;\n  }\n  // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n  dfs(i - 1, src, tar, buf);\n  // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n  move(src, tar);\n  // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n  dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nvoid solveHanota(List<int> A, List<int> B, List<int> C) {\n  int n = A.length;\n  // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n  dfs(n, A, B, C);\n}\n
        hanota.rs
        /* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfn move_pan(src: &mut Vec<i32>, tar: &mut Vec<i32>) {\n    // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n    let pan = src.pop().unwrap();\n    // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n    tar.push(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nfn dfs(i: i32, src: &mut Vec<i32>, buf: &mut Vec<i32>, tar: &mut Vec<i32>) {\n    // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n    if i == 1 {\n        move_pan(src, tar);\n        return;\n    }\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n    dfs(i - 1, src, tar, buf);\n    // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n    move_pan(src, tar);\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n    dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nfn solve_hanota(A: &mut Vec<i32>, B: &mut Vec<i32>, C: &mut Vec<i32>) {\n    let n = A.len() as i32;\n    // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n    dfs(n, A, B, C);\n}\n
        hanota.c
        /* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(int *src, int *srcSize, int *tar, int *tarSize) {\n    // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n    int pan = src[*srcSize - 1];\n    src[*srcSize - 1] = 0;\n    (*srcSize)--;\n    // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n    tar[*tarSize] = pan;\n    (*tarSize)++;\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nvoid dfs(int i, int *src, int *srcSize, int *buf, int *bufSize, int *tar, int *tarSize) {\n    // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n    if (i == 1) {\n        move(src, srcSize, tar, tarSize);\n        return;\n    }\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n    dfs(i - 1, src, srcSize, tar, tarSize, buf, bufSize);\n    // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n    move(src, srcSize, tar, tarSize);\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n    dfs(i - 1, buf, bufSize, src, srcSize, tar, tarSize);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nvoid solveHanota(int *A, int *ASize, int *B, int *BSize, int *C, int *CSize) {\n    // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n    dfs(*ASize, A, ASize, B, BSize, C, CSize);\n}\n
        hanota.kt
        /* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfun move(src: MutableList<Int>, tar: MutableList<Int>) {\n    // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n    val pan = src.removeAt(src.size - 1)\n    // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n    tar.add(pan)\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nfun dfs(i: Int, src: MutableList<Int>, buf: MutableList<Int>, tar: MutableList<Int>) {\n    // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n    if (i == 1) {\n        move(src, tar)\n        return\n    }\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n    dfs(i - 1, src, tar, buf)\n    // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n    move(src, tar)\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n    dfs(i - 1, buf, src, tar)\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nfun solveHanota(A: MutableList<Int>, B: MutableList<Int>, C: MutableList<Int>) {\n    val n = A.size\n    // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n    dfs(n, A, B, C)\n}\n
        hanota.rb
        ### \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 ###\ndef move(src, tar)\n  # \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n  pan = src.pop\n  # \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n  tar << pan\nend\n\n### \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) ###\ndef dfs(i, src, buf, tar)\n  # \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n  if i == 1\n    move(src, tar)\n    return\n  end\n\n  # \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n  dfs(i - 1, src, tar, buf)\n  # \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n  move(src, tar)\n  # \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n  dfs(i - 1, buf, src, tar)\nend\n\n### \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 ###\ndef solve_hanota(_A, _B, _C)\n  n = _A.length\n  # \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n  dfs(n, _A, _B, _C)\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5982\u56fe 12-15 \u6240\u793a\uff0c\u6c49\u8bfa\u5854\u95ee\u9898\u5f62\u6210\u4e00\u68f5\u9ad8\u5ea6\u4e3a \\(n\\) \u7684\u9012\u5f52\u6811\uff0c\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u5b50\u95ee\u9898\uff0c\u5bf9\u5e94\u4e00\u4e2a\u5f00\u542f\u7684 dfs() \u51fd\u6570\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

        \u56fe 12-15 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898\u7684\u9012\u5f52\u6811

        Quote

        \u6c49\u8bfa\u5854\u95ee\u9898\u6e90\u81ea\u4e00\u4e2a\u53e4\u8001\u7684\u4f20\u8bf4\u3002\u5728\u53e4\u5370\u5ea6\u7684\u4e00\u4e2a\u5bfa\u5e99\u91cc\uff0c\u50e7\u4fa3\u4eec\u6709\u4e09\u6839\u9ad8\u5927\u7684\u94bb\u77f3\u67f1\u5b50\uff0c\u4ee5\u53ca \\(64\\) \u4e2a\u5927\u5c0f\u4e0d\u4e00\u7684\u91d1\u5706\u76d8\u3002\u50e7\u4fa3\u4eec\u4e0d\u65ad\u5730\u79fb\u52a8\u5706\u76d8\uff0c\u4ed6\u4eec\u76f8\u4fe1\u5728\u6700\u540e\u4e00\u4e2a\u5706\u76d8\u88ab\u6b63\u786e\u653e\u7f6e\u7684\u90a3\u4e00\u523b\uff0c\u8fd9\u4e2a\u4e16\u754c\u5c31\u4f1a\u7ed3\u675f\u3002

        \u7136\u800c\uff0c\u5373\u4f7f\u50e7\u4fa3\u4eec\u6bcf\u79d2\u949f\u79fb\u52a8\u4e00\u6b21\uff0c\u603b\u5171\u9700\u8981\u5927\u7ea6 \\(2^{64} \\approx 1.84\u00d710^{19}\\) \u79d2\uff0c\u5408\u7ea6 \\(5850\\) \u4ebf\u5e74\uff0c\u8fdc\u8fdc\u8d85\u8fc7\u4e86\u73b0\u5728\u5bf9\u5b87\u5b99\u5e74\u9f84\u7684\u4f30\u8ba1\u3002\u6240\u4ee5\uff0c\u5018\u82e5\u8fd9\u4e2a\u4f20\u8bf4\u662f\u771f\u7684\uff0c\u6211\u4eec\u5e94\u8be5\u4e0d\u9700\u8981\u62c5\u5fc3\u4e16\u754c\u672b\u65e5\u7684\u5230\u6765\u3002

        "},{"location":"chapter_divide_and_conquer/summary/","title":"12.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_divide_and_conquer/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u5206\u6cbb\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7b97\u6cd5\u8bbe\u8ba1\u7b56\u7565\uff0c\u5305\u62ec\u5206\uff08\u5212\u5206\uff09\u548c\u6cbb\uff08\u5408\u5e76\uff09\u4e24\u4e2a\u9636\u6bb5\uff0c\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\u3002
        • \u5224\u65ad\u662f\u5426\u662f\u5206\u6cbb\u7b97\u6cd5\u95ee\u9898\u7684\u4f9d\u636e\u5305\u62ec\uff1a\u95ee\u9898\u80fd\u5426\u5206\u89e3\u3001\u5b50\u95ee\u9898\u662f\u5426\u72ec\u7acb\u3001\u5b50\u95ee\u9898\u80fd\u5426\u5408\u5e76\u3002
        • \u5f52\u5e76\u6392\u5e8f\u662f\u5206\u6cbb\u7b56\u7565\u7684\u5178\u578b\u5e94\u7528\uff0c\u5176\u9012\u5f52\u5730\u5c06\u6570\u7ec4\u5212\u5206\u4e3a\u7b49\u957f\u7684\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u76f4\u5230\u53ea\u5269\u4e00\u4e2a\u5143\u7d20\u65f6\u5f00\u59cb\u9010\u5c42\u5408\u5e76\uff0c\u4ece\u800c\u5b8c\u6210\u6392\u5e8f\u3002
        • \u5f15\u5165\u5206\u6cbb\u7b56\u7565\u5f80\u5f80\u53ef\u4ee5\u63d0\u5347\u7b97\u6cd5\u6548\u7387\u3002\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u7b56\u7565\u51cf\u5c11\u4e86\u64cd\u4f5c\u6570\u91cf\uff1b\u53e6\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u540e\u6709\u5229\u4e8e\u7cfb\u7edf\u7684\u5e76\u884c\u4f18\u5316\u3002
        • \u5206\u6cbb\u65e2\u53ef\u4ee5\u89e3\u51b3\u8bb8\u591a\u7b97\u6cd5\u95ee\u9898\uff0c\u4e5f\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u8bbe\u8ba1\u4e2d\uff0c\u5904\u5904\u53ef\u89c1\u5176\u8eab\u5f71\u3002
        • \u76f8\u8f83\u4e8e\u66b4\u529b\u641c\u7d22\uff0c\u81ea\u9002\u5e94\u641c\u7d22\u6548\u7387\u66f4\u9ad8\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u7684\u641c\u7d22\u7b97\u6cd5\u901a\u5e38\u662f\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u5b9e\u73b0\u7684\u3002
        • \u4e8c\u5206\u67e5\u627e\u662f\u5206\u6cbb\u7b56\u7565\u7684\u53e6\u4e00\u4e2a\u5178\u578b\u5e94\u7528\uff0c\u5b83\u4e0d\u5305\u542b\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u8fdb\u884c\u5408\u5e76\u7684\u6b65\u9aa4\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u5206\u6cbb\u5b9e\u73b0\u4e8c\u5206\u67e5\u627e\u3002
        • \u5728\u6784\u5efa\u4e8c\u53c9\u6811\u7684\u95ee\u9898\u4e2d\uff0c\u6784\u5efa\u6811\uff08\u539f\u95ee\u9898\uff09\u53ef\u4ee5\u5212\u5206\u4e3a\u6784\u5efa\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\uff08\u5b50\u95ee\u9898\uff09\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5212\u5206\u524d\u5e8f\u904d\u5386\u548c\u4e2d\u5e8f\u904d\u5386\u7684\u7d22\u5f15\u533a\u95f4\u6765\u5b9e\u73b0\u3002
        • \u5728\u6c49\u8bfa\u5854\u95ee\u9898\u4e2d\uff0c\u4e00\u4e2a\u89c4\u6a21\u4e3a \\(n\\) \u7684\u95ee\u9898\u53ef\u4ee5\u5212\u5206\u4e3a\u4e24\u4e2a\u89c4\u6a21\u4e3a \\(n-1\\) \u7684\u5b50\u95ee\u9898\u548c\u4e00\u4e2a\u89c4\u6a21\u4e3a \\(1\\) \u7684\u5b50\u95ee\u9898\u3002\u6309\u987a\u5e8f\u89e3\u51b3\u8fd9\u4e09\u4e2a\u5b50\u95ee\u9898\u540e\uff0c\u539f\u95ee\u9898\u968f\u4e4b\u5f97\u5230\u89e3\u51b3\u3002
        "},{"location":"chapter_dynamic_programming/","title":"\u7b2c 14 \u7ae0 \u00a0 \u52a8\u6001\u89c4\u5212","text":"

        Abstract

        \u5c0f\u6eaa\u6c47\u5165\u6cb3\u6d41\uff0c\u6c5f\u6cb3\u6c47\u5165\u5927\u6d77\u3002

        \u52a8\u6001\u89c4\u5212\u5c06\u5c0f\u95ee\u9898\u7684\u89e3\u6c47\u96c6\u6210\u5927\u95ee\u9898\u7684\u7b54\u6848\uff0c\u4e00\u6b65\u6b65\u5f15\u9886\u6211\u4eec\u8d70\u5411\u89e3\u51b3\u95ee\u9898\u7684\u5f7c\u5cb8\u3002

        "},{"location":"chapter_dynamic_programming/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 14.1 \u00a0 \u521d\u63a2\u52a8\u6001\u89c4\u5212
        • 14.2 \u00a0 DP \u95ee\u9898\u7279\u6027
        • 14.3 \u00a0 DP \u89e3\u9898\u601d\u8def
        • 14.4 \u00a0 0-1 \u80cc\u5305\u95ee\u9898
        • 14.5 \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898
        • 14.6 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898
        • 14.7 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_dynamic_programming/dp_problem_features/","title":"14.2 \u00a0 \u52a8\u6001\u89c4\u5212\u95ee\u9898\u7279\u6027","text":"

        \u5728\u4e0a\u4e00\u8282\u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u52a8\u6001\u89c4\u5212\u662f\u5982\u4f55\u901a\u8fc7\u5b50\u95ee\u9898\u5206\u89e3\u6765\u6c42\u89e3\u539f\u95ee\u9898\u7684\u3002\u5b9e\u9645\u4e0a\uff0c\u5b50\u95ee\u9898\u5206\u89e3\u662f\u4e00\u79cd\u901a\u7528\u7684\u7b97\u6cd5\u601d\u8def\uff0c\u5728\u5206\u6cbb\u3001\u52a8\u6001\u89c4\u5212\u3001\u56de\u6eaf\u4e2d\u7684\u4fa7\u91cd\u70b9\u4e0d\u540c\u3002

        • \u5206\u6cbb\u7b97\u6cd5\u9012\u5f52\u5730\u5c06\u539f\u95ee\u9898\u5212\u5206\u4e3a\u591a\u4e2a\u76f8\u4e92\u72ec\u7acb\u7684\u5b50\u95ee\u9898\uff0c\u76f4\u81f3\u6700\u5c0f\u5b50\u95ee\u9898\uff0c\u5e76\u5728\u56de\u6eaf\u4e2d\u5408\u5e76\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u6700\u7ec8\u5f97\u5230\u539f\u95ee\u9898\u7684\u89e3\u3002
        • \u52a8\u6001\u89c4\u5212\u4e5f\u5bf9\u95ee\u9898\u8fdb\u884c\u9012\u5f52\u5206\u89e3\uff0c\u4f46\u4e0e\u5206\u6cbb\u7b97\u6cd5\u7684\u4e3b\u8981\u533a\u522b\u662f\uff0c\u52a8\u6001\u89c4\u5212\u4e2d\u7684\u5b50\u95ee\u9898\u662f\u76f8\u4e92\u4f9d\u8d56\u7684\uff0c\u5728\u5206\u89e3\u8fc7\u7a0b\u4e2d\u4f1a\u51fa\u73b0\u8bb8\u591a\u91cd\u53e0\u5b50\u95ee\u9898\u3002
        • \u56de\u6eaf\u7b97\u6cd5\u5728\u5c1d\u8bd5\u548c\u56de\u9000\u4e2d\u7a77\u4e3e\u6240\u6709\u53ef\u80fd\u7684\u89e3\uff0c\u5e76\u901a\u8fc7\u526a\u679d\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u641c\u7d22\u5206\u652f\u3002\u539f\u95ee\u9898\u7684\u89e3\u7531\u4e00\u7cfb\u5217\u51b3\u7b56\u6b65\u9aa4\u6784\u6210\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6bcf\u4e2a\u51b3\u7b56\u6b65\u9aa4\u4e4b\u524d\u7684\u5b50\u5e8f\u5217\u770b\u4f5c\u4e00\u4e2a\u5b50\u95ee\u9898\u3002

        \u5b9e\u9645\u4e0a\uff0c\u52a8\u6001\u89c4\u5212\u5e38\u7528\u6765\u6c42\u89e3\u6700\u4f18\u5316\u95ee\u9898\uff0c\u5b83\u4eec\u4e0d\u4ec5\u5305\u542b\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u8fd8\u5177\u6709\u53e6\u5916\u4e24\u5927\u7279\u6027\uff1a\u6700\u4f18\u5b50\u7ed3\u6784\u3001\u65e0\u540e\u6548\u6027\u3002

        "},{"location":"chapter_dynamic_programming/dp_problem_features/#1421","title":"14.2.1 \u00a0 \u6700\u4f18\u5b50\u7ed3\u6784","text":"

        \u6211\u4eec\u5bf9\u722c\u697c\u68af\u95ee\u9898\u7a0d\u4f5c\u6539\u52a8\uff0c\u4f7f\u4e4b\u66f4\u52a0\u9002\u5408\u5c55\u793a\u6700\u4f18\u5b50\u7ed3\u6784\u6982\u5ff5\u3002

        \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7

        \u7ed9\u5b9a\u4e00\u4e2a\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\uff0c\u6bcf\u4e00\u9636\u697c\u68af\u4e0a\u90fd\u8d34\u6709\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\uff0c\u8868\u793a\u4f60\u5728\u8be5\u53f0\u9636\u6240\u9700\u8981\u4ed8\u51fa\u7684\u4ee3\u4ef7\u3002\u7ed9\u5b9a\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\u6570\u7ec4 \\(cost\\) \uff0c\u5176\u4e2d \\(cost[i]\\) \u8868\u793a\u5728\u7b2c \\(i\\) \u4e2a\u53f0\u9636\u9700\u8981\u4ed8\u51fa\u7684\u4ee3\u4ef7\uff0c\\(cost[0]\\) \u4e3a\u5730\u9762\uff08\u8d77\u59cb\u70b9\uff09\u3002\u8bf7\u8ba1\u7b97\u6700\u5c11\u9700\u8981\u4ed8\u51fa\u591a\u5c11\u4ee3\u4ef7\u624d\u80fd\u5230\u8fbe\u9876\u90e8\uff1f

        \u5982\u56fe 14-6 \u6240\u793a\uff0c\u82e5\u7b2c \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u9636\u7684\u4ee3\u4ef7\u5206\u522b\u4e3a \\(1\\)\u3001\\(10\\)\u3001\\(1\\) \uff0c\u5219\u4ece\u5730\u9762\u722c\u5230\u7b2c \\(3\\) \u9636\u7684\u6700\u5c0f\u4ee3\u4ef7\u4e3a \\(2\\) \u3002

        \u56fe 14-6 \u00a0 \u722c\u5230\u7b2c 3 \u9636\u7684\u6700\u5c0f\u4ee3\u4ef7

        \u8bbe \\(dp[i]\\) \u4e3a\u722c\u5230\u7b2c \\(i\\) \u9636\u7d2f\u8ba1\u4ed8\u51fa\u7684\u4ee3\u4ef7\uff0c\u7531\u4e8e\u7b2c \\(i\\) \u9636\u53ea\u53ef\u80fd\u4ece \\(i - 1\\) \u9636\u6216 \\(i - 2\\) \u9636\u8d70\u6765\uff0c\u56e0\u6b64 \\(dp[i]\\) \u53ea\u53ef\u80fd\u7b49\u4e8e \\(dp[i - 1] + cost[i]\\) \u6216 \\(dp[i - 2] + cost[i]\\) \u3002\u4e3a\u4e86\u5c3d\u53ef\u80fd\u51cf\u5c11\u4ee3\u4ef7\uff0c\u6211\u4eec\u5e94\u8be5\u9009\u62e9\u4e24\u8005\u4e2d\u8f83\u5c0f\u7684\u90a3\u4e00\u4e2a\uff1a

        \\[ dp[i] = \\min(dp[i-1], dp[i-2]) + cost[i] \\]

        \u8fd9\u4fbf\u53ef\u4ee5\u5f15\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\u7684\u542b\u4e49\uff1a\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u662f\u4ece\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u6784\u5efa\u5f97\u6765\u7684\u3002

        \u672c\u9898\u663e\u7136\u5177\u6709\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\u6211\u4eec\u4ece\u4e24\u4e2a\u5b50\u95ee\u9898\u6700\u4f18\u89e3 \\(dp[i-1]\\) \u548c \\(dp[i-2]\\) \u4e2d\u6311\u9009\u51fa\u8f83\u4f18\u7684\u90a3\u4e00\u4e2a\uff0c\u5e76\u7528\u5b83\u6784\u5efa\u51fa\u539f\u95ee\u9898 \\(dp[i]\\) \u7684\u6700\u4f18\u89e3\u3002

        \u90a3\u4e48\uff0c\u4e0a\u4e00\u8282\u7684\u722c\u697c\u68af\u9898\u76ee\u6709\u6ca1\u6709\u6700\u4f18\u5b50\u7ed3\u6784\u5462\uff1f\u5b83\u7684\u76ee\u6807\u662f\u6c42\u89e3\u65b9\u6848\u6570\u91cf\uff0c\u770b\u4f3c\u662f\u4e00\u4e2a\u8ba1\u6570\u95ee\u9898\uff0c\u4f46\u5982\u679c\u6362\u4e00\u79cd\u95ee\u6cd5\uff1a\u201c\u6c42\u89e3\u6700\u5927\u65b9\u6848\u6570\u91cf\u201d\u3002\u6211\u4eec\u610f\u5916\u5730\u53d1\u73b0\uff0c\u867d\u7136\u9898\u76ee\u4fee\u6539\u524d\u540e\u662f\u7b49\u4ef7\u7684\uff0c\u4f46\u6700\u4f18\u5b50\u7ed3\u6784\u6d6e\u73b0\u51fa\u6765\u4e86\uff1a\u7b2c \\(n\\) \u9636\u6700\u5927\u65b9\u6848\u6570\u91cf\u7b49\u4e8e\u7b2c \\(n-1\\) \u9636\u548c\u7b2c \\(n-2\\) \u9636\u6700\u5927\u65b9\u6848\u6570\u91cf\u4e4b\u548c\u3002\u6240\u4ee5\u8bf4\uff0c\u6700\u4f18\u5b50\u7ed3\u6784\u7684\u89e3\u91ca\u65b9\u5f0f\u6bd4\u8f83\u7075\u6d3b\uff0c\u5728\u4e0d\u540c\u95ee\u9898\u4e2d\u4f1a\u6709\u4e0d\u540c\u7684\u542b\u4e49\u3002

        \u6839\u636e\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff0c\u4ee5\u53ca\u521d\u59cb\u72b6\u6001 \\(dp[1] = cost[1]\\) \u548c \\(dp[2] = cost[2]\\) \uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f97\u5230\u52a8\u6001\u89c4\u5212\u4ee3\u7801\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_cost_climbing_stairs_dp.py
        def min_cost_climbing_stairs_dp(cost: list[int]) -> int:\n    \"\"\"\u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n    n = len(cost) - 1\n    if n == 1 or n == 2:\n        return cost[n]\n    # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    dp = [0] * (n + 1)\n    # \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1], dp[2] = cost[1], cost[2]\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i in range(3, n + 1):\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n    return dp[n]\n
        min_cost_climbing_stairs_dp.cpp
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(vector<int> &cost) {\n    int n = cost.size() - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    vector<int> dp(n + 1);\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
        min_cost_climbing_stairs_dp.java
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(int[] cost) {\n    int n = cost.length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    int[] dp = new int[n + 1];\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
        min_cost_climbing_stairs_dp.cs
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint MinCostClimbingStairsDP(int[] cost) {\n    int n = cost.Length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    int[] dp = new int[n + 1];\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i] = Math.Min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
        min_cost_climbing_stairs_dp.go
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDP(cost []int) int {\n    n := len(cost) - 1\n    if n == 1 || n == 2 {\n        return cost[n]\n    }\n    min := func(a, b int) int {\n        if a < b {\n            return a\n        }\n        return b\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    dp := make([]int, n+1)\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = cost[1]\n    dp[2] = cost[2]\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i := 3; i <= n; i++ {\n        dp[i] = min(dp[i-1], dp[i-2]) + cost[i]\n    }\n    return dp[n]\n}\n
        min_cost_climbing_stairs_dp.swift
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDP(cost: [Int]) -> Int {\n    let n = cost.count - 1\n    if n == 1 || n == 2 {\n        return cost[n]\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    var dp = Array(repeating: 0, count: n + 1)\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = cost[1]\n    dp[2] = cost[2]\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i in 3 ... n {\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n    }\n    return dp[n]\n}\n
        min_cost_climbing_stairs_dp.js
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDP(cost) {\n    const n = cost.length - 1;\n    if (n === 1 || n === 2) {\n        return cost[n];\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    const dp = new Array(n + 1);\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (let i = 3; i <= n; i++) {\n        dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
        min_cost_climbing_stairs_dp.ts
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDP(cost: Array<number>): number {\n    const n = cost.length - 1;\n    if (n === 1 || n === 2) {\n        return cost[n];\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    const dp = new Array(n + 1);\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (let i = 3; i <= n; i++) {\n        dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
        min_cost_climbing_stairs_dp.dart
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(List<int> cost) {\n  int n = cost.length - 1;\n  if (n == 1 || n == 2) return cost[n];\n  // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n  List<int> dp = List.filled(n + 1, 0);\n  // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n  dp[1] = cost[1];\n  dp[2] = cost[2];\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n  for (int i = 3; i <= n; i++) {\n    dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n  }\n  return dp[n];\n}\n
        min_cost_climbing_stairs_dp.rs
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfn min_cost_climbing_stairs_dp(cost: &[i32]) -> i32 {\n    let n = cost.len() - 1;\n    if n == 1 || n == 2 {\n        return cost[n];\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    let mut dp = vec![-1; n + 1];\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i in 3..=n {\n        dp[i] = cmp::min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    dp[n]\n}\n
        min_cost_climbing_stairs_dp.c
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(int cost[], int costSize) {\n    int n = costSize - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    int *dp = calloc(n + 1, sizeof(int));\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i] = myMin(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    int res = dp[n];\n    // \u91ca\u653e\u5185\u5b58\n    free(dp);\n    return res;\n}\n
        min_cost_climbing_stairs_dp.kt
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfun minCostClimbingStairsDP(cost: IntArray): Int {\n    val n = cost.size - 1\n    if (n == 1 || n == 2) return cost[n]\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    val dp = IntArray(n + 1)\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = cost[1]\n    dp[2] = cost[2]\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (i in 3..n) {\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n    }\n    return dp[n]\n}\n
        min_cost_climbing_stairs_dp.rb
        ### \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 ###\ndef min_cost_climbing_stairs_dp(cost)\n  n = cost.length - 1\n  return cost[n] if n == 1 || n == 2\n  # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n  dp = Array.new(n + 1, 0)\n  # \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n  dp[1], dp[2] = cost[1], cost[2]\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n  (3...(n + 1)).each { |i| dp[i] = [dp[i - 1], dp[i - 2]].min + cost[i] }\n  dp[n]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 14-7 \u5c55\u793a\u4e86\u4ee5\u4e0a\u4ee3\u7801\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b\u3002

        \u56fe 14-7 \u00a0 \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

        \u672c\u9898\u4e5f\u53ef\u4ee5\u8fdb\u884c\u7a7a\u95f4\u4f18\u5316\uff0c\u5c06\u4e00\u7ef4\u538b\u7f29\u81f3\u96f6\u7ef4\uff0c\u4f7f\u5f97\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n)\\) \u964d\u81f3 \\(O(1)\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_cost_climbing_stairs_dp.py
        def min_cost_climbing_stairs_dp_comp(cost: list[int]) -> int:\n    \"\"\"\u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n    n = len(cost) - 1\n    if n == 1 or n == 2:\n        return cost[n]\n    a, b = cost[1], cost[2]\n    for i in range(3, n + 1):\n        a, b = b, min(a, b) + cost[i]\n    return b\n
        min_cost_climbing_stairs_dp.cpp
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(vector<int> &cost) {\n    int n = cost.size() - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
        min_cost_climbing_stairs_dp.java
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(int[] cost) {\n    int n = cost.length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = Math.min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
        min_cost_climbing_stairs_dp.cs
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint MinCostClimbingStairsDPComp(int[] cost) {\n    int n = cost.Length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = Math.Min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
        min_cost_climbing_stairs_dp.go
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDPComp(cost []int) int {\n    n := len(cost) - 1\n    if n == 1 || n == 2 {\n        return cost[n]\n    }\n    min := func(a, b int) int {\n        if a < b {\n            return a\n        }\n        return b\n    }\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    a, b := cost[1], cost[2]\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i := 3; i <= n; i++ {\n        tmp := b\n        b = min(a, tmp) + cost[i]\n        a = tmp\n    }\n    return b\n}\n
        min_cost_climbing_stairs_dp.swift
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDPComp(cost: [Int]) -> Int {\n    let n = cost.count - 1\n    if n == 1 || n == 2 {\n        return cost[n]\n    }\n    var (a, b) = (cost[1], cost[2])\n    for i in 3 ... n {\n        (a, b) = (b, min(a, b) + cost[i])\n    }\n    return b\n}\n
        min_cost_climbing_stairs_dp.js
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDPComp(cost) {\n    const n = cost.length - 1;\n    if (n === 1 || n === 2) {\n        return cost[n];\n    }\n    let a = cost[1],\n        b = cost[2];\n    for (let i = 3; i <= n; i++) {\n        const tmp = b;\n        b = Math.min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
        min_cost_climbing_stairs_dp.ts
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDPComp(cost: Array<number>): number {\n    const n = cost.length - 1;\n    if (n === 1 || n === 2) {\n        return cost[n];\n    }\n    let a = cost[1],\n        b = cost[2];\n    for (let i = 3; i <= n; i++) {\n        const tmp = b;\n        b = Math.min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
        min_cost_climbing_stairs_dp.dart
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(List<int> cost) {\n  int n = cost.length - 1;\n  if (n == 1 || n == 2) return cost[n];\n  int a = cost[1], b = cost[2];\n  for (int i = 3; i <= n; i++) {\n    int tmp = b;\n    b = min(a, tmp) + cost[i];\n    a = tmp;\n  }\n  return b;\n}\n
        min_cost_climbing_stairs_dp.rs
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn min_cost_climbing_stairs_dp_comp(cost: &[i32]) -> i32 {\n    let n = cost.len() - 1;\n    if n == 1 || n == 2 {\n        return cost[n];\n    };\n    let (mut a, mut b) = (cost[1], cost[2]);\n    for i in 3..=n {\n        let tmp = b;\n        b = cmp::min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    b\n}\n
        min_cost_climbing_stairs_dp.c
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(int cost[], int costSize) {\n    int n = costSize - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = myMin(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
        min_cost_climbing_stairs_dp.kt
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfun minCostClimbingStairsDPComp(cost: IntArray): Int {\n    val n = cost.size - 1\n    if (n == 1 || n == 2) return cost[n]\n    var a = cost[1]\n    var b = cost[2]\n    for (i in 3..n) {\n        val tmp = b\n        b = min(a, tmp) + cost[i]\n        a = tmp\n    }\n    return b\n}\n
        min_cost_climbing_stairs_dp.rb
        ### \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 ###\ndef min_cost_climbing_stairs_dp(cost)\n  n = cost.length - 1\n  return cost[n] if n == 1 || n == 2\n  # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n  dp = Array.new(n + 1, 0)\n  # \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n  dp[1], dp[2] = cost[1], cost[2]\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n  (3...(n + 1)).each { |i| dp[i] = [dp[i - 1], dp[i - 2]].min + cost[i] }\n  dp[n]\nend\n\n# \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\ndef min_cost_climbing_stairs_dp_comp(cost)\n  n = cost.length - 1\n  return cost[n] if n == 1 || n == 2\n  a, b = cost[1], cost[2]\n  (3...(n + 1)).each { |i| a, b = b, [a, b].min + cost[i] }\n  b\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_dynamic_programming/dp_problem_features/#1422","title":"14.2.2 \u00a0 \u65e0\u540e\u6548\u6027","text":"

        \u65e0\u540e\u6548\u6027\u662f\u52a8\u6001\u89c4\u5212\u80fd\u591f\u6709\u6548\u89e3\u51b3\u95ee\u9898\u7684\u91cd\u8981\u7279\u6027\u4e4b\u4e00\uff0c\u5176\u5b9a\u4e49\u4e3a\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u786e\u5b9a\u7684\u72b6\u6001\uff0c\u5b83\u7684\u672a\u6765\u53d1\u5c55\u53ea\u4e0e\u5f53\u524d\u72b6\u6001\u6709\u5173\uff0c\u800c\u4e0e\u8fc7\u53bb\u7ecf\u5386\u7684\u6240\u6709\u72b6\u6001\u65e0\u5173\u3002

        \u4ee5\u722c\u697c\u68af\u95ee\u9898\u4e3a\u4f8b\uff0c\u7ed9\u5b9a\u72b6\u6001 \\(i\\) \uff0c\u5b83\u4f1a\u53d1\u5c55\u51fa\u72b6\u6001 \\(i+1\\) \u548c\u72b6\u6001 \\(i+2\\) \uff0c\u5206\u522b\u5bf9\u5e94\u8df3 \\(1\\) \u6b65\u548c\u8df3 \\(2\\) \u6b65\u3002\u5728\u505a\u51fa\u8fd9\u4e24\u79cd\u9009\u62e9\u65f6\uff0c\u6211\u4eec\u65e0\u987b\u8003\u8651\u72b6\u6001 \\(i\\) \u4e4b\u524d\u7684\u72b6\u6001\uff0c\u5b83\u4eec\u5bf9\u72b6\u6001 \\(i\\) \u7684\u672a\u6765\u6ca1\u6709\u5f71\u54cd\u3002

        \u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u7ed9\u722c\u697c\u68af\u95ee\u9898\u6dfb\u52a0\u4e00\u4e2a\u7ea6\u675f\uff0c\u60c5\u51b5\u5c31\u4e0d\u4e00\u6837\u4e86\u3002

        \u5e26\u7ea6\u675f\u722c\u697c\u68af

        \u7ed9\u5b9a\u4e00\u4e2a\u5171\u6709 \\(n\\) \u9636\u7684\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\uff0c\u4f46\u4e0d\u80fd\u8fde\u7eed\u4e24\u8f6e\u8df3 \\(1\\) \u9636\uff0c\u8bf7\u95ee\u6709\u591a\u5c11\u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\uff1f

        \u5982\u56fe 14-8 \u6240\u793a\uff0c\u722c\u4e0a\u7b2c \\(3\\) \u9636\u4ec5\u5269 \\(2\\) \u79cd\u53ef\u884c\u65b9\u6848\uff0c\u5176\u4e2d\u8fde\u7eed\u4e09\u6b21\u8df3 \\(1\\) \u9636\u7684\u65b9\u6848\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\uff0c\u56e0\u6b64\u88ab\u820d\u5f03\u3002

        \u56fe 14-8 \u00a0 \u5e26\u7ea6\u675f\u722c\u5230\u7b2c 3 \u9636\u7684\u65b9\u6848\u6570\u91cf

        \u5728\u8be5\u95ee\u9898\u4e2d\uff0c\u5982\u679c\u4e0a\u4e00\u8f6e\u662f\u8df3 \\(1\\) \u9636\u4e0a\u6765\u7684\uff0c\u90a3\u4e48\u4e0b\u4e00\u8f6e\u5c31\u5fc5\u987b\u8df3 \\(2\\) \u9636\u3002\u8fd9\u610f\u5473\u7740\uff0c\u4e0b\u4e00\u6b65\u9009\u62e9\u4e0d\u80fd\u7531\u5f53\u524d\u72b6\u6001\uff08\u5f53\u524d\u6240\u5728\u697c\u68af\u9636\u6570\uff09\u72ec\u7acb\u51b3\u5b9a\uff0c\u8fd8\u548c\u524d\u4e00\u4e2a\u72b6\u6001\uff08\u4e0a\u4e00\u8f6e\u6240\u5728\u697c\u68af\u9636\u6570\uff09\u6709\u5173\u3002

        \u4e0d\u96be\u53d1\u73b0\uff0c\u6b64\u95ee\u9898\u5df2\u4e0d\u6ee1\u8db3\u65e0\u540e\u6548\u6027\uff0c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b \\(dp[i] = dp[i-1] + dp[i-2]\\) \u4e5f\u5931\u6548\u4e86\uff0c\u56e0\u4e3a \\(dp[i-1]\\) \u4ee3\u8868\u672c\u8f6e\u8df3 \\(1\\) \u9636\uff0c\u4f46\u5176\u4e2d\u5305\u542b\u4e86\u8bb8\u591a\u201c\u4e0a\u4e00\u8f6e\u662f\u8df3 \\(1\\) \u9636\u4e0a\u6765\u7684\u201d\u65b9\u6848\uff0c\u800c\u4e3a\u4e86\u6ee1\u8db3\u7ea6\u675f\uff0c\u6211\u4eec\u5c31\u4e0d\u80fd\u5c06 \\(dp[i-1]\\) \u76f4\u63a5\u8ba1\u5165 \\(dp[i]\\) \u4e2d\u3002

        \u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u6269\u5c55\u72b6\u6001\u5b9a\u4e49\uff1a\u72b6\u6001 \\([i, j]\\) \u8868\u793a\u5904\u5728\u7b2c \\(i\\) \u9636\u5e76\u4e14\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(j\\) \u9636\uff0c\u5176\u4e2d \\(j \\in \\{1, 2\\}\\) \u3002\u6b64\u72b6\u6001\u5b9a\u4e49\u6709\u6548\u5730\u533a\u5206\u4e86\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(1\\) \u9636\u8fd8\u662f \\(2\\) \u9636\uff0c\u6211\u4eec\u53ef\u4ee5\u636e\u6b64\u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u4ece\u4f55\u800c\u6765\u7684\u3002

        • \u5f53\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(1\\) \u9636\u65f6\uff0c\u4e0a\u4e0a\u4e00\u8f6e\u53ea\u80fd\u9009\u62e9\u8df3 \\(2\\) \u9636\uff0c\u5373 \\(dp[i, 1]\\) \u53ea\u80fd\u4ece \\(dp[i-1, 2]\\) \u8f6c\u79fb\u8fc7\u6765\u3002
        • \u5f53\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(2\\) \u9636\u65f6\uff0c\u4e0a\u4e0a\u4e00\u8f6e\u53ef\u9009\u62e9\u8df3 \\(1\\) \u9636\u6216\u8df3 \\(2\\) \u9636\uff0c\u5373 \\(dp[i, 2]\\) \u53ef\u4ee5\u4ece \\(dp[i-2, 1]\\) \u6216 \\(dp[i-2, 2]\\) \u8f6c\u79fb\u8fc7\u6765\u3002

        \u5982\u56fe 14-9 \u6240\u793a\uff0c\u5728\u8be5\u5b9a\u4e49\u4e0b\uff0c\\(dp[i, j]\\) \u8868\u793a\u72b6\u6001 \\([i, j]\\) \u5bf9\u5e94\u7684\u65b9\u6848\u6570\u3002\u6b64\u65f6\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a

        \\[ \\begin{cases} dp[i, 1] = dp[i-1, 2] \\\\ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2] \\end{cases} \\]

        \u56fe 14-9 \u00a0 \u8003\u8651\u7ea6\u675f\u4e0b\u7684\u9012\u63a8\u5173\u7cfb

        \u6700\u7ec8\uff0c\u8fd4\u56de \\(dp[n, 1] + dp[n, 2]\\) \u5373\u53ef\uff0c\u4e24\u8005\u4e4b\u548c\u4ee3\u8868\u722c\u5230\u7b2c \\(n\\) \u9636\u7684\u65b9\u6848\u603b\u6570\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_constraint_dp.py
        def climbing_stairs_constraint_dp(n: int) -> int:\n    \"\"\"\u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n    if n == 1 or n == 2:\n        return 1\n    # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    dp = [[0] * 3 for _ in range(n + 1)]\n    # \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1][1], dp[1][2] = 1, 0\n    dp[2][1], dp[2][2] = 0, 1\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i in range(3, n + 1):\n        dp[i][1] = dp[i - 1][2]\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n    return dp[n][1] + dp[n][2]\n
        climbing_stairs_constraint_dp.cpp
        /* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    vector<vector<int>> dp(n + 1, vector<int>(3, 0));\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
        climbing_stairs_constraint_dp.java
        /* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    int[][] dp = new int[n + 1][3];\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
        climbing_stairs_constraint_dp.cs
        /* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint ClimbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    int[,] dp = new int[n + 1, 3];\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1, 1] = 1;\n    dp[1, 2] = 0;\n    dp[2, 1] = 0;\n    dp[2, 2] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i, 1] = dp[i - 1, 2];\n        dp[i, 2] = dp[i - 2, 1] + dp[i - 2, 2];\n    }\n    return dp[n, 1] + dp[n, 2];\n}\n
        climbing_stairs_constraint_dp.go
        /* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsConstraintDP(n int) int {\n    if n == 1 || n == 2 {\n        return 1\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    dp := make([][3]int, n+1)\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1][1] = 1\n    dp[1][2] = 0\n    dp[2][1] = 0\n    dp[2][2] = 1\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i := 3; i <= n; i++ {\n        dp[i][1] = dp[i-1][2]\n        dp[i][2] = dp[i-2][1] + dp[i-2][2]\n    }\n    return dp[n][1] + dp[n][2]\n}\n
        climbing_stairs_constraint_dp.swift
        /* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsConstraintDP(n: Int) -> Int {\n    if n == 1 || n == 2 {\n        return 1\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    var dp = Array(repeating: Array(repeating: 0, count: 3), count: n + 1)\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1][1] = 1\n    dp[1][2] = 0\n    dp[2][1] = 0\n    dp[2][2] = 1\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i in 3 ... n {\n        dp[i][1] = dp[i - 1][2]\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n    }\n    return dp[n][1] + dp[n][2]\n}\n
        climbing_stairs_constraint_dp.js
        /* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsConstraintDP(n) {\n    if (n === 1 || n === 2) {\n        return 1;\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    const dp = Array.from(new Array(n + 1), () => new Array(3));\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (let i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
        climbing_stairs_constraint_dp.ts
        /* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsConstraintDP(n: number): number {\n    if (n === 1 || n === 2) {\n        return 1;\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    const dp = Array.from({ length: n + 1 }, () => new Array(3));\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (let i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
        climbing_stairs_constraint_dp.dart
        /* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\n  if (n == 1 || n == 2) {\n    return 1;\n  }\n  // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(3, 0));\n  // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n  dp[1][1] = 1;\n  dp[1][2] = 0;\n  dp[2][1] = 0;\n  dp[2][2] = 1;\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n  for (int i = 3; i <= n; i++) {\n    dp[i][1] = dp[i - 1][2];\n    dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n  }\n  return dp[n][1] + dp[n][2];\n}\n
        climbing_stairs_constraint_dp.rs
        /* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfn climbing_stairs_constraint_dp(n: usize) -> i32 {\n    if n == 1 || n == 2 {\n        return 1;\n    };\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    let mut dp = vec![vec![-1; 3]; n + 1];\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i in 3..=n {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    dp[n][1] + dp[n][2]\n}\n
        climbing_stairs_constraint_dp.c
        /* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(3, sizeof(int));\n    }\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    int res = dp[n][1] + dp[n][2];\n    // \u91ca\u653e\u5185\u5b58\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    free(dp);\n    return res;\n}\n
        climbing_stairs_constraint_dp.kt
        /* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfun climbingStairsConstraintDP(n: Int): Int {\n    if (n == 1 || n == 2) {\n        return 1\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    val dp = Array(n + 1) { IntArray(3) }\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1][1] = 1\n    dp[1][2] = 0\n    dp[2][1] = 0\n    dp[2][2] = 1\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (i in 3..n) {\n        dp[i][1] = dp[i - 1][2]\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n    }\n    return dp[n][1] + dp[n][2]\n}\n
        climbing_stairs_constraint_dp.rb
        ### \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 ###\ndef climbing_stairs_constraint_dp(n)\n  return 1 if n == 1 || n == 2\n\n  # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n  dp = Array.new(n + 1) { Array.new(3, 0) }\n  # \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n  dp[1][1], dp[1][2] = 1, 0\n  dp[2][1], dp[2][2] = 0, 1\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n  for i in 3...(n + 1)\n    dp[i][1] = dp[i - 1][2]\n    dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n  end\n\n  dp[n][1] + dp[n][2]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5728\u4e0a\u9762\u7684\u6848\u4f8b\u4e2d\uff0c\u7531\u4e8e\u4ec5\u9700\u591a\u8003\u8651\u524d\u9762\u4e00\u4e2a\u72b6\u6001\uff0c\u56e0\u6b64\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u901a\u8fc7\u6269\u5c55\u72b6\u6001\u5b9a\u4e49\uff0c\u4f7f\u5f97\u95ee\u9898\u91cd\u65b0\u6ee1\u8db3\u65e0\u540e\u6548\u6027\u3002\u7136\u800c\uff0c\u67d0\u4e9b\u95ee\u9898\u5177\u6709\u975e\u5e38\u4e25\u91cd\u7684\u201c\u6709\u540e\u6548\u6027\u201d\u3002

        \u722c\u697c\u68af\u4e0e\u969c\u788d\u751f\u6210

        \u7ed9\u5b9a\u4e00\u4e2a\u5171\u6709 \\(n\\) \u9636\u7684\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\u3002\u89c4\u5b9a\u5f53\u722c\u5230\u7b2c \\(i\\) \u9636\u65f6\uff0c\u7cfb\u7edf\u81ea\u52a8\u4f1a\u5728\u7b2c \\(2i\\) \u9636\u4e0a\u653e\u4e0a\u969c\u788d\u7269\uff0c\u4e4b\u540e\u6240\u6709\u8f6e\u90fd\u4e0d\u5141\u8bb8\u8df3\u5230\u7b2c \\(2i\\) \u9636\u4e0a\u3002\u4f8b\u5982\uff0c\u524d\u4e24\u8f6e\u5206\u522b\u8df3\u5230\u4e86\u7b2c \\(2\\)\u3001\\(3\\) \u9636\u4e0a\uff0c\u5219\u4e4b\u540e\u5c31\u4e0d\u80fd\u8df3\u5230\u7b2c \\(4\\)\u3001\\(6\\) \u9636\u4e0a\u3002\u8bf7\u95ee\u6709\u591a\u5c11\u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\uff1f

        \u5728\u8fd9\u4e2a\u95ee\u9898\u4e2d\uff0c\u4e0b\u6b21\u8df3\u8dc3\u4f9d\u8d56\u8fc7\u53bb\u6240\u6709\u7684\u72b6\u6001\uff0c\u56e0\u4e3a\u6bcf\u4e00\u6b21\u8df3\u8dc3\u90fd\u4f1a\u5728\u66f4\u9ad8\u7684\u9636\u68af\u4e0a\u8bbe\u7f6e\u969c\u788d\uff0c\u5e76\u5f71\u54cd\u672a\u6765\u7684\u8df3\u8dc3\u3002\u5bf9\u4e8e\u8fd9\u7c7b\u95ee\u9898\uff0c\u52a8\u6001\u89c4\u5212\u5f80\u5f80\u96be\u4ee5\u89e3\u51b3\u3002

        \u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u590d\u6742\u7684\u7ec4\u5408\u4f18\u5316\u95ee\u9898\uff08\u4f8b\u5982\u65c5\u884c\u5546\u95ee\u9898\uff09\u4e0d\u6ee1\u8db3\u65e0\u540e\u6548\u6027\u3002\u5bf9\u4e8e\u8fd9\u7c7b\u95ee\u9898\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u9009\u62e9\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\uff0c\u4f8b\u5982\u542f\u53d1\u5f0f\u641c\u7d22\u3001\u9057\u4f20\u7b97\u6cd5\u3001\u5f3a\u5316\u5b66\u4e60\u7b49\uff0c\u4ece\u800c\u5728\u6709\u9650\u65f6\u95f4\u5185\u5f97\u5230\u53ef\u7528\u7684\u5c40\u90e8\u6700\u4f18\u89e3\u3002

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/","title":"14.3 \u00a0 \u52a8\u6001\u89c4\u5212\u89e3\u9898\u601d\u8def","text":"

        \u4e0a\u4e24\u8282\u4ecb\u7ecd\u4e86\u52a8\u6001\u89c4\u5212\u95ee\u9898\u7684\u4e3b\u8981\u7279\u5f81\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u4e00\u8d77\u63a2\u7a76\u4e24\u4e2a\u66f4\u52a0\u5b9e\u7528\u7684\u95ee\u9898\u3002

        1. \u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u95ee\u9898\u662f\u4e0d\u662f\u52a8\u6001\u89c4\u5212\u95ee\u9898\uff1f
        2. \u6c42\u89e3\u52a8\u6001\u89c4\u5212\u95ee\u9898\u8be5\u4ece\u4f55\u5904\u5165\u624b\uff0c\u5b8c\u6574\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f
        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1431","title":"14.3.1 \u00a0 \u95ee\u9898\u5224\u65ad","text":"

        \u603b\u7684\u6765\u8bf4\uff0c\u5982\u679c\u4e00\u4e2a\u95ee\u9898\u5305\u542b\u91cd\u53e0\u5b50\u95ee\u9898\u3001\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u5e76\u6ee1\u8db3\u65e0\u540e\u6548\u6027\uff0c\u90a3\u4e48\u5b83\u901a\u5e38\u9002\u5408\u7528\u52a8\u6001\u89c4\u5212\u6c42\u89e3\u3002\u7136\u800c\uff0c\u6211\u4eec\u5f88\u96be\u4ece\u95ee\u9898\u63cf\u8ff0\u4e2d\u76f4\u63a5\u63d0\u53d6\u51fa\u8fd9\u4e9b\u7279\u6027\u3002\u56e0\u6b64\u6211\u4eec\u901a\u5e38\u4f1a\u653e\u5bbd\u6761\u4ef6\uff0c\u5148\u89c2\u5bdf\u95ee\u9898\u662f\u5426\u9002\u5408\u4f7f\u7528\u56de\u6eaf\uff08\u7a77\u4e3e\uff09\u89e3\u51b3\u3002

        \u9002\u5408\u7528\u56de\u6eaf\u89e3\u51b3\u7684\u95ee\u9898\u901a\u5e38\u6ee1\u8db3\u201c\u51b3\u7b56\u6811\u6a21\u578b\u201d\uff0c\u8fd9\u79cd\u95ee\u9898\u53ef\u4ee5\u4f7f\u7528\u6811\u5f62\u7ed3\u6784\u6765\u63cf\u8ff0\uff0c\u5176\u4e2d\u6bcf\u4e00\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u51b3\u7b56\uff0c\u6bcf\u4e00\u6761\u8def\u5f84\u4ee3\u8868\u4e00\u4e2a\u51b3\u7b56\u5e8f\u5217\u3002

        \u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c\u95ee\u9898\u5305\u542b\u660e\u786e\u7684\u51b3\u7b56\u6982\u5ff5\uff0c\u5e76\u4e14\u89e3\u662f\u901a\u8fc7\u4e00\u7cfb\u5217\u51b3\u7b56\u4ea7\u751f\u7684\uff0c\u90a3\u4e48\u5b83\u5c31\u6ee1\u8db3\u51b3\u7b56\u6811\u6a21\u578b\uff0c\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528\u56de\u6eaf\u6765\u89e3\u51b3\u3002

        \u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u52a8\u6001\u89c4\u5212\u95ee\u9898\u8fd8\u6709\u4e00\u4e9b\u5224\u65ad\u7684\u201c\u52a0\u5206\u9879\u201d\u3002

        • \u95ee\u9898\u5305\u542b\u6700\u5927\uff08\u5c0f\uff09\u6216\u6700\u591a\uff08\u5c11\uff09\u7b49\u6700\u4f18\u5316\u63cf\u8ff0\u3002
        • \u95ee\u9898\u7684\u72b6\u6001\u80fd\u591f\u4f7f\u7528\u4e00\u4e2a\u5217\u8868\u3001\u591a\u7ef4\u77e9\u9635\u6216\u6811\u6765\u8868\u793a\uff0c\u5e76\u4e14\u4e00\u4e2a\u72b6\u6001\u4e0e\u5176\u5468\u56f4\u7684\u72b6\u6001\u5b58\u5728\u9012\u63a8\u5173\u7cfb\u3002

        \u76f8\u5e94\u5730\uff0c\u4e5f\u5b58\u5728\u4e00\u4e9b\u201c\u51cf\u5206\u9879\u201d\u3002

        • \u95ee\u9898\u7684\u76ee\u6807\u662f\u627e\u51fa\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u800c\u4e0d\u662f\u627e\u51fa\u6700\u4f18\u89e3\u3002
        • \u95ee\u9898\u63cf\u8ff0\u4e2d\u6709\u660e\u663e\u7684\u6392\u5217\u7ec4\u5408\u7684\u7279\u5f81\uff0c\u9700\u8981\u8fd4\u56de\u5177\u4f53\u7684\u591a\u4e2a\u65b9\u6848\u3002

        \u5982\u679c\u4e00\u4e2a\u95ee\u9898\u6ee1\u8db3\u51b3\u7b56\u6811\u6a21\u578b\uff0c\u5e76\u5177\u6709\u8f83\u4e3a\u660e\u663e\u7684\u201c\u52a0\u5206\u9879\u201d\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5047\u8bbe\u5b83\u662f\u4e00\u4e2a\u52a8\u6001\u89c4\u5212\u95ee\u9898\uff0c\u5e76\u5728\u6c42\u89e3\u8fc7\u7a0b\u4e2d\u9a8c\u8bc1\u5b83\u3002

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1432","title":"14.3.2 \u00a0 \u95ee\u9898\u6c42\u89e3\u6b65\u9aa4","text":"

        \u52a8\u6001\u89c4\u5212\u7684\u89e3\u9898\u6d41\u7a0b\u4f1a\u56e0\u95ee\u9898\u7684\u6027\u8d28\u548c\u96be\u5ea6\u800c\u6709\u6240\u4e0d\u540c\uff0c\u4f46\u901a\u5e38\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u63cf\u8ff0\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u5efa\u7acb \\(dp\\) \u8868\uff0c\u63a8\u5bfc\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff0c\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u7b49\u3002

        \u4e3a\u4e86\u66f4\u5f62\u8c61\u5730\u5c55\u793a\u89e3\u9898\u6b65\u9aa4\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u7ecf\u5178\u95ee\u9898\u201c\u6700\u5c0f\u8def\u5f84\u548c\u201d\u6765\u4e3e\u4f8b\u3002

        Question

        \u7ed9\u5b9a\u4e00\u4e2a \\(n \\times m\\) \u7684\u4e8c\u7ef4\u7f51\u683c grid \uff0c\u7f51\u683c\u4e2d\u7684\u6bcf\u4e2a\u5355\u5143\u683c\u5305\u542b\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\uff0c\u8868\u793a\u8be5\u5355\u5143\u683c\u7684\u4ee3\u4ef7\u3002\u673a\u5668\u4eba\u4ee5\u5de6\u4e0a\u89d2\u5355\u5143\u683c\u4e3a\u8d77\u59cb\u70b9\uff0c\u6bcf\u6b21\u53ea\u80fd\u5411\u4e0b\u6216\u8005\u5411\u53f3\u79fb\u52a8\u4e00\u6b65\uff0c\u76f4\u81f3\u5230\u8fbe\u53f3\u4e0b\u89d2\u5355\u5143\u683c\u3002\u8bf7\u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230\u53f3\u4e0b\u89d2\u7684\u6700\u5c0f\u8def\u5f84\u548c\u3002

        \u56fe 14-10 \u5c55\u793a\u4e86\u4e00\u4e2a\u4f8b\u5b50\uff0c\u7ed9\u5b9a\u7f51\u683c\u7684\u6700\u5c0f\u8def\u5f84\u548c\u4e3a \\(13\\) \u3002

        \u56fe 14-10 \u00a0 \u6700\u5c0f\u8def\u5f84\u548c\u793a\u4f8b\u6570\u636e

        \u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868

        \u672c\u9898\u7684\u6bcf\u4e00\u8f6e\u7684\u51b3\u7b56\u5c31\u662f\u4ece\u5f53\u524d\u683c\u5b50\u5411\u4e0b\u6216\u5411\u53f3\u8d70\u4e00\u6b65\u3002\u8bbe\u5f53\u524d\u683c\u5b50\u7684\u884c\u5217\u7d22\u5f15\u4e3a \\([i, j]\\) \uff0c\u5219\u5411\u4e0b\u6216\u5411\u53f3\u8d70\u4e00\u6b65\u540e\uff0c\u7d22\u5f15\u53d8\u4e3a \\([i+1, j]\\) \u6216 \\([i, j+1]\\) \u3002\u56e0\u6b64\uff0c\u72b6\u6001\u5e94\u5305\u542b\u884c\u7d22\u5f15\u548c\u5217\u7d22\u5f15\u4e24\u4e2a\u53d8\u91cf\uff0c\u8bb0\u4e3a \\([i, j]\\) \u3002

        \u72b6\u6001 \\([i, j]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u4e3a\uff1a\u4ece\u8d77\u59cb\u70b9 \\([0, 0]\\) \u8d70\u5230 \\([i, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\uff0c\u89e3\u8bb0\u4e3a \\(dp[i, j]\\) \u3002

        \u81f3\u6b64\uff0c\u6211\u4eec\u5c31\u5f97\u5230\u4e86\u56fe 14-11 \u6240\u793a\u7684\u4e8c\u7ef4 \\(dp\\) \u77e9\u9635\uff0c\u5176\u5c3a\u5bf8\u4e0e\u8f93\u5165\u7f51\u683c \\(grid\\) \u76f8\u540c\u3002

        \u56fe 14-11 \u00a0 \u72b6\u6001\u5b9a\u4e49\u4e0e dp \u8868

        Note

        \u52a8\u6001\u89c4\u5212\u548c\u56de\u6eaf\u8fc7\u7a0b\u53ef\u4ee5\u63cf\u8ff0\u4e3a\u4e00\u4e2a\u51b3\u7b56\u5e8f\u5217\uff0c\u800c\u72b6\u6001\u7531\u6240\u6709\u51b3\u7b56\u53d8\u91cf\u6784\u6210\u3002\u5b83\u5e94\u5f53\u5305\u542b\u63cf\u8ff0\u89e3\u9898\u8fdb\u5ea6\u7684\u6240\u6709\u53d8\u91cf\uff0c\u5176\u5305\u542b\u4e86\u8db3\u591f\u7684\u4fe1\u606f\uff0c\u80fd\u591f\u7528\u6765\u63a8\u5bfc\u51fa\u4e0b\u4e00\u4e2a\u72b6\u6001\u3002

        \u6bcf\u4e2a\u72b6\u6001\u90fd\u5bf9\u5e94\u4e00\u4e2a\u5b50\u95ee\u9898\uff0c\u6211\u4eec\u4f1a\u5b9a\u4e49\u4e00\u4e2a \\(dp\\) \u8868\u6765\u5b58\u50a8\u6240\u6709\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u72b6\u6001\u7684\u6bcf\u4e2a\u72ec\u7acb\u53d8\u91cf\u90fd\u662f \\(dp\\) \u8868\u7684\u4e00\u4e2a\u7ef4\u5ea6\u3002\u4ece\u672c\u8d28\u4e0a\u770b\uff0c\\(dp\\) \u8868\u662f\u72b6\u6001\u548c\u5b50\u95ee\u9898\u7684\u89e3\u4e4b\u95f4\u7684\u6620\u5c04\u3002

        \u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b

        \u5bf9\u4e8e\u72b6\u6001 \\([i, j]\\) \uff0c\u5b83\u53ea\u80fd\u4ece\u4e0a\u8fb9\u683c\u5b50 \\([i-1, j]\\) \u548c\u5de6\u8fb9\u683c\u5b50 \\([i, j-1]\\) \u8f6c\u79fb\u800c\u6765\u3002\u56e0\u6b64\u6700\u4f18\u5b50\u7ed3\u6784\u4e3a\uff1a\u5230\u8fbe \\([i, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\u7531 \\([i, j-1]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\u4e0e \\([i-1, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\u4e2d\u8f83\u5c0f\u7684\u90a3\u4e00\u4e2a\u51b3\u5b9a\u3002

        \u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u53ef\u63a8\u51fa\u56fe 14-12 \u6240\u793a\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff1a

        \\[ dp[i, j] = \\min(dp[i-1, j], dp[i, j-1]) + grid[i, j] \\]

        \u56fe 14-12 \u00a0 \u6700\u4f18\u5b50\u7ed3\u6784\u4e0e\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b

        Note

        \u6839\u636e\u5b9a\u4e49\u597d\u7684 \\(dp\\) \u8868\uff0c\u601d\u8003\u539f\u95ee\u9898\u548c\u5b50\u95ee\u9898\u7684\u5173\u7cfb\uff0c\u627e\u51fa\u901a\u8fc7\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u6765\u6784\u9020\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u7684\u65b9\u6cd5\uff0c\u5373\u6700\u4f18\u5b50\u7ed3\u6784\u3002

        \u4e00\u65e6\u6211\u4eec\u627e\u5230\u4e86\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u6784\u5efa\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u3002

        \u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f

        \u5728\u672c\u9898\u4e2d\uff0c\u5904\u5728\u9996\u884c\u7684\u72b6\u6001\u53ea\u80fd\u4ece\u5176\u5de6\u8fb9\u7684\u72b6\u6001\u5f97\u6765\uff0c\u5904\u5728\u9996\u5217\u7684\u72b6\u6001\u53ea\u80fd\u4ece\u5176\u4e0a\u8fb9\u7684\u72b6\u6001\u5f97\u6765\uff0c\u56e0\u6b64\u9996\u884c \\(i = 0\\) \u548c\u9996\u5217 \\(j = 0\\) \u662f\u8fb9\u754c\u6761\u4ef6\u3002

        \u5982\u56fe 14-13 \u6240\u793a\uff0c\u7531\u4e8e\u6bcf\u4e2a\u683c\u5b50\u662f\u7531\u5176\u5de6\u65b9\u683c\u5b50\u548c\u4e0a\u65b9\u683c\u5b50\u8f6c\u79fb\u800c\u6765\uff0c\u56e0\u6b64\u6211\u4eec\u4f7f\u7528\u5faa\u73af\u6765\u904d\u5386\u77e9\u9635\uff0c\u5916\u5faa\u73af\u904d\u5386\u5404\u884c\uff0c\u5185\u5faa\u73af\u904d\u5386\u5404\u5217\u3002

        \u56fe 14-13 \u00a0 \u8fb9\u754c\u6761\u4ef6\u4e0e\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f

        Note

        \u8fb9\u754c\u6761\u4ef6\u5728\u52a8\u6001\u89c4\u5212\u4e2d\u7528\u4e8e\u521d\u59cb\u5316 \\(dp\\) \u8868\uff0c\u5728\u641c\u7d22\u4e2d\u7528\u4e8e\u526a\u679d\u3002

        \u72b6\u6001\u8f6c\u79fb\u987a\u5e8f\u7684\u6838\u5fc3\u662f\u8981\u4fdd\u8bc1\u5728\u8ba1\u7b97\u5f53\u524d\u95ee\u9898\u7684\u89e3\u65f6\uff0c\u6240\u6709\u5b83\u4f9d\u8d56\u7684\u66f4\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\u90fd\u5df2\u7ecf\u88ab\u6b63\u786e\u5730\u8ba1\u7b97\u51fa\u6765\u3002

        \u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u6211\u4eec\u5df2\u7ecf\u53ef\u4ee5\u76f4\u63a5\u5199\u51fa\u52a8\u6001\u89c4\u5212\u4ee3\u7801\u3002\u7136\u800c\u5b50\u95ee\u9898\u5206\u89e3\u662f\u4e00\u79cd\u4ece\u9876\u81f3\u5e95\u7684\u601d\u60f3\uff0c\u56e0\u6b64\u6309\u7167\u201c\u66b4\u529b\u641c\u7d22 \\(\\rightarrow\\) \u8bb0\u5fc6\u5316\u641c\u7d22 \\(\\rightarrow\\) \u52a8\u6001\u89c4\u5212\u201d\u7684\u987a\u5e8f\u5b9e\u73b0\u66f4\u52a0\u7b26\u5408\u601d\u7ef4\u4e60\u60ef\u3002

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1","title":"1. \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u7d22","text":"

        \u4ece\u72b6\u6001 \\([i, j]\\) \u5f00\u59cb\u641c\u7d22\uff0c\u4e0d\u65ad\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u72b6\u6001 \\([i-1, j]\\) \u548c \\([i, j-1]\\) \uff0c\u9012\u5f52\u51fd\u6570\u5305\u62ec\u4ee5\u4e0b\u8981\u7d20\u3002

        • \u9012\u5f52\u53c2\u6570\uff1a\u72b6\u6001 \\([i, j]\\) \u3002
        • \u8fd4\u56de\u503c\uff1a\u4ece \\([0, 0]\\) \u5230 \\([i, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c \\(dp[i, j]\\) \u3002
        • \u7ec8\u6b62\u6761\u4ef6\uff1a\u5f53 \\(i = 0\\) \u4e14 \\(j = 0\\) \u65f6\uff0c\u8fd4\u56de\u4ee3\u4ef7 \\(grid[0, 0]\\) \u3002
        • \u526a\u679d\uff1a\u5f53 \\(i < 0\\) \u65f6\u6216 \\(j < 0\\) \u65f6\u7d22\u5f15\u8d8a\u754c\uff0c\u6b64\u65f6\u8fd4\u56de\u4ee3\u4ef7 \\(+\\infty\\) \uff0c\u4ee3\u8868\u4e0d\u53ef\u884c\u3002

        \u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
        def min_path_sum_dfs(grid: list[list[int]], i: int, j: int) -> int:\n    \"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22\"\"\"\n    # \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if i == 0 and j == 0:\n        return grid[0][0]\n    # \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if i < 0 or j < 0:\n        return inf\n    # \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    up = min_path_sum_dfs(grid, i - 1, j)\n    left = min_path_sum_dfs(grid, i, j - 1)\n    # \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    return min(left, up) + grid[i][j]\n
        min_path_sum.cpp
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(vector<vector<int>> &grid, int i, int j) {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return INT_MAX;\n    }\n    // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    int up = minPathSumDFS(grid, i - 1, j);\n    int left = minPathSumDFS(grid, i, j - 1);\n    // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    return min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\n}\n
        min_path_sum.java
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(int[][] grid, int i, int j) {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return Integer.MAX_VALUE;\n    }\n    // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    int up = minPathSumDFS(grid, i - 1, j);\n    int left = minPathSumDFS(grid, i, j - 1);\n    // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    return Math.min(left, up) + grid[i][j];\n}\n
        min_path_sum.cs
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint MinPathSumDFS(int[][] grid, int i, int j) {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return int.MaxValue;\n    }\n    // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    int up = MinPathSumDFS(grid, i - 1, j);\n    int left = MinPathSumDFS(grid, i, j - 1);\n    // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    return Math.Min(left, up) + grid[i][j];\n}\n
        min_path_sum.go
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc minPathSumDFS(grid [][]int, i, j int) int {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if i == 0 && j == 0 {\n        return grid[0][0]\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if i < 0 || j < 0 {\n        return math.MaxInt\n    }\n    // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    up := minPathSumDFS(grid, i-1, j)\n    left := minPathSumDFS(grid, i, j-1)\n    // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    return int(math.Min(float64(left), float64(up))) + grid[i][j]\n}\n
        min_path_sum.swift
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc minPathSumDFS(grid: [[Int]], i: Int, j: Int) -> Int {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if i == 0, j == 0 {\n        return grid[0][0]\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if i < 0 || j < 0 {\n        return .max\n    }\n    // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    let up = minPathSumDFS(grid: grid, i: i - 1, j: j)\n    let left = minPathSumDFS(grid: grid, i: i, j: j - 1)\n    // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    return min(left, up) + grid[i][j]\n}\n
        min_path_sum.js
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction minPathSumDFS(grid, i, j) {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i === 0 && j === 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return Infinity;\n    }\n    // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    const up = minPathSumDFS(grid, i - 1, j);\n    const left = minPathSumDFS(grid, i, j - 1);\n    // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    return Math.min(left, up) + grid[i][j];\n}\n
        min_path_sum.ts
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction minPathSumDFS(\n    grid: Array<Array<number>>,\n    i: number,\n    j: number\n): number {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i === 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return Infinity;\n    }\n    // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    const up = minPathSumDFS(grid, i - 1, j);\n    const left = minPathSumDFS(grid, i, j - 1);\n    // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    return Math.min(left, up) + grid[i][j];\n}\n
        min_path_sum.dart
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(List<List<int>> grid, int i, int j) {\n  // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n  if (i == 0 && j == 0) {\n    return grid[0][0];\n  }\n  // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n  if (i < 0 || j < 0) {\n    // \u5728 Dart \u4e2d\uff0cint \u7c7b\u578b\u662f\u56fa\u5b9a\u8303\u56f4\u7684\u6574\u6570\uff0c\u4e0d\u5b58\u5728\u8868\u793a\u201c\u65e0\u7a77\u5927\u201d\u7684\u503c\n    return BigInt.from(2).pow(31).toInt();\n  }\n  // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n  int up = minPathSumDFS(grid, i - 1, j);\n  int left = minPathSumDFS(grid, i, j - 1);\n  // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n  return min(left, up) + grid[i][j];\n}\n
        min_path_sum.rs
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfn min_path_sum_dfs(grid: &Vec<Vec<i32>>, i: i32, j: i32) -> i32 {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if i == 0 && j == 0 {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if i < 0 || j < 0 {\n        return i32::MAX;\n    }\n    // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    let up = min_path_sum_dfs(grid, i - 1, j);\n    let left = min_path_sum_dfs(grid, i, j - 1);\n    // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    std::cmp::min(left, up) + grid[i as usize][j as usize]\n}\n
        min_path_sum.c
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(int grid[MAX_SIZE][MAX_SIZE], int i, int j) {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return INT_MAX;\n    }\n    // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    int up = minPathSumDFS(grid, i - 1, j);\n    int left = minPathSumDFS(grid, i, j - 1);\n    // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    return myMin(left, up) != INT_MAX ? myMin(left, up) + grid[i][j] : INT_MAX;\n}\n
        min_path_sum.kt
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfun minPathSumDFS(grid: Array<IntArray>, i: Int, j: Int): Int {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i == 0 && j == 0) {\n        return grid[0][0]\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return Int.MAX_VALUE\n    }\n    // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    val up = minPathSumDFS(grid, i - 1, j)\n    val left = minPathSumDFS(grid, i, j - 1)\n    // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    return min(left, up) + grid[i][j]\n}\n
        min_path_sum.rb
        ### \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 ###\ndef min_path_sum_dfs(grid, i, j)\n  # \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n  return grid[i][j] if i == 0 && j == 0\n  # \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n  return Float::INFINITY if i < 0 || j < 0\n  # \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n  up = min_path_sum_dfs(grid, i - 1, j)\n  left = min_path_sum_dfs(grid, i, j - 1)\n  # \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n  [left, up].min + grid[i][j]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 14-14 \u7ed9\u51fa\u4e86\u4ee5 \\(dp[2, 1]\\) \u4e3a\u6839\u8282\u70b9\u7684\u9012\u5f52\u6811\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e9b\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u5176\u6570\u91cf\u4f1a\u968f\u7740\u7f51\u683c grid \u7684\u5c3a\u5bf8\u53d8\u5927\u800c\u6025\u5267\u589e\u591a\u3002

        \u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u9020\u6210\u91cd\u53e0\u5b50\u95ee\u9898\u7684\u539f\u56e0\u4e3a\uff1a\u5b58\u5728\u591a\u6761\u8def\u5f84\u53ef\u4ee5\u4ece\u5de6\u4e0a\u89d2\u5230\u8fbe\u67d0\u4e00\u5355\u5143\u683c\u3002

        \u56fe 14-14 \u00a0 \u66b4\u529b\u641c\u7d22\u9012\u5f52\u6811

        \u6bcf\u4e2a\u72b6\u6001\u90fd\u6709\u5411\u4e0b\u548c\u5411\u53f3\u4e24\u79cd\u9009\u62e9\uff0c\u4ece\u5de6\u4e0a\u89d2\u8d70\u5230\u53f3\u4e0b\u89d2\u603b\u5171\u9700\u8981 \\(m + n - 2\\) \u6b65\uff0c\u6240\u4ee5\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^{m + n})\\) \uff0c\u5176\u4e2d \\(n\\) \u548c \\(m\\) \u5206\u522b\u4e3a\u7f51\u683c\u7684\u884c\u6570\u548c\u5217\u6570\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u79cd\u8ba1\u7b97\u65b9\u5f0f\u672a\u8003\u8651\u4e34\u8fd1\u7f51\u683c\u8fb9\u754c\u7684\u60c5\u51b5\uff0c\u5f53\u5230\u8fbe\u7f51\u683c\u8fb9\u754c\u65f6\u53ea\u5269\u4e0b\u4e00\u79cd\u9009\u62e9\uff0c\u56e0\u6b64\u5b9e\u9645\u7684\u8def\u5f84\u6570\u91cf\u4f1a\u5c11\u4e00\u4e9b\u3002

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#2","title":"2. \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22","text":"

        \u6211\u4eec\u5f15\u5165\u4e00\u4e2a\u548c\u7f51\u683c grid \u76f8\u540c\u5c3a\u5bf8\u7684\u8bb0\u5fc6\u5217\u8868 mem \uff0c\u7528\u4e8e\u8bb0\u5f55\u5404\u4e2a\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5e76\u5c06\u91cd\u53e0\u5b50\u95ee\u9898\u8fdb\u884c\u526a\u679d\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
        def min_path_sum_dfs_mem(\n    grid: list[list[int]], mem: list[list[int]], i: int, j: int\n) -> int:\n    \"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n    # \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if i == 0 and j == 0:\n        return grid[0][0]\n    # \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if i < 0 or j < 0:\n        return inf\n    # \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if mem[i][j] != -1:\n        return mem[i][j]\n    # \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    up = min_path_sum_dfs_mem(grid, mem, i - 1, j)\n    left = min_path_sum_dfs_mem(grid, mem, i, j - 1)\n    # \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    mem[i][j] = min(left, up) + grid[i][j]\n    return mem[i][j]\n
        min_path_sum.cpp
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(vector<vector<int>> &grid, vector<vector<int>> &mem, int i, int j) {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return INT_MAX;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    int up = minPathSumDFSMem(grid, mem, i - 1, j);\n    int left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    mem[i][j] = min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\n    return mem[i][j];\n}\n
        min_path_sum.java
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return Integer.MAX_VALUE;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    int up = minPathSumDFSMem(grid, mem, i - 1, j);\n    int left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    mem[i][j] = Math.min(left, up) + grid[i][j];\n    return mem[i][j];\n}\n
        min_path_sum.cs
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint MinPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return int.MaxValue;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    int up = MinPathSumDFSMem(grid, mem, i - 1, j);\n    int left = MinPathSumDFSMem(grid, mem, i, j - 1);\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    mem[i][j] = Math.Min(left, up) + grid[i][j];\n    return mem[i][j];\n}\n
        min_path_sum.go
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc minPathSumDFSMem(grid, mem [][]int, i, j int) int {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if i == 0 && j == 0 {\n        return grid[0][0]\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if i < 0 || j < 0 {\n        return math.MaxInt\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if mem[i][j] != -1 {\n        return mem[i][j]\n    }\n    // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    up := minPathSumDFSMem(grid, mem, i-1, j)\n    left := minPathSumDFSMem(grid, mem, i, j-1)\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    mem[i][j] = int(math.Min(float64(left), float64(up))) + grid[i][j]\n    return mem[i][j]\n}\n
        min_path_sum.swift
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc minPathSumDFSMem(grid: [[Int]], mem: inout [[Int]], i: Int, j: Int) -> Int {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if i == 0, j == 0 {\n        return grid[0][0]\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if i < 0 || j < 0 {\n        return .max\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if mem[i][j] != -1 {\n        return mem[i][j]\n    }\n    // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    let up = minPathSumDFSMem(grid: grid, mem: &mem, i: i - 1, j: j)\n    let left = minPathSumDFSMem(grid: grid, mem: &mem, i: i, j: j - 1)\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    mem[i][j] = min(left, up) + grid[i][j]\n    return mem[i][j]\n}\n
        min_path_sum.js
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction minPathSumDFSMem(grid, mem, i, j) {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i === 0 && j === 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return Infinity;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][j] !== -1) {\n        return mem[i][j];\n    }\n    // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    const up = minPathSumDFSMem(grid, mem, i - 1, j);\n    const left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    mem[i][j] = Math.min(left, up) + grid[i][j];\n    return mem[i][j];\n}\n
        min_path_sum.ts
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction minPathSumDFSMem(\n    grid: Array<Array<number>>,\n    mem: Array<Array<number>>,\n    i: number,\n    j: number\n): number {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i === 0 && j === 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return Infinity;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    const up = minPathSumDFSMem(grid, mem, i - 1, j);\n    const left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    mem[i][j] = Math.min(left, up) + grid[i][j];\n    return mem[i][j];\n}\n
        min_path_sum.dart
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(List<List<int>> grid, List<List<int>> mem, int i, int j) {\n  // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n  if (i == 0 && j == 0) {\n    return grid[0][0];\n  }\n  // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n  if (i < 0 || j < 0) {\n    // \u5728 Dart \u4e2d\uff0cint \u7c7b\u578b\u662f\u56fa\u5b9a\u8303\u56f4\u7684\u6574\u6570\uff0c\u4e0d\u5b58\u5728\u8868\u793a\u201c\u65e0\u7a77\u5927\u201d\u7684\u503c\n    return BigInt.from(2).pow(31).toInt();\n  }\n  // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n  if (mem[i][j] != -1) {\n    return mem[i][j];\n  }\n  // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n  int up = minPathSumDFSMem(grid, mem, i - 1, j);\n  int left = minPathSumDFSMem(grid, mem, i, j - 1);\n  // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n  mem[i][j] = min(left, up) + grid[i][j];\n  return mem[i][j];\n}\n
        min_path_sum.rs
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn min_path_sum_dfs_mem(grid: &Vec<Vec<i32>>, mem: &mut Vec<Vec<i32>>, i: i32, j: i32) -> i32 {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if i == 0 && j == 0 {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if i < 0 || j < 0 {\n        return i32::MAX;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if mem[i as usize][j as usize] != -1 {\n        return mem[i as usize][j as usize];\n    }\n    // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    let up = min_path_sum_dfs_mem(grid, mem, i - 1, j);\n    let left = min_path_sum_dfs_mem(grid, mem, i, j - 1);\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    mem[i as usize][j as usize] = std::cmp::min(left, up) + grid[i as usize][j as usize];\n    mem[i as usize][j as usize]\n}\n
        min_path_sum.c
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(int grid[MAX_SIZE][MAX_SIZE], int mem[MAX_SIZE][MAX_SIZE], int i, int j) {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return INT_MAX;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    int up = minPathSumDFSMem(grid, mem, i - 1, j);\n    int left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    mem[i][j] = myMin(left, up) != INT_MAX ? myMin(left, up) + grid[i][j] : INT_MAX;\n    return mem[i][j];\n}\n
        min_path_sum.kt
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfun minPathSumDFSMem(\n    grid: Array<IntArray>,\n    mem: Array<IntArray>,\n    i: Int,\n    j: Int\n): Int {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i == 0 && j == 0) {\n        return grid[0][0]\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return Int.MAX_VALUE\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][j] != -1) {\n        return mem[i][j]\n    }\n    // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    val up = minPathSumDFSMem(grid, mem, i - 1, j)\n    val left = minPathSumDFSMem(grid, mem, i, j - 1)\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    mem[i][j] = min(left, up) + grid[i][j]\n    return mem[i][j]\n}\n
        min_path_sum.rb
        ### \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 ###\ndef min_path_sum_dfs_mem(grid, mem, i, j)\n  # \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n  return grid[0][0] if i == 0 && j == 0\n  # \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n  return Float::INFINITY if i < 0 || j < 0\n  # \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n  return mem[i][j] if mem[i][j] != -1\n  # \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n  up = min_path_sum_dfs_mem(grid, mem, i - 1, j)\n  left = min_path_sum_dfs_mem(grid, mem, i, j - 1)\n  # \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n  mem[i][j] = [left, up].min + grid[i][j]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5982\u56fe 14-15 \u6240\u793a\uff0c\u5728\u5f15\u5165\u8bb0\u5fc6\u5316\u540e\uff0c\u6240\u6709\u5b50\u95ee\u9898\u7684\u89e3\u53ea\u9700\u8ba1\u7b97\u4e00\u6b21\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u72b6\u6001\u603b\u6570\uff0c\u5373\u7f51\u683c\u5c3a\u5bf8 \\(O(nm)\\) \u3002

        \u56fe 14-15 \u00a0 \u8bb0\u5fc6\u5316\u641c\u7d22\u9012\u5f52\u6811

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#3","title":"3. \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52a8\u6001\u89c4\u5212","text":"

        \u57fa\u4e8e\u8fed\u4ee3\u5b9e\u73b0\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
        def min_path_sum_dp(grid: list[list[int]]) -> int:\n    \"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n    n, m = len(grid), len(grid[0])\n    # \u521d\u59cb\u5316 dp \u8868\n    dp = [[0] * m for _ in range(n)]\n    dp[0][0] = grid[0][0]\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for j in range(1, m):\n        dp[0][j] = dp[0][j - 1] + grid[0][j]\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    for i in range(1, n):\n        dp[i][0] = dp[i - 1][0] + grid[i][0]\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i in range(1, n):\n        for j in range(1, m):\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n    return dp[n - 1][m - 1]\n
        min_path_sum.cpp
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(vector<vector<int>> &grid) {\n    int n = grid.size(), m = grid[0].size();\n    // \u521d\u59cb\u5316 dp \u8868\n    vector<vector<int>> dp(n, vector<int>(m));\n    dp[0][0] = grid[0][0];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (int j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    for (int i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
        min_path_sum.java
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(int[][] grid) {\n    int n = grid.length, m = grid[0].length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[][] dp = new int[n][m];\n    dp[0][0] = grid[0][0];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (int j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    for (int i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
        min_path_sum.cs
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint MinPathSumDP(int[][] grid) {\n    int n = grid.Length, m = grid[0].Length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[,] dp = new int[n, m];\n    dp[0, 0] = grid[0][0];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (int j = 1; j < m; j++) {\n        dp[0, j] = dp[0, j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    for (int i = 1; i < n; i++) {\n        dp[i, 0] = dp[i - 1, 0] + grid[i][0];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i, j] = Math.Min(dp[i, j - 1], dp[i - 1, j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1, m - 1];\n}\n
        min_path_sum.go
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDP(grid [][]int) int {\n    n, m := len(grid), len(grid[0])\n    // \u521d\u59cb\u5316 dp \u8868\n    dp := make([][]int, n)\n    for i := 0; i < n; i++ {\n        dp[i] = make([]int, m)\n    }\n    dp[0][0] = grid[0][0]\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for j := 1; j < m; j++ {\n        dp[0][j] = dp[0][j-1] + grid[0][j]\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    for i := 1; i < n; i++ {\n        dp[i][0] = dp[i-1][0] + grid[i][0]\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i := 1; i < n; i++ {\n        for j := 1; j < m; j++ {\n            dp[i][j] = int(math.Min(float64(dp[i][j-1]), float64(dp[i-1][j]))) + grid[i][j]\n        }\n    }\n    return dp[n-1][m-1]\n}\n
        min_path_sum.swift
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDP(grid: [[Int]]) -> Int {\n    let n = grid.count\n    let m = grid[0].count\n    // \u521d\u59cb\u5316 dp \u8868\n    var dp = Array(repeating: Array(repeating: 0, count: m), count: n)\n    dp[0][0] = grid[0][0]\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for j in 1 ..< m {\n        dp[0][j] = dp[0][j - 1] + grid[0][j]\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    for i in 1 ..< n {\n        dp[i][0] = dp[i - 1][0] + grid[i][0]\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i in 1 ..< n {\n        for j in 1 ..< m {\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n        }\n    }\n    return dp[n - 1][m - 1]\n}\n
        min_path_sum.js
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDP(grid) {\n    const n = grid.length,\n        m = grid[0].length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: n }, () =>\n        Array.from({ length: m }, () => 0)\n    );\n    dp[0][0] = grid[0][0];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (let j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    for (let i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (let i = 1; i < n; i++) {\n        for (let j = 1; j < m; j++) {\n            dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
        min_path_sum.ts
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDP(grid: Array<Array<number>>): number {\n    const n = grid.length,\n        m = grid[0].length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: n }, () =>\n        Array.from({ length: m }, () => 0)\n    );\n    dp[0][0] = grid[0][0];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (let j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    for (let i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (let i = 1; i < n; i++) {\n        for (let j: number = 1; j < m; j++) {\n            dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
        min_path_sum.dart
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(List<List<int>> grid) {\n  int n = grid.length, m = grid[0].length;\n  // \u521d\u59cb\u5316 dp \u8868\n  List<List<int>> dp = List.generate(n, (i) => List.filled(m, 0));\n  dp[0][0] = grid[0][0];\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n  for (int j = 1; j < m; j++) {\n    dp[0][j] = dp[0][j - 1] + grid[0][j];\n  }\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n  for (int i = 1; i < n; i++) {\n    dp[i][0] = dp[i - 1][0] + grid[i][0];\n  }\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n  for (int i = 1; i < n; i++) {\n    for (int j = 1; j < m; j++) {\n      dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n    }\n  }\n  return dp[n - 1][m - 1];\n}\n
        min_path_sum.rs
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfn min_path_sum_dp(grid: &Vec<Vec<i32>>) -> i32 {\n    let (n, m) = (grid.len(), grid[0].len());\n    // \u521d\u59cb\u5316 dp \u8868\n    let mut dp = vec![vec![0; m]; n];\n    dp[0][0] = grid[0][0];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for j in 1..m {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    for i in 1..n {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i in 1..n {\n        for j in 1..m {\n            dp[i][j] = std::cmp::min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    dp[n - 1][m - 1]\n}\n
        min_path_sum.c
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(int grid[MAX_SIZE][MAX_SIZE], int n, int m) {\n    // \u521d\u59cb\u5316 dp \u8868\n    int **dp = malloc(n * sizeof(int *));\n    for (int i = 0; i < n; i++) {\n        dp[i] = calloc(m, sizeof(int));\n    }\n    dp[0][0] = grid[0][0];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (int j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    for (int i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i][j] = myMin(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    int res = dp[n - 1][m - 1];\n    // \u91ca\u653e\u5185\u5b58\n    for (int i = 0; i < n; i++) {\n        free(dp[i]);\n    }\n    return res;\n}\n
        min_path_sum.kt
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfun minPathSumDP(grid: Array<IntArray>): Int {\n    val n = grid.size\n    val m = grid[0].size\n    // \u521d\u59cb\u5316 dp \u8868\n    val dp = Array(n) { IntArray(m) }\n    dp[0][0] = grid[0][0]\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (j in 1..<m) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j]\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    for (i in 1..<n) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0]\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (i in 1..<n) {\n        for (j in 1..<m) {\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n        }\n    }\n    return dp[n - 1][m - 1]\n}\n
        min_path_sum.rb
        ### \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 ###\ndef min_path_sum_dp(grid)\n  n, m = grid.length, grid.first.length\n  # \u521d\u59cb\u5316 dp \u8868\n  dp = Array.new(n) { Array.new(m, 0) }\n  dp[0][0] = grid[0][0]\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n  (1...m).each { |j| dp[0][j] = dp[0][j - 1] + grid[0][j] }\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n  (1...n).each { |i| dp[i][0] = dp[i - 1][0] + grid[i][0] }\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n  for i in 1...n\n    for j in 1...m\n      dp[i][j] = [dp[i][j - 1], dp[i - 1][j]].min + grid[i][j]\n    end\n  end\n  dp[n -1][m -1]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 14-16 \u5c55\u793a\u4e86\u6700\u5c0f\u8def\u5f84\u548c\u7684\u72b6\u6001\u8f6c\u79fb\u8fc7\u7a0b\uff0c\u5176\u904d\u5386\u4e86\u6574\u4e2a\u7f51\u683c\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nm)\\) \u3002

        \u6570\u7ec4 dp \u5927\u5c0f\u4e3a \\(n \\times m\\) \uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nm)\\) \u3002

        <1><2><3><4><5><6><7><8><9><10><11><12>

        \u56fe 14-16 \u00a0 \u6700\u5c0f\u8def\u5f84\u548c\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#4","title":"4. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

        \u7531\u4e8e\u6bcf\u4e2a\u683c\u5b50\u53ea\u4e0e\u5176\u5de6\u8fb9\u548c\u4e0a\u8fb9\u7684\u683c\u5b50\u6709\u5173\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u53ea\u7528\u4e00\u4e2a\u5355\u884c\u6570\u7ec4\u6765\u5b9e\u73b0 \\(dp\\) \u8868\u3002

        \u8bf7\u6ce8\u610f\uff0c\u56e0\u4e3a\u6570\u7ec4 dp \u53ea\u80fd\u8868\u793a\u4e00\u884c\u7684\u72b6\u6001\uff0c\u6240\u4ee5\u6211\u4eec\u65e0\u6cd5\u63d0\u524d\u521d\u59cb\u5316\u9996\u5217\u72b6\u6001\uff0c\u800c\u662f\u5728\u904d\u5386\u6bcf\u884c\u65f6\u66f4\u65b0\u5b83\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
        def min_path_sum_dp_comp(grid: list[list[int]]) -> int:\n    \"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n    n, m = len(grid), len(grid[0])\n    # \u521d\u59cb\u5316 dp \u8868\n    dp = [0] * m\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    dp[0] = grid[0][0]\n    for j in range(1, m):\n        dp[j] = dp[j - 1] + grid[0][j]\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for i in range(1, n):\n        # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        dp[0] = dp[0] + grid[i][0]\n        # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for j in range(1, m):\n            dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n    return dp[m - 1]\n
        min_path_sum.cpp
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(vector<vector<int>> &grid) {\n    int n = grid.size(), m = grid[0].size();\n    // \u521d\u59cb\u5316 dp \u8868\n    vector<int> dp(m);\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    dp[0] = grid[0][0];\n    for (int j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (int i = 1; i < n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        dp[0] = dp[0] + grid[i][0];\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (int j = 1; j < m; j++) {\n            dp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
        min_path_sum.java
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(int[][] grid) {\n    int n = grid.length, m = grid[0].length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[] dp = new int[m];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    dp[0] = grid[0][0];\n    for (int j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (int i = 1; i < n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        dp[0] = dp[0] + grid[i][0];\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (int j = 1; j < m; j++) {\n            dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
        min_path_sum.cs
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint MinPathSumDPComp(int[][] grid) {\n    int n = grid.Length, m = grid[0].Length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[] dp = new int[m];\n    dp[0] = grid[0][0];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (int j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (int i = 1; i < n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        dp[0] = dp[0] + grid[i][0];\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (int j = 1; j < m; j++) {\n            dp[j] = Math.Min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
        min_path_sum.go
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDPComp(grid [][]int) int {\n    n, m := len(grid), len(grid[0])\n    // \u521d\u59cb\u5316 dp \u8868\n    dp := make([]int, m)\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    dp[0] = grid[0][0]\n    for j := 1; j < m; j++ {\n        dp[j] = dp[j-1] + grid[0][j]\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i := 1; i < n; i++ {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        dp[0] = dp[0] + grid[i][0]\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for j := 1; j < m; j++ {\n            dp[j] = int(math.Min(float64(dp[j-1]), float64(dp[j]))) + grid[i][j]\n        }\n    }\n    return dp[m-1]\n}\n
        min_path_sum.swift
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDPComp(grid: [[Int]]) -> Int {\n    let n = grid.count\n    let m = grid[0].count\n    // \u521d\u59cb\u5316 dp \u8868\n    var dp = Array(repeating: 0, count: m)\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    dp[0] = grid[0][0]\n    for j in 1 ..< m {\n        dp[j] = dp[j - 1] + grid[0][j]\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for i in 1 ..< n {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        dp[0] = dp[0] + grid[i][0]\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for j in 1 ..< m {\n            dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n        }\n    }\n    return dp[m - 1]\n}\n
        min_path_sum.js
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDPComp(grid) {\n    const n = grid.length,\n        m = grid[0].length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = new Array(m);\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    dp[0] = grid[0][0];\n    for (let j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (let i = 1; i < n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        dp[0] = dp[0] + grid[i][0];\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (let j = 1; j < m; j++) {\n            dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
        min_path_sum.ts
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDPComp(grid: Array<Array<number>>): number {\n    const n = grid.length,\n        m = grid[0].length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = new Array(m);\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    dp[0] = grid[0][0];\n    for (let j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (let i = 1; i < n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        dp[0] = dp[0] + grid[i][0];\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (let j = 1; j < m; j++) {\n            dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
        min_path_sum.dart
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(List<List<int>> grid) {\n  int n = grid.length, m = grid[0].length;\n  // \u521d\u59cb\u5316 dp \u8868\n  List<int> dp = List.filled(m, 0);\n  dp[0] = grid[0][0];\n  for (int j = 1; j < m; j++) {\n    dp[j] = dp[j - 1] + grid[0][j];\n  }\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n  for (int i = 1; i < n; i++) {\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    dp[0] = dp[0] + grid[i][0];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n    for (int j = 1; j < m; j++) {\n      dp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n    }\n  }\n  return dp[m - 1];\n}\n
        min_path_sum.rs
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn min_path_sum_dp_comp(grid: &Vec<Vec<i32>>) -> i32 {\n    let (n, m) = (grid.len(), grid[0].len());\n    // \u521d\u59cb\u5316 dp \u8868\n    let mut dp = vec![0; m];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    dp[0] = grid[0][0];\n    for j in 1..m {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for i in 1..n {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        dp[0] = dp[0] + grid[i][0];\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for j in 1..m {\n            dp[j] = std::cmp::min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    dp[m - 1]\n}\n
        min_path_sum.c
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(int grid[MAX_SIZE][MAX_SIZE], int n, int m) {\n    // \u521d\u59cb\u5316 dp \u8868\n    int *dp = calloc(m, sizeof(int));\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    dp[0] = grid[0][0];\n    for (int j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (int i = 1; i < n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        dp[0] = dp[0] + grid[i][0];\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (int j = 1; j < m; j++) {\n            dp[j] = myMin(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    int res = dp[m - 1];\n    // \u91ca\u653e\u5185\u5b58\n    free(dp);\n    return res;\n}\n
        min_path_sum.kt
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfun minPathSumDPComp(grid: Array<IntArray>): Int {\n    val n = grid.size\n    val m = grid[0].size\n    // \u521d\u59cb\u5316 dp \u8868\n    val dp = IntArray(m)\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    dp[0] = grid[0][0]\n    for (j in 1..<m) {\n        dp[j] = dp[j - 1] + grid[0][j]\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (i in 1..<n) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        dp[0] = dp[0] + grid[i][0]\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (j in 1..<m) {\n            dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n        }\n    }\n    return dp[m - 1]\n}\n
        min_path_sum.rb
        ### \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 ###\ndef min_path_sum_dp_comp(grid)\n  n, m = grid.length, grid.first.length\n  # \u521d\u59cb\u5316 dp \u8868\n  dp = Array.new(m, 0)\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n  dp[0] = grid[0][0]\n  (1...m).each { |j| dp[j] = dp[j - 1] + grid[0][j] }\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n  for i in 1...n\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    dp[0] = dp[0] + grid[i][0]\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n    (1...m).each { |j| dp[j] = [dp[j - 1], dp[j]].min + grid[i][j] }\n  end\n  dp[m - 1]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_dynamic_programming/edit_distance_problem/","title":"14.6 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898","text":"

        \u7f16\u8f91\u8ddd\u79bb\uff0c\u4e5f\u79f0 Levenshtein \u8ddd\u79bb\uff0c\u6307\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e4b\u95f4\u4e92\u76f8\u8f6c\u6362\u7684\u6700\u5c11\u4fee\u6539\u6b21\u6570\uff0c\u901a\u5e38\u7528\u4e8e\u5728\u4fe1\u606f\u68c0\u7d22\u548c\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4e2d\u5ea6\u91cf\u4e24\u4e2a\u5e8f\u5217\u7684\u76f8\u4f3c\u5ea6\u3002

        Question

        \u8f93\u5165\u4e24\u4e2a\u5b57\u7b26\u4e32 \\(s\\) \u548c \\(t\\) \uff0c\u8fd4\u56de\u5c06 \\(s\\) \u8f6c\u6362\u4e3a \\(t\\) \u6240\u9700\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u3002

        \u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u8fdb\u884c\u4e09\u79cd\u7f16\u8f91\u64cd\u4f5c\uff1a\u63d2\u5165\u4e00\u4e2a\u5b57\u7b26\u3001\u5220\u9664\u4e00\u4e2a\u5b57\u7b26\u3001\u5c06\u5b57\u7b26\u66ff\u6362\u4e3a\u4efb\u610f\u4e00\u4e2a\u5b57\u7b26\u3002

        \u5982\u56fe 14-27 \u6240\u793a\uff0c\u5c06 kitten \u8f6c\u6362\u4e3a sitting \u9700\u8981\u7f16\u8f91 3 \u6b65\uff0c\u5305\u62ec 2 \u6b21\u66ff\u6362\u64cd\u4f5c\u4e0e 1 \u6b21\u6dfb\u52a0\u64cd\u4f5c\uff1b\u5c06 hello \u8f6c\u6362\u4e3a algo \u9700\u8981 3 \u6b65\uff0c\u5305\u62ec 2 \u6b21\u66ff\u6362\u64cd\u4f5c\u548c 1 \u6b21\u5220\u9664\u64cd\u4f5c\u3002

        \u56fe 14-27 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u7684\u793a\u4f8b\u6570\u636e

        \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898\u53ef\u4ee5\u5f88\u81ea\u7136\u5730\u7528\u51b3\u7b56\u6811\u6a21\u578b\u6765\u89e3\u91ca\u3002\u5b57\u7b26\u4e32\u5bf9\u5e94\u6811\u8282\u70b9\uff0c\u4e00\u8f6e\u51b3\u7b56\uff08\u4e00\u6b21\u7f16\u8f91\u64cd\u4f5c\uff09\u5bf9\u5e94\u6811\u7684\u4e00\u6761\u8fb9\u3002

        \u5982\u56fe 14-28 \u6240\u793a\uff0c\u5728\u4e0d\u9650\u5236\u64cd\u4f5c\u7684\u60c5\u51b5\u4e0b\uff0c\u6bcf\u4e2a\u8282\u70b9\u90fd\u53ef\u4ee5\u6d3e\u751f\u51fa\u8bb8\u591a\u6761\u8fb9\uff0c\u6bcf\u6761\u8fb9\u5bf9\u5e94\u4e00\u79cd\u64cd\u4f5c\uff0c\u8fd9\u610f\u5473\u7740\u4ece hello \u8f6c\u6362\u5230 algo \u6709\u8bb8\u591a\u79cd\u53ef\u80fd\u7684\u8def\u5f84\u3002

        \u4ece\u51b3\u7b56\u6811\u7684\u89d2\u5ea6\u770b\uff0c\u672c\u9898\u7684\u76ee\u6807\u662f\u6c42\u89e3\u8282\u70b9 hello \u548c\u8282\u70b9 algo \u4e4b\u95f4\u7684\u6700\u77ed\u8def\u5f84\u3002

        \u56fe 14-28 \u00a0 \u57fa\u4e8e\u51b3\u7b56\u6811\u6a21\u578b\u8868\u793a\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898

        "},{"location":"chapter_dynamic_programming/edit_distance_problem/#1","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"

        \u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868

        \u6bcf\u4e00\u8f6e\u7684\u51b3\u7b56\u662f\u5bf9\u5b57\u7b26\u4e32 \\(s\\) \u8fdb\u884c\u4e00\u6b21\u7f16\u8f91\u64cd\u4f5c\u3002

        \u6211\u4eec\u5e0c\u671b\u5728\u7f16\u8f91\u64cd\u4f5c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u95ee\u9898\u7684\u89c4\u6a21\u9010\u6e10\u7f29\u5c0f\uff0c\u8fd9\u6837\u624d\u80fd\u6784\u5efa\u5b50\u95ee\u9898\u3002\u8bbe\u5b57\u7b26\u4e32 \\(s\\) \u548c \\(t\\) \u7684\u957f\u5ea6\u5206\u522b\u4e3a \\(n\\) \u548c \\(m\\) \uff0c\u6211\u4eec\u5148\u8003\u8651\u4e24\u5b57\u7b26\u4e32\u5c3e\u90e8\u7684\u5b57\u7b26 \\(s[n-1]\\) \u548c \\(t[m-1]\\) \u3002

        • \u82e5 \\(s[n-1]\\) \u548c \\(t[m-1]\\) \u76f8\u540c\uff0c\u6211\u4eec\u53ef\u4ee5\u8df3\u8fc7\u5b83\u4eec\uff0c\u76f4\u63a5\u8003\u8651 \\(s[n-2]\\) \u548c \\(t[m-2]\\) \u3002
        • \u82e5 \\(s[n-1]\\) \u548c \\(t[m-1]\\) \u4e0d\u540c\uff0c\u6211\u4eec\u9700\u8981\u5bf9 \\(s\\) \u8fdb\u884c\u4e00\u6b21\u7f16\u8f91\uff08\u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\uff09\uff0c\u4f7f\u5f97\u4e24\u5b57\u7b26\u4e32\u5c3e\u90e8\u7684\u5b57\u7b26\u76f8\u540c\uff0c\u4ece\u800c\u53ef\u4ee5\u8df3\u8fc7\u5b83\u4eec\uff0c\u8003\u8651\u89c4\u6a21\u66f4\u5c0f\u7684\u95ee\u9898\u3002

        \u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u5728\u5b57\u7b26\u4e32 \\(s\\) \u4e2d\u8fdb\u884c\u7684\u6bcf\u4e00\u8f6e\u51b3\u7b56\uff08\u7f16\u8f91\u64cd\u4f5c\uff09\uff0c\u90fd\u4f1a\u4f7f\u5f97 \\(s\\) \u548c \\(t\\) \u4e2d\u5269\u4f59\u7684\u5f85\u5339\u914d\u5b57\u7b26\u53d1\u751f\u53d8\u5316\u3002\u56e0\u6b64\uff0c\u72b6\u6001\u4e3a\u5f53\u524d\u5728 \\(s\\) \u548c \\(t\\) \u4e2d\u8003\u8651\u7684\u7b2c \\(i\\) \u548c\u7b2c \\(j\\) \u4e2a\u5b57\u7b26\uff0c\u8bb0\u4e3a \\([i, j]\\) \u3002

        \u72b6\u6001 \\([i, j]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\uff1a\u5c06 \\(s\\) \u7684\u524d \\(i\\) \u4e2a\u5b57\u7b26\u66f4\u6539\u4e3a \\(t\\) \u7684\u524d \\(j\\) \u4e2a\u5b57\u7b26\u6240\u9700\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u3002

        \u81f3\u6b64\uff0c\u5f97\u5230\u4e00\u4e2a\u5c3a\u5bf8\u4e3a \\((i+1) \\times (j+1)\\) \u7684\u4e8c\u7ef4 \\(dp\\) \u8868\u3002

        \u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b

        \u8003\u8651\u5b50\u95ee\u9898 \\(dp[i, j]\\) \uff0c\u5176\u5bf9\u5e94\u7684\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5c3e\u90e8\u5b57\u7b26\u4e3a \\(s[i-1]\\) \u548c \\(t[j-1]\\) \uff0c\u53ef\u6839\u636e\u4e0d\u540c\u7f16\u8f91\u64cd\u4f5c\u5206\u4e3a\u56fe 14-29 \u6240\u793a\u7684\u4e09\u79cd\u60c5\u51b5\u3002

        1. \u5728 \\(s[i-1]\\) \u4e4b\u540e\u6dfb\u52a0 \\(t[j-1]\\) \uff0c\u5219\u5269\u4f59\u5b50\u95ee\u9898 \\(dp[i, j-1]\\) \u3002
        2. \u5220\u9664 \\(s[i-1]\\) \uff0c\u5219\u5269\u4f59\u5b50\u95ee\u9898 \\(dp[i-1, j]\\) \u3002
        3. \u5c06 \\(s[i-1]\\) \u66ff\u6362\u4e3a \\(t[j-1]\\) \uff0c\u5219\u5269\u4f59\u5b50\u95ee\u9898 \\(dp[i-1, j-1]\\) \u3002

        \u56fe 14-29 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u7684\u72b6\u6001\u8f6c\u79fb

        \u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u53ef\u5f97\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\\(dp[i, j]\\) \u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u7b49\u4e8e \\(dp[i, j-1]\\)\u3001\\(dp[i-1, j]\\)\u3001\\(dp[i-1, j-1]\\) \u4e09\u8005\u4e2d\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\uff0c\u518d\u52a0\u4e0a\u672c\u6b21\u7684\u7f16\u8f91\u6b65\u6570 \\(1\\) \u3002\u5bf9\u5e94\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a

        \\[ dp[i, j] = \\min(dp[i, j-1], dp[i-1, j], dp[i-1, j-1]) + 1 \\]

        \u8bf7\u6ce8\u610f\uff0c\u5f53 \\(s[i-1]\\) \u548c \\(t[j-1]\\) \u76f8\u540c\u65f6\uff0c\u65e0\u987b\u7f16\u8f91\u5f53\u524d\u5b57\u7b26\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a

        \\[ dp[i, j] = dp[i-1, j-1] \\]

        \u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f

        \u5f53\u4e24\u5b57\u7b26\u4e32\u90fd\u4e3a\u7a7a\u65f6\uff0c\u7f16\u8f91\u6b65\u6570\u4e3a \\(0\\) \uff0c\u5373 \\(dp[0, 0] = 0\\) \u3002\u5f53 \\(s\\) \u4e3a\u7a7a\u4f46 \\(t\\) \u4e0d\u4e3a\u7a7a\u65f6\uff0c\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u7b49\u4e8e \\(t\\) \u7684\u957f\u5ea6\uff0c\u5373\u9996\u884c \\(dp[0, j] = j\\) \u3002\u5f53 \\(s\\) \u4e0d\u4e3a\u7a7a\u4f46 \\(t\\) \u4e3a\u7a7a\u65f6\uff0c\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u7b49\u4e8e \\(s\\) \u7684\u957f\u5ea6\uff0c\u5373\u9996\u5217 \\(dp[i, 0] = i\\) \u3002

        \u89c2\u5bdf\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff0c\u89e3 \\(dp[i, j]\\) \u4f9d\u8d56\u5de6\u65b9\u3001\u4e0a\u65b9\u3001\u5de6\u4e0a\u65b9\u7684\u89e3\uff0c\u56e0\u6b64\u901a\u8fc7\u4e24\u5c42\u5faa\u73af\u6b63\u5e8f\u904d\u5386\u6574\u4e2a \\(dp\\) \u8868\u5373\u53ef\u3002

        "},{"location":"chapter_dynamic_programming/edit_distance_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby edit_distance.py
        def edit_distance_dp(s: str, t: str) -> int:\n    \"\"\"\u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n    n, m = len(s), len(t)\n    dp = [[0] * (m + 1) for _ in range(n + 1)]\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for i in range(1, n + 1):\n        dp[i][0] = i\n    for j in range(1, m + 1):\n        dp[0][j] = j\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i in range(1, n + 1):\n        for j in range(1, m + 1):\n            if s[i - 1] == t[j - 1]:\n                # \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[i][j] = dp[i - 1][j - 1]\n            else:\n                # \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1\n    return dp[n][m]\n
        edit_distance.cpp
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(string s, string t) {\n    int n = s.length(), m = t.length();\n    vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (int i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s[i - 1] == t[j - 1]) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
        edit_distance.java
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(String s, String t) {\n    int n = s.length(), m = t.length();\n    int[][] dp = new int[n + 1][m + 1];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (int i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s.charAt(i - 1) == t.charAt(j - 1)) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
        edit_distance.cs
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint EditDistanceDP(string s, string t) {\n    int n = s.Length, m = t.Length;\n    int[,] dp = new int[n + 1, m + 1];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (int i = 1; i <= n; i++) {\n        dp[i, 0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0, j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s[i - 1] == t[j - 1]) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[i, j] = dp[i - 1, j - 1];\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[i, j] = Math.Min(Math.Min(dp[i, j - 1], dp[i - 1, j]), dp[i - 1, j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n, m];\n}\n
        edit_distance.go
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDP(s string, t string) int {\n    n := len(s)\n    m := len(t)\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, m+1)\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for i := 1; i <= n; i++ {\n        dp[i][0] = i\n    }\n    for j := 1; j <= m; j++ {\n        dp[0][j] = j\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i := 1; i <= n; i++ {\n        for j := 1; j <= m; j++ {\n            if s[i-1] == t[j-1] {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[i][j] = dp[i-1][j-1]\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[i][j] = MinInt(MinInt(dp[i][j-1], dp[i-1][j]), dp[i-1][j-1]) + 1\n            }\n        }\n    }\n    return dp[n][m]\n}\n
        edit_distance.swift
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDP(s: String, t: String) -> Int {\n    let n = s.utf8CString.count\n    let m = t.utf8CString.count\n    var dp = Array(repeating: Array(repeating: 0, count: m + 1), count: n + 1)\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for i in 1 ... n {\n        dp[i][0] = i\n    }\n    for j in 1 ... m {\n        dp[0][j] = j\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i in 1 ... n {\n        for j in 1 ... m {\n            if s.utf8CString[i - 1] == t.utf8CString[j - 1] {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[i][j] = dp[i - 1][j - 1]\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1\n            }\n        }\n    }\n    return dp[n][m]\n}\n
        edit_distance.js
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDP(s, t) {\n    const n = s.length,\n        m = t.length;\n    const dp = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (let i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (let j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (let i = 1; i <= n; i++) {\n        for (let j = 1; j <= m; j++) {\n            if (s.charAt(i - 1) === t.charAt(j - 1)) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[i][j] =\n                    Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
        edit_distance.ts
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDP(s: string, t: string): number {\n    const n = s.length,\n        m = t.length;\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: m + 1 }, () => 0)\n    );\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (let i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (let j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (let i = 1; i <= n; i++) {\n        for (let j = 1; j <= m; j++) {\n            if (s.charAt(i - 1) === t.charAt(j - 1)) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[i][j] =\n                    Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
        edit_distance.dart
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(String s, String t) {\n  int n = s.length, m = t.length;\n  List<List<int>> dp = List.generate(n + 1, (_) => List.filled(m + 1, 0));\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n  for (int i = 1; i <= n; i++) {\n    dp[i][0] = i;\n  }\n  for (int j = 1; j <= m; j++) {\n    dp[0][j] = j;\n  }\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n  for (int i = 1; i <= n; i++) {\n    for (int j = 1; j <= m; j++) {\n      if (s[i - 1] == t[j - 1]) {\n        // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n        dp[i][j] = dp[i - 1][j - 1];\n      } else {\n        // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n        dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n      }\n    }\n  }\n  return dp[n][m];\n}\n
        edit_distance.rs
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfn edit_distance_dp(s: &str, t: &str) -> i32 {\n    let (n, m) = (s.len(), t.len());\n    let mut dp = vec![vec![0; m + 1]; n + 1];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for i in 1..=n {\n        dp[i][0] = i as i32;\n    }\n    for j in 1..m {\n        dp[0][j] = j as i32;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i in 1..=n {\n        for j in 1..=m {\n            if s.chars().nth(i - 1) == t.chars().nth(j - 1) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[i][j] =\n                    std::cmp::min(std::cmp::min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    dp[n][m]\n}\n
        edit_distance.c
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(char *s, char *t, int n, int m) {\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(m + 1, sizeof(int));\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (int i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s[i - 1] == t[j - 1]) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[i][j] = myMin(myMin(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    int res = dp[n][m];\n    // \u91ca\u653e\u5185\u5b58\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    return res;\n}\n
        edit_distance.kt
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfun editDistanceDP(s: String, t: String): Int {\n    val n = s.length\n    val m = t.length\n    val dp = Array(n + 1) { IntArray(m + 1) }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (i in 1..n) {\n        dp[i][0] = i\n    }\n    for (j in 1..m) {\n        dp[0][j] = j\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (i in 1..n) {\n        for (j in 1..m) {\n            if (s[i - 1] == t[j - 1]) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[i][j] = dp[i - 1][j - 1]\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1\n            }\n        }\n    }\n    return dp[n][m]\n}\n
        edit_distance.rb
        ### \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 ###\ndef edit_distance_dp(s, t)\n  n, m = s.length, t.length\n  dp = Array.new(n + 1) { Array.new(m + 1, 0) }\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n  (1...(n + 1)).each { |i| dp[i][0] = i }\n  (1...(m + 1)).each { |j| dp[0][j] = j }\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n  for i in 1...(n + 1)\n    for j in 1...(m +1)\n      if s[i - 1] == t[j - 1]\n        # \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n        dp[i][j] = dp[i - 1][j - 1]\n      else\n        # \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n        dp[i][j] = [dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]].min + 1\n      end\n    end\n  end\n  dp[n][m]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5982\u56fe 14-30 \u6240\u793a\uff0c\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898\u7684\u72b6\u6001\u8f6c\u79fb\u8fc7\u7a0b\u4e0e\u80cc\u5305\u95ee\u9898\u975e\u5e38\u7c7b\u4f3c\uff0c\u90fd\u53ef\u4ee5\u770b\u4f5c\u586b\u5199\u4e00\u4e2a\u4e8c\u7ef4\u7f51\u683c\u7684\u8fc7\u7a0b\u3002

        <1><2><3><4><5><6><7><8><9><10><11><12><13><14><15>

        \u56fe 14-30 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

        "},{"location":"chapter_dynamic_programming/edit_distance_problem/#3","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

        \u7531\u4e8e \\(dp[i,j]\\) \u662f\u7531\u4e0a\u65b9 \\(dp[i-1, j]\\)\u3001\u5de6\u65b9 \\(dp[i, j-1]\\)\u3001\u5de6\u4e0a\u65b9 \\(dp[i-1, j-1]\\) \u8f6c\u79fb\u800c\u6765\u7684\uff0c\u800c\u6b63\u5e8f\u904d\u5386\u4f1a\u4e22\u5931\u5de6\u4e0a\u65b9 \\(dp[i-1, j-1]\\) \uff0c\u5012\u5e8f\u904d\u5386\u65e0\u6cd5\u63d0\u524d\u6784\u5efa \\(dp[i, j-1]\\) \uff0c\u56e0\u6b64\u4e24\u79cd\u904d\u5386\u987a\u5e8f\u90fd\u4e0d\u53ef\u53d6\u3002

        \u4e3a\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u53d8\u91cf leftup \u6765\u6682\u5b58\u5de6\u4e0a\u65b9\u7684\u89e3 \\(dp[i-1, j-1]\\) \uff0c\u4ece\u800c\u53ea\u9700\u8003\u8651\u5de6\u65b9\u548c\u4e0a\u65b9\u7684\u89e3\u3002\u6b64\u65f6\u7684\u60c5\u51b5\u4e0e\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u76f8\u540c\uff0c\u53ef\u4f7f\u7528\u6b63\u5e8f\u904d\u5386\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby edit_distance.py
        def edit_distance_dp_comp(s: str, t: str) -> int:\n    \"\"\"\u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n    n, m = len(s), len(t)\n    dp = [0] * (m + 1)\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for j in range(1, m + 1):\n        dp[j] = j\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for i in range(1, n + 1):\n        # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        leftup = dp[0]  # \u6682\u5b58 dp[i-1, j-1]\n        dp[0] += 1\n        # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for j in range(1, m + 1):\n            temp = dp[j]\n            if s[i - 1] == t[j - 1]:\n                # \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[j] = leftup\n            else:\n                # \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[j] = min(dp[j - 1], dp[j], leftup) + 1\n            leftup = temp  # \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n    return dp[m]\n
        edit_distance.cpp
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(string s, string t) {\n    int n = s.length(), m = t.length();\n    vector<int> dp(m + 1, 0);\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (int j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (int i = 1; i <= n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        int leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n        dp[0] = i;\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (int j = 1; j <= m; j++) {\n            int temp = dp[j];\n            if (s[i - 1] == t[j - 1]) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[j] = leftup;\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n        }\n    }\n    return dp[m];\n}\n
        edit_distance.java
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(String s, String t) {\n    int n = s.length(), m = t.length();\n    int[] dp = new int[m + 1];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (int j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (int i = 1; i <= n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        int leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n        dp[0] = i;\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (int j = 1; j <= m; j++) {\n            int temp = dp[j];\n            if (s.charAt(i - 1) == t.charAt(j - 1)) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[j] = leftup;\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n        }\n    }\n    return dp[m];\n}\n
        edit_distance.cs
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint EditDistanceDPComp(string s, string t) {\n    int n = s.Length, m = t.Length;\n    int[] dp = new int[m + 1];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (int j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (int i = 1; i <= n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        int leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n        dp[0] = i;\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (int j = 1; j <= m; j++) {\n            int temp = dp[j];\n            if (s[i - 1] == t[j - 1]) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[j] = leftup;\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[j] = Math.Min(Math.Min(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n        }\n    }\n    return dp[m];\n}\n
        edit_distance.go
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDPComp(s string, t string) int {\n    n := len(s)\n    m := len(t)\n    dp := make([]int, m+1)\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for j := 1; j <= m; j++ {\n        dp[j] = j\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for i := 1; i <= n; i++ {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        leftUp := dp[0] // \u6682\u5b58 dp[i-1, j-1]\n        dp[0] = i\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for j := 1; j <= m; j++ {\n            temp := dp[j]\n            if s[i-1] == t[j-1] {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[j] = leftUp\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[j] = MinInt(MinInt(dp[j-1], dp[j]), leftUp) + 1\n            }\n            leftUp = temp // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n        }\n    }\n    return dp[m]\n}\n
        edit_distance.swift
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDPComp(s: String, t: String) -> Int {\n    let n = s.utf8CString.count\n    let m = t.utf8CString.count\n    var dp = Array(repeating: 0, count: m + 1)\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for j in 1 ... m {\n        dp[j] = j\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for i in 1 ... n {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        var leftup = dp[0] // \u6682\u5b58 dp[i-1, j-1]\n        dp[0] = i\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for j in 1 ... m {\n            let temp = dp[j]\n            if s.utf8CString[i - 1] == t.utf8CString[j - 1] {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[j] = leftup\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1\n            }\n            leftup = temp // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n        }\n    }\n    return dp[m]\n}\n
        edit_distance.js
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDPComp(s, t) {\n    const n = s.length,\n        m = t.length;\n    const dp = new Array(m + 1).fill(0);\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (let j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (let i = 1; i <= n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        let leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n        dp[0] = i;\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (let j = 1; j <= m; j++) {\n            const temp = dp[j];\n            if (s.charAt(i - 1) === t.charAt(j - 1)) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[j] = leftup;\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[j] = Math.min(dp[j - 1], dp[j], leftup) + 1;\n            }\n            leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n        }\n    }\n    return dp[m];\n}\n
        edit_distance.ts
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDPComp(s: string, t: string): number {\n    const n = s.length,\n        m = t.length;\n    const dp = new Array(m + 1).fill(0);\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (let j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (let i = 1; i <= n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        let leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n        dp[0] = i;\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (let j = 1; j <= m; j++) {\n            const temp = dp[j];\n            if (s.charAt(i - 1) === t.charAt(j - 1)) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[j] = leftup;\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[j] = Math.min(dp[j - 1], dp[j], leftup) + 1;\n            }\n            leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n        }\n    }\n    return dp[m];\n}\n
        edit_distance.dart
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(String s, String t) {\n  int n = s.length, m = t.length;\n  List<int> dp = List.filled(m + 1, 0);\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n  for (int j = 1; j <= m; j++) {\n    dp[j] = j;\n  }\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n  for (int i = 1; i <= n; i++) {\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    int leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n    dp[0] = i;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n    for (int j = 1; j <= m; j++) {\n      int temp = dp[j];\n      if (s[i - 1] == t[j - 1]) {\n        // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n        dp[j] = leftup;\n      } else {\n        // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n        dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n      }\n      leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n    }\n  }\n  return dp[m];\n}\n
        edit_distance.rs
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn edit_distance_dp_comp(s: &str, t: &str) -> i32 {\n    let (n, m) = (s.len(), t.len());\n    let mut dp = vec![0; m + 1];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for j in 1..m {\n        dp[j] = j as i32;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for i in 1..=n {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        let mut leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n        dp[0] = i as i32;\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for j in 1..=m {\n            let temp = dp[j];\n            if s.chars().nth(i - 1) == t.chars().nth(j - 1) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[j] = leftup;\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[j] = std::cmp::min(std::cmp::min(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n        }\n    }\n    dp[m]\n}\n
        edit_distance.c
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(char *s, char *t, int n, int m) {\n    int *dp = calloc(m + 1, sizeof(int));\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (int j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (int i = 1; i <= n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        int leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n        dp[0] = i;\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (int j = 1; j <= m; j++) {\n            int temp = dp[j];\n            if (s[i - 1] == t[j - 1]) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[j] = leftup;\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[j] = myMin(myMin(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n        }\n    }\n    int res = dp[m];\n    // \u91ca\u653e\u5185\u5b58\n    free(dp);\n    return res;\n}\n
        edit_distance.kt
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfun editDistanceDPComp(s: String, t: String): Int {\n    val n = s.length\n    val m = t.length\n    val dp = IntArray(m + 1)\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (j in 1..m) {\n        dp[j] = j\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (i in 1..n) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        var leftup = dp[0] // \u6682\u5b58 dp[i-1, j-1]\n        dp[0] = i\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (j in 1..m) {\n            val temp = dp[j]\n            if (s[i - 1] == t[j - 1]) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[j] = leftup\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1\n            }\n            leftup = temp // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n        }\n    }\n    return dp[m]\n}\n
        edit_distance.rb
        ### \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 ###\ndef edit_distance_dp_comp(s, t)\n  n, m = s.length, t.length\n  dp = Array.new(m + 1, 0)\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n  (1...(m + 1)).each { |j| dp[j] = j }\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n  for i in 1...(n + 1)\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    leftup = dp.first # \u6682\u5b58 dp[i-1, j-1]\n    dp[0] += 1\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n    for j in 1...(m + 1)\n      temp = dp[j]\n      if s[i - 1] == t[j - 1]\n        # \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n        dp[j] = leftup\n      else\n        # \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n        dp[j] = [dp[j - 1], dp[j], leftup].min + 1\n      end\n      leftup = temp # \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n    end\n  end\n  dp[m]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/","title":"14.1 \u00a0 \u521d\u63a2\u52a8\u6001\u89c4\u5212","text":"

        \u52a8\u6001\u89c4\u5212\uff08dynamic programming\uff09\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u7b97\u6cd5\u8303\u5f0f\uff0c\u5b83\u5c06\u4e00\u4e2a\u95ee\u9898\u5206\u89e3\u4e3a\u4e00\u7cfb\u5217\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u5e76\u901a\u8fc7\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\u6765\u907f\u514d\u91cd\u590d\u8ba1\u7b97\uff0c\u4ece\u800c\u5927\u5e45\u63d0\u5347\u65f6\u95f4\u6548\u7387\u3002

        \u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u4ece\u4e00\u4e2a\u7ecf\u5178\u4f8b\u9898\u5165\u624b\uff0c\u5148\u7ed9\u51fa\u5b83\u7684\u66b4\u529b\u56de\u6eaf\u89e3\u6cd5\uff0c\u89c2\u5bdf\u5176\u4e2d\u5305\u542b\u7684\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u518d\u9010\u6b65\u5bfc\u51fa\u66f4\u9ad8\u6548\u7684\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u3002

        \u722c\u697c\u68af

        \u7ed9\u5b9a\u4e00\u4e2a\u5171\u6709 \\(n\\) \u9636\u7684\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\uff0c\u8bf7\u95ee\u6709\u591a\u5c11\u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\uff1f

        \u5982\u56fe 14-1 \u6240\u793a\uff0c\u5bf9\u4e8e\u4e00\u4e2a \\(3\\) \u9636\u697c\u68af\uff0c\u5171\u6709 \\(3\\) \u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\u3002

        \u56fe 14-1 \u00a0 \u722c\u5230\u7b2c 3 \u9636\u7684\u65b9\u6848\u6570\u91cf

        \u672c\u9898\u7684\u76ee\u6807\u662f\u6c42\u89e3\u65b9\u6848\u6570\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u901a\u8fc7\u56de\u6eaf\u6765\u7a77\u4e3e\u6240\u6709\u53ef\u80fd\u6027\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5c06\u722c\u697c\u68af\u60f3\u8c61\u4e3a\u4e00\u4e2a\u591a\u8f6e\u9009\u62e9\u7684\u8fc7\u7a0b\uff1a\u4ece\u5730\u9762\u51fa\u53d1\uff0c\u6bcf\u8f6e\u9009\u62e9\u4e0a \\(1\\) \u9636\u6216 \\(2\\) \u9636\uff0c\u6bcf\u5f53\u5230\u8fbe\u697c\u68af\u9876\u90e8\u65f6\u5c31\u5c06\u65b9\u6848\u6570\u91cf\u52a0 \\(1\\) \uff0c\u5f53\u8d8a\u8fc7\u697c\u68af\u9876\u90e8\u65f6\u5c31\u5c06\u5176\u526a\u679d\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_backtrack.py
        def backtrack(choices: list[int], state: int, n: int, res: list[int]) -> int:\n    \"\"\"\u56de\u6eaf\"\"\"\n    # \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n    if state == n:\n        res[0] += 1\n    # \u904d\u5386\u6240\u6709\u9009\u62e9\n    for choice in choices:\n        # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n        if state + choice > n:\n            continue\n        # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n        backtrack(choices, state + choice, n, res)\n        # \u56de\u9000\n\ndef climbing_stairs_backtrack(n: int) -> int:\n    \"\"\"\u722c\u697c\u68af\uff1a\u56de\u6eaf\"\"\"\n    choices = [1, 2]  # \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n    state = 0  # \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n    res = [0]  # \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n    backtrack(choices, state, n, res)\n    return res[0]\n
        climbing_stairs_backtrack.cpp
        /* \u56de\u6eaf */\nvoid backtrack(vector<int> &choices, int state, int n, vector<int> &res) {\n    // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n    if (state == n)\n        res[0]++;\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (auto &choice : choices) {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n        if (state + choice > n)\n            continue;\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n        backtrack(choices, state + choice, n, res);\n        // \u56de\u9000\n    }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\n    vector<int> choices = {1, 2}; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n    int state = 0;                // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n    vector<int> res = {0};        // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n    backtrack(choices, state, n, res);\n    return res[0];\n}\n
        climbing_stairs_backtrack.java
        /* \u56de\u6eaf */\nvoid backtrack(List<Integer> choices, int state, int n, List<Integer> res) {\n    // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n    if (state == n)\n        res.set(0, res.get(0) + 1);\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (Integer choice : choices) {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n        if (state + choice > n)\n            continue;\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n        backtrack(choices, state + choice, n, res);\n        // \u56de\u9000\n    }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\n    List<Integer> choices = Arrays.asList(1, 2); // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n    int state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n    List<Integer> res = new ArrayList<>();\n    res.add(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n    backtrack(choices, state, n, res);\n    return res.get(0);\n}\n
        climbing_stairs_backtrack.cs
        /* \u56de\u6eaf */\nvoid Backtrack(List<int> choices, int state, int n, List<int> res) {\n    // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n    if (state == n)\n        res[0]++;\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    foreach (int choice in choices) {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n        if (state + choice > n)\n            continue;\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n        Backtrack(choices, state + choice, n, res);\n        // \u56de\u9000\n    }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint ClimbingStairsBacktrack(int n) {\n    List<int> choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n    int state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n    List<int> res = [0]; // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n    Backtrack(choices, state, n, res);\n    return res[0];\n}\n
        climbing_stairs_backtrack.go
        /* \u56de\u6eaf */\nfunc backtrack(choices []int, state, n int, res []int) {\n    // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n    if state == n {\n        res[0] = res[0] + 1\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for _, choice := range choices {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n        if state+choice > n {\n            continue\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n        backtrack(choices, state+choice, n, res)\n        // \u56de\u9000\n    }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunc climbingStairsBacktrack(n int) int {\n    // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n    choices := []int{1, 2}\n    // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n    state := 0\n    res := make([]int, 1)\n    // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n    res[0] = 0\n    backtrack(choices, state, n, res)\n    return res[0]\n}\n
        climbing_stairs_backtrack.swift
        /* \u56de\u6eaf */\nfunc backtrack(choices: [Int], state: Int, n: Int, res: inout [Int]) {\n    // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n    if state == n {\n        res[0] += 1\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for choice in choices {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n        if state + choice > n {\n            continue\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n        backtrack(choices: choices, state: state + choice, n: n, res: &res)\n        // \u56de\u9000\n    }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunc climbingStairsBacktrack(n: Int) -> Int {\n    let choices = [1, 2] // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n    let state = 0 // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n    var res: [Int] = []\n    res.append(0) // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n    backtrack(choices: choices, state: state, n: n, res: &res)\n    return res[0]\n}\n
        climbing_stairs_backtrack.js
        /* \u56de\u6eaf */\nfunction backtrack(choices, state, n, res) {\n    // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n    if (state === n) res.set(0, res.get(0) + 1);\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (const choice of choices) {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n        if (state + choice > n) continue;\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n        backtrack(choices, state + choice, n, res);\n        // \u56de\u9000\n    }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunction climbingStairsBacktrack(n) {\n    const choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n    const state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n    const res = new Map();\n    res.set(0, 0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n    backtrack(choices, state, n, res);\n    return res.get(0);\n}\n
        climbing_stairs_backtrack.ts
        /* \u56de\u6eaf */\nfunction backtrack(\n    choices: number[],\n    state: number,\n    n: number,\n    res: Map<0, any>\n): void {\n    // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n    if (state === n) res.set(0, res.get(0) + 1);\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (const choice of choices) {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n        if (state + choice > n) continue;\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n        backtrack(choices, state + choice, n, res);\n        // \u56de\u9000\n    }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunction climbingStairsBacktrack(n: number): number {\n    const choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n    const state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n    const res = new Map();\n    res.set(0, 0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n    backtrack(choices, state, n, res);\n    return res.get(0);\n}\n
        climbing_stairs_backtrack.dart
        /* \u56de\u6eaf */\nvoid backtrack(List<int> choices, int state, int n, List<int> res) {\n  // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n  if (state == n) {\n    res[0]++;\n  }\n  // \u904d\u5386\u6240\u6709\u9009\u62e9\n  for (int choice in choices) {\n    // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n    if (state + choice > n) continue;\n    // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n    backtrack(choices, state + choice, n, res);\n    // \u56de\u9000\n  }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\n  List<int> choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n  int state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n  List<int> res = [];\n  res.add(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n  backtrack(choices, state, n, res);\n  return res[0];\n}\n
        climbing_stairs_backtrack.rs
        /* \u56de\u6eaf */\nfn backtrack(choices: &[i32], state: i32, n: i32, res: &mut [i32]) {\n    // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n    if state == n {\n        res[0] = res[0] + 1;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for &choice in choices {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n        if state + choice > n {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n        backtrack(choices, state + choice, n, res);\n        // \u56de\u9000\n    }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfn climbing_stairs_backtrack(n: usize) -> i32 {\n    let choices = vec![1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n    let state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n    let mut res = Vec::new();\n    res.push(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n    backtrack(&choices, state, n as i32, &mut res);\n    res[0]\n}\n
        climbing_stairs_backtrack.c
        /* \u56de\u6eaf */\nvoid backtrack(int *choices, int state, int n, int *res, int len) {\n    // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n    if (state == n)\n        res[0]++;\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (int i = 0; i < len; i++) {\n        int choice = choices[i];\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n        if (state + choice > n)\n            continue;\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n        backtrack(choices, state + choice, n, res, len);\n        // \u56de\u9000\n    }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\n    int choices[2] = {1, 2}; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n    int state = 0;           // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n    int *res = (int *)malloc(sizeof(int));\n    *res = 0; // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n    int len = sizeof(choices) / sizeof(int);\n    backtrack(choices, state, n, res, len);\n    int result = *res;\n    free(res);\n    return result;\n}\n
        climbing_stairs_backtrack.kt
        /* \u56de\u6eaf */\nfun backtrack(\n    choices: MutableList<Int>,\n    state: Int,\n    n: Int,\n    res: MutableList<Int>\n) {\n    // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n    if (state == n)\n        res[0] = res[0] + 1\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (choice in choices) {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n        if (state + choice > n) continue\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n        backtrack(choices, state + choice, n, res)\n        // \u56de\u9000\n    }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfun climbingStairsBacktrack(n: Int): Int {\n    val choices = mutableListOf(1, 2) // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n    val state = 0 // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n    val res = mutableListOf<Int>()\n    res.add(0) // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n    backtrack(choices, state, n, res)\n    return res[0]\n}\n
        climbing_stairs_backtrack.rb
        ### \u56de\u6eaf ###\ndef backtrack(choices, state, n, res)\n  # \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n  res[0] += 1 if state == n\n  # \u904d\u5386\u6240\u6709\u9009\u62e9\n  for choice in choices\n    # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n    next if state + choice > n\n\n    # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n    backtrack(choices, state + choice, n, res)\n  end\n  # \u56de\u9000\nend\n\n### \u722c\u697c\u68af\uff1a\u56de\u6eaf ###\ndef climbing_stairs_backtrack(n)\n  choices = [1, 2] # \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n  state = 0 # \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n  res = [0] # \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n  backtrack(choices, state, n, res)\n  res.first\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1411","title":"14.1.1 \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u7d22","text":"

        \u56de\u6eaf\u7b97\u6cd5\u901a\u5e38\u5e76\u4e0d\u663e\u5f0f\u5730\u5bf9\u95ee\u9898\u8fdb\u884c\u62c6\u89e3\uff0c\u800c\u662f\u5c06\u6c42\u89e3\u95ee\u9898\u770b\u4f5c\u4e00\u7cfb\u5217\u51b3\u7b56\u6b65\u9aa4\uff0c\u901a\u8fc7\u8bd5\u63a2\u548c\u526a\u679d\uff0c\u641c\u7d22\u6240\u6709\u53ef\u80fd\u7684\u89e3\u3002

        \u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u4ece\u95ee\u9898\u5206\u89e3\u7684\u89d2\u5ea6\u5206\u6790\u8fd9\u9053\u9898\u3002\u8bbe\u722c\u5230\u7b2c \\(i\\) \u9636\u5171\u6709 \\(dp[i]\\) \u79cd\u65b9\u6848\uff0c\u90a3\u4e48 \\(dp[i]\\) \u5c31\u662f\u539f\u95ee\u9898\uff0c\u5176\u5b50\u95ee\u9898\u5305\u62ec\uff1a

        \\[ dp[i-1], dp[i-2], \\dots, dp[2], dp[1] \\]

        \u7531\u4e8e\u6bcf\u8f6e\u53ea\u80fd\u4e0a \\(1\\) \u9636\u6216 \\(2\\) \u9636\uff0c\u56e0\u6b64\u5f53\u6211\u4eec\u7ad9\u5728\u7b2c \\(i\\) \u9636\u697c\u68af\u4e0a\u65f6\uff0c\u4e0a\u4e00\u8f6e\u53ea\u53ef\u80fd\u7ad9\u5728\u7b2c \\(i - 1\\) \u9636\u6216\u7b2c \\(i - 2\\) \u9636\u4e0a\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u53ea\u80fd\u4ece\u7b2c \\(i -1\\) \u9636\u6216\u7b2c \\(i - 2\\) \u9636\u8fc8\u5411\u7b2c \\(i\\) \u9636\u3002

        \u7531\u6b64\u4fbf\u53ef\u5f97\u51fa\u4e00\u4e2a\u91cd\u8981\u63a8\u8bba\uff1a\u722c\u5230\u7b2c \\(i - 1\\) \u9636\u7684\u65b9\u6848\u6570\u52a0\u4e0a\u722c\u5230\u7b2c \\(i - 2\\) \u9636\u7684\u65b9\u6848\u6570\u5c31\u7b49\u4e8e\u722c\u5230\u7b2c \\(i\\) \u9636\u7684\u65b9\u6848\u6570\u3002\u516c\u5f0f\u5982\u4e0b\uff1a

        \\[ dp[i] = dp[i-1] + dp[i-2] \\]

        \u8fd9\u610f\u5473\u7740\u5728\u722c\u697c\u68af\u95ee\u9898\u4e2d\uff0c\u5404\u4e2a\u5b50\u95ee\u9898\u4e4b\u95f4\u5b58\u5728\u9012\u63a8\u5173\u7cfb\uff0c\u539f\u95ee\u9898\u7684\u89e3\u53ef\u4ee5\u7531\u5b50\u95ee\u9898\u7684\u89e3\u6784\u5efa\u5f97\u6765\u3002\u56fe 14-2 \u5c55\u793a\u4e86\u8be5\u9012\u63a8\u5173\u7cfb\u3002

        \u56fe 14-2 \u00a0 \u65b9\u6848\u6570\u91cf\u9012\u63a8\u5173\u7cfb

        \u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9012\u63a8\u516c\u5f0f\u5f97\u5230\u66b4\u529b\u641c\u7d22\u89e3\u6cd5\u3002\u4ee5 \\(dp[n]\\) \u4e3a\u8d77\u59cb\u70b9\uff0c\u9012\u5f52\u5730\u5c06\u4e00\u4e2a\u8f83\u5927\u95ee\u9898\u62c6\u89e3\u4e3a\u4e24\u4e2a\u8f83\u5c0f\u95ee\u9898\u7684\u548c\uff0c\u76f4\u81f3\u5230\u8fbe\u6700\u5c0f\u5b50\u95ee\u9898 \\(dp[1]\\) \u548c \\(dp[2]\\) \u65f6\u8fd4\u56de\u3002\u5176\u4e2d\uff0c\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\u662f\u5df2\u77e5\u7684\uff0c\u5373 \\(dp[1] = 1\\)\u3001\\(dp[2] = 2\\) \uff0c\u8868\u793a\u722c\u5230\u7b2c \\(1\\)\u3001\\(2\\) \u9636\u5206\u522b\u6709 \\(1\\)\u3001\\(2\\) \u79cd\u65b9\u6848\u3002

        \u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5b83\u548c\u6807\u51c6\u56de\u6eaf\u4ee3\u7801\u90fd\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff0c\u4f46\u66f4\u52a0\u7b80\u6d01\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dfs.py
        def dfs(i: int) -> int:\n    \"\"\"\u641c\u7d22\"\"\"\n    # \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if i == 1 or i == 2:\n        return i\n    # dp[i] = dp[i-1] + dp[i-2]\n    count = dfs(i - 1) + dfs(i - 2)\n    return count\n\ndef climbing_stairs_dfs(n: int) -> int:\n    \"\"\"\u722c\u697c\u68af\uff1a\u641c\u7d22\"\"\"\n    return dfs(n)\n
        climbing_stairs_dfs.cpp
        /* \u641c\u7d22 */\nint dfs(int i) {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\n    return dfs(n);\n}\n
        climbing_stairs_dfs.java
        /* \u641c\u7d22 */\nint dfs(int i) {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\n    return dfs(n);\n}\n
        climbing_stairs_dfs.cs
        /* \u641c\u7d22 */\nint DFS(int i) {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = DFS(i - 1) + DFS(i - 2);\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint ClimbingStairsDFS(int n) {\n    return DFS(n);\n}\n
        climbing_stairs_dfs.go
        /* \u641c\u7d22 */\nfunc dfs(i int) int {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if i == 1 || i == 2 {\n        return i\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    count := dfs(i-1) + dfs(i-2)\n    return count\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunc climbingStairsDFS(n int) int {\n    return dfs(n)\n}\n
        climbing_stairs_dfs.swift
        /* \u641c\u7d22 */\nfunc dfs(i: Int) -> Int {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if i == 1 || i == 2 {\n        return i\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    let count = dfs(i: i - 1) + dfs(i: i - 2)\n    return count\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunc climbingStairsDFS(n: Int) -> Int {\n    dfs(i: n)\n}\n
        climbing_stairs_dfs.js
        /* \u641c\u7d22 */\nfunction dfs(i) {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i === 1 || i === 2) return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    const count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunction climbingStairsDFS(n) {\n    return dfs(n);\n}\n
        climbing_stairs_dfs.ts
        /* \u641c\u7d22 */\nfunction dfs(i: number): number {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i === 1 || i === 2) return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    const count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunction climbingStairsDFS(n: number): number {\n    return dfs(n);\n}\n
        climbing_stairs_dfs.dart
        /* \u641c\u7d22 */\nint dfs(int i) {\n  // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n  if (i == 1 || i == 2) return i;\n  // dp[i] = dp[i-1] + dp[i-2]\n  int count = dfs(i - 1) + dfs(i - 2);\n  return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\n  return dfs(n);\n}\n
        climbing_stairs_dfs.rs
        /* \u641c\u7d22 */\nfn dfs(i: usize) -> i32 {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if i == 1 || i == 2 {\n        return i as i32;\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    let count = dfs(i - 1) + dfs(i - 2);\n    count\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfn climbing_stairs_dfs(n: usize) -> i32 {\n    dfs(n)\n}\n
        climbing_stairs_dfs.c
        /* \u641c\u7d22 */\nint dfs(int i) {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\n    return dfs(n);\n}\n
        climbing_stairs_dfs.kt
        /* \u641c\u7d22 */\nfun dfs(i: Int): Int {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i == 1 || i == 2) return i\n    // dp[i] = dp[i-1] + dp[i-2]\n    val count = dfs(i - 1) + dfs(i - 2)\n    return count\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfun climbingStairsDFS(n: Int): Int {\n    return dfs(n)\n}\n
        climbing_stairs_dfs.rb
        ### \u641c\u7d22 ###\ndef dfs(i)\n  # \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n  return i if i == 1 || i == 2\n  # dp[i] = dp[i-1] + dp[i-2]\n  dfs(i - 1) + dfs(i - 2)\nend\n\n### \u722c\u697c\u68af\uff1a\u641c\u7d22 ###\ndef climbing_stairs_dfs(n)\n  dfs(n)\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 14-3 \u5c55\u793a\u4e86\u66b4\u529b\u641c\u7d22\u5f62\u6210\u7684\u9012\u5f52\u6811\u3002\u5bf9\u4e8e\u95ee\u9898 \\(dp[n]\\) \uff0c\u5176\u9012\u5f52\u6811\u7684\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002\u6307\u6570\u9636\u5c5e\u4e8e\u7206\u70b8\u5f0f\u589e\u957f\uff0c\u5982\u679c\u6211\u4eec\u8f93\u5165\u4e00\u4e2a\u6bd4\u8f83\u5927\u7684 \\(n\\) \uff0c\u5219\u4f1a\u9677\u5165\u6f2b\u957f\u7684\u7b49\u5f85\u4e4b\u4e2d\u3002

        \u56fe 14-3 \u00a0 \u722c\u697c\u68af\u5bf9\u5e94\u9012\u5f52\u6811

        \u89c2\u5bdf\u56fe 14-3 \uff0c\u6307\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f\u201c\u91cd\u53e0\u5b50\u95ee\u9898\u201d\u5bfc\u81f4\u7684\u3002\u4f8b\u5982 \\(dp[9]\\) \u88ab\u5206\u89e3\u4e3a \\(dp[8]\\) \u548c \\(dp[7]\\) \uff0c\\(dp[8]\\) \u88ab\u5206\u89e3\u4e3a \\(dp[7]\\) \u548c \\(dp[6]\\) \uff0c\u4e24\u8005\u90fd\u5305\u542b\u5b50\u95ee\u9898 \\(dp[7]\\) \u3002

        \u4ee5\u6b64\u7c7b\u63a8\uff0c\u5b50\u95ee\u9898\u4e2d\u5305\u542b\u66f4\u5c0f\u7684\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u5b50\u5b50\u5b59\u5b59\u65e0\u7a77\u5c3d\u4e5f\u3002\u7edd\u5927\u90e8\u5206\u8ba1\u7b97\u8d44\u6e90\u90fd\u6d6a\u8d39\u5728\u8fd9\u4e9b\u91cd\u53e0\u7684\u5b50\u95ee\u9898\u4e0a\u3002

        "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1412","title":"14.1.2 \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22","text":"

        \u4e3a\u4e86\u63d0\u5347\u7b97\u6cd5\u6548\u7387\uff0c\u6211\u4eec\u5e0c\u671b\u6240\u6709\u7684\u91cd\u53e0\u5b50\u95ee\u9898\u90fd\u53ea\u88ab\u8ba1\u7b97\u4e00\u6b21\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u6570\u7ec4 mem \u6765\u8bb0\u5f55\u6bcf\u4e2a\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5e76\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u5c06\u91cd\u53e0\u5b50\u95ee\u9898\u526a\u679d\u3002

        1. \u5f53\u9996\u6b21\u8ba1\u7b97 \\(dp[i]\\) \u65f6\uff0c\u6211\u4eec\u5c06\u5176\u8bb0\u5f55\u81f3 mem[i] \uff0c\u4ee5\u4fbf\u4e4b\u540e\u4f7f\u7528\u3002
        2. \u5f53\u518d\u6b21\u9700\u8981\u8ba1\u7b97 \\(dp[i]\\) \u65f6\uff0c\u6211\u4eec\u4fbf\u53ef\u76f4\u63a5\u4ece mem[i] \u4e2d\u83b7\u53d6\u7ed3\u679c\uff0c\u4ece\u800c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u8be5\u5b50\u95ee\u9898\u3002

        \u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dfs_mem.py
        def dfs(i: int, mem: list[int]) -> int:\n    \"\"\"\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n    # \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if i == 1 or i == 2:\n        return i\n    # \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n    if mem[i] != -1:\n        return mem[i]\n    # dp[i] = dp[i-1] + dp[i-2]\n    count = dfs(i - 1, mem) + dfs(i - 2, mem)\n    # \u8bb0\u5f55 dp[i]\n    mem[i] = count\n    return count\n\ndef climbing_stairs_dfs_mem(n: int) -> int:\n    \"\"\"\u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n    # mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n    mem = [-1] * (n + 1)\n    return dfs(n, mem)\n
        climbing_stairs_dfs_mem.cpp
        /* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, vector<int> &mem) {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i == 1 || i == 2)\n        return i;\n    // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // \u8bb0\u5f55 dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n    // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n    vector<int> mem(n + 1, -1);\n    return dfs(n, mem);\n}\n
        climbing_stairs_dfs_mem.java
        /* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, int[] mem) {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i == 1 || i == 2)\n        return i;\n    // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // \u8bb0\u5f55 dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n    // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n    int[] mem = new int[n + 1];\n    Arrays.fill(mem, -1);\n    return dfs(n, mem);\n}\n
        climbing_stairs_dfs_mem.cs
        /* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint DFS(int i, int[] mem) {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i == 1 || i == 2)\n        return i;\n    // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = DFS(i - 1, mem) + DFS(i - 2, mem);\n    // \u8bb0\u5f55 dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint ClimbingStairsDFSMem(int n) {\n    // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n    int[] mem = new int[n + 1];\n    Array.Fill(mem, -1);\n    return DFS(n, mem);\n}\n
        climbing_stairs_dfs_mem.go
        /* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc dfsMem(i int, mem []int) int {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if i == 1 || i == 2 {\n        return i\n    }\n    // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n    if mem[i] != -1 {\n        return mem[i]\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    count := dfsMem(i-1, mem) + dfsMem(i-2, mem)\n    // \u8bb0\u5f55 dp[i]\n    mem[i] = count\n    return count\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc climbingStairsDFSMem(n int) int {\n    // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n    mem := make([]int, n+1)\n    for i := range mem {\n        mem[i] = -1\n    }\n    return dfsMem(n, mem)\n}\n
        climbing_stairs_dfs_mem.swift
        /* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc dfs(i: Int, mem: inout [Int]) -> Int {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if i == 1 || i == 2 {\n        return i\n    }\n    // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n    if mem[i] != -1 {\n        return mem[i]\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    let count = dfs(i: i - 1, mem: &mem) + dfs(i: i - 2, mem: &mem)\n    // \u8bb0\u5f55 dp[i]\n    mem[i] = count\n    return count\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc climbingStairsDFSMem(n: Int) -> Int {\n    // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n    var mem = Array(repeating: -1, count: n + 1)\n    return dfs(i: n, mem: &mem)\n}\n
        climbing_stairs_dfs_mem.js
        /* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction dfs(i, mem) {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i === 1 || i === 2) return i;\n    // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n    if (mem[i] != -1) return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    const count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // \u8bb0\u5f55 dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction climbingStairsDFSMem(n) {\n    // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n    const mem = new Array(n + 1).fill(-1);\n    return dfs(n, mem);\n}\n
        climbing_stairs_dfs_mem.ts
        /* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction dfs(i: number, mem: number[]): number {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i === 1 || i === 2) return i;\n    // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n    if (mem[i] != -1) return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    const count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // \u8bb0\u5f55 dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction climbingStairsDFSMem(n: number): number {\n    // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n    const mem = new Array(n + 1).fill(-1);\n    return dfs(n, mem);\n}\n
        climbing_stairs_dfs_mem.dart
        /* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, List<int> mem) {\n  // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n  if (i == 1 || i == 2) return i;\n  // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n  if (mem[i] != -1) return mem[i];\n  // dp[i] = dp[i-1] + dp[i-2]\n  int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n  // \u8bb0\u5f55 dp[i]\n  mem[i] = count;\n  return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n  // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n  List<int> mem = List.filled(n + 1, -1);\n  return dfs(n, mem);\n}\n
        climbing_stairs_dfs_mem.rs
        /* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn dfs(i: usize, mem: &mut [i32]) -> i32 {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if i == 1 || i == 2 {\n        return i as i32;\n    }\n    // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n    if mem[i] != -1 {\n        return mem[i];\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    let count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // \u8bb0\u5f55 dp[i]\n    mem[i] = count;\n    count\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn climbing_stairs_dfs_mem(n: usize) -> i32 {\n    // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n    let mut mem = vec![-1; n + 1];\n    dfs(n, &mut mem)\n}\n
        climbing_stairs_dfs_mem.c
        /* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, int *mem) {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i == 1 || i == 2)\n        return i;\n    // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // \u8bb0\u5f55 dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n    // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n    int *mem = (int *)malloc((n + 1) * sizeof(int));\n    for (int i = 0; i <= n; i++) {\n        mem[i] = -1;\n    }\n    int result = dfs(n, mem);\n    free(mem);\n    return result;\n}\n
        climbing_stairs_dfs_mem.kt
        /* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfun dfs(i: Int, mem: IntArray): Int {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i == 1 || i == 2) return i\n    // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n    if (mem[i] != -1) return mem[i]\n    // dp[i] = dp[i-1] + dp[i-2]\n    val count = dfs(i - 1, mem) + dfs(i - 2, mem)\n    // \u8bb0\u5f55 dp[i]\n    mem[i] = count\n    return count\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfun climbingStairsDFSMem(n: Int): Int {\n    // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n    val mem = IntArray(n + 1)\n    mem.fill(-1)\n    return dfs(n, mem)\n}\n
        climbing_stairs_dfs_mem.rb
        ### \u8bb0\u5fc6\u5316\u641c\u7d22 ###\ndef dfs(i, mem)\n  # \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n  return i if i == 1 || i == 2\n  # \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n  return mem[i] if mem[i] != -1\n\n  # dp[i] = dp[i-1] + dp[i-2]\n  count = dfs(i - 1, mem) + dfs(i - 2, mem)\n  # \u8bb0\u5f55 dp[i]\n  mem[i] = count\nend\n\n### \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 ###\ndef climbing_stairs_dfs_mem(n)\n  # mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n  mem = Array.new(n + 1, -1)\n  dfs(n, mem)\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u89c2\u5bdf\u56fe 14-4 \uff0c\u7ecf\u8fc7\u8bb0\u5fc6\u5316\u5904\u7406\u540e\uff0c\u6240\u6709\u91cd\u53e0\u5b50\u95ee\u9898\u90fd\u53ea\u9700\u8ba1\u7b97\u4e00\u6b21\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(n)\\) \uff0c\u8fd9\u662f\u4e00\u4e2a\u5de8\u5927\u7684\u98de\u8dc3\u3002

        \u56fe 14-4 \u00a0 \u8bb0\u5fc6\u5316\u641c\u7d22\u5bf9\u5e94\u9012\u5f52\u6811

        "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1413","title":"14.1.3 \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52a8\u6001\u89c4\u5212","text":"

        \u8bb0\u5fc6\u5316\u641c\u7d22\u662f\u4e00\u79cd\u201c\u4ece\u9876\u81f3\u5e95\u201d\u7684\u65b9\u6cd5\uff1a\u6211\u4eec\u4ece\u539f\u95ee\u9898\uff08\u6839\u8282\u70b9\uff09\u5f00\u59cb\uff0c\u9012\u5f52\u5730\u5c06\u8f83\u5927\u5b50\u95ee\u9898\u5206\u89e3\u4e3a\u8f83\u5c0f\u5b50\u95ee\u9898\uff0c\u76f4\u81f3\u89e3\u5df2\u77e5\u7684\u6700\u5c0f\u5b50\u95ee\u9898\uff08\u53f6\u8282\u70b9\uff09\u3002\u4e4b\u540e\uff0c\u901a\u8fc7\u56de\u6eaf\u9010\u5c42\u6536\u96c6\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u6784\u5efa\u51fa\u539f\u95ee\u9898\u7684\u89e3\u3002

        \u4e0e\u4e4b\u76f8\u53cd\uff0c\u52a8\u6001\u89c4\u5212\u662f\u4e00\u79cd\u201c\u4ece\u5e95\u81f3\u9876\u201d\u7684\u65b9\u6cd5\uff1a\u4ece\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\u5f00\u59cb\uff0c\u8fed\u4ee3\u5730\u6784\u5efa\u66f4\u5927\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u76f4\u81f3\u5f97\u5230\u539f\u95ee\u9898\u7684\u89e3\u3002

        \u7531\u4e8e\u52a8\u6001\u89c4\u5212\u4e0d\u5305\u542b\u56de\u6eaf\u8fc7\u7a0b\uff0c\u56e0\u6b64\u53ea\u9700\u4f7f\u7528\u5faa\u73af\u8fed\u4ee3\u5b9e\u73b0\uff0c\u65e0\u987b\u4f7f\u7528\u9012\u5f52\u3002\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521d\u59cb\u5316\u4e00\u4e2a\u6570\u7ec4 dp \u6765\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5b83\u8d77\u5230\u4e86\u4e0e\u8bb0\u5fc6\u5316\u641c\u7d22\u4e2d\u6570\u7ec4 mem \u76f8\u540c\u7684\u8bb0\u5f55\u4f5c\u7528\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dp.py
        def climbing_stairs_dp(n: int) -> int:\n    \"\"\"\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n    if n == 1 or n == 2:\n        return n\n    # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    dp = [0] * (n + 1)\n    # \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1], dp[2] = 1, 2\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i in range(3, n + 1):\n        dp[i] = dp[i - 1] + dp[i - 2]\n    return dp[n]\n
        climbing_stairs_dp.cpp
        /* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    vector<int> dp(n + 1);\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
        climbing_stairs_dp.java
        /* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    int[] dp = new int[n + 1];\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
        climbing_stairs_dp.cs
        /* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint ClimbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    int[] dp = new int[n + 1];\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
        climbing_stairs_dp.go
        /* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDP(n int) int {\n    if n == 1 || n == 2 {\n        return n\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    dp := make([]int, n+1)\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = 1\n    dp[2] = 2\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i := 3; i <= n; i++ {\n        dp[i] = dp[i-1] + dp[i-2]\n    }\n    return dp[n]\n}\n
        climbing_stairs_dp.swift
        /* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDP(n: Int) -> Int {\n    if n == 1 || n == 2 {\n        return n\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    var dp = Array(repeating: 0, count: n + 1)\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = 1\n    dp[2] = 2\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i in 3 ... n {\n        dp[i] = dp[i - 1] + dp[i - 2]\n    }\n    return dp[n]\n}\n
        climbing_stairs_dp.js
        /* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDP(n) {\n    if (n === 1 || n === 2) return n;\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    const dp = new Array(n + 1).fill(-1);\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (let i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
        climbing_stairs_dp.ts
        /* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDP(n: number): number {\n    if (n === 1 || n === 2) return n;\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    const dp = new Array(n + 1).fill(-1);\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (let i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
        climbing_stairs_dp.dart
        /* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\n  if (n == 1 || n == 2) return n;\n  // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n  List<int> dp = List.filled(n + 1, 0);\n  // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n  dp[1] = 1;\n  dp[2] = 2;\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n  for (int i = 3; i <= n; i++) {\n    dp[i] = dp[i - 1] + dp[i - 2];\n  }\n  return dp[n];\n}\n
        climbing_stairs_dp.rs
        /* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfn climbing_stairs_dp(n: usize) -> i32 {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if n == 1 || n == 2 {\n        return n as i32;\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    let mut dp = vec![-1; n + 1];\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i in 3..=n {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    dp[n]\n}\n
        climbing_stairs_dp.c
        /* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    int *dp = (int *)malloc((n + 1) * sizeof(int));\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    int result = dp[n];\n    free(dp);\n    return result;\n}\n
        climbing_stairs_dp.kt
        /* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfun climbingStairsDP(n: Int): Int {\n    if (n == 1 || n == 2) return n\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    val dp = IntArray(n + 1)\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = 1\n    dp[2] = 2\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (i in 3..n) {\n        dp[i] = dp[i - 1] + dp[i - 2]\n    }\n    return dp[n]\n}\n
        climbing_stairs_dp.rb
        ### \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 ###\ndef climbing_stairs_dp(n)\n  return n  if n == 1 || n == 2\n\n  # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n  dp = Array.new(n + 1, 0)\n  # \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n  dp[1], dp[2] = 1, 2\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n  (3...(n + 1)).each { |i| dp[i] = dp[i - 1] + dp[i - 2] }\n\n  dp[n]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 14-5 \u6a21\u62df\u4e86\u4ee5\u4e0a\u4ee3\u7801\u7684\u6267\u884c\u8fc7\u7a0b\u3002

        \u56fe 14-5 \u00a0 \u722c\u697c\u68af\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

        \u4e0e\u56de\u6eaf\u7b97\u6cd5\u4e00\u6837\uff0c\u52a8\u6001\u89c4\u5212\u4e5f\u4f7f\u7528\u201c\u72b6\u6001\u201d\u6982\u5ff5\u6765\u8868\u793a\u95ee\u9898\u6c42\u89e3\u7684\u7279\u5b9a\u9636\u6bb5\uff0c\u6bcf\u4e2a\u72b6\u6001\u90fd\u5bf9\u5e94\u4e00\u4e2a\u5b50\u95ee\u9898\u4ee5\u53ca\u76f8\u5e94\u7684\u5c40\u90e8\u6700\u4f18\u89e3\u3002\u4f8b\u5982\uff0c\u722c\u697c\u68af\u95ee\u9898\u7684\u72b6\u6001\u5b9a\u4e49\u4e3a\u5f53\u524d\u6240\u5728\u697c\u68af\u9636\u6570 \\(i\\) \u3002

        \u6839\u636e\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u53ef\u4ee5\u603b\u7ed3\u51fa\u52a8\u6001\u89c4\u5212\u7684\u5e38\u7528\u672f\u8bed\u3002

        • \u5c06\u6570\u7ec4 dp \u79f0\u4e3a dp \u8868\uff0c\\(dp[i]\\) \u8868\u793a\u72b6\u6001 \\(i\\) \u5bf9\u5e94\u5b50\u95ee\u9898\u7684\u89e3\u3002
        • \u5c06\u6700\u5c0f\u5b50\u95ee\u9898\u5bf9\u5e94\u7684\u72b6\u6001\uff08\u7b2c \\(1\\) \u9636\u548c\u7b2c \\(2\\) \u9636\u697c\u68af\uff09\u79f0\u4e3a\u521d\u59cb\u72b6\u6001\u3002
        • \u5c06\u9012\u63a8\u516c\u5f0f \\(dp[i] = dp[i-1] + dp[i-2]\\) \u79f0\u4e3a\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u3002
        "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1414","title":"14.1.4 \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

        \u7ec6\u5fc3\u7684\u8bfb\u8005\u53ef\u80fd\u53d1\u73b0\u4e86\uff0c\u7531\u4e8e \\(dp[i]\\) \u53ea\u4e0e \\(dp[i-1]\\) \u548c \\(dp[i-2]\\) \u6709\u5173\uff0c\u56e0\u6b64\u6211\u4eec\u65e0\u987b\u4f7f\u7528\u4e00\u4e2a\u6570\u7ec4 dp \u6765\u5b58\u50a8\u6240\u6709\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u800c\u53ea\u9700\u4e24\u4e2a\u53d8\u91cf\u6eda\u52a8\u524d\u8fdb\u5373\u53ef\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dp.py
        def climbing_stairs_dp_comp(n: int) -> int:\n    \"\"\"\u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n    if n == 1 or n == 2:\n        return n\n    a, b = 1, 2\n    for _ in range(3, n + 1):\n        a, b = b, a + b\n    return b\n
        climbing_stairs_dp.cpp
        /* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
        climbing_stairs_dp.java
        /* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
        climbing_stairs_dp.cs
        /* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint ClimbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
        climbing_stairs_dp.go
        /* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDPComp(n int) int {\n    if n == 1 || n == 2 {\n        return n\n    }\n    a, b := 1, 2\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i := 3; i <= n; i++ {\n        a, b = b, a+b\n    }\n    return b\n}\n
        climbing_stairs_dp.swift
        /* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDPComp(n: Int) -> Int {\n    if n == 1 || n == 2 {\n        return n\n    }\n    var a = 1\n    var b = 2\n    for _ in 3 ... n {\n        (a, b) = (b, a + b)\n    }\n    return b\n}\n
        climbing_stairs_dp.js
        /* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDPComp(n) {\n    if (n === 1 || n === 2) return n;\n    let a = 1,\n        b = 2;\n    for (let i = 3; i <= n; i++) {\n        const tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
        climbing_stairs_dp.ts
        /* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDPComp(n: number): number {\n    if (n === 1 || n === 2) return n;\n    let a = 1,\n        b = 2;\n    for (let i = 3; i <= n; i++) {\n        const tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
        climbing_stairs_dp.dart
        /* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\n  if (n == 1 || n == 2) return n;\n  int a = 1, b = 2;\n  for (int i = 3; i <= n; i++) {\n    int tmp = b;\n    b = a + b;\n    a = tmp;\n  }\n  return b;\n}\n
        climbing_stairs_dp.rs
        /* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn climbing_stairs_dp_comp(n: usize) -> i32 {\n    if n == 1 || n == 2 {\n        return n as i32;\n    }\n    let (mut a, mut b) = (1, 2);\n    for _ in 3..=n {\n        let tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    b\n}\n
        climbing_stairs_dp.c
        /* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
        climbing_stairs_dp.kt
        /* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfun climbingStairsDPComp(n: Int): Int {\n    if (n == 1 || n == 2) return n\n    var a = 1\n    var b = 2\n    for (i in 3..n) {\n        val temp = b\n        b += a\n        a = temp\n    }\n    return b\n}\n
        climbing_stairs_dp.rb
        ### \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 ###\ndef climbing_stairs_dp_comp(n)\n  return n if n == 1 || n == 2\n\n  a, b = 1, 2\n  (3...(n + 1)).each { a, b = b, a + b }\n\n  b\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u89c2\u5bdf\u4ee5\u4e0a\u4ee3\u7801\uff0c\u7531\u4e8e\u7701\u53bb\u4e86\u6570\u7ec4 dp \u5360\u7528\u7684\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n)\\) \u964d\u81f3 \\(O(1)\\) \u3002

        \u5728\u52a8\u6001\u89c4\u5212\u95ee\u9898\u4e2d\uff0c\u5f53\u524d\u72b6\u6001\u5f80\u5f80\u4ec5\u4e0e\u524d\u9762\u6709\u9650\u4e2a\u72b6\u6001\u6709\u5173\uff0c\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u53ea\u4fdd\u7559\u5fc5\u8981\u7684\u72b6\u6001\uff0c\u901a\u8fc7\u201c\u964d\u7ef4\u201d\u6765\u8282\u7701\u5185\u5b58\u7a7a\u95f4\u3002\u8fd9\u79cd\u7a7a\u95f4\u4f18\u5316\u6280\u5de7\u88ab\u79f0\u4e3a\u201c\u6eda\u52a8\u53d8\u91cf\u201d\u6216\u201c\u6eda\u52a8\u6570\u7ec4\u201d\u3002

        "},{"location":"chapter_dynamic_programming/knapsack_problem/","title":"14.4 \u00a0 0-1 \u80cc\u5305\u95ee\u9898","text":"

        \u80cc\u5305\u95ee\u9898\u662f\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u52a8\u6001\u89c4\u5212\u5165\u95e8\u9898\u76ee\uff0c\u662f\u52a8\u6001\u89c4\u5212\u4e2d\u6700\u5e38\u89c1\u7684\u95ee\u9898\u5f62\u5f0f\u3002\u5176\u5177\u6709\u5f88\u591a\u53d8\u79cd\uff0c\u4f8b\u5982 0-1 \u80cc\u5305\u95ee\u9898\u3001\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u3001\u591a\u91cd\u80cc\u5305\u95ee\u9898\u7b49\u3002

        \u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5148\u6765\u6c42\u89e3\u6700\u5e38\u89c1\u7684 0-1 \u80cc\u5305\u95ee\u9898\u3002

        Question

        \u7ed9\u5b9a \\(n\\) \u4e2a\u7269\u54c1\uff0c\u7b2c \\(i\\) \u4e2a\u7269\u54c1\u7684\u91cd\u91cf\u4e3a \\(wgt[i-1]\\)\u3001\u4ef7\u503c\u4e3a \\(val[i-1]\\) \uff0c\u548c\u4e00\u4e2a\u5bb9\u91cf\u4e3a \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u4e2a\u7269\u54c1\u53ea\u80fd\u9009\u62e9\u4e00\u6b21\uff0c\u95ee\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u80fd\u653e\u5165\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u3002

        \u89c2\u5bdf\u56fe 14-17 \uff0c\u7531\u4e8e\u7269\u54c1\u7f16\u53f7 \\(i\\) \u4ece \\(1\\) \u5f00\u59cb\u8ba1\u6570\uff0c\u6570\u7ec4\u7d22\u5f15\u4ece \\(0\\) \u5f00\u59cb\u8ba1\u6570\uff0c\u56e0\u6b64\u7269\u54c1 \\(i\\) \u5bf9\u5e94\u91cd\u91cf \\(wgt[i-1]\\) \u548c\u4ef7\u503c \\(val[i-1]\\) \u3002

        \u56fe 14-17 \u00a0 0-1 \u80cc\u5305\u7684\u793a\u4f8b\u6570\u636e

        \u6211\u4eec\u53ef\u4ee5\u5c06 0-1 \u80cc\u5305\u95ee\u9898\u770b\u4f5c\u4e00\u4e2a\u7531 \\(n\\) \u8f6e\u51b3\u7b56\u7ec4\u6210\u7684\u8fc7\u7a0b\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u7269\u4f53\u90fd\u6709\u4e0d\u653e\u5165\u548c\u653e\u5165\u4e24\u79cd\u51b3\u7b56\uff0c\u56e0\u6b64\u8be5\u95ee\u9898\u6ee1\u8db3\u51b3\u7b56\u6811\u6a21\u578b\u3002

        \u8be5\u95ee\u9898\u7684\u76ee\u6807\u662f\u6c42\u89e3\u201c\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u80fd\u653e\u5165\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u201d\uff0c\u56e0\u6b64\u8f83\u5927\u6982\u7387\u662f\u4e00\u4e2a\u52a8\u6001\u89c4\u5212\u95ee\u9898\u3002

        \u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868

        \u5bf9\u4e8e\u6bcf\u4e2a\u7269\u54c1\u6765\u8bf4\uff0c\u4e0d\u653e\u5165\u80cc\u5305\uff0c\u80cc\u5305\u5bb9\u91cf\u4e0d\u53d8\uff1b\u653e\u5165\u80cc\u5305\uff0c\u80cc\u5305\u5bb9\u91cf\u51cf\u5c0f\u3002\u7531\u6b64\u53ef\u5f97\u72b6\u6001\u5b9a\u4e49\uff1a\u5f53\u524d\u7269\u54c1\u7f16\u53f7 \\(i\\) \u548c\u80cc\u5305\u5bb9\u91cf \\(c\\) \uff0c\u8bb0\u4e3a \\([i, c]\\) \u3002

        \u72b6\u6001 \\([i, c]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u4e3a\uff1a\u524d \\(i\\) \u4e2a\u7269\u54c1\u5728\u5bb9\u91cf\u4e3a \\(c\\) \u7684\u80cc\u5305\u4e2d\u7684\u6700\u5927\u4ef7\u503c\uff0c\u8bb0\u4e3a \\(dp[i, c]\\) \u3002

        \u5f85\u6c42\u89e3\u7684\u662f \\(dp[n, cap]\\) \uff0c\u56e0\u6b64\u9700\u8981\u4e00\u4e2a\u5c3a\u5bf8\u4e3a \\((n+1) \\times (cap+1)\\) \u7684\u4e8c\u7ef4 \\(dp\\) \u8868\u3002

        \u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b

        \u5f53\u6211\u4eec\u505a\u51fa\u7269\u54c1 \\(i\\) \u7684\u51b3\u7b56\u540e\uff0c\u5269\u4f59\u7684\u662f\u524d \\(i-1\\) \u4e2a\u7269\u54c1\u51b3\u7b56\u7684\u5b50\u95ee\u9898\uff0c\u53ef\u5206\u4e3a\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\u3002

        • \u4e0d\u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u80cc\u5305\u5bb9\u91cf\u4e0d\u53d8\uff0c\u72b6\u6001\u53d8\u5316\u4e3a \\([i-1, c]\\) \u3002
        • \u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u80cc\u5305\u5bb9\u91cf\u51cf\u5c11 \\(wgt[i-1]\\) \uff0c\u4ef7\u503c\u589e\u52a0 \\(val[i-1]\\) \uff0c\u72b6\u6001\u53d8\u5316\u4e3a \\([i-1, c-wgt[i-1]]\\) \u3002

        \u4e0a\u8ff0\u5206\u6790\u5411\u6211\u4eec\u63ed\u793a\u4e86\u672c\u9898\u7684\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\u6700\u5927\u4ef7\u503c \\(dp[i, c]\\) \u7b49\u4e8e\u4e0d\u653e\u5165\u7269\u54c1 \\(i\\) \u548c\u653e\u5165\u7269\u54c1 \\(i\\) \u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\u3002\u7531\u6b64\u53ef\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff1a

        \\[ dp[i, c] = \\max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1]) \\]

        \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u82e5\u5f53\u524d\u7269\u54c1\u91cd\u91cf \\(wgt[i - 1]\\) \u8d85\u51fa\u5269\u4f59\u80cc\u5305\u5bb9\u91cf \\(c\\) \uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\u3002

        \u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f

        \u5f53\u65e0\u7269\u54c1\u6216\u80cc\u5305\u5bb9\u91cf\u4e3a \\(0\\) \u65f6\u6700\u5927\u4ef7\u503c\u4e3a \\(0\\) \uff0c\u5373\u9996\u5217 \\(dp[i, 0]\\) \u548c\u9996\u884c \\(dp[0, c]\\) \u90fd\u7b49\u4e8e \\(0\\) \u3002

        \u5f53\u524d\u72b6\u6001 \\([i, c]\\) \u4ece\u4e0a\u65b9\u7684\u72b6\u6001 \\([i-1, c]\\) \u548c\u5de6\u4e0a\u65b9\u7684\u72b6\u6001 \\([i-1, c-wgt[i-1]]\\) \u8f6c\u79fb\u800c\u6765\uff0c\u56e0\u6b64\u901a\u8fc7\u4e24\u5c42\u5faa\u73af\u6b63\u5e8f\u904d\u5386\u6574\u4e2a \\(dp\\) \u8868\u5373\u53ef\u3002

        \u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u6211\u4eec\u63a5\u4e0b\u6765\u6309\u987a\u5e8f\u5b9e\u73b0\u66b4\u529b\u641c\u7d22\u3001\u8bb0\u5fc6\u5316\u641c\u7d22\u3001\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u3002

        "},{"location":"chapter_dynamic_programming/knapsack_problem/#1","title":"1. \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u7d22","text":"

        \u641c\u7d22\u4ee3\u7801\u5305\u542b\u4ee5\u4e0b\u8981\u7d20\u3002

        • \u9012\u5f52\u53c2\u6570\uff1a\u72b6\u6001 \\([i, c]\\) \u3002
        • \u8fd4\u56de\u503c\uff1a\u5b50\u95ee\u9898\u7684\u89e3 \\(dp[i, c]\\) \u3002
        • \u7ec8\u6b62\u6761\u4ef6\uff1a\u5f53\u7269\u54c1\u7f16\u53f7\u8d8a\u754c \\(i = 0\\) \u6216\u80cc\u5305\u5269\u4f59\u5bb9\u91cf\u4e3a \\(0\\) \u65f6\uff0c\u7ec8\u6b62\u9012\u5f52\u5e76\u8fd4\u56de\u4ef7\u503c \\(0\\) \u3002
        • \u526a\u679d\uff1a\u82e5\u5f53\u524d\u7269\u54c1\u91cd\u91cf\u8d85\u51fa\u80cc\u5305\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
        def knapsack_dfs(wgt: list[int], val: list[int], i: int, c: int) -> int:\n    \"\"\"0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22\"\"\"\n    # \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if i == 0 or c == 0:\n        return 0\n    # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if wgt[i - 1] > c:\n        return knapsack_dfs(wgt, val, i - 1, c)\n    # \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    no = knapsack_dfs(wgt, val, i - 1, c)\n    yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]\n    # \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    return max(no, yes)\n
        knapsack.cpp
        /* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(vector<int> &wgt, vector<int> &val, int i, int c) {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    int no = knapsackDFS(wgt, val, i - 1, c);\n    int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    return max(no, yes);\n}\n
        knapsack.java
        /* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(int[] wgt, int[] val, int i, int c) {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    int no = knapsackDFS(wgt, val, i - 1, c);\n    int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    return Math.max(no, yes);\n}\n
        knapsack.cs
        /* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint KnapsackDFS(int[] weight, int[] val, int i, int c) {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (weight[i - 1] > c) {\n        return KnapsackDFS(weight, val, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    int no = KnapsackDFS(weight, val, i - 1, c);\n    int yes = KnapsackDFS(weight, val, i - 1, c - weight[i - 1]) + val[i - 1];\n    // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    return Math.Max(no, yes);\n}\n
        knapsack.go
        /* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc knapsackDFS(wgt, val []int, i, c int) int {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if i == 0 || c == 0 {\n        return 0\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if wgt[i-1] > c {\n        return knapsackDFS(wgt, val, i-1, c)\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    no := knapsackDFS(wgt, val, i-1, c)\n    yes := knapsackDFS(wgt, val, i-1, c-wgt[i-1]) + val[i-1]\n    // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    return int(math.Max(float64(no), float64(yes)))\n}\n
        knapsack.swift
        /* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc knapsackDFS(wgt: [Int], val: [Int], i: Int, c: Int) -> Int {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if i == 0 || c == 0 {\n        return 0\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if wgt[i - 1] > c {\n        return knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    let no = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)\n    let yes = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]\n    // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    return max(no, yes)\n}\n
        knapsack.js
        /* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction knapsackDFS(wgt, val, i, c) {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i === 0 || c === 0) {\n        return 0;\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    const no = knapsackDFS(wgt, val, i - 1, c);\n    const yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    return Math.max(no, yes);\n}\n
        knapsack.ts
        /* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction knapsackDFS(\n    wgt: Array<number>,\n    val: Array<number>,\n    i: number,\n    c: number\n): number {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i === 0 || c === 0) {\n        return 0;\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    const no = knapsackDFS(wgt, val, i - 1, c);\n    const yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    return Math.max(no, yes);\n}\n
        knapsack.dart
        /* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(List<int> wgt, List<int> val, int i, int c) {\n  // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n  if (i == 0 || c == 0) {\n    return 0;\n  }\n  // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n  if (wgt[i - 1] > c) {\n    return knapsackDFS(wgt, val, i - 1, c);\n  }\n  // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n  int no = knapsackDFS(wgt, val, i - 1, c);\n  int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n  // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n  return max(no, yes);\n}\n
        knapsack.rs
        /* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfn knapsack_dfs(wgt: &[i32], val: &[i32], i: usize, c: usize) -> i32 {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if i == 0 || c == 0 {\n        return 0;\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if wgt[i - 1] > c as i32 {\n        return knapsack_dfs(wgt, val, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    let no = knapsack_dfs(wgt, val, i - 1, c);\n    let yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1] as usize) + val[i - 1];\n    // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    std::cmp::max(no, yes)\n}\n
        knapsack.c
        /* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(int wgt[], int val[], int i, int c) {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    int no = knapsackDFS(wgt, val, i - 1, c);\n    int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    return myMax(no, yes);\n}\n
        knapsack.kt
        /* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfun knapsackDFS(\n    wgt: IntArray,\n    _val: IntArray,\n    i: Int,\n    c: Int\n): Int {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i == 0 || c == 0) {\n        return 0\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, _val, i - 1, c)\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    val no = knapsackDFS(wgt, _val, i - 1, c)\n    val yes = knapsackDFS(wgt, _val, i - 1, c - wgt[i - 1]) + _val[i - 1]\n    // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    return max(no, yes)\n}\n
        knapsack.rb
        ### 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 ###\ndef knapsack_dfs(wgt, val, i, c)\n  # \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n  return 0 if i == 0 || c == 0\n  # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n  return knapsack_dfs(wgt, val, i - 1, c) if wgt[i - 1] > c\n  # \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n  no = knapsack_dfs(wgt, val, i - 1, c)\n  yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]\n  # \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n  [no, yes].max\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5982\u56fe 14-18 \u6240\u793a\uff0c\u7531\u4e8e\u6bcf\u4e2a\u7269\u54c1\u90fd\u4f1a\u4ea7\u751f\u4e0d\u9009\u548c\u9009\u4e24\u6761\u641c\u7d22\u5206\u652f\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002

        \u89c2\u5bdf\u9012\u5f52\u6811\uff0c\u5bb9\u6613\u53d1\u73b0\u5176\u4e2d\u5b58\u5728\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u4f8b\u5982 \\(dp[1, 10]\\) \u7b49\u3002\u800c\u5f53\u7269\u54c1\u8f83\u591a\u3001\u80cc\u5305\u5bb9\u91cf\u8f83\u5927\uff0c\u5c24\u5176\u662f\u76f8\u540c\u91cd\u91cf\u7684\u7269\u54c1\u8f83\u591a\u65f6\uff0c\u91cd\u53e0\u5b50\u95ee\u9898\u7684\u6570\u91cf\u5c06\u4f1a\u5927\u5e45\u589e\u591a\u3002

        \u56fe 14-18 \u00a0 0-1 \u80cc\u5305\u95ee\u9898\u7684\u66b4\u529b\u641c\u7d22\u9012\u5f52\u6811

        "},{"location":"chapter_dynamic_programming/knapsack_problem/#2","title":"2. \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22","text":"

        \u4e3a\u4e86\u4fdd\u8bc1\u91cd\u53e0\u5b50\u95ee\u9898\u53ea\u88ab\u8ba1\u7b97\u4e00\u6b21\uff0c\u6211\u4eec\u501f\u52a9\u8bb0\u5fc6\u5217\u8868 mem \u6765\u8bb0\u5f55\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5176\u4e2d mem[i][c] \u5bf9\u5e94 \\(dp[i, c]\\) \u3002

        \u5f15\u5165\u8bb0\u5fc6\u5316\u4e4b\u540e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u5b50\u95ee\u9898\u6570\u91cf\uff0c\u4e5f\u5c31\u662f \\(O(n \\times cap)\\) \u3002\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
        def knapsack_dfs_mem(\n    wgt: list[int], val: list[int], mem: list[list[int]], i: int, c: int\n) -> int:\n    \"\"\"0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n    # \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if i == 0 or c == 0:\n        return 0\n    # \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if mem[i][c] != -1:\n        return mem[i][c]\n    # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if wgt[i - 1] > c:\n        return knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n    # \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    no = knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n    yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1]\n    # \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    mem[i][c] = max(no, yes)\n    return mem[i][c]\n
        knapsack.cpp
        /* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(vector<int> &wgt, vector<int> &val, vector<vector<int>> &mem, int i, int c) {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][c] != -1) {\n        return mem[i][c];\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, mem, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n    int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    mem[i][c] = max(no, yes);\n    return mem[i][c];\n}\n
        knapsack.java
        /* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(int[] wgt, int[] val, int[][] mem, int i, int c) {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][c] != -1) {\n        return mem[i][c];\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, mem, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n    int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    mem[i][c] = Math.max(no, yes);\n    return mem[i][c];\n}\n
        knapsack.cs
        /* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint KnapsackDFSMem(int[] weight, int[] val, int[][] mem, int i, int c) {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][c] != -1) {\n        return mem[i][c];\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (weight[i - 1] > c) {\n        return KnapsackDFSMem(weight, val, mem, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    int no = KnapsackDFSMem(weight, val, mem, i - 1, c);\n    int yes = KnapsackDFSMem(weight, val, mem, i - 1, c - weight[i - 1]) + val[i - 1];\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    mem[i][c] = Math.Max(no, yes);\n    return mem[i][c];\n}\n
        knapsack.go
        /* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc knapsackDFSMem(wgt, val []int, mem [][]int, i, c int) int {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if i == 0 || c == 0 {\n        return 0\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if mem[i][c] != -1 {\n        return mem[i][c]\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if wgt[i-1] > c {\n        return knapsackDFSMem(wgt, val, mem, i-1, c)\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    no := knapsackDFSMem(wgt, val, mem, i-1, c)\n    yes := knapsackDFSMem(wgt, val, mem, i-1, c-wgt[i-1]) + val[i-1]\n    // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    mem[i][c] = int(math.Max(float64(no), float64(yes)))\n    return mem[i][c]\n}\n
        knapsack.swift
        /* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc knapsackDFSMem(wgt: [Int], val: [Int], mem: inout [[Int]], i: Int, c: Int) -> Int {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if i == 0 || c == 0 {\n        return 0\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if mem[i][c] != -1 {\n        return mem[i][c]\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if wgt[i - 1] > c {\n        return knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c)\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    let no = knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c)\n    let yes = knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    mem[i][c] = max(no, yes)\n    return mem[i][c]\n}\n
        knapsack.js
        /* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction knapsackDFSMem(wgt, val, mem, i, c) {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i === 0 || c === 0) {\n        return 0;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][c] !== -1) {\n        return mem[i][c];\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, mem, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    const no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n    const yes =\n        knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    mem[i][c] = Math.max(no, yes);\n    return mem[i][c];\n}\n
        knapsack.ts
        /* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction knapsackDFSMem(\n    wgt: Array<number>,\n    val: Array<number>,\n    mem: Array<Array<number>>,\n    i: number,\n    c: number\n): number {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i === 0 || c === 0) {\n        return 0;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][c] !== -1) {\n        return mem[i][c];\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, mem, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    const no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n    const yes =\n        knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    mem[i][c] = Math.max(no, yes);\n    return mem[i][c];\n}\n
        knapsack.dart
        /* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(\n  List<int> wgt,\n  List<int> val,\n  List<List<int>> mem,\n  int i,\n  int c,\n) {\n  // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n  if (i == 0 || c == 0) {\n    return 0;\n  }\n  // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n  if (mem[i][c] != -1) {\n    return mem[i][c];\n  }\n  // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n  if (wgt[i - 1] > c) {\n    return knapsackDFSMem(wgt, val, mem, i - 1, c);\n  }\n  // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n  int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n  int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n  // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n  mem[i][c] = max(no, yes);\n  return mem[i][c];\n}\n
        knapsack.rs
        /* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn knapsack_dfs_mem(wgt: &[i32], val: &[i32], mem: &mut Vec<Vec<i32>>, i: usize, c: usize) -> i32 {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if i == 0 || c == 0 {\n        return 0;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if mem[i][c] != -1 {\n        return mem[i][c];\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if wgt[i - 1] > c as i32 {\n        return knapsack_dfs_mem(wgt, val, mem, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    let no = knapsack_dfs_mem(wgt, val, mem, i - 1, c);\n    let yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1] as usize) + val[i - 1];\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    mem[i][c] = std::cmp::max(no, yes);\n    mem[i][c]\n}\n
        knapsack.c
        /* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(int wgt[], int val[], int memCols, int **mem, int i, int c) {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][c] != -1) {\n        return mem[i][c];\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, memCols, mem, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    int no = knapsackDFSMem(wgt, val, memCols, mem, i - 1, c);\n    int yes = knapsackDFSMem(wgt, val, memCols, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    mem[i][c] = myMax(no, yes);\n    return mem[i][c];\n}\n
        knapsack.kt
        /* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfun knapsackDFSMem(\n    wgt: IntArray,\n    _val: IntArray,\n    mem: Array<IntArray>,\n    i: Int,\n    c: Int\n): Int {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i == 0 || c == 0) {\n        return 0\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][c] != -1) {\n        return mem[i][c]\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, _val, mem, i - 1, c)\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    val no = knapsackDFSMem(wgt, _val, mem, i - 1, c)\n    val yes = knapsackDFSMem(wgt, _val, mem, i - 1, c - wgt[i - 1]) + _val[i - 1]\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    mem[i][c] = max(no, yes)\n    return mem[i][c]\n}\n
        knapsack.rb
        ### 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 ###\ndef knapsack_dfs_mem(wgt, val, mem, i, c)\n  # \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n  return 0 if i == 0 || c == 0\n  # \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n  return mem[i][c] if mem[i][c] != -1\n  # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n  return knapsack_dfs_mem(wgt, val, mem, i - 1, c) if wgt[i - 1] > c\n  # \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n  no = knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n  yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1]\n  # \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n  mem[i][c] = [no, yes].max\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 14-19 \u5c55\u793a\u4e86\u5728\u8bb0\u5fc6\u5316\u641c\u7d22\u4e2d\u88ab\u526a\u6389\u7684\u641c\u7d22\u5206\u652f\u3002

        \u56fe 14-19 \u00a0 0-1 \u80cc\u5305\u95ee\u9898\u7684\u8bb0\u5fc6\u5316\u641c\u7d22\u9012\u5f52\u6811

        "},{"location":"chapter_dynamic_programming/knapsack_problem/#3","title":"3. \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52a8\u6001\u89c4\u5212","text":"

        \u52a8\u6001\u89c4\u5212\u5b9e\u8d28\u4e0a\u5c31\u662f\u5728\u72b6\u6001\u8f6c\u79fb\u4e2d\u586b\u5145 \\(dp\\) \u8868\u7684\u8fc7\u7a0b\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
        def knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n    n = len(wgt)\n    # \u521d\u59cb\u5316 dp \u8868\n    dp = [[0] * (cap + 1) for _ in range(n + 1)]\n    # \u72b6\u6001\u8f6c\u79fb\n    for i in range(1, n + 1):\n        for c in range(1, cap + 1):\n            if wgt[i - 1] > c:\n                # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c]\n            else:\n                # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\n    return dp[n][cap]\n
        knapsack.cpp
        /* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // \u521d\u59cb\u5316 dp \u8868\n    vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        knapsack.java
        /* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[][] dp = new int[n + 1][cap + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = Math.max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        knapsack.cs
        /* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint KnapsackDP(int[] weight, int[] val, int cap) {\n    int n = weight.Length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[,] dp = new int[n + 1, cap + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (weight[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i, c] = dp[i - 1, c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i, c] = Math.Max(dp[i - 1, c - weight[i - 1]] + val[i - 1], dp[i - 1, c]);\n            }\n        }\n    }\n    return dp[n, cap];\n}\n
        knapsack.go
        /* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDP(wgt, val []int, cap int) int {\n    n := len(wgt)\n    // \u521d\u59cb\u5316 dp \u8868\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, cap+1)\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for i := 1; i <= n; i++ {\n        for c := 1; c <= cap; c++ {\n            if wgt[i-1] > c {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i-1][c]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i-1][c-wgt[i-1]]+val[i-1])))\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
        knapsack.swift
        /* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDP(wgt: [Int], val: [Int], cap: Int) -> Int {\n    let n = wgt.count\n    // \u521d\u59cb\u5316 dp \u8868\n    var dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1 ... n {\n        for c in 1 ... cap {\n            if wgt[i - 1] > c {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
        knapsack.js
        /* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDP(wgt, val, cap) {\n    const n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array(n + 1)\n        .fill(0)\n        .map(() => Array(cap + 1).fill(0));\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = Math.max(\n                    dp[i - 1][c],\n                    dp[i - 1][c - wgt[i - 1]] + val[i - 1]\n                );\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        knapsack.ts
        /* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDP(\n    wgt: Array<number>,\n    val: Array<number>,\n    cap: number\n): number {\n    const n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: cap + 1 }, () => 0)\n    );\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = Math.max(\n                    dp[i - 1][c],\n                    dp[i - 1][c - wgt[i - 1]] + val[i - 1]\n                );\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        knapsack.dart
        /* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(List<int> wgt, List<int> val, int cap) {\n  int n = wgt.length;\n  // \u521d\u59cb\u5316 dp \u8868\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(cap + 1, 0));\n  // \u72b6\u6001\u8f6c\u79fb\n  for (int i = 1; i <= n; i++) {\n    for (int c = 1; c <= cap; c++) {\n      if (wgt[i - 1] > c) {\n        // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n        dp[i][c] = dp[i - 1][c];\n      } else {\n        // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n        dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n      }\n    }\n  }\n  return dp[n][cap];\n}\n
        knapsack.rs
        /* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfn knapsack_dp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n    let n = wgt.len();\n    // \u521d\u59cb\u5316 dp \u8868\n    let mut dp = vec![vec![0; cap + 1]; n + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1..=n {\n        for c in 1..=cap {\n            if wgt[i - 1] > c as i32 {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = std::cmp::max(\n                    dp[i - 1][c],\n                    dp[i - 1][c - wgt[i - 1] as usize] + val[i - 1],\n                );\n            }\n        }\n    }\n    dp[n][cap]\n}\n
        knapsack.c
        /* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(int wgt[], int val[], int cap, int wgtSize) {\n    int n = wgtSize;\n    // \u521d\u59cb\u5316 dp \u8868\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(cap + 1, sizeof(int));\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = myMax(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    int res = dp[n][cap];\n    // \u91ca\u653e\u5185\u5b58\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    return res;\n}\n
        knapsack.kt
        /* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfun knapsackDP(wgt: IntArray, _val: IntArray, cap: Int): Int {\n    val n = wgt.size\n    // \u521d\u59cb\u5316 dp \u8868\n    val dp = Array(n + 1) { IntArray(cap + 1) }\n    // \u72b6\u6001\u8f6c\u79fb\n    for (i in 1..n) {\n        for (c in 1..cap) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + _val[i - 1])\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
        knapsack.rb
        ### 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 ###\ndef knapsack_dp(wgt, val, cap)\n  n = wgt.length\n  # \u521d\u59cb\u5316 dp \u8868\n  dp = Array.new(n + 1) { Array.new(cap + 1, 0) }\n  # \u72b6\u6001\u8f6c\u79fb\n  for i in 1...(n + 1)\n    for c in 1...(cap + 1)\n      if wgt[i - 1] > c\n        # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n        dp[i][c] = dp[i - 1][c]\n      else\n        # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n        dp[i][c] = [dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[n][cap]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5982\u56fe 14-20 \u6240\u793a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u90fd\u7531\u6570\u7ec4 dp \u5927\u5c0f\u51b3\u5b9a\uff0c\u5373 \\(O(n \\times cap)\\) \u3002

        <1><2><3><4><5><6><7><8><9><10><11><12><13><14>

        \u56fe 14-20 \u00a0 0-1 \u80cc\u5305\u95ee\u9898\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

        "},{"location":"chapter_dynamic_programming/knapsack_problem/#4","title":"4. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

        \u7531\u4e8e\u6bcf\u4e2a\u72b6\u6001\u90fd\u53ea\u4e0e\u5176\u4e0a\u4e00\u884c\u7684\u72b6\u6001\u6709\u5173\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e24\u4e2a\u6570\u7ec4\u6eda\u52a8\u524d\u8fdb\uff0c\u5c06\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n^2)\\) \u964d\u81f3 \\(O(n)\\) \u3002

        \u8fdb\u4e00\u6b65\u601d\u8003\uff0c\u6211\u4eec\u80fd\u5426\u4ec5\u7528\u4e00\u4e2a\u6570\u7ec4\u5b9e\u73b0\u7a7a\u95f4\u4f18\u5316\u5462\uff1f\u89c2\u5bdf\u53ef\u77e5\uff0c\u6bcf\u4e2a\u72b6\u6001\u90fd\u662f\u7531\u6b63\u4e0a\u65b9\u6216\u5de6\u4e0a\u65b9\u7684\u683c\u5b50\u8f6c\u79fb\u8fc7\u6765\u7684\u3002\u5047\u8bbe\u53ea\u6709\u4e00\u4e2a\u6570\u7ec4\uff0c\u5f53\u5f00\u59cb\u904d\u5386\u7b2c \\(i\\) \u884c\u65f6\uff0c\u8be5\u6570\u7ec4\u5b58\u50a8\u7684\u4ecd\u7136\u662f\u7b2c \\(i-1\\) \u884c\u7684\u72b6\u6001\u3002

        • \u5982\u679c\u91c7\u53d6\u6b63\u5e8f\u904d\u5386\uff0c\u90a3\u4e48\u904d\u5386\u5230 \\(dp[i, j]\\) \u65f6\uff0c\u5de6\u4e0a\u65b9 \\(dp[i-1, 1]\\) ~ \\(dp[i-1, j-1]\\) \u503c\u53ef\u80fd\u5df2\u7ecf\u88ab\u8986\u76d6\uff0c\u6b64\u65f6\u5c31\u65e0\u6cd5\u5f97\u5230\u6b63\u786e\u7684\u72b6\u6001\u8f6c\u79fb\u7ed3\u679c\u3002
        • \u5982\u679c\u91c7\u53d6\u5012\u5e8f\u904d\u5386\uff0c\u5219\u4e0d\u4f1a\u53d1\u751f\u8986\u76d6\u95ee\u9898\uff0c\u72b6\u6001\u8f6c\u79fb\u53ef\u4ee5\u6b63\u786e\u8fdb\u884c\u3002

        \u56fe 14-21 \u5c55\u793a\u4e86\u5728\u5355\u4e2a\u6570\u7ec4\u4e0b\u4ece\u7b2c \\(i = 1\\) \u884c\u8f6c\u6362\u81f3\u7b2c \\(i = 2\\) \u884c\u7684\u8fc7\u7a0b\u3002\u8bf7\u601d\u8003\u6b63\u5e8f\u904d\u5386\u548c\u5012\u5e8f\u904d\u5386\u7684\u533a\u522b\u3002

        <1><2><3><4><5><6>

        \u56fe 14-21 \u00a0 0-1 \u80cc\u5305\u7684\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

        \u5728\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u4ec5\u9700\u5c06\u6570\u7ec4 dp \u7684\u7b2c\u4e00\u7ef4 \\(i\\) \u76f4\u63a5\u5220\u9664\uff0c\u5e76\u4e14\u628a\u5185\u5faa\u73af\u66f4\u6539\u4e3a\u5012\u5e8f\u904d\u5386\u5373\u53ef\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
        def knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n    n = len(wgt)\n    # \u521d\u59cb\u5316 dp \u8868\n    dp = [0] * (cap + 1)\n    # \u72b6\u6001\u8f6c\u79fb\n    for i in range(1, n + 1):\n        # \u5012\u5e8f\u904d\u5386\n        for c in range(cap, 0, -1):\n            if wgt[i - 1] > c:\n                # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c]\n            else:\n                # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n    return dp[cap]\n
        knapsack.cpp
        /* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // \u521d\u59cb\u5316 dp \u8868\n    vector<int> dp(cap + 1, 0);\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        // \u5012\u5e8f\u904d\u5386\n        for (int c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        knapsack.java
        /* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[] dp = new int[cap + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        // \u5012\u5e8f\u904d\u5386\n        for (int c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        knapsack.cs
        /* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint KnapsackDPComp(int[] weight, int[] val, int cap) {\n    int n = weight.Length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[] dp = new int[cap + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        // \u5012\u5e8f\u904d\u5386\n        for (int c = cap; c > 0; c--) {\n            if (weight[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = Math.Max(dp[c], dp[c - weight[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        knapsack.go
        /* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDPComp(wgt, val []int, cap int) int {\n    n := len(wgt)\n    // \u521d\u59cb\u5316 dp \u8868\n    dp := make([]int, cap+1)\n    // \u72b6\u6001\u8f6c\u79fb\n    for i := 1; i <= n; i++ {\n        // \u5012\u5e8f\u904d\u5386\n        for c := cap; c >= 1; c-- {\n            if wgt[i-1] <= c {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))\n            }\n        }\n    }\n    return dp[cap]\n}\n
        knapsack.swift
        /* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDPComp(wgt: [Int], val: [Int], cap: Int) -> Int {\n    let n = wgt.count\n    // \u521d\u59cb\u5316 dp \u8868\n    var dp = Array(repeating: 0, count: cap + 1)\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1 ... n {\n        // \u5012\u5e8f\u904d\u5386\n        for c in (1 ... cap).reversed() {\n            if wgt[i - 1] <= c {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n            }\n        }\n    }\n    return dp[cap]\n}\n
        knapsack.js
        /* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDPComp(wgt, val, cap) {\n    const n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array(cap + 1).fill(0);\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        // \u5012\u5e8f\u904d\u5386\n        for (let c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        knapsack.ts
        /* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDPComp(\n    wgt: Array<number>,\n    val: Array<number>,\n    cap: number\n): number {\n    const n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array(cap + 1).fill(0);\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        // \u5012\u5e8f\u904d\u5386\n        for (let c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        knapsack.dart
        /* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(List<int> wgt, List<int> val, int cap) {\n  int n = wgt.length;\n  // \u521d\u59cb\u5316 dp \u8868\n  List<int> dp = List.filled(cap + 1, 0);\n  // \u72b6\u6001\u8f6c\u79fb\n  for (int i = 1; i <= n; i++) {\n    // \u5012\u5e8f\u904d\u5386\n    for (int c = cap; c >= 1; c--) {\n      if (wgt[i - 1] <= c) {\n        // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n        dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n      }\n    }\n  }\n  return dp[cap];\n}\n
        knapsack.rs
        /* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn knapsack_dp_comp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n    let n = wgt.len();\n    // \u521d\u59cb\u5316 dp \u8868\n    let mut dp = vec![0; cap + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1..=n {\n        // \u5012\u5e8f\u904d\u5386\n        for c in (1..=cap).rev() {\n            if wgt[i - 1] <= c as i32 {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);\n            }\n        }\n    }\n    dp[cap]\n}\n
        knapsack.c
        /* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(int wgt[], int val[], int cap, int wgtSize) {\n    int n = wgtSize;\n    // \u521d\u59cb\u5316 dp \u8868\n    int *dp = calloc(cap + 1, sizeof(int));\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        // \u5012\u5e8f\u904d\u5386\n        for (int c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = myMax(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    int res = dp[cap];\n    // \u91ca\u653e\u5185\u5b58\n    free(dp);\n    return res;\n}\n
        knapsack.kt
        /* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfun knapsackDPComp(wgt: IntArray, _val: IntArray, cap: Int): Int {\n    val n = wgt.size\n    // \u521d\u59cb\u5316 dp \u8868\n    val dp = IntArray(cap + 1)\n    // \u72b6\u6001\u8f6c\u79fb\n    for (i in 1..n) {\n        // \u5012\u5e8f\u904d\u5386\n        for (c in cap downTo 1) {\n            if (wgt[i - 1] <= c) {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])\n            }\n        }\n    }\n    return dp[cap]\n}\n
        knapsack.rb
        ### 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 ###\ndef knapsack_dp_comp(wgt, val, cap)\n  n = wgt.length\n  # \u521d\u59cb\u5316 dp \u8868\n  dp = Array.new(cap + 1, 0)\n  # \u72b6\u6001\u8f6c\u79fb\n  for i in 1...(n + 1)\n    # \u5012\u5e8f\u904d\u5386\n    for c in cap.downto(1)\n      if wgt[i - 1] > c\n        # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n        dp[c] = dp[c]\n      else\n        # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n        dp[c] = [dp[c], dp[c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[cap]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_dynamic_programming/summary/","title":"14.7 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_dynamic_programming/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u52a8\u6001\u89c4\u5212\u5bf9\u95ee\u9898\u8fdb\u884c\u5206\u89e3\uff0c\u5e76\u901a\u8fc7\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\u6765\u89c4\u907f\u91cd\u590d\u8ba1\u7b97\uff0c\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002
        • \u4e0d\u8003\u8651\u65f6\u95f4\u7684\u524d\u63d0\u4e0b\uff0c\u6240\u6709\u52a8\u6001\u89c4\u5212\u95ee\u9898\u90fd\u53ef\u4ee5\u7528\u56de\u6eaf\uff08\u66b4\u529b\u641c\u7d22\uff09\u8fdb\u884c\u6c42\u89e3\uff0c\u4f46\u9012\u5f52\u6811\u4e2d\u5b58\u5728\u5927\u91cf\u7684\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u6548\u7387\u6781\u4f4e\u3002\u901a\u8fc7\u5f15\u5165\u8bb0\u5fc6\u5316\u5217\u8868\uff0c\u53ef\u4ee5\u5b58\u50a8\u6240\u6709\u8ba1\u7b97\u8fc7\u7684\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u4ece\u800c\u4fdd\u8bc1\u91cd\u53e0\u5b50\u95ee\u9898\u53ea\u88ab\u8ba1\u7b97\u4e00\u6b21\u3002
        • \u8bb0\u5fc6\u5316\u641c\u7d22\u662f\u4e00\u79cd\u4ece\u9876\u81f3\u5e95\u7684\u9012\u5f52\u5f0f\u89e3\u6cd5\uff0c\u800c\u4e0e\u4e4b\u5bf9\u5e94\u7684\u52a8\u6001\u89c4\u5212\u662f\u4e00\u79cd\u4ece\u5e95\u81f3\u9876\u7684\u9012\u63a8\u5f0f\u89e3\u6cd5\uff0c\u5176\u5982\u540c\u201c\u586b\u5199\u8868\u683c\u201d\u4e00\u6837\u3002\u7531\u4e8e\u5f53\u524d\u72b6\u6001\u4ec5\u4f9d\u8d56\u67d0\u4e9b\u5c40\u90e8\u72b6\u6001\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u6d88\u9664 \\(dp\\) \u8868\u7684\u4e00\u4e2a\u7ef4\u5ea6\uff0c\u4ece\u800c\u964d\u4f4e\u7a7a\u95f4\u590d\u6742\u5ea6\u3002
        • \u5b50\u95ee\u9898\u5206\u89e3\u662f\u4e00\u79cd\u901a\u7528\u7684\u7b97\u6cd5\u601d\u8def\uff0c\u5728\u5206\u6cbb\u3001\u52a8\u6001\u89c4\u5212\u3001\u56de\u6eaf\u4e2d\u5177\u6709\u4e0d\u540c\u7684\u6027\u8d28\u3002
        • \u52a8\u6001\u89c4\u5212\u95ee\u9898\u6709\u4e09\u5927\u7279\u6027\uff1a\u91cd\u53e0\u5b50\u95ee\u9898\u3001\u6700\u4f18\u5b50\u7ed3\u6784\u3001\u65e0\u540e\u6548\u6027\u3002
        • \u5982\u679c\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u53ef\u4ee5\u4ece\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u6784\u5efa\u5f97\u6765\uff0c\u5219\u5b83\u5c31\u5177\u6709\u6700\u4f18\u5b50\u7ed3\u6784\u3002
        • \u65e0\u540e\u6548\u6027\u6307\u5bf9\u4e8e\u4e00\u4e2a\u72b6\u6001\uff0c\u5176\u672a\u6765\u53d1\u5c55\u53ea\u4e0e\u8be5\u72b6\u6001\u6709\u5173\uff0c\u800c\u4e0e\u8fc7\u53bb\u7ecf\u5386\u7684\u6240\u6709\u72b6\u6001\u65e0\u5173\u3002\u8bb8\u591a\u7ec4\u5408\u4f18\u5316\u95ee\u9898\u4e0d\u5177\u6709\u65e0\u540e\u6548\u6027\uff0c\u65e0\u6cd5\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u5feb\u901f\u6c42\u89e3\u3002

        \u80cc\u5305\u95ee\u9898

        • \u80cc\u5305\u95ee\u9898\u662f\u6700\u5178\u578b\u7684\u52a8\u6001\u89c4\u5212\u95ee\u9898\u4e4b\u4e00\uff0c\u5177\u6709 0-1 \u80cc\u5305\u3001\u5b8c\u5168\u80cc\u5305\u3001\u591a\u91cd\u80cc\u5305\u7b49\u53d8\u79cd\u3002
        • 0-1 \u80cc\u5305\u7684\u72b6\u6001\u5b9a\u4e49\u4e3a\u524d \\(i\\) \u4e2a\u7269\u54c1\u5728\u5bb9\u91cf\u4e3a \\(c\\) \u7684\u80cc\u5305\u4e2d\u7684\u6700\u5927\u4ef7\u503c\u3002\u6839\u636e\u4e0d\u653e\u5165\u80cc\u5305\u548c\u653e\u5165\u80cc\u5305\u4e24\u79cd\u51b3\u7b56\uff0c\u53ef\u5f97\u5230\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u5e76\u6784\u5efa\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u3002\u5728\u7a7a\u95f4\u4f18\u5316\u4e2d\uff0c\u7531\u4e8e\u6bcf\u4e2a\u72b6\u6001\u4f9d\u8d56\u6b63\u4e0a\u65b9\u548c\u5de6\u4e0a\u65b9\u7684\u72b6\u6001\uff0c\u56e0\u6b64\u9700\u8981\u5012\u5e8f\u904d\u5386\u5217\u8868\uff0c\u907f\u514d\u5de6\u4e0a\u65b9\u72b6\u6001\u88ab\u8986\u76d6\u3002
        • \u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u6bcf\u79cd\u7269\u54c1\u7684\u9009\u53d6\u6570\u91cf\u65e0\u9650\u5236\uff0c\u56e0\u6b64\u9009\u62e9\u653e\u5165\u7269\u54c1\u7684\u72b6\u6001\u8f6c\u79fb\u4e0e 0-1 \u80cc\u5305\u95ee\u9898\u4e0d\u540c\u3002\u7531\u4e8e\u72b6\u6001\u4f9d\u8d56\u6b63\u4e0a\u65b9\u548c\u6b63\u5de6\u65b9\u7684\u72b6\u6001\uff0c\u56e0\u6b64\u5728\u7a7a\u95f4\u4f18\u5316\u4e2d\u5e94\u5f53\u6b63\u5e8f\u904d\u5386\u3002
        • \u96f6\u94b1\u5151\u6362\u95ee\u9898\u662f\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u4e00\u4e2a\u53d8\u79cd\u3002\u5b83\u4ece\u6c42\u201c\u6700\u5927\u201d\u4ef7\u503c\u53d8\u4e3a\u6c42\u201c\u6700\u5c0f\u201d\u786c\u5e01\u6570\u91cf\uff0c\u56e0\u6b64\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e2d\u7684 \\(\\max()\\) \u5e94\u6539\u4e3a \\(\\min()\\) \u3002\u4ece\u8ffd\u6c42\u201c\u4e0d\u8d85\u8fc7\u201d\u80cc\u5305\u5bb9\u91cf\u5230\u8ffd\u6c42\u201c\u6070\u597d\u201d\u51d1\u51fa\u76ee\u6807\u91d1\u989d\uff0c\u56e0\u6b64\u4f7f\u7528 \\(amt + 1\\) \u6765\u8868\u793a\u201c\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u201d\u7684\u65e0\u6548\u89e3\u3002
        • \u96f6\u94b1\u5151\u6362\u95ee\u9898 II \u4ece\u6c42\u201c\u6700\u5c11\u786c\u5e01\u6570\u91cf\u201d\u6539\u4e3a\u6c42\u201c\u786c\u5e01\u7ec4\u5408\u6570\u91cf\u201d\uff0c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u76f8\u5e94\u5730\u4ece \\(\\min()\\) \u6539\u4e3a\u6c42\u548c\u8fd0\u7b97\u7b26\u3002

        \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898

        • \u7f16\u8f91\u8ddd\u79bb\uff08Levenshtein \u8ddd\u79bb\uff09\u7528\u4e8e\u8861\u91cf\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e4b\u95f4\u7684\u76f8\u4f3c\u5ea6\uff0c\u5176\u5b9a\u4e49\u4e3a\u4ece\u4e00\u4e2a\u5b57\u7b26\u4e32\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\uff0c\u7f16\u8f91\u64cd\u4f5c\u5305\u62ec\u6dfb\u52a0\u3001\u5220\u9664\u3001\u66ff\u6362\u3002
        • \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898\u7684\u72b6\u6001\u5b9a\u4e49\u4e3a\u5c06 \\(s\\) \u7684\u524d \\(i\\) \u4e2a\u5b57\u7b26\u66f4\u6539\u4e3a \\(t\\) \u7684\u524d \\(j\\) \u4e2a\u5b57\u7b26\u6240\u9700\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u3002\u5f53 \\(s[i] \\ne t[j]\\) \u65f6\uff0c\u5177\u6709\u4e09\u79cd\u51b3\u7b56\uff1a\u6dfb\u52a0\u3001\u5220\u9664\u3001\u66ff\u6362\uff0c\u5b83\u4eec\u90fd\u6709\u76f8\u5e94\u7684\u5269\u4f59\u5b50\u95ee\u9898\u3002\u636e\u6b64\u4fbf\u53ef\u4ee5\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\u4e0e\u6784\u5efa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u3002\u800c\u5f53 \\(s[i] = t[j]\\) \u65f6\uff0c\u65e0\u987b\u7f16\u8f91\u5f53\u524d\u5b57\u7b26\u3002
        • \u5728\u7f16\u8f91\u8ddd\u79bb\u4e2d\uff0c\u72b6\u6001\u4f9d\u8d56\u5176\u6b63\u4e0a\u65b9\u3001\u6b63\u5de6\u65b9\u3001\u5de6\u4e0a\u65b9\u7684\u72b6\u6001\uff0c\u56e0\u6b64\u7a7a\u95f4\u4f18\u5316\u540e\u6b63\u5e8f\u6216\u5012\u5e8f\u904d\u5386\u90fd\u65e0\u6cd5\u6b63\u786e\u5730\u8fdb\u884c\u72b6\u6001\u8f6c\u79fb\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5229\u7528\u4e00\u4e2a\u53d8\u91cf\u6682\u5b58\u5de6\u4e0a\u65b9\u72b6\u6001\uff0c\u4ece\u800c\u8f6c\u5316\u5230\u4e0e\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7b49\u4ef7\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u5728\u7a7a\u95f4\u4f18\u5316\u540e\u8fdb\u884c\u6b63\u5e8f\u904d\u5386\u3002
        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/","title":"14.5 \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898","text":"

        \u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5148\u6c42\u89e3\u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u80cc\u5305\u95ee\u9898\uff1a\u5b8c\u5168\u80cc\u5305\uff0c\u518d\u4e86\u89e3\u5b83\u7684\u4e00\u79cd\u7279\u4f8b\uff1a\u96f6\u94b1\u5151\u6362\u3002

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1451","title":"14.5.1 \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898","text":"

        Question

        \u7ed9\u5b9a \\(n\\) \u4e2a\u7269\u54c1\uff0c\u7b2c \\(i\\) \u4e2a\u7269\u54c1\u7684\u91cd\u91cf\u4e3a \\(wgt[i-1]\\)\u3001\u4ef7\u503c\u4e3a \\(val[i-1]\\) \uff0c\u548c\u4e00\u4e2a\u5bb9\u91cf\u4e3a \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u4e2a\u7269\u54c1\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u80fd\u653e\u5165\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u3002\u793a\u4f8b\u5982\u56fe 14-22 \u6240\u793a\u3002

        \u56fe 14-22 \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"

        \u5b8c\u5168\u80cc\u5305\u95ee\u9898\u548c 0-1 \u80cc\u5305\u95ee\u9898\u975e\u5e38\u76f8\u4f3c\uff0c\u533a\u522b\u4ec5\u5728\u4e8e\u4e0d\u9650\u5236\u7269\u54c1\u7684\u9009\u62e9\u6b21\u6570\u3002

        • \u5728 0-1 \u80cc\u5305\u95ee\u9898\u4e2d\uff0c\u6bcf\u79cd\u7269\u54c1\u53ea\u6709\u4e00\u4e2a\uff0c\u56e0\u6b64\u5c06\u7269\u54c1 \\(i\\) \u653e\u5165\u80cc\u5305\u540e\uff0c\u53ea\u80fd\u4ece\u524d \\(i-1\\) \u4e2a\u7269\u54c1\u4e2d\u9009\u62e9\u3002
        • \u5728\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u4e2d\uff0c\u6bcf\u79cd\u7269\u54c1\u7684\u6570\u91cf\u662f\u65e0\u9650\u7684\uff0c\u56e0\u6b64\u5c06\u7269\u54c1 \\(i\\) \u653e\u5165\u80cc\u5305\u540e\uff0c\u4ecd\u53ef\u4ee5\u4ece\u524d \\(i\\) \u4e2a\u7269\u54c1\u4e2d\u9009\u62e9\u3002

        \u5728\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u89c4\u5b9a\u4e0b\uff0c\u72b6\u6001 \\([i, c]\\) \u7684\u53d8\u5316\u5206\u4e3a\u4e24\u79cd\u60c5\u51b5\u3002

        • \u4e0d\u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u4e0e 0-1 \u80cc\u5305\u95ee\u9898\u76f8\u540c\uff0c\u8f6c\u79fb\u81f3 \\([i-1, c]\\) \u3002
        • \u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u4e0e 0-1 \u80cc\u5305\u95ee\u9898\u4e0d\u540c\uff0c\u8f6c\u79fb\u81f3 \\([i, c-wgt[i-1]]\\) \u3002

        \u4ece\u800c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u53d8\u4e3a\uff1a

        \\[ dp[i, c] = \\max(dp[i-1, c], dp[i, c - wgt[i-1]] + val[i-1]) \\]"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u5bf9\u6bd4\u4e24\u9053\u9898\u76ee\u7684\u4ee3\u7801\uff0c\u72b6\u6001\u8f6c\u79fb\u4e2d\u6709\u4e00\u5904\u4ece \\(i-1\\) \u53d8\u4e3a \\(i\\) \uff0c\u5176\u4f59\u5b8c\u5168\u4e00\u81f4\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby unbounded_knapsack.py
        def unbounded_knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"\u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n    n = len(wgt)\n    # \u521d\u59cb\u5316 dp \u8868\n    dp = [[0] * (cap + 1) for _ in range(n + 1)]\n    # \u72b6\u6001\u8f6c\u79fb\n    for i in range(1, n + 1):\n        for c in range(1, cap + 1):\n            if wgt[i - 1] > c:\n                # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c]\n            else:\n                # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\n    return dp[n][cap]\n
        unbounded_knapsack.cpp
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // \u521d\u59cb\u5316 dp \u8868\n    vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        unbounded_knapsack.java
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[][] dp = new int[n + 1][cap + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = Math.max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        unbounded_knapsack.cs
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint UnboundedKnapsackDP(int[] wgt, int[] val, int cap) {\n    int n = wgt.Length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[,] dp = new int[n + 1, cap + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i, c] = dp[i - 1, c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i, c] = Math.Max(dp[i - 1, c], dp[i, c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n, cap];\n}\n
        unbounded_knapsack.go
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDP(wgt, val []int, cap int) int {\n    n := len(wgt)\n    // \u521d\u59cb\u5316 dp \u8868\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, cap+1)\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for i := 1; i <= n; i++ {\n        for c := 1; c <= cap; c++ {\n            if wgt[i-1] > c {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i-1][c]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i][c-wgt[i-1]]+val[i-1])))\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
        unbounded_knapsack.swift
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDP(wgt: [Int], val: [Int], cap: Int) -> Int {\n    let n = wgt.count\n    // \u521d\u59cb\u5316 dp \u8868\n    var dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1 ... n {\n        for c in 1 ... cap {\n            if wgt[i - 1] > c {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
        unbounded_knapsack.js
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDP(wgt, val, cap) {\n    const n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: cap + 1 }, () => 0)\n    );\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = Math.max(\n                    dp[i - 1][c],\n                    dp[i][c - wgt[i - 1]] + val[i - 1]\n                );\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        unbounded_knapsack.ts
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDP(\n    wgt: Array<number>,\n    val: Array<number>,\n    cap: number\n): number {\n    const n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: cap + 1 }, () => 0)\n    );\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = Math.max(\n                    dp[i - 1][c],\n                    dp[i][c - wgt[i - 1]] + val[i - 1]\n                );\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        unbounded_knapsack.dart
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(List<int> wgt, List<int> val, int cap) {\n  int n = wgt.length;\n  // \u521d\u59cb\u5316 dp \u8868\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(cap + 1, 0));\n  // \u72b6\u6001\u8f6c\u79fb\n  for (int i = 1; i <= n; i++) {\n    for (int c = 1; c <= cap; c++) {\n      if (wgt[i - 1] > c) {\n        // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n        dp[i][c] = dp[i - 1][c];\n      } else {\n        // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n        dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n      }\n    }\n  }\n  return dp[n][cap];\n}\n
        unbounded_knapsack.rs
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfn unbounded_knapsack_dp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n    let n = wgt.len();\n    // \u521d\u59cb\u5316 dp \u8868\n    let mut dp = vec![vec![0; cap + 1]; n + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1..=n {\n        for c in 1..=cap {\n            if wgt[i - 1] > c as i32 {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = std::cmp::max(dp[i - 1][c], dp[i][c - wgt[i - 1] as usize] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        unbounded_knapsack.c
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(int wgt[], int val[], int cap, int wgtSize) {\n    int n = wgtSize;\n    // \u521d\u59cb\u5316 dp \u8868\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(cap + 1, sizeof(int));\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = myMax(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    int res = dp[n][cap];\n    // \u91ca\u653e\u5185\u5b58\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    return res;\n}\n
        unbounded_knapsack.kt
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfun unboundedKnapsackDP(wgt: IntArray, _val: IntArray, cap: Int): Int {\n    val n = wgt.size\n    // \u521d\u59cb\u5316 dp \u8868\n    val dp = Array(n + 1) { IntArray(cap + 1) }\n    // \u72b6\u6001\u8f6c\u79fb\n    for (i in 1..n) {\n        for (c in 1..cap) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + _val[i - 1])\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
        unbounded_knapsack.rb
        ### \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 ###\ndef unbounded_knapsack_dp(wgt, val, cap)\n  n = wgt.length\n  # \u521d\u59cb\u5316 dp \u8868\n  dp = Array.new(n + 1) { Array.new(cap + 1, 0) }\n  # \u72b6\u6001\u8f6c\u79fb\n  for i in 1...(n + 1)\n    for c in 1...(cap + 1)\n      if wgt[i - 1] > c\n        # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n        dp[i][c] = dp[i - 1][c]\n      else\n        # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n        dp[i][c] = [dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[n][cap]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

        \u7531\u4e8e\u5f53\u524d\u72b6\u6001\u662f\u4ece\u5de6\u8fb9\u548c\u4e0a\u8fb9\u7684\u72b6\u6001\u8f6c\u79fb\u800c\u6765\u7684\uff0c\u56e0\u6b64\u7a7a\u95f4\u4f18\u5316\u540e\u5e94\u8be5\u5bf9 \\(dp\\) \u8868\u4e2d\u7684\u6bcf\u4e00\u884c\u8fdb\u884c\u6b63\u5e8f\u904d\u5386\u3002

        \u8fd9\u4e2a\u904d\u5386\u987a\u5e8f\u4e0e 0-1 \u80cc\u5305\u6b63\u597d\u76f8\u53cd\u3002\u8bf7\u501f\u52a9\u56fe 14-23 \u6765\u7406\u89e3\u4e24\u8005\u7684\u533a\u522b\u3002

        <1><2><3><4><5><6>

        \u56fe 14-23 \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898\u5728\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

        \u4ee3\u7801\u5b9e\u73b0\u6bd4\u8f83\u7b80\u5355\uff0c\u4ec5\u9700\u5c06\u6570\u7ec4 dp \u7684\u7b2c\u4e00\u7ef4\u5220\u9664\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby unbounded_knapsack.py
        def unbounded_knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"\u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n    n = len(wgt)\n    # \u521d\u59cb\u5316 dp \u8868\n    dp = [0] * (cap + 1)\n    # \u72b6\u6001\u8f6c\u79fb\n    for i in range(1, n + 1):\n        # \u6b63\u5e8f\u904d\u5386\n        for c in range(1, cap + 1):\n            if wgt[i - 1] > c:\n                # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c]\n            else:\n                # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n    return dp[cap]\n
        unbounded_knapsack.cpp
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // \u521d\u59cb\u5316 dp \u8868\n    vector<int> dp(cap + 1, 0);\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        unbounded_knapsack.java
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[] dp = new int[cap + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        unbounded_knapsack.cs
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint UnboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\n    int n = wgt.Length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[] dp = new int[cap + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = Math.Max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        unbounded_knapsack.go
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDPComp(wgt, val []int, cap int) int {\n    n := len(wgt)\n    // \u521d\u59cb\u5316 dp \u8868\n    dp := make([]int, cap+1)\n    // \u72b6\u6001\u8f6c\u79fb\n    for i := 1; i <= n; i++ {\n        for c := 1; c <= cap; c++ {\n            if wgt[i-1] > c {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))\n            }\n        }\n    }\n    return dp[cap]\n}\n
        unbounded_knapsack.swift
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDPComp(wgt: [Int], val: [Int], cap: Int) -> Int {\n    let n = wgt.count\n    // \u521d\u59cb\u5316 dp \u8868\n    var dp = Array(repeating: 0, count: cap + 1)\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1 ... n {\n        for c in 1 ... cap {\n            if wgt[i - 1] > c {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n            }\n        }\n    }\n    return dp[cap]\n}\n
        unbounded_knapsack.js
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDPComp(wgt, val, cap) {\n    const n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: cap + 1 }, () => 0);\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        unbounded_knapsack.ts
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDPComp(\n    wgt: Array<number>,\n    val: Array<number>,\n    cap: number\n): number {\n    const n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: cap + 1 }, () => 0);\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        unbounded_knapsack.dart
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(List<int> wgt, List<int> val, int cap) {\n  int n = wgt.length;\n  // \u521d\u59cb\u5316 dp \u8868\n  List<int> dp = List.filled(cap + 1, 0);\n  // \u72b6\u6001\u8f6c\u79fb\n  for (int i = 1; i <= n; i++) {\n    for (int c = 1; c <= cap; c++) {\n      if (wgt[i - 1] > c) {\n        // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n        dp[c] = dp[c];\n      } else {\n        // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n        dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n      }\n    }\n  }\n  return dp[cap];\n}\n
        unbounded_knapsack.rs
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn unbounded_knapsack_dp_comp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n    let n = wgt.len();\n    // \u521d\u59cb\u5316 dp \u8868\n    let mut dp = vec![0; cap + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1..=n {\n        for c in 1..=cap {\n            if wgt[i - 1] > c as i32 {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);\n            }\n        }\n    }\n    dp[cap]\n}\n
        unbounded_knapsack.c
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(int wgt[], int val[], int cap, int wgtSize) {\n    int n = wgtSize;\n    // \u521d\u59cb\u5316 dp \u8868\n    int *dp = calloc(cap + 1, sizeof(int));\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = myMax(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    int res = dp[cap];\n    // \u91ca\u653e\u5185\u5b58\n    free(dp);\n    return res;\n}\n
        unbounded_knapsack.kt
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfun unboundedKnapsackDPComp(\n    wgt: IntArray,\n    _val: IntArray,\n    cap: Int\n): Int {\n    val n = wgt.size\n    // \u521d\u59cb\u5316 dp \u8868\n    val dp = IntArray(cap + 1)\n    // \u72b6\u6001\u8f6c\u79fb\n    for (i in 1..n) {\n        for (c in 1..cap) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])\n            }\n        }\n    }\n    return dp[cap]\n}\n
        unbounded_knapsack.rb
        ### \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 ###\ndef unbounded_knapsack_dp(wgt, val, cap)\n  n = wgt.length\n  # \u521d\u59cb\u5316 dp \u8868\n  dp = Array.new(n + 1) { Array.new(cap + 1, 0) }\n  # \u72b6\u6001\u8f6c\u79fb\n  for i in 1...(n + 1)\n    for c in 1...(cap + 1)\n      if wgt[i - 1] > c\n        # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n        dp[i][c] = dp[i - 1][c]\n      else\n        # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n        dp[i][c] = [dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[n][cap]\nend\n\n### \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 ##3\ndef unbounded_knapsack_dp_comp(wgt, val, cap)\n  n = wgt.length\n  # \u521d\u59cb\u5316 dp \u8868\n  dp = Array.new(cap + 1, 0)\n  # \u72b6\u6001\u8f6c\u79fb\n  for i in 1...(n + 1)\n    # \u6b63\u5e8f\u904d\u5386\n    for c in 1...(cap + 1)\n      if wgt[i -1] > c\n        # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n        dp[c] = dp[c]\n      else\n        # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n        dp[c] = [dp[c], dp[c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[cap]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1452","title":"14.5.2 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898","text":"

        \u80cc\u5305\u95ee\u9898\u662f\u4e00\u5927\u7c7b\u52a8\u6001\u89c4\u5212\u95ee\u9898\u7684\u4ee3\u8868\uff0c\u5176\u62e5\u6709\u5f88\u591a\u53d8\u79cd\uff0c\u4f8b\u5982\u96f6\u94b1\u5151\u6362\u95ee\u9898\u3002

        Question

        \u7ed9\u5b9a \\(n\\) \u79cd\u786c\u5e01\uff0c\u7b2c \\(i\\) \u79cd\u786c\u5e01\u7684\u9762\u503c\u4e3a \\(coins[i - 1]\\) \uff0c\u76ee\u6807\u91d1\u989d\u4e3a \\(amt\\) \uff0c\u6bcf\u79cd\u786c\u5e01\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u80fd\u591f\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\u3002\u5982\u679c\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\uff0c\u5219\u8fd4\u56de \\(-1\\) \u3002\u793a\u4f8b\u5982\u56fe 14-24 \u6240\u793a\u3002

        \u56fe 14-24 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1_1","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"

        \u96f6\u94b1\u5151\u6362\u53ef\u4ee5\u770b\u4f5c\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u4e00\u79cd\u7279\u6b8a\u60c5\u51b5\uff0c\u4e24\u8005\u5177\u6709\u4ee5\u4e0b\u8054\u7cfb\u4e0e\u4e0d\u540c\u70b9\u3002

        • \u4e24\u9053\u9898\u53ef\u4ee5\u76f8\u4e92\u8f6c\u6362\uff0c\u201c\u7269\u54c1\u201d\u5bf9\u5e94\u201c\u786c\u5e01\u201d\u3001\u201c\u7269\u54c1\u91cd\u91cf\u201d\u5bf9\u5e94\u201c\u786c\u5e01\u9762\u503c\u201d\u3001\u201c\u80cc\u5305\u5bb9\u91cf\u201d\u5bf9\u5e94\u201c\u76ee\u6807\u91d1\u989d\u201d\u3002
        • \u4f18\u5316\u76ee\u6807\u76f8\u53cd\uff0c\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u662f\u8981\u6700\u5927\u5316\u7269\u54c1\u4ef7\u503c\uff0c\u96f6\u94b1\u5151\u6362\u95ee\u9898\u662f\u8981\u6700\u5c0f\u5316\u786c\u5e01\u6570\u91cf\u3002
        • \u5b8c\u5168\u80cc\u5305\u95ee\u9898\u662f\u6c42\u201c\u4e0d\u8d85\u8fc7\u201d\u80cc\u5305\u5bb9\u91cf\u4e0b\u7684\u89e3\uff0c\u96f6\u94b1\u5151\u6362\u662f\u6c42\u201c\u6070\u597d\u201d\u51d1\u5230\u76ee\u6807\u91d1\u989d\u7684\u89e3\u3002

        \u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868

        \u72b6\u6001 \\([i, a]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u4e3a\uff1a\u524d \\(i\\) \u79cd\u786c\u5e01\u80fd\u591f\u51d1\u51fa\u91d1\u989d \\(a\\) \u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\uff0c\u8bb0\u4e3a \\(dp[i, a]\\) \u3002

        \u4e8c\u7ef4 \\(dp\\) \u8868\u7684\u5c3a\u5bf8\u4e3a \\((n+1) \\times (amt+1)\\) \u3002

        \u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b

        \u672c\u9898\u4e0e\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u5b58\u5728\u4ee5\u4e0b\u4e24\u70b9\u5dee\u5f02\u3002

        • \u672c\u9898\u8981\u6c42\u6700\u5c0f\u503c\uff0c\u56e0\u6b64\u9700\u5c06\u8fd0\u7b97\u7b26 \\(\\max()\\) \u66f4\u6539\u4e3a \\(\\min()\\) \u3002
        • \u4f18\u5316\u4e3b\u4f53\u662f\u786c\u5e01\u6570\u91cf\u800c\u975e\u5546\u54c1\u4ef7\u503c\uff0c\u56e0\u6b64\u5728\u9009\u4e2d\u786c\u5e01\u65f6\u6267\u884c \\(+1\\) \u5373\u53ef\u3002
        \\[ dp[i, a] = \\min(dp[i-1, a], dp[i, a - coins[i-1]] + 1) \\]

        \u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f

        \u5f53\u76ee\u6807\u91d1\u989d\u4e3a \\(0\\) \u65f6\uff0c\u51d1\u51fa\u5b83\u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\u4e3a \\(0\\) \uff0c\u5373\u9996\u5217\u6240\u6709 \\(dp[i, 0]\\) \u90fd\u7b49\u4e8e \\(0\\) \u3002

        \u5f53\u65e0\u786c\u5e01\u65f6\uff0c\u65e0\u6cd5\u51d1\u51fa\u4efb\u610f \\(> 0\\) \u7684\u76ee\u6807\u91d1\u989d\uff0c\u5373\u662f\u65e0\u6548\u89e3\u3002\u4e3a\u4f7f\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e2d\u7684 \\(\\min()\\) \u51fd\u6570\u80fd\u591f\u8bc6\u522b\u5e76\u8fc7\u6ee4\u65e0\u6548\u89e3\uff0c\u6211\u4eec\u8003\u8651\u4f7f\u7528 \\(+ \\infty\\) \u6765\u8868\u793a\u5b83\u4eec\uff0c\u5373\u4ee4\u9996\u884c\u6240\u6709 \\(dp[0, a]\\) \u90fd\u7b49\u4e8e \\(+ \\infty\\) \u3002

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2_1","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u5e76\u672a\u63d0\u4f9b \\(+ \\infty\\) \u53d8\u91cf\uff0c\u53ea\u80fd\u4f7f\u7528\u6574\u578b int \u7684\u6700\u5927\u503c\u6765\u4ee3\u66ff\u3002\u800c\u8fd9\u53c8\u4f1a\u5bfc\u81f4\u5927\u6570\u8d8a\u754c\uff1a\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e2d\u7684 \\(+ 1\\) \u64cd\u4f5c\u53ef\u80fd\u53d1\u751f\u6ea2\u51fa\u3002

        \u4e3a\u6b64\uff0c\u6211\u4eec\u91c7\u7528\u6570\u5b57 \\(amt + 1\\) \u6765\u8868\u793a\u65e0\u6548\u89e3\uff0c\u56e0\u4e3a\u51d1\u51fa \\(amt\\) \u7684\u786c\u5e01\u6570\u91cf\u6700\u591a\u4e3a \\(amt\\) \u3002\u6700\u540e\u8fd4\u56de\u524d\uff0c\u5224\u65ad \\(dp[n, amt]\\) \u662f\u5426\u7b49\u4e8e \\(amt + 1\\) \uff0c\u82e5\u662f\u5219\u8fd4\u56de \\(-1\\) \uff0c\u4ee3\u8868\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change.py
        def coin_change_dp(coins: list[int], amt: int) -> int:\n    \"\"\"\u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n    n = len(coins)\n    MAX = amt + 1\n    # \u521d\u59cb\u5316 dp \u8868\n    dp = [[0] * (amt + 1) for _ in range(n + 1)]\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for a in range(1, amt + 1):\n        dp[0][a] = MAX\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i in range(1, n + 1):\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a]\n            else:\n                # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n    return dp[n][amt] if dp[n][amt] != MAX else -1\n
        coin_change.cpp
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(vector<int> &coins, int amt) {\n    int n = coins.size();\n    int MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    vector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (int a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
        coin_change.java
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(int[] coins, int amt) {\n    int n = coins.length;\n    int MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[][] dp = new int[n + 1][amt + 1];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (int a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
        coin_change.cs
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint CoinChangeDP(int[] coins, int amt) {\n    int n = coins.Length;\n    int MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[,] dp = new int[n + 1, amt + 1];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (int a = 1; a <= amt; a++) {\n        dp[0, a] = MAX;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i, a] = dp[i - 1, a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[i, a] = Math.Min(dp[i - 1, a], dp[i, a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n, amt] != MAX ? dp[n, amt] : -1;\n}\n
        coin_change.go
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDP(coins []int, amt int) int {\n    n := len(coins)\n    max := amt + 1\n    // \u521d\u59cb\u5316 dp \u8868\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, amt+1)\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for a := 1; a <= amt; a++ {\n        dp[0][a] = max\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i := 1; i <= n; i++ {\n        for a := 1; a <= amt; a++ {\n            if coins[i-1] > a {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i-1][a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[i][a] = int(math.Min(float64(dp[i-1][a]), float64(dp[i][a-coins[i-1]]+1)))\n            }\n        }\n    }\n    if dp[n][amt] != max {\n        return dp[n][amt]\n    }\n    return -1\n}\n
        coin_change.swift
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDP(coins: [Int], amt: Int) -> Int {\n    let n = coins.count\n    let MAX = amt + 1\n    // \u521d\u59cb\u5316 dp \u8868\n    var dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for a in 1 ... amt {\n        dp[0][a] = MAX\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i in 1 ... n {\n        for a in 1 ... amt {\n            if coins[i - 1] > a {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n            }\n        }\n    }\n    return dp[n][amt] != MAX ? dp[n][amt] : -1\n}\n
        coin_change.js
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDP(coins, amt) {\n    const n = coins.length;\n    const MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: amt + 1 }, () => 0)\n    );\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (let a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] !== MAX ? dp[n][amt] : -1;\n}\n
        coin_change.ts
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDP(coins: Array<number>, amt: number): number {\n    const n = coins.length;\n    const MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: amt + 1 }, () => 0)\n    );\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (let a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] !== MAX ? dp[n][amt] : -1;\n}\n
        coin_change.dart
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(List<int> coins, int amt) {\n  int n = coins.length;\n  int MAX = amt + 1;\n  // \u521d\u59cb\u5316 dp \u8868\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(amt + 1, 0));\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n  for (int a = 1; a <= amt; a++) {\n    dp[0][a] = MAX;\n  }\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n  for (int i = 1; i <= n; i++) {\n    for (int a = 1; a <= amt; a++) {\n      if (coins[i - 1] > a) {\n        // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n        dp[i][a] = dp[i - 1][a];\n      } else {\n        // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n        dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n      }\n    }\n  }\n  return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
        coin_change.rs
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfn coin_change_dp(coins: &[i32], amt: usize) -> i32 {\n    let n = coins.len();\n    let max = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    let mut dp = vec![vec![0; amt + 1]; n + 1];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for a in 1..=amt {\n        dp[0][a] = max;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i in 1..=n {\n        for a in 1..=amt {\n            if coins[i - 1] > a as i32 {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[i][a] = std::cmp::min(dp[i - 1][a], dp[i][a - coins[i - 1] as usize] + 1);\n            }\n        }\n    }\n    if dp[n][amt] != max {\n        return dp[n][amt] as i32;\n    } else {\n        -1\n    }\n}\n
        coin_change.c
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(int coins[], int amt, int coinsSize) {\n    int n = coinsSize;\n    int MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(amt + 1, sizeof(int));\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (int a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[i][a] = myMin(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    int res = dp[n][amt] != MAX ? dp[n][amt] : -1;\n    // \u91ca\u653e\u5185\u5b58\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    free(dp);\n    return res;\n}\n
        coin_change.kt
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfun coinChangeDP(coins: IntArray, amt: Int): Int {\n    val n = coins.size\n    val MAX = amt + 1\n    // \u521d\u59cb\u5316 dp \u8868\n    val dp = Array(n + 1) { IntArray(amt + 1) }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (a in 1..amt) {\n        dp[0][a] = MAX\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (i in 1..n) {\n        for (a in 1..amt) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n            }\n        }\n    }\n    return if (dp[n][amt] != MAX) dp[n][amt] else -1\n}\n
        coin_change.rb
        ### \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 ###\ndef coin_change_dp(coins, amt)\n  n = coins.length\n  _MAX = amt + 1\n  # \u521d\u59cb\u5316 dp \u8868\n  dp = Array.new(n + 1) { Array.new(amt + 1, 0) }\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n  (1...(amt + 1)).each { |a| dp[0][a] = _MAX }\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n  for i in 1...(n + 1)\n    for a in 1...(amt + 1)\n      if coins[i - 1] > a\n        # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n        dp[i][a] = dp[i - 1][a]\n      else\n        # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n        dp[i][a] = [dp[i - 1][a], dp[i][a - coins[i - 1]] + 1].min\n      end\n    end\n  end\n  dp[n][amt] != _MAX ? dp[n][amt] : -1\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 14-25 \u5c55\u793a\u4e86\u96f6\u94b1\u5151\u6362\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b\uff0c\u548c\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u975e\u5e38\u76f8\u4f3c\u3002

        <1><2><3><4><5><6><7><8><9><10><11><12><13><14><15>

        \u56fe 14-25 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3_1","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

        \u96f6\u94b1\u5151\u6362\u7684\u7a7a\u95f4\u4f18\u5316\u7684\u5904\u7406\u65b9\u5f0f\u548c\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u4e00\u81f4\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change.py
        def coin_change_dp_comp(coins: list[int], amt: int) -> int:\n    \"\"\"\u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n    n = len(coins)\n    MAX = amt + 1\n    # \u521d\u59cb\u5316 dp \u8868\n    dp = [MAX] * (amt + 1)\n    dp[0] = 0\n    # \u72b6\u6001\u8f6c\u79fb\n    for i in range(1, n + 1):\n        # \u6b63\u5e8f\u904d\u5386\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a]\n            else:\n                # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n    return dp[amt] if dp[amt] != MAX else -1\n
        coin_change.cpp
        /* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(vector<int> &coins, int amt) {\n    int n = coins.size();\n    int MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    vector<int> dp(amt + 1, MAX);\n    dp[0] = 0;\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1;\n}\n
        coin_change.java
        /* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(int[] coins, int amt) {\n    int n = coins.length;\n    int MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[] dp = new int[amt + 1];\n    Arrays.fill(dp, MAX);\n    dp[0] = 0;\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1;\n}\n
        coin_change.cs
        /* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint CoinChangeDPComp(int[] coins, int amt) {\n    int n = coins.Length;\n    int MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[] dp = new int[amt + 1];\n    Array.Fill(dp, MAX);\n    dp[0] = 0;\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[a] = Math.Min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1;\n}\n
        coin_change.go
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDPComp(coins []int, amt int) int {\n    n := len(coins)\n    max := amt + 1\n    // \u521d\u59cb\u5316 dp \u8868\n    dp := make([]int, amt+1)\n    for i := 1; i <= amt; i++ {\n        dp[i] = max\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for i := 1; i <= n; i++ {\n        // \u6b63\u5e8f\u904d\u5386\n        for a := 1; a <= amt; a++ {\n            if coins[i-1] > a {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[a] = int(math.Min(float64(dp[a]), float64(dp[a-coins[i-1]]+1)))\n            }\n        }\n    }\n    if dp[amt] != max {\n        return dp[amt]\n    }\n    return -1\n}\n
        coin_change.swift
        /* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDPComp(coins: [Int], amt: Int) -> Int {\n    let n = coins.count\n    let MAX = amt + 1\n    // \u521d\u59cb\u5316 dp \u8868\n    var dp = Array(repeating: MAX, count: amt + 1)\n    dp[0] = 0\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1 ... n {\n        for a in 1 ... amt {\n            if coins[i - 1] > a {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1\n}\n
        coin_change.js
        /* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDPComp(coins, amt) {\n    const n = coins.length;\n    const MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: amt + 1 }, () => MAX);\n    dp[0] = 0;\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] !== MAX ? dp[amt] : -1;\n}\n
        coin_change.ts
        /* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDPComp(coins: Array<number>, amt: number): number {\n    const n = coins.length;\n    const MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: amt + 1 }, () => MAX);\n    dp[0] = 0;\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] !== MAX ? dp[amt] : -1;\n}\n
        coin_change.dart
        /* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(List<int> coins, int amt) {\n  int n = coins.length;\n  int MAX = amt + 1;\n  // \u521d\u59cb\u5316 dp \u8868\n  List<int> dp = List.filled(amt + 1, MAX);\n  dp[0] = 0;\n  // \u72b6\u6001\u8f6c\u79fb\n  for (int i = 1; i <= n; i++) {\n    for (int a = 1; a <= amt; a++) {\n      if (coins[i - 1] > a) {\n        // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n        dp[a] = dp[a];\n      } else {\n        // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n        dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n      }\n    }\n  }\n  return dp[amt] != MAX ? dp[amt] : -1;\n}\n
        coin_change.rs
        /* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn coin_change_dp_comp(coins: &[i32], amt: usize) -> i32 {\n    let n = coins.len();\n    let max = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    let mut dp = vec![0; amt + 1];\n    dp.fill(max);\n    dp[0] = 0;\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1..=n {\n        for a in 1..=amt {\n            if coins[i - 1] > a as i32 {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[a] = std::cmp::min(dp[a], dp[a - coins[i - 1] as usize] + 1);\n            }\n        }\n    }\n    if dp[amt] != max {\n        return dp[amt] as i32;\n    } else {\n        -1\n    }\n}\n
        coin_change.c
        /* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(int coins[], int amt, int coinsSize) {\n    int n = coinsSize;\n    int MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    int *dp = malloc((amt + 1) * sizeof(int));\n    for (int j = 1; j <= amt; j++) {\n        dp[j] = MAX;\n    } \n    dp[0] = 0;\n\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[a] = myMin(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    int res = dp[amt] != MAX ? dp[amt] : -1;\n    // \u91ca\u653e\u5185\u5b58\n    free(dp);\n    return res;\n}\n
        coin_change.kt
        /* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfun coinChangeDPComp(coins: IntArray, amt: Int): Int {\n    val n = coins.size\n    val MAX = amt + 1\n    // \u521d\u59cb\u5316 dp \u8868\n    val dp = IntArray(amt + 1)\n    dp.fill(MAX)\n    dp[0] = 0\n    // \u72b6\u6001\u8f6c\u79fb\n    for (i in 1..n) {\n        for (a in 1..amt) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n            }\n        }\n    }\n    return if (dp[amt] != MAX) dp[amt] else -1\n}\n
        coin_change.rb
        ### \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 ###\ndef coin_change_dp_comp(coins, amt)\n  n = coins.length\n  _MAX = amt + 1\n  # \u521d\u59cb\u5316 dp \u8868\n  dp = Array.new(amt + 1, _MAX)\n  dp[0] = 0\n  # \u72b6\u6001\u8f6c\u79fb\n  for i in 1...(n + 1)\n    # \u6b63\u5e8f\u904d\u5386\n    for a in 1...(amt + 1)\n      if coins[i - 1] > a\n        # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n        dp[a] = dp[a]\n      else\n        # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n        dp[a] = [dp[a], dp[a - coins[i - 1]] + 1].min\n      end\n    end\n  end\n  dp[amt] != _MAX ? dp[amt] : -1\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1453-ii","title":"14.5.3 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898 II","text":"

        Question

        \u7ed9\u5b9a \\(n\\) \u79cd\u786c\u5e01\uff0c\u7b2c \\(i\\) \u79cd\u786c\u5e01\u7684\u9762\u503c\u4e3a \\(coins[i - 1]\\) \uff0c\u76ee\u6807\u91d1\u989d\u4e3a \\(amt\\) \uff0c\u6bcf\u79cd\u786c\u5e01\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u7684\u786c\u5e01\u7ec4\u5408\u6570\u91cf\u3002\u793a\u4f8b\u5982\u56fe 14-26 \u6240\u793a\u3002

        \u56fe 14-26 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898 II \u7684\u793a\u4f8b\u6570\u636e

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1_2","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"

        \u76f8\u6bd4\u4e8e\u4e0a\u4e00\u9898\uff0c\u672c\u9898\u76ee\u6807\u662f\u6c42\u7ec4\u5408\u6570\u91cf\uff0c\u56e0\u6b64\u5b50\u95ee\u9898\u53d8\u4e3a\uff1a\u524d \\(i\\) \u79cd\u786c\u5e01\u80fd\u591f\u51d1\u51fa\u91d1\u989d \\(a\\) \u7684\u7ec4\u5408\u6570\u91cf\u3002\u800c \\(dp\\) \u8868\u4ecd\u7136\u662f\u5c3a\u5bf8\u4e3a \\((n+1) \\times (amt + 1)\\) \u7684\u4e8c\u7ef4\u77e9\u9635\u3002

        \u5f53\u524d\u72b6\u6001\u7684\u7ec4\u5408\u6570\u91cf\u7b49\u4e8e\u4e0d\u9009\u5f53\u524d\u786c\u5e01\u4e0e\u9009\u5f53\u524d\u786c\u5e01\u8fd9\u4e24\u79cd\u51b3\u7b56\u7684\u7ec4\u5408\u6570\u91cf\u4e4b\u548c\u3002\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a

        \\[ dp[i, a] = dp[i-1, a] + dp[i, a - coins[i-1]] \\]

        \u5f53\u76ee\u6807\u91d1\u989d\u4e3a \\(0\\) \u65f6\uff0c\u65e0\u987b\u9009\u62e9\u4efb\u4f55\u786c\u5e01\u5373\u53ef\u51d1\u51fa\u76ee\u6807\u91d1\u989d\uff0c\u56e0\u6b64\u5e94\u5c06\u9996\u5217\u6240\u6709 \\(dp[i, 0]\\) \u90fd\u521d\u59cb\u5316\u4e3a \\(1\\) \u3002\u5f53\u65e0\u786c\u5e01\u65f6\uff0c\u65e0\u6cd5\u51d1\u51fa\u4efb\u4f55 \\(>0\\) \u7684\u76ee\u6807\u91d1\u989d\uff0c\u56e0\u6b64\u9996\u884c\u6240\u6709 \\(dp[0, a]\\) \u90fd\u7b49\u4e8e \\(0\\) \u3002

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2_2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change_ii.py
        def coin_change_ii_dp(coins: list[int], amt: int) -> int:\n    \"\"\"\u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n    n = len(coins)\n    # \u521d\u59cb\u5316 dp \u8868\n    dp = [[0] * (amt + 1) for _ in range(n + 1)]\n    # \u521d\u59cb\u5316\u9996\u5217\n    for i in range(n + 1):\n        dp[i][0] = 1\n    # \u72b6\u6001\u8f6c\u79fb\n    for i in range(1, n + 1):\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a]\n            else:\n                # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n    return dp[n][amt]\n
        coin_change_ii.cpp
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(vector<int> &coins, int amt) {\n    int n = coins.size();\n    // \u521d\u59cb\u5316 dp \u8868\n    vector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n    // \u521d\u59cb\u5316\u9996\u5217\n    for (int i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
        coin_change_ii.java
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(int[] coins, int amt) {\n    int n = coins.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[][] dp = new int[n + 1][amt + 1];\n    // \u521d\u59cb\u5316\u9996\u5217\n    for (int i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
        coin_change_ii.cs
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint CoinChangeIIDP(int[] coins, int amt) {\n    int n = coins.Length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[,] dp = new int[n + 1, amt + 1];\n    // \u521d\u59cb\u5316\u9996\u5217\n    for (int i = 0; i <= n; i++) {\n        dp[i, 0] = 1;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i, a] = dp[i - 1, a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[i, a] = dp[i - 1, a] + dp[i, a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n, amt];\n}\n
        coin_change_ii.go
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDP(coins []int, amt int) int {\n    n := len(coins)\n    // \u521d\u59cb\u5316 dp \u8868\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, amt+1)\n    }\n    // \u521d\u59cb\u5316\u9996\u5217\n    for i := 0; i <= n; i++ {\n        dp[i][0] = 1\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i := 1; i <= n; i++ {\n        for a := 1; a <= amt; a++ {\n            if coins[i-1] > a {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i-1][a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[i][a] = dp[i-1][a] + dp[i][a-coins[i-1]]\n            }\n        }\n    }\n    return dp[n][amt]\n}\n
        coin_change_ii.swift
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDP(coins: [Int], amt: Int) -> Int {\n    let n = coins.count\n    // \u521d\u59cb\u5316 dp \u8868\n    var dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)\n    // \u521d\u59cb\u5316\u9996\u5217\n    for i in 0 ... n {\n        dp[i][0] = 1\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1 ... n {\n        for a in 1 ... amt {\n            if coins[i - 1] > a {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n            }\n        }\n    }\n    return dp[n][amt]\n}\n
        coin_change_ii.js
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDP(coins, amt) {\n    const n = coins.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: amt + 1 }, () => 0)\n    );\n    // \u521d\u59cb\u5316\u9996\u5217\n    for (let i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
        coin_change_ii.ts
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDP(coins: Array<number>, amt: number): number {\n    const n = coins.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: amt + 1 }, () => 0)\n    );\n    // \u521d\u59cb\u5316\u9996\u5217\n    for (let i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
        coin_change_ii.dart
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(List<int> coins, int amt) {\n  int n = coins.length;\n  // \u521d\u59cb\u5316 dp \u8868\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(amt + 1, 0));\n  // \u521d\u59cb\u5316\u9996\u5217\n  for (int i = 0; i <= n; i++) {\n    dp[i][0] = 1;\n  }\n  // \u72b6\u6001\u8f6c\u79fb\n  for (int i = 1; i <= n; i++) {\n    for (int a = 1; a <= amt; a++) {\n      if (coins[i - 1] > a) {\n        // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n        dp[i][a] = dp[i - 1][a];\n      } else {\n        // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n        dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n      }\n    }\n  }\n  return dp[n][amt];\n}\n
        coin_change_ii.rs
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfn coin_change_ii_dp(coins: &[i32], amt: usize) -> i32 {\n    let n = coins.len();\n    // \u521d\u59cb\u5316 dp \u8868\n    let mut dp = vec![vec![0; amt + 1]; n + 1];\n    // \u521d\u59cb\u5316\u9996\u5217\n    for i in 0..=n {\n        dp[i][0] = 1;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1..=n {\n        for a in 1..=amt {\n            if coins[i - 1] > a as i32 {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1] as usize];\n            }\n        }\n    }\n    dp[n][amt]\n}\n
        coin_change_ii.c
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(int coins[], int amt, int coinsSize) {\n    int n = coinsSize;\n    // \u521d\u59cb\u5316 dp \u8868\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(amt + 1, sizeof(int));\n    }\n    // \u521d\u59cb\u5316\u9996\u5217\n    for (int i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    int res = dp[n][amt];\n    // \u91ca\u653e\u5185\u5b58\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    free(dp);\n    return res;\n}\n
        coin_change_ii.kt
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfun coinChangeIIDP(coins: IntArray, amt: Int): Int {\n    val n = coins.size\n    // \u521d\u59cb\u5316 dp \u8868\n    val dp = Array(n + 1) { IntArray(amt + 1) }\n    // \u521d\u59cb\u5316\u9996\u5217\n    for (i in 0..n) {\n        dp[i][0] = 1\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for (i in 1..n) {\n        for (a in 1..amt) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n            }\n        }\n    }\n    return dp[n][amt]\n}\n
        coin_change_ii.rb
        ### \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 ###\ndef coin_change_ii_dp(coins, amt)\n  n = coins.length\n  # \u521d\u59cb\u5316 dp \u8868\n  dp = Array.new(n + 1) { Array.new(amt + 1, 0) }\n  # \u521d\u59cb\u5316\u9996\u5217\n  (0...(n + 1)).each { |i| dp[i][0] = 1 }\n  # \u72b6\u6001\u8f6c\u79fb\n  for i in 1...(n + 1)\n    for a in 1...(amt + 1)\n      if coins[i - 1] > a\n        # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n        dp[i][a] = dp[i - 1][a]\n      else\n        # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n        dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n      end\n    end\n  end\n  dp[n][amt]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3_2","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

        \u7a7a\u95f4\u4f18\u5316\u5904\u7406\u65b9\u5f0f\u76f8\u540c\uff0c\u5220\u9664\u786c\u5e01\u7ef4\u5ea6\u5373\u53ef\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change_ii.py
        def coin_change_ii_dp_comp(coins: list[int], amt: int) -> int:\n    \"\"\"\u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n    n = len(coins)\n    # \u521d\u59cb\u5316 dp \u8868\n    dp = [0] * (amt + 1)\n    dp[0] = 1\n    # \u72b6\u6001\u8f6c\u79fb\n    for i in range(1, n + 1):\n        # \u6b63\u5e8f\u904d\u5386\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a]\n            else:\n                # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[a] = dp[a] + dp[a - coins[i - 1]]\n    return dp[amt]\n
        coin_change_ii.cpp
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(vector<int> &coins, int amt) {\n    int n = coins.size();\n    // \u521d\u59cb\u5316 dp \u8868\n    vector<int> dp(amt + 1, 0);\n    dp[0] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
        coin_change_ii.java
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(int[] coins, int amt) {\n    int n = coins.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[] dp = new int[amt + 1];\n    dp[0] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
        coin_change_ii.cs
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint CoinChangeIIDPComp(int[] coins, int amt) {\n    int n = coins.Length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[] dp = new int[amt + 1];\n    dp[0] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
        coin_change_ii.go
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDPComp(coins []int, amt int) int {\n    n := len(coins)\n    // \u521d\u59cb\u5316 dp \u8868\n    dp := make([]int, amt+1)\n    dp[0] = 1\n    // \u72b6\u6001\u8f6c\u79fb\n    for i := 1; i <= n; i++ {\n        // \u6b63\u5e8f\u904d\u5386\n        for a := 1; a <= amt; a++ {\n            if coins[i-1] > a {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[a] = dp[a] + dp[a-coins[i-1]]\n            }\n        }\n    }\n    return dp[amt]\n}\n
        coin_change_ii.swift
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDPComp(coins: [Int], amt: Int) -> Int {\n    let n = coins.count\n    // \u521d\u59cb\u5316 dp \u8868\n    var dp = Array(repeating: 0, count: amt + 1)\n    dp[0] = 1\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1 ... n {\n        for a in 1 ... amt {\n            if coins[i - 1] > a {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[a] = dp[a] + dp[a - coins[i - 1]]\n            }\n        }\n    }\n    return dp[amt]\n}\n
        coin_change_ii.js
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDPComp(coins, amt) {\n    const n = coins.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: amt + 1 }, () => 0);\n    dp[0] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
        coin_change_ii.ts
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDPComp(coins: Array<number>, amt: number): number {\n    const n = coins.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: amt + 1 }, () => 0);\n    dp[0] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
        coin_change_ii.dart
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(List<int> coins, int amt) {\n  int n = coins.length;\n  // \u521d\u59cb\u5316 dp \u8868\n  List<int> dp = List.filled(amt + 1, 0);\n  dp[0] = 1;\n  // \u72b6\u6001\u8f6c\u79fb\n  for (int i = 1; i <= n; i++) {\n    for (int a = 1; a <= amt; a++) {\n      if (coins[i - 1] > a) {\n        // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n        dp[a] = dp[a];\n      } else {\n        // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n        dp[a] = dp[a] + dp[a - coins[i - 1]];\n      }\n    }\n  }\n  return dp[amt];\n}\n
        coin_change_ii.rs
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn coin_change_ii_dp_comp(coins: &[i32], amt: usize) -> i32 {\n    let n = coins.len();\n    // \u521d\u59cb\u5316 dp \u8868\n    let mut dp = vec![0; amt + 1];\n    dp[0] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1..=n {\n        for a in 1..=amt {\n            if coins[i - 1] > a as i32 {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[a] = dp[a] + dp[a - coins[i - 1] as usize];\n            }\n        }\n    }\n    dp[amt]\n}\n
        coin_change_ii.c
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(int coins[], int amt, int coinsSize) {\n    int n = coinsSize;\n    // \u521d\u59cb\u5316 dp \u8868\n    int *dp = calloc(amt + 1, sizeof(int));\n    dp[0] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    int res = dp[amt];\n    // \u91ca\u653e\u5185\u5b58\n    free(dp);\n    return res;\n}\n
        coin_change_ii.kt
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfun coinChangeIIDPComp(coins: IntArray, amt: Int): Int {\n    val n = coins.size\n    // \u521d\u59cb\u5316 dp \u8868\n    val dp = IntArray(amt + 1)\n    dp[0] = 1\n    // \u72b6\u6001\u8f6c\u79fb\n    for (i in 1..n) {\n        for (a in 1..amt) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[a] = dp[a] + dp[a - coins[i - 1]]\n            }\n        }\n    }\n    return dp[amt]\n}\n
        coin_change_ii.rb
        ### \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 ###\ndef coin_change_ii_dp_comp(coins, amt)\n  n = coins.length\n  # \u521d\u59cb\u5316 dp \u8868\n  dp = Array.new(amt + 1, 0)\n  dp[0] = 1\n  # \u72b6\u6001\u8f6c\u79fb\n  for i in 1...(n + 1)\n    # \u6b63\u5e8f\u904d\u5386\n    for a in 1...(amt + 1)\n      if coins[i - 1] > a\n        # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n        dp[a] = dp[a]\n      else\n        # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n        dp[a] = dp[a] + dp[a - coins[i - 1]]\n      end\n    end\n  end\n  dp[amt]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_graph/","title":"\u7b2c 9 \u7ae0 \u00a0 \u56fe","text":"

        Abstract

        \u5728\u751f\u547d\u65c5\u9014\u4e2d\uff0c\u6211\u4eec\u5c31\u50cf\u662f\u4e00\u4e2a\u4e2a\u8282\u70b9\uff0c\u88ab\u65e0\u6570\u770b\u4e0d\u89c1\u7684\u8fb9\u76f8\u8fde\u3002

        \u6bcf\u4e00\u6b21\u7684\u76f8\u8bc6\u4e0e\u76f8\u79bb\uff0c\u90fd\u5728\u8fd9\u5f20\u5de8\u5927\u7684\u7f51\u7edc\u56fe\u4e2d\u7559\u4e0b\u72ec\u7279\u7684\u5370\u8bb0\u3002

        "},{"location":"chapter_graph/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 9.1 \u00a0 \u56fe
        • 9.2 \u00a0 \u56fe\u57fa\u7840\u64cd\u4f5c
        • 9.3 \u00a0 \u56fe\u7684\u904d\u5386
        • 9.4 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_graph/graph/","title":"9.1 \u00a0 \u56fe","text":"

        \u56fe\uff08graph\uff09\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u7531\u9876\u70b9\uff08vertex\uff09\u548c\u8fb9\uff08edge\uff09\u7ec4\u6210\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u56fe \\(G\\) \u62bd\u8c61\u5730\u8868\u793a\u4e3a\u4e00\u7ec4\u9876\u70b9 \\(V\\) \u548c\u4e00\u7ec4\u8fb9 \\(E\\) \u7684\u96c6\u5408\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u4e00\u4e2a\u5305\u542b 5 \u4e2a\u9876\u70b9\u548c 7 \u6761\u8fb9\u7684\u56fe\u3002

        \\[ \\begin{aligned} V & = \\{ 1, 2, 3, 4, 5 \\} \\newline E & = \\{ (1,2), (1,3), (1,5), (2,3), (2,4), (2,5), (4,5) \\} \\newline G & = \\{ V, E \\} \\newline \\end{aligned} \\]

        \u5982\u679c\u5c06\u9876\u70b9\u770b\u4f5c\u8282\u70b9\uff0c\u5c06\u8fb9\u770b\u4f5c\u8fde\u63a5\u5404\u4e2a\u8282\u70b9\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u56fe\u770b\u4f5c\u4e00\u79cd\u4ece\u94fe\u8868\u62d3\u5c55\u800c\u6765\u7684\u6570\u636e\u7ed3\u6784\u3002\u5982\u56fe 9-1 \u6240\u793a\uff0c\u76f8\u8f83\u4e8e\u7ebf\u6027\u5173\u7cfb\uff08\u94fe\u8868\uff09\u548c\u5206\u6cbb\u5173\u7cfb\uff08\u6811\uff09\uff0c\u7f51\u7edc\u5173\u7cfb\uff08\u56fe\uff09\u7684\u81ea\u7531\u5ea6\u66f4\u9ad8\uff0c\u56e0\u800c\u66f4\u4e3a\u590d\u6742\u3002

        \u56fe 9-1 \u00a0 \u94fe\u8868\u3001\u6811\u3001\u56fe\u4e4b\u95f4\u7684\u5173\u7cfb

        "},{"location":"chapter_graph/graph/#911","title":"9.1.1 \u00a0 \u56fe\u7684\u5e38\u89c1\u7c7b\u578b\u4e0e\u672f\u8bed","text":"

        \u6839\u636e\u8fb9\u662f\u5426\u5177\u6709\u65b9\u5411\uff0c\u53ef\u5206\u4e3a\u65e0\u5411\u56fe\uff08undirected graph\uff09\u548c\u6709\u5411\u56fe\uff08directed graph\uff09\uff0c\u5982\u56fe 9-2 \u6240\u793a\u3002

        • \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u8fb9\u8868\u793a\u4e24\u9876\u70b9\u4e4b\u95f4\u7684\u201c\u53cc\u5411\u201d\u8fde\u63a5\u5173\u7cfb\uff0c\u4f8b\u5982\u5fae\u4fe1\u6216 QQ \u4e2d\u7684\u201c\u597d\u53cb\u5173\u7cfb\u201d\u3002
        • \u5728\u6709\u5411\u56fe\u4e2d\uff0c\u8fb9\u5177\u6709\u65b9\u5411\u6027\uff0c\u5373 \\(A \\rightarrow B\\) \u548c \\(A \\leftarrow B\\) \u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u662f\u76f8\u4e92\u72ec\u7acb\u7684\uff0c\u4f8b\u5982\u5fae\u535a\u6216\u6296\u97f3\u4e0a\u7684\u201c\u5173\u6ce8\u201d\u4e0e\u201c\u88ab\u5173\u6ce8\u201d\u5173\u7cfb\u3002

        \u56fe 9-2 \u00a0 \u6709\u5411\u56fe\u4e0e\u65e0\u5411\u56fe

        \u6839\u636e\u6240\u6709\u9876\u70b9\u662f\u5426\u8fde\u901a\uff0c\u53ef\u5206\u4e3a\u8fde\u901a\u56fe\uff08connected graph\uff09\u548c\u975e\u8fde\u901a\u56fe\uff08disconnected graph\uff09\uff0c\u5982\u56fe 9-3 \u6240\u793a\u3002

        • \u5bf9\u4e8e\u8fde\u901a\u56fe\uff0c\u4ece\u67d0\u4e2a\u9876\u70b9\u51fa\u53d1\uff0c\u53ef\u4ee5\u5230\u8fbe\u5176\u4f59\u4efb\u610f\u9876\u70b9\u3002
        • \u5bf9\u4e8e\u975e\u8fde\u901a\u56fe\uff0c\u4ece\u67d0\u4e2a\u9876\u70b9\u51fa\u53d1\uff0c\u81f3\u5c11\u6709\u4e00\u4e2a\u9876\u70b9\u65e0\u6cd5\u5230\u8fbe\u3002

        \u56fe 9-3 \u00a0 \u8fde\u901a\u56fe\u4e0e\u975e\u8fde\u901a\u56fe

        \u6211\u4eec\u8fd8\u53ef\u4ee5\u4e3a\u8fb9\u6dfb\u52a0\u201c\u6743\u91cd\u201d\u53d8\u91cf\uff0c\u4ece\u800c\u5f97\u5230\u5982\u56fe 9-4 \u6240\u793a\u7684\u6709\u6743\u56fe\uff08weighted graph\uff09\u3002\u4f8b\u5982\u5728\u300a\u738b\u8005\u8363\u8000\u300b\u7b49\u624b\u6e38\u4e2d\uff0c\u7cfb\u7edf\u4f1a\u6839\u636e\u5171\u540c\u6e38\u620f\u65f6\u95f4\u6765\u8ba1\u7b97\u73a9\u5bb6\u4e4b\u95f4\u7684\u201c\u4eb2\u5bc6\u5ea6\u201d\uff0c\u8fd9\u79cd\u4eb2\u5bc6\u5ea6\u7f51\u7edc\u5c31\u53ef\u4ee5\u7528\u6709\u6743\u56fe\u6765\u8868\u793a\u3002

        \u56fe 9-4 \u00a0 \u6709\u6743\u56fe\u4e0e\u65e0\u6743\u56fe

        \u56fe\u6570\u636e\u7ed3\u6784\u5305\u542b\u4ee5\u4e0b\u5e38\u7528\u672f\u8bed\u3002

        • \u90bb\u63a5\uff08adjacency\uff09\uff1a\u5f53\u4e24\u9876\u70b9\u4e4b\u95f4\u5b58\u5728\u8fb9\u76f8\u8fde\u65f6\uff0c\u79f0\u8fd9\u4e24\u9876\u70b9\u201c\u90bb\u63a5\u201d\u3002\u5728\u56fe 9-4 \u4e2d\uff0c\u9876\u70b9 1 \u7684\u90bb\u63a5\u9876\u70b9\u4e3a\u9876\u70b9 2\u30013\u30015\u3002
        • \u8def\u5f84\uff08path\uff09\uff1a\u4ece\u9876\u70b9 A \u5230\u9876\u70b9 B \u7ecf\u8fc7\u7684\u8fb9\u6784\u6210\u7684\u5e8f\u5217\u88ab\u79f0\u4e3a\u4ece A \u5230 B \u7684\u201c\u8def\u5f84\u201d\u3002\u5728\u56fe 9-4 \u4e2d\uff0c\u8fb9\u5e8f\u5217 1-5-2-4 \u662f\u9876\u70b9 1 \u5230\u9876\u70b9 4 \u7684\u4e00\u6761\u8def\u5f84\u3002
        • \u5ea6\uff08degree\uff09\uff1a\u4e00\u4e2a\u9876\u70b9\u62e5\u6709\u7684\u8fb9\u6570\u3002\u5bf9\u4e8e\u6709\u5411\u56fe\uff0c\u5165\u5ea6\uff08in-degree\uff09\u8868\u793a\u6709\u591a\u5c11\u6761\u8fb9\u6307\u5411\u8be5\u9876\u70b9\uff0c\u51fa\u5ea6\uff08out-degree\uff09\u8868\u793a\u6709\u591a\u5c11\u6761\u8fb9\u4ece\u8be5\u9876\u70b9\u6307\u51fa\u3002
        "},{"location":"chapter_graph/graph/#912","title":"9.1.2 \u00a0 \u56fe\u7684\u8868\u793a","text":"

        \u56fe\u7684\u5e38\u7528\u8868\u793a\u65b9\u5f0f\u5305\u62ec\u201c\u90bb\u63a5\u77e9\u9635\u201d\u548c\u201c\u90bb\u63a5\u8868\u201d\u3002\u4ee5\u4e0b\u4f7f\u7528\u65e0\u5411\u56fe\u8fdb\u884c\u4e3e\u4f8b\u3002

        "},{"location":"chapter_graph/graph/#1","title":"1. \u00a0 \u90bb\u63a5\u77e9\u9635","text":"

        \u8bbe\u56fe\u7684\u9876\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u90bb\u63a5\u77e9\u9635\uff08adjacency matrix\uff09\u4f7f\u7528\u4e00\u4e2a \\(n \\times n\\) \u5927\u5c0f\u7684\u77e9\u9635\u6765\u8868\u793a\u56fe\uff0c\u6bcf\u4e00\u884c\uff08\u5217\uff09\u4ee3\u8868\u4e00\u4e2a\u9876\u70b9\uff0c\u77e9\u9635\u5143\u7d20\u4ee3\u8868\u8fb9\uff0c\u7528 \\(1\\) \u6216 \\(0\\) \u8868\u793a\u4e24\u4e2a\u9876\u70b9\u4e4b\u95f4\u662f\u5426\u5b58\u5728\u8fb9\u3002

        \u5982\u56fe 9-5 \u6240\u793a\uff0c\u8bbe\u90bb\u63a5\u77e9\u9635\u4e3a \\(M\\)\u3001\u9876\u70b9\u5217\u8868\u4e3a \\(V\\) \uff0c\u90a3\u4e48\u77e9\u9635\u5143\u7d20 \\(M[i, j] = 1\\) \u8868\u793a\u9876\u70b9 \\(V[i]\\) \u5230\u9876\u70b9 \\(V[j]\\) \u4e4b\u95f4\u5b58\u5728\u8fb9\uff0c\u53cd\u4e4b \\(M[i, j] = 0\\) \u8868\u793a\u4e24\u9876\u70b9\u4e4b\u95f4\u65e0\u8fb9\u3002

        \u56fe 9-5 \u00a0 \u56fe\u7684\u90bb\u63a5\u77e9\u9635\u8868\u793a

        \u90bb\u63a5\u77e9\u9635\u5177\u6709\u4ee5\u4e0b\u7279\u6027\u3002

        • \u5728\u7b80\u5355\u56fe\u4e2d\uff0c\u9876\u70b9\u4e0d\u80fd\u4e0e\u81ea\u8eab\u76f8\u8fde\uff0c\u6b64\u65f6\u90bb\u63a5\u77e9\u9635\u4e3b\u5bf9\u89d2\u7ebf\u5143\u7d20\u6ca1\u6709\u610f\u4e49\u3002
        • \u5bf9\u4e8e\u65e0\u5411\u56fe\uff0c\u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u7b49\u4ef7\uff0c\u6b64\u65f6\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\u3002
        • \u5c06\u90bb\u63a5\u77e9\u9635\u7684\u5143\u7d20\u4ece \\(1\\) \u548c \\(0\\) \u66ff\u6362\u4e3a\u6743\u91cd\uff0c\u5219\u53ef\u8868\u793a\u6709\u6743\u56fe\u3002

        \u4f7f\u7528\u90bb\u63a5\u77e9\u9635\u8868\u793a\u56fe\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u77e9\u9635\u5143\u7d20\u4ee5\u83b7\u53d6\u8fb9\uff0c\u56e0\u6b64\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u7684\u6548\u7387\u5f88\u9ad8\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(1)\\) \u3002\u7136\u800c\uff0c\u77e9\u9635\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u5185\u5b58\u5360\u7528\u8f83\u591a\u3002

        "},{"location":"chapter_graph/graph/#2","title":"2. \u00a0 \u90bb\u63a5\u8868","text":"

        \u90bb\u63a5\u8868\uff08adjacency list\uff09\u4f7f\u7528 \\(n\\) \u4e2a\u94fe\u8868\u6765\u8868\u793a\u56fe\uff0c\u94fe\u8868\u8282\u70b9\u8868\u793a\u9876\u70b9\u3002\u7b2c \\(i\\) \u4e2a\u94fe\u8868\u5bf9\u5e94\u9876\u70b9 \\(i\\) \uff0c\u5176\u4e2d\u5b58\u50a8\u4e86\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff08\u4e0e\u8be5\u9876\u70b9\u76f8\u8fde\u7684\u9876\u70b9\uff09\u3002\u56fe 9-6 \u5c55\u793a\u4e86\u4e00\u4e2a\u4f7f\u7528\u90bb\u63a5\u8868\u5b58\u50a8\u7684\u56fe\u7684\u793a\u4f8b\u3002

        \u56fe 9-6 \u00a0 \u56fe\u7684\u90bb\u63a5\u8868\u8868\u793a

        \u90bb\u63a5\u8868\u4ec5\u5b58\u50a8\u5b9e\u9645\u5b58\u5728\u7684\u8fb9\uff0c\u800c\u8fb9\u7684\u603b\u6570\u901a\u5e38\u8fdc\u5c0f\u4e8e \\(n^2\\) \uff0c\u56e0\u6b64\u5b83\u66f4\u52a0\u8282\u7701\u7a7a\u95f4\u3002\u7136\u800c\uff0c\u5728\u90bb\u63a5\u8868\u4e2d\u9700\u8981\u901a\u8fc7\u904d\u5386\u94fe\u8868\u6765\u67e5\u627e\u8fb9\uff0c\u56e0\u6b64\u5176\u65f6\u95f4\u6548\u7387\u4e0d\u5982\u90bb\u63a5\u77e9\u9635\u3002

        \u89c2\u5bdf\u56fe 9-6 \uff0c\u90bb\u63a5\u8868\u7ed3\u6784\u4e0e\u54c8\u5e0c\u8868\u4e2d\u7684\u201c\u94fe\u5f0f\u5730\u5740\u201d\u975e\u5e38\u76f8\u4f3c\uff0c\u56e0\u6b64\u6211\u4eec\u4e5f\u53ef\u4ee5\u91c7\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u6765\u4f18\u5316\u6548\u7387\u3002\u6bd4\u5982\u5f53\u94fe\u8868\u8f83\u957f\u65f6\uff0c\u53ef\u4ee5\u5c06\u94fe\u8868\u8f6c\u5316\u4e3a AVL \u6811\u6216\u7ea2\u9ed1\u6811\uff0c\u4ece\u800c\u5c06\u65f6\u95f4\u6548\u7387\u4ece \\(O(n)\\) \u4f18\u5316\u81f3 \\(O(\\log n)\\) \uff1b\u8fd8\u53ef\u4ee5\u628a\u94fe\u8868\u8f6c\u6362\u4e3a\u54c8\u5e0c\u8868\uff0c\u4ece\u800c\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u964d\u81f3 \\(O(1)\\) \u3002

        "},{"location":"chapter_graph/graph/#913","title":"9.1.3 \u00a0 \u56fe\u7684\u5e38\u89c1\u5e94\u7528","text":"

        \u5982\u8868 9-1 \u6240\u793a\uff0c\u8bb8\u591a\u73b0\u5b9e\u7cfb\u7edf\u53ef\u4ee5\u7528\u56fe\u6765\u5efa\u6a21\uff0c\u76f8\u5e94\u7684\u95ee\u9898\u4e5f\u53ef\u4ee5\u7ea6\u5316\u4e3a\u56fe\u8ba1\u7b97\u95ee\u9898\u3002

        \u8868 9-1 \u00a0 \u73b0\u5b9e\u751f\u6d3b\u4e2d\u5e38\u89c1\u7684\u56fe

        \u9876\u70b9 \u8fb9 \u56fe\u8ba1\u7b97\u95ee\u9898 \u793e\u4ea4\u7f51\u7edc \u7528\u6237 \u597d\u53cb\u5173\u7cfb \u6f5c\u5728\u597d\u53cb\u63a8\u8350 \u5730\u94c1\u7ebf\u8def \u7ad9\u70b9 \u7ad9\u70b9\u95f4\u7684\u8fde\u901a\u6027 \u6700\u77ed\u8def\u7ebf\u63a8\u8350 \u592a\u9633\u7cfb \u661f\u4f53 \u661f\u4f53\u95f4\u7684\u4e07\u6709\u5f15\u529b\u4f5c\u7528 \u884c\u661f\u8f68\u9053\u8ba1\u7b97"},{"location":"chapter_graph/graph_operations/","title":"9.2 \u00a0 \u56fe\u7684\u57fa\u7840\u64cd\u4f5c","text":"

        \u56fe\u7684\u57fa\u7840\u64cd\u4f5c\u53ef\u5206\u4e3a\u5bf9\u201c\u8fb9\u201d\u7684\u64cd\u4f5c\u548c\u5bf9\u201c\u9876\u70b9\u201d\u7684\u64cd\u4f5c\u3002\u5728\u201c\u90bb\u63a5\u77e9\u9635\u201d\u548c\u201c\u90bb\u63a5\u8868\u201d\u4e24\u79cd\u8868\u793a\u65b9\u6cd5\u4e0b\uff0c\u5b9e\u73b0\u65b9\u5f0f\u6709\u6240\u4e0d\u540c\u3002

        "},{"location":"chapter_graph/graph_operations/#921","title":"9.2.1 \u00a0 \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u7684\u5b9e\u73b0","text":"

        \u7ed9\u5b9a\u4e00\u4e2a\u9876\u70b9\u6570\u91cf\u4e3a \\(n\\) \u7684\u65e0\u5411\u56fe\uff0c\u5219\u5404\u79cd\u64cd\u4f5c\u7684\u5b9e\u73b0\u65b9\u5f0f\u5982\u56fe 9-7 \u6240\u793a\u3002

        • \u6dfb\u52a0\u6216\u5220\u9664\u8fb9\uff1a\u76f4\u63a5\u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u4fee\u6539\u6307\u5b9a\u7684\u8fb9\u5373\u53ef\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002\u800c\u7531\u4e8e\u662f\u65e0\u5411\u56fe\uff0c\u56e0\u6b64\u9700\u8981\u540c\u65f6\u66f4\u65b0\u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u3002
        • \u6dfb\u52a0\u9876\u70b9\uff1a\u5728\u90bb\u63a5\u77e9\u9635\u7684\u5c3e\u90e8\u6dfb\u52a0\u4e00\u884c\u4e00\u5217\uff0c\u5e76\u5168\u90e8\u586b \\(0\\) \u5373\u53ef\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002
        • \u5220\u9664\u9876\u70b9\uff1a\u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u4e00\u884c\u4e00\u5217\u3002\u5f53\u5220\u9664\u9996\u884c\u9996\u5217\u65f6\u8fbe\u5230\u6700\u5dee\u60c5\u51b5\uff0c\u9700\u8981\u5c06 \\((n-1)^2\\) \u4e2a\u5143\u7d20\u201c\u5411\u5de6\u4e0a\u79fb\u52a8\u201d\uff0c\u4ece\u800c\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002
        • \u521d\u59cb\u5316\uff1a\u4f20\u5165 \\(n\\) \u4e2a\u9876\u70b9\uff0c\u521d\u59cb\u5316\u957f\u5ea6\u4e3a \\(n\\) \u7684\u9876\u70b9\u5217\u8868 vertices \uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff1b\u521d\u59cb\u5316 \\(n \\times n\\) \u5927\u5c0f\u7684\u90bb\u63a5\u77e9\u9635 adjMat \uff0c\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002
        \u521d\u59cb\u5316\u90bb\u63a5\u77e9\u9635\u6dfb\u52a0\u8fb9\u5220\u9664\u8fb9\u6dfb\u52a0\u9876\u70b9\u5220\u9664\u9876\u70b9

        \u56fe 9-7 \u00a0 \u90bb\u63a5\u77e9\u9635\u7684\u521d\u59cb\u5316\u3001\u589e\u5220\u8fb9\u3001\u589e\u5220\u9876\u70b9

        \u4ee5\u4e0b\u662f\u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u8868\u793a\u56fe\u7684\u5b9e\u73b0\u4ee3\u7801\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_adjacency_matrix.py
        class GraphAdjMat:\n    \"\"\"\u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\"\"\"\n\n    def __init__(self, vertices: list[int], edges: list[list[int]]):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        # \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n        self.vertices: list[int] = []\n        # \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n        self.adj_mat: list[list[int]] = []\n        # \u6dfb\u52a0\u9876\u70b9\n        for val in vertices:\n            self.add_vertex(val)\n        # \u6dfb\u52a0\u8fb9\n        # \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        for e in edges:\n            self.add_edge(e[0], e[1])\n\n    def size(self) -> int:\n        \"\"\"\u83b7\u53d6\u9876\u70b9\u6570\u91cf\"\"\"\n        return len(self.vertices)\n\n    def add_vertex(self, val: int):\n        \"\"\"\u6dfb\u52a0\u9876\u70b9\"\"\"\n        n = self.size()\n        # \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n        self.vertices.append(val)\n        # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n        new_row = [0] * n\n        self.adj_mat.append(new_row)\n        # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n        for row in self.adj_mat:\n            row.append(0)\n\n    def remove_vertex(self, index: int):\n        \"\"\"\u5220\u9664\u9876\u70b9\"\"\"\n        if index >= self.size():\n            raise IndexError()\n        # \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n        self.vertices.pop(index)\n        # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n        self.adj_mat.pop(index)\n        # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n        for row in self.adj_mat:\n            row.pop(index)\n\n    def add_edge(self, i: int, j: int):\n        \"\"\"\u6dfb\u52a0\u8fb9\"\"\"\n        # \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        # \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\n            raise IndexError()\n        # \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n        self.adj_mat[i][j] = 1\n        self.adj_mat[j][i] = 1\n\n    def remove_edge(self, i: int, j: int):\n        \"\"\"\u5220\u9664\u8fb9\"\"\"\n        # \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        # \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\n            raise IndexError()\n        self.adj_mat[i][j] = 0\n        self.adj_mat[j][i] = 0\n\n    def print(self):\n        \"\"\"\u6253\u5370\u90bb\u63a5\u77e9\u9635\"\"\"\n        print(\"\u9876\u70b9\u5217\u8868 =\", self.vertices)\n        print(\"\u90bb\u63a5\u77e9\u9635 =\")\n        print_matrix(self.adj_mat)\n
        graph_adjacency_matrix.cpp
        /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n    vector<int> vertices;       // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    vector<vector<int>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n  public:\n    /* \u6784\u9020\u65b9\u6cd5 */\n    GraphAdjMat(const vector<int> &vertices, const vector<vector<int>> &edges) {\n        // \u6dfb\u52a0\u9876\u70b9\n        for (int val : vertices) {\n            addVertex(val);\n        }\n        // \u6dfb\u52a0\u8fb9\n        // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        for (const vector<int> &edge : edges) {\n            addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    int size() const {\n        return vertices.size();\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    void addVertex(int val) {\n        int n = size();\n        // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n        vertices.push_back(val);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n        adjMat.emplace_back(vector<int>(n, 0));\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n        for (vector<int> &row : adjMat) {\n            row.push_back(0);\n        }\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    void removeVertex(int index) {\n        if (index >= size()) {\n            throw out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n        }\n        // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n        vertices.erase(vertices.begin() + index);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n        adjMat.erase(adjMat.begin() + index);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n        for (vector<int> &row : adjMat) {\n            row.erase(row.begin() + index);\n        }\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    void addEdge(int i, int j) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n            throw out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n        }\n        // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n        adjMat[i][j] = 1;\n        adjMat[j][i] = 1;\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    void removeEdge(int i, int j) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n            throw out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n        }\n        adjMat[i][j] = 0;\n        adjMat[j][i] = 0;\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n    void print() {\n        cout << \"\u9876\u70b9\u5217\u8868 = \";\n        printVector(vertices);\n        cout << \"\u90bb\u63a5\u77e9\u9635 =\" << endl;\n        printVectorMatrix(adjMat);\n    }\n};\n
        graph_adjacency_matrix.java
        /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n    List<Integer> vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    List<List<Integer>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public GraphAdjMat(int[] vertices, int[][] edges) {\n        this.vertices = new ArrayList<>();\n        this.adjMat = new ArrayList<>();\n        // \u6dfb\u52a0\u9876\u70b9\n        for (int val : vertices) {\n            addVertex(val);\n        }\n        // \u6dfb\u52a0\u8fb9\n        // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        for (int[] e : edges) {\n            addEdge(e[0], e[1]);\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    public int size() {\n        return vertices.size();\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    public void addVertex(int val) {\n        int n = size();\n        // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n        vertices.add(val);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n        List<Integer> newRow = new ArrayList<>(n);\n        for (int j = 0; j < n; j++) {\n            newRow.add(0);\n        }\n        adjMat.add(newRow);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n        for (List<Integer> row : adjMat) {\n            row.add(0);\n        }\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    public void removeVertex(int index) {\n        if (index >= size())\n            throw new IndexOutOfBoundsException();\n        // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n        vertices.remove(index);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n        adjMat.remove(index);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n        for (List<Integer> row : adjMat) {\n            row.remove(index);\n        }\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    public void addEdge(int i, int j) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw new IndexOutOfBoundsException();\n        // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n        adjMat.get(i).set(j, 1);\n        adjMat.get(j).set(i, 1);\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    public void removeEdge(int i, int j) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw new IndexOutOfBoundsException();\n        adjMat.get(i).set(j, 0);\n        adjMat.get(j).set(i, 0);\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n    public void print() {\n        System.out.print(\"\u9876\u70b9\u5217\u8868 = \");\n        System.out.println(vertices);\n        System.out.println(\"\u90bb\u63a5\u77e9\u9635 =\");\n        PrintUtil.printMatrix(adjMat);\n    }\n}\n
        graph_adjacency_matrix.cs
        /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n    List<int> vertices;     // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    List<List<int>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n    /* \u6784\u9020\u51fd\u6570 */\n    public GraphAdjMat(int[] vertices, int[][] edges) {\n        this.vertices = [];\n        this.adjMat = [];\n        // \u6dfb\u52a0\u9876\u70b9\n        foreach (int val in vertices) {\n            AddVertex(val);\n        }\n        // \u6dfb\u52a0\u8fb9\n        // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        foreach (int[] e in edges) {\n            AddEdge(e[0], e[1]);\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    int Size() {\n        return vertices.Count;\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    public void AddVertex(int val) {\n        int n = Size();\n        // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n        vertices.Add(val);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n        List<int> newRow = new(n);\n        for (int j = 0; j < n; j++) {\n            newRow.Add(0);\n        }\n        adjMat.Add(newRow);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n        foreach (List<int> row in adjMat) {\n            row.Add(0);\n        }\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    public void RemoveVertex(int index) {\n        if (index >= Size())\n            throw new IndexOutOfRangeException();\n        // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n        vertices.RemoveAt(index);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n        adjMat.RemoveAt(index);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n        foreach (List<int> row in adjMat) {\n            row.RemoveAt(index);\n        }\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    public void AddEdge(int i, int j) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)\n            throw new IndexOutOfRangeException();\n        // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n        adjMat[i][j] = 1;\n        adjMat[j][i] = 1;\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    public void RemoveEdge(int i, int j) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)\n            throw new IndexOutOfRangeException();\n        adjMat[i][j] = 0;\n        adjMat[j][i] = 0;\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n    public void Print() {\n        Console.Write(\"\u9876\u70b9\u5217\u8868 = \");\n        PrintUtil.PrintList(vertices);\n        Console.WriteLine(\"\u90bb\u63a5\u77e9\u9635 =\");\n        PrintUtil.PrintMatrix(adjMat);\n    }\n}\n
        graph_adjacency_matrix.go
        /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\ntype graphAdjMat struct {\n    // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    vertices []int\n    // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    adjMat [][]int\n}\n\n/* \u6784\u9020\u51fd\u6570 */\nfunc newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat {\n    // \u6dfb\u52a0\u9876\u70b9\n    n := len(vertices)\n    adjMat := make([][]int, n)\n    for i := range adjMat {\n        adjMat[i] = make([]int, n)\n    }\n    // \u521d\u59cb\u5316\u56fe\n    g := &graphAdjMat{\n        vertices: vertices,\n        adjMat:   adjMat,\n    }\n    // \u6dfb\u52a0\u8fb9\n    // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    for i := range edges {\n        g.addEdge(edges[i][0], edges[i][1])\n    }\n    return g\n}\n\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc (g *graphAdjMat) size() int {\n    return len(g.vertices)\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc (g *graphAdjMat) addVertex(val int) {\n    n := g.size()\n    // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n    g.vertices = append(g.vertices, val)\n    // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n    newRow := make([]int, n)\n    g.adjMat = append(g.adjMat, newRow)\n    // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n    for i := range g.adjMat {\n        g.adjMat[i] = append(g.adjMat[i], 0)\n    }\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nfunc (g *graphAdjMat) removeVertex(index int) {\n    if index >= g.size() {\n        return\n    }\n    // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n    g.vertices = append(g.vertices[:index], g.vertices[index+1:]...)\n    // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n    g.adjMat = append(g.adjMat[:index], g.adjMat[index+1:]...)\n    // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n    for i := range g.adjMat {\n        g.adjMat[i] = append(g.adjMat[i][:index], g.adjMat[i][index+1:]...)\n    }\n}\n\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc (g *graphAdjMat) addEdge(i, j int) {\n    // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n    if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\n        fmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n    }\n    // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n    g.adjMat[i][j] = 1\n    g.adjMat[j][i] = 1\n}\n\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc (g *graphAdjMat) removeEdge(i, j int) {\n    // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n    if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\n        fmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n    }\n    g.adjMat[i][j] = 0\n    g.adjMat[j][i] = 0\n}\n\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nfunc (g *graphAdjMat) print() {\n    fmt.Printf(\"\\t\u9876\u70b9\u5217\u8868 = %v\\n\", g.vertices)\n    fmt.Printf(\"\\t\u90bb\u63a5\u77e9\u9635 = \\n\")\n    for i := range g.adjMat {\n        fmt.Printf(\"\\t\\t\\t%v\\n\", g.adjMat[i])\n    }\n}\n
        graph_adjacency_matrix.swift
        /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n    private var vertices: [Int] // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    private var adjMat: [[Int]] // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    init(vertices: [Int], edges: [[Int]]) {\n        self.vertices = []\n        adjMat = []\n        // \u6dfb\u52a0\u9876\u70b9\n        for val in vertices {\n            addVertex(val: val)\n        }\n        // \u6dfb\u52a0\u8fb9\n        // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        for e in edges {\n            addEdge(i: e[0], j: e[1])\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    func size() -> Int {\n        vertices.count\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    func addVertex(val: Int) {\n        let n = size()\n        // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n        vertices.append(val)\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n        let newRow = Array(repeating: 0, count: n)\n        adjMat.append(newRow)\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n        for i in adjMat.indices {\n            adjMat[i].append(0)\n        }\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    func removeVertex(index: Int) {\n        if index >= size() {\n            fatalError(\"\u8d8a\u754c\")\n        }\n        // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n        vertices.remove(at: index)\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n        adjMat.remove(at: index)\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n        for i in adjMat.indices {\n            adjMat[i].remove(at: index)\n        }\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    func addEdge(i: Int, j: Int) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if i < 0 || j < 0 || i >= size() || j >= size() || i == j {\n            fatalError(\"\u8d8a\u754c\")\n        }\n        // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n        adjMat[i][j] = 1\n        adjMat[j][i] = 1\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    func removeEdge(i: Int, j: Int) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if i < 0 || j < 0 || i >= size() || j >= size() || i == j {\n            fatalError(\"\u8d8a\u754c\")\n        }\n        adjMat[i][j] = 0\n        adjMat[j][i] = 0\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n    func print() {\n        Swift.print(\"\u9876\u70b9\u5217\u8868 = \", terminator: \"\")\n        Swift.print(vertices)\n        Swift.print(\"\u90bb\u63a5\u77e9\u9635 =\")\n        PrintUtil.printMatrix(matrix: adjMat)\n    }\n}\n
        graph_adjacency_matrix.js
        /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n    vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n    /* \u6784\u9020\u51fd\u6570 */\n    constructor(vertices, edges) {\n        this.vertices = [];\n        this.adjMat = [];\n        // \u6dfb\u52a0\u9876\u70b9\n        for (const val of vertices) {\n            this.addVertex(val);\n        }\n        // \u6dfb\u52a0\u8fb9\n        // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        for (const e of edges) {\n            this.addEdge(e[0], e[1]);\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    size() {\n        return this.vertices.length;\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    addVertex(val) {\n        const n = this.size();\n        // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n        this.vertices.push(val);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n        const newRow = [];\n        for (let j = 0; j < n; j++) {\n            newRow.push(0);\n        }\n        this.adjMat.push(newRow);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n        for (const row of this.adjMat) {\n            row.push(0);\n        }\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    removeVertex(index) {\n        if (index >= this.size()) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n        this.vertices.splice(index, 1);\n\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n        this.adjMat.splice(index, 1);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n        for (const row of this.adjMat) {\n            row.splice(index, 1);\n        }\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    addEdge(i, j) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) === (j, i)\n        this.adjMat[i][j] = 1;\n        this.adjMat[j][i] = 1;\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    removeEdge(i, j) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        this.adjMat[i][j] = 0;\n        this.adjMat[j][i] = 0;\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n    print() {\n        console.log('\u9876\u70b9\u5217\u8868 = ', this.vertices);\n        console.log('\u90bb\u63a5\u77e9\u9635 =', this.adjMat);\n    }\n}\n
        graph_adjacency_matrix.ts
        /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n    vertices: number[]; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    adjMat: number[][]; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n    /* \u6784\u9020\u51fd\u6570 */\n    constructor(vertices: number[], edges: number[][]) {\n        this.vertices = [];\n        this.adjMat = [];\n        // \u6dfb\u52a0\u9876\u70b9\n        for (const val of vertices) {\n            this.addVertex(val);\n        }\n        // \u6dfb\u52a0\u8fb9\n        // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        for (const e of edges) {\n            this.addEdge(e[0], e[1]);\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    size(): number {\n        return this.vertices.length;\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    addVertex(val: number): void {\n        const n: number = this.size();\n        // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n        this.vertices.push(val);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n        const newRow: number[] = [];\n        for (let j: number = 0; j < n; j++) {\n            newRow.push(0);\n        }\n        this.adjMat.push(newRow);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n        for (const row of this.adjMat) {\n            row.push(0);\n        }\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    removeVertex(index: number): void {\n        if (index >= this.size()) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n        this.vertices.splice(index, 1);\n\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n        this.adjMat.splice(index, 1);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n        for (const row of this.adjMat) {\n            row.splice(index, 1);\n        }\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    addEdge(i: number, j: number): void {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) === (j, i)\n        this.adjMat[i][j] = 1;\n        this.adjMat[j][i] = 1;\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    removeEdge(i: number, j: number): void {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        this.adjMat[i][j] = 0;\n        this.adjMat[j][i] = 0;\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n    print(): void {\n        console.log('\u9876\u70b9\u5217\u8868 = ', this.vertices);\n        console.log('\u90bb\u63a5\u77e9\u9635 =', this.adjMat);\n    }\n}\n
        graph_adjacency_matrix.dart
        /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n  List<int> vertices = []; // \u9876\u70b9\u5143\u7d20\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n  List<List<int>> adjMat = []; //\u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n  /* \u6784\u9020\u65b9\u6cd5 */\n  GraphAdjMat(List<int> vertices, List<List<int>> edges) {\n    this.vertices = [];\n    this.adjMat = [];\n    // \u6dfb\u52a0\u9876\u70b9\n    for (int val in vertices) {\n      addVertex(val);\n    }\n    // \u6dfb\u52a0\u8fb9\n    // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    for (List<int> e in edges) {\n      addEdge(e[0], e[1]);\n    }\n  }\n\n  /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n  int size() {\n    return vertices.length;\n  }\n\n  /* \u6dfb\u52a0\u9876\u70b9 */\n  void addVertex(int val) {\n    int n = size();\n    // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n    vertices.add(val);\n    // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n    List<int> newRow = List.filled(n, 0, growable: true);\n    adjMat.add(newRow);\n    // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n    for (List<int> row in adjMat) {\n      row.add(0);\n    }\n  }\n\n  /* \u5220\u9664\u9876\u70b9 */\n  void removeVertex(int index) {\n    if (index >= size()) {\n      throw IndexError;\n    }\n    // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n    vertices.removeAt(index);\n    // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n    adjMat.removeAt(index);\n    // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n    for (List<int> row in adjMat) {\n      row.removeAt(index);\n    }\n  }\n\n  /* \u6dfb\u52a0\u8fb9 */\n  // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n  void addEdge(int i, int j) {\n    // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n    if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n      throw IndexError;\n    }\n    // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n    adjMat[i][j] = 1;\n    adjMat[j][i] = 1;\n  }\n\n  /* \u5220\u9664\u8fb9 */\n  // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n  void removeEdge(int i, int j) {\n    // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n    if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n      throw IndexError;\n    }\n    adjMat[i][j] = 0;\n    adjMat[j][i] = 0;\n  }\n\n  /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n  void printAdjMat() {\n    print(\"\u9876\u70b9\u5217\u8868 = $vertices\");\n    print(\"\u90bb\u63a5\u77e9\u9635 = \");\n    printMatrix(adjMat);\n  }\n}\n
        graph_adjacency_matrix.rs
        /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u578b */\npub struct GraphAdjMat {\n    // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    pub vertices: Vec<i32>,\n    // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    pub adj_mat: Vec<Vec<i32>>,\n}\n\nimpl GraphAdjMat {\n    /* \u6784\u9020\u65b9\u6cd5 */\n    pub fn new(vertices: Vec<i32>, edges: Vec<[usize; 2]>) -> Self {\n        let mut graph = GraphAdjMat {\n            vertices: vec![],\n            adj_mat: vec![],\n        };\n        // \u6dfb\u52a0\u9876\u70b9\n        for val in vertices {\n            graph.add_vertex(val);\n        }\n        // \u6dfb\u52a0\u8fb9\n        // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        for edge in edges {\n            graph.add_edge(edge[0], edge[1])\n        }\n\n        graph\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    pub fn size(&self) -> usize {\n        self.vertices.len()\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    pub fn add_vertex(&mut self, val: i32) {\n        let n = self.size();\n        // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n        self.vertices.push(val);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n        self.adj_mat.push(vec![0; n]);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n        for row in self.adj_mat.iter_mut() {\n            row.push(0);\n        }\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    pub fn remove_vertex(&mut self, index: usize) {\n        if index >= self.size() {\n            panic!(\"index error\")\n        }\n        // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n        self.vertices.remove(index);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n        self.adj_mat.remove(index);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n        for row in self.adj_mat.iter_mut() {\n            row.remove(index);\n        }\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    pub fn add_edge(&mut self, i: usize, j: usize) {\n        // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if i >= self.size() || j >= self.size() || i == j {\n            panic!(\"index error\")\n        }\n        // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n        self.adj_mat[i][j] = 1;\n        self.adj_mat[j][i] = 1;\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    pub fn remove_edge(&mut self, i: usize, j: usize) {\n        // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if i >= self.size() || j >= self.size() || i == j {\n            panic!(\"index error\")\n        }\n        self.adj_mat[i][j] = 0;\n        self.adj_mat[j][i] = 0;\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n    pub fn print(&self) {\n        println!(\"\u9876\u70b9\u5217\u8868 = {:?}\", self.vertices);\n        println!(\"\u90bb\u63a5\u77e9\u9635 =\");\n        println!(\"[\");\n        for row in &self.adj_mat {\n            println!(\"  {:?},\", row);\n        }\n        println!(\"]\")\n    }\n}\n
        graph_adjacency_matrix.c
        /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7ed3\u6784\u4f53 */\ntypedef struct {\n    int vertices[MAX_SIZE];\n    int adjMat[MAX_SIZE][MAX_SIZE];\n    int size;\n} GraphAdjMat;\n\n/* \u6784\u9020\u51fd\u6570 */\nGraphAdjMat *newGraphAdjMat() {\n    GraphAdjMat *graph = (GraphAdjMat *)malloc(sizeof(GraphAdjMat));\n    graph->size = 0;\n    for (int i = 0; i < MAX_SIZE; i++) {\n        for (int j = 0; j < MAX_SIZE; j++) {\n            graph->adjMat[i][j] = 0;\n        }\n    }\n    return graph;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delGraphAdjMat(GraphAdjMat *graph) {\n    free(graph);\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(GraphAdjMat *graph, int val) {\n    if (graph->size == MAX_SIZE) {\n        fprintf(stderr, \"\u56fe\u7684\u9876\u70b9\u6570\u91cf\u5df2\u8fbe\u6700\u5927\u503c\\n\");\n        return;\n    }\n    // \u6dfb\u52a0\u7b2c n \u4e2a\u9876\u70b9\uff0c\u5e76\u5c06\u7b2c n \u884c\u548c\u5217\u7f6e\u96f6\n    int n = graph->size;\n    graph->vertices[n] = val;\n    for (int i = 0; i <= n; i++) {\n        graph->adjMat[n][i] = graph->adjMat[i][n] = 0;\n    }\n    graph->size++;\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(GraphAdjMat *graph, int index) {\n    if (index < 0 || index >= graph->size) {\n        fprintf(stderr, \"\u9876\u70b9\u7d22\u5f15\u8d8a\u754c\\n\");\n        return;\n    }\n    // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n    for (int i = index; i < graph->size - 1; i++) {\n        graph->vertices[i] = graph->vertices[i + 1];\n    }\n    // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n    for (int i = index; i < graph->size - 1; i++) {\n        for (int j = 0; j < graph->size; j++) {\n            graph->adjMat[i][j] = graph->adjMat[i + 1][j];\n        }\n    }\n    // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n    for (int i = 0; i < graph->size; i++) {\n        for (int j = index; j < graph->size - 1; j++) {\n            graph->adjMat[i][j] = graph->adjMat[i][j + 1];\n        }\n    }\n    graph->size--;\n}\n\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid addEdge(GraphAdjMat *graph, int i, int j) {\n    if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n        fprintf(stderr, \"\u8fb9\u7d22\u5f15\u8d8a\u754c\u6216\u76f8\u7b49\\n\");\n        return;\n    }\n    graph->adjMat[i][j] = 1;\n    graph->adjMat[j][i] = 1;\n}\n\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid removeEdge(GraphAdjMat *graph, int i, int j) {\n    if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n        fprintf(stderr, \"\u8fb9\u7d22\u5f15\u8d8a\u754c\u6216\u76f8\u7b49\\n\");\n        return;\n    }\n    graph->adjMat[i][j] = 0;\n    graph->adjMat[j][i] = 0;\n}\n\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nvoid printGraphAdjMat(GraphAdjMat *graph) {\n    printf(\"\u9876\u70b9\u5217\u8868 = \");\n    printArray(graph->vertices, graph->size);\n    printf(\"\u90bb\u63a5\u77e9\u9635 =\\n\");\n    for (int i = 0; i < graph->size; i++) {\n        printArray(graph->adjMat[i], graph->size);\n    }\n}\n
        graph_adjacency_matrix.kt
        /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {\n    val vertices = mutableListOf<Int>() // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    val adjMat = mutableListOf<MutableList<Int>>() // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    init {\n        // \u6dfb\u52a0\u9876\u70b9\n        for (vertex in vertices) {\n            addVertex(vertex)\n        }\n        // \u6dfb\u52a0\u8fb9\n        // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        for (edge in edges) {\n            addEdge(edge[0], edge[1])\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    fun size(): Int {\n        return vertices.size\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    fun addVertex(_val: Int) {\n        val n = size()\n        // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n        vertices.add(_val)\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n        val newRow = mutableListOf<Int>()\n        for (j in 0..<n) {\n            newRow.add(0)\n        }\n        adjMat.add(newRow)\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n        for (row in adjMat) {\n            row.add(0)\n        }\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    fun removeVertex(index: Int) {\n        if (index >= size())\n            throw IndexOutOfBoundsException()\n        // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n        vertices.removeAt(index)\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n        adjMat.removeAt(index)\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n        for (row in adjMat) {\n            row.removeAt(index)\n        }\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    fun addEdge(i: Int, j: Int) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw IndexOutOfBoundsException()\n        // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n        adjMat[i][j] = 1\n        adjMat[j][i] = 1\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    fun removeEdge(i: Int, j: Int) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw IndexOutOfBoundsException()\n        adjMat[i][j] = 0\n        adjMat[j][i] = 0\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n    fun print() {\n        print(\"\u9876\u70b9\u5217\u8868 = \")\n        println(vertices)\n        println(\"\u90bb\u63a5\u77e9\u9635 =\")\n        printMatrix(adjMat)\n    }\n}\n
        graph_adjacency_matrix.rb
        ### \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b ###\nclass GraphAdjMat\n  def initialize(vertices, edges)\n    ### \u6784\u9020\u65b9\u6cd5 ###\n    # \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    @vertices = []\n    # \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    @adj_mat = []\n    # \u6dfb\u52a0\u9876\u70b9\n    vertices.each { |val| add_vertex(val) }\n    # \u6dfb\u52a0\u8fb9\n    # \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    edges.each { |e| add_edge(e[0], e[1]) }\n  end\n\n  ### \u83b7\u53d6\u9876\u70b9\u6570\u91cf ###\n  def size\n    @vertices.length\n  end\n\n  ### \u6dfb\u52a0\u9876\u70b9 ###\n  def add_vertex(val)\n    n = size\n    # \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n    @vertices << val\n    # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n    new_row = Array.new(n, 0)\n    @adj_mat << new_row\n    # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n    @adj_mat.each { |row| row << 0 }\n  end\n\n  ### \u5220\u9664\u9876\u70b9 ###\n  def remove_vertex(index)\n    raise IndexError if index >= size\n\n    # \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n    @vertices.delete_at(index)\n    # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n    @adj_mat.delete_at(index)\n    # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n    @adj_mat.each { |row| row.delete_at(index) }\n  end\n\n  ### \u6dfb\u52a0\u8fb9 ###\n  def add_edge(i, j)\n    # \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    # \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n    if i < 0 || j < 0 || i >= size || j >= size || i == j\n      raise IndexError\n    end\n    # \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n    @adj_mat[i][j] = 1\n    @adj_mat[j][i] = 1\n  end\n\n  ### \u5220\u9664\u8fb9 ###\n  def remove_edge(i, j)\n    # \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    # \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n    if i < 0 || j < 0 || i >= size || j >= size || i == j\n      raise IndexError\n    end\n    @adj_mat[i][j] = 0\n    @adj_mat[j][i] = 0\n  end\n\n  ### \u6253\u5370\u90bb\u63a5\u77e9\u9635 ###\n  def __print__\n    puts \"\u9876\u70b9\u5217\u8868 = #{@vertices}\"\n    puts '\u90bb\u63a5\u77e9\u9635 ='\n    print_matrix(@adj_mat)\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_graph/graph_operations/#922","title":"9.2.2 \u00a0 \u57fa\u4e8e\u90bb\u63a5\u8868\u7684\u5b9e\u73b0","text":"

        \u8bbe\u65e0\u5411\u56fe\u7684\u9876\u70b9\u603b\u6570\u4e3a \\(n\\)\u3001\u8fb9\u603b\u6570\u4e3a \\(m\\) \uff0c\u5219\u53ef\u6839\u636e\u56fe 9-8 \u6240\u793a\u7684\u65b9\u6cd5\u5b9e\u73b0\u5404\u79cd\u64cd\u4f5c\u3002

        • \u6dfb\u52a0\u8fb9\uff1a\u5728\u9876\u70b9\u5bf9\u5e94\u94fe\u8868\u7684\u672b\u5c3e\u6dfb\u52a0\u8fb9\u5373\u53ef\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002\u56e0\u4e3a\u662f\u65e0\u5411\u56fe\uff0c\u6240\u4ee5\u9700\u8981\u540c\u65f6\u6dfb\u52a0\u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u3002
        • \u5220\u9664\u8fb9\uff1a\u5728\u9876\u70b9\u5bf9\u5e94\u94fe\u8868\u4e2d\u67e5\u627e\u5e76\u5220\u9664\u6307\u5b9a\u8fb9\uff0c\u4f7f\u7528 \\(O(m)\\) \u65f6\u95f4\u3002\u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u9700\u8981\u540c\u65f6\u5220\u9664\u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u3002
        • \u6dfb\u52a0\u9876\u70b9\uff1a\u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u94fe\u8868\uff0c\u5e76\u5c06\u65b0\u589e\u9876\u70b9\u4f5c\u4e3a\u94fe\u8868\u5934\u8282\u70b9\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002
        • \u5220\u9664\u9876\u70b9\uff1a\u9700\u904d\u5386\u6574\u4e2a\u90bb\u63a5\u8868\uff0c\u5220\u9664\u5305\u542b\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u8fb9\uff0c\u4f7f\u7528 \\(O(n + m)\\) \u65f6\u95f4\u3002
        • \u521d\u59cb\u5316\uff1a\u5728\u90bb\u63a5\u8868\u4e2d\u521b\u5efa \\(n\\) \u4e2a\u9876\u70b9\u548c \\(2m\\) \u6761\u8fb9\uff0c\u4f7f\u7528 \\(O(n + m)\\) \u65f6\u95f4\u3002
        \u521d\u59cb\u5316\u90bb\u63a5\u8868\u6dfb\u52a0\u8fb9\u5220\u9664\u8fb9\u6dfb\u52a0\u9876\u70b9\u5220\u9664\u9876\u70b9

        \u56fe 9-8 \u00a0 \u90bb\u63a5\u8868\u7684\u521d\u59cb\u5316\u3001\u589e\u5220\u8fb9\u3001\u589e\u5220\u9876\u70b9

        \u4ee5\u4e0b\u662f\u90bb\u63a5\u8868\u7684\u4ee3\u7801\u5b9e\u73b0\u3002\u5bf9\u6bd4\u56fe 9-8 \uff0c\u5b9e\u9645\u4ee3\u7801\u6709\u4ee5\u4e0b\u4e0d\u540c\u3002

        • \u4e3a\u4e86\u65b9\u4fbf\u6dfb\u52a0\u4e0e\u5220\u9664\u9876\u70b9\uff0c\u4ee5\u53ca\u7b80\u5316\u4ee3\u7801\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\u6765\u4ee3\u66ff\u94fe\u8868\u3002
        • \u4f7f\u7528\u54c8\u5e0c\u8868\u6765\u5b58\u50a8\u90bb\u63a5\u8868\uff0ckey \u4e3a\u9876\u70b9\u5b9e\u4f8b\uff0cvalue \u4e3a\u8be5\u9876\u70b9\u7684\u90bb\u63a5\u9876\u70b9\u5217\u8868\uff08\u94fe\u8868\uff09\u3002

        \u53e6\u5916\uff0c\u6211\u4eec\u5728\u90bb\u63a5\u8868\u4e2d\u4f7f\u7528 Vertex \u7c7b\u6765\u8868\u793a\u9876\u70b9\uff0c\u8fd9\u6837\u505a\u7684\u539f\u56e0\u662f\uff1a\u5982\u679c\u4e0e\u90bb\u63a5\u77e9\u9635\u4e00\u6837\uff0c\u7528\u5217\u8868\u7d22\u5f15\u6765\u533a\u5206\u4e0d\u540c\u9876\u70b9\uff0c\u90a3\u4e48\u5047\u8bbe\u8981\u5220\u9664\u7d22\u5f15\u4e3a \\(i\\) \u7684\u9876\u70b9\uff0c\u5219\u9700\u904d\u5386\u6574\u4e2a\u90bb\u63a5\u8868\uff0c\u5c06\u6240\u6709\u5927\u4e8e \\(i\\) \u7684\u7d22\u5f15\u5168\u90e8\u51cf \\(1\\) \uff0c\u6548\u7387\u5f88\u4f4e\u3002\u800c\u5982\u679c\u6bcf\u4e2a\u9876\u70b9\u90fd\u662f\u552f\u4e00\u7684 Vertex \u5b9e\u4f8b\uff0c\u5220\u9664\u67d0\u4e00\u9876\u70b9\u4e4b\u540e\u5c31\u65e0\u987b\u6539\u52a8\u5176\u4ed6\u9876\u70b9\u4e86\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_adjacency_list.py
        class GraphAdjList:\n    \"\"\"\u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\"\"\"\n\n    def __init__(self, edges: list[list[Vertex]]):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        # \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n        self.adj_list = dict[Vertex, list[Vertex]]()\n        # \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n        for edge in edges:\n            self.add_vertex(edge[0])\n            self.add_vertex(edge[1])\n            self.add_edge(edge[0], edge[1])\n\n    def size(self) -> int:\n        \"\"\"\u83b7\u53d6\u9876\u70b9\u6570\u91cf\"\"\"\n        return len(self.adj_list)\n\n    def add_edge(self, vet1: Vertex, vet2: Vertex):\n        \"\"\"\u6dfb\u52a0\u8fb9\"\"\"\n        if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n            raise ValueError()\n        # \u6dfb\u52a0\u8fb9 vet1 - vet2\n        self.adj_list[vet1].append(vet2)\n        self.adj_list[vet2].append(vet1)\n\n    def remove_edge(self, vet1: Vertex, vet2: Vertex):\n        \"\"\"\u5220\u9664\u8fb9\"\"\"\n        if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n            raise ValueError()\n        # \u5220\u9664\u8fb9 vet1 - vet2\n        self.adj_list[vet1].remove(vet2)\n        self.adj_list[vet2].remove(vet1)\n\n    def add_vertex(self, vet: Vertex):\n        \"\"\"\u6dfb\u52a0\u9876\u70b9\"\"\"\n        if vet in self.adj_list:\n            return\n        # \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n        self.adj_list[vet] = []\n\n    def remove_vertex(self, vet: Vertex):\n        \"\"\"\u5220\u9664\u9876\u70b9\"\"\"\n        if vet not in self.adj_list:\n            raise ValueError()\n        # \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n        self.adj_list.pop(vet)\n        # \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n        for vertex in self.adj_list:\n            if vet in self.adj_list[vertex]:\n                self.adj_list[vertex].remove(vet)\n\n    def print(self):\n        \"\"\"\u6253\u5370\u90bb\u63a5\u8868\"\"\"\n        print(\"\u90bb\u63a5\u8868 =\")\n        for vertex in self.adj_list:\n            tmp = [v.val for v in self.adj_list[vertex]]\n            print(f\"{vertex.val}: {tmp},\")\n
        graph_adjacency_list.cpp
        /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n  public:\n    // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    unordered_map<Vertex *, vector<Vertex *>> adjList;\n\n    /* \u5728 vector \u4e2d\u5220\u9664\u6307\u5b9a\u8282\u70b9 */\n    void remove(vector<Vertex *> &vec, Vertex *vet) {\n        for (int i = 0; i < vec.size(); i++) {\n            if (vec[i] == vet) {\n                vec.erase(vec.begin() + i);\n                break;\n            }\n        }\n    }\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    GraphAdjList(const vector<vector<Vertex *>> &edges) {\n        // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n        for (const vector<Vertex *> &edge : edges) {\n            addVertex(edge[0]);\n            addVertex(edge[1]);\n            addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    int size() {\n        return adjList.size();\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    void addEdge(Vertex *vet1, Vertex *vet2) {\n        if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n            throw invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n        // \u6dfb\u52a0\u8fb9 vet1 - vet2\n        adjList[vet1].push_back(vet2);\n        adjList[vet2].push_back(vet1);\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    void removeEdge(Vertex *vet1, Vertex *vet2) {\n        if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n            throw invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n        // \u5220\u9664\u8fb9 vet1 - vet2\n        remove(adjList[vet1], vet2);\n        remove(adjList[vet2], vet1);\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    void addVertex(Vertex *vet) {\n        if (adjList.count(vet))\n            return;\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n        adjList[vet] = vector<Vertex *>();\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    void removeVertex(Vertex *vet) {\n        if (!adjList.count(vet))\n            throw invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n        adjList.erase(vet);\n        // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n        for (auto &adj : adjList) {\n            remove(adj.second, vet);\n        }\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u8868 */\n    void print() {\n        cout << \"\u90bb\u63a5\u8868 =\" << endl;\n        for (auto &adj : adjList) {\n            const auto &key = adj.first;\n            const auto &vec = adj.second;\n            cout << key->val << \": \";\n            printVector(vetsToVals(vec));\n        }\n    }\n};\n
        graph_adjacency_list.java
        /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n    // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    Map<Vertex, List<Vertex>> adjList;\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public GraphAdjList(Vertex[][] edges) {\n        this.adjList = new HashMap<>();\n        // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n        for (Vertex[] edge : edges) {\n            addVertex(edge[0]);\n            addVertex(edge[1]);\n            addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    public int size() {\n        return adjList.size();\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    public void addEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw new IllegalArgumentException();\n        // \u6dfb\u52a0\u8fb9 vet1 - vet2\n        adjList.get(vet1).add(vet2);\n        adjList.get(vet2).add(vet1);\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    public void removeEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw new IllegalArgumentException();\n        // \u5220\u9664\u8fb9 vet1 - vet2\n        adjList.get(vet1).remove(vet2);\n        adjList.get(vet2).remove(vet1);\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    public void addVertex(Vertex vet) {\n        if (adjList.containsKey(vet))\n            return;\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n        adjList.put(vet, new ArrayList<>());\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    public void removeVertex(Vertex vet) {\n        if (!adjList.containsKey(vet))\n            throw new IllegalArgumentException();\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n        adjList.remove(vet);\n        // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n        for (List<Vertex> list : adjList.values()) {\n            list.remove(vet);\n        }\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u8868 */\n    public void print() {\n        System.out.println(\"\u90bb\u63a5\u8868 =\");\n        for (Map.Entry<Vertex, List<Vertex>> pair : adjList.entrySet()) {\n            List<Integer> tmp = new ArrayList<>();\n            for (Vertex vertex : pair.getValue())\n                tmp.add(vertex.val);\n            System.out.println(pair.getKey().val + \": \" + tmp + \",\");\n        }\n    }\n}\n
        graph_adjacency_list.cs
        /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n    // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    public Dictionary<Vertex, List<Vertex>> adjList;\n\n    /* \u6784\u9020\u51fd\u6570 */\n    public GraphAdjList(Vertex[][] edges) {\n        adjList = [];\n        // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n        foreach (Vertex[] edge in edges) {\n            AddVertex(edge[0]);\n            AddVertex(edge[1]);\n            AddEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    int Size() {\n        return adjList.Count;\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    public void AddEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\n            throw new InvalidOperationException();\n        // \u6dfb\u52a0\u8fb9 vet1 - vet2\n        adjList[vet1].Add(vet2);\n        adjList[vet2].Add(vet1);\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    public void RemoveEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\n            throw new InvalidOperationException();\n        // \u5220\u9664\u8fb9 vet1 - vet2\n        adjList[vet1].Remove(vet2);\n        adjList[vet2].Remove(vet1);\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    public void AddVertex(Vertex vet) {\n        if (adjList.ContainsKey(vet))\n            return;\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n        adjList.Add(vet, []);\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    public void RemoveVertex(Vertex vet) {\n        if (!adjList.ContainsKey(vet))\n            throw new InvalidOperationException();\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n        adjList.Remove(vet);\n        // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n        foreach (List<Vertex> list in adjList.Values) {\n            list.Remove(vet);\n        }\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u8868 */\n    public void Print() {\n        Console.WriteLine(\"\u90bb\u63a5\u8868 =\");\n        foreach (KeyValuePair<Vertex, List<Vertex>> pair in adjList) {\n            List<int> tmp = [];\n            foreach (Vertex vertex in pair.Value)\n                tmp.Add(vertex.val);\n            Console.WriteLine(pair.Key.val + \": [\" + string.Join(\", \", tmp) + \"],\");\n        }\n    }\n}\n
        graph_adjacency_list.go
        /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\ntype graphAdjList struct {\n    // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    adjList map[Vertex][]Vertex\n}\n\n/* \u6784\u9020\u51fd\u6570 */\nfunc newGraphAdjList(edges [][]Vertex) *graphAdjList {\n    g := &graphAdjList{\n        adjList: make(map[Vertex][]Vertex),\n    }\n    // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n    for _, edge := range edges {\n        g.addVertex(edge[0])\n        g.addVertex(edge[1])\n        g.addEdge(edge[0], edge[1])\n    }\n    return g\n}\n\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc (g *graphAdjList) size() int {\n    return len(g.adjList)\n}\n\n/* \u6dfb\u52a0\u8fb9 */\nfunc (g *graphAdjList) addEdge(vet1 Vertex, vet2 Vertex) {\n    _, ok1 := g.adjList[vet1]\n    _, ok2 := g.adjList[vet2]\n    if !ok1 || !ok2 || vet1 == vet2 {\n        panic(\"error\")\n    }\n    // \u6dfb\u52a0\u8fb9 vet1 - vet2, \u6dfb\u52a0\u533f\u540d struct{},\n    g.adjList[vet1] = append(g.adjList[vet1], vet2)\n    g.adjList[vet2] = append(g.adjList[vet2], vet1)\n}\n\n/* \u5220\u9664\u8fb9 */\nfunc (g *graphAdjList) removeEdge(vet1 Vertex, vet2 Vertex) {\n    _, ok1 := g.adjList[vet1]\n    _, ok2 := g.adjList[vet2]\n    if !ok1 || !ok2 || vet1 == vet2 {\n        panic(\"error\")\n    }\n    // \u5220\u9664\u8fb9 vet1 - vet2\n    g.adjList[vet1] = DeleteSliceElms(g.adjList[vet1], vet2)\n    g.adjList[vet2] = DeleteSliceElms(g.adjList[vet2], vet1)\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc (g *graphAdjList) addVertex(vet Vertex) {\n    _, ok := g.adjList[vet]\n    if ok {\n        return\n    }\n    // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n    g.adjList[vet] = make([]Vertex, 0)\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nfunc (g *graphAdjList) removeVertex(vet Vertex) {\n    _, ok := g.adjList[vet]\n    if !ok {\n        panic(\"error\")\n    }\n    // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n    delete(g.adjList, vet)\n    // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n    for v, list := range g.adjList {\n        g.adjList[v] = DeleteSliceElms(list, vet)\n    }\n}\n\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nfunc (g *graphAdjList) print() {\n    var builder strings.Builder\n    fmt.Printf(\"\u90bb\u63a5\u8868 = \\n\")\n    for k, v := range g.adjList {\n        builder.WriteString(\"\\t\\t\" + strconv.Itoa(k.Val) + \": \")\n        for _, vet := range v {\n            builder.WriteString(strconv.Itoa(vet.Val) + \" \")\n        }\n        fmt.Println(builder.String())\n        builder.Reset()\n    }\n}\n
        graph_adjacency_list.swift
        /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n    // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    public private(set) var adjList: [Vertex: [Vertex]]\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public init(edges: [[Vertex]]) {\n        adjList = [:]\n        // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n        for edge in edges {\n            addVertex(vet: edge[0])\n            addVertex(vet: edge[1])\n            addEdge(vet1: edge[0], vet2: edge[1])\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    public func size() -> Int {\n        adjList.count\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    public func addEdge(vet1: Vertex, vet2: Vertex) {\n        if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\n            fatalError(\"\u53c2\u6570\u9519\u8bef\")\n        }\n        // \u6dfb\u52a0\u8fb9 vet1 - vet2\n        adjList[vet1]?.append(vet2)\n        adjList[vet2]?.append(vet1)\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    public func removeEdge(vet1: Vertex, vet2: Vertex) {\n        if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\n            fatalError(\"\u53c2\u6570\u9519\u8bef\")\n        }\n        // \u5220\u9664\u8fb9 vet1 - vet2\n        adjList[vet1]?.removeAll { $0 == vet2 }\n        adjList[vet2]?.removeAll { $0 == vet1 }\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    public func addVertex(vet: Vertex) {\n        if adjList[vet] != nil {\n            return\n        }\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n        adjList[vet] = []\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    public func removeVertex(vet: Vertex) {\n        if adjList[vet] == nil {\n            fatalError(\"\u53c2\u6570\u9519\u8bef\")\n        }\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n        adjList.removeValue(forKey: vet)\n        // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n        for key in adjList.keys {\n            adjList[key]?.removeAll { $0 == vet }\n        }\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u8868 */\n    public func print() {\n        Swift.print(\"\u90bb\u63a5\u8868 =\")\n        for (vertex, list) in adjList {\n            let list = list.map { $0.val }\n            Swift.print(\"\\(vertex.val): \\(list),\")\n        }\n    }\n}\n
        graph_adjacency_list.js
        /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n    // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    adjList;\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor(edges) {\n        this.adjList = new Map();\n        // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n        for (const edge of edges) {\n            this.addVertex(edge[0]);\n            this.addVertex(edge[1]);\n            this.addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    size() {\n        return this.adjList.size;\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    addEdge(vet1, vet2) {\n        if (\n            !this.adjList.has(vet1) ||\n            !this.adjList.has(vet2) ||\n            vet1 === vet2\n        ) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // \u6dfb\u52a0\u8fb9 vet1 - vet2\n        this.adjList.get(vet1).push(vet2);\n        this.adjList.get(vet2).push(vet1);\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    removeEdge(vet1, vet2) {\n        if (\n            !this.adjList.has(vet1) ||\n            !this.adjList.has(vet2) ||\n            vet1 === vet2 ||\n            this.adjList.get(vet1).indexOf(vet2) === -1\n        ) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // \u5220\u9664\u8fb9 vet1 - vet2\n        this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\n        this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    addVertex(vet) {\n        if (this.adjList.has(vet)) return;\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n        this.adjList.set(vet, []);\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    removeVertex(vet) {\n        if (!this.adjList.has(vet)) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n        this.adjList.delete(vet);\n        // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n        for (const set of this.adjList.values()) {\n            const index = set.indexOf(vet);\n            if (index > -1) {\n                set.splice(index, 1);\n            }\n        }\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u8868 */\n    print() {\n        console.log('\u90bb\u63a5\u8868 =');\n        for (const [key, value] of this.adjList) {\n            const tmp = [];\n            for (const vertex of value) {\n                tmp.push(vertex.val);\n            }\n            console.log(key.val + ': ' + tmp.join());\n        }\n    }\n}\n
        graph_adjacency_list.ts
        /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n    // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    adjList: Map<Vertex, Vertex[]>;\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor(edges: Vertex[][]) {\n        this.adjList = new Map();\n        // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n        for (const edge of edges) {\n            this.addVertex(edge[0]);\n            this.addVertex(edge[1]);\n            this.addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    size(): number {\n        return this.adjList.size;\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    addEdge(vet1: Vertex, vet2: Vertex): void {\n        if (\n            !this.adjList.has(vet1) ||\n            !this.adjList.has(vet2) ||\n            vet1 === vet2\n        ) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // \u6dfb\u52a0\u8fb9 vet1 - vet2\n        this.adjList.get(vet1).push(vet2);\n        this.adjList.get(vet2).push(vet1);\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    removeEdge(vet1: Vertex, vet2: Vertex): void {\n        if (\n            !this.adjList.has(vet1) ||\n            !this.adjList.has(vet2) ||\n            vet1 === vet2 ||\n            this.adjList.get(vet1).indexOf(vet2) === -1\n        ) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // \u5220\u9664\u8fb9 vet1 - vet2\n        this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\n        this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    addVertex(vet: Vertex): void {\n        if (this.adjList.has(vet)) return;\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n        this.adjList.set(vet, []);\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    removeVertex(vet: Vertex): void {\n        if (!this.adjList.has(vet)) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n        this.adjList.delete(vet);\n        // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n        for (const set of this.adjList.values()) {\n            const index: number = set.indexOf(vet);\n            if (index > -1) {\n                set.splice(index, 1);\n            }\n        }\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u8868 */\n    print(): void {\n        console.log('\u90bb\u63a5\u8868 =');\n        for (const [key, value] of this.adjList.entries()) {\n            const tmp = [];\n            for (const vertex of value) {\n                tmp.push(vertex.val);\n            }\n            console.log(key.val + ': ' + tmp.join());\n        }\n    }\n}\n
        graph_adjacency_list.dart
        /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n  // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n  Map<Vertex, List<Vertex>> adjList = {};\n\n  /* \u6784\u9020\u65b9\u6cd5 */\n  GraphAdjList(List<List<Vertex>> edges) {\n    for (List<Vertex> edge in edges) {\n      addVertex(edge[0]);\n      addVertex(edge[1]);\n      addEdge(edge[0], edge[1]);\n    }\n  }\n\n  /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n  int size() {\n    return adjList.length;\n  }\n\n  /* \u6dfb\u52a0\u8fb9 */\n  void addEdge(Vertex vet1, Vertex vet2) {\n    if (!adjList.containsKey(vet1) ||\n        !adjList.containsKey(vet2) ||\n        vet1 == vet2) {\n      throw ArgumentError;\n    }\n    // \u6dfb\u52a0\u8fb9 vet1 - vet2\n    adjList[vet1]!.add(vet2);\n    adjList[vet2]!.add(vet1);\n  }\n\n  /* \u5220\u9664\u8fb9 */\n  void removeEdge(Vertex vet1, Vertex vet2) {\n    if (!adjList.containsKey(vet1) ||\n        !adjList.containsKey(vet2) ||\n        vet1 == vet2) {\n      throw ArgumentError;\n    }\n    // \u5220\u9664\u8fb9 vet1 - vet2\n    adjList[vet1]!.remove(vet2);\n    adjList[vet2]!.remove(vet1);\n  }\n\n  /* \u6dfb\u52a0\u9876\u70b9 */\n  void addVertex(Vertex vet) {\n    if (adjList.containsKey(vet)) return;\n    // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n    adjList[vet] = [];\n  }\n\n  /* \u5220\u9664\u9876\u70b9 */\n  void removeVertex(Vertex vet) {\n    if (!adjList.containsKey(vet)) {\n      throw ArgumentError;\n    }\n    // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n    adjList.remove(vet);\n    // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n    adjList.forEach((key, value) {\n      value.remove(vet);\n    });\n  }\n\n  /* \u6253\u5370\u90bb\u63a5\u8868 */\n  void printAdjList() {\n    print(\"\u90bb\u63a5\u8868 =\");\n    adjList.forEach((key, value) {\n      List<int> tmp = [];\n      for (Vertex vertex in value) {\n        tmp.add(vertex.val);\n      }\n      print(\"${key.val}: $tmp,\");\n    });\n  }\n}\n
        graph_adjacency_list.rs
        /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u578b */\npub struct GraphAdjList {\n    // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    pub adj_list: HashMap<Vertex, Vec<Vertex>>, // maybe HashSet<Vertex> for value part is better?\n}\n\nimpl GraphAdjList {\n    /* \u6784\u9020\u65b9\u6cd5 */\n    pub fn new(edges: Vec<[Vertex; 2]>) -> Self {\n        let mut graph = GraphAdjList {\n            adj_list: HashMap::new(),\n        };\n        // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n        for edge in edges {\n            graph.add_vertex(edge[0]);\n            graph.add_vertex(edge[1]);\n            graph.add_edge(edge[0], edge[1]);\n        }\n\n        graph\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    #[allow(unused)]\n    pub fn size(&self) -> usize {\n        self.adj_list.len()\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    pub fn add_edge(&mut self, vet1: Vertex, vet2: Vertex) {\n        if vet1 == vet2 {\n            panic!(\"value error\");\n        }\n        // \u6dfb\u52a0\u8fb9 vet1 - vet2\n        self.adj_list.entry(vet1).or_default().push(vet2);\n        self.adj_list.entry(vet2).or_default().push(vet1);\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    #[allow(unused)]\n    pub fn remove_edge(&mut self, vet1: Vertex, vet2: Vertex) {\n        if vet1 == vet2 {\n            panic!(\"value error\");\n        }\n        // \u5220\u9664\u8fb9 vet1 - vet2\n        self.adj_list\n            .entry(vet1)\n            .and_modify(|v| v.retain(|&e| e != vet2));\n        self.adj_list\n            .entry(vet2)\n            .and_modify(|v| v.retain(|&e| e != vet1));\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    pub fn add_vertex(&mut self, vet: Vertex) {\n        if self.adj_list.contains_key(&vet) {\n            return;\n        }\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n        self.adj_list.insert(vet, vec![]);\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    #[allow(unused)]\n    pub fn remove_vertex(&mut self, vet: Vertex) {\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n        self.adj_list.remove(&vet);\n        // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n        for list in self.adj_list.values_mut() {\n            list.retain(|&v| v != vet);\n        }\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u8868 */\n    pub fn print(&self) {\n        println!(\"\u90bb\u63a5\u8868 =\");\n        for (vertex, list) in &self.adj_list {\n            let list = list.iter().map(|vertex| vertex.val).collect::<Vec<i32>>();\n            println!(\"{}: {:?},\", vertex.val, list);\n        }\n    }\n}\n
        graph_adjacency_list.c
        /* \u8282\u70b9\u7ed3\u6784\u4f53 */\ntypedef struct AdjListNode {\n    Vertex *vertex;           // \u9876\u70b9\n    struct AdjListNode *next; // \u540e\u7ee7\u8282\u70b9\n} AdjListNode;\n\n/* \u67e5\u627e\u9876\u70b9\u5bf9\u5e94\u7684\u8282\u70b9 */\nAdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {\n    for (int i = 0; i < graph->size; i++) {\n        if (graph->heads[i]->vertex == vet) {\n            return graph->heads[i];\n        }\n    }\n    return NULL;\n}\n\n/* \u6dfb\u52a0\u8fb9\u8f85\u52a9\u51fd\u6570 */\nvoid addEdgeHelper(AdjListNode *head, Vertex *vet) {\n    AdjListNode *node = (AdjListNode *)malloc(sizeof(AdjListNode));\n    node->vertex = vet;\n    // \u5934\u63d2\u6cd5\n    node->next = head->next;\n    head->next = node;\n}\n\n/* \u5220\u9664\u8fb9\u8f85\u52a9\u51fd\u6570 */\nvoid removeEdgeHelper(AdjListNode *head, Vertex *vet) {\n    AdjListNode *pre = head;\n    AdjListNode *cur = head->next;\n    // \u5728\u94fe\u8868\u4e2d\u641c\u7d22 vet \u5bf9\u5e94\u8282\u70b9\n    while (cur != NULL && cur->vertex != vet) {\n        pre = cur;\n        cur = cur->next;\n    }\n    if (cur == NULL)\n        return;\n    // \u5c06 vet \u5bf9\u5e94\u8282\u70b9\u4ece\u94fe\u8868\u4e2d\u5220\u9664\n    pre->next = cur->next;\n    // \u91ca\u653e\u5185\u5b58\n    free(cur);\n}\n\n/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\ntypedef struct {\n    AdjListNode *heads[MAX_SIZE]; // \u8282\u70b9\u6570\u7ec4\n    int size;                     // \u8282\u70b9\u6570\u91cf\n} GraphAdjList;\n\n/* \u6784\u9020\u51fd\u6570 */\nGraphAdjList *newGraphAdjList() {\n    GraphAdjList *graph = (GraphAdjList *)malloc(sizeof(GraphAdjList));\n    if (!graph) {\n        return NULL;\n    }\n    graph->size = 0;\n    for (int i = 0; i < MAX_SIZE; i++) {\n        graph->heads[i] = NULL;\n    }\n    return graph;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delGraphAdjList(GraphAdjList *graph) {\n    for (int i = 0; i < graph->size; i++) {\n        AdjListNode *cur = graph->heads[i];\n        while (cur != NULL) {\n            AdjListNode *next = cur->next;\n            if (cur != graph->heads[i]) {\n                free(cur);\n            }\n            cur = next;\n        }\n        free(graph->heads[i]->vertex);\n        free(graph->heads[i]);\n    }\n    free(graph);\n}\n\n/* \u67e5\u627e\u9876\u70b9\u5bf9\u5e94\u7684\u8282\u70b9 */\nAdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {\n    for (int i = 0; i < graph->size; i++) {\n        if (graph->heads[i]->vertex == vet) {\n            return graph->heads[i];\n        }\n    }\n    return NULL;\n}\n\n/* \u6dfb\u52a0\u8fb9 */\nvoid addEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) {\n    AdjListNode *head1 = findNode(graph, vet1);\n    AdjListNode *head2 = findNode(graph, vet2);\n    assert(head1 != NULL && head2 != NULL && head1 != head2);\n    // \u6dfb\u52a0\u8fb9 vet1 - vet2\n    addEdgeHelper(head1, vet2);\n    addEdgeHelper(head2, vet1);\n}\n\n/* \u5220\u9664\u8fb9 */\nvoid removeEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) {\n    AdjListNode *head1 = findNode(graph, vet1);\n    AdjListNode *head2 = findNode(graph, vet2);\n    assert(head1 != NULL && head2 != NULL);\n    // \u5220\u9664\u8fb9 vet1 - vet2\n    removeEdgeHelper(head1, head2->vertex);\n    removeEdgeHelper(head2, head1->vertex);\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(GraphAdjList *graph, Vertex *vet) {\n    assert(graph != NULL && graph->size < MAX_SIZE);\n    AdjListNode *head = (AdjListNode *)malloc(sizeof(AdjListNode));\n    head->vertex = vet;\n    head->next = NULL;\n    // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n    graph->heads[graph->size++] = head;\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(GraphAdjList *graph, Vertex *vet) {\n    AdjListNode *node = findNode(graph, vet);\n    assert(node != NULL);\n    // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n    AdjListNode *cur = node, *pre = NULL;\n    while (cur) {\n        pre = cur;\n        cur = cur->next;\n        free(pre);\n    }\n    // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n    for (int i = 0; i < graph->size; i++) {\n        cur = graph->heads[i];\n        pre = NULL;\n        while (cur) {\n            pre = cur;\n            cur = cur->next;\n            if (cur && cur->vertex == vet) {\n                pre->next = cur->next;\n                free(cur);\n                break;\n            }\n        }\n    }\n    // \u5c06\u8be5\u9876\u70b9\u4e4b\u540e\u7684\u9876\u70b9\u5411\u524d\u79fb\u52a8\uff0c\u4ee5\u586b\u8865\u7a7a\u7f3a\n    int i;\n    for (i = 0; i < graph->size; i++) {\n        if (graph->heads[i] == node)\n            break;\n    }\n    for (int j = i; j < graph->size - 1; j++) {\n        graph->heads[j] = graph->heads[j + 1];\n    }\n    graph->size--;\n    free(vet);\n}\n
        graph_adjacency_list.kt
        /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList(edges: Array<Array<Vertex?>>) {\n    // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    val adjList = HashMap<Vertex, MutableList<Vertex>>()\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    init {\n        // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n        for (edge in edges) {\n            addVertex(edge[0]!!)\n            addVertex(edge[1]!!)\n            addEdge(edge[0]!!, edge[1]!!)\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    fun size(): Int {\n        return adjList.size\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    fun addEdge(vet1: Vertex, vet2: Vertex) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw IllegalArgumentException()\n        // \u6dfb\u52a0\u8fb9 vet1 - vet2\n        adjList[vet1]?.add(vet2)\n        adjList[vet2]?.add(vet1)\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    fun removeEdge(vet1: Vertex, vet2: Vertex) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw IllegalArgumentException()\n        // \u5220\u9664\u8fb9 vet1 - vet2\n        adjList[vet1]?.remove(vet2)\n        adjList[vet2]?.remove(vet1)\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    fun addVertex(vet: Vertex) {\n        if (adjList.containsKey(vet))\n            return\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n        adjList[vet] = mutableListOf()\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    fun removeVertex(vet: Vertex) {\n        if (!adjList.containsKey(vet))\n            throw IllegalArgumentException()\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n        adjList.remove(vet)\n        // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n        for (list in adjList.values) {\n            list.remove(vet)\n        }\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u8868 */\n    fun print() {\n        println(\"\u90bb\u63a5\u8868 =\")\n        for (pair in adjList.entries) {\n            val tmp = mutableListOf<Int>()\n            for (vertex in pair.value) {\n                tmp.add(vertex._val)\n            }\n            println(\"${pair.key._val}: $tmp,\")\n        }\n    }\n}\n
        graph_adjacency_list.rb
        ### \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b ###\nclass GraphAdjList\n  attr_reader :adj_list\n\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize(edges)\n    # \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    @adj_list = {}\n    # \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n    for edge in edges\n      add_vertex(edge[0])\n      add_vertex(edge[1])\n      add_edge(edge[0], edge[1])\n    end\n  end\n\n  ### \u83b7\u53d6\u9876\u70b9\u6570\u91cf ###\n  def size\n    @adj_list.length\n  end\n\n  ### \u6dfb\u52a0\u8fb9 ###\n  def add_edge(vet1, vet2)\n    raise ArgumentError if !@adj_list.include?(vet1) || !@adj_list.include?(vet2)\n\n    @adj_list[vet1] << vet2\n    @adj_list[vet2] << vet1\n  end\n\n  ### \u5220\u9664\u8fb9 ###\n  def remove_edge(vet1, vet2)\n    raise ArgumentError if !@adj_list.include?(vet1) || !@adj_list.include?(vet2)\n\n    # \u5220\u9664\u8fb9 vet1 - vet2\n    @adj_list[vet1].delete(vet2)\n    @adj_list[vet2].delete(vet1)\n  end\n\n  ### \u6dfb\u52a0\u9876\u70b9 ###\n  def add_vertex(vet)\n    return if @adj_list.include?(vet)\n\n    # \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n    @adj_list[vet] = []\n  end\n\n  ### \u5220\u9664\u9876\u70b9 ###\n  def remove_vertex(vet)\n    raise ArgumentError unless @adj_list.include?(vet)\n\n    # \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n    @adj_list.delete(vet)\n    # \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n    for vertex in @adj_list\n      @adj_list[vertex.first].delete(vet) if @adj_list[vertex.first].include?(vet)\n    end\n  end\n\n  ### \u6253\u5370\u90bb\u63a5\u8868 ###\n  def __print__\n    puts '\u90bb\u63a5\u8868 ='\n    for vertex in @adj_list\n      tmp = @adj_list[vertex.first].map { |v| v.val }\n      puts \"#{vertex.first.val}: #{tmp},\"\n    end\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_graph/graph_operations/#923","title":"9.2.3 \u00a0 \u6548\u7387\u5bf9\u6bd4","text":"

        \u8bbe\u56fe\u4e2d\u5171\u6709 \\(n\\) \u4e2a\u9876\u70b9\u548c \\(m\\) \u6761\u8fb9\uff0c\u8868 9-2 \u5bf9\u6bd4\u4e86\u90bb\u63a5\u77e9\u9635\u548c\u90bb\u63a5\u8868\u7684\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u3002\u8bf7\u6ce8\u610f\uff0c\u90bb\u63a5\u8868\uff08\u94fe\u8868\uff09\u5bf9\u5e94\u672c\u6587\u5b9e\u73b0\uff0c\u800c\u90bb\u63a5\u8868\uff08\u54c8\u5e0c\u8868\uff09\u4e13\u6307\u5c06\u6240\u6709\u94fe\u8868\u66ff\u6362\u4e3a\u54c8\u5e0c\u8868\u540e\u7684\u5b9e\u73b0\u3002

        \u8868 9-2 \u00a0 \u90bb\u63a5\u77e9\u9635\u4e0e\u90bb\u63a5\u8868\u5bf9\u6bd4

        \u90bb\u63a5\u77e9\u9635 \u90bb\u63a5\u8868\uff08\u94fe\u8868\uff09 \u90bb\u63a5\u8868\uff08\u54c8\u5e0c\u8868\uff09 \u5224\u65ad\u662f\u5426\u90bb\u63a5 \\(O(1)\\) \\(O(n)\\) \\(O(1)\\) \u6dfb\u52a0\u8fb9 \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u8fb9 \\(O(1)\\) \\(O(n)\\) \\(O(1)\\) \u6dfb\u52a0\u9876\u70b9 \\(O(n)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u9876\u70b9 \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n)\\) \u5185\u5b58\u7a7a\u95f4\u5360\u7528 \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n + m)\\)

        \u89c2\u5bdf\u8868 9-2 \uff0c\u4f3c\u4e4e\u90bb\u63a5\u8868\uff08\u54c8\u5e0c\u8868\uff09\u7684\u65f6\u95f4\u6548\u7387\u4e0e\u7a7a\u95f4\u6548\u7387\u6700\u4f18\u3002\u4f46\u5b9e\u9645\u4e0a\uff0c\u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u64cd\u4f5c\u8fb9\u7684\u6548\u7387\u66f4\u9ad8\uff0c\u53ea\u9700\u4e00\u6b21\u6570\u7ec4\u8bbf\u95ee\u6216\u8d4b\u503c\u64cd\u4f5c\u5373\u53ef\u3002\u7efc\u5408\u6765\u770b\uff0c\u90bb\u63a5\u77e9\u9635\u4f53\u73b0\u4e86\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\u7684\u539f\u5219\uff0c\u800c\u90bb\u63a5\u8868\u4f53\u73b0\u4e86\u201c\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4\u201d\u7684\u539f\u5219\u3002

        "},{"location":"chapter_graph/graph_traversal/","title":"9.3 \u00a0 \u56fe\u7684\u904d\u5386","text":"

        \u6811\u4ee3\u8868\u7684\u662f\u201c\u4e00\u5bf9\u591a\u201d\u7684\u5173\u7cfb\uff0c\u800c\u56fe\u5219\u5177\u6709\u66f4\u9ad8\u7684\u81ea\u7531\u5ea6\uff0c\u53ef\u4ee5\u8868\u793a\u4efb\u610f\u7684\u201c\u591a\u5bf9\u591a\u201d\u5173\u7cfb\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u6811\u770b\u4f5c\u56fe\u7684\u4e00\u79cd\u7279\u4f8b\u3002\u663e\u7136\uff0c\u6811\u7684\u904d\u5386\u64cd\u4f5c\u4e5f\u662f\u56fe\u7684\u904d\u5386\u64cd\u4f5c\u7684\u4e00\u79cd\u7279\u4f8b\u3002

        \u56fe\u548c\u6811\u90fd\u9700\u8981\u5e94\u7528\u641c\u7d22\u7b97\u6cd5\u6765\u5b9e\u73b0\u904d\u5386\u64cd\u4f5c\u3002\u56fe\u7684\u904d\u5386\u65b9\u5f0f\u4e5f\u53ef\u5206\u4e3a\u4e24\u79cd\uff1a\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u548c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u3002

        "},{"location":"chapter_graph/graph_traversal/#931","title":"9.3.1 \u00a0 \u5e7f\u5ea6\u4f18\u5148\u904d\u5386","text":"

        \u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u7531\u8fd1\u53ca\u8fdc\u7684\u904d\u5386\u65b9\u5f0f\uff0c\u4ece\u67d0\u4e2a\u8282\u70b9\u51fa\u53d1\uff0c\u59cb\u7ec8\u4f18\u5148\u8bbf\u95ee\u8ddd\u79bb\u6700\u8fd1\u7684\u9876\u70b9\uff0c\u5e76\u4e00\u5c42\u5c42\u5411\u5916\u6269\u5f20\u3002\u5982\u56fe 9-9 \u6240\u793a\uff0c\u4ece\u5de6\u4e0a\u89d2\u9876\u70b9\u51fa\u53d1\uff0c\u9996\u5148\u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff0c\u7136\u540e\u904d\u5386\u4e0b\u4e00\u4e2a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u6240\u6709\u9876\u70b9\u8bbf\u95ee\u5b8c\u6bd5\u3002

        \u56fe 9-9 \u00a0 \u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386

        "},{"location":"chapter_graph/graph_traversal/#1","title":"1. \u00a0 \u7b97\u6cd5\u5b9e\u73b0","text":"

        BFS \u901a\u5e38\u501f\u52a9\u961f\u5217\u6765\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002\u961f\u5217\u5177\u6709\u201c\u5148\u5165\u5148\u51fa\u201d\u7684\u6027\u8d28\uff0c\u8fd9\u4e0e BFS \u7684\u201c\u7531\u8fd1\u53ca\u8fdc\u201d\u7684\u601d\u60f3\u5f02\u66f2\u540c\u5de5\u3002

        1. \u5c06\u904d\u5386\u8d77\u59cb\u9876\u70b9 startVet \u52a0\u5165\u961f\u5217\uff0c\u5e76\u5f00\u542f\u5faa\u73af\u3002
        2. \u5728\u5faa\u73af\u7684\u6bcf\u8f6e\u8fed\u4ee3\u4e2d\uff0c\u5f39\u51fa\u961f\u9996\u9876\u70b9\u5e76\u8bb0\u5f55\u8bbf\u95ee\uff0c\u7136\u540e\u5c06\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\u52a0\u5165\u5230\u961f\u5217\u5c3e\u90e8\u3002
        3. \u5faa\u73af\u6b65\u9aa4 2. \uff0c\u76f4\u5230\u6240\u6709\u9876\u70b9\u88ab\u8bbf\u95ee\u5b8c\u6bd5\u540e\u7ed3\u675f\u3002

        \u4e3a\u4e86\u9632\u6b62\u91cd\u590d\u904d\u5386\u9876\u70b9\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u96c6\u5408 visited \u6765\u8bb0\u5f55\u54ea\u4e9b\u8282\u70b9\u5df2\u88ab\u8bbf\u95ee\u3002

        Tip

        \u54c8\u5e0c\u96c6\u5408\u53ef\u4ee5\u770b\u4f5c\u4e00\u4e2a\u53ea\u5b58\u50a8 key \u800c\u4e0d\u5b58\u50a8 value \u7684\u54c8\u5e0c\u8868\uff0c\u5b83\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u590d\u6742\u5ea6\u4e0b\u8fdb\u884c key \u7684\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u3002\u6839\u636e key \u7684\u552f\u4e00\u6027\uff0c\u54c8\u5e0c\u96c6\u5408\u901a\u5e38\u7528\u4e8e\u6570\u636e\u53bb\u91cd\u7b49\u573a\u666f\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_bfs.py
        def graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n    \"\"\"\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\"\"\"\n    # \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    # \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    res = []\n    # \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    visited = set[Vertex]([start_vet])\n    # \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n    que = deque[Vertex]([start_vet])\n    # \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n    while len(que) > 0:\n        vet = que.popleft()  # \u961f\u9996\u9876\u70b9\u51fa\u961f\n        res.append(vet)  # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n        # \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n        for adj_vet in graph.adj_list[vet]:\n            if adj_vet in visited:\n                continue  # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n            que.append(adj_vet)  # \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n            visited.add(adj_vet)  # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n    # \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n    return res\n
        graph_bfs.cpp
        /* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvector<Vertex *> graphBFS(GraphAdjList &graph, Vertex *startVet) {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    vector<Vertex *> res;\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    unordered_set<Vertex *> visited = {startVet};\n    // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n    queue<Vertex *> que;\n    que.push(startVet);\n    // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n    while (!que.empty()) {\n        Vertex *vet = que.front();\n        que.pop();          // \u961f\u9996\u9876\u70b9\u51fa\u961f\n        res.push_back(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n        // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n        for (auto adjVet : graph.adjList[vet]) {\n            if (visited.count(adjVet))\n                continue;            // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n            que.push(adjVet);        // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n            visited.emplace(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n        }\n    }\n    // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n    return res;\n}\n
        graph_bfs.java
        /* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    List<Vertex> res = new ArrayList<>();\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    Set<Vertex> visited = new HashSet<>();\n    visited.add(startVet);\n    // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n    Queue<Vertex> que = new LinkedList<>();\n    que.offer(startVet);\n    // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n    while (!que.isEmpty()) {\n        Vertex vet = que.poll(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n        res.add(vet);            // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n        // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n        for (Vertex adjVet : graph.adjList.get(vet)) {\n            if (visited.contains(adjVet))\n                continue;        // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n            que.offer(adjVet);   // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n            visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n        }\n    }\n    // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n    return res;\n}\n
        graph_bfs.cs
        /* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> GraphBFS(GraphAdjList graph, Vertex startVet) {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    List<Vertex> res = [];\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    HashSet<Vertex> visited = [startVet];\n    // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n    Queue<Vertex> que = new();\n    que.Enqueue(startVet);\n    // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n    while (que.Count > 0) {\n        Vertex vet = que.Dequeue(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n        res.Add(vet);               // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n        foreach (Vertex adjVet in graph.adjList[vet]) {\n            if (visited.Contains(adjVet)) {\n                continue;          // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n            }\n            que.Enqueue(adjVet);   // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n            visited.Add(adjVet);   // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n        }\n    }\n\n    // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n    return res;\n}\n
        graph_bfs.go
        /* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphBFS(g *graphAdjList, startVet Vertex) []Vertex {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    res := make([]Vertex, 0)\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    visited := make(map[Vertex]struct{})\n    visited[startVet] = struct{}{}\n    // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS, \u4f7f\u7528\u5207\u7247\u6a21\u62df\u961f\u5217\n    queue := make([]Vertex, 0)\n    queue = append(queue, startVet)\n    // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n    for len(queue) > 0 {\n        // \u961f\u9996\u9876\u70b9\u51fa\u961f\n        vet := queue[0]\n        queue = queue[1:]\n        // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n        res = append(res, vet)\n        // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n        for _, adjVet := range g.adjList[vet] {\n            _, isExist := visited[adjVet]\n            // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n            if !isExist {\n                queue = append(queue, adjVet)\n                visited[adjVet] = struct{}{}\n            }\n        }\n    }\n    // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n    return res\n}\n
        graph_bfs.swift
        /* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphBFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    var res: [Vertex] = []\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    var visited: Set<Vertex> = [startVet]\n    // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n    var que: [Vertex] = [startVet]\n    // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n    while !que.isEmpty {\n        let vet = que.removeFirst() // \u961f\u9996\u9876\u70b9\u51fa\u961f\n        res.append(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n        // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n        for adjVet in graph.adjList[vet] ?? [] {\n            if visited.contains(adjVet) {\n                continue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n            }\n            que.append(adjVet) // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n            visited.insert(adjVet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n        }\n    }\n    // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n    return res\n}\n
        graph_bfs.js
        /* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphBFS(graph, startVet) {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    const res = [];\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    const visited = new Set();\n    visited.add(startVet);\n    // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n    const que = [startVet];\n    // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n    while (que.length) {\n        const vet = que.shift(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n        res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n        // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n        for (const adjVet of graph.adjList.get(vet) ?? []) {\n            if (visited.has(adjVet)) {\n                continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n            }\n            que.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n            visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n        }\n    }\n    // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n    return res;\n}\n
        graph_bfs.ts
        /* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphBFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    const res: Vertex[] = [];\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    const visited: Set<Vertex> = new Set();\n    visited.add(startVet);\n    // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n    const que = [startVet];\n    // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n    while (que.length) {\n        const vet = que.shift(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n        res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n        // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n        for (const adjVet of graph.adjList.get(vet) ?? []) {\n            if (visited.has(adjVet)) {\n                continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n            }\n            que.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\n            visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n        }\n    }\n    // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n    return res;\n}\n
        graph_bfs.dart
        /* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\nList<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {\n  // \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n  // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n  List<Vertex> res = [];\n  // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n  Set<Vertex> visited = {};\n  visited.add(startVet);\n  // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n  Queue<Vertex> que = Queue();\n  que.add(startVet);\n  // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n  while (que.isNotEmpty) {\n    Vertex vet = que.removeFirst(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n    res.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    for (Vertex adjVet in graph.adjList[vet]!) {\n      if (visited.contains(adjVet)) {\n        continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n      }\n      que.add(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n      visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n    }\n  }\n  // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n  return res;\n}\n
        graph_bfs.rs
        /* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfn graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    let mut res = vec![];\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    let mut visited = HashSet::new();\n    visited.insert(start_vet);\n    // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n    let mut que = VecDeque::new();\n    que.push_back(start_vet);\n    // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n    while let Some(vet) = que.pop_front() {\n        res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n\n        // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n        if let Some(adj_vets) = graph.adj_list.get(&vet) {\n            for &adj_vet in adj_vets {\n                if visited.contains(&adj_vet) {\n                    continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n                }\n                que.push_back(adj_vet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n                visited.insert(adj_vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n            }\n        }\n    }\n    // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n    res\n}\n
        graph_bfs.c
        /* \u8282\u70b9\u961f\u5217\u7ed3\u6784\u4f53 */\ntypedef struct {\n    Vertex *vertices[MAX_SIZE];\n    int front, rear, size;\n} Queue;\n\n/* \u6784\u9020\u51fd\u6570 */\nQueue *newQueue() {\n    Queue *q = (Queue *)malloc(sizeof(Queue));\n    q->front = q->rear = q->size = 0;\n    return q;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nint isEmpty(Queue *q) {\n    return q->size == 0;\n}\n\n/* \u5165\u961f\u64cd\u4f5c */\nvoid enqueue(Queue *q, Vertex *vet) {\n    q->vertices[q->rear] = vet;\n    q->rear = (q->rear + 1) % MAX_SIZE;\n    q->size++;\n}\n\n/* \u51fa\u961f\u64cd\u4f5c */\nVertex *dequeue(Queue *q) {\n    Vertex *vet = q->vertices[q->front];\n    q->front = (q->front + 1) % MAX_SIZE;\n    q->size--;\n    return vet;\n}\n\n/* \u68c0\u67e5\u9876\u70b9\u662f\u5426\u5df2\u88ab\u8bbf\u95ee */\nint isVisited(Vertex **visited, int size, Vertex *vet) {\n    // \u904d\u5386\u67e5\u627e\u8282\u70b9\uff0c\u4f7f\u7528 O(n) \u65f6\u95f4\n    for (int i = 0; i < size; i++) {\n        if (visited[i] == vet)\n            return 1;\n    }\n    return 0;\n}\n\n/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvoid graphBFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize, Vertex **visited, int *visitedSize) {\n    // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n    Queue *queue = newQueue();\n    enqueue(queue, startVet);\n    visited[(*visitedSize)++] = startVet;\n    // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n    while (!isEmpty(queue)) {\n        Vertex *vet = dequeue(queue); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n        res[(*resSize)++] = vet;      // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n        // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n        AdjListNode *node = findNode(graph, vet);\n        while (node != NULL) {\n            // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n            if (!isVisited(visited, *visitedSize, node->vertex)) {\n                enqueue(queue, node->vertex);             // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n                visited[(*visitedSize)++] = node->vertex; // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n            }\n            node = node->next;\n        }\n    }\n    // \u91ca\u653e\u5185\u5b58\n    free(queue);\n}\n
        graph_bfs.kt
        /* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfun graphBFS(graph: GraphAdjList, startVet: Vertex): MutableList<Vertex?> {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    val res = mutableListOf<Vertex?>()\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    val visited = HashSet<Vertex>()\n    visited.add(startVet)\n    // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n    val que = LinkedList<Vertex>()\n    que.offer(startVet)\n    // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n    while (!que.isEmpty()) {\n        val vet = que.poll() // \u961f\u9996\u9876\u70b9\u51fa\u961f\n        res.add(vet)         // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n        // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n        for (adjVet in graph.adjList[vet]!!) {\n            if (visited.contains(adjVet))\n                continue        // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n            que.offer(adjVet)   // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n            visited.add(adjVet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n        }\n    }\n    // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n    return res\n}\n
        graph_bfs.rb
        ### \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 ###\ndef graph_bfs(graph, start_vet)\n  # \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n  # \u9876\u70b9\u904d\u5386\u5e8f\u5217\n  res = []\n  # \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n  visited = Set.new([start_vet])\n  # \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n  que = [start_vet]\n  # \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n  while que.length > 0\n    vet = que.shift # \u961f\u9996\u9876\u70b9\u51fa\u961f\n    res << vet # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    # \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    for adj_vet in graph.adj_list[vet]\n      next if visited.include?(adj_vet) # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n      que << adj_vet # \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n      visited.add(adj_vet) # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n    end\n  end\n  # \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u4ee3\u7801\u76f8\u5bf9\u62bd\u8c61\uff0c\u5efa\u8bae\u5bf9\u7167\u56fe 9-10 \u6765\u52a0\u6df1\u7406\u89e3\u3002

        <1><2><3><4><5><6><7><8><9><10><11>

        \u56fe 9-10 \u00a0 \u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u6b65\u9aa4

        \u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u7684\u5e8f\u5217\u662f\u5426\u552f\u4e00\uff1f

        \u4e0d\u552f\u4e00\u3002\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u53ea\u8981\u6c42\u6309\u201c\u7531\u8fd1\u53ca\u8fdc\u201d\u7684\u987a\u5e8f\u904d\u5386\uff0c\u800c\u591a\u4e2a\u76f8\u540c\u8ddd\u79bb\u7684\u9876\u70b9\u7684\u904d\u5386\u987a\u5e8f\u5141\u8bb8\u88ab\u4efb\u610f\u6253\u4e71\u3002\u4ee5\u56fe 9-10 \u4e3a\u4f8b\uff0c\u9876\u70b9 \\(1\\)\u3001\\(3\\) \u7684\u8bbf\u95ee\u987a\u5e8f\u53ef\u4ee5\u4ea4\u6362\uff0c\u9876\u70b9 \\(2\\)\u3001\\(4\\)\u3001\\(6\\) \u7684\u8bbf\u95ee\u987a\u5e8f\u4e5f\u53ef\u4ee5\u4efb\u610f\u4ea4\u6362\u3002

        "},{"location":"chapter_graph/graph_traversal/#2","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"

        \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u6240\u6709\u9876\u70b9\u90fd\u4f1a\u5165\u961f\u5e76\u51fa\u961f\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(|V|)\\) \u65f6\u95f4\uff1b\u5728\u904d\u5386\u90bb\u63a5\u9876\u70b9\u7684\u8fc7\u7a0b\u4e2d\uff0c\u7531\u4e8e\u662f\u65e0\u5411\u56fe\uff0c\u56e0\u6b64\u6240\u6709\u8fb9\u90fd\u4f1a\u88ab\u8bbf\u95ee \\(2\\) \u6b21\uff0c\u4f7f\u7528 \\(O(2|E|)\\) \u65f6\u95f4\uff1b\u603b\u4f53\u4f7f\u7528 \\(O(|V| + |E|)\\) \u65f6\u95f4\u3002

        \u7a7a\u95f4\u590d\u6742\u5ea6\uff1a\u5217\u8868 res \uff0c\u54c8\u5e0c\u96c6\u5408 visited \uff0c\u961f\u5217 que \u4e2d\u7684\u9876\u70b9\u6570\u91cf\u6700\u591a\u4e3a \\(|V|\\) \uff0c\u4f7f\u7528 \\(O(|V|)\\) \u7a7a\u95f4\u3002

        "},{"location":"chapter_graph/graph_traversal/#932","title":"9.3.2 \u00a0 \u6df1\u5ea6\u4f18\u5148\u904d\u5386","text":"

        \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u4f18\u5148\u8d70\u5230\u5e95\u3001\u65e0\u8def\u53ef\u8d70\u518d\u56de\u5934\u7684\u904d\u5386\u65b9\u5f0f\u3002\u5982\u56fe 9-11 \u6240\u793a\uff0c\u4ece\u5de6\u4e0a\u89d2\u9876\u70b9\u51fa\u53d1\uff0c\u8bbf\u95ee\u5f53\u524d\u9876\u70b9\u7684\u67d0\u4e2a\u90bb\u63a5\u9876\u70b9\uff0c\u76f4\u5230\u8d70\u5230\u5c3d\u5934\u65f6\u8fd4\u56de\uff0c\u518d\u7ee7\u7eed\u8d70\u5230\u5c3d\u5934\u5e76\u8fd4\u56de\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u6240\u6709\u9876\u70b9\u904d\u5386\u5b8c\u6210\u3002

        \u56fe 9-11 \u00a0 \u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386

        "},{"location":"chapter_graph/graph_traversal/#1_1","title":"1. \u00a0 \u7b97\u6cd5\u5b9e\u73b0","text":"

        \u8fd9\u79cd\u201c\u8d70\u5230\u5c3d\u5934\u518d\u8fd4\u56de\u201d\u7684\u7b97\u6cd5\u8303\u5f0f\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u6765\u5b9e\u73b0\u3002\u4e0e\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u7c7b\u4f3c\uff0c\u5728\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u4e2d\uff0c\u6211\u4eec\u4e5f\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u96c6\u5408 visited \u6765\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\uff0c\u4ee5\u907f\u514d\u91cd\u590d\u8bbf\u95ee\u9876\u70b9\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_dfs.py
        def dfs(graph: GraphAdjList, visited: set[Vertex], res: list[Vertex], vet: Vertex):\n    \"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570\"\"\"\n    res.append(vet)  # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    visited.add(vet)  # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n    # \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    for adjVet in graph.adj_list[vet]:\n        if adjVet in visited:\n            continue  # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n        # \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n        dfs(graph, visited, res, adjVet)\n\ndef graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n    \"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386\"\"\"\n    # \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    # \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    res = []\n    # \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    visited = set[Vertex]()\n    dfs(graph, visited, res, start_vet)\n    return res\n
        graph_dfs.cpp
        /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList &graph, unordered_set<Vertex *> &visited, vector<Vertex *> &res, Vertex *vet) {\n    res.push_back(vet);   // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    visited.emplace(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n    // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    for (Vertex *adjVet : graph.adjList[vet]) {\n        if (visited.count(adjVet))\n            continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n        // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvector<Vertex *> graphDFS(GraphAdjList &graph, Vertex *startVet) {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    vector<Vertex *> res;\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    unordered_set<Vertex *> visited;\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
        graph_dfs.java
        /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList graph, Set<Vertex> visited, List<Vertex> res, Vertex vet) {\n    res.add(vet);     // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n    // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    for (Vertex adjVet : graph.adjList.get(vet)) {\n        if (visited.contains(adjVet))\n            continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n        // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    List<Vertex> res = new ArrayList<>();\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    Set<Vertex> visited = new HashSet<>();\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
        graph_dfs.cs
        /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nvoid DFS(GraphAdjList graph, HashSet<Vertex> visited, List<Vertex> res, Vertex vet) {\n    res.Add(vet);     // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    visited.Add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n    // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    foreach (Vertex adjVet in graph.adjList[vet]) {\n        if (visited.Contains(adjVet)) {\n            continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9                             \n        }\n        // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n        DFS(graph, visited, res, adjVet);\n    }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> GraphDFS(GraphAdjList graph, Vertex startVet) {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    List<Vertex> res = [];\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    HashSet<Vertex> visited = [];\n    DFS(graph, visited, res, startVet);\n    return res;\n}\n
        graph_dfs.go
        /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nfunc dfs(g *graphAdjList, visited map[Vertex]struct{}, res *[]Vertex, vet Vertex) {\n    // append \u64cd\u4f5c\u4f1a\u8fd4\u56de\u65b0\u7684\u7684\u5f15\u7528\uff0c\u5fc5\u987b\u8ba9\u539f\u5f15\u7528\u91cd\u65b0\u8d4b\u503c\u4e3a\u65b0slice\u7684\u5f15\u7528\n    *res = append(*res, vet)\n    visited[vet] = struct{}{}\n    // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    for _, adjVet := range g.adjList[vet] {\n        _, isExist := visited[adjVet]\n        // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n        if !isExist {\n            dfs(g, visited, res, adjVet)\n        }\n    }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphDFS(g *graphAdjList, startVet Vertex) []Vertex {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    res := make([]Vertex, 0)\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    visited := make(map[Vertex]struct{})\n    dfs(g, visited, &res, startVet)\n    // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n    return res\n}\n
        graph_dfs.swift
        /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nfunc dfs(graph: GraphAdjList, visited: inout Set<Vertex>, res: inout [Vertex], vet: Vertex) {\n    res.append(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    visited.insert(vet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n    // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    for adjVet in graph.adjList[vet] ?? [] {\n        if visited.contains(adjVet) {\n            continue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n        }\n        // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n        dfs(graph: graph, visited: &visited, res: &res, vet: adjVet)\n    }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphDFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    var res: [Vertex] = []\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    var visited: Set<Vertex> = []\n    dfs(graph: graph, visited: &visited, res: &res, vet: startVet)\n    return res\n}\n
        graph_dfs.js
        /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction dfs(graph, visited, res, vet) {\n    res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n    // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    for (const adjVet of graph.adjList.get(vet)) {\n        if (visited.has(adjVet)) {\n            continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n        }\n        // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphDFS(graph, startVet) {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    const res = [];\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    const visited = new Set();\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
        graph_dfs.ts
        /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nfunction dfs(\n    graph: GraphAdjList,\n    visited: Set<Vertex>,\n    res: Vertex[],\n    vet: Vertex\n): void {\n    res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n    // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    for (const adjVet of graph.adjList.get(vet)) {\n        if (visited.has(adjVet)) {\n            continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n        }\n        // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphDFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    const res: Vertex[] = [];\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    const visited: Set<Vertex> = new Set();\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
        graph_dfs.dart
        /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nvoid dfs(\n  GraphAdjList graph,\n  Set<Vertex> visited,\n  List<Vertex> res,\n  Vertex vet,\n) {\n  res.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n  visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n  // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n  for (Vertex adjVet in graph.adjList[vet]!) {\n    if (visited.contains(adjVet)) {\n      continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n    }\n    // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n    dfs(graph, visited, res, adjVet);\n  }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n  // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n  List<Vertex> res = [];\n  // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n  Set<Vertex> visited = {};\n  dfs(graph, visited, res, startVet);\n  return res;\n}\n
        graph_dfs.rs
        /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nfn dfs(graph: &GraphAdjList, visited: &mut HashSet<Vertex>, res: &mut Vec<Vertex>, vet: Vertex) {\n    res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    visited.insert(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n                         // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    if let Some(adj_vets) = graph.adj_list.get(&vet) {\n        for &adj_vet in adj_vets {\n            if visited.contains(&adj_vet) {\n                continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n            }\n            // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n            dfs(graph, visited, res, adj_vet);\n        }\n    }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfn graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    let mut res = vec![];\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    let mut visited = HashSet::new();\n    dfs(&graph, &mut visited, &mut res, start_vet);\n\n    res\n}\n
        graph_dfs.c
        /* \u68c0\u67e5\u9876\u70b9\u662f\u5426\u5df2\u88ab\u8bbf\u95ee */\nint isVisited(Vertex **res, int size, Vertex *vet) {\n    // \u904d\u5386\u67e5\u627e\u8282\u70b9\uff0c\u4f7f\u7528 O(n) \u65f6\u95f4\n    for (int i = 0; i < size; i++) {\n        if (res[i] == vet) {\n            return 1;\n        }\n    }\n    return 0;\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList *graph, Vertex **res, int *resSize, Vertex *vet) {\n    // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    res[(*resSize)++] = vet;\n    // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    AdjListNode *node = findNode(graph, vet);\n    while (node != NULL) {\n        // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n        if (!isVisited(res, *resSize, node->vertex)) {\n            // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n            dfs(graph, res, resSize, node->vertex);\n        }\n        node = node->next;\n    }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvoid graphDFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize) {\n    dfs(graph, res, resSize, startVet);\n}\n
        graph_dfs.kt
        /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nfun dfs(\n    graph: GraphAdjList,\n    visited: MutableSet<Vertex?>,\n    res: MutableList<Vertex?>,\n    vet: Vertex?\n) {\n    res.add(vet)     // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    visited.add(vet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n    // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    for (adjVet in graph.adjList[vet]!!) {\n        if (visited.contains(adjVet))\n            continue  // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n        // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n        dfs(graph, visited, res, adjVet)\n    }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfun graphDFS(graph: GraphAdjList, startVet: Vertex?): MutableList<Vertex?> {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    val res = mutableListOf<Vertex?>()\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    val visited = HashSet<Vertex?>()\n    dfs(graph, visited, res, startVet)\n    return res\n}\n
        graph_dfs.rb
        ### \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 ###\ndef dfs(graph, visited, res, vet)\n  res << vet # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n  visited.add(vet) # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n  # \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n  for adj_vet in graph.adj_list[vet]\n    next if visited.include?(adj_vet) # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n    # \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n    dfs(graph, visited, res, adj_vet)\n  end\nend\n\n### \u6df1\u5ea6\u4f18\u5148\u904d\u5386 ###\ndef graph_dfs(graph, start_vet)\n  # \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n  # \u9876\u70b9\u904d\u5386\u5e8f\u5217\n  res = []\n  # \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n  visited = Set.new\n  dfs(graph, visited, res, start_vet)\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u7b97\u6cd5\u6d41\u7a0b\u5982\u56fe 9-12 \u6240\u793a\u3002

        • \u76f4\u865a\u7ebf\u4ee3\u8868\u5411\u4e0b\u9012\u63a8\uff0c\u8868\u793a\u5f00\u542f\u4e86\u4e00\u4e2a\u65b0\u7684\u9012\u5f52\u65b9\u6cd5\u6765\u8bbf\u95ee\u65b0\u9876\u70b9\u3002
        • \u66f2\u865a\u7ebf\u4ee3\u8868\u5411\u4e0a\u56de\u6eaf\uff0c\u8868\u793a\u6b64\u9012\u5f52\u65b9\u6cd5\u5df2\u7ecf\u8fd4\u56de\uff0c\u56de\u6eaf\u5230\u4e86\u5f00\u542f\u6b64\u65b9\u6cd5\u7684\u4f4d\u7f6e\u3002

        \u4e3a\u4e86\u52a0\u6df1\u7406\u89e3\uff0c\u5efa\u8bae\u5c06\u56fe 9-12 \u4e0e\u4ee3\u7801\u7ed3\u5408\u8d77\u6765\uff0c\u5728\u8111\u4e2d\u6a21\u62df\uff08\u6216\u8005\u7528\u7b14\u753b\u4e0b\u6765\uff09\u6574\u4e2a DFS \u8fc7\u7a0b\uff0c\u5305\u62ec\u6bcf\u4e2a\u9012\u5f52\u65b9\u6cd5\u4f55\u65f6\u5f00\u542f\u3001\u4f55\u65f6\u8fd4\u56de\u3002

        <1><2><3><4><5><6><7><8><9><10><11>

        \u56fe 9-12 \u00a0 \u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u6b65\u9aa4

        \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u5e8f\u5217\u662f\u5426\u552f\u4e00\uff1f

        \u4e0e\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u7c7b\u4f3c\uff0c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u5e8f\u5217\u7684\u987a\u5e8f\u4e5f\u4e0d\u662f\u552f\u4e00\u7684\u3002\u7ed9\u5b9a\u67d0\u9876\u70b9\uff0c\u5148\u5f80\u54ea\u4e2a\u65b9\u5411\u63a2\u7d22\u90fd\u53ef\u4ee5\uff0c\u5373\u90bb\u63a5\u9876\u70b9\u7684\u987a\u5e8f\u53ef\u4ee5\u4efb\u610f\u6253\u4e71\uff0c\u90fd\u662f\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u3002

        \u4ee5\u6811\u7684\u904d\u5386\u4e3a\u4f8b\uff0c\u201c\u6839 \\(\\rightarrow\\) \u5de6 \\(\\rightarrow\\) \u53f3\u201d\u201c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u201d\u201c\u5de6 \\(\\rightarrow\\) \u53f3 \\(\\rightarrow\\) \u6839\u201d\u5206\u522b\u5bf9\u5e94\u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386\uff0c\u5b83\u4eec\u5c55\u793a\u4e86\u4e09\u79cd\u904d\u5386\u4f18\u5148\u7ea7\uff0c\u7136\u800c\u8fd9\u4e09\u8005\u90fd\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u3002

        "},{"location":"chapter_graph/graph_traversal/#2_1","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"

        \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u6240\u6709\u9876\u70b9\u90fd\u4f1a\u88ab\u8bbf\u95ee \\(1\\) \u6b21\uff0c\u4f7f\u7528 \\(O(|V|)\\) \u65f6\u95f4\uff1b\u6240\u6709\u8fb9\u90fd\u4f1a\u88ab\u8bbf\u95ee \\(2\\) \u6b21\uff0c\u4f7f\u7528 \\(O(2|E|)\\) \u65f6\u95f4\uff1b\u603b\u4f53\u4f7f\u7528 \\(O(|V| + |E|)\\) \u65f6\u95f4\u3002

        \u7a7a\u95f4\u590d\u6742\u5ea6\uff1a\u5217\u8868 res \uff0c\u54c8\u5e0c\u96c6\u5408 visited \u9876\u70b9\u6570\u91cf\u6700\u591a\u4e3a \\(|V|\\) \uff0c\u9012\u5f52\u6df1\u5ea6\u6700\u5927\u4e3a \\(|V|\\) \uff0c\u56e0\u6b64\u4f7f\u7528 \\(O(|V|)\\) \u7a7a\u95f4\u3002

        "},{"location":"chapter_graph/summary/","title":"9.4 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_graph/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u56fe\u7531\u9876\u70b9\u548c\u8fb9\u7ec4\u6210\uff0c\u53ef\u4ee5\u8868\u793a\u4e3a\u4e00\u7ec4\u9876\u70b9\u548c\u4e00\u7ec4\u8fb9\u6784\u6210\u7684\u96c6\u5408\u3002
        • \u76f8\u8f83\u4e8e\u7ebf\u6027\u5173\u7cfb\uff08\u94fe\u8868\uff09\u548c\u5206\u6cbb\u5173\u7cfb\uff08\u6811\uff09\uff0c\u7f51\u7edc\u5173\u7cfb\uff08\u56fe\uff09\u5177\u6709\u66f4\u9ad8\u7684\u81ea\u7531\u5ea6\uff0c\u56e0\u800c\u66f4\u4e3a\u590d\u6742\u3002
        • \u6709\u5411\u56fe\u7684\u8fb9\u5177\u6709\u65b9\u5411\u6027\uff0c\u8fde\u901a\u56fe\u4e2d\u7684\u4efb\u610f\u9876\u70b9\u5747\u53ef\u8fbe\uff0c\u6709\u6743\u56fe\u7684\u6bcf\u6761\u8fb9\u90fd\u5305\u542b\u6743\u91cd\u53d8\u91cf\u3002
        • \u90bb\u63a5\u77e9\u9635\u5229\u7528\u77e9\u9635\u6765\u8868\u793a\u56fe\uff0c\u6bcf\u4e00\u884c\uff08\u5217\uff09\u4ee3\u8868\u4e00\u4e2a\u9876\u70b9\uff0c\u77e9\u9635\u5143\u7d20\u4ee3\u8868\u8fb9\uff0c\u7528 \\(1\\) \u6216 \\(0\\) \u8868\u793a\u4e24\u4e2a\u9876\u70b9\u4e4b\u95f4\u6709\u8fb9\u6216\u65e0\u8fb9\u3002\u90bb\u63a5\u77e9\u9635\u5728\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u4e0a\u6548\u7387\u5f88\u9ad8\uff0c\u4f46\u7a7a\u95f4\u5360\u7528\u8f83\u591a\u3002
        • \u90bb\u63a5\u8868\u4f7f\u7528\u591a\u4e2a\u94fe\u8868\u6765\u8868\u793a\u56fe\uff0c\u7b2c \\(i\\) \u4e2a\u94fe\u8868\u5bf9\u5e94\u9876\u70b9 \\(i\\) \uff0c\u5176\u4e2d\u5b58\u50a8\u4e86\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\u3002\u90bb\u63a5\u8868\u76f8\u5bf9\u4e8e\u90bb\u63a5\u77e9\u9635\u66f4\u52a0\u8282\u7701\u7a7a\u95f4\uff0c\u4f46\u7531\u4e8e\u9700\u8981\u904d\u5386\u94fe\u8868\u6765\u67e5\u627e\u8fb9\uff0c\u56e0\u6b64\u65f6\u95f4\u6548\u7387\u8f83\u4f4e\u3002
        • \u5f53\u90bb\u63a5\u8868\u4e2d\u7684\u94fe\u8868\u8fc7\u957f\u65f6\uff0c\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811\u6216\u54c8\u5e0c\u8868\uff0c\u4ece\u800c\u63d0\u5347\u67e5\u8be2\u6548\u7387\u3002
        • \u4ece\u7b97\u6cd5\u601d\u60f3\u7684\u89d2\u5ea6\u5206\u6790\uff0c\u90bb\u63a5\u77e9\u9635\u4f53\u73b0\u4e86\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\uff0c\u90bb\u63a5\u8868\u4f53\u73b0\u4e86\u201c\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4\u201d\u3002
        • \u56fe\u53ef\u7528\u4e8e\u5efa\u6a21\u5404\u7c7b\u73b0\u5b9e\u7cfb\u7edf\uff0c\u5982\u793e\u4ea4\u7f51\u7edc\u3001\u5730\u94c1\u7ebf\u8def\u7b49\u3002
        • \u6811\u662f\u56fe\u7684\u4e00\u79cd\u7279\u4f8b\uff0c\u6811\u7684\u904d\u5386\u4e5f\u662f\u56fe\u7684\u904d\u5386\u7684\u4e00\u79cd\u7279\u4f8b\u3002
        • \u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u7531\u8fd1\u53ca\u8fdc\u3001\u5c42\u5c42\u6269\u5f20\u7684\u641c\u7d22\u65b9\u5f0f\uff0c\u901a\u5e38\u501f\u52a9\u961f\u5217\u5b9e\u73b0\u3002
        • \u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u4f18\u5148\u8d70\u5230\u5e95\u3001\u65e0\u8def\u53ef\u8d70\u65f6\u518d\u56de\u6eaf\u7684\u641c\u7d22\u65b9\u5f0f\uff0c\u5e38\u57fa\u4e8e\u9012\u5f52\u6765\u5b9e\u73b0\u3002
        "},{"location":"chapter_graph/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u8def\u5f84\u7684\u5b9a\u4e49\u662f\u9876\u70b9\u5e8f\u5217\u8fd8\u662f\u8fb9\u5e8f\u5217\uff1f

        \u7ef4\u57fa\u767e\u79d1\u4e0a\u4e0d\u540c\u8bed\u8a00\u7248\u672c\u7684\u5b9a\u4e49\u4e0d\u4e00\u81f4\uff1a\u82f1\u6587\u7248\u662f\u201c\u8def\u5f84\u662f\u4e00\u4e2a\u8fb9\u5e8f\u5217\u201d\uff0c\u800c\u4e2d\u6587\u7248\u662f\u201c\u8def\u5f84\u662f\u4e00\u4e2a\u9876\u70b9\u5e8f\u5217\u201d\u3002\u4ee5\u4e0b\u662f\u82f1\u6587\u7248\u539f\u6587\uff1aIn graph theory, a path in a graph is a finite or infinite sequence of edges which joins a sequence of vertices.

        \u5728\u672c\u6587\u4e2d\uff0c\u8def\u5f84\u88ab\u89c6\u4e3a\u4e00\u4e2a\u8fb9\u5e8f\u5217\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u9876\u70b9\u5e8f\u5217\u3002\u8fd9\u662f\u56e0\u4e3a\u4e24\u4e2a\u9876\u70b9\u4e4b\u95f4\u53ef\u80fd\u5b58\u5728\u591a\u6761\u8fb9\u8fde\u63a5\uff0c\u6b64\u65f6\u6bcf\u6761\u8fb9\u90fd\u5bf9\u5e94\u4e00\u6761\u8def\u5f84\u3002

        Q\uff1a\u975e\u8fde\u901a\u56fe\u4e2d\u662f\u5426\u4f1a\u6709\u65e0\u6cd5\u904d\u5386\u5230\u7684\u70b9\uff1f

        \u5728\u975e\u8fde\u901a\u56fe\u4e2d\uff0c\u4ece\u67d0\u4e2a\u9876\u70b9\u51fa\u53d1\uff0c\u81f3\u5c11\u6709\u4e00\u4e2a\u9876\u70b9\u65e0\u6cd5\u5230\u8fbe\u3002\u904d\u5386\u975e\u8fde\u901a\u56fe\u9700\u8981\u8bbe\u7f6e\u591a\u4e2a\u8d77\u70b9\uff0c\u4ee5\u904d\u5386\u5230\u56fe\u7684\u6240\u6709\u8fde\u901a\u5206\u91cf\u3002

        Q\uff1a\u5728\u90bb\u63a5\u8868\u4e2d\uff0c\u201c\u4e0e\u8be5\u9876\u70b9\u76f8\u8fde\u7684\u6240\u6709\u9876\u70b9\u201d\u7684\u9876\u70b9\u987a\u5e8f\u662f\u5426\u6709\u8981\u6c42\uff1f

        \u53ef\u4ee5\u662f\u4efb\u610f\u987a\u5e8f\u3002\u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u6309\u7167\u6307\u5b9a\u89c4\u5219\u6765\u6392\u5e8f\uff0c\u6bd4\u5982\u6309\u7167\u9876\u70b9\u6dfb\u52a0\u7684\u6b21\u5e8f\uff0c\u6216\u8005\u6309\u7167\u9876\u70b9\u503c\u5927\u5c0f\u7684\u987a\u5e8f\u7b49\uff0c\u8fd9\u6837\u6709\u52a9\u4e8e\u5feb\u901f\u67e5\u627e\u201c\u5e26\u6709\u67d0\u79cd\u6781\u503c\u201d\u7684\u9876\u70b9\u3002

        "},{"location":"chapter_greedy/","title":"\u7b2c 15 \u7ae0 \u00a0 \u8d2a\u5fc3","text":"

        Abstract

        \u5411\u65e5\u8475\u671d\u7740\u592a\u9633\u8f6c\u52a8\uff0c\u65f6\u523b\u8ffd\u6c42\u81ea\u8eab\u6210\u957f\u7684\u6700\u5927\u53ef\u80fd\u3002

        \u8d2a\u5fc3\u7b56\u7565\u5728\u4e00\u8f6e\u8f6e\u7684\u7b80\u5355\u9009\u62e9\u4e2d\uff0c\u9010\u6b65\u5bfc\u5411\u6700\u4f73\u7b54\u6848\u3002

        "},{"location":"chapter_greedy/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 15.1 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5
        • 15.2 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898
        • 15.3 \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898
        • 15.4 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u95ee\u9898
        • 15.5 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_greedy/fractional_knapsack_problem/","title":"15.2 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898","text":"

        Question

        \u7ed9\u5b9a \\(n\\) \u4e2a\u7269\u54c1\uff0c\u7b2c \\(i\\) \u4e2a\u7269\u54c1\u7684\u91cd\u91cf\u4e3a \\(wgt[i-1]\\)\u3001\u4ef7\u503c\u4e3a \\(val[i-1]\\) \uff0c\u548c\u4e00\u4e2a\u5bb9\u91cf\u4e3a \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u4e2a\u7269\u54c1\u53ea\u80fd\u9009\u62e9\u4e00\u6b21\uff0c\u4f46\u53ef\u4ee5\u9009\u62e9\u7269\u54c1\u7684\u4e00\u90e8\u5206\uff0c\u4ef7\u503c\u6839\u636e\u9009\u62e9\u7684\u91cd\u91cf\u6bd4\u4f8b\u8ba1\u7b97\uff0c\u95ee\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u80cc\u5305\u4e2d\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u3002\u793a\u4f8b\u5982\u56fe 15-3 \u6240\u793a\u3002

        \u56fe 15-3 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e

        \u5206\u6570\u80cc\u5305\u95ee\u9898\u548c 0-1 \u80cc\u5305\u95ee\u9898\u6574\u4f53\u4e0a\u975e\u5e38\u76f8\u4f3c\uff0c\u72b6\u6001\u5305\u542b\u5f53\u524d\u7269\u54c1 \\(i\\) \u548c\u5bb9\u91cf \\(c\\) \uff0c\u76ee\u6807\u662f\u6c42\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u7684\u6700\u5927\u4ef7\u503c\u3002

        \u4e0d\u540c\u70b9\u5728\u4e8e\uff0c\u672c\u9898\u5141\u8bb8\u53ea\u9009\u62e9\u7269\u54c1\u7684\u4e00\u90e8\u5206\u3002\u5982\u56fe 15-4 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u7269\u54c1\u4efb\u610f\u5730\u8fdb\u884c\u5207\u5206\uff0c\u5e76\u6309\u7167\u91cd\u91cf\u6bd4\u4f8b\u6765\u8ba1\u7b97\u76f8\u5e94\u4ef7\u503c\u3002

        1. \u5bf9\u4e8e\u7269\u54c1 \\(i\\) \uff0c\u5b83\u5728\u5355\u4f4d\u91cd\u91cf\u4e0b\u7684\u4ef7\u503c\u4e3a \\(val[i-1] / wgt[i-1]\\) \uff0c\u7b80\u79f0\u5355\u4f4d\u4ef7\u503c\u3002
        2. \u5047\u8bbe\u653e\u5165\u4e00\u90e8\u5206\u7269\u54c1 \\(i\\) \uff0c\u91cd\u91cf\u4e3a \\(w\\) \uff0c\u5219\u80cc\u5305\u589e\u52a0\u7684\u4ef7\u503c\u4e3a \\(w \\times val[i-1] / wgt[i-1]\\) \u3002

        \u56fe 15-4 \u00a0 \u7269\u54c1\u5728\u5355\u4f4d\u91cd\u91cf\u4e0b\u7684\u4ef7\u503c

        "},{"location":"chapter_greedy/fractional_knapsack_problem/#1","title":"1. \u00a0 \u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a","text":"

        \u6700\u5927\u5316\u80cc\u5305\u5185\u7269\u54c1\u603b\u4ef7\u503c\uff0c\u672c\u8d28\u4e0a\u662f\u6700\u5927\u5316\u5355\u4f4d\u91cd\u91cf\u4e0b\u7684\u7269\u54c1\u4ef7\u503c\u3002\u7531\u6b64\u4fbf\u53ef\u63a8\u7406\u51fa\u56fe 15-5 \u6240\u793a\u7684\u8d2a\u5fc3\u7b56\u7565\u3002

        1. \u5c06\u7269\u54c1\u6309\u7167\u5355\u4f4d\u4ef7\u503c\u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\u3002
        2. \u904d\u5386\u6240\u6709\u7269\u54c1\uff0c\u6bcf\u8f6e\u8d2a\u5fc3\u5730\u9009\u62e9\u5355\u4f4d\u4ef7\u503c\u6700\u9ad8\u7684\u7269\u54c1\u3002
        3. \u82e5\u5269\u4f59\u80cc\u5305\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u4f7f\u7528\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u586b\u6ee1\u80cc\u5305\u3002

        \u56fe 15-5 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898\u7684\u8d2a\u5fc3\u7b56\u7565

        "},{"location":"chapter_greedy/fractional_knapsack_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u6211\u4eec\u5efa\u7acb\u4e86\u4e00\u4e2a\u7269\u54c1\u7c7b Item \uff0c\u4ee5\u4fbf\u5c06\u7269\u54c1\u6309\u7167\u5355\u4f4d\u4ef7\u503c\u8fdb\u884c\u6392\u5e8f\u3002\u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u5f53\u80cc\u5305\u5df2\u6ee1\u65f6\u8df3\u51fa\u5e76\u8fd4\u56de\u89e3\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby fractional_knapsack.py
        class Item:\n    \"\"\"\u7269\u54c1\"\"\"\n\n    def __init__(self, w: int, v: int):\n        self.w = w  # \u7269\u54c1\u91cd\u91cf\n        self.v = v  # \u7269\u54c1\u4ef7\u503c\n\ndef fractional_knapsack(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"\u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3\"\"\"\n    # \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n    items = [Item(w, v) for w, v in zip(wgt, val)]\n    # \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n    items.sort(key=lambda item: item.v / item.w, reverse=True)\n    # \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n    res = 0\n    for item in items:\n        if item.w <= cap:\n            # \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n            res += item.v\n            cap -= item.w\n        else:\n            # \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n            res += (item.v / item.w) * cap\n            # \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n            break\n    return res\n
        fractional_knapsack.cpp
        /* \u7269\u54c1 */\nclass Item {\n  public:\n    int w; // \u7269\u54c1\u91cd\u91cf\n    int v; // \u7269\u54c1\u4ef7\u503c\n\n    Item(int w, int v) : w(w), v(v) {\n    }\n};\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble fractionalKnapsack(vector<int> &wgt, vector<int> &val, int cap) {\n    // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n    vector<Item> items;\n    for (int i = 0; i < wgt.size(); i++) {\n        items.push_back(Item(wgt[i], val[i]));\n    }\n    // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n    sort(items.begin(), items.end(), [](Item &a, Item &b) { return (double)a.v / a.w > (double)b.v / b.w; });\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n    double res = 0;\n    for (auto &item : items) {\n        if (item.w <= cap) {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n            res += (double)item.v / item.w * cap;\n            // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n            break;\n        }\n    }\n    return res;\n}\n
        fractional_knapsack.java
        /* \u7269\u54c1 */\nclass Item {\n    int w; // \u7269\u54c1\u91cd\u91cf\n    int v; // \u7269\u54c1\u4ef7\u503c\n\n    public Item(int w, int v) {\n        this.w = w;\n        this.v = v;\n    }\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble fractionalKnapsack(int[] wgt, int[] val, int cap) {\n    // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n    Item[] items = new Item[wgt.length];\n    for (int i = 0; i < wgt.length; i++) {\n        items[i] = new Item(wgt[i], val[i]);\n    }\n    // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n    Arrays.sort(items, Comparator.comparingDouble(item -> -((double) item.v / item.w)));\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n    double res = 0;\n    for (Item item : items) {\n        if (item.w <= cap) {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n            res += (double) item.v / item.w * cap;\n            // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n            break;\n        }\n    }\n    return res;\n}\n
        fractional_knapsack.cs
        /* \u7269\u54c1 */\nclass Item(int w, int v) {\n    public int w = w; // \u7269\u54c1\u91cd\u91cf\n    public int v = v; // \u7269\u54c1\u4ef7\u503c\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble FractionalKnapsack(int[] wgt, int[] val, int cap) {\n    // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n    Item[] items = new Item[wgt.Length];\n    for (int i = 0; i < wgt.Length; i++) {\n        items[i] = new Item(wgt[i], val[i]);\n    }\n    // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n    Array.Sort(items, (x, y) => (y.v / y.w).CompareTo(x.v / x.w));\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n    double res = 0;\n    foreach (Item item in items) {\n        if (item.w <= cap) {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n            res += (double)item.v / item.w * cap;\n            // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n            break;\n        }\n    }\n    return res;\n}\n
        fractional_knapsack.go
        /* \u7269\u54c1 */\ntype Item struct {\n    w int // \u7269\u54c1\u91cd\u91cf\n    v int // \u7269\u54c1\u4ef7\u503c\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunc fractionalKnapsack(wgt []int, val []int, cap int) float64 {\n    // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n    items := make([]Item, len(wgt))\n    for i := 0; i < len(wgt); i++ {\n        items[i] = Item{wgt[i], val[i]}\n    }\n    // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n    sort.Slice(items, func(i, j int) bool {\n        return float64(items[i].v)/float64(items[i].w) > float64(items[j].v)/float64(items[j].w)\n    })\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n    res := 0.0\n    for _, item := range items {\n        if item.w <= cap {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n            res += float64(item.v)\n            cap -= item.w\n        } else {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n            res += float64(item.v) / float64(item.w) * float64(cap)\n            // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n            break\n        }\n    }\n    return res\n}\n
        fractional_knapsack.swift
        /* \u7269\u54c1 */\nclass Item {\n    var w: Int // \u7269\u54c1\u91cd\u91cf\n    var v: Int // \u7269\u54c1\u4ef7\u503c\n\n    init(w: Int, v: Int) {\n        self.w = w\n        self.v = v\n    }\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunc fractionalKnapsack(wgt: [Int], val: [Int], cap: Int) -> Double {\n    // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n    var items = zip(wgt, val).map { Item(w: $0, v: $1) }\n    // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n    items.sort { -(Double($0.v) / Double($0.w)) < -(Double($1.v) / Double($1.w)) }\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n    var res = 0.0\n    var cap = cap\n    for item in items {\n        if item.w <= cap {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n            res += Double(item.v)\n            cap -= item.w\n        } else {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n            res += Double(item.v) / Double(item.w) * Double(cap)\n            // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n            break\n        }\n    }\n    return res\n}\n
        fractional_knapsack.js
        /* \u7269\u54c1 */\nclass Item {\n    constructor(w, v) {\n        this.w = w; // \u7269\u54c1\u91cd\u91cf\n        this.v = v; // \u7269\u54c1\u4ef7\u503c\n    }\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunction fractionalKnapsack(wgt, val, cap) {\n    // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n    const items = wgt.map((w, i) => new Item(w, val[i]));\n    // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n    items.sort((a, b) => b.v / b.w - a.v / a.w);\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n    let res = 0;\n    for (const item of items) {\n        if (item.w <= cap) {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n            res += (item.v / item.w) * cap;\n            // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n            break;\n        }\n    }\n    return res;\n}\n
        fractional_knapsack.ts
        /* \u7269\u54c1 */\nclass Item {\n    w: number; // \u7269\u54c1\u91cd\u91cf\n    v: number; // \u7269\u54c1\u4ef7\u503c\n\n    constructor(w: number, v: number) {\n        this.w = w;\n        this.v = v;\n    }\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunction fractionalKnapsack(wgt: number[], val: number[], cap: number): number {\n    // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n    const items: Item[] = wgt.map((w, i) => new Item(w, val[i]));\n    // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n    items.sort((a, b) => b.v / b.w - a.v / a.w);\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n    let res = 0;\n    for (const item of items) {\n        if (item.w <= cap) {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n            res += (item.v / item.w) * cap;\n            // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n            break;\n        }\n    }\n    return res;\n}\n
        fractional_knapsack.dart
        /* \u7269\u54c1 */\nclass Item {\n  int w; // \u7269\u54c1\u91cd\u91cf\n  int v; // \u7269\u54c1\u4ef7\u503c\n\n  Item(this.w, this.v);\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble fractionalKnapsack(List<int> wgt, List<int> val, int cap) {\n  // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n  List<Item> items = List.generate(wgt.length, (i) => Item(wgt[i], val[i]));\n  // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n  items.sort((a, b) => (b.v / b.w).compareTo(a.v / a.w));\n  // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n  double res = 0;\n  for (Item item in items) {\n    if (item.w <= cap) {\n      // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n      res += item.v;\n      cap -= item.w;\n    } else {\n      // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n      res += item.v / item.w * cap;\n      // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n      break;\n    }\n  }\n  return res;\n}\n
        fractional_knapsack.rs
        /* \u7269\u54c1 */\nstruct Item {\n    w: i32, // \u7269\u54c1\u91cd\u91cf\n    v: i32, // \u7269\u54c1\u4ef7\u503c\n}\n\nimpl Item {\n    fn new(w: i32, v: i32) -> Self {\n        Self { w, v }\n    }\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfn fractional_knapsack(wgt: &[i32], val: &[i32], mut cap: i32) -> f64 {\n    // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n    let mut items = wgt\n        .iter()\n        .zip(val.iter())\n        .map(|(&w, &v)| Item::new(w, v))\n        .collect::<Vec<Item>>();\n    // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n    items.sort_by(|a, b| {\n        (b.v as f64 / b.w as f64)\n            .partial_cmp(&(a.v as f64 / a.w as f64))\n            .unwrap()\n    });\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n    let mut res = 0.0;\n    for item in &items {\n        if item.w <= cap {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n            res += item.v as f64;\n            cap -= item.w;\n        } else {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n            res += item.v as f64 / item.w as f64 * cap as f64;\n            // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n            break;\n        }\n    }\n    res\n}\n
        fractional_knapsack.c
        /* \u7269\u54c1 */\ntypedef struct {\n    int w; // \u7269\u54c1\u91cd\u91cf\n    int v; // \u7269\u54c1\u4ef7\u503c\n} Item;\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfloat fractionalKnapsack(int wgt[], int val[], int itemCount, int cap) {\n    // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n    Item *items = malloc(sizeof(Item) * itemCount);\n    for (int i = 0; i < itemCount; i++) {\n        items[i] = (Item){.w = wgt[i], .v = val[i]};\n    }\n    // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n    qsort(items, (size_t)itemCount, sizeof(Item), sortByValueDensity);\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n    float res = 0.0;\n    for (int i = 0; i < itemCount; i++) {\n        if (items[i].w <= cap) {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n            res += items[i].v;\n            cap -= items[i].w;\n        } else {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n            res += (float)cap / items[i].w * items[i].v;\n            cap = 0;\n            break;\n        }\n    }\n    free(items);\n    return res;\n}\n
        fractional_knapsack.kt
        /* \u7269\u54c1 */\nclass Item(\n    val w: Int, // \u7269\u54c1\n    val v: Int  // \u7269\u54c1\u4ef7\u503c\n)\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfun fractionalKnapsack(wgt: IntArray, _val: IntArray, c: Int): Double {\n    // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n    var cap = c\n    val items = arrayOfNulls<Item>(wgt.size)\n    for (i in wgt.indices) {\n        items[i] = Item(wgt[i], _val[i])\n    }\n    // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n    items.sortBy { item: Item? -> -(item!!.v.toDouble() / item.w) }\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n    var res = 0.0\n    for (item in items) {\n        if (item!!.w <= cap) {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n            res += item.v\n            cap -= item.w\n        } else {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n            res += item.v.toDouble() / item.w * cap\n            // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n            break\n        }\n    }\n    return res\n}\n
        fractional_knapsack.rb
        ### \u7269\u54c1 ###\nclass Item\n  attr_accessor :w # \u7269\u54c1\u91cd\u91cf\n  attr_accessor :v # \u7269\u54c1\u4ef7\u503c\n\n  def initialize(w, v)\n    @w = w\n    @v = v\n  end\nend\n\n### \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 ###\ndef fractional_knapsack(wgt, val, cap)\n  # \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\uff0c\u4ef7\u503c\n  items = wgt.each_with_index.map { |w, i| Item.new(w, val[i]) }\n  # \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n  items.sort! { |a, b| (b.v.to_f / b.w) <=> (a.v.to_f / a.w) }\n  # \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n  res = 0\n  for item in items\n    if item.w <= cap\n      # \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n      res += item.v\n      cap -= item.w\n    else\n      # \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n      res += (item.v.to_f / item.w) * cap\n      # \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n      break\n    end\n  end\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5185\u7f6e\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3a \\(O(\\log n)\\) \uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3a \\(O(\\log n)\\) \u6216 \\(O(n)\\) \uff0c\u53d6\u51b3\u4e8e\u7f16\u7a0b\u8bed\u8a00\u7684\u5177\u4f53\u5b9e\u73b0\u3002

        \u9664\u6392\u5e8f\u4e4b\u5916\uff0c\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u904d\u5386\u6574\u4e2a\u7269\u54c1\u5217\u8868\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u7269\u54c1\u6570\u91cf\u3002

        \u7531\u4e8e\u521d\u59cb\u5316\u4e86\u4e00\u4e2a Item \u5bf9\u8c61\u5217\u8868\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

        "},{"location":"chapter_greedy/fractional_knapsack_problem/#3","title":"3. \u00a0 \u6b63\u786e\u6027\u8bc1\u660e","text":"

        \u91c7\u7528\u53cd\u8bc1\u6cd5\u3002\u5047\u8bbe\u7269\u54c1 \\(x\\) \u662f\u5355\u4f4d\u4ef7\u503c\u6700\u9ad8\u7684\u7269\u54c1\uff0c\u4f7f\u7528\u67d0\u7b97\u6cd5\u6c42\u5f97\u6700\u5927\u4ef7\u503c\u4e3a res \uff0c\u4f46\u8be5\u89e3\u4e2d\u4e0d\u5305\u542b\u7269\u54c1 \\(x\\) \u3002

        \u73b0\u5728\u4ece\u80cc\u5305\u4e2d\u62ff\u51fa\u5355\u4f4d\u91cd\u91cf\u7684\u4efb\u610f\u7269\u54c1\uff0c\u5e76\u66ff\u6362\u4e3a\u5355\u4f4d\u91cd\u91cf\u7684\u7269\u54c1 \\(x\\) \u3002\u7531\u4e8e\u7269\u54c1 \\(x\\) \u7684\u5355\u4f4d\u4ef7\u503c\u6700\u9ad8\uff0c\u56e0\u6b64\u66ff\u6362\u540e\u7684\u603b\u4ef7\u503c\u4e00\u5b9a\u5927\u4e8e res \u3002\u8fd9\u4e0e res \u662f\u6700\u4f18\u89e3\u77db\u76fe\uff0c\u8bf4\u660e\u6700\u4f18\u89e3\u4e2d\u5fc5\u987b\u5305\u542b\u7269\u54c1 \\(x\\) \u3002

        \u5bf9\u4e8e\u8be5\u89e3\u4e2d\u7684\u5176\u4ed6\u7269\u54c1\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u6784\u5efa\u51fa\u4e0a\u8ff0\u77db\u76fe\u3002\u603b\u800c\u8a00\u4e4b\uff0c\u5355\u4f4d\u4ef7\u503c\u66f4\u5927\u7684\u7269\u54c1\u603b\u662f\u66f4\u4f18\u9009\u62e9\uff0c\u8fd9\u8bf4\u660e\u8d2a\u5fc3\u7b56\u7565\u662f\u6709\u6548\u7684\u3002

        \u5982\u56fe 15-6 \u6240\u793a\uff0c\u5982\u679c\u5c06\u7269\u54c1\u91cd\u91cf\u548c\u7269\u54c1\u5355\u4f4d\u4ef7\u503c\u5206\u522b\u770b\u4f5c\u4e00\u5f20\u4e8c\u7ef4\u56fe\u8868\u7684\u6a2a\u8f74\u548c\u7eb5\u8f74\uff0c\u5219\u5206\u6570\u80cc\u5305\u95ee\u9898\u53ef\u8f6c\u5316\u4e3a\u201c\u6c42\u5728\u6709\u9650\u6a2a\u8f74\u533a\u95f4\u4e0b\u56f4\u6210\u7684\u6700\u5927\u9762\u79ef\u201d\u3002\u8fd9\u4e2a\u7c7b\u6bd4\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4ece\u51e0\u4f55\u89d2\u5ea6\u7406\u89e3\u8d2a\u5fc3\u7b56\u7565\u7684\u6709\u6548\u6027\u3002

        \u56fe 15-6 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898\u7684\u51e0\u4f55\u8868\u793a

        "},{"location":"chapter_greedy/greedy_algorithm/","title":"15.1 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5","text":"

        \u8d2a\u5fc3\u7b97\u6cd5\uff08greedy algorithm\uff09\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u89e3\u51b3\u4f18\u5316\u95ee\u9898\u7684\u7b97\u6cd5\uff0c\u5176\u57fa\u672c\u601d\u60f3\u662f\u5728\u95ee\u9898\u7684\u6bcf\u4e2a\u51b3\u7b56\u9636\u6bb5\uff0c\u90fd\u9009\u62e9\u5f53\u524d\u770b\u8d77\u6765\u6700\u4f18\u7684\u9009\u62e9\uff0c\u5373\u8d2a\u5fc3\u5730\u505a\u51fa\u5c40\u90e8\u6700\u4f18\u7684\u51b3\u7b56\uff0c\u4ee5\u671f\u83b7\u5f97\u5168\u5c40\u6700\u4f18\u89e3\u3002\u8d2a\u5fc3\u7b97\u6cd5\u7b80\u6d01\u4e14\u9ad8\u6548\uff0c\u5728\u8bb8\u591a\u5b9e\u9645\u95ee\u9898\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002

        \u8d2a\u5fc3\u7b97\u6cd5\u548c\u52a8\u6001\u89c4\u5212\u90fd\u5e38\u7528\u4e8e\u89e3\u51b3\u4f18\u5316\u95ee\u9898\u3002\u5b83\u4eec\u4e4b\u95f4\u5b58\u5728\u4e00\u4e9b\u76f8\u4f3c\u4e4b\u5904\uff0c\u6bd4\u5982\u90fd\u4f9d\u8d56\u6700\u4f18\u5b50\u7ed3\u6784\u6027\u8d28\uff0c\u4f46\u5de5\u4f5c\u539f\u7406\u4e0d\u540c\u3002

        • \u52a8\u6001\u89c4\u5212\u4f1a\u6839\u636e\u4e4b\u524d\u9636\u6bb5\u7684\u6240\u6709\u51b3\u7b56\u6765\u8003\u8651\u5f53\u524d\u51b3\u7b56\uff0c\u5e76\u4f7f\u7528\u8fc7\u53bb\u5b50\u95ee\u9898\u7684\u89e3\u6765\u6784\u5efa\u5f53\u524d\u5b50\u95ee\u9898\u7684\u89e3\u3002
        • \u8d2a\u5fc3\u7b97\u6cd5\u4e0d\u4f1a\u8003\u8651\u8fc7\u53bb\u7684\u51b3\u7b56\uff0c\u800c\u662f\u4e00\u8def\u5411\u524d\u5730\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u4e0d\u65ad\u7f29\u5c0f\u95ee\u9898\u8303\u56f4\uff0c\u76f4\u81f3\u95ee\u9898\u88ab\u89e3\u51b3\u3002

        \u6211\u4eec\u5148\u901a\u8fc7\u4f8b\u9898\u201c\u96f6\u94b1\u5151\u6362\u201d\u4e86\u89e3\u8d2a\u5fc3\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u3002\u8fd9\u9053\u9898\u5df2\u7ecf\u5728\u201c\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u201d\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u76f8\u4fe1\u4f60\u5bf9\u5b83\u5e76\u4e0d\u964c\u751f\u3002

        Question

        \u7ed9\u5b9a \\(n\\) \u79cd\u786c\u5e01\uff0c\u7b2c \\(i\\) \u79cd\u786c\u5e01\u7684\u9762\u503c\u4e3a \\(coins[i - 1]\\) \uff0c\u76ee\u6807\u91d1\u989d\u4e3a \\(amt\\) \uff0c\u6bcf\u79cd\u786c\u5e01\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u80fd\u591f\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\u3002\u5982\u679c\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\uff0c\u5219\u8fd4\u56de \\(-1\\) \u3002

        \u672c\u9898\u91c7\u53d6\u7684\u8d2a\u5fc3\u7b56\u7565\u5982\u56fe 15-1 \u6240\u793a\u3002\u7ed9\u5b9a\u76ee\u6807\u91d1\u989d\uff0c\u6211\u4eec\u8d2a\u5fc3\u5730\u9009\u62e9\u4e0d\u5927\u4e8e\u4e14\u6700\u63a5\u8fd1\u5b83\u7684\u786c\u5e01\uff0c\u4e0d\u65ad\u5faa\u73af\u8be5\u6b65\u9aa4\uff0c\u76f4\u81f3\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u4e3a\u6b62\u3002

        \u56fe 15-1 \u00a0 \u96f6\u94b1\u5151\u6362\u7684\u8d2a\u5fc3\u7b56\u7565

        \u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change_greedy.py
        def coin_change_greedy(coins: list[int], amt: int) -> int:\n    \"\"\"\u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3\"\"\"\n    # \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n    i = len(coins) - 1\n    count = 0\n    # \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n    while amt > 0:\n        # \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n        while i > 0 and coins[i] > amt:\n            i -= 1\n        # \u9009\u62e9 coins[i]\n        amt -= coins[i]\n        count += 1\n    # \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n    return count if amt == 0 else -1\n
        coin_change_greedy.cpp
        /* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(vector<int> &coins, int amt) {\n    // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n    int i = coins.size() - 1;\n    int count = 0;\n    // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n    while (amt > 0) {\n        // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // \u9009\u62e9 coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n    return amt == 0 ? count : -1;\n}\n
        coin_change_greedy.java
        /* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(int[] coins, int amt) {\n    // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n    int i = coins.length - 1;\n    int count = 0;\n    // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n    while (amt > 0) {\n        // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // \u9009\u62e9 coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n    return amt == 0 ? count : -1;\n}\n
        coin_change_greedy.cs
        /* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint CoinChangeGreedy(int[] coins, int amt) {\n    // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n    int i = coins.Length - 1;\n    int count = 0;\n    // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n    while (amt > 0) {\n        // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // \u9009\u62e9 coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n    return amt == 0 ? count : -1;\n}\n
        coin_change_greedy.go
        /* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunc coinChangeGreedy(coins []int, amt int) int {\n    // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n    i := len(coins) - 1\n    count := 0\n    // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n    for amt > 0 {\n        // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n        for i > 0 && coins[i] > amt {\n            i--\n        }\n        // \u9009\u62e9 coins[i]\n        amt -= coins[i]\n        count++\n    }\n    // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n    if amt != 0 {\n        return -1\n    }\n    return count\n}\n
        coin_change_greedy.swift
        /* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunc coinChangeGreedy(coins: [Int], amt: Int) -> Int {\n    // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n    var i = coins.count - 1\n    var count = 0\n    var amt = amt\n    // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n    while amt > 0 {\n        // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n        while i > 0 && coins[i] > amt {\n            i -= 1\n        }\n        // \u9009\u62e9 coins[i]\n        amt -= coins[i]\n        count += 1\n    }\n    // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n    return amt == 0 ? count : -1\n}\n
        coin_change_greedy.js
        /* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunction coinChangeGreedy(coins, amt) {\n    // \u5047\u8bbe coins \u6570\u7ec4\u6709\u5e8f\n    let i = coins.length - 1;\n    let count = 0;\n    // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n    while (amt > 0) {\n        // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // \u9009\u62e9 coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n    return amt === 0 ? count : -1;\n}\n
        coin_change_greedy.ts
        /* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunction coinChangeGreedy(coins: number[], amt: number): number {\n    // \u5047\u8bbe coins \u6570\u7ec4\u6709\u5e8f\n    let i = coins.length - 1;\n    let count = 0;\n    // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n    while (amt > 0) {\n        // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // \u9009\u62e9 coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n    return amt === 0 ? count : -1;\n}\n
        coin_change_greedy.dart
        /* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(List<int> coins, int amt) {\n  // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n  int i = coins.length - 1;\n  int count = 0;\n  // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n  while (amt > 0) {\n    // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n    while (i > 0 && coins[i] > amt) {\n      i--;\n    }\n    // \u9009\u62e9 coins[i]\n    amt -= coins[i];\n    count++;\n  }\n  // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n  return amt == 0 ? count : -1;\n}\n
        coin_change_greedy.rs
        /* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfn coin_change_greedy(coins: &[i32], mut amt: i32) -> i32 {\n    // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n    let mut i = coins.len() - 1;\n    let mut count = 0;\n    // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n    while amt > 0 {\n        // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n        while i > 0 && coins[i] > amt {\n            i -= 1;\n        }\n        // \u9009\u62e9 coins[i]\n        amt -= coins[i];\n        count += 1;\n    }\n    // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n    if amt == 0 {\n        count\n    } else {\n        -1\n    }\n}\n
        coin_change_greedy.c
        /* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(int *coins, int size, int amt) {\n    // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n    int i = size - 1;\n    int count = 0;\n    // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n    while (amt > 0) {\n        // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // \u9009\u62e9 coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n    return amt == 0 ? count : -1;\n}\n
        coin_change_greedy.kt
        /* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfun coinChangeGreedy(coins: IntArray, amt: Int): Int {\n    // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n    var am = amt\n    var i = coins.size - 1\n    var count = 0\n    // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n    while (am > 0) {\n        // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n        while (i > 0 && coins[i] > am) {\n            i--\n        }\n        // \u9009\u62e9 coins[i]\n        am -= coins[i]\n        count++\n    }\n    // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n    return if (am == 0) count else -1\n}\n
        coin_change_greedy.rb
        ### \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 ###\ndef coin_change_greedy(coins, amt)\n  # \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n  i = coins.length - 1\n  count = 0\n  # \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n  while amt > 0\n    # \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n    while i > 0 && coins[i] > amt\n      i -= 1\n    end\n    # \u9009\u62e9 coins[i]\n    amt -= coins[i]\n    count += 1\n  end\n  # \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c \u5219\u8fd4\u56de -1\n  amt == 0 ? count : -1\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u4f60\u53ef\u80fd\u4f1a\u4e0d\u7531\u5730\u53d1\u51fa\u611f\u53f9\uff1aSo clean \uff01\u8d2a\u5fc3\u7b97\u6cd5\u4ec5\u7528\u7ea6\u5341\u884c\u4ee3\u7801\u5c31\u89e3\u51b3\u4e86\u96f6\u94b1\u5151\u6362\u95ee\u9898\u3002

        "},{"location":"chapter_greedy/greedy_algorithm/#1511","title":"15.1.1 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5\u7684\u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"

        \u8d2a\u5fc3\u7b97\u6cd5\u4e0d\u4ec5\u64cd\u4f5c\u76f4\u63a5\u3001\u5b9e\u73b0\u7b80\u5355\uff0c\u800c\u4e14\u901a\u5e38\u6548\u7387\u4e5f\u5f88\u9ad8\u3002\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u8bb0\u786c\u5e01\u6700\u5c0f\u9762\u503c\u4e3a \\(\\min(coins)\\) \uff0c\u5219\u8d2a\u5fc3\u9009\u62e9\u6700\u591a\u5faa\u73af \\(amt / \\min(coins)\\) \u6b21\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(amt / \\min(coins))\\) \u3002\u8fd9\u6bd4\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n \\times amt)\\) \u5c0f\u4e86\u4e00\u4e2a\u6570\u91cf\u7ea7\u3002

        \u7136\u800c\uff0c\u5bf9\u4e8e\u67d0\u4e9b\u786c\u5e01\u9762\u503c\u7ec4\u5408\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u5e76\u4e0d\u80fd\u627e\u5230\u6700\u4f18\u89e3\u3002\u56fe 15-2 \u7ed9\u51fa\u4e86\u4e24\u4e2a\u793a\u4f8b\u3002

        • \u6b63\u4f8b \\(coins = [1, 5, 10, 20, 50, 100]\\)\uff1a\u5728\u8be5\u786c\u5e01\u7ec4\u5408\u4e0b\uff0c\u7ed9\u5b9a\u4efb\u610f \\(amt\\) \uff0c\u8d2a\u5fc3\u7b97\u6cd5\u90fd\u53ef\u4ee5\u627e\u5230\u6700\u4f18\u89e3\u3002
        • \u53cd\u4f8b \\(coins = [1, 20, 50]\\)\uff1a\u5047\u8bbe \\(amt = 60\\) \uff0c\u8d2a\u5fc3\u7b97\u6cd5\u53ea\u80fd\u627e\u5230 \\(50 + 1 \\times 10\\) \u7684\u5151\u6362\u7ec4\u5408\uff0c\u5171\u8ba1 \\(11\\) \u679a\u786c\u5e01\uff0c\u4f46\u52a8\u6001\u89c4\u5212\u53ef\u4ee5\u627e\u5230\u6700\u4f18\u89e3 \\(20 + 20 + 20\\) \uff0c\u4ec5\u9700 \\(3\\) \u679a\u786c\u5e01\u3002
        • \u53cd\u4f8b \\(coins = [1, 49, 50]\\)\uff1a\u5047\u8bbe \\(amt = 98\\) \uff0c\u8d2a\u5fc3\u7b97\u6cd5\u53ea\u80fd\u627e\u5230 \\(50 + 1 \\times 48\\) \u7684\u5151\u6362\u7ec4\u5408\uff0c\u5171\u8ba1 \\(49\\) \u679a\u786c\u5e01\uff0c\u4f46\u52a8\u6001\u89c4\u5212\u53ef\u4ee5\u627e\u5230\u6700\u4f18\u89e3 \\(49 + 49\\) \uff0c\u4ec5\u9700 \\(2\\) \u679a\u786c\u5e01\u3002

        \u56fe 15-2 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5\u65e0\u6cd5\u627e\u51fa\u6700\u4f18\u89e3\u7684\u793a\u4f8b

        \u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u4e8e\u96f6\u94b1\u5151\u6362\u95ee\u9898\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u65e0\u6cd5\u4fdd\u8bc1\u627e\u5230\u5168\u5c40\u6700\u4f18\u89e3\uff0c\u5e76\u4e14\u6709\u53ef\u80fd\u627e\u5230\u975e\u5e38\u5dee\u7684\u89e3\u3002\u5b83\u66f4\u9002\u5408\u7528\u52a8\u6001\u89c4\u5212\u89e3\u51b3\u3002

        \u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u7684\u9002\u7528\u60c5\u51b5\u5206\u4ee5\u4e0b\u4e24\u79cd\u3002

        1. \u53ef\u4ee5\u4fdd\u8bc1\u627e\u5230\u6700\u4f18\u89e3\uff1a\u8d2a\u5fc3\u7b97\u6cd5\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5f80\u5f80\u662f\u6700\u4f18\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u5f80\u5f80\u6bd4\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\u66f4\u9ad8\u6548\u3002
        2. \u53ef\u4ee5\u627e\u5230\u8fd1\u4f3c\u6700\u4f18\u89e3\uff1a\u8d2a\u5fc3\u7b97\u6cd5\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e5f\u662f\u53ef\u7528\u7684\u3002\u5bf9\u4e8e\u5f88\u591a\u590d\u6742\u95ee\u9898\u6765\u8bf4\uff0c\u5bfb\u627e\u5168\u5c40\u6700\u4f18\u89e3\u975e\u5e38\u56f0\u96be\uff0c\u80fd\u4ee5\u8f83\u9ad8\u6548\u7387\u627e\u5230\u6b21\u4f18\u89e3\u4e5f\u662f\u975e\u5e38\u4e0d\u9519\u7684\u3002
        "},{"location":"chapter_greedy/greedy_algorithm/#1512","title":"15.1.2 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5\u7279\u6027","text":"

        \u90a3\u4e48\u95ee\u9898\u6765\u4e86\uff0c\u4ec0\u4e48\u6837\u7684\u95ee\u9898\u9002\u5408\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\u5462\uff1f\u6216\u8005\u8bf4\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4fdd\u8bc1\u627e\u5230\u6700\u4f18\u89e3\uff1f

        \u76f8\u8f83\u4e8e\u52a8\u6001\u89c4\u5212\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u7684\u4f7f\u7528\u6761\u4ef6\u66f4\u52a0\u82db\u523b\uff0c\u5176\u4e3b\u8981\u5173\u6ce8\u95ee\u9898\u7684\u4e24\u4e2a\u6027\u8d28\u3002

        • \u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\uff1a\u53ea\u6709\u5f53\u5c40\u90e8\u6700\u4f18\u9009\u62e9\u59cb\u7ec8\u53ef\u4ee5\u5bfc\u81f4\u5168\u5c40\u6700\u4f18\u89e3\u65f6\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u624d\u80fd\u4fdd\u8bc1\u5f97\u5230\u6700\u4f18\u89e3\u3002
        • \u6700\u4f18\u5b50\u7ed3\u6784\uff1a\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u5305\u542b\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u3002

        \u6700\u4f18\u5b50\u7ed3\u6784\u5df2\u7ecf\u5728\u201c\u52a8\u6001\u89c4\u5212\u201d\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u8fd9\u91cc\u4e0d\u518d\u8d58\u8ff0\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u4e00\u4e9b\u95ee\u9898\u7684\u6700\u4f18\u5b50\u7ed3\u6784\u5e76\u4e0d\u660e\u663e\uff0c\u4f46\u4ecd\u7136\u53ef\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u89e3\u51b3\u3002

        \u6211\u4eec\u4e3b\u8981\u63a2\u7a76\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u7684\u5224\u65ad\u65b9\u6cd5\u3002\u867d\u7136\u5b83\u7684\u63cf\u8ff0\u770b\u4e0a\u53bb\u6bd4\u8f83\u7b80\u5355\uff0c\u4f46\u5b9e\u9645\u4e0a\u5bf9\u4e8e\u8bb8\u591a\u95ee\u9898\uff0c\u8bc1\u660e\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u5e76\u975e\u6613\u4e8b\u3002

        \u4f8b\u5982\u96f6\u94b1\u5151\u6362\u95ee\u9898\uff0c\u6211\u4eec\u867d\u7136\u80fd\u591f\u5bb9\u6613\u5730\u4e3e\u51fa\u53cd\u4f8b\uff0c\u5bf9\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u8fdb\u884c\u8bc1\u4f2a\uff0c\u4f46\u8bc1\u5b9e\u7684\u96be\u5ea6\u8f83\u5927\u3002\u5982\u679c\u95ee\uff1a\u6ee1\u8db3\u4ec0\u4e48\u6761\u4ef6\u7684\u786c\u5e01\u7ec4\u5408\u53ef\u4ee5\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\uff1f\u6211\u4eec\u5f80\u5f80\u53ea\u80fd\u51ed\u501f\u76f4\u89c9\u6216\u4e3e\u4f8b\u5b50\u6765\u7ed9\u51fa\u4e00\u4e2a\u6a21\u68f1\u4e24\u53ef\u7684\u7b54\u6848\uff0c\u800c\u96be\u4ee5\u7ed9\u51fa\u4e25\u8c28\u7684\u6570\u5b66\u8bc1\u660e\u3002

        Quote

        \u6709\u4e00\u7bc7\u8bba\u6587\u7ed9\u51fa\u4e86\u4e00\u4e2a \\(O(n^3)\\) \u65f6\u95f4\u590d\u6742\u5ea6\u7684\u7b97\u6cd5\uff0c\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u786c\u5e01\u7ec4\u5408\u80fd\u5426\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u627e\u51fa\u4efb\u610f\u91d1\u989d\u7684\u6700\u4f18\u89e3\u3002

        Pearson, D. A polynomial-time algorithm for the change-making problem[J]. Operations Research Letters, 2005, 33(3): 231-234.

        "},{"location":"chapter_greedy/greedy_algorithm/#1513","title":"15.1.3 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5\u89e3\u9898\u6b65\u9aa4","text":"

        \u8d2a\u5fc3\u95ee\u9898\u7684\u89e3\u51b3\u6d41\u7a0b\u5927\u4f53\u53ef\u5206\u4e3a\u4ee5\u4e0b\u4e09\u6b65\u3002

        1. \u95ee\u9898\u5206\u6790\uff1a\u68b3\u7406\u4e0e\u7406\u89e3\u95ee\u9898\u7279\u6027\uff0c\u5305\u62ec\u72b6\u6001\u5b9a\u4e49\u3001\u4f18\u5316\u76ee\u6807\u548c\u7ea6\u675f\u6761\u4ef6\u7b49\u3002\u8fd9\u4e00\u6b65\u5728\u56de\u6eaf\u548c\u52a8\u6001\u89c4\u5212\u4e2d\u90fd\u6709\u6d89\u53ca\u3002
        2. \u786e\u5b9a\u8d2a\u5fc3\u7b56\u7565\uff1a\u786e\u5b9a\u5982\u4f55\u5728\u6bcf\u4e00\u6b65\u4e2d\u505a\u51fa\u8d2a\u5fc3\u9009\u62e9\u3002\u8fd9\u4e2a\u7b56\u7565\u80fd\u591f\u5728\u6bcf\u4e00\u6b65\u51cf\u5c0f\u95ee\u9898\u7684\u89c4\u6a21\uff0c\u5e76\u6700\u7ec8\u89e3\u51b3\u6574\u4e2a\u95ee\u9898\u3002
        3. \u6b63\u786e\u6027\u8bc1\u660e\uff1a\u901a\u5e38\u9700\u8981\u8bc1\u660e\u95ee\u9898\u5177\u6709\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u548c\u6700\u4f18\u5b50\u7ed3\u6784\u3002\u8fd9\u4e2a\u6b65\u9aa4\u53ef\u80fd\u9700\u8981\u7528\u5230\u6570\u5b66\u8bc1\u660e\uff0c\u4f8b\u5982\u5f52\u7eb3\u6cd5\u6216\u53cd\u8bc1\u6cd5\u7b49\u3002

        \u786e\u5b9a\u8d2a\u5fc3\u7b56\u7565\u662f\u6c42\u89e3\u95ee\u9898\u7684\u6838\u5fc3\u6b65\u9aa4\uff0c\u4f46\u5b9e\u65bd\u8d77\u6765\u53ef\u80fd\u5e76\u4e0d\u5bb9\u6613\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002

        • \u4e0d\u540c\u95ee\u9898\u7684\u8d2a\u5fc3\u7b56\u7565\u7684\u5dee\u5f02\u8f83\u5927\u3002\u5bf9\u4e8e\u8bb8\u591a\u95ee\u9898\u6765\u8bf4\uff0c\u8d2a\u5fc3\u7b56\u7565\u6bd4\u8f83\u6d45\u663e\uff0c\u6211\u4eec\u901a\u8fc7\u4e00\u4e9b\u5927\u6982\u7684\u601d\u8003\u4e0e\u5c1d\u8bd5\u5c31\u80fd\u5f97\u51fa\u3002\u800c\u5bf9\u4e8e\u4e00\u4e9b\u590d\u6742\u95ee\u9898\uff0c\u8d2a\u5fc3\u7b56\u7565\u53ef\u80fd\u975e\u5e38\u9690\u853d\uff0c\u8fd9\u79cd\u60c5\u51b5\u5c31\u975e\u5e38\u8003\u9a8c\u4e2a\u4eba\u7684\u89e3\u9898\u7ecf\u9a8c\u4e0e\u7b97\u6cd5\u80fd\u529b\u4e86\u3002
        • \u67d0\u4e9b\u8d2a\u5fc3\u7b56\u7565\u5177\u6709\u8f83\u5f3a\u7684\u8ff7\u60d1\u6027\u3002\u5f53\u6211\u4eec\u6ee1\u6000\u4fe1\u5fc3\u8bbe\u8ba1\u597d\u8d2a\u5fc3\u7b56\u7565\uff0c\u5199\u51fa\u89e3\u9898\u4ee3\u7801\u5e76\u63d0\u4ea4\u8fd0\u884c\uff0c\u5f88\u53ef\u80fd\u53d1\u73b0\u90e8\u5206\u6d4b\u8bd5\u6837\u4f8b\u65e0\u6cd5\u901a\u8fc7\u3002\u8fd9\u662f\u56e0\u4e3a\u8bbe\u8ba1\u7684\u8d2a\u5fc3\u7b56\u7565\u53ea\u662f\u201c\u90e8\u5206\u6b63\u786e\u201d\u7684\uff0c\u4e0a\u6587\u4ecb\u7ecd\u7684\u96f6\u94b1\u5151\u6362\u5c31\u662f\u4e00\u4e2a\u5178\u578b\u6848\u4f8b\u3002

        \u4e3a\u4e86\u4fdd\u8bc1\u6b63\u786e\u6027\uff0c\u6211\u4eec\u5e94\u8be5\u5bf9\u8d2a\u5fc3\u7b56\u7565\u8fdb\u884c\u4e25\u8c28\u7684\u6570\u5b66\u8bc1\u660e\uff0c\u901a\u5e38\u9700\u8981\u7528\u5230\u53cd\u8bc1\u6cd5\u6216\u6570\u5b66\u5f52\u7eb3\u6cd5\u3002

        \u7136\u800c\uff0c\u6b63\u786e\u6027\u8bc1\u660e\u4e5f\u5f88\u53ef\u80fd\u4e0d\u662f\u4e00\u4ef6\u6613\u4e8b\u3002\u5982\u82e5\u6ca1\u6709\u5934\u7eea\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u9009\u62e9\u9762\u5411\u6d4b\u8bd5\u7528\u4f8b\u8fdb\u884c\u4ee3\u7801\u8c03\u8bd5\uff0c\u4e00\u6b65\u6b65\u4fee\u6539\u4e0e\u9a8c\u8bc1\u8d2a\u5fc3\u7b56\u7565\u3002

        "},{"location":"chapter_greedy/greedy_algorithm/#1514","title":"15.1.4 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5\u5178\u578b\u4f8b\u9898","text":"

        \u8d2a\u5fc3\u7b97\u6cd5\u5e38\u5e38\u5e94\u7528\u5728\u6ee1\u8db3\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u548c\u6700\u4f18\u5b50\u7ed3\u6784\u7684\u4f18\u5316\u95ee\u9898\u4e2d\uff0c\u4ee5\u4e0b\u5217\u4e3e\u4e86\u4e00\u4e9b\u5178\u578b\u7684\u8d2a\u5fc3\u7b97\u6cd5\u95ee\u9898\u3002

        • \u786c\u5e01\u627e\u96f6\u95ee\u9898\uff1a\u5728\u67d0\u4e9b\u786c\u5e01\u7ec4\u5408\u4e0b\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u603b\u662f\u53ef\u4ee5\u5f97\u5230\u6700\u4f18\u89e3\u3002
        • \u533a\u95f4\u8c03\u5ea6\u95ee\u9898\uff1a\u5047\u8bbe\u4f60\u6709\u4e00\u4e9b\u4efb\u52a1\uff0c\u6bcf\u4e2a\u4efb\u52a1\u5728\u4e00\u6bb5\u65f6\u95f4\u5185\u8fdb\u884c\uff0c\u4f60\u7684\u76ee\u6807\u662f\u5b8c\u6210\u5c3d\u53ef\u80fd\u591a\u7684\u4efb\u52a1\u3002\u5982\u679c\u6bcf\u6b21\u90fd\u9009\u62e9\u7ed3\u675f\u65f6\u95f4\u6700\u65e9\u7684\u4efb\u52a1\uff0c\u90a3\u4e48\u8d2a\u5fc3\u7b97\u6cd5\u5c31\u53ef\u4ee5\u5f97\u5230\u6700\u4f18\u89e3\u3002
        • \u5206\u6570\u80cc\u5305\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u7ec4\u7269\u54c1\u548c\u4e00\u4e2a\u8f7d\u91cd\u91cf\uff0c\u4f60\u7684\u76ee\u6807\u662f\u9009\u62e9\u4e00\u7ec4\u7269\u54c1\uff0c\u4f7f\u5f97\u603b\u91cd\u91cf\u4e0d\u8d85\u8fc7\u8f7d\u91cd\u91cf\uff0c\u4e14\u603b\u4ef7\u503c\u6700\u5927\u3002\u5982\u679c\u6bcf\u6b21\u90fd\u9009\u62e9\u6027\u4ef7\u6bd4\u6700\u9ad8\uff08\u4ef7\u503c / \u91cd\u91cf\uff09\u7684\u7269\u54c1\uff0c\u90a3\u4e48\u8d2a\u5fc3\u7b97\u6cd5\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\u53ef\u4ee5\u5f97\u5230\u6700\u4f18\u89e3\u3002
        • \u80a1\u7968\u4e70\u5356\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u7ec4\u80a1\u7968\u7684\u5386\u53f2\u4ef7\u683c\uff0c\u4f60\u53ef\u4ee5\u8fdb\u884c\u591a\u6b21\u4e70\u5356\uff0c\u4f46\u5982\u679c\u4f60\u5df2\u7ecf\u6301\u6709\u80a1\u7968\uff0c\u90a3\u4e48\u5728\u5356\u51fa\u4e4b\u524d\u4e0d\u80fd\u518d\u4e70\uff0c\u76ee\u6807\u662f\u83b7\u53d6\u6700\u5927\u5229\u6da6\u3002
        • \u970d\u592b\u66fc\u7f16\u7801\uff1a\u970d\u592b\u66fc\u7f16\u7801\u662f\u4e00\u79cd\u7528\u4e8e\u65e0\u635f\u6570\u636e\u538b\u7f29\u7684\u8d2a\u5fc3\u7b97\u6cd5\u3002\u901a\u8fc7\u6784\u5efa\u970d\u592b\u66fc\u6811\uff0c\u6bcf\u6b21\u9009\u62e9\u51fa\u73b0\u9891\u7387\u6700\u4f4e\u7684\u4e24\u4e2a\u8282\u70b9\u5408\u5e76\uff0c\u6700\u540e\u5f97\u5230\u7684\u970d\u592b\u66fc\u6811\u7684\u5e26\u6743\u8def\u5f84\u957f\u5ea6\uff08\u7f16\u7801\u957f\u5ea6\uff09\u6700\u5c0f\u3002
        • Dijkstra \u7b97\u6cd5\uff1a\u5b83\u662f\u4e00\u79cd\u89e3\u51b3\u7ed9\u5b9a\u6e90\u9876\u70b9\u5230\u5176\u4f59\u5404\u9876\u70b9\u7684\u6700\u77ed\u8def\u5f84\u95ee\u9898\u7684\u8d2a\u5fc3\u7b97\u6cd5\u3002
        "},{"location":"chapter_greedy/max_capacity_problem/","title":"15.3 \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898","text":"

        Question

        \u8f93\u5165\u4e00\u4e2a\u6570\u7ec4 \\(ht\\) \uff0c\u5176\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u4ee3\u8868\u4e00\u4e2a\u5782\u76f4\u9694\u677f\u7684\u9ad8\u5ea6\u3002\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e24\u4e2a\u9694\u677f\uff0c\u4ee5\u53ca\u5b83\u4eec\u4e4b\u95f4\u7684\u7a7a\u95f4\u53ef\u4ee5\u7ec4\u6210\u4e00\u4e2a\u5bb9\u5668\u3002

        \u5bb9\u5668\u7684\u5bb9\u91cf\u7b49\u4e8e\u9ad8\u5ea6\u548c\u5bbd\u5ea6\u7684\u4e58\u79ef\uff08\u9762\u79ef\uff09\uff0c\u5176\u4e2d\u9ad8\u5ea6\u7531\u8f83\u77ed\u7684\u9694\u677f\u51b3\u5b9a\uff0c\u5bbd\u5ea6\u662f\u4e24\u4e2a\u9694\u677f\u7684\u6570\u7ec4\u7d22\u5f15\u4e4b\u5dee\u3002

        \u8bf7\u5728\u6570\u7ec4\u4e2d\u9009\u62e9\u4e24\u4e2a\u9694\u677f\uff0c\u4f7f\u5f97\u7ec4\u6210\u7684\u5bb9\u5668\u7684\u5bb9\u91cf\u6700\u5927\uff0c\u8fd4\u56de\u6700\u5927\u5bb9\u91cf\u3002\u793a\u4f8b\u5982\u56fe 15-7 \u6240\u793a\u3002

        \u56fe 15-7 \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e

        \u5bb9\u5668\u7531\u4efb\u610f\u4e24\u4e2a\u9694\u677f\u56f4\u6210\uff0c\u56e0\u6b64\u672c\u9898\u7684\u72b6\u6001\u4e3a\u4e24\u4e2a\u9694\u677f\u7684\u7d22\u5f15\uff0c\u8bb0\u4e3a \\([i, j]\\) \u3002

        \u6839\u636e\u9898\u610f\uff0c\u5bb9\u91cf\u7b49\u4e8e\u9ad8\u5ea6\u4e58\u4ee5\u5bbd\u5ea6\uff0c\u5176\u4e2d\u9ad8\u5ea6\u7531\u77ed\u677f\u51b3\u5b9a\uff0c\u5bbd\u5ea6\u662f\u4e24\u9694\u677f\u7684\u6570\u7ec4\u7d22\u5f15\u4e4b\u5dee\u3002\u8bbe\u5bb9\u91cf\u4e3a \\(cap[i, j]\\) \uff0c\u5219\u53ef\u5f97\u8ba1\u7b97\u516c\u5f0f\uff1a

        \\[ cap[i, j] = \\min(ht[i], ht[j]) \\times (j - i) \\]

        \u8bbe\u6570\u7ec4\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u4e24\u4e2a\u9694\u677f\u7684\u7ec4\u5408\u6570\u91cf\uff08\u72b6\u6001\u603b\u6570\uff09\u4e3a \\(C_n^2 = \\frac{n(n - 1)}{2}\\) \u4e2a\u3002\u6700\u76f4\u63a5\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u7a77\u4e3e\u6240\u6709\u72b6\u6001\uff0c\u4ece\u800c\u6c42\u5f97\u6700\u5927\u5bb9\u91cf\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002

        "},{"location":"chapter_greedy/max_capacity_problem/#1","title":"1. \u00a0 \u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a","text":"

        \u8fd9\u9053\u9898\u8fd8\u6709\u66f4\u9ad8\u6548\u7387\u7684\u89e3\u6cd5\u3002\u5982\u56fe 15-8 \u6240\u793a\uff0c\u73b0\u9009\u53d6\u4e00\u4e2a\u72b6\u6001 \\([i, j]\\) \uff0c\u5176\u6ee1\u8db3\u7d22\u5f15 \\(i < j\\) \u4e14\u9ad8\u5ea6 \\(ht[i] < ht[j]\\) \uff0c\u5373 \\(i\\) \u4e3a\u77ed\u677f\u3001\\(j\\) \u4e3a\u957f\u677f\u3002

        \u56fe 15-8 \u00a0 \u521d\u59cb\u72b6\u6001

        \u5982\u56fe 15-9 \u6240\u793a\uff0c\u82e5\u6b64\u65f6\u5c06\u957f\u677f \\(j\\) \u5411\u77ed\u677f \\(i\\) \u9760\u8fd1\uff0c\u5219\u5bb9\u91cf\u4e00\u5b9a\u53d8\u5c0f\u3002

        \u8fd9\u662f\u56e0\u4e3a\u5728\u79fb\u52a8\u957f\u677f \\(j\\) \u540e\uff0c\u5bbd\u5ea6 \\(j-i\\) \u80af\u5b9a\u53d8\u5c0f\uff1b\u800c\u9ad8\u5ea6\u7531\u77ed\u677f\u51b3\u5b9a\uff0c\u56e0\u6b64\u9ad8\u5ea6\u53ea\u53ef\u80fd\u4e0d\u53d8\uff08 \\(i\\) \u4ecd\u4e3a\u77ed\u677f\uff09\u6216\u53d8\u5c0f\uff08\u79fb\u52a8\u540e\u7684 \\(j\\) \u6210\u4e3a\u77ed\u677f\uff09\u3002

        \u56fe 15-9 \u00a0 \u5411\u5185\u79fb\u52a8\u957f\u677f\u540e\u7684\u72b6\u6001

        \u53cd\u5411\u601d\u8003\uff0c\u6211\u4eec\u53ea\u6709\u5411\u5185\u6536\u7f29\u77ed\u677f \\(i\\) \uff0c\u624d\u6709\u53ef\u80fd\u4f7f\u5bb9\u91cf\u53d8\u5927\u3002\u56e0\u4e3a\u867d\u7136\u5bbd\u5ea6\u4e00\u5b9a\u53d8\u5c0f\uff0c\u4f46\u9ad8\u5ea6\u53ef\u80fd\u4f1a\u53d8\u5927\uff08\u79fb\u52a8\u540e\u7684\u77ed\u677f \\(i\\) \u53ef\u80fd\u4f1a\u53d8\u957f\uff09\u3002\u4f8b\u5982\u5728\u56fe 15-10 \u4e2d\uff0c\u79fb\u52a8\u77ed\u677f\u540e\u9762\u79ef\u53d8\u5927\u3002

        \u56fe 15-10 \u00a0 \u5411\u5185\u79fb\u52a8\u77ed\u677f\u540e\u7684\u72b6\u6001

        \u7531\u6b64\u4fbf\u53ef\u63a8\u51fa\u672c\u9898\u7684\u8d2a\u5fc3\u7b56\u7565\uff1a\u521d\u59cb\u5316\u4e24\u6307\u9488\uff0c\u4f7f\u5176\u5206\u5217\u5bb9\u5668\u4e24\u7aef\uff0c\u6bcf\u8f6e\u5411\u5185\u6536\u7f29\u77ed\u677f\u5bf9\u5e94\u7684\u6307\u9488\uff0c\u76f4\u81f3\u4e24\u6307\u9488\u76f8\u9047\u3002

        \u56fe 15-11 \u5c55\u793a\u4e86\u8d2a\u5fc3\u7b56\u7565\u7684\u6267\u884c\u8fc7\u7a0b\u3002

        1. \u521d\u59cb\u72b6\u6001\u4e0b\uff0c\u6307\u9488 \\(i\\) \u548c \\(j\\) \u5206\u5217\u6570\u7ec4\u4e24\u7aef\u3002
        2. \u8ba1\u7b97\u5f53\u524d\u72b6\u6001\u7684\u5bb9\u91cf \\(cap[i, j]\\) \uff0c\u5e76\u66f4\u65b0\u6700\u5927\u5bb9\u91cf\u3002
        3. \u6bd4\u8f83\u677f \\(i\\) \u548c\u677f \\(j\\) \u7684\u9ad8\u5ea6\uff0c\u5e76\u5c06\u77ed\u677f\u5411\u5185\u79fb\u52a8\u4e00\u683c\u3002
        4. \u5faa\u73af\u6267\u884c\u7b2c 2. \u6b65\u548c\u7b2c 3. \u6b65\uff0c\u76f4\u81f3 \\(i\\) \u548c \\(j\\) \u76f8\u9047\u65f6\u7ed3\u675f\u3002
        <1><2><3><4><5><6><7><8><9>

        \u56fe 15-11 \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898\u7684\u8d2a\u5fc3\u8fc7\u7a0b

        "},{"location":"chapter_greedy/max_capacity_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u4ee3\u7801\u5faa\u73af\u6700\u591a \\(n\\) \u8f6e\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

        \u53d8\u91cf \\(i\\)\u3001\\(j\\)\u3001\\(res\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby max_capacity.py
        def max_capacity(ht: list[int]) -> int:\n    \"\"\"\u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3\"\"\"\n    # \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n    i, j = 0, len(ht) - 1\n    # \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n    res = 0\n    # \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n    while i < j:\n        # \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n        cap = min(ht[i], ht[j]) * (j - i)\n        res = max(res, cap)\n        # \u5411\u5185\u79fb\u52a8\u77ed\u677f\n        if ht[i] < ht[j]:\n            i += 1\n        else:\n            j -= 1\n    return res\n
        max_capacity.cpp
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(vector<int> &ht) {\n    // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n    int i = 0, j = ht.size() - 1;\n    // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n    int res = 0;\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n    while (i < j) {\n        // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n        int cap = min(ht[i], ht[j]) * (j - i);\n        res = max(res, cap);\n        // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
        max_capacity.java
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(int[] ht) {\n    // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n    int i = 0, j = ht.length - 1;\n    // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n    int res = 0;\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n    while (i < j) {\n        // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n        int cap = Math.min(ht[i], ht[j]) * (j - i);\n        res = Math.max(res, cap);\n        // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
        max_capacity.cs
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint MaxCapacity(int[] ht) {\n    // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n    int i = 0, j = ht.Length - 1;\n    // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n    int res = 0;\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n    while (i < j) {\n        // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n        int cap = Math.Min(ht[i], ht[j]) * (j - i);\n        res = Math.Max(res, cap);\n        // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
        max_capacity.go
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunc maxCapacity(ht []int) int {\n    // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n    i, j := 0, len(ht)-1\n    // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n    res := 0\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n    for i < j {\n        // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n        capacity := int(math.Min(float64(ht[i]), float64(ht[j]))) * (j - i)\n        res = int(math.Max(float64(res), float64(capacity)))\n        // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n        if ht[i] < ht[j] {\n            i++\n        } else {\n            j--\n        }\n    }\n    return res\n}\n
        max_capacity.swift
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunc maxCapacity(ht: [Int]) -> Int {\n    // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n    var i = ht.startIndex, j = ht.endIndex - 1\n    // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n    var res = 0\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n    while i < j {\n        // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n        let cap = min(ht[i], ht[j]) * (j - i)\n        res = max(res, cap)\n        // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n        if ht[i] < ht[j] {\n            i += 1\n        } else {\n            j -= 1\n        }\n    }\n    return res\n}\n
        max_capacity.js
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunction maxCapacity(ht) {\n    // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n    let i = 0,\n        j = ht.length - 1;\n    // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n    let res = 0;\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n    while (i < j) {\n        // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n        const cap = Math.min(ht[i], ht[j]) * (j - i);\n        res = Math.max(res, cap);\n        // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n        if (ht[i] < ht[j]) {\n            i += 1;\n        } else {\n            j -= 1;\n        }\n    }\n    return res;\n}\n
        max_capacity.ts
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunction maxCapacity(ht: number[]): number {\n    // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n    let i = 0,\n        j = ht.length - 1;\n    // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n    let res = 0;\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n    while (i < j) {\n        // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n        const cap: number = Math.min(ht[i], ht[j]) * (j - i);\n        res = Math.max(res, cap);\n        // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n        if (ht[i] < ht[j]) {\n            i += 1;\n        } else {\n            j -= 1;\n        }\n    }\n    return res;\n}\n
        max_capacity.dart
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(List<int> ht) {\n  // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n  int i = 0, j = ht.length - 1;\n  // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n  int res = 0;\n  // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n  while (i < j) {\n    // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n    int cap = min(ht[i], ht[j]) * (j - i);\n    res = max(res, cap);\n    // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n    if (ht[i] < ht[j]) {\n      i++;\n    } else {\n      j--;\n    }\n  }\n  return res;\n}\n
        max_capacity.rs
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfn max_capacity(ht: &[i32]) -> i32 {\n    // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n    let mut i = 0;\n    let mut j = ht.len() - 1;\n    // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n    let mut res = 0;\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n    while i < j {\n        // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n        let cap = std::cmp::min(ht[i], ht[j]) * (j - i) as i32;\n        res = std::cmp::max(res, cap);\n        // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n        if ht[i] < ht[j] {\n            i += 1;\n        } else {\n            j -= 1;\n        }\n    }\n    res\n}\n
        max_capacity.c
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(int ht[], int htLength) {\n    // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n    int i = 0;\n    int j = htLength - 1;\n    // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n    int res = 0;\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n    while (i < j) {\n        // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n        int capacity = myMin(ht[i], ht[j]) * (j - i);\n        res = myMax(res, capacity);\n        // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
        max_capacity.kt
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfun maxCapacity(ht: IntArray): Int {\n    // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n    var i = 0\n    var j = ht.size - 1\n    // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n    var res = 0\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n    while (i < j) {\n        // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n        val cap = min(ht[i], ht[j]) * (j - i)\n        res = max(res, cap)\n        // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n        if (ht[i] < ht[j]) {\n            i++\n        } else {\n            j--\n        }\n    }\n    return res\n}\n
        max_capacity.rb
        ### \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 ###\ndef max_capacity(ht)\n  # \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n  i, j = 0, ht.length - 1\n  # \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n  res = 0\n\n  # \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n  while i < j\n    # \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n    cap = [ht[i], ht[j]].min * (j - i)\n    res = [res, cap].max\n    # \u5411\u5185\u79fb\u52a8\u77ed\u677f\n    if ht[i] < ht[j]\n      i += 1\n    else\n      j -= 1\n    end\n  end\n\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_greedy/max_capacity_problem/#3","title":"3. \u00a0 \u6b63\u786e\u6027\u8bc1\u660e","text":"

        \u4e4b\u6240\u4ee5\u8d2a\u5fc3\u6bd4\u7a77\u4e3e\u66f4\u5feb\uff0c\u662f\u56e0\u4e3a\u6bcf\u8f6e\u7684\u8d2a\u5fc3\u9009\u62e9\u90fd\u4f1a\u201c\u8df3\u8fc7\u201d\u4e00\u4e9b\u72b6\u6001\u3002

        \u6bd4\u5982\u5728\u72b6\u6001 \\(cap[i, j]\\) \u4e0b\uff0c\\(i\\) \u4e3a\u77ed\u677f\u3001\\(j\\) \u4e3a\u957f\u677f\u3002\u82e5\u8d2a\u5fc3\u5730\u5c06\u77ed\u677f \\(i\\) \u5411\u5185\u79fb\u52a8\u4e00\u683c\uff0c\u4f1a\u5bfc\u81f4\u56fe 15-12 \u6240\u793a\u7684\u72b6\u6001\u88ab\u201c\u8df3\u8fc7\u201d\u3002\u8fd9\u610f\u5473\u7740\u4e4b\u540e\u65e0\u6cd5\u9a8c\u8bc1\u8fd9\u4e9b\u72b6\u6001\u7684\u5bb9\u91cf\u5927\u5c0f\u3002

        \\[ cap[i, i+1], cap[i, i+2], \\dots, cap[i, j-2], cap[i, j-1] \\]

        \u56fe 15-12 \u00a0 \u79fb\u52a8\u77ed\u677f\u5bfc\u81f4\u88ab\u8df3\u8fc7\u7684\u72b6\u6001

        \u89c2\u5bdf\u53d1\u73b0\uff0c\u8fd9\u4e9b\u88ab\u8df3\u8fc7\u7684\u72b6\u6001\u5b9e\u9645\u4e0a\u5c31\u662f\u5c06\u957f\u677f \\(j\\) \u5411\u5185\u79fb\u52a8\u7684\u6240\u6709\u72b6\u6001\u3002\u524d\u9762\u6211\u4eec\u5df2\u7ecf\u8bc1\u660e\u5185\u79fb\u957f\u677f\u4e00\u5b9a\u4f1a\u5bfc\u81f4\u5bb9\u91cf\u53d8\u5c0f\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u88ab\u8df3\u8fc7\u7684\u72b6\u6001\u90fd\u4e0d\u53ef\u80fd\u662f\u6700\u4f18\u89e3\uff0c\u8df3\u8fc7\u5b83\u4eec\u4e0d\u4f1a\u5bfc\u81f4\u9519\u8fc7\u6700\u4f18\u89e3\u3002

        \u4ee5\u4e0a\u5206\u6790\u8bf4\u660e\uff0c\u79fb\u52a8\u77ed\u677f\u7684\u64cd\u4f5c\u662f\u201c\u5b89\u5168\u201d\u7684\uff0c\u8d2a\u5fc3\u7b56\u7565\u662f\u6709\u6548\u7684\u3002

        "},{"location":"chapter_greedy/max_product_cutting_problem/","title":"15.4 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u95ee\u9898","text":"

        Question

        \u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570 \\(n\\) \uff0c\u5c06\u5176\u5207\u5206\u4e3a\u81f3\u5c11\u4e24\u4e2a\u6b63\u6574\u6570\u7684\u548c\uff0c\u6c42\u5207\u5206\u540e\u6240\u6709\u6574\u6570\u7684\u4e58\u79ef\u6700\u5927\u662f\u591a\u5c11\uff0c\u5982\u56fe 15-13 \u6240\u793a\u3002

        \u56fe 15-13 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u7684\u95ee\u9898\u5b9a\u4e49

        \u5047\u8bbe\u6211\u4eec\u5c06 \\(n\\) \u5207\u5206\u4e3a \\(m\\) \u4e2a\u6574\u6570\u56e0\u5b50\uff0c\u5176\u4e2d\u7b2c \\(i\\) \u4e2a\u56e0\u5b50\u8bb0\u4e3a \\(n_i\\) \uff0c\u5373

        \\[ n = \\sum_{i=1}^{m}n_i \\]

        \u672c\u9898\u7684\u76ee\u6807\u662f\u6c42\u5f97\u6240\u6709\u6574\u6570\u56e0\u5b50\u7684\u6700\u5927\u4e58\u79ef\uff0c\u5373

        \\[ \\max(\\prod_{i=1}^{m}n_i) \\]

        \u6211\u4eec\u9700\u8981\u601d\u8003\u7684\u662f\uff1a\u5207\u5206\u6570\u91cf \\(m\\) \u5e94\u8be5\u591a\u5927\uff0c\u6bcf\u4e2a \\(n_i\\) \u5e94\u8be5\u662f\u591a\u5c11\uff1f

        "},{"location":"chapter_greedy/max_product_cutting_problem/#1","title":"1. \u00a0 \u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a","text":"

        \u6839\u636e\u7ecf\u9a8c\uff0c\u4e24\u4e2a\u6574\u6570\u7684\u4e58\u79ef\u5f80\u5f80\u6bd4\u5b83\u4eec\u7684\u52a0\u548c\u66f4\u5927\u3002\u5047\u8bbe\u4ece \\(n\\) \u4e2d\u5206\u51fa\u4e00\u4e2a\u56e0\u5b50 \\(2\\) \uff0c\u5219\u5b83\u4eec\u7684\u4e58\u79ef\u4e3a \\(2(n-2)\\) \u3002\u6211\u4eec\u5c06\u8be5\u4e58\u79ef\u4e0e \\(n\\) \u4f5c\u6bd4\u8f83\uff1a

        \\[ \\begin{aligned} 2(n-2) & \\geq n \\newline 2n - n - 4 & \\geq 0 \\newline n & \\geq 4 \\end{aligned} \\]

        \u5982\u56fe 15-14 \u6240\u793a\uff0c\u5f53 \\(n \\geq 4\\) \u65f6\uff0c\u5207\u5206\u51fa\u4e00\u4e2a \\(2\\) \u540e\u4e58\u79ef\u4f1a\u53d8\u5927\uff0c\u8fd9\u8bf4\u660e\u5927\u4e8e\u7b49\u4e8e \\(4\\) \u7684\u6574\u6570\u90fd\u5e94\u8be5\u88ab\u5207\u5206\u3002

        \u8d2a\u5fc3\u7b56\u7565\u4e00\uff1a\u5982\u679c\u5207\u5206\u65b9\u6848\u4e2d\u5305\u542b \\(\\geq 4\\) \u7684\u56e0\u5b50\uff0c\u90a3\u4e48\u5b83\u5c31\u5e94\u8be5\u88ab\u7ee7\u7eed\u5207\u5206\u3002\u6700\u7ec8\u7684\u5207\u5206\u65b9\u6848\u53ea\u5e94\u51fa\u73b0 \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u8fd9\u4e09\u79cd\u56e0\u5b50\u3002

        \u56fe 15-14 \u00a0 \u5207\u5206\u5bfc\u81f4\u4e58\u79ef\u53d8\u5927

        \u63a5\u4e0b\u6765\u601d\u8003\u54ea\u4e2a\u56e0\u5b50\u662f\u6700\u4f18\u7684\u3002\u5728 \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u8fd9\u4e09\u4e2a\u56e0\u5b50\u4e2d\uff0c\u663e\u7136 \\(1\\) \u662f\u6700\u5dee\u7684\uff0c\u56e0\u4e3a \\(1 \\times (n-1) < n\\) \u6052\u6210\u7acb\uff0c\u5373\u5207\u5206\u51fa \\(1\\) \u53cd\u800c\u4f1a\u5bfc\u81f4\u4e58\u79ef\u51cf\u5c0f\u3002

        \u5982\u56fe 15-15 \u6240\u793a\uff0c\u5f53 \\(n = 6\\) \u65f6\uff0c\u6709 \\(3 \\times 3 > 2 \\times 2 \\times 2\\) \u3002\u8fd9\u610f\u5473\u7740\u5207\u5206\u51fa \\(3\\) \u6bd4\u5207\u5206\u51fa \\(2\\) \u66f4\u4f18\u3002

        \u8d2a\u5fc3\u7b56\u7565\u4e8c\uff1a\u5728\u5207\u5206\u65b9\u6848\u4e2d\uff0c\u6700\u591a\u53ea\u5e94\u5b58\u5728\u4e24\u4e2a \\(2\\) \u3002\u56e0\u4e3a\u4e09\u4e2a \\(2\\) \u603b\u662f\u53ef\u4ee5\u66ff\u6362\u4e3a\u4e24\u4e2a \\(3\\) \uff0c\u4ece\u800c\u83b7\u5f97\u66f4\u5927\u7684\u4e58\u79ef\u3002

        \u56fe 15-15 \u00a0 \u6700\u4f18\u5207\u5206\u56e0\u5b50

        \u7efc\u4e0a\u6240\u8ff0\uff0c\u53ef\u63a8\u7406\u51fa\u4ee5\u4e0b\u8d2a\u5fc3\u7b56\u7565\u3002

        1. \u8f93\u5165\u6574\u6570 \\(n\\) \uff0c\u4ece\u5176\u4e0d\u65ad\u5730\u5207\u5206\u51fa\u56e0\u5b50 \\(3\\) \uff0c\u76f4\u81f3\u4f59\u6570\u4e3a \\(0\\)\u3001\\(1\\)\u3001\\(2\\) \u3002
        2. \u5f53\u4f59\u6570\u4e3a \\(0\\) \u65f6\uff0c\u4ee3\u8868 \\(n\\) \u662f \\(3\\) \u7684\u500d\u6570\uff0c\u56e0\u6b64\u4e0d\u505a\u4efb\u4f55\u5904\u7406\u3002
        3. \u5f53\u4f59\u6570\u4e3a \\(2\\) \u65f6\uff0c\u4e0d\u7ee7\u7eed\u5212\u5206\uff0c\u4fdd\u7559\u3002
        4. \u5f53\u4f59\u6570\u4e3a \\(1\\) \u65f6\uff0c\u7531\u4e8e \\(2 \\times 2 > 1 \\times 3\\) \uff0c\u56e0\u6b64\u5e94\u5c06\u6700\u540e\u4e00\u4e2a \\(3\\) \u66ff\u6362\u4e3a \\(2\\) \u3002
        "},{"location":"chapter_greedy/max_product_cutting_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u5982\u56fe 15-16 \u6240\u793a\uff0c\u6211\u4eec\u65e0\u987b\u901a\u8fc7\u5faa\u73af\u6765\u5207\u5206\u6574\u6570\uff0c\u800c\u53ef\u4ee5\u5229\u7528\u5411\u4e0b\u6574\u9664\u8fd0\u7b97\u5f97\u5230 \\(3\\) \u7684\u4e2a\u6570 \\(a\\) \uff0c\u7528\u53d6\u6a21\u8fd0\u7b97\u5f97\u5230\u4f59\u6570 \\(b\\) \uff0c\u6b64\u65f6\u6709\uff1a

        \\[ n = 3 a + b \\]

        \u8bf7\u6ce8\u610f\uff0c\u5bf9\u4e8e \\(n \\leq 3\\) \u7684\u8fb9\u754c\u60c5\u51b5\uff0c\u5fc5\u987b\u62c6\u5206\u51fa\u4e00\u4e2a \\(1\\) \uff0c\u4e58\u79ef\u4e3a \\(1 \\times (n - 1)\\) \u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby max_product_cutting.py
        def max_product_cutting(n: int) -> int:\n    \"\"\"\u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3\"\"\"\n    # \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n    if n <= 3:\n        return 1 * (n - 1)\n    # \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n    a, b = n // 3, n % 3\n    if b == 1:\n        # \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n        return int(math.pow(3, a - 1)) * 2 * 2\n    if b == 2:\n        # \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        return int(math.pow(3, a)) * 2\n    # \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n    return int(math.pow(3, a))\n
        max_product_cutting.cpp
        /* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n    // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n        return (int)pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        return (int)pow(3, a) * 2;\n    }\n    // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n    return (int)pow(3, a);\n}\n
        max_product_cutting.java
        /* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n    // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n        return (int) Math.pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        return (int) Math.pow(3, a) * 2;\n    }\n    // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n    return (int) Math.pow(3, a);\n}\n
        max_product_cutting.cs
        /* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint MaxProductCutting(int n) {\n    // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n        return (int)Math.Pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        return (int)Math.Pow(3, a) * 2;\n    }\n    // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n    return (int)Math.Pow(3, a);\n}\n
        max_product_cutting.go
        /* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunc maxProductCutting(n int) int {\n    // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n    if n <= 3 {\n        return 1 * (n - 1)\n    }\n    // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n    a := n / 3\n    b := n % 3\n    if b == 1 {\n        // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n        return int(math.Pow(3, float64(a-1))) * 2 * 2\n    }\n    if b == 2 {\n        // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        return int(math.Pow(3, float64(a))) * 2\n    }\n    // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n    return int(math.Pow(3, float64(a)))\n}\n
        max_product_cutting.swift
        /* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunc maxProductCutting(n: Int) -> Int {\n    // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n    if n <= 3 {\n        return 1 * (n - 1)\n    }\n    // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n    let a = n / 3\n    let b = n % 3\n    if b == 1 {\n        // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n        return pow(3, a - 1) * 2 * 2\n    }\n    if b == 2 {\n        // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        return pow(3, a) * 2\n    }\n    // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n    return pow(3, a)\n}\n
        max_product_cutting.js
        /* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunction maxProductCutting(n) {\n    // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n    let a = Math.floor(n / 3);\n    let b = n % 3;\n    if (b === 1) {\n        // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n        return Math.pow(3, a - 1) * 2 * 2;\n    }\n    if (b === 2) {\n        // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        return Math.pow(3, a) * 2;\n    }\n    // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n    return Math.pow(3, a);\n}\n
        max_product_cutting.ts
        /* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunction maxProductCutting(n: number): number {\n    // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n    let a: number = Math.floor(n / 3);\n    let b: number = n % 3;\n    if (b === 1) {\n        // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n        return Math.pow(3, a - 1) * 2 * 2;\n    }\n    if (b === 2) {\n        // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        return Math.pow(3, a) * 2;\n    }\n    // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n    return Math.pow(3, a);\n}\n
        max_product_cutting.dart
        /* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n  // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n  if (n <= 3) {\n    return 1 * (n - 1);\n  }\n  // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n  int a = n ~/ 3;\n  int b = n % 3;\n  if (b == 1) {\n    // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n    return (pow(3, a - 1) * 2 * 2).toInt();\n  }\n  if (b == 2) {\n    // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n    return (pow(3, a) * 2).toInt();\n  }\n  // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n  return pow(3, a).toInt();\n}\n
        max_product_cutting.rs
        /* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfn max_product_cutting(n: i32) -> i32 {\n    // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n    if n <= 3 {\n        return 1 * (n - 1);\n    }\n    // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n    let a = n / 3;\n    let b = n % 3;\n    if b == 1 {\n        // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n        3_i32.pow(a as u32 - 1) * 2 * 2\n    } else if b == 2 {\n        // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        3_i32.pow(a as u32) * 2\n    } else {\n        // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        3_i32.pow(a as u32)\n    }\n}\n
        max_product_cutting.c
        /* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n    // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n        return pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        return pow(3, a) * 2;\n    }\n    // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n    return pow(3, a);\n}\n
        max_product_cutting.kt
        /* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfun maxProductCutting(n: Int): Int {\n    // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n    if (n <= 3) {\n        return 1 * (n - 1)\n    }\n    // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n    val a = n / 3\n    val b = n % 3\n    if (b == 1) {\n        // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n        return 3.0.pow((a - 1)).toInt() * 2 * 2\n    }\n    if (b == 2) {\n        // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        return 3.0.pow(a).toInt() * 2 * 2\n    }\n    // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n    return 3.0.pow(a).toInt()\n}\n
        max_product_cutting.rb
        ### \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 ###\ndef max_product_cutting(n)\n  # \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n  return 1 * (n - 1) if n <= 3\n  # \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n  a, b = n / 3, n % 3\n  # \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n  return (3.pow(a - 1) * 2 * 2).to_i if b == 1\n  # \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n  return (3.pow(a) * 2).to_i if b == 2\n  # \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n  3.pow(a).to_i\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 15-16 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u7684\u8ba1\u7b97\u65b9\u6cd5

        \u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u7f16\u7a0b\u8bed\u8a00\u7684\u5e42\u8fd0\u7b97\u7684\u5b9e\u73b0\u65b9\u6cd5\u3002\u4ee5 Python \u4e3a\u4f8b\uff0c\u5e38\u7528\u7684\u5e42\u8ba1\u7b97\u51fd\u6570\u6709\u4e09\u79cd\u3002

        • \u8fd0\u7b97\u7b26 ** \u548c\u51fd\u6570 pow() \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(\\log\u2061 a)\\) \u3002
        • \u51fd\u6570 math.pow() \u5185\u90e8\u8c03\u7528 C \u8bed\u8a00\u5e93\u7684 pow() \u51fd\u6570\uff0c\u5176\u6267\u884c\u6d6e\u70b9\u53d6\u5e42\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002

        \u53d8\u91cf \\(a\\) \u548c \\(b\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002

        "},{"location":"chapter_greedy/max_product_cutting_problem/#3","title":"3. \u00a0 \u6b63\u786e\u6027\u8bc1\u660e","text":"

        \u4f7f\u7528\u53cd\u8bc1\u6cd5\uff0c\u53ea\u5206\u6790 \\(n \\geq 4\\) \u7684\u60c5\u51b5\u3002

        1. \u6240\u6709\u56e0\u5b50 \\(\\leq 3\\) \uff1a\u5047\u8bbe\u6700\u4f18\u5207\u5206\u65b9\u6848\u4e2d\u5b58\u5728 \\(\\geq 4\\) \u7684\u56e0\u5b50 \\(x\\) \uff0c\u90a3\u4e48\u4e00\u5b9a\u53ef\u4ee5\u5c06\u5176\u7ee7\u7eed\u5212\u5206\u4e3a \\(2(x-2)\\) \uff0c\u4ece\u800c\u83b7\u5f97\u66f4\u5927\uff08\u6216\u76f8\u7b49\uff09\u7684\u4e58\u79ef\u3002\u8fd9\u4e0e\u5047\u8bbe\u77db\u76fe\u3002
        2. \u5207\u5206\u65b9\u6848\u4e0d\u5305\u542b \\(1\\) \uff1a\u5047\u8bbe\u6700\u4f18\u5207\u5206\u65b9\u6848\u4e2d\u5b58\u5728\u4e00\u4e2a\u56e0\u5b50 \\(1\\) \uff0c\u90a3\u4e48\u5b83\u4e00\u5b9a\u53ef\u4ee5\u5408\u5e76\u5165\u53e6\u5916\u4e00\u4e2a\u56e0\u5b50\u4e2d\uff0c\u4ee5\u83b7\u5f97\u66f4\u5927\u7684\u4e58\u79ef\u3002\u8fd9\u4e0e\u5047\u8bbe\u77db\u76fe\u3002
        3. \u5207\u5206\u65b9\u6848\u6700\u591a\u5305\u542b\u4e24\u4e2a \\(2\\) \uff1a\u5047\u8bbe\u6700\u4f18\u5207\u5206\u65b9\u6848\u4e2d\u5305\u542b\u4e09\u4e2a \\(2\\) \uff0c\u90a3\u4e48\u4e00\u5b9a\u53ef\u4ee5\u66ff\u6362\u4e3a\u4e24\u4e2a \\(3\\) \uff0c\u4e58\u79ef\u66f4\u5927\u3002\u8fd9\u4e0e\u5047\u8bbe\u77db\u76fe\u3002
        "},{"location":"chapter_greedy/summary/","title":"15.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_greedy/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u8d2a\u5fc3\u7b97\u6cd5\u901a\u5e38\u7528\u4e8e\u89e3\u51b3\u6700\u4f18\u5316\u95ee\u9898\uff0c\u5176\u539f\u7406\u662f\u5728\u6bcf\u4e2a\u51b3\u7b56\u9636\u6bb5\u90fd\u505a\u51fa\u5c40\u90e8\u6700\u4f18\u7684\u51b3\u7b56\uff0c\u4ee5\u671f\u83b7\u5f97\u5168\u5c40\u6700\u4f18\u89e3\u3002
        • \u8d2a\u5fc3\u7b97\u6cd5\u4f1a\u8fed\u4ee3\u5730\u505a\u51fa\u4e00\u4e2a\u53c8\u4e00\u4e2a\u7684\u8d2a\u5fc3\u9009\u62e9\uff0c\u6bcf\u8f6e\u90fd\u5c06\u95ee\u9898\u8f6c\u5316\u6210\u4e00\u4e2a\u89c4\u6a21\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u76f4\u5230\u95ee\u9898\u88ab\u89e3\u51b3\u3002
        • \u8d2a\u5fc3\u7b97\u6cd5\u4e0d\u4ec5\u5b9e\u73b0\u7b80\u5355\uff0c\u8fd8\u5177\u6709\u5f88\u9ad8\u7684\u89e3\u9898\u6548\u7387\u3002\u76f8\u6bd4\u4e8e\u52a8\u6001\u89c4\u5212\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u66f4\u4f4e\u3002
        • \u5728\u96f6\u94b1\u5151\u6362\u95ee\u9898\u4e2d\uff0c\u5bf9\u4e8e\u67d0\u4e9b\u786c\u5e01\u7ec4\u5408\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u53ef\u4ee5\u4fdd\u8bc1\u627e\u5230\u6700\u4f18\u89e3\uff1b\u5bf9\u4e8e\u53e6\u5916\u4e00\u4e9b\u786c\u5e01\u7ec4\u5408\u5219\u4e0d\u7136\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u53ef\u80fd\u627e\u5230\u5f88\u5dee\u7684\u89e3\u3002
        • \u9002\u5408\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\u7684\u95ee\u9898\u5177\u6709\u4e24\u5927\u6027\u8d28\uff1a\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u548c\u6700\u4f18\u5b50\u7ed3\u6784\u3002\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u4ee3\u8868\u8d2a\u5fc3\u7b56\u7565\u7684\u6709\u6548\u6027\u3002
        • \u5bf9\u4e8e\u67d0\u4e9b\u590d\u6742\u95ee\u9898\uff0c\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u7684\u8bc1\u660e\u5e76\u4e0d\u7b80\u5355\u3002\u76f8\u5bf9\u6765\u8bf4\uff0c\u8bc1\u4f2a\u66f4\u52a0\u5bb9\u6613\uff0c\u4f8b\u5982\u96f6\u94b1\u5151\u6362\u95ee\u9898\u3002
        • \u6c42\u89e3\u8d2a\u5fc3\u95ee\u9898\u4e3b\u8981\u5206\u4e3a\u4e09\u6b65\uff1a\u95ee\u9898\u5206\u6790\u3001\u786e\u5b9a\u8d2a\u5fc3\u7b56\u7565\u3001\u6b63\u786e\u6027\u8bc1\u660e\u3002\u5176\u4e2d\uff0c\u786e\u5b9a\u8d2a\u5fc3\u7b56\u7565\u662f\u6838\u5fc3\u6b65\u9aa4\uff0c\u6b63\u786e\u6027\u8bc1\u660e\u5f80\u5f80\u662f\u96be\u70b9\u3002
        • \u5206\u6570\u80cc\u5305\u95ee\u9898\u5728 0-1 \u80cc\u5305\u7684\u57fa\u7840\u4e0a\uff0c\u5141\u8bb8\u9009\u62e9\u7269\u54c1\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u53ef\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\u3002\u8d2a\u5fc3\u7b56\u7565\u7684\u6b63\u786e\u6027\u53ef\u4ee5\u4f7f\u7528\u53cd\u8bc1\u6cd5\u6765\u8bc1\u660e\u3002
        • \u6700\u5927\u5bb9\u91cf\u95ee\u9898\u53ef\u4f7f\u7528\u7a77\u4e3e\u6cd5\u6c42\u89e3\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002\u901a\u8fc7\u8bbe\u8ba1\u8d2a\u5fc3\u7b56\u7565\uff0c\u6bcf\u8f6e\u5411\u5185\u79fb\u52a8\u77ed\u677f\uff0c\u53ef\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(n)\\) \u3002
        • \u5728\u6700\u5927\u5207\u5206\u4e58\u79ef\u95ee\u9898\u4e2d\uff0c\u6211\u4eec\u5148\u540e\u63a8\u7406\u51fa\u4e24\u4e2a\u8d2a\u5fc3\u7b56\u7565\uff1a\\(\\geq 4\\) \u7684\u6574\u6570\u90fd\u5e94\u8be5\u7ee7\u7eed\u5207\u5206\uff0c\u6700\u4f18\u5207\u5206\u56e0\u5b50\u4e3a \\(3\\) \u3002\u4ee3\u7801\u4e2d\u5305\u542b\u5e42\u8fd0\u7b97\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u5e42\u8fd0\u7b97\u5b9e\u73b0\u65b9\u6cd5\uff0c\u901a\u5e38\u4e3a \\(O(1)\\) \u6216 \\(O(\\log n)\\) \u3002
        "},{"location":"chapter_hashing/","title":"\u7b2c 6 \u7ae0 \u00a0 \u54c8\u5e0c\u8868","text":"

        Abstract

        \u5728\u8ba1\u7b97\u673a\u4e16\u754c\u4e2d\uff0c\u54c8\u5e0c\u8868\u5982\u540c\u4e00\u4f4d\u806a\u6167\u7684\u56fe\u4e66\u7ba1\u7406\u5458\u3002

        \u4ed6\u77e5\u9053\u5982\u4f55\u8ba1\u7b97\u7d22\u4e66\u53f7\uff0c\u4ece\u800c\u53ef\u4ee5\u5feb\u901f\u627e\u5230\u76ee\u6807\u56fe\u4e66\u3002

        "},{"location":"chapter_hashing/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 6.1 \u00a0 \u54c8\u5e0c\u8868
        • 6.2 \u00a0 \u54c8\u5e0c\u51b2\u7a81
        • 6.3 \u00a0 \u54c8\u5e0c\u7b97\u6cd5
        • 6.4 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_hashing/hash_algorithm/","title":"6.3 \u00a0 \u54c8\u5e0c\u7b97\u6cd5","text":"

        \u524d\u4e24\u8282\u4ecb\u7ecd\u4e86\u54c8\u5e0c\u8868\u7684\u5de5\u4f5c\u539f\u7406\u548c\u54c8\u5e0c\u51b2\u7a81\u7684\u5904\u7406\u65b9\u6cd5\u3002\u7136\u800c\u65e0\u8bba\u662f\u5f00\u653e\u5bfb\u5740\u8fd8\u662f\u94fe\u5f0f\u5730\u5740\uff0c\u5b83\u4eec\u53ea\u80fd\u4fdd\u8bc1\u54c8\u5e0c\u8868\u53ef\u4ee5\u5728\u53d1\u751f\u51b2\u7a81\u65f6\u6b63\u5e38\u5de5\u4f5c\uff0c\u800c\u65e0\u6cd5\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u7684\u53d1\u751f\u3002

        \u5982\u679c\u54c8\u5e0c\u51b2\u7a81\u8fc7\u4e8e\u9891\u7e41\uff0c\u54c8\u5e0c\u8868\u7684\u6027\u80fd\u5219\u4f1a\u6025\u5267\u52a3\u5316\u3002\u5982\u56fe 6-8 \u6240\u793a\uff0c\u5bf9\u4e8e\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\uff0c\u7406\u60f3\u60c5\u51b5\u4e0b\u952e\u503c\u5bf9\u5747\u5300\u5206\u5e03\u5728\u5404\u4e2a\u6876\u4e2d\uff0c\u8fbe\u5230\u6700\u4f73\u67e5\u8be2\u6548\u7387\uff1b\u6700\u5dee\u60c5\u51b5\u4e0b\u6240\u6709\u952e\u503c\u5bf9\u90fd\u5b58\u50a8\u5230\u540c\u4e00\u4e2a\u6876\u4e2d\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u9000\u5316\u81f3 \\(O(n)\\) \u3002

        \u56fe 6-8 \u00a0 \u54c8\u5e0c\u51b2\u7a81\u7684\u6700\u4f73\u60c5\u51b5\u4e0e\u6700\u5dee\u60c5\u51b5

        \u952e\u503c\u5bf9\u7684\u5206\u5e03\u60c5\u51b5\u7531\u54c8\u5e0c\u51fd\u6570\u51b3\u5b9a\u3002\u56de\u5fc6\u54c8\u5e0c\u51fd\u6570\u7684\u8ba1\u7b97\u6b65\u9aa4\uff0c\u5148\u8ba1\u7b97\u54c8\u5e0c\u503c\uff0c\u518d\u5bf9\u6570\u7ec4\u957f\u5ea6\u53d6\u6a21\uff1a

        index = hash(key) % capacity\n

        \u89c2\u5bdf\u4ee5\u4e0a\u516c\u5f0f\uff0c\u5f53\u54c8\u5e0c\u8868\u5bb9\u91cf capacity \u56fa\u5b9a\u65f6\uff0c\u54c8\u5e0c\u7b97\u6cd5 hash() \u51b3\u5b9a\u4e86\u8f93\u51fa\u503c\uff0c\u8fdb\u800c\u51b3\u5b9a\u4e86\u952e\u503c\u5bf9\u5728\u54c8\u5e0c\u8868\u4e2d\u7684\u5206\u5e03\u60c5\u51b5\u3002

        \u8fd9\u610f\u5473\u7740\uff0c\u4e3a\u4e86\u964d\u4f4e\u54c8\u5e0c\u51b2\u7a81\u7684\u53d1\u751f\u6982\u7387\uff0c\u6211\u4eec\u5e94\u5f53\u5c06\u6ce8\u610f\u529b\u96c6\u4e2d\u5728\u54c8\u5e0c\u7b97\u6cd5 hash() \u7684\u8bbe\u8ba1\u4e0a\u3002

        "},{"location":"chapter_hashing/hash_algorithm/#631","title":"6.3.1 \u00a0 \u54c8\u5e0c\u7b97\u6cd5\u7684\u76ee\u6807","text":"

        \u4e3a\u4e86\u5b9e\u73b0\u201c\u65e2\u5feb\u53c8\u7a33\u201d\u7684\u54c8\u5e0c\u8868\u6570\u636e\u7ed3\u6784\uff0c\u54c8\u5e0c\u7b97\u6cd5\u5e94\u5177\u5907\u4ee5\u4e0b\u7279\u70b9\u3002

        • \u786e\u5b9a\u6027\uff1a\u5bf9\u4e8e\u76f8\u540c\u7684\u8f93\u5165\uff0c\u54c8\u5e0c\u7b97\u6cd5\u5e94\u59cb\u7ec8\u4ea7\u751f\u76f8\u540c\u7684\u8f93\u51fa\u3002\u8fd9\u6837\u624d\u80fd\u786e\u4fdd\u54c8\u5e0c\u8868\u662f\u53ef\u9760\u7684\u3002
        • \u6548\u7387\u9ad8\uff1a\u8ba1\u7b97\u54c8\u5e0c\u503c\u7684\u8fc7\u7a0b\u5e94\u8be5\u8db3\u591f\u5feb\u3002\u8ba1\u7b97\u5f00\u9500\u8d8a\u5c0f\uff0c\u54c8\u5e0c\u8868\u7684\u5b9e\u7528\u6027\u8d8a\u9ad8\u3002
        • \u5747\u5300\u5206\u5e03\uff1a\u54c8\u5e0c\u7b97\u6cd5\u5e94\u4f7f\u5f97\u952e\u503c\u5bf9\u5747\u5300\u5206\u5e03\u5728\u54c8\u5e0c\u8868\u4e2d\u3002\u5206\u5e03\u8d8a\u5747\u5300\uff0c\u54c8\u5e0c\u51b2\u7a81\u7684\u6982\u7387\u5c31\u8d8a\u4f4e\u3002

        \u5b9e\u9645\u4e0a\uff0c\u54c8\u5e0c\u7b97\u6cd5\u9664\u4e86\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u54c8\u5e0c\u8868\uff0c\u8fd8\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5176\u4ed6\u9886\u57df\u4e2d\u3002

        • \u5bc6\u7801\u5b58\u50a8\uff1a\u4e3a\u4e86\u4fdd\u62a4\u7528\u6237\u5bc6\u7801\u7684\u5b89\u5168\uff0c\u7cfb\u7edf\u901a\u5e38\u4e0d\u4f1a\u76f4\u63a5\u5b58\u50a8\u7528\u6237\u7684\u660e\u6587\u5bc6\u7801\uff0c\u800c\u662f\u5b58\u50a8\u5bc6\u7801\u7684\u54c8\u5e0c\u503c\u3002\u5f53\u7528\u6237\u8f93\u5165\u5bc6\u7801\u65f6\uff0c\u7cfb\u7edf\u4f1a\u5bf9\u8f93\u5165\u7684\u5bc6\u7801\u8ba1\u7b97\u54c8\u5e0c\u503c\uff0c\u7136\u540e\u4e0e\u5b58\u50a8\u7684\u54c8\u5e0c\u503c\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u4e24\u8005\u5339\u914d\uff0c\u90a3\u4e48\u5bc6\u7801\u5c31\u88ab\u89c6\u4e3a\u6b63\u786e\u3002
        • \u6570\u636e\u5b8c\u6574\u6027\u68c0\u67e5\uff1a\u6570\u636e\u53d1\u9001\u65b9\u53ef\u4ee5\u8ba1\u7b97\u6570\u636e\u7684\u54c8\u5e0c\u503c\u5e76\u5c06\u5176\u4e00\u540c\u53d1\u9001\uff1b\u63a5\u6536\u65b9\u53ef\u4ee5\u91cd\u65b0\u8ba1\u7b97\u63a5\u6536\u5230\u7684\u6570\u636e\u7684\u54c8\u5e0c\u503c\uff0c\u5e76\u4e0e\u63a5\u6536\u5230\u7684\u54c8\u5e0c\u503c\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u4e24\u8005\u5339\u914d\uff0c\u90a3\u4e48\u6570\u636e\u5c31\u88ab\u89c6\u4e3a\u5b8c\u6574\u3002

        \u5bf9\u4e8e\u5bc6\u7801\u5b66\u7684\u76f8\u5173\u5e94\u7528\uff0c\u4e3a\u4e86\u9632\u6b62\u4ece\u54c8\u5e0c\u503c\u63a8\u5bfc\u51fa\u539f\u59cb\u5bc6\u7801\u7b49\u9006\u5411\u5de5\u7a0b\uff0c\u54c8\u5e0c\u7b97\u6cd5\u9700\u8981\u5177\u5907\u66f4\u9ad8\u7b49\u7ea7\u7684\u5b89\u5168\u7279\u6027\u3002

        • \u5355\u5411\u6027\uff1a\u65e0\u6cd5\u901a\u8fc7\u54c8\u5e0c\u503c\u53cd\u63a8\u51fa\u5173\u4e8e\u8f93\u5165\u6570\u636e\u7684\u4efb\u4f55\u4fe1\u606f\u3002
        • \u6297\u78b0\u649e\u6027\uff1a\u5e94\u5f53\u6781\u96be\u627e\u5230\u4e24\u4e2a\u4e0d\u540c\u7684\u8f93\u5165\uff0c\u4f7f\u5f97\u5b83\u4eec\u7684\u54c8\u5e0c\u503c\u76f8\u540c\u3002
        • \u96ea\u5d29\u6548\u5e94\uff1a\u8f93\u5165\u7684\u5fae\u5c0f\u53d8\u5316\u5e94\u5f53\u5bfc\u81f4\u8f93\u51fa\u7684\u663e\u8457\u4e14\u4e0d\u53ef\u9884\u6d4b\u7684\u53d8\u5316\u3002

        \u8bf7\u6ce8\u610f\uff0c\u201c\u5747\u5300\u5206\u5e03\u201d\u4e0e\u201c\u6297\u78b0\u649e\u6027\u201d\u662f\u4e24\u4e2a\u72ec\u7acb\u7684\u6982\u5ff5\uff0c\u6ee1\u8db3\u5747\u5300\u5206\u5e03\u4e0d\u4e00\u5b9a\u6ee1\u8db3\u6297\u78b0\u649e\u6027\u3002\u4f8b\u5982\uff0c\u5728\u968f\u673a\u8f93\u5165 key \u4e0b\uff0c\u54c8\u5e0c\u51fd\u6570 key % 100 \u53ef\u4ee5\u4ea7\u751f\u5747\u5300\u5206\u5e03\u7684\u8f93\u51fa\u3002\u7136\u800c\u8be5\u54c8\u5e0c\u7b97\u6cd5\u8fc7\u4e8e\u7b80\u5355\uff0c\u6240\u6709\u540e\u4e24\u4f4d\u76f8\u7b49\u7684 key \u7684\u8f93\u51fa\u90fd\u76f8\u540c\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u4ece\u54c8\u5e0c\u503c\u53cd\u63a8\u51fa\u53ef\u7528\u7684 key \uff0c\u4ece\u800c\u7834\u89e3\u5bc6\u7801\u3002

        "},{"location":"chapter_hashing/hash_algorithm/#632","title":"6.3.2 \u00a0 \u54c8\u5e0c\u7b97\u6cd5\u7684\u8bbe\u8ba1","text":"

        \u54c8\u5e0c\u7b97\u6cd5\u7684\u8bbe\u8ba1\u662f\u4e00\u4e2a\u9700\u8981\u8003\u8651\u8bb8\u591a\u56e0\u7d20\u7684\u590d\u6742\u95ee\u9898\u3002\u7136\u800c\u5bf9\u4e8e\u67d0\u4e9b\u8981\u6c42\u4e0d\u9ad8\u7684\u573a\u666f\uff0c\u6211\u4eec\u4e5f\u80fd\u8bbe\u8ba1\u4e00\u4e9b\u7b80\u5355\u7684\u54c8\u5e0c\u7b97\u6cd5\u3002

        • \u52a0\u6cd5\u54c8\u5e0c\uff1a\u5bf9\u8f93\u5165\u7684\u6bcf\u4e2a\u5b57\u7b26\u7684 ASCII \u7801\u8fdb\u884c\u76f8\u52a0\uff0c\u5c06\u5f97\u5230\u7684\u603b\u548c\u4f5c\u4e3a\u54c8\u5e0c\u503c\u3002
        • \u4e58\u6cd5\u54c8\u5e0c\uff1a\u5229\u7528\u4e58\u6cd5\u7684\u4e0d\u76f8\u5173\u6027\uff0c\u6bcf\u8f6e\u4e58\u4ee5\u4e00\u4e2a\u5e38\u6570\uff0c\u5c06\u5404\u4e2a\u5b57\u7b26\u7684 ASCII \u7801\u7d2f\u79ef\u5230\u54c8\u5e0c\u503c\u4e2d\u3002
        • \u5f02\u6216\u54c8\u5e0c\uff1a\u5c06\u8f93\u5165\u6570\u636e\u7684\u6bcf\u4e2a\u5143\u7d20\u901a\u8fc7\u5f02\u6216\u64cd\u4f5c\u7d2f\u79ef\u5230\u4e00\u4e2a\u54c8\u5e0c\u503c\u4e2d\u3002
        • \u65cb\u8f6c\u54c8\u5e0c\uff1a\u5c06\u6bcf\u4e2a\u5b57\u7b26\u7684 ASCII \u7801\u7d2f\u79ef\u5230\u4e00\u4e2a\u54c8\u5e0c\u503c\u4e2d\uff0c\u6bcf\u6b21\u7d2f\u79ef\u4e4b\u524d\u90fd\u4f1a\u5bf9\u54c8\u5e0c\u503c\u8fdb\u884c\u65cb\u8f6c\u64cd\u4f5c\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby simple_hash.py
        def add_hash(key: str) -> int:\n    \"\"\"\u52a0\u6cd5\u54c8\u5e0c\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash += ord(c)\n    return hash % modulus\n\ndef mul_hash(key: str) -> int:\n    \"\"\"\u4e58\u6cd5\u54c8\u5e0c\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash = 31 * hash + ord(c)\n    return hash % modulus\n\ndef xor_hash(key: str) -> int:\n    \"\"\"\u5f02\u6216\u54c8\u5e0c\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash ^= ord(c)\n    return hash % modulus\n\ndef rot_hash(key: str) -> int:\n    \"\"\"\u65cb\u8f6c\u54c8\u5e0c\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash = (hash << 4) ^ (hash >> 28) ^ ord(c)\n    return hash % modulus\n
        simple_hash.cpp
        /* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(string key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash = (hash + (int)c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(string key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash = (31 * hash + (int)c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(string key) {\n    int hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash ^= (int)c;\n    }\n    return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(string key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ (int)c) % MODULUS;\n    }\n    return (int)hash;\n}\n
        simple_hash.java
        /* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(String key) {\n    long hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash = (hash + (int) c) % MODULUS;\n    }\n    return (int) hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(String key) {\n    long hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash = (31 * hash + (int) c) % MODULUS;\n    }\n    return (int) hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(String key) {\n    int hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash ^= (int) c;\n    }\n    return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(String key) {\n    long hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ (int) c) % MODULUS;\n    }\n    return (int) hash;\n}\n
        simple_hash.cs
        /* \u52a0\u6cd5\u54c8\u5e0c */\nint AddHash(string key) {\n    long hash = 0;\n    const int MODULUS = 1000000007;\n    foreach (char c in key) {\n        hash = (hash + c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint MulHash(string key) {\n    long hash = 0;\n    const int MODULUS = 1000000007;\n    foreach (char c in key) {\n        hash = (31 * hash + c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint XorHash(string key) {\n    int hash = 0;\n    const int MODULUS = 1000000007;\n    foreach (char c in key) {\n        hash ^= c;\n    }\n    return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint RotHash(string key) {\n    long hash = 0;\n    const int MODULUS = 1000000007;\n    foreach (char c in key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ c) % MODULUS;\n    }\n    return (int)hash;\n}\n
        simple_hash.go
        /* \u52a0\u6cd5\u54c8\u5e0c */\nfunc addHash(key string) int {\n    var hash int64\n    var modulus int64\n\n    modulus = 1000000007\n    for _, b := range []byte(key) {\n        hash = (hash + int64(b)) % modulus\n    }\n    return int(hash)\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunc mulHash(key string) int {\n    var hash int64\n    var modulus int64\n\n    modulus = 1000000007\n    for _, b := range []byte(key) {\n        hash = (31*hash + int64(b)) % modulus\n    }\n    return int(hash)\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunc xorHash(key string) int {\n    hash := 0\n    modulus := 1000000007\n    for _, b := range []byte(key) {\n        fmt.Println(int(b))\n        hash ^= int(b)\n        hash = (31*hash + int(b)) % modulus\n    }\n    return hash & modulus\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunc rotHash(key string) int {\n    var hash int64\n    var modulus int64\n\n    modulus = 1000000007\n    for _, b := range []byte(key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ int64(b)) % modulus\n    }\n    return int(hash)\n}\n
        simple_hash.swift
        /* \u52a0\u6cd5\u54c8\u5e0c */\nfunc addHash(key: String) -> Int {\n    var hash = 0\n    let MODULUS = 1_000_000_007\n    for c in key {\n        for scalar in c.unicodeScalars {\n            hash = (hash + Int(scalar.value)) % MODULUS\n        }\n    }\n    return hash\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunc mulHash(key: String) -> Int {\n    var hash = 0\n    let MODULUS = 1_000_000_007\n    for c in key {\n        for scalar in c.unicodeScalars {\n            hash = (31 * hash + Int(scalar.value)) % MODULUS\n        }\n    }\n    return hash\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunc xorHash(key: String) -> Int {\n    var hash = 0\n    let MODULUS = 1_000_000_007\n    for c in key {\n        for scalar in c.unicodeScalars {\n            hash ^= Int(scalar.value)\n        }\n    }\n    return hash & MODULUS\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunc rotHash(key: String) -> Int {\n    var hash = 0\n    let MODULUS = 1_000_000_007\n    for c in key {\n        for scalar in c.unicodeScalars {\n            hash = ((hash << 4) ^ (hash >> 28) ^ Int(scalar.value)) % MODULUS\n        }\n    }\n    return hash\n}\n
        simple_hash.js
        /* \u52a0\u6cd5\u54c8\u5e0c */\nfunction addHash(key) {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = (hash + c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunction mulHash(key) {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunction xorHash(key) {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash ^= c.charCodeAt(0);\n    }\n    return hash % MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunction rotHash(key) {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n
        simple_hash.ts
        /* \u52a0\u6cd5\u54c8\u5e0c */\nfunction addHash(key: string): number {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = (hash + c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunction mulHash(key: string): number {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunction xorHash(key: string): number {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash ^= c.charCodeAt(0);\n    }\n    return hash % MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunction rotHash(key: string): number {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n
        simple_hash.dart
        /* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(String key) {\n  int hash = 0;\n  final int MODULUS = 1000000007;\n  for (int i = 0; i < key.length; i++) {\n    hash = (hash + key.codeUnitAt(i)) % MODULUS;\n  }\n  return hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(String key) {\n  int hash = 0;\n  final int MODULUS = 1000000007;\n  for (int i = 0; i < key.length; i++) {\n    hash = (31 * hash + key.codeUnitAt(i)) % MODULUS;\n  }\n  return hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(String key) {\n  int hash = 0;\n  final int MODULUS = 1000000007;\n  for (int i = 0; i < key.length; i++) {\n    hash ^= key.codeUnitAt(i);\n  }\n  return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(String key) {\n  int hash = 0;\n  final int MODULUS = 1000000007;\n  for (int i = 0; i < key.length; i++) {\n    hash = ((hash << 4) ^ (hash >> 28) ^ key.codeUnitAt(i)) % MODULUS;\n  }\n  return hash;\n}\n
        simple_hash.rs
        /* \u52a0\u6cd5\u54c8\u5e0c */\nfn add_hash(key: &str) -> i32 {\n    let mut hash = 0_i64;\n    const MODULUS: i64 = 1000000007;\n\n    for c in key.chars() {\n        hash = (hash + c as i64) % MODULUS;\n    }\n\n    hash as i32\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfn mul_hash(key: &str) -> i32 {\n    let mut hash = 0_i64;\n    const MODULUS: i64 = 1000000007;\n\n    for c in key.chars() {\n        hash = (31 * hash + c as i64) % MODULUS;\n    }\n\n    hash as i32\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfn xor_hash(key: &str) -> i32 {\n    let mut hash = 0_i64;\n    const MODULUS: i64 = 1000000007;\n\n    for c in key.chars() {\n        hash ^= c as i64;\n    }\n\n    (hash & MODULUS) as i32\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfn rot_hash(key: &str) -> i32 {\n    let mut hash = 0_i64;\n    const MODULUS: i64 = 1000000007;\n\n    for c in key.chars() {\n        hash = ((hash << 4) ^ (hash >> 28) ^ c as i64) % MODULUS;\n    }\n\n    hash as i32\n}\n
        simple_hash.c
        /* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(char *key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (int i = 0; i < strlen(key); i++) {\n        hash = (hash + (unsigned char)key[i]) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(char *key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (int i = 0; i < strlen(key); i++) {\n        hash = (31 * hash + (unsigned char)key[i]) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(char *key) {\n    int hash = 0;\n    const int MODULUS = 1000000007;\n\n    for (int i = 0; i < strlen(key); i++) {\n        hash ^= (unsigned char)key[i];\n    }\n    return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(char *key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (int i = 0; i < strlen(key); i++) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ (unsigned char)key[i]) % MODULUS;\n    }\n\n    return (int)hash;\n}\n
        simple_hash.kt
        /* \u52a0\u6cd5\u54c8\u5e0c */\nfun addHash(key: String): Int {\n    var hash = 0L\n    val MODULUS = 1000000007\n    for (c in key.toCharArray()) {\n        hash = (hash + c.code) % MODULUS\n    }\n    return hash.toInt()\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfun mulHash(key: String): Int {\n    var hash = 0L\n    val MODULUS = 1000000007\n    for (c in key.toCharArray()) {\n        hash = (31 * hash + c.code) % MODULUS\n    }\n    return hash.toInt()\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfun xorHash(key: String): Int {\n    var hash = 0\n    val MODULUS = 1000000007\n    for (c in key.toCharArray()) {\n        hash = hash xor c.code\n    }\n    return hash and MODULUS\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfun rotHash(key: String): Int {\n    var hash = 0L\n    val MODULUS = 1000000007\n    for (c in key.toCharArray()) {\n        hash = ((hash shl 4) xor (hash shr 28) xor c.code.toLong()) % MODULUS\n    }\n    return hash.toInt()\n}\n
        simple_hash.rb
        ### \u52a0\u6cd5\u54c8\u5e0c ###\ndef add_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash += c.ord }\n\n  hash % modulus\nend\n\n### \u4e58\u6cd5\u54c8\u5e0c ###\ndef mul_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash = 31 * hash + c.ord }\n\n  hash % modulus\nend\n\n### \u5f02\u6216\u54c8\u5e0c ###\ndef xor_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash ^= c.ord }\n\n  hash % modulus\nend\n\n### \u65cb\u8f6c\u54c8\u5e0c ###\ndef rot_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash = (hash << 4) ^ (hash >> 28) ^ c.ord }\n\n  hash % modulus\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u89c2\u5bdf\u53d1\u73b0\uff0c\u6bcf\u79cd\u54c8\u5e0c\u7b97\u6cd5\u7684\u6700\u540e\u4e00\u6b65\u90fd\u662f\u5bf9\u5927\u8d28\u6570 \\(1000000007\\) \u53d6\u6a21\uff0c\u4ee5\u786e\u4fdd\u54c8\u5e0c\u503c\u5728\u5408\u9002\u7684\u8303\u56f4\u5185\u3002\u503c\u5f97\u601d\u8003\u7684\u662f\uff0c\u4e3a\u4ec0\u4e48\u8981\u5f3a\u8c03\u5bf9\u8d28\u6570\u53d6\u6a21\uff0c\u6216\u8005\u8bf4\u5bf9\u5408\u6570\u53d6\u6a21\u7684\u5f0a\u7aef\u662f\u4ec0\u4e48\uff1f\u8fd9\u662f\u4e00\u4e2a\u6709\u8da3\u7684\u95ee\u9898\u3002

        \u5148\u7ed9\u51fa\u7ed3\u8bba\uff1a\u4f7f\u7528\u5927\u8d28\u6570\u4f5c\u4e3a\u6a21\u6570\uff0c\u53ef\u4ee5\u6700\u5927\u5316\u5730\u4fdd\u8bc1\u54c8\u5e0c\u503c\u7684\u5747\u5300\u5206\u5e03\u3002\u56e0\u4e3a\u8d28\u6570\u4e0d\u4e0e\u5176\u4ed6\u6570\u5b57\u5b58\u5728\u516c\u7ea6\u6570\uff0c\u53ef\u4ee5\u51cf\u5c11\u56e0\u53d6\u6a21\u64cd\u4f5c\u800c\u4ea7\u751f\u7684\u5468\u671f\u6027\u6a21\u5f0f\uff0c\u4ece\u800c\u907f\u514d\u54c8\u5e0c\u51b2\u7a81\u3002

        \u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u6211\u4eec\u9009\u62e9\u5408\u6570 \\(9\\) \u4f5c\u4e3a\u6a21\u6570\uff0c\u5b83\u53ef\u4ee5\u88ab \\(3\\) \u6574\u9664\uff0c\u90a3\u4e48\u6240\u6709\u53ef\u4ee5\u88ab \\(3\\) \u6574\u9664\u7684 key \u90fd\u4f1a\u88ab\u6620\u5c04\u5230 \\(0\\)\u3001\\(3\\)\u3001\\(6\\) \u8fd9\u4e09\u4e2a\u54c8\u5e0c\u503c\u3002

        \\[ \\begin{aligned} \\text{modulus} & = 9 \\newline \\text{key} & = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\dots \\} \\newline \\text{hash} & = \\{ 0, 3, 6, 0, 3, 6, 0, 3, 6, 0, 3, 6,\\dots \\} \\end{aligned} \\]

        \u5982\u679c\u8f93\u5165 key \u6070\u597d\u6ee1\u8db3\u8fd9\u79cd\u7b49\u5dee\u6570\u5217\u7684\u6570\u636e\u5206\u5e03\uff0c\u90a3\u4e48\u54c8\u5e0c\u503c\u5c31\u4f1a\u51fa\u73b0\u805a\u5806\uff0c\u4ece\u800c\u52a0\u91cd\u54c8\u5e0c\u51b2\u7a81\u3002\u73b0\u5728\uff0c\u5047\u8bbe\u5c06 modulus \u66ff\u6362\u4e3a\u8d28\u6570 \\(13\\) \uff0c\u7531\u4e8e key \u548c modulus \u4e4b\u95f4\u4e0d\u5b58\u5728\u516c\u7ea6\u6570\uff0c\u56e0\u6b64\u8f93\u51fa\u7684\u54c8\u5e0c\u503c\u7684\u5747\u5300\u6027\u4f1a\u660e\u663e\u63d0\u5347\u3002

        \\[ \\begin{aligned} \\text{modulus} & = 13 \\newline \\text{key} & = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\dots \\} \\newline \\text{hash} & = \\{ 0, 3, 6, 9, 12, 2, 5, 8, 11, 1, 4, 7, \\dots \\} \\end{aligned} \\]

        \u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u5982\u679c\u80fd\u591f\u4fdd\u8bc1 key \u662f\u968f\u673a\u5747\u5300\u5206\u5e03\u7684\uff0c\u90a3\u4e48\u9009\u62e9\u8d28\u6570\u6216\u8005\u5408\u6570\u4f5c\u4e3a\u6a21\u6570\u90fd\u53ef\u4ee5\uff0c\u5b83\u4eec\u90fd\u80fd\u8f93\u51fa\u5747\u5300\u5206\u5e03\u7684\u54c8\u5e0c\u503c\u3002\u800c\u5f53 key \u7684\u5206\u5e03\u5b58\u5728\u67d0\u79cd\u5468\u671f\u6027\u65f6\uff0c\u5bf9\u5408\u6570\u53d6\u6a21\u66f4\u5bb9\u6613\u51fa\u73b0\u805a\u96c6\u73b0\u8c61\u3002

        \u603b\u800c\u8a00\u4e4b\uff0c\u6211\u4eec\u901a\u5e38\u9009\u53d6\u8d28\u6570\u4f5c\u4e3a\u6a21\u6570\uff0c\u5e76\u4e14\u8fd9\u4e2a\u8d28\u6570\u6700\u597d\u8db3\u591f\u5927\uff0c\u4ee5\u5c3d\u53ef\u80fd\u6d88\u9664\u5468\u671f\u6027\u6a21\u5f0f\uff0c\u63d0\u5347\u54c8\u5e0c\u7b97\u6cd5\u7684\u7a33\u5065\u6027\u3002

        "},{"location":"chapter_hashing/hash_algorithm/#633","title":"6.3.3 \u00a0 \u5e38\u89c1\u54c8\u5e0c\u7b97\u6cd5","text":"

        \u4e0d\u96be\u53d1\u73b0\uff0c\u4ee5\u4e0a\u4ecb\u7ecd\u7684\u7b80\u5355\u54c8\u5e0c\u7b97\u6cd5\u90fd\u6bd4\u8f83\u201c\u8106\u5f31\u201d\uff0c\u8fdc\u8fdc\u6ca1\u6709\u8fbe\u5230\u54c8\u5e0c\u7b97\u6cd5\u7684\u8bbe\u8ba1\u76ee\u6807\u3002\u4f8b\u5982\uff0c\u7531\u4e8e\u52a0\u6cd5\u548c\u5f02\u6216\u6ee1\u8db3\u4ea4\u6362\u5f8b\uff0c\u56e0\u6b64\u52a0\u6cd5\u54c8\u5e0c\u548c\u5f02\u6216\u54c8\u5e0c\u65e0\u6cd5\u533a\u5206\u5185\u5bb9\u76f8\u540c\u4f46\u987a\u5e8f\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\uff0c\u8fd9\u53ef\u80fd\u4f1a\u52a0\u5267\u54c8\u5e0c\u51b2\u7a81\uff0c\u5e76\u5f15\u8d77\u4e00\u4e9b\u5b89\u5168\u95ee\u9898\u3002

        \u5728\u5b9e\u9645\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u7528\u4e00\u4e9b\u6807\u51c6\u54c8\u5e0c\u7b97\u6cd5\uff0c\u4f8b\u5982 MD5\u3001SHA-1\u3001SHA-2 \u548c SHA-3 \u7b49\u3002\u5b83\u4eec\u53ef\u4ee5\u5c06\u4efb\u610f\u957f\u5ea6\u7684\u8f93\u5165\u6570\u636e\u6620\u5c04\u5230\u6052\u5b9a\u957f\u5ea6\u7684\u54c8\u5e0c\u503c\u3002

        \u8fd1\u4e00\u4e2a\u4e16\u7eaa\u4ee5\u6765\uff0c\u54c8\u5e0c\u7b97\u6cd5\u5904\u5728\u4e0d\u65ad\u5347\u7ea7\u4e0e\u4f18\u5316\u7684\u8fc7\u7a0b\u4e2d\u3002\u4e00\u90e8\u5206\u7814\u7a76\u4eba\u5458\u52aa\u529b\u63d0\u5347\u54c8\u5e0c\u7b97\u6cd5\u7684\u6027\u80fd\uff0c\u53e6\u4e00\u90e8\u5206\u7814\u7a76\u4eba\u5458\u548c\u9ed1\u5ba2\u5219\u81f4\u529b\u4e8e\u5bfb\u627e\u54c8\u5e0c\u7b97\u6cd5\u7684\u5b89\u5168\u6027\u95ee\u9898\u3002\u8868 6-2 \u5c55\u793a\u4e86\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u5e38\u89c1\u7684\u54c8\u5e0c\u7b97\u6cd5\u3002

        • MD5 \u548c SHA-1 \u5df2\u591a\u6b21\u88ab\u6210\u529f\u653b\u51fb\uff0c\u56e0\u6b64\u5b83\u4eec\u88ab\u5404\u7c7b\u5b89\u5168\u5e94\u7528\u5f03\u7528\u3002
        • SHA-2 \u7cfb\u5217\u4e2d\u7684 SHA-256 \u662f\u6700\u5b89\u5168\u7684\u54c8\u5e0c\u7b97\u6cd5\u4e4b\u4e00\uff0c\u4ecd\u672a\u51fa\u73b0\u6210\u529f\u7684\u653b\u51fb\u6848\u4f8b\uff0c\u56e0\u6b64\u5e38\u7528\u5728\u5404\u7c7b\u5b89\u5168\u5e94\u7528\u4e0e\u534f\u8bae\u4e2d\u3002
        • SHA-3 \u76f8\u8f83 SHA-2 \u7684\u5b9e\u73b0\u5f00\u9500\u66f4\u4f4e\u3001\u8ba1\u7b97\u6548\u7387\u66f4\u9ad8\uff0c\u4f46\u76ee\u524d\u4f7f\u7528\u8986\u76d6\u5ea6\u4e0d\u5982 SHA-2 \u7cfb\u5217\u3002

        \u8868 6-2 \u00a0 \u5e38\u89c1\u7684\u54c8\u5e0c\u7b97\u6cd5

        MD5 SHA-1 SHA-2 SHA-3 \u63a8\u51fa\u65f6\u95f4 1992 1995 2002 2008 \u8f93\u51fa\u957f\u5ea6 128 bit 160 bit 256/512 bit 224/256/384/512 bit \u54c8\u5e0c\u51b2\u7a81 \u8f83\u591a \u8f83\u591a \u5f88\u5c11 \u5f88\u5c11 \u5b89\u5168\u7b49\u7ea7 \u4f4e\uff0c\u5df2\u88ab\u6210\u529f\u653b\u51fb \u4f4e\uff0c\u5df2\u88ab\u6210\u529f\u653b\u51fb \u9ad8 \u9ad8 \u5e94\u7528 \u5df2\u88ab\u5f03\u7528\uff0c\u4ecd\u7528\u4e8e\u6570\u636e\u5b8c\u6574\u6027\u68c0\u67e5 \u5df2\u88ab\u5f03\u7528 \u52a0\u5bc6\u8d27\u5e01\u4ea4\u6613\u9a8c\u8bc1\u3001\u6570\u5b57\u7b7e\u540d\u7b49 \u53ef\u7528\u4e8e\u66ff\u4ee3 SHA-2"},{"location":"chapter_hashing/hash_algorithm/#634","title":"6.3.4 \u00a0 \u6570\u636e\u7ed3\u6784\u7684\u54c8\u5e0c\u503c","text":"

        \u6211\u4eec\u77e5\u9053\uff0c\u54c8\u5e0c\u8868\u7684 key \u53ef\u4ee5\u662f\u6574\u6570\u3001\u5c0f\u6570\u6216\u5b57\u7b26\u4e32\u7b49\u6570\u636e\u7c7b\u578b\u3002\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u4f1a\u4e3a\u8fd9\u4e9b\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u5185\u7f6e\u7684\u54c8\u5e0c\u7b97\u6cd5\uff0c\u7528\u4e8e\u8ba1\u7b97\u54c8\u5e0c\u8868\u4e2d\u7684\u6876\u7d22\u5f15\u3002\u4ee5 Python \u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528 hash() \u51fd\u6570\u6765\u8ba1\u7b97\u5404\u79cd\u6570\u636e\u7c7b\u578b\u7684\u54c8\u5e0c\u503c\u3002

        • \u6574\u6570\u548c\u5e03\u5c14\u91cf\u7684\u54c8\u5e0c\u503c\u5c31\u662f\u5176\u672c\u8eab\u3002
        • \u6d6e\u70b9\u6570\u548c\u5b57\u7b26\u4e32\u7684\u54c8\u5e0c\u503c\u8ba1\u7b97\u8f83\u4e3a\u590d\u6742\uff0c\u6709\u5174\u8da3\u7684\u8bfb\u8005\u8bf7\u81ea\u884c\u5b66\u4e60\u3002
        • \u5143\u7ec4\u7684\u54c8\u5e0c\u503c\u662f\u5bf9\u5176\u4e2d\u6bcf\u4e00\u4e2a\u5143\u7d20\u8fdb\u884c\u54c8\u5e0c\uff0c\u7136\u540e\u5c06\u8fd9\u4e9b\u54c8\u5e0c\u503c\u7ec4\u5408\u8d77\u6765\uff0c\u5f97\u5230\u5355\u4e00\u7684\u54c8\u5e0c\u503c\u3002
        • \u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u57fa\u4e8e\u5176\u5185\u5b58\u5730\u5740\u751f\u6210\u3002\u901a\u8fc7\u91cd\u5199\u5bf9\u8c61\u7684\u54c8\u5e0c\u65b9\u6cd5\uff0c\u53ef\u5b9e\u73b0\u57fa\u4e8e\u5185\u5bb9\u751f\u6210\u54c8\u5e0c\u503c\u3002

        Tip

        \u8bf7\u6ce8\u610f\uff0c\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u7684\u5185\u7f6e\u54c8\u5e0c\u503c\u8ba1\u7b97\u51fd\u6570\u7684\u5b9a\u4e49\u548c\u65b9\u6cd5\u4e0d\u540c\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby built_in_hash.py
        num = 3\nhash_num = hash(num)\n# \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\n\nbol = True\nhash_bol = hash(bol)\n# \u5e03\u5c14\u91cf True \u7684\u54c8\u5e0c\u503c\u4e3a 1\n\ndec = 3.14159\nhash_dec = hash(dec)\n# \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 326484311674566659\n\nstr = \"Hello \u7b97\u6cd5\"\nhash_str = hash(str)\n# \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a 4617003410720528961\n\ntup = (12836, \"\u5c0f\u54c8\")\nhash_tup = hash(tup)\n# \u5143\u7ec4 (12836, '\u5c0f\u54c8') \u7684\u54c8\u5e0c\u503c\u4e3a 1029005403108185979\n\nobj = ListNode(0)\nhash_obj = hash(obj)\n# \u8282\u70b9\u5bf9\u8c61 <ListNode object at 0x1058fd810> \u7684\u54c8\u5e0c\u503c\u4e3a 274267521\n
        built_in_hash.cpp
        int num = 3;\nsize_t hashNum = hash<int>()(num);\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\n\nbool bol = true;\nsize_t hashBol = hash<bool>()(bol);\n// \u5e03\u5c14\u91cf 1 \u7684\u54c8\u5e0c\u503c\u4e3a 1\n\ndouble dec = 3.14159;\nsize_t hashDec = hash<double>()(dec);\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 4614256650576692846\n\nstring str = \"Hello \u7b97\u6cd5\";\nsize_t hashStr = hash<string>()(str);\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a 15466937326284535026\n\n// \u5728 C++ \u4e2d\uff0c\u5185\u7f6e std:hash() \u4ec5\u63d0\u4f9b\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u54c8\u5e0c\u503c\u8ba1\u7b97\n// \u6570\u7ec4\u3001\u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u8ba1\u7b97\u9700\u8981\u81ea\u884c\u5b9e\u73b0\n
        built_in_hash.java
        int num = 3;\nint hashNum = Integer.hashCode(num);\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\n\nboolean bol = true;\nint hashBol = Boolean.hashCode(bol);\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 1231\n\ndouble dec = 3.14159;\nint hashDec = Double.hashCode(dec);\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -1340954729\n\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode();\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a -727081396\n\nObject[] arr = { 12836, \"\u5c0f\u54c8\" };\nint hashTup = Arrays.hashCode(arr);\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 1151158\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode();\n// \u8282\u70b9\u5bf9\u8c61 utils.ListNode@7dc5e7b4 \u7684\u54c8\u5e0c\u503c\u4e3a 2110121908\n
        built_in_hash.cs
        int num = 3;\nint hashNum = num.GetHashCode();\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3;\n\nbool bol = true;\nint hashBol = bol.GetHashCode();\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 1;\n\ndouble dec = 3.14159;\nint hashDec = dec.GetHashCode();\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -1340954729;\n\nstring str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.GetHashCode();\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a -586107568;\n\nobject[] arr = [12836, \"\u5c0f\u54c8\"];\nint hashTup = arr.GetHashCode();\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 42931033;\n\nListNode obj = new(0);\nint hashObj = obj.GetHashCode();\n// \u8282\u70b9\u5bf9\u8c61 0 \u7684\u54c8\u5e0c\u503c\u4e3a 39053774;\n
        built_in_hash.go
        // Go \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n
        built_in_hash.swift
        let num = 3\nlet hashNum = num.hashValue\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 9047044699613009734\n\nlet bol = true\nlet hashBol = bol.hashValue\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a -4431640247352757451\n\nlet dec = 3.14159\nlet hashDec = dec.hashValue\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -2465384235396674631\n\nlet str = \"Hello \u7b97\u6cd5\"\nlet hashStr = str.hashValue\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a -7850626797806988787\n\nlet arr = [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")]\nlet hashTup = arr.hashValue\n// \u6570\u7ec4 [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")] \u7684\u54c8\u5e0c\u503c\u4e3a -2308633508154532996\n\nlet obj = ListNode(x: 0)\nlet hashObj = obj.hashValue\n// \u8282\u70b9\u5bf9\u8c61 utils.ListNode \u7684\u54c8\u5e0c\u503c\u4e3a -2434780518035996159\n
        built_in_hash.js
        // JavaScript \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n
        built_in_hash.ts
        // TypeScript \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n
        built_in_hash.dart
        int num = 3;\nint hashNum = num.hashCode;\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 34803\n\nbool bol = true;\nint hashBol = bol.hashCode;\n// \u5e03\u5c14\u503c true \u7684\u54c8\u5e0c\u503c\u4e3a 1231\n\ndouble dec = 3.14159;\nint hashDec = dec.hashCode;\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 2570631074981783\n\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode;\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a 468167534\n\nList arr = [12836, \"\u5c0f\u54c8\"];\nint hashArr = arr.hashCode;\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 976512528\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode;\n// \u8282\u70b9\u5bf9\u8c61 Instance of 'ListNode' \u7684\u54c8\u5e0c\u503c\u4e3a 1033450432\n
        built_in_hash.rs
        use std::collections::hash_map::DefaultHasher;\nuse std::hash::{Hash, Hasher};\n\nlet num = 3;\nlet mut num_hasher = DefaultHasher::new();\nnum.hash(&mut num_hasher);\nlet hash_num = num_hasher.finish();\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 568126464209439262\n\nlet bol = true;\nlet mut bol_hasher = DefaultHasher::new();\nbol.hash(&mut bol_hasher);\nlet hash_bol = bol_hasher.finish();\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 4952851536318644461\n\nlet dec: f32 = 3.14159;\nlet mut dec_hasher = DefaultHasher::new();\ndec.to_bits().hash(&mut dec_hasher);\nlet hash_dec = dec_hasher.finish();\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 2566941990314602357\n\nlet str = \"Hello \u7b97\u6cd5\";\nlet mut str_hasher = DefaultHasher::new();\nstr.hash(&mut str_hasher);\nlet hash_str = str_hasher.finish();\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a 16092673739211250988\n\nlet arr = (&12836, &\"\u5c0f\u54c8\");\nlet mut tup_hasher = DefaultHasher::new();\narr.hash(&mut tup_hasher);\nlet hash_tup = tup_hasher.finish();\n// \u5143\u7ec4 (12836, \"\u5c0f\u54c8\") \u7684\u54c8\u5e0c\u503c\u4e3a 1885128010422702749\n\nlet node = ListNode::new(42);\nlet mut hasher = DefaultHasher::new();\nnode.borrow().val.hash(&mut hasher);\nlet hash = hasher.finish();\n// \u8282\u70b9\u5bf9\u8c61 RefCell { value: ListNode { val: 42, next: None } } \u7684\u54c8\u5e0c\u503c\u4e3a15387811073369036852\n
        built_in_hash.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n
        built_in_hash.kt
        val num = 3\nval hashNum = num.hashCode()\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\n\nval bol = true\nval hashBol = bol.hashCode()\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 1231\n\nval dec = 3.14159\nval hashDec = dec.hashCode()\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -1340954729\n\nval str = \"Hello \u7b97\u6cd5\"\nval hashStr = str.hashCode()\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a -727081396\n\nval arr = arrayOf<Any>(12836, \"\u5c0f\u54c8\")\nval hashTup = arr.hashCode()\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 189568618\n\nval obj = ListNode(0)\nval hashObj = obj.hashCode()\n// \u8282\u70b9\u5bf9\u8c61 utils.ListNode@1d81eb93 \u7684\u54c8\u5e0c\u503c\u4e3a 495053715\n
        built_in_hash.rb
        num = 3\nhash_num = num.hash\n# \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a -4385856518450339636\n\nbol = true\nhash_bol = bol.hash\n# \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a -1617938112149317027\n\ndec = 3.14159\nhash_dec = dec.hash\n# \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -1479186995943067893\n\nstr = \"Hello \u7b97\u6cd5\"\nhash_str = str.hash\n# \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a -4075943250025831763\n\ntup = [12836, '\u5c0f\u54c8']\nhash_tup = tup.hash\n# \u5143\u7ec4 (12836, '\u5c0f\u54c8') \u7684\u54c8\u5e0c\u503c\u4e3a 1999544809202288822\n\nobj = ListNode.new(0)\nhash_obj = obj.hash\n# \u8282\u70b9\u5bf9\u8c61 #<ListNode:0x000078133140ab70> \u7684\u54c8\u5e0c\u503c\u4e3a 4302940560806366381\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5728\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u53ea\u6709\u4e0d\u53ef\u53d8\u5bf9\u8c61\u624d\u53ef\u4f5c\u4e3a\u54c8\u5e0c\u8868\u7684 key \u3002\u5047\u5982\u6211\u4eec\u5c06\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\u4f5c\u4e3a key \uff0c\u5f53\u5217\u8868\u7684\u5185\u5bb9\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u5b83\u7684\u54c8\u5e0c\u503c\u4e5f\u968f\u4e4b\u6539\u53d8\uff0c\u6211\u4eec\u5c31\u65e0\u6cd5\u5728\u54c8\u5e0c\u8868\u4e2d\u67e5\u8be2\u5230\u539f\u5148\u7684 value \u4e86\u3002

        \u867d\u7136\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff08\u6bd4\u5982\u94fe\u8868\u8282\u70b9\uff09\u7684\u6210\u5458\u53d8\u91cf\u662f\u53ef\u53d8\u7684\uff0c\u4f46\u5b83\u662f\u53ef\u54c8\u5e0c\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u901a\u5e38\u662f\u57fa\u4e8e\u5185\u5b58\u5730\u5740\u751f\u6210\u7684\uff0c\u5373\u4f7f\u5bf9\u8c61\u7684\u5185\u5bb9\u53d1\u751f\u4e86\u53d8\u5316\uff0c\u4f46\u5b83\u7684\u5185\u5b58\u5730\u5740\u4e0d\u53d8\uff0c\u54c8\u5e0c\u503c\u4ecd\u7136\u662f\u4e0d\u53d8\u7684\u3002

        \u7ec6\u5fc3\u7684\u4f60\u53ef\u80fd\u53d1\u73b0\u5728\u4e0d\u540c\u63a7\u5236\u53f0\u4e2d\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u8f93\u51fa\u7684\u54c8\u5e0c\u503c\u662f\u4e0d\u540c\u7684\u3002\u8fd9\u662f\u56e0\u4e3a Python \u89e3\u91ca\u5668\u5728\u6bcf\u6b21\u542f\u52a8\u65f6\uff0c\u90fd\u4f1a\u4e3a\u5b57\u7b26\u4e32\u54c8\u5e0c\u51fd\u6570\u52a0\u5165\u4e00\u4e2a\u968f\u673a\u7684\u76d0\uff08salt\uff09\u503c\u3002\u8fd9\u79cd\u505a\u6cd5\u53ef\u4ee5\u6709\u6548\u9632\u6b62 HashDoS \u653b\u51fb\uff0c\u63d0\u5347\u54c8\u5e0c\u7b97\u6cd5\u7684\u5b89\u5168\u6027\u3002

        "},{"location":"chapter_hashing/hash_collision/","title":"6.2 \u00a0 \u54c8\u5e0c\u51b2\u7a81","text":"

        \u4e0a\u4e00\u8282\u63d0\u5230\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u54c8\u5e0c\u51fd\u6570\u7684\u8f93\u5165\u7a7a\u95f4\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\uff0c\u56e0\u6b64\u7406\u8bba\u4e0a\u54c8\u5e0c\u51b2\u7a81\u662f\u4e0d\u53ef\u907f\u514d\u7684\u3002\u6bd4\u5982\uff0c\u8f93\u5165\u7a7a\u95f4\u4e3a\u5168\u4f53\u6574\u6570\uff0c\u8f93\u51fa\u7a7a\u95f4\u4e3a\u6570\u7ec4\u5bb9\u91cf\u5927\u5c0f\uff0c\u5219\u5fc5\u7136\u6709\u591a\u4e2a\u6574\u6570\u6620\u5c04\u81f3\u540c\u4e00\u6876\u7d22\u5f15\u3002

        \u54c8\u5e0c\u51b2\u7a81\u4f1a\u5bfc\u81f4\u67e5\u8be2\u7ed3\u679c\u9519\u8bef\uff0c\u4e25\u91cd\u5f71\u54cd\u54c8\u5e0c\u8868\u7684\u53ef\u7528\u6027\u3002\u4e3a\u4e86\u89e3\u51b3\u8be5\u95ee\u9898\uff0c\u6bcf\u5f53\u9047\u5230\u54c8\u5e0c\u51b2\u7a81\u65f6\uff0c\u6211\u4eec\u5c31\u8fdb\u884c\u54c8\u5e0c\u8868\u6269\u5bb9\uff0c\u76f4\u81f3\u51b2\u7a81\u6d88\u5931\u4e3a\u6b62\u3002\u6b64\u65b9\u6cd5\u7b80\u5355\u7c97\u66b4\u4e14\u6709\u6548\uff0c\u4f46\u6548\u7387\u592a\u4f4e\uff0c\u56e0\u4e3a\u54c8\u5e0c\u8868\u6269\u5bb9\u9700\u8981\u8fdb\u884c\u5927\u91cf\u7684\u6570\u636e\u642c\u8fd0\u4e0e\u54c8\u5e0c\u503c\u8ba1\u7b97\u3002\u4e3a\u4e86\u63d0\u5347\u6548\u7387\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u7b56\u7565\u3002

        1. \u6539\u826f\u54c8\u5e0c\u8868\u6570\u636e\u7ed3\u6784\uff0c\u4f7f\u5f97\u54c8\u5e0c\u8868\u53ef\u4ee5\u5728\u51fa\u73b0\u54c8\u5e0c\u51b2\u7a81\u65f6\u6b63\u5e38\u5de5\u4f5c\u3002
        2. \u4ec5\u5728\u5fc5\u8981\u65f6\uff0c\u5373\u5f53\u54c8\u5e0c\u51b2\u7a81\u6bd4\u8f83\u4e25\u91cd\u65f6\uff0c\u624d\u6267\u884c\u6269\u5bb9\u64cd\u4f5c\u3002

        \u54c8\u5e0c\u8868\u7684\u7ed3\u6784\u6539\u826f\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u201c\u94fe\u5f0f\u5730\u5740\u201d\u548c\u201c\u5f00\u653e\u5bfb\u5740\u201d\u3002

        "},{"location":"chapter_hashing/hash_collision/#621","title":"6.2.1 \u00a0 \u94fe\u5f0f\u5730\u5740","text":"

        \u5728\u539f\u59cb\u54c8\u5e0c\u8868\u4e2d\uff0c\u6bcf\u4e2a\u6876\u4ec5\u80fd\u5b58\u50a8\u4e00\u4e2a\u952e\u503c\u5bf9\u3002\u94fe\u5f0f\u5730\u5740\uff08separate chaining\uff09\u5c06\u5355\u4e2a\u5143\u7d20\u8f6c\u6362\u4e3a\u94fe\u8868\uff0c\u5c06\u952e\u503c\u5bf9\u4f5c\u4e3a\u94fe\u8868\u8282\u70b9\uff0c\u5c06\u6240\u6709\u53d1\u751f\u51b2\u7a81\u7684\u952e\u503c\u5bf9\u90fd\u5b58\u50a8\u5728\u540c\u4e00\u94fe\u8868\u4e2d\u3002\u56fe 6-5 \u5c55\u793a\u4e86\u4e00\u4e2a\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\u7684\u4f8b\u5b50\u3002

        \u56fe 6-5 \u00a0 \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868

        \u57fa\u4e8e\u94fe\u5f0f\u5730\u5740\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\u7684\u64cd\u4f5c\u65b9\u6cd5\u53d1\u751f\u4e86\u4ee5\u4e0b\u53d8\u5316\u3002

        • \u67e5\u8be2\u5143\u7d20\uff1a\u8f93\u5165 key \uff0c\u7ecf\u8fc7\u54c8\u5e0c\u51fd\u6570\u5f97\u5230\u6876\u7d22\u5f15\uff0c\u5373\u53ef\u8bbf\u95ee\u94fe\u8868\u5934\u8282\u70b9\uff0c\u7136\u540e\u904d\u5386\u94fe\u8868\u5e76\u5bf9\u6bd4 key \u4ee5\u67e5\u627e\u76ee\u6807\u952e\u503c\u5bf9\u3002
        • \u6dfb\u52a0\u5143\u7d20\uff1a\u9996\u5148\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u8bbf\u95ee\u94fe\u8868\u5934\u8282\u70b9\uff0c\u7136\u540e\u5c06\u8282\u70b9\uff08\u952e\u503c\u5bf9\uff09\u6dfb\u52a0\u5230\u94fe\u8868\u4e2d\u3002
        • \u5220\u9664\u5143\u7d20\uff1a\u6839\u636e\u54c8\u5e0c\u51fd\u6570\u7684\u7ed3\u679c\u8bbf\u95ee\u94fe\u8868\u5934\u90e8\uff0c\u63a5\u7740\u904d\u5386\u94fe\u8868\u4ee5\u67e5\u627e\u76ee\u6807\u8282\u70b9\u5e76\u5c06\u5176\u5220\u9664\u3002

        \u94fe\u5f0f\u5730\u5740\u5b58\u5728\u4ee5\u4e0b\u5c40\u9650\u6027\u3002

        • \u5360\u7528\u7a7a\u95f4\u589e\u5927\uff1a\u94fe\u8868\u5305\u542b\u8282\u70b9\u6307\u9488\uff0c\u5b83\u76f8\u6bd4\u6570\u7ec4\u66f4\u52a0\u8017\u8d39\u5185\u5b58\u7a7a\u95f4\u3002
        • \u67e5\u8be2\u6548\u7387\u964d\u4f4e\uff1a\u56e0\u4e3a\u9700\u8981\u7ebf\u6027\u904d\u5386\u94fe\u8868\u6765\u67e5\u627e\u5bf9\u5e94\u5143\u7d20\u3002

        \u4ee5\u4e0b\u4ee3\u7801\u7ed9\u51fa\u4e86\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\u7684\u7b80\u5355\u5b9e\u73b0\uff0c\u9700\u8981\u6ce8\u610f\u4e24\u70b9\u3002

        • \u4f7f\u7528\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\u4ee3\u66ff\u94fe\u8868\uff0c\u4ece\u800c\u7b80\u5316\u4ee3\u7801\u3002\u5728\u8fd9\u79cd\u8bbe\u5b9a\u4e0b\uff0c\u54c8\u5e0c\u8868\uff08\u6570\u7ec4\uff09\u5305\u542b\u591a\u4e2a\u6876\uff0c\u6bcf\u4e2a\u6876\u90fd\u662f\u4e00\u4e2a\u5217\u8868\u3002
        • \u4ee5\u4e0b\u5b9e\u73b0\u5305\u542b\u54c8\u5e0c\u8868\u6269\u5bb9\u65b9\u6cd5\u3002\u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7 \\(\\frac{2}{3}\\) \u65f6\uff0c\u6211\u4eec\u5c06\u54c8\u5e0c\u8868\u6269\u5bb9\u81f3\u539f\u5148\u7684 \\(2\\) \u500d\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map_chaining.py
        class HashMapChaining:\n    \"\"\"\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\"\"\"\n\n    def __init__(self):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        self.size = 0  # \u952e\u503c\u5bf9\u6570\u91cf\n        self.capacity = 4  # \u54c8\u5e0c\u8868\u5bb9\u91cf\n        self.load_thres = 2.0 / 3.0  # \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n        self.extend_ratio = 2  # \u6269\u5bb9\u500d\u6570\n        self.buckets = [[] for _ in range(self.capacity)]  # \u6876\u6570\u7ec4\n\n    def hash_func(self, key: int) -> int:\n        \"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\n        return key % self.capacity\n\n    def load_factor(self) -> float:\n        \"\"\"\u8d1f\u8f7d\u56e0\u5b50\"\"\"\n        return self.size / self.capacity\n\n    def get(self, key: int) -> str | None:\n        \"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\n        index = self.hash_func(key)\n        bucket = self.buckets[index]\n        # \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        for pair in bucket:\n            if pair.key == key:\n                return pair.val\n        # \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de None\n        return None\n\n    def put(self, key: int, val: str):\n        \"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"\n        # \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if self.load_factor() > self.load_thres:\n            self.extend()\n        index = self.hash_func(key)\n        bucket = self.buckets[index]\n        # \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n        for pair in bucket:\n            if pair.key == key:\n                pair.val = val\n                return\n        # \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n        pair = Pair(key, val)\n        bucket.append(pair)\n        self.size += 1\n\n    def remove(self, key: int):\n        \"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\n        index = self.hash_func(key)\n        bucket = self.buckets[index]\n        # \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n        for pair in bucket:\n            if pair.key == key:\n                bucket.remove(pair)\n                self.size -= 1\n                break\n\n    def extend(self):\n        \"\"\"\u6269\u5bb9\u54c8\u5e0c\u8868\"\"\"\n        # \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        buckets = self.buckets\n        # \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        self.capacity *= self.extend_ratio\n        self.buckets = [[] for _ in range(self.capacity)]\n        self.size = 0\n        # \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for bucket in buckets:\n            for pair in bucket:\n                self.put(pair.key, pair.val)\n\n    def print(self):\n        \"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\n        for bucket in self.buckets:\n            res = []\n            for pair in bucket:\n                res.append(str(pair.key) + \" -> \" + pair.val)\n            print(res)\n
        hash_map_chaining.cpp
        /* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n  private:\n    int size;                       // \u952e\u503c\u5bf9\u6570\u91cf\n    int capacity;                   // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    double loadThres;               // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    int extendRatio;                // \u6269\u5bb9\u500d\u6570\n    vector<vector<Pair *>> buckets; // \u6876\u6570\u7ec4\n\n  public:\n    /* \u6784\u9020\u65b9\u6cd5 */\n    HashMapChaining() : size(0), capacity(4), loadThres(2.0 / 3.0), extendRatio(2) {\n        buckets.resize(capacity);\n    }\n\n    /* \u6790\u6784\u65b9\u6cd5 */\n    ~HashMapChaining() {\n        for (auto &bucket : buckets) {\n            for (Pair *pair : bucket) {\n                // \u91ca\u653e\u5185\u5b58\n                delete pair;\n            }\n        }\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    double loadFactor() {\n        return (double)size / (double)capacity;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    string get(int key) {\n        int index = hashFunc(key);\n        // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        for (Pair *pair : buckets[index]) {\n            if (pair->key == key) {\n                return pair->val;\n            }\n        }\n        // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n        return \"\";\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    void put(int key, string val) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        int index = hashFunc(key);\n        // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n        for (Pair *pair : buckets[index]) {\n            if (pair->key == key) {\n                pair->val = val;\n                return;\n            }\n        }\n        // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n        buckets[index].push_back(new Pair(key, val));\n        size++;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    void remove(int key) {\n        int index = hashFunc(key);\n        auto &bucket = buckets[index];\n        // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n        for (int i = 0; i < bucket.size(); i++) {\n            if (bucket[i]->key == key) {\n                Pair *tmp = bucket[i];\n                bucket.erase(bucket.begin() + i); // \u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n                delete tmp;                       // \u91ca\u653e\u5185\u5b58\n                size--;\n                return;\n            }\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    void extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        vector<vector<Pair *>> bucketsTmp = buckets;\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        capacity *= extendRatio;\n        buckets.clear();\n        buckets.resize(capacity);\n        size = 0;\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for (auto &bucket : bucketsTmp) {\n            for (Pair *pair : bucket) {\n                put(pair->key, pair->val);\n                // \u91ca\u653e\u5185\u5b58\n                delete pair;\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    void print() {\n        for (auto &bucket : buckets) {\n            cout << \"[\";\n            for (Pair *pair : bucket) {\n                cout << pair->key << \" -> \" << pair->val << \", \";\n            }\n            cout << \"]\\n\";\n        }\n    }\n};\n
        hash_map_chaining.java
        /* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n    int size; // \u952e\u503c\u5bf9\u6570\u91cf\n    int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    int extendRatio; // \u6269\u5bb9\u500d\u6570\n    List<List<Pair>> buckets; // \u6876\u6570\u7ec4\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public HashMapChaining() {\n        size = 0;\n        capacity = 4;\n        loadThres = 2.0 / 3.0;\n        extendRatio = 2;\n        buckets = new ArrayList<>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.add(new ArrayList<>());\n        }\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    double loadFactor() {\n        return (double) size / capacity;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    String get(int key) {\n        int index = hashFunc(key);\n        List<Pair> bucket = buckets.get(index);\n        // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        for (Pair pair : bucket) {\n            if (pair.key == key) {\n                return pair.val;\n            }\n        }\n        // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n        return null;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    void put(int key, String val) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        int index = hashFunc(key);\n        List<Pair> bucket = buckets.get(index);\n        // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n        for (Pair pair : bucket) {\n            if (pair.key == key) {\n                pair.val = val;\n                return;\n            }\n        }\n        // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n        Pair pair = new Pair(key, val);\n        bucket.add(pair);\n        size++;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    void remove(int key) {\n        int index = hashFunc(key);\n        List<Pair> bucket = buckets.get(index);\n        // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n        for (Pair pair : bucket) {\n            if (pair.key == key) {\n                bucket.remove(pair);\n                size--;\n                break;\n            }\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    void extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        List<List<Pair>> bucketsTmp = buckets;\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        capacity *= extendRatio;\n        buckets = new ArrayList<>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.add(new ArrayList<>());\n        }\n        size = 0;\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for (List<Pair> bucket : bucketsTmp) {\n            for (Pair pair : bucket) {\n                put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    void print() {\n        for (List<Pair> bucket : buckets) {\n            List<String> res = new ArrayList<>();\n            for (Pair pair : bucket) {\n                res.add(pair.key + \" -> \" + pair.val);\n            }\n            System.out.println(res);\n        }\n    }\n}\n
        hash_map_chaining.cs
        /* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n    int size; // \u952e\u503c\u5bf9\u6570\u91cf\n    int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    int extendRatio; // \u6269\u5bb9\u500d\u6570\n    List<List<Pair>> buckets; // \u6876\u6570\u7ec4\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public HashMapChaining() {\n        size = 0;\n        capacity = 4;\n        loadThres = 2.0 / 3.0;\n        extendRatio = 2;\n        buckets = new List<List<Pair>>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.Add([]);\n        }\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    int HashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    double LoadFactor() {\n        return (double)size / capacity;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    public string? Get(int key) {\n        int index = HashFunc(key);\n        // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        foreach (Pair pair in buckets[index]) {\n            if (pair.key == key) {\n                return pair.val;\n            }\n        }\n        // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n        return null;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    public void Put(int key, string val) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (LoadFactor() > loadThres) {\n            Extend();\n        }\n        int index = HashFunc(key);\n        // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n        foreach (Pair pair in buckets[index]) {\n            if (pair.key == key) {\n                pair.val = val;\n                return;\n            }\n        }\n        // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n        buckets[index].Add(new Pair(key, val));\n        size++;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    public void Remove(int key) {\n        int index = HashFunc(key);\n        // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n        foreach (Pair pair in buckets[index].ToList()) {\n            if (pair.key == key) {\n                buckets[index].Remove(pair);\n                size--;\n                break;\n            }\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    void Extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        List<List<Pair>> bucketsTmp = buckets;\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        capacity *= extendRatio;\n        buckets = new List<List<Pair>>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.Add([]);\n        }\n        size = 0;\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        foreach (List<Pair> bucket in bucketsTmp) {\n            foreach (Pair pair in bucket) {\n                Put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    public void Print() {\n        foreach (List<Pair> bucket in buckets) {\n            List<string> res = [];\n            foreach (Pair pair in bucket) {\n                res.Add(pair.key + \" -> \" + pair.val);\n            }\n            foreach (string kv in res) {\n                Console.WriteLine(kv);\n            }\n        }\n    }\n}\n
        hash_map_chaining.go
        /* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\ntype hashMapChaining struct {\n    size        int      // \u952e\u503c\u5bf9\u6570\u91cf\n    capacity    int      // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    loadThres   float64  // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    extendRatio int      // \u6269\u5bb9\u500d\u6570\n    buckets     [][]pair // \u6876\u6570\u7ec4\n}\n\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newHashMapChaining() *hashMapChaining {\n    buckets := make([][]pair, 4)\n    for i := 0; i < 4; i++ {\n        buckets[i] = make([]pair, 0)\n    }\n    return &hashMapChaining{\n        size:        0,\n        capacity:    4,\n        loadThres:   2.0 / 3.0,\n        extendRatio: 2,\n        buckets:     buckets,\n    }\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (m *hashMapChaining) hashFunc(key int) int {\n    return key % m.capacity\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc (m *hashMapChaining) loadFactor() float64 {\n    return float64(m.size) / float64(m.capacity)\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (m *hashMapChaining) get(key int) string {\n    idx := m.hashFunc(key)\n    bucket := m.buckets[idx]\n    // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n    for _, p := range bucket {\n        if p.key == key {\n            return p.val\n        }\n    }\n    // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n    return \"\"\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (m *hashMapChaining) put(key int, val string) {\n    // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n    if m.loadFactor() > m.loadThres {\n        m.extend()\n    }\n    idx := m.hashFunc(key)\n    // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n    for i := range m.buckets[idx] {\n        if m.buckets[idx][i].key == key {\n            m.buckets[idx][i].val = val\n            return\n        }\n    }\n    // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n    p := pair{\n        key: key,\n        val: val,\n    }\n    m.buckets[idx] = append(m.buckets[idx], p)\n    m.size += 1\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (m *hashMapChaining) remove(key int) {\n    idx := m.hashFunc(key)\n    // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n    for i, p := range m.buckets[idx] {\n        if p.key == key {\n            // \u5207\u7247\u5220\u9664\n            m.buckets[idx] = append(m.buckets[idx][:i], m.buckets[idx][i+1:]...)\n            m.size -= 1\n            break\n        }\n    }\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc (m *hashMapChaining) extend() {\n    // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n    tmpBuckets := make([][]pair, len(m.buckets))\n    for i := 0; i < len(m.buckets); i++ {\n        tmpBuckets[i] = make([]pair, len(m.buckets[i]))\n        copy(tmpBuckets[i], m.buckets[i])\n    }\n    // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n    m.capacity *= m.extendRatio\n    m.buckets = make([][]pair, m.capacity)\n    for i := 0; i < m.capacity; i++ {\n        m.buckets[i] = make([]pair, 0)\n    }\n    m.size = 0\n    // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n    for _, bucket := range tmpBuckets {\n        for _, p := range bucket {\n            m.put(p.key, p.val)\n        }\n    }\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (m *hashMapChaining) print() {\n    var builder strings.Builder\n\n    for _, bucket := range m.buckets {\n        builder.WriteString(\"[\")\n        for _, p := range bucket {\n            builder.WriteString(strconv.Itoa(p.key) + \" -> \" + p.val + \" \")\n        }\n        builder.WriteString(\"]\")\n        fmt.Println(builder.String())\n        builder.Reset()\n    }\n}\n
        hash_map_chaining.swift
        /* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n    var size: Int // \u952e\u503c\u5bf9\u6570\u91cf\n    var capacity: Int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    var loadThres: Double // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    var extendRatio: Int // \u6269\u5bb9\u500d\u6570\n    var buckets: [[Pair]] // \u6876\u6570\u7ec4\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    init() {\n        size = 0\n        capacity = 4\n        loadThres = 2.0 / 3.0\n        extendRatio = 2\n        buckets = Array(repeating: [], count: capacity)\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    func hashFunc(key: Int) -> Int {\n        key % capacity\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    func loadFactor() -> Double {\n        Double(size) / Double(capacity)\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    func get(key: Int) -> String? {\n        let index = hashFunc(key: key)\n        let bucket = buckets[index]\n        // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        for pair in bucket {\n            if pair.key == key {\n                return pair.val\n            }\n        }\n        // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de nil\n        return nil\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    func put(key: Int, val: String) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if loadFactor() > loadThres {\n            extend()\n        }\n        let index = hashFunc(key: key)\n        let bucket = buckets[index]\n        // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n        for pair in bucket {\n            if pair.key == key {\n                pair.val = val\n                return\n            }\n        }\n        // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n        let pair = Pair(key: key, val: val)\n        buckets[index].append(pair)\n        size += 1\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    func remove(key: Int) {\n        let index = hashFunc(key: key)\n        let bucket = buckets[index]\n        // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n        for (pairIndex, pair) in bucket.enumerated() {\n            if pair.key == key {\n                buckets[index].remove(at: pairIndex)\n                size -= 1\n                break\n            }\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    func extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        let bucketsTmp = buckets\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        capacity *= extendRatio\n        buckets = Array(repeating: [], count: capacity)\n        size = 0\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for bucket in bucketsTmp {\n            for pair in bucket {\n                put(key: pair.key, val: pair.val)\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    func print() {\n        for bucket in buckets {\n            let res = bucket.map { \"\\($0.key) -> \\($0.val)\" }\n            Swift.print(res)\n        }\n    }\n}\n
        hash_map_chaining.js
        /* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n    #size; // \u952e\u503c\u5bf9\u6570\u91cf\n    #capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    #loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    #extendRatio; // \u6269\u5bb9\u500d\u6570\n    #buckets; // \u6876\u6570\u7ec4\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor() {\n        this.#size = 0;\n        this.#capacity = 4;\n        this.#loadThres = 2.0 / 3.0;\n        this.#extendRatio = 2;\n        this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    #hashFunc(key) {\n        return key % this.#capacity;\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    #loadFactor() {\n        return this.#size / this.#capacity;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    get(key) {\n        const index = this.#hashFunc(key);\n        const bucket = this.#buckets[index];\n        // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        for (const pair of bucket) {\n            if (pair.key === key) {\n                return pair.val;\n            }\n        }\n        // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n        return null;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    put(key, val) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (this.#loadFactor() > this.#loadThres) {\n            this.#extend();\n        }\n        const index = this.#hashFunc(key);\n        const bucket = this.#buckets[index];\n        // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n        for (const pair of bucket) {\n            if (pair.key === key) {\n                pair.val = val;\n                return;\n            }\n        }\n        // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n        const pair = new Pair(key, val);\n        bucket.push(pair);\n        this.#size++;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    remove(key) {\n        const index = this.#hashFunc(key);\n        let bucket = this.#buckets[index];\n        // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n        for (let i = 0; i < bucket.length; i++) {\n            if (bucket[i].key === key) {\n                bucket.splice(i, 1);\n                this.#size--;\n                break;\n            }\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    #extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        const bucketsTmp = this.#buckets;\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        this.#capacity *= this.#extendRatio;\n        this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n        this.#size = 0;\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for (const bucket of bucketsTmp) {\n            for (const pair of bucket) {\n                this.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    print() {\n        for (const bucket of this.#buckets) {\n            let res = [];\n            for (const pair of bucket) {\n                res.push(pair.key + ' -> ' + pair.val);\n            }\n            console.log(res);\n        }\n    }\n}\n
        hash_map_chaining.ts
        /* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n    #size: number; // \u952e\u503c\u5bf9\u6570\u91cf\n    #capacity: number; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    #loadThres: number; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    #extendRatio: number; // \u6269\u5bb9\u500d\u6570\n    #buckets: Pair[][]; // \u6876\u6570\u7ec4\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor() {\n        this.#size = 0;\n        this.#capacity = 4;\n        this.#loadThres = 2.0 / 3.0;\n        this.#extendRatio = 2;\n        this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    #hashFunc(key: number): number {\n        return key % this.#capacity;\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    #loadFactor(): number {\n        return this.#size / this.#capacity;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    get(key: number): string | null {\n        const index = this.#hashFunc(key);\n        const bucket = this.#buckets[index];\n        // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        for (const pair of bucket) {\n            if (pair.key === key) {\n                return pair.val;\n            }\n        }\n        // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n        return null;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    put(key: number, val: string): void {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (this.#loadFactor() > this.#loadThres) {\n            this.#extend();\n        }\n        const index = this.#hashFunc(key);\n        const bucket = this.#buckets[index];\n        // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n        for (const pair of bucket) {\n            if (pair.key === key) {\n                pair.val = val;\n                return;\n            }\n        }\n        // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n        const pair = new Pair(key, val);\n        bucket.push(pair);\n        this.#size++;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    remove(key: number): void {\n        const index = this.#hashFunc(key);\n        let bucket = this.#buckets[index];\n        // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n        for (let i = 0; i < bucket.length; i++) {\n            if (bucket[i].key === key) {\n                bucket.splice(i, 1);\n                this.#size--;\n                break;\n            }\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    #extend(): void {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        const bucketsTmp = this.#buckets;\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        this.#capacity *= this.#extendRatio;\n        this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n        this.#size = 0;\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for (const bucket of bucketsTmp) {\n            for (const pair of bucket) {\n                this.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    print(): void {\n        for (const bucket of this.#buckets) {\n            let res = [];\n            for (const pair of bucket) {\n                res.push(pair.key + ' -> ' + pair.val);\n            }\n            console.log(res);\n        }\n    }\n}\n
        hash_map_chaining.dart
        /* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n  late int size; // \u952e\u503c\u5bf9\u6570\u91cf\n  late int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n  late double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n  late int extendRatio; // \u6269\u5bb9\u500d\u6570\n  late List<List<Pair>> buckets; // \u6876\u6570\u7ec4\n\n  /* \u6784\u9020\u65b9\u6cd5 */\n  HashMapChaining() {\n    size = 0;\n    capacity = 4;\n    loadThres = 2.0 / 3.0;\n    extendRatio = 2;\n    buckets = List.generate(capacity, (_) => []);\n  }\n\n  /* \u54c8\u5e0c\u51fd\u6570 */\n  int hashFunc(int key) {\n    return key % capacity;\n  }\n\n  /* \u8d1f\u8f7d\u56e0\u5b50 */\n  double loadFactor() {\n    return size / capacity;\n  }\n\n  /* \u67e5\u8be2\u64cd\u4f5c */\n  String? get(int key) {\n    int index = hashFunc(key);\n    List<Pair> bucket = buckets[index];\n    // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n    for (Pair pair in bucket) {\n      if (pair.key == key) {\n        return pair.val;\n      }\n    }\n    // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n    return null;\n  }\n\n  /* \u6dfb\u52a0\u64cd\u4f5c */\n  void put(int key, String val) {\n    // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n    if (loadFactor() > loadThres) {\n      extend();\n    }\n    int index = hashFunc(key);\n    List<Pair> bucket = buckets[index];\n    // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n    for (Pair pair in bucket) {\n      if (pair.key == key) {\n        pair.val = val;\n        return;\n      }\n    }\n    // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n    Pair pair = Pair(key, val);\n    bucket.add(pair);\n    size++;\n  }\n\n  /* \u5220\u9664\u64cd\u4f5c */\n  void remove(int key) {\n    int index = hashFunc(key);\n    List<Pair> bucket = buckets[index];\n    // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n    for (Pair pair in bucket) {\n      if (pair.key == key) {\n        bucket.remove(pair);\n        size--;\n        break;\n      }\n    }\n  }\n\n  /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n  void extend() {\n    // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n    List<List<Pair>> bucketsTmp = buckets;\n    // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n    capacity *= extendRatio;\n    buckets = List.generate(capacity, (_) => []);\n    size = 0;\n    // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n    for (List<Pair> bucket in bucketsTmp) {\n      for (Pair pair in bucket) {\n        put(pair.key, pair.val);\n      }\n    }\n  }\n\n  /* \u6253\u5370\u54c8\u5e0c\u8868 */\n  void printHashMap() {\n    for (List<Pair> bucket in buckets) {\n      List<String> res = [];\n      for (Pair pair in bucket) {\n        res.add(\"${pair.key} -> ${pair.val}\");\n      }\n      print(res);\n    }\n  }\n}\n
        hash_map_chaining.rs
        /* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nstruct HashMapChaining {\n    size: usize,\n    capacity: usize,\n    load_thres: f32,\n    extend_ratio: usize,\n    buckets: Vec<Vec<Pair>>,\n}\n\nimpl HashMapChaining {\n    /* \u6784\u9020\u65b9\u6cd5 */\n    fn new() -> Self {\n        Self {\n            size: 0,\n            capacity: 4,\n            load_thres: 2.0 / 3.0,\n            extend_ratio: 2,\n            buckets: vec![vec![]; 4],\n        }\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    fn hash_func(&self, key: i32) -> usize {\n        key as usize % self.capacity\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    fn load_factor(&self) -> f32 {\n        self.size as f32 / self.capacity as f32\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    fn remove(&mut self, key: i32) -> Option<String> {\n        let index = self.hash_func(key);\n\n        // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n        for (i, p) in self.buckets[index].iter_mut().enumerate() {\n            if p.key == key {\n                let pair = self.buckets[index].remove(i);\n                self.size -= 1;\n                return Some(pair.val);\n            }\n        }\n\n        // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de None\n        None\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    fn extend(&mut self) {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        let buckets_tmp = std::mem::take(&mut self.buckets);\n\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        self.capacity *= self.extend_ratio;\n        self.buckets = vec![Vec::new(); self.capacity as usize];\n        self.size = 0;\n\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for bucket in buckets_tmp {\n            for pair in bucket {\n                self.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    fn print(&self) {\n        for bucket in &self.buckets {\n            let mut res = Vec::new();\n            for pair in bucket {\n                res.push(format!(\"{} -> {}\", pair.key, pair.val));\n            }\n            println!(\"{:?}\", res);\n        }\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    fn put(&mut self, key: i32, val: String) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if self.load_factor() > self.load_thres {\n            self.extend();\n        }\n\n        let index = self.hash_func(key);\n\n        // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n        for pair in self.buckets[index].iter_mut() {\n            if pair.key == key {\n                pair.val = val;\n                return;\n            }\n        }\n\n        // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n        let pair = Pair { key, val };\n        self.buckets[index].push(pair);\n        self.size += 1;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    fn get(&self, key: i32) -> Option<&str> {\n        let index = self.hash_func(key);\n\n        // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        for pair in self.buckets[index].iter() {\n            if pair.key == key {\n                return Some(&pair.val);\n            }\n        }\n\n        // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de None\n        None\n    }\n}\n
        hash_map_chaining.c
        /* \u94fe\u8868\u8282\u70b9 */\ntypedef struct Node {\n    Pair *pair;\n    struct Node *next;\n} Node;\n\n/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\ntypedef struct {\n    int size;         // \u952e\u503c\u5bf9\u6570\u91cf\n    int capacity;     // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    int extendRatio;  // \u6269\u5bb9\u500d\u6570\n    Node **buckets;   // \u6876\u6570\u7ec4\n} HashMapChaining;\n\n/* \u6784\u9020\u51fd\u6570 */\nHashMapChaining *newHashMapChaining() {\n    HashMapChaining *hashMap = (HashMapChaining *)malloc(sizeof(HashMapChaining));\n    hashMap->size = 0;\n    hashMap->capacity = 4;\n    hashMap->loadThres = 2.0 / 3.0;\n    hashMap->extendRatio = 2;\n    hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));\n    for (int i = 0; i < hashMap->capacity; i++) {\n        hashMap->buckets[i] = NULL;\n    }\n    return hashMap;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delHashMapChaining(HashMapChaining *hashMap) {\n    for (int i = 0; i < hashMap->capacity; i++) {\n        Node *cur = hashMap->buckets[i];\n        while (cur) {\n            Node *tmp = cur;\n            cur = cur->next;\n            free(tmp->pair);\n            free(tmp);\n        }\n    }\n    free(hashMap->buckets);\n    free(hashMap);\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(HashMapChaining *hashMap, int key) {\n    return key % hashMap->capacity;\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor(HashMapChaining *hashMap) {\n    return (double)hashMap->size / (double)hashMap->capacity;\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nchar *get(HashMapChaining *hashMap, int key) {\n    int index = hashFunc(hashMap, key);\n    // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n    Node *cur = hashMap->buckets[index];\n    while (cur) {\n        if (cur->pair->key == key) {\n            return cur->pair->val;\n        }\n        cur = cur->next;\n    }\n    return \"\"; // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(HashMapChaining *hashMap, int key, const char *val) {\n    // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n    if (loadFactor(hashMap) > hashMap->loadThres) {\n        extend(hashMap);\n    }\n    int index = hashFunc(hashMap, key);\n    // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n    Node *cur = hashMap->buckets[index];\n    while (cur) {\n        if (cur->pair->key == key) {\n            strcpy(cur->pair->val, val); // \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n            return;\n        }\n        cur = cur->next;\n    }\n    // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n    Pair *newPair = (Pair *)malloc(sizeof(Pair));\n    newPair->key = key;\n    strcpy(newPair->val, val);\n    Node *newNode = (Node *)malloc(sizeof(Node));\n    newNode->pair = newPair;\n    newNode->next = hashMap->buckets[index];\n    hashMap->buckets[index] = newNode;\n    hashMap->size++;\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend(HashMapChaining *hashMap) {\n    // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n    int oldCapacity = hashMap->capacity;\n    Node **oldBuckets = hashMap->buckets;\n    // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n    hashMap->capacity *= hashMap->extendRatio;\n    hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));\n    for (int i = 0; i < hashMap->capacity; i++) {\n        hashMap->buckets[i] = NULL;\n    }\n    hashMap->size = 0;\n    // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n    for (int i = 0; i < oldCapacity; i++) {\n        Node *cur = oldBuckets[i];\n        while (cur) {\n            put(hashMap, cur->pair->key, cur->pair->val);\n            Node *temp = cur;\n            cur = cur->next;\n            // \u91ca\u653e\u5185\u5b58\n            free(temp->pair);\n            free(temp);\n        }\n    }\n\n    free(oldBuckets);\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeItem(HashMapChaining *hashMap, int key) {\n    int index = hashFunc(hashMap, key);\n    Node *cur = hashMap->buckets[index];\n    Node *pre = NULL;\n    while (cur) {\n        if (cur->pair->key == key) {\n            // \u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n            if (pre) {\n                pre->next = cur->next;\n            } else {\n                hashMap->buckets[index] = cur->next;\n            }\n            // \u91ca\u653e\u5185\u5b58\n            free(cur->pair);\n            free(cur);\n            hashMap->size--;\n            return;\n        }\n        pre = cur;\n        cur = cur->next;\n    }\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(HashMapChaining *hashMap) {\n    for (int i = 0; i < hashMap->capacity; i++) {\n        Node *cur = hashMap->buckets[i];\n        printf(\"[\");\n        while (cur) {\n            printf(\"%d -> %s, \", cur->pair->key, cur->pair->val);\n            cur = cur->next;\n        }\n        printf(\"]\\n\");\n    }\n}\n
        hash_map_chaining.kt
        /* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n    var size: Int // \u952e\u503c\u5bf9\u6570\u91cf\n    var capacity: Int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    val loadThres: Double // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    val extendRatio: Int // \u6269\u5bb9\u500d\u6570\n    var buckets: MutableList<MutableList<Pair>> // \u6876\u6570\u7ec4\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    init {\n        size = 0\n        capacity = 4\n        loadThres = 2.0 / 3.0\n        extendRatio = 2\n        buckets = mutableListOf()\n        for (i in 0..<capacity) {\n            buckets.add(mutableListOf())\n        }\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    fun hashFunc(key: Int): Int {\n        return key % capacity\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    fun loadFactor(): Double {\n        return (size / capacity).toDouble()\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    fun get(key: Int): String? {\n        val index = hashFunc(key)\n        val bucket = buckets[index]\n        // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        for (pair in bucket) {\n            if (pair.key == key) return pair._val\n        }\n        // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n        return null\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    fun put(key: Int, _val: String) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (loadFactor() > loadThres) {\n            extend()\n        }\n        val index = hashFunc(key)\n        val bucket = buckets[index]\n        // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n        for (pair in bucket) {\n            if (pair.key == key) {\n                pair._val = _val\n                return\n            }\n        }\n        // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n        val pair = Pair(key, _val)\n        bucket.add(pair)\n        size++\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    fun remove(key: Int) {\n        val index = hashFunc(key)\n        val bucket = buckets[index]\n        // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n        for (pair in bucket) {\n            if (pair.key == key) {\n                bucket.remove(pair)\n                size--\n                break\n            }\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    fun extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        val bucketsTmp = buckets\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        capacity *= extendRatio\n        // mutablelist \u65e0\u56fa\u5b9a\u5927\u5c0f\n        buckets = mutableListOf()\n        for (i in 0..<capacity) {\n            buckets.add(mutableListOf())\n        }\n        size = 0\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for (bucket in bucketsTmp) {\n            for (pair in bucket) {\n                put(pair.key, pair._val)\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    fun print() {\n        for (bucket in buckets) {\n            val res = mutableListOf<String>()\n            for (pair in bucket) {\n                val k = pair.key\n                val v = pair._val\n                res.add(\"$k -> $v\")\n            }\n            println(res)\n        }\n    }\n}\n
        hash_map_chaining.rb
        ### \u952e\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 ###\nclass HashMapChaining\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize\n    @size = 0 # \u952e\u503c\u5bf9\u6570\u91cf\n    @capacity = 4 # \u54c8\u5e0c\u8868\u5bb9\u91cf\n    @load_thres = 2.0 / 3.0 # \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    @extend_ratio = 2 # \u6269\u5bb9\u500d\u6570\n    @buckets = Array.new(@capacity) { [] } # \u6876\u6570\u7ec4\n  end\n\n  ### \u54c8\u5e0c\u51fd\u6570 ###\n  def hash_func(key)\n    key % @capacity\n  end\n\n  ### \u8d1f\u8f7d\u56e0\u5b50 ###\n  def load_factor\n    @size / @capacity\n  end\n\n  ### \u67e5\u8be2\u64cd\u4f5c ###\n  def get(key)\n    index = hash_func(key)\n    bucket = @buckets[index]\n    # \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n    for pair in bucket\n      return pair.val if pair.key == key\n    end\n    # \u82e5\u672a\u627e\u5230 key , \u5219\u8fd4\u56de nil\n    nil\n  end\n\n  ### \u6dfb\u52a0\u64cd\u4f5c ###\n  def put(key, val)\n    # \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n    extend if load_factor > @load_thres\n    index = hash_func(key)\n    bucket = @buckets[index]\n    # \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n    for pair in bucket\n      if pair.key == key\n        pair.val = val\n        return\n      end\n    end\n    # \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n    pair = Pair.new(key, val)\n    bucket << pair\n    @size += 1\n  end\n\n  ### \u5220\u9664\u64cd\u4f5c ###\n  def remove(key)\n    index = hash_func(key)\n    bucket = @buckets[index]\n    # \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n    for pair in bucket\n      if pair.key == key\n        bucket.delete(pair)\n        @size -= 1\n        break\n      end\n    end\n  end\n\n  ### \u6269\u5bb9\u54c8\u5e0c\u8868 ###\n  def extend\n    # \u66ab\u5b58\u539f\u54c8\u5e0c\u8868\n    buckets = @buckets\n    # \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n    @capacity *= @extend_ratio\n    @buckets = Array.new(@capacity) { [] }\n    @size = 0\n    # \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n    for bucket in buckets\n      for pair in bucket\n        put(pair.key, pair.val)\n      end\n    end\n  end\n\n  ### \u6253\u5370\u54c8\u5e0c\u8868 ###\n  def print\n    for bucket in @buckets\n      res = []\n      for pair in bucket\n        res << \"#{pair.key} -> #{pair.val}\"\n      end\n      pp res\n    end\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u94fe\u8868\u5f88\u957f\u65f6\uff0c\u67e5\u8be2\u6548\u7387 \\(O(n)\\) \u5f88\u5dee\u3002\u6b64\u65f6\u53ef\u4ee5\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u201cAVL \u6811\u201d\u6216\u201c\u7ea2\u9ed1\u6811\u201d\uff0c\u4ece\u800c\u5c06\u67e5\u8be2\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(\\log n)\\) \u3002

        "},{"location":"chapter_hashing/hash_collision/#622","title":"6.2.2 \u00a0 \u5f00\u653e\u5bfb\u5740","text":"

        \u5f00\u653e\u5bfb\u5740\uff08open addressing\uff09\u4e0d\u5f15\u5165\u989d\u5916\u7684\u6570\u636e\u7ed3\u6784\uff0c\u800c\u662f\u901a\u8fc7\u201c\u591a\u6b21\u63a2\u6d4b\u201d\u6765\u5904\u7406\u54c8\u5e0c\u51b2\u7a81\uff0c\u63a2\u6d4b\u65b9\u5f0f\u4e3b\u8981\u5305\u62ec\u7ebf\u6027\u63a2\u6d4b\u3001\u5e73\u65b9\u63a2\u6d4b\u548c\u591a\u6b21\u54c8\u5e0c\u7b49\u3002

        \u4e0b\u9762\u4ee5\u7ebf\u6027\u63a2\u6d4b\u4e3a\u4f8b\uff0c\u4ecb\u7ecd\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\u7684\u5de5\u4f5c\u673a\u5236\u3002

        "},{"location":"chapter_hashing/hash_collision/#1","title":"1. \u00a0 \u7ebf\u6027\u63a2\u6d4b","text":"

        \u7ebf\u6027\u63a2\u6d4b\u91c7\u7528\u56fa\u5b9a\u6b65\u957f\u7684\u7ebf\u6027\u641c\u7d22\u6765\u8fdb\u884c\u63a2\u6d4b\uff0c\u5176\u64cd\u4f5c\u65b9\u6cd5\u4e0e\u666e\u901a\u54c8\u5e0c\u8868\u6709\u6240\u4e0d\u540c\u3002

        • \u63d2\u5165\u5143\u7d20\uff1a\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u82e5\u53d1\u73b0\u6876\u5185\u5df2\u6709\u5143\u7d20\uff0c\u5219\u4ece\u51b2\u7a81\u4f4d\u7f6e\u5411\u540e\u7ebf\u6027\u904d\u5386\uff08\u6b65\u957f\u901a\u5e38\u4e3a \\(1\\) \uff09\uff0c\u76f4\u81f3\u627e\u5230\u7a7a\u6876\uff0c\u5c06\u5143\u7d20\u63d2\u5165\u5176\u4e2d\u3002
        • \u67e5\u627e\u5143\u7d20\uff1a\u82e5\u53d1\u73b0\u54c8\u5e0c\u51b2\u7a81\uff0c\u5219\u4f7f\u7528\u76f8\u540c\u6b65\u957f\u5411\u540e\u8fdb\u884c\u7ebf\u6027\u904d\u5386\uff0c\u76f4\u5230\u627e\u5230\u5bf9\u5e94\u5143\u7d20\uff0c\u8fd4\u56de value \u5373\u53ef\uff1b\u5982\u679c\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u76ee\u6807\u5143\u7d20\u4e0d\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u8fd4\u56de None \u3002

        \u56fe 6-6 \u5c55\u793a\u4e86\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\uff09\u54c8\u5e0c\u8868\u7684\u952e\u503c\u5bf9\u5206\u5e03\u3002\u6839\u636e\u6b64\u54c8\u5e0c\u51fd\u6570\uff0c\u6700\u540e\u4e24\u4f4d\u76f8\u540c\u7684 key \u90fd\u4f1a\u88ab\u6620\u5c04\u5230\u76f8\u540c\u7684\u6876\u3002\u800c\u901a\u8fc7\u7ebf\u6027\u63a2\u6d4b\uff0c\u5b83\u4eec\u88ab\u4f9d\u6b21\u5b58\u50a8\u5728\u8be5\u6876\u4ee5\u53ca\u4e4b\u4e0b\u7684\u6876\u4e2d\u3002

        \u56fe 6-6 \u00a0 \u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\uff09\u54c8\u5e0c\u8868\u7684\u952e\u503c\u5bf9\u5206\u5e03

        \u7136\u800c\uff0c\u7ebf\u6027\u63a2\u6d4b\u5bb9\u6613\u4ea7\u751f\u201c\u805a\u96c6\u73b0\u8c61\u201d\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u6570\u7ec4\u4e2d\u8fde\u7eed\u88ab\u5360\u7528\u7684\u4f4d\u7f6e\u8d8a\u957f\uff0c\u8fd9\u4e9b\u8fde\u7eed\u4f4d\u7f6e\u53d1\u751f\u54c8\u5e0c\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u8d8a\u5927\uff0c\u4ece\u800c\u8fdb\u4e00\u6b65\u4fc3\u4f7f\u8be5\u4f4d\u7f6e\u7684\u805a\u5806\u751f\u957f\uff0c\u5f62\u6210\u6076\u6027\u5faa\u73af\uff0c\u6700\u7ec8\u5bfc\u81f4\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u6548\u7387\u52a3\u5316\u3002

        \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6211\u4eec\u4e0d\u80fd\u5728\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\u4e2d\u76f4\u63a5\u5220\u9664\u5143\u7d20\u3002\u8fd9\u662f\u56e0\u4e3a\u5220\u9664\u5143\u7d20\u4f1a\u5728\u6570\u7ec4\u5185\u4ea7\u751f\u4e00\u4e2a\u7a7a\u6876 None \uff0c\u800c\u5f53\u67e5\u8be2\u5143\u7d20\u65f6\uff0c\u7ebf\u6027\u63a2\u6d4b\u5230\u8be5\u7a7a\u6876\u5c31\u4f1a\u8fd4\u56de\uff0c\u56e0\u6b64\u5728\u8be5\u7a7a\u6876\u4e4b\u4e0b\u7684\u5143\u7d20\u90fd\u65e0\u6cd5\u518d\u88ab\u8bbf\u95ee\u5230\uff0c\u7a0b\u5e8f\u53ef\u80fd\u8bef\u5224\u8fd9\u4e9b\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u5982\u56fe 6-7 \u6240\u793a\u3002

        \u56fe 6-7 \u00a0 \u5728\u5f00\u653e\u5bfb\u5740\u4e2d\u5220\u9664\u5143\u7d20\u5bfc\u81f4\u7684\u67e5\u8be2\u95ee\u9898

        \u4e3a\u4e86\u89e3\u51b3\u8be5\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u7528\u61d2\u5220\u9664\uff08lazy deletion\uff09\u673a\u5236\uff1a\u5b83\u4e0d\u76f4\u63a5\u4ece\u54c8\u5e0c\u8868\u4e2d\u79fb\u9664\u5143\u7d20\uff0c\u800c\u662f\u5229\u7528\u4e00\u4e2a\u5e38\u91cf TOMBSTONE \u6765\u6807\u8bb0\u8fd9\u4e2a\u6876\u3002\u5728\u8be5\u673a\u5236\u4e0b\uff0cNone \u548c TOMBSTONE \u90fd\u4ee3\u8868\u7a7a\u6876\uff0c\u90fd\u53ef\u4ee5\u653e\u7f6e\u952e\u503c\u5bf9\u3002\u4f46\u4e0d\u540c\u7684\u662f\uff0c\u7ebf\u6027\u63a2\u6d4b\u5230 TOMBSTONE \u65f6\u5e94\u8be5\u7ee7\u7eed\u904d\u5386\uff0c\u56e0\u4e3a\u5176\u4e4b\u4e0b\u53ef\u80fd\u8fd8\u5b58\u5728\u952e\u503c\u5bf9\u3002

        \u7136\u800c\uff0c\u61d2\u5220\u9664\u53ef\u80fd\u4f1a\u52a0\u901f\u54c8\u5e0c\u8868\u7684\u6027\u80fd\u9000\u5316\u3002\u8fd9\u662f\u56e0\u4e3a\u6bcf\u6b21\u5220\u9664\u64cd\u4f5c\u90fd\u4f1a\u4ea7\u751f\u4e00\u4e2a\u5220\u9664\u6807\u8bb0\uff0c\u968f\u7740 TOMBSTONE \u7684\u589e\u52a0\uff0c\u641c\u7d22\u65f6\u95f4\u4e5f\u4f1a\u589e\u52a0\uff0c\u56e0\u4e3a\u7ebf\u6027\u63a2\u6d4b\u53ef\u80fd\u9700\u8981\u8df3\u8fc7\u591a\u4e2a TOMBSTONE \u624d\u80fd\u627e\u5230\u76ee\u6807\u5143\u7d20\u3002

        \u4e3a\u6b64\uff0c\u8003\u8651\u5728\u7ebf\u6027\u63a2\u6d4b\u4e2d\u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a TOMBSTONE \u7684\u7d22\u5f15\uff0c\u5e76\u5c06\u641c\u7d22\u5230\u7684\u76ee\u6807\u5143\u7d20\u4e0e\u8be5 TOMBSTONE \u4ea4\u6362\u4f4d\u7f6e\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u5f53\u6bcf\u6b21\u67e5\u8be2\u6216\u6dfb\u52a0\u5143\u7d20\u65f6\uff0c\u5143\u7d20\u4f1a\u88ab\u79fb\u52a8\u81f3\u8ddd\u79bb\u7406\u60f3\u4f4d\u7f6e\uff08\u63a2\u6d4b\u8d77\u59cb\u70b9\uff09\u66f4\u8fd1\u7684\u6876\uff0c\u4ece\u800c\u4f18\u5316\u67e5\u8be2\u6548\u7387\u3002

        \u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u5305\u542b\u61d2\u5220\u9664\u7684\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\uff09\u54c8\u5e0c\u8868\u3002\u4e3a\u4e86\u66f4\u52a0\u5145\u5206\u5730\u4f7f\u7528\u54c8\u5e0c\u8868\u7684\u7a7a\u95f4\uff0c\u6211\u4eec\u5c06\u54c8\u5e0c\u8868\u770b\u4f5c\u4e00\u4e2a\u201c\u73af\u5f62\u6570\u7ec4\u201d\uff0c\u5f53\u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u56de\u5230\u5934\u90e8\u7ee7\u7eed\u904d\u5386\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map_open_addressing.py
        class HashMapOpenAddressing:\n    \"\"\"\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\"\"\"\n\n    def __init__(self):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        self.size = 0  # \u952e\u503c\u5bf9\u6570\u91cf\n        self.capacity = 4  # \u54c8\u5e0c\u8868\u5bb9\u91cf\n        self.load_thres = 2.0 / 3.0  # \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n        self.extend_ratio = 2  # \u6269\u5bb9\u500d\u6570\n        self.buckets: list[Pair | None] = [None] * self.capacity  # \u6876\u6570\u7ec4\n        self.TOMBSTONE = Pair(-1, \"-1\")  # \u5220\u9664\u6807\u8bb0\n\n    def hash_func(self, key: int) -> int:\n        \"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\n        return key % self.capacity\n\n    def load_factor(self) -> float:\n        \"\"\"\u8d1f\u8f7d\u56e0\u5b50\"\"\"\n        return self.size / self.capacity\n\n    def find_bucket(self, key: int) -> int:\n        \"\"\"\u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\"\"\"\n        index = self.hash_func(key)\n        first_tombstone = -1\n        # \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n        while self.buckets[index] is not None:\n            # \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n            if self.buckets[index].key == key:\n                # \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n                if first_tombstone != -1:\n                    self.buckets[first_tombstone] = self.buckets[index]\n                    self.buckets[index] = self.TOMBSTONE\n                    return first_tombstone  # \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n                return index  # \u8fd4\u56de\u6876\u7d22\u5f15\n            # \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n            if first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE:\n                first_tombstone = index\n            # \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n            index = (index + 1) % self.capacity\n        # \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n        return index if first_tombstone == -1 else first_tombstone\n\n    def get(self, key: int) -> str:\n        \"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\n        # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        index = self.find_bucket(key)\n        # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        if self.buckets[index] not in [None, self.TOMBSTONE]:\n            return self.buckets[index].val\n        # \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de None\n        return None\n\n    def put(self, key: int, val: str):\n        \"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"\n        # \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if self.load_factor() > self.load_thres:\n            self.extend()\n        # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        index = self.find_bucket(key)\n        # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n        if self.buckets[index] not in [None, self.TOMBSTONE]:\n            self.buckets[index].val = val\n            return\n        # \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n        self.buckets[index] = Pair(key, val)\n        self.size += 1\n\n    def remove(self, key: int):\n        \"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\n        # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        index = self.find_bucket(key)\n        # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n        if self.buckets[index] not in [None, self.TOMBSTONE]:\n            self.buckets[index] = self.TOMBSTONE\n            self.size -= 1\n\n    def extend(self):\n        \"\"\"\u6269\u5bb9\u54c8\u5e0c\u8868\"\"\"\n        # \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        buckets_tmp = self.buckets\n        # \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        self.capacity *= self.extend_ratio\n        self.buckets = [None] * self.capacity\n        self.size = 0\n        # \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for pair in buckets_tmp:\n            if pair not in [None, self.TOMBSTONE]:\n                self.put(pair.key, pair.val)\n\n    def print(self):\n        \"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\n        for pair in self.buckets:\n            if pair is None:\n                print(\"None\")\n            elif pair is self.TOMBSTONE:\n                print(\"TOMBSTONE\")\n            else:\n                print(pair.key, \"->\", pair.val)\n
        hash_map_open_addressing.cpp
        /* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n  private:\n    int size;                             // \u952e\u503c\u5bf9\u6570\u91cf\n    int capacity = 4;                     // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    const double loadThres = 2.0 / 3.0;     // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    const int extendRatio = 2;            // \u6269\u5bb9\u500d\u6570\n    vector<Pair *> buckets;               // \u6876\u6570\u7ec4\n    Pair *TOMBSTONE = new Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n  public:\n    /* \u6784\u9020\u65b9\u6cd5 */\n    HashMapOpenAddressing() : size(0), buckets(capacity, nullptr) {\n    }\n\n    /* \u6790\u6784\u65b9\u6cd5 */\n    ~HashMapOpenAddressing() {\n        for (Pair *pair : buckets) {\n            if (pair != nullptr && pair != TOMBSTONE) {\n                delete pair;\n            }\n        }\n        delete TOMBSTONE;\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    double loadFactor() {\n        return (double)size / capacity;\n    }\n\n    /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n    int findBucket(int key) {\n        int index = hashFunc(key);\n        int firstTombstone = -1;\n        // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n        while (buckets[index] != nullptr) {\n            // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n            if (buckets[index]->key == key) {\n                // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index];\n                    buckets[index] = TOMBSTONE;\n                    return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n                }\n                return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n            }\n            // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index;\n            }\n            // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n            index = (index + 1) % capacity;\n        }\n        // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n        return firstTombstone == -1 ? index : firstTombstone;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    string get(int key) {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        int index = findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n            return buckets[index]->val;\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n        return \"\";\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    void put(int key, string val) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        int index = findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n        if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n            buckets[index]->val = val;\n            return;\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n        buckets[index] = new Pair(key, val);\n        size++;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    void remove(int key) {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        int index = findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n        if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n            delete buckets[index];\n            buckets[index] = TOMBSTONE;\n            size--;\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    void extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        vector<Pair *> bucketsTmp = buckets;\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        capacity *= extendRatio;\n        buckets = vector<Pair *>(capacity, nullptr);\n        size = 0;\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for (Pair *pair : bucketsTmp) {\n            if (pair != nullptr && pair != TOMBSTONE) {\n                put(pair->key, pair->val);\n                delete pair;\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    void print() {\n        for (Pair *pair : buckets) {\n            if (pair == nullptr) {\n                cout << \"nullptr\" << endl;\n            } else if (pair == TOMBSTONE) {\n                cout << \"TOMBSTONE\" << endl;\n            } else {\n                cout << pair->key << \" -> \" << pair->val << endl;\n            }\n        }\n    }\n};\n
        hash_map_open_addressing.java
        /* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n    private int size; // \u952e\u503c\u5bf9\u6570\u91cf\n    private int capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    private final double loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    private final int extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n    private Pair[] buckets; // \u6876\u6570\u7ec4\n    private final Pair TOMBSTONE = new Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public HashMapOpenAddressing() {\n        size = 0;\n        buckets = new Pair[capacity];\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    private int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    private double loadFactor() {\n        return (double) size / capacity;\n    }\n\n    /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n    private int findBucket(int key) {\n        int index = hashFunc(key);\n        int firstTombstone = -1;\n        // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n        while (buckets[index] != null) {\n            // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n            if (buckets[index].key == key) {\n                // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index];\n                    buckets[index] = TOMBSTONE;\n                    return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n                }\n                return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n            }\n            // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index;\n            }\n            // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n            index = (index + 1) % capacity;\n        }\n        // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n        return firstTombstone == -1 ? index : firstTombstone;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    public String get(int key) {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        int index = findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            return buckets[index].val;\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n        return null;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    public void put(int key, String val) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        int index = findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index].val = val;\n            return;\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n        buckets[index] = new Pair(key, val);\n        size++;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    public void remove(int key) {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        int index = findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index] = TOMBSTONE;\n            size--;\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    private void extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        Pair[] bucketsTmp = buckets;\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        capacity *= extendRatio;\n        buckets = new Pair[capacity];\n        size = 0;\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for (Pair pair : bucketsTmp) {\n            if (pair != null && pair != TOMBSTONE) {\n                put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    public void print() {\n        for (Pair pair : buckets) {\n            if (pair == null) {\n                System.out.println(\"null\");\n            } else if (pair == TOMBSTONE) {\n                System.out.println(\"TOMBSTONE\");\n            } else {\n                System.out.println(pair.key + \" -> \" + pair.val);\n            }\n        }\n    }\n}\n
        hash_map_open_addressing.cs
        /* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n    int size; // \u952e\u503c\u5bf9\u6570\u91cf\n    int capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    double loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    int extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n    Pair[] buckets; // \u6876\u6570\u7ec4\n    Pair TOMBSTONE = new(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public HashMapOpenAddressing() {\n        size = 0;\n        buckets = new Pair[capacity];\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    int HashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    double LoadFactor() {\n        return (double)size / capacity;\n    }\n\n    /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n    int FindBucket(int key) {\n        int index = HashFunc(key);\n        int firstTombstone = -1;\n        // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n        while (buckets[index] != null) {\n            // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n            if (buckets[index].key == key) {\n                // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index];\n                    buckets[index] = TOMBSTONE;\n                    return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n                }\n                return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n            }\n            // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index;\n            }\n            // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n            index = (index + 1) % capacity;\n        }\n        // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n        return firstTombstone == -1 ? index : firstTombstone;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    public string? Get(int key) {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        int index = FindBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            return buckets[index].val;\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n        return null;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    public void Put(int key, string val) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (LoadFactor() > loadThres) {\n            Extend();\n        }\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        int index = FindBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index].val = val;\n            return;\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n        buckets[index] = new Pair(key, val);\n        size++;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    public void Remove(int key) {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        int index = FindBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index] = TOMBSTONE;\n            size--;\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    void Extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        Pair[] bucketsTmp = buckets;\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        capacity *= extendRatio;\n        buckets = new Pair[capacity];\n        size = 0;\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        foreach (Pair pair in bucketsTmp) {\n            if (pair != null && pair != TOMBSTONE) {\n                Put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    public void Print() {\n        foreach (Pair pair in buckets) {\n            if (pair == null) {\n                Console.WriteLine(\"null\");\n            } else if (pair == TOMBSTONE) {\n                Console.WriteLine(\"TOMBSTONE\");\n            } else {\n                Console.WriteLine(pair.key + \" -> \" + pair.val);\n            }\n        }\n    }\n}\n
        hash_map_open_addressing.go
        /* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\ntype hashMapOpenAddressing struct {\n    size        int     // \u952e\u503c\u5bf9\u6570\u91cf\n    capacity    int     // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    loadThres   float64 // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    extendRatio int     // \u6269\u5bb9\u500d\u6570\n    buckets     []*pair // \u6876\u6570\u7ec4\n    TOMBSTONE   *pair   // \u5220\u9664\u6807\u8bb0\n}\n\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newHashMapOpenAddressing() *hashMapOpenAddressing {\n    return &hashMapOpenAddressing{\n        size:        0,\n        capacity:    4,\n        loadThres:   2.0 / 3.0,\n        extendRatio: 2,\n        buckets:     make([]*pair, 4),\n        TOMBSTONE:   &pair{-1, \"-1\"},\n    }\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (h *hashMapOpenAddressing) hashFunc(key int) int {\n    return key % h.capacity // \u6839\u636e\u952e\u8ba1\u7b97\u54c8\u5e0c\u503c\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc (h *hashMapOpenAddressing) loadFactor() float64 {\n    return float64(h.size) / float64(h.capacity) // \u8ba1\u7b97\u5f53\u524d\u8d1f\u8f7d\u56e0\u5b50\n}\n\n/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nfunc (h *hashMapOpenAddressing) findBucket(key int) int {\n    index := h.hashFunc(key) // \u83b7\u53d6\u521d\u59cb\u7d22\u5f15\n    firstTombstone := -1     // \u8bb0\u5f55\u9047\u5230\u7684\u7b2c\u4e00\u4e2aTOMBSTONE\u7684\u4f4d\u7f6e\n    for h.buckets[index] != nil {\n        if h.buckets[index].key == key {\n            if firstTombstone != -1 {\n                // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n                h.buckets[firstTombstone] = h.buckets[index]\n                h.buckets[index] = h.TOMBSTONE\n                return firstTombstone // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n            }\n            return index // \u8fd4\u56de\u627e\u5230\u7684\u7d22\u5f15\n        }\n        if firstTombstone == -1 && h.buckets[index] == h.TOMBSTONE {\n            firstTombstone = index // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\u7684\u4f4d\u7f6e\n        }\n        index = (index + 1) % h.capacity // \u7ebf\u6027\u63a2\u6d4b\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n    }\n    // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n    if firstTombstone != -1 {\n        return firstTombstone\n    }\n    return index\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (h *hashMapOpenAddressing) get(key int) string {\n    index := h.findBucket(key) // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    if h.buckets[index] != nil && h.buckets[index] != h.TOMBSTONE {\n        return h.buckets[index].val // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n    }\n    return \"\" // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de \"\"\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (h *hashMapOpenAddressing) put(key int, val string) {\n    if h.loadFactor() > h.loadThres {\n        h.extend() // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n    }\n    index := h.findBucket(key) // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    if h.buckets[index] == nil || h.buckets[index] == h.TOMBSTONE {\n        h.buckets[index] = &pair{key, val} // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n        h.size++\n    } else {\n        h.buckets[index].val = val // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val\n    }\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (h *hashMapOpenAddressing) remove(key int) {\n    index := h.findBucket(key) // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    if h.buckets[index] != nil && h.buckets[index] != h.TOMBSTONE {\n        h.buckets[index] = h.TOMBSTONE // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n        h.size--\n    }\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc (h *hashMapOpenAddressing) extend() {\n    oldBuckets := h.buckets               // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n    h.capacity *= h.extendRatio           // \u66f4\u65b0\u5bb9\u91cf\n    h.buckets = make([]*pair, h.capacity) // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n    h.size = 0                            // \u91cd\u7f6e\u5927\u5c0f\n    // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n    for _, pair := range oldBuckets {\n        if pair != nil && pair != h.TOMBSTONE {\n            h.put(pair.key, pair.val)\n        }\n    }\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (h *hashMapOpenAddressing) print() {\n    for _, pair := range h.buckets {\n        if pair == nil {\n            fmt.Println(\"nil\")\n        } else if pair == h.TOMBSTONE {\n            fmt.Println(\"TOMBSTONE\")\n        } else {\n            fmt.Printf(\"%d -> %s\\n\", pair.key, pair.val)\n        }\n    }\n}\n
        hash_map_open_addressing.swift
        /* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n    var size: Int // \u952e\u503c\u5bf9\u6570\u91cf\n    var capacity: Int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    var loadThres: Double // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    var extendRatio: Int // \u6269\u5bb9\u500d\u6570\n    var buckets: [Pair?] // \u6876\u6570\u7ec4\n    var TOMBSTONE: Pair // \u5220\u9664\u6807\u8bb0\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    init() {\n        size = 0\n        capacity = 4\n        loadThres = 2.0 / 3.0\n        extendRatio = 2\n        buckets = Array(repeating: nil, count: capacity)\n        TOMBSTONE = Pair(key: -1, val: \"-1\")\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    func hashFunc(key: Int) -> Int {\n        key % capacity\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    func loadFactor() -> Double {\n        Double(size) / Double(capacity)\n    }\n\n    /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n    func findBucket(key: Int) -> Int {\n        var index = hashFunc(key: key)\n        var firstTombstone = -1\n        // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n        while buckets[index] != nil {\n            // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n            if buckets[index]!.key == key {\n                // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n                if firstTombstone != -1 {\n                    buckets[firstTombstone] = buckets[index]\n                    buckets[index] = TOMBSTONE\n                    return firstTombstone // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n                }\n                return index // \u8fd4\u56de\u6876\u7d22\u5f15\n            }\n            // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n            if firstTombstone == -1 && buckets[index] == TOMBSTONE {\n                firstTombstone = index\n            }\n            // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n            index = (index + 1) % capacity\n        }\n        // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n        return firstTombstone == -1 ? index : firstTombstone\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    func get(key: Int) -> String? {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        let index = findBucket(key: key)\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        if buckets[index] != nil, buckets[index] != TOMBSTONE {\n            return buckets[index]!.val\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n        return nil\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    func put(key: Int, val: String) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if loadFactor() > loadThres {\n            extend()\n        }\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        let index = findBucket(key: key)\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n        if buckets[index] != nil, buckets[index] != TOMBSTONE {\n            buckets[index]!.val = val\n            return\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n        buckets[index] = Pair(key: key, val: val)\n        size += 1\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    func remove(key: Int) {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        let index = findBucket(key: key)\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n        if buckets[index] != nil, buckets[index] != TOMBSTONE {\n            buckets[index] = TOMBSTONE\n            size -= 1\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    func extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        let bucketsTmp = buckets\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        capacity *= extendRatio\n        buckets = Array(repeating: nil, count: capacity)\n        size = 0\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for pair in bucketsTmp {\n            if let pair, pair != TOMBSTONE {\n                put(key: pair.key, val: pair.val)\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    func print() {\n        for pair in buckets {\n            if pair == nil {\n                Swift.print(\"null\")\n            } else if pair == TOMBSTONE {\n                Swift.print(\"TOMBSTONE\")\n            } else {\n                Swift.print(\"\\(pair!.key) -> \\(pair!.val)\")\n            }\n        }\n    }\n}\n
        hash_map_open_addressing.js
        /* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n    #size; // \u952e\u503c\u5bf9\u6570\u91cf\n    #capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    #loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    #extendRatio; // \u6269\u5bb9\u500d\u6570\n    #buckets; // \u6876\u6570\u7ec4\n    #TOMBSTONE; // \u5220\u9664\u6807\u8bb0\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor() {\n        this.#size = 0; // \u952e\u503c\u5bf9\u6570\u91cf\n        this.#capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n        this.#loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n        this.#extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n        this.#buckets = Array(this.#capacity).fill(null); // \u6876\u6570\u7ec4\n        this.#TOMBSTONE = new Pair(-1, '-1'); // \u5220\u9664\u6807\u8bb0\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    #hashFunc(key) {\n        return key % this.#capacity;\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    #loadFactor() {\n        return this.#size / this.#capacity;\n    }\n\n    /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n    #findBucket(key) {\n        let index = this.#hashFunc(key);\n        let firstTombstone = -1;\n        // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n        while (this.#buckets[index] !== null) {\n            // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n            if (this.#buckets[index].key === key) {\n                // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n                if (firstTombstone !== -1) {\n                    this.#buckets[firstTombstone] = this.#buckets[index];\n                    this.#buckets[index] = this.#TOMBSTONE;\n                    return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n                }\n                return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n            }\n            // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n            if (\n                firstTombstone === -1 &&\n                this.#buckets[index] === this.#TOMBSTONE\n            ) {\n                firstTombstone = index;\n            }\n            // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n            index = (index + 1) % this.#capacity;\n        }\n        // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n        return firstTombstone === -1 ? index : firstTombstone;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    get(key) {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        const index = this.#findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        if (\n            this.#buckets[index] !== null &&\n            this.#buckets[index] !== this.#TOMBSTONE\n        ) {\n            return this.#buckets[index].val;\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n        return null;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    put(key, val) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (this.#loadFactor() > this.#loadThres) {\n            this.#extend();\n        }\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        const index = this.#findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n        if (\n            this.#buckets[index] !== null &&\n            this.#buckets[index] !== this.#TOMBSTONE\n        ) {\n            this.#buckets[index].val = val;\n            return;\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n        this.#buckets[index] = new Pair(key, val);\n        this.#size++;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    remove(key) {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        const index = this.#findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n        if (\n            this.#buckets[index] !== null &&\n            this.#buckets[index] !== this.#TOMBSTONE\n        ) {\n            this.#buckets[index] = this.#TOMBSTONE;\n            this.#size--;\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    #extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        const bucketsTmp = this.#buckets;\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        this.#capacity *= this.#extendRatio;\n        this.#buckets = Array(this.#capacity).fill(null);\n        this.#size = 0;\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for (const pair of bucketsTmp) {\n            if (pair !== null && pair !== this.#TOMBSTONE) {\n                this.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    print() {\n        for (const pair of this.#buckets) {\n            if (pair === null) {\n                console.log('null');\n            } else if (pair === this.#TOMBSTONE) {\n                console.log('TOMBSTONE');\n            } else {\n                console.log(pair.key + ' -> ' + pair.val);\n            }\n        }\n    }\n}\n
        hash_map_open_addressing.ts
        /* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n    private size: number; // \u952e\u503c\u5bf9\u6570\u91cf\n    private capacity: number; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    private loadThres: number; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    private extendRatio: number; // \u6269\u5bb9\u500d\u6570\n    private buckets: Array<Pair | null>; // \u6876\u6570\u7ec4\n    private TOMBSTONE: Pair; // \u5220\u9664\u6807\u8bb0\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor() {\n        this.size = 0; // \u952e\u503c\u5bf9\u6570\u91cf\n        this.capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n        this.loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n        this.extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n        this.buckets = Array(this.capacity).fill(null); // \u6876\u6570\u7ec4\n        this.TOMBSTONE = new Pair(-1, '-1'); // \u5220\u9664\u6807\u8bb0\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    private hashFunc(key: number): number {\n        return key % this.capacity;\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    private loadFactor(): number {\n        return this.size / this.capacity;\n    }\n\n    /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n    private findBucket(key: number): number {\n        let index = this.hashFunc(key);\n        let firstTombstone = -1;\n        // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n        while (this.buckets[index] !== null) {\n            // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n            if (this.buckets[index]!.key === key) {\n                // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n                if (firstTombstone !== -1) {\n                    this.buckets[firstTombstone] = this.buckets[index];\n                    this.buckets[index] = this.TOMBSTONE;\n                    return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n                }\n                return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n            }\n            // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n            if (\n                firstTombstone === -1 &&\n                this.buckets[index] === this.TOMBSTONE\n            ) {\n                firstTombstone = index;\n            }\n            // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n            index = (index + 1) % this.capacity;\n        }\n        // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n        return firstTombstone === -1 ? index : firstTombstone;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    get(key: number): string | null {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        const index = this.findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        if (\n            this.buckets[index] !== null &&\n            this.buckets[index] !== this.TOMBSTONE\n        ) {\n            return this.buckets[index]!.val;\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n        return null;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    put(key: number, val: string): void {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (this.loadFactor() > this.loadThres) {\n            this.extend();\n        }\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        const index = this.findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n        if (\n            this.buckets[index] !== null &&\n            this.buckets[index] !== this.TOMBSTONE\n        ) {\n            this.buckets[index]!.val = val;\n            return;\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n        this.buckets[index] = new Pair(key, val);\n        this.size++;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    remove(key: number): void {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        const index = this.findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n        if (\n            this.buckets[index] !== null &&\n            this.buckets[index] !== this.TOMBSTONE\n        ) {\n            this.buckets[index] = this.TOMBSTONE;\n            this.size--;\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    private extend(): void {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        const bucketsTmp = this.buckets;\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        this.capacity *= this.extendRatio;\n        this.buckets = Array(this.capacity).fill(null);\n        this.size = 0;\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for (const pair of bucketsTmp) {\n            if (pair !== null && pair !== this.TOMBSTONE) {\n                this.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    print(): void {\n        for (const pair of this.buckets) {\n            if (pair === null) {\n                console.log('null');\n            } else if (pair === this.TOMBSTONE) {\n                console.log('TOMBSTONE');\n            } else {\n                console.log(pair.key + ' -> ' + pair.val);\n            }\n        }\n    }\n}\n
        hash_map_open_addressing.dart
        /* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n  late int _size; // \u952e\u503c\u5bf9\u6570\u91cf\n  int _capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n  double _loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n  int _extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n  late List<Pair?> _buckets; // \u6876\u6570\u7ec4\n  Pair _TOMBSTONE = Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n  /* \u6784\u9020\u65b9\u6cd5 */\n  HashMapOpenAddressing() {\n    _size = 0;\n    _buckets = List.generate(_capacity, (index) => null);\n  }\n\n  /* \u54c8\u5e0c\u51fd\u6570 */\n  int hashFunc(int key) {\n    return key % _capacity;\n  }\n\n  /* \u8d1f\u8f7d\u56e0\u5b50 */\n  double loadFactor() {\n    return _size / _capacity;\n  }\n\n  /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n  int findBucket(int key) {\n    int index = hashFunc(key);\n    int firstTombstone = -1;\n    // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n    while (_buckets[index] != null) {\n      // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n      if (_buckets[index]!.key == key) {\n        // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n        if (firstTombstone != -1) {\n          _buckets[firstTombstone] = _buckets[index];\n          _buckets[index] = _TOMBSTONE;\n          return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n        }\n        return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n      }\n      // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n      if (firstTombstone == -1 && _buckets[index] == _TOMBSTONE) {\n        firstTombstone = index;\n      }\n      // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n      index = (index + 1) % _capacity;\n    }\n    // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n    return firstTombstone == -1 ? index : firstTombstone;\n  }\n\n  /* \u67e5\u8be2\u64cd\u4f5c */\n  String? get(int key) {\n    // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    int index = findBucket(key);\n    // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n    if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n      return _buckets[index]!.val;\n    }\n    // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n    return null;\n  }\n\n  /* \u6dfb\u52a0\u64cd\u4f5c */\n  void put(int key, String val) {\n    // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n    if (loadFactor() > _loadThres) {\n      extend();\n    }\n    // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    int index = findBucket(key);\n    // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n    if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n      _buckets[index]!.val = val;\n      return;\n    }\n    // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n    _buckets[index] = new Pair(key, val);\n    _size++;\n  }\n\n  /* \u5220\u9664\u64cd\u4f5c */\n  void remove(int key) {\n    // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    int index = findBucket(key);\n    // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n    if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n      _buckets[index] = _TOMBSTONE;\n      _size--;\n    }\n  }\n\n  /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n  void extend() {\n    // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n    List<Pair?> bucketsTmp = _buckets;\n    // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n    _capacity *= _extendRatio;\n    _buckets = List.generate(_capacity, (index) => null);\n    _size = 0;\n    // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n    for (Pair? pair in bucketsTmp) {\n      if (pair != null && pair != _TOMBSTONE) {\n        put(pair.key, pair.val);\n      }\n    }\n  }\n\n  /* \u6253\u5370\u54c8\u5e0c\u8868 */\n  void printHashMap() {\n    for (Pair? pair in _buckets) {\n      if (pair == null) {\n        print(\"null\");\n      } else if (pair == _TOMBSTONE) {\n        print(\"TOMBSTONE\");\n      } else {\n        print(\"${pair.key} -> ${pair.val}\");\n      }\n    }\n  }\n}\n
        hash_map_open_addressing.rs
        /* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nstruct HashMapOpenAddressing {\n    size: usize,                // \u952e\u503c\u5bf9\u6570\u91cf\n    capacity: usize,            // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    load_thres: f64,            // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    extend_ratio: usize,        // \u6269\u5bb9\u500d\u6570\n    buckets: Vec<Option<Pair>>, // \u6876\u6570\u7ec4\n    TOMBSTONE: Option<Pair>,    // \u5220\u9664\u6807\u8bb0\n}\n\nimpl HashMapOpenAddressing {\n    /* \u6784\u9020\u65b9\u6cd5 */\n    fn new() -> Self {\n        Self {\n            size: 0,\n            capacity: 4,\n            load_thres: 2.0 / 3.0,\n            extend_ratio: 2,\n            buckets: vec![None; 4],\n            TOMBSTONE: Some(Pair {\n                key: -1,\n                val: \"-1\".to_string(),\n            }),\n        }\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    fn hash_func(&self, key: i32) -> usize {\n        (key % self.capacity as i32) as usize\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    fn load_factor(&self) -> f64 {\n        self.size as f64 / self.capacity as f64\n    }\n\n    /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n    fn find_bucket(&mut self, key: i32) -> usize {\n        let mut index = self.hash_func(key);\n        let mut first_tombstone = -1;\n        // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n        while self.buckets[index].is_some() {\n            // \u82e5\u9047\u5230 key\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n            if self.buckets[index].as_ref().unwrap().key == key {\n                // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u5efa\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n                if first_tombstone != -1 {\n                    self.buckets[first_tombstone as usize] = self.buckets[index].take();\n                    self.buckets[index] = self.TOMBSTONE.clone();\n                    return first_tombstone as usize; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n                }\n                return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n            }\n            // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n            if first_tombstone == -1 && self.buckets[index] == self.TOMBSTONE {\n                first_tombstone = index as i32;\n            }\n            // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n            index = (index + 1) % self.capacity;\n        }\n        // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n        if first_tombstone == -1 {\n            index\n        } else {\n            first_tombstone as usize\n        }\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    fn get(&mut self, key: i32) -> Option<&str> {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        let index = self.find_bucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n            return self.buckets[index].as_ref().map(|pair| &pair.val as &str);\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n        None\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    fn put(&mut self, key: i32, val: String) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if self.load_factor() > self.load_thres {\n            self.extend();\n        }\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        let index = self.find_bucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n        if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n            self.buckets[index].as_mut().unwrap().val = val;\n            return;\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n        self.buckets[index] = Some(Pair { key, val });\n        self.size += 1;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    fn remove(&mut self, key: i32) {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        let index = self.find_bucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n        if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n            self.buckets[index] = self.TOMBSTONE.clone();\n            self.size -= 1;\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    fn extend(&mut self) {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        let buckets_tmp = self.buckets.clone();\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        self.capacity *= self.extend_ratio;\n        self.buckets = vec![None; self.capacity];\n        self.size = 0;\n\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for pair in buckets_tmp {\n            if pair.is_none() || pair == self.TOMBSTONE {\n                continue;\n            }\n            let pair = pair.unwrap();\n\n            self.put(pair.key, pair.val);\n        }\n    }\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    fn print(&self) {\n        for pair in &self.buckets {\n            if pair.is_none() {\n                println!(\"null\");\n            } else if pair == &self.TOMBSTONE {\n                println!(\"TOMBSTONE\");\n            } else {\n                let pair = pair.as_ref().unwrap();\n                println!(\"{} -> {}\", pair.key, pair.val);\n            }\n        }\n    }\n}\n
        hash_map_open_addressing.c
        /* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\ntypedef struct {\n    int size;         // \u952e\u503c\u5bf9\u6570\u91cf\n    int capacity;     // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    int extendRatio;  // \u6269\u5bb9\u500d\u6570\n    Pair **buckets;   // \u6876\u6570\u7ec4\n    Pair *TOMBSTONE;  // \u5220\u9664\u6807\u8bb0\n} HashMapOpenAddressing;\n\n/* \u6784\u9020\u51fd\u6570 */\nHashMapOpenAddressing *newHashMapOpenAddressing() {\n    HashMapOpenAddressing *hashMap = (HashMapOpenAddressing *)malloc(sizeof(HashMapOpenAddressing));\n    hashMap->size = 0;\n    hashMap->capacity = 4;\n    hashMap->loadThres = 2.0 / 3.0;\n    hashMap->extendRatio = 2;\n    hashMap->buckets = (Pair **)calloc(hashMap->capacity, sizeof(Pair *));\n    hashMap->TOMBSTONE = (Pair *)malloc(sizeof(Pair));\n    hashMap->TOMBSTONE->key = -1;\n    hashMap->TOMBSTONE->val = \"-1\";\n\n    return hashMap;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delHashMapOpenAddressing(HashMapOpenAddressing *hashMap) {\n    for (int i = 0; i < hashMap->capacity; i++) {\n        Pair *pair = hashMap->buckets[i];\n        if (pair != NULL && pair != hashMap->TOMBSTONE) {\n            free(pair->val);\n            free(pair);\n        }\n    }\n    free(hashMap->buckets);\n    free(hashMap->TOMBSTONE);\n    free(hashMap);\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(HashMapOpenAddressing *hashMap, int key) {\n    return key % hashMap->capacity;\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor(HashMapOpenAddressing *hashMap) {\n    return (double)hashMap->size / (double)hashMap->capacity;\n}\n\n/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nint findBucket(HashMapOpenAddressing *hashMap, int key) {\n    int index = hashFunc(hashMap, key);\n    int firstTombstone = -1;\n    // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n    while (hashMap->buckets[index] != NULL) {\n        // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        if (hashMap->buckets[index]->key == key) {\n            // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n            if (firstTombstone != -1) {\n                hashMap->buckets[firstTombstone] = hashMap->buckets[index];\n                hashMap->buckets[index] = hashMap->TOMBSTONE;\n                return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n            }\n            return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n        }\n        // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n        if (firstTombstone == -1 && hashMap->buckets[index] == hashMap->TOMBSTONE) {\n            firstTombstone = index;\n        }\n        // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n        index = (index + 1) % hashMap->capacity;\n    }\n    // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n    return firstTombstone == -1 ? index : firstTombstone;\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nchar *get(HashMapOpenAddressing *hashMap, int key) {\n    // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    int index = findBucket(hashMap, key);\n    // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n    if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n        return hashMap->buckets[index]->val;\n    }\n    // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n    return \"\";\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(HashMapOpenAddressing *hashMap, int key, char *val) {\n    // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n    if (loadFactor(hashMap) > hashMap->loadThres) {\n        extend(hashMap);\n    }\n    // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    int index = findBucket(hashMap, key);\n    // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n    if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n        free(hashMap->buckets[index]->val);\n        hashMap->buckets[index]->val = (char *)malloc(sizeof(strlen(val) + 1));\n        strcpy(hashMap->buckets[index]->val, val);\n        hashMap->buckets[index]->val[strlen(val)] = '\\0';\n        return;\n    }\n    // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n    Pair *pair = (Pair *)malloc(sizeof(Pair));\n    pair->key = key;\n    pair->val = (char *)malloc(sizeof(strlen(val) + 1));\n    strcpy(pair->val, val);\n    pair->val[strlen(val)] = '\\0';\n\n    hashMap->buckets[index] = pair;\n    hashMap->size++;\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeItem(HashMapOpenAddressing *hashMap, int key) {\n    // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    int index = findBucket(hashMap, key);\n    // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n    if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n        Pair *pair = hashMap->buckets[index];\n        free(pair->val);\n        free(pair);\n        hashMap->buckets[index] = hashMap->TOMBSTONE;\n        hashMap->size--;\n    }\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend(HashMapOpenAddressing *hashMap) {\n    // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n    Pair **bucketsTmp = hashMap->buckets;\n    int oldCapacity = hashMap->capacity;\n    // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n    hashMap->capacity *= hashMap->extendRatio;\n    hashMap->buckets = (Pair **)calloc(hashMap->capacity, sizeof(Pair *));\n    hashMap->size = 0;\n    // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n    for (int i = 0; i < oldCapacity; i++) {\n        Pair *pair = bucketsTmp[i];\n        if (pair != NULL && pair != hashMap->TOMBSTONE) {\n            put(hashMap, pair->key, pair->val);\n            free(pair->val);\n            free(pair);\n        }\n    }\n    free(bucketsTmp);\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(HashMapOpenAddressing *hashMap) {\n    for (int i = 0; i < hashMap->capacity; i++) {\n        Pair *pair = hashMap->buckets[i];\n        if (pair == NULL) {\n            printf(\"NULL\\n\");\n        } else if (pair == hashMap->TOMBSTONE) {\n            printf(\"TOMBSTONE\\n\");\n        } else {\n            printf(\"%d -> %s\\n\", pair->key, pair->val);\n        }\n    }\n}\n
        hash_map_open_addressing.kt
        /* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n    private var size: Int               // \u952e\u503c\u5bf9\u6570\u91cf\n    private var capacity: Int           // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    private val loadThres: Double       // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    private val extendRatio: Int        // \u6269\u5bb9\u500d\u6570\n    private var buckets: Array<Pair?>   // \u6876\u6570\u7ec4\n    private val TOMBSTONE: Pair         // \u5220\u9664\u6807\u8bb0\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    init {\n        size = 0\n        capacity = 4\n        loadThres = 2.0 / 3.0\n        extendRatio = 2\n        buckets = arrayOfNulls(capacity)\n        TOMBSTONE = Pair(-1, \"-1\")\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    fun hashFunc(key: Int): Int {\n        return key % capacity\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    fun loadFactor(): Double {\n        return (size / capacity).toDouble()\n    }\n\n    /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n    fun findBucket(key: Int): Int {\n        var index = hashFunc(key)\n        var firstTombstone = -1\n        // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n        while (buckets[index] != null) {\n            // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n            if (buckets[index]?.key == key) {\n                // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index]\n                    buckets[index] = TOMBSTONE\n                    return firstTombstone // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n                }\n                return index // \u8fd4\u56de\u6876\u7d22\u5f15\n            }\n            // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index\n            }\n            // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n            index = (index + 1) % capacity\n        }\n        // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n        return if (firstTombstone == -1) index else firstTombstone\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    fun get(key: Int): String? {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        val index = findBucket(key)\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            return buckets[index]?._val\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n        return null\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    fun put(key: Int, _val: String) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (loadFactor() > loadThres) {\n            extend()\n        }\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        val index = findBucket(key)\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index]!!._val = _val\n            return\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n        buckets[index] = Pair(key, _val)\n        size++\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    fun remove(key: Int) {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        val index = findBucket(key)\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index] = TOMBSTONE\n            size--\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    fun extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        val bucketsTmp = buckets\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        capacity *= extendRatio\n        buckets = arrayOfNulls(capacity)\n        size = 0\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for (pair in bucketsTmp) {\n            if (pair != null && pair != TOMBSTONE) {\n                put(pair.key, pair._val)\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    fun print() {\n        for (pair in buckets) {\n            if (pair == null) {\n                println(\"null\")\n            } else if (pair == TOMBSTONE) {\n                println(\"TOMESTOME\")\n            } else {\n                println(\"${pair.key} -> ${pair._val}\")\n            }\n        }\n    }\n}\n
        hash_map_open_addressing.rb
        ### \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 ###\nclass HashMapOpenAddressing\n  TOMBSTONE = Pair.new(-1, '-1') # \u5220\u9664\u6807\u8bb0\n\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize\n    @size = 0 # \u952e\u503c\u5bf9\u6570\u91cf\n    @capacity = 4 # \u54c8\u5e0c\u8868\u5bb9\u91cf\n    @load_thres = 2.0 / 3.0 # \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    @extend_ratio = 2 # \u6269\u5bb9\u500d\u6570\n    @buckets = Array.new(@capacity) # \u6876\u6570\u7ec4\n  end\n\n  ### \u54c8\u5e0c\u51fd\u6570 ###\n  def hash_func(key)\n    key % @capacity\n  end\n\n  ### \u8d1f\u8f7d\u56e0\u5b50 ###\n  def load_factor\n    @size / @capacity\n  end\n\n  ### \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 ###\n  def find_bucket(key)\n    index = hash_func(key)\n    first_tombstone = -1\n    # \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n    while !@buckets[index].nil?\n      # \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n      if @buckets[index].key == key\n        # \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n        if first_tombstone != -1\n          @buckets[first_tombstone] = @buckets[index]\n          @buckets[index] = TOMBSTONE\n          return first_tombstone # \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n        end\n        return index # \u8fd4\u56de\u6876\u7d22\u5f15\n      end\n      # \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n      first_tombstone = index if first_tombstone == -1 && @buckets[index] == TOMBSTONE\n      # \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n      index = (index + 1) % @capacity\n    end\n    # \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n    first_tombstone == -1 ? index : first_tombstone\n  end\n\n  ### \u67e5\u8be2\u64cd\u4f5c ###\n  def get(key)\n    # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    index = find_bucket(key)\n    # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n    return @buckets[index].val unless [nil, TOMBSTONE].include?(@buckets[index])\n    # \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de nil\n    nil\n  end\n\n  ### \u6dfb\u52a0\u64cd\u4f5c ###\n  def put(key, val)\n    # \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n    extend if load_factor > @load_thres\n    # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    index = find_bucket(key)\n    # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5f00\u8fd4\u56de\n    unless [nil, TOMBSTONE].include?(@buckets[index])\n      @buckets[index].val = val\n      return\n    end\n    # \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n    @buckets[index] = Pair.new(key, val)\n    @size += 1\n  end\n\n  ### \u5220\u9664\u64cd\u4f5c ###\n  def remove(key)\n    # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    index = find_bucket(key)\n    # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n    unless [nil, TOMBSTONE].include?(@buckets[index])\n      @buckets[index] = TOMBSTONE\n      @size -= 1\n    end\n  end\n\n  ### \u6269\u5bb9\u54c8\u5e0c\u8868 ###\n  def extend\n    # \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n    buckets_tmp = @buckets\n    # \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n    @capacity *= @extend_ratio\n    @buckets = Array.new(@capacity)\n    @size = 0\n    # \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n    for pair in buckets_tmp\n      put(pair.key, pair.val) unless [nil, TOMBSTONE].include?(pair)\n    end\n  end\n\n  ### \u6253\u5370\u54c8\u5e0c\u8868 ###\n  def print\n    for pair in @buckets\n      if pair.nil?\n        puts \"Nil\"\n      elsif pair == TOMBSTONE\n        puts \"TOMBSTONE\"\n      else\n        puts \"#{pair.key} -> #{pair.val}\"\n      end\n    end\n  end\nend\n
        "},{"location":"chapter_hashing/hash_collision/#2","title":"2. \u00a0 \u5e73\u65b9\u63a2\u6d4b","text":"

        \u5e73\u65b9\u63a2\u6d4b\u4e0e\u7ebf\u6027\u63a2\u6d4b\u7c7b\u4f3c\uff0c\u90fd\u662f\u5f00\u653e\u5bfb\u5740\u7684\u5e38\u89c1\u7b56\u7565\u4e4b\u4e00\u3002\u5f53\u53d1\u751f\u51b2\u7a81\u65f6\uff0c\u5e73\u65b9\u63a2\u6d4b\u4e0d\u662f\u7b80\u5355\u5730\u8df3\u8fc7\u4e00\u4e2a\u56fa\u5b9a\u7684\u6b65\u6570\uff0c\u800c\u662f\u8df3\u8fc7\u201c\u63a2\u6d4b\u6b21\u6570\u7684\u5e73\u65b9\u201d\u7684\u6b65\u6570\uff0c\u5373 \\(1, 4, 9, \\dots\\) \u6b65\u3002

        \u5e73\u65b9\u63a2\u6d4b\u4e3b\u8981\u5177\u6709\u4ee5\u4e0b\u4f18\u52bf\u3002

        • \u5e73\u65b9\u63a2\u6d4b\u901a\u8fc7\u8df3\u8fc7\u63a2\u6d4b\u6b21\u6570\u5e73\u65b9\u7684\u8ddd\u79bb\uff0c\u8bd5\u56fe\u7f13\u89e3\u7ebf\u6027\u63a2\u6d4b\u7684\u805a\u96c6\u6548\u5e94\u3002
        • \u5e73\u65b9\u63a2\u6d4b\u4f1a\u8df3\u8fc7\u66f4\u5927\u7684\u8ddd\u79bb\u6765\u5bfb\u627e\u7a7a\u4f4d\u7f6e\uff0c\u6709\u52a9\u4e8e\u6570\u636e\u5206\u5e03\u5f97\u66f4\u52a0\u5747\u5300\u3002

        \u7136\u800c\uff0c\u5e73\u65b9\u63a2\u6d4b\u5e76\u4e0d\u662f\u5b8c\u7f8e\u7684\u3002

        • \u4ecd\u7136\u5b58\u5728\u805a\u96c6\u73b0\u8c61\uff0c\u5373\u67d0\u4e9b\u4f4d\u7f6e\u6bd4\u5176\u4ed6\u4f4d\u7f6e\u66f4\u5bb9\u6613\u88ab\u5360\u7528\u3002
        • \u7531\u4e8e\u5e73\u65b9\u7684\u589e\u957f\uff0c\u5e73\u65b9\u63a2\u6d4b\u53ef\u80fd\u4e0d\u4f1a\u63a2\u6d4b\u6574\u4e2a\u54c8\u5e0c\u8868\uff0c\u8fd9\u610f\u5473\u7740\u5373\u4f7f\u54c8\u5e0c\u8868\u4e2d\u6709\u7a7a\u6876\uff0c\u5e73\u65b9\u63a2\u6d4b\u4e5f\u53ef\u80fd\u65e0\u6cd5\u8bbf\u95ee\u5230\u5b83\u3002
        "},{"location":"chapter_hashing/hash_collision/#3","title":"3. \u00a0 \u591a\u6b21\u54c8\u5e0c","text":"

        \u987e\u540d\u601d\u4e49\uff0c\u591a\u6b21\u54c8\u5e0c\u65b9\u6cd5\u4f7f\u7528\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570 \\(f_1(x)\\)\u3001\\(f_2(x)\\)\u3001\\(f_3(x)\\)\u3001\\(\\dots\\) \u8fdb\u884c\u63a2\u6d4b\u3002

        • \u63d2\u5165\u5143\u7d20\uff1a\u82e5\u54c8\u5e0c\u51fd\u6570 \\(f_1(x)\\) \u51fa\u73b0\u51b2\u7a81\uff0c\u5219\u5c1d\u8bd5 \\(f_2(x)\\) \uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u5230\u627e\u5230\u7a7a\u4f4d\u540e\u63d2\u5165\u5143\u7d20\u3002
        • \u67e5\u627e\u5143\u7d20\uff1a\u5728\u76f8\u540c\u7684\u54c8\u5e0c\u51fd\u6570\u987a\u5e8f\u4e0b\u8fdb\u884c\u67e5\u627e\uff0c\u76f4\u5230\u627e\u5230\u76ee\u6807\u5143\u7d20\u65f6\u8fd4\u56de\uff1b\u82e5\u9047\u5230\u7a7a\u4f4d\u6216\u5df2\u5c1d\u8bd5\u6240\u6709\u54c8\u5e0c\u51fd\u6570\uff0c\u8bf4\u660e\u54c8\u5e0c\u8868\u4e2d\u4e0d\u5b58\u5728\u8be5\u5143\u7d20\uff0c\u5219\u8fd4\u56de None \u3002

        \u4e0e\u7ebf\u6027\u63a2\u6d4b\u76f8\u6bd4\uff0c\u591a\u6b21\u54c8\u5e0c\u65b9\u6cd5\u4e0d\u6613\u4ea7\u751f\u805a\u96c6\uff0c\u4f46\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570\u4f1a\u5e26\u6765\u989d\u5916\u7684\u8ba1\u7b97\u91cf\u3002

        Tip

        \u8bf7\u6ce8\u610f\uff0c\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\u3001\u5e73\u65b9\u63a2\u6d4b\u548c\u591a\u6b21\u54c8\u5e0c\uff09\u54c8\u5e0c\u8868\u90fd\u5b58\u5728\u201c\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u201d\u7684\u95ee\u9898\u3002

        "},{"location":"chapter_hashing/hash_collision/#623","title":"6.2.3 \u00a0 \u7f16\u7a0b\u8bed\u8a00\u7684\u9009\u62e9","text":"

        \u5404\u79cd\u7f16\u7a0b\u8bed\u8a00\u91c7\u53d6\u4e86\u4e0d\u540c\u7684\u54c8\u5e0c\u8868\u5b9e\u73b0\u7b56\u7565\uff0c\u4e0b\u9762\u4e3e\u51e0\u4e2a\u4f8b\u5b50\u3002

        • Python \u91c7\u7528\u5f00\u653e\u5bfb\u5740\u3002\u5b57\u5178 dict \u4f7f\u7528\u4f2a\u968f\u673a\u6570\u8fdb\u884c\u63a2\u6d4b\u3002
        • Java \u91c7\u7528\u94fe\u5f0f\u5730\u5740\u3002\u81ea JDK 1.8 \u4ee5\u6765\uff0c\u5f53 HashMap \u5185\u6570\u7ec4\u957f\u5ea6\u8fbe\u5230 64 \u4e14\u94fe\u8868\u957f\u5ea6\u8fbe\u5230 8 \u65f6\uff0c\u94fe\u8868\u4f1a\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811\u4ee5\u63d0\u5347\u67e5\u627e\u6027\u80fd\u3002
        • Go \u91c7\u7528\u94fe\u5f0f\u5730\u5740\u3002Go \u89c4\u5b9a\u6bcf\u4e2a\u6876\u6700\u591a\u5b58\u50a8 8 \u4e2a\u952e\u503c\u5bf9\uff0c\u8d85\u51fa\u5bb9\u91cf\u5219\u8fde\u63a5\u4e00\u4e2a\u6ea2\u51fa\u6876\uff1b\u5f53\u6ea2\u51fa\u6876\u8fc7\u591a\u65f6\uff0c\u4f1a\u6267\u884c\u4e00\u6b21\u7279\u6b8a\u7684\u7b49\u91cf\u6269\u5bb9\u64cd\u4f5c\uff0c\u4ee5\u786e\u4fdd\u6027\u80fd\u3002
        "},{"location":"chapter_hashing/hash_map/","title":"6.1 \u00a0 \u54c8\u5e0c\u8868","text":"

        \u54c8\u5e0c\u8868\uff08hash table\uff09\uff0c\u53c8\u79f0\u6563\u5217\u8868\uff0c\u5b83\u901a\u8fc7\u5efa\u7acb\u952e key \u4e0e\u503c value \u4e4b\u95f4\u7684\u6620\u5c04\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u5143\u7d20\u67e5\u8be2\u3002\u5177\u4f53\u800c\u8a00\uff0c\u6211\u4eec\u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u4e00\u4e2a\u952e key \uff0c\u5219\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u83b7\u53d6\u5bf9\u5e94\u7684\u503c value \u3002

        \u5982\u56fe 6-1 \u6240\u793a\uff0c\u7ed9\u5b9a \\(n\\) \u4e2a\u5b66\u751f\uff0c\u6bcf\u4e2a\u5b66\u751f\u90fd\u6709\u201c\u59d3\u540d\u201d\u548c\u201c\u5b66\u53f7\u201d\u4e24\u9879\u6570\u636e\u3002\u5047\u5982\u6211\u4eec\u5e0c\u671b\u5b9e\u73b0\u201c\u8f93\u5165\u4e00\u4e2a\u5b66\u53f7\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u59d3\u540d\u201d\u7684\u67e5\u8be2\u529f\u80fd\uff0c\u5219\u53ef\u4ee5\u91c7\u7528\u56fe 6-1 \u6240\u793a\u7684\u54c8\u5e0c\u8868\u6765\u5b9e\u73b0\u3002

        \u56fe 6-1 \u00a0 \u54c8\u5e0c\u8868\u7684\u62bd\u8c61\u8868\u793a

        \u9664\u54c8\u5e0c\u8868\u5916\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u67e5\u8be2\u529f\u80fd\uff0c\u5b83\u4eec\u7684\u6548\u7387\u5bf9\u6bd4\u5982\u8868 6-1 \u6240\u793a\u3002

        • \u6dfb\u52a0\u5143\u7d20\uff1a\u4ec5\u9700\u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u6570\u7ec4\uff08\u94fe\u8868\uff09\u7684\u5c3e\u90e8\u5373\u53ef\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002
        • \u67e5\u8be2\u5143\u7d20\uff1a\u7531\u4e8e\u6570\u7ec4\uff08\u94fe\u8868\uff09\u662f\u4e71\u5e8f\u7684\uff0c\u56e0\u6b64\u9700\u8981\u904d\u5386\u5176\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002
        • \u5220\u9664\u5143\u7d20\uff1a\u9700\u8981\u5148\u67e5\u8be2\u5230\u5143\u7d20\uff0c\u518d\u4ece\u6570\u7ec4\uff08\u94fe\u8868\uff09\u4e2d\u5220\u9664\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002

        \u8868 6-1 \u00a0 \u5143\u7d20\u67e5\u8be2\u6548\u7387\u5bf9\u6bd4

        \u6570\u7ec4 \u94fe\u8868 \u54c8\u5e0c\u8868 \u67e5\u627e\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(1)\\) \u6dfb\u52a0\u5143\u7d20 \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(1)\\)

        \u89c2\u5bdf\u53d1\u73b0\uff0c\u5728\u54c8\u5e0c\u8868\u4e2d\u8fdb\u884c\u589e\u5220\u67e5\u6539\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f \\(O(1)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002

        "},{"location":"chapter_hashing/hash_map/#611","title":"6.1.1 \u00a0 \u54c8\u5e0c\u8868\u5e38\u7528\u64cd\u4f5c","text":"

        \u54c8\u5e0c\u8868\u7684\u5e38\u89c1\u64cd\u4f5c\u5305\u62ec\uff1a\u521d\u59cb\u5316\u3001\u67e5\u8be2\u64cd\u4f5c\u3001\u6dfb\u52a0\u952e\u503c\u5bf9\u548c\u5220\u9664\u952e\u503c\u5bf9\u7b49\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map.py
        # \u521d\u59cb\u5316\u54c8\u5e0c\u8868\nhmap: dict = {}\n\n# \u6dfb\u52a0\u64cd\u4f5c\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u5570\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9e2d\"\n\n# \u67e5\u8be2\u64cd\u4f5c\n# \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nname: str = hmap[15937]\n\n# \u5220\u9664\u64cd\u4f5c\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nhmap.pop(10583)\n
        hash_map.cpp
        /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nunordered_map<int, string> map;\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap[12836] = \"\u5c0f\u54c8\";\nmap[15937] = \"\u5c0f\u5570\";\nmap[16750] = \"\u5c0f\u7b97\";\nmap[13276] = \"\u5c0f\u6cd5\";\nmap[10583] = \"\u5c0f\u9e2d\";\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nstring name = map[15937];\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.erase(10583);\n
        hash_map.java
        /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nMap<Integer, String> map = new HashMap<>();\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.put(12836, \"\u5c0f\u54c8\");\nmap.put(15937, \"\u5c0f\u5570\");\nmap.put(16750, \"\u5c0f\u7b97\");\nmap.put(13276, \"\u5c0f\u6cd5\");\nmap.put(10583, \"\u5c0f\u9e2d\");\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nString name = map.get(15937);\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.remove(10583);\n
        hash_map.cs
        /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nDictionary<int, string> map = new() {\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    // \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\n    { 12836, \"\u5c0f\u54c8\" },\n    { 15937, \"\u5c0f\u5570\" },\n    { 16750, \"\u5c0f\u7b97\" },\n    { 13276, \"\u5c0f\u6cd5\" },\n    { 10583, \"\u5c0f\u9e2d\" }\n};\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nstring name = map[15937];\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.Remove(10583);\n
        hash_map_test.go
        /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nhmap := make(map[int]string)\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u5570\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9e2d\"\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nname := hmap[15937]\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\ndelete(hmap, 10583)\n
        hash_map.swift
        /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nvar map: [Int: String] = [:]\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap[12836] = \"\u5c0f\u54c8\"\nmap[15937] = \"\u5c0f\u5570\"\nmap[16750] = \"\u5c0f\u7b97\"\nmap[13276] = \"\u5c0f\u6cd5\"\nmap[10583] = \"\u5c0f\u9e2d\"\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet name = map[15937]!\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.removeValue(forKey: 10583)\n
        hash_map.js
        /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nconst map = new Map();\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.set(12836, '\u5c0f\u54c8');\nmap.set(15937, '\u5c0f\u5570');\nmap.set(16750, '\u5c0f\u7b97');\nmap.set(13276, '\u5c0f\u6cd5');\nmap.set(10583, '\u5c0f\u9e2d');\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet name = map.get(15937);\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.delete(10583);\n
        hash_map.ts
        /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nconst map = new Map<number, string>();\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.set(12836, '\u5c0f\u54c8');\nmap.set(15937, '\u5c0f\u5570');\nmap.set(16750, '\u5c0f\u7b97');\nmap.set(13276, '\u5c0f\u6cd5');\nmap.set(10583, '\u5c0f\u9e2d');\nconsole.info('\\n\u6dfb\u52a0\u5b8c\u6210\u540e\uff0c\u54c8\u5e0c\u8868\u4e3a\\nKey -> Value');\nconsole.info(map);\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet name = map.get(15937);\nconsole.info('\\n\u8f93\u5165\u5b66\u53f7 15937 \uff0c\u67e5\u8be2\u5230\u59d3\u540d ' + name);\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.delete(10583);\nconsole.info('\\n\u5220\u9664 10583 \u540e\uff0c\u54c8\u5e0c\u8868\u4e3a\\nKey -> Value');\nconsole.info(map);\n
        hash_map.dart
        /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nMap<int, String> map = {};\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap[12836] = \"\u5c0f\u54c8\";\nmap[15937] = \"\u5c0f\u5570\";\nmap[16750] = \"\u5c0f\u7b97\";\nmap[13276] = \"\u5c0f\u6cd5\";\nmap[10583] = \"\u5c0f\u9e2d\";\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nString name = map[15937];\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.remove(10583);\n
        hash_map.rs
        use std::collections::HashMap;\n\n/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nlet mut map: HashMap<i32, String> = HashMap::new();\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.insert(12836, \"\u5c0f\u54c8\".to_string());\nmap.insert(15937, \"\u5c0f\u5570\".to_string());\nmap.insert(16750, \"\u5c0f\u7b97\".to_string());\nmap.insert(13279, \"\u5c0f\u6cd5\".to_string());\nmap.insert(10583, \"\u5c0f\u9e2d\".to_string());\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet _name: Option<&String> = map.get(&15937);\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nlet _removed_value: Option<String> = map.remove(&10583);\n
        hash_map.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u8868\n
        hash_map.kt
        /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nval map = HashMap<Int,String>()\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap[12836] = \"\u5c0f\u54c8\"\nmap[15937] = \"\u5c0f\u5570\"\nmap[16750] = \"\u5c0f\u7b97\"\nmap[13276] = \"\u5c0f\u6cd5\"\nmap[10583] = \"\u5c0f\u9e2d\"\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nval name = map[15937]\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.remove(10583)\n
        hash_map.rb
        # \u521d\u59cb\u5316\u54c8\u5e0c\u8868\nhmap = {}\n\n# \u6dfb\u52a0\u64cd\u4f5c\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u5570\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9e2d\"\n\n# \u67e5\u8be2\u64cd\u4f5c\n# \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nname = hmap[15937]\n\n# \u5220\u9664\u64cd\u4f5c\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nhmap.delete(10583)\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u54c8\u5e0c\u8868\u6709\u4e09\u79cd\u5e38\u7528\u7684\u904d\u5386\u65b9\u5f0f\uff1a\u904d\u5386\u952e\u503c\u5bf9\u3001\u904d\u5386\u952e\u548c\u904d\u5386\u503c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map.py
        # \u904d\u5386\u54c8\u5e0c\u8868\n# \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor key, value in hmap.items():\n    print(key, \"->\", value)\n# \u5355\u72ec\u904d\u5386\u952e key\nfor key in hmap.keys():\n    print(key)\n# \u5355\u72ec\u904d\u5386\u503c value\nfor value in hmap.values():\n    print(value)\n
        hash_map.cpp
        /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor (auto kv: map) {\n    cout << kv.first << \" -> \" << kv.second << endl;\n}\n// \u4f7f\u7528\u8fed\u4ee3\u5668\u904d\u5386 key->value\nfor (auto iter = map.begin(); iter != map.end(); iter++) {\n    cout << iter->first << \"->\" << iter->second << endl;\n}\n
        hash_map.java
        /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor (Map.Entry <Integer, String> kv: map.entrySet()) {\n    System.out.println(kv.getKey() + \" -> \" + kv.getValue());\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor (int key: map.keySet()) {\n    System.out.println(key);\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor (String val: map.values()) {\n    System.out.println(val);\n}\n
        hash_map.cs
        /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nforeach (var kv in map) {\n    Console.WriteLine(kv.Key + \" -> \" + kv.Value);\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nforeach (int key in map.Keys) {\n    Console.WriteLine(key);\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nforeach (string val in map.Values) {\n    Console.WriteLine(val);\n}\n
        hash_map_test.go
        /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor key, value := range hmap {\n    fmt.Println(key, \"->\", value)\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor key := range hmap {\n    fmt.Println(key)\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor _, value := range hmap {\n    fmt.Println(value)\n}\n
        hash_map.swift
        /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nfor (key, value) in map {\n    print(\"\\(key) -> \\(value)\")\n}\n// \u5355\u72ec\u904d\u5386\u952e Key\nfor key in map.keys {\n    print(key)\n}\n// \u5355\u72ec\u904d\u5386\u503c Value\nfor value in map.values {\n    print(value)\n}\n
        hash_map.js
        /* \u904d\u5386\u54c8\u5e0c\u8868 */\nconsole.info('\\n\u904d\u5386\u952e\u503c\u5bf9 Key->Value');\nfor (const [k, v] of map.entries()) {\n    console.info(k + ' -> ' + v);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u952e Key');\nfor (const k of map.keys()) {\n    console.info(k);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u503c Value');\nfor (const v of map.values()) {\n    console.info(v);\n}\n
        hash_map.ts
        /* \u904d\u5386\u54c8\u5e0c\u8868 */\nconsole.info('\\n\u904d\u5386\u952e\u503c\u5bf9 Key->Value');\nfor (const [k, v] of map.entries()) {\n    console.info(k + ' -> ' + v);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u952e Key');\nfor (const k of map.keys()) {\n    console.info(k);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u503c Value');\nfor (const v of map.values()) {\n    console.info(v);\n}\n
        hash_map.dart
        /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nmap.forEach((key, value) {\n  print('$key -> $value');\n});\n\n// \u5355\u72ec\u904d\u5386\u952e Key\nmap.keys.forEach((key) {\n  print(key);\n});\n\n// \u5355\u72ec\u904d\u5386\u503c Value\nmap.values.forEach((value) {\n  print(value);\n});\n
        hash_map.rs
        /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nfor (key, value) in &map {\n    println!(\"{key} -> {value}\");\n}\n\n// \u5355\u72ec\u904d\u5386\u952e Key\nfor key in map.keys() {\n    println!(\"{key}\");\n}\n\n// \u5355\u72ec\u904d\u5386\u503c Value\nfor value in map.values() {\n    println!(\"{value}\");\n}\n
        hash_map.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u8868\n
        hash_map.kt
        /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor ((key, value) in map) {\n    println(\"$key -> $value\")\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor (key in map.keys) {\n    println(key)\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor (_val in map.values) {\n    println(_val)\n}\n
        hash_map.rb
        # \u904d\u5386\u54c8\u5e0c\u8868\n# \u904d\u5386\u952e\u503c\u5bf9 key->value\nhmap.entries.each { |key, value| puts \"#{key} -> #{value}\" }\n\n# \u5355\u72ec\u904d\u5386\u952e key\nhmap.keys.each { |key| puts key }\n\n# \u5355\u72ec\u904d\u5386\u503c value\nhmap.values.each { |val| puts val }\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_hashing/hash_map/#612","title":"6.1.2 \u00a0 \u54c8\u5e0c\u8868\u7b80\u5355\u5b9e\u73b0","text":"

        \u6211\u4eec\u5148\u8003\u8651\u6700\u7b80\u5355\u7684\u60c5\u51b5\uff0c\u4ec5\u7528\u4e00\u4e2a\u6570\u7ec4\u6765\u5b9e\u73b0\u54c8\u5e0c\u8868\u3002\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u6211\u4eec\u5c06\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u7a7a\u4f4d\u79f0\u4e3a\u6876\uff08bucket\uff09\uff0c\u6bcf\u4e2a\u6876\u53ef\u5b58\u50a8\u4e00\u4e2a\u952e\u503c\u5bf9\u3002\u56e0\u6b64\uff0c\u67e5\u8be2\u64cd\u4f5c\u5c31\u662f\u627e\u5230 key \u5bf9\u5e94\u7684\u6876\uff0c\u5e76\u5728\u6876\u4e2d\u83b7\u53d6 value \u3002

        \u90a3\u4e48\uff0c\u5982\u4f55\u57fa\u4e8e key \u5b9a\u4f4d\u5bf9\u5e94\u7684\u6876\u5462\uff1f\u8fd9\u662f\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\uff08hash function\uff09\u5b9e\u73b0\u7684\u3002\u54c8\u5e0c\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5c06\u4e00\u4e2a\u8f83\u5927\u7684\u8f93\u5165\u7a7a\u95f4\u6620\u5c04\u5230\u4e00\u4e2a\u8f83\u5c0f\u7684\u8f93\u51fa\u7a7a\u95f4\u3002\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u8f93\u5165\u7a7a\u95f4\u662f\u6240\u6709 key \uff0c\u8f93\u51fa\u7a7a\u95f4\u662f\u6240\u6709\u6876\uff08\u6570\u7ec4\u7d22\u5f15\uff09\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u8f93\u5165\u4e00\u4e2a key \uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u5f97\u5230\u8be5 key \u5bf9\u5e94\u7684\u952e\u503c\u5bf9\u5728\u6570\u7ec4\u4e2d\u7684\u5b58\u50a8\u4f4d\u7f6e\u3002

        \u8f93\u5165\u4e00\u4e2a key \uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u8ba1\u7b97\u8fc7\u7a0b\u5206\u4e3a\u4ee5\u4e0b\u4e24\u6b65\u3002

        1. \u901a\u8fc7\u67d0\u79cd\u54c8\u5e0c\u7b97\u6cd5 hash() \u8ba1\u7b97\u5f97\u5230\u54c8\u5e0c\u503c\u3002
        2. \u5c06\u54c8\u5e0c\u503c\u5bf9\u6876\u6570\u91cf\uff08\u6570\u7ec4\u957f\u5ea6\uff09capacity \u53d6\u6a21\uff0c\u4ece\u800c\u83b7\u53d6\u8be5 key \u5bf9\u5e94\u7684\u6876\uff08\u6570\u7ec4\u7d22\u5f15\uff09index \u3002
        index = hash(key) % capacity\n

        \u968f\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5229\u7528 index \u5728\u54c8\u5e0c\u8868\u4e2d\u8bbf\u95ee\u5bf9\u5e94\u7684\u6876\uff0c\u4ece\u800c\u83b7\u53d6 value \u3002

        \u8bbe\u6570\u7ec4\u957f\u5ea6 capacity = 100\u3001\u54c8\u5e0c\u7b97\u6cd5 hash(key) = key \uff0c\u6613\u5f97\u54c8\u5e0c\u51fd\u6570\u4e3a key % 100 \u3002\u56fe 6-2 \u4ee5 key \u5b66\u53f7\u548c value \u59d3\u540d\u4e3a\u4f8b\uff0c\u5c55\u793a\u4e86\u54c8\u5e0c\u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406\u3002

        \u56fe 6-2 \u00a0 \u54c8\u5e0c\u51fd\u6570\u5de5\u4f5c\u539f\u7406

        \u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u54c8\u5e0c\u8868\u3002\u5176\u4e2d\uff0c\u6211\u4eec\u5c06 key \u548c value \u5c01\u88c5\u6210\u4e00\u4e2a\u7c7b Pair \uff0c\u4ee5\u8868\u793a\u952e\u503c\u5bf9\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_hash_map.py
        class Pair:\n    \"\"\"\u952e\u503c\u5bf9\"\"\"\n\n    def __init__(self, key: int, val: str):\n        self.key = key\n        self.val = val\n\nclass ArrayHashMap:\n    \"\"\"\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\"\"\"\n\n    def __init__(self):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        # \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n        self.buckets: list[Pair | None] = [None] * 100\n\n    def hash_func(self, key: int) -> int:\n        \"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\n        index = key % 100\n        return index\n\n    def get(self, key: int) -> str | None:\n        \"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\n        index: int = self.hash_func(key)\n        pair: Pair = self.buckets[index]\n        if pair is None:\n            return None\n        return pair.val\n\n    def put(self, key: int, val: str):\n        \"\"\"\u6dfb\u52a0\u548c\u66f4\u65b0\u64cd\u4f5c\"\"\"\n        pair = Pair(key, val)\n        index: int = self.hash_func(key)\n        self.buckets[index] = pair\n\n    def remove(self, key: int):\n        \"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\n        index: int = self.hash_func(key)\n        # \u7f6e\u4e3a None \uff0c\u4ee3\u8868\u5220\u9664\n        self.buckets[index] = None\n\n    def entry_set(self) -> list[Pair]:\n        \"\"\"\u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9\"\"\"\n        result: list[Pair] = []\n        for pair in self.buckets:\n            if pair is not None:\n                result.append(pair)\n        return result\n\n    def key_set(self) -> list[int]:\n        \"\"\"\u83b7\u53d6\u6240\u6709\u952e\"\"\"\n        result = []\n        for pair in self.buckets:\n            if pair is not None:\n                result.append(pair.key)\n        return result\n\n    def value_set(self) -> list[str]:\n        \"\"\"\u83b7\u53d6\u6240\u6709\u503c\"\"\"\n        result = []\n        for pair in self.buckets:\n            if pair is not None:\n                result.append(pair.val)\n        return result\n\n    def print(self):\n        \"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\n        for pair in self.buckets:\n            if pair is not None:\n                print(pair.key, \"->\", pair.val)\n
        array_hash_map.cpp
        /* \u952e\u503c\u5bf9 */\nstruct Pair {\n  public:\n    int key;\n    string val;\n    Pair(int key, string val) {\n        this->key = key;\n        this->val = val;\n    }\n};\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n  private:\n    vector<Pair *> buckets;\n\n  public:\n    ArrayHashMap() {\n        // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n        buckets = vector<Pair *>(100);\n    }\n\n    ~ArrayHashMap() {\n        // \u91ca\u653e\u5185\u5b58\n        for (const auto &bucket : buckets) {\n            delete bucket;\n        }\n        buckets.clear();\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    int hashFunc(int key) {\n        int index = key % 100;\n        return index;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    string get(int key) {\n        int index = hashFunc(key);\n        Pair *pair = buckets[index];\n        if (pair == nullptr)\n            return \"\";\n        return pair->val;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    void put(int key, string val) {\n        Pair *pair = new Pair(key, val);\n        int index = hashFunc(key);\n        buckets[index] = pair;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    void remove(int key) {\n        int index = hashFunc(key);\n        // \u91ca\u653e\u5185\u5b58\u5e76\u7f6e\u4e3a nullptr\n        delete buckets[index];\n        buckets[index] = nullptr;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n    vector<Pair *> pairSet() {\n        vector<Pair *> pairSet;\n        for (Pair *pair : buckets) {\n            if (pair != nullptr) {\n                pairSet.push_back(pair);\n            }\n        }\n        return pairSet;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e */\n    vector<int> keySet() {\n        vector<int> keySet;\n        for (Pair *pair : buckets) {\n            if (pair != nullptr) {\n                keySet.push_back(pair->key);\n            }\n        }\n        return keySet;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u503c */\n    vector<string> valueSet() {\n        vector<string> valueSet;\n        for (Pair *pair : buckets) {\n            if (pair != nullptr) {\n                valueSet.push_back(pair->val);\n            }\n        }\n        return valueSet;\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    void print() {\n        for (Pair *kv : pairSet()) {\n            cout << kv->key << \" -> \" << kv->val << endl;\n        }\n    }\n};\n
        array_hash_map.java
        /* \u952e\u503c\u5bf9 */\nclass Pair {\n    public int key;\n    public String val;\n\n    public Pair(int key, String val) {\n        this.key = key;\n        this.val = val;\n    }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n    private List<Pair> buckets;\n\n    public ArrayHashMap() {\n        // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n        buckets = new ArrayList<>();\n        for (int i = 0; i < 100; i++) {\n            buckets.add(null);\n        }\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    private int hashFunc(int key) {\n        int index = key % 100;\n        return index;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    public String get(int key) {\n        int index = hashFunc(key);\n        Pair pair = buckets.get(index);\n        if (pair == null)\n            return null;\n        return pair.val;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    public void put(int key, String val) {\n        Pair pair = new Pair(key, val);\n        int index = hashFunc(key);\n        buckets.set(index, pair);\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    public void remove(int key) {\n        int index = hashFunc(key);\n        // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n        buckets.set(index, null);\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n    public List<Pair> pairSet() {\n        List<Pair> pairSet = new ArrayList<>();\n        for (Pair pair : buckets) {\n            if (pair != null)\n                pairSet.add(pair);\n        }\n        return pairSet;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e */\n    public List<Integer> keySet() {\n        List<Integer> keySet = new ArrayList<>();\n        for (Pair pair : buckets) {\n            if (pair != null)\n                keySet.add(pair.key);\n        }\n        return keySet;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u503c */\n    public List<String> valueSet() {\n        List<String> valueSet = new ArrayList<>();\n        for (Pair pair : buckets) {\n            if (pair != null)\n                valueSet.add(pair.val);\n        }\n        return valueSet;\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    public void print() {\n        for (Pair kv : pairSet()) {\n            System.out.println(kv.key + \" -> \" + kv.val);\n        }\n    }\n}\n
        array_hash_map.cs
        /* \u952e\u503c\u5bf9 int->string */\nclass Pair(int key, string val) {\n    public int key = key;\n    public string val = val;\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n    List<Pair?> buckets;\n    public ArrayHashMap() {\n        // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n        buckets = [];\n        for (int i = 0; i < 100; i++) {\n            buckets.Add(null);\n        }\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    int HashFunc(int key) {\n        int index = key % 100;\n        return index;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    public string? Get(int key) {\n        int index = HashFunc(key);\n        Pair? pair = buckets[index];\n        if (pair == null) return null;\n        return pair.val;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    public void Put(int key, string val) {\n        Pair pair = new(key, val);\n        int index = HashFunc(key);\n        buckets[index] = pair;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    public void Remove(int key) {\n        int index = HashFunc(key);\n        // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n        buckets[index] = null;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n    public List<Pair> PairSet() {\n        List<Pair> pairSet = [];\n        foreach (Pair? pair in buckets) {\n            if (pair != null)\n                pairSet.Add(pair);\n        }\n        return pairSet;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e */\n    public List<int> KeySet() {\n        List<int> keySet = [];\n        foreach (Pair? pair in buckets) {\n            if (pair != null)\n                keySet.Add(pair.key);\n        }\n        return keySet;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u503c */\n    public List<string> ValueSet() {\n        List<string> valueSet = [];\n        foreach (Pair? pair in buckets) {\n            if (pair != null)\n                valueSet.Add(pair.val);\n        }\n        return valueSet;\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    public void Print() {\n        foreach (Pair kv in PairSet()) {\n            Console.WriteLine(kv.key + \" -> \" + kv.val);\n        }\n    }\n}\n
        array_hash_map.go
        /* \u952e\u503c\u5bf9 */\ntype pair struct {\n    key int\n    val string\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\ntype arrayHashMap struct {\n    buckets []*pair\n}\n\n/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nfunc newArrayHashMap() *arrayHashMap {\n    // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n    buckets := make([]*pair, 100)\n    return &arrayHashMap{buckets: buckets}\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (a *arrayHashMap) hashFunc(key int) int {\n    index := key % 100\n    return index\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (a *arrayHashMap) get(key int) string {\n    index := a.hashFunc(key)\n    pair := a.buckets[index]\n    if pair == nil {\n        return \"Not Found\"\n    }\n    return pair.val\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (a *arrayHashMap) put(key int, val string) {\n    pair := &pair{key: key, val: val}\n    index := a.hashFunc(key)\n    a.buckets[index] = pair\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (a *arrayHashMap) remove(key int) {\n    index := a.hashFunc(key)\n    // \u7f6e\u4e3a nil \uff0c\u4ee3\u8868\u5220\u9664\n    a.buckets[index] = nil\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e\u5bf9 */\nfunc (a *arrayHashMap) pairSet() []*pair {\n    var pairs []*pair\n    for _, pair := range a.buckets {\n        if pair != nil {\n            pairs = append(pairs, pair)\n        }\n    }\n    return pairs\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e */\nfunc (a *arrayHashMap) keySet() []int {\n    var keys []int\n    for _, pair := range a.buckets {\n        if pair != nil {\n            keys = append(keys, pair.key)\n        }\n    }\n    return keys\n}\n\n/* \u83b7\u53d6\u6240\u6709\u503c */\nfunc (a *arrayHashMap) valueSet() []string {\n    var values []string\n    for _, pair := range a.buckets {\n        if pair != nil {\n            values = append(values, pair.val)\n        }\n    }\n    return values\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (a *arrayHashMap) print() {\n    for _, pair := range a.buckets {\n        if pair != nil {\n            fmt.Println(pair.key, \"->\", pair.val)\n        }\n    }\n}\n
        array_hash_map.swift
        /* \u952e\u503c\u5bf9 */\nclass Pair: Equatable {\n    public var key: Int\n    public var val: String\n\n    public init(key: Int, val: String) {\n        self.key = key\n        self.val = val\n    }\n\n    public static func == (lhs: Pair, rhs: Pair) -> Bool {\n        lhs.key == rhs.key && lhs.val == rhs.val\n    }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n    private var buckets: [Pair?]\n\n    init() {\n        // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n        buckets = Array(repeating: nil, count: 100)\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    private func hashFunc(key: Int) -> Int {\n        let index = key % 100\n        return index\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    func get(key: Int) -> String? {\n        let index = hashFunc(key: key)\n        let pair = buckets[index]\n        return pair?.val\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    func put(key: Int, val: String) {\n        let pair = Pair(key: key, val: val)\n        let index = hashFunc(key: key)\n        buckets[index] = pair\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    func remove(key: Int) {\n        let index = hashFunc(key: key)\n        // \u7f6e\u4e3a nil \uff0c\u4ee3\u8868\u5220\u9664\n        buckets[index] = nil\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n    func pairSet() -> [Pair] {\n        buckets.compactMap { $0 }\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e */\n    func keySet() -> [Int] {\n        buckets.compactMap { $0?.key }\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u503c */\n    func valueSet() -> [String] {\n        buckets.compactMap { $0?.val }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    func print() {\n        for pair in pairSet() {\n            Swift.print(\"\\(pair.key) -> \\(pair.val)\")\n        }\n    }\n}\n
        array_hash_map.js
        /* \u952e\u503c\u5bf9 Number -> String */\nclass Pair {\n    constructor(key, val) {\n        this.key = key;\n        this.val = val;\n    }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n    #buckets;\n    constructor() {\n        // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n        this.#buckets = new Array(100).fill(null);\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    #hashFunc(key) {\n        return key % 100;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    get(key) {\n        let index = this.#hashFunc(key);\n        let pair = this.#buckets[index];\n        if (pair === null) return null;\n        return pair.val;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    set(key, val) {\n        let index = this.#hashFunc(key);\n        this.#buckets[index] = new Pair(key, val);\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    delete(key) {\n        let index = this.#hashFunc(key);\n        // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n        this.#buckets[index] = null;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n    entries() {\n        let arr = [];\n        for (let i = 0; i < this.#buckets.length; i++) {\n            if (this.#buckets[i]) {\n                arr.push(this.#buckets[i]);\n            }\n        }\n        return arr;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e */\n    keys() {\n        let arr = [];\n        for (let i = 0; i < this.#buckets.length; i++) {\n            if (this.#buckets[i]) {\n                arr.push(this.#buckets[i].key);\n            }\n        }\n        return arr;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u503c */\n    values() {\n        let arr = [];\n        for (let i = 0; i < this.#buckets.length; i++) {\n            if (this.#buckets[i]) {\n                arr.push(this.#buckets[i].val);\n            }\n        }\n        return arr;\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    print() {\n        let pairSet = this.entries();\n        for (const pair of pairSet) {\n            console.info(`${pair.key} -> ${pair.val}`);\n        }\n    }\n}\n
        array_hash_map.ts
        /* \u952e\u503c\u5bf9 Number -> String */\nclass Pair {\n    public key: number;\n    public val: string;\n\n    constructor(key: number, val: string) {\n        this.key = key;\n        this.val = val;\n    }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n    private readonly buckets: (Pair | null)[];\n\n    constructor() {\n        // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n        this.buckets = new Array(100).fill(null);\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    private hashFunc(key: number): number {\n        return key % 100;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    public get(key: number): string | null {\n        let index = this.hashFunc(key);\n        let pair = this.buckets[index];\n        if (pair === null) return null;\n        return pair.val;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    public set(key: number, val: string) {\n        let index = this.hashFunc(key);\n        this.buckets[index] = new Pair(key, val);\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    public delete(key: number) {\n        let index = this.hashFunc(key);\n        // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n        this.buckets[index] = null;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n    public entries(): (Pair | null)[] {\n        let arr: (Pair | null)[] = [];\n        for (let i = 0; i < this.buckets.length; i++) {\n            if (this.buckets[i]) {\n                arr.push(this.buckets[i]);\n            }\n        }\n        return arr;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e */\n    public keys(): (number | undefined)[] {\n        let arr: (number | undefined)[] = [];\n        for (let i = 0; i < this.buckets.length; i++) {\n            if (this.buckets[i]) {\n                arr.push(this.buckets[i].key);\n            }\n        }\n        return arr;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u503c */\n    public values(): (string | undefined)[] {\n        let arr: (string | undefined)[] = [];\n        for (let i = 0; i < this.buckets.length; i++) {\n            if (this.buckets[i]) {\n                arr.push(this.buckets[i].val);\n            }\n        }\n        return arr;\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    public print() {\n        let pairSet = this.entries();\n        for (const pair of pairSet) {\n            console.info(`${pair.key} -> ${pair.val}`);\n        }\n    }\n}\n
        array_hash_map.dart
        /* \u952e\u503c\u5bf9 */\nclass Pair {\n  int key;\n  String val;\n  Pair(this.key, this.val);\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n  late List<Pair?> _buckets;\n\n  ArrayHashMap() {\n    // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n    _buckets = List.filled(100, null);\n  }\n\n  /* \u54c8\u5e0c\u51fd\u6570 */\n  int _hashFunc(int key) {\n    final int index = key % 100;\n    return index;\n  }\n\n  /* \u67e5\u8be2\u64cd\u4f5c */\n  String? get(int key) {\n    final int index = _hashFunc(key);\n    final Pair? pair = _buckets[index];\n    if (pair == null) {\n      return null;\n    }\n    return pair.val;\n  }\n\n  /* \u6dfb\u52a0\u64cd\u4f5c */\n  void put(int key, String val) {\n    final Pair pair = Pair(key, val);\n    final int index = _hashFunc(key);\n    _buckets[index] = pair;\n  }\n\n  /* \u5220\u9664\u64cd\u4f5c */\n  void remove(int key) {\n    final int index = _hashFunc(key);\n    _buckets[index] = null;\n  }\n\n  /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n  List<Pair> pairSet() {\n    List<Pair> pairSet = [];\n    for (final Pair? pair in _buckets) {\n      if (pair != null) {\n        pairSet.add(pair);\n      }\n    }\n    return pairSet;\n  }\n\n  /* \u83b7\u53d6\u6240\u6709\u952e */\n  List<int> keySet() {\n    List<int> keySet = [];\n    for (final Pair? pair in _buckets) {\n      if (pair != null) {\n        keySet.add(pair.key);\n      }\n    }\n    return keySet;\n  }\n\n  /* \u83b7\u53d6\u6240\u6709\u503c */\n  List<String> values() {\n    List<String> valueSet = [];\n    for (final Pair? pair in _buckets) {\n      if (pair != null) {\n        valueSet.add(pair.val);\n      }\n    }\n    return valueSet;\n  }\n\n  /* \u6253\u5370\u54c8\u5e0c\u8868 */\n  void printHashMap() {\n    for (final Pair kv in pairSet()) {\n      print(\"${kv.key} -> ${kv.val}\");\n    }\n  }\n}\n
        array_hash_map.rs
        /* \u952e\u503c\u5bf9 */\n#[derive(Debug, Clone, PartialEq)]\npub struct Pair {\n    pub key: i32,\n    pub val: String,\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\npub struct ArrayHashMap {\n    buckets: Vec<Option<Pair>>,\n}\n\nimpl ArrayHashMap {\n    pub fn new() -> ArrayHashMap {\n        // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n        Self {\n            buckets: vec![None; 100],\n        }\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    fn hash_func(&self, key: i32) -> usize {\n        key as usize % 100\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    pub fn get(&self, key: i32) -> Option<&String> {\n        let index = self.hash_func(key);\n        self.buckets[index].as_ref().map(|pair| &pair.val)\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    pub fn put(&mut self, key: i32, val: &str) {\n        let index = self.hash_func(key);\n        self.buckets[index] = Some(Pair {\n            key,\n            val: val.to_string(),\n        });\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    pub fn remove(&mut self, key: i32) {\n        let index = self.hash_func(key);\n        // \u7f6e\u4e3a None \uff0c\u4ee3\u8868\u5220\u9664\n        self.buckets[index] = None;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n    pub fn entry_set(&self) -> Vec<&Pair> {\n        self.buckets\n            .iter()\n            .filter_map(|pair| pair.as_ref())\n            .collect()\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e */\n    pub fn key_set(&self) -> Vec<&i32> {\n        self.buckets\n            .iter()\n            .filter_map(|pair| pair.as_ref().map(|pair| &pair.key))\n            .collect()\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u503c */\n    pub fn value_set(&self) -> Vec<&String> {\n        self.buckets\n            .iter()\n            .filter_map(|pair| pair.as_ref().map(|pair| &pair.val))\n            .collect()\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    pub fn print(&self) {\n        for pair in self.entry_set() {\n            println!(\"{} -> {}\", pair.key, pair.val);\n        }\n    }\n}\n
        array_hash_map.c
        /* \u952e\u503c\u5bf9 int->string */\ntypedef struct {\n    int key;\n    char *val;\n} Pair;\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\ntypedef struct {\n    Pair *buckets[MAX_SIZE];\n} ArrayHashMap;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayHashMap *newArrayHashMap() {\n    ArrayHashMap *hmap = malloc(sizeof(ArrayHashMap));\n    for (int i=0; i < MAX_SIZE; i++) {\n        hmap->buckets[i] = NULL;\n    }\n    return hmap;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayHashMap(ArrayHashMap *hmap) {\n    for (int i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            free(hmap->buckets[i]->val);\n            free(hmap->buckets[i]);\n        }\n    }\n    free(hmap);\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(ArrayHashMap *hmap, const int key, const char *val) {\n    Pair *Pair = malloc(sizeof(Pair));\n    Pair->key = key;\n    Pair->val = malloc(strlen(val) + 1);\n    strcpy(Pair->val, val);\n\n    int index = hashFunc(key);\n    hmap->buckets[index] = Pair;\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeItem(ArrayHashMap *hmap, const int key) {\n    int index = hashFunc(key);\n    free(hmap->buckets[index]->val);\n    free(hmap->buckets[index]);\n    hmap->buckets[index] = NULL;\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nvoid pairSet(ArrayHashMap *hmap, MapSet *set) {\n    Pair *entries;\n    int i = 0, index = 0;\n    int total = 0;\n    /* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            total++;\n        }\n    }\n    entries = malloc(sizeof(Pair) * total);\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            entries[index].key = hmap->buckets[i]->key;\n            entries[index].val = malloc(strlen(hmap->buckets[i]->val) + 1);\n            strcpy(entries[index].val, hmap->buckets[i]->val);\n            index++;\n        }\n    }\n    set->set = entries;\n    set->len = total;\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e */\nvoid keySet(ArrayHashMap *hmap, MapSet *set) {\n    int *keys;\n    int i = 0, index = 0;\n    int total = 0;\n    /* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            total++;\n        }\n    }\n    keys = malloc(total * sizeof(int));\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            keys[index] = hmap->buckets[i]->key;\n            index++;\n        }\n    }\n    set->set = keys;\n    set->len = total;\n}\n\n/* \u83b7\u53d6\u6240\u6709\u503c */\nvoid valueSet(ArrayHashMap *hmap, MapSet *set) {\n    char **vals;\n    int i = 0, index = 0;\n    int total = 0;\n    /* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            total++;\n        }\n    }\n    vals = malloc(total * sizeof(char *));\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            vals[index] = hmap->buckets[i]->val;\n            index++;\n        }\n    }\n    set->set = vals;\n    set->len = total;\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(ArrayHashMap *hmap) {\n    int i;\n    MapSet set;\n    pairSet(hmap, &set);\n    Pair *entries = (Pair *)set.set;\n    for (i = 0; i < set.len; i++) {\n        printf(\"%d -> %s\\n\", entries[i].key, entries[i].val);\n    }\n    free(set.set);\n}\n
        array_hash_map.kt
        /* \u952e\u503c\u5bf9 */\nclass Pair(\n    var key: Int,\n    var _val: String\n)\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n    // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n    private val buckets = arrayOfNulls<Pair>(100)\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    fun hashFunc(key: Int): Int {\n        val index = key % 100\n        return index\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    fun get(key: Int): String? {\n        val index = hashFunc(key)\n        val pair = buckets[index] ?: return null\n        return pair._val\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    fun put(key: Int, _val: String) {\n        val pair = Pair(key, _val)\n        val index = hashFunc(key)\n        buckets[index] = pair\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    fun remove(key: Int) {\n        val index = hashFunc(key)\n        // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n        buckets[index] = null\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n    fun pairSet(): MutableList<Pair> {\n        val pairSet = mutableListOf<Pair>()\n        for (pair in buckets) {\n            if (pair != null)\n                pairSet.add(pair)\n        }\n        return pairSet\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e */\n    fun keySet(): MutableList<Int> {\n        val keySet = mutableListOf<Int>()\n        for (pair in buckets) {\n            if (pair != null)\n                keySet.add(pair.key)\n        }\n        return keySet\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u503c */\n    fun valueSet(): MutableList<String> {\n        val valueSet = mutableListOf<String>()\n        for (pair in buckets) {\n            if (pair != null)\n                valueSet.add(pair._val)\n        }\n        return valueSet\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    fun print() {\n        for (kv in pairSet()) {\n            val key = kv.key\n            val _val = kv._val\n            println(\"$key -> $_val\")\n        }\n    }\n}\n
        array_hash_map.rb
        ### \u952e\u503c\u5bf9 ###\nclass Pair\n  attr_accessor :key, :val\n\n  def initialize(key, val)\n    @key = key\n    @val = val\n  end\nend\n\n### \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 ###\nclass ArrayHashMap\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize\n    # \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n    @buckets = Array.new(100)\n  end\n\n  ### \u54c8\u5e0c\u51fd\u6570 ###\n  def hash_func(key)\n    index = key % 100\n  end\n\n  ### \u67e5\u8be2\u64cd\u4f5c ###\n  def get(key)\n    index = hash_func(key)\n    pair = @buckets[index]\n\n    return if pair.nil?\n    pair.val\n  end\n\n  ### \u6dfb\u52a0\u64cd\u4f5c ###\n  def put(key, val)\n    pair = Pair.new(key, val)\n    index = hash_func(key)\n    @buckets[index] = pair\n  end\n\n  ### \u5220\u9664\u64cd\u4f5c ###\n  def remove(key)\n    index = hash_func(key)\n    # \u7f6e\u4e3a nil \uff0c\u4ee3\u8868\u5220\u9664\n    @buckets[index] = nil\n  end\n\n  ### \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 ###\n  def entry_set\n    result = []\n    @buckets.each { |pair| result << pair unless pair.nil? }\n    result\n  end\n\n  ### \u83b7\u53d6\u6240\u6709\u952e ###\n  def key_set\n    result = []\n    @buckets.each { |pair| result << pair.key unless pair.nil? }\n    result\n  end\n\n  ### \u83b7\u53d6\u6240\u6709\u503c ###\n  def value_set\n    result = []\n    @buckets.each { |pair| result << pair.val unless pair.nil? }\n    result\n  end\n\n  ### \u6253\u5370\u54c8\u5e0c\u8868 ###\n  def print\n    @buckets.each { |pair| puts \"#{pair.key} -> #{pair.val}\" unless pair.nil? }\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_hashing/hash_map/#613","title":"6.1.3 \u00a0 \u54c8\u5e0c\u51b2\u7a81\u4e0e\u6269\u5bb9","text":"

        \u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5c06\u6240\u6709 key \u6784\u6210\u7684\u8f93\u5165\u7a7a\u95f4\u6620\u5c04\u5230\u6570\u7ec4\u6240\u6709\u7d22\u5f15\u6784\u6210\u7684\u8f93\u51fa\u7a7a\u95f4\uff0c\u800c\u8f93\u5165\u7a7a\u95f4\u5f80\u5f80\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u7406\u8bba\u4e0a\u4e00\u5b9a\u5b58\u5728\u201c\u591a\u4e2a\u8f93\u5165\u5bf9\u5e94\u76f8\u540c\u8f93\u51fa\u201d\u7684\u60c5\u51b5\u3002

        \u5bf9\u4e8e\u4e0a\u8ff0\u793a\u4f8b\u4e2d\u7684\u54c8\u5e0c\u51fd\u6570\uff0c\u5f53\u8f93\u5165\u7684 key \u540e\u4e24\u4f4d\u76f8\u540c\u65f6\uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u8f93\u51fa\u7ed3\u679c\u4e5f\u76f8\u540c\u3002\u4f8b\u5982\uff0c\u67e5\u8be2\u5b66\u53f7\u4e3a 12836 \u548c 20336 \u7684\u4e24\u4e2a\u5b66\u751f\u65f6\uff0c\u6211\u4eec\u5f97\u5230\uff1a

        12836 % 100 = 36\n20336 % 100 = 36\n

        \u5982\u56fe 6-3 \u6240\u793a\uff0c\u4e24\u4e2a\u5b66\u53f7\u6307\u5411\u4e86\u540c\u4e00\u4e2a\u59d3\u540d\uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u5bf9\u7684\u3002\u6211\u4eec\u5c06\u8fd9\u79cd\u591a\u4e2a\u8f93\u5165\u5bf9\u5e94\u540c\u4e00\u8f93\u51fa\u7684\u60c5\u51b5\u79f0\u4e3a\u54c8\u5e0c\u51b2\u7a81\uff08hash collision\uff09\u3002

        \u56fe 6-3 \u00a0 \u54c8\u5e0c\u51b2\u7a81\u793a\u4f8b

        \u5bb9\u6613\u60f3\u5230\uff0c\u54c8\u5e0c\u8868\u5bb9\u91cf \\(n\\) \u8d8a\u5927\uff0c\u591a\u4e2a key \u88ab\u5206\u914d\u5230\u540c\u4e00\u4e2a\u6876\u4e2d\u7684\u6982\u7387\u5c31\u8d8a\u4f4e\uff0c\u51b2\u7a81\u5c31\u8d8a\u5c11\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6269\u5bb9\u54c8\u5e0c\u8868\u6765\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u3002

        \u5982\u56fe 6-4 \u6240\u793a\uff0c\u6269\u5bb9\u524d\u952e\u503c\u5bf9 (136, A) \u548c (236, D) \u53d1\u751f\u51b2\u7a81\uff0c\u6269\u5bb9\u540e\u51b2\u7a81\u6d88\u5931\u3002

        \u56fe 6-4 \u00a0 \u54c8\u5e0c\u8868\u6269\u5bb9

        \u7c7b\u4f3c\u4e8e\u6570\u7ec4\u6269\u5bb9\uff0c\u54c8\u5e0c\u8868\u6269\u5bb9\u9700\u5c06\u6240\u6709\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u8fc1\u79fb\u81f3\u65b0\u54c8\u5e0c\u8868\uff0c\u975e\u5e38\u8017\u65f6\uff1b\u5e76\u4e14\u7531\u4e8e\u54c8\u5e0c\u8868\u5bb9\u91cf capacity \u6539\u53d8\uff0c\u6211\u4eec\u9700\u8981\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u6765\u91cd\u65b0\u8ba1\u7b97\u6240\u6709\u952e\u503c\u5bf9\u7684\u5b58\u50a8\u4f4d\u7f6e\uff0c\u8fd9\u8fdb\u4e00\u6b65\u589e\u52a0\u4e86\u6269\u5bb9\u8fc7\u7a0b\u7684\u8ba1\u7b97\u5f00\u9500\u3002\u4e3a\u6b64\uff0c\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u4f1a\u9884\u7559\u8db3\u591f\u5927\u7684\u54c8\u5e0c\u8868\u5bb9\u91cf\uff0c\u9632\u6b62\u9891\u7e41\u6269\u5bb9\u3002

        \u8d1f\u8f7d\u56e0\u5b50\uff08load factor\uff09\u662f\u54c8\u5e0c\u8868\u7684\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\uff0c\u5176\u5b9a\u4e49\u4e3a\u54c8\u5e0c\u8868\u7684\u5143\u7d20\u6570\u91cf\u9664\u4ee5\u6876\u6570\u91cf\uff0c\u7528\u4e8e\u8861\u91cf\u54c8\u5e0c\u51b2\u7a81\u7684\u4e25\u91cd\u7a0b\u5ea6\uff0c\u4e5f\u5e38\u4f5c\u4e3a\u54c8\u5e0c\u8868\u6269\u5bb9\u7684\u89e6\u53d1\u6761\u4ef6\u3002\u4f8b\u5982\u5728 Java \u4e2d\uff0c\u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7 \\(0.75\\) \u65f6\uff0c\u7cfb\u7edf\u4f1a\u5c06\u54c8\u5e0c\u8868\u6269\u5bb9\u81f3\u539f\u5148\u7684 \\(2\\) \u500d\u3002

        "},{"location":"chapter_hashing/summary/","title":"6.4 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_hashing/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u8f93\u5165 key \uff0c\u54c8\u5e0c\u8868\u80fd\u591f\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u67e5\u8be2\u5230 value \uff0c\u6548\u7387\u975e\u5e38\u9ad8\u3002
        • \u5e38\u89c1\u7684\u54c8\u5e0c\u8868\u64cd\u4f5c\u5305\u62ec\u67e5\u8be2\u3001\u6dfb\u52a0\u952e\u503c\u5bf9\u3001\u5220\u9664\u952e\u503c\u5bf9\u548c\u904d\u5386\u54c8\u5e0c\u8868\u7b49\u3002
        • \u54c8\u5e0c\u51fd\u6570\u5c06 key \u6620\u5c04\u4e3a\u6570\u7ec4\u7d22\u5f15\uff0c\u4ece\u800c\u8bbf\u95ee\u5bf9\u5e94\u6876\u5e76\u83b7\u53d6 value \u3002
        • \u4e24\u4e2a\u4e0d\u540c\u7684 key \u53ef\u80fd\u5728\u7ecf\u8fc7\u54c8\u5e0c\u51fd\u6570\u540e\u5f97\u5230\u76f8\u540c\u7684\u6570\u7ec4\u7d22\u5f15\uff0c\u5bfc\u81f4\u67e5\u8be2\u7ed3\u679c\u51fa\u9519\uff0c\u8fd9\u79cd\u73b0\u8c61\u88ab\u79f0\u4e3a\u54c8\u5e0c\u51b2\u7a81\u3002
        • \u54c8\u5e0c\u8868\u5bb9\u91cf\u8d8a\u5927\uff0c\u54c8\u5e0c\u51b2\u7a81\u7684\u6982\u7387\u5c31\u8d8a\u4f4e\u3002\u56e0\u6b64\u53ef\u4ee5\u901a\u8fc7\u6269\u5bb9\u54c8\u5e0c\u8868\u6765\u7f13\u89e3\u54c8\u5e0c\u51b2\u7a81\u3002\u4e0e\u6570\u7ec4\u6269\u5bb9\u7c7b\u4f3c\uff0c\u54c8\u5e0c\u8868\u6269\u5bb9\u64cd\u4f5c\u7684\u5f00\u9500\u5f88\u5927\u3002
        • \u8d1f\u8f7d\u56e0\u5b50\u5b9a\u4e49\u4e3a\u54c8\u5e0c\u8868\u4e2d\u5143\u7d20\u6570\u91cf\u9664\u4ee5\u6876\u6570\u91cf\uff0c\u53cd\u6620\u4e86\u54c8\u5e0c\u51b2\u7a81\u7684\u4e25\u91cd\u7a0b\u5ea6\uff0c\u5e38\u7528\u4f5c\u89e6\u53d1\u54c8\u5e0c\u8868\u6269\u5bb9\u7684\u6761\u4ef6\u3002
        • \u94fe\u5f0f\u5730\u5740\u901a\u8fc7\u5c06\u5355\u4e2a\u5143\u7d20\u8f6c\u5316\u4e3a\u94fe\u8868\uff0c\u5c06\u6240\u6709\u51b2\u7a81\u5143\u7d20\u5b58\u50a8\u5728\u540c\u4e00\u4e2a\u94fe\u8868\u4e2d\u3002\u7136\u800c\uff0c\u94fe\u8868\u8fc7\u957f\u4f1a\u964d\u4f4e\u67e5\u8be2\u6548\u7387\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fdb\u4e00\u6b65\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811\u6765\u63d0\u9ad8\u6548\u7387\u3002
        • \u5f00\u653e\u5bfb\u5740\u901a\u8fc7\u591a\u6b21\u63a2\u6d4b\u6765\u5904\u7406\u54c8\u5e0c\u51b2\u7a81\u3002\u7ebf\u6027\u63a2\u6d4b\u4f7f\u7528\u56fa\u5b9a\u6b65\u957f\uff0c\u7f3a\u70b9\u662f\u4e0d\u80fd\u5220\u9664\u5143\u7d20\uff0c\u4e14\u5bb9\u6613\u4ea7\u751f\u805a\u96c6\u3002\u591a\u6b21\u54c8\u5e0c\u4f7f\u7528\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570\u8fdb\u884c\u63a2\u6d4b\uff0c\u76f8\u8f83\u7ebf\u6027\u63a2\u6d4b\u66f4\u4e0d\u6613\u4ea7\u751f\u805a\u96c6\uff0c\u4f46\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570\u589e\u52a0\u4e86\u8ba1\u7b97\u91cf\u3002
        • \u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u91c7\u53d6\u4e86\u4e0d\u540c\u7684\u54c8\u5e0c\u8868\u5b9e\u73b0\u3002\u4f8b\u5982\uff0cJava \u7684 HashMap \u4f7f\u7528\u94fe\u5f0f\u5730\u5740\uff0c\u800c Python \u7684 Dict \u91c7\u7528\u5f00\u653e\u5bfb\u5740\u3002
        • \u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u6211\u4eec\u5e0c\u671b\u54c8\u5e0c\u7b97\u6cd5\u5177\u6709\u786e\u5b9a\u6027\u3001\u9ad8\u6548\u7387\u548c\u5747\u5300\u5206\u5e03\u7684\u7279\u70b9\u3002\u5728\u5bc6\u7801\u5b66\u4e2d\uff0c\u54c8\u5e0c\u7b97\u6cd5\u8fd8\u5e94\u8be5\u5177\u5907\u6297\u78b0\u649e\u6027\u548c\u96ea\u5d29\u6548\u5e94\u3002
        • \u54c8\u5e0c\u7b97\u6cd5\u901a\u5e38\u91c7\u7528\u5927\u8d28\u6570\u4f5c\u4e3a\u6a21\u6570\uff0c\u4ee5\u6700\u5927\u5316\u5730\u4fdd\u8bc1\u54c8\u5e0c\u503c\u5747\u5300\u5206\u5e03\uff0c\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u3002
        • \u5e38\u89c1\u7684\u54c8\u5e0c\u7b97\u6cd5\u5305\u62ec MD5\u3001SHA-1\u3001SHA-2 \u548c SHA-3 \u7b49\u3002MD5 \u5e38\u7528\u4e8e\u6821\u9a8c\u6587\u4ef6\u5b8c\u6574\u6027\uff0cSHA-2 \u5e38\u7528\u4e8e\u5b89\u5168\u5e94\u7528\u4e0e\u534f\u8bae\u3002
        • \u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u4f1a\u4e3a\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u7b97\u6cd5\uff0c\u7528\u4e8e\u8ba1\u7b97\u54c8\u5e0c\u8868\u4e2d\u7684\u6876\u7d22\u5f15\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u53ea\u6709\u4e0d\u53ef\u53d8\u5bf9\u8c61\u662f\u53ef\u54c8\u5e0c\u7684\u3002
        "},{"location":"chapter_hashing/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u662f \\(O(n)\\) \uff1f

        \u5f53\u54c8\u5e0c\u51b2\u7a81\u6bd4\u8f83\u4e25\u91cd\u65f6\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u9000\u5316\u81f3 \\(O(n)\\) \u3002\u5f53\u54c8\u5e0c\u51fd\u6570\u8bbe\u8ba1\u5f97\u6bd4\u8f83\u597d\u3001\u5bb9\u91cf\u8bbe\u7f6e\u6bd4\u8f83\u5408\u7406\u3001\u51b2\u7a81\u6bd4\u8f83\u5e73\u5747\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u3002\u6211\u4eec\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u5185\u7f6e\u7684\u54c8\u5e0c\u8868\u65f6\uff0c\u901a\u5e38\u8ba4\u4e3a\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u3002

        Q\uff1a\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528\u54c8\u5e0c\u51fd\u6570 \\(f(x) = x\\) \u5462\uff1f\u8fd9\u6837\u5c31\u4e0d\u4f1a\u6709\u51b2\u7a81\u4e86\u3002

        \u5728 \\(f(x) = x\\) \u54c8\u5e0c\u51fd\u6570\u4e0b\uff0c\u6bcf\u4e2a\u5143\u7d20\u5bf9\u5e94\u552f\u4e00\u7684\u6876\u7d22\u5f15\uff0c\u8fd9\u4e0e\u6570\u7ec4\u7b49\u4ef7\u3002\u7136\u800c\uff0c\u8f93\u5165\u7a7a\u95f4\u901a\u5e38\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\uff08\u6570\u7ec4\u957f\u5ea6\uff09\uff0c\u56e0\u6b64\u54c8\u5e0c\u51fd\u6570\u7684\u6700\u540e\u4e00\u6b65\u5f80\u5f80\u662f\u5bf9\u6570\u7ec4\u957f\u5ea6\u53d6\u6a21\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u54c8\u5e0c\u8868\u7684\u76ee\u6807\u662f\u5c06\u4e00\u4e2a\u8f83\u5927\u7684\u72b6\u6001\u7a7a\u95f4\u6620\u5c04\u5230\u4e00\u4e2a\u8f83\u5c0f\u7684\u7a7a\u95f4\uff0c\u5e76\u63d0\u4f9b \\(O(1)\\) \u7684\u67e5\u8be2\u6548\u7387\u3002

        Q\uff1a\u54c8\u5e0c\u8868\u5e95\u5c42\u5b9e\u73b0\u662f\u6570\u7ec4\u3001\u94fe\u8868\u3001\u4e8c\u53c9\u6811\uff0c\u4f46\u4e3a\u4ec0\u4e48\u6548\u7387\u53ef\u4ee5\u6bd4\u5b83\u4eec\u66f4\u9ad8\u5462\uff1f

        \u9996\u5148\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u6548\u7387\u53d8\u9ad8\uff0c\u4f46\u7a7a\u95f4\u6548\u7387\u53d8\u4f4e\u4e86\u3002\u54c8\u5e0c\u8868\u6709\u76f8\u5f53\u4e00\u90e8\u5206\u5185\u5b58\u672a\u4f7f\u7528\u3002

        \u5176\u6b21\uff0c\u53ea\u662f\u5728\u7279\u5b9a\u4f7f\u7528\u573a\u666f\u4e0b\u65f6\u95f4\u6548\u7387\u53d8\u9ad8\u4e86\u3002\u5982\u679c\u4e00\u4e2a\u529f\u80fd\u80fd\u591f\u5728\u76f8\u540c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e0b\u4f7f\u7528\u6570\u7ec4\u6216\u94fe\u8868\u5b9e\u73b0\uff0c\u90a3\u4e48\u901a\u5e38\u6bd4\u54c8\u5e0c\u8868\u66f4\u5feb\u3002\u8fd9\u662f\u56e0\u4e3a\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u9700\u8981\u5f00\u9500\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u5e38\u6570\u9879\u66f4\u5927\u3002

        \u6700\u540e\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u80fd\u53d1\u751f\u52a3\u5316\u3002\u4f8b\u5982\u5728\u94fe\u5f0f\u5730\u5740\u4e2d\uff0c\u6211\u4eec\u91c7\u53d6\u5728\u94fe\u8868\u6216\u7ea2\u9ed1\u6811\u4e2d\u6267\u884c\u67e5\u627e\u64cd\u4f5c\uff0c\u4ecd\u7136\u6709\u9000\u5316\u81f3 \\(O(n)\\) \u65f6\u95f4\u7684\u98ce\u9669\u3002

        Q\uff1a\u591a\u6b21\u54c8\u5e0c\u6709\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u7684\u7f3a\u9677\u5417\uff1f\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u7a7a\u95f4\u8fd8\u80fd\u518d\u6b21\u4f7f\u7528\u5417\uff1f

        \u591a\u6b21\u54c8\u5e0c\u662f\u5f00\u653e\u5bfb\u5740\u7684\u4e00\u79cd\uff0c\u5f00\u653e\u5bfb\u5740\u6cd5\u90fd\u6709\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u7684\u7f3a\u9677\uff0c\u9700\u8981\u901a\u8fc7\u6807\u8bb0\u5220\u9664\u3002\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u7a7a\u95f4\u53ef\u4ee5\u518d\u6b21\u4f7f\u7528\u3002\u5f53\u5c06\u65b0\u5143\u7d20\u63d2\u5165\u54c8\u5e0c\u8868\uff0c\u5e76\u4e14\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u627e\u5230\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u4f4d\u7f6e\u65f6\uff0c\u8be5\u4f4d\u7f6e\u53ef\u4ee5\u88ab\u65b0\u5143\u7d20\u4f7f\u7528\u3002\u8fd9\u6837\u505a\u65e2\u80fd\u4fdd\u6301\u54c8\u5e0c\u8868\u7684\u63a2\u6d4b\u5e8f\u5217\u4e0d\u53d8\uff0c\u53c8\u80fd\u4fdd\u8bc1\u54c8\u5e0c\u8868\u7684\u7a7a\u95f4\u4f7f\u7528\u7387\u3002

        Q\uff1a\u4e3a\u4ec0\u4e48\u5728\u7ebf\u6027\u63a2\u6d4b\u4e2d\uff0c\u67e5\u627e\u5143\u7d20\u7684\u65f6\u5019\u4f1a\u51fa\u73b0\u54c8\u5e0c\u51b2\u7a81\u5462\uff1f

        \u67e5\u627e\u7684\u65f6\u5019\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u627e\u5230\u5bf9\u5e94\u7684\u6876\u548c\u952e\u503c\u5bf9\uff0c\u53d1\u73b0 key \u4e0d\u5339\u914d\uff0c\u8fd9\u5c31\u4ee3\u8868\u6709\u54c8\u5e0c\u51b2\u7a81\u3002\u56e0\u6b64\uff0c\u7ebf\u6027\u63a2\u6d4b\u6cd5\u4f1a\u6839\u636e\u9884\u5148\u8bbe\u5b9a\u7684\u6b65\u957f\u4f9d\u6b21\u5411\u4e0b\u67e5\u627e\uff0c\u76f4\u81f3\u627e\u5230\u6b63\u786e\u7684\u952e\u503c\u5bf9\u6216\u65e0\u6cd5\u627e\u5230\u8df3\u51fa\u4e3a\u6b62\u3002

        Q\uff1a\u4e3a\u4ec0\u4e48\u54c8\u5e0c\u8868\u6269\u5bb9\u80fd\u591f\u7f13\u89e3\u54c8\u5e0c\u51b2\u7a81\uff1f

        \u54c8\u5e0c\u51fd\u6570\u7684\u6700\u540e\u4e00\u6b65\u5f80\u5f80\u662f\u5bf9\u6570\u7ec4\u957f\u5ea6 \\(n\\) \u53d6\u6a21\uff08\u53d6\u4f59\uff09\uff0c\u8ba9\u8f93\u51fa\u503c\u843d\u5728\u6570\u7ec4\u7d22\u5f15\u8303\u56f4\u5185\uff1b\u5728\u6269\u5bb9\u540e\uff0c\u6570\u7ec4\u957f\u5ea6 \\(n\\) \u53d1\u751f\u53d8\u5316\uff0c\u800c key \u5bf9\u5e94\u7684\u7d22\u5f15\u4e5f\u53ef\u80fd\u53d1\u751f\u53d8\u5316\u3002\u539f\u5148\u843d\u5728\u540c\u4e00\u4e2a\u6876\u7684\u591a\u4e2a key \uff0c\u5728\u6269\u5bb9\u540e\u53ef\u80fd\u4f1a\u88ab\u5206\u914d\u5230\u591a\u4e2a\u6876\u4e2d\uff0c\u4ece\u800c\u5b9e\u73b0\u54c8\u5e0c\u51b2\u7a81\u7684\u7f13\u89e3\u3002

        Q\uff1a\u5982\u679c\u4e3a\u4e86\u9ad8\u6548\u7684\u5b58\u53d6\uff0c\u90a3\u4e48\u76f4\u63a5\u4f7f\u7528\u6570\u7ec4\u4e0d\u5c31\u597d\u4e86\u5417\uff1f

        \u5f53\u6570\u636e\u7684 key \u662f\u8fde\u7eed\u7684\u5c0f\u8303\u56f4\u6574\u6570\u65f6\uff0c\u76f4\u63a5\u7528\u6570\u7ec4\u5373\u53ef\uff0c\u7b80\u5355\u9ad8\u6548\u3002\u4f46\u5f53 key \u662f\u5176\u4ed6\u7c7b\u578b\uff08\u4f8b\u5982\u5b57\u7b26\u4e32\uff09\u65f6\uff0c\u5c31\u9700\u8981\u501f\u52a9\u54c8\u5e0c\u51fd\u6570\u5c06 key \u6620\u5c04\u4e3a\u6570\u7ec4\u7d22\u5f15\uff0c\u518d\u901a\u8fc7\u6876\u6570\u7ec4\u5b58\u50a8\u5143\u7d20\uff0c\u8fd9\u6837\u7684\u7ed3\u6784\u5c31\u662f\u54c8\u5e0c\u8868\u3002

        "},{"location":"chapter_heap/","title":"\u7b2c 8 \u7ae0 \u00a0 \u5806","text":"

        Abstract

        \u5806\u5c31\u50cf\u662f\u5c71\u5cb3\u5cf0\u5ce6\uff0c\u5c42\u53e0\u8d77\u4f0f\u3001\u5f62\u6001\u5404\u5f02\u3002

        \u5ea7\u5ea7\u5c71\u5cf0\u9ad8\u4f4e\u9519\u843d\uff0c\u800c\u6700\u9ad8\u7684\u5c71\u5cf0\u603b\u662f\u6700\u5148\u6620\u5165\u773c\u5e18\u3002

        "},{"location":"chapter_heap/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 8.1 \u00a0 \u5806
        • 8.2 \u00a0 \u5efa\u5806\u64cd\u4f5c
        • 8.3 \u00a0 Top-k \u95ee\u9898
        • 8.4 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_heap/build_heap/","title":"8.2 \u00a0 \u5efa\u5806\u64cd\u4f5c","text":"

        \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u4f7f\u7528\u4e00\u4e2a\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\u6765\u6784\u5efa\u4e00\u4e2a\u5806\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u201c\u5efa\u5806\u64cd\u4f5c\u201d\u3002

        "},{"location":"chapter_heap/build_heap/#821","title":"8.2.1 \u00a0 \u501f\u52a9\u5165\u5806\u64cd\u4f5c\u5b9e\u73b0","text":"

        \u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u7a7a\u5806\uff0c\u7136\u540e\u904d\u5386\u5217\u8868\uff0c\u4f9d\u6b21\u5bf9\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u201c\u5165\u5806\u64cd\u4f5c\u201d\uff0c\u5373\u5148\u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u5806\u7684\u5c3e\u90e8\uff0c\u518d\u5bf9\u8be5\u5143\u7d20\u6267\u884c\u201c\u4ece\u5e95\u81f3\u9876\u201d\u5806\u5316\u3002

        \u6bcf\u5f53\u4e00\u4e2a\u5143\u7d20\u5165\u5806\uff0c\u5806\u7684\u957f\u5ea6\u5c31\u52a0\u4e00\u3002\u7531\u4e8e\u8282\u70b9\u662f\u4ece\u9876\u5230\u5e95\u4f9d\u6b21\u88ab\u6dfb\u52a0\u8fdb\u4e8c\u53c9\u6811\u7684\uff0c\u56e0\u6b64\u5806\u662f\u201c\u81ea\u4e0a\u800c\u4e0b\u201d\u6784\u5efa\u7684\u3002

        \u8bbe\u5143\u7d20\u6570\u91cf\u4e3a \\(n\\) \uff0c\u6bcf\u4e2a\u5143\u7d20\u7684\u5165\u5806\u64cd\u4f5c\u4f7f\u7528 \\(O(\\log{n})\\) \u65f6\u95f4\uff0c\u56e0\u6b64\u8be5\u5efa\u5806\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002

        "},{"location":"chapter_heap/build_heap/#822","title":"8.2.2 \u00a0 \u901a\u8fc7\u904d\u5386\u5806\u5316\u5b9e\u73b0","text":"

        \u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4e00\u79cd\u66f4\u4e3a\u9ad8\u6548\u7684\u5efa\u5806\u65b9\u6cd5\uff0c\u5171\u5206\u4e3a\u4e24\u6b65\u3002

        1. \u5c06\u5217\u8868\u6240\u6709\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u5730\u6dfb\u52a0\u5230\u5806\u4e2d\uff0c\u6b64\u65f6\u5806\u7684\u6027\u8d28\u5c1a\u672a\u5f97\u5230\u6ee1\u8db3\u3002
        2. \u5012\u5e8f\u904d\u5386\u5806\uff08\u5c42\u5e8f\u904d\u5386\u7684\u5012\u5e8f\uff09\uff0c\u4f9d\u6b21\u5bf9\u6bcf\u4e2a\u975e\u53f6\u8282\u70b9\u6267\u884c\u201c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u201d\u3002

        \u6bcf\u5f53\u5806\u5316\u4e00\u4e2a\u8282\u70b9\u540e\uff0c\u4ee5\u8be5\u8282\u70b9\u4e3a\u6839\u8282\u70b9\u7684\u5b50\u6811\u5c31\u5f62\u6210\u4e00\u4e2a\u5408\u6cd5\u7684\u5b50\u5806\u3002\u800c\u7531\u4e8e\u662f\u5012\u5e8f\u904d\u5386\uff0c\u56e0\u6b64\u5806\u662f\u201c\u81ea\u4e0b\u800c\u4e0a\u201d\u6784\u5efa\u7684\u3002

        \u4e4b\u6240\u4ee5\u9009\u62e9\u5012\u5e8f\u904d\u5386\uff0c\u662f\u56e0\u4e3a\u8fd9\u6837\u80fd\u591f\u4fdd\u8bc1\u5f53\u524d\u8282\u70b9\u4e4b\u4e0b\u7684\u5b50\u6811\u5df2\u7ecf\u662f\u5408\u6cd5\u7684\u5b50\u5806\uff0c\u8fd9\u6837\u5806\u5316\u5f53\u524d\u8282\u70b9\u624d\u662f\u6709\u6548\u7684\u3002

        \u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u7531\u4e8e\u53f6\u8282\u70b9\u6ca1\u6709\u5b50\u8282\u70b9\uff0c\u56e0\u6b64\u5b83\u4eec\u5929\u7136\u5c31\u662f\u5408\u6cd5\u7684\u5b50\u5806\uff0c\u65e0\u987b\u5806\u5316\u3002\u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u6700\u540e\u4e00\u4e2a\u975e\u53f6\u8282\u70b9\u662f\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u7684\u7236\u8282\u70b9\uff0c\u6211\u4eec\u4ece\u5b83\u5f00\u59cb\u5012\u5e8f\u904d\u5386\u5e76\u6267\u884c\u5806\u5316\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
        def __init__(self, nums: list[int]):\n    \"\"\"\u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806\"\"\"\n    # \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n    self.max_heap = nums\n    # \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for i in range(self.parent(self.size() - 1), -1, -1):\n        self.sift_down(i)\n
        my_heap.cpp
        /* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(vector<int> nums) {\n    // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n    maxHeap = nums;\n    // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for (int i = parent(size() - 1); i >= 0; i--) {\n        siftDown(i);\n    }\n}\n
        my_heap.java
        /* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(List<Integer> nums) {\n    // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n    maxHeap = new ArrayList<>(nums);\n    // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for (int i = parent(size() - 1); i >= 0; i--) {\n        siftDown(i);\n    }\n}\n
        my_heap.cs
        /* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(IEnumerable<int> nums) {\n    // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n    maxHeap = new List<int>(nums);\n    // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    var size = Parent(this.Size() - 1);\n    for (int i = size; i >= 0; i--) {\n        SiftDown(i);\n    }\n}\n
        my_heap.go
        /* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u5207\u7247\u5efa\u5806 */\nfunc newMaxHeap(nums []any) *maxHeap {\n    // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n    h := &maxHeap{data: nums}\n    for i := h.parent(len(h.data) - 1); i >= 0; i-- {\n        // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n        h.siftDown(i)\n    }\n    return h\n}\n
        my_heap.swift
        /* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\ninit(nums: [Int]) {\n    // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n    maxHeap = nums\n    // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for i in (0 ... parent(i: size() - 1)).reversed() {\n        siftDown(i: i)\n    }\n}\n
        my_heap.js
        /* \u6784\u9020\u65b9\u6cd5\uff0c\u5efa\u7acb\u7a7a\u5806\u6216\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nconstructor(nums) {\n    // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n    this.#maxHeap = nums === undefined ? [] : [...nums];\n    // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for (let i = this.#parent(this.size() - 1); i >= 0; i--) {\n        this.#siftDown(i);\n    }\n}\n
        my_heap.ts
        /* \u6784\u9020\u65b9\u6cd5\uff0c\u5efa\u7acb\u7a7a\u5806\u6216\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nconstructor(nums?: number[]) {\n    // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n    this.maxHeap = nums === undefined ? [] : [...nums];\n    // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for (let i = this.parent(this.size() - 1); i >= 0; i--) {\n        this.siftDown(i);\n    }\n}\n
        my_heap.dart
        /* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(List<int> nums) {\n  // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n  _maxHeap = nums;\n  // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n  for (int i = _parent(size() - 1); i >= 0; i--) {\n    siftDown(i);\n  }\n}\n
        my_heap.rs
        /* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nfn new(nums: Vec<i32>) -> Self {\n    // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n    let mut heap = MaxHeap { max_heap: nums };\n    // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for i in (0..=Self::parent(heap.size() - 1)).rev() {\n        heap.sift_down(i);\n    }\n    heap\n}\n
        my_heap.c
        /* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u5207\u7247\u5efa\u5806 */\nMaxHeap *newMaxHeap(int nums[], int size) {\n    // \u6240\u6709\u5143\u7d20\u5165\u5806\n    MaxHeap *maxHeap = (MaxHeap *)malloc(sizeof(MaxHeap));\n    maxHeap->size = size;\n    memcpy(maxHeap->data, nums, size * sizeof(int));\n    for (int i = parent(maxHeap, size - 1); i >= 0; i--) {\n        // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n        siftDown(maxHeap, i);\n    }\n    return maxHeap;\n}\n
        my_heap.kt
        /* \u5927\u9876\u5806 */\nclass MaxHeap(nums: MutableList<Int>?) {\n    // \u4f7f\u7528\u5217\u8868\u800c\u975e\u6570\u7ec4\uff0c\u8fd9\u6837\u65e0\u987b\u8003\u8651\u6269\u5bb9\u95ee\u9898\n    private val maxHeap = mutableListOf<Int>()\n\n    /* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\n    init {\n        // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n        maxHeap.addAll(nums!!)\n        // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n        for (i in parent(size() - 1) downTo 0) {\n            siftDown(i)\n        }\n    }\n\n    /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    private fun left(i: Int): Int {\n        return 2 * i + 1\n    }\n\n    /* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    private fun right(i: Int): Int {\n        return 2 * i + 2\n    }\n\n    /* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n    private fun parent(i: Int): Int {\n        return (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n    }\n\n    /* \u4ea4\u6362\u5143\u7d20 */\n    private fun swap(i: Int, j: Int) {\n        val temp = maxHeap[i]\n        maxHeap[i] = maxHeap[j]\n        maxHeap[j] = temp\n    }\n\n    /* \u83b7\u53d6\u5806\u5927\u5c0f */\n    fun size(): Int {\n        return maxHeap.size\n    }\n\n    /* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\n    fun isEmpty(): Boolean {\n        /* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\n        return size() == 0\n    }\n\n    /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\n    fun peek(): Int {\n        return maxHeap[0]\n    }\n\n    /* \u5143\u7d20\u5165\u5806 */\n    fun push(_val: Int) {\n        // \u6dfb\u52a0\u8282\u70b9\n        maxHeap.add(_val)\n        // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n        siftUp(size() - 1)\n    }\n\n    /* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\n    private fun siftUp(it: Int) {\n        // Kotlin\u7684\u51fd\u6570\u53c2\u6570\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u521b\u5efa\u4e34\u65f6\u53d8\u91cf\n        var i = it\n        while (true) {\n            // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n            val p = parent(i)\n            // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n            if (p < 0 || maxHeap[i] <= maxHeap[p]) break\n            // \u4ea4\u6362\u4e24\u8282\u70b9\n            swap(i, p)\n            // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n            i = p\n        }\n    }\n\n    /* \u5143\u7d20\u51fa\u5806 */\n    fun pop(): Int {\n        // \u5224\u7a7a\u5904\u7406\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        swap(0, size() - 1)\n        // \u5220\u9664\u8282\u70b9\n        val _val = maxHeap.removeAt(size() - 1)\n        // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n        siftDown(0)\n        // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n        return _val\n    }\n\n    /* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\n    private fun siftDown(it: Int) {\n        // Kotlin\u7684\u51fd\u6570\u53c2\u6570\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u521b\u5efa\u4e34\u65f6\u53d8\u91cf\n        var i = it\n        while (true) {\n            // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n            val l = left(i)\n            val r = right(i)\n            var ma = i\n            if (l < size() && maxHeap[l] > maxHeap[ma]) ma = l\n            if (r < size() && maxHeap[r] > maxHeap[ma]) ma = r\n            // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n            if (ma == i) break\n            // \u4ea4\u6362\u4e24\u8282\u70b9\n            swap(i, ma)\n            // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n            i = ma\n        }\n    }\n\n    /* \u6253\u5370\u5806\uff08\u4e8c\u53c9\u6811\uff09 */\n    fun print() {\n        val queue = PriorityQueue { a: Int, b: Int -> b - a }\n        queue.addAll(maxHeap)\n        printHeap(queue)\n    }\n}\n
        my_heap.rb
        ### \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 ###\ndef initialize(nums)\n  # \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n  @max_heap = nums\n  # \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n  parent(size - 1).downto(0) do |i|\n    sift_down(i)\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_heap/build_heap/#823","title":"8.2.3 \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"

        \u4e0b\u9762\uff0c\u6211\u4eec\u6765\u5c1d\u8bd5\u63a8\u7b97\u7b2c\u4e8c\u79cd\u5efa\u5806\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002

        • \u5047\u8bbe\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u5219\u53f6\u8282\u70b9\u6570\u91cf\u4e3a \\((n + 1) / 2\\) \uff0c\u5176\u4e2d \\(/\\) \u4e3a\u5411\u4e0b\u6574\u9664\u3002\u56e0\u6b64\u9700\u8981\u5806\u5316\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\((n - 1) / 2\\) \u3002
        • \u5728\u4ece\u9876\u81f3\u5e95\u5806\u5316\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6bcf\u4e2a\u8282\u70b9\u6700\u591a\u5806\u5316\u5230\u53f6\u8282\u70b9\uff0c\u56e0\u6b64\u6700\u5927\u8fed\u4ee3\u6b21\u6570\u4e3a\u4e8c\u53c9\u6811\u9ad8\u5ea6 \\(\\log n\\) \u3002

        \u5c06\u4e0a\u8ff0\u4e24\u8005\u76f8\u4e58\uff0c\u53ef\u5f97\u5230\u5efa\u5806\u8fc7\u7a0b\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002\u4f46\u8fd9\u4e2a\u4f30\u7b97\u7ed3\u679c\u5e76\u4e0d\u51c6\u786e\uff0c\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u8003\u8651\u5230\u4e8c\u53c9\u6811\u5e95\u5c42\u8282\u70b9\u6570\u91cf\u8fdc\u591a\u4e8e\u9876\u5c42\u8282\u70b9\u7684\u6027\u8d28\u3002

        \u63a5\u4e0b\u6765\u6211\u4eec\u6765\u8fdb\u884c\u66f4\u4e3a\u51c6\u786e\u7684\u8ba1\u7b97\u3002\u4e3a\u4e86\u964d\u4f4e\u8ba1\u7b97\u96be\u5ea6\uff0c\u5047\u8bbe\u7ed9\u5b9a\u4e00\u4e2a\u8282\u70b9\u6570\u91cf\u4e3a \\(n\\) \u3001\u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u201c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u201d\uff0c\u8be5\u5047\u8bbe\u4e0d\u4f1a\u5f71\u54cd\u8ba1\u7b97\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002

        \u56fe 8-5 \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u5404\u5c42\u8282\u70b9\u6570\u91cf

        \u5982\u56fe 8-5 \u6240\u793a\uff0c\u8282\u70b9\u201c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u201d\u7684\u6700\u5927\u8fed\u4ee3\u6b21\u6570\u7b49\u4e8e\u8be5\u8282\u70b9\u5230\u53f6\u8282\u70b9\u7684\u8ddd\u79bb\uff0c\u800c\u8be5\u8ddd\u79bb\u6b63\u662f\u201c\u8282\u70b9\u9ad8\u5ea6\u201d\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u5404\u5c42\u7684\u201c\u8282\u70b9\u6570\u91cf \\(\\times\\) \u8282\u70b9\u9ad8\u5ea6\u201d\u6c42\u548c\uff0c\u5f97\u5230\u6240\u6709\u8282\u70b9\u7684\u5806\u5316\u8fed\u4ee3\u6b21\u6570\u7684\u603b\u548c\u3002

        \\[ T(h) = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{(h-1)}\\times1 \\]

        \u5316\u7b80\u4e0a\u5f0f\u9700\u8981\u501f\u52a9\u4e2d\u5b66\u7684\u6570\u5217\u77e5\u8bc6\uff0c\u5148\u5c06 \\(T(h)\\) \u4e58\u4ee5 \\(2\\) \uff0c\u5f97\u5230\uff1a

        \\[ \\begin{aligned} T(h) & = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{h-1}\\times1 \\newline 2 T(h) & = 2^1h + 2^2(h-1) + 2^3(h-2) + \\dots + 2^{h}\\times1 \\newline \\end{aligned} \\]

        \u4f7f\u7528\u9519\u4f4d\u76f8\u51cf\u6cd5\uff0c\u7528\u4e0b\u5f0f \\(2 T(h)\\) \u51cf\u53bb\u4e0a\u5f0f \\(T(h)\\) \uff0c\u53ef\u5f97\uff1a

        \\[ 2T(h) - T(h) = T(h) = -2^0h + 2^1 + 2^2 + \\dots + 2^{h-1} + 2^h \\]

        \u89c2\u5bdf\u4e0a\u5f0f\uff0c\u53d1\u73b0 \\(T(h)\\) \u662f\u4e00\u4e2a\u7b49\u6bd4\u6570\u5217\uff0c\u53ef\u76f4\u63a5\u4f7f\u7528\u6c42\u548c\u516c\u5f0f\uff0c\u5f97\u5230\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\uff1a

        \\[ \\begin{aligned} T(h) & = 2 \\frac{1 - 2^h}{1 - 2} - h \\newline & = 2^{h+1} - h - 2 \\newline & = O(2^h) \\end{aligned} \\]

        \u8fdb\u4e00\u6b65\uff0c\u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(n = 2^{h+1} - 1\\) \uff0c\u6613\u5f97\u590d\u6742\u5ea6\u4e3a \\(O(2^h) = O(n)\\) \u3002\u4ee5\u4e0a\u63a8\u7b97\u8868\u660e\uff0c\u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002

        "},{"location":"chapter_heap/heap/","title":"8.1 \u00a0 \u5806","text":"

        \u5806\uff08heap\uff09\u662f\u4e00\u79cd\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u4e3b\u8981\u53ef\u5206\u4e3a\u4e24\u79cd\u7c7b\u578b\uff0c\u5982\u56fe 8-1 \u6240\u793a\u3002

        • \u5c0f\u9876\u5806\uff08min heap\uff09\uff1a\u4efb\u610f\u8282\u70b9\u7684\u503c \\(\\leq\\) \u5176\u5b50\u8282\u70b9\u7684\u503c\u3002
        • \u5927\u9876\u5806\uff08max heap\uff09\uff1a\u4efb\u610f\u8282\u70b9\u7684\u503c \\(\\geq\\) \u5176\u5b50\u8282\u70b9\u7684\u503c\u3002

        \u56fe 8-1 \u00a0 \u5c0f\u9876\u5806\u4e0e\u5927\u9876\u5806

        \u5806\u4f5c\u4e3a\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u4e00\u4e2a\u7279\u4f8b\uff0c\u5177\u6709\u4ee5\u4e0b\u7279\u6027\u3002

        • \u6700\u5e95\u5c42\u8282\u70b9\u9760\u5de6\u586b\u5145\uff0c\u5176\u4ed6\u5c42\u7684\u8282\u70b9\u90fd\u88ab\u586b\u6ee1\u3002
        • \u6211\u4eec\u5c06\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\u79f0\u4e3a\u201c\u5806\u9876\u201d\uff0c\u5c06\u5e95\u5c42\u6700\u9760\u53f3\u7684\u8282\u70b9\u79f0\u4e3a\u201c\u5806\u5e95\u201d\u3002
        • \u5bf9\u4e8e\u5927\u9876\u5806\uff08\u5c0f\u9876\u5806\uff09\uff0c\u5806\u9876\u5143\u7d20\uff08\u6839\u8282\u70b9\uff09\u7684\u503c\u662f\u6700\u5927\uff08\u6700\u5c0f\uff09\u7684\u3002
        "},{"location":"chapter_heap/heap/#811","title":"8.1.1 \u00a0 \u5806\u7684\u5e38\u7528\u64cd\u4f5c","text":"

        \u9700\u8981\u6307\u51fa\u7684\u662f\uff0c\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u7684\u662f\u4f18\u5148\u961f\u5217\uff08priority queue\uff09\uff0c\u8fd9\u662f\u4e00\u79cd\u62bd\u8c61\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b9a\u4e49\u4e3a\u5177\u6709\u4f18\u5148\u7ea7\u6392\u5e8f\u7684\u961f\u5217\u3002

        \u5b9e\u9645\u4e0a\uff0c\u5806\u901a\u5e38\u7528\u4e8e\u5b9e\u73b0\u4f18\u5148\u961f\u5217\uff0c\u5927\u9876\u5806\u76f8\u5f53\u4e8e\u5143\u7d20\u6309\u4ece\u5927\u5230\u5c0f\u7684\u987a\u5e8f\u51fa\u961f\u7684\u4f18\u5148\u961f\u5217\u3002\u4ece\u4f7f\u7528\u89d2\u5ea6\u6765\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u201c\u4f18\u5148\u961f\u5217\u201d\u548c\u201c\u5806\u201d\u770b\u4f5c\u7b49\u4ef7\u7684\u6570\u636e\u7ed3\u6784\u3002\u56e0\u6b64\uff0c\u672c\u4e66\u5bf9\u4e24\u8005\u4e0d\u505a\u7279\u522b\u533a\u5206\uff0c\u7edf\u4e00\u79f0\u4f5c\u201c\u5806\u201d\u3002

        \u5806\u7684\u5e38\u7528\u64cd\u4f5c\u89c1\u8868 8-1 \uff0c\u65b9\u6cd5\u540d\u9700\u8981\u6839\u636e\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002

        \u8868 8-1 \u00a0 \u5806\u7684\u64cd\u4f5c\u6548\u7387

        \u65b9\u6cd5\u540d \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push() \u5143\u7d20\u5165\u5806 \\(O(\\log n)\\) pop() \u5806\u9876\u5143\u7d20\u51fa\u5806 \\(O(\\log n)\\) peek() \u8bbf\u95ee\u5806\u9876\u5143\u7d20\uff08\u5bf9\u4e8e\u5927 / \u5c0f\u9876\u5806\u5206\u522b\u4e3a\u6700\u5927 / \u5c0f\u503c\uff09 \\(O(1)\\) size() \u83b7\u53d6\u5806\u7684\u5143\u7d20\u6570\u91cf \\(O(1)\\) isEmpty() \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a \\(O(1)\\)

        \u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u7684\u5806\u7c7b\uff08\u6216\u4f18\u5148\u961f\u5217\u7c7b\uff09\u3002

        \u7c7b\u4f3c\u4e8e\u6392\u5e8f\u7b97\u6cd5\u4e2d\u7684\u201c\u4ece\u5c0f\u5230\u5927\u6392\u5217\u201d\u548c\u201c\u4ece\u5927\u5230\u5c0f\u6392\u5217\u201d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a flag \u6216\u4fee\u6539 Comparator \u5b9e\u73b0\u201c\u5c0f\u9876\u5806\u201d\u4e0e\u201c\u5927\u9876\u5806\u201d\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby heap.py
        # \u521d\u59cb\u5316\u5c0f\u9876\u5806\nmin_heap, flag = [], 1\n# \u521d\u59cb\u5316\u5927\u9876\u5806\nmax_heap, flag = [], -1\n\n# Python \u7684 heapq \u6a21\u5757\u9ed8\u8ba4\u5b9e\u73b0\u5c0f\u9876\u5806\n# \u8003\u8651\u5c06\u201c\u5143\u7d20\u53d6\u8d1f\u201d\u540e\u518d\u5165\u5806\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06\u5927\u5c0f\u5173\u7cfb\u98a0\u5012\uff0c\u4ece\u800c\u5b9e\u73b0\u5927\u9876\u5806\n# \u5728\u672c\u793a\u4f8b\u4e2d\uff0cflag = 1 \u65f6\u5bf9\u5e94\u5c0f\u9876\u5806\uff0cflag = -1 \u65f6\u5bf9\u5e94\u5927\u9876\u5806\n\n# \u5143\u7d20\u5165\u5806\nheapq.heappush(max_heap, flag * 1)\nheapq.heappush(max_heap, flag * 3)\nheapq.heappush(max_heap, flag * 2)\nheapq.heappush(max_heap, flag * 5)\nheapq.heappush(max_heap, flag * 4)\n\n# \u83b7\u53d6\u5806\u9876\u5143\u7d20\npeek: int = flag * max_heap[0] # 5\n\n# \u5806\u9876\u5143\u7d20\u51fa\u5806\n# \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nval = flag * heapq.heappop(max_heap) # 5\nval = flag * heapq.heappop(max_heap) # 4\nval = flag * heapq.heappop(max_heap) # 3\nval = flag * heapq.heappop(max_heap) # 2\nval = flag * heapq.heappop(max_heap) # 1\n\n# \u83b7\u53d6\u5806\u5927\u5c0f\nsize: int = len(max_heap)\n\n# \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = not max_heap\n\n# \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806\nmin_heap: list[int] = [1, 3, 2, 5, 4]\nheapq.heapify(min_heap)\n
        heap.cpp
        /* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\npriority_queue<int, vector<int>, greater<int>> minHeap;\n// \u521d\u59cb\u5316\u5927\u9876\u5806\npriority_queue<int, vector<int>, less<int>> maxHeap;\n\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.push(1);\nmaxHeap.push(3);\nmaxHeap.push(2);\nmaxHeap.push(5);\nmaxHeap.push(4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.top(); // 5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nmaxHeap.pop(); // 5\nmaxHeap.pop(); // 4\nmaxHeap.pop(); // 3\nmaxHeap.pop(); // 2\nmaxHeap.pop(); // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.size();\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = maxHeap.empty();\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nvector<int> input{1, 3, 2, 5, 4};\npriority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());\n
        heap.java
        /* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nQueue<Integer> minHeap = new PriorityQueue<>();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\uff08\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);\n\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.offer(1);\nmaxHeap.offer(3);\nmaxHeap.offer(2);\nmaxHeap.offer(5);\nmaxHeap.offer(4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.peek(); // 5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = maxHeap.poll(); // 5\npeek = maxHeap.poll(); // 4\npeek = maxHeap.poll(); // 3\npeek = maxHeap.poll(); // 2\npeek = maxHeap.poll(); // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.size();\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = maxHeap.isEmpty();\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));\n
        heap.cs
        /* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nPriorityQueue<int, int> minHeap = new();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\uff08\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u4fee\u6539 Comparer \u5373\u53ef\uff09\nPriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y.CompareTo(x)));\n\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.Enqueue(1, 1);\nmaxHeap.Enqueue(3, 3);\nmaxHeap.Enqueue(2, 2);\nmaxHeap.Enqueue(5, 5);\nmaxHeap.Enqueue(4, 4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.Peek();//5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = maxHeap.Dequeue();  // 5\npeek = maxHeap.Dequeue();  // 4\npeek = maxHeap.Dequeue();  // 3\npeek = maxHeap.Dequeue();  // 2\npeek = maxHeap.Dequeue();  // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.Count;\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = maxHeap.Count == 0;\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = new PriorityQueue<int, int>([(1, 1), (3, 3), (2, 2), (5, 5), (4, 4)]);\n
        heap.go
        // Go \u8bed\u8a00\u4e2d\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0 heap.Interface \u6765\u6784\u5efa\u6574\u6570\u5927\u9876\u5806\n// \u5b9e\u73b0 heap.Interface \u9700\u8981\u540c\u65f6\u5b9e\u73b0 sort.Interface\ntype intHeap []any\n\n// Push heap.Interface \u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u63a8\u5165\u5143\u7d20\u5230\u5806\nfunc (h *intHeap) Push(x any) {\n    // Push \u548c Pop \u4f7f\u7528 pointer receiver \u4f5c\u4e3a\u53c2\u6570\n    // \u56e0\u4e3a\u5b83\u4eec\u4e0d\u4ec5\u4f1a\u5bf9\u5207\u7247\u7684\u5185\u5bb9\u8fdb\u884c\u8c03\u6574\uff0c\u8fd8\u4f1a\u4fee\u6539\u5207\u7247\u7684\u957f\u5ea6\u3002\n    *h = append(*h, x.(int))\n}\n\n// Pop heap.Interface \u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u5f39\u51fa\u5806\u9876\u5143\u7d20\nfunc (h *intHeap) Pop() any {\n    // \u5f85\u51fa\u5806\u5143\u7d20\u5b58\u653e\u5728\u6700\u540e\n    last := (*h)[len(*h)-1]\n    *h = (*h)[:len(*h)-1]\n    return last\n}\n\n// Len sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Len() int {\n    return len(*h)\n}\n\n// Less sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Less(i, j int) bool {\n    // \u5982\u679c\u5b9e\u73b0\u5c0f\u9876\u5806\uff0c\u5219\u9700\u8981\u8c03\u6574\u4e3a\u5c0f\u4e8e\u53f7\n    return (*h)[i].(int) > (*h)[j].(int)\n}\n\n// Swap sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Swap(i, j int) {\n    (*h)[i], (*h)[j] = (*h)[j], (*h)[i]\n}\n\n// Top \u83b7\u53d6\u5806\u9876\u5143\u7d20\nfunc (h *intHeap) Top() any {\n    return (*h)[0]\n}\n\n/* Driver Code */\nfunc TestHeap(t *testing.T) {\n    /* \u521d\u59cb\u5316\u5806 */\n    // \u521d\u59cb\u5316\u5927\u9876\u5806\n    maxHeap := &intHeap{}\n    heap.Init(maxHeap)\n    /* \u5143\u7d20\u5165\u5806 */\n    // \u8c03\u7528 heap.Interface \u7684\u65b9\u6cd5\uff0c\u6765\u6dfb\u52a0\u5143\u7d20\n    heap.Push(maxHeap, 1)\n    heap.Push(maxHeap, 3)\n    heap.Push(maxHeap, 2)\n    heap.Push(maxHeap, 4)\n    heap.Push(maxHeap, 5)\n\n    /* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\n    top := maxHeap.Top()\n    fmt.Printf(\"\u5806\u9876\u5143\u7d20\u4e3a %d\\n\", top)\n\n    /* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n    // \u8c03\u7528 heap.Interface \u7684\u65b9\u6cd5\uff0c\u6765\u79fb\u9664\u5143\u7d20\n    heap.Pop(maxHeap) // 5\n    heap.Pop(maxHeap) // 4\n    heap.Pop(maxHeap) // 3\n    heap.Pop(maxHeap) // 2\n    heap.Pop(maxHeap) // 1\n\n    /* \u83b7\u53d6\u5806\u5927\u5c0f */\n    size := len(*maxHeap)\n    fmt.Printf(\"\u5806\u5143\u7d20\u6570\u91cf\u4e3a %d\\n\", size)\n\n    /* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\n    isEmpty := len(*maxHeap) == 0\n    fmt.Printf(\"\u5806\u662f\u5426\u4e3a\u7a7a %t\\n\", isEmpty)\n}\n
        heap.swift
        /* \u521d\u59cb\u5316\u5806 */\n// Swift \u7684 Heap \u7c7b\u578b\u540c\u65f6\u652f\u6301\u6700\u5927\u5806\u548c\u6700\u5c0f\u5806\uff0c\u4e14\u9700\u8981\u5f15\u5165 swift-collections\nvar heap = Heap<Int>()\n\n/* \u5143\u7d20\u5165\u5806 */\nheap.insert(1)\nheap.insert(3)\nheap.insert(2)\nheap.insert(5)\nheap.insert(4)\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nvar peek = heap.max()!\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\npeek = heap.removeMax() // 5\npeek = heap.removeMax() // 4\npeek = heap.removeMax() // 3\npeek = heap.removeMax() // 2\npeek = heap.removeMax() // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nlet size = heap.count\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = heap.isEmpty\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nlet heap2 = Heap([1, 3, 2, 5, 4])\n
        heap.js
        // JavaScript \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
        heap.ts
        // TypeScript \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
        heap.dart
        // Dart \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
        heap.rs
        use std::collections::BinaryHeap;\nuse std::cmp::Reverse;\n\n/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nlet mut min_heap = BinaryHeap::<Reverse<i32>>::new();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\nlet mut max_heap = BinaryHeap::new();\n\n/* \u5143\u7d20\u5165\u5806 */\nmax_heap.push(1);\nmax_heap.push(3);\nmax_heap.push(2);\nmax_heap.push(5);\nmax_heap.push(4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nlet peek = max_heap.peek().unwrap();  // 5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nlet peek = max_heap.pop().unwrap();   // 5\nlet peek = max_heap.pop().unwrap();   // 4\nlet peek = max_heap.pop().unwrap();   // 3\nlet peek = max_heap.pop().unwrap();   // 2\nlet peek = max_heap.pop().unwrap();   // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nlet size = max_heap.len();\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = max_heap.is_empty();\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nlet min_heap = BinaryHeap::from(vec![Reverse(1), Reverse(3), Reverse(2), Reverse(5), Reverse(4)]);\n
        heap.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
        heap.kt
        /* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nvar minHeap = PriorityQueue<Int>()\n// \u521d\u59cb\u5316\u5927\u9876\u5806\uff08\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nval maxHeap = PriorityQueue { a: Int, b: Int -> b - a }\n\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.offer(1)\nmaxHeap.offer(3)\nmaxHeap.offer(2)\nmaxHeap.offer(5)\nmaxHeap.offer(4)\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nvar peek = maxHeap.peek() // 5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = maxHeap.poll() // 5\npeek = maxHeap.poll() // 4\npeek = maxHeap.poll() // 3\npeek = maxHeap.poll() // 2\npeek = maxHeap.poll() // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nval size = maxHeap.size\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nval isEmpty = maxHeap.isEmpty()\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = PriorityQueue(mutableListOf(1, 3, 2, 5, 4))\n
        heap.rb
        # Ruby \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_heap/heap/#812","title":"8.1.2 \u00a0 \u5806\u7684\u5b9e\u73b0","text":"

        \u4e0b\u6587\u5b9e\u73b0\u7684\u662f\u5927\u9876\u5806\u3002\u82e5\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u5c0f\u9876\u5806\uff0c\u53ea\u9700\u5c06\u6240\u6709\u5927\u5c0f\u903b\u8f91\u5224\u65ad\u8fdb\u884c\u9006\u8f6c\uff08\u4f8b\u5982\uff0c\u5c06 \\(\\geq\\) \u66ff\u6362\u4e3a \\(\\leq\\) \uff09\u3002\u611f\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u3002

        "},{"location":"chapter_heap/heap/#1","title":"1. \u00a0 \u5806\u7684\u5b58\u50a8\u4e0e\u8868\u793a","text":"

        \u201c\u4e8c\u53c9\u6811\u201d\u7ae0\u8282\u8bb2\u8fc7\uff0c\u5b8c\u5168\u4e8c\u53c9\u6811\u975e\u5e38\u9002\u5408\u7528\u6570\u7ec4\u6765\u8868\u793a\u3002\u7531\u4e8e\u5806\u6b63\u662f\u4e00\u79cd\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u56e0\u6b64\u6211\u4eec\u5c06\u91c7\u7528\u6570\u7ec4\u6765\u5b58\u50a8\u5806\u3002

        \u5f53\u4f7f\u7528\u6570\u7ec4\u8868\u793a\u4e8c\u53c9\u6811\u65f6\uff0c\u5143\u7d20\u4ee3\u8868\u8282\u70b9\u503c\uff0c\u7d22\u5f15\u4ee3\u8868\u8282\u70b9\u5728\u4e8c\u53c9\u6811\u4e2d\u7684\u4f4d\u7f6e\u3002\u8282\u70b9\u6307\u9488\u901a\u8fc7\u7d22\u5f15\u6620\u5c04\u516c\u5f0f\u6765\u5b9e\u73b0\u3002

        \u5982\u56fe 8-2 \u6240\u793a\uff0c\u7ed9\u5b9a\u7d22\u5f15 \\(i\\) \uff0c\u5176\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15\u4e3a \\(2i + 1\\) \uff0c\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15\u4e3a \\(2i + 2\\) \uff0c\u7236\u8282\u70b9\u7684\u7d22\u5f15\u4e3a \\((i - 1) / 2\\)\uff08\u5411\u4e0b\u6574\u9664\uff09\u3002\u5f53\u7d22\u5f15\u8d8a\u754c\u65f6\uff0c\u8868\u793a\u7a7a\u8282\u70b9\u6216\u8282\u70b9\u4e0d\u5b58\u5728\u3002

        \u56fe 8-2 \u00a0 \u5806\u7684\u8868\u793a\u4e0e\u5b58\u50a8

        \u6211\u4eec\u53ef\u4ee5\u5c06\u7d22\u5f15\u6620\u5c04\u516c\u5f0f\u5c01\u88c5\u6210\u51fd\u6570\uff0c\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
        def left(self, i: int) -> int:\n    \"\"\"\u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n    return 2 * i + 1\n\ndef right(self, i: int) -> int:\n    \"\"\"\u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n    return 2 * i + 2\n\ndef parent(self, i: int) -> int:\n    \"\"\"\u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n    return (i - 1) // 2  # \u5411\u4e0b\u6574\u9664\n
        my_heap.cpp
        /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint left(int i) {\n    return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint right(int i) {\n    return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint parent(int i) {\n    return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n
        my_heap.java
        /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint left(int i) {\n    return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint right(int i) {\n    return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint parent(int i) {\n    return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n
        my_heap.cs
        /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint Left(int i) {\n    return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint Right(int i) {\n    return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint Parent(int i) {\n    return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n
        my_heap.go
        /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (h *maxHeap) left(i int) int {\n    return 2*i + 1\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (h *maxHeap) right(i int) int {\n    return 2*i + 2\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (h *maxHeap) parent(i int) int {\n    // \u5411\u4e0b\u6574\u9664\n    return (i - 1) / 2\n}\n
        my_heap.swift
        /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc left(i: Int) -> Int {\n    2 * i + 1\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc right(i: Int) -> Int {\n    2 * i + 2\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc parent(i: Int) -> Int {\n    (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n
        my_heap.js
        /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n#left(i) {\n    return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n#right(i) {\n    return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n#parent(i) {\n    return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n}\n
        my_heap.ts
        /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nleft(i: number): number {\n    return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nright(i: number): number {\n    return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nparent(i: number): number {\n    return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n}\n
        my_heap.dart
        /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint _left(int i) {\n  return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint _right(int i) {\n  return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint _parent(int i) {\n  return (i - 1) ~/ 2; // \u5411\u4e0b\u6574\u9664\n}\n
        my_heap.rs
        /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfn left(i: usize) -> usize {\n    2 * i + 1\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfn right(i: usize) -> usize {\n    2 * i + 2\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfn parent(i: usize) -> usize {\n    (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n
        my_heap.c
        /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint left(MaxHeap *maxHeap, int i) {\n    return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint right(MaxHeap *maxHeap, int i) {\n    return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint parent(MaxHeap *maxHeap, int i) {\n    return (i - 1) / 2; // \u5411\u4e0b\u53d6\u6574\n}\n
        my_heap.kt
        /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfun left(i: Int): Int {\n    return 2 * i + 1\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfun right(i: Int): Int {\n    return 2 * i + 2\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfun parent(i: Int): Int {\n    return (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n
        my_heap.rb
        ### \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 ###\ndef left(i)\n  2 * i + 1\nend\n\n### \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 ###\ndef right(i)\n  2 * i + 2\nend\n\n### \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 ###\ndef parent(i)\n  (i - 1) / 2     # \u5411\u4e0b\u6574\u9664\nend\n
        "},{"location":"chapter_heap/heap/#2","title":"2. \u00a0 \u8bbf\u95ee\u5806\u9876\u5143\u7d20","text":"

        \u5806\u9876\u5143\u7d20\u5373\u4e3a\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\uff0c\u4e5f\u5c31\u662f\u5217\u8868\u7684\u9996\u4e2a\u5143\u7d20\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
        def peek(self) -> int:\n    \"\"\"\u8bbf\u95ee\u5806\u9876\u5143\u7d20\"\"\"\n    return self.max_heap[0]\n
        my_heap.cpp
        /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\n    return maxHeap[0];\n}\n
        my_heap.java
        /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\n    return maxHeap.get(0);\n}\n
        my_heap.cs
        /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint Peek() {\n    return maxHeap[0];\n}\n
        my_heap.go
        /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc (h *maxHeap) peek() any {\n    return h.data[0]\n}\n
        my_heap.swift
        /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc peek() -> Int {\n    maxHeap[0]\n}\n
        my_heap.js
        /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\npeek() {\n    return this.#maxHeap[0];\n}\n
        my_heap.ts
        /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\npeek(): number {\n    return this.maxHeap[0];\n}\n
        my_heap.dart
        /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\n  return _maxHeap[0];\n}\n
        my_heap.rs
        /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfn peek(&self) -> Option<i32> {\n    self.max_heap.first().copied()\n}\n
        my_heap.c
        /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek(MaxHeap *maxHeap) {\n    return maxHeap->data[0];\n}\n
        my_heap.kt
        /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfun peek(): Int {\n    return maxHeap[0]\n}\n
        my_heap.rb
        ### \u8bbf\u95ee\u5806\u9876\u5143\u7d20 ###\ndef peek\n  @max_heap[0]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_heap/heap/#3","title":"3. \u00a0 \u5143\u7d20\u5165\u5806","text":"

        \u7ed9\u5b9a\u5143\u7d20 val \uff0c\u6211\u4eec\u9996\u5148\u5c06\u5176\u6dfb\u52a0\u5230\u5806\u5e95\u3002\u6dfb\u52a0\u4e4b\u540e\uff0c\u7531\u4e8e val \u53ef\u80fd\u5927\u4e8e\u5806\u4e2d\u5176\u4ed6\u5143\u7d20\uff0c\u5806\u7684\u6210\u7acb\u6761\u4ef6\u53ef\u80fd\u5df2\u88ab\u7834\u574f\uff0c\u56e0\u6b64\u9700\u8981\u4fee\u590d\u4ece\u63d2\u5165\u8282\u70b9\u5230\u6839\u8282\u70b9\u7684\u8def\u5f84\u4e0a\u7684\u5404\u4e2a\u8282\u70b9\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u88ab\u79f0\u4e3a\u5806\u5316\uff08heapify\uff09\u3002

        \u8003\u8651\u4ece\u5165\u5806\u8282\u70b9\u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u6267\u884c\u5806\u5316\u3002\u5982\u56fe 8-3 \u6240\u793a\uff0c\u6211\u4eec\u6bd4\u8f83\u63d2\u5165\u8282\u70b9\u4e0e\u5176\u7236\u8282\u70b9\u7684\u503c\uff0c\u5982\u679c\u63d2\u5165\u8282\u70b9\u66f4\u5927\uff0c\u5219\u5c06\u5b83\u4eec\u4ea4\u6362\u3002\u7136\u540e\u7ee7\u7eed\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u4ece\u5e95\u81f3\u9876\u4fee\u590d\u5806\u4e2d\u7684\u5404\u4e2a\u8282\u70b9\uff0c\u76f4\u81f3\u8d8a\u8fc7\u6839\u8282\u70b9\u6216\u9047\u5230\u65e0\u987b\u4ea4\u6362\u7684\u8282\u70b9\u65f6\u7ed3\u675f\u3002

        <1><2><3><4><5><6><7><8><9>

        \u56fe 8-3 \u00a0 \u5143\u7d20\u5165\u5806\u6b65\u9aa4

        \u8bbe\u8282\u70b9\u603b\u6570\u4e3a \\(n\\) \uff0c\u5219\u6811\u7684\u9ad8\u5ea6\u4e3a \\(O(\\log n)\\) \u3002\u7531\u6b64\u53ef\u77e5\uff0c\u5806\u5316\u64cd\u4f5c\u7684\u5faa\u73af\u8f6e\u6570\u6700\u591a\u4e3a \\(O(\\log n)\\) \uff0c\u5143\u7d20\u5165\u5806\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
        def push(self, val: int):\n    \"\"\"\u5143\u7d20\u5165\u5806\"\"\"\n    # \u6dfb\u52a0\u8282\u70b9\n    self.max_heap.append(val)\n    # \u4ece\u5e95\u81f3\u9876\u5806\u5316\n    self.sift_up(self.size() - 1)\n\ndef sift_up(self, i: int):\n    \"\"\"\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316\"\"\"\n    while True:\n        # \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n        p = self.parent(i)\n        # \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n        if p < 0 or self.max_heap[i] <= self.max_heap[p]:\n            break\n        # \u4ea4\u6362\u4e24\u8282\u70b9\n        self.swap(i, p)\n        # \u5faa\u73af\u5411\u4e0a\u5806\u5316\n        i = p\n
        my_heap.cpp
        /* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n    // \u6dfb\u52a0\u8282\u70b9\n    maxHeap.push_back(val);\n    // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n    siftUp(size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\n    while (true) {\n        // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n        int p = parent(i);\n        // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n        if (p < 0 || maxHeap[i] <= maxHeap[p])\n            break;\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        swap(maxHeap[i], maxHeap[p]);\n        // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n        i = p;\n    }\n}\n
        my_heap.java
        /* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n    // \u6dfb\u52a0\u8282\u70b9\n    maxHeap.add(val);\n    // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n    siftUp(size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\n    while (true) {\n        // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n        int p = parent(i);\n        // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n        if (p < 0 || maxHeap.get(i) <= maxHeap.get(p))\n            break;\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        swap(i, p);\n        // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n        i = p;\n    }\n}\n
        my_heap.cs
        /* \u5143\u7d20\u5165\u5806 */\nvoid Push(int val) {\n    // \u6dfb\u52a0\u8282\u70b9\n    maxHeap.Add(val);\n    // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n    SiftUp(Size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid SiftUp(int i) {\n    while (true) {\n        // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n        int p = Parent(i);\n        // \u82e5\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\uff0c\u5219\u7ed3\u675f\u5806\u5316\n        if (p < 0 || maxHeap[i] <= maxHeap[p])\n            break;\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        Swap(i, p);\n        // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n        i = p;\n    }\n}\n
        my_heap.go
        /* \u5143\u7d20\u5165\u5806 */\nfunc (h *maxHeap) push(val any) {\n    // \u6dfb\u52a0\u8282\u70b9\n    h.data = append(h.data, val)\n    // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n    h.siftUp(len(h.data) - 1)\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfunc (h *maxHeap) siftUp(i int) {\n    for true {\n        // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n        p := h.parent(i)\n        // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n        if p < 0 || h.data[i].(int) <= h.data[p].(int) {\n            break\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        h.swap(i, p)\n        // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n        i = p\n    }\n}\n
        my_heap.swift
        /* \u5143\u7d20\u5165\u5806 */\nfunc push(val: Int) {\n    // \u6dfb\u52a0\u8282\u70b9\n    maxHeap.append(val)\n    // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n    siftUp(i: size() - 1)\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfunc siftUp(i: Int) {\n    var i = i\n    while true {\n        // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n        let p = parent(i: i)\n        // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n        if p < 0 || maxHeap[i] <= maxHeap[p] {\n            break\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        swap(i: i, j: p)\n        // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n        i = p\n    }\n}\n
        my_heap.js
        /* \u5143\u7d20\u5165\u5806 */\npush(val) {\n    // \u6dfb\u52a0\u8282\u70b9\n    this.#maxHeap.push(val);\n    // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n    this.#siftUp(this.size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\n#siftUp(i) {\n    while (true) {\n        // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n        const p = this.#parent(i);\n        // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n        if (p < 0 || this.#maxHeap[i] <= this.#maxHeap[p]) break;\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        this.#swap(i, p);\n        // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n        i = p;\n    }\n}\n
        my_heap.ts
        /* \u5143\u7d20\u5165\u5806 */\npush(val: number): void {\n    // \u6dfb\u52a0\u8282\u70b9\n    this.maxHeap.push(val);\n    // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n    this.siftUp(this.size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nsiftUp(i: number): void {\n    while (true) {\n        // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n        const p = this.parent(i);\n        // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n        if (p < 0 || this.maxHeap[i] <= this.maxHeap[p]) break;\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        this.swap(i, p);\n        // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n        i = p;\n    }\n}\n
        my_heap.dart
        /* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n  // \u6dfb\u52a0\u8282\u70b9\n  _maxHeap.add(val);\n  // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n  siftUp(size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\n  while (true) {\n    // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n    int p = _parent(i);\n    // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n    if (p < 0 || _maxHeap[i] <= _maxHeap[p]) {\n      break;\n    }\n    // \u4ea4\u6362\u4e24\u8282\u70b9\n    _swap(i, p);\n    // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n    i = p;\n  }\n}\n
        my_heap.rs
        /* \u5143\u7d20\u5165\u5806 */\nfn push(&mut self, val: i32) {\n    // \u6dfb\u52a0\u8282\u70b9\n    self.max_heap.push(val);\n    // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n    self.sift_up(self.size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfn sift_up(&mut self, mut i: usize) {\n    loop {\n        // \u8282\u70b9 i \u5df2\u7ecf\u662f\u5806\u9876\u8282\u70b9\u4e86\uff0c\u7ed3\u675f\u5806\u5316\n        if i == 0 {\n            break;\n        }\n        // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n        let p = Self::parent(i);\n        // \u5f53\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n        if self.max_heap[i] <= self.max_heap[p] {\n            break;\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        self.swap(i, p);\n        // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n        i = p;\n    }\n}\n
        my_heap.c
        /* \u5143\u7d20\u5165\u5806 */\nvoid push(MaxHeap *maxHeap, int val) {\n    // \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4e0d\u5e94\u8be5\u6dfb\u52a0\u8fd9\u4e48\u591a\u8282\u70b9\n    if (maxHeap->size == MAX_SIZE) {\n        printf(\"heap is full!\");\n        return;\n    }\n    // \u6dfb\u52a0\u8282\u70b9\n    maxHeap->data[maxHeap->size] = val;\n    maxHeap->size++;\n\n    // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n    siftUp(maxHeap, maxHeap->size - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(MaxHeap *maxHeap, int i) {\n    while (true) {\n        // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n        int p = parent(maxHeap, i);\n        // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n        if (p < 0 || maxHeap->data[i] <= maxHeap->data[p]) {\n            break;\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        swap(maxHeap, i, p);\n        // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n        i = p;\n    }\n}\n
        my_heap.kt
        /* \u5143\u7d20\u5165\u5806 */\nfun push(_val: Int) {\n    // \u6dfb\u52a0\u8282\u70b9\n    maxHeap.add(_val)\n    // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n    siftUp(size() - 1)\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfun siftUp(it: Int) {\n    // Kotlin\u7684\u51fd\u6570\u53c2\u6570\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u521b\u5efa\u4e34\u65f6\u53d8\u91cf\n    var i = it\n    while (true) {\n        // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n        val p = parent(i)\n        // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n        if (p < 0 || maxHeap[i] <= maxHeap[p]) break\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        swap(i, p)\n        // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n        i = p\n    }\n}\n
        my_heap.rb
        ### \u5143\u7d20\u5165\u5806 ###\ndef push(val)\n  # \u6dfb\u52a0\u8282\u70b9\n  @max_heap << val\n  # \u4ece\u5e95\u81f3\u9876\u5806\u5316\n  sift_up(size - 1)\nend\n\n### \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 ###\ndef sift_up(i)\n  loop do\n    # \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n    p = parent(i)\n    # \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n    break if p < 0 || @max_heap[i] <= @max_heap[p]\n    # \u4ea4\u6362\u4e24\u8282\u70b9\n    swap(i, p)\n    # \u5faa\u73af\u5411\u4e0a\u5806\u5316\n    i = p\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_heap/heap/#4","title":"4. \u00a0 \u5806\u9876\u5143\u7d20\u51fa\u5806","text":"

        \u5806\u9876\u5143\u7d20\u662f\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\uff0c\u5373\u5217\u8868\u9996\u5143\u7d20\u3002\u5982\u679c\u6211\u4eec\u76f4\u63a5\u4ece\u5217\u8868\u4e2d\u5220\u9664\u9996\u5143\u7d20\uff0c\u90a3\u4e48\u4e8c\u53c9\u6811\u4e2d\u6240\u6709\u8282\u70b9\u7684\u7d22\u5f15\u90fd\u4f1a\u53d1\u751f\u53d8\u5316\uff0c\u8fd9\u5c06\u4f7f\u5f97\u540e\u7eed\u4f7f\u7528\u5806\u5316\u8fdb\u884c\u4fee\u590d\u53d8\u5f97\u56f0\u96be\u3002\u4e3a\u4e86\u5c3d\u91cf\u51cf\u5c11\u5143\u7d20\u7d22\u5f15\u7684\u53d8\u52a8\uff0c\u6211\u4eec\u91c7\u7528\u4ee5\u4e0b\u64cd\u4f5c\u6b65\u9aa4\u3002

        1. \u4ea4\u6362\u5806\u9876\u5143\u7d20\u4e0e\u5806\u5e95\u5143\u7d20\uff08\u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff09\u3002
        2. \u4ea4\u6362\u5b8c\u6210\u540e\uff0c\u5c06\u5806\u5e95\u4ece\u5217\u8868\u4e2d\u5220\u9664\uff08\u6ce8\u610f\uff0c\u7531\u4e8e\u5df2\u7ecf\u4ea4\u6362\uff0c\u56e0\u6b64\u5b9e\u9645\u4e0a\u5220\u9664\u7684\u662f\u539f\u6765\u7684\u5806\u9876\u5143\u7d20\uff09\u3002
        3. \u4ece\u6839\u8282\u70b9\u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u6267\u884c\u5806\u5316\u3002

        \u5982\u56fe 8-4 \u6240\u793a\uff0c\u201c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u201d\u7684\u64cd\u4f5c\u65b9\u5411\u4e0e\u201c\u4ece\u5e95\u81f3\u9876\u5806\u5316\u201d\u76f8\u53cd\uff0c\u6211\u4eec\u5c06\u6839\u8282\u70b9\u7684\u503c\u4e0e\u5176\u4e24\u4e2a\u5b50\u8282\u70b9\u7684\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u5c06\u6700\u5927\u7684\u5b50\u8282\u70b9\u4e0e\u6839\u8282\u70b9\u4ea4\u6362\u3002\u7136\u540e\u5faa\u73af\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u76f4\u5230\u8d8a\u8fc7\u53f6\u8282\u70b9\u6216\u9047\u5230\u65e0\u987b\u4ea4\u6362\u7684\u8282\u70b9\u65f6\u7ed3\u675f\u3002

        <1><2><3><4><5><6><7><8><9><10>

        \u56fe 8-4 \u00a0 \u5806\u9876\u5143\u7d20\u51fa\u5806\u6b65\u9aa4

        \u4e0e\u5143\u7d20\u5165\u5806\u64cd\u4f5c\u76f8\u4f3c\uff0c\u5806\u9876\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4e3a \\(O(\\log n)\\) \u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
        def pop(self) -> int:\n    \"\"\"\u5143\u7d20\u51fa\u5806\"\"\"\n    # \u5224\u7a7a\u5904\u7406\n    if self.is_empty():\n        raise IndexError(\"\u5806\u4e3a\u7a7a\")\n    # \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    self.swap(0, self.size() - 1)\n    # \u5220\u9664\u8282\u70b9\n    val = self.max_heap.pop()\n    # \u4ece\u9876\u81f3\u5e95\u5806\u5316\n    self.sift_down(0)\n    # \u8fd4\u56de\u5806\u9876\u5143\u7d20\n    return val\n\ndef sift_down(self, i: int):\n    \"\"\"\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\"\"\"\n    while True:\n        # \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        l, r, ma = self.left(i), self.right(i), i\n        if l < self.size() and self.max_heap[l] > self.max_heap[ma]:\n            ma = l\n        if r < self.size() and self.max_heap[r] > self.max_heap[ma]:\n            ma = r\n        # \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if ma == i:\n            break\n        # \u4ea4\u6362\u4e24\u8282\u70b9\n        self.swap(i, ma)\n        # \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma\n
        my_heap.cpp
        /* \u5143\u7d20\u51fa\u5806 */\nvoid pop() {\n    // \u5224\u7a7a\u5904\u7406\n    if (isEmpty()) {\n        throw out_of_range(\"\u5806\u4e3a\u7a7a\");\n    }\n    // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    swap(maxHeap[0], maxHeap[size() - 1]);\n    // \u5220\u9664\u8282\u70b9\n    maxHeap.pop_back();\n    // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n    siftDown(0);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        int l = left(i), r = right(i), ma = i;\n        if (l < size() && maxHeap[l] > maxHeap[ma])\n            ma = l;\n        if (r < size() && maxHeap[r] > maxHeap[ma])\n            ma = r;\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma == i)\n            break;\n        swap(maxHeap[i], maxHeap[ma]);\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n
        my_heap.java
        /* \u5143\u7d20\u51fa\u5806 */\nint pop() {\n    // \u5224\u7a7a\u5904\u7406\n    if (isEmpty())\n        throw new IndexOutOfBoundsException();\n    // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    swap(0, size() - 1);\n    // \u5220\u9664\u8282\u70b9\n    int val = maxHeap.remove(size() - 1);\n    // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n    siftDown(0);\n    // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n    return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        int l = left(i), r = right(i), ma = i;\n        if (l < size() && maxHeap.get(l) > maxHeap.get(ma))\n            ma = l;\n        if (r < size() && maxHeap.get(r) > maxHeap.get(ma))\n            ma = r;\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma == i)\n            break;\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        swap(i, ma);\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n
        my_heap.cs
        /* \u5143\u7d20\u51fa\u5806 */\nint Pop() {\n    // \u5224\u7a7a\u5904\u7406\n    if (IsEmpty())\n        throw new IndexOutOfRangeException();\n    // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    Swap(0, Size() - 1);\n    // \u5220\u9664\u8282\u70b9\n    int val = maxHeap.Last();\n    maxHeap.RemoveAt(Size() - 1);\n    // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n    SiftDown(0);\n    // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n    return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid SiftDown(int i) {\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        int l = Left(i), r = Right(i), ma = i;\n        if (l < Size() && maxHeap[l] > maxHeap[ma])\n            ma = l;\n        if (r < Size() && maxHeap[r] > maxHeap[ma])\n            ma = r;\n        // \u82e5\u201c\u8282\u70b9 i \u6700\u5927\u201d\u6216\u201c\u8d8a\u8fc7\u53f6\u8282\u70b9\u201d\uff0c\u5219\u7ed3\u675f\u5806\u5316\n        if (ma == i) break;\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        Swap(i, ma);\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n
        my_heap.go
        /* \u5143\u7d20\u51fa\u5806 */\nfunc (h *maxHeap) pop() any {\n    // \u5224\u7a7a\u5904\u7406\n    if h.isEmpty() {\n        fmt.Println(\"error\")\n        return nil\n    }\n    // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    h.swap(0, h.size()-1)\n    // \u5220\u9664\u8282\u70b9\n    val := h.data[len(h.data)-1]\n    h.data = h.data[:len(h.data)-1]\n    // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n    h.siftDown(0)\n\n    // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n    return val\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc (h *maxHeap) siftDown(i int) {\n    for true {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a max\n        l, r, max := h.left(i), h.right(i), i\n        if l < h.size() && h.data[l].(int) > h.data[max].(int) {\n            max = l\n        }\n        if r < h.size() && h.data[r].(int) > h.data[max].(int) {\n            max = r\n        }\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if max == i {\n            break\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        h.swap(i, max)\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = max\n    }\n}\n
        my_heap.swift
        /* \u5143\u7d20\u51fa\u5806 */\nfunc pop() -> Int {\n    // \u5224\u7a7a\u5904\u7406\n    if isEmpty() {\n        fatalError(\"\u5806\u4e3a\u7a7a\")\n    }\n    // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    swap(i: 0, j: size() - 1)\n    // \u5220\u9664\u8282\u70b9\n    let val = maxHeap.remove(at: size() - 1)\n    // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n    siftDown(i: 0)\n    // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n    return val\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc siftDown(i: Int) {\n    var i = i\n    while true {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        let l = left(i: i)\n        let r = right(i: i)\n        var ma = i\n        if l < size(), maxHeap[l] > maxHeap[ma] {\n            ma = l\n        }\n        if r < size(), maxHeap[r] > maxHeap[ma] {\n            ma = r\n        }\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if ma == i {\n            break\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        swap(i: i, j: ma)\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma\n    }\n}\n
        my_heap.js
        /* \u5143\u7d20\u51fa\u5806 */\npop() {\n    // \u5224\u7a7a\u5904\u7406\n    if (this.isEmpty()) throw new Error('\u5806\u4e3a\u7a7a');\n    // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    this.#swap(0, this.size() - 1);\n    // \u5220\u9664\u8282\u70b9\n    const val = this.#maxHeap.pop();\n    // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n    this.#siftDown(0);\n    // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n    return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\n#siftDown(i) {\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        const l = this.#left(i),\n            r = this.#right(i);\n        let ma = i;\n        if (l < this.size() && this.#maxHeap[l] > this.#maxHeap[ma]) ma = l;\n        if (r < this.size() && this.#maxHeap[r] > this.#maxHeap[ma]) ma = r;\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma === i) break;\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        this.#swap(i, ma);\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n
        my_heap.ts
        /* \u5143\u7d20\u51fa\u5806 */\npop(): number {\n    // \u5224\u7a7a\u5904\u7406\n    if (this.isEmpty()) throw new RangeError('Heap is empty.');\n    // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    this.swap(0, this.size() - 1);\n    // \u5220\u9664\u8282\u70b9\n    const val = this.maxHeap.pop();\n    // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n    this.siftDown(0);\n    // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n    return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nsiftDown(i: number): void {\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        const l = this.left(i),\n            r = this.right(i);\n        let ma = i;\n        if (l < this.size() && this.maxHeap[l] > this.maxHeap[ma]) ma = l;\n        if (r < this.size() && this.maxHeap[r] > this.maxHeap[ma]) ma = r;\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma === i) break;\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        this.swap(i, ma);\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n
        my_heap.dart
        /* \u5143\u7d20\u51fa\u5806 */\nint pop() {\n  // \u5224\u7a7a\u5904\u7406\n  if (isEmpty()) throw Exception('\u5806\u4e3a\u7a7a');\n  // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n  _swap(0, size() - 1);\n  // \u5220\u9664\u8282\u70b9\n  int val = _maxHeap.removeLast();\n  // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n  siftDown(0);\n  // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n  return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\n  while (true) {\n    // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n    int l = _left(i);\n    int r = _right(i);\n    int ma = i;\n    if (l < size() && _maxHeap[l] > _maxHeap[ma]) ma = l;\n    if (r < size() && _maxHeap[r] > _maxHeap[ma]) ma = r;\n    // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n    if (ma == i) break;\n    // \u4ea4\u6362\u4e24\u8282\u70b9\n    _swap(i, ma);\n    // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n    i = ma;\n  }\n}\n
        my_heap.rs
        /* \u5143\u7d20\u51fa\u5806 */\nfn pop(&mut self) -> i32 {\n    // \u5224\u7a7a\u5904\u7406\n    if self.is_empty() {\n        panic!(\"index out of bounds\");\n    }\n    // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    self.swap(0, self.size() - 1);\n    // \u5220\u9664\u8282\u70b9\n    let val = self.max_heap.pop().unwrap();\n    // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n    self.sift_down(0);\n    // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n    val\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfn sift_down(&mut self, mut i: usize) {\n    loop {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        let (l, r, mut ma) = (Self::left(i), Self::right(i), i);\n        if l < self.size() && self.max_heap[l] > self.max_heap[ma] {\n            ma = l;\n        }\n        if r < self.size() && self.max_heap[r] > self.max_heap[ma] {\n            ma = r;\n        }\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if ma == i {\n            break;\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        self.swap(i, ma);\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n
        my_heap.c
        /* \u5143\u7d20\u51fa\u5806 */\nint pop(MaxHeap *maxHeap) {\n    // \u5224\u7a7a\u5904\u7406\n    if (isEmpty(maxHeap)) {\n        printf(\"heap is empty!\");\n        return INT_MAX;\n    }\n    // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    swap(maxHeap, 0, size(maxHeap) - 1);\n    // \u5220\u9664\u8282\u70b9\n    int val = maxHeap->data[maxHeap->size - 1];\n    maxHeap->size--;\n    // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n    siftDown(maxHeap, 0);\n\n    // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n    return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(MaxHeap *maxHeap, int i) {\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a max\n        int l = left(maxHeap, i);\n        int r = right(maxHeap, i);\n        int max = i;\n        if (l < size(maxHeap) && maxHeap->data[l] > maxHeap->data[max]) {\n            max = l;\n        }\n        if (r < size(maxHeap) && maxHeap->data[r] > maxHeap->data[max]) {\n            max = r;\n        }\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (max == i) {\n            break;\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        swap(maxHeap, i, max);\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = max;\n    }\n}\n
        my_heap.kt
        /* \u5143\u7d20\u51fa\u5806 */\nfun pop(): Int {\n    // \u5224\u7a7a\u5904\u7406\n    if (isEmpty()) throw IndexOutOfBoundsException()\n    // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    swap(0, size() - 1)\n    // \u5220\u9664\u8282\u70b9\n    val _val = maxHeap.removeAt(size() - 1)\n    // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n    siftDown(0)\n    // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n    return _val\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfun siftDown(it: Int) {\n    // Kotlin\u7684\u51fd\u6570\u53c2\u6570\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u521b\u5efa\u4e34\u65f6\u53d8\u91cf\n    var i = it\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        val l = left(i)\n        val r = right(i)\n        var ma = i\n        if (l < size() && maxHeap[l] > maxHeap[ma]) ma = l\n        if (r < size() && maxHeap[r] > maxHeap[ma]) ma = r\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma == i) break\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        swap(i, ma)\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma\n    }\n}\n
        my_heap.rb
        ### \u5143\u7d20\u51fa\u5806 ###\ndef pop\n  # \u5224\u7a7a\u5904\u7406\n  raise IndexError, \"\u5806\u4e3a\u7a7a\" if is_empty?\n  # \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n  swap(0, size - 1)\n  # \u5220\u9664\u8282\u70b9\n  val = @max_heap.pop\n  # \u4ece\u9876\u81f3\u5e95\u5806\u5316\n  sift_down(0)\n  # \u8fd4\u56de\u5806\u9876\u5143\u7d20\n  val\nend\n\n### \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 ###\ndef sift_down(i)\n  loop do\n    # \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n    l, r, ma = left(i), right(i), i\n    ma = l if l < size && @max_heap[l] > @max_heap[ma]\n    ma = r if r < size && @max_heap[r] > @max_heap[ma]\n\n    # \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n    break if ma == i\n\n    # \u4ea4\u6362\u4e24\u8282\u70b9\n    swap(i, ma)\n    # \u5faa\u73af\u5411\u4e0b\u5806\u5316\n    i = ma\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_heap/heap/#813","title":"8.1.3 \u00a0 \u5806\u7684\u5e38\u89c1\u5e94\u7528","text":"
        • \u4f18\u5148\u961f\u5217\uff1a\u5806\u901a\u5e38\u4f5c\u4e3a\u5b9e\u73b0\u4f18\u5148\u961f\u5217\u7684\u9996\u9009\u6570\u636e\u7ed3\u6784\uff0c\u5176\u5165\u961f\u548c\u51fa\u961f\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(\\log n)\\) \uff0c\u800c\u5efa\u5806\u64cd\u4f5c\u4e3a \\(O(n)\\) \uff0c\u8fd9\u4e9b\u64cd\u4f5c\u90fd\u975e\u5e38\u9ad8\u6548\u3002
        • \u5806\u6392\u5e8f\uff1a\u7ed9\u5b9a\u4e00\u7ec4\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u5b83\u4eec\u5efa\u7acb\u4e00\u4e2a\u5806\uff0c\u7136\u540e\u4e0d\u65ad\u5730\u6267\u884c\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\uff0c\u4ece\u800c\u5f97\u5230\u6709\u5e8f\u6570\u636e\u3002\u7136\u800c\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528\u4e00\u79cd\u66f4\u4f18\u96c5\u7684\u65b9\u5f0f\u5b9e\u73b0\u5806\u6392\u5e8f\uff0c\u8be6\u89c1\u201c\u5806\u6392\u5e8f\u201d\u7ae0\u8282\u3002
        • \u83b7\u53d6\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\uff1a\u8fd9\u662f\u4e00\u4e2a\u7ecf\u5178\u7684\u7b97\u6cd5\u95ee\u9898\uff0c\u540c\u65f6\u4e5f\u662f\u4e00\u79cd\u5178\u578b\u5e94\u7528\uff0c\u4f8b\u5982\u9009\u62e9\u70ed\u5ea6\u524d 10 \u7684\u65b0\u95fb\u4f5c\u4e3a\u5fae\u535a\u70ed\u641c\uff0c\u9009\u53d6\u9500\u91cf\u524d 10 \u7684\u5546\u54c1\u7b49\u3002
        "},{"location":"chapter_heap/summary/","title":"8.4 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_heap/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u5806\u662f\u4e00\u68f5\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u6839\u636e\u6210\u7acb\u6761\u4ef6\u53ef\u5206\u4e3a\u5927\u9876\u5806\u548c\u5c0f\u9876\u5806\u3002\u5927\uff08\u5c0f\uff09\u9876\u5806\u7684\u5806\u9876\u5143\u7d20\u662f\u6700\u5927\uff08\u5c0f\uff09\u7684\u3002
        • \u4f18\u5148\u961f\u5217\u7684\u5b9a\u4e49\u662f\u5177\u6709\u51fa\u961f\u4f18\u5148\u7ea7\u7684\u961f\u5217\uff0c\u901a\u5e38\u4f7f\u7528\u5806\u6765\u5b9e\u73b0\u3002
        • \u5806\u7684\u5e38\u7528\u64cd\u4f5c\u53ca\u5176\u5bf9\u5e94\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5305\u62ec\uff1a\u5143\u7d20\u5165\u5806 \\(O(\\log n)\\)\u3001\u5806\u9876\u5143\u7d20\u51fa\u5806 \\(O(\\log n)\\) \u548c\u8bbf\u95ee\u5806\u9876\u5143\u7d20 \\(O(1)\\) \u7b49\u3002
        • \u5b8c\u5168\u4e8c\u53c9\u6811\u975e\u5e38\u9002\u5408\u7528\u6570\u7ec4\u8868\u793a\uff0c\u56e0\u6b64\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u6570\u7ec4\u6765\u5b58\u50a8\u5806\u3002
        • \u5806\u5316\u64cd\u4f5c\u7528\u4e8e\u7ef4\u62a4\u5806\u7684\u6027\u8d28\uff0c\u5728\u5165\u5806\u548c\u51fa\u5806\u64cd\u4f5c\u4e2d\u90fd\u4f1a\u7528\u5230\u3002
        • \u8f93\u5165 \\(n\\) \u4e2a\u5143\u7d20\u5e76\u5efa\u5806\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f18\u5316\u81f3 \\(O(n)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002
        • Top-k \u662f\u4e00\u4e2a\u7ecf\u5178\u7b97\u6cd5\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u5806\u6570\u636e\u7ed3\u6784\u9ad8\u6548\u89e3\u51b3\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log k)\\) \u3002
        "},{"location":"chapter_heap/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u6570\u636e\u7ed3\u6784\u7684\u201c\u5806\u201d\u4e0e\u5185\u5b58\u7ba1\u7406\u7684\u201c\u5806\u201d\u662f\u540c\u4e00\u4e2a\u6982\u5ff5\u5417\uff1f

        \u4e24\u8005\u4e0d\u662f\u540c\u4e00\u4e2a\u6982\u5ff5\uff0c\u53ea\u662f\u78b0\u5de7\u90fd\u53eb\u201c\u5806\u201d\u3002\u8ba1\u7b97\u673a\u7cfb\u7edf\u5185\u5b58\u4e2d\u7684\u5806\u662f\u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u4e00\u90e8\u5206\uff0c\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5b58\u50a8\u6570\u636e\u3002\u7a0b\u5e8f\u53ef\u4ee5\u8bf7\u6c42\u4e00\u5b9a\u91cf\u7684\u5806\u5185\u5b58\uff0c\u7528\u4e8e\u5b58\u50a8\u5982\u5bf9\u8c61\u548c\u6570\u7ec4\u7b49\u590d\u6742\u7ed3\u6784\u3002\u5f53\u8fd9\u4e9b\u6570\u636e\u4e0d\u518d\u9700\u8981\u65f6\uff0c\u7a0b\u5e8f\u9700\u8981\u91ca\u653e\u8fd9\u4e9b\u5185\u5b58\uff0c\u4ee5\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\u3002\u76f8\u8f83\u4e8e\u6808\u5185\u5b58\uff0c\u5806\u5185\u5b58\u7684\u7ba1\u7406\u548c\u4f7f\u7528\u9700\u8981\u66f4\u8c28\u614e\uff0c\u4f7f\u7528\u4e0d\u5f53\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u548c\u91ce\u6307\u9488\u7b49\u95ee\u9898\u3002

        "},{"location":"chapter_heap/top_k/","title":"8.3 \u00a0 Top-k \u95ee\u9898","text":"

        Question

        \u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u65e0\u5e8f\u6570\u7ec4 nums \uff0c\u8bf7\u8fd4\u56de\u6570\u7ec4\u4e2d\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\u3002

        \u5bf9\u4e8e\u8be5\u95ee\u9898\uff0c\u6211\u4eec\u5148\u4ecb\u7ecd\u4e24\u79cd\u601d\u8def\u6bd4\u8f83\u76f4\u63a5\u7684\u89e3\u6cd5\uff0c\u518d\u4ecb\u7ecd\u6548\u7387\u66f4\u9ad8\u7684\u5806\u89e3\u6cd5\u3002

        "},{"location":"chapter_heap/top_k/#831","title":"8.3.1 \u00a0 \u65b9\u6cd5\u4e00\uff1a\u904d\u5386\u9009\u62e9","text":"

        \u6211\u4eec\u53ef\u4ee5\u8fdb\u884c\u56fe 8-6 \u6240\u793a\u7684 \\(k\\) \u8f6e\u904d\u5386\uff0c\u5206\u522b\u5728\u6bcf\u8f6e\u4e2d\u63d0\u53d6\u7b2c \\(1\\)\u3001\\(2\\)\u3001\\(\\dots\\)\u3001\\(k\\) \u5927\u7684\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nk)\\) \u3002

        \u6b64\u65b9\u6cd5\u53ea\u9002\u7528\u4e8e \\(k \\ll n\\) \u7684\u60c5\u51b5\uff0c\u56e0\u4e3a\u5f53 \\(k\\) \u4e0e \\(n\\) \u6bd4\u8f83\u63a5\u8fd1\u65f6\uff0c\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u5411\u4e8e \\(O(n^2)\\) \uff0c\u975e\u5e38\u8017\u65f6\u3002

        \u56fe 8-6 \u00a0 \u904d\u5386\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20

        Tip

        \u5f53 \\(k = n\\) \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u5b8c\u6574\u7684\u6709\u5e8f\u5e8f\u5217\uff0c\u6b64\u65f6\u7b49\u4ef7\u4e8e\u201c\u9009\u62e9\u6392\u5e8f\u201d\u7b97\u6cd5\u3002

        "},{"location":"chapter_heap/top_k/#832","title":"8.3.2 \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u6392\u5e8f","text":"

        \u5982\u56fe 8-7 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u5bf9\u6570\u7ec4 nums \u8fdb\u884c\u6392\u5e8f\uff0c\u518d\u8fd4\u56de\u6700\u53f3\u8fb9\u7684 \\(k\\) \u4e2a\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002

        \u663e\u7136\uff0c\u8be5\u65b9\u6cd5\u201c\u8d85\u989d\u201d\u5b8c\u6210\u4efb\u52a1\u4e86\uff0c\u56e0\u4e3a\u6211\u4eec\u53ea\u9700\u627e\u51fa\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\u5373\u53ef\uff0c\u800c\u4e0d\u9700\u8981\u6392\u5e8f\u5176\u4ed6\u5143\u7d20\u3002

        \u56fe 8-7 \u00a0 \u6392\u5e8f\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20

        "},{"location":"chapter_heap/top_k/#833","title":"8.3.3 \u00a0 \u65b9\u6cd5\u4e09\uff1a\u5806","text":"

        \u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u5806\u66f4\u52a0\u9ad8\u6548\u5730\u89e3\u51b3 Top-k \u95ee\u9898\uff0c\u6d41\u7a0b\u5982\u56fe 8-8 \u6240\u793a\u3002

        1. \u521d\u59cb\u5316\u4e00\u4e2a\u5c0f\u9876\u5806\uff0c\u5176\u5806\u9876\u5143\u7d20\u6700\u5c0f\u3002
        2. \u5148\u5c06\u6570\u7ec4\u7684\u524d \\(k\\) \u4e2a\u5143\u7d20\u4f9d\u6b21\u5165\u5806\u3002
        3. \u4ece\u7b2c \\(k + 1\\) \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\uff0c\u5e76\u5c06\u5f53\u524d\u5143\u7d20\u5165\u5806\u3002
        4. \u904d\u5386\u5b8c\u6210\u540e\uff0c\u5806\u4e2d\u4fdd\u5b58\u7684\u5c31\u662f\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\u3002
        <1><2><3><4><5><6><7><8><9>

        \u56fe 8-8 \u00a0 \u57fa\u4e8e\u5806\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20

        \u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby top_k.py
        def top_k_heap(nums: list[int], k: int) -> list[int]:\n    \"\"\"\u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20\"\"\"\n    # \u521d\u59cb\u5316\u5c0f\u9876\u5806\n    heap = []\n    # \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n    for i in range(k):\n        heapq.heappush(heap, nums[i])\n    # \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n    for i in range(k, len(nums)):\n        # \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n        if nums[i] > heap[0]:\n            heapq.heappop(heap)\n            heapq.heappush(heap, nums[i])\n    return heap\n
        top_k.cpp
        /* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\npriority_queue<int, vector<int>, greater<int>> topKHeap(vector<int> &nums, int k) {\n    // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n    priority_queue<int, vector<int>, greater<int>> heap;\n    // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n    for (int i = 0; i < k; i++) {\n        heap.push(nums[i]);\n    }\n    // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n    for (int i = k; i < nums.size(); i++) {\n        // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n        if (nums[i] > heap.top()) {\n            heap.pop();\n            heap.push(nums[i]);\n        }\n    }\n    return heap;\n}\n
        top_k.java
        /* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nQueue<Integer> topKHeap(int[] nums, int k) {\n    // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n    Queue<Integer> heap = new PriorityQueue<Integer>();\n    // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n    for (int i = 0; i < k; i++) {\n        heap.offer(nums[i]);\n    }\n    // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n    for (int i = k; i < nums.length; i++) {\n        // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n        if (nums[i] > heap.peek()) {\n            heap.poll();\n            heap.offer(nums[i]);\n        }\n    }\n    return heap;\n}\n
        top_k.cs
        /* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nPriorityQueue<int, int> TopKHeap(int[] nums, int k) {\n    // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n    PriorityQueue<int, int> heap = new();\n    // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n    for (int i = 0; i < k; i++) {\n        heap.Enqueue(nums[i], nums[i]);\n    }\n    // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n    for (int i = k; i < nums.Length; i++) {\n        // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n        if (nums[i] > heap.Peek()) {\n            heap.Dequeue();\n            heap.Enqueue(nums[i], nums[i]);\n        }\n    }\n    return heap;\n}\n
        top_k.go
        /* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunc topKHeap(nums []int, k int) *minHeap {\n    // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n    h := &minHeap{}\n    heap.Init(h)\n    // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n    for i := 0; i < k; i++ {\n        heap.Push(h, nums[i])\n    }\n    // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n    for i := k; i < len(nums); i++ {\n        // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n        if nums[i] > h.Top().(int) {\n            heap.Pop(h)\n            heap.Push(h, nums[i])\n        }\n    }\n    return h\n}\n
        top_k.swift
        /* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunc topKHeap(nums: [Int], k: Int) -> [Int] {\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5c0f\u9876\u5806\uff0c\u5e76\u5c06\u524d k \u4e2a\u5143\u7d20\u5efa\u5806\n    var heap = Heap(nums.prefix(k))\n    // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n    for i in nums.indices.dropFirst(k) {\n        // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n        if nums[i] > heap.min()! {\n            _ = heap.removeMin()\n            heap.insert(nums[i])\n        }\n    }\n    return heap.unordered\n}\n
        top_k.js
        /* \u5143\u7d20\u5165\u5806 */\nfunction pushMinHeap(maxHeap, val) {\n    // \u5143\u7d20\u53d6\u53cd\n    maxHeap.push(-val);\n}\n\n/* \u5143\u7d20\u51fa\u5806 */\nfunction popMinHeap(maxHeap) {\n    // \u5143\u7d20\u53d6\u53cd\n    return -maxHeap.pop();\n}\n\n/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunction peekMinHeap(maxHeap) {\n    // \u5143\u7d20\u53d6\u53cd\n    return -maxHeap.peek();\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nfunction getMinHeap(maxHeap) {\n    // \u5143\u7d20\u53d6\u53cd\n    return maxHeap.getMaxHeap().map((num) => -num);\n}\n\n/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunction topKHeap(nums, k) {\n    // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n    // \u8bf7\u6ce8\u610f\uff1a\u6211\u4eec\u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u7528\u5927\u9876\u5806\u6765\u6a21\u62df\u5c0f\u9876\u5806\n    const maxHeap = new MaxHeap([]);\n    // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n    for (let i = 0; i < k; i++) {\n        pushMinHeap(maxHeap, nums[i]);\n    }\n    // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n    for (let i = k; i < nums.length; i++) {\n        // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n        if (nums[i] > peekMinHeap(maxHeap)) {\n            popMinHeap(maxHeap);\n            pushMinHeap(maxHeap, nums[i]);\n        }\n    }\n    // \u8fd4\u56de\u5806\u4e2d\u5143\u7d20\n    return getMinHeap(maxHeap);\n}\n
        top_k.ts
        /* \u5143\u7d20\u5165\u5806 */\nfunction pushMinHeap(maxHeap: MaxHeap, val: number): void {\n    // \u5143\u7d20\u53d6\u53cd\n    maxHeap.push(-val);\n}\n\n/* \u5143\u7d20\u51fa\u5806 */\nfunction popMinHeap(maxHeap: MaxHeap): number {\n    // \u5143\u7d20\u53d6\u53cd\n    return -maxHeap.pop();\n}\n\n/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunction peekMinHeap(maxHeap: MaxHeap): number {\n    // \u5143\u7d20\u53d6\u53cd\n    return -maxHeap.peek();\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nfunction getMinHeap(maxHeap: MaxHeap): number[] {\n    // \u5143\u7d20\u53d6\u53cd\n    return maxHeap.getMaxHeap().map((num: number) => -num);\n}\n\n/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunction topKHeap(nums: number[], k: number): number[] {\n    // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n    // \u8bf7\u6ce8\u610f\uff1a\u6211\u4eec\u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u7528\u5927\u9876\u5806\u6765\u6a21\u62df\u5c0f\u9876\u5806\n    const maxHeap = new MaxHeap([]);\n    // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n    for (let i = 0; i < k; i++) {\n        pushMinHeap(maxHeap, nums[i]);\n    }\n    // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n    for (let i = k; i < nums.length; i++) {\n        // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n        if (nums[i] > peekMinHeap(maxHeap)) {\n            popMinHeap(maxHeap);\n            pushMinHeap(maxHeap, nums[i]);\n        }\n    }\n    // \u8fd4\u56de\u5806\u4e2d\u5143\u7d20\n    return getMinHeap(maxHeap);\n}\n
        top_k.dart
        /* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nMinHeap topKHeap(List<int> nums, int k) {\n  // \u521d\u59cb\u5316\u5c0f\u9876\u5806\uff0c\u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n  MinHeap heap = MinHeap(nums.sublist(0, k));\n  // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n  for (int i = k; i < nums.length; i++) {\n    // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n    if (nums[i] > heap.peek()) {\n      heap.pop();\n      heap.push(nums[i]);\n    }\n  }\n  return heap;\n}\n
        top_k.rs
        /* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfn top_k_heap(nums: Vec<i32>, k: usize) -> BinaryHeap<Reverse<i32>> {\n    // BinaryHeap \u662f\u5927\u9876\u5806\uff0c\u4f7f\u7528 Reverse \u5c06\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u5b9e\u73b0\u5c0f\u9876\u5806\n    let mut heap = BinaryHeap::<Reverse<i32>>::new();\n    // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n    for &num in nums.iter().take(k) {\n        heap.push(Reverse(num));\n    }\n    // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n    for &num in nums.iter().skip(k) {\n        // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n        if num > heap.peek().unwrap().0 {\n            heap.pop();\n            heap.push(Reverse(num));\n        }\n    }\n    heap\n}\n
        top_k.c
        /* \u5143\u7d20\u5165\u5806 */\nvoid pushMinHeap(MaxHeap *maxHeap, int val) {\n    // \u5143\u7d20\u53d6\u53cd\n    push(maxHeap, -val);\n}\n\n/* \u5143\u7d20\u51fa\u5806 */\nint popMinHeap(MaxHeap *maxHeap) {\n    // \u5143\u7d20\u53d6\u53cd\n    return -pop(maxHeap);\n}\n\n/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peekMinHeap(MaxHeap *maxHeap) {\n    // \u5143\u7d20\u53d6\u53cd\n    return -peek(maxHeap);\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nint *getMinHeap(MaxHeap *maxHeap) {\n    // \u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\u5e76\u5b58\u5165 res \u6570\u7ec4\n    int *res = (int *)malloc(maxHeap->size * sizeof(int));\n    for (int i = 0; i < maxHeap->size; i++) {\n        res[i] = -maxHeap->data[i];\n    }\n    return res;\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nint *getMinHeap(MaxHeap *maxHeap) {\n    // \u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\u5e76\u5b58\u5165 res \u6570\u7ec4\n    int *res = (int *)malloc(maxHeap->size * sizeof(int));\n    for (int i = 0; i < maxHeap->size; i++) {\n        res[i] = -maxHeap->data[i];\n    }\n    return res;\n}\n\n// \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20\u7684\u51fd\u6570\nint *topKHeap(int *nums, int sizeNums, int k) {\n    // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n    // \u8bf7\u6ce8\u610f\uff1a\u6211\u4eec\u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u7528\u5927\u9876\u5806\u6765\u6a21\u62df\u5c0f\u9876\u5806\n    int *empty = (int *)malloc(0);\n    MaxHeap *maxHeap = newMaxHeap(empty, 0);\n    // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n    for (int i = 0; i < k; i++) {\n        pushMinHeap(maxHeap, nums[i]);\n    }\n    // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n    for (int i = k; i < sizeNums; i++) {\n        // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n        if (nums[i] > peekMinHeap(maxHeap)) {\n            popMinHeap(maxHeap);\n            pushMinHeap(maxHeap, nums[i]);\n        }\n    }\n    int *res = getMinHeap(maxHeap);\n    // \u91ca\u653e\u5185\u5b58\n    delMaxHeap(maxHeap);\n    return res;\n}\n
        top_k.kt
        /* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfun topKHeap(nums: IntArray, k: Int): Queue<Int> {\n    // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n    val heap = PriorityQueue<Int>()\n    // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n    for (i in 0..<k) {\n        heap.offer(nums[i])\n    }\n    // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n    for (i in k..<nums.size) {\n        // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n        if (nums[i] > heap.peek()) {\n            heap.poll()\n            heap.offer(nums[i])\n        }\n    }\n    return heap\n}\n
        top_k.rb
        ### \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 ###\ndef top_k_heap(nums, k)\n  # \u521d\u59cb\u5316\u5c0f\u9876\u5806\n  # \u8bf7\u6ce8\u610f\uff1a\u6211\u4eec\u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u7528\u5927\u9876\u5806\u6765\u6a21\u62df\u5c0f\u9876\u5806\n  max_heap = MaxHeap.new([])\n\n  # \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n  for i in 0...k\n    push_min_heap(max_heap, nums[i])\n  end\n\n  # \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n  for i in k...nums.length\n    # \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n    if nums[i] > peek_min_heap(max_heap)\n      pop_min_heap(max_heap)\n      push_min_heap(max_heap, nums[i])\n    end\n  end\n\n  get_min_heap(max_heap)\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u603b\u5171\u6267\u884c\u4e86 \\(n\\) \u8f6e\u5165\u5806\u548c\u51fa\u5806\uff0c\u5806\u7684\u6700\u5927\u957f\u5ea6\u4e3a \\(k\\) \uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log k)\\) \u3002\u8be5\u65b9\u6cd5\u7684\u6548\u7387\u5f88\u9ad8\uff0c\u5f53 \\(k\\) \u8f83\u5c0f\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u5411 \\(O(n)\\) \uff1b\u5f53 \\(k\\) \u8f83\u5927\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u4f1a\u8d85\u8fc7 \\(O(n \\log n)\\) \u3002

        \u53e6\u5916\uff0c\u8be5\u65b9\u6cd5\u9002\u7528\u4e8e\u52a8\u6001\u6570\u636e\u6d41\u7684\u4f7f\u7528\u573a\u666f\u3002\u5728\u4e0d\u65ad\u52a0\u5165\u6570\u636e\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u6301\u7eed\u7ef4\u62a4\u5806\u5185\u7684\u5143\u7d20\uff0c\u4ece\u800c\u5b9e\u73b0\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\u7684\u52a8\u6001\u66f4\u65b0\u3002

        "},{"location":"chapter_hello_algo/","title":"\u5e8f","text":"

        \u51e0\u5e74\u524d\uff0c\u6211\u5728\u529b\u6263\u4e0a\u5206\u4eab\u4e86\u201c\u5251\u6307 Offer\u201d\u7cfb\u5217\u9898\u89e3\uff0c\u53d7\u5230\u4e86\u8bb8\u591a\u8bfb\u8005\u7684\u9f13\u52b1\u548c\u652f\u6301\u3002\u5728\u4e0e\u8bfb\u8005\u4ea4\u6d41\u671f\u95f4\uff0c\u6211\u6700\u5e38\u88ab\u95ee\u7684\u4e00\u4e2a\u95ee\u9898\u662f\u201c\u5982\u4f55\u5165\u95e8\u7b97\u6cd5\u201d\u3002\u9010\u6e10\u5730\uff0c\u6211\u5bf9\u8fd9\u4e2a\u95ee\u9898\u4ea7\u751f\u4e86\u6d53\u539a\u7684\u5174\u8da3\u3002

        \u4e24\u773c\u4e00\u62b9\u9ed1\u5730\u5237\u9898\u4f3c\u4e4e\u662f\u6700\u53d7\u6b22\u8fce\u7684\u65b9\u6cd5\uff0c\u7b80\u5355\u3001\u76f4\u63a5\u4e14\u6709\u6548\u3002\u7136\u800c\u5237\u9898\u5c31\u5982\u540c\u73a9\u201c\u626b\u96f7\u201d\u6e38\u620f\uff0c\u81ea\u5b66\u80fd\u529b\u5f3a\u7684\u4eba\u80fd\u591f\u987a\u5229\u5c06\u5730\u96f7\u9010\u4e2a\u6392\u6389\uff0c\u800c\u57fa\u7840\u4e0d\u8db3\u7684\u4eba\u5f88\u53ef\u80fd\u88ab\u70b8\u5f97\u6ee1\u5934\u662f\u5305\uff0c\u5e76\u5728\u632b\u6298\u4e2d\u6b65\u6b65\u9000\u7f29\u3002\u901a\u8bfb\u6559\u6750\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u505a\u6cd5\uff0c\u4f46\u5bf9\u4e8e\u9762\u5411\u6c42\u804c\u7684\u4eba\u6765\u8bf4\uff0c\u6bd5\u4e1a\u8bba\u6587\u3001\u6295\u9012\u7b80\u5386\u3001\u51c6\u5907\u7b14\u8bd5\u548c\u9762\u8bd5\u5df2\u7ecf\u6d88\u8017\u4e86\u5927\u90e8\u5206\u7cbe\u529b\uff0c\u5543\u539a\u91cd\u7684\u4e66\u5f80\u5f80\u53d8\u6210\u4e86\u4e00\u9879\u8270\u5de8\u7684\u6311\u6218\u3002

        \u5982\u679c\u4f60\u4e5f\u9762\u4e34\u7c7b\u4f3c\u7684\u56f0\u6270\uff0c\u90a3\u4e48\u5f88\u5e78\u8fd0\u8fd9\u672c\u4e66\u201c\u627e\u201d\u5230\u4e86\u4f60\u3002\u672c\u4e66\u662f\u6211\u5bf9\u8fd9\u4e2a\u95ee\u9898\u7ed9\u51fa\u7684\u7b54\u6848\uff0c\u5373\u4f7f\u4e0d\u662f\u6700\u4f18\u89e3\uff0c\u4e5f\u81f3\u5c11\u662f\u4e00\u6b21\u79ef\u6781\u7684\u5c1d\u8bd5\u3002\u672c\u4e66\u867d\u7136\u4e0d\u8db3\u4ee5\u8ba9\u4f60\u76f4\u63a5\u62ff\u5230 Offer\uff0c\u4f46\u4f1a\u5f15\u5bfc\u4f60\u63a2\u7d22\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u201c\u77e5\u8bc6\u5730\u56fe\u201d\uff0c\u5e26\u4f60\u4e86\u89e3\u4e0d\u540c\u201c\u5730\u96f7\u201d\u7684\u5f62\u72b6\u3001\u5927\u5c0f\u548c\u5206\u5e03\u4f4d\u7f6e\uff0c\u8ba9\u4f60\u638c\u63e1\u5404\u79cd\u201c\u6392\u96f7\u65b9\u6cd5\u201d\u3002\u6709\u4e86\u8fd9\u4e9b\u672c\u9886\uff0c\u76f8\u4fe1\u4f60\u53ef\u4ee5\u66f4\u52a0\u81ea\u5982\u5730\u5237\u9898\u548c\u9605\u8bfb\u6587\u732e\uff0c\u9010\u6b65\u6784\u5efa\u8d77\u5b8c\u6574\u7684\u77e5\u8bc6\u4f53\u7cfb\u3002

        \u6211\u6df1\u6df1\u8d5e\u540c\u8d39\u66fc\u6559\u6388\u6240\u8a00\uff1a\u201cKnowledge isn't free. You have to pay attention.\u201d\u4ece\u8fd9\u4e2a\u610f\u4e49\u4e0a\u770b\uff0c\u8fd9\u672c\u4e66\u5e76\u975e\u5b8c\u5168\u201c\u514d\u8d39\u201d\u3002\u4e3a\u4e86\u4e0d\u8f9c\u8d1f\u4f60\u4e3a\u672c\u4e66\u6240\u4ed8\u51fa\u7684\u5b9d\u8d35\u201c\u6ce8\u610f\u529b\u201d\uff0c\u6211\u4f1a\u7aed\u5c3d\u6240\u80fd\uff0c\u6295\u5165\u6700\u5927\u7684\u201c\u6ce8\u610f\u529b\u201d\u6765\u5b8c\u6210\u672c\u4e66\u7684\u521b\u4f5c\u3002

        \u672c\u4eba\u81ea\u77e5\u5b66\u758f\u624d\u6d45\uff0c\u4e66\u4e2d\u5185\u5bb9\u867d\u7136\u5df2\u7ecf\u8fc7\u4e00\u6bb5\u65f6\u95f4\u7684\u6253\u78e8\uff0c\u4f46\u4e00\u5b9a\u4ecd\u6709\u8bb8\u591a\u9519\u8bef\uff0c\u6073\u8bf7\u5404\u4f4d\u8001\u5e08\u548c\u540c\u5b66\u6279\u8bc4\u6307\u6b63\u3002

        Hello\uff0c\u7b97\u6cd5\uff01

        \u8ba1\u7b97\u673a\u7684\u51fa\u73b0\u7ed9\u4e16\u754c\u5e26\u6765\u4e86\u5de8\u5927\u53d8\u9769\uff0c\u5b83\u51ed\u501f\u9ad8\u901f\u7684\u8ba1\u7b97\u80fd\u529b\u548c\u51fa\u8272\u7684\u53ef\u7f16\u7a0b\u6027\uff0c\u6210\u4e3a\u4e86\u6267\u884c\u7b97\u6cd5\u4e0e\u5904\u7406\u6570\u636e\u7684\u7406\u60f3\u5a92\u4ecb\u3002\u65e0\u8bba\u662f\u7535\u5b50\u6e38\u620f\u7684\u903c\u771f\u753b\u9762\u3001\u81ea\u52a8\u9a7e\u9a76\u7684\u667a\u80fd\u51b3\u7b56\uff0c\u8fd8\u662f AlphaGo \u7684\u7cbe\u5f69\u68cb\u5c40\u3001ChatGPT \u7684\u81ea\u7136\u4ea4\u4e92\uff0c\u8fd9\u4e9b\u5e94\u7528\u90fd\u662f\u7b97\u6cd5\u5728\u8ba1\u7b97\u673a\u4e0a\u7684\u7cbe\u5999\u6f14\u7ece\u3002

        \u4e8b\u5b9e\u4e0a\uff0c\u5728\u8ba1\u7b97\u673a\u95ee\u4e16\u4e4b\u524d\uff0c\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u5c31\u5df2\u7ecf\u5b58\u5728\u4e8e\u4e16\u754c\u7684\u5404\u4e2a\u89d2\u843d\u3002\u65e9\u671f\u7684\u7b97\u6cd5\u76f8\u5bf9\u7b80\u5355\uff0c\u4f8b\u5982\u53e4\u4ee3\u7684\u8ba1\u6570\u65b9\u6cd5\u548c\u5de5\u5177\u5236\u4f5c\u6b65\u9aa4\u7b49\u3002\u968f\u7740\u6587\u660e\u7684\u8fdb\u6b65\uff0c\u7b97\u6cd5\u9010\u6e10\u53d8\u5f97\u66f4\u52a0\u7cbe\u7ec6\u548c\u590d\u6742\u3002\u4ece\u5de7\u593a\u5929\u5de5\u7684\u5320\u4eba\u6280\u827a\u3001\u5230\u89e3\u653e\u751f\u4ea7\u529b\u7684\u5de5\u4e1a\u4ea7\u54c1\u3001\u518d\u5230\u5b87\u5b99\u8fd0\u884c\u7684\u79d1\u5b66\u89c4\u5f8b\uff0c\u51e0\u4e4e\u6bcf\u4e00\u4ef6\u5e73\u51e1\u6216\u4ee4\u4eba\u60ca\u53f9\u7684\u4e8b\u7269\u80cc\u540e\uff0c\u90fd\u9690\u85cf\u7740\u7cbe\u5999\u7684\u7b97\u6cd5\u601d\u60f3\u3002

        \u540c\u6837\uff0c\u6570\u636e\u7ed3\u6784\u65e0\u5904\u4e0d\u5728\uff1a\u5927\u5230\u793e\u4f1a\u7f51\u7edc\uff0c\u5c0f\u5230\u5730\u94c1\u7ebf\u8def\uff0c\u8bb8\u591a\u7cfb\u7edf\u90fd\u53ef\u4ee5\u5efa\u6a21\u4e3a\u201c\u56fe\u201d\uff1b\u5927\u5230\u4e00\u4e2a\u56fd\u5bb6\uff0c\u5c0f\u5230\u4e00\u4e2a\u5bb6\u5ead\uff0c\u793e\u4f1a\u7684\u4e3b\u8981\u7ec4\u7ec7\u5f62\u5f0f\u5448\u73b0\u51fa\u201c\u6811\u201d\u7684\u7279\u5f81\uff1b\u51ac\u5929\u7684\u8863\u670d\u5c31\u50cf\u201c\u6808\u201d\uff0c\u6700\u5148\u7a7f\u4e0a\u7684\u6700\u540e\u624d\u80fd\u8131\u4e0b\uff1b\u7fbd\u6bdb\u7403\u7b52\u5219\u5982\u540c\u201c\u961f\u5217\u201d\uff0c\u4e00\u7aef\u653e\u5165\u3001\u53e6\u4e00\u7aef\u53d6\u51fa\uff1b\u5b57\u5178\u5c31\u50cf\u4e00\u4e2a\u201c\u54c8\u5e0c\u8868\u201d\uff0c\u80fd\u591f\u5feb\u901f\u67e5\u627e\u76ee\u6807\u8bcd\u6761\u3002

        \u672c\u4e66\u65e8\u5728\u901a\u8fc7\u6e05\u6670\u6613\u61c2\u7684\u52a8\u753b\u56fe\u89e3\u548c\u53ef\u8fd0\u884c\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u4f7f\u8bfb\u8005\u7406\u89e3\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u5e76\u80fd\u591f\u901a\u8fc7\u7f16\u7a0b\u6765\u5b9e\u73b0\u5b83\u4eec\u3002\u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u672c\u4e66\u81f4\u529b\u4e8e\u63ed\u793a\u7b97\u6cd5\u5728\u590d\u6742\u4e16\u754c\u4e2d\u7684\u751f\u52a8\u4f53\u73b0\uff0c\u5c55\u73b0\u7b97\u6cd5\u4e4b\u7f8e\u3002\u5e0c\u671b\u672c\u4e66\u80fd\u591f\u5e2e\u52a9\u5230\u4f60\uff01

        "},{"location":"chapter_introduction/","title":"\u7b2c 1 \u7ae0 \u00a0 \u521d\u8bc6\u7b97\u6cd5","text":"

        Abstract

        \u4e00\u4f4d\u5c11\u5973\u7fe9\u7fe9\u8d77\u821e\uff0c\u4e0e\u6570\u636e\u4ea4\u7ec7\u5728\u4e00\u8d77\uff0c\u88d9\u6446\u4e0a\u98d8\u626c\u7740\u7b97\u6cd5\u7684\u65cb\u5f8b\u3002

        \u5979\u9080\u8bf7\u4f60\u5171\u821e\uff0c\u8bf7\u7d27\u8ddf\u5979\u7684\u6b65\u4f10\uff0c\u8e0f\u5165\u5145\u6ee1\u903b\u8f91\u4e0e\u7f8e\u611f\u7684\u7b97\u6cd5\u4e16\u754c\u3002

        "},{"location":"chapter_introduction/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 1.1 \u00a0 \u7b97\u6cd5\u65e0\u5904\u4e0d\u5728
        • 1.2 \u00a0 \u7b97\u6cd5\u662f\u4ec0\u4e48
        • 1.3 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_introduction/algorithms_are_everywhere/","title":"1.1 \u00a0 \u7b97\u6cd5\u65e0\u5904\u4e0d\u5728","text":"

        \u5f53\u6211\u4eec\u542c\u5230\u201c\u7b97\u6cd5\u201d\u8fd9\u4e2a\u8bcd\u65f6\uff0c\u5f88\u81ea\u7136\u5730\u4f1a\u60f3\u5230\u6570\u5b66\u3002\u7136\u800c\u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u7b97\u6cd5\u5e76\u4e0d\u6d89\u53ca\u590d\u6742\u6570\u5b66\uff0c\u800c\u662f\u66f4\u591a\u5730\u4f9d\u8d56\u57fa\u672c\u903b\u8f91\uff0c\u8fd9\u4e9b\u903b\u8f91\u5728\u6211\u4eec\u7684\u65e5\u5e38\u751f\u6d3b\u4e2d\u5904\u5904\u53ef\u89c1\u3002

        \u5728\u6b63\u5f0f\u63a2\u8ba8\u7b97\u6cd5\u4e4b\u524d\uff0c\u6709\u4e00\u4e2a\u6709\u8da3\u7684\u4e8b\u5b9e\u503c\u5f97\u5206\u4eab\uff1a\u4f60\u5df2\u7ecf\u5728\u4e0d\u77e5\u4e0d\u89c9\u4e2d\u5b66\u4f1a\u4e86\u8bb8\u591a\u7b97\u6cd5\uff0c\u5e76\u4e60\u60ef\u5c06\u5b83\u4eec\u5e94\u7528\u5230\u65e5\u5e38\u751f\u6d3b\u4e2d\u4e86\u3002\u4e0b\u9762\u6211\u5c06\u4e3e\u51e0\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u6765\u8bc1\u5b9e\u8fd9\u4e00\u70b9\u3002

        \u4f8b\u4e00\uff1a\u67e5\u5b57\u5178\u3002\u5728\u5b57\u5178\u91cc\uff0c\u6bcf\u4e2a\u6c49\u5b57\u90fd\u5bf9\u5e94\u4e00\u4e2a\u62fc\u97f3\uff0c\u800c\u5b57\u5178\u662f\u6309\u7167\u62fc\u97f3\u5b57\u6bcd\u987a\u5e8f\u6392\u5217\u7684\u3002\u5047\u8bbe\u6211\u4eec\u9700\u8981\u67e5\u627e\u4e00\u4e2a\u62fc\u97f3\u9996\u5b57\u6bcd\u4e3a \\(r\\) \u7684\u5b57\uff0c\u901a\u5e38\u4f1a\u6309\u7167\u56fe 1-1 \u6240\u793a\u7684\u65b9\u5f0f\u5b9e\u73b0\u3002

        1. \u7ffb\u5f00\u5b57\u5178\u7ea6\u4e00\u534a\u7684\u9875\u6570\uff0c\u67e5\u770b\u8be5\u9875\u7684\u9996\u5b57\u6bcd\u662f\u4ec0\u4e48\uff0c\u5047\u8bbe\u9996\u5b57\u6bcd\u4e3a \\(m\\) \u3002
        2. \u7531\u4e8e\u5728\u62fc\u97f3\u5b57\u6bcd\u8868\u4e2d \\(r\\) \u4f4d\u4e8e \\(m\\) \u4e4b\u540e\uff0c\u6240\u4ee5\u6392\u9664\u5b57\u5178\u524d\u534a\u90e8\u5206\uff0c\u67e5\u627e\u8303\u56f4\u7f29\u5c0f\u5230\u540e\u534a\u90e8\u5206\u3002
        3. \u4e0d\u65ad\u91cd\u590d\u6b65\u9aa4 1. \u548c\u6b65\u9aa4 2. \uff0c\u76f4\u81f3\u627e\u5230\u62fc\u97f3\u9996\u5b57\u6bcd\u4e3a \\(r\\) \u7684\u9875\u7801\u4e3a\u6b62\u3002
        <1><2><3><4><5>

        \u56fe 1-1 \u00a0 \u67e5\u5b57\u5178\u6b65\u9aa4

        \u67e5\u5b57\u5178\u8fd9\u4e2a\u5c0f\u5b66\u751f\u5fc5\u5907\u6280\u80fd\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u8457\u540d\u7684\u201c\u4e8c\u5206\u67e5\u627e\u201d\u7b97\u6cd5\u3002\u4ece\u6570\u636e\u7ed3\u6784\u7684\u89d2\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u5b57\u5178\u89c6\u4e3a\u4e00\u4e2a\u5df2\u6392\u5e8f\u7684\u201c\u6570\u7ec4\u201d\uff1b\u4ece\u7b97\u6cd5\u7684\u89d2\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e0a\u8ff0\u67e5\u5b57\u5178\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\u770b\u4f5c\u201c\u4e8c\u5206\u67e5\u627e\u201d\u3002

        \u4f8b\u4e8c\uff1a\u6574\u7406\u6251\u514b\u3002\u6211\u4eec\u5728\u6253\u724c\u65f6\uff0c\u6bcf\u5c40\u90fd\u9700\u8981\u6574\u7406\u624b\u4e2d\u7684\u6251\u514b\u724c\uff0c\u4f7f\u5176\u4ece\u5c0f\u5230\u5927\u6392\u5217\uff0c\u5b9e\u73b0\u6d41\u7a0b\u5982\u56fe 1-2 \u6240\u793a\u3002

        1. \u5c06\u6251\u514b\u724c\u5212\u5206\u4e3a\u201c\u6709\u5e8f\u201d\u548c\u201c\u65e0\u5e8f\u201d\u4e24\u90e8\u5206\uff0c\u5e76\u5047\u8bbe\u521d\u59cb\u72b6\u6001\u4e0b\u6700\u5de6 1 \u5f20\u6251\u514b\u724c\u5df2\u7ecf\u6709\u5e8f\u3002
        2. \u5728\u65e0\u5e8f\u90e8\u5206\u62bd\u51fa\u4e00\u5f20\u6251\u514b\u724c\uff0c\u63d2\u5165\u81f3\u6709\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\uff1b\u5b8c\u6210\u540e\u6700\u5de6 2 \u5f20\u6251\u514b\u5df2\u7ecf\u6709\u5e8f\u3002
        3. \u4e0d\u65ad\u5faa\u73af\u6b65\u9aa4 2. \uff0c\u6bcf\u4e00\u8f6e\u5c06\u4e00\u5f20\u6251\u514b\u724c\u4ece\u65e0\u5e8f\u90e8\u5206\u63d2\u5165\u81f3\u6709\u5e8f\u90e8\u5206\uff0c\u76f4\u81f3\u6240\u6709\u6251\u514b\u724c\u90fd\u6709\u5e8f\u3002

        \u56fe 1-2 \u00a0 \u6251\u514b\u6392\u5e8f\u6b65\u9aa4

        \u4e0a\u8ff0\u6574\u7406\u6251\u514b\u724c\u7684\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u201c\u63d2\u5165\u6392\u5e8f\u201d\u7b97\u6cd5\uff0c\u5b83\u5728\u5904\u7406\u5c0f\u578b\u6570\u636e\u96c6\u65f6\u975e\u5e38\u9ad8\u6548\u3002\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u7684\u6392\u5e8f\u5e93\u51fd\u6570\u4e2d\u90fd\u6709\u63d2\u5165\u6392\u5e8f\u7684\u8eab\u5f71\u3002

        \u4f8b\u4e09\uff1a\u8d27\u5e01\u627e\u96f6\u3002\u5047\u8bbe\u6211\u4eec\u5728\u8d85\u5e02\u8d2d\u4e70\u4e86 \\(69\\) \u5143\u7684\u5546\u54c1\uff0c\u7ed9\u4e86\u6536\u94f6\u5458 \\(100\\) \u5143\uff0c\u5219\u6536\u94f6\u5458\u9700\u8981\u627e\u6211\u4eec \\(31\\) \u5143\u3002\u4ed6\u4f1a\u5f88\u81ea\u7136\u5730\u5b8c\u6210\u5982\u56fe 1-3 \u6240\u793a\u7684\u601d\u8003\u3002

        1. \u53ef\u9009\u9879\u662f\u6bd4 \\(31\\) \u5143\u9762\u503c\u66f4\u5c0f\u7684\u8d27\u5e01\uff0c\u5305\u62ec \\(1\\) \u5143\u3001\\(5\\) \u5143\u3001\\(10\\) \u5143\u3001\\(20\\) \u5143\u3002
        2. \u4ece\u53ef\u9009\u9879\u4e2d\u62ff\u51fa\u6700\u5927\u7684 \\(20\\) \u5143\uff0c\u5269\u4f59 \\(31 - 20 = 11\\) \u5143\u3002
        3. \u4ece\u5269\u4f59\u53ef\u9009\u9879\u4e2d\u62ff\u51fa\u6700\u5927\u7684 \\(10\\) \u5143\uff0c\u5269\u4f59 \\(11 - 10 = 1\\) \u5143\u3002
        4. \u4ece\u5269\u4f59\u53ef\u9009\u9879\u4e2d\u62ff\u51fa\u6700\u5927\u7684 \\(1\\) \u5143\uff0c\u5269\u4f59 \\(1 - 1 = 0\\) \u5143\u3002
        5. \u5b8c\u6210\u627e\u96f6\uff0c\u65b9\u6848\u4e3a \\(20 + 10 + 1 = 31\\) \u5143\u3002

        \u56fe 1-3 \u00a0 \u8d27\u5e01\u627e\u96f6\u8fc7\u7a0b

        \u5728\u4ee5\u4e0a\u6b65\u9aa4\u4e2d\uff0c\u6211\u4eec\u6bcf\u4e00\u6b65\u90fd\u91c7\u53d6\u5f53\u524d\u770b\u6765\u6700\u597d\u7684\u9009\u62e9\uff08\u5c3d\u53ef\u80fd\u7528\u5927\u9762\u989d\u7684\u8d27\u5e01\uff09\uff0c\u6700\u7ec8\u5f97\u5230\u4e86\u53ef\u884c\u7684\u627e\u96f6\u65b9\u6848\u3002\u4ece\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\u8fd9\u79cd\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u201c\u8d2a\u5fc3\u201d\u7b97\u6cd5\u3002

        \u5c0f\u5230\u70f9\u996a\u4e00\u9053\u83dc\uff0c\u5927\u5230\u661f\u9645\u822a\u884c\uff0c\u51e0\u4e4e\u6240\u6709\u95ee\u9898\u7684\u89e3\u51b3\u90fd\u79bb\u4e0d\u5f00\u7b97\u6cd5\u3002\u8ba1\u7b97\u673a\u7684\u51fa\u73b0\u4f7f\u5f97\u6211\u4eec\u80fd\u591f\u901a\u8fc7\u7f16\u7a0b\u5c06\u6570\u636e\u7ed3\u6784\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\uff0c\u540c\u65f6\u7f16\u5199\u4ee3\u7801\u8c03\u7528 CPU \u548c GPU \u6267\u884c\u7b97\u6cd5\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u80fd\u628a\u751f\u6d3b\u4e2d\u7684\u95ee\u9898\u8f6c\u79fb\u5230\u8ba1\u7b97\u673a\u4e0a\uff0c\u4ee5\u66f4\u9ad8\u6548\u7684\u65b9\u5f0f\u89e3\u51b3\u5404\u79cd\u590d\u6742\u95ee\u9898\u3002

        Tip

        \u5982\u679c\u4f60\u5bf9\u6570\u636e\u7ed3\u6784\u3001\u7b97\u6cd5\u3001\u6570\u7ec4\u548c\u4e8c\u5206\u67e5\u627e\u7b49\u6982\u5ff5\u4ecd\u611f\u5230\u4e00\u77e5\u534a\u89e3\uff0c\u8bf7\u7ee7\u7eed\u5f80\u4e0b\u9605\u8bfb\uff0c\u672c\u4e66\u5c06\u5f15\u5bfc\u4f60\u8fc8\u5165\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u77e5\u8bc6\u6bbf\u5802\u3002

        "},{"location":"chapter_introduction/summary/","title":"1.3 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_introduction/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u7b97\u6cd5\u5728\u65e5\u5e38\u751f\u6d3b\u4e2d\u65e0\u5904\u4e0d\u5728\uff0c\u5e76\u4e0d\u662f\u9065\u4e0d\u53ef\u53ca\u7684\u9ad8\u6df1\u77e5\u8bc6\u3002\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u5df2\u7ecf\u5728\u4e0d\u77e5\u4e0d\u89c9\u4e2d\u5b66\u4f1a\u4e86\u8bb8\u591a\u7b97\u6cd5\uff0c\u7528\u4ee5\u89e3\u51b3\u751f\u6d3b\u4e2d\u7684\u5927\u5c0f\u95ee\u9898\u3002
        • \u67e5\u5b57\u5178\u7684\u539f\u7406\u4e0e\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u76f8\u4e00\u81f4\u3002\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u4f53\u73b0\u4e86\u5206\u800c\u6cbb\u4e4b\u7684\u91cd\u8981\u7b97\u6cd5\u601d\u60f3\u3002
        • \u6574\u7406\u6251\u514b\u7684\u8fc7\u7a0b\u4e0e\u63d2\u5165\u6392\u5e8f\u7b97\u6cd5\u975e\u5e38\u7c7b\u4f3c\u3002\u63d2\u5165\u6392\u5e8f\u7b97\u6cd5\u9002\u5408\u6392\u5e8f\u5c0f\u578b\u6570\u636e\u96c6\u3002
        • \u8d27\u5e01\u627e\u96f6\u7684\u6b65\u9aa4\u672c\u8d28\u4e0a\u662f\u8d2a\u5fc3\u7b97\u6cd5\uff0c\u6bcf\u4e00\u6b65\u90fd\u91c7\u53d6\u5f53\u524d\u770b\u6765\u6700\u597d\u7684\u9009\u62e9\u3002
        • \u7b97\u6cd5\u662f\u5728\u6709\u9650\u65f6\u95f4\u5185\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u7684\u4e00\u7ec4\u6307\u4ee4\u6216\u64cd\u4f5c\u6b65\u9aa4\uff0c\u800c\u6570\u636e\u7ed3\u6784\u662f\u8ba1\u7b97\u673a\u4e2d\u7ec4\u7ec7\u548c\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\u3002
        • \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7d27\u5bc6\u76f8\u8fde\u3002\u6570\u636e\u7ed3\u6784\u662f\u7b97\u6cd5\u7684\u57fa\u77f3\uff0c\u800c\u7b97\u6cd5\u4e3a\u6570\u636e\u7ed3\u6784\u6ce8\u5165\u751f\u547d\u529b\u3002
        • \u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7c7b\u6bd4\u4e3a\u62fc\u88c5\u79ef\u6728\uff0c\u79ef\u6728\u4ee3\u8868\u6570\u636e\uff0c\u79ef\u6728\u7684\u5f62\u72b6\u548c\u8fde\u63a5\u65b9\u5f0f\u7b49\u4ee3\u8868\u6570\u636e\u7ed3\u6784\uff0c\u62fc\u88c5\u79ef\u6728\u7684\u6b65\u9aa4\u5219\u5bf9\u5e94\u7b97\u6cd5\u3002
        "},{"location":"chapter_introduction/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u4f5c\u4e3a\u4e00\u540d\u7a0b\u5e8f\u5458\uff0c\u6211\u5728\u65e5\u5e38\u5de5\u4f5c\u4e2d\u4ece\u672a\u7528\u7b97\u6cd5\u89e3\u51b3\u8fc7\u95ee\u9898\uff0c\u5e38\u7528\u7b97\u6cd5\u90fd\u88ab\u7f16\u7a0b\u8bed\u8a00\u5c01\u88c5\u597d\u4e86\uff0c\u76f4\u63a5\u7528\u5c31\u53ef\u4ee5\u4e86\uff1b\u8fd9\u662f\u5426\u610f\u5473\u7740\u6211\u4eec\u5de5\u4f5c\u4e2d\u7684\u95ee\u9898\u8fd8\u6ca1\u6709\u5230\u8fbe\u9700\u8981\u7b97\u6cd5\u7684\u7a0b\u5ea6\uff1f

        \u5982\u679c\u628a\u5177\u4f53\u7684\u5de5\u4f5c\u6280\u80fd\u6bd4\u4f5c\u662f\u6b66\u529f\u7684\u201c\u62db\u5f0f\u201d\u7684\u8bdd\uff0c\u90a3\u4e48\u57fa\u7840\u79d1\u76ee\u5e94\u8be5\u66f4\u50cf\u662f\u201c\u5185\u529f\u201d\u3002

        \u6211\u8ba4\u4e3a\u5b66\u7b97\u6cd5\uff08\u4ee5\u53ca\u5176\u4ed6\u57fa\u7840\u79d1\u76ee\uff09\u7684\u610f\u4e49\u4e0d\u662f\u5728\u4e8e\u5728\u5de5\u4f5c\u4e2d\u4ece\u96f6\u5b9e\u73b0\u5b83\uff0c\u800c\u662f\u57fa\u4e8e\u5b66\u5230\u7684\u77e5\u8bc6\uff0c\u5728\u89e3\u51b3\u95ee\u9898\u65f6\u80fd\u591f\u4f5c\u51fa\u4e13\u4e1a\u7684\u53cd\u5e94\u548c\u5224\u65ad\uff0c\u4ece\u800c\u63d0\u5347\u5de5\u4f5c\u7684\u6574\u4f53\u8d28\u91cf\u3002\u4e3e\u4e00\u4e2a\u7b80\u5355\u4f8b\u5b50\uff0c\u6bcf\u79cd\u7f16\u7a0b\u8bed\u8a00\u90fd\u5185\u7f6e\u4e86\u6392\u5e8f\u51fd\u6570\uff1a

        • \u5982\u679c\u6211\u4eec\u6ca1\u6709\u5b66\u8fc7\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\uff0c\u90a3\u4e48\u7ed9\u5b9a\u4efb\u4f55\u6570\u636e\uff0c\u6211\u4eec\u53ef\u80fd\u90fd\u585e\u7ed9\u8fd9\u4e2a\u6392\u5e8f\u51fd\u6570\u53bb\u505a\u4e86\u3002\u8fd0\u884c\u987a\u7545\u3001\u6027\u80fd\u4e0d\u9519\uff0c\u770b\u4e0a\u53bb\u5e76\u6ca1\u6709\u4ec0\u4e48\u95ee\u9898\u3002
        • \u4f46\u5982\u679c\u5b66\u8fc7\u7b97\u6cd5\uff0c\u6211\u4eec\u5c31\u4f1a\u77e5\u9053\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(n \\log n)\\) \uff1b\u800c\u5982\u679c\u7ed9\u5b9a\u7684\u6570\u636e\u662f\u56fa\u5b9a\u4f4d\u6570\u7684\u6574\u6570\uff08\u4f8b\u5982\u5b66\u53f7\uff09\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u53ef\u4ee5\u7528\u6548\u7387\u66f4\u9ad8\u7684\u201c\u57fa\u6570\u6392\u5e8f\u201d\u6765\u505a\uff0c\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u964d\u4e3a \\(O(nk)\\) \uff0c\u5176\u4e2d \\(k\\) \u4e3a\u4f4d\u6570\u3002\u5f53\u6570\u636e\u4f53\u91cf\u5f88\u5927\u65f6\uff0c\u8282\u7701\u51fa\u6765\u7684\u8fd0\u884c\u65f6\u95f4\u5c31\u80fd\u521b\u9020\u8f83\u5927\u4ef7\u503c\uff08\u6210\u672c\u964d\u4f4e\u3001\u4f53\u9a8c\u53d8\u597d\u7b49\uff09\u3002

        \u5728\u5de5\u7a0b\u9886\u57df\u4e2d\uff0c\u5927\u91cf\u95ee\u9898\u662f\u96be\u4ee5\u8fbe\u5230\u6700\u4f18\u89e3\u7684\uff0c\u8bb8\u591a\u95ee\u9898\u53ea\u662f\u88ab\u201c\u5dee\u4e0d\u591a\u201d\u5730\u89e3\u51b3\u4e86\u3002\u95ee\u9898\u7684\u96be\u6613\u7a0b\u5ea6\u4e00\u65b9\u9762\u53d6\u51b3\u4e8e\u95ee\u9898\u672c\u8eab\u7684\u6027\u8d28\uff0c\u53e6\u4e00\u65b9\u9762\u4e5f\u53d6\u51b3\u4e8e\u89c2\u6d4b\u95ee\u9898\u7684\u4eba\u7684\u77e5\u8bc6\u50a8\u5907\u3002\u4eba\u7684\u77e5\u8bc6\u8d8a\u5b8c\u5907\u3001\u7ecf\u9a8c\u8d8a\u591a\uff0c\u5206\u6790\u95ee\u9898\u5c31\u4f1a\u8d8a\u6df1\u5165\uff0c\u95ee\u9898\u5c31\u80fd\u88ab\u89e3\u51b3\u5f97\u66f4\u4f18\u96c5\u3002

        "},{"location":"chapter_introduction/what_is_dsa/","title":"1.2 \u00a0 \u7b97\u6cd5\u662f\u4ec0\u4e48","text":""},{"location":"chapter_introduction/what_is_dsa/#121","title":"1.2.1 \u00a0 \u7b97\u6cd5\u5b9a\u4e49","text":"

        \u7b97\u6cd5\uff08algorithm\uff09\u662f\u5728\u6709\u9650\u65f6\u95f4\u5185\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u7684\u4e00\u7ec4\u6307\u4ee4\u6216\u64cd\u4f5c\u6b65\u9aa4\uff0c\u5b83\u5177\u6709\u4ee5\u4e0b\u7279\u6027\u3002

        • \u95ee\u9898\u662f\u660e\u786e\u7684\uff0c\u5305\u542b\u6e05\u6670\u7684\u8f93\u5165\u548c\u8f93\u51fa\u5b9a\u4e49\u3002
        • \u5177\u6709\u53ef\u884c\u6027\uff0c\u80fd\u591f\u5728\u6709\u9650\u6b65\u9aa4\u3001\u65f6\u95f4\u548c\u5185\u5b58\u7a7a\u95f4\u4e0b\u5b8c\u6210\u3002
        • \u5404\u6b65\u9aa4\u90fd\u6709\u786e\u5b9a\u7684\u542b\u4e49\uff0c\u5728\u76f8\u540c\u7684\u8f93\u5165\u548c\u8fd0\u884c\u6761\u4ef6\u4e0b\uff0c\u8f93\u51fa\u59cb\u7ec8\u76f8\u540c\u3002
        "},{"location":"chapter_introduction/what_is_dsa/#122","title":"1.2.2 \u00a0 \u6570\u636e\u7ed3\u6784\u5b9a\u4e49","text":"

        \u6570\u636e\u7ed3\u6784\uff08data structure\uff09\u662f\u7ec4\u7ec7\u548c\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\uff0c\u6db5\u76d6\u6570\u636e\u5185\u5bb9\u3001\u6570\u636e\u4e4b\u95f4\u5173\u7cfb\u548c\u6570\u636e\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5b83\u5177\u6709\u4ee5\u4e0b\u8bbe\u8ba1\u76ee\u6807\u3002

        • \u7a7a\u95f4\u5360\u7528\u5c3d\u91cf\u5c11\uff0c\u4ee5\u8282\u7701\u8ba1\u7b97\u673a\u5185\u5b58\u3002
        • \u6570\u636e\u64cd\u4f5c\u5c3d\u53ef\u80fd\u5feb\u901f\uff0c\u6db5\u76d6\u6570\u636e\u8bbf\u95ee\u3001\u6dfb\u52a0\u3001\u5220\u9664\u3001\u66f4\u65b0\u7b49\u3002
        • \u63d0\u4f9b\u7b80\u6d01\u7684\u6570\u636e\u8868\u793a\u548c\u903b\u8f91\u4fe1\u606f\uff0c\u4ee5\u4fbf\u7b97\u6cd5\u9ad8\u6548\u8fd0\u884c\u3002

        \u6570\u636e\u7ed3\u6784\u8bbe\u8ba1\u662f\u4e00\u4e2a\u5145\u6ee1\u6743\u8861\u7684\u8fc7\u7a0b\u3002\u5982\u679c\u60f3\u5728\u67d0\u65b9\u9762\u53d6\u5f97\u63d0\u5347\uff0c\u5f80\u5f80\u9700\u8981\u5728\u53e6\u4e00\u65b9\u9762\u4f5c\u51fa\u59a5\u534f\u3002\u4e0b\u9762\u4e3e\u4e24\u4e2a\u4f8b\u5b50\u3002

        • \u94fe\u8868\u76f8\u8f83\u4e8e\u6570\u7ec4\uff0c\u5728\u6570\u636e\u6dfb\u52a0\u548c\u5220\u9664\u64cd\u4f5c\u4e0a\u66f4\u52a0\u4fbf\u6377\uff0c\u4f46\u727a\u7272\u4e86\u6570\u636e\u8bbf\u95ee\u901f\u5ea6\u3002
        • \u56fe\u76f8\u8f83\u4e8e\u94fe\u8868\uff0c\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u903b\u8f91\u4fe1\u606f\uff0c\u4f46\u9700\u8981\u5360\u7528\u66f4\u5927\u7684\u5185\u5b58\u7a7a\u95f4\u3002
        "},{"location":"chapter_introduction/what_is_dsa/#123","title":"1.2.3 \u00a0 \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u5173\u7cfb","text":"

        \u5982\u56fe 1-4 \u6240\u793a\uff0c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u9ad8\u5ea6\u76f8\u5173\u3001\u7d27\u5bc6\u7ed3\u5408\uff0c\u5177\u4f53\u8868\u73b0\u5728\u4ee5\u4e0b\u4e09\u4e2a\u65b9\u9762\u3002

        • \u6570\u636e\u7ed3\u6784\u662f\u7b97\u6cd5\u7684\u57fa\u77f3\u3002\u6570\u636e\u7ed3\u6784\u4e3a\u7b97\u6cd5\u63d0\u4f9b\u4e86\u7ed3\u6784\u5316\u5b58\u50a8\u7684\u6570\u636e\uff0c\u4ee5\u53ca\u64cd\u4f5c\u6570\u636e\u7684\u65b9\u6cd5\u3002
        • \u7b97\u6cd5\u4e3a\u6570\u636e\u7ed3\u6784\u6ce8\u5165\u751f\u547d\u529b\u3002\u6570\u636e\u7ed3\u6784\u672c\u8eab\u4ec5\u5b58\u50a8\u6570\u636e\u4fe1\u606f\uff0c\u7ed3\u5408\u7b97\u6cd5\u624d\u80fd\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u3002
        • \u7b97\u6cd5\u901a\u5e38\u53ef\u4ee5\u57fa\u4e8e\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\u5b9e\u73b0\uff0c\u4f46\u6267\u884c\u6548\u7387\u53ef\u80fd\u76f8\u5dee\u5f88\u5927\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u662f\u5173\u952e\u3002

        \u56fe 1-4 \u00a0 \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u5173\u7cfb

        \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u72b9\u5982\u56fe 1-5 \u6240\u793a\u7684\u62fc\u88c5\u79ef\u6728\u3002\u4e00\u5957\u79ef\u6728\uff0c\u9664\u4e86\u5305\u542b\u8bb8\u591a\u96f6\u4ef6\u4e4b\u5916\uff0c\u8fd8\u9644\u6709\u8be6\u7ec6\u7684\u7ec4\u88c5\u8bf4\u660e\u4e66\u3002\u6211\u4eec\u6309\u7167\u8bf4\u660e\u4e66\u4e00\u6b65\u6b65\u64cd\u4f5c\uff0c\u5c31\u80fd\u7ec4\u88c5\u51fa\u7cbe\u7f8e\u7684\u79ef\u6728\u6a21\u578b\u3002

        \u56fe 1-5 \u00a0 \u62fc\u88c5\u79ef\u6728

        \u4e24\u8005\u7684\u8be6\u7ec6\u5bf9\u5e94\u5173\u7cfb\u5982\u8868 1-1 \u6240\u793a\u3002

        \u8868 1-1 \u00a0 \u5c06\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7c7b\u6bd4\u4e3a\u62fc\u88c5\u79ef\u6728

        \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5 \u62fc\u88c5\u79ef\u6728 \u8f93\u5165\u6570\u636e \u672a\u62fc\u88c5\u7684\u79ef\u6728 \u6570\u636e\u7ed3\u6784 \u79ef\u6728\u7ec4\u7ec7\u5f62\u5f0f\uff0c\u5305\u62ec\u5f62\u72b6\u3001\u5927\u5c0f\u3001\u8fde\u63a5\u65b9\u5f0f\u7b49 \u7b97\u6cd5 \u628a\u79ef\u6728\u62fc\u6210\u76ee\u6807\u5f62\u6001\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\u6b65\u9aa4 \u8f93\u51fa\u6570\u636e \u79ef\u6728\u6a21\u578b

        \u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u662f\u72ec\u7acb\u4e8e\u7f16\u7a0b\u8bed\u8a00\u7684\u3002\u6b63\u56e0\u5982\u6b64\uff0c\u672c\u4e66\u5f97\u4ee5\u63d0\u4f9b\u57fa\u4e8e\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u7684\u5b9e\u73b0\u3002

        \u7ea6\u5b9a\u4fd7\u6210\u7684\u7b80\u79f0

        \u5728\u5b9e\u9645\u8ba8\u8bba\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u201c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u201d\u7b80\u79f0\u4e3a\u201c\u7b97\u6cd5\u201d\u3002\u6bd4\u5982\u4f17\u6240\u5468\u77e5\u7684 LeetCode \u7b97\u6cd5\u9898\u76ee\uff0c\u5b9e\u9645\u4e0a\u540c\u65f6\u8003\u67e5\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u4e24\u65b9\u9762\u7684\u77e5\u8bc6\u3002

        "},{"location":"chapter_paperbook/","title":"\u7eb8\u8d28\u4e66","text":"

        \u7ecf\u8fc7\u957f\u65f6\u95f4\u7684\u6253\u78e8\uff0c\u300aHello \u7b97\u6cd5\u300b\u7eb8\u8d28\u4e66\u7ec8\u4e8e\u53d1\u5e03\u4e86\uff01\u6b64\u65f6\u7684\u5fc3\u60c5\u53ef\u4ee5\u7528\u4e00\u53e5\u8bd7\u6765\u5f62\u5bb9\uff1a

        \u8ffd\u98ce\u8d76\u6708\u83ab\u505c\u7559\uff0c\u5e73\u829c\u5c3d\u5904\u662f\u6625\u5c71\u3002

        \u4ee5\u4e0b\u89c6\u9891\u5c55\u793a\u4e86\u7eb8\u8d28\u4e66\uff0c\u5e76\u4e14\u5305\u542b\u6211\u7684\u4e00\u4e9b\u601d\u8003\uff1a

        • \u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u91cd\u8981\u6027\u3002
        • \u4e3a\u4ec0\u4e48\u5728\u7eb8\u8d28\u4e66\u4e2d\u9009\u62e9 Python\u3002
        • \u5bf9\u77e5\u8bc6\u5206\u4eab\u7684\u7406\u89e3\u3002

        \u65b0\u4eba UP \u4e3b\uff0c\u8bf7\u591a\u591a\u5173\u7167\u3001\u4e00\u952e\u4e09\u8fde\uff5e\u8c22\u8c22\uff01

        \u9644\u7eb8\u8d28\u4e66\u5feb\u7167\uff1a

        "},{"location":"chapter_paperbook/#_2","title":"\u4f18\u52bf\u4e0e\u4e0d\u8db3","text":"

        \u603b\u7ed3\u4e00\u4e0b\u7eb8\u8d28\u4e66\u53ef\u80fd\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u60ca\u559c\u7684\u5730\u65b9\uff1a

        • \u91c7\u7528\u5168\u5f69\u5370\u5237\uff0c\u80fd\u591f\u539f\u6c41\u539f\u5473\u5730\u53d1\u6325\u51fa\u672c\u4e66\u201c\u52a8\u753b\u56fe\u89e3\u201d\u7684\u4f18\u52bf\u3002
        • \u8003\u7a76\u7eb8\u5f20\u6750\u8d28\uff0c\u65e2\u4fdd\u8bc1\u8272\u5f69\u9ad8\u5ea6\u8fd8\u539f\uff0c\u4e5f\u4fdd\u7559\u7eb8\u8d28\u4e66\u7279\u6709\u7684\u8d28\u611f\u3002
        • \u7eb8\u8d28\u7248\u6bd4\u7f51\u9875\u7248\u7684\u683c\u5f0f\u66f4\u52a0\u89c4\u8303\uff0c\u4f8b\u5982\u56fe\u4e2d\u7684\u516c\u5f0f\u4f7f\u7528\u659c\u4f53\u3002
        • \u5728\u4e0d\u63d0\u5347\u5b9a\u4ef7\u7684\u524d\u63d0\u4e0b\uff0c\u9644\u8d60\u601d\u7ef4\u5bfc\u56fe\u6298\u9875\u3001\u4e66\u7b7e\u3002
        • \u7eb8\u8d28\u4e66\u3001\u7f51\u9875\u7248\u3001PDF \u7248\u5185\u5bb9\u540c\u6b65\uff0c\u968f\u610f\u5207\u6362\u9605\u8bfb\u3002

        Tip

        \u7531\u4e8e\u7eb8\u8d28\u4e66\u548c\u7f51\u9875\u7248\u7684\u540c\u6b65\u96be\u5ea6\u8f83\u5927\uff0c\u56e0\u6b64\u53ef\u80fd\u4f1a\u6709\u4e00\u4e9b\u7ec6\u8282\u4e0a\u7684\u4e0d\u540c\uff0c\u8bf7\u60a8\u89c1\u8c05\uff01

        \u5f53\u7136\uff0c\u7eb8\u8d28\u4e66\u4e5f\u6709\u4e00\u4e9b\u503c\u5f97\u5927\u5bb6\u5165\u624b\u524d\u8003\u8651\u7684\u5730\u65b9\uff1a

        • \u4f7f\u7528 Python \u8bed\u8a00\uff0c\u53ef\u80fd\u4e0d\u5339\u914d\u4f60\u7684\u4e3b\u8bed\u8a00\uff08\u53ef\u4ee5\u628a Python \u770b\u4f5c\u4f2a\u4ee3\u7801\uff0c\u91cd\u5728\u7406\u89e3\u601d\u8def\uff09\u3002
        • \u5168\u5f69\u5370\u5237\u867d\u7136\u5927\u5e45\u63d0\u5347\u4e86\u56fe\u89e3\u548c\u4ee3\u7801\u7684\u9605\u8bfb\u4f53\u9a8c\uff0c\u4f46\u4ef7\u683c\u4f1a\u6bd4\u9ed1\u767d\u5370\u5237\u9ad8\u4e00\u4e9b\u3002

        Tip

        \u201c\u5370\u5237\u8d28\u91cf\u201d\u548c\u201c\u4ef7\u683c\u201d\u5c31\u50cf\u7b97\u6cd5\u4e2d\u7684\u201c\u65f6\u95f4\u6548\u7387\u201d\u548c\u201c\u7a7a\u95f4\u6548\u7387\u201d\uff0c\u96be\u4ee5\u4e24\u5168\u3002\u800c\u6211\u8ba4\u4e3a\uff0c\u201c\u5370\u5237\u8d28\u91cf\u201d\u5bf9\u5e94\u7684\u662f\u201c\u65f6\u95f4\u6548\u7387\u201d\uff0c\u66f4\u5e94\u8be5\u88ab\u6ce8\u91cd\u3002

        "},{"location":"chapter_paperbook/#_3","title":"\u8d2d\u4e70\u94fe\u63a5","text":"

        \u5982\u679c\u4f60\u5bf9\u7eb8\u8d28\u4e66\u611f\u5174\u8da3\uff0c\u53ef\u4ee5\u8003\u8651\u5165\u624b\u4e00\u672c\u3002\u6211\u4eec\u4e3a\u5927\u5bb6\u4e89\u53d6\u5230\u4e86\u65b0\u4e66 5 \u6298\u4f18\u60e0\uff0c\u8bf7\u89c1\u6b64\u94fe\u63a5\u6216\u626b\u63cf\u4ee5\u4e0b\u4e8c\u7ef4\u7801\uff1a

        "},{"location":"chapter_paperbook/#_4","title":"\u5c3e\u8bb0","text":"

        \u8d77\u521d\uff0c\u6211\u4f4e\u4f30\u4e86\u7eb8\u8d28\u4e66\u51fa\u7248\u7684\u5de5\u4f5c\u91cf\uff0c\u4ee5\u4e3a\u53ea\u8981\u7ef4\u62a4\u597d\u4e86\u5f00\u6e90\u9879\u76ee\uff0c\u7eb8\u8d28\u7248\u5c31\u53ef\u4ee5\u901a\u8fc7\u67d0\u4e9b\u81ea\u52a8\u5316\u624b\u6bb5\u751f\u6210\u51fa\u6765\u3002\u5b9e\u8df5\u8bc1\u660e\uff0c\u7eb8\u8d28\u4e66\u7684\u751f\u4ea7\u6d41\u7a0b\u4e0e\u5f00\u6e90\u9879\u76ee\u7684\u66f4\u65b0\u673a\u5236\u5b58\u5728\u5f88\u5927\u7684\u4e0d\u540c\uff0c\u4e24\u8005\u4e4b\u95f4\u7684\u8f6c\u5316\u9700\u8981\u505a\u8bb8\u591a\u989d\u5916\u5de5\u4f5c\u3002

        \u4e00\u672c\u4e66\u7684\u521d\u7a3f\u4e0e\u8fbe\u5230\u51fa\u7248\u6807\u51c6\u7684\u5b9a\u7a3f\u4e4b\u95f4\u4ecd\u6709\u8f83\u957f\u8ddd\u79bb\uff0c\u9700\u8981\u51fa\u7248\u793e\uff08\u7b56\u5212\u3001\u7f16\u8f91\u3001\u8bbe\u8ba1\u3001\u5e02\u573a\u7b49\uff09\u4e0e\u4f5c\u8005\u7684\u901a\u529b\u5408\u4f5c\u3001\u957f\u671f\u96d5\u7422\u3002\u5728\u6b64\u611f\u8c22\u56fe\u7075\u7b56\u5212\u7f16\u8f91\u738b\u519b\u82b1\u3001\u4ee5\u53ca\u4eba\u6c11\u90ae\u7535\u51fa\u7248\u793e\u548c\u56fe\u7075\u793e\u533a\u6bcf\u4f4d\u53c2\u4e0e\u672c\u4e66\u51fa\u7248\u6d41\u7a0b\u7684\u5de5\u4f5c\u4eba\u5458\uff01

        \u5e0c\u671b\u8fd9\u672c\u4e66\u80fd\u591f\u5e2e\u52a9\u5230\u4f60\uff01

        "},{"location":"chapter_preface/","title":"\u7b2c 0 \u7ae0 \u00a0 \u524d\u8a00","text":"

        Abstract

        \u7b97\u6cd5\u72b9\u5982\u7f8e\u5999\u7684\u4ea4\u54cd\u4e50\uff0c\u6bcf\u4e00\u884c\u4ee3\u7801\u90fd\u50cf\u97f5\u5f8b\u822c\u6d41\u6dcc\u3002

        \u613f\u8fd9\u672c\u4e66\u5728\u4f60\u7684\u8111\u6d77\u4e2d\u8f7b\u8f7b\u54cd\u8d77\uff0c\u7559\u4e0b\u72ec\u7279\u800c\u6df1\u523b\u7684\u65cb\u5f8b\u3002

        "},{"location":"chapter_preface/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 0.1 \u00a0 \u5173\u4e8e\u672c\u4e66
        • 0.2 \u00a0 \u5982\u4f55\u4f7f\u7528\u672c\u4e66
        • 0.3 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_preface/about_the_book/","title":"0.1 \u00a0 \u5173\u4e8e\u672c\u4e66","text":"

        \u672c\u9879\u76ee\u65e8\u5728\u521b\u5efa\u4e00\u672c\u5f00\u6e90\u3001\u514d\u8d39\u3001\u5bf9\u65b0\u624b\u53cb\u597d\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u5165\u95e8\u6559\u7a0b\u3002

        • \u5168\u4e66\u91c7\u7528\u52a8\u753b\u56fe\u89e3\uff0c\u5185\u5bb9\u6e05\u6670\u6613\u61c2\u3001\u5b66\u4e60\u66f2\u7ebf\u5e73\u6ed1\uff0c\u5f15\u5bfc\u521d\u5b66\u8005\u63a2\u7d22\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u77e5\u8bc6\u5730\u56fe\u3002
        • \u6e90\u4ee3\u7801\u53ef\u4e00\u952e\u8fd0\u884c\uff0c\u5e2e\u52a9\u8bfb\u8005\u5728\u7ec3\u4e60\u4e2d\u63d0\u5347\u7f16\u7a0b\u6280\u80fd\uff0c\u4e86\u89e3\u7b97\u6cd5\u5de5\u4f5c\u539f\u7406\u548c\u6570\u636e\u7ed3\u6784\u5e95\u5c42\u5b9e\u73b0\u3002
        • \u63d0\u5021\u8bfb\u8005\u4e92\u52a9\u5b66\u4e60\uff0c\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u533a\u63d0\u51fa\u95ee\u9898\u4e0e\u5206\u4eab\u89c1\u89e3\uff0c\u5728\u4ea4\u6d41\u8ba8\u8bba\u4e2d\u5171\u540c\u8fdb\u6b65\u3002
        "},{"location":"chapter_preface/about_the_book/#011","title":"0.1.1 \u00a0 \u8bfb\u8005\u5bf9\u8c61","text":"

        \u82e5\u4f60\u662f\u7b97\u6cd5\u521d\u5b66\u8005\uff0c\u4ece\u672a\u63a5\u89e6\u8fc7\u7b97\u6cd5\uff0c\u6216\u8005\u5df2\u7ecf\u6709\u4e00\u4e9b\u5237\u9898\u7ecf\u9a8c\uff0c\u5bf9\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u6709\u6a21\u7cca\u7684\u8ba4\u8bc6\uff0c\u5728\u4f1a\u4e0e\u4e0d\u4f1a\u4e4b\u95f4\u53cd\u590d\u6a2a\u8df3\uff0c\u90a3\u4e48\u672c\u4e66\u6b63\u662f\u4e3a\u4f60\u91cf\u8eab\u5b9a\u5236\u7684\uff01

        \u5982\u679c\u4f60\u5df2\u7ecf\u79ef\u7d2f\u4e00\u5b9a\u7684\u5237\u9898\u91cf\uff0c\u719f\u6089\u5927\u90e8\u5206\u9898\u578b\uff0c\u90a3\u4e48\u672c\u4e66\u53ef\u52a9\u4f60\u56de\u987e\u4e0e\u68b3\u7406\u7b97\u6cd5\u77e5\u8bc6\u4f53\u7cfb\uff0c\u4ed3\u5e93\u6e90\u4ee3\u7801\u53ef\u4ee5\u5f53\u4f5c\u201c\u5237\u9898\u5de5\u5177\u5e93\u201d\u6216\u201c\u7b97\u6cd5\u5b57\u5178\u201d\u6765\u4f7f\u7528\u3002

        \u82e5\u4f60\u662f\u7b97\u6cd5\u201c\u5927\u795e\u201d\uff0c\u6211\u4eec\u671f\u5f85\u6536\u5230\u4f60\u7684\u5b9d\u8d35\u5efa\u8bae\uff0c\u6216\u8005\u4e00\u8d77\u53c2\u4e0e\u521b\u4f5c\u3002

        \u524d\u7f6e\u6761\u4ef6

        \u4f60\u9700\u8981\u81f3\u5c11\u5177\u5907\u4efb\u4e00\u8bed\u8a00\u7684\u7f16\u7a0b\u57fa\u7840\uff0c\u80fd\u591f\u9605\u8bfb\u548c\u7f16\u5199\u7b80\u5355\u4ee3\u7801\u3002

        "},{"location":"chapter_preface/about_the_book/#012","title":"0.1.2 \u00a0 \u5185\u5bb9\u7ed3\u6784","text":"

        \u672c\u4e66\u7684\u4e3b\u8981\u5185\u5bb9\u5982\u56fe 0-1 \u6240\u793a\u3002

        • \u590d\u6742\u5ea6\u5206\u6790\uff1a\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u7684\u8bc4\u4ef7\u7ef4\u5ea6\u4e0e\u65b9\u6cd5\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u3001\u5e38\u89c1\u7c7b\u578b\u3001\u793a\u4f8b\u7b49\u3002
        • \u6570\u636e\u7ed3\u6784\uff1a\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u6570\u636e\u7ed3\u6784\u7684\u5206\u7c7b\u65b9\u6cd5\u3002\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\u7b49\u6570\u636e\u7ed3\u6784\u7684\u5b9a\u4e49\u3001\u4f18\u7f3a\u70b9\u3001\u5e38\u7528\u64cd\u4f5c\u3001\u5e38\u89c1\u7c7b\u578b\u3001\u5178\u578b\u5e94\u7528\u3001\u5b9e\u73b0\u65b9\u6cd5\u7b49\u3002
        • \u7b97\u6cd5\uff1a\u641c\u7d22\u3001\u6392\u5e8f\u3001\u5206\u6cbb\u3001\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\u3001\u8d2a\u5fc3\u7b49\u7b97\u6cd5\u7684\u5b9a\u4e49\u3001\u4f18\u7f3a\u70b9\u3001\u6548\u7387\u3001\u5e94\u7528\u573a\u666f\u3001\u89e3\u9898\u6b65\u9aa4\u548c\u793a\u4f8b\u95ee\u9898\u7b49\u3002

        \u56fe 0-1 \u00a0 \u672c\u4e66\u4e3b\u8981\u5185\u5bb9

        "},{"location":"chapter_preface/about_the_book/#013","title":"0.1.3 \u00a0 \u81f4\u8c22","text":"

        \u672c\u4e66\u5728\u5f00\u6e90\u793e\u533a\u4f17\u591a\u8d21\u732e\u8005\u7684\u5171\u540c\u52aa\u529b\u4e0b\u4e0d\u65ad\u5b8c\u5584\u3002\u611f\u8c22\u6bcf\u4e00\u4f4d\u6295\u5165\u65f6\u95f4\u4e0e\u7cbe\u529b\u7684\u64b0\u7a3f\u4eba\uff0c\u4ed6\u4eec\u662f\uff08\u6309\u7167 GitHub \u81ea\u52a8\u751f\u6210\u7684\u987a\u5e8f\uff09\uff1akrahets\u3001coderonion\u3001Gonglja\u3001nuomi1\u3001Reanon\u3001justin-tse\u3001hpstory\u3001danielsss\u3001curtishd\u3001night-cruise\u3001S-N-O-R-L-A-X\u3001rongyi\u3001msk397\u3001gvenusleo\u3001khoaxuantu\u3001rivertwilight\u3001K3v123\u3001gyt95\u3001zhuoqinyue\u3001yuelinxin\u3001Zuoxun\u3001mingXta\u3001Phoenix0415\u3001FangYuan33\u3001GN-Yu\u3001longsizhuo\u3001IsChristina\u3001xBLACKICEx\u3001guowei-gong\u3001Cathay-Chen\u3001pengchzn\u3001QiLOL\u3001magentaqin\u3001hello-ikun\u3001JoseHung\u3001qualifier1024\u3001thomasq0\u3001sunshinesDL\u3001L-Super\u3001Guanngxu\u3001Transmigration-zhou\u3001WSL0809\u3001Slone123c\u3001lhxsm\u3001yuan0221\u3001what-is-me\u3001Shyam-Chen\u3001theNefelibatas\u3001longranger2\u3001codeberg-user\u3001xiongsp\u3001JeffersonHuang\u3001prinpal\u3001seven1240\u3001Wonderdch\u3001malone6\u3001xiaomiusa87\u3001gaofer\u3001bluebean-cloud\u3001a16su\u3001SamJin98\u3001hongyun-robot\u3001nanlei\u3001XiaChuerwu\u3001yd-j\u3001iron-irax\u3001mgisr\u3001steventimes\u3001junminhong\u3001heshuyue\u3001danny900714\u3001MolDuM\u3001Nigh\u3001Dr-XYZ\u3001XC-Zero\u3001reeswell\u3001PXG-XPG\u3001NI-SW\u3001Horbin-Magician\u3001Enlightenus\u3001YangXuanyi\u3001beatrix-chan\u3001DullSword\u3001xjr7670\u3001jiaxianhua\u3001qq909244296\u3001iStig\u3001boloboloda\u3001hts0000\u3001gledfish\u3001wenjianmin\u3001keshida\u3001kilikilikid\u3001lclc6\u3001lwbaptx\u3001linyejoe2\u3001liuxjerry\u3001llql1211\u3001fbigm\u3001echo1937\u3001szu17dmy\u3001dshlstarr\u3001Yucao-cy\u3001coderlef\u3001czruby\u3001bongbongbakudan\u3001beintentional\u3001ZongYangL\u3001ZhongYuuu\u3001ZhongGuanbin\u3001hezhizhen\u3001linzeyan\u3001ZJKung\u3001luluxia\u3001xb534\u3001ztkuaikuai\u3001yw-1021\u3001ElaBosak233\u3001baagod\u3001zhouLion\u3001yishangzhang\u3001yi427\u3001yanedie\u3001yabo083\u3001weibk\u3001wangwang105\u3001th1nk3r-ing\u3001tao363\u30014yDX3906\u3001syd168\u3001sslmj2020\u3001smilelsb\u3001siqyka\u3001selear\u3001sdshaoda\u3001Xi-Row\u3001popozhu\u3001nuquist19\u3001noobcodemaker\u3001XiaoK29\u3001chadyi\u3001lyl625760\u3001lucaswangdev\u30010130w\u3001shanghai-Jerry\u3001EJackYang\u3001Javesun99\u3001eltociear\u3001lipusheng\u3001KNChiu\u3001BlindTerran\u3001ShiMaRing\u3001lovelock\u3001FreddieLi\u3001FloranceYeh\u3001fanchenggang\u3001gltianwen\u3001goerll\u3001nedchu\u3001curly210102\u3001CuB3y0nd\u3001KraHsu\u3001CarrotDLaw\u3001youshaoXG\u3001bubble9um\u3001Asashishi\u3001Asa0oo0o0o\u3001fanenr\u3001eagleanurag\u3001akshiterate\u300152coder\u3001foursevenlove\u3001KorsChen\u3001GaochaoZhu\u3001hopkings2008\u3001yang-le\u3001realwujing\u3001Evilrabbit520\u3001Umer-Jahangir\u3001Turing-1024-Lee\u3001Suremotoo\u3001paoxiaomooo\u3001Chieko-Seren\u3001Allen-Scai\u3001ymmmas\u3001Risuntsy\u3001Richard-Zhang1019\u3001RafaelCaso\u3001qingpeng9802\u3001primexiao\u3001Urbaner3\u3001zhongfq\u3001nidhoggfgg\u3001MwumLi\u3001CreatorMetaSky\u3001martinx\u3001ZnYang2018\u3001hugtyftg\u3001logan-qiu\u3001psychelzh\u3001Keynman\u3001KeiichiKasai \u548c KawaiiAsh\u3002

        \u672c\u4e66\u7684\u4ee3\u7801\u5ba1\u9605\u5de5\u4f5c\u7531 coderonion\u3001curtishd\u3001Gonglja\u3001gvenusleo\u3001hpstory\u3001justin-tse\u3001khoaxuantu\u3001krahets\u3001night-cruise\u3001nuomi1\u3001Reanon \u548c rongyi \u5b8c\u6210\uff08\u6309\u7167\u9996\u5b57\u6bcd\u987a\u5e8f\u6392\u5217\uff09\u3002\u611f\u8c22\u4ed6\u4eec\u4ed8\u51fa\u7684\u65f6\u95f4\u4e0e\u7cbe\u529b\uff0c\u6b63\u662f\u4ed6\u4eec\u786e\u4fdd\u4e86\u5404\u8bed\u8a00\u4ee3\u7801\u7684\u89c4\u8303\u4e0e\u7edf\u4e00\u3002

        \u672c\u4e66\u7684\u7e41\u4f53\u4e2d\u6587\u7248\u7531 Shyam-Chen \u548c Dr-XYZ \u5ba1\u9605\uff0c\u82f1\u6587\u7248\u7531 yuelinxin\u3001K3v123\u3001QiLOL\u3001Phoenix0415\u3001SamJin98\u3001yanedie\u3001RafaelCaso\u3001pengchzn\u3001thomasq0 \u548c magentaqin \u5ba1\u9605\u3002\u6b63\u662f\u56e0\u4e3a\u4ed6\u4eec\u7684\u6301\u7eed\u8d21\u732e\uff0c\u8fd9\u672c\u4e66\u624d\u80fd\u591f\u670d\u52a1\u4e8e\u66f4\u5e7f\u6cdb\u7684\u8bfb\u8005\u7fa4\u4f53\uff0c\u611f\u8c22\u4ed6\u4eec\u3002

        \u672c\u4e66\u7684 ePub \u7535\u5b50\u4e66\u751f\u6210\u5de5\u5177\u7531 zhongfq \u5f00\u53d1\u3002\u611f\u8c22\u4ed6\u7684\u8d21\u732e\uff0c\u4e3a\u8bfb\u8005\u63d0\u4f9b\u4e86\u66f4\u52a0\u81ea\u7531\u7684\u9605\u8bfb\u65b9\u5f0f\u3002

        \u5728\u672c\u4e66\u7684\u521b\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u6211\u5f97\u5230\u4e86\u8bb8\u591a\u4eba\u7684\u5e2e\u52a9\u3002

        • \u611f\u8c22\u6211\u5728\u516c\u53f8\u7684\u5bfc\u5e08\u674e\u6c50\u535a\u58eb\uff0c\u5728\u4e00\u6b21\u7545\u8c08\u4e2d\u4f60\u9f13\u52b1\u6211\u201c\u5feb\u884c\u52a8\u8d77\u6765\u201d\uff0c\u575a\u5b9a\u4e86\u6211\u5199\u8fd9\u672c\u4e66\u7684\u51b3\u5fc3\uff1b
        • \u611f\u8c22\u6211\u7684\u5973\u670b\u53cb\u6ce1\u6ce1\u4f5c\u4e3a\u672c\u4e66\u7684\u9996\u4f4d\u8bfb\u8005\uff0c\u4ece\u7b97\u6cd5\u5c0f\u767d\u7684\u89d2\u5ea6\u63d0\u51fa\u8bb8\u591a\u5b9d\u8d35\u5efa\u8bae\uff0c\u4f7f\u5f97\u672c\u4e66\u66f4\u9002\u5408\u65b0\u624b\u9605\u8bfb\uff1b
        • \u611f\u8c22\u817e\u5b9d\u3001\u7426\u5b9d\u3001\u98de\u5b9d\u4e3a\u672c\u4e66\u8d77\u4e86\u4e00\u4e2a\u5bcc\u6709\u521b\u610f\u7684\u540d\u5b57\uff0c\u5524\u8d77\u5927\u5bb6\u5199\u4e0b\u7b2c\u4e00\u884c\u4ee3\u7801\u201cHello World!\u201d\u7684\u7f8e\u597d\u56de\u5fc6\uff1b
        • \u611f\u8c22\u6821\u94e8\u5728\u77e5\u8bc6\u4ea7\u6743\u65b9\u9762\u63d0\u4f9b\u7684\u4e13\u4e1a\u5e2e\u52a9\uff0c\u8fd9\u5bf9\u672c\u5f00\u6e90\u4e66\u7684\u5b8c\u5584\u8d77\u5230\u4e86\u91cd\u8981\u4f5c\u7528\uff1b
        • \u611f\u8c22\u82cf\u6f7c\u4e3a\u672c\u4e66\u8bbe\u8ba1\u4e86\u7cbe\u7f8e\u7684\u5c01\u9762\u548c logo \uff0c\u5e76\u5728\u6211\u7684\u5f3a\u8feb\u75c7\u7684\u9a71\u4f7f\u4e0b\u591a\u6b21\u8010\u5fc3\u4fee\u6539\uff1b
        • \u611f\u8c22 @squidfunk \u63d0\u4f9b\u7684\u6392\u7248\u5efa\u8bae\uff0c\u4ee5\u53ca\u4ed6\u5f00\u53d1\u7684\u5f00\u6e90\u6587\u6863\u4e3b\u9898 Material-for-MkDocs \u3002

        \u5728\u5199\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u6211\u9605\u8bfb\u4e86\u8bb8\u591a\u5173\u4e8e\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u6559\u6750\u548c\u6587\u7ae0\u3002\u8fd9\u4e9b\u4f5c\u54c1\u4e3a\u672c\u4e66\u63d0\u4f9b\u4e86\u4f18\u79c0\u7684\u8303\u672c\uff0c\u786e\u4fdd\u4e86\u672c\u4e66\u5185\u5bb9\u7684\u51c6\u786e\u6027\u4e0e\u54c1\u8d28\u3002\u5728\u6b64\u611f\u8c22\u6240\u6709\u8001\u5e08\u548c\u524d\u8f88\u7684\u6770\u51fa\u8d21\u732e\uff01

        \u672c\u4e66\u5021\u5bfc\u624b\u8111\u5e76\u7528\u7684\u5b66\u4e60\u65b9\u5f0f\uff0c\u5728\u8fd9\u4e00\u70b9\u4e0a\u6211\u6df1\u53d7\u300a\u52a8\u624b\u5b66\u6df1\u5ea6\u5b66\u4e60\u300b\u7684\u542f\u53d1\u3002\u5728\u6b64\u5411\u5404\u4f4d\u8bfb\u8005\u5f3a\u70c8\u63a8\u8350\u8fd9\u672c\u4f18\u79c0\u7684\u8457\u4f5c\u3002

        \u8877\u5fc3\u611f\u8c22\u6211\u7684\u7236\u6bcd\uff0c\u6b63\u662f\u4f60\u4eec\u4e00\u76f4\u4ee5\u6765\u7684\u652f\u6301\u4e0e\u9f13\u52b1\uff0c\u8ba9\u6211\u6709\u673a\u4f1a\u505a\u8fd9\u4ef6\u5bcc\u6709\u8da3\u5473\u7684\u4e8b\u3002

        "},{"location":"chapter_preface/suggestions/","title":"0.2 \u00a0 \u5982\u4f55\u4f7f\u7528\u672c\u4e66","text":"

        Tip

        \u4e3a\u4e86\u83b7\u5f97\u6700\u4f73\u7684\u9605\u8bfb\u4f53\u9a8c\uff0c\u5efa\u8bae\u4f60\u901a\u8bfb\u672c\u8282\u5185\u5bb9\u3002

        "},{"location":"chapter_preface/suggestions/#021","title":"0.2.1 \u00a0 \u884c\u6587\u98ce\u683c\u7ea6\u5b9a","text":"
        • \u6807\u9898\u540e\u6807\u6ce8 * \u7684\u662f\u9009\u8bfb\u7ae0\u8282\uff0c\u5185\u5bb9\u76f8\u5bf9\u56f0\u96be\u3002\u5982\u679c\u4f60\u7684\u65f6\u95f4\u6709\u9650\uff0c\u53ef\u4ee5\u5148\u8df3\u8fc7\u3002
        • \u4e13\u4e1a\u672f\u8bed\u4f1a\u4f7f\u7528\u9ed1\u4f53\uff08\u7eb8\u8d28\u7248\u548c PDF \u7248\uff09\u6216\u6dfb\u52a0\u4e0b\u5212\u7ebf\uff08\u7f51\u9875\u7248\uff09\uff0c\u4f8b\u5982\u6570\u7ec4\uff08array\uff09\u3002\u5efa\u8bae\u8bb0\u4f4f\u5b83\u4eec\uff0c\u4ee5\u4fbf\u9605\u8bfb\u6587\u732e\u3002
        • \u91cd\u70b9\u5185\u5bb9\u548c\u603b\u7ed3\u6027\u8bed\u53e5\u4f1a \u52a0\u7c97\uff0c\u8fd9\u7c7b\u6587\u5b57\u503c\u5f97\u7279\u522b\u5173\u6ce8\u3002
        • \u6709\u7279\u6307\u542b\u4e49\u7684\u8bcd\u53e5\u4f1a\u4f7f\u7528\u201c\u5f15\u53f7\u201d\u6807\u6ce8\uff0c\u4ee5\u907f\u514d\u6b67\u4e49\u3002
        • \u5f53\u6d89\u53ca\u7f16\u7a0b\u8bed\u8a00\u4e4b\u95f4\u4e0d\u4e00\u81f4\u7684\u540d\u8bcd\u65f6\uff0c\u672c\u4e66\u5747\u4ee5 Python \u4e3a\u51c6\uff0c\u4f8b\u5982\u4f7f\u7528 None \u6765\u8868\u793a\u201c\u7a7a\u201d\u3002
        • \u672c\u4e66\u90e8\u5206\u653e\u5f03\u4e86\u7f16\u7a0b\u8bed\u8a00\u7684\u6ce8\u91ca\u89c4\u8303\uff0c\u4ee5\u6362\u53d6\u66f4\u52a0\u7d27\u51d1\u7684\u5185\u5bb9\u6392\u7248\u3002\u6ce8\u91ca\u4e3b\u8981\u5206\u4e3a\u4e09\u79cd\u7c7b\u578b\uff1a\u6807\u9898\u6ce8\u91ca\u3001\u5185\u5bb9\u6ce8\u91ca\u3001\u591a\u884c\u6ce8\u91ca\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        \"\"\"\u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49\"\"\"\n\n# \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n\"\"\"\n\u591a\u884c\n\u6ce8\u91ca\n\"\"\"\n
        /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
        /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
        /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
        /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
        /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
        /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
        /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
        /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
        /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
        /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
        /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
        ### \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 ###\n\n# \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n# \u591a\u884c\n# \u6ce8\u91ca\n
        "},{"location":"chapter_preface/suggestions/#022","title":"0.2.2 \u00a0 \u5728\u52a8\u753b\u56fe\u89e3\u4e2d\u9ad8\u6548\u5b66\u4e60","text":"

        \u76f8\u8f83\u4e8e\u6587\u5b57\uff0c\u89c6\u9891\u548c\u56fe\u7247\u5177\u6709\u66f4\u9ad8\u7684\u4fe1\u606f\u5bc6\u5ea6\u548c\u7ed3\u6784\u5316\u7a0b\u5ea6\uff0c\u66f4\u6613\u4e8e\u7406\u89e3\u3002\u5728\u672c\u4e66\u4e2d\uff0c\u91cd\u70b9\u548c\u96be\u70b9\u77e5\u8bc6\u5c06\u4e3b\u8981\u901a\u8fc7\u52a8\u753b\u4ee5\u56fe\u89e3\u5f62\u5f0f\u5c55\u793a\uff0c\u800c\u6587\u5b57\u5219\u4f5c\u4e3a\u89e3\u91ca\u4e0e\u8865\u5145\u3002

        \u5982\u679c\u4f60\u5728\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u53d1\u73b0\u67d0\u6bb5\u5185\u5bb9\u63d0\u4f9b\u4e86\u5982\u56fe 0-2 \u6240\u793a\u7684\u52a8\u753b\u56fe\u89e3\uff0c\u8bf7\u4ee5\u56fe\u4e3a\u4e3b\u3001\u4ee5\u6587\u5b57\u4e3a\u8f85\uff0c\u7efc\u5408\u4e24\u8005\u6765\u7406\u89e3\u5185\u5bb9\u3002

        \u56fe 0-2 \u00a0 \u52a8\u753b\u56fe\u89e3\u793a\u4f8b

        "},{"location":"chapter_preface/suggestions/#023","title":"0.2.3 \u00a0 \u5728\u4ee3\u7801\u5b9e\u8df5\u4e2d\u52a0\u6df1\u7406\u89e3","text":"

        \u672c\u4e66\u7684\u914d\u5957\u4ee3\u7801\u6258\u7ba1\u5728 GitHub \u4ed3\u5e93\u3002\u5982\u56fe 0-3 \u6240\u793a\uff0c\u6e90\u4ee3\u7801\u9644\u6709\u6d4b\u8bd5\u6837\u4f8b\uff0c\u53ef\u4e00\u952e\u8fd0\u884c\u3002

        \u5982\u679c\u65f6\u95f4\u5141\u8bb8\uff0c\u5efa\u8bae\u4f60\u53c2\u7167\u4ee3\u7801\u81ea\u884c\u6572\u4e00\u904d\u3002\u5982\u679c\u5b66\u4e60\u65f6\u95f4\u6709\u9650\uff0c\u8bf7\u81f3\u5c11\u901a\u8bfb\u5e76\u8fd0\u884c\u6240\u6709\u4ee3\u7801\u3002

        \u4e0e\u9605\u8bfb\u4ee3\u7801\u76f8\u6bd4\uff0c\u7f16\u5199\u4ee3\u7801\u7684\u8fc7\u7a0b\u5f80\u5f80\u80fd\u5e26\u6765\u66f4\u591a\u6536\u83b7\u3002\u52a8\u624b\u5b66\uff0c\u624d\u662f\u771f\u7684\u5b66\u3002

        \u56fe 0-3 \u00a0 \u8fd0\u884c\u4ee3\u7801\u793a\u4f8b

        \u8fd0\u884c\u4ee3\u7801\u7684\u524d\u7f6e\u5de5\u4f5c\u4e3b\u8981\u5206\u4e3a\u4e09\u6b65\u3002

        \u7b2c\u4e00\u6b65\uff1a\u5b89\u88c5\u672c\u5730\u7f16\u7a0b\u73af\u5883\u3002\u8bf7\u53c2\u7167\u9644\u5f55\u6240\u793a\u7684\u6559\u7a0b\u8fdb\u884c\u5b89\u88c5\uff0c\u5982\u679c\u5df2\u5b89\u88c5\uff0c\u5219\u53ef\u8df3\u8fc7\u6b64\u6b65\u9aa4\u3002

        \u7b2c\u4e8c\u6b65\uff1a\u514b\u9686\u6216\u4e0b\u8f7d\u4ee3\u7801\u4ed3\u5e93\u3002\u524d\u5f80 GitHub \u4ed3\u5e93\u3002\u5982\u679c\u5df2\u7ecf\u5b89\u88c5 Git \uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u514b\u9686\u672c\u4ed3\u5e93\uff1a

        git clone https://github.com/krahets/hello-algo.git\n

        \u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5728\u56fe 0-4 \u6240\u793a\u7684\u4f4d\u7f6e\uff0c\u70b9\u51fb\u201cDownload ZIP\u201d\u6309\u94ae\u76f4\u63a5\u4e0b\u8f7d\u4ee3\u7801\u538b\u7f29\u5305\uff0c\u7136\u540e\u5728\u672c\u5730\u89e3\u538b\u5373\u53ef\u3002

        \u56fe 0-4 \u00a0 \u514b\u9686\u4ed3\u5e93\u4e0e\u4e0b\u8f7d\u4ee3\u7801

        \u7b2c\u4e09\u6b65\uff1a\u8fd0\u884c\u6e90\u4ee3\u7801\u3002\u5982\u56fe 0-5 \u6240\u793a\uff0c\u5bf9\u4e8e\u9876\u90e8\u6807\u6709\u6587\u4ef6\u540d\u79f0\u7684\u4ee3\u7801\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4ed3\u5e93\u7684 codes \u6587\u4ef6\u5939\u5185\u627e\u5230\u5bf9\u5e94\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u3002\u6e90\u4ee3\u7801\u6587\u4ef6\u53ef\u4e00\u952e\u8fd0\u884c\uff0c\u5c06\u5e2e\u52a9\u4f60\u8282\u7701\u4e0d\u5fc5\u8981\u7684\u8c03\u8bd5\u65f6\u95f4\uff0c\u8ba9\u4f60\u80fd\u591f\u4e13\u6ce8\u4e8e\u5b66\u4e60\u5185\u5bb9\u3002

        \u56fe 0-5 \u00a0 \u4ee3\u7801\u5757\u4e0e\u5bf9\u5e94\u7684\u6e90\u4ee3\u7801\u6587\u4ef6

        \u9664\u4e86\u672c\u5730\u8fd0\u884c\u4ee3\u7801\uff0c\u7f51\u9875\u7248\u8fd8\u652f\u6301 Python \u4ee3\u7801\u7684\u53ef\u89c6\u5316\u8fd0\u884c\uff08\u57fa\u4e8e pythontutor \u5b9e\u73b0\uff09\u3002\u5982\u56fe 0-6 \u6240\u793a\uff0c\u4f60\u53ef\u4ee5\u70b9\u51fb\u4ee3\u7801\u5757\u4e0b\u65b9\u7684\u201c\u53ef\u89c6\u5316\u8fd0\u884c\u201d\u6765\u5c55\u5f00\u89c6\u56fe\uff0c\u89c2\u5bdf\u7b97\u6cd5\u4ee3\u7801\u7684\u6267\u884c\u8fc7\u7a0b\uff1b\u4e5f\u53ef\u4ee5\u70b9\u51fb\u201c\u5168\u5c4f\u89c2\u770b\u201d\uff0c\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u9605\u89c8\u4f53\u9a8c\u3002

        \u56fe 0-6 \u00a0 Python \u4ee3\u7801\u7684\u53ef\u89c6\u5316\u8fd0\u884c

        "},{"location":"chapter_preface/suggestions/#024","title":"0.2.4 \u00a0 \u5728\u63d0\u95ee\u8ba8\u8bba\u4e2d\u5171\u540c\u6210\u957f","text":"

        \u5728\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u8bf7\u4e0d\u8981\u8f7b\u6613\u8df3\u8fc7\u90a3\u4e9b\u6ca1\u5b66\u660e\u767d\u7684\u77e5\u8bc6\u70b9\u3002\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u63d0\u51fa\u4f60\u7684\u95ee\u9898\uff0c\u6211\u548c\u5c0f\u4f19\u4f34\u4eec\u5c06\u7aed\u8bda\u4e3a\u4f60\u89e3\u7b54\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u53ef\u5728\u4e24\u5929\u5185\u56de\u590d\u3002

        \u5982\u56fe 0-7 \u6240\u793a\uff0c\u7f51\u9875\u7248\u6bcf\u4e2a\u7ae0\u8282\u7684\u5e95\u90e8\u90fd\u914d\u6709\u8bc4\u8bba\u533a\u3002\u5e0c\u671b\u4f60\u80fd\u591a\u5173\u6ce8\u8bc4\u8bba\u533a\u7684\u5185\u5bb9\u3002\u4e00\u65b9\u9762\uff0c\u4f60\u53ef\u4ee5\u4e86\u89e3\u5927\u5bb6\u9047\u5230\u7684\u95ee\u9898\uff0c\u4ece\u800c\u67e5\u6f0f\u8865\u7f3a\uff0c\u6fc0\u53d1\u66f4\u6df1\u5165\u7684\u601d\u8003\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u671f\u5f85\u4f60\u80fd\u6177\u6168\u5730\u56de\u7b54\u5176\u4ed6\u5c0f\u4f19\u4f34\u7684\u95ee\u9898\uff0c\u5206\u4eab\u4f60\u7684\u89c1\u89e3\uff0c\u5e2e\u52a9\u4ed6\u4eba\u8fdb\u6b65\u3002

        \u56fe 0-7 \u00a0 \u8bc4\u8bba\u533a\u793a\u4f8b

        "},{"location":"chapter_preface/suggestions/#025","title":"0.2.5 \u00a0 \u7b97\u6cd5\u5b66\u4e60\u8def\u7ebf","text":"

        \u4ece\u603b\u4f53\u4e0a\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u8fc7\u7a0b\u5212\u5206\u4e3a\u4e09\u4e2a\u9636\u6bb5\u3002

        1. \u9636\u6bb5\u4e00\uff1a\u7b97\u6cd5\u5165\u95e8\u3002\u6211\u4eec\u9700\u8981\u719f\u6089\u5404\u79cd\u6570\u636e\u7ed3\u6784\u7684\u7279\u70b9\u548c\u7528\u6cd5\uff0c\u5b66\u4e60\u4e0d\u540c\u7b97\u6cd5\u7684\u539f\u7406\u3001\u6d41\u7a0b\u3001\u7528\u9014\u548c\u6548\u7387\u7b49\u65b9\u9762\u7684\u5185\u5bb9\u3002
        2. \u9636\u6bb5\u4e8c\uff1a\u5237\u7b97\u6cd5\u9898\u3002\u5efa\u8bae\u4ece\u70ed\u95e8\u9898\u76ee\u5f00\u5237\uff0c\u5148\u79ef\u7d2f\u81f3\u5c11 100 \u9053\u9898\u76ee\uff0c\u719f\u6089\u4e3b\u6d41\u7684\u7b97\u6cd5\u95ee\u9898\u3002\u521d\u6b21\u5237\u9898\u65f6\uff0c\u201c\u77e5\u8bc6\u9057\u5fd8\u201d\u53ef\u80fd\u662f\u4e00\u4e2a\u6311\u6218\uff0c\u4f46\u8bf7\u653e\u5fc3\uff0c\u8fd9\u662f\u5f88\u6b63\u5e38\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u6309\u7167\u201c\u827e\u5bbe\u6d69\u65af\u9057\u5fd8\u66f2\u7ebf\u201d\u6765\u590d\u4e60\u9898\u76ee\uff0c\u901a\u5e38\u5728\u8fdb\u884c 3\uff5e5 \u8f6e\u7684\u91cd\u590d\u540e\uff0c\u5c31\u80fd\u5c06\u5176\u7262\u8bb0\u5728\u5fc3\u3002\u63a8\u8350\u7684\u9898\u5355\u548c\u5237\u9898\u8ba1\u5212\u8bf7\u89c1\u6b64 GitHub \u4ed3\u5e93\u3002
        3. \u9636\u6bb5\u4e09\uff1a\u642d\u5efa\u77e5\u8bc6\u4f53\u7cfb\u3002\u5728\u5b66\u4e60\u65b9\u9762\uff0c\u6211\u4eec\u53ef\u4ee5\u9605\u8bfb\u7b97\u6cd5\u4e13\u680f\u6587\u7ae0\u3001\u89e3\u9898\u6846\u67b6\u548c\u7b97\u6cd5\u6559\u6750\uff0c\u4ee5\u4e0d\u65ad\u4e30\u5bcc\u77e5\u8bc6\u4f53\u7cfb\u3002\u5728\u5237\u9898\u65b9\u9762\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u91c7\u7528\u8fdb\u9636\u5237\u9898\u7b56\u7565\uff0c\u5982\u6309\u4e13\u9898\u5206\u7c7b\u3001\u4e00\u9898\u591a\u89e3\u3001\u4e00\u89e3\u591a\u9898\u7b49\uff0c\u76f8\u5173\u7684\u5237\u9898\u5fc3\u5f97\u53ef\u4ee5\u5728\u5404\u4e2a\u793e\u533a\u627e\u5230\u3002

        \u5982\u56fe 0-8 \u6240\u793a\uff0c\u672c\u4e66\u5185\u5bb9\u4e3b\u8981\u6db5\u76d6\u201c\u9636\u6bb5\u4e00\u201d\uff0c\u65e8\u5728\u5e2e\u52a9\u4f60\u66f4\u9ad8\u6548\u5730\u5c55\u5f00\u9636\u6bb5\u4e8c\u548c\u9636\u6bb5\u4e09\u7684\u5b66\u4e60\u3002

        \u56fe 0-8 \u00a0 \u7b97\u6cd5\u5b66\u4e60\u8def\u7ebf

        "},{"location":"chapter_preface/summary/","title":"0.3 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_preface/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u672c\u4e66\u7684\u4e3b\u8981\u53d7\u4f17\u662f\u7b97\u6cd5\u521d\u5b66\u8005\u3002\u5982\u679c\u4f60\u5df2\u6709\u4e00\u5b9a\u57fa\u7840\uff0c\u672c\u4e66\u80fd\u5e2e\u52a9\u4f60\u7cfb\u7edf\u56de\u987e\u7b97\u6cd5\u77e5\u8bc6\uff0c\u4e66\u4e2d\u6e90\u4ee3\u7801\u4e5f\u53ef\u4f5c\u4e3a\u201c\u5237\u9898\u5de5\u5177\u5e93\u201d\u4f7f\u7528\u3002
        • \u4e66\u4e2d\u5185\u5bb9\u4e3b\u8981\u5305\u62ec\u590d\u6742\u5ea6\u5206\u6790\u3001\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u4e09\u90e8\u5206\uff0c\u6db5\u76d6\u4e86\u8be5\u9886\u57df\u7684\u5927\u90e8\u5206\u4e3b\u9898\u3002
        • \u5bf9\u4e8e\u7b97\u6cd5\u65b0\u624b\uff0c\u5728\u521d\u5b66\u9636\u6bb5\u9605\u8bfb\u4e00\u672c\u5165\u95e8\u4e66\u81f3\u5173\u91cd\u8981\uff0c\u53ef\u4ee5\u5c11\u8d70\u8bb8\u591a\u5f2f\u8def\u3002
        • \u4e66\u4e2d\u7684\u52a8\u753b\u56fe\u89e3\u901a\u5e38\u7528\u4e8e\u4ecb\u7ecd\u91cd\u70b9\u548c\u96be\u70b9\u77e5\u8bc6\u3002\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u5e94\u7ed9\u4e88\u8fd9\u4e9b\u5185\u5bb9\u66f4\u591a\u5173\u6ce8\u3002
        • \u5b9e\u8df5\u4e43\u5b66\u4e60\u7f16\u7a0b\u4e4b\u6700\u4f73\u9014\u5f84\u3002\u5f3a\u70c8\u5efa\u8bae\u8fd0\u884c\u6e90\u4ee3\u7801\u5e76\u4eb2\u81ea\u6572\u4ee3\u7801\u3002
        • \u672c\u4e66\u7f51\u9875\u7248\u7684\u6bcf\u4e2a\u7ae0\u8282\u90fd\u8bbe\u6709\u8bc4\u8bba\u533a\uff0c\u6b22\u8fce\u968f\u65f6\u5206\u4eab\u4f60\u7684\u7591\u60d1\u4e0e\u89c1\u89e3\u3002
        "},{"location":"chapter_reference/","title":"\u53c2\u8003\u6587\u732e","text":"

        [1] Thomas H. Cormen, et al. Introduction to Algorithms (3rd Edition).

        [2] Aditya Bhargava. Grokking Algorithms: An Illustrated Guide for Programmers and Other Curious People (1st Edition).

        [3] Robert Sedgewick, et al. Algorithms (4th Edition).

        [4] \u4e25\u851a\u654f. \u6570\u636e\u7ed3\u6784\uff08C \u8bed\u8a00\u7248\uff09.

        [5] \u9093\u4fca\u8f89. \u6570\u636e\u7ed3\u6784\uff08C++ \u8bed\u8a00\u7248\uff0c\u7b2c\u4e09\u7248\uff09.

        [6] \u9a6c\u514b \u827e\u4f26 \u7ef4\u65af\u8457\uff0c\u9648\u8d8a\u8bd1. \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u5206\u6790\uff1aJava\u8bed\u8a00\u63cf\u8ff0\uff08\u7b2c\u4e09\u7248\uff09.

        [7] \u7a0b\u6770. \u5927\u8bdd\u6570\u636e\u7ed3\u6784.

        [8] \u738b\u4e89. \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u4e4b\u7f8e.

        [9] Gayle Laakmann McDowell. Cracking the Coding Interview: 189 Programming Questions and Solutions (6th Edition).

        [10] Aston Zhang, et al. Dive into Deep Learning.

        "},{"location":"chapter_searching/","title":"\u7b2c 10 \u7ae0 \u00a0 \u641c\u7d22","text":"

        Abstract

        \u641c\u7d22\u662f\u4e00\u573a\u672a\u77e5\u7684\u5192\u9669\uff0c\u6211\u4eec\u6216\u8bb8\u9700\u8981\u8d70\u904d\u795e\u79d8\u7a7a\u95f4\u7684\u6bcf\u4e2a\u89d2\u843d\uff0c\u53c8\u6216\u8bb8\u53ef\u4ee5\u5feb\u901f\u9501\u5b9a\u76ee\u6807\u3002

        \u5728\u8fd9\u573a\u5bfb\u89c5\u4e4b\u65c5\u4e2d\uff0c\u6bcf\u4e00\u6b21\u63a2\u7d22\u90fd\u53ef\u80fd\u5f97\u5230\u4e00\u4e2a\u672a\u66fe\u6599\u60f3\u7684\u7b54\u6848\u3002

        "},{"location":"chapter_searching/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 10.1 \u00a0 \u4e8c\u5206\u67e5\u627e
        • 10.2 \u00a0 \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9
        • 10.3 \u00a0 \u4e8c\u5206\u67e5\u627e\u8fb9\u754c
        • 10.4 \u00a0 \u54c8\u5e0c\u4f18\u5316\u7b56\u7565
        • 10.5 \u00a0 \u91cd\u8bc6\u641c\u7d22\u7b97\u6cd5
        • 10.6 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_searching/binary_search/","title":"10.1 \u00a0 \u4e8c\u5206\u67e5\u627e","text":"

        \u4e8c\u5206\u67e5\u627e\uff08binary search\uff09\u662f\u4e00\u79cd\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u9ad8\u6548\u641c\u7d22\u7b97\u6cd5\u3002\u5b83\u5229\u7528\u6570\u636e\u7684\u6709\u5e8f\u6027\uff0c\u6bcf\u8f6e\u7f29\u5c0f\u4e00\u534a\u641c\u7d22\u8303\u56f4\uff0c\u76f4\u81f3\u627e\u5230\u76ee\u6807\u5143\u7d20\u6216\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u4e3a\u6b62\u3002

        Question

        \u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4 nums \uff0c\u5143\u7d20\u6309\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u6392\u5217\u4e14\u4e0d\u91cd\u590d\u3002\u8bf7\u67e5\u627e\u5e76\u8fd4\u56de\u5143\u7d20 target \u5728\u8be5\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\u3002\u82e5\u6570\u7ec4\u4e0d\u5305\u542b\u8be5\u5143\u7d20\uff0c\u5219\u8fd4\u56de \\(-1\\) \u3002\u793a\u4f8b\u5982\u56fe 10-1 \u6240\u793a\u3002

        \u56fe 10-1 \u00a0 \u4e8c\u5206\u67e5\u627e\u793a\u4f8b\u6570\u636e

        \u5982\u56fe 10-2 \u6240\u793a\uff0c\u6211\u4eec\u5148\u521d\u59cb\u5316\u6307\u9488 \\(i = 0\\) \u548c \\(j = n - 1\\) \uff0c\u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u548c\u5c3e\u5143\u7d20\uff0c\u4ee3\u8868\u641c\u7d22\u533a\u95f4 \\([0, n - 1]\\) \u3002\u8bf7\u6ce8\u610f\uff0c\u4e2d\u62ec\u53f7\u8868\u793a\u95ed\u533a\u95f4\uff0c\u5176\u5305\u542b\u8fb9\u754c\u503c\u672c\u8eab\u3002

        \u63a5\u4e0b\u6765\uff0c\u5faa\u73af\u6267\u884c\u4ee5\u4e0b\u4e24\u6b65\u3002

        1. \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 \\(m = \\lfloor {(i + j) / 2} \\rfloor\\) \uff0c\u5176\u4e2d \\(\\lfloor \\: \\rfloor\\) \u8868\u793a\u5411\u4e0b\u53d6\u6574\u64cd\u4f5c\u3002
        2. \u5224\u65ad nums[m] \u548c target \u7684\u5927\u5c0f\u5173\u7cfb\uff0c\u5206\u4e3a\u4ee5\u4e0b\u4e09\u79cd\u60c5\u51b5\u3002
          1. \u5f53 nums[m] < target \u65f6\uff0c\u8bf4\u660e target \u5728\u533a\u95f4 \\([m + 1, j]\\) \u4e2d\uff0c\u56e0\u6b64\u6267\u884c \\(i = m + 1\\) \u3002
          2. \u5f53 nums[m] > target \u65f6\uff0c\u8bf4\u660e target \u5728\u533a\u95f4 \\([i, m - 1]\\) \u4e2d\uff0c\u56e0\u6b64\u6267\u884c \\(j = m - 1\\) \u3002
          3. \u5f53 nums[m] = target \u65f6\uff0c\u8bf4\u660e\u627e\u5230 target \uff0c\u56e0\u6b64\u8fd4\u56de\u7d22\u5f15 \\(m\\) \u3002

        \u82e5\u6570\u7ec4\u4e0d\u5305\u542b\u76ee\u6807\u5143\u7d20\uff0c\u641c\u7d22\u533a\u95f4\u6700\u7ec8\u4f1a\u7f29\u5c0f\u4e3a\u7a7a\u3002\u6b64\u65f6\u8fd4\u56de \\(-1\\) \u3002

        <1><2><3><4><5><6><7>

        \u56fe 10-2 \u00a0 \u4e8c\u5206\u67e5\u627e\u6d41\u7a0b

        \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e \\(i\\) \u548c \\(j\\) \u90fd\u662f int \u7c7b\u578b\uff0c\u56e0\u6b64 \\(i + j\\) \u53ef\u80fd\u4f1a\u8d85\u51fa int \u7c7b\u578b\u7684\u53d6\u503c\u8303\u56f4\u3002\u4e3a\u4e86\u907f\u514d\u5927\u6570\u8d8a\u754c\uff0c\u6211\u4eec\u901a\u5e38\u91c7\u7528\u516c\u5f0f \\(m = \\lfloor {i + (j - i) / 2} \\rfloor\\) \u6765\u8ba1\u7b97\u4e2d\u70b9\u3002

        \u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search.py
        def binary_search(nums: list[int], target: int) -> int:\n    \"\"\"\u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09\"\"\"\n    # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n    i, j = 0, len(nums) - 1\n    # \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n    while i <= j:\n        # \u7406\u8bba\u4e0a Python \u7684\u6570\u5b57\u53ef\u4ee5\u65e0\u9650\u5927\uff08\u53d6\u51b3\u4e8e\u5185\u5b58\u5927\u5c0f\uff09\uff0c\u65e0\u987b\u8003\u8651\u5927\u6570\u8d8a\u754c\u95ee\u9898\n        m = (i + j) // 2  # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m] < target:\n            i = m + 1  # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        elif nums[m] > target:\n            j = m - 1  # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        else:\n            return m  # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n    return -1  # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n
        binary_search.cpp
        /* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(vector<int> &nums, int target) {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n    int i = 0, j = nums.size() - 1;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target)    // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1;\n        else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1;\n        else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.java
        /* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(int[] nums, int target) {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n    int i = 0, j = nums.length - 1;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1;\n        else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1;\n        else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.cs
        /* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint BinarySearch(int[] nums, int target) {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n    int i = 0, j = nums.Length - 1;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n    while (i <= j) {\n        int m = i + (j - i) / 2;   // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target)      // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1;\n        else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1;\n        else                       // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.go
        /* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunc binarySearch(nums []int, target int) int {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n    i, j := 0, len(nums)-1\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n    for i <= j {\n        m := i + (j-i)/2      // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1\n        } else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1\n        } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m\n        }\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1\n}\n
        binary_search.swift
        /* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunc binarySearch(nums: [Int], target: Int) -> Int {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n    var i = nums.startIndex\n    var j = nums.endIndex - 1\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n    while i <= j {\n        let m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1\n        } else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1\n        } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m\n        }\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1\n}\n
        binary_search.js
        /* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunction binarySearch(nums, target) {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n    let i = 0,\n        j = nums.length - 1;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n    while (i <= j) {\n        // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m \uff0c\u4f7f\u7528 parseInt() \u5411\u4e0b\u53d6\u6574\n        const m = parseInt(i + (j - i) / 2);\n        if (nums[m] < target)\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1;\n        else if (nums[m] > target)\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1;\n        else return m; // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.ts
        /* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunction binarySearch(nums: number[], target: number): number {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n    let i = 0,\n        j = nums.length - 1;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n    while (i <= j) {\n        // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        const m = Math.floor(i + (j - i) / 2);\n        if (nums[m] < target) {\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1;\n        } else if (nums[m] > target) {\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1;\n        } else {\n            // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n        }\n    }\n    return -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\n
        binary_search.dart
        /* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(List<int> nums, int target) {\n  // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n  int i = 0, j = nums.length - 1;\n  // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n  while (i <= j) {\n    int m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    if (nums[m] < target) {\n      // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n      i = m + 1;\n    } else if (nums[m] > target) {\n      // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n      j = m - 1;\n    } else {\n      // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n      return m;\n    }\n  }\n  // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n  return -1;\n}\n
        binary_search.rs
        /* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfn binary_search(nums: &[i32], target: i32) -> i32 {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n    let mut i = 0;\n    let mut j = nums.len() as i32 - 1;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n    while i <= j {\n        let m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m as usize] < target {\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1;\n        } else if nums[m as usize] > target {\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1;\n        } else {\n            // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n        }\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.c
        /* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(int *nums, int len, int target) {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n    int i = 0, j = len - 1;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target)    // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1;\n        else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1;\n        else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.kt
        /* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfun binarySearch(nums: IntArray, target: Int): Int {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n    var i = 0\n    var j = nums.size - 1\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n    while (i <= j) {\n        val m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1\n        else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1\n        else  // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1\n}\n
        binary_search.rb
        ### \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 ###\ndef binary_search(nums, target)\n  # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n  i, j = 0, nums.length - 1\n\n  # \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n  while i <= j\n    # \u7406\u8bba\u4e0a Ruby \u7684\u6570\u5b57\u53ef\u4ee5\u65e0\u9650\u5927\uff08\u53d6\u51b3\u4e8e\u5185\u5b58\u5927\u5c0f\uff09\uff0c\u65e0\u987b\u8003\u8651\u5927\u6570\u8d8a\u754c\u95ee\u9898\n    m = (i + j) / 2   # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n\n    if nums[m] < target\n      i = m + 1 # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n    elsif nums[m] > target\n      j = m - 1 # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n    else\n      return m  # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n    end\n  end\n\n  -1  # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \uff1a\u5728\u4e8c\u5206\u5faa\u73af\u4e2d\uff0c\u533a\u95f4\u6bcf\u8f6e\u7f29\u5c0f\u4e00\u534a\uff0c\u56e0\u6b64\u5faa\u73af\u6b21\u6570\u4e3a \\(\\log_2 n\\) \u3002

        \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff1a\u6307\u9488 \\(i\\) \u548c \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7a7a\u95f4\u3002

        "},{"location":"chapter_searching/binary_search/#1011","title":"10.1.1 \u00a0 \u533a\u95f4\u8868\u793a\u65b9\u6cd5","text":"

        \u9664\u4e86\u4e0a\u8ff0\u53cc\u95ed\u533a\u95f4\u5916\uff0c\u5e38\u89c1\u7684\u533a\u95f4\u8868\u793a\u8fd8\u6709\u201c\u5de6\u95ed\u53f3\u5f00\u201d\u533a\u95f4\uff0c\u5b9a\u4e49\u4e3a \\([0, n)\\) \uff0c\u5373\u5de6\u8fb9\u754c\u5305\u542b\u81ea\u8eab\uff0c\u53f3\u8fb9\u754c\u4e0d\u5305\u542b\u81ea\u8eab\u3002\u5728\u8be5\u8868\u793a\u4e0b\uff0c\u533a\u95f4 \\([i, j)\\) \u5728 \\(i = j\\) \u65f6\u4e3a\u7a7a\u3002

        \u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u8be5\u8868\u793a\u5b9e\u73b0\u5177\u6709\u76f8\u540c\u529f\u80fd\u7684\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search.py
        def binary_search_lcro(nums: list[int], target: int) -> int:\n    \"\"\"\u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09\"\"\"\n    # \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n    i, j = 0, len(nums)\n    # \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n    while i < j:\n        m = (i + j) // 2  # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m] < target:\n            i = m + 1  # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n        elif nums[m] > target:\n            j = m  # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n        else:\n            return m  # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n    return -1  # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n
        binary_search.cpp
        /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nint binarySearchLCRO(vector<int> &nums, int target) {\n    // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n    int i = 0, j = nums.size();\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n    while (i < j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target)    // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n            i = m + 1;\n        else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n            j = m;\n        else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.java
        /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nint binarySearchLCRO(int[] nums, int target) {\n    // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n    int i = 0, j = nums.length;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n    while (i < j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n            i = m + 1;\n        else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n            j = m;\n        else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.cs
        /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nint BinarySearchLCRO(int[] nums, int target) {\n    // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n    int i = 0, j = nums.Length;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n    while (i < j) {\n        int m = i + (j - i) / 2;   // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target)      // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n            i = m + 1;\n        else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n            j = m;\n        else                       // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.go
        /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nfunc binarySearchLCRO(nums []int, target int) int {\n    // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n    i, j := 0, len(nums)\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n    for i < j {\n        m := i + (j-i)/2      // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n            i = m + 1\n        } else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n            j = m\n        } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m\n        }\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1\n}\n
        binary_search.swift
        /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nfunc binarySearchLCRO(nums: [Int], target: Int) -> Int {\n    // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n    var i = nums.startIndex\n    var j = nums.endIndex\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n    while i < j {\n        let m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n            i = m + 1\n        } else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n            j = m\n        } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m\n        }\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1\n}\n
        binary_search.js
        /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nfunction binarySearchLCRO(nums, target) {\n    // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n    let i = 0,\n        j = nums.length;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n    while (i < j) {\n        // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m \uff0c\u4f7f\u7528 parseInt() \u5411\u4e0b\u53d6\u6574\n        const m = parseInt(i + (j - i) / 2);\n        if (nums[m] < target)\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n            i = m + 1;\n        else if (nums[m] > target)\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n            j = m;\n        // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        else return m;\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.ts
        /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nfunction binarySearchLCRO(nums: number[], target: number): number {\n    // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n    let i = 0,\n        j = nums.length;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n    while (i < j) {\n        // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        const m = Math.floor(i + (j - i) / 2);\n        if (nums[m] < target) {\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n            i = m + 1;\n        } else if (nums[m] > target) {\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n            j = m;\n        } else {\n            // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n        }\n    }\n    return -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\n
        binary_search.dart
        /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nint binarySearchLCRO(List<int> nums, int target) {\n  // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n  int i = 0, j = nums.length;\n  // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n  while (i < j) {\n    int m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    if (nums[m] < target) {\n      // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n      i = m + 1;\n    } else if (nums[m] > target) {\n      // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n      j = m;\n    } else {\n      // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n      return m;\n    }\n  }\n  // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n  return -1;\n}\n
        binary_search.rs
        /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nfn binary_search_lcro(nums: &[i32], target: i32) -> i32 {\n    // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n    let mut i = 0;\n    let mut j = nums.len() as i32;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n    while i < j {\n        let m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m as usize] < target {\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n            i = m + 1;\n        } else if nums[m as usize] > target {\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n            j = m;\n        } else {\n            // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n        }\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.c
        /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nint binarySearchLCRO(int *nums, int len, int target) {\n    // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n    int i = 0, j = len;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n    while (i < j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target)    // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n            i = m + 1;\n        else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n            j = m;\n        else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.kt
        /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nfun binarySearchLCRO(nums: IntArray, target: Int): Int {\n    // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n    var i = 0\n    var j = nums.size\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n    while (i < j) {\n        val m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n            i = m + 1\n        else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n            j = m\n        else  // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1\n}\n
        binary_search.rb
        ### \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 ###\ndef binary_search_lcro(nums, target)\n  # \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n  i, j = 0, nums.length\n\n  # \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n  while i < j\n    # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    m = (i + j) / 2\n\n    if nums[m] < target\n      i = m + 1 # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n    elsif nums[m] > target\n      j = m - 1 # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n    else\n      return m  # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n    end\n  end\n\n  -1  # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5982\u56fe 10-3 \u6240\u793a\uff0c\u5728\u4e24\u79cd\u533a\u95f4\u8868\u793a\u4e0b\uff0c\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u7684\u521d\u59cb\u5316\u3001\u5faa\u73af\u6761\u4ef6\u548c\u7f29\u5c0f\u533a\u95f4\u64cd\u4f5c\u7686\u6709\u6240\u4e0d\u540c\u3002

        \u7531\u4e8e\u201c\u53cc\u95ed\u533a\u95f4\u201d\u8868\u793a\u4e2d\u7684\u5de6\u53f3\u8fb9\u754c\u90fd\u88ab\u5b9a\u4e49\u4e3a\u95ed\u533a\u95f4\uff0c\u56e0\u6b64\u901a\u8fc7\u6307\u9488 \\(i\\) \u548c\u6307\u9488 \\(j\\) \u7f29\u5c0f\u533a\u95f4\u7684\u64cd\u4f5c\u4e5f\u662f\u5bf9\u79f0\u7684\u3002\u8fd9\u6837\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\uff0c\u56e0\u6b64\u4e00\u822c\u5efa\u8bae\u91c7\u7528\u201c\u53cc\u95ed\u533a\u95f4\u201d\u7684\u5199\u6cd5\u3002

        \u56fe 10-3 \u00a0 \u4e24\u79cd\u533a\u95f4\u5b9a\u4e49

        "},{"location":"chapter_searching/binary_search/#1012","title":"10.1.2 \u00a0 \u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"

        \u4e8c\u5206\u67e5\u627e\u5728\u65f6\u95f4\u548c\u7a7a\u95f4\u65b9\u9762\u90fd\u6709\u8f83\u597d\u7684\u6027\u80fd\u3002

        • \u4e8c\u5206\u67e5\u627e\u7684\u65f6\u95f4\u6548\u7387\u9ad8\u3002\u5728\u5927\u6570\u636e\u91cf\u4e0b\uff0c\u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5177\u6709\u663e\u8457\u4f18\u52bf\u3002\u4f8b\u5982\uff0c\u5f53\u6570\u636e\u5927\u5c0f \\(n = 2^{20}\\) \u65f6\uff0c\u7ebf\u6027\u67e5\u627e\u9700\u8981 \\(2^{20} = 1048576\\) \u8f6e\u5faa\u73af\uff0c\u800c\u4e8c\u5206\u67e5\u627e\u4ec5\u9700 \\(\\log_2 2^{20} = 20\\) \u8f6e\u5faa\u73af\u3002
        • \u4e8c\u5206\u67e5\u627e\u65e0\u987b\u989d\u5916\u7a7a\u95f4\u3002\u76f8\u8f83\u4e8e\u9700\u8981\u501f\u52a9\u989d\u5916\u7a7a\u95f4\u7684\u641c\u7d22\u7b97\u6cd5\uff08\u4f8b\u5982\u54c8\u5e0c\u67e5\u627e\uff09\uff0c\u4e8c\u5206\u67e5\u627e\u66f4\u52a0\u8282\u7701\u7a7a\u95f4\u3002

        \u7136\u800c\uff0c\u4e8c\u5206\u67e5\u627e\u5e76\u975e\u9002\u7528\u4e8e\u6240\u6709\u60c5\u51b5\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002

        • \u4e8c\u5206\u67e5\u627e\u4ec5\u9002\u7528\u4e8e\u6709\u5e8f\u6570\u636e\u3002\u82e5\u8f93\u5165\u6570\u636e\u65e0\u5e8f\uff0c\u4e3a\u4e86\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u800c\u4e13\u95e8\u8fdb\u884c\u6392\u5e8f\uff0c\u5f97\u4e0d\u507f\u5931\u3002\u56e0\u4e3a\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3a \\(O(n \\log n)\\) \uff0c\u6bd4\u7ebf\u6027\u67e5\u627e\u548c\u4e8c\u5206\u67e5\u627e\u90fd\u66f4\u9ad8\u3002\u5bf9\u4e8e\u9891\u7e41\u63d2\u5165\u5143\u7d20\u7684\u573a\u666f\uff0c\u4e3a\u4fdd\u6301\u6570\u7ec4\u6709\u5e8f\u6027\uff0c\u9700\u8981\u5c06\u5143\u7d20\u63d2\u5165\u5230\u7279\u5b9a\u4f4d\u7f6e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u4e5f\u662f\u975e\u5e38\u6602\u8d35\u7684\u3002
        • \u4e8c\u5206\u67e5\u627e\u4ec5\u9002\u7528\u4e8e\u6570\u7ec4\u3002\u4e8c\u5206\u67e5\u627e\u9700\u8981\u8df3\u8dc3\u5f0f\uff08\u975e\u8fde\u7eed\u5730\uff09\u8bbf\u95ee\u5143\u7d20\uff0c\u800c\u5728\u94fe\u8868\u4e2d\u6267\u884c\u8df3\u8dc3\u5f0f\u8bbf\u95ee\u7684\u6548\u7387\u8f83\u4f4e\uff0c\u56e0\u6b64\u4e0d\u9002\u5408\u5e94\u7528\u5728\u94fe\u8868\u6216\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u3002
        • \u5c0f\u6570\u636e\u91cf\u4e0b\uff0c\u7ebf\u6027\u67e5\u627e\u6027\u80fd\u66f4\u4f73\u3002\u5728\u7ebf\u6027\u67e5\u627e\u4e2d\uff0c\u6bcf\u8f6e\u53ea\u9700 1 \u6b21\u5224\u65ad\u64cd\u4f5c\uff1b\u800c\u5728\u4e8c\u5206\u67e5\u627e\u4e2d\uff0c\u9700\u8981 1 \u6b21\u52a0\u6cd5\u30011 \u6b21\u9664\u6cd5\u30011 ~ 3 \u6b21\u5224\u65ad\u64cd\u4f5c\u30011 \u6b21\u52a0\u6cd5\uff08\u51cf\u6cd5\uff09\uff0c\u5171 4 ~ 6 \u4e2a\u5355\u5143\u64cd\u4f5c\uff1b\u56e0\u6b64\uff0c\u5f53\u6570\u636e\u91cf \\(n\\) \u8f83\u5c0f\u65f6\uff0c\u7ebf\u6027\u67e5\u627e\u53cd\u800c\u6bd4\u4e8c\u5206\u67e5\u627e\u66f4\u5feb\u3002
        "},{"location":"chapter_searching/binary_search_edge/","title":"10.3 \u00a0 \u4e8c\u5206\u67e5\u627e\u8fb9\u754c","text":""},{"location":"chapter_searching/binary_search_edge/#1031","title":"10.3.1 \u00a0 \u67e5\u627e\u5de6\u8fb9\u754c","text":"

        Question

        \u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6709\u5e8f\u6570\u7ec4 nums \uff0c\u5176\u4e2d\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\u3002\u8bf7\u8fd4\u56de\u6570\u7ec4\u4e2d\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 target \u7684\u7d22\u5f15\u3002\u82e5\u6570\u7ec4\u4e2d\u4e0d\u5305\u542b\u8be5\u5143\u7d20\uff0c\u5219\u8fd4\u56de \\(-1\\) \u3002

        \u56de\u5fc6\u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\u7684\u65b9\u6cd5\uff0c\u641c\u7d22\u5b8c\u6210\u540e \\(i\\) \u6307\u5411\u6700\u5de6\u4e00\u4e2a target \uff0c\u56e0\u6b64\u67e5\u627e\u63d2\u5165\u70b9\u672c\u8d28\u4e0a\u662f\u5728\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target \u7684\u7d22\u5f15\u3002

        \u8003\u8651\u901a\u8fc7\u67e5\u627e\u63d2\u5165\u70b9\u7684\u51fd\u6570\u5b9e\u73b0\u67e5\u627e\u5de6\u8fb9\u754c\u3002\u8bf7\u6ce8\u610f\uff0c\u6570\u7ec4\u4e2d\u53ef\u80fd\u4e0d\u5305\u542b target \uff0c\u8fd9\u79cd\u60c5\u51b5\u53ef\u80fd\u5bfc\u81f4\u4ee5\u4e0b\u4e24\u79cd\u7ed3\u679c\u3002

        • \u63d2\u5165\u70b9\u7684\u7d22\u5f15 \\(i\\) \u8d8a\u754c\u3002
        • \u5143\u7d20 nums[i] \u4e0e target \u4e0d\u76f8\u7b49\u3002

        \u5f53\u9047\u5230\u4ee5\u4e0a\u4e24\u79cd\u60c5\u51b5\u65f6\uff0c\u76f4\u63a5\u8fd4\u56de \\(-1\\) \u5373\u53ef\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_edge.py
        def binary_search_left_edge(nums: list[int], target: int) -> int:\n    \"\"\"\u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target\"\"\"\n    # \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n    i = binary_search_insertion(nums, target)\n    # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if i == len(nums) or nums[i] != target:\n        return -1\n    # \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n    return i\n
        binary_search_edge.cpp
        /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(vector<int> &nums, int target) {\n    // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n    int i = binarySearchInsertion(nums, target);\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (i == nums.size() || nums[i] != target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n    return i;\n}\n
        binary_search_edge.java
        /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(int[] nums, int target) {\n    // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n    int i = binary_search_insertion.binarySearchInsertion(nums, target);\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (i == nums.length || nums[i] != target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n    return i;\n}\n
        binary_search_edge.cs
        /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint BinarySearchLeftEdge(int[] nums, int target) {\n    // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n    int i = binary_search_insertion.BinarySearchInsertion(nums, target);\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (i == nums.Length || nums[i] != target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n    return i;\n}\n
        binary_search_edge.go
        /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunc binarySearchLeftEdge(nums []int, target int) int {\n    // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n    i := binarySearchInsertion(nums, target)\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if i == len(nums) || nums[i] != target {\n        return -1\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n    return i\n}\n
        binary_search_edge.swift
        /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunc binarySearchLeftEdge(nums: [Int], target: Int) -> Int {\n    // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n    let i = binarySearchInsertion(nums: nums, target: target)\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if i == nums.endIndex || nums[i] != target {\n        return -1\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n    return i\n}\n
        binary_search_edge.js
        /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunction binarySearchLeftEdge(nums, target) {\n    // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n    const i = binarySearchInsertion(nums, target);\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (i === nums.length || nums[i] !== target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n    return i;\n}\n
        binary_search_edge.ts
        /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunction binarySearchLeftEdge(nums: Array<number>, target: number): number {\n    // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n    const i = binarySearchInsertion(nums, target);\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (i === nums.length || nums[i] !== target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n    return i;\n}\n
        binary_search_edge.dart
        /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(List<int> nums, int target) {\n  // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n  int i = binarySearchInsertion(nums, target);\n  // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n  if (i == nums.length || nums[i] != target) {\n    return -1;\n  }\n  // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n  return i;\n}\n
        binary_search_edge.rs
        /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfn binary_search_left_edge(nums: &[i32], target: i32) -> i32 {\n    // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n    let i = binary_search_insertion(nums, target);\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if i == nums.len() as i32 || nums[i as usize] != target {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n    i\n}\n
        binary_search_edge.c
        /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(int *nums, int numSize, int target) {\n    // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n    int i = binarySearchInsertion(nums, numSize, target);\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (i == numSize || nums[i] != target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n    return i;\n}\n
        binary_search_edge.kt
        /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfun binarySearchLeftEdge(nums: IntArray, target: Int): Int {\n    // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n    val i = binarySearchInsertion(nums, target)\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (i == nums.size || nums[i] != target) {\n        return -1\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n    return i\n}\n
        binary_search_edge.rb
        ### \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target ###\ndef binary_search_left_edge(nums, target)\n  # \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n  i = binary_search_insertion(nums, target)\n\n  # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n  return -1 if i == nums.length || nums[i] != target\n\n  i # \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_searching/binary_search_edge/#1032","title":"10.3.2 \u00a0 \u67e5\u627e\u53f3\u8fb9\u754c","text":"

        \u90a3\u4e48\u5982\u4f55\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target \u5462\uff1f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u662f\u4fee\u6539\u4ee3\u7801\uff0c\u66ff\u6362\u5728 nums[m] == target \u60c5\u51b5\u4e0b\u7684\u6307\u9488\u6536\u7f29\u64cd\u4f5c\u3002\u4ee3\u7801\u5728\u6b64\u7701\u7565\uff0c\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u3002

        \u4e0b\u9762\u6211\u4eec\u4ecb\u7ecd\u4e24\u79cd\u66f4\u52a0\u53d6\u5de7\u7684\u65b9\u6cd5\u3002

        "},{"location":"chapter_searching/binary_search_edge/#1","title":"1. \u00a0 \u590d\u7528\u67e5\u627e\u5de6\u8fb9\u754c","text":"

        \u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u67e5\u627e\u6700\u5de6\u5143\u7d20\u7684\u51fd\u6570\u6765\u67e5\u627e\u6700\u53f3\u5143\u7d20\uff0c\u5177\u4f53\u65b9\u6cd5\u4e3a\uff1a\u5c06\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\u3002

        \u5982\u56fe 10-7 \u6240\u793a\uff0c\u67e5\u627e\u5b8c\u6210\u540e\uff0c\u6307\u9488 \\(i\\) \u6307\u5411\u6700\u5de6\u4e00\u4e2a target + 1\uff08\u5982\u679c\u5b58\u5728\uff09\uff0c\u800c \\(j\\) \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0c\u56e0\u6b64\u8fd4\u56de \\(j\\) \u5373\u53ef\u3002

        \u56fe 10-7 \u00a0 \u5c06\u67e5\u627e\u53f3\u8fb9\u754c\u8f6c\u5316\u4e3a\u67e5\u627e\u5de6\u8fb9\u754c

        \u8bf7\u6ce8\u610f\uff0c\u8fd4\u56de\u7684\u63d2\u5165\u70b9\u662f \\(i\\) \uff0c\u56e0\u6b64\u9700\u8981\u5c06\u5176\u51cf \\(1\\) \uff0c\u4ece\u800c\u83b7\u5f97 \\(j\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_edge.py
        def binary_search_right_edge(nums: list[int], target: int) -> int:\n    \"\"\"\u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target\"\"\"\n    # \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n    i = binary_search_insertion(nums, target + 1)\n    # j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n    j = i - 1\n    # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if j == -1 or nums[j] != target:\n        return -1\n    # \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n    return j\n
        binary_search_edge.cpp
        /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(vector<int> &nums, int target) {\n    // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n    int i = binarySearchInsertion(nums, target + 1);\n    // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n    int j = i - 1;\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n    return j;\n}\n
        binary_search_edge.java
        /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(int[] nums, int target) {\n    // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n    int i = binary_search_insertion.binarySearchInsertion(nums, target + 1);\n    // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n    int j = i - 1;\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n    return j;\n}\n
        binary_search_edge.cs
        /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint BinarySearchRightEdge(int[] nums, int target) {\n    // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n    int i = binary_search_insertion.BinarySearchInsertion(nums, target + 1);\n    // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n    int j = i - 1;\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n    return j;\n}\n
        binary_search_edge.go
        /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunc binarySearchRightEdge(nums []int, target int) int {\n    // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n    i := binarySearchInsertion(nums, target+1)\n    // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n    j := i - 1\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if j == -1 || nums[j] != target {\n        return -1\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n    return j\n}\n
        binary_search_edge.swift
        /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunc binarySearchRightEdge(nums: [Int], target: Int) -> Int {\n    // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n    let i = binarySearchInsertion(nums: nums, target: target + 1)\n    // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n    let j = i - 1\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if j == -1 || nums[j] != target {\n        return -1\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n    return j\n}\n
        binary_search_edge.js
        /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunction binarySearchRightEdge(nums, target) {\n    // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n    const i = binarySearchInsertion(nums, target + 1);\n    // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n    const j = i - 1;\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (j === -1 || nums[j] !== target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n    return j;\n}\n
        binary_search_edge.ts
        /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunction binarySearchRightEdge(nums: Array<number>, target: number): number {\n    // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n    const i = binarySearchInsertion(nums, target + 1);\n    // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n    const j = i - 1;\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (j === -1 || nums[j] !== target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n    return j;\n}\n
        binary_search_edge.dart
        /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(List<int> nums, int target) {\n  // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n  int i = binarySearchInsertion(nums, target + 1);\n  // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n  int j = i - 1;\n  // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n  if (j == -1 || nums[j] != target) {\n    return -1;\n  }\n  // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n  return j;\n}\n
        binary_search_edge.rs
        /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfn binary_search_right_edge(nums: &[i32], target: i32) -> i32 {\n    // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n    let i = binary_search_insertion(nums, target + 1);\n    // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n    let j = i - 1;\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if j == -1 || nums[j as usize] != target {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n    j\n}\n
        binary_search_edge.c
        /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(int *nums, int numSize, int target) {\n    // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n    int i = binarySearchInsertion(nums, numSize, target + 1);\n    // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n    int j = i - 1;\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n    return j;\n}\n
        binary_search_edge.kt
        /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfun binarySearchRightEdge(nums: IntArray, target: Int): Int {\n    // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n    val i = binarySearchInsertion(nums, target + 1)\n    // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n    val j = i - 1\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (j == -1 || nums[j] != target) {\n        return -1\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n    return j\n}\n
        binary_search_edge.rb
        ### \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target ###\ndef binary_search_right_edge(nums, target)\n  # \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n  i = binary_search_insertion(nums, target + 1)\n\n  # j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n  j = i - 1\n\n  # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n  return -1 if j == -1 || nums[j] != target\n\n  j # \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_searching/binary_search_edge/#2","title":"2. \u00a0 \u8f6c\u5316\u4e3a\u67e5\u627e\u5143\u7d20","text":"

        \u6211\u4eec\u77e5\u9053\uff0c\u5f53\u6570\u7ec4\u4e0d\u5305\u542b target \u65f6\uff0c\u6700\u7ec8 \\(i\\) \u548c \\(j\\) \u4f1a\u5206\u522b\u6307\u5411\u9996\u4e2a\u5927\u4e8e\u3001\u5c0f\u4e8e target \u7684\u5143\u7d20\u3002

        \u56e0\u6b64\uff0c\u5982\u56fe 10-8 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u6784\u9020\u4e00\u4e2a\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728\u7684\u5143\u7d20\uff0c\u7528\u4e8e\u67e5\u627e\u5de6\u53f3\u8fb9\u754c\u3002

        • \u67e5\u627e\u6700\u5de6\u4e00\u4e2a target \uff1a\u53ef\u4ee5\u8f6c\u5316\u4e3a\u67e5\u627e target - 0.5 \uff0c\u5e76\u8fd4\u56de\u6307\u9488 \\(i\\) \u3002
        • \u67e5\u627e\u6700\u53f3\u4e00\u4e2a target \uff1a\u53ef\u4ee5\u8f6c\u5316\u4e3a\u67e5\u627e target + 0.5 \uff0c\u5e76\u8fd4\u56de\u6307\u9488 \\(j\\) \u3002

        \u56fe 10-8 \u00a0 \u5c06\u67e5\u627e\u8fb9\u754c\u8f6c\u5316\u4e3a\u67e5\u627e\u5143\u7d20

        \u4ee3\u7801\u5728\u6b64\u7701\u7565\uff0c\u4ee5\u4e0b\u4e24\u70b9\u503c\u5f97\u6ce8\u610f\u3002

        • \u7ed9\u5b9a\u6570\u7ec4\u4e0d\u5305\u542b\u5c0f\u6570\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u65e0\u987b\u5173\u5fc3\u5982\u4f55\u5904\u7406\u76f8\u7b49\u7684\u60c5\u51b5\u3002
        • \u56e0\u4e3a\u8be5\u65b9\u6cd5\u5f15\u5165\u4e86\u5c0f\u6570\uff0c\u6240\u4ee5\u9700\u8981\u5c06\u51fd\u6570\u4e2d\u7684\u53d8\u91cf target \u6539\u4e3a\u6d6e\u70b9\u6570\u7c7b\u578b\uff08Python \u65e0\u987b\u6539\u52a8\uff09\u3002
        "},{"location":"chapter_searching/binary_search_insertion/","title":"10.2 \u00a0 \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9","text":"

        \u4e8c\u5206\u67e5\u627e\u4e0d\u4ec5\u53ef\u7528\u4e8e\u641c\u7d22\u76ee\u6807\u5143\u7d20\uff0c\u8fd8\u53ef\u7528\u4e8e\u89e3\u51b3\u8bb8\u591a\u53d8\u79cd\u95ee\u9898\uff0c\u6bd4\u5982\u641c\u7d22\u76ee\u6807\u5143\u7d20\u7684\u63d2\u5165\u4f4d\u7f6e\u3002

        "},{"location":"chapter_searching/binary_search_insertion/#1021","title":"10.2.1 \u00a0 \u65e0\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"

        Question

        \u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6709\u5e8f\u6570\u7ec4 nums \u548c\u4e00\u4e2a\u5143\u7d20 target \uff0c\u6570\u7ec4\u4e0d\u5b58\u5728\u91cd\u590d\u5143\u7d20\u3002\u73b0\u5c06 target \u63d2\u5165\u6570\u7ec4 nums \u4e2d\uff0c\u5e76\u4fdd\u6301\u5176\u6709\u5e8f\u6027\u3002\u82e5\u6570\u7ec4\u4e2d\u5df2\u5b58\u5728\u5143\u7d20 target \uff0c\u5219\u63d2\u5165\u5230\u5176\u5de6\u65b9\u3002\u8bf7\u8fd4\u56de\u63d2\u5165\u540e target \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\u3002\u793a\u4f8b\u5982\u56fe 10-4 \u6240\u793a\u3002

        \u56fe 10-4 \u00a0 \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\u793a\u4f8b\u6570\u636e

        \u5982\u679c\u60f3\u590d\u7528\u4e0a\u4e00\u8282\u7684\u4e8c\u5206\u67e5\u627e\u4ee3\u7801\uff0c\u5219\u9700\u8981\u56de\u7b54\u4ee5\u4e0b\u4e24\u4e2a\u95ee\u9898\u3002

        \u95ee\u9898\u4e00\uff1a\u5f53\u6570\u7ec4\u4e2d\u5305\u542b target \u65f6\uff0c\u63d2\u5165\u70b9\u7684\u7d22\u5f15\u662f\u5426\u662f\u8be5\u5143\u7d20\u7684\u7d22\u5f15\uff1f

        \u9898\u76ee\u8981\u6c42\u5c06 target \u63d2\u5165\u5230\u76f8\u7b49\u5143\u7d20\u7684\u5de6\u8fb9\uff0c\u8fd9\u610f\u5473\u7740\u65b0\u63d2\u5165\u7684 target \u66ff\u6362\u4e86\u539f\u6765 target \u7684\u4f4d\u7f6e\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u6570\u7ec4\u5305\u542b target \u65f6\uff0c\u63d2\u5165\u70b9\u7684\u7d22\u5f15\u5c31\u662f\u8be5 target \u7684\u7d22\u5f15\u3002

        \u95ee\u9898\u4e8c\uff1a\u5f53\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728 target \u65f6\uff0c\u63d2\u5165\u70b9\u662f\u54ea\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff1f

        \u8fdb\u4e00\u6b65\u601d\u8003\u4e8c\u5206\u67e5\u627e\u8fc7\u7a0b\uff1a\u5f53 nums[m] < target \u65f6 \\(i\\) \u79fb\u52a8\uff0c\u8fd9\u610f\u5473\u7740\u6307\u9488 \\(i\\) \u5728\u5411\u5927\u4e8e\u7b49\u4e8e target \u7684\u5143\u7d20\u9760\u8fd1\u3002\u540c\u7406\uff0c\u6307\u9488 \\(j\\) \u59cb\u7ec8\u5728\u5411\u5c0f\u4e8e\u7b49\u4e8e target \u7684\u5143\u7d20\u9760\u8fd1\u3002

        \u56e0\u6b64\u4e8c\u5206\u7ed3\u675f\u65f6\u4e00\u5b9a\u6709\uff1a\\(i\\) \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\uff0c\\(j\\) \u6307\u5411\u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u3002\u6613\u5f97\u5f53\u6570\u7ec4\u4e0d\u5305\u542b target \u65f6\uff0c\u63d2\u5165\u7d22\u5f15\u4e3a \\(i\\) \u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_insertion.py
        def binary_search_insertion_simple(nums: list[int], target: int) -> int:\n    \"\"\"\u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09\"\"\"\n    i, j = 0, len(nums) - 1  # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while i <= j:\n        m = (i + j) // 2  # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m] < target:\n            i = m + 1  # target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        elif nums[m] > target:\n            j = m - 1  # target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        else:\n            return m  # \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n    # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i\n
        binary_search_insertion.cpp
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(vector<int> &nums, int target) {\n    int i = 0, j = nums.size() - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n        }\n    }\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.java
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(int[] nums, int target) {\n    int i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n        }\n    }\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.cs
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint BinarySearchInsertionSimple(int[] nums, int target) {\n    int i = 0, j = nums.Length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n        }\n    }\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.go
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertionSimple(nums []int, target int) int {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    i, j := 0, len(nums)-1\n    for i <= j {\n        // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        m := i + (j-i)/2\n        if nums[m] < target {\n            // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1\n        } else if nums[m] > target {\n            // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1\n        } else {\n            // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n            return m\n        }\n    }\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i\n}\n
        binary_search_insertion.swift
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertionSimple(nums: [Int], target: Int) -> Int {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    var i = nums.startIndex\n    var j = nums.endIndex - 1\n    while i <= j {\n        let m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m] < target {\n            i = m + 1 // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if nums[m] > target {\n            j = m - 1 // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            return m // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n        }\n    }\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i\n}\n
        binary_search_insertion.js
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertionSimple(nums, target) {\n    let i = 0,\n        j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        const m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n        }\n    }\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.ts
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertionSimple(\n    nums: Array<number>,\n    target: number\n): number {\n    let i = 0,\n        j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        const m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n        }\n    }\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.dart
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(List<int> nums, int target) {\n  int i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n  while (i <= j) {\n    int m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    if (nums[m] < target) {\n      i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n    } else if (nums[m] > target) {\n      j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n    } else {\n      return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n    }\n  }\n  // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n  return i;\n}\n
        binary_search_insertion.rs
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfn binary_search_insertion_simple(nums: &[i32], target: i32) -> i32 {\n    let (mut i, mut j) = (0, nums.len() as i32 - 1); // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while i <= j {\n        let m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m as usize] < target {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if nums[m as usize] > target {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            return m;\n        }\n    }\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n    i\n}\n
        binary_search_insertion.c
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(int *nums, int numSize, int target) {\n    int i = 0, j = numSize - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n        }\n    }\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.kt
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfun binarySearchInsertionSimple(nums: IntArray, target: Int): Int {\n    var i = 0\n    var j = nums.size - 1 // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        val m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) {\n            i = m + 1 // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1 // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            return m // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n        }\n    }\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i\n}\n
        binary_search_insertion.rb
        ### \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 ###\ndef binary_search_insertion_simple(nums, target)\n  # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n  i, j = 0, nums.length - 1\n\n  while i <= j\n    # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    m = (i + j) / 2\n\n    if nums[m] < target\n      i = m + 1 # target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n    elsif nums[m] > target\n      j = m - 1 # target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n    else\n      return m  # \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n    end\n  end\n\n  i # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_searching/binary_search_insertion/#1022","title":"10.2.2 \u00a0 \u5b58\u5728\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"

        Question

        \u5728\u4e0a\u4e00\u9898\u7684\u57fa\u7840\u4e0a\uff0c\u89c4\u5b9a\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u5176\u4f59\u4e0d\u53d8\u3002

        \u5047\u8bbe\u6570\u7ec4\u4e2d\u5b58\u5728\u591a\u4e2a target \uff0c\u5219\u666e\u901a\u4e8c\u5206\u67e5\u627e\u53ea\u80fd\u8fd4\u56de\u5176\u4e2d\u4e00\u4e2a target \u7684\u7d22\u5f15\uff0c\u800c\u65e0\u6cd5\u786e\u5b9a\u8be5\u5143\u7d20\u7684\u5de6\u8fb9\u548c\u53f3\u8fb9\u8fd8\u6709\u591a\u5c11 target\u3002

        \u9898\u76ee\u8981\u6c42\u5c06\u76ee\u6807\u5143\u7d20\u63d2\u5165\u5230\u6700\u5de6\u8fb9\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5de6\u4e00\u4e2a target \u7684\u7d22\u5f15\u3002\u521d\u6b65\u8003\u8651\u901a\u8fc7\u56fe 10-5 \u6240\u793a\u7684\u6b65\u9aa4\u5b9e\u73b0\u3002

        1. \u6267\u884c\u4e8c\u5206\u67e5\u627e\uff0c\u5f97\u5230\u4efb\u610f\u4e00\u4e2a target \u7684\u7d22\u5f15\uff0c\u8bb0\u4e3a \\(k\\) \u3002
        2. \u4ece\u7d22\u5f15 \\(k\\) \u5f00\u59cb\uff0c\u5411\u5de6\u8fdb\u884c\u7ebf\u6027\u904d\u5386\uff0c\u5f53\u627e\u5230\u6700\u5de6\u8fb9\u7684 target \u65f6\u8fd4\u56de\u3002

        \u56fe 10-5 \u00a0 \u7ebf\u6027\u67e5\u627e\u91cd\u590d\u5143\u7d20\u7684\u63d2\u5165\u70b9

        \u6b64\u65b9\u6cd5\u867d\u7136\u53ef\u7528\uff0c\u4f46\u5176\u5305\u542b\u7ebf\u6027\u67e5\u627e\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002\u5f53\u6570\u7ec4\u4e2d\u5b58\u5728\u5f88\u591a\u91cd\u590d\u7684 target \u65f6\uff0c\u8be5\u65b9\u6cd5\u6548\u7387\u5f88\u4f4e\u3002

        \u73b0\u8003\u8651\u62d3\u5c55\u4e8c\u5206\u67e5\u627e\u4ee3\u7801\u3002\u5982\u56fe 10-6 \u6240\u793a\uff0c\u6574\u4f53\u6d41\u7a0b\u4fdd\u6301\u4e0d\u53d8\uff0c\u6bcf\u8f6e\u5148\u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 \\(m\\) \uff0c\u518d\u5224\u65ad target \u548c nums[m] \u7684\u5927\u5c0f\u5173\u7cfb\uff0c\u5206\u4e3a\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5\u3002

        • \u5f53 nums[m] < target \u6216 nums[m] > target \u65f6\uff0c\u8bf4\u660e\u8fd8\u6ca1\u6709\u627e\u5230 target \uff0c\u56e0\u6b64\u91c7\u7528\u666e\u901a\u4e8c\u5206\u67e5\u627e\u7684\u7f29\u5c0f\u533a\u95f4\u64cd\u4f5c\uff0c\u4ece\u800c\u4f7f\u6307\u9488 \\(i\\) \u548c \\(j\\) \u5411 target \u9760\u8fd1\u3002
        • \u5f53 nums[m] == target \u65f6\uff0c\u8bf4\u660e\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 \\([i, m - 1]\\) \u4e2d\uff0c\u56e0\u6b64\u91c7\u7528 \\(j = m - 1\\) \u6765\u7f29\u5c0f\u533a\u95f4\uff0c\u4ece\u800c\u4f7f\u6307\u9488 \\(j\\) \u5411\u5c0f\u4e8e target \u7684\u5143\u7d20\u9760\u8fd1\u3002

        \u5faa\u73af\u5b8c\u6210\u540e\uff0c\\(i\\) \u6307\u5411\u6700\u5de6\u8fb9\u7684 target \uff0c\\(j\\) \u6307\u5411\u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\uff0c\u56e0\u6b64\u7d22\u5f15 \\(i\\) \u5c31\u662f\u63d2\u5165\u70b9\u3002

        <1><2><3><4><5><6><7><8>

        \u56fe 10-6 \u00a0 \u4e8c\u5206\u67e5\u627e\u91cd\u590d\u5143\u7d20\u7684\u63d2\u5165\u70b9\u7684\u6b65\u9aa4

        \u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5224\u65ad\u5206\u652f nums[m] > target \u548c nums[m] == target \u7684\u64cd\u4f5c\u76f8\u540c\uff0c\u56e0\u6b64\u4e24\u8005\u53ef\u4ee5\u5408\u5e76\u3002

        \u5373\u4fbf\u5982\u6b64\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u5c06\u5224\u65ad\u6761\u4ef6\u4fdd\u6301\u5c55\u5f00\uff0c\u56e0\u4e3a\u5176\u903b\u8f91\u66f4\u52a0\u6e05\u6670\u3001\u53ef\u8bfb\u6027\u66f4\u597d\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_insertion.py
        def binary_search_insertion(nums: list[int], target: int) -> int:\n    \"\"\"\u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09\"\"\"\n    i, j = 0, len(nums) - 1  # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while i <= j:\n        m = (i + j) // 2  # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m] < target:\n            i = m + 1  # target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        elif nums[m] > target:\n            j = m - 1  # target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        else:\n            j = m - 1  # \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n    # \u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i\n
        binary_search_insertion.cpp
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(vector<int> &nums, int target) {\n    int i = 0, j = nums.size() - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n        }\n    }\n    // \u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.java
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(int[] nums, int target) {\n    int i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n        }\n    }\n    // \u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.cs
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint BinarySearchInsertion(int[] nums, int target) {\n    int i = 0, j = nums.Length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n        }\n    }\n    // \u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.go
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertion(nums []int, target int) int {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    i, j := 0, len(nums)-1\n    for i <= j {\n        // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        m := i + (j-i)/2\n        if nums[m] < target {\n            // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1\n        } else if nums[m] > target {\n            // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1\n        } else {\n            // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1\n        }\n    }\n    // \u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i\n}\n
        binary_search_insertion.swift
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertion(nums: [Int], target: Int) -> Int {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    var i = nums.startIndex\n    var j = nums.endIndex - 1\n    while i <= j {\n        let m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m] < target {\n            i = m + 1 // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if nums[m] > target {\n            j = m - 1 // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            j = m - 1 // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n        }\n    }\n    // \u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i\n}\n
        binary_search_insertion.js
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertion(nums, target) {\n    let i = 0,\n        j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        const m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n        }\n    }\n    // \u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.ts
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertion(nums: Array<number>, target: number): number {\n    let i = 0,\n        j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        const m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n        }\n    }\n    // \u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.dart
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(List<int> nums, int target) {\n  int i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n  while (i <= j) {\n    int m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    if (nums[m] < target) {\n      i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n    } else if (nums[m] > target) {\n      j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n    } else {\n      j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n    }\n  }\n  // \u8fd4\u56de\u63d2\u5165\u70b9 i\n  return i;\n}\n
        binary_search_insertion.rs
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\npub fn binary_search_insertion(nums: &[i32], target: i32) -> i32 {\n    let (mut i, mut j) = (0, nums.len() as i32 - 1); // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while i <= j {\n        let m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m as usize] < target {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if nums[m as usize] > target {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n        }\n    }\n    // \u8fd4\u56de\u63d2\u5165\u70b9 i\n    i\n}\n
        binary_search_insertion.c
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(int *nums, int numSize, int target) {\n    int i = 0, j = numSize - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n        }\n    }\n    // \u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.kt
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfun binarySearchInsertion(nums: IntArray, target: Int): Int {\n    var i = 0\n    var j = nums.size - 1 // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        val m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) {\n            i = m + 1 // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1 // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            j = m - 1 // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n        }\n    }\n    // \u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i\n}\n
        binary_search_insertion.rb
        ### \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 ###\ndef binary_search_insertion(nums, target)\n  # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n  i, j = 0, nums.length - 1\n\n  while i <= j\n    # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    m = (i + j) / 2\n\n    if nums[m] < target\n      i = m + 1 # target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n    elsif nums[m] > target\n      j = m - 1 # target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n    else\n      j = m - 1 # \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n    end\n  end\n\n  i # \u8fd4\u56de\u63d2\u5165\u70b9 i\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        Tip

        \u672c\u8282\u7684\u4ee3\u7801\u90fd\u662f\u201c\u53cc\u95ed\u533a\u95f4\u201d\u5199\u6cd5\u3002\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u201c\u5de6\u95ed\u53f3\u5f00\u201d\u5199\u6cd5\u3002

        \u603b\u7684\u6765\u770b\uff0c\u4e8c\u5206\u67e5\u627e\u65e0\u975e\u5c31\u662f\u7ed9\u6307\u9488 \\(i\\) \u548c \\(j\\) \u5206\u522b\u8bbe\u5b9a\u641c\u7d22\u76ee\u6807\uff0c\u76ee\u6807\u53ef\u80fd\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u5143\u7d20\uff08\u4f8b\u5982 target \uff09\uff0c\u4e5f\u53ef\u80fd\u662f\u4e00\u4e2a\u5143\u7d20\u8303\u56f4\uff08\u4f8b\u5982\u5c0f\u4e8e target \u7684\u5143\u7d20\uff09\u3002

        \u5728\u4e0d\u65ad\u7684\u5faa\u73af\u4e8c\u5206\u4e2d\uff0c\u6307\u9488 \\(i\\) \u548c \\(j\\) \u90fd\u9010\u6e10\u903c\u8fd1\u9884\u5148\u8bbe\u5b9a\u7684\u76ee\u6807\u3002\u6700\u7ec8\uff0c\u5b83\u4eec\u6216\u662f\u6210\u529f\u627e\u5230\u7b54\u6848\uff0c\u6216\u662f\u8d8a\u8fc7\u8fb9\u754c\u540e\u505c\u6b62\u3002

        "},{"location":"chapter_searching/replace_linear_by_hashing/","title":"10.4 \u00a0 \u54c8\u5e0c\u4f18\u5316\u7b56\u7565","text":"

        \u5728\u7b97\u6cd5\u9898\u4e2d\uff0c\u6211\u4eec\u5e38\u901a\u8fc7\u5c06\u7ebf\u6027\u67e5\u627e\u66ff\u6362\u4e3a\u54c8\u5e0c\u67e5\u627e\u6765\u964d\u4f4e\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u6211\u4eec\u501f\u52a9\u4e00\u4e2a\u7b97\u6cd5\u9898\u6765\u52a0\u6df1\u7406\u89e3\u3002

        Question

        \u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums \u548c\u4e00\u4e2a\u76ee\u6807\u5143\u7d20 target \uff0c\u8bf7\u5728\u6570\u7ec4\u4e2d\u641c\u7d22\u201c\u548c\u201d\u4e3a target \u7684\u4e24\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u6570\u7ec4\u7d22\u5f15\u3002\u8fd4\u56de\u4efb\u610f\u4e00\u4e2a\u89e3\u5373\u53ef\u3002

        "},{"location":"chapter_searching/replace_linear_by_hashing/#1041","title":"10.4.1 \u00a0 \u7ebf\u6027\u67e5\u627e\uff1a\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4","text":"

        \u8003\u8651\u76f4\u63a5\u904d\u5386\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\u3002\u5982\u56fe 10-9 \u6240\u793a\uff0c\u6211\u4eec\u5f00\u542f\u4e00\u4e2a\u4e24\u5c42\u5faa\u73af\uff0c\u5728\u6bcf\u8f6e\u4e2d\u5224\u65ad\u4e24\u4e2a\u6574\u6570\u7684\u548c\u662f\u5426\u4e3a target \uff0c\u82e5\u662f\uff0c\u5219\u8fd4\u56de\u5b83\u4eec\u7684\u7d22\u5f15\u3002

        \u56fe 10-9 \u00a0 \u7ebf\u6027\u67e5\u627e\u6c42\u89e3\u4e24\u6570\u4e4b\u548c

        \u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby two_sum.py
        def two_sum_brute_force(nums: list[int], target: int) -> list[int]:\n    \"\"\"\u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e\"\"\"\n    # \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n    for i in range(len(nums) - 1):\n        for j in range(i + 1, len(nums)):\n            if nums[i] + nums[j] == target:\n                return [i, j]\n    return []\n
        two_sum.cpp
        /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nvector<int> twoSumBruteForce(vector<int> &nums, int target) {\n    int size = nums.size();\n    // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n    for (int i = 0; i < size - 1; i++) {\n        for (int j = i + 1; j < size; j++) {\n            if (nums[i] + nums[j] == target)\n                return {i, j};\n        }\n    }\n    return {};\n}\n
        two_sum.java
        /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint[] twoSumBruteForce(int[] nums, int target) {\n    int size = nums.length;\n    // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n    for (int i = 0; i < size - 1; i++) {\n        for (int j = i + 1; j < size; j++) {\n            if (nums[i] + nums[j] == target)\n                return new int[] { i, j };\n        }\n    }\n    return new int[0];\n}\n
        two_sum.cs
        /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint[] TwoSumBruteForce(int[] nums, int target) {\n    int size = nums.Length;\n    // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n    for (int i = 0; i < size - 1; i++) {\n        for (int j = i + 1; j < size; j++) {\n            if (nums[i] + nums[j] == target)\n                return [i, j];\n        }\n    }\n    return [];\n}\n
        two_sum.go
        /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunc twoSumBruteForce(nums []int, target int) []int {\n    size := len(nums)\n    // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n    for i := 0; i < size-1; i++ {\n        for j := i + 1; j < size; j++ {\n            if nums[i]+nums[j] == target {\n                return []int{i, j}\n            }\n        }\n    }\n    return nil\n}\n
        two_sum.swift
        /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunc twoSumBruteForce(nums: [Int], target: Int) -> [Int] {\n    // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n    for i in nums.indices.dropLast() {\n        for j in nums.indices.dropFirst(i + 1) {\n            if nums[i] + nums[j] == target {\n                return [i, j]\n            }\n        }\n    }\n    return [0]\n}\n
        two_sum.js
        /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunction twoSumBruteForce(nums, target) {\n    const n = nums.length;\n    // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n    for (let i = 0; i < n; i++) {\n        for (let j = i + 1; j < n; j++) {\n            if (nums[i] + nums[j] === target) {\n                return [i, j];\n            }\n        }\n    }\n    return [];\n}\n
        two_sum.ts
        /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunction twoSumBruteForce(nums: number[], target: number): number[] {\n    const n = nums.length;\n    // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n    for (let i = 0; i < n; i++) {\n        for (let j = i + 1; j < n; j++) {\n            if (nums[i] + nums[j] === target) {\n                return [i, j];\n            }\n        }\n    }\n    return [];\n}\n
        two_sum.dart
        /* \u65b9\u6cd5\u4e00\uff1a \u66b4\u529b\u679a\u4e3e */\nList<int> twoSumBruteForce(List<int> nums, int target) {\n  int size = nums.length;\n  // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n  for (var i = 0; i < size - 1; i++) {\n    for (var j = i + 1; j < size; j++) {\n      if (nums[i] + nums[j] == target) return [i, j];\n    }\n  }\n  return [0];\n}\n
        two_sum.rs
        /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\npub fn two_sum_brute_force(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {\n    let size = nums.len();\n    // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n    for i in 0..size - 1 {\n        for j in i + 1..size {\n            if nums[i] + nums[j] == target {\n                return Some(vec![i as i32, j as i32]);\n            }\n        }\n    }\n    None\n}\n
        two_sum.c
        /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint *twoSumBruteForce(int *nums, int numsSize, int target, int *returnSize) {\n    for (int i = 0; i < numsSize; ++i) {\n        for (int j = i + 1; j < numsSize; ++j) {\n            if (nums[i] + nums[j] == target) {\n                int *res = malloc(sizeof(int) * 2);\n                res[0] = i, res[1] = j;\n                *returnSize = 2;\n                return res;\n            }\n        }\n    }\n    *returnSize = 0;\n    return NULL;\n}\n
        two_sum.kt
        /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfun twoSumBruteForce(nums: IntArray, target: Int): IntArray {\n    val size = nums.size\n    // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n    for (i in 0..<size - 1) {\n        for (j in i + 1..<size) {\n            if (nums[i] + nums[j] == target) return intArrayOf(i, j)\n        }\n    }\n    return IntArray(0)\n}\n
        two_sum.rb
        ### \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e ###\ndef two_sum_brute_force(nums, target)\n  # \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n  for i in 0...(nums.length - 1)\n    for j in (i + 1)...nums.length\n      return [i, j] if nums[i] + nums[j] == target\n    end\n  end\n\n  []\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u6b64\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff0c\u5728\u5927\u6570\u636e\u91cf\u4e0b\u975e\u5e38\u8017\u65f6\u3002

        "},{"location":"chapter_searching/replace_linear_by_hashing/#1042","title":"10.4.2 \u00a0 \u54c8\u5e0c\u67e5\u627e\uff1a\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4","text":"

        \u8003\u8651\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868\uff0c\u952e\u503c\u5bf9\u5206\u522b\u4e3a\u6570\u7ec4\u5143\u7d20\u548c\u5143\u7d20\u7d22\u5f15\u3002\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u6bcf\u8f6e\u6267\u884c\u56fe 10-10 \u6240\u793a\u7684\u6b65\u9aa4\u3002

        1. \u5224\u65ad\u6570\u5b57 target - nums[i] \u662f\u5426\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u82e5\u662f\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u8fd9\u4e24\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u3002
        2. \u5c06\u952e\u503c\u5bf9 nums[i] \u548c\u7d22\u5f15 i \u6dfb\u52a0\u8fdb\u54c8\u5e0c\u8868\u3002
        <1><2><3>

        \u56fe 10-10 \u00a0 \u8f85\u52a9\u54c8\u5e0c\u8868\u6c42\u89e3\u4e24\u6570\u4e4b\u548c

        \u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff0c\u4ec5\u9700\u5355\u5c42\u5faa\u73af\u5373\u53ef\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby two_sum.py
        def two_sum_hash_table(nums: list[int], target: int) -> list[int]:\n    \"\"\"\u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868\"\"\"\n    # \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    dic = {}\n    # \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    for i in range(len(nums)):\n        if target - nums[i] in dic:\n            return [dic[target - nums[i]], i]\n        dic[nums[i]] = i\n    return []\n
        two_sum.cpp
        /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nvector<int> twoSumHashTable(vector<int> &nums, int target) {\n    int size = nums.size();\n    // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    unordered_map<int, int> dic;\n    // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    for (int i = 0; i < size; i++) {\n        if (dic.find(target - nums[i]) != dic.end()) {\n            return {dic[target - nums[i]], i};\n        }\n        dic.emplace(nums[i], i);\n    }\n    return {};\n}\n
        two_sum.java
        /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint[] twoSumHashTable(int[] nums, int target) {\n    int size = nums.length;\n    // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    Map<Integer, Integer> dic = new HashMap<>();\n    // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    for (int i = 0; i < size; i++) {\n        if (dic.containsKey(target - nums[i])) {\n            return new int[] { dic.get(target - nums[i]), i };\n        }\n        dic.put(nums[i], i);\n    }\n    return new int[0];\n}\n
        two_sum.cs
        /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint[] TwoSumHashTable(int[] nums, int target) {\n    int size = nums.Length;\n    // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    Dictionary<int, int> dic = [];\n    // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    for (int i = 0; i < size; i++) {\n        if (dic.ContainsKey(target - nums[i])) {\n            return [dic[target - nums[i]], i];\n        }\n        dic.Add(nums[i], i);\n    }\n    return [];\n}\n
        two_sum.go
        /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunc twoSumHashTable(nums []int, target int) []int {\n    // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    hashTable := map[int]int{}\n    // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    for idx, val := range nums {\n        if preIdx, ok := hashTable[target-val]; ok {\n            return []int{preIdx, idx}\n        }\n        hashTable[val] = idx\n    }\n    return nil\n}\n
        two_sum.swift
        /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunc twoSumHashTable(nums: [Int], target: Int) -> [Int] {\n    // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    var dic: [Int: Int] = [:]\n    // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    for i in nums.indices {\n        if let j = dic[target - nums[i]] {\n            return [j, i]\n        }\n        dic[nums[i]] = i\n    }\n    return [0]\n}\n
        two_sum.js
        /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunction twoSumHashTable(nums, target) {\n    // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    let m = {};\n    // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    for (let i = 0; i < nums.length; i++) {\n        if (m[target - nums[i]] !== undefined) {\n            return [m[target - nums[i]], i];\n        } else {\n            m[nums[i]] = i;\n        }\n    }\n    return [];\n}\n
        two_sum.ts
        /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunction twoSumHashTable(nums: number[], target: number): number[] {\n    // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    let m: Map<number, number> = new Map();\n    // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    for (let i = 0; i < nums.length; i++) {\n        let index = m.get(target - nums[i]);\n        if (index !== undefined) {\n            return [index, i];\n        } else {\n            m.set(nums[i], i);\n        }\n    }\n    return [];\n}\n
        two_sum.dart
        /* \u65b9\u6cd5\u4e8c\uff1a \u8f85\u52a9\u54c8\u5e0c\u8868 */\nList<int> twoSumHashTable(List<int> nums, int target) {\n  int size = nums.length;\n  // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n  Map<int, int> dic = HashMap();\n  // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n  for (var i = 0; i < size; i++) {\n    if (dic.containsKey(target - nums[i])) {\n      return [dic[target - nums[i]]!, i];\n    }\n    dic.putIfAbsent(nums[i], () => i);\n  }\n  return [0];\n}\n
        two_sum.rs
        /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\npub fn two_sum_hash_table(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {\n    // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    let mut dic = HashMap::new();\n    // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    for (i, num) in nums.iter().enumerate() {\n        match dic.get(&(target - num)) {\n            Some(v) => return Some(vec![*v as i32, i as i32]),\n            None => dic.insert(num, i as i32),\n        };\n    }\n    None\n}\n
        two_sum.c
        /* \u54c8\u5e0c\u8868 */\ntypedef struct {\n    int key;\n    int val;\n    UT_hash_handle hh; // \u57fa\u4e8e uthash.h \u5b9e\u73b0\n} HashTable;\n\n/* \u54c8\u5e0c\u8868\u67e5\u8be2 */\nHashTable *find(HashTable *h, int key) {\n    HashTable *tmp;\n    HASH_FIND_INT(h, &key, tmp);\n    return tmp;\n}\n\n/* \u54c8\u5e0c\u8868\u5143\u7d20\u63d2\u5165 */\nvoid insert(HashTable **h, int key, int val) {\n    HashTable *t = find(*h, key);\n    if (t == NULL) {\n        HashTable *tmp = malloc(sizeof(HashTable));\n        tmp->key = key, tmp->val = val;\n        HASH_ADD_INT(*h, key, tmp);\n    } else {\n        t->val = val;\n    }\n}\n\n/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {\n    HashTable *hashtable = NULL;\n    for (int i = 0; i < numsSize; i++) {\n        HashTable *t = find(hashtable, target - nums[i]);\n        if (t != NULL) {\n            int *res = malloc(sizeof(int) * 2);\n            res[0] = t->val, res[1] = i;\n            *returnSize = 2;\n            return res;\n        }\n        insert(&hashtable, nums[i], i);\n    }\n    *returnSize = 0;\n    return NULL;\n}\n
        two_sum.kt
        /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfun twoSumHashTable(nums: IntArray, target: Int): IntArray {\n    val size = nums.size\n    // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    val dic = HashMap<Int, Int>()\n    // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    for (i in 0..<size) {\n        if (dic.containsKey(target - nums[i])) {\n            return intArrayOf(dic[target - nums[i]]!!, i)\n        }\n        dic[nums[i]] = i\n    }\n    return IntArray(0)\n}\n
        two_sum.rb
        ### \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 ###\ndef two_sum_hash_table(nums, target)\n  # \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n  dic = {}\n  # \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n  for i in 0...nums.length\n    return [dic[target - nums[i]], i] if dic.has_key?(target - nums[i])\n\n    dic[nums[i]] = i\n  end\n\n  []\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u6b64\u65b9\u6cd5\u901a\u8fc7\u54c8\u5e0c\u67e5\u627e\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n^2)\\) \u964d\u81f3 \\(O(n)\\) \uff0c\u5927\u5e45\u63d0\u5347\u8fd0\u884c\u6548\u7387\u3002

        \u7531\u4e8e\u9700\u8981\u7ef4\u62a4\u4e00\u4e2a\u989d\u5916\u7684\u54c8\u5e0c\u8868\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002\u5c3d\u7ba1\u5982\u6b64\uff0c\u8be5\u65b9\u6cd5\u7684\u6574\u4f53\u65f6\u7a7a\u6548\u7387\u66f4\u4e3a\u5747\u8861\uff0c\u56e0\u6b64\u5b83\u662f\u672c\u9898\u7684\u6700\u4f18\u89e3\u6cd5\u3002

        "},{"location":"chapter_searching/searching_algorithm_revisited/","title":"10.5 \u00a0 \u91cd\u8bc6\u641c\u7d22\u7b97\u6cd5","text":"

        \u641c\u7d22\u7b97\u6cd5\uff08searching algorithm\uff09\u7528\u4e8e\u5728\u6570\u636e\u7ed3\u6784\uff08\u4f8b\u5982\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6811\u6216\u56fe\uff09\u4e2d\u641c\u7d22\u4e00\u4e2a\u6216\u4e00\u7ec4\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002

        \u641c\u7d22\u7b97\u6cd5\u53ef\u6839\u636e\u5b9e\u73b0\u601d\u8def\u5206\u4e3a\u4ee5\u4e0b\u4e24\u7c7b\u3002

        • \u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784\u6765\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\uff0c\u4f8b\u5982\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6811\u548c\u56fe\u7684\u904d\u5386\u7b49\u3002
        • \u5229\u7528\u6570\u636e\u7ec4\u7ec7\u7ed3\u6784\u6216\u6570\u636e\u5305\u542b\u7684\u5148\u9a8c\u4fe1\u606f\uff0c\u5b9e\u73b0\u9ad8\u6548\u5143\u7d20\u67e5\u627e\uff0c\u4f8b\u5982\u4e8c\u5206\u67e5\u627e\u3001\u54c8\u5e0c\u67e5\u627e\u548c\u4e8c\u53c9\u641c\u7d22\u6811\u67e5\u627e\u7b49\u3002

        \u4e0d\u96be\u53d1\u73b0\uff0c\u8fd9\u4e9b\u77e5\u8bc6\u70b9\u90fd\u5df2\u5728\u524d\u9762\u7684\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u56e0\u6b64\u641c\u7d22\u7b97\u6cd5\u5bf9\u4e8e\u6211\u4eec\u6765\u8bf4\u5e76\u4e0d\u964c\u751f\u3002\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4ece\u66f4\u52a0\u7cfb\u7edf\u7684\u89c6\u89d2\u5207\u5165\uff0c\u91cd\u65b0\u5ba1\u89c6\u641c\u7d22\u7b97\u6cd5\u3002

        "},{"location":"chapter_searching/searching_algorithm_revisited/#1051","title":"10.5.1 \u00a0 \u66b4\u529b\u641c\u7d22","text":"

        \u66b4\u529b\u641c\u7d22\u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784\u7684\u6bcf\u4e2a\u5143\u7d20\u6765\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002

        • \u201c\u7ebf\u6027\u641c\u7d22\u201d\u9002\u7528\u4e8e\u6570\u7ec4\u548c\u94fe\u8868\u7b49\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002\u5b83\u4ece\u6570\u636e\u7ed3\u6784\u7684\u4e00\u7aef\u5f00\u59cb\uff0c\u9010\u4e2a\u8bbf\u95ee\u5143\u7d20\uff0c\u76f4\u5230\u627e\u5230\u76ee\u6807\u5143\u7d20\u6216\u5230\u8fbe\u53e6\u4e00\u7aef\u4ecd\u6ca1\u6709\u627e\u5230\u76ee\u6807\u5143\u7d20\u4e3a\u6b62\u3002
        • \u201c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u201d\u548c\u201c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u201d\u662f\u56fe\u548c\u6811\u7684\u4e24\u79cd\u904d\u5386\u7b56\u7565\u3002\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u4ece\u521d\u59cb\u8282\u70b9\u5f00\u59cb\u9010\u5c42\u641c\u7d22\uff0c\u7531\u8fd1\u53ca\u8fdc\u5730\u8bbf\u95ee\u5404\u4e2a\u8282\u70b9\u3002\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u4ece\u521d\u59cb\u8282\u70b9\u5f00\u59cb\uff0c\u6cbf\u7740\u4e00\u6761\u8def\u5f84\u8d70\u5230\u5934\uff0c\u518d\u56de\u6eaf\u5e76\u5c1d\u8bd5\u5176\u4ed6\u8def\u5f84\uff0c\u76f4\u5230\u904d\u5386\u5b8c\u6574\u4e2a\u6570\u636e\u7ed3\u6784\u3002

        \u66b4\u529b\u641c\u7d22\u7684\u4f18\u70b9\u662f\u7b80\u5355\u4e14\u901a\u7528\u6027\u597d\uff0c\u65e0\u987b\u5bf9\u6570\u636e\u505a\u9884\u5904\u7406\u548c\u501f\u52a9\u989d\u5916\u7684\u6570\u636e\u7ed3\u6784\u3002

        \u7136\u800c\uff0c\u6b64\u7c7b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u5143\u7d20\u6570\u91cf\uff0c\u56e0\u6b64\u5728\u6570\u636e\u91cf\u8f83\u5927\u7684\u60c5\u51b5\u4e0b\u6027\u80fd\u8f83\u5dee\u3002

        "},{"location":"chapter_searching/searching_algorithm_revisited/#1052","title":"10.5.2 \u00a0 \u81ea\u9002\u5e94\u641c\u7d22","text":"

        \u81ea\u9002\u5e94\u641c\u7d22\u5229\u7528\u6570\u636e\u7684\u7279\u6709\u5c5e\u6027\uff08\u4f8b\u5982\u6709\u5e8f\u6027\uff09\u6765\u4f18\u5316\u641c\u7d22\u8fc7\u7a0b\uff0c\u4ece\u800c\u66f4\u9ad8\u6548\u5730\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002

        • \u201c\u4e8c\u5206\u67e5\u627e\u201d\u5229\u7528\u6570\u636e\u7684\u6709\u5e8f\u6027\u5b9e\u73b0\u9ad8\u6548\u67e5\u627e\uff0c\u4ec5\u9002\u7528\u4e8e\u6570\u7ec4\u3002
        • \u201c\u54c8\u5e0c\u67e5\u627e\u201d\u5229\u7528\u54c8\u5e0c\u8868\u5c06\u641c\u7d22\u6570\u636e\u548c\u76ee\u6807\u6570\u636e\u5efa\u7acb\u4e3a\u952e\u503c\u5bf9\u6620\u5c04\uff0c\u4ece\u800c\u5b9e\u73b0\u67e5\u8be2\u64cd\u4f5c\u3002
        • \u201c\u6811\u67e5\u627e\u201d\u5728\u7279\u5b9a\u7684\u6811\u7ed3\u6784\uff08\u4f8b\u5982\u4e8c\u53c9\u641c\u7d22\u6811\uff09\u4e2d\uff0c\u57fa\u4e8e\u6bd4\u8f83\u8282\u70b9\u503c\u6765\u5feb\u901f\u6392\u9664\u8282\u70b9\uff0c\u4ece\u800c\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002

        \u6b64\u7c7b\u7b97\u6cd5\u7684\u4f18\u70b9\u662f\u6548\u7387\u9ad8\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe\u5230 \\(O(\\log n)\\) \u751a\u81f3 \\(O(1)\\) \u3002

        \u7136\u800c\uff0c\u4f7f\u7528\u8fd9\u4e9b\u7b97\u6cd5\u5f80\u5f80\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u3002\u4f8b\u5982\uff0c\u4e8c\u5206\u67e5\u627e\u9700\u8981\u9884\u5148\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u54c8\u5e0c\u67e5\u627e\u548c\u6811\u67e5\u627e\u90fd\u9700\u8981\u501f\u52a9\u989d\u5916\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7ef4\u62a4\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u4e5f\u9700\u8981\u989d\u5916\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u5f00\u9500\u3002

        Tip

        \u81ea\u9002\u5e94\u641c\u7d22\u7b97\u6cd5\u5e38\u88ab\u79f0\u4e3a\u67e5\u627e\u7b97\u6cd5\uff0c\u4e3b\u8981\u7528\u4e8e\u5728\u7279\u5b9a\u6570\u636e\u7ed3\u6784\u4e2d\u5feb\u901f\u68c0\u7d22\u76ee\u6807\u5143\u7d20\u3002

        "},{"location":"chapter_searching/searching_algorithm_revisited/#1053","title":"10.5.3 \u00a0 \u641c\u7d22\u65b9\u6cd5\u9009\u53d6","text":"

        \u7ed9\u5b9a\u5927\u5c0f\u4e3a \\(n\\) \u7684\u4e00\u7ec4\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7ebf\u6027\u641c\u7d22\u3001\u4e8c\u5206\u67e5\u627e\u3001\u6811\u67e5\u627e\u3001\u54c8\u5e0c\u67e5\u627e\u7b49\u591a\u79cd\u65b9\u6cd5\u4ece\u4e2d\u641c\u7d22\u76ee\u6807\u5143\u7d20\u3002\u5404\u4e2a\u65b9\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u5982\u56fe 10-11 \u6240\u793a\u3002

        \u56fe 10-11 \u00a0 \u591a\u79cd\u641c\u7d22\u7b56\u7565

        \u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\u7684\u64cd\u4f5c\u6548\u7387\u4e0e\u7279\u6027\u5982\u8868 10-1 \u6240\u793a\u3002

        \u8868 10-1 \u00a0 \u67e5\u627e\u7b97\u6cd5\u6548\u7387\u5bf9\u6bd4

        \u7ebf\u6027\u641c\u7d22 \u4e8c\u5206\u67e5\u627e \u6811\u67e5\u627e \u54c8\u5e0c\u67e5\u627e \u67e5\u627e\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u63d2\u5165\u5143\u7d20 \\(O(1)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u989d\u5916\u7a7a\u95f4 \\(O(1)\\) \\(O(1)\\) \\(O(n)\\) \\(O(n)\\) \u6570\u636e\u9884\u5904\u7406 / \u6392\u5e8f \\(O(n \\log n)\\) \u5efa\u6811 \\(O(n \\log n)\\) \u5efa\u54c8\u5e0c\u8868 \\(O(n)\\) \u6570\u636e\u662f\u5426\u6709\u5e8f \u65e0\u5e8f \u6709\u5e8f \u6709\u5e8f \u65e0\u5e8f

        \u641c\u7d22\u7b97\u6cd5\u7684\u9009\u62e9\u8fd8\u53d6\u51b3\u89c4\u6a21\u3001\u641c\u7d22\u6027\u80fd\u8981\u6c42\u3001\u6570\u636e\u67e5\u8be2\u4e0e\u66f4\u65b0\u9891\u7387\u7b49\u3002

        \u7ebf\u6027\u641c\u7d22

        • \u901a\u7528\u6027\u8f83\u597d\uff0c\u65e0\u987b\u4efb\u4f55\u6570\u636e\u9884\u5904\u7406\u64cd\u4f5c\u3002\u5047\u5982\u6211\u4eec\u4ec5\u9700\u67e5\u8be2\u4e00\u6b21\u6570\u636e\uff0c\u90a3\u4e48\u5176\u4ed6\u4e09\u79cd\u65b9\u6cd5\u7684\u6570\u636e\u9884\u5904\u7406\u7684\u65f6\u95f4\u6bd4\u7ebf\u6027\u641c\u7d22\u7684\u65f6\u95f4\u8fd8\u8981\u66f4\u957f\u3002
        • \u9002\u7528\u4e8e\u4f53\u91cf\u8f83\u5c0f\u7684\u6570\u636e\uff0c\u6b64\u60c5\u51b5\u4e0b\u65f6\u95f4\u590d\u6742\u5ea6\u5bf9\u6548\u7387\u5f71\u54cd\u8f83\u5c0f\u3002
        • \u9002\u7528\u4e8e\u6570\u636e\u66f4\u65b0\u9891\u7387\u8f83\u9ad8\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u8be5\u65b9\u6cd5\u4e0d\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u4efb\u4f55\u989d\u5916\u7ef4\u62a4\u3002

        \u4e8c\u5206\u67e5\u627e

        • \u9002\u7528\u4e8e\u5927\u6570\u636e\u91cf\u7684\u60c5\u51b5\uff0c\u6548\u7387\u8868\u73b0\u7a33\u5b9a\uff0c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u3002
        • \u6570\u636e\u91cf\u4e0d\u80fd\u8fc7\u5927\uff0c\u56e0\u4e3a\u5b58\u50a8\u6570\u7ec4\u9700\u8981\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u3002
        • \u4e0d\u9002\u7528\u4e8e\u9ad8\u9891\u589e\u5220\u6570\u636e\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u7ef4\u62a4\u6709\u5e8f\u6570\u7ec4\u7684\u5f00\u9500\u8f83\u5927\u3002

        \u54c8\u5e0c\u67e5\u627e

        • \u9002\u5408\u5bf9\u67e5\u8be2\u6027\u80fd\u8981\u6c42\u5f88\u9ad8\u7684\u573a\u666f\uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002
        • \u4e0d\u9002\u5408\u9700\u8981\u6709\u5e8f\u6570\u636e\u6216\u8303\u56f4\u67e5\u627e\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u54c8\u5e0c\u8868\u65e0\u6cd5\u7ef4\u62a4\u6570\u636e\u7684\u6709\u5e8f\u6027\u3002
        • \u5bf9\u54c8\u5e0c\u51fd\u6570\u548c\u54c8\u5e0c\u51b2\u7a81\u5904\u7406\u7b56\u7565\u7684\u4f9d\u8d56\u6027\u8f83\u9ad8\uff0c\u5177\u6709\u8f83\u5927\u7684\u6027\u80fd\u52a3\u5316\u98ce\u9669\u3002
        • \u4e0d\u9002\u5408\u6570\u636e\u91cf\u8fc7\u5927\u7684\u60c5\u51b5\uff0c\u56e0\u4e3a\u54c8\u5e0c\u8868\u9700\u8981\u989d\u5916\u7a7a\u95f4\u6765\u6700\u5927\u7a0b\u5ea6\u5730\u51cf\u5c11\u51b2\u7a81\uff0c\u4ece\u800c\u63d0\u4f9b\u826f\u597d\u7684\u67e5\u8be2\u6027\u80fd\u3002

        \u6811\u67e5\u627e

        • \u9002\u7528\u4e8e\u6d77\u91cf\u6570\u636e\uff0c\u56e0\u4e3a\u6811\u8282\u70b9\u5728\u5185\u5b58\u4e2d\u662f\u5206\u6563\u5b58\u50a8\u7684\u3002
        • \u9002\u5408\u9700\u8981\u7ef4\u62a4\u6709\u5e8f\u6570\u636e\u6216\u8303\u56f4\u67e5\u627e\u7684\u573a\u666f\u3002
        • \u5728\u6301\u7eed\u589e\u5220\u8282\u70b9\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u53ef\u80fd\u4ea7\u751f\u503e\u659c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(n)\\) \u3002
        • \u82e5\u4f7f\u7528 AVL \u6811\u6216\u7ea2\u9ed1\u6811\uff0c\u5219\u5404\u9879\u64cd\u4f5c\u53ef\u5728 \\(O(\\log n)\\) \u6548\u7387\u4e0b\u7a33\u5b9a\u8fd0\u884c\uff0c\u4f46\u7ef4\u62a4\u6811\u5e73\u8861\u7684\u64cd\u4f5c\u4f1a\u589e\u52a0\u989d\u5916\u7684\u5f00\u9500\u3002
        "},{"location":"chapter_searching/summary/","title":"10.6 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_searching/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u4e8c\u5206\u67e5\u627e\u4f9d\u8d56\u6570\u636e\u7684\u6709\u5e8f\u6027\uff0c\u901a\u8fc7\u5faa\u73af\u9010\u6b65\u7f29\u51cf\u4e00\u534a\u641c\u7d22\u533a\u95f4\u6765\u8fdb\u884c\u67e5\u627e\u3002\u5b83\u8981\u6c42\u8f93\u5165\u6570\u636e\u6709\u5e8f\uff0c\u4e14\u4ec5\u9002\u7528\u4e8e\u6570\u7ec4\u6216\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u3002
        • \u66b4\u529b\u641c\u7d22\u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784\u6765\u5b9a\u4f4d\u6570\u636e\u3002\u7ebf\u6027\u641c\u7d22\u9002\u7528\u4e8e\u6570\u7ec4\u548c\u94fe\u8868\uff0c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u548c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u9002\u7528\u4e8e\u56fe\u548c\u6811\u3002\u6b64\u7c7b\u7b97\u6cd5\u901a\u7528\u6027\u597d\uff0c\u65e0\u987b\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\uff0c\u4f46\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u8f83\u9ad8\u3002
        • \u54c8\u5e0c\u67e5\u627e\u3001\u6811\u67e5\u627e\u548c\u4e8c\u5206\u67e5\u627e\u5c5e\u4e8e\u9ad8\u6548\u641c\u7d22\u65b9\u6cd5\uff0c\u53ef\u5728\u7279\u5b9a\u6570\u636e\u7ed3\u6784\u4e2d\u5feb\u901f\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002\u6b64\u7c7b\u7b97\u6cd5\u6548\u7387\u9ad8\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe \\(O(\\log n)\\) \u751a\u81f3 \\(O(1)\\) \uff0c\u4f46\u901a\u5e38\u9700\u8981\u501f\u52a9\u989d\u5916\u6570\u636e\u7ed3\u6784\u3002
        • \u5b9e\u9645\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u6570\u636e\u89c4\u6a21\u3001\u641c\u7d22\u6027\u80fd\u8981\u6c42\u3001\u6570\u636e\u67e5\u8be2\u548c\u66f4\u65b0\u9891\u7387\u7b49\u56e0\u7d20\u8fdb\u884c\u5177\u4f53\u5206\u6790\uff0c\u4ece\u800c\u9009\u62e9\u5408\u9002\u7684\u641c\u7d22\u65b9\u6cd5\u3002
        • \u7ebf\u6027\u641c\u7d22\u9002\u7528\u4e8e\u5c0f\u578b\u6216\u9891\u7e41\u66f4\u65b0\u7684\u6570\u636e\uff1b\u4e8c\u5206\u67e5\u627e\u9002\u7528\u4e8e\u5927\u578b\u3001\u6392\u5e8f\u7684\u6570\u636e\uff1b\u54c8\u5e0c\u67e5\u627e\u9002\u7528\u4e8e\u5bf9\u67e5\u8be2\u6548\u7387\u8981\u6c42\u8f83\u9ad8\u4e14\u65e0\u987b\u8303\u56f4\u67e5\u8be2\u7684\u6570\u636e\uff1b\u6811\u67e5\u627e\u9002\u7528\u4e8e\u9700\u8981\u7ef4\u62a4\u987a\u5e8f\u548c\u652f\u6301\u8303\u56f4\u67e5\u8be2\u7684\u5927\u578b\u52a8\u6001\u6570\u636e\u3002
        • \u7528\u54c8\u5e0c\u67e5\u627e\u66ff\u6362\u7ebf\u6027\u67e5\u627e\u662f\u4e00\u79cd\u5e38\u7528\u7684\u4f18\u5316\u8fd0\u884c\u65f6\u95f4\u7684\u7b56\u7565\uff0c\u53ef\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n)\\) \u964d\u81f3 \\(O(1)\\) \u3002
        "},{"location":"chapter_sorting/","title":"\u7b2c 11 \u7ae0 \u00a0 \u6392\u5e8f","text":"

        Abstract

        \u6392\u5e8f\u72b9\u5982\u4e00\u628a\u5c06\u6df7\u4e71\u53d8\u4e3a\u79e9\u5e8f\u7684\u9b54\u6cd5\u94a5\u5319\uff0c\u4f7f\u6211\u4eec\u80fd\u4ee5\u66f4\u9ad8\u6548\u7684\u65b9\u5f0f\u7406\u89e3\u4e0e\u5904\u7406\u6570\u636e\u3002

        \u65e0\u8bba\u662f\u7b80\u5355\u7684\u5347\u5e8f\uff0c\u8fd8\u662f\u590d\u6742\u7684\u5206\u7c7b\u6392\u5217\uff0c\u6392\u5e8f\u90fd\u5411\u6211\u4eec\u5c55\u793a\u4e86\u6570\u636e\u7684\u548c\u8c10\u7f8e\u611f\u3002

        "},{"location":"chapter_sorting/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 11.1 \u00a0 \u6392\u5e8f\u7b97\u6cd5
        • 11.2 \u00a0 \u9009\u62e9\u6392\u5e8f
        • 11.3 \u00a0 \u5192\u6ce1\u6392\u5e8f
        • 11.4 \u00a0 \u63d2\u5165\u6392\u5e8f
        • 11.5 \u00a0 \u5feb\u901f\u6392\u5e8f
        • 11.6 \u00a0 \u5f52\u5e76\u6392\u5e8f
        • 11.7 \u00a0 \u5806\u6392\u5e8f
        • 11.8 \u00a0 \u6876\u6392\u5e8f
        • 11.9 \u00a0 \u8ba1\u6570\u6392\u5e8f
        • 11.10 \u00a0 \u57fa\u6570\u6392\u5e8f
        • 11.11 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_sorting/bubble_sort/","title":"11.3 \u00a0 \u5192\u6ce1\u6392\u5e8f","text":"

        \u5192\u6ce1\u6392\u5e8f\uff08bubble sort\uff09\u901a\u8fc7\u8fde\u7eed\u5730\u6bd4\u8f83\u4e0e\u4ea4\u6362\u76f8\u90bb\u5143\u7d20\u5b9e\u73b0\u6392\u5e8f\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5c31\u50cf\u6c14\u6ce1\u4ece\u5e95\u90e8\u5347\u5230\u9876\u90e8\u4e00\u6837\uff0c\u56e0\u6b64\u5f97\u540d\u5192\u6ce1\u6392\u5e8f\u3002

        \u5982\u56fe 11-4 \u6240\u793a\uff0c\u5192\u6ce1\u8fc7\u7a0b\u53ef\u4ee5\u5229\u7528\u5143\u7d20\u4ea4\u6362\u64cd\u4f5c\u6765\u6a21\u62df\uff1a\u4ece\u6570\u7ec4\u6700\u5de6\u7aef\u5f00\u59cb\u5411\u53f3\u904d\u5386\uff0c\u4f9d\u6b21\u6bd4\u8f83\u76f8\u90bb\u5143\u7d20\u5927\u5c0f\uff0c\u5982\u679c\u201c\u5de6\u5143\u7d20 > \u53f3\u5143\u7d20\u201d\u5c31\u4ea4\u6362\u4e8c\u8005\u3002\u904d\u5386\u5b8c\u6210\u540e\uff0c\u6700\u5927\u7684\u5143\u7d20\u4f1a\u88ab\u79fb\u52a8\u5230\u6570\u7ec4\u7684\u6700\u53f3\u7aef\u3002

        <1><2><3><4><5><6><7>

        \u56fe 11-4 \u00a0 \u5229\u7528\u5143\u7d20\u4ea4\u6362\u64cd\u4f5c\u6a21\u62df\u5192\u6ce1

        "},{"location":"chapter_sorting/bubble_sort/#1131","title":"11.3.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

        \u8bbe\u6570\u7ec4\u7684\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u5192\u6ce1\u6392\u5e8f\u7684\u6b65\u9aa4\u5982\u56fe 11-5 \u6240\u793a\u3002

        1. \u9996\u5148\uff0c\u5bf9 \\(n\\) \u4e2a\u5143\u7d20\u6267\u884c\u201c\u5192\u6ce1\u201d\uff0c\u5c06\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u6b63\u786e\u4f4d\u7f6e\u3002
        2. \u63a5\u4e0b\u6765\uff0c\u5bf9\u5269\u4f59 \\(n - 1\\) \u4e2a\u5143\u7d20\u6267\u884c\u201c\u5192\u6ce1\u201d\uff0c\u5c06\u7b2c\u4e8c\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u6b63\u786e\u4f4d\u7f6e\u3002
        3. \u4ee5\u6b64\u7c7b\u63a8\uff0c\u7ecf\u8fc7 \\(n - 1\\) \u8f6e\u201c\u5192\u6ce1\u201d\u540e\uff0c\u524d \\(n - 1\\) \u5927\u7684\u5143\u7d20\u90fd\u88ab\u4ea4\u6362\u81f3\u6b63\u786e\u4f4d\u7f6e\u3002
        4. \u4ec5\u5269\u7684\u4e00\u4e2a\u5143\u7d20\u5fc5\u5b9a\u662f\u6700\u5c0f\u5143\u7d20\uff0c\u65e0\u987b\u6392\u5e8f\uff0c\u56e0\u6b64\u6570\u7ec4\u6392\u5e8f\u5b8c\u6210\u3002

        \u56fe 11-5 \u00a0 \u5192\u6ce1\u6392\u5e8f\u6d41\u7a0b

        \u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby bubble_sort.py
        def bubble_sort(nums: list[int]):\n    \"\"\"\u5192\u6ce1\u6392\u5e8f\"\"\"\n    n = len(nums)\n    # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i in range(n - 1, 0, -1):\n        # \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for j in range(i):\n            if nums[j] > nums[j + 1]:\n                # \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                nums[j], nums[j + 1] = nums[j + 1], nums[j]\n
        bubble_sort.cpp
        /* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(vector<int> &nums) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = nums.size() - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                // \u8fd9\u91cc\u4f7f\u7528\u4e86 std::swap() \u51fd\u6570\n                swap(nums[j], nums[j + 1]);\n            }\n        }\n    }\n}\n
        bubble_sort.java
        /* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(int[] nums) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = nums.length - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n            }\n        }\n    }\n}\n
        bubble_sort.cs
        /* \u5192\u6ce1\u6392\u5e8f */\nvoid BubbleSort(int[] nums) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = nums.Length - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n            }\n        }\n    }\n}\n
        bubble_sort.go
        /* \u5192\u6ce1\u6392\u5e8f */\nfunc bubbleSort(nums []int) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i := len(nums) - 1; i > 0; i-- {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for j := 0; j < i; j++ {\n            if nums[j] > nums[j+1] {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                nums[j], nums[j+1] = nums[j+1], nums[j]\n            }\n        }\n    }\n}\n
        bubble_sort.swift
        /* \u5192\u6ce1\u6392\u5e8f */\nfunc bubbleSort(nums: inout [Int]) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i in nums.indices.dropFirst().reversed() {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for j in 0 ..< i {\n            if nums[j] > nums[j + 1] {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                nums.swapAt(j, j + 1)\n            }\n        }\n    }\n}\n
        bubble_sort.js
        /* \u5192\u6ce1\u6392\u5e8f */\nfunction bubbleSort(nums) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n            }\n        }\n    }\n}\n
        bubble_sort.ts
        /* \u5192\u6ce1\u6392\u5e8f */\nfunction bubbleSort(nums: number[]): void {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n            }\n        }\n    }\n}\n
        bubble_sort.dart
        /* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(List<int> nums) {\n  // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n  for (int i = nums.length - 1; i > 0; i--) {\n    // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n    for (int j = 0; j < i; j++) {\n      if (nums[j] > nums[j + 1]) {\n        // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n        int tmp = nums[j];\n        nums[j] = nums[j + 1];\n        nums[j + 1] = tmp;\n      }\n    }\n  }\n}\n
        bubble_sort.rs
        /* \u5192\u6ce1\u6392\u5e8f */\nfn bubble_sort(nums: &mut [i32]) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i in (1..nums.len()).rev() {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for j in 0..i {\n            if nums[j] > nums[j + 1] {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                nums.swap(j, j + 1);\n            }\n        }\n    }\n}\n
        bubble_sort.c
        /* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(int nums[], int size) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = size - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                int temp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = temp;\n            }\n        }\n    }\n}\n
        bubble_sort.kt
        /* \u5192\u6ce1\u6392\u5e8f */\nfun bubbleSort(nums: IntArray) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (i in nums.size - 1 downTo 1) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (j in 0..<i) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                val temp = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = temp\n            }\n        }\n    }\n}\n
        bubble_sort.rb
        ### \u5192\u6ce1\u6392\u5e8f ###\ndef bubble_sort(nums)\n  n = nums.length\n  # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n  for i in (n - 1).downto(1)\n    # \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n    for j in 0...i\n      if nums[j] > nums[j + 1]\n        # \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n        nums[j], nums[j + 1] = nums[j + 1], nums[j]\n      end\n    end\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/bubble_sort/#1132","title":"11.3.2 \u00a0 \u6548\u7387\u4f18\u5316","text":"

        \u6211\u4eec\u53d1\u73b0\uff0c\u5982\u679c\u67d0\u8f6e\u201c\u5192\u6ce1\u201d\u4e2d\u6ca1\u6709\u6267\u884c\u4efb\u4f55\u4ea4\u6362\u64cd\u4f5c\uff0c\u8bf4\u660e\u6570\u7ec4\u5df2\u7ecf\u5b8c\u6210\u6392\u5e8f\uff0c\u53ef\u76f4\u63a5\u8fd4\u56de\u7ed3\u679c\u3002\u56e0\u6b64\uff0c\u53ef\u4ee5\u589e\u52a0\u4e00\u4e2a\u6807\u5fd7\u4f4d flag \u6765\u76d1\u6d4b\u8fd9\u79cd\u60c5\u51b5\uff0c\u4e00\u65e6\u51fa\u73b0\u5c31\u7acb\u5373\u8fd4\u56de\u3002

        \u7ecf\u8fc7\u4f18\u5316\uff0c\u5192\u6ce1\u6392\u5e8f\u7684\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(n^2)\\) \uff1b\u4f46\u5f53\u8f93\u5165\u6570\u7ec4\u5b8c\u5168\u6709\u5e8f\u65f6\uff0c\u53ef\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby bubble_sort.py
        def bubble_sort_with_flag(nums: list[int]):\n    \"\"\"\u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09\"\"\"\n    n = len(nums)\n    # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i in range(n - 1, 0, -1):\n        flag = False  # \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n        # \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for j in range(i):\n            if nums[j] > nums[j + 1]:\n                # \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                nums[j], nums[j + 1] = nums[j + 1], nums[j]\n                flag = True  # \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n        if not flag:\n            break  # \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n
        bubble_sort.cpp
        /* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid bubbleSortWithFlag(vector<int> &nums) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = nums.size() - 1; i > 0; i--) {\n        bool flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                // \u8fd9\u91cc\u4f7f\u7528\u4e86 std::swap() \u51fd\u6570\n                swap(nums[j], nums[j + 1]);\n                flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n            }\n        }\n        if (!flag)\n            break; // \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n    }\n}\n
        bubble_sort.java
        /* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09 */\nvoid bubbleSortWithFlag(int[] nums) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = nums.length - 1; i > 0; i--) {\n        boolean flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n            }\n        }\n        if (!flag)\n            break; // \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n    }\n}\n
        bubble_sort.cs
        /* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid BubbleSortWithFlag(int[] nums) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = nums.Length - 1; i > 0; i--) {\n        bool flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n                flag = true;  // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n            }\n        }\n        if (!flag) break;     // \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n    }\n}\n
        bubble_sort.go
        /* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunc bubbleSortWithFlag(nums []int) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i := len(nums) - 1; i > 0; i-- {\n        flag := false // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for j := 0; j < i; j++ {\n            if nums[j] > nums[j+1] {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                nums[j], nums[j+1] = nums[j+1], nums[j]\n                flag = true // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n            }\n        }\n        if flag == false { // \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n            break\n        }\n    }\n}\n
        bubble_sort.swift
        /* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunc bubbleSortWithFlag(nums: inout [Int]) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i in nums.indices.dropFirst().reversed() {\n        var flag = false // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n        for j in 0 ..< i {\n            if nums[j] > nums[j + 1] {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                nums.swapAt(j, j + 1)\n                flag = true // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n            }\n        }\n        if !flag { // \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n            break\n        }\n    }\n}\n
        bubble_sort.js
        /* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunction bubbleSortWithFlag(nums) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        let flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n            }\n        }\n        if (!flag) break; // \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n    }\n}\n
        bubble_sort.ts
        /* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunction bubbleSortWithFlag(nums: number[]): void {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        let flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n            }\n        }\n        if (!flag) break; // \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n    }\n}\n
        bubble_sort.dart
        /* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid bubbleSortWithFlag(List<int> nums) {\n  // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n  for (int i = nums.length - 1; i > 0; i--) {\n    bool flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n    // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n    for (int j = 0; j < i; j++) {\n      if (nums[j] > nums[j + 1]) {\n        // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n        int tmp = nums[j];\n        nums[j] = nums[j + 1];\n        nums[j + 1] = tmp;\n        flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n      }\n    }\n    if (!flag) break; // \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n  }\n}\n
        bubble_sort.rs
        /* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09 */\nfn bubble_sort_with_flag(nums: &mut [i32]) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i in (1..nums.len()).rev() {\n        let mut flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for j in 0..i {\n            if nums[j] > nums[j + 1] {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                nums.swap(j, j + 1);\n                flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n            }\n        }\n        if !flag {\n            break; // \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n        };\n    }\n}\n
        bubble_sort.c
        /* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid bubbleSortWithFlag(int nums[], int size) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = size - 1; i > 0; i--) {\n        bool flag = false;\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                int temp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = temp;\n                flag = true;\n            }\n        }\n        if (!flag)\n            break;\n    }\n}\n
        bubble_sort.kt
        /* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09 */\nfun bubbleSortWithFlag(nums: IntArray) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (i in nums.size - 1 downTo 1) {\n        var flag = false // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (j in 0..<i) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                val temp = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = temp\n                flag = true // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n            }\n        }\n        if (!flag) break // \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n    }\n}\n
        bubble_sort.rb
        ### \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09###\ndef bubble_sort_with_flag(nums)\n  n = nums.length\n  # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n  for i in (n - 1).downto(1)\n    flag = false # \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n\n    # \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n    for j in 0...i\n      if nums[j] > nums[j + 1]\n        # \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n        nums[j], nums[j + 1] = nums[j + 1], nums[j]\n        flag = true # \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n      end\n    end\n\n    break unless flag # \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/bubble_sort/#1133","title":"11.3.3 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\)\u3001\u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u5404\u8f6e\u201c\u5192\u6ce1\u201d\u904d\u5386\u7684\u6570\u7ec4\u957f\u5ea6\u4f9d\u6b21\u4e3a \\(n - 1\\)\u3001\\(n - 2\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \uff0c\u603b\u548c\u4e3a \\((n - 1) n / 2\\) \u3002\u5728\u5f15\u5165 flag \u4f18\u5316\u540e\uff0c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe\u5230 \\(O(n)\\) \u3002
        • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\)\u3001\u539f\u5730\u6392\u5e8f\uff1a\u6307\u9488 \\(i\\) \u548c \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002
        • \u7a33\u5b9a\u6392\u5e8f\uff1a\u7531\u4e8e\u5728\u201c\u5192\u6ce1\u201d\u4e2d\u9047\u5230\u76f8\u7b49\u5143\u7d20\u4e0d\u4ea4\u6362\u3002
        "},{"location":"chapter_sorting/bucket_sort/","title":"11.8 \u00a0 \u6876\u6392\u5e8f","text":"

        \u524d\u8ff0\u51e0\u79cd\u6392\u5e8f\u7b97\u6cd5\u90fd\u5c5e\u4e8e\u201c\u57fa\u4e8e\u6bd4\u8f83\u7684\u6392\u5e8f\u7b97\u6cd5\u201d\uff0c\u5b83\u4eec\u901a\u8fc7\u6bd4\u8f83\u5143\u7d20\u95f4\u7684\u5927\u5c0f\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u6b64\u7c7b\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u65e0\u6cd5\u8d85\u8d8a \\(O(n \\log n)\\) \u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u51e0\u79cd\u201c\u975e\u6bd4\u8f83\u6392\u5e8f\u7b97\u6cd5\u201d\uff0c\u5b83\u4eec\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u8fbe\u5230\u7ebf\u6027\u9636\u3002

        \u6876\u6392\u5e8f\uff08bucket sort\uff09\u662f\u5206\u6cbb\u7b56\u7565\u7684\u4e00\u4e2a\u5178\u578b\u5e94\u7528\u3002\u5b83\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e9b\u5177\u6709\u5927\u5c0f\u987a\u5e8f\u7684\u6876\uff0c\u6bcf\u4e2a\u6876\u5bf9\u5e94\u4e00\u4e2a\u6570\u636e\u8303\u56f4\uff0c\u5c06\u6570\u636e\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\uff1b\u7136\u540e\uff0c\u5728\u6bcf\u4e2a\u6876\u5185\u90e8\u5206\u522b\u6267\u884c\u6392\u5e8f\uff1b\u6700\u7ec8\u6309\u7167\u6876\u7684\u987a\u5e8f\u5c06\u6240\u6709\u6570\u636e\u5408\u5e76\u3002

        "},{"location":"chapter_sorting/bucket_sort/#1181","title":"11.8.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

        \u8003\u8651\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\uff0c\u5176\u5143\u7d20\u662f\u8303\u56f4 \\([0, 1)\\) \u5185\u7684\u6d6e\u70b9\u6570\u3002\u6876\u6392\u5e8f\u7684\u6d41\u7a0b\u5982\u56fe 11-13 \u6240\u793a\u3002

        1. \u521d\u59cb\u5316 \\(k\\) \u4e2a\u6876\uff0c\u5c06 \\(n\\) \u4e2a\u5143\u7d20\u5206\u914d\u5230 \\(k\\) \u4e2a\u6876\u4e2d\u3002
        2. \u5bf9\u6bcf\u4e2a\u6876\u5206\u522b\u6267\u884c\u6392\u5e8f\uff08\u8fd9\u91cc\u91c7\u7528\u7f16\u7a0b\u8bed\u8a00\u7684\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff09\u3002
        3. \u6309\u7167\u6876\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u5408\u5e76\u7ed3\u679c\u3002

        \u56fe 11-13 \u00a0 \u6876\u6392\u5e8f\u7b97\u6cd5\u6d41\u7a0b

        \u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby bucket_sort.py
        def bucket_sort(nums: list[float]):\n    \"\"\"\u6876\u6392\u5e8f\"\"\"\n    # \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n    k = len(nums) // 2\n    buckets = [[] for _ in range(k)]\n    # 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n    for num in nums:\n        # \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n        i = int(num * k)\n        # \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n        buckets[i].append(num)\n    # 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n    for bucket in buckets:\n        # \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n        bucket.sort()\n    # 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n    i = 0\n    for bucket in buckets:\n        for num in bucket:\n            nums[i] = num\n            i += 1\n
        bucket_sort.cpp
        /* \u6876\u6392\u5e8f */\nvoid bucketSort(vector<float> &nums) {\n    // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n    int k = nums.size() / 2;\n    vector<vector<float>> buckets(k);\n    // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n    for (float num : nums) {\n        // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n        int i = num * k;\n        // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 bucket_idx\n        buckets[i].push_back(num);\n    }\n    // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n    for (vector<float> &bucket : buckets) {\n        // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n        sort(bucket.begin(), bucket.end());\n    }\n    // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n    int i = 0;\n    for (vector<float> &bucket : buckets) {\n        for (float num : bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
        bucket_sort.java
        /* \u6876\u6392\u5e8f */\nvoid bucketSort(float[] nums) {\n    // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n    int k = nums.length / 2;\n    List<List<Float>> buckets = new ArrayList<>();\n    for (int i = 0; i < k; i++) {\n        buckets.add(new ArrayList<>());\n    }\n    // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n    for (float num : nums) {\n        // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n        int i = (int) (num * k);\n        // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n        buckets.get(i).add(num);\n    }\n    // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n    for (List<Float> bucket : buckets) {\n        // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n        Collections.sort(bucket);\n    }\n    // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n    int i = 0;\n    for (List<Float> bucket : buckets) {\n        for (float num : bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
        bucket_sort.cs
        /* \u6876\u6392\u5e8f */\nvoid BucketSort(float[] nums) {\n    // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n    int k = nums.Length / 2;\n    List<List<float>> buckets = [];\n    for (int i = 0; i < k; i++) {\n        buckets.Add([]);\n    }\n    // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n    foreach (float num in nums) {\n        // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n        int i = (int)(num * k);\n        // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n        buckets[i].Add(num);\n    }\n    // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n    foreach (List<float> bucket in buckets) {\n        // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n        bucket.Sort();\n    }\n    // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n    int j = 0;\n    foreach (List<float> bucket in buckets) {\n        foreach (float num in bucket) {\n            nums[j++] = num;\n        }\n    }\n}\n
        bucket_sort.go
        /* \u6876\u6392\u5e8f */\nfunc bucketSort(nums []float64) {\n    // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n    k := len(nums) / 2\n    buckets := make([][]float64, k)\n    for i := 0; i < k; i++ {\n        buckets[i] = make([]float64, 0)\n    }\n    // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n    for _, num := range nums {\n        // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n        i := int(num * float64(k))\n        // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n        buckets[i] = append(buckets[i], num)\n    }\n    // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n    for i := 0; i < k; i++ {\n        // \u4f7f\u7528\u5185\u7f6e\u5207\u7247\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n        sort.Float64s(buckets[i])\n    }\n    // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n    i := 0\n    for _, bucket := range buckets {\n        for _, num := range bucket {\n            nums[i] = num\n            i++\n        }\n    }\n}\n
        bucket_sort.swift
        /* \u6876\u6392\u5e8f */\nfunc bucketSort(nums: inout [Double]) {\n    // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n    let k = nums.count / 2\n    var buckets = (0 ..< k).map { _ in [Double]() }\n    // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n    for num in nums {\n        // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n        let i = Int(num * Double(k))\n        // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n        buckets[i].append(num)\n    }\n    // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n    for i in buckets.indices {\n        // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n        buckets[i].sort()\n    }\n    // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n    var i = nums.startIndex\n    for bucket in buckets {\n        for num in bucket {\n            nums[i] = num\n            i += 1\n        }\n    }\n}\n
        bucket_sort.js
        /* \u6876\u6392\u5e8f */\nfunction bucketSort(nums) {\n    // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n    const k = nums.length / 2;\n    const buckets = [];\n    for (let i = 0; i < k; i++) {\n        buckets.push([]);\n    }\n    // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n    for (const num of nums) {\n        // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n        const i = Math.floor(num * k);\n        // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n        buckets[i].push(num);\n    }\n    // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n    for (const bucket of buckets) {\n        // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n        bucket.sort((a, b) => a - b);\n    }\n    // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n    let i = 0;\n    for (const bucket of buckets) {\n        for (const num of bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
        bucket_sort.ts
        /* \u6876\u6392\u5e8f */\nfunction bucketSort(nums: number[]): void {\n    // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n    const k = nums.length / 2;\n    const buckets: number[][] = [];\n    for (let i = 0; i < k; i++) {\n        buckets.push([]);\n    }\n    // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n    for (const num of nums) {\n        // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n        const i = Math.floor(num * k);\n        // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n        buckets[i].push(num);\n    }\n    // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n    for (const bucket of buckets) {\n        // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n        bucket.sort((a, b) => a - b);\n    }\n    // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n    let i = 0;\n    for (const bucket of buckets) {\n        for (const num of bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
        bucket_sort.dart
        /* \u6876\u6392\u5e8f */\nvoid bucketSort(List<double> nums) {\n  // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n  int k = nums.length ~/ 2;\n  List<List<double>> buckets = List.generate(k, (index) => []);\n\n  // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n  for (double _num in nums) {\n    // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 _num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n    int i = (_num * k).toInt();\n    // \u5c06 _num \u6dfb\u52a0\u8fdb\u6876 bucket_idx\n    buckets[i].add(_num);\n  }\n  // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n  for (List<double> bucket in buckets) {\n    bucket.sort();\n  }\n  // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n  int i = 0;\n  for (List<double> bucket in buckets) {\n    for (double _num in bucket) {\n      nums[i++] = _num;\n    }\n  }\n}\n
        bucket_sort.rs
        /* \u6876\u6392\u5e8f */\nfn bucket_sort(nums: &mut [f64]) {\n    // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n    let k = nums.len() / 2;\n    let mut buckets = vec![vec![]; k];\n    // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n    for &num in nums.iter() {\n        // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n        let i = (num * k as f64) as usize;\n        // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n        buckets[i].push(num);\n    }\n    // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n    for bucket in &mut buckets {\n        // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n        bucket.sort_by(|a, b| a.partial_cmp(b).unwrap());\n    }\n    // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n    let mut i = 0;\n    for bucket in buckets.iter() {\n        for &num in bucket.iter() {\n            nums[i] = num;\n            i += 1;\n        }\n    }\n}\n
        bucket_sort.c
        /* \u6876\u6392\u5e8f */\nvoid bucketSort(float nums[], int n) {\n    int k = n / 2;                                 // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\n    int *sizes = malloc(k * sizeof(int));          // \u8bb0\u5f55\u6bcf\u4e2a\u6876\u7684\u5927\u5c0f\n    float **buckets = malloc(k * sizeof(float *)); // \u52a8\u6001\u6570\u7ec4\u7684\u6570\u7ec4\uff08\u6876\uff09\n    // \u4e3a\u6bcf\u4e2a\u6876\u9884\u5206\u914d\u8db3\u591f\u7684\u7a7a\u95f4\n    for (int i = 0; i < k; ++i) {\n        buckets[i] = (float *)malloc(n * sizeof(float));\n        sizes[i] = 0;\n    }\n    // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n    for (int i = 0; i < n; ++i) {\n        int idx = (int)(nums[i] * k);\n        buckets[idx][sizes[idx]++] = nums[i];\n    }\n    // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n    for (int i = 0; i < k; ++i) {\n        qsort(buckets[i], sizes[i], sizeof(float), compare);\n    }\n    // 3. \u5408\u5e76\u6392\u5e8f\u540e\u7684\u6876\n    int idx = 0;\n    for (int i = 0; i < k; ++i) {\n        for (int j = 0; j < sizes[i]; ++j) {\n            nums[idx++] = buckets[i][j];\n        }\n        // \u91ca\u653e\u5185\u5b58\n        free(buckets[i]);\n    }\n}\n
        bucket_sort.kt
        /* \u6876\u6392\u5e8f */\nfun bucketSort(nums: FloatArray) {\n    // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n    val k = nums.size / 2\n    val buckets = mutableListOf<MutableList<Float>>()\n    for (i in 0..<k) {\n        buckets.add(mutableListOf())\n    }\n    // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n    for (num in nums) {\n        // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n        val i = (num * k).toInt()\n        // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n        buckets[i].add(num)\n    }\n    // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n    for (bucket in buckets) {\n        // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n        bucket.sort()\n    }\n    // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n    var i = 0\n    for (bucket in buckets) {\n        for (num in bucket) {\n            nums[i++] = num\n        }\n    }\n}\n
        bucket_sort.rb
        ### \u6876\u6392\u5e8f ###\ndef bucket_sort(nums)\n  # \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n  k = nums.length / 2\n  buckets = Array.new(k) { [] }\n\n  # 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n  nums.each do |num|\n    # \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n    i = (num * k).to_i\n    # \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n    buckets[i] << num\n  end\n\n  # 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n  buckets.each do |bucket|\n    # \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n    bucket.sort!\n  end\n\n  # 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n  i = 0\n  buckets.each do |bucket|\n    bucket.each do |num|\n      nums[i] = num\n      i += 1\n    end\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/bucket_sort/#1182","title":"11.8.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"

        \u6876\u6392\u5e8f\u9002\u7528\u4e8e\u5904\u7406\u4f53\u91cf\u5f88\u5927\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u8f93\u5165\u6570\u636e\u5305\u542b 100 \u4e07\u4e2a\u5143\u7d20\uff0c\u7531\u4e8e\u7a7a\u95f4\u9650\u5236\uff0c\u7cfb\u7edf\u5185\u5b58\u65e0\u6cd5\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5206\u6210 1000 \u4e2a\u6876\uff0c\u7136\u540e\u5206\u522b\u5bf9\u6bcf\u4e2a\u6876\u8fdb\u884c\u6392\u5e8f\uff0c\u6700\u540e\u5c06\u7ed3\u679c\u5408\u5e76\u3002

        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n + k)\\) \uff1a\u5047\u8bbe\u5143\u7d20\u5728\u5404\u4e2a\u6876\u5185\u5e73\u5747\u5206\u5e03\uff0c\u90a3\u4e48\u6bcf\u4e2a\u6876\u5185\u7684\u5143\u7d20\u6570\u91cf\u4e3a \\(\\frac{n}{k}\\) \u3002\u5047\u8bbe\u6392\u5e8f\u5355\u4e2a\u6876\u4f7f\u7528 \\(O(\\frac{n}{k} \\log\\frac{n}{k})\\) \u65f6\u95f4\uff0c\u5219\u6392\u5e8f\u6240\u6709\u6876\u4f7f\u7528 \\(O(n \\log\\frac{n}{k})\\) \u65f6\u95f4\u3002\u5f53\u6876\u6570\u91cf \\(k\\) \u6bd4\u8f83\u5927\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5219\u8d8b\u5411\u4e8e \\(O(n)\\) \u3002\u5408\u5e76\u7ed3\u679c\u65f6\u9700\u8981\u904d\u5386\u6240\u6709\u6876\u548c\u5143\u7d20\uff0c\u82b1\u8d39 \\(O(n + k)\\) \u65f6\u95f4\u3002\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u6570\u636e\u88ab\u5206\u914d\u5230\u4e00\u4e2a\u6876\u4e2d\uff0c\u4e14\u6392\u5e8f\u8be5\u6876\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002
        • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n + k)\\)\u3001\u975e\u539f\u5730\u6392\u5e8f\uff1a\u9700\u8981\u501f\u52a9 \\(k\\) \u4e2a\u6876\u548c\u603b\u5171 \\(n\\) \u4e2a\u5143\u7d20\u7684\u989d\u5916\u7a7a\u95f4\u3002
        • \u6876\u6392\u5e8f\u662f\u5426\u7a33\u5b9a\u53d6\u51b3\u4e8e\u6392\u5e8f\u6876\u5185\u5143\u7d20\u7684\u7b97\u6cd5\u662f\u5426\u7a33\u5b9a\u3002
        "},{"location":"chapter_sorting/bucket_sort/#1183","title":"11.8.3 \u00a0 \u5982\u4f55\u5b9e\u73b0\u5e73\u5747\u5206\u914d","text":"

        \u6876\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7406\u8bba\u4e0a\u53ef\u4ee5\u8fbe\u5230 \\(O(n)\\) \uff0c\u5173\u952e\u5728\u4e8e\u5c06\u5143\u7d20\u5747\u5300\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\uff0c\u56e0\u4e3a\u5b9e\u9645\u6570\u636e\u5f80\u5f80\u4e0d\u662f\u5747\u5300\u5206\u5e03\u7684\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u8981\u5c06\u6dd8\u5b9d\u4e0a\u7684\u6240\u6709\u5546\u54c1\u6309\u4ef7\u683c\u8303\u56f4\u5e73\u5747\u5206\u914d\u5230 10 \u4e2a\u6876\u4e2d\uff0c\u4f46\u5546\u54c1\u4ef7\u683c\u5206\u5e03\u4e0d\u5747\uff0c\u4f4e\u4e8e 100 \u5143\u7684\u975e\u5e38\u591a\uff0c\u9ad8\u4e8e 1000 \u5143\u7684\u975e\u5e38\u5c11\u3002\u82e5\u5c06\u4ef7\u683c\u533a\u95f4\u5e73\u5747\u5212\u5206\u4e3a 10 \u4e2a\uff0c\u5404\u4e2a\u6876\u4e2d\u7684\u5546\u54c1\u6570\u91cf\u5dee\u8ddd\u4f1a\u975e\u5e38\u5927\u3002

        \u4e3a\u5b9e\u73b0\u5e73\u5747\u5206\u914d\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u8bbe\u5b9a\u4e00\u6761\u5927\u81f4\u7684\u5206\u754c\u7ebf\uff0c\u5c06\u6570\u636e\u7c97\u7565\u5730\u5206\u5230 3 \u4e2a\u6876\u4e2d\u3002\u5206\u914d\u5b8c\u6bd5\u540e\uff0c\u518d\u5c06\u5546\u54c1\u8f83\u591a\u7684\u6876\u7ee7\u7eed\u5212\u5206\u4e3a 3 \u4e2a\u6876\uff0c\u76f4\u81f3\u6240\u6709\u6876\u4e2d\u7684\u5143\u7d20\u6570\u91cf\u5927\u81f4\u76f8\u7b49\u3002

        \u5982\u56fe 11-14 \u6240\u793a\uff0c\u8fd9\u79cd\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u521b\u5efa\u4e00\u68f5\u9012\u5f52\u6811\uff0c\u76ee\u6807\u662f\u8ba9\u53f6\u8282\u70b9\u7684\u503c\u5c3d\u53ef\u80fd\u5e73\u5747\u3002\u5f53\u7136\uff0c\u4e0d\u4e00\u5b9a\u8981\u6bcf\u8f6e\u5c06\u6570\u636e\u5212\u5206\u4e3a 3 \u4e2a\u6876\uff0c\u5177\u4f53\u5212\u5206\u65b9\u5f0f\u53ef\u6839\u636e\u6570\u636e\u7279\u70b9\u7075\u6d3b\u9009\u62e9\u3002

        \u56fe 11-14 \u00a0 \u9012\u5f52\u5212\u5206\u6876

        \u5982\u679c\u6211\u4eec\u63d0\u524d\u77e5\u9053\u5546\u54c1\u4ef7\u683c\u7684\u6982\u7387\u5206\u5e03\uff0c\u5219\u53ef\u4ee5\u6839\u636e\u6570\u636e\u6982\u7387\u5206\u5e03\u8bbe\u7f6e\u6bcf\u4e2a\u6876\u7684\u4ef7\u683c\u5206\u754c\u7ebf\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6570\u636e\u5206\u5e03\u5e76\u4e0d\u4e00\u5b9a\u9700\u8981\u7279\u610f\u7edf\u8ba1\uff0c\u4e5f\u53ef\u4ee5\u6839\u636e\u6570\u636e\u7279\u70b9\u91c7\u7528\u67d0\u79cd\u6982\u7387\u6a21\u578b\u8fdb\u884c\u8fd1\u4f3c\u3002

        \u5982\u56fe 11-15 \u6240\u793a\uff0c\u6211\u4eec\u5047\u8bbe\u5546\u54c1\u4ef7\u683c\u670d\u4ece\u6b63\u6001\u5206\u5e03\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5408\u7406\u5730\u8bbe\u5b9a\u4ef7\u683c\u533a\u95f4\uff0c\u4ece\u800c\u5c06\u5546\u54c1\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\u3002

        \u56fe 11-15 \u00a0 \u6839\u636e\u6982\u7387\u5206\u5e03\u5212\u5206\u6876

        "},{"location":"chapter_sorting/counting_sort/","title":"11.9 \u00a0 \u8ba1\u6570\u6392\u5e8f","text":"

        \u8ba1\u6570\u6392\u5e8f\uff08counting sort\uff09\u901a\u8fc7\u7edf\u8ba1\u5143\u7d20\u6570\u91cf\u6765\u5b9e\u73b0\u6392\u5e8f\uff0c\u901a\u5e38\u5e94\u7528\u4e8e\u6574\u6570\u6570\u7ec4\u3002

        "},{"location":"chapter_sorting/counting_sort/#1191","title":"11.9.1 \u00a0 \u7b80\u5355\u5b9e\u73b0","text":"

        \u5148\u6765\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u3002\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4 nums \uff0c\u5176\u4e2d\u7684\u5143\u7d20\u90fd\u662f\u201c\u975e\u8d1f\u6574\u6570\u201d\uff0c\u8ba1\u6570\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u56fe 11-16 \u6240\u793a\u3002

        1. \u904d\u5386\u6570\u7ec4\uff0c\u627e\u51fa\u5176\u4e2d\u7684\u6700\u5927\u6570\u5b57\uff0c\u8bb0\u4e3a \\(m\\) \uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(m + 1\\) \u7684\u8f85\u52a9\u6570\u7ec4 counter \u3002
        2. \u501f\u52a9 counter \u7edf\u8ba1 nums \u4e2d\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u5176\u4e2d counter[num] \u5bf9\u5e94\u6570\u5b57 num \u7684\u51fa\u73b0\u6b21\u6570\u3002\u7edf\u8ba1\u65b9\u6cd5\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u904d\u5386 nums\uff08\u8bbe\u5f53\u524d\u6570\u5b57\u4e3a num\uff09\uff0c\u6bcf\u8f6e\u5c06 counter[num] \u589e\u52a0 \\(1\\) \u5373\u53ef\u3002
        3. \u7531\u4e8e counter \u7684\u5404\u4e2a\u7d22\u5f15\u5929\u7136\u6709\u5e8f\uff0c\u56e0\u6b64\u76f8\u5f53\u4e8e\u6240\u6709\u6570\u5b57\u5df2\u7ecf\u6392\u5e8f\u597d\u4e86\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u904d\u5386 counter \uff0c\u6839\u636e\u5404\u6570\u5b57\u51fa\u73b0\u6b21\u6570\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u586b\u5165 nums \u5373\u53ef\u3002

        \u56fe 11-16 \u00a0 \u8ba1\u6570\u6392\u5e8f\u6d41\u7a0b

        \u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby counting_sort.py
        def counting_sort_naive(nums: list[int]):\n    \"\"\"\u8ba1\u6570\u6392\u5e8f\"\"\"\n    # \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\n    # 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    m = max(nums)\n    # 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    # counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    counter = [0] * (m + 1)\n    for num in nums:\n        counter[num] += 1\n    # 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n    i = 0\n    for num in range(m + 1):\n        for _ in range(counter[num]):\n            nums[i] = num\n            i += 1\n
        counting_sort.cpp
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(vector<int> &nums) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    int m = 0;\n    for (int num : nums) {\n        m = max(m, num);\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    vector<int> counter(m + 1, 0);\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
        counting_sort.java
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(int[] nums) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    int m = 0;\n    for (int num : nums) {\n        m = Math.max(m, num);\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    int[] counter = new int[m + 1];\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
        counting_sort.cs
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid CountingSortNaive(int[] nums) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    int m = 0;\n    foreach (int num in nums) {\n        m = Math.Max(m, num);\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    int[] counter = new int[m + 1];\n    foreach (int num in nums) {\n        counter[num]++;\n    }\n    // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
        counting_sort.go
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfunc countingSortNaive(nums []int) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    m := 0\n    for _, num := range nums {\n        if num > m {\n            m = num\n        }\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    counter := make([]int, m+1)\n    for _, num := range nums {\n        counter[num]++\n    }\n    // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n    for i, num := 0, 0; num < m+1; num++ {\n        for j := 0; j < counter[num]; j++ {\n            nums[i] = num\n            i++\n        }\n    }\n}\n
        counting_sort.swift
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfunc countingSortNaive(nums: inout [Int]) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    let m = nums.max()!\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    var counter = Array(repeating: 0, count: m + 1)\n    for num in nums {\n        counter[num] += 1\n    }\n    // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n    var i = 0\n    for num in 0 ..< m + 1 {\n        for _ in 0 ..< counter[num] {\n            nums[i] = num\n            i += 1\n        }\n    }\n}\n
        counting_sort.js
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfunction countingSortNaive(nums) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    let m = Math.max(...nums);\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    const counter = new Array(m + 1).fill(0);\n    for (const num of nums) {\n        counter[num]++;\n    }\n    // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n    let i = 0;\n    for (let num = 0; num < m + 1; num++) {\n        for (let j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
        counting_sort.ts
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfunction countingSortNaive(nums: number[]): void {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    let m: number = Math.max(...nums);\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    const counter: number[] = new Array<number>(m + 1).fill(0);\n    for (const num of nums) {\n        counter[num]++;\n    }\n    // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n    let i = 0;\n    for (let num = 0; num < m + 1; num++) {\n        for (let j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
        counting_sort.dart
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(List<int> nums) {\n  // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n  int m = 0;\n  for (int _num in nums) {\n    m = max(m, _num);\n  }\n  // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n  // counter[_num] \u4ee3\u8868 _num \u7684\u51fa\u73b0\u6b21\u6570\n  List<int> counter = List.filled(m + 1, 0);\n  for (int _num in nums) {\n    counter[_num]++;\n  }\n  // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n  int i = 0;\n  for (int _num = 0; _num < m + 1; _num++) {\n    for (int j = 0; j < counter[_num]; j++, i++) {\n      nums[i] = _num;\n    }\n  }\n}\n
        counting_sort.rs
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfn counting_sort_naive(nums: &mut [i32]) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    let m = *nums.iter().max().unwrap();\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    let mut counter = vec![0; m as usize + 1];\n    for &num in nums.iter() {\n        counter[num as usize] += 1;\n    }\n    // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n    let mut i = 0;\n    for num in 0..m + 1 {\n        for _ in 0..counter[num as usize] {\n            nums[i] = num;\n            i += 1;\n        }\n    }\n}\n
        counting_sort.c
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(int nums[], int size) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    int m = 0;\n    for (int i = 0; i < size; i++) {\n        if (nums[i] > m) {\n            m = nums[i];\n        }\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    int *counter = calloc(m + 1, sizeof(int));\n    for (int i = 0; i < size; i++) {\n        counter[nums[i]]++;\n    }\n    // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n    // 4. \u91ca\u653e\u5185\u5b58\n    free(counter);\n}\n
        counting_sort.kt
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfun countingSortNaive(nums: IntArray) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    var m = 0\n    for (num in nums) {\n        m = max(m, num)\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    val counter = IntArray(m + 1)\n    for (num in nums) {\n        counter[num]++\n    }\n    // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n    var i = 0\n    for (num in 0..<m + 1) {\n        var j = 0\n        while (j < counter[num]) {\n            nums[i] = num\n            j++\n            i++\n        }\n    }\n}\n
        counting_sort.rb
        ### \u8ba1\u6570\u6392\u5e8f ###\ndef counting_sort_naive(nums)\n  # \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\n  # 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n  m = 0\n  nums.each { |num| m = [m, num].max }\n  # 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n  # counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n  counter = Array.new(m + 1, 0)\n  nums.each { |num| counter[num] += 1 }\n  # 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n  i = 0\n  for num in 0...(m + 1)\n    (0...counter[num]).each do\n      nums[i] = num\n      i += 1\n    end\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u8ba1\u6570\u6392\u5e8f\u4e0e\u6876\u6392\u5e8f\u7684\u8054\u7cfb

        \u4ece\u6876\u6392\u5e8f\u7684\u89d2\u5ea6\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8ba1\u6570\u6392\u5e8f\u4e2d\u7684\u8ba1\u6570\u6570\u7ec4 counter \u7684\u6bcf\u4e2a\u7d22\u5f15\u89c6\u4e3a\u4e00\u4e2a\u6876\uff0c\u5c06\u7edf\u8ba1\u6570\u91cf\u7684\u8fc7\u7a0b\u770b\u4f5c\u5c06\u5404\u4e2a\u5143\u7d20\u5206\u914d\u5230\u5bf9\u5e94\u7684\u6876\u4e2d\u3002\u672c\u8d28\u4e0a\uff0c\u8ba1\u6570\u6392\u5e8f\u662f\u6876\u6392\u5e8f\u5728\u6574\u578b\u6570\u636e\u4e0b\u7684\u4e00\u4e2a\u7279\u4f8b\u3002

        "},{"location":"chapter_sorting/counting_sort/#1192","title":"11.9.2 \u00a0 \u5b8c\u6574\u5b9e\u73b0","text":"

        \u7ec6\u5fc3\u7684\u8bfb\u8005\u53ef\u80fd\u53d1\u73b0\u4e86\uff0c\u5982\u679c\u8f93\u5165\u6570\u636e\u662f\u5bf9\u8c61\uff0c\u4e0a\u8ff0\u6b65\u9aa4 3. \u5c31\u5931\u6548\u4e86\u3002\u5047\u8bbe\u8f93\u5165\u6570\u636e\u662f\u5546\u54c1\u5bf9\u8c61\uff0c\u6211\u4eec\u60f3\u6309\u7167\u5546\u54c1\u4ef7\u683c\uff08\u7c7b\u7684\u6210\u5458\u53d8\u91cf\uff09\u5bf9\u5546\u54c1\u8fdb\u884c\u6392\u5e8f\uff0c\u800c\u4e0a\u8ff0\u7b97\u6cd5\u53ea\u80fd\u7ed9\u51fa\u4ef7\u683c\u7684\u6392\u5e8f\u7ed3\u679c\u3002

        \u90a3\u4e48\u5982\u4f55\u624d\u80fd\u5f97\u5230\u539f\u6570\u636e\u7684\u6392\u5e8f\u7ed3\u679c\u5462\uff1f\u6211\u4eec\u9996\u5148\u8ba1\u7b97 counter \u7684\u201c\u524d\u7f00\u548c\u201d\u3002\u987e\u540d\u601d\u4e49\uff0c\u7d22\u5f15 i \u5904\u7684\u524d\u7f00\u548c prefix[i] \u7b49\u4e8e\u6570\u7ec4\u524d i \u4e2a\u5143\u7d20\u4e4b\u548c\uff1a

        \\[ \\text{prefix}[i] = \\sum_{j=0}^i \\text{counter[j]} \\]

        \u524d\u7f00\u548c\u5177\u6709\u660e\u786e\u7684\u610f\u4e49\uff0cprefix[num] - 1 \u4ee3\u8868\u5143\u7d20 num \u5728\u7ed3\u679c\u6570\u7ec4 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u3002\u8fd9\u4e2a\u4fe1\u606f\u975e\u5e38\u5173\u952e\uff0c\u56e0\u4e3a\u5b83\u544a\u8bc9\u6211\u4eec\u5404\u4e2a\u5143\u7d20\u5e94\u8be5\u51fa\u73b0\u5728\u7ed3\u679c\u6570\u7ec4\u7684\u54ea\u4e2a\u4f4d\u7f6e\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5012\u5e8f\u904d\u5386\u539f\u6570\u7ec4 nums \u7684\u6bcf\u4e2a\u5143\u7d20 num \uff0c\u5728\u6bcf\u8f6e\u8fed\u4ee3\u4e2d\u6267\u884c\u4ee5\u4e0b\u4e24\u6b65\u3002

        1. \u5c06 num \u586b\u5165\u6570\u7ec4 res \u7684\u7d22\u5f15 prefix[num] - 1 \u5904\u3002
        2. \u4ee4\u524d\u7f00\u548c prefix[num] \u51cf\u5c0f \\(1\\) \uff0c\u4ece\u800c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\u3002

        \u904d\u5386\u5b8c\u6210\u540e\uff0c\u6570\u7ec4 res \u4e2d\u5c31\u662f\u6392\u5e8f\u597d\u7684\u7ed3\u679c\uff0c\u6700\u540e\u4f7f\u7528 res \u8986\u76d6\u539f\u6570\u7ec4 nums \u5373\u53ef\u3002\u56fe 11-17 \u5c55\u793a\u4e86\u5b8c\u6574\u7684\u8ba1\u6570\u6392\u5e8f\u6d41\u7a0b\u3002

        <1><2><3><4><5><6><7><8>

        \u56fe 11-17 \u00a0 \u8ba1\u6570\u6392\u5e8f\u6b65\u9aa4

        \u8ba1\u6570\u6392\u5e8f\u7684\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby counting_sort.py
        def counting_sort(nums: list[int]):\n    \"\"\"\u8ba1\u6570\u6392\u5e8f\"\"\"\n    # \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\n    # 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    m = max(nums)\n    # 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    # counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    counter = [0] * (m + 1)\n    for num in nums:\n        counter[num] += 1\n    # 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n    # \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n    for i in range(m):\n        counter[i + 1] += counter[i]\n    # 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n    # \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n    n = len(nums)\n    res = [0] * n\n    for i in range(n - 1, -1, -1):\n        num = nums[i]\n        res[counter[num] - 1] = num  # \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n        counter[num] -= 1  # \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n    # \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n    for i in range(n):\n        nums[i] = res[i]\n
        counting_sort.cpp
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid countingSort(vector<int> &nums) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    int m = 0;\n    for (int num : nums) {\n        m = max(m, num);\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    vector<int> counter(m + 1, 0);\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n    // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n    // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n    int n = nums.size();\n    vector<int> res(n);\n    for (int i = n - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n        counter[num]--;              // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n    nums = res;\n}\n
        counting_sort.java
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid countingSort(int[] nums) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    int m = 0;\n    for (int num : nums) {\n        m = Math.max(m, num);\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    int[] counter = new int[m + 1];\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n    // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n    // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n    int n = nums.length;\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n        counter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (int i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n
        counting_sort.cs
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid CountingSort(int[] nums) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    int m = 0;\n    foreach (int num in nums) {\n        m = Math.Max(m, num);\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    int[] counter = new int[m + 1];\n    foreach (int num in nums) {\n        counter[num]++;\n    }\n    // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n    // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n    // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n    int n = nums.Length;\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n        counter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (int i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n
        counting_sort.go
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfunc countingSort(nums []int) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    m := 0\n    for _, num := range nums {\n        if num > m {\n            m = num\n        }\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    counter := make([]int, m+1)\n    for _, num := range nums {\n        counter[num]++\n    }\n    // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n    // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n    for i := 0; i < m; i++ {\n        counter[i+1] += counter[i]\n    }\n    // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n    // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n    n := len(nums)\n    res := make([]int, n)\n    for i := n - 1; i >= 0; i-- {\n        num := nums[i]\n        // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n        res[counter[num]-1] = num\n        // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n        counter[num]--\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n    copy(nums, res)\n}\n
        counting_sort.swift
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfunc countingSort(nums: inout [Int]) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    let m = nums.max()!\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    var counter = Array(repeating: 0, count: m + 1)\n    for num in nums {\n        counter[num] += 1\n    }\n    // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n    // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n    for i in 0 ..< m {\n        counter[i + 1] += counter[i]\n    }\n    // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n    // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n    var res = Array(repeating: 0, count: nums.count)\n    for i in nums.indices.reversed() {\n        let num = nums[i]\n        res[counter[num] - 1] = num // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n        counter[num] -= 1 // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n    for i in nums.indices {\n        nums[i] = res[i]\n    }\n}\n
        counting_sort.js
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfunction countingSort(nums) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    let m = Math.max(...nums);\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    const counter = new Array(m + 1).fill(0);\n    for (const num of nums) {\n        counter[num]++;\n    }\n    // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n    // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n    for (let i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n    // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n    const n = nums.length;\n    const res = new Array(n);\n    for (let i = n - 1; i >= 0; i--) {\n        const num = nums[i];\n        res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n        counter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (let i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n
        counting_sort.ts
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfunction countingSort(nums: number[]): void {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    let m: number = Math.max(...nums);\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    const counter: number[] = new Array<number>(m + 1).fill(0);\n    for (const num of nums) {\n        counter[num]++;\n    }\n    // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n    // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n    for (let i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n    // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n    const n = nums.length;\n    const res: number[] = new Array<number>(n);\n    for (let i = n - 1; i >= 0; i--) {\n        const num = nums[i];\n        res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n        counter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (let i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n
        counting_sort.dart
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid countingSort(List<int> nums) {\n  // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n  int m = 0;\n  for (int _num in nums) {\n    m = max(m, _num);\n  }\n  // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n  // counter[_num] \u4ee3\u8868 _num \u7684\u51fa\u73b0\u6b21\u6570\n  List<int> counter = List.filled(m + 1, 0);\n  for (int _num in nums) {\n    counter[_num]++;\n  }\n  // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n  // \u5373 counter[_num]-1 \u662f _num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n  for (int i = 0; i < m; i++) {\n    counter[i + 1] += counter[i];\n  }\n  // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n  // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n  int n = nums.length;\n  List<int> res = List.filled(n, 0);\n  for (int i = n - 1; i >= 0; i--) {\n    int _num = nums[i];\n    res[counter[_num] - 1] = _num; // \u5c06 _num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n    counter[_num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e _num \u7684\u7d22\u5f15\n  }\n  // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n  nums.setAll(0, res);\n}\n
        counting_sort.rs
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfn counting_sort(nums: &mut [i32]) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    let m = *nums.iter().max().unwrap() as usize;\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    let mut counter = vec![0; m + 1];\n    for &num in nums.iter() {\n        counter[num as usize] += 1;\n    }\n    // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n    // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n    for i in 0..m {\n        counter[i + 1] += counter[i];\n    }\n    // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n    // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n    let n = nums.len();\n    let mut res = vec![0; n];\n    for i in (0..n).rev() {\n        let num = nums[i];\n        res[counter[num as usize] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n        counter[num as usize] -= 1; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n    nums.copy_from_slice(&res)\n}\n
        counting_sort.c
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid countingSort(int nums[], int size) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    int m = 0;\n    for (int i = 0; i < size; i++) {\n        if (nums[i] > m) {\n            m = nums[i];\n        }\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    int *counter = calloc(m, sizeof(int));\n    for (int i = 0; i < size; i++) {\n        counter[nums[i]]++;\n    }\n    // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n    // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n    // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n    int *res = malloc(sizeof(int) * size);\n    for (int i = size - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n        counter[num]--;              // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n    memcpy(nums, res, size * sizeof(int));\n    // 5. \u91ca\u653e\u5185\u5b58\n    free(res);\n    free(counter);\n}\n
        counting_sort.kt
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfun countingSort(nums: IntArray) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    var m = 0\n    for (num in nums) {\n        m = max(m, num)\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    val counter = IntArray(m + 1)\n    for (num in nums) {\n        counter[num]++\n    }\n    // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n    // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n    for (i in 0..<m) {\n        counter[i + 1] += counter[i]\n    }\n    // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n    // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n    val n = nums.size\n    val res = IntArray(n)\n    for (i in n - 1 downTo 0) {\n        val num = nums[i]\n        res[counter[num] - 1] = num // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n        counter[num]-- // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (i in 0..<n) {\n        nums[i] = res[i]\n    }\n}\n
        counting_sort.rb
        ### \u8ba1\u6570\u6392\u5e8f ###\ndef counting_sort(nums)\n  # \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\n  # 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n  m = nums.max\n  # 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n  # counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n  counter = Array.new(m + 1, 0)\n  nums.each { |num| counter[num] += 1 }\n  # 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n  # \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n  (0...m).each { |i| counter[i + 1] += counter[i] }\n  # 4. \u5012\u5e8f\u904d\u5386 nums, \u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n  # \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n  n = nums.length\n  res = Array.new(n, 0)\n  (n - 1).downto(0).each do |i|\n    num = nums[i]\n    res[counter[num] - 1] = num # \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n    counter[num] -= 1 # \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n  end\n  # \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n  (0...n).each { |i| nums[i] = res[i] }\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/counting_sort/#1193","title":"11.9.3 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n + m)\\)\u3001\u975e\u81ea\u9002\u5e94\u6392\u5e8f \uff1a\u6d89\u53ca\u904d\u5386 nums \u548c\u904d\u5386 counter \uff0c\u90fd\u4f7f\u7528\u7ebf\u6027\u65f6\u95f4\u3002\u4e00\u822c\u60c5\u51b5\u4e0b \\(n \\gg m\\) \uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u4e8e \\(O(n)\\) \u3002
        • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n + m)\\)\u3001\u975e\u539f\u5730\u6392\u5e8f\uff1a\u501f\u52a9\u4e86\u957f\u5ea6\u5206\u522b\u4e3a \\(n\\) \u548c \\(m\\) \u7684\u6570\u7ec4 res \u548c counter \u3002
        • \u7a33\u5b9a\u6392\u5e8f\uff1a\u7531\u4e8e\u5411 res \u4e2d\u586b\u5145\u5143\u7d20\u7684\u987a\u5e8f\u662f\u201c\u4ece\u53f3\u5411\u5de6\u201d\u7684\uff0c\u56e0\u6b64\u5012\u5e8f\u904d\u5386 nums \u53ef\u4ee5\u907f\u514d\u6539\u53d8\u76f8\u7b49\u5143\u7d20\u4e4b\u95f4\u7684\u76f8\u5bf9\u4f4d\u7f6e\uff0c\u4ece\u800c\u5b9e\u73b0\u7a33\u5b9a\u6392\u5e8f\u3002\u5b9e\u9645\u4e0a\uff0c\u6b63\u5e8f\u904d\u5386 nums \u4e5f\u53ef\u4ee5\u5f97\u5230\u6b63\u786e\u7684\u6392\u5e8f\u7ed3\u679c\uff0c\u4f46\u7ed3\u679c\u662f\u975e\u7a33\u5b9a\u7684\u3002
        "},{"location":"chapter_sorting/counting_sort/#1194","title":"11.9.4 \u00a0 \u5c40\u9650\u6027","text":"

        \u770b\u5230\u8fd9\u91cc\uff0c\u4f60\u4e5f\u8bb8\u4f1a\u89c9\u5f97\u8ba1\u6570\u6392\u5e8f\u975e\u5e38\u5de7\u5999\uff0c\u4ec5\u901a\u8fc7\u7edf\u8ba1\u6570\u91cf\u5c31\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6548\u7684\u6392\u5e8f\u3002\u7136\u800c\uff0c\u4f7f\u7528\u8ba1\u6570\u6392\u5e8f\u7684\u524d\u7f6e\u6761\u4ef6\u76f8\u5bf9\u8f83\u4e3a\u4e25\u683c\u3002

        \u8ba1\u6570\u6392\u5e8f\u53ea\u9002\u7528\u4e8e\u975e\u8d1f\u6574\u6570\u3002\u82e5\u60f3\u5c06\u5176\u7528\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\uff0c\u9700\u8981\u786e\u4fdd\u8fd9\u4e9b\u6570\u636e\u53ef\u4ee5\u8f6c\u6362\u4e3a\u975e\u8d1f\u6574\u6570\uff0c\u5e76\u4e14\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u4e0d\u80fd\u6539\u53d8\u5404\u4e2a\u5143\u7d20\u4e4b\u95f4\u7684\u76f8\u5bf9\u5927\u5c0f\u5173\u7cfb\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5305\u542b\u8d1f\u6570\u7684\u6574\u6570\u6570\u7ec4\uff0c\u53ef\u4ee5\u5148\u7ed9\u6240\u6709\u6570\u5b57\u52a0\u4e0a\u4e00\u4e2a\u5e38\u6570\uff0c\u5c06\u5168\u90e8\u6570\u5b57\u8f6c\u5316\u4e3a\u6b63\u6570\uff0c\u6392\u5e8f\u5b8c\u6210\u540e\u518d\u8f6c\u6362\u56de\u53bb\u3002

        \u8ba1\u6570\u6392\u5e8f\u9002\u7528\u4e8e\u6570\u636e\u91cf\u5927\u4f46\u6570\u636e\u8303\u56f4\u8f83\u5c0f\u7684\u60c5\u51b5\u3002\u6bd4\u5982\uff0c\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d \\(m\\) \u4e0d\u80fd\u592a\u5927\uff0c\u5426\u5219\u4f1a\u5360\u7528\u8fc7\u591a\u7a7a\u95f4\u3002\u800c\u5f53 \\(n \\ll m\\) \u65f6\uff0c\u8ba1\u6570\u6392\u5e8f\u4f7f\u7528 \\(O(m)\\) \u65f6\u95f4\uff0c\u53ef\u80fd\u6bd4 \\(O(n \\log n)\\) \u7684\u6392\u5e8f\u7b97\u6cd5\u8fd8\u8981\u6162\u3002

        "},{"location":"chapter_sorting/heap_sort/","title":"11.7 \u00a0 \u5806\u6392\u5e8f","text":"

        Tip

        \u9605\u8bfb\u672c\u8282\u524d\uff0c\u8bf7\u786e\u4fdd\u5df2\u5b66\u5b8c\u201c\u5806\u201d\u7ae0\u8282\u3002

        \u5806\u6392\u5e8f\uff08heap sort\uff09\u662f\u4e00\u79cd\u57fa\u4e8e\u5806\u6570\u636e\u7ed3\u6784\u5b9e\u73b0\u7684\u9ad8\u6548\u6392\u5e8f\u7b97\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5df2\u7ecf\u5b66\u8fc7\u7684\u201c\u5efa\u5806\u64cd\u4f5c\u201d\u548c\u201c\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\u201d\u5b9e\u73b0\u5806\u6392\u5e8f\u3002

        1. \u8f93\u5165\u6570\u7ec4\u5e76\u5efa\u7acb\u5c0f\u9876\u5806\uff0c\u6b64\u65f6\u6700\u5c0f\u5143\u7d20\u4f4d\u4e8e\u5806\u9876\u3002
        2. \u4e0d\u65ad\u6267\u884c\u51fa\u5806\u64cd\u4f5c\uff0c\u4f9d\u6b21\u8bb0\u5f55\u51fa\u5806\u5143\u7d20\uff0c\u5373\u53ef\u5f97\u5230\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\u7684\u5e8f\u5217\u3002

        \u4ee5\u4e0a\u65b9\u6cd5\u867d\u7136\u53ef\u884c\uff0c\u4f46\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u989d\u5916\u6570\u7ec4\u6765\u4fdd\u5b58\u5f39\u51fa\u7684\u5143\u7d20\uff0c\u6bd4\u8f83\u6d6a\u8d39\u7a7a\u95f4\u3002\u5728\u5b9e\u9645\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u4e00\u79cd\u66f4\u52a0\u4f18\u96c5\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002

        "},{"location":"chapter_sorting/heap_sort/#1171","title":"11.7.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

        \u8bbe\u6570\u7ec4\u7684\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u5806\u6392\u5e8f\u7684\u6d41\u7a0b\u5982\u56fe 11-12 \u6240\u793a\u3002

        1. \u8f93\u5165\u6570\u7ec4\u5e76\u5efa\u7acb\u5927\u9876\u5806\u3002\u5b8c\u6210\u540e\uff0c\u6700\u5927\u5143\u7d20\u4f4d\u4e8e\u5806\u9876\u3002
        2. \u5c06\u5806\u9876\u5143\u7d20\uff08\u7b2c\u4e00\u4e2a\u5143\u7d20\uff09\u4e0e\u5806\u5e95\u5143\u7d20\uff08\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff09\u4ea4\u6362\u3002\u5b8c\u6210\u4ea4\u6362\u540e\uff0c\u5806\u7684\u957f\u5ea6\u51cf \\(1\\) \uff0c\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u52a0 \\(1\\) \u3002
        3. \u4ece\u5806\u9876\u5143\u7d20\u5f00\u59cb\uff0c\u4ece\u9876\u5230\u5e95\u6267\u884c\u5806\u5316\u64cd\u4f5c\uff08sift down\uff09\u3002\u5b8c\u6210\u5806\u5316\u540e\uff0c\u5806\u7684\u6027\u8d28\u5f97\u5230\u4fee\u590d\u3002
        4. \u5faa\u73af\u6267\u884c\u7b2c 2. \u6b65\u548c\u7b2c 3. \u6b65\u3002\u5faa\u73af \\(n - 1\\) \u8f6e\u540e\uff0c\u5373\u53ef\u5b8c\u6210\u6570\u7ec4\u6392\u5e8f\u3002

        Tip

        \u5b9e\u9645\u4e0a\uff0c\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\u4e2d\u4e5f\u5305\u542b\u7b2c 2. \u6b65\u548c\u7b2c 3. \u6b65\uff0c\u53ea\u662f\u591a\u4e86\u4e00\u4e2a\u5f39\u51fa\u5143\u7d20\u7684\u6b65\u9aa4\u3002

        <1><2><3><4><5><6><7><8><9><10><11><12>

        \u56fe 11-12 \u00a0 \u5806\u6392\u5e8f\u6b65\u9aa4

        \u5728\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e0e\u201c\u5806\u201d\u7ae0\u8282\u76f8\u540c\u7684\u4ece\u9876\u81f3\u5e95\u5806\u5316 sift_down() \u51fd\u6570\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e\u5806\u7684\u957f\u5ea6\u4f1a\u968f\u7740\u63d0\u53d6\u6700\u5927\u5143\u7d20\u800c\u51cf\u5c0f\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u7ed9 sift_down() \u51fd\u6570\u6dfb\u52a0\u4e00\u4e2a\u957f\u5ea6\u53c2\u6570 \\(n\\) \uff0c\u7528\u4e8e\u6307\u5b9a\u5806\u7684\u5f53\u524d\u6709\u6548\u957f\u5ea6\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby heap_sort.py
        def sift_down(nums: list[int], n: int, i: int):\n    \"\"\"\u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\"\"\"\n    while True:\n        # \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        l = 2 * i + 1\n        r = 2 * i + 2\n        ma = i\n        if l < n and nums[l] > nums[ma]:\n            ma = l\n        if r < n and nums[r] > nums[ma]:\n            ma = r\n        # \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if ma == i:\n            break\n        # \u4ea4\u6362\u4e24\u8282\u70b9\n        nums[i], nums[ma] = nums[ma], nums[i]\n        # \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma\n\ndef heap_sort(nums: list[int]):\n    \"\"\"\u5806\u6392\u5e8f\"\"\"\n    # \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for i in range(len(nums) // 2 - 1, -1, -1):\n        sift_down(nums, len(nums), i)\n    # \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n    for i in range(len(nums) - 1, 0, -1):\n        # \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        nums[0], nums[i] = nums[i], nums[0]\n        # \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n        sift_down(nums, i, 0)\n
        heap_sort.cpp
        /* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(vector<int> &nums, int n, int i) {\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma == i) {\n            break;\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        swap(nums[i], nums[ma]);\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n\n/* \u5806\u6392\u5e8f */\nvoid heapSort(vector<int> &nums) {\n    // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for (int i = nums.size() / 2 - 1; i >= 0; --i) {\n        siftDown(nums, nums.size(), i);\n    }\n    // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n    for (int i = nums.size() - 1; i > 0; --i) {\n        // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        swap(nums[0], nums[i]);\n        // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n        siftDown(nums, i, 0);\n    }\n}\n
        heap_sort.java
        /* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int[] nums, int n, int i) {\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma == i)\n            break;\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        int temp = nums[i];\n        nums[i] = nums[ma];\n        nums[ma] = temp;\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n\n/* \u5806\u6392\u5e8f */\nvoid heapSort(int[] nums) {\n    // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for (int i = nums.length / 2 - 1; i >= 0; i--) {\n        siftDown(nums, nums.length, i);\n    }\n    // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n    for (int i = nums.length - 1; i > 0; i--) {\n        // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        int tmp = nums[0];\n        nums[0] = nums[i];\n        nums[i] = tmp;\n        // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n        siftDown(nums, i, 0);\n    }\n}\n
        heap_sort.cs
        /* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid SiftDown(int[] nums, int n, int i) {\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma == i)\n            break;\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        (nums[ma], nums[i]) = (nums[i], nums[ma]);\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n\n/* \u5806\u6392\u5e8f */\nvoid HeapSort(int[] nums) {\n    // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for (int i = nums.Length / 2 - 1; i >= 0; i--) {\n        SiftDown(nums, nums.Length, i);\n    }\n    // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n    for (int i = nums.Length - 1; i > 0; i--) {\n        // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        (nums[i], nums[0]) = (nums[0], nums[i]);\n        // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n        SiftDown(nums, i, 0);\n    }\n}\n
        heap_sort.go
        /* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc siftDown(nums *[]int, n, i int) {\n    for true {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        l := 2*i + 1\n        r := 2*i + 2\n        ma := i\n        if l < n && (*nums)[l] > (*nums)[ma] {\n            ma = l\n        }\n        if r < n && (*nums)[r] > (*nums)[ma] {\n            ma = r\n        }\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if ma == i {\n            break\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        (*nums)[i], (*nums)[ma] = (*nums)[ma], (*nums)[i]\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma\n    }\n}\n\n/* \u5806\u6392\u5e8f */\nfunc heapSort(nums *[]int) {\n    // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for i := len(*nums)/2 - 1; i >= 0; i-- {\n        siftDown(nums, len(*nums), i)\n    }\n    // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n    for i := len(*nums) - 1; i > 0; i-- {\n        // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        (*nums)[0], (*nums)[i] = (*nums)[i], (*nums)[0]\n        // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n        siftDown(nums, i, 0)\n    }\n}\n
        heap_sort.swift
        /* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc siftDown(nums: inout [Int], n: Int, i: Int) {\n    var i = i\n    while true {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        let l = 2 * i + 1\n        let r = 2 * i + 2\n        var ma = i\n        if l < n, nums[l] > nums[ma] {\n            ma = l\n        }\n        if r < n, nums[r] > nums[ma] {\n            ma = r\n        }\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if ma == i {\n            break\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        nums.swapAt(i, ma)\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma\n    }\n}\n\n/* \u5806\u6392\u5e8f */\nfunc heapSort(nums: inout [Int]) {\n    // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for i in stride(from: nums.count / 2 - 1, through: 0, by: -1) {\n        siftDown(nums: &nums, n: nums.count, i: i)\n    }\n    // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n    for i in nums.indices.dropFirst().reversed() {\n        // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        nums.swapAt(0, i)\n        // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n        siftDown(nums: &nums, n: i, i: 0)\n    }\n}\n
        heap_sort.js
        /* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunction siftDown(nums, n, i) {\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        let l = 2 * i + 1;\n        let r = 2 * i + 2;\n        let ma = i;\n        if (l < n && nums[l] > nums[ma]) {\n            ma = l;\n        }\n        if (r < n && nums[r] > nums[ma]) {\n            ma = r;\n        }\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma === i) {\n            break;\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        [nums[i], nums[ma]] = [nums[ma], nums[i]];\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n\n/* \u5806\u6392\u5e8f */\nfunction heapSort(nums) {\n    // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for (let i = Math.floor(nums.length / 2) - 1; i >= 0; i--) {\n        siftDown(nums, nums.length, i);\n    }\n    // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n    for (let i = nums.length - 1; i > 0; i--) {\n        // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        [nums[0], nums[i]] = [nums[i], nums[0]];\n        // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n        siftDown(nums, i, 0);\n    }\n}\n
        heap_sort.ts
        /* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunction siftDown(nums: number[], n: number, i: number): void {\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        let l = 2 * i + 1;\n        let r = 2 * i + 2;\n        let ma = i;\n        if (l < n && nums[l] > nums[ma]) {\n            ma = l;\n        }\n        if (r < n && nums[r] > nums[ma]) {\n            ma = r;\n        }\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma === i) {\n            break;\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        [nums[i], nums[ma]] = [nums[ma], nums[i]];\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n\n/* \u5806\u6392\u5e8f */\nfunction heapSort(nums: number[]): void {\n    // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for (let i = Math.floor(nums.length / 2) - 1; i >= 0; i--) {\n        siftDown(nums, nums.length, i);\n    }\n    // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n    for (let i = nums.length - 1; i > 0; i--) {\n        // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        [nums[0], nums[i]] = [nums[i], nums[0]];\n        // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n        siftDown(nums, i, 0);\n    }\n}\n
        heap_sort.dart
        /* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(List<int> nums, int n, int i) {\n  while (true) {\n    // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n    int l = 2 * i + 1;\n    int r = 2 * i + 2;\n    int ma = i;\n    if (l < n && nums[l] > nums[ma]) ma = l;\n    if (r < n && nums[r] > nums[ma]) ma = r;\n    // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n    if (ma == i) break;\n    // \u4ea4\u6362\u4e24\u8282\u70b9\n    int temp = nums[i];\n    nums[i] = nums[ma];\n    nums[ma] = temp;\n    // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n    i = ma;\n  }\n}\n\n/* \u5806\u6392\u5e8f */\nvoid heapSort(List<int> nums) {\n  // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n  for (int i = nums.length ~/ 2 - 1; i >= 0; i--) {\n    siftDown(nums, nums.length, i);\n  }\n  // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n  for (int i = nums.length - 1; i > 0; i--) {\n    // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    int tmp = nums[0];\n    nums[0] = nums[i];\n    nums[i] = tmp;\n    // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n    siftDown(nums, i, 0);\n  }\n}\n
        heap_sort.rs
        /* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfn sift_down(nums: &mut [i32], n: usize, mut i: usize) {\n    loop {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        let l = 2 * i + 1;\n        let r = 2 * i + 2;\n        let mut ma = i;\n        if l < n && nums[l] > nums[ma] {\n            ma = l;\n        }\n        if r < n && nums[r] > nums[ma] {\n            ma = r;\n        }\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if ma == i {\n            break;\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        nums.swap(i, ma);\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n\n/* \u5806\u6392\u5e8f */\nfn heap_sort(nums: &mut [i32]) {\n    // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for i in (0..nums.len() / 2).rev() {\n        sift_down(nums, nums.len(), i);\n    }\n    // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n    for i in (1..nums.len()).rev() {\n        // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        nums.swap(0, i);\n        // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n        sift_down(nums, i, 0);\n    }\n}\n
        heap_sort.c
        /* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int nums[], int n, int i) {\n    while (1) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma == i) {\n            break;\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        int temp = nums[i];\n        nums[i] = nums[ma];\n        nums[ma] = temp;\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n\n/* \u5806\u6392\u5e8f */\nvoid heapSort(int nums[], int n) {\n    // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for (int i = n / 2 - 1; i >= 0; --i) {\n        siftDown(nums, n, i);\n    }\n    // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n    for (int i = n - 1; i > 0; --i) {\n        // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        int tmp = nums[0];\n        nums[0] = nums[i];\n        nums[i] = tmp;\n        // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n        siftDown(nums, i, 0);\n    }\n}\n
        heap_sort.kt
        /* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfun siftDown(nums: IntArray, n: Int, li: Int) {\n    var i = li\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        val l = 2 * i + 1\n        val r = 2 * i + 2\n        var ma = i\n        if (l < n && nums[l] > nums[ma]) \n            ma = l\n        if (r < n && nums[r] > nums[ma]) \n            ma = r\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma == i) \n            break\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        val temp = nums[i]\n        nums[i] = nums[ma]\n        nums[ma] = temp\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma\n    }\n}\n\n/* \u5806\u6392\u5e8f */\nfun heapSort(nums: IntArray) {\n    // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for (i in nums.size / 2 - 1 downTo 0) {\n        siftDown(nums, nums.size, i)\n    }\n    // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n    for (i in nums.size - 1 downTo 1) {\n        // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        val temp = nums[0]\n        nums[0] = nums[i]\n        nums[i] = temp\n        // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n        siftDown(nums, i, 0)\n    }\n}\n
        heap_sort.rb
        ### \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 ###\ndef sift_down(nums, n, i)\n  while true\n    # \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n    l = 2 * i + 1\n    r = 2 * i + 2\n    ma = i\n    ma = l if l < n && nums[l] > nums[ma]\n    ma = r if r < n && nums[r] > nums[ma]\n    # \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n    break if ma == i\n    # \u4ea4\u6362\u4e24\u8282\u70b9\n    nums[i], nums[ma] = nums[ma], nums[i]\n    # \u5faa\u73af\u5411\u4e0b\u5806\u5316\n    i = ma\n  end\nend\n\n### \u5806\u6392\u5e8f ###\ndef heap_sort(nums)\n  # \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n  (nums.length / 2 - 1).downto(0) do |i|\n    sift_down(nums, nums.length, i)\n  end\n  # \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n  (nums.length - 1).downto(1) do |i|\n    # \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    nums[0], nums[i] = nums[i], nums[0]\n    # \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n    sift_down(nums, i, 0)\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/heap_sort/#1172","title":"11.7.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\)\u3001\u975e\u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u5efa\u5806\u64cd\u4f5c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002\u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \uff0c\u5171\u5faa\u73af \\(n - 1\\) \u8f6e\u3002
        • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\)\u3001\u539f\u5730\u6392\u5e8f\uff1a\u51e0\u4e2a\u6307\u9488\u53d8\u91cf\u4f7f\u7528 \\(O(1)\\) \u7a7a\u95f4\u3002\u5143\u7d20\u4ea4\u6362\u548c\u5806\u5316\u64cd\u4f5c\u90fd\u662f\u5728\u539f\u6570\u7ec4\u4e0a\u8fdb\u884c\u7684\u3002
        • \u975e\u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u4ea4\u6362\u5806\u9876\u5143\u7d20\u548c\u5806\u5e95\u5143\u7d20\u65f6\uff0c\u76f8\u7b49\u5143\u7d20\u7684\u76f8\u5bf9\u4f4d\u7f6e\u53ef\u80fd\u53d1\u751f\u53d8\u5316\u3002
        "},{"location":"chapter_sorting/insertion_sort/","title":"11.4 \u00a0 \u63d2\u5165\u6392\u5e8f","text":"

        \u63d2\u5165\u6392\u5e8f\uff08insertion sort\uff09\u662f\u4e00\u79cd\u7b80\u5355\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5b83\u7684\u5de5\u4f5c\u539f\u7406\u4e0e\u624b\u52a8\u6574\u7406\u4e00\u526f\u724c\u7684\u8fc7\u7a0b\u975e\u5e38\u76f8\u4f3c\u3002

        \u5177\u4f53\u6765\u8bf4\uff0c\u6211\u4eec\u5728\u672a\u6392\u5e8f\u533a\u95f4\u9009\u62e9\u4e00\u4e2a\u57fa\u51c6\u5143\u7d20\uff0c\u5c06\u8be5\u5143\u7d20\u4e0e\u5176\u5de6\u4fa7\u5df2\u6392\u5e8f\u533a\u95f4\u7684\u5143\u7d20\u9010\u4e00\u6bd4\u8f83\u5927\u5c0f\uff0c\u5e76\u5c06\u8be5\u5143\u7d20\u63d2\u5165\u5230\u6b63\u786e\u7684\u4f4d\u7f6e\u3002

        \u56fe 11-6 \u5c55\u793a\u4e86\u6570\u7ec4\u63d2\u5165\u5143\u7d20\u7684\u64cd\u4f5c\u6d41\u7a0b\u3002\u8bbe\u57fa\u51c6\u5143\u7d20\u4e3a base \uff0c\u6211\u4eec\u9700\u8981\u5c06\u4ece\u76ee\u6807\u7d22\u5f15\u5230 base \u4e4b\u95f4\u7684\u6240\u6709\u5143\u7d20\u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\uff0c\u7136\u540e\u5c06 base \u8d4b\u503c\u7ed9\u76ee\u6807\u7d22\u5f15\u3002

        \u56fe 11-6 \u00a0 \u5355\u6b21\u63d2\u5165\u64cd\u4f5c

        "},{"location":"chapter_sorting/insertion_sort/#1141","title":"11.4.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

        \u63d2\u5165\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u56fe 11-7 \u6240\u793a\u3002

        1. \u521d\u59cb\u72b6\u6001\u4e0b\uff0c\u6570\u7ec4\u7684\u7b2c 1 \u4e2a\u5143\u7d20\u5df2\u5b8c\u6210\u6392\u5e8f\u3002
        2. \u9009\u53d6\u6570\u7ec4\u7684\u7b2c 2 \u4e2a\u5143\u7d20\u4f5c\u4e3a base \uff0c\u5c06\u5176\u63d2\u5165\u5230\u6b63\u786e\u4f4d\u7f6e\u540e\uff0c\u6570\u7ec4\u7684\u524d 2 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002
        3. \u9009\u53d6\u7b2c 3 \u4e2a\u5143\u7d20\u4f5c\u4e3a base \uff0c\u5c06\u5176\u63d2\u5165\u5230\u6b63\u786e\u4f4d\u7f6e\u540e\uff0c\u6570\u7ec4\u7684\u524d 3 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002
        4. \u4ee5\u6b64\u7c7b\u63a8\uff0c\u5728\u6700\u540e\u4e00\u8f6e\u4e2d\uff0c\u9009\u53d6\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a base \uff0c\u5c06\u5176\u63d2\u5165\u5230\u6b63\u786e\u4f4d\u7f6e\u540e\uff0c\u6240\u6709\u5143\u7d20\u5747\u5df2\u6392\u5e8f\u3002

        \u56fe 11-7 \u00a0 \u63d2\u5165\u6392\u5e8f\u6d41\u7a0b

        \u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby insertion_sort.py
        def insertion_sort(nums: list[int]):\n    \"\"\"\u63d2\u5165\u6392\u5e8f\"\"\"\n    # \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n    for i in range(1, len(nums)):\n        base = nums[i]\n        j = i - 1\n        # \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n        while j >= 0 and nums[j] > base:\n            nums[j + 1] = nums[j]  # \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n            j -= 1\n        nums[j + 1] = base  # \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n
        insertion_sort.cpp
        /* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(vector<int> &nums) {\n    // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n    for (int i = 1; i < nums.size(); i++) {\n        int base = nums[i], j = i - 1;\n        // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n            j--;\n        }\n        nums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n    }\n}\n
        insertion_sort.java
        /* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(int[] nums) {\n    // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n    for (int i = 1; i < nums.length; i++) {\n        int base = nums[i], j = i - 1;\n        // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n            j--;\n        }\n        nums[j + 1] = base;        // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n    }\n}\n
        insertion_sort.cs
        /* \u63d2\u5165\u6392\u5e8f */\nvoid InsertionSort(int[] nums) {\n    // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n    for (int i = 1; i < nums.Length; i++) {\n        int bas = nums[i], j = i - 1;\n        // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n        while (j >= 0 && nums[j] > bas) {\n            nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n            j--;\n        }\n        nums[j + 1] = bas;         // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n    }\n}\n
        insertion_sort.go
        /* \u63d2\u5165\u6392\u5e8f */\nfunc insertionSort(nums []int) {\n    // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n    for i := 1; i < len(nums); i++ {\n        base := nums[i]\n        j := i - 1\n        // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n        for j >= 0 && nums[j] > base {\n            nums[j+1] = nums[j] // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n            j--\n        }\n        nums[j+1] = base // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n    }\n}\n
        insertion_sort.swift
        /* \u63d2\u5165\u6392\u5e8f */\nfunc insertionSort(nums: inout [Int]) {\n    // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n    for i in nums.indices.dropFirst() {\n        let base = nums[i]\n        var j = i - 1\n        // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n        while j >= 0, nums[j] > base {\n            nums[j + 1] = nums[j] // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n            j -= 1\n        }\n        nums[j + 1] = base // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n    }\n}\n
        insertion_sort.js
        /* \u63d2\u5165\u6392\u5e8f */\nfunction insertionSort(nums) {\n    // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n    for (let i = 1; i < nums.length; i++) {\n        let base = nums[i],\n            j = i - 1;\n        // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n            j--;\n        }\n        nums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n    }\n}\n
        insertion_sort.ts
        /* \u63d2\u5165\u6392\u5e8f */\nfunction insertionSort(nums: number[]): void {\n    // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n    for (let i = 1; i < nums.length; i++) {\n        const base = nums[i];\n        let j = i - 1;\n        // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n            j--;\n        }\n        nums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n    }\n}\n
        insertion_sort.dart
        /* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(List<int> nums) {\n  // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n  for (int i = 1; i < nums.length; i++) {\n    int base = nums[i], j = i - 1;\n    // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n    while (j >= 0 && nums[j] > base) {\n      nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n      j--;\n    }\n    nums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n  }\n}\n
        insertion_sort.rs
        /* \u63d2\u5165\u6392\u5e8f */\nfn insertion_sort(nums: &mut [i32]) {\n    // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n    for i in 1..nums.len() {\n        let (base, mut j) = (nums[i], (i - 1) as i32);\n        // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n        while j >= 0 && nums[j as usize] > base {\n            nums[(j + 1) as usize] = nums[j as usize]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n            j -= 1;\n        }\n        nums[(j + 1) as usize] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n    }\n}\n
        insertion_sort.c
        /* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(int nums[], int size) {\n    // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n    for (int i = 1; i < size; i++) {\n        int base = nums[i], j = i - 1;\n        // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n        while (j >= 0 && nums[j] > base) {\n            // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n            nums[j + 1] = nums[j];\n            j--;\n        }\n        // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n        nums[j + 1] = base;\n    }\n}\n
        insertion_sort.kt
        /* \u63d2\u5165\u6392\u5e8f */\nfun insertionSort(nums: IntArray) {\n    //\u5916\u5faa\u73af: \u5df2\u6392\u5e8f\u5143\u7d20\u4e3a 1, 2, ..., n\n    for (i in nums.indices) {\n        val base = nums[i]\n        var j = i - 1\n        // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j] // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n            j--\n        }\n        nums[j + 1] = base        // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n    }\n}\n
        insertion_sort.rb
        ### \u63d2\u5165\u6392\u5e8f ###\ndef insertion_sort(nums)\n  n = nums.length\n  # \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n  for i in 1...n\n    base = nums[i]\n    j = i - 1\n    # \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n    while j >= 0 && nums[j] > base\n      nums[j + 1] = nums[j] # \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n      j -= 1\n    end\n    nums[j + 1] = base # \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/insertion_sort/#1142","title":"11.4.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\)\u3001\u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u6bcf\u6b21\u63d2\u5165\u64cd\u4f5c\u5206\u522b\u9700\u8981\u5faa\u73af \\(n - 1\\)\u3001\\(n-2\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u6b21\uff0c\u6c42\u548c\u5f97\u5230 \\((n - 1) n / 2\\) \uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002\u5728\u9047\u5230\u6709\u5e8f\u6570\u636e\u65f6\uff0c\u63d2\u5165\u64cd\u4f5c\u4f1a\u63d0\u524d\u7ec8\u6b62\u3002\u5f53\u8f93\u5165\u6570\u7ec4\u5b8c\u5168\u6709\u5e8f\u65f6\uff0c\u63d2\u5165\u6392\u5e8f\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3002
        • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\)\u3001\u539f\u5730\u6392\u5e8f\uff1a\u6307\u9488 \\(i\\) \u548c \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002
        • \u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u63d2\u5165\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f1a\u5c06\u5143\u7d20\u63d2\u5165\u5230\u76f8\u7b49\u5143\u7d20\u7684\u53f3\u4fa7\uff0c\u4e0d\u4f1a\u6539\u53d8\u5b83\u4eec\u7684\u987a\u5e8f\u3002
        "},{"location":"chapter_sorting/insertion_sort/#1143","title":"11.4.3 \u00a0 \u63d2\u5165\u6392\u5e8f\u7684\u4f18\u52bf","text":"

        \u63d2\u5165\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u800c\u6211\u4eec\u5373\u5c06\u5b66\u4e60\u7684\u5feb\u901f\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002\u5c3d\u7ba1\u63d2\u5165\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u66f4\u9ad8\uff0c\u4f46\u5728\u6570\u636e\u91cf\u8f83\u5c0f\u7684\u60c5\u51b5\u4e0b\uff0c\u63d2\u5165\u6392\u5e8f\u901a\u5e38\u66f4\u5feb\u3002

        \u8fd9\u4e2a\u7ed3\u8bba\u4e0e\u7ebf\u6027\u67e5\u627e\u548c\u4e8c\u5206\u67e5\u627e\u7684\u9002\u7528\u60c5\u51b5\u7684\u7ed3\u8bba\u7c7b\u4f3c\u3002\u5feb\u901f\u6392\u5e8f\u8fd9\u7c7b \\(O(n \\log n)\\) \u7684\u7b97\u6cd5\u5c5e\u4e8e\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5f80\u5f80\u5305\u542b\u66f4\u591a\u5355\u5143\u8ba1\u7b97\u64cd\u4f5c\u3002\u800c\u5728\u6570\u636e\u91cf\u8f83\u5c0f\u65f6\uff0c\\(n^2\\) \u548c \\(n \\log n\\) \u7684\u6570\u503c\u6bd4\u8f83\u63a5\u8fd1\uff0c\u590d\u6742\u5ea6\u4e0d\u5360\u4e3b\u5bfc\u5730\u4f4d\uff0c\u6bcf\u8f6e\u4e2d\u7684\u5355\u5143\u64cd\u4f5c\u6570\u91cf\u8d77\u5230\u51b3\u5b9a\u6027\u4f5c\u7528\u3002

        \u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\uff08\u4f8b\u5982 Java\uff09\u7684\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\u91c7\u7528\u4e86\u63d2\u5165\u6392\u5e8f\uff0c\u5927\u81f4\u601d\u8def\u4e3a\uff1a\u5bf9\u4e8e\u957f\u6570\u7ec4\uff0c\u91c7\u7528\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u4f8b\u5982\u5feb\u901f\u6392\u5e8f\uff1b\u5bf9\u4e8e\u77ed\u6570\u7ec4\uff0c\u76f4\u63a5\u4f7f\u7528\u63d2\u5165\u6392\u5e8f\u3002

        \u867d\u7136\u5192\u6ce1\u6392\u5e8f\u3001\u9009\u62e9\u6392\u5e8f\u548c\u63d2\u5165\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n^2)\\) \uff0c\u4f46\u5728\u5b9e\u9645\u60c5\u51b5\u4e2d\uff0c\u63d2\u5165\u6392\u5e8f\u7684\u4f7f\u7528\u9891\u7387\u663e\u8457\u9ad8\u4e8e\u5192\u6ce1\u6392\u5e8f\u548c\u9009\u62e9\u6392\u5e8f\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002

        • \u5192\u6ce1\u6392\u5e8f\u57fa\u4e8e\u5143\u7d20\u4ea4\u6362\u5b9e\u73b0\uff0c\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\uff0c\u5171\u6d89\u53ca 3 \u4e2a\u5355\u5143\u64cd\u4f5c\uff1b\u63d2\u5165\u6392\u5e8f\u57fa\u4e8e\u5143\u7d20\u8d4b\u503c\u5b9e\u73b0\uff0c\u4ec5\u9700 1 \u4e2a\u5355\u5143\u64cd\u4f5c\u3002\u56e0\u6b64\uff0c\u5192\u6ce1\u6392\u5e8f\u7684\u8ba1\u7b97\u5f00\u9500\u901a\u5e38\u6bd4\u63d2\u5165\u6392\u5e8f\u66f4\u9ad8\u3002
        • \u9009\u62e9\u6392\u5e8f\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n^2)\\) \u3002\u5982\u679c\u7ed9\u5b9a\u4e00\u7ec4\u90e8\u5206\u6709\u5e8f\u7684\u6570\u636e\uff0c\u63d2\u5165\u6392\u5e8f\u901a\u5e38\u6bd4\u9009\u62e9\u6392\u5e8f\u6548\u7387\u66f4\u9ad8\u3002
        • \u9009\u62e9\u6392\u5e8f\u4e0d\u7a33\u5b9a\uff0c\u65e0\u6cd5\u5e94\u7528\u4e8e\u591a\u7ea7\u6392\u5e8f\u3002
        "},{"location":"chapter_sorting/merge_sort/","title":"11.6 \u00a0 \u5f52\u5e76\u6392\u5e8f","text":"

        \u5f52\u5e76\u6392\u5e8f\uff08merge sort\uff09\u662f\u4e00\u79cd\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5305\u542b\u56fe 11-10 \u6240\u793a\u7684\u201c\u5212\u5206\u201d\u548c\u201c\u5408\u5e76\u201d\u9636\u6bb5\u3002

        1. \u5212\u5206\u9636\u6bb5\uff1a\u901a\u8fc7\u9012\u5f52\u4e0d\u65ad\u5730\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5904\u5206\u5f00\uff0c\u5c06\u957f\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u8f6c\u6362\u4e3a\u77ed\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u3002
        2. \u5408\u5e76\u9636\u6bb5\uff1a\u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u5212\u5206\uff0c\u5f00\u59cb\u5408\u5e76\uff0c\u6301\u7eed\u5730\u5c06\u5de6\u53f3\u4e24\u4e2a\u8f83\u77ed\u7684\u6709\u5e8f\u6570\u7ec4\u5408\u5e76\u4e3a\u4e00\u4e2a\u8f83\u957f\u7684\u6709\u5e8f\u6570\u7ec4\uff0c\u76f4\u81f3\u7ed3\u675f\u3002

        \u56fe 11-10 \u00a0 \u5f52\u5e76\u6392\u5e8f\u7684\u5212\u5206\u4e0e\u5408\u5e76\u9636\u6bb5

        "},{"location":"chapter_sorting/merge_sort/#1161","title":"11.6.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

        \u5982\u56fe 11-11 \u6240\u793a\uff0c\u201c\u5212\u5206\u9636\u6bb5\u201d\u4ece\u9876\u81f3\u5e95\u9012\u5f52\u5730\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5207\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\u3002

        1. \u8ba1\u7b97\u6570\u7ec4\u4e2d\u70b9 mid \uff0c\u9012\u5f52\u5212\u5206\u5de6\u5b50\u6570\u7ec4\uff08\u533a\u95f4 [left, mid] \uff09\u548c\u53f3\u5b50\u6570\u7ec4\uff08\u533a\u95f4 [mid + 1, right] \uff09\u3002
        2. \u9012\u5f52\u6267\u884c\u6b65\u9aa4 1. \uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u533a\u95f4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u3002

        \u201c\u5408\u5e76\u9636\u6bb5\u201d\u4ece\u5e95\u81f3\u9876\u5730\u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u5408\u5e76\u4e3a\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4ece\u957f\u5ea6\u4e3a 1 \u7684\u5b50\u6570\u7ec4\u5f00\u59cb\u5408\u5e76\uff0c\u5408\u5e76\u9636\u6bb5\u4e2d\u7684\u6bcf\u4e2a\u5b50\u6570\u7ec4\u90fd\u662f\u6709\u5e8f\u7684\u3002

        <1><2><3><4><5><6><7><8><9><10>

        \u56fe 11-11 \u00a0 \u5f52\u5e76\u6392\u5e8f\u6b65\u9aa4

        \u89c2\u5bdf\u53d1\u73b0\uff0c\u5f52\u5e76\u6392\u5e8f\u4e0e\u4e8c\u53c9\u6811\u540e\u5e8f\u904d\u5386\u7684\u9012\u5f52\u987a\u5e8f\u662f\u4e00\u81f4\u7684\u3002

        • \u540e\u5e8f\u904d\u5386\uff1a\u5148\u9012\u5f52\u5de6\u5b50\u6811\uff0c\u518d\u9012\u5f52\u53f3\u5b50\u6811\uff0c\u6700\u540e\u5904\u7406\u6839\u8282\u70b9\u3002
        • \u5f52\u5e76\u6392\u5e8f\uff1a\u5148\u9012\u5f52\u5de6\u5b50\u6570\u7ec4\uff0c\u518d\u9012\u5f52\u53f3\u5b50\u6570\u7ec4\uff0c\u6700\u540e\u5904\u7406\u5408\u5e76\u3002

        \u5f52\u5e76\u6392\u5e8f\u7684\u5b9e\u73b0\u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\u3002\u8bf7\u6ce8\u610f\uff0cnums \u7684\u5f85\u5408\u5e76\u533a\u95f4\u4e3a [left, right] \uff0c\u800c tmp \u7684\u5bf9\u5e94\u533a\u95f4\u4e3a [0, right - left] \u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby merge_sort.py
        def merge(nums: list[int], left: int, mid: int, right: int):\n    \"\"\"\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\"\"\"\n    # \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n    # \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n    tmp = [0] * (right - left + 1)\n    # \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n    i, j, k = left, mid + 1, 0\n    # \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while i <= mid and j <= right:\n        if nums[i] <= nums[j]:\n            tmp[k] = nums[i]\n            i += 1\n        else:\n            tmp[k] = nums[j]\n            j += 1\n        k += 1\n    # \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while i <= mid:\n        tmp[k] = nums[i]\n        i += 1\n        k += 1\n    while j <= right:\n        tmp[k] = nums[j]\n        j += 1\n        k += 1\n    # \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n    for k in range(0, len(tmp)):\n        nums[left + k] = tmp[k]\n\ndef merge_sort(nums: list[int], left: int, right: int):\n    \"\"\"\u5f52\u5e76\u6392\u5e8f\"\"\"\n    # \u7ec8\u6b62\u6761\u4ef6\n    if left >= right:\n        return  # \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    # \u5212\u5206\u9636\u6bb5\n    mid = (left + right) // 2 # \u8ba1\u7b97\u4e2d\u70b9\n    merge_sort(nums, left, mid)  # \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n    merge_sort(nums, mid + 1, right)  # \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n    # \u5408\u5e76\u9636\u6bb5\n    merge(nums, left, mid, right)\n
        merge_sort.cpp
        /* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nvoid merge(vector<int> &nums, int left, int mid, int right) {\n    // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n    // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n    vector<int> tmp(right - left + 1);\n    // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n    int i = left, j = mid + 1, k = 0;\n    // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++];\n        else\n            tmp[k++] = nums[j++];\n    }\n    // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n    for (k = 0; k < tmp.size(); k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(vector<int> &nums, int left, int right) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (left >= right)\n        return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    // \u5212\u5206\u9636\u6bb5\n    int mid = left + (right - left) / 2;    // \u8ba1\u7b97\u4e2d\u70b9\n    mergeSort(nums, left, mid);      // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n    mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n    // \u5408\u5e76\u9636\u6bb5\n    merge(nums, left, mid, right);\n}\n
        merge_sort.java
        /* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nvoid merge(int[] nums, int left, int mid, int right) {\n    // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n    // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n    int[] tmp = new int[right - left + 1];\n    // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n    int i = left, j = mid + 1, k = 0;\n    // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++];\n        else\n            tmp[k++] = nums[j++];\n    }\n    // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n    for (k = 0; k < tmp.length; k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(int[] nums, int left, int right) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (left >= right)\n        return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    // \u5212\u5206\u9636\u6bb5\n    int mid = left + (right - left) / 2; // \u8ba1\u7b97\u4e2d\u70b9\n    mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n    mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n    // \u5408\u5e76\u9636\u6bb5\n    merge(nums, left, mid, right);\n}\n
        merge_sort.cs
        /* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nvoid Merge(int[] nums, int left, int mid, int right) {\n    // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n    // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n    int[] tmp = new int[right - left + 1];\n    // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n    int i = left, j = mid + 1, k = 0;\n    // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++];\n        else\n            tmp[k++] = nums[j++];\n    }\n    // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n    for (k = 0; k < tmp.Length; ++k) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid MergeSort(int[] nums, int left, int right) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (left >= right) return;       // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    // \u5212\u5206\u9636\u6bb5\n    int mid = left + (right - left) / 2;    // \u8ba1\u7b97\u4e2d\u70b9\n    MergeSort(nums, left, mid);      // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n    MergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n    // \u5408\u5e76\u9636\u6bb5\n    Merge(nums, left, mid, right);\n}\n
        merge_sort.go
        /* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nfunc merge(nums []int, left, mid, right int) {\n    // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n    // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n    tmp := make([]int, right-left+1)\n    // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n    i, j, k := left, mid+1, 0\n    // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    for i <= mid && j <= right {\n        if nums[i] <= nums[j] {\n            tmp[k] = nums[i]\n            i++\n        } else {\n            tmp[k] = nums[j]\n            j++\n        }\n        k++\n    }\n    // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    for i <= mid {\n        tmp[k] = nums[i]\n        i++\n        k++\n    }\n    for j <= right {\n        tmp[k] = nums[j]\n        j++\n        k++\n    }\n    // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n    for k := 0; k < len(tmp); k++ {\n        nums[left+k] = tmp[k]\n    }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nfunc mergeSort(nums []int, left, right int) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if left >= right {\n        return\n    }\n    // \u5212\u5206\u9636\u6bb5\n    mid := left + (right - left) / 2\n    mergeSort(nums, left, mid)\n    mergeSort(nums, mid+1, right)\n    // \u5408\u5e76\u9636\u6bb5\n    merge(nums, left, mid, right)\n}\n
        merge_sort.swift
        /* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nfunc merge(nums: inout [Int], left: Int, mid: Int, right: Int) {\n    // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n    // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n    var tmp = Array(repeating: 0, count: right - left + 1)\n    // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n    var i = left, j = mid + 1, k = 0\n    // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while i <= mid, j <= right {\n        if nums[i] <= nums[j] {\n            tmp[k] = nums[i]\n            i += 1\n        } else {\n            tmp[k] = nums[j]\n            j += 1\n        }\n        k += 1\n    }\n    // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while i <= mid {\n        tmp[k] = nums[i]\n        i += 1\n        k += 1\n    }\n    while j <= right {\n        tmp[k] = nums[j]\n        j += 1\n        k += 1\n    }\n    // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n    for k in tmp.indices {\n        nums[left + k] = tmp[k]\n    }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nfunc mergeSort(nums: inout [Int], left: Int, right: Int) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if left >= right { // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n        return\n    }\n    // \u5212\u5206\u9636\u6bb5\n    let mid = left + (right - left) / 2 // \u8ba1\u7b97\u4e2d\u70b9\n    mergeSort(nums: &nums, left: left, right: mid) // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n    mergeSort(nums: &nums, left: mid + 1, right: right) // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n    // \u5408\u5e76\u9636\u6bb5\n    merge(nums: &nums, left: left, mid: mid, right: right)\n}\n
        merge_sort.js
        /* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nfunction merge(nums, left, mid, right) {\n    // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n    // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n    const tmp = new Array(right - left + 1);\n    // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n    let i = left,\n        j = mid + 1,\n        k = 0;\n    // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j]) {\n            tmp[k++] = nums[i++];\n        } else {\n            tmp[k++] = nums[j++];\n        }\n    }\n    // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n    for (k = 0; k < tmp.length; k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nfunction mergeSort(nums, left, right) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    // \u5212\u5206\u9636\u6bb5\n    let mid = Math.floor(left + (right - left) / 2); // \u8ba1\u7b97\u4e2d\u70b9\n    mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n    mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n    // \u5408\u5e76\u9636\u6bb5\n    merge(nums, left, mid, right);\n}\n
        merge_sort.ts
        /* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nfunction merge(nums: number[], left: number, mid: number, right: number): void {\n    // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n    // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n    const tmp = new Array(right - left + 1);\n    // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n    let i = left,\n        j = mid + 1,\n        k = 0;\n    // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j]) {\n            tmp[k++] = nums[i++];\n        } else {\n            tmp[k++] = nums[j++];\n        }\n    }\n    // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n    for (k = 0; k < tmp.length; k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nfunction mergeSort(nums: number[], left: number, right: number): void {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    // \u5212\u5206\u9636\u6bb5\n    let mid = Math.floor(left + (right - left) / 2); // \u8ba1\u7b97\u4e2d\u70b9\n    mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n    mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n    // \u5408\u5e76\u9636\u6bb5\n    merge(nums, left, mid, right);\n}\n
        merge_sort.dart
        /* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nvoid merge(List<int> nums, int left, int mid, int right) {\n  // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n  // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n  List<int> tmp = List.filled(right - left + 1, 0);\n  // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n  int i = left, j = mid + 1, k = 0;\n  // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n  while (i <= mid && j <= right) {\n    if (nums[i] <= nums[j])\n      tmp[k++] = nums[i++];\n    else\n      tmp[k++] = nums[j++];\n  }\n  // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n  while (i <= mid) {\n    tmp[k++] = nums[i++];\n  }\n  while (j <= right) {\n    tmp[k++] = nums[j++];\n  }\n  // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n  for (k = 0; k < tmp.length; k++) {\n    nums[left + k] = tmp[k];\n  }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(List<int> nums, int left, int right) {\n  // \u7ec8\u6b62\u6761\u4ef6\n  if (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n  // \u5212\u5206\u9636\u6bb5\n  int mid = left + (right - left) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\n  mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n  mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n  // \u5408\u5e76\u9636\u6bb5\n  merge(nums, left, mid, right);\n}\n
        merge_sort.rs
        /* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nfn merge(nums: &mut [i32], left: usize, mid: usize, right: usize) {\n    // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n    // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n    let tmp_size = right - left + 1;\n    let mut tmp = vec![0; tmp_size];\n    // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n    let (mut i, mut j, mut k) = (left, mid + 1, 0);\n    // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while i <= mid && j <= right {\n        if nums[i] <= nums[j] {\n            tmp[k] = nums[i];\n            i += 1;\n        } else {\n            tmp[k] = nums[j];\n            j += 1;\n        }\n        k += 1;\n    }\n    // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while i <= mid {\n        tmp[k] = nums[i];\n        k += 1;\n        i += 1;\n    }\n    while j <= right {\n        tmp[k] = nums[j];\n        k += 1;\n        j += 1;\n    }\n    // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n    for k in 0..tmp_size {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nfn merge_sort(nums: &mut [i32], left: usize, right: usize) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if left >= right {\n        return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    }\n\n    // \u5212\u5206\u9636\u6bb5\n    let mid = left + (right - left) / 2; // \u8ba1\u7b97\u4e2d\u70b9\n    merge_sort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n    merge_sort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n\n    // \u5408\u5e76\u9636\u6bb5\n    merge(nums, left, mid, right);\n}\n
        merge_sort.c
        /* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nvoid merge(int *nums, int left, int mid, int right) {\n    // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n    // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n    int tmpSize = right - left + 1;\n    int *tmp = (int *)malloc(tmpSize * sizeof(int));\n    // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n    int i = left, j = mid + 1, k = 0;\n    // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j]) {\n            tmp[k++] = nums[i++];\n        } else {\n            tmp[k++] = nums[j++];\n        }\n    }\n    // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n    for (k = 0; k < tmpSize; ++k) {\n        nums[left + k] = tmp[k];\n    }\n    // \u91ca\u653e\u5185\u5b58\n    free(tmp);\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(int *nums, int left, int right) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (left >= right)\n        return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    // \u5212\u5206\u9636\u6bb5\n    int mid = left + (right - left) / 2;    // \u8ba1\u7b97\u4e2d\u70b9\n    mergeSort(nums, left, mid);      // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n    mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n    // \u5408\u5e76\u9636\u6bb5\n    merge(nums, left, mid, right);\n}\n
        merge_sort.kt
        /* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nfun merge(nums: IntArray, left: Int, mid: Int, right: Int) {\n    // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n    // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n    val tmp = IntArray(right - left + 1)\n    // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n    var i = left\n    var j = mid + 1\n    var k = 0\n    // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++]\n        else\n            tmp[k++] = nums[j++]\n    }\n    // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid) {\n        tmp[k++] = nums[i++]\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++]\n    }\n    // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n    for (l in tmp.indices) {\n        nums[left + l] = tmp[l]\n    }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nfun mergeSort(nums: IntArray, left: Int, right: Int) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (left >= right) return  // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    // \u5212\u5206\u9636\u6bb5\n    val mid = left + (right - left) / 2 // \u8ba1\u7b97\u4e2d\u70b9\n    mergeSort(nums, left, mid) // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n    mergeSort(nums, mid + 1, right) // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n    // \u5408\u5e76\u9636\u6bb5\n    merge(nums, left, mid, right)\n}\n
        merge_sort.rb
        ### \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 ###\ndef merge(nums, left, mid, right)\n  # \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n  # \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp\uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n  tmp = Array.new(right - left + 1, 0)\n  # \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n  i, j, k = left, mid + 1, 0\n  # \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n  while i <= mid && j <= right\n    if nums[i] <= nums[j]\n      tmp[k] = nums[i]\n      i += 1\n    else\n      tmp[k] = nums[j]\n      j += 1\n    end\n    k += 1\n  end\n  # \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n  while i <= mid\n    tmp[k] = nums[i]\n    i += 1\n    k += 1\n  end\n  while j <= right\n    tmp[k] = nums[j]\n    j += 1\n    k += 1\n  end\n  # \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n  (0...tmp.length).each do |k|\n    nums[left + k] = tmp[k]\n  end\nend\n\n### \u5f52\u5e76\u6392\u5e8f ###\ndef merge_sort(nums, left, right)\n  # \u7ec8\u6b62\u6761\u4ef6\n  # \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n  return if left >= right\n  # \u5212\u5206\u9636\u6bb5\n  mid = left + (right - left) / 2 # \u8ba1\u7b97\u4e2d\u70b9\n  merge_sort(nums, left, mid) # \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n  merge_sort(nums, mid + 1, right) # \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n  # \u5408\u5e76\u9636\u6bb5\n  merge(nums, left, mid, right)\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/merge_sort/#1162","title":"11.6.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\)\u3001\u975e\u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u5212\u5206\u4ea7\u751f\u9ad8\u5ea6\u4e3a \\(\\log n\\) \u7684\u9012\u5f52\u6811\uff0c\u6bcf\u5c42\u5408\u5e76\u7684\u603b\u64cd\u4f5c\u6570\u91cf\u4e3a \\(n\\) \uff0c\u56e0\u6b64\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002
        • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\)\u3001\u975e\u539f\u5730\u6392\u5e8f\uff1a\u9012\u5f52\u6df1\u5ea6\u4e3a \\(\\log n\\) \uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\u3002\u5408\u5e76\u64cd\u4f5c\u9700\u8981\u501f\u52a9\u8f85\u52a9\u6570\u7ec4\u5b9e\u73b0\uff0c\u4f7f\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002
        • \u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u5408\u5e76\u8fc7\u7a0b\u4e2d\uff0c\u76f8\u7b49\u5143\u7d20\u7684\u6b21\u5e8f\u4fdd\u6301\u4e0d\u53d8\u3002
        "},{"location":"chapter_sorting/merge_sort/#1163","title":"11.6.3 \u00a0 \u94fe\u8868\u6392\u5e8f","text":"

        \u5bf9\u4e8e\u94fe\u8868\uff0c\u5f52\u5e76\u6392\u5e8f\u76f8\u8f83\u4e8e\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\u5177\u6709\u663e\u8457\u4f18\u52bf\uff0c\u53ef\u4ee5\u5c06\u94fe\u8868\u6392\u5e8f\u4efb\u52a1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(1)\\) \u3002

        • \u5212\u5206\u9636\u6bb5\uff1a\u53ef\u4ee5\u4f7f\u7528\u201c\u8fed\u4ee3\u201d\u66ff\u4ee3\u201c\u9012\u5f52\u201d\u6765\u5b9e\u73b0\u94fe\u8868\u5212\u5206\u5de5\u4f5c\uff0c\u4ece\u800c\u7701\u53bb\u9012\u5f52\u4f7f\u7528\u7684\u6808\u5e27\u7a7a\u95f4\u3002
        • \u5408\u5e76\u9636\u6bb5\uff1a\u5728\u94fe\u8868\u4e2d\uff0c\u8282\u70b9\u589e\u5220\u64cd\u4f5c\u4ec5\u9700\u6539\u53d8\u5f15\u7528\uff08\u6307\u9488\uff09\u5373\u53ef\u5b9e\u73b0\uff0c\u56e0\u6b64\u5408\u5e76\u9636\u6bb5\uff08\u5c06\u4e24\u4e2a\u77ed\u6709\u5e8f\u94fe\u8868\u5408\u5e76\u4e3a\u4e00\u4e2a\u957f\u6709\u5e8f\u94fe\u8868\uff09\u65e0\u987b\u521b\u5efa\u989d\u5916\u94fe\u8868\u3002

        \u5177\u4f53\u5b9e\u73b0\u7ec6\u8282\u6bd4\u8f83\u590d\u6742\uff0c\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u67e5\u9605\u76f8\u5173\u8d44\u6599\u8fdb\u884c\u5b66\u4e60\u3002

        "},{"location":"chapter_sorting/quick_sort/","title":"11.5 \u00a0 \u5feb\u901f\u6392\u5e8f","text":"

        \u5feb\u901f\u6392\u5e8f\uff08quick sort\uff09\u662f\u4e00\u79cd\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u8fd0\u884c\u9ad8\u6548\uff0c\u5e94\u7528\u5e7f\u6cdb\u3002

        \u5feb\u901f\u6392\u5e8f\u7684\u6838\u5fc3\u64cd\u4f5c\u662f\u201c\u54e8\u5175\u5212\u5206\u201d\uff0c\u5176\u76ee\u6807\u662f\uff1a\u9009\u62e9\u6570\u7ec4\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u4f5c\u4e3a\u201c\u57fa\u51c6\u6570\u201d\uff0c\u5c06\u6240\u6709\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\u79fb\u5230\u5176\u5de6\u4fa7\uff0c\u800c\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\u79fb\u5230\u5176\u53f3\u4fa7\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u54e8\u5175\u5212\u5206\u7684\u6d41\u7a0b\u5982\u56fe 11-8 \u6240\u793a\u3002

        1. \u9009\u53d6\u6570\u7ec4\u6700\u5de6\u7aef\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\u6570\uff0c\u521d\u59cb\u5316\u4e24\u4e2a\u6307\u9488 i \u548c j \u5206\u522b\u6307\u5411\u6570\u7ec4\u7684\u4e24\u7aef\u3002
        2. \u8bbe\u7f6e\u4e00\u4e2a\u5faa\u73af\uff0c\u5728\u6bcf\u8f6e\u4e2d\u4f7f\u7528 i\uff08j\uff09\u5206\u522b\u5bfb\u627e\u7b2c\u4e00\u4e2a\u6bd4\u57fa\u51c6\u6570\u5927\uff08\u5c0f\uff09\u7684\u5143\u7d20\uff0c\u7136\u540e\u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\u3002
        3. \u5faa\u73af\u6267\u884c\u6b65\u9aa4 2. \uff0c\u76f4\u5230 i \u548c j \u76f8\u9047\u65f6\u505c\u6b62\uff0c\u6700\u540e\u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u4e2a\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\u3002
        <1><2><3><4><5><6><7><8><9>

        \u56fe 11-8 \u00a0 \u54e8\u5175\u5212\u5206\u6b65\u9aa4

        \u54e8\u5175\u5212\u5206\u5b8c\u6210\u540e\uff0c\u539f\u6570\u7ec4\u88ab\u5212\u5206\u6210\u4e09\u90e8\u5206\uff1a\u5de6\u5b50\u6570\u7ec4\u3001\u57fa\u51c6\u6570\u3001\u53f3\u5b50\u6570\u7ec4\uff0c\u4e14\u6ee1\u8db3\u201c\u5de6\u5b50\u6570\u7ec4\u4efb\u610f\u5143\u7d20 \\(\\leq\\) \u57fa\u51c6\u6570 \\(\\leq\\) \u53f3\u5b50\u6570\u7ec4\u4efb\u610f\u5143\u7d20\u201d\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u63a5\u4e0b\u6765\u53ea\u9700\u5bf9\u8fd9\u4e24\u4e2a\u5b50\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002

        \u5feb\u901f\u6392\u5e8f\u7684\u5206\u6cbb\u7b56\u7565

        \u54e8\u5175\u5212\u5206\u7684\u5b9e\u8d28\u662f\u5c06\u4e00\u4e2a\u8f83\u957f\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u7b80\u5316\u4e3a\u4e24\u4e2a\u8f83\u77ed\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
        def partition(self, nums: list[int], left: int, right: int) -> int:\n    \"\"\"\u54e8\u5175\u5212\u5206\"\"\"\n    # \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    i, j = left, right\n    while i < j:\n        while i < j and nums[j] >= nums[left]:\n            j -= 1  # \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while i < j and nums[i] <= nums[left]:\n            i += 1  # \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        # \u5143\u7d20\u4ea4\u6362\n        nums[i], nums[j] = nums[j], nums[i]\n    # \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    nums[i], nums[left] = nums[left], nums[i]\n    return i  # \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n
        quick_sort.cpp
        /* \u54e8\u5175\u5212\u5206 */\nint partition(vector<int> &nums, int left, int right) {\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;                // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while (i < j && nums[i] <= nums[left])\n            i++;                // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        swap(nums[i], nums[j]); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    swap(nums[i], nums[left]);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i;                   // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.java
        /* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(int[] nums, int i, int j) {\n    int tmp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5212\u5206 */\nint partition(int[] nums, int left, int right) {\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    swap(nums, i, left);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i;             // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.cs
        /* \u5143\u7d20\u4ea4\u6362 */\nvoid Swap(int[] nums, int i, int j) {\n    (nums[j], nums[i]) = (nums[i], nums[j]);\n}\n\n/* \u54e8\u5175\u5212\u5206 */\nint Partition(int[] nums, int left, int right) {\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        Swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    Swap(nums, i, left);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i;             // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.go
        /* \u54e8\u5175\u5212\u5206 */\nfunc (q *quickSort) partition(nums []int, left, right int) int {\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    i, j := left, right\n    for i < j {\n        for i < j && nums[j] >= nums[left] {\n            j-- // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        for i < j && nums[i] <= nums[left] {\n            i++ // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        // \u5143\u7d20\u4ea4\u6362\n        nums[i], nums[j] = nums[j], nums[i]\n    }\n    // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    nums[i], nums[left] = nums[left], nums[i]\n    return i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.swift
        /* \u54e8\u5175\u5212\u5206 */\nfunc partition(nums: inout [Int], left: Int, right: Int) -> Int {\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    var i = left\n    var j = right\n    while i < j {\n        while i < j, nums[j] >= nums[left] {\n            j -= 1 // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        while i < j, nums[i] <= nums[left] {\n            i += 1 // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        nums.swapAt(i, j) // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    nums.swapAt(i, left) // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.js
        /* \u5143\u7d20\u4ea4\u6362 */\nswap(nums, i, j) {\n    let tmp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5212\u5206 */\npartition(nums, left, right) {\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    let i = left,\n        j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) {\n            j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        while (i < j && nums[i] <= nums[left]) {\n            i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        // \u5143\u7d20\u4ea4\u6362\n        this.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    this.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.ts
        /* \u5143\u7d20\u4ea4\u6362 */\nswap(nums: number[], i: number, j: number): void {\n    let tmp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5212\u5206 */\npartition(nums: number[], left: number, right: number): number {\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    let i = left,\n        j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) {\n            j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        while (i < j && nums[i] <= nums[left]) {\n            i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        // \u5143\u7d20\u4ea4\u6362\n        this.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    this.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.dart
        /* \u5143\u7d20\u4ea4\u6362 */\nvoid _swap(List<int> nums, int i, int j) {\n  int tmp = nums[i];\n  nums[i] = nums[j];\n  nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5212\u5206 */\nint _partition(List<int> nums, int left, int right) {\n  // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n  int i = left, j = right;\n  while (i < j) {\n    while (i < j && nums[j] >= nums[left]) j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n    while (i < j && nums[i] <= nums[left]) i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n    _swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n  }\n  _swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n  return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.rs
        /* \u54e8\u5175\u5212\u5206 */\nfn partition(nums: &mut [i32], left: usize, right: usize) -> usize {\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    let (mut i, mut j) = (left, right);\n    while i < j {\n        while i < j && nums[j] >= nums[left] {\n            j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        while i < j && nums[i] <= nums[left] {\n            i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        nums.swap(i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    nums.swap(i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.c
        /* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(int nums[], int i, int j) {\n    int tmp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5212\u5206 */\nint partition(int nums[], int left, int right) {\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) {\n            j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        while (i < j && nums[i] <= nums[left]) {\n            i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n        swap(nums, i, j);\n    }\n    // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    swap(nums, i, left);\n    // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n    return i;\n}\n
        quick_sort.kt
        /* \u5143\u7d20\u4ea4\u6362 */\nfun swap(nums: IntArray, i: Int, j: Int) {\n    val temp = nums[i]\n    nums[i] = nums[j]\n    nums[j] = temp\n}\n\n/* \u54e8\u5175\u5212\u5206 */\nfun partition(nums: IntArray, left: Int, right: Int): Int {\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    var i = left\n    var j = right\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--           // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while (i < j && nums[i] <= nums[left])\n            i++           // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        swap(nums, i, j)  // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    swap(nums, i, left)   // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i              // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.rb
        ### \u54e8\u5175\u5212\u5206 ###\ndef partition(nums, left, right)\n  # \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n  i, j = left, right\n  while i < j\n    while i < j && nums[j] >= nums[left]\n      j -= 1 # \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n    end\n    while i < j && nums[i] <= nums[left]\n      i += 1 # \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n    end\n    # \u5143\u7d20\u4ea4\u6362\n    nums[i], nums[j] = nums[j], nums[i]\n  end\n  # \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n  nums[i], nums[left] = nums[left], nums[i]\n  i # \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/quick_sort/#1151","title":"11.5.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

        \u5feb\u901f\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u56fe 11-9 \u6240\u793a\u3002

        1. \u9996\u5148\uff0c\u5bf9\u539f\u6570\u7ec4\u6267\u884c\u4e00\u6b21\u201c\u54e8\u5175\u5212\u5206\u201d\uff0c\u5f97\u5230\u672a\u6392\u5e8f\u7684\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u3002
        2. \u7136\u540e\uff0c\u5bf9\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u5206\u522b\u9012\u5f52\u6267\u884c\u201c\u54e8\u5175\u5212\u5206\u201d\u3002
        3. \u6301\u7eed\u9012\u5f52\uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\uff0c\u4ece\u800c\u5b8c\u6210\u6574\u4e2a\u6570\u7ec4\u7684\u6392\u5e8f\u3002

        \u56fe 11-9 \u00a0 \u5feb\u901f\u6392\u5e8f\u6d41\u7a0b

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
        def quick_sort(self, nums: list[int], left: int, right: int):\n    \"\"\"\u5feb\u901f\u6392\u5e8f\"\"\"\n    # \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    if left >= right:\n        return\n    # \u54e8\u5175\u5212\u5206\n    pivot = self.partition(nums, left, right)\n    # \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    self.quick_sort(nums, left, pivot - 1)\n    self.quick_sort(nums, pivot + 1, right)\n
        quick_sort.cpp
        /* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(vector<int> &nums, int left, int right) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    if (left >= right)\n        return;\n    // \u54e8\u5175\u5212\u5206\n    int pivot = partition(nums, left, right);\n    // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    quickSort(nums, left, pivot - 1);\n    quickSort(nums, pivot + 1, right);\n}\n
        quick_sort.java
        /* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(int[] nums, int left, int right) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    if (left >= right)\n        return;\n    // \u54e8\u5175\u5212\u5206\n    int pivot = partition(nums, left, right);\n    // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    quickSort(nums, left, pivot - 1);\n    quickSort(nums, pivot + 1, right);\n}\n
        quick_sort.cs
        /* \u5feb\u901f\u6392\u5e8f */\nvoid QuickSort(int[] nums, int left, int right) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    if (left >= right)\n        return;\n    // \u54e8\u5175\u5212\u5206\n    int pivot = Partition(nums, left, right);\n    // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    QuickSort(nums, left, pivot - 1);\n    QuickSort(nums, pivot + 1, right);\n}\n
        quick_sort.go
        /* \u5feb\u901f\u6392\u5e8f */\nfunc (q *quickSort) quickSort(nums []int, left, right int) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    if left >= right {\n        return\n    }\n    // \u54e8\u5175\u5212\u5206\n    pivot := q.partition(nums, left, right)\n    // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    q.quickSort(nums, left, pivot-1)\n    q.quickSort(nums, pivot+1, right)\n}\n
        quick_sort.swift
        /* \u5feb\u901f\u6392\u5e8f */\nfunc quickSort(nums: inout [Int], left: Int, right: Int) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    if left >= right {\n        return\n    }\n    // \u54e8\u5175\u5212\u5206\n    let pivot = partition(nums: &nums, left: left, right: right)\n    // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    quickSort(nums: &nums, left: left, right: pivot - 1)\n    quickSort(nums: &nums, left: pivot + 1, right: right)\n}\n
        quick_sort.js
        /* \u5feb\u901f\u6392\u5e8f */\nquickSort(nums, left, right) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    if (left >= right) return;\n    // \u54e8\u5175\u5212\u5206\n    const pivot = this.partition(nums, left, right);\n    // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    this.quickSort(nums, left, pivot - 1);\n    this.quickSort(nums, pivot + 1, right);\n}\n
        quick_sort.ts
        /* \u5feb\u901f\u6392\u5e8f */\nquickSort(nums: number[], left: number, right: number): void {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    if (left >= right) {\n        return;\n    }\n    // \u54e8\u5175\u5212\u5206\n    const pivot = this.partition(nums, left, right);\n    // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    this.quickSort(nums, left, pivot - 1);\n    this.quickSort(nums, pivot + 1, right);\n}\n
        quick_sort.dart
        /* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(List<int> nums, int left, int right) {\n  // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n  if (left >= right) return;\n  // \u54e8\u5175\u5212\u5206\n  int pivot = _partition(nums, left, right);\n  // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n  quickSort(nums, left, pivot - 1);\n  quickSort(nums, pivot + 1, right);\n}\n
        quick_sort.rs
        /* \u5feb\u901f\u6392\u5e8f */\npub fn quick_sort(left: i32, right: i32, nums: &mut [i32]) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    if left >= right {\n        return;\n    }\n    // \u54e8\u5175\u5212\u5206\n    let pivot = Self::partition(nums, left as usize, right as usize) as i32;\n    // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    Self::quick_sort(left, pivot - 1, nums);\n    Self::quick_sort(pivot + 1, right, nums);\n}\n
        quick_sort.c
        /* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(int nums[], int left, int right) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    if (left >= right) {\n        return;\n    }\n    // \u54e8\u5175\u5212\u5206\n    int pivot = partition(nums, left, right);\n    // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    quickSort(nums, left, pivot - 1);\n    quickSort(nums, pivot + 1, right);\n}\n
        quick_sort.kt
        /* \u5feb\u901f\u6392\u5e8f */\nfun quickSort(nums: IntArray, left: Int, right: Int) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    if (left >= right) return\n    // \u54e8\u5175\u5212\u5206\n    val pivot = partition(nums, left, right)\n    // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    quickSort(nums, left, pivot - 1)\n    quickSort(nums, pivot + 1, right)\n}\n
        quick_sort.rb
        ### \u5feb\u901f\u6392\u5e8f\u7c7b ###\ndef quick_sort(nums, left, right)\n  # \u5b50\u6570\u7ec4\u957f\u5ea6\u4e0d\u4e3a 1 \u65f6\u9012\u5f52\n  if left < right\n    # \u54e8\u5175\u5212\u5206\n    pivot = partition(nums, left, right)\n    # \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    quick_sort(nums, left, pivot - 1)\n    quick_sort(nums, pivot + 1, right)\n  end\n  nums\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/quick_sort/#1152","title":"11.5.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\)\u3001\u975e\u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u5728\u5e73\u5747\u60c5\u51b5\u4e0b\uff0c\u54e8\u5175\u5212\u5206\u7684\u9012\u5f52\u5c42\u6570\u4e3a \\(\\log n\\) \uff0c\u6bcf\u5c42\u4e2d\u7684\u603b\u5faa\u73af\u6570\u4e3a \\(n\\) \uff0c\u603b\u4f53\u4f7f\u7528 \\(O(n \\log n)\\) \u65f6\u95f4\u3002\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u64cd\u4f5c\u90fd\u5c06\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\u5212\u5206\u4e3a\u957f\u5ea6\u4e3a \\(0\\) \u548c \\(n - 1\\) \u7684\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u6b64\u65f6\u9012\u5f52\u5c42\u6570\u8fbe\u5230 \\(n\\) \uff0c\u6bcf\u5c42\u4e2d\u7684\u5faa\u73af\u6570\u4e3a \\(n\\) \uff0c\u603b\u4f53\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002
        • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\)\u3001\u539f\u5730\u6392\u5e8f\uff1a\u5728\u8f93\u5165\u6570\u7ec4\u5b8c\u5168\u5012\u5e8f\u7684\u60c5\u51b5\u4e0b\uff0c\u8fbe\u5230\u6700\u5dee\u9012\u5f52\u6df1\u5ea6 \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002\u6392\u5e8f\u64cd\u4f5c\u662f\u5728\u539f\u6570\u7ec4\u4e0a\u8fdb\u884c\u7684\uff0c\u672a\u501f\u52a9\u989d\u5916\u6570\u7ec4\u3002
        • \u975e\u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u54e8\u5175\u5212\u5206\u7684\u6700\u540e\u4e00\u6b65\uff0c\u57fa\u51c6\u6570\u53ef\u80fd\u4f1a\u88ab\u4ea4\u6362\u81f3\u76f8\u7b49\u5143\u7d20\u7684\u53f3\u4fa7\u3002
        "},{"location":"chapter_sorting/quick_sort/#1153","title":"11.5.3 \u00a0 \u5feb\u901f\u6392\u5e8f\u4e3a\u4ec0\u4e48\u5feb","text":"

        \u4ece\u540d\u79f0\u4e0a\u5c31\u80fd\u770b\u51fa\uff0c\u5feb\u901f\u6392\u5e8f\u5728\u6548\u7387\u65b9\u9762\u5e94\u8be5\u5177\u6709\u4e00\u5b9a\u7684\u4f18\u52bf\u3002\u5c3d\u7ba1\u5feb\u901f\u6392\u5e8f\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e0e\u201c\u5f52\u5e76\u6392\u5e8f\u201d\u548c\u201c\u5806\u6392\u5e8f\u201d\u76f8\u540c\uff0c\u4f46\u901a\u5e38\u5feb\u901f\u6392\u5e8f\u7684\u6548\u7387\u66f4\u9ad8\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002

        • \u51fa\u73b0\u6700\u5dee\u60c5\u51b5\u7684\u6982\u7387\u5f88\u4f4e\uff1a\u867d\u7136\u5feb\u901f\u6392\u5e8f\u7684\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u6ca1\u6709\u5f52\u5e76\u6392\u5e8f\u7a33\u5b9a\uff0c\u4f46\u5728\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5feb\u901f\u6392\u5e8f\u80fd\u5728 \\(O(n \\log n)\\) \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e0b\u8fd0\u884c\u3002
        • \u7f13\u5b58\u4f7f\u7528\u6548\u7387\u9ad8\uff1a\u5728\u6267\u884c\u54e8\u5175\u5212\u5206\u64cd\u4f5c\u65f6\uff0c\u7cfb\u7edf\u53ef\u5c06\u6574\u4e2a\u5b50\u6570\u7ec4\u52a0\u8f7d\u5230\u7f13\u5b58\uff0c\u56e0\u6b64\u8bbf\u95ee\u5143\u7d20\u7684\u6548\u7387\u8f83\u9ad8\u3002\u800c\u50cf\u201c\u5806\u6392\u5e8f\u201d\u8fd9\u7c7b\u7b97\u6cd5\u9700\u8981\u8df3\u8dc3\u5f0f\u8bbf\u95ee\u5143\u7d20\uff0c\u4ece\u800c\u7f3a\u4e4f\u8fd9\u4e00\u7279\u6027\u3002
        • \u590d\u6742\u5ea6\u7684\u5e38\u6570\u7cfb\u6570\u5c0f\uff1a\u5728\u4e0a\u8ff0\u4e09\u79cd\u7b97\u6cd5\u4e2d\uff0c\u5feb\u901f\u6392\u5e8f\u7684\u6bd4\u8f83\u3001\u8d4b\u503c\u3001\u4ea4\u6362\u7b49\u64cd\u4f5c\u7684\u603b\u6570\u91cf\u6700\u5c11\u3002\u8fd9\u4e0e\u201c\u63d2\u5165\u6392\u5e8f\u201d\u6bd4\u201c\u5192\u6ce1\u6392\u5e8f\u201d\u66f4\u5feb\u7684\u539f\u56e0\u7c7b\u4f3c\u3002
        "},{"location":"chapter_sorting/quick_sort/#1154","title":"11.5.4 \u00a0 \u57fa\u51c6\u6570\u4f18\u5316","text":"

        \u5feb\u901f\u6392\u5e8f\u5728\u67d0\u4e9b\u8f93\u5165\u4e0b\u7684\u65f6\u95f4\u6548\u7387\u53ef\u80fd\u964d\u4f4e\u3002\u4e3e\u4e00\u4e2a\u6781\u7aef\u4f8b\u5b50\uff0c\u5047\u8bbe\u8f93\u5165\u6570\u7ec4\u662f\u5b8c\u5168\u5012\u5e8f\u7684\uff0c\u7531\u4e8e\u6211\u4eec\u9009\u62e9\u6700\u5de6\u7aef\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\u6570\uff0c\u90a3\u4e48\u5728\u54e8\u5175\u5212\u5206\u5b8c\u6210\u540e\uff0c\u57fa\u51c6\u6570\u88ab\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u53f3\u7aef\uff0c\u5bfc\u81f4\u5de6\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a \\(n - 1\\)\u3001\u53f3\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a \\(0\\) \u3002\u5982\u6b64\u9012\u5f52\u4e0b\u53bb\uff0c\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u540e\u90fd\u6709\u4e00\u4e2a\u5b50\u6570\u7ec4\u7684\u957f\u5ea6\u4e3a \\(0\\) \uff0c\u5206\u6cbb\u7b56\u7565\u5931\u6548\uff0c\u5feb\u901f\u6392\u5e8f\u9000\u5316\u4e3a\u201c\u5192\u6ce1\u6392\u5e8f\u201d\u7684\u8fd1\u4f3c\u5f62\u5f0f\u3002

        \u4e3a\u4e86\u5c3d\u91cf\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f18\u5316\u54e8\u5175\u5212\u5206\u4e2d\u7684\u57fa\u51c6\u6570\u7684\u9009\u53d6\u7b56\u7565\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u968f\u673a\u9009\u53d6\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\u6570\u3002\u7136\u800c\uff0c\u5982\u679c\u8fd0\u6c14\u4e0d\u4f73\uff0c\u6bcf\u6b21\u90fd\u9009\u5230\u4e0d\u7406\u60f3\u7684\u57fa\u51c6\u6570\uff0c\u6548\u7387\u4ecd\u7136\u4e0d\u5c3d\u5982\u4eba\u610f\u3002

        \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u751f\u6210\u7684\u662f\u201c\u4f2a\u968f\u673a\u6570\u201d\u3002\u5982\u679c\u6211\u4eec\u9488\u5bf9\u4f2a\u968f\u673a\u6570\u5e8f\u5217\u6784\u5efa\u4e00\u4e2a\u7279\u5b9a\u7684\u6d4b\u8bd5\u6837\u4f8b\uff0c\u90a3\u4e48\u5feb\u901f\u6392\u5e8f\u7684\u6548\u7387\u4ecd\u7136\u53ef\u80fd\u52a3\u5316\u3002

        \u4e3a\u4e86\u8fdb\u4e00\u6b65\u6539\u8fdb\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6570\u7ec4\u4e2d\u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\uff08\u901a\u5e38\u4e3a\u6570\u7ec4\u7684\u9996\u3001\u5c3e\u3001\u4e2d\u70b9\u5143\u7d20\uff09\uff0c\u5e76\u5c06\u8fd9\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\u4f5c\u4e3a\u57fa\u51c6\u6570\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u57fa\u51c6\u6570\u201c\u65e2\u4e0d\u592a\u5c0f\u4e5f\u4e0d\u592a\u5927\u201d\u7684\u6982\u7387\u5c06\u5927\u5e45\u63d0\u5347\u3002\u5f53\u7136\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u9009\u53d6\u66f4\u591a\u5019\u9009\u5143\u7d20\uff0c\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u7b97\u6cd5\u7684\u7a33\u5065\u6027\u3002\u91c7\u7528\u8fd9\u79cd\u65b9\u6cd5\u540e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(n^2)\\) \u7684\u6982\u7387\u5927\u5927\u964d\u4f4e\u3002

        \u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
        def median_three(self, nums: list[int], left: int, mid: int, right: int) -> int:\n    \"\"\"\u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\"\"\"\n    l, m, r = nums[left], nums[mid], nums[right]\n    if (l <= m <= r) or (r <= m <= l):\n        return mid  # m \u5728 l \u548c r \u4e4b\u95f4\n    if (m <= l <= r) or (r <= l <= m):\n        return left  # l \u5728 m \u548c r \u4e4b\u95f4\n    return right\n\ndef partition(self, nums: list[int], left: int, right: int) -> int:\n    \"\"\"\u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09\"\"\"\n    # \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    med = self.median_three(nums, left, (left + right) // 2, right)\n    # \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n    nums[left], nums[med] = nums[med], nums[left]\n    # \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    i, j = left, right\n    while i < j:\n        while i < j and nums[j] >= nums[left]:\n            j -= 1  # \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while i < j and nums[i] <= nums[left]:\n            i += 1  # \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        # \u5143\u7d20\u4ea4\u6362\n        nums[i], nums[j] = nums[j], nums[i]\n    # \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    nums[i], nums[left] = nums[left], nums[i]\n    return i  # \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n
        quick_sort.cpp
        /* \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint medianThree(vector<int> &nums, int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m \u5728 l \u548c r \u4e4b\u95f4\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l \u5728 m \u548c r \u4e4b\u95f4\n    return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint partition(vector<int> &nums, int left, int right) {\n    // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n    int med = medianThree(nums, left, (left + right) / 2, right);\n    // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n    swap(nums[left], nums[med]);\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;                // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while (i < j && nums[i] <= nums[left])\n            i++;                // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        swap(nums[i], nums[j]); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    swap(nums[i], nums[left]);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i;                   // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.java
        /* \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint medianThree(int[] nums, int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m \u5728 l \u548c r \u4e4b\u95f4\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l \u5728 m \u548c r \u4e4b\u95f4\n    return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint partition(int[] nums, int left, int right) {\n    // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n    int med = medianThree(nums, left, (left + right) / 2, right);\n    // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n    swap(nums, left, med);\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    swap(nums, i, left);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i;             // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.cs
        /* \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint MedianThree(int[] nums, int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m \u5728 l \u548c r \u4e4b\u95f4\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l \u5728 m \u548c r \u4e4b\u95f4\n    return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint Partition(int[] nums, int left, int right) {\n    // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n    int med = MedianThree(nums, left, (left + right) / 2, right);\n    // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n    Swap(nums, left, med);\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        Swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    Swap(nums, i, left);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i;             // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.go
        /* \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nfunc (q *quickSortMedian) medianThree(nums []int, left, mid, right int) int {\n    l, m, r := nums[left], nums[mid], nums[right]\n    if (l <= m && m <= r) || (r <= m && m <= l) {\n        return mid // m \u5728 l \u548c r \u4e4b\u95f4\n    }\n    if (m <= l && l <= r) || (r <= l && l <= m) {\n        return left // l \u5728 m \u548c r \u4e4b\u95f4\n    }\n    return right\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09*/\nfunc (q *quickSortMedian) partition(nums []int, left, right int) int {\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    med := q.medianThree(nums, left, (left+right)/2, right)\n    // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n    nums[left], nums[med] = nums[med], nums[left]\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    i, j := left, right\n    for i < j {\n        for i < j && nums[j] >= nums[left] {\n            j-- //\u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        for i < j && nums[i] <= nums[left] {\n            i++ //\u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        //\u5143\u7d20\u4ea4\u6362\n        nums[i], nums[j] = nums[j], nums[i]\n    }\n    //\u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    nums[i], nums[left] = nums[left], nums[i]\n    return i //\u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.swift
        /* \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nfunc medianThree(nums: [Int], left: Int, mid: Int, right: Int) -> Int {\n    let l = nums[left]\n    let m = nums[mid]\n    let r = nums[right]\n    if (l <= m && m <= r) || (r <= m && m <= l) {\n        return mid // m \u5728 l \u548c r \u4e4b\u95f4\n    }\n    if (m <= l && l <= r) || (r <= l && l <= m) {\n        return left // l \u5728 m \u548c r \u4e4b\u95f4\n    }\n    return right\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nfunc partitionMedian(nums: inout [Int], left: Int, right: Int) -> Int {\n    // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n    let med = medianThree(nums: nums, left: left, mid: left + (right - left) / 2, right: right)\n    // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n    nums.swapAt(left, med)\n    return partition(nums: &nums, left: left, right: right)\n}\n
        quick_sort.js
        /* \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nmedianThree(nums, left, mid, right) {\n    let l = nums[left],\n        m = nums[mid],\n        r = nums[right];\n    // m \u5728 l \u548c r \u4e4b\u95f4\n    if ((l <= m && m <= r) || (r <= m && m <= l)) return mid;\n    // l \u5728 m \u548c r \u4e4b\u95f4\n    if ((m <= l && l <= r) || (r <= l && l <= m)) return left;\n    return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\npartition(nums, left, right) {\n    // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n    let med = this.medianThree(\n        nums,\n        left,\n        Math.floor((left + right) / 2),\n        right\n    );\n    // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n    this.swap(nums, left, med);\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    let i = left,\n        j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while (i < j && nums[i] <= nums[left]) i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        this.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    this.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.ts
        /* \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nmedianThree(\n    nums: number[],\n    left: number,\n    mid: number,\n    right: number\n): number {\n    let l = nums[left],\n        m = nums[mid],\n        r = nums[right];\n    // m \u5728 l \u548c r \u4e4b\u95f4\n    if ((l <= m && m <= r) || (r <= m && m <= l)) return mid;\n    // l \u5728 m \u548c r \u4e4b\u95f4\n    if ((m <= l && l <= r) || (r <= l && l <= m)) return left;\n    return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\npartition(nums: number[], left: number, right: number): number {\n    // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n    let med = this.medianThree(\n        nums,\n        left,\n        Math.floor((left + right) / 2),\n        right\n    );\n    // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n    this.swap(nums, left, med);\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    let i = left,\n        j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) {\n            j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        while (i < j && nums[i] <= nums[left]) {\n            i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        this.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    this.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.dart
        /* \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint _medianThree(List<int> nums, int left, int mid, int right) {\n  int l = nums[left], m = nums[mid], r = nums[right];\n  if ((l <= m && m <= r) || (r <= m && m <= l))\n    return mid; // m \u5728 l \u548c r \u4e4b\u95f4\n  if ((m <= l && l <= r) || (r <= l && l <= m))\n    return left; // l \u5728 m \u548c r \u4e4b\u95f4\n  return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint _partition(List<int> nums, int left, int right) {\n  // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n  int med = _medianThree(nums, left, (left + right) ~/ 2, right);\n  // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n  _swap(nums, left, med);\n  // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n  int i = left, j = right;\n  while (i < j) {\n    while (i < j && nums[j] >= nums[left]) j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n    while (i < j && nums[i] <= nums[left]) i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n    _swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n  }\n  _swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n  return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.rs
        /* \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nfn median_three(nums: &mut [i32], left: usize, mid: usize, right: usize) -> usize {\n    let (l, m, r) = (nums[left], nums[mid], nums[right]);\n    if (l <= m && m <= r) || (r <= m && m <= l) {\n        return mid; // m \u5728 l \u548c r \u4e4b\u95f4\n    }\n    if (m <= l && l <= r) || (r <= l && l <= m) {\n        return left; // l \u5728 m \u548c r \u4e4b\u95f4\n    }\n    right\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nfn partition(nums: &mut [i32], left: usize, right: usize) -> usize {\n    // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n    let med = Self::median_three(nums, left, (left + right) / 2, right);\n    // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n    nums.swap(left, med);\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    let (mut i, mut j) = (left, right);\n    while i < j {\n        while i < j && nums[j] >= nums[left] {\n            j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        while i < j && nums[i] <= nums[left] {\n            i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        nums.swap(i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    nums.swap(i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.c
        /* \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint medianThree(int nums[], int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m \u5728 l \u548c r \u4e4b\u95f4\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l \u5728 m \u548c r \u4e4b\u95f4\n    return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint partitionMedian(int nums[], int left, int right) {\n    // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n    int med = medianThree(nums, left, (left + right) / 2, right);\n    // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n    swap(nums, left, med);\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i;            // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.kt
        /* \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nfun medianThree(nums: IntArray, left: Int, mid: Int, right: Int): Int {\n    val l = nums[left]\n    val m = nums[mid]\n    val r = nums[right]\n    if ((m in l..r) || (m in r..l))\n        return mid  // m \u5728 l \u548c r \u4e4b\u95f4\n    if ((l in m..r) || (l in r..m))\n        return left // l \u5728 m \u548c r \u4e4b\u95f4\n    return right\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nfun partitionMedian(nums: IntArray, left: Int, right: Int): Int {\n    // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n    val med = medianThree(nums, left, (left + right) / 2, right)\n    // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n    swap(nums, left, med)\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    var i = left\n    var j = right\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--                      // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while (i < j && nums[i] <= nums[left])\n            i++                      // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        swap(nums, i, j)             // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    swap(nums, i, left)              // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i                         // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.rb
        ### \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 ###\ndef median_three(nums, left, mid, right)\n  # \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n  _l, _m, _r = nums[left], nums[mid], nums[right]\n  # m \u5728 l \u548c r \u4e4b\u95f4\n  return mid if (_l <= _m && _m <= _r) || (_r <= _m && _m <= _l)\n  # l \u5728 m \u548c r \u4e4b\u95f4\n  return left if (_m <= _l && _l <= _r) || (_r <= _l && _l <= _m)\n  return right\nend\n\n### \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09###\ndef partition(nums, left, right)\n  ### \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n  med = median_three(nums, left, (left + right) / 2, right)\n  # \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u65ad\n  nums[left], nums[med] = nums[med], nums[left]\n  i, j = left, right\n  while i < j\n    while i < j && nums[j] >= nums[left]\n      j -= 1 # \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n    end\n    while i < j && nums[i] <= nums[left]\n      i += 1 # \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n    end\n    # \u5143\u7d20\u4ea4\u6362\n    nums[i], nums[j] = nums[j], nums[i]\n  end\n  # \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n  nums[i], nums[left] = nums[left], nums[i]\n  i # \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/quick_sort/#1155","title":"11.5.5 \u00a0 \u9012\u5f52\u6df1\u5ea6\u4f18\u5316","text":"

        \u5728\u67d0\u4e9b\u8f93\u5165\u4e0b\uff0c\u5feb\u901f\u6392\u5e8f\u53ef\u80fd\u5360\u7528\u7a7a\u95f4\u8f83\u591a\u3002\u4ee5\u5b8c\u5168\u6709\u5e8f\u7684\u8f93\u5165\u6570\u7ec4\u4e3a\u4f8b\uff0c\u8bbe\u9012\u5f52\u4e2d\u7684\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a \\(m\\) \uff0c\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u64cd\u4f5c\u90fd\u5c06\u4ea7\u751f\u957f\u5ea6\u4e3a \\(0\\) \u7684\u5de6\u5b50\u6570\u7ec4\u548c\u957f\u5ea6\u4e3a \\(m - 1\\) \u7684\u53f3\u5b50\u6570\u7ec4\uff0c\u8fd9\u610f\u5473\u7740\u6bcf\u4e00\u5c42\u9012\u5f52\u8c03\u7528\u51cf\u5c11\u7684\u95ee\u9898\u89c4\u6a21\u975e\u5e38\u5c0f\uff08\u53ea\u51cf\u5c11\u4e00\u4e2a\u5143\u7d20\uff09\uff0c\u9012\u5f52\u6811\u7684\u9ad8\u5ea6\u4f1a\u8fbe\u5230 \\(n - 1\\) \uff0c\u6b64\u65f6\u9700\u8981\u5360\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\u3002

        \u4e3a\u4e86\u9632\u6b62\u6808\u5e27\u7a7a\u95f4\u7684\u7d2f\u79ef\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6bcf\u8f6e\u54e8\u5175\u6392\u5e8f\u5b8c\u6210\u540e\uff0c\u6bd4\u8f83\u4e24\u4e2a\u5b50\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u4ec5\u5bf9\u8f83\u77ed\u7684\u5b50\u6570\u7ec4\u8fdb\u884c\u9012\u5f52\u3002\u7531\u4e8e\u8f83\u77ed\u5b50\u6570\u7ec4\u7684\u957f\u5ea6\u4e0d\u4f1a\u8d85\u8fc7 \\(n / 2\\) \uff0c\u56e0\u6b64\u8fd9\u79cd\u65b9\u6cd5\u80fd\u786e\u4fdd\u9012\u5f52\u6df1\u5ea6\u4e0d\u8d85\u8fc7 \\(\\log n\\) \uff0c\u4ece\u800c\u5c06\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(\\log n)\\) \u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
        def quick_sort(self, nums: list[int], left: int, right: int):\n    \"\"\"\u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09\"\"\"\n    # \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n    while left < right:\n        # \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n        pivot = self.partition(nums, left, right)\n        # \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n        if pivot - left < right - pivot:\n            self.quick_sort(nums, left, pivot - 1)  # \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n            left = pivot + 1  # \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n        else:\n            self.quick_sort(nums, pivot + 1, right)  # \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n            right = pivot - 1  # \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n
        quick_sort.cpp
        /* \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09 */\nvoid quickSort(vector<int> &nums, int left, int right) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n    while (left < right) {\n        // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n        int pivot = partition(nums, left, right);\n        // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n        if (pivot - left < right - pivot) {\n            quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n            left = pivot + 1;                 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n        } else {\n            quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n            right = pivot - 1;                 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n        }\n    }\n}\n
        quick_sort.java
        /* \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09 */\nvoid quickSort(int[] nums, int left, int right) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n    while (left < right) {\n        // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n        int pivot = partition(nums, left, right);\n        // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n        if (pivot - left < right - pivot) {\n            quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n            left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n        } else {\n            quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n            right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n        }\n    }\n}\n
        quick_sort.cs
        /* \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09 */\nvoid QuickSort(int[] nums, int left, int right) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n    while (left < right) {\n        // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n        int pivot = Partition(nums, left, right);\n        // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n        if (pivot - left < right - pivot) {\n            QuickSort(nums, left, pivot - 1);  // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n            left = pivot + 1;  // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n        } else {\n            QuickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n            right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n        }\n    }\n}\n
        quick_sort.go
        /* \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09*/\nfunc (q *quickSortTailCall) quickSort(nums []int, left, right int) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n    for left < right {\n        // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n        pivot := q.partition(nums, left, right)\n        // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n        if pivot-left < right-pivot {\n            q.quickSort(nums, left, pivot-1) // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n            left = pivot + 1                 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n        } else {\n            q.quickSort(nums, pivot+1, right) // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n            right = pivot - 1                 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n        }\n    }\n}\n
        quick_sort.swift
        /* \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09 */\nfunc quickSortTailCall(nums: inout [Int], left: Int, right: Int) {\n    var left = left\n    var right = right\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n    while left < right {\n        // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n        let pivot = partition(nums: &nums, left: left, right: right)\n        // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n        if (pivot - left) < (right - pivot) {\n            quickSortTailCall(nums: &nums, left: left, right: pivot - 1) // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n            left = pivot + 1 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n        } else {\n            quickSortTailCall(nums: &nums, left: pivot + 1, right: right) // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n            right = pivot - 1 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n        }\n    }\n}\n
        quick_sort.js
        /* \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09 */\nquickSort(nums, left, right) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n    while (left < right) {\n        // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n        let pivot = this.partition(nums, left, right);\n        // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n        if (pivot - left < right - pivot) {\n            this.quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n            left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n        } else {\n            this.quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n            right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n        }\n    }\n}\n
        quick_sort.ts
        /* \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09 */\nquickSort(nums: number[], left: number, right: number): void {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n    while (left < right) {\n        // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n        let pivot = this.partition(nums, left, right);\n        // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n        if (pivot - left < right - pivot) {\n            this.quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n            left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n        } else {\n            this.quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n            right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n        }\n    }\n}\n
        quick_sort.dart
        /* \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09 */\nvoid quickSort(List<int> nums, int left, int right) {\n  // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n  while (left < right) {\n    // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n    int pivot = _partition(nums, left, right);\n    // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n    if (pivot - left < right - pivot) {\n      quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n      left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n    } else {\n      quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n      right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n    }\n  }\n}\n
        quick_sort.rs
        /* \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09 */\npub fn quick_sort(mut left: i32, mut right: i32, nums: &mut [i32]) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n    while left < right {\n        // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n        let pivot = Self::partition(nums, left as usize, right as usize) as i32;\n        // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n        if pivot - left < right - pivot {\n            Self::quick_sort(left, pivot - 1, nums); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n            left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n        } else {\n            Self::quick_sort(pivot + 1, right, nums); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n            right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n        }\n    }\n}\n
        quick_sort.c
        /* \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09 */\nvoid quickSortTailCall(int nums[], int left, int right) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n    while (left < right) {\n        // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n        int pivot = partition(nums, left, right);\n        // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n        if (pivot - left < right - pivot) {\n            // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n            quickSortTailCall(nums, left, pivot - 1);\n            // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n            left = pivot + 1;\n        } else {\n            // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n            quickSortTailCall(nums, pivot + 1, right);\n            // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n            right = pivot - 1;\n        }\n    }\n}\n
        quick_sort.kt
        /* \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09 */\nfun quickSortTailCall(nums: IntArray, left: Int, right: Int) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n    var l = left\n    var r = right\n    while (l < r) {\n        // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n        val pivot = partition(nums, l, r)\n        // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n        if (pivot - l < r - pivot) {\n            quickSort(nums, l, pivot - 1) // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n            l = pivot + 1 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n        } else {\n            quickSort(nums, pivot + 1, r) // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n            r = pivot - 1 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n        }\n    }\n}\n
        quick_sort.rb
        ### \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09###\ndef quick_sort(nums, left, right)\n  # \u5b50\u6570\u7ec4\u957f\u5ea6\u4e0d\u4e3a 1 \u65f6\u9012\u5f52\n  while left < right\n    # \u54e8\u5175\u5212\u5206\n    pivot = partition(nums, left, right)\n    # \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n    if pivot - left < right - pivot\n      quick_sort(nums, left, pivot - 1)\n      left = pivot + 1 # \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n    else\n      quick_sort(nums, pivot + 1, right)\n      right = pivot - 1 # \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n    end\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/radix_sort/","title":"11.10 \u00a0 \u57fa\u6570\u6392\u5e8f","text":"

        \u4e0a\u4e00\u8282\u4ecb\u7ecd\u4e86\u8ba1\u6570\u6392\u5e8f\uff0c\u5b83\u9002\u7528\u4e8e\u6570\u636e\u91cf \\(n\\) \u8f83\u5927\u4f46\u6570\u636e\u8303\u56f4 \\(m\\) \u8f83\u5c0f\u7684\u60c5\u51b5\u3002\u5047\u8bbe\u6211\u4eec\u9700\u8981\u5bf9 \\(n = 10^6\\) \u4e2a\u5b66\u53f7\u8fdb\u884c\u6392\u5e8f\uff0c\u800c\u5b66\u53f7\u662f\u4e00\u4e2a \\(8\\) \u4f4d\u6570\u5b57\uff0c\u8fd9\u610f\u5473\u7740\u6570\u636e\u8303\u56f4 \\(m = 10^8\\) \u975e\u5e38\u5927\uff0c\u4f7f\u7528\u8ba1\u6570\u6392\u5e8f\u9700\u8981\u5206\u914d\u5927\u91cf\u5185\u5b58\u7a7a\u95f4\uff0c\u800c\u57fa\u6570\u6392\u5e8f\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002

        \u57fa\u6570\u6392\u5e8f\uff08radix sort\uff09\u7684\u6838\u5fc3\u601d\u60f3\u4e0e\u8ba1\u6570\u6392\u5e8f\u4e00\u81f4\uff0c\u4e5f\u901a\u8fc7\u7edf\u8ba1\u4e2a\u6570\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u57fa\u6570\u6392\u5e8f\u5229\u7528\u6570\u5b57\u5404\u4f4d\u4e4b\u95f4\u7684\u9012\u8fdb\u5173\u7cfb\uff0c\u4f9d\u6b21\u5bf9\u6bcf\u4e00\u4f4d\u8fdb\u884c\u6392\u5e8f\uff0c\u4ece\u800c\u5f97\u5230\u6700\u7ec8\u7684\u6392\u5e8f\u7ed3\u679c\u3002

        "},{"location":"chapter_sorting/radix_sort/#11101","title":"11.10.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

        \u4ee5\u5b66\u53f7\u6570\u636e\u4e3a\u4f8b\uff0c\u5047\u8bbe\u6570\u5b57\u7684\u6700\u4f4e\u4f4d\u662f\u7b2c \\(1\\) \u4f4d\uff0c\u6700\u9ad8\u4f4d\u662f\u7b2c \\(8\\) \u4f4d\uff0c\u57fa\u6570\u6392\u5e8f\u7684\u6d41\u7a0b\u5982\u56fe 11-18 \u6240\u793a\u3002

        1. \u521d\u59cb\u5316\u4f4d\u6570 \\(k = 1\\) \u3002
        2. \u5bf9\u5b66\u53f7\u7684\u7b2c \\(k\\) \u4f4d\u6267\u884c\u201c\u8ba1\u6570\u6392\u5e8f\u201d\u3002\u5b8c\u6210\u540e\uff0c\u6570\u636e\u4f1a\u6839\u636e\u7b2c \\(k\\) \u4f4d\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\u3002
        3. \u5c06 \\(k\\) \u589e\u52a0 \\(1\\) \uff0c\u7136\u540e\u8fd4\u56de\u6b65\u9aa4 2. \u7ee7\u7eed\u8fed\u4ee3\uff0c\u76f4\u5230\u6240\u6709\u4f4d\u90fd\u6392\u5e8f\u5b8c\u6210\u540e\u7ed3\u675f\u3002

        \u56fe 11-18 \u00a0 \u57fa\u6570\u6392\u5e8f\u7b97\u6cd5\u6d41\u7a0b

        \u4e0b\u9762\u5256\u6790\u4ee3\u7801\u5b9e\u73b0\u3002\u5bf9\u4e8e\u4e00\u4e2a \\(d\\) \u8fdb\u5236\u7684\u6570\u5b57 \\(x\\) \uff0c\u8981\u83b7\u53d6\u5176\u7b2c \\(k\\) \u4f4d \\(x_k\\) \uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8ba1\u7b97\u516c\u5f0f\uff1a

        \\[ x_k = \\lfloor\\frac{x}{d^{k-1}}\\rfloor \\bmod d \\]

        \u5176\u4e2d \\(\\lfloor a \\rfloor\\) \u8868\u793a\u5bf9\u6d6e\u70b9\u6570 \\(a\\) \u5411\u4e0b\u53d6\u6574\uff0c\u800c \\(\\bmod \\: d\\) \u8868\u793a\u5bf9 \\(d\\) \u53d6\u6a21\uff08\u53d6\u4f59\uff09\u3002\u5bf9\u4e8e\u5b66\u53f7\u6570\u636e\uff0c\\(d = 10\\) \u4e14 \\(k \\in [1, 8]\\) \u3002

        \u6b64\u5916\uff0c\u6211\u4eec\u9700\u8981\u5c0f\u5e45\u6539\u52a8\u8ba1\u6570\u6392\u5e8f\u4ee3\u7801\uff0c\u4f7f\u4e4b\u53ef\u4ee5\u6839\u636e\u6570\u5b57\u7684\u7b2c \\(k\\) \u4f4d\u8fdb\u884c\u6392\u5e8f\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby radix_sort.py
        def digit(num: int, exp: int) -> int:\n    \"\"\"\u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1)\"\"\"\n    # \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n    return (num // exp) % 10\n\ndef counting_sort_digit(nums: list[int], exp: int):\n    \"\"\"\u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09\"\"\"\n    # \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n    counter = [0] * 10\n    n = len(nums)\n    # \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    for i in range(n):\n        d = digit(nums[i], exp)  # \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n        counter[d] += 1  # \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n    # \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n    for i in range(1, 10):\n        counter[i] += counter[i - 1]\n    # \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n    res = [0] * n\n    for i in range(n - 1, -1, -1):\n        d = digit(nums[i], exp)\n        j = counter[d] - 1  # \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n        res[j] = nums[i]  # \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n        counter[d] -= 1  # \u5c06 d \u7684\u6570\u91cf\u51cf 1\n    # \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n    for i in range(n):\n        nums[i] = res[i]\n\ndef radix_sort(nums: list[int]):\n    \"\"\"\u57fa\u6570\u6392\u5e8f\"\"\"\n    # \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n    m = max(nums)\n    # \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n    exp = 1\n    while exp <= m:\n        # \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n        # k = 1 -> exp = 1\n        # k = 2 -> exp = 10\n        # \u5373 exp = 10^(k-1)\n        counting_sort_digit(nums, exp)\n        exp *= 10\n
        radix_sort.cpp
        /* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int num, int exp) {\n    // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n    return (num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(vector<int> &nums, int exp) {\n    // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n    vector<int> counter(10, 0);\n    int n = nums.size();\n    // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    for (int i = 0; i < n; i++) {\n        int d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n        counter[d]++;                // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n    }\n    // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n    vector<int> res(n, 0);\n    for (int i = n - 1; i >= 0; i--) {\n        int d = digit(nums[i], exp);\n        int j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n        res[j] = nums[i];       // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n        counter[d]--;           // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (int i = 0; i < n; i++)\n        nums[i] = res[i];\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nvoid radixSort(vector<int> &nums) {\n    // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n    int m = *max_element(nums.begin(), nums.end());\n    // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n    for (int exp = 1; exp <= m; exp *= 10)\n        // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u5373 exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n}\n
        radix_sort.java
        /* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int num, int exp) {\n    // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n    return (num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(int[] nums, int exp) {\n    // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n    int[] counter = new int[10];\n    int n = nums.length;\n    // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    for (int i = 0; i < n; i++) {\n        int d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n        counter[d]++;                // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n    }\n    // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int d = digit(nums[i], exp);\n        int j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n        res[j] = nums[i];       // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n        counter[d]--;           // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (int i = 0; i < n; i++)\n        nums[i] = res[i];\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nvoid radixSort(int[] nums) {\n    // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n    int m = Integer.MIN_VALUE;\n    for (int num : nums)\n        if (num > m)\n            m = num;\n    // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n    for (int exp = 1; exp <= m; exp *= 10) {\n        // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u5373 exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n    }\n}\n
        radix_sort.cs
        /* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint Digit(int num, int exp) {\n    // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n    return (num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid CountingSortDigit(int[] nums, int exp) {\n    // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n    int[] counter = new int[10];\n    int n = nums.Length;\n    // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    for (int i = 0; i < n; i++) {\n        int d = Digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n        counter[d]++;                // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n    }\n    // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int d = Digit(nums[i], exp);\n        int j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n        res[j] = nums[i];       // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n        counter[d]--;           // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (int i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nvoid RadixSort(int[] nums) {\n    // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n    int m = int.MinValue;\n    foreach (int num in nums) {\n        if (num > m) m = num;\n    }\n    // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n    for (int exp = 1; exp <= m; exp *= 10) {\n        // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u5373 exp = 10^(k-1)\n        CountingSortDigit(nums, exp);\n    }\n}\n
        radix_sort.go
        /* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunc digit(num, exp int) int {\n    // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n    return (num / exp) % 10\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunc countingSortDigit(nums []int, exp int) {\n    // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n    counter := make([]int, 10)\n    n := len(nums)\n    // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    for i := 0; i < n; i++ {\n        d := digit(nums[i], exp) // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n        counter[d]++             // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n    }\n    // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n    for i := 1; i < 10; i++ {\n        counter[i] += counter[i-1]\n    }\n    // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n    res := make([]int, n)\n    for i := n - 1; i >= 0; i-- {\n        d := digit(nums[i], exp)\n        j := counter[d] - 1 // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n        res[j] = nums[i]    // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n        counter[d]--        // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n    for i := 0; i < n; i++ {\n        nums[i] = res[i]\n    }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nfunc radixSort(nums []int) {\n    // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n    max := math.MinInt\n    for _, num := range nums {\n        if num > max {\n            max = num\n        }\n    }\n    // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n    for exp := 1; max >= exp; exp *= 10 {\n        // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u5373 exp = 10^(k-1)\n        countingSortDigit(nums, exp)\n    }\n}\n
        radix_sort.swift
        /* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunc digit(num: Int, exp: Int) -> Int {\n    // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n    (num / exp) % 10\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunc countingSortDigit(nums: inout [Int], exp: Int) {\n    // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n    var counter = Array(repeating: 0, count: 10)\n    // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    for i in nums.indices {\n        let d = digit(num: nums[i], exp: exp) // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n        counter[d] += 1 // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n    }\n    // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n    for i in 1 ..< 10 {\n        counter[i] += counter[i - 1]\n    }\n    // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n    var res = Array(repeating: 0, count: nums.count)\n    for i in nums.indices.reversed() {\n        let d = digit(num: nums[i], exp: exp)\n        let j = counter[d] - 1 // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n        res[j] = nums[i] // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n        counter[d] -= 1 // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n    for i in nums.indices {\n        nums[i] = res[i]\n    }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nfunc radixSort(nums: inout [Int]) {\n    // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n    var m = Int.min\n    for num in nums {\n        if num > m {\n            m = num\n        }\n    }\n    // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n    for exp in sequence(first: 1, next: { m >= ($0 * 10) ? $0 * 10 : nil }) {\n        // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u5373 exp = 10^(k-1)\n        countingSortDigit(nums: &nums, exp: exp)\n    }\n}\n
        radix_sort.js
        /* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunction digit(num, exp) {\n    // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n    return Math.floor(num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunction countingSortDigit(nums, exp) {\n    // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n    const counter = new Array(10).fill(0);\n    const n = nums.length;\n    // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    for (let i = 0; i < n; i++) {\n        const d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n        counter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n    }\n    // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n    for (let i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n    const res = new Array(n).fill(0);\n    for (let i = n - 1; i >= 0; i--) {\n        const d = digit(nums[i], exp);\n        const j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n        res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n        counter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (let i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nfunction radixSort(nums) {\n    // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n    let m = Math.max(... nums);\n    // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n    for (let exp = 1; exp <= m; exp *= 10) {\n        // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u5373 exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n    }\n}\n
        radix_sort.ts
        /* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunction digit(num: number, exp: number): number {\n    // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n    return Math.floor(num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunction countingSortDigit(nums: number[], exp: number): void {\n    // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n    const counter = new Array(10).fill(0);\n    const n = nums.length;\n    // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    for (let i = 0; i < n; i++) {\n        const d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n        counter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n    }\n    // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n    for (let i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n    const res = new Array(n).fill(0);\n    for (let i = n - 1; i >= 0; i--) {\n        const d = digit(nums[i], exp);\n        const j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n        res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n        counter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (let i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nfunction radixSort(nums: number[]): void {\n    // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n    let m: number = Math.max(... nums);\n    // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n    for (let exp = 1; exp <= m; exp *= 10) {\n        // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u5373 exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n    }\n}\n
        radix_sort.dart
        /* \u83b7\u53d6\u5143\u7d20 _num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int _num, int exp) {\n  // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n  return (_num ~/ exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(List<int> nums, int exp) {\n  // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n  List<int> counter = List<int>.filled(10, 0);\n  int n = nums.length;\n  // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n  for (int i = 0; i < n; i++) {\n    int d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n    counter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n  }\n  // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n  for (int i = 1; i < 10; i++) {\n    counter[i] += counter[i - 1];\n  }\n  // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n  List<int> res = List<int>.filled(n, 0);\n  for (int i = n - 1; i >= 0; i--) {\n    int d = digit(nums[i], exp);\n    int j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n    res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n    counter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n  }\n  // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n  for (int i = 0; i < n; i++) nums[i] = res[i];\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nvoid radixSort(List<int> nums) {\n  // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n  // dart \u4e2d int \u7684\u957f\u5ea6\u662f 64 \u4f4d\u7684\n  int m = -1 << 63;\n  for (int _num in nums) if (_num > m) m = _num;\n  // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n  for (int exp = 1; exp <= m; exp *= 10)\n    // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n    // k = 1 -> exp = 1\n    // k = 2 -> exp = 10\n    // \u5373 exp = 10^(k-1)\n    countingSortDigit(nums, exp);\n}\n
        radix_sort.rs
        /* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfn digit(num: i32, exp: i32) -> usize {\n    // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n    return ((num / exp) % 10) as usize;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfn counting_sort_digit(nums: &mut [i32], exp: i32) {\n    // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n    let mut counter = [0; 10];\n    let n = nums.len();\n    // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    for i in 0..n {\n        let d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n        counter[d] += 1; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n    }\n    // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n    for i in 1..10 {\n        counter[i] += counter[i - 1];\n    }\n    // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n    let mut res = vec![0; n];\n    for i in (0..n).rev() {\n        let d = digit(nums[i], exp);\n        let j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n        res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n        counter[d] -= 1; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n    nums.copy_from_slice(&res);\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nfn radix_sort(nums: &mut [i32]) {\n    // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n    let m = *nums.into_iter().max().unwrap();\n    // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n    let mut exp = 1;\n    while exp <= m {\n        counting_sort_digit(nums, exp);\n        exp *= 10;\n    }\n}\n
        radix_sort.c
        /* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int num, int exp) {\n    // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n    return (num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(int nums[], int size, int exp) {\n    // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n    int *counter = (int *)malloc((sizeof(int) * 10));\n    memset(counter, 0, sizeof(int) * 10); // \u521d\u59cb\u5316\u4e3a 0 \u4ee5\u652f\u6301\u540e\u7eed\u5185\u5b58\u91ca\u653e\n    // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    for (int i = 0; i < size; i++) {\n        // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n        int d = digit(nums[i], exp);\n        // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n        counter[d]++;\n    }\n    // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n    int *res = (int *)malloc(sizeof(int) * size);\n    for (int i = size - 1; i >= 0; i--) {\n        int d = digit(nums[i], exp);\n        int j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n        res[j] = nums[i];       // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n        counter[d]--;           // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (int i = 0; i < size; i++) {\n        nums[i] = res[i];\n    }\n    // \u91ca\u653e\u5185\u5b58\n    free(res);\n    free(counter);\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nvoid radixSort(int nums[], int size) {\n    // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n    int max = INT32_MIN;\n    for (int i = 0; i < size; i++) {\n        if (nums[i] > max) {\n            max = nums[i];\n        }\n    }\n    // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n    for (int exp = 1; max >= exp; exp *= 10)\n        // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u5373 exp = 10^(k-1)\n        countingSortDigit(nums, size, exp);\n}\n
        radix_sort.kt
        /* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfun digit(num: Int, exp: Int): Int {\n    // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n    return (num / exp) % 10\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfun countingSortDigit(nums: IntArray, exp: Int) {\n    // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n    val counter = IntArray(10)\n    val n = nums.size\n    // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    for (i in 0..<n) {\n        val d = digit(nums[i], exp) // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n        counter[d]++                // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n    }\n    // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n    for (i in 1..9) {\n        counter[i] += counter[i - 1]\n    }\n    // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n    val res = IntArray(n)\n    for (i in n - 1 downTo 0) {\n        val d = digit(nums[i], exp)\n        val j = counter[d] - 1 // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n        res[j] = nums[i]       // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n        counter[d]--           // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (i in 0..<n)\n        nums[i] = res[i]\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nfun radixSort(nums: IntArray) {\n    // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n    var m = Int.MIN_VALUE\n    for (num in nums) if (num > m) m = num\n    var exp = 1\n    // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n    while (exp <= m) {\n        // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u5373 exp = 10^(k-1)\n        countingSortDigit(nums, exp)\n        exp *= 10\n    }\n}\n
        radix_sort.rb
        ### \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) ###\ndef digit(num, exp)\n  # \u8f6c\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n  (num / exp) % 10\nend\n\n### \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09###\ndef counting_sort_digit(nums, exp)\n  # \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n  counter = Array.new(10, 0)\n  n = nums.length\n  # \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n  for i in 0...n\n    d = digit(nums[i], exp) # \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n    counter[d] += 1 # \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n  end\n  # \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n  (1...10).each { |i| counter[i] += counter[i - 1] }\n  # \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n  res = Array.new(n, 0)\n  for i in (n - 1).downto(0)\n    d = digit(nums[i], exp)\n    j = counter[d] - 1 # \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n    res[j] = nums[i] # \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n    counter[d] -= 1 # \u5c06 d \u7684\u6570\u91cf\u51cf 1\n  end\n  # \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n  (0...n).each { |i| nums[i] = res[i] }\nend\n\n### \u57fa\u6570\u6392\u5e8f ###\ndef radix_sort(nums)\n  # \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n  m = nums.max\n  # \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n  exp = 1\n  while exp <= m\n    # \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n    # k = 1 -> exp = 1\n    # k = 2 -> exp = 10\n    # \u5373 exp = 10^(k-1)\n    counting_sort_digit(nums, exp)\n    exp *= 10\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u4e3a\u4ec0\u4e48\u4ece\u6700\u4f4e\u4f4d\u5f00\u59cb\u6392\u5e8f\uff1f

        \u5728\u8fde\u7eed\u7684\u6392\u5e8f\u8f6e\u6b21\u4e2d\uff0c\u540e\u4e00\u8f6e\u6392\u5e8f\u4f1a\u8986\u76d6\u524d\u4e00\u8f6e\u6392\u5e8f\u7684\u7ed3\u679c\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u5982\u679c\u7b2c\u4e00\u8f6e\u6392\u5e8f\u7ed3\u679c \\(a < b\\) \uff0c\u800c\u7b2c\u4e8c\u8f6e\u6392\u5e8f\u7ed3\u679c \\(a > b\\) \uff0c\u90a3\u4e48\u7b2c\u4e8c\u8f6e\u7684\u7ed3\u679c\u5c06\u53d6\u4ee3\u7b2c\u4e00\u8f6e\u7684\u7ed3\u679c\u3002\u7531\u4e8e\u6570\u5b57\u7684\u9ad8\u4f4d\u4f18\u5148\u7ea7\u9ad8\u4e8e\u4f4e\u4f4d\uff0c\u56e0\u6b64\u5e94\u8be5\u5148\u6392\u5e8f\u4f4e\u4f4d\u518d\u6392\u5e8f\u9ad8\u4f4d\u3002

        "},{"location":"chapter_sorting/radix_sort/#11102","title":"11.10.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"

        \u76f8\u8f83\u4e8e\u8ba1\u6570\u6392\u5e8f\uff0c\u57fa\u6570\u6392\u5e8f\u9002\u7528\u4e8e\u6570\u503c\u8303\u56f4\u8f83\u5927\u7684\u60c5\u51b5\uff0c\u4f46\u524d\u63d0\u662f\u6570\u636e\u5fc5\u987b\u53ef\u4ee5\u8868\u793a\u4e3a\u56fa\u5b9a\u4f4d\u6570\u7684\u683c\u5f0f\uff0c\u4e14\u4f4d\u6570\u4e0d\u80fd\u8fc7\u5927\u3002\u4f8b\u5982\uff0c\u6d6e\u70b9\u6570\u4e0d\u9002\u5408\u4f7f\u7528\u57fa\u6570\u6392\u5e8f\uff0c\u56e0\u4e3a\u5176\u4f4d\u6570 \\(k\\) \u8fc7\u5927\uff0c\u53ef\u80fd\u5bfc\u81f4\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(nk) \\gg O(n^2)\\) \u3002

        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nk)\\)\u3001\u975e\u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u8bbe\u6570\u636e\u91cf\u4e3a \\(n\\)\u3001\u6570\u636e\u4e3a \\(d\\) \u8fdb\u5236\u3001\u6700\u5927\u4f4d\u6570\u4e3a \\(k\\) \uff0c\u5219\u5bf9\u67d0\u4e00\u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\u4f7f\u7528 \\(O(n + d)\\) \u65f6\u95f4\uff0c\u6392\u5e8f\u6240\u6709 \\(k\\) \u4f4d\u4f7f\u7528 \\(O((n + d)k)\\) \u65f6\u95f4\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\\(d\\) \u548c \\(k\\) \u90fd\u76f8\u5bf9\u8f83\u5c0f\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u5411 \\(O(n)\\) \u3002
        • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n + d)\\)\u3001\u975e\u539f\u5730\u6392\u5e8f\uff1a\u4e0e\u8ba1\u6570\u6392\u5e8f\u76f8\u540c\uff0c\u57fa\u6570\u6392\u5e8f\u9700\u8981\u501f\u52a9\u957f\u5ea6\u4e3a \\(n\\) \u548c \\(d\\) \u7684\u6570\u7ec4 res \u548c counter \u3002
        • \u7a33\u5b9a\u6392\u5e8f\uff1a\u5f53\u8ba1\u6570\u6392\u5e8f\u7a33\u5b9a\u65f6\uff0c\u57fa\u6570\u6392\u5e8f\u4e5f\u7a33\u5b9a\uff1b\u5f53\u8ba1\u6570\u6392\u5e8f\u4e0d\u7a33\u5b9a\u65f6\uff0c\u57fa\u6570\u6392\u5e8f\u65e0\u6cd5\u4fdd\u8bc1\u5f97\u5230\u6b63\u786e\u7684\u6392\u5e8f\u7ed3\u679c\u3002
        "},{"location":"chapter_sorting/selection_sort/","title":"11.2 \u00a0 \u9009\u62e9\u6392\u5e8f","text":"

        \u9009\u62e9\u6392\u5e8f\uff08selection sort\uff09\u7684\u5de5\u4f5c\u539f\u7406\u975e\u5e38\u7b80\u5355\uff1a\u5f00\u542f\u4e00\u4e2a\u5faa\u73af\uff0c\u6bcf\u8f6e\u4ece\u672a\u6392\u5e8f\u533a\u95f4\u9009\u62e9\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u5c06\u5176\u653e\u5230\u5df2\u6392\u5e8f\u533a\u95f4\u7684\u672b\u5c3e\u3002

        \u8bbe\u6570\u7ec4\u7684\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u9009\u62e9\u6392\u5e8f\u7684\u7b97\u6cd5\u6d41\u7a0b\u5982\u56fe 11-2 \u6240\u793a\u3002

        1. \u521d\u59cb\u72b6\u6001\u4e0b\uff0c\u6240\u6709\u5143\u7d20\u672a\u6392\u5e8f\uff0c\u5373\u672a\u6392\u5e8f\uff08\u7d22\u5f15\uff09\u533a\u95f4\u4e3a \\([0, n-1]\\) \u3002
        2. \u9009\u53d6\u533a\u95f4 \\([0, n-1]\\) \u4e2d\u7684\u6700\u5c0f\u5143\u7d20\uff0c\u5c06\u5176\u4e0e\u7d22\u5f15 \\(0\\) \u5904\u7684\u5143\u7d20\u4ea4\u6362\u3002\u5b8c\u6210\u540e\uff0c\u6570\u7ec4\u524d 1 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002
        3. \u9009\u53d6\u533a\u95f4 \\([1, n-1]\\) \u4e2d\u7684\u6700\u5c0f\u5143\u7d20\uff0c\u5c06\u5176\u4e0e\u7d22\u5f15 \\(1\\) \u5904\u7684\u5143\u7d20\u4ea4\u6362\u3002\u5b8c\u6210\u540e\uff0c\u6570\u7ec4\u524d 2 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002
        4. \u4ee5\u6b64\u7c7b\u63a8\u3002\u7ecf\u8fc7 \\(n - 1\\) \u8f6e\u9009\u62e9\u4e0e\u4ea4\u6362\u540e\uff0c\u6570\u7ec4\u524d \\(n - 1\\) \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002
        5. \u4ec5\u5269\u7684\u4e00\u4e2a\u5143\u7d20\u5fc5\u5b9a\u662f\u6700\u5927\u5143\u7d20\uff0c\u65e0\u987b\u6392\u5e8f\uff0c\u56e0\u6b64\u6570\u7ec4\u6392\u5e8f\u5b8c\u6210\u3002
        <1><2><3><4><5><6><7><8><9><10><11>

        \u56fe 11-2 \u00a0 \u9009\u62e9\u6392\u5e8f\u6b65\u9aa4

        \u5728\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u7528 \\(k\\) \u6765\u8bb0\u5f55\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby selection_sort.py
        def selection_sort(nums: list[int]):\n    \"\"\"\u9009\u62e9\u6392\u5e8f\"\"\"\n    n = len(nums)\n    # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n    for i in range(n - 1):\n        # \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n        k = i\n        for j in range(i + 1, n):\n            if nums[j] < nums[k]:\n                k = j  # \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n        # \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n        nums[i], nums[k] = nums[k], nums[i]\n
        selection_sort.cpp
        /* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(vector<int> &nums) {\n    int n = nums.size();\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n        }\n        // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n        swap(nums[i], nums[k]);\n    }\n}\n
        selection_sort.java
        /* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(int[] nums) {\n    int n = nums.length;\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n        }\n        // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n        int temp = nums[i];\n        nums[i] = nums[k];\n        nums[k] = temp;\n    }\n}\n
        selection_sort.cs
        /* \u9009\u62e9\u6392\u5e8f */\nvoid SelectionSort(int[] nums) {\n    int n = nums.Length;\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n        }\n        // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n        (nums[k], nums[i]) = (nums[i], nums[k]);\n    }\n}\n
        selection_sort.go
        /* \u9009\u62e9\u6392\u5e8f */\nfunc selectionSort(nums []int) {\n    n := len(nums)\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n    for i := 0; i < n-1; i++ {\n        // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n        k := i\n        for j := i + 1; j < n; j++ {\n            if nums[j] < nums[k] {\n                // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n                k = j\n            }\n        }\n        // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n        nums[i], nums[k] = nums[k], nums[i]\n\n    }\n}\n
        selection_sort.swift
        /* \u9009\u62e9\u6392\u5e8f */\nfunc selectionSort(nums: inout [Int]) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n    for i in nums.indices.dropLast() {\n        // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n        var k = i\n        for j in nums.indices.dropFirst(i + 1) {\n            if nums[j] < nums[k] {\n                k = j // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n            }\n        }\n        // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n        nums.swapAt(i, k)\n    }\n}\n
        selection_sort.js
        /* \u9009\u62e9\u6392\u5e8f */\nfunction selectionSort(nums) {\n    let n = nums.length;\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n    for (let i = 0; i < n - 1; i++) {\n        // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n        let k = i;\n        for (let j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k]) {\n                k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n            }\n        }\n        // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n        [nums[i], nums[k]] = [nums[k], nums[i]];\n    }\n}\n
        selection_sort.ts
        /* \u9009\u62e9\u6392\u5e8f */\nfunction selectionSort(nums: number[]): void {\n    let n = nums.length;\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n    for (let i = 0; i < n - 1; i++) {\n        // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n        let k = i;\n        for (let j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k]) {\n                k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n            }\n        }\n        // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n        [nums[i], nums[k]] = [nums[k], nums[i]];\n    }\n}\n
        selection_sort.dart
        /* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(List<int> nums) {\n  int n = nums.length;\n  // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n  for (int i = 0; i < n - 1; i++) {\n    // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n    int k = i;\n    for (int j = i + 1; j < n; j++) {\n      if (nums[j] < nums[k]) k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n    }\n    // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n    int temp = nums[i];\n    nums[i] = nums[k];\n    nums[k] = temp;\n  }\n}\n
        selection_sort.rs
        /* \u9009\u62e9\u6392\u5e8f */\nfn selection_sort(nums: &mut [i32]) {\n    if nums.is_empty() {\n        return;\n    }\n    let n = nums.len();\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n    for i in 0..n - 1 {\n        // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n        let mut k = i;\n        for j in i + 1..n {\n            if nums[j] < nums[k] {\n                k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n            }\n        }\n        // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n        nums.swap(i, k);\n    }\n}\n
        selection_sort.c
        /* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(int nums[], int n) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n        }\n        // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n        int temp = nums[i];\n        nums[i] = nums[k];\n        nums[k] = temp;\n    }\n}\n
        selection_sort.kt
        /* \u9009\u62e9\u6392\u5e8f */\nfun selectionSort(nums: IntArray) {\n    val n = nums.size\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n    for (i in 0..<n - 1) {\n        var k = i\n        // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n        for (j in i + 1..<n) {\n            if (nums[j] < nums[k])\n                k = j // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n        }\n        // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n        val temp = nums[i]\n        nums[i] = nums[k]\n        nums[k] = temp\n    }\n}\n
        selection_sort.rb
        ### \u9009\u62e9\u6392\u5e8f ###\ndef selection_sort(nums)\n  n = nums.length\n  # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n  for i in 0...(n - 1)\n    # \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n    k = i\n    for j in (i + 1)...n\n      if nums[j] < nums[k]\n        k = j # \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n      end\n    end\n    # \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n    nums[i], nums[k] = nums[k], nums[i]\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/selection_sort/#1121","title":"11.2.1 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\)\u3001\u975e\u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u5916\u5faa\u73af\u5171 \\(n - 1\\) \u8f6e\uff0c\u7b2c\u4e00\u8f6e\u7684\u672a\u6392\u5e8f\u533a\u95f4\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u6700\u540e\u4e00\u8f6e\u7684\u672a\u6392\u5e8f\u533a\u95f4\u957f\u5ea6\u4e3a \\(2\\) \uff0c\u5373\u5404\u8f6e\u5916\u5faa\u73af\u5206\u522b\u5305\u542b \\(n\\)\u3001\\(n - 1\\)\u3001\\(\\dots\\)\u3001\\(3\\)\u3001\\(2\\) \u8f6e\u5185\u5faa\u73af\uff0c\u6c42\u548c\u4e3a \\(\\frac{(n - 1)(n + 2)}{2}\\) \u3002
        • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\)\u3001\u539f\u5730\u6392\u5e8f\uff1a\u6307\u9488 \\(i\\) \u548c \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002
        • \u975e\u7a33\u5b9a\u6392\u5e8f\uff1a\u5982\u56fe 11-3 \u6240\u793a\uff0c\u5143\u7d20 nums[i] \u6709\u53ef\u80fd\u88ab\u4ea4\u6362\u81f3\u4e0e\u5176\u76f8\u7b49\u7684\u5143\u7d20\u7684\u53f3\u8fb9\uff0c\u5bfc\u81f4\u4e24\u8005\u7684\u76f8\u5bf9\u987a\u5e8f\u53d1\u751f\u6539\u53d8\u3002

        \u56fe 11-3 \u00a0 \u9009\u62e9\u6392\u5e8f\u975e\u7a33\u5b9a\u793a\u4f8b

        "},{"location":"chapter_sorting/sorting_algorithm/","title":"11.1 \u00a0 \u6392\u5e8f\u7b97\u6cd5","text":"

        \u6392\u5e8f\u7b97\u6cd5\uff08sorting algorithm\uff09\u7528\u4e8e\u5bf9\u4e00\u7ec4\u6570\u636e\u6309\u7167\u7279\u5b9a\u987a\u5e8f\u8fdb\u884c\u6392\u5217\u3002\u6392\u5e8f\u7b97\u6cd5\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u56e0\u4e3a\u6709\u5e8f\u6570\u636e\u901a\u5e38\u80fd\u591f\u88ab\u66f4\u9ad8\u6548\u5730\u67e5\u627e\u3001\u5206\u6790\u548c\u5904\u7406\u3002

        \u5982\u56fe 11-1 \u6240\u793a\uff0c\u6392\u5e8f\u7b97\u6cd5\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u662f\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u7b49\u3002\u6392\u5e8f\u7684\u5224\u65ad\u89c4\u5219\u53ef\u6839\u636e\u9700\u6c42\u8bbe\u5b9a\uff0c\u5982\u6570\u5b57\u5927\u5c0f\u3001\u5b57\u7b26 ASCII \u7801\u987a\u5e8f\u6216\u81ea\u5b9a\u4e49\u89c4\u5219\u3002

        \u56fe 11-1 \u00a0 \u6570\u636e\u7c7b\u578b\u548c\u5224\u65ad\u89c4\u5219\u793a\u4f8b

        "},{"location":"chapter_sorting/sorting_algorithm/#1111","title":"11.1.1 \u00a0 \u8bc4\u4ef7\u7ef4\u5ea6","text":"

        \u8fd0\u884c\u6548\u7387\uff1a\u6211\u4eec\u671f\u671b\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5c3d\u91cf\u4f4e\uff0c\u4e14\u603b\u4f53\u64cd\u4f5c\u6570\u91cf\u8f83\u5c11\uff08\u65f6\u95f4\u590d\u6742\u5ea6\u4e2d\u7684\u5e38\u6570\u9879\u53d8\u5c0f\uff09\u3002\u5bf9\u4e8e\u5927\u6570\u636e\u91cf\u7684\u60c5\u51b5\uff0c\u8fd0\u884c\u6548\u7387\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002

        \u5c31\u5730\u6027\uff1a\u987e\u540d\u601d\u4e49\uff0c\u539f\u5730\u6392\u5e8f\u901a\u8fc7\u5728\u539f\u6570\u7ec4\u4e0a\u76f4\u63a5\u64cd\u4f5c\u5b9e\u73b0\u6392\u5e8f\uff0c\u65e0\u987b\u501f\u52a9\u989d\u5916\u7684\u8f85\u52a9\u6570\u7ec4\uff0c\u4ece\u800c\u8282\u7701\u5185\u5b58\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u539f\u5730\u6392\u5e8f\u7684\u6570\u636e\u642c\u8fd0\u64cd\u4f5c\u8f83\u5c11\uff0c\u8fd0\u884c\u901f\u5ea6\u4e5f\u66f4\u5feb\u3002

        \u7a33\u5b9a\u6027\uff1a\u7a33\u5b9a\u6392\u5e8f\u5728\u5b8c\u6210\u6392\u5e8f\u540e\uff0c\u76f8\u7b49\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u76f8\u5bf9\u987a\u5e8f\u4e0d\u53d1\u751f\u6539\u53d8\u3002

        \u7a33\u5b9a\u6392\u5e8f\u662f\u591a\u7ea7\u6392\u5e8f\u573a\u666f\u7684\u5fc5\u8981\u6761\u4ef6\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b58\u50a8\u5b66\u751f\u4fe1\u606f\u7684\u8868\u683c\uff0c\u7b2c 1 \u5217\u548c\u7b2c 2 \u5217\u5206\u522b\u662f\u59d3\u540d\u548c\u5e74\u9f84\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u975e\u7a33\u5b9a\u6392\u5e8f\u53ef\u80fd\u5bfc\u81f4\u8f93\u5165\u6570\u636e\u7684\u6709\u5e8f\u6027\u4e27\u5931\uff1a

        # \u8f93\u5165\u6570\u636e\u662f\u6309\u7167\u59d3\u540d\u6392\u5e8f\u597d\u7684\n# (name, age)\n  ('A', 19)\n  ('B', 18)\n  ('C', 21)\n  ('D', 19)\n  ('E', 23)\n\n# \u5047\u8bbe\u4f7f\u7528\u975e\u7a33\u5b9a\u6392\u5e8f\u7b97\u6cd5\u6309\u5e74\u9f84\u6392\u5e8f\u5217\u8868\uff0c\n# \u7ed3\u679c\u4e2d ('D', 19) \u548c ('A', 19) \u7684\u76f8\u5bf9\u4f4d\u7f6e\u6539\u53d8\uff0c\n# \u8f93\u5165\u6570\u636e\u6309\u59d3\u540d\u6392\u5e8f\u7684\u6027\u8d28\u4e22\u5931\n  ('B', 18)\n  ('D', 19)\n  ('A', 19)\n  ('C', 21)\n  ('E', 23)\n

        \u81ea\u9002\u5e94\u6027\uff1a\u81ea\u9002\u5e94\u6392\u5e8f\u80fd\u591f\u5229\u7528\u8f93\u5165\u6570\u636e\u5df2\u6709\u7684\u987a\u5e8f\u4fe1\u606f\u6765\u51cf\u5c11\u8ba1\u7b97\u91cf\uff0c\u8fbe\u5230\u66f4\u4f18\u7684\u65f6\u95f4\u6548\u7387\u3002\u81ea\u9002\u5e94\u6392\u5e8f\u7b97\u6cd5\u7684\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4f18\u4e8e\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u3002

        \u662f\u5426\u57fa\u4e8e\u6bd4\u8f83\uff1a\u57fa\u4e8e\u6bd4\u8f83\u7684\u6392\u5e8f\u4f9d\u8d56\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff08\\(<\\)\u3001\\(=\\)\u3001\\(>\\)\uff09\u6765\u5224\u65ad\u5143\u7d20\u7684\u76f8\u5bf9\u987a\u5e8f\uff0c\u4ece\u800c\u6392\u5e8f\u6574\u4e2a\u6570\u7ec4\uff0c\u7406\u8bba\u6700\u4f18\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002\u800c\u975e\u6bd4\u8f83\u6392\u5e8f\u4e0d\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe \\(O(n)\\) \uff0c\u4f46\u5176\u901a\u7528\u6027\u76f8\u5bf9\u8f83\u5dee\u3002

        "},{"location":"chapter_sorting/sorting_algorithm/#1112","title":"11.1.2 \u00a0 \u7406\u60f3\u6392\u5e8f\u7b97\u6cd5","text":"

        \u8fd0\u884c\u5feb\u3001\u539f\u5730\u3001\u7a33\u5b9a\u3001\u81ea\u9002\u5e94\u3001\u901a\u7528\u6027\u597d\u3002\u663e\u7136\uff0c\u8fc4\u4eca\u4e3a\u6b62\u5c1a\u672a\u53d1\u73b0\u517c\u5177\u4ee5\u4e0a\u6240\u6709\u7279\u6027\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u56e0\u6b64\uff0c\u5728\u9009\u62e9\u6392\u5e8f\u7b97\u6cd5\u65f6\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u6570\u636e\u7279\u70b9\u548c\u95ee\u9898\u9700\u6c42\u6765\u51b3\u5b9a\u3002

        \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u5171\u540c\u5b66\u4e60\u5404\u79cd\u6392\u5e8f\u7b97\u6cd5\uff0c\u5e76\u57fa\u4e8e\u4e0a\u8ff0\u8bc4\u4ef7\u7ef4\u5ea6\u5bf9\u5404\u4e2a\u6392\u5e8f\u7b97\u6cd5\u7684\u4f18\u7f3a\u70b9\u8fdb\u884c\u5206\u6790\u3002

        "},{"location":"chapter_sorting/summary/","title":"11.11 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_sorting/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u5192\u6ce1\u6392\u5e8f\u901a\u8fc7\u4ea4\u6362\u76f8\u90bb\u5143\u7d20\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u901a\u8fc7\u6dfb\u52a0\u4e00\u4e2a\u6807\u5fd7\u4f4d\u6765\u5b9e\u73b0\u63d0\u524d\u8fd4\u56de\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5192\u6ce1\u6392\u5e8f\u7684\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u5230 \\(O(n)\\) \u3002
        • \u63d2\u5165\u6392\u5e8f\u6bcf\u8f6e\u5c06\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u5143\u7d20\u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4\u7684\u6b63\u786e\u4f4d\u7f6e\uff0c\u4ece\u800c\u5b8c\u6210\u6392\u5e8f\u3002\u867d\u7136\u63d2\u5165\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u4f46\u7531\u4e8e\u5355\u5143\u64cd\u4f5c\u76f8\u5bf9\u8f83\u5c11\uff0c\u56e0\u6b64\u5728\u5c0f\u6570\u636e\u91cf\u7684\u6392\u5e8f\u4efb\u52a1\u4e2d\u975e\u5e38\u53d7\u6b22\u8fce\u3002
        • \u5feb\u901f\u6392\u5e8f\u57fa\u4e8e\u54e8\u5175\u5212\u5206\u64cd\u4f5c\u5b9e\u73b0\u6392\u5e8f\u3002\u5728\u54e8\u5175\u5212\u5206\u4e2d\uff0c\u6709\u53ef\u80fd\u6bcf\u6b21\u90fd\u9009\u53d6\u5230\u6700\u5dee\u7684\u57fa\u51c6\u6570\uff0c\u5bfc\u81f4\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(n^2)\\) \u3002\u5f15\u5165\u4e2d\u4f4d\u6570\u57fa\u51c6\u6570\u6216\u968f\u673a\u57fa\u51c6\u6570\u53ef\u4ee5\u964d\u4f4e\u8fd9\u79cd\u52a3\u5316\u7684\u6982\u7387\u3002\u901a\u8fc7\u4f18\u5148\u9012\u5f52\u8f83\u77ed\u5b50\u533a\u95f4\uff0c\u53ef\u6709\u6548\u51cf\u5c0f\u9012\u5f52\u6df1\u5ea6\uff0c\u5c06\u7a7a\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u5230 \\(O(\\log n)\\) \u3002
        • \u5f52\u5e76\u6392\u5e8f\u5305\u62ec\u5212\u5206\u548c\u5408\u5e76\u4e24\u4e2a\u9636\u6bb5\uff0c\u5178\u578b\u5730\u4f53\u73b0\u4e86\u5206\u6cbb\u7b56\u7565\u3002\u5728\u5f52\u5e76\u6392\u5e8f\u4e2d\uff0c\u6392\u5e8f\u6570\u7ec4\u9700\u8981\u521b\u5efa\u8f85\u52a9\u6570\u7ec4\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff1b\u7136\u800c\u6392\u5e8f\u94fe\u8868\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f18\u5316\u81f3 \\(O(1)\\) \u3002
        • \u6876\u6392\u5e8f\u5305\u542b\u4e09\u4e2a\u6b65\u9aa4\uff1a\u6570\u636e\u5206\u6876\u3001\u6876\u5185\u6392\u5e8f\u548c\u5408\u5e76\u7ed3\u679c\u3002\u5b83\u540c\u6837\u4f53\u73b0\u4e86\u5206\u6cbb\u7b56\u7565\uff0c\u9002\u7528\u4e8e\u6570\u636e\u4f53\u91cf\u5f88\u5927\u7684\u60c5\u51b5\u3002\u6876\u6392\u5e8f\u7684\u5173\u952e\u5728\u4e8e\u5bf9\u6570\u636e\u8fdb\u884c\u5e73\u5747\u5206\u914d\u3002
        • \u8ba1\u6570\u6392\u5e8f\u662f\u6876\u6392\u5e8f\u7684\u4e00\u4e2a\u7279\u4f8b\uff0c\u5b83\u901a\u8fc7\u7edf\u8ba1\u6570\u636e\u51fa\u73b0\u7684\u6b21\u6570\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u8ba1\u6570\u6392\u5e8f\u9002\u7528\u4e8e\u6570\u636e\u91cf\u5927\u4f46\u6570\u636e\u8303\u56f4\u6709\u9650\u7684\u60c5\u51b5\uff0c\u5e76\u4e14\u8981\u6c42\u6570\u636e\u80fd\u591f\u8f6c\u6362\u4e3a\u6b63\u6574\u6570\u3002
        • \u57fa\u6570\u6392\u5e8f\u901a\u8fc7\u9010\u4f4d\u6392\u5e8f\u6765\u5b9e\u73b0\u6570\u636e\u6392\u5e8f\uff0c\u8981\u6c42\u6570\u636e\u80fd\u591f\u8868\u793a\u4e3a\u56fa\u5b9a\u4f4d\u6570\u7684\u6570\u5b57\u3002
        • \u603b\u7684\u6765\u8bf4\uff0c\u6211\u4eec\u5e0c\u671b\u627e\u5230\u4e00\u79cd\u6392\u5e8f\u7b97\u6cd5\uff0c\u5177\u6709\u9ad8\u6548\u7387\u3001\u7a33\u5b9a\u3001\u539f\u5730\u4ee5\u53ca\u81ea\u9002\u5e94\u6027\u7b49\u4f18\u70b9\u3002\u7136\u800c\uff0c\u6b63\u5982\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u4e00\u6837\uff0c\u6ca1\u6709\u4e00\u79cd\u6392\u5e8f\u7b97\u6cd5\u80fd\u591f\u540c\u65f6\u6ee1\u8db3\u6240\u6709\u8fd9\u4e9b\u6761\u4ef6\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u6570\u636e\u7684\u7279\u6027\u6765\u9009\u62e9\u5408\u9002\u7684\u6392\u5e8f\u7b97\u6cd5\u3002
        • \u56fe 11-19 \u5bf9\u6bd4\u4e86\u4e3b\u6d41\u6392\u5e8f\u7b97\u6cd5\u7684\u6548\u7387\u3001\u7a33\u5b9a\u6027\u3001\u5c31\u5730\u6027\u548c\u81ea\u9002\u5e94\u6027\u7b49\u3002

        \u56fe 11-19 \u00a0 \u6392\u5e8f\u7b97\u6cd5\u5bf9\u6bd4

        "},{"location":"chapter_sorting/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u6392\u5e8f\u7b97\u6cd5\u7a33\u5b9a\u6027\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u662f\u5fc5\u9700\u7684\uff1f

        \u5728\u73b0\u5b9e\u4e2d\uff0c\u6211\u4eec\u6709\u53ef\u80fd\u57fa\u4e8e\u5bf9\u8c61\u7684\u67d0\u4e2a\u5c5e\u6027\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u5b66\u751f\u6709\u59d3\u540d\u548c\u8eab\u9ad8\u4e24\u4e2a\u5c5e\u6027\uff0c\u6211\u4eec\u5e0c\u671b\u5b9e\u73b0\u4e00\u4e2a\u591a\u7ea7\u6392\u5e8f\uff1a\u5148\u6309\u7167\u59d3\u540d\u8fdb\u884c\u6392\u5e8f\uff0c\u5f97\u5230 (A, 180) (B, 185) (C, 170) (D, 170) \uff1b\u518d\u5bf9\u8eab\u9ad8\u8fdb\u884c\u6392\u5e8f\u3002\u7531\u4e8e\u6392\u5e8f\u7b97\u6cd5\u4e0d\u7a33\u5b9a\uff0c\u56e0\u6b64\u53ef\u80fd\u5f97\u5230 (D, 170) (C, 170) (A, 180) (B, 185) \u3002

        \u53ef\u4ee5\u53d1\u73b0\uff0c\u5b66\u751f D \u548c C \u7684\u4f4d\u7f6e\u53d1\u751f\u4e86\u4ea4\u6362\uff0c\u59d3\u540d\u7684\u6709\u5e8f\u6027\u88ab\u7834\u574f\u4e86\uff0c\u800c\u8fd9\u662f\u6211\u4eec\u4e0d\u5e0c\u671b\u770b\u5230\u7684\u3002

        Q\uff1a\u54e8\u5175\u5212\u5206\u4e2d\u201c\u4ece\u53f3\u5f80\u5de6\u67e5\u627e\u201d\u4e0e\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\u7684\u987a\u5e8f\u53ef\u4ee5\u4ea4\u6362\u5417\uff1f

        \u4e0d\u884c\uff0c\u5f53\u6211\u4eec\u4ee5\u6700\u5de6\u7aef\u5143\u7d20\u4e3a\u57fa\u51c6\u6570\u65f6\uff0c\u5fc5\u987b\u5148\u201c\u4ece\u53f3\u5f80\u5de6\u67e5\u627e\u201d\u518d\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\u3002\u8fd9\u4e2a\u7ed3\u8bba\u6709\u4e9b\u53cd\u76f4\u89c9\uff0c\u6211\u4eec\u6765\u5256\u6790\u4e00\u4e0b\u539f\u56e0\u3002

        \u54e8\u5175\u5212\u5206 partition() \u7684\u6700\u540e\u4e00\u6b65\u662f\u4ea4\u6362 nums[left] \u548c nums[i] \u3002\u5b8c\u6210\u4ea4\u6362\u540e\uff0c\u57fa\u51c6\u6570\u5de6\u8fb9\u7684\u5143\u7d20\u90fd <= \u57fa\u51c6\u6570\uff0c\u8fd9\u5c31\u8981\u6c42\u6700\u540e\u4e00\u6b65\u4ea4\u6362\u524d nums[left] >= nums[i] \u5fc5\u987b\u6210\u7acb\u3002\u5047\u8bbe\u6211\u4eec\u5148\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\uff0c\u90a3\u4e48\u5982\u679c\u627e\u4e0d\u5230\u6bd4\u57fa\u51c6\u6570\u66f4\u5927\u7684\u5143\u7d20\uff0c\u5219\u4f1a\u5728 i == j \u65f6\u8df3\u51fa\u5faa\u73af\uff0c\u6b64\u65f6\u53ef\u80fd nums[j] == nums[i] > nums[left]\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6b64\u65f6\u6700\u540e\u4e00\u6b65\u4ea4\u6362\u64cd\u4f5c\u4f1a\u628a\u4e00\u4e2a\u6bd4\u57fa\u51c6\u6570\u66f4\u5927\u7684\u5143\u7d20\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\uff0c\u5bfc\u81f4\u54e8\u5175\u5212\u5206\u5931\u8d25\u3002

        \u4e3e\u4e2a\u4f8b\u5b50\uff0c\u7ed9\u5b9a\u6570\u7ec4 [0, 0, 0, 0, 1] \uff0c\u5982\u679c\u5148\u201c\u4ece\u5de6\u5411\u53f3\u67e5\u627e\u201d\uff0c\u54e8\u5175\u5212\u5206\u540e\u6570\u7ec4\u4e3a [1, 0, 0, 0, 0] \uff0c\u8fd9\u4e2a\u7ed3\u679c\u662f\u4e0d\u6b63\u786e\u7684\u3002

        \u518d\u6df1\u5165\u601d\u8003\u4e00\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u9009\u62e9 nums[right] \u4e3a\u57fa\u51c6\u6570\uff0c\u90a3\u4e48\u6b63\u597d\u53cd\u8fc7\u6765\uff0c\u5fc5\u987b\u5148\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\u3002

        Q\uff1a\u5173\u4e8e\u5feb\u901f\u6392\u5e8f\u7684\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff0c\u4e3a\u4ec0\u4e48\u9009\u77ed\u7684\u6570\u7ec4\u80fd\u4fdd\u8bc1\u9012\u5f52\u6df1\u5ea6\u4e0d\u8d85\u8fc7 \\(\\log n\\) \uff1f

        \u9012\u5f52\u6df1\u5ea6\u5c31\u662f\u5f53\u524d\u672a\u8fd4\u56de\u7684\u9012\u5f52\u65b9\u6cd5\u7684\u6570\u91cf\u3002\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u6211\u4eec\u5c06\u539f\u6570\u7ec4\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\u3002\u5728\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\u540e\uff0c\u5411\u4e0b\u9012\u5f52\u7684\u5b50\u6570\u7ec4\u957f\u5ea6\u6700\u5927\u4e3a\u539f\u6570\u7ec4\u957f\u5ea6\u7684\u4e00\u534a\u3002\u5047\u8bbe\u6700\u5dee\u60c5\u51b5\uff0c\u4e00\u76f4\u4e3a\u4e00\u534a\u957f\u5ea6\uff0c\u90a3\u4e48\u6700\u7ec8\u7684\u9012\u5f52\u6df1\u5ea6\u5c31\u662f \\(\\log n\\) \u3002

        \u56de\u987e\u539f\u59cb\u7684\u5feb\u901f\u6392\u5e8f\uff0c\u6211\u4eec\u6709\u53ef\u80fd\u4f1a\u8fde\u7eed\u5730\u9012\u5f52\u957f\u5ea6\u8f83\u5927\u7684\u6570\u7ec4\uff0c\u6700\u5dee\u60c5\u51b5\u4e0b\u4e3a \\(n\\)\u3001\\(n - 1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \uff0c\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \u3002\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u51fa\u73b0\u3002

        Q\uff1a\u5f53\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u76f8\u7b49\u65f6\uff0c\u5feb\u901f\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(n^2)\\) \u5417\uff1f\u8be5\u5982\u4f55\u5904\u7406\u8fd9\u79cd\u9000\u5316\u60c5\u51b5\uff1f

        \u662f\u7684\u3002\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u8003\u8651\u901a\u8fc7\u54e8\u5175\u5212\u5206\u5c06\u6570\u7ec4\u5212\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\uff1a\u5c0f\u4e8e\u3001\u7b49\u4e8e\u3001\u5927\u4e8e\u57fa\u51c6\u6570\u3002\u4ec5\u5411\u4e0b\u9012\u5f52\u5c0f\u4e8e\u548c\u5927\u4e8e\u7684\u4e24\u90e8\u5206\u3002\u5728\u8be5\u65b9\u6cd5\u4e0b\uff0c\u8f93\u5165\u5143\u7d20\u5168\u90e8\u76f8\u7b49\u7684\u6570\u7ec4\uff0c\u4ec5\u4e00\u8f6e\u54e8\u5175\u5212\u5206\u5373\u53ef\u5b8c\u6210\u6392\u5e8f\u3002

        Q\uff1a\u6876\u6392\u5e8f\u7684\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\u4ec0\u4e48\u662f \\(O(n^2)\\) \uff1f

        \u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u5143\u7d20\u88ab\u5206\u81f3\u540c\u4e00\u4e2a\u6876\u4e2d\u3002\u5982\u679c\u6211\u4eec\u91c7\u7528\u4e00\u4e2a \\(O(n^2)\\) \u7b97\u6cd5\u6765\u6392\u5e8f\u8fd9\u4e9b\u5143\u7d20\uff0c\u5219\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002

        "},{"location":"chapter_stack_and_queue/","title":"\u7b2c 5 \u7ae0 \u00a0 \u6808\u4e0e\u961f\u5217","text":"

        Abstract

        \u6808\u5982\u540c\u53e0\u732b\u732b\uff0c\u800c\u961f\u5217\u5c31\u50cf\u732b\u732b\u6392\u961f\u3002

        \u4e24\u8005\u5206\u522b\u4ee3\u8868\u5148\u5165\u540e\u51fa\u548c\u5148\u5165\u5148\u51fa\u7684\u903b\u8f91\u5173\u7cfb\u3002

        "},{"location":"chapter_stack_and_queue/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 5.1 \u00a0 \u6808
        • 5.2 \u00a0 \u961f\u5217
        • 5.3 \u00a0 \u53cc\u5411\u961f\u5217
        • 5.4 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_stack_and_queue/deque/","title":"5.3 \u00a0 \u53cc\u5411\u961f\u5217","text":"

        \u5728\u961f\u5217\u4e2d\uff0c\u6211\u4eec\u4ec5\u80fd\u5220\u9664\u5934\u90e8\u5143\u7d20\u6216\u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u3002\u5982\u56fe 5-7 \u6240\u793a\uff0c\u53cc\u5411\u961f\u5217\uff08double-ended queue\uff09\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\uff0c\u5141\u8bb8\u5728\u5934\u90e8\u548c\u5c3e\u90e8\u6267\u884c\u5143\u7d20\u7684\u6dfb\u52a0\u6216\u5220\u9664\u64cd\u4f5c\u3002

        \u56fe 5-7 \u00a0 \u53cc\u5411\u961f\u5217\u7684\u64cd\u4f5c

        "},{"location":"chapter_stack_and_queue/deque/#531","title":"5.3.1 \u00a0 \u53cc\u5411\u961f\u5217\u5e38\u7528\u64cd\u4f5c","text":"

        \u53cc\u5411\u961f\u5217\u7684\u5e38\u7528\u64cd\u4f5c\u5982\u8868 5-3 \u6240\u793a\uff0c\u5177\u4f53\u7684\u65b9\u6cd5\u540d\u79f0\u9700\u8981\u6839\u636e\u6240\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002

        \u8868 5-3 \u00a0 \u53cc\u5411\u961f\u5217\u64cd\u4f5c\u6548\u7387

        \u65b9\u6cd5\u540d \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push_first() \u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u961f\u9996 \\(O(1)\\) push_last() \u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u961f\u5c3e \\(O(1)\\) pop_first() \u5220\u9664\u961f\u9996\u5143\u7d20 \\(O(1)\\) pop_last() \u5220\u9664\u961f\u5c3e\u5143\u7d20 \\(O(1)\\) peek_first() \u8bbf\u95ee\u961f\u9996\u5143\u7d20 \\(O(1)\\) peek_last() \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 \\(O(1)\\)

        \u540c\u6837\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u4e2d\u5df2\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\u7c7b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby deque.py
        from collections import deque\n\n# \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217\ndeq: deque[int] = deque()\n\n# \u5143\u7d20\u5165\u961f\ndeq.append(2)      # \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeq.append(5)\ndeq.append(4)\ndeq.appendleft(3)  # \u6dfb\u52a0\u81f3\u961f\u9996\ndeq.appendleft(1)\n\n# \u8bbf\u95ee\u5143\u7d20\nfront: int = deq[0]  # \u961f\u9996\u5143\u7d20\nrear: int = deq[-1]  # \u961f\u5c3e\u5143\u7d20\n\n# \u5143\u7d20\u51fa\u961f\npop_front: int = deq.popleft()  # \u961f\u9996\u5143\u7d20\u51fa\u961f\npop_rear: int = deq.pop()       # \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n# \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\nsize: int = len(deq)\n\n# \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = len(deq) == 0\n
        deque.cpp
        /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\ndeque<int> deque;\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.push_back(2);   // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3);  // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.push_front(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nint front = deque.front(); // \u961f\u9996\u5143\u7d20\nint back = deque.back();   // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\ndeque.pop_front();  // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.pop_back();   // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.size();\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty = deque.empty();\n
        deque.java
        /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\nDeque<Integer> deque = new LinkedList<>();\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.offerLast(2);   // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.offerLast(5);\ndeque.offerLast(4);\ndeque.offerFirst(3);  // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.offerFirst(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.peekFirst();  // \u961f\u9996\u5143\u7d20\nint peekLast = deque.peekLast();    // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\nint popFirst = deque.pollFirst();  // \u961f\u9996\u5143\u7d20\u51fa\u961f\nint popLast = deque.pollLast();    // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.size();\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = deque.isEmpty();\n
        deque.cs
        /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 C# \u4e2d\uff0c\u5c06\u94fe\u8868 LinkedList \u770b\u4f5c\u53cc\u5411\u961f\u5217\u6765\u4f7f\u7528\nLinkedList<int> deque = new();\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.AddLast(2);   // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.AddLast(5);\ndeque.AddLast(4);\ndeque.AddFirst(3);  // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.AddFirst(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.First.Value;  // \u961f\u9996\u5143\u7d20\nint peekLast = deque.Last.Value;    // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\ndeque.RemoveFirst();  // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.RemoveLast();   // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.Count;\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = deque.Count == 0;\n
        deque_test.go
        /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 Go \u4e2d\uff0c\u5c06 list \u4f5c\u4e3a\u53cc\u5411\u961f\u5217\u4f7f\u7528\ndeque := list.New()\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.PushBack(2)      // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.PushBack(5)\ndeque.PushBack(4)\ndeque.PushFront(3)     // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.PushFront(1)\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nfront := deque.Front() // \u961f\u9996\u5143\u7d20\nrear := deque.Back()   // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\ndeque.Remove(front)    // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.Remove(rear)     // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nsize := deque.Len()\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty := deque.Len() == 0\n
        deque.swift
        /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// Swift \u6ca1\u6709\u5185\u7f6e\u7684\u53cc\u5411\u961f\u5217\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u53cc\u5411\u961f\u5217\u6765\u4f7f\u7528\nvar deque: [Int] = []\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.append(2) // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.append(5)\ndeque.append(4)\ndeque.insert(3, at: 0) // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.insert(1, at: 0)\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nlet peekFirst = deque.first! // \u961f\u9996\u5143\u7d20\nlet peekLast = deque.last! // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u4f7f\u7528 Array \u6a21\u62df\u65f6 popFirst \u7684\u590d\u6742\u5ea6\u4e3a O(n)\nlet popFirst = deque.removeFirst() // \u961f\u9996\u5143\u7d20\u51fa\u961f\nlet popLast = deque.removeLast() // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = deque.count\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = deque.isEmpty\n
        deque.js
        /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// JavaScript \u6ca1\u6709\u5185\u7f6e\u7684\u53cc\u7aef\u961f\u5217\uff0c\u53ea\u80fd\u628a Array \u5f53\u4f5c\u53cc\u7aef\u961f\u5217\u6765\u4f7f\u7528\nconst deque = [];\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cunshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\ndeque.unshift(3);\ndeque.unshift(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nconst peekFirst = deque[0];\nconst peekLast = deque[deque.length - 1];\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst popFront = deque.shift();\nconst popBack = deque.pop();\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = deque.length;\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst isEmpty = size === 0;\n
        deque.ts
        /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// TypeScript \u6ca1\u6709\u5185\u7f6e\u7684\u53cc\u7aef\u961f\u5217\uff0c\u53ea\u80fd\u628a Array \u5f53\u4f5c\u53cc\u7aef\u961f\u5217\u6765\u4f7f\u7528\nconst deque: number[] = [];\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cunshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\ndeque.unshift(3);\ndeque.unshift(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nconst peekFirst: number = deque[0];\nconst peekLast: number = deque[deque.length - 1];\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst popFront: number = deque.shift() as number;\nconst popBack: number = deque.pop() as number;\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nconst size: number = deque.length;\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst isEmpty: boolean = size === 0;\n
        deque.dart
        /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 Dart \u4e2d\uff0cQueue \u88ab\u5b9a\u4e49\u4e3a\u53cc\u5411\u961f\u5217\nQueue<int> deque = Queue<int>();\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.addLast(2);  // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.addLast(5);\ndeque.addLast(4);\ndeque.addFirst(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.addFirst(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.first; // \u961f\u9996\u5143\u7d20\nint peekLast = deque.last;   // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\nint popFirst = deque.removeFirst(); // \u961f\u9996\u5143\u7d20\u51fa\u961f\nint popLast = deque.removeLast();   // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.length;\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = deque.isEmpty;\n
        deque.rs
        /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\nlet mut deque: VecDeque<u32> = VecDeque::new();\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.push_back(2);  // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.push_front(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nif let Some(front) = deque.front() { // \u961f\u9996\u5143\u7d20\n}\nif let Some(rear) = deque.back() {   // \u961f\u5c3e\u5143\u7d20\n}\n\n/* \u5143\u7d20\u51fa\u961f */\nif let Some(pop_front) = deque.pop_front() { // \u961f\u9996\u5143\u7d20\u51fa\u961f\n}\nif let Some(pop_rear) = deque.pop_back() {   // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n}\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = deque.len();\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = deque.is_empty();\n
        deque.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e\u53cc\u5411\u961f\u5217\n
        deque.kt
        /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\nval deque = LinkedList<Int>()\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.offerLast(2)  // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.offerLast(5)\ndeque.offerLast(4)\ndeque.offerFirst(3) // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.offerFirst(1)\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nval peekFirst = deque.peekFirst() // \u961f\u9996\u5143\u7d20\nval peekLast = deque.peekLast()   // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\nval popFirst = deque.pollFirst() // \u961f\u9996\u5143\u7d20\u51fa\u961f\nval popLast = deque.pollLast()   // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nval size = deque.size\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nval isEmpty = deque.isEmpty()\n
        deque.rb
        # \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217\n# Ruby \u6ca1\u6709\u5185\u76f4\u7684\u53cc\u7aef\u961f\u5217\uff0c\u53ea\u80fd\u628a Array \u5f53\u4f5c\u53cc\u7aef\u961f\u5217\u6765\u4f7f\u7528\ndeque = []\n\n# \u5143\u7d20\u5982\u961f\ndeque << 2\ndeque << 5\ndeque << 4\n# \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cArray#unshift \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\ndeque.unshift(3)\ndeque.unshift(1)\n\n# \u8bbf\u95ee\u5143\u7d20\npeek_first = deque.first\npeek_last = deque.last\n\n# \u5143\u7d20\u51fa\u961f\n# \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0c Array#shift \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\npop_front = deque.shift\npop_back = deque.pop\n\n# \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\nsize = deque.length\n\n# \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\nis_empty = size.zero?\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_stack_and_queue/deque/#532","title":"5.3.2 \u00a0 \u53cc\u5411\u961f\u5217\u5b9e\u73b0 *","text":"

        \u53cc\u5411\u961f\u5217\u7684\u5b9e\u73b0\u4e0e\u961f\u5217\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u9009\u62e9\u94fe\u8868\u6216\u6570\u7ec4\u4f5c\u4e3a\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002

        "},{"location":"chapter_stack_and_queue/deque/#1","title":"1. \u00a0 \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u7684\u5b9e\u73b0","text":"

        \u56de\u987e\u4e0a\u4e00\u8282\u5185\u5bb9\uff0c\u6211\u4eec\u4f7f\u7528\u666e\u901a\u5355\u5411\u94fe\u8868\u6765\u5b9e\u73b0\u961f\u5217\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u5220\u9664\u5934\u8282\u70b9\uff08\u5bf9\u5e94\u51fa\u961f\u64cd\u4f5c\uff09\u548c\u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0\u65b0\u8282\u70b9\uff08\u5bf9\u5e94\u5165\u961f\u64cd\u4f5c\uff09\u3002

        \u5bf9\u4e8e\u53cc\u5411\u961f\u5217\u800c\u8a00\uff0c\u5934\u90e8\u548c\u5c3e\u90e8\u90fd\u53ef\u4ee5\u6267\u884c\u5165\u961f\u548c\u51fa\u961f\u64cd\u4f5c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u53cc\u5411\u961f\u5217\u9700\u8981\u5b9e\u73b0\u53e6\u4e00\u4e2a\u5bf9\u79f0\u65b9\u5411\u7684\u64cd\u4f5c\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u91c7\u7528\u201c\u53cc\u5411\u94fe\u8868\u201d\u4f5c\u4e3a\u53cc\u5411\u961f\u5217\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002

        \u5982\u56fe 5-8 \u6240\u793a\uff0c\u6211\u4eec\u5c06\u53cc\u5411\u94fe\u8868\u7684\u5934\u8282\u70b9\u548c\u5c3e\u8282\u70b9\u89c6\u4e3a\u53cc\u5411\u961f\u5217\u7684\u961f\u9996\u548c\u961f\u5c3e\uff0c\u540c\u65f6\u5b9e\u73b0\u5728\u4e24\u7aef\u6dfb\u52a0\u548c\u5220\u9664\u8282\u70b9\u7684\u529f\u80fd\u3002

        LinkedListDequepush_last()push_first()pop_last()pop_first()

        \u56fe 5-8 \u00a0 \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c

        \u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linkedlist_deque.py
        class ListNode:\n    \"\"\"\u53cc\u5411\u94fe\u8868\u8282\u70b9\"\"\"\n\n    def __init__(self, val: int):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        self.val: int = val\n        self.next: ListNode | None = None  # \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n        self.prev: ListNode | None = None  # \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\nclass LinkedListDeque:\n    \"\"\"\u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\"\"\"\n\n    def __init__(self):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        self._front: ListNode | None = None  # \u5934\u8282\u70b9 front\n        self._rear: ListNode | None = None  # \u5c3e\u8282\u70b9 rear\n        self._size: int = 0  # \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n    def size(self) -> int:\n        \"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n        return self._size == 0\n\n    def push(self, num: int, is_front: bool):\n        \"\"\"\u5165\u961f\u64cd\u4f5c\"\"\"\n        node = ListNode(num)\n        # \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n        if self.is_empty():\n            self._front = self._rear = node\n        # \u961f\u9996\u5165\u961f\u64cd\u4f5c\n        elif is_front:\n            # \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n            self._front.prev = node\n            node.next = self._front\n            self._front = node  # \u66f4\u65b0\u5934\u8282\u70b9\n        # \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n        else:\n            # \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n            self._rear.next = node\n            node.prev = self._rear\n            self._rear = node  # \u66f4\u65b0\u5c3e\u8282\u70b9\n        self._size += 1  # \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n\n    def push_first(self, num: int):\n        \"\"\"\u961f\u9996\u5165\u961f\"\"\"\n        self.push(num, True)\n\n    def push_last(self, num: int):\n        \"\"\"\u961f\u5c3e\u5165\u961f\"\"\"\n        self.push(num, False)\n\n    def pop(self, is_front: bool) -> int:\n        \"\"\"\u51fa\u961f\u64cd\u4f5c\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        # \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n        if is_front:\n            val: int = self._front.val  # \u6682\u5b58\u5934\u8282\u70b9\u503c\n            # \u5220\u9664\u5934\u8282\u70b9\n            fnext: ListNode | None = self._front.next\n            if fnext is not None:\n                fnext.prev = None\n                self._front.next = None\n            self._front = fnext  # \u66f4\u65b0\u5934\u8282\u70b9\n        # \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n        else:\n            val: int = self._rear.val  # \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n            # \u5220\u9664\u5c3e\u8282\u70b9\n            rprev: ListNode | None = self._rear.prev\n            if rprev is not None:\n                rprev.next = None\n                self._rear.prev = None\n            self._rear = rprev  # \u66f4\u65b0\u5c3e\u8282\u70b9\n        self._size -= 1  # \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n        return val\n\n    def pop_first(self) -> int:\n        \"\"\"\u961f\u9996\u51fa\u961f\"\"\"\n        return self.pop(True)\n\n    def pop_last(self) -> int:\n        \"\"\"\u961f\u5c3e\u51fa\u961f\"\"\"\n        return self.pop(False)\n\n    def peek_first(self) -> int:\n        \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        return self._front.val\n\n    def peek_last(self) -> int:\n        \"\"\"\u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        return self._rear.val\n\n    def to_array(self) -> list[int]:\n        \"\"\"\u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\"\"\"\n        node = self._front\n        res = [0] * self.size()\n        for i in range(self.size()):\n            res[i] = node.val\n            node = node.next\n        return res\n
        linkedlist_deque.cpp
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nstruct DoublyListNode {\n    int val;              // \u8282\u70b9\u503c\n    DoublyListNode *next; // \u540e\u7ee7\u8282\u70b9\u6307\u9488\n    DoublyListNode *prev; // \u524d\u9a71\u8282\u70b9\u6307\u9488\n    DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {\n    }\n};\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n  private:\n    DoublyListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n    int queSize = 0;              // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n  public:\n    /* \u6784\u9020\u65b9\u6cd5 */\n    LinkedListDeque() : front(nullptr), rear(nullptr) {\n    }\n\n    /* \u6790\u6784\u65b9\u6cd5 */\n    ~LinkedListDeque() {\n        // \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\n        DoublyListNode *pre, *cur = front;\n        while (cur != nullptr) {\n            pre = cur;\n            cur = cur->next;\n            delete pre;\n        }\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    int size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    bool isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u5165\u961f\u64cd\u4f5c */\n    void push(int num, bool isFront) {\n        DoublyListNode *node = new DoublyListNode(num);\n        // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n        if (isEmpty())\n            front = rear = node;\n        // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n        else if (isFront) {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n            front->prev = node;\n            node->next = front;\n            front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n        // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n        } else {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n            rear->next = node;\n            node->prev = rear;\n            rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n        queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    void pushFirst(int num) {\n        push(num, true);\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    void pushLast(int num) {\n        push(num, false);\n    }\n\n    /* \u51fa\u961f\u64cd\u4f5c */\n    int pop(bool isFront) {\n        if (isEmpty())\n            throw out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\n        int val;\n        // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n        if (isFront) {\n            val = front->val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n            // \u5220\u9664\u5934\u8282\u70b9\n            DoublyListNode *fNext = front->next;\n            if (fNext != nullptr) {\n                fNext->prev = nullptr;\n                front->next = nullptr;\n            }\n            delete front;\n            front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n        // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n        } else {\n            val = rear->val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n            // \u5220\u9664\u5c3e\u8282\u70b9\n            DoublyListNode *rPrev = rear->prev;\n            if (rPrev != nullptr) {\n                rPrev->next = nullptr;\n                rear->prev = nullptr;\n            }\n            delete rear;\n            rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n        queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n        return val;\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    int popFirst() {\n        return pop(true);\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    int popLast() {\n        return pop(false);\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    int peekFirst() {\n        if (isEmpty())\n            throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n        return front->val;\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    int peekLast() {\n        if (isEmpty())\n            throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n        return rear->val;\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    vector<int> toVector() {\n        DoublyListNode *node = front;\n        vector<int> res(size());\n        for (int i = 0; i < res.size(); i++) {\n            res[i] = node->val;\n            node = node->next;\n        }\n        return res;\n    }\n};\n
        linkedlist_deque.java
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n    int val; // \u8282\u70b9\u503c\n    ListNode next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n    ListNode prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\n    ListNode(int val) {\n        this.val = val;\n        prev = next = null;\n    }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n    private ListNode front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n    private int queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n    public LinkedListDeque() {\n        front = rear = null;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    public int size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u5165\u961f\u64cd\u4f5c */\n    private void push(int num, boolean isFront) {\n        ListNode node = new ListNode(num);\n        // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n        if (isEmpty())\n            front = rear = node;\n        // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n        else if (isFront) {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n            front.prev = node;\n            node.next = front;\n            front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n        // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n        } else {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n            rear.next = node;\n            node.prev = rear;\n            rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n        queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    public void pushFirst(int num) {\n        push(num, true);\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    public void pushLast(int num) {\n        push(num, false);\n    }\n\n    /* \u51fa\u961f\u64cd\u4f5c */\n    private int pop(boolean isFront) {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        int val;\n        // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n        if (isFront) {\n            val = front.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n            // \u5220\u9664\u5934\u8282\u70b9\n            ListNode fNext = front.next;\n            if (fNext != null) {\n                fNext.prev = null;\n                front.next = null;\n            }\n            front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n        // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n        } else {\n            val = rear.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n            // \u5220\u9664\u5c3e\u8282\u70b9\n            ListNode rPrev = rear.prev;\n            if (rPrev != null) {\n                rPrev.next = null;\n                rear.prev = null;\n            }\n            rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n        queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n        return val;\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    public int popFirst() {\n        return pop(true);\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    public int popLast() {\n        return pop(false);\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    public int peekFirst() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return front.val;\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    public int peekLast() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return rear.val;\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    public int[] toArray() {\n        ListNode node = front;\n        int[] res = new int[size()];\n        for (int i = 0; i < res.length; i++) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_deque.cs
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode(int val) {\n    public int val = val;       // \u8282\u70b9\u503c\n    public ListNode? next = null; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n    public ListNode? prev = null; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n    ListNode? front, rear; // \u5934\u8282\u70b9 front, \u5c3e\u8282\u70b9 rear\n    int queSize = 0;      // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n    public LinkedListDeque() {\n        front = null;\n        rear = null;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    public int Size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    public bool IsEmpty() {\n        return Size() == 0;\n    }\n\n    /* \u5165\u961f\u64cd\u4f5c */\n    void Push(int num, bool isFront) {\n        ListNode node = new(num);\n        // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n        if (IsEmpty()) {\n            front = node;\n            rear = node;\n        }\n        // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n        else if (isFront) {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n            front!.prev = node;\n            node.next = front;\n            front = node; // \u66f4\u65b0\u5934\u8282\u70b9                           \n        }\n        // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n        else {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n            rear!.next = node;\n            node.prev = rear;\n            rear = node;  // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n\n        queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    public void PushFirst(int num) {\n        Push(num, true);\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    public void PushLast(int num) {\n        Push(num, false);\n    }\n\n    /* \u51fa\u961f\u64cd\u4f5c */\n    int? Pop(bool isFront) {\n        if (IsEmpty())\n            throw new Exception();\n        int? val;\n        // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n        if (isFront) {\n            val = front?.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n            // \u5220\u9664\u5934\u8282\u70b9\n            ListNode? fNext = front?.next;\n            if (fNext != null) {\n                fNext.prev = null;\n                front!.next = null;\n            }\n            front = fNext;   // \u66f4\u65b0\u5934\u8282\u70b9\n        }\n        // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n        else {\n            val = rear?.val;  // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n            // \u5220\u9664\u5c3e\u8282\u70b9\n            ListNode? rPrev = rear?.prev;\n            if (rPrev != null) {\n                rPrev.next = null;\n                rear!.prev = null;\n            }\n            rear = rPrev;    // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n\n        queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n        return val;\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    public int? PopFirst() {\n        return Pop(true);\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    public int? PopLast() {\n        return Pop(false);\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    public int? PeekFirst() {\n        if (IsEmpty())\n            throw new Exception();\n        return front?.val;\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    public int? PeekLast() {\n        if (IsEmpty())\n            throw new Exception();\n        return rear?.val;\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    public int?[] ToArray() {\n        ListNode? node = front;\n        int?[] res = new int?[Size()];\n        for (int i = 0; i < res.Length; i++) {\n            res[i] = node?.val;\n            node = node?.next;\n        }\n\n        return res;\n    }\n}\n
        linkedlist_deque.go
        /* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntype linkedListDeque struct {\n    // \u4f7f\u7528\u5185\u7f6e\u5305 list\n    data *list.List\n}\n\n/* \u521d\u59cb\u5316\u53cc\u7aef\u961f\u5217 */\nfunc newLinkedListDeque() *linkedListDeque {\n    return &linkedListDeque{\n        data: list.New(),\n    }\n}\n\n/* \u961f\u9996\u5143\u7d20\u5165\u961f */\nfunc (s *linkedListDeque) pushFirst(value any) {\n    s.data.PushFront(value)\n}\n\n/* \u961f\u5c3e\u5143\u7d20\u5165\u961f */\nfunc (s *linkedListDeque) pushLast(value any) {\n    s.data.PushBack(value)\n}\n\n/* \u961f\u9996\u5143\u7d20\u51fa\u961f */\nfunc (s *linkedListDeque) popFirst() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Front()\n    s.data.Remove(e)\n    return e.Value\n}\n\n/* \u961f\u5c3e\u5143\u7d20\u51fa\u961f */\nfunc (s *linkedListDeque) popLast() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Back()\n    s.data.Remove(e)\n    return e.Value\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (s *linkedListDeque) peekFirst() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Front()\n    return e.Value\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc (s *linkedListDeque) peekLast() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Back()\n    return e.Value\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (s *linkedListDeque) size() int {\n    return s.data.Len()\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListDeque) isEmpty() bool {\n    return s.data.Len() == 0\n}\n\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListDeque) toList() *list.List {\n    return s.data\n}\n
        linkedlist_deque.swift
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n    var val: Int // \u8282\u70b9\u503c\n    var next: ListNode? // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n    weak var prev: ListNode? // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\n    init(val: Int) {\n        self.val = val\n    }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n    private var front: ListNode? // \u5934\u8282\u70b9 front\n    private var rear: ListNode? // \u5c3e\u8282\u70b9 rear\n    private var _size: Int // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n    init() {\n        _size = 0\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    func size() -> Int {\n        _size\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* \u5165\u961f\u64cd\u4f5c */\n    private func push(num: Int, isFront: Bool) {\n        let node = ListNode(val: num)\n        // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n        if isEmpty() {\n            front = node\n            rear = node\n        }\n        // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n        else if isFront {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n            front?.prev = node\n            node.next = front\n            front = node // \u66f4\u65b0\u5934\u8282\u70b9\n        }\n        // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n        else {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n            rear?.next = node\n            node.prev = rear\n            rear = node // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n        _size += 1 // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    func pushFirst(num: Int) {\n        push(num: num, isFront: true)\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    func pushLast(num: Int) {\n        push(num: num, isFront: false)\n    }\n\n    /* \u51fa\u961f\u64cd\u4f5c */\n    private func pop(isFront: Bool) -> Int {\n        if isEmpty() {\n            fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        }\n        let val: Int\n        // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n        if isFront {\n            val = front!.val // \u6682\u5b58\u5934\u8282\u70b9\u503c\n            // \u5220\u9664\u5934\u8282\u70b9\n            let fNext = front?.next\n            if fNext != nil {\n                fNext?.prev = nil\n                front?.next = nil\n            }\n            front = fNext // \u66f4\u65b0\u5934\u8282\u70b9\n        }\n        // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n        else {\n            val = rear!.val // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n            // \u5220\u9664\u5c3e\u8282\u70b9\n            let rPrev = rear?.prev\n            if rPrev != nil {\n                rPrev?.next = nil\n                rear?.prev = nil\n            }\n            rear = rPrev // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n        _size -= 1 // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n        return val\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    func popFirst() -> Int {\n        pop(isFront: true)\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    func popLast() -> Int {\n        pop(isFront: false)\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    func peekFirst() -> Int {\n        if isEmpty() {\n            fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        }\n        return front!.val\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    func peekLast() -> Int {\n        if isEmpty() {\n            fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        }\n        return rear!.val\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    func toArray() -> [Int] {\n        var node = front\n        var res = Array(repeating: 0, count: size())\n        for i in res.indices {\n            res[i] = node!.val\n            node = node?.next\n        }\n        return res\n    }\n}\n
        linkedlist_deque.js
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n    prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n    next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n    val; // \u8282\u70b9\u503c\n\n    constructor(val) {\n        this.val = val;\n        this.next = null;\n        this.prev = null;\n    }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n    #front; // \u5934\u8282\u70b9 front\n    #rear; // \u5c3e\u8282\u70b9 rear\n    #queSize; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n    constructor() {\n        this.#front = null;\n        this.#rear = null;\n        this.#queSize = 0;\n    }\n\n    /* \u961f\u5c3e\u5165\u961f\u64cd\u4f5c */\n    pushLast(val) {\n        const node = new ListNode(val);\n        // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n        if (this.#queSize === 0) {\n            this.#front = node;\n            this.#rear = node;\n        } else {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n            this.#rear.next = node;\n            node.prev = this.#rear;\n            this.#rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n        this.#queSize++;\n    }\n\n    /* \u961f\u9996\u5165\u961f\u64cd\u4f5c */\n    pushFirst(val) {\n        const node = new ListNode(val);\n        // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n        if (this.#queSize === 0) {\n            this.#front = node;\n            this.#rear = node;\n        } else {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n            this.#front.prev = node;\n            node.next = this.#front;\n            this.#front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n        }\n        this.#queSize++;\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c */\n    popLast() {\n        if (this.#queSize === 0) {\n            return null;\n        }\n        const value = this.#rear.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n        // \u5220\u9664\u5c3e\u8282\u70b9\n        let temp = this.#rear.prev;\n        if (temp !== null) {\n            temp.next = null;\n            this.#rear.prev = null;\n        }\n        this.#rear = temp; // \u66f4\u65b0\u5c3e\u8282\u70b9\n        this.#queSize--;\n        return value;\n    }\n\n    /* \u961f\u9996\u51fa\u961f\u64cd\u4f5c */\n    popFirst() {\n        if (this.#queSize === 0) {\n            return null;\n        }\n        const value = this.#front.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n        // \u5220\u9664\u5934\u8282\u70b9\n        let temp = this.#front.next;\n        if (temp !== null) {\n            temp.prev = null;\n            this.#front.next = null;\n        }\n        this.#front = temp; // \u66f4\u65b0\u5934\u8282\u70b9\n        this.#queSize--;\n        return value;\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    peekLast() {\n        return this.#queSize === 0 ? null : this.#rear.val;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    peekFirst() {\n        return this.#queSize === 0 ? null : this.#front.val;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    size() {\n        return this.#queSize;\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    isEmpty() {\n        return this.#queSize === 0;\n    }\n\n    /* \u6253\u5370\u53cc\u5411\u961f\u5217 */\n    print() {\n        const arr = [];\n        let temp = this.#front;\n        while (temp !== null) {\n            arr.push(temp.val);\n            temp = temp.next;\n        }\n        console.log('[' + arr.join(', ') + ']');\n    }\n}\n
        linkedlist_deque.ts
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n    prev: ListNode; // \u524d\u9a71\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n    next: ListNode; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n    val: number; // \u8282\u70b9\u503c\n\n    constructor(val: number) {\n        this.val = val;\n        this.next = null;\n        this.prev = null;\n    }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n    private front: ListNode; // \u5934\u8282\u70b9 front\n    private rear: ListNode; // \u5c3e\u8282\u70b9 rear\n    private queSize: number; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n    constructor() {\n        this.front = null;\n        this.rear = null;\n        this.queSize = 0;\n    }\n\n    /* \u961f\u5c3e\u5165\u961f\u64cd\u4f5c */\n    pushLast(val: number): void {\n        const node: ListNode = new ListNode(val);\n        // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n        if (this.queSize === 0) {\n            this.front = node;\n            this.rear = node;\n        } else {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n            this.rear.next = node;\n            node.prev = this.rear;\n            this.rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n        this.queSize++;\n    }\n\n    /* \u961f\u9996\u5165\u961f\u64cd\u4f5c */\n    pushFirst(val: number): void {\n        const node: ListNode = new ListNode(val);\n        // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n        if (this.queSize === 0) {\n            this.front = node;\n            this.rear = node;\n        } else {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n            this.front.prev = node;\n            node.next = this.front;\n            this.front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n        }\n        this.queSize++;\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c */\n    popLast(): number {\n        if (this.queSize === 0) {\n            return null;\n        }\n        const value: number = this.rear.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n        // \u5220\u9664\u5c3e\u8282\u70b9\n        let temp: ListNode = this.rear.prev;\n        if (temp !== null) {\n            temp.next = null;\n            this.rear.prev = null;\n        }\n        this.rear = temp; // \u66f4\u65b0\u5c3e\u8282\u70b9\n        this.queSize--;\n        return value;\n    }\n\n    /* \u961f\u9996\u51fa\u961f\u64cd\u4f5c */\n    popFirst(): number {\n        if (this.queSize === 0) {\n            return null;\n        }\n        const value: number = this.front.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n        // \u5220\u9664\u5934\u8282\u70b9\n        let temp: ListNode = this.front.next;\n        if (temp !== null) {\n            temp.prev = null;\n            this.front.next = null;\n        }\n        this.front = temp; // \u66f4\u65b0\u5934\u8282\u70b9\n        this.queSize--;\n        return value;\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    peekLast(): number {\n        return this.queSize === 0 ? null : this.rear.val;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    peekFirst(): number {\n        return this.queSize === 0 ? null : this.front.val;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    size(): number {\n        return this.queSize;\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    isEmpty(): boolean {\n        return this.queSize === 0;\n    }\n\n    /* \u6253\u5370\u53cc\u5411\u961f\u5217 */\n    print(): void {\n        const arr: number[] = [];\n        let temp: ListNode = this.front;\n        while (temp !== null) {\n            arr.push(temp.val);\n            temp = temp.next;\n        }\n        console.log('[' + arr.join(', ') + ']');\n    }\n}\n
        linkedlist_deque.dart
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n  int val; // \u8282\u70b9\u503c\n  ListNode? next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n  ListNode? prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\n  ListNode(this.val, {this.next, this.prev});\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u5bf9\u5217 */\nclass LinkedListDeque {\n  late ListNode? _front; // \u5934\u8282\u70b9 _front\n  late ListNode? _rear; // \u5c3e\u8282\u70b9 _rear\n  int _queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n  LinkedListDeque() {\n    this._front = null;\n    this._rear = null;\n  }\n\n  /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u957f\u5ea6 */\n  int size() {\n    return this._queSize;\n  }\n\n  /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n  bool isEmpty() {\n    return size() == 0;\n  }\n\n  /* \u5165\u961f\u64cd\u4f5c */\n  void push(int _num, bool isFront) {\n    final ListNode node = ListNode(_num);\n    if (isEmpty()) {\n      // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 _front \u548c _rear \u90fd\u6307\u5411 node\n      _front = _rear = node;\n    } else if (isFront) {\n      // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n      // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n      _front!.prev = node;\n      node.next = _front;\n      _front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n    } else {\n      // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n      // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n      _rear!.next = node;\n      node.prev = _rear;\n      _rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n    }\n    _queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n  }\n\n  /* \u961f\u9996\u5165\u961f */\n  void pushFirst(int _num) {\n    push(_num, true);\n  }\n\n  /* \u961f\u5c3e\u5165\u961f */\n  void pushLast(int _num) {\n    push(_num, false);\n  }\n\n  /* \u51fa\u961f\u64cd\u4f5c */\n  int? pop(bool isFront) {\n    // \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de null\n    if (isEmpty()) {\n      return null;\n    }\n    final int val;\n    if (isFront) {\n      // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n      val = _front!.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n      // \u5220\u9664\u5934\u8282\u70b9\n      ListNode? fNext = _front!.next;\n      if (fNext != null) {\n        fNext.prev = null;\n        _front!.next = null;\n      }\n      _front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n    } else {\n      // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n      val = _rear!.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n      // \u5220\u9664\u5c3e\u8282\u70b9\n      ListNode? rPrev = _rear!.prev;\n      if (rPrev != null) {\n        rPrev.next = null;\n        _rear!.prev = null;\n      }\n      _rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n    }\n    _queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n    return val;\n  }\n\n  /* \u961f\u9996\u51fa\u961f */\n  int? popFirst() {\n    return pop(true);\n  }\n\n  /* \u961f\u5c3e\u51fa\u961f */\n  int? popLast() {\n    return pop(false);\n  }\n\n  /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n  int? peekFirst() {\n    return _front?.val;\n  }\n\n  /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n  int? peekLast() {\n    return _rear?.val;\n  }\n\n  /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n  List<int> toArray() {\n    ListNode? node = _front;\n    final List<int> res = [];\n    for (int i = 0; i < _queSize; i++) {\n      res.add(node!.val);\n      node = node.next;\n    }\n    return res;\n  }\n}\n
        linkedlist_deque.rs
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\npub struct ListNode<T> {\n    pub val: T,                                 // \u8282\u70b9\u503c\n    pub next: Option<Rc<RefCell<ListNode<T>>>>, // \u540e\u7ee7\u8282\u70b9\u6307\u9488\n    pub prev: Option<Rc<RefCell<ListNode<T>>>>, // \u524d\u9a71\u8282\u70b9\u6307\u9488\n}\n\nimpl<T> ListNode<T> {\n    pub fn new(val: T) -> Rc<RefCell<ListNode<T>>> {\n        Rc::new(RefCell::new(ListNode {\n            val,\n            next: None,\n            prev: None,\n        }))\n    }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\n#[allow(dead_code)]\npub struct LinkedListDeque<T> {\n    front: Option<Rc<RefCell<ListNode<T>>>>, // \u5934\u8282\u70b9 front\n    rear: Option<Rc<RefCell<ListNode<T>>>>,  // \u5c3e\u8282\u70b9 rear\n    que_size: usize,                         // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n}\n\nimpl<T: Copy> LinkedListDeque<T> {\n    pub fn new() -> Self {\n        Self {\n            front: None,\n            rear: None,\n            que_size: 0,\n        }\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    pub fn size(&self) -> usize {\n        return self.que_size;\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    pub fn is_empty(&self) -> bool {\n        return self.que_size == 0;\n    }\n\n    /* \u5165\u961f\u64cd\u4f5c */\n    fn push(&mut self, num: T, is_front: bool) {\n        let node = ListNode::new(num);\n        // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n        if is_front {\n            match self.front.take() {\n                // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n                None => {\n                    self.rear = Some(node.clone());\n                    self.front = Some(node);\n                }\n                // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n                Some(old_front) => {\n                    old_front.borrow_mut().prev = Some(node.clone());\n                    node.borrow_mut().next = Some(old_front);\n                    self.front = Some(node); // \u66f4\u65b0\u5934\u8282\u70b9\n                }\n            }\n        }\n        // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n        else {\n            match self.rear.take() {\n                // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n                None => {\n                    self.front = Some(node.clone());\n                    self.rear = Some(node);\n                }\n                // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n                Some(old_rear) => {\n                    old_rear.borrow_mut().next = Some(node.clone());\n                    node.borrow_mut().prev = Some(old_rear);\n                    self.rear = Some(node); // \u66f4\u65b0\u5c3e\u8282\u70b9\n                }\n            }\n        }\n        self.que_size += 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    pub fn push_first(&mut self, num: T) {\n        self.push(num, true);\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    pub fn push_last(&mut self, num: T) {\n        self.push(num, false);\n    }\n\n    /* \u51fa\u961f\u64cd\u4f5c */\n    fn pop(&mut self, is_front: bool) -> Option<T> {\n        // \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de None\n        if self.is_empty() {\n            return None;\n        };\n        // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n        if is_front {\n            self.front.take().map(|old_front| {\n                match old_front.borrow_mut().next.take() {\n                    Some(new_front) => {\n                        new_front.borrow_mut().prev.take();\n                        self.front = Some(new_front); // \u66f4\u65b0\u5934\u8282\u70b9\n                    }\n                    None => {\n                        self.rear.take();\n                    }\n                }\n                self.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n                old_front.borrow().val\n            })\n        }\n        // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n        else {\n            self.rear.take().map(|old_rear| {\n                match old_rear.borrow_mut().prev.take() {\n                    Some(new_rear) => {\n                        new_rear.borrow_mut().next.take();\n                        self.rear = Some(new_rear); // \u66f4\u65b0\u5c3e\u8282\u70b9\n                    }\n                    None => {\n                        self.front.take();\n                    }\n                }\n                self.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n                old_rear.borrow().val\n            })\n        }\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    pub fn pop_first(&mut self) -> Option<T> {\n        return self.pop(true);\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    pub fn pop_last(&mut self) -> Option<T> {\n        return self.pop(false);\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    pub fn peek_first(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n        self.front.as_ref()\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    pub fn peek_last(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n        self.rear.as_ref()\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n        let mut res: Vec<T> = Vec::new();\n        fn recur<T: Copy>(cur: Option<&Rc<RefCell<ListNode<T>>>>, res: &mut Vec<T>) {\n            if let Some(cur) = cur {\n                res.push(cur.borrow().val);\n                recur(cur.borrow().next.as_ref(), res);\n            }\n        }\n\n        recur(head, &mut res);\n        res\n    }\n}\n
        linkedlist_deque.c
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\ntypedef struct DoublyListNode {\n    int val;                     // \u8282\u70b9\u503c\n    struct DoublyListNode *next; // \u540e\u7ee7\u8282\u70b9\n    struct DoublyListNode *prev; // \u524d\u9a71\u8282\u70b9\n} DoublyListNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nDoublyListNode *newDoublyListNode(int num) {\n    DoublyListNode *new = (DoublyListNode *)malloc(sizeof(DoublyListNode));\n    new->val = num;\n    new->next = NULL;\n    new->prev = NULL;\n    return new;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delDoublyListNode(DoublyListNode *node) {\n    free(node);\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntypedef struct {\n    DoublyListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n    int queSize;                  // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n} LinkedListDeque;\n\n/* \u6784\u9020\u51fd\u6570 */\nLinkedListDeque *newLinkedListDeque() {\n    LinkedListDeque *deque = (LinkedListDeque *)malloc(sizeof(LinkedListDeque));\n    deque->front = NULL;\n    deque->rear = NULL;\n    deque->queSize = 0;\n    return deque;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListdeque(LinkedListDeque *deque) {\n    // \u91ca\u653e\u6240\u6709\u8282\u70b9\n    for (int i = 0; i < deque->queSize && deque->front != NULL; i++) {\n        DoublyListNode *tmp = deque->front;\n        deque->front = deque->front->next;\n        free(tmp);\n    }\n    // \u91ca\u653e deque \u7ed3\u6784\u4f53\n    free(deque);\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(LinkedListDeque *deque) {\n    return deque->queSize;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(LinkedListDeque *deque) {\n    return (size(deque) == 0);\n}\n\n/* \u5165\u961f */\nvoid push(LinkedListDeque *deque, int num, bool isFront) {\n    DoublyListNode *node = newDoublyListNode(num);\n    // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411node\n    if (empty(deque)) {\n        deque->front = deque->rear = node;\n    }\n    // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n    else if (isFront) {\n        // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n        deque->front->prev = node;\n        node->next = deque->front;\n        deque->front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n    }\n    // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n    else {\n        // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n        deque->rear->next = node;\n        node->prev = deque->rear;\n        deque->rear = node;\n    }\n    deque->queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(LinkedListDeque *deque, int num) {\n    push(deque, num, true);\n}\n\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(LinkedListDeque *deque, int num) {\n    push(deque, num, false);\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst(LinkedListDeque *deque) {\n    assert(size(deque) && deque->front);\n    return deque->front->val;\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast(LinkedListDeque *deque) {\n    assert(size(deque) && deque->rear);\n    return deque->rear->val;\n}\n\n/* \u51fa\u961f */\nint pop(LinkedListDeque *deque, bool isFront) {\n    if (empty(deque))\n        return -1;\n    int val;\n    // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n    if (isFront) {\n        val = peekFirst(deque); // \u6682\u5b58\u5934\u8282\u70b9\u503c\n        DoublyListNode *fNext = deque->front->next;\n        if (fNext) {\n            fNext->prev = NULL;\n            deque->front->next = NULL;\n        }\n        delDoublyListNode(deque->front);\n        deque->front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n    }\n    // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n    else {\n        val = peekLast(deque); // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n        DoublyListNode *rPrev = deque->rear->prev;\n        if (rPrev) {\n            rPrev->next = NULL;\n            deque->rear->prev = NULL;\n        }\n        delDoublyListNode(deque->rear);\n        deque->rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n    }\n    deque->queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n    return val;\n}\n\n/* \u961f\u9996\u51fa\u961f */\nint popFirst(LinkedListDeque *deque) {\n    return pop(deque, true);\n}\n\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast(LinkedListDeque *deque) {\n    return pop(deque, false);\n}\n\n/* \u6253\u5370\u961f\u5217 */\nvoid printLinkedListDeque(LinkedListDeque *deque) {\n    int *arr = malloc(sizeof(int) * deque->queSize);\n    // \u62f7\u8d1d\u94fe\u8868\u4e2d\u7684\u6570\u636e\u5230\u6570\u7ec4\n    int i;\n    DoublyListNode *node;\n    for (i = 0, node = deque->front; i < deque->queSize; i++) {\n        arr[i] = node->val;\n        node = node->next;\n    }\n    printArray(arr, deque->queSize);\n    free(arr);\n}\n
        linkedlist_deque.kt
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode(var _val: Int) {\n    // \u8282\u70b9\u503c\n    var next: ListNode? = null // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n    var prev: ListNode? = null // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n    private var front: ListNode? = null // \u5934\u8282\u70b9 front\n    private var rear: ListNode? = null // \u5c3e\u8282\u70b9 rear\n    private var queSize: Int = 0 // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    fun size(): Int {\n        return queSize\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    fun isEmpty(): Boolean {\n        return size() == 0\n    }\n\n    /* \u5165\u961f\u64cd\u4f5c */\n    fun push(num: Int, isFront: Boolean) {\n        val node = ListNode(num)\n        // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n        if (isEmpty()) {\n            rear = node\n            front = rear\n            // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n        } else if (isFront) {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n            front?.prev = node\n            node.next = front\n            front = node // \u66f4\u65b0\u5934\u8282\u70b9\n            // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n        } else {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n            rear?.next = node\n            node.prev = rear\n            rear = node // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n        queSize++ // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    fun pushFirst(num: Int) {\n        push(num, true)\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    fun pushLast(num: Int) {\n        push(num, false)\n    }\n\n    /* \u51fa\u961f\u64cd\u4f5c */\n    fun pop(isFront: Boolean): Int {\n        if (isEmpty()) \n            throw IndexOutOfBoundsException()\n        val _val: Int\n        // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n        if (isFront) {\n            _val = front!!._val // \u6682\u5b58\u5934\u8282\u70b9\u503c\n            // \u5220\u9664\u5934\u8282\u70b9\n            val fNext = front!!.next\n            if (fNext != null) {\n                fNext.prev = null\n                front!!.next = null\n            }\n            front = fNext // \u66f4\u65b0\u5934\u8282\u70b9\n            // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n        } else {\n            _val = rear!!._val // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n            // \u5220\u9664\u5c3e\u8282\u70b9\n            val rPrev = rear!!.prev\n            if (rPrev != null) {\n                rPrev.next = null\n                rear!!.prev = null\n            }\n            rear = rPrev // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n        queSize-- // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n        return _val\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    fun popFirst(): Int {\n        return pop(true)\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    fun popLast(): Int {\n        return pop(false)\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    fun peekFirst(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return front!!._val\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    fun peekLast(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return rear!!._val\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    fun toArray(): IntArray {\n        var node = front\n        val res = IntArray(size())\n        for (i in res.indices) {\n            res[i] = node!!._val\n            node = node.next\n        }\n        return res\n    }\n}\n
        linkedlist_deque.rb
        =begin\nFile: linkedlist_deque.rb\nCreated Time: 2024-04-06\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n### \u53cc\u5411\u94fe\u8868\u8282\u70b9\nclass ListNode\n  attr_accessor :val\n  attr_accessor :next # \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n  attr_accessor :prev # \u524d\u8eaf\u8282\u70b9\u5f15\u7528\n\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize(val)\n    @val = val\n  end\nend\n\n### \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 ###\nclass LinkedListDeque\n  ### \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 ###\n  attr_reader :size\n\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize\n    @front = nil  # \u5934\u8282\u70b9 front\n    @rear = nil   # \u5c3e\u8282\u70b9 rear\n    @size = 0     # \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n  end\n\n  ### \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a ###\n  def is_empty?\n    size.zero?\n  end\n\n  ### \u5165\u961f\u64cd\u4f5c ###\n  def push(num, is_front)\n    node = ListNode.new(num)\n    # \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c \u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n    if is_empty?\n      @front = @rear = node\n    # \u961f\u9996\u5165\u961f\u64cd\u4f5c\n    elsif is_front\n      # \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n      @front.prev = node\n      node.next = @front\n      @front = node # \u66f4\u65b0\u5934\u8282\u70b9\n    # \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n    else\n      # \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n      @rear.next = node\n      node.prev = @rear\n      @rear = node # \u66f4\u65b0\u5c3e\u8282\u70b9\n    end\n    @size += 1 # \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n  end\n\n  ### \u961f\u9996\u5165\u961f ###\n  def push_first(num)\n    push(num, true)\n  end\n\n  ### \u961f\u5c3e\u5165\u961f ###\n  def push_last(num)\n    push(num, false)\n  end\n\n  ### \u51fa\u961f\u64cd\u4f5c ###\n  def pop(is_front)\n    raise IndexError, '\u53cc\u5411\u961f\u5217\u4e3a\u7a7a' if is_empty?\n\n    # \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n    if is_front\n      val = @front.val # \u6682\u5b58\u5934\u8282\u70b9\u503c\n      # \u5220\u9664\u5934\u8282\u70b9\n      fnext = @front.next\n      unless fnext.nil?\n        fnext.prev = nil\n        @front.next = nil\n      end\n      @front = fnext # \u66f4\u65b0\u5934\u8282\u70b9\n    # \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n    else\n      val = @rear.val # \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n      # \u5220\u9664\u5c3e\u8282\u70b9\n      rprev = @rear.prev\n      unless rprev.nil?\n        rprev.next = nil\n        @rear.prev = nil\n      end\n      @rear = rprev # \u66f4\u65b0\u5c3e\u8282\u70b9\n    end\n    @size -= 1 # \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n\n    val\n  end\n\n  ### \u961f\u9996\u51fa\u961f ###\n  def pop_first\n    pop(true)\n  end\n\n  ### \u961f\u9996\u51fa\u961f ###\n  def pop_last\n    pop(false)\n  end\n\n  ### \u8bbf\u95ee\u961f\u9996\u5143\u7d20 ###\n  def peek_first\n    raise IndexError, '\u53cc\u5411\u961f\u5217\u4e3a\u7a7a' if is_empty?\n\n    @front.val\n  end\n\n  ### \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 ###\n  def peek_last\n    raise IndexError, '\u53cc\u5411\u961f\u5217\u4e3a\u7a7a' if is_empty?\n\n    @rear.val\n  end\n\n  ### \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 ###\n  def to_array\n    node = @front\n    res = Array.new(size, 0)\n    for i in 0...size\n      res[i] = node.val\n      node = node.next\n    end\n    res\n  end\nend\n
        "},{"location":"chapter_stack_and_queue/deque/#2","title":"2. \u00a0 \u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"

        \u5982\u56fe 5-9 \u6240\u793a\uff0c\u4e0e\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u961f\u5217\u7c7b\u4f3c\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u73af\u5f62\u6570\u7ec4\u6765\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u3002

        ArrayDequepush_last()push_first()pop_last()pop_first()

        \u56fe 5-9 \u00a0 \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c

        \u5728\u961f\u5217\u7684\u5b9e\u73b0\u57fa\u7840\u4e0a\uff0c\u4ec5\u9700\u589e\u52a0\u201c\u961f\u9996\u5165\u961f\u201d\u548c\u201c\u961f\u5c3e\u51fa\u961f\u201d\u7684\u65b9\u6cd5\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_deque.py
        class ArrayDeque:\n    \"\"\"\u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\"\"\"\n\n    def __init__(self, capacity: int):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        self._nums: list[int] = [0] * capacity\n        self._front: int = 0\n        self._size: int = 0\n\n    def capacity(self) -> int:\n        \"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf\"\"\"\n        return len(self._nums)\n\n    def size(self) -> int:\n        \"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n        return self._size == 0\n\n    def index(self, i: int) -> int:\n        \"\"\"\u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15\"\"\"\n        # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n        # \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n        # \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n        return (i + self.capacity()) % self.capacity()\n\n    def push_first(self, num: int):\n        \"\"\"\u961f\u9996\u5165\u961f\"\"\"\n        if self._size == self.capacity():\n            print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n            return\n        # \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n        # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n        self._front = self.index(self._front - 1)\n        # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n        self._nums[self._front] = num\n        self._size += 1\n\n    def push_last(self, num: int):\n        \"\"\"\u961f\u5c3e\u5165\u961f\"\"\"\n        if self._size == self.capacity():\n            print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n            return\n        # \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        rear = self.index(self._front + self._size)\n        # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        self._nums[rear] = num\n        self._size += 1\n\n    def pop_first(self) -> int:\n        \"\"\"\u961f\u9996\u51fa\u961f\"\"\"\n        num = self.peek_first()\n        # \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        self._front = self.index(self._front + 1)\n        self._size -= 1\n        return num\n\n    def pop_last(self) -> int:\n        \"\"\"\u961f\u5c3e\u51fa\u961f\"\"\"\n        num = self.peek_last()\n        self._size -= 1\n        return num\n\n    def peek_first(self) -> int:\n        \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        return self._nums[self._front]\n\n    def peek_last(self) -> int:\n        \"\"\"\u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        # \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n        last = self.index(self._front + self._size - 1)\n        return self._nums[last]\n\n    def to_array(self) -> list[int]:\n        \"\"\"\u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\"\"\"\n        # \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        res = []\n        for i in range(self._size):\n            res.append(self._nums[self.index(self._front + i)])\n        return res\n
        array_deque.cpp
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n  private:\n    vector<int> nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    int front;        // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    int queSize;      // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n  public:\n    /* \u6784\u9020\u65b9\u6cd5 */\n    ArrayDeque(int capacity) {\n        nums.resize(capacity);\n        front = queSize = 0;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n    int capacity() {\n        return nums.size();\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    int size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    bool isEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n    int index(int i) {\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n        return (i + capacity()) % capacity();\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    void pushFirst(int num) {\n        if (queSize == capacity()) {\n            cout << \"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\" << endl;\n            return;\n        }\n        // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n        front = index(front - 1);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n        nums[front] = num;\n        queSize++;\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    void pushLast(int num) {\n        if (queSize == capacity()) {\n            cout << \"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\" << endl;\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        int rear = index(front + queSize);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    int popFirst() {\n        int num = peekFirst();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        front = index(front + 1);\n        queSize--;\n        return num;\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    int popLast() {\n        int num = peekLast();\n        queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    int peekFirst() {\n        if (isEmpty())\n            throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n        return nums[front];\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    int peekLast() {\n        if (isEmpty())\n            throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n        // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n        int last = index(front + queSize - 1);\n        return nums[last];\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    vector<int> toVector() {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        vector<int> res(queSize);\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[index(j)];\n        }\n        return res;\n    }\n};\n
        array_deque.java
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n    private int[] nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    private int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    private int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public ArrayDeque(int capacity) {\n        this.nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n    public int capacity() {\n        return nums.length;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    public int size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    public boolean isEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n    private int index(int i) {\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n        return (i + capacity()) % capacity();\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    public void pushFirst(int num) {\n        if (queSize == capacity()) {\n            System.out.println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n            return;\n        }\n        // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n        front = index(front - 1);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n        nums[front] = num;\n        queSize++;\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    public void pushLast(int num) {\n        if (queSize == capacity()) {\n            System.out.println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        int rear = index(front + queSize);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    public int popFirst() {\n        int num = peekFirst();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        front = index(front + 1);\n        queSize--;\n        return num;\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    public int popLast() {\n        int num = peekLast();\n        queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    public int peekFirst() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return nums[front];\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    public int peekLast() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n        int last = index(front + queSize - 1);\n        return nums[last];\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    public int[] toArray() {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[index(j)];\n        }\n        return res;\n    }\n}\n
        array_deque.cs
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n    int[] nums;  // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    int front;   // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public ArrayDeque(int capacity) {\n        nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n    int Capacity() {\n        return nums.Length;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    public int Size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    public bool IsEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n    int Index(int i) {\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n        return (i + Capacity()) % Capacity();\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    public void PushFirst(int num) {\n        if (queSize == Capacity()) {\n            Console.WriteLine(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n            return;\n        }\n        // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n        front = Index(front - 1);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n        nums[front] = num;\n        queSize++;\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    public void PushLast(int num) {\n        if (queSize == Capacity()) {\n            Console.WriteLine(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        int rear = Index(front + queSize);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    public int PopFirst() {\n        int num = PeekFirst();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        front = Index(front + 1);\n        queSize--;\n        return num;\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    public int PopLast() {\n        int num = PeekLast();\n        queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    public int PeekFirst() {\n        if (IsEmpty()) {\n            throw new InvalidOperationException();\n        }\n        return nums[front];\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    public int PeekLast() {\n        if (IsEmpty()) {\n            throw new InvalidOperationException();\n        }\n        // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n        int last = Index(front + queSize - 1);\n        return nums[last];\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    public int[] ToArray() {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[Index(j)];\n        }\n        return res;\n    }\n}\n
        array_deque.go
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntype arrayDeque struct {\n    nums        []int // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    front       int   // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    queSize     int   // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n    queCapacity int   // \u961f\u5217\u5bb9\u91cf\uff08\u5373\u6700\u5927\u5bb9\u7eb3\u5143\u7d20\u6570\u91cf\uff09\n}\n\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newArrayDeque(queCapacity int) *arrayDeque {\n    return &arrayDeque{\n        nums:        make([]int, queCapacity),\n        queCapacity: queCapacity,\n        front:       0,\n        queSize:     0,\n    }\n}\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (q *arrayDeque) size() int {\n    return q.queSize\n}\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (q *arrayDeque) isEmpty() bool {\n    return q.queSize == 0\n}\n\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nfunc (q *arrayDeque) index(i int) int {\n    // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n    // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n    // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n    return (i + q.queCapacity) % q.queCapacity\n}\n\n/* \u961f\u9996\u5165\u961f */\nfunc (q *arrayDeque) pushFirst(num int) {\n    if q.queSize == q.queCapacity {\n        fmt.Println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n        return\n    }\n    // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n    // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n    q.front = q.index(q.front - 1)\n    // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n    q.nums[q.front] = num\n    q.queSize++\n}\n\n/* \u961f\u5c3e\u5165\u961f */\nfunc (q *arrayDeque) pushLast(num int) {\n    if q.queSize == q.queCapacity {\n        fmt.Println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n        return\n    }\n    // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n    rear := q.index(q.front + q.queSize)\n    // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n    q.nums[rear] = num\n    q.queSize++\n}\n\n/* \u961f\u9996\u51fa\u961f */\nfunc (q *arrayDeque) popFirst() any {\n    num := q.peekFirst()\n    if num == nil {\n        return nil\n    }\n    // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    q.front = q.index(q.front + 1)\n    q.queSize--\n    return num\n}\n\n/* \u961f\u5c3e\u51fa\u961f */\nfunc (q *arrayDeque) popLast() any {\n    num := q.peekLast()\n    if num == nil {\n        return nil\n    }\n    q.queSize--\n    return num\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (q *arrayDeque) peekFirst() any {\n    if q.isEmpty() {\n        return nil\n    }\n    return q.nums[q.front]\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc (q *arrayDeque) peekLast() any {\n    if q.isEmpty() {\n        return nil\n    }\n    // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n    last := q.index(q.front + q.queSize - 1)\n    return q.nums[last]\n}\n\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (q *arrayDeque) toSlice() []int {\n    // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n    res := make([]int, q.queSize)\n    for i, j := 0, q.front; i < q.queSize; i++ {\n        res[i] = q.nums[q.index(j)]\n        j++\n    }\n    return res\n}\n
        array_deque.swift
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n    private var nums: [Int] // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    private var front: Int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    private var _size: Int // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    init(capacity: Int) {\n        nums = Array(repeating: 0, count: capacity)\n        front = 0\n        _size = 0\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n    func capacity() -> Int {\n        nums.count\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    func size() -> Int {\n        _size\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n    private func index(i: Int) -> Int {\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n        (i + capacity()) % capacity()\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    func pushFirst(num: Int) {\n        if size() == capacity() {\n            print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n            return\n        }\n        // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n        front = index(i: front - 1)\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n        nums[front] = num\n        _size += 1\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    func pushLast(num: Int) {\n        if size() == capacity() {\n            print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n            return\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        let rear = index(i: front + size())\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        nums[rear] = num\n        _size += 1\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    func popFirst() -> Int {\n        let num = peekFirst()\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        front = index(i: front + 1)\n        _size -= 1\n        return num\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    func popLast() -> Int {\n        let num = peekLast()\n        _size -= 1\n        return num\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    func peekFirst() -> Int {\n        if isEmpty() {\n            fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        }\n        return nums[front]\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    func peekLast() -> Int {\n        if isEmpty() {\n            fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        }\n        // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n        let last = index(i: front + size() - 1)\n        return nums[last]\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    func toArray() -> [Int] {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        (front ..< front + size()).map { nums[index(i: $0)] }\n    }\n}\n
        array_deque.js
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n    #nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    #front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    #queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor(capacity) {\n        this.#nums = new Array(capacity);\n        this.#front = 0;\n        this.#queSize = 0;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n    capacity() {\n        return this.#nums.length;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    size() {\n        return this.#queSize;\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    isEmpty() {\n        return this.#queSize === 0;\n    }\n\n    /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n    index(i) {\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n        return (i + this.capacity()) % this.capacity();\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    pushFirst(num) {\n        if (this.#queSize === this.capacity()) {\n            console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n            return;\n        }\n        // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n        this.#front = this.index(this.#front - 1);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n        this.#nums[this.#front] = num;\n        this.#queSize++;\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    pushLast(num) {\n        if (this.#queSize === this.capacity()) {\n            console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        const rear = this.index(this.#front + this.#queSize);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        this.#nums[rear] = num;\n        this.#queSize++;\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    popFirst() {\n        const num = this.peekFirst();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        this.#front = this.index(this.#front + 1);\n        this.#queSize--;\n        return num;\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    popLast() {\n        const num = this.peekLast();\n        this.#queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    peekFirst() {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        return this.#nums[this.#front];\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    peekLast() {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n        const last = this.index(this.#front + this.#queSize - 1);\n        return this.#nums[last];\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    toArray() {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        const res = [];\n        for (let i = 0, j = this.#front; i < this.#queSize; i++, j++) {\n            res[i] = this.#nums[this.index(j)];\n        }\n        return res;\n    }\n}\n
        array_deque.ts
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n    private nums: number[]; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    private front: number; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    private queSize: number; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor(capacity: number) {\n        this.nums = new Array(capacity);\n        this.front = 0;\n        this.queSize = 0;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n    capacity(): number {\n        return this.nums.length;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    size(): number {\n        return this.queSize;\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    isEmpty(): boolean {\n        return this.queSize === 0;\n    }\n\n    /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n    index(i: number): number {\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n        return (i + this.capacity()) % this.capacity();\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    pushFirst(num: number): void {\n        if (this.queSize === this.capacity()) {\n            console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n            return;\n        }\n        // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n        this.front = this.index(this.front - 1);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n        this.nums[this.front] = num;\n        this.queSize++;\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    pushLast(num: number): void {\n        if (this.queSize === this.capacity()) {\n            console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        const rear: number = this.index(this.front + this.queSize);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        this.nums[rear] = num;\n        this.queSize++;\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    popFirst(): number {\n        const num: number = this.peekFirst();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        this.front = this.index(this.front + 1);\n        this.queSize--;\n        return num;\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    popLast(): number {\n        const num: number = this.peekLast();\n        this.queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    peekFirst(): number {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        return this.nums[this.front];\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    peekLast(): number {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n        const last = this.index(this.front + this.queSize - 1);\n        return this.nums[last];\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    toArray(): number[] {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        const res: number[] = [];\n        for (let i = 0, j = this.front; i < this.queSize; i++, j++) {\n            res[i] = this.nums[this.index(j)];\n        }\n        return res;\n    }\n}\n
        array_deque.dart
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n  late List<int> _nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n  late int _front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n  late int _queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n  /* \u6784\u9020\u65b9\u6cd5 */\n  ArrayDeque(int capacity) {\n    this._nums = List.filled(capacity, 0);\n    this._front = this._queSize = 0;\n  }\n\n  /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n  int capacity() {\n    return _nums.length;\n  }\n\n  /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n  int size() {\n    return _queSize;\n  }\n\n  /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n  bool isEmpty() {\n    return _queSize == 0;\n  }\n\n  /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n  int index(int i) {\n    // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n    // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n    // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n    return (i + capacity()) % capacity();\n  }\n\n  /* \u961f\u9996\u5165\u961f */\n  void pushFirst(int _num) {\n    if (_queSize == capacity()) {\n      throw Exception(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n    }\n    // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n    // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 _front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n    _front = index(_front - 1);\n    // \u5c06 _num \u6dfb\u52a0\u81f3\u961f\u9996\n    _nums[_front] = _num;\n    _queSize++;\n  }\n\n  /* \u961f\u5c3e\u5165\u961f */\n  void pushLast(int _num) {\n    if (_queSize == capacity()) {\n      throw Exception(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n    }\n    // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n    int rear = index(_front + _queSize);\n    // \u5c06 _num \u6dfb\u52a0\u81f3\u961f\u5c3e\n    _nums[rear] = _num;\n    _queSize++;\n  }\n\n  /* \u961f\u9996\u51fa\u961f */\n  int popFirst() {\n    int _num = peekFirst();\n    // \u961f\u9996\u6307\u9488\u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n    _front = index(_front + 1);\n    _queSize--;\n    return _num;\n  }\n\n  /* \u961f\u5c3e\u51fa\u961f */\n  int popLast() {\n    int _num = peekLast();\n    _queSize--;\n    return _num;\n  }\n\n  /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n  int peekFirst() {\n    if (isEmpty()) {\n      throw Exception(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n    }\n    return _nums[_front];\n  }\n\n  /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n  int peekLast() {\n    if (isEmpty()) {\n      throw Exception(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n    }\n    // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n    int last = index(_front + _queSize - 1);\n    return _nums[last];\n  }\n\n  /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n  List<int> toArray() {\n    // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n    List<int> res = List.filled(_queSize, 0);\n    for (int i = 0, j = _front; i < _queSize; i++, j++) {\n      res[i] = _nums[index(j)];\n    }\n    return res;\n  }\n}\n
        array_deque.rs
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nstruct ArrayDeque<T> {\n    nums: Vec<T>,    // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    front: usize,    // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    que_size: usize, // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n}\n\nimpl<T: Copy + Default> ArrayDeque<T> {\n    /* \u6784\u9020\u65b9\u6cd5 */\n    pub fn new(capacity: usize) -> Self {\n        Self {\n            nums: vec![T::default(); capacity],\n            front: 0,\n            que_size: 0,\n        }\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n    pub fn capacity(&self) -> usize {\n        self.nums.len()\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    pub fn size(&self) -> usize {\n        self.que_size\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    pub fn is_empty(&self) -> bool {\n        self.que_size == 0\n    }\n\n    /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n    fn index(&self, i: i32) -> usize {\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n        ((i + self.capacity() as i32) % self.capacity() as i32) as usize\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    pub fn push_first(&mut self, num: T) {\n        if self.que_size == self.capacity() {\n            println!(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n            return;\n        }\n        // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n        self.front = self.index(self.front as i32 - 1);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n        self.nums[self.front] = num;\n        self.que_size += 1;\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    pub fn push_last(&mut self, num: T) {\n        if self.que_size == self.capacity() {\n            println!(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        let rear = self.index(self.front as i32 + self.que_size as i32);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        self.nums[rear] = num;\n        self.que_size += 1;\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    fn pop_first(&mut self) -> T {\n        let num = self.peek_first();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        self.front = self.index(self.front as i32 + 1);\n        self.que_size -= 1;\n        num\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    fn pop_last(&mut self) -> T {\n        let num = self.peek_last();\n        self.que_size -= 1;\n        num\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    fn peek_first(&self) -> T {\n        if self.is_empty() {\n            panic!(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        };\n        self.nums[self.front]\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    fn peek_last(&self) -> T {\n        if self.is_empty() {\n            panic!(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        };\n        // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n        let last = self.index(self.front as i32 + self.que_size as i32 - 1);\n        self.nums[last]\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    fn to_array(&self) -> Vec<T> {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        let mut res = vec![T::default(); self.que_size];\n        let mut j = self.front;\n        for i in 0..self.que_size {\n            res[i] = self.nums[self.index(j as i32)];\n            j += 1;\n        }\n        res\n    }\n}\n
        array_deque.c
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntypedef struct {\n    int *nums;       // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    int front;       // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    int queSize;     // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\n    int queCapacity; // \u961f\u5217\u5bb9\u91cf\n} ArrayDeque;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayDeque *newArrayDeque(int capacity) {\n    ArrayDeque *deque = (ArrayDeque *)malloc(sizeof(ArrayDeque));\n    // \u521d\u59cb\u5316\u6570\u7ec4\n    deque->queCapacity = capacity;\n    deque->nums = (int *)malloc(sizeof(int) * deque->queCapacity);\n    deque->front = deque->queSize = 0;\n    return deque;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayDeque(ArrayDeque *deque) {\n    free(deque->nums);\n    free(deque);\n}\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity(ArrayDeque *deque) {\n    return deque->queCapacity;\n}\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size(ArrayDeque *deque) {\n    return deque->queSize;\n}\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(ArrayDeque *deque) {\n    return deque->queSize == 0;\n}\n\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nint dequeIndex(ArrayDeque *deque, int i) {\n    // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n    // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u56de\u5230\u5934\u90e8\n    // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n    return ((i + capacity(deque)) % capacity(deque));\n}\n\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(ArrayDeque *deque, int num) {\n    if (deque->queSize == capacity(deque)) {\n        printf(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\\r\\n\");\n        return;\n    }\n    // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n    // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u56de\u5230\u5c3e\u90e8\n    deque->front = dequeIndex(deque, deque->front - 1);\n    // \u5c06 num \u6dfb\u52a0\u5230\u961f\u9996\n    deque->nums[deque->front] = num;\n    deque->queSize++;\n}\n\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(ArrayDeque *deque, int num) {\n    if (deque->queSize == capacity(deque)) {\n        printf(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\\r\\n\");\n        return;\n    }\n    // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n    int rear = dequeIndex(deque, deque->front + deque->queSize);\n    // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n    deque->nums[rear] = num;\n    deque->queSize++;\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst(ArrayDeque *deque) {\n    // \u8bbf\u95ee\u5f02\u5e38\uff1a\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\n    assert(empty(deque) == 0);\n    return deque->nums[deque->front];\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast(ArrayDeque *deque) {\n    // \u8bbf\u95ee\u5f02\u5e38\uff1a\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\n    assert(empty(deque) == 0);\n    int last = dequeIndex(deque, deque->front + deque->queSize - 1);\n    return deque->nums[last];\n}\n\n/* \u961f\u9996\u51fa\u961f */\nint popFirst(ArrayDeque *deque) {\n    int num = peekFirst(deque);\n    // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    deque->front = dequeIndex(deque, deque->front + 1);\n    deque->queSize--;\n    return num;\n}\n\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast(ArrayDeque *deque) {\n    int num = peekLast(deque);\n    deque->queSize--;\n    return num;\n}\n\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nint *toArray(ArrayDeque *deque, int *queSize) {\n    *queSize = deque->queSize;\n    int *res = (int *)calloc(deque->queSize, sizeof(int));\n    int j = deque->front;\n    for (int i = 0; i < deque->queSize; i++) {\n        res[i] = deque->nums[j % deque->queCapacity];\n        j++;\n    }\n    return res;\n}\n
        array_deque.kt
        /* \u6784\u9020\u65b9\u6cd5 */\nclass ArrayDeque(capacity: Int) {\n    private var nums: IntArray = IntArray(capacity) // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    private var front: Int = 0 // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    private var queSize: Int = 0 // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n    fun capacity(): Int {\n        return nums.size\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    fun size(): Int {\n        return queSize\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    fun isEmpty(): Boolean {\n        return queSize == 0\n    }\n\n    /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n    private fun index(i: Int): Int {\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n        return (i + capacity()) % capacity()\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    fun pushFirst(num: Int) {\n        if (queSize == capacity()) {\n            println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n            return\n        }\n        // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n        front = index(front - 1)\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n        nums[front] = num\n        queSize++\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    fun pushLast(num: Int) {\n        if (queSize == capacity()) {\n            println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n            return\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        val rear = index(front + queSize)\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        nums[rear] = num\n        queSize++\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    fun popFirst(): Int {\n        val num = peekFirst()\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        front = index(front + 1)\n        queSize--\n        return num\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    fun popLast(): Int {\n        val num = peekLast()\n        queSize--\n        return num\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    fun peekFirst(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return nums[front]\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    fun peekLast(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n        val last = index(front + queSize - 1)\n        return nums[last]\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    fun toArray(): IntArray {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        val res = IntArray(queSize)\n        var i = 0\n        var j = front\n        while (i < queSize) {\n            res[i] = nums[index(j)]\n            i++\n            j++\n        }\n        return res\n    }\n}\n
        array_deque.rb
        ### \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 ###\nclass ArrayDeque\n  ### \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 ###\n  attr_reader :size\n\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize(capacity)\n    @nums = Array.new(capacity, 0)\n    @front = 0\n    @size = 0\n  end\n\n  ### \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf ###\n  def capacity\n    @nums.length\n  end\n\n  ### \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a ###\n  def is_empty?\n    size.zero?\n  end\n\n  ### \u961f\u9996\u5165\u961f ###\n  def push_first(num)\n    if size == capacity\n      puts '\u53cc\u5411\u961f\u5217\u5df2\u6ee1'\n      return\n    end\n\n    # \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n    # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n    @front = index(@front - 1)\n    # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n    @nums[@front] = num\n    @size += 1\n  end\n\n  ### \u961f\u5c3e\u5165\u961f ###\n  def push_last(num)\n    if size == capacity\n      puts '\u53cc\u5411\u961f\u5217\u5df2\u6ee1'\n      return\n    end\n\n    # \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n    rear = index(@front + size)\n    # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n    @nums[rear] = num\n    @size += 1\n  end\n\n  ### \u961f\u9996\u51fa\u961f ###\n  def pop_first\n    num = peek_first\n    # \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    @front = index(@front + 1)\n    @size -= 1\n    num\n  end\n\n  ### \u961f\u5c3e\u51fa\u961f ###\n  def pop_last\n    num = peek_last\n    @size -= 1\n    num\n  end\n\n  ### \u8bbf\u95ee\u961f\u9996\u5143\u7d20 ###\n  def peek_first\n    raise IndexError, '\u53cc\u5411\u961f\u5217\u4e3a\u7a7a' if is_empty?\n\n    @nums[@front]\n  end\n\n  ### \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 ###\n  def peek_last\n    raise IndexError, '\u53cc\u5411\u961f\u5217\u4e3a\u7a7a' if is_empty?\n\n    # \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n    last = index(@front + size - 1)\n    @nums[last]\n  end\n\n  ### \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 ###\n  def to_array\n    # \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n    res = []\n    for i in 0...size\n      res << @nums[index(@front + i)]\n    end\n    res\n  end\n\n  private\n\n  ### \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 ###\n  def index(i)\n    # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n    # \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n    # \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n    (i + capacity) % capacity\n  end\nend\n
        "},{"location":"chapter_stack_and_queue/deque/#533","title":"5.3.3 \u00a0 \u53cc\u5411\u961f\u5217\u5e94\u7528","text":"

        \u53cc\u5411\u961f\u5217\u517c\u5177\u6808\u4e0e\u961f\u5217\u7684\u903b\u8f91\uff0c\u56e0\u6b64\u5b83\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e24\u8005\u7684\u6240\u6709\u5e94\u7528\u573a\u666f\uff0c\u540c\u65f6\u63d0\u4f9b\u66f4\u9ad8\u7684\u81ea\u7531\u5ea6\u3002

        \u6211\u4eec\u77e5\u9053\uff0c\u8f6f\u4ef6\u7684\u201c\u64a4\u9500\u201d\u529f\u80fd\u901a\u5e38\u4f7f\u7528\u6808\u6765\u5b9e\u73b0\uff1a\u7cfb\u7edf\u5c06\u6bcf\u6b21\u66f4\u6539\u64cd\u4f5c push \u5230\u6808\u4e2d\uff0c\u7136\u540e\u901a\u8fc7 pop \u5b9e\u73b0\u64a4\u9500\u3002\u7136\u800c\uff0c\u8003\u8651\u5230\u7cfb\u7edf\u8d44\u6e90\u7684\u9650\u5236\uff0c\u8f6f\u4ef6\u901a\u5e38\u4f1a\u9650\u5236\u64a4\u9500\u7684\u6b65\u6570\uff08\u4f8b\u5982\u4ec5\u5141\u8bb8\u4fdd\u5b58 \\(50\\) \u6b65\uff09\u3002\u5f53\u6808\u7684\u957f\u5ea6\u8d85\u8fc7 \\(50\\) \u65f6\uff0c\u8f6f\u4ef6\u9700\u8981\u5728\u6808\u5e95\uff08\u961f\u9996\uff09\u6267\u884c\u5220\u9664\u64cd\u4f5c\u3002\u4f46\u6808\u65e0\u6cd5\u5b9e\u73b0\u8be5\u529f\u80fd\uff0c\u6b64\u65f6\u5c31\u9700\u8981\u4f7f\u7528\u53cc\u5411\u961f\u5217\u6765\u66ff\u4ee3\u6808\u3002\u8bf7\u6ce8\u610f\uff0c\u201c\u64a4\u9500\u201d\u7684\u6838\u5fc3\u903b\u8f91\u4ecd\u7136\u9075\u5faa\u6808\u7684\u5148\u5165\u540e\u51fa\u539f\u5219\uff0c\u53ea\u662f\u53cc\u5411\u961f\u5217\u80fd\u591f\u66f4\u52a0\u7075\u6d3b\u5730\u5b9e\u73b0\u4e00\u4e9b\u989d\u5916\u903b\u8f91\u3002

        "},{"location":"chapter_stack_and_queue/queue/","title":"5.2 \u00a0 \u961f\u5217","text":"

        \u961f\u5217\uff08queue\uff09\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u5148\u51fa\u89c4\u5219\u7684\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002\u987e\u540d\u601d\u4e49\uff0c\u961f\u5217\u6a21\u62df\u4e86\u6392\u961f\u73b0\u8c61\uff0c\u5373\u65b0\u6765\u7684\u4eba\u4e0d\u65ad\u52a0\u5165\u961f\u5217\u5c3e\u90e8\uff0c\u800c\u4f4d\u4e8e\u961f\u5217\u5934\u90e8\u7684\u4eba\u9010\u4e2a\u79bb\u5f00\u3002

        \u5982\u56fe 5-4 \u6240\u793a\uff0c\u6211\u4eec\u5c06\u961f\u5217\u5934\u90e8\u79f0\u4e3a\u201c\u961f\u9996\u201d\uff0c\u5c3e\u90e8\u79f0\u4e3a\u201c\u961f\u5c3e\u201d\uff0c\u5c06\u628a\u5143\u7d20\u52a0\u5165\u961f\u5c3e\u7684\u64cd\u4f5c\u79f0\u4e3a\u201c\u5165\u961f\u201d\uff0c\u5220\u9664\u961f\u9996\u5143\u7d20\u7684\u64cd\u4f5c\u79f0\u4e3a\u201c\u51fa\u961f\u201d\u3002

        \u56fe 5-4 \u00a0 \u961f\u5217\u7684\u5148\u5165\u5148\u51fa\u89c4\u5219

        "},{"location":"chapter_stack_and_queue/queue/#521","title":"5.2.1 \u00a0 \u961f\u5217\u5e38\u7528\u64cd\u4f5c","text":"

        \u961f\u5217\u7684\u5e38\u89c1\u64cd\u4f5c\u5982\u8868 5-2 \u6240\u793a\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u7684\u65b9\u6cd5\u540d\u79f0\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u6211\u4eec\u5728\u6b64\u91c7\u7528\u4e0e\u6808\u76f8\u540c\u7684\u65b9\u6cd5\u547d\u540d\u3002

        \u8868 5-2 \u00a0 \u961f\u5217\u64cd\u4f5c\u6548\u7387

        \u65b9\u6cd5\u540d \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push() \u5143\u7d20\u5165\u961f\uff0c\u5373\u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u961f\u5c3e \\(O(1)\\) pop() \u961f\u9996\u5143\u7d20\u51fa\u961f \\(O(1)\\) peek() \u8bbf\u95ee\u961f\u9996\u5143\u7d20 \\(O(1)\\)

        \u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u4e2d\u73b0\u6210\u7684\u961f\u5217\u7c7b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby queue.py
        from collections import deque\n\n# \u521d\u59cb\u5316\u961f\u5217\n# \u5728 Python \u4e2d\uff0c\u6211\u4eec\u4e00\u822c\u5c06\u53cc\u5411\u961f\u5217\u7c7b deque \u5f53\u4f5c\u961f\u5217\u4f7f\u7528\n# \u867d\u7136 queue.Queue() \u662f\u7eaf\u6b63\u7684\u961f\u5217\u7c7b\uff0c\u4f46\u4e0d\u592a\u597d\u7528\uff0c\u56e0\u6b64\u4e0d\u63a8\u8350\nque: deque[int] = deque()\n\n# \u5143\u7d20\u5165\u961f\nque.append(1)\nque.append(3)\nque.append(2)\nque.append(5)\nque.append(4)\n\n# \u8bbf\u95ee\u961f\u9996\u5143\u7d20\nfront: int = que[0]\n\n# \u5143\u7d20\u51fa\u961f\npop: int = que.popleft()\n\n# \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\nsize: int = len(que)\n\n# \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = len(que) == 0\n
        queue.cpp
        /* \u521d\u59cb\u5316\u961f\u5217 */\nqueue<int> queue;\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint front = queue.front();\n\n/* \u5143\u7d20\u51fa\u961f */\nqueue.pop();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.size();\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty = queue.empty();\n
        queue.java
        /* \u521d\u59cb\u5316\u961f\u5217 */\nQueue<Integer> queue = new LinkedList<>();\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.offer(1);\nqueue.offer(3);\nqueue.offer(2);\nqueue.offer(5);\nqueue.offer(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.peek();\n\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.poll();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.size();\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = queue.isEmpty();\n
        queue.cs
        /* \u521d\u59cb\u5316\u961f\u5217 */\nQueue<int> queue = new();\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.Enqueue(1);\nqueue.Enqueue(3);\nqueue.Enqueue(2);\nqueue.Enqueue(5);\nqueue.Enqueue(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.Peek();\n\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.Dequeue();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.Count;\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = queue.Count == 0;\n
        queue_test.go
        /* \u521d\u59cb\u5316\u961f\u5217 */\n// \u5728 Go \u4e2d\uff0c\u5c06 list \u4f5c\u4e3a\u961f\u5217\u6765\u4f7f\u7528\nqueue := list.New()\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.PushBack(1)\nqueue.PushBack(3)\nqueue.PushBack(2)\nqueue.PushBack(5)\nqueue.PushBack(4)\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek := queue.Front()\n\n/* \u5143\u7d20\u51fa\u961f */\npop := queue.Front()\nqueue.Remove(pop)\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nsize := queue.Len()\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty := queue.Len() == 0\n
        queue.swift
        /* \u521d\u59cb\u5316\u961f\u5217 */\n// Swift \u6ca1\u6709\u5185\u7f6e\u7684\u961f\u5217\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u961f\u5217\u6765\u4f7f\u7528\nvar queue: [Int] = []\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.append(1)\nqueue.append(3)\nqueue.append(2)\nqueue.append(5)\nqueue.append(4)\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nlet peek = queue.first!\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u7531\u4e8e\u662f\u6570\u7ec4\uff0c\u56e0\u6b64 removeFirst \u7684\u590d\u6742\u5ea6\u4e3a O(n)\nlet pool = queue.removeFirst()\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = queue.count\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = queue.isEmpty\n
        queue.js
        /* \u521d\u59cb\u5316\u961f\u5217 */\n// JavaScript \u6ca1\u6709\u5185\u7f6e\u7684\u961f\u5217\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u961f\u5217\u6765\u4f7f\u7528\nconst queue = [];\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nconst peek = queue[0];\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u5e95\u5c42\u662f\u6570\u7ec4\uff0c\u56e0\u6b64 shift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst pop = queue.shift();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = queue.length;\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst empty = queue.length === 0;\n
        queue.ts
        /* \u521d\u59cb\u5316\u961f\u5217 */\n// TypeScript \u6ca1\u6709\u5185\u7f6e\u7684\u961f\u5217\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u961f\u5217\u6765\u4f7f\u7528\nconst queue: number[] = [];\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nconst peek = queue[0];\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u5e95\u5c42\u662f\u6570\u7ec4\uff0c\u56e0\u6b64 shift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst pop = queue.shift();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = queue.length;\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst empty = queue.length === 0;\n
        queue.dart
        /* \u521d\u59cb\u5316\u961f\u5217 */\n// \u5728 Dart \u4e2d\uff0c\u961f\u5217\u7c7b Qeque \u662f\u53cc\u5411\u961f\u5217\uff0c\u4e5f\u53ef\u4f5c\u4e3a\u961f\u5217\u4f7f\u7528\nQueue<int> queue = Queue();\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.add(1);\nqueue.add(3);\nqueue.add(2);\nqueue.add(5);\nqueue.add(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.first;\n\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.removeFirst();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.length;\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = queue.isEmpty;\n
        queue.rs
        /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 Rust \u4e2d\u4f7f\u7528\u53cc\u5411\u961f\u5217\u4f5c\u4e3a\u666e\u901a\u961f\u5217\u6765\u4f7f\u7528\nlet mut deque: VecDeque<u32> = VecDeque::new();\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.push_back(1);\ndeque.push_back(3);\ndeque.push_back(2);\ndeque.push_back(5);\ndeque.push_back(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nif let Some(front) = deque.front() {\n}\n\n/* \u5143\u7d20\u51fa\u961f */\nif let Some(pop) = deque.pop_front() {\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = deque.len();\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = deque.is_empty();\n
        queue.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e\u961f\u5217\n
        queue.kt
        /* \u521d\u59cb\u5316\u961f\u5217 */\nval queue = LinkedList<Int>()\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.offer(1)\nqueue.offer(3)\nqueue.offer(2)\nqueue.offer(5)\nqueue.offer(4)\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nval peek = queue.peek()\n\n/* \u5143\u7d20\u51fa\u961f */\nval pop = queue.poll()\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nval size = queue.size\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nval isEmpty = queue.isEmpty()\n
        queue.rb
        # \u521d\u59cb\u5316\u961f\u5217\n# Ruby \u5185\u7f6e\u7684\u961f\u5217\uff08Thread::Queue) \u6ca1\u6709 peek \u548c\u904d\u5386\u65b9\u6cd5\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u961f\u5217\u6765\u4f7f\u7528\nqueue = []\n\n# \u5143\u7d20\u5165\u961f\nqueue.push(1)\nqueue.push(3)\nqueue.push(2)\nqueue.push(5)\nqueue.push(4)\n\n# \u8bbf\u95ee\u961f\u5217\u5143\u7d20\npeek = queue.first\n\n# \u5143\u7d20\u51fa\u961f\n# \u6e05\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cArray#shift \u65b9\u6cd5\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\npop = queue.shift\n\n# \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\nsize = queue.length\n\n# \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\nis_empty = queue.empty?\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_stack_and_queue/queue/#522","title":"5.2.2 \u00a0 \u961f\u5217\u5b9e\u73b0","text":"

        \u4e3a\u4e86\u5b9e\u73b0\u961f\u5217\uff0c\u6211\u4eec\u9700\u8981\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5728\u4e00\u7aef\u6dfb\u52a0\u5143\u7d20\uff0c\u5e76\u5728\u53e6\u4e00\u7aef\u5220\u9664\u5143\u7d20\uff0c\u94fe\u8868\u548c\u6570\u7ec4\u90fd\u7b26\u5408\u8981\u6c42\u3002

        "},{"location":"chapter_stack_and_queue/queue/#1","title":"1. \u00a0 \u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0","text":"

        \u5982\u56fe 5-5 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u94fe\u8868\u7684\u201c\u5934\u8282\u70b9\u201d\u548c\u201c\u5c3e\u8282\u70b9\u201d\u5206\u522b\u89c6\u4e3a\u201c\u961f\u9996\u201d\u548c\u201c\u961f\u5c3e\u201d\uff0c\u89c4\u5b9a\u961f\u5c3e\u4ec5\u53ef\u6dfb\u52a0\u8282\u70b9\uff0c\u961f\u9996\u4ec5\u53ef\u5220\u9664\u8282\u70b9\u3002

        LinkedListQueuepush()pop()

        \u56fe 5-5 \u00a0 \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c

        \u4ee5\u4e0b\u662f\u7528\u94fe\u8868\u5b9e\u73b0\u961f\u5217\u7684\u4ee3\u7801\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linkedlist_queue.py
        class LinkedListQueue:\n    \"\"\"\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217\"\"\"\n\n    def __init__(self):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        self._front: ListNode | None = None  # \u5934\u8282\u70b9 front\n        self._rear: ListNode | None = None  # \u5c3e\u8282\u70b9 rear\n        self._size: int = 0\n\n    def size(self) -> int:\n        \"\"\"\u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n        return self._size == 0\n\n    def push(self, num: int):\n        \"\"\"\u5165\u961f\"\"\"\n        # \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n        node = ListNode(num)\n        # \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n        if self._front is None:\n            self._front = node\n            self._rear = node\n        # \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n        else:\n            self._rear.next = node\n            self._rear = node\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"\u51fa\u961f\"\"\"\n        num = self.peek()\n        # \u5220\u9664\u5934\u8282\u70b9\n        self._front = self._front.next\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u961f\u5217\u4e3a\u7a7a\")\n        return self._front.val\n\n    def to_list(self) -> list[int]:\n        \"\"\"\u8f6c\u5316\u4e3a\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n        queue = []\n        temp = self._front\n        while temp:\n            queue.append(temp.val)\n            temp = temp.next\n        return queue\n
        linkedlist_queue.cpp
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n  private:\n    ListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n    int queSize;\n\n  public:\n    LinkedListQueue() {\n        front = nullptr;\n        rear = nullptr;\n        queSize = 0;\n    }\n\n    ~LinkedListQueue() {\n        // \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\n        freeMemoryLinkedList(front);\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    int size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    bool isEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u5165\u961f */\n    void push(int num) {\n        // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n        ListNode *node = new ListNode(num);\n        // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n        if (front == nullptr) {\n            front = node;\n            rear = node;\n        }\n        // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n        else {\n            rear->next = node;\n            rear = node;\n        }\n        queSize++;\n    }\n\n    /* \u51fa\u961f */\n    int pop() {\n        int num = peek();\n        // \u5220\u9664\u5934\u8282\u70b9\n        ListNode *tmp = front;\n        front = front->next;\n        // \u91ca\u653e\u5185\u5b58\n        delete tmp;\n        queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    int peek() {\n        if (size() == 0)\n            throw out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\n        return front->val;\n    }\n\n    /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Vector \u5e76\u8fd4\u56de */\n    vector<int> toVector() {\n        ListNode *node = front;\n        vector<int> res(size());\n        for (int i = 0; i < res.size(); i++) {\n            res[i] = node->val;\n            node = node->next;\n        }\n        return res;\n    }\n};\n
        linkedlist_queue.java
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n    private ListNode front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n    private int queSize = 0;\n\n    public LinkedListQueue() {\n        front = null;\n        rear = null;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    public int size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u5165\u961f */\n    public void push(int num) {\n        // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n        ListNode node = new ListNode(num);\n        // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n        if (front == null) {\n            front = node;\n            rear = node;\n        // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n        } else {\n            rear.next = node;\n            rear = node;\n        }\n        queSize++;\n    }\n\n    /* \u51fa\u961f */\n    public int pop() {\n        int num = peek();\n        // \u5220\u9664\u5934\u8282\u70b9\n        front = front.next;\n        queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return front.val;\n    }\n\n    /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    public int[] toArray() {\n        ListNode node = front;\n        int[] res = new int[size()];\n        for (int i = 0; i < res.length; i++) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_queue.cs
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n    ListNode? front, rear;  // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear \n    int queSize = 0;\n\n    public LinkedListQueue() {\n        front = null;\n        rear = null;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    public int Size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    public bool IsEmpty() {\n        return Size() == 0;\n    }\n\n    /* \u5165\u961f */\n    public void Push(int num) {\n        // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n        ListNode node = new(num);\n        // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n        if (front == null) {\n            front = node;\n            rear = node;\n            // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n        } else if (rear != null) {\n            rear.next = node;\n            rear = node;\n        }\n        queSize++;\n    }\n\n    /* \u51fa\u961f */\n    public int Pop() {\n        int num = Peek();\n        // \u5220\u9664\u5934\u8282\u70b9\n        front = front?.next;\n        queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    public int Peek() {\n        if (IsEmpty())\n            throw new Exception();\n        return front!.val;\n    }\n\n    /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    public int[] ToArray() {\n        if (front == null)\n            return [];\n\n        ListNode? node = front;\n        int[] res = new int[Size()];\n        for (int i = 0; i < res.Length; i++) {\n            res[i] = node!.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_queue.go
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\ntype linkedListQueue struct {\n    // \u4f7f\u7528\u5185\u7f6e\u5305 list \u6765\u5b9e\u73b0\u961f\u5217\n    data *list.List\n}\n\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newLinkedListQueue() *linkedListQueue {\n    return &linkedListQueue{\n        data: list.New(),\n    }\n}\n\n/* \u5165\u961f */\nfunc (s *linkedListQueue) push(value any) {\n    s.data.PushBack(value)\n}\n\n/* \u51fa\u961f */\nfunc (s *linkedListQueue) pop() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Front()\n    s.data.Remove(e)\n    return e.Value\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (s *linkedListQueue) peek() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Front()\n    return e.Value\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (s *linkedListQueue) size() int {\n    return s.data.Len()\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListQueue) isEmpty() bool {\n    return s.data.Len() == 0\n}\n\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListQueue) toList() *list.List {\n    return s.data\n}\n
        linkedlist_queue.swift
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n    private var front: ListNode? // \u5934\u8282\u70b9\n    private var rear: ListNode? // \u5c3e\u8282\u70b9\n    private var _size: Int\n\n    init() {\n        _size = 0\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    func size() -> Int {\n        _size\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* \u5165\u961f */\n    func push(num: Int) {\n        // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n        let node = ListNode(x: num)\n        // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n        if front == nil {\n            front = node\n            rear = node\n        }\n        // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n        else {\n            rear?.next = node\n            rear = node\n        }\n        _size += 1\n    }\n\n    /* \u51fa\u961f */\n    @discardableResult\n    func pop() -> Int {\n        let num = peek()\n        // \u5220\u9664\u5934\u8282\u70b9\n        front = front?.next\n        _size -= 1\n        return num\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    func peek() -> Int {\n        if isEmpty() {\n            fatalError(\"\u961f\u5217\u4e3a\u7a7a\")\n        }\n        return front!.val\n    }\n\n    /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    func toArray() -> [Int] {\n        var node = front\n        var res = Array(repeating: 0, count: size())\n        for i in res.indices {\n            res[i] = node!.val\n            node = node?.next\n        }\n        return res\n    }\n}\n
        linkedlist_queue.js
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n    #front; // \u5934\u8282\u70b9 #front\n    #rear; // \u5c3e\u8282\u70b9 #rear\n    #queSize = 0;\n\n    constructor() {\n        this.#front = null;\n        this.#rear = null;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    get size() {\n        return this.#queSize;\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    isEmpty() {\n        return this.size === 0;\n    }\n\n    /* \u5165\u961f */\n    push(num) {\n        // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n        const node = new ListNode(num);\n        // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n        if (!this.#front) {\n            this.#front = node;\n            this.#rear = node;\n            // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n        } else {\n            this.#rear.next = node;\n            this.#rear = node;\n        }\n        this.#queSize++;\n    }\n\n    /* \u51fa\u961f */\n    pop() {\n        const num = this.peek();\n        // \u5220\u9664\u5934\u8282\u70b9\n        this.#front = this.#front.next;\n        this.#queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    peek() {\n        if (this.size === 0) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n        return this.#front.val;\n    }\n\n    /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    toArray() {\n        let node = this.#front;\n        const res = new Array(this.size);\n        for (let i = 0; i < res.length; i++) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_queue.ts
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n    private front: ListNode | null; // \u5934\u8282\u70b9 front\n    private rear: ListNode | null; // \u5c3e\u8282\u70b9 rear\n    private queSize: number = 0;\n\n    constructor() {\n        this.front = null;\n        this.rear = null;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    get size(): number {\n        return this.queSize;\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    isEmpty(): boolean {\n        return this.size === 0;\n    }\n\n    /* \u5165\u961f */\n    push(num: number): void {\n        // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n        const node = new ListNode(num);\n        // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n        if (!this.front) {\n            this.front = node;\n            this.rear = node;\n            // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n        } else {\n            this.rear!.next = node;\n            this.rear = node;\n        }\n        this.queSize++;\n    }\n\n    /* \u51fa\u961f */\n    pop(): number {\n        const num = this.peek();\n        if (!this.front) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n        // \u5220\u9664\u5934\u8282\u70b9\n        this.front = this.front.next;\n        this.queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    peek(): number {\n        if (this.size === 0) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n        return this.front!.val;\n    }\n\n    /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    toArray(): number[] {\n        let node = this.front;\n        const res = new Array<number>(this.size);\n        for (let i = 0; i < res.length; i++) {\n            res[i] = node!.val;\n            node = node!.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_queue.dart
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n  ListNode? _front; // \u5934\u8282\u70b9 _front\n  ListNode? _rear; // \u5c3e\u8282\u70b9 _rear\n  int _queSize = 0; // \u961f\u5217\u957f\u5ea6\n\n  LinkedListQueue() {\n    _front = null;\n    _rear = null;\n  }\n\n  /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n  int size() {\n    return _queSize;\n  }\n\n  /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n  bool isEmpty() {\n    return _queSize == 0;\n  }\n\n  /* \u5165\u961f */\n  void push(int _num) {\n    // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 _num\n    final node = ListNode(_num);\n    // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n    if (_front == null) {\n      _front = node;\n      _rear = node;\n    } else {\n      // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n      _rear!.next = node;\n      _rear = node;\n    }\n    _queSize++;\n  }\n\n  /* \u51fa\u961f */\n  int pop() {\n    final int _num = peek();\n    // \u5220\u9664\u5934\u8282\u70b9\n    _front = _front!.next;\n    _queSize--;\n    return _num;\n  }\n\n  /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n  int peek() {\n    if (_queSize == 0) {\n      throw Exception('\u961f\u5217\u4e3a\u7a7a');\n    }\n    return _front!.val;\n  }\n\n  /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n  List<int> toArray() {\n    ListNode? node = _front;\n    final List<int> queue = [];\n    while (node != null) {\n      queue.add(node.val);\n      node = node.next;\n    }\n    return queue;\n  }\n}\n
        linkedlist_queue.rs
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\n#[allow(dead_code)]\npub struct LinkedListQueue<T> {\n    front: Option<Rc<RefCell<ListNode<T>>>>, // \u5934\u8282\u70b9 front\n    rear: Option<Rc<RefCell<ListNode<T>>>>,  // \u5c3e\u8282\u70b9 rear\n    que_size: usize,                         // \u961f\u5217\u7684\u957f\u5ea6\n}\n\nimpl<T: Copy> LinkedListQueue<T> {\n    pub fn new() -> Self {\n        Self {\n            front: None,\n            rear: None,\n            que_size: 0,\n        }\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    pub fn size(&self) -> usize {\n        return self.que_size;\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    pub fn is_empty(&self) -> bool {\n        return self.que_size == 0;\n    }\n\n    /* \u5165\u961f */\n    pub fn push(&mut self, num: T) {\n        // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n        let new_rear = ListNode::new(num);\n        match self.rear.take() {\n            // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n            Some(old_rear) => {\n                old_rear.borrow_mut().next = Some(new_rear.clone());\n                self.rear = Some(new_rear);\n            }\n            // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n            None => {\n                self.front = Some(new_rear.clone());\n                self.rear = Some(new_rear);\n            }\n        }\n        self.que_size += 1;\n    }\n\n    /* \u51fa\u961f */\n    pub fn pop(&mut self) -> Option<T> {\n        self.front.take().map(|old_front| {\n            match old_front.borrow_mut().next.take() {\n                Some(new_front) => {\n                    self.front = Some(new_front);\n                }\n                None => {\n                    self.rear.take();\n                }\n            }\n            self.que_size -= 1;\n            old_front.borrow().val\n        })\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n        self.front.as_ref()\n    }\n\n    /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n        let mut res: Vec<T> = Vec::new();\n\n        fn recur<T: Copy>(cur: Option<&Rc<RefCell<ListNode<T>>>>, res: &mut Vec<T>) {\n            if let Some(cur) = cur {\n                res.push(cur.borrow().val);\n                recur(cur.borrow().next.as_ref(), res);\n            }\n        }\n\n        recur(head, &mut res);\n\n        res\n    }\n}\n
        linkedlist_queue.c
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\ntypedef struct {\n    ListNode *front, *rear;\n    int queSize;\n} LinkedListQueue;\n\n/* \u6784\u9020\u51fd\u6570 */\nLinkedListQueue *newLinkedListQueue() {\n    LinkedListQueue *queue = (LinkedListQueue *)malloc(sizeof(LinkedListQueue));\n    queue->front = NULL;\n    queue->rear = NULL;\n    queue->queSize = 0;\n    return queue;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListQueue(LinkedListQueue *queue) {\n    // \u91ca\u653e\u6240\u6709\u8282\u70b9\n    while (queue->front != NULL) {\n        ListNode *tmp = queue->front;\n        queue->front = queue->front->next;\n        free(tmp);\n    }\n    // \u91ca\u653e queue \u7ed3\u6784\u4f53\n    free(queue);\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(LinkedListQueue *queue) {\n    return queue->queSize;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(LinkedListQueue *queue) {\n    return (size(queue) == 0);\n}\n\n/* \u5165\u961f */\nvoid push(LinkedListQueue *queue, int num) {\n    // \u5c3e\u8282\u70b9\u5904\u6dfb\u52a0 node\n    ListNode *node = newListNode(num);\n    // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n    if (queue->front == NULL) {\n        queue->front = node;\n        queue->rear = node;\n    }\n    // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n    else {\n        queue->rear->next = node;\n        queue->rear = node;\n    }\n    queue->queSize++;\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek(LinkedListQueue *queue) {\n    assert(size(queue) && queue->front);\n    return queue->front->val;\n}\n\n/* \u51fa\u961f */\nint pop(LinkedListQueue *queue) {\n    int num = peek(queue);\n    ListNode *tmp = queue->front;\n    queue->front = queue->front->next;\n    free(tmp);\n    queue->queSize--;\n    return num;\n}\n\n/* \u6253\u5370\u961f\u5217 */\nvoid printLinkedListQueue(LinkedListQueue *queue) {\n    int *arr = malloc(sizeof(int) * queue->queSize);\n    // \u62f7\u8d1d\u94fe\u8868\u4e2d\u7684\u6570\u636e\u5230\u6570\u7ec4\n    int i;\n    ListNode *node;\n    for (i = 0, node = queue->front; i < queue->queSize; i++) {\n        arr[i] = node->val;\n        node = node->next;\n    }\n    printArray(arr, queue->queSize);\n    free(arr);\n}\n
        linkedlist_queue.kt
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue(\n    // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n    private var front: ListNode? = null,\n    private var rear: ListNode? = null,\n    private var queSize: Int = 0\n) {\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    fun size(): Int {\n        return queSize\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    fun isEmpty(): Boolean {\n        return size() == 0\n    }\n\n    /* \u5165\u961f */\n    fun push(num: Int) {\n        // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n        val node = ListNode(num)\n        // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n        if (front == null) {\n            front = node\n            rear = node\n            // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n        } else {\n            rear?.next = node\n            rear = node\n        }\n        queSize++\n    }\n\n    /* \u51fa\u961f */\n    fun pop(): Int {\n        val num = peek()\n        // \u5220\u9664\u5934\u8282\u70b9\n        front = front?.next\n        queSize--\n        return num\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    fun peek(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return front!!._val\n    }\n\n    /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    fun toArray(): IntArray {\n        var node = front\n        val res = IntArray(size())\n        for (i in res.indices) {\n            res[i] = node!!._val\n            node = node.next\n        }\n        return res\n    }\n}\n
        linkedlist_queue.rb
        ### \u57fa\u4e8e\u94fe\u8868\u5934\u73b0\u7684\u961f\u5217 ###\nclass LinkedListQueue\n  ### \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 ###\n  attr_reader :size\n\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize\n    @front = nil  # \u5934\u8282\u70b9 front\n    @rear = nil   # \u5c3e\u8282\u70b9 rear\n    @size = 0\n  end\n\n  ### \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a ###\n  def is_empty?\n    @front.nil?\n  end\n\n  ### \u5165\u961f ###\n  def push(num)\n    # \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n    node = ListNode.new(num)\n\n    # \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\uff0c\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n    if @front.nil?\n      @front = node\n      @rear = node\n    # \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u4ee4\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n    else\n      @rear.next = node\n      @rear = node\n    end\n\n    @size += 1\n  end\n\n  ### \u51fa\u961f ###\n  def pop\n    num = peek\n    # \u5220\u9664\u5934\u8282\u70b9\n    @front = @front.next\n    @size -= 1\n    num\n  end\n\n  ### \u8bbf\u95ee\u961f\u9996\u5143\u7d20 ###\n  def peek\n    raise IndexError, '\u961f\u5217\u4e3a\u7a7a' if is_empty?\n\n    @front.val\n  end\n\n  ### \u5c06\u94fe\u8868\u4e3a Array \u5e76\u8fd4\u56de ###\n  def to_array\n    queue = []\n    temp = @front\n    while temp\n      queue << temp.val\n      temp = temp.next\n    end\n    queue\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_stack_and_queue/queue/#2","title":"2. \u00a0 \u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"

        \u5728\u6570\u7ec4\u4e2d\u5220\u9664\u9996\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u8fd9\u4f1a\u5bfc\u81f4\u51fa\u961f\u64cd\u4f5c\u6548\u7387\u8f83\u4f4e\u3002\u7136\u800c\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u5de7\u5999\u65b9\u6cd5\u6765\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\u3002

        \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u53d8\u91cf front \u6307\u5411\u961f\u9996\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u5e76\u7ef4\u62a4\u4e00\u4e2a\u53d8\u91cf size \u7528\u4e8e\u8bb0\u5f55\u961f\u5217\u957f\u5ea6\u3002\u5b9a\u4e49 rear = front + size \uff0c\u8fd9\u4e2a\u516c\u5f0f\u8ba1\u7b97\u51fa\u7684 rear \u6307\u5411\u961f\u5c3e\u5143\u7d20\u4e4b\u540e\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\u3002

        \u57fa\u4e8e\u6b64\u8bbe\u8ba1\uff0c\u6570\u7ec4\u4e2d\u5305\u542b\u5143\u7d20\u7684\u6709\u6548\u533a\u95f4\u4e3a [front, rear - 1]\uff0c\u5404\u79cd\u64cd\u4f5c\u7684\u5b9e\u73b0\u65b9\u6cd5\u5982\u56fe 5-6 \u6240\u793a\u3002

        • \u5165\u961f\u64cd\u4f5c\uff1a\u5c06\u8f93\u5165\u5143\u7d20\u8d4b\u503c\u7ed9 rear \u7d22\u5f15\u5904\uff0c\u5e76\u5c06 size \u589e\u52a0 1 \u3002
        • \u51fa\u961f\u64cd\u4f5c\uff1a\u53ea\u9700\u5c06 front \u589e\u52a0 1 \uff0c\u5e76\u5c06 size \u51cf\u5c11 1 \u3002

        \u53ef\u4ee5\u770b\u5230\uff0c\u5165\u961f\u548c\u51fa\u961f\u64cd\u4f5c\u90fd\u53ea\u9700\u8fdb\u884c\u4e00\u6b21\u64cd\u4f5c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(1)\\) \u3002

        ArrayQueuepush()pop()

        \u56fe 5-6 \u00a0 \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c

        \u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\u4e00\u4e2a\u95ee\u9898\uff1a\u5728\u4e0d\u65ad\u8fdb\u884c\u5165\u961f\u548c\u51fa\u961f\u7684\u8fc7\u7a0b\u4e2d\uff0cfront \u548c rear \u90fd\u5728\u5411\u53f3\u79fb\u52a8\uff0c\u5f53\u5b83\u4eec\u5230\u8fbe\u6570\u7ec4\u5c3e\u90e8\u65f6\u5c31\u65e0\u6cd5\u7ee7\u7eed\u79fb\u52a8\u4e86\u3002\u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u89c6\u4e3a\u9996\u5c3e\u76f8\u63a5\u7684\u201c\u73af\u5f62\u6570\u7ec4\u201d\u3002

        \u5bf9\u4e8e\u73af\u5f62\u6570\u7ec4\uff0c\u6211\u4eec\u9700\u8981\u8ba9 front \u6216 rear \u5728\u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u76f4\u63a5\u56de\u5230\u6570\u7ec4\u5934\u90e8\u7ee7\u7eed\u904d\u5386\u3002\u8fd9\u79cd\u5468\u671f\u6027\u89c4\u5f8b\u53ef\u4ee5\u901a\u8fc7\u201c\u53d6\u4f59\u64cd\u4f5c\u201d\u6765\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_queue.py
        class ArrayQueue:\n    \"\"\"\u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217\"\"\"\n\n    def __init__(self, size: int):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        self._nums: list[int] = [0] * size  # \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n        self._front: int = 0  # \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n        self._size: int = 0  # \u961f\u5217\u957f\u5ea6\n\n    def capacity(self) -> int:\n        \"\"\"\u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf\"\"\"\n        return len(self._nums)\n\n    def size(self) -> int:\n        \"\"\"\u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n        return self._size == 0\n\n    def push(self, num: int):\n        \"\"\"\u5165\u961f\"\"\"\n        if self._size == self.capacity():\n            raise IndexError(\"\u961f\u5217\u5df2\u6ee1\")\n        # \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n        rear: int = (self._front + self._size) % self.capacity()\n        # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        self._nums[rear] = num\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"\u51fa\u961f\"\"\"\n        num: int = self.peek()\n        # \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n        self._front = (self._front + 1) % self.capacity()\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u961f\u5217\u4e3a\u7a7a\")\n        return self._nums[self._front]\n\n    def to_list(self) -> list[int]:\n        \"\"\"\u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n        res = [0] * self.size()\n        j: int = self._front\n        for i in range(self.size()):\n            res[i] = self._nums[(j % self.capacity())]\n            j += 1\n        return res\n
        array_queue.cpp
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n  private:\n    int *nums;       // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    int front;       // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    int queSize;     // \u961f\u5217\u957f\u5ea6\n    int queCapacity; // \u961f\u5217\u5bb9\u91cf\n\n  public:\n    ArrayQueue(int capacity) {\n        // \u521d\u59cb\u5316\u6570\u7ec4\n        nums = new int[capacity];\n        queCapacity = capacity;\n        front = queSize = 0;\n    }\n\n    ~ArrayQueue() {\n        delete[] nums;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n    int capacity() {\n        return queCapacity;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    int size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    bool isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u5165\u961f */\n    void push(int num) {\n        if (queSize == queCapacity) {\n            cout << \"\u961f\u5217\u5df2\u6ee1\" << endl;\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n        int rear = (front + queSize) % queCapacity;\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u51fa\u961f */\n    int pop() {\n        int num = peek();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n        front = (front + 1) % queCapacity;\n        queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    int peek() {\n        if (isEmpty())\n            throw out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\n        return nums[front];\n    }\n\n    /* \u5c06\u6570\u7ec4\u8f6c\u5316\u4e3a Vector \u5e76\u8fd4\u56de */\n    vector<int> toVector() {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        vector<int> arr(queSize);\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            arr[i] = nums[j % queCapacity];\n        }\n        return arr;\n    }\n};\n
        array_queue.java
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n    private int[] nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    private int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    private int queSize; // \u961f\u5217\u957f\u5ea6\n\n    public ArrayQueue(int capacity) {\n        nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n    public int capacity() {\n        return nums.length;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    public int size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    public boolean isEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u5165\u961f */\n    public void push(int num) {\n        if (queSize == capacity()) {\n            System.out.println(\"\u961f\u5217\u5df2\u6ee1\");\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n        int rear = (front + queSize) % capacity();\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u51fa\u961f */\n    public int pop() {\n        int num = peek();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n        front = (front + 1) % capacity();\n        queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return nums[front];\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4 */\n    public int[] toArray() {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[j % capacity()];\n        }\n        return res;\n    }\n}\n
        array_queue.cs
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n    int[] nums;  // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    int front;   // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    int queSize; // \u961f\u5217\u957f\u5ea6\n\n    public ArrayQueue(int capacity) {\n        nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n    int Capacity() {\n        return nums.Length;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    public int Size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    public bool IsEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u5165\u961f */\n    public void Push(int num) {\n        if (queSize == Capacity()) {\n            Console.WriteLine(\"\u961f\u5217\u5df2\u6ee1\");\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n        int rear = (front + queSize) % Capacity();\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u51fa\u961f */\n    public int Pop() {\n        int num = Peek();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n        front = (front + 1) % Capacity();\n        queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    public int Peek() {\n        if (IsEmpty())\n            throw new Exception();\n        return nums[front];\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4 */\n    public int[] ToArray() {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[j % this.Capacity()];\n        }\n        return res;\n    }\n}\n
        array_queue.go
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\ntype arrayQueue struct {\n    nums        []int // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    front       int   // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    queSize     int   // \u961f\u5217\u957f\u5ea6\n    queCapacity int   // \u961f\u5217\u5bb9\u91cf\uff08\u5373\u6700\u5927\u5bb9\u7eb3\u5143\u7d20\u6570\u91cf\uff09\n}\n\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newArrayQueue(queCapacity int) *arrayQueue {\n    return &arrayQueue{\n        nums:        make([]int, queCapacity),\n        queCapacity: queCapacity,\n        front:       0,\n        queSize:     0,\n    }\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (q *arrayQueue) size() int {\n    return q.queSize\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (q *arrayQueue) isEmpty() bool {\n    return q.queSize == 0\n}\n\n/* \u5165\u961f */\nfunc (q *arrayQueue) push(num int) {\n    // \u5f53 rear == queCapacity \u8868\u793a\u961f\u5217\u5df2\u6ee1\n    if q.queSize == q.queCapacity {\n        return\n    }\n    // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n    // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n    rear := (q.front + q.queSize) % q.queCapacity\n    // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n    q.nums[rear] = num\n    q.queSize++\n}\n\n/* \u51fa\u961f */\nfunc (q *arrayQueue) pop() any {\n    num := q.peek()\n    if num == nil {\n        return nil\n    }\n\n    // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n    q.front = (q.front + 1) % q.queCapacity\n    q.queSize--\n    return num\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (q *arrayQueue) peek() any {\n    if q.isEmpty() {\n        return nil\n    }\n    return q.nums[q.front]\n}\n\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (q *arrayQueue) toSlice() []int {\n    rear := (q.front + q.queSize)\n    if rear >= q.queCapacity {\n        rear %= q.queCapacity\n        return append(q.nums[q.front:], q.nums[:rear]...)\n    }\n    return q.nums[q.front:rear]\n}\n
        array_queue.swift
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n    private var nums: [Int] // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    private var front: Int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    private var _size: Int // \u961f\u5217\u957f\u5ea6\n\n    init(capacity: Int) {\n        // \u521d\u59cb\u5316\u6570\u7ec4\n        nums = Array(repeating: 0, count: capacity)\n        front = 0\n        _size = 0\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n    func capacity() -> Int {\n        nums.count\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    func size() -> Int {\n        _size\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* \u5165\u961f */\n    func push(num: Int) {\n        if size() == capacity() {\n            print(\"\u961f\u5217\u5df2\u6ee1\")\n            return\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n        let rear = (front + size()) % capacity()\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        nums[rear] = num\n        _size += 1\n    }\n\n    /* \u51fa\u961f */\n    @discardableResult\n    func pop() -> Int {\n        let num = peek()\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n        front = (front + 1) % capacity()\n        _size -= 1\n        return num\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    func peek() -> Int {\n        if isEmpty() {\n            fatalError(\"\u961f\u5217\u4e3a\u7a7a\")\n        }\n        return nums[front]\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4 */\n    func toArray() -> [Int] {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        (front ..< front + size()).map { nums[$0 % capacity()] }\n    }\n}\n
        array_queue.js
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n    #nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    #front = 0; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    #queSize = 0; // \u961f\u5217\u957f\u5ea6\n\n    constructor(capacity) {\n        this.#nums = new Array(capacity);\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n    get capacity() {\n        return this.#nums.length;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    get size() {\n        return this.#queSize;\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    isEmpty() {\n        return this.#queSize === 0;\n    }\n\n    /* \u5165\u961f */\n    push(num) {\n        if (this.size === this.capacity) {\n            console.log('\u961f\u5217\u5df2\u6ee1');\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n        const rear = (this.#front + this.size) % this.capacity;\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        this.#nums[rear] = num;\n        this.#queSize++;\n    }\n\n    /* \u51fa\u961f */\n    pop() {\n        const num = this.peek();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n        this.#front = (this.#front + 1) % this.capacity;\n        this.#queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    peek() {\n        if (this.isEmpty()) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n        return this.#nums[this.#front];\n    }\n\n    /* \u8fd4\u56de Array */\n    toArray() {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        const arr = new Array(this.size);\n        for (let i = 0, j = this.#front; i < this.size; i++, j++) {\n            arr[i] = this.#nums[j % this.capacity];\n        }\n        return arr;\n    }\n}\n
        array_queue.ts
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n    private nums: number[]; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    private front: number; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    private queSize: number; // \u961f\u5217\u957f\u5ea6\n\n    constructor(capacity: number) {\n        this.nums = new Array(capacity);\n        this.front = this.queSize = 0;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n    get capacity(): number {\n        return this.nums.length;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    get size(): number {\n        return this.queSize;\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    isEmpty(): boolean {\n        return this.queSize === 0;\n    }\n\n    /* \u5165\u961f */\n    push(num: number): void {\n        if (this.size === this.capacity) {\n            console.log('\u961f\u5217\u5df2\u6ee1');\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n        const rear = (this.front + this.queSize) % this.capacity;\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        this.nums[rear] = num;\n        this.queSize++;\n    }\n\n    /* \u51fa\u961f */\n    pop(): number {\n        const num = this.peek();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n        this.front = (this.front + 1) % this.capacity;\n        this.queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    peek(): number {\n        if (this.isEmpty()) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n        return this.nums[this.front];\n    }\n\n    /* \u8fd4\u56de Array */\n    toArray(): number[] {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        const arr = new Array(this.size);\n        for (let i = 0, j = this.front; i < this.size; i++, j++) {\n            arr[i] = this.nums[j % this.capacity];\n        }\n        return arr;\n    }\n}\n
        array_queue.dart
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n  late List<int> _nums; // \u7528\u4e8e\u50a8\u5b58\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n  late int _front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n  late int _queSize; // \u961f\u5217\u957f\u5ea6\n\n  ArrayQueue(int capacity) {\n    _nums = List.filled(capacity, 0);\n    _front = _queSize = 0;\n  }\n\n  /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n  int capaCity() {\n    return _nums.length;\n  }\n\n  /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n  int size() {\n    return _queSize;\n  }\n\n  /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n  bool isEmpty() {\n    return _queSize == 0;\n  }\n\n  /* \u5165\u961f */\n  void push(int _num) {\n    if (_queSize == capaCity()) {\n      throw Exception(\"\u961f\u5217\u5df2\u6ee1\");\n    }\n    // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n    // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n    int rear = (_front + _queSize) % capaCity();\n    // \u5c06 _num \u6dfb\u52a0\u81f3\u961f\u5c3e\n    _nums[rear] = _num;\n    _queSize++;\n  }\n\n  /* \u51fa\u961f */\n  int pop() {\n    int _num = peek();\n    // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n    _front = (_front + 1) % capaCity();\n    _queSize--;\n    return _num;\n  }\n\n  /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n  int peek() {\n    if (isEmpty()) {\n      throw Exception(\"\u961f\u5217\u4e3a\u7a7a\");\n    }\n    return _nums[_front];\n  }\n\n  /* \u8fd4\u56de Array */\n  List<int> toArray() {\n    // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n    final List<int> res = List.filled(_queSize, 0);\n    for (int i = 0, j = _front; i < _queSize; i++, j++) {\n      res[i] = _nums[j % capaCity()];\n    }\n    return res;\n  }\n}\n
        array_queue.rs
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nstruct ArrayQueue<T> {\n    nums: Vec<T>,      // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    front: i32,        // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    que_size: i32,     // \u961f\u5217\u957f\u5ea6\n    que_capacity: i32, // \u961f\u5217\u5bb9\u91cf\n}\n\nimpl<T: Copy + Default> ArrayQueue<T> {\n    /* \u6784\u9020\u65b9\u6cd5 */\n    fn new(capacity: i32) -> ArrayQueue<T> {\n        ArrayQueue {\n            nums: vec![T::default(); capacity as usize],\n            front: 0,\n            que_size: 0,\n            que_capacity: capacity,\n        }\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n    fn capacity(&self) -> i32 {\n        self.que_capacity\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    fn size(&self) -> i32 {\n        self.que_size\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    fn is_empty(&self) -> bool {\n        self.que_size == 0\n    }\n\n    /* \u5165\u961f */\n    fn push(&mut self, num: T) {\n        if self.que_size == self.capacity() {\n            println!(\"\u961f\u5217\u5df2\u6ee1\");\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n        let rear = (self.front + self.que_size) % self.que_capacity;\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        self.nums[rear as usize] = num;\n        self.que_size += 1;\n    }\n\n    /* \u51fa\u961f */\n    fn pop(&mut self) -> T {\n        let num = self.peek();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n        self.front = (self.front + 1) % self.que_capacity;\n        self.que_size -= 1;\n        num\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    fn peek(&self) -> T {\n        if self.is_empty() {\n            panic!(\"index out of bounds\");\n        }\n        self.nums[self.front as usize]\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4 */\n    fn to_vector(&self) -> Vec<T> {\n        let cap = self.que_capacity;\n        let mut j = self.front;\n        let mut arr = vec![T::default(); cap as usize];\n        for i in 0..self.que_size {\n            arr[i as usize] = self.nums[(j % cap) as usize];\n            j += 1;\n        }\n        arr\n    }\n}\n
        array_queue.c
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\ntypedef struct {\n    int *nums;       // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    int front;       // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    int queSize;     // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\n    int queCapacity; // \u961f\u5217\u5bb9\u91cf\n} ArrayQueue;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayQueue *newArrayQueue(int capacity) {\n    ArrayQueue *queue = (ArrayQueue *)malloc(sizeof(ArrayQueue));\n    // \u521d\u59cb\u5316\u6570\u7ec4\n    queue->queCapacity = capacity;\n    queue->nums = (int *)malloc(sizeof(int) * queue->queCapacity);\n    queue->front = queue->queSize = 0;\n    return queue;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayQueue(ArrayQueue *queue) {\n    free(queue->nums);\n    free(queue);\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity(ArrayQueue *queue) {\n    return queue->queCapacity;\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(ArrayQueue *queue) {\n    return queue->queSize;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(ArrayQueue *queue) {\n    return queue->queSize == 0;\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek(ArrayQueue *queue) {\n    assert(size(queue) != 0);\n    return queue->nums[queue->front];\n}\n\n/* \u5165\u961f */\nvoid push(ArrayQueue *queue, int num) {\n    if (size(queue) == capacity(queue)) {\n        printf(\"\u961f\u5217\u5df2\u6ee1\\r\\n\");\n        return;\n    }\n    // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n    // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n    int rear = (queue->front + queue->queSize) % queue->queCapacity;\n    // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n    queue->nums[rear] = num;\n    queue->queSize++;\n}\n\n/* \u51fa\u961f */\nint pop(ArrayQueue *queue) {\n    int num = peek(queue);\n    // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n    queue->front = (queue->front + 1) % queue->queCapacity;\n    queue->queSize--;\n    return num;\n}\n\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nint *toArray(ArrayQueue *queue, int *queSize) {\n    *queSize = queue->queSize;\n    int *res = (int *)calloc(queue->queSize, sizeof(int));\n    int j = queue->front;\n    for (int i = 0; i < queue->queSize; i++) {\n        res[i] = queue->nums[j % queue->queCapacity];\n        j++;\n    }\n    return res;\n}\n
        array_queue.kt
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue(capacity: Int) {\n    private val nums: IntArray = IntArray(capacity) // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    private var front: Int = 0 // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    private var queSize: Int = 0 // \u961f\u5217\u957f\u5ea6\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n    fun capacity(): Int {\n        return nums.size\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    fun size(): Int {\n        return queSize\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    fun isEmpty(): Boolean {\n        return queSize == 0\n    }\n\n    /* \u5165\u961f */\n    fun push(num: Int) {\n        if (queSize == capacity()) {\n            println(\"\u961f\u5217\u5df2\u6ee1\")\n            return\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n        val rear = (front + queSize) % capacity()\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        nums[rear] = num\n        queSize++\n    }\n\n    /* \u51fa\u961f */\n    fun pop(): Int {\n        val num = peek()\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n        front = (front + 1) % capacity()\n        queSize--\n        return num\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    fun peek(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return nums[front]\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4 */\n    fun toArray(): IntArray {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        val res = IntArray(queSize)\n        var i = 0\n        var j = front\n        while (i < queSize) {\n            res[i] = nums[j % capacity()]\n            i++\n            j++\n        }\n        return res\n    }\n}\n
        array_queue.rb
        ### \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 ###\nclass ArrayQueue\n  ### \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 ###\n  attr_reader :size\n\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize(size)\n    @nums = Array.new(size, 0) # \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    @front = 0 # \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    @size = 0 # \u961f\u5217\u957f\u5ea6\n  end\n\n  ### \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf ###\n  def capacity\n    @nums.length\n  end\n\n  ### \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a ###\n  def is_empty?\n    size.zero?\n  end\n\n  ### \u5165\u961f ###\n  def push(num)\n    raise IndexError, '\u961f\u5217\u5df2\u6ee1' if size == capacity\n\n    # \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n    # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n    rear = (@front + size) % capacity\n    # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n    @nums[rear] = num\n    @size += 1\n  end\n\n  ### \u51fa\u961f ###\n  def pop\n    num = peek\n    # \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n    @front = (@front + 1) % capacity\n    @size -= 1\n    num\n  end\n\n  ### \u8bbf\u95ee\u961f\u9996\u5143\u7d20 ###\n  def peek\n    raise IndexError, '\u961f\u5217\u4e3a\u7a7a' if is_empty?\n\n    @nums[@front]\n  end\n\n  ### \u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370 ###\n  def to_array\n    res = Array.new(size, 0)\n    j = @front\n\n    for i in 0...size\n      res[i] = @nums[j % capacity]\n      j += 1\n    end\n\n    res\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u4ee5\u4e0a\u5b9e\u73b0\u7684\u961f\u5217\u4ecd\u7136\u5177\u6709\u5c40\u9650\u6027\uff1a\u5176\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002\u7136\u800c\uff0c\u8fd9\u4e2a\u95ee\u9898\u4e0d\u96be\u89e3\u51b3\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u66ff\u6362\u4e3a\u52a8\u6001\u6570\u7ec4\uff0c\u4ece\u800c\u5f15\u5165\u6269\u5bb9\u673a\u5236\u3002\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u5c1d\u8bd5\u81ea\u884c\u5b9e\u73b0\u3002

        \u4e24\u79cd\u5b9e\u73b0\u7684\u5bf9\u6bd4\u7ed3\u8bba\u4e0e\u6808\u4e00\u81f4\uff0c\u5728\u6b64\u4e0d\u518d\u8d58\u8ff0\u3002

        "},{"location":"chapter_stack_and_queue/queue/#523","title":"5.2.3 \u00a0 \u961f\u5217\u5178\u578b\u5e94\u7528","text":"
        • \u6dd8\u5b9d\u8ba2\u5355\u3002\u8d2d\u7269\u8005\u4e0b\u5355\u540e\uff0c\u8ba2\u5355\u5c06\u52a0\u5165\u961f\u5217\u4e2d\uff0c\u7cfb\u7edf\u968f\u540e\u4f1a\u6839\u636e\u987a\u5e8f\u5904\u7406\u961f\u5217\u4e2d\u7684\u8ba2\u5355\u3002\u5728\u53cc\u5341\u4e00\u671f\u95f4\uff0c\u77ed\u65f6\u95f4\u5185\u4f1a\u4ea7\u751f\u6d77\u91cf\u8ba2\u5355\uff0c\u9ad8\u5e76\u53d1\u6210\u4e3a\u5de5\u7a0b\u5e08\u4eec\u9700\u8981\u91cd\u70b9\u653b\u514b\u7684\u95ee\u9898\u3002
        • \u5404\u7c7b\u5f85\u529e\u4e8b\u9879\u3002\u4efb\u4f55\u9700\u8981\u5b9e\u73b0\u201c\u5148\u6765\u540e\u5230\u201d\u529f\u80fd\u7684\u573a\u666f\uff0c\u4f8b\u5982\u6253\u5370\u673a\u7684\u4efb\u52a1\u961f\u5217\u3001\u9910\u5385\u7684\u51fa\u9910\u961f\u5217\u7b49\uff0c\u961f\u5217\u5728\u8fd9\u4e9b\u573a\u666f\u4e2d\u53ef\u4ee5\u6709\u6548\u5730\u7ef4\u62a4\u5904\u7406\u987a\u5e8f\u3002
        "},{"location":"chapter_stack_and_queue/stack/","title":"5.1 \u00a0 \u6808","text":"

        \u6808\uff08stack\uff09\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u540e\u51fa\u903b\u8f91\u7684\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002

        \u6211\u4eec\u53ef\u4ee5\u5c06\u6808\u7c7b\u6bd4\u4e3a\u684c\u9762\u4e0a\u7684\u4e00\u645e\u76d8\u5b50\uff0c\u89c4\u5b9a\u6bcf\u6b21\u53ea\u80fd\u79fb\u52a8\u4e00\u4e2a\u76d8\u5b50\uff0c\u90a3\u4e48\u60f3\u53d6\u51fa\u5e95\u90e8\u7684\u76d8\u5b50\uff0c\u5219\u9700\u8981\u5148\u5c06\u4e0a\u9762\u7684\u76d8\u5b50\u4f9d\u6b21\u79fb\u8d70\u3002\u6211\u4eec\u5c06\u76d8\u5b50\u66ff\u6362\u4e3a\u5404\u79cd\u7c7b\u578b\u7684\u5143\u7d20\uff08\u5982\u6574\u6570\u3001\u5b57\u7b26\u3001\u5bf9\u8c61\u7b49\uff09\uff0c\u5c31\u5f97\u5230\u4e86\u6808\u8fd9\u79cd\u6570\u636e\u7ed3\u6784\u3002

        \u5982\u56fe 5-1 \u6240\u793a\uff0c\u6211\u4eec\u628a\u5806\u53e0\u5143\u7d20\u7684\u9876\u90e8\u79f0\u4e3a\u201c\u6808\u9876\u201d\uff0c\u5e95\u90e8\u79f0\u4e3a\u201c\u6808\u5e95\u201d\u3002\u5c06\u628a\u5143\u7d20\u6dfb\u52a0\u5230\u6808\u9876\u7684\u64cd\u4f5c\u53eb\u4f5c\u201c\u5165\u6808\u201d\uff0c\u5220\u9664\u6808\u9876\u5143\u7d20\u7684\u64cd\u4f5c\u53eb\u4f5c\u201c\u51fa\u6808\u201d\u3002

        \u56fe 5-1 \u00a0 \u6808\u7684\u5148\u5165\u540e\u51fa\u89c4\u5219

        "},{"location":"chapter_stack_and_queue/stack/#511","title":"5.1.1 \u00a0 \u6808\u7684\u5e38\u7528\u64cd\u4f5c","text":"

        \u6808\u7684\u5e38\u7528\u64cd\u4f5c\u5982\u8868 5-1 \u6240\u793a\uff0c\u5177\u4f53\u7684\u65b9\u6cd5\u540d\u9700\u8981\u6839\u636e\u6240\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002\u5728\u6b64\uff0c\u6211\u4eec\u4ee5\u5e38\u89c1\u7684 push()\u3001pop()\u3001peek() \u547d\u540d\u4e3a\u4f8b\u3002

        \u8868 5-1 \u00a0 \u6808\u7684\u64cd\u4f5c\u6548\u7387

        \u65b9\u6cd5 \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push() \u5143\u7d20\u5165\u6808\uff08\u6dfb\u52a0\u81f3\u6808\u9876\uff09 \\(O(1)\\) pop() \u6808\u9876\u5143\u7d20\u51fa\u6808 \\(O(1)\\) peek() \u8bbf\u95ee\u6808\u9876\u5143\u7d20 \\(O(1)\\)

        \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u5185\u7f6e\u7684\u6808\u7c7b\u3002\u7136\u800c\uff0c\u67d0\u4e9b\u8bed\u8a00\u53ef\u80fd\u6ca1\u6709\u4e13\u95e8\u63d0\u4f9b\u6808\u7c7b\uff0c\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u5c06\u8be5\u8bed\u8a00\u7684\u201c\u6570\u7ec4\u201d\u6216\u201c\u94fe\u8868\u201d\u5f53\u4f5c\u6808\u6765\u4f7f\u7528\uff0c\u5e76\u5728\u7a0b\u5e8f\u903b\u8f91\u4e0a\u5ffd\u7565\u4e0e\u6808\u65e0\u5173\u7684\u64cd\u4f5c\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby stack.py
        # \u521d\u59cb\u5316\u6808\n# Python \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a list \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nstack: list[int] = []\n\n# \u5143\u7d20\u5165\u6808\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n# \u8bbf\u95ee\u6808\u9876\u5143\u7d20\npeek: int = stack[-1]\n\n# \u5143\u7d20\u51fa\u6808\npop: int = stack.pop()\n\n# \u83b7\u53d6\u6808\u7684\u957f\u5ea6\nsize: int = len(stack)\n\n# \u5224\u65ad\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = len(stack) == 0\n
        stack.cpp
        /* \u521d\u59cb\u5316\u6808 */\nstack<int> stack;\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint top = stack.top();\n\n/* \u5143\u7d20\u51fa\u6808 */\nstack.pop(); // \u65e0\u8fd4\u56de\u503c\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.size();\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool empty = stack.empty();\n
        stack.java
        /* \u521d\u59cb\u5316\u6808 */\nStack<Integer> stack = new Stack<>();\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.peek();\n\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.pop();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.size();\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = stack.isEmpty();\n
        stack.cs
        /* \u521d\u59cb\u5316\u6808 */\nStack<int> stack = new();\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.Push(1);\nstack.Push(3);\nstack.Push(2);\nstack.Push(5);\nstack.Push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.Peek();\n\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.Pop();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.Count;\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = stack.Count == 0;\n
        stack_test.go
        /* \u521d\u59cb\u5316\u6808 */\n// \u5728 Go \u4e2d\uff0c\u63a8\u8350\u5c06 Slice \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nvar stack []int\n\n/* \u5143\u7d20\u5165\u6808 */\nstack = append(stack, 1)\nstack = append(stack, 3)\nstack = append(stack, 2)\nstack = append(stack, 5)\nstack = append(stack, 4)\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npeek := stack[len(stack)-1]\n\n/* \u5143\u7d20\u51fa\u6808 */\npop := stack[len(stack)-1]\nstack = stack[:len(stack)-1]\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nsize := len(stack)\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nisEmpty := len(stack) == 0\n
        stack.swift
        /* \u521d\u59cb\u5316\u6808 */\n// Swift \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nvar stack: [Int] = []\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nlet peek = stack.last!\n\n/* \u5143\u7d20\u51fa\u6808 */\nlet pop = stack.removeLast()\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nlet size = stack.count\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = stack.isEmpty\n
        stack.js
        /* \u521d\u59cb\u5316\u6808 */\n// JavaScript \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nconst stack = [];\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nconst peek = stack[stack.length-1];\n\n/* \u5143\u7d20\u51fa\u6808 */\nconst pop = stack.pop();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nconst size = stack.length;\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nconst is_empty = stack.length === 0;\n
        stack.ts
        /* \u521d\u59cb\u5316\u6808 */\n// TypeScript \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nconst stack: number[] = [];\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nconst peek = stack[stack.length - 1];\n\n/* \u5143\u7d20\u51fa\u6808 */\nconst pop = stack.pop();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nconst size = stack.length;\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nconst is_empty = stack.length === 0;\n
        stack.dart
        /* \u521d\u59cb\u5316\u6808 */\n// Dart \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a List \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nList<int> stack = [];\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.add(1);\nstack.add(3);\nstack.add(2);\nstack.add(5);\nstack.add(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.last;\n\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.removeLast();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.length;\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = stack.isEmpty;\n
        stack.rs
        /* \u521d\u59cb\u5316\u6808 */\n// \u628a Vec \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nlet mut stack: Vec<i32> = Vec::new();\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nlet top = stack.last().unwrap();\n\n/* \u5143\u7d20\u51fa\u6808 */\nlet pop = stack.pop().unwrap();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nlet size = stack.len();\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = stack.is_empty();\n
        stack.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e\u6808\n
        stack.kt
        /* \u521d\u59cb\u5316\u6808 */\nval stack = Stack<Int>()\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1)\nstack.push(3)\nstack.push(2)\nstack.push(5)\nstack.push(4)\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nval peek = stack.peek()\n\n/* \u5143\u7d20\u51fa\u6808 */\nval pop = stack.pop()\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nval size = stack.size\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nval isEmpty = stack.isEmpty()\n
        stack.rb
        # \u521d\u59cb\u5316\u6808\n# Ruby \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nstack = []\n\n# \u5143\u7d20\u5165\u6808\nstack << 1\nstack << 3\nstack << 2\nstack << 5\nstack << 4\n\n# \u8bbf\u95ee\u6808\u9876\u5143\u7d20\npeek = stack.last\n\n# \u5143\u7d20\u51fa\u6808\npop = stack.pop\n\n# \u83b7\u53d6\u6808\u7684\u957f\u5ea6\nsize = stack.length\n\n# \u5224\u65ad\u662f\u5426\u4e3a\u7a7a\nis_empty = stack.empty?\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_stack_and_queue/stack/#512","title":"5.1.2 \u00a0 \u6808\u7684\u5b9e\u73b0","text":"

        \u4e3a\u4e86\u6df1\u5165\u4e86\u89e3\u6808\u7684\u8fd0\u884c\u673a\u5236\uff0c\u6211\u4eec\u6765\u5c1d\u8bd5\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2a\u6808\u7c7b\u3002

        \u6808\u9075\u5faa\u5148\u5165\u540e\u51fa\u7684\u539f\u5219\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u80fd\u5728\u6808\u9876\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\u3002\u7136\u800c\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u90fd\u53ef\u4ee5\u5728\u4efb\u610f\u4f4d\u7f6e\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\uff0c\u56e0\u6b64\u6808\u53ef\u4ee5\u89c6\u4e3a\u4e00\u79cd\u53d7\u9650\u5236\u7684\u6570\u7ec4\u6216\u94fe\u8868\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u201c\u5c4f\u853d\u201d\u6570\u7ec4\u6216\u94fe\u8868\u7684\u90e8\u5206\u65e0\u5173\u64cd\u4f5c\uff0c\u4f7f\u5176\u5bf9\u5916\u8868\u73b0\u7684\u903b\u8f91\u7b26\u5408\u6808\u7684\u7279\u6027\u3002

        "},{"location":"chapter_stack_and_queue/stack/#1","title":"1. \u00a0 \u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0","text":"

        \u4f7f\u7528\u94fe\u8868\u5b9e\u73b0\u6808\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u94fe\u8868\u7684\u5934\u8282\u70b9\u89c6\u4e3a\u6808\u9876\uff0c\u5c3e\u8282\u70b9\u89c6\u4e3a\u6808\u5e95\u3002

        \u5982\u56fe 5-2 \u6240\u793a\uff0c\u5bf9\u4e8e\u5165\u6808\u64cd\u4f5c\uff0c\u6211\u4eec\u53ea\u9700\u5c06\u5143\u7d20\u63d2\u5165\u94fe\u8868\u5934\u90e8\uff0c\u8fd9\u79cd\u8282\u70b9\u63d2\u5165\u65b9\u6cd5\u88ab\u79f0\u4e3a\u201c\u5934\u63d2\u6cd5\u201d\u3002\u800c\u5bf9\u4e8e\u51fa\u6808\u64cd\u4f5c\uff0c\u53ea\u9700\u5c06\u5934\u8282\u70b9\u4ece\u94fe\u8868\u4e2d\u5220\u9664\u5373\u53ef\u3002

        LinkedListStackpush()pop()

        \u56fe 5-2 \u00a0 \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u6808\u7684\u5165\u6808\u51fa\u6808\u64cd\u4f5c

        \u4ee5\u4e0b\u662f\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u6808\u7684\u793a\u4f8b\u4ee3\u7801\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linkedlist_stack.py
        class LinkedListStack:\n    \"\"\"\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\"\"\"\n\n    def __init__(self):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        self._peek: ListNode | None = None\n        self._size: int = 0\n\n    def size(self) -> int:\n        \"\"\"\u83b7\u53d6\u6808\u7684\u957f\u5ea6\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\"\"\"\n        return self._size == 0\n\n    def push(self, val: int):\n        \"\"\"\u5165\u6808\"\"\"\n        node = ListNode(val)\n        node.next = self._peek\n        self._peek = node\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"\u51fa\u6808\"\"\"\n        num = self.peek()\n        self._peek = self._peek.next\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"\u8bbf\u95ee\u6808\u9876\u5143\u7d20\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u6808\u4e3a\u7a7a\")\n        return self._peek.val\n\n    def to_list(self) -> list[int]:\n        \"\"\"\u8f6c\u5316\u4e3a\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n        arr = []\n        node = self._peek\n        while node:\n            arr.append(node.val)\n            node = node.next\n        arr.reverse()\n        return arr\n
        linkedlist_stack.cpp
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n  private:\n    ListNode *stackTop; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n    int stkSize;        // \u6808\u7684\u957f\u5ea6\n\n  public:\n    LinkedListStack() {\n        stackTop = nullptr;\n        stkSize = 0;\n    }\n\n    ~LinkedListStack() {\n        // \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\n        freeMemoryLinkedList(stackTop);\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    int size() {\n        return stkSize;\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    bool isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u5165\u6808 */\n    void push(int num) {\n        ListNode *node = new ListNode(num);\n        node->next = stackTop;\n        stackTop = node;\n        stkSize++;\n    }\n\n    /* \u51fa\u6808 */\n    int pop() {\n        int num = top();\n        ListNode *tmp = stackTop;\n        stackTop = stackTop->next;\n        // \u91ca\u653e\u5185\u5b58\n        delete tmp;\n        stkSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    int top() {\n        if (isEmpty())\n            throw out_of_range(\"\u6808\u4e3a\u7a7a\");\n        return stackTop->val;\n    }\n\n    /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    vector<int> toVector() {\n        ListNode *node = stackTop;\n        vector<int> res(size());\n        for (int i = res.size() - 1; i >= 0; i--) {\n            res[i] = node->val;\n            node = node->next;\n        }\n        return res;\n    }\n};\n
        linkedlist_stack.java
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n    private ListNode stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n    private int stkSize = 0; // \u6808\u7684\u957f\u5ea6\n\n    public LinkedListStack() {\n        stackPeek = null;\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    public int size() {\n        return stkSize;\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u5165\u6808 */\n    public void push(int num) {\n        ListNode node = new ListNode(num);\n        node.next = stackPeek;\n        stackPeek = node;\n        stkSize++;\n    }\n\n    /* \u51fa\u6808 */\n    public int pop() {\n        int num = peek();\n        stackPeek = stackPeek.next;\n        stkSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return stackPeek.val;\n    }\n\n    /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    public int[] toArray() {\n        ListNode node = stackPeek;\n        int[] res = new int[size()];\n        for (int i = res.length - 1; i >= 0; i--) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_stack.cs
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n    ListNode? stackPeek;  // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n    int stkSize = 0;   // \u6808\u7684\u957f\u5ea6\n\n    public LinkedListStack() {\n        stackPeek = null;\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    public int Size() {\n        return stkSize;\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    public bool IsEmpty() {\n        return Size() == 0;\n    }\n\n    /* \u5165\u6808 */\n    public void Push(int num) {\n        ListNode node = new(num) {\n            next = stackPeek\n        };\n        stackPeek = node;\n        stkSize++;\n    }\n\n    /* \u51fa\u6808 */\n    public int Pop() {\n        int num = Peek();\n        stackPeek = stackPeek!.next;\n        stkSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    public int Peek() {\n        if (IsEmpty())\n            throw new Exception();\n        return stackPeek!.val;\n    }\n\n    /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    public int[] ToArray() {\n        if (stackPeek == null)\n            return [];\n\n        ListNode? node = stackPeek;\n        int[] res = new int[Size()];\n        for (int i = res.Length - 1; i >= 0; i--) {\n            res[i] = node!.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_stack.go
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\ntype linkedListStack struct {\n    // \u4f7f\u7528\u5185\u7f6e\u5305 list \u6765\u5b9e\u73b0\u6808\n    data *list.List\n}\n\n/* \u521d\u59cb\u5316\u6808 */\nfunc newLinkedListStack() *linkedListStack {\n    return &linkedListStack{\n        data: list.New(),\n    }\n}\n\n/* \u5165\u6808 */\nfunc (s *linkedListStack) push(value int) {\n    s.data.PushBack(value)\n}\n\n/* \u51fa\u6808 */\nfunc (s *linkedListStack) pop() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Back()\n    s.data.Remove(e)\n    return e.Value\n}\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nfunc (s *linkedListStack) peek() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Back()\n    return e.Value\n}\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nfunc (s *linkedListStack) size() int {\n    return s.data.Len()\n}\n\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListStack) isEmpty() bool {\n    return s.data.Len() == 0\n}\n\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListStack) toList() *list.List {\n    return s.data\n}\n
        linkedlist_stack.swift
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n    private var _peek: ListNode? // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n    private var _size: Int // \u6808\u7684\u957f\u5ea6\n\n    init() {\n        _size = 0\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    func size() -> Int {\n        _size\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* \u5165\u6808 */\n    func push(num: Int) {\n        let node = ListNode(x: num)\n        node.next = _peek\n        _peek = node\n        _size += 1\n    }\n\n    /* \u51fa\u6808 */\n    @discardableResult\n    func pop() -> Int {\n        let num = peek()\n        _peek = _peek?.next\n        _size -= 1\n        return num\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    func peek() -> Int {\n        if isEmpty() {\n            fatalError(\"\u6808\u4e3a\u7a7a\")\n        }\n        return _peek!.val\n    }\n\n    /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    func toArray() -> [Int] {\n        var node = _peek\n        var res = Array(repeating: 0, count: size())\n        for i in res.indices.reversed() {\n            res[i] = node!.val\n            node = node?.next\n        }\n        return res\n    }\n}\n
        linkedlist_stack.js
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n    #stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n    #stkSize = 0; // \u6808\u7684\u957f\u5ea6\n\n    constructor() {\n        this.#stackPeek = null;\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    get size() {\n        return this.#stkSize;\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    isEmpty() {\n        return this.size === 0;\n    }\n\n    /* \u5165\u6808 */\n    push(num) {\n        const node = new ListNode(num);\n        node.next = this.#stackPeek;\n        this.#stackPeek = node;\n        this.#stkSize++;\n    }\n\n    /* \u51fa\u6808 */\n    pop() {\n        const num = this.peek();\n        this.#stackPeek = this.#stackPeek.next;\n        this.#stkSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    peek() {\n        if (!this.#stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\n        return this.#stackPeek.val;\n    }\n\n    /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    toArray() {\n        let node = this.#stackPeek;\n        const res = new Array(this.size);\n        for (let i = res.length - 1; i >= 0; i--) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_stack.ts
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n    private stackPeek: ListNode | null; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n    private stkSize: number = 0; // \u6808\u7684\u957f\u5ea6\n\n    constructor() {\n        this.stackPeek = null;\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    get size(): number {\n        return this.stkSize;\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    isEmpty(): boolean {\n        return this.size === 0;\n    }\n\n    /* \u5165\u6808 */\n    push(num: number): void {\n        const node = new ListNode(num);\n        node.next = this.stackPeek;\n        this.stackPeek = node;\n        this.stkSize++;\n    }\n\n    /* \u51fa\u6808 */\n    pop(): number {\n        const num = this.peek();\n        if (!this.stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\n        this.stackPeek = this.stackPeek.next;\n        this.stkSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    peek(): number {\n        if (!this.stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\n        return this.stackPeek.val;\n    }\n\n    /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    toArray(): number[] {\n        let node = this.stackPeek;\n        const res = new Array<number>(this.size);\n        for (let i = res.length - 1; i >= 0; i--) {\n            res[i] = node!.val;\n            node = node!.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_stack.dart
        /* \u57fa\u4e8e\u94fe\u8868\u7c7b\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n  ListNode? _stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n  int _stkSize = 0; // \u6808\u7684\u957f\u5ea6\n\n  LinkedListStack() {\n    _stackPeek = null;\n  }\n\n  /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n  int size() {\n    return _stkSize;\n  }\n\n  /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n  bool isEmpty() {\n    return _stkSize == 0;\n  }\n\n  /* \u5165\u6808 */\n  void push(int _num) {\n    final ListNode node = ListNode(_num);\n    node.next = _stackPeek;\n    _stackPeek = node;\n    _stkSize++;\n  }\n\n  /* \u51fa\u6808 */\n  int pop() {\n    final int _num = peek();\n    _stackPeek = _stackPeek!.next;\n    _stkSize--;\n    return _num;\n  }\n\n  /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n  int peek() {\n    if (_stackPeek == null) {\n      throw Exception(\"\u6808\u4e3a\u7a7a\");\n    }\n    return _stackPeek!.val;\n  }\n\n  /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a List \u5e76\u8fd4\u56de */\n  List<int> toList() {\n    ListNode? node = _stackPeek;\n    List<int> list = [];\n    while (node != null) {\n      list.add(node.val);\n      node = node.next;\n    }\n    list = list.reversed.toList();\n    return list;\n  }\n}\n
        linkedlist_stack.rs
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\n#[allow(dead_code)]\npub struct LinkedListStack<T> {\n    stack_peek: Option<Rc<RefCell<ListNode<T>>>>, // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n    stk_size: usize,                              // \u6808\u7684\u957f\u5ea6\n}\n\nimpl<T: Copy> LinkedListStack<T> {\n    pub fn new() -> Self {\n        Self {\n            stack_peek: None,\n            stk_size: 0,\n        }\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    pub fn size(&self) -> usize {\n        return self.stk_size;\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    pub fn is_empty(&self) -> bool {\n        return self.size() == 0;\n    }\n\n    /* \u5165\u6808 */\n    pub fn push(&mut self, num: T) {\n        let node = ListNode::new(num);\n        node.borrow_mut().next = self.stack_peek.take();\n        self.stack_peek = Some(node);\n        self.stk_size += 1;\n    }\n\n    /* \u51fa\u6808 */\n    pub fn pop(&mut self) -> Option<T> {\n        self.stack_peek.take().map(|old_head| {\n            self.stack_peek = old_head.borrow_mut().next.take();\n            self.stk_size -= 1;\n\n            old_head.borrow().val\n        })\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n        self.stack_peek.as_ref()\n    }\n\n    /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    pub fn to_array(&self) -> Vec<T> {\n        fn _to_array<T: Sized + Copy>(head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n            if let Some(node) = head {\n                let mut nums = _to_array(node.borrow().next.as_ref());\n                nums.push(node.borrow().val);\n                return nums;\n            }\n            return Vec::new();\n        }\n\n        _to_array(self.peek())\n    }\n}\n
        linkedlist_stack.c
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\ntypedef struct {\n    ListNode *top; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n    int size;      // \u6808\u7684\u957f\u5ea6\n} LinkedListStack;\n\n/* \u6784\u9020\u51fd\u6570 */\nLinkedListStack *newLinkedListStack() {\n    LinkedListStack *s = malloc(sizeof(LinkedListStack));\n    s->top = NULL;\n    s->size = 0;\n    return s;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListStack(LinkedListStack *s) {\n    while (s->top) {\n        ListNode *n = s->top->next;\n        free(s->top);\n        s->top = n;\n    }\n    free(s);\n}\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size(LinkedListStack *s) {\n    return s->size;\n}\n\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty(LinkedListStack *s) {\n    return size(s) == 0;\n}\n\n/* \u5165\u6808 */\nvoid push(LinkedListStack *s, int num) {\n    ListNode *node = (ListNode *)malloc(sizeof(ListNode));\n    node->next = s->top; // \u66f4\u65b0\u65b0\u52a0\u8282\u70b9\u6307\u9488\u57df\n    node->val = num;     // \u66f4\u65b0\u65b0\u52a0\u8282\u70b9\u6570\u636e\u57df\n    s->top = node;       // \u66f4\u65b0\u6808\u9876\n    s->size++;           // \u66f4\u65b0\u6808\u5927\u5c0f\n}\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek(LinkedListStack *s) {\n    if (s->size == 0) {\n        printf(\"\u6808\u4e3a\u7a7a\\n\");\n        return INT_MAX;\n    }\n    return s->top->val;\n}\n\n/* \u51fa\u6808 */\nint pop(LinkedListStack *s) {\n    int val = peek(s);\n    ListNode *tmp = s->top;\n    s->top = s->top->next;\n    // \u91ca\u653e\u5185\u5b58\n    free(tmp);\n    s->size--;\n    return val;\n}\n
        linkedlist_stack.kt
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack(\n    private var stackPeek: ListNode? = null, // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n    private var stkSize: Int = 0 // \u6808\u7684\u957f\u5ea6\n) {\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    fun size(): Int {\n        return stkSize\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    fun isEmpty(): Boolean {\n        return size() == 0\n    }\n\n    /* \u5165\u6808 */\n    fun push(num: Int) {\n        val node = ListNode(num)\n        node.next = stackPeek\n        stackPeek = node\n        stkSize++\n    }\n\n    /* \u51fa\u6808 */\n    fun pop(): Int? {\n        val num = peek()\n        stackPeek = stackPeek?.next\n        stkSize--\n        return num\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    fun peek(): Int? {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return stackPeek?._val\n    }\n\n    /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    fun toArray(): IntArray {\n        var node = stackPeek\n        val res = IntArray(size())\n        for (i in res.size - 1 downTo 0) {\n            res[i] = node?._val!!\n            node = node.next\n        }\n        return res\n    }\n}\n
        linkedlist_stack.rb
        ### \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 ###\nclass LinkedListStack\n  attr_reader :size\n\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize\n    @size = 0\n  end\n\n  ### \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a ###\n  def is_empty?\n    @peek.nil?\n  end\n\n  ### \u5165\u6808 ###\n  def push(val)\n    node = ListNode.new(val)\n    node.next = @peek\n    @peek = node\n    @size += 1\n  end\n\n  ### \u51fa\u6808 ###\n  def pop\n    num = peek\n    @peek = @peek.next\n    @size -= 1\n    num\n  end\n\n  ### \u8bbf\u95ee\u6808\u9876\u5143\u7d20 ###\n  def peek\n    raise IndexError, '\u6808\u4e3a\u7a7a' if is_empty?\n\n    @peek.val\n  end\n\n  ### \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u53cd\u56de ###\n  def to_array\n    arr = []\n    node = @peek\n    while node\n      arr << node.val\n      node = node.next\n    end\n    arr.reverse\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_stack_and_queue/stack/#2","title":"2. \u00a0 \u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"

        \u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u6808\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u7684\u5c3e\u90e8\u4f5c\u4e3a\u6808\u9876\u3002\u5982\u56fe 5-3 \u6240\u793a\uff0c\u5165\u6808\u4e0e\u51fa\u6808\u64cd\u4f5c\u5206\u522b\u5bf9\u5e94\u5728\u6570\u7ec4\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u4e0e\u5220\u9664\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(1)\\) \u3002

        ArrayStackpush()pop()

        \u56fe 5-3 \u00a0 \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u6808\u7684\u5165\u6808\u51fa\u6808\u64cd\u4f5c

        \u7531\u4e8e\u5165\u6808\u7684\u5143\u7d20\u53ef\u80fd\u4f1a\u6e90\u6e90\u4e0d\u65ad\u5730\u589e\u52a0\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\uff0c\u8fd9\u6837\u5c31\u65e0\u987b\u81ea\u884c\u5904\u7406\u6570\u7ec4\u6269\u5bb9\u95ee\u9898\u3002\u4ee5\u4e0b\u4e3a\u793a\u4f8b\u4ee3\u7801\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_stack.py
        class ArrayStack:\n    \"\"\"\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\"\"\"\n\n    def __init__(self):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        self._stack: list[int] = []\n\n    def size(self) -> int:\n        \"\"\"\u83b7\u53d6\u6808\u7684\u957f\u5ea6\"\"\"\n        return len(self._stack)\n\n    def is_empty(self) -> bool:\n        \"\"\"\u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\"\"\"\n        return self.size() == 0\n\n    def push(self, item: int):\n        \"\"\"\u5165\u6808\"\"\"\n        self._stack.append(item)\n\n    def pop(self) -> int:\n        \"\"\"\u51fa\u6808\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u6808\u4e3a\u7a7a\")\n        return self._stack.pop()\n\n    def peek(self) -> int:\n        \"\"\"\u8bbf\u95ee\u6808\u9876\u5143\u7d20\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u6808\u4e3a\u7a7a\")\n        return self._stack[-1]\n\n    def to_list(self) -> list[int]:\n        \"\"\"\u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n        return self._stack\n
        array_stack.cpp
        /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n  private:\n    vector<int> stack;\n\n  public:\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    int size() {\n        return stack.size();\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    bool isEmpty() {\n        return stack.size() == 0;\n    }\n\n    /* \u5165\u6808 */\n    void push(int num) {\n        stack.push_back(num);\n    }\n\n    /* \u51fa\u6808 */\n    int pop() {\n        int num = top();\n        stack.pop_back();\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    int top() {\n        if (isEmpty())\n            throw out_of_range(\"\u6808\u4e3a\u7a7a\");\n        return stack.back();\n    }\n\n    /* \u8fd4\u56de Vector */\n    vector<int> toVector() {\n        return stack;\n    }\n};\n
        array_stack.java
        /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n    private ArrayList<Integer> stack;\n\n    public ArrayStack() {\n        // \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\n        stack = new ArrayList<>();\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    public int size() {\n        return stack.size();\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u5165\u6808 */\n    public void push(int num) {\n        stack.add(num);\n    }\n\n    /* \u51fa\u6808 */\n    public int pop() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return stack.remove(size() - 1);\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return stack.get(size() - 1);\n    }\n\n    /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    public Object[] toArray() {\n        return stack.toArray();\n    }\n}\n
        array_stack.cs
        /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n    List<int> stack;\n    public ArrayStack() {\n        // \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\n        stack = [];\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    public int Size() {\n        return stack.Count;\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    public bool IsEmpty() {\n        return Size() == 0;\n    }\n\n    /* \u5165\u6808 */\n    public void Push(int num) {\n        stack.Add(num);\n    }\n\n    /* \u51fa\u6808 */\n    public int Pop() {\n        if (IsEmpty())\n            throw new Exception();\n        var val = Peek();\n        stack.RemoveAt(Size() - 1);\n        return val;\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    public int Peek() {\n        if (IsEmpty())\n            throw new Exception();\n        return stack[Size() - 1];\n    }\n\n    /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    public int[] ToArray() {\n        return [.. stack];\n    }\n}\n
        array_stack.go
        /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\ntype arrayStack struct {\n    data []int // \u6570\u636e\n}\n\n/* \u521d\u59cb\u5316\u6808 */\nfunc newArrayStack() *arrayStack {\n    return &arrayStack{\n        // \u8bbe\u7f6e\u6808\u7684\u957f\u5ea6\u4e3a 0\uff0c\u5bb9\u91cf\u4e3a 16\n        data: make([]int, 0, 16),\n    }\n}\n\n/* \u6808\u7684\u957f\u5ea6 */\nfunc (s *arrayStack) size() int {\n    return len(s.data)\n}\n\n/* \u6808\u662f\u5426\u4e3a\u7a7a */\nfunc (s *arrayStack) isEmpty() bool {\n    return s.size() == 0\n}\n\n/* \u5165\u6808 */\nfunc (s *arrayStack) push(v int) {\n    // \u5207\u7247\u4f1a\u81ea\u52a8\u6269\u5bb9\n    s.data = append(s.data, v)\n}\n\n/* \u51fa\u6808 */\nfunc (s *arrayStack) pop() any {\n    val := s.peek()\n    s.data = s.data[:len(s.data)-1]\n    return val\n}\n\n/* \u83b7\u53d6\u6808\u9876\u5143\u7d20 */\nfunc (s *arrayStack) peek() any {\n    if s.isEmpty() {\n        return nil\n    }\n    val := s.data[len(s.data)-1]\n    return val\n}\n\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (s *arrayStack) toSlice() []int {\n    return s.data\n}\n
        array_stack.swift
        /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n    private var stack: [Int]\n\n    init() {\n        // \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\n        stack = []\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    func size() -> Int {\n        stack.count\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    func isEmpty() -> Bool {\n        stack.isEmpty\n    }\n\n    /* \u5165\u6808 */\n    func push(num: Int) {\n        stack.append(num)\n    }\n\n    /* \u51fa\u6808 */\n    @discardableResult\n    func pop() -> Int {\n        if isEmpty() {\n            fatalError(\"\u6808\u4e3a\u7a7a\")\n        }\n        return stack.removeLast()\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    func peek() -> Int {\n        if isEmpty() {\n            fatalError(\"\u6808\u4e3a\u7a7a\")\n        }\n        return stack.last!\n    }\n\n    /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    func toArray() -> [Int] {\n        stack\n    }\n}\n
        array_stack.js
        /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n    #stack;\n    constructor() {\n        this.#stack = [];\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    get size() {\n        return this.#stack.length;\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    isEmpty() {\n        return this.#stack.length === 0;\n    }\n\n    /* \u5165\u6808 */\n    push(num) {\n        this.#stack.push(num);\n    }\n\n    /* \u51fa\u6808 */\n    pop() {\n        if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n        return this.#stack.pop();\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    top() {\n        if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n        return this.#stack[this.#stack.length - 1];\n    }\n\n    /* \u8fd4\u56de Array */\n    toArray() {\n        return this.#stack;\n    }\n}\n
        array_stack.ts
        /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n    private stack: number[];\n    constructor() {\n        this.stack = [];\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    get size(): number {\n        return this.stack.length;\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    isEmpty(): boolean {\n        return this.stack.length === 0;\n    }\n\n    /* \u5165\u6808 */\n    push(num: number): void {\n        this.stack.push(num);\n    }\n\n    /* \u51fa\u6808 */\n    pop(): number | undefined {\n        if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n        return this.stack.pop();\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    top(): number | undefined {\n        if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n        return this.stack[this.stack.length - 1];\n    }\n\n    /* \u8fd4\u56de Array */\n    toArray() {\n        return this.stack;\n    }\n}\n
        array_stack.dart
        /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n  late List<int> _stack;\n  ArrayStack() {\n    _stack = [];\n  }\n\n  /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n  int size() {\n    return _stack.length;\n  }\n\n  /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n  bool isEmpty() {\n    return _stack.isEmpty;\n  }\n\n  /* \u5165\u6808 */\n  void push(int _num) {\n    _stack.add(_num);\n  }\n\n  /* \u51fa\u6808 */\n  int pop() {\n    if (isEmpty()) {\n      throw Exception(\"\u6808\u4e3a\u7a7a\");\n    }\n    return _stack.removeLast();\n  }\n\n  /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n  int peek() {\n    if (isEmpty()) {\n      throw Exception(\"\u6808\u4e3a\u7a7a\");\n    }\n    return _stack.last;\n  }\n\n  /* \u5c06\u6808\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n  List<int> toArray() => _stack;\n}\n
        array_stack.rs
        /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nstruct ArrayStack<T> {\n    stack: Vec<T>,\n}\n\nimpl<T> ArrayStack<T> {\n    /* \u521d\u59cb\u5316\u6808 */\n    fn new() -> ArrayStack<T> {\n        ArrayStack::<T> {\n            stack: Vec::<T>::new(),\n        }\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    fn size(&self) -> usize {\n        self.stack.len()\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    fn is_empty(&self) -> bool {\n        self.size() == 0\n    }\n\n    /* \u5165\u6808 */\n    fn push(&mut self, num: T) {\n        self.stack.push(num);\n    }\n\n    /* \u51fa\u6808 */\n    fn pop(&mut self) -> Option<T> {\n        self.stack.pop()\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    fn peek(&self) -> Option<&T> {\n        if self.is_empty() {\n            panic!(\"\u6808\u4e3a\u7a7a\")\n        };\n        self.stack.last()\n    }\n\n    /* \u8fd4\u56de &Vec */\n    fn to_array(&self) -> &Vec<T> {\n        &self.stack\n    }\n}\n
        array_stack.c
        /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\ntypedef struct {\n    int *data;\n    int size;\n} ArrayStack;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayStack *newArrayStack() {\n    ArrayStack *stack = malloc(sizeof(ArrayStack));\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5927\u5bb9\u91cf\uff0c\u907f\u514d\u6269\u5bb9\n    stack->data = malloc(sizeof(int) * MAX_SIZE);\n    stack->size = 0;\n    return stack;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayStack(ArrayStack *stack) {\n    free(stack->data);\n    free(stack);\n}\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size(ArrayStack *stack) {\n    return stack->size;\n}\n\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty(ArrayStack *stack) {\n    return stack->size == 0;\n}\n\n/* \u5165\u6808 */\nvoid push(ArrayStack *stack, int num) {\n    if (stack->size == MAX_SIZE) {\n        printf(\"\u6808\u5df2\u6ee1\\n\");\n        return;\n    }\n    stack->data[stack->size] = num;\n    stack->size++;\n}\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek(ArrayStack *stack) {\n    if (stack->size == 0) {\n        printf(\"\u6808\u4e3a\u7a7a\\n\");\n        return INT_MAX;\n    }\n    return stack->data[stack->size - 1];\n}\n\n/* \u51fa\u6808 */\nint pop(ArrayStack *stack) {\n    int val = peek(stack);\n    stack->size--;\n    return val;\n}\n
        array_stack.kt
        /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n    // \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\n    private val stack = mutableListOf<Int>()\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    fun size(): Int {\n        return stack.size\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    fun isEmpty(): Boolean {\n        return size() == 0\n    }\n\n    /* \u5165\u6808 */\n    fun push(num: Int) {\n        stack.add(num)\n    }\n\n    /* \u51fa\u6808 */\n    fun pop(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return stack.removeAt(size() - 1)\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    fun peek(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return stack[size() - 1]\n    }\n\n    /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    fun toArray(): Array<Any> {\n        return stack.toTypedArray()\n    }\n}\n
        array_stack.rb
        ### \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 ###\nclass ArrayStack\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize\n    @stack = []\n  end\n\n  ### \u83b7\u53d6\u6808\u7684\u957f\u5ea6 ###\n  def size\n    @stack.length\n  end\n\n  ### \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a ###\n  def is_empty?\n    @stack.empty?\n  end\n\n  ### \u5165\u6808 ###\n  def push(item)\n    @stack << item\n  end\n\n  ### \u51fa\u6808 ###\n  def pop\n    raise IndexError, '\u6808\u4e3a\u7a7a' if is_empty?\n\n    @stack.pop\n  end\n\n  ### \u8bbf\u95ee\u6808\u9876\u5143\u7d20 ###\n  def peek\n    raise IndexError, '\u6808\u4e3a\u7a7a' if is_empty?\n\n    @stack.last\n  end\n\n  ### \u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370 ###\n  def to_array\n    @stack\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_stack_and_queue/stack/#513","title":"5.1.3 \u00a0 \u4e24\u79cd\u5b9e\u73b0\u5bf9\u6bd4","text":"

        \u652f\u6301\u64cd\u4f5c

        \u4e24\u79cd\u5b9e\u73b0\u90fd\u652f\u6301\u6808\u5b9a\u4e49\u4e2d\u7684\u5404\u9879\u64cd\u4f5c\u3002\u6570\u7ec4\u5b9e\u73b0\u989d\u5916\u652f\u6301\u968f\u673a\u8bbf\u95ee\uff0c\u4f46\u8fd9\u5df2\u8d85\u51fa\u4e86\u6808\u7684\u5b9a\u4e49\u8303\u7574\uff0c\u56e0\u6b64\u4e00\u822c\u4e0d\u4f1a\u7528\u5230\u3002

        \u65f6\u95f4\u6548\u7387

        \u5728\u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0\u4e2d\uff0c\u5165\u6808\u548c\u51fa\u6808\u64cd\u4f5c\u90fd\u5728\u9884\u5148\u5206\u914d\u597d\u7684\u8fde\u7eed\u5185\u5b58\u4e2d\u8fdb\u884c\uff0c\u5177\u6709\u5f88\u597d\u7684\u7f13\u5b58\u672c\u5730\u6027\uff0c\u56e0\u6b64\u6548\u7387\u8f83\u9ad8\u3002\u7136\u800c\uff0c\u5982\u679c\u5165\u6808\u65f6\u8d85\u51fa\u6570\u7ec4\u5bb9\u91cf\uff0c\u4f1a\u89e6\u53d1\u6269\u5bb9\u673a\u5236\uff0c\u5bfc\u81f4\u8be5\u6b21\u5165\u6808\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53d8\u4e3a \\(O(n)\\) \u3002

        \u5728\u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0\u4e2d\uff0c\u94fe\u8868\u7684\u6269\u5bb9\u975e\u5e38\u7075\u6d3b\uff0c\u4e0d\u5b58\u5728\u4e0a\u8ff0\u6570\u7ec4\u6269\u5bb9\u65f6\u6548\u7387\u964d\u4f4e\u7684\u95ee\u9898\u3002\u4f46\u662f\uff0c\u5165\u6808\u64cd\u4f5c\u9700\u8981\u521d\u59cb\u5316\u8282\u70b9\u5bf9\u8c61\u5e76\u4fee\u6539\u6307\u9488\uff0c\u56e0\u6b64\u6548\u7387\u76f8\u5bf9\u8f83\u4f4e\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u5165\u6808\u5143\u7d20\u672c\u8eab\u5c31\u662f\u8282\u70b9\u5bf9\u8c61\uff0c\u90a3\u4e48\u53ef\u4ee5\u7701\u53bb\u521d\u59cb\u5316\u6b65\u9aa4\uff0c\u4ece\u800c\u63d0\u9ad8\u6548\u7387\u3002

        \u7efc\u4e0a\u6240\u8ff0\uff0c\u5f53\u5165\u6808\u4e0e\u51fa\u6808\u64cd\u4f5c\u7684\u5143\u7d20\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b\u65f6\uff0c\u4f8b\u5982 int \u6216 double \uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\u3002

        • \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\u5728\u89e6\u53d1\u6269\u5bb9\u65f6\u6548\u7387\u4f1a\u964d\u4f4e\uff0c\u4f46\u7531\u4e8e\u6269\u5bb9\u662f\u4f4e\u9891\u64cd\u4f5c\uff0c\u56e0\u6b64\u5e73\u5747\u6548\u7387\u66f4\u9ad8\u3002
        • \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\u53ef\u4ee5\u63d0\u4f9b\u66f4\u52a0\u7a33\u5b9a\u7684\u6548\u7387\u8868\u73b0\u3002

        \u7a7a\u95f4\u6548\u7387

        \u5728\u521d\u59cb\u5316\u5217\u8868\u65f6\uff0c\u7cfb\u7edf\u4f1a\u4e3a\u5217\u8868\u5206\u914d\u201c\u521d\u59cb\u5bb9\u91cf\u201d\uff0c\u8be5\u5bb9\u91cf\u53ef\u80fd\u8d85\u51fa\u5b9e\u9645\u9700\u6c42\uff1b\u5e76\u4e14\uff0c\u6269\u5bb9\u673a\u5236\u901a\u5e38\u662f\u6309\u7167\u7279\u5b9a\u500d\u7387\uff08\u4f8b\u5982 2 \u500d\uff09\u8fdb\u884c\u6269\u5bb9\u7684\uff0c\u6269\u5bb9\u540e\u7684\u5bb9\u91cf\u4e5f\u53ef\u80fd\u8d85\u51fa\u5b9e\u9645\u9700\u6c42\u3002\u56e0\u6b64\uff0c\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\u53ef\u80fd\u9020\u6210\u4e00\u5b9a\u7684\u7a7a\u95f4\u6d6a\u8d39\u3002

        \u7136\u800c\uff0c\u7531\u4e8e\u94fe\u8868\u8282\u70b9\u9700\u8981\u989d\u5916\u5b58\u50a8\u6307\u9488\uff0c\u56e0\u6b64\u94fe\u8868\u8282\u70b9\u5360\u7528\u7684\u7a7a\u95f4\u76f8\u5bf9\u8f83\u5927\u3002

        \u7efc\u4e0a\uff0c\u6211\u4eec\u4e0d\u80fd\u7b80\u5355\u5730\u786e\u5b9a\u54ea\u79cd\u5b9e\u73b0\u66f4\u52a0\u8282\u7701\u5185\u5b58\uff0c\u9700\u8981\u9488\u5bf9\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u5206\u6790\u3002

        "},{"location":"chapter_stack_and_queue/stack/#514","title":"5.1.4 \u00a0 \u6808\u7684\u5178\u578b\u5e94\u7528","text":"
        • \u6d4f\u89c8\u5668\u4e2d\u7684\u540e\u9000\u4e0e\u524d\u8fdb\u3001\u8f6f\u4ef6\u4e2d\u7684\u64a4\u9500\u4e0e\u53cd\u64a4\u9500\u3002\u6bcf\u5f53\u6211\u4eec\u6253\u5f00\u65b0\u7684\u7f51\u9875\uff0c\u6d4f\u89c8\u5668\u5c31\u4f1a\u5bf9\u4e0a\u4e00\u4e2a\u7f51\u9875\u6267\u884c\u5165\u6808\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u540e\u9000\u64cd\u4f5c\u56de\u5230\u4e0a\u4e00\u4e2a\u7f51\u9875\u3002\u540e\u9000\u64cd\u4f5c\u5b9e\u9645\u4e0a\u662f\u5728\u6267\u884c\u51fa\u6808\u3002\u5982\u679c\u8981\u540c\u65f6\u652f\u6301\u540e\u9000\u548c\u524d\u8fdb\uff0c\u90a3\u4e48\u9700\u8981\u4e24\u4e2a\u6808\u6765\u914d\u5408\u5b9e\u73b0\u3002
        • \u7a0b\u5e8f\u5185\u5b58\u7ba1\u7406\u3002\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u7cfb\u7edf\u90fd\u4f1a\u5728\u6808\u9876\u6dfb\u52a0\u4e00\u4e2a\u6808\u5e27\uff0c\u7528\u4e8e\u8bb0\u5f55\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002\u5728\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u5411\u4e0b\u9012\u63a8\u9636\u6bb5\u4f1a\u4e0d\u65ad\u6267\u884c\u5165\u6808\u64cd\u4f5c\uff0c\u800c\u5411\u4e0a\u56de\u6eaf\u9636\u6bb5\u5219\u4f1a\u4e0d\u65ad\u6267\u884c\u51fa\u6808\u64cd\u4f5c\u3002
        "},{"location":"chapter_stack_and_queue/summary/","title":"5.4 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_stack_and_queue/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u6808\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u540e\u51fa\u539f\u5219\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u901a\u8fc7\u6570\u7ec4\u6216\u94fe\u8868\u6765\u5b9e\u73b0\u3002
        • \u5728\u65f6\u95f4\u6548\u7387\u65b9\u9762\uff0c\u6808\u7684\u6570\u7ec4\u5b9e\u73b0\u5177\u6709\u8f83\u9ad8\u7684\u5e73\u5747\u6548\u7387\uff0c\u4f46\u5728\u6269\u5bb9\u8fc7\u7a0b\u4e2d\uff0c\u5355\u6b21\u5165\u6808\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u52a3\u5316\u81f3 \\(O(n)\\) \u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u6808\u7684\u94fe\u8868\u5b9e\u73b0\u5177\u6709\u66f4\u4e3a\u7a33\u5b9a\u7684\u6548\u7387\u8868\u73b0\u3002
        • \u5728\u7a7a\u95f4\u6548\u7387\u65b9\u9762\uff0c\u6808\u7684\u6570\u7ec4\u5b9e\u73b0\u53ef\u80fd\u5bfc\u81f4\u4e00\u5b9a\u7a0b\u5ea6\u7684\u7a7a\u95f4\u6d6a\u8d39\u3002\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u94fe\u8868\u8282\u70b9\u6240\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u6bd4\u6570\u7ec4\u5143\u7d20\u66f4\u5927\u3002
        • \u961f\u5217\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u5148\u51fa\u539f\u5219\u7684\u6570\u636e\u7ed3\u6784\uff0c\u540c\u6837\u53ef\u4ee5\u901a\u8fc7\u6570\u7ec4\u6216\u94fe\u8868\u6765\u5b9e\u73b0\u3002\u5728\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u7684\u5bf9\u6bd4\u4e0a\uff0c\u961f\u5217\u7684\u7ed3\u8bba\u4e0e\u524d\u8ff0\u6808\u7684\u7ed3\u8bba\u76f8\u4f3c\u3002
        • \u53cc\u5411\u961f\u5217\u662f\u4e00\u79cd\u5177\u6709\u66f4\u9ad8\u81ea\u7531\u5ea6\u7684\u961f\u5217\uff0c\u5b83\u5141\u8bb8\u5728\u4e24\u7aef\u8fdb\u884c\u5143\u7d20\u7684\u6dfb\u52a0\u548c\u5220\u9664\u64cd\u4f5c\u3002
        "},{"location":"chapter_stack_and_queue/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u6d4f\u89c8\u5668\u7684\u524d\u8fdb\u540e\u9000\u662f\u5426\u662f\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\uff1f

        \u6d4f\u89c8\u5668\u7684\u524d\u8fdb\u540e\u9000\u529f\u80fd\u672c\u8d28\u4e0a\u662f\u201c\u6808\u201d\u7684\u4f53\u73b0\u3002\u5f53\u7528\u6237\u8bbf\u95ee\u4e00\u4e2a\u65b0\u9875\u9762\u65f6\uff0c\u8be5\u9875\u9762\u4f1a\u88ab\u6dfb\u52a0\u5230\u6808\u9876\uff1b\u5f53\u7528\u6237\u70b9\u51fb\u540e\u9000\u6309\u94ae\u65f6\uff0c\u8be5\u9875\u9762\u4f1a\u4ece\u6808\u9876\u5f39\u51fa\u3002\u4f7f\u7528\u53cc\u5411\u961f\u5217\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u4e00\u4e9b\u989d\u5916\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u5728\u201c\u53cc\u5411\u961f\u5217\u201d\u7ae0\u8282\u6709\u63d0\u5230\u3002

        Q\uff1a\u5728\u51fa\u6808\u540e\uff0c\u662f\u5426\u9700\u8981\u91ca\u653e\u51fa\u6808\u8282\u70b9\u7684\u5185\u5b58\uff1f

        \u5982\u679c\u540e\u7eed\u4ecd\u9700\u8981\u4f7f\u7528\u5f39\u51fa\u8282\u70b9\uff0c\u5219\u4e0d\u9700\u8981\u91ca\u653e\u5185\u5b58\u3002\u82e5\u4e4b\u540e\u4e0d\u9700\u8981\u7528\u5230\uff0cJava \u548c Python \u7b49\u8bed\u8a00\u62e5\u6709\u81ea\u52a8\u5783\u573e\u56de\u6536\u673a\u5236\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u624b\u52a8\u91ca\u653e\u5185\u5b58\uff1b\u5728 C \u548c C++ \u4e2d\u9700\u8981\u624b\u52a8\u91ca\u653e\u5185\u5b58\u3002

        Q\uff1a\u53cc\u5411\u961f\u5217\u50cf\u662f\u4e24\u4e2a\u6808\u62fc\u63a5\u5728\u4e86\u4e00\u8d77\uff0c\u5b83\u7684\u7528\u9014\u662f\u4ec0\u4e48\uff1f

        \u53cc\u5411\u961f\u5217\u5c31\u50cf\u662f\u6808\u548c\u961f\u5217\u7684\u7ec4\u5408\u6216\u4e24\u4e2a\u6808\u62fc\u5728\u4e86\u4e00\u8d77\u3002\u5b83\u8868\u73b0\u7684\u662f\u6808 + \u961f\u5217\u7684\u903b\u8f91\uff0c\u56e0\u6b64\u53ef\u4ee5\u5b9e\u73b0\u6808\u4e0e\u961f\u5217\u7684\u6240\u6709\u5e94\u7528\uff0c\u5e76\u4e14\u66f4\u52a0\u7075\u6d3b\u3002

        Q\uff1a\u64a4\u9500\uff08undo\uff09\u548c\u53cd\u64a4\u9500\uff08redo\uff09\u5177\u4f53\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1f

        \u4f7f\u7528\u4e24\u4e2a\u6808\uff0c\u6808 A \u7528\u4e8e\u64a4\u9500\uff0c\u6808 B \u7528\u4e8e\u53cd\u64a4\u9500\u3002

        1. \u6bcf\u5f53\u7528\u6237\u6267\u884c\u4e00\u4e2a\u64cd\u4f5c\uff0c\u5c06\u8fd9\u4e2a\u64cd\u4f5c\u538b\u5165\u6808 A \uff0c\u5e76\u6e05\u7a7a\u6808 B \u3002
        2. \u5f53\u7528\u6237\u6267\u884c\u201c\u64a4\u9500\u201d\u65f6\uff0c\u4ece\u6808 A \u4e2d\u5f39\u51fa\u6700\u8fd1\u7684\u64cd\u4f5c\uff0c\u5e76\u5c06\u5176\u538b\u5165\u6808 B \u3002
        3. \u5f53\u7528\u6237\u6267\u884c\u201c\u53cd\u64a4\u9500\u201d\u65f6\uff0c\u4ece\u6808 B \u4e2d\u5f39\u51fa\u6700\u8fd1\u7684\u64cd\u4f5c\uff0c\u5e76\u5c06\u5176\u538b\u5165\u6808 A \u3002
        "},{"location":"chapter_tree/","title":"\u7b2c 7 \u7ae0 \u00a0 \u6811","text":"

        Abstract

        \u53c2\u5929\u5927\u6811\u5145\u6ee1\u751f\u547d\u529b\uff0c\u6839\u6df1\u53f6\u8302\uff0c\u5206\u679d\u6276\u758f\u3002

        \u5b83\u4e3a\u6211\u4eec\u5c55\u73b0\u4e86\u6570\u636e\u5206\u6cbb\u7684\u751f\u52a8\u5f62\u6001\u3002

        "},{"location":"chapter_tree/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 7.1 \u00a0 \u4e8c\u53c9\u6811
        • 7.2 \u00a0 \u4e8c\u53c9\u6811\u904d\u5386
        • 7.3 \u00a0 \u4e8c\u53c9\u6811\u6570\u7ec4\u8868\u793a
        • 7.4 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811
        • 7.5 \u00a0 AVL \u6811 *
        • 7.6 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_tree/array_representation_of_tree/","title":"7.3 \u00a0 \u4e8c\u53c9\u6811\u6570\u7ec4\u8868\u793a","text":"

        \u5728\u94fe\u8868\u8868\u793a\u4e0b\uff0c\u4e8c\u53c9\u6811\u7684\u5b58\u50a8\u5355\u5143\u4e3a\u8282\u70b9 TreeNode \uff0c\u8282\u70b9\u4e4b\u95f4\u901a\u8fc7\u6307\u9488\u76f8\u8fde\u63a5\u3002\u4e0a\u4e00\u8282\u4ecb\u7ecd\u4e86\u94fe\u8868\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7684\u5404\u9879\u57fa\u672c\u64cd\u4f5c\u3002

        \u90a3\u4e48\uff0c\u6211\u4eec\u80fd\u5426\u7528\u6570\u7ec4\u6765\u8868\u793a\u4e8c\u53c9\u6811\u5462\uff1f\u7b54\u6848\u662f\u80af\u5b9a\u7684\u3002

        "},{"location":"chapter_tree/array_representation_of_tree/#731","title":"7.3.1 \u00a0 \u8868\u793a\u5b8c\u7f8e\u4e8c\u53c9\u6811","text":"

        \u5148\u5206\u6790\u4e00\u4e2a\u7b80\u5355\u6848\u4f8b\u3002\u7ed9\u5b9a\u4e00\u68f5\u5b8c\u7f8e\u4e8c\u53c9\u6811\uff0c\u6211\u4eec\u5c06\u6240\u6709\u8282\u70b9\u6309\u7167\u5c42\u5e8f\u904d\u5386\u7684\u987a\u5e8f\u5b58\u50a8\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff0c\u5219\u6bcf\u4e2a\u8282\u70b9\u90fd\u5bf9\u5e94\u552f\u4e00\u7684\u6570\u7ec4\u7d22\u5f15\u3002

        \u6839\u636e\u5c42\u5e8f\u904d\u5386\u7684\u7279\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u63a8\u5bfc\u51fa\u7236\u8282\u70b9\u7d22\u5f15\u4e0e\u5b50\u8282\u70b9\u7d22\u5f15\u4e4b\u95f4\u7684\u201c\u6620\u5c04\u516c\u5f0f\u201d\uff1a\u82e5\u67d0\u8282\u70b9\u7684\u7d22\u5f15\u4e3a \\(i\\) \uff0c\u5219\u8be5\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 1\\) \uff0c\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 2\\) \u3002\u56fe 7-12 \u5c55\u793a\u4e86\u5404\u4e2a\u8282\u70b9\u7d22\u5f15\u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb\u3002

        \u56fe 7-12 \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a

        \u6620\u5c04\u516c\u5f0f\u7684\u89d2\u8272\u76f8\u5f53\u4e8e\u94fe\u8868\u4e2d\u7684\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\u3002\u7ed9\u5b9a\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u8282\u70b9\uff0c\u6211\u4eec\u90fd\u53ef\u4ee5\u901a\u8fc7\u6620\u5c04\u516c\u5f0f\u6765\u8bbf\u95ee\u5b83\u7684\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\u3002

        "},{"location":"chapter_tree/array_representation_of_tree/#732","title":"7.3.2 \u00a0 \u8868\u793a\u4efb\u610f\u4e8c\u53c9\u6811","text":"

        \u5b8c\u7f8e\u4e8c\u53c9\u6811\u662f\u4e00\u4e2a\u7279\u4f8b\uff0c\u5728\u4e8c\u53c9\u6811\u7684\u4e2d\u95f4\u5c42\u901a\u5e38\u5b58\u5728\u8bb8\u591a None \u3002\u7531\u4e8e\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5e76\u4e0d\u5305\u542b\u8fd9\u4e9b None \uff0c\u56e0\u6b64\u6211\u4eec\u65e0\u6cd5\u4ec5\u51ed\u8be5\u5e8f\u5217\u6765\u63a8\u6d4b None \u7684\u6570\u91cf\u548c\u5206\u5e03\u4f4d\u7f6e\u3002\u8fd9\u610f\u5473\u7740\u5b58\u5728\u591a\u79cd\u4e8c\u53c9\u6811\u7ed3\u6784\u90fd\u7b26\u5408\u8be5\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u3002

        \u5982\u56fe 7-13 \u6240\u793a\uff0c\u7ed9\u5b9a\u4e00\u68f5\u975e\u5b8c\u7f8e\u4e8c\u53c9\u6811\uff0c\u4e0a\u8ff0\u6570\u7ec4\u8868\u793a\u65b9\u6cd5\u5df2\u7ecf\u5931\u6548\u3002

        \u56fe 7-13 \u00a0 \u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5bf9\u5e94\u591a\u79cd\u4e8c\u53c9\u6811\u53ef\u80fd\u6027

        \u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u5728\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u4e2d\u663e\u5f0f\u5730\u5199\u51fa\u6240\u6709 None \u3002\u5982\u56fe 7-14 \u6240\u793a\uff0c\u8fd9\u6837\u5904\u7406\u540e\uff0c\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5c31\u53ef\u4ee5\u552f\u4e00\u8868\u793a\u4e8c\u53c9\u6811\u4e86\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        # \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a\n# \u4f7f\u7528 None \u6765\u8868\u793a\u7a7a\u4f4d\ntree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]\n
        /* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int \u6700\u5927\u503c INT_MAX \u6807\u8bb0\u7a7a\u4f4d\nvector<int> tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
        /* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int \u7684\u5305\u88c5\u7c7b Integer \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nInteger[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };\n
        /* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nint?[] tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
        /* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 any \u7c7b\u578b\u7684\u5207\u7247, \u5c31\u53ef\u4ee5\u4f7f\u7528 nil \u6765\u6807\u8bb0\u7a7a\u4f4d\ntree := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15}\n
        /* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 Int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 nil \u6765\u6807\u8bb0\u7a7a\u4f4d\nlet tree: [Int?] = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]\n
        /* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 null \u6765\u8868\u793a\u7a7a\u4f4d\nlet tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
        /* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 null \u6765\u8868\u793a\u7a7a\u4f4d\nlet tree: (number | null)[] = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
        /* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nList<int?> tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
        /* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 None \u6765\u6807\u8bb0\u7a7a\u4f4d\nlet tree = [Some(1), Some(2), Some(3), Some(4), None, Some(6), Some(7), Some(8), Some(9), None, None, Some(12), None, None, Some(15)];\n
        /* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int \u6700\u5927\u503c\u6807\u8bb0\u7a7a\u4f4d\uff0c\u56e0\u6b64\u8981\u6c42\u8282\u70b9\u503c\u4e0d\u80fd\u4e3a INT_MAX\nint tree[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
        /* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 null \u6765\u8868\u793a\u7a7a\u4f4d\nval tree = arrayOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 )\n
        ### \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a ###\n# \u4f7f\u7528 nil \u6765\u8868\u793a\u7a7a\u4f4d\ntree = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]\n

        \u56fe 7-14 \u00a0 \u4efb\u610f\u7c7b\u578b\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a

        \u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u5b8c\u5168\u4e8c\u53c9\u6811\u975e\u5e38\u9002\u5408\u4f7f\u7528\u6570\u7ec4\u6765\u8868\u793a\u3002\u56de\u987e\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u5b9a\u4e49\uff0cNone \u53ea\u51fa\u73b0\u5728\u6700\u5e95\u5c42\u4e14\u9760\u53f3\u7684\u4f4d\u7f6e\uff0c\u56e0\u6b64\u6240\u6709 None \u4e00\u5b9a\u51fa\u73b0\u5728\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u7684\u672b\u5c3e\u3002

        \u8fd9\u610f\u5473\u7740\u4f7f\u7528\u6570\u7ec4\u8868\u793a\u5b8c\u5168\u4e8c\u53c9\u6811\u65f6\uff0c\u53ef\u4ee5\u7701\u7565\u5b58\u50a8\u6240\u6709 None \uff0c\u975e\u5e38\u65b9\u4fbf\u3002\u56fe 7-15 \u7ed9\u51fa\u4e86\u4e00\u4e2a\u4f8b\u5b50\u3002

        \u56fe 7-15 \u00a0 \u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a

        \u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u68f5\u57fa\u4e8e\u6570\u7ec4\u8868\u793a\u7684\u4e8c\u53c9\u6811\uff0c\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\u64cd\u4f5c\u3002

        • \u7ed9\u5b9a\u67d0\u8282\u70b9\uff0c\u83b7\u53d6\u5b83\u7684\u503c\u3001\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\u3001\u7236\u8282\u70b9\u3002
        • \u83b7\u53d6\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u3001\u540e\u5e8f\u904d\u5386\u3001\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_binary_tree.py
        class ArrayBinaryTree:\n    \"\"\"\u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b\"\"\"\n\n    def __init__(self, arr: list[int | None]):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        self._tree = list(arr)\n\n    def size(self):\n        \"\"\"\u5217\u8868\u5bb9\u91cf\"\"\"\n        return len(self._tree)\n\n    def val(self, i: int) -> int | None:\n        \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c\"\"\"\n        # \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de None \uff0c\u4ee3\u8868\u7a7a\u4f4d\n        if i < 0 or i >= self.size():\n            return None\n        return self._tree[i]\n\n    def left(self, i: int) -> int | None:\n        \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n        return 2 * i + 1\n\n    def right(self, i: int) -> int | None:\n        \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n        return 2 * i + 2\n\n    def parent(self, i: int) -> int | None:\n        \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n        return (i - 1) // 2\n\n    def level_order(self) -> list[int]:\n        \"\"\"\u5c42\u5e8f\u904d\u5386\"\"\"\n        self.res = []\n        # \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n        for i in range(self.size()):\n            if self.val(i) is not None:\n                self.res.append(self.val(i))\n        return self.res\n\n    def dfs(self, i: int, order: str):\n        \"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386\"\"\"\n        if self.val(i) is None:\n            return\n        # \u524d\u5e8f\u904d\u5386\n        if order == \"pre\":\n            self.res.append(self.val(i))\n        self.dfs(self.left(i), order)\n        # \u4e2d\u5e8f\u904d\u5386\n        if order == \"in\":\n            self.res.append(self.val(i))\n        self.dfs(self.right(i), order)\n        # \u540e\u5e8f\u904d\u5386\n        if order == \"post\":\n            self.res.append(self.val(i))\n\n    def pre_order(self) -> list[int]:\n        \"\"\"\u524d\u5e8f\u904d\u5386\"\"\"\n        self.res = []\n        self.dfs(0, order=\"pre\")\n        return self.res\n\n    def in_order(self) -> list[int]:\n        \"\"\"\u4e2d\u5e8f\u904d\u5386\"\"\"\n        self.res = []\n        self.dfs(0, order=\"in\")\n        return self.res\n\n    def post_order(self) -> list[int]:\n        \"\"\"\u540e\u5e8f\u904d\u5386\"\"\"\n        self.res = []\n        self.dfs(0, order=\"post\")\n        return self.res\n
        array_binary_tree.cpp
        /* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n  public:\n    /* \u6784\u9020\u65b9\u6cd5 */\n    ArrayBinaryTree(vector<int> arr) {\n        tree = arr;\n    }\n\n    /* \u5217\u8868\u5bb9\u91cf */\n    int size() {\n        return tree.size();\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n    int val(int i) {\n        // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de INT_MAX \uff0c\u4ee3\u8868\u7a7a\u4f4d\n        if (i < 0 || i >= size())\n            return INT_MAX;\n        return tree[i];\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    int left(int i) {\n        return 2 * i + 1;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    int right(int i) {\n        return 2 * i + 2;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n    int parent(int i) {\n        return (i - 1) / 2;\n    }\n\n    /* \u5c42\u5e8f\u904d\u5386 */\n    vector<int> levelOrder() {\n        vector<int> res;\n        // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n        for (int i = 0; i < size(); i++) {\n            if (val(i) != INT_MAX)\n                res.push_back(val(i));\n        }\n        return res;\n    }\n\n    /* \u524d\u5e8f\u904d\u5386 */\n    vector<int> preOrder() {\n        vector<int> res;\n        dfs(0, \"pre\", res);\n        return res;\n    }\n\n    /* \u4e2d\u5e8f\u904d\u5386 */\n    vector<int> inOrder() {\n        vector<int> res;\n        dfs(0, \"in\", res);\n        return res;\n    }\n\n    /* \u540e\u5e8f\u904d\u5386 */\n    vector<int> postOrder() {\n        vector<int> res;\n        dfs(0, \"post\", res);\n        return res;\n    }\n\n  private:\n    vector<int> tree;\n\n    /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n    void dfs(int i, string order, vector<int> &res) {\n        // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n        if (val(i) == INT_MAX)\n            return;\n        // \u524d\u5e8f\u904d\u5386\n        if (order == \"pre\")\n            res.push_back(val(i));\n        dfs(left(i), order, res);\n        // \u4e2d\u5e8f\u904d\u5386\n        if (order == \"in\")\n            res.push_back(val(i));\n        dfs(right(i), order, res);\n        // \u540e\u5e8f\u904d\u5386\n        if (order == \"post\")\n            res.push_back(val(i));\n    }\n};\n
        array_binary_tree.java
        /* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n    private List<Integer> tree;\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public ArrayBinaryTree(List<Integer> arr) {\n        tree = new ArrayList<>(arr);\n    }\n\n    /* \u5217\u8868\u5bb9\u91cf */\n    public int size() {\n        return tree.size();\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n    public Integer val(int i) {\n        // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n        if (i < 0 || i >= size())\n            return null;\n        return tree.get(i);\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    public Integer left(int i) {\n        return 2 * i + 1;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    public Integer right(int i) {\n        return 2 * i + 2;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n    public Integer parent(int i) {\n        return (i - 1) / 2;\n    }\n\n    /* \u5c42\u5e8f\u904d\u5386 */\n    public List<Integer> levelOrder() {\n        List<Integer> res = new ArrayList<>();\n        // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n        for (int i = 0; i < size(); i++) {\n            if (val(i) != null)\n                res.add(val(i));\n        }\n        return res;\n    }\n\n    /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n    private void dfs(Integer i, String order, List<Integer> res) {\n        // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n        if (val(i) == null)\n            return;\n        // \u524d\u5e8f\u904d\u5386\n        if (\"pre\".equals(order))\n            res.add(val(i));\n        dfs(left(i), order, res);\n        // \u4e2d\u5e8f\u904d\u5386\n        if (\"in\".equals(order))\n            res.add(val(i));\n        dfs(right(i), order, res);\n        // \u540e\u5e8f\u904d\u5386\n        if (\"post\".equals(order))\n            res.add(val(i));\n    }\n\n    /* \u524d\u5e8f\u904d\u5386 */\n    public List<Integer> preOrder() {\n        List<Integer> res = new ArrayList<>();\n        dfs(0, \"pre\", res);\n        return res;\n    }\n\n    /* \u4e2d\u5e8f\u904d\u5386 */\n    public List<Integer> inOrder() {\n        List<Integer> res = new ArrayList<>();\n        dfs(0, \"in\", res);\n        return res;\n    }\n\n    /* \u540e\u5e8f\u904d\u5386 */\n    public List<Integer> postOrder() {\n        List<Integer> res = new ArrayList<>();\n        dfs(0, \"post\", res);\n        return res;\n    }\n}\n
        array_binary_tree.cs
        /* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree(List<int?> arr) {\n    List<int?> tree = new(arr);\n\n    /* \u5217\u8868\u5bb9\u91cf */\n    public int Size() {\n        return tree.Count;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n    public int? Val(int i) {\n        // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n        if (i < 0 || i >= Size())\n            return null;\n        return tree[i];\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    public int Left(int i) {\n        return 2 * i + 1;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    public int Right(int i) {\n        return 2 * i + 2;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n    public int Parent(int i) {\n        return (i - 1) / 2;\n    }\n\n    /* \u5c42\u5e8f\u904d\u5386 */\n    public List<int> LevelOrder() {\n        List<int> res = [];\n        // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n        for (int i = 0; i < Size(); i++) {\n            if (Val(i).HasValue)\n                res.Add(Val(i)!.Value);\n        }\n        return res;\n    }\n\n    /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n    void DFS(int i, string order, List<int> res) {\n        // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n        if (!Val(i).HasValue)\n            return;\n        // \u524d\u5e8f\u904d\u5386\n        if (order == \"pre\")\n            res.Add(Val(i)!.Value);\n        DFS(Left(i), order, res);\n        // \u4e2d\u5e8f\u904d\u5386\n        if (order == \"in\")\n            res.Add(Val(i)!.Value);\n        DFS(Right(i), order, res);\n        // \u540e\u5e8f\u904d\u5386\n        if (order == \"post\")\n            res.Add(Val(i)!.Value);\n    }\n\n    /* \u524d\u5e8f\u904d\u5386 */\n    public List<int> PreOrder() {\n        List<int> res = [];\n        DFS(0, \"pre\", res);\n        return res;\n    }\n\n    /* \u4e2d\u5e8f\u904d\u5386 */\n    public List<int> InOrder() {\n        List<int> res = [];\n        DFS(0, \"in\", res);\n        return res;\n    }\n\n    /* \u540e\u5e8f\u904d\u5386 */\n    public List<int> PostOrder() {\n        List<int> res = [];\n        DFS(0, \"post\", res);\n        return res;\n    }\n}\n
        array_binary_tree.go
        /* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\ntype arrayBinaryTree struct {\n    tree []any\n}\n\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newArrayBinaryTree(arr []any) *arrayBinaryTree {\n    return &arrayBinaryTree{\n        tree: arr,\n    }\n}\n\n/* \u5217\u8868\u5bb9\u91cf */\nfunc (abt *arrayBinaryTree) size() int {\n    return len(abt.tree)\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nfunc (abt *arrayBinaryTree) val(i int) any {\n    // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n    if i < 0 || i >= abt.size() {\n        return nil\n    }\n    return abt.tree[i]\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) left(i int) int {\n    return 2*i + 1\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) right(i int) int {\n    return 2*i + 2\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) parent(i int) int {\n    return (i - 1) / 2\n}\n\n/* \u5c42\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) levelOrder() []any {\n    var res []any\n    // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n    for i := 0; i < abt.size(); i++ {\n        if abt.val(i) != nil {\n            res = append(res, abt.val(i))\n        }\n    }\n    return res\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nfunc (abt *arrayBinaryTree) dfs(i int, order string, res *[]any) {\n    // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n    if abt.val(i) == nil {\n        return\n    }\n    // \u524d\u5e8f\u904d\u5386\n    if order == \"pre\" {\n        *res = append(*res, abt.val(i))\n    }\n    abt.dfs(abt.left(i), order, res)\n    // \u4e2d\u5e8f\u904d\u5386\n    if order == \"in\" {\n        *res = append(*res, abt.val(i))\n    }\n    abt.dfs(abt.right(i), order, res)\n    // \u540e\u5e8f\u904d\u5386\n    if order == \"post\" {\n        *res = append(*res, abt.val(i))\n    }\n}\n\n/* \u524d\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) preOrder() []any {\n    var res []any\n    abt.dfs(0, \"pre\", &res)\n    return res\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) inOrder() []any {\n    var res []any\n    abt.dfs(0, \"in\", &res)\n    return res\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) postOrder() []any {\n    var res []any\n    abt.dfs(0, \"post\", &res)\n    return res\n}\n
        array_binary_tree.swift
        /* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n    private var tree: [Int?]\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    init(arr: [Int?]) {\n        tree = arr\n    }\n\n    /* \u5217\u8868\u5bb9\u91cf */\n    func size() -> Int {\n        tree.count\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n    func val(i: Int) -> Int? {\n        // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n        if i < 0 || i >= size() {\n            return nil\n        }\n        return tree[i]\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    func left(i: Int) -> Int {\n        2 * i + 1\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    func right(i: Int) -> Int {\n        2 * i + 2\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n    func parent(i: Int) -> Int {\n        (i - 1) / 2\n    }\n\n    /* \u5c42\u5e8f\u904d\u5386 */\n    func levelOrder() -> [Int] {\n        var res: [Int] = []\n        // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n        for i in 0 ..< size() {\n            if let val = val(i: i) {\n                res.append(val)\n            }\n        }\n        return res\n    }\n\n    /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n    private func dfs(i: Int, order: String, res: inout [Int]) {\n        // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n        guard let val = val(i: i) else {\n            return\n        }\n        // \u524d\u5e8f\u904d\u5386\n        if order == \"pre\" {\n            res.append(val)\n        }\n        dfs(i: left(i: i), order: order, res: &res)\n        // \u4e2d\u5e8f\u904d\u5386\n        if order == \"in\" {\n            res.append(val)\n        }\n        dfs(i: right(i: i), order: order, res: &res)\n        // \u540e\u5e8f\u904d\u5386\n        if order == \"post\" {\n            res.append(val)\n        }\n    }\n\n    /* \u524d\u5e8f\u904d\u5386 */\n    func preOrder() -> [Int] {\n        var res: [Int] = []\n        dfs(i: 0, order: \"pre\", res: &res)\n        return res\n    }\n\n    /* \u4e2d\u5e8f\u904d\u5386 */\n    func inOrder() -> [Int] {\n        var res: [Int] = []\n        dfs(i: 0, order: \"in\", res: &res)\n        return res\n    }\n\n    /* \u540e\u5e8f\u904d\u5386 */\n    func postOrder() -> [Int] {\n        var res: [Int] = []\n        dfs(i: 0, order: \"post\", res: &res)\n        return res\n    }\n}\n
        array_binary_tree.js
        /* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n    #tree;\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor(arr) {\n        this.#tree = arr;\n    }\n\n    /* \u5217\u8868\u5bb9\u91cf */\n    size() {\n        return this.#tree.length;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n    val(i) {\n        // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n        if (i < 0 || i >= this.size()) return null;\n        return this.#tree[i];\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    left(i) {\n        return 2 * i + 1;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    right(i) {\n        return 2 * i + 2;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n    parent(i) {\n        return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n    }\n\n    /* \u5c42\u5e8f\u904d\u5386 */\n    levelOrder() {\n        let res = [];\n        // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n        for (let i = 0; i < this.size(); i++) {\n            if (this.val(i) !== null) res.push(this.val(i));\n        }\n        return res;\n    }\n\n    /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n    #dfs(i, order, res) {\n        // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n        if (this.val(i) === null) return;\n        // \u524d\u5e8f\u904d\u5386\n        if (order === 'pre') res.push(this.val(i));\n        this.#dfs(this.left(i), order, res);\n        // \u4e2d\u5e8f\u904d\u5386\n        if (order === 'in') res.push(this.val(i));\n        this.#dfs(this.right(i), order, res);\n        // \u540e\u5e8f\u904d\u5386\n        if (order === 'post') res.push(this.val(i));\n    }\n\n    /* \u524d\u5e8f\u904d\u5386 */\n    preOrder() {\n        const res = [];\n        this.#dfs(0, 'pre', res);\n        return res;\n    }\n\n    /* \u4e2d\u5e8f\u904d\u5386 */\n    inOrder() {\n        const res = [];\n        this.#dfs(0, 'in', res);\n        return res;\n    }\n\n    /* \u540e\u5e8f\u904d\u5386 */\n    postOrder() {\n        const res = [];\n        this.#dfs(0, 'post', res);\n        return res;\n    }\n}\n
        array_binary_tree.ts
        /* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n    #tree: (number | null)[];\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor(arr: (number | null)[]) {\n        this.#tree = arr;\n    }\n\n    /* \u5217\u8868\u5bb9\u91cf */\n    size(): number {\n        return this.#tree.length;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n    val(i: number): number | null {\n        // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n        if (i < 0 || i >= this.size()) return null;\n        return this.#tree[i];\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    left(i: number): number {\n        return 2 * i + 1;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    right(i: number): number {\n        return 2 * i + 2;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n    parent(i: number): number {\n        return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n    }\n\n    /* \u5c42\u5e8f\u904d\u5386 */\n    levelOrder(): number[] {\n        let res = [];\n        // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n        for (let i = 0; i < this.size(); i++) {\n            if (this.val(i) !== null) res.push(this.val(i));\n        }\n        return res;\n    }\n\n    /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n    #dfs(i: number, order: Order, res: (number | null)[]): void {\n        // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n        if (this.val(i) === null) return;\n        // \u524d\u5e8f\u904d\u5386\n        if (order === 'pre') res.push(this.val(i));\n        this.#dfs(this.left(i), order, res);\n        // \u4e2d\u5e8f\u904d\u5386\n        if (order === 'in') res.push(this.val(i));\n        this.#dfs(this.right(i), order, res);\n        // \u540e\u5e8f\u904d\u5386\n        if (order === 'post') res.push(this.val(i));\n    }\n\n    /* \u524d\u5e8f\u904d\u5386 */\n    preOrder(): (number | null)[] {\n        const res = [];\n        this.#dfs(0, 'pre', res);\n        return res;\n    }\n\n    /* \u4e2d\u5e8f\u904d\u5386 */\n    inOrder(): (number | null)[] {\n        const res = [];\n        this.#dfs(0, 'in', res);\n        return res;\n    }\n\n    /* \u540e\u5e8f\u904d\u5386 */\n    postOrder(): (number | null)[] {\n        const res = [];\n        this.#dfs(0, 'post', res);\n        return res;\n    }\n}\n
        array_binary_tree.dart
        /* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n  late List<int?> _tree;\n\n  /* \u6784\u9020\u65b9\u6cd5 */\n  ArrayBinaryTree(this._tree);\n\n  /* \u5217\u8868\u5bb9\u91cf */\n  int size() {\n    return _tree.length;\n  }\n\n  /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n  int? val(int i) {\n    // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n    if (i < 0 || i >= size()) {\n      return null;\n    }\n    return _tree[i];\n  }\n\n  /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n  int? left(int i) {\n    return 2 * i + 1;\n  }\n\n  /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n  int? right(int i) {\n    return 2 * i + 2;\n  }\n\n  /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n  int? parent(int i) {\n    return (i - 1) ~/ 2;\n  }\n\n  /* \u5c42\u5e8f\u904d\u5386 */\n  List<int> levelOrder() {\n    List<int> res = [];\n    for (int i = 0; i < size(); i++) {\n      if (val(i) != null) {\n        res.add(val(i)!);\n      }\n    }\n    return res;\n  }\n\n  /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n  void dfs(int i, String order, List<int?> res) {\n    // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n    if (val(i) == null) {\n      return;\n    }\n    // \u524d\u5e8f\u904d\u5386\n    if (order == 'pre') {\n      res.add(val(i));\n    }\n    dfs(left(i)!, order, res);\n    // \u4e2d\u5e8f\u904d\u5386\n    if (order == 'in') {\n      res.add(val(i));\n    }\n    dfs(right(i)!, order, res);\n    // \u540e\u5e8f\u904d\u5386\n    if (order == 'post') {\n      res.add(val(i));\n    }\n  }\n\n  /* \u524d\u5e8f\u904d\u5386 */\n  List<int?> preOrder() {\n    List<int?> res = [];\n    dfs(0, 'pre', res);\n    return res;\n  }\n\n  /* \u4e2d\u5e8f\u904d\u5386 */\n  List<int?> inOrder() {\n    List<int?> res = [];\n    dfs(0, 'in', res);\n    return res;\n  }\n\n  /* \u540e\u5e8f\u904d\u5386 */\n  List<int?> postOrder() {\n    List<int?> res = [];\n    dfs(0, 'post', res);\n    return res;\n  }\n}\n
        array_binary_tree.rs
        /* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nstruct ArrayBinaryTree {\n    tree: Vec<Option<i32>>,\n}\n\nimpl ArrayBinaryTree {\n    /* \u6784\u9020\u65b9\u6cd5 */\n    fn new(arr: Vec<Option<i32>>) -> Self {\n        Self { tree: arr }\n    }\n\n    /* \u5217\u8868\u5bb9\u91cf */\n    fn size(&self) -> i32 {\n        self.tree.len() as i32\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n    fn val(&self, i: i32) -> Option<i32> {\n        // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de None \uff0c\u4ee3\u8868\u7a7a\u4f4d\n        if i < 0 || i >= self.size() {\n            None\n        } else {\n            self.tree[i as usize]\n        }\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    fn left(&self, i: i32) -> i32 {\n        2 * i + 1\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    fn right(&self, i: i32) -> i32 {\n        2 * i + 2\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n    fn parent(&self, i: i32) -> i32 {\n        (i - 1) / 2\n    }\n\n    /* \u5c42\u5e8f\u904d\u5386 */\n    fn level_order(&self) -> Vec<i32> {\n        self.tree.iter().filter_map(|&x| x).collect()\n    }\n\n    /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n    fn dfs(&self, i: i32, order: &'static str, res: &mut Vec<i32>) {\n        if self.val(i).is_none() {\n            return;\n        }\n        let val = self.val(i).unwrap();\n        // \u524d\u5e8f\u904d\u5386\n        if order == \"pre\" {\n            res.push(val);\n        }\n        self.dfs(self.left(i), order, res);\n        // \u4e2d\u5e8f\u904d\u5386\n        if order == \"in\" {\n            res.push(val);\n        }\n        self.dfs(self.right(i), order, res);\n        // \u540e\u5e8f\u904d\u5386\n        if order == \"post\" {\n            res.push(val);\n        }\n    }\n\n    /* \u524d\u5e8f\u904d\u5386 */\n    fn pre_order(&self) -> Vec<i32> {\n        let mut res = vec![];\n        self.dfs(0, \"pre\", &mut res);\n        res\n    }\n\n    /* \u4e2d\u5e8f\u904d\u5386 */\n    fn in_order(&self) -> Vec<i32> {\n        let mut res = vec![];\n        self.dfs(0, \"in\", &mut res);\n        res\n    }\n\n    /* \u540e\u5e8f\u904d\u5386 */\n    fn post_order(&self) -> Vec<i32> {\n        let mut res = vec![];\n        self.dfs(0, \"post\", &mut res);\n        res\n    }\n}\n
        array_binary_tree.c
        /* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7ed3\u6784\u4f53 */\ntypedef struct {\n    int *tree;\n    int size;\n} ArrayBinaryTree;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayBinaryTree *newArrayBinaryTree(int *arr, int arrSize) {\n    ArrayBinaryTree *abt = (ArrayBinaryTree *)malloc(sizeof(ArrayBinaryTree));\n    abt->tree = malloc(sizeof(int) * arrSize);\n    memcpy(abt->tree, arr, sizeof(int) * arrSize);\n    abt->size = arrSize;\n    return abt;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayBinaryTree(ArrayBinaryTree *abt) {\n    free(abt->tree);\n    free(abt);\n}\n\n/* \u5217\u8868\u5bb9\u91cf */\nint size(ArrayBinaryTree *abt) {\n    return abt->size;\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nint val(ArrayBinaryTree *abt, int i) {\n    // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de INT_MAX \uff0c\u4ee3\u8868\u7a7a\u4f4d\n    if (i < 0 || i >= size(abt))\n        return INT_MAX;\n    return abt->tree[i];\n}\n\n/* \u5c42\u5e8f\u904d\u5386 */\nint *levelOrder(ArrayBinaryTree *abt, int *returnSize) {\n    int *res = (int *)malloc(sizeof(int) * size(abt));\n    int index = 0;\n    // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n    for (int i = 0; i < size(abt); i++) {\n        if (val(abt, i) != INT_MAX)\n            res[index++] = val(abt, i);\n    }\n    *returnSize = index;\n    return res;\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nvoid dfs(ArrayBinaryTree *abt, int i, char *order, int *res, int *index) {\n    // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n    if (val(abt, i) == INT_MAX)\n        return;\n    // \u524d\u5e8f\u904d\u5386\n    if (strcmp(order, \"pre\") == 0)\n        res[(*index)++] = val(abt, i);\n    dfs(abt, left(i), order, res, index);\n    // \u4e2d\u5e8f\u904d\u5386\n    if (strcmp(order, \"in\") == 0)\n        res[(*index)++] = val(abt, i);\n    dfs(abt, right(i), order, res, index);\n    // \u540e\u5e8f\u904d\u5386\n    if (strcmp(order, \"post\") == 0)\n        res[(*index)++] = val(abt, i);\n}\n\n/* \u524d\u5e8f\u904d\u5386 */\nint *preOrder(ArrayBinaryTree *abt, int *returnSize) {\n    int *res = (int *)malloc(sizeof(int) * size(abt));\n    int index = 0;\n    dfs(abt, 0, \"pre\", res, &index);\n    *returnSize = index;\n    return res;\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nint *inOrder(ArrayBinaryTree *abt, int *returnSize) {\n    int *res = (int *)malloc(sizeof(int) * size(abt));\n    int index = 0;\n    dfs(abt, 0, \"in\", res, &index);\n    *returnSize = index;\n    return res;\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nint *postOrder(ArrayBinaryTree *abt, int *returnSize) {\n    int *res = (int *)malloc(sizeof(int) * size(abt));\n    int index = 0;\n    dfs(abt, 0, \"post\", res, &index);\n    *returnSize = index;\n    return res;\n}\n
        array_binary_tree.kt
        /* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree(val tree: MutableList<Int?>) {\n    /* \u5217\u8868\u5bb9\u91cf */\n    fun size(): Int {\n        return tree.size\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n    fun _val(i: Int): Int? {\n        // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n        if (i < 0 || i >= size()) return null\n        return tree[i]\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    fun left(i: Int): Int {\n        return 2 * i + 1\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    fun right(i: Int): Int {\n        return 2 * i + 2\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n    fun parent(i: Int): Int {\n        return (i - 1) / 2\n    }\n\n    /* \u5c42\u5e8f\u904d\u5386 */\n    fun levelOrder(): MutableList<Int?> {\n        val res = mutableListOf<Int?>()\n        // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n        for (i in 0..<size()) {\n            if (_val(i) != null)\n                res.add(_val(i))\n        }\n        return res\n    }\n\n    /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n    fun dfs(i: Int, order: String, res: MutableList<Int?>) {\n        // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n        if (_val(i) == null)\n            return\n        // \u524d\u5e8f\u904d\u5386\n        if (\"pre\" == order)\n            res.add(_val(i))\n        dfs(left(i), order, res)\n        // \u4e2d\u5e8f\u904d\u5386\n        if (\"in\" == order)\n            res.add(_val(i))\n        dfs(right(i), order, res)\n        // \u540e\u5e8f\u904d\u5386\n        if (\"post\" == order)\n            res.add(_val(i))\n    }\n\n    /* \u524d\u5e8f\u904d\u5386 */\n    fun preOrder(): MutableList<Int?> {\n        val res = mutableListOf<Int?>()\n        dfs(0, \"pre\", res)\n        return res\n    }\n\n    /* \u4e2d\u5e8f\u904d\u5386 */\n    fun inOrder(): MutableList<Int?> {\n        val res = mutableListOf<Int?>()\n        dfs(0, \"in\", res)\n        return res\n    }\n\n    /* \u540e\u5e8f\u904d\u5386 */\n    fun postOrder(): MutableList<Int?> {\n        val res = mutableListOf<Int?>()\n        dfs(0, \"post\", res)\n        return res\n    }\n}\n
        array_binary_tree.rb
        ### \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b ###\nclass ArrayBinaryTree\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize(arr)\n    @tree = arr.to_a\n  end\n\n  ### \u5217\u8868\u5bb9\u91cf ###\n  def size\n    @tree.length\n  end\n\n  ### \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c ###\n  def val(i)\n    # \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de nil \uff0c\u4ee3\u8868\u7a7a\u4f4d\n    return if i < 0 || i >= size\n\n    @tree[i]\n  end\n\n  ### \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 ###\n  def left(i)\n    2 * i + 1\n  end\n\n  ### \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 ###\n  def right(i)\n    2 * i + 2\n  end\n\n  ### \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 ###\n  def parent(i)\n    (i - 1) / 2\n  end\n\n  ### \u5c42\u5e8f\u904d\u5386 ###\n  def level_order\n    @res = []\n\n    # \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n    for i in 0...size\n      @res << val(i) unless val(i).nil?\n    end\n\n    @res\n  end\n\n  ### \u6df1\u5ea6\u4f18\u5148\u904d\u5386 ###\n  def dfs(i, order)\n    return if val(i).nil?\n    # \u524d\u5e8f\u904d\u5386\n    @res << val(i) if order == :pre\n    dfs(left(i), order)\n    # \u4e2d\u5e8f\u904d\u5386\n    @res << val(i) if order == :in\n    dfs(right(i), order)\n    # \u540e\u5e8f\u904d\u5386\n    @res << val(i) if order == :post\n  end\n\n  ### \u524d\u5e8f\u904d\u5386 ###\n  def pre_order\n    @res = []\n    dfs(0, :pre)\n    @res\n  end\n\n  ### \u4e2d\u5e8f\u904d\u5386 ###\n  def in_order\n    @res = []\n    dfs(0, :in)\n    @res\n  end\n\n  ### \u540e\u5e8f\u904d\u5386 ###\n  def post_order\n    @res = []\n    dfs(0, :post)\n    @res\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_tree/array_representation_of_tree/#733","title":"7.3.3 \u00a0 \u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"

        \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a\u4e3b\u8981\u6709\u4ee5\u4e0b\u4f18\u70b9\u3002

        • \u6570\u7ec4\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\uff0c\u5bf9\u7f13\u5b58\u53cb\u597d\uff0c\u8bbf\u95ee\u4e0e\u904d\u5386\u901f\u5ea6\u8f83\u5feb\u3002
        • \u4e0d\u9700\u8981\u5b58\u50a8\u6307\u9488\uff0c\u6bd4\u8f83\u8282\u7701\u7a7a\u95f4\u3002
        • \u5141\u8bb8\u968f\u673a\u8bbf\u95ee\u8282\u70b9\u3002

        \u7136\u800c\uff0c\u6570\u7ec4\u8868\u793a\u4e5f\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027\u3002

        • \u6570\u7ec4\u5b58\u50a8\u9700\u8981\u8fde\u7eed\u5185\u5b58\u7a7a\u95f4\uff0c\u56e0\u6b64\u4e0d\u9002\u5408\u5b58\u50a8\u6570\u636e\u91cf\u8fc7\u5927\u7684\u6811\u3002
        • \u589e\u5220\u8282\u70b9\u9700\u8981\u901a\u8fc7\u6570\u7ec4\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u5b9e\u73b0\uff0c\u6548\u7387\u8f83\u4f4e\u3002
        • \u5f53\u4e8c\u53c9\u6811\u4e2d\u5b58\u5728\u5927\u91cf None \u65f6\uff0c\u6570\u7ec4\u4e2d\u5305\u542b\u7684\u8282\u70b9\u6570\u636e\u6bd4\u91cd\u8f83\u4f4e\uff0c\u7a7a\u95f4\u5229\u7528\u7387\u8f83\u4f4e\u3002
        "},{"location":"chapter_tree/avl_tree/","title":"7.5 \u00a0 AVL \u6811 *","text":"

        \u5728\u201c\u4e8c\u53c9\u641c\u7d22\u6811\u201d\u7ae0\u8282\u4e2d\u6211\u4eec\u63d0\u5230\uff0c\u5728\u591a\u6b21\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u540e\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u53ef\u80fd\u9000\u5316\u4e3a\u94fe\u8868\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5c06\u4ece \\(O(\\log n)\\) \u52a3\u5316\u4e3a \\(O(n)\\) \u3002

        \u5982\u56fe 7-24 \u6240\u793a\uff0c\u7ecf\u8fc7\u4e24\u6b21\u5220\u9664\u8282\u70b9\u64cd\u4f5c\uff0c\u8fd9\u68f5\u4e8c\u53c9\u641c\u7d22\u6811\u4fbf\u4f1a\u9000\u5316\u4e3a\u94fe\u8868\u3002

        \u56fe 7-24 \u00a0 AVL \u6811\u5728\u5220\u9664\u8282\u70b9\u540e\u53d1\u751f\u9000\u5316

        \u518d\u4f8b\u5982\uff0c\u5728\u56fe 7-25 \u6240\u793a\u7684\u5b8c\u7f8e\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e24\u4e2a\u8282\u70b9\u540e\uff0c\u6811\u5c06\u4e25\u91cd\u5411\u5de6\u503e\u659c\uff0c\u67e5\u627e\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u968f\u4e4b\u52a3\u5316\u3002

        \u56fe 7-25 \u00a0 AVL \u6811\u5728\u63d2\u5165\u8282\u70b9\u540e\u53d1\u751f\u9000\u5316

        1962 \u5e74 G. M. Adelson-Velsky \u548c E. M. Landis \u5728\u8bba\u6587\u201cAn algorithm for the organization of information\u201d\u4e2d\u63d0\u51fa\u4e86 AVL \u6811\u3002\u8bba\u6587\u4e2d\u8be6\u7ec6\u63cf\u8ff0\u4e86\u4e00\u7cfb\u5217\u64cd\u4f5c\uff0c\u786e\u4fdd\u5728\u6301\u7eed\u6dfb\u52a0\u548c\u5220\u9664\u8282\u70b9\u540e\uff0cAVL \u6811\u4e0d\u4f1a\u9000\u5316\uff0c\u4ece\u800c\u4f7f\u5f97\u5404\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4fdd\u6301\u5728 \\(O(\\log n)\\) \u7ea7\u522b\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5728\u9700\u8981\u9891\u7e41\u8fdb\u884c\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u7684\u573a\u666f\u4e2d\uff0cAVL \u6811\u80fd\u59cb\u7ec8\u4fdd\u6301\u9ad8\u6548\u7684\u6570\u636e\u64cd\u4f5c\u6027\u80fd\uff0c\u5177\u6709\u5f88\u597d\u7684\u5e94\u7528\u4ef7\u503c\u3002

        "},{"location":"chapter_tree/avl_tree/#751-avl","title":"7.5.1 \u00a0 AVL \u6811\u5e38\u89c1\u672f\u8bed","text":"

        AVL \u6811\u65e2\u662f\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u4e5f\u662f\u5e73\u8861\u4e8c\u53c9\u6811\uff0c\u540c\u65f6\u6ee1\u8db3\u8fd9\u4e24\u7c7b\u4e8c\u53c9\u6811\u7684\u6240\u6709\u6027\u8d28\uff0c\u56e0\u6b64\u662f\u4e00\u79cd\u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811\uff08balanced binary search tree\uff09\u3002

        "},{"location":"chapter_tree/avl_tree/#1","title":"1. \u00a0 \u8282\u70b9\u9ad8\u5ea6","text":"

        \u7531\u4e8e AVL \u6811\u7684\u76f8\u5173\u64cd\u4f5c\u9700\u8981\u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u4e3a\u8282\u70b9\u7c7b\u6dfb\u52a0 height \u53d8\u91cf\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        class TreeNode:\n    \"\"\"AVL \u6811\u8282\u70b9\u7c7b\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val                 # \u8282\u70b9\u503c\n        self.height: int = 0                # \u8282\u70b9\u9ad8\u5ea6\n        self.left: TreeNode | None = None   # \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n        self.right: TreeNode | None = None  # \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n
        /* AVL \u6811\u8282\u70b9\u7c7b */\nstruct TreeNode {\n    int val{};          // \u8282\u70b9\u503c\n    int height = 0;     // \u8282\u70b9\u9ad8\u5ea6\n    TreeNode *left{};   // \u5de6\u5b50\u8282\u70b9\n    TreeNode *right{};  // \u53f3\u5b50\u8282\u70b9\n    TreeNode() = default;\n    explicit TreeNode(int x) : val(x){}\n};\n
        /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n    public int val;        // \u8282\u70b9\u503c\n    public int height;     // \u8282\u70b9\u9ad8\u5ea6\n    public TreeNode left;  // \u5de6\u5b50\u8282\u70b9\n    public TreeNode right; // \u53f3\u5b50\u8282\u70b9\n    public TreeNode(int x) { val = x; }\n}\n
        /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode(int? x) {\n    public int? val = x;    // \u8282\u70b9\u503c\n    public int height;      // \u8282\u70b9\u9ad8\u5ea6\n    public TreeNode? left;  // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n    public TreeNode? right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n
        /* AVL \u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype TreeNode struct {\n    Val    int       // \u8282\u70b9\u503c\n    Height int       // \u8282\u70b9\u9ad8\u5ea6\n    Left   *TreeNode // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n    Right  *TreeNode // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n
        /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n    var val: Int // \u8282\u70b9\u503c\n    var height: Int // \u8282\u70b9\u9ad8\u5ea6\n    var left: TreeNode? // \u5de6\u5b50\u8282\u70b9\n    var right: TreeNode? // \u53f3\u5b50\u8282\u70b9\n\n    init(x: Int) {\n        val = x\n        height = 0\n    }\n}\n
        /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n    val; // \u8282\u70b9\u503c\n    height; //\u8282\u70b9\u9ad8\u5ea6\n    left; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n    right; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n    constructor(val, left, right, height) {\n        this.val = val === undefined ? 0 : val;\n        this.height = height === undefined ? 0 : height;\n        this.left = left === undefined ? null : left;\n        this.right = right === undefined ? null : right;\n    }\n}\n
        /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n    val: number;            // \u8282\u70b9\u503c\n    height: number;         // \u8282\u70b9\u9ad8\u5ea6\n    left: TreeNode | null;  // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n    right: TreeNode | null; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n    constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {\n        this.val = val === undefined ? 0 : val;\n        this.height = height === undefined ? 0 : height;\n        this.left = left === undefined ? null : left;\n        this.right = right === undefined ? null : right;\n    }\n}\n
        /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n  int val;         // \u8282\u70b9\u503c\n  int height;      // \u8282\u70b9\u9ad8\u5ea6\n  TreeNode? left;  // \u5de6\u5b50\u8282\u70b9\n  TreeNode? right; // \u53f3\u5b50\u8282\u70b9\n  TreeNode(this.val, [this.height = 0, this.left, this.right]);\n}\n
        use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* AVL \u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\n    val: i32,                               // \u8282\u70b9\u503c\n    height: i32,                            // \u8282\u70b9\u9ad8\u5ea6\n    left: Option<Rc<RefCell<TreeNode>>>,    // \u5de6\u5b50\u8282\u70b9\n    right: Option<Rc<RefCell<TreeNode>>>,   // \u53f3\u5b50\u8282\u70b9\n}\n\nimpl TreeNode {\n    /* \u6784\u9020\u65b9\u6cd5 */\n    fn new(val: i32) -> Rc<RefCell<Self>> {\n        Rc::new(RefCell::new(Self {\n            val,\n            height: 0,\n            left: None,\n            right: None\n        }))\n    }\n}\n
        /* AVL \u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\ntypedef struct TreeNode {\n    int val;\n    int height;\n    struct TreeNode *left;\n    struct TreeNode *right;\n} TreeNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nTreeNode *newTreeNode(int val) {\n    TreeNode *node;\n\n    node = (TreeNode *)malloc(sizeof(TreeNode));\n    node->val = val;\n    node->height = 0;\n    node->left = NULL;\n    node->right = NULL;\n    return node;\n}\n
        /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode(val _val: Int) {  // \u8282\u70b9\u503c\n    val height: Int = 0          // \u8282\u70b9\u9ad8\u5ea6\n    val left: TreeNode? = null   // \u5de6\u5b50\u8282\u70b9\n    val right: TreeNode? = null  // \u53f3\u5b50\u8282\u70b9\n}\n
        ### AVL \u6811\u8282\u70b9\u7c7b ###\nclass TreeNode\n  attr_accessor :val    # \u8282\u70b9\u503c\n  attr_accessor :height # \u8282\u70b9\u9ad8\u5ea6\n  attr_accessor :left   # \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n  attr_accessor :right  # \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n\n  def initialize(val)\n    @val = val\n    @height = 0\n  end\nend\n

        \u201c\u8282\u70b9\u9ad8\u5ea6\u201d\u662f\u6307\u4ece\u8be5\u8282\u70b9\u5230\u5b83\u7684\u6700\u8fdc\u53f6\u8282\u70b9\u7684\u8ddd\u79bb\uff0c\u5373\u6240\u7ecf\u8fc7\u7684\u201c\u8fb9\u201d\u7684\u6570\u91cf\u3002\u9700\u8981\u7279\u522b\u6ce8\u610f\u7684\u662f\uff0c\u53f6\u8282\u70b9\u7684\u9ad8\u5ea6\u4e3a \\(0\\) \uff0c\u800c\u7a7a\u8282\u70b9\u7684\u9ad8\u5ea6\u4e3a \\(-1\\) \u3002\u6211\u4eec\u5c06\u521b\u5efa\u4e24\u4e2a\u5de5\u5177\u51fd\u6570\uff0c\u5206\u522b\u7528\u4e8e\u83b7\u53d6\u548c\u66f4\u65b0\u8282\u70b9\u7684\u9ad8\u5ea6\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def height(self, node: TreeNode | None) -> int:\n    \"\"\"\u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6\"\"\"\n    # \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n    if node is not None:\n        return node.height\n    return -1\n\ndef update_height(self, node: TreeNode | None):\n    \"\"\"\u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\"\"\"\n    # \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n    node.height = max([self.height(node.left), self.height(node.right)]) + 1\n
        avl_tree.cpp
        /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode *node) {\n    // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n    return node == nullptr ? -1 : node->height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode *node) {\n    // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n    node->height = max(height(node->left), height(node->right)) + 1;\n}\n
        avl_tree.java
        /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode node) {\n    // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n    return node == null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode node) {\n    // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n    node.height = Math.max(height(node.left), height(node.right)) + 1;\n}\n
        avl_tree.cs
        /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint Height(TreeNode? node) {\n    // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n    return node == null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid UpdateHeight(TreeNode node) {\n    // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n    node.height = Math.Max(Height(node.left), Height(node.right)) + 1;\n}\n
        avl_tree.go
        /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfunc (t *aVLTree) height(node *TreeNode) int {\n    // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n    if node != nil {\n        return node.Height\n    }\n    return -1\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfunc (t *aVLTree) updateHeight(node *TreeNode) {\n    lh := t.height(node.Left)\n    rh := t.height(node.Right)\n    // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n    if lh > rh {\n        node.Height = lh + 1\n    } else {\n        node.Height = rh + 1\n    }\n}\n
        avl_tree.swift
        /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfunc height(node: TreeNode?) -> Int {\n    // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n    node?.height ?? -1\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfunc updateHeight(node: TreeNode?) {\n    // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n    node?.height = max(height(node: node?.left), height(node: node?.right)) + 1\n}\n
        avl_tree.js
        /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nheight(node) {\n    // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n    return node === null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\n#updateHeight(node) {\n    // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n    node.height =\n        Math.max(this.height(node.left), this.height(node.right)) + 1;\n}\n
        avl_tree.ts
        /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nheight(node: TreeNode): number {\n    // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n    return node === null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nupdateHeight(node: TreeNode): void {\n    // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n    node.height =\n        Math.max(this.height(node.left), this.height(node.right)) + 1;\n}\n
        avl_tree.dart
        /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode? node) {\n  // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n  return node == null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode? node) {\n  // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n  node!.height = max(height(node.left), height(node.right)) + 1;\n}\n
        avl_tree.rs
        /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfn height(node: OptionTreeNodeRc) -> i32 {\n    // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n    match node {\n        Some(node) => node.borrow().height,\n        None => -1,\n    }\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfn update_height(node: OptionTreeNodeRc) {\n    if let Some(node) = node {\n        let left = node.borrow().left.clone();\n        let right = node.borrow().right.clone();\n        // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n        node.borrow_mut().height = std::cmp::max(Self::height(left), Self::height(right)) + 1;\n    }\n}\n
        avl_tree.c
        /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode *node) {\n    // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n    if (node != NULL) {\n        return node->height;\n    }\n    return -1;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode *node) {\n    int lh = height(node->left);\n    int rh = height(node->right);\n    // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n    if (lh > rh) {\n        node->height = lh + 1;\n    } else {\n        node->height = rh + 1;\n    }\n}\n
        avl_tree.kt
        /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfun height(node: TreeNode?): Int {\n    // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n    return node?.height ?: -1\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfun updateHeight(node: TreeNode?) {\n    // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n    node?.height = max(height(node?.left), height(node?.right)) + 1\n}\n
        avl_tree.rb
        ### \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 ###\ndef height(node)\n  # \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n  return node.height unless node.nil?\n\n  -1\nend\n\n### \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 ###\ndef update_height(node)\n  # \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n  node.height = [height(node.left), height(node.right)].max + 1\nend\n
        "},{"location":"chapter_tree/avl_tree/#2","title":"2. \u00a0 \u8282\u70b9\u5e73\u8861\u56e0\u5b50","text":"

        \u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\uff08balance factor\uff09\u5b9a\u4e49\u4e3a\u8282\u70b9\u5de6\u5b50\u6811\u7684\u9ad8\u5ea6\u51cf\u53bb\u53f3\u5b50\u6811\u7684\u9ad8\u5ea6\uff0c\u540c\u65f6\u89c4\u5b9a\u7a7a\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u4e3a \\(0\\) \u3002\u6211\u4eec\u540c\u6837\u5c06\u83b7\u53d6\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u7684\u529f\u80fd\u5c01\u88c5\u6210\u51fd\u6570\uff0c\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def balance_factor(self, node: TreeNode | None) -> int:\n    \"\"\"\u83b7\u53d6\u5e73\u8861\u56e0\u5b50\"\"\"\n    # \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n    if node is None:\n        return 0\n    # \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n    return self.height(node.left) - self.height(node.right)\n
        avl_tree.cpp
        /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode *node) {\n    // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n    if (node == nullptr)\n        return 0;\n    // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n    return height(node->left) - height(node->right);\n}\n
        avl_tree.java
        /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode node) {\n    // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n    if (node == null)\n        return 0;\n    // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n    return height(node.left) - height(node.right);\n}\n
        avl_tree.cs
        /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint BalanceFactor(TreeNode? node) {\n    // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n    if (node == null) return 0;\n    // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n    return Height(node.left) - Height(node.right);\n}\n
        avl_tree.go
        /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfunc (t *aVLTree) balanceFactor(node *TreeNode) int {\n    // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n    if node == nil {\n        return 0\n    }\n    // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n    return t.height(node.Left) - t.height(node.Right)\n}\n
        avl_tree.swift
        /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfunc balanceFactor(node: TreeNode?) -> Int {\n    // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n    guard let node = node else { return 0 }\n    // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n    return height(node: node.left) - height(node: node.right)\n}\n
        avl_tree.js
        /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nbalanceFactor(node) {\n    // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n    if (node === null) return 0;\n    // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n    return this.height(node.left) - this.height(node.right);\n}\n
        avl_tree.ts
        /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nbalanceFactor(node: TreeNode): number {\n    // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n    if (node === null) return 0;\n    // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n    return this.height(node.left) - this.height(node.right);\n}\n
        avl_tree.dart
        /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode? node) {\n  // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n  if (node == null) return 0;\n  // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n  return height(node.left) - height(node.right);\n}\n
        avl_tree.rs
        /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfn balance_factor(node: OptionTreeNodeRc) -> i32 {\n    match node {\n        // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n        None => 0,\n        // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n        Some(node) => {\n            Self::height(node.borrow().left.clone()) - Self::height(node.borrow().right.clone())\n        }\n    }\n}\n
        avl_tree.c
        /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode *node) {\n    // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n    if (node == NULL) {\n        return 0;\n    }\n    // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n    return height(node->left) - height(node->right);\n}\n
        avl_tree.kt
        /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfun balanceFactor(node: TreeNode?): Int {\n    // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n    if (node == null) return 0\n    // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n    return height(node.left) - height(node.right)\n}\n
        avl_tree.rb
        ### \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 ###\ndef balance_factor(node)\n  # \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n  return 0 if node.nil?\n\n  # \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n  height(node.left) - height(node.right)\nend\n

        Tip

        \u8bbe\u5e73\u8861\u56e0\u5b50\u4e3a \\(f\\) \uff0c\u5219\u4e00\u68f5 AVL \u6811\u7684\u4efb\u610f\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u7686\u6ee1\u8db3 \\(-1 \\le f \\le 1\\) \u3002

        "},{"location":"chapter_tree/avl_tree/#752-avl","title":"7.5.2 \u00a0 AVL \u6811\u65cb\u8f6c","text":"

        AVL \u6811\u7684\u7279\u70b9\u5728\u4e8e\u201c\u65cb\u8f6c\u201d\u64cd\u4f5c\uff0c\u5b83\u80fd\u591f\u5728\u4e0d\u5f71\u54cd\u4e8c\u53c9\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u7684\u524d\u63d0\u4e0b\uff0c\u4f7f\u5931\u8861\u8282\u70b9\u91cd\u65b0\u6062\u590d\u5e73\u8861\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u65cb\u8f6c\u64cd\u4f5c\u65e2\u80fd\u4fdd\u6301\u201c\u4e8c\u53c9\u641c\u7d22\u6811\u201d\u7684\u6027\u8d28\uff0c\u4e5f\u80fd\u4f7f\u6811\u91cd\u65b0\u53d8\u4e3a\u201c\u5e73\u8861\u4e8c\u53c9\u6811\u201d\u3002

        \u6211\u4eec\u5c06\u5e73\u8861\u56e0\u5b50\u7edd\u5bf9\u503c \\(> 1\\) \u7684\u8282\u70b9\u79f0\u4e3a\u201c\u5931\u8861\u8282\u70b9\u201d\u3002\u6839\u636e\u8282\u70b9\u5931\u8861\u60c5\u51b5\u7684\u4e0d\u540c\uff0c\u65cb\u8f6c\u64cd\u4f5c\u5206\u4e3a\u56db\u79cd\uff1a\u53f3\u65cb\u3001\u5de6\u65cb\u3001\u5148\u53f3\u65cb\u540e\u5de6\u65cb\u3001\u5148\u5de6\u65cb\u540e\u53f3\u65cb\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65cb\u8f6c\u64cd\u4f5c\u3002

        "},{"location":"chapter_tree/avl_tree/#1_1","title":"1. \u00a0 \u53f3\u65cb","text":"

        \u5982\u56fe 7-26 \u6240\u793a\uff0c\u8282\u70b9\u4e0b\u65b9\u4e3a\u5e73\u8861\u56e0\u5b50\u3002\u4ece\u5e95\u81f3\u9876\u770b\uff0c\u4e8c\u53c9\u6811\u4e2d\u9996\u4e2a\u5931\u8861\u8282\u70b9\u662f\u201c\u8282\u70b9 3\u201d\u3002\u6211\u4eec\u5173\u6ce8\u4ee5\u8be5\u5931\u8861\u8282\u70b9\u4e3a\u6839\u8282\u70b9\u7684\u5b50\u6811\uff0c\u5c06\u8be5\u8282\u70b9\u8bb0\u4e3a node \uff0c\u5176\u5de6\u5b50\u8282\u70b9\u8bb0\u4e3a child \uff0c\u6267\u884c\u201c\u53f3\u65cb\u201d\u64cd\u4f5c\u3002\u5b8c\u6210\u53f3\u65cb\u540e\uff0c\u5b50\u6811\u6062\u590d\u5e73\u8861\uff0c\u5e76\u4e14\u4ecd\u7136\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6027\u8d28\u3002

        <1><2><3><4>

        \u56fe 7-26 \u00a0 \u53f3\u65cb\u64cd\u4f5c\u6b65\u9aa4

        \u5982\u56fe 7-27 \u6240\u793a\uff0c\u5f53\u8282\u70b9 child \u6709\u53f3\u5b50\u8282\u70b9\uff08\u8bb0\u4e3a grand_child \uff09\u65f6\uff0c\u9700\u8981\u5728\u53f3\u65cb\u4e2d\u6dfb\u52a0\u4e00\u6b65\uff1a\u5c06 grand_child \u4f5c\u4e3a node \u7684\u5de6\u5b50\u8282\u70b9\u3002

        \u56fe 7-27 \u00a0 \u6709 grand_child \u7684\u53f3\u65cb\u64cd\u4f5c

        \u201c\u5411\u53f3\u65cb\u8f6c\u201d\u662f\u4e00\u79cd\u5f62\u8c61\u5316\u7684\u8bf4\u6cd5\uff0c\u5b9e\u9645\u4e0a\u9700\u8981\u901a\u8fc7\u4fee\u6539\u8282\u70b9\u6307\u9488\u6765\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def right_rotate(self, node: TreeNode | None) -> TreeNode | None:\n    \"\"\"\u53f3\u65cb\u64cd\u4f5c\"\"\"\n    child = node.left\n    grand_child = child.right\n    # \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n    child.right = node\n    node.left = grand_child\n    # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    self.update_height(node)\n    self.update_height(child)\n    # \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child\n
        avl_tree.cpp
        /* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode *rightRotate(TreeNode *node) {\n    TreeNode *child = node->left;\n    TreeNode *grandChild = child->right;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n    child->right = node;\n    node->left = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node);\n    updateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.java
        /* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode rightRotate(TreeNode node) {\n    TreeNode child = node.left;\n    TreeNode grandChild = child.right;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n    child.right = node;\n    node.left = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node);\n    updateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.cs
        /* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode? RightRotate(TreeNode? node) {\n    TreeNode? child = node?.left;\n    TreeNode? grandChild = child?.right;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n    child.right = node;\n    node.left = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    UpdateHeight(node);\n    UpdateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.go
        /* \u53f3\u65cb\u64cd\u4f5c */\nfunc (t *aVLTree) rightRotate(node *TreeNode) *TreeNode {\n    child := node.Left\n    grandChild := child.Right\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n    child.Right = node\n    node.Left = grandChild\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    t.updateHeight(node)\n    t.updateHeight(child)\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child\n}\n
        avl_tree.swift
        /* \u53f3\u65cb\u64cd\u4f5c */\nfunc rightRotate(node: TreeNode?) -> TreeNode? {\n    let child = node?.left\n    let grandChild = child?.right\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n    child?.right = node\n    node?.left = grandChild\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node: node)\n    updateHeight(node: child)\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child\n}\n
        avl_tree.js
        /* \u53f3\u65cb\u64cd\u4f5c */\n#rightRotate(node) {\n    const child = node.left;\n    const grandChild = child.right;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n    child.right = node;\n    node.left = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    this.#updateHeight(node);\n    this.#updateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.ts
        /* \u53f3\u65cb\u64cd\u4f5c */\nrightRotate(node: TreeNode): TreeNode {\n    const child = node.left;\n    const grandChild = child.right;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n    child.right = node;\n    node.left = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    this.updateHeight(node);\n    this.updateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.dart
        /* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode? rightRotate(TreeNode? node) {\n  TreeNode? child = node!.left;\n  TreeNode? grandChild = child!.right;\n  // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n  child.right = node;\n  node.left = grandChild;\n  // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n  updateHeight(node);\n  updateHeight(child);\n  // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n  return child;\n}\n
        avl_tree.rs
        /* \u53f3\u65cb\u64cd\u4f5c */\nfn right_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n    match node {\n        Some(node) => {\n            let child = node.borrow().left.clone().unwrap();\n            let grand_child = child.borrow().right.clone();\n            // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n            child.borrow_mut().right = Some(node.clone());\n            node.borrow_mut().left = grand_child;\n            // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n            Self::update_height(Some(node));\n            Self::update_height(Some(child.clone()));\n            // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n            Some(child)\n        }\n        None => None,\n    }\n}\n
        avl_tree.c
        /* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode *rightRotate(TreeNode *node) {\n    TreeNode *child, *grandChild;\n    child = node->left;\n    grandChild = child->right;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n    child->right = node;\n    node->left = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node);\n    updateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.kt
        /* \u53f3\u65cb\u64cd\u4f5c */\nfun rightRotate(node: TreeNode?): TreeNode {\n    val child = node!!.left\n    val grandChild = child!!.right\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n    child.right = node\n    node.left = grandChild\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node)\n    updateHeight(child)\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child\n}\n
        avl_tree.rb
        ### \u53f3\u65cb\u64cd\u4f5c ###\ndef right_rotate(node)\n  child = node.left\n  grand_child = child.right\n  # \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n  child.right = node\n  node.left = grand_child\n  # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n  update_height(node)\n  update_height(child)\n  # \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n  child\nend\n
        "},{"location":"chapter_tree/avl_tree/#2_1","title":"2. \u00a0 \u5de6\u65cb","text":"

        \u76f8\u5e94\u5730\uff0c\u5982\u679c\u8003\u8651\u4e0a\u8ff0\u5931\u8861\u4e8c\u53c9\u6811\u7684\u201c\u955c\u50cf\u201d\uff0c\u5219\u9700\u8981\u6267\u884c\u56fe 7-28 \u6240\u793a\u7684\u201c\u5de6\u65cb\u201d\u64cd\u4f5c\u3002

        \u56fe 7-28 \u00a0 \u5de6\u65cb\u64cd\u4f5c

        \u540c\u7406\uff0c\u5982\u56fe 7-29 \u6240\u793a\uff0c\u5f53\u8282\u70b9 child \u6709\u5de6\u5b50\u8282\u70b9\uff08\u8bb0\u4e3a grand_child \uff09\u65f6\uff0c\u9700\u8981\u5728\u5de6\u65cb\u4e2d\u6dfb\u52a0\u4e00\u6b65\uff1a\u5c06 grand_child \u4f5c\u4e3a node \u7684\u53f3\u5b50\u8282\u70b9\u3002

        \u56fe 7-29 \u00a0 \u6709 grand_child \u7684\u5de6\u65cb\u64cd\u4f5c

        \u53ef\u4ee5\u89c2\u5bdf\u5230\uff0c\u53f3\u65cb\u548c\u5de6\u65cb\u64cd\u4f5c\u5728\u903b\u8f91\u4e0a\u662f\u955c\u50cf\u5bf9\u79f0\u7684\uff0c\u5b83\u4eec\u5206\u522b\u89e3\u51b3\u7684\u4e24\u79cd\u5931\u8861\u60c5\u51b5\u4e5f\u662f\u5bf9\u79f0\u7684\u3002\u57fa\u4e8e\u5bf9\u79f0\u6027\uff0c\u6211\u4eec\u53ea\u9700\u5c06\u53f3\u65cb\u7684\u5b9e\u73b0\u4ee3\u7801\u4e2d\u7684\u6240\u6709\u7684 left \u66ff\u6362\u4e3a right \uff0c\u5c06\u6240\u6709\u7684 right \u66ff\u6362\u4e3a left \uff0c\u5373\u53ef\u5f97\u5230\u5de6\u65cb\u7684\u5b9e\u73b0\u4ee3\u7801\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def left_rotate(self, node: TreeNode | None) -> TreeNode | None:\n    \"\"\"\u5de6\u65cb\u64cd\u4f5c\"\"\"\n    child = node.right\n    grand_child = child.left\n    # \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n    child.left = node\n    node.right = grand_child\n    # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    self.update_height(node)\n    self.update_height(child)\n    # \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child\n
        avl_tree.cpp
        /* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode *leftRotate(TreeNode *node) {\n    TreeNode *child = node->right;\n    TreeNode *grandChild = child->left;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n    child->left = node;\n    node->right = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node);\n    updateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.java
        /* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode leftRotate(TreeNode node) {\n    TreeNode child = node.right;\n    TreeNode grandChild = child.left;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n    child.left = node;\n    node.right = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node);\n    updateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.cs
        /* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode? LeftRotate(TreeNode? node) {\n    TreeNode? child = node?.right;\n    TreeNode? grandChild = child?.left;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n    child.left = node;\n    node.right = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    UpdateHeight(node);\n    UpdateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.go
        /* \u5de6\u65cb\u64cd\u4f5c */\nfunc (t *aVLTree) leftRotate(node *TreeNode) *TreeNode {\n    child := node.Right\n    grandChild := child.Left\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n    child.Left = node\n    node.Right = grandChild\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    t.updateHeight(node)\n    t.updateHeight(child)\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child\n}\n
        avl_tree.swift
        /* \u5de6\u65cb\u64cd\u4f5c */\nfunc leftRotate(node: TreeNode?) -> TreeNode? {\n    let child = node?.right\n    let grandChild = child?.left\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n    child?.left = node\n    node?.right = grandChild\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node: node)\n    updateHeight(node: child)\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child\n}\n
        avl_tree.js
        /* \u5de6\u65cb\u64cd\u4f5c */\n#leftRotate(node) {\n    const child = node.right;\n    const grandChild = child.left;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n    child.left = node;\n    node.right = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    this.#updateHeight(node);\n    this.#updateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.ts
        /* \u5de6\u65cb\u64cd\u4f5c */\nleftRotate(node: TreeNode): TreeNode {\n    const child = node.right;\n    const grandChild = child.left;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n    child.left = node;\n    node.right = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    this.updateHeight(node);\n    this.updateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.dart
        /* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode? leftRotate(TreeNode? node) {\n  TreeNode? child = node!.right;\n  TreeNode? grandChild = child!.left;\n  // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n  child.left = node;\n  node.right = grandChild;\n  // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n  updateHeight(node);\n  updateHeight(child);\n  // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n  return child;\n}\n
        avl_tree.rs
        /* \u5de6\u65cb\u64cd\u4f5c */\nfn left_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n    match node {\n        Some(node) => {\n            let child = node.borrow().right.clone().unwrap();\n            let grand_child = child.borrow().left.clone();\n            // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n            child.borrow_mut().left = Some(node.clone());\n            node.borrow_mut().right = grand_child;\n            // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n            Self::update_height(Some(node));\n            Self::update_height(Some(child.clone()));\n            // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n            Some(child)\n        }\n        None => None,\n    }\n}\n
        avl_tree.c
        /* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode *leftRotate(TreeNode *node) {\n    TreeNode *child, *grandChild;\n    child = node->right;\n    grandChild = child->left;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n    child->left = node;\n    node->right = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node);\n    updateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.kt
        /* \u5de6\u65cb\u64cd\u4f5c */\nfun leftRotate(node: TreeNode?): TreeNode {\n    val child = node!!.right\n    val grandChild = child!!.left\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n    child.left = node\n    node.right = grandChild\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node)\n    updateHeight(child)\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child\n}\n
        avl_tree.rb
        ### \u5de6\u65cb\u64cd\u4f5c ###\ndef left_rotate(node)\n  child = node.right\n  grand_child = child.left\n  # \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n  child.left = node\n  node.right = grand_child\n  # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n  update_height(node)\n  update_height(child)\n  # \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n  child\nend\n
        "},{"location":"chapter_tree/avl_tree/#3","title":"3. \u00a0 \u5148\u5de6\u65cb\u540e\u53f3\u65cb","text":"

        \u5bf9\u4e8e\u56fe 7-30 \u4e2d\u7684\u5931\u8861\u8282\u70b9 3 \uff0c\u4ec5\u4f7f\u7528\u5de6\u65cb\u6216\u53f3\u65cb\u90fd\u65e0\u6cd5\u4f7f\u5b50\u6811\u6062\u590d\u5e73\u8861\u3002\u6b64\u65f6\u9700\u8981\u5148\u5bf9 child \u6267\u884c\u201c\u5de6\u65cb\u201d\uff0c\u518d\u5bf9 node \u6267\u884c\u201c\u53f3\u65cb\u201d\u3002

        \u56fe 7-30 \u00a0 \u5148\u5de6\u65cb\u540e\u53f3\u65cb

        "},{"location":"chapter_tree/avl_tree/#4","title":"4. \u00a0 \u5148\u53f3\u65cb\u540e\u5de6\u65cb","text":"

        \u5982\u56fe 7-31 \u6240\u793a\uff0c\u5bf9\u4e8e\u4e0a\u8ff0\u5931\u8861\u4e8c\u53c9\u6811\u7684\u955c\u50cf\u60c5\u51b5\uff0c\u9700\u8981\u5148\u5bf9 child \u6267\u884c\u201c\u53f3\u65cb\u201d\uff0c\u518d\u5bf9 node \u6267\u884c\u201c\u5de6\u65cb\u201d\u3002

        \u56fe 7-31 \u00a0 \u5148\u53f3\u65cb\u540e\u5de6\u65cb

        "},{"location":"chapter_tree/avl_tree/#5","title":"5. \u00a0 \u65cb\u8f6c\u7684\u9009\u62e9","text":"

        \u56fe 7-32 \u5c55\u793a\u7684\u56db\u79cd\u5931\u8861\u60c5\u51b5\u4e0e\u4e0a\u8ff0\u6848\u4f8b\u9010\u4e2a\u5bf9\u5e94\uff0c\u5206\u522b\u9700\u8981\u91c7\u7528\u53f3\u65cb\u3001\u5148\u5de6\u65cb\u540e\u53f3\u65cb\u3001\u5148\u53f3\u65cb\u540e\u5de6\u65cb\u3001\u5de6\u65cb\u7684\u64cd\u4f5c\u3002

        \u56fe 7-32 \u00a0 AVL \u6811\u7684\u56db\u79cd\u65cb\u8f6c\u60c5\u51b5

        \u5982\u4e0b\u8868\u6240\u793a\uff0c\u6211\u4eec\u901a\u8fc7\u5224\u65ad\u5931\u8861\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u4ee5\u53ca\u8f83\u9ad8\u4e00\u4fa7\u5b50\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u7684\u6b63\u8d1f\u53f7\uff0c\u6765\u786e\u5b9a\u5931\u8861\u8282\u70b9\u5c5e\u4e8e\u56fe 7-32 \u4e2d\u7684\u54ea\u79cd\u60c5\u51b5\u3002

        \u8868 7-3 \u00a0 \u56db\u79cd\u65cb\u8f6c\u60c5\u51b5\u7684\u9009\u62e9\u6761\u4ef6

        \u5931\u8861\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50 \u5b50\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50 \u5e94\u91c7\u7528\u7684\u65cb\u8f6c\u65b9\u6cd5 \\(> 1\\) \uff08\u5de6\u504f\u6811\uff09 \\(\\geq 0\\) \u53f3\u65cb \\(> 1\\) \uff08\u5de6\u504f\u6811\uff09 \\(<0\\) \u5148\u5de6\u65cb\u540e\u53f3\u65cb \\(< -1\\) \uff08\u53f3\u504f\u6811\uff09 \\(\\leq 0\\) \u5de6\u65cb \\(< -1\\) \uff08\u53f3\u504f\u6811\uff09 \\(>0\\) \u5148\u53f3\u65cb\u540e\u5de6\u65cb

        \u4e3a\u4e86\u4fbf\u4e8e\u4f7f\u7528\uff0c\u6211\u4eec\u5c06\u65cb\u8f6c\u64cd\u4f5c\u5c01\u88c5\u6210\u4e00\u4e2a\u51fd\u6570\u3002\u6709\u4e86\u8fd9\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u5c31\u80fd\u5bf9\u5404\u79cd\u5931\u8861\u60c5\u51b5\u8fdb\u884c\u65cb\u8f6c\uff0c\u4f7f\u5931\u8861\u8282\u70b9\u91cd\u65b0\u6062\u590d\u5e73\u8861\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def rotate(self, node: TreeNode | None) -> TreeNode | None:\n    \"\"\"\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\"\"\"\n    # \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n    balance_factor = self.balance_factor(node)\n    # \u5de6\u504f\u6811\n    if balance_factor > 1:\n        if self.balance_factor(node.left) >= 0:\n            # \u53f3\u65cb\n            return self.right_rotate(node)\n        else:\n            # \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n            node.left = self.left_rotate(node.left)\n            return self.right_rotate(node)\n    # \u53f3\u504f\u6811\n    elif balance_factor < -1:\n        if self.balance_factor(node.right) <= 0:\n            # \u5de6\u65cb\n            return self.left_rotate(node)\n        else:\n            # \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n            node.right = self.right_rotate(node.right)\n            return self.left_rotate(node)\n    # \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n    return node\n
        avl_tree.cpp
        /* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode *rotate(TreeNode *node) {\n    // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n    int _balanceFactor = balanceFactor(node);\n    // \u5de6\u504f\u6811\n    if (_balanceFactor > 1) {\n        if (balanceFactor(node->left) >= 0) {\n            // \u53f3\u65cb\n            return rightRotate(node);\n        } else {\n            // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n            node->left = leftRotate(node->left);\n            return rightRotate(node);\n        }\n    }\n    // \u53f3\u504f\u6811\n    if (_balanceFactor < -1) {\n        if (balanceFactor(node->right) <= 0) {\n            // \u5de6\u65cb\n            return leftRotate(node);\n        } else {\n            // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n            node->right = rightRotate(node->right);\n            return leftRotate(node);\n        }\n    }\n    // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n    return node;\n}\n
        avl_tree.java
        /* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode rotate(TreeNode node) {\n    // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n    int balanceFactor = balanceFactor(node);\n    // \u5de6\u504f\u6811\n    if (balanceFactor > 1) {\n        if (balanceFactor(node.left) >= 0) {\n            // \u53f3\u65cb\n            return rightRotate(node);\n        } else {\n            // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n            node.left = leftRotate(node.left);\n            return rightRotate(node);\n        }\n    }\n    // \u53f3\u504f\u6811\n    if (balanceFactor < -1) {\n        if (balanceFactor(node.right) <= 0) {\n            // \u5de6\u65cb\n            return leftRotate(node);\n        } else {\n            // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n            node.right = rightRotate(node.right);\n            return leftRotate(node);\n        }\n    }\n    // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n    return node;\n}\n
        avl_tree.cs
        /* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode? Rotate(TreeNode? node) {\n    // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n    int balanceFactorInt = BalanceFactor(node);\n    // \u5de6\u504f\u6811\n    if (balanceFactorInt > 1) {\n        if (BalanceFactor(node?.left) >= 0) {\n            // \u53f3\u65cb\n            return RightRotate(node);\n        } else {\n            // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n            node!.left = LeftRotate(node!.left);\n            return RightRotate(node);\n        }\n    }\n    // \u53f3\u504f\u6811\n    if (balanceFactorInt < -1) {\n        if (BalanceFactor(node?.right) <= 0) {\n            // \u5de6\u65cb\n            return LeftRotate(node);\n        } else {\n            // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n            node!.right = RightRotate(node!.right);\n            return LeftRotate(node);\n        }\n    }\n    // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n    return node;\n}\n
        avl_tree.go
        /* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfunc (t *aVLTree) rotate(node *TreeNode) *TreeNode {\n    // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n    // Go \u63a8\u8350\u77ed\u53d8\u91cf\uff0c\u8fd9\u91cc bf \u6307\u4ee3 t.balanceFactor\n    bf := t.balanceFactor(node)\n    // \u5de6\u504f\u6811\n    if bf > 1 {\n        if t.balanceFactor(node.Left) >= 0 {\n            // \u53f3\u65cb\n            return t.rightRotate(node)\n        } else {\n            // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n            node.Left = t.leftRotate(node.Left)\n            return t.rightRotate(node)\n        }\n    }\n    // \u53f3\u504f\u6811\n    if bf < -1 {\n        if t.balanceFactor(node.Right) <= 0 {\n            // \u5de6\u65cb\n            return t.leftRotate(node)\n        } else {\n            // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n            node.Right = t.rightRotate(node.Right)\n            return t.leftRotate(node)\n        }\n    }\n    // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n    return node\n}\n
        avl_tree.swift
        /* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfunc rotate(node: TreeNode?) -> TreeNode? {\n    // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n    let balanceFactor = balanceFactor(node: node)\n    // \u5de6\u504f\u6811\n    if balanceFactor > 1 {\n        if self.balanceFactor(node: node?.left) >= 0 {\n            // \u53f3\u65cb\n            return rightRotate(node: node)\n        } else {\n            // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n            node?.left = leftRotate(node: node?.left)\n            return rightRotate(node: node)\n        }\n    }\n    // \u53f3\u504f\u6811\n    if balanceFactor < -1 {\n        if self.balanceFactor(node: node?.right) <= 0 {\n            // \u5de6\u65cb\n            return leftRotate(node: node)\n        } else {\n            // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n            node?.right = rightRotate(node: node?.right)\n            return leftRotate(node: node)\n        }\n    }\n    // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n    return node\n}\n
        avl_tree.js
        /* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n#rotate(node) {\n    // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n    const balanceFactor = this.balanceFactor(node);\n    // \u5de6\u504f\u6811\n    if (balanceFactor > 1) {\n        if (this.balanceFactor(node.left) >= 0) {\n            // \u53f3\u65cb\n            return this.#rightRotate(node);\n        } else {\n            // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n            node.left = this.#leftRotate(node.left);\n            return this.#rightRotate(node);\n        }\n    }\n    // \u53f3\u504f\u6811\n    if (balanceFactor < -1) {\n        if (this.balanceFactor(node.right) <= 0) {\n            // \u5de6\u65cb\n            return this.#leftRotate(node);\n        } else {\n            // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n            node.right = this.#rightRotate(node.right);\n            return this.#leftRotate(node);\n        }\n    }\n    // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n    return node;\n}\n
        avl_tree.ts
        /* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nrotate(node: TreeNode): TreeNode {\n    // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n    const balanceFactor = this.balanceFactor(node);\n    // \u5de6\u504f\u6811\n    if (balanceFactor > 1) {\n        if (this.balanceFactor(node.left) >= 0) {\n            // \u53f3\u65cb\n            return this.rightRotate(node);\n        } else {\n            // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n            node.left = this.leftRotate(node.left);\n            return this.rightRotate(node);\n        }\n    }\n    // \u53f3\u504f\u6811\n    if (balanceFactor < -1) {\n        if (this.balanceFactor(node.right) <= 0) {\n            // \u5de6\u65cb\n            return this.leftRotate(node);\n        } else {\n            // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n            node.right = this.rightRotate(node.right);\n            return this.leftRotate(node);\n        }\n    }\n    // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n    return node;\n}\n
        avl_tree.dart
        /* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode? rotate(TreeNode? node) {\n  // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n  int factor = balanceFactor(node);\n  // \u5de6\u504f\u6811\n  if (factor > 1) {\n    if (balanceFactor(node!.left) >= 0) {\n      // \u53f3\u65cb\n      return rightRotate(node);\n    } else {\n      // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n      node.left = leftRotate(node.left);\n      return rightRotate(node);\n    }\n  }\n  // \u53f3\u504f\u6811\n  if (factor < -1) {\n    if (balanceFactor(node!.right) <= 0) {\n      // \u5de6\u65cb\n      return leftRotate(node);\n    } else {\n      // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n      node.right = rightRotate(node.right);\n      return leftRotate(node);\n    }\n  }\n  // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n  return node;\n}\n
        avl_tree.rs
        /* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfn rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n    // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n    let balance_factor = Self::balance_factor(node.clone());\n    // \u5de6\u504f\u6811\n    if balance_factor > 1 {\n        let node = node.unwrap();\n        if Self::balance_factor(node.borrow().left.clone()) >= 0 {\n            // \u53f3\u65cb\n            Self::right_rotate(Some(node))\n        } else {\n            // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n            let left = node.borrow().left.clone();\n            node.borrow_mut().left = Self::left_rotate(left);\n            Self::right_rotate(Some(node))\n        }\n    }\n    // \u53f3\u504f\u6811\n    else if balance_factor < -1 {\n        let node = node.unwrap();\n        if Self::balance_factor(node.borrow().right.clone()) <= 0 {\n            // \u5de6\u65cb\n            Self::left_rotate(Some(node))\n        } else {\n            // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n            let right = node.borrow().right.clone();\n            node.borrow_mut().right = Self::right_rotate(right);\n            Self::left_rotate(Some(node))\n        }\n    } else {\n        // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n        node\n    }\n}\n
        avl_tree.c
        /* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode *rotate(TreeNode *node) {\n    // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n    int bf = balanceFactor(node);\n    // \u5de6\u504f\u6811\n    if (bf > 1) {\n        if (balanceFactor(node->left) >= 0) {\n            // \u53f3\u65cb\n            return rightRotate(node);\n        } else {\n            // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n            node->left = leftRotate(node->left);\n            return rightRotate(node);\n        }\n    }\n    // \u53f3\u504f\u6811\n    if (bf < -1) {\n        if (balanceFactor(node->right) <= 0) {\n            // \u5de6\u65cb\n            return leftRotate(node);\n        } else {\n            // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n            node->right = rightRotate(node->right);\n            return leftRotate(node);\n        }\n    }\n    // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n    return node;\n}\n
        avl_tree.kt
        /* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfun rotate(node: TreeNode): TreeNode {\n    // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n    val balanceFactor = balanceFactor(node)\n    // \u5de6\u504f\u6811\n    if (balanceFactor > 1) {\n        if (balanceFactor(node.left) >= 0) {\n            // \u53f3\u65cb\n            return rightRotate(node)\n        } else {\n            // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n            node.left = leftRotate(node.left)\n            return rightRotate(node)\n        }\n    }\n    // \u53f3\u504f\u6811\n    if (balanceFactor < -1) {\n        if (balanceFactor(node.right) <= 0) {\n            // \u5de6\u65cb\n            return leftRotate(node)\n        } else {\n            // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n            node.right = rightRotate(node.right)\n            return leftRotate(node)\n        }\n    }\n    // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n    return node\n}\n
        avl_tree.rb
        ### \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 ###\ndef rotate(node)\n  # \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n  balance_factor = balance_factor(node)\n  # \u5de6\u904d\u6811\n  if balance_factor > 1\n    if balance_factor(node.left) >= 0\n      # \u53f3\u65cb\n      return right_rotate(node)\n    else\n      # \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n      node.left = left_rotate(node.left)\n      return right_rotate(node)\n    end\n  # \u53f3\u904d\u6811\n  elsif balance_factor < -1\n    if balance_factor(node.right) <= 0\n      # \u5de6\u65cb\n      return left_rotate(node)\n    else\n      # \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n      node.right = right_rotate(node.right)\n      return left_rotate(node)\n    end\n  end\n  # \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n  node\nend\n
        "},{"location":"chapter_tree/avl_tree/#753-avl","title":"7.5.3 \u00a0 AVL \u6811\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_tree/avl_tree/#1_2","title":"1. \u00a0 \u63d2\u5165\u8282\u70b9","text":"

        AVL \u6811\u7684\u8282\u70b9\u63d2\u5165\u64cd\u4f5c\u4e0e\u4e8c\u53c9\u641c\u7d22\u6811\u5728\u4e3b\u4f53\u4e0a\u7c7b\u4f3c\u3002\u552f\u4e00\u7684\u533a\u522b\u5728\u4e8e\uff0c\u5728 AVL \u6811\u4e2d\u63d2\u5165\u8282\u70b9\u540e\uff0c\u4ece\u8be5\u8282\u70b9\u5230\u6839\u8282\u70b9\u7684\u8def\u5f84\u4e0a\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e00\u7cfb\u5217\u5931\u8861\u8282\u70b9\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4ece\u8fd9\u4e2a\u8282\u70b9\u5f00\u59cb\uff0c\u81ea\u5e95\u5411\u4e0a\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6240\u6709\u5931\u8861\u8282\u70b9\u6062\u590d\u5e73\u8861\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def insert(self, val):\n    \"\"\"\u63d2\u5165\u8282\u70b9\"\"\"\n    self._root = self.insert_helper(self._root, val)\n\ndef insert_helper(self, node: TreeNode | None, val: int) -> TreeNode:\n    \"\"\"\u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\"\"\"\n    if node is None:\n        return TreeNode(val)\n    # 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9\n    if val < node.val:\n        node.left = self.insert_helper(node.left, val)\n    elif val > node.val:\n        node.right = self.insert_helper(node.right, val)\n    else:\n        # \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n        return node\n    # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    self.update_height(node)\n    # 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n    return self.rotate(node)\n
        avl_tree.cpp
        /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\n    root = insertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *insertHelper(TreeNode *node, int val) {\n    if (node == nullptr)\n        return new TreeNode(val);\n    /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n    if (val < node->val)\n        node->left = insertHelper(node->left, val);\n    else if (val > node->val)\n        node->right = insertHelper(node->right, val);\n    else\n        return node;    // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n    updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.java
        /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\n    root = insertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode insertHelper(TreeNode node, int val) {\n    if (node == null)\n        return new TreeNode(val);\n    /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n    if (val < node.val)\n        node.left = insertHelper(node.left, val);\n    else if (val > node.val)\n        node.right = insertHelper(node.right, val);\n    else\n        return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n    updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.cs
        /* \u63d2\u5165\u8282\u70b9 */\nvoid Insert(int val) {\n    root = InsertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? InsertHelper(TreeNode? node, int val) {\n    if (node == null) return new TreeNode(val);\n    /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n    if (val < node.val)\n        node.left = InsertHelper(node.left, val);\n    else if (val > node.val)\n        node.right = InsertHelper(node.right, val);\n    else\n        return node;     // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n    UpdateHeight(node);  // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = Rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.go
        /* \u63d2\u5165\u8282\u70b9 */\nfunc (t *aVLTree) insert(val int) {\n    t.root = t.insertHelper(t.root, val)\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nfunc (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode {\n    if node == nil {\n        return NewTreeNode(val)\n    }\n    /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n    if val < node.Val.(int) {\n        node.Left = t.insertHelper(node.Left, val)\n    } else if val > node.Val.(int) {\n        node.Right = t.insertHelper(node.Right, val)\n    } else {\n        // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n        return node\n    }\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    t.updateHeight(node)\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = t.rotate(node)\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node\n}\n
        avl_tree.swift
        /* \u63d2\u5165\u8282\u70b9 */\nfunc insert(val: Int) {\n    root = insertHelper(node: root, val: val)\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc insertHelper(node: TreeNode?, val: Int) -> TreeNode? {\n    var node = node\n    if node == nil {\n        return TreeNode(x: val)\n    }\n    /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n    if val < node!.val {\n        node?.left = insertHelper(node: node?.left, val: val)\n    } else if val > node!.val {\n        node?.right = insertHelper(node: node?.right, val: val)\n    } else {\n        return node // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n    }\n    updateHeight(node: node) // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = rotate(node: node)\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node\n}\n
        avl_tree.js
        /* \u63d2\u5165\u8282\u70b9 */\ninsert(val) {\n    this.root = this.#insertHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\n#insertHelper(node, val) {\n    if (node === null) return new TreeNode(val);\n    /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n    if (val < node.val) node.left = this.#insertHelper(node.left, val);\n    else if (val > node.val)\n        node.right = this.#insertHelper(node.right, val);\n    else return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n    this.#updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = this.#rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.ts
        /* \u63d2\u5165\u8282\u70b9 */\ninsert(val: number): void {\n    this.root = this.insertHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\ninsertHelper(node: TreeNode, val: number): TreeNode {\n    if (node === null) return new TreeNode(val);\n    /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n    if (val < node.val) {\n        node.left = this.insertHelper(node.left, val);\n    } else if (val > node.val) {\n        node.right = this.insertHelper(node.right, val);\n    } else {\n        return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n    }\n    this.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = this.rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.dart
        /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\n  root = insertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? insertHelper(TreeNode? node, int val) {\n  if (node == null) return TreeNode(val);\n  /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n  if (val < node.val)\n    node.left = insertHelper(node.left, val);\n  else if (val > node.val)\n    node.right = insertHelper(node.right, val);\n  else\n    return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n  updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n  /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n  node = rotate(node);\n  // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n  return node;\n}\n
        avl_tree.rs
        /* \u63d2\u5165\u8282\u70b9 */\nfn insert(&mut self, val: i32) {\n    self.root = Self::insert_helper(self.root.clone(), val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfn insert_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {\n    match node {\n        Some(mut node) => {\n            /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n            match {\n                let node_val = node.borrow().val;\n                node_val\n            }\n            .cmp(&val)\n            {\n                Ordering::Greater => {\n                    let left = node.borrow().left.clone();\n                    node.borrow_mut().left = Self::insert_helper(left, val);\n                }\n                Ordering::Less => {\n                    let right = node.borrow().right.clone();\n                    node.borrow_mut().right = Self::insert_helper(right, val);\n                }\n                Ordering::Equal => {\n                    return Some(node); // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n                }\n            }\n            Self::update_height(Some(node.clone())); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n\n            /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n            node = Self::rotate(Some(node)).unwrap();\n            // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n            Some(node)\n        }\n        None => Some(TreeNode::new(val)),\n    }\n}\n
        avl_tree.c
        /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(AVLTree *tree, int val) {\n    tree->root = insertHelper(tree->root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nTreeNode *insertHelper(TreeNode *node, int val) {\n    if (node == NULL) {\n        return newTreeNode(val);\n    }\n    /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n    if (val < node->val) {\n        node->left = insertHelper(node->left, val);\n    } else if (val > node->val) {\n        node->right = insertHelper(node->right, val);\n    } else {\n        // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n        return node;\n    }\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node);\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.kt
        /* \u63d2\u5165\u8282\u70b9 */\nfun insert(_val: Int) {\n    root = insertHelper(root, _val)\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfun insertHelper(n: TreeNode?, _val: Int): TreeNode {\n    if (n == null)\n        return TreeNode(_val)\n    var node = n\n    /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n    if (_val < node._val)\n        node.left = insertHelper(node.left, _val)\n    else if (_val > node._val)\n        node.right = insertHelper(node.right, _val)\n    else\n        return node // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n    updateHeight(node) // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = rotate(node)\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node\n}\n
        avl_tree.rb
        ### \u63d2\u5165\u8282\u70b9 ###\ndef insert(val)\n  @root = insert_helper(@root, val)\nend\n\n### \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09###\ndef insert_helper(node, val)\n  return TreeNode.new(val) if node.nil?\n  # 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9\n  if val < node.val\n    node.left = insert_helper(node.left, val)\n  elsif val > node.val\n    node.right = insert_helper(node.right, val)\n  else\n    # \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n    return node\n  end\n  # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n  update_height(node)\n  # 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n  rotate(node)\nend\n
        "},{"location":"chapter_tree/avl_tree/#2_2","title":"2. \u00a0 \u5220\u9664\u8282\u70b9","text":"

        \u7c7b\u4f3c\u5730\uff0c\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5220\u9664\u8282\u70b9\u65b9\u6cd5\u7684\u57fa\u7840\u4e0a\uff0c\u9700\u8981\u4ece\u5e95\u81f3\u9876\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6240\u6709\u5931\u8861\u8282\u70b9\u6062\u590d\u5e73\u8861\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def remove(self, val: int):\n    \"\"\"\u5220\u9664\u8282\u70b9\"\"\"\n    self._root = self.remove_helper(self._root, val)\n\ndef remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None:\n    \"\"\"\u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\"\"\"\n    if node is None:\n        return None\n    # 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664\n    if val < node.val:\n        node.left = self.remove_helper(node.left, val)\n    elif val > node.val:\n        node.right = self.remove_helper(node.right, val)\n    else:\n        if node.left is None or node.right is None:\n            child = node.left or node.right\n            # \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n            if child is None:\n                return None\n            # \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n            else:\n                node = child\n        else:\n            # \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n            temp = node.right\n            while temp.left is not None:\n                temp = temp.left\n            node.right = self.remove_helper(node.right, temp.val)\n            node.val = temp.val\n    # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    self.update_height(node)\n    # 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n    return self.rotate(node)\n
        avl_tree.cpp
        /* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\n    root = removeHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *removeHelper(TreeNode *node, int val) {\n    if (node == nullptr)\n        return nullptr;\n    /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n    if (val < node->val)\n        node->left = removeHelper(node->left, val);\n    else if (val > node->val)\n        node->right = removeHelper(node->right, val);\n    else {\n        if (node->left == nullptr || node->right == nullptr) {\n            TreeNode *child = node->left != nullptr ? node->left : node->right;\n            // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n            if (child == nullptr) {\n                delete node;\n                return nullptr;\n            }\n            // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n            else {\n                delete node;\n                node = child;\n            }\n        } else {\n            // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n            TreeNode *temp = node->right;\n            while (temp->left != nullptr) {\n                temp = temp->left;\n            }\n            int tempVal = temp->val;\n            node->right = removeHelper(node->right, temp->val);\n            node->val = tempVal;\n        }\n    }\n    updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.java
        /* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\n    root = removeHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode removeHelper(TreeNode node, int val) {\n    if (node == null)\n        return null;\n    /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n    if (val < node.val)\n        node.left = removeHelper(node.left, val);\n    else if (val > node.val)\n        node.right = removeHelper(node.right, val);\n    else {\n        if (node.left == null || node.right == null) {\n            TreeNode child = node.left != null ? node.left : node.right;\n            // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n            if (child == null)\n                return null;\n            // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n            else\n                node = child;\n        } else {\n            // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n            TreeNode temp = node.right;\n            while (temp.left != null) {\n                temp = temp.left;\n            }\n            node.right = removeHelper(node.right, temp.val);\n            node.val = temp.val;\n        }\n    }\n    updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.cs
        /* \u5220\u9664\u8282\u70b9 */\nvoid Remove(int val) {\n    root = RemoveHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? RemoveHelper(TreeNode? node, int val) {\n    if (node == null) return null;\n    /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n    if (val < node.val)\n        node.left = RemoveHelper(node.left, val);\n    else if (val > node.val)\n        node.right = RemoveHelper(node.right, val);\n    else {\n        if (node.left == null || node.right == null) {\n            TreeNode? child = node.left ?? node.right;\n            // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n            if (child == null)\n                return null;\n            // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n            else\n                node = child;\n        } else {\n            // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n            TreeNode? temp = node.right;\n            while (temp.left != null) {\n                temp = temp.left;\n            }\n            node.right = RemoveHelper(node.right, temp.val!.Value);\n            node.val = temp.val;\n        }\n    }\n    UpdateHeight(node);  // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = Rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.go
        /* \u5220\u9664\u8282\u70b9 */\nfunc (t *aVLTree) remove(val int) {\n    t.root = t.removeHelper(t.root, val)\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nfunc (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {\n    if node == nil {\n        return nil\n    }\n    /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n    if val < node.Val.(int) {\n        node.Left = t.removeHelper(node.Left, val)\n    } else if val > node.Val.(int) {\n        node.Right = t.removeHelper(node.Right, val)\n    } else {\n        if node.Left == nil || node.Right == nil {\n            child := node.Left\n            if node.Right != nil {\n                child = node.Right\n            }\n            if child == nil {\n                // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n                return nil\n            } else {\n                // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n                node = child\n            }\n        } else {\n            // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n            temp := node.Right\n            for temp.Left != nil {\n                temp = temp.Left\n            }\n            node.Right = t.removeHelper(node.Right, temp.Val.(int))\n            node.Val = temp.Val\n        }\n    }\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    t.updateHeight(node)\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = t.rotate(node)\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node\n}\n
        avl_tree.swift
        /* \u5220\u9664\u8282\u70b9 */\nfunc remove(val: Int) {\n    root = removeHelper(node: root, val: val)\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc removeHelper(node: TreeNode?, val: Int) -> TreeNode? {\n    var node = node\n    if node == nil {\n        return nil\n    }\n    /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n    if val < node!.val {\n        node?.left = removeHelper(node: node?.left, val: val)\n    } else if val > node!.val {\n        node?.right = removeHelper(node: node?.right, val: val)\n    } else {\n        if node?.left == nil || node?.right == nil {\n            let child = node?.left ?? node?.right\n            // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n            if child == nil {\n                return nil\n            }\n            // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n            else {\n                node = child\n            }\n        } else {\n            // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n            var temp = node?.right\n            while temp?.left != nil {\n                temp = temp?.left\n            }\n            node?.right = removeHelper(node: node?.right, val: temp!.val)\n            node?.val = temp!.val\n        }\n    }\n    updateHeight(node: node) // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = rotate(node: node)\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node\n}\n
        avl_tree.js
        /* \u5220\u9664\u8282\u70b9 */\nremove(val) {\n    this.root = this.#removeHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\n#removeHelper(node, val) {\n    if (node === null) return null;\n    /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n    if (val < node.val) node.left = this.#removeHelper(node.left, val);\n    else if (val > node.val)\n        node.right = this.#removeHelper(node.right, val);\n    else {\n        if (node.left === null || node.right === null) {\n            const child = node.left !== null ? node.left : node.right;\n            // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n            if (child === null) return null;\n            // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n            else node = child;\n        } else {\n            // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n            let temp = node.right;\n            while (temp.left !== null) {\n                temp = temp.left;\n            }\n            node.right = this.#removeHelper(node.right, temp.val);\n            node.val = temp.val;\n        }\n    }\n    this.#updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = this.#rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.ts
        /* \u5220\u9664\u8282\u70b9 */\nremove(val: number): void {\n    this.root = this.removeHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nremoveHelper(node: TreeNode, val: number): TreeNode {\n    if (node === null) return null;\n    /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n    if (val < node.val) {\n        node.left = this.removeHelper(node.left, val);\n    } else if (val > node.val) {\n        node.right = this.removeHelper(node.right, val);\n    } else {\n        if (node.left === null || node.right === null) {\n            const child = node.left !== null ? node.left : node.right;\n            // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n            if (child === null) {\n                return null;\n            } else {\n                // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n                node = child;\n            }\n        } else {\n            // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n            let temp = node.right;\n            while (temp.left !== null) {\n                temp = temp.left;\n            }\n            node.right = this.removeHelper(node.right, temp.val);\n            node.val = temp.val;\n        }\n    }\n    this.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = this.rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.dart
        /* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\n  root = removeHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? removeHelper(TreeNode? node, int val) {\n  if (node == null) return null;\n  /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n  if (val < node.val)\n    node.left = removeHelper(node.left, val);\n  else if (val > node.val)\n    node.right = removeHelper(node.right, val);\n  else {\n    if (node.left == null || node.right == null) {\n      TreeNode? child = node.left ?? node.right;\n      // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n      if (child == null)\n        return null;\n      // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n      else\n        node = child;\n    } else {\n      // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n      TreeNode? temp = node.right;\n      while (temp!.left != null) {\n        temp = temp.left;\n      }\n      node.right = removeHelper(node.right, temp.val);\n      node.val = temp.val;\n    }\n  }\n  updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n  /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n  node = rotate(node);\n  // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n  return node;\n}\n
        avl_tree.rs
        /* \u5220\u9664\u8282\u70b9 */\nfn remove(&self, val: i32) {\n    Self::remove_helper(self.root.clone(), val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfn remove_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {\n    match node {\n        Some(mut node) => {\n            /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n            if val < node.borrow().val {\n                let left = node.borrow().left.clone();\n                node.borrow_mut().left = Self::remove_helper(left, val);\n            } else if val > node.borrow().val {\n                let right = node.borrow().right.clone();\n                node.borrow_mut().right = Self::remove_helper(right, val);\n            } else if node.borrow().left.is_none() || node.borrow().right.is_none() {\n                let child = if node.borrow().left.is_some() {\n                    node.borrow().left.clone()\n                } else {\n                    node.borrow().right.clone()\n                };\n                match child {\n                    // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n                    None => {\n                        return None;\n                    }\n                    // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n                    Some(child) => node = child,\n                }\n            } else {\n                // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n                let mut temp = node.borrow().right.clone().unwrap();\n                loop {\n                    let temp_left = temp.borrow().left.clone();\n                    if temp_left.is_none() {\n                        break;\n                    }\n                    temp = temp_left.unwrap();\n                }\n                let right = node.borrow().right.clone();\n                node.borrow_mut().right = Self::remove_helper(right, temp.borrow().val);\n                node.borrow_mut().val = temp.borrow().val;\n            }\n            Self::update_height(Some(node.clone())); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n\n            /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n            node = Self::rotate(Some(node)).unwrap();\n            // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n            Some(node)\n        }\n        None => None,\n    }\n}\n
        avl_tree.c
        /* \u5220\u9664\u8282\u70b9 */\n// \u7531\u4e8e\u5f15\u5165\u4e86 stdio.h \uff0c\u6b64\u5904\u65e0\u6cd5\u4f7f\u7528 remove \u5173\u952e\u8bcd\nvoid removeItem(AVLTree *tree, int val) {\n    TreeNode *root = removeHelper(tree->root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nTreeNode *removeHelper(TreeNode *node, int val) {\n    TreeNode *child, *grandChild;\n    if (node == NULL) {\n        return NULL;\n    }\n    /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n    if (val < node->val) {\n        node->left = removeHelper(node->left, val);\n    } else if (val > node->val) {\n        node->right = removeHelper(node->right, val);\n    } else {\n        if (node->left == NULL || node->right == NULL) {\n            child = node->left;\n            if (node->right != NULL) {\n                child = node->right;\n            }\n            // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n            if (child == NULL) {\n                return NULL;\n            } else {\n                // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n                node = child;\n            }\n        } else {\n            // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n            TreeNode *temp = node->right;\n            while (temp->left != NULL) {\n                temp = temp->left;\n            }\n            int tempVal = temp->val;\n            node->right = removeHelper(node->right, temp->val);\n            node->val = tempVal;\n        }\n    }\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node);\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.kt
        /* \u5220\u9664\u8282\u70b9 */\nfun remove(_val: Int) {\n    root = removeHelper(root, _val)\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfun removeHelper(n: TreeNode?, _val: Int): TreeNode? {\n    var node = n ?: return null\n    /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n    if (_val < node._val)\n        node.left = removeHelper(node.left, _val)\n    else if (_val > node._val)\n        node.right = removeHelper(node.right, _val)\n    else {\n        if (node.left == null || node.right == null) {\n            val child = if (node.left != null)\n                node.left\n            else\n                node.right\n            // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n            if (child == null)\n                return null\n            // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n            else\n                node = child\n        } else {\n            // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n            var temp = node.right\n            while (temp!!.left != null) {\n                temp = temp.left\n            }\n            node.right = removeHelper(node.right, temp._val)\n            node._val = temp._val\n        }\n    }\n    updateHeight(node) // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = rotate(node)\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node\n}\n
        avl_tree.rb
        ### \u5220\u9664\u8282\u70b9 ###\ndef remove(val)\n  @root = remove_helper(@root, val)\nend\n\n### \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09###\ndef remove_helper(node, val)\n  return if node.nil?\n  # 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664\n  if val < node.val\n    node.left = remove_helper(node.left, val)\n  elsif val > node.val\n    node.right = remove_helper(node.right, val)\n  else\n    if node.left.nil? || node.right.nil?\n      child = node.left || node.right\n      # \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n      return if child.nil?\n      # \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n      node = child\n    else\n      # \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n      temp = node.right\n      while !temp.left.nil?\n        temp = temp.left\n      end\n      node.right = remove_helper(node.right, temp.val)\n      node.val = temp.val\n    end\n  end\n  # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n  update_height(node)\n  # 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n  rotate(node)\nend\n
        "},{"location":"chapter_tree/avl_tree/#3_1","title":"3. \u00a0 \u67e5\u627e\u8282\u70b9","text":"

        AVL \u6811\u7684\u8282\u70b9\u67e5\u627e\u64cd\u4f5c\u4e0e\u4e8c\u53c9\u641c\u7d22\u6811\u4e00\u81f4\uff0c\u5728\u6b64\u4e0d\u518d\u8d58\u8ff0\u3002

        "},{"location":"chapter_tree/avl_tree/#754-avl","title":"7.5.4 \u00a0 AVL \u6811\u5178\u578b\u5e94\u7528","text":"
        • \u7ec4\u7ec7\u548c\u5b58\u50a8\u5927\u578b\u6570\u636e\uff0c\u9002\u7528\u4e8e\u9ad8\u9891\u67e5\u627e\u3001\u4f4e\u9891\u589e\u5220\u7684\u573a\u666f\u3002
        • \u7528\u4e8e\u6784\u5efa\u6570\u636e\u5e93\u4e2d\u7684\u7d22\u5f15\u7cfb\u7edf\u3002
        • \u7ea2\u9ed1\u6811\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811\u3002\u76f8\u8f83\u4e8e AVL \u6811\uff0c\u7ea2\u9ed1\u6811\u7684\u5e73\u8861\u6761\u4ef6\u66f4\u5bbd\u677e\uff0c\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u6240\u9700\u7684\u65cb\u8f6c\u64cd\u4f5c\u66f4\u5c11\uff0c\u8282\u70b9\u589e\u5220\u64cd\u4f5c\u7684\u5e73\u5747\u6548\u7387\u66f4\u9ad8\u3002
        "},{"location":"chapter_tree/binary_search_tree/","title":"7.4 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811","text":"

        \u5982\u56fe 7-16 \u6240\u793a\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\uff08binary search tree\uff09\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\u3002

        1. \u5bf9\u4e8e\u6839\u8282\u70b9\uff0c\u5de6\u5b50\u6811\u4e2d\u6240\u6709\u8282\u70b9\u7684\u503c \\(<\\) \u6839\u8282\u70b9\u7684\u503c \\(<\\) \u53f3\u5b50\u6811\u4e2d\u6240\u6709\u8282\u70b9\u7684\u503c\u3002
        2. \u4efb\u610f\u8282\u70b9\u7684\u5de6\u3001\u53f3\u5b50\u6811\u4e5f\u662f\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u5373\u540c\u6837\u6ee1\u8db3\u6761\u4ef6 1. \u3002

        \u56fe 7-16 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811

        "},{"location":"chapter_tree/binary_search_tree/#741","title":"7.4.1 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u64cd\u4f5c","text":"

        \u6211\u4eec\u5c06\u4e8c\u53c9\u641c\u7d22\u6811\u5c01\u88c5\u4e3a\u4e00\u4e2a\u7c7b BinarySearchTree \uff0c\u5e76\u58f0\u660e\u4e00\u4e2a\u6210\u5458\u53d8\u91cf root \uff0c\u6307\u5411\u6811\u7684\u6839\u8282\u70b9\u3002

        "},{"location":"chapter_tree/binary_search_tree/#1","title":"1. \u00a0 \u67e5\u627e\u8282\u70b9","text":"

        \u7ed9\u5b9a\u76ee\u6807\u8282\u70b9\u503c num \uff0c\u53ef\u4ee5\u6839\u636e\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6027\u8d28\u6765\u67e5\u627e\u3002\u5982\u56fe 7-17 \u6240\u793a\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u8282\u70b9 cur \uff0c\u4ece\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9 root \u51fa\u53d1\uff0c\u5faa\u73af\u6bd4\u8f83\u8282\u70b9\u503c cur.val \u548c num \u4e4b\u95f4\u7684\u5927\u5c0f\u5173\u7cfb\u3002

        • \u82e5 cur.val < num \uff0c\u8bf4\u660e\u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\uff0c\u56e0\u6b64\u6267\u884c cur = cur.right \u3002
        • \u82e5 cur.val > num \uff0c\u8bf4\u660e\u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\uff0c\u56e0\u6b64\u6267\u884c cur = cur.left \u3002
        • \u82e5 cur.val = num \uff0c\u8bf4\u660e\u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\u5e76\u8fd4\u56de\u8be5\u8282\u70b9\u3002
        <1><2><3><4>

        \u56fe 7-17 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u67e5\u627e\u8282\u70b9\u793a\u4f8b

        \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u67e5\u627e\u64cd\u4f5c\u4e0e\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u4e00\u81f4\uff0c\u90fd\u662f\u6bcf\u8f6e\u6392\u9664\u4e00\u534a\u60c5\u51b5\u3002\u5faa\u73af\u6b21\u6570\u6700\u591a\u4e3a\u4e8c\u53c9\u6811\u7684\u9ad8\u5ea6\uff0c\u5f53\u4e8c\u53c9\u6811\u5e73\u8861\u65f6\uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_tree.py
        def search(self, num: int) -> TreeNode | None:\n    \"\"\"\u67e5\u627e\u8282\u70b9\"\"\"\n    cur = self._root\n    # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while cur is not None:\n        # \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if cur.val < num:\n            cur = cur.right\n        # \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        elif cur.val > num:\n            cur = cur.left\n        # \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        else:\n            break\n    return cur\n
        binary_search_tree.cpp
        /* \u67e5\u627e\u8282\u70b9 */\nTreeNode *search(int num) {\n    TreeNode *cur = root;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != nullptr) {\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur->val < num)\n            cur = cur->right;\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else if (cur->val > num)\n            cur = cur->left;\n        // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        else\n            break;\n    }\n    // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n    return cur;\n}\n
        binary_search_tree.java
        /* \u67e5\u627e\u8282\u70b9 */\nTreeNode search(int num) {\n    TreeNode cur = root;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != null) {\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num)\n            cur = cur.right;\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else if (cur.val > num)\n            cur = cur.left;\n        // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        else\n            break;\n    }\n    // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n    return cur;\n}\n
        binary_search_tree.cs
        /* \u67e5\u627e\u8282\u70b9 */\nTreeNode? Search(int num) {\n    TreeNode? cur = root;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != null) {\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num) cur =\n            cur.right;\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else if (cur.val > num)\n            cur = cur.left;\n        // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        else\n            break;\n    }\n    // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n    return cur;\n}\n
        binary_search_tree.go
        /* \u67e5\u627e\u8282\u70b9 */\nfunc (bst *binarySearchTree) search(num int) *TreeNode {\n    node := bst.root\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    for node != nil {\n        if node.Val.(int) < num {\n            // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n            node = node.Right\n        } else if node.Val.(int) > num {\n            // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n            node = node.Left\n        } else {\n            // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n            break\n        }\n    }\n    // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n    return node\n}\n
        binary_search_tree.swift
        /* \u67e5\u627e\u8282\u70b9 */\nfunc search(num: Int) -> TreeNode? {\n    var cur = root\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while cur != nil {\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if cur!.val < num {\n            cur = cur?.right\n        }\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else if cur!.val > num {\n            cur = cur?.left\n        }\n        // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        else {\n            break\n        }\n    }\n    // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n    return cur\n}\n
        binary_search_tree.js
        /* \u67e5\u627e\u8282\u70b9 */\nsearch(num) {\n    let cur = this.root;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur !== null) {\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num) cur = cur.right;\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else if (cur.val > num) cur = cur.left;\n        // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        else break;\n    }\n    // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n    return cur;\n}\n
        binary_search_tree.ts
        /* \u67e5\u627e\u8282\u70b9 */\nsearch(num: number): TreeNode | null {\n    let cur = this.root;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur !== null) {\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num) cur = cur.right;\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else if (cur.val > num) cur = cur.left;\n        // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        else break;\n    }\n    // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n    return cur;\n}\n
        binary_search_tree.dart
        /* \u67e5\u627e\u8282\u70b9 */\nTreeNode? search(int _num) {\n  TreeNode? cur = _root;\n  // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n  while (cur != null) {\n    // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n    if (cur.val < _num)\n      cur = cur.right;\n    // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n    else if (cur.val > _num)\n      cur = cur.left;\n    // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n    else\n      break;\n  }\n  // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n  return cur;\n}\n
        binary_search_tree.rs
        /* \u67e5\u627e\u8282\u70b9 */\npub fn search(&self, num: i32) -> OptionTreeNodeRc {\n    let mut cur = self.root.clone();\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while let Some(node) = cur.clone() {\n        match num.cmp(&node.borrow().val) {\n            // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n            Ordering::Greater => cur = node.borrow().right.clone(),\n            // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n            Ordering::Less => cur = node.borrow().left.clone(),\n            // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n            Ordering::Equal => break,\n        }\n    }\n\n    // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n    cur\n}\n
        binary_search_tree.c
        /* \u67e5\u627e\u8282\u70b9 */\nTreeNode *search(BinarySearchTree *bst, int num) {\n    TreeNode *cur = bst->root;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != NULL) {\n        if (cur->val < num) {\n            // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n            cur = cur->right;\n        } else if (cur->val > num) {\n            // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n            cur = cur->left;\n        } else {\n            // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n            break;\n        }\n    }\n    // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n    return cur;\n}\n
        binary_search_tree.kt
        /* \u67e5\u627e\u8282\u70b9 */\nfun search(num: Int): TreeNode? {\n    var cur = root\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != null) {\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        cur = if (cur._val < num)\n            cur.right\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else if (cur._val > num)\n            cur.left\n        // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        else\n            break\n    }\n    // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n    return cur\n}\n
        binary_search_tree.rb
        ### \u67e5\u627e\u8282\u70b9 ###\ndef search(num)\n  cur = @root\n\n  # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n  while !cur.nil?\n    # \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n    if cur.val < num\n      cur = cur.right\n    # \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n    elsif cur.val > num\n      cur = cur.left\n    # \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n    else\n      break\n    end\n  end\n\n  cur\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_tree/binary_search_tree/#2","title":"2. \u00a0 \u63d2\u5165\u8282\u70b9","text":"

        \u7ed9\u5b9a\u4e00\u4e2a\u5f85\u63d2\u5165\u5143\u7d20 num \uff0c\u4e3a\u4e86\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u201c\u5de6\u5b50\u6811 < \u6839\u8282\u70b9 < \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\uff0c\u63d2\u5165\u64cd\u4f5c\u6d41\u7a0b\u5982\u56fe 7-18 \u6240\u793a\u3002

        1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff1a\u4e0e\u67e5\u627e\u64cd\u4f5c\u76f8\u4f3c\uff0c\u4ece\u6839\u8282\u70b9\u51fa\u53d1\uff0c\u6839\u636e\u5f53\u524d\u8282\u70b9\u503c\u548c num \u7684\u5927\u5c0f\u5173\u7cfb\u5faa\u73af\u5411\u4e0b\u641c\u7d22\uff0c\u76f4\u5230\u8d8a\u8fc7\u53f6\u8282\u70b9\uff08\u904d\u5386\u81f3 None \uff09\u65f6\u8df3\u51fa\u5faa\u73af\u3002
        2. \u5728\u8be5\u4f4d\u7f6e\u63d2\u5165\u8282\u70b9\uff1a\u521d\u59cb\u5316\u8282\u70b9 num \uff0c\u5c06\u8be5\u8282\u70b9\u7f6e\u4e8e None \u7684\u4f4d\u7f6e\u3002

        \u56fe 7-18 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u63d2\u5165\u8282\u70b9

        \u5728\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u4e24\u70b9\u3002

        • \u4e8c\u53c9\u641c\u7d22\u6811\u4e0d\u5141\u8bb8\u5b58\u5728\u91cd\u590d\u8282\u70b9\uff0c\u5426\u5219\u5c06\u8fdd\u53cd\u5176\u5b9a\u4e49\u3002\u56e0\u6b64\uff0c\u82e5\u5f85\u63d2\u5165\u8282\u70b9\u5728\u6811\u4e2d\u5df2\u5b58\u5728\uff0c\u5219\u4e0d\u6267\u884c\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\u3002
        • \u4e3a\u4e86\u5b9e\u73b0\u63d2\u5165\u8282\u70b9\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u8282\u70b9 pre \u4fdd\u5b58\u4e0a\u4e00\u8f6e\u5faa\u73af\u7684\u8282\u70b9\u3002\u8fd9\u6837\u5728\u904d\u5386\u81f3 None \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u5230\u5176\u7236\u8282\u70b9\uff0c\u4ece\u800c\u5b8c\u6210\u8282\u70b9\u63d2\u5165\u64cd\u4f5c\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_tree.py
        def insert(self, num: int):\n    \"\"\"\u63d2\u5165\u8282\u70b9\"\"\"\n    # \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n    if self._root is None:\n        self._root = TreeNode(num)\n        return\n    # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    cur, pre = self._root, None\n    while cur is not None:\n        # \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n        if cur.val == num:\n            return\n        pre = cur\n        # \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if cur.val < num:\n            cur = cur.right\n        # \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else:\n            cur = cur.left\n    # \u63d2\u5165\u8282\u70b9\n    node = TreeNode(num)\n    if pre.val < num:\n        pre.right = node\n    else:\n        pre.left = node\n
        binary_search_tree.cpp
        /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n    if (root == nullptr) {\n        root = new TreeNode(num);\n        return;\n    }\n    TreeNode *cur = root, *pre = nullptr;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != nullptr) {\n        // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n        if (cur->val == num)\n            return;\n        pre = cur;\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur->val < num)\n            cur = cur->right;\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else\n            cur = cur->left;\n    }\n    // \u63d2\u5165\u8282\u70b9\n    TreeNode *node = new TreeNode(num);\n    if (pre->val < num)\n        pre->right = node;\n    else\n        pre->left = node;\n}\n
        binary_search_tree.java
        /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n    if (root == null) {\n        root = new TreeNode(num);\n        return;\n    }\n    TreeNode cur = root, pre = null;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != null) {\n        // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n        if (cur.val == num)\n            return;\n        pre = cur;\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num)\n            cur = cur.right;\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else\n            cur = cur.left;\n    }\n    // \u63d2\u5165\u8282\u70b9\n    TreeNode node = new TreeNode(num);\n    if (pre.val < num)\n        pre.right = node;\n    else\n        pre.left = node;\n}\n
        binary_search_tree.cs
        /* \u63d2\u5165\u8282\u70b9 */\nvoid Insert(int num) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n    if (root == null) {\n        root = new TreeNode(num);\n        return;\n    }\n    TreeNode? cur = root, pre = null;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != null) {\n        // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n        if (cur.val == num)\n            return;\n        pre = cur;\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num)\n            cur = cur.right;\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else\n            cur = cur.left;\n    }\n\n    // \u63d2\u5165\u8282\u70b9\n    TreeNode node = new(num);\n    if (pre != null) {\n        if (pre.val < num)\n            pre.right = node;\n        else\n            pre.left = node;\n    }\n}\n
        binary_search_tree.go
        /* \u63d2\u5165\u8282\u70b9 */\nfunc (bst *binarySearchTree) insert(num int) {\n    cur := bst.root\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n    if cur == nil {\n        bst.root = NewTreeNode(num)\n        return\n    }\n    // \u5f85\u63d2\u5165\u8282\u70b9\u4e4b\u524d\u7684\u8282\u70b9\u4f4d\u7f6e\n    var pre *TreeNode = nil\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    for cur != nil {\n        if cur.Val == num {\n            return\n        }\n        pre = cur\n        if cur.Val.(int) < num {\n            cur = cur.Right\n        } else {\n            cur = cur.Left\n        }\n    }\n    // \u63d2\u5165\u8282\u70b9\n    node := NewTreeNode(num)\n    if pre.Val.(int) < num {\n        pre.Right = node\n    } else {\n        pre.Left = node\n    }\n}\n
        binary_search_tree.swift
        /* \u63d2\u5165\u8282\u70b9 */\nfunc insert(num: Int) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n    if root == nil {\n        root = TreeNode(x: num)\n        return\n    }\n    var cur = root\n    var pre: TreeNode?\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while cur != nil {\n        // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n        if cur!.val == num {\n            return\n        }\n        pre = cur\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if cur!.val < num {\n            cur = cur?.right\n        }\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else {\n            cur = cur?.left\n        }\n    }\n    // \u63d2\u5165\u8282\u70b9\n    let node = TreeNode(x: num)\n    if pre!.val < num {\n        pre?.right = node\n    } else {\n        pre?.left = node\n    }\n}\n
        binary_search_tree.js
        /* \u63d2\u5165\u8282\u70b9 */\ninsert(num) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n    if (this.root === null) {\n        this.root = new TreeNode(num);\n        return;\n    }\n    let cur = this.root,\n        pre = null;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur !== null) {\n        // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n        if (cur.val === num) return;\n        pre = cur;\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num) cur = cur.right;\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else cur = cur.left;\n    }\n    // \u63d2\u5165\u8282\u70b9\n    const node = new TreeNode(num);\n    if (pre.val < num) pre.right = node;\n    else pre.left = node;\n}\n
        binary_search_tree.ts
        /* \u63d2\u5165\u8282\u70b9 */\ninsert(num: number): void {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n    if (this.root === null) {\n        this.root = new TreeNode(num);\n        return;\n    }\n    let cur: TreeNode | null = this.root,\n        pre: TreeNode | null = null;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur !== null) {\n        // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n        if (cur.val === num) return;\n        pre = cur;\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num) cur = cur.right;\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else cur = cur.left;\n    }\n    // \u63d2\u5165\u8282\u70b9\n    const node = new TreeNode(num);\n    if (pre!.val < num) pre!.right = node;\n    else pre!.left = node;\n}\n
        binary_search_tree.dart
        /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int _num) {\n  // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n  if (_root == null) {\n    _root = TreeNode(_num);\n    return;\n  }\n  TreeNode? cur = _root;\n  TreeNode? pre = null;\n  // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n  while (cur != null) {\n    // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n    if (cur.val == _num) return;\n    pre = cur;\n    // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n    if (cur.val < _num)\n      cur = cur.right;\n    // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n    else\n      cur = cur.left;\n  }\n  // \u63d2\u5165\u8282\u70b9\n  TreeNode? node = TreeNode(_num);\n  if (pre!.val < _num)\n    pre.right = node;\n  else\n    pre.left = node;\n}\n
        binary_search_tree.rs
        /* \u63d2\u5165\u8282\u70b9 */\npub fn insert(&mut self, num: i32) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n    if self.root.is_none() {\n        self.root = Some(TreeNode::new(num));\n        return;\n    }\n    let mut cur = self.root.clone();\n    let mut pre = None;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while let Some(node) = cur.clone() {\n        match num.cmp(&node.borrow().val) {\n            // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n            Ordering::Equal => return,\n            // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n            Ordering::Greater => {\n                pre = cur.clone();\n                cur = node.borrow().right.clone();\n            }\n            // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n            Ordering::Less => {\n                pre = cur.clone();\n                cur = node.borrow().left.clone();\n            }\n        }\n    }\n    // \u63d2\u5165\u8282\u70b9\n    let pre = pre.unwrap();\n    let node = Some(TreeNode::new(num));\n    if num > pre.borrow().val {\n        pre.borrow_mut().right = node;\n    } else {\n        pre.borrow_mut().left = node;\n    }\n}\n
        binary_search_tree.c
        /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(BinarySearchTree *bst, int num) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n    if (bst->root == NULL) {\n        bst->root = newTreeNode(num);\n        return;\n    }\n    TreeNode *cur = bst->root, *pre = NULL;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != NULL) {\n        // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n        if (cur->val == num) {\n            return;\n        }\n        pre = cur;\n        if (cur->val < num) {\n            // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n            cur = cur->right;\n        } else {\n            // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n            cur = cur->left;\n        }\n    }\n    // \u63d2\u5165\u8282\u70b9\n    TreeNode *node = newTreeNode(num);\n    if (pre->val < num) {\n        pre->right = node;\n    } else {\n        pre->left = node;\n    }\n}\n
        binary_search_tree.kt
        /* \u63d2\u5165\u8282\u70b9 */\nfun insert(num: Int) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n    if (root == null) {\n        root = TreeNode(num)\n        return\n    }\n    var cur = root\n    var pre: TreeNode? = null\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != null) {\n        // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n        if (cur._val == num)\n            return\n        pre = cur\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        cur = if (cur._val < num)\n            cur.right\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else\n            cur.left\n    }\n    // \u63d2\u5165\u8282\u70b9\n    val node = TreeNode(num)\n    if (pre?._val!! < num)\n        pre.right = node\n    else\n        pre.left = node\n}\n
        binary_search_tree.rb
        ### \u63d2\u5165\u8282\u70b9 ###\ndef insert(num)\n  # \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n  if @root.nil?\n    @root = TreeNode.new(num)\n    return\n  end\n\n  # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n  cur, pre = @root, nil\n  while !cur.nil?\n    # \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n    return if cur.val == num\n\n    pre = cur\n    # \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n    if cur.val < num\n      cur = cur.right\n    # \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n    else\n      cur = cur.left\n    end\n  end\n\n  # \u63d2\u5165\u8282\u70b9\n  node = TreeNode.new(num)\n  if pre.val < num\n    pre.right = node\n  else\n    pre.left = node\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u4e0e\u67e5\u627e\u8282\u70b9\u76f8\u540c\uff0c\u63d2\u5165\u8282\u70b9\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\u3002

        "},{"location":"chapter_tree/binary_search_tree/#3","title":"3. \u00a0 \u5220\u9664\u8282\u70b9","text":"

        \u5148\u5728\u4e8c\u53c9\u6811\u4e2d\u67e5\u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u518d\u5c06\u5176\u5220\u9664\u3002\u4e0e\u63d2\u5165\u8282\u70b9\u7c7b\u4f3c\uff0c\u6211\u4eec\u9700\u8981\u4fdd\u8bc1\u5728\u5220\u9664\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u201c\u5de6\u5b50\u6811 < \u6839\u8282\u70b9 < \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\u4ecd\u7136\u6ee1\u8db3\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u6839\u636e\u76ee\u6807\u8282\u70b9\u7684\u5b50\u8282\u70b9\u6570\u91cf\uff0c\u5206 0\u30011 \u548c 2 \u4e09\u79cd\u60c5\u51b5\uff0c\u6267\u884c\u5bf9\u5e94\u7684\u5220\u9664\u8282\u70b9\u64cd\u4f5c\u3002

        \u5982\u56fe 7-19 \u6240\u793a\uff0c\u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(0\\) \u65f6\uff0c\u8868\u793a\u8be5\u8282\u70b9\u662f\u53f6\u8282\u70b9\uff0c\u53ef\u4ee5\u76f4\u63a5\u5220\u9664\u3002

        \u56fe 7-19 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 0 \uff09

        \u5982\u56fe 7-20 \u6240\u793a\uff0c\u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(1\\) \u65f6\uff0c\u5c06\u5f85\u5220\u9664\u8282\u70b9\u66ff\u6362\u4e3a\u5176\u5b50\u8282\u70b9\u5373\u53ef\u3002

        \u56fe 7-20 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 1 \uff09

        \u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(2\\) \u65f6\uff0c\u6211\u4eec\u65e0\u6cd5\u76f4\u63a5\u5220\u9664\u5b83\uff0c\u800c\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u8282\u70b9\u66ff\u6362\u8be5\u8282\u70b9\u3002\u7531\u4e8e\u8981\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u201c\u5de6\u5b50\u6811 \\(<\\) \u6839\u8282\u70b9 \\(<\\) \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\uff0c\u56e0\u6b64\u8fd9\u4e2a\u8282\u70b9\u53ef\u4ee5\u662f\u53f3\u5b50\u6811\u7684\u6700\u5c0f\u8282\u70b9\u6216\u5de6\u5b50\u6811\u7684\u6700\u5927\u8282\u70b9\u3002

        \u5047\u8bbe\u6211\u4eec\u9009\u62e9\u53f3\u5b50\u6811\u7684\u6700\u5c0f\u8282\u70b9\uff08\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\uff09\uff0c\u5219\u5220\u9664\u64cd\u4f5c\u6d41\u7a0b\u5982\u56fe 7-21 \u6240\u793a\u3002

        1. \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\u5728\u201c\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u201d\u4e2d\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\uff0c\u8bb0\u4e3a tmp \u3002
        2. \u7528 tmp \u7684\u503c\u8986\u76d6\u5f85\u5220\u9664\u8282\u70b9\u7684\u503c\uff0c\u5e76\u5728\u6811\u4e2d\u9012\u5f52\u5220\u9664\u8282\u70b9 tmp \u3002
        <1><2><3><4>

        \u56fe 7-21 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 2 \uff09

        \u5220\u9664\u8282\u70b9\u64cd\u4f5c\u540c\u6837\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\uff0c\u5176\u4e2d\u67e5\u627e\u5f85\u5220\u9664\u8282\u70b9\u9700\u8981 \\(O(\\log n)\\) \u65f6\u95f4\uff0c\u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u540e\u7ee7\u8282\u70b9\u9700\u8981 \\(O(\\log n)\\) \u65f6\u95f4\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_tree.py
        def remove(self, num: int):\n    \"\"\"\u5220\u9664\u8282\u70b9\"\"\"\n    # \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n    if self._root is None:\n        return\n    # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    cur, pre = self._root, None\n    while cur is not None:\n        # \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        if cur.val == num:\n            break\n        pre = cur\n        # \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if cur.val < num:\n            cur = cur.right\n        # \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else:\n            cur = cur.left\n    # \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if cur is None:\n        return\n\n    # \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n    if cur.left is None or cur.right is None:\n        # \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n        child = cur.left or cur.right\n        # \u5220\u9664\u8282\u70b9 cur\n        if cur != self._root:\n            if pre.left == cur:\n                pre.left = child\n            else:\n                pre.right = child\n        else:\n            # \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n            self._root = child\n    # \u5b50\u8282\u70b9\u6570\u91cf = 2\n    else:\n        # \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n        tmp: TreeNode = cur.right\n        while tmp.left is not None:\n            tmp = tmp.left\n        # \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n        self.remove(tmp.val)\n        # \u7528 tmp \u8986\u76d6 cur\n        cur.val = tmp.val\n
        binary_search_tree.cpp
        /* \u5220\u9664\u8282\u70b9 */\nvoid remove(int num) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n    if (root == nullptr)\n        return;\n    TreeNode *cur = root, *pre = nullptr;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != nullptr) {\n        // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        if (cur->val == num)\n            break;\n        pre = cur;\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur->val < num)\n            cur = cur->right;\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else\n            cur = cur->left;\n    }\n    // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (cur == nullptr)\n        return;\n    // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n    if (cur->left == nullptr || cur->right == nullptr) {\n        // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\n        TreeNode *child = cur->left != nullptr ? cur->left : cur->right;\n        // \u5220\u9664\u8282\u70b9 cur\n        if (cur != root) {\n            if (pre->left == cur)\n                pre->left = child;\n            else\n                pre->right = child;\n        } else {\n            // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n            root = child;\n        }\n        // \u91ca\u653e\u5185\u5b58\n        delete cur;\n    }\n    // \u5b50\u8282\u70b9\u6570\u91cf = 2\n    else {\n        // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n        TreeNode *tmp = cur->right;\n        while (tmp->left != nullptr) {\n            tmp = tmp->left;\n        }\n        int tmpVal = tmp->val;\n        // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n        remove(tmp->val);\n        // \u7528 tmp \u8986\u76d6 cur\n        cur->val = tmpVal;\n    }\n}\n
        binary_search_tree.java
        /* \u5220\u9664\u8282\u70b9 */\nvoid remove(int num) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n    if (root == null)\n        return;\n    TreeNode cur = root, pre = null;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != null) {\n        // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        if (cur.val == num)\n            break;\n        pre = cur;\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num)\n            cur = cur.right;\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else\n            cur = cur.left;\n    }\n    // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (cur == null)\n        return;\n    // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n    if (cur.left == null || cur.right == null) {\n        // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n        TreeNode child = cur.left != null ? cur.left : cur.right;\n        // \u5220\u9664\u8282\u70b9 cur\n        if (cur != root) {\n            if (pre.left == cur)\n                pre.left = child;\n            else\n                pre.right = child;\n        } else {\n            // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n            root = child;\n        }\n    }\n    // \u5b50\u8282\u70b9\u6570\u91cf = 2\n    else {\n        // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n        TreeNode tmp = cur.right;\n        while (tmp.left != null) {\n            tmp = tmp.left;\n        }\n        // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n        remove(tmp.val);\n        // \u7528 tmp \u8986\u76d6 cur\n        cur.val = tmp.val;\n    }\n}\n
        binary_search_tree.cs
        /* \u5220\u9664\u8282\u70b9 */\nvoid Remove(int num) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n    if (root == null)\n        return;\n    TreeNode? cur = root, pre = null;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != null) {\n        // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        if (cur.val == num)\n            break;\n        pre = cur;\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num)\n            cur = cur.right;\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else\n            cur = cur.left;\n    }\n    // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (cur == null)\n        return;\n    // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n    if (cur.left == null || cur.right == null) {\n        // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n        TreeNode? child = cur.left ?? cur.right;\n        // \u5220\u9664\u8282\u70b9 cur\n        if (cur != root) {\n            if (pre!.left == cur)\n                pre.left = child;\n            else\n                pre.right = child;\n        } else {\n            // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n            root = child;\n        }\n    }\n    // \u5b50\u8282\u70b9\u6570\u91cf = 2\n    else {\n        // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n        TreeNode? tmp = cur.right;\n        while (tmp.left != null) {\n            tmp = tmp.left;\n        }\n        // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n        Remove(tmp.val!.Value);\n        // \u7528 tmp \u8986\u76d6 cur\n        cur.val = tmp.val;\n    }\n}\n
        binary_search_tree.go
        /* \u5220\u9664\u8282\u70b9 */\nfunc (bst *binarySearchTree) remove(num int) {\n    cur := bst.root\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n    if cur == nil {\n        return\n    }\n    // \u5f85\u5220\u9664\u8282\u70b9\u4e4b\u524d\u7684\u8282\u70b9\u4f4d\u7f6e\n    var pre *TreeNode = nil\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    for cur != nil {\n        if cur.Val == num {\n            break\n        }\n        pre = cur\n        if cur.Val.(int) < num {\n            // \u5f85\u5220\u9664\u8282\u70b9\u5728\u53f3\u5b50\u6811\u4e2d\n            cur = cur.Right\n        } else {\n            // \u5f85\u5220\u9664\u8282\u70b9\u5728\u5de6\u5b50\u6811\u4e2d\n            cur = cur.Left\n        }\n    }\n    // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if cur == nil {\n        return\n    }\n    // \u5b50\u8282\u70b9\u6570\u4e3a 0 \u6216 1\n    if cur.Left == nil || cur.Right == nil {\n        var child *TreeNode = nil\n        // \u53d6\u51fa\u5f85\u5220\u9664\u8282\u70b9\u7684\u5b50\u8282\u70b9\n        if cur.Left != nil {\n            child = cur.Left\n        } else {\n            child = cur.Right\n        }\n        // \u5220\u9664\u8282\u70b9 cur\n        if cur != bst.root {\n            if pre.Left == cur {\n                pre.Left = child\n            } else {\n                pre.Right = child\n            }\n        } else {\n            // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n            bst.root = child\n        }\n        // \u5b50\u8282\u70b9\u6570\u4e3a 2\n    } else {\n        // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u5f85\u5220\u9664\u8282\u70b9 cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n        tmp := cur.Right\n        for tmp.Left != nil {\n            tmp = tmp.Left\n        }\n        // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n        bst.remove(tmp.Val.(int))\n        // \u7528 tmp \u8986\u76d6 cur\n        cur.Val = tmp.Val\n    }\n}\n
        binary_search_tree.swift
        /* \u5220\u9664\u8282\u70b9 */\nfunc remove(num: Int) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n    if root == nil {\n        return\n    }\n    var cur = root\n    var pre: TreeNode?\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while cur != nil {\n        // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        if cur!.val == num {\n            break\n        }\n        pre = cur\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if cur!.val < num {\n            cur = cur?.right\n        }\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else {\n            cur = cur?.left\n        }\n    }\n    // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if cur == nil {\n        return\n    }\n    // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n    if cur?.left == nil || cur?.right == nil {\n        // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n        let child = cur?.left ?? cur?.right\n        // \u5220\u9664\u8282\u70b9 cur\n        if cur !== root {\n            if pre?.left === cur {\n                pre?.left = child\n            } else {\n                pre?.right = child\n            }\n        } else {\n            // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n            root = child\n        }\n    }\n    // \u5b50\u8282\u70b9\u6570\u91cf = 2\n    else {\n        // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n        var tmp = cur?.right\n        while tmp?.left != nil {\n            tmp = tmp?.left\n        }\n        // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n        remove(num: tmp!.val)\n        // \u7528 tmp \u8986\u76d6 cur\n        cur?.val = tmp!.val\n    }\n}\n
        binary_search_tree.js
        /* \u5220\u9664\u8282\u70b9 */\nremove(num) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n    if (this.root === null) return;\n    let cur = this.root,\n        pre = null;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur !== null) {\n        // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        if (cur.val === num) break;\n        pre = cur;\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num) cur = cur.right;\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else cur = cur.left;\n    }\n    // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (cur === null) return;\n    // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n    if (cur.left === null || cur.right === null) {\n        // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n        const child = cur.left !== null ? cur.left : cur.right;\n        // \u5220\u9664\u8282\u70b9 cur\n        if (cur !== this.root) {\n            if (pre.left === cur) pre.left = child;\n            else pre.right = child;\n        } else {\n            // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n            this.root = child;\n        }\n    }\n    // \u5b50\u8282\u70b9\u6570\u91cf = 2\n    else {\n        // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n        let tmp = cur.right;\n        while (tmp.left !== null) {\n            tmp = tmp.left;\n        }\n        // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n        this.remove(tmp.val);\n        // \u7528 tmp \u8986\u76d6 cur\n        cur.val = tmp.val;\n    }\n}\n
        binary_search_tree.ts
        /* \u5220\u9664\u8282\u70b9 */\nremove(num: number): void {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n    if (this.root === null) return;\n    let cur: TreeNode | null = this.root,\n        pre: TreeNode | null = null;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur !== null) {\n        // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        if (cur.val === num) break;\n        pre = cur;\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num) cur = cur.right;\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else cur = cur.left;\n    }\n    // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (cur === null) return;\n    // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n    if (cur.left === null || cur.right === null) {\n        // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n        const child: TreeNode | null =\n            cur.left !== null ? cur.left : cur.right;\n        // \u5220\u9664\u8282\u70b9 cur\n        if (cur !== this.root) {\n            if (pre!.left === cur) pre!.left = child;\n            else pre!.right = child;\n        } else {\n            // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n            this.root = child;\n        }\n    }\n    // \u5b50\u8282\u70b9\u6570\u91cf = 2\n    else {\n        // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n        let tmp: TreeNode | null = cur.right;\n        while (tmp!.left !== null) {\n            tmp = tmp!.left;\n        }\n        // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n        this.remove(tmp!.val);\n        // \u7528 tmp \u8986\u76d6 cur\n        cur.val = tmp!.val;\n    }\n}\n
        binary_search_tree.dart
        /* \u5220\u9664\u8282\u70b9 */\nvoid remove(int _num) {\n  // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n  if (_root == null) return;\n  TreeNode? cur = _root;\n  TreeNode? pre = null;\n  // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n  while (cur != null) {\n    // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n    if (cur.val == _num) break;\n    pre = cur;\n    // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n    if (cur.val < _num)\n      cur = cur.right;\n    // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n    else\n      cur = cur.left;\n  }\n  // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n  if (cur == null) return;\n  // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n  if (cur.left == null || cur.right == null) {\n    // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n    TreeNode? child = cur.left ?? cur.right;\n    // \u5220\u9664\u8282\u70b9 cur\n    if (cur != _root) {\n      if (pre!.left == cur)\n        pre.left = child;\n      else\n        pre.right = child;\n    } else {\n      // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n      _root = child;\n    }\n  } else {\n    // \u5b50\u8282\u70b9\u6570\u91cf = 2\n    // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n    TreeNode? tmp = cur.right;\n    while (tmp!.left != null) {\n      tmp = tmp.left;\n    }\n    // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n    remove(tmp.val);\n    // \u7528 tmp \u8986\u76d6 cur\n    cur.val = tmp.val;\n  }\n}\n
        binary_search_tree.rs
        /* \u5220\u9664\u8282\u70b9 */\npub fn remove(&mut self, num: i32) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n    if self.root.is_none() {\n        return;\n    }\n    let mut cur = self.root.clone();\n    let mut pre = None;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while let Some(node) = cur.clone() {\n        match num.cmp(&node.borrow().val) {\n            // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n            Ordering::Equal => break,\n            // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n            Ordering::Greater => {\n                pre = cur.clone();\n                cur = node.borrow().right.clone();\n            }\n            // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n            Ordering::Less => {\n                pre = cur.clone();\n                cur = node.borrow().left.clone();\n            }\n        }\n    }\n    // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if cur.is_none() {\n        return;\n    }\n    let cur = cur.unwrap();\n    let (left_child, right_child) = (cur.borrow().left.clone(), cur.borrow().right.clone());\n    match (left_child.clone(), right_child.clone()) {\n        // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n        (None, None) | (Some(_), None) | (None, Some(_)) => {\n            // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\n            let child = left_child.or(right_child);\n            let pre = pre.unwrap();\n            // \u5220\u9664\u8282\u70b9 cur\n            if !Rc::ptr_eq(&cur, self.root.as_ref().unwrap()) {\n                let left = pre.borrow().left.clone();\n                if left.is_some() && Rc::ptr_eq(left.as_ref().unwrap(), &cur) {\n                    pre.borrow_mut().left = child;\n                } else {\n                    pre.borrow_mut().right = child;\n                }\n            } else {\n                // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n                self.root = child;\n            }\n        }\n        // \u5b50\u8282\u70b9\u6570\u91cf = 2\n        (Some(_), Some(_)) => {\n            // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n            let mut tmp = cur.borrow().right.clone();\n            while let Some(node) = tmp.clone() {\n                if node.borrow().left.is_some() {\n                    tmp = node.borrow().left.clone();\n                } else {\n                    break;\n                }\n            }\n            let tmp_val = tmp.unwrap().borrow().val;\n            // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n            self.remove(tmp_val);\n            // \u7528 tmp \u8986\u76d6 cur\n            cur.borrow_mut().val = tmp_val;\n        }\n    }\n}\n
        binary_search_tree.c
        /* \u5220\u9664\u8282\u70b9 */\n// \u7531\u4e8e\u5f15\u5165\u4e86 stdio.h \uff0c\u6b64\u5904\u65e0\u6cd5\u4f7f\u7528 remove \u5173\u952e\u8bcd\nvoid removeItem(BinarySearchTree *bst, int num) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n    if (bst->root == NULL)\n        return;\n    TreeNode *cur = bst->root, *pre = NULL;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != NULL) {\n        // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        if (cur->val == num)\n            break;\n        pre = cur;\n        if (cur->val < num) {\n            // \u5f85\u5220\u9664\u8282\u70b9\u5728 root \u7684\u53f3\u5b50\u6811\u4e2d\n            cur = cur->right;\n        } else {\n            // \u5f85\u5220\u9664\u8282\u70b9\u5728 root \u7684\u5de6\u5b50\u6811\u4e2d\n            cur = cur->left;\n        }\n    }\n    // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (cur == NULL)\n        return;\n    // \u5224\u65ad\u5f85\u5220\u9664\u8282\u70b9\u662f\u5426\u5b58\u5728\u5b50\u8282\u70b9\n    if (cur->left == NULL || cur->right == NULL) {\n        /* \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1 */\n        // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\n        TreeNode *child = cur->left != NULL ? cur->left : cur->right;\n        // \u5220\u9664\u8282\u70b9 cur\n        if (pre->left == cur) {\n            pre->left = child;\n        } else {\n            pre->right = child;\n        }\n        // \u91ca\u653e\u5185\u5b58\n        free(cur);\n    } else {\n        /* \u5b50\u8282\u70b9\u6570\u91cf = 2 */\n        // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n        TreeNode *tmp = cur->right;\n        while (tmp->left != NULL) {\n            tmp = tmp->left;\n        }\n        int tmpVal = tmp->val;\n        // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n        removeItem(bst, tmp->val);\n        // \u7528 tmp \u8986\u76d6 cur\n        cur->val = tmpVal;\n    }\n}\n
        binary_search_tree.kt
        /* \u5220\u9664\u8282\u70b9 */\nfun remove(num: Int) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n    if (root == null)\n        return\n    var cur = root\n    var pre: TreeNode? = null\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != null) {\n        // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        if (cur._val == num)\n            break\n        pre = cur\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        cur = if (cur._val < num)\n            cur.right\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else\n            cur.left\n    }\n    // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (cur == null)\n        return\n    // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n    if (cur.left == null || cur.right == null) {\n        // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n        val child = if (cur.left != null)\n            cur.left\n        else\n            cur.right\n        // \u5220\u9664\u8282\u70b9 cur\n        if (cur != root) {\n            if (pre!!.left == cur)\n                pre.left = child\n            else\n                pre.right = child\n        } else {\n            // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n            root = child\n        }\n        // \u5b50\u8282\u70b9\u6570\u91cf = 2\n    } else {\n        // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n        var tmp = cur.right\n        while (tmp!!.left != null) {\n            tmp = tmp.left\n        }\n        // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n        remove(tmp._val)\n        // \u7528 tmp \u8986\u76d6 cur\n        cur._val = tmp._val\n    }\n}\n
        binary_search_tree.rb
        ### \u5220\u9664\u8282\u70b9 ###\ndef remove(num)\n  # \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n  return if @root.nil?\n\n  # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n  cur, pre = @root, nil\n  while !cur.nil?\n    # \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n    break if cur.val == num\n\n    pre = cur\n    # \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n    if cur.val < num\n      cur = cur.right\n    # \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n    else\n      cur = cur.left\n    end\n  end\n  # \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n  return if cur.nil?\n\n  # \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n  if cur.left.nil? || cur.right.nil?\n    # \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n    child = cur.left || cur.right\n    # \u5220\u9664\u8282\u70b9 cur\n    if cur != @root\n      if pre.left == cur\n        pre.left = child\n      else\n        pre.right = child\n      end\n    else\n      # \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n      @root = child\n    end\n  # \u5b50\u8282\u70b9\u6570\u91cf = 2\n  else\n    # \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n    tmp = cur.right\n    while !tmp.left.nil?\n      tmp = tmp.left\n    end\n    # \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n    remove(tmp.val)\n    # \u7528 tmp \u8986\u76d6 cur\n    cur.val = tmp.val\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_tree/binary_search_tree/#4","title":"4. \u00a0 \u4e2d\u5e8f\u904d\u5386\u6709\u5e8f","text":"

        \u5982\u56fe 7-22 \u6240\u793a\uff0c\u4e8c\u53c9\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u9075\u5faa\u201c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u201d\u7684\u904d\u5386\u987a\u5e8f\uff0c\u800c\u4e8c\u53c9\u641c\u7d22\u6811\u6ee1\u8db3\u201c\u5de6\u5b50\u8282\u70b9 \\(<\\) \u6839\u8282\u70b9 \\(<\\) \u53f3\u5b50\u8282\u70b9\u201d\u7684\u5927\u5c0f\u5173\u7cfb\u3002

        \u8fd9\u610f\u5473\u7740\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u8fdb\u884c\u4e2d\u5e8f\u904d\u5386\u65f6\uff0c\u603b\u662f\u4f1a\u4f18\u5148\u904d\u5386\u4e0b\u4e00\u4e2a\u6700\u5c0f\u8282\u70b9\uff0c\u4ece\u800c\u5f97\u51fa\u4e00\u4e2a\u91cd\u8981\u6027\u8d28\uff1a\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u662f\u5347\u5e8f\u7684\u3002

        \u5229\u7528\u4e2d\u5e8f\u904d\u5386\u5347\u5e8f\u7684\u6027\u8d28\uff0c\u6211\u4eec\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u83b7\u53d6\u6709\u5e8f\u6570\u636e\u4ec5\u9700 \\(O(n)\\) \u65f6\u95f4\uff0c\u65e0\u987b\u8fdb\u884c\u989d\u5916\u7684\u6392\u5e8f\u64cd\u4f5c\uff0c\u975e\u5e38\u9ad8\u6548\u3002

        \u56fe 7-22 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217

        "},{"location":"chapter_tree/binary_search_tree/#742","title":"7.4.2 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6548\u7387","text":"

        \u7ed9\u5b9a\u4e00\u7ec4\u6570\u636e\uff0c\u6211\u4eec\u8003\u8651\u4f7f\u7528\u6570\u7ec4\u6216\u4e8c\u53c9\u641c\u7d22\u6811\u5b58\u50a8\u3002\u89c2\u5bdf\u8868 7-2 \uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5404\u9879\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f\u5bf9\u6570\u9636\uff0c\u5177\u6709\u7a33\u5b9a\u4e14\u9ad8\u6548\u7684\u6027\u80fd\u3002\u53ea\u6709\u5728\u9ad8\u9891\u6dfb\u52a0\u3001\u4f4e\u9891\u67e5\u627e\u5220\u9664\u6570\u636e\u7684\u573a\u666f\u4e0b\uff0c\u6570\u7ec4\u6bd4\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6548\u7387\u66f4\u9ad8\u3002

        \u8868 7-2 \u00a0 \u6570\u7ec4\u4e0e\u641c\u7d22\u6811\u7684\u6548\u7387\u5bf9\u6bd4

        \u65e0\u5e8f\u6570\u7ec4 \u4e8c\u53c9\u641c\u7d22\u6811 \u67e5\u627e\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\) \u63d2\u5165\u5143\u7d20 \\(O(1)\\) \\(O(\\log n)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\)

        \u5728\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u662f\u201c\u5e73\u8861\u201d\u7684\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728 \\(\\log n\\) \u8f6e\u5faa\u73af\u5185\u67e5\u627e\u4efb\u610f\u8282\u70b9\u3002

        \u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u4e0d\u65ad\u5730\u63d2\u5165\u548c\u5220\u9664\u8282\u70b9\uff0c\u53ef\u80fd\u5bfc\u81f4\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u56fe 7-23 \u6240\u793a\u7684\u94fe\u8868\uff0c\u8fd9\u65f6\u5404\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4f1a\u9000\u5316\u4e3a \\(O(n)\\) \u3002

        \u56fe 7-23 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u9000\u5316

        "},{"location":"chapter_tree/binary_search_tree/#743","title":"7.4.3 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u5e38\u89c1\u5e94\u7528","text":"
        • \u7528\u4f5c\u7cfb\u7edf\u4e2d\u7684\u591a\u7ea7\u7d22\u5f15\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u67e5\u627e\u3001\u63d2\u5165\u3001\u5220\u9664\u64cd\u4f5c\u3002
        • \u4f5c\u4e3a\u67d0\u4e9b\u641c\u7d22\u7b97\u6cd5\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002
        • \u7528\u4e8e\u5b58\u50a8\u6570\u636e\u6d41\uff0c\u4ee5\u4fdd\u6301\u5176\u6709\u5e8f\u72b6\u6001\u3002
        "},{"location":"chapter_tree/binary_tree/","title":"7.1 \u00a0 \u4e8c\u53c9\u6811","text":"

        \u4e8c\u53c9\u6811\uff08binary tree\uff09\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u4ee3\u8868\u201c\u7956\u5148\u201d\u4e0e\u201c\u540e\u4ee3\u201d\u4e4b\u95f4\u7684\u6d3e\u751f\u5173\u7cfb\uff0c\u4f53\u73b0\u4e86\u201c\u4e00\u5206\u4e3a\u4e8c\u201d\u7684\u5206\u6cbb\u903b\u8f91\u3002\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u4e8c\u53c9\u6811\u7684\u57fa\u672c\u5355\u5143\u662f\u8282\u70b9\uff0c\u6bcf\u4e2a\u8282\u70b9\u5305\u542b\u503c\u3001\u5de6\u5b50\u8282\u70b9\u5f15\u7528\u548c\u53f3\u5b50\u8282\u70b9\u5f15\u7528\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        class TreeNode:\n    \"\"\"\u4e8c\u53c9\u6811\u8282\u70b9\u7c7b\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val                # \u8282\u70b9\u503c\n        self.left: TreeNode | None = None  # \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n        self.right: TreeNode | None = None # \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n
        /* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\n    int val;          // \u8282\u70b9\u503c\n    TreeNode *left;   // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n    TreeNode *right;  // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n};\n
        /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n    int val;         // \u8282\u70b9\u503c\n    TreeNode left;   // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n    TreeNode right;  // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n    TreeNode(int x) { val = x; }\n}\n
        /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode(int? x) {\n    public int? val = x;    // \u8282\u70b9\u503c\n    public TreeNode? left;  // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n    public TreeNode? right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n
        /* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype TreeNode struct {\n    Val   int\n    Left  *TreeNode\n    Right *TreeNode\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc NewTreeNode(v int) *TreeNode {\n    return &TreeNode{\n        Left:  nil, // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n        Right: nil, // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n        Val:   v,   // \u8282\u70b9\u503c\n    }\n}\n
        /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n    var val: Int // \u8282\u70b9\u503c\n    var left: TreeNode? // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n    var right: TreeNode? // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n\n    init(x: Int) {\n        val = x\n    }\n}\n
        /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n    val; // \u8282\u70b9\u503c\n    left; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n    right; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n    constructor(val, left, right) {\n        this.val = val === undefined ? 0 : val;\n        this.left = left === undefined ? null : left;\n        this.right = right === undefined ? null : right;\n    }\n}\n
        /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n    val: number;\n    left: TreeNode | null;\n    right: TreeNode | null;\n\n    constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {\n        this.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\n        this.left = left === undefined ? null : left; // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n        this.right = right === undefined ? null : right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n    }\n}\n
        /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n  int val;         // \u8282\u70b9\u503c\n  TreeNode? left;  // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n  TreeNode? right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n  TreeNode(this.val, [this.left, this.right]);\n}\n
        use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\n    val: i32,                               // \u8282\u70b9\u503c\n    left: Option<Rc<RefCell<TreeNode>>>,    // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n    right: Option<Rc<RefCell<TreeNode>>>,   // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n\nimpl TreeNode {\n    /* \u6784\u9020\u65b9\u6cd5 */\n    fn new(val: i32) -> Rc<RefCell<Self>> {\n        Rc::new(RefCell::new(Self {\n            val,\n            left: None,\n            right: None\n        }))\n    }\n}\n
        /* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\ntypedef struct TreeNode {\n    int val;                // \u8282\u70b9\u503c\n    int height;             // \u8282\u70b9\u9ad8\u5ea6\n    struct TreeNode *left;  // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n    struct TreeNode *right; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n} TreeNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nTreeNode *newTreeNode(int val) {\n    TreeNode *node;\n\n    node = (TreeNode *)malloc(sizeof(TreeNode));\n    node->val = val;\n    node->height = 0;\n    node->left = NULL;\n    node->right = NULL;\n    return node;\n}\n
        /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode(val _val: Int) {  // \u8282\u70b9\u503c\n    val left: TreeNode? = null   // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n    val right: TreeNode? = null  // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n
        ### \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b ###\nclass TreeNode\n  attr_accessor :val    # \u8282\u70b9\u503c\n  attr_accessor :left   # \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n  attr_accessor :right  # \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n\n  def initialize(val)\n    @val = val\n  end\nend\n

        \u6bcf\u4e2a\u8282\u70b9\u90fd\u6709\u4e24\u4e2a\u5f15\u7528\uff08\u6307\u9488\uff09\uff0c\u5206\u522b\u6307\u5411\u5de6\u5b50\u8282\u70b9\uff08left-child node\uff09\u548c\u53f3\u5b50\u8282\u70b9\uff08right-child node\uff09\uff0c\u8be5\u8282\u70b9\u88ab\u79f0\u4e3a\u8fd9\u4e24\u4e2a\u5b50\u8282\u70b9\u7684\u7236\u8282\u70b9\uff08parent node\uff09\u3002\u5f53\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u65f6\uff0c\u6211\u4eec\u5c06\u8be5\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u79f0\u4e3a\u8be5\u8282\u70b9\u7684\u5de6\u5b50\u6811\uff08left subtree\uff09\uff0c\u540c\u7406\u53ef\u5f97\u53f3\u5b50\u6811\uff08right subtree\uff09\u3002

        \u5728\u4e8c\u53c9\u6811\u4e2d\uff0c\u9664\u53f6\u8282\u70b9\u5916\uff0c\u5176\u4ed6\u6240\u6709\u8282\u70b9\u90fd\u5305\u542b\u5b50\u8282\u70b9\u548c\u975e\u7a7a\u5b50\u6811\u3002\u5982\u56fe 7-1 \u6240\u793a\uff0c\u5982\u679c\u5c06\u201c\u8282\u70b9 2\u201d\u89c6\u4e3a\u7236\u8282\u70b9\uff0c\u5219\u5176\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\u5206\u522b\u662f\u201c\u8282\u70b9 4\u201d\u548c\u201c\u8282\u70b9 5\u201d\uff0c\u5de6\u5b50\u6811\u662f\u201c\u8282\u70b9 4 \u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u201d\uff0c\u53f3\u5b50\u6811\u662f\u201c\u8282\u70b9 5 \u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u201d\u3002

        \u56fe 7-1 \u00a0 \u7236\u8282\u70b9\u3001\u5b50\u8282\u70b9\u3001\u5b50\u6811

        "},{"location":"chapter_tree/binary_tree/#711","title":"7.1.1 \u00a0 \u4e8c\u53c9\u6811\u5e38\u89c1\u672f\u8bed","text":"

        \u4e8c\u53c9\u6811\u7684\u5e38\u7528\u672f\u8bed\u5982\u56fe 7-2 \u6240\u793a\u3002

        • \u6839\u8282\u70b9\uff08root node\uff09\uff1a\u4f4d\u4e8e\u4e8c\u53c9\u6811\u9876\u5c42\u7684\u8282\u70b9\uff0c\u6ca1\u6709\u7236\u8282\u70b9\u3002
        • \u53f6\u8282\u70b9\uff08leaf node\uff09\uff1a\u6ca1\u6709\u5b50\u8282\u70b9\u7684\u8282\u70b9\uff0c\u5176\u4e24\u4e2a\u6307\u9488\u5747\u6307\u5411 None \u3002
        • \u8fb9\uff08edge\uff09\uff1a\u8fde\u63a5\u4e24\u4e2a\u8282\u70b9\u7684\u7ebf\u6bb5\uff0c\u5373\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\u3002
        • \u8282\u70b9\u6240\u5728\u7684\u5c42\uff08level\uff09\uff1a\u4ece\u9876\u81f3\u5e95\u9012\u589e\uff0c\u6839\u8282\u70b9\u6240\u5728\u5c42\u4e3a 1 \u3002
        • \u8282\u70b9\u7684\u5ea6\uff08degree\uff09\uff1a\u8282\u70b9\u7684\u5b50\u8282\u70b9\u7684\u6570\u91cf\u3002\u5728\u4e8c\u53c9\u6811\u4e2d\uff0c\u5ea6\u7684\u53d6\u503c\u8303\u56f4\u662f 0\u30011\u30012 \u3002
        • \u4e8c\u53c9\u6811\u7684\u9ad8\u5ea6\uff08height\uff09\uff1a\u4ece\u6839\u8282\u70b9\u5230\u6700\u8fdc\u53f6\u8282\u70b9\u6240\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\u3002
        • \u8282\u70b9\u7684\u6df1\u5ea6\uff08depth\uff09\uff1a\u4ece\u6839\u8282\u70b9\u5230\u8be5\u8282\u70b9\u6240\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\u3002
        • \u8282\u70b9\u7684\u9ad8\u5ea6\uff08height\uff09\uff1a\u4ece\u8ddd\u79bb\u8be5\u8282\u70b9\u6700\u8fdc\u7684\u53f6\u8282\u70b9\u5230\u8be5\u8282\u70b9\u6240\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\u3002

        \u56fe 7-2 \u00a0 \u4e8c\u53c9\u6811\u7684\u5e38\u7528\u672f\u8bed

        Tip

        \u8bf7\u6ce8\u610f\uff0c\u6211\u4eec\u901a\u5e38\u5c06\u201c\u9ad8\u5ea6\u201d\u548c\u201c\u6df1\u5ea6\u201d\u5b9a\u4e49\u4e3a\u201c\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\u201d\uff0c\u4f46\u6709\u4e9b\u9898\u76ee\u6216\u6559\u6750\u53ef\u80fd\u4f1a\u5c06\u5176\u5b9a\u4e49\u4e3a\u201c\u7ecf\u8fc7\u7684\u8282\u70b9\u7684\u6570\u91cf\u201d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u9ad8\u5ea6\u548c\u6df1\u5ea6\u90fd\u9700\u8981\u52a0 1 \u3002

        "},{"location":"chapter_tree/binary_tree/#712","title":"7.1.2 \u00a0 \u4e8c\u53c9\u6811\u57fa\u672c\u64cd\u4f5c","text":""},{"location":"chapter_tree/binary_tree/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u4e8c\u53c9\u6811","text":"

        \u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u9996\u5148\u521d\u59cb\u5316\u8282\u70b9\uff0c\u7136\u540e\u6784\u5efa\u5f15\u7528\uff08\u6307\u9488\uff09\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree.py
        # \u521d\u59cb\u5316\u4e8c\u53c9\u6811\n# \u521d\u59cb\u5316\u8282\u70b9\nn1 = TreeNode(val=1)\nn2 = TreeNode(val=2)\nn3 = TreeNode(val=3)\nn4 = TreeNode(val=4)\nn5 = TreeNode(val=5)\n# \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
        binary_tree.cpp
        /* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode* n1 = new TreeNode(1);\nTreeNode* n2 = new TreeNode(2);\nTreeNode* n3 = new TreeNode(3);\nTreeNode* n4 = new TreeNode(4);\nTreeNode* n5 = new TreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
        binary_tree.java
        // \u521d\u59cb\u5316\u8282\u70b9\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
        binary_tree.cs
        /* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode n1 = new(1);\nTreeNode n2 = new(2);\nTreeNode n3 = new(3);\nTreeNode n4 = new(4);\nTreeNode n5 = new(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
        binary_tree.go
        /* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nn1 := NewTreeNode(1)\nn2 := NewTreeNode(2)\nn3 := NewTreeNode(3)\nn4 := NewTreeNode(4)\nn5 := NewTreeNode(5)\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.Left = n2\nn1.Right = n3\nn2.Left = n4\nn2.Right = n5\n
        binary_tree.swift
        // \u521d\u59cb\u5316\u8282\u70b9\nlet n1 = TreeNode(x: 1)\nlet n2 = TreeNode(x: 2)\nlet n3 = TreeNode(x: 3)\nlet n4 = TreeNode(x: 4)\nlet n5 = TreeNode(x: 5)\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
        binary_tree.js
        /* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nlet n1 = new TreeNode(1),\n    n2 = new TreeNode(2),\n    n3 = new TreeNode(3),\n    n4 = new TreeNode(4),\n    n5 = new TreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
        binary_tree.ts
        /* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nlet n1 = new TreeNode(1),\n    n2 = new TreeNode(2),\n    n3 = new TreeNode(3),\n    n4 = new TreeNode(4),\n    n5 = new TreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
        binary_tree.dart
        /* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
        binary_tree.rs
        // \u521d\u59cb\u5316\u8282\u70b9\nlet n1 = TreeNode::new(1);\nlet n2 = TreeNode::new(2);\nlet n3 = TreeNode::new(3);\nlet n4 = TreeNode::new(4);\nlet n5 = TreeNode::new(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.borrow_mut().left = Some(n2.clone());\nn1.borrow_mut().right = Some(n3);\nn2.borrow_mut().left = Some(n4);\nn2.borrow_mut().right = Some(n5);\n
        binary_tree.c
        /* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode *n1 = newTreeNode(1);\nTreeNode *n2 = newTreeNode(2);\nTreeNode *n3 = newTreeNode(3);\nTreeNode *n4 = newTreeNode(4);\nTreeNode *n5 = newTreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
        binary_tree.kt
        // \u521d\u59cb\u5316\u8282\u70b9\nval n1 = TreeNode(1)\nval n2 = TreeNode(2)\nval n3 = TreeNode(3)\nval n4 = TreeNode(4)\nval n5 = TreeNode(5)\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
        binary_tree.rb
        # \u521d\u59cb\u5316\u4e8c\u53c9\u6811\n# \u521d\u59cb\u5316\u8282\u70b9\nn1 = TreeNode.new(1)\nn2 = TreeNode.new(2)\nn3 = TreeNode.new(3)\nn4 = TreeNode.new(4)\nn5 = TreeNode.new(5)\n# \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_tree/binary_tree/#2","title":"2. \u00a0 \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9","text":"

        \u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u5728\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u6307\u9488\u6765\u5b9e\u73b0\u3002\u56fe 7-3 \u7ed9\u51fa\u4e86\u4e00\u4e2a\u793a\u4f8b\u3002

        \u56fe 7-3 \u00a0 \u5728\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree.py
        # \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\np = TreeNode(0)\n# \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = p\np.left = n2\n# \u5220\u9664\u8282\u70b9 P\nn1.left = n2\n
        binary_tree.cpp
        /* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode* P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1->left = P;\nP->left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1->left = n2;\n// \u91ca\u653e\u5185\u5b58\ndelete P;\n
        binary_tree.java
        TreeNode P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
        binary_tree.cs
        /* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode P = new(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
        binary_tree.go
        /* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\np := NewTreeNode(0)\nn1.Left = p\np.Left = n2\n// \u5220\u9664\u8282\u70b9 P\nn1.Left = n2\n
        binary_tree.swift
        let P = TreeNode(x: 0)\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P\nP.left = n2\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2\n
        binary_tree.js
        /* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nlet P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
        binary_tree.ts
        /* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nconst P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
        binary_tree.dart
        /* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
        binary_tree.rs
        let p = TreeNode::new(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.borrow_mut().left = Some(p.clone());\np.borrow_mut().left = Some(n2.clone());\n// \u5220\u9664\u8282\u70b9 p\nn1.borrow_mut().left = Some(n2);\n
        binary_tree.c
        /* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode *P = newTreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1->left = P;\nP->left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1->left = n2;\n// \u91ca\u653e\u5185\u5b58\nfree(P);\n
        binary_tree.kt
        val P = TreeNode(0)\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P\nP.left = n2\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2\n
        binary_tree.rb
        # \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\n_p = TreeNode.new(0)\n# \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 _p\nn1.left = _p\n_p.left = n2\n# \u5220\u9664\u8282\u70b9\nn1.left = n2\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        Tip

        \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u63d2\u5165\u8282\u70b9\u53ef\u80fd\u4f1a\u6539\u53d8\u4e8c\u53c9\u6811\u7684\u539f\u6709\u903b\u8f91\u7ed3\u6784\uff0c\u800c\u5220\u9664\u8282\u70b9\u901a\u5e38\u610f\u5473\u7740\u5220\u9664\u8be5\u8282\u70b9\u53ca\u5176\u6240\u6709\u5b50\u6811\u3002\u56e0\u6b64\uff0c\u5728\u4e8c\u53c9\u6811\u4e2d\uff0c\u63d2\u5165\u4e0e\u5220\u9664\u901a\u5e38\u662f\u7531\u4e00\u5957\u64cd\u4f5c\u914d\u5408\u5b8c\u6210\u7684\uff0c\u4ee5\u5b9e\u73b0\u6709\u5b9e\u9645\u610f\u4e49\u7684\u64cd\u4f5c\u3002

        "},{"location":"chapter_tree/binary_tree/#713","title":"7.1.3 \u00a0 \u5e38\u89c1\u4e8c\u53c9\u6811\u7c7b\u578b","text":""},{"location":"chapter_tree/binary_tree/#1_1","title":"1. \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811","text":"

        \u5982\u56fe 7-4 \u6240\u793a\uff0c\u5b8c\u7f8e\u4e8c\u53c9\u6811\uff08perfect binary tree\uff09\u6240\u6709\u5c42\u7684\u8282\u70b9\u90fd\u88ab\u5b8c\u5168\u586b\u6ee1\u3002\u5728\u5b8c\u7f8e\u4e8c\u53c9\u6811\u4e2d\uff0c\u53f6\u8282\u70b9\u7684\u5ea6\u4e3a \\(0\\) \uff0c\u5176\u4f59\u6240\u6709\u8282\u70b9\u7684\u5ea6\u90fd\u4e3a \\(2\\) \uff1b\u82e5\u6811\u7684\u9ad8\u5ea6\u4e3a \\(h\\) \uff0c\u5219\u8282\u70b9\u603b\u6570\u4e3a \\(2^{h+1} - 1\\) \uff0c\u5448\u73b0\u6807\u51c6\u7684\u6307\u6570\u7ea7\u5173\u7cfb\uff0c\u53cd\u6620\u4e86\u81ea\u7136\u754c\u4e2d\u5e38\u89c1\u7684\u7ec6\u80de\u5206\u88c2\u73b0\u8c61\u3002

        Tip

        \u8bf7\u6ce8\u610f\uff0c\u5728\u4e2d\u6587\u793e\u533a\u4e2d\uff0c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u5e38\u88ab\u79f0\u4e3a\u6ee1\u4e8c\u53c9\u6811\u3002

        \u56fe 7-4 \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811

        "},{"location":"chapter_tree/binary_tree/#2_1","title":"2. \u00a0 \u5b8c\u5168\u4e8c\u53c9\u6811","text":"

        \u5982\u56fe 7-5 \u6240\u793a\uff0c\u5b8c\u5168\u4e8c\u53c9\u6811\uff08complete binary tree\uff09\u4ec5\u5141\u8bb8\u6700\u5e95\u5c42\u7684\u8282\u70b9\u4e0d\u5b8c\u5168\u586b\u6ee1\uff0c\u4e14\u6700\u5e95\u5c42\u7684\u8282\u70b9\u5fc5\u987b\u4ece\u5de6\u81f3\u53f3\u4f9d\u6b21\u8fde\u7eed\u586b\u5145\u3002\u8bf7\u6ce8\u610f\uff0c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u4e5f\u662f\u4e00\u68f5\u5b8c\u5168\u4e8c\u53c9\u6811\u3002

        \u56fe 7-5 \u00a0 \u5b8c\u5168\u4e8c\u53c9\u6811

        "},{"location":"chapter_tree/binary_tree/#3","title":"3. \u00a0 \u5b8c\u6ee1\u4e8c\u53c9\u6811","text":"

        \u5982\u56fe 7-6 \u6240\u793a\uff0c\u5b8c\u6ee1\u4e8c\u53c9\u6811\uff08full binary tree\uff09\u9664\u4e86\u53f6\u8282\u70b9\u4e4b\u5916\uff0c\u5176\u4f59\u6240\u6709\u8282\u70b9\u90fd\u6709\u4e24\u4e2a\u5b50\u8282\u70b9\u3002

        \u56fe 7-6 \u00a0 \u5b8c\u6ee1\u4e8c\u53c9\u6811

        "},{"location":"chapter_tree/binary_tree/#4","title":"4. \u00a0 \u5e73\u8861\u4e8c\u53c9\u6811","text":"

        \u5982\u56fe 7-7 \u6240\u793a\uff0c\u5e73\u8861\u4e8c\u53c9\u6811\uff08balanced binary tree\uff09\u4e2d\u4efb\u610f\u8282\u70b9\u7684\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u7684\u9ad8\u5ea6\u4e4b\u5dee\u7684\u7edd\u5bf9\u503c\u4e0d\u8d85\u8fc7 1 \u3002

        \u56fe 7-7 \u00a0 \u5e73\u8861\u4e8c\u53c9\u6811

        "},{"location":"chapter_tree/binary_tree/#714","title":"7.1.4 \u00a0 \u4e8c\u53c9\u6811\u7684\u9000\u5316","text":"

        \u56fe 7-8 \u5c55\u793a\u4e86\u4e8c\u53c9\u6811\u7684\u7406\u60f3\u7ed3\u6784\u4e0e\u9000\u5316\u7ed3\u6784\u3002\u5f53\u4e8c\u53c9\u6811\u7684\u6bcf\u5c42\u8282\u70b9\u90fd\u88ab\u586b\u6ee1\u65f6\uff0c\u8fbe\u5230\u201c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u201d\uff1b\u800c\u5f53\u6240\u6709\u8282\u70b9\u90fd\u504f\u5411\u4e00\u4fa7\u65f6\uff0c\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u201c\u94fe\u8868\u201d\u3002

        • \u5b8c\u7f8e\u4e8c\u53c9\u6811\u662f\u7406\u60f3\u60c5\u51b5\uff0c\u53ef\u4ee5\u5145\u5206\u53d1\u6325\u4e8c\u53c9\u6811\u201c\u5206\u6cbb\u201d\u7684\u4f18\u52bf\u3002
        • \u94fe\u8868\u5219\u662f\u53e6\u4e00\u4e2a\u6781\u7aef\uff0c\u5404\u9879\u64cd\u4f5c\u90fd\u53d8\u4e3a\u7ebf\u6027\u64cd\u4f5c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u9000\u5316\u81f3 \\(O(n)\\) \u3002

        \u56fe 7-8 \u00a0 \u4e8c\u53c9\u6811\u7684\u6700\u4f73\u7ed3\u6784\u4e0e\u6700\u5dee\u7ed3\u6784

        \u5982\u8868 7-1 \u6240\u793a\uff0c\u5728\u6700\u4f73\u7ed3\u6784\u548c\u6700\u5dee\u7ed3\u6784\u4e0b\uff0c\u4e8c\u53c9\u6811\u7684\u53f6\u8282\u70b9\u6570\u91cf\u3001\u8282\u70b9\u603b\u6570\u3001\u9ad8\u5ea6\u7b49\u8fbe\u5230\u6781\u5927\u503c\u6216\u6781\u5c0f\u503c\u3002

        \u8868 7-1 \u00a0 \u4e8c\u53c9\u6811\u7684\u6700\u4f73\u7ed3\u6784\u4e0e\u6700\u5dee\u7ed3\u6784

        \u5b8c\u7f8e\u4e8c\u53c9\u6811 \u94fe\u8868 \u7b2c \\(i\\) \u5c42\u7684\u8282\u70b9\u6570\u91cf \\(2^{i-1}\\) \\(1\\) \u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u6811\u7684\u53f6\u8282\u70b9\u6570\u91cf \\(2^h\\) \\(1\\) \u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u6811\u7684\u8282\u70b9\u603b\u6570 \\(2^{h+1} - 1\\) \\(h + 1\\) \u8282\u70b9\u603b\u6570\u4e3a \\(n\\) \u7684\u6811\u7684\u9ad8\u5ea6 \\(\\log_2 (n+1) - 1\\) \\(n - 1\\)"},{"location":"chapter_tree/binary_tree_traversal/","title":"7.2 \u00a0 \u4e8c\u53c9\u6811\u904d\u5386","text":"

        \u4ece\u7269\u7406\u7ed3\u6784\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u6811\u662f\u4e00\u79cd\u57fa\u4e8e\u94fe\u8868\u7684\u6570\u636e\u7ed3\u6784\uff0c\u56e0\u6b64\u5176\u904d\u5386\u65b9\u5f0f\u662f\u901a\u8fc7\u6307\u9488\u9010\u4e2a\u8bbf\u95ee\u8282\u70b9\u3002\u7136\u800c\uff0c\u6811\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u8fd9\u4f7f\u5f97\u904d\u5386\u6811\u6bd4\u904d\u5386\u94fe\u8868\u66f4\u52a0\u590d\u6742\uff0c\u9700\u8981\u501f\u52a9\u641c\u7d22\u7b97\u6cd5\u6765\u5b9e\u73b0\u3002

        \u4e8c\u53c9\u6811\u5e38\u89c1\u7684\u904d\u5386\u65b9\u5f0f\u5305\u62ec\u5c42\u5e8f\u904d\u5386\u3001\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u548c\u540e\u5e8f\u904d\u5386\u7b49\u3002

        "},{"location":"chapter_tree/binary_tree_traversal/#721","title":"7.2.1 \u00a0 \u5c42\u5e8f\u904d\u5386","text":"

        \u5982\u56fe 7-9 \u6240\u793a\uff0c\u5c42\u5e8f\u904d\u5386\uff08level-order traversal\uff09\u4ece\u9876\u90e8\u5230\u5e95\u90e8\u9010\u5c42\u904d\u5386\u4e8c\u53c9\u6811\uff0c\u5e76\u5728\u6bcf\u4e00\u5c42\u6309\u7167\u4ece\u5de6\u5230\u53f3\u7684\u987a\u5e8f\u8bbf\u95ee\u8282\u70b9\u3002

        \u5c42\u5e8f\u904d\u5386\u672c\u8d28\u4e0a\u5c5e\u4e8e\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\uff08breadth-first traversal\uff09\uff0c\u4e5f\u79f0\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff08breadth-first search, BFS\uff09\uff0c\u5b83\u4f53\u73b0\u4e86\u4e00\u79cd\u201c\u4e00\u5708\u4e00\u5708\u5411\u5916\u6269\u5c55\u201d\u7684\u9010\u5c42\u904d\u5386\u65b9\u5f0f\u3002

        \u56fe 7-9 \u00a0 \u4e8c\u53c9\u6811\u7684\u5c42\u5e8f\u904d\u5386

        "},{"location":"chapter_tree/binary_tree_traversal/#1","title":"1. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u901a\u5e38\u501f\u52a9\u201c\u961f\u5217\u201d\u6765\u5b9e\u73b0\u3002\u961f\u5217\u9075\u5faa\u201c\u5148\u8fdb\u5148\u51fa\u201d\u7684\u89c4\u5219\uff0c\u800c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u5219\u9075\u5faa\u201c\u9010\u5c42\u63a8\u8fdb\u201d\u7684\u89c4\u5219\uff0c\u4e24\u8005\u80cc\u540e\u7684\u601d\u60f3\u662f\u4e00\u81f4\u7684\u3002\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree_bfs.py
        def level_order(root: TreeNode | None) -> list[int]:\n    \"\"\"\u5c42\u5e8f\u904d\u5386\"\"\"\n    # \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n    queue: deque[TreeNode] = deque()\n    queue.append(root)\n    # \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n    res = []\n    while queue:\n        node: TreeNode = queue.popleft()  # \u961f\u5217\u51fa\u961f\n        res.append(node.val)  # \u4fdd\u5b58\u8282\u70b9\u503c\n        if node.left is not None:\n            queue.append(node.left)  # \u5de6\u5b50\u8282\u70b9\u5165\u961f\n        if node.right is not None:\n            queue.append(node.right)  # \u53f3\u5b50\u8282\u70b9\u5165\u961f\n    return res\n
        binary_tree_bfs.cpp
        /* \u5c42\u5e8f\u904d\u5386 */\nvector<int> levelOrder(TreeNode *root) {\n    // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n    queue<TreeNode *> queue;\n    queue.push(root);\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n    vector<int> vec;\n    while (!queue.empty()) {\n        TreeNode *node = queue.front();\n        queue.pop();              // \u961f\u5217\u51fa\u961f\n        vec.push_back(node->val); // \u4fdd\u5b58\u8282\u70b9\u503c\n        if (node->left != nullptr)\n            queue.push(node->left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n        if (node->right != nullptr)\n            queue.push(node->right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n    }\n    return vec;\n}\n
        binary_tree_bfs.java
        /* \u5c42\u5e8f\u904d\u5386 */\nList<Integer> levelOrder(TreeNode root) {\n    // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n    Queue<TreeNode> queue = new LinkedList<>();\n    queue.add(root);\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n    List<Integer> list = new ArrayList<>();\n    while (!queue.isEmpty()) {\n        TreeNode node = queue.poll(); // \u961f\u5217\u51fa\u961f\n        list.add(node.val);           // \u4fdd\u5b58\u8282\u70b9\u503c\n        if (node.left != null)\n            queue.offer(node.left);   // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n        if (node.right != null)\n            queue.offer(node.right);  // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n    }\n    return list;\n}\n
        binary_tree_bfs.cs
        /* \u5c42\u5e8f\u904d\u5386 */\nList<int> LevelOrder(TreeNode root) {\n    // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n    Queue<TreeNode> queue = new();\n    queue.Enqueue(root);\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n    List<int> list = [];\n    while (queue.Count != 0) {\n        TreeNode node = queue.Dequeue(); // \u961f\u5217\u51fa\u961f\n        list.Add(node.val!.Value);       // \u4fdd\u5b58\u8282\u70b9\u503c\n        if (node.left != null)\n            queue.Enqueue(node.left);    // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n        if (node.right != null)\n            queue.Enqueue(node.right);   // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n    }\n    return list;\n}\n
        binary_tree_bfs.go
        /* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder(root *TreeNode) []any {\n    // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n    queue := list.New()\n    queue.PushBack(root)\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5207\u7247\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n    nums := make([]any, 0)\n    for queue.Len() > 0 {\n        // \u961f\u5217\u51fa\u961f\n        node := queue.Remove(queue.Front()).(*TreeNode)\n        // \u4fdd\u5b58\u8282\u70b9\u503c\n        nums = append(nums, node.Val)\n        if node.Left != nil {\n            // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n            queue.PushBack(node.Left)\n        }\n        if node.Right != nil {\n            // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n            queue.PushBack(node.Right)\n        }\n    }\n    return nums\n}\n
        binary_tree_bfs.swift
        /* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder(root: TreeNode) -> [Int] {\n    // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n    var queue: [TreeNode] = [root]\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n    var list: [Int] = []\n    while !queue.isEmpty {\n        let node = queue.removeFirst() // \u961f\u5217\u51fa\u961f\n        list.append(node.val) // \u4fdd\u5b58\u8282\u70b9\u503c\n        if let left = node.left {\n            queue.append(left) // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n        }\n        if let right = node.right {\n            queue.append(right) // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n        }\n    }\n    return list\n}\n
        binary_tree_bfs.js
        /* \u5c42\u5e8f\u904d\u5386 */\nfunction levelOrder(root) {\n    // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n    const queue = [root];\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n    const list = [];\n    while (queue.length) {\n        let node = queue.shift(); // \u961f\u5217\u51fa\u961f\n        list.push(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n        if (node.left) queue.push(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n        if (node.right) queue.push(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n    }\n    return list;\n}\n
        binary_tree_bfs.ts
        /* \u5c42\u5e8f\u904d\u5386 */\nfunction levelOrder(root: TreeNode | null): number[] {\n    // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n    const queue = [root];\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n    const list: number[] = [];\n    while (queue.length) {\n        let node = queue.shift() as TreeNode; // \u961f\u5217\u51fa\u961f\n        list.push(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n        if (node.left) {\n            queue.push(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n        }\n        if (node.right) {\n            queue.push(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n        }\n    }\n    return list;\n}\n
        binary_tree_bfs.dart
        /* \u5c42\u5e8f\u904d\u5386 */\nList<int> levelOrder(TreeNode? root) {\n  // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n  Queue<TreeNode?> queue = Queue();\n  queue.add(root);\n  // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n  List<int> res = [];\n  while (queue.isNotEmpty) {\n    TreeNode? node = queue.removeFirst(); // \u961f\u5217\u51fa\u961f\n    res.add(node!.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n    if (node.left != null) queue.add(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n    if (node.right != null) queue.add(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n  }\n  return res;\n}\n
        binary_tree_bfs.rs
        /* \u5c42\u5e8f\u904d\u5386 */\nfn level_order(root: &Rc<RefCell<TreeNode>>) -> Vec<i32> {\n    // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n    let mut que = VecDeque::new();\n    que.push_back(root.clone());\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n    let mut vec = Vec::new();\n\n    while let Some(node) = que.pop_front() {\n        // \u961f\u5217\u51fa\u961f\n        vec.push(node.borrow().val); // \u4fdd\u5b58\u8282\u70b9\u503c\n        if let Some(left) = node.borrow().left.as_ref() {\n            que.push_back(left.clone()); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n        }\n        if let Some(right) = node.borrow().right.as_ref() {\n            que.push_back(right.clone()); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n        };\n    }\n    vec\n}\n
        binary_tree_bfs.c
        /* \u5c42\u5e8f\u904d\u5386 */\nint *levelOrder(TreeNode *root, int *size) {\n    /* \u8f85\u52a9\u961f\u5217 */\n    int front, rear;\n    int index, *arr;\n    TreeNode *node;\n    TreeNode **queue;\n\n    /* \u8f85\u52a9\u961f\u5217 */\n    queue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_SIZE);\n    // \u961f\u5217\u6307\u9488\n    front = 0, rear = 0;\n    // \u52a0\u5165\u6839\u8282\u70b9\n    queue[rear++] = root;\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n    /* \u8f85\u52a9\u6570\u7ec4 */\n    arr = (int *)malloc(sizeof(int) * MAX_SIZE);\n    // \u6570\u7ec4\u6307\u9488\n    index = 0;\n    while (front < rear) {\n        // \u961f\u5217\u51fa\u961f\n        node = queue[front++];\n        // \u4fdd\u5b58\u8282\u70b9\u503c\n        arr[index++] = node->val;\n        if (node->left != NULL) {\n            // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n            queue[rear++] = node->left;\n        }\n        if (node->right != NULL) {\n            // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n            queue[rear++] = node->right;\n        }\n    }\n    // \u66f4\u65b0\u6570\u7ec4\u957f\u5ea6\u7684\u503c\n    *size = index;\n    arr = realloc(arr, sizeof(int) * (*size));\n\n    // \u91ca\u653e\u8f85\u52a9\u6570\u7ec4\u7a7a\u95f4\n    free(queue);\n    return arr;\n}\n
        binary_tree_bfs.kt
        /* \u5c42\u5e8f\u904d\u5386 */\nfun levelOrder(root: TreeNode?): MutableList<Int> {\n    // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n    val queue = LinkedList<TreeNode?>()\n    queue.add(root)\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n    val list = mutableListOf<Int>()\n    while (queue.isNotEmpty()) {\n        val node = queue.poll()      // \u961f\u5217\u51fa\u961f\n        list.add(node?._val!!)       // \u4fdd\u5b58\u8282\u70b9\u503c\n        if (node.left != null)\n            queue.offer(node.left)   // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n        if (node.right != null)\n            queue.offer(node.right)  // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n    }\n    return list\n}\n
        binary_tree_bfs.rb
        ### \u5c42\u5e8f\u904d\u5386 ###\ndef level_order(root)\n  # \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n  queue = [root]\n  # \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n  res = []\n  while !queue.empty?\n    node = queue.shift # \u961f\u5217\u51fa\u961f\n    res << node.val # \u4fdd\u5b58\u8282\u70b9\u503c\n    queue << node.left unless node.left.nil? # \u5de6\u5b50\u8282\u70b9\u5165\u961f\n    queue << node.right unless node.right.nil? # \u53f3\u5b50\u8282\u70b9\u5165\u961f\n  end\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_tree/binary_tree_traversal/#2","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"
        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff1a\u6240\u6709\u8282\u70b9\u88ab\u8bbf\u95ee\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff0c\u5176\u4e2d \\(n\\) \u4e3a\u8282\u70b9\u6570\u91cf\u3002
        • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff1a\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u5373\u6ee1\u4e8c\u53c9\u6811\u65f6\uff0c\u904d\u5386\u5230\u6700\u5e95\u5c42\u4e4b\u524d\uff0c\u961f\u5217\u4e2d\u6700\u591a\u540c\u65f6\u5b58\u5728 \\((n + 1) / 2\\) \u4e2a\u8282\u70b9\uff0c\u5360\u7528 \\(O(n)\\) \u7a7a\u95f4\u3002
        "},{"location":"chapter_tree/binary_tree_traversal/#722","title":"7.2.2 \u00a0 \u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386","text":"

        \u76f8\u5e94\u5730\uff0c\u524d\u5e8f\u3001\u4e2d\u5e8f\u548c\u540e\u5e8f\u904d\u5386\u90fd\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u904d\u5386\uff08depth-first traversal\uff09\uff0c\u4e5f\u79f0\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff08depth-first search, DFS\uff09\uff0c\u5b83\u4f53\u73b0\u4e86\u4e00\u79cd\u201c\u5148\u8d70\u5230\u5c3d\u5934\uff0c\u518d\u56de\u6eaf\u7ee7\u7eed\u201d\u7684\u904d\u5386\u65b9\u5f0f\u3002

        \u56fe 7-10 \u5c55\u793a\u4e86\u5bf9\u4e8c\u53c9\u6811\u8fdb\u884c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u5de5\u4f5c\u539f\u7406\u3002\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u5c31\u50cf\u662f\u7ed5\u7740\u6574\u68f5\u4e8c\u53c9\u6811\u7684\u5916\u56f4\u201c\u8d70\u201d\u4e00\u5708\uff0c\u5728\u6bcf\u4e2a\u8282\u70b9\u90fd\u4f1a\u9047\u5230\u4e09\u4e2a\u4f4d\u7f6e\uff0c\u5206\u522b\u5bf9\u5e94\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u548c\u540e\u5e8f\u904d\u5386\u3002

        \u56fe 7-10 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386

        "},{"location":"chapter_tree/binary_tree_traversal/#1_1","title":"1. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree_dfs.py
        def pre_order(root: TreeNode | None):\n    \"\"\"\u524d\u5e8f\u904d\u5386\"\"\"\n    if root is None:\n        return\n    # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n    res.append(root.val)\n    pre_order(root=root.left)\n    pre_order(root=root.right)\n\ndef in_order(root: TreeNode | None):\n    \"\"\"\u4e2d\u5e8f\u904d\u5386\"\"\"\n    if root is None:\n        return\n    # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n    in_order(root=root.left)\n    res.append(root.val)\n    in_order(root=root.right)\n\ndef post_order(root: TreeNode | None):\n    \"\"\"\u540e\u5e8f\u904d\u5386\"\"\"\n    if root is None:\n        return\n    # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n    post_order(root=root.left)\n    post_order(root=root.right)\n    res.append(root.val)\n
        binary_tree_dfs.cpp
        /* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode *root) {\n    if (root == nullptr)\n        return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n    vec.push_back(root->val);\n    preOrder(root->left);\n    preOrder(root->right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode *root) {\n    if (root == nullptr)\n        return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n    inOrder(root->left);\n    vec.push_back(root->val);\n    inOrder(root->right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode *root) {\n    if (root == nullptr)\n        return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n    postOrder(root->left);\n    postOrder(root->right);\n    vec.push_back(root->val);\n}\n
        binary_tree_dfs.java
        /* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode root) {\n    if (root == null)\n        return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n    list.add(root.val);\n    preOrder(root.left);\n    preOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode root) {\n    if (root == null)\n        return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n    inOrder(root.left);\n    list.add(root.val);\n    inOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode root) {\n    if (root == null)\n        return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n    postOrder(root.left);\n    postOrder(root.right);\n    list.add(root.val);\n}\n
        binary_tree_dfs.cs
        /* \u524d\u5e8f\u904d\u5386 */\nvoid PreOrder(TreeNode? root) {\n    if (root == null) return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n    list.Add(root.val!.Value);\n    PreOrder(root.left);\n    PreOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid InOrder(TreeNode? root) {\n    if (root == null) return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n    InOrder(root.left);\n    list.Add(root.val!.Value);\n    InOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid PostOrder(TreeNode? root) {\n    if (root == null) return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n    PostOrder(root.left);\n    PostOrder(root.right);\n    list.Add(root.val!.Value);\n}\n
        binary_tree_dfs.go
        /* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder(node *TreeNode) {\n    if node == nil {\n        return\n    }\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n    nums = append(nums, node.Val)\n    preOrder(node.Left)\n    preOrder(node.Right)\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder(node *TreeNode) {\n    if node == nil {\n        return\n    }\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n    inOrder(node.Left)\n    nums = append(nums, node.Val)\n    inOrder(node.Right)\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder(node *TreeNode) {\n    if node == nil {\n        return\n    }\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n    postOrder(node.Left)\n    postOrder(node.Right)\n    nums = append(nums, node.Val)\n}\n
        binary_tree_dfs.swift
        /* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n    list.append(root.val)\n    preOrder(root: root.left)\n    preOrder(root: root.right)\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n    inOrder(root: root.left)\n    list.append(root.val)\n    inOrder(root: root.right)\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n    postOrder(root: root.left)\n    postOrder(root: root.right)\n    list.append(root.val)\n}\n
        binary_tree_dfs.js
        /* \u524d\u5e8f\u904d\u5386 */\nfunction preOrder(root) {\n    if (root === null) return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n    list.push(root.val);\n    preOrder(root.left);\n    preOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunction inOrder(root) {\n    if (root === null) return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n    inOrder(root.left);\n    list.push(root.val);\n    inOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunction postOrder(root) {\n    if (root === null) return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n    postOrder(root.left);\n    postOrder(root.right);\n    list.push(root.val);\n}\n
        binary_tree_dfs.ts
        /* \u524d\u5e8f\u904d\u5386 */\nfunction preOrder(root: TreeNode | null): void {\n    if (root === null) {\n        return;\n    }\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n    list.push(root.val);\n    preOrder(root.left);\n    preOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunction inOrder(root: TreeNode | null): void {\n    if (root === null) {\n        return;\n    }\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n    inOrder(root.left);\n    list.push(root.val);\n    inOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunction postOrder(root: TreeNode | null): void {\n    if (root === null) {\n        return;\n    }\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n    postOrder(root.left);\n    postOrder(root.right);\n    list.push(root.val);\n}\n
        binary_tree_dfs.dart
        /* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode? node) {\n  if (node == null) return;\n  // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n  list.add(node.val);\n  preOrder(node.left);\n  preOrder(node.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode? node) {\n  if (node == null) return;\n  // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n  inOrder(node.left);\n  list.add(node.val);\n  inOrder(node.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode? node) {\n  if (node == null) return;\n  // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n  postOrder(node.left);\n  postOrder(node.right);\n  list.add(node.val);\n}\n
        binary_tree_dfs.rs
        /* \u524d\u5e8f\u904d\u5386 */\nfn pre_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n    let mut result = vec![];\n\n    fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {\n        if let Some(node) = root {\n            // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n            let node = node.borrow();\n            res.push(node.val);\n            dfs(node.left.as_ref(), res);\n            dfs(node.right.as_ref(), res);\n        }\n    }\n    dfs(root, &mut result);\n\n    result\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfn in_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n    let mut result = vec![];\n\n    fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {\n        if let Some(node) = root {\n            // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n            let node = node.borrow();\n            dfs(node.left.as_ref(), res);\n            res.push(node.val);\n            dfs(node.right.as_ref(), res);\n        }\n    }\n    dfs(root, &mut result);\n\n    result\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfn post_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n    let mut result = vec![];\n\n    fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {\n        if let Some(node) = root {\n            // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n            let node = node.borrow();\n            dfs(node.left.as_ref(), res);\n            dfs(node.right.as_ref(), res);\n            res.push(node.val);\n        }\n    }\n\n    dfs(root, &mut result);\n\n    result\n}\n
        binary_tree_dfs.c
        /* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode *root, int *size) {\n    if (root == NULL)\n        return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n    arr[(*size)++] = root->val;\n    preOrder(root->left, size);\n    preOrder(root->right, size);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode *root, int *size) {\n    if (root == NULL)\n        return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n    inOrder(root->left, size);\n    arr[(*size)++] = root->val;\n    inOrder(root->right, size);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode *root, int *size) {\n    if (root == NULL)\n        return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n    postOrder(root->left, size);\n    postOrder(root->right, size);\n    arr[(*size)++] = root->val;\n}\n
        binary_tree_dfs.kt
        /* \u524d\u5e8f\u904d\u5386 */\nfun preOrder(root: TreeNode?) {\n    if (root == null) return\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n    list.add(root._val)\n    preOrder(root.left)\n    preOrder(root.right)\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfun inOrder(root: TreeNode?) {\n    if (root == null) return\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n    inOrder(root.left)\n    list.add(root._val)\n    inOrder(root.right)\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfun postOrder(root: TreeNode?) {\n    if (root == null) return\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n    postOrder(root.left)\n    postOrder(root.right)\n    list.add(root._val)\n}\n
        binary_tree_dfs.rb
        ### \u524d\u5e8f\u904d\u5386 ###\ndef pre_order(root)\n  return if root.nil?\n\n  # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n  $res << root.val\n  pre_order(root.left)\n  pre_order(root.right)\nend\n\n### \u4e2d\u5e8f\u904d\u5386 ###\ndef in_order(root)\n  return if root.nil?\n\n  # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n  in_order(root.left)\n  $res << root.val\n  in_order(root.right)\nend\n\n### \u540e\u5e8f\u904d\u5386 ###\ndef post_order(root)\n  return if root.nil?\n\n  # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n  post_order(root.left)\n  post_order(root.right)\n  $res << root.val\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        Tip

        \u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u4e5f\u53ef\u4ee5\u57fa\u4e8e\u8fed\u4ee3\u5b9e\u73b0\uff0c\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u7814\u7a76\u3002

        \u56fe 7-11 \u5c55\u793a\u4e86\u524d\u5e8f\u904d\u5386\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b\uff0c\u5176\u53ef\u5206\u4e3a\u201c\u9012\u201d\u548c\u201c\u5f52\u201d\u4e24\u4e2a\u9006\u5411\u7684\u90e8\u5206\u3002

        1. \u201c\u9012\u201d\u8868\u793a\u5f00\u542f\u65b0\u65b9\u6cd5\uff0c\u7a0b\u5e8f\u5728\u6b64\u8fc7\u7a0b\u4e2d\u8bbf\u95ee\u4e0b\u4e00\u4e2a\u8282\u70b9\u3002
        2. \u201c\u5f52\u201d\u8868\u793a\u51fd\u6570\u8fd4\u56de\uff0c\u4ee3\u8868\u5f53\u524d\u8282\u70b9\u5df2\u7ecf\u8bbf\u95ee\u5b8c\u6bd5\u3002
        <1><2><3><4><5><6><7><8><9><10><11>

        \u56fe 7-11 \u00a0 \u524d\u5e8f\u904d\u5386\u7684\u9012\u5f52\u8fc7\u7a0b

        "},{"location":"chapter_tree/binary_tree_traversal/#2_1","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"
        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff1a\u6240\u6709\u8282\u70b9\u88ab\u8bbf\u95ee\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002
        • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff1a\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u5373\u6811\u9000\u5316\u4e3a\u94fe\u8868\u65f6\uff0c\u9012\u5f52\u6df1\u5ea6\u8fbe\u5230 \\(n\\) \uff0c\u7cfb\u7edf\u5360\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002
        "},{"location":"chapter_tree/summary/","title":"7.6 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_tree/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u4e8c\u53c9\u6811\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u4f53\u73b0\u201c\u4e00\u5206\u4e3a\u4e8c\u201d\u7684\u5206\u6cbb\u903b\u8f91\u3002\u6bcf\u4e2a\u4e8c\u53c9\u6811\u8282\u70b9\u5305\u542b\u4e00\u4e2a\u503c\u4ee5\u53ca\u4e24\u4e2a\u6307\u9488\uff0c\u5206\u522b\u6307\u5411\u5176\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\u3002
        • \u5bf9\u4e8e\u4e8c\u53c9\u6811\u4e2d\u7684\u67d0\u4e2a\u8282\u70b9\uff0c\u5176\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\u53ca\u5176\u4ee5\u4e0b\u5f62\u6210\u7684\u6811\u88ab\u79f0\u4e3a\u8be5\u8282\u70b9\u7684\u5de6\uff08\u53f3\uff09\u5b50\u6811\u3002
        • \u4e8c\u53c9\u6811\u7684\u76f8\u5173\u672f\u8bed\u5305\u62ec\u6839\u8282\u70b9\u3001\u53f6\u8282\u70b9\u3001\u5c42\u3001\u5ea6\u3001\u8fb9\u3001\u9ad8\u5ea6\u548c\u6df1\u5ea6\u7b49\u3002
        • \u4e8c\u53c9\u6811\u7684\u521d\u59cb\u5316\u3001\u8282\u70b9\u63d2\u5165\u548c\u8282\u70b9\u5220\u9664\u64cd\u4f5c\u4e0e\u94fe\u8868\u64cd\u4f5c\u65b9\u6cd5\u7c7b\u4f3c\u3002
        • \u5e38\u89c1\u7684\u4e8c\u53c9\u6811\u7c7b\u578b\u6709\u5b8c\u7f8e\u4e8c\u53c9\u6811\u3001\u5b8c\u5168\u4e8c\u53c9\u6811\u3001\u5b8c\u6ee1\u4e8c\u53c9\u6811\u548c\u5e73\u8861\u4e8c\u53c9\u6811\u3002\u5b8c\u7f8e\u4e8c\u53c9\u6811\u662f\u6700\u7406\u60f3\u7684\u72b6\u6001\uff0c\u800c\u94fe\u8868\u662f\u9000\u5316\u540e\u7684\u6700\u5dee\u72b6\u6001\u3002
        • \u4e8c\u53c9\u6811\u53ef\u4ee5\u7528\u6570\u7ec4\u8868\u793a\uff0c\u65b9\u6cd5\u662f\u5c06\u8282\u70b9\u503c\u548c\u7a7a\u4f4d\u6309\u5c42\u5e8f\u904d\u5386\u987a\u5e8f\u6392\u5217\uff0c\u5e76\u6839\u636e\u7236\u8282\u70b9\u4e0e\u5b50\u8282\u70b9\u4e4b\u95f4\u7684\u7d22\u5f15\u6620\u5c04\u5173\u7cfb\u6765\u5b9e\u73b0\u6307\u9488\u3002
        • \u4e8c\u53c9\u6811\u7684\u5c42\u5e8f\u904d\u5386\u662f\u4e00\u79cd\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u65b9\u6cd5\uff0c\u5b83\u4f53\u73b0\u4e86\u201c\u4e00\u5708\u4e00\u5708\u5411\u5916\u6269\u5c55\u201d\u7684\u9010\u5c42\u904d\u5386\u65b9\u5f0f\uff0c\u901a\u5e38\u901a\u8fc7\u961f\u5217\u6765\u5b9e\u73b0\u3002
        • \u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386\u7686\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff0c\u5b83\u4eec\u4f53\u73b0\u4e86\u201c\u5148\u8d70\u5230\u5c3d\u5934\uff0c\u518d\u56de\u6eaf\u7ee7\u7eed\u201d\u7684\u904d\u5386\u65b9\u5f0f\uff0c\u901a\u5e38\u4f7f\u7528\u9012\u5f52\u6765\u5b9e\u73b0\u3002
        • \u4e8c\u53c9\u641c\u7d22\u6811\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u5143\u7d20\u67e5\u627e\u6570\u636e\u7ed3\u6784\uff0c\u5176\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(\\log n)\\) \u3002\u5f53\u4e8c\u53c9\u641c\u7d22\u6811\u9000\u5316\u4e3a\u94fe\u8868\u65f6\uff0c\u5404\u9879\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u52a3\u5316\u81f3 \\(O(n)\\) \u3002
        • AVL \u6811\uff0c\u4e5f\u79f0\u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u5b83\u901a\u8fc7\u65cb\u8f6c\u64cd\u4f5c\u786e\u4fdd\u5728\u4e0d\u65ad\u63d2\u5165\u548c\u5220\u9664\u8282\u70b9\u540e\u6811\u4ecd\u7136\u4fdd\u6301\u5e73\u8861\u3002
        • AVL \u6811\u7684\u65cb\u8f6c\u64cd\u4f5c\u5305\u62ec\u53f3\u65cb\u3001\u5de6\u65cb\u3001\u5148\u53f3\u65cb\u518d\u5de6\u65cb\u3001\u5148\u5de6\u65cb\u518d\u53f3\u65cb\u3002\u5728\u63d2\u5165\u6216\u5220\u9664\u8282\u70b9\u540e\uff0cAVL \u6811\u4f1a\u4ece\u5e95\u5411\u9876\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\u3002
        "},{"location":"chapter_tree/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u5bf9\u4e8e\u53ea\u6709\u4e00\u4e2a\u8282\u70b9\u7684\u4e8c\u53c9\u6811\uff0c\u6811\u7684\u9ad8\u5ea6\u548c\u6839\u8282\u70b9\u7684\u6df1\u5ea6\u90fd\u662f \\(0\\) \u5417\uff1f

        \u662f\u7684\uff0c\u56e0\u4e3a\u9ad8\u5ea6\u548c\u6df1\u5ea6\u901a\u5e38\u5b9a\u4e49\u4e3a\u201c\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\u201d\u3002

        Q\uff1a\u4e8c\u53c9\u6811\u4e2d\u7684\u63d2\u5165\u4e0e\u5220\u9664\u4e00\u822c\u7531\u4e00\u5957\u64cd\u4f5c\u914d\u5408\u5b8c\u6210\uff0c\u8fd9\u91cc\u7684\u201c\u4e00\u5957\u64cd\u4f5c\u201d\u6307\u4ec0\u4e48\u5462\uff1f\u53ef\u4ee5\u7406\u89e3\u4e3a\u8d44\u6e90\u7684\u5b50\u8282\u70b9\u7684\u8d44\u6e90\u91ca\u653e\u5417\uff1f

        \u62ff\u4e8c\u53c9\u641c\u7d22\u6811\u6765\u4e3e\u4f8b\uff0c\u5220\u9664\u8282\u70b9\u64cd\u4f5c\u8981\u5206\u4e09\u79cd\u60c5\u51b5\u5904\u7406\uff0c\u5176\u4e2d\u6bcf\u79cd\u60c5\u51b5\u90fd\u9700\u8981\u8fdb\u884c\u591a\u4e2a\u6b65\u9aa4\u7684\u8282\u70b9\u64cd\u4f5c\u3002

        Q\uff1a\u4e3a\u4ec0\u4e48 DFS \u904d\u5386\u4e8c\u53c9\u6811\u6709\u524d\u3001\u4e2d\u3001\u540e\u4e09\u79cd\u987a\u5e8f\uff0c\u5206\u522b\u6709\u4ec0\u4e48\u7528\u5462\uff1f

        \u4e0e\u987a\u5e8f\u548c\u9006\u5e8f\u904d\u5386\u6570\u7ec4\u7c7b\u4f3c\uff0c\u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386\u662f\u4e09\u79cd\u4e8c\u53c9\u6811\u904d\u5386\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u5f97\u5230\u4e00\u4e2a\u7279\u5b9a\u987a\u5e8f\u7684\u904d\u5386\u7ed3\u679c\u3002\u4f8b\u5982\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\uff0c\u7531\u4e8e\u8282\u70b9\u5927\u5c0f\u6ee1\u8db3 \u5de6\u5b50\u8282\u70b9\u503c < \u6839\u8282\u70b9\u503c < \u53f3\u5b50\u8282\u70b9\u503c \uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u8981\u6309\u7167\u201c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u201d\u7684\u4f18\u5148\u7ea7\u904d\u5386\u6811\uff0c\u5c31\u53ef\u4ee5\u83b7\u5f97\u6709\u5e8f\u7684\u8282\u70b9\u5e8f\u5217\u3002

        Q\uff1a\u53f3\u65cb\u64cd\u4f5c\u662f\u5904\u7406\u5931\u8861\u8282\u70b9 node\u3001child\u3001grand_child \u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u90a3 node \u7684\u7236\u8282\u70b9\u548c node \u539f\u6765\u7684\u8fde\u63a5\u4e0d\u9700\u8981\u7ef4\u62a4\u5417\uff1f\u53f3\u65cb\u64cd\u4f5c\u540e\u5c82\u4e0d\u662f\u65ad\u6389\u4e86\uff1f

        \u6211\u4eec\u9700\u8981\u4ece\u9012\u5f52\u7684\u89c6\u89d2\u6765\u770b\u8fd9\u4e2a\u95ee\u9898\u3002\u53f3\u65cb\u64cd\u4f5c right_rotate(root) \u4f20\u5165\u7684\u662f\u5b50\u6811\u7684\u6839\u8282\u70b9\uff0c\u6700\u7ec8 return child \u8fd4\u56de\u65cb\u8f6c\u4e4b\u540e\u7684\u5b50\u6811\u7684\u6839\u8282\u70b9\u3002\u5b50\u6811\u7684\u6839\u8282\u70b9\u548c\u5176\u7236\u8282\u70b9\u7684\u8fde\u63a5\u662f\u5728\u8be5\u51fd\u6570\u8fd4\u56de\u540e\u5b8c\u6210\u7684\uff0c\u4e0d\u5c5e\u4e8e\u53f3\u65cb\u64cd\u4f5c\u7684\u7ef4\u62a4\u8303\u56f4\u3002

        Q\uff1a\u5728 C++ \u4e2d\uff0c\u51fd\u6570\u88ab\u5212\u5206\u5230 private \u548c public \u4e2d\uff0c\u8fd9\u65b9\u9762\u6709\u4ec0\u4e48\u8003\u91cf\u5417\uff1f\u4e3a\u4ec0\u4e48\u8981\u5c06 height() \u51fd\u6570\u548c updateHeight() \u51fd\u6570\u5206\u522b\u653e\u5728 public \u548c private \u4e2d\u5462\uff1f

        \u4e3b\u8981\u770b\u65b9\u6cd5\u7684\u4f7f\u7528\u8303\u56f4\uff0c\u5982\u679c\u65b9\u6cd5\u53ea\u5728\u7c7b\u5185\u90e8\u4f7f\u7528\uff0c\u90a3\u4e48\u5c31\u8bbe\u8ba1\u4e3a private \u3002\u4f8b\u5982\uff0c\u7528\u6237\u5355\u72ec\u8c03\u7528 updateHeight() \u662f\u6ca1\u6709\u610f\u4e49\u7684\uff0c\u5b83\u53ea\u662f\u63d2\u5165\u3001\u5220\u9664\u64cd\u4f5c\u4e2d\u7684\u4e00\u6b65\u3002\u800c height() \u662f\u8bbf\u95ee\u8282\u70b9\u9ad8\u5ea6\uff0c\u7c7b\u4f3c\u4e8e vector.size() \uff0c\u56e0\u6b64\u8bbe\u7f6e\u6210 public \u4ee5\u4fbf\u4f7f\u7528\u3002

        Q\uff1a\u5982\u4f55\u4ece\u4e00\u7ec4\u8f93\u5165\u6570\u636e\u6784\u5efa\u4e00\u68f5\u4e8c\u53c9\u641c\u7d22\u6811\uff1f\u6839\u8282\u70b9\u7684\u9009\u62e9\u662f\u4e0d\u662f\u5f88\u91cd\u8981\uff1f

        \u662f\u7684\uff0c\u6784\u5efa\u6811\u7684\u65b9\u6cd5\u5df2\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4ee3\u7801\u4e2d\u7684 build_tree() \u65b9\u6cd5\u4e2d\u7ed9\u51fa\u3002\u81f3\u4e8e\u6839\u8282\u70b9\u7684\u9009\u62e9\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u8f93\u5165\u6570\u636e\u6392\u5e8f\uff0c\u7136\u540e\u5c06\u4e2d\u70b9\u5143\u7d20\u4f5c\u4e3a\u6839\u8282\u70b9\uff0c\u518d\u9012\u5f52\u5730\u6784\u5efa\u5de6\u53f3\u5b50\u6811\u3002\u8fd9\u6837\u505a\u53ef\u4ee5\u6700\u5927\u7a0b\u5ea6\u4fdd\u8bc1\u6811\u7684\u5e73\u8861\u6027\u3002

        Q\uff1a\u5728 Java \u4e2d\uff0c\u5b57\u7b26\u4e32\u5bf9\u6bd4\u662f\u5426\u4e00\u5b9a\u8981\u7528 equals() \u65b9\u6cd5\uff1f

        \u5728 Java \u4e2d\uff0c\u5bf9\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c== \u7528\u4e8e\u5bf9\u6bd4\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002\u5bf9\u4e8e\u5f15\u7528\u7c7b\u578b\uff0c\u4e24\u79cd\u7b26\u53f7\u7684\u5de5\u4f5c\u539f\u7406\u662f\u4e0d\u540c\u7684\u3002

        • == \uff1a\u7528\u6765\u6bd4\u8f83\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5373\u5b83\u4eec\u5728\u5185\u5b58\u4e2d\u7684\u4f4d\u7f6e\u662f\u5426\u76f8\u540c\u3002
        • equals()\uff1a\u7528\u6765\u5bf9\u6bd4\u4e24\u4e2a\u5bf9\u8c61\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002

        \u56e0\u6b64\uff0c\u5982\u679c\u8981\u5bf9\u6bd4\u503c\uff0c\u6211\u4eec\u5e94\u8be5\u4f7f\u7528 equals() \u3002\u7136\u800c\uff0c\u901a\u8fc7 String a = \"hi\"; String b = \"hi\"; \u521d\u59cb\u5316\u7684\u5b57\u7b26\u4e32\u90fd\u5b58\u50a8\u5728\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u4e2d\uff0c\u5b83\u4eec\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u56e0\u6b64\u4e5f\u53ef\u4ee5\u7528 a == b \u6765\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u3002

        Q\uff1a\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u5230\u6700\u5e95\u5c42\u4e4b\u524d\uff0c\u961f\u5217\u4e2d\u7684\u8282\u70b9\u6570\u91cf\u662f \\(2^h\\) \u5417\uff1f

        \u662f\u7684\uff0c\u4f8b\u5982\u9ad8\u5ea6 \\(h = 2\\) \u7684\u6ee1\u4e8c\u53c9\u6811\uff0c\u5176\u8282\u70b9\u603b\u6570 \\(n = 7\\) \uff0c\u5219\u5e95\u5c42\u8282\u70b9\u6570\u91cf \\(4 = 2^h = (n + 1) / 2\\) \u3002

        "}]} \ No newline at end of file +{"config":{"lang":["en"],"separator":"[\\s\\u200b\\u3000\\-\u3001\u3002\uff0c\uff0e\uff1f\uff01\uff1b]+","pipeline":["stemmer"],"fields":{"title":{"boost":1000.0},"text":{"boost":1.0},"tags":{"boost":1000000.0}}},"docs":[{"location":"chapter_appendix/","title":"\u7b2c 16 \u7ae0 \u00a0 \u9644\u5f55","text":""},{"location":"chapter_appendix/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 16.1 \u00a0 \u7f16\u7a0b\u73af\u5883\u5b89\u88c5
        • 16.2 \u00a0 \u4e00\u8d77\u53c2\u4e0e\u521b\u4f5c
        • 16.3 \u00a0 \u672f\u8bed\u8868
        "},{"location":"chapter_appendix/contribution/","title":"16.2 \u00a0 \u4e00\u8d77\u53c2\u4e0e\u521b\u4f5c","text":"

        \u7531\u4e8e\u7b14\u8005\u80fd\u529b\u6709\u9650\uff0c\u4e66\u4e2d\u96be\u514d\u5b58\u5728\u4e00\u4e9b\u9057\u6f0f\u548c\u9519\u8bef\uff0c\u8bf7\u60a8\u8c05\u89e3\u3002\u5982\u679c\u60a8\u53d1\u73b0\u4e86\u7b14\u8bef\u3001\u94fe\u63a5\u5931\u6548\u3001\u5185\u5bb9\u7f3a\u5931\u3001\u6587\u5b57\u6b67\u4e49\u3001\u89e3\u91ca\u4e0d\u6e05\u6670\u6216\u884c\u6587\u7ed3\u6784\u4e0d\u5408\u7406\u7b49\u95ee\u9898\uff0c\u8bf7\u534f\u52a9\u6211\u4eec\u8fdb\u884c\u4fee\u6b63\uff0c\u4ee5\u7ed9\u8bfb\u8005\u63d0\u4f9b\u66f4\u4f18\u8d28\u7684\u5b66\u4e60\u8d44\u6e90\u3002

        \u6240\u6709\u64b0\u7a3f\u4eba\u7684 GitHub ID \u5c06\u5728\u672c\u4e66\u4ed3\u5e93\u3001\u7f51\u9875\u7248\u548c PDF \u7248\u7684\u4e3b\u9875\u4e0a\u8fdb\u884c\u5c55\u793a\uff0c\u4ee5\u611f\u8c22\u4ed6\u4eec\u5bf9\u5f00\u6e90\u793e\u533a\u7684\u65e0\u79c1\u5949\u732e\u3002

        \u5f00\u6e90\u7684\u9b45\u529b

        \u7eb8\u8d28\u56fe\u4e66\u7684\u4e24\u6b21\u5370\u5237\u7684\u95f4\u9694\u65f6\u95f4\u5f80\u5f80\u8f83\u4e45\uff0c\u5185\u5bb9\u66f4\u65b0\u975e\u5e38\u4e0d\u65b9\u4fbf\u3002

        \u800c\u5728\u672c\u5f00\u6e90\u4e66\u4e2d\uff0c\u5185\u5bb9\u66f4\u8fed\u7684\u65f6\u95f4\u88ab\u7f29\u77ed\u81f3\u6570\u65e5\u751a\u81f3\u51e0\u4e2a\u5c0f\u65f6\u3002

        "},{"location":"chapter_appendix/contribution/#1","title":"1. \u00a0 \u5185\u5bb9\u5fae\u8c03","text":"

        \u5982\u56fe 16-3 \u6240\u793a\uff0c\u6bcf\u4e2a\u9875\u9762\u7684\u53f3\u4e0a\u89d2\u90fd\u6709\u201c\u7f16\u8f91\u56fe\u6807\u201d\u3002\u60a8\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u4fee\u6539\u6587\u672c\u6216\u4ee3\u7801\u3002

        1. \u70b9\u51fb\u201c\u7f16\u8f91\u56fe\u6807\u201d\uff0c\u5982\u679c\u9047\u5230\u201c\u9700\u8981 Fork \u6b64\u4ed3\u5e93\u201d\u7684\u63d0\u793a\uff0c\u8bf7\u540c\u610f\u8be5\u64cd\u4f5c\u3002
        2. \u4fee\u6539 Markdown \u6e90\u6587\u4ef6\u5185\u5bb9\uff0c\u68c0\u67e5\u5185\u5bb9\u7684\u6b63\u786e\u6027\uff0c\u5e76\u5c3d\u91cf\u4fdd\u6301\u6392\u7248\u683c\u5f0f\u7684\u7edf\u4e00\u3002
        3. \u5728\u9875\u9762\u5e95\u90e8\u586b\u5199\u4fee\u6539\u8bf4\u660e\uff0c\u7136\u540e\u70b9\u51fb\u201cPropose file change\u201d\u6309\u94ae\u3002\u9875\u9762\u8df3\u8f6c\u540e\uff0c\u70b9\u51fb\u201cCreate pull request\u201d\u6309\u94ae\u5373\u53ef\u53d1\u8d77\u62c9\u53d6\u8bf7\u6c42\u3002

        \u56fe 16-3 \u00a0 \u9875\u9762\u7f16\u8f91\u6309\u952e

        \u56fe\u7247\u65e0\u6cd5\u76f4\u63a5\u4fee\u6539\uff0c\u9700\u8981\u901a\u8fc7\u65b0\u5efa Issue \u6216\u8bc4\u8bba\u7559\u8a00\u6765\u63cf\u8ff0\u95ee\u9898\uff0c\u6211\u4eec\u4f1a\u5c3d\u5feb\u91cd\u65b0\u7ed8\u5236\u5e76\u66ff\u6362\u56fe\u7247\u3002

        "},{"location":"chapter_appendix/contribution/#2","title":"2. \u00a0 \u5185\u5bb9\u521b\u4f5c","text":"

        \u5982\u679c\u60a8\u6709\u5174\u8da3\u53c2\u4e0e\u6b64\u5f00\u6e90\u9879\u76ee\uff0c\u5305\u62ec\u5c06\u4ee3\u7801\u7ffb\u8bd1\u6210\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u3001\u6269\u5c55\u6587\u7ae0\u5185\u5bb9\u7b49\uff0c\u90a3\u4e48\u9700\u8981\u5b9e\u65bd\u4ee5\u4e0b Pull Request \u5de5\u4f5c\u6d41\u7a0b\u3002

        1. \u767b\u5f55 GitHub \uff0c\u5c06\u672c\u4e66\u7684\u4ee3\u7801\u4ed3\u5e93 Fork \u5230\u4e2a\u4eba\u8d26\u53f7\u4e0b\u3002
        2. \u8fdb\u5165\u60a8\u7684 Fork \u4ed3\u5e93\u7f51\u9875\uff0c\u4f7f\u7528 git clone \u547d\u4ee4\u5c06\u4ed3\u5e93\u514b\u9686\u81f3\u672c\u5730\u3002
        3. \u5728\u672c\u5730\u8fdb\u884c\u5185\u5bb9\u521b\u4f5c\uff0c\u5e76\u8fdb\u884c\u5b8c\u6574\u6d4b\u8bd5\uff0c\u9a8c\u8bc1\u4ee3\u7801\u7684\u6b63\u786e\u6027\u3002
        4. \u5c06\u672c\u5730\u6240\u505a\u66f4\u6539 Commit \uff0c\u7136\u540e Push \u81f3\u8fdc\u7a0b\u4ed3\u5e93\u3002
        5. \u5237\u65b0\u4ed3\u5e93\u7f51\u9875\uff0c\u70b9\u51fb\u201cCreate pull request\u201d\u6309\u94ae\u5373\u53ef\u53d1\u8d77\u62c9\u53d6\u8bf7\u6c42\u3002
        "},{"location":"chapter_appendix/contribution/#3-docker","title":"3. \u00a0 Docker \u90e8\u7f72","text":"

        \u5728 hello-algo \u6839\u76ee\u5f55\u4e0b\uff0c\u6267\u884c\u4ee5\u4e0b Docker \u811a\u672c\uff0c\u5373\u53ef\u5728 http://localhost:8000 \u8bbf\u95ee\u672c\u9879\u76ee\uff1a

        docker-compose up -d\n

        \u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5373\u53ef\u5220\u9664\u90e8\u7f72\uff1a

        docker-compose down\n
        "},{"location":"chapter_appendix/installation/","title":"16.1 \u00a0 \u7f16\u7a0b\u73af\u5883\u5b89\u88c5","text":""},{"location":"chapter_appendix/installation/#1611-ide","title":"16.1.1 \u00a0 \u5b89\u88c5 IDE","text":"

        \u63a8\u8350\u4f7f\u7528\u5f00\u6e90\u3001\u8f7b\u91cf\u7684 VS Code \u4f5c\u4e3a\u672c\u5730\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u3002\u8bbf\u95ee VS Code \u5b98\u7f51\uff0c\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u9009\u62e9\u76f8\u5e94\u7248\u672c\u7684 VS Code \u8fdb\u884c\u4e0b\u8f7d\u548c\u5b89\u88c5\u3002

        \u56fe 16-1 \u00a0 \u4ece\u5b98\u7f51\u4e0b\u8f7d VS Code

        VS Code \u62e5\u6709\u5f3a\u5927\u7684\u6269\u5c55\u5305\u751f\u6001\u7cfb\u7edf\uff0c\u652f\u6301\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u7684\u8fd0\u884c\u548c\u8c03\u8bd5\u3002\u4ee5 Python \u4e3a\u4f8b\uff0c\u5b89\u88c5\u201cPython Extension Pack\u201d\u6269\u5c55\u5305\u4e4b\u540e\uff0c\u5373\u53ef\u8fdb\u884c Python \u4ee3\u7801\u8c03\u8bd5\u3002\u5b89\u88c5\u6b65\u9aa4\u5982\u56fe 16-2 \u6240\u793a\u3002

        \u56fe 16-2 \u00a0 \u5b89\u88c5 VS Code \u6269\u5c55\u5305

        "},{"location":"chapter_appendix/installation/#1612","title":"16.1.2 \u00a0 \u5b89\u88c5\u8bed\u8a00\u73af\u5883","text":""},{"location":"chapter_appendix/installation/#1-python","title":"1. \u00a0 Python \u73af\u5883","text":"
        1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Miniconda3 \uff0c\u9700\u8981 Python 3.10 \u6216\u66f4\u65b0\u7248\u672c\u3002
        2. \u5728 VS Code \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 python \uff0c\u5b89\u88c5 Python Extension Pack \u3002
        3. \uff08\u53ef\u9009\uff09\u5728\u547d\u4ee4\u884c\u8f93\u5165 pip install black \uff0c\u5b89\u88c5\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\u3002
        "},{"location":"chapter_appendix/installation/#2-cc","title":"2. \u00a0 C/C++ \u73af\u5883","text":"
        1. Windows \u7cfb\u7edf\u9700\u8981\u5b89\u88c5 MinGW\uff08\u914d\u7f6e\u6559\u7a0b\uff09\uff1bMacOS \u81ea\u5e26 Clang \uff0c\u65e0\u987b\u5b89\u88c5\u3002
        2. \u5728 VS Code \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 c++ \uff0c\u5b89\u88c5 C/C++ Extension Pack \u3002
        3. \uff08\u53ef\u9009\uff09\u6253\u5f00 Settings \u9875\u9762\uff0c\u641c\u7d22 Clang_format_fallback Style \u4ee3\u7801\u683c\u5f0f\u5316\u9009\u9879\uff0c\u8bbe\u7f6e\u4e3a { BasedOnStyle: Microsoft, BreakBeforeBraces: Attach } \u3002
        "},{"location":"chapter_appendix/installation/#3-java","title":"3. \u00a0 Java \u73af\u5883","text":"
        1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 OpenJDK\uff08\u7248\u672c\u9700\u6ee1\u8db3 > JDK 9\uff09\u3002
        2. \u5728 VS Code \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 java \uff0c\u5b89\u88c5 Extension Pack for Java \u3002
        "},{"location":"chapter_appendix/installation/#4-c","title":"4. \u00a0 C# \u73af\u5883","text":"
        1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 .Net 8.0 \u3002
        2. \u5728 VS Code \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 C# Dev Kit \uff0c\u5b89\u88c5 C# Dev Kit \uff08\u914d\u7f6e\u6559\u7a0b\uff09\u3002
        3. \u4e5f\u53ef\u4f7f\u7528 Visual Studio\uff08\u5b89\u88c5\u6559\u7a0b\uff09\u3002
        "},{"location":"chapter_appendix/installation/#5-go","title":"5. \u00a0 Go \u73af\u5883","text":"
        1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 go \u3002
        2. \u5728 VS Code \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 go \uff0c\u5b89\u88c5 Go \u3002
        3. \u6309\u5feb\u6377\u952e Ctrl + Shift + P \u547c\u51fa\u547d\u4ee4\u680f\uff0c\u8f93\u5165 go \uff0c\u9009\u62e9 Go: Install/Update Tools \uff0c\u5168\u90e8\u52fe\u9009\u5e76\u5b89\u88c5\u5373\u53ef\u3002
        "},{"location":"chapter_appendix/installation/#6-swift","title":"6. \u00a0 Swift \u73af\u5883","text":"
        1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Swift \u3002
        2. \u5728 VS Code \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 swift \uff0c\u5b89\u88c5 Swift for Visual Studio Code \u3002
        "},{"location":"chapter_appendix/installation/#7-javascript","title":"7. \u00a0 JavaScript \u73af\u5883","text":"
        1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Node.js \u3002
        2. \uff08\u53ef\u9009\uff09\u5728 VS Code \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 Prettier \uff0c\u5b89\u88c5\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\u3002
        "},{"location":"chapter_appendix/installation/#8-typescript","title":"8. \u00a0 TypeScript \u73af\u5883","text":"
        1. \u540c JavaScript \u73af\u5883\u5b89\u88c5\u6b65\u9aa4\u3002
        2. \u5b89\u88c5 TypeScript Execute (tsx) \u3002
        3. \u5728 VS Code \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 typescript \uff0c\u5b89\u88c5 Pretty TypeScript Errors \u3002
        "},{"location":"chapter_appendix/installation/#9-dart","title":"9. \u00a0 Dart \u73af\u5883","text":"
        1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Dart \u3002
        2. \u5728 VS Code \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 dart \uff0c\u5b89\u88c5 Dart \u3002
        "},{"location":"chapter_appendix/installation/#10-rust","title":"10. \u00a0 Rust \u73af\u5883","text":"
        1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Rust \u3002
        2. \u5728 VS Code \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 rust \uff0c\u5b89\u88c5 rust-analyzer \u3002
        "},{"location":"chapter_appendix/terminology/","title":"16.3 \u00a0 \u672f\u8bed\u8868","text":"

        \u8868 16-1 \u5217\u51fa\u4e86\u4e66\u4e2d\u51fa\u73b0\u7684\u91cd\u8981\u672f\u8bed\uff0c\u503c\u5f97\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\u3002

        • \u5efa\u8bae\u8bb0\u4f4f\u540d\u8bcd\u7684\u82f1\u6587\u53eb\u6cd5\uff0c\u4ee5\u4fbf\u9605\u8bfb\u82f1\u6587\u6587\u732e\u3002
        • \u90e8\u5206\u540d\u8bcd\u5728\u7b80\u4f53\u4e2d\u6587\u548c\u7e41\u4f53\u4e2d\u6587\u4e0b\u7684\u53eb\u6cd5\u4e0d\u540c\u3002

        \u8868 16-1 \u00a0 \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u91cd\u8981\u540d\u8bcd

        English \u7b80\u4f53\u4e2d\u6587 \u7e41\u4f53\u4e2d\u6587 algorithm \u7b97\u6cd5 \u6f14\u7b97\u6cd5 data structure \u6570\u636e\u7ed3\u6784 \u8cc7\u6599\u7d50\u69cb code \u4ee3\u7801 \u7a0b\u5f0f\u78bc file \u6587\u4ef6 \u6a94\u6848 function \u51fd\u6570 \u51fd\u5f0f method \u65b9\u6cd5 \u65b9\u6cd5 variable \u53d8\u91cf \u8b8a\u6578 asymptotic complexity analysis \u6e10\u8fd1\u590d\u6742\u5ea6\u5206\u6790 \u6f38\u8fd1\u8907\u96dc\u5ea6\u5206\u6790 time complexity \u65f6\u95f4\u590d\u6742\u5ea6 \u6642\u9593\u8907\u96dc\u5ea6 space complexity \u7a7a\u95f4\u590d\u6742\u5ea6 \u7a7a\u9593\u8907\u96dc\u5ea6 loop \u5faa\u73af \u8ff4\u5708 iteration \u8fed\u4ee3 \u8fed\u4ee3 recursion \u9012\u5f52 \u905e\u8ff4 tail recursion \u5c3e\u9012\u5f52 \u5c3e\u905e\u8ff4 recursion tree \u9012\u5f52\u6811 \u905e\u8ff4\u6a39 big-\\(O\\) notation \u5927 \\(O\\) \u8bb0\u53f7 \u5927 \\(O\\) \u8a18\u865f asymptotic upper bound \u6e10\u8fd1\u4e0a\u754c \u6f38\u8fd1\u4e0a\u754c sign-magnitude \u539f\u7801 \u539f\u78bc 1\u2019s complement \u53cd\u7801 \u4e00\u88dc\u6578 2\u2019s complement \u8865\u7801 \u4e8c\u88dc\u6578 array \u6570\u7ec4 \u9663\u5217 index \u7d22\u5f15 \u7d22\u5f15 linked list \u94fe\u8868 \u93c8\u7d50\u4e32\u5217 linked list node, list node \u94fe\u8868\u8282\u70b9 \u93c8\u7d50\u4e32\u5217\u7bc0\u9ede head node \u5934\u8282\u70b9 \u982d\u7bc0\u9ede tail node \u5c3e\u8282\u70b9 \u5c3e\u7bc0\u9ede list \u5217\u8868 \u4e32\u5217 dynamic array \u52a8\u6001\u6570\u7ec4 \u52d5\u614b\u9663\u5217 hard disk \u786c\u76d8 \u786c\u789f random-access memory (RAM) \u5185\u5b58 \u8a18\u61b6\u9ad4 cache memory \u7f13\u5b58 \u5feb\u53d6 cache miss \u7f13\u5b58\u672a\u547d\u4e2d \u5feb\u53d6\u672a\u547d\u4e2d cache hit rate \u7f13\u5b58\u547d\u4e2d\u7387 \u5feb\u53d6\u547d\u4e2d\u7387 stack \u6808 \u5806\u758a top of the stack \u6808\u9876 \u5806\u758a\u9802 bottom of the stack \u6808\u5e95 \u5806\u758a\u5e95 queue \u961f\u5217 \u4f47\u5217 double-ended queue \u53cc\u5411\u961f\u5217 \u96d9\u5411\u4f47\u5217 front of the queue \u961f\u9996 \u4f47\u5217\u9996 rear of the queue \u961f\u5c3e \u4f47\u5217\u5c3e hash table \u54c8\u5e0c\u8868 \u96dc\u6e4a\u8868 hash set \u54c8\u5e0c\u96c6\u5408 \u96dc\u6e4a\u96c6\u5408 bucket \u6876 \u6876 hash function \u54c8\u5e0c\u51fd\u6570 \u96dc\u6e4a\u51fd\u5f0f hash collision \u54c8\u5e0c\u51b2\u7a81 \u96dc\u6e4a\u885d\u7a81 load factor \u8d1f\u8f7d\u56e0\u5b50 \u8ca0\u8f09\u56e0\u5b50 separate chaining \u94fe\u5f0f\u5730\u5740 \u93c8\u7d50\u4f4d\u5740 open addressing \u5f00\u653e\u5bfb\u5740 \u958b\u653e\u5b9a\u5740 linear probing \u7ebf\u6027\u63a2\u6d4b \u7dda\u6027\u63a2\u67e5 lazy deletion \u61d2\u5220\u9664 \u61f6\u522a\u9664 binary tree \u4e8c\u53c9\u6811 \u4e8c\u5143\u6a39 tree node \u6811\u8282\u70b9 \u6a39\u7bc0\u9ede left-child node \u5de6\u5b50\u8282\u70b9 \u5de6\u5b50\u7bc0\u9ede right-child node \u53f3\u5b50\u8282\u70b9 \u53f3\u5b50\u7bc0\u9ede parent node \u7236\u8282\u70b9 \u7236\u7bc0\u9ede left subtree \u5de6\u5b50\u6811 \u5de6\u5b50\u6a39 right subtree \u53f3\u5b50\u6811 \u53f3\u5b50\u6a39 root node \u6839\u8282\u70b9 \u6839\u7bc0\u9ede leaf node \u53f6\u8282\u70b9 \u8449\u7bc0\u9ede edge \u8fb9 \u908a level \u5c42 \u5c64 degree \u5ea6 \u5ea6 height \u9ad8\u5ea6 \u9ad8\u5ea6 depth \u6df1\u5ea6 \u6df1\u5ea6 perfect binary tree \u5b8c\u7f8e\u4e8c\u53c9\u6811 \u5b8c\u7f8e\u4e8c\u5143\u6a39 complete binary tree \u5b8c\u5168\u4e8c\u53c9\u6811 \u5b8c\u5168\u4e8c\u5143\u6a39 full binary tree \u5b8c\u6ee1\u4e8c\u53c9\u6811 \u5b8c\u6eff\u4e8c\u5143\u6a39 balanced binary tree \u5e73\u8861\u4e8c\u53c9\u6811 \u5e73\u8861\u4e8c\u5143\u6a39 binary search tree \u4e8c\u53c9\u641c\u7d22\u6811 \u4e8c\u5143\u641c\u5c0b\u6a39 AVL tree AVL \u6811 AVL \u6a39 red-black tree \u7ea2\u9ed1\u6811 \u7d05\u9ed1\u6a39 level-order traversal \u5c42\u5e8f\u904d\u5386 \u5c64\u5e8f\u8d70\u8a2a breadth-first traversal \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 \u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a depth-first traversal \u6df1\u5ea6\u4f18\u5148\u904d\u5386 \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a binary search tree \u4e8c\u53c9\u641c\u7d22\u6811 \u4e8c\u5143\u641c\u5c0b\u6a39 balanced binary search tree \u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811 \u5e73\u8861\u4e8c\u5143\u641c\u5c0b\u6a39 balance factor \u5e73\u8861\u56e0\u5b50 \u5e73\u8861\u56e0\u5b50 heap \u5806 \u5806\u7a4d max heap \u5927\u9876\u5806 \u5927\u9802\u5806\u7a4d min heap \u5c0f\u9876\u5806 \u5c0f\u9802\u5806\u7a4d priority queue \u4f18\u5148\u961f\u5217 \u512a\u5148\u4f47\u5217 heapify \u5806\u5316 \u5806\u7a4d\u5316 top-\\(k\\) problem Top-\\(k\\) \u95ee\u9898 Top-\\(k\\) \u554f\u984c graph \u56fe \u5716 vertex \u9876\u70b9 \u9802\u9ede undirected graph \u65e0\u5411\u56fe \u7121\u5411\u5716 directed graph \u6709\u5411\u56fe \u6709\u5411\u5716 connected graph \u8fde\u901a\u56fe \u9023\u901a\u5716 disconnected graph \u975e\u8fde\u901a\u56fe \u975e\u9023\u901a\u5716 weighted graph \u6709\u6743\u56fe \u6709\u6b0a\u5716 adjacency \u90bb\u63a5 \u9130\u63a5 path \u8def\u5f84 \u8def\u5f91 in-degree \u5165\u5ea6 \u5165\u5ea6 out-degree \u51fa\u5ea6 \u51fa\u5ea6 adjacency matrix \u90bb\u63a5\u77e9\u9635 \u9130\u63a5\u77e9\u9663 adjacency list \u90bb\u63a5\u8868 \u9130\u63a5\u8868 breadth-first search \u5e7f\u5ea6\u4f18\u5148\u641c\u7d22 \u5ee3\u5ea6\u512a\u5148\u641c\u5c0b depth-first search \u6df1\u5ea6\u4f18\u5148\u641c\u7d22 \u6df1\u5ea6\u512a\u5148\u641c\u5c0b binary search \u4e8c\u5206\u67e5\u627e \u4e8c\u5206\u641c\u5c0b searching algorithm \u641c\u7d22\u7b97\u6cd5 \u641c\u5c0b\u6f14\u7b97\u6cd5 sorting algorithm \u6392\u5e8f\u7b97\u6cd5 \u6392\u5e8f\u6f14\u7b97\u6cd5 selection sort \u9009\u62e9\u6392\u5e8f \u9078\u64c7\u6392\u5e8f bubble sort \u5192\u6ce1\u6392\u5e8f \u6ce1\u6cab\u6392\u5e8f insertion sort \u63d2\u5165\u6392\u5e8f \u63d2\u5165\u6392\u5e8f quick sort \u5feb\u901f\u6392\u5e8f \u5feb\u901f\u6392\u5e8f merge sort \u5f52\u5e76\u6392\u5e8f \u5408\u4f75\u6392\u5e8f heap sort \u5806\u6392\u5e8f \u5806\u7a4d\u6392\u5e8f bucket sort \u6876\u6392\u5e8f \u6876\u6392\u5e8f counting sort \u8ba1\u6570\u6392\u5e8f \u8a08\u6578\u6392\u5e8f radix sort \u57fa\u6570\u6392\u5e8f \u57fa\u6578\u6392\u5e8f divide and conquer \u5206\u6cbb \u5206\u6cbb hanota problem \u6c49\u8bfa\u5854\u95ee\u9898 \u6cb3\u5167\u5854\u554f\u984c backtracking algorithm \u56de\u6eaf\u7b97\u6cd5 \u56de\u6eaf\u6f14\u7b97\u6cd5 constraint \u7ea6\u675f \u7d04\u675f solution \u89e3 \u89e3 state \u72b6\u6001 \u72c0\u614b pruning \u526a\u679d \u526a\u679d permutations problem \u5168\u6392\u5217\u95ee\u9898 \u5168\u6392\u5217\u554f\u984c subset-sum problem \u5b50\u96c6\u548c\u95ee\u9898 \u5b50\u96c6\u5408\u554f\u984c \\(n\\)-queens problem \\(n\\) \u7687\u540e\u95ee\u9898 \\(n\\) \u7687\u540e\u554f\u984c dynamic programming \u52a8\u6001\u89c4\u5212 \u52d5\u614b\u898f\u5283 initial state \u521d\u59cb\u72b6\u6001 \u521d\u59cb\u72c0\u614b state-transition equation \u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b \u72c0\u614b\u8f49\u79fb\u65b9\u7a0b knapsack problem \u80cc\u5305\u95ee\u9898 \u80cc\u5305\u554f\u984c edit distance problem \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898 \u7de8\u8f2f\u8ddd\u96e2\u554f\u984c greedy algorithm \u8d2a\u5fc3\u7b97\u6cd5 \u8caa\u5a6a\u6f14\u7b97\u6cd5"},{"location":"chapter_array_and_linkedlist/","title":"\u7b2c 4 \u7ae0 \u00a0 \u6570\u7ec4\u4e0e\u94fe\u8868","text":"

        Abstract

        \u6570\u636e\u7ed3\u6784\u7684\u4e16\u754c\u5982\u540c\u4e00\u5835\u539a\u5b9e\u7684\u7816\u5899\u3002

        \u6570\u7ec4\u7684\u7816\u5757\u6574\u9f50\u6392\u5217\uff0c\u9010\u4e2a\u7d27\u8d34\u3002\u94fe\u8868\u7684\u7816\u5757\u5206\u6563\u5404\u5904\uff0c\u8fde\u63a5\u7684\u85e4\u8513\u81ea\u7531\u5730\u7a7f\u68ad\u4e8e\u7816\u7f1d\u4e4b\u95f4\u3002

        "},{"location":"chapter_array_and_linkedlist/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 4.1 \u00a0 \u6570\u7ec4
        • 4.2 \u00a0 \u94fe\u8868
        • 4.3 \u00a0 \u5217\u8868
        • 4.4 \u00a0 \u5185\u5b58\u4e0e\u7f13\u5b58 *
        • 4.5 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_array_and_linkedlist/array/","title":"4.1 \u00a0 \u6570\u7ec4","text":"

        \u6570\u7ec4\uff08array\uff09\u662f\u4e00\u79cd\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u5176\u5c06\u76f8\u540c\u7c7b\u578b\u7684\u5143\u7d20\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\u3002\u6211\u4eec\u5c06\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e\u79f0\u4e3a\u8be5\u5143\u7d20\u7684\u7d22\u5f15\uff08index\uff09\u3002\u56fe 4-1 \u5c55\u793a\u4e86\u6570\u7ec4\u7684\u4e3b\u8981\u6982\u5ff5\u548c\u5b58\u50a8\u65b9\u5f0f\u3002

        \u56fe 4-1 \u00a0 \u6570\u7ec4\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f

        "},{"location":"chapter_array_and_linkedlist/array/#411","title":"4.1.1 \u00a0 \u6570\u7ec4\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/array/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u6570\u7ec4","text":"

        \u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u7528\u6570\u7ec4\u7684\u4e24\u79cd\u521d\u59cb\u5316\u65b9\u5f0f\uff1a\u65e0\u521d\u59cb\u503c\u3001\u7ed9\u5b9a\u521d\u59cb\u503c\u3002\u5728\u672a\u6307\u5b9a\u521d\u59cb\u503c\u7684\u60c5\u51b5\u4e0b\uff0c\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4f1a\u5c06\u6570\u7ec4\u5143\u7d20\u521d\u59cb\u5316\u4e3a \\(0\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        # \u521d\u59cb\u5316\u6570\u7ec4\narr: list[int] = [0] * 5  # [ 0, 0, 0, 0, 0 ]\nnums: list[int] = [1, 3, 2, 5, 4]\n
        array.cpp
        /* \u521d\u59cb\u5316\u6570\u7ec4 */\n// \u5b58\u50a8\u5728\u6808\u4e0a\nint arr[5];\nint nums[5] = { 1, 3, 2, 5, 4 };\n// \u5b58\u50a8\u5728\u5806\u4e0a\uff08\u9700\u8981\u624b\u52a8\u91ca\u653e\u7a7a\u95f4\uff09\nint* arr1 = new int[5];\nint* nums1 = new int[5] { 1, 3, 2, 5, 4 };\n
        array.java
        /* \u521d\u59cb\u5316\u6570\u7ec4 */\nint[] arr = new int[5]; // { 0, 0, 0, 0, 0 }\nint[] nums = { 1, 3, 2, 5, 4 };\n
        array.cs
        /* \u521d\u59cb\u5316\u6570\u7ec4 */\nint[] arr = new int[5]; // [ 0, 0, 0, 0, 0 ]\nint[] nums = [1, 3, 2, 5, 4];\n
        array.go
        /* \u521d\u59cb\u5316\u6570\u7ec4 */\nvar arr [5]int\n// \u5728 Go \u4e2d\uff0c\u6307\u5b9a\u957f\u5ea6\u65f6\uff08[5]int\uff09\u4e3a\u6570\u7ec4\uff0c\u4e0d\u6307\u5b9a\u957f\u5ea6\u65f6\uff08[]int\uff09\u4e3a\u5207\u7247\n// \u7531\u4e8e Go \u7684\u6570\u7ec4\u88ab\u8bbe\u8ba1\u4e3a\u5728\u7f16\u8bd1\u671f\u786e\u5b9a\u957f\u5ea6\uff0c\u56e0\u6b64\u53ea\u80fd\u4f7f\u7528\u5e38\u91cf\u6765\u6307\u5b9a\u957f\u5ea6\n// \u4e3a\u4e86\u65b9\u4fbf\u5b9e\u73b0\u6269\u5bb9 extend() \u65b9\u6cd5\uff0c\u4ee5\u4e0b\u5c06\u5207\u7247\uff08Slice\uff09\u770b\u4f5c\u6570\u7ec4\uff08Array\uff09\nnums := []int{1, 3, 2, 5, 4}\n
        array.swift
        /* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr = Array(repeating: 0, count: 5) // [0, 0, 0, 0, 0]\nlet nums = [1, 3, 2, 5, 4]\n
        array.js
        /* \u521d\u59cb\u5316\u6570\u7ec4 */\nvar arr = new Array(5).fill(0);\nvar nums = [1, 3, 2, 5, 4];\n
        array.ts
        /* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr: number[] = new Array(5).fill(0);\nlet nums: number[] = [1, 3, 2, 5, 4];\n
        array.dart
        /* \u521d\u59cb\u5316\u6570\u7ec4 */\nList<int> arr = List.filled(5, 0); // [0, 0, 0, 0, 0]\nList<int> nums = [1, 3, 2, 5, 4];\n
        array.rs
        /* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr: [i32; 5] = [0; 5]; // [0, 0, 0, 0, 0]\nlet slice: &[i32] = &[0; 5];\n// \u5728 Rust \u4e2d\uff0c\u6307\u5b9a\u957f\u5ea6\u65f6\uff08[i32; 5]\uff09\u4e3a\u6570\u7ec4\uff0c\u4e0d\u6307\u5b9a\u957f\u5ea6\u65f6\uff08&[i32]\uff09\u4e3a\u5207\u7247\n// \u7531\u4e8e Rust \u7684\u6570\u7ec4\u88ab\u8bbe\u8ba1\u4e3a\u5728\u7f16\u8bd1\u671f\u786e\u5b9a\u957f\u5ea6\uff0c\u56e0\u6b64\u53ea\u80fd\u4f7f\u7528\u5e38\u91cf\u6765\u6307\u5b9a\u957f\u5ea6\n// Vector \u662f Rust \u4e00\u822c\u60c5\u51b5\u4e0b\u7528\u4f5c\u52a8\u6001\u6570\u7ec4\u7684\u7c7b\u578b\n// \u4e3a\u4e86\u65b9\u4fbf\u5b9e\u73b0\u6269\u5bb9 extend() \u65b9\u6cd5\uff0c\u4ee5\u4e0b\u5c06 vector \u770b\u4f5c\u6570\u7ec4\uff08array\uff09\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
        array.c
        /* \u521d\u59cb\u5316\u6570\u7ec4 */\nint arr[5] = { 0 }; // { 0, 0, 0, 0, 0 }\nint nums[5] = { 1, 3, 2, 5, 4 };\n
        array.kt
        /* \u521d\u59cb\u5316\u6570\u7ec4 */\nvar arr = IntArray(5) // { 0, 0, 0, 0, 0 }\nvar nums = intArrayOf(1, 3, 2, 5, 4)\n
        array.rb
        # \u521d\u59cb\u5316\u6570\u7ec4\narr = Array.new(5, 0)\nnums = [1, 3, 2, 5, 4]\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/array/#2","title":"2. \u00a0 \u8bbf\u95ee\u5143\u7d20","text":"

        \u6570\u7ec4\u5143\u7d20\u88ab\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\uff0c\u8fd9\u610f\u5473\u7740\u8ba1\u7b97\u6570\u7ec4\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u975e\u5e38\u5bb9\u6613\u3002\u7ed9\u5b9a\u6570\u7ec4\u5185\u5b58\u5730\u5740\uff08\u9996\u5143\u7d20\u5185\u5b58\u5730\u5740\uff09\u548c\u67d0\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u56fe 4-2 \u6240\u793a\u7684\u516c\u5f0f\u8ba1\u7b97\u5f97\u5230\u8be5\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\uff0c\u4ece\u800c\u76f4\u63a5\u8bbf\u95ee\u8be5\u5143\u7d20\u3002

        \u56fe 4-2 \u00a0 \u6570\u7ec4\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u8ba1\u7b97

        \u89c2\u5bdf\u56fe 4-2 \uff0c\u6211\u4eec\u53d1\u73b0\u6570\u7ec4\u9996\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u4e3a \\(0\\) \uff0c\u8fd9\u4f3c\u4e4e\u6709\u4e9b\u53cd\u76f4\u89c9\uff0c\u56e0\u4e3a\u4ece \\(1\\) \u5f00\u59cb\u8ba1\u6570\u4f1a\u66f4\u81ea\u7136\u3002\u4f46\u4ece\u5730\u5740\u8ba1\u7b97\u516c\u5f0f\u7684\u89d2\u5ea6\u770b\uff0c\u7d22\u5f15\u672c\u8d28\u4e0a\u662f\u5185\u5b58\u5730\u5740\u7684\u504f\u79fb\u91cf\u3002\u9996\u4e2a\u5143\u7d20\u7684\u5730\u5740\u504f\u79fb\u91cf\u662f \\(0\\) \uff0c\u56e0\u6b64\u5b83\u7684\u7d22\u5f15\u4e3a \\(0\\) \u662f\u5408\u7406\u7684\u3002

        \u5728\u6570\u7ec4\u4e2d\u8bbf\u95ee\u5143\u7d20\u975e\u5e38\u9ad8\u6548\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u968f\u673a\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u5143\u7d20\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def random_access(nums: list[int]) -> int:\n    \"\"\"\u968f\u673a\u8bbf\u95ee\u5143\u7d20\"\"\"\n    # \u5728\u533a\u95f4 [0, len(nums)-1] \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n    random_index = random.randint(0, len(nums) - 1)\n    # \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n    random_num = nums[random_index]\n    return random_num\n
        array.cpp
        /* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n    // \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n    int randomIndex = rand() % size;\n    // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
        array.java
        /* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int[] nums) {\n    // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n    int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);\n    // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
        array.cs
        /* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint RandomAccess(int[] nums) {\n    Random random = new();\n    // \u5728\u533a\u95f4 [0, nums.Length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n    int randomIndex = random.Next(nums.Length);\n    // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
        array.go
        /* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunc randomAccess(nums []int) (randomNum int) {\n    // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n    randomIndex := rand.Intn(len(nums))\n    // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n    randomNum = nums[randomIndex]\n    return\n}\n
        array.swift
        /* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunc randomAccess(nums: [Int]) -> Int {\n    // \u5728\u533a\u95f4 [0, nums.count) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n    let randomIndex = nums.indices.randomElement()!\n    // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n    let randomNum = nums[randomIndex]\n    return randomNum\n}\n
        array.js
        /* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunction randomAccess(nums) {\n    // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n    const random_index = Math.floor(Math.random() * nums.length);\n    // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n    const random_num = nums[random_index];\n    return random_num;\n}\n
        array.ts
        /* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunction randomAccess(nums: number[]): number {\n    // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n    const random_index = Math.floor(Math.random() * nums.length);\n    // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n    const random_num = nums[random_index];\n    return random_num;\n}\n
        array.dart
        /* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(List<int> nums) {\n  // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n  int randomIndex = Random().nextInt(nums.length);\n  // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n  int randomNum = nums[randomIndex];\n  return randomNum;\n}\n
        array.rs
        /* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfn random_access(nums: &[i32]) -> i32 {\n    // \u5728\u533a\u95f4 [0, nums.len()) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n    let random_index = rand::thread_rng().gen_range(0..nums.len());\n    // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n    let random_num = nums[random_index];\n    random_num\n}\n
        array.c
        /* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n    // \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n    int randomIndex = rand() % size;\n    // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
        array.kt
        /* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfun randomAccess(nums: IntArray): Int {\n    // \u5728\u533a\u95f4 [0, nums.size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n    val randomIndex = ThreadLocalRandom.current().nextInt(0, nums.size)\n    // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n    val randomNum = nums[randomIndex]\n    return randomNum\n}\n
        array.rb
        ### \u968f\u673a\u8bbf\u95ee\u5143\u7d20 ###\ndef random_access(nums)\n  # \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n  random_index = Random.rand(0...nums.length)\n\n  # \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n  nums[random_index]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/array/#3","title":"3. \u00a0 \u63d2\u5165\u5143\u7d20","text":"

        \u6570\u7ec4\u5143\u7d20\u5728\u5185\u5b58\u4e2d\u662f\u201c\u7d27\u6328\u7740\u7684\u201d\uff0c\u5b83\u4eec\u4e4b\u95f4\u6ca1\u6709\u7a7a\u95f4\u518d\u5b58\u653e\u4efb\u4f55\u6570\u636e\u3002\u5982\u56fe 4-3 \u6240\u793a\uff0c\u5982\u679c\u60f3\u5728\u6570\u7ec4\u4e2d\u95f4\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\uff0c\u5219\u9700\u8981\u5c06\u8be5\u5143\u7d20\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u4e4b\u540e\u518d\u628a\u5143\u7d20\u8d4b\u503c\u7ed9\u8be5\u7d22\u5f15\u3002

        \u56fe 4-3 \u00a0 \u6570\u7ec4\u63d2\u5165\u5143\u7d20\u793a\u4f8b

        \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u56fa\u5b9a\u7684\uff0c\u56e0\u6b64\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\u5fc5\u5b9a\u4f1a\u5bfc\u81f4\u6570\u7ec4\u5c3e\u90e8\u5143\u7d20\u201c\u4e22\u5931\u201d\u3002\u6211\u4eec\u5c06\u8fd9\u4e2a\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u7559\u5728\u201c\u5217\u8868\u201d\u7ae0\u8282\u4e2d\u8ba8\u8bba\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def insert(nums: list[int], num: int, index: int):\n    \"\"\"\u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num\"\"\"\n    # \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for i in range(len(nums) - 1, index, -1):\n        nums[i] = nums[i - 1]\n    # \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n    nums[index] = num\n
        array.cpp
        /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int *nums, int size, int num, int index) {\n    // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for (int i = size - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n    nums[index] = num;\n}\n
        array.java
        /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int[] nums, int num, int index) {\n    // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for (int i = nums.length - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n    nums[index] = num;\n}\n
        array.cs
        /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid Insert(int[] nums, int num, int index) {\n    // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for (int i = nums.Length - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n    nums[index] = num;\n}\n
        array.go
        /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunc insert(nums []int, num int, index int) {\n    // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for i := len(nums) - 1; i > index; i-- {\n        nums[i] = nums[i-1]\n    }\n    // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n    nums[index] = num\n}\n
        array.swift
        /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunc insert(nums: inout [Int], num: Int, index: Int) {\n    // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for i in nums.indices.dropFirst(index).reversed() {\n        nums[i] = nums[i - 1]\n    }\n    // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n    nums[index] = num\n}\n
        array.js
        /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunction insert(nums, num, index) {\n    // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for (let i = nums.length - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n    nums[index] = num;\n}\n
        array.ts
        /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunction insert(nums: number[], num: number, index: number): void {\n    // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for (let i = nums.length - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n    nums[index] = num;\n}\n
        array.dart
        /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 _num */\nvoid insert(List<int> nums, int _num, int index) {\n  // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n  for (var i = nums.length - 1; i > index; i--) {\n    nums[i] = nums[i - 1];\n  }\n  // \u5c06 _num \u8d4b\u7ed9 index \u5904\u5143\u7d20\n  nums[index] = _num;\n}\n
        array.rs
        /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfn insert(nums: &mut [i32], num: i32, index: usize) {\n    // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for i in (index + 1..nums.len()).rev() {\n        nums[i] = nums[i - 1];\n    }\n    // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n    nums[index] = num;\n}\n
        array.c
        /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int *nums, int size, int num, int index) {\n    // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for (int i = size - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n    nums[index] = num;\n}\n
        array.kt
        /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfun insert(nums: IntArray, num: Int, index: Int) {\n    // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for (i in nums.size - 1 downTo index + 1) {\n        nums[i] = nums[i - 1]\n    }\n    // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n    nums[index] = num\n}\n
        array.rb
        ### \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num ###\ndef insert(nums, num, index)\n  # \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n  for i in (nums.length - 1).downto(index + 1)\n    nums[i] = nums[i - 1]\n  end\n\n  # \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n  nums[index] = num\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/array/#4","title":"4. \u00a0 \u5220\u9664\u5143\u7d20","text":"

        \u540c\u7406\uff0c\u5982\u56fe 4-4 \u6240\u793a\uff0c\u82e5\u60f3\u5220\u9664\u7d22\u5f15 \\(i\\) \u5904\u7684\u5143\u7d20\uff0c\u5219\u9700\u8981\u628a\u7d22\u5f15 \\(i\\) \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\u3002

        \u56fe 4-4 \u00a0 \u6570\u7ec4\u5220\u9664\u5143\u7d20\u793a\u4f8b

        \u8bf7\u6ce8\u610f\uff0c\u5220\u9664\u5143\u7d20\u5b8c\u6210\u540e\uff0c\u539f\u5148\u672b\u5c3e\u7684\u5143\u7d20\u53d8\u5f97\u201c\u65e0\u610f\u4e49\u201d\u4e86\uff0c\u6240\u4ee5\u6211\u4eec\u65e0\u987b\u7279\u610f\u53bb\u4fee\u6539\u5b83\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def remove(nums: list[int], index: int):\n    \"\"\"\u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20\"\"\"\n    # \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for i in range(index, len(nums) - 1):\n        nums[i] = nums[i + 1]\n
        array.cpp
        /* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid remove(int *nums, int size, int index) {\n    // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for (int i = index; i < size - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
        array.java
        /* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid remove(int[] nums, int index) {\n    // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for (int i = index; i < nums.length - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
        array.cs
        /* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid Remove(int[] nums, int index) {\n    // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for (int i = index; i < nums.Length - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
        array.go
        /* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunc remove(nums []int, index int) {\n    // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for i := index; i < len(nums)-1; i++ {\n        nums[i] = nums[i+1]\n    }\n}\n
        array.swift
        /* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunc remove(nums: inout [Int], index: Int) {\n    // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for i in nums.indices.dropFirst(index).dropLast() {\n        nums[i] = nums[i + 1]\n    }\n}\n
        array.js
        /* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunction remove(nums, index) {\n    // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for (let i = index; i < nums.length - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
        array.ts
        /* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunction remove(nums: number[], index: number): void {\n    // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for (let i = index; i < nums.length - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
        array.dart
        /* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid remove(List<int> nums, int index) {\n  // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n  for (var i = index; i < nums.length - 1; i++) {\n    nums[i] = nums[i + 1];\n  }\n}\n
        array.rs
        /* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfn remove(nums: &mut [i32], index: usize) {\n    // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for i in index..nums.len() - 1 {\n        nums[i] = nums[i + 1];\n    }\n}\n
        array.c
        /* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nvoid removeItem(int *nums, int size, int index) {\n    // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for (int i = index; i < size - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
        array.kt
        /* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfun remove(nums: IntArray, index: Int) {\n    // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for (i in index..<nums.size - 1) {\n        nums[i] = nums[i + 1]\n    }\n}\n
        array.rb
        ### \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 ###\ndef remove(nums, index)\n  # \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n  for i in index...(nums.length - 1)\n    nums[i] = nums[i + 1]\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u603b\u7684\u6765\u770b\uff0c\u6570\u7ec4\u7684\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u6709\u4ee5\u4e0b\u7f3a\u70b9\u3002

        • \u65f6\u95f4\u590d\u6742\u5ea6\u9ad8\uff1a\u6570\u7ec4\u7684\u63d2\u5165\u548c\u5220\u9664\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u6570\u7ec4\u957f\u5ea6\u3002
        • \u4e22\u5931\u5143\u7d20\uff1a\u7531\u4e8e\u6570\u7ec4\u7684\u957f\u5ea6\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u5728\u63d2\u5165\u5143\u7d20\u540e\uff0c\u8d85\u51fa\u6570\u7ec4\u957f\u5ea6\u8303\u56f4\u7684\u5143\u7d20\u4f1a\u4e22\u5931\u3002
        • \u5185\u5b58\u6d6a\u8d39\uff1a\u6211\u4eec\u53ef\u4ee5\u521d\u59cb\u5316\u4e00\u4e2a\u6bd4\u8f83\u957f\u7684\u6570\u7ec4\uff0c\u53ea\u7528\u524d\u9762\u4e00\u90e8\u5206\uff0c\u8fd9\u6837\u5728\u63d2\u5165\u6570\u636e\u65f6\uff0c\u4e22\u5931\u7684\u672b\u5c3e\u5143\u7d20\u90fd\u662f\u201c\u65e0\u610f\u4e49\u201d\u7684\uff0c\u4f46\u8fd9\u6837\u505a\u4f1a\u9020\u6210\u90e8\u5206\u5185\u5b58\u7a7a\u95f4\u6d6a\u8d39\u3002
        "},{"location":"chapter_array_and_linkedlist/array/#5","title":"5. \u00a0 \u904d\u5386\u6570\u7ec4","text":"

        \u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u65e2\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u904d\u5386\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def traverse(nums: list[int]):\n    \"\"\"\u904d\u5386\u6570\u7ec4\"\"\"\n    count = 0\n    # \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n    for i in range(len(nums)):\n        count += nums[i]\n    # \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n    for num in nums:\n        count += num\n    # \u540c\u65f6\u904d\u5386\u6570\u636e\u7d22\u5f15\u548c\u5143\u7d20\n    for i, num in enumerate(nums):\n        count += nums[i]\n        count += num\n
        array.cpp
        /* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int *nums, int size) {\n    int count = 0;\n    // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n    for (int i = 0; i < size; i++) {\n        count += nums[i];\n    }\n}\n
        array.java
        /* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int[] nums) {\n    int count = 0;\n    // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n    for (int i = 0; i < nums.length; i++) {\n        count += nums[i];\n    }\n    // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n    for (int num : nums) {\n        count += num;\n    }\n}\n
        array.cs
        /* \u904d\u5386\u6570\u7ec4 */\nvoid Traverse(int[] nums) {\n    int count = 0;\n    // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n    for (int i = 0; i < nums.Length; i++) {\n        count += nums[i];\n    }\n    // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n    foreach (int num in nums) {\n        count += num;\n    }\n}\n
        array.go
        /* \u904d\u5386\u6570\u7ec4 */\nfunc traverse(nums []int) {\n    count := 0\n    // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n    for i := 0; i < len(nums); i++ {\n        count += nums[i]\n    }\n    count = 0\n    // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n    for _, num := range nums {\n        count += num\n    }\n    // \u540c\u65f6\u904d\u5386\u6570\u636e\u7d22\u5f15\u548c\u5143\u7d20\n    for i, num := range nums {\n        count += nums[i]\n        count += num\n    }\n}\n
        array.swift
        /* \u904d\u5386\u6570\u7ec4 */\nfunc traverse(nums: [Int]) {\n    var count = 0\n    // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n    for i in nums.indices {\n        count += nums[i]\n    }\n    // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n    for num in nums {\n        count += num\n    }\n    // \u540c\u65f6\u904d\u5386\u6570\u636e\u7d22\u5f15\u548c\u5143\u7d20\n    for (i, num) in nums.enumerated() {\n        count += nums[i]\n        count += num\n    }\n}\n
        array.js
        /* \u904d\u5386\u6570\u7ec4 */\nfunction traverse(nums) {\n    let count = 0;\n    // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n    for (let i = 0; i < nums.length; i++) {\n        count += nums[i];\n    }\n    // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n    for (const num of nums) {\n        count += num;\n    }\n}\n
        array.ts
        /* \u904d\u5386\u6570\u7ec4 */\nfunction traverse(nums: number[]): void {\n    let count = 0;\n    // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n    for (let i = 0; i < nums.length; i++) {\n        count += nums[i];\n    }\n    // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n    for (const num of nums) {\n        count += num;\n    }\n}\n
        array.dart
        /* \u904d\u5386\u6570\u7ec4\u5143\u7d20 */\nvoid traverse(List<int> nums) {\n  int count = 0;\n  // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n  for (var i = 0; i < nums.length; i++) {\n    count += nums[i];\n  }\n  // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n  for (int _num in nums) {\n    count += _num;\n  }\n  // \u901a\u8fc7 forEach \u65b9\u6cd5\u904d\u5386\u6570\u7ec4\n  nums.forEach((_num) {\n    count += _num;\n  });\n}\n
        array.rs
        /* \u904d\u5386\u6570\u7ec4 */\nfn traverse(nums: &[i32]) {\n    let mut _count = 0;\n    // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n    for i in 0..nums.len() {\n        _count += nums[i];\n    }\n    // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n    _count = 0;\n    for &num in nums {\n        _count += num;\n    }\n}\n
        array.c
        /* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int *nums, int size) {\n    int count = 0;\n    // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n    for (int i = 0; i < size; i++) {\n        count += nums[i];\n    }\n}\n
        array.kt
        /* \u904d\u5386\u6570\u7ec4 */\nfun traverse(nums: IntArray) {\n    var count = 0\n    // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n    for (i in nums.indices) {\n        count += nums[i]\n    }\n    // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n    for (j in nums) {\n        count += j\n    }\n}\n
        array.rb
        ### \u904d\u5386\u6570\u7ec4 ###\ndef traverse(nums)\n  count = 0\n\n  # \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n  for i in 0...nums.length\n    count += nums[i]\n  end\n\n  # \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n  for num in nums\n    count += num\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/array/#6","title":"6. \u00a0 \u67e5\u627e\u5143\u7d20","text":"

        \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\u9700\u8981\u904d\u5386\u6570\u7ec4\uff0c\u6bcf\u8f6e\u5224\u65ad\u5143\u7d20\u503c\u662f\u5426\u5339\u914d\uff0c\u82e5\u5339\u914d\u5219\u8f93\u51fa\u5bf9\u5e94\u7d22\u5f15\u3002

        \u56e0\u4e3a\u6570\u7ec4\u662f\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u6240\u4ee5\u4e0a\u8ff0\u67e5\u627e\u64cd\u4f5c\u88ab\u79f0\u4e3a\u201c\u7ebf\u6027\u67e5\u627e\u201d\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def find(nums: list[int], target: int) -> int:\n    \"\"\"\u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\"\"\"\n    for i in range(len(nums)):\n        if nums[i] == target:\n            return i\n    return -1\n
        array.cpp
        /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int *nums, int size, int target) {\n    for (int i = 0; i < size; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
        array.java
        /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int[] nums, int target) {\n    for (int i = 0; i < nums.length; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
        array.cs
        /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint Find(int[] nums, int target) {\n    for (int i = 0; i < nums.Length; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
        array.go
        /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunc find(nums []int, target int) (index int) {\n    index = -1\n    for i := 0; i < len(nums); i++ {\n        if nums[i] == target {\n            index = i\n            break\n        }\n    }\n    return\n}\n
        array.swift
        /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunc find(nums: [Int], target: Int) -> Int {\n    for i in nums.indices {\n        if nums[i] == target {\n            return i\n        }\n    }\n    return -1\n}\n
        array.js
        /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunction find(nums, target) {\n    for (let i = 0; i < nums.length; i++) {\n        if (nums[i] === target) return i;\n    }\n    return -1;\n}\n
        array.ts
        /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunction find(nums: number[], target: number): number {\n    for (let i = 0; i < nums.length; i++) {\n        if (nums[i] === target) {\n            return i;\n        }\n    }\n    return -1;\n}\n
        array.dart
        /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(List<int> nums, int target) {\n  for (var i = 0; i < nums.length; i++) {\n    if (nums[i] == target) return i;\n  }\n  return -1;\n}\n
        array.rs
        /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfn find(nums: &[i32], target: i32) -> Option<usize> {\n    for i in 0..nums.len() {\n        if nums[i] == target {\n            return Some(i);\n        }\n    }\n    None\n}\n
        array.c
        /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int *nums, int size, int target) {\n    for (int i = 0; i < size; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
        array.kt
        /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfun find(nums: IntArray, target: Int): Int {\n    for (i in nums.indices) {\n        if (nums[i] == target)\n            return i\n    }\n    return -1\n}\n
        array.rb
        ### \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 ###\ndef find(nums, target)\n  for i in 0...nums.length\n    return i if nums[i] == target\n  end\n\n  -1\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/array/#7","title":"7. \u00a0 \u6269\u5bb9\u6570\u7ec4","text":"

        \u5728\u590d\u6742\u7684\u7cfb\u7edf\u73af\u5883\u4e2d\uff0c\u7a0b\u5e8f\u96be\u4ee5\u4fdd\u8bc1\u6570\u7ec4\u4e4b\u540e\u7684\u5185\u5b58\u7a7a\u95f4\u662f\u53ef\u7528\u7684\uff0c\u4ece\u800c\u65e0\u6cd5\u5b89\u5168\u5730\u6269\u5c55\u6570\u7ec4\u5bb9\u91cf\u3002\u56e0\u6b64\u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u4e0d\u53ef\u53d8\u7684\u3002

        \u5982\u679c\u6211\u4eec\u5e0c\u671b\u6269\u5bb9\u6570\u7ec4\uff0c\u5219\u9700\u91cd\u65b0\u5efa\u7acb\u4e00\u4e2a\u66f4\u5927\u7684\u6570\u7ec4\uff0c\u7136\u540e\u628a\u539f\u6570\u7ec4\u5143\u7d20\u4f9d\u6b21\u590d\u5236\u5230\u65b0\u6570\u7ec4\u3002\u8fd9\u662f\u4e00\u4e2a \\(O(n)\\) \u7684\u64cd\u4f5c\uff0c\u5728\u6570\u7ec4\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\u975e\u5e38\u8017\u65f6\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
        def extend(nums: list[int], enlarge: int) -> list[int]:\n    \"\"\"\u6269\u5c55\u6570\u7ec4\u957f\u5ea6\"\"\"\n    # \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n    res = [0] * (len(nums) + enlarge)\n    # \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    for i in range(len(nums)):\n        res[i] = nums[i]\n    # \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n    return res\n
        array.cpp
        /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint *extend(int *nums, int size, int enlarge) {\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n    int *res = new int[size + enlarge];\n    // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    for (int i = 0; i < size; i++) {\n        res[i] = nums[i];\n    }\n    // \u91ca\u653e\u5185\u5b58\n    delete[] nums;\n    // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n    return res;\n}\n
        array.java
        /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint[] extend(int[] nums, int enlarge) {\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n    int[] res = new int[nums.length + enlarge];\n    // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    for (int i = 0; i < nums.length; i++) {\n        res[i] = nums[i];\n    }\n    // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n    return res;\n}\n
        array.cs
        /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint[] Extend(int[] nums, int enlarge) {\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n    int[] res = new int[nums.Length + enlarge];\n    // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    for (int i = 0; i < nums.Length; i++) {\n        res[i] = nums[i];\n    }\n    // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n    return res;\n}\n
        array.go
        /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfunc extend(nums []int, enlarge int) []int {\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n    res := make([]int, len(nums)+enlarge)\n    // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    for i, num := range nums {\n        res[i] = num\n    }\n    // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n    return res\n}\n
        array.swift
        /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfunc extend(nums: [Int], enlarge: Int) -> [Int] {\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n    var res = Array(repeating: 0, count: nums.count + enlarge)\n    // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    for i in nums.indices {\n        res[i] = nums[i]\n    }\n    // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n    return res\n}\n
        array.js
        /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\n// \u8bf7\u6ce8\u610f\uff0cJavaScript \u7684 Array \u662f\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u6269\u5c55\n// \u4e3a\u4e86\u65b9\u4fbf\u5b66\u4e60\uff0c\u672c\u51fd\u6570\u5c06 Array \u770b\u4f5c\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6570\u7ec4\nfunction extend(nums, enlarge) {\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n    const res = new Array(nums.length + enlarge).fill(0);\n    // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    for (let i = 0; i < nums.length; i++) {\n        res[i] = nums[i];\n    }\n    // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n    return res;\n}\n
        array.ts
        /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\n// \u8bf7\u6ce8\u610f\uff0cTypeScript \u7684 Array \u662f\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u6269\u5c55\n// \u4e3a\u4e86\u65b9\u4fbf\u5b66\u4e60\uff0c\u672c\u51fd\u6570\u5c06 Array \u770b\u4f5c\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6570\u7ec4\nfunction extend(nums: number[], enlarge: number): number[] {\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n    const res = new Array(nums.length + enlarge).fill(0);\n    // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    for (let i = 0; i < nums.length; i++) {\n        res[i] = nums[i];\n    }\n    // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n    return res;\n}\n
        array.dart
        /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nList<int> extend(List<int> nums, int enlarge) {\n  // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n  List<int> res = List.filled(nums.length + enlarge, 0);\n  // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n  for (var i = 0; i < nums.length; i++) {\n    res[i] = nums[i];\n  }\n  // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n  return res;\n}\n
        array.rs
        /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfn extend(nums: &[i32], enlarge: usize) -> Vec<i32> {\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n    let mut res: Vec<i32> = vec![0; nums.len() + enlarge];\n    // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\n    res[0..nums.len()].copy_from_slice(nums);\n\n    // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n    res\n}\n
        array.c
        /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint *extend(int *nums, int size, int enlarge) {\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n    int *res = (int *)malloc(sizeof(int) * (size + enlarge));\n    // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    for (int i = 0; i < size; i++) {\n        res[i] = nums[i];\n    }\n    // \u521d\u59cb\u5316\u6269\u5c55\u540e\u7684\u7a7a\u95f4\n    for (int i = size; i < size + enlarge; i++) {\n        res[i] = 0;\n    }\n    // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n    return res;\n}\n
        array.kt
        /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfun extend(nums: IntArray, enlarge: Int): IntArray {\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n    val res = IntArray(nums.size + enlarge)\n    // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    for (i in nums.indices) {\n        res[i] = nums[i]\n    }\n    // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n    return res\n}\n
        array.rb
        ### \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 ###\n# \u8bf7\u6ce8\u610f\uff0cRuby \u7684 Array \u662f\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u6269\u5c55\n# \u4e3a\u4e86\u65b9\u4fbf\u5b66\u4e60\uff0c\u672c\u51fd\u6570\u5c06 Array \u770b\u4f5c\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6570\u7ec4\ndef extend(nums, enlarge)\n  # \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n  res = Array.new(nums.length + enlarge, 0)\n\n  # \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n  for i in 0...nums.length\n    res[i] = nums[i]\n  end\n\n  # \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/array/#412","title":"4.1.2 \u00a0 \u6570\u7ec4\u7684\u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"

        \u6570\u7ec4\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u5185\uff0c\u4e14\u5143\u7d20\u7c7b\u578b\u76f8\u540c\u3002\u8fd9\u79cd\u505a\u6cd5\u5305\u542b\u4e30\u5bcc\u7684\u5148\u9a8c\u4fe1\u606f\uff0c\u7cfb\u7edf\u53ef\u4ee5\u5229\u7528\u8fd9\u4e9b\u4fe1\u606f\u6765\u4f18\u5316\u6570\u636e\u7ed3\u6784\u7684\u64cd\u4f5c\u6548\u7387\u3002

        • \u7a7a\u95f4\u6548\u7387\u9ad8\uff1a\u6570\u7ec4\u4e3a\u6570\u636e\u5206\u914d\u4e86\u8fde\u7eed\u7684\u5185\u5b58\u5757\uff0c\u65e0\u987b\u989d\u5916\u7684\u7ed3\u6784\u5f00\u9500\u3002
        • \u652f\u6301\u968f\u673a\u8bbf\u95ee\uff1a\u6570\u7ec4\u5141\u8bb8\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u8bbf\u95ee\u4efb\u4f55\u5143\u7d20\u3002
        • \u7f13\u5b58\u5c40\u90e8\u6027\uff1a\u5f53\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u65f6\uff0c\u8ba1\u7b97\u673a\u4e0d\u4ec5\u4f1a\u52a0\u8f7d\u5b83\uff0c\u8fd8\u4f1a\u7f13\u5b58\u5176\u5468\u56f4\u7684\u5176\u4ed6\u6570\u636e\uff0c\u4ece\u800c\u501f\u52a9\u9ad8\u901f\u7f13\u5b58\u6765\u63d0\u5347\u540e\u7eed\u64cd\u4f5c\u7684\u6267\u884c\u901f\u5ea6\u3002

        \u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u662f\u4e00\u628a\u53cc\u5203\u5251\uff0c\u5176\u5b58\u5728\u4ee5\u4e0b\u5c40\u9650\u6027\u3002

        • \u63d2\u5165\u4e0e\u5220\u9664\u6548\u7387\u4f4e\uff1a\u5f53\u6570\u7ec4\u4e2d\u5143\u7d20\u8f83\u591a\u65f6\uff0c\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u9700\u8981\u79fb\u52a8\u5927\u91cf\u7684\u5143\u7d20\u3002
        • \u957f\u5ea6\u4e0d\u53ef\u53d8\uff1a\u6570\u7ec4\u5728\u521d\u59cb\u5316\u540e\u957f\u5ea6\u5c31\u56fa\u5b9a\u4e86\uff0c\u6269\u5bb9\u6570\u7ec4\u9700\u8981\u5c06\u6240\u6709\u6570\u636e\u590d\u5236\u5230\u65b0\u6570\u7ec4\uff0c\u5f00\u9500\u5f88\u5927\u3002
        • \u7a7a\u95f4\u6d6a\u8d39\uff1a\u5982\u679c\u6570\u7ec4\u5206\u914d\u7684\u5927\u5c0f\u8d85\u8fc7\u5b9e\u9645\u6240\u9700\uff0c\u90a3\u4e48\u591a\u4f59\u7684\u7a7a\u95f4\u5c31\u88ab\u6d6a\u8d39\u4e86\u3002
        "},{"location":"chapter_array_and_linkedlist/array/#413","title":"4.1.3 \u00a0 \u6570\u7ec4\u5178\u578b\u5e94\u7528","text":"

        \u6570\u7ec4\u662f\u4e00\u79cd\u57fa\u7840\u4e14\u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\uff0c\u65e2\u9891\u7e41\u5e94\u7528\u5728\u5404\u7c7b\u7b97\u6cd5\u4e4b\u4e2d\uff0c\u4e5f\u53ef\u7528\u4e8e\u5b9e\u73b0\u5404\u79cd\u590d\u6742\u6570\u636e\u7ed3\u6784\u3002

        • \u968f\u673a\u8bbf\u95ee\uff1a\u5982\u679c\u6211\u4eec\u60f3\u968f\u673a\u62bd\u53d6\u4e00\u4e9b\u6837\u672c\uff0c\u90a3\u4e48\u53ef\u4ee5\u7528\u6570\u7ec4\u5b58\u50a8\uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u968f\u673a\u5e8f\u5217\uff0c\u6839\u636e\u7d22\u5f15\u5b9e\u73b0\u968f\u673a\u62bd\u6837\u3002
        • \u6392\u5e8f\u548c\u641c\u7d22\uff1a\u6570\u7ec4\u662f\u6392\u5e8f\u548c\u641c\u7d22\u7b97\u6cd5\u6700\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u3001\u4e8c\u5206\u67e5\u627e\u7b49\u90fd\u4e3b\u8981\u5728\u6570\u7ec4\u4e0a\u8fdb\u884c\u3002
        • \u67e5\u627e\u8868\uff1a\u5f53\u9700\u8981\u5feb\u901f\u67e5\u627e\u4e00\u4e2a\u5143\u7d20\u6216\u5176\u5bf9\u5e94\u5173\u7cfb\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u4f5c\u4e3a\u67e5\u627e\u8868\u3002\u5047\u5982\u6211\u4eec\u60f3\u5b9e\u73b0\u5b57\u7b26\u5230 ASCII \u7801\u7684\u6620\u5c04\uff0c\u5219\u53ef\u4ee5\u5c06\u5b57\u7b26\u7684 ASCII \u7801\u503c\u4f5c\u4e3a\u7d22\u5f15\uff0c\u5bf9\u5e94\u7684\u5143\u7d20\u5b58\u653e\u5728\u6570\u7ec4\u4e2d\u7684\u5bf9\u5e94\u4f4d\u7f6e\u3002
        • \u673a\u5668\u5b66\u4e60\uff1a\u795e\u7ecf\u7f51\u7edc\u4e2d\u5927\u91cf\u4f7f\u7528\u4e86\u5411\u91cf\u3001\u77e9\u9635\u3001\u5f20\u91cf\u4e4b\u95f4\u7684\u7ebf\u6027\u4ee3\u6570\u8fd0\u7b97\uff0c\u8fd9\u4e9b\u6570\u636e\u90fd\u662f\u4ee5\u6570\u7ec4\u7684\u5f62\u5f0f\u6784\u5efa\u7684\u3002\u6570\u7ec4\u662f\u795e\u7ecf\u7f51\u7edc\u7f16\u7a0b\u4e2d\u6700\u5e38\u4f7f\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002
        • \u6570\u636e\u7ed3\u6784\u5b9e\u73b0\uff1a\u6570\u7ec4\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u5806\u3001\u56fe\u7b49\u6570\u636e\u7ed3\u6784\u3002\u4f8b\u5982\uff0c\u56fe\u7684\u90bb\u63a5\u77e9\u9635\u8868\u793a\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u3002
        "},{"location":"chapter_array_and_linkedlist/linked_list/","title":"4.2 \u00a0 \u94fe\u8868","text":"

        \u5185\u5b58\u7a7a\u95f4\u662f\u6240\u6709\u7a0b\u5e8f\u7684\u516c\u5171\u8d44\u6e90\uff0c\u5728\u4e00\u4e2a\u590d\u6742\u7684\u7cfb\u7edf\u8fd0\u884c\u73af\u5883\u4e0b\uff0c\u7a7a\u95f2\u7684\u5185\u5b58\u7a7a\u95f4\u53ef\u80fd\u6563\u843d\u5728\u5185\u5b58\u5404\u5904\u3002\u6211\u4eec\u77e5\u9053\uff0c\u5b58\u50a8\u6570\u7ec4\u7684\u5185\u5b58\u7a7a\u95f4\u5fc5\u987b\u662f\u8fde\u7eed\u7684\uff0c\u800c\u5f53\u6570\u7ec4\u975e\u5e38\u5927\u65f6\uff0c\u5185\u5b58\u53ef\u80fd\u65e0\u6cd5\u63d0\u4f9b\u5982\u6b64\u5927\u7684\u8fde\u7eed\u7a7a\u95f4\u3002\u6b64\u65f6\u94fe\u8868\u7684\u7075\u6d3b\u6027\u4f18\u52bf\u5c31\u4f53\u73b0\u51fa\u6765\u4e86\u3002

        \u94fe\u8868\uff08linked list\uff09\u662f\u4e00\u79cd\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u8282\u70b9\u5bf9\u8c61\uff0c\u5404\u4e2a\u8282\u70b9\u901a\u8fc7\u201c\u5f15\u7528\u201d\u76f8\u8fde\u63a5\u3002\u5f15\u7528\u8bb0\u5f55\u4e86\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u5185\u5b58\u5730\u5740\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u4ece\u5f53\u524d\u8282\u70b9\u8bbf\u95ee\u5230\u4e0b\u4e00\u4e2a\u8282\u70b9\u3002

        \u94fe\u8868\u7684\u8bbe\u8ba1\u4f7f\u5f97\u5404\u4e2a\u8282\u70b9\u53ef\u4ee5\u5206\u6563\u5b58\u50a8\u5728\u5185\u5b58\u5404\u5904\uff0c\u5b83\u4eec\u7684\u5185\u5b58\u5730\u5740\u65e0\u987b\u8fde\u7eed\u3002

        \u56fe 4-5 \u00a0 \u94fe\u8868\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f

        \u89c2\u5bdf\u56fe 4-5 \uff0c\u94fe\u8868\u7684\u7ec4\u6210\u5355\u4f4d\u662f\u8282\u70b9\uff08node\uff09\u5bf9\u8c61\u3002\u6bcf\u4e2a\u8282\u70b9\u90fd\u5305\u542b\u4e24\u9879\u6570\u636e\uff1a\u8282\u70b9\u7684\u201c\u503c\u201d\u548c\u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u201c\u5f15\u7528\u201d\u3002

        • \u94fe\u8868\u7684\u9996\u4e2a\u8282\u70b9\u88ab\u79f0\u4e3a\u201c\u5934\u8282\u70b9\u201d\uff0c\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u88ab\u79f0\u4e3a\u201c\u5c3e\u8282\u70b9\u201d\u3002
        • \u5c3e\u8282\u70b9\u6307\u5411\u7684\u662f\u201c\u7a7a\u201d\uff0c\u5b83\u5728 Java\u3001C++ \u548c Python \u4e2d\u5206\u522b\u88ab\u8bb0\u4e3a null\u3001nullptr \u548c None \u3002
        • \u5728 C\u3001C++\u3001Go \u548c Rust \u7b49\u652f\u6301\u6307\u9488\u7684\u8bed\u8a00\u4e2d\uff0c\u4e0a\u8ff0\u201c\u5f15\u7528\u201d\u5e94\u88ab\u66ff\u6362\u4e3a\u201c\u6307\u9488\u201d\u3002

        \u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u94fe\u8868\u8282\u70b9 ListNode \u9664\u4e86\u5305\u542b\u503c\uff0c\u8fd8\u9700\u989d\u5916\u4fdd\u5b58\u4e00\u4e2a\u5f15\u7528\uff08\u6307\u9488\uff09\u3002\u56e0\u6b64\u5728\u76f8\u540c\u6570\u636e\u91cf\u4e0b\uff0c\u94fe\u8868\u6bd4\u6570\u7ec4\u5360\u7528\u66f4\u591a\u7684\u5185\u5b58\u7a7a\u95f4\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        class ListNode:\n    \"\"\"\u94fe\u8868\u8282\u70b9\u7c7b\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val               # \u8282\u70b9\u503c\n        self.next: ListNode | None = None # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n
        /* \u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\n    int val;         // \u8282\u70b9\u503c\n    ListNode *next;  // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n    ListNode(int x) : val(x), next(nullptr) {}  // \u6784\u9020\u51fd\u6570\n};\n
        /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n    int val;        // \u8282\u70b9\u503c\n    ListNode next;  // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n    ListNode(int x) { val = x; }  // \u6784\u9020\u51fd\u6570\n}\n
        /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode(int x) {  //\u6784\u9020\u51fd\u6570\n    int val = x;         // \u8282\u70b9\u503c\n    ListNode? next;      // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n}\n
        /* \u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype ListNode struct {\n    Val  int       // \u8282\u70b9\u503c\n    Next *ListNode // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n}\n\n// NewListNode \u6784\u9020\u51fd\u6570\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u94fe\u8868\nfunc NewListNode(val int) *ListNode {\n    return &ListNode{\n        Val:  val,\n        Next: nil,\n    }\n}\n
        /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n    var val: Int // \u8282\u70b9\u503c\n    var next: ListNode? // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n\n    init(x: Int) { // \u6784\u9020\u51fd\u6570\n        val = x\n    }\n}\n
        /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n    constructor(val, next) {\n        this.val = (val === undefined ? 0 : val);       // \u8282\u70b9\u503c\n        this.next = (next === undefined ? null : next); // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n    }\n}\n
        /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n    val: number;\n    next: ListNode | null;\n    constructor(val?: number, next?: ListNode | null) {\n        this.val = val === undefined ? 0 : val;        // \u8282\u70b9\u503c\n        this.next = next === undefined ? null : next;  // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n    }\n}\n
        /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n  int val; // \u8282\u70b9\u503c\n  ListNode? next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n  ListNode(this.val, [this.next]); // \u6784\u9020\u51fd\u6570\n}\n
        use std::rc::Rc;\nuse std::cell::RefCell;\n/* \u94fe\u8868\u8282\u70b9\u7c7b */\n#[derive(Debug)]\nstruct ListNode {\n    val: i32, // \u8282\u70b9\u503c\n    next: Option<Rc<RefCell<ListNode>>>, // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n}\n
        /* \u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\ntypedef struct ListNode {\n    int val;               // \u8282\u70b9\u503c\n    struct ListNode *next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n} ListNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nListNode *newListNode(int val) {\n    ListNode *node;\n    node = (ListNode *) malloc(sizeof(ListNode));\n    node->val = val;\n    node->next = NULL;\n    return node;\n}\n
        /* \u94fe\u8868\u8282\u70b9\u7c7b */\n// \u6784\u9020\u65b9\u6cd5\nclass ListNode(x: Int) {\n    val _val: Int = x          // \u8282\u70b9\u503c\n    val next: ListNode? = null // \u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u5f15\u7528\n}\n
        # \u94fe\u8868\u8282\u70b9\u7c7b\nclass ListNode\n  attr_accessor :val  # \u8282\u70b9\u503c\n  attr_accessor :next # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n\n  def initialize(val=0, next_node=nil)\n    @val = val\n    @next = next_node\n  end\nend\n
        "},{"location":"chapter_array_and_linkedlist/linked_list/#421","title":"4.2.1 \u00a0 \u94fe\u8868\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/linked_list/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u94fe\u8868","text":"

        \u5efa\u7acb\u94fe\u8868\u5206\u4e3a\u4e24\u6b65\uff0c\u7b2c\u4e00\u6b65\u662f\u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\u5bf9\u8c61\uff0c\u7b2c\u4e8c\u6b65\u662f\u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\u5173\u7cfb\u3002\u521d\u59cb\u5316\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4ece\u94fe\u8868\u7684\u5934\u8282\u70b9\u51fa\u53d1\uff0c\u901a\u8fc7\u5f15\u7528\u6307\u5411 next \u4f9d\u6b21\u8bbf\u95ee\u6240\u6709\u8282\u70b9\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
        # \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4\n# \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nn0 = ListNode(1)\nn1 = ListNode(3)\nn2 = ListNode(2)\nn3 = ListNode(5)\nn4 = ListNode(4)\n# \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
        linked_list.cpp
        /* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode* n0 = new ListNode(1);\nListNode* n1 = new ListNode(3);\nListNode* n2 = new ListNode(2);\nListNode* n3 = new ListNode(5);\nListNode* n4 = new ListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
        linked_list.java
        /* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode n0 = new ListNode(1);\nListNode n1 = new ListNode(3);\nListNode n2 = new ListNode(2);\nListNode n3 = new ListNode(5);\nListNode n4 = new ListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
        linked_list.cs
        /* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode n0 = new(1);\nListNode n1 = new(3);\nListNode n2 = new(2);\nListNode n3 = new(5);\nListNode n4 = new(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
        linked_list.go
        /* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nn0 := NewListNode(1)\nn1 := NewListNode(3)\nn2 := NewListNode(2)\nn3 := NewListNode(5)\nn4 := NewListNode(4)\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.Next = n1\nn1.Next = n2\nn2.Next = n3\nn3.Next = n4\n
        linked_list.swift
        /* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nlet n0 = ListNode(x: 1)\nlet n1 = ListNode(x: 3)\nlet n2 = ListNode(x: 2)\nlet n3 = ListNode(x: 5)\nlet n4 = ListNode(x: 4)\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
        linked_list.js
        /* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
        linked_list.ts
        /* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
        linked_list.dart
        /* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\\\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode n0 = ListNode(1);\nListNode n1 = ListNode(3);\nListNode n2 = ListNode(2);\nListNode n3 = ListNode(5);\nListNode n4 = ListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
        linked_list.rs
        /* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nlet n0 = Rc::new(RefCell::new(ListNode { val: 1, next: None }));\nlet n1 = Rc::new(RefCell::new(ListNode { val: 3, next: None }));\nlet n2 = Rc::new(RefCell::new(ListNode { val: 2, next: None }));\nlet n3 = Rc::new(RefCell::new(ListNode { val: 5, next: None }));\nlet n4 = Rc::new(RefCell::new(ListNode { val: 4, next: None }));\n\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.borrow_mut().next = Some(n1.clone());\nn1.borrow_mut().next = Some(n2.clone());\nn2.borrow_mut().next = Some(n3.clone());\nn3.borrow_mut().next = Some(n4.clone());\n
        linked_list.c
        /* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode* n0 = newListNode(1);\nListNode* n1 = newListNode(3);\nListNode* n2 = newListNode(2);\nListNode* n3 = newListNode(5);\nListNode* n4 = newListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
        linked_list.kt
        /* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nval n0 = ListNode(1)\nval n1 = ListNode(3)\nval n2 = ListNode(2)\nval n3 = ListNode(5)\nval n4 = ListNode(4)\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
        linked_list.rb
        # \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4\n# \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nn0 = ListNode.new(1)\nn1 = ListNode.new(3)\nn2 = ListNode.new(2)\nn3 = ListNode.new(5)\nn4 = ListNode.new(4)\n# \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u6570\u7ec4\u6574\u4f53\u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u6bd4\u5982\u6570\u7ec4 nums \u5305\u542b\u5143\u7d20 nums[0] \u548c nums[1] \u7b49\uff0c\u800c\u94fe\u8868\u662f\u7531\u591a\u4e2a\u72ec\u7acb\u7684\u8282\u70b9\u5bf9\u8c61\u7ec4\u6210\u7684\u3002\u6211\u4eec\u901a\u5e38\u5c06\u5934\u8282\u70b9\u5f53\u4f5c\u94fe\u8868\u7684\u4ee3\u79f0\uff0c\u6bd4\u5982\u4ee5\u4e0a\u4ee3\u7801\u4e2d\u7684\u94fe\u8868\u53ef\u8bb0\u4f5c\u94fe\u8868 n0 \u3002

        "},{"location":"chapter_array_and_linkedlist/linked_list/#2","title":"2. \u00a0 \u63d2\u5165\u8282\u70b9","text":"

        \u5728\u94fe\u8868\u4e2d\u63d2\u5165\u8282\u70b9\u975e\u5e38\u5bb9\u6613\u3002\u5982\u56fe 4-6 \u6240\u793a\uff0c\u5047\u8bbe\u6211\u4eec\u60f3\u5728\u76f8\u90bb\u7684\u4e24\u4e2a\u8282\u70b9 n0 \u548c n1 \u4e4b\u95f4\u63d2\u5165\u4e00\u4e2a\u65b0\u8282\u70b9 P \uff0c\u5219\u53ea\u9700\u6539\u53d8\u4e24\u4e2a\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\u5373\u53ef\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002

        \u76f8\u6bd4\u4e4b\u4e0b\uff0c\u5728\u6570\u7ec4\u4e2d\u63d2\u5165\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5728\u5927\u6570\u636e\u91cf\u4e0b\u7684\u6548\u7387\u8f83\u4f4e\u3002

        \u56fe 4-6 \u00a0 \u94fe\u8868\u63d2\u5165\u8282\u70b9\u793a\u4f8b

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
        def insert(n0: ListNode, P: ListNode):\n    \"\"\"\u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P\"\"\"\n    n1 = n0.next\n    P.next = n1\n    n0.next = P\n
        linked_list.cpp
        /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode *n0, ListNode *P) {\n    ListNode *n1 = n0->next;\n    P->next = n1;\n    n0->next = P;\n}\n
        linked_list.java
        /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode n0, ListNode P) {\n    ListNode n1 = n0.next;\n    P.next = n1;\n    n0.next = P;\n}\n
        linked_list.cs
        /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid Insert(ListNode n0, ListNode P) {\n    ListNode? n1 = n0.next;\n    P.next = n1;\n    n0.next = P;\n}\n
        linked_list.go
        /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunc insertNode(n0 *ListNode, P *ListNode) {\n    n1 := n0.Next\n    P.Next = n1\n    n0.Next = P\n}\n
        linked_list.swift
        /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunc insert(n0: ListNode, P: ListNode) {\n    let n1 = n0.next\n    P.next = n1\n    n0.next = P\n}\n
        linked_list.js
        /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunction insert(n0, P) {\n    const n1 = n0.next;\n    P.next = n1;\n    n0.next = P;\n}\n
        linked_list.ts
        /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunction insert(n0: ListNode, P: ListNode): void {\n    const n1 = n0.next;\n    P.next = n1;\n    n0.next = P;\n}\n
        linked_list.dart
        /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode n0, ListNode P) {\n  ListNode? n1 = n0.next;\n  P.next = n1;\n  n0.next = P;\n}\n
        linked_list.rs
        /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\n#[allow(non_snake_case)]\npub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) {\n    let n1 = n0.borrow_mut().next.take();\n    P.borrow_mut().next = n1;\n    n0.borrow_mut().next = Some(P);\n}\n
        linked_list.c
        /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode *n0, ListNode *P) {\n    ListNode *n1 = n0->next;\n    P->next = n1;\n    n0->next = P;\n}\n
        linked_list.kt
        /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfun insert(n0: ListNode?, p: ListNode?) {\n    val n1 = n0?.next\n    p?.next = n1\n    n0?.next = p\n}\n
        linked_list.rb
        ### \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 _p ###\n# Ruby \u7684 `p` \u662f\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c `P` \u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528 `_p` \u4ee3\u66ff\ndef insert(n0, _p)\n  n1 = n0.next\n  _p.next = n1\n  n0.next = _p\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/linked_list/#3","title":"3. \u00a0 \u5220\u9664\u8282\u70b9","text":"

        \u5982\u56fe 4-7 \u6240\u793a\uff0c\u5728\u94fe\u8868\u4e2d\u5220\u9664\u8282\u70b9\u4e5f\u975e\u5e38\u65b9\u4fbf\uff0c\u53ea\u9700\u6539\u53d8\u4e00\u4e2a\u8282\u70b9\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\u5373\u53ef\u3002

        \u8bf7\u6ce8\u610f\uff0c\u5c3d\u7ba1\u5728\u5220\u9664\u64cd\u4f5c\u5b8c\u6210\u540e\u8282\u70b9 P \u4ecd\u7136\u6307\u5411 n1 \uff0c\u4f46\u5b9e\u9645\u4e0a\u904d\u5386\u6b64\u94fe\u8868\u5df2\u7ecf\u65e0\u6cd5\u8bbf\u95ee\u5230 P \uff0c\u8fd9\u610f\u5473\u7740 P \u5df2\u7ecf\u4e0d\u518d\u5c5e\u4e8e\u8be5\u94fe\u8868\u4e86\u3002

        \u56fe 4-7 \u00a0 \u94fe\u8868\u5220\u9664\u8282\u70b9

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
        def remove(n0: ListNode):\n    \"\"\"\u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9\"\"\"\n    if not n0.next:\n        return\n    # n0 -> P -> n1\n    P = n0.next\n    n1 = P.next\n    n0.next = n1\n
        linked_list.cpp
        /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode *n0) {\n    if (n0->next == nullptr)\n        return;\n    // n0 -> P -> n1\n    ListNode *P = n0->next;\n    ListNode *n1 = P->next;\n    n0->next = n1;\n    // \u91ca\u653e\u5185\u5b58\n    delete P;\n}\n
        linked_list.java
        /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode n0) {\n    if (n0.next == null)\n        return;\n    // n0 -> P -> n1\n    ListNode P = n0.next;\n    ListNode n1 = P.next;\n    n0.next = n1;\n}\n
        linked_list.cs
        /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid Remove(ListNode n0) {\n    if (n0.next == null)\n        return;\n    // n0 -> P -> n1\n    ListNode P = n0.next;\n    ListNode? n1 = P.next;\n    n0.next = n1;\n}\n
        linked_list.go
        /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunc removeItem(n0 *ListNode) {\n    if n0.Next == nil {\n        return\n    }\n    // n0 -> P -> n1\n    P := n0.Next\n    n1 := P.Next\n    n0.Next = n1\n}\n
        linked_list.swift
        /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunc remove(n0: ListNode) {\n    if n0.next == nil {\n        return\n    }\n    // n0 -> P -> n1\n    let P = n0.next\n    let n1 = P?.next\n    n0.next = n1\n}\n
        linked_list.js
        /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunction remove(n0) {\n    if (!n0.next) return;\n    // n0 -> P -> n1\n    const P = n0.next;\n    const n1 = P.next;\n    n0.next = n1;\n}\n
        linked_list.ts
        /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunction remove(n0: ListNode): void {\n    if (!n0.next) {\n        return;\n    }\n    // n0 -> P -> n1\n    const P = n0.next;\n    const n1 = P.next;\n    n0.next = n1;\n}\n
        linked_list.dart
        /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode n0) {\n  if (n0.next == null) return;\n  // n0 -> P -> n1\n  ListNode P = n0.next!;\n  ListNode? n1 = P.next;\n  n0.next = n1;\n}\n
        linked_list.rs
        /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\n#[allow(non_snake_case)]\npub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {\n    // n0 -> P -> n1\n    let P = n0.borrow_mut().next.take();\n    if let Some(node) = P {\n        let n1 = node.borrow_mut().next.take();\n        n0.borrow_mut().next = n1;\n    }\n}\n
        linked_list.c
        /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nvoid removeItem(ListNode *n0) {\n    if (!n0->next)\n        return;\n    // n0 -> P -> n1\n    ListNode *P = n0->next;\n    ListNode *n1 = P->next;\n    n0->next = n1;\n    // \u91ca\u653e\u5185\u5b58\n    free(P);\n}\n
        linked_list.kt
        /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfun remove(n0: ListNode?) {\n    if (n0?.next == null)\n        return\n    // n0 -> P -> n1\n    val p = n0.next\n    val n1 = p?.next\n    n0.next = n1\n}\n
        linked_list.rb
        ### \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 ###\ndef remove(n0)\n  return if n0.next.nil?\n\n  # n0 -> remove_node -> n1\n  remove_node = n0.next\n  n1 = remove_node.next\n  n0.next = n1\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/linked_list/#4","title":"4. \u00a0 \u8bbf\u95ee\u8282\u70b9","text":"

        \u5728\u94fe\u8868\u4e2d\u8bbf\u95ee\u8282\u70b9\u7684\u6548\u7387\u8f83\u4f4e\u3002\u5982\u4e0a\u4e00\u8282\u6240\u8ff0\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u4e0b\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u5143\u7d20\u3002\u94fe\u8868\u5219\u4e0d\u7136\uff0c\u7a0b\u5e8f\u9700\u8981\u4ece\u5934\u8282\u70b9\u51fa\u53d1\uff0c\u9010\u4e2a\u5411\u540e\u904d\u5386\uff0c\u76f4\u81f3\u627e\u5230\u76ee\u6807\u8282\u70b9\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8bbf\u95ee\u94fe\u8868\u7684\u7b2c \\(i\\) \u4e2a\u8282\u70b9\u9700\u8981\u5faa\u73af \\(i - 1\\) \u8f6e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
        def access(head: ListNode, index: int) -> ListNode | None:\n    \"\"\"\u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9\"\"\"\n    for _ in range(index):\n        if not head:\n            return None\n        head = head.next\n    return head\n
        linked_list.cpp
        /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode *access(ListNode *head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == nullptr)\n            return nullptr;\n        head = head->next;\n    }\n    return head;\n}\n
        linked_list.java
        /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode access(ListNode head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == null)\n            return null;\n        head = head.next;\n    }\n    return head;\n}\n
        linked_list.cs
        /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode? Access(ListNode? head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == null)\n            return null;\n        head = head.next;\n    }\n    return head;\n}\n
        linked_list.go
        /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunc access(head *ListNode, index int) *ListNode {\n    for i := 0; i < index; i++ {\n        if head == nil {\n            return nil\n        }\n        head = head.Next\n    }\n    return head\n}\n
        linked_list.swift
        /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunc access(head: ListNode, index: Int) -> ListNode? {\n    var head: ListNode? = head\n    for _ in 0 ..< index {\n        if head == nil {\n            return nil\n        }\n        head = head?.next\n    }\n    return head\n}\n
        linked_list.js
        /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunction access(head, index) {\n    for (let i = 0; i < index; i++) {\n        if (!head) {\n            return null;\n        }\n        head = head.next;\n    }\n    return head;\n}\n
        linked_list.ts
        /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunction access(head: ListNode | null, index: number): ListNode | null {\n    for (let i = 0; i < index; i++) {\n        if (!head) {\n            return null;\n        }\n        head = head.next;\n    }\n    return head;\n}\n
        linked_list.dart
        /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode? access(ListNode? head, int index) {\n  for (var i = 0; i < index; i++) {\n    if (head == null) return null;\n    head = head.next;\n  }\n  return head;\n}\n
        linked_list.rs
        /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\npub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Option<Rc<RefCell<ListNode<T>>>> {\n    fn dfs<T>(\n        head: Option<&Rc<RefCell<ListNode<T>>>>,\n        index: i32,\n    ) -> Option<Rc<RefCell<ListNode<T>>>> {\n        if index <= 0 {\n            return head.cloned();\n        }\n\n        if let Some(node) = head {\n            dfs(node.borrow().next.as_ref(), index - 1)\n        } else {\n            None\n        }\n    }\n\n    dfs(Some(head).as_ref(), index)\n}\n
        linked_list.c
        /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode *access(ListNode *head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == NULL)\n            return NULL;\n        head = head->next;\n    }\n    return head;\n}\n
        linked_list.kt
        /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfun access(head: ListNode?, index: Int): ListNode? {\n    var h = head\n    for (i in 0..<index) {\n        if (h == null)\n            return null\n        h = h.next\n    }\n    return h\n}\n
        linked_list.rb
        ### \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 ###\ndef access(head, index)\n  for i in 0...index\n    return nil if head.nil?\n    head = head.next\n  end\n\n  head\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/linked_list/#5","title":"5. \u00a0 \u67e5\u627e\u8282\u70b9","text":"

        \u904d\u5386\u94fe\u8868\uff0c\u67e5\u627e\u5176\u4e2d\u503c\u4e3a target \u7684\u8282\u70b9\uff0c\u8f93\u51fa\u8be5\u8282\u70b9\u5728\u94fe\u8868\u4e2d\u7684\u7d22\u5f15\u3002\u6b64\u8fc7\u7a0b\u4e5f\u5c5e\u4e8e\u7ebf\u6027\u67e5\u627e\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
        def find(head: ListNode, target: int) -> int:\n    \"\"\"\u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9\"\"\"\n    index = 0\n    while head:\n        if head.val == target:\n            return index\n        head = head.next\n        index += 1\n    return -1\n
        linked_list.cpp
        /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode *head, int target) {\n    int index = 0;\n    while (head != nullptr) {\n        if (head->val == target)\n            return index;\n        head = head->next;\n        index++;\n    }\n    return -1;\n}\n
        linked_list.java
        /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode head, int target) {\n    int index = 0;\n    while (head != null) {\n        if (head.val == target)\n            return index;\n        head = head.next;\n        index++;\n    }\n    return -1;\n}\n
        linked_list.cs
        /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint Find(ListNode? head, int target) {\n    int index = 0;\n    while (head != null) {\n        if (head.val == target)\n            return index;\n        head = head.next;\n        index++;\n    }\n    return -1;\n}\n
        linked_list.go
        /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunc findNode(head *ListNode, target int) int {\n    index := 0\n    for head != nil {\n        if head.Val == target {\n            return index\n        }\n        head = head.Next\n        index++\n    }\n    return -1\n}\n
        linked_list.swift
        /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunc find(head: ListNode, target: Int) -> Int {\n    var head: ListNode? = head\n    var index = 0\n    while head != nil {\n        if head?.val == target {\n            return index\n        }\n        head = head?.next\n        index += 1\n    }\n    return -1\n}\n
        linked_list.js
        /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunction find(head, target) {\n    let index = 0;\n    while (head !== null) {\n        if (head.val === target) {\n            return index;\n        }\n        head = head.next;\n        index += 1;\n    }\n    return -1;\n}\n
        linked_list.ts
        /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunction find(head: ListNode | null, target: number): number {\n    let index = 0;\n    while (head !== null) {\n        if (head.val === target) {\n            return index;\n        }\n        head = head.next;\n        index += 1;\n    }\n    return -1;\n}\n
        linked_list.dart
        /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode? head, int target) {\n  int index = 0;\n  while (head != null) {\n    if (head.val == target) {\n      return index;\n    }\n    head = head.next;\n    index++;\n  }\n  return -1;\n}\n
        linked_list.rs
        /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\npub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T) -> i32 {\n    fn find<T: PartialEq>(head: Option<&Rc<RefCell<ListNode<T>>>>, target: T, idx: i32) -> i32 {\n        if let Some(node) = head {\n            if node.borrow().val == target {\n                return idx;\n            }\n            return find(node.borrow().next.as_ref(), target, idx + 1);\n        } else {\n            -1\n        }\n    }\n\n    find(Some(head).as_ref(), target, 0)\n}\n
        linked_list.c
        /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode *head, int target) {\n    int index = 0;\n    while (head) {\n        if (head->val == target)\n            return index;\n        head = head->next;\n        index++;\n    }\n    return -1;\n}\n
        linked_list.kt
        /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfun find(head: ListNode?, target: Int): Int {\n    var index = 0\n    var h = head\n    while (h != null) {\n        if (h._val == target)\n            return index\n        h = h.next\n        index++\n    }\n    return -1\n}\n
        linked_list.rb
        ### \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 ###\ndef find(head, target)\n  index = 0\n  while head\n    return index if head.val == target\n    head = head.next\n    index += 1\n  end\n\n  -1\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/linked_list/#422-vs","title":"4.2.2 \u00a0 \u6570\u7ec4 vs. \u94fe\u8868","text":"

        \u8868 4-1 \u603b\u7ed3\u4e86\u6570\u7ec4\u548c\u94fe\u8868\u7684\u5404\u9879\u7279\u70b9\u5e76\u5bf9\u6bd4\u4e86\u64cd\u4f5c\u6548\u7387\u3002\u7531\u4e8e\u5b83\u4eec\u91c7\u7528\u4e24\u79cd\u76f8\u53cd\u7684\u5b58\u50a8\u7b56\u7565\uff0c\u56e0\u6b64\u5404\u79cd\u6027\u8d28\u548c\u64cd\u4f5c\u6548\u7387\u4e5f\u5448\u73b0\u5bf9\u7acb\u7684\u7279\u70b9\u3002

        \u8868 4-1 \u00a0 \u6570\u7ec4\u4e0e\u94fe\u8868\u7684\u6548\u7387\u5bf9\u6bd4

        \u6570\u7ec4 \u94fe\u8868 \u5b58\u50a8\u65b9\u5f0f \u8fde\u7eed\u5185\u5b58\u7a7a\u95f4 \u5206\u6563\u5185\u5b58\u7a7a\u95f4 \u5bb9\u91cf\u6269\u5c55 \u957f\u5ea6\u4e0d\u53ef\u53d8 \u53ef\u7075\u6d3b\u6269\u5c55 \u5185\u5b58\u6548\u7387 \u5143\u7d20\u5360\u7528\u5185\u5b58\u5c11\u3001\u4f46\u53ef\u80fd\u6d6a\u8d39\u7a7a\u95f4 \u5143\u7d20\u5360\u7528\u5185\u5b58\u591a \u8bbf\u95ee\u5143\u7d20 \\(O(1)\\) \\(O(n)\\) \u6dfb\u52a0\u5143\u7d20 \\(O(n)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(1)\\)"},{"location":"chapter_array_and_linkedlist/linked_list/#423","title":"4.2.3 \u00a0 \u5e38\u89c1\u94fe\u8868\u7c7b\u578b","text":"

        \u5982\u56fe 4-8 \u6240\u793a\uff0c\u5e38\u89c1\u7684\u94fe\u8868\u7c7b\u578b\u5305\u62ec\u4e09\u79cd\u3002

        • \u5355\u5411\u94fe\u8868\uff1a\u5373\u524d\u9762\u4ecb\u7ecd\u7684\u666e\u901a\u94fe\u8868\u3002\u5355\u5411\u94fe\u8868\u7684\u8282\u70b9\u5305\u542b\u503c\u548c\u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\u4e24\u9879\u6570\u636e\u3002\u6211\u4eec\u5c06\u9996\u4e2a\u8282\u70b9\u79f0\u4e3a\u5934\u8282\u70b9\uff0c\u5c06\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u79f0\u4e3a\u5c3e\u8282\u70b9\uff0c\u5c3e\u8282\u70b9\u6307\u5411\u7a7a None \u3002
        • \u73af\u5f62\u94fe\u8868\uff1a\u5982\u679c\u6211\u4eec\u4ee4\u5355\u5411\u94fe\u8868\u7684\u5c3e\u8282\u70b9\u6307\u5411\u5934\u8282\u70b9\uff08\u9996\u5c3e\u76f8\u63a5\uff09\uff0c\u5219\u5f97\u5230\u4e00\u4e2a\u73af\u5f62\u94fe\u8868\u3002\u5728\u73af\u5f62\u94fe\u8868\u4e2d\uff0c\u4efb\u610f\u8282\u70b9\u90fd\u53ef\u4ee5\u89c6\u4f5c\u5934\u8282\u70b9\u3002
        • \u53cc\u5411\u94fe\u8868\uff1a\u4e0e\u5355\u5411\u94fe\u8868\u76f8\u6bd4\uff0c\u53cc\u5411\u94fe\u8868\u8bb0\u5f55\u4e86\u4e24\u4e2a\u65b9\u5411\u7684\u5f15\u7528\u3002\u53cc\u5411\u94fe\u8868\u7684\u8282\u70b9\u5b9a\u4e49\u540c\u65f6\u5305\u542b\u6307\u5411\u540e\u7ee7\u8282\u70b9\uff08\u4e0b\u4e00\u4e2a\u8282\u70b9\uff09\u548c\u524d\u9a71\u8282\u70b9\uff08\u4e0a\u4e00\u4e2a\u8282\u70b9\uff09\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\u3002\u76f8\u8f83\u4e8e\u5355\u5411\u94fe\u8868\uff0c\u53cc\u5411\u94fe\u8868\u66f4\u5177\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u671d\u4e24\u4e2a\u65b9\u5411\u904d\u5386\u94fe\u8868\uff0c\u4f46\u76f8\u5e94\u5730\u4e5f\u9700\u8981\u5360\u7528\u66f4\u591a\u7684\u5185\u5b58\u7a7a\u95f4\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        class ListNode:\n    \"\"\"\u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val                # \u8282\u70b9\u503c\n        self.next: ListNode | None = None  # \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n        self.prev: ListNode | None = None  # \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\n    int val;         // \u8282\u70b9\u503c\n    ListNode *next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\n    ListNode *prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n    ListNode(int x) : val(x), next(nullptr), prev(nullptr) {}  // \u6784\u9020\u51fd\u6570\n};\n
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n    int val;        // \u8282\u70b9\u503c\n    ListNode next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n    ListNode prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n    ListNode(int x) { val = x; }  // \u6784\u9020\u51fd\u6570\n}\n
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode(int x) {  // \u6784\u9020\u51fd\u6570\n    int val = x;    // \u8282\u70b9\u503c\n    ListNode next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n    ListNode prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n}\n
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype DoublyListNode struct {\n    Val  int             // \u8282\u70b9\u503c\n    Next *DoublyListNode // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\n    Prev *DoublyListNode // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n}\n\n// NewDoublyListNode \u521d\u59cb\u5316\nfunc NewDoublyListNode(val int) *DoublyListNode {\n    return &DoublyListNode{\n        Val:  val,\n        Next: nil,\n        Prev: nil,\n    }\n}\n
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n    var val: Int // \u8282\u70b9\u503c\n    var next: ListNode? // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n    var prev: ListNode? // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n\n    init(x: Int) { // \u6784\u9020\u51fd\u6570\n        val = x\n    }\n}\n
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n    constructor(val, next, prev) {\n        this.val = val  ===  undefined ? 0 : val;        // \u8282\u70b9\u503c\n        this.next = next  ===  undefined ? null : next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n        this.prev = prev  ===  undefined ? null : prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n    }\n}\n
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n    val: number;\n    next: ListNode | null;\n    prev: ListNode | null;\n    constructor(val?: number, next?: ListNode | null, prev?: ListNode | null) {\n        this.val = val  ===  undefined ? 0 : val;        // \u8282\u70b9\u503c\n        this.next = next  ===  undefined ? null : next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n        this.prev = prev  ===  undefined ? null : prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n    }\n}\n
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n    int val;        // \u8282\u70b9\u503c\n    ListNode? next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n    ListNode? prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n    ListNode(this.val, [this.next, this.prev]);  // \u6784\u9020\u51fd\u6570\n}\n
        use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\u578b */\n#[derive(Debug)]\nstruct ListNode {\n    val: i32, // \u8282\u70b9\u503c\n    next: Option<Rc<RefCell<ListNode>>>, // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\n    prev: Option<Rc<RefCell<ListNode>>>, // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n}\n\n/* \u6784\u9020\u51fd\u6570 */\nimpl ListNode {\n    fn new(val: i32) -> Self {\n        ListNode {\n            val,\n            next: None,\n            prev: None,\n        }\n    }\n}\n
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\ntypedef struct ListNode {\n    int val;               // \u8282\u70b9\u503c\n    struct ListNode *next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\n    struct ListNode *prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n} ListNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nListNode *newListNode(int val) {\n    ListNode *node;\n    node = (ListNode *) malloc(sizeof(ListNode));\n    node->val = val;\n    node->next = NULL;\n    node->prev = NULL;\n    return node;\n}\n
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\n// \u6784\u9020\u65b9\u6cd5\nclass ListNode(x: Int) {\n    val _val: Int = x           // \u8282\u70b9\u503c\n    val next: ListNode? = null  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n    val prev: ListNode? = null  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n}\n
        # \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\nclass ListNode\n  attr_accessor :val    # \u8282\u70b9\u503c\n  attr_accessor :next   # \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n  attr_accessor :prev   # \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n\n  def initialize(val=0, next_node=nil, prev_node=nil)\n    @val = val\n    @next = next_node\n    @prev = prev_node\n  end\nend\n

        \u56fe 4-8 \u00a0 \u5e38\u89c1\u94fe\u8868\u79cd\u7c7b

        "},{"location":"chapter_array_and_linkedlist/linked_list/#424","title":"4.2.4 \u00a0 \u94fe\u8868\u5178\u578b\u5e94\u7528","text":"

        \u5355\u5411\u94fe\u8868\u901a\u5e38\u7528\u4e8e\u5b9e\u73b0\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u548c\u56fe\u7b49\u6570\u636e\u7ed3\u6784\u3002

        • \u6808\u4e0e\u961f\u5217\uff1a\u5f53\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u90fd\u5728\u94fe\u8868\u7684\u4e00\u7aef\u8fdb\u884c\u65f6\uff0c\u5b83\u8868\u73b0\u7684\u7279\u6027\u4e3a\u5148\u8fdb\u540e\u51fa\uff0c\u5bf9\u5e94\u6808\uff1b\u5f53\u63d2\u5165\u64cd\u4f5c\u5728\u94fe\u8868\u7684\u4e00\u7aef\u8fdb\u884c\uff0c\u5220\u9664\u64cd\u4f5c\u5728\u94fe\u8868\u7684\u53e6\u4e00\u7aef\u8fdb\u884c\uff0c\u5b83\u8868\u73b0\u7684\u7279\u6027\u4e3a\u5148\u8fdb\u5148\u51fa\uff0c\u5bf9\u5e94\u961f\u5217\u3002
        • \u54c8\u5e0c\u8868\uff1a\u94fe\u5f0f\u5730\u5740\u662f\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u7684\u4e3b\u6d41\u65b9\u6848\u4e4b\u4e00\uff0c\u5728\u8be5\u65b9\u6848\u4e2d\uff0c\u6240\u6709\u51b2\u7a81\u7684\u5143\u7d20\u90fd\u4f1a\u88ab\u653e\u5230\u4e00\u4e2a\u94fe\u8868\u4e2d\u3002
        • \u56fe\uff1a\u90bb\u63a5\u8868\u662f\u8868\u793a\u56fe\u7684\u4e00\u79cd\u5e38\u7528\u65b9\u5f0f\uff0c\u5176\u4e2d\u56fe\u7684\u6bcf\u4e2a\u9876\u70b9\u90fd\u4e0e\u4e00\u4e2a\u94fe\u8868\u76f8\u5173\u8054\uff0c\u94fe\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u4ee3\u8868\u4e0e\u8be5\u9876\u70b9\u76f8\u8fde\u7684\u5176\u4ed6\u9876\u70b9\u3002

        \u53cc\u5411\u94fe\u8868\u5e38\u7528\u4e8e\u9700\u8981\u5feb\u901f\u67e5\u627e\u524d\u4e00\u4e2a\u548c\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u573a\u666f\u3002

        • \u9ad8\u7ea7\u6570\u636e\u7ed3\u6784\uff1a\u6bd4\u5982\u5728\u7ea2\u9ed1\u6811\u3001B \u6811\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u8bbf\u95ee\u8282\u70b9\u7684\u7236\u8282\u70b9\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728\u8282\u70b9\u4e2d\u4fdd\u5b58\u4e00\u4e2a\u6307\u5411\u7236\u8282\u70b9\u7684\u5f15\u7528\u6765\u5b9e\u73b0\uff0c\u7c7b\u4f3c\u4e8e\u53cc\u5411\u94fe\u8868\u3002
        • \u6d4f\u89c8\u5668\u5386\u53f2\uff1a\u5728\u7f51\u9875\u6d4f\u89c8\u5668\u4e2d\uff0c\u5f53\u7528\u6237\u70b9\u51fb\u524d\u8fdb\u6216\u540e\u9000\u6309\u94ae\u65f6\uff0c\u6d4f\u89c8\u5668\u9700\u8981\u77e5\u9053\u7528\u6237\u8bbf\u95ee\u8fc7\u7684\u524d\u4e00\u4e2a\u548c\u540e\u4e00\u4e2a\u7f51\u9875\u3002\u53cc\u5411\u94fe\u8868\u7684\u7279\u6027\u4f7f\u5f97\u8fd9\u79cd\u64cd\u4f5c\u53d8\u5f97\u7b80\u5355\u3002
        • LRU \u7b97\u6cd5\uff1a\u5728\u7f13\u5b58\u6dd8\u6c70\uff08LRU\uff09\u7b97\u6cd5\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5feb\u901f\u627e\u5230\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\u6570\u636e\uff0c\u4ee5\u53ca\u652f\u6301\u5feb\u901f\u6dfb\u52a0\u548c\u5220\u9664\u8282\u70b9\u3002\u8fd9\u65f6\u5019\u4f7f\u7528\u53cc\u5411\u94fe\u8868\u5c31\u975e\u5e38\u5408\u9002\u3002

        \u73af\u5f62\u94fe\u8868\u5e38\u7528\u4e8e\u9700\u8981\u5468\u671f\u6027\u64cd\u4f5c\u7684\u573a\u666f\uff0c\u6bd4\u5982\u64cd\u4f5c\u7cfb\u7edf\u7684\u8d44\u6e90\u8c03\u5ea6\u3002

        • \u65f6\u95f4\u7247\u8f6e\u8f6c\u8c03\u5ea6\u7b97\u6cd5\uff1a\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u65f6\u95f4\u7247\u8f6e\u8f6c\u8c03\u5ea6\u7b97\u6cd5\u662f\u4e00\u79cd\u5e38\u89c1\u7684 CPU \u8c03\u5ea6\u7b97\u6cd5\uff0c\u5b83\u9700\u8981\u5bf9\u4e00\u7ec4\u8fdb\u7a0b\u8fdb\u884c\u5faa\u73af\u3002\u6bcf\u4e2a\u8fdb\u7a0b\u88ab\u8d4b\u4e88\u4e00\u4e2a\u65f6\u95f4\u7247\uff0c\u5f53\u65f6\u95f4\u7247\u7528\u5b8c\u65f6\uff0cCPU \u5c06\u5207\u6362\u5230\u4e0b\u4e00\u4e2a\u8fdb\u7a0b\u3002\u8fd9\u79cd\u5faa\u73af\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7\u73af\u5f62\u94fe\u8868\u6765\u5b9e\u73b0\u3002
        • \u6570\u636e\u7f13\u51b2\u533a\uff1a\u5728\u67d0\u4e9b\u6570\u636e\u7f13\u51b2\u533a\u7684\u5b9e\u73b0\u4e2d\uff0c\u4e5f\u53ef\u80fd\u4f1a\u4f7f\u7528\u73af\u5f62\u94fe\u8868\u3002\u6bd4\u5982\u5728\u97f3\u9891\u3001\u89c6\u9891\u64ad\u653e\u5668\u4e2d\uff0c\u6570\u636e\u6d41\u53ef\u80fd\u4f1a\u88ab\u5206\u6210\u591a\u4e2a\u7f13\u51b2\u5757\u5e76\u653e\u5165\u4e00\u4e2a\u73af\u5f62\u94fe\u8868\uff0c\u4ee5\u4fbf\u5b9e\u73b0\u65e0\u7f1d\u64ad\u653e\u3002
        "},{"location":"chapter_array_and_linkedlist/list/","title":"4.3 \u00a0 \u5217\u8868","text":"

        \u5217\u8868\uff08list\uff09\u662f\u4e00\u4e2a\u62bd\u8c61\u7684\u6570\u636e\u7ed3\u6784\u6982\u5ff5\uff0c\u5b83\u8868\u793a\u5143\u7d20\u7684\u6709\u5e8f\u96c6\u5408\uff0c\u652f\u6301\u5143\u7d20\u8bbf\u95ee\u3001\u4fee\u6539\u3001\u6dfb\u52a0\u3001\u5220\u9664\u548c\u904d\u5386\u7b49\u64cd\u4f5c\uff0c\u65e0\u987b\u4f7f\u7528\u8005\u8003\u8651\u5bb9\u91cf\u9650\u5236\u7684\u95ee\u9898\u3002\u5217\u8868\u53ef\u4ee5\u57fa\u4e8e\u94fe\u8868\u6216\u6570\u7ec4\u5b9e\u73b0\u3002

        • \u94fe\u8868\u5929\u7136\u53ef\u4ee5\u770b\u4f5c\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u652f\u6301\u5143\u7d20\u589e\u5220\u67e5\u6539\u64cd\u4f5c\uff0c\u5e76\u4e14\u53ef\u4ee5\u7075\u6d3b\u52a8\u6001\u6269\u5bb9\u3002
        • \u6570\u7ec4\u4e5f\u652f\u6301\u5143\u7d20\u589e\u5220\u67e5\u6539\uff0c\u4f46\u7531\u4e8e\u5176\u957f\u5ea6\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u53ea\u80fd\u770b\u4f5c\u4e00\u4e2a\u5177\u6709\u957f\u5ea6\u9650\u5236\u7684\u5217\u8868\u3002

        \u5f53\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u5217\u8868\u65f6\uff0c\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6027\u8d28\u4f1a\u5bfc\u81f4\u5217\u8868\u7684\u5b9e\u7528\u6027\u964d\u4f4e\u3002\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u901a\u5e38\u65e0\u6cd5\u4e8b\u5148\u786e\u5b9a\u9700\u8981\u5b58\u50a8\u591a\u5c11\u6570\u636e\uff0c\u4ece\u800c\u96be\u4ee5\u9009\u62e9\u5408\u9002\u7684\u5217\u8868\u957f\u5ea6\u3002\u82e5\u957f\u5ea6\u8fc7\u5c0f\uff0c\u5219\u5f88\u53ef\u80fd\u65e0\u6cd5\u6ee1\u8db3\u4f7f\u7528\u9700\u6c42\uff1b\u82e5\u957f\u5ea6\u8fc7\u5927\uff0c\u5219\u4f1a\u9020\u6210\u5185\u5b58\u7a7a\u95f4\u6d6a\u8d39\u3002

        \u4e3a\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\uff08dynamic array\uff09\u6765\u5b9e\u73b0\u5217\u8868\u3002\u5b83\u7ee7\u627f\u4e86\u6570\u7ec4\u7684\u5404\u9879\u4f18\u70b9\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u52a8\u6001\u6269\u5bb9\u3002

        \u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u5217\u8868\u662f\u57fa\u4e8e\u52a8\u6001\u6570\u7ec4\u5b9e\u73b0\u7684\uff0c\u4f8b\u5982 Python \u4e2d\u7684 list \u3001Java \u4e2d\u7684 ArrayList \u3001C++ \u4e2d\u7684 vector \u548c C# \u4e2d\u7684 List \u7b49\u3002\u5728\u63a5\u4e0b\u6765\u7684\u8ba8\u8bba\u4e2d\uff0c\u6211\u4eec\u5c06\u628a\u201c\u5217\u8868\u201d\u548c\u201c\u52a8\u6001\u6570\u7ec4\u201d\u89c6\u4e3a\u7b49\u540c\u7684\u6982\u5ff5\u3002

        "},{"location":"chapter_array_and_linkedlist/list/#431","title":"4.3.1 \u00a0 \u5217\u8868\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/list/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u5217\u8868","text":"

        \u6211\u4eec\u901a\u5e38\u4f7f\u7528\u201c\u65e0\u521d\u59cb\u503c\u201d\u548c\u201c\u6709\u521d\u59cb\u503c\u201d\u8fd9\u4e24\u79cd\u521d\u59cb\u5316\u65b9\u6cd5\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
        # \u521d\u59cb\u5316\u5217\u8868\n# \u65e0\u521d\u59cb\u503c\nnums1: list[int] = []\n# \u6709\u521d\u59cb\u503c\nnums: list[int] = [1, 3, 2, 5, 4]\n
        list.cpp
        /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u9700\u6ce8\u610f\uff0cC++ \u4e2d vector \u5373\u662f\u672c\u6587\u63cf\u8ff0\u7684 nums\n// \u65e0\u521d\u59cb\u503c\nvector<int> nums1;\n// \u6709\u521d\u59cb\u503c\nvector<int> nums = { 1, 3, 2, 5, 4 };\n
        list.java
        /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<Integer> nums1 = new ArrayList<>();\n// \u6709\u521d\u59cb\u503c\uff08\u6ce8\u610f\u6570\u7ec4\u7684\u5143\u7d20\u7c7b\u578b\u9700\u4e3a int[] \u7684\u5305\u88c5\u7c7b Integer[]\uff09\nInteger[] numbers = new Integer[] { 1, 3, 2, 5, 4 };\nList<Integer> nums = new ArrayList<>(Arrays.asList(numbers));\n
        list.cs
        /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<int> nums1 = [];\n// \u6709\u521d\u59cb\u503c\nint[] numbers = [1, 3, 2, 5, 4];\nList<int> nums = [.. numbers];\n
        list_test.go
        /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nnums1 := []int{}\n// \u6709\u521d\u59cb\u503c\nnums := []int{1, 3, 2, 5, 4}\n
        list.swift
        /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nlet nums1: [Int] = []\n// \u6709\u521d\u59cb\u503c\nvar nums = [1, 3, 2, 5, 4]\n
        list.js
        /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nconst nums1 = [];\n// \u6709\u521d\u59cb\u503c\nconst nums = [1, 3, 2, 5, 4];\n
        list.ts
        /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nconst nums1: number[] = [];\n// \u6709\u521d\u59cb\u503c\nconst nums: number[] = [1, 3, 2, 5, 4];\n
        list.dart
        /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<int> nums1 = [];\n// \u6709\u521d\u59cb\u503c\nList<int> nums = [1, 3, 2, 5, 4];\n
        list.rs
        /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nlet nums1: Vec<i32> = Vec::new();\n// \u6709\u521d\u59cb\u503c\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
        list.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
        list.kt
        /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nvar nums1 = listOf<Int>()\n// \u6709\u521d\u59cb\u503c\nvar numbers = arrayOf(1, 3, 2, 5, 4)\nvar nums = numbers.toMutableList()\n
        list.rb
        # \u521d\u59cb\u5316\u5217\u8868\n# \u65e0\u521d\u59cb\u503c\nnums1 = []\n# \u6709\u521d\u59cb\u503c\nnums = [1, 3, 2, 5, 4]\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/list/#2","title":"2. \u00a0 \u8bbf\u95ee\u5143\u7d20","text":"

        \u5217\u8868\u672c\u8d28\u4e0a\u662f\u6570\u7ec4\uff0c\u56e0\u6b64\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u8bbf\u95ee\u548c\u66f4\u65b0\u5143\u7d20\uff0c\u6548\u7387\u5f88\u9ad8\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
        # \u8bbf\u95ee\u5143\u7d20\nnum: int = nums[1]  # \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n# \u66f4\u65b0\u5143\u7d20\nnums[1] = 0    # \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        list.cpp
        /* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        list.java
        /* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums.get(1);  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums.set(1, 0);  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        list.cs
        /* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        list_test.go
        /* \u8bbf\u95ee\u5143\u7d20 */\nnum := nums[1]  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0     // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        list.swift
        /* \u8bbf\u95ee\u5143\u7d20 */\nlet num = nums[1] // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0 // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        list.js
        /* \u8bbf\u95ee\u5143\u7d20 */\nconst num = nums[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        list.ts
        /* \u8bbf\u95ee\u5143\u7d20 */\nconst num: number = nums[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        list.dart
        /* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        list.rs
        /* \u8bbf\u95ee\u5143\u7d20 */\nlet num: i32 = nums[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;             // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        list.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
        list.kt
        /* \u8bbf\u95ee\u5143\u7d20 */\nval num = nums[1]       // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0             // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        list.rb
        # \u8bbf\u95ee\u5143\u7d20\nnum = nums[1] # \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n# \u66f4\u65b0\u5143\u7d20\nnums[1] = 0 # \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/list/#3","title":"3. \u00a0 \u63d2\u5165\u4e0e\u5220\u9664\u5143\u7d20","text":"

        \u76f8\u8f83\u4e8e\u6570\u7ec4\uff0c\u5217\u8868\u53ef\u4ee5\u81ea\u7531\u5730\u6dfb\u52a0\u4e0e\u5220\u9664\u5143\u7d20\u3002\u5728\u5217\u8868\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff0c\u4f46\u63d2\u5165\u548c\u5220\u9664\u5143\u7d20\u7684\u6548\u7387\u4ecd\u4e0e\u6570\u7ec4\u76f8\u540c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
        # \u6e05\u7a7a\u5217\u8868\nnums.clear()\n\n# \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n# \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\nnums.insert(3, 6)  # \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n# \u5220\u9664\u5143\u7d20\nnums.pop(3)        # \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        list.cpp
        /* \u6e05\u7a7a\u5217\u8868 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.push_back(1);\nnums.push_back(3);\nnums.push_back(2);\nnums.push_back(5);\nnums.push_back(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.insert(nums.begin() + 3, 6);  // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.erase(nums.begin() + 3);      // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        list.java
        /* \u6e05\u7a7a\u5217\u8868 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.add(3, 6);  // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.remove(3);  // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        list.cs
        /* \u6e05\u7a7a\u5217\u8868 */\nnums.Clear();\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.Add(1);\nnums.Add(3);\nnums.Add(2);\nnums.Add(5);\nnums.Add(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.Insert(3, 6);  // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.RemoveAt(3);  // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        list_test.go
        /* \u6e05\u7a7a\u5217\u8868 */\nnums = nil\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums = append(nums, 1)\nnums = append(nums, 3)\nnums = append(nums, 2)\nnums = append(nums, 5)\nnums = append(nums, 4)\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums = append(nums[:3], append([]int{6}, nums[3:]...)...) // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums = append(nums[:3], nums[4:]...) // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        list.swift
        /* \u6e05\u7a7a\u5217\u8868 */\nnums.removeAll()\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.insert(6, at: 3) // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.remove(at: 3) // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        list.js
        /* \u6e05\u7a7a\u5217\u8868 */\nnums.length = 0;\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.splice(3, 0, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.splice(3, 1);  // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        list.ts
        /* \u6e05\u7a7a\u5217\u8868 */\nnums.length = 0;\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.splice(3, 0, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.splice(3, 1);  // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        list.dart
        /* \u6e05\u7a7a\u5217\u8868 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.insert(3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.removeAt(3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        list.rs
        /* \u6e05\u7a7a\u5217\u8868 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.insert(3, 6);  // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.remove(3);    // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        list.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
        list.kt
        /* \u6e05\u7a7a\u5217\u8868 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.add(3, 6);  // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.remove(3);  // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        list.rb
        # \u6e05\u7a7a\u5217\u8868\nnums.clear\n\n# \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\nnums << 1\nnums << 3\nnums << 2\nnums << 5\nnums << 4\n\n# \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\nnums.insert(3, 6) # \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n# \u5220\u9664\u5143\u7d20\nnums.delete_at(3) # \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/list/#4","title":"4. \u00a0 \u904d\u5386\u5217\u8868","text":"

        \u4e0e\u6570\u7ec4\u4e00\u6837\uff0c\u5217\u8868\u53ef\u4ee5\u6839\u636e\u7d22\u5f15\u904d\u5386\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u904d\u5386\u5404\u5143\u7d20\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
        # \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\ncount = 0\nfor i in range(len(nums)):\n    count += nums[i]\n\n# \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20\nfor num in nums:\n    count += num\n
        list.cpp
        /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\n    count += nums[i];\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (int num : nums) {\n    count += num;\n}\n
        list.java
        /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\n    count += nums.get(i);\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\nfor (int num : nums) {\n    count += num;\n}\n
        list.cs
        /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < nums.Count; i++) {\n    count += nums[i];\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nforeach (int num in nums) {\n    count += num;\n}\n
        list_test.go
        /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\ncount := 0\nfor i := 0; i < len(nums); i++ {\n    count += nums[i]\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0\nfor _, num := range nums {\n    count += num\n}\n
        list.swift
        /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nvar count = 0\nfor i in nums.indices {\n    count += nums[i]\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0\nfor num in nums {\n    count += num\n}\n
        list.js
        /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\n    count += nums[i];\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (const num of nums) {\n    count += num;\n}\n
        list.ts
        /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\n    count += nums[i];\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (const num of nums) {\n    count += num;\n}\n
        list.dart
        /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (var i = 0; i < nums.length; i++) {\n    count += nums[i];\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (var num in nums) {\n    count += num;\n}\n
        list.rs
        // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\nlet mut _count = 0;\nfor i in 0..nums.len() {\n    _count += nums[i];\n}\n\n// \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20\n_count = 0;\nfor num in &nums {\n    _count += num;\n}\n
        list.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
        list.kt
        /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nvar count = 0\nfor (i in nums.indices) {\n    count += nums[i]\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\nfor (num in nums) {\n    count += num\n}\n
        list.rb
        # \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\ncount = 0\nfor i in 0...nums.length\n    count += nums[i]\nend\n\n# \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20\ncount = 0\nfor num in nums\n    count += num\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/list/#5","title":"5. \u00a0 \u62fc\u63a5\u5217\u8868","text":"

        \u7ed9\u5b9a\u4e00\u4e2a\u65b0\u5217\u8868 nums1 \uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u62fc\u63a5\u5230\u539f\u5217\u8868\u7684\u5c3e\u90e8\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
        # \u62fc\u63a5\u4e24\u4e2a\u5217\u8868\nnums1: list[int] = [6, 8, 7, 10, 9]\nnums += nums1  # \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
        list.cpp
        /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nvector<int> nums1 = { 6, 8, 7, 10, 9 };\n// \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\nnums.insert(nums.end(), nums1.begin(), nums1.end());\n
        list.java
        /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<Integer> nums1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));\nnums.addAll(nums1);  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
        list.cs
        /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.AddRange(nums1);  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
        list_test.go
        /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nnums1 := []int{6, 8, 7, 10, 9}\nnums = append(nums, nums1...)  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
        list.swift
        /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nlet nums1 = [6, 8, 7, 10, 9]\nnums.append(contentsOf: nums1) // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
        list.js
        /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nconst nums1 = [6, 8, 7, 10, 9];\nnums.push(...nums1);  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
        list.ts
        /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nconst nums1: number[] = [6, 8, 7, 10, 9];\nnums.push(...nums1);  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
        list.dart
        /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.addAll(nums1);  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
        list.rs
        /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nlet nums1: Vec<i32> = vec![6, 8, 7, 10, 9];\nnums.extend(nums1);\n
        list.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
        list.kt
        /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nval nums1 = intArrayOf(6, 8, 7, 10, 9).toMutableList()\nnums.addAll(nums1)  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
        list.rb
        # \u62fc\u63a5\u4e24\u4e2a\u5217\u8868\nnums1 = [6, 8, 7, 10, 9]\nnums += nums1\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/list/#6","title":"6. \u00a0 \u6392\u5e8f\u5217\u8868","text":"

        \u5b8c\u6210\u5217\u8868\u6392\u5e8f\u540e\uff0c\u6211\u4eec\u4fbf\u53ef\u4ee5\u4f7f\u7528\u5728\u6570\u7ec4\u7c7b\u7b97\u6cd5\u9898\u4e2d\u7ecf\u5e38\u8003\u67e5\u7684\u201c\u4e8c\u5206\u67e5\u627e\u201d\u548c\u201c\u53cc\u6307\u9488\u201d\u7b97\u6cd5\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
        # \u6392\u5e8f\u5217\u8868\nnums.sort()  # \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        list.cpp
        /* \u6392\u5e8f\u5217\u8868 */\nsort(nums.begin(), nums.end());  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        list.java
        /* \u6392\u5e8f\u5217\u8868 */\nCollections.sort(nums);  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        list.cs
        /* \u6392\u5e8f\u5217\u8868 */\nnums.Sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        list_test.go
        /* \u6392\u5e8f\u5217\u8868 */\nsort.Ints(nums)  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        list.swift
        /* \u6392\u5e8f\u5217\u8868 */\nnums.sort() // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        list.js
        /* \u6392\u5e8f\u5217\u8868 */\nnums.sort((a, b) => a - b);  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        list.ts
        /* \u6392\u5e8f\u5217\u8868 */\nnums.sort((a, b) => a - b);  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        list.dart
        /* \u6392\u5e8f\u5217\u8868 */\nnums.sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        list.rs
        /* \u6392\u5e8f\u5217\u8868 */\nnums.sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        list.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
        list.kt
        /* \u6392\u5e8f\u5217\u8868 */\nnums.sort() // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        list.rb
        # \u6392\u5e8f\u5217\u8868\nnums = nums.sort { |a, b| a <=> b } # \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/list/#432","title":"4.3.2 \u00a0 \u5217\u8868\u5b9e\u73b0","text":"

        \u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u5185\u7f6e\u4e86\u5217\u8868\uff0c\u4f8b\u5982 Java\u3001C++\u3001Python \u7b49\u3002\u5b83\u4eec\u7684\u5b9e\u73b0\u6bd4\u8f83\u590d\u6742\uff0c\u5404\u4e2a\u53c2\u6570\u7684\u8bbe\u5b9a\u4e5f\u975e\u5e38\u8003\u7a76\uff0c\u4f8b\u5982\u521d\u59cb\u5bb9\u91cf\u3001\u6269\u5bb9\u500d\u6570\u7b49\u3002\u611f\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u67e5\u9605\u6e90\u7801\u8fdb\u884c\u5b66\u4e60\u3002

        \u4e3a\u4e86\u52a0\u6df1\u5bf9\u5217\u8868\u5de5\u4f5c\u539f\u7406\u7684\u7406\u89e3\uff0c\u6211\u4eec\u5c1d\u8bd5\u5b9e\u73b0\u4e00\u4e2a\u7b80\u6613\u7248\u5217\u8868\uff0c\u5305\u62ec\u4ee5\u4e0b\u4e09\u4e2a\u91cd\u70b9\u8bbe\u8ba1\u3002

        • \u521d\u59cb\u5bb9\u91cf\uff1a\u9009\u53d6\u4e00\u4e2a\u5408\u7406\u7684\u6570\u7ec4\u521d\u59cb\u5bb9\u91cf\u3002\u5728\u672c\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9009\u62e9 10 \u4f5c\u4e3a\u521d\u59cb\u5bb9\u91cf\u3002
        • \u6570\u91cf\u8bb0\u5f55\uff1a\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf size \uff0c\u7528\u4e8e\u8bb0\u5f55\u5217\u8868\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff0c\u5e76\u968f\u7740\u5143\u7d20\u63d2\u5165\u548c\u5220\u9664\u5b9e\u65f6\u66f4\u65b0\u3002\u6839\u636e\u6b64\u53d8\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4f4d\u5217\u8868\u5c3e\u90e8\uff0c\u4ee5\u53ca\u5224\u65ad\u662f\u5426\u9700\u8981\u6269\u5bb9\u3002
        • \u6269\u5bb9\u673a\u5236\uff1a\u82e5\u63d2\u5165\u5143\u7d20\u65f6\u5217\u8868\u5bb9\u91cf\u5df2\u6ee1\uff0c\u5219\u9700\u8981\u8fdb\u884c\u6269\u5bb9\u3002\u5148\u6839\u636e\u6269\u5bb9\u500d\u6570\u521b\u5efa\u4e00\u4e2a\u66f4\u5927\u7684\u6570\u7ec4\uff0c\u518d\u5c06\u5f53\u524d\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u4f9d\u6b21\u79fb\u52a8\u81f3\u65b0\u6570\u7ec4\u3002\u5728\u672c\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u89c4\u5b9a\u6bcf\u6b21\u5c06\u6570\u7ec4\u6269\u5bb9\u81f3\u4e4b\u524d\u7684 2 \u500d\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_list.py
        class MyList:\n    \"\"\"\u5217\u8868\u7c7b\"\"\"\n\n    def __init__(self):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        self._capacity: int = 10  # \u5217\u8868\u5bb9\u91cf\n        self._arr: list[int] = [0] * self._capacity  # \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n        self._size: int = 0  # \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n        self._extend_ratio: int = 2  # \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n    def size(self) -> int:\n        \"\"\"\u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\"\"\"\n        return self._size\n\n    def capacity(self) -> int:\n        \"\"\"\u83b7\u53d6\u5217\u8868\u5bb9\u91cf\"\"\"\n        return self._capacity\n\n    def get(self, index: int) -> int:\n        \"\"\"\u8bbf\u95ee\u5143\u7d20\"\"\"\n        # \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n        if index < 0 or index >= self._size:\n            raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n        return self._arr[index]\n\n    def set(self, num: int, index: int):\n        \"\"\"\u66f4\u65b0\u5143\u7d20\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n        self._arr[index] = num\n\n    def add(self, num: int):\n        \"\"\"\u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\"\"\"\n        # \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if self.size() == self.capacity():\n            self.extend_capacity()\n        self._arr[self._size] = num\n        self._size += 1\n\n    def insert(self, num: int, index: int):\n        \"\"\"\u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n        # \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if self._size == self.capacity():\n            self.extend_capacity()\n        # \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        for j in range(self._size - 1, index - 1, -1):\n            self._arr[j + 1] = self._arr[j]\n        self._arr[index] = num\n        # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        self._size += 1\n\n    def remove(self, index: int) -> int:\n        \"\"\"\u5220\u9664\u5143\u7d20\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n        num = self._arr[index]\n        # \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n        for j in range(index, self._size - 1):\n            self._arr[j] = self._arr[j + 1]\n        # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        self._size -= 1\n        # \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n        return num\n\n    def extend_capacity(self):\n        \"\"\"\u5217\u8868\u6269\u5bb9\"\"\"\n        # \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 _extend_ratio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n        self._arr = self._arr + [0] * self.capacity() * (self._extend_ratio - 1)\n        # \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n        self._capacity = len(self._arr)\n\n    def to_array(self) -> list[int]:\n        \"\"\"\u8fd4\u56de\u6709\u6548\u957f\u5ea6\u7684\u5217\u8868\"\"\"\n        return self._arr[: self._size]\n
        my_list.cpp
        /* \u5217\u8868\u7c7b */\nclass MyList {\n  private:\n    int *arr;             // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n    int arrCapacity = 10; // \u5217\u8868\u5bb9\u91cf\n    int arrSize = 0;      // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n    int extendRatio = 2;   // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n  public:\n    /* \u6784\u9020\u65b9\u6cd5 */\n    MyList() {\n        arr = new int[arrCapacity];\n    }\n\n    /* \u6790\u6784\u65b9\u6cd5 */\n    ~MyList() {\n        delete[] arr;\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n    int size() {\n        return arrSize;\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n    int capacity() {\n        return arrCapacity;\n    }\n\n    /* \u8bbf\u95ee\u5143\u7d20 */\n    int get(int index) {\n        // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n        if (index < 0 || index >= size())\n            throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n        return arr[index];\n    }\n\n    /* \u66f4\u65b0\u5143\u7d20 */\n    void set(int index, int num) {\n        if (index < 0 || index >= size())\n            throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n        arr[index] = num;\n    }\n\n    /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n    void add(int num) {\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if (size() == capacity())\n            extendCapacity();\n        arr[size()] = num;\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        arrSize++;\n    }\n\n    /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n    void insert(int index, int num) {\n        if (index < 0 || index >= size())\n            throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if (size() == capacity())\n            extendCapacity();\n        // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        for (int j = size() - 1; j >= index; j--) {\n            arr[j + 1] = arr[j];\n        }\n        arr[index] = num;\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        arrSize++;\n    }\n\n    /* \u5220\u9664\u5143\u7d20 */\n    int remove(int index) {\n        if (index < 0 || index >= size())\n            throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n        int num = arr[index];\n        // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n        for (int j = index; j < size() - 1; j++) {\n            arr[j] = arr[j + 1];\n        }\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        arrSize--;\n        // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n        return num;\n    }\n\n    /* \u5217\u8868\u6269\u5bb9 */\n    void extendCapacity() {\n        // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\n        int newCapacity = capacity() * extendRatio;\n        int *tmp = arr;\n        arr = new int[newCapacity];\n        // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n        for (int i = 0; i < size(); i++) {\n            arr[i] = tmp[i];\n        }\n        // \u91ca\u653e\u5185\u5b58\n        delete[] tmp;\n        arrCapacity = newCapacity;\n    }\n\n    /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a Vector \u7528\u4e8e\u6253\u5370 */\n    vector<int> toVector() {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        vector<int> vec(size());\n        for (int i = 0; i < size(); i++) {\n            vec[i] = arr[i];\n        }\n        return vec;\n    }\n};\n
        my_list.java
        /* \u5217\u8868\u7c7b */\nclass MyList {\n    private int[] arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n    private int capacity = 10; // \u5217\u8868\u5bb9\u91cf\n    private int size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n    private int extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public MyList() {\n        arr = new int[capacity];\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\n    public int size() {\n        return size;\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n    public int capacity() {\n        return capacity;\n    }\n\n    /* \u8bbf\u95ee\u5143\u7d20 */\n    public int get(int index) {\n        // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n        return arr[index];\n    }\n\n    /* \u66f4\u65b0\u5143\u7d20 */\n    public void set(int index, int num) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n        arr[index] = num;\n    }\n\n    /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n    public void add(int num) {\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if (size == capacity())\n            extendCapacity();\n        arr[size] = num;\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        size++;\n    }\n\n    /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n    public void insert(int index, int num) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if (size == capacity())\n            extendCapacity();\n        // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        for (int j = size - 1; j >= index; j--) {\n            arr[j + 1] = arr[j];\n        }\n        arr[index] = num;\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        size++;\n    }\n\n    /* \u5220\u9664\u5143\u7d20 */\n    public int remove(int index) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n        int num = arr[index];\n        // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n        for (int j = index; j < size - 1; j++) {\n            arr[j] = arr[j + 1];\n        }\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        size--;\n        // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n        return num;\n    }\n\n    /* \u5217\u8868\u6269\u5bb9 */\n    public void extendCapacity() {\n        // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n        arr = Arrays.copyOf(arr, capacity() * extendRatio);\n        // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n        capacity = arr.length;\n    }\n\n    /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n    public int[] toArray() {\n        int size = size();\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        int[] arr = new int[size];\n        for (int i = 0; i < size; i++) {\n            arr[i] = get(i);\n        }\n        return arr;\n    }\n}\n
        my_list.cs
        /* \u5217\u8868\u7c7b */\nclass MyList {\n    private int[] arr;           // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n    private int arrCapacity = 10;    // \u5217\u8868\u5bb9\u91cf\n    private int arrSize = 0;         // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n    private readonly int extendRatio = 2;  // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public MyList() {\n        arr = new int[arrCapacity];\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n    public int Size() {\n        return arrSize;\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n    public int Capacity() {\n        return arrCapacity;\n    }\n\n    /* \u8bbf\u95ee\u5143\u7d20 */\n    public int Get(int index) {\n        // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n        return arr[index];\n    }\n\n    /* \u66f4\u65b0\u5143\u7d20 */\n    public void Set(int index, int num) {\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n        arr[index] = num;\n    }\n\n    /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n    public void Add(int num) {\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if (arrSize == arrCapacity)\n            ExtendCapacity();\n        arr[arrSize] = num;\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        arrSize++;\n    }\n\n    /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n    public void Insert(int index, int num) {\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if (arrSize == arrCapacity)\n            ExtendCapacity();\n        // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        for (int j = arrSize - 1; j >= index; j--) {\n            arr[j + 1] = arr[j];\n        }\n        arr[index] = num;\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        arrSize++;\n    }\n\n    /* \u5220\u9664\u5143\u7d20 */\n    public int Remove(int index) {\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n        int num = arr[index];\n        // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n        for (int j = index; j < arrSize - 1; j++) {\n            arr[j] = arr[j + 1];\n        }\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        arrSize--;\n        // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n        return num;\n    }\n\n    /* \u5217\u8868\u6269\u5bb9 */\n    public void ExtendCapacity() {\n        // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a arrCapacity * extendRatio \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n        Array.Resize(ref arr, arrCapacity * extendRatio);\n        // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n        arrCapacity = arr.Length;\n    }\n\n    /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n    public int[] ToArray() {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        int[] arr = new int[arrSize];\n        for (int i = 0; i < arrSize; i++) {\n            arr[i] = Get(i);\n        }\n        return arr;\n    }\n}\n
        my_list.go
        /* \u5217\u8868\u7c7b */\ntype myList struct {\n    arrCapacity int\n    arr         []int\n    arrSize     int\n    extendRatio int\n}\n\n/* \u6784\u9020\u51fd\u6570 */\nfunc newMyList() *myList {\n    return &myList{\n        arrCapacity: 10,              // \u5217\u8868\u5bb9\u91cf\n        arr:         make([]int, 10), // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n        arrSize:     0,               // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n        extendRatio: 2,               // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n    }\n}\n\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\nfunc (l *myList) size() int {\n    return l.arrSize\n}\n\n/*  \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nfunc (l *myList) capacity() int {\n    return l.arrCapacity\n}\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nfunc (l *myList) get(index int) int {\n    // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n    if index < 0 || index >= l.arrSize {\n        panic(\"\u7d22\u5f15\u8d8a\u754c\")\n    }\n    return l.arr[index]\n}\n\n/* \u66f4\u65b0\u5143\u7d20 */\nfunc (l *myList) set(num, index int) {\n    if index < 0 || index >= l.arrSize {\n        panic(\"\u7d22\u5f15\u8d8a\u754c\")\n    }\n    l.arr[index] = num\n}\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nfunc (l *myList) add(num int) {\n    // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n    if l.arrSize == l.arrCapacity {\n        l.extendCapacity()\n    }\n    l.arr[l.arrSize] = num\n    // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n    l.arrSize++\n}\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nfunc (l *myList) insert(num, index int) {\n    if index < 0 || index >= l.arrSize {\n        panic(\"\u7d22\u5f15\u8d8a\u754c\")\n    }\n    // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n    if l.arrSize == l.arrCapacity {\n        l.extendCapacity()\n    }\n    // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for j := l.arrSize - 1; j >= index; j-- {\n        l.arr[j+1] = l.arr[j]\n    }\n    l.arr[index] = num\n    // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n    l.arrSize++\n}\n\n/* \u5220\u9664\u5143\u7d20 */\nfunc (l *myList) remove(index int) int {\n    if index < 0 || index >= l.arrSize {\n        panic(\"\u7d22\u5f15\u8d8a\u754c\")\n    }\n    num := l.arr[index]\n    // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for j := index; j < l.arrSize-1; j++ {\n        l.arr[j] = l.arr[j+1]\n    }\n    // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n    l.arrSize--\n    // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n    return num\n}\n\n/* \u5217\u8868\u6269\u5bb9 */\nfunc (l *myList) extendCapacity() {\n    // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    l.arr = append(l.arr, make([]int, l.arrCapacity*(l.extendRatio-1))...)\n    // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n    l.arrCapacity = len(l.arr)\n}\n\n/* \u8fd4\u56de\u6709\u6548\u957f\u5ea6\u7684\u5217\u8868 */\nfunc (l *myList) toArray() []int {\n    // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n    return l.arr[:l.arrSize]\n}\n
        my_list.swift
        /* \u5217\u8868\u7c7b */\nclass MyList {\n    private var arr: [Int] // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n    private var _capacity: Int // \u5217\u8868\u5bb9\u91cf\n    private var _size: Int // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n    private let extendRatio: Int // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    init() {\n        _capacity = 10\n        _size = 0\n        extendRatio = 2\n        arr = Array(repeating: 0, count: _capacity)\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n    func size() -> Int {\n        _size\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n    func capacity() -> Int {\n        _capacity\n    }\n\n    /* \u8bbf\u95ee\u5143\u7d20 */\n    func get(index: Int) -> Int {\n        // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u9519\u8bef\uff0c\u4e0b\u540c\n        if index < 0 || index >= size() {\n            fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n        }\n        return arr[index]\n    }\n\n    /* \u66f4\u65b0\u5143\u7d20 */\n    func set(index: Int, num: Int) {\n        if index < 0 || index >= size() {\n            fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n        }\n        arr[index] = num\n    }\n\n    /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n    func add(num: Int) {\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if size() == capacity() {\n            extendCapacity()\n        }\n        arr[size()] = num\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        _size += 1\n    }\n\n    /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n    func insert(index: Int, num: Int) {\n        if index < 0 || index >= size() {\n            fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n        }\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if size() == capacity() {\n            extendCapacity()\n        }\n        // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        for j in (index ..< size()).reversed() {\n            arr[j + 1] = arr[j]\n        }\n        arr[index] = num\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        _size += 1\n    }\n\n    /* \u5220\u9664\u5143\u7d20 */\n    @discardableResult\n    func remove(index: Int) -> Int {\n        if index < 0 || index >= size() {\n            fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n        }\n        let num = arr[index]\n        // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n        for j in index ..< (size() - 1) {\n            arr[j] = arr[j + 1]\n        }\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        _size -= 1\n        // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n        return num\n    }\n\n    /* \u5217\u8868\u6269\u5bb9 */\n    func extendCapacity() {\n        // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n        arr = arr + Array(repeating: 0, count: capacity() * (extendRatio - 1))\n        // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n        _capacity = arr.count\n    }\n\n    /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n    func toArray() -> [Int] {\n        Array(arr.prefix(size()))\n    }\n}\n
        my_list.js
        /* \u5217\u8868\u7c7b */\nclass MyList {\n    #arr = new Array(); // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n    #capacity = 10; // \u5217\u8868\u5bb9\u91cf\n    #size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n    #extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor() {\n        this.#arr = new Array(this.#capacity);\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n    size() {\n        return this.#size;\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n    capacity() {\n        return this.#capacity;\n    }\n\n    /* \u8bbf\u95ee\u5143\u7d20 */\n    get(index) {\n        // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n        if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n        return this.#arr[index];\n    }\n\n    /* \u66f4\u65b0\u5143\u7d20 */\n    set(index, num) {\n        if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n        this.#arr[index] = num;\n    }\n\n    /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n    add(num) {\n        // \u5982\u679c\u957f\u5ea6\u7b49\u4e8e\u5bb9\u91cf\uff0c\u5219\u9700\u8981\u6269\u5bb9\n        if (this.#size === this.#capacity) {\n            this.extendCapacity();\n        }\n        // \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u5c3e\u90e8\n        this.#arr[this.#size] = num;\n        this.#size++;\n    }\n\n    /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n    insert(index, num) {\n        if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if (this.#size === this.#capacity) {\n            this.extendCapacity();\n        }\n        // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        for (let j = this.#size - 1; j >= index; j--) {\n            this.#arr[j + 1] = this.#arr[j];\n        }\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        this.#arr[index] = num;\n        this.#size++;\n    }\n\n    /* \u5220\u9664\u5143\u7d20 */\n    remove(index) {\n        if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n        let num = this.#arr[index];\n        // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n        for (let j = index; j < this.#size - 1; j++) {\n            this.#arr[j] = this.#arr[j + 1];\n        }\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        this.#size--;\n        // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n        return num;\n    }\n\n    /* \u5217\u8868\u6269\u5bb9 */\n    extendCapacity() {\n        // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n        this.#arr = this.#arr.concat(\n            new Array(this.capacity() * (this.#extendRatio - 1))\n        );\n        // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n        this.#capacity = this.#arr.length;\n    }\n\n    /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n    toArray() {\n        let size = this.size();\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        const arr = new Array(size);\n        for (let i = 0; i < size; i++) {\n            arr[i] = this.get(i);\n        }\n        return arr;\n    }\n}\n
        my_list.ts
        /* \u5217\u8868\u7c7b */\nclass MyList {\n    private arr: Array<number>; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n    private _capacity: number = 10; // \u5217\u8868\u5bb9\u91cf\n    private _size: number = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n    private extendRatio: number = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor() {\n        this.arr = new Array(this._capacity);\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n    public size(): number {\n        return this._size;\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n    public capacity(): number {\n        return this._capacity;\n    }\n\n    /* \u8bbf\u95ee\u5143\u7d20 */\n    public get(index: number): number {\n        // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n        if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n        return this.arr[index];\n    }\n\n    /* \u66f4\u65b0\u5143\u7d20 */\n    public set(index: number, num: number): void {\n        if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n        this.arr[index] = num;\n    }\n\n    /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n    public add(num: number): void {\n        // \u5982\u679c\u957f\u5ea6\u7b49\u4e8e\u5bb9\u91cf\uff0c\u5219\u9700\u8981\u6269\u5bb9\n        if (this._size === this._capacity) this.extendCapacity();\n        // \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u5c3e\u90e8\n        this.arr[this._size] = num;\n        this._size++;\n    }\n\n    /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n    public insert(index: number, num: number): void {\n        if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if (this._size === this._capacity) {\n            this.extendCapacity();\n        }\n        // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        for (let j = this._size - 1; j >= index; j--) {\n            this.arr[j + 1] = this.arr[j];\n        }\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        this.arr[index] = num;\n        this._size++;\n    }\n\n    /* \u5220\u9664\u5143\u7d20 */\n    public remove(index: number): number {\n        if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n        let num = this.arr[index];\n        // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n        for (let j = index; j < this._size - 1; j++) {\n            this.arr[j] = this.arr[j + 1];\n        }\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        this._size--;\n        // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n        return num;\n    }\n\n    /* \u5217\u8868\u6269\u5bb9 */\n    public extendCapacity(): void {\n        // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a size \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n        this.arr = this.arr.concat(\n            new Array(this.capacity() * (this.extendRatio - 1))\n        );\n        // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n        this._capacity = this.arr.length;\n    }\n\n    /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n    public toArray(): number[] {\n        let size = this.size();\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        const arr = new Array(size);\n        for (let i = 0; i < size; i++) {\n            arr[i] = this.get(i);\n        }\n        return arr;\n    }\n}\n
        my_list.dart
        /* \u5217\u8868\u7c7b */\nclass MyList {\n  late List<int> _arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n  int _capacity = 10; // \u5217\u8868\u5bb9\u91cf\n  int _size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n  int _extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n  /* \u6784\u9020\u65b9\u6cd5 */\n  MyList() {\n    _arr = List.filled(_capacity, 0);\n  }\n\n  /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n  int size() => _size;\n\n  /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n  int capacity() => _capacity;\n\n  /* \u8bbf\u95ee\u5143\u7d20 */\n  int get(int index) {\n    if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n    return _arr[index];\n  }\n\n  /* \u66f4\u65b0\u5143\u7d20 */\n  void set(int index, int _num) {\n    if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n    _arr[index] = _num;\n  }\n\n  /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n  void add(int _num) {\n    // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n    if (_size == _capacity) extendCapacity();\n    _arr[_size] = _num;\n    // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n    _size++;\n  }\n\n  /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n  void insert(int index, int _num) {\n    if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n    // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n    if (_size == _capacity) extendCapacity();\n    // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for (var j = _size - 1; j >= index; j--) {\n      _arr[j + 1] = _arr[j];\n    }\n    _arr[index] = _num;\n    // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n    _size++;\n  }\n\n  /* \u5220\u9664\u5143\u7d20 */\n  int remove(int index) {\n    if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n    int _num = _arr[index];\n    // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for (var j = index; j < _size - 1; j++) {\n      _arr[j] = _arr[j + 1];\n    }\n    // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n    _size--;\n    // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n    return _num;\n  }\n\n  /* \u5217\u8868\u6269\u5bb9 */\n  void extendCapacity() {\n    // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 _extendRatio \u500d\u7684\u65b0\u6570\u7ec4\n    final _newNums = List.filled(_capacity * _extendRatio, 0);\n    // \u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    List.copyRange(_newNums, 0, _arr);\n    // \u66f4\u65b0 _arr \u7684\u5f15\u7528\n    _arr = _newNums;\n    // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n    _capacity = _arr.length;\n  }\n\n  /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n  List<int> toArray() {\n    List<int> arr = [];\n    for (var i = 0; i < _size; i++) {\n      arr.add(get(i));\n    }\n    return arr;\n  }\n}\n
        my_list.rs
        /* \u5217\u8868\u7c7b */\n#[allow(dead_code)]\nstruct MyList {\n    arr: Vec<i32>,       // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n    capacity: usize,     // \u5217\u8868\u5bb9\u91cf\n    size: usize,         // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n    extend_ratio: usize, // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n}\n\n#[allow(unused, unused_comparisons)]\nimpl MyList {\n    /* \u6784\u9020\u65b9\u6cd5 */\n    pub fn new(capacity: usize) -> Self {\n        let mut vec = vec![0; capacity];\n        Self {\n            arr: vec,\n            capacity,\n            size: 0,\n            extend_ratio: 2,\n        }\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n    pub fn size(&self) -> usize {\n        return self.size;\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n    pub fn capacity(&self) -> usize {\n        return self.capacity;\n    }\n\n    /* \u8bbf\u95ee\u5143\u7d20 */\n    pub fn get(&self, index: usize) -> i32 {\n        // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n        if index >= self.size {\n            panic!(\"\u7d22\u5f15\u8d8a\u754c\")\n        };\n        return self.arr[index];\n    }\n\n    /* \u66f4\u65b0\u5143\u7d20 */\n    pub fn set(&mut self, index: usize, num: i32) {\n        if index >= self.size {\n            panic!(\"\u7d22\u5f15\u8d8a\u754c\")\n        };\n        self.arr[index] = num;\n    }\n\n    /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n    pub fn add(&mut self, num: i32) {\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if self.size == self.capacity() {\n            self.extend_capacity();\n        }\n        self.arr[self.size] = num;\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        self.size += 1;\n    }\n\n    /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n    pub fn insert(&mut self, index: usize, num: i32) {\n        if index >= self.size() {\n            panic!(\"\u7d22\u5f15\u8d8a\u754c\")\n        };\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if self.size == self.capacity() {\n            self.extend_capacity();\n        }\n        // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        for j in (index..self.size).rev() {\n            self.arr[j + 1] = self.arr[j];\n        }\n        self.arr[index] = num;\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        self.size += 1;\n    }\n\n    /* \u5220\u9664\u5143\u7d20 */\n    pub fn remove(&mut self, index: usize) -> i32 {\n        if index >= self.size() {\n            panic!(\"\u7d22\u5f15\u8d8a\u754c\")\n        };\n        let num = self.arr[index];\n        // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n        for j in index..self.size - 1 {\n            self.arr[j] = self.arr[j + 1];\n        }\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        self.size -= 1;\n        // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n        return num;\n    }\n\n    /* \u5217\u8868\u6269\u5bb9 */\n    pub fn extend_capacity(&mut self) {\n        // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extend_ratio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n        let new_capacity = self.capacity * self.extend_ratio;\n        self.arr.resize(new_capacity, 0);\n        // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n        self.capacity = new_capacity;\n    }\n\n    /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n    pub fn to_array(&self) -> Vec<i32> {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        let mut arr = Vec::new();\n        for i in 0..self.size {\n            arr.push(self.get(i));\n        }\n        arr\n    }\n}\n
        my_list.c
        /* \u5217\u8868\u7c7b */\ntypedef struct {\n    int *arr;        // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n    int capacity;    // \u5217\u8868\u5bb9\u91cf\n    int size;        // \u5217\u8868\u5927\u5c0f\n    int extendRatio; // \u5217\u8868\u6bcf\u6b21\u6269\u5bb9\u7684\u500d\u6570\n} MyList;\n\n/* \u6784\u9020\u51fd\u6570 */\nMyList *newMyList() {\n    MyList *nums = malloc(sizeof(MyList));\n    nums->capacity = 10;\n    nums->arr = malloc(sizeof(int) * nums->capacity);\n    nums->size = 0;\n    nums->extendRatio = 2;\n    return nums;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delMyList(MyList *nums) {\n    free(nums->arr);\n    free(nums);\n}\n\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6 */\nint size(MyList *nums) {\n    return nums->size;\n}\n\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nint capacity(MyList *nums) {\n    return nums->capacity;\n}\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nint get(MyList *nums, int index) {\n    assert(index >= 0 && index < nums->size);\n    return nums->arr[index];\n}\n\n/* \u66f4\u65b0\u5143\u7d20 */\nvoid set(MyList *nums, int index, int num) {\n    assert(index >= 0 && index < nums->size);\n    nums->arr[index] = num;\n}\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nvoid add(MyList *nums, int num) {\n    if (size(nums) == capacity(nums)) {\n        extendCapacity(nums); // \u6269\u5bb9\n    }\n    nums->arr[size(nums)] = num;\n    nums->size++;\n}\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nvoid insert(MyList *nums, int index, int num) {\n    assert(index >= 0 && index < size(nums));\n    // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n    if (size(nums) == capacity(nums)) {\n        extendCapacity(nums); // \u6269\u5bb9\n    }\n    for (int i = size(nums); i > index; --i) {\n        nums->arr[i] = nums->arr[i - 1];\n    }\n    nums->arr[index] = num;\n    nums->size++;\n}\n\n/* \u5220\u9664\u5143\u7d20 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nint removeItem(MyList *nums, int index) {\n    assert(index >= 0 && index < size(nums));\n    int num = nums->arr[index];\n    for (int i = index; i < size(nums) - 1; i++) {\n        nums->arr[i] = nums->arr[i + 1];\n    }\n    nums->size--;\n    return num;\n}\n\n/* \u5217\u8868\u6269\u5bb9 */\nvoid extendCapacity(MyList *nums) {\n    // \u5148\u5206\u914d\u7a7a\u95f4\n    int newCapacity = capacity(nums) * nums->extendRatio;\n    int *extend = (int *)malloc(sizeof(int) * newCapacity);\n    int *temp = nums->arr;\n\n    // \u62f7\u8d1d\u65e7\u6570\u636e\u5230\u65b0\u6570\u636e\n    for (int i = 0; i < size(nums); i++)\n        extend[i] = nums->arr[i];\n\n    // \u91ca\u653e\u65e7\u6570\u636e\n    free(temp);\n\n    // \u66f4\u65b0\u65b0\u6570\u636e\n    nums->arr = extend;\n    nums->capacity = newCapacity;\n}\n\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a Array \u7528\u4e8e\u6253\u5370 */\nint *toArray(MyList *nums) {\n    return nums->arr;\n}\n
        my_list.kt
        /* \u5217\u8868\u7c7b */\nclass MyList {\n    private var arr: IntArray = intArrayOf() // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n    private var capacity: Int = 10 // \u5217\u8868\u5bb9\u91cf\n    private var size: Int = 0 // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n    private var extendRatio: Int = 2 // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    init {\n        arr = IntArray(capacity)\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\n    fun size(): Int {\n        return size\n    }\n\n    /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n    fun capacity(): Int {\n        return capacity\n    }\n\n    /* \u8bbf\u95ee\u5143\u7d20 */\n    fun get(index: Int): Int {\n        // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n        if (index < 0 || index >= size)\n            throw IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\")\n        return arr[index]\n    }\n\n    /* \u66f4\u65b0\u5143\u7d20 */\n    fun set(index: Int, num: Int) {\n        if (index < 0 || index >= size)\n            throw IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\")\n        arr[index] = num\n    }\n\n    /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n    fun add(num: Int) {\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if (size == capacity())\n            extendCapacity()\n        arr[size] = num\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        size++\n    }\n\n    /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n    fun insert(index: Int, num: Int) {\n        if (index < 0 || index >= size)\n            throw IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\")\n        // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n        if (size == capacity())\n            extendCapacity()\n        // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        for (j in size - 1 downTo index)\n            arr[j + 1] = arr[j]\n        arr[index] = num\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        size++\n    }\n\n    /* \u5220\u9664\u5143\u7d20 */\n    fun remove(index: Int): Int {\n        if (index < 0 || index >= size)\n            throw IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\")\n        val num = arr[index]\n        // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n        for (j in index..<size - 1)\n            arr[j] = arr[j + 1]\n        // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n        size--\n        // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n        return num\n    }\n\n    /* \u5217\u8868\u6269\u5bb9 */\n    fun extendCapacity() {\n        // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n        arr = arr.copyOf(capacity() * extendRatio)\n        // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n        capacity = arr.size\n    }\n\n    /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n    fun toArray(): IntArray {\n        val size = size()\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        val arr = IntArray(size)\n        for (i in 0..<size) {\n            arr[i] = get(i)\n        }\n        return arr\n    }\n}\n
        my_list.rb
        ### \u5217\u8868\u7c7b ###\nclass MyList\n  attr_reader :size       # \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n  attr_reader :capacity   # \u83b7\u53d6\u5217\u8868\u5bb9\u91cf\n\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize\n    @capacity = 10\n    @size = 0\n    @extend_ratio = 2\n    @arr = Array.new(capacity)\n  end\n\n  ### \u8bbf\u95ee\u5143\u7d20 ###\n  def get(index)\n    # \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n    raise IndexError, \"\u7d22\u5f15\u8d8a\u754c\" if index < 0 || index >= size\n    @arr[index]\n  end\n\n  ### \u8bbf\u95ee\u5143\u7d20 ###\n  def set(index, num)\n    raise IndexError, \"\u7d22\u5f15\u8d8a\u754c\" if index < 0 || index >= size\n    @arr[index] = num\n  end\n\n  ### \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 ###\n  def add(num)\n    # \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n    extend_capacity if size == capacity\n    @arr[size] = num\n\n    # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n    @size += 1\n  end\n\n  ### \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 ###\n  def insert(index, num)\n    raise IndexError, \"\u7d22\u5f15\u8d8a\u754c\" if index < 0 || index >= size\n\n    # \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n    extend_capacity if size == capacity\n\n    # \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    for j in (size - 1).downto(index)\n      @arr[j + 1] = @arr[j]\n    end\n    @arr[index] = num\n\n    # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n    @size += 1\n  end\n\n  ### \u5220\u9664\u5143\u7d20 ###\n  def remove(index)\n    raise IndexError, \"\u7d22\u5f15\u8d8a\u754c\" if index < 0 || index >= size\n    num = @arr[index]\n\n    # \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n    for j in index...size\n      @arr[j] = @arr[j + 1]\n    end\n\n    # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n    @size -= 1\n\n    # \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n    num\n  end\n\n  ### \u5217\u8868\u6269\u5bb9 ###\n  def extend_capacity\n    # \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extend_ratio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n    arr = @arr.dup + Array.new(capacity * (@extend_ratio - 1))\n    # \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n    @capacity = arr.length\n  end\n\n  ### \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 ###\n  def to_array\n    sz = size\n    # \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n    arr = Array.new(sz)\n    for i in 0...sz\n      arr[i] = get(i)\n    end\n    arr\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_array_and_linkedlist/ram_and_cache/","title":"4.4 \u00a0 \u5185\u5b58\u4e0e\u7f13\u5b58 *","text":"

        \u5728\u672c\u7ae0\u7684\u524d\u4e24\u8282\u4e2d\uff0c\u6211\u4eec\u63a2\u8ba8\u4e86\u6570\u7ec4\u548c\u94fe\u8868\u8fd9\u4e24\u79cd\u57fa\u7840\u4e14\u91cd\u8981\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u4eec\u5206\u522b\u4ee3\u8868\u4e86\u201c\u8fde\u7eed\u5b58\u50a8\u201d\u548c\u201c\u5206\u6563\u5b58\u50a8\u201d\u4e24\u79cd\u7269\u7406\u7ed3\u6784\u3002

        \u5b9e\u9645\u4e0a\uff0c\u7269\u7406\u7ed3\u6784\u5728\u5f88\u5927\u7a0b\u5ea6\u4e0a\u51b3\u5b9a\u4e86\u7a0b\u5e8f\u5bf9\u5185\u5b58\u548c\u7f13\u5b58\u7684\u4f7f\u7528\u6548\u7387\uff0c\u8fdb\u800c\u5f71\u54cd\u7b97\u6cd5\u7a0b\u5e8f\u7684\u6574\u4f53\u6027\u80fd\u3002

        "},{"location":"chapter_array_and_linkedlist/ram_and_cache/#441","title":"4.4.1 \u00a0 \u8ba1\u7b97\u673a\u5b58\u50a8\u8bbe\u5907","text":"

        \u8ba1\u7b97\u673a\u4e2d\u5305\u62ec\u4e09\u79cd\u7c7b\u578b\u7684\u5b58\u50a8\u8bbe\u5907\uff1a\u786c\u76d8\uff08hard disk\uff09\u3001\u5185\u5b58\uff08random-access memory, RAM\uff09\u3001\u7f13\u5b58\uff08cache memory\uff09\u3002\u8868 4-2 \u5c55\u793a\u4e86\u5b83\u4eec\u5728\u8ba1\u7b97\u673a\u7cfb\u7edf\u4e2d\u7684\u4e0d\u540c\u89d2\u8272\u548c\u6027\u80fd\u7279\u70b9\u3002

        \u8868 4-2 \u00a0 \u8ba1\u7b97\u673a\u7684\u5b58\u50a8\u8bbe\u5907

        \u786c\u76d8 \u5185\u5b58 \u7f13\u5b58 \u7528\u9014 \u957f\u671f\u5b58\u50a8\u6570\u636e\uff0c\u5305\u62ec\u64cd\u4f5c\u7cfb\u7edf\u3001\u7a0b\u5e8f\u3001\u6587\u4ef6\u7b49 \u4e34\u65f6\u5b58\u50a8\u5f53\u524d\u8fd0\u884c\u7684\u7a0b\u5e8f\u548c\u6b63\u5728\u5904\u7406\u7684\u6570\u636e \u5b58\u50a8\u7ecf\u5e38\u8bbf\u95ee\u7684\u6570\u636e\u548c\u6307\u4ee4\uff0c\u51cf\u5c11 CPU \u8bbf\u95ee\u5185\u5b58\u7684\u6b21\u6570 \u6613\u5931\u6027 \u65ad\u7535\u540e\u6570\u636e\u4e0d\u4f1a\u4e22\u5931 \u65ad\u7535\u540e\u6570\u636e\u4f1a\u4e22\u5931 \u65ad\u7535\u540e\u6570\u636e\u4f1a\u4e22\u5931 \u5bb9\u91cf \u8f83\u5927\uff0cTB \u7ea7\u522b \u8f83\u5c0f\uff0cGB \u7ea7\u522b \u975e\u5e38\u5c0f\uff0cMB \u7ea7\u522b \u901f\u5ea6 \u8f83\u6162\uff0c\u51e0\u767e\u5230\u51e0\u5343 MB/s \u8f83\u5feb\uff0c\u51e0\u5341 GB/s \u975e\u5e38\u5feb\uff0c\u51e0\u5341\u5230\u51e0\u767e GB/s \u4ef7\u683c\uff08\u4eba\u6c11\u5e01\uff09 \u8f83\u4fbf\u5b9c\uff0c\u51e0\u6bdb\u5230\u51e0\u5143 / GB \u8f83\u8d35\uff0c\u51e0\u5341\u5230\u51e0\u767e\u5143 / GB \u975e\u5e38\u8d35\uff0c\u968f CPU \u6253\u5305\u8ba1\u4ef7

        \u6211\u4eec\u53ef\u4ee5\u5c06\u8ba1\u7b97\u673a\u5b58\u50a8\u7cfb\u7edf\u60f3\u8c61\u4e3a\u56fe 4-9 \u6240\u793a\u7684\u91d1\u5b57\u5854\u7ed3\u6784\u3002\u8d8a\u9760\u8fd1\u91d1\u5b57\u5854\u9876\u7aef\u7684\u5b58\u50a8\u8bbe\u5907\u7684\u901f\u5ea6\u8d8a\u5feb\u3001\u5bb9\u91cf\u8d8a\u5c0f\u3001\u6210\u672c\u8d8a\u9ad8\u3002\u8fd9\u79cd\u591a\u5c42\u7ea7\u7684\u8bbe\u8ba1\u5e76\u975e\u5076\u7136\uff0c\u800c\u662f\u8ba1\u7b97\u673a\u79d1\u5b66\u5bb6\u548c\u5de5\u7a0b\u5e08\u4eec\u7ecf\u8fc7\u6df1\u601d\u719f\u8651\u7684\u7ed3\u679c\u3002

        • \u786c\u76d8\u96be\u4ee5\u88ab\u5185\u5b58\u53d6\u4ee3\u3002\u9996\u5148\uff0c\u5185\u5b58\u4e2d\u7684\u6570\u636e\u5728\u65ad\u7535\u540e\u4f1a\u4e22\u5931\uff0c\u56e0\u6b64\u5b83\u4e0d\u9002\u5408\u957f\u671f\u5b58\u50a8\u6570\u636e\uff1b\u5176\u6b21\uff0c\u5185\u5b58\u7684\u6210\u672c\u662f\u786c\u76d8\u7684\u51e0\u5341\u500d\uff0c\u8fd9\u4f7f\u5f97\u5b83\u96be\u4ee5\u5728\u6d88\u8d39\u8005\u5e02\u573a\u666e\u53ca\u3002
        • \u7f13\u5b58\u7684\u5927\u5bb9\u91cf\u548c\u9ad8\u901f\u5ea6\u96be\u4ee5\u517c\u5f97\u3002\u968f\u7740 L1\u3001L2\u3001L3 \u7f13\u5b58\u7684\u5bb9\u91cf\u9010\u6b65\u589e\u5927\uff0c\u5176\u7269\u7406\u5c3a\u5bf8\u4f1a\u53d8\u5927\uff0c\u4e0e CPU \u6838\u5fc3\u4e4b\u95f4\u7684\u7269\u7406\u8ddd\u79bb\u4f1a\u53d8\u8fdc\uff0c\u4ece\u800c\u5bfc\u81f4\u6570\u636e\u4f20\u8f93\u65f6\u95f4\u589e\u52a0\uff0c\u5143\u7d20\u8bbf\u95ee\u5ef6\u8fdf\u53d8\u9ad8\u3002\u5728\u5f53\u524d\u6280\u672f\u4e0b\uff0c\u591a\u5c42\u7ea7\u7684\u7f13\u5b58\u7ed3\u6784\u662f\u5bb9\u91cf\u3001\u901f\u5ea6\u548c\u6210\u672c\u4e4b\u95f4\u7684\u6700\u4f73\u5e73\u8861\u70b9\u3002

        \u56fe 4-9 \u00a0 \u8ba1\u7b97\u673a\u5b58\u50a8\u7cfb\u7edf

        Tip

        \u8ba1\u7b97\u673a\u7684\u5b58\u50a8\u5c42\u6b21\u7ed3\u6784\u4f53\u73b0\u4e86\u901f\u5ea6\u3001\u5bb9\u91cf\u548c\u6210\u672c\u4e09\u8005\u4e4b\u95f4\u7684\u7cbe\u5999\u5e73\u8861\u3002\u5b9e\u9645\u4e0a\uff0c\u8fd9\u79cd\u6743\u8861\u666e\u904d\u5b58\u5728\u4e8e\u6240\u6709\u5de5\u4e1a\u9886\u57df\uff0c\u5b83\u8981\u6c42\u6211\u4eec\u5728\u4e0d\u540c\u7684\u4f18\u52bf\u548c\u9650\u5236\u4e4b\u95f4\u627e\u5230\u6700\u4f73\u5e73\u8861\u70b9\u3002

        \u603b\u7684\u6765\u8bf4\uff0c\u786c\u76d8\u7528\u4e8e\u957f\u671f\u5b58\u50a8\u5927\u91cf\u6570\u636e\uff0c\u5185\u5b58\u7528\u4e8e\u4e34\u65f6\u5b58\u50a8\u7a0b\u5e8f\u8fd0\u884c\u4e2d\u6b63\u5728\u5904\u7406\u7684\u6570\u636e\uff0c\u800c\u7f13\u5b58\u5219\u7528\u4e8e\u5b58\u50a8\u7ecf\u5e38\u8bbf\u95ee\u7684\u6570\u636e\u548c\u6307\u4ee4\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u8fd0\u884c\u6548\u7387\u3002\u4e09\u8005\u5171\u540c\u534f\u4f5c\uff0c\u786e\u4fdd\u8ba1\u7b97\u673a\u7cfb\u7edf\u9ad8\u6548\u8fd0\u884c\u3002

        \u5982\u56fe 4-10 \u6240\u793a\uff0c\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u6570\u636e\u4f1a\u4ece\u786c\u76d8\u4e2d\u88ab\u8bfb\u53d6\u5230\u5185\u5b58\u4e2d\uff0c\u4f9b CPU \u8ba1\u7b97\u4f7f\u7528\u3002\u7f13\u5b58\u53ef\u4ee5\u770b\u4f5c CPU \u7684\u4e00\u90e8\u5206\uff0c\u5b83\u901a\u8fc7\u667a\u80fd\u5730\u4ece\u5185\u5b58\u52a0\u8f7d\u6570\u636e\uff0c\u7ed9 CPU \u63d0\u4f9b\u9ad8\u901f\u7684\u6570\u636e\u8bfb\u53d6\uff0c\u4ece\u800c\u663e\u8457\u63d0\u5347\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\uff0c\u51cf\u5c11\u5bf9\u8f83\u6162\u7684\u5185\u5b58\u7684\u4f9d\u8d56\u3002

        \u56fe 4-10 \u00a0 \u786c\u76d8\u3001\u5185\u5b58\u548c\u7f13\u5b58\u4e4b\u95f4\u7684\u6570\u636e\u6d41\u901a

        "},{"location":"chapter_array_and_linkedlist/ram_and_cache/#442","title":"4.4.2 \u00a0 \u6570\u636e\u7ed3\u6784\u7684\u5185\u5b58\u6548\u7387","text":"

        \u5728\u5185\u5b58\u7a7a\u95f4\u5229\u7528\u65b9\u9762\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u5404\u81ea\u5177\u6709\u4f18\u52bf\u548c\u5c40\u9650\u6027\u3002

        \u4e00\u65b9\u9762\uff0c\u5185\u5b58\u662f\u6709\u9650\u7684\uff0c\u4e14\u540c\u4e00\u5757\u5185\u5b58\u4e0d\u80fd\u88ab\u591a\u4e2a\u7a0b\u5e8f\u5171\u4eab\uff0c\u56e0\u6b64\u6211\u4eec\u5e0c\u671b\u6570\u636e\u7ed3\u6784\u80fd\u591f\u5c3d\u53ef\u80fd\u9ad8\u6548\u5730\u5229\u7528\u7a7a\u95f4\u3002\u6570\u7ec4\u7684\u5143\u7d20\u7d27\u5bc6\u6392\u5217\uff0c\u4e0d\u9700\u8981\u989d\u5916\u7684\u7a7a\u95f4\u6765\u5b58\u50a8\u94fe\u8868\u8282\u70b9\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\uff0c\u56e0\u6b64\u7a7a\u95f4\u6548\u7387\u66f4\u9ad8\u3002\u7136\u800c\uff0c\u6570\u7ec4\u9700\u8981\u4e00\u6b21\u6027\u5206\u914d\u8db3\u591f\u7684\u8fde\u7eed\u5185\u5b58\u7a7a\u95f4\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6d6a\u8d39\uff0c\u6570\u7ec4\u6269\u5bb9\u4e5f\u9700\u8981\u989d\u5916\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u6210\u672c\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u94fe\u8868\u4ee5\u201c\u8282\u70b9\u201d\u4e3a\u5355\u4f4d\u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d\u548c\u56de\u6536\uff0c\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002

        \u53e6\u4e00\u65b9\u9762\uff0c\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u968f\u7740\u53cd\u590d\u7533\u8bf7\u4e0e\u91ca\u653e\u5185\u5b58\uff0c\u7a7a\u95f2\u5185\u5b58\u7684\u788e\u7247\u5316\u7a0b\u5ea6\u4f1a\u8d8a\u6765\u8d8a\u9ad8\uff0c\u4ece\u800c\u5bfc\u81f4\u5185\u5b58\u7684\u5229\u7528\u6548\u7387\u964d\u4f4e\u3002\u6570\u7ec4\u7531\u4e8e\u5176\u8fde\u7eed\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u76f8\u5bf9\u4e0d\u5bb9\u6613\u5bfc\u81f4\u5185\u5b58\u788e\u7247\u5316\u3002\u76f8\u53cd\uff0c\u94fe\u8868\u7684\u5143\u7d20\u662f\u5206\u6563\u5b58\u50a8\u7684\uff0c\u5728\u9891\u7e41\u7684\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u4e2d\uff0c\u66f4\u5bb9\u6613\u5bfc\u81f4\u5185\u5b58\u788e\u7247\u5316\u3002

        "},{"location":"chapter_array_and_linkedlist/ram_and_cache/#443","title":"4.4.3 \u00a0 \u6570\u636e\u7ed3\u6784\u7684\u7f13\u5b58\u6548\u7387","text":"

        \u7f13\u5b58\u867d\u7136\u5728\u7a7a\u95f4\u5bb9\u91cf\u4e0a\u8fdc\u5c0f\u4e8e\u5185\u5b58\uff0c\u4f46\u5b83\u6bd4\u5185\u5b58\u5feb\u5f97\u591a\uff0c\u5728\u7a0b\u5e8f\u6267\u884c\u901f\u5ea6\u4e0a\u8d77\u7740\u81f3\u5173\u91cd\u8981\u7684\u4f5c\u7528\u3002\u7531\u4e8e\u7f13\u5b58\u7684\u5bb9\u91cf\u6709\u9650\uff0c\u53ea\u80fd\u5b58\u50a8\u4e00\u5c0f\u90e8\u5206\u9891\u7e41\u8bbf\u95ee\u7684\u6570\u636e\uff0c\u56e0\u6b64\u5f53 CPU \u5c1d\u8bd5\u8bbf\u95ee\u7684\u6570\u636e\u4e0d\u5728\u7f13\u5b58\u4e2d\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u7f13\u5b58\u672a\u547d\u4e2d\uff08cache miss\uff09\uff0c\u6b64\u65f6 CPU \u4e0d\u5f97\u4e0d\u4ece\u901f\u5ea6\u8f83\u6162\u7684\u5185\u5b58\u4e2d\u52a0\u8f7d\u6240\u9700\u6570\u636e\u3002

        \u663e\u7136\uff0c\u201c\u7f13\u5b58\u672a\u547d\u4e2d\u201d\u8d8a\u5c11\uff0cCPU \u8bfb\u5199\u6570\u636e\u7684\u6548\u7387\u5c31\u8d8a\u9ad8\uff0c\u7a0b\u5e8f\u6027\u80fd\u4e5f\u5c31\u8d8a\u597d\u3002\u6211\u4eec\u5c06 CPU \u4ece\u7f13\u5b58\u4e2d\u6210\u529f\u83b7\u53d6\u6570\u636e\u7684\u6bd4\u4f8b\u79f0\u4e3a\u7f13\u5b58\u547d\u4e2d\u7387\uff08cache hit rate\uff09\uff0c\u8fd9\u4e2a\u6307\u6807\u901a\u5e38\u7528\u6765\u8861\u91cf\u7f13\u5b58\u6548\u7387\u3002

        \u4e3a\u4e86\u5c3d\u53ef\u80fd\u8fbe\u5230\u66f4\u9ad8\u7684\u6548\u7387\uff0c\u7f13\u5b58\u4f1a\u91c7\u53d6\u4ee5\u4e0b\u6570\u636e\u52a0\u8f7d\u673a\u5236\u3002

        • \u7f13\u5b58\u884c\uff1a\u7f13\u5b58\u4e0d\u662f\u5355\u4e2a\u5b57\u8282\u5730\u5b58\u50a8\u4e0e\u52a0\u8f7d\u6570\u636e\uff0c\u800c\u662f\u4ee5\u7f13\u5b58\u884c\u4e3a\u5355\u4f4d\u3002\u76f8\u6bd4\u4e8e\u5355\u4e2a\u5b57\u8282\u7684\u4f20\u8f93\uff0c\u7f13\u5b58\u884c\u7684\u4f20\u8f93\u5f62\u5f0f\u66f4\u52a0\u9ad8\u6548\u3002
        • \u9884\u53d6\u673a\u5236\uff1a\u5904\u7406\u5668\u4f1a\u5c1d\u8bd5\u9884\u6d4b\u6570\u636e\u8bbf\u95ee\u6a21\u5f0f\uff08\u4f8b\u5982\u987a\u5e8f\u8bbf\u95ee\u3001\u56fa\u5b9a\u6b65\u957f\u8df3\u8dc3\u8bbf\u95ee\u7b49\uff09\uff0c\u5e76\u6839\u636e\u7279\u5b9a\u6a21\u5f0f\u5c06\u6570\u636e\u52a0\u8f7d\u81f3\u7f13\u5b58\u4e4b\u4e2d\uff0c\u4ece\u800c\u63d0\u5347\u547d\u4e2d\u7387\u3002
        • \u7a7a\u95f4\u5c40\u90e8\u6027\uff1a\u5982\u679c\u4e00\u4e2a\u6570\u636e\u88ab\u8bbf\u95ee\uff0c\u90a3\u4e48\u5b83\u9644\u8fd1\u7684\u6570\u636e\u53ef\u80fd\u8fd1\u671f\u4e5f\u4f1a\u88ab\u8bbf\u95ee\u3002\u56e0\u6b64\uff0c\u7f13\u5b58\u5728\u52a0\u8f7d\u67d0\u4e00\u6570\u636e\u65f6\uff0c\u4e5f\u4f1a\u52a0\u8f7d\u5176\u9644\u8fd1\u7684\u6570\u636e\uff0c\u4ee5\u63d0\u9ad8\u547d\u4e2d\u7387\u3002
        • \u65f6\u95f4\u5c40\u90e8\u6027\uff1a\u5982\u679c\u4e00\u4e2a\u6570\u636e\u88ab\u8bbf\u95ee\uff0c\u90a3\u4e48\u5b83\u5728\u4e0d\u4e45\u7684\u5c06\u6765\u5f88\u53ef\u80fd\u518d\u6b21\u88ab\u8bbf\u95ee\u3002\u7f13\u5b58\u5229\u7528\u8fd9\u4e00\u539f\u7406\uff0c\u901a\u8fc7\u4fdd\u7559\u6700\u8fd1\u8bbf\u95ee\u8fc7\u7684\u6570\u636e\u6765\u63d0\u9ad8\u547d\u4e2d\u7387\u3002

        \u5b9e\u9645\u4e0a\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u5bf9\u7f13\u5b58\u7684\u5229\u7528\u6548\u7387\u662f\u4e0d\u540c\u7684\uff0c\u4e3b\u8981\u4f53\u73b0\u5728\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u3002

        • \u5360\u7528\u7a7a\u95f4\uff1a\u94fe\u8868\u5143\u7d20\u6bd4\u6570\u7ec4\u5143\u7d20\u5360\u7528\u7a7a\u95f4\u66f4\u591a\uff0c\u5bfc\u81f4\u7f13\u5b58\u4e2d\u5bb9\u7eb3\u7684\u6709\u6548\u6570\u636e\u91cf\u66f4\u5c11\u3002
        • \u7f13\u5b58\u884c\uff1a\u94fe\u8868\u6570\u636e\u5206\u6563\u5728\u5185\u5b58\u5404\u5904\uff0c\u800c\u7f13\u5b58\u662f\u201c\u6309\u884c\u52a0\u8f7d\u201d\u7684\uff0c\u56e0\u6b64\u52a0\u8f7d\u5230\u65e0\u6548\u6570\u636e\u7684\u6bd4\u4f8b\u66f4\u9ad8\u3002
        • \u9884\u53d6\u673a\u5236\uff1a\u6570\u7ec4\u6bd4\u94fe\u8868\u7684\u6570\u636e\u8bbf\u95ee\u6a21\u5f0f\u66f4\u5177\u201c\u53ef\u9884\u6d4b\u6027\u201d\uff0c\u5373\u7cfb\u7edf\u66f4\u5bb9\u6613\u731c\u51fa\u5373\u5c06\u88ab\u52a0\u8f7d\u7684\u6570\u636e\u3002
        • \u7a7a\u95f4\u5c40\u90e8\u6027\uff1a\u6570\u7ec4\u88ab\u5b58\u50a8\u5728\u96c6\u4e2d\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\uff0c\u56e0\u6b64\u88ab\u52a0\u8f7d\u6570\u636e\u9644\u8fd1\u7684\u6570\u636e\u66f4\u6709\u53ef\u80fd\u5373\u5c06\u88ab\u8bbf\u95ee\u3002

        \u603b\u4f53\u800c\u8a00\uff0c\u6570\u7ec4\u5177\u6709\u66f4\u9ad8\u7684\u7f13\u5b58\u547d\u4e2d\u7387\uff0c\u56e0\u6b64\u5b83\u5728\u64cd\u4f5c\u6548\u7387\u4e0a\u901a\u5e38\u4f18\u4e8e\u94fe\u8868\u3002\u8fd9\u4f7f\u5f97\u5728\u89e3\u51b3\u7b97\u6cd5\u95ee\u9898\u65f6\uff0c\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u5f80\u5f80\u66f4\u53d7\u6b22\u8fce\u3002

        \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u9ad8\u7f13\u5b58\u6548\u7387\u5e76\u4e0d\u610f\u5473\u7740\u6570\u7ec4\u5728\u6240\u6709\u60c5\u51b5\u4e0b\u90fd\u4f18\u4e8e\u94fe\u8868\u3002\u5b9e\u9645\u5e94\u7528\u4e2d\u9009\u62e9\u54ea\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u6765\u51b3\u5b9a\u3002\u4f8b\u5982\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u90fd\u53ef\u4ee5\u5b9e\u73b0\u201c\u6808\u201d\u6570\u636e\u7ed3\u6784\uff08\u4e0b\u4e00\u7ae0\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\uff09\uff0c\u4f46\u5b83\u4eec\u9002\u7528\u4e8e\u4e0d\u540c\u573a\u666f\u3002

        • \u5728\u505a\u7b97\u6cd5\u9898\u65f6\uff0c\u6211\u4eec\u4f1a\u503e\u5411\u4e8e\u9009\u62e9\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u64cd\u4f5c\u6548\u7387\u548c\u968f\u673a\u8bbf\u95ee\u7684\u80fd\u529b\uff0c\u4ee3\u4ef7\u4ec5\u662f\u9700\u8981\u9884\u5148\u4e3a\u6570\u7ec4\u5206\u914d\u4e00\u5b9a\u7684\u5185\u5b58\u7a7a\u95f4\u3002
        • \u5982\u679c\u6570\u636e\u91cf\u975e\u5e38\u5927\u3001\u52a8\u6001\u6027\u5f88\u9ad8\u3001\u6808\u7684\u9884\u671f\u5927\u5c0f\u96be\u4ee5\u4f30\u8ba1\uff0c\u90a3\u4e48\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\u66f4\u52a0\u5408\u9002\u3002\u94fe\u8868\u80fd\u591f\u5c06\u5927\u91cf\u6570\u636e\u5206\u6563\u5b58\u50a8\u4e8e\u5185\u5b58\u7684\u4e0d\u540c\u90e8\u5206\uff0c\u5e76\u4e14\u907f\u514d\u4e86\u6570\u7ec4\u6269\u5bb9\u4ea7\u751f\u7684\u989d\u5916\u5f00\u9500\u3002
        "},{"location":"chapter_array_and_linkedlist/summary/","title":"4.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_array_and_linkedlist/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u6570\u7ec4\u548c\u94fe\u8868\u662f\u4e24\u79cd\u57fa\u672c\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5206\u522b\u4ee3\u8868\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u4e24\u79cd\u5b58\u50a8\u65b9\u5f0f\uff1a\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u548c\u5206\u6563\u7a7a\u95f4\u5b58\u50a8\u3002\u4e24\u8005\u7684\u7279\u70b9\u5448\u73b0\u51fa\u4e92\u8865\u7684\u7279\u6027\u3002
        • \u6570\u7ec4\u652f\u6301\u968f\u673a\u8bbf\u95ee\u3001\u5360\u7528\u5185\u5b58\u8f83\u5c11\uff1b\u4f46\u63d2\u5165\u548c\u5220\u9664\u5143\u7d20\u6548\u7387\u4f4e\uff0c\u4e14\u521d\u59cb\u5316\u540e\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002
        • \u94fe\u8868\u901a\u8fc7\u66f4\u6539\u5f15\u7528\uff08\u6307\u9488\uff09\u5b9e\u73b0\u9ad8\u6548\u7684\u8282\u70b9\u63d2\u5165\u4e0e\u5220\u9664\uff0c\u4e14\u53ef\u4ee5\u7075\u6d3b\u8c03\u6574\u957f\u5ea6\uff1b\u4f46\u8282\u70b9\u8bbf\u95ee\u6548\u7387\u4f4e\u3001\u5360\u7528\u5185\u5b58\u8f83\u591a\u3002\u5e38\u89c1\u7684\u94fe\u8868\u7c7b\u578b\u5305\u62ec\u5355\u5411\u94fe\u8868\u3001\u73af\u5f62\u94fe\u8868\u3001\u53cc\u5411\u94fe\u8868\u3002
        • \u5217\u8868\u662f\u4e00\u79cd\u652f\u6301\u589e\u5220\u67e5\u6539\u7684\u5143\u7d20\u6709\u5e8f\u96c6\u5408\uff0c\u901a\u5e38\u57fa\u4e8e\u52a8\u6001\u6570\u7ec4\u5b9e\u73b0\u3002\u5b83\u4fdd\u7559\u4e86\u6570\u7ec4\u7684\u4f18\u52bf\uff0c\u540c\u65f6\u53ef\u4ee5\u7075\u6d3b\u8c03\u6574\u957f\u5ea6\u3002
        • \u5217\u8868\u7684\u51fa\u73b0\u5927\u5e45\u63d0\u9ad8\u4e86\u6570\u7ec4\u7684\u5b9e\u7528\u6027\uff0c\u4f46\u53ef\u80fd\u5bfc\u81f4\u90e8\u5206\u5185\u5b58\u7a7a\u95f4\u6d6a\u8d39\u3002
        • \u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u6570\u636e\u4e3b\u8981\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u3002\u6570\u7ec4\u53ef\u63d0\u4f9b\u66f4\u9ad8\u7684\u5185\u5b58\u7a7a\u95f4\u6548\u7387\uff0c\u800c\u94fe\u8868\u5219\u5728\u5185\u5b58\u4f7f\u7528\u4e0a\u66f4\u52a0\u7075\u6d3b\u3002
        • \u7f13\u5b58\u901a\u8fc7\u7f13\u5b58\u884c\u3001\u9884\u53d6\u673a\u5236\u4ee5\u53ca\u7a7a\u95f4\u5c40\u90e8\u6027\u548c\u65f6\u95f4\u5c40\u90e8\u6027\u7b49\u6570\u636e\u52a0\u8f7d\u673a\u5236\uff0c\u4e3a CPU \u63d0\u4f9b\u5feb\u901f\u6570\u636e\u8bbf\u95ee\uff0c\u663e\u8457\u63d0\u5347\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002
        • \u7531\u4e8e\u6570\u7ec4\u5177\u6709\u66f4\u9ad8\u7684\u7f13\u5b58\u547d\u4e2d\u7387\uff0c\u56e0\u6b64\u5b83\u901a\u5e38\u6bd4\u94fe\u8868\u66f4\u9ad8\u6548\u3002\u5728\u9009\u62e9\u6570\u636e\u7ed3\u6784\u65f6\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u573a\u666f\u505a\u51fa\u6070\u5f53\u9009\u62e9\u3002
        "},{"location":"chapter_array_and_linkedlist/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u6570\u7ec4\u5b58\u50a8\u5728\u6808\u4e0a\u548c\u5b58\u50a8\u5728\u5806\u4e0a\uff0c\u5bf9\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u662f\u5426\u6709\u5f71\u54cd\uff1f

        \u5b58\u50a8\u5728\u6808\u4e0a\u548c\u5806\u4e0a\u7684\u6570\u7ec4\u90fd\u88ab\u5b58\u50a8\u5728\u8fde\u7eed\u5185\u5b58\u7a7a\u95f4\u5185\uff0c\u6570\u636e\u64cd\u4f5c\u6548\u7387\u57fa\u672c\u4e00\u81f4\u3002\u7136\u800c\uff0c\u6808\u548c\u5806\u5177\u6709\u5404\u81ea\u7684\u7279\u70b9\uff0c\u4ece\u800c\u5bfc\u81f4\u4ee5\u4e0b\u4e0d\u540c\u70b9\u3002

        1. \u5206\u914d\u548c\u91ca\u653e\u6548\u7387\uff1a\u6808\u662f\u4e00\u5757\u8f83\u5c0f\u7684\u5185\u5b58\uff0c\u5206\u914d\u7531\u7f16\u8bd1\u5668\u81ea\u52a8\u5b8c\u6210\uff1b\u800c\u5806\u5185\u5b58\u76f8\u5bf9\u66f4\u5927\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u52a8\u6001\u5206\u914d\uff0c\u66f4\u5bb9\u6613\u788e\u7247\u5316\u3002\u56e0\u6b64\uff0c\u5806\u4e0a\u7684\u5206\u914d\u548c\u91ca\u653e\u64cd\u4f5c\u901a\u5e38\u6bd4\u6808\u4e0a\u7684\u6162\u3002
        2. \u5927\u5c0f\u9650\u5236\uff1a\u6808\u5185\u5b58\u76f8\u5bf9\u8f83\u5c0f\uff0c\u5806\u7684\u5927\u5c0f\u4e00\u822c\u53d7\u9650\u4e8e\u53ef\u7528\u5185\u5b58\u3002\u56e0\u6b64\u5806\u66f4\u52a0\u9002\u5408\u5b58\u50a8\u5927\u578b\u6570\u7ec4\u3002
        3. \u7075\u6d3b\u6027\uff1a\u6808\u4e0a\u7684\u6570\u7ec4\u7684\u5927\u5c0f\u9700\u8981\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\uff0c\u800c\u5806\u4e0a\u7684\u6570\u7ec4\u7684\u5927\u5c0f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u786e\u5b9a\u3002

        Q\uff1a\u4e3a\u4ec0\u4e48\u6570\u7ec4\u8981\u6c42\u76f8\u540c\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u800c\u5728\u94fe\u8868\u4e2d\u5374\u6ca1\u6709\u5f3a\u8c03\u76f8\u540c\u7c7b\u578b\u5462\uff1f

        \u94fe\u8868\u7531\u8282\u70b9\u7ec4\u6210\uff0c\u8282\u70b9\u4e4b\u95f4\u901a\u8fc7\u5f15\u7528\uff08\u6307\u9488\uff09\u8fde\u63a5\uff0c\u5404\u4e2a\u8282\u70b9\u53ef\u4ee5\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u4f8b\u5982 int\u3001double\u3001string\u3001object \u7b49\u3002

        \u76f8\u5bf9\u5730\uff0c\u6570\u7ec4\u5143\u7d20\u5219\u5fc5\u987b\u662f\u76f8\u540c\u7c7b\u578b\u7684\uff0c\u8fd9\u6837\u624d\u80fd\u901a\u8fc7\u8ba1\u7b97\u504f\u79fb\u91cf\u6765\u83b7\u53d6\u5bf9\u5e94\u5143\u7d20\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c\u6570\u7ec4\u540c\u65f6\u5305\u542b int \u548c long \u4e24\u79cd\u7c7b\u578b\uff0c\u5355\u4e2a\u5143\u7d20\u5206\u522b\u5360\u7528 4 \u5b57\u8282\u548c 8 \u5b57\u8282 \uff0c\u6b64\u65f6\u5c31\u4e0d\u80fd\u7528\u4ee5\u4e0b\u516c\u5f0f\u8ba1\u7b97\u504f\u79fb\u91cf\u4e86\uff0c\u56e0\u4e3a\u6570\u7ec4\u4e2d\u5305\u542b\u4e86\u4e24\u79cd\u201c\u5143\u7d20\u957f\u5ea6\u201d\u3002

        # \u5143\u7d20\u5185\u5b58\u5730\u5740 = \u6570\u7ec4\u5185\u5b58\u5730\u5740\uff08\u9996\u5143\u7d20\u5185\u5b58\u5730\u5740\uff09 + \u5143\u7d20\u957f\u5ea6 * \u5143\u7d20\u7d22\u5f15\n

        Q\uff1a\u5220\u9664\u8282\u70b9 P \u540e\uff0c\u662f\u5426\u9700\u8981\u628a P.next \u8bbe\u4e3a None \u5462\uff1f

        \u4e0d\u4fee\u6539 P.next \u4e5f\u53ef\u4ee5\u3002\u4ece\u8be5\u94fe\u8868\u7684\u89d2\u5ea6\u770b\uff0c\u4ece\u5934\u8282\u70b9\u904d\u5386\u5230\u5c3e\u8282\u70b9\u5df2\u7ecf\u4e0d\u4f1a\u9047\u5230 P \u4e86\u3002\u8fd9\u610f\u5473\u7740\u8282\u70b9 P \u5df2\u7ecf\u4ece\u94fe\u8868\u4e2d\u5220\u9664\u4e86\uff0c\u6b64\u65f6\u8282\u70b9 P \u6307\u5411\u54ea\u91cc\u90fd\u4e0d\u4f1a\u5bf9\u8be5\u94fe\u8868\u4ea7\u751f\u5f71\u54cd\u3002

        \u4ece\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\uff08\u505a\u9898\uff09\u7684\u89d2\u5ea6\u770b\uff0c\u4e0d\u65ad\u5f00\u6ca1\u6709\u5173\u7cfb\uff0c\u53ea\u8981\u4fdd\u8bc1\u7a0b\u5e8f\u7684\u903b\u8f91\u662f\u6b63\u786e\u7684\u5c31\u884c\u3002\u4ece\u6807\u51c6\u5e93\u7684\u89d2\u5ea6\u770b\uff0c\u65ad\u5f00\u66f4\u52a0\u5b89\u5168\u3001\u903b\u8f91\u66f4\u52a0\u6e05\u6670\u3002\u5982\u679c\u4e0d\u65ad\u5f00\uff0c\u5047\u8bbe\u88ab\u5220\u9664\u8282\u70b9\u672a\u88ab\u6b63\u5e38\u56de\u6536\uff0c\u90a3\u4e48\u5b83\u4f1a\u5f71\u54cd\u540e\u7ee7\u8282\u70b9\u7684\u5185\u5b58\u56de\u6536\u3002

        Q\uff1a\u5728\u94fe\u8868\u4e2d\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u3002\u4f46\u662f\u589e\u5220\u4e4b\u524d\u90fd\u9700\u8981 \\(O(n)\\) \u7684\u65f6\u95f4\u67e5\u627e\u5143\u7d20\uff0c\u90a3\u4e3a\u4ec0\u4e48\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u662f \\(O(n)\\) \u5462\uff1f

        \u5982\u679c\u662f\u5148\u67e5\u627e\u5143\u7d20\u3001\u518d\u5220\u9664\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u786e\u5b9e\u662f \\(O(n)\\) \u3002\u7136\u800c\uff0c\u94fe\u8868\u7684 \\(O(1)\\) \u589e\u5220\u7684\u4f18\u52bf\u53ef\u4ee5\u5728\u5176\u4ed6\u5e94\u7528\u4e0a\u5f97\u5230\u4f53\u73b0\u3002\u4f8b\u5982\uff0c\u53cc\u5411\u961f\u5217\u9002\u5408\u4f7f\u7528\u94fe\u8868\u5b9e\u73b0\uff0c\u6211\u4eec\u7ef4\u62a4\u4e00\u4e2a\u6307\u9488\u53d8\u91cf\u59cb\u7ec8\u6307\u5411\u5934\u8282\u70b9\u3001\u5c3e\u8282\u70b9\uff0c\u6bcf\u6b21\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u90fd\u662f \\(O(1)\\) \u3002

        Q\uff1a\u56fe\u201c\u94fe\u8868\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f\u201d\u4e2d\uff0c\u6d45\u84dd\u8272\u7684\u5b58\u50a8\u8282\u70b9\u6307\u9488\u662f\u5360\u7528\u4e00\u5757\u5185\u5b58\u5730\u5740\u5417\uff1f\u8fd8\u662f\u548c\u8282\u70b9\u503c\u5404\u5360\u4e00\u534a\u5462\uff1f

        \u8be5\u793a\u610f\u56fe\u53ea\u662f\u5b9a\u6027\u8868\u793a\uff0c\u5b9a\u91cf\u8868\u793a\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u5206\u6790\u3002

        • \u4e0d\u540c\u7c7b\u578b\u7684\u8282\u70b9\u503c\u5360\u7528\u7684\u7a7a\u95f4\u662f\u4e0d\u540c\u7684\uff0c\u6bd4\u5982 int\u3001long\u3001double \u548c\u5b9e\u4f8b\u5bf9\u8c61\u7b49\u3002
        • \u6307\u9488\u53d8\u91cf\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u5927\u5c0f\u6839\u636e\u6240\u4f7f\u7528\u7684\u64cd\u4f5c\u7cfb\u7edf\u53ca\u7f16\u8bd1\u73af\u5883\u800c\u5b9a\uff0c\u5927\u591a\u4e3a 8 \u5b57\u8282\u6216 4 \u5b57\u8282\u3002

        Q\uff1a\u5728\u5217\u8868\u672b\u5c3e\u6dfb\u52a0\u5143\u7d20\u662f\u5426\u65f6\u65f6\u523b\u523b\u90fd\u4e3a \\(O(1)\\) \uff1f

        \u5982\u679c\u6dfb\u52a0\u5143\u7d20\u65f6\u8d85\u51fa\u5217\u8868\u957f\u5ea6\uff0c\u5219\u9700\u8981\u5148\u6269\u5bb9\u5217\u8868\u518d\u6dfb\u52a0\u3002\u7cfb\u7edf\u4f1a\u7533\u8bf7\u4e00\u5757\u65b0\u7684\u5185\u5b58\uff0c\u5e76\u5c06\u539f\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\u642c\u8fd0\u8fc7\u53bb\uff0c\u8fd9\u65f6\u5019\u65f6\u95f4\u590d\u6742\u5ea6\u5c31\u4f1a\u662f \\(O(n)\\) \u3002

        Q\uff1a\u201c\u5217\u8868\u7684\u51fa\u73b0\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u6570\u7ec4\u7684\u5b9e\u7528\u6027\uff0c\u4f46\u53ef\u80fd\u5bfc\u81f4\u90e8\u5206\u5185\u5b58\u7a7a\u95f4\u6d6a\u8d39\u201d\uff0c\u8fd9\u91cc\u7684\u7a7a\u95f4\u6d6a\u8d39\u662f\u6307\u989d\u5916\u589e\u52a0\u7684\u53d8\u91cf\u5982\u5bb9\u91cf\u3001\u957f\u5ea6\u3001\u6269\u5bb9\u500d\u6570\u6240\u5360\u7684\u5185\u5b58\u5417\uff1f

        \u8fd9\u91cc\u7684\u7a7a\u95f4\u6d6a\u8d39\u4e3b\u8981\u6709\u4e24\u65b9\u9762\u542b\u4e49\uff1a\u4e00\u65b9\u9762\uff0c\u5217\u8868\u90fd\u4f1a\u8bbe\u5b9a\u4e00\u4e2a\u521d\u59cb\u957f\u5ea6\uff0c\u6211\u4eec\u4e0d\u4e00\u5b9a\u9700\u8981\u7528\u8fd9\u4e48\u591a\uff1b\u53e6\u4e00\u65b9\u9762\uff0c\u4e3a\u4e86\u9632\u6b62\u9891\u7e41\u6269\u5bb9\uff0c\u6269\u5bb9\u4e00\u822c\u4f1a\u4e58\u4ee5\u4e00\u4e2a\u7cfb\u6570\uff0c\u6bd4\u5982 \\(\\times 1.5\\) \u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4e5f\u4f1a\u51fa\u73b0\u5f88\u591a\u7a7a\u4f4d\uff0c\u6211\u4eec\u901a\u5e38\u4e0d\u80fd\u5b8c\u5168\u586b\u6ee1\u5b83\u4eec\u3002

        Q\uff1a\u5728 Python \u4e2d\u521d\u59cb\u5316 n = [1, 2, 3] \u540e\uff0c\u8fd9 3 \u4e2a\u5143\u7d20\u7684\u5730\u5740\u662f\u76f8\u8fde\u7684\uff0c\u4f46\u662f\u521d\u59cb\u5316 m = [2, 1, 3] \u4f1a\u53d1\u73b0\u5b83\u4eec\u6bcf\u4e2a\u5143\u7d20\u7684 id \u5e76\u4e0d\u662f\u8fde\u7eed\u7684\uff0c\u800c\u662f\u5206\u522b\u8ddf n \u4e2d\u7684\u76f8\u540c\u3002\u8fd9\u4e9b\u5143\u7d20\u7684\u5730\u5740\u4e0d\u8fde\u7eed\uff0c\u90a3\u4e48 m \u8fd8\u662f\u6570\u7ec4\u5417\uff1f

        \u5047\u5982\u628a\u5217\u8868\u5143\u7d20\u6362\u6210\u94fe\u8868\u8282\u70b9 n = [n1, n2, n3, n4, n5] \uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u8fd9 5 \u4e2a\u8282\u70b9\u5bf9\u8c61\u4e5f\u5206\u6563\u5b58\u50a8\u5728\u5185\u5b58\u5404\u5904\u3002\u7136\u800c\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u5217\u8868\u7d22\u5f15\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u83b7\u53d6\u8282\u70b9\u5185\u5b58\u5730\u5740\uff0c\u4ece\u800c\u8bbf\u95ee\u5230\u5bf9\u5e94\u7684\u8282\u70b9\u3002\u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u4e2d\u5b58\u50a8\u7684\u662f\u8282\u70b9\u7684\u5f15\u7528\uff0c\u800c\u975e\u8282\u70b9\u672c\u8eab\u3002

        \u4e0e\u8bb8\u591a\u8bed\u8a00\u4e0d\u540c\uff0cPython \u4e2d\u7684\u6570\u5b57\u4e5f\u88ab\u5305\u88c5\u4e3a\u5bf9\u8c61\uff0c\u5217\u8868\u4e2d\u5b58\u50a8\u7684\u4e0d\u662f\u6570\u5b57\u672c\u8eab\uff0c\u800c\u662f\u5bf9\u6570\u5b57\u7684\u5f15\u7528\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4f1a\u53d1\u73b0\u4e24\u4e2a\u6570\u7ec4\u4e2d\u7684\u76f8\u540c\u6570\u5b57\u62e5\u6709\u540c\u4e00\u4e2a id \uff0c\u5e76\u4e14\u8fd9\u4e9b\u6570\u5b57\u7684\u5185\u5b58\u5730\u5740\u65e0\u987b\u8fde\u7eed\u3002

        Q\uff1aC++ STL \u91cc\u9762\u7684 std::list \u5df2\u7ecf\u5b9e\u73b0\u4e86\u53cc\u5411\u94fe\u8868\uff0c\u4f46\u597d\u50cf\u4e00\u4e9b\u7b97\u6cd5\u4e66\u4e0a\u4e0d\u600e\u4e48\u76f4\u63a5\u4f7f\u7528\u5b83\uff0c\u662f\u4e0d\u662f\u56e0\u4e3a\u6709\u4ec0\u4e48\u5c40\u9650\u6027\u5462\uff1f

        \u4e00\u65b9\u9762\uff0c\u6211\u4eec\u5f80\u5f80\u66f4\u9752\u7750\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u7b97\u6cd5\uff0c\u800c\u53ea\u5728\u5fc5\u8981\u65f6\u624d\u4f7f\u7528\u94fe\u8868\uff0c\u4e3b\u8981\u6709\u4e24\u4e2a\u539f\u56e0\u3002

        • \u7a7a\u95f4\u5f00\u9500\uff1a\u7531\u4e8e\u6bcf\u4e2a\u5143\u7d20\u9700\u8981\u4e24\u4e2a\u989d\u5916\u7684\u6307\u9488\uff08\u4e00\u4e2a\u7528\u4e8e\u524d\u4e00\u4e2a\u5143\u7d20\uff0c\u4e00\u4e2a\u7528\u4e8e\u540e\u4e00\u4e2a\u5143\u7d20\uff09\uff0c\u6240\u4ee5 std::list \u901a\u5e38\u6bd4 std::vector \u66f4\u5360\u7528\u7a7a\u95f4\u3002
        • \u7f13\u5b58\u4e0d\u53cb\u597d\uff1a\u7531\u4e8e\u6570\u636e\u4e0d\u662f\u8fde\u7eed\u5b58\u653e\u7684\uff0c\u56e0\u6b64 std::list \u5bf9\u7f13\u5b58\u7684\u5229\u7528\u7387\u8f83\u4f4e\u3002\u4e00\u822c\u60c5\u51b5\u4e0b\uff0cstd::vector \u7684\u6027\u80fd\u4f1a\u66f4\u597d\u3002

        \u53e6\u4e00\u65b9\u9762\uff0c\u5fc5\u8981\u4f7f\u7528\u94fe\u8868\u7684\u60c5\u51b5\u4e3b\u8981\u662f\u4e8c\u53c9\u6811\u548c\u56fe\u3002\u6808\u548c\u961f\u5217\u5f80\u5f80\u4f1a\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u7684 stack \u548c queue \uff0c\u800c\u975e\u94fe\u8868\u3002

        Q\uff1a\u64cd\u4f5c res = [[0]] * n \u751f\u6210\u4e86\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\uff0c\u5176\u4e2d\u6bcf\u4e00\u4e2a [0] \u90fd\u662f\u72ec\u7acb\u7684\u5417\uff1f

        \u4e0d\u662f\u72ec\u7acb\u7684\u3002\u6b64\u4e8c\u7ef4\u5217\u8868\u4e2d\uff0c\u6240\u6709\u7684 [0] \u5b9e\u9645\u4e0a\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u5982\u679c\u6211\u4eec\u4fee\u6539\u5176\u4e2d\u4e00\u4e2a\u5143\u7d20\uff0c\u4f1a\u53d1\u73b0\u6240\u6709\u7684\u5bf9\u5e94\u5143\u7d20\u90fd\u4f1a\u968f\u4e4b\u6539\u53d8\u3002

        \u5982\u679c\u5e0c\u671b\u4e8c\u7ef4\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a [0] \u90fd\u662f\u72ec\u7acb\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528 res = [[0] for _ in range(n)] \u6765\u5b9e\u73b0\u3002\u8fd9\u79cd\u65b9\u5f0f\u7684\u539f\u7406\u662f\u521d\u59cb\u5316\u4e86 \\(n\\) \u4e2a\u72ec\u7acb\u7684 [0] \u5217\u8868\u5bf9\u8c61\u3002

        Q\uff1a\u64cd\u4f5c res = [0] * n \u751f\u6210\u4e86\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u6bcf\u4e00\u4e2a\u6574\u6570 0 \u90fd\u662f\u72ec\u7acb\u7684\u5417\uff1f

        \u5728\u8be5\u5217\u8868\u4e2d\uff0c\u6240\u6709\u6574\u6570 0 \u90fd\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u8fd9\u662f\u56e0\u4e3a Python \u5bf9\u5c0f\u6574\u6570\uff08\u901a\u5e38\u662f -5 \u5230 256\uff09\u91c7\u7528\u4e86\u7f13\u5b58\u6c60\u673a\u5236\uff0c\u4ee5\u4fbf\u6700\u5927\u5316\u5bf9\u8c61\u590d\u7528\uff0c\u4ece\u800c\u63d0\u5347\u6027\u80fd\u3002

        \u867d\u7136\u5b83\u4eec\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4f46\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u72ec\u7acb\u4fee\u6539\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u8fd9\u662f\u56e0\u4e3a Python \u7684\u6574\u6570\u662f\u201c\u4e0d\u53ef\u53d8\u5bf9\u8c61\u201d\u3002\u5f53\u6211\u4eec\u4fee\u6539\u67d0\u4e2a\u5143\u7d20\u65f6\uff0c\u5b9e\u9645\u4e0a\u662f\u5207\u6362\u4e3a\u53e6\u4e00\u4e2a\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u800c\u4e0d\u662f\u6539\u53d8\u539f\u6709\u5bf9\u8c61\u672c\u8eab\u3002

        \u7136\u800c\uff0c\u5f53\u5217\u8868\u5143\u7d20\u662f\u201c\u53ef\u53d8\u5bf9\u8c61\u201d\u65f6\uff08\u4f8b\u5982\u5217\u8868\u3001\u5b57\u5178\u6216\u7c7b\u5b9e\u4f8b\u7b49\uff09\uff0c\u4fee\u6539\u67d0\u4e2a\u5143\u7d20\u4f1a\u76f4\u63a5\u6539\u53d8\u8be5\u5bf9\u8c61\u672c\u8eab\uff0c\u6240\u6709\u5f15\u7528\u8be5\u5bf9\u8c61\u7684\u5143\u7d20\u90fd\u4f1a\u4ea7\u751f\u76f8\u540c\u53d8\u5316\u3002

        "},{"location":"chapter_backtracking/","title":"\u7b2c 13 \u7ae0 \u00a0 \u56de\u6eaf","text":"

        Abstract

        \u6211\u4eec\u5982\u540c\u8ff7\u5bab\u4e2d\u7684\u63a2\u7d22\u8005\uff0c\u5728\u524d\u8fdb\u7684\u9053\u8def\u4e0a\u53ef\u80fd\u4f1a\u9047\u5230\u56f0\u96be\u3002

        \u56de\u6eaf\u7684\u529b\u91cf\u8ba9\u6211\u4eec\u80fd\u591f\u91cd\u65b0\u5f00\u59cb\uff0c\u4e0d\u65ad\u5c1d\u8bd5\uff0c\u6700\u7ec8\u627e\u5230\u901a\u5f80\u5149\u660e\u7684\u51fa\u53e3\u3002

        "},{"location":"chapter_backtracking/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 13.1 \u00a0 \u56de\u6eaf\u7b97\u6cd5
        • 13.2 \u00a0 \u5168\u6392\u5217\u95ee\u9898
        • 13.3 \u00a0 \u5b50\u96c6\u548c\u95ee\u9898
        • 13.4 \u00a0 N \u7687\u540e\u95ee\u9898
        • 13.5 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_backtracking/backtracking_algorithm/","title":"13.1 \u00a0 \u56de\u6eaf\u7b97\u6cd5","text":"

        \u56de\u6eaf\u7b97\u6cd5\uff08backtracking algorithm\uff09\u662f\u4e00\u79cd\u901a\u8fc7\u7a77\u4e3e\u6765\u89e3\u51b3\u95ee\u9898\u7684\u65b9\u6cd5\uff0c\u5b83\u7684\u6838\u5fc3\u601d\u60f3\u662f\u4ece\u4e00\u4e2a\u521d\u59cb\u72b6\u6001\u51fa\u53d1\uff0c\u66b4\u529b\u641c\u7d22\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5f53\u9047\u5230\u6b63\u786e\u7684\u89e3\u5219\u5c06\u5176\u8bb0\u5f55\uff0c\u76f4\u5230\u627e\u5230\u89e3\u6216\u8005\u5c1d\u8bd5\u4e86\u6240\u6709\u53ef\u80fd\u7684\u9009\u62e9\u90fd\u65e0\u6cd5\u627e\u5230\u89e3\u4e3a\u6b62\u3002

        \u56de\u6eaf\u7b97\u6cd5\u901a\u5e38\u91c7\u7528\u201c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u201d\u6765\u904d\u5386\u89e3\u7a7a\u95f4\u3002\u5728\u201c\u4e8c\u53c9\u6811\u201d\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u63d0\u5230\u524d\u5e8f\u3001\u4e2d\u5e8f\u548c\u540e\u5e8f\u904d\u5386\u90fd\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5229\u7528\u524d\u5e8f\u904d\u5386\u6784\u9020\u4e00\u4e2a\u56de\u6eaf\u95ee\u9898\uff0c\u9010\u6b65\u4e86\u89e3\u56de\u6eaf\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u3002

        \u4f8b\u9898\u4e00

        \u7ed9\u5b9a\u4e00\u68f5\u4e8c\u53c9\u6811\uff0c\u641c\u7d22\u5e76\u8bb0\u5f55\u6240\u6709\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\uff0c\u8bf7\u8fd4\u56de\u8282\u70b9\u5217\u8868\u3002

        \u5bf9\u4e8e\u6b64\u9898\uff0c\u6211\u4eec\u524d\u5e8f\u904d\u5386\u8fd9\u68f5\u6811\uff0c\u5e76\u5224\u65ad\u5f53\u524d\u8282\u70b9\u7684\u503c\u662f\u5426\u4e3a \\(7\\) \uff0c\u82e5\u662f\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u7684\u503c\u52a0\u5165\u7ed3\u679c\u5217\u8868 res \u4e4b\u4e2d\u3002\u76f8\u5173\u8fc7\u7a0b\u5b9e\u73b0\u5982\u56fe 13-1 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_i_compact.py
        def pre_order(root: TreeNode):\n    \"\"\"\u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00\"\"\"\n    if root is None:\n        return\n    if root.val == 7:\n        # \u8bb0\u5f55\u89e3\n        res.append(root)\n    pre_order(root.left)\n    pre_order(root.right)\n
        preorder_traversal_i_compact.cpp
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode *root) {\n    if (root == nullptr) {\n        return;\n    }\n    if (root->val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res.push_back(root);\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n}\n
        preorder_traversal_i_compact.java
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode root) {\n    if (root == null) {\n        return;\n    }\n    if (root.val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res.add(root);\n    }\n    preOrder(root.left);\n    preOrder(root.right);\n}\n
        preorder_traversal_i_compact.cs
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid PreOrder(TreeNode? root) {\n    if (root == null) {\n        return;\n    }\n    if (root.val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res.Add(root);\n    }\n    PreOrder(root.left);\n    PreOrder(root.right);\n}\n
        preorder_traversal_i_compact.go
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunc preOrderI(root *TreeNode, res *[]*TreeNode) {\n    if root == nil {\n        return\n    }\n    if (root.Val).(int) == 7 {\n        // \u8bb0\u5f55\u89e3\n        *res = append(*res, root)\n    }\n    preOrderI(root.Left, res)\n    preOrderI(root.Right, res)\n}\n
        preorder_traversal_i_compact.swift
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunc preOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    if root.val == 7 {\n        // \u8bb0\u5f55\u89e3\n        res.append(root)\n    }\n    preOrder(root: root.left)\n    preOrder(root: root.right)\n}\n
        preorder_traversal_i_compact.js
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunction preOrder(root, res) {\n    if (root === null) {\n        return;\n    }\n    if (root.val === 7) {\n        // \u8bb0\u5f55\u89e3\n        res.push(root);\n    }\n    preOrder(root.left, res);\n    preOrder(root.right, res);\n}\n
        preorder_traversal_i_compact.ts
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunction preOrder(root: TreeNode | null, res: TreeNode[]): void {\n    if (root === null) {\n        return;\n    }\n    if (root.val === 7) {\n        // \u8bb0\u5f55\u89e3\n        res.push(root);\n    }\n    preOrder(root.left, res);\n    preOrder(root.right, res);\n}\n
        preorder_traversal_i_compact.dart
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode? root, List<TreeNode> res) {\n  if (root == null) {\n    return;\n  }\n  if (root.val == 7) {\n    // \u8bb0\u5f55\u89e3\n    res.add(root);\n  }\n  preOrder(root.left, res);\n  preOrder(root.right, res);\n}\n
        preorder_traversal_i_compact.rs
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfn pre_order(res: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<&Rc<RefCell<TreeNode>>>) {\n    if root.is_none() {\n        return;\n    }\n    if let Some(node) = root {\n        if node.borrow().val == 7 {\n            // \u8bb0\u5f55\u89e3\n            res.push(node.clone());\n        }\n        pre_order(res, node.borrow().left.as_ref());\n        pre_order(res, node.borrow().right.as_ref());\n    }\n}\n
        preorder_traversal_i_compact.c
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode *root) {\n    if (root == NULL) {\n        return;\n    }\n    if (root->val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res[resSize++] = root;\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n}\n
        preorder_traversal_i_compact.kt
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfun preOrder(root: TreeNode?) {\n    if (root == null) {\n        return\n    }\n    if (root._val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res!!.add(root)\n    }\n    preOrder(root.left)\n    preOrder(root.right)\n}\n
        preorder_traversal_i_compact.rb
        ### \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 ###\ndef pre_order(root)\n  return unless root\n\n  # \u8bb0\u5f55\u89e3\n  $res << root if root.val == 7\n\n  pre_order(root.left)\n  pre_order(root.right)\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 13-1 \u00a0 \u5728\u524d\u5e8f\u904d\u5386\u4e2d\u641c\u7d22\u8282\u70b9

        "},{"location":"chapter_backtracking/backtracking_algorithm/#1311","title":"13.1.1 \u00a0 \u5c1d\u8bd5\u4e0e\u56de\u9000","text":"

        \u4e4b\u6240\u4ee5\u79f0\u4e4b\u4e3a\u56de\u6eaf\u7b97\u6cd5\uff0c\u662f\u56e0\u4e3a\u8be5\u7b97\u6cd5\u5728\u641c\u7d22\u89e3\u7a7a\u95f4\u65f6\u4f1a\u91c7\u7528\u201c\u5c1d\u8bd5\u201d\u4e0e\u201c\u56de\u9000\u201d\u7684\u7b56\u7565\u3002\u5f53\u7b97\u6cd5\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u9047\u5230\u67d0\u4e2a\u72b6\u6001\u65e0\u6cd5\u7ee7\u7eed\u524d\u8fdb\u6216\u65e0\u6cd5\u5f97\u5230\u6ee1\u8db3\u6761\u4ef6\u7684\u89e3\u65f6\uff0c\u5b83\u4f1a\u64a4\u9500\u4e0a\u4e00\u6b65\u7684\u9009\u62e9\uff0c\u9000\u56de\u5230\u4e4b\u524d\u7684\u72b6\u6001\uff0c\u5e76\u5c1d\u8bd5\u5176\u4ed6\u53ef\u80fd\u7684\u9009\u62e9\u3002

        \u5bf9\u4e8e\u4f8b\u9898\u4e00\uff0c\u8bbf\u95ee\u6bcf\u4e2a\u8282\u70b9\u90fd\u4ee3\u8868\u4e00\u6b21\u201c\u5c1d\u8bd5\u201d\uff0c\u800c\u8d8a\u8fc7\u53f6\u8282\u70b9\u6216\u8fd4\u56de\u7236\u8282\u70b9\u7684 return \u5219\u8868\u793a\u201c\u56de\u9000\u201d\u3002

        \u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u56de\u9000\u5e76\u4e0d\u4ec5\u4ec5\u5305\u62ec\u51fd\u6570\u8fd4\u56de\u3002\u4e3a\u89e3\u91ca\u8fd9\u4e00\u70b9\uff0c\u6211\u4eec\u5bf9\u4f8b\u9898\u4e00\u7a0d\u4f5c\u62d3\u5c55\u3002

        \u4f8b\u9898\u4e8c

        \u5728\u4e8c\u53c9\u6811\u4e2d\u641c\u7d22\u6240\u6709\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\uff0c\u8bf7\u8fd4\u56de\u6839\u8282\u70b9\u5230\u8fd9\u4e9b\u8282\u70b9\u7684\u8def\u5f84\u3002

        \u5728\u4f8b\u9898\u4e00\u4ee3\u7801\u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u5217\u8868 path \u8bb0\u5f55\u8bbf\u95ee\u8fc7\u7684\u8282\u70b9\u8def\u5f84\u3002\u5f53\u8bbf\u95ee\u5230\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\u65f6\uff0c\u5219\u590d\u5236 path \u5e76\u6dfb\u52a0\u8fdb\u7ed3\u679c\u5217\u8868 res \u3002\u904d\u5386\u5b8c\u6210\u540e\uff0cres \u4e2d\u4fdd\u5b58\u7684\u5c31\u662f\u6240\u6709\u7684\u89e3\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_ii_compact.py
        def pre_order(root: TreeNode):\n    \"\"\"\u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c\"\"\"\n    if root is None:\n        return\n    # \u5c1d\u8bd5\n    path.append(root)\n    if root.val == 7:\n        # \u8bb0\u5f55\u89e3\n        res.append(list(path))\n    pre_order(root.left)\n    pre_order(root.right)\n    # \u56de\u9000\n    path.pop()\n
        preorder_traversal_ii_compact.cpp
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode *root) {\n    if (root == nullptr) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path.push_back(root);\n    if (root->val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res.push_back(path);\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // \u56de\u9000\n    path.pop_back();\n}\n
        preorder_traversal_ii_compact.java
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode root) {\n    if (root == null) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path.add(root);\n    if (root.val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res.add(new ArrayList<>(path));\n    }\n    preOrder(root.left);\n    preOrder(root.right);\n    // \u56de\u9000\n    path.remove(path.size() - 1);\n}\n
        preorder_traversal_ii_compact.cs
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid PreOrder(TreeNode? root) {\n    if (root == null) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path.Add(root);\n    if (root.val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res.Add(new List<TreeNode>(path));\n    }\n    PreOrder(root.left);\n    PreOrder(root.right);\n    // \u56de\u9000\n    path.RemoveAt(path.Count - 1);\n}\n
        preorder_traversal_ii_compact.go
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunc preOrderII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {\n    if root == nil {\n        return\n    }\n    // \u5c1d\u8bd5\n    *path = append(*path, root)\n    if root.Val.(int) == 7 {\n        // \u8bb0\u5f55\u89e3\n        *res = append(*res, append([]*TreeNode{}, *path...))\n    }\n    preOrderII(root.Left, res, path)\n    preOrderII(root.Right, res, path)\n    // \u56de\u9000\n    *path = (*path)[:len(*path)-1]\n}\n
        preorder_traversal_ii_compact.swift
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunc preOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    // \u5c1d\u8bd5\n    path.append(root)\n    if root.val == 7 {\n        // \u8bb0\u5f55\u89e3\n        res.append(path)\n    }\n    preOrder(root: root.left)\n    preOrder(root: root.right)\n    // \u56de\u9000\n    path.removeLast()\n}\n
        preorder_traversal_ii_compact.js
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunction preOrder(root, path, res) {\n    if (root === null) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path.push(root);\n    if (root.val === 7) {\n        // \u8bb0\u5f55\u89e3\n        res.push([...path]);\n    }\n    preOrder(root.left, path, res);\n    preOrder(root.right, path, res);\n    // \u56de\u9000\n    path.pop();\n}\n
        preorder_traversal_ii_compact.ts
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunction preOrder(\n    root: TreeNode | null,\n    path: TreeNode[],\n    res: TreeNode[][]\n): void {\n    if (root === null) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path.push(root);\n    if (root.val === 7) {\n        // \u8bb0\u5f55\u89e3\n        res.push([...path]);\n    }\n    preOrder(root.left, path, res);\n    preOrder(root.right, path, res);\n    // \u56de\u9000\n    path.pop();\n}\n
        preorder_traversal_ii_compact.dart
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(\n  TreeNode? root,\n  List<TreeNode> path,\n  List<List<TreeNode>> res,\n) {\n  if (root == null) {\n    return;\n  }\n\n  // \u5c1d\u8bd5\n  path.add(root);\n  if (root.val == 7) {\n    // \u8bb0\u5f55\u89e3\n    res.add(List.from(path));\n  }\n  preOrder(root.left, path, res);\n  preOrder(root.right, path, res);\n  // \u56de\u9000\n  path.removeLast();\n}\n
        preorder_traversal_ii_compact.rs
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfn pre_order(\n    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,\n    path: &mut Vec<Rc<RefCell<TreeNode>>>,\n    root: Option<&Rc<RefCell<TreeNode>>>,\n) {\n    if root.is_none() {\n        return;\n    }\n    if let Some(node) = root {\n        // \u5c1d\u8bd5\n        path.push(node.clone());\n        if node.borrow().val == 7 {\n            // \u8bb0\u5f55\u89e3\n            res.push(path.clone());\n        }\n        pre_order(res, path, node.borrow().left.as_ref());\n        pre_order(res, path, node.borrow().right.as_ref());\n        // \u56de\u9000\n        path.pop();\n    }\n}\n
        preorder_traversal_ii_compact.c
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode *root) {\n    if (root == NULL) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path[pathSize++] = root;\n    if (root->val == 7) {\n        // \u8bb0\u5f55\u89e3\n        for (int i = 0; i < pathSize; ++i) {\n            res[resSize][i] = path[i];\n        }\n        resSize++;\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // \u56de\u9000\n    pathSize--;\n}\n
        preorder_traversal_ii_compact.kt
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfun preOrder(root: TreeNode?) {\n    if (root == null) {\n        return\n    }\n    // \u5c1d\u8bd5\n    path!!.add(root)\n    if (root._val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res!!.add(path!!.toMutableList())\n    }\n    preOrder(root.left)\n    preOrder(root.right)\n    // \u56de\u9000\n    path!!.removeAt(path!!.size - 1)\n}\n
        preorder_traversal_ii_compact.rb
        ### \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c ###\ndef pre_order(root)\n  return unless root\n\n  # \u5c1d\u8bd5\n  $path << root\n\n  # \u8bb0\u5f55\u89e3\n  $res << $path.dup if root.val == 7\n\n  pre_order(root.left)\n  pre_order(root.right)\n\n  # \u56de\u9000\n  $path.pop\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5728\u6bcf\u6b21\u201c\u5c1d\u8bd5\u201d\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5c06\u5f53\u524d\u8282\u70b9\u6dfb\u52a0\u8fdb path \u6765\u8bb0\u5f55\u8def\u5f84\uff1b\u800c\u5728\u201c\u56de\u9000\u201d\u524d\uff0c\u6211\u4eec\u9700\u8981\u5c06\u8be5\u8282\u70b9\u4ece path \u4e2d\u5f39\u51fa\uff0c\u4ee5\u6062\u590d\u672c\u6b21\u5c1d\u8bd5\u4e4b\u524d\u7684\u72b6\u6001\u3002

        \u89c2\u5bdf\u56fe 13-2 \u6240\u793a\u7684\u8fc7\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5c1d\u8bd5\u548c\u56de\u9000\u7406\u89e3\u4e3a\u201c\u524d\u8fdb\u201d\u4e0e\u201c\u64a4\u9500\u201d\uff0c\u4e24\u4e2a\u64cd\u4f5c\u4e92\u4e3a\u9006\u5411\u3002

        <1><2><3><4><5><6><7><8><9><10><11>

        \u56fe 13-2 \u00a0 \u5c1d\u8bd5\u4e0e\u56de\u9000

        "},{"location":"chapter_backtracking/backtracking_algorithm/#1312","title":"13.1.2 \u00a0 \u526a\u679d","text":"

        \u590d\u6742\u7684\u56de\u6eaf\u95ee\u9898\u901a\u5e38\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a\u7ea6\u675f\u6761\u4ef6\uff0c\u7ea6\u675f\u6761\u4ef6\u901a\u5e38\u53ef\u7528\u4e8e\u201c\u526a\u679d\u201d\u3002

        \u4f8b\u9898\u4e09

        \u5728\u4e8c\u53c9\u6811\u4e2d\u641c\u7d22\u6240\u6709\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\uff0c\u8bf7\u8fd4\u56de\u6839\u8282\u70b9\u5230\u8fd9\u4e9b\u8282\u70b9\u7684\u8def\u5f84\uff0c\u5e76\u8981\u6c42\u8def\u5f84\u4e2d\u4e0d\u5305\u542b\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u3002

        \u4e3a\u4e86\u6ee1\u8db3\u4ee5\u4e0a\u7ea6\u675f\u6761\u4ef6\uff0c\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u526a\u679d\u64cd\u4f5c\uff1a\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u82e5\u9047\u5230\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\uff0c\u5219\u63d0\u524d\u8fd4\u56de\uff0c\u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_iii_compact.py
        def pre_order(root: TreeNode):\n    \"\"\"\u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09\"\"\"\n    # \u526a\u679d\n    if root is None or root.val == 3:\n        return\n    # \u5c1d\u8bd5\n    path.append(root)\n    if root.val == 7:\n        # \u8bb0\u5f55\u89e3\n        res.append(list(path))\n    pre_order(root.left)\n    pre_order(root.right)\n    # \u56de\u9000\n    path.pop()\n
        preorder_traversal_iii_compact.cpp
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode *root) {\n    // \u526a\u679d\n    if (root == nullptr || root->val == 3) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path.push_back(root);\n    if (root->val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res.push_back(path);\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // \u56de\u9000\n    path.pop_back();\n}\n
        preorder_traversal_iii_compact.java
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode root) {\n    // \u526a\u679d\n    if (root == null || root.val == 3) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path.add(root);\n    if (root.val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res.add(new ArrayList<>(path));\n    }\n    preOrder(root.left);\n    preOrder(root.right);\n    // \u56de\u9000\n    path.remove(path.size() - 1);\n}\n
        preorder_traversal_iii_compact.cs
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid PreOrder(TreeNode? root) {\n    // \u526a\u679d\n    if (root == null || root.val == 3) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path.Add(root);\n    if (root.val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res.Add(new List<TreeNode>(path));\n    }\n    PreOrder(root.left);\n    PreOrder(root.right);\n    // \u56de\u9000\n    path.RemoveAt(path.Count - 1);\n}\n
        preorder_traversal_iii_compact.go
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunc preOrderIII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {\n    // \u526a\u679d\n    if root == nil || root.Val == 3 {\n        return\n    }\n    // \u5c1d\u8bd5\n    *path = append(*path, root)\n    if root.Val.(int) == 7 {\n        // \u8bb0\u5f55\u89e3\n        *res = append(*res, append([]*TreeNode{}, *path...))\n    }\n    preOrderIII(root.Left, res, path)\n    preOrderIII(root.Right, res, path)\n    // \u56de\u9000\n    *path = (*path)[:len(*path)-1]\n}\n
        preorder_traversal_iii_compact.swift
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunc preOrder(root: TreeNode?) {\n    // \u526a\u679d\n    guard let root = root, root.val != 3 else {\n        return\n    }\n    // \u5c1d\u8bd5\n    path.append(root)\n    if root.val == 7 {\n        // \u8bb0\u5f55\u89e3\n        res.append(path)\n    }\n    preOrder(root: root.left)\n    preOrder(root: root.right)\n    // \u56de\u9000\n    path.removeLast()\n}\n
        preorder_traversal_iii_compact.js
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunction preOrder(root, path, res) {\n    // \u526a\u679d\n    if (root === null || root.val === 3) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path.push(root);\n    if (root.val === 7) {\n        // \u8bb0\u5f55\u89e3\n        res.push([...path]);\n    }\n    preOrder(root.left, path, res);\n    preOrder(root.right, path, res);\n    // \u56de\u9000\n    path.pop();\n}\n
        preorder_traversal_iii_compact.ts
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunction preOrder(\n    root: TreeNode | null,\n    path: TreeNode[],\n    res: TreeNode[][]\n): void {\n    // \u526a\u679d\n    if (root === null || root.val === 3) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path.push(root);\n    if (root.val === 7) {\n        // \u8bb0\u5f55\u89e3\n        res.push([...path]);\n    }\n    preOrder(root.left, path, res);\n    preOrder(root.right, path, res);\n    // \u56de\u9000\n    path.pop();\n}\n
        preorder_traversal_iii_compact.dart
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(\n  TreeNode? root,\n  List<TreeNode> path,\n  List<List<TreeNode>> res,\n) {\n  if (root == null || root.val == 3) {\n    return;\n  }\n\n  // \u5c1d\u8bd5\n  path.add(root);\n  if (root.val == 7) {\n    // \u8bb0\u5f55\u89e3\n    res.add(List.from(path));\n  }\n  preOrder(root.left, path, res);\n  preOrder(root.right, path, res);\n  // \u56de\u9000\n  path.removeLast();\n}\n
        preorder_traversal_iii_compact.rs
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfn pre_order(\n    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,\n    path: &mut Vec<Rc<RefCell<TreeNode>>>,\n    root: Option<&Rc<RefCell<TreeNode>>>,\n) {\n    // \u526a\u679d\n    if root.is_none() || root.as_ref().unwrap().borrow().val == 3 {\n        return;\n    }\n    if let Some(node) = root {\n        // \u5c1d\u8bd5\n        path.push(node.clone());\n        if node.borrow().val == 7 {\n            // \u8bb0\u5f55\u89e3\n            res.push(path.clone());\n        }\n        pre_order(res, path, node.borrow().left.as_ref());\n        pre_order(res, path, node.borrow().right.as_ref());\n        // \u56de\u9000\n        path.pop();\n    }\n}\n
        preorder_traversal_iii_compact.c
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode *root) {\n    // \u526a\u679d\n    if (root == NULL || root->val == 3) {\n        return;\n    }\n    // \u5c1d\u8bd5\n    path[pathSize++] = root;\n    if (root->val == 7) {\n        // \u8bb0\u5f55\u89e3\n        for (int i = 0; i < pathSize; i++) {\n            res[resSize][i] = path[i];\n        }\n        resSize++;\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // \u56de\u9000\n    pathSize--;\n}\n
        preorder_traversal_iii_compact.kt
        /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfun preOrder(root: TreeNode?) {\n    // \u526a\u679d\n    if (root == null || root._val == 3) {\n        return\n    }\n    // \u5c1d\u8bd5\n    path!!.add(root)\n    if (root._val == 7) {\n        // \u8bb0\u5f55\u89e3\n        res!!.add(path!!.toMutableList())\n    }\n    preOrder(root.left)\n    preOrder(root.right)\n    // \u56de\u9000\n    path!!.removeAt(path!!.size - 1)\n}\n
        preorder_traversal_iii_compact.rb
        ### \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 ###\ndef pre_order(root)\n  # \u526a\u679d\n  return if !root || root.val == 3\n\n  # \u5c1d\u8bd5\n  $path.append(root)\n\n  # \u8bb0\u5f55\u89e3\n  $res << $path.dup if root.val == 7\n\n  pre_order(root.left)\n  pre_order(root.right)\n\n  # \u56de\u9000\n  $path.pop\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u201c\u526a\u679d\u201d\u662f\u4e00\u4e2a\u975e\u5e38\u5f62\u8c61\u7684\u540d\u8bcd\u3002\u5982\u56fe 13-3 \u6240\u793a\uff0c\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u201c\u526a\u6389\u201d\u4e86\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u641c\u7d22\u5206\u652f\uff0c\u907f\u514d\u8bb8\u591a\u65e0\u610f\u4e49\u7684\u5c1d\u8bd5\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u641c\u7d22\u6548\u7387\u3002

        \u56fe 13-3 \u00a0 \u6839\u636e\u7ea6\u675f\u6761\u4ef6\u526a\u679d

        "},{"location":"chapter_backtracking/backtracking_algorithm/#1313","title":"13.1.3 \u00a0 \u6846\u67b6\u4ee3\u7801","text":"

        \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c1d\u8bd5\u5c06\u56de\u6eaf\u7684\u201c\u5c1d\u8bd5\u3001\u56de\u9000\u3001\u526a\u679d\u201d\u7684\u4e3b\u4f53\u6846\u67b6\u63d0\u70bc\u51fa\u6765\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u901a\u7528\u6027\u3002

        \u5728\u4ee5\u4e0b\u6846\u67b6\u4ee3\u7801\u4e2d\uff0cstate \u8868\u793a\u95ee\u9898\u7684\u5f53\u524d\u72b6\u6001\uff0cchoices \u8868\u793a\u5f53\u524d\u72b6\u6001\u4e0b\u53ef\u4ee5\u505a\u51fa\u7684\u9009\u62e9\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        def backtrack(state: State, choices: list[choice], res: list[state]):\n    \"\"\"\u56de\u6eaf\u7b97\u6cd5\u6846\u67b6\"\"\"\n    # \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if is_solution(state):\n        # \u8bb0\u5f55\u89e3\n        record_solution(state, res)\n        # \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n        return\n    # \u904d\u5386\u6240\u6709\u9009\u62e9\n    for choice in choices:\n        # \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if is_valid(state, choice):\n            # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            make_choice(state, choice)\n            backtrack(state, choices, res)\n            # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undo_choice(state, choice)\n
        /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State *state, vector<Choice *> &choices, vector<State *> &res) {\n    // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if (isSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res);\n        // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (Choice choice : choices) {\n        // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state, choice);\n        }\n    }\n}\n
        /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State state, List<Choice> choices, List<State> res) {\n    // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if (isSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res);\n        // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (Choice choice : choices) {\n        // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state, choice);\n        }\n    }\n}\n
        /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid Backtrack(State state, List<Choice> choices, List<State> res) {\n    // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if (IsSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        RecordSolution(state, res);\n        // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    foreach (Choice choice in choices) {\n        // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (IsValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            MakeChoice(state, choice);\n            Backtrack(state, choices, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            UndoChoice(state, choice);\n        }\n    }\n}\n
        /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunc backtrack(state *State, choices []Choice, res *[]State) {\n    // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if isSolution(state) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res)\n        // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for _, choice := range choices {\n        // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if isValid(state, choice) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice)\n            backtrack(state, choices, res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state, choice)\n        }\n    }\n}\n
        /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunc backtrack(state: inout State, choices: [Choice], res: inout [State]) {\n    // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if isSolution(state: state) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state: state, res: &res)\n        // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for choice in choices {\n        // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if isValid(state: state, choice: choice) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state: &state, choice: choice)\n            backtrack(state: &state, choices: choices, res: &res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state: &state, choice: choice)\n        }\n    }\n}\n
        /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunction backtrack(state, choices, res) {\n    // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if (isSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res);\n        // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (let choice of choices) {\n        // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state, choice);\n        }\n    }\n}\n
        /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunction backtrack(state: State, choices: Choice[], res: State[]): void {\n    // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if (isSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res);\n        // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (let choice of choices) {\n        // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state, choice);\n        }\n    }\n}\n
        /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State state, List<Choice>, List<State> res) {\n  // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n  if (isSolution(state)) {\n    // \u8bb0\u5f55\u89e3\n    recordSolution(state, res);\n    // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n    return;\n  }\n  // \u904d\u5386\u6240\u6709\u9009\u62e9\n  for (Choice choice in choices) {\n    // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n    if (isValid(state, choice)) {\n      // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n      makeChoice(state, choice);\n      backtrack(state, choices, res);\n      // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n      undoChoice(state, choice);\n    }\n  }\n}\n
        /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfn backtrack(state: &mut State, choices: &Vec<Choice>, res: &mut Vec<State>) {\n    // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if is_solution(state) {\n        // \u8bb0\u5f55\u89e3\n        record_solution(state, res);\n        // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for choice in choices {\n        // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if is_valid(state, choice) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            make_choice(state, choice);\n            backtrack(state, choices, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undo_choice(state, choice);\n        }\n    }\n}\n
        /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State *state, Choice *choices, int numChoices, State *res, int numRes) {\n    // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if (isSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res, numRes);\n        // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (int i = 0; i < numChoices; i++) {\n        // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(state, &choices[i])) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, &choices[i]);\n            backtrack(state, choices, numChoices, res, numRes);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state, &choices[i]);\n        }\n    }\n}\n
        /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfun backtrack(state: State?, choices: List<Choice?>, res: List<State?>?) {\n    // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if (isSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res)\n        // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (choice in choices) {\n        // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice)\n            backtrack(state, choices, res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state, choice)\n        }\n    }\n}\n
        ### \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 ###\ndef backtrack(state, choices, res)\n    # \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n    if is_solution?(state)\n        # \u8bb0\u5f55\u89e3\n        record_solution(state, res)\n        return\n    end\n\n    # \u904d\u5386\u6240\u6709\u9009\u62e9\n    for choice in choices\n        # \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if is_valid?(state, choice)\n            # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            make_choice(state, choice)\n            backtrack(state, choices, res)\n            # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undo_choice(state, choice)\n        end\n    end\nend\n

        \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u57fa\u4e8e\u6846\u67b6\u4ee3\u7801\u6765\u89e3\u51b3\u4f8b\u9898\u4e09\u3002\u72b6\u6001 state \u4e3a\u8282\u70b9\u904d\u5386\u8def\u5f84\uff0c\u9009\u62e9 choices \u4e3a\u5f53\u524d\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\uff0c\u7ed3\u679c res \u662f\u8def\u5f84\u5217\u8868\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_iii_template.py
        def is_solution(state: list[TreeNode]) -> bool:\n    \"\"\"\u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3\"\"\"\n    return state and state[-1].val == 7\n\ndef record_solution(state: list[TreeNode], res: list[list[TreeNode]]):\n    \"\"\"\u8bb0\u5f55\u89e3\"\"\"\n    res.append(list(state))\n\ndef is_valid(state: list[TreeNode], choice: TreeNode) -> bool:\n    \"\"\"\u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5\"\"\"\n    return choice is not None and choice.val != 3\n\ndef make_choice(state: list[TreeNode], choice: TreeNode):\n    \"\"\"\u66f4\u65b0\u72b6\u6001\"\"\"\n    state.append(choice)\n\ndef undo_choice(state: list[TreeNode], choice: TreeNode):\n    \"\"\"\u6062\u590d\u72b6\u6001\"\"\"\n    state.pop()\n\ndef backtrack(\n    state: list[TreeNode], choices: list[TreeNode], res: list[list[TreeNode]]\n):\n    \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09\"\"\"\n    # \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n    if is_solution(state):\n        # \u8bb0\u5f55\u89e3\n        record_solution(state, res)\n    # \u904d\u5386\u6240\u6709\u9009\u62e9\n    for choice in choices:\n        # \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if is_valid(state, choice):\n            # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            make_choice(state, choice)\n            # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, [choice.left, choice.right], res)\n            # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undo_choice(state, choice)\n
        preorder_traversal_iii_template.cpp
        /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(vector<TreeNode *> &state) {\n    return !state.empty() && state.back()->val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(vector<TreeNode *> &state, vector<vector<TreeNode *>> &res) {\n    res.push_back(state);\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nbool isValid(vector<TreeNode *> &state, TreeNode *choice) {\n    return choice != nullptr && choice->val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(vector<TreeNode *> &state, TreeNode *choice) {\n    state.push_back(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(vector<TreeNode *> &state, TreeNode *choice) {\n    state.pop_back();\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<vector<TreeNode *>> &res) {\n    // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n    if (isSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res);\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (TreeNode *choice : choices) {\n        // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            vector<TreeNode *> nextChoices{choice->left, choice->right};\n            backtrack(state, nextChoices, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state, choice);\n        }\n    }\n}\n
        preorder_traversal_iii_template.java
        /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nboolean isSolution(List<TreeNode> state) {\n    return !state.isEmpty() && state.get(state.size() - 1).val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n    res.add(new ArrayList<>(state));\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nboolean isValid(List<TreeNode> state, TreeNode choice) {\n    return choice != null && choice.val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(List<TreeNode> state, TreeNode choice) {\n    state.add(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(List<TreeNode> state, TreeNode choice) {\n    state.remove(state.size() - 1);\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {\n    // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n    if (isSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res);\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (TreeNode choice : choices) {\n        // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, Arrays.asList(choice.left, choice.right), res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state, choice);\n        }\n    }\n}\n
        preorder_traversal_iii_template.cs
        /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool IsSolution(List<TreeNode> state) {\n    return state.Count != 0 && state[^1].val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nvoid RecordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n    res.Add(new List<TreeNode>(state));\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nbool IsValid(List<TreeNode> state, TreeNode choice) {\n    return choice != null && choice.val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid MakeChoice(List<TreeNode> state, TreeNode choice) {\n    state.Add(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nvoid UndoChoice(List<TreeNode> state, TreeNode choice) {\n    state.RemoveAt(state.Count - 1);\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid Backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {\n    // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n    if (IsSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        RecordSolution(state, res);\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    foreach (TreeNode choice in choices) {\n        // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (IsValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            MakeChoice(state, choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            Backtrack(state, [choice.left!, choice.right!], res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            UndoChoice(state, choice);\n        }\n    }\n}\n
        preorder_traversal_iii_template.go
        /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunc isSolution(state *[]*TreeNode) bool {\n    return len(*state) != 0 && (*state)[len(*state)-1].Val == 7\n}\n\n/* \u8bb0\u5f55\u89e3 */\nfunc recordSolution(state *[]*TreeNode, res *[][]*TreeNode) {\n    *res = append(*res, append([]*TreeNode{}, *state...))\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfunc isValid(state *[]*TreeNode, choice *TreeNode) bool {\n    return choice != nil && choice.Val != 3\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nfunc makeChoice(state *[]*TreeNode, choice *TreeNode) {\n    *state = append(*state, choice)\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nfunc undoChoice(state *[]*TreeNode, choice *TreeNode) {\n    *state = (*state)[:len(*state)-1]\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunc backtrackIII(state *[]*TreeNode, choices *[]*TreeNode, res *[][]*TreeNode) {\n    // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n    if isSolution(state) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res)\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for _, choice := range *choices {\n        // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if isValid(state, choice) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice)\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            temp := make([]*TreeNode, 0)\n            temp = append(temp, choice.Left, choice.Right)\n            backtrackIII(state, &temp, res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state, choice)\n        }\n    }\n}\n
        preorder_traversal_iii_template.swift
        /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunc isSolution(state: [TreeNode]) -> Bool {\n    !state.isEmpty && state.last!.val == 7\n}\n\n/* \u8bb0\u5f55\u89e3 */\nfunc recordSolution(state: [TreeNode], res: inout [[TreeNode]]) {\n    res.append(state)\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfunc isValid(state: [TreeNode], choice: TreeNode?) -> Bool {\n    choice != nil && choice!.val != 3\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nfunc makeChoice(state: inout [TreeNode], choice: TreeNode) {\n    state.append(choice)\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nfunc undoChoice(state: inout [TreeNode], choice: TreeNode) {\n    state.removeLast()\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunc backtrack(state: inout [TreeNode], choices: [TreeNode], res: inout [[TreeNode]]) {\n    // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n    if isSolution(state: state) {\n        recordSolution(state: state, res: &res)\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for choice in choices {\n        // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if isValid(state: state, choice: choice) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state: &state, choice: choice)\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state: &state, choices: [choice.left, choice.right].compactMap { $0 }, res: &res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state: &state, choice: choice)\n        }\n    }\n}\n
        preorder_traversal_iii_template.js
        /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunction isSolution(state) {\n    return state && state[state.length - 1]?.val === 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nfunction recordSolution(state, res) {\n    res.push([...state]);\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfunction isValid(state, choice) {\n    return choice !== null && choice.val !== 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nfunction makeChoice(state, choice) {\n    state.push(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nfunction undoChoice(state) {\n    state.pop();\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunction backtrack(state, choices, res) {\n    // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n    if (isSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res);\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (const choice of choices) {\n        // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, [choice.left, choice.right], res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state);\n        }\n    }\n}\n
        preorder_traversal_iii_template.ts
        /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunction isSolution(state: TreeNode[]): boolean {\n    return state && state[state.length - 1]?.val === 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nfunction recordSolution(state: TreeNode[], res: TreeNode[][]): void {\n    res.push([...state]);\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfunction isValid(state: TreeNode[], choice: TreeNode): boolean {\n    return choice !== null && choice.val !== 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nfunction makeChoice(state: TreeNode[], choice: TreeNode): void {\n    state.push(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nfunction undoChoice(state: TreeNode[]): void {\n    state.pop();\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunction backtrack(\n    state: TreeNode[],\n    choices: TreeNode[],\n    res: TreeNode[][]\n): void {\n    // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n    if (isSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res);\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (const choice of choices) {\n        // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, [choice.left, choice.right], res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state);\n        }\n    }\n}\n
        preorder_traversal_iii_template.dart
        /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(List<TreeNode> state) {\n  return state.isNotEmpty && state.last.val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n  res.add(List.from(state));\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nbool isValid(List<TreeNode> state, TreeNode? choice) {\n  return choice != null && choice.val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(List<TreeNode> state, TreeNode? choice) {\n  state.add(choice!);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(List<TreeNode> state, TreeNode? choice) {\n  state.removeLast();\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(\n  List<TreeNode> state,\n  List<TreeNode?> choices,\n  List<List<TreeNode>> res,\n) {\n  // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n  if (isSolution(state)) {\n    // \u8bb0\u5f55\u89e3\n    recordSolution(state, res);\n  }\n  // \u904d\u5386\u6240\u6709\u9009\u62e9\n  for (TreeNode? choice in choices) {\n    // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n    if (isValid(state, choice)) {\n      // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n      makeChoice(state, choice);\n      // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n      backtrack(state, [choice!.left, choice.right], res);\n      // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n      undoChoice(state, choice);\n    }\n  }\n}\n
        preorder_traversal_iii_template.rs
        /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfn is_solution(state: &mut Vec<Rc<RefCell<TreeNode>>>) -> bool {\n    return !state.is_empty() && state.last().unwrap().borrow().val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nfn record_solution(\n    state: &mut Vec<Rc<RefCell<TreeNode>>>,\n    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,\n) {\n    res.push(state.clone());\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfn is_valid(_: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Option<&Rc<RefCell<TreeNode>>>) -> bool {\n    return choice.is_some() && choice.unwrap().borrow().val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nfn make_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Rc<RefCell<TreeNode>>) {\n    state.push(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nfn undo_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, _: Rc<RefCell<TreeNode>>) {\n    state.pop();\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfn backtrack(\n    state: &mut Vec<Rc<RefCell<TreeNode>>>,\n    choices: &Vec<Option<&Rc<RefCell<TreeNode>>>>,\n    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,\n) {\n    // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n    if is_solution(state) {\n        // \u8bb0\u5f55\u89e3\n        record_solution(state, res);\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for &choice in choices.iter() {\n        // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if is_valid(state, choice) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            make_choice(state, choice.unwrap().clone());\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(\n                state,\n                &vec![\n                    choice.unwrap().borrow().left.as_ref(),\n                    choice.unwrap().borrow().right.as_ref(),\n                ],\n                res,\n            );\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undo_choice(state, choice.unwrap().clone());\n        }\n    }\n}\n
        preorder_traversal_iii_template.c
        /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(void) {\n    return pathSize > 0 && path[pathSize - 1]->val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(void) {\n    for (int i = 0; i < pathSize; i++) {\n        res[resSize][i] = path[i];\n    }\n    resSize++;\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nbool isValid(TreeNode *choice) {\n    return choice != NULL && choice->val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(TreeNode *choice) {\n    path[pathSize++] = choice;\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(void) {\n    pathSize--;\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(TreeNode *choices[2]) {\n    // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n    if (isSolution()) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution();\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (int i = 0; i < 2; i++) {\n        TreeNode *choice = choices[i];\n        // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            TreeNode *nextChoices[2] = {choice->left, choice->right};\n            backtrack(nextChoices);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice();\n        }\n    }\n}\n
        preorder_traversal_iii_template.kt
        /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfun isSolution(state: MutableList<TreeNode?>): Boolean {\n    return state.isNotEmpty() && state[state.size - 1]?._val == 7\n}\n\n/* \u8bb0\u5f55\u89e3 */\nfun recordSolution(state: MutableList<TreeNode?>?, res: MutableList<MutableList<TreeNode?>?>) {\n    res.add(state!!.toMutableList())\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfun isValid(state: MutableList<TreeNode?>?, choice: TreeNode?): Boolean {\n    return choice != null && choice._val != 3\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nfun makeChoice(state: MutableList<TreeNode?>, choice: TreeNode?) {\n    state.add(choice)\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nfun undoChoice(state: MutableList<TreeNode?>, choice: TreeNode?) {\n    state.removeLast()\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfun backtrack(\n    state: MutableList<TreeNode?>,\n    choices: MutableList<TreeNode?>,\n    res: MutableList<MutableList<TreeNode?>?>\n) {\n    // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n    if (isSolution(state)) {\n        // \u8bb0\u5f55\u89e3\n        recordSolution(state, res)\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (choice in choices) {\n        // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n        if (isValid(state, choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            makeChoice(state, choice)\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, mutableListOf(choice!!.left, choice.right), res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            undoChoice(state, choice)\n        }\n    }\n}\n
        preorder_traversal_iii_template.rb
        ### \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 ###\ndef is_solution?(state)\n  !state.empty? && state.last.val == 7\nend\n\n### \u8bb0\u5f55\u89e3 ###\ndef record_solution(state, res)\n  res << state.dup\nend\n\n### \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 ###\ndef is_valid?(state, choice)\n  choice && choice.val != 3\nend\n\n### \u66f4\u65b0\u72b6\u6001 ###\ndef make_choice(state, choice)\n  state << choice\nend\n\n### \u6062\u590d\u72b6\u6001 ###\ndef undo_choice(state, choice)\n  state.pop\nend\n\n### \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 ###\ndef backtrack(state, choices, res)\n  # \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n  record_solution(state, res) if is_solution?(state)\n\n  # \u904d\u5386\u6240\u6709\u9009\u62e9\n  for choice in choices\n    # \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n    if is_valid?(state, choice)\n      # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n      make_choice(state, choice)\n      # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n      backtrack(state, [choice.left, choice.right], res)\n      # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n      undo_choice(state, choice)\n    end\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u6839\u636e\u9898\u610f\uff0c\u6211\u4eec\u5728\u627e\u5230\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\u540e\u5e94\u8be5\u7ee7\u7eed\u641c\u7d22\uff0c\u56e0\u6b64\u9700\u8981\u5c06\u8bb0\u5f55\u89e3\u4e4b\u540e\u7684 return \u8bed\u53e5\u5220\u9664\u3002\u56fe 13-4 \u5bf9\u6bd4\u4e86\u4fdd\u7559\u6216\u5220\u9664 return \u8bed\u53e5\u7684\u641c\u7d22\u8fc7\u7a0b\u3002

        \u56fe 13-4 \u00a0 \u4fdd\u7559\u4e0e\u5220\u9664 return \u7684\u641c\u7d22\u8fc7\u7a0b\u5bf9\u6bd4

        \u76f8\u6bd4\u57fa\u4e8e\u524d\u5e8f\u904d\u5386\u7684\u4ee3\u7801\u5b9e\u73b0\uff0c\u57fa\u4e8e\u56de\u6eaf\u7b97\u6cd5\u6846\u67b6\u7684\u4ee3\u7801\u5b9e\u73b0\u867d\u7136\u663e\u5f97\u5570\u5506\uff0c\u4f46\u901a\u7528\u6027\u66f4\u597d\u3002\u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u56de\u6eaf\u95ee\u9898\u53ef\u4ee5\u5728\u8be5\u6846\u67b6\u4e0b\u89e3\u51b3\u3002\u6211\u4eec\u53ea\u9700\u6839\u636e\u5177\u4f53\u95ee\u9898\u6765\u5b9a\u4e49 state \u548c choices \uff0c\u5e76\u5b9e\u73b0\u6846\u67b6\u4e2d\u7684\u5404\u4e2a\u65b9\u6cd5\u5373\u53ef\u3002

        "},{"location":"chapter_backtracking/backtracking_algorithm/#1314","title":"13.1.4 \u00a0 \u5e38\u7528\u672f\u8bed","text":"

        \u4e3a\u4e86\u66f4\u6e05\u6670\u5730\u5206\u6790\u7b97\u6cd5\u95ee\u9898\uff0c\u6211\u4eec\u603b\u7ed3\u4e00\u4e0b\u56de\u6eaf\u7b97\u6cd5\u4e2d\u5e38\u7528\u672f\u8bed\u7684\u542b\u4e49\uff0c\u5e76\u5bf9\u7167\u4f8b\u9898\u4e09\u7ed9\u51fa\u5bf9\u5e94\u793a\u4f8b\uff0c\u5982\u8868 13-1 \u6240\u793a\u3002

        \u8868 13-1 \u00a0 \u5e38\u89c1\u7684\u56de\u6eaf\u7b97\u6cd5\u672f\u8bed

        \u540d\u8bcd \u5b9a\u4e49 \u4f8b\u9898\u4e09 \u89e3\uff08solution\uff09 \u89e3\u662f\u6ee1\u8db3\u95ee\u9898\u7279\u5b9a\u6761\u4ef6\u7684\u7b54\u6848\uff0c\u53ef\u80fd\u6709\u4e00\u4e2a\u6216\u591a\u4e2a \u6839\u8282\u70b9\u5230\u8282\u70b9 \\(7\\) \u7684\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u6240\u6709\u8def\u5f84 \u7ea6\u675f\u6761\u4ef6\uff08constraint\uff09 \u7ea6\u675f\u6761\u4ef6\u662f\u95ee\u9898\u4e2d\u9650\u5236\u89e3\u7684\u53ef\u884c\u6027\u7684\u6761\u4ef6\uff0c\u901a\u5e38\u7528\u4e8e\u526a\u679d \u8def\u5f84\u4e2d\u4e0d\u5305\u542b\u8282\u70b9 \\(3\\) \u72b6\u6001\uff08state\uff09 \u72b6\u6001\u8868\u793a\u95ee\u9898\u5728\u67d0\u4e00\u65f6\u523b\u7684\u60c5\u51b5\uff0c\u5305\u62ec\u5df2\u7ecf\u505a\u51fa\u7684\u9009\u62e9 \u5f53\u524d\u5df2\u8bbf\u95ee\u7684\u8282\u70b9\u8def\u5f84\uff0c\u5373 path \u8282\u70b9\u5217\u8868 \u5c1d\u8bd5\uff08attempt\uff09 \u5c1d\u8bd5\u662f\u6839\u636e\u53ef\u7528\u9009\u62e9\u6765\u63a2\u7d22\u89e3\u7a7a\u95f4\u7684\u8fc7\u7a0b\uff0c\u5305\u62ec\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\uff0c\u68c0\u67e5\u662f\u5426\u4e3a\u89e3 \u9012\u5f52\u8bbf\u95ee\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\uff0c\u5c06\u8282\u70b9\u6dfb\u52a0\u8fdb path \uff0c\u5224\u65ad\u8282\u70b9\u7684\u503c\u662f\u5426\u4e3a \\(7\\) \u56de\u9000\uff08backtracking\uff09 \u56de\u9000\u6307\u9047\u5230\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u72b6\u6001\u65f6\uff0c\u64a4\u9500\u524d\u9762\u505a\u51fa\u7684\u9009\u62e9\uff0c\u56de\u5230\u4e0a\u4e00\u4e2a\u72b6\u6001 \u5f53\u8d8a\u8fc7\u53f6\u8282\u70b9\u3001\u7ed3\u675f\u8282\u70b9\u8bbf\u95ee\u3001\u9047\u5230\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u65f6\u7ec8\u6b62\u641c\u7d22\uff0c\u51fd\u6570\u8fd4\u56de \u526a\u679d\uff08pruning\uff09 \u526a\u679d\u662f\u6839\u636e\u95ee\u9898\u7279\u6027\u548c\u7ea6\u675f\u6761\u4ef6\u907f\u514d\u65e0\u610f\u4e49\u7684\u641c\u7d22\u8def\u5f84\u7684\u65b9\u6cd5\uff0c\u53ef\u63d0\u9ad8\u641c\u7d22\u6548\u7387 \u5f53\u9047\u5230\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u65f6\uff0c\u5219\u4e0d\u518d\u7ee7\u7eed\u641c\u7d22

        Tip

        \u95ee\u9898\u3001\u89e3\u3001\u72b6\u6001\u7b49\u6982\u5ff5\u662f\u901a\u7528\u7684\uff0c\u5728\u5206\u6cbb\u3001\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\u3001\u8d2a\u5fc3\u7b49\u7b97\u6cd5\u4e2d\u90fd\u6709\u6d89\u53ca\u3002

        "},{"location":"chapter_backtracking/backtracking_algorithm/#1315","title":"13.1.5 \u00a0 \u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"

        \u56de\u6eaf\u7b97\u6cd5\u672c\u8d28\u4e0a\u662f\u4e00\u79cd\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u7b97\u6cd5\uff0c\u5b83\u5c1d\u8bd5\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\u76f4\u5230\u627e\u5230\u6ee1\u8db3\u6761\u4ef6\u7684\u89e3\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u80fd\u591f\u627e\u5230\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u800c\u4e14\u5728\u5408\u7406\u7684\u526a\u679d\u64cd\u4f5c\u4e0b\uff0c\u5177\u6709\u5f88\u9ad8\u7684\u6548\u7387\u3002

        \u7136\u800c\uff0c\u5728\u5904\u7406\u5927\u89c4\u6a21\u6216\u8005\u590d\u6742\u95ee\u9898\u65f6\uff0c\u56de\u6eaf\u7b97\u6cd5\u7684\u8fd0\u884c\u6548\u7387\u53ef\u80fd\u96be\u4ee5\u63a5\u53d7\u3002

        • \u65f6\u95f4\uff1a\u56de\u6eaf\u7b97\u6cd5\u901a\u5e38\u9700\u8981\u904d\u5386\u72b6\u6001\u7a7a\u95f4\u7684\u6240\u6709\u53ef\u80fd\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u8fbe\u5230\u6307\u6570\u9636\u6216\u9636\u4e58\u9636\u3002
        • \u7a7a\u95f4\uff1a\u5728\u9012\u5f52\u8c03\u7528\u4e2d\u9700\u8981\u4fdd\u5b58\u5f53\u524d\u7684\u72b6\u6001\uff08\u4f8b\u5982\u8def\u5f84\u3001\u7528\u4e8e\u526a\u679d\u7684\u8f85\u52a9\u53d8\u91cf\u7b49\uff09\uff0c\u5f53\u6df1\u5ea6\u5f88\u5927\u65f6\uff0c\u7a7a\u95f4\u9700\u6c42\u53ef\u80fd\u4f1a\u53d8\u5f97\u5f88\u5927\u3002

        \u5373\u4fbf\u5982\u6b64\uff0c\u56de\u6eaf\u7b97\u6cd5\u4ecd\u7136\u662f\u67d0\u4e9b\u641c\u7d22\u95ee\u9898\u548c\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\u7684\u6700\u4f73\u89e3\u51b3\u65b9\u6848\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u95ee\u9898\uff0c\u7531\u4e8e\u65e0\u6cd5\u9884\u6d4b\u54ea\u4e9b\u9009\u62e9\u53ef\u751f\u6210\u6709\u6548\u7684\u89e3\uff0c\u56e0\u6b64\u6211\u4eec\u5fc5\u987b\u5bf9\u6240\u6709\u53ef\u80fd\u7684\u9009\u62e9\u8fdb\u884c\u904d\u5386\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5173\u952e\u662f\u5982\u4f55\u4f18\u5316\u6548\u7387\uff0c\u5e38\u89c1\u7684\u6548\u7387\u4f18\u5316\u65b9\u6cd5\u6709\u4e24\u79cd\u3002

        • \u526a\u679d\uff1a\u907f\u514d\u641c\u7d22\u90a3\u4e9b\u80af\u5b9a\u4e0d\u4f1a\u4ea7\u751f\u89e3\u7684\u8def\u5f84\uff0c\u4ece\u800c\u8282\u7701\u65f6\u95f4\u548c\u7a7a\u95f4\u3002
        • \u542f\u53d1\u5f0f\u641c\u7d22\uff1a\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u5f15\u5165\u4e00\u4e9b\u7b56\u7565\u6216\u8005\u4f30\u8ba1\u503c\uff0c\u4ece\u800c\u4f18\u5148\u641c\u7d22\u6700\u6709\u53ef\u80fd\u4ea7\u751f\u6709\u6548\u89e3\u7684\u8def\u5f84\u3002
        "},{"location":"chapter_backtracking/backtracking_algorithm/#1316","title":"13.1.6 \u00a0 \u56de\u6eaf\u5178\u578b\u4f8b\u9898","text":"

        \u56de\u6eaf\u7b97\u6cd5\u53ef\u7528\u4e8e\u89e3\u51b3\u8bb8\u591a\u641c\u7d22\u95ee\u9898\u3001\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\u548c\u7ec4\u5408\u4f18\u5316\u95ee\u9898\u3002

        \u641c\u7d22\u95ee\u9898\uff1a\u8fd9\u7c7b\u95ee\u9898\u7684\u76ee\u6807\u662f\u627e\u5230\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u89e3\u51b3\u65b9\u6848\u3002

        • \u5168\u6392\u5217\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u96c6\u5408\uff0c\u6c42\u51fa\u5176\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u7ec4\u5408\u3002
        • \u5b50\u96c6\u548c\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u96c6\u5408\u548c\u4e00\u4e2a\u76ee\u6807\u548c\uff0c\u627e\u5230\u96c6\u5408\u4e2d\u6240\u6709\u548c\u4e3a\u76ee\u6807\u548c\u7684\u5b50\u96c6\u3002
        • \u6c49\u8bfa\u5854\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e09\u6839\u67f1\u5b50\u548c\u4e00\u7cfb\u5217\u5927\u5c0f\u4e0d\u540c\u7684\u5706\u76d8\uff0c\u8981\u6c42\u5c06\u6240\u6709\u5706\u76d8\u4ece\u4e00\u6839\u67f1\u5b50\u79fb\u52a8\u5230\u53e6\u4e00\u6839\u67f1\u5b50\uff0c\u6bcf\u6b21\u53ea\u80fd\u79fb\u52a8\u4e00\u4e2a\u5706\u76d8\uff0c\u4e14\u4e0d\u80fd\u5c06\u5927\u5706\u76d8\u653e\u5728\u5c0f\u5706\u76d8\u4e0a\u3002

        \u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\uff1a\u8fd9\u7c7b\u95ee\u9898\u7684\u76ee\u6807\u662f\u627e\u5230\u6ee1\u8db3\u6240\u6709\u7ea6\u675f\u6761\u4ef6\u7684\u89e3\u3002

        • \\(n\\) \u7687\u540e\uff1a\u5728 \\(n \\times n\\) \u7684\u68cb\u76d8\u4e0a\u653e\u7f6e \\(n\\) \u4e2a\u7687\u540e\uff0c\u4f7f\u5f97\u5b83\u4eec\u4e92\u4e0d\u653b\u51fb\u3002
        • \u6570\u72ec\uff1a\u5728 \\(9 \\times 9\\) \u7684\u7f51\u683c\u4e2d\u586b\u5165\u6570\u5b57 \\(1\\) ~ \\(9\\) \uff0c\u4f7f\u5f97\u6bcf\u884c\u3001\u6bcf\u5217\u548c\u6bcf\u4e2a \\(3 \\times 3\\) \u5b50\u7f51\u683c\u4e2d\u7684\u6570\u5b57\u4e0d\u91cd\u590d\u3002
        • \u56fe\u7740\u8272\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u65e0\u5411\u56fe\uff0c\u7528\u6700\u5c11\u7684\u989c\u8272\u7ed9\u56fe\u7684\u6bcf\u4e2a\u9876\u70b9\u7740\u8272\uff0c\u4f7f\u5f97\u76f8\u90bb\u9876\u70b9\u989c\u8272\u4e0d\u540c\u3002

        \u7ec4\u5408\u4f18\u5316\u95ee\u9898\uff1a\u8fd9\u7c7b\u95ee\u9898\u7684\u76ee\u6807\u662f\u5728\u4e00\u4e2a\u7ec4\u5408\u7a7a\u95f4\u4e2d\u627e\u5230\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u7684\u6700\u4f18\u89e3\u3002

        • 0-1 \u80cc\u5305\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u7ec4\u7269\u54c1\u548c\u4e00\u4e2a\u80cc\u5305\uff0c\u6bcf\u4e2a\u7269\u54c1\u6709\u4e00\u5b9a\u7684\u4ef7\u503c\u548c\u91cd\u91cf\uff0c\u8981\u6c42\u5728\u80cc\u5305\u5bb9\u91cf\u9650\u5236\u5185\uff0c\u9009\u62e9\u7269\u54c1\u4f7f\u5f97\u603b\u4ef7\u503c\u6700\u5927\u3002
        • \u65c5\u884c\u5546\u95ee\u9898\uff1a\u5728\u4e00\u4e2a\u56fe\u4e2d\uff0c\u4ece\u4e00\u4e2a\u70b9\u51fa\u53d1\uff0c\u8bbf\u95ee\u6240\u6709\u5176\u4ed6\u70b9\u6070\u597d\u4e00\u6b21\u540e\u8fd4\u56de\u8d77\u70b9\uff0c\u6c42\u6700\u77ed\u8def\u5f84\u3002
        • \u6700\u5927\u56e2\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u65e0\u5411\u56fe\uff0c\u627e\u5230\u6700\u5927\u7684\u5b8c\u5168\u5b50\u56fe\uff0c\u5373\u5b50\u56fe\u4e2d\u7684\u4efb\u610f\u4e24\u4e2a\u9876\u70b9\u4e4b\u95f4\u90fd\u6709\u8fb9\u76f8\u8fde\u3002

        \u8bf7\u6ce8\u610f\uff0c\u5bf9\u4e8e\u8bb8\u591a\u7ec4\u5408\u4f18\u5316\u95ee\u9898\uff0c\u56de\u6eaf\u4e0d\u662f\u6700\u4f18\u89e3\u51b3\u65b9\u6848\u3002

        • 0-1 \u80cc\u5305\u95ee\u9898\u901a\u5e38\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u89e3\u51b3\uff0c\u4ee5\u8fbe\u5230\u66f4\u9ad8\u7684\u65f6\u95f4\u6548\u7387\u3002
        • \u65c5\u884c\u5546\u662f\u4e00\u4e2a\u8457\u540d\u7684 NP-Hard \u95ee\u9898\uff0c\u5e38\u7528\u89e3\u6cd5\u6709\u9057\u4f20\u7b97\u6cd5\u548c\u8681\u7fa4\u7b97\u6cd5\u7b49\u3002
        • \u6700\u5927\u56e2\u95ee\u9898\u662f\u56fe\u8bba\u4e2d\u7684\u4e00\u4e2a\u7ecf\u5178\u95ee\u9898\uff0c\u53ef\u7528\u8d2a\u5fc3\u7b97\u6cd5\u7b49\u542f\u53d1\u5f0f\u7b97\u6cd5\u6765\u89e3\u51b3\u3002
        "},{"location":"chapter_backtracking/n_queens_problem/","title":"13.4 \u00a0 n \u7687\u540e\u95ee\u9898","text":"

        Question

        \u6839\u636e\u56fd\u9645\u8c61\u68cb\u7684\u89c4\u5219\uff0c\u7687\u540e\u53ef\u4ee5\u653b\u51fb\u4e0e\u540c\u5904\u4e00\u884c\u3001\u4e00\u5217\u6216\u4e00\u6761\u659c\u7ebf\u4e0a\u7684\u68cb\u5b50\u3002\u7ed9\u5b9a \\(n\\) \u4e2a\u7687\u540e\u548c\u4e00\u4e2a \\(n \\times n\\) \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5bfb\u627e\u4f7f\u5f97\u6240\u6709\u7687\u540e\u4e4b\u95f4\u65e0\u6cd5\u76f8\u4e92\u653b\u51fb\u7684\u6446\u653e\u65b9\u6848\u3002

        \u5982\u56fe 13-15 \u6240\u793a\uff0c\u5f53 \\(n = 4\\) \u65f6\uff0c\u5171\u53ef\u4ee5\u627e\u5230\u4e24\u4e2a\u89e3\u3002\u4ece\u56de\u6eaf\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\\(n \\times n\\) \u5927\u5c0f\u7684\u68cb\u76d8\u5171\u6709 \\(n^2\\) \u4e2a\u683c\u5b50\uff0c\u7ed9\u51fa\u4e86\u6240\u6709\u7684\u9009\u62e9 choices \u3002\u5728\u9010\u4e2a\u653e\u7f6e\u7687\u540e\u7684\u8fc7\u7a0b\u4e2d\uff0c\u68cb\u76d8\u72b6\u6001\u5728\u4e0d\u65ad\u5730\u53d8\u5316\uff0c\u6bcf\u4e2a\u65f6\u523b\u7684\u68cb\u76d8\u5c31\u662f\u72b6\u6001 state \u3002

        \u56fe 13-15 \u00a0 4 \u7687\u540e\u95ee\u9898\u7684\u89e3

        \u56fe 13-16 \u5c55\u793a\u4e86\u672c\u9898\u7684\u4e09\u4e2a\u7ea6\u675f\u6761\u4ef6\uff1a\u591a\u4e2a\u7687\u540e\u4e0d\u80fd\u5728\u540c\u4e00\u884c\u3001\u540c\u4e00\u5217\u3001\u540c\u4e00\u6761\u5bf9\u89d2\u7ebf\u4e0a\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5bf9\u89d2\u7ebf\u5206\u4e3a\u4e3b\u5bf9\u89d2\u7ebf \\ \u548c\u6b21\u5bf9\u89d2\u7ebf / \u4e24\u79cd\u3002

        \u56fe 13-16 \u00a0 n \u7687\u540e\u95ee\u9898\u7684\u7ea6\u675f\u6761\u4ef6

        "},{"location":"chapter_backtracking/n_queens_problem/#1","title":"1. \u00a0 \u9010\u884c\u653e\u7f6e\u7b56\u7565","text":"

        \u7687\u540e\u7684\u6570\u91cf\u548c\u68cb\u76d8\u7684\u884c\u6570\u90fd\u4e3a \\(n\\) \uff0c\u56e0\u6b64\u6211\u4eec\u5bb9\u6613\u5f97\u5230\u4e00\u4e2a\u63a8\u8bba\uff1a\u68cb\u76d8\u6bcf\u884c\u90fd\u5141\u8bb8\u4e14\u53ea\u5141\u8bb8\u653e\u7f6e\u4e00\u4e2a\u7687\u540e\u3002

        \u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u53d6\u9010\u884c\u653e\u7f6e\u7b56\u7565\uff1a\u4ece\u7b2c\u4e00\u884c\u5f00\u59cb\uff0c\u5728\u6bcf\u884c\u653e\u7f6e\u4e00\u4e2a\u7687\u540e\uff0c\u76f4\u81f3\u6700\u540e\u4e00\u884c\u7ed3\u675f\u3002

        \u56fe 13-17 \u6240\u793a\u4e3a 4 \u7687\u540e\u95ee\u9898\u7684\u9010\u884c\u653e\u7f6e\u8fc7\u7a0b\u3002\u53d7\u753b\u5e45\u9650\u5236\uff0c\u56fe 13-17 \u4ec5\u5c55\u5f00\u4e86\u7b2c\u4e00\u884c\u7684\u5176\u4e2d\u4e00\u4e2a\u641c\u7d22\u5206\u652f\uff0c\u5e76\u4e14\u5c06\u4e0d\u6ee1\u8db3\u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u65b9\u6848\u90fd\u8fdb\u884c\u4e86\u526a\u679d\u3002

        \u56fe 13-17 \u00a0 \u9010\u884c\u653e\u7f6e\u7b56\u7565

        \u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u9010\u884c\u653e\u7f6e\u7b56\u7565\u8d77\u5230\u4e86\u526a\u679d\u7684\u4f5c\u7528\uff0c\u5b83\u907f\u514d\u4e86\u540c\u4e00\u884c\u51fa\u73b0\u591a\u4e2a\u7687\u540e\u7684\u6240\u6709\u641c\u7d22\u5206\u652f\u3002

        "},{"location":"chapter_backtracking/n_queens_problem/#2","title":"2. \u00a0 \u5217\u4e0e\u5bf9\u89d2\u7ebf\u526a\u679d","text":"

        \u4e3a\u4e86\u6ee1\u8db3\u5217\u7ea6\u675f\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u5e03\u5c14\u578b\u6570\u7ec4 cols \u8bb0\u5f55\u6bcf\u4e00\u5217\u662f\u5426\u6709\u7687\u540e\u3002\u5728\u6bcf\u6b21\u51b3\u5b9a\u653e\u7f6e\u524d\uff0c\u6211\u4eec\u901a\u8fc7 cols \u5c06\u5df2\u6709\u7687\u540e\u7684\u5217\u8fdb\u884c\u526a\u679d\uff0c\u5e76\u5728\u56de\u6eaf\u4e2d\u52a8\u6001\u66f4\u65b0 cols \u7684\u72b6\u6001\u3002

        Tip

        \u8bf7\u6ce8\u610f\uff0c\u77e9\u9635\u7684\u8d77\u70b9\u4f4d\u4e8e\u5de6\u4e0a\u89d2\uff0c\u5176\u4e2d\u884c\u7d22\u5f15\u4ece\u4e0a\u5230\u4e0b\u589e\u52a0\uff0c\u5217\u7d22\u5f15\u4ece\u5de6\u5230\u53f3\u589e\u52a0\u3002

        \u90a3\u4e48\uff0c\u5982\u4f55\u5904\u7406\u5bf9\u89d2\u7ebf\u7ea6\u675f\u5462\uff1f\u8bbe\u68cb\u76d8\u4e2d\u67d0\u4e2a\u683c\u5b50\u7684\u884c\u5217\u7d22\u5f15\u4e3a \\((row, col)\\) \uff0c\u9009\u5b9a\u77e9\u9635\u4e2d\u7684\u67d0\u6761\u4e3b\u5bf9\u89d2\u7ebf\uff0c\u6211\u4eec\u53d1\u73b0\u8be5\u5bf9\u89d2\u7ebf\u4e0a\u6240\u6709\u683c\u5b50\u7684\u884c\u7d22\u5f15\u51cf\u5217\u7d22\u5f15\u90fd\u76f8\u7b49\uff0c\u5373\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u6240\u6709\u683c\u5b50\u7684 \\(row - col\\) \u4e3a\u6052\u5b9a\u503c\u3002

        \u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4e24\u4e2a\u683c\u5b50\u6ee1\u8db3 \\(row_1 - col_1 = row_2 - col_2\\) \uff0c\u5219\u5b83\u4eec\u4e00\u5b9a\u5904\u5728\u540c\u4e00\u6761\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u3002\u5229\u7528\u8be5\u89c4\u5f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u501f\u52a9\u56fe 13-18 \u6240\u793a\u7684\u6570\u7ec4 diags1 \u8bb0\u5f55\u6bcf\u6761\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\u3002

        \u540c\u7406\uff0c\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u7684\u6240\u6709\u683c\u5b50\u7684 \\(row + col\\) \u662f\u6052\u5b9a\u503c\u3002\u6211\u4eec\u540c\u6837\u4e5f\u53ef\u4ee5\u501f\u52a9\u6570\u7ec4 diags2 \u6765\u5904\u7406\u6b21\u5bf9\u89d2\u7ebf\u7ea6\u675f\u3002

        \u56fe 13-18 \u00a0 \u5904\u7406\u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f

        "},{"location":"chapter_backtracking/n_queens_problem/#3","title":"3. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u8bf7\u6ce8\u610f\uff0c\\(n\\) \u7ef4\u65b9\u9635\u4e2d \\(row - col\\) \u7684\u8303\u56f4\u662f \\([-n + 1, n - 1]\\) \uff0c\\(row + col\\) \u7684\u8303\u56f4\u662f \\([0, 2n - 2]\\) \uff0c\u6240\u4ee5\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\u7684\u6570\u91cf\u90fd\u4e3a \\(2n - 1\\) \uff0c\u5373\u6570\u7ec4 diags1 \u548c diags2 \u7684\u957f\u5ea6\u90fd\u4e3a \\(2n - 1\\) \u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby n_queens.py
        def backtrack(\n    row: int,\n    n: int,\n    state: list[list[str]],\n    res: list[list[list[str]]],\n    cols: list[bool],\n    diags1: list[bool],\n    diags2: list[bool],\n):\n    \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e\"\"\"\n    # \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if row == n:\n        res.append([list(row) for row in state])\n        return\n    # \u904d\u5386\u6240\u6709\u5217\n    for col in range(n):\n        # \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n        diag1 = row - col + n - 1\n        diag2 = row + col\n        # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n        if not cols[col] and not diags1[diag1] and not diags2[diag2]:\n            # \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n            state[row][col] = \"Q\"\n            cols[col] = diags1[diag1] = diags2[diag2] = True\n            # \u653e\u7f6e\u4e0b\u4e00\u884c\n            backtrack(row + 1, n, state, res, cols, diags1, diags2)\n            # \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n            state[row][col] = \"#\"\n            cols[col] = diags1[diag1] = diags2[diag2] = False\n\ndef n_queens(n: int) -> list[list[list[str]]]:\n    \"\"\"\u6c42\u89e3 n \u7687\u540e\"\"\"\n    # \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n    state = [[\"#\" for _ in range(n)] for _ in range(n)]\n    cols = [False] * n  # \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n    diags1 = [False] * (2 * n - 1)  # \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    diags2 = [False] * (2 * n - 1)  # \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    res = []\n    backtrack(0, n, state, res, cols, diags1, diags2)\n\n    return res\n
        n_queens.cpp
        /* \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e */\nvoid backtrack(int row, int n, vector<vector<string>> &state, vector<vector<vector<string>>> &res, vector<bool> &cols,\n               vector<bool> &diags1, vector<bool> &diags2) {\n    // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (row == n) {\n        res.push_back(state);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u5217\n    for (int col = 0; col < n; col++) {\n        // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n            state[row][col] = \"Q\";\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // \u653e\u7f6e\u4e0b\u4e00\u884c\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n            state[row][col] = \"#\";\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nvector<vector<vector<string>>> nQueens(int n) {\n    // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n    vector<vector<string>> state(n, vector<string>(n, \"#\"));\n    vector<bool> cols(n, false);           // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n    vector<bool> diags1(2 * n - 1, false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    vector<bool> diags2(2 * n - 1, false); // \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    vector<vector<vector<string>>> res;\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n\n    return res;\n}\n
        n_queens.java
        /* \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e */\nvoid backtrack(int row, int n, List<List<String>> state, List<List<List<String>>> res,\n        boolean[] cols, boolean[] diags1, boolean[] diags2) {\n    // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (row == n) {\n        List<List<String>> copyState = new ArrayList<>();\n        for (List<String> sRow : state) {\n            copyState.add(new ArrayList<>(sRow));\n        }\n        res.add(copyState);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u5217\n    for (int col = 0; col < n; col++) {\n        // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n            state.get(row).set(col, \"Q\");\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // \u653e\u7f6e\u4e0b\u4e00\u884c\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n            state.get(row).set(col, \"#\");\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nList<List<List<String>>> nQueens(int n) {\n    // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n    List<List<String>> state = new ArrayList<>();\n    for (int i = 0; i < n; i++) {\n        List<String> row = new ArrayList<>();\n        for (int j = 0; j < n; j++) {\n            row.add(\"#\");\n        }\n        state.add(row);\n    }\n    boolean[] cols = new boolean[n]; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n    boolean[] diags1 = new boolean[2 * n - 1]; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    boolean[] diags2 = new boolean[2 * n - 1]; // \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    List<List<List<String>>> res = new ArrayList<>();\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n\n    return res;\n}\n
        n_queens.cs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e */\nvoid Backtrack(int row, int n, List<List<string>> state, List<List<List<string>>> res,\n        bool[] cols, bool[] diags1, bool[] diags2) {\n    // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (row == n) {\n        List<List<string>> copyState = [];\n        foreach (List<string> sRow in state) {\n            copyState.Add(new List<string>(sRow));\n        }\n        res.Add(copyState);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u5217\n    for (int col = 0; col < n; col++) {\n        // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n            state[row][col] = \"Q\";\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // \u653e\u7f6e\u4e0b\u4e00\u884c\n            Backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n            state[row][col] = \"#\";\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nList<List<List<string>>> NQueens(int n) {\n    // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n    List<List<string>> state = [];\n    for (int i = 0; i < n; i++) {\n        List<string> row = [];\n        for (int j = 0; j < n; j++) {\n            row.Add(\"#\");\n        }\n        state.Add(row);\n    }\n    bool[] cols = new bool[n]; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n    bool[] diags1 = new bool[2 * n - 1]; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    bool[] diags2 = new bool[2 * n - 1]; // \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    List<List<List<string>>> res = [];\n\n    Backtrack(0, n, state, res, cols, diags1, diags2);\n\n    return res;\n}\n
        n_queens.go
        /* \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e */\nfunc backtrack(row, n int, state *[][]string, res *[][][]string, cols, diags1, diags2 *[]bool) {\n    // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if row == n {\n        newState := make([][]string, len(*state))\n        for i, _ := range newState {\n            newState[i] = make([]string, len((*state)[0]))\n            copy(newState[i], (*state)[i])\n\n        }\n        *res = append(*res, newState)\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u5217\n    for col := 0; col < n; col++ {\n        // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n        diag1 := row - col + n - 1\n        diag2 := row + col\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n        if !(*cols)[col] && !(*diags1)[diag1] && !(*diags2)[diag2] {\n            // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n            (*state)[row][col] = \"Q\"\n            (*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = true, true, true\n            // \u653e\u7f6e\u4e0b\u4e00\u884c\n            backtrack(row+1, n, state, res, cols, diags1, diags2)\n            // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n            (*state)[row][col] = \"#\"\n            (*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = false, false, false\n        }\n    }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nfunc nQueens(n int) [][][]string {\n    // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n    state := make([][]string, n)\n    for i := 0; i < n; i++ {\n        row := make([]string, n)\n        for i := 0; i < n; i++ {\n            row[i] = \"#\"\n        }\n        state[i] = row\n    }\n    // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n    cols := make([]bool, n)\n    diags1 := make([]bool, 2*n-1)\n    diags2 := make([]bool, 2*n-1)\n    res := make([][][]string, 0)\n    backtrack(0, n, &state, &res, &cols, &diags1, &diags2)\n    return res\n}\n
        n_queens.swift
        /* \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e */\nfunc backtrack(row: Int, n: Int, state: inout [[String]], res: inout [[[String]]], cols: inout [Bool], diags1: inout [Bool], diags2: inout [Bool]) {\n    // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if row == n {\n        res.append(state)\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u5217\n    for col in 0 ..< n {\n        // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n        let diag1 = row - col + n - 1\n        let diag2 = row + col\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n        if !cols[col] && !diags1[diag1] && !diags2[diag2] {\n            // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n            state[row][col] = \"Q\"\n            cols[col] = true\n            diags1[diag1] = true\n            diags2[diag2] = true\n            // \u653e\u7f6e\u4e0b\u4e00\u884c\n            backtrack(row: row + 1, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)\n            // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n            state[row][col] = \"#\"\n            cols[col] = false\n            diags1[diag1] = false\n            diags2[diag2] = false\n        }\n    }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nfunc nQueens(n: Int) -> [[[String]]] {\n    // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n    var state = Array(repeating: Array(repeating: \"#\", count: n), count: n)\n    var cols = Array(repeating: false, count: n) // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n    var diags1 = Array(repeating: false, count: 2 * n - 1) // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    var diags2 = Array(repeating: false, count: 2 * n - 1) // \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    var res: [[[String]]] = []\n\n    backtrack(row: 0, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)\n\n    return res\n}\n
        n_queens.js
        /* \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e */\nfunction backtrack(row, n, state, res, cols, diags1, diags2) {\n    // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (row === n) {\n        res.push(state.map((row) => row.slice()));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u5217\n    for (let col = 0; col < n; col++) {\n        // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n        const diag1 = row - col + n - 1;\n        const diag2 = row + col;\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n            state[row][col] = 'Q';\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // \u653e\u7f6e\u4e0b\u4e00\u884c\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n            state[row][col] = '#';\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nfunction nQueens(n) {\n    // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n    const state = Array.from({ length: n }, () => Array(n).fill('#'));\n    const cols = Array(n).fill(false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n    const diags1 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    const diags2 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    const res = [];\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n    return res;\n}\n
        n_queens.ts
        /* \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e */\nfunction backtrack(\n    row: number,\n    n: number,\n    state: string[][],\n    res: string[][][],\n    cols: boolean[],\n    diags1: boolean[],\n    diags2: boolean[]\n): void {\n    // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (row === n) {\n        res.push(state.map((row) => row.slice()));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u5217\n    for (let col = 0; col < n; col++) {\n        // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n        const diag1 = row - col + n - 1;\n        const diag2 = row + col;\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n            state[row][col] = 'Q';\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // \u653e\u7f6e\u4e0b\u4e00\u884c\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n            state[row][col] = '#';\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nfunction nQueens(n: number): string[][][] {\n    // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n    const state = Array.from({ length: n }, () => Array(n).fill('#'));\n    const cols = Array(n).fill(false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n    const diags1 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    const diags2 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    const res: string[][][] = [];\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n    return res;\n}\n
        n_queens.dart
        /* \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e */\nvoid backtrack(\n  int row,\n  int n,\n  List<List<String>> state,\n  List<List<List<String>>> res,\n  List<bool> cols,\n  List<bool> diags1,\n  List<bool> diags2,\n) {\n  // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n  if (row == n) {\n    List<List<String>> copyState = [];\n    for (List<String> sRow in state) {\n      copyState.add(List.from(sRow));\n    }\n    res.add(copyState);\n    return;\n  }\n  // \u904d\u5386\u6240\u6709\u5217\n  for (int col = 0; col < n; col++) {\n    // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n    int diag1 = row - col + n - 1;\n    int diag2 = row + col;\n    // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n    if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n      // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n      state[row][col] = \"Q\";\n      cols[col] = true;\n      diags1[diag1] = true;\n      diags2[diag2] = true;\n      // \u653e\u7f6e\u4e0b\u4e00\u884c\n      backtrack(row + 1, n, state, res, cols, diags1, diags2);\n      // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n      state[row][col] = \"#\";\n      cols[col] = false;\n      diags1[diag1] = false;\n      diags2[diag2] = false;\n    }\n  }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nList<List<List<String>>> nQueens(int n) {\n  // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n  List<List<String>> state = List.generate(n, (index) => List.filled(n, \"#\"));\n  List<bool> cols = List.filled(n, false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n  List<bool> diags1 = List.filled(2 * n - 1, false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n  List<bool> diags2 = List.filled(2 * n - 1, false); // \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n  List<List<List<String>>> res = [];\n\n  backtrack(0, n, state, res, cols, diags1, diags2);\n\n  return res;\n}\n
        n_queens.rs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e */\nfn backtrack(\n    row: usize,\n    n: usize,\n    state: &mut Vec<Vec<String>>,\n    res: &mut Vec<Vec<Vec<String>>>,\n    cols: &mut [bool],\n    diags1: &mut [bool],\n    diags2: &mut [bool],\n) {\n    // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if row == n {\n        res.push(state.clone());\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u5217\n    for col in 0..n {\n        // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n        let diag1 = row + n - 1 - col;\n        let diag2 = row + col;\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n        if !cols[col] && !diags1[diag1] && !diags2[diag2] {\n            // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n            state[row][col] = \"Q\".into();\n            (cols[col], diags1[diag1], diags2[diag2]) = (true, true, true);\n            // \u653e\u7f6e\u4e0b\u4e00\u884c\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n            state[row][col] = \"#\".into();\n            (cols[col], diags1[diag1], diags2[diag2]) = (false, false, false);\n        }\n    }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nfn n_queens(n: usize) -> Vec<Vec<Vec<String>>> {\n    // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n    let mut state: Vec<Vec<String>> = vec![vec![\"#\".to_string(); n]; n];\n    let mut cols = vec![false; n]; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n    let mut diags1 = vec![false; 2 * n - 1]; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    let mut diags2 = vec![false; 2 * n - 1]; // \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    let mut res: Vec<Vec<Vec<String>>> = Vec::new();\n\n    backtrack(\n        0,\n        n,\n        &mut state,\n        &mut res,\n        &mut cols,\n        &mut diags1,\n        &mut diags2,\n    );\n\n    res\n}\n
        n_queens.c
        /* \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e */\nvoid backtrack(int row, int n, char state[MAX_SIZE][MAX_SIZE], char ***res, int *resSize, bool cols[MAX_SIZE],\n               bool diags1[2 * MAX_SIZE - 1], bool diags2[2 * MAX_SIZE - 1]) {\n    // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (row == n) {\n        res[*resSize] = (char **)malloc(sizeof(char *) * n);\n        for (int i = 0; i < n; ++i) {\n            res[*resSize][i] = (char *)malloc(sizeof(char) * (n + 1));\n            strcpy(res[*resSize][i], state[i]);\n        }\n        (*resSize)++;\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u5217\n    for (int col = 0; col < n; col++) {\n        // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n            state[row][col] = 'Q';\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // \u653e\u7f6e\u4e0b\u4e00\u884c\n            backtrack(row + 1, n, state, res, resSize, cols, diags1, diags2);\n            // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n            state[row][col] = '#';\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nchar ***nQueens(int n, int *returnSize) {\n    char state[MAX_SIZE][MAX_SIZE];\n    // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n    for (int i = 0; i < n; ++i) {\n        for (int j = 0; j < n; ++j) {\n            state[i][j] = '#';\n        }\n        state[i][n] = '\\0';\n    }\n    bool cols[MAX_SIZE] = {false};           // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n    bool diags1[2 * MAX_SIZE - 1] = {false}; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    bool diags2[2 * MAX_SIZE - 1] = {false}; // \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n\n    char ***res = (char ***)malloc(sizeof(char **) * MAX_SIZE);\n    *returnSize = 0;\n    backtrack(0, n, state, res, returnSize, cols, diags1, diags2);\n    return res;\n}\n
        n_queens.kt
        /* \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e */\nfun backtrack(\n    row: Int,\n    n: Int,\n    state: MutableList<MutableList<String>>,\n    res: MutableList<MutableList<MutableList<String>>?>,\n    cols: BooleanArray,\n    diags1: BooleanArray,\n    diags2: BooleanArray\n) {\n    // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (row == n) {\n        val copyState = mutableListOf<MutableList<String>>()\n        for (sRow in state) {\n            copyState.add(sRow.toMutableList())\n        }\n        res.add(copyState)\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u5217\n    for (col in 0..<n) {\n        // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n        val diag1 = row - col + n - 1\n        val diag2 = row + col\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n            state[row][col] = \"Q\"\n            diags2[diag2] = true\n            diags1[diag1] = diags2[diag2]\n            cols[col] = diags1[diag1]\n            // \u653e\u7f6e\u4e0b\u4e00\u884c\n            backtrack(row + 1, n, state, res, cols, diags1, diags2)\n            // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n            state[row][col] = \"#\"\n            diags2[diag2] = false\n            diags1[diag1] = diags2[diag2]\n            cols[col] = diags1[diag1]\n        }\n    }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nfun nQueens(n: Int): MutableList<MutableList<MutableList<String>>?> {\n    // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n    val state = mutableListOf<MutableList<String>>()\n    for (i in 0..<n) {\n        val row = mutableListOf<String>()\n        for (j in 0..<n) {\n            row.add(\"#\")\n        }\n        state.add(row)\n    }\n    val cols = BooleanArray(n) // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n    val diags1 = BooleanArray(2 * n - 1) // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    val diags2 = BooleanArray(2 * n - 1) // \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n    val res = mutableListOf<MutableList<MutableList<String>>?>()\n\n    backtrack(0, n, state, res, cols, diags1, diags2)\n\n    return res\n}\n
        n_queens.rb
        ### \u56de\u6eaf\u7b97\u6cd5\uff1an \u7687\u540e ###\ndef backtrack(row, n, state, res, cols, diags1, diags2)\n  # \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n  if row == n\n    res << state.map { |row| row.dup }\n    return\n  end\n\n  # \u904d\u5386\u6240\u6709\u5217\n  for col in 0...n\n    # \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\n    diag1 = row - col + n - 1\n    diag2 = row + col\n    # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n    if !cols[col] && !diags1[diag1] && !diags2[diag2]\n      # \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n      state[row][col] = \"Q\"\n      cols[col] = diags1[diag1] = diags2[diag2] = true\n      # \u653e\u7f6e\u4e0b\u4e00\u884c\n      backtrack(row + 1, n, state, res, cols, diags1, diags2)\n      # \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n      state[row][col] = \"#\"\n      cols[col] = diags1[diag1] = diags2[diag2] = false\n    end\n  end\nend\n\n### \u6c42\u89e3 n \u7687\u540e ###\ndef n_queens(n)\n  # \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n  state = Array.new(n) { Array.new(n, \"#\") }\n  cols = Array.new(n, false) # \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n  diags1 = Array.new(2 * n - 1, false) # \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n  diags2 = Array.new(2 * n - 1, false) # \u8bb0\u5f55\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n  res = []\n  backtrack(0, n, state, res, cols, diags1, diags2)\n\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u9010\u884c\u653e\u7f6e \\(n\\) \u6b21\uff0c\u8003\u8651\u5217\u7ea6\u675f\uff0c\u5219\u4ece\u7b2c\u4e00\u884c\u5230\u6700\u540e\u4e00\u884c\u5206\u522b\u6709 \\(n\\)\u3001\\(n-1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u4e2a\u9009\u62e9\uff0c\u4f7f\u7528 \\(O(n!)\\) \u65f6\u95f4\u3002\u5f53\u8bb0\u5f55\u89e3\u65f6\uff0c\u9700\u8981\u590d\u5236\u77e9\u9635 state \u5e76\u6dfb\u52a0\u8fdb res \uff0c\u590d\u5236\u64cd\u4f5c\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002\u56e0\u6b64\uff0c\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n! \\cdot n^2)\\) \u3002\u5b9e\u9645\u4e0a\uff0c\u6839\u636e\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u526a\u679d\u4e5f\u80fd\u591f\u5927\u5e45\u7f29\u5c0f\u641c\u7d22\u7a7a\u95f4\uff0c\u56e0\u800c\u641c\u7d22\u6548\u7387\u5f80\u5f80\u4f18\u4e8e\u4ee5\u4e0a\u65f6\u95f4\u590d\u6742\u5ea6\u3002

        \u6570\u7ec4 state \u4f7f\u7528 \\(O(n^2)\\) \u7a7a\u95f4\uff0c\u6570\u7ec4 cols\u3001diags1 \u548c diags2 \u7686\u4f7f\u7528 \\(O(n)\\) \u7a7a\u95f4\u3002\u6700\u5927\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002

        "},{"location":"chapter_backtracking/permutations_problem/","title":"13.2 \u00a0 \u5168\u6392\u5217\u95ee\u9898","text":"

        \u5168\u6392\u5217\u95ee\u9898\u662f\u56de\u6eaf\u7b97\u6cd5\u7684\u4e00\u4e2a\u5178\u578b\u5e94\u7528\u3002\u5b83\u7684\u5b9a\u4e49\u662f\u5728\u7ed9\u5b9a\u4e00\u4e2a\u96c6\u5408\uff08\u5982\u4e00\u4e2a\u6570\u7ec4\u6216\u5b57\u7b26\u4e32\uff09\u7684\u60c5\u51b5\u4e0b\uff0c\u627e\u51fa\u5176\u4e2d\u5143\u7d20\u7684\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u3002

        \u8868 13-2 \u5217\u4e3e\u4e86\u51e0\u4e2a\u793a\u4f8b\u6570\u636e\uff0c\u5305\u62ec\u8f93\u5165\u6570\u7ec4\u548c\u5bf9\u5e94\u7684\u6240\u6709\u6392\u5217\u3002

        \u8868 13-2 \u00a0 \u5168\u6392\u5217\u793a\u4f8b

        \u8f93\u5165\u6570\u7ec4 \u6240\u6709\u6392\u5217 \\([1]\\) \\([1]\\) \\([1, 2]\\) \\([1, 2], [2, 1]\\) \\([1, 2, 3]\\) \\([1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]\\)"},{"location":"chapter_backtracking/permutations_problem/#1321","title":"13.2.1 \u00a0 \u65e0\u76f8\u7b49\u5143\u7d20\u7684\u60c5\u51b5","text":"

        Question

        \u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u5176\u4e2d\u4e0d\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u8fd4\u56de\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u3002

        \u4ece\u56de\u6eaf\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u751f\u6210\u6392\u5217\u7684\u8fc7\u7a0b\u60f3\u8c61\u6210\u4e00\u7cfb\u5217\u9009\u62e9\u7684\u7ed3\u679c\u3002\u5047\u8bbe\u8f93\u5165\u6570\u7ec4\u4e3a \\([1, 2, 3]\\) \uff0c\u5982\u679c\u6211\u4eec\u5148\u9009\u62e9 \\(1\\) \uff0c\u518d\u9009\u62e9 \\(3\\) \uff0c\u6700\u540e\u9009\u62e9 \\(2\\) \uff0c\u5219\u83b7\u5f97\u6392\u5217 \\([1, 3, 2]\\) \u3002\u56de\u9000\u8868\u793a\u64a4\u9500\u4e00\u4e2a\u9009\u62e9\uff0c\u4e4b\u540e\u7ee7\u7eed\u5c1d\u8bd5\u5176\u4ed6\u9009\u62e9\u3002

        \u4ece\u56de\u6eaf\u4ee3\u7801\u7684\u89d2\u5ea6\u770b\uff0c\u5019\u9009\u96c6\u5408 choices \u662f\u8f93\u5165\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u72b6\u6001 state \u662f\u76f4\u81f3\u76ee\u524d\u5df2\u88ab\u9009\u62e9\u7684\u5143\u7d20\u3002\u8bf7\u6ce8\u610f\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ea\u5141\u8bb8\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u56e0\u6b64 state \u4e2d\u7684\u6240\u6709\u5143\u7d20\u90fd\u5e94\u8be5\u662f\u552f\u4e00\u7684\u3002

        \u5982\u56fe 13-5 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u641c\u7d22\u8fc7\u7a0b\u5c55\u5f00\u6210\u4e00\u68f5\u9012\u5f52\u6811\uff0c\u6811\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u5f53\u524d\u72b6\u6001 state \u3002\u4ece\u6839\u8282\u70b9\u5f00\u59cb\uff0c\u7ecf\u8fc7\u4e09\u8f6e\u9009\u62e9\u540e\u5230\u8fbe\u53f6\u8282\u70b9\uff0c\u6bcf\u4e2a\u53f6\u8282\u70b9\u90fd\u5bf9\u5e94\u4e00\u4e2a\u6392\u5217\u3002

        \u56fe 13-5 \u00a0 \u5168\u6392\u5217\u7684\u9012\u5f52\u6811

        "},{"location":"chapter_backtracking/permutations_problem/#1","title":"1. \u00a0 \u91cd\u590d\u9009\u62e9\u526a\u679d","text":"

        \u4e3a\u4e86\u5b9e\u73b0\u6bcf\u4e2a\u5143\u7d20\u53ea\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u6211\u4eec\u8003\u8651\u5f15\u5165\u4e00\u4e2a\u5e03\u5c14\u578b\u6570\u7ec4 selected \uff0c\u5176\u4e2d selected[i] \u8868\u793a choices[i] \u662f\u5426\u5df2\u88ab\u9009\u62e9\uff0c\u5e76\u57fa\u4e8e\u5b83\u5b9e\u73b0\u4ee5\u4e0b\u526a\u679d\u64cd\u4f5c\u3002

        • \u5728\u505a\u51fa\u9009\u62e9 choice[i] \u540e\uff0c\u6211\u4eec\u5c31\u5c06 selected[i] \u8d4b\u503c\u4e3a \\(\\text{True}\\) \uff0c\u4ee3\u8868\u5b83\u5df2\u88ab\u9009\u62e9\u3002
        • \u904d\u5386\u9009\u62e9\u5217\u8868 choices \u65f6\uff0c\u8df3\u8fc7\u6240\u6709\u5df2\u88ab\u9009\u62e9\u7684\u8282\u70b9\uff0c\u5373\u526a\u679d\u3002

        \u5982\u56fe 13-6 \u6240\u793a\uff0c\u5047\u8bbe\u6211\u4eec\u7b2c\u4e00\u8f6e\u9009\u62e9 1 \uff0c\u7b2c\u4e8c\u8f6e\u9009\u62e9 3 \uff0c\u7b2c\u4e09\u8f6e\u9009\u62e9 2 \uff0c\u5219\u9700\u8981\u5728\u7b2c\u4e8c\u8f6e\u526a\u6389\u5143\u7d20 1 \u7684\u5206\u652f\uff0c\u5728\u7b2c\u4e09\u8f6e\u526a\u6389\u5143\u7d20 1 \u548c\u5143\u7d20 3 \u7684\u5206\u652f\u3002

        \u56fe 13-6 \u00a0 \u5168\u6392\u5217\u526a\u679d\u793a\u4f8b

        \u89c2\u5bdf\u56fe 13-6 \u53d1\u73b0\uff0c\u8be5\u526a\u679d\u64cd\u4f5c\u5c06\u641c\u7d22\u7a7a\u95f4\u5927\u5c0f\u4ece \\(O(n^n)\\) \u51cf\u5c0f\u81f3 \\(O(n!)\\) \u3002

        "},{"location":"chapter_backtracking/permutations_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u60f3\u6e05\u695a\u4ee5\u4e0a\u4fe1\u606f\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u6846\u67b6\u4ee3\u7801\u4e2d\u505a\u201c\u5b8c\u5f62\u586b\u7a7a\u201d\u4e86\u3002\u4e3a\u4e86\u7f29\u77ed\u6574\u4f53\u4ee3\u7801\uff0c\u6211\u4eec\u4e0d\u5355\u72ec\u5b9e\u73b0\u6846\u67b6\u4ee3\u7801\u4e2d\u7684\u5404\u4e2a\u51fd\u6570\uff0c\u800c\u662f\u5c06\u5b83\u4eec\u5c55\u5f00\u5728 backtrack() \u51fd\u6570\u4e2d\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby permutations_i.py
        def backtrack(\n    state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]\n):\n    \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I\"\"\"\n    # \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if len(state) == len(choices):\n        res.append(list(state))\n        return\n    # \u904d\u5386\u6240\u6709\u9009\u62e9\n    for i, choice in enumerate(choices):\n        # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n        if not selected[i]:\n            # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            selected[i] = True\n            state.append(choice)\n            # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res)\n            # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = False\n            state.pop()\n\ndef permutations_i(nums: list[int]) -> list[list[int]]:\n    \"\"\"\u5168\u6392\u5217 I\"\"\"\n    res = []\n    backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\n    return res\n
        permutations_i.cpp
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &selected, vector<vector<int>> &res) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.size() == choices.size()) {\n        res.push_back(state);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (int i = 0; i < choices.size(); i++) {\n        int choice = choices[i];\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n        if (!selected[i]) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            selected[i] = true;\n            state.push_back(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.pop_back();\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 I */\nvector<vector<int>> permutationsI(vector<int> nums) {\n    vector<int> state;\n    vector<bool> selected(nums.size(), false);\n    vector<vector<int>> res;\n    backtrack(state, nums, selected, res);\n    return res;\n}\n
        permutations_i.java
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.size() == choices.length) {\n        res.add(new ArrayList<Integer>(state));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (int i = 0; i < choices.length; i++) {\n        int choice = choices[i];\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n        if (!selected[i]) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            selected[i] = true;\n            state.add(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.remove(state.size() - 1);\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 I */\nList<List<Integer>> permutationsI(int[] nums) {\n    List<List<Integer>> res = new ArrayList<List<Integer>>();\n    backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);\n    return res;\n}\n
        permutations_i.cs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.Count == choices.Length) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (int i = 0; i < choices.Length; i++) {\n        int choice = choices[i];\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n        if (!selected[i]) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            selected[i] = true;\n            state.Add(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            Backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.RemoveAt(state.Count - 1);\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 I */\nList<List<int>> PermutationsI(int[] nums) {\n    List<List<int>> res = [];\n    Backtrack([], nums, new bool[nums.Length], res);\n    return res;\n}\n
        permutations_i.go
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunc backtrackI(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if len(*state) == len(*choices) {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for i := 0; i < len(*choices); i++ {\n        choice := (*choices)[i]\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n        if !(*selected)[i] {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            (*selected)[i] = true\n            *state = append(*state, choice)\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrackI(state, choices, selected, res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            (*selected)[i] = false\n            *state = (*state)[:len(*state)-1]\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 I */\nfunc permutationsI(nums []int) [][]int {\n    res := make([][]int, 0)\n    state := make([]int, 0)\n    selected := make([]bool, len(nums))\n    backtrackI(&state, &nums, &selected, &res)\n    return res\n}\n
        permutations_i.swift
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunc backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if state.count == choices.count {\n        res.append(state)\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (i, choice) in choices.enumerated() {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n        if !selected[i] {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            selected[i] = true\n            state.append(choice)\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state: &state, choices: choices, selected: &selected, res: &res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false\n            state.removeLast()\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 I */\nfunc permutationsI(nums: [Int]) -> [[Int]] {\n    var state: [Int] = []\n    var selected = Array(repeating: false, count: nums.count)\n    var res: [[Int]] = []\n    backtrack(state: &state, choices: nums, selected: &selected, res: &res)\n    return res\n}\n
        permutations_i.js
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunction backtrack(state, choices, selected, res) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.length === choices.length) {\n        res.push([...state]);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    choices.forEach((choice, i) => {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n        if (!selected[i]) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            selected[i] = true;\n            state.push(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.pop();\n        }\n    });\n}\n\n/* \u5168\u6392\u5217 I */\nfunction permutationsI(nums) {\n    const res = [];\n    backtrack([], nums, Array(nums.length).fill(false), res);\n    return res;\n}\n
        permutations_i.ts
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunction backtrack(\n    state: number[],\n    choices: number[],\n    selected: boolean[],\n    res: number[][]\n): void {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.length === choices.length) {\n        res.push([...state]);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    choices.forEach((choice, i) => {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n        if (!selected[i]) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            selected[i] = true;\n            state.push(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.pop();\n        }\n    });\n}\n\n/* \u5168\u6392\u5217 I */\nfunction permutationsI(nums: number[]): number[][] {\n    const res: number[][] = [];\n    backtrack([], nums, Array(nums.length).fill(false), res);\n    return res;\n}\n
        permutations_i.dart
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(\n  List<int> state,\n  List<int> choices,\n  List<bool> selected,\n  List<List<int>> res,\n) {\n  // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n  if (state.length == choices.length) {\n    res.add(List.from(state));\n    return;\n  }\n  // \u904d\u5386\u6240\u6709\u9009\u62e9\n  for (int i = 0; i < choices.length; i++) {\n    int choice = choices[i];\n    // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n    if (!selected[i]) {\n      // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n      selected[i] = true;\n      state.add(choice);\n      // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n      backtrack(state, choices, selected, res);\n      // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n      selected[i] = false;\n      state.removeLast();\n    }\n  }\n}\n\n/* \u5168\u6392\u5217 I */\nList<List<int>> permutationsI(List<int> nums) {\n  List<List<int>> res = [];\n  backtrack([], nums, List.filled(nums.length, false), res);\n  return res;\n}\n
        permutations_i.rs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if state.len() == choices.len() {\n        res.push(state);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for i in 0..choices.len() {\n        let choice = choices[i];\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n        if !selected[i] {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            selected[i] = true;\n            state.push(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state.clone(), choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.pop();\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 I */\nfn permutations_i(nums: &mut [i32]) -> Vec<Vec<i32>> {\n    let mut res = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    backtrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);\n    res\n}\n
        permutations_i.c
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(int *state, int stateSize, int *choices, int choicesSize, bool *selected, int **res, int *resSize) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (stateSize == choicesSize) {\n        res[*resSize] = (int *)malloc(choicesSize * sizeof(int));\n        for (int i = 0; i < choicesSize; i++) {\n            res[*resSize][i] = state[i];\n        }\n        (*resSize)++;\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (int i = 0; i < choicesSize; i++) {\n        int choice = choices[i];\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n        if (!selected[i]) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            selected[i] = true;\n            state[stateSize] = choice;\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, stateSize + 1, choices, choicesSize, selected, res, resSize);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 I */\nint **permutationsI(int *nums, int numsSize, int *returnSize) {\n    int *state = (int *)malloc(numsSize * sizeof(int));\n    bool *selected = (bool *)malloc(numsSize * sizeof(bool));\n    for (int i = 0; i < numsSize; i++) {\n        selected[i] = false;\n    }\n    int **res = (int **)malloc(MAX_SIZE * sizeof(int *));\n    *returnSize = 0;\n\n    backtrack(state, 0, nums, numsSize, selected, res, returnSize);\n\n    free(state);\n    free(selected);\n\n    return res;\n}\n
        permutations_i.kt
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfun backtrack(\n    state: MutableList<Int>,\n    choices: IntArray,\n    selected: BooleanArray,\n    res: MutableList<MutableList<Int>?>\n) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.size == choices.size) {\n        res.add(state.toMutableList())\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (i in choices.indices) {\n        val choice = choices[i]\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n        if (!selected[i]) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            selected[i] = true\n            state.add(choice)\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false\n            state.removeAt(state.size - 1)\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 I */\nfun permutationsI(nums: IntArray): MutableList<MutableList<Int>?> {\n    val res = mutableListOf<MutableList<Int>?>()\n    backtrack(mutableListOf(), nums, BooleanArray(nums.size), res)\n    return res\n}\n
        permutations_i.rb
        ### \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I ###\ndef backtrack(state, choices, selected, res)\n  # \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n  if state.length == choices.length\n    res << state.dup\n    return\n  end\n\n  # \u904d\u5386\u6240\u6709\u9009\u62e9\n  choices.each_with_index do |choice, i|\n    # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n    unless selected[i]\n      # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n      selected[i] = true\n      state << choice\n      # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n      backtrack(state, choices, selected, res)\n      # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n      selected[i] = false\n      state.pop\n    end\n  end\nend\n\n### \u5168\u6392\u5217 I ###\ndef permutations_i(nums)\n  res = []\n  backtrack([], nums, Array.new(nums.length, false), res)\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_backtracking/permutations_problem/#1322","title":"13.2.2 \u00a0 \u8003\u8651\u76f8\u7b49\u5143\u7d20\u7684\u60c5\u51b5","text":"

        Question

        \u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u8fd4\u56de\u6240\u6709\u4e0d\u91cd\u590d\u7684\u6392\u5217\u3002

        \u5047\u8bbe\u8f93\u5165\u6570\u7ec4\u4e3a \\([1, 1, 2]\\) \u3002\u4e3a\u4e86\u65b9\u4fbf\u533a\u5206\u4e24\u4e2a\u91cd\u590d\u5143\u7d20 \\(1\\) \uff0c\u6211\u4eec\u5c06\u7b2c\u4e8c\u4e2a \\(1\\) \u8bb0\u4e3a \\(\\hat{1}\\) \u3002

        \u5982\u56fe 13-7 \u6240\u793a\uff0c\u4e0a\u8ff0\u65b9\u6cd5\u751f\u6210\u7684\u6392\u5217\u6709\u4e00\u534a\u662f\u91cd\u590d\u7684\u3002

        \u56fe 13-7 \u00a0 \u91cd\u590d\u6392\u5217

        \u90a3\u4e48\u5982\u4f55\u53bb\u9664\u91cd\u590d\u7684\u6392\u5217\u5462\uff1f\u6700\u76f4\u63a5\u5730\uff0c\u8003\u8651\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u96c6\u5408\uff0c\u76f4\u63a5\u5bf9\u6392\u5217\u7ed3\u679c\u8fdb\u884c\u53bb\u91cd\u3002\u7136\u800c\u8fd9\u6837\u505a\u4e0d\u591f\u4f18\u96c5\uff0c\u56e0\u4e3a\u751f\u6210\u91cd\u590d\u6392\u5217\u7684\u641c\u7d22\u5206\u652f\u6ca1\u6709\u5fc5\u8981\uff0c\u5e94\u5f53\u63d0\u524d\u8bc6\u522b\u5e76\u526a\u679d\uff0c\u8fd9\u6837\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u5347\u7b97\u6cd5\u6548\u7387\u3002

        "},{"location":"chapter_backtracking/permutations_problem/#1_1","title":"1. \u00a0 \u76f8\u7b49\u5143\u7d20\u526a\u679d","text":"

        \u89c2\u5bdf\u56fe 13-8 \uff0c\u5728\u7b2c\u4e00\u8f6e\u4e2d\uff0c\u9009\u62e9 \\(1\\) \u6216\u9009\u62e9 \\(\\hat{1}\\) \u662f\u7b49\u4ef7\u7684\uff0c\u5728\u8fd9\u4e24\u4e2a\u9009\u62e9\u4e4b\u4e0b\u751f\u6210\u7684\u6240\u6709\u6392\u5217\u90fd\u662f\u91cd\u590d\u7684\u3002\u56e0\u6b64\u5e94\u8be5\u628a \\(\\hat{1}\\) \u526a\u679d\u3002

        \u540c\u7406\uff0c\u5728\u7b2c\u4e00\u8f6e\u9009\u62e9 \\(2\\) \u4e4b\u540e\uff0c\u7b2c\u4e8c\u8f6e\u9009\u62e9\u4e2d\u7684 \\(1\\) \u548c \\(\\hat{1}\\) \u4e5f\u4f1a\u4ea7\u751f\u91cd\u590d\u5206\u652f\uff0c\u56e0\u6b64\u4e5f\u5e94\u5c06\u7b2c\u4e8c\u8f6e\u7684 \\(\\hat{1}\\) \u526a\u679d\u3002

        \u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u6211\u4eec\u7684\u76ee\u6807\u662f\u5728\u67d0\u4e00\u8f6e\u9009\u62e9\u4e2d\uff0c\u4fdd\u8bc1\u591a\u4e2a\u76f8\u7b49\u7684\u5143\u7d20\u4ec5\u88ab\u9009\u62e9\u4e00\u6b21\u3002

        \u56fe 13-8 \u00a0 \u91cd\u590d\u6392\u5217\u526a\u679d

        "},{"location":"chapter_backtracking/permutations_problem/#2_1","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u5728\u4e0a\u4e00\u9898\u7684\u4ee3\u7801\u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u8003\u8651\u5728\u6bcf\u4e00\u8f6e\u9009\u62e9\u4e2d\u5f00\u542f\u4e00\u4e2a\u54c8\u5e0c\u96c6\u5408 duplicated \uff0c\u7528\u4e8e\u8bb0\u5f55\u8be5\u8f6e\u4e2d\u5df2\u7ecf\u5c1d\u8bd5\u8fc7\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u91cd\u590d\u5143\u7d20\u526a\u679d\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby permutations_ii.py
        def backtrack(\n    state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]\n):\n    \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II\"\"\"\n    # \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if len(state) == len(choices):\n        res.append(list(state))\n        return\n    # \u904d\u5386\u6240\u6709\u9009\u62e9\n    duplicated = set[int]()\n    for i, choice in enumerate(choices):\n        # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n        if not selected[i] and choice not in duplicated:\n            # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            duplicated.add(choice)  # \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n            selected[i] = True\n            state.append(choice)\n            # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res)\n            # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = False\n            state.pop()\n\ndef permutations_ii(nums: list[int]) -> list[list[int]]:\n    \"\"\"\u5168\u6392\u5217 II\"\"\"\n    res = []\n    backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\n    return res\n
        permutations_ii.cpp
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &selected, vector<vector<int>> &res) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.size() == choices.size()) {\n        res.push_back(state);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    unordered_set<int> duplicated;\n    for (int i = 0; i < choices.size(); i++) {\n        int choice = choices[i];\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n        if (!selected[i] && duplicated.find(choice) == duplicated.end()) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            duplicated.emplace(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n            selected[i] = true;\n            state.push_back(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.pop_back();\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 II */\nvector<vector<int>> permutationsII(vector<int> nums) {\n    vector<int> state;\n    vector<bool> selected(nums.size(), false);\n    vector<vector<int>> res;\n    backtrack(state, nums, selected, res);\n    return res;\n}\n
        permutations_ii.java
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.size() == choices.length) {\n        res.add(new ArrayList<Integer>(state));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    Set<Integer> duplicated = new HashSet<Integer>();\n    for (int i = 0; i < choices.length; i++) {\n        int choice = choices[i];\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n        if (!selected[i] && !duplicated.contains(choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            duplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n            selected[i] = true;\n            state.add(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.remove(state.size() - 1);\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 II */\nList<List<Integer>> permutationsII(int[] nums) {\n    List<List<Integer>> res = new ArrayList<List<Integer>>();\n    backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);\n    return res;\n}\n
        permutations_ii.cs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.Count == choices.Length) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    HashSet<int> duplicated = [];\n    for (int i = 0; i < choices.Length; i++) {\n        int choice = choices[i];\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n        if (!selected[i] && !duplicated.Contains(choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            duplicated.Add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n            selected[i] = true;\n            state.Add(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            Backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.RemoveAt(state.Count - 1);\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 II */\nList<List<int>> PermutationsII(int[] nums) {\n    List<List<int>> res = [];\n    Backtrack([], nums, new bool[nums.Length], res);\n    return res;\n}\n
        permutations_ii.go
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunc backtrackII(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if len(*state) == len(*choices) {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    duplicated := make(map[int]struct{}, 0)\n    for i := 0; i < len(*choices); i++ {\n        choice := (*choices)[i]\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n        if _, ok := duplicated[choice]; !ok && !(*selected)[i] {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n            duplicated[choice] = struct{}{}\n            (*selected)[i] = true\n            *state = append(*state, choice)\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrackII(state, choices, selected, res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            (*selected)[i] = false\n            *state = (*state)[:len(*state)-1]\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 II */\nfunc permutationsII(nums []int) [][]int {\n    res := make([][]int, 0)\n    state := make([]int, 0)\n    selected := make([]bool, len(nums))\n    backtrackII(&state, &nums, &selected, &res)\n    return res\n}\n
        permutations_ii.swift
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunc backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if state.count == choices.count {\n        res.append(state)\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    var duplicated: Set<Int> = []\n    for (i, choice) in choices.enumerated() {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n        if !selected[i], !duplicated.contains(choice) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            duplicated.insert(choice) // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n            selected[i] = true\n            state.append(choice)\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state: &state, choices: choices, selected: &selected, res: &res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false\n            state.removeLast()\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 II */\nfunc permutationsII(nums: [Int]) -> [[Int]] {\n    var state: [Int] = []\n    var selected = Array(repeating: false, count: nums.count)\n    var res: [[Int]] = []\n    backtrack(state: &state, choices: nums, selected: &selected, res: &res)\n    return res\n}\n
        permutations_ii.js
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunction backtrack(state, choices, selected, res) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.length === choices.length) {\n        res.push([...state]);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    const duplicated = new Set();\n    choices.forEach((choice, i) => {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n        if (!selected[i] && !duplicated.has(choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            duplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n            selected[i] = true;\n            state.push(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.pop();\n        }\n    });\n}\n\n/* \u5168\u6392\u5217 II */\nfunction permutationsII(nums) {\n    const res = [];\n    backtrack([], nums, Array(nums.length).fill(false), res);\n    return res;\n}\n
        permutations_ii.ts
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunction backtrack(\n    state: number[],\n    choices: number[],\n    selected: boolean[],\n    res: number[][]\n): void {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.length === choices.length) {\n        res.push([...state]);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    const duplicated = new Set();\n    choices.forEach((choice, i) => {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n        if (!selected[i] && !duplicated.has(choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            duplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n            selected[i] = true;\n            state.push(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.pop();\n        }\n    });\n}\n\n/* \u5168\u6392\u5217 II */\nfunction permutationsII(nums: number[]): number[][] {\n    const res: number[][] = [];\n    backtrack([], nums, Array(nums.length).fill(false), res);\n    return res;\n}\n
        permutations_ii.dart
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(\n  List<int> state,\n  List<int> choices,\n  List<bool> selected,\n  List<List<int>> res,\n) {\n  // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n  if (state.length == choices.length) {\n    res.add(List.from(state));\n    return;\n  }\n  // \u904d\u5386\u6240\u6709\u9009\u62e9\n  Set<int> duplicated = {};\n  for (int i = 0; i < choices.length; i++) {\n    int choice = choices[i];\n    // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n    if (!selected[i] && !duplicated.contains(choice)) {\n      // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n      duplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n      selected[i] = true;\n      state.add(choice);\n      // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n      backtrack(state, choices, selected, res);\n      // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n      selected[i] = false;\n      state.removeLast();\n    }\n  }\n}\n\n/* \u5168\u6392\u5217 II */\nList<List<int>> permutationsII(List<int> nums) {\n  List<List<int>> res = [];\n  backtrack([], nums, List.filled(nums.length, false), res);\n  return res;\n}\n
        permutations_ii.rs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if state.len() == choices.len() {\n        res.push(state);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    let mut duplicated = HashSet::<i32>::new();\n    for i in 0..choices.len() {\n        let choice = choices[i];\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n        if !selected[i] && !duplicated.contains(&choice) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            duplicated.insert(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n            selected[i] = true;\n            state.push(choice);\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state.clone(), choices, selected, res);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n            state.pop();\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 II */\nfn permutations_ii(nums: &mut [i32]) -> Vec<Vec<i32>> {\n    let mut res = Vec::new();\n    backtrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);\n    res\n}\n
        permutations_ii.c
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(int *state, int stateSize, int *choices, int choicesSize, bool *selected, int **res, int *resSize) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (stateSize == choicesSize) {\n        res[*resSize] = (int *)malloc(choicesSize * sizeof(int));\n        for (int i = 0; i < choicesSize; i++) {\n            res[*resSize][i] = state[i];\n        }\n        (*resSize)++;\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    bool duplicated[MAX_SIZE] = {false};\n    for (int i = 0; i < choicesSize; i++) {\n        int choice = choices[i];\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n        if (!selected[i] && !duplicated[choice]) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            duplicated[choice] = true; // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n            selected[i] = true;\n            state[stateSize] = choice;\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, stateSize + 1, choices, choicesSize, selected, res, resSize);\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false;\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 II */\nint **permutationsII(int *nums, int numsSize, int *returnSize) {\n    int *state = (int *)malloc(numsSize * sizeof(int));\n    bool *selected = (bool *)malloc(numsSize * sizeof(bool));\n    for (int i = 0; i < numsSize; i++) {\n        selected[i] = false;\n    }\n    int **res = (int **)malloc(MAX_SIZE * sizeof(int *));\n    *returnSize = 0;\n\n    backtrack(state, 0, nums, numsSize, selected, res, returnSize);\n\n    free(state);\n    free(selected);\n\n    return res;\n}\n
        permutations_ii.kt
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfun backtrack(\n    state: MutableList<Int>,\n    choices: IntArray,\n    selected: BooleanArray,\n    res: MutableList<MutableList<Int>?>\n) {\n    // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (state.size == choices.size) {\n        res.add(state.toMutableList())\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    val duplicated = HashSet<Int>()\n    for (i in choices.indices) {\n        val choice = choices[i]\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n        if (!selected[i] && !duplicated.contains(choice)) {\n            // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n            duplicated.add(choice) // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n            selected[i] = true\n            state.add(choice)\n            // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n            backtrack(state, choices, selected, res)\n            // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n            selected[i] = false\n            state.removeAt(state.size - 1)\n        }\n    }\n}\n\n/* \u5168\u6392\u5217 II */\nfun permutationsII(nums: IntArray): MutableList<MutableList<Int>?> {\n    val res = mutableListOf<MutableList<Int>?>()\n    backtrack(mutableListOf(), nums, BooleanArray(nums.size), res)\n    return res\n}\n
        permutations_ii.rb
        ### \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II ###\ndef backtrack(state, choices, selected, res)\n  # \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n  if state.length == choices.length\n    res << state.dup\n    return\n  end\n\n  # \u904d\u5386\u6240\u6709\u9009\u62e9\n  duplicated = Set.new\n  choices.each_with_index do |choice, i|\n    # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n    if !selected[i] && !duplicated.include?(choice)\n      # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n      duplicated.add(choice)\n      selected[i] = true\n      state << choice\n      # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n      backtrack(state, choices, selected, res)\n      # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n      selected[i] = false\n      state.pop\n    end\n  end\nend\n\n### \u5168\u6392\u5217 II ###\ndef permutations_ii(nums)\n  res = []\n  backtrack([], nums, Array.new(nums.length, false), res)\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5047\u8bbe\u5143\u7d20\u4e24\u4e24\u4e4b\u95f4\u4e92\u4e0d\u76f8\u540c\uff0c\u5219 \\(n\\) \u4e2a\u5143\u7d20\u5171\u6709 \\(n!\\) \u79cd\u6392\u5217\uff08\u9636\u4e58\uff09\uff1b\u5728\u8bb0\u5f55\u7ed3\u679c\u65f6\uff0c\u9700\u8981\u590d\u5236\u957f\u5ea6\u4e3a \\(n\\) \u7684\u5217\u8868\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n!n)\\) \u3002

        \u6700\u5927\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002selected \u4f7f\u7528 \\(O(n)\\) \u7a7a\u95f4\u3002\u540c\u4e00\u65f6\u523b\u6700\u591a\u5171\u6709 \\(n\\) \u4e2a duplicated \uff0c\u4f7f\u7528 \\(O(n^2)\\) \u7a7a\u95f4\u3002\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002

        "},{"location":"chapter_backtracking/permutations_problem/#3","title":"3. \u00a0 \u4e24\u79cd\u526a\u679d\u5bf9\u6bd4","text":"

        \u8bf7\u6ce8\u610f\uff0c\u867d\u7136 selected \u548c duplicated \u90fd\u7528\u4e8e\u526a\u679d\uff0c\u4f46\u4e24\u8005\u7684\u76ee\u6807\u4e0d\u540c\u3002

        • \u91cd\u590d\u9009\u62e9\u526a\u679d\uff1a\u6574\u4e2a\u641c\u7d22\u8fc7\u7a0b\u4e2d\u53ea\u6709\u4e00\u4e2a selected \u3002\u5b83\u8bb0\u5f55\u7684\u662f\u5f53\u524d\u72b6\u6001\u4e2d\u5305\u542b\u54ea\u4e9b\u5143\u7d20\uff0c\u5176\u4f5c\u7528\u662f\u907f\u514d\u67d0\u4e2a\u5143\u7d20\u5728 state \u4e2d\u91cd\u590d\u51fa\u73b0\u3002
        • \u76f8\u7b49\u5143\u7d20\u526a\u679d\uff1a\u6bcf\u8f6e\u9009\u62e9\uff08\u6bcf\u4e2a\u8c03\u7528\u7684 backtrack \u51fd\u6570\uff09\u90fd\u5305\u542b\u4e00\u4e2a duplicated \u3002\u5b83\u8bb0\u5f55\u7684\u662f\u5728\u672c\u8f6e\u904d\u5386\uff08for \u5faa\u73af\uff09\u4e2d\u54ea\u4e9b\u5143\u7d20\u5df2\u88ab\u9009\u62e9\u8fc7\uff0c\u5176\u4f5c\u7528\u662f\u4fdd\u8bc1\u76f8\u7b49\u5143\u7d20\u53ea\u88ab\u9009\u62e9\u4e00\u6b21\u3002

        \u56fe 13-9 \u5c55\u793a\u4e86\u4e24\u4e2a\u526a\u679d\u6761\u4ef6\u7684\u751f\u6548\u8303\u56f4\u3002\u6ce8\u610f\uff0c\u6811\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u9009\u62e9\uff0c\u4ece\u6839\u8282\u70b9\u5230\u53f6\u8282\u70b9\u7684\u8def\u5f84\u4e0a\u7684\u5404\u4e2a\u8282\u70b9\u6784\u6210\u4e00\u4e2a\u6392\u5217\u3002

        \u56fe 13-9 \u00a0 \u4e24\u79cd\u526a\u679d\u6761\u4ef6\u7684\u4f5c\u7528\u8303\u56f4

        "},{"location":"chapter_backtracking/subset_sum_problem/","title":"13.3 \u00a0 \u5b50\u96c6\u548c\u95ee\u9898","text":""},{"location":"chapter_backtracking/subset_sum_problem/#1331","title":"13.3.1 \u00a0 \u65e0\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"

        Question

        \u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4 nums \u548c\u4e00\u4e2a\u76ee\u6807\u6b63\u6574\u6570 target \uff0c\u8bf7\u627e\u51fa\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\uff0c\u4f7f\u5f97\u7ec4\u5408\u4e2d\u7684\u5143\u7d20\u548c\u7b49\u4e8e target \u3002\u7ed9\u5b9a\u6570\u7ec4\u65e0\u91cd\u590d\u5143\u7d20\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ef\u4ee5\u88ab\u9009\u53d6\u591a\u6b21\u3002\u8bf7\u4ee5\u5217\u8868\u5f62\u5f0f\u8fd4\u56de\u8fd9\u4e9b\u7ec4\u5408\uff0c\u5217\u8868\u4e2d\u4e0d\u5e94\u5305\u542b\u91cd\u590d\u7ec4\u5408\u3002

        \u4f8b\u5982\uff0c\u8f93\u5165\u96c6\u5408 \\(\\{3, 4, 5\\}\\) \u548c\u76ee\u6807\u6574\u6570 \\(9\\) \uff0c\u89e3\u4e3a \\(\\{3, 3, 3\\}, \\{4, 5\\}\\) \u3002\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u4e24\u70b9\u3002

        • \u8f93\u5165\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u88ab\u65e0\u9650\u6b21\u91cd\u590d\u9009\u53d6\u3002
        • \u5b50\u96c6\u4e0d\u533a\u5206\u5143\u7d20\u987a\u5e8f\uff0c\u6bd4\u5982 \\(\\{4, 5\\}\\) \u548c \\(\\{5, 4\\}\\) \u662f\u540c\u4e00\u4e2a\u5b50\u96c6\u3002
        "},{"location":"chapter_backtracking/subset_sum_problem/#1","title":"1. \u00a0 \u53c2\u8003\u5168\u6392\u5217\u89e3\u6cd5","text":"

        \u7c7b\u4f3c\u4e8e\u5168\u6392\u5217\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u5b50\u96c6\u7684\u751f\u6210\u8fc7\u7a0b\u60f3\u8c61\u6210\u4e00\u7cfb\u5217\u9009\u62e9\u7684\u7ed3\u679c\uff0c\u5e76\u5728\u9009\u62e9\u8fc7\u7a0b\u4e2d\u5b9e\u65f6\u66f4\u65b0\u201c\u5143\u7d20\u548c\u201d\uff0c\u5f53\u5143\u7d20\u548c\u7b49\u4e8e target \u65f6\uff0c\u5c31\u5c06\u5b50\u96c6\u8bb0\u5f55\u81f3\u7ed3\u679c\u5217\u8868\u3002

        \u800c\u4e0e\u5168\u6392\u5217\u95ee\u9898\u4e0d\u540c\u7684\u662f\uff0c\u672c\u9898\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u88ab\u65e0\u9650\u6b21\u9009\u53d6\uff0c\u56e0\u6b64\u65e0\u987b\u501f\u52a9 selected \u5e03\u5c14\u5217\u8868\u6765\u8bb0\u5f55\u5143\u7d20\u662f\u5426\u5df2\u88ab\u9009\u62e9\u3002\u6211\u4eec\u53ef\u4ee5\u5bf9\u5168\u6392\u5217\u4ee3\u7801\u8fdb\u884c\u5c0f\u5e45\u4fee\u6539\uff0c\u521d\u6b65\u5f97\u5230\u89e3\u9898\u4ee3\u7801\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby subset_sum_i_naive.py
        def backtrack(\n    state: list[int],\n    target: int,\n    total: int,\n    choices: list[int],\n    res: list[list[int]],\n):\n    \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I\"\"\"\n    # \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if total == target:\n        res.append(list(state))\n        return\n    # \u904d\u5386\u6240\u6709\u9009\u62e9\n    for i in range(len(choices)):\n        # \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n        if total + choices[i] > target:\n            continue\n        # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n        state.append(choices[i])\n        # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target, total + choices[i], choices, res)\n        # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop()\n\ndef subset_sum_i_naive(nums: list[int], target: int) -> list[list[int]]:\n    \"\"\"\u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09\"\"\"\n    state = []  # \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    total = 0  # \u5b50\u96c6\u548c\n    res = []  # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, total, nums, res)\n    return res\n
        subset_sum_i_naive.cpp
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(vector<int> &state, int target, int total, vector<int> &choices, vector<vector<int>> &res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (total == target) {\n        res.push_back(state);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (size_t i = 0; i < choices.size(); i++) {\n        // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n        state.push_back(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target, total + choices[i], choices, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop_back();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nvector<vector<int>> subsetSumINaive(vector<int> &nums, int target) {\n    vector<int> state;       // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    int total = 0;           // \u5b50\u96c6\u548c\n    vector<vector<int>> res; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
        subset_sum_i_naive.java
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(List<Integer> state, int target, int total, int[] choices, List<List<Integer>> res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (total == target) {\n        res.add(new ArrayList<>(state));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (int i = 0; i < choices.length; i++) {\n        // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n        state.add(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target, total + choices[i], choices, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.remove(state.size() - 1);\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nList<List<Integer>> subsetSumINaive(int[] nums, int target) {\n    List<Integer> state = new ArrayList<>(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    int total = 0; // \u5b50\u96c6\u548c\n    List<List<Integer>> res = new ArrayList<>(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
        subset_sum_i_naive.cs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid Backtrack(List<int> state, int target, int total, int[] choices, List<List<int>> res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (total == target) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (int i = 0; i < choices.Length; i++) {\n        // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n        state.Add(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        Backtrack(state, target, total + choices[i], choices, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.RemoveAt(state.Count - 1);\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nList<List<int>> SubsetSumINaive(int[] nums, int target) {\n    List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    int total = 0; // \u5b50\u96c6\u548c\n    List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    Backtrack(state, target, total, nums, res);\n    return res;\n}\n
        subset_sum_i_naive.go
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrackSubsetSumINaive(total, target int, state, choices *[]int, res *[][]int) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if target == total {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for i := 0; i < len(*choices); i++ {\n        // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n        if total+(*choices)[i] > target {\n            continue\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n        *state = append(*state, (*choices)[i])\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrackSubsetSumINaive(total+(*choices)[i], target, state, choices, res)\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        *state = (*state)[:len(*state)-1]\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunc subsetSumINaive(nums []int, target int) [][]int {\n    state := make([]int, 0) // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    total := 0              // \u5b50\u96c6\u548c\n    res := make([][]int, 0) // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrackSubsetSumINaive(total, target, &state, &nums, &res)\n    return res\n}\n
        subset_sum_i_naive.swift
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrack(state: inout [Int], target: Int, total: Int, choices: [Int], res: inout [[Int]]) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if total == target {\n        res.append(state)\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for i in choices.indices {\n        // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n        if total + choices[i] > target {\n            continue\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n        state.append(choices[i])\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state: &state, target: target, total: total + choices[i], choices: choices, res: &res)\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.removeLast()\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunc subsetSumINaive(nums: [Int], target: Int) -> [[Int]] {\n    var state: [Int] = [] // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    let total = 0 // \u5b50\u96c6\u548c\n    var res: [[Int]] = [] // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state: &state, target: target, total: total, choices: nums, res: &res)\n    return res\n}\n
        subset_sum_i_naive.js
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(state, target, total, choices, res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (total === target) {\n        res.push([...state]);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (let i = 0; i < choices.length; i++) {\n        // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n        state.push(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target, total + choices[i], choices, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunction subsetSumINaive(nums, target) {\n    const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    const total = 0; // \u5b50\u96c6\u548c\n    const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
        subset_sum_i_naive.ts
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(\n    state: number[],\n    target: number,\n    total: number,\n    choices: number[],\n    res: number[][]\n): void {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (total === target) {\n        res.push([...state]);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (let i = 0; i < choices.length; i++) {\n        // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n        state.push(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target, total + choices[i], choices, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunction subsetSumINaive(nums: number[], target: number): number[][] {\n    const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    const total = 0; // \u5b50\u96c6\u548c\n    const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
        subset_sum_i_naive.dart
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(\n  List<int> state,\n  int target,\n  int total,\n  List<int> choices,\n  List<List<int>> res,\n) {\n  // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n  if (total == target) {\n    res.add(List.from(state));\n    return;\n  }\n  // \u904d\u5386\u6240\u6709\u9009\u62e9\n  for (int i = 0; i < choices.length; i++) {\n    // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n    if (total + choices[i] > target) {\n      continue;\n    }\n    // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n    state.add(choices[i]);\n    // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n    backtrack(state, target, total + choices[i], choices, res);\n    // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n    state.removeLast();\n  }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nList<List<int>> subsetSumINaive(List<int> nums, int target) {\n  List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n  int total = 0; // \u5143\u7d20\u548c\n  List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n  backtrack(state, target, total, nums, res);\n  return res;\n}\n
        subset_sum_i_naive.rs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfn backtrack(\n    state: &mut Vec<i32>,\n    target: i32,\n    total: i32,\n    choices: &[i32],\n    res: &mut Vec<Vec<i32>>,\n) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if total == target {\n        res.push(state.clone());\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for i in 0..choices.len() {\n        // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n        if total + choices[i] > target {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n        state.push(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target, total + choices[i], choices, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfn subset_sum_i_naive(nums: &[i32], target: i32) -> Vec<Vec<i32>> {\n    let mut state = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    let total = 0; // \u5b50\u96c6\u548c\n    let mut res = Vec::new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(&mut state, target, total, nums, &mut res);\n    res\n}\n
        subset_sum_i_naive.c
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(int target, int total, int *choices, int choicesSize) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (total == target) {\n        for (int i = 0; i < stateSize; i++) {\n            res[resSize][i] = state[i];\n        }\n        resColSizes[resSize++] = stateSize;\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (int i = 0; i < choicesSize; i++) {\n        // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n        state[stateSize++] = choices[i];\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(target, total + choices[i], choices, choicesSize);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        stateSize--;\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nvoid subsetSumINaive(int *nums, int numsSize, int target) {\n    resSize = 0; // \u521d\u59cb\u5316\u89e3\u7684\u6570\u91cf\u4e3a0\n    backtrack(target, 0, nums, numsSize);\n}\n
        subset_sum_i_naive.kt
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfun backtrack(\n    state: MutableList<Int>,\n    target: Int,\n    total: Int,\n    choices: IntArray,\n    res: MutableList<MutableList<Int>?>\n) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (total == target) {\n        res.add(state.toMutableList())\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (i in choices.indices) {\n        // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n        if (total + choices[i] > target) {\n            continue\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n        state.add(choices[i])\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target, total + choices[i], choices, res)\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.removeAt(state.size - 1)\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfun subsetSumINaive(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {\n    val state = mutableListOf<Int>() // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    val total = 0 // \u5b50\u96c6\u548c\n    val res = mutableListOf<MutableList<Int>?>() // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, total, nums, res)\n    return res\n}\n
        subset_sum_i_naive.rb
        ### \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I ###\ndef backtrack(state, target, total, choices, res)\n  # \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n  if total == target\n    res << state.dup\n    return\n  end\n\n  # \u904d\u5386\u6240\u6709\u9009\u62e9\n  for i in 0...choices.length\n    # \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n    next if total + choices[i] > target\n    # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n    state << choices[i]\n    # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n    backtrack(state, target, total + choices[i], choices, res)\n    # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n    state.pop\n  end\nend\n\n### \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09###\ndef subset_sum_i_naive(nums, target)\n  state = [] # \u72b6\u6001\uff08\u5b50\u96c6\uff09\n  total = 0 # \u5b50\u96c6\u548c\n  res = [] # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n  backtrack(state, target, total, nums, res)\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5411\u4ee5\u4e0a\u4ee3\u7801\u8f93\u5165\u6570\u7ec4 \\([3, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a \\([3, 3, 3], [4, 5], [5, 4]\\) \u3002\u867d\u7136\u6210\u529f\u627e\u51fa\u4e86\u6240\u6709\u548c\u4e3a \\(9\\) \u7684\u5b50\u96c6\uff0c\u4f46\u5176\u4e2d\u5b58\u5728\u91cd\u590d\u7684\u5b50\u96c6 \\([4, 5]\\) \u548c \\([5, 4]\\) \u3002

        \u8fd9\u662f\u56e0\u4e3a\u641c\u7d22\u8fc7\u7a0b\u662f\u533a\u5206\u9009\u62e9\u987a\u5e8f\u7684\uff0c\u7136\u800c\u5b50\u96c6\u4e0d\u533a\u5206\u9009\u62e9\u987a\u5e8f\u3002\u5982\u56fe 13-10 \u6240\u793a\uff0c\u5148\u9009 \\(4\\) \u540e\u9009 \\(5\\) \u4e0e\u5148\u9009 \\(5\\) \u540e\u9009 \\(4\\) \u662f\u4e0d\u540c\u7684\u5206\u652f\uff0c\u4f46\u5bf9\u5e94\u540c\u4e00\u4e2a\u5b50\u96c6\u3002

        \u56fe 13-10 \u00a0 \u5b50\u96c6\u641c\u7d22\u4e0e\u8d8a\u754c\u526a\u679d

        \u4e3a\u4e86\u53bb\u9664\u91cd\u590d\u5b50\u96c6\uff0c\u4e00\u79cd\u76f4\u63a5\u7684\u601d\u8def\u662f\u5bf9\u7ed3\u679c\u5217\u8868\u8fdb\u884c\u53bb\u91cd\u3002\u4f46\u8fd9\u4e2a\u65b9\u6cd5\u6548\u7387\u5f88\u4f4e\uff0c\u6709\u4e24\u65b9\u9762\u539f\u56e0\u3002

        • \u5f53\u6570\u7ec4\u5143\u7d20\u8f83\u591a\uff0c\u5c24\u5176\u662f\u5f53 target \u8f83\u5927\u65f6\uff0c\u641c\u7d22\u8fc7\u7a0b\u4f1a\u4ea7\u751f\u5927\u91cf\u7684\u91cd\u590d\u5b50\u96c6\u3002
        • \u6bd4\u8f83\u5b50\u96c6\uff08\u6570\u7ec4\uff09\u7684\u5f02\u540c\u975e\u5e38\u8017\u65f6\uff0c\u9700\u8981\u5148\u6392\u5e8f\u6570\u7ec4\uff0c\u518d\u6bd4\u8f83\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u5f02\u540c\u3002
        "},{"location":"chapter_backtracking/subset_sum_problem/#2","title":"2. \u00a0 \u91cd\u590d\u5b50\u96c6\u526a\u679d","text":"

        \u6211\u4eec\u8003\u8651\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u901a\u8fc7\u526a\u679d\u8fdb\u884c\u53bb\u91cd\u3002\u89c2\u5bdf\u56fe 13-11 \uff0c\u91cd\u590d\u5b50\u96c6\u662f\u5728\u4ee5\u4e0d\u540c\u987a\u5e8f\u9009\u62e9\u6570\u7ec4\u5143\u7d20\u65f6\u4ea7\u751f\u7684\uff0c\u4f8b\u5982\u4ee5\u4e0b\u60c5\u51b5\u3002

        1. \u5f53\u7b2c\u4e00\u8f6e\u548c\u7b2c\u4e8c\u8f6e\u5206\u522b\u9009\u62e9 \\(3\\) \u548c \\(4\\) \u65f6\uff0c\u4f1a\u751f\u6210\u5305\u542b\u8fd9\u4e24\u4e2a\u5143\u7d20\u7684\u6240\u6709\u5b50\u96c6\uff0c\u8bb0\u4e3a \\([3, 4, \\dots]\\) \u3002
        2. \u4e4b\u540e\uff0c\u5f53\u7b2c\u4e00\u8f6e\u9009\u62e9 \\(4\\) \u65f6\uff0c\u5219\u7b2c\u4e8c\u8f6e\u5e94\u8be5\u8df3\u8fc7 \\(3\\) \uff0c\u56e0\u4e3a\u8be5\u9009\u62e9\u4ea7\u751f\u7684\u5b50\u96c6 \\([4, 3, \\dots]\\) \u548c\u7b2c 1. \u6b65\u4e2d\u751f\u6210\u7684\u5b50\u96c6\u5b8c\u5168\u91cd\u590d\u3002

        \u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u6bcf\u4e00\u5c42\u7684\u9009\u62e9\u90fd\u662f\u4ece\u5de6\u5230\u53f3\u88ab\u9010\u4e2a\u5c1d\u8bd5\u7684\uff0c\u56e0\u6b64\u8d8a\u9760\u53f3\u7684\u5206\u652f\u88ab\u526a\u6389\u7684\u8d8a\u591a\u3002

        1. \u524d\u4e24\u8f6e\u9009\u62e9 \\(3\\) \u548c \\(5\\) \uff0c\u751f\u6210\u5b50\u96c6 \\([3, 5, \\dots]\\) \u3002
        2. \u524d\u4e24\u8f6e\u9009\u62e9 \\(4\\) \u548c \\(5\\) \uff0c\u751f\u6210\u5b50\u96c6 \\([4, 5, \\dots]\\) \u3002
        3. \u82e5\u7b2c\u4e00\u8f6e\u9009\u62e9 \\(5\\) \uff0c\u5219\u7b2c\u4e8c\u8f6e\u5e94\u8be5\u8df3\u8fc7 \\(3\\) \u548c \\(4\\) \uff0c\u56e0\u4e3a\u5b50\u96c6 \\([5, 3, \\dots]\\) \u548c \\([5, 4, \\dots]\\) \u4e0e\u7b2c 1. \u6b65\u548c\u7b2c 2. \u6b65\u4e2d\u63cf\u8ff0\u7684\u5b50\u96c6\u5b8c\u5168\u91cd\u590d\u3002

        \u56fe 13-11 \u00a0 \u4e0d\u540c\u9009\u62e9\u987a\u5e8f\u5bfc\u81f4\u7684\u91cd\u590d\u5b50\u96c6

        \u603b\u7ed3\u6765\u770b\uff0c\u7ed9\u5b9a\u8f93\u5165\u6570\u7ec4 \\([x_1, x_2, \\dots, x_n]\\) \uff0c\u8bbe\u641c\u7d22\u8fc7\u7a0b\u4e2d\u7684\u9009\u62e9\u5e8f\u5217\u4e3a \\([x_{i_1}, x_{i_2}, \\dots, x_{i_m}]\\) \uff0c\u5219\u8be5\u9009\u62e9\u5e8f\u5217\u9700\u8981\u6ee1\u8db3 \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\) \uff0c\u4e0d\u6ee1\u8db3\u8be5\u6761\u4ef6\u7684\u9009\u62e9\u5e8f\u5217\u90fd\u4f1a\u9020\u6210\u91cd\u590d\uff0c\u5e94\u5f53\u526a\u679d\u3002

        "},{"location":"chapter_backtracking/subset_sum_problem/#3","title":"3. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u4e3a\u5b9e\u73b0\u8be5\u526a\u679d\uff0c\u6211\u4eec\u521d\u59cb\u5316\u53d8\u91cf start \uff0c\u7528\u4e8e\u6307\u793a\u904d\u5386\u8d77\u59cb\u70b9\u3002\u5f53\u505a\u51fa\u9009\u62e9 \\(x_{i}\\) \u540e\uff0c\u8bbe\u5b9a\u4e0b\u4e00\u8f6e\u4ece\u7d22\u5f15 \\(i\\) \u5f00\u59cb\u904d\u5386\u3002\u8fd9\u6837\u505a\u5c31\u53ef\u4ee5\u8ba9\u9009\u62e9\u5e8f\u5217\u6ee1\u8db3 \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\) \uff0c\u4ece\u800c\u4fdd\u8bc1\u5b50\u96c6\u552f\u4e00\u3002

        \u9664\u6b64\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u5bf9\u4ee3\u7801\u8fdb\u884c\u4e86\u4ee5\u4e0b\u4e24\u9879\u4f18\u5316\u3002

        • \u5728\u5f00\u542f\u641c\u7d22\u524d\uff0c\u5148\u5c06\u6570\u7ec4 nums \u6392\u5e8f\u3002\u5728\u904d\u5386\u6240\u6709\u9009\u62e9\u65f6\uff0c\u5f53\u5b50\u96c6\u548c\u8d85\u8fc7 target \u65f6\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\uff0c\u56e0\u4e3a\u540e\u8fb9\u7684\u5143\u7d20\u66f4\u5927\uff0c\u5176\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target \u3002
        • \u7701\u53bb\u5143\u7d20\u548c\u53d8\u91cf total \uff0c\u901a\u8fc7\u5728 target \u4e0a\u6267\u884c\u51cf\u6cd5\u6765\u7edf\u8ba1\u5143\u7d20\u548c\uff0c\u5f53 target \u7b49\u4e8e \\(0\\) \u65f6\u8bb0\u5f55\u89e3\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby subset_sum_i.py
        def backtrack(\n    state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n    \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I\"\"\"\n    # \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if target == 0:\n        res.append(list(state))\n        return\n    # \u904d\u5386\u6240\u6709\u9009\u62e9\n    # \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    for i in range(start, len(choices)):\n        # \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        # \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if target - choices[i] < 0:\n            break\n        # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.append(choices[i])\n        # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i, res)\n        # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop()\n\ndef subset_sum_i(nums: list[int], target: int) -> list[list[int]]:\n    \"\"\"\u6c42\u89e3\u5b50\u96c6\u548c I\"\"\"\n    state = []  # \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    nums.sort()  # \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    start = 0  # \u904d\u5386\u8d77\u59cb\u70b9\n    res = []  # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res)\n    return res\n
        subset_sum_i.cpp
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target == 0) {\n        res.push_back(state);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    for (int i = start; i < choices.size(); i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.push_back(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop_back();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nvector<vector<int>> subsetSumI(vector<int> &nums, int target) {\n    vector<int> state;              // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    sort(nums.begin(), nums.end()); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    int start = 0;                  // \u904d\u5386\u8d77\u59cb\u70b9\n    vector<vector<int>> res;        // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_i.java
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target == 0) {\n        res.add(new ArrayList<>(state));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    for (int i = start; i < choices.length; i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.add(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.remove(state.size() - 1);\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList<List<Integer>> subsetSumI(int[] nums, int target) {\n    List<Integer> state = new ArrayList<>(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    Arrays.sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n    List<List<Integer>> res = new ArrayList<>(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_i.cs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target == 0) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    for (int i = start; i < choices.Length; i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.Add(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        Backtrack(state, target - choices[i], choices, i, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.RemoveAt(state.Count - 1);\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList<List<int>> SubsetSumI(int[] nums, int target) {\n    List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    Array.Sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n    List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    Backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_i.go
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrackSubsetSumI(start, target int, state, choices *[]int, res *[][]int) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if target == 0 {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    for i := start; i < len(*choices); i++ {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if target-(*choices)[i] < 0 {\n            break\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        *state = append(*state, (*choices)[i])\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrackSubsetSumI(i, target-(*choices)[i], state, choices, res)\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        *state = (*state)[:len(*state)-1]\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunc subsetSumI(nums []int, target int) [][]int {\n    state := make([]int, 0) // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    sort.Ints(nums)         // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    start := 0              // \u904d\u5386\u8d77\u59cb\u70b9\n    res := make([][]int, 0) // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrackSubsetSumI(start, target, &state, &nums, &res)\n    return res\n}\n
        subset_sum_i.swift
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if target == 0 {\n        res.append(state)\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    for i in choices.indices.dropFirst(start) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if target - choices[i] < 0 {\n            break\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.append(choices[i])\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state: &state, target: target - choices[i], choices: choices, start: i, res: &res)\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.removeLast()\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunc subsetSumI(nums: [Int], target: Int) -> [[Int]] {\n    var state: [Int] = [] // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    let nums = nums.sorted() // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    let start = 0 // \u904d\u5386\u8d77\u59cb\u70b9\n    var res: [[Int]] = [] // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state: &state, target: target, choices: nums, start: start, res: &res)\n    return res\n}\n
        subset_sum_i.js
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(state, target, choices, start, res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target === 0) {\n        res.push([...state]);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    for (let i = start; i < choices.length; i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.push(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunction subsetSumI(nums, target) {\n    const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    nums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    const start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n    const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_i.ts
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(\n    state: number[],\n    target: number,\n    choices: number[],\n    start: number,\n    res: number[][]\n): void {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target === 0) {\n        res.push([...state]);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    for (let i = start; i < choices.length; i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.push(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunction subsetSumI(nums: number[], target: number): number[][] {\n    const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    nums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    const start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n    const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_i.dart
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(\n  List<int> state,\n  int target,\n  List<int> choices,\n  int start,\n  List<List<int>> res,\n) {\n  // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n  if (target == 0) {\n    res.add(List.from(state));\n    return;\n  }\n  // \u904d\u5386\u6240\u6709\u9009\u62e9\n  // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n  for (int i = start; i < choices.length; i++) {\n    // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n    // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n    if (target - choices[i] < 0) {\n      break;\n    }\n    // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n    state.add(choices[i]);\n    // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n    backtrack(state, target - choices[i], choices, i, res);\n    // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n    state.removeLast();\n  }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList<List<int>> subsetSumI(List<int> nums, int target) {\n  List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n  nums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n  int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n  List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n  backtrack(state, target, nums, start, res);\n  return res;\n}\n
        subset_sum_i.rs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfn backtrack(\n    state: &mut Vec<i32>,\n    target: i32,\n    choices: &[i32],\n    start: usize,\n    res: &mut Vec<Vec<i32>>,\n) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if target == 0 {\n        res.push(state.clone());\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    for i in start..choices.len() {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if target - choices[i] < 0 {\n            break;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.push(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfn subset_sum_i(nums: &mut [i32], target: i32) -> Vec<Vec<i32>> {\n    let mut state = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    nums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    let start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n    let mut res = Vec::new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(&mut state, target, nums, start, &mut res);\n    res\n}\n
        subset_sum_i.c
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(int target, int *choices, int choicesSize, int start) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target == 0) {\n        for (int i = 0; i < stateSize; ++i) {\n            res[resSize][i] = state[i];\n        }\n        resColSizes[resSize++] = stateSize;\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    for (int i = start; i < choicesSize; i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state[stateSize] = choices[i];\n        stateSize++;\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(target - choices[i], choices, choicesSize, i);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        stateSize--;\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nvoid subsetSumI(int *nums, int numsSize, int target) {\n    qsort(nums, numsSize, sizeof(int), cmp); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    int start = 0;                           // \u904d\u5386\u8d77\u59cb\u70b9\n    backtrack(target, nums, numsSize, start);\n}\n
        subset_sum_i.kt
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfun backtrack(\n    state: MutableList<Int>,\n    target: Int,\n    choices: IntArray,\n    start: Int,\n    res: MutableList<MutableList<Int>?>\n) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target == 0) {\n        res.add(state.toMutableList())\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    for (i in start..<choices.size) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.add(choices[i])\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i, res)\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.removeAt(state.size - 1)\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfun subsetSumI(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {\n    val state = mutableListOf<Int>() // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    nums.sort() // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    val start = 0 // \u904d\u5386\u8d77\u59cb\u70b9\n    val res = mutableListOf<MutableList<Int>?>() // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res)\n    return res\n}\n
        subset_sum_i.rb
        ### \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I ###\ndef backtrack(state, target, choices, start, res)\n  # \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n  if target.zero?\n    res << state.dup\n    return\n  end\n  # \u904d\u5386\u6240\u6709\u9009\u62e9\n  # \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n  for i in start...choices.length\n    # \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n    # \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n    break if target - choices[i] < 0\n    # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n    state << choices[i]\n    # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n    backtrack(state, target - choices[i], choices, i, res)\n    # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n    state.pop\n  end\nend\n\n### \u6c42\u89e3\u5b50\u96c6\u548c I ###\ndef subset_sum_i(nums, target)\n  state = [] # \u72b6\u6001\uff08\u5b50\u96c6\uff09\n  nums.sort! # \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n  start = 0 # \u904d\u5386\u8d77\u59cb\u70b9\n  res = [] # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n  backtrack(state, target, nums, start, res)\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 13-12 \u6240\u793a\u4e3a\u5c06\u6570\u7ec4 \\([3, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \u8f93\u5165\u4ee5\u4e0a\u4ee3\u7801\u540e\u7684\u6574\u4f53\u56de\u6eaf\u8fc7\u7a0b\u3002

        \u56fe 13-12 \u00a0 \u5b50\u96c6\u548c I \u56de\u6eaf\u8fc7\u7a0b

        "},{"location":"chapter_backtracking/subset_sum_problem/#1332","title":"13.3.2 \u00a0 \u8003\u8651\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"

        Question

        \u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4 nums \u548c\u4e00\u4e2a\u76ee\u6807\u6b63\u6574\u6570 target \uff0c\u8bf7\u627e\u51fa\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\uff0c\u4f7f\u5f97\u7ec4\u5408\u4e2d\u7684\u5143\u7d20\u548c\u7b49\u4e8e target \u3002\u7ed9\u5b9a\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ea\u53ef\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u8bf7\u4ee5\u5217\u8868\u5f62\u5f0f\u8fd4\u56de\u8fd9\u4e9b\u7ec4\u5408\uff0c\u5217\u8868\u4e2d\u4e0d\u5e94\u5305\u542b\u91cd\u590d\u7ec4\u5408\u3002

        \u76f8\u6bd4\u4e8e\u4e0a\u9898\uff0c\u672c\u9898\u7684\u8f93\u5165\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u8fd9\u5f15\u5165\u4e86\u65b0\u7684\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u7ed9\u5b9a\u6570\u7ec4 \\([4, \\hat{4}, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \uff0c\u5219\u73b0\u6709\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c\u4e3a \\([4, 5], [\\hat{4}, 5]\\) \uff0c\u51fa\u73b0\u4e86\u91cd\u590d\u5b50\u96c6\u3002

        \u9020\u6210\u8fd9\u79cd\u91cd\u590d\u7684\u539f\u56e0\u662f\u76f8\u7b49\u5143\u7d20\u5728\u67d0\u8f6e\u4e2d\u88ab\u591a\u6b21\u9009\u62e9\u3002\u5728\u56fe 13-13 \u4e2d\uff0c\u7b2c\u4e00\u8f6e\u5171\u6709\u4e09\u4e2a\u9009\u62e9\uff0c\u5176\u4e2d\u4e24\u4e2a\u90fd\u4e3a \\(4\\) \uff0c\u4f1a\u4ea7\u751f\u4e24\u4e2a\u91cd\u590d\u7684\u641c\u7d22\u5206\u652f\uff0c\u4ece\u800c\u8f93\u51fa\u91cd\u590d\u5b50\u96c6\uff1b\u540c\u7406\uff0c\u7b2c\u4e8c\u8f6e\u7684\u4e24\u4e2a \\(4\\) \u4e5f\u4f1a\u4ea7\u751f\u91cd\u590d\u5b50\u96c6\u3002

        \u56fe 13-13 \u00a0 \u76f8\u7b49\u5143\u7d20\u5bfc\u81f4\u7684\u91cd\u590d\u5b50\u96c6

        "},{"location":"chapter_backtracking/subset_sum_problem/#1_1","title":"1. \u00a0 \u76f8\u7b49\u5143\u7d20\u526a\u679d","text":"

        \u4e3a\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u9700\u8981\u9650\u5236\u76f8\u7b49\u5143\u7d20\u5728\u6bcf\u4e00\u8f6e\u4e2d\u53ea\u80fd\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u5b9e\u73b0\u65b9\u5f0f\u6bd4\u8f83\u5de7\u5999\uff1a\u7531\u4e8e\u6570\u7ec4\u662f\u5df2\u6392\u5e8f\u7684\uff0c\u56e0\u6b64\u76f8\u7b49\u5143\u7d20\u90fd\u662f\u76f8\u90bb\u7684\u3002\u8fd9\u610f\u5473\u7740\u5728\u67d0\u8f6e\u9009\u62e9\u4e2d\uff0c\u82e5\u5f53\u524d\u5143\u7d20\u4e0e\u5176\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u5219\u8bf4\u660e\u5b83\u5df2\u7ecf\u88ab\u9009\u62e9\u8fc7\uff0c\u56e0\u6b64\u76f4\u63a5\u8df3\u8fc7\u5f53\u524d\u5143\u7d20\u3002

        \u4e0e\u6b64\u540c\u65f6\uff0c\u672c\u9898\u89c4\u5b9a\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u53ea\u80fd\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5229\u7528\u53d8\u91cf start \u6765\u6ee1\u8db3\u8be5\u7ea6\u675f\uff1a\u5f53\u505a\u51fa\u9009\u62e9 \\(x_{i}\\) \u540e\uff0c\u8bbe\u5b9a\u4e0b\u4e00\u8f6e\u4ece\u7d22\u5f15 \\(i + 1\\) \u5f00\u59cb\u5411\u540e\u904d\u5386\u3002\u8fd9\u6837\u65e2\u80fd\u53bb\u9664\u91cd\u590d\u5b50\u96c6\uff0c\u4e5f\u80fd\u907f\u514d\u91cd\u590d\u9009\u62e9\u5143\u7d20\u3002

        "},{"location":"chapter_backtracking/subset_sum_problem/#2_1","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby subset_sum_ii.py
        def backtrack(\n    state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n    \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II\"\"\"\n    # \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if target == 0:\n        res.append(list(state))\n        return\n    # \u904d\u5386\u6240\u6709\u9009\u62e9\n    # \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    # \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n    for i in range(start, len(choices)):\n        # \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        # \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if target - choices[i] < 0:\n            break\n        # \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if i > start and choices[i] == choices[i - 1]:\n            continue\n        # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.append(choices[i])\n        # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i + 1, res)\n        # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop()\n\ndef subset_sum_ii(nums: list[int], target: int) -> list[list[int]]:\n    \"\"\"\u6c42\u89e3\u5b50\u96c6\u548c II\"\"\"\n    state = []  # \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    nums.sort()  # \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    start = 0  # \u904d\u5386\u8d77\u59cb\u70b9\n    res = []  # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res)\n    return res\n
        subset_sum_ii.cpp
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target == 0) {\n        res.push_back(state);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n    for (int i = start; i < choices.size(); i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.push_back(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop_back();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nvector<vector<int>> subsetSumII(vector<int> &nums, int target) {\n    vector<int> state;              // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    sort(nums.begin(), nums.end()); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    int start = 0;                  // \u904d\u5386\u8d77\u59cb\u70b9\n    vector<vector<int>> res;        // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_ii.java
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target == 0) {\n        res.add(new ArrayList<>(state));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n    for (int i = start; i < choices.length; i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.add(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.remove(state.size() - 1);\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList<List<Integer>> subsetSumII(int[] nums, int target) {\n    List<Integer> state = new ArrayList<>(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    Arrays.sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n    List<List<Integer>> res = new ArrayList<>(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_ii.cs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target == 0) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n    for (int i = start; i < choices.Length; i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.Add(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        Backtrack(state, target - choices[i], choices, i + 1, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.RemoveAt(state.Count - 1);\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList<List<int>> SubsetSumII(int[] nums, int target) {\n    List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    Array.Sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n    List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    Backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_ii.go
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunc backtrackSubsetSumII(start, target int, state, choices *[]int, res *[][]int) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if target == 0 {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n    for i := start; i < len(*choices); i++ {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if target-(*choices)[i] < 0 {\n            break\n        }\n        // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if i > start && (*choices)[i] == (*choices)[i-1] {\n            continue\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        *state = append(*state, (*choices)[i])\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrackSubsetSumII(i+1, target-(*choices)[i], state, choices, res)\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        *state = (*state)[:len(*state)-1]\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunc subsetSumII(nums []int, target int) [][]int {\n    state := make([]int, 0) // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    sort.Ints(nums)         // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    start := 0              // \u904d\u5386\u8d77\u59cb\u70b9\n    res := make([][]int, 0) // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrackSubsetSumII(start, target, &state, &nums, &res)\n    return res\n}\n
        subset_sum_ii.swift
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunc backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if target == 0 {\n        res.append(state)\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n    for i in choices.indices.dropFirst(start) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if target - choices[i] < 0 {\n            break\n        }\n        // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if i > start, choices[i] == choices[i - 1] {\n            continue\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.append(choices[i])\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state: &state, target: target - choices[i], choices: choices, start: i + 1, res: &res)\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.removeLast()\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunc subsetSumII(nums: [Int], target: Int) -> [[Int]] {\n    var state: [Int] = [] // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    let nums = nums.sorted() // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    let start = 0 // \u904d\u5386\u8d77\u59cb\u70b9\n    var res: [[Int]] = [] // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state: &state, target: target, choices: nums, start: start, res: &res)\n    return res\n}\n
        subset_sum_ii.js
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunction backtrack(state, target, choices, start, res) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target === 0) {\n        res.push([...state]);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n    for (let i = start; i < choices.length; i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if (i > start && choices[i] === choices[i - 1]) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.push(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunction subsetSumII(nums, target) {\n    const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    nums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    const start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n    const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_ii.ts
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunction backtrack(\n    state: number[],\n    target: number,\n    choices: number[],\n    start: number,\n    res: number[][]\n): void {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target === 0) {\n        res.push([...state]);\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n    for (let i = start; i < choices.length; i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if (i > start && choices[i] === choices[i - 1]) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.push(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunction subsetSumII(nums: number[], target: number): number[][] {\n    const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    nums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    const start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n    const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
        subset_sum_ii.dart
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(\n  List<int> state,\n  int target,\n  List<int> choices,\n  int start,\n  List<List<int>> res,\n) {\n  // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n  if (target == 0) {\n    res.add(List.from(state));\n    return;\n  }\n  // \u904d\u5386\u6240\u6709\u9009\u62e9\n  // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n  // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n  for (int i = start; i < choices.length; i++) {\n    // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n    // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n    if (target - choices[i] < 0) {\n      break;\n    }\n    // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n    if (i > start && choices[i] == choices[i - 1]) {\n      continue;\n    }\n    // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n    state.add(choices[i]);\n    // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n    backtrack(state, target - choices[i], choices, i + 1, res);\n    // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n    state.removeLast();\n  }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList<List<int>> subsetSumII(List<int> nums, int target) {\n  List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n  nums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n  int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n  List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n  backtrack(state, target, nums, start, res);\n  return res;\n}\n
        subset_sum_ii.rs
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfn backtrack(\n    state: &mut Vec<i32>,\n    target: i32,\n    choices: &[i32],\n    start: usize,\n    res: &mut Vec<Vec<i32>>,\n) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if target == 0 {\n        res.push(state.clone());\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n    for i in start..choices.len() {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if target - choices[i] < 0 {\n            break;\n        }\n        // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if i > start && choices[i] == choices[i - 1] {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.push(choices[i]);\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.pop();\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfn subset_sum_ii(nums: &mut [i32], target: i32) -> Vec<Vec<i32>> {\n    let mut state = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    nums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    let start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n    let mut res = Vec::new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(&mut state, target, nums, start, &mut res);\n    res\n}\n
        subset_sum_ii.c
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(int target, int *choices, int choicesSize, int start) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target == 0) {\n        for (int i = 0; i < stateSize; i++) {\n            res[resSize][i] = state[i];\n        }\n        resColSizes[resSize++] = stateSize;\n        return;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n    for (int i = start; i < choicesSize; i++) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\n        if (target - choices[i] < 0) {\n            continue;\n        }\n        // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state[stateSize] = choices[i];\n        stateSize++;\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(target - choices[i], choices, choicesSize, i + 1);\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        stateSize--;\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nvoid subsetSumII(int *nums, int numsSize, int target) {\n    // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    qsort(nums, numsSize, sizeof(int), cmp);\n    // \u5f00\u59cb\u56de\u6eaf\n    backtrack(target, nums, numsSize, 0);\n}\n
        subset_sum_ii.kt
        /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfun backtrack(\n    state: MutableList<Int>,\n    target: Int,\n    choices: IntArray,\n    start: Int,\n    res: MutableList<MutableList<Int>?>\n) {\n    // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n    if (target == 0) {\n        res.add(state.toMutableList())\n        return\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n    // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n    for (i in start..<choices.size) {\n        // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n        // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n        if (target - choices[i] < 0) {\n            break\n        }\n        // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n        state.add(choices[i])\n        // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n        backtrack(state, target - choices[i], choices, i + 1, res)\n        // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n        state.removeAt(state.size - 1)\n    }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfun subsetSumII(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {\n    val state = mutableListOf<Int>() // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n    nums.sort() // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n    val start = 0 // \u904d\u5386\u8d77\u59cb\u70b9\n    val res = mutableListOf<MutableList<Int>?>() // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n    backtrack(state, target, nums, start, res)\n    return res\n}\n
        subset_sum_ii.rb
        ### \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II ###\ndef backtrack(state, target, choices, start, res)\n  # \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n  if target.zero?\n    res << state.dup\n    return\n  end\n\n  # \u904d\u5386\u6240\u6709\u9009\u62e9\n  # \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n  # \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n  for i in start...choices.length\n    # \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n    # \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n    break if target - choices[i] < 0\n    # \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n    next if i > start && choices[i] == choices[i - 1]\n    # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n    state << choices[i]\n    # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n    backtrack(state, target - choices[i], choices, i + 1, res)\n    # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n    state.pop\n  end\nend\n\n### \u6c42\u89e3\u5b50\u96c6\u548c II ###\ndef subset_sum_ii(nums, target)\n  state = [] # \u72b6\u6001\uff08\u5b50\u96c6\uff09\n  nums.sort! # \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n  start = 0 # \u904d\u5386\u8d77\u59cb\u70b9\n  res = [] # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n  backtrack(state, target, nums, start, res)\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 13-14 \u5c55\u793a\u4e86\u6570\u7ec4 \\([4, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \u7684\u56de\u6eaf\u8fc7\u7a0b\uff0c\u5171\u5305\u542b\u56db\u79cd\u526a\u679d\u64cd\u4f5c\u3002\u8bf7\u4f60\u5c06\u56fe\u793a\u4e0e\u4ee3\u7801\u6ce8\u91ca\u76f8\u7ed3\u5408\uff0c\u7406\u89e3\u6574\u4e2a\u641c\u7d22\u8fc7\u7a0b\uff0c\u4ee5\u53ca\u6bcf\u79cd\u526a\u679d\u64cd\u4f5c\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002

        \u56fe 13-14 \u00a0 \u5b50\u96c6\u548c II \u56de\u6eaf\u8fc7\u7a0b

        "},{"location":"chapter_backtracking/summary/","title":"13.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_backtracking/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u56de\u6eaf\u7b97\u6cd5\u672c\u8d28\u662f\u7a77\u4e3e\u6cd5\uff0c\u901a\u8fc7\u5bf9\u89e3\u7a7a\u95f4\u8fdb\u884c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u6765\u5bfb\u627e\u7b26\u5408\u6761\u4ef6\u7684\u89e3\u3002\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u9047\u5230\u6ee1\u8db3\u6761\u4ef6\u7684\u89e3\u5219\u8bb0\u5f55\uff0c\u76f4\u81f3\u627e\u5230\u6240\u6709\u89e3\u6216\u904d\u5386\u5b8c\u6210\u540e\u7ed3\u675f\u3002
        • \u56de\u6eaf\u7b97\u6cd5\u7684\u641c\u7d22\u8fc7\u7a0b\u5305\u62ec\u5c1d\u8bd5\u4e0e\u56de\u9000\u4e24\u4e2a\u90e8\u5206\u3002\u5b83\u901a\u8fc7\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u6765\u5c1d\u8bd5\u5404\u79cd\u9009\u62e9\uff0c\u5f53\u9047\u5230\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u60c5\u51b5\u65f6\uff0c\u5219\u64a4\u9500\u4e0a\u4e00\u6b65\u7684\u9009\u62e9\uff0c\u9000\u56de\u5230\u4e4b\u524d\u7684\u72b6\u6001\uff0c\u5e76\u7ee7\u7eed\u5c1d\u8bd5\u5176\u4ed6\u9009\u62e9\u3002\u5c1d\u8bd5\u4e0e\u56de\u9000\u662f\u4e24\u4e2a\u65b9\u5411\u76f8\u53cd\u7684\u64cd\u4f5c\u3002
        • \u56de\u6eaf\u95ee\u9898\u901a\u5e38\u5305\u542b\u591a\u4e2a\u7ea6\u675f\u6761\u4ef6\uff0c\u5b83\u4eec\u53ef\u7528\u4e8e\u5b9e\u73b0\u526a\u679d\u64cd\u4f5c\u3002\u526a\u679d\u53ef\u4ee5\u63d0\u524d\u7ed3\u675f\u4e0d\u5fc5\u8981\u7684\u641c\u7d22\u5206\u652f\uff0c\u5927\u5e45\u63d0\u5347\u641c\u7d22\u6548\u7387\u3002
        • \u56de\u6eaf\u7b97\u6cd5\u4e3b\u8981\u53ef\u7528\u4e8e\u89e3\u51b3\u641c\u7d22\u95ee\u9898\u548c\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\u3002\u7ec4\u5408\u4f18\u5316\u95ee\u9898\u867d\u7136\u53ef\u4ee5\u7528\u56de\u6eaf\u7b97\u6cd5\u89e3\u51b3\uff0c\u4f46\u5f80\u5f80\u5b58\u5728\u6548\u7387\u66f4\u9ad8\u6216\u6548\u679c\u66f4\u597d\u7684\u89e3\u6cd5\u3002
        • \u5168\u6392\u5217\u95ee\u9898\u65e8\u5728\u641c\u7d22\u7ed9\u5b9a\u96c6\u5408\u5143\u7d20\u7684\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u3002\u6211\u4eec\u501f\u52a9\u4e00\u4e2a\u6570\u7ec4\u6765\u8bb0\u5f55\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u88ab\u9009\u62e9\uff0c\u526a\u6389\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\u7684\u641c\u7d22\u5206\u652f\uff0c\u786e\u4fdd\u6bcf\u4e2a\u5143\u7d20\u53ea\u88ab\u9009\u62e9\u4e00\u6b21\u3002
        • \u5728\u5168\u6392\u5217\u95ee\u9898\u4e2d\uff0c\u5982\u679c\u96c6\u5408\u4e2d\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff0c\u5219\u6700\u7ec8\u7ed3\u679c\u4f1a\u51fa\u73b0\u91cd\u590d\u6392\u5217\u3002\u6211\u4eec\u9700\u8981\u7ea6\u675f\u76f8\u7b49\u5143\u7d20\u5728\u6bcf\u8f6e\u4e2d\u53ea\u80fd\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u8fd9\u901a\u5e38\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u96c6\u5408\u6765\u5b9e\u73b0\u3002
        • \u5b50\u96c6\u548c\u95ee\u9898\u7684\u76ee\u6807\u662f\u5728\u7ed9\u5b9a\u96c6\u5408\u4e2d\u627e\u5230\u548c\u4e3a\u76ee\u6807\u503c\u7684\u6240\u6709\u5b50\u96c6\u3002\u96c6\u5408\u4e0d\u533a\u5206\u5143\u7d20\u987a\u5e8f\uff0c\u800c\u641c\u7d22\u8fc7\u7a0b\u4f1a\u8f93\u51fa\u6240\u6709\u987a\u5e8f\u7684\u7ed3\u679c\uff0c\u4ea7\u751f\u91cd\u590d\u5b50\u96c6\u3002\u6211\u4eec\u5728\u56de\u6eaf\u524d\u5c06\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u8bbe\u7f6e\u4e00\u4e2a\u53d8\u91cf\u6765\u6307\u793a\u6bcf\u4e00\u8f6e\u7684\u904d\u5386\u8d77\u59cb\u70b9\uff0c\u4ece\u800c\u5c06\u751f\u6210\u91cd\u590d\u5b50\u96c6\u7684\u641c\u7d22\u5206\u652f\u8fdb\u884c\u526a\u679d\u3002
        • \u5bf9\u4e8e\u5b50\u96c6\u548c\u95ee\u9898\uff0c\u6570\u7ec4\u4e2d\u7684\u76f8\u7b49\u5143\u7d20\u4f1a\u4ea7\u751f\u91cd\u590d\u96c6\u5408\u3002\u6211\u4eec\u5229\u7528\u6570\u7ec4\u5df2\u6392\u5e8f\u7684\u524d\u7f6e\u6761\u4ef6\uff0c\u901a\u8fc7\u5224\u65ad\u76f8\u90bb\u5143\u7d20\u662f\u5426\u76f8\u7b49\u5b9e\u73b0\u526a\u679d\uff0c\u4ece\u800c\u786e\u4fdd\u76f8\u7b49\u5143\u7d20\u5728\u6bcf\u8f6e\u4e2d\u53ea\u80fd\u88ab\u9009\u4e2d\u4e00\u6b21\u3002
        • \\(n\\) \u7687\u540e\u95ee\u9898\u65e8\u5728\u5bfb\u627e\u5c06 \\(n\\) \u4e2a\u7687\u540e\u653e\u7f6e\u5230 \\(n \\times n\\) \u5c3a\u5bf8\u68cb\u76d8\u4e0a\u7684\u65b9\u6848\uff0c\u8981\u6c42\u6240\u6709\u7687\u540e\u4e24\u4e24\u4e4b\u95f4\u65e0\u6cd5\u653b\u51fb\u5bf9\u65b9\u3002\u8be5\u95ee\u9898\u7684\u7ea6\u675f\u6761\u4ef6\u6709\u884c\u7ea6\u675f\u3001\u5217\u7ea6\u675f\u3001\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\u7ea6\u675f\u3002\u4e3a\u6ee1\u8db3\u884c\u7ea6\u675f\uff0c\u6211\u4eec\u91c7\u7528\u6309\u884c\u653e\u7f6e\u7684\u7b56\u7565\uff0c\u4fdd\u8bc1\u6bcf\u4e00\u884c\u653e\u7f6e\u4e00\u4e2a\u7687\u540e\u3002
        • \u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u5904\u7406\u65b9\u5f0f\u7c7b\u4f3c\u3002\u5bf9\u4e8e\u5217\u7ea6\u675f\uff0c\u6211\u4eec\u5229\u7528\u4e00\u4e2a\u6570\u7ec4\u6765\u8bb0\u5f55\u6bcf\u4e00\u5217\u662f\u5426\u6709\u7687\u540e\uff0c\u4ece\u800c\u6307\u793a\u9009\u4e2d\u7684\u683c\u5b50\u662f\u5426\u5408\u6cd5\u3002\u5bf9\u4e8e\u5bf9\u89d2\u7ebf\u7ea6\u675f\uff0c\u6211\u4eec\u501f\u52a9\u4e24\u4e2a\u6570\u7ec4\u6765\u5206\u522b\u8bb0\u5f55\u8be5\u4e3b\u3001\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u5b58\u5728\u7687\u540e\uff1b\u96be\u70b9\u5728\u4e8e\u627e\u51fa\u5904\u5728\u540c\u4e00\u4e3b\uff08\u526f\uff09\u5bf9\u89d2\u7ebf\u4e0a\u7684\u683c\u5b50\u6240\u6ee1\u8db3\u7684\u884c\u5217\u7d22\u5f15\u89c4\u5f8b\u3002
        "},{"location":"chapter_backtracking/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u600e\u4e48\u7406\u89e3\u56de\u6eaf\u548c\u9012\u5f52\u7684\u5173\u7cfb\uff1f

        \u603b\u7684\u6765\u770b\uff0c\u56de\u6eaf\u662f\u4e00\u79cd\u201c\u7b97\u6cd5\u7b56\u7565\u201d\uff0c\u800c\u9012\u5f52\u66f4\u50cf\u662f\u4e00\u4e2a\u201c\u5de5\u5177\u201d\u3002

        • \u56de\u6eaf\u7b97\u6cd5\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\u3002\u7136\u800c\uff0c\u56de\u6eaf\u662f\u9012\u5f52\u7684\u5e94\u7528\u573a\u666f\u4e4b\u4e00\uff0c\u662f\u9012\u5f52\u5728\u641c\u7d22\u95ee\u9898\u4e2d\u7684\u5e94\u7528\u3002
        • \u9012\u5f52\u7684\u7ed3\u6784\u4f53\u73b0\u4e86\u201c\u5b50\u95ee\u9898\u5206\u89e3\u201d\u7684\u89e3\u9898\u8303\u5f0f\uff0c\u5e38\u7528\u4e8e\u89e3\u51b3\u5206\u6cbb\u3001\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\uff08\u8bb0\u5fc6\u5316\u9012\u5f52\uff09\u7b49\u95ee\u9898\u3002
        "},{"location":"chapter_computational_complexity/","title":"\u7b2c 2 \u7ae0 \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"

        Abstract

        \u590d\u6742\u5ea6\u5206\u6790\u72b9\u5982\u6d69\u701a\u7684\u7b97\u6cd5\u5b87\u5b99\u4e2d\u7684\u65f6\u7a7a\u5411\u5bfc\u3002

        \u5b83\u5e26\u9886\u6211\u4eec\u5728\u65f6\u95f4\u4e0e\u7a7a\u95f4\u8fd9\u4e24\u4e2a\u7ef4\u5ea6\u4e0a\u6df1\u5165\u63a2\u7d22\uff0c\u5bfb\u627e\u66f4\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\u3002

        "},{"location":"chapter_computational_complexity/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 2.1 \u00a0 \u7b97\u6cd5\u6548\u7387\u8bc4\u4f30
        • 2.2 \u00a0 \u8fed\u4ee3\u4e0e\u9012\u5f52
        • 2.3 \u00a0 \u65f6\u95f4\u590d\u6742\u5ea6
        • 2.4 \u00a0 \u7a7a\u95f4\u590d\u6742\u5ea6
        • 2.5 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_computational_complexity/iteration_and_recursion/","title":"2.2 \u00a0 \u8fed\u4ee3\u4e0e\u9012\u5f52","text":"

        \u5728\u7b97\u6cd5\u4e2d\uff0c\u91cd\u590d\u6267\u884c\u67d0\u4e2a\u4efb\u52a1\u662f\u5f88\u5e38\u89c1\u7684\uff0c\u5b83\u4e0e\u590d\u6742\u5ea6\u5206\u6790\u606f\u606f\u76f8\u5173\u3002\u56e0\u6b64\uff0c\u5728\u4ecb\u7ecd\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u6765\u4e86\u89e3\u5982\u4f55\u5728\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u91cd\u590d\u6267\u884c\u4efb\u52a1\uff0c\u5373\u4e24\u79cd\u57fa\u672c\u7684\u7a0b\u5e8f\u63a7\u5236\u7ed3\u6784\uff1a\u8fed\u4ee3\u3001\u9012\u5f52\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#221","title":"2.2.1 \u00a0 \u8fed\u4ee3","text":"

        \u8fed\u4ee3\uff08iteration\uff09\u662f\u4e00\u79cd\u91cd\u590d\u6267\u884c\u67d0\u4e2a\u4efb\u52a1\u7684\u63a7\u5236\u7ed3\u6784\u3002\u5728\u8fed\u4ee3\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u5728\u6ee1\u8db3\u4e00\u5b9a\u7684\u6761\u4ef6\u4e0b\u91cd\u590d\u6267\u884c\u67d0\u6bb5\u4ee3\u7801\uff0c\u76f4\u5230\u8fd9\u4e2a\u6761\u4ef6\u4e0d\u518d\u6ee1\u8db3\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#1-for","title":"1. \u00a0 for \u5faa\u73af","text":"

        for \u5faa\u73af\u662f\u6700\u5e38\u89c1\u7684\u8fed\u4ee3\u5f62\u5f0f\u4e4b\u4e00\uff0c\u9002\u5408\u5728\u9884\u5148\u77e5\u9053\u8fed\u4ee3\u6b21\u6570\u65f6\u4f7f\u7528\u3002

        \u4ee5\u4e0b\u51fd\u6570\u57fa\u4e8e for \u5faa\u73af\u5b9e\u73b0\u4e86\u6c42\u548c \\(1 + 2 + \\dots + n\\) \uff0c\u6c42\u548c\u7ed3\u679c\u4f7f\u7528\u53d8\u91cf res \u8bb0\u5f55\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cPython \u4e2d range(a, b) \u5bf9\u5e94\u7684\u533a\u95f4\u662f\u201c\u5de6\u95ed\u53f3\u5f00\u201d\u7684\uff0c\u5bf9\u5e94\u7684\u904d\u5386\u8303\u56f4\u4e3a \\(a, a + 1, \\dots, b-1\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
        def for_loop(n: int) -> int:\n    \"\"\"for \u5faa\u73af\"\"\"\n    res = 0\n    # \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for i in range(1, n + 1):\n        res += i\n    return res\n
        iteration.cpp
        /* for \u5faa\u73af */\nint forLoop(int n) {\n    int res = 0;\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; ++i) {\n        res += i;\n    }\n    return res;\n}\n
        iteration.java
        /* for \u5faa\u73af */\nint forLoop(int n) {\n    int res = 0;\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
        iteration.cs
        /* for \u5faa\u73af */\nint ForLoop(int n) {\n    int res = 0;\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
        iteration.go
        /* for \u5faa\u73af */\nfunc forLoop(n int) int {\n    res := 0\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for i := 1; i <= n; i++ {\n        res += i\n    }\n    return res\n}\n
        iteration.swift
        /* for \u5faa\u73af */\nfunc forLoop(n: Int) -> Int {\n    var res = 0\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for i in 1 ... n {\n        res += i\n    }\n    return res\n}\n
        iteration.js
        /* for \u5faa\u73af */\nfunction forLoop(n) {\n    let res = 0;\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for (let i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
        iteration.ts
        /* for \u5faa\u73af */\nfunction forLoop(n: number): number {\n    let res = 0;\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for (let i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
        iteration.dart
        /* for \u5faa\u73af */\nint forLoop(int n) {\n  int res = 0;\n  // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n  for (int i = 1; i <= n; i++) {\n    res += i;\n  }\n  return res;\n}\n
        iteration.rs
        /* for \u5faa\u73af */\nfn for_loop(n: i32) -> i32 {\n    let mut res = 0;\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for i in 1..=n {\n        res += i;\n    }\n    res\n}\n
        iteration.c
        /* for \u5faa\u73af */\nint forLoop(int n) {\n    int res = 0;\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
        iteration.kt
        /* for \u5faa\u73af */\nfun forLoop(n: Int): Int {\n    var res = 0\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for (i in 1..n) {\n        res += i\n    }\n    return res\n}\n
        iteration.rb
        ### for \u5faa\u73af ###\ndef for_loop(n)\n  res = 0\n\n  # \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n  for i in 1..n\n    res += i\n  end\n\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 2-1 \u662f\u8be5\u6c42\u548c\u51fd\u6570\u7684\u6d41\u7a0b\u6846\u56fe\u3002

        \u56fe 2-1 \u00a0 \u6c42\u548c\u51fd\u6570\u7684\u6d41\u7a0b\u6846\u56fe

        \u6b64\u6c42\u548c\u51fd\u6570\u7684\u64cd\u4f5c\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u6210\u6b63\u6bd4\uff0c\u6216\u8005\u8bf4\u6210\u201c\u7ebf\u6027\u5173\u7cfb\u201d\u3002\u5b9e\u9645\u4e0a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u63cf\u8ff0\u7684\u5c31\u662f\u8fd9\u4e2a\u201c\u7ebf\u6027\u5173\u7cfb\u201d\u3002\u76f8\u5173\u5185\u5bb9\u5c06\u4f1a\u5728\u4e0b\u4e00\u8282\u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#2-while","title":"2. \u00a0 while \u5faa\u73af","text":"

        \u4e0e for \u5faa\u73af\u7c7b\u4f3c\uff0cwhile \u5faa\u73af\u4e5f\u662f\u4e00\u79cd\u5b9e\u73b0\u8fed\u4ee3\u7684\u65b9\u6cd5\u3002\u5728 while \u5faa\u73af\u4e2d\uff0c\u7a0b\u5e8f\u6bcf\u8f6e\u90fd\u4f1a\u5148\u68c0\u67e5\u6761\u4ef6\uff0c\u5982\u679c\u6761\u4ef6\u4e3a\u771f\uff0c\u5219\u7ee7\u7eed\u6267\u884c\uff0c\u5426\u5219\u5c31\u7ed3\u675f\u5faa\u73af\u3002

        \u4e0b\u9762\u6211\u4eec\u7528 while \u5faa\u73af\u6765\u5b9e\u73b0\u6c42\u548c \\(1 + 2 + \\dots + n\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
        def while_loop(n: int) -> int:\n    \"\"\"while \u5faa\u73af\"\"\"\n    res = 0\n    i = 1  # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    # \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    while i <= n:\n        res += i\n        i += 1  # \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    return res\n
        iteration.cpp
        /* while \u5faa\u73af */\nint whileLoop(int n) {\n    int res = 0;\n    int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    }\n    return res;\n}\n
        iteration.java
        /* while \u5faa\u73af */\nint whileLoop(int n) {\n    int res = 0;\n    int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    }\n    return res;\n}\n
        iteration.cs
        /* while \u5faa\u73af */\nint WhileLoop(int n) {\n    int res = 0;\n    int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i += 1; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    }\n    return res;\n}\n
        iteration.go
        /* while \u5faa\u73af */\nfunc whileLoop(n int) int {\n    res := 0\n    // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    i := 1\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    for i <= n {\n        res += i\n        // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i++\n    }\n    return res\n}\n
        iteration.swift
        /* while \u5faa\u73af */\nfunc whileLoop(n: Int) -> Int {\n    var res = 0\n    var i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    while i <= n {\n        res += i\n        i += 1 // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    }\n    return res\n}\n
        iteration.js
        /* while \u5faa\u73af */\nfunction whileLoop(n) {\n    let res = 0;\n    let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    }\n    return res;\n}\n
        iteration.ts
        /* while \u5faa\u73af */\nfunction whileLoop(n: number): number {\n    let res = 0;\n    let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    }\n    return res;\n}\n
        iteration.dart
        /* while \u5faa\u73af */\nint whileLoop(int n) {\n  int res = 0;\n  int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n  // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n  while (i <= n) {\n    res += i;\n    i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n  }\n  return res;\n}\n
        iteration.rs
        /* while \u5faa\u73af */\nfn while_loop(n: i32) -> i32 {\n    let mut res = 0;\n    let mut i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    while i <= n {\n        res += i;\n        i += 1; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    }\n    res\n}\n
        iteration.c
        /* while \u5faa\u73af */\nint whileLoop(int n) {\n    int res = 0;\n    int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    }\n    return res;\n}\n
        iteration.kt
        /* while \u5faa\u73af */\nfun whileLoop(n: Int): Int {\n    var res = 0\n    var i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i\n        i++ // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    }\n    return res\n}\n
        iteration.rb
        ### while \u5faa\u73af ###\ndef while_loop(n)\n  res = 0\n  i = 1 # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n\n  # \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n  while i <= n\n    res += i\n    i += 1 # \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n  end\n\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        while \u5faa\u73af\u6bd4 for \u5faa\u73af\u7684\u81ea\u7531\u5ea6\u66f4\u9ad8\u3002\u5728 while \u5faa\u73af\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u81ea\u7531\u5730\u8bbe\u8ba1\u6761\u4ef6\u53d8\u91cf\u7684\u521d\u59cb\u5316\u548c\u66f4\u65b0\u6b65\u9aa4\u3002

        \u4f8b\u5982\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u6761\u4ef6\u53d8\u91cf \\(i\\) \u6bcf\u8f6e\u8fdb\u884c\u4e24\u6b21\u66f4\u65b0\uff0c\u8fd9\u79cd\u60c5\u51b5\u5c31\u4e0d\u592a\u65b9\u4fbf\u7528 for \u5faa\u73af\u5b9e\u73b0\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
        def while_loop_ii(n: int) -> int:\n    \"\"\"while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09\"\"\"\n    res = 0\n    i = 1  # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    # \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n    while i <= n:\n        res += i\n        # \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i += 1\n        i *= 2\n    return res\n
        iteration.cpp
        /* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n    int res = 0;\n    int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
        iteration.java
        /* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n    int res = 0;\n    int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
        iteration.cs
        /* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint WhileLoopII(int n) {\n    int res = 0;\n    int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i += 1; \n        i *= 2;\n    }\n    return res;\n}\n
        iteration.go
        /* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunc whileLoopII(n int) int {\n    res := 0\n    // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    i := 1\n    // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n    for i <= n {\n        res += i\n        // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i++\n        i *= 2\n    }\n    return res\n}\n
        iteration.swift
        /* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunc whileLoopII(n: Int) -> Int {\n    var res = 0\n    var i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n    while i <= n {\n        res += i\n        // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i += 1\n        i *= 2\n    }\n    return res\n}\n
        iteration.js
        /* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunction whileLoopII(n) {\n    let res = 0;\n    let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
        iteration.ts
        /* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunction whileLoopII(n: number): number {\n    let res = 0;\n    let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
        iteration.dart
        /* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n  int res = 0;\n  int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n  // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n  while (i <= n) {\n    res += i;\n    // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    i++;\n    i *= 2;\n  }\n  return res;\n}\n
        iteration.rs
        /* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfn while_loop_ii(n: i32) -> i32 {\n    let mut res = 0;\n    let mut i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n\n    // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n    while i <= n {\n        res += i;\n        // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i += 1;\n        i *= 2;\n    }\n    res\n}\n
        iteration.c
        /* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n    int res = 0;\n    int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
        iteration.kt
        /* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfun whileLoopII(n: Int): Int {\n    var res = 0\n    var i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n    // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n    while (i <= n) {\n        res += i\n        // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n        i++\n        i *= 2\n    }\n    return res\n}\n
        iteration.rb
        ### while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09###\ndef while_loop_ii(n)\n  res = 0\n  i = 1 # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n\n  # \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n  while i <= n\n    res += i\n    # \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n    i += 1\n    i *= 2\n  end\n\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u603b\u7684\u6765\u8bf4\uff0cfor \u5faa\u73af\u7684\u4ee3\u7801\u66f4\u52a0\u7d27\u51d1\uff0cwhile \u5faa\u73af\u66f4\u52a0\u7075\u6d3b\uff0c\u4e24\u8005\u90fd\u53ef\u4ee5\u5b9e\u73b0\u8fed\u4ee3\u7ed3\u6784\u3002\u9009\u62e9\u4f7f\u7528\u54ea\u4e00\u4e2a\u5e94\u8be5\u6839\u636e\u7279\u5b9a\u95ee\u9898\u7684\u9700\u6c42\u6765\u51b3\u5b9a\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#3","title":"3. \u00a0 \u5d4c\u5957\u5faa\u73af","text":"

        \u6211\u4eec\u53ef\u4ee5\u5728\u4e00\u4e2a\u5faa\u73af\u7ed3\u6784\u5185\u5d4c\u5957\u53e6\u4e00\u4e2a\u5faa\u73af\u7ed3\u6784\uff0c\u4e0b\u9762\u4ee5 for \u5faa\u73af\u4e3a\u4f8b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
        def nested_for_loop(n: int) -> str:\n    \"\"\"\u53cc\u5c42 for \u5faa\u73af\"\"\"\n    res = \"\"\n    # \u5faa\u73af i = 1, 2, ..., n-1, n\n    for i in range(1, n + 1):\n        # \u5faa\u73af j = 1, 2, ..., n-1, n\n        for j in range(1, n + 1):\n            res += f\"({i}, {j}), \"\n    return res\n
        iteration.cpp
        /* \u53cc\u5c42 for \u5faa\u73af */\nstring nestedForLoop(int n) {\n    ostringstream res;\n    // \u5faa\u73af i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; ++i) {\n        // \u5faa\u73af j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; ++j) {\n            res << \"(\" << i << \", \" << j << \"), \";\n        }\n    }\n    return res.str();\n}\n
        iteration.java
        /* \u53cc\u5c42 for \u5faa\u73af */\nString nestedForLoop(int n) {\n    StringBuilder res = new StringBuilder();\n    // \u5faa\u73af i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        // \u5faa\u73af j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; j++) {\n            res.append(\"(\" + i + \", \" + j + \"), \");\n        }\n    }\n    return res.toString();\n}\n
        iteration.cs
        /* \u53cc\u5c42 for \u5faa\u73af */\nstring NestedForLoop(int n) {\n    StringBuilder res = new();\n    // \u5faa\u73af i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        // \u5faa\u73af j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; j++) {\n            res.Append($\"({i}, {j}), \");\n        }\n    }\n    return res.ToString();\n}\n
        iteration.go
        /* \u53cc\u5c42 for \u5faa\u73af */\nfunc nestedForLoop(n int) string {\n    res := \"\"\n    // \u5faa\u73af i = 1, 2, ..., n-1, n\n    for i := 1; i <= n; i++ {\n        for j := 1; j <= n; j++ {\n            // \u5faa\u73af j = 1, 2, ..., n-1, n\n            res += fmt.Sprintf(\"(%d, %d), \", i, j)\n        }\n    }\n    return res\n}\n
        iteration.swift
        /* \u53cc\u5c42 for \u5faa\u73af */\nfunc nestedForLoop(n: Int) -> String {\n    var res = \"\"\n    // \u5faa\u73af i = 1, 2, ..., n-1, n\n    for i in 1 ... n {\n        // \u5faa\u73af j = 1, 2, ..., n-1, n\n        for j in 1 ... n {\n            res.append(\"(\\(i), \\(j)), \")\n        }\n    }\n    return res\n}\n
        iteration.js
        /* \u53cc\u5c42 for \u5faa\u73af */\nfunction nestedForLoop(n) {\n    let res = '';\n    // \u5faa\u73af i = 1, 2, ..., n-1, n\n    for (let i = 1; i <= n; i++) {\n        // \u5faa\u73af j = 1, 2, ..., n-1, n\n        for (let j = 1; j <= n; j++) {\n            res += `(${i}, ${j}), `;\n        }\n    }\n    return res;\n}\n
        iteration.ts
        /* \u53cc\u5c42 for \u5faa\u73af */\nfunction nestedForLoop(n: number): string {\n    let res = '';\n    // \u5faa\u73af i = 1, 2, ..., n-1, n\n    for (let i = 1; i <= n; i++) {\n        // \u5faa\u73af j = 1, 2, ..., n-1, n\n        for (let j = 1; j <= n; j++) {\n            res += `(${i}, ${j}), `;\n        }\n    }\n    return res;\n}\n
        iteration.dart
        /* \u53cc\u5c42 for \u5faa\u73af */\nString nestedForLoop(int n) {\n  String res = \"\";\n  // \u5faa\u73af i = 1, 2, ..., n-1, n\n  for (int i = 1; i <= n; i++) {\n    // \u5faa\u73af j = 1, 2, ..., n-1, n\n    for (int j = 1; j <= n; j++) {\n      res += \"($i, $j), \";\n    }\n  }\n  return res;\n}\n
        iteration.rs
        /* \u53cc\u5c42 for \u5faa\u73af */\nfn nested_for_loop(n: i32) -> String {\n    let mut res = vec![];\n    // \u5faa\u73af i = 1, 2, ..., n-1, n\n    for i in 1..=n {\n        // \u5faa\u73af j = 1, 2, ..., n-1, n\n        for j in 1..=n {\n            res.push(format!(\"({}, {}), \", i, j));\n        }\n    }\n    res.join(\"\")\n}\n
        iteration.c
        /* \u53cc\u5c42 for \u5faa\u73af */\nchar *nestedForLoop(int n) {\n    // n * n \u4e3a\u5bf9\u5e94\u70b9\u6570\u91cf\uff0c\"(i, j), \" \u5bf9\u5e94\u5b57\u7b26\u4e32\u957f\u6700\u5927\u4e3a 6+10*2\uff0c\u52a0\u4e0a\u6700\u540e\u4e00\u4e2a\u7a7a\u5b57\u7b26 \\0 \u7684\u989d\u5916\u7a7a\u95f4\n    int size = n * n * 26 + 1;\n    char *res = malloc(size * sizeof(char));\n    // \u5faa\u73af i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        // \u5faa\u73af j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; j++) {\n            char tmp[26];\n            snprintf(tmp, sizeof(tmp), \"(%d, %d), \", i, j);\n            strncat(res, tmp, size - strlen(res) - 1);\n        }\n    }\n    return res;\n}\n
        iteration.kt
        /* \u53cc\u5c42 for \u5faa\u73af */\nfun nestedForLoop(n: Int): String {\n    val res = StringBuilder()\n    // \u5faa\u73af i = 1, 2, ..., n-1, n\n    for (i in 1..n) {\n        // \u5faa\u73af j = 1, 2, ..., n-1, n\n        for (j in 1..n) {\n            res.append(\" ($i, $j), \")\n        }\n    }\n    return res.toString()\n}\n
        iteration.rb
        ### \u53cc\u5c42 for \u5faa\u73af ###\ndef nested_for_loop(n)\n  res = \"\"\n\n  # \u5faa\u73af i = 1, 2, ..., n-1, n\n  for i in 1..n\n    # \u5faa\u73af j = 1, 2, ..., n-1, n\n    for j in 1..n\n      res += \"(#{i}, #{j}), \"\n    end\n  end\n\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 2-2 \u662f\u8be5\u5d4c\u5957\u5faa\u73af\u7684\u6d41\u7a0b\u6846\u56fe\u3002

        \u56fe 2-2 \u00a0 \u5d4c\u5957\u5faa\u73af\u7684\u6d41\u7a0b\u6846\u56fe

        \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570\u7684\u64cd\u4f5c\u6570\u91cf\u4e0e \\(n^2\\) \u6210\u6b63\u6bd4\uff0c\u6216\u8005\u8bf4\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u548c\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u6210\u201c\u5e73\u65b9\u5173\u7cfb\u201d\u3002

        \u6211\u4eec\u53ef\u4ee5\u7ee7\u7eed\u6dfb\u52a0\u5d4c\u5957\u5faa\u73af\uff0c\u6bcf\u4e00\u6b21\u5d4c\u5957\u90fd\u662f\u4e00\u6b21\u201c\u5347\u7ef4\u201d\uff0c\u5c06\u4f1a\u4f7f\u65f6\u95f4\u590d\u6742\u5ea6\u63d0\u9ad8\u81f3\u201c\u7acb\u65b9\u5173\u7cfb\u201d\u201c\u56db\u6b21\u65b9\u5173\u7cfb\u201d\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#222","title":"2.2.2 \u00a0 \u9012\u5f52","text":"

        \u9012\u5f52\uff08recursion\uff09\u662f\u4e00\u79cd\u7b97\u6cd5\u7b56\u7565\uff0c\u901a\u8fc7\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u6765\u89e3\u51b3\u95ee\u9898\u3002\u5b83\u4e3b\u8981\u5305\u542b\u4e24\u4e2a\u9636\u6bb5\u3002

        1. \u9012\uff1a\u7a0b\u5e8f\u4e0d\u65ad\u6df1\u5165\u5730\u8c03\u7528\u81ea\u8eab\uff0c\u901a\u5e38\u4f20\u5165\u66f4\u5c0f\u6216\u66f4\u7b80\u5316\u7684\u53c2\u6570\uff0c\u76f4\u5230\u8fbe\u5230\u201c\u7ec8\u6b62\u6761\u4ef6\u201d\u3002
        2. \u5f52\uff1a\u89e6\u53d1\u201c\u7ec8\u6b62\u6761\u4ef6\u201d\u540e\uff0c\u7a0b\u5e8f\u4ece\u6700\u6df1\u5c42\u7684\u9012\u5f52\u51fd\u6570\u5f00\u59cb\u9010\u5c42\u8fd4\u56de\uff0c\u6c47\u805a\u6bcf\u4e00\u5c42\u7684\u7ed3\u679c\u3002

        \u800c\u4ece\u5b9e\u73b0\u7684\u89d2\u5ea6\u770b\uff0c\u9012\u5f52\u4ee3\u7801\u4e3b\u8981\u5305\u542b\u4e09\u4e2a\u8981\u7d20\u3002

        1. \u7ec8\u6b62\u6761\u4ef6\uff1a\u7528\u4e8e\u51b3\u5b9a\u4ec0\u4e48\u65f6\u5019\u7531\u201c\u9012\u201d\u8f6c\u201c\u5f52\u201d\u3002
        2. \u9012\u5f52\u8c03\u7528\uff1a\u5bf9\u5e94\u201c\u9012\u201d\uff0c\u51fd\u6570\u8c03\u7528\u81ea\u8eab\uff0c\u901a\u5e38\u8f93\u5165\u66f4\u5c0f\u6216\u66f4\u7b80\u5316\u7684\u53c2\u6570\u3002
        3. \u8fd4\u56de\u7ed3\u679c\uff1a\u5bf9\u5e94\u201c\u5f52\u201d\uff0c\u5c06\u5f53\u524d\u9012\u5f52\u5c42\u7ea7\u7684\u7ed3\u679c\u8fd4\u56de\u81f3\u4e0a\u4e00\u5c42\u3002

        \u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u6211\u4eec\u53ea\u9700\u8c03\u7528\u51fd\u6570 recur(n) \uff0c\u5c31\u53ef\u4ee5\u5b8c\u6210 \\(1 + 2 + \\dots + n\\) \u7684\u8ba1\u7b97\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
        def recur(n: int) -> int:\n    \"\"\"\u9012\u5f52\"\"\"\n    # \u7ec8\u6b62\u6761\u4ef6\n    if n == 1:\n        return 1\n    # \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    res = recur(n - 1)\n    # \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    return n + res\n
        recursion.cpp
        /* \u9012\u5f52 */\nint recur(int n) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n == 1)\n        return 1;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    int res = recur(n - 1);\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    return n + res;\n}\n
        recursion.java
        /* \u9012\u5f52 */\nint recur(int n) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n == 1)\n        return 1;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    int res = recur(n - 1);\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    return n + res;\n}\n
        recursion.cs
        /* \u9012\u5f52 */\nint Recur(int n) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n == 1)\n        return 1;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    int res = Recur(n - 1);\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    return n + res;\n}\n
        recursion.go
        /* \u9012\u5f52 */\nfunc recur(n int) int {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if n == 1 {\n        return 1\n    }\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    res := recur(n - 1)\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    return n + res\n}\n
        recursion.swift
        /* \u9012\u5f52 */\nfunc recur(n: Int) -> Int {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if n == 1 {\n        return 1\n    }\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    let res = recur(n: n - 1)\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    return n + res\n}\n
        recursion.js
        /* \u9012\u5f52 */\nfunction recur(n) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n === 1) return 1;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    const res = recur(n - 1);\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    return n + res;\n}\n
        recursion.ts
        /* \u9012\u5f52 */\nfunction recur(n: number): number {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n === 1) return 1;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    const res = recur(n - 1);\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    return n + res;\n}\n
        recursion.dart
        /* \u9012\u5f52 */\nint recur(int n) {\n  // \u7ec8\u6b62\u6761\u4ef6\n  if (n == 1) return 1;\n  // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n  int res = recur(n - 1);\n  // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n  return n + res;\n}\n
        recursion.rs
        /* \u9012\u5f52 */\nfn recur(n: i32) -> i32 {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if n == 1 {\n        return 1;\n    }\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    let res = recur(n - 1);\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    n + res\n}\n
        recursion.c
        /* \u9012\u5f52 */\nint recur(int n) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n == 1)\n        return 1;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    int res = recur(n - 1);\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    return n + res;\n}\n
        recursion.kt
        /* \u9012\u5f52 */\nfun recur(n: Int): Int {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n == 1)\n        return 1\n    // \u9012: \u9012\u5f52\u8c03\u7528\n    val res = recur(n - 1)\n    // \u5f52: \u8fd4\u56de\u7ed3\u679c\n    return n + res\n}\n
        recursion.rb
        ### \u9012\u5f52 ###\ndef recur(n)\n  # \u7ec8\u6b62\u6761\u4ef6\n  return 1 if n == 1\n  # \u9012\uff1a\u9012\u5f52\u8c03\u7528\n  res = recur(n - 1)\n  # \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n  n + res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 2-3 \u5c55\u793a\u4e86\u8be5\u51fd\u6570\u7684\u9012\u5f52\u8fc7\u7a0b\u3002

        \u56fe 2-3 \u00a0 \u6c42\u548c\u51fd\u6570\u7684\u9012\u5f52\u8fc7\u7a0b

        \u867d\u7136\u4ece\u8ba1\u7b97\u89d2\u5ea6\u770b\uff0c\u8fed\u4ee3\u4e0e\u9012\u5f52\u53ef\u4ee5\u5f97\u5230\u76f8\u540c\u7684\u7ed3\u679c\uff0c\u4f46\u5b83\u4eec\u4ee3\u8868\u4e86\u4e24\u79cd\u5b8c\u5168\u4e0d\u540c\u7684\u601d\u8003\u548c\u89e3\u51b3\u95ee\u9898\u7684\u8303\u5f0f\u3002

        • \u8fed\u4ee3\uff1a\u201c\u81ea\u4e0b\u800c\u4e0a\u201d\u5730\u89e3\u51b3\u95ee\u9898\u3002\u4ece\u6700\u57fa\u7840\u7684\u6b65\u9aa4\u5f00\u59cb\uff0c\u7136\u540e\u4e0d\u65ad\u91cd\u590d\u6216\u7d2f\u52a0\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u76f4\u5230\u4efb\u52a1\u5b8c\u6210\u3002
        • \u9012\u5f52\uff1a\u201c\u81ea\u4e0a\u800c\u4e0b\u201d\u5730\u89e3\u51b3\u95ee\u9898\u3002\u5c06\u539f\u95ee\u9898\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u8fd9\u4e9b\u5b50\u95ee\u9898\u548c\u539f\u95ee\u9898\u5177\u6709\u76f8\u540c\u7684\u5f62\u5f0f\u3002\u63a5\u4e0b\u6765\u5c06\u5b50\u95ee\u9898\u7ee7\u7eed\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u76f4\u5230\u57fa\u672c\u60c5\u51b5\u65f6\u505c\u6b62\uff08\u57fa\u672c\u60c5\u51b5\u7684\u89e3\u662f\u5df2\u77e5\u7684\uff09\u3002

        \u4ee5\u4e0a\u8ff0\u6c42\u548c\u51fd\u6570\u4e3a\u4f8b\uff0c\u8bbe\u95ee\u9898 \\(f(n) = 1 + 2 + \\dots + n\\) \u3002

        • \u8fed\u4ee3\uff1a\u5728\u5faa\u73af\u4e2d\u6a21\u62df\u6c42\u548c\u8fc7\u7a0b\uff0c\u4ece \\(1\\) \u904d\u5386\u5230 \\(n\\) \uff0c\u6bcf\u8f6e\u6267\u884c\u6c42\u548c\u64cd\u4f5c\uff0c\u5373\u53ef\u6c42\u5f97 \\(f(n)\\) \u3002
        • \u9012\u5f52\uff1a\u5c06\u95ee\u9898\u5206\u89e3\u4e3a\u5b50\u95ee\u9898 \\(f(n) = n + f(n-1)\\) \uff0c\u4e0d\u65ad\uff08\u9012\u5f52\u5730\uff09\u5206\u89e3\u4e0b\u53bb\uff0c\u76f4\u81f3\u57fa\u672c\u60c5\u51b5 \\(f(1) = 1\\) \u65f6\u7ec8\u6b62\u3002
        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#1","title":"1. \u00a0 \u8c03\u7528\u6808","text":"

        \u9012\u5f52\u51fd\u6570\u6bcf\u6b21\u8c03\u7528\u81ea\u8eab\u65f6\uff0c\u7cfb\u7edf\u90fd\u4f1a\u4e3a\u65b0\u5f00\u542f\u7684\u51fd\u6570\u5206\u914d\u5185\u5b58\uff0c\u4ee5\u5b58\u50a8\u5c40\u90e8\u53d8\u91cf\u3001\u8c03\u7528\u5730\u5740\u548c\u5176\u4ed6\u4fe1\u606f\u7b49\u3002\u8fd9\u5c06\u5bfc\u81f4\u4e24\u65b9\u9762\u7684\u7ed3\u679c\u3002

        • \u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u6570\u636e\u90fd\u5b58\u50a8\u5728\u79f0\u4e3a\u201c\u6808\u5e27\u7a7a\u95f4\u201d\u7684\u5185\u5b58\u533a\u57df\u4e2d\uff0c\u76f4\u81f3\u51fd\u6570\u8fd4\u56de\u540e\u624d\u4f1a\u88ab\u91ca\u653e\u3002\u56e0\u6b64\uff0c\u9012\u5f52\u901a\u5e38\u6bd4\u8fed\u4ee3\u66f4\u52a0\u8017\u8d39\u5185\u5b58\u7a7a\u95f4\u3002
        • \u9012\u5f52\u8c03\u7528\u51fd\u6570\u4f1a\u4ea7\u751f\u989d\u5916\u7684\u5f00\u9500\u3002\u56e0\u6b64\u9012\u5f52\u901a\u5e38\u6bd4\u5faa\u73af\u7684\u65f6\u95f4\u6548\u7387\u66f4\u4f4e\u3002

        \u5982\u56fe 2-4 \u6240\u793a\uff0c\u5728\u89e6\u53d1\u7ec8\u6b62\u6761\u4ef6\u524d\uff0c\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684\u9012\u5f52\u51fd\u6570\uff0c\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \u3002

        \u56fe 2-4 \u00a0 \u9012\u5f52\u8c03\u7528\u6df1\u5ea6

        \u5728\u5b9e\u9645\u4e2d\uff0c\u7f16\u7a0b\u8bed\u8a00\u5141\u8bb8\u7684\u9012\u5f52\u6df1\u5ea6\u901a\u5e38\u662f\u6709\u9650\u7684\uff0c\u8fc7\u6df1\u7684\u9012\u5f52\u53ef\u80fd\u5bfc\u81f4\u6808\u6ea2\u51fa\u9519\u8bef\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#2","title":"2. \u00a0 \u5c3e\u9012\u5f52","text":"

        \u6709\u8da3\u7684\u662f\uff0c\u5982\u679c\u51fd\u6570\u5728\u8fd4\u56de\u524d\u7684\u6700\u540e\u4e00\u6b65\u624d\u8fdb\u884c\u9012\u5f52\u8c03\u7528\uff0c\u5219\u8be5\u51fd\u6570\u53ef\u4ee5\u88ab\u7f16\u8bd1\u5668\u6216\u89e3\u91ca\u5668\u4f18\u5316\uff0c\u4f7f\u5176\u5728\u7a7a\u95f4\u6548\u7387\u4e0a\u4e0e\u8fed\u4ee3\u76f8\u5f53\u3002\u8fd9\u79cd\u60c5\u51b5\u88ab\u79f0\u4e3a\u5c3e\u9012\u5f52\uff08tail recursion\uff09\u3002

        • \u666e\u901a\u9012\u5f52\uff1a\u5f53\u51fd\u6570\u8fd4\u56de\u5230\u4e0a\u4e00\u5c42\u7ea7\u7684\u51fd\u6570\u540e\uff0c\u9700\u8981\u7ee7\u7eed\u6267\u884c\u4ee3\u7801\uff0c\u56e0\u6b64\u7cfb\u7edf\u9700\u8981\u4fdd\u5b58\u4e0a\u4e00\u5c42\u8c03\u7528\u7684\u4e0a\u4e0b\u6587\u3002
        • \u5c3e\u9012\u5f52\uff1a\u9012\u5f52\u8c03\u7528\u662f\u51fd\u6570\u8fd4\u56de\u524d\u7684\u6700\u540e\u4e00\u4e2a\u64cd\u4f5c\uff0c\u8fd9\u610f\u5473\u7740\u51fd\u6570\u8fd4\u56de\u5230\u4e0a\u4e00\u5c42\u7ea7\u540e\uff0c\u65e0\u987b\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\uff0c\u56e0\u6b64\u7cfb\u7edf\u65e0\u987b\u4fdd\u5b58\u4e0a\u4e00\u5c42\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u3002

        \u4ee5\u8ba1\u7b97 \\(1 + 2 + \\dots + n\\) \u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u7ed3\u679c\u53d8\u91cf res \u8bbe\u4e3a\u51fd\u6570\u53c2\u6570\uff0c\u4ece\u800c\u5b9e\u73b0\u5c3e\u9012\u5f52\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
        def tail_recur(n, res):\n    \"\"\"\u5c3e\u9012\u5f52\"\"\"\n    # \u7ec8\u6b62\u6761\u4ef6\n    if n == 0:\n        return res\n    # \u5c3e\u9012\u5f52\u8c03\u7528\n    return tail_recur(n - 1, res + n)\n
        recursion.cpp
        /* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n == 0)\n        return res;\n    // \u5c3e\u9012\u5f52\u8c03\u7528\n    return tailRecur(n - 1, res + n);\n}\n
        recursion.java
        /* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n == 0)\n        return res;\n    // \u5c3e\u9012\u5f52\u8c03\u7528\n    return tailRecur(n - 1, res + n);\n}\n
        recursion.cs
        /* \u5c3e\u9012\u5f52 */\nint TailRecur(int n, int res) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n == 0)\n        return res;\n    // \u5c3e\u9012\u5f52\u8c03\u7528\n    return TailRecur(n - 1, res + n);\n}\n
        recursion.go
        /* \u5c3e\u9012\u5f52 */\nfunc tailRecur(n int, res int) int {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if n == 0 {\n        return res\n    }\n    // \u5c3e\u9012\u5f52\u8c03\u7528\n    return tailRecur(n-1, res+n)\n}\n
        recursion.swift
        /* \u5c3e\u9012\u5f52 */\nfunc tailRecur(n: Int, res: Int) -> Int {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if n == 0 {\n        return res\n    }\n    // \u5c3e\u9012\u5f52\u8c03\u7528\n    return tailRecur(n: n - 1, res: res + n)\n}\n
        recursion.js
        /* \u5c3e\u9012\u5f52 */\nfunction tailRecur(n, res) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n === 0) return res;\n    // \u5c3e\u9012\u5f52\u8c03\u7528\n    return tailRecur(n - 1, res + n);\n}\n
        recursion.ts
        /* \u5c3e\u9012\u5f52 */\nfunction tailRecur(n: number, res: number): number {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n === 0) return res;\n    // \u5c3e\u9012\u5f52\u8c03\u7528\n    return tailRecur(n - 1, res + n);\n}\n
        recursion.dart
        /* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n  // \u7ec8\u6b62\u6761\u4ef6\n  if (n == 0) return res;\n  // \u5c3e\u9012\u5f52\u8c03\u7528\n  return tailRecur(n - 1, res + n);\n}\n
        recursion.rs
        /* \u5c3e\u9012\u5f52 */\nfn tail_recur(n: i32, res: i32) -> i32 {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if n == 0 {\n        return res;\n    }\n    // \u5c3e\u9012\u5f52\u8c03\u7528\n    tail_recur(n - 1, res + n)\n}\n
        recursion.c
        /* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n == 0)\n        return res;\n    // \u5c3e\u9012\u5f52\u8c03\u7528\n    return tailRecur(n - 1, res + n);\n}\n
        recursion.kt
        /* \u5c3e\u9012\u5f52 */\ntailrec fun tailRecur(n: Int, res: Int): Int {\n    // \u6dfb\u52a0 tailrec \u5173\u952e\u8bcd\uff0c\u4ee5\u5f00\u542f\u5c3e\u9012\u5f52\u4f18\u5316\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (n == 0)\n        return res\n    // \u5c3e\u9012\u5f52\u8c03\u7528\n    return tailRecur(n - 1, res + n)\n}\n
        recursion.rb
        ### \u5c3e\u9012\u5f52 ###\ndef tail_recur(n, res)\n  # \u7ec8\u6b62\u6761\u4ef6\n  return res if n == 0\n  # \u5c3e\u9012\u5f52\u8c03\u7528\n  tail_recur(n - 1, res + n)\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5c3e\u9012\u5f52\u7684\u6267\u884c\u8fc7\u7a0b\u5982\u56fe 2-5 \u6240\u793a\u3002\u5bf9\u6bd4\u666e\u901a\u9012\u5f52\u548c\u5c3e\u9012\u5f52\uff0c\u4e24\u8005\u7684\u6c42\u548c\u64cd\u4f5c\u7684\u6267\u884c\u70b9\u662f\u4e0d\u540c\u7684\u3002

        • \u666e\u901a\u9012\u5f52\uff1a\u6c42\u548c\u64cd\u4f5c\u662f\u5728\u201c\u5f52\u201d\u7684\u8fc7\u7a0b\u4e2d\u6267\u884c\u7684\uff0c\u6bcf\u5c42\u8fd4\u56de\u540e\u90fd\u8981\u518d\u6267\u884c\u4e00\u6b21\u6c42\u548c\u64cd\u4f5c\u3002
        • \u5c3e\u9012\u5f52\uff1a\u6c42\u548c\u64cd\u4f5c\u662f\u5728\u201c\u9012\u201d\u7684\u8fc7\u7a0b\u4e2d\u6267\u884c\u7684\uff0c\u201c\u5f52\u201d\u7684\u8fc7\u7a0b\u53ea\u9700\u5c42\u5c42\u8fd4\u56de\u3002

        \u56fe 2-5 \u00a0 \u5c3e\u9012\u5f52\u8fc7\u7a0b

        Tip

        \u8bf7\u6ce8\u610f\uff0c\u8bb8\u591a\u7f16\u8bd1\u5668\u6216\u89e3\u91ca\u5668\u5e76\u4e0d\u652f\u6301\u5c3e\u9012\u5f52\u4f18\u5316\u3002\u4f8b\u5982\uff0cPython \u9ed8\u8ba4\u4e0d\u652f\u6301\u5c3e\u9012\u5f52\u4f18\u5316\uff0c\u56e0\u6b64\u5373\u4f7f\u51fd\u6570\u662f\u5c3e\u9012\u5f52\u5f62\u5f0f\uff0c\u4ecd\u7136\u53ef\u80fd\u4f1a\u9047\u5230\u6808\u6ea2\u51fa\u95ee\u9898\u3002

        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#3_1","title":"3. \u00a0 \u9012\u5f52\u6811","text":"

        \u5f53\u5904\u7406\u4e0e\u201c\u5206\u6cbb\u201d\u76f8\u5173\u7684\u7b97\u6cd5\u95ee\u9898\u65f6\uff0c\u9012\u5f52\u5f80\u5f80\u6bd4\u8fed\u4ee3\u7684\u601d\u8def\u66f4\u52a0\u76f4\u89c2\u3001\u4ee3\u7801\u66f4\u52a0\u6613\u8bfb\u3002\u4ee5\u201c\u6590\u6ce2\u90a3\u5951\u6570\u5217\u201d\u4e3a\u4f8b\u3002

        Question

        \u7ed9\u5b9a\u4e00\u4e2a\u6590\u6ce2\u90a3\u5951\u6570\u5217 \\(0, 1, 1, 2, 3, 5, 8, 13, \\dots\\) \uff0c\u6c42\u8be5\u6570\u5217\u7684\u7b2c \\(n\\) \u4e2a\u6570\u5b57\u3002

        \u8bbe\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u7b2c \\(n\\) \u4e2a\u6570\u5b57\u4e3a \\(f(n)\\) \uff0c\u6613\u5f97\u4e24\u4e2a\u7ed3\u8bba\u3002

        • \u6570\u5217\u7684\u524d\u4e24\u4e2a\u6570\u5b57\u4e3a \\(f(1) = 0\\) \u548c \\(f(2) = 1\\) \u3002
        • \u6570\u5217\u4e2d\u7684\u6bcf\u4e2a\u6570\u5b57\u662f\u524d\u4e24\u4e2a\u6570\u5b57\u7684\u548c\uff0c\u5373 \\(f(n) = f(n - 1) + f(n - 2)\\) \u3002

        \u6309\u7167\u9012\u63a8\u5173\u7cfb\u8fdb\u884c\u9012\u5f52\u8c03\u7528\uff0c\u5c06\u524d\u4e24\u4e2a\u6570\u5b57\u4f5c\u4e3a\u7ec8\u6b62\u6761\u4ef6\uff0c\u4fbf\u53ef\u5199\u51fa\u9012\u5f52\u4ee3\u7801\u3002\u8c03\u7528 fib(n) \u5373\u53ef\u5f97\u5230\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u7b2c \\(n\\) \u4e2a\u6570\u5b57\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
        def fib(n: int) -> int:\n    \"\"\"\u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52\"\"\"\n    # \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if n == 1 or n == 2:\n        return n - 1\n    # \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n    res = fib(n - 1) + fib(n - 2)\n    # \u8fd4\u56de\u7ed3\u679c f(n)\n    return res\n
        recursion.cpp
        /* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n    // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n    int res = fib(n - 1) + fib(n - 2);\n    // \u8fd4\u56de\u7ed3\u679c f(n)\n    return res;\n}\n
        recursion.java
        /* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n    // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n    int res = fib(n - 1) + fib(n - 2);\n    // \u8fd4\u56de\u7ed3\u679c f(n)\n    return res;\n}\n
        recursion.cs
        /* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint Fib(int n) {\n    // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n    int res = Fib(n - 1) + Fib(n - 2);\n    // \u8fd4\u56de\u7ed3\u679c f(n)\n    return res;\n}\n
        recursion.go
        /* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunc fib(n int) int {\n    // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if n == 1 || n == 2 {\n        return n - 1\n    }\n    // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n    res := fib(n-1) + fib(n-2)\n    // \u8fd4\u56de\u7ed3\u679c f(n)\n    return res\n}\n
        recursion.swift
        /* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunc fib(n: Int) -> Int {\n    // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if n == 1 || n == 2 {\n        return n - 1\n    }\n    // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n    let res = fib(n: n - 1) + fib(n: n - 2)\n    // \u8fd4\u56de\u7ed3\u679c f(n)\n    return res\n}\n
        recursion.js
        /* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunction fib(n) {\n    // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if (n === 1 || n === 2) return n - 1;\n    // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n    const res = fib(n - 1) + fib(n - 2);\n    // \u8fd4\u56de\u7ed3\u679c f(n)\n    return res;\n}\n
        recursion.ts
        /* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunction fib(n: number): number {\n    // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if (n === 1 || n === 2) return n - 1;\n    // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n    const res = fib(n - 1) + fib(n - 2);\n    // \u8fd4\u56de\u7ed3\u679c f(n)\n    return res;\n}\n
        recursion.dart
        /* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n  // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n  if (n == 1 || n == 2) return n - 1;\n  // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n  int res = fib(n - 1) + fib(n - 2);\n  // \u8fd4\u56de\u7ed3\u679c f(n)\n  return res;\n}\n
        recursion.rs
        /* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfn fib(n: i32) -> i32 {\n    // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if n == 1 || n == 2 {\n        return n - 1;\n    }\n    // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n    let res = fib(n - 1) + fib(n - 2);\n    // \u8fd4\u56de\u7ed3\u679c\n    res\n}\n
        recursion.c
        /* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n    // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n    int res = fib(n - 1) + fib(n - 2);\n    // \u8fd4\u56de\u7ed3\u679c f(n)\n    return res;\n}\n
        recursion.kt
        /* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfun fib(n: Int): Int {\n    // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1\n    // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n    val res = fib(n - 1) + fib(n - 2)\n    // \u8fd4\u56de\u7ed3\u679c f(n)\n    return res\n}\n
        recursion.rb
        ### \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 ###\ndef fib(n)\n  # \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n  return n - 1 if n == 1 || n == 2\n  # \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n  res = fib(n - 1) + fib(n - 2)\n  # \u8fd4\u56de\u7ed3\u679c f(n)\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u89c2\u5bdf\u4ee5\u4e0a\u4ee3\u7801\uff0c\u6211\u4eec\u5728\u51fd\u6570\u5185\u9012\u5f52\u8c03\u7528\u4e86\u4e24\u4e2a\u51fd\u6570\uff0c\u8fd9\u610f\u5473\u7740\u4ece\u4e00\u4e2a\u8c03\u7528\u4ea7\u751f\u4e86\u4e24\u4e2a\u8c03\u7528\u5206\u652f\u3002\u5982\u56fe 2-6 \u6240\u793a\uff0c\u8fd9\u6837\u4e0d\u65ad\u9012\u5f52\u8c03\u7528\u4e0b\u53bb\uff0c\u6700\u7ec8\u5c06\u4ea7\u751f\u4e00\u68f5\u5c42\u6570\u4e3a \\(n\\) \u7684\u9012\u5f52\u6811\uff08recursion tree\uff09\u3002

        \u56fe 2-6 \u00a0 \u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u9012\u5f52\u6811

        \u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u9012\u5f52\u4f53\u73b0\u4e86\u201c\u5c06\u95ee\u9898\u5206\u89e3\u4e3a\u66f4\u5c0f\u5b50\u95ee\u9898\u201d\u7684\u601d\u7ef4\u8303\u5f0f\uff0c\u8fd9\u79cd\u5206\u6cbb\u7b56\u7565\u81f3\u5173\u91cd\u8981\u3002

        • \u4ece\u7b97\u6cd5\u89d2\u5ea6\u770b\uff0c\u641c\u7d22\u3001\u6392\u5e8f\u3001\u56de\u6eaf\u3001\u5206\u6cbb\u3001\u52a8\u6001\u89c4\u5212\u7b49\u8bb8\u591a\u91cd\u8981\u7b97\u6cd5\u7b56\u7565\u76f4\u63a5\u6216\u95f4\u63a5\u5730\u5e94\u7528\u4e86\u8fd9\u79cd\u601d\u7ef4\u65b9\u5f0f\u3002
        • \u4ece\u6570\u636e\u7ed3\u6784\u89d2\u5ea6\u770b\uff0c\u9012\u5f52\u5929\u7136\u9002\u5408\u5904\u7406\u94fe\u8868\u3001\u6811\u548c\u56fe\u7684\u76f8\u5173\u95ee\u9898\uff0c\u56e0\u4e3a\u5b83\u4eec\u975e\u5e38\u9002\u5408\u7528\u5206\u6cbb\u601d\u60f3\u8fdb\u884c\u5206\u6790\u3002
        "},{"location":"chapter_computational_complexity/iteration_and_recursion/#223","title":"2.2.3 \u00a0 \u4e24\u8005\u5bf9\u6bd4","text":"

        \u603b\u7ed3\u4ee5\u4e0a\u5185\u5bb9\uff0c\u5982\u8868 2-1 \u6240\u793a\uff0c\u8fed\u4ee3\u548c\u9012\u5f52\u5728\u5b9e\u73b0\u3001\u6027\u80fd\u548c\u9002\u7528\u6027\u4e0a\u6709\u6240\u4e0d\u540c\u3002

        \u8868 2-1 \u00a0 \u8fed\u4ee3\u4e0e\u9012\u5f52\u7279\u70b9\u5bf9\u6bd4

        \u8fed\u4ee3 \u9012\u5f52 \u5b9e\u73b0\u65b9\u5f0f \u5faa\u73af\u7ed3\u6784 \u51fd\u6570\u8c03\u7528\u81ea\u8eab \u65f6\u95f4\u6548\u7387 \u6548\u7387\u901a\u5e38\u8f83\u9ad8\uff0c\u65e0\u51fd\u6570\u8c03\u7528\u5f00\u9500 \u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u90fd\u4f1a\u4ea7\u751f\u5f00\u9500 \u5185\u5b58\u4f7f\u7528 \u901a\u5e38\u4f7f\u7528\u56fa\u5b9a\u5927\u5c0f\u7684\u5185\u5b58\u7a7a\u95f4 \u7d2f\u79ef\u51fd\u6570\u8c03\u7528\u53ef\u80fd\u4f7f\u7528\u5927\u91cf\u7684\u6808\u5e27\u7a7a\u95f4 \u9002\u7528\u95ee\u9898 \u9002\u7528\u4e8e\u7b80\u5355\u5faa\u73af\u4efb\u52a1\uff0c\u4ee3\u7801\u76f4\u89c2\u3001\u53ef\u8bfb\u6027\u597d \u9002\u7528\u4e8e\u5b50\u95ee\u9898\u5206\u89e3\uff0c\u5982\u6811\u3001\u56fe\u3001\u5206\u6cbb\u3001\u56de\u6eaf\u7b49\uff0c\u4ee3\u7801\u7ed3\u6784\u7b80\u6d01\u3001\u6e05\u6670

        Tip

        \u5982\u679c\u611f\u89c9\u4ee5\u4e0b\u5185\u5bb9\u7406\u89e3\u56f0\u96be\uff0c\u53ef\u4ee5\u5728\u8bfb\u5b8c\u201c\u6808\u201d\u7ae0\u8282\u540e\u518d\u6765\u590d\u4e60\u3002

        \u90a3\u4e48\uff0c\u8fed\u4ee3\u548c\u9012\u5f52\u5177\u6709\u4ec0\u4e48\u5185\u5728\u8054\u7cfb\u5462\uff1f\u4ee5\u4e0a\u8ff0\u9012\u5f52\u51fd\u6570\u4e3a\u4f8b\uff0c\u6c42\u548c\u64cd\u4f5c\u5728\u9012\u5f52\u7684\u201c\u5f52\u201d\u9636\u6bb5\u8fdb\u884c\u3002\u8fd9\u610f\u5473\u7740\u6700\u521d\u88ab\u8c03\u7528\u7684\u51fd\u6570\u5b9e\u9645\u4e0a\u662f\u6700\u540e\u5b8c\u6210\u5176\u6c42\u548c\u64cd\u4f5c\u7684\uff0c\u8fd9\u79cd\u5de5\u4f5c\u673a\u5236\u4e0e\u6808\u7684\u201c\u5148\u5165\u540e\u51fa\u201d\u539f\u5219\u5f02\u66f2\u540c\u5de5\u3002

        \u4e8b\u5b9e\u4e0a\uff0c\u201c\u8c03\u7528\u6808\u201d\u548c\u201c\u6808\u5e27\u7a7a\u95f4\u201d\u8fd9\u7c7b\u9012\u5f52\u672f\u8bed\u5df2\u7ecf\u6697\u793a\u4e86\u9012\u5f52\u4e0e\u6808\u4e4b\u95f4\u7684\u5bc6\u5207\u5173\u7cfb\u3002

        1. \u9012\uff1a\u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u7cfb\u7edf\u4f1a\u5728\u201c\u8c03\u7528\u6808\u201d\u4e0a\u4e3a\u8be5\u51fd\u6570\u5206\u914d\u65b0\u7684\u6808\u5e27\uff0c\u7528\u4e8e\u5b58\u50a8\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\u3001\u53c2\u6570\u3001\u8fd4\u56de\u5730\u5740\u7b49\u6570\u636e\u3002
        2. \u5f52\uff1a\u5f53\u51fd\u6570\u5b8c\u6210\u6267\u884c\u5e76\u8fd4\u56de\u65f6\uff0c\u5bf9\u5e94\u7684\u6808\u5e27\u4f1a\u88ab\u4ece\u201c\u8c03\u7528\u6808\u201d\u4e0a\u79fb\u9664\uff0c\u6062\u590d\u4e4b\u524d\u51fd\u6570\u7684\u6267\u884c\u73af\u5883\u3002

        \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u8c03\u7528\u6808\u7684\u884c\u4e3a\uff0c\u4ece\u800c\u5c06\u9012\u5f52\u8f6c\u5316\u4e3a\u8fed\u4ee3\u5f62\u5f0f\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
        def for_loop_recur(n: int) -> int:\n    \"\"\"\u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52\"\"\"\n    # \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n    stack = []\n    res = 0\n    # \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    for i in range(n, 0, -1):\n        # \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n        stack.append(i)\n    # \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    while stack:\n        # \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n        res += stack.pop()\n    # res = 1+2+3+...+n\n    return res\n
        recursion.cpp
        /* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n    // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n    stack<int> stack;\n    int res = 0;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    for (int i = n; i > 0; i--) {\n        // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n        stack.push(i);\n    }\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    while (!stack.empty()) {\n        // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n        res += stack.top();\n        stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
        recursion.java
        /* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n    // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n    Stack<Integer> stack = new Stack<>();\n    int res = 0;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    for (int i = n; i > 0; i--) {\n        // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n        stack.push(i);\n    }\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    while (!stack.isEmpty()) {\n        // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n        res += stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
        recursion.cs
        /* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint ForLoopRecur(int n) {\n    // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n    Stack<int> stack = new();\n    int res = 0;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    for (int i = n; i > 0; i--) {\n        // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n        stack.Push(i);\n    }\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    while (stack.Count > 0) {\n        // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n        res += stack.Pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
        recursion.go
        /* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunc forLoopRecur(n int) int {\n    // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n    stack := list.New()\n    res := 0\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    for i := n; i > 0; i-- {\n        // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n        stack.PushBack(i)\n    }\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    for stack.Len() != 0 {\n        // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n        res += stack.Back().Value.(int)\n        stack.Remove(stack.Back())\n    }\n    // res = 1+2+3+...+n\n    return res\n}\n
        recursion.swift
        /* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunc forLoopRecur(n: Int) -> Int {\n    // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n    var stack: [Int] = []\n    var res = 0\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    for i in (1 ... n).reversed() {\n        // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n        stack.append(i)\n    }\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    while !stack.isEmpty {\n        // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n        res += stack.removeLast()\n    }\n    // res = 1+2+3+...+n\n    return res\n}\n
        recursion.js
        /* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunction forLoopRecur(n) {\n    // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n    const stack = [];\n    let res = 0;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    for (let i = n; i > 0; i--) {\n        // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n        stack.push(i);\n    }\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    while (stack.length) {\n        // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n        res += stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
        recursion.ts
        /* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunction forLoopRecur(n: number): number {\n    // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808 \n    const stack: number[] = [];\n    let res: number = 0;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    for (let i = n; i > 0; i--) {\n        // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n        stack.push(i);\n    }\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    while (stack.length) {\n        // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n        res += stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
        recursion.dart
        /* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n  // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n  List<int> stack = [];\n  int res = 0;\n  // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n  for (int i = n; i > 0; i--) {\n    // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n    stack.add(i);\n  }\n  // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n  while (!stack.isEmpty) {\n    // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n    res += stack.removeLast();\n  }\n  // res = 1+2+3+...+n\n  return res;\n}\n
        recursion.rs
        /* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfn for_loop_recur(n: i32) -> i32 {\n    // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n    let mut stack = Vec::new();\n    let mut res = 0;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    for i in (1..=n).rev() {\n        // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n        stack.push(i);\n    }\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    while !stack.is_empty() {\n        // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n        res += stack.pop().unwrap();\n    }\n    // res = 1+2+3+...+n\n    res\n}\n
        recursion.c
        /* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n    int stack[1000]; // \u501f\u52a9\u4e00\u4e2a\u5927\u6570\u7ec4\u6765\u6a21\u62df\u6808\n    int top = -1;    // \u6808\u9876\u7d22\u5f15\n    int res = 0;\n    // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n    for (int i = n; i > 0; i--) {\n        // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n        stack[1 + top++] = i;\n    }\n    // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n    while (top >= 0) {\n        // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n        res += stack[top--];\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
        recursion.kt
        /* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfun forLoopRecur(n: Int): Int {\n    // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n    val stack = Stack<Int>()\n    var res = 0\n    // \u9012: \u9012\u5f52\u8c03\u7528\n    for (i in n downTo 0) {\n        // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n        stack.push(i)\n    }\n    // \u5f52: \u8fd4\u56de\u7ed3\u679c\n    while (stack.isNotEmpty()) {\n        // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n        res += stack.pop()\n    }\n    // res = 1+2+3+...+n\n    return res\n}\n
        recursion.rb
        ### \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 ###\ndef for_loop_recur(n)\n  # \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n  stack = []\n  res = 0\n\n  # \u9012\uff1a\u9012\u5f52\u8c03\u7528\n  for i in n.downto(0)\n    # \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n    stack << i\n  end\n  # \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n  while !stack.empty?\n    res += stack.pop\n  end\n\n  # res = 1+2+3+...+n\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u89c2\u5bdf\u4ee5\u4e0a\u4ee3\u7801\uff0c\u5f53\u9012\u5f52\u8f6c\u5316\u4e3a\u8fed\u4ee3\u540e\uff0c\u4ee3\u7801\u53d8\u5f97\u66f4\u52a0\u590d\u6742\u4e86\u3002\u5c3d\u7ba1\u8fed\u4ee3\u548c\u9012\u5f52\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4e92\u76f8\u8f6c\u5316\uff0c\u4f46\u4e0d\u4e00\u5b9a\u503c\u5f97\u8fd9\u6837\u505a\uff0c\u6709\u4ee5\u4e0b\u4e24\u70b9\u539f\u56e0\u3002

        • \u8f6c\u5316\u540e\u7684\u4ee3\u7801\u53ef\u80fd\u66f4\u52a0\u96be\u4ee5\u7406\u89e3\uff0c\u53ef\u8bfb\u6027\u66f4\u5dee\u3002
        • \u5bf9\u4e8e\u67d0\u4e9b\u590d\u6742\u95ee\u9898\uff0c\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\u7684\u884c\u4e3a\u53ef\u80fd\u975e\u5e38\u56f0\u96be\u3002

        \u603b\u4e4b\uff0c\u9009\u62e9\u8fed\u4ee3\u8fd8\u662f\u9012\u5f52\u53d6\u51b3\u4e8e\u7279\u5b9a\u95ee\u9898\u7684\u6027\u8d28\u3002\u5728\u7f16\u7a0b\u5b9e\u8df5\u4e2d\uff0c\u6743\u8861\u4e24\u8005\u7684\u4f18\u52a3\u5e76\u6839\u636e\u60c5\u5883\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u81f3\u5173\u91cd\u8981\u3002

        "},{"location":"chapter_computational_complexity/performance_evaluation/","title":"2.1 \u00a0 \u7b97\u6cd5\u6548\u7387\u8bc4\u4f30","text":"

        \u5728\u7b97\u6cd5\u8bbe\u8ba1\u4e2d\uff0c\u6211\u4eec\u5148\u540e\u8ffd\u6c42\u4ee5\u4e0b\u4e24\u4e2a\u5c42\u9762\u7684\u76ee\u6807\u3002

        1. \u627e\u5230\u95ee\u9898\u89e3\u6cd5\uff1a\u7b97\u6cd5\u9700\u8981\u5728\u89c4\u5b9a\u7684\u8f93\u5165\u8303\u56f4\u5185\u53ef\u9760\u5730\u6c42\u5f97\u95ee\u9898\u7684\u6b63\u786e\u89e3\u3002
        2. \u5bfb\u6c42\u6700\u4f18\u89e3\u6cd5\uff1a\u540c\u4e00\u4e2a\u95ee\u9898\u53ef\u80fd\u5b58\u5728\u591a\u79cd\u89e3\u6cd5\uff0c\u6211\u4eec\u5e0c\u671b\u627e\u5230\u5c3d\u53ef\u80fd\u9ad8\u6548\u7684\u7b97\u6cd5\u3002

        \u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u80fd\u591f\u89e3\u51b3\u95ee\u9898\u7684\u524d\u63d0\u4e0b\uff0c\u7b97\u6cd5\u6548\u7387\u5df2\u6210\u4e3a\u8861\u91cf\u7b97\u6cd5\u4f18\u52a3\u7684\u4e3b\u8981\u8bc4\u4ef7\u6307\u6807\uff0c\u5b83\u5305\u62ec\u4ee5\u4e0b\u4e24\u4e2a\u7ef4\u5ea6\u3002

        • \u65f6\u95f4\u6548\u7387\uff1a\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u7684\u957f\u77ed\u3002
        • \u7a7a\u95f4\u6548\u7387\uff1a\u7b97\u6cd5\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u7684\u5927\u5c0f\u3002

        \u7b80\u800c\u8a00\u4e4b\uff0c\u6211\u4eec\u7684\u76ee\u6807\u662f\u8bbe\u8ba1\u201c\u65e2\u5feb\u53c8\u7701\u201d\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u3002\u800c\u6709\u6548\u5730\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u53ea\u6709\u8fd9\u6837\uff0c\u6211\u4eec\u624d\u80fd\u5c06\u5404\u79cd\u7b97\u6cd5\u8fdb\u884c\u5bf9\u6bd4\uff0c\u8fdb\u800c\u6307\u5bfc\u7b97\u6cd5\u8bbe\u8ba1\u4e0e\u4f18\u5316\u8fc7\u7a0b\u3002

        \u6548\u7387\u8bc4\u4f30\u65b9\u6cd5\u4e3b\u8981\u5206\u4e3a\u4e24\u79cd\uff1a\u5b9e\u9645\u6d4b\u8bd5\u3001\u7406\u8bba\u4f30\u7b97\u3002

        "},{"location":"chapter_computational_complexity/performance_evaluation/#211","title":"2.1.1 \u00a0 \u5b9e\u9645\u6d4b\u8bd5","text":"

        \u5047\u8bbe\u6211\u4eec\u73b0\u5728\u6709\u7b97\u6cd5 A \u548c\u7b97\u6cd5 B \uff0c\u5b83\u4eec\u90fd\u80fd\u89e3\u51b3\u540c\u4e00\u95ee\u9898\uff0c\u73b0\u5728\u9700\u8981\u5bf9\u6bd4\u8fd9\u4e24\u4e2a\u7b97\u6cd5\u7684\u6548\u7387\u3002\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u627e\u4e00\u53f0\u8ba1\u7b97\u673a\uff0c\u8fd0\u884c\u8fd9\u4e24\u4e2a\u7b97\u6cd5\uff0c\u5e76\u76d1\u63a7\u8bb0\u5f55\u5b83\u4eec\u7684\u8fd0\u884c\u65f6\u95f4\u548c\u5185\u5b58\u5360\u7528\u60c5\u51b5\u3002\u8fd9\u79cd\u8bc4\u4f30\u65b9\u5f0f\u80fd\u591f\u53cd\u6620\u771f\u5b9e\u60c5\u51b5\uff0c\u4f46\u4e5f\u5b58\u5728\u8f83\u5927\u7684\u5c40\u9650\u6027\u3002

        \u4e00\u65b9\u9762\uff0c\u96be\u4ee5\u6392\u9664\u6d4b\u8bd5\u73af\u5883\u7684\u5e72\u6270\u56e0\u7d20\u3002\u786c\u4ef6\u914d\u7f6e\u4f1a\u5f71\u54cd\u7b97\u6cd5\u7684\u6027\u80fd\u8868\u73b0\u3002\u6bd4\u5982\u4e00\u4e2a\u7b97\u6cd5\u7684\u5e76\u884c\u5ea6\u8f83\u9ad8\uff0c\u90a3\u4e48\u5b83\u5c31\u66f4\u9002\u5408\u5728\u591a\u6838 CPU \u4e0a\u8fd0\u884c\uff0c\u4e00\u4e2a\u7b97\u6cd5\u7684\u5185\u5b58\u64cd\u4f5c\u5bc6\u96c6\uff0c\u90a3\u4e48\u5b83\u5728\u9ad8\u6027\u80fd\u5185\u5b58\u4e0a\u7684\u8868\u73b0\u5c31\u4f1a\u66f4\u597d\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u7b97\u6cd5\u5728\u4e0d\u540c\u7684\u673a\u5668\u4e0a\u7684\u6d4b\u8bd5\u7ed3\u679c\u53ef\u80fd\u662f\u4e0d\u4e00\u81f4\u7684\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u9700\u8981\u5728\u5404\u79cd\u673a\u5668\u4e0a\u8fdb\u884c\u6d4b\u8bd5\uff0c\u7edf\u8ba1\u5e73\u5747\u6548\u7387\uff0c\u800c\u8fd9\u662f\u4e0d\u73b0\u5b9e\u7684\u3002

        \u53e6\u4e00\u65b9\u9762\uff0c\u5c55\u5f00\u5b8c\u6574\u6d4b\u8bd5\u975e\u5e38\u8017\u8d39\u8d44\u6e90\u3002\u968f\u7740\u8f93\u5165\u6570\u636e\u91cf\u7684\u53d8\u5316\uff0c\u7b97\u6cd5\u4f1a\u8868\u73b0\u51fa\u4e0d\u540c\u7684\u6548\u7387\u3002\u4f8b\u5982\uff0c\u5728\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5c0f\u65f6\uff0c\u7b97\u6cd5 A \u7684\u8fd0\u884c\u65f6\u95f4\u6bd4\u7b97\u6cd5 B \u77ed\uff1b\u800c\u5728\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5927\u65f6\uff0c\u6d4b\u8bd5\u7ed3\u679c\u53ef\u80fd\u6070\u6070\u76f8\u53cd\u3002\u56e0\u6b64\uff0c\u4e3a\u4e86\u5f97\u5230\u6709\u8bf4\u670d\u529b\u7684\u7ed3\u8bba\uff0c\u6211\u4eec\u9700\u8981\u6d4b\u8bd5\u5404\u79cd\u89c4\u6a21\u7684\u8f93\u5165\u6570\u636e\uff0c\u800c\u8fd9\u9700\u8981\u8017\u8d39\u5927\u91cf\u7684\u8ba1\u7b97\u8d44\u6e90\u3002

        "},{"location":"chapter_computational_complexity/performance_evaluation/#212","title":"2.1.2 \u00a0 \u7406\u8bba\u4f30\u7b97","text":"

        \u7531\u4e8e\u5b9e\u9645\u6d4b\u8bd5\u5177\u6709\u8f83\u5927\u7684\u5c40\u9650\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u4ec5\u901a\u8fc7\u4e00\u4e9b\u8ba1\u7b97\u6765\u8bc4\u4f30\u7b97\u6cd5\u7684\u6548\u7387\u3002\u8fd9\u79cd\u4f30\u7b97\u65b9\u6cd5\u88ab\u79f0\u4e3a\u6e10\u8fd1\u590d\u6742\u5ea6\u5206\u6790\uff08asymptotic complexity analysis\uff09\uff0c\u7b80\u79f0\u590d\u6742\u5ea6\u5206\u6790\u3002

        \u590d\u6742\u5ea6\u5206\u6790\u80fd\u591f\u4f53\u73b0\u7b97\u6cd5\u8fd0\u884c\u6240\u9700\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u8d44\u6e90\u4e0e\u8f93\u5165\u6570\u636e\u89c4\u6a21\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u5b83\u63cf\u8ff0\u4e86\u968f\u7740\u8f93\u5165\u6570\u636e\u89c4\u6a21\u7684\u589e\u52a0\uff0c\u7b97\u6cd5\u6267\u884c\u6240\u9700\u65f6\u95f4\u548c\u7a7a\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u3002\u8fd9\u4e2a\u5b9a\u4e49\u6709\u4e9b\u62d7\u53e3\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u5206\u4e3a\u4e09\u4e2a\u91cd\u70b9\u6765\u7406\u89e3\u3002

        • \u201c\u65f6\u95f4\u548c\u7a7a\u95f4\u8d44\u6e90\u201d\u5206\u522b\u5bf9\u5e94\u65f6\u95f4\u590d\u6742\u5ea6\uff08time complexity\uff09\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\uff08space complexity\uff09\u3002
        • \u201c\u968f\u7740\u8f93\u5165\u6570\u636e\u89c4\u6a21\u7684\u589e\u52a0\u201d\u610f\u5473\u7740\u590d\u6742\u5ea6\u53cd\u6620\u4e86\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u4e0e\u8f93\u5165\u6570\u636e\u89c4\u6a21\u4e4b\u95f4\u7684\u5173\u7cfb\u3002
        • \u201c\u65f6\u95f4\u548c\u7a7a\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u201d\u8868\u793a\u590d\u6742\u5ea6\u5206\u6790\u5173\u6ce8\u7684\u4e0d\u662f\u8fd0\u884c\u65f6\u95f4\u6216\u5360\u7528\u7a7a\u95f4\u7684\u5177\u4f53\u503c\uff0c\u800c\u662f\u65f6\u95f4\u6216\u7a7a\u95f4\u589e\u957f\u7684\u201c\u5feb\u6162\u201d\u3002

        \u590d\u6742\u5ea6\u5206\u6790\u514b\u670d\u4e86\u5b9e\u9645\u6d4b\u8bd5\u65b9\u6cd5\u7684\u5f0a\u7aef\uff0c\u4f53\u73b0\u5728\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u3002

        • \u5b83\u65e0\u9700\u5b9e\u9645\u8fd0\u884c\u4ee3\u7801\uff0c\u66f4\u52a0\u7eff\u8272\u8282\u80fd\u3002
        • \u5b83\u72ec\u7acb\u4e8e\u6d4b\u8bd5\u73af\u5883\uff0c\u5206\u6790\u7ed3\u679c\u9002\u7528\u4e8e\u6240\u6709\u8fd0\u884c\u5e73\u53f0\u3002
        • \u5b83\u53ef\u4ee5\u4f53\u73b0\u4e0d\u540c\u6570\u636e\u91cf\u4e0b\u7684\u7b97\u6cd5\u6548\u7387\uff0c\u5c24\u5176\u662f\u5728\u5927\u6570\u636e\u91cf\u4e0b\u7684\u7b97\u6cd5\u6027\u80fd\u3002

        Tip

        \u5982\u679c\u4f60\u4ecd\u5bf9\u590d\u6742\u5ea6\u7684\u6982\u5ff5\u611f\u5230\u56f0\u60d1\uff0c\u65e0\u987b\u62c5\u5fc3\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7ae0\u8282\u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u3002

        \u590d\u6742\u5ea6\u5206\u6790\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u628a\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\u7684\u201c\u6807\u5c3a\u201d\uff0c\u4f7f\u6211\u4eec\u53ef\u4ee5\u8861\u91cf\u6267\u884c\u67d0\u4e2a\u7b97\u6cd5\u6240\u9700\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u8d44\u6e90\uff0c\u5bf9\u6bd4\u4e0d\u540c\u7b97\u6cd5\u4e4b\u95f4\u7684\u6548\u7387\u3002

        \u590d\u6742\u5ea6\u662f\u4e2a\u6570\u5b66\u6982\u5ff5\uff0c\u5bf9\u4e8e\u521d\u5b66\u8005\u53ef\u80fd\u6bd4\u8f83\u62bd\u8c61\uff0c\u5b66\u4e60\u96be\u5ea6\u76f8\u5bf9\u8f83\u9ad8\u3002\u4ece\u8fd9\u4e2a\u89d2\u5ea6\u770b\uff0c\u590d\u6742\u5ea6\u5206\u6790\u53ef\u80fd\u4e0d\u592a\u9002\u5408\u4f5c\u4e3a\u6700\u5148\u4ecb\u7ecd\u7684\u5185\u5bb9\u3002\u7136\u800c\uff0c\u5f53\u6211\u4eec\u8ba8\u8bba\u67d0\u4e2a\u6570\u636e\u7ed3\u6784\u6216\u7b97\u6cd5\u7684\u7279\u70b9\u65f6\uff0c\u96be\u4ee5\u907f\u514d\u8981\u5206\u6790\u5176\u8fd0\u884c\u901f\u5ea6\u548c\u7a7a\u95f4\u4f7f\u7528\u60c5\u51b5\u3002

        \u7efc\u4e0a\u6240\u8ff0\uff0c\u5efa\u8bae\u4f60\u5728\u6df1\u5165\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u4e4b\u524d\uff0c\u5148\u5bf9\u590d\u6742\u5ea6\u5206\u6790\u5efa\u7acb\u521d\u6b65\u7684\u4e86\u89e3\uff0c\u4ee5\u4fbf\u80fd\u591f\u5b8c\u6210\u7b80\u5355\u7b97\u6cd5\u7684\u590d\u6742\u5ea6\u5206\u6790\u3002

        "},{"location":"chapter_computational_complexity/space_complexity/","title":"2.4 \u00a0 \u7a7a\u95f4\u590d\u6742\u5ea6","text":"

        \u7a7a\u95f4\u590d\u6742\u5ea6\uff08space complexity\uff09\u7528\u4e8e\u8861\u91cf\u7b97\u6cd5\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u968f\u7740\u6570\u636e\u91cf\u53d8\u5927\u65f6\u7684\u589e\u957f\u8d8b\u52bf\u3002\u8fd9\u4e2a\u6982\u5ff5\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u975e\u5e38\u7c7b\u4f3c\uff0c\u53ea\u9700\u5c06\u201c\u8fd0\u884c\u65f6\u95f4\u201d\u66ff\u6362\u4e3a\u201c\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u201d\u3002

        "},{"location":"chapter_computational_complexity/space_complexity/#241","title":"2.4.1 \u00a0 \u7b97\u6cd5\u76f8\u5173\u7a7a\u95f4","text":"

        \u7b97\u6cd5\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\u3002

        • \u8f93\u5165\u7a7a\u95f4\uff1a\u7528\u4e8e\u5b58\u50a8\u7b97\u6cd5\u7684\u8f93\u5165\u6570\u636e\u3002
        • \u6682\u5b58\u7a7a\u95f4\uff1a\u7528\u4e8e\u5b58\u50a8\u7b97\u6cd5\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u53d8\u91cf\u3001\u5bf9\u8c61\u3001\u51fd\u6570\u4e0a\u4e0b\u6587\u7b49\u6570\u636e\u3002
        • \u8f93\u51fa\u7a7a\u95f4\uff1a\u7528\u4e8e\u5b58\u50a8\u7b97\u6cd5\u7684\u8f93\u51fa\u6570\u636e\u3002

        \u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u7edf\u8ba1\u8303\u56f4\u662f\u201c\u6682\u5b58\u7a7a\u95f4\u201d\u52a0\u4e0a\u201c\u8f93\u51fa\u7a7a\u95f4\u201d\u3002

        \u6682\u5b58\u7a7a\u95f4\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5212\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\u3002

        • \u6682\u5b58\u6570\u636e\uff1a\u7528\u4e8e\u4fdd\u5b58\u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u5404\u79cd\u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u7b49\u3002
        • \u6808\u5e27\u7a7a\u95f4\uff1a\u7528\u4e8e\u4fdd\u5b58\u8c03\u7528\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u6570\u636e\u3002\u7cfb\u7edf\u5728\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u65f6\u90fd\u4f1a\u5728\u6808\u9876\u90e8\u521b\u5efa\u4e00\u4e2a\u6808\u5e27\uff0c\u51fd\u6570\u8fd4\u56de\u540e\uff0c\u6808\u5e27\u7a7a\u95f4\u4f1a\u88ab\u91ca\u653e\u3002
        • \u6307\u4ee4\u7a7a\u95f4\uff1a\u7528\u4e8e\u4fdd\u5b58\u7f16\u8bd1\u540e\u7684\u7a0b\u5e8f\u6307\u4ee4\uff0c\u5728\u5b9e\u9645\u7edf\u8ba1\u4e2d\u901a\u5e38\u5ffd\u7565\u4e0d\u8ba1\u3002

        \u5728\u5206\u6790\u4e00\u6bb5\u7a0b\u5e8f\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u7edf\u8ba1\u6682\u5b58\u6570\u636e\u3001\u6808\u5e27\u7a7a\u95f4\u548c\u8f93\u51fa\u6570\u636e\u4e09\u90e8\u5206\uff0c\u5982\u56fe 2-15 \u6240\u793a\u3002

        \u56fe 2-15 \u00a0 \u7b97\u6cd5\u4f7f\u7528\u7684\u76f8\u5173\u7a7a\u95f4

        \u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        class Node:\n    \"\"\"\u7c7b\"\"\"\n    def __init__(self, x: int):\n        self.val: int = x              # \u8282\u70b9\u503c\n        self.next: Node | None = None  # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n\ndef function() -> int:\n    \"\"\"\u51fd\u6570\"\"\"\n    # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n    return 0\n\ndef algorithm(n) -> int:  # \u8f93\u5165\u6570\u636e\n    A = 0                 # \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff0c\u4e00\u822c\u7528\u5927\u5199\u5b57\u6bcd\u8868\u793a\uff09\n    b = 0                 # \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    node = Node(0)        # \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n    c = function()        # \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    return A + b + c      # \u8f93\u51fa\u6570\u636e\n
        /* \u7ed3\u6784\u4f53 */\nstruct Node {\n    int val;\n    Node *next;\n    Node(int x) : val(x), next(nullptr) {}\n};\n\n/* \u51fd\u6570 */\nint func() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n    return 0;\n}\n\nint algorithm(int n) {        // \u8f93\u5165\u6570\u636e\n    const int a = 0;          // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n    int b = 0;                // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    Node* node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n    int c = func();           // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    return a + b + c;         // \u8f93\u51fa\u6570\u636e\n}\n
        /* \u7c7b */\nclass Node {\n    int val;\n    Node next;\n    Node(int x) { val = x; }\n}\n\n/* \u51fd\u6570 */\nint function() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n    return 0;\n}\n\nint algorithm(int n) {        // \u8f93\u5165\u6570\u636e\n    final int a = 0;          // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n    int b = 0;                // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    Node node = new Node(0);  // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n    int c = function();       // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    return a + b + c;         // \u8f93\u51fa\u6570\u636e\n}\n
        /* \u7c7b */\nclass Node(int x) {\n    int val = x;\n    Node next;\n}\n\n/* \u51fd\u6570 */\nint Function() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n    return 0;\n}\n\nint Algorithm(int n) {        // \u8f93\u5165\u6570\u636e\n    const int a = 0;          // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n    int b = 0;                // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    Node node = new(0);       // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n    int c = Function();       // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    return a + b + c;         // \u8f93\u51fa\u6570\u636e\n}\n
        /* \u7ed3\u6784\u4f53 */\ntype node struct {\n    val  int\n    next *node\n}\n\n/* \u521b\u5efa node \u7ed3\u6784\u4f53  */\nfunc newNode(val int) *node {\n    return &node{val: val}\n}\n\n/* \u51fd\u6570 */\nfunc function() int {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n    return 0\n}\n\nfunc algorithm(n int) int { // \u8f93\u5165\u6570\u636e\n    const a = 0             // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n    b := 0                  // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    newNode(0)              // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n    c := function()         // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    return a + b + c        // \u8f93\u51fa\u6570\u636e\n}\n
        /* \u7c7b */\nclass Node {\n    var val: Int\n    var next: Node?\n\n    init(x: Int) {\n        val = x\n    }\n}\n\n/* \u51fd\u6570 */\nfunc function() -> Int {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n    return 0\n}\n\nfunc algorithm(n: Int) -> Int { // \u8f93\u5165\u6570\u636e\n    let a = 0             // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n    var b = 0             // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    let node = Node(x: 0) // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n    let c = function()    // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    return a + b + c      // \u8f93\u51fa\u6570\u636e\n}\n
        /* \u7c7b */\nclass Node {\n    val;\n    next;\n    constructor(val) {\n        this.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\n        this.next = null;                       // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n    }\n}\n\n/* \u51fd\u6570 */\nfunction constFunc() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n\nfunction algorithm(n) {       // \u8f93\u5165\u6570\u636e\n    const a = 0;              // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n    let b = 0;                // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    const node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n    const c = constFunc();    // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    return a + b + c;         // \u8f93\u51fa\u6570\u636e\n}\n
        /* \u7c7b */\nclass Node {\n    val: number;\n    next: Node | null;\n    constructor(val?: number) {\n        this.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\n        this.next = null;                       // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n    }\n}\n\n/* \u51fd\u6570 */\nfunction constFunc(): number {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n\nfunction algorithm(n: number): number { // \u8f93\u5165\u6570\u636e\n    const a = 0;                        // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n    let b = 0;                          // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    const node = new Node(0);           // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n    const c = constFunc();              // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    return a + b + c;                   // \u8f93\u51fa\u6570\u636e\n}\n
        /* \u7c7b */\nclass Node {\n  int val;\n  Node next;\n  Node(this.val, [this.next]);\n}\n\n/* \u51fd\u6570 */\nint function() {\n  // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n  return 0;\n}\n\nint algorithm(int n) {  // \u8f93\u5165\u6570\u636e\n  const int a = 0;      // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n  int b = 0;            // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n  Node node = Node(0);  // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n  int c = function();   // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n  return a + b + c;     // \u8f93\u51fa\u6570\u636e\n}\n
        use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u7ed3\u6784\u4f53 */\nstruct Node {\n    val: i32,\n    next: Option<Rc<RefCell<Node>>>,\n}\n\n/* \u521b\u5efa Node \u7ed3\u6784\u4f53 */\nimpl Node {\n    fn new(val: i32) -> Self {\n        Self { val: val, next: None }\n    }\n}\n\n/* \u51fd\u6570 */\nfn function() -> i32 {      \n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n    return 0;\n}\n\nfn algorithm(n: i32) -> i32 {       // \u8f93\u5165\u6570\u636e\n    const a: i32 = 0;               // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n    let mut b = 0;                  // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    let node = Node::new(0);        // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n    let c = function();             // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    return a + b + c;               // \u8f93\u51fa\u6570\u636e\n}\n
        /* \u51fd\u6570 */\nint func() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n    return 0;\n}\n\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\n    const int a = 0;   // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n    int b = 0;         // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    int c = func();    // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    return a + b + c;  // \u8f93\u51fa\u6570\u636e\n}\n
        /* \u7c7b */\nclass Node(var _val: Int) {\n    var next: Node? = null\n}\n\n/* \u51fd\u6570 */\nfun function(): Int {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n    return 0\n}\n\nfun algorithm(n: Int): Int { // \u8f93\u5165\u6570\u636e\n    val a = 0                // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n    var b = 0                // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    val node = Node(0)       // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n    val c = function()       // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    return a + b + c         // \u8f93\u51fa\u6570\u636e\n}\n
        ### \u7c7b ###\nclass Node\n    attr_accessor :val      # \u8282\u70b9\u503c\n    attr_accessor :next     # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n\n    def initialize(x)\n        @val = x\n    end\nend\n\n### \u51fd\u6570 ###\ndef function\n    # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n    0\nend\n\n### \u7b97\u6cd5 ###\ndef algorithm(n)        # \u8f93\u5165\u6570\u636e\n    a = 0               # \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n    b = 0               # \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n    node = Node.new(0)  # \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n    c = function        # \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n    a + b + c           # \u8f93\u51fa\u6570\u636e\nend\n
        "},{"location":"chapter_computational_complexity/space_complexity/#242","title":"2.4.2 \u00a0 \u63a8\u7b97\u65b9\u6cd5","text":"

        \u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u5927\u81f4\u76f8\u540c\uff0c\u53ea\u9700\u5c06\u7edf\u8ba1\u5bf9\u8c61\u4ece\u201c\u64cd\u4f5c\u6570\u91cf\u201d\u8f6c\u4e3a\u201c\u4f7f\u7528\u7a7a\u95f4\u5927\u5c0f\u201d\u3002

        \u800c\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u540c\u7684\u662f\uff0c\u6211\u4eec\u901a\u5e38\u53ea\u5173\u6ce8\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u3002\u8fd9\u662f\u56e0\u4e3a\u5185\u5b58\u7a7a\u95f4\u662f\u4e00\u9879\u786c\u6027\u8981\u6c42\uff0c\u6211\u4eec\u5fc5\u987b\u786e\u4fdd\u5728\u6240\u6709\u8f93\u5165\u6570\u636e\u4e0b\u90fd\u6709\u8db3\u591f\u7684\u5185\u5b58\u7a7a\u95f4\u9884\u7559\u3002

        \u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e2d\u7684\u201c\u6700\u5dee\u201d\u6709\u4e24\u5c42\u542b\u4e49\u3002

        1. \u4ee5\u6700\u5dee\u8f93\u5165\u6570\u636e\u4e3a\u51c6\uff1a\u5f53 \\(n < 10\\) \u65f6\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff1b\u4f46\u5f53 \\(n > 10\\) \u65f6\uff0c\u521d\u59cb\u5316\u7684\u6570\u7ec4 nums \u5360\u7528 \\(O(n)\\) \u7a7a\u95f4\uff0c\u56e0\u6b64\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002
        2. \u4ee5\u7b97\u6cd5\u8fd0\u884c\u4e2d\u7684\u5cf0\u503c\u5185\u5b58\u4e3a\u51c6\uff1a\u4f8b\u5982\uff0c\u7a0b\u5e8f\u5728\u6267\u884c\u6700\u540e\u4e00\u884c\u4e4b\u524d\uff0c\u5360\u7528 \\(O(1)\\) \u7a7a\u95f4\uff1b\u5f53\u521d\u59cb\u5316\u6570\u7ec4 nums \u65f6\uff0c\u7a0b\u5e8f\u5360\u7528 \\(O(n)\\) \u7a7a\u95f4\uff0c\u56e0\u6b64\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        def algorithm(n: int):\n    a = 0               # O(1)\n    b = [0] * 10000     # O(1)\n    if n > 10:\n        nums = [0] * n  # O(n)\n
        void algorithm(int n) {\n    int a = 0;               // O(1)\n    vector<int> b(10000);    // O(1)\n    if (n > 10)\n        vector<int> nums(n); // O(n)\n}\n
        void algorithm(int n) {\n    int a = 0;                   // O(1)\n    int[] b = new int[10000];    // O(1)\n    if (n > 10)\n        int[] nums = new int[n]; // O(n)\n}\n
        void Algorithm(int n) {\n    int a = 0;                   // O(1)\n    int[] b = new int[10000];    // O(1)\n    if (n > 10) {\n        int[] nums = new int[n]; // O(n)\n    }\n}\n
        func algorithm(n int) {\n    a := 0                      // O(1)\n    b := make([]int, 10000)     // O(1)\n    var nums []int\n    if n > 10 {\n        nums := make([]int, n)  // O(n)\n    }\n    fmt.Println(a, b, nums)\n}\n
        func algorithm(n: Int) {\n    let a = 0 // O(1)\n    let b = Array(repeating: 0, count: 10000) // O(1)\n    if n > 10 {\n        let nums = Array(repeating: 0, count: n) // O(n)\n    }\n}\n
        function algorithm(n) {\n    const a = 0;                   // O(1)\n    const b = new Array(10000);    // O(1)\n    if (n > 10) {\n        const nums = new Array(n); // O(n)\n    }\n}\n
        function algorithm(n: number): void {\n    const a = 0;                   // O(1)\n    const b = new Array(10000);    // O(1)\n    if (n > 10) {\n        const nums = new Array(n); // O(n)\n    }\n}\n
        void algorithm(int n) {\n  int a = 0;                            // O(1)\n  List<int> b = List.filled(10000, 0);  // O(1)\n  if (n > 10) {\n    List<int> nums = List.filled(n, 0); // O(n)\n  }\n}\n
        fn algorithm(n: i32) {\n    let a = 0;                              // O(1)\n    let b = [0; 10000];                     // O(1)\n    if n > 10 {\n        let nums = vec![0; n as usize];     // O(n)\n    }\n}\n
        void algorithm(int n) {\n    int a = 0;               // O(1)\n    int b[10000];            // O(1)\n    if (n > 10)\n        int nums[n] = {0};   // O(n)\n}\n
        fun algorithm(n: Int) {\n    val a = 0                    // O(1)\n    val b = IntArray(10000)      // O(1)\n    if (n > 10) {\n        val nums = IntArray(n)   // O(n)\n    }\n}\n
        def algorithm(n)\n    a = 0                           # O(1)\n    b = Array.new(10000)            # O(1)\n    nums = Array.new(n) if n > 10   # O(n)\nend\n

        \u5728\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u7edf\u8ba1\u6808\u5e27\u7a7a\u95f4\u3002\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        def function() -> int:\n    # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0\n\ndef loop(n: int):\n    \"\"\"\u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1)\"\"\"\n    for _ in range(n):\n        function()\n\ndef recur(n: int):\n    \"\"\"\u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\"\"\"\n    if n == 1:\n        return\n    return recur(n - 1)\n
        int func() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n/* \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) */\nvoid loop(int n) {\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n/* \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) */\nvoid recur(int n) {\n    if (n == 1) return;\n    recur(n - 1);\n}\n
        int function() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n/* \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) */\nvoid loop(int n) {\n    for (int i = 0; i < n; i++) {\n        function();\n    }\n}\n/* \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) */\nvoid recur(int n) {\n    if (n == 1) return;\n    recur(n - 1);\n}\n
        int Function() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n/* \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) */\nvoid Loop(int n) {\n    for (int i = 0; i < n; i++) {\n        Function();\n    }\n}\n/* \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) */\nint Recur(int n) {\n    if (n == 1) return 1;\n    return Recur(n - 1);\n}\n
        func function() int {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0\n}\n\n/* \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) */\nfunc loop(n int) {\n    for i := 0; i < n; i++ {\n        function()\n    }\n}\n\n/* \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) */\nfunc recur(n int) {\n    if n == 1 {\n        return\n    }\n    recur(n - 1)\n}\n
        @discardableResult\nfunc function() -> Int {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0\n}\n\n/* \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) */\nfunc loop(n: Int) {\n    for _ in 0 ..< n {\n        function()\n    }\n}\n\n/* \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) */\nfunc recur(n: Int) {\n    if n == 1 {\n        return\n    }\n    recur(n: n - 1)\n}\n
        function constFunc() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n/* \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) */\nfunction loop(n) {\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n/* \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) */\nfunction recur(n) {\n    if (n === 1) return;\n    return recur(n - 1);\n}\n
        function constFunc(): number {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n/* \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) */\nfunction loop(n: number): void {\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n/* \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) */\nfunction recur(n: number): void {\n    if (n === 1) return;\n    return recur(n - 1);\n}\n
        int function() {\n  // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n  return 0;\n}\n/* \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) */\nvoid loop(int n) {\n  for (int i = 0; i < n; i++) {\n    function();\n  }\n}\n/* \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) */\nvoid recur(int n) {\n  if (n == 1) return;\n  recur(n - 1);\n}\n
        fn function() -> i32 {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n/* \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) */\nfn loop(n: i32) {\n    for i in 0..n {\n        function();\n    }\n}\n/* \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) */\nfn recur(n: i32) {\n    if n == 1 {\n        return;\n    }\n    recur(n - 1);\n}\n
        int func() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n/* \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) */\nvoid loop(int n) {\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n/* \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) */\nvoid recur(int n) {\n    if (n == 1) return;\n    recur(n - 1);\n}\n
        fun function(): Int {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0\n}\n/* \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) */\nfun loop(n: Int) {\n    for (i in 0..<n) {\n        function()\n    }\n}\n/* \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) */\nfun recur(n: Int) {\n    if (n == 1) return\n    return recur(n - 1)\n}\n
        def function\n    # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    0\nend\n\n### \u5faa\u73af\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) ###\ndef loop(n)\n    (0...n).each { function }\nend\n\n### \u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n) ###\ndef recur(n)\n    return if n == 1\n    recur(n - 1)\nend\n

        \u51fd\u6570 loop() \u548c recur() \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n)\\) \uff0c\u4f46\u7a7a\u95f4\u590d\u6742\u5ea6\u4e0d\u540c\u3002

        • \u51fd\u6570 loop() \u5728\u5faa\u73af\u4e2d\u8c03\u7528\u4e86 \\(n\\) \u6b21 function() \uff0c\u6bcf\u8f6e\u4e2d\u7684 function() \u90fd\u8fd4\u56de\u5e76\u91ca\u653e\u4e86\u6808\u5e27\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \u3002
        • \u9012\u5f52\u51fd\u6570 recur() \u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u4f1a\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684 recur() \uff0c\u4ece\u800c\u5360\u7528 \\(O(n)\\) \u7684\u6808\u5e27\u7a7a\u95f4\u3002
        "},{"location":"chapter_computational_complexity/space_complexity/#243","title":"2.4.3 \u00a0 \u5e38\u89c1\u7c7b\u578b","text":"

        \u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u56fe 2-16 \u5c55\u793a\u4e86\u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\uff08\u4ece\u4f4e\u5230\u9ad8\u6392\u5217\uff09\u3002

        \\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n^2) < O(2^n) \\newline \\text{\u5e38\u6570\u9636} < \\text{\u5bf9\u6570\u9636} < \\text{\u7ebf\u6027\u9636} < \\text{\u5e73\u65b9\u9636} < \\text{\u6307\u6570\u9636} \\end{aligned} \\]

        \u56fe 2-16 \u00a0 \u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b

        "},{"location":"chapter_computational_complexity/space_complexity/#1-o1","title":"1. \u00a0 \u5e38\u6570\u9636 \\(O(1)\\)","text":"

        \u5e38\u6570\u9636\u5e38\u89c1\u4e8e\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\u7684\u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u3002

        \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u5faa\u73af\u4e2d\u521d\u59cb\u5316\u53d8\u91cf\u6216\u8c03\u7528\u51fd\u6570\u800c\u5360\u7528\u7684\u5185\u5b58\uff0c\u5728\u8fdb\u5165\u4e0b\u4e00\u5faa\u73af\u540e\u5c31\u4f1a\u88ab\u91ca\u653e\uff0c\u56e0\u6b64\u4e0d\u4f1a\u7d2f\u79ef\u5360\u7528\u7a7a\u95f4\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def function() -> int:\n    \"\"\"\u51fd\u6570\"\"\"\n    # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0\n\ndef constant(n: int):\n    \"\"\"\u5e38\u6570\u9636\"\"\"\n    # \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n    a = 0\n    nums = [0] * 10000\n    node = ListNode(0)\n    # \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n    for _ in range(n):\n        c = 0\n    # \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n    for _ in range(n):\n        function()\n
        space_complexity.cpp
        /* \u51fd\u6570 */\nint func() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n    // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n    const int a = 0;\n    int b = 0;\n    vector<int> nums(10000);\n    ListNode node(0);\n    // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n
        space_complexity.java
        /* \u51fd\u6570 */\nint function() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n    // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n    final int a = 0;\n    int b = 0;\n    int[] nums = new int[10000];\n    ListNode node = new ListNode(0);\n    // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n    for (int i = 0; i < n; i++) {\n        function();\n    }\n}\n
        space_complexity.cs
        /* \u51fd\u6570 */\nint Function() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid Constant(int n) {\n    // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n    int a = 0;\n    int b = 0;\n    int[] nums = new int[10000];\n    ListNode node = new(0);\n    // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n    for (int i = 0; i < n; i++) {\n        Function();\n    }\n}\n
        space_complexity.go
        /* \u51fd\u6570 */\nfunc function() int {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n    return 0\n}\n\n/* \u5e38\u6570\u9636 */\nfunc spaceConstant(n int) {\n    // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n    const a = 0\n    b := 0\n    nums := make([]int, 10000)\n    node := newNode(0)\n    // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n    var c int\n    for i := 0; i < n; i++ {\n        c = 0\n    }\n    // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n    for i := 0; i < n; i++ {\n        function()\n    }\n    b += 0\n    c += 0\n    nums[0] = 0\n    node.val = 0\n}\n
        space_complexity.swift
        /* \u51fd\u6570 */\n@discardableResult\nfunc function() -> Int {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0\n}\n\n/* \u5e38\u6570\u9636 */\nfunc constant(n: Int) {\n    // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n    let a = 0\n    var b = 0\n    let nums = Array(repeating: 0, count: 10000)\n    let node = ListNode(x: 0)\n    // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n    for _ in 0 ..< n {\n        let c = 0\n    }\n    // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n    for _ in 0 ..< n {\n        function()\n    }\n}\n
        space_complexity.js
        /* \u51fd\u6570 */\nfunction constFunc() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nfunction constant(n) {\n    // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n    const a = 0;\n    const b = 0;\n    const nums = new Array(10000);\n    const node = new ListNode(0);\n    // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n    for (let i = 0; i < n; i++) {\n        const c = 0;\n    }\n    // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n
        space_complexity.ts
        /* \u51fd\u6570 */\nfunction constFunc(): number {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nfunction constant(n: number): void {\n    // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n    const a = 0;\n    const b = 0;\n    const nums = new Array(10000);\n    const node = new ListNode(0);\n    // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n    for (let i = 0; i < n; i++) {\n        const c = 0;\n    }\n    // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n
        space_complexity.dart
        /* \u51fd\u6570 */\nint function() {\n  // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n  return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n  // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n  final int a = 0;\n  int b = 0;\n  List<int> nums = List.filled(10000, 0);\n  ListNode node = ListNode(0);\n  // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n  for (var i = 0; i < n; i++) {\n    int c = 0;\n  }\n  // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n  for (var i = 0; i < n; i++) {\n    function();\n  }\n}\n
        space_complexity.rs
        /* \u51fd\u6570 */\nfn function() -> i32 {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n\n/* \u5e38\u6570\u9636 */\n#[allow(unused)]\nfn constant(n: i32) {\n    // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n    const A: i32 = 0;\n    let b = 0;\n    let nums = vec![0; 10000];\n    let node = ListNode::new(0);\n    // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n    for i in 0..n {\n        let c = 0;\n    }\n    // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n    for i in 0..n {\n        function();\n    }\n}\n
        space_complexity.c
        /* \u51fd\u6570 */\nint func() {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n    // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n    const int a = 0;\n    int b = 0;\n    int nums[1000];\n    ListNode *node = newListNode(0);\n    free(node);\n    // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n
        space_complexity.kt
        /* \u51fd\u6570 */\nfun function(): Int {\n    // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    return 0\n}\n\n/* \u5e38\u6570\u9636 */\nfun constant(n: Int) {\n    // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n    val a = 0\n    var b = 0\n    val nums = Array(10000) { 0 }\n    val node = ListNode(0)\n    // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n    for (i in 0..<n) {\n        val c = 0\n    }\n    // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n    for (i in 0..<n) {\n        function()\n    }\n}\n
        space_complexity.rb
        ### \u51fd\u6570 ###\ndef function\n  # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n  0\nend\n\n### \u5e38\u6570\u9636 ###\ndef constant(n)\n  # \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n  a = 0\n  nums = [0] * 10000\n  node = ListNode.new\n\n  # \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n  (0...n).each { c = 0 }\n  # \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n  (0...n).each { function }\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_computational_complexity/space_complexity/#2-on","title":"2. \u00a0 \u7ebf\u6027\u9636 \\(O(n)\\)","text":"

        \u7ebf\u6027\u9636\u5e38\u89c1\u4e8e\u5143\u7d20\u6570\u91cf\u4e0e \\(n\\) \u6210\u6b63\u6bd4\u7684\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u7b49\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def linear(n: int):\n    \"\"\"\u7ebf\u6027\u9636\"\"\"\n    # \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    nums = [0] * n\n    # \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    hmap = dict[int, str]()\n    for i in range(n):\n        hmap[i] = str(i)\n
        space_complexity.cpp
        /* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n    // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n    vector<int> nums(n);\n    // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    vector<ListNode> nodes;\n    for (int i = 0; i < n; i++) {\n        nodes.push_back(ListNode(i));\n    }\n    // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    unordered_map<int, string> map;\n    for (int i = 0; i < n; i++) {\n        map[i] = to_string(i);\n    }\n}\n
        space_complexity.java
        /* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n    // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n    int[] nums = new int[n];\n    // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    List<ListNode> nodes = new ArrayList<>();\n    for (int i = 0; i < n; i++) {\n        nodes.add(new ListNode(i));\n    }\n    // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    Map<Integer, String> map = new HashMap<>();\n    for (int i = 0; i < n; i++) {\n        map.put(i, String.valueOf(i));\n    }\n}\n
        space_complexity.cs
        /* \u7ebf\u6027\u9636 */\nvoid Linear(int n) {\n    // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n    int[] nums = new int[n];\n    // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    List<ListNode> nodes = [];\n    for (int i = 0; i < n; i++) {\n        nodes.Add(new ListNode(i));\n    }\n    // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    Dictionary<int, string> map = [];\n    for (int i = 0; i < n; i++) {\n        map.Add(i, i.ToString());\n    }\n}\n
        space_complexity.go
        /* \u7ebf\u6027\u9636 */\nfunc spaceLinear(n int) {\n    // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n    _ = make([]int, n)\n    // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    var nodes []*node\n    for i := 0; i < n; i++ {\n        nodes = append(nodes, newNode(i))\n    }\n    // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    m := make(map[int]string, n)\n    for i := 0; i < n; i++ {\n        m[i] = strconv.Itoa(i)\n    }\n}\n
        space_complexity.swift
        /* \u7ebf\u6027\u9636 */\nfunc linear(n: Int) {\n    // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n    let nums = Array(repeating: 0, count: n)\n    // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    let nodes = (0 ..< n).map { ListNode(x: $0) }\n    // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    let map = Dictionary(uniqueKeysWithValues: (0 ..< n).map { ($0, \"\\($0)\") })\n}\n
        space_complexity.js
        /* \u7ebf\u6027\u9636 */\nfunction linear(n) {\n    // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n    const nums = new Array(n);\n    // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    const nodes = [];\n    for (let i = 0; i < n; i++) {\n        nodes.push(new ListNode(i));\n    }\n    // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    const map = new Map();\n    for (let i = 0; i < n; i++) {\n        map.set(i, i.toString());\n    }\n}\n
        space_complexity.ts
        /* \u7ebf\u6027\u9636 */\nfunction linear(n: number): void {\n    // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n    const nums = new Array(n);\n    // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    const nodes: ListNode[] = [];\n    for (let i = 0; i < n; i++) {\n        nodes.push(new ListNode(i));\n    }\n    // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    const map = new Map();\n    for (let i = 0; i < n; i++) {\n        map.set(i, i.toString());\n    }\n}\n
        space_complexity.dart
        /* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n  // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n  List<int> nums = List.filled(n, 0);\n  // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n  List<ListNode> nodes = [];\n  for (var i = 0; i < n; i++) {\n    nodes.add(ListNode(i));\n  }\n  // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n  Map<int, String> map = HashMap();\n  for (var i = 0; i < n; i++) {\n    map.putIfAbsent(i, () => i.toString());\n  }\n}\n
        space_complexity.rs
        /* \u7ebf\u6027\u9636 */\n#[allow(unused)]\nfn linear(n: i32) {\n    // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n    let mut nums = vec![0; n as usize];\n    // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    let mut nodes = Vec::new();\n    for i in 0..n {\n        nodes.push(ListNode::new(i))\n    }\n    // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    let mut map = HashMap::new();\n    for i in 0..n {\n        map.insert(i, i.to_string());\n    }\n}\n
        space_complexity.c
        /* \u54c8\u5e0c\u8868 */\ntypedef struct {\n    int key;\n    int val;\n    UT_hash_handle hh; // \u57fa\u4e8e uthash.h \u5b9e\u73b0\n} HashTable;\n\n/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n    // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n    int *nums = malloc(sizeof(int) * n);\n    free(nums);\n\n    // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    ListNode **nodes = malloc(sizeof(ListNode *) * n);\n    for (int i = 0; i < n; i++) {\n        nodes[i] = newListNode(i);\n    }\n    // \u5185\u5b58\u91ca\u653e\n    for (int i = 0; i < n; i++) {\n        free(nodes[i]);\n    }\n    free(nodes);\n\n    // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    HashTable *h = NULL;\n    for (int i = 0; i < n; i++) {\n        HashTable *tmp = malloc(sizeof(HashTable));\n        tmp->key = i;\n        tmp->val = i;\n        HASH_ADD_INT(h, key, tmp);\n    }\n\n    // \u5185\u5b58\u91ca\u653e\n    HashTable *curr, *tmp;\n    HASH_ITER(hh, h, curr, tmp) {\n        HASH_DEL(h, curr);\n        free(curr);\n    }\n}\n
        space_complexity.kt
        /* \u7ebf\u6027\u9636 */\nfun linear(n: Int) {\n    // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n    val nums = Array(n) { 0 }\n    // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    val nodes = mutableListOf<ListNode>()\n    for (i in 0..<n) {\n        nodes.add(ListNode(i))\n    }\n    // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n    val map = mutableMapOf<Int, String>()\n    for (i in 0..<n) {\n        map[i] = i.toString()\n    }\n}\n
        space_complexity.rb
        ### \u7ebf\u6027\u9636 ###\ndef linear(n)\n  # \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n  nums = Array.new(n, 0)\n\n  # \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n  hmap = {}\n  for i in 0...n\n    hmap[i] = i.to_s\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5982\u56fe 2-17 \u6240\u793a\uff0c\u6b64\u51fd\u6570\u7684\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u5373\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684 linear_recur() \u51fd\u6570\uff0c\u4f7f\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def linear_recur(n: int):\n    \"\"\"\u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n    print(\"\u9012\u5f52 n =\", n)\n    if n == 1:\n        return\n    linear_recur(n - 1)\n
        space_complexity.cpp
        /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n    cout << \"\u9012\u5f52 n = \" << n << endl;\n    if (n == 1)\n        return;\n    linearRecur(n - 1);\n}\n
        space_complexity.java
        /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n    System.out.println(\"\u9012\u5f52 n = \" + n);\n    if (n == 1)\n        return;\n    linearRecur(n - 1);\n}\n
        space_complexity.cs
        /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid LinearRecur(int n) {\n    Console.WriteLine(\"\u9012\u5f52 n = \" + n);\n    if (n == 1) return;\n    LinearRecur(n - 1);\n}\n
        space_complexity.go
        /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc spaceLinearRecur(n int) {\n    fmt.Println(\"\u9012\u5f52 n =\", n)\n    if n == 1 {\n        return\n    }\n    spaceLinearRecur(n - 1)\n}\n
        space_complexity.swift
        /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc linearRecur(n: Int) {\n    print(\"\u9012\u5f52 n = \\(n)\")\n    if n == 1 {\n        return\n    }\n    linearRecur(n: n - 1)\n}\n
        space_complexity.js
        /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction linearRecur(n) {\n    console.log(`\u9012\u5f52 n = ${n}`);\n    if (n === 1) return;\n    linearRecur(n - 1);\n}\n
        space_complexity.ts
        /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction linearRecur(n: number): void {\n    console.log(`\u9012\u5f52 n = ${n}`);\n    if (n === 1) return;\n    linearRecur(n - 1);\n}\n
        space_complexity.dart
        /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n  print('\u9012\u5f52 n = $n');\n  if (n == 1) return;\n  linearRecur(n - 1);\n}\n
        space_complexity.rs
        /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn linear_recur(n: i32) {\n    println!(\"\u9012\u5f52 n = {}\", n);\n    if n == 1 {\n        return;\n    };\n    linear_recur(n - 1);\n}\n
        space_complexity.c
        /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n    printf(\"\u9012\u5f52 n = %d\\r\\n\", n);\n    if (n == 1)\n        return;\n    linearRecur(n - 1);\n}\n
        space_complexity.kt
        /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfun linearRecur(n: Int) {\n    println(\"\u9012\u5f52 n = $n\")\n    if (n == 1)\n        return\n    linearRecur(n - 1)\n}\n
        space_complexity.rb
        ### \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09###\ndef linear_recur(n)\n  puts \"\u9012\u5f52 n = #{n}\"\n  return if n == 1\n  linear_recur(n - 1)\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 2-17 \u00a0 \u9012\u5f52\u51fd\u6570\u4ea7\u751f\u7684\u7ebf\u6027\u9636\u7a7a\u95f4\u590d\u6742\u5ea6

        "},{"location":"chapter_computational_complexity/space_complexity/#3-on2","title":"3. \u00a0 \u5e73\u65b9\u9636 \\(O(n^2)\\)","text":"

        \u5e73\u65b9\u9636\u5e38\u89c1\u4e8e\u77e9\u9635\u548c\u56fe\uff0c\u5143\u7d20\u6570\u91cf\u4e0e \\(n\\) \u6210\u5e73\u65b9\u5173\u7cfb\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def quadratic(n: int):\n    \"\"\"\u5e73\u65b9\u9636\"\"\"\n    # \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n    num_matrix = [[0] * n for _ in range(n)]\n
        space_complexity.cpp
        /* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n    // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n    vector<vector<int>> numMatrix;\n    for (int i = 0; i < n; i++) {\n        vector<int> tmp;\n        for (int j = 0; j < n; j++) {\n            tmp.push_back(0);\n        }\n        numMatrix.push_back(tmp);\n    }\n}\n
        space_complexity.java
        /* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n    // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n    int[][] numMatrix = new int[n][n];\n    // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n    List<List<Integer>> numList = new ArrayList<>();\n    for (int i = 0; i < n; i++) {\n        List<Integer> tmp = new ArrayList<>();\n        for (int j = 0; j < n; j++) {\n            tmp.add(0);\n        }\n        numList.add(tmp);\n    }\n}\n
        space_complexity.cs
        /* \u5e73\u65b9\u9636 */\nvoid Quadratic(int n) {\n    // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n    int[,] numMatrix = new int[n, n];\n    // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n    List<List<int>> numList = [];\n    for (int i = 0; i < n; i++) {\n        List<int> tmp = [];\n        for (int j = 0; j < n; j++) {\n            tmp.Add(0);\n        }\n        numList.Add(tmp);\n    }\n}\n
        space_complexity.go
        /* \u5e73\u65b9\u9636 */\nfunc spaceQuadratic(n int) {\n    // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n    numMatrix := make([][]int, n)\n    for i := 0; i < n; i++ {\n        numMatrix[i] = make([]int, n)\n    }\n}\n
        space_complexity.swift
        /* \u5e73\u65b9\u9636 */\nfunc quadratic(n: Int) {\n    // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n    let numList = Array(repeating: Array(repeating: 0, count: n), count: n)\n}\n
        space_complexity.js
        /* \u5e73\u65b9\u9636 */\nfunction quadratic(n) {\n    // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n    const numMatrix = Array(n)\n        .fill(null)\n        .map(() => Array(n).fill(null));\n    // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n    const numList = [];\n    for (let i = 0; i < n; i++) {\n        const tmp = [];\n        for (let j = 0; j < n; j++) {\n            tmp.push(0);\n        }\n        numList.push(tmp);\n    }\n}\n
        space_complexity.ts
        /* \u5e73\u65b9\u9636 */\nfunction quadratic(n: number): void {\n    // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n    const numMatrix = Array(n)\n        .fill(null)\n        .map(() => Array(n).fill(null));\n    // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n    const numList = [];\n    for (let i = 0; i < n; i++) {\n        const tmp = [];\n        for (let j = 0; j < n; j++) {\n            tmp.push(0);\n        }\n        numList.push(tmp);\n    }\n}\n
        space_complexity.dart
        /* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n  // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n  List<List<int>> numMatrix = List.generate(n, (_) => List.filled(n, 0));\n  // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n  List<List<int>> numList = [];\n  for (var i = 0; i < n; i++) {\n    List<int> tmp = [];\n    for (int j = 0; j < n; j++) {\n      tmp.add(0);\n    }\n    numList.add(tmp);\n  }\n}\n
        space_complexity.rs
        /* \u5e73\u65b9\u9636 */\n#[allow(unused)]\nfn quadratic(n: i32) {\n    // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n    let num_matrix = vec![vec![0; n as usize]; n as usize];\n    // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n    let mut num_list = Vec::new();\n    for i in 0..n {\n        let mut tmp = Vec::new();\n        for j in 0..n {\n            tmp.push(0);\n        }\n        num_list.push(tmp);\n    }\n}\n
        space_complexity.c
        /* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n    // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n    int **numMatrix = malloc(sizeof(int *) * n);\n    for (int i = 0; i < n; i++) {\n        int *tmp = malloc(sizeof(int) * n);\n        for (int j = 0; j < n; j++) {\n            tmp[j] = 0;\n        }\n        numMatrix[i] = tmp;\n    }\n\n    // \u5185\u5b58\u91ca\u653e\n    for (int i = 0; i < n; i++) {\n        free(numMatrix[i]);\n    }\n    free(numMatrix);\n}\n
        space_complexity.kt
        /* \u5e73\u65b9\u9636 */\nfun quadratic(n: Int) {\n    // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n    val numMatrix = arrayOfNulls<Array<Int>?>(n)\n    // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n    val numList = mutableListOf<MutableList<Int>>()\n    for (i in 0..<n) {\n        val tmp = mutableListOf<Int>()\n        for (j in 0..<n) {\n            tmp.add(0)\n        }\n        numList.add(tmp)\n    }\n}\n
        space_complexity.rb
        ### \u5e73\u65b9\u9636 ###\ndef quadratic(n)\n  # \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n  Array.new(n) { Array.new(n, 0) }\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5982\u56fe 2-18 \u6240\u793a\uff0c\u8be5\u51fd\u6570\u7684\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u5728\u6bcf\u4e2a\u9012\u5f52\u51fd\u6570\u4e2d\u90fd\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u6570\u7ec4\uff0c\u957f\u5ea6\u5206\u522b\u4e3a \\(n\\)\u3001\\(n-1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \uff0c\u5e73\u5747\u957f\u5ea6\u4e3a \\(n / 2\\) \uff0c\u56e0\u6b64\u603b\u4f53\u5360\u7528 \\(O(n^2)\\) \u7a7a\u95f4\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def quadratic_recur(n: int) -> int:\n    \"\"\"\u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n    if n <= 0:\n        return 0\n    # \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n    nums = [0] * n\n    return quadratic_recur(n - 1)\n
        space_complexity.cpp
        /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n    if (n <= 0)\n        return 0;\n    vector<int> nums(n);\n    cout << \"\u9012\u5f52 n = \" << n << \" \u4e2d\u7684 nums \u957f\u5ea6 = \" << nums.size() << endl;\n    return quadraticRecur(n - 1);\n}\n
        space_complexity.java
        /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n    if (n <= 0)\n        return 0;\n    // \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n    int[] nums = new int[n];\n    System.out.println(\"\u9012\u5f52 n = \" + n + \" \u4e2d\u7684 nums \u957f\u5ea6 = \" + nums.length);\n    return quadraticRecur(n - 1);\n}\n
        space_complexity.cs
        /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint QuadraticRecur(int n) {\n    if (n <= 0) return 0;\n    int[] nums = new int[n];\n    Console.WriteLine(\"\u9012\u5f52 n = \" + n + \" \u4e2d\u7684 nums \u957f\u5ea6 = \" + nums.Length);\n    return QuadraticRecur(n - 1);\n}\n
        space_complexity.go
        /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc spaceQuadraticRecur(n int) int {\n    if n <= 0 {\n        return 0\n    }\n    nums := make([]int, n)\n    fmt.Printf(\"\u9012\u5f52 n = %d \u4e2d\u7684 nums \u957f\u5ea6 = %d \\n\", n, len(nums))\n    return spaceQuadraticRecur(n - 1)\n}\n
        space_complexity.swift
        /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\n@discardableResult\nfunc quadraticRecur(n: Int) -> Int {\n    if n <= 0 {\n        return 0\n    }\n    // \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n    let nums = Array(repeating: 0, count: n)\n    print(\"\u9012\u5f52 n = \\(n) \u4e2d\u7684 nums \u957f\u5ea6 = \\(nums.count)\")\n    return quadraticRecur(n: n - 1)\n}\n
        space_complexity.js
        /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction quadraticRecur(n) {\n    if (n <= 0) return 0;\n    const nums = new Array(n);\n    console.log(`\u9012\u5f52 n = ${n} \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}`);\n    return quadraticRecur(n - 1);\n}\n
        space_complexity.ts
        /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction quadraticRecur(n: number): number {\n    if (n <= 0) return 0;\n    const nums = new Array(n);\n    console.log(`\u9012\u5f52 n = ${n} \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}`);\n    return quadraticRecur(n - 1);\n}\n
        space_complexity.dart
        /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n  if (n <= 0) return 0;\n  List<int> nums = List.filled(n, 0);\n  print('\u9012\u5f52 n = $n \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}');\n  return quadraticRecur(n - 1);\n}\n
        space_complexity.rs
        /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn quadratic_recur(n: i32) -> i32 {\n    if n <= 0 {\n        return 0;\n    };\n    // \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n    let nums = vec![0; n as usize];\n    println!(\"\u9012\u5f52 n = {} \u4e2d\u7684 nums \u957f\u5ea6 = {}\", n, nums.len());\n    return quadratic_recur(n - 1);\n}\n
        space_complexity.c
        /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n    if (n <= 0)\n        return 0;\n    int *nums = malloc(sizeof(int) * n);\n    printf(\"\u9012\u5f52 n = %d \u4e2d\u7684 nums \u957f\u5ea6 = %d\\r\\n\", n, n);\n    int res = quadraticRecur(n - 1);\n    free(nums);\n    return res;\n}\n
        space_complexity.kt
        /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\ntailrec fun quadraticRecur(n: Int): Int {\n    if (n <= 0)\n        return 0\n    // \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n    val nums = Array(n) { 0 }\n    println(\"\u9012\u5f52 n = $n \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.size}\")\n    return quadraticRecur(n - 1)\n}\n
        space_complexity.rb
        ### \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09###\ndef quadratic_recur(n)\n  return 0 unless n > 0\n\n  # \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n  nums = Array.new(n, 0)\n  quadratic_recur(n - 1)\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 2-18 \u00a0 \u9012\u5f52\u51fd\u6570\u4ea7\u751f\u7684\u5e73\u65b9\u9636\u7a7a\u95f4\u590d\u6742\u5ea6

        "},{"location":"chapter_computational_complexity/space_complexity/#4-o2n","title":"4. \u00a0 \u6307\u6570\u9636 \\(O(2^n)\\)","text":"

        \u6307\u6570\u9636\u5e38\u89c1\u4e8e\u4e8c\u53c9\u6811\u3002\u89c2\u5bdf\u56fe 2-19 \uff0c\u5c42\u6570\u4e3a \\(n\\) \u7684\u201c\u6ee1\u4e8c\u53c9\u6811\u201d\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(2^n - 1\\) \uff0c\u5360\u7528 \\(O(2^n)\\) \u7a7a\u95f4\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
        def build_tree(n: int) -> TreeNode | None:\n    \"\"\"\u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09\"\"\"\n    if n == 0:\n        return None\n    root = TreeNode(0)\n    root.left = build_tree(n - 1)\n    root.right = build_tree(n - 1)\n    return root\n
        space_complexity.cpp
        /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode *buildTree(int n) {\n    if (n == 0)\n        return nullptr;\n    TreeNode *root = new TreeNode(0);\n    root->left = buildTree(n - 1);\n    root->right = buildTree(n - 1);\n    return root;\n}\n
        space_complexity.java
        /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode buildTree(int n) {\n    if (n == 0)\n        return null;\n    TreeNode root = new TreeNode(0);\n    root.left = buildTree(n - 1);\n    root.right = buildTree(n - 1);\n    return root;\n}\n
        space_complexity.cs
        /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode? BuildTree(int n) {\n    if (n == 0) return null;\n    TreeNode root = new(0) {\n        left = BuildTree(n - 1),\n        right = BuildTree(n - 1)\n    };\n    return root;\n}\n
        space_complexity.go
        /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunc buildTree(n int) *TreeNode {\n    if n == 0 {\n        return nil\n    }\n    root := NewTreeNode(0)\n    root.Left = buildTree(n - 1)\n    root.Right = buildTree(n - 1)\n    return root\n}\n
        space_complexity.swift
        /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunc buildTree(n: Int) -> TreeNode? {\n    if n == 0 {\n        return nil\n    }\n    let root = TreeNode(x: 0)\n    root.left = buildTree(n: n - 1)\n    root.right = buildTree(n: n - 1)\n    return root\n}\n
        space_complexity.js
        /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunction buildTree(n) {\n    if (n === 0) return null;\n    const root = new TreeNode(0);\n    root.left = buildTree(n - 1);\n    root.right = buildTree(n - 1);\n    return root;\n}\n
        space_complexity.ts
        /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunction buildTree(n: number): TreeNode | null {\n    if (n === 0) return null;\n    const root = new TreeNode(0);\n    root.left = buildTree(n - 1);\n    root.right = buildTree(n - 1);\n    return root;\n}\n
        space_complexity.dart
        /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode? buildTree(int n) {\n  if (n == 0) return null;\n  TreeNode root = TreeNode(0);\n  root.left = buildTree(n - 1);\n  root.right = buildTree(n - 1);\n  return root;\n}\n
        space_complexity.rs
        /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfn build_tree(n: i32) -> Option<Rc<RefCell<TreeNode>>> {\n    if n == 0 {\n        return None;\n    };\n    let root = TreeNode::new(0);\n    root.borrow_mut().left = build_tree(n - 1);\n    root.borrow_mut().right = build_tree(n - 1);\n    return Some(root);\n}\n
        space_complexity.c
        /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode *buildTree(int n) {\n    if (n == 0)\n        return NULL;\n    TreeNode *root = newTreeNode(0);\n    root->left = buildTree(n - 1);\n    root->right = buildTree(n - 1);\n    return root;\n}\n
        space_complexity.kt
        /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfun buildTree(n: Int): TreeNode? {\n    if (n == 0)\n        return null\n    val root = TreeNode(0)\n    root.left = buildTree(n - 1)\n    root.right = buildTree(n - 1)\n    return root\n}\n
        space_complexity.rb
        ### \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09###\ndef build_tree(n)\n  return if n == 0\n\n  TreeNode.new.tap do |root|\n    root.left = build_tree(n - 1)\n    root.right = build_tree(n - 1)\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 2-19 \u00a0 \u6ee1\u4e8c\u53c9\u6811\u4ea7\u751f\u7684\u6307\u6570\u9636\u7a7a\u95f4\u590d\u6742\u5ea6

        "},{"location":"chapter_computational_complexity/space_complexity/#5-olog-n","title":"5. \u00a0 \u5bf9\u6570\u9636 \\(O(\\log n)\\)","text":"

        \u5bf9\u6570\u9636\u5e38\u89c1\u4e8e\u5206\u6cbb\u7b97\u6cd5\u3002\u4f8b\u5982\u5f52\u5e76\u6392\u5e8f\uff0c\u8f93\u5165\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\uff0c\u6bcf\u8f6e\u9012\u5f52\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5904\u5212\u5206\u4e3a\u4e24\u534a\uff0c\u5f62\u6210\u9ad8\u5ea6\u4e3a \\(\\log n\\) \u7684\u9012\u5f52\u6811\uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u6808\u5e27\u7a7a\u95f4\u3002

        \u518d\u4f8b\u5982\u5c06\u6570\u5b57\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\uff0c\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570 \\(n\\) \uff0c\u5b83\u7684\u4f4d\u6570\u4e3a \\(\\lfloor \\log_{10} n \\rfloor + 1\\) \uff0c\u5373\u5bf9\u5e94\u5b57\u7b26\u4e32\u957f\u5ea6\u4e3a \\(\\lfloor \\log_{10} n \\rfloor + 1\\) \uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log_{10} n + 1) = O(\\log n)\\) \u3002

        "},{"location":"chapter_computational_complexity/space_complexity/#244","title":"2.4.4 \u00a0 \u6743\u8861\u65f6\u95f4\u4e0e\u7a7a\u95f4","text":"

        \u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u90fd\u80fd\u8fbe\u5230\u6700\u4f18\u3002\u7136\u800c\u5728\u5b9e\u9645\u60c5\u51b5\u4e2d\uff0c\u540c\u65f6\u4f18\u5316\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u975e\u5e38\u56f0\u96be\u3002

        \u964d\u4f4e\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u9700\u8981\u4ee5\u63d0\u5347\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a\u4ee3\u4ef7\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u6211\u4eec\u5c06\u727a\u7272\u5185\u5b58\u7a7a\u95f4\u6765\u63d0\u5347\u7b97\u6cd5\u8fd0\u884c\u901f\u5ea6\u7684\u601d\u8def\u79f0\u4e3a\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\uff1b\u53cd\u4e4b\uff0c\u5219\u79f0\u4e3a\u201c\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4\u201d\u3002

        \u9009\u62e9\u54ea\u79cd\u601d\u8def\u53d6\u51b3\u4e8e\u6211\u4eec\u66f4\u770b\u91cd\u54ea\u4e2a\u65b9\u9762\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u65f6\u95f4\u6bd4\u7a7a\u95f4\u66f4\u5b9d\u8d35\uff0c\u56e0\u6b64\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\u901a\u5e38\u662f\u66f4\u5e38\u7528\u7684\u7b56\u7565\u3002\u5f53\u7136\uff0c\u5728\u6570\u636e\u91cf\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u63a7\u5236\u7a7a\u95f4\u590d\u6742\u5ea6\u4e5f\u975e\u5e38\u91cd\u8981\u3002

        "},{"location":"chapter_computational_complexity/summary/","title":"2.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_computational_complexity/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"

        \u7b97\u6cd5\u6548\u7387\u8bc4\u4f30

        • \u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u662f\u8861\u91cf\u7b97\u6cd5\u4f18\u52a3\u7684\u4e24\u4e2a\u4e3b\u8981\u8bc4\u4ef7\u6307\u6807\u3002
        • \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b9e\u9645\u6d4b\u8bd5\u6765\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\uff0c\u4f46\u96be\u4ee5\u6d88\u9664\u6d4b\u8bd5\u73af\u5883\u7684\u5f71\u54cd\uff0c\u4e14\u4f1a\u8017\u8d39\u5927\u91cf\u8ba1\u7b97\u8d44\u6e90\u3002
        • \u590d\u6742\u5ea6\u5206\u6790\u53ef\u4ee5\u6d88\u9664\u5b9e\u9645\u6d4b\u8bd5\u7684\u5f0a\u7aef\uff0c\u5206\u6790\u7ed3\u679c\u9002\u7528\u4e8e\u6240\u6709\u8fd0\u884c\u5e73\u53f0\uff0c\u5e76\u4e14\u80fd\u591f\u63ed\u793a\u7b97\u6cd5\u5728\u4e0d\u540c\u6570\u636e\u89c4\u6a21\u4e0b\u7684\u6548\u7387\u3002

        \u65f6\u95f4\u590d\u6742\u5ea6

        • \u65f6\u95f4\u590d\u6742\u5ea6\u7528\u4e8e\u8861\u91cf\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u968f\u6570\u636e\u91cf\u589e\u957f\u7684\u8d8b\u52bf\uff0c\u53ef\u4ee5\u6709\u6548\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u5931\u6548\uff0c\u5982\u5728\u8f93\u5165\u7684\u6570\u636e\u91cf\u8f83\u5c0f\u6216\u65f6\u95f4\u590d\u6742\u5ea6\u76f8\u540c\u65f6\uff0c\u65e0\u6cd5\u7cbe\u786e\u5bf9\u6bd4\u7b97\u6cd5\u6548\u7387\u7684\u4f18\u52a3\u3002
        • \u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4f7f\u7528\u5927 \\(O\\) \u7b26\u53f7\u8868\u793a\uff0c\u5bf9\u5e94\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c\uff0c\u53cd\u6620\u5f53 \\(n\\) \u8d8b\u5411\u6b63\u65e0\u7a77\u65f6\uff0c\u64cd\u4f5c\u6570\u91cf \\(T(n)\\) \u7684\u589e\u957f\u7ea7\u522b\u3002
        • \u63a8\u7b97\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u4e3a\u4e24\u6b65\uff0c\u9996\u5148\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf\uff0c\u7136\u540e\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c\u3002
        • \u5e38\u89c1\u65f6\u95f4\u590d\u6742\u5ea6\u4ece\u4f4e\u5230\u9ad8\u6392\u5217\u6709 \\(O(1)\\)\u3001\\(O(\\log n)\\)\u3001\\(O(n)\\)\u3001\\(O(n \\log n)\\)\u3001\\(O(n^2)\\)\u3001\\(O(2^n)\\) \u548c \\(O(n!)\\) \u7b49\u3002
        • \u67d0\u4e9b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u975e\u56fa\u5b9a\uff0c\u800c\u662f\u4e0e\u8f93\u5165\u6570\u636e\u7684\u5206\u5e03\u6709\u5173\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u4e3a\u6700\u5dee\u3001\u6700\u4f73\u3001\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u51e0\u4e4e\u4e0d\u7528\uff0c\u56e0\u4e3a\u8f93\u5165\u6570\u636e\u4e00\u822c\u9700\u8981\u6ee1\u8db3\u4e25\u683c\u6761\u4ef6\u624d\u80fd\u8fbe\u5230\u6700\u4f73\u60c5\u51b5\u3002
        • \u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u53cd\u6620\u7b97\u6cd5\u5728\u968f\u673a\u6570\u636e\u8f93\u5165\u4e0b\u7684\u8fd0\u884c\u6548\u7387\uff0c\u6700\u63a5\u8fd1\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u7b97\u6cd5\u6027\u80fd\u3002\u8ba1\u7b97\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u9700\u8981\u7edf\u8ba1\u8f93\u5165\u6570\u636e\u5206\u5e03\u4ee5\u53ca\u7efc\u5408\u540e\u7684\u6570\u5b66\u671f\u671b\u3002

        \u7a7a\u95f4\u590d\u6742\u5ea6

        • \u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u4f5c\u7528\u7c7b\u4f3c\u4e8e\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u7528\u4e8e\u8861\u91cf\u7b97\u6cd5\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u968f\u6570\u636e\u91cf\u589e\u957f\u7684\u8d8b\u52bf\u3002
        • \u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u76f8\u5173\u5185\u5b58\u7a7a\u95f4\u53ef\u5206\u4e3a\u8f93\u5165\u7a7a\u95f4\u3001\u6682\u5b58\u7a7a\u95f4\u3001\u8f93\u51fa\u7a7a\u95f4\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u8f93\u5165\u7a7a\u95f4\u4e0d\u7eb3\u5165\u7a7a\u95f4\u590d\u6742\u5ea6\u8ba1\u7b97\u3002\u6682\u5b58\u7a7a\u95f4\u53ef\u5206\u4e3a\u6682\u5b58\u6570\u636e\u3001\u6808\u5e27\u7a7a\u95f4\u548c\u6307\u4ee4\u7a7a\u95f4\uff0c\u5176\u4e2d\u6808\u5e27\u7a7a\u95f4\u901a\u5e38\u4ec5\u5728\u9012\u5f52\u51fd\u6570\u4e2d\u5f71\u54cd\u7a7a\u95f4\u590d\u6742\u5ea6\u3002
        • \u6211\u4eec\u901a\u5e38\u53ea\u5173\u6ce8\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u5373\u7edf\u8ba1\u7b97\u6cd5\u5728\u6700\u5dee\u8f93\u5165\u6570\u636e\u548c\u6700\u5dee\u8fd0\u884c\u65f6\u523b\u4e0b\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u3002
        • \u5e38\u89c1\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece\u4f4e\u5230\u9ad8\u6392\u5217\u6709 \\(O(1)\\)\u3001\\(O(\\log n)\\)\u3001\\(O(n)\\)\u3001\\(O(n^2)\\) \u548c \\(O(2^n)\\) \u7b49\u3002
        "},{"location":"chapter_computational_complexity/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u5c3e\u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u5417\uff1f

        \u7406\u8bba\u4e0a\uff0c\u5c3e\u9012\u5f52\u51fd\u6570\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f18\u5316\u81f3 \\(O(1)\\) \u3002\u4e0d\u8fc7\u7edd\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\uff08\u4f8b\u5982 Java\u3001Python\u3001C++\u3001Go\u3001C# \u7b49\uff09\u4e0d\u652f\u6301\u81ea\u52a8\u4f18\u5316\u5c3e\u9012\u5f52\uff0c\u56e0\u6b64\u901a\u5e38\u8ba4\u4e3a\u7a7a\u95f4\u590d\u6742\u5ea6\u662f \\(O(n)\\) \u3002

        Q\uff1a\u51fd\u6570\u548c\u65b9\u6cd5\u8fd9\u4e24\u4e2a\u672f\u8bed\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f

        \u51fd\u6570\uff08function\uff09\u53ef\u4ee5\u88ab\u72ec\u7acb\u6267\u884c\uff0c\u6240\u6709\u53c2\u6570\u90fd\u4ee5\u663e\u5f0f\u4f20\u9012\u3002\u65b9\u6cd5\uff08method\uff09\u4e0e\u4e00\u4e2a\u5bf9\u8c61\u5173\u8054\uff0c\u88ab\u9690\u5f0f\u4f20\u9012\u7ed9\u8c03\u7528\u5b83\u7684\u5bf9\u8c61\uff0c\u80fd\u591f\u5bf9\u7c7b\u7684\u5b9e\u4f8b\u4e2d\u5305\u542b\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002

        \u4e0b\u9762\u4ee5\u51e0\u79cd\u5e38\u89c1\u7684\u7f16\u7a0b\u8bed\u8a00\u4e3a\u4f8b\u6765\u8bf4\u660e\u3002

        • C \u8bed\u8a00\u662f\u8fc7\u7a0b\u5f0f\u7f16\u7a0b\u8bed\u8a00\uff0c\u6ca1\u6709\u9762\u5411\u5bf9\u8c61\u7684\u6982\u5ff5\uff0c\u6240\u4ee5\u53ea\u6709\u51fd\u6570\u3002\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u7ed3\u6784\u4f53\uff08struct\uff09\u6765\u6a21\u62df\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff0c\u4e0e\u7ed3\u6784\u4f53\u76f8\u5173\u8054\u7684\u51fd\u6570\u5c31\u76f8\u5f53\u4e8e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u65b9\u6cd5\u3002
        • Java \u548c C# \u662f\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u4ee3\u7801\u5757\uff08\u65b9\u6cd5\uff09\u901a\u5e38\u4f5c\u4e3a\u67d0\u4e2a\u7c7b\u7684\u4e00\u90e8\u5206\u3002\u9759\u6001\u65b9\u6cd5\u7684\u884c\u4e3a\u7c7b\u4f3c\u4e8e\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u88ab\u7ed1\u5b9a\u5728\u7c7b\u4e0a\uff0c\u4e0d\u80fd\u8bbf\u95ee\u7279\u5b9a\u7684\u5b9e\u4f8b\u53d8\u91cf\u3002
        • C++ \u548c Python \u65e2\u652f\u6301\u8fc7\u7a0b\u5f0f\u7f16\u7a0b\uff08\u51fd\u6570\uff09\uff0c\u4e5f\u652f\u6301\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08\u65b9\u6cd5\uff09\u3002

        Q\uff1a\u56fe\u89e3\u201c\u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\u201d\u53cd\u6620\u7684\u662f\u5426\u662f\u5360\u7528\u7a7a\u95f4\u7684\u7edd\u5bf9\u5927\u5c0f\uff1f

        \u4e0d\u662f\uff0c\u8be5\u56fe\u5c55\u793a\u7684\u662f\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u5176\u53cd\u6620\u7684\u662f\u589e\u957f\u8d8b\u52bf\uff0c\u800c\u4e0d\u662f\u5360\u7528\u7a7a\u95f4\u7684\u7edd\u5bf9\u5927\u5c0f\u3002

        \u5047\u8bbe\u53d6 \\(n = 8\\) \uff0c\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\u6bcf\u6761\u66f2\u7ebf\u7684\u503c\u4e0e\u51fd\u6570\u5bf9\u5e94\u4e0d\u4e0a\u3002\u8fd9\u662f\u56e0\u4e3a\u6bcf\u6761\u66f2\u7ebf\u90fd\u5305\u542b\u4e00\u4e2a\u5e38\u6570\u9879\uff0c\u7528\u4e8e\u5c06\u53d6\u503c\u8303\u56f4\u538b\u7f29\u5230\u4e00\u4e2a\u89c6\u89c9\u8212\u9002\u7684\u8303\u56f4\u5185\u3002

        \u5728\u5b9e\u9645\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u901a\u5e38\u4e0d\u77e5\u9053\u6bcf\u4e2a\u65b9\u6cd5\u7684\u201c\u5e38\u6570\u9879\u201d\u590d\u6742\u5ea6\u662f\u591a\u5c11\uff0c\u6240\u4ee5\u4e00\u822c\u65e0\u6cd5\u4ec5\u51ed\u590d\u6742\u5ea6\u6765\u9009\u62e9 \\(n = 8\\) \u4e4b\u4e0b\u7684\u6700\u4f18\u89e3\u6cd5\u3002\u4f46\u5bf9\u4e8e \\(n = 8^5\\) \u5c31\u5f88\u597d\u9009\u4e86\uff0c\u8fd9\u65f6\u589e\u957f\u8d8b\u52bf\u5df2\u7ecf\u5360\u4e3b\u5bfc\u4e86\u3002

        Q \u662f\u5426\u5b58\u5728\u6839\u636e\u5b9e\u9645\u4f7f\u7528\u573a\u666f\uff0c\u9009\u62e9\u727a\u7272\u65f6\u95f4\uff08\u6216\u7a7a\u95f4\uff09\u6765\u8bbe\u8ba1\u7b97\u6cd5\u7684\u60c5\u51b5\uff1f

        \u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5927\u90e8\u5206\u60c5\u51b5\u4f1a\u9009\u62e9\u727a\u7272\u7a7a\u95f4\u6362\u65f6\u95f4\u3002\u4f8b\u5982\u6570\u636e\u5e93\u7d22\u5f15\uff0c\u6211\u4eec\u901a\u5e38\u9009\u62e9\u5efa\u7acb B+ \u6811\u6216\u54c8\u5e0c\u7d22\u5f15\uff0c\u5360\u7528\u5927\u91cf\u5185\u5b58\u7a7a\u95f4\uff0c\u4ee5\u6362\u53d6 \\(O(\\log n)\\) \u751a\u81f3 \\(O(1)\\) \u7684\u9ad8\u6548\u67e5\u8be2\u3002

        \u5728\u7a7a\u95f4\u8d44\u6e90\u5b9d\u8d35\u7684\u573a\u666f\uff0c\u4e5f\u4f1a\u9009\u62e9\u727a\u7272\u65f6\u95f4\u6362\u7a7a\u95f4\u3002\u4f8b\u5982\u5728\u5d4c\u5165\u5f0f\u5f00\u53d1\u4e2d\uff0c\u8bbe\u5907\u5185\u5b58\u5f88\u5b9d\u8d35\uff0c\u5de5\u7a0b\u5e08\u53ef\u80fd\u4f1a\u653e\u5f03\u4f7f\u7528\u54c8\u5e0c\u8868\uff0c\u9009\u62e9\u4f7f\u7528\u6570\u7ec4\u987a\u5e8f\u67e5\u627e\uff0c\u4ee5\u8282\u7701\u5185\u5b58\u5360\u7528\uff0c\u4ee3\u4ef7\u662f\u67e5\u627e\u53d8\u6162\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/","title":"2.3 \u00a0 \u65f6\u95f4\u590d\u6742\u5ea6","text":"

        \u8fd0\u884c\u65f6\u95f4\u53ef\u4ee5\u76f4\u89c2\u4e14\u51c6\u786e\u5730\u53cd\u6620\u7b97\u6cd5\u7684\u6548\u7387\u3002\u5982\u679c\u6211\u4eec\u60f3\u51c6\u786e\u9884\u4f30\u4e00\u6bb5\u4ee3\u7801\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u5e94\u8be5\u5982\u4f55\u64cd\u4f5c\u5462\uff1f

        1. \u786e\u5b9a\u8fd0\u884c\u5e73\u53f0\uff0c\u5305\u62ec\u786c\u4ef6\u914d\u7f6e\u3001\u7f16\u7a0b\u8bed\u8a00\u3001\u7cfb\u7edf\u73af\u5883\u7b49\uff0c\u8fd9\u4e9b\u56e0\u7d20\u90fd\u4f1a\u5f71\u54cd\u4ee3\u7801\u7684\u8fd0\u884c\u6548\u7387\u3002
        2. \u8bc4\u4f30\u5404\u79cd\u8ba1\u7b97\u64cd\u4f5c\u6240\u9700\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u4f8b\u5982\u52a0\u6cd5\u64cd\u4f5c + \u9700\u8981 1 ns \uff0c\u4e58\u6cd5\u64cd\u4f5c * \u9700\u8981 10 ns \uff0c\u6253\u5370\u64cd\u4f5c print() \u9700\u8981 5 ns \u7b49\u3002
        3. \u7edf\u8ba1\u4ee3\u7801\u4e2d\u6240\u6709\u7684\u8ba1\u7b97\u64cd\u4f5c\uff0c\u5e76\u5c06\u6240\u6709\u64cd\u4f5c\u7684\u6267\u884c\u65f6\u95f4\u6c42\u548c\uff0c\u4ece\u800c\u5f97\u5230\u8fd0\u884c\u65f6\u95f4\u3002

        \u4f8b\u5982\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        # \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\ndef algorithm(n: int):\n    a = 2      # 1 ns\n    a = a + 1  # 1 ns\n    a = a * 2  # 10 ns\n    # \u5faa\u73af n \u6b21\n    for _ in range(n):  # 1 ns\n        print(0)        # 5 ns\n
        // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // \u5faa\u73af n \u6b21\n    for (int i = 0; i < n; i++) {  // 1 ns\n        cout << 0 << endl;         // 5 ns\n    }\n}\n
        // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // \u5faa\u73af n \u6b21\n    for (int i = 0; i < n; i++) {  // 1 ns\n        System.out.println(0);     // 5 ns\n    }\n}\n
        // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid Algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // \u5faa\u73af n \u6b21\n    for (int i = 0; i < n; i++) {  // 1 ns\n        Console.WriteLine(0);      // 5 ns\n    }\n}\n
        // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunc algorithm(n int) {\n    a := 2     // 1 ns\n    a = a + 1  // 1 ns\n    a = a * 2  // 10 ns\n    // \u5faa\u73af n \u6b21\n    for i := 0; i < n; i++ {  // 1 ns\n        fmt.Println(a)        // 5 ns\n    }\n}\n
        // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunc algorithm(n: Int) {\n    var a = 2 // 1 ns\n    a = a + 1 // 1 ns\n    a = a * 2 // 10 ns\n    // \u5faa\u73af n \u6b21\n    for _ in 0 ..< n { // 1 ns\n        print(0) // 5 ns\n    }\n}\n
        // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunction algorithm(n) {\n    var a = 2; // 1 ns\n    a = a + 1; // 1 ns\n    a = a * 2; // 10 ns\n    // \u5faa\u73af n \u6b21\n    for(let i = 0; i < n; i++) { // 1 ns\n        console.log(0); // 5 ns\n    }\n}\n
        // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunction algorithm(n: number): void {\n    var a: number = 2; // 1 ns\n    a = a + 1; // 1 ns\n    a = a * 2; // 10 ns\n    // \u5faa\u73af n \u6b21\n    for(let i = 0; i < n; i++) { // 1 ns\n        console.log(0); // 5 ns\n    }\n}\n
        // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\n  int a = 2; // 1 ns\n  a = a + 1; // 1 ns\n  a = a * 2; // 10 ns\n  // \u5faa\u73af n \u6b21\n  for (int i = 0; i < n; i++) { // 1 ns\n    print(0); // 5 ns\n  }\n}\n
        // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfn algorithm(n: i32) {\n    let mut a = 2;      // 1 ns\n    a = a + 1;          // 1 ns\n    a = a * 2;          // 10 ns\n    // \u5faa\u73af n \u6b21\n    for _ in 0..n {     // 1 ns\n        println!(\"{}\", 0);  // 5 ns\n    }\n}\n
        // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\n    int a = 2;  // 1 ns\n    a = a + 1;  // 1 ns\n    a = a * 2;  // 10 ns\n    // \u5faa\u73af n \u6b21\n    for (int i = 0; i < n; i++) {   // 1 ns\n        printf(\"%d\", 0);            // 5 ns\n    }\n}\n
        // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfun algorithm(n: Int) {\n    var a = 2 // 1 ns\n    a = a + 1 // 1 ns\n    a = a * 2 // 10 ns\n    // \u5faa\u73af n \u6b21\n    for (i in 0..<n) {  // 1 ns\n        println(0)      // 5 ns\n    }\n}\n
        # \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\ndef algorithm(n)\n    a = 2       # 1 ns\n    a = a + 1   # 1 ns\n    a = a * 2   # 10 ns\n    # \u5faa\u73af n \u6b21\n    (0...n).each do # 1 ns\n        puts 0      # 5 ns\n    end\nend\n

        \u6839\u636e\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5f97\u5230\u7b97\u6cd5\u7684\u8fd0\u884c\u65f6\u95f4\u4e3a \\((6n + 12)\\) ns \uff1a

        \\[ 1 + 1 + 10 + (1 + 5) \\times n = 6n + 12 \\]

        \u4f46\u5b9e\u9645\u4e0a\uff0c\u7edf\u8ba1\u7b97\u6cd5\u7684\u8fd0\u884c\u65f6\u95f4\u65e2\u4e0d\u5408\u7406\u4e5f\u4e0d\u73b0\u5b9e\u3002\u9996\u5148\uff0c\u6211\u4eec\u4e0d\u5e0c\u671b\u5c06\u9884\u4f30\u65f6\u95f4\u548c\u8fd0\u884c\u5e73\u53f0\u7ed1\u5b9a\uff0c\u56e0\u4e3a\u7b97\u6cd5\u9700\u8981\u5728\u5404\u79cd\u4e0d\u540c\u7684\u5e73\u53f0\u4e0a\u8fd0\u884c\u3002\u5176\u6b21\uff0c\u6211\u4eec\u5f88\u96be\u83b7\u77e5\u6bcf\u79cd\u64cd\u4f5c\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u8fd9\u7ed9\u9884\u4f30\u8fc7\u7a0b\u5e26\u6765\u4e86\u6781\u5927\u7684\u96be\u5ea6\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#231","title":"2.3.1 \u00a0 \u7edf\u8ba1\u65f6\u95f4\u589e\u957f\u8d8b\u52bf","text":"

        \u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u7edf\u8ba1\u7684\u4e0d\u662f\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\uff0c\u800c\u662f\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u968f\u7740\u6570\u636e\u91cf\u53d8\u5927\u65f6\u7684\u589e\u957f\u8d8b\u52bf\u3002

        \u201c\u65f6\u95f4\u589e\u957f\u8d8b\u52bf\u201d\u8fd9\u4e2a\u6982\u5ff5\u6bd4\u8f83\u62bd\u8c61\uff0c\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u6765\u52a0\u4ee5\u7406\u89e3\u3002\u5047\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u7ed9\u5b9a\u4e09\u4e2a\u7b97\u6cd5 A\u3001B \u548c C \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        # \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_A(n: int):\n    print(0)\n# \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\ndef algorithm_B(n: int):\n    for _ in range(n):\n        print(0)\n# \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_C(n: int):\n    for _ in range(1000000):\n        print(0)\n
        // \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\n    cout << 0 << endl;\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\n    for (int i = 0; i < n; i++) {\n        cout << 0 << endl;\n    }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        cout << 0 << endl;\n    }\n}\n
        // \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\n    System.out.println(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\n    for (int i = 0; i < n; i++) {\n        System.out.println(0);\n    }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        System.out.println(0);\n    }\n}\n
        // \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid AlgorithmA(int n) {\n    Console.WriteLine(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid AlgorithmB(int n) {\n    for (int i = 0; i < n; i++) {\n        Console.WriteLine(0);\n    }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid AlgorithmC(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        Console.WriteLine(0);\n    }\n}\n
        // \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithm_A(n int) {\n    fmt.Println(0)\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunc algorithm_B(n int) {\n    for i := 0; i < n; i++ {\n        fmt.Println(0)\n    }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithm_C(n int) {\n    for i := 0; i < 1000000; i++ {\n        fmt.Println(0)\n    }\n}\n
        // \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithmA(n: Int) {\n    print(0)\n}\n\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunc algorithmB(n: Int) {\n    for _ in 0 ..< n {\n        print(0)\n    }\n}\n\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithmC(n: Int) {\n    for _ in 0 ..< 1_000_000 {\n        print(0)\n    }\n}\n
        // \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_A(n) {\n    console.log(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunction algorithm_B(n) {\n    for (let i = 0; i < n; i++) {\n        console.log(0);\n    }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_C(n) {\n    for (let i = 0; i < 1000000; i++) {\n        console.log(0);\n    }\n}\n
        // \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_A(n: number): void {\n    console.log(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunction algorithm_B(n: number): void {\n    for (let i = 0; i < n; i++) {\n        console.log(0);\n    }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_C(n: number): void {\n    for (let i = 0; i < 1000000; i++) {\n        console.log(0);\n    }\n}\n
        // \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithmA(int n) {\n  print(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithmB(int n) {\n  for (int i = 0; i < n; i++) {\n    print(0);\n  }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithmC(int n) {\n  for (int i = 0; i < 1000000; i++) {\n    print(0);\n  }\n}\n
        // \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfn algorithm_A(n: i32) {\n    println!(\"{}\", 0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfn algorithm_B(n: i32) {\n    for _ in 0..n {\n        println!(\"{}\", 0);\n    }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfn algorithm_C(n: i32) {\n    for _ in 0..1000000 {\n        println!(\"{}\", 0);\n    }\n}\n
        // \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\n    printf(\"%d\", 0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\n    for (int i = 0; i < n; i++) {\n        printf(\"%d\", 0);\n    }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        printf(\"%d\", 0);\n    }\n}\n
        // \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfun algoritm_A(n: Int) {\n    println(0)\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfun algorithm_B(n: Int) {\n    for (i in 0..<n){\n        println(0)\n    }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfun algorithm_C(n: Int) {\n    for (i in 0..<1000000) {\n        println(0)\n    }\n}\n
        # \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_A(n)\n    puts 0\nend\n\n# \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\ndef algorithm_B(n)\n    (0...n).each { puts 0 }\nend\n\n# \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_C(n)\n    (0...1_000_000).each { puts 0 }\nend\n

        \u56fe 2-7 \u5c55\u793a\u4e86\u4ee5\u4e0a\u4e09\u4e2a\u7b97\u6cd5\u51fd\u6570\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002

        • \u7b97\u6cd5 A \u53ea\u6709 \\(1\\) \u4e2a\u6253\u5370\u64cd\u4f5c\uff0c\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u4e0d\u968f\u7740 \\(n\\) \u589e\u5927\u800c\u589e\u957f\u3002\u6211\u4eec\u79f0\u6b64\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\u201c\u5e38\u6570\u9636\u201d\u3002
        • \u7b97\u6cd5 B \u4e2d\u7684\u6253\u5370\u64cd\u4f5c\u9700\u8981\u5faa\u73af \\(n\\) \u6b21\uff0c\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u968f\u7740 \\(n\\) \u589e\u5927\u5448\u7ebf\u6027\u589e\u957f\u3002\u6b64\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u88ab\u79f0\u4e3a\u201c\u7ebf\u6027\u9636\u201d\u3002
        • \u7b97\u6cd5 C \u4e2d\u7684\u6253\u5370\u64cd\u4f5c\u9700\u8981\u5faa\u73af \\(1000000\\) \u6b21\uff0c\u867d\u7136\u8fd0\u884c\u65f6\u95f4\u5f88\u957f\uff0c\u4f46\u5b83\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\u3002\u56e0\u6b64 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u548c A \u76f8\u540c\uff0c\u4ecd\u4e3a\u201c\u5e38\u6570\u9636\u201d\u3002

        \u56fe 2-7 \u00a0 \u7b97\u6cd5 A\u3001B \u548c C \u7684\u65f6\u95f4\u589e\u957f\u8d8b\u52bf

        \u76f8\u8f83\u4e8e\u76f4\u63a5\u7edf\u8ba1\u7b97\u6cd5\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u6709\u54ea\u4e9b\u7279\u70b9\u5462\uff1f

        • \u65f6\u95f4\u590d\u6742\u5ea6\u80fd\u591f\u6709\u6548\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\u3002\u4f8b\u5982\uff0c\u7b97\u6cd5 B \u7684\u8fd0\u884c\u65f6\u95f4\u5448\u7ebf\u6027\u589e\u957f\uff0c\u5728 \\(n > 1\\) \u65f6\u6bd4\u7b97\u6cd5 A \u66f4\u6162\uff0c\u5728 \\(n > 1000000\\) \u65f6\u6bd4\u7b97\u6cd5 C \u66f4\u6162\u3002\u4e8b\u5b9e\u4e0a\uff0c\u53ea\u8981\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u8db3\u591f\u5927\uff0c\u590d\u6742\u5ea6\u4e3a\u201c\u5e38\u6570\u9636\u201d\u7684\u7b97\u6cd5\u4e00\u5b9a\u4f18\u4e8e\u201c\u7ebf\u6027\u9636\u201d\u7684\u7b97\u6cd5\uff0c\u8fd9\u6b63\u662f\u65f6\u95f4\u589e\u957f\u8d8b\u52bf\u7684\u542b\u4e49\u3002
        • \u65f6\u95f4\u590d\u6742\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u66f4\u7b80\u4fbf\u3002\u663e\u7136\uff0c\u8fd0\u884c\u5e73\u53f0\u548c\u8ba1\u7b97\u64cd\u4f5c\u7c7b\u578b\u90fd\u4e0e\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u65e0\u5173\u3002\u56e0\u6b64\u5728\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u5c06\u6240\u6709\u8ba1\u7b97\u64cd\u4f5c\u7684\u6267\u884c\u65f6\u95f4\u89c6\u4e3a\u76f8\u540c\u7684\u201c\u5355\u4f4d\u65f6\u95f4\u201d\uff0c\u4ece\u800c\u5c06\u201c\u8ba1\u7b97\u64cd\u4f5c\u8fd0\u884c\u65f6\u95f4\u7edf\u8ba1\u201d\u7b80\u5316\u4e3a\u201c\u8ba1\u7b97\u64cd\u4f5c\u6570\u91cf\u7edf\u8ba1\u201d\uff0c\u8fd9\u6837\u4e00\u6765\u4f30\u7b97\u96be\u5ea6\u5c31\u5927\u5927\u964d\u4f4e\u4e86\u3002
        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u5b58\u5728\u4e00\u5b9a\u7684\u5c40\u9650\u6027\u3002\u4f8b\u5982\uff0c\u5c3d\u7ba1\u7b97\u6cd5 A \u548c C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u76f8\u540c\uff0c\u4f46\u5b9e\u9645\u8fd0\u884c\u65f6\u95f4\u5dee\u522b\u5f88\u5927\u3002\u540c\u6837\uff0c\u5c3d\u7ba1\u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u6bd4 C \u9ad8\uff0c\u4f46\u5728\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u8f83\u5c0f\u65f6\uff0c\u7b97\u6cd5 B \u660e\u663e\u4f18\u4e8e\u7b97\u6cd5 C \u3002\u5bf9\u4e8e\u6b64\u7c7b\u60c5\u51b5\uff0c\u6211\u4eec\u65f6\u5e38\u96be\u4ee5\u4ec5\u51ed\u65f6\u95f4\u590d\u6742\u5ea6\u5224\u65ad\u7b97\u6cd5\u6548\u7387\u7684\u9ad8\u4f4e\u3002\u5f53\u7136\uff0c\u5c3d\u7ba1\u5b58\u5728\u4e0a\u8ff0\u95ee\u9898\uff0c\u590d\u6742\u5ea6\u5206\u6790\u4ecd\u7136\u662f\u8bc4\u5224\u7b97\u6cd5\u6548\u7387\u6700\u6709\u6548\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\u3002
        "},{"location":"chapter_computational_complexity/time_complexity/#232","title":"2.3.2 \u00a0 \u51fd\u6570\u6e10\u8fd1\u4e0a\u754c","text":"

        \u7ed9\u5b9a\u4e00\u4e2a\u8f93\u5165\u5927\u5c0f\u4e3a \\(n\\) \u7684\u51fd\u6570\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        def algorithm(n: int):\n    a = 1      # +1\n    a = a + 1  # +1\n    a = a * 2  # +1\n    # \u5faa\u73af n \u6b21\n    for i in range(n):  # +1\n        print(0)        # +1\n
        void algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // \u5faa\u73af n \u6b21\n    for (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n        cout << 0 << endl;    // +1\n    }\n}\n
        void algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // \u5faa\u73af n \u6b21\n    for (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n        System.out.println(0);    // +1\n    }\n}\n
        void Algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // \u5faa\u73af n \u6b21\n    for (int i = 0; i < n; i++) {   // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n        Console.WriteLine(0);   // +1\n    }\n}\n
        func algorithm(n int) {\n    a := 1      // +1\n    a = a + 1   // +1\n    a = a * 2   // +1\n    // \u5faa\u73af n \u6b21\n    for i := 0; i < n; i++ {   // +1\n        fmt.Println(a)         // +1\n    }\n}\n
        func algorithm(n: Int) {\n    var a = 1 // +1\n    a = a + 1 // +1\n    a = a * 2 // +1\n    // \u5faa\u73af n \u6b21\n    for _ in 0 ..< n { // +1\n        print(0) // +1\n    }\n}\n
        function algorithm(n) {\n    var a = 1; // +1\n    a += 1; // +1\n    a *= 2; // +1\n    // \u5faa\u73af n \u6b21\n    for(let i = 0; i < n; i++){ // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n        console.log(0); // +1\n    }\n}\n
        function algorithm(n: number): void{\n    var a: number = 1; // +1\n    a += 1; // +1\n    a *= 2; // +1\n    // \u5faa\u73af n \u6b21\n    for(let i = 0; i < n; i++){ // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n        console.log(0); // +1\n    }\n}\n
        void algorithm(int n) {\n  int a = 1; // +1\n  a = a + 1; // +1\n  a = a * 2; // +1\n  // \u5faa\u73af n \u6b21\n  for (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n    print(0); // +1\n  }\n}\n
        fn algorithm(n: i32) {\n    let mut a = 1;   // +1\n    a = a + 1;      // +1\n    a = a * 2;      // +1\n\n    // \u5faa\u73af n \u6b21\n    for _ in 0..n { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n        println!(\"{}\", 0); // +1\n    }\n}\n
        void algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // \u5faa\u73af n \u6b21\n    for (int i = 0; i < n; i++) {   // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n        printf(\"%d\", 0);            // +1\n    }\n}\n
        fun algorithm(n: Int) {\n    var a = 1 // +1\n    a = a + 1 // +1\n    a = a * 2 // +1\n    // \u5faa\u73af n \u6b21\n    for (i in 0..<n) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n        println(0) // +1\n    }\n}\n
        def algorithm(n)\n    a = 1       # +1\n    a = a + 1   # +1\n    a = a * 2   # +1\n    # \u5faa\u73af n \u6b21\n    (0...n).each do # +1\n        puts 0      # +1\n    end\nend\n

        \u8bbe\u7b97\u6cd5\u7684\u64cd\u4f5c\u6570\u91cf\u662f\u4e00\u4e2a\u5173\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u7684\u51fd\u6570\uff0c\u8bb0\u4e3a \\(T(n)\\) \uff0c\u5219\u4ee5\u4e0a\u51fd\u6570\u7684\u64cd\u4f5c\u6570\u91cf\u4e3a\uff1a

        \\[ T(n) = 3 + 2n \\]

        \\(T(n)\\) \u662f\u4e00\u6b21\u51fd\u6570\uff0c\u8bf4\u660e\u5176\u8fd0\u884c\u65f6\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u662f\u7ebf\u6027\u7684\uff0c\u56e0\u6b64\u5b83\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f\u7ebf\u6027\u9636\u3002

        \u6211\u4eec\u5c06\u7ebf\u6027\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8bb0\u4e3a \\(O(n)\\) \uff0c\u8fd9\u4e2a\u6570\u5b66\u7b26\u53f7\u79f0\u4e3a\u5927 \\(O\\) \u8bb0\u53f7\uff08big-\\(O\\) notation\uff09\uff0c\u8868\u793a\u51fd\u6570 \\(T(n)\\) \u7684\u6e10\u8fd1\u4e0a\u754c\uff08asymptotic upper bound\uff09\u3002

        \u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u672c\u8d28\u4e0a\u662f\u8ba1\u7b97\u201c\u64cd\u4f5c\u6570\u91cf \\(T(n)\\)\u201d\u7684\u6e10\u8fd1\u4e0a\u754c\uff0c\u5b83\u5177\u6709\u660e\u786e\u7684\u6570\u5b66\u5b9a\u4e49\u3002

        \u51fd\u6570\u6e10\u8fd1\u4e0a\u754c

        \u82e5\u5b58\u5728\u6b63\u5b9e\u6570 \\(c\\) \u548c\u5b9e\u6570 \\(n_0\\) \uff0c\u4f7f\u5f97\u5bf9\u4e8e\u6240\u6709\u7684 \\(n > n_0\\) \uff0c\u5747\u6709 \\(T(n) \\leq c \\cdot f(n)\\) \uff0c\u5219\u53ef\u8ba4\u4e3a \\(f(n)\\) \u7ed9\u51fa\u4e86 \\(T(n)\\) \u7684\u4e00\u4e2a\u6e10\u8fd1\u4e0a\u754c\uff0c\u8bb0\u4e3a \\(T(n) = O(f(n))\\) \u3002

        \u5982\u56fe 2-8 \u6240\u793a\uff0c\u8ba1\u7b97\u6e10\u8fd1\u4e0a\u754c\u5c31\u662f\u5bfb\u627e\u4e00\u4e2a\u51fd\u6570 \\(f(n)\\) \uff0c\u4f7f\u5f97\u5f53 \\(n\\) \u8d8b\u5411\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\\(T(n)\\) \u548c \\(f(n)\\) \u5904\u4e8e\u76f8\u540c\u7684\u589e\u957f\u7ea7\u522b\uff0c\u4ec5\u76f8\u5dee\u4e00\u4e2a\u5e38\u6570\u7cfb\u6570 \\(c\\)\u3002

        \u56fe 2-8 \u00a0 \u51fd\u6570\u7684\u6e10\u8fd1\u4e0a\u754c

        "},{"location":"chapter_computational_complexity/time_complexity/#233","title":"2.3.3 \u00a0 \u63a8\u7b97\u65b9\u6cd5","text":"

        \u6e10\u8fd1\u4e0a\u754c\u7684\u6570\u5b66\u5473\u513f\u6709\u70b9\u91cd\uff0c\u5982\u679c\u4f60\u611f\u89c9\u6ca1\u6709\u5b8c\u5168\u7406\u89e3\uff0c\u4e5f\u65e0\u987b\u62c5\u5fc3\u3002\u6211\u4eec\u53ef\u4ee5\u5148\u638c\u63e1\u63a8\u7b97\u65b9\u6cd5\uff0c\u5728\u4e0d\u65ad\u7684\u5b9e\u8df5\u4e2d\uff0c\u5c31\u53ef\u4ee5\u9010\u6e10\u9886\u609f\u5176\u6570\u5b66\u610f\u4e49\u3002

        \u6839\u636e\u5b9a\u4e49\uff0c\u786e\u5b9a \\(f(n)\\) \u4e4b\u540e\uff0c\u6211\u4eec\u4fbf\u53ef\u5f97\u5230\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(f(n))\\) \u3002\u90a3\u4e48\u5982\u4f55\u786e\u5b9a\u6e10\u8fd1\u4e0a\u754c \\(f(n)\\) \u5462\uff1f\u603b\u4f53\u5206\u4e3a\u4e24\u6b65\uff1a\u9996\u5148\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf\uff0c\u7136\u540e\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#1","title":"1. \u00a0 \u7b2c\u4e00\u6b65\uff1a\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf","text":"

        \u9488\u5bf9\u4ee3\u7801\uff0c\u9010\u884c\u4ece\u4e0a\u5230\u4e0b\u8ba1\u7b97\u5373\u53ef\u3002\u7136\u800c\uff0c\u7531\u4e8e\u4e0a\u8ff0 \\(c \\cdot f(n)\\) \u4e2d\u7684\u5e38\u6570\u7cfb\u6570 \\(c\\) \u53ef\u4ee5\u53d6\u4efb\u610f\u5927\u5c0f\uff0c\u56e0\u6b64\u64cd\u4f5c\u6570\u91cf \\(T(n)\\) \u4e2d\u7684\u5404\u79cd\u7cfb\u6570\u3001\u5e38\u6570\u9879\u90fd\u53ef\u4ee5\u5ffd\u7565\u3002\u6839\u636e\u6b64\u539f\u5219\uff0c\u53ef\u4ee5\u603b\u7ed3\u51fa\u4ee5\u4e0b\u8ba1\u6570\u7b80\u5316\u6280\u5de7\u3002

        1. \u5ffd\u7565 \\(T(n)\\) \u4e2d\u7684\u5e38\u6570\u3002\u56e0\u4e3a\u5b83\u4eec\u90fd\u4e0e \\(n\\) \u65e0\u5173\uff0c\u6240\u4ee5\u5bf9\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u4ea7\u751f\u5f71\u54cd\u3002
        2. \u7701\u7565\u6240\u6709\u7cfb\u6570\u3002\u4f8b\u5982\uff0c\u5faa\u73af \\(2n\\) \u6b21\u3001\\(5n + 1\\) \u6b21\u7b49\uff0c\u90fd\u53ef\u4ee5\u7b80\u5316\u8bb0\u4e3a \\(n\\) \u6b21\uff0c\u56e0\u4e3a \\(n\\) \u524d\u9762\u7684\u7cfb\u6570\u5bf9\u65f6\u95f4\u590d\u6742\u5ea6\u6ca1\u6709\u5f71\u54cd\u3002
        3. \u5faa\u73af\u5d4c\u5957\u65f6\u4f7f\u7528\u4e58\u6cd5\u3002\u603b\u64cd\u4f5c\u6570\u91cf\u7b49\u4e8e\u5916\u5c42\u5faa\u73af\u548c\u5185\u5c42\u5faa\u73af\u64cd\u4f5c\u6570\u91cf\u4e4b\u79ef\uff0c\u6bcf\u4e00\u5c42\u5faa\u73af\u4f9d\u7136\u53ef\u4ee5\u5206\u522b\u5957\u7528\u7b2c 1. \u70b9\u548c\u7b2c 2. \u70b9\u7684\u6280\u5de7\u3002

        \u7ed9\u5b9a\u4e00\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4e0a\u8ff0\u6280\u5de7\u6765\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        def algorithm(n: int):\n    a = 1      # +0\uff08\u6280\u5de7 1\uff09\n    a = a + n  # +0\uff08\u6280\u5de7 1\uff09\n    # +n\uff08\u6280\u5de7 2\uff09\n    for i in range(5 * n + 1):\n        print(0)\n    # +n*n\uff08\u6280\u5de7 3\uff09\n    for i in range(2 * n):\n        for j in range(n + 1):\n            print(0)\n
        void algorithm(int n) {\n    int a = 1;  // +0\uff08\u6280\u5de7 1\uff09\n    a = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n    // +n\uff08\u6280\u5de7 2\uff09\n    for (int i = 0; i < 5 * n + 1; i++) {\n        cout << 0 << endl;\n    }\n    // +n*n\uff08\u6280\u5de7 3\uff09\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            cout << 0 << endl;\n        }\n    }\n}\n
        void algorithm(int n) {\n    int a = 1;  // +0\uff08\u6280\u5de7 1\uff09\n    a = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n    // +n\uff08\u6280\u5de7 2\uff09\n    for (int i = 0; i < 5 * n + 1; i++) {\n        System.out.println(0);\n    }\n    // +n*n\uff08\u6280\u5de7 3\uff09\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            System.out.println(0);\n        }\n    }\n}\n
        void Algorithm(int n) {\n    int a = 1;  // +0\uff08\u6280\u5de7 1\uff09\n    a = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n    // +n\uff08\u6280\u5de7 2\uff09\n    for (int i = 0; i < 5 * n + 1; i++) {\n        Console.WriteLine(0);\n    }\n    // +n*n\uff08\u6280\u5de7 3\uff09\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            Console.WriteLine(0);\n        }\n    }\n}\n
        func algorithm(n int) {\n    a := 1     // +0\uff08\u6280\u5de7 1\uff09\n    a = a + n  // +0\uff08\u6280\u5de7 1\uff09\n    // +n\uff08\u6280\u5de7 2\uff09\n    for i := 0; i < 5 * n + 1; i++ {\n        fmt.Println(0)\n    }\n    // +n*n\uff08\u6280\u5de7 3\uff09\n    for i := 0; i < 2 * n; i++ {\n        for j := 0; j < n + 1; j++ {\n            fmt.Println(0)\n        }\n    }\n}\n
        func algorithm(n: Int) {\n    var a = 1 // +0\uff08\u6280\u5de7 1\uff09\n    a = a + n // +0\uff08\u6280\u5de7 1\uff09\n    // +n\uff08\u6280\u5de7 2\uff09\n    for _ in 0 ..< (5 * n + 1) {\n        print(0)\n    }\n    // +n*n\uff08\u6280\u5de7 3\uff09\n    for _ in 0 ..< (2 * n) {\n        for _ in 0 ..< (n + 1) {\n            print(0)\n        }\n    }\n}\n
        function algorithm(n) {\n    let a = 1;  // +0\uff08\u6280\u5de7 1\uff09\n    a = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n    // +n\uff08\u6280\u5de7 2\uff09\n    for (let i = 0; i < 5 * n + 1; i++) {\n        console.log(0);\n    }\n    // +n*n\uff08\u6280\u5de7 3\uff09\n    for (let i = 0; i < 2 * n; i++) {\n        for (let j = 0; j < n + 1; j++) {\n            console.log(0);\n        }\n    }\n}\n
        function algorithm(n: number): void {\n    let a = 1;  // +0\uff08\u6280\u5de7 1\uff09\n    a = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n    // +n\uff08\u6280\u5de7 2\uff09\n    for (let i = 0; i < 5 * n + 1; i++) {\n        console.log(0);\n    }\n    // +n*n\uff08\u6280\u5de7 3\uff09\n    for (let i = 0; i < 2 * n; i++) {\n        for (let j = 0; j < n + 1; j++) {\n            console.log(0);\n        }\n    }\n}\n
        void algorithm(int n) {\n  int a = 1; // +0\uff08\u6280\u5de7 1\uff09\n  a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n  // +n\uff08\u6280\u5de7 2\uff09\n  for (int i = 0; i < 5 * n + 1; i++) {\n    print(0);\n  }\n  // +n*n\uff08\u6280\u5de7 3\uff09\n  for (int i = 0; i < 2 * n; i++) {\n    for (int j = 0; j < n + 1; j++) {\n      print(0);\n    }\n  }\n}\n
        fn algorithm(n: i32) {\n    let mut a = 1;     // +0\uff08\u6280\u5de7 1\uff09\n    a = a + n;        // +0\uff08\u6280\u5de7 1\uff09\n\n    // +n\uff08\u6280\u5de7 2\uff09\n    for i in 0..(5 * n + 1) {\n        println!(\"{}\", 0);\n    }\n\n    // +n*n\uff08\u6280\u5de7 3\uff09\n    for i in 0..(2 * n) {\n        for j in 0..(n + 1) {\n            println!(\"{}\", 0);\n        }\n    }\n}\n
        void algorithm(int n) {\n    int a = 1;  // +0\uff08\u6280\u5de7 1\uff09\n    a = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n    // +n\uff08\u6280\u5de7 2\uff09\n    for (int i = 0; i < 5 * n + 1; i++) {\n        printf(\"%d\", 0);\n    }\n    // +n*n\uff08\u6280\u5de7 3\uff09\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            printf(\"%d\", 0);\n        }\n    }\n}\n
        fun algorithm(n: Int) {\n    var a = 1   // +0\uff08\u6280\u5de7 1\uff09\n    a = a + n   // +0\uff08\u6280\u5de7 1\uff09\n    // +n\uff08\u6280\u5de7 2\uff09\n    for (i in 0..<5 * n + 1) {\n        println(0)\n    }\n    // +n*n\uff08\u6280\u5de7 3\uff09\n    for (i in 0..<2 * n) {\n        for (j in 0..<n + 1) {\n            println(0)\n        }\n    }\n}\n
        def algorithm(n)\n    a = 1       # +0\uff08\u6280\u5de7 1\uff09\n    a = a + n   # +0\uff08\u6280\u5de7 1\uff09\n    # +n\uff08\u6280\u5de7 2\uff09\n    (0...(5 * n + 1)).each do { puts 0 }\n    # +n*n\uff08\u6280\u5de7 3\uff09\n    (0...(2 * n)).each do\n        (0...(n + 1)).each do { puts 0 }\n    end\nend\n

        \u4ee5\u4e0b\u516c\u5f0f\u5c55\u793a\u4e86\u4f7f\u7528\u4e0a\u8ff0\u6280\u5de7\u524d\u540e\u7684\u7edf\u8ba1\u7ed3\u679c\uff0c\u4e24\u8005\u63a8\u7b97\u51fa\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n^2)\\) \u3002

        \\[ \\begin{aligned} T(n) & = 2n(n + 1) + (5n + 1) + 2 & \\text{\u5b8c\u6574\u7edf\u8ba1 (-.-|||)} \\newline & = 2n^2 + 7n + 3 \\newline T(n) & = n^2 + n & \\text{\u5077\u61d2\u7edf\u8ba1 (o.O)} \\end{aligned} \\]"},{"location":"chapter_computational_complexity/time_complexity/#2","title":"2. \u00a0 \u7b2c\u4e8c\u6b65\uff1a\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c","text":"

        \u65f6\u95f4\u590d\u6742\u5ea6\u7531 \\(T(n)\\) \u4e2d\u6700\u9ad8\u9636\u7684\u9879\u6765\u51b3\u5b9a\u3002\u8fd9\u662f\u56e0\u4e3a\u5728 \\(n\\) \u8d8b\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\u6700\u9ad8\u9636\u7684\u9879\u5c06\u53d1\u6325\u4e3b\u5bfc\u4f5c\u7528\uff0c\u5176\u4ed6\u9879\u7684\u5f71\u54cd\u90fd\u53ef\u4ee5\u5ffd\u7565\u3002

        \u8868 2-2 \u5c55\u793a\u4e86\u4e00\u4e9b\u4f8b\u5b50\uff0c\u5176\u4e2d\u4e00\u4e9b\u5938\u5f20\u7684\u503c\u662f\u4e3a\u4e86\u5f3a\u8c03\u201c\u7cfb\u6570\u65e0\u6cd5\u64bc\u52a8\u9636\u6570\u201d\u8fd9\u4e00\u7ed3\u8bba\u3002\u5f53 \\(n\\) \u8d8b\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\u8fd9\u4e9b\u5e38\u6570\u53d8\u5f97\u65e0\u8db3\u8f7b\u91cd\u3002

        \u8868 2-2 \u00a0 \u4e0d\u540c\u64cd\u4f5c\u6570\u91cf\u5bf9\u5e94\u7684\u65f6\u95f4\u590d\u6742\u5ea6

        \u64cd\u4f5c\u6570\u91cf \\(T(n)\\) \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(f(n))\\) \\(100000\\) \\(O(1)\\) \\(3n + 2\\) \\(O(n)\\) \\(2n^2 + 3n + 2\\) \\(O(n^2)\\) \\(n^3 + 10000n^2\\) \\(O(n^3)\\) \\(2^n + 10000n^{10000}\\) \\(O(2^n)\\)"},{"location":"chapter_computational_complexity/time_complexity/#234","title":"2.3.4 \u00a0 \u5e38\u89c1\u7c7b\u578b","text":"

        \u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u5e38\u89c1\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\u5982\u56fe 2-9 \u6240\u793a\uff08\u6309\u7167\u4ece\u4f4e\u5230\u9ad8\u7684\u987a\u5e8f\u6392\u5217\uff09\u3002

        \\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n \\log n) < O(n^2) < O(2^n) < O(n!) \\newline \\text{\u5e38\u6570\u9636} < \\text{\u5bf9\u6570\u9636} < \\text{\u7ebf\u6027\u9636} < \\text{\u7ebf\u6027\u5bf9\u6570\u9636} < \\text{\u5e73\u65b9\u9636} < \\text{\u6307\u6570\u9636} < \\text{\u9636\u4e58\u9636} \\end{aligned} \\]

        \u56fe 2-9 \u00a0 \u5e38\u89c1\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7c7b\u578b

        "},{"location":"chapter_computational_complexity/time_complexity/#1-o1","title":"1. \u00a0 \u5e38\u6570\u9636 \\(O(1)\\)","text":"

        \u5e38\u6570\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\uff0c\u5373\u4e0d\u968f\u7740 \\(n\\) \u7684\u53d8\u5316\u800c\u53d8\u5316\u3002

        \u5728\u4ee5\u4e0b\u51fd\u6570\u4e2d\uff0c\u5c3d\u7ba1\u64cd\u4f5c\u6570\u91cf size \u53ef\u80fd\u5f88\u5927\uff0c\u4f46\u7531\u4e8e\u5176\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def constant(n: int) -> int:\n    \"\"\"\u5e38\u6570\u9636\"\"\"\n    count = 0\n    size = 100000\n    for _ in range(size):\n        count += 1\n    return count\n
        time_complexity.cpp
        /* \u5e38\u6570\u9636 */\nint constant(int n) {\n    int count = 0;\n    int size = 100000;\n    for (int i = 0; i < size; i++)\n        count++;\n    return count;\n}\n
        time_complexity.java
        /* \u5e38\u6570\u9636 */\nint constant(int n) {\n    int count = 0;\n    int size = 100000;\n    for (int i = 0; i < size; i++)\n        count++;\n    return count;\n}\n
        time_complexity.cs
        /* \u5e38\u6570\u9636 */\nint Constant(int n) {\n    int count = 0;\n    int size = 100000;\n    for (int i = 0; i < size; i++)\n        count++;\n    return count;\n}\n
        time_complexity.go
        /* \u5e38\u6570\u9636 */\nfunc constant(n int) int {\n    count := 0\n    size := 100000\n    for i := 0; i < size; i++ {\n        count++\n    }\n    return count\n}\n
        time_complexity.swift
        /* \u5e38\u6570\u9636 */\nfunc constant(n: Int) -> Int {\n    var count = 0\n    let size = 100_000\n    for _ in 0 ..< size {\n        count += 1\n    }\n    return count\n}\n
        time_complexity.js
        /* \u5e38\u6570\u9636 */\nfunction constant(n) {\n    let count = 0;\n    const size = 100000;\n    for (let i = 0; i < size; i++) count++;\n    return count;\n}\n
        time_complexity.ts
        /* \u5e38\u6570\u9636 */\nfunction constant(n: number): number {\n    let count = 0;\n    const size = 100000;\n    for (let i = 0; i < size; i++) count++;\n    return count;\n}\n
        time_complexity.dart
        /* \u5e38\u6570\u9636 */\nint constant(int n) {\n  int count = 0;\n  int size = 100000;\n  for (var i = 0; i < size; i++) {\n    count++;\n  }\n  return count;\n}\n
        time_complexity.rs
        /* \u5e38\u6570\u9636 */\nfn constant(n: i32) -> i32 {\n    _ = n;\n    let mut count = 0;\n    let size = 100_000;\n    for _ in 0..size {\n        count += 1;\n    }\n    count\n}\n
        time_complexity.c
        /* \u5e38\u6570\u9636 */\nint constant(int n) {\n    int count = 0;\n    int size = 100000;\n    int i = 0;\n    for (int i = 0; i < size; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.kt
        /* \u5e38\u6570\u9636 */\nfun constant(n: Int): Int {\n    var count = 0\n    val size = 100000\n    for (i in 0..<size)\n        count++\n    return count\n}\n
        time_complexity.rb
        ### \u5e38\u6570\u9636 ###\ndef constant(n)\n  count = 0\n  size = 100000\n\n  (0...size).each { count += 1 }\n\n  count\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_computational_complexity/time_complexity/#2-on","title":"2. \u00a0 \u7ebf\u6027\u9636 \\(O(n)\\)","text":"

        \u7ebf\u6027\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u76f8\u5bf9\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u4ee5\u7ebf\u6027\u7ea7\u522b\u589e\u957f\u3002\u7ebf\u6027\u9636\u901a\u5e38\u51fa\u73b0\u5728\u5355\u5c42\u5faa\u73af\u4e2d\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def linear(n: int) -> int:\n    \"\"\"\u7ebf\u6027\u9636\"\"\"\n    count = 0\n    for _ in range(n):\n        count += 1\n    return count\n
        time_complexity.cpp
        /* \u7ebf\u6027\u9636 */\nint linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++)\n        count++;\n    return count;\n}\n
        time_complexity.java
        /* \u7ebf\u6027\u9636 */\nint linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++)\n        count++;\n    return count;\n}\n
        time_complexity.cs
        /* \u7ebf\u6027\u9636 */\nint Linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++)\n        count++;\n    return count;\n}\n
        time_complexity.go
        /* \u7ebf\u6027\u9636 */\nfunc linear(n int) int {\n    count := 0\n    for i := 0; i < n; i++ {\n        count++\n    }\n    return count\n}\n
        time_complexity.swift
        /* \u7ebf\u6027\u9636 */\nfunc linear(n: Int) -> Int {\n    var count = 0\n    for _ in 0 ..< n {\n        count += 1\n    }\n    return count\n}\n
        time_complexity.js
        /* \u7ebf\u6027\u9636 */\nfunction linear(n) {\n    let count = 0;\n    for (let i = 0; i < n; i++) count++;\n    return count;\n}\n
        time_complexity.ts
        /* \u7ebf\u6027\u9636 */\nfunction linear(n: number): number {\n    let count = 0;\n    for (let i = 0; i < n; i++) count++;\n    return count;\n}\n
        time_complexity.dart
        /* \u7ebf\u6027\u9636 */\nint linear(int n) {\n  int count = 0;\n  for (var i = 0; i < n; i++) {\n    count++;\n  }\n  return count;\n}\n
        time_complexity.rs
        /* \u7ebf\u6027\u9636 */\nfn linear(n: i32) -> i32 {\n    let mut count = 0;\n    for _ in 0..n {\n        count += 1;\n    }\n    count\n}\n
        time_complexity.c
        /* \u7ebf\u6027\u9636 */\nint linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.kt
        /* \u7ebf\u6027\u9636 */\nfun linear(n: Int): Int {\n    var count = 0\n    for (i in 0..<n)\n        count++\n    return count\n}\n
        time_complexity.rb
        ### \u7ebf\u6027\u9636 ###\ndef linear(n)\n  count = 0\n  (0...n).each { count += 1 }\n  count\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u904d\u5386\u6570\u7ec4\u548c\u904d\u5386\u94fe\u8868\u7b49\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u6570\u7ec4\u6216\u94fe\u8868\u7684\u957f\u5ea6\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def array_traversal(nums: list[int]) -> int:\n    \"\"\"\u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09\"\"\"\n    count = 0\n    # \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n    for num in nums:\n        count += 1\n    return count\n
        time_complexity.cpp
        /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(vector<int> &nums) {\n    int count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n    for (int num : nums) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int[] nums) {\n    int count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n    for (int num : nums) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.cs
        /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint ArrayTraversal(int[] nums) {\n    int count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n    foreach (int num in nums) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.go
        /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunc arrayTraversal(nums []int) int {\n    count := 0\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n    for range nums {\n        count++\n    }\n    return count\n}\n
        time_complexity.swift
        /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunc arrayTraversal(nums: [Int]) -> Int {\n    var count = 0\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n    for _ in nums {\n        count += 1\n    }\n    return count\n}\n
        time_complexity.js
        /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunction arrayTraversal(nums) {\n    let count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n    for (let i = 0; i < nums.length; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.ts
        /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunction arrayTraversal(nums: number[]): number {\n    let count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n    for (let i = 0; i < nums.length; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.dart
        /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(List<int> nums) {\n  int count = 0;\n  // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n  for (var _num in nums) {\n    count++;\n  }\n  return count;\n}\n
        time_complexity.rs
        /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfn array_traversal(nums: &[i32]) -> i32 {\n    let mut count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n    for _ in nums {\n        count += 1;\n    }\n    count\n}\n
        time_complexity.c
        /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int *nums, int n) {\n    int count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.kt
        /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfun arrayTraversal(nums: IntArray): Int {\n    var count = 0\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n    for (num in nums) {\n        count++\n    }\n    return count\n}\n
        time_complexity.rb
        ### \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09###\ndef array_traversal(nums)\n  count = 0\n\n  # \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n  for num in nums\n    count += 1\n  end\n\n  count\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u9700\u6839\u636e\u8f93\u5165\u6570\u636e\u7684\u7c7b\u578b\u6765\u5177\u4f53\u786e\u5b9a\u3002\u6bd4\u5982\u5728\u7b2c\u4e00\u4e2a\u793a\u4f8b\u4e2d\uff0c\u53d8\u91cf \\(n\\) \u4e3a\u8f93\u5165\u6570\u636e\u5927\u5c0f\uff1b\u5728\u7b2c\u4e8c\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6570\u7ec4\u957f\u5ea6 \\(n\\) \u4e3a\u6570\u636e\u5927\u5c0f\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#3-on2","title":"3. \u00a0 \u5e73\u65b9\u9636 \\(O(n^2)\\)","text":"

        \u5e73\u65b9\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u76f8\u5bf9\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u4ee5\u5e73\u65b9\u7ea7\u522b\u589e\u957f\u3002\u5e73\u65b9\u9636\u901a\u5e38\u51fa\u73b0\u5728\u5d4c\u5957\u5faa\u73af\u4e2d\uff0c\u5916\u5c42\u5faa\u73af\u548c\u5185\u5c42\u5faa\u73af\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n)\\) \uff0c\u56e0\u6b64\u603b\u4f53\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def quadratic(n: int) -> int:\n    \"\"\"\u5e73\u65b9\u9636\"\"\"\n    count = 0\n    # \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n    for i in range(n):\n        for j in range(n):\n            count += 1\n    return count\n
        time_complexity.cpp
        /* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n    int count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n    int count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
        time_complexity.cs
        /* \u5e73\u65b9\u9636 */\nint Quadratic(int n) {\n    int count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
        time_complexity.go
        /* \u5e73\u65b9\u9636 */\nfunc quadratic(n int) int {\n    count := 0\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n    for i := 0; i < n; i++ {\n        for j := 0; j < n; j++ {\n            count++\n        }\n    }\n    return count\n}\n
        time_complexity.swift
        /* \u5e73\u65b9\u9636 */\nfunc quadratic(n: Int) -> Int {\n    var count = 0\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n    for _ in 0 ..< n {\n        for _ in 0 ..< n {\n            count += 1\n        }\n    }\n    return count\n}\n
        time_complexity.js
        /* \u5e73\u65b9\u9636 */\nfunction quadratic(n) {\n    let count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n    for (let i = 0; i < n; i++) {\n        for (let j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
        time_complexity.ts
        /* \u5e73\u65b9\u9636 */\nfunction quadratic(n: number): number {\n    let count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n    for (let i = 0; i < n; i++) {\n        for (let j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
        time_complexity.dart
        /* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n  int count = 0;\n  // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n  for (int i = 0; i < n; i++) {\n    for (int j = 0; j < n; j++) {\n      count++;\n    }\n  }\n  return count;\n}\n
        time_complexity.rs
        /* \u5e73\u65b9\u9636 */\nfn quadratic(n: i32) -> i32 {\n    let mut count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n    for _ in 0..n {\n        for _ in 0..n {\n            count += 1;\n        }\n    }\n    count\n}\n
        time_complexity.c
        /* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n    int count = 0;\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
        time_complexity.kt
        /* \u5e73\u65b9\u9636 */\nfun quadratic(n: Int): Int {\n    var count = 0\n    // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n    for (i in 0..<n) {\n        for (j in 0..<n) {\n            count++\n        }\n    }\n    return count\n}\n
        time_complexity.rb
        ### \u5e73\u65b9\u9636 ###\ndef quadratic(n)\n  count = 0\n\n  # \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n  for i in 0...n\n    for j in 0...n\n      count += 1\n    end\n  end\n\n  count\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 2-10 \u5bf9\u6bd4\u4e86\u5e38\u6570\u9636\u3001\u7ebf\u6027\u9636\u548c\u5e73\u65b9\u9636\u4e09\u79cd\u65f6\u95f4\u590d\u6742\u5ea6\u3002

        \u56fe 2-10 \u00a0 \u5e38\u6570\u9636\u3001\u7ebf\u6027\u9636\u548c\u5e73\u65b9\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

        \u4ee5\u5192\u6ce1\u6392\u5e8f\u4e3a\u4f8b\uff0c\u5916\u5c42\u5faa\u73af\u6267\u884c \\(n - 1\\) \u6b21\uff0c\u5185\u5c42\u5faa\u73af\u6267\u884c \\(n-1\\)\u3001\\(n-2\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u6b21\uff0c\u5e73\u5747\u4e3a \\(n / 2\\) \u6b21\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O((n - 1) n / 2) = O(n^2)\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def bubble_sort(nums: list[int]) -> int:\n    \"\"\"\u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09\"\"\"\n    count = 0  # \u8ba1\u6570\u5668\n    # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i in range(len(nums) - 1, 0, -1):\n        # \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for j in range(i):\n            if nums[j] > nums[j + 1]:\n                # \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                tmp: int = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = tmp\n                count += 3  # \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n    return count\n
        time_complexity.cpp
        /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(vector<int> &nums) {\n    int count = 0; // \u8ba1\u6570\u5668\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = nums.size() - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n            }\n        }\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int[] nums) {\n    int count = 0; // \u8ba1\u6570\u5668\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = nums.length - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n            }\n        }\n    }\n    return count;\n}\n
        time_complexity.cs
        /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint BubbleSort(int[] nums) {\n    int count = 0;  // \u8ba1\u6570\u5668\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = nums.Length - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n                count += 3;  // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n            }\n        }\n    }\n    return count;\n}\n
        time_complexity.go
        /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunc bubbleSort(nums []int) int {\n    count := 0 // \u8ba1\u6570\u5668\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i := len(nums) - 1; i > 0; i-- {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for j := 0; j < i; j++ {\n            if nums[j] > nums[j+1] {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                tmp := nums[j]\n                nums[j] = nums[j+1]\n                nums[j+1] = tmp\n                count += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n            }\n        }\n    }\n    return count\n}\n
        time_complexity.swift
        /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunc bubbleSort(nums: inout [Int]) -> Int {\n    var count = 0 // \u8ba1\u6570\u5668\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i in nums.indices.dropFirst().reversed() {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for j in 0 ..< i {\n            if nums[j] > nums[j + 1] {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                let tmp = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = tmp\n                count += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n            }\n        }\n    }\n    return count\n}\n
        time_complexity.js
        /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunction bubbleSort(nums) {\n    let count = 0; // \u8ba1\u6570\u5668\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n            }\n        }\n    }\n    return count;\n}\n
        time_complexity.ts
        /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunction bubbleSort(nums: number[]): number {\n    let count = 0; // \u8ba1\u6570\u5668\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n            }\n        }\n    }\n    return count;\n}\n
        time_complexity.dart
        /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(List<int> nums) {\n  int count = 0; // \u8ba1\u6570\u5668\n  // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n  for (var i = nums.length - 1; i > 0; i--) {\n    // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n    for (var j = 0; j < i; j++) {\n      if (nums[j] > nums[j + 1]) {\n        // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n        int tmp = nums[j];\n        nums[j] = nums[j + 1];\n        nums[j + 1] = tmp;\n        count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n      }\n    }\n  }\n  return count;\n}\n
        time_complexity.rs
        /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfn bubble_sort(nums: &mut [i32]) -> i32 {\n    let mut count = 0; // \u8ba1\u6570\u5668\n\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i in (1..nums.len()).rev() {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for j in 0..i {\n            if nums[j] > nums[j + 1] {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n            }\n        }\n    }\n    count\n}\n
        time_complexity.c
        /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int *nums, int n) {\n    int count = 0; // \u8ba1\u6570\u5668\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = n - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n            }\n        }\n    }\n    return count;\n}\n
        time_complexity.kt
        /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfun bubbleSort(nums: IntArray): Int {\n    var count = 0 // \u8ba1\u6570\u5668\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (i in nums.size - 1 downTo 1) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (j in 0..<i) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                val temp = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = temp\n                count += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n            }\n        }\n    }\n    return count\n}\n
        time_complexity.rb
        ### \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09###\ndef bubble_sort(nums)\n  count = 0  # \u8ba1\u6570\u5668\n\n  # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n  for i in (nums.length - 1).downto(0)\n    # \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n    for j in 0...i\n      if nums[j] > nums[j + 1]\n        # \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n        tmp = nums[j]\n        nums[j] = nums[j + 1]\n        nums[j + 1] = tmp\n        count += 3 # \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n      end\n    end\n  end\n\n  count\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_computational_complexity/time_complexity/#4-o2n","title":"4. \u00a0 \u6307\u6570\u9636 \\(O(2^n)\\)","text":"

        \u751f\u7269\u5b66\u7684\u201c\u7ec6\u80de\u5206\u88c2\u201d\u662f\u6307\u6570\u9636\u589e\u957f\u7684\u5178\u578b\u4f8b\u5b50\uff1a\u521d\u59cb\u72b6\u6001\u4e3a \\(1\\) \u4e2a\u7ec6\u80de\uff0c\u5206\u88c2\u4e00\u8f6e\u540e\u53d8\u4e3a \\(2\\) \u4e2a\uff0c\u5206\u88c2\u4e24\u8f6e\u540e\u53d8\u4e3a \\(4\\) \u4e2a\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u5206\u88c2 \\(n\\) \u8f6e\u540e\u6709 \\(2^n\\) \u4e2a\u7ec6\u80de\u3002

        \u56fe 2-11 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6a21\u62df\u4e86\u7ec6\u80de\u5206\u88c2\u7684\u8fc7\u7a0b\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002\u8bf7\u6ce8\u610f\uff0c\u8f93\u5165 \\(n\\) \u8868\u793a\u5206\u88c2\u8f6e\u6570\uff0c\u8fd4\u56de\u503c count \u8868\u793a\u603b\u5206\u88c2\u6b21\u6570\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def exponential(n: int) -> int:\n    \"\"\"\u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\"\"\"\n    count = 0\n    base = 1\n    # \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n    for _ in range(n):\n        for _ in range(base):\n            count += 1\n        base *= 2\n    # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count\n
        time_complexity.cpp
        /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n    int count = 0, base = 1;\n    // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
        time_complexity.java
        /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n    int count = 0, base = 1;\n    // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
        time_complexity.cs
        /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint Exponential(int n) {\n    int count = 0, bas = 1;\n    // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < bas; j++) {\n            count++;\n        }\n        bas *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
        time_complexity.go
        /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09*/\nfunc exponential(n int) int {\n    count, base := 0, 1\n    // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n    for i := 0; i < n; i++ {\n        for j := 0; j < base; j++ {\n            count++\n        }\n        base *= 2\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count\n}\n
        time_complexity.swift
        /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunc exponential(n: Int) -> Int {\n    var count = 0\n    var base = 1\n    // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n    for _ in 0 ..< n {\n        for _ in 0 ..< base {\n            count += 1\n        }\n        base *= 2\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count\n}\n
        time_complexity.js
        /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction exponential(n) {\n    let count = 0,\n        base = 1;\n    // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n    for (let i = 0; i < n; i++) {\n        for (let j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
        time_complexity.ts
        /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction exponential(n: number): number {\n    let count = 0,\n        base = 1;\n    // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n    for (let i = 0; i < n; i++) {\n        for (let j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
        time_complexity.dart
        /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n  int count = 0, base = 1;\n  // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n  for (var i = 0; i < n; i++) {\n    for (var j = 0; j < base; j++) {\n      count++;\n    }\n    base *= 2;\n  }\n  // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n  return count;\n}\n
        time_complexity.rs
        /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfn exponential(n: i32) -> i32 {\n    let mut count = 0;\n    let mut base = 1;\n    // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n    for _ in 0..n {\n        for _ in 0..base {\n            count += 1\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    count\n}\n
        time_complexity.c
        /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n    int count = 0;\n    int bas = 1;\n    // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < bas; j++) {\n            count++;\n        }\n        bas *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
        time_complexity.kt
        /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfun exponential(n: Int): Int {\n    var count = 0\n    var base = 1\n    // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n    for (i in 0..<n) {\n        for (j in 0..<base) {\n            count++\n        }\n        base *= 2\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count\n}\n
        time_complexity.rb
        ### \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09###\ndef exponential(n)\n  count, base = 0, 1\n\n  # \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n  (0...n).each do\n    (0...base).each { count += 1 }\n    base *= 2\n  end\n\n  # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n  count\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 2-11 \u00a0 \u6307\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

        \u5728\u5b9e\u9645\u7b97\u6cd5\u4e2d\uff0c\u6307\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u9012\u5f52\u51fd\u6570\u4e2d\u3002\u4f8b\u5982\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u5176\u9012\u5f52\u5730\u4e00\u5206\u4e3a\u4e8c\uff0c\u7ecf\u8fc7 \\(n\\) \u6b21\u5206\u88c2\u540e\u505c\u6b62\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def exp_recur(n: int) -> int:\n    \"\"\"\u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n    if n == 1:\n        return 1\n    return exp_recur(n - 1) + exp_recur(n - 1) + 1\n
        time_complexity.cpp
        /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n    if (n == 1)\n        return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
        time_complexity.java
        /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n    if (n == 1)\n        return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
        time_complexity.cs
        /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint ExpRecur(int n) {\n    if (n == 1) return 1;\n    return ExpRecur(n - 1) + ExpRecur(n - 1) + 1;\n}\n
        time_complexity.go
        /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09*/\nfunc expRecur(n int) int {\n    if n == 1 {\n        return 1\n    }\n    return expRecur(n-1) + expRecur(n-1) + 1\n}\n
        time_complexity.swift
        /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc expRecur(n: Int) -> Int {\n    if n == 1 {\n        return 1\n    }\n    return expRecur(n: n - 1) + expRecur(n: n - 1) + 1\n}\n
        time_complexity.js
        /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction expRecur(n) {\n    if (n === 1) return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
        time_complexity.ts
        /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction expRecur(n: number): number {\n    if (n === 1) return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
        time_complexity.dart
        /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n  if (n == 1) return 1;\n  return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
        time_complexity.rs
        /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn exp_recur(n: i32) -> i32 {\n    if n == 1 {\n        return 1;\n    }\n    exp_recur(n - 1) + exp_recur(n - 1) + 1\n}\n
        time_complexity.c
        /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n    if (n == 1)\n        return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
        time_complexity.kt
        /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfun expRecur(n: Int): Int {\n    if (n == 1) {\n        return 1\n    }\n    return expRecur(n - 1) + expRecur(n - 1) + 1\n}\n
        time_complexity.rb
        ### \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09###\ndef exp_recur(n)\n  return 1 if n == 1\n  exp_recur(n - 1) + exp_recur(n - 1) + 1\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u6307\u6570\u9636\u589e\u957f\u975e\u5e38\u8fc5\u901f\uff0c\u5728\u7a77\u4e3e\u6cd5\uff08\u66b4\u529b\u641c\u7d22\u3001\u56de\u6eaf\u7b49\uff09\u4e2d\u6bd4\u8f83\u5e38\u89c1\u3002\u5bf9\u4e8e\u6570\u636e\u89c4\u6a21\u8f83\u5927\u7684\u95ee\u9898\uff0c\u6307\u6570\u9636\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u6216\u8d2a\u5fc3\u7b97\u6cd5\u7b49\u6765\u89e3\u51b3\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#5-olog-n","title":"5. \u00a0 \u5bf9\u6570\u9636 \\(O(\\log n)\\)","text":"

        \u4e0e\u6307\u6570\u9636\u76f8\u53cd\uff0c\u5bf9\u6570\u9636\u53cd\u6620\u4e86\u201c\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\u201d\u7684\u60c5\u51b5\u3002\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u7531\u4e8e\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\uff0c\u56e0\u6b64\u5faa\u73af\u6b21\u6570\u662f \\(\\log_2 n\\) \uff0c\u5373 \\(2^n\\) \u7684\u53cd\u51fd\u6570\u3002

        \u56fe 2-12 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6a21\u62df\u4e86\u201c\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\u201d\u7684\u8fc7\u7a0b\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log_2 n)\\) \uff0c\u7b80\u8bb0\u4e3a \\(O(\\log n)\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def logarithmic(n: int) -> int:\n    \"\"\"\u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\"\"\"\n    count = 0\n    while n > 1:\n        n = n / 2\n        count += 1\n    return count\n
        time_complexity.cpp
        /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
        time_complexity.cs
        /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint Logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n /= 2;\n        count++;\n    }\n    return count;\n}\n
        time_complexity.go
        /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09*/\nfunc logarithmic(n int) int {\n    count := 0\n    for n > 1 {\n        n = n / 2\n        count++\n    }\n    return count\n}\n
        time_complexity.swift
        /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunc logarithmic(n: Int) -> Int {\n    var count = 0\n    var n = n\n    while n > 1 {\n        n = n / 2\n        count += 1\n    }\n    return count\n}\n
        time_complexity.js
        /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction logarithmic(n) {\n    let count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
        time_complexity.ts
        /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction logarithmic(n: number): number {\n    let count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
        time_complexity.dart
        /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(int n) {\n  int count = 0;\n  while (n > 1) {\n    n = n ~/ 2;\n    count++;\n  }\n  return count;\n}\n
        time_complexity.rs
        /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfn logarithmic(mut n: i32) -> i32 {\n    let mut count = 0;\n    while n > 1 {\n        n = n / 2;\n        count += 1;\n    }\n    count\n}\n
        time_complexity.c
        /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
        time_complexity.kt
        /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfun logarithmic(n: Int): Int {\n    var n1 = n\n    var count = 0\n    while (n1 > 1) {\n        n1 /= 2\n        count++\n    }\n    return count\n}\n
        time_complexity.rb
        ### \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09###\ndef logarithmic(n)\n  count = 0\n\n  while n > 1\n    n /= 2\n    count += 1\n  end\n\n  count\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 2-12 \u00a0 \u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

        \u4e0e\u6307\u6570\u9636\u7c7b\u4f3c\uff0c\u5bf9\u6570\u9636\u4e5f\u5e38\u51fa\u73b0\u4e8e\u9012\u5f52\u51fd\u6570\u4e2d\u3002\u4ee5\u4e0b\u4ee3\u7801\u5f62\u6210\u4e86\u4e00\u68f5\u9ad8\u5ea6\u4e3a \\(\\log_2 n\\) \u7684\u9012\u5f52\u6811\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def log_recur(n: int) -> int:\n    \"\"\"\u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n    if n <= 1:\n        return 0\n    return log_recur(n / 2) + 1\n
        time_complexity.cpp
        /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(int n) {\n    if (n <= 1)\n        return 0;\n    return logRecur(n / 2) + 1;\n}\n
        time_complexity.java
        /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(int n) {\n    if (n <= 1)\n        return 0;\n    return logRecur(n / 2) + 1;\n}\n
        time_complexity.cs
        /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint LogRecur(int n) {\n    if (n <= 1) return 0;\n    return LogRecur(n / 2) + 1;\n}\n
        time_complexity.go
        /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09*/\nfunc logRecur(n int) int {\n    if n <= 1 {\n        return 0\n    }\n    return logRecur(n/2) + 1\n}\n
        time_complexity.swift
        /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc logRecur(n: Int) -> Int {\n    if n <= 1 {\n        return 0\n    }\n    return logRecur(n: n / 2) + 1\n}\n
        time_complexity.js
        /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction logRecur(n) {\n    if (n <= 1) return 0;\n    return logRecur(n / 2) + 1;\n}\n
        time_complexity.ts
        /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction logRecur(n: number): number {\n    if (n <= 1) return 0;\n    return logRecur(n / 2) + 1;\n}\n
        time_complexity.dart
        /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(int n) {\n  if (n <= 1) return 0;\n  return logRecur(n ~/ 2) + 1;\n}\n
        time_complexity.rs
        /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn log_recur(n: i32) -> i32 {\n    if n <= 1 {\n        return 0;\n    }\n    log_recur(n / 2) + 1\n}\n
        time_complexity.c
        /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(int n) {\n    if (n <= 1)\n        return 0;\n    return logRecur(n / 2) + 1;\n}\n
        time_complexity.kt
        /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfun logRecur(n: Int): Int {\n    if (n <= 1)\n        return 0\n    return logRecur(n / 2) + 1\n}\n
        time_complexity.rb
        ### \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09###\ndef log_recur(n)\n  return 0 unless n > 1\n  log_recur(n / 2) + 1\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5bf9\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u7b97\u6cd5\u4e2d\uff0c\u4f53\u73b0\u4e86\u201c\u4e00\u5206\u4e3a\u591a\u201d\u548c\u201c\u5316\u7e41\u4e3a\u7b80\u201d\u7684\u7b97\u6cd5\u601d\u60f3\u3002\u5b83\u589e\u957f\u7f13\u6162\uff0c\u662f\u4ec5\u6b21\u4e8e\u5e38\u6570\u9636\u7684\u7406\u60f3\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002

        \\(O(\\log n)\\) \u7684\u5e95\u6570\u662f\u591a\u5c11\uff1f

        \u51c6\u786e\u6765\u8bf4\uff0c\u201c\u4e00\u5206\u4e3a \\(m\\)\u201d\u5bf9\u5e94\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(\\log_m n)\\) \u3002\u800c\u901a\u8fc7\u5bf9\u6570\u6362\u5e95\u516c\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u5177\u6709\u4e0d\u540c\u5e95\u6570\u3001\u76f8\u7b49\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a

        \\[ O(\\log_m n) = O(\\log_k n / \\log_k m) = O(\\log_k n) \\]

        \u4e5f\u5c31\u662f\u8bf4\uff0c\u5e95\u6570 \\(m\\) \u53ef\u4ee5\u5728\u4e0d\u5f71\u54cd\u590d\u6742\u5ea6\u7684\u524d\u63d0\u4e0b\u8f6c\u6362\u3002\u56e0\u6b64\u6211\u4eec\u901a\u5e38\u4f1a\u7701\u7565\u5e95\u6570 \\(m\\) \uff0c\u5c06\u5bf9\u6570\u9636\u76f4\u63a5\u8bb0\u4e3a \\(O(\\log n)\\) \u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#6-on-log-n","title":"6. \u00a0 \u7ebf\u6027\u5bf9\u6570\u9636 \\(O(n \\log n)\\)","text":"

        \u7ebf\u6027\u5bf9\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u5d4c\u5957\u5faa\u73af\u4e2d\uff0c\u4e24\u5c42\u5faa\u73af\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u522b\u4e3a \\(O(\\log n)\\) \u548c \\(O(n)\\) \u3002\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def linear_log_recur(n: int) -> int:\n    \"\"\"\u7ebf\u6027\u5bf9\u6570\u9636\"\"\"\n    if n <= 1:\n        return 1\n    # \u4e00\u5206\u4e3a\u4e8c\uff0c\u5b50\u95ee\u9898\u7684\u89c4\u6a21\u51cf\u5c0f\u4e00\u534a\n    count = linear_log_recur(n // 2) + linear_log_recur(n // 2)\n    # \u5f53\u524d\u5b50\u95ee\u9898\u5305\u542b n \u4e2a\u64cd\u4f5c\n    for _ in range(n):\n        count += 1\n    return count\n
        time_complexity.cpp
        /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(int n) {\n    if (n <= 1)\n        return 1;\n    int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(int n) {\n    if (n <= 1)\n        return 1;\n    int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.cs
        /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint LinearLogRecur(int n) {\n    if (n <= 1) return 1;\n    int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.go
        /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunc linearLogRecur(n int) int {\n    if n <= 1 {\n        return 1\n    }\n    count := linearLogRecur(n/2) + linearLogRecur(n/2)\n    for i := 0; i < n; i++ {\n        count++\n    }\n    return count\n}\n
        time_complexity.swift
        /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunc linearLogRecur(n: Int) -> Int {\n    if n <= 1 {\n        return 1\n    }\n    var count = linearLogRecur(n: n / 2) + linearLogRecur(n: n / 2)\n    for _ in stride(from: 0, to: n, by: 1) {\n        count += 1\n    }\n    return count\n}\n
        time_complexity.js
        /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunction linearLogRecur(n) {\n    if (n <= 1) return 1;\n    let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (let i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.ts
        /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunction linearLogRecur(n: number): number {\n    if (n <= 1) return 1;\n    let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (let i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.dart
        /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(int n) {\n  if (n <= 1) return 1;\n  int count = linearLogRecur(n ~/ 2) + linearLogRecur(n ~/ 2);\n  for (var i = 0; i < n; i++) {\n    count++;\n  }\n  return count;\n}\n
        time_complexity.rs
        /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfn linear_log_recur(n: i32) -> i32 {\n    if n <= 1 {\n        return 1;\n    }\n    let mut count = linear_log_recur(n / 2) + linear_log_recur(n / 2);\n    for _ in 0..n {\n        count += 1;\n    }\n    return count;\n}\n
        time_complexity.c
        /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(int n) {\n    if (n <= 1)\n        return 1;\n    int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
        time_complexity.kt
        /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfun linearLogRecur(n: Int): Int {\n    if (n <= 1)\n        return 1\n    var count = linearLogRecur(n / 2) + linearLogRecur(n / 2)\n    for (i in 0..<n) {\n        count++\n    }\n    return count\n}\n
        time_complexity.rb
        ### \u7ebf\u6027\u5bf9\u6570\u9636 ###\ndef linear_log_recur(n)\n  return 1 unless n > 1\n\n  count = linear_log_recur(n / 2) + linear_log_recur(n / 2)\n  (0...n).each { count += 1 }\n\n  count\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 2-13 \u5c55\u793a\u4e86\u7ebf\u6027\u5bf9\u6570\u9636\u7684\u751f\u6210\u65b9\u5f0f\u3002\u4e8c\u53c9\u6811\u7684\u6bcf\u4e00\u5c42\u7684\u64cd\u4f5c\u603b\u6570\u90fd\u4e3a \\(n\\) \uff0c\u6811\u5171\u6709 \\(\\log_2 n + 1\\) \u5c42\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002

        \u56fe 2-13 \u00a0 \u7ebf\u6027\u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

        \u4e3b\u6d41\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3a \\(O(n \\log n)\\) \uff0c\u4f8b\u5982\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u3001\u5806\u6392\u5e8f\u7b49\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#7-on","title":"7. \u00a0 \u9636\u4e58\u9636 \\(O(n!)\\)","text":"

        \u9636\u4e58\u9636\u5bf9\u5e94\u6570\u5b66\u4e0a\u7684\u201c\u5168\u6392\u5217\u201d\u95ee\u9898\u3002\u7ed9\u5b9a \\(n\\) \u4e2a\u4e92\u4e0d\u91cd\u590d\u7684\u5143\u7d20\uff0c\u6c42\u5176\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u65b9\u6848\uff0c\u65b9\u6848\u6570\u91cf\u4e3a\uff1a

        \\[ n! = n \\times (n - 1) \\times (n - 2) \\times \\dots \\times 2 \\times 1 \\]

        \u9636\u4e58\u901a\u5e38\u4f7f\u7528\u9012\u5f52\u5b9e\u73b0\u3002\u5982\u56fe 2-14 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u7b2c\u4e00\u5c42\u5206\u88c2\u51fa \\(n\\) \u4e2a\uff0c\u7b2c\u4e8c\u5c42\u5206\u88c2\u51fa \\(n - 1\\) \u4e2a\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u7b2c \\(n\\) \u5c42\u65f6\u505c\u6b62\u5206\u88c2\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
        def factorial_recur(n: int) -> int:\n    \"\"\"\u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n    if n == 0:\n        return 1\n    count = 0\n    # \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n    for _ in range(n):\n        count += factorial_recur(n - 1)\n    return count\n
        time_complexity.cpp
        /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n    if (n == 0)\n        return 1;\n    int count = 0;\n    // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n    for (int i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
        time_complexity.java
        /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n    if (n == 0)\n        return 1;\n    int count = 0;\n    // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n    for (int i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
        time_complexity.cs
        /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint FactorialRecur(int n) {\n    if (n == 0) return 1;\n    int count = 0;\n    // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n    for (int i = 0; i < n; i++) {\n        count += FactorialRecur(n - 1);\n    }\n    return count;\n}\n
        time_complexity.go
        /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc factorialRecur(n int) int {\n    if n == 0 {\n        return 1\n    }\n    count := 0\n    // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n    for i := 0; i < n; i++ {\n        count += factorialRecur(n - 1)\n    }\n    return count\n}\n
        time_complexity.swift
        /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc factorialRecur(n: Int) -> Int {\n    if n == 0 {\n        return 1\n    }\n    var count = 0\n    // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n    for _ in 0 ..< n {\n        count += factorialRecur(n: n - 1)\n    }\n    return count\n}\n
        time_complexity.js
        /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction factorialRecur(n) {\n    if (n === 0) return 1;\n    let count = 0;\n    // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n    for (let i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
        time_complexity.ts
        /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction factorialRecur(n: number): number {\n    if (n === 0) return 1;\n    let count = 0;\n    // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n    for (let i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
        time_complexity.dart
        /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n  if (n == 0) return 1;\n  int count = 0;\n  // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n  for (var i = 0; i < n; i++) {\n    count += factorialRecur(n - 1);\n  }\n  return count;\n}\n
        time_complexity.rs
        /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn factorial_recur(n: i32) -> i32 {\n    if n == 0 {\n        return 1;\n    }\n    let mut count = 0;\n    // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n    for _ in 0..n {\n        count += factorial_recur(n - 1);\n    }\n    count\n}\n
        time_complexity.c
        /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n    if (n == 0)\n        return 1;\n    int count = 0;\n    for (int i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
        time_complexity.kt
        /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfun factorialRecur(n: Int): Int {\n    if (n == 0)\n        return 1\n    var count = 0\n    // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n    for (i in 0..<n) {\n        count += factorialRecur(n - 1)\n    }\n    return count\n}\n
        time_complexity.rb
        ### \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09###\ndef factorial_recur(n)\n  return 1 if n == 0\n\n  count = 0\n  # \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n  (0...n).each { count += factorial_recur(n - 1) }\n\n  count\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 2-14 \u00a0 \u9636\u4e58\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

        \u8bf7\u6ce8\u610f\uff0c\u56e0\u4e3a\u5f53 \\(n \\geq 4\\) \u65f6\u6052\u6709 \\(n! > 2^n\\) \uff0c\u6240\u4ee5\u9636\u4e58\u9636\u6bd4\u6307\u6570\u9636\u589e\u957f\u5f97\u66f4\u5feb\uff0c\u5728 \\(n\\) \u8f83\u5927\u65f6\u4e5f\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\u3002

        "},{"location":"chapter_computational_complexity/time_complexity/#235","title":"2.3.5 \u00a0 \u6700\u5dee\u3001\u6700\u4f73\u3001\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6","text":"

        \u7b97\u6cd5\u7684\u65f6\u95f4\u6548\u7387\u5f80\u5f80\u4e0d\u662f\u56fa\u5b9a\u7684\uff0c\u800c\u662f\u4e0e\u8f93\u5165\u6570\u636e\u7684\u5206\u5e03\u6709\u5173\u3002\u5047\u8bbe\u8f93\u5165\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4 nums \uff0c\u5176\u4e2d nums \u7531\u4ece \\(1\\) \u81f3 \\(n\\) \u7684\u6570\u5b57\u7ec4\u6210\uff0c\u6bcf\u4e2a\u6570\u5b57\u53ea\u51fa\u73b0\u4e00\u6b21\uff1b\u4f46\u5143\u7d20\u987a\u5e8f\u662f\u968f\u673a\u6253\u4e71\u7684\uff0c\u4efb\u52a1\u76ee\u6807\u662f\u8fd4\u56de\u5143\u7d20 \\(1\\) \u7684\u7d22\u5f15\u3002\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\u3002

        • \u5f53 nums = [?, ?, ..., 1] \uff0c\u5373\u5f53\u672b\u5c3e\u5143\u7d20\u662f \\(1\\) \u65f6\uff0c\u9700\u8981\u5b8c\u6574\u904d\u5386\u6570\u7ec4\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3002
        • \u5f53 nums = [1, ?, ?, ...] \uff0c\u5373\u5f53\u9996\u4e2a\u5143\u7d20\u4e3a \\(1\\) \u65f6\uff0c\u65e0\u8bba\u6570\u7ec4\u591a\u957f\u90fd\u4e0d\u9700\u8981\u7ee7\u7eed\u904d\u5386\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 \\(\\Omega(1)\\) \u3002

        \u201c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u201d\u5bf9\u5e94\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c\uff0c\u4f7f\u7528\u5927 \\(O\\) \u8bb0\u53f7\u8868\u793a\u3002\u76f8\u5e94\u5730\uff0c\u201c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u201d\u5bf9\u5e94\u51fd\u6570\u6e10\u8fd1\u4e0b\u754c\uff0c\u7528 \\(\\Omega\\) \u8bb0\u53f7\u8868\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby worst_best_time_complexity.py
        def random_numbers(n: int) -> list[int]:\n    \"\"\"\u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a: 1, 2, ..., n \uff0c\u987a\u5e8f\u88ab\u6253\u4e71\"\"\"\n    # \u751f\u6210\u6570\u7ec4 nums =: 1, 2, 3, ..., n\n    nums = [i for i in range(1, n + 1)]\n    # \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n    random.shuffle(nums)\n    return nums\n\ndef find_one(nums: list[int]) -> int:\n    \"\"\"\u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15\"\"\"\n    for i in range(len(nums)):\n        # \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n        # \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n        if nums[i] == 1:\n            return i\n    return -1\n
        worst_best_time_complexity.cpp
        /* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nvector<int> randomNumbers(int n) {\n    vector<int> nums(n);\n    // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // \u4f7f\u7528\u7cfb\u7edf\u65f6\u95f4\u751f\u6210\u968f\u673a\u79cd\u5b50\n    unsigned seed = chrono::system_clock::now().time_since_epoch().count();\n    // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n    shuffle(nums.begin(), nums.end(), default_random_engine(seed));\n    return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(vector<int> &nums) {\n    for (int i = 0; i < nums.size(); i++) {\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
        worst_best_time_complexity.java
        /* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint[] randomNumbers(int n) {\n    Integer[] nums = new Integer[n];\n    // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n    Collections.shuffle(Arrays.asList(nums));\n    // Integer[] -> int[]\n    int[] res = new int[n];\n    for (int i = 0; i < n; i++) {\n        res[i] = nums[i];\n    }\n    return res;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int[] nums) {\n    for (int i = 0; i < nums.length; i++) {\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
        worst_best_time_complexity.cs
        /* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint[] RandomNumbers(int n) {\n    int[] nums = new int[n];\n    // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n\n    // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n    for (int i = 0; i < nums.Length; i++) {\n        int index = new Random().Next(i, nums.Length);\n        (nums[i], nums[index]) = (nums[index], nums[i]);\n    }\n    return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint FindOne(int[] nums) {\n    for (int i = 0; i < nums.Length; i++) {\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
        worst_best_time_complexity.go
        /* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunc randomNumbers(n int) []int {\n    nums := make([]int, n)\n    // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n    for i := 0; i < n; i++ {\n        nums[i] = i + 1\n    }\n    // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n    rand.Shuffle(len(nums), func(i, j int) {\n        nums[i], nums[j] = nums[j], nums[i]\n    })\n    return nums\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunc findOne(nums []int) int {\n    for i := 0; i < len(nums); i++ {\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n        if nums[i] == 1 {\n            return i\n        }\n    }\n    return -1\n}\n
        worst_best_time_complexity.swift
        /* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunc randomNumbers(n: Int) -> [Int] {\n    // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n    var nums = Array(1 ... n)\n    // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n    nums.shuffle()\n    return nums\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunc findOne(nums: [Int]) -> Int {\n    for i in nums.indices {\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n        if nums[i] == 1 {\n            return i\n        }\n    }\n    return -1\n}\n
        worst_best_time_complexity.js
        /* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunction randomNumbers(n) {\n    const nums = Array(n);\n    // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n    for (let i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n    for (let i = 0; i < n; i++) {\n        const r = Math.floor(Math.random() * (i + 1));\n        const temp = nums[i];\n        nums[i] = nums[r];\n        nums[r] = temp;\n    }\n    return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunction findOne(nums) {\n    for (let i = 0; i < nums.length; i++) {\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n        if (nums[i] === 1) {\n            return i;\n        }\n    }\n    return -1;\n}\n
        worst_best_time_complexity.ts
        /* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunction randomNumbers(n: number): number[] {\n    const nums = Array(n);\n    // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n    for (let i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n    for (let i = 0; i < n; i++) {\n        const r = Math.floor(Math.random() * (i + 1));\n        const temp = nums[i];\n        nums[i] = nums[r];\n        nums[r] = temp;\n    }\n    return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunction findOne(nums: number[]): number {\n    for (let i = 0; i < nums.length; i++) {\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n        if (nums[i] === 1) {\n            return i;\n        }\n    }\n    return -1;\n}\n
        worst_best_time_complexity.dart
        /* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nList<int> randomNumbers(int n) {\n  final nums = List.filled(n, 0);\n  // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n  for (var i = 0; i < n; i++) {\n    nums[i] = i + 1;\n  }\n  // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n  nums.shuffle();\n\n  return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(List<int> nums) {\n  for (var i = 0; i < nums.length; i++) {\n    // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n    // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n    if (nums[i] == 1) return i;\n  }\n\n  return -1;\n}\n
        worst_best_time_complexity.rs
        /* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfn random_numbers(n: i32) -> Vec<i32> {\n    // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n    let mut nums = (1..=n).collect::<Vec<i32>>();\n    // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n    nums.shuffle(&mut thread_rng());\n    nums\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfn find_one(nums: &[i32]) -> Option<usize> {\n    for i in 0..nums.len() {\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n        if nums[i] == 1 {\n            return Some(i);\n        }\n    }\n    None\n}\n
        worst_best_time_complexity.c
        /* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint *randomNumbers(int n) {\n    // \u5206\u914d\u5806\u533a\u5185\u5b58\uff08\u521b\u5efa\u4e00\u7ef4\u53ef\u53d8\u957f\u6570\u7ec4\uff1a\u6570\u7ec4\u4e2d\u5143\u7d20\u6570\u91cf\u4e3a n \uff0c\u5143\u7d20\u7c7b\u578b\u4e3a int \uff09\n    int *nums = (int *)malloc(n * sizeof(int));\n    // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n    for (int i = n - 1; i > 0; i--) {\n        int j = rand() % (i + 1);\n        int temp = nums[i];\n        nums[i] = nums[j];\n        nums[j] = temp;\n    }\n    return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int *nums, int n) {\n    for (int i = 0; i < n; i++) {\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
        worst_best_time_complexity.kt
        /* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfun randomNumbers(n: Int): Array<Int?> {\n    val nums = IntArray(n)\n    // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n    for (i in 0..<n) {\n        nums[i] = i + 1\n    }\n    // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n    nums.shuffle()\n    val res = arrayOfNulls<Int>(n)\n    for (i in 0..<n) {\n        res[i] = nums[i]\n    }\n    return res\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfun findOne(nums: Array<Int?>): Int {\n    for (i in nums.indices) {\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n        // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n        if (nums[i] == 1)\n            return i\n    }\n    return -1\n}\n
        worst_best_time_complexity.rb
        ### \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a: 1, 2, ..., n \uff0c\u987a\u5e8f\u88ab\u6253\u4e71 ###\ndef random_numbers(n)\n  # \u751f\u6210\u6570\u7ec4 nums =: 1, 2, 3, ..., n\n  nums = Array.new(n) { |i| i + 1 }\n  # \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n  nums.shuffle!\nend\n\n### \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 ###\ndef find_one(nums)\n  for i in 0...nums.length\n    # \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n    # \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n    return i if nums[i] == 1\n  end\n\n  -1\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6211\u4eec\u5728\u5b9e\u9645\u4e2d\u5f88\u5c11\u4f7f\u7528\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u56e0\u4e3a\u901a\u5e38\u53ea\u6709\u5728\u5f88\u5c0f\u6982\u7387\u4e0b\u624d\u80fd\u8fbe\u5230\uff0c\u53ef\u80fd\u4f1a\u5e26\u6765\u4e00\u5b9a\u7684\u8bef\u5bfc\u6027\u3002\u800c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u66f4\u4e3a\u5b9e\u7528\uff0c\u56e0\u4e3a\u5b83\u7ed9\u51fa\u4e86\u4e00\u4e2a\u6548\u7387\u5b89\u5168\u503c\uff0c\u8ba9\u6211\u4eec\u53ef\u4ee5\u653e\u5fc3\u5730\u4f7f\u7528\u7b97\u6cd5\u3002

        \u4ece\u4e0a\u8ff0\u793a\u4f8b\u53ef\u4ee5\u770b\u51fa\uff0c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u53ea\u51fa\u73b0\u4e8e\u201c\u7279\u6b8a\u7684\u6570\u636e\u5206\u5e03\u201d\uff0c\u8fd9\u4e9b\u60c5\u51b5\u7684\u51fa\u73b0\u6982\u7387\u53ef\u80fd\u5f88\u5c0f\uff0c\u5e76\u4e0d\u80fd\u771f\u5b9e\u5730\u53cd\u6620\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f53\u73b0\u7b97\u6cd5\u5728\u968f\u673a\u8f93\u5165\u6570\u636e\u4e0b\u7684\u8fd0\u884c\u6548\u7387\uff0c\u7528 \\(\\Theta\\) \u8bb0\u53f7\u6765\u8868\u793a\u3002

        \u5bf9\u4e8e\u90e8\u5206\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u63a8\u7b97\u51fa\u968f\u673a\u6570\u636e\u5206\u5e03\u4e0b\u7684\u5e73\u5747\u60c5\u51b5\u3002\u6bd4\u5982\u4e0a\u8ff0\u793a\u4f8b\uff0c\u7531\u4e8e\u8f93\u5165\u6570\u7ec4\u662f\u88ab\u6253\u4e71\u7684\uff0c\u56e0\u6b64\u5143\u7d20 \\(1\\) \u51fa\u73b0\u5728\u4efb\u610f\u7d22\u5f15\u7684\u6982\u7387\u90fd\u662f\u76f8\u7b49\u7684\uff0c\u90a3\u4e48\u7b97\u6cd5\u7684\u5e73\u5747\u5faa\u73af\u6b21\u6570\u5c31\u662f\u6570\u7ec4\u957f\u5ea6\u7684\u4e00\u534a \\(n / 2\\) \uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(\\Theta(n / 2) = \\Theta(n)\\) \u3002

        \u4f46\u5bf9\u4e8e\u8f83\u4e3a\u590d\u6742\u7684\u7b97\u6cd5\uff0c\u8ba1\u7b97\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u5f80\u5f80\u6bd4\u8f83\u56f0\u96be\uff0c\u56e0\u4e3a\u5f88\u96be\u5206\u6790\u51fa\u5728\u6570\u636e\u5206\u5e03\u4e0b\u7684\u6574\u4f53\u6570\u5b66\u671f\u671b\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4f5c\u4e3a\u7b97\u6cd5\u6548\u7387\u7684\u8bc4\u5224\u6807\u51c6\u3002

        \u4e3a\u4ec0\u4e48\u5f88\u5c11\u770b\u5230 \\(\\Theta\\) \u7b26\u53f7\uff1f

        \u53ef\u80fd\u7531\u4e8e \\(O\\) \u7b26\u53f7\u8fc7\u4e8e\u6717\u6717\u4e0a\u53e3\uff0c\u56e0\u6b64\u6211\u4eec\u5e38\u5e38\u4f7f\u7528\u5b83\u6765\u8868\u793a\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u4f46\u4ece\u4e25\u683c\u610f\u4e49\u4e0a\u8bb2\uff0c\u8fd9\u79cd\u505a\u6cd5\u5e76\u4e0d\u89c4\u8303\u3002\u5728\u672c\u4e66\u548c\u5176\u4ed6\u8d44\u6599\u4e2d\uff0c\u82e5\u9047\u5230\u7c7b\u4f3c\u201c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\)\u201d\u7684\u8868\u8ff0\uff0c\u8bf7\u5c06\u5176\u76f4\u63a5\u7406\u89e3\u4e3a \\(\\Theta(n)\\) \u3002

        "},{"location":"chapter_data_structure/","title":"\u7b2c 3 \u7ae0 \u00a0 \u6570\u636e\u7ed3\u6784","text":"

        Abstract

        \u6570\u636e\u7ed3\u6784\u5982\u540c\u4e00\u526f\u7a33\u56fa\u800c\u591a\u6837\u7684\u6846\u67b6\u3002

        \u5b83\u4e3a\u6570\u636e\u7684\u6709\u5e8f\u7ec4\u7ec7\u63d0\u4f9b\u4e86\u84dd\u56fe\uff0c\u7b97\u6cd5\u5f97\u4ee5\u5728\u6b64\u57fa\u7840\u4e0a\u751f\u52a8\u8d77\u6765\u3002

        "},{"location":"chapter_data_structure/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 3.1 \u00a0 \u6570\u636e\u7ed3\u6784\u5206\u7c7b
        • 3.2 \u00a0 \u57fa\u672c\u6570\u636e\u7c7b\u578b
        • 3.3 \u00a0 \u6570\u5b57\u7f16\u7801 *
        • 3.4 \u00a0 \u5b57\u7b26\u7f16\u7801 *
        • 3.5 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_data_structure/basic_data_types/","title":"3.2 \u00a0 \u57fa\u672c\u6570\u636e\u7c7b\u578b","text":"

        \u5f53\u8c08\u53ca\u8ba1\u7b97\u673a\u4e2d\u7684\u6570\u636e\u65f6\uff0c\u6211\u4eec\u4f1a\u60f3\u5230\u6587\u672c\u3001\u56fe\u7247\u3001\u89c6\u9891\u3001\u8bed\u97f3\u30013D \u6a21\u578b\u7b49\u5404\u79cd\u5f62\u5f0f\u3002\u5c3d\u7ba1\u8fd9\u4e9b\u6570\u636e\u7684\u7ec4\u7ec7\u5f62\u5f0f\u5404\u5f02\uff0c\u4f46\u5b83\u4eec\u90fd\u7531\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6784\u6210\u3002

        \u57fa\u672c\u6570\u636e\u7c7b\u578b\u662f CPU \u53ef\u4ee5\u76f4\u63a5\u8fdb\u884c\u8fd0\u7b97\u7684\u7c7b\u578b\uff0c\u5728\u7b97\u6cd5\u4e2d\u76f4\u63a5\u88ab\u4f7f\u7528\uff0c\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\u3002

        • \u6574\u6570\u7c7b\u578b byte\u3001short\u3001int\u3001long \u3002
        • \u6d6e\u70b9\u6570\u7c7b\u578b float\u3001double \uff0c\u7528\u4e8e\u8868\u793a\u5c0f\u6570\u3002
        • \u5b57\u7b26\u7c7b\u578b char \uff0c\u7528\u4e8e\u8868\u793a\u5404\u79cd\u8bed\u8a00\u7684\u5b57\u6bcd\u3001\u6807\u70b9\u7b26\u53f7\u751a\u81f3\u8868\u60c5\u7b26\u53f7\u7b49\u3002
        • \u5e03\u5c14\u7c7b\u578b bool \uff0c\u7528\u4e8e\u8868\u793a\u201c\u662f\u201d\u4e0e\u201c\u5426\u201d\u5224\u65ad\u3002

        \u57fa\u672c\u6570\u636e\u7c7b\u578b\u4ee5\u4e8c\u8fdb\u5236\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u4e2d\u3002\u4e00\u4e2a\u4e8c\u8fdb\u5236\u4f4d\u5373\u4e3a \\(1\\) \u6bd4\u7279\u3002\u5728\u7edd\u5927\u591a\u6570\u73b0\u4ee3\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\\(1\\) \u5b57\u8282\uff08byte\uff09\u7531 \\(8\\) \u6bd4\u7279\uff08bit\uff09\u7ec4\u6210\u3002

        \u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u53d6\u503c\u8303\u56f4\u53d6\u51b3\u4e8e\u5176\u5360\u7528\u7684\u7a7a\u95f4\u5927\u5c0f\u3002\u4e0b\u9762\u4ee5 Java \u4e3a\u4f8b\u3002

        • \u6574\u6570\u7c7b\u578b byte \u5360\u7528 \\(1\\) \u5b57\u8282 = \\(8\\) \u6bd4\u7279 \uff0c\u53ef\u4ee5\u8868\u793a \\(2^{8}\\) \u4e2a\u6570\u5b57\u3002
        • \u6574\u6570\u7c7b\u578b int \u5360\u7528 \\(4\\) \u5b57\u8282 = \\(32\\) \u6bd4\u7279 \uff0c\u53ef\u4ee5\u8868\u793a \\(2^{32}\\) \u4e2a\u6570\u5b57\u3002

        \u8868 3-1 \u5217\u4e3e\u4e86 Java \u4e2d\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u5360\u7528\u7a7a\u95f4\u3001\u53d6\u503c\u8303\u56f4\u548c\u9ed8\u8ba4\u503c\u3002\u6b64\u8868\u683c\u65e0\u987b\u6b7b\u8bb0\u786c\u80cc\uff0c\u5927\u81f4\u7406\u89e3\u5373\u53ef\uff0c\u9700\u8981\u65f6\u53ef\u4ee5\u901a\u8fc7\u67e5\u8868\u6765\u56de\u5fc6\u3002

        \u8868 3-1 \u00a0 \u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u5360\u7528\u7a7a\u95f4\u548c\u53d6\u503c\u8303\u56f4

        \u7c7b\u578b \u7b26\u53f7 \u5360\u7528\u7a7a\u95f4 \u6700\u5c0f\u503c \u6700\u5927\u503c \u9ed8\u8ba4\u503c \u6574\u6570 byte 1 \u5b57\u8282 \\(-2^7\\) (\\(-128\\)) \\(2^7 - 1\\) (\\(127\\)) \\(0\\) short 2 \u5b57\u8282 \\(-2^{15}\\) \\(2^{15} - 1\\) \\(0\\) int 4 \u5b57\u8282 \\(-2^{31}\\) \\(2^{31} - 1\\) \\(0\\) long 8 \u5b57\u8282 \\(-2^{63}\\) \\(2^{63} - 1\\) \\(0\\) \u6d6e\u70b9\u6570 float 4 \u5b57\u8282 \\(1.175 \\times 10^{-38}\\) \\(3.403 \\times 10^{38}\\) \\(0.0\\text{f}\\) double 8 \u5b57\u8282 \\(2.225 \\times 10^{-308}\\) \\(1.798 \\times 10^{308}\\) \\(0.0\\) \u5b57\u7b26 char 2 \u5b57\u8282 \\(0\\) \\(2^{16} - 1\\) \\(0\\) \u5e03\u5c14 bool 1 \u5b57\u8282 \\(\\text{false}\\) \\(\\text{true}\\) \\(\\text{false}\\)

        \u8bf7\u6ce8\u610f\uff0c\u8868 3-1 \u9488\u5bf9\u7684\u662f Java \u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u60c5\u51b5\u3002\u6bcf\u79cd\u7f16\u7a0b\u8bed\u8a00\u90fd\u6709\u5404\u81ea\u7684\u6570\u636e\u7c7b\u578b\u5b9a\u4e49\uff0c\u5b83\u4eec\u7684\u5360\u7528\u7a7a\u95f4\u3001\u53d6\u503c\u8303\u56f4\u548c\u9ed8\u8ba4\u503c\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002

        • \u5728 Python \u4e2d\uff0c\u6574\u6570\u7c7b\u578b int \u53ef\u4ee5\u662f\u4efb\u610f\u5927\u5c0f\uff0c\u53ea\u53d7\u9650\u4e8e\u53ef\u7528\u5185\u5b58\uff1b\u6d6e\u70b9\u6570 float \u662f\u53cc\u7cbe\u5ea6 64 \u4f4d\uff1b\u6ca1\u6709 char \u7c7b\u578b\uff0c\u5355\u4e2a\u5b57\u7b26\u5b9e\u9645\u4e0a\u662f\u957f\u5ea6\u4e3a 1 \u7684\u5b57\u7b26\u4e32 str \u3002
        • C \u548c C++ \u672a\u660e\u786e\u89c4\u5b9a\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u5927\u5c0f\uff0c\u800c\u56e0\u5b9e\u73b0\u548c\u5e73\u53f0\u5404\u5f02\u3002\u8868 3-1 \u9075\u5faa LP64 \u6570\u636e\u6a21\u578b\uff0c\u5176\u7528\u4e8e\u5305\u62ec Linux \u548c macOS \u5728\u5185\u7684 Unix 64 \u4f4d\u64cd\u4f5c\u7cfb\u7edf\u3002
        • \u5b57\u7b26 char \u7684\u5927\u5c0f\u5728 C \u548c C++ \u4e2d\u4e3a 1 \u5b57\u8282\uff0c\u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\u53d6\u51b3\u4e8e\u7279\u5b9a\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u6cd5\uff0c\u8be6\u89c1\u201c\u5b57\u7b26\u7f16\u7801\u201d\u7ae0\u8282\u3002
        • \u5373\u4f7f\u8868\u793a\u5e03\u5c14\u91cf\u4ec5\u9700 1 \u4f4d\uff08\\(0\\) \u6216 \\(1\\)\uff09\uff0c\u5b83\u5728\u5185\u5b58\u4e2d\u901a\u5e38\u4e5f\u5b58\u50a8\u4e3a 1 \u5b57\u8282\u3002\u8fd9\u662f\u56e0\u4e3a\u73b0\u4ee3\u8ba1\u7b97\u673a CPU \u901a\u5e38\u5c06 1 \u5b57\u8282\u4f5c\u4e3a\u6700\u5c0f\u5bfb\u5740\u5185\u5b58\u5355\u5143\u3002

        \u90a3\u4e48\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e0e\u6570\u636e\u7ed3\u6784\u4e4b\u95f4\u6709\u4ec0\u4e48\u8054\u7cfb\u5462\uff1f\u6211\u4eec\u77e5\u9053\uff0c\u6570\u636e\u7ed3\u6784\u662f\u5728\u8ba1\u7b97\u673a\u4e2d\u7ec4\u7ec7\u4e0e\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\u3002\u8fd9\u53e5\u8bdd\u7684\u4e3b\u8bed\u662f\u201c\u7ed3\u6784\u201d\u800c\u975e\u201c\u6570\u636e\u201d\u3002

        \u5982\u679c\u60f3\u8868\u793a\u201c\u4e00\u6392\u6570\u5b57\u201d\uff0c\u6211\u4eec\u81ea\u7136\u4f1a\u60f3\u5230\u4f7f\u7528\u6570\u7ec4\u3002\u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u7684\u7ebf\u6027\u7ed3\u6784\u53ef\u4ee5\u8868\u793a\u6570\u5b57\u7684\u76f8\u90bb\u5173\u7cfb\u548c\u987a\u5e8f\u5173\u7cfb\uff0c\u4f46\u81f3\u4e8e\u5b58\u50a8\u7684\u5185\u5bb9\u662f\u6574\u6570 int\u3001\u5c0f\u6570 float \u8fd8\u662f\u5b57\u7b26 char \uff0c\u5219\u4e0e\u201c\u6570\u636e\u7ed3\u6784\u201d\u65e0\u5173\u3002

        \u6362\u53e5\u8bdd\u8bf4\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u4e86\u6570\u636e\u7684\u201c\u5185\u5bb9\u7c7b\u578b\u201d\uff0c\u800c\u6570\u636e\u7ed3\u6784\u63d0\u4f9b\u4e86\u6570\u636e\u7684\u201c\u7ec4\u7ec7\u65b9\u5f0f\u201d\u3002\u4f8b\u5982\u4ee5\u4e0b\u4ee3\u7801\uff0c\u6211\u4eec\u7528\u76f8\u540c\u7684\u6570\u636e\u7ed3\u6784\uff08\u6570\u7ec4\uff09\u6765\u5b58\u50a8\u4e0e\u8868\u793a\u4e0d\u540c\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec int\u3001float\u3001char\u3001bool \u7b49\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        # \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nnumbers: list[int] = [0] * 5\ndecimals: list[float] = [0.0] * 5\n# Python \u7684\u5b57\u7b26\u5b9e\u9645\u4e0a\u662f\u957f\u5ea6\u4e3a 1 \u7684\u5b57\u7b26\u4e32\ncharacters: list[str] = ['0'] * 5\nbools: list[bool] = [False] * 5\n# Python \u7684\u5217\u8868\u53ef\u4ee5\u81ea\u7531\u5b58\u50a8\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u8c61\u5f15\u7528\ndata = [0, 0.0, 'a', False, ListNode(0)]\n
        // \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint numbers[5];\nfloat decimals[5];\nchar characters[5];\nbool bools[5];\n
        // \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nboolean[] bools = new boolean[5];\n
        // \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nbool[] bools = new bool[5];\n
        // \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nvar numbers = [5]int{}\nvar decimals = [5]float64{}\nvar characters = [5]byte{}\nvar bools = [5]bool{}\n
        // \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nlet numbers = Array(repeating: 0, count: 5)\nlet decimals = Array(repeating: 0.0, count: 5)\nlet characters: [Character] = Array(repeating: \"a\", count: 5)\nlet bools = Array(repeating: false, count: 5)\n
        // JavaScript \u7684\u6570\u7ec4\u53ef\u4ee5\u81ea\u7531\u5b58\u50a8\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u8c61\nconst array = [0, 0.0, 'a', false];\n
        // \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nconst numbers: number[] = [];\nconst characters: string[] = [];\nconst bools: boolean[] = [];\n
        // \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nList<int> numbers = List.filled(5, 0);\nList<double> decimals = List.filled(5, 0.0);\nList<String> characters = List.filled(5, 'a');\nList<bool> bools = List.filled(5, false);\n
        // \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nlet numbers: Vec<i32> = vec![0; 5];\nlet decimals: Vec<f32> = vec![0.0; 5];\nlet characters: Vec<char> = vec!['0'; 5];\nlet bools: Vec<bool> = vec![false; 5];\n
        // \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint numbers[10];\nfloat decimals[10];\nchar characters[10];\nbool bools[10];\n
        // \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nval numbers = IntArray(5)\nval decinals = FloatArray(5)\nval characters = CharArray(5)\nval bools = BooleanArray(5)\n
        # Ruby \u7684\u5217\u8868\u53ef\u4ee5\u81ea\u7531\u5b58\u50a8\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u8c61\u5f15\u7528\ndata = [0, 0.0, 'a', false, ListNode(0)]\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_data_structure/character_encoding/","title":"3.4 \u00a0 \u5b57\u7b26\u7f16\u7801 *","text":"

        \u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u6240\u6709\u6570\u636e\u90fd\u662f\u4ee5\u4e8c\u8fdb\u5236\u6570\u7684\u5f62\u5f0f\u5b58\u50a8\u7684\uff0c\u5b57\u7b26 char \u4e5f\u4e0d\u4f8b\u5916\u3002\u4e3a\u4e86\u8868\u793a\u5b57\u7b26\uff0c\u6211\u4eec\u9700\u8981\u5efa\u7acb\u4e00\u5957\u201c\u5b57\u7b26\u96c6\u201d\uff0c\u89c4\u5b9a\u6bcf\u4e2a\u5b57\u7b26\u548c\u4e8c\u8fdb\u5236\u6570\u4e4b\u95f4\u7684\u4e00\u4e00\u5bf9\u5e94\u5173\u7cfb\u3002\u6709\u4e86\u5b57\u7b26\u96c6\u4e4b\u540e\uff0c\u8ba1\u7b97\u673a\u5c31\u53ef\u4ee5\u901a\u8fc7\u67e5\u8868\u5b8c\u6210\u4e8c\u8fdb\u5236\u6570\u5230\u5b57\u7b26\u7684\u8f6c\u6362\u3002

        "},{"location":"chapter_data_structure/character_encoding/#341-ascii","title":"3.4.1 \u00a0 ASCII \u5b57\u7b26\u96c6","text":"

        ASCII \u7801\u662f\u6700\u65e9\u51fa\u73b0\u7684\u5b57\u7b26\u96c6\uff0c\u5176\u5168\u79f0\u4e3a American Standard Code for Information Interchange\uff08\u7f8e\u56fd\u6807\u51c6\u4fe1\u606f\u4ea4\u6362\u4ee3\u7801\uff09\u3002\u5b83\u4f7f\u7528 7 \u4f4d\u4e8c\u8fdb\u5236\u6570\uff08\u4e00\u4e2a\u5b57\u8282\u7684\u4f4e 7 \u4f4d\uff09\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff0c\u6700\u591a\u80fd\u591f\u8868\u793a 128 \u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\u3002\u5982\u56fe 3-6 \u6240\u793a\uff0cASCII \u7801\u5305\u62ec\u82f1\u6587\u5b57\u6bcd\u7684\u5927\u5c0f\u5199\u3001\u6570\u5b57 0 ~ 9\u3001\u4e00\u4e9b\u6807\u70b9\u7b26\u53f7\uff0c\u4ee5\u53ca\u4e00\u4e9b\u63a7\u5236\u5b57\u7b26\uff08\u5982\u6362\u884c\u7b26\u548c\u5236\u8868\u7b26\uff09\u3002

        \u56fe 3-6 \u00a0 ASCII \u7801

        \u7136\u800c\uff0cASCII \u7801\u4ec5\u80fd\u591f\u8868\u793a\u82f1\u6587\u3002\u968f\u7740\u8ba1\u7b97\u673a\u7684\u5168\u7403\u5316\uff0c\u8bde\u751f\u4e86\u4e00\u79cd\u80fd\u591f\u8868\u793a\u66f4\u591a\u8bed\u8a00\u7684 EASCII \u5b57\u7b26\u96c6\u3002\u5b83\u5728 ASCII \u7684 7 \u4f4d\u57fa\u7840\u4e0a\u6269\u5c55\u5230 8 \u4f4d\uff0c\u80fd\u591f\u8868\u793a 256 \u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\u3002

        \u5728\u4e16\u754c\u8303\u56f4\u5185\uff0c\u9646\u7eed\u51fa\u73b0\u4e86\u4e00\u6279\u9002\u7528\u4e8e\u4e0d\u540c\u5730\u533a\u7684 EASCII \u5b57\u7b26\u96c6\u3002\u8fd9\u4e9b\u5b57\u7b26\u96c6\u7684\u524d 128 \u4e2a\u5b57\u7b26\u7edf\u4e00\u4e3a ASCII \u7801\uff0c\u540e 128 \u4e2a\u5b57\u7b26\u5b9a\u4e49\u4e0d\u540c\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u8bed\u8a00\u7684\u9700\u6c42\u3002

        "},{"location":"chapter_data_structure/character_encoding/#342-gbk","title":"3.4.2 \u00a0 GBK \u5b57\u7b26\u96c6","text":"

        \u540e\u6765\u4eba\u4eec\u53d1\u73b0\uff0cEASCII \u7801\u4ecd\u7136\u65e0\u6cd5\u6ee1\u8db3\u8bb8\u591a\u8bed\u8a00\u7684\u5b57\u7b26\u6570\u91cf\u8981\u6c42\u3002\u6bd4\u5982\u6c49\u5b57\u6709\u8fd1\u5341\u4e07\u4e2a\uff0c\u5149\u65e5\u5e38\u4f7f\u7528\u7684\u5c31\u6709\u51e0\u5343\u4e2a\u3002\u4e2d\u56fd\u56fd\u5bb6\u6807\u51c6\u603b\u5c40\u4e8e 1980 \u5e74\u53d1\u5e03\u4e86 GB2312 \u5b57\u7b26\u96c6\uff0c\u5176\u6536\u5f55\u4e86 6763 \u4e2a\u6c49\u5b57\uff0c\u57fa\u672c\u6ee1\u8db3\u4e86\u6c49\u5b57\u7684\u8ba1\u7b97\u673a\u5904\u7406\u9700\u8981\u3002

        \u7136\u800c\uff0cGB2312 \u65e0\u6cd5\u5904\u7406\u90e8\u5206\u7f55\u89c1\u5b57\u548c\u7e41\u4f53\u5b57\u3002GBK \u5b57\u7b26\u96c6\u662f\u5728 GB2312 \u7684\u57fa\u7840\u4e0a\u6269\u5c55\u5f97\u5230\u7684\uff0c\u5b83\u5171\u6536\u5f55\u4e86 21886 \u4e2a\u6c49\u5b57\u3002\u5728 GBK \u7684\u7f16\u7801\u65b9\u6848\u4e2d\uff0cASCII \u5b57\u7b26\u4f7f\u7528\u4e00\u4e2a\u5b57\u8282\u8868\u793a\uff0c\u6c49\u5b57\u4f7f\u7528\u4e24\u4e2a\u5b57\u8282\u8868\u793a\u3002

        "},{"location":"chapter_data_structure/character_encoding/#343-unicode","title":"3.4.3 \u00a0 Unicode \u5b57\u7b26\u96c6","text":"

        \u968f\u7740\u8ba1\u7b97\u673a\u6280\u672f\u7684\u84ec\u52c3\u53d1\u5c55\uff0c\u5b57\u7b26\u96c6\u4e0e\u7f16\u7801\u6807\u51c6\u767e\u82b1\u9f50\u653e\uff0c\u800c\u8fd9\u5e26\u6765\u4e86\u8bb8\u591a\u95ee\u9898\u3002\u4e00\u65b9\u9762\uff0c\u8fd9\u4e9b\u5b57\u7b26\u96c6\u4e00\u822c\u53ea\u5b9a\u4e49\u4e86\u7279\u5b9a\u8bed\u8a00\u7684\u5b57\u7b26\uff0c\u65e0\u6cd5\u5728\u591a\u8bed\u8a00\u73af\u5883\u4e0b\u6b63\u5e38\u5de5\u4f5c\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u540c\u4e00\u79cd\u8bed\u8a00\u5b58\u5728\u591a\u79cd\u5b57\u7b26\u96c6\u6807\u51c6\uff0c\u5982\u679c\u4e24\u53f0\u8ba1\u7b97\u673a\u4f7f\u7528\u7684\u662f\u4e0d\u540c\u7684\u7f16\u7801\u6807\u51c6\uff0c\u5219\u5728\u4fe1\u606f\u4f20\u9012\u65f6\u5c31\u4f1a\u51fa\u73b0\u4e71\u7801\u3002

        \u90a3\u4e2a\u65f6\u4ee3\u7684\u7814\u7a76\u4eba\u5458\u5c31\u5728\u60f3\uff1a\u5982\u679c\u63a8\u51fa\u4e00\u4e2a\u8db3\u591f\u5b8c\u6574\u7684\u5b57\u7b26\u96c6\uff0c\u5c06\u4e16\u754c\u8303\u56f4\u5185\u7684\u6240\u6709\u8bed\u8a00\u548c\u7b26\u53f7\u90fd\u6536\u5f55\u5176\u4e2d\uff0c\u4e0d\u5c31\u53ef\u4ee5\u89e3\u51b3\u8de8\u8bed\u8a00\u73af\u5883\u548c\u4e71\u7801\u95ee\u9898\u4e86\u5417\uff1f\u5728\u8fd9\u79cd\u60f3\u6cd5\u7684\u9a71\u52a8\u4e0b\uff0c\u4e00\u4e2a\u5927\u800c\u5168\u7684\u5b57\u7b26\u96c6 Unicode \u5e94\u8fd0\u800c\u751f\u3002

        Unicode \u7684\u4e2d\u6587\u540d\u79f0\u4e3a\u201c\u7edf\u4e00\u7801\u201d\uff0c\u7406\u8bba\u4e0a\u80fd\u5bb9\u7eb3 100 \u591a\u4e07\u4e2a\u5b57\u7b26\u3002\u5b83\u81f4\u529b\u4e8e\u5c06\u5168\u7403\u8303\u56f4\u5185\u7684\u5b57\u7b26\u7eb3\u5165\u7edf\u4e00\u7684\u5b57\u7b26\u96c6\u4e4b\u4e2d\uff0c\u63d0\u4f9b\u4e00\u79cd\u901a\u7528\u7684\u5b57\u7b26\u96c6\u6765\u5904\u7406\u548c\u663e\u793a\u5404\u79cd\u8bed\u8a00\u6587\u5b57\uff0c\u51cf\u5c11\u56e0\u4e3a\u7f16\u7801\u6807\u51c6\u4e0d\u540c\u800c\u4ea7\u751f\u7684\u4e71\u7801\u95ee\u9898\u3002

        \u81ea 1991 \u5e74\u53d1\u5e03\u4ee5\u6765\uff0cUnicode \u4e0d\u65ad\u6269\u5145\u65b0\u7684\u8bed\u8a00\u4e0e\u5b57\u7b26\u3002\u622a\u81f3 2022 \u5e74 9 \u6708\uff0cUnicode \u5df2\u7ecf\u5305\u542b 149186 \u4e2a\u5b57\u7b26\uff0c\u5305\u62ec\u5404\u79cd\u8bed\u8a00\u7684\u5b57\u7b26\u3001\u7b26\u53f7\u751a\u81f3\u8868\u60c5\u7b26\u53f7\u7b49\u3002\u5728\u5e9e\u5927\u7684 Unicode \u5b57\u7b26\u96c6\u4e2d\uff0c\u5e38\u7528\u7684\u5b57\u7b26\u5360\u7528 2 \u5b57\u8282\uff0c\u6709\u4e9b\u751f\u50fb\u7684\u5b57\u7b26\u5360\u7528 3 \u5b57\u8282\u751a\u81f3 4 \u5b57\u8282\u3002

        Unicode \u662f\u4e00\u79cd\u901a\u7528\u5b57\u7b26\u96c6\uff0c\u672c\u8d28\u4e0a\u662f\u7ed9\u6bcf\u4e2a\u5b57\u7b26\u5206\u914d\u4e00\u4e2a\u7f16\u53f7\uff08\u79f0\u4e3a\u201c\u7801\u70b9\u201d\uff09\uff0c\u4f46\u5b83\u5e76\u6ca1\u6709\u89c4\u5b9a\u5728\u8ba1\u7b97\u673a\u4e2d\u5982\u4f55\u5b58\u50a8\u8fd9\u4e9b\u5b57\u7b26\u7801\u70b9\u3002\u6211\u4eec\u4e0d\u7981\u4f1a\u95ee\uff1a\u5f53\u591a\u79cd\u957f\u5ea6\u7684 Unicode \u7801\u70b9\u540c\u65f6\u51fa\u73b0\u5728\u4e00\u4e2a\u6587\u672c\u4e2d\u65f6\uff0c\u7cfb\u7edf\u5982\u4f55\u89e3\u6790\u5b57\u7b26\uff1f\u4f8b\u5982\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a 2 \u5b57\u8282\u7684\u7f16\u7801\uff0c\u7cfb\u7edf\u5982\u4f55\u786e\u8ba4\u5b83\u662f\u4e00\u4e2a 2 \u5b57\u8282\u7684\u5b57\u7b26\u8fd8\u662f\u4e24\u4e2a 1 \u5b57\u8282\u7684\u5b57\u7b26\uff1f

        \u5bf9\u4e8e\u4ee5\u4e0a\u95ee\u9898\uff0c\u4e00\u79cd\u76f4\u63a5\u7684\u89e3\u51b3\u65b9\u6848\u662f\u5c06\u6240\u6709\u5b57\u7b26\u5b58\u50a8\u4e3a\u7b49\u957f\u7684\u7f16\u7801\u3002\u5982\u56fe 3-7 \u6240\u793a\uff0c\u201cHello\u201d\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 1 \u5b57\u8282\uff0c\u201c\u7b97\u6cd5\u201d\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u5b57\u8282\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u9ad8\u4f4d\u586b 0 \u5c06\u201cHello \u7b97\u6cd5\u201d\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u90fd\u7f16\u7801\u4e3a 2 \u5b57\u8282\u957f\u5ea6\u3002\u8fd9\u6837\u7cfb\u7edf\u5c31\u53ef\u4ee5\u6bcf\u9694 2 \u5b57\u8282\u89e3\u6790\u4e00\u4e2a\u5b57\u7b26\uff0c\u6062\u590d\u8fd9\u4e2a\u77ed\u8bed\u7684\u5185\u5bb9\u4e86\u3002

        \u56fe 3-7 \u00a0 Unicode \u7f16\u7801\u793a\u4f8b

        \u7136\u800c ASCII \u7801\u5df2\u7ecf\u5411\u6211\u4eec\u8bc1\u660e\uff0c\u7f16\u7801\u82f1\u6587\u53ea\u9700 1 \u5b57\u8282\u3002\u82e5\u91c7\u7528\u4e0a\u8ff0\u65b9\u6848\uff0c\u82f1\u6587\u6587\u672c\u5360\u7528\u7a7a\u95f4\u7684\u5927\u5c0f\u5c06\u4f1a\u662f ASCII \u7f16\u7801\u4e0b\u7684\u4e24\u500d\uff0c\u975e\u5e38\u6d6a\u8d39\u5185\u5b58\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4e00\u79cd\u66f4\u52a0\u9ad8\u6548\u7684 Unicode \u7f16\u7801\u65b9\u6cd5\u3002

        "},{"location":"chapter_data_structure/character_encoding/#344-utf-8","title":"3.4.4 \u00a0 UTF-8 \u7f16\u7801","text":"

        \u76ee\u524d\uff0cUTF-8 \u5df2\u6210\u4e3a\u56fd\u9645\u4e0a\u4f7f\u7528\u6700\u5e7f\u6cdb\u7684 Unicode \u7f16\u7801\u65b9\u6cd5\u3002\u5b83\u662f\u4e00\u79cd\u53ef\u53d8\u957f\u5ea6\u7684\u7f16\u7801\uff0c\u4f7f\u7528 1 \u5230 4 \u5b57\u8282\u6765\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff0c\u6839\u636e\u5b57\u7b26\u7684\u590d\u6742\u6027\u800c\u53d8\u3002ASCII \u5b57\u7b26\u53ea\u9700 1 \u5b57\u8282\uff0c\u62c9\u4e01\u5b57\u6bcd\u548c\u5e0c\u814a\u5b57\u6bcd\u9700\u8981 2 \u5b57\u8282\uff0c\u5e38\u7528\u7684\u4e2d\u6587\u5b57\u7b26\u9700\u8981 3 \u5b57\u8282\uff0c\u5176\u4ed6\u7684\u4e00\u4e9b\u751f\u50fb\u5b57\u7b26\u9700\u8981 4 \u5b57\u8282\u3002

        UTF-8 \u7684\u7f16\u7801\u89c4\u5219\u5e76\u4e0d\u590d\u6742\uff0c\u5206\u4e3a\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\u3002

        • \u5bf9\u4e8e\u957f\u5ea6\u4e3a 1 \u5b57\u8282\u7684\u5b57\u7b26\uff0c\u5c06\u6700\u9ad8\u4f4d\u8bbe\u7f6e\u4e3a \\(0\\) \uff0c\u5176\u4f59 7 \u4f4d\u8bbe\u7f6e\u4e3a Unicode \u7801\u70b9\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0cASCII \u5b57\u7b26\u5728 Unicode \u5b57\u7b26\u96c6\u4e2d\u5360\u636e\u4e86\u524d 128 \u4e2a\u7801\u70b9\u3002\u4e5f\u5c31\u662f\u8bf4\uff0cUTF-8 \u7f16\u7801\u53ef\u4ee5\u5411\u4e0b\u517c\u5bb9 ASCII \u7801\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 UTF-8 \u6765\u89e3\u6790\u5e74\u4ee3\u4e45\u8fdc\u7684 ASCII \u7801\u6587\u672c\u3002
        • \u5bf9\u4e8e\u957f\u5ea6\u4e3a \\(n\\) \u5b57\u8282\u7684\u5b57\u7b26\uff08\u5176\u4e2d \\(n > 1\\)\uff09\uff0c\u5c06\u9996\u4e2a\u5b57\u8282\u7684\u9ad8 \\(n\\) \u4f4d\u90fd\u8bbe\u7f6e\u4e3a \\(1\\) \uff0c\u7b2c \\(n + 1\\) \u4f4d\u8bbe\u7f6e\u4e3a \\(0\\) \uff1b\u4ece\u7b2c\u4e8c\u4e2a\u5b57\u8282\u5f00\u59cb\uff0c\u5c06\u6bcf\u4e2a\u5b57\u8282\u7684\u9ad8 2 \u4f4d\u90fd\u8bbe\u7f6e\u4e3a \\(10\\) \uff1b\u5176\u4f59\u6240\u6709\u4f4d\u7528\u4e8e\u586b\u5145\u5b57\u7b26\u7684 Unicode \u7801\u70b9\u3002

        \u56fe 3-8 \u5c55\u793a\u4e86\u201cHello\u7b97\u6cd5\u201d\u5bf9\u5e94\u7684 UTF-8 \u7f16\u7801\u3002\u89c2\u5bdf\u53d1\u73b0\uff0c\u7531\u4e8e\u6700\u9ad8 \\(n\\) \u4f4d\u90fd\u8bbe\u7f6e\u4e3a \\(1\\) \uff0c\u56e0\u6b64\u7cfb\u7edf\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u6700\u9ad8\u4f4d \\(1\\) \u7684\u4e2a\u6570\u6765\u89e3\u6790\u51fa\u5b57\u7b26\u7684\u957f\u5ea6\u4e3a \\(n\\) \u3002

        \u4f46\u4e3a\u4ec0\u4e48\u8981\u5c06\u5176\u4f59\u6240\u6709\u5b57\u8282\u7684\u9ad8 2 \u4f4d\u90fd\u8bbe\u7f6e\u4e3a \\(10\\) \u5462\uff1f\u5b9e\u9645\u4e0a\uff0c\u8fd9\u4e2a \\(10\\) \u80fd\u591f\u8d77\u5230\u6821\u9a8c\u7b26\u7684\u4f5c\u7528\u3002\u5047\u8bbe\u7cfb\u7edf\u4ece\u4e00\u4e2a\u9519\u8bef\u7684\u5b57\u8282\u5f00\u59cb\u89e3\u6790\u6587\u672c\uff0c\u5b57\u8282\u5934\u90e8\u7684 \\(10\\) \u80fd\u591f\u5e2e\u52a9\u7cfb\u7edf\u5feb\u901f\u5224\u65ad\u51fa\u5f02\u5e38\u3002

        \u4e4b\u6240\u4ee5\u5c06 \\(10\\) \u5f53\u4f5c\u6821\u9a8c\u7b26\uff0c\u662f\u56e0\u4e3a\u5728 UTF-8 \u7f16\u7801\u89c4\u5219\u4e0b\uff0c\u4e0d\u53ef\u80fd\u6709\u5b57\u7b26\u7684\u6700\u9ad8\u4e24\u4f4d\u662f \\(10\\) \u3002\u8fd9\u4e2a\u7ed3\u8bba\u53ef\u4ee5\u7528\u53cd\u8bc1\u6cd5\u6765\u8bc1\u660e\uff1a\u5047\u8bbe\u4e00\u4e2a\u5b57\u7b26\u7684\u6700\u9ad8\u4e24\u4f4d\u662f \\(10\\) \uff0c\u8bf4\u660e\u8be5\u5b57\u7b26\u7684\u957f\u5ea6\u4e3a \\(1\\) \uff0c\u5bf9\u5e94 ASCII \u7801\u3002\u800c ASCII \u7801\u7684\u6700\u9ad8\u4f4d\u5e94\u8be5\u662f \\(0\\) \uff0c\u4e0e\u5047\u8bbe\u77db\u76fe\u3002

        \u56fe 3-8 \u00a0 UTF-8 \u7f16\u7801\u793a\u4f8b

        \u9664\u4e86 UTF-8 \u4e4b\u5916\uff0c\u5e38\u89c1\u7684\u7f16\u7801\u65b9\u5f0f\u8fd8\u5305\u62ec\u4ee5\u4e0b\u4e24\u79cd\u3002

        • UTF-16 \u7f16\u7801\uff1a\u4f7f\u7528 2 \u6216 4 \u5b57\u8282\u6765\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\u3002\u6240\u6709\u7684 ASCII \u5b57\u7b26\u548c\u5e38\u7528\u7684\u975e\u82f1\u6587\u5b57\u7b26\uff0c\u90fd\u7528 2 \u5b57\u8282\u8868\u793a\uff1b\u5c11\u6570\u5b57\u7b26\u9700\u8981\u7528\u5230 4 \u5b57\u8282\u8868\u793a\u3002\u5bf9\u4e8e 2 \u5b57\u8282\u7684\u5b57\u7b26\uff0cUTF-16 \u7f16\u7801\u4e0e Unicode \u7801\u70b9\u76f8\u7b49\u3002
        • UTF-32 \u7f16\u7801\uff1a\u6bcf\u4e2a\u5b57\u7b26\u90fd\u4f7f\u7528 4 \u5b57\u8282\u3002\u8fd9\u610f\u5473\u7740 UTF-32 \u6bd4 UTF-8 \u548c UTF-16 \u66f4\u5360\u7528\u7a7a\u95f4\uff0c\u7279\u522b\u662f\u5bf9\u4e8e ASCII \u5b57\u7b26\u5360\u6bd4\u8f83\u9ad8\u7684\u6587\u672c\u3002

        \u4ece\u5b58\u50a8\u7a7a\u95f4\u5360\u7528\u7684\u89d2\u5ea6\u770b\uff0c\u4f7f\u7528 UTF-8 \u8868\u793a\u82f1\u6587\u5b57\u7b26\u975e\u5e38\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u4ec5\u9700 1 \u5b57\u8282\uff1b\u4f7f\u7528 UTF-16 \u7f16\u7801\u67d0\u4e9b\u975e\u82f1\u6587\u5b57\u7b26\uff08\u4f8b\u5982\u4e2d\u6587\uff09\u4f1a\u66f4\u52a0\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u4ec5\u9700 2 \u5b57\u8282\uff0c\u800c UTF-8 \u53ef\u80fd\u9700\u8981 3 \u5b57\u8282\u3002

        \u4ece\u517c\u5bb9\u6027\u7684\u89d2\u5ea6\u770b\uff0cUTF-8 \u7684\u901a\u7528\u6027\u6700\u4f73\uff0c\u8bb8\u591a\u5de5\u5177\u548c\u5e93\u4f18\u5148\u652f\u6301 UTF-8 \u3002

        "},{"location":"chapter_data_structure/character_encoding/#345","title":"3.4.5 \u00a0 \u7f16\u7a0b\u8bed\u8a00\u7684\u5b57\u7b26\u7f16\u7801","text":"

        \u5bf9\u4e8e\u4ee5\u5f80\u7684\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\uff0c\u7a0b\u5e8f\u8fd0\u884c\u4e2d\u7684\u5b57\u7b26\u4e32\u90fd\u91c7\u7528 UTF-16 \u6216 UTF-32 \u8fd9\u7c7b\u7b49\u957f\u7f16\u7801\u3002\u5728\u7b49\u957f\u7f16\u7801\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u770b\u4f5c\u6570\u7ec4\u6765\u5904\u7406\uff0c\u8fd9\u79cd\u505a\u6cd5\u5177\u6709\u4ee5\u4e0b\u4f18\u70b9\u3002

        • \u968f\u673a\u8bbf\u95ee\uff1aUTF-16 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u8fdb\u884c\u968f\u673a\u8bbf\u95ee\u3002UTF-8 \u662f\u4e00\u79cd\u53d8\u957f\u7f16\u7801\uff0c\u8981\u60f3\u627e\u5230\u7b2c \\(i\\) \u4e2a\u5b57\u7b26\uff0c\u6211\u4eec\u9700\u8981\u4ece\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u5904\u904d\u5386\u5230\u7b2c \\(i\\) \u4e2a\u5b57\u7b26\uff0c\u8fd9\u9700\u8981 \\(O(n)\\) \u7684\u65f6\u95f4\u3002
        • \u5b57\u7b26\u8ba1\u6570\uff1a\u4e0e\u968f\u673a\u8bbf\u95ee\u7c7b\u4f3c\uff0c\u8ba1\u7b97 UTF-16 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u4e5f\u662f \\(O(1)\\) \u7684\u64cd\u4f5c\u3002\u4f46\u662f\uff0c\u8ba1\u7b97 UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u9700\u8981\u904d\u5386\u6574\u4e2a\u5b57\u7b26\u4e32\u3002
        • \u5b57\u7b26\u4e32\u64cd\u4f5c\uff1a\u5728 UTF-16 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u4e0a\uff0c\u5f88\u591a\u5b57\u7b26\u4e32\u64cd\u4f5c\uff08\u5982\u5206\u5272\u3001\u8fde\u63a5\u3001\u63d2\u5165\u3001\u5220\u9664\u7b49\uff09\u66f4\u5bb9\u6613\u8fdb\u884c\u3002\u5728 UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u4e0a\uff0c\u8fdb\u884c\u8fd9\u4e9b\u64cd\u4f5c\u901a\u5e38\u9700\u8981\u989d\u5916\u7684\u8ba1\u7b97\uff0c\u4ee5\u786e\u4fdd\u4e0d\u4f1a\u4ea7\u751f\u65e0\u6548\u7684 UTF-8 \u7f16\u7801\u3002

        \u5b9e\u9645\u4e0a\uff0c\u7f16\u7a0b\u8bed\u8a00\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u6848\u8bbe\u8ba1\u662f\u4e00\u4e2a\u5f88\u6709\u8da3\u7684\u8bdd\u9898\uff0c\u6d89\u53ca\u8bb8\u591a\u56e0\u7d20\u3002

        • Java \u7684 String \u7c7b\u578b\u4f7f\u7528 UTF-16 \u7f16\u7801\uff0c\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u5b57\u8282\u3002\u8fd9\u662f\u56e0\u4e3a Java \u8bed\u8a00\u8bbe\u8ba1\u4e4b\u521d\uff0c\u4eba\u4eec\u8ba4\u4e3a 16 \u4f4d\u8db3\u4ee5\u8868\u793a\u6240\u6709\u53ef\u80fd\u7684\u5b57\u7b26\u3002\u7136\u800c\uff0c\u8fd9\u662f\u4e00\u4e2a\u4e0d\u6b63\u786e\u7684\u5224\u65ad\u3002\u540e\u6765 Unicode \u89c4\u8303\u6269\u5c55\u5230\u4e86\u8d85\u8fc7 16 \u4f4d\uff0c\u6240\u4ee5 Java \u4e2d\u7684\u5b57\u7b26\u73b0\u5728\u53ef\u80fd\u7531\u4e00\u5bf9 16 \u4f4d\u7684\u503c\uff08\u79f0\u4e3a\u201c\u4ee3\u7406\u5bf9\u201d\uff09\u8868\u793a\u3002
        • JavaScript \u548c TypeScript \u7684\u5b57\u7b26\u4e32\u4f7f\u7528 UTF-16 \u7f16\u7801\u7684\u539f\u56e0\u4e0e Java \u7c7b\u4f3c\u3002\u5f53 1995 \u5e74 Netscape \u516c\u53f8\u9996\u6b21\u63a8\u51fa JavaScript \u8bed\u8a00\u65f6\uff0cUnicode \u8fd8\u5904\u4e8e\u53d1\u5c55\u65e9\u671f\uff0c\u90a3\u65f6\u5019\u4f7f\u7528 16 \u4f4d\u7684\u7f16\u7801\u5c31\u8db3\u4ee5\u8868\u793a\u6240\u6709\u7684 Unicode \u5b57\u7b26\u4e86\u3002
        • C# \u4f7f\u7528 UTF-16 \u7f16\u7801\uff0c\u4e3b\u8981\u662f\u56e0\u4e3a .NET \u5e73\u53f0\u662f\u7531 Microsoft \u8bbe\u8ba1\u7684\uff0c\u800c Microsoft \u7684\u5f88\u591a\u6280\u672f\uff08\u5305\u62ec Windows \u64cd\u4f5c\u7cfb\u7edf\uff09\u90fd\u5e7f\u6cdb\u4f7f\u7528 UTF-16 \u7f16\u7801\u3002

        \u7531\u4e8e\u4ee5\u4e0a\u7f16\u7a0b\u8bed\u8a00\u5bf9\u5b57\u7b26\u6570\u91cf\u7684\u4f4e\u4f30\uff0c\u5b83\u4eec\u4e0d\u5f97\u4e0d\u91c7\u53d6\u201c\u4ee3\u7406\u5bf9\u201d\u7684\u65b9\u5f0f\u6765\u8868\u793a\u8d85\u8fc7 16 \u4f4d\u957f\u5ea6\u7684 Unicode \u5b57\u7b26\u3002\u8fd9\u662f\u4e00\u4e2a\u4e0d\u5f97\u5df2\u4e3a\u4e4b\u7684\u65e0\u5948\u4e4b\u4e3e\u3002\u4e00\u65b9\u9762\uff0c\u5305\u542b\u4ee3\u7406\u5bf9\u7684\u5b57\u7b26\u4e32\u4e2d\uff0c\u4e00\u4e2a\u5b57\u7b26\u53ef\u80fd\u5360\u7528 2 \u5b57\u8282\u6216 4 \u5b57\u8282\uff0c\u4ece\u800c\u4e27\u5931\u4e86\u7b49\u957f\u7f16\u7801\u7684\u4f18\u52bf\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u5904\u7406\u4ee3\u7406\u5bf9\u9700\u8981\u989d\u5916\u589e\u52a0\u4ee3\u7801\uff0c\u8fd9\u63d0\u9ad8\u4e86\u7f16\u7a0b\u7684\u590d\u6742\u6027\u548c\u8c03\u8bd5\u96be\u5ea6\u3002

        \u51fa\u4e8e\u4ee5\u4e0a\u539f\u56e0\uff0c\u90e8\u5206\u7f16\u7a0b\u8bed\u8a00\u63d0\u51fa\u4e86\u4e00\u4e9b\u4e0d\u540c\u7684\u7f16\u7801\u65b9\u6848\u3002

        • Python \u4e2d\u7684 str \u4f7f\u7528 Unicode \u7f16\u7801\uff0c\u5e76\u91c7\u7528\u4e00\u79cd\u7075\u6d3b\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u5b58\u50a8\u7684\u5b57\u7b26\u957f\u5ea6\u53d6\u51b3\u4e8e\u5b57\u7b26\u4e32\u4e2d\u6700\u5927\u7684 Unicode \u7801\u70b9\u3002\u82e5\u5b57\u7b26\u4e32\u4e2d\u5168\u90e8\u662f ASCII \u5b57\u7b26\uff0c\u5219\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 1 \u5b57\u8282\uff1b\u5982\u679c\u6709\u5b57\u7b26\u8d85\u51fa\u4e86 ASCII \u8303\u56f4\uff0c\u4f46\u5168\u90e8\u5728\u57fa\u672c\u591a\u8bed\u8a00\u5e73\u9762\uff08BMP\uff09\u5185\uff0c\u5219\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u5b57\u8282\uff1b\u5982\u679c\u6709\u8d85\u51fa BMP \u7684\u5b57\u7b26\uff0c\u5219\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 4 \u5b57\u8282\u3002
        • Go \u8bed\u8a00\u7684 string \u7c7b\u578b\u5728\u5185\u90e8\u4f7f\u7528 UTF-8 \u7f16\u7801\u3002Go \u8bed\u8a00\u8fd8\u63d0\u4f9b\u4e86 rune \u7c7b\u578b\uff0c\u5b83\u7528\u4e8e\u8868\u793a\u5355\u4e2a Unicode \u7801\u70b9\u3002
        • Rust \u8bed\u8a00\u7684 str \u548c String \u7c7b\u578b\u5728\u5185\u90e8\u4f7f\u7528 UTF-8 \u7f16\u7801\u3002Rust \u4e5f\u63d0\u4f9b\u4e86 char \u7c7b\u578b\uff0c\u7528\u4e8e\u8868\u793a\u5355\u4e2a Unicode \u7801\u70b9\u3002

        \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4ee5\u4e0a\u8ba8\u8bba\u7684\u90fd\u662f\u5b57\u7b26\u4e32\u5728\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u8fd9\u548c\u5b57\u7b26\u4e32\u5982\u4f55\u5728\u6587\u4ef6\u4e2d\u5b58\u50a8\u6216\u5728\u7f51\u7edc\u4e2d\u4f20\u8f93\u662f\u4e0d\u540c\u7684\u95ee\u9898\u3002\u5728\u6587\u4ef6\u5b58\u50a8\u6216\u7f51\u7edc\u4f20\u8f93\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a UTF-8 \u683c\u5f0f\uff0c\u4ee5\u8fbe\u5230\u6700\u4f18\u7684\u517c\u5bb9\u6027\u548c\u7a7a\u95f4\u6548\u7387\u3002

        "},{"location":"chapter_data_structure/classification_of_data_structure/","title":"3.1 \u00a0 \u6570\u636e\u7ed3\u6784\u5206\u7c7b","text":"

        \u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\u5305\u62ec\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\uff0c\u5b83\u4eec\u53ef\u4ee5\u4ece\u201c\u903b\u8f91\u7ed3\u6784\u201d\u548c\u201c\u7269\u7406\u7ed3\u6784\u201d\u4e24\u4e2a\u7ef4\u5ea6\u8fdb\u884c\u5206\u7c7b\u3002

        "},{"location":"chapter_data_structure/classification_of_data_structure/#311","title":"3.1.1 \u00a0 \u903b\u8f91\u7ed3\u6784\uff1a\u7ebf\u6027\u4e0e\u975e\u7ebf\u6027","text":"

        \u903b\u8f91\u7ed3\u6784\u63ed\u793a\u4e86\u6570\u636e\u5143\u7d20\u4e4b\u95f4\u7684\u903b\u8f91\u5173\u7cfb\u3002\u5728\u6570\u7ec4\u548c\u94fe\u8868\u4e2d\uff0c\u6570\u636e\u6309\u7167\u4e00\u5b9a\u987a\u5e8f\u6392\u5217\uff0c\u4f53\u73b0\u4e86\u6570\u636e\u4e4b\u95f4\u7684\u7ebf\u6027\u5173\u7cfb\uff1b\u800c\u5728\u6811\u4e2d\uff0c\u6570\u636e\u4ece\u9876\u90e8\u5411\u4e0b\u6309\u5c42\u6b21\u6392\u5217\uff0c\u8868\u73b0\u51fa\u201c\u7956\u5148\u201d\u4e0e\u201c\u540e\u4ee3\u201d\u4e4b\u95f4\u7684\u6d3e\u751f\u5173\u7cfb\uff1b\u56fe\u5219\u7531\u8282\u70b9\u548c\u8fb9\u6784\u6210\uff0c\u53cd\u6620\u4e86\u590d\u6742\u7684\u7f51\u7edc\u5173\u7cfb\u3002

        \u5982\u56fe 3-1 \u6240\u793a\uff0c\u903b\u8f91\u7ed3\u6784\u53ef\u5206\u4e3a\u201c\u7ebf\u6027\u201d\u548c\u201c\u975e\u7ebf\u6027\u201d\u4e24\u5927\u7c7b\u3002\u7ebf\u6027\u7ed3\u6784\u6bd4\u8f83\u76f4\u89c2\uff0c\u6307\u6570\u636e\u5728\u903b\u8f91\u5173\u7cfb\u4e0a\u5448\u7ebf\u6027\u6392\u5217\uff1b\u975e\u7ebf\u6027\u7ed3\u6784\u5219\u76f8\u53cd\uff0c\u5448\u975e\u7ebf\u6027\u6392\u5217\u3002

        • \u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff1a\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\uff0c\u5143\u7d20\u4e4b\u95f4\u662f\u4e00\u5bf9\u4e00\u7684\u987a\u5e8f\u5173\u7cfb\u3002
        • \u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff1a\u6811\u3001\u5806\u3001\u56fe\u3001\u54c8\u5e0c\u8868\u3002

        \u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5212\u5206\u4e3a\u6811\u5f62\u7ed3\u6784\u548c\u7f51\u72b6\u7ed3\u6784\u3002

        • \u6811\u5f62\u7ed3\u6784\uff1a\u6811\u3001\u5806\u3001\u54c8\u5e0c\u8868\uff0c\u5143\u7d20\u4e4b\u95f4\u662f\u4e00\u5bf9\u591a\u7684\u5173\u7cfb\u3002
        • \u7f51\u72b6\u7ed3\u6784\uff1a\u56fe\uff0c\u5143\u7d20\u4e4b\u95f4\u662f\u591a\u5bf9\u591a\u7684\u5173\u7cfb\u3002

        \u56fe 3-1 \u00a0 \u7ebf\u6027\u6570\u636e\u7ed3\u6784\u4e0e\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784

        "},{"location":"chapter_data_structure/classification_of_data_structure/#312","title":"3.1.2 \u00a0 \u7269\u7406\u7ed3\u6784\uff1a\u8fde\u7eed\u4e0e\u5206\u6563","text":"

        \u5f53\u7b97\u6cd5\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u6b63\u5728\u5904\u7406\u7684\u6570\u636e\u4e3b\u8981\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u3002\u56fe 3-2 \u5c55\u793a\u4e86\u4e00\u4e2a\u8ba1\u7b97\u673a\u5185\u5b58\u6761\uff0c\u5176\u4e2d\u6bcf\u4e2a\u9ed1\u8272\u65b9\u5757\u90fd\u5305\u542b\u4e00\u5757\u5185\u5b58\u7a7a\u95f4\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u5185\u5b58\u60f3\u8c61\u6210\u4e00\u4e2a\u5de8\u5927\u7684 Excel \u8868\u683c\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5355\u5143\u683c\u90fd\u53ef\u4ee5\u5b58\u50a8\u4e00\u5b9a\u5927\u5c0f\u7684\u6570\u636e\u3002

        \u7cfb\u7edf\u901a\u8fc7\u5185\u5b58\u5730\u5740\u6765\u8bbf\u95ee\u76ee\u6807\u4f4d\u7f6e\u7684\u6570\u636e\u3002\u5982\u56fe 3-2 \u6240\u793a\uff0c\u8ba1\u7b97\u673a\u6839\u636e\u7279\u5b9a\u89c4\u5219\u4e3a\u8868\u683c\u4e2d\u7684\u6bcf\u4e2a\u5355\u5143\u683c\u5206\u914d\u7f16\u53f7\uff0c\u786e\u4fdd\u6bcf\u4e2a\u5185\u5b58\u7a7a\u95f4\u90fd\u6709\u552f\u4e00\u7684\u5185\u5b58\u5730\u5740\u3002\u6709\u4e86\u8fd9\u4e9b\u5730\u5740\uff0c\u7a0b\u5e8f\u4fbf\u53ef\u4ee5\u8bbf\u95ee\u5185\u5b58\u4e2d\u7684\u6570\u636e\u3002

        \u56fe 3-2 \u00a0 \u5185\u5b58\u6761\u3001\u5185\u5b58\u7a7a\u95f4\u3001\u5185\u5b58\u5730\u5740

        Tip

        \u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u5c06\u5185\u5b58\u6bd4\u4f5c Excel \u8868\u683c\u662f\u4e00\u4e2a\u7b80\u5316\u7684\u7c7b\u6bd4\uff0c\u5b9e\u9645\u5185\u5b58\u7684\u5de5\u4f5c\u673a\u5236\u6bd4\u8f83\u590d\u6742\uff0c\u6d89\u53ca\u5730\u5740\u7a7a\u95f4\u3001\u5185\u5b58\u7ba1\u7406\u3001\u7f13\u5b58\u673a\u5236\u3001\u865a\u62df\u5185\u5b58\u548c\u7269\u7406\u5185\u5b58\u7b49\u6982\u5ff5\u3002

        \u5185\u5b58\u662f\u6240\u6709\u7a0b\u5e8f\u7684\u5171\u4eab\u8d44\u6e90\uff0c\u5f53\u67d0\u5757\u5185\u5b58\u88ab\u67d0\u4e2a\u7a0b\u5e8f\u5360\u7528\u65f6\uff0c\u5219\u901a\u5e38\u65e0\u6cd5\u88ab\u5176\u4ed6\u7a0b\u5e8f\u540c\u65f6\u4f7f\u7528\u4e86\u3002\u56e0\u6b64\u5728\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u8bbe\u8ba1\u4e2d\uff0c\u5185\u5b58\u8d44\u6e90\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u6bd4\u5982\uff0c\u7b97\u6cd5\u6240\u5360\u7528\u7684\u5185\u5b58\u5cf0\u503c\u4e0d\u5e94\u8d85\u8fc7\u7cfb\u7edf\u5269\u4f59\u7a7a\u95f2\u5185\u5b58\uff1b\u5982\u679c\u7f3a\u5c11\u8fde\u7eed\u5927\u5757\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u90a3\u4e48\u6240\u9009\u7528\u7684\u6570\u636e\u7ed3\u6784\u5fc5\u987b\u80fd\u591f\u5b58\u50a8\u5728\u5206\u6563\u7684\u5185\u5b58\u7a7a\u95f4\u5185\u3002

        \u5982\u56fe 3-3 \u6240\u793a\uff0c\u7269\u7406\u7ed3\u6784\u53cd\u6620\u4e86\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u53ef\u5206\u4e3a\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\uff08\u6570\u7ec4\uff09\u548c\u5206\u6563\u7a7a\u95f4\u5b58\u50a8\uff08\u94fe\u8868\uff09\u3002\u7269\u7406\u7ed3\u6784\u4ece\u5e95\u5c42\u51b3\u5b9a\u4e86\u6570\u636e\u7684\u8bbf\u95ee\u3001\u66f4\u65b0\u3001\u589e\u5220\u7b49\u64cd\u4f5c\u65b9\u6cd5\uff0c\u4e24\u79cd\u7269\u7406\u7ed3\u6784\u5728\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u65b9\u9762\u5448\u73b0\u51fa\u4e92\u8865\u7684\u7279\u70b9\u3002

        \u56fe 3-3 \u00a0 \u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u4e0e\u5206\u6563\u7a7a\u95f4\u5b58\u50a8

        \u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6240\u6709\u6570\u636e\u7ed3\u6784\u90fd\u662f\u57fa\u4e8e\u6570\u7ec4\u3001\u94fe\u8868\u6216\u4e8c\u8005\u7684\u7ec4\u5408\u5b9e\u73b0\u7684\u3002\u4f8b\u5982\uff0c\u6808\u548c\u961f\u5217\u65e2\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u94fe\u8868\u5b9e\u73b0\uff1b\u800c\u54c8\u5e0c\u8868\u7684\u5b9e\u73b0\u53ef\u80fd\u540c\u65f6\u5305\u542b\u6570\u7ec4\u548c\u94fe\u8868\u3002

        • \u57fa\u4e8e\u6570\u7ec4\u53ef\u5b9e\u73b0\uff1a\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\u3001\u77e9\u9635\u3001\u5f20\u91cf\uff08\u7ef4\u5ea6 \\(\\geq 3\\) \u7684\u6570\u7ec4\uff09\u7b49\u3002
        • \u57fa\u4e8e\u94fe\u8868\u53ef\u5b9e\u73b0\uff1a\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\u7b49\u3002

        \u94fe\u8868\u5728\u521d\u59cb\u5316\u540e\uff0c\u4ecd\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u5bf9\u5176\u957f\u5ea6\u8fdb\u884c\u8c03\u6574\uff0c\u56e0\u6b64\u4e5f\u79f0\u201c\u52a8\u6001\u6570\u636e\u7ed3\u6784\u201d\u3002\u6570\u7ec4\u5728\u521d\u59cb\u5316\u540e\u957f\u5ea6\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u4e5f\u79f0\u201c\u9759\u6001\u6570\u636e\u7ed3\u6784\u201d\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6570\u7ec4\u53ef\u901a\u8fc7\u91cd\u65b0\u5206\u914d\u5185\u5b58\u5b9e\u73b0\u957f\u5ea6\u53d8\u5316\uff0c\u4ece\u800c\u5177\u5907\u4e00\u5b9a\u7684\u201c\u52a8\u6001\u6027\u201d\u3002

        Tip

        \u5982\u679c\u4f60\u611f\u89c9\u7269\u7406\u7ed3\u6784\u7406\u89e3\u8d77\u6765\u6709\u56f0\u96be\uff0c\u5efa\u8bae\u5148\u9605\u8bfb\u4e0b\u4e00\u7ae0\uff0c\u7136\u540e\u518d\u56de\u987e\u672c\u8282\u5185\u5bb9\u3002

        "},{"location":"chapter_data_structure/number_encoding/","title":"3.3 \u00a0 \u6570\u5b57\u7f16\u7801 *","text":"

        Tip

        \u5728\u672c\u4e66\u4e2d\uff0c\u6807\u9898\u5e26\u6709 * \u7b26\u53f7\u7684\u662f\u9009\u8bfb\u7ae0\u8282\u3002\u5982\u679c\u4f60\u65f6\u95f4\u6709\u9650\u6216\u611f\u5230\u7406\u89e3\u56f0\u96be\uff0c\u53ef\u4ee5\u5148\u8df3\u8fc7\uff0c\u7b49\u5b66\u5b8c\u5fc5\u8bfb\u7ae0\u8282\u540e\u518d\u5355\u72ec\u653b\u514b\u3002

        "},{"location":"chapter_data_structure/number_encoding/#331","title":"3.3.1 \u00a0 \u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801","text":"

        \u5728\u4e0a\u4e00\u8282\u7684\u8868\u683c\u4e2d\u6211\u4eec\u53d1\u73b0\uff0c\u6240\u6709\u6574\u6570\u7c7b\u578b\u80fd\u591f\u8868\u793a\u7684\u8d1f\u6570\u90fd\u6bd4\u6b63\u6570\u591a\u4e00\u4e2a\uff0c\u4f8b\u5982 byte \u7684\u53d6\u503c\u8303\u56f4\u662f \\([-128, 127]\\) \u3002\u8fd9\u4e2a\u73b0\u8c61\u6bd4\u8f83\u53cd\u76f4\u89c9\uff0c\u5b83\u7684\u5185\u5728\u539f\u56e0\u6d89\u53ca\u539f\u7801\u3001\u53cd\u7801\u3001\u8865\u7801\u7684\u76f8\u5173\u77e5\u8bc6\u3002

        \u9996\u5148\u9700\u8981\u6307\u51fa\uff0c\u6570\u5b57\u662f\u4ee5\u201c\u8865\u7801\u201d\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u4e2d\u7684\u3002\u5728\u5206\u6790\u8fd9\u6837\u505a\u7684\u539f\u56e0\u4e4b\u524d\uff0c\u9996\u5148\u7ed9\u51fa\u4e09\u8005\u7684\u5b9a\u4e49\u3002

        • \u539f\u7801\uff1a\u6211\u4eec\u5c06\u6570\u5b57\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u7684\u6700\u9ad8\u4f4d\u89c6\u4e3a\u7b26\u53f7\u4f4d\uff0c\u5176\u4e2d \\(0\\) \u8868\u793a\u6b63\u6570\uff0c\\(1\\) \u8868\u793a\u8d1f\u6570\uff0c\u5176\u4f59\u4f4d\u8868\u793a\u6570\u5b57\u7684\u503c\u3002
        • \u53cd\u7801\uff1a\u6b63\u6570\u7684\u53cd\u7801\u4e0e\u5176\u539f\u7801\u76f8\u540c\uff0c\u8d1f\u6570\u7684\u53cd\u7801\u662f\u5bf9\u5176\u539f\u7801\u9664\u7b26\u53f7\u4f4d\u5916\u7684\u6240\u6709\u4f4d\u53d6\u53cd\u3002
        • \u8865\u7801\uff1a\u6b63\u6570\u7684\u8865\u7801\u4e0e\u5176\u539f\u7801\u76f8\u540c\uff0c\u8d1f\u6570\u7684\u8865\u7801\u662f\u5728\u5176\u53cd\u7801\u7684\u57fa\u7840\u4e0a\u52a0 \\(1\\) \u3002

        \u56fe 3-4 \u5c55\u793a\u4e86\u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801\u4e4b\u95f4\u7684\u8f6c\u6362\u65b9\u6cd5\u3002

        \u56fe 3-4 \u00a0 \u539f\u7801\u3001\u53cd\u7801\u4e0e\u8865\u7801\u4e4b\u95f4\u7684\u76f8\u4e92\u8f6c\u6362

        \u539f\u7801\uff08sign-magnitude\uff09\u867d\u7136\u6700\u76f4\u89c2\uff0c\u4f46\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027\u3002\u4e00\u65b9\u9762\uff0c\u8d1f\u6570\u7684\u539f\u7801\u4e0d\u80fd\u76f4\u63a5\u7528\u4e8e\u8fd0\u7b97\u3002\u4f8b\u5982\u5728\u539f\u7801\u4e0b\u8ba1\u7b97 \\(1 + (-2)\\) \uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f \\(-3\\) \uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u5bf9\u7684\u3002

        \\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 + 1000 \\; 0010 \\newline & = 1000 \\; 0011 \\newline & \\rightarrow -3 \\end{aligned} \\]

        \u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u8ba1\u7b97\u673a\u5f15\u5165\u4e86\u53cd\u7801\uff081's complement\uff09\u3002\u5982\u679c\u6211\u4eec\u5148\u5c06\u539f\u7801\u8f6c\u6362\u4e3a\u53cd\u7801\uff0c\u5e76\u5728\u53cd\u7801\u4e0b\u8ba1\u7b97 \\(1 + (-2)\\) \uff0c\u6700\u540e\u5c06\u7ed3\u679c\u4ece\u53cd\u7801\u8f6c\u6362\u56de\u539f\u7801\uff0c\u5219\u53ef\u5f97\u5230\u6b63\u786e\u7ed3\u679c \\(-1\\) \u3002

        \\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 \\; \\text{(\u539f\u7801)} + 1000 \\; 0010 \\; \\text{(\u539f\u7801)} \\newline & = 0000 \\; 0001 \\; \\text{(\u53cd\u7801)} + 1111 \\; 1101 \\; \\text{(\u53cd\u7801)} \\newline & = 1111 \\; 1110 \\; \\text{(\u53cd\u7801)} \\newline & = 1000 \\; 0001 \\; \\text{(\u539f\u7801)} \\newline & \\rightarrow -1 \\end{aligned} \\]

        \u53e6\u4e00\u65b9\u9762\uff0c\u6570\u5b57\u96f6\u7684\u539f\u7801\u6709 \\(+0\\) \u548c \\(-0\\) \u4e24\u79cd\u8868\u793a\u65b9\u5f0f\u3002\u8fd9\u610f\u5473\u7740\u6570\u5b57\u96f6\u5bf9\u5e94\u4e24\u4e2a\u4e0d\u540c\u7684\u4e8c\u8fdb\u5236\u7f16\u7801\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5e26\u6765\u6b67\u4e49\u3002\u6bd4\u5982\u5728\u6761\u4ef6\u5224\u65ad\u4e2d\uff0c\u5982\u679c\u6ca1\u6709\u533a\u5206\u6b63\u96f6\u548c\u8d1f\u96f6\uff0c\u5219\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5224\u65ad\u7ed3\u679c\u51fa\u9519\u3002\u800c\u5982\u679c\u6211\u4eec\u60f3\u5904\u7406\u6b63\u96f6\u548c\u8d1f\u96f6\u6b67\u4e49\uff0c\u5219\u9700\u8981\u5f15\u5165\u989d\u5916\u7684\u5224\u65ad\u64cd\u4f5c\uff0c\u8fd9\u53ef\u80fd\u4f1a\u964d\u4f4e\u8ba1\u7b97\u673a\u7684\u8fd0\u7b97\u6548\u7387\u3002

        \\[ \\begin{aligned} +0 & \\rightarrow 0000 \\; 0000 \\newline -0 & \\rightarrow 1000 \\; 0000 \\end{aligned} \\]

        \u4e0e\u539f\u7801\u4e00\u6837\uff0c\u53cd\u7801\u4e5f\u5b58\u5728\u6b63\u8d1f\u96f6\u6b67\u4e49\u95ee\u9898\uff0c\u56e0\u6b64\u8ba1\u7b97\u673a\u8fdb\u4e00\u6b65\u5f15\u5165\u4e86\u8865\u7801\uff082's complement\uff09\u3002\u6211\u4eec\u5148\u6765\u89c2\u5bdf\u4e00\u4e0b\u8d1f\u96f6\u7684\u539f\u7801\u3001\u53cd\u7801\u3001\u8865\u7801\u7684\u8f6c\u6362\u8fc7\u7a0b\uff1a

        \\[ \\begin{aligned} -0 \\rightarrow \\; & 1000 \\; 0000 \\; \\text{(\u539f\u7801)} \\newline = \\; & 1111 \\; 1111 \\; \\text{(\u53cd\u7801)} \\newline = 1 \\; & 0000 \\; 0000 \\; \\text{(\u8865\u7801)} \\newline \\end{aligned} \\]

        \u5728\u8d1f\u96f6\u7684\u53cd\u7801\u57fa\u7840\u4e0a\u52a0 \\(1\\) \u4f1a\u4ea7\u751f\u8fdb\u4f4d\uff0c\u4f46 byte \u7c7b\u578b\u7684\u957f\u5ea6\u53ea\u6709 8 \u4f4d\uff0c\u56e0\u6b64\u6ea2\u51fa\u5230\u7b2c 9 \u4f4d\u7684 \\(1\\) \u4f1a\u88ab\u820d\u5f03\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8d1f\u96f6\u7684\u8865\u7801\u4e3a \\(0000 \\; 0000\\) \uff0c\u4e0e\u6b63\u96f6\u7684\u8865\u7801\u76f8\u540c\u3002\u8fd9\u610f\u5473\u7740\u5728\u8865\u7801\u8868\u793a\u4e2d\u53ea\u5b58\u5728\u4e00\u4e2a\u96f6\uff0c\u6b63\u8d1f\u96f6\u6b67\u4e49\u4ece\u800c\u5f97\u5230\u89e3\u51b3\u3002

        \u8fd8\u5269\u6700\u540e\u4e00\u4e2a\u7591\u60d1\uff1abyte \u7c7b\u578b\u7684\u53d6\u503c\u8303\u56f4\u662f \\([-128, 127]\\) \uff0c\u591a\u51fa\u6765\u7684\u4e00\u4e2a\u8d1f\u6570 \\(-128\\) \u662f\u5982\u4f55\u5f97\u5230\u7684\u5462\uff1f\u6211\u4eec\u6ce8\u610f\u5230\uff0c\u533a\u95f4 \\([-127, +127]\\) \u5185\u7684\u6240\u6709\u6574\u6570\u90fd\u6709\u5bf9\u5e94\u7684\u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801\uff0c\u5e76\u4e14\u539f\u7801\u548c\u8865\u7801\u4e4b\u95f4\u53ef\u4ee5\u4e92\u76f8\u8f6c\u6362\u3002

        \u7136\u800c\uff0c\u8865\u7801 \\(1000 \\; 0000\\) \u662f\u4e00\u4e2a\u4f8b\u5916\uff0c\u5b83\u5e76\u6ca1\u6709\u5bf9\u5e94\u7684\u539f\u7801\u3002\u6839\u636e\u8f6c\u6362\u65b9\u6cd5\uff0c\u6211\u4eec\u5f97\u5230\u8be5\u8865\u7801\u7684\u539f\u7801\u4e3a \\(0000 \\; 0000\\) \u3002\u8fd9\u663e\u7136\u662f\u77db\u76fe\u7684\uff0c\u56e0\u4e3a\u8be5\u539f\u7801\u8868\u793a\u6570\u5b57 \\(0\\) \uff0c\u5b83\u7684\u8865\u7801\u5e94\u8be5\u662f\u81ea\u8eab\u3002\u8ba1\u7b97\u673a\u89c4\u5b9a\u8fd9\u4e2a\u7279\u6b8a\u7684\u8865\u7801 \\(1000 \\; 0000\\) \u4ee3\u8868 \\(-128\\) \u3002\u5b9e\u9645\u4e0a\uff0c\\((-1) + (-127)\\) \u5728\u8865\u7801\u4e0b\u7684\u8ba1\u7b97\u7ed3\u679c\u5c31\u662f \\(-128\\) \u3002

        \\[ \\begin{aligned} & (-127) + (-1) \\newline & \\rightarrow 1111 \\; 1111 \\; \\text{(\u539f\u7801)} + 1000 \\; 0001 \\; \\text{(\u539f\u7801)} \\newline & = 1000 \\; 0000 \\; \\text{(\u53cd\u7801)} + 1111 \\; 1110 \\; \\text{(\u53cd\u7801)} \\newline & = 1000 \\; 0001 \\; \\text{(\u8865\u7801)} + 1111 \\; 1111 \\; \\text{(\u8865\u7801)} \\newline & = 1000 \\; 0000 \\; \\text{(\u8865\u7801)} \\newline & \\rightarrow -128 \\end{aligned} \\]

        \u4f60\u53ef\u80fd\u5df2\u7ecf\u53d1\u73b0\u4e86\uff0c\u4e0a\u8ff0\u6240\u6709\u8ba1\u7b97\u90fd\u662f\u52a0\u6cd5\u8fd0\u7b97\u3002\u8fd9\u6697\u793a\u7740\u4e00\u4e2a\u91cd\u8981\u4e8b\u5b9e\uff1a\u8ba1\u7b97\u673a\u5185\u90e8\u7684\u786c\u4ef6\u7535\u8def\u4e3b\u8981\u662f\u57fa\u4e8e\u52a0\u6cd5\u8fd0\u7b97\u8bbe\u8ba1\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u52a0\u6cd5\u8fd0\u7b97\u76f8\u5bf9\u4e8e\u5176\u4ed6\u8fd0\u7b97\uff08\u6bd4\u5982\u4e58\u6cd5\u3001\u9664\u6cd5\u548c\u51cf\u6cd5\uff09\u6765\u8bf4\uff0c\u786c\u4ef6\u5b9e\u73b0\u8d77\u6765\u66f4\u7b80\u5355\uff0c\u66f4\u5bb9\u6613\u8fdb\u884c\u5e76\u884c\u5316\u5904\u7406\uff0c\u8fd0\u7b97\u901f\u5ea6\u66f4\u5feb\u3002

        \u8bf7\u6ce8\u610f\uff0c\u8fd9\u5e76\u4e0d\u610f\u5473\u7740\u8ba1\u7b97\u673a\u53ea\u80fd\u505a\u52a0\u6cd5\u3002\u901a\u8fc7\u5c06\u52a0\u6cd5\u4e0e\u4e00\u4e9b\u57fa\u672c\u903b\u8f91\u8fd0\u7b97\u7ed3\u5408\uff0c\u8ba1\u7b97\u673a\u80fd\u591f\u5b9e\u73b0\u5404\u79cd\u5176\u4ed6\u7684\u6570\u5b66\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u51cf\u6cd5 \\(a - b\\) \u53ef\u4ee5\u8f6c\u6362\u4e3a\u8ba1\u7b97\u52a0\u6cd5 \\(a + (-b)\\) \uff1b\u8ba1\u7b97\u4e58\u6cd5\u548c\u9664\u6cd5\u53ef\u4ee5\u8f6c\u6362\u4e3a\u8ba1\u7b97\u591a\u6b21\u52a0\u6cd5\u6216\u51cf\u6cd5\u3002

        \u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u603b\u7ed3\u51fa\u8ba1\u7b97\u673a\u4f7f\u7528\u8865\u7801\u7684\u539f\u56e0\uff1a\u57fa\u4e8e\u8865\u7801\u8868\u793a\uff0c\u8ba1\u7b97\u673a\u53ef\u4ee5\u7528\u540c\u6837\u7684\u7535\u8def\u548c\u64cd\u4f5c\u6765\u5904\u7406\u6b63\u6570\u548c\u8d1f\u6570\u7684\u52a0\u6cd5\uff0c\u4e0d\u9700\u8981\u8bbe\u8ba1\u7279\u6b8a\u7684\u786c\u4ef6\u7535\u8def\u6765\u5904\u7406\u51cf\u6cd5\uff0c\u5e76\u4e14\u65e0\u987b\u7279\u522b\u5904\u7406\u6b63\u8d1f\u96f6\u7684\u6b67\u4e49\u95ee\u9898\u3002\u8fd9\u5927\u5927\u7b80\u5316\u4e86\u786c\u4ef6\u8bbe\u8ba1\uff0c\u63d0\u9ad8\u4e86\u8fd0\u7b97\u6548\u7387\u3002

        \u8865\u7801\u7684\u8bbe\u8ba1\u975e\u5e38\u7cbe\u5999\uff0c\u56e0\u7bc7\u5e45\u5173\u7cfb\u6211\u4eec\u5c31\u5148\u4ecb\u7ecd\u5230\u8fd9\u91cc\uff0c\u5efa\u8bae\u6709\u5174\u8da3\u7684\u8bfb\u8005\u8fdb\u4e00\u6b65\u6df1\u5165\u4e86\u89e3\u3002

        "},{"location":"chapter_data_structure/number_encoding/#332","title":"3.3.2 \u00a0 \u6d6e\u70b9\u6570\u7f16\u7801","text":"

        \u7ec6\u5fc3\u7684\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\uff1aint \u548c float \u957f\u5ea6\u76f8\u540c\uff0c\u90fd\u662f 4 \u5b57\u8282 \uff0c\u4f46\u4e3a\u4ec0\u4e48 float \u7684\u53d6\u503c\u8303\u56f4\u8fdc\u5927\u4e8e int \uff1f\u8fd9\u975e\u5e38\u53cd\u76f4\u89c9\uff0c\u56e0\u4e3a\u6309\u7406\u8bf4 float \u9700\u8981\u8868\u793a\u5c0f\u6570\uff0c\u53d6\u503c\u8303\u56f4\u5e94\u8be5\u53d8\u5c0f\u624d\u5bf9\u3002

        \u5b9e\u9645\u4e0a\uff0c\u8fd9\u662f\u56e0\u4e3a\u6d6e\u70b9\u6570 float \u91c7\u7528\u4e86\u4e0d\u540c\u7684\u8868\u793a\u65b9\u5f0f\u3002\u8bb0\u4e00\u4e2a 32 \u6bd4\u7279\u957f\u5ea6\u7684\u4e8c\u8fdb\u5236\u6570\u4e3a\uff1a

        \\[ b_{31} b_{30} b_{29} \\ldots b_2 b_1 b_0 \\]

        \u6839\u636e IEEE 754 \u6807\u51c6\uff0c32-bit \u957f\u5ea6\u7684 float \u7531\u4ee5\u4e0b\u4e09\u4e2a\u90e8\u5206\u6784\u6210\u3002

        • \u7b26\u53f7\u4f4d \\(\\mathrm{S}\\) \uff1a\u5360 1 \u4f4d \uff0c\u5bf9\u5e94 \\(b_{31}\\) \u3002
        • \u6307\u6570\u4f4d \\(\\mathrm{E}\\) \uff1a\u5360 8 \u4f4d \uff0c\u5bf9\u5e94 \\(b_{30} b_{29} \\ldots b_{23}\\) \u3002
        • \u5206\u6570\u4f4d \\(\\mathrm{N}\\) \uff1a\u5360 23 \u4f4d \uff0c\u5bf9\u5e94 \\(b_{22} b_{21} \\ldots b_0\\) \u3002

        \u4e8c\u8fdb\u5236\u6570 float \u5bf9\u5e94\u503c\u7684\u8ba1\u7b97\u65b9\u6cd5\u4e3a\uff1a

        \\[ \\text {val} = (-1)^{b_{31}} \\times 2^{\\left(b_{30} b_{29} \\ldots b_{23}\\right)_2-127} \\times\\left(1 . b_{22} b_{21} \\ldots b_0\\right)_2 \\]

        \u8f6c\u5316\u5230\u5341\u8fdb\u5236\u4e0b\u7684\u8ba1\u7b97\u516c\u5f0f\u4e3a\uff1a

        \\[ \\text {val}=(-1)^{\\mathrm{S}} \\times 2^{\\mathrm{E} -127} \\times (1 + \\mathrm{N}) \\]

        \u5176\u4e2d\u5404\u9879\u7684\u53d6\u503c\u8303\u56f4\u4e3a\uff1a

        \\[ \\begin{aligned} \\mathrm{S} \\in & \\{ 0, 1\\}, \\quad \\mathrm{E} \\in \\{ 1, 2, \\dots, 254 \\} \\newline (1 + \\mathrm{N}) = & (1 + \\sum_{i=1}^{23} b_{23-i} 2^{-i}) \\subset [1, 2 - 2^{-23}] \\end{aligned} \\]

        \u56fe 3-5 \u00a0 IEEE 754 \u6807\u51c6\u4e0b\u7684 float \u7684\u8ba1\u7b97\u793a\u4f8b

        \u89c2\u5bdf\u56fe 3-5 \uff0c\u7ed9\u5b9a\u4e00\u4e2a\u793a\u4f8b\u6570\u636e \\(\\mathrm{S} = 0\\) \uff0c \\(\\mathrm{E} = 124\\) \uff0c\\(\\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\\) \uff0c\u5219\u6709\uff1a

        \\[ \\text { val } = (-1)^0 \\times 2^{124 - 127} \\times (1 + 0.375) = 0.171875 \\]

        \u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u56de\u7b54\u6700\u521d\u7684\u95ee\u9898\uff1afloat \u7684\u8868\u793a\u65b9\u5f0f\u5305\u542b\u6307\u6570\u4f4d\uff0c\u5bfc\u81f4\u5176\u53d6\u503c\u8303\u56f4\u8fdc\u5927\u4e8e int \u3002\u6839\u636e\u4ee5\u4e0a\u8ba1\u7b97\uff0cfloat \u53ef\u8868\u793a\u7684\u6700\u5927\u6b63\u6570\u4e3a \\(2^{254 - 127} \\times (2 - 2^{-23}) \\approx 3.4 \\times 10^{38}\\) \uff0c\u5207\u6362\u7b26\u53f7\u4f4d\u4fbf\u53ef\u5f97\u5230\u6700\u5c0f\u8d1f\u6570\u3002

        \u5c3d\u7ba1\u6d6e\u70b9\u6570 float \u6269\u5c55\u4e86\u53d6\u503c\u8303\u56f4\uff0c\u4f46\u5176\u526f\u4f5c\u7528\u662f\u727a\u7272\u4e86\u7cbe\u5ea6\u3002\u6574\u6570\u7c7b\u578b int \u5c06\u5168\u90e8 32 \u6bd4\u7279\u7528\u4e8e\u8868\u793a\u6570\u5b57\uff0c\u6570\u5b57\u662f\u5747\u5300\u5206\u5e03\u7684\uff1b\u800c\u7531\u4e8e\u6307\u6570\u4f4d\u7684\u5b58\u5728\uff0c\u6d6e\u70b9\u6570 float \u7684\u6570\u503c\u8d8a\u5927\uff0c\u76f8\u90bb\u4e24\u4e2a\u6570\u5b57\u4e4b\u95f4\u7684\u5dee\u503c\u5c31\u4f1a\u8d8b\u5411\u8d8a\u5927\u3002

        \u5982\u8868 3-2 \u6240\u793a\uff0c\u6307\u6570\u4f4d \\(\\mathrm{E} = 0\\) \u548c \\(\\mathrm{E} = 255\\) \u5177\u6709\u7279\u6b8a\u542b\u4e49\uff0c\u7528\u4e8e\u8868\u793a\u96f6\u3001\u65e0\u7a77\u5927\u3001\\(\\mathrm{NaN}\\) \u7b49\u3002

        \u8868 3-2 \u00a0 \u6307\u6570\u4f4d\u542b\u4e49

        \u6307\u6570\u4f4d E \u5206\u6570\u4f4d \\(\\mathrm{N} = 0\\) \u5206\u6570\u4f4d \\(\\mathrm{N} \\ne 0\\) \u8ba1\u7b97\u516c\u5f0f \\(0\\) \\(\\pm 0\\) \u6b21\u6b63\u89c4\u6570 \\((-1)^{\\mathrm{S}} \\times 2^{-126} \\times (0.\\mathrm{N})\\) \\(1, 2, \\dots, 254\\) \u6b63\u89c4\u6570 \u6b63\u89c4\u6570 \\((-1)^{\\mathrm{S}} \\times 2^{(\\mathrm{E} -127)} \\times (1.\\mathrm{N})\\) \\(255\\) \\(\\pm \\infty\\) \\(\\mathrm{NaN}\\)

        \u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6b21\u6b63\u89c4\u6570\u663e\u8457\u63d0\u5347\u4e86\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u3002\u6700\u5c0f\u6b63\u6b63\u89c4\u6570\u4e3a \\(2^{-126}\\) \uff0c\u6700\u5c0f\u6b63\u6b21\u6b63\u89c4\u6570\u4e3a \\(2^{-126} \\times 2^{-23}\\) \u3002

        \u53cc\u7cbe\u5ea6 double \u4e5f\u91c7\u7528\u7c7b\u4f3c\u4e8e float \u7684\u8868\u793a\u65b9\u6cd5\uff0c\u5728\u6b64\u4e0d\u505a\u8d58\u8ff0\u3002

        "},{"location":"chapter_data_structure/summary/","title":"3.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_data_structure/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u4ece\u903b\u8f91\u7ed3\u6784\u548c\u7269\u7406\u7ed3\u6784\u4e24\u4e2a\u89d2\u5ea6\u8fdb\u884c\u5206\u7c7b\u3002\u903b\u8f91\u7ed3\u6784\u63cf\u8ff0\u4e86\u6570\u636e\u5143\u7d20\u4e4b\u95f4\u7684\u903b\u8f91\u5173\u7cfb\uff0c\u800c\u7269\u7406\u7ed3\u6784\u63cf\u8ff0\u4e86\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u65b9\u5f0f\u3002
        • \u5e38\u89c1\u7684\u903b\u8f91\u7ed3\u6784\u5305\u62ec\u7ebf\u6027\u3001\u6811\u72b6\u548c\u7f51\u72b6\u7b49\u3002\u901a\u5e38\u6211\u4eec\u6839\u636e\u903b\u8f91\u7ed3\u6784\u5c06\u6570\u636e\u7ed3\u6784\u5206\u4e3a\u7ebf\u6027\uff08\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\uff09\u548c\u975e\u7ebf\u6027\uff08\u6811\u3001\u56fe\u3001\u5806\uff09\u4e24\u79cd\u3002\u54c8\u5e0c\u8868\u7684\u5b9e\u73b0\u53ef\u80fd\u540c\u65f6\u5305\u542b\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u548c\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002
        • \u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u6570\u636e\u88ab\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u3002\u6bcf\u4e2a\u5185\u5b58\u7a7a\u95f4\u90fd\u62e5\u6709\u5bf9\u5e94\u7684\u5185\u5b58\u5730\u5740\uff0c\u7a0b\u5e8f\u901a\u8fc7\u8fd9\u4e9b\u5185\u5b58\u5730\u5740\u8bbf\u95ee\u6570\u636e\u3002
        • \u7269\u7406\u7ed3\u6784\u4e3b\u8981\u5206\u4e3a\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\uff08\u6570\u7ec4\uff09\u548c\u5206\u6563\u7a7a\u95f4\u5b58\u50a8\uff08\u94fe\u8868\uff09\u3002\u6240\u6709\u6570\u636e\u7ed3\u6784\u90fd\u662f\u7531\u6570\u7ec4\u3001\u94fe\u8868\u6216\u4e24\u8005\u7684\u7ec4\u5408\u5b9e\u73b0\u7684\u3002
        • \u8ba1\u7b97\u673a\u4e2d\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u5305\u62ec\u6574\u6570 byte\u3001short\u3001int\u3001long \uff0c\u6d6e\u70b9\u6570 float\u3001double \uff0c\u5b57\u7b26 char \u548c\u5e03\u5c14 bool \u3002\u5b83\u4eec\u7684\u53d6\u503c\u8303\u56f4\u53d6\u51b3\u4e8e\u5360\u7528\u7a7a\u95f4\u5927\u5c0f\u548c\u8868\u793a\u65b9\u5f0f\u3002
        • \u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801\u662f\u5728\u8ba1\u7b97\u673a\u4e2d\u7f16\u7801\u6570\u5b57\u7684\u4e09\u79cd\u65b9\u6cd5\uff0c\u5b83\u4eec\u4e4b\u95f4\u53ef\u4ee5\u76f8\u4e92\u8f6c\u6362\u3002\u6574\u6570\u7684\u539f\u7801\u7684\u6700\u9ad8\u4f4d\u662f\u7b26\u53f7\u4f4d\uff0c\u5176\u4f59\u4f4d\u662f\u6570\u5b57\u7684\u503c\u3002
        • \u6574\u6570\u5728\u8ba1\u7b97\u673a\u4e2d\u662f\u4ee5\u8865\u7801\u7684\u5f62\u5f0f\u5b58\u50a8\u7684\u3002\u5728\u8865\u7801\u8868\u793a\u4e0b\uff0c\u8ba1\u7b97\u673a\u53ef\u4ee5\u5bf9\u6b63\u6570\u548c\u8d1f\u6570\u7684\u52a0\u6cd5\u4e00\u89c6\u540c\u4ec1\uff0c\u4e0d\u9700\u8981\u4e3a\u51cf\u6cd5\u64cd\u4f5c\u5355\u72ec\u8bbe\u8ba1\u7279\u6b8a\u7684\u786c\u4ef6\u7535\u8def\uff0c\u5e76\u4e14\u4e0d\u5b58\u5728\u6b63\u8d1f\u96f6\u6b67\u4e49\u7684\u95ee\u9898\u3002
        • \u6d6e\u70b9\u6570\u7684\u7f16\u7801\u7531 1 \u4f4d\u7b26\u53f7\u4f4d\u30018 \u4f4d\u6307\u6570\u4f4d\u548c 23 \u4f4d\u5206\u6570\u4f4d\u6784\u6210\u3002\u7531\u4e8e\u5b58\u5728\u6307\u6570\u4f4d\uff0c\u56e0\u6b64\u6d6e\u70b9\u6570\u7684\u53d6\u503c\u8303\u56f4\u8fdc\u5927\u4e8e\u6574\u6570\uff0c\u4ee3\u4ef7\u662f\u727a\u7272\u4e86\u7cbe\u5ea6\u3002
        • ASCII \u7801\u662f\u6700\u65e9\u51fa\u73b0\u7684\u82f1\u6587\u5b57\u7b26\u96c6\uff0c\u957f\u5ea6\u4e3a 1 \u5b57\u8282\uff0c\u5171\u6536\u5f55 127 \u4e2a\u5b57\u7b26\u3002GBK \u5b57\u7b26\u96c6\u662f\u5e38\u7528\u7684\u4e2d\u6587\u5b57\u7b26\u96c6\uff0c\u5171\u6536\u5f55\u4e24\u4e07\u591a\u4e2a\u6c49\u5b57\u3002Unicode \u81f4\u529b\u4e8e\u63d0\u4f9b\u4e00\u4e2a\u5b8c\u6574\u7684\u5b57\u7b26\u96c6\u6807\u51c6\uff0c\u6536\u5f55\u4e16\u754c\u4e0a\u5404\u79cd\u8bed\u8a00\u7684\u5b57\u7b26\uff0c\u4ece\u800c\u89e3\u51b3\u7531\u4e8e\u5b57\u7b26\u7f16\u7801\u65b9\u6cd5\u4e0d\u4e00\u81f4\u800c\u5bfc\u81f4\u7684\u4e71\u7801\u95ee\u9898\u3002
        • UTF-8 \u662f\u6700\u53d7\u6b22\u8fce\u7684 Unicode \u7f16\u7801\u65b9\u6cd5\uff0c\u901a\u7528\u6027\u975e\u5e38\u597d\u3002\u5b83\u662f\u4e00\u79cd\u53d8\u957f\u7684\u7f16\u7801\u65b9\u6cd5\uff0c\u5177\u6709\u5f88\u597d\u7684\u6269\u5c55\u6027\uff0c\u6709\u6548\u63d0\u5347\u4e86\u5b58\u50a8\u7a7a\u95f4\u7684\u4f7f\u7528\u6548\u7387\u3002UTF-16 \u548c UTF-32 \u662f\u7b49\u957f\u7684\u7f16\u7801\u65b9\u6cd5\u3002\u5728\u7f16\u7801\u4e2d\u6587\u65f6\uff0cUTF-16 \u5360\u7528\u7684\u7a7a\u95f4\u6bd4 UTF-8 \u66f4\u5c0f\u3002Java \u548c C# \u7b49\u7f16\u7a0b\u8bed\u8a00\u9ed8\u8ba4\u4f7f\u7528 UTF-16 \u7f16\u7801\u3002
        "},{"location":"chapter_data_structure/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u4e3a\u4ec0\u4e48\u54c8\u5e0c\u8868\u540c\u65f6\u5305\u542b\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u548c\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff1f

        \u54c8\u5e0c\u8868\u5e95\u5c42\u662f\u6570\u7ec4\uff0c\u800c\u4e3a\u4e86\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u4f7f\u7528\u201c\u94fe\u5f0f\u5730\u5740\u201d\uff08\u540e\u7eed\u201c\u54c8\u5e0c\u51b2\u7a81\u201d\u7ae0\u8282\u4f1a\u8bb2\uff09\uff1a\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u6876\u6307\u5411\u4e00\u4e2a\u94fe\u8868\uff0c\u5f53\u94fe\u8868\u957f\u5ea6\u8d85\u8fc7\u4e00\u5b9a\u9608\u503c\u65f6\uff0c\u53c8\u53ef\u80fd\u88ab\u8f6c\u5316\u4e3a\u6811\uff08\u901a\u5e38\u4e3a\u7ea2\u9ed1\u6811\uff09\u3002

        \u4ece\u5b58\u50a8\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u54c8\u5e0c\u8868\u7684\u5e95\u5c42\u662f\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e00\u4e2a\u6876\u69fd\u4f4d\u53ef\u80fd\u5305\u542b\u4e00\u4e2a\u503c\uff0c\u4e5f\u53ef\u80fd\u5305\u542b\u4e00\u4e2a\u94fe\u8868\u6216\u4e00\u68f5\u6811\u3002\u56e0\u6b64\uff0c\u54c8\u5e0c\u8868\u53ef\u80fd\u540c\u65f6\u5305\u542b\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff08\u6570\u7ec4\u3001\u94fe\u8868\uff09\u548c\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff08\u6811\uff09\u3002

        Q\uff1achar \u7c7b\u578b\u7684\u957f\u5ea6\u662f 1 \u5b57\u8282\u5417\uff1f

        char \u7c7b\u578b\u7684\u957f\u5ea6\u7531\u7f16\u7a0b\u8bed\u8a00\u91c7\u7528\u7684\u7f16\u7801\u65b9\u6cd5\u51b3\u5b9a\u3002\u4f8b\u5982\uff0cJava\u3001JavaScript\u3001TypeScript\u3001C# \u90fd\u91c7\u7528 UTF-16 \u7f16\u7801\uff08\u4fdd\u5b58 Unicode \u7801\u70b9\uff09\uff0c\u56e0\u6b64 char \u7c7b\u578b\u7684\u957f\u5ea6\u4e3a 2 \u5b57\u8282\u3002

        Q\uff1a\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u4e5f\u79f0\u201c\u9759\u6001\u6570\u636e\u7ed3\u6784\u201d \u662f\u5426\u6709\u6b67\u4e49\uff1f\u6808\u4e5f\u53ef\u4ee5\u8fdb\u884c\u51fa\u6808\u548c\u5165\u6808\u7b49\u64cd\u4f5c\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u90fd\u662f\u201c\u52a8\u6001\u201d\u7684\u3002

        \u6808\u786e\u5b9e\u53ef\u4ee5\u5b9e\u73b0\u52a8\u6001\u7684\u6570\u636e\u64cd\u4f5c\uff0c\u4f46\u6570\u636e\u7ed3\u6784\u4ecd\u7136\u662f\u201c\u9759\u6001\u201d\uff08\u957f\u5ea6\u4e0d\u53ef\u53d8\uff09\u7684\u3002\u5c3d\u7ba1\u57fa\u4e8e\u6570\u7ec4\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u52a8\u6001\u5730\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\uff0c\u4f46\u5b83\u4eec\u7684\u5bb9\u91cf\u662f\u56fa\u5b9a\u7684\u3002\u5982\u679c\u6570\u636e\u91cf\u8d85\u51fa\u4e86\u9884\u5206\u914d\u7684\u5927\u5c0f\uff0c\u5c31\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u66f4\u5927\u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u65e7\u6570\u7ec4\u7684\u5185\u5bb9\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002

        Q\uff1a\u5728\u6784\u5efa\u6808\uff08\u961f\u5217\uff09\u7684\u65f6\u5019\uff0c\u672a\u6307\u5b9a\u5b83\u7684\u5927\u5c0f\uff0c\u4e3a\u4ec0\u4e48\u5b83\u4eec\u662f\u201c\u9759\u6001\u6570\u636e\u7ed3\u6784\u201d\u5462\uff1f

        \u5728\u9ad8\u7ea7\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u65e0\u987b\u4eba\u5de5\u6307\u5b9a\u6808\uff08\u961f\u5217\uff09\u7684\u521d\u59cb\u5bb9\u91cf\uff0c\u8fd9\u4e2a\u5de5\u4f5c\u7531\u7c7b\u5185\u90e8\u81ea\u52a8\u5b8c\u6210\u3002\u4f8b\u5982\uff0cJava \u7684 ArrayList \u7684\u521d\u59cb\u5bb9\u91cf\u901a\u5e38\u4e3a 10\u3002\u53e6\u5916\uff0c\u6269\u5bb9\u64cd\u4f5c\u4e5f\u662f\u81ea\u52a8\u5b9e\u73b0\u7684\u3002\u8be6\u89c1\u540e\u7eed\u7684\u201c\u5217\u8868\u201d\u7ae0\u8282\u3002

        Q\uff1a\u539f\u7801\u8f6c\u8865\u7801\u7684\u65b9\u6cd5\u662f\u201c\u5148\u53d6\u53cd\u540e\u52a0 1\u201d\uff0c\u90a3\u4e48\u8865\u7801\u8f6c\u539f\u7801\u5e94\u8be5\u662f\u9006\u8fd0\u7b97\u201c\u5148\u51cf 1 \u540e\u53d6\u53cd\u201d\uff0c\u800c\u8865\u7801\u8f6c\u539f\u7801\u4e5f\u4e00\u6837\u53ef\u4ee5\u901a\u8fc7\u201c\u5148\u53d6\u53cd\u540e\u52a0 1\u201d\u5f97\u5230\uff0c\u8fd9\u662f\u4e3a\u4ec0\u4e48\u5462\uff1f

        \u8fd9\u662f\u56e0\u4e3a\u539f\u7801\u548c\u8865\u7801\u7684\u76f8\u4e92\u8f6c\u6362\u5b9e\u9645\u4e0a\u662f\u8ba1\u7b97\u201c\u8865\u6570\u201d\u7684\u8fc7\u7a0b\u3002\u6211\u4eec\u5148\u7ed9\u51fa\u8865\u6570\u7684\u5b9a\u4e49\uff1a\u5047\u8bbe \\(a + b = c\\) \uff0c\u90a3\u4e48\u6211\u4eec\u79f0 \\(a\\) \u662f \\(b\\) \u5230 \\(c\\) \u7684\u8865\u6570\uff0c\u53cd\u4e4b\u4e5f\u79f0 \\(b\\) \u662f \\(a\\) \u5230 \\(c\\) \u7684\u8865\u6570\u3002

        \u7ed9\u5b9a\u4e00\u4e2a \\(n = 4\\) \u4f4d\u957f\u5ea6\u7684\u4e8c\u8fdb\u5236\u6570 \\(0010\\) \uff0c\u5982\u679c\u5c06\u8fd9\u4e2a\u6570\u5b57\u770b\u4f5c\u539f\u7801\uff08\u4e0d\u8003\u8651\u7b26\u53f7\u4f4d\uff09\uff0c\u90a3\u4e48\u5b83\u7684\u8865\u7801\u9700\u901a\u8fc7\u201c\u5148\u53d6\u53cd\u540e\u52a0 1\u201d\u5f97\u5230\uff1a

        \\[ 0010 \\rightarrow 1101 \\rightarrow 1110 \\]

        \u6211\u4eec\u4f1a\u53d1\u73b0\uff0c\u539f\u7801\u548c\u8865\u7801\u7684\u548c\u662f \\(0010 + 1110 = 10000\\) \uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u8865\u7801 \\(1110\\) \u662f\u539f\u7801 \\(0010\\) \u5230 \\(10000\\) \u7684\u201c\u8865\u6570\u201d\u3002\u8fd9\u610f\u5473\u7740\u4e0a\u8ff0\u201c\u5148\u53d6\u53cd\u540e\u52a0 1\u201d\u5b9e\u9645\u4e0a\u662f\u8ba1\u7b97\u5230 \\(10000\\) \u7684\u8865\u6570\u7684\u8fc7\u7a0b\u3002

        \u90a3\u4e48\uff0c\u8865\u7801 \\(1110\\) \u5230 \\(10000\\) \u7684\u201c\u8865\u6570\u201d\u662f\u591a\u5c11\u5462\uff1f\u6211\u4eec\u4f9d\u7136\u53ef\u4ee5\u7528\u201c\u5148\u53d6\u53cd\u540e\u52a0 1\u201d\u5f97\u5230\u5b83\uff1a

        \\[ 1110 \\rightarrow 0001 \\rightarrow 0010 \\]

        \u6362\u53e5\u8bdd\u8bf4\uff0c\u539f\u7801\u548c\u8865\u7801\u4e92\u4e3a\u5bf9\u65b9\u5230 \\(10000\\) \u7684\u201c\u8865\u6570\u201d\uff0c\u56e0\u6b64\u201c\u539f\u7801\u8f6c\u8865\u7801\u201d\u548c\u201c\u8865\u7801\u8f6c\u539f\u7801\u201d\u53ef\u4ee5\u7528\u76f8\u540c\u7684\u64cd\u4f5c\uff08\u5148\u53d6\u53cd\u540e\u52a0 1 \uff09\u5b9e\u73b0\u3002

        \u5f53\u7136\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u7528\u9006\u8fd0\u7b97\u6765\u6c42\u8865\u7801 \\(1110\\) \u7684\u539f\u7801\uff0c\u5373\u201c\u5148\u51cf 1 \u540e\u53d6\u53cd\u201d\uff1a

        \\[ 1110 \\rightarrow 1101 \\rightarrow 0010 \\]

        \u603b\u7ed3\u6765\u770b\uff0c\u201c\u5148\u53d6\u53cd\u540e\u52a0 1\u201d\u548c\u201c\u5148\u51cf 1 \u540e\u53d6\u53cd\u201d\u8fd9\u4e24\u79cd\u8fd0\u7b97\u90fd\u662f\u5728\u8ba1\u7b97\u5230 \\(10000\\) \u7684\u8865\u6570\uff0c\u5b83\u4eec\u662f\u7b49\u4ef7\u7684\u3002

        \u672c\u8d28\u4e0a\u770b\uff0c\u201c\u53d6\u53cd\u201d\u64cd\u4f5c\u5b9e\u9645\u4e0a\u662f\u6c42\u5230 \\(1111\\) \u7684\u8865\u6570\uff08\u56e0\u4e3a\u6052\u6709 \u539f\u7801 + \u53cd\u7801 = 1111\uff09\uff1b\u800c\u5728\u53cd\u7801\u57fa\u7840\u4e0a\u518d\u52a0 1 \u5f97\u5230\u7684\u8865\u7801\uff0c\u5c31\u662f\u5230 \\(10000\\) \u7684\u8865\u6570\u3002

        \u4e0a\u8ff0\u4ee5 \\(n = 4\\) \u4e3a\u4f8b\uff0c\u5176\u53ef\u88ab\u63a8\u5e7f\u81f3\u4efb\u610f\u4f4d\u6570\u7684\u4e8c\u8fdb\u5236\u6570\u3002

        "},{"location":"chapter_divide_and_conquer/","title":"\u7b2c 12 \u7ae0 \u00a0 \u5206\u6cbb","text":"

        Abstract

        \u96be\u9898\u88ab\u9010\u5c42\u62c6\u89e3\uff0c\u6bcf\u4e00\u6b21\u7684\u62c6\u89e3\u90fd\u4f7f\u5b83\u53d8\u5f97\u66f4\u4e3a\u7b80\u5355\u3002

        \u5206\u800c\u6cbb\u4e4b\u63ed\u793a\u4e86\u4e00\u4e2a\u91cd\u8981\u7684\u4e8b\u5b9e\uff1a\u4ece\u7b80\u5355\u505a\u8d77\uff0c\u4e00\u5207\u90fd\u4e0d\u518d\u590d\u6742\u3002

        "},{"location":"chapter_divide_and_conquer/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 12.1 \u00a0 \u5206\u6cbb\u7b97\u6cd5
        • 12.2 \u00a0 \u5206\u6cbb\u641c\u7d22\u7b56\u7565
        • 12.3 \u00a0 \u6784\u5efa\u6811\u95ee\u9898
        • 12.4 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898
        • 12.5 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_divide_and_conquer/binary_search_recur/","title":"12.2 \u00a0 \u5206\u6cbb\u641c\u7d22\u7b56\u7565","text":"

        \u6211\u4eec\u5df2\u7ecf\u5b66\u8fc7\uff0c\u641c\u7d22\u7b97\u6cd5\u5206\u4e3a\u4e24\u5927\u7c7b\u3002

        • \u66b4\u529b\u641c\u7d22\uff1a\u5b83\u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784\u5b9e\u73b0\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002
        • \u81ea\u9002\u5e94\u641c\u7d22\uff1a\u5b83\u5229\u7528\u7279\u6709\u7684\u6570\u636e\u7ec4\u7ec7\u5f62\u5f0f\u6216\u5148\u9a8c\u4fe1\u606f\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe\u5230 \\(O(\\log n)\\) \u751a\u81f3 \\(O(1)\\) \u3002

        \u5b9e\u9645\u4e0a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u7684\u641c\u7d22\u7b97\u6cd5\u901a\u5e38\u662f\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u5b9e\u73b0\u7684\uff0c\u4f8b\u5982\u4e8c\u5206\u67e5\u627e\u548c\u6811\u3002

        • \u4e8c\u5206\u67e5\u627e\u7684\u6bcf\u4e00\u6b65\u90fd\u5c06\u95ee\u9898\uff08\u5728\u6570\u7ec4\u4e2d\u641c\u7d22\u76ee\u6807\u5143\u7d20\uff09\u5206\u89e3\u4e3a\u4e00\u4e2a\u5c0f\u95ee\u9898\uff08\u5728\u6570\u7ec4\u7684\u4e00\u534a\u4e2d\u641c\u7d22\u76ee\u6807\u5143\u7d20\uff09\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4e00\u76f4\u6301\u7eed\u5230\u6570\u7ec4\u4e3a\u7a7a\u6216\u627e\u5230\u76ee\u6807\u5143\u7d20\u4e3a\u6b62\u3002
        • \u6811\u662f\u5206\u6cbb\u601d\u60f3\u7684\u4ee3\u8868\uff0c\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u3001AVL \u6811\u3001\u5806\u7b49\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u5404\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7686\u4e3a \\(O(\\log n)\\) \u3002

        \u4e8c\u5206\u67e5\u627e\u7684\u5206\u6cbb\u7b56\u7565\u5982\u4e0b\u6240\u793a\u3002

        • \u95ee\u9898\u53ef\u4ee5\u5206\u89e3\uff1a\u4e8c\u5206\u67e5\u627e\u9012\u5f52\u5730\u5c06\u539f\u95ee\u9898\uff08\u5728\u6570\u7ec4\u4e2d\u8fdb\u884c\u67e5\u627e\uff09\u5206\u89e3\u4e3a\u5b50\u95ee\u9898\uff08\u5728\u6570\u7ec4\u7684\u4e00\u534a\u4e2d\u8fdb\u884c\u67e5\u627e\uff09\uff0c\u8fd9\u662f\u901a\u8fc7\u6bd4\u8f83\u4e2d\u95f4\u5143\u7d20\u548c\u76ee\u6807\u5143\u7d20\u6765\u5b9e\u73b0\u7684\u3002
        • \u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff1a\u5728\u4e8c\u5206\u67e5\u627e\u4e2d\uff0c\u6bcf\u8f6e\u53ea\u5904\u7406\u4e00\u4e2a\u5b50\u95ee\u9898\uff0c\u5b83\u4e0d\u53d7\u5176\u4ed6\u5b50\u95ee\u9898\u7684\u5f71\u54cd\u3002
        • \u5b50\u95ee\u9898\u7684\u89e3\u65e0\u987b\u5408\u5e76\uff1a\u4e8c\u5206\u67e5\u627e\u65e8\u5728\u67e5\u627e\u4e00\u4e2a\u7279\u5b9a\u5143\u7d20\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u8fdb\u884c\u5408\u5e76\u3002\u5f53\u5b50\u95ee\u9898\u5f97\u5230\u89e3\u51b3\u65f6\uff0c\u539f\u95ee\u9898\u4e5f\u4f1a\u540c\u65f6\u5f97\u5230\u89e3\u51b3\u3002

        \u5206\u6cbb\u80fd\u591f\u63d0\u5347\u641c\u7d22\u6548\u7387\uff0c\u672c\u8d28\u4e0a\u662f\u56e0\u4e3a\u66b4\u529b\u641c\u7d22\u6bcf\u8f6e\u53ea\u80fd\u6392\u9664\u4e00\u4e2a\u9009\u9879\uff0c\u800c\u5206\u6cbb\u641c\u7d22\u6bcf\u8f6e\u53ef\u4ee5\u6392\u9664\u4e00\u534a\u9009\u9879\u3002

        "},{"location":"chapter_divide_and_conquer/binary_search_recur/#1","title":"1. \u00a0 \u57fa\u4e8e\u5206\u6cbb\u5b9e\u73b0\u4e8c\u5206\u67e5\u627e","text":"

        \u5728\u4e4b\u524d\u7684\u7ae0\u8282\u4e2d\uff0c\u4e8c\u5206\u67e5\u627e\u662f\u57fa\u4e8e\u9012\u63a8\uff08\u8fed\u4ee3\uff09\u5b9e\u73b0\u7684\u3002\u73b0\u5728\u6211\u4eec\u57fa\u4e8e\u5206\u6cbb\uff08\u9012\u5f52\uff09\u6765\u5b9e\u73b0\u5b83\u3002

        Question

        \u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6709\u5e8f\u6570\u7ec4 nums \uff0c\u5176\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u662f\u552f\u4e00\u7684\uff0c\u8bf7\u67e5\u627e\u5143\u7d20 target \u3002

        \u4ece\u5206\u6cbb\u89d2\u5ea6\uff0c\u6211\u4eec\u5c06\u641c\u7d22\u533a\u95f4 \\([i, j]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u8bb0\u4e3a \\(f(i, j)\\) \u3002

        \u4ee5\u539f\u95ee\u9898 \\(f(0, n-1)\\) \u4e3a\u8d77\u59cb\u70b9\uff0c\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u4e8c\u5206\u67e5\u627e\u3002

        1. \u8ba1\u7b97\u641c\u7d22\u533a\u95f4 \\([i, j]\\) \u7684\u4e2d\u70b9 \\(m\\) \uff0c\u6839\u636e\u5b83\u6392\u9664\u4e00\u534a\u641c\u7d22\u533a\u95f4\u3002
        2. \u9012\u5f52\u6c42\u89e3\u89c4\u6a21\u51cf\u5c0f\u4e00\u534a\u7684\u5b50\u95ee\u9898\uff0c\u53ef\u80fd\u4e3a \\(f(i, m-1)\\) \u6216 \\(f(m+1, j)\\) \u3002
        3. \u5faa\u73af\u7b2c 1. \u6b65\u548c\u7b2c 2. \u6b65\uff0c\u76f4\u81f3\u627e\u5230 target \u6216\u533a\u95f4\u4e3a\u7a7a\u65f6\u8fd4\u56de\u3002

        \u56fe 12-4 \u5c55\u793a\u4e86\u5728\u6570\u7ec4\u4e2d\u4e8c\u5206\u67e5\u627e\u5143\u7d20 \\(6\\) \u7684\u5206\u6cbb\u8fc7\u7a0b\u3002

        \u56fe 12-4 \u00a0 \u4e8c\u5206\u67e5\u627e\u7684\u5206\u6cbb\u8fc7\u7a0b

        \u5728\u5b9e\u73b0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 dfs() \u6765\u6c42\u89e3\u95ee\u9898 \\(f(i, j)\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_recur.py
        def dfs(nums: list[int], target: int, i: int, j: int) -> int:\n    \"\"\"\u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j)\"\"\"\n    # \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n    if i > j:\n        return -1\n    # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    m = (i + j) // 2\n    if nums[m] < target:\n        # \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n        return dfs(nums, target, m + 1, j)\n    elif nums[m] > target:\n        # \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n        return dfs(nums, target, i, m - 1)\n    else:\n        # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        return m\n\ndef binary_search(nums: list[int], target: int) -> int:\n    \"\"\"\u4e8c\u5206\u67e5\u627e\"\"\"\n    n = len(nums)\n    # \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n    return dfs(nums, target, 0, n - 1)\n
        binary_search_recur.cpp
        /* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(vector<int> &nums, int target, int i, int j) {\n    // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n    if (i > j) {\n        return -1;\n    }\n    // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    int m = (i + j) / 2;\n    if (nums[m] < target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        return m;\n    }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(vector<int> &nums, int target) {\n    int n = nums.size();\n    // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
        binary_search_recur.java
        /* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(int[] nums, int target, int i, int j) {\n    // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n    if (i > j) {\n        return -1;\n    }\n    // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    int m = (i + j) / 2;\n    if (nums[m] < target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        return m;\n    }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(int[] nums, int target) {\n    int n = nums.length;\n    // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
        binary_search_recur.cs
        /* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint DFS(int[] nums, int target, int i, int j) {\n    // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n    if (i > j) {\n        return -1;\n    }\n    // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    int m = (i + j) / 2;\n    if (nums[m] < target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n        return DFS(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n        return DFS(nums, target, i, m - 1);\n    } else {\n        // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        return m;\n    }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nint BinarySearch(int[] nums, int target) {\n    int n = nums.Length;\n    // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n    return DFS(nums, target, 0, n - 1);\n}\n
        binary_search_recur.go
        /* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunc dfs(nums []int, target, i, j int) int {\n    // \u5982\u679c\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u6ca1\u6709\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n    if i > j {\n        return -1\n    }\n    //    \u8ba1\u7b97\u7d22\u5f15\u4e2d\u70b9\n    m := i + ((j - i) >> 1)\n    //\u5224\u65ad\u4e2d\u70b9\u4e0e\u76ee\u6807\u5143\u7d20\u5927\u5c0f\n    if nums[m] < target {\n        // \u5c0f\u4e8e\u5219\u9012\u5f52\u53f3\u534a\u6570\u7ec4\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n        return dfs(nums, target, m+1, j)\n    } else if nums[m] > target {\n        // \u5927\u4e8e\u5219\u9012\u5f52\u5de6\u534a\u6570\u7ec4\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n        return dfs(nums, target, i, m-1)\n    } else {\n        // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        return m\n    }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nfunc binarySearch(nums []int, target int) int {\n    n := len(nums)\n    return dfs(nums, target, 0, n-1)\n}\n
        binary_search_recur.swift
        /* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunc dfs(nums: [Int], target: Int, i: Int, j: Int) -> Int {\n    // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n    if i > j {\n        return -1\n    }\n    // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    let m = (i + j) / 2\n    if nums[m] < target {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n        return dfs(nums: nums, target: target, i: m + 1, j: j)\n    } else if nums[m] > target {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n        return dfs(nums: nums, target: target, i: i, j: m - 1)\n    } else {\n        // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        return m\n    }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nfunc binarySearch(nums: [Int], target: Int) -> Int {\n    // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n    dfs(nums: nums, target: target, i: nums.startIndex, j: nums.endIndex - 1)\n}\n
        binary_search_recur.js
        /* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunction dfs(nums, target, i, j) {\n    // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n    if (i > j) {\n        return -1;\n    }\n    // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    const m = i + ((j - i) >> 1);\n    if (nums[m] < target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        return m;\n    }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nfunction binarySearch(nums, target) {\n    const n = nums.length;\n    // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
        binary_search_recur.ts
        /* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunction dfs(nums: number[], target: number, i: number, j: number): number {\n    // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n    if (i > j) {\n        return -1;\n    }\n    // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    const m = i + ((j - i) >> 1);\n    if (nums[m] < target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        return m;\n    }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nfunction binarySearch(nums: number[], target: number): number {\n    const n = nums.length;\n    // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
        binary_search_recur.dart
        /* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(List<int> nums, int target, int i, int j) {\n  // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n  if (i > j) {\n    return -1;\n  }\n  // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n  int m = (i + j) ~/ 2;\n  if (nums[m] < target) {\n    // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n    return dfs(nums, target, m + 1, j);\n  } else if (nums[m] > target) {\n    // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n    return dfs(nums, target, i, m - 1);\n  } else {\n    // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n    return m;\n  }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(List<int> nums, int target) {\n  int n = nums.length;\n  // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n  return dfs(nums, target, 0, n - 1);\n}\n
        binary_search_recur.rs
        /* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfn dfs(nums: &[i32], target: i32, i: i32, j: i32) -> i32 {\n    // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n    if i > j {\n        return -1;\n    }\n    let m: i32 = i + (j - i) / 2;\n    if nums[m as usize] < target {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if nums[m as usize] > target {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        return m;\n    }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nfn binary_search(nums: &[i32], target: i32) -> i32 {\n    let n = nums.len() as i32;\n    // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n    dfs(nums, target, 0, n - 1)\n}\n
        binary_search_recur.c
        /* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(int nums[], int target, int i, int j) {\n    // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n    if (i > j) {\n        return -1;\n    }\n    // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    int m = (i + j) / 2;\n    if (nums[m] < target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        return m;\n    }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(int nums[], int target, int numsSize) {\n    int n = numsSize;\n    // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
        binary_search_recur.kt
        /* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfun dfs(\n    nums: IntArray,\n    target: Int,\n    i: Int,\n    j: Int\n): Int {\n    // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n    if (i > j) {\n        return -1\n    }\n    // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    val m = (i + j) / 2\n    return if (nums[m] < target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n        dfs(nums, target, m + 1, j)\n    } else if (nums[m] > target) {\n        // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n        dfs(nums, target, i, m - 1)\n    } else {\n        // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        m\n    }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nfun binarySearch(nums: IntArray, target: Int): Int {\n    val n = nums.size\n    // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n    return dfs(nums, target, 0, n - 1)\n}\n
        binary_search_recur.rb
        ### \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) ###\ndef dfs(nums, target, i, j)\n  # \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n  return -1 if i > j\n\n  # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n  m = (i + j) / 2\n\n  if nums[m] < target\n    # \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n    return dfs(nums, target, m + 1, j)\n  elsif nums[m] > target\n    # \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n    return dfs(nums, target, i, m - 1)\n  else\n    # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n    return m\n  end\nend\n\n### \u4e8c\u5206\u67e5\u627e ###\ndef binary_search(nums, target)\n  n = nums.length\n  # \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n  dfs(nums, target, 0, n - 1)\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/","title":"12.3 \u00a0 \u6784\u5efa\u4e8c\u53c9\u6811\u95ee\u9898","text":"

        Question

        \u7ed9\u5b9a\u4e00\u68f5\u4e8c\u53c9\u6811\u7684\u524d\u5e8f\u904d\u5386 preorder \u548c\u4e2d\u5e8f\u904d\u5386 inorder \uff0c\u8bf7\u4ece\u4e2d\u6784\u5efa\u4e8c\u53c9\u6811\uff0c\u8fd4\u56de\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\u3002\u5047\u8bbe\u4e8c\u53c9\u6811\u4e2d\u6ca1\u6709\u503c\u91cd\u590d\u7684\u8282\u70b9\uff08\u5982\u56fe 12-5 \u6240\u793a\uff09\u3002

        \u56fe 12-5 \u00a0 \u6784\u5efa\u4e8c\u53c9\u6811\u7684\u793a\u4f8b\u6570\u636e

        "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#1","title":"1. \u00a0 \u5224\u65ad\u662f\u5426\u4e3a\u5206\u6cbb\u95ee\u9898","text":"

        \u539f\u95ee\u9898\u5b9a\u4e49\u4e3a\u4ece preorder \u548c inorder \u6784\u5efa\u4e8c\u53c9\u6811\uff0c\u662f\u4e00\u4e2a\u5178\u578b\u7684\u5206\u6cbb\u95ee\u9898\u3002

        • \u95ee\u9898\u53ef\u4ee5\u5206\u89e3\uff1a\u4ece\u5206\u6cbb\u7684\u89d2\u5ea6\u5207\u5165\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u539f\u95ee\u9898\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\u3001\u6784\u5efa\u53f3\u5b50\u6811\uff0c\u52a0\u4e0a\u4e00\u6b65\u64cd\u4f5c\uff1a\u521d\u59cb\u5316\u6839\u8282\u70b9\u3002\u800c\u5bf9\u4e8e\u6bcf\u68f5\u5b50\u6811\uff08\u5b50\u95ee\u9898\uff09\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u590d\u7528\u4ee5\u4e0a\u5212\u5206\u65b9\u6cd5\uff0c\u5c06\u5176\u5212\u5206\u4e3a\u66f4\u5c0f\u7684\u5b50\u6811\uff08\u5b50\u95ee\u9898\uff09\uff0c\u76f4\u81f3\u8fbe\u5230\u6700\u5c0f\u5b50\u95ee\u9898\uff08\u7a7a\u5b50\u6811\uff09\u65f6\u7ec8\u6b62\u3002
        • \u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff1a\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u662f\u76f8\u4e92\u72ec\u7acb\u7684\uff0c\u5b83\u4eec\u4e4b\u95f4\u6ca1\u6709\u4ea4\u96c6\u3002\u5728\u6784\u5efa\u5de6\u5b50\u6811\u65f6\uff0c\u6211\u4eec\u53ea\u9700\u5173\u6ce8\u4e2d\u5e8f\u904d\u5386\u548c\u524d\u5e8f\u904d\u5386\u4e2d\u4e0e\u5de6\u5b50\u6811\u5bf9\u5e94\u7684\u90e8\u5206\u3002\u53f3\u5b50\u6811\u540c\u7406\u3002
        • \u5b50\u95ee\u9898\u7684\u89e3\u53ef\u4ee5\u5408\u5e76\uff1a\u4e00\u65e6\u5f97\u5230\u4e86\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\uff08\u5b50\u95ee\u9898\u7684\u89e3\uff09\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u5b83\u4eec\u94fe\u63a5\u5230\u6839\u8282\u70b9\u4e0a\uff0c\u5f97\u5230\u539f\u95ee\u9898\u7684\u89e3\u3002
        "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#2","title":"2. \u00a0 \u5982\u4f55\u5212\u5206\u5b50\u6811","text":"

        \u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u8fd9\u9053\u9898\u53ef\u4ee5\u4f7f\u7528\u5206\u6cbb\u6765\u6c42\u89e3\uff0c\u4f46\u5982\u4f55\u901a\u8fc7\u524d\u5e8f\u904d\u5386 preorder \u548c\u4e2d\u5e8f\u904d\u5386 inorder \u6765\u5212\u5206\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u5462\uff1f

        \u6839\u636e\u5b9a\u4e49\uff0cpreorder \u548c inorder \u90fd\u53ef\u4ee5\u5212\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\u3002

        • \u524d\u5e8f\u904d\u5386\uff1a[ \u6839\u8282\u70b9 | \u5de6\u5b50\u6811 | \u53f3\u5b50\u6811 ] \uff0c\u4f8b\u5982\u56fe 12-5 \u7684\u6811\u5bf9\u5e94 [ 3 | 9 | 2 1 7 ] \u3002
        • \u4e2d\u5e8f\u904d\u5386\uff1a[ \u5de6\u5b50\u6811 | \u6839\u8282\u70b9 \uff5c \u53f3\u5b50\u6811 ] \uff0c\u4f8b\u5982\u56fe 12-5 \u7684\u6811\u5bf9\u5e94 [ 9 | 3 | 1 2 7 ] \u3002

        \u4ee5\u4e0a\u56fe\u6570\u636e\u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u56fe 12-6 \u6240\u793a\u7684\u6b65\u9aa4\u5f97\u5230\u5212\u5206\u7ed3\u679c\u3002

        1. \u524d\u5e8f\u904d\u5386\u7684\u9996\u5143\u7d20 3 \u662f\u6839\u8282\u70b9\u7684\u503c\u3002
        2. \u67e5\u627e\u6839\u8282\u70b9 3 \u5728 inorder \u4e2d\u7684\u7d22\u5f15\uff0c\u5229\u7528\u8be5\u7d22\u5f15\u53ef\u5c06 inorder \u5212\u5206\u4e3a [ 9 | 3 \uff5c 1 2 7 ] \u3002
        3. \u6839\u636e inorder \u7684\u5212\u5206\u7ed3\u679c\uff0c\u6613\u5f97\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u7684\u8282\u70b9\u6570\u91cf\u5206\u522b\u4e3a 1 \u548c 3 \uff0c\u4ece\u800c\u53ef\u5c06 preorder \u5212\u5206\u4e3a [ 3 | 9 | 2 1 7 ] \u3002

        \u56fe 12-6 \u00a0 \u5728\u524d\u5e8f\u904d\u5386\u548c\u4e2d\u5e8f\u904d\u5386\u4e2d\u5212\u5206\u5b50\u6811

        "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#3","title":"3. \u00a0 \u57fa\u4e8e\u53d8\u91cf\u63cf\u8ff0\u5b50\u6811\u533a\u95f4","text":"

        \u6839\u636e\u4ee5\u4e0a\u5212\u5206\u65b9\u6cd5\uff0c\u6211\u4eec\u5df2\u7ecf\u5f97\u5230\u6839\u8282\u70b9\u3001\u5de6\u5b50\u6811\u3001\u53f3\u5b50\u6811\u5728 preorder \u548c inorder \u4e2d\u7684\u7d22\u5f15\u533a\u95f4\u3002\u800c\u4e3a\u4e86\u63cf\u8ff0\u8fd9\u4e9b\u7d22\u5f15\u533a\u95f4\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u51e0\u4e2a\u6307\u9488\u53d8\u91cf\u3002

        • \u5c06\u5f53\u524d\u6811\u7684\u6839\u8282\u70b9\u5728 preorder \u4e2d\u7684\u7d22\u5f15\u8bb0\u4e3a \\(i\\) \u3002
        • \u5c06\u5f53\u524d\u6811\u7684\u6839\u8282\u70b9\u5728 inorder \u4e2d\u7684\u7d22\u5f15\u8bb0\u4e3a \\(m\\) \u3002
        • \u5c06\u5f53\u524d\u6811\u5728 inorder \u4e2d\u7684\u7d22\u5f15\u533a\u95f4\u8bb0\u4e3a \\([l, r]\\) \u3002

        \u5982\u8868 12-1 \u6240\u793a\uff0c\u901a\u8fc7\u4ee5\u4e0a\u53d8\u91cf\u5373\u53ef\u8868\u793a\u6839\u8282\u70b9\u5728 preorder \u4e2d\u7684\u7d22\u5f15\uff0c\u4ee5\u53ca\u5b50\u6811\u5728 inorder \u4e2d\u7684\u7d22\u5f15\u533a\u95f4\u3002

        \u8868 12-1 \u00a0 \u6839\u8282\u70b9\u548c\u5b50\u6811\u5728\u524d\u5e8f\u904d\u5386\u548c\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u7d22\u5f15

        \u6839\u8282\u70b9\u5728 preorder \u4e2d\u7684\u7d22\u5f15 \u5b50\u6811\u5728 inorder \u4e2d\u7684\u7d22\u5f15\u533a\u95f4 \u5f53\u524d\u6811 \\(i\\) \\([l, r]\\) \u5de6\u5b50\u6811 \\(i + 1\\) \\([l, m-1]\\) \u53f3\u5b50\u6811 \\(i + 1 + (m - l)\\) \\([m+1, r]\\)

        \u8bf7\u6ce8\u610f\uff0c\u53f3\u5b50\u6811\u6839\u8282\u70b9\u7d22\u5f15\u4e2d\u7684 \\((m-l)\\) \u7684\u542b\u4e49\u662f\u201c\u5de6\u5b50\u6811\u7684\u8282\u70b9\u6570\u91cf\u201d\uff0c\u5efa\u8bae\u7ed3\u5408\u56fe 12-7 \u7406\u89e3\u3002

        \u56fe 12-7 \u00a0 \u6839\u8282\u70b9\u548c\u5de6\u53f3\u5b50\u6811\u7684\u7d22\u5f15\u533a\u95f4\u8868\u793a

        "},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#4","title":"4. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u4e3a\u4e86\u63d0\u5347\u67e5\u8be2 \\(m\\) \u7684\u6548\u7387\uff0c\u6211\u4eec\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868 hmap \u6765\u5b58\u50a8\u6570\u7ec4 inorder \u4e2d\u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby build_tree.py
        def dfs(\n    preorder: list[int],\n    inorder_map: dict[int, int],\n    i: int,\n    l: int,\n    r: int,\n) -> TreeNode | None:\n    \"\"\"\u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb\"\"\"\n    # \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n    if r - l < 0:\n        return None\n    # \u521d\u59cb\u5316\u6839\u8282\u70b9\n    root = TreeNode(preorder[i])\n    # \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n    m = inorder_map[preorder[i]]\n    # \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n    root.left = dfs(preorder, inorder_map, i + 1, l, m - 1)\n    # \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n    root.right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r)\n    # \u8fd4\u56de\u6839\u8282\u70b9\n    return root\n\ndef build_tree(preorder: list[int], inorder: list[int]) -> TreeNode | None:\n    \"\"\"\u6784\u5efa\u4e8c\u53c9\u6811\"\"\"\n    # \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n    inorder_map = {val: i for i, val in enumerate(inorder)}\n    root = dfs(preorder, inorder_map, 0, 0, len(inorder) - 1)\n    return root\n
        build_tree.cpp
        /* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode *dfs(vector<int> &preorder, unordered_map<int, int> &inorderMap, int i, int l, int r) {\n    // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n    if (r - l < 0)\n        return NULL;\n    // \u521d\u59cb\u5316\u6839\u8282\u70b9\n    TreeNode *root = new TreeNode(preorder[i]);\n    // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n    int m = inorderMap[preorder[i]];\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n    root->left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n    root->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // \u8fd4\u56de\u6839\u8282\u70b9\n    return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {\n    // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n    unordered_map<int, int> inorderMap;\n    for (int i = 0; i < inorder.size(); i++) {\n        inorderMap[inorder[i]] = i;\n    }\n    TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorder.size() - 1);\n    return root;\n}\n
        build_tree.java
        /* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode dfs(int[] preorder, Map<Integer, Integer> inorderMap, int i, int l, int r) {\n    // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n    if (r - l < 0)\n        return null;\n    // \u521d\u59cb\u5316\u6839\u8282\u70b9\n    TreeNode root = new TreeNode(preorder[i]);\n    // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n    int m = inorderMap.get(preorder[i]);\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n    root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n    root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // \u8fd4\u56de\u6839\u8282\u70b9\n    return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode buildTree(int[] preorder, int[] inorder) {\n    // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n    Map<Integer, Integer> inorderMap = new HashMap<>();\n    for (int i = 0; i < inorder.length; i++) {\n        inorderMap.put(inorder[i], i);\n    }\n    TreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n    return root;\n}\n
        build_tree.cs
        /* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode? DFS(int[] preorder, Dictionary<int, int> inorderMap, int i, int l, int r) {\n    // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n    if (r - l < 0)\n        return null;\n    // \u521d\u59cb\u5316\u6839\u8282\u70b9\n    TreeNode root = new(preorder[i]);\n    // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n    int m = inorderMap[preorder[i]];\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n    root.left = DFS(preorder, inorderMap, i + 1, l, m - 1);\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n    root.right = DFS(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // \u8fd4\u56de\u6839\u8282\u70b9\n    return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode? BuildTree(int[] preorder, int[] inorder) {\n    // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n    Dictionary<int, int> inorderMap = [];\n    for (int i = 0; i < inorder.Length; i++) {\n        inorderMap.TryAdd(inorder[i], i);\n    }\n    TreeNode? root = DFS(preorder, inorderMap, 0, 0, inorder.Length - 1);\n    return root;\n}\n
        build_tree.go
        /* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunc dfsBuildTree(preorder []int, inorderMap map[int]int, i, l, r int) *TreeNode {\n    // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n    if r-l < 0 {\n        return nil\n    }\n    // \u521d\u59cb\u5316\u6839\u8282\u70b9\n    root := NewTreeNode(preorder[i])\n    // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n    m := inorderMap[preorder[i]]\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n    root.Left = dfsBuildTree(preorder, inorderMap, i+1, l, m-1)\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n    root.Right = dfsBuildTree(preorder, inorderMap, i+1+m-l, m+1, r)\n    // \u8fd4\u56de\u6839\u8282\u70b9\n    return root\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunc buildTree(preorder, inorder []int) *TreeNode {\n    // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n    inorderMap := make(map[int]int, len(inorder))\n    for i := 0; i < len(inorder); i++ {\n        inorderMap[inorder[i]] = i\n    }\n\n    root := dfsBuildTree(preorder, inorderMap, 0, 0, len(inorder)-1)\n    return root\n}\n
        build_tree.swift
        /* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunc dfs(preorder: [Int], inorderMap: [Int: Int], i: Int, l: Int, r: Int) -> TreeNode? {\n    // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n    if r - l < 0 {\n        return nil\n    }\n    // \u521d\u59cb\u5316\u6839\u8282\u70b9\n    let root = TreeNode(x: preorder[i])\n    // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n    let m = inorderMap[preorder[i]]!\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n    root.left = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1, l: l, r: m - 1)\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n    root.right = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1 + m - l, l: m + 1, r: r)\n    // \u8fd4\u56de\u6839\u8282\u70b9\n    return root\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunc buildTree(preorder: [Int], inorder: [Int]) -> TreeNode? {\n    // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n    let inorderMap = inorder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }\n    return dfs(preorder: preorder, inorderMap: inorderMap, i: inorder.startIndex, l: inorder.startIndex, r: inorder.endIndex - 1)\n}\n
        build_tree.js
        /* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunction dfs(preorder, inorderMap, i, l, r) {\n    // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n    if (r - l < 0) return null;\n    // \u521d\u59cb\u5316\u6839\u8282\u70b9\n    const root = new TreeNode(preorder[i]);\n    // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n    const m = inorderMap.get(preorder[i]);\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n    root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n    root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // \u8fd4\u56de\u6839\u8282\u70b9\n    return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunction buildTree(preorder, inorder) {\n    // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n    let inorderMap = new Map();\n    for (let i = 0; i < inorder.length; i++) {\n        inorderMap.set(inorder[i], i);\n    }\n    const root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n    return root;\n}\n
        build_tree.ts
        /* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunction dfs(\n    preorder: number[],\n    inorderMap: Map<number, number>,\n    i: number,\n    l: number,\n    r: number\n): TreeNode | null {\n    // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n    if (r - l < 0) return null;\n    // \u521d\u59cb\u5316\u6839\u8282\u70b9\n    const root: TreeNode = new TreeNode(preorder[i]);\n    // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n    const m = inorderMap.get(preorder[i]);\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n    root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n    root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // \u8fd4\u56de\u6839\u8282\u70b9\n    return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunction buildTree(preorder: number[], inorder: number[]): TreeNode | null {\n    // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n    let inorderMap = new Map<number, number>();\n    for (let i = 0; i < inorder.length; i++) {\n        inorderMap.set(inorder[i], i);\n    }\n    const root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n    return root;\n}\n
        build_tree.dart
        /* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode? dfs(\n  List<int> preorder,\n  Map<int, int> inorderMap,\n  int i,\n  int l,\n  int r,\n) {\n  // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n  if (r - l < 0) {\n    return null;\n  }\n  // \u521d\u59cb\u5316\u6839\u8282\u70b9\n  TreeNode? root = TreeNode(preorder[i]);\n  // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n  int m = inorderMap[preorder[i]]!;\n  // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n  root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n  // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n  root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n  // \u8fd4\u56de\u6839\u8282\u70b9\n  return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode? buildTree(List<int> preorder, List<int> inorder) {\n  // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n  Map<int, int> inorderMap = {};\n  for (int i = 0; i < inorder.length; i++) {\n    inorderMap[inorder[i]] = i;\n  }\n  TreeNode? root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n  return root;\n}\n
        build_tree.rs
        /* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfn dfs(\n    preorder: &[i32],\n    inorder_map: &HashMap<i32, i32>,\n    i: i32,\n    l: i32,\n    r: i32,\n) -> Option<Rc<RefCell<TreeNode>>> {\n    // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n    if r - l < 0 {\n        return None;\n    }\n    // \u521d\u59cb\u5316\u6839\u8282\u70b9\n    let root = TreeNode::new(preorder[i as usize]);\n    // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n    let m = inorder_map.get(&preorder[i as usize]).unwrap();\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n    root.borrow_mut().left = dfs(preorder, inorder_map, i + 1, l, m - 1);\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n    root.borrow_mut().right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r);\n    // \u8fd4\u56de\u6839\u8282\u70b9\n    Some(root)\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfn build_tree(preorder: &[i32], inorder: &[i32]) -> Option<Rc<RefCell<TreeNode>>> {\n    // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n    let mut inorder_map: HashMap<i32, i32> = HashMap::new();\n    for i in 0..inorder.len() {\n        inorder_map.insert(inorder[i], i as i32);\n    }\n    let root = dfs(preorder, &inorder_map, 0, 0, inorder.len() as i32 - 1);\n    root\n}\n
        build_tree.c
        /* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode *dfs(int *preorder, int *inorderMap, int i, int l, int r, int size) {\n    // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n    if (r - l < 0)\n        return NULL;\n    // \u521d\u59cb\u5316\u6839\u8282\u70b9\n    TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));\n    root->val = preorder[i];\n    root->left = NULL;\n    root->right = NULL;\n    // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n    int m = inorderMap[preorder[i]];\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n    root->left = dfs(preorder, inorderMap, i + 1, l, m - 1, size);\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n    root->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r, size);\n    // \u8fd4\u56de\u6839\u8282\u70b9\n    return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode *buildTree(int *preorder, int preorderSize, int *inorder, int inorderSize) {\n    // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n    int *inorderMap = (int *)malloc(sizeof(int) * MAX_SIZE);\n    for (int i = 0; i < inorderSize; i++) {\n        inorderMap[inorder[i]] = i;\n    }\n    TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorderSize - 1, inorderSize);\n    free(inorderMap);\n    return root;\n}\n
        build_tree.kt
        /* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfun dfs(\n    preorder: IntArray,\n    inorderMap: Map<Int?, Int?>,\n    i: Int,\n    l: Int,\n    r: Int\n): TreeNode? {\n    // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n    if (r - l < 0) return null\n    // \u521d\u59cb\u5316\u6839\u8282\u70b9\n    val root = TreeNode(preorder[i])\n    // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n    val m = inorderMap[preorder[i]]!!\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n    root.left = dfs(preorder, inorderMap, i + 1, l, m - 1)\n    // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n    root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r)\n    // \u8fd4\u56de\u6839\u8282\u70b9\n    return root\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfun buildTree(preorder: IntArray, inorder: IntArray): TreeNode? {\n    // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n    val inorderMap = HashMap<Int?, Int?>()\n    for (i in inorder.indices) {\n        inorderMap[inorder[i]] = i\n    }\n    val root = dfs(preorder, inorderMap, 0, 0, inorder.size - 1)\n    return root\n}\n
        build_tree.rb
        ### \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb ###\ndef dfs(preorder, inorder_map, i, l, r)\n  # \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n  return if r - l < 0\n\n  # \u521d\u59cb\u5316\u6839\u8282\u70b9\n  root = TreeNode.new(preorder[i])\n  # \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n  m = inorder_map[preorder[i]]\n  # \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n  root.left = dfs(preorder, inorder_map, i + 1, l, m - 1)\n  # \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n  root.right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r)\n\n  # \u8fd4\u56de\u6839\u8282\u70b9\n  root\nend\n\n### \u6784\u5efa\u4e8c\u53c9\u6811 ###\ndef build_tree(preorder, inorder)\n  # \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n  inorder_map = {}\n  inorder.each_with_index { |val, i| inorder_map[val] = i }\n  dfs(preorder, inorder_map, 0, 0, inorder.length - 1)\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 12-8 \u5c55\u793a\u4e86\u6784\u5efa\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b\uff0c\u5404\u4e2a\u8282\u70b9\u662f\u5728\u5411\u4e0b\u201c\u9012\u201d\u7684\u8fc7\u7a0b\u4e2d\u5efa\u7acb\u7684\uff0c\u800c\u5404\u6761\u8fb9\uff08\u5f15\u7528\uff09\u662f\u5728\u5411\u4e0a\u201c\u5f52\u201d\u7684\u8fc7\u7a0b\u4e2d\u5efa\u7acb\u7684\u3002

        <1><2><3><4><5><6><7><8><9>

        \u56fe 12-8 \u00a0 \u6784\u5efa\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b

        \u6bcf\u4e2a\u9012\u5f52\u51fd\u6570\u5185\u7684\u524d\u5e8f\u904d\u5386 preorder \u548c\u4e2d\u5e8f\u904d\u5386 inorder \u7684\u5212\u5206\u7ed3\u679c\u5982\u56fe 12-9 \u6240\u793a\u3002

        \u56fe 12-9 \u00a0 \u6bcf\u4e2a\u9012\u5f52\u51fd\u6570\u4e2d\u7684\u5212\u5206\u7ed3\u679c

        \u8bbe\u6811\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u521d\u59cb\u5316\u6bcf\u4e00\u4e2a\u8282\u70b9\uff08\u6267\u884c\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 dfs() \uff09\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002\u56e0\u6b64\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

        \u54c8\u5e0c\u8868\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u5373\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u94fe\u8868\u65f6\uff0c\u9012\u5f52\u6df1\u5ea6\u8fbe\u5230 \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u7684\u6808\u5e27\u7a7a\u95f4\u3002\u56e0\u6b64\u603b\u4f53\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/","title":"12.1 \u00a0 \u5206\u6cbb\u7b97\u6cd5","text":"

        \u5206\u6cbb\uff08divide and conquer\uff09\uff0c\u5168\u79f0\u5206\u800c\u6cbb\u4e4b\uff0c\u662f\u4e00\u79cd\u975e\u5e38\u91cd\u8981\u4e14\u5e38\u89c1\u7684\u7b97\u6cd5\u7b56\u7565\u3002\u5206\u6cbb\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\uff0c\u5305\u62ec\u201c\u5206\u201d\u548c\u201c\u6cbb\u201d\u4e24\u4e2a\u6b65\u9aa4\u3002

        1. \u5206\uff08\u5212\u5206\u9636\u6bb5\uff09\uff1a\u9012\u5f52\u5730\u5c06\u539f\u95ee\u9898\u5206\u89e3\u4e3a\u4e24\u4e2a\u6216\u591a\u4e2a\u5b50\u95ee\u9898\uff0c\u76f4\u81f3\u5230\u8fbe\u6700\u5c0f\u5b50\u95ee\u9898\u65f6\u7ec8\u6b62\u3002
        2. \u6cbb\uff08\u5408\u5e76\u9636\u6bb5\uff09\uff1a\u4ece\u5df2\u77e5\u89e3\u7684\u6700\u5c0f\u5b50\u95ee\u9898\u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5730\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u8fdb\u884c\u5408\u5e76\uff0c\u4ece\u800c\u6784\u5efa\u51fa\u539f\u95ee\u9898\u7684\u89e3\u3002

        \u5982\u56fe 12-1 \u6240\u793a\uff0c\u201c\u5f52\u5e76\u6392\u5e8f\u201d\u662f\u5206\u6cbb\u7b56\u7565\u7684\u5178\u578b\u5e94\u7528\u4e4b\u4e00\u3002

        1. \u5206\uff1a\u9012\u5f52\u5730\u5c06\u539f\u6570\u7ec4\uff08\u539f\u95ee\u9898\uff09\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff08\u5b50\u95ee\u9898\uff09\uff0c\u76f4\u5230\u5b50\u6570\u7ec4\u53ea\u5269\u4e00\u4e2a\u5143\u7d20\uff08\u6700\u5c0f\u5b50\u95ee\u9898\uff09\u3002
        2. \u6cbb\uff1a\u4ece\u5e95\u81f3\u9876\u5730\u5c06\u6709\u5e8f\u7684\u5b50\u6570\u7ec4\uff08\u5b50\u95ee\u9898\u7684\u89e3\uff09\u8fdb\u884c\u5408\u5e76\uff0c\u4ece\u800c\u5f97\u5230\u6709\u5e8f\u7684\u539f\u6570\u7ec4\uff08\u539f\u95ee\u9898\u7684\u89e3\uff09\u3002

        \u56fe 12-1 \u00a0 \u5f52\u5e76\u6392\u5e8f\u7684\u5206\u6cbb\u7b56\u7565

        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1211","title":"12.1.1 \u00a0 \u5982\u4f55\u5224\u65ad\u5206\u6cbb\u95ee\u9898","text":"

        \u4e00\u4e2a\u95ee\u9898\u662f\u5426\u9002\u5408\u4f7f\u7528\u5206\u6cbb\u89e3\u51b3\uff0c\u901a\u5e38\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u51e0\u4e2a\u5224\u65ad\u4f9d\u636e\u3002

        1. \u95ee\u9898\u53ef\u4ee5\u5206\u89e3\uff1a\u539f\u95ee\u9898\u53ef\u4ee5\u5206\u89e3\u6210\u89c4\u6a21\u66f4\u5c0f\u3001\u7c7b\u4f3c\u7684\u5b50\u95ee\u9898\uff0c\u4ee5\u53ca\u80fd\u591f\u4ee5\u76f8\u540c\u65b9\u5f0f\u9012\u5f52\u5730\u8fdb\u884c\u5212\u5206\u3002
        2. \u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff1a\u5b50\u95ee\u9898\u4e4b\u95f4\u6ca1\u6709\u91cd\u53e0\uff0c\u4e92\u4e0d\u4f9d\u8d56\uff0c\u53ef\u4ee5\u72ec\u7acb\u89e3\u51b3\u3002
        3. \u5b50\u95ee\u9898\u7684\u89e3\u53ef\u4ee5\u5408\u5e76\uff1a\u539f\u95ee\u9898\u7684\u89e3\u901a\u8fc7\u5408\u5e76\u5b50\u95ee\u9898\u7684\u89e3\u5f97\u6765\u3002

        \u663e\u7136\uff0c\u5f52\u5e76\u6392\u5e8f\u6ee1\u8db3\u4ee5\u4e0a\u4e09\u4e2a\u5224\u65ad\u4f9d\u636e\u3002

        1. \u95ee\u9898\u53ef\u4ee5\u5206\u89e3\uff1a\u9012\u5f52\u5730\u5c06\u6570\u7ec4\uff08\u539f\u95ee\u9898\uff09\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff08\u5b50\u95ee\u9898\uff09\u3002
        2. \u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff1a\u6bcf\u4e2a\u5b50\u6570\u7ec4\u90fd\u53ef\u4ee5\u72ec\u7acb\u5730\u8fdb\u884c\u6392\u5e8f\uff08\u5b50\u95ee\u9898\u53ef\u4ee5\u72ec\u7acb\u8fdb\u884c\u6c42\u89e3\uff09\u3002
        3. \u5b50\u95ee\u9898\u7684\u89e3\u53ef\u4ee5\u5408\u5e76\uff1a\u4e24\u4e2a\u6709\u5e8f\u5b50\u6570\u7ec4\uff08\u5b50\u95ee\u9898\u7684\u89e3\uff09\u53ef\u4ee5\u5408\u5e76\u4e3a\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4\uff08\u539f\u95ee\u9898\u7684\u89e3\uff09\u3002
        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1212","title":"12.1.2 \u00a0 \u901a\u8fc7\u5206\u6cbb\u63d0\u5347\u6548\u7387","text":"

        \u5206\u6cbb\u4e0d\u4ec5\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u51b3\u7b97\u6cd5\u95ee\u9898\uff0c\u5f80\u5f80\u8fd8\u53ef\u4ee5\u63d0\u5347\u7b97\u6cd5\u6548\u7387\u3002\u5728\u6392\u5e8f\u7b97\u6cd5\u4e2d\uff0c\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u3001\u5806\u6392\u5e8f\u76f8\u8f83\u4e8e\u9009\u62e9\u3001\u5192\u6ce1\u3001\u63d2\u5165\u6392\u5e8f\u66f4\u5feb\uff0c\u5c31\u662f\u56e0\u4e3a\u5b83\u4eec\u5e94\u7528\u4e86\u5206\u6cbb\u7b56\u7565\u3002

        \u90a3\u4e48\uff0c\u6211\u4eec\u4e0d\u7981\u53d1\u95ee\uff1a\u4e3a\u4ec0\u4e48\u5206\u6cbb\u53ef\u4ee5\u63d0\u5347\u7b97\u6cd5\u6548\u7387\uff0c\u5176\u5e95\u5c42\u903b\u8f91\u662f\u4ec0\u4e48\uff1f\u6362\u53e5\u8bdd\u8bf4\uff0c\u5c06\u5927\u95ee\u9898\u5206\u89e3\u4e3a\u591a\u4e2a\u5b50\u95ee\u9898\u3001\u89e3\u51b3\u5b50\u95ee\u9898\u3001\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u5408\u5e76\u4e3a\u539f\u95ee\u9898\u7684\u89e3\uff0c\u8fd9\u51e0\u6b65\u7684\u6548\u7387\u4e3a\u4ec0\u4e48\u6bd4\u76f4\u63a5\u89e3\u51b3\u539f\u95ee\u9898\u7684\u6548\u7387\u66f4\u9ad8\uff1f\u8fd9\u4e2a\u95ee\u9898\u53ef\u4ee5\u4ece\u64cd\u4f5c\u6570\u91cf\u548c\u5e76\u884c\u8ba1\u7b97\u4e24\u65b9\u9762\u6765\u8ba8\u8bba\u3002

        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1","title":"1. \u00a0 \u64cd\u4f5c\u6570\u91cf\u4f18\u5316","text":"

        \u4ee5\u201c\u5192\u6ce1\u6392\u5e8f\u201d\u4e3a\u4f8b\uff0c\u5176\u5904\u7406\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\u9700\u8981 \\(O(n^2)\\) \u65f6\u95f4\u3002\u5047\u8bbe\u6211\u4eec\u6309\u7167\u56fe 12-2 \u6240\u793a\u7684\u65b9\u5f0f\uff0c\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5904\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u5219\u5212\u5206\u9700\u8981 \\(O(n)\\) \u65f6\u95f4\uff0c\u6392\u5e8f\u6bcf\u4e2a\u5b50\u6570\u7ec4\u9700\u8981 \\(O((n / 2)^2)\\) \u65f6\u95f4\uff0c\u5408\u5e76\u4e24\u4e2a\u5b50\u6570\u7ec4\u9700\u8981 \\(O(n)\\) \u65f6\u95f4\uff0c\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\uff1a

        \\[ O(n + (\\frac{n}{2})^2 \\times 2 + n) = O(\\frac{n^2}{2} + 2n) \\]

        \u56fe 12-2 \u00a0 \u5212\u5206\u6570\u7ec4\u524d\u540e\u7684\u5192\u6ce1\u6392\u5e8f

        \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8ba1\u7b97\u4ee5\u4e0b\u4e0d\u7b49\u5f0f\uff0c\u5176\u5de6\u8fb9\u548c\u53f3\u8fb9\u5206\u522b\u4e3a\u5212\u5206\u524d\u548c\u5212\u5206\u540e\u7684\u64cd\u4f5c\u603b\u6570\uff1a

        \\[ \\begin{aligned} n^2 & > \\frac{n^2}{2} + 2n \\newline n^2 - \\frac{n^2}{2} - 2n & > 0 \\newline n(n - 4) & > 0 \\end{aligned} \\]

        \u8fd9\u610f\u5473\u7740\u5f53 \\(n > 4\\) \u65f6\uff0c\u5212\u5206\u540e\u7684\u64cd\u4f5c\u6570\u91cf\u66f4\u5c11\uff0c\u6392\u5e8f\u6548\u7387\u5e94\u8be5\u66f4\u9ad8\u3002\u8bf7\u6ce8\u610f\uff0c\u5212\u5206\u540e\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u7136\u662f\u5e73\u65b9\u9636 \\(O(n^2)\\) \uff0c\u53ea\u662f\u590d\u6742\u5ea6\u4e2d\u7684\u5e38\u6570\u9879\u53d8\u5c0f\u4e86\u3002

        \u8fdb\u4e00\u6b65\u60f3\uff0c\u5982\u679c\u6211\u4eec\u628a\u5b50\u6570\u7ec4\u4e0d\u65ad\u5730\u518d\u4ece\u4e2d\u70b9\u5904\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u53ea\u5269\u4e00\u4e2a\u5143\u7d20\u65f6\u505c\u6b62\u5212\u5206\u5462\uff1f\u8fd9\u79cd\u601d\u8def\u5b9e\u9645\u4e0a\u5c31\u662f\u201c\u5f52\u5e76\u6392\u5e8f\u201d\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002

        \u518d\u601d\u8003\uff0c\u5982\u679c\u6211\u4eec\u591a\u8bbe\u7f6e\u51e0\u4e2a\u5212\u5206\u70b9\uff0c\u5c06\u539f\u6570\u7ec4\u5e73\u5747\u5212\u5206\u4e3a \\(k\\) \u4e2a\u5b50\u6570\u7ec4\u5462\uff1f\u8fd9\u79cd\u60c5\u51b5\u4e0e\u201c\u6876\u6392\u5e8f\u201d\u975e\u5e38\u7c7b\u4f3c\uff0c\u5b83\u975e\u5e38\u9002\u5408\u6392\u5e8f\u6d77\u91cf\u6570\u636e\uff0c\u7406\u8bba\u4e0a\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u8fbe\u5230 \\(O(n + k)\\) \u3002

        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#2","title":"2. \u00a0 \u5e76\u884c\u8ba1\u7b97\u4f18\u5316","text":"

        \u6211\u4eec\u77e5\u9053\uff0c\u5206\u6cbb\u751f\u6210\u7684\u5b50\u95ee\u9898\u662f\u76f8\u4e92\u72ec\u7acb\u7684\uff0c\u56e0\u6b64\u901a\u5e38\u53ef\u4ee5\u5e76\u884c\u89e3\u51b3\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5206\u6cbb\u4e0d\u4ec5\u53ef\u4ee5\u964d\u4f4e\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u8fd8\u6709\u5229\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u7684\u5e76\u884c\u4f18\u5316\u3002

        \u5e76\u884c\u4f18\u5316\u5728\u591a\u6838\u6216\u591a\u5904\u7406\u5668\u7684\u73af\u5883\u4e2d\u5c24\u5176\u6709\u6548\uff0c\u56e0\u4e3a\u7cfb\u7edf\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5b50\u95ee\u9898\uff0c\u66f4\u52a0\u5145\u5206\u5730\u5229\u7528\u8ba1\u7b97\u8d44\u6e90\uff0c\u4ece\u800c\u663e\u8457\u51cf\u5c11\u603b\u4f53\u7684\u8fd0\u884c\u65f6\u95f4\u3002

        \u6bd4\u5982\u5728\u56fe 12-3 \u6240\u793a\u7684\u201c\u6876\u6392\u5e8f\u201d\u4e2d\uff0c\u6211\u4eec\u5c06\u6d77\u91cf\u7684\u6570\u636e\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\uff0c\u5219\u53ef\u5c06\u6240\u6709\u6876\u7684\u6392\u5e8f\u4efb\u52a1\u5206\u6563\u5230\u5404\u4e2a\u8ba1\u7b97\u5355\u5143\uff0c\u5b8c\u6210\u540e\u518d\u5408\u5e76\u7ed3\u679c\u3002

        \u56fe 12-3 \u00a0 \u6876\u6392\u5e8f\u7684\u5e76\u884c\u8ba1\u7b97

        "},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1213","title":"12.1.3 \u00a0 \u5206\u6cbb\u5e38\u89c1\u5e94\u7528","text":"

        \u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u53ef\u4ee5\u7528\u6765\u89e3\u51b3\u8bb8\u591a\u7ecf\u5178\u7b97\u6cd5\u95ee\u9898\u3002

        • \u5bfb\u627e\u6700\u8fd1\u70b9\u5bf9\uff1a\u8be5\u7b97\u6cd5\u9996\u5148\u5c06\u70b9\u96c6\u5206\u6210\u4e24\u90e8\u5206\uff0c\u7136\u540e\u5206\u522b\u627e\u51fa\u4e24\u90e8\u5206\u4e2d\u7684\u6700\u8fd1\u70b9\u5bf9\uff0c\u6700\u540e\u627e\u51fa\u8de8\u8d8a\u4e24\u90e8\u5206\u7684\u6700\u8fd1\u70b9\u5bf9\u3002
        • \u5927\u6574\u6570\u4e58\u6cd5\uff1a\u4f8b\u5982 Karatsuba \u7b97\u6cd5\uff0c\u5b83\u5c06\u5927\u6574\u6570\u4e58\u6cd5\u5206\u89e3\u4e3a\u51e0\u4e2a\u8f83\u5c0f\u7684\u6574\u6570\u7684\u4e58\u6cd5\u548c\u52a0\u6cd5\u3002
        • \u77e9\u9635\u4e58\u6cd5\uff1a\u4f8b\u5982 Strassen \u7b97\u6cd5\uff0c\u5b83\u5c06\u5927\u77e9\u9635\u4e58\u6cd5\u5206\u89e3\u4e3a\u591a\u4e2a\u5c0f\u77e9\u9635\u7684\u4e58\u6cd5\u548c\u52a0\u6cd5\u3002
        • \u6c49\u8bfa\u5854\u95ee\u9898\uff1a\u6c49\u8bfa\u5854\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u89e3\u51b3\uff0c\u8fd9\u662f\u5178\u578b\u7684\u5206\u6cbb\u7b56\u7565\u5e94\u7528\u3002
        • \u6c42\u89e3\u9006\u5e8f\u5bf9\uff1a\u5728\u4e00\u4e2a\u5e8f\u5217\u4e2d\uff0c\u5982\u679c\u524d\u9762\u7684\u6570\u5b57\u5927\u4e8e\u540e\u9762\u7684\u6570\u5b57\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u6570\u5b57\u6784\u6210\u4e00\u4e2a\u9006\u5e8f\u5bf9\u3002\u6c42\u89e3\u9006\u5e8f\u5bf9\u95ee\u9898\u53ef\u4ee5\u5229\u7528\u5206\u6cbb\u7684\u601d\u60f3\uff0c\u501f\u52a9\u5f52\u5e76\u6392\u5e8f\u8fdb\u884c\u6c42\u89e3\u3002

        \u53e6\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u5728\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u7684\u8bbe\u8ba1\u4e2d\u5e94\u7528\u5f97\u975e\u5e38\u5e7f\u6cdb\u3002

        • \u4e8c\u5206\u67e5\u627e\uff1a\u4e8c\u5206\u67e5\u627e\u662f\u5c06\u6709\u5e8f\u6570\u7ec4\u4ece\u4e2d\u70b9\u7d22\u5f15\u5904\u5206\u4e3a\u4e24\u90e8\u5206\uff0c\u7136\u540e\u6839\u636e\u76ee\u6807\u503c\u4e0e\u4e2d\u95f4\u5143\u7d20\u503c\u6bd4\u8f83\u7ed3\u679c\uff0c\u51b3\u5b9a\u6392\u9664\u54ea\u4e00\u534a\u533a\u95f4\uff0c\u5e76\u5728\u5269\u4f59\u533a\u95f4\u6267\u884c\u76f8\u540c\u7684\u4e8c\u5206\u64cd\u4f5c\u3002
        • \u5f52\u5e76\u6392\u5e8f\uff1a\u672c\u8282\u5f00\u5934\u5df2\u4ecb\u7ecd\uff0c\u4e0d\u518d\u8d58\u8ff0\u3002
        • \u5feb\u901f\u6392\u5e8f\uff1a\u5feb\u901f\u6392\u5e8f\u662f\u9009\u53d6\u4e00\u4e2a\u57fa\u51c6\u503c\uff0c\u7136\u540e\u628a\u6570\u7ec4\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u4e00\u4e2a\u5b50\u6570\u7ec4\u7684\u5143\u7d20\u6bd4\u57fa\u51c6\u503c\u5c0f\uff0c\u53e6\u4e00\u5b50\u6570\u7ec4\u7684\u5143\u7d20\u6bd4\u57fa\u51c6\u503c\u5927\uff0c\u518d\u5bf9\u8fd9\u4e24\u90e8\u5206\u8fdb\u884c\u76f8\u540c\u7684\u5212\u5206\u64cd\u4f5c\uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u53ea\u5269\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002
        • \u6876\u6392\u5e8f\uff1a\u6876\u6392\u5e8f\u7684\u57fa\u672c\u601d\u60f3\u662f\u5c06\u6570\u636e\u5206\u6563\u5230\u591a\u4e2a\u6876\uff0c\u7136\u540e\u5bf9\u6bcf\u4e2a\u6876\u5185\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\uff0c\u6700\u540e\u5c06\u5404\u4e2a\u6876\u7684\u5143\u7d20\u4f9d\u6b21\u53d6\u51fa\uff0c\u4ece\u800c\u5f97\u5230\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4\u3002
        • \u6811\uff1a\u4f8b\u5982\u4e8c\u53c9\u641c\u7d22\u6811\u3001AVL \u6811\u3001\u7ea2\u9ed1\u6811\u3001B \u6811\u3001B+ \u6811\u7b49\uff0c\u5b83\u4eec\u7684\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u7b49\u64cd\u4f5c\u90fd\u53ef\u4ee5\u89c6\u4e3a\u5206\u6cbb\u7b56\u7565\u7684\u5e94\u7528\u3002
        • \u5806\uff1a\u5806\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u5176\u5404\u79cd\u64cd\u4f5c\uff0c\u5982\u63d2\u5165\u3001\u5220\u9664\u548c\u5806\u5316\uff0c\u5b9e\u9645\u4e0a\u90fd\u9690\u542b\u4e86\u5206\u6cbb\u7684\u601d\u60f3\u3002
        • \u54c8\u5e0c\u8868\uff1a\u867d\u7136\u54c8\u5e0c\u8868\u5e76\u4e0d\u76f4\u63a5\u5e94\u7528\u5206\u6cbb\uff0c\u4f46\u67d0\u4e9b\u54c8\u5e0c\u51b2\u7a81\u89e3\u51b3\u65b9\u6848\u95f4\u63a5\u5e94\u7528\u4e86\u5206\u6cbb\u7b56\u7565\uff0c\u4f8b\u5982\uff0c\u94fe\u5f0f\u5730\u5740\u4e2d\u7684\u957f\u94fe\u8868\u4f1a\u88ab\u8f6c\u5316\u4e3a\u7ea2\u9ed1\u6811\uff0c\u4ee5\u63d0\u5347\u67e5\u8be2\u6548\u7387\u3002

        \u53ef\u4ee5\u770b\u51fa\uff0c\u5206\u6cbb\u662f\u4e00\u79cd\u201c\u6da6\u7269\u7ec6\u65e0\u58f0\u201d\u7684\u7b97\u6cd5\u601d\u60f3\uff0c\u9690\u542b\u5728\u5404\u79cd\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784\u4e4b\u4e2d\u3002

        "},{"location":"chapter_divide_and_conquer/hanota_problem/","title":"12.4 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898","text":"

        \u5728\u5f52\u5e76\u6392\u5e8f\u548c\u6784\u5efa\u4e8c\u53c9\u6811\u4e2d\uff0c\u6211\u4eec\u90fd\u662f\u5c06\u539f\u95ee\u9898\u5206\u89e3\u4e3a\u4e24\u4e2a\u89c4\u6a21\u4e3a\u539f\u95ee\u9898\u4e00\u534a\u7684\u5b50\u95ee\u9898\u3002\u7136\u800c\u5bf9\u4e8e\u6c49\u8bfa\u5854\u95ee\u9898\uff0c\u6211\u4eec\u91c7\u7528\u4e0d\u540c\u7684\u5206\u89e3\u7b56\u7565\u3002

        Question

        \u7ed9\u5b9a\u4e09\u6839\u67f1\u5b50\uff0c\u8bb0\u4e3a A\u3001B \u548c C \u3002\u8d77\u59cb\u72b6\u6001\u4e0b\uff0c\u67f1\u5b50 A \u4e0a\u5957\u7740 \\(n\\) \u4e2a\u5706\u76d8\uff0c\u5b83\u4eec\u4ece\u4e0a\u5230\u4e0b\u6309\u7167\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u6392\u5217\u3002\u6211\u4eec\u7684\u4efb\u52a1\u662f\u8981\u628a\u8fd9 \\(n\\) \u4e2a\u5706\u76d8\u79fb\u5230\u67f1\u5b50 C \u4e0a\uff0c\u5e76\u4fdd\u6301\u5b83\u4eec\u7684\u539f\u6709\u987a\u5e8f\u4e0d\u53d8\uff08\u5982\u56fe 12-10 \u6240\u793a\uff09\u3002\u5728\u79fb\u52a8\u5706\u76d8\u7684\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u9075\u5b88\u4ee5\u4e0b\u89c4\u5219\u3002

        1. \u5706\u76d8\u53ea\u80fd\u4ece\u4e00\u6839\u67f1\u5b50\u9876\u90e8\u62ff\u51fa\uff0c\u4ece\u53e6\u4e00\u6839\u67f1\u5b50\u9876\u90e8\u653e\u5165\u3002
        2. \u6bcf\u6b21\u53ea\u80fd\u79fb\u52a8\u4e00\u4e2a\u5706\u76d8\u3002
        3. \u5c0f\u5706\u76d8\u5fc5\u987b\u65f6\u523b\u4f4d\u4e8e\u5927\u5706\u76d8\u4e4b\u4e0a\u3002

        \u56fe 12-10 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898\u793a\u4f8b

        \u6211\u4eec\u5c06\u89c4\u6a21\u4e3a \\(i\\) \u7684\u6c49\u8bfa\u5854\u95ee\u9898\u8bb0\u4f5c \\(f(i)\\) \u3002\u4f8b\u5982 \\(f(3)\\) \u4ee3\u8868\u5c06 \\(3\\) \u4e2a\u5706\u76d8\u4ece A \u79fb\u52a8\u81f3 C \u7684\u6c49\u8bfa\u5854\u95ee\u9898\u3002

        "},{"location":"chapter_divide_and_conquer/hanota_problem/#1","title":"1. \u00a0 \u8003\u8651\u57fa\u672c\u60c5\u51b5","text":"

        \u5982\u56fe 12-11 \u6240\u793a\uff0c\u5bf9\u4e8e\u95ee\u9898 \\(f(1)\\) \uff0c\u5373\u5f53\u53ea\u6709\u4e00\u4e2a\u5706\u76d8\u65f6\uff0c\u6211\u4eec\u5c06\u5b83\u76f4\u63a5\u4ece A \u79fb\u52a8\u81f3 C \u5373\u53ef\u3002

        <1><2>

        \u56fe 12-11 \u00a0 \u89c4\u6a21\u4e3a 1 \u7684\u95ee\u9898\u7684\u89e3

        \u5982\u56fe 12-12 \u6240\u793a\uff0c\u5bf9\u4e8e\u95ee\u9898 \\(f(2)\\) \uff0c\u5373\u5f53\u6709\u4e24\u4e2a\u5706\u76d8\u65f6\uff0c\u7531\u4e8e\u8981\u65f6\u523b\u6ee1\u8db3\u5c0f\u5706\u76d8\u5728\u5927\u5706\u76d8\u4e4b\u4e0a\uff0c\u56e0\u6b64\u9700\u8981\u501f\u52a9 B \u6765\u5b8c\u6210\u79fb\u52a8\u3002

        1. \u5148\u5c06\u4e0a\u9762\u7684\u5c0f\u5706\u76d8\u4ece A \u79fb\u81f3 B \u3002
        2. \u518d\u5c06\u5927\u5706\u76d8\u4ece A \u79fb\u81f3 C \u3002
        3. \u6700\u540e\u5c06\u5c0f\u5706\u76d8\u4ece B \u79fb\u81f3 C \u3002
        <1><2><3><4>

        \u56fe 12-12 \u00a0 \u89c4\u6a21\u4e3a 2 \u7684\u95ee\u9898\u7684\u89e3

        \u89e3\u51b3\u95ee\u9898 \\(f(2)\\) \u7684\u8fc7\u7a0b\u53ef\u603b\u7ed3\u4e3a\uff1a\u5c06\u4e24\u4e2a\u5706\u76d8\u501f\u52a9 B \u4ece A \u79fb\u81f3 C \u3002\u5176\u4e2d\uff0cC \u79f0\u4e3a\u76ee\u6807\u67f1\u3001B \u79f0\u4e3a\u7f13\u51b2\u67f1\u3002

        "},{"location":"chapter_divide_and_conquer/hanota_problem/#2","title":"2. \u00a0 \u5b50\u95ee\u9898\u5206\u89e3","text":"

        \u5bf9\u4e8e\u95ee\u9898 \\(f(3)\\) \uff0c\u5373\u5f53\u6709\u4e09\u4e2a\u5706\u76d8\u65f6\uff0c\u60c5\u51b5\u53d8\u5f97\u7a0d\u5fae\u590d\u6742\u4e86\u4e00\u4e9b\u3002

        \u56e0\u4e3a\u5df2\u77e5 \\(f(1)\\) \u548c \\(f(2)\\) \u7684\u89e3\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ece\u5206\u6cbb\u89d2\u5ea6\u601d\u8003\uff0c\u5c06 A \u9876\u90e8\u7684\u4e24\u4e2a\u5706\u76d8\u770b\u4f5c\u4e00\u4e2a\u6574\u4f53\uff0c\u6267\u884c\u56fe 12-13 \u6240\u793a\u7684\u6b65\u9aa4\u3002\u8fd9\u6837\u4e09\u4e2a\u5706\u76d8\u5c31\u88ab\u987a\u5229\u5730\u4ece A \u79fb\u81f3 C \u4e86\u3002

        1. \u4ee4 B \u4e3a\u76ee\u6807\u67f1\u3001C \u4e3a\u7f13\u51b2\u67f1\uff0c\u5c06\u4e24\u4e2a\u5706\u76d8\u4ece A \u79fb\u81f3 B \u3002
        2. \u5c06 A \u4e2d\u5269\u4f59\u7684\u4e00\u4e2a\u5706\u76d8\u4ece A \u76f4\u63a5\u79fb\u52a8\u81f3 C \u3002
        3. \u4ee4 C \u4e3a\u76ee\u6807\u67f1\u3001A \u4e3a\u7f13\u51b2\u67f1\uff0c\u5c06\u4e24\u4e2a\u5706\u76d8\u4ece B \u79fb\u81f3 C \u3002
        <1><2><3><4>

        \u56fe 12-13 \u00a0 \u89c4\u6a21\u4e3a 3 \u7684\u95ee\u9898\u7684\u89e3

        \u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u6211\u4eec\u5c06\u95ee\u9898 \\(f(3)\\) \u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u95ee\u9898 \\(f(2)\\) \u548c\u4e00\u4e2a\u5b50\u95ee\u9898 \\(f(1)\\) \u3002\u6309\u987a\u5e8f\u89e3\u51b3\u8fd9\u4e09\u4e2a\u5b50\u95ee\u9898\u4e4b\u540e\uff0c\u539f\u95ee\u9898\u968f\u4e4b\u5f97\u5230\u89e3\u51b3\u3002\u8fd9\u8bf4\u660e\u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff0c\u800c\u4e14\u89e3\u53ef\u4ee5\u5408\u5e76\u3002

        \u81f3\u6b64\uff0c\u6211\u4eec\u53ef\u603b\u7ed3\u51fa\u56fe 12-14 \u6240\u793a\u7684\u89e3\u51b3\u6c49\u8bfa\u5854\u95ee\u9898\u7684\u5206\u6cbb\u7b56\u7565\uff1a\u5c06\u539f\u95ee\u9898 \\(f(n)\\) \u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u95ee\u9898 \\(f(n-1)\\) \u548c\u4e00\u4e2a\u5b50\u95ee\u9898 \\(f(1)\\) \uff0c\u5e76\u6309\u7167\u4ee5\u4e0b\u987a\u5e8f\u89e3\u51b3\u8fd9\u4e09\u4e2a\u5b50\u95ee\u9898\u3002

        1. \u5c06 \\(n-1\\) \u4e2a\u5706\u76d8\u501f\u52a9 C \u4ece A \u79fb\u81f3 B \u3002
        2. \u5c06\u5269\u4f59 \\(1\\) \u4e2a\u5706\u76d8\u4ece A \u76f4\u63a5\u79fb\u81f3 C \u3002
        3. \u5c06 \\(n-1\\) \u4e2a\u5706\u76d8\u501f\u52a9 A \u4ece B \u79fb\u81f3 C \u3002

        \u5bf9\u4e8e\u8fd9\u4e24\u4e2a\u5b50\u95ee\u9898 \\(f(n-1)\\) \uff0c\u53ef\u4ee5\u901a\u8fc7\u76f8\u540c\u7684\u65b9\u5f0f\u8fdb\u884c\u9012\u5f52\u5212\u5206\uff0c\u76f4\u81f3\u8fbe\u5230\u6700\u5c0f\u5b50\u95ee\u9898 \\(f(1)\\) \u3002\u800c \\(f(1)\\) \u7684\u89e3\u662f\u5df2\u77e5\u7684\uff0c\u53ea\u9700\u4e00\u6b21\u79fb\u52a8\u64cd\u4f5c\u5373\u53ef\u3002

        \u56fe 12-14 \u00a0 \u89e3\u51b3\u6c49\u8bfa\u5854\u95ee\u9898\u7684\u5206\u6cbb\u7b56\u7565

        "},{"location":"chapter_divide_and_conquer/hanota_problem/#3","title":"3. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u5728\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 dfs(i, src, buf, tar) \uff0c\u5b83\u7684\u4f5c\u7528\u662f\u5c06\u67f1 src \u9876\u90e8\u7684 \\(i\\) \u4e2a\u5706\u76d8\u501f\u52a9\u7f13\u51b2\u67f1 buf \u79fb\u52a8\u81f3\u76ee\u6807\u67f1 tar \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hanota.py
        def move(src: list[int], tar: list[int]):\n    \"\"\"\u79fb\u52a8\u4e00\u4e2a\u5706\u76d8\"\"\"\n    # \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n    pan = src.pop()\n    # \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n    tar.append(pan)\n\ndef dfs(i: int, src: list[int], buf: list[int], tar: list[int]):\n    \"\"\"\u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i)\"\"\"\n    # \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n    if i == 1:\n        move(src, tar)\n        return\n    # \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n    dfs(i - 1, src, tar, buf)\n    # \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n    move(src, tar)\n    # \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n    dfs(i - 1, buf, src, tar)\n\ndef solve_hanota(A: list[int], B: list[int], C: list[int]):\n    \"\"\"\u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898\"\"\"\n    n = len(A)\n    # \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n    dfs(n, A, B, C)\n
        hanota.cpp
        /* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(vector<int> &src, vector<int> &tar) {\n    // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n    int pan = src.back();\n    src.pop_back();\n    // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n    tar.push_back(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nvoid dfs(int i, vector<int> &src, vector<int> &buf, vector<int> &tar) {\n    // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n    if (i == 1) {\n        move(src, tar);\n        return;\n    }\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n    dfs(i - 1, src, tar, buf);\n    // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n    move(src, tar);\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n    dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nvoid solveHanota(vector<int> &A, vector<int> &B, vector<int> &C) {\n    int n = A.size();\n    // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n    dfs(n, A, B, C);\n}\n
        hanota.java
        /* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(List<Integer> src, List<Integer> tar) {\n    // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n    Integer pan = src.remove(src.size() - 1);\n    // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n    tar.add(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nvoid dfs(int i, List<Integer> src, List<Integer> buf, List<Integer> tar) {\n    // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n    if (i == 1) {\n        move(src, tar);\n        return;\n    }\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n    dfs(i - 1, src, tar, buf);\n    // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n    move(src, tar);\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n    dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nvoid solveHanota(List<Integer> A, List<Integer> B, List<Integer> C) {\n    int n = A.size();\n    // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n    dfs(n, A, B, C);\n}\n
        hanota.cs
        /* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid Move(List<int> src, List<int> tar) {\n    // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n    int pan = src[^1];\n    src.RemoveAt(src.Count - 1);\n    // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n    tar.Add(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nvoid DFS(int i, List<int> src, List<int> buf, List<int> tar) {\n    // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n    if (i == 1) {\n        Move(src, tar);\n        return;\n    }\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n    DFS(i - 1, src, tar, buf);\n    // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n    Move(src, tar);\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n    DFS(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nvoid SolveHanota(List<int> A, List<int> B, List<int> C) {\n    int n = A.Count;\n    // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n    DFS(n, A, B, C);\n}\n
        hanota.go
        /* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunc move(src, tar *list.List) {\n    // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n    pan := src.Back()\n    // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n    tar.PushBack(pan.Value)\n    // \u79fb\u9664 src \u9876\u90e8\u5706\u76d8\n    src.Remove(pan)\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nfunc dfsHanota(i int, src, buf, tar *list.List) {\n    // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n    if i == 1 {\n        move(src, tar)\n        return\n    }\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n    dfsHanota(i-1, src, tar, buf)\n    // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n    move(src, tar)\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n    dfsHanota(i-1, buf, src, tar)\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nfunc solveHanota(A, B, C *list.List) {\n    n := A.Len()\n    // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n    dfsHanota(n, A, B, C)\n}\n
        hanota.swift
        /* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunc move(src: inout [Int], tar: inout [Int]) {\n    // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n    let pan = src.popLast()!\n    // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n    tar.append(pan)\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nfunc dfs(i: Int, src: inout [Int], buf: inout [Int], tar: inout [Int]) {\n    // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n    if i == 1 {\n        move(src: &src, tar: &tar)\n        return\n    }\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n    dfs(i: i - 1, src: &src, buf: &tar, tar: &buf)\n    // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n    move(src: &src, tar: &tar)\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n    dfs(i: i - 1, src: &buf, buf: &src, tar: &tar)\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nfunc solveHanota(A: inout [Int], B: inout [Int], C: inout [Int]) {\n    let n = A.count\n    // \u5217\u8868\u5c3e\u90e8\u662f\u67f1\u5b50\u9876\u90e8\n    // \u5c06 src \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n    dfs(i: n, src: &A, buf: &B, tar: &C)\n}\n
        hanota.js
        /* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunction move(src, tar) {\n    // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n    const pan = src.pop();\n    // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n    tar.push(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nfunction dfs(i, src, buf, tar) {\n    // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n    if (i === 1) {\n        move(src, tar);\n        return;\n    }\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n    dfs(i - 1, src, tar, buf);\n    // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n    move(src, tar);\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n    dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nfunction solveHanota(A, B, C) {\n    const n = A.length;\n    // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n    dfs(n, A, B, C);\n}\n
        hanota.ts
        /* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunction move(src: number[], tar: number[]): void {\n    // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n    const pan = src.pop();\n    // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n    tar.push(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nfunction dfs(i: number, src: number[], buf: number[], tar: number[]): void {\n    // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n    if (i === 1) {\n        move(src, tar);\n        return;\n    }\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n    dfs(i - 1, src, tar, buf);\n    // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n    move(src, tar);\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n    dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nfunction solveHanota(A: number[], B: number[], C: number[]): void {\n    const n = A.length;\n    // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n    dfs(n, A, B, C);\n}\n
        hanota.dart
        /* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(List<int> src, List<int> tar) {\n  // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n  int pan = src.removeLast();\n  // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n  tar.add(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nvoid dfs(int i, List<int> src, List<int> buf, List<int> tar) {\n  // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n  if (i == 1) {\n    move(src, tar);\n    return;\n  }\n  // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n  dfs(i - 1, src, tar, buf);\n  // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n  move(src, tar);\n  // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n  dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nvoid solveHanota(List<int> A, List<int> B, List<int> C) {\n  int n = A.length;\n  // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n  dfs(n, A, B, C);\n}\n
        hanota.rs
        /* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfn move_pan(src: &mut Vec<i32>, tar: &mut Vec<i32>) {\n    // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n    let pan = src.pop().unwrap();\n    // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n    tar.push(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nfn dfs(i: i32, src: &mut Vec<i32>, buf: &mut Vec<i32>, tar: &mut Vec<i32>) {\n    // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n    if i == 1 {\n        move_pan(src, tar);\n        return;\n    }\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n    dfs(i - 1, src, tar, buf);\n    // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n    move_pan(src, tar);\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n    dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nfn solve_hanota(A: &mut Vec<i32>, B: &mut Vec<i32>, C: &mut Vec<i32>) {\n    let n = A.len() as i32;\n    // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n    dfs(n, A, B, C);\n}\n
        hanota.c
        /* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(int *src, int *srcSize, int *tar, int *tarSize) {\n    // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n    int pan = src[*srcSize - 1];\n    src[*srcSize - 1] = 0;\n    (*srcSize)--;\n    // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n    tar[*tarSize] = pan;\n    (*tarSize)++;\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nvoid dfs(int i, int *src, int *srcSize, int *buf, int *bufSize, int *tar, int *tarSize) {\n    // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n    if (i == 1) {\n        move(src, srcSize, tar, tarSize);\n        return;\n    }\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n    dfs(i - 1, src, srcSize, tar, tarSize, buf, bufSize);\n    // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n    move(src, srcSize, tar, tarSize);\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n    dfs(i - 1, buf, bufSize, src, srcSize, tar, tarSize);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nvoid solveHanota(int *A, int *ASize, int *B, int *BSize, int *C, int *CSize) {\n    // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n    dfs(*ASize, A, ASize, B, BSize, C, CSize);\n}\n
        hanota.kt
        /* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfun move(src: MutableList<Int>, tar: MutableList<Int>) {\n    // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n    val pan = src.removeAt(src.size - 1)\n    // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n    tar.add(pan)\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nfun dfs(i: Int, src: MutableList<Int>, buf: MutableList<Int>, tar: MutableList<Int>) {\n    // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n    if (i == 1) {\n        move(src, tar)\n        return\n    }\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n    dfs(i - 1, src, tar, buf)\n    // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n    move(src, tar)\n    // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n    dfs(i - 1, buf, src, tar)\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nfun solveHanota(A: MutableList<Int>, B: MutableList<Int>, C: MutableList<Int>) {\n    val n = A.size\n    // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n    dfs(n, A, B, C)\n}\n
        hanota.rb
        ### \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 ###\ndef move(src, tar)\n  # \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n  pan = src.pop\n  # \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n  tar << pan\nend\n\n### \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) ###\ndef dfs(i, src, buf, tar)\n  # \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n  if i == 1\n    move(src, tar)\n    return\n  end\n\n  # \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n  dfs(i - 1, src, tar, buf)\n  # \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n  move(src, tar)\n  # \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n  dfs(i - 1, buf, src, tar)\nend\n\n### \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 ###\ndef solve_hanota(_A, _B, _C)\n  n = _A.length\n  # \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n  dfs(n, _A, _B, _C)\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5982\u56fe 12-15 \u6240\u793a\uff0c\u6c49\u8bfa\u5854\u95ee\u9898\u5f62\u6210\u4e00\u68f5\u9ad8\u5ea6\u4e3a \\(n\\) \u7684\u9012\u5f52\u6811\uff0c\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u5b50\u95ee\u9898\uff0c\u5bf9\u5e94\u4e00\u4e2a\u5f00\u542f\u7684 dfs() \u51fd\u6570\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

        \u56fe 12-15 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898\u7684\u9012\u5f52\u6811

        Quote

        \u6c49\u8bfa\u5854\u95ee\u9898\u6e90\u81ea\u4e00\u4e2a\u53e4\u8001\u7684\u4f20\u8bf4\u3002\u5728\u53e4\u5370\u5ea6\u7684\u4e00\u4e2a\u5bfa\u5e99\u91cc\uff0c\u50e7\u4fa3\u4eec\u6709\u4e09\u6839\u9ad8\u5927\u7684\u94bb\u77f3\u67f1\u5b50\uff0c\u4ee5\u53ca \\(64\\) \u4e2a\u5927\u5c0f\u4e0d\u4e00\u7684\u91d1\u5706\u76d8\u3002\u50e7\u4fa3\u4eec\u4e0d\u65ad\u5730\u79fb\u52a8\u5706\u76d8\uff0c\u4ed6\u4eec\u76f8\u4fe1\u5728\u6700\u540e\u4e00\u4e2a\u5706\u76d8\u88ab\u6b63\u786e\u653e\u7f6e\u7684\u90a3\u4e00\u523b\uff0c\u8fd9\u4e2a\u4e16\u754c\u5c31\u4f1a\u7ed3\u675f\u3002

        \u7136\u800c\uff0c\u5373\u4f7f\u50e7\u4fa3\u4eec\u6bcf\u79d2\u949f\u79fb\u52a8\u4e00\u6b21\uff0c\u603b\u5171\u9700\u8981\u5927\u7ea6 \\(2^{64} \\approx 1.84\u00d710^{19}\\) \u79d2\uff0c\u5408\u7ea6 \\(5850\\) \u4ebf\u5e74\uff0c\u8fdc\u8fdc\u8d85\u8fc7\u4e86\u73b0\u5728\u5bf9\u5b87\u5b99\u5e74\u9f84\u7684\u4f30\u8ba1\u3002\u6240\u4ee5\uff0c\u5018\u82e5\u8fd9\u4e2a\u4f20\u8bf4\u662f\u771f\u7684\uff0c\u6211\u4eec\u5e94\u8be5\u4e0d\u9700\u8981\u62c5\u5fc3\u4e16\u754c\u672b\u65e5\u7684\u5230\u6765\u3002

        "},{"location":"chapter_divide_and_conquer/summary/","title":"12.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_divide_and_conquer/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u5206\u6cbb\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7b97\u6cd5\u8bbe\u8ba1\u7b56\u7565\uff0c\u5305\u62ec\u5206\uff08\u5212\u5206\uff09\u548c\u6cbb\uff08\u5408\u5e76\uff09\u4e24\u4e2a\u9636\u6bb5\uff0c\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\u3002
        • \u5224\u65ad\u662f\u5426\u662f\u5206\u6cbb\u7b97\u6cd5\u95ee\u9898\u7684\u4f9d\u636e\u5305\u62ec\uff1a\u95ee\u9898\u80fd\u5426\u5206\u89e3\u3001\u5b50\u95ee\u9898\u662f\u5426\u72ec\u7acb\u3001\u5b50\u95ee\u9898\u80fd\u5426\u5408\u5e76\u3002
        • \u5f52\u5e76\u6392\u5e8f\u662f\u5206\u6cbb\u7b56\u7565\u7684\u5178\u578b\u5e94\u7528\uff0c\u5176\u9012\u5f52\u5730\u5c06\u6570\u7ec4\u5212\u5206\u4e3a\u7b49\u957f\u7684\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u76f4\u5230\u53ea\u5269\u4e00\u4e2a\u5143\u7d20\u65f6\u5f00\u59cb\u9010\u5c42\u5408\u5e76\uff0c\u4ece\u800c\u5b8c\u6210\u6392\u5e8f\u3002
        • \u5f15\u5165\u5206\u6cbb\u7b56\u7565\u5f80\u5f80\u53ef\u4ee5\u63d0\u5347\u7b97\u6cd5\u6548\u7387\u3002\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u7b56\u7565\u51cf\u5c11\u4e86\u64cd\u4f5c\u6570\u91cf\uff1b\u53e6\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u540e\u6709\u5229\u4e8e\u7cfb\u7edf\u7684\u5e76\u884c\u4f18\u5316\u3002
        • \u5206\u6cbb\u65e2\u53ef\u4ee5\u89e3\u51b3\u8bb8\u591a\u7b97\u6cd5\u95ee\u9898\uff0c\u4e5f\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u8bbe\u8ba1\u4e2d\uff0c\u5904\u5904\u53ef\u89c1\u5176\u8eab\u5f71\u3002
        • \u76f8\u8f83\u4e8e\u66b4\u529b\u641c\u7d22\uff0c\u81ea\u9002\u5e94\u641c\u7d22\u6548\u7387\u66f4\u9ad8\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u7684\u641c\u7d22\u7b97\u6cd5\u901a\u5e38\u662f\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u5b9e\u73b0\u7684\u3002
        • \u4e8c\u5206\u67e5\u627e\u662f\u5206\u6cbb\u7b56\u7565\u7684\u53e6\u4e00\u4e2a\u5178\u578b\u5e94\u7528\uff0c\u5b83\u4e0d\u5305\u542b\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u8fdb\u884c\u5408\u5e76\u7684\u6b65\u9aa4\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u5206\u6cbb\u5b9e\u73b0\u4e8c\u5206\u67e5\u627e\u3002
        • \u5728\u6784\u5efa\u4e8c\u53c9\u6811\u7684\u95ee\u9898\u4e2d\uff0c\u6784\u5efa\u6811\uff08\u539f\u95ee\u9898\uff09\u53ef\u4ee5\u5212\u5206\u4e3a\u6784\u5efa\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\uff08\u5b50\u95ee\u9898\uff09\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5212\u5206\u524d\u5e8f\u904d\u5386\u548c\u4e2d\u5e8f\u904d\u5386\u7684\u7d22\u5f15\u533a\u95f4\u6765\u5b9e\u73b0\u3002
        • \u5728\u6c49\u8bfa\u5854\u95ee\u9898\u4e2d\uff0c\u4e00\u4e2a\u89c4\u6a21\u4e3a \\(n\\) \u7684\u95ee\u9898\u53ef\u4ee5\u5212\u5206\u4e3a\u4e24\u4e2a\u89c4\u6a21\u4e3a \\(n-1\\) \u7684\u5b50\u95ee\u9898\u548c\u4e00\u4e2a\u89c4\u6a21\u4e3a \\(1\\) \u7684\u5b50\u95ee\u9898\u3002\u6309\u987a\u5e8f\u89e3\u51b3\u8fd9\u4e09\u4e2a\u5b50\u95ee\u9898\u540e\uff0c\u539f\u95ee\u9898\u968f\u4e4b\u5f97\u5230\u89e3\u51b3\u3002
        "},{"location":"chapter_dynamic_programming/","title":"\u7b2c 14 \u7ae0 \u00a0 \u52a8\u6001\u89c4\u5212","text":"

        Abstract

        \u5c0f\u6eaa\u6c47\u5165\u6cb3\u6d41\uff0c\u6c5f\u6cb3\u6c47\u5165\u5927\u6d77\u3002

        \u52a8\u6001\u89c4\u5212\u5c06\u5c0f\u95ee\u9898\u7684\u89e3\u6c47\u96c6\u6210\u5927\u95ee\u9898\u7684\u7b54\u6848\uff0c\u4e00\u6b65\u6b65\u5f15\u9886\u6211\u4eec\u8d70\u5411\u89e3\u51b3\u95ee\u9898\u7684\u5f7c\u5cb8\u3002

        "},{"location":"chapter_dynamic_programming/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 14.1 \u00a0 \u521d\u63a2\u52a8\u6001\u89c4\u5212
        • 14.2 \u00a0 DP \u95ee\u9898\u7279\u6027
        • 14.3 \u00a0 DP \u89e3\u9898\u601d\u8def
        • 14.4 \u00a0 0-1 \u80cc\u5305\u95ee\u9898
        • 14.5 \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898
        • 14.6 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898
        • 14.7 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_dynamic_programming/dp_problem_features/","title":"14.2 \u00a0 \u52a8\u6001\u89c4\u5212\u95ee\u9898\u7279\u6027","text":"

        \u5728\u4e0a\u4e00\u8282\u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u52a8\u6001\u89c4\u5212\u662f\u5982\u4f55\u901a\u8fc7\u5b50\u95ee\u9898\u5206\u89e3\u6765\u6c42\u89e3\u539f\u95ee\u9898\u7684\u3002\u5b9e\u9645\u4e0a\uff0c\u5b50\u95ee\u9898\u5206\u89e3\u662f\u4e00\u79cd\u901a\u7528\u7684\u7b97\u6cd5\u601d\u8def\uff0c\u5728\u5206\u6cbb\u3001\u52a8\u6001\u89c4\u5212\u3001\u56de\u6eaf\u4e2d\u7684\u4fa7\u91cd\u70b9\u4e0d\u540c\u3002

        • \u5206\u6cbb\u7b97\u6cd5\u9012\u5f52\u5730\u5c06\u539f\u95ee\u9898\u5212\u5206\u4e3a\u591a\u4e2a\u76f8\u4e92\u72ec\u7acb\u7684\u5b50\u95ee\u9898\uff0c\u76f4\u81f3\u6700\u5c0f\u5b50\u95ee\u9898\uff0c\u5e76\u5728\u56de\u6eaf\u4e2d\u5408\u5e76\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u6700\u7ec8\u5f97\u5230\u539f\u95ee\u9898\u7684\u89e3\u3002
        • \u52a8\u6001\u89c4\u5212\u4e5f\u5bf9\u95ee\u9898\u8fdb\u884c\u9012\u5f52\u5206\u89e3\uff0c\u4f46\u4e0e\u5206\u6cbb\u7b97\u6cd5\u7684\u4e3b\u8981\u533a\u522b\u662f\uff0c\u52a8\u6001\u89c4\u5212\u4e2d\u7684\u5b50\u95ee\u9898\u662f\u76f8\u4e92\u4f9d\u8d56\u7684\uff0c\u5728\u5206\u89e3\u8fc7\u7a0b\u4e2d\u4f1a\u51fa\u73b0\u8bb8\u591a\u91cd\u53e0\u5b50\u95ee\u9898\u3002
        • \u56de\u6eaf\u7b97\u6cd5\u5728\u5c1d\u8bd5\u548c\u56de\u9000\u4e2d\u7a77\u4e3e\u6240\u6709\u53ef\u80fd\u7684\u89e3\uff0c\u5e76\u901a\u8fc7\u526a\u679d\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u641c\u7d22\u5206\u652f\u3002\u539f\u95ee\u9898\u7684\u89e3\u7531\u4e00\u7cfb\u5217\u51b3\u7b56\u6b65\u9aa4\u6784\u6210\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6bcf\u4e2a\u51b3\u7b56\u6b65\u9aa4\u4e4b\u524d\u7684\u5b50\u5e8f\u5217\u770b\u4f5c\u4e00\u4e2a\u5b50\u95ee\u9898\u3002

        \u5b9e\u9645\u4e0a\uff0c\u52a8\u6001\u89c4\u5212\u5e38\u7528\u6765\u6c42\u89e3\u6700\u4f18\u5316\u95ee\u9898\uff0c\u5b83\u4eec\u4e0d\u4ec5\u5305\u542b\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u8fd8\u5177\u6709\u53e6\u5916\u4e24\u5927\u7279\u6027\uff1a\u6700\u4f18\u5b50\u7ed3\u6784\u3001\u65e0\u540e\u6548\u6027\u3002

        "},{"location":"chapter_dynamic_programming/dp_problem_features/#1421","title":"14.2.1 \u00a0 \u6700\u4f18\u5b50\u7ed3\u6784","text":"

        \u6211\u4eec\u5bf9\u722c\u697c\u68af\u95ee\u9898\u7a0d\u4f5c\u6539\u52a8\uff0c\u4f7f\u4e4b\u66f4\u52a0\u9002\u5408\u5c55\u793a\u6700\u4f18\u5b50\u7ed3\u6784\u6982\u5ff5\u3002

        \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7

        \u7ed9\u5b9a\u4e00\u4e2a\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\uff0c\u6bcf\u4e00\u9636\u697c\u68af\u4e0a\u90fd\u8d34\u6709\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\uff0c\u8868\u793a\u4f60\u5728\u8be5\u53f0\u9636\u6240\u9700\u8981\u4ed8\u51fa\u7684\u4ee3\u4ef7\u3002\u7ed9\u5b9a\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\u6570\u7ec4 \\(cost\\) \uff0c\u5176\u4e2d \\(cost[i]\\) \u8868\u793a\u5728\u7b2c \\(i\\) \u4e2a\u53f0\u9636\u9700\u8981\u4ed8\u51fa\u7684\u4ee3\u4ef7\uff0c\\(cost[0]\\) \u4e3a\u5730\u9762\uff08\u8d77\u59cb\u70b9\uff09\u3002\u8bf7\u8ba1\u7b97\u6700\u5c11\u9700\u8981\u4ed8\u51fa\u591a\u5c11\u4ee3\u4ef7\u624d\u80fd\u5230\u8fbe\u9876\u90e8\uff1f

        \u5982\u56fe 14-6 \u6240\u793a\uff0c\u82e5\u7b2c \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u9636\u7684\u4ee3\u4ef7\u5206\u522b\u4e3a \\(1\\)\u3001\\(10\\)\u3001\\(1\\) \uff0c\u5219\u4ece\u5730\u9762\u722c\u5230\u7b2c \\(3\\) \u9636\u7684\u6700\u5c0f\u4ee3\u4ef7\u4e3a \\(2\\) \u3002

        \u56fe 14-6 \u00a0 \u722c\u5230\u7b2c 3 \u9636\u7684\u6700\u5c0f\u4ee3\u4ef7

        \u8bbe \\(dp[i]\\) \u4e3a\u722c\u5230\u7b2c \\(i\\) \u9636\u7d2f\u8ba1\u4ed8\u51fa\u7684\u4ee3\u4ef7\uff0c\u7531\u4e8e\u7b2c \\(i\\) \u9636\u53ea\u53ef\u80fd\u4ece \\(i - 1\\) \u9636\u6216 \\(i - 2\\) \u9636\u8d70\u6765\uff0c\u56e0\u6b64 \\(dp[i]\\) \u53ea\u53ef\u80fd\u7b49\u4e8e \\(dp[i - 1] + cost[i]\\) \u6216 \\(dp[i - 2] + cost[i]\\) \u3002\u4e3a\u4e86\u5c3d\u53ef\u80fd\u51cf\u5c11\u4ee3\u4ef7\uff0c\u6211\u4eec\u5e94\u8be5\u9009\u62e9\u4e24\u8005\u4e2d\u8f83\u5c0f\u7684\u90a3\u4e00\u4e2a\uff1a

        \\[ dp[i] = \\min(dp[i-1], dp[i-2]) + cost[i] \\]

        \u8fd9\u4fbf\u53ef\u4ee5\u5f15\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\u7684\u542b\u4e49\uff1a\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u662f\u4ece\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u6784\u5efa\u5f97\u6765\u7684\u3002

        \u672c\u9898\u663e\u7136\u5177\u6709\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\u6211\u4eec\u4ece\u4e24\u4e2a\u5b50\u95ee\u9898\u6700\u4f18\u89e3 \\(dp[i-1]\\) \u548c \\(dp[i-2]\\) \u4e2d\u6311\u9009\u51fa\u8f83\u4f18\u7684\u90a3\u4e00\u4e2a\uff0c\u5e76\u7528\u5b83\u6784\u5efa\u51fa\u539f\u95ee\u9898 \\(dp[i]\\) \u7684\u6700\u4f18\u89e3\u3002

        \u90a3\u4e48\uff0c\u4e0a\u4e00\u8282\u7684\u722c\u697c\u68af\u9898\u76ee\u6709\u6ca1\u6709\u6700\u4f18\u5b50\u7ed3\u6784\u5462\uff1f\u5b83\u7684\u76ee\u6807\u662f\u6c42\u89e3\u65b9\u6848\u6570\u91cf\uff0c\u770b\u4f3c\u662f\u4e00\u4e2a\u8ba1\u6570\u95ee\u9898\uff0c\u4f46\u5982\u679c\u6362\u4e00\u79cd\u95ee\u6cd5\uff1a\u201c\u6c42\u89e3\u6700\u5927\u65b9\u6848\u6570\u91cf\u201d\u3002\u6211\u4eec\u610f\u5916\u5730\u53d1\u73b0\uff0c\u867d\u7136\u9898\u76ee\u4fee\u6539\u524d\u540e\u662f\u7b49\u4ef7\u7684\uff0c\u4f46\u6700\u4f18\u5b50\u7ed3\u6784\u6d6e\u73b0\u51fa\u6765\u4e86\uff1a\u7b2c \\(n\\) \u9636\u6700\u5927\u65b9\u6848\u6570\u91cf\u7b49\u4e8e\u7b2c \\(n-1\\) \u9636\u548c\u7b2c \\(n-2\\) \u9636\u6700\u5927\u65b9\u6848\u6570\u91cf\u4e4b\u548c\u3002\u6240\u4ee5\u8bf4\uff0c\u6700\u4f18\u5b50\u7ed3\u6784\u7684\u89e3\u91ca\u65b9\u5f0f\u6bd4\u8f83\u7075\u6d3b\uff0c\u5728\u4e0d\u540c\u95ee\u9898\u4e2d\u4f1a\u6709\u4e0d\u540c\u7684\u542b\u4e49\u3002

        \u6839\u636e\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff0c\u4ee5\u53ca\u521d\u59cb\u72b6\u6001 \\(dp[1] = cost[1]\\) \u548c \\(dp[2] = cost[2]\\) \uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f97\u5230\u52a8\u6001\u89c4\u5212\u4ee3\u7801\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_cost_climbing_stairs_dp.py
        def min_cost_climbing_stairs_dp(cost: list[int]) -> int:\n    \"\"\"\u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n    n = len(cost) - 1\n    if n == 1 or n == 2:\n        return cost[n]\n    # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    dp = [0] * (n + 1)\n    # \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1], dp[2] = cost[1], cost[2]\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i in range(3, n + 1):\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n    return dp[n]\n
        min_cost_climbing_stairs_dp.cpp
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(vector<int> &cost) {\n    int n = cost.size() - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    vector<int> dp(n + 1);\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
        min_cost_climbing_stairs_dp.java
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(int[] cost) {\n    int n = cost.length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    int[] dp = new int[n + 1];\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
        min_cost_climbing_stairs_dp.cs
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint MinCostClimbingStairsDP(int[] cost) {\n    int n = cost.Length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    int[] dp = new int[n + 1];\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i] = Math.Min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
        min_cost_climbing_stairs_dp.go
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDP(cost []int) int {\n    n := len(cost) - 1\n    if n == 1 || n == 2 {\n        return cost[n]\n    }\n    min := func(a, b int) int {\n        if a < b {\n            return a\n        }\n        return b\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    dp := make([]int, n+1)\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = cost[1]\n    dp[2] = cost[2]\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i := 3; i <= n; i++ {\n        dp[i] = min(dp[i-1], dp[i-2]) + cost[i]\n    }\n    return dp[n]\n}\n
        min_cost_climbing_stairs_dp.swift
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDP(cost: [Int]) -> Int {\n    let n = cost.count - 1\n    if n == 1 || n == 2 {\n        return cost[n]\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    var dp = Array(repeating: 0, count: n + 1)\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = cost[1]\n    dp[2] = cost[2]\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i in 3 ... n {\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n    }\n    return dp[n]\n}\n
        min_cost_climbing_stairs_dp.js
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDP(cost) {\n    const n = cost.length - 1;\n    if (n === 1 || n === 2) {\n        return cost[n];\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    const dp = new Array(n + 1);\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (let i = 3; i <= n; i++) {\n        dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
        min_cost_climbing_stairs_dp.ts
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDP(cost: Array<number>): number {\n    const n = cost.length - 1;\n    if (n === 1 || n === 2) {\n        return cost[n];\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    const dp = new Array(n + 1);\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (let i = 3; i <= n; i++) {\n        dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
        min_cost_climbing_stairs_dp.dart
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(List<int> cost) {\n  int n = cost.length - 1;\n  if (n == 1 || n == 2) return cost[n];\n  // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n  List<int> dp = List.filled(n + 1, 0);\n  // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n  dp[1] = cost[1];\n  dp[2] = cost[2];\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n  for (int i = 3; i <= n; i++) {\n    dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n  }\n  return dp[n];\n}\n
        min_cost_climbing_stairs_dp.rs
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfn min_cost_climbing_stairs_dp(cost: &[i32]) -> i32 {\n    let n = cost.len() - 1;\n    if n == 1 || n == 2 {\n        return cost[n];\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    let mut dp = vec![-1; n + 1];\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i in 3..=n {\n        dp[i] = cmp::min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    dp[n]\n}\n
        min_cost_climbing_stairs_dp.c
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(int cost[], int costSize) {\n    int n = costSize - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    int *dp = calloc(n + 1, sizeof(int));\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i] = myMin(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    int res = dp[n];\n    // \u91ca\u653e\u5185\u5b58\n    free(dp);\n    return res;\n}\n
        min_cost_climbing_stairs_dp.kt
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfun minCostClimbingStairsDP(cost: IntArray): Int {\n    val n = cost.size - 1\n    if (n == 1 || n == 2) return cost[n]\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    val dp = IntArray(n + 1)\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = cost[1]\n    dp[2] = cost[2]\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (i in 3..n) {\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n    }\n    return dp[n]\n}\n
        min_cost_climbing_stairs_dp.rb
        ### \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 ###\ndef min_cost_climbing_stairs_dp(cost)\n  n = cost.length - 1\n  return cost[n] if n == 1 || n == 2\n  # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n  dp = Array.new(n + 1, 0)\n  # \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n  dp[1], dp[2] = cost[1], cost[2]\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n  (3...(n + 1)).each { |i| dp[i] = [dp[i - 1], dp[i - 2]].min + cost[i] }\n  dp[n]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 14-7 \u5c55\u793a\u4e86\u4ee5\u4e0a\u4ee3\u7801\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b\u3002

        \u56fe 14-7 \u00a0 \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

        \u672c\u9898\u4e5f\u53ef\u4ee5\u8fdb\u884c\u7a7a\u95f4\u4f18\u5316\uff0c\u5c06\u4e00\u7ef4\u538b\u7f29\u81f3\u96f6\u7ef4\uff0c\u4f7f\u5f97\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n)\\) \u964d\u81f3 \\(O(1)\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_cost_climbing_stairs_dp.py
        def min_cost_climbing_stairs_dp_comp(cost: list[int]) -> int:\n    \"\"\"\u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n    n = len(cost) - 1\n    if n == 1 or n == 2:\n        return cost[n]\n    a, b = cost[1], cost[2]\n    for i in range(3, n + 1):\n        a, b = b, min(a, b) + cost[i]\n    return b\n
        min_cost_climbing_stairs_dp.cpp
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(vector<int> &cost) {\n    int n = cost.size() - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
        min_cost_climbing_stairs_dp.java
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(int[] cost) {\n    int n = cost.length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = Math.min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
        min_cost_climbing_stairs_dp.cs
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint MinCostClimbingStairsDPComp(int[] cost) {\n    int n = cost.Length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = Math.Min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
        min_cost_climbing_stairs_dp.go
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDPComp(cost []int) int {\n    n := len(cost) - 1\n    if n == 1 || n == 2 {\n        return cost[n]\n    }\n    min := func(a, b int) int {\n        if a < b {\n            return a\n        }\n        return b\n    }\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    a, b := cost[1], cost[2]\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i := 3; i <= n; i++ {\n        tmp := b\n        b = min(a, tmp) + cost[i]\n        a = tmp\n    }\n    return b\n}\n
        min_cost_climbing_stairs_dp.swift
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDPComp(cost: [Int]) -> Int {\n    let n = cost.count - 1\n    if n == 1 || n == 2 {\n        return cost[n]\n    }\n    var (a, b) = (cost[1], cost[2])\n    for i in 3 ... n {\n        (a, b) = (b, min(a, b) + cost[i])\n    }\n    return b\n}\n
        min_cost_climbing_stairs_dp.js
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDPComp(cost) {\n    const n = cost.length - 1;\n    if (n === 1 || n === 2) {\n        return cost[n];\n    }\n    let a = cost[1],\n        b = cost[2];\n    for (let i = 3; i <= n; i++) {\n        const tmp = b;\n        b = Math.min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
        min_cost_climbing_stairs_dp.ts
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDPComp(cost: Array<number>): number {\n    const n = cost.length - 1;\n    if (n === 1 || n === 2) {\n        return cost[n];\n    }\n    let a = cost[1],\n        b = cost[2];\n    for (let i = 3; i <= n; i++) {\n        const tmp = b;\n        b = Math.min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
        min_cost_climbing_stairs_dp.dart
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(List<int> cost) {\n  int n = cost.length - 1;\n  if (n == 1 || n == 2) return cost[n];\n  int a = cost[1], b = cost[2];\n  for (int i = 3; i <= n; i++) {\n    int tmp = b;\n    b = min(a, tmp) + cost[i];\n    a = tmp;\n  }\n  return b;\n}\n
        min_cost_climbing_stairs_dp.rs
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn min_cost_climbing_stairs_dp_comp(cost: &[i32]) -> i32 {\n    let n = cost.len() - 1;\n    if n == 1 || n == 2 {\n        return cost[n];\n    };\n    let (mut a, mut b) = (cost[1], cost[2]);\n    for i in 3..=n {\n        let tmp = b;\n        b = cmp::min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    b\n}\n
        min_cost_climbing_stairs_dp.c
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(int cost[], int costSize) {\n    int n = costSize - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = myMin(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
        min_cost_climbing_stairs_dp.kt
        /* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfun minCostClimbingStairsDPComp(cost: IntArray): Int {\n    val n = cost.size - 1\n    if (n == 1 || n == 2) return cost[n]\n    var a = cost[1]\n    var b = cost[2]\n    for (i in 3..n) {\n        val tmp = b\n        b = min(a, tmp) + cost[i]\n        a = tmp\n    }\n    return b\n}\n
        min_cost_climbing_stairs_dp.rb
        ### \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 ###\ndef min_cost_climbing_stairs_dp(cost)\n  n = cost.length - 1\n  return cost[n] if n == 1 || n == 2\n  # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n  dp = Array.new(n + 1, 0)\n  # \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n  dp[1], dp[2] = cost[1], cost[2]\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n  (3...(n + 1)).each { |i| dp[i] = [dp[i - 1], dp[i - 2]].min + cost[i] }\n  dp[n]\nend\n\n# \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\ndef min_cost_climbing_stairs_dp_comp(cost)\n  n = cost.length - 1\n  return cost[n] if n == 1 || n == 2\n  a, b = cost[1], cost[2]\n  (3...(n + 1)).each { |i| a, b = b, [a, b].min + cost[i] }\n  b\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_dynamic_programming/dp_problem_features/#1422","title":"14.2.2 \u00a0 \u65e0\u540e\u6548\u6027","text":"

        \u65e0\u540e\u6548\u6027\u662f\u52a8\u6001\u89c4\u5212\u80fd\u591f\u6709\u6548\u89e3\u51b3\u95ee\u9898\u7684\u91cd\u8981\u7279\u6027\u4e4b\u4e00\uff0c\u5176\u5b9a\u4e49\u4e3a\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u786e\u5b9a\u7684\u72b6\u6001\uff0c\u5b83\u7684\u672a\u6765\u53d1\u5c55\u53ea\u4e0e\u5f53\u524d\u72b6\u6001\u6709\u5173\uff0c\u800c\u4e0e\u8fc7\u53bb\u7ecf\u5386\u7684\u6240\u6709\u72b6\u6001\u65e0\u5173\u3002

        \u4ee5\u722c\u697c\u68af\u95ee\u9898\u4e3a\u4f8b\uff0c\u7ed9\u5b9a\u72b6\u6001 \\(i\\) \uff0c\u5b83\u4f1a\u53d1\u5c55\u51fa\u72b6\u6001 \\(i+1\\) \u548c\u72b6\u6001 \\(i+2\\) \uff0c\u5206\u522b\u5bf9\u5e94\u8df3 \\(1\\) \u6b65\u548c\u8df3 \\(2\\) \u6b65\u3002\u5728\u505a\u51fa\u8fd9\u4e24\u79cd\u9009\u62e9\u65f6\uff0c\u6211\u4eec\u65e0\u987b\u8003\u8651\u72b6\u6001 \\(i\\) \u4e4b\u524d\u7684\u72b6\u6001\uff0c\u5b83\u4eec\u5bf9\u72b6\u6001 \\(i\\) \u7684\u672a\u6765\u6ca1\u6709\u5f71\u54cd\u3002

        \u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u7ed9\u722c\u697c\u68af\u95ee\u9898\u6dfb\u52a0\u4e00\u4e2a\u7ea6\u675f\uff0c\u60c5\u51b5\u5c31\u4e0d\u4e00\u6837\u4e86\u3002

        \u5e26\u7ea6\u675f\u722c\u697c\u68af

        \u7ed9\u5b9a\u4e00\u4e2a\u5171\u6709 \\(n\\) \u9636\u7684\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\uff0c\u4f46\u4e0d\u80fd\u8fde\u7eed\u4e24\u8f6e\u8df3 \\(1\\) \u9636\uff0c\u8bf7\u95ee\u6709\u591a\u5c11\u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\uff1f

        \u5982\u56fe 14-8 \u6240\u793a\uff0c\u722c\u4e0a\u7b2c \\(3\\) \u9636\u4ec5\u5269 \\(2\\) \u79cd\u53ef\u884c\u65b9\u6848\uff0c\u5176\u4e2d\u8fde\u7eed\u4e09\u6b21\u8df3 \\(1\\) \u9636\u7684\u65b9\u6848\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\uff0c\u56e0\u6b64\u88ab\u820d\u5f03\u3002

        \u56fe 14-8 \u00a0 \u5e26\u7ea6\u675f\u722c\u5230\u7b2c 3 \u9636\u7684\u65b9\u6848\u6570\u91cf

        \u5728\u8be5\u95ee\u9898\u4e2d\uff0c\u5982\u679c\u4e0a\u4e00\u8f6e\u662f\u8df3 \\(1\\) \u9636\u4e0a\u6765\u7684\uff0c\u90a3\u4e48\u4e0b\u4e00\u8f6e\u5c31\u5fc5\u987b\u8df3 \\(2\\) \u9636\u3002\u8fd9\u610f\u5473\u7740\uff0c\u4e0b\u4e00\u6b65\u9009\u62e9\u4e0d\u80fd\u7531\u5f53\u524d\u72b6\u6001\uff08\u5f53\u524d\u6240\u5728\u697c\u68af\u9636\u6570\uff09\u72ec\u7acb\u51b3\u5b9a\uff0c\u8fd8\u548c\u524d\u4e00\u4e2a\u72b6\u6001\uff08\u4e0a\u4e00\u8f6e\u6240\u5728\u697c\u68af\u9636\u6570\uff09\u6709\u5173\u3002

        \u4e0d\u96be\u53d1\u73b0\uff0c\u6b64\u95ee\u9898\u5df2\u4e0d\u6ee1\u8db3\u65e0\u540e\u6548\u6027\uff0c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b \\(dp[i] = dp[i-1] + dp[i-2]\\) \u4e5f\u5931\u6548\u4e86\uff0c\u56e0\u4e3a \\(dp[i-1]\\) \u4ee3\u8868\u672c\u8f6e\u8df3 \\(1\\) \u9636\uff0c\u4f46\u5176\u4e2d\u5305\u542b\u4e86\u8bb8\u591a\u201c\u4e0a\u4e00\u8f6e\u662f\u8df3 \\(1\\) \u9636\u4e0a\u6765\u7684\u201d\u65b9\u6848\uff0c\u800c\u4e3a\u4e86\u6ee1\u8db3\u7ea6\u675f\uff0c\u6211\u4eec\u5c31\u4e0d\u80fd\u5c06 \\(dp[i-1]\\) \u76f4\u63a5\u8ba1\u5165 \\(dp[i]\\) \u4e2d\u3002

        \u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u6269\u5c55\u72b6\u6001\u5b9a\u4e49\uff1a\u72b6\u6001 \\([i, j]\\) \u8868\u793a\u5904\u5728\u7b2c \\(i\\) \u9636\u5e76\u4e14\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(j\\) \u9636\uff0c\u5176\u4e2d \\(j \\in \\{1, 2\\}\\) \u3002\u6b64\u72b6\u6001\u5b9a\u4e49\u6709\u6548\u5730\u533a\u5206\u4e86\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(1\\) \u9636\u8fd8\u662f \\(2\\) \u9636\uff0c\u6211\u4eec\u53ef\u4ee5\u636e\u6b64\u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u4ece\u4f55\u800c\u6765\u7684\u3002

        • \u5f53\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(1\\) \u9636\u65f6\uff0c\u4e0a\u4e0a\u4e00\u8f6e\u53ea\u80fd\u9009\u62e9\u8df3 \\(2\\) \u9636\uff0c\u5373 \\(dp[i, 1]\\) \u53ea\u80fd\u4ece \\(dp[i-1, 2]\\) \u8f6c\u79fb\u8fc7\u6765\u3002
        • \u5f53\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(2\\) \u9636\u65f6\uff0c\u4e0a\u4e0a\u4e00\u8f6e\u53ef\u9009\u62e9\u8df3 \\(1\\) \u9636\u6216\u8df3 \\(2\\) \u9636\uff0c\u5373 \\(dp[i, 2]\\) \u53ef\u4ee5\u4ece \\(dp[i-2, 1]\\) \u6216 \\(dp[i-2, 2]\\) \u8f6c\u79fb\u8fc7\u6765\u3002

        \u5982\u56fe 14-9 \u6240\u793a\uff0c\u5728\u8be5\u5b9a\u4e49\u4e0b\uff0c\\(dp[i, j]\\) \u8868\u793a\u72b6\u6001 \\([i, j]\\) \u5bf9\u5e94\u7684\u65b9\u6848\u6570\u3002\u6b64\u65f6\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a

        \\[ \\begin{cases} dp[i, 1] = dp[i-1, 2] \\\\ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2] \\end{cases} \\]

        \u56fe 14-9 \u00a0 \u8003\u8651\u7ea6\u675f\u4e0b\u7684\u9012\u63a8\u5173\u7cfb

        \u6700\u7ec8\uff0c\u8fd4\u56de \\(dp[n, 1] + dp[n, 2]\\) \u5373\u53ef\uff0c\u4e24\u8005\u4e4b\u548c\u4ee3\u8868\u722c\u5230\u7b2c \\(n\\) \u9636\u7684\u65b9\u6848\u603b\u6570\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_constraint_dp.py
        def climbing_stairs_constraint_dp(n: int) -> int:\n    \"\"\"\u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n    if n == 1 or n == 2:\n        return 1\n    # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    dp = [[0] * 3 for _ in range(n + 1)]\n    # \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1][1], dp[1][2] = 1, 0\n    dp[2][1], dp[2][2] = 0, 1\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i in range(3, n + 1):\n        dp[i][1] = dp[i - 1][2]\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n    return dp[n][1] + dp[n][2]\n
        climbing_stairs_constraint_dp.cpp
        /* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    vector<vector<int>> dp(n + 1, vector<int>(3, 0));\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
        climbing_stairs_constraint_dp.java
        /* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    int[][] dp = new int[n + 1][3];\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
        climbing_stairs_constraint_dp.cs
        /* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint ClimbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    int[,] dp = new int[n + 1, 3];\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1, 1] = 1;\n    dp[1, 2] = 0;\n    dp[2, 1] = 0;\n    dp[2, 2] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i, 1] = dp[i - 1, 2];\n        dp[i, 2] = dp[i - 2, 1] + dp[i - 2, 2];\n    }\n    return dp[n, 1] + dp[n, 2];\n}\n
        climbing_stairs_constraint_dp.go
        /* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsConstraintDP(n int) int {\n    if n == 1 || n == 2 {\n        return 1\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    dp := make([][3]int, n+1)\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1][1] = 1\n    dp[1][2] = 0\n    dp[2][1] = 0\n    dp[2][2] = 1\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i := 3; i <= n; i++ {\n        dp[i][1] = dp[i-1][2]\n        dp[i][2] = dp[i-2][1] + dp[i-2][2]\n    }\n    return dp[n][1] + dp[n][2]\n}\n
        climbing_stairs_constraint_dp.swift
        /* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsConstraintDP(n: Int) -> Int {\n    if n == 1 || n == 2 {\n        return 1\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    var dp = Array(repeating: Array(repeating: 0, count: 3), count: n + 1)\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1][1] = 1\n    dp[1][2] = 0\n    dp[2][1] = 0\n    dp[2][2] = 1\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i in 3 ... n {\n        dp[i][1] = dp[i - 1][2]\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n    }\n    return dp[n][1] + dp[n][2]\n}\n
        climbing_stairs_constraint_dp.js
        /* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsConstraintDP(n) {\n    if (n === 1 || n === 2) {\n        return 1;\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    const dp = Array.from(new Array(n + 1), () => new Array(3));\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (let i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
        climbing_stairs_constraint_dp.ts
        /* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsConstraintDP(n: number): number {\n    if (n === 1 || n === 2) {\n        return 1;\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    const dp = Array.from({ length: n + 1 }, () => new Array(3));\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (let i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
        climbing_stairs_constraint_dp.dart
        /* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\n  if (n == 1 || n == 2) {\n    return 1;\n  }\n  // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(3, 0));\n  // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n  dp[1][1] = 1;\n  dp[1][2] = 0;\n  dp[2][1] = 0;\n  dp[2][2] = 1;\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n  for (int i = 3; i <= n; i++) {\n    dp[i][1] = dp[i - 1][2];\n    dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n  }\n  return dp[n][1] + dp[n][2];\n}\n
        climbing_stairs_constraint_dp.rs
        /* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfn climbing_stairs_constraint_dp(n: usize) -> i32 {\n    if n == 1 || n == 2 {\n        return 1;\n    };\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    let mut dp = vec![vec![-1; 3]; n + 1];\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i in 3..=n {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    dp[n][1] + dp[n][2]\n}\n
        climbing_stairs_constraint_dp.c
        /* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(3, sizeof(int));\n    }\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    int res = dp[n][1] + dp[n][2];\n    // \u91ca\u653e\u5185\u5b58\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    free(dp);\n    return res;\n}\n
        climbing_stairs_constraint_dp.kt
        /* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfun climbingStairsConstraintDP(n: Int): Int {\n    if (n == 1 || n == 2) {\n        return 1\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    val dp = Array(n + 1) { IntArray(3) }\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1][1] = 1\n    dp[1][2] = 0\n    dp[2][1] = 0\n    dp[2][2] = 1\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (i in 3..n) {\n        dp[i][1] = dp[i - 1][2]\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n    }\n    return dp[n][1] + dp[n][2]\n}\n
        climbing_stairs_constraint_dp.rb
        ### \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 ###\ndef climbing_stairs_constraint_dp(n)\n  return 1 if n == 1 || n == 2\n\n  # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n  dp = Array.new(n + 1) { Array.new(3, 0) }\n  # \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n  dp[1][1], dp[1][2] = 1, 0\n  dp[2][1], dp[2][2] = 0, 1\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n  for i in 3...(n + 1)\n    dp[i][1] = dp[i - 1][2]\n    dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n  end\n\n  dp[n][1] + dp[n][2]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5728\u4e0a\u9762\u7684\u6848\u4f8b\u4e2d\uff0c\u7531\u4e8e\u4ec5\u9700\u591a\u8003\u8651\u524d\u9762\u4e00\u4e2a\u72b6\u6001\uff0c\u56e0\u6b64\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u901a\u8fc7\u6269\u5c55\u72b6\u6001\u5b9a\u4e49\uff0c\u4f7f\u5f97\u95ee\u9898\u91cd\u65b0\u6ee1\u8db3\u65e0\u540e\u6548\u6027\u3002\u7136\u800c\uff0c\u67d0\u4e9b\u95ee\u9898\u5177\u6709\u975e\u5e38\u4e25\u91cd\u7684\u201c\u6709\u540e\u6548\u6027\u201d\u3002

        \u722c\u697c\u68af\u4e0e\u969c\u788d\u751f\u6210

        \u7ed9\u5b9a\u4e00\u4e2a\u5171\u6709 \\(n\\) \u9636\u7684\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\u3002\u89c4\u5b9a\u5f53\u722c\u5230\u7b2c \\(i\\) \u9636\u65f6\uff0c\u7cfb\u7edf\u81ea\u52a8\u4f1a\u5728\u7b2c \\(2i\\) \u9636\u4e0a\u653e\u4e0a\u969c\u788d\u7269\uff0c\u4e4b\u540e\u6240\u6709\u8f6e\u90fd\u4e0d\u5141\u8bb8\u8df3\u5230\u7b2c \\(2i\\) \u9636\u4e0a\u3002\u4f8b\u5982\uff0c\u524d\u4e24\u8f6e\u5206\u522b\u8df3\u5230\u4e86\u7b2c \\(2\\)\u3001\\(3\\) \u9636\u4e0a\uff0c\u5219\u4e4b\u540e\u5c31\u4e0d\u80fd\u8df3\u5230\u7b2c \\(4\\)\u3001\\(6\\) \u9636\u4e0a\u3002\u8bf7\u95ee\u6709\u591a\u5c11\u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\uff1f

        \u5728\u8fd9\u4e2a\u95ee\u9898\u4e2d\uff0c\u4e0b\u6b21\u8df3\u8dc3\u4f9d\u8d56\u8fc7\u53bb\u6240\u6709\u7684\u72b6\u6001\uff0c\u56e0\u4e3a\u6bcf\u4e00\u6b21\u8df3\u8dc3\u90fd\u4f1a\u5728\u66f4\u9ad8\u7684\u9636\u68af\u4e0a\u8bbe\u7f6e\u969c\u788d\uff0c\u5e76\u5f71\u54cd\u672a\u6765\u7684\u8df3\u8dc3\u3002\u5bf9\u4e8e\u8fd9\u7c7b\u95ee\u9898\uff0c\u52a8\u6001\u89c4\u5212\u5f80\u5f80\u96be\u4ee5\u89e3\u51b3\u3002

        \u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u590d\u6742\u7684\u7ec4\u5408\u4f18\u5316\u95ee\u9898\uff08\u4f8b\u5982\u65c5\u884c\u5546\u95ee\u9898\uff09\u4e0d\u6ee1\u8db3\u65e0\u540e\u6548\u6027\u3002\u5bf9\u4e8e\u8fd9\u7c7b\u95ee\u9898\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u9009\u62e9\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\uff0c\u4f8b\u5982\u542f\u53d1\u5f0f\u641c\u7d22\u3001\u9057\u4f20\u7b97\u6cd5\u3001\u5f3a\u5316\u5b66\u4e60\u7b49\uff0c\u4ece\u800c\u5728\u6709\u9650\u65f6\u95f4\u5185\u5f97\u5230\u53ef\u7528\u7684\u5c40\u90e8\u6700\u4f18\u89e3\u3002

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/","title":"14.3 \u00a0 \u52a8\u6001\u89c4\u5212\u89e3\u9898\u601d\u8def","text":"

        \u4e0a\u4e24\u8282\u4ecb\u7ecd\u4e86\u52a8\u6001\u89c4\u5212\u95ee\u9898\u7684\u4e3b\u8981\u7279\u5f81\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u4e00\u8d77\u63a2\u7a76\u4e24\u4e2a\u66f4\u52a0\u5b9e\u7528\u7684\u95ee\u9898\u3002

        1. \u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u95ee\u9898\u662f\u4e0d\u662f\u52a8\u6001\u89c4\u5212\u95ee\u9898\uff1f
        2. \u6c42\u89e3\u52a8\u6001\u89c4\u5212\u95ee\u9898\u8be5\u4ece\u4f55\u5904\u5165\u624b\uff0c\u5b8c\u6574\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f
        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1431","title":"14.3.1 \u00a0 \u95ee\u9898\u5224\u65ad","text":"

        \u603b\u7684\u6765\u8bf4\uff0c\u5982\u679c\u4e00\u4e2a\u95ee\u9898\u5305\u542b\u91cd\u53e0\u5b50\u95ee\u9898\u3001\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u5e76\u6ee1\u8db3\u65e0\u540e\u6548\u6027\uff0c\u90a3\u4e48\u5b83\u901a\u5e38\u9002\u5408\u7528\u52a8\u6001\u89c4\u5212\u6c42\u89e3\u3002\u7136\u800c\uff0c\u6211\u4eec\u5f88\u96be\u4ece\u95ee\u9898\u63cf\u8ff0\u4e2d\u76f4\u63a5\u63d0\u53d6\u51fa\u8fd9\u4e9b\u7279\u6027\u3002\u56e0\u6b64\u6211\u4eec\u901a\u5e38\u4f1a\u653e\u5bbd\u6761\u4ef6\uff0c\u5148\u89c2\u5bdf\u95ee\u9898\u662f\u5426\u9002\u5408\u4f7f\u7528\u56de\u6eaf\uff08\u7a77\u4e3e\uff09\u89e3\u51b3\u3002

        \u9002\u5408\u7528\u56de\u6eaf\u89e3\u51b3\u7684\u95ee\u9898\u901a\u5e38\u6ee1\u8db3\u201c\u51b3\u7b56\u6811\u6a21\u578b\u201d\uff0c\u8fd9\u79cd\u95ee\u9898\u53ef\u4ee5\u4f7f\u7528\u6811\u5f62\u7ed3\u6784\u6765\u63cf\u8ff0\uff0c\u5176\u4e2d\u6bcf\u4e00\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u51b3\u7b56\uff0c\u6bcf\u4e00\u6761\u8def\u5f84\u4ee3\u8868\u4e00\u4e2a\u51b3\u7b56\u5e8f\u5217\u3002

        \u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c\u95ee\u9898\u5305\u542b\u660e\u786e\u7684\u51b3\u7b56\u6982\u5ff5\uff0c\u5e76\u4e14\u89e3\u662f\u901a\u8fc7\u4e00\u7cfb\u5217\u51b3\u7b56\u4ea7\u751f\u7684\uff0c\u90a3\u4e48\u5b83\u5c31\u6ee1\u8db3\u51b3\u7b56\u6811\u6a21\u578b\uff0c\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528\u56de\u6eaf\u6765\u89e3\u51b3\u3002

        \u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u52a8\u6001\u89c4\u5212\u95ee\u9898\u8fd8\u6709\u4e00\u4e9b\u5224\u65ad\u7684\u201c\u52a0\u5206\u9879\u201d\u3002

        • \u95ee\u9898\u5305\u542b\u6700\u5927\uff08\u5c0f\uff09\u6216\u6700\u591a\uff08\u5c11\uff09\u7b49\u6700\u4f18\u5316\u63cf\u8ff0\u3002
        • \u95ee\u9898\u7684\u72b6\u6001\u80fd\u591f\u4f7f\u7528\u4e00\u4e2a\u5217\u8868\u3001\u591a\u7ef4\u77e9\u9635\u6216\u6811\u6765\u8868\u793a\uff0c\u5e76\u4e14\u4e00\u4e2a\u72b6\u6001\u4e0e\u5176\u5468\u56f4\u7684\u72b6\u6001\u5b58\u5728\u9012\u63a8\u5173\u7cfb\u3002

        \u76f8\u5e94\u5730\uff0c\u4e5f\u5b58\u5728\u4e00\u4e9b\u201c\u51cf\u5206\u9879\u201d\u3002

        • \u95ee\u9898\u7684\u76ee\u6807\u662f\u627e\u51fa\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u800c\u4e0d\u662f\u627e\u51fa\u6700\u4f18\u89e3\u3002
        • \u95ee\u9898\u63cf\u8ff0\u4e2d\u6709\u660e\u663e\u7684\u6392\u5217\u7ec4\u5408\u7684\u7279\u5f81\uff0c\u9700\u8981\u8fd4\u56de\u5177\u4f53\u7684\u591a\u4e2a\u65b9\u6848\u3002

        \u5982\u679c\u4e00\u4e2a\u95ee\u9898\u6ee1\u8db3\u51b3\u7b56\u6811\u6a21\u578b\uff0c\u5e76\u5177\u6709\u8f83\u4e3a\u660e\u663e\u7684\u201c\u52a0\u5206\u9879\u201d\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5047\u8bbe\u5b83\u662f\u4e00\u4e2a\u52a8\u6001\u89c4\u5212\u95ee\u9898\uff0c\u5e76\u5728\u6c42\u89e3\u8fc7\u7a0b\u4e2d\u9a8c\u8bc1\u5b83\u3002

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1432","title":"14.3.2 \u00a0 \u95ee\u9898\u6c42\u89e3\u6b65\u9aa4","text":"

        \u52a8\u6001\u89c4\u5212\u7684\u89e3\u9898\u6d41\u7a0b\u4f1a\u56e0\u95ee\u9898\u7684\u6027\u8d28\u548c\u96be\u5ea6\u800c\u6709\u6240\u4e0d\u540c\uff0c\u4f46\u901a\u5e38\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u63cf\u8ff0\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u5efa\u7acb \\(dp\\) \u8868\uff0c\u63a8\u5bfc\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff0c\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u7b49\u3002

        \u4e3a\u4e86\u66f4\u5f62\u8c61\u5730\u5c55\u793a\u89e3\u9898\u6b65\u9aa4\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u7ecf\u5178\u95ee\u9898\u201c\u6700\u5c0f\u8def\u5f84\u548c\u201d\u6765\u4e3e\u4f8b\u3002

        Question

        \u7ed9\u5b9a\u4e00\u4e2a \\(n \\times m\\) \u7684\u4e8c\u7ef4\u7f51\u683c grid \uff0c\u7f51\u683c\u4e2d\u7684\u6bcf\u4e2a\u5355\u5143\u683c\u5305\u542b\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\uff0c\u8868\u793a\u8be5\u5355\u5143\u683c\u7684\u4ee3\u4ef7\u3002\u673a\u5668\u4eba\u4ee5\u5de6\u4e0a\u89d2\u5355\u5143\u683c\u4e3a\u8d77\u59cb\u70b9\uff0c\u6bcf\u6b21\u53ea\u80fd\u5411\u4e0b\u6216\u8005\u5411\u53f3\u79fb\u52a8\u4e00\u6b65\uff0c\u76f4\u81f3\u5230\u8fbe\u53f3\u4e0b\u89d2\u5355\u5143\u683c\u3002\u8bf7\u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230\u53f3\u4e0b\u89d2\u7684\u6700\u5c0f\u8def\u5f84\u548c\u3002

        \u56fe 14-10 \u5c55\u793a\u4e86\u4e00\u4e2a\u4f8b\u5b50\uff0c\u7ed9\u5b9a\u7f51\u683c\u7684\u6700\u5c0f\u8def\u5f84\u548c\u4e3a \\(13\\) \u3002

        \u56fe 14-10 \u00a0 \u6700\u5c0f\u8def\u5f84\u548c\u793a\u4f8b\u6570\u636e

        \u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868

        \u672c\u9898\u7684\u6bcf\u4e00\u8f6e\u7684\u51b3\u7b56\u5c31\u662f\u4ece\u5f53\u524d\u683c\u5b50\u5411\u4e0b\u6216\u5411\u53f3\u8d70\u4e00\u6b65\u3002\u8bbe\u5f53\u524d\u683c\u5b50\u7684\u884c\u5217\u7d22\u5f15\u4e3a \\([i, j]\\) \uff0c\u5219\u5411\u4e0b\u6216\u5411\u53f3\u8d70\u4e00\u6b65\u540e\uff0c\u7d22\u5f15\u53d8\u4e3a \\([i+1, j]\\) \u6216 \\([i, j+1]\\) \u3002\u56e0\u6b64\uff0c\u72b6\u6001\u5e94\u5305\u542b\u884c\u7d22\u5f15\u548c\u5217\u7d22\u5f15\u4e24\u4e2a\u53d8\u91cf\uff0c\u8bb0\u4e3a \\([i, j]\\) \u3002

        \u72b6\u6001 \\([i, j]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u4e3a\uff1a\u4ece\u8d77\u59cb\u70b9 \\([0, 0]\\) \u8d70\u5230 \\([i, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\uff0c\u89e3\u8bb0\u4e3a \\(dp[i, j]\\) \u3002

        \u81f3\u6b64\uff0c\u6211\u4eec\u5c31\u5f97\u5230\u4e86\u56fe 14-11 \u6240\u793a\u7684\u4e8c\u7ef4 \\(dp\\) \u77e9\u9635\uff0c\u5176\u5c3a\u5bf8\u4e0e\u8f93\u5165\u7f51\u683c \\(grid\\) \u76f8\u540c\u3002

        \u56fe 14-11 \u00a0 \u72b6\u6001\u5b9a\u4e49\u4e0e dp \u8868

        Note

        \u52a8\u6001\u89c4\u5212\u548c\u56de\u6eaf\u8fc7\u7a0b\u53ef\u4ee5\u63cf\u8ff0\u4e3a\u4e00\u4e2a\u51b3\u7b56\u5e8f\u5217\uff0c\u800c\u72b6\u6001\u7531\u6240\u6709\u51b3\u7b56\u53d8\u91cf\u6784\u6210\u3002\u5b83\u5e94\u5f53\u5305\u542b\u63cf\u8ff0\u89e3\u9898\u8fdb\u5ea6\u7684\u6240\u6709\u53d8\u91cf\uff0c\u5176\u5305\u542b\u4e86\u8db3\u591f\u7684\u4fe1\u606f\uff0c\u80fd\u591f\u7528\u6765\u63a8\u5bfc\u51fa\u4e0b\u4e00\u4e2a\u72b6\u6001\u3002

        \u6bcf\u4e2a\u72b6\u6001\u90fd\u5bf9\u5e94\u4e00\u4e2a\u5b50\u95ee\u9898\uff0c\u6211\u4eec\u4f1a\u5b9a\u4e49\u4e00\u4e2a \\(dp\\) \u8868\u6765\u5b58\u50a8\u6240\u6709\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u72b6\u6001\u7684\u6bcf\u4e2a\u72ec\u7acb\u53d8\u91cf\u90fd\u662f \\(dp\\) \u8868\u7684\u4e00\u4e2a\u7ef4\u5ea6\u3002\u4ece\u672c\u8d28\u4e0a\u770b\uff0c\\(dp\\) \u8868\u662f\u72b6\u6001\u548c\u5b50\u95ee\u9898\u7684\u89e3\u4e4b\u95f4\u7684\u6620\u5c04\u3002

        \u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b

        \u5bf9\u4e8e\u72b6\u6001 \\([i, j]\\) \uff0c\u5b83\u53ea\u80fd\u4ece\u4e0a\u8fb9\u683c\u5b50 \\([i-1, j]\\) \u548c\u5de6\u8fb9\u683c\u5b50 \\([i, j-1]\\) \u8f6c\u79fb\u800c\u6765\u3002\u56e0\u6b64\u6700\u4f18\u5b50\u7ed3\u6784\u4e3a\uff1a\u5230\u8fbe \\([i, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\u7531 \\([i, j-1]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\u4e0e \\([i-1, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\u4e2d\u8f83\u5c0f\u7684\u90a3\u4e00\u4e2a\u51b3\u5b9a\u3002

        \u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u53ef\u63a8\u51fa\u56fe 14-12 \u6240\u793a\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff1a

        \\[ dp[i, j] = \\min(dp[i-1, j], dp[i, j-1]) + grid[i, j] \\]

        \u56fe 14-12 \u00a0 \u6700\u4f18\u5b50\u7ed3\u6784\u4e0e\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b

        Note

        \u6839\u636e\u5b9a\u4e49\u597d\u7684 \\(dp\\) \u8868\uff0c\u601d\u8003\u539f\u95ee\u9898\u548c\u5b50\u95ee\u9898\u7684\u5173\u7cfb\uff0c\u627e\u51fa\u901a\u8fc7\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u6765\u6784\u9020\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u7684\u65b9\u6cd5\uff0c\u5373\u6700\u4f18\u5b50\u7ed3\u6784\u3002

        \u4e00\u65e6\u6211\u4eec\u627e\u5230\u4e86\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u6784\u5efa\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u3002

        \u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f

        \u5728\u672c\u9898\u4e2d\uff0c\u5904\u5728\u9996\u884c\u7684\u72b6\u6001\u53ea\u80fd\u4ece\u5176\u5de6\u8fb9\u7684\u72b6\u6001\u5f97\u6765\uff0c\u5904\u5728\u9996\u5217\u7684\u72b6\u6001\u53ea\u80fd\u4ece\u5176\u4e0a\u8fb9\u7684\u72b6\u6001\u5f97\u6765\uff0c\u56e0\u6b64\u9996\u884c \\(i = 0\\) \u548c\u9996\u5217 \\(j = 0\\) \u662f\u8fb9\u754c\u6761\u4ef6\u3002

        \u5982\u56fe 14-13 \u6240\u793a\uff0c\u7531\u4e8e\u6bcf\u4e2a\u683c\u5b50\u662f\u7531\u5176\u5de6\u65b9\u683c\u5b50\u548c\u4e0a\u65b9\u683c\u5b50\u8f6c\u79fb\u800c\u6765\uff0c\u56e0\u6b64\u6211\u4eec\u4f7f\u7528\u5faa\u73af\u6765\u904d\u5386\u77e9\u9635\uff0c\u5916\u5faa\u73af\u904d\u5386\u5404\u884c\uff0c\u5185\u5faa\u73af\u904d\u5386\u5404\u5217\u3002

        \u56fe 14-13 \u00a0 \u8fb9\u754c\u6761\u4ef6\u4e0e\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f

        Note

        \u8fb9\u754c\u6761\u4ef6\u5728\u52a8\u6001\u89c4\u5212\u4e2d\u7528\u4e8e\u521d\u59cb\u5316 \\(dp\\) \u8868\uff0c\u5728\u641c\u7d22\u4e2d\u7528\u4e8e\u526a\u679d\u3002

        \u72b6\u6001\u8f6c\u79fb\u987a\u5e8f\u7684\u6838\u5fc3\u662f\u8981\u4fdd\u8bc1\u5728\u8ba1\u7b97\u5f53\u524d\u95ee\u9898\u7684\u89e3\u65f6\uff0c\u6240\u6709\u5b83\u4f9d\u8d56\u7684\u66f4\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\u90fd\u5df2\u7ecf\u88ab\u6b63\u786e\u5730\u8ba1\u7b97\u51fa\u6765\u3002

        \u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u6211\u4eec\u5df2\u7ecf\u53ef\u4ee5\u76f4\u63a5\u5199\u51fa\u52a8\u6001\u89c4\u5212\u4ee3\u7801\u3002\u7136\u800c\u5b50\u95ee\u9898\u5206\u89e3\u662f\u4e00\u79cd\u4ece\u9876\u81f3\u5e95\u7684\u601d\u60f3\uff0c\u56e0\u6b64\u6309\u7167\u201c\u66b4\u529b\u641c\u7d22 \\(\\rightarrow\\) \u8bb0\u5fc6\u5316\u641c\u7d22 \\(\\rightarrow\\) \u52a8\u6001\u89c4\u5212\u201d\u7684\u987a\u5e8f\u5b9e\u73b0\u66f4\u52a0\u7b26\u5408\u601d\u7ef4\u4e60\u60ef\u3002

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1","title":"1. \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u7d22","text":"

        \u4ece\u72b6\u6001 \\([i, j]\\) \u5f00\u59cb\u641c\u7d22\uff0c\u4e0d\u65ad\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u72b6\u6001 \\([i-1, j]\\) \u548c \\([i, j-1]\\) \uff0c\u9012\u5f52\u51fd\u6570\u5305\u62ec\u4ee5\u4e0b\u8981\u7d20\u3002

        • \u9012\u5f52\u53c2\u6570\uff1a\u72b6\u6001 \\([i, j]\\) \u3002
        • \u8fd4\u56de\u503c\uff1a\u4ece \\([0, 0]\\) \u5230 \\([i, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c \\(dp[i, j]\\) \u3002
        • \u7ec8\u6b62\u6761\u4ef6\uff1a\u5f53 \\(i = 0\\) \u4e14 \\(j = 0\\) \u65f6\uff0c\u8fd4\u56de\u4ee3\u4ef7 \\(grid[0, 0]\\) \u3002
        • \u526a\u679d\uff1a\u5f53 \\(i < 0\\) \u65f6\u6216 \\(j < 0\\) \u65f6\u7d22\u5f15\u8d8a\u754c\uff0c\u6b64\u65f6\u8fd4\u56de\u4ee3\u4ef7 \\(+\\infty\\) \uff0c\u4ee3\u8868\u4e0d\u53ef\u884c\u3002

        \u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
        def min_path_sum_dfs(grid: list[list[int]], i: int, j: int) -> int:\n    \"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22\"\"\"\n    # \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if i == 0 and j == 0:\n        return grid[0][0]\n    # \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if i < 0 or j < 0:\n        return inf\n    # \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    up = min_path_sum_dfs(grid, i - 1, j)\n    left = min_path_sum_dfs(grid, i, j - 1)\n    # \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    return min(left, up) + grid[i][j]\n
        min_path_sum.cpp
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(vector<vector<int>> &grid, int i, int j) {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return INT_MAX;\n    }\n    // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    int up = minPathSumDFS(grid, i - 1, j);\n    int left = minPathSumDFS(grid, i, j - 1);\n    // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    return min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\n}\n
        min_path_sum.java
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(int[][] grid, int i, int j) {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return Integer.MAX_VALUE;\n    }\n    // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    int up = minPathSumDFS(grid, i - 1, j);\n    int left = minPathSumDFS(grid, i, j - 1);\n    // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    return Math.min(left, up) + grid[i][j];\n}\n
        min_path_sum.cs
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint MinPathSumDFS(int[][] grid, int i, int j) {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return int.MaxValue;\n    }\n    // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    int up = MinPathSumDFS(grid, i - 1, j);\n    int left = MinPathSumDFS(grid, i, j - 1);\n    // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    return Math.Min(left, up) + grid[i][j];\n}\n
        min_path_sum.go
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc minPathSumDFS(grid [][]int, i, j int) int {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if i == 0 && j == 0 {\n        return grid[0][0]\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if i < 0 || j < 0 {\n        return math.MaxInt\n    }\n    // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    up := minPathSumDFS(grid, i-1, j)\n    left := minPathSumDFS(grid, i, j-1)\n    // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    return int(math.Min(float64(left), float64(up))) + grid[i][j]\n}\n
        min_path_sum.swift
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc minPathSumDFS(grid: [[Int]], i: Int, j: Int) -> Int {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if i == 0, j == 0 {\n        return grid[0][0]\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if i < 0 || j < 0 {\n        return .max\n    }\n    // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    let up = minPathSumDFS(grid: grid, i: i - 1, j: j)\n    let left = minPathSumDFS(grid: grid, i: i, j: j - 1)\n    // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    return min(left, up) + grid[i][j]\n}\n
        min_path_sum.js
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction minPathSumDFS(grid, i, j) {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i === 0 && j === 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return Infinity;\n    }\n    // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    const up = minPathSumDFS(grid, i - 1, j);\n    const left = minPathSumDFS(grid, i, j - 1);\n    // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    return Math.min(left, up) + grid[i][j];\n}\n
        min_path_sum.ts
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction minPathSumDFS(\n    grid: Array<Array<number>>,\n    i: number,\n    j: number\n): number {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i === 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return Infinity;\n    }\n    // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    const up = minPathSumDFS(grid, i - 1, j);\n    const left = minPathSumDFS(grid, i, j - 1);\n    // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    return Math.min(left, up) + grid[i][j];\n}\n
        min_path_sum.dart
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(List<List<int>> grid, int i, int j) {\n  // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n  if (i == 0 && j == 0) {\n    return grid[0][0];\n  }\n  // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n  if (i < 0 || j < 0) {\n    // \u5728 Dart \u4e2d\uff0cint \u7c7b\u578b\u662f\u56fa\u5b9a\u8303\u56f4\u7684\u6574\u6570\uff0c\u4e0d\u5b58\u5728\u8868\u793a\u201c\u65e0\u7a77\u5927\u201d\u7684\u503c\n    return BigInt.from(2).pow(31).toInt();\n  }\n  // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n  int up = minPathSumDFS(grid, i - 1, j);\n  int left = minPathSumDFS(grid, i, j - 1);\n  // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n  return min(left, up) + grid[i][j];\n}\n
        min_path_sum.rs
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfn min_path_sum_dfs(grid: &Vec<Vec<i32>>, i: i32, j: i32) -> i32 {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if i == 0 && j == 0 {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if i < 0 || j < 0 {\n        return i32::MAX;\n    }\n    // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    let up = min_path_sum_dfs(grid, i - 1, j);\n    let left = min_path_sum_dfs(grid, i, j - 1);\n    // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    std::cmp::min(left, up) + grid[i as usize][j as usize]\n}\n
        min_path_sum.c
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(int grid[MAX_SIZE][MAX_SIZE], int i, int j) {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return INT_MAX;\n    }\n    // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    int up = minPathSumDFS(grid, i - 1, j);\n    int left = minPathSumDFS(grid, i, j - 1);\n    // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    return myMin(left, up) != INT_MAX ? myMin(left, up) + grid[i][j] : INT_MAX;\n}\n
        min_path_sum.kt
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfun minPathSumDFS(grid: Array<IntArray>, i: Int, j: Int): Int {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i == 0 && j == 0) {\n        return grid[0][0]\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return Int.MAX_VALUE\n    }\n    // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    val up = minPathSumDFS(grid, i - 1, j)\n    val left = minPathSumDFS(grid, i, j - 1)\n    // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    return min(left, up) + grid[i][j]\n}\n
        min_path_sum.rb
        ### \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 ###\ndef min_path_sum_dfs(grid, i, j)\n  # \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n  return grid[i][j] if i == 0 && j == 0\n  # \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n  return Float::INFINITY if i < 0 || j < 0\n  # \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n  up = min_path_sum_dfs(grid, i - 1, j)\n  left = min_path_sum_dfs(grid, i, j - 1)\n  # \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n  [left, up].min + grid[i][j]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 14-14 \u7ed9\u51fa\u4e86\u4ee5 \\(dp[2, 1]\\) \u4e3a\u6839\u8282\u70b9\u7684\u9012\u5f52\u6811\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e9b\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u5176\u6570\u91cf\u4f1a\u968f\u7740\u7f51\u683c grid \u7684\u5c3a\u5bf8\u53d8\u5927\u800c\u6025\u5267\u589e\u591a\u3002

        \u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u9020\u6210\u91cd\u53e0\u5b50\u95ee\u9898\u7684\u539f\u56e0\u4e3a\uff1a\u5b58\u5728\u591a\u6761\u8def\u5f84\u53ef\u4ee5\u4ece\u5de6\u4e0a\u89d2\u5230\u8fbe\u67d0\u4e00\u5355\u5143\u683c\u3002

        \u56fe 14-14 \u00a0 \u66b4\u529b\u641c\u7d22\u9012\u5f52\u6811

        \u6bcf\u4e2a\u72b6\u6001\u90fd\u6709\u5411\u4e0b\u548c\u5411\u53f3\u4e24\u79cd\u9009\u62e9\uff0c\u4ece\u5de6\u4e0a\u89d2\u8d70\u5230\u53f3\u4e0b\u89d2\u603b\u5171\u9700\u8981 \\(m + n - 2\\) \u6b65\uff0c\u6240\u4ee5\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^{m + n})\\) \uff0c\u5176\u4e2d \\(n\\) \u548c \\(m\\) \u5206\u522b\u4e3a\u7f51\u683c\u7684\u884c\u6570\u548c\u5217\u6570\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u79cd\u8ba1\u7b97\u65b9\u5f0f\u672a\u8003\u8651\u4e34\u8fd1\u7f51\u683c\u8fb9\u754c\u7684\u60c5\u51b5\uff0c\u5f53\u5230\u8fbe\u7f51\u683c\u8fb9\u754c\u65f6\u53ea\u5269\u4e0b\u4e00\u79cd\u9009\u62e9\uff0c\u56e0\u6b64\u5b9e\u9645\u7684\u8def\u5f84\u6570\u91cf\u4f1a\u5c11\u4e00\u4e9b\u3002

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#2","title":"2. \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22","text":"

        \u6211\u4eec\u5f15\u5165\u4e00\u4e2a\u548c\u7f51\u683c grid \u76f8\u540c\u5c3a\u5bf8\u7684\u8bb0\u5fc6\u5217\u8868 mem \uff0c\u7528\u4e8e\u8bb0\u5f55\u5404\u4e2a\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5e76\u5c06\u91cd\u53e0\u5b50\u95ee\u9898\u8fdb\u884c\u526a\u679d\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
        def min_path_sum_dfs_mem(\n    grid: list[list[int]], mem: list[list[int]], i: int, j: int\n) -> int:\n    \"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n    # \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if i == 0 and j == 0:\n        return grid[0][0]\n    # \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if i < 0 or j < 0:\n        return inf\n    # \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if mem[i][j] != -1:\n        return mem[i][j]\n    # \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    up = min_path_sum_dfs_mem(grid, mem, i - 1, j)\n    left = min_path_sum_dfs_mem(grid, mem, i, j - 1)\n    # \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    mem[i][j] = min(left, up) + grid[i][j]\n    return mem[i][j]\n
        min_path_sum.cpp
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(vector<vector<int>> &grid, vector<vector<int>> &mem, int i, int j) {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return INT_MAX;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    int up = minPathSumDFSMem(grid, mem, i - 1, j);\n    int left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    mem[i][j] = min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\n    return mem[i][j];\n}\n
        min_path_sum.java
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return Integer.MAX_VALUE;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    int up = minPathSumDFSMem(grid, mem, i - 1, j);\n    int left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    mem[i][j] = Math.min(left, up) + grid[i][j];\n    return mem[i][j];\n}\n
        min_path_sum.cs
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint MinPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return int.MaxValue;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    int up = MinPathSumDFSMem(grid, mem, i - 1, j);\n    int left = MinPathSumDFSMem(grid, mem, i, j - 1);\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    mem[i][j] = Math.Min(left, up) + grid[i][j];\n    return mem[i][j];\n}\n
        min_path_sum.go
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc minPathSumDFSMem(grid, mem [][]int, i, j int) int {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if i == 0 && j == 0 {\n        return grid[0][0]\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if i < 0 || j < 0 {\n        return math.MaxInt\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if mem[i][j] != -1 {\n        return mem[i][j]\n    }\n    // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    up := minPathSumDFSMem(grid, mem, i-1, j)\n    left := minPathSumDFSMem(grid, mem, i, j-1)\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    mem[i][j] = int(math.Min(float64(left), float64(up))) + grid[i][j]\n    return mem[i][j]\n}\n
        min_path_sum.swift
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc minPathSumDFSMem(grid: [[Int]], mem: inout [[Int]], i: Int, j: Int) -> Int {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if i == 0, j == 0 {\n        return grid[0][0]\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if i < 0 || j < 0 {\n        return .max\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if mem[i][j] != -1 {\n        return mem[i][j]\n    }\n    // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    let up = minPathSumDFSMem(grid: grid, mem: &mem, i: i - 1, j: j)\n    let left = minPathSumDFSMem(grid: grid, mem: &mem, i: i, j: j - 1)\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    mem[i][j] = min(left, up) + grid[i][j]\n    return mem[i][j]\n}\n
        min_path_sum.js
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction minPathSumDFSMem(grid, mem, i, j) {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i === 0 && j === 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return Infinity;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][j] !== -1) {\n        return mem[i][j];\n    }\n    // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    const up = minPathSumDFSMem(grid, mem, i - 1, j);\n    const left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    mem[i][j] = Math.min(left, up) + grid[i][j];\n    return mem[i][j];\n}\n
        min_path_sum.ts
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction minPathSumDFSMem(\n    grid: Array<Array<number>>,\n    mem: Array<Array<number>>,\n    i: number,\n    j: number\n): number {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i === 0 && j === 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return Infinity;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    const up = minPathSumDFSMem(grid, mem, i - 1, j);\n    const left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    mem[i][j] = Math.min(left, up) + grid[i][j];\n    return mem[i][j];\n}\n
        min_path_sum.dart
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(List<List<int>> grid, List<List<int>> mem, int i, int j) {\n  // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n  if (i == 0 && j == 0) {\n    return grid[0][0];\n  }\n  // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n  if (i < 0 || j < 0) {\n    // \u5728 Dart \u4e2d\uff0cint \u7c7b\u578b\u662f\u56fa\u5b9a\u8303\u56f4\u7684\u6574\u6570\uff0c\u4e0d\u5b58\u5728\u8868\u793a\u201c\u65e0\u7a77\u5927\u201d\u7684\u503c\n    return BigInt.from(2).pow(31).toInt();\n  }\n  // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n  if (mem[i][j] != -1) {\n    return mem[i][j];\n  }\n  // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n  int up = minPathSumDFSMem(grid, mem, i - 1, j);\n  int left = minPathSumDFSMem(grid, mem, i, j - 1);\n  // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n  mem[i][j] = min(left, up) + grid[i][j];\n  return mem[i][j];\n}\n
        min_path_sum.rs
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn min_path_sum_dfs_mem(grid: &Vec<Vec<i32>>, mem: &mut Vec<Vec<i32>>, i: i32, j: i32) -> i32 {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if i == 0 && j == 0 {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if i < 0 || j < 0 {\n        return i32::MAX;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if mem[i as usize][j as usize] != -1 {\n        return mem[i as usize][j as usize];\n    }\n    // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    let up = min_path_sum_dfs_mem(grid, mem, i - 1, j);\n    let left = min_path_sum_dfs_mem(grid, mem, i, j - 1);\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    mem[i as usize][j as usize] = std::cmp::min(left, up) + grid[i as usize][j as usize];\n    mem[i as usize][j as usize]\n}\n
        min_path_sum.c
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(int grid[MAX_SIZE][MAX_SIZE], int mem[MAX_SIZE][MAX_SIZE], int i, int j) {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return INT_MAX;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    int up = minPathSumDFSMem(grid, mem, i - 1, j);\n    int left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    mem[i][j] = myMin(left, up) != INT_MAX ? myMin(left, up) + grid[i][j] : INT_MAX;\n    return mem[i][j];\n}\n
        min_path_sum.kt
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfun minPathSumDFSMem(\n    grid: Array<IntArray>,\n    mem: Array<IntArray>,\n    i: Int,\n    j: Int\n): Int {\n    // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n    if (i == 0 && j == 0) {\n        return grid[0][0]\n    }\n    // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n    if (i < 0 || j < 0) {\n        return Int.MAX_VALUE\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][j] != -1) {\n        return mem[i][j]\n    }\n    // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    val up = minPathSumDFSMem(grid, mem, i - 1, j)\n    val left = minPathSumDFSMem(grid, mem, i, j - 1)\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n    mem[i][j] = min(left, up) + grid[i][j]\n    return mem[i][j]\n}\n
        min_path_sum.rb
        ### \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 ###\ndef min_path_sum_dfs_mem(grid, mem, i, j)\n  # \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n  return grid[0][0] if i == 0 && j == 0\n  # \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n  return Float::INFINITY if i < 0 || j < 0\n  # \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n  return mem[i][j] if mem[i][j] != -1\n  # \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n  up = min_path_sum_dfs_mem(grid, mem, i - 1, j)\n  left = min_path_sum_dfs_mem(grid, mem, i, j - 1)\n  # \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n  mem[i][j] = [left, up].min + grid[i][j]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5982\u56fe 14-15 \u6240\u793a\uff0c\u5728\u5f15\u5165\u8bb0\u5fc6\u5316\u540e\uff0c\u6240\u6709\u5b50\u95ee\u9898\u7684\u89e3\u53ea\u9700\u8ba1\u7b97\u4e00\u6b21\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u72b6\u6001\u603b\u6570\uff0c\u5373\u7f51\u683c\u5c3a\u5bf8 \\(O(nm)\\) \u3002

        \u56fe 14-15 \u00a0 \u8bb0\u5fc6\u5316\u641c\u7d22\u9012\u5f52\u6811

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#3","title":"3. \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52a8\u6001\u89c4\u5212","text":"

        \u57fa\u4e8e\u8fed\u4ee3\u5b9e\u73b0\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
        def min_path_sum_dp(grid: list[list[int]]) -> int:\n    \"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n    n, m = len(grid), len(grid[0])\n    # \u521d\u59cb\u5316 dp \u8868\n    dp = [[0] * m for _ in range(n)]\n    dp[0][0] = grid[0][0]\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for j in range(1, m):\n        dp[0][j] = dp[0][j - 1] + grid[0][j]\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    for i in range(1, n):\n        dp[i][0] = dp[i - 1][0] + grid[i][0]\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i in range(1, n):\n        for j in range(1, m):\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n    return dp[n - 1][m - 1]\n
        min_path_sum.cpp
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(vector<vector<int>> &grid) {\n    int n = grid.size(), m = grid[0].size();\n    // \u521d\u59cb\u5316 dp \u8868\n    vector<vector<int>> dp(n, vector<int>(m));\n    dp[0][0] = grid[0][0];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (int j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    for (int i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
        min_path_sum.java
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(int[][] grid) {\n    int n = grid.length, m = grid[0].length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[][] dp = new int[n][m];\n    dp[0][0] = grid[0][0];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (int j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    for (int i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
        min_path_sum.cs
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint MinPathSumDP(int[][] grid) {\n    int n = grid.Length, m = grid[0].Length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[,] dp = new int[n, m];\n    dp[0, 0] = grid[0][0];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (int j = 1; j < m; j++) {\n        dp[0, j] = dp[0, j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    for (int i = 1; i < n; i++) {\n        dp[i, 0] = dp[i - 1, 0] + grid[i][0];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i, j] = Math.Min(dp[i, j - 1], dp[i - 1, j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1, m - 1];\n}\n
        min_path_sum.go
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDP(grid [][]int) int {\n    n, m := len(grid), len(grid[0])\n    // \u521d\u59cb\u5316 dp \u8868\n    dp := make([][]int, n)\n    for i := 0; i < n; i++ {\n        dp[i] = make([]int, m)\n    }\n    dp[0][0] = grid[0][0]\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for j := 1; j < m; j++ {\n        dp[0][j] = dp[0][j-1] + grid[0][j]\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    for i := 1; i < n; i++ {\n        dp[i][0] = dp[i-1][0] + grid[i][0]\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i := 1; i < n; i++ {\n        for j := 1; j < m; j++ {\n            dp[i][j] = int(math.Min(float64(dp[i][j-1]), float64(dp[i-1][j]))) + grid[i][j]\n        }\n    }\n    return dp[n-1][m-1]\n}\n
        min_path_sum.swift
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDP(grid: [[Int]]) -> Int {\n    let n = grid.count\n    let m = grid[0].count\n    // \u521d\u59cb\u5316 dp \u8868\n    var dp = Array(repeating: Array(repeating: 0, count: m), count: n)\n    dp[0][0] = grid[0][0]\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for j in 1 ..< m {\n        dp[0][j] = dp[0][j - 1] + grid[0][j]\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    for i in 1 ..< n {\n        dp[i][0] = dp[i - 1][0] + grid[i][0]\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i in 1 ..< n {\n        for j in 1 ..< m {\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n        }\n    }\n    return dp[n - 1][m - 1]\n}\n
        min_path_sum.js
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDP(grid) {\n    const n = grid.length,\n        m = grid[0].length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: n }, () =>\n        Array.from({ length: m }, () => 0)\n    );\n    dp[0][0] = grid[0][0];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (let j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    for (let i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (let i = 1; i < n; i++) {\n        for (let j = 1; j < m; j++) {\n            dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
        min_path_sum.ts
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDP(grid: Array<Array<number>>): number {\n    const n = grid.length,\n        m = grid[0].length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: n }, () =>\n        Array.from({ length: m }, () => 0)\n    );\n    dp[0][0] = grid[0][0];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (let j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    for (let i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (let i = 1; i < n; i++) {\n        for (let j: number = 1; j < m; j++) {\n            dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
        min_path_sum.dart
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(List<List<int>> grid) {\n  int n = grid.length, m = grid[0].length;\n  // \u521d\u59cb\u5316 dp \u8868\n  List<List<int>> dp = List.generate(n, (i) => List.filled(m, 0));\n  dp[0][0] = grid[0][0];\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n  for (int j = 1; j < m; j++) {\n    dp[0][j] = dp[0][j - 1] + grid[0][j];\n  }\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n  for (int i = 1; i < n; i++) {\n    dp[i][0] = dp[i - 1][0] + grid[i][0];\n  }\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n  for (int i = 1; i < n; i++) {\n    for (int j = 1; j < m; j++) {\n      dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n    }\n  }\n  return dp[n - 1][m - 1];\n}\n
        min_path_sum.rs
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfn min_path_sum_dp(grid: &Vec<Vec<i32>>) -> i32 {\n    let (n, m) = (grid.len(), grid[0].len());\n    // \u521d\u59cb\u5316 dp \u8868\n    let mut dp = vec![vec![0; m]; n];\n    dp[0][0] = grid[0][0];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for j in 1..m {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    for i in 1..n {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i in 1..n {\n        for j in 1..m {\n            dp[i][j] = std::cmp::min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    dp[n - 1][m - 1]\n}\n
        min_path_sum.c
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(int grid[MAX_SIZE][MAX_SIZE], int n, int m) {\n    // \u521d\u59cb\u5316 dp \u8868\n    int **dp = malloc(n * sizeof(int *));\n    for (int i = 0; i < n; i++) {\n        dp[i] = calloc(m, sizeof(int));\n    }\n    dp[0][0] = grid[0][0];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (int j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    for (int i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i][j] = myMin(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    int res = dp[n - 1][m - 1];\n    // \u91ca\u653e\u5185\u5b58\n    for (int i = 0; i < n; i++) {\n        free(dp[i]);\n    }\n    return res;\n}\n
        min_path_sum.kt
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfun minPathSumDP(grid: Array<IntArray>): Int {\n    val n = grid.size\n    val m = grid[0].size\n    // \u521d\u59cb\u5316 dp \u8868\n    val dp = Array(n) { IntArray(m) }\n    dp[0][0] = grid[0][0]\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (j in 1..<m) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j]\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    for (i in 1..<n) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0]\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (i in 1..<n) {\n        for (j in 1..<m) {\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n        }\n    }\n    return dp[n - 1][m - 1]\n}\n
        min_path_sum.rb
        ### \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 ###\ndef min_path_sum_dp(grid)\n  n, m = grid.length, grid.first.length\n  # \u521d\u59cb\u5316 dp \u8868\n  dp = Array.new(n) { Array.new(m, 0) }\n  dp[0][0] = grid[0][0]\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n  (1...m).each { |j| dp[0][j] = dp[0][j - 1] + grid[0][j] }\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n  (1...n).each { |i| dp[i][0] = dp[i - 1][0] + grid[i][0] }\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n  for i in 1...n\n    for j in 1...m\n      dp[i][j] = [dp[i][j - 1], dp[i - 1][j]].min + grid[i][j]\n    end\n  end\n  dp[n -1][m -1]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 14-16 \u5c55\u793a\u4e86\u6700\u5c0f\u8def\u5f84\u548c\u7684\u72b6\u6001\u8f6c\u79fb\u8fc7\u7a0b\uff0c\u5176\u904d\u5386\u4e86\u6574\u4e2a\u7f51\u683c\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nm)\\) \u3002

        \u6570\u7ec4 dp \u5927\u5c0f\u4e3a \\(n \\times m\\) \uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nm)\\) \u3002

        <1><2><3><4><5><6><7><8><9><10><11><12>

        \u56fe 14-16 \u00a0 \u6700\u5c0f\u8def\u5f84\u548c\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

        "},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#4","title":"4. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

        \u7531\u4e8e\u6bcf\u4e2a\u683c\u5b50\u53ea\u4e0e\u5176\u5de6\u8fb9\u548c\u4e0a\u8fb9\u7684\u683c\u5b50\u6709\u5173\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u53ea\u7528\u4e00\u4e2a\u5355\u884c\u6570\u7ec4\u6765\u5b9e\u73b0 \\(dp\\) \u8868\u3002

        \u8bf7\u6ce8\u610f\uff0c\u56e0\u4e3a\u6570\u7ec4 dp \u53ea\u80fd\u8868\u793a\u4e00\u884c\u7684\u72b6\u6001\uff0c\u6240\u4ee5\u6211\u4eec\u65e0\u6cd5\u63d0\u524d\u521d\u59cb\u5316\u9996\u5217\u72b6\u6001\uff0c\u800c\u662f\u5728\u904d\u5386\u6bcf\u884c\u65f6\u66f4\u65b0\u5b83\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
        def min_path_sum_dp_comp(grid: list[list[int]]) -> int:\n    \"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n    n, m = len(grid), len(grid[0])\n    # \u521d\u59cb\u5316 dp \u8868\n    dp = [0] * m\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    dp[0] = grid[0][0]\n    for j in range(1, m):\n        dp[j] = dp[j - 1] + grid[0][j]\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for i in range(1, n):\n        # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        dp[0] = dp[0] + grid[i][0]\n        # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for j in range(1, m):\n            dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n    return dp[m - 1]\n
        min_path_sum.cpp
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(vector<vector<int>> &grid) {\n    int n = grid.size(), m = grid[0].size();\n    // \u521d\u59cb\u5316 dp \u8868\n    vector<int> dp(m);\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    dp[0] = grid[0][0];\n    for (int j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (int i = 1; i < n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        dp[0] = dp[0] + grid[i][0];\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (int j = 1; j < m; j++) {\n            dp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
        min_path_sum.java
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(int[][] grid) {\n    int n = grid.length, m = grid[0].length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[] dp = new int[m];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    dp[0] = grid[0][0];\n    for (int j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (int i = 1; i < n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        dp[0] = dp[0] + grid[i][0];\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (int j = 1; j < m; j++) {\n            dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
        min_path_sum.cs
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint MinPathSumDPComp(int[][] grid) {\n    int n = grid.Length, m = grid[0].Length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[] dp = new int[m];\n    dp[0] = grid[0][0];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (int j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (int i = 1; i < n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        dp[0] = dp[0] + grid[i][0];\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (int j = 1; j < m; j++) {\n            dp[j] = Math.Min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
        min_path_sum.go
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDPComp(grid [][]int) int {\n    n, m := len(grid), len(grid[0])\n    // \u521d\u59cb\u5316 dp \u8868\n    dp := make([]int, m)\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    dp[0] = grid[0][0]\n    for j := 1; j < m; j++ {\n        dp[j] = dp[j-1] + grid[0][j]\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i := 1; i < n; i++ {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        dp[0] = dp[0] + grid[i][0]\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for j := 1; j < m; j++ {\n            dp[j] = int(math.Min(float64(dp[j-1]), float64(dp[j]))) + grid[i][j]\n        }\n    }\n    return dp[m-1]\n}\n
        min_path_sum.swift
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDPComp(grid: [[Int]]) -> Int {\n    let n = grid.count\n    let m = grid[0].count\n    // \u521d\u59cb\u5316 dp \u8868\n    var dp = Array(repeating: 0, count: m)\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    dp[0] = grid[0][0]\n    for j in 1 ..< m {\n        dp[j] = dp[j - 1] + grid[0][j]\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for i in 1 ..< n {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        dp[0] = dp[0] + grid[i][0]\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for j in 1 ..< m {\n            dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n        }\n    }\n    return dp[m - 1]\n}\n
        min_path_sum.js
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDPComp(grid) {\n    const n = grid.length,\n        m = grid[0].length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = new Array(m);\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    dp[0] = grid[0][0];\n    for (let j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (let i = 1; i < n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        dp[0] = dp[0] + grid[i][0];\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (let j = 1; j < m; j++) {\n            dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
        min_path_sum.ts
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDPComp(grid: Array<Array<number>>): number {\n    const n = grid.length,\n        m = grid[0].length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = new Array(m);\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    dp[0] = grid[0][0];\n    for (let j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (let i = 1; i < n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        dp[0] = dp[0] + grid[i][0];\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (let j = 1; j < m; j++) {\n            dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
        min_path_sum.dart
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(List<List<int>> grid) {\n  int n = grid.length, m = grid[0].length;\n  // \u521d\u59cb\u5316 dp \u8868\n  List<int> dp = List.filled(m, 0);\n  dp[0] = grid[0][0];\n  for (int j = 1; j < m; j++) {\n    dp[j] = dp[j - 1] + grid[0][j];\n  }\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n  for (int i = 1; i < n; i++) {\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    dp[0] = dp[0] + grid[i][0];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n    for (int j = 1; j < m; j++) {\n      dp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n    }\n  }\n  return dp[m - 1];\n}\n
        min_path_sum.rs
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn min_path_sum_dp_comp(grid: &Vec<Vec<i32>>) -> i32 {\n    let (n, m) = (grid.len(), grid[0].len());\n    // \u521d\u59cb\u5316 dp \u8868\n    let mut dp = vec![0; m];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    dp[0] = grid[0][0];\n    for j in 1..m {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for i in 1..n {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        dp[0] = dp[0] + grid[i][0];\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for j in 1..m {\n            dp[j] = std::cmp::min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    dp[m - 1]\n}\n
        min_path_sum.c
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(int grid[MAX_SIZE][MAX_SIZE], int n, int m) {\n    // \u521d\u59cb\u5316 dp \u8868\n    int *dp = calloc(m, sizeof(int));\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    dp[0] = grid[0][0];\n    for (int j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (int i = 1; i < n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        dp[0] = dp[0] + grid[i][0];\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (int j = 1; j < m; j++) {\n            dp[j] = myMin(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    int res = dp[m - 1];\n    // \u91ca\u653e\u5185\u5b58\n    free(dp);\n    return res;\n}\n
        min_path_sum.kt
        /* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfun minPathSumDPComp(grid: Array<IntArray>): Int {\n    val n = grid.size\n    val m = grid[0].size\n    // \u521d\u59cb\u5316 dp \u8868\n    val dp = IntArray(m)\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    dp[0] = grid[0][0]\n    for (j in 1..<m) {\n        dp[j] = dp[j - 1] + grid[0][j]\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (i in 1..<n) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        dp[0] = dp[0] + grid[i][0]\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (j in 1..<m) {\n            dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n        }\n    }\n    return dp[m - 1]\n}\n
        min_path_sum.rb
        ### \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 ###\ndef min_path_sum_dp_comp(grid)\n  n, m = grid.length, grid.first.length\n  # \u521d\u59cb\u5316 dp \u8868\n  dp = Array.new(m, 0)\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n  dp[0] = grid[0][0]\n  (1...m).each { |j| dp[j] = dp[j - 1] + grid[0][j] }\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n  for i in 1...n\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    dp[0] = dp[0] + grid[i][0]\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n    (1...m).each { |j| dp[j] = [dp[j - 1], dp[j]].min + grid[i][j] }\n  end\n  dp[m - 1]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_dynamic_programming/edit_distance_problem/","title":"14.6 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898","text":"

        \u7f16\u8f91\u8ddd\u79bb\uff0c\u4e5f\u79f0 Levenshtein \u8ddd\u79bb\uff0c\u6307\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e4b\u95f4\u4e92\u76f8\u8f6c\u6362\u7684\u6700\u5c11\u4fee\u6539\u6b21\u6570\uff0c\u901a\u5e38\u7528\u4e8e\u5728\u4fe1\u606f\u68c0\u7d22\u548c\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4e2d\u5ea6\u91cf\u4e24\u4e2a\u5e8f\u5217\u7684\u76f8\u4f3c\u5ea6\u3002

        Question

        \u8f93\u5165\u4e24\u4e2a\u5b57\u7b26\u4e32 \\(s\\) \u548c \\(t\\) \uff0c\u8fd4\u56de\u5c06 \\(s\\) \u8f6c\u6362\u4e3a \\(t\\) \u6240\u9700\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u3002

        \u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u8fdb\u884c\u4e09\u79cd\u7f16\u8f91\u64cd\u4f5c\uff1a\u63d2\u5165\u4e00\u4e2a\u5b57\u7b26\u3001\u5220\u9664\u4e00\u4e2a\u5b57\u7b26\u3001\u5c06\u5b57\u7b26\u66ff\u6362\u4e3a\u4efb\u610f\u4e00\u4e2a\u5b57\u7b26\u3002

        \u5982\u56fe 14-27 \u6240\u793a\uff0c\u5c06 kitten \u8f6c\u6362\u4e3a sitting \u9700\u8981\u7f16\u8f91 3 \u6b65\uff0c\u5305\u62ec 2 \u6b21\u66ff\u6362\u64cd\u4f5c\u4e0e 1 \u6b21\u6dfb\u52a0\u64cd\u4f5c\uff1b\u5c06 hello \u8f6c\u6362\u4e3a algo \u9700\u8981 3 \u6b65\uff0c\u5305\u62ec 2 \u6b21\u66ff\u6362\u64cd\u4f5c\u548c 1 \u6b21\u5220\u9664\u64cd\u4f5c\u3002

        \u56fe 14-27 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u7684\u793a\u4f8b\u6570\u636e

        \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898\u53ef\u4ee5\u5f88\u81ea\u7136\u5730\u7528\u51b3\u7b56\u6811\u6a21\u578b\u6765\u89e3\u91ca\u3002\u5b57\u7b26\u4e32\u5bf9\u5e94\u6811\u8282\u70b9\uff0c\u4e00\u8f6e\u51b3\u7b56\uff08\u4e00\u6b21\u7f16\u8f91\u64cd\u4f5c\uff09\u5bf9\u5e94\u6811\u7684\u4e00\u6761\u8fb9\u3002

        \u5982\u56fe 14-28 \u6240\u793a\uff0c\u5728\u4e0d\u9650\u5236\u64cd\u4f5c\u7684\u60c5\u51b5\u4e0b\uff0c\u6bcf\u4e2a\u8282\u70b9\u90fd\u53ef\u4ee5\u6d3e\u751f\u51fa\u8bb8\u591a\u6761\u8fb9\uff0c\u6bcf\u6761\u8fb9\u5bf9\u5e94\u4e00\u79cd\u64cd\u4f5c\uff0c\u8fd9\u610f\u5473\u7740\u4ece hello \u8f6c\u6362\u5230 algo \u6709\u8bb8\u591a\u79cd\u53ef\u80fd\u7684\u8def\u5f84\u3002

        \u4ece\u51b3\u7b56\u6811\u7684\u89d2\u5ea6\u770b\uff0c\u672c\u9898\u7684\u76ee\u6807\u662f\u6c42\u89e3\u8282\u70b9 hello \u548c\u8282\u70b9 algo \u4e4b\u95f4\u7684\u6700\u77ed\u8def\u5f84\u3002

        \u56fe 14-28 \u00a0 \u57fa\u4e8e\u51b3\u7b56\u6811\u6a21\u578b\u8868\u793a\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898

        "},{"location":"chapter_dynamic_programming/edit_distance_problem/#1","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"

        \u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868

        \u6bcf\u4e00\u8f6e\u7684\u51b3\u7b56\u662f\u5bf9\u5b57\u7b26\u4e32 \\(s\\) \u8fdb\u884c\u4e00\u6b21\u7f16\u8f91\u64cd\u4f5c\u3002

        \u6211\u4eec\u5e0c\u671b\u5728\u7f16\u8f91\u64cd\u4f5c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u95ee\u9898\u7684\u89c4\u6a21\u9010\u6e10\u7f29\u5c0f\uff0c\u8fd9\u6837\u624d\u80fd\u6784\u5efa\u5b50\u95ee\u9898\u3002\u8bbe\u5b57\u7b26\u4e32 \\(s\\) \u548c \\(t\\) \u7684\u957f\u5ea6\u5206\u522b\u4e3a \\(n\\) \u548c \\(m\\) \uff0c\u6211\u4eec\u5148\u8003\u8651\u4e24\u5b57\u7b26\u4e32\u5c3e\u90e8\u7684\u5b57\u7b26 \\(s[n-1]\\) \u548c \\(t[m-1]\\) \u3002

        • \u82e5 \\(s[n-1]\\) \u548c \\(t[m-1]\\) \u76f8\u540c\uff0c\u6211\u4eec\u53ef\u4ee5\u8df3\u8fc7\u5b83\u4eec\uff0c\u76f4\u63a5\u8003\u8651 \\(s[n-2]\\) \u548c \\(t[m-2]\\) \u3002
        • \u82e5 \\(s[n-1]\\) \u548c \\(t[m-1]\\) \u4e0d\u540c\uff0c\u6211\u4eec\u9700\u8981\u5bf9 \\(s\\) \u8fdb\u884c\u4e00\u6b21\u7f16\u8f91\uff08\u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\uff09\uff0c\u4f7f\u5f97\u4e24\u5b57\u7b26\u4e32\u5c3e\u90e8\u7684\u5b57\u7b26\u76f8\u540c\uff0c\u4ece\u800c\u53ef\u4ee5\u8df3\u8fc7\u5b83\u4eec\uff0c\u8003\u8651\u89c4\u6a21\u66f4\u5c0f\u7684\u95ee\u9898\u3002

        \u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u5728\u5b57\u7b26\u4e32 \\(s\\) \u4e2d\u8fdb\u884c\u7684\u6bcf\u4e00\u8f6e\u51b3\u7b56\uff08\u7f16\u8f91\u64cd\u4f5c\uff09\uff0c\u90fd\u4f1a\u4f7f\u5f97 \\(s\\) \u548c \\(t\\) \u4e2d\u5269\u4f59\u7684\u5f85\u5339\u914d\u5b57\u7b26\u53d1\u751f\u53d8\u5316\u3002\u56e0\u6b64\uff0c\u72b6\u6001\u4e3a\u5f53\u524d\u5728 \\(s\\) \u548c \\(t\\) \u4e2d\u8003\u8651\u7684\u7b2c \\(i\\) \u548c\u7b2c \\(j\\) \u4e2a\u5b57\u7b26\uff0c\u8bb0\u4e3a \\([i, j]\\) \u3002

        \u72b6\u6001 \\([i, j]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\uff1a\u5c06 \\(s\\) \u7684\u524d \\(i\\) \u4e2a\u5b57\u7b26\u66f4\u6539\u4e3a \\(t\\) \u7684\u524d \\(j\\) \u4e2a\u5b57\u7b26\u6240\u9700\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u3002

        \u81f3\u6b64\uff0c\u5f97\u5230\u4e00\u4e2a\u5c3a\u5bf8\u4e3a \\((i+1) \\times (j+1)\\) \u7684\u4e8c\u7ef4 \\(dp\\) \u8868\u3002

        \u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b

        \u8003\u8651\u5b50\u95ee\u9898 \\(dp[i, j]\\) \uff0c\u5176\u5bf9\u5e94\u7684\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5c3e\u90e8\u5b57\u7b26\u4e3a \\(s[i-1]\\) \u548c \\(t[j-1]\\) \uff0c\u53ef\u6839\u636e\u4e0d\u540c\u7f16\u8f91\u64cd\u4f5c\u5206\u4e3a\u56fe 14-29 \u6240\u793a\u7684\u4e09\u79cd\u60c5\u51b5\u3002

        1. \u5728 \\(s[i-1]\\) \u4e4b\u540e\u6dfb\u52a0 \\(t[j-1]\\) \uff0c\u5219\u5269\u4f59\u5b50\u95ee\u9898 \\(dp[i, j-1]\\) \u3002
        2. \u5220\u9664 \\(s[i-1]\\) \uff0c\u5219\u5269\u4f59\u5b50\u95ee\u9898 \\(dp[i-1, j]\\) \u3002
        3. \u5c06 \\(s[i-1]\\) \u66ff\u6362\u4e3a \\(t[j-1]\\) \uff0c\u5219\u5269\u4f59\u5b50\u95ee\u9898 \\(dp[i-1, j-1]\\) \u3002

        \u56fe 14-29 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u7684\u72b6\u6001\u8f6c\u79fb

        \u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u53ef\u5f97\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\\(dp[i, j]\\) \u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u7b49\u4e8e \\(dp[i, j-1]\\)\u3001\\(dp[i-1, j]\\)\u3001\\(dp[i-1, j-1]\\) \u4e09\u8005\u4e2d\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\uff0c\u518d\u52a0\u4e0a\u672c\u6b21\u7684\u7f16\u8f91\u6b65\u6570 \\(1\\) \u3002\u5bf9\u5e94\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a

        \\[ dp[i, j] = \\min(dp[i, j-1], dp[i-1, j], dp[i-1, j-1]) + 1 \\]

        \u8bf7\u6ce8\u610f\uff0c\u5f53 \\(s[i-1]\\) \u548c \\(t[j-1]\\) \u76f8\u540c\u65f6\uff0c\u65e0\u987b\u7f16\u8f91\u5f53\u524d\u5b57\u7b26\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a

        \\[ dp[i, j] = dp[i-1, j-1] \\]

        \u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f

        \u5f53\u4e24\u5b57\u7b26\u4e32\u90fd\u4e3a\u7a7a\u65f6\uff0c\u7f16\u8f91\u6b65\u6570\u4e3a \\(0\\) \uff0c\u5373 \\(dp[0, 0] = 0\\) \u3002\u5f53 \\(s\\) \u4e3a\u7a7a\u4f46 \\(t\\) \u4e0d\u4e3a\u7a7a\u65f6\uff0c\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u7b49\u4e8e \\(t\\) \u7684\u957f\u5ea6\uff0c\u5373\u9996\u884c \\(dp[0, j] = j\\) \u3002\u5f53 \\(s\\) \u4e0d\u4e3a\u7a7a\u4f46 \\(t\\) \u4e3a\u7a7a\u65f6\uff0c\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u7b49\u4e8e \\(s\\) \u7684\u957f\u5ea6\uff0c\u5373\u9996\u5217 \\(dp[i, 0] = i\\) \u3002

        \u89c2\u5bdf\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff0c\u89e3 \\(dp[i, j]\\) \u4f9d\u8d56\u5de6\u65b9\u3001\u4e0a\u65b9\u3001\u5de6\u4e0a\u65b9\u7684\u89e3\uff0c\u56e0\u6b64\u901a\u8fc7\u4e24\u5c42\u5faa\u73af\u6b63\u5e8f\u904d\u5386\u6574\u4e2a \\(dp\\) \u8868\u5373\u53ef\u3002

        "},{"location":"chapter_dynamic_programming/edit_distance_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby edit_distance.py
        def edit_distance_dp(s: str, t: str) -> int:\n    \"\"\"\u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n    n, m = len(s), len(t)\n    dp = [[0] * (m + 1) for _ in range(n + 1)]\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for i in range(1, n + 1):\n        dp[i][0] = i\n    for j in range(1, m + 1):\n        dp[0][j] = j\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i in range(1, n + 1):\n        for j in range(1, m + 1):\n            if s[i - 1] == t[j - 1]:\n                # \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[i][j] = dp[i - 1][j - 1]\n            else:\n                # \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1\n    return dp[n][m]\n
        edit_distance.cpp
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(string s, string t) {\n    int n = s.length(), m = t.length();\n    vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (int i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s[i - 1] == t[j - 1]) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
        edit_distance.java
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(String s, String t) {\n    int n = s.length(), m = t.length();\n    int[][] dp = new int[n + 1][m + 1];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (int i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s.charAt(i - 1) == t.charAt(j - 1)) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
        edit_distance.cs
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint EditDistanceDP(string s, string t) {\n    int n = s.Length, m = t.Length;\n    int[,] dp = new int[n + 1, m + 1];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (int i = 1; i <= n; i++) {\n        dp[i, 0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0, j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s[i - 1] == t[j - 1]) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[i, j] = dp[i - 1, j - 1];\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[i, j] = Math.Min(Math.Min(dp[i, j - 1], dp[i - 1, j]), dp[i - 1, j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n, m];\n}\n
        edit_distance.go
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDP(s string, t string) int {\n    n := len(s)\n    m := len(t)\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, m+1)\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for i := 1; i <= n; i++ {\n        dp[i][0] = i\n    }\n    for j := 1; j <= m; j++ {\n        dp[0][j] = j\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i := 1; i <= n; i++ {\n        for j := 1; j <= m; j++ {\n            if s[i-1] == t[j-1] {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[i][j] = dp[i-1][j-1]\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[i][j] = MinInt(MinInt(dp[i][j-1], dp[i-1][j]), dp[i-1][j-1]) + 1\n            }\n        }\n    }\n    return dp[n][m]\n}\n
        edit_distance.swift
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDP(s: String, t: String) -> Int {\n    let n = s.utf8CString.count\n    let m = t.utf8CString.count\n    var dp = Array(repeating: Array(repeating: 0, count: m + 1), count: n + 1)\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for i in 1 ... n {\n        dp[i][0] = i\n    }\n    for j in 1 ... m {\n        dp[0][j] = j\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i in 1 ... n {\n        for j in 1 ... m {\n            if s.utf8CString[i - 1] == t.utf8CString[j - 1] {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[i][j] = dp[i - 1][j - 1]\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1\n            }\n        }\n    }\n    return dp[n][m]\n}\n
        edit_distance.js
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDP(s, t) {\n    const n = s.length,\n        m = t.length;\n    const dp = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (let i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (let j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (let i = 1; i <= n; i++) {\n        for (let j = 1; j <= m; j++) {\n            if (s.charAt(i - 1) === t.charAt(j - 1)) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[i][j] =\n                    Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
        edit_distance.ts
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDP(s: string, t: string): number {\n    const n = s.length,\n        m = t.length;\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: m + 1 }, () => 0)\n    );\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (let i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (let j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (let i = 1; i <= n; i++) {\n        for (let j = 1; j <= m; j++) {\n            if (s.charAt(i - 1) === t.charAt(j - 1)) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[i][j] =\n                    Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
        edit_distance.dart
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(String s, String t) {\n  int n = s.length, m = t.length;\n  List<List<int>> dp = List.generate(n + 1, (_) => List.filled(m + 1, 0));\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n  for (int i = 1; i <= n; i++) {\n    dp[i][0] = i;\n  }\n  for (int j = 1; j <= m; j++) {\n    dp[0][j] = j;\n  }\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n  for (int i = 1; i <= n; i++) {\n    for (int j = 1; j <= m; j++) {\n      if (s[i - 1] == t[j - 1]) {\n        // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n        dp[i][j] = dp[i - 1][j - 1];\n      } else {\n        // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n        dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n      }\n    }\n  }\n  return dp[n][m];\n}\n
        edit_distance.rs
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfn edit_distance_dp(s: &str, t: &str) -> i32 {\n    let (n, m) = (s.len(), t.len());\n    let mut dp = vec![vec![0; m + 1]; n + 1];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for i in 1..=n {\n        dp[i][0] = i as i32;\n    }\n    for j in 1..m {\n        dp[0][j] = j as i32;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i in 1..=n {\n        for j in 1..=m {\n            if s.chars().nth(i - 1) == t.chars().nth(j - 1) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[i][j] =\n                    std::cmp::min(std::cmp::min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    dp[n][m]\n}\n
        edit_distance.c
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(char *s, char *t, int n, int m) {\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(m + 1, sizeof(int));\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (int i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s[i - 1] == t[j - 1]) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[i][j] = myMin(myMin(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    int res = dp[n][m];\n    // \u91ca\u653e\u5185\u5b58\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    return res;\n}\n
        edit_distance.kt
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfun editDistanceDP(s: String, t: String): Int {\n    val n = s.length\n    val m = t.length\n    val dp = Array(n + 1) { IntArray(m + 1) }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (i in 1..n) {\n        dp[i][0] = i\n    }\n    for (j in 1..m) {\n        dp[0][j] = j\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (i in 1..n) {\n        for (j in 1..m) {\n            if (s[i - 1] == t[j - 1]) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[i][j] = dp[i - 1][j - 1]\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1\n            }\n        }\n    }\n    return dp[n][m]\n}\n
        edit_distance.rb
        ### \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 ###\ndef edit_distance_dp(s, t)\n  n, m = s.length, t.length\n  dp = Array.new(n + 1) { Array.new(m + 1, 0) }\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n  (1...(n + 1)).each { |i| dp[i][0] = i }\n  (1...(m + 1)).each { |j| dp[0][j] = j }\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n  for i in 1...(n + 1)\n    for j in 1...(m +1)\n      if s[i - 1] == t[j - 1]\n        # \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n        dp[i][j] = dp[i - 1][j - 1]\n      else\n        # \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n        dp[i][j] = [dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]].min + 1\n      end\n    end\n  end\n  dp[n][m]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5982\u56fe 14-30 \u6240\u793a\uff0c\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898\u7684\u72b6\u6001\u8f6c\u79fb\u8fc7\u7a0b\u4e0e\u80cc\u5305\u95ee\u9898\u975e\u5e38\u7c7b\u4f3c\uff0c\u90fd\u53ef\u4ee5\u770b\u4f5c\u586b\u5199\u4e00\u4e2a\u4e8c\u7ef4\u7f51\u683c\u7684\u8fc7\u7a0b\u3002

        <1><2><3><4><5><6><7><8><9><10><11><12><13><14><15>

        \u56fe 14-30 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

        "},{"location":"chapter_dynamic_programming/edit_distance_problem/#3","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

        \u7531\u4e8e \\(dp[i,j]\\) \u662f\u7531\u4e0a\u65b9 \\(dp[i-1, j]\\)\u3001\u5de6\u65b9 \\(dp[i, j-1]\\)\u3001\u5de6\u4e0a\u65b9 \\(dp[i-1, j-1]\\) \u8f6c\u79fb\u800c\u6765\u7684\uff0c\u800c\u6b63\u5e8f\u904d\u5386\u4f1a\u4e22\u5931\u5de6\u4e0a\u65b9 \\(dp[i-1, j-1]\\) \uff0c\u5012\u5e8f\u904d\u5386\u65e0\u6cd5\u63d0\u524d\u6784\u5efa \\(dp[i, j-1]\\) \uff0c\u56e0\u6b64\u4e24\u79cd\u904d\u5386\u987a\u5e8f\u90fd\u4e0d\u53ef\u53d6\u3002

        \u4e3a\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u53d8\u91cf leftup \u6765\u6682\u5b58\u5de6\u4e0a\u65b9\u7684\u89e3 \\(dp[i-1, j-1]\\) \uff0c\u4ece\u800c\u53ea\u9700\u8003\u8651\u5de6\u65b9\u548c\u4e0a\u65b9\u7684\u89e3\u3002\u6b64\u65f6\u7684\u60c5\u51b5\u4e0e\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u76f8\u540c\uff0c\u53ef\u4f7f\u7528\u6b63\u5e8f\u904d\u5386\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby edit_distance.py
        def edit_distance_dp_comp(s: str, t: str) -> int:\n    \"\"\"\u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n    n, m = len(s), len(t)\n    dp = [0] * (m + 1)\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for j in range(1, m + 1):\n        dp[j] = j\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for i in range(1, n + 1):\n        # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        leftup = dp[0]  # \u6682\u5b58 dp[i-1, j-1]\n        dp[0] += 1\n        # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for j in range(1, m + 1):\n            temp = dp[j]\n            if s[i - 1] == t[j - 1]:\n                # \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[j] = leftup\n            else:\n                # \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[j] = min(dp[j - 1], dp[j], leftup) + 1\n            leftup = temp  # \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n    return dp[m]\n
        edit_distance.cpp
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(string s, string t) {\n    int n = s.length(), m = t.length();\n    vector<int> dp(m + 1, 0);\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (int j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (int i = 1; i <= n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        int leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n        dp[0] = i;\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (int j = 1; j <= m; j++) {\n            int temp = dp[j];\n            if (s[i - 1] == t[j - 1]) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[j] = leftup;\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n        }\n    }\n    return dp[m];\n}\n
        edit_distance.java
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(String s, String t) {\n    int n = s.length(), m = t.length();\n    int[] dp = new int[m + 1];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (int j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (int i = 1; i <= n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        int leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n        dp[0] = i;\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (int j = 1; j <= m; j++) {\n            int temp = dp[j];\n            if (s.charAt(i - 1) == t.charAt(j - 1)) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[j] = leftup;\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n        }\n    }\n    return dp[m];\n}\n
        edit_distance.cs
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint EditDistanceDPComp(string s, string t) {\n    int n = s.Length, m = t.Length;\n    int[] dp = new int[m + 1];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (int j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (int i = 1; i <= n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        int leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n        dp[0] = i;\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (int j = 1; j <= m; j++) {\n            int temp = dp[j];\n            if (s[i - 1] == t[j - 1]) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[j] = leftup;\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[j] = Math.Min(Math.Min(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n        }\n    }\n    return dp[m];\n}\n
        edit_distance.go
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDPComp(s string, t string) int {\n    n := len(s)\n    m := len(t)\n    dp := make([]int, m+1)\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for j := 1; j <= m; j++ {\n        dp[j] = j\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for i := 1; i <= n; i++ {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        leftUp := dp[0] // \u6682\u5b58 dp[i-1, j-1]\n        dp[0] = i\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for j := 1; j <= m; j++ {\n            temp := dp[j]\n            if s[i-1] == t[j-1] {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[j] = leftUp\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[j] = MinInt(MinInt(dp[j-1], dp[j]), leftUp) + 1\n            }\n            leftUp = temp // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n        }\n    }\n    return dp[m]\n}\n
        edit_distance.swift
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDPComp(s: String, t: String) -> Int {\n    let n = s.utf8CString.count\n    let m = t.utf8CString.count\n    var dp = Array(repeating: 0, count: m + 1)\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for j in 1 ... m {\n        dp[j] = j\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for i in 1 ... n {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        var leftup = dp[0] // \u6682\u5b58 dp[i-1, j-1]\n        dp[0] = i\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for j in 1 ... m {\n            let temp = dp[j]\n            if s.utf8CString[i - 1] == t.utf8CString[j - 1] {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[j] = leftup\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1\n            }\n            leftup = temp // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n        }\n    }\n    return dp[m]\n}\n
        edit_distance.js
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDPComp(s, t) {\n    const n = s.length,\n        m = t.length;\n    const dp = new Array(m + 1).fill(0);\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (let j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (let i = 1; i <= n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        let leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n        dp[0] = i;\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (let j = 1; j <= m; j++) {\n            const temp = dp[j];\n            if (s.charAt(i - 1) === t.charAt(j - 1)) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[j] = leftup;\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[j] = Math.min(dp[j - 1], dp[j], leftup) + 1;\n            }\n            leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n        }\n    }\n    return dp[m];\n}\n
        edit_distance.ts
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDPComp(s: string, t: string): number {\n    const n = s.length,\n        m = t.length;\n    const dp = new Array(m + 1).fill(0);\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (let j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (let i = 1; i <= n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        let leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n        dp[0] = i;\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (let j = 1; j <= m; j++) {\n            const temp = dp[j];\n            if (s.charAt(i - 1) === t.charAt(j - 1)) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[j] = leftup;\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[j] = Math.min(dp[j - 1], dp[j], leftup) + 1;\n            }\n            leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n        }\n    }\n    return dp[m];\n}\n
        edit_distance.dart
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(String s, String t) {\n  int n = s.length, m = t.length;\n  List<int> dp = List.filled(m + 1, 0);\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n  for (int j = 1; j <= m; j++) {\n    dp[j] = j;\n  }\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n  for (int i = 1; i <= n; i++) {\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    int leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n    dp[0] = i;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n    for (int j = 1; j <= m; j++) {\n      int temp = dp[j];\n      if (s[i - 1] == t[j - 1]) {\n        // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n        dp[j] = leftup;\n      } else {\n        // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n        dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n      }\n      leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n    }\n  }\n  return dp[m];\n}\n
        edit_distance.rs
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn edit_distance_dp_comp(s: &str, t: &str) -> i32 {\n    let (n, m) = (s.len(), t.len());\n    let mut dp = vec![0; m + 1];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for j in 1..m {\n        dp[j] = j as i32;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for i in 1..=n {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        let mut leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n        dp[0] = i as i32;\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for j in 1..=m {\n            let temp = dp[j];\n            if s.chars().nth(i - 1) == t.chars().nth(j - 1) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[j] = leftup;\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[j] = std::cmp::min(std::cmp::min(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n        }\n    }\n    dp[m]\n}\n
        edit_distance.c
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(char *s, char *t, int n, int m) {\n    int *dp = calloc(m + 1, sizeof(int));\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (int j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (int i = 1; i <= n; i++) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        int leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n        dp[0] = i;\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (int j = 1; j <= m; j++) {\n            int temp = dp[j];\n            if (s[i - 1] == t[j - 1]) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[j] = leftup;\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[j] = myMin(myMin(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n        }\n    }\n    int res = dp[m];\n    // \u91ca\u653e\u5185\u5b58\n    free(dp);\n    return res;\n}\n
        edit_distance.kt
        /* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfun editDistanceDPComp(s: String, t: String): Int {\n    val n = s.length\n    val m = t.length\n    val dp = IntArray(m + 1)\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n    for (j in 1..m) {\n        dp[j] = j\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n    for (i in 1..n) {\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n        var leftup = dp[0] // \u6682\u5b58 dp[i-1, j-1]\n        dp[0] = i\n        // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n        for (j in 1..m) {\n            val temp = dp[j]\n            if (s[i - 1] == t[j - 1]) {\n                // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n                dp[j] = leftup\n            } else {\n                // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n                dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1\n            }\n            leftup = temp // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n        }\n    }\n    return dp[m]\n}\n
        edit_distance.rb
        ### \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 ###\ndef edit_distance_dp_comp(s, t)\n  n, m = s.length, t.length\n  dp = Array.new(m + 1, 0)\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n  (1...(m + 1)).each { |j| dp[j] = j }\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n  for i in 1...(n + 1)\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n    leftup = dp.first # \u6682\u5b58 dp[i-1, j-1]\n    dp[0] += 1\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n    for j in 1...(m + 1)\n      temp = dp[j]\n      if s[i - 1] == t[j - 1]\n        # \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n        dp[j] = leftup\n      else\n        # \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n        dp[j] = [dp[j - 1], dp[j], leftup].min + 1\n      end\n      leftup = temp # \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n    end\n  end\n  dp[m]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/","title":"14.1 \u00a0 \u521d\u63a2\u52a8\u6001\u89c4\u5212","text":"

        \u52a8\u6001\u89c4\u5212\uff08dynamic programming\uff09\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u7b97\u6cd5\u8303\u5f0f\uff0c\u5b83\u5c06\u4e00\u4e2a\u95ee\u9898\u5206\u89e3\u4e3a\u4e00\u7cfb\u5217\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u5e76\u901a\u8fc7\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\u6765\u907f\u514d\u91cd\u590d\u8ba1\u7b97\uff0c\u4ece\u800c\u5927\u5e45\u63d0\u5347\u65f6\u95f4\u6548\u7387\u3002

        \u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u4ece\u4e00\u4e2a\u7ecf\u5178\u4f8b\u9898\u5165\u624b\uff0c\u5148\u7ed9\u51fa\u5b83\u7684\u66b4\u529b\u56de\u6eaf\u89e3\u6cd5\uff0c\u89c2\u5bdf\u5176\u4e2d\u5305\u542b\u7684\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u518d\u9010\u6b65\u5bfc\u51fa\u66f4\u9ad8\u6548\u7684\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u3002

        \u722c\u697c\u68af

        \u7ed9\u5b9a\u4e00\u4e2a\u5171\u6709 \\(n\\) \u9636\u7684\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\uff0c\u8bf7\u95ee\u6709\u591a\u5c11\u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\uff1f

        \u5982\u56fe 14-1 \u6240\u793a\uff0c\u5bf9\u4e8e\u4e00\u4e2a \\(3\\) \u9636\u697c\u68af\uff0c\u5171\u6709 \\(3\\) \u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\u3002

        \u56fe 14-1 \u00a0 \u722c\u5230\u7b2c 3 \u9636\u7684\u65b9\u6848\u6570\u91cf

        \u672c\u9898\u7684\u76ee\u6807\u662f\u6c42\u89e3\u65b9\u6848\u6570\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u901a\u8fc7\u56de\u6eaf\u6765\u7a77\u4e3e\u6240\u6709\u53ef\u80fd\u6027\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5c06\u722c\u697c\u68af\u60f3\u8c61\u4e3a\u4e00\u4e2a\u591a\u8f6e\u9009\u62e9\u7684\u8fc7\u7a0b\uff1a\u4ece\u5730\u9762\u51fa\u53d1\uff0c\u6bcf\u8f6e\u9009\u62e9\u4e0a \\(1\\) \u9636\u6216 \\(2\\) \u9636\uff0c\u6bcf\u5f53\u5230\u8fbe\u697c\u68af\u9876\u90e8\u65f6\u5c31\u5c06\u65b9\u6848\u6570\u91cf\u52a0 \\(1\\) \uff0c\u5f53\u8d8a\u8fc7\u697c\u68af\u9876\u90e8\u65f6\u5c31\u5c06\u5176\u526a\u679d\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_backtrack.py
        def backtrack(choices: list[int], state: int, n: int, res: list[int]) -> int:\n    \"\"\"\u56de\u6eaf\"\"\"\n    # \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n    if state == n:\n        res[0] += 1\n    # \u904d\u5386\u6240\u6709\u9009\u62e9\n    for choice in choices:\n        # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n        if state + choice > n:\n            continue\n        # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n        backtrack(choices, state + choice, n, res)\n        # \u56de\u9000\n\ndef climbing_stairs_backtrack(n: int) -> int:\n    \"\"\"\u722c\u697c\u68af\uff1a\u56de\u6eaf\"\"\"\n    choices = [1, 2]  # \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n    state = 0  # \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n    res = [0]  # \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n    backtrack(choices, state, n, res)\n    return res[0]\n
        climbing_stairs_backtrack.cpp
        /* \u56de\u6eaf */\nvoid backtrack(vector<int> &choices, int state, int n, vector<int> &res) {\n    // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n    if (state == n)\n        res[0]++;\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (auto &choice : choices) {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n        if (state + choice > n)\n            continue;\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n        backtrack(choices, state + choice, n, res);\n        // \u56de\u9000\n    }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\n    vector<int> choices = {1, 2}; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n    int state = 0;                // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n    vector<int> res = {0};        // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n    backtrack(choices, state, n, res);\n    return res[0];\n}\n
        climbing_stairs_backtrack.java
        /* \u56de\u6eaf */\nvoid backtrack(List<Integer> choices, int state, int n, List<Integer> res) {\n    // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n    if (state == n)\n        res.set(0, res.get(0) + 1);\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (Integer choice : choices) {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n        if (state + choice > n)\n            continue;\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n        backtrack(choices, state + choice, n, res);\n        // \u56de\u9000\n    }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\n    List<Integer> choices = Arrays.asList(1, 2); // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n    int state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n    List<Integer> res = new ArrayList<>();\n    res.add(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n    backtrack(choices, state, n, res);\n    return res.get(0);\n}\n
        climbing_stairs_backtrack.cs
        /* \u56de\u6eaf */\nvoid Backtrack(List<int> choices, int state, int n, List<int> res) {\n    // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n    if (state == n)\n        res[0]++;\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    foreach (int choice in choices) {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n        if (state + choice > n)\n            continue;\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n        Backtrack(choices, state + choice, n, res);\n        // \u56de\u9000\n    }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint ClimbingStairsBacktrack(int n) {\n    List<int> choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n    int state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n    List<int> res = [0]; // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n    Backtrack(choices, state, n, res);\n    return res[0];\n}\n
        climbing_stairs_backtrack.go
        /* \u56de\u6eaf */\nfunc backtrack(choices []int, state, n int, res []int) {\n    // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n    if state == n {\n        res[0] = res[0] + 1\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for _, choice := range choices {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n        if state+choice > n {\n            continue\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n        backtrack(choices, state+choice, n, res)\n        // \u56de\u9000\n    }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunc climbingStairsBacktrack(n int) int {\n    // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n    choices := []int{1, 2}\n    // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n    state := 0\n    res := make([]int, 1)\n    // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n    res[0] = 0\n    backtrack(choices, state, n, res)\n    return res[0]\n}\n
        climbing_stairs_backtrack.swift
        /* \u56de\u6eaf */\nfunc backtrack(choices: [Int], state: Int, n: Int, res: inout [Int]) {\n    // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n    if state == n {\n        res[0] += 1\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for choice in choices {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n        if state + choice > n {\n            continue\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n        backtrack(choices: choices, state: state + choice, n: n, res: &res)\n        // \u56de\u9000\n    }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunc climbingStairsBacktrack(n: Int) -> Int {\n    let choices = [1, 2] // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n    let state = 0 // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n    var res: [Int] = []\n    res.append(0) // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n    backtrack(choices: choices, state: state, n: n, res: &res)\n    return res[0]\n}\n
        climbing_stairs_backtrack.js
        /* \u56de\u6eaf */\nfunction backtrack(choices, state, n, res) {\n    // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n    if (state === n) res.set(0, res.get(0) + 1);\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (const choice of choices) {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n        if (state + choice > n) continue;\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n        backtrack(choices, state + choice, n, res);\n        // \u56de\u9000\n    }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunction climbingStairsBacktrack(n) {\n    const choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n    const state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n    const res = new Map();\n    res.set(0, 0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n    backtrack(choices, state, n, res);\n    return res.get(0);\n}\n
        climbing_stairs_backtrack.ts
        /* \u56de\u6eaf */\nfunction backtrack(\n    choices: number[],\n    state: number,\n    n: number,\n    res: Map<0, any>\n): void {\n    // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n    if (state === n) res.set(0, res.get(0) + 1);\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (const choice of choices) {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n        if (state + choice > n) continue;\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n        backtrack(choices, state + choice, n, res);\n        // \u56de\u9000\n    }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunction climbingStairsBacktrack(n: number): number {\n    const choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n    const state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n    const res = new Map();\n    res.set(0, 0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n    backtrack(choices, state, n, res);\n    return res.get(0);\n}\n
        climbing_stairs_backtrack.dart
        /* \u56de\u6eaf */\nvoid backtrack(List<int> choices, int state, int n, List<int> res) {\n  // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n  if (state == n) {\n    res[0]++;\n  }\n  // \u904d\u5386\u6240\u6709\u9009\u62e9\n  for (int choice in choices) {\n    // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n    if (state + choice > n) continue;\n    // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n    backtrack(choices, state + choice, n, res);\n    // \u56de\u9000\n  }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\n  List<int> choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n  int state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n  List<int> res = [];\n  res.add(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n  backtrack(choices, state, n, res);\n  return res[0];\n}\n
        climbing_stairs_backtrack.rs
        /* \u56de\u6eaf */\nfn backtrack(choices: &[i32], state: i32, n: i32, res: &mut [i32]) {\n    // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n    if state == n {\n        res[0] = res[0] + 1;\n    }\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for &choice in choices {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n        if state + choice > n {\n            continue;\n        }\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n        backtrack(choices, state + choice, n, res);\n        // \u56de\u9000\n    }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfn climbing_stairs_backtrack(n: usize) -> i32 {\n    let choices = vec![1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n    let state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n    let mut res = Vec::new();\n    res.push(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n    backtrack(&choices, state, n as i32, &mut res);\n    res[0]\n}\n
        climbing_stairs_backtrack.c
        /* \u56de\u6eaf */\nvoid backtrack(int *choices, int state, int n, int *res, int len) {\n    // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n    if (state == n)\n        res[0]++;\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (int i = 0; i < len; i++) {\n        int choice = choices[i];\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n        if (state + choice > n)\n            continue;\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n        backtrack(choices, state + choice, n, res, len);\n        // \u56de\u9000\n    }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\n    int choices[2] = {1, 2}; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n    int state = 0;           // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n    int *res = (int *)malloc(sizeof(int));\n    *res = 0; // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n    int len = sizeof(choices) / sizeof(int);\n    backtrack(choices, state, n, res, len);\n    int result = *res;\n    free(res);\n    return result;\n}\n
        climbing_stairs_backtrack.kt
        /* \u56de\u6eaf */\nfun backtrack(\n    choices: MutableList<Int>,\n    state: Int,\n    n: Int,\n    res: MutableList<Int>\n) {\n    // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n    if (state == n)\n        res[0] = res[0] + 1\n    // \u904d\u5386\u6240\u6709\u9009\u62e9\n    for (choice in choices) {\n        // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n        if (state + choice > n) continue\n        // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n        backtrack(choices, state + choice, n, res)\n        // \u56de\u9000\n    }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfun climbingStairsBacktrack(n: Int): Int {\n    val choices = mutableListOf(1, 2) // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n    val state = 0 // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n    val res = mutableListOf<Int>()\n    res.add(0) // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n    backtrack(choices, state, n, res)\n    return res[0]\n}\n
        climbing_stairs_backtrack.rb
        ### \u56de\u6eaf ###\ndef backtrack(choices, state, n, res)\n  # \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n  res[0] += 1 if state == n\n  # \u904d\u5386\u6240\u6709\u9009\u62e9\n  for choice in choices\n    # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n    next if state + choice > n\n\n    # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n    backtrack(choices, state + choice, n, res)\n  end\n  # \u56de\u9000\nend\n\n### \u722c\u697c\u68af\uff1a\u56de\u6eaf ###\ndef climbing_stairs_backtrack(n)\n  choices = [1, 2] # \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n  state = 0 # \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n  res = [0] # \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n  backtrack(choices, state, n, res)\n  res.first\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1411","title":"14.1.1 \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u7d22","text":"

        \u56de\u6eaf\u7b97\u6cd5\u901a\u5e38\u5e76\u4e0d\u663e\u5f0f\u5730\u5bf9\u95ee\u9898\u8fdb\u884c\u62c6\u89e3\uff0c\u800c\u662f\u5c06\u6c42\u89e3\u95ee\u9898\u770b\u4f5c\u4e00\u7cfb\u5217\u51b3\u7b56\u6b65\u9aa4\uff0c\u901a\u8fc7\u8bd5\u63a2\u548c\u526a\u679d\uff0c\u641c\u7d22\u6240\u6709\u53ef\u80fd\u7684\u89e3\u3002

        \u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u4ece\u95ee\u9898\u5206\u89e3\u7684\u89d2\u5ea6\u5206\u6790\u8fd9\u9053\u9898\u3002\u8bbe\u722c\u5230\u7b2c \\(i\\) \u9636\u5171\u6709 \\(dp[i]\\) \u79cd\u65b9\u6848\uff0c\u90a3\u4e48 \\(dp[i]\\) \u5c31\u662f\u539f\u95ee\u9898\uff0c\u5176\u5b50\u95ee\u9898\u5305\u62ec\uff1a

        \\[ dp[i-1], dp[i-2], \\dots, dp[2], dp[1] \\]

        \u7531\u4e8e\u6bcf\u8f6e\u53ea\u80fd\u4e0a \\(1\\) \u9636\u6216 \\(2\\) \u9636\uff0c\u56e0\u6b64\u5f53\u6211\u4eec\u7ad9\u5728\u7b2c \\(i\\) \u9636\u697c\u68af\u4e0a\u65f6\uff0c\u4e0a\u4e00\u8f6e\u53ea\u53ef\u80fd\u7ad9\u5728\u7b2c \\(i - 1\\) \u9636\u6216\u7b2c \\(i - 2\\) \u9636\u4e0a\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u53ea\u80fd\u4ece\u7b2c \\(i -1\\) \u9636\u6216\u7b2c \\(i - 2\\) \u9636\u8fc8\u5411\u7b2c \\(i\\) \u9636\u3002

        \u7531\u6b64\u4fbf\u53ef\u5f97\u51fa\u4e00\u4e2a\u91cd\u8981\u63a8\u8bba\uff1a\u722c\u5230\u7b2c \\(i - 1\\) \u9636\u7684\u65b9\u6848\u6570\u52a0\u4e0a\u722c\u5230\u7b2c \\(i - 2\\) \u9636\u7684\u65b9\u6848\u6570\u5c31\u7b49\u4e8e\u722c\u5230\u7b2c \\(i\\) \u9636\u7684\u65b9\u6848\u6570\u3002\u516c\u5f0f\u5982\u4e0b\uff1a

        \\[ dp[i] = dp[i-1] + dp[i-2] \\]

        \u8fd9\u610f\u5473\u7740\u5728\u722c\u697c\u68af\u95ee\u9898\u4e2d\uff0c\u5404\u4e2a\u5b50\u95ee\u9898\u4e4b\u95f4\u5b58\u5728\u9012\u63a8\u5173\u7cfb\uff0c\u539f\u95ee\u9898\u7684\u89e3\u53ef\u4ee5\u7531\u5b50\u95ee\u9898\u7684\u89e3\u6784\u5efa\u5f97\u6765\u3002\u56fe 14-2 \u5c55\u793a\u4e86\u8be5\u9012\u63a8\u5173\u7cfb\u3002

        \u56fe 14-2 \u00a0 \u65b9\u6848\u6570\u91cf\u9012\u63a8\u5173\u7cfb

        \u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9012\u63a8\u516c\u5f0f\u5f97\u5230\u66b4\u529b\u641c\u7d22\u89e3\u6cd5\u3002\u4ee5 \\(dp[n]\\) \u4e3a\u8d77\u59cb\u70b9\uff0c\u9012\u5f52\u5730\u5c06\u4e00\u4e2a\u8f83\u5927\u95ee\u9898\u62c6\u89e3\u4e3a\u4e24\u4e2a\u8f83\u5c0f\u95ee\u9898\u7684\u548c\uff0c\u76f4\u81f3\u5230\u8fbe\u6700\u5c0f\u5b50\u95ee\u9898 \\(dp[1]\\) \u548c \\(dp[2]\\) \u65f6\u8fd4\u56de\u3002\u5176\u4e2d\uff0c\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\u662f\u5df2\u77e5\u7684\uff0c\u5373 \\(dp[1] = 1\\)\u3001\\(dp[2] = 2\\) \uff0c\u8868\u793a\u722c\u5230\u7b2c \\(1\\)\u3001\\(2\\) \u9636\u5206\u522b\u6709 \\(1\\)\u3001\\(2\\) \u79cd\u65b9\u6848\u3002

        \u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5b83\u548c\u6807\u51c6\u56de\u6eaf\u4ee3\u7801\u90fd\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff0c\u4f46\u66f4\u52a0\u7b80\u6d01\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dfs.py
        def dfs(i: int) -> int:\n    \"\"\"\u641c\u7d22\"\"\"\n    # \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if i == 1 or i == 2:\n        return i\n    # dp[i] = dp[i-1] + dp[i-2]\n    count = dfs(i - 1) + dfs(i - 2)\n    return count\n\ndef climbing_stairs_dfs(n: int) -> int:\n    \"\"\"\u722c\u697c\u68af\uff1a\u641c\u7d22\"\"\"\n    return dfs(n)\n
        climbing_stairs_dfs.cpp
        /* \u641c\u7d22 */\nint dfs(int i) {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\n    return dfs(n);\n}\n
        climbing_stairs_dfs.java
        /* \u641c\u7d22 */\nint dfs(int i) {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\n    return dfs(n);\n}\n
        climbing_stairs_dfs.cs
        /* \u641c\u7d22 */\nint DFS(int i) {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = DFS(i - 1) + DFS(i - 2);\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint ClimbingStairsDFS(int n) {\n    return DFS(n);\n}\n
        climbing_stairs_dfs.go
        /* \u641c\u7d22 */\nfunc dfs(i int) int {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if i == 1 || i == 2 {\n        return i\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    count := dfs(i-1) + dfs(i-2)\n    return count\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunc climbingStairsDFS(n int) int {\n    return dfs(n)\n}\n
        climbing_stairs_dfs.swift
        /* \u641c\u7d22 */\nfunc dfs(i: Int) -> Int {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if i == 1 || i == 2 {\n        return i\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    let count = dfs(i: i - 1) + dfs(i: i - 2)\n    return count\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunc climbingStairsDFS(n: Int) -> Int {\n    dfs(i: n)\n}\n
        climbing_stairs_dfs.js
        /* \u641c\u7d22 */\nfunction dfs(i) {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i === 1 || i === 2) return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    const count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunction climbingStairsDFS(n) {\n    return dfs(n);\n}\n
        climbing_stairs_dfs.ts
        /* \u641c\u7d22 */\nfunction dfs(i: number): number {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i === 1 || i === 2) return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    const count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunction climbingStairsDFS(n: number): number {\n    return dfs(n);\n}\n
        climbing_stairs_dfs.dart
        /* \u641c\u7d22 */\nint dfs(int i) {\n  // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n  if (i == 1 || i == 2) return i;\n  // dp[i] = dp[i-1] + dp[i-2]\n  int count = dfs(i - 1) + dfs(i - 2);\n  return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\n  return dfs(n);\n}\n
        climbing_stairs_dfs.rs
        /* \u641c\u7d22 */\nfn dfs(i: usize) -> i32 {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if i == 1 || i == 2 {\n        return i as i32;\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    let count = dfs(i - 1) + dfs(i - 2);\n    count\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfn climbing_stairs_dfs(n: usize) -> i32 {\n    dfs(n)\n}\n
        climbing_stairs_dfs.c
        /* \u641c\u7d22 */\nint dfs(int i) {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\n    return dfs(n);\n}\n
        climbing_stairs_dfs.kt
        /* \u641c\u7d22 */\nfun dfs(i: Int): Int {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i == 1 || i == 2) return i\n    // dp[i] = dp[i-1] + dp[i-2]\n    val count = dfs(i - 1) + dfs(i - 2)\n    return count\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfun climbingStairsDFS(n: Int): Int {\n    return dfs(n)\n}\n
        climbing_stairs_dfs.rb
        ### \u641c\u7d22 ###\ndef dfs(i)\n  # \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n  return i if i == 1 || i == 2\n  # dp[i] = dp[i-1] + dp[i-2]\n  dfs(i - 1) + dfs(i - 2)\nend\n\n### \u722c\u697c\u68af\uff1a\u641c\u7d22 ###\ndef climbing_stairs_dfs(n)\n  dfs(n)\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 14-3 \u5c55\u793a\u4e86\u66b4\u529b\u641c\u7d22\u5f62\u6210\u7684\u9012\u5f52\u6811\u3002\u5bf9\u4e8e\u95ee\u9898 \\(dp[n]\\) \uff0c\u5176\u9012\u5f52\u6811\u7684\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002\u6307\u6570\u9636\u5c5e\u4e8e\u7206\u70b8\u5f0f\u589e\u957f\uff0c\u5982\u679c\u6211\u4eec\u8f93\u5165\u4e00\u4e2a\u6bd4\u8f83\u5927\u7684 \\(n\\) \uff0c\u5219\u4f1a\u9677\u5165\u6f2b\u957f\u7684\u7b49\u5f85\u4e4b\u4e2d\u3002

        \u56fe 14-3 \u00a0 \u722c\u697c\u68af\u5bf9\u5e94\u9012\u5f52\u6811

        \u89c2\u5bdf\u56fe 14-3 \uff0c\u6307\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f\u201c\u91cd\u53e0\u5b50\u95ee\u9898\u201d\u5bfc\u81f4\u7684\u3002\u4f8b\u5982 \\(dp[9]\\) \u88ab\u5206\u89e3\u4e3a \\(dp[8]\\) \u548c \\(dp[7]\\) \uff0c\\(dp[8]\\) \u88ab\u5206\u89e3\u4e3a \\(dp[7]\\) \u548c \\(dp[6]\\) \uff0c\u4e24\u8005\u90fd\u5305\u542b\u5b50\u95ee\u9898 \\(dp[7]\\) \u3002

        \u4ee5\u6b64\u7c7b\u63a8\uff0c\u5b50\u95ee\u9898\u4e2d\u5305\u542b\u66f4\u5c0f\u7684\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u5b50\u5b50\u5b59\u5b59\u65e0\u7a77\u5c3d\u4e5f\u3002\u7edd\u5927\u90e8\u5206\u8ba1\u7b97\u8d44\u6e90\u90fd\u6d6a\u8d39\u5728\u8fd9\u4e9b\u91cd\u53e0\u7684\u5b50\u95ee\u9898\u4e0a\u3002

        "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1412","title":"14.1.2 \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22","text":"

        \u4e3a\u4e86\u63d0\u5347\u7b97\u6cd5\u6548\u7387\uff0c\u6211\u4eec\u5e0c\u671b\u6240\u6709\u7684\u91cd\u53e0\u5b50\u95ee\u9898\u90fd\u53ea\u88ab\u8ba1\u7b97\u4e00\u6b21\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u6570\u7ec4 mem \u6765\u8bb0\u5f55\u6bcf\u4e2a\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5e76\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u5c06\u91cd\u53e0\u5b50\u95ee\u9898\u526a\u679d\u3002

        1. \u5f53\u9996\u6b21\u8ba1\u7b97 \\(dp[i]\\) \u65f6\uff0c\u6211\u4eec\u5c06\u5176\u8bb0\u5f55\u81f3 mem[i] \uff0c\u4ee5\u4fbf\u4e4b\u540e\u4f7f\u7528\u3002
        2. \u5f53\u518d\u6b21\u9700\u8981\u8ba1\u7b97 \\(dp[i]\\) \u65f6\uff0c\u6211\u4eec\u4fbf\u53ef\u76f4\u63a5\u4ece mem[i] \u4e2d\u83b7\u53d6\u7ed3\u679c\uff0c\u4ece\u800c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u8be5\u5b50\u95ee\u9898\u3002

        \u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dfs_mem.py
        def dfs(i: int, mem: list[int]) -> int:\n    \"\"\"\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n    # \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if i == 1 or i == 2:\n        return i\n    # \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n    if mem[i] != -1:\n        return mem[i]\n    # dp[i] = dp[i-1] + dp[i-2]\n    count = dfs(i - 1, mem) + dfs(i - 2, mem)\n    # \u8bb0\u5f55 dp[i]\n    mem[i] = count\n    return count\n\ndef climbing_stairs_dfs_mem(n: int) -> int:\n    \"\"\"\u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n    # mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n    mem = [-1] * (n + 1)\n    return dfs(n, mem)\n
        climbing_stairs_dfs_mem.cpp
        /* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, vector<int> &mem) {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i == 1 || i == 2)\n        return i;\n    // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // \u8bb0\u5f55 dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n    // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n    vector<int> mem(n + 1, -1);\n    return dfs(n, mem);\n}\n
        climbing_stairs_dfs_mem.java
        /* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, int[] mem) {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i == 1 || i == 2)\n        return i;\n    // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // \u8bb0\u5f55 dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n    // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n    int[] mem = new int[n + 1];\n    Arrays.fill(mem, -1);\n    return dfs(n, mem);\n}\n
        climbing_stairs_dfs_mem.cs
        /* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint DFS(int i, int[] mem) {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i == 1 || i == 2)\n        return i;\n    // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = DFS(i - 1, mem) + DFS(i - 2, mem);\n    // \u8bb0\u5f55 dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint ClimbingStairsDFSMem(int n) {\n    // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n    int[] mem = new int[n + 1];\n    Array.Fill(mem, -1);\n    return DFS(n, mem);\n}\n
        climbing_stairs_dfs_mem.go
        /* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc dfsMem(i int, mem []int) int {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if i == 1 || i == 2 {\n        return i\n    }\n    // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n    if mem[i] != -1 {\n        return mem[i]\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    count := dfsMem(i-1, mem) + dfsMem(i-2, mem)\n    // \u8bb0\u5f55 dp[i]\n    mem[i] = count\n    return count\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc climbingStairsDFSMem(n int) int {\n    // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n    mem := make([]int, n+1)\n    for i := range mem {\n        mem[i] = -1\n    }\n    return dfsMem(n, mem)\n}\n
        climbing_stairs_dfs_mem.swift
        /* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc dfs(i: Int, mem: inout [Int]) -> Int {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if i == 1 || i == 2 {\n        return i\n    }\n    // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n    if mem[i] != -1 {\n        return mem[i]\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    let count = dfs(i: i - 1, mem: &mem) + dfs(i: i - 2, mem: &mem)\n    // \u8bb0\u5f55 dp[i]\n    mem[i] = count\n    return count\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc climbingStairsDFSMem(n: Int) -> Int {\n    // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n    var mem = Array(repeating: -1, count: n + 1)\n    return dfs(i: n, mem: &mem)\n}\n
        climbing_stairs_dfs_mem.js
        /* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction dfs(i, mem) {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i === 1 || i === 2) return i;\n    // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n    if (mem[i] != -1) return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    const count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // \u8bb0\u5f55 dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction climbingStairsDFSMem(n) {\n    // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n    const mem = new Array(n + 1).fill(-1);\n    return dfs(n, mem);\n}\n
        climbing_stairs_dfs_mem.ts
        /* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction dfs(i: number, mem: number[]): number {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i === 1 || i === 2) return i;\n    // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n    if (mem[i] != -1) return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    const count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // \u8bb0\u5f55 dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction climbingStairsDFSMem(n: number): number {\n    // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n    const mem = new Array(n + 1).fill(-1);\n    return dfs(n, mem);\n}\n
        climbing_stairs_dfs_mem.dart
        /* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, List<int> mem) {\n  // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n  if (i == 1 || i == 2) return i;\n  // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n  if (mem[i] != -1) return mem[i];\n  // dp[i] = dp[i-1] + dp[i-2]\n  int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n  // \u8bb0\u5f55 dp[i]\n  mem[i] = count;\n  return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n  // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n  List<int> mem = List.filled(n + 1, -1);\n  return dfs(n, mem);\n}\n
        climbing_stairs_dfs_mem.rs
        /* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn dfs(i: usize, mem: &mut [i32]) -> i32 {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if i == 1 || i == 2 {\n        return i as i32;\n    }\n    // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n    if mem[i] != -1 {\n        return mem[i];\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    let count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // \u8bb0\u5f55 dp[i]\n    mem[i] = count;\n    count\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn climbing_stairs_dfs_mem(n: usize) -> i32 {\n    // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n    let mut mem = vec![-1; n + 1];\n    dfs(n, &mut mem)\n}\n
        climbing_stairs_dfs_mem.c
        /* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, int *mem) {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i == 1 || i == 2)\n        return i;\n    // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // \u8bb0\u5f55 dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n    // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n    int *mem = (int *)malloc((n + 1) * sizeof(int));\n    for (int i = 0; i <= n; i++) {\n        mem[i] = -1;\n    }\n    int result = dfs(n, mem);\n    free(mem);\n    return result;\n}\n
        climbing_stairs_dfs_mem.kt
        /* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfun dfs(i: Int, mem: IntArray): Int {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if (i == 1 || i == 2) return i\n    // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n    if (mem[i] != -1) return mem[i]\n    // dp[i] = dp[i-1] + dp[i-2]\n    val count = dfs(i - 1, mem) + dfs(i - 2, mem)\n    // \u8bb0\u5f55 dp[i]\n    mem[i] = count\n    return count\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfun climbingStairsDFSMem(n: Int): Int {\n    // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n    val mem = IntArray(n + 1)\n    mem.fill(-1)\n    return dfs(n, mem)\n}\n
        climbing_stairs_dfs_mem.rb
        ### \u8bb0\u5fc6\u5316\u641c\u7d22 ###\ndef dfs(i, mem)\n  # \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n  return i if i == 1 || i == 2\n  # \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n  return mem[i] if mem[i] != -1\n\n  # dp[i] = dp[i-1] + dp[i-2]\n  count = dfs(i - 1, mem) + dfs(i - 2, mem)\n  # \u8bb0\u5f55 dp[i]\n  mem[i] = count\nend\n\n### \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 ###\ndef climbing_stairs_dfs_mem(n)\n  # mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n  mem = Array.new(n + 1, -1)\n  dfs(n, mem)\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u89c2\u5bdf\u56fe 14-4 \uff0c\u7ecf\u8fc7\u8bb0\u5fc6\u5316\u5904\u7406\u540e\uff0c\u6240\u6709\u91cd\u53e0\u5b50\u95ee\u9898\u90fd\u53ea\u9700\u8ba1\u7b97\u4e00\u6b21\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(n)\\) \uff0c\u8fd9\u662f\u4e00\u4e2a\u5de8\u5927\u7684\u98de\u8dc3\u3002

        \u56fe 14-4 \u00a0 \u8bb0\u5fc6\u5316\u641c\u7d22\u5bf9\u5e94\u9012\u5f52\u6811

        "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1413","title":"14.1.3 \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52a8\u6001\u89c4\u5212","text":"

        \u8bb0\u5fc6\u5316\u641c\u7d22\u662f\u4e00\u79cd\u201c\u4ece\u9876\u81f3\u5e95\u201d\u7684\u65b9\u6cd5\uff1a\u6211\u4eec\u4ece\u539f\u95ee\u9898\uff08\u6839\u8282\u70b9\uff09\u5f00\u59cb\uff0c\u9012\u5f52\u5730\u5c06\u8f83\u5927\u5b50\u95ee\u9898\u5206\u89e3\u4e3a\u8f83\u5c0f\u5b50\u95ee\u9898\uff0c\u76f4\u81f3\u89e3\u5df2\u77e5\u7684\u6700\u5c0f\u5b50\u95ee\u9898\uff08\u53f6\u8282\u70b9\uff09\u3002\u4e4b\u540e\uff0c\u901a\u8fc7\u56de\u6eaf\u9010\u5c42\u6536\u96c6\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u6784\u5efa\u51fa\u539f\u95ee\u9898\u7684\u89e3\u3002

        \u4e0e\u4e4b\u76f8\u53cd\uff0c\u52a8\u6001\u89c4\u5212\u662f\u4e00\u79cd\u201c\u4ece\u5e95\u81f3\u9876\u201d\u7684\u65b9\u6cd5\uff1a\u4ece\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\u5f00\u59cb\uff0c\u8fed\u4ee3\u5730\u6784\u5efa\u66f4\u5927\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u76f4\u81f3\u5f97\u5230\u539f\u95ee\u9898\u7684\u89e3\u3002

        \u7531\u4e8e\u52a8\u6001\u89c4\u5212\u4e0d\u5305\u542b\u56de\u6eaf\u8fc7\u7a0b\uff0c\u56e0\u6b64\u53ea\u9700\u4f7f\u7528\u5faa\u73af\u8fed\u4ee3\u5b9e\u73b0\uff0c\u65e0\u987b\u4f7f\u7528\u9012\u5f52\u3002\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521d\u59cb\u5316\u4e00\u4e2a\u6570\u7ec4 dp \u6765\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5b83\u8d77\u5230\u4e86\u4e0e\u8bb0\u5fc6\u5316\u641c\u7d22\u4e2d\u6570\u7ec4 mem \u76f8\u540c\u7684\u8bb0\u5f55\u4f5c\u7528\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dp.py
        def climbing_stairs_dp(n: int) -> int:\n    \"\"\"\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n    if n == 1 or n == 2:\n        return n\n    # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    dp = [0] * (n + 1)\n    # \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1], dp[2] = 1, 2\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i in range(3, n + 1):\n        dp[i] = dp[i - 1] + dp[i - 2]\n    return dp[n]\n
        climbing_stairs_dp.cpp
        /* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    vector<int> dp(n + 1);\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
        climbing_stairs_dp.java
        /* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    int[] dp = new int[n + 1];\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
        climbing_stairs_dp.cs
        /* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint ClimbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    int[] dp = new int[n + 1];\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
        climbing_stairs_dp.go
        /* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDP(n int) int {\n    if n == 1 || n == 2 {\n        return n\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    dp := make([]int, n+1)\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = 1\n    dp[2] = 2\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i := 3; i <= n; i++ {\n        dp[i] = dp[i-1] + dp[i-2]\n    }\n    return dp[n]\n}\n
        climbing_stairs_dp.swift
        /* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDP(n: Int) -> Int {\n    if n == 1 || n == 2 {\n        return n\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    var dp = Array(repeating: 0, count: n + 1)\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = 1\n    dp[2] = 2\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i in 3 ... n {\n        dp[i] = dp[i - 1] + dp[i - 2]\n    }\n    return dp[n]\n}\n
        climbing_stairs_dp.js
        /* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDP(n) {\n    if (n === 1 || n === 2) return n;\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    const dp = new Array(n + 1).fill(-1);\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (let i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
        climbing_stairs_dp.ts
        /* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDP(n: number): number {\n    if (n === 1 || n === 2) return n;\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    const dp = new Array(n + 1).fill(-1);\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (let i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
        climbing_stairs_dp.dart
        /* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\n  if (n == 1 || n == 2) return n;\n  // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n  List<int> dp = List.filled(n + 1, 0);\n  // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n  dp[1] = 1;\n  dp[2] = 2;\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n  for (int i = 3; i <= n; i++) {\n    dp[i] = dp[i - 1] + dp[i - 2];\n  }\n  return dp[n];\n}\n
        climbing_stairs_dp.rs
        /* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfn climbing_stairs_dp(n: usize) -> i32 {\n    // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n    if n == 1 || n == 2 {\n        return n as i32;\n    }\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    let mut dp = vec![-1; n + 1];\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i in 3..=n {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    dp[n]\n}\n
        climbing_stairs_dp.c
        /* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    int *dp = (int *)malloc((n + 1) * sizeof(int));\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    int result = dp[n];\n    free(dp);\n    return result;\n}\n
        climbing_stairs_dp.kt
        /* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfun climbingStairsDP(n: Int): Int {\n    if (n == 1 || n == 2) return n\n    // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n    val dp = IntArray(n + 1)\n    // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n    dp[1] = 1\n    dp[2] = 2\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for (i in 3..n) {\n        dp[i] = dp[i - 1] + dp[i - 2]\n    }\n    return dp[n]\n}\n
        climbing_stairs_dp.rb
        ### \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 ###\ndef climbing_stairs_dp(n)\n  return n  if n == 1 || n == 2\n\n  # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n  dp = Array.new(n + 1, 0)\n  # \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n  dp[1], dp[2] = 1, 2\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n  (3...(n + 1)).each { |i| dp[i] = dp[i - 1] + dp[i - 2] }\n\n  dp[n]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 14-5 \u6a21\u62df\u4e86\u4ee5\u4e0a\u4ee3\u7801\u7684\u6267\u884c\u8fc7\u7a0b\u3002

        \u56fe 14-5 \u00a0 \u722c\u697c\u68af\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

        \u4e0e\u56de\u6eaf\u7b97\u6cd5\u4e00\u6837\uff0c\u52a8\u6001\u89c4\u5212\u4e5f\u4f7f\u7528\u201c\u72b6\u6001\u201d\u6982\u5ff5\u6765\u8868\u793a\u95ee\u9898\u6c42\u89e3\u7684\u7279\u5b9a\u9636\u6bb5\uff0c\u6bcf\u4e2a\u72b6\u6001\u90fd\u5bf9\u5e94\u4e00\u4e2a\u5b50\u95ee\u9898\u4ee5\u53ca\u76f8\u5e94\u7684\u5c40\u90e8\u6700\u4f18\u89e3\u3002\u4f8b\u5982\uff0c\u722c\u697c\u68af\u95ee\u9898\u7684\u72b6\u6001\u5b9a\u4e49\u4e3a\u5f53\u524d\u6240\u5728\u697c\u68af\u9636\u6570 \\(i\\) \u3002

        \u6839\u636e\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u53ef\u4ee5\u603b\u7ed3\u51fa\u52a8\u6001\u89c4\u5212\u7684\u5e38\u7528\u672f\u8bed\u3002

        • \u5c06\u6570\u7ec4 dp \u79f0\u4e3a dp \u8868\uff0c\\(dp[i]\\) \u8868\u793a\u72b6\u6001 \\(i\\) \u5bf9\u5e94\u5b50\u95ee\u9898\u7684\u89e3\u3002
        • \u5c06\u6700\u5c0f\u5b50\u95ee\u9898\u5bf9\u5e94\u7684\u72b6\u6001\uff08\u7b2c \\(1\\) \u9636\u548c\u7b2c \\(2\\) \u9636\u697c\u68af\uff09\u79f0\u4e3a\u521d\u59cb\u72b6\u6001\u3002
        • \u5c06\u9012\u63a8\u516c\u5f0f \\(dp[i] = dp[i-1] + dp[i-2]\\) \u79f0\u4e3a\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u3002
        "},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1414","title":"14.1.4 \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

        \u7ec6\u5fc3\u7684\u8bfb\u8005\u53ef\u80fd\u53d1\u73b0\u4e86\uff0c\u7531\u4e8e \\(dp[i]\\) \u53ea\u4e0e \\(dp[i-1]\\) \u548c \\(dp[i-2]\\) \u6709\u5173\uff0c\u56e0\u6b64\u6211\u4eec\u65e0\u987b\u4f7f\u7528\u4e00\u4e2a\u6570\u7ec4 dp \u6765\u5b58\u50a8\u6240\u6709\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u800c\u53ea\u9700\u4e24\u4e2a\u53d8\u91cf\u6eda\u52a8\u524d\u8fdb\u5373\u53ef\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dp.py
        def climbing_stairs_dp_comp(n: int) -> int:\n    \"\"\"\u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n    if n == 1 or n == 2:\n        return n\n    a, b = 1, 2\n    for _ in range(3, n + 1):\n        a, b = b, a + b\n    return b\n
        climbing_stairs_dp.cpp
        /* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
        climbing_stairs_dp.java
        /* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
        climbing_stairs_dp.cs
        /* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint ClimbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
        climbing_stairs_dp.go
        /* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDPComp(n int) int {\n    if n == 1 || n == 2 {\n        return n\n    }\n    a, b := 1, 2\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n    for i := 3; i <= n; i++ {\n        a, b = b, a+b\n    }\n    return b\n}\n
        climbing_stairs_dp.swift
        /* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDPComp(n: Int) -> Int {\n    if n == 1 || n == 2 {\n        return n\n    }\n    var a = 1\n    var b = 2\n    for _ in 3 ... n {\n        (a, b) = (b, a + b)\n    }\n    return b\n}\n
        climbing_stairs_dp.js
        /* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDPComp(n) {\n    if (n === 1 || n === 2) return n;\n    let a = 1,\n        b = 2;\n    for (let i = 3; i <= n; i++) {\n        const tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
        climbing_stairs_dp.ts
        /* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDPComp(n: number): number {\n    if (n === 1 || n === 2) return n;\n    let a = 1,\n        b = 2;\n    for (let i = 3; i <= n; i++) {\n        const tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
        climbing_stairs_dp.dart
        /* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\n  if (n == 1 || n == 2) return n;\n  int a = 1, b = 2;\n  for (int i = 3; i <= n; i++) {\n    int tmp = b;\n    b = a + b;\n    a = tmp;\n  }\n  return b;\n}\n
        climbing_stairs_dp.rs
        /* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn climbing_stairs_dp_comp(n: usize) -> i32 {\n    if n == 1 || n == 2 {\n        return n as i32;\n    }\n    let (mut a, mut b) = (1, 2);\n    for _ in 3..=n {\n        let tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    b\n}\n
        climbing_stairs_dp.c
        /* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
        climbing_stairs_dp.kt
        /* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfun climbingStairsDPComp(n: Int): Int {\n    if (n == 1 || n == 2) return n\n    var a = 1\n    var b = 2\n    for (i in 3..n) {\n        val temp = b\n        b += a\n        a = temp\n    }\n    return b\n}\n
        climbing_stairs_dp.rb
        ### \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 ###\ndef climbing_stairs_dp_comp(n)\n  return n if n == 1 || n == 2\n\n  a, b = 1, 2\n  (3...(n + 1)).each { a, b = b, a + b }\n\n  b\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u89c2\u5bdf\u4ee5\u4e0a\u4ee3\u7801\uff0c\u7531\u4e8e\u7701\u53bb\u4e86\u6570\u7ec4 dp \u5360\u7528\u7684\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n)\\) \u964d\u81f3 \\(O(1)\\) \u3002

        \u5728\u52a8\u6001\u89c4\u5212\u95ee\u9898\u4e2d\uff0c\u5f53\u524d\u72b6\u6001\u5f80\u5f80\u4ec5\u4e0e\u524d\u9762\u6709\u9650\u4e2a\u72b6\u6001\u6709\u5173\uff0c\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u53ea\u4fdd\u7559\u5fc5\u8981\u7684\u72b6\u6001\uff0c\u901a\u8fc7\u201c\u964d\u7ef4\u201d\u6765\u8282\u7701\u5185\u5b58\u7a7a\u95f4\u3002\u8fd9\u79cd\u7a7a\u95f4\u4f18\u5316\u6280\u5de7\u88ab\u79f0\u4e3a\u201c\u6eda\u52a8\u53d8\u91cf\u201d\u6216\u201c\u6eda\u52a8\u6570\u7ec4\u201d\u3002

        "},{"location":"chapter_dynamic_programming/knapsack_problem/","title":"14.4 \u00a0 0-1 \u80cc\u5305\u95ee\u9898","text":"

        \u80cc\u5305\u95ee\u9898\u662f\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u52a8\u6001\u89c4\u5212\u5165\u95e8\u9898\u76ee\uff0c\u662f\u52a8\u6001\u89c4\u5212\u4e2d\u6700\u5e38\u89c1\u7684\u95ee\u9898\u5f62\u5f0f\u3002\u5176\u5177\u6709\u5f88\u591a\u53d8\u79cd\uff0c\u4f8b\u5982 0-1 \u80cc\u5305\u95ee\u9898\u3001\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u3001\u591a\u91cd\u80cc\u5305\u95ee\u9898\u7b49\u3002

        \u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5148\u6765\u6c42\u89e3\u6700\u5e38\u89c1\u7684 0-1 \u80cc\u5305\u95ee\u9898\u3002

        Question

        \u7ed9\u5b9a \\(n\\) \u4e2a\u7269\u54c1\uff0c\u7b2c \\(i\\) \u4e2a\u7269\u54c1\u7684\u91cd\u91cf\u4e3a \\(wgt[i-1]\\)\u3001\u4ef7\u503c\u4e3a \\(val[i-1]\\) \uff0c\u548c\u4e00\u4e2a\u5bb9\u91cf\u4e3a \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u4e2a\u7269\u54c1\u53ea\u80fd\u9009\u62e9\u4e00\u6b21\uff0c\u95ee\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u80fd\u653e\u5165\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u3002

        \u89c2\u5bdf\u56fe 14-17 \uff0c\u7531\u4e8e\u7269\u54c1\u7f16\u53f7 \\(i\\) \u4ece \\(1\\) \u5f00\u59cb\u8ba1\u6570\uff0c\u6570\u7ec4\u7d22\u5f15\u4ece \\(0\\) \u5f00\u59cb\u8ba1\u6570\uff0c\u56e0\u6b64\u7269\u54c1 \\(i\\) \u5bf9\u5e94\u91cd\u91cf \\(wgt[i-1]\\) \u548c\u4ef7\u503c \\(val[i-1]\\) \u3002

        \u56fe 14-17 \u00a0 0-1 \u80cc\u5305\u7684\u793a\u4f8b\u6570\u636e

        \u6211\u4eec\u53ef\u4ee5\u5c06 0-1 \u80cc\u5305\u95ee\u9898\u770b\u4f5c\u4e00\u4e2a\u7531 \\(n\\) \u8f6e\u51b3\u7b56\u7ec4\u6210\u7684\u8fc7\u7a0b\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u7269\u4f53\u90fd\u6709\u4e0d\u653e\u5165\u548c\u653e\u5165\u4e24\u79cd\u51b3\u7b56\uff0c\u56e0\u6b64\u8be5\u95ee\u9898\u6ee1\u8db3\u51b3\u7b56\u6811\u6a21\u578b\u3002

        \u8be5\u95ee\u9898\u7684\u76ee\u6807\u662f\u6c42\u89e3\u201c\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u80fd\u653e\u5165\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u201d\uff0c\u56e0\u6b64\u8f83\u5927\u6982\u7387\u662f\u4e00\u4e2a\u52a8\u6001\u89c4\u5212\u95ee\u9898\u3002

        \u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868

        \u5bf9\u4e8e\u6bcf\u4e2a\u7269\u54c1\u6765\u8bf4\uff0c\u4e0d\u653e\u5165\u80cc\u5305\uff0c\u80cc\u5305\u5bb9\u91cf\u4e0d\u53d8\uff1b\u653e\u5165\u80cc\u5305\uff0c\u80cc\u5305\u5bb9\u91cf\u51cf\u5c0f\u3002\u7531\u6b64\u53ef\u5f97\u72b6\u6001\u5b9a\u4e49\uff1a\u5f53\u524d\u7269\u54c1\u7f16\u53f7 \\(i\\) \u548c\u80cc\u5305\u5bb9\u91cf \\(c\\) \uff0c\u8bb0\u4e3a \\([i, c]\\) \u3002

        \u72b6\u6001 \\([i, c]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u4e3a\uff1a\u524d \\(i\\) \u4e2a\u7269\u54c1\u5728\u5bb9\u91cf\u4e3a \\(c\\) \u7684\u80cc\u5305\u4e2d\u7684\u6700\u5927\u4ef7\u503c\uff0c\u8bb0\u4e3a \\(dp[i, c]\\) \u3002

        \u5f85\u6c42\u89e3\u7684\u662f \\(dp[n, cap]\\) \uff0c\u56e0\u6b64\u9700\u8981\u4e00\u4e2a\u5c3a\u5bf8\u4e3a \\((n+1) \\times (cap+1)\\) \u7684\u4e8c\u7ef4 \\(dp\\) \u8868\u3002

        \u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b

        \u5f53\u6211\u4eec\u505a\u51fa\u7269\u54c1 \\(i\\) \u7684\u51b3\u7b56\u540e\uff0c\u5269\u4f59\u7684\u662f\u524d \\(i-1\\) \u4e2a\u7269\u54c1\u51b3\u7b56\u7684\u5b50\u95ee\u9898\uff0c\u53ef\u5206\u4e3a\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\u3002

        • \u4e0d\u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u80cc\u5305\u5bb9\u91cf\u4e0d\u53d8\uff0c\u72b6\u6001\u53d8\u5316\u4e3a \\([i-1, c]\\) \u3002
        • \u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u80cc\u5305\u5bb9\u91cf\u51cf\u5c11 \\(wgt[i-1]\\) \uff0c\u4ef7\u503c\u589e\u52a0 \\(val[i-1]\\) \uff0c\u72b6\u6001\u53d8\u5316\u4e3a \\([i-1, c-wgt[i-1]]\\) \u3002

        \u4e0a\u8ff0\u5206\u6790\u5411\u6211\u4eec\u63ed\u793a\u4e86\u672c\u9898\u7684\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\u6700\u5927\u4ef7\u503c \\(dp[i, c]\\) \u7b49\u4e8e\u4e0d\u653e\u5165\u7269\u54c1 \\(i\\) \u548c\u653e\u5165\u7269\u54c1 \\(i\\) \u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\u3002\u7531\u6b64\u53ef\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff1a

        \\[ dp[i, c] = \\max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1]) \\]

        \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u82e5\u5f53\u524d\u7269\u54c1\u91cd\u91cf \\(wgt[i - 1]\\) \u8d85\u51fa\u5269\u4f59\u80cc\u5305\u5bb9\u91cf \\(c\\) \uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\u3002

        \u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f

        \u5f53\u65e0\u7269\u54c1\u6216\u80cc\u5305\u5bb9\u91cf\u4e3a \\(0\\) \u65f6\u6700\u5927\u4ef7\u503c\u4e3a \\(0\\) \uff0c\u5373\u9996\u5217 \\(dp[i, 0]\\) \u548c\u9996\u884c \\(dp[0, c]\\) \u90fd\u7b49\u4e8e \\(0\\) \u3002

        \u5f53\u524d\u72b6\u6001 \\([i, c]\\) \u4ece\u4e0a\u65b9\u7684\u72b6\u6001 \\([i-1, c]\\) \u548c\u5de6\u4e0a\u65b9\u7684\u72b6\u6001 \\([i-1, c-wgt[i-1]]\\) \u8f6c\u79fb\u800c\u6765\uff0c\u56e0\u6b64\u901a\u8fc7\u4e24\u5c42\u5faa\u73af\u6b63\u5e8f\u904d\u5386\u6574\u4e2a \\(dp\\) \u8868\u5373\u53ef\u3002

        \u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u6211\u4eec\u63a5\u4e0b\u6765\u6309\u987a\u5e8f\u5b9e\u73b0\u66b4\u529b\u641c\u7d22\u3001\u8bb0\u5fc6\u5316\u641c\u7d22\u3001\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u3002

        "},{"location":"chapter_dynamic_programming/knapsack_problem/#1","title":"1. \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u7d22","text":"

        \u641c\u7d22\u4ee3\u7801\u5305\u542b\u4ee5\u4e0b\u8981\u7d20\u3002

        • \u9012\u5f52\u53c2\u6570\uff1a\u72b6\u6001 \\([i, c]\\) \u3002
        • \u8fd4\u56de\u503c\uff1a\u5b50\u95ee\u9898\u7684\u89e3 \\(dp[i, c]\\) \u3002
        • \u7ec8\u6b62\u6761\u4ef6\uff1a\u5f53\u7269\u54c1\u7f16\u53f7\u8d8a\u754c \\(i = 0\\) \u6216\u80cc\u5305\u5269\u4f59\u5bb9\u91cf\u4e3a \\(0\\) \u65f6\uff0c\u7ec8\u6b62\u9012\u5f52\u5e76\u8fd4\u56de\u4ef7\u503c \\(0\\) \u3002
        • \u526a\u679d\uff1a\u82e5\u5f53\u524d\u7269\u54c1\u91cd\u91cf\u8d85\u51fa\u80cc\u5305\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
        def knapsack_dfs(wgt: list[int], val: list[int], i: int, c: int) -> int:\n    \"\"\"0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22\"\"\"\n    # \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if i == 0 or c == 0:\n        return 0\n    # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if wgt[i - 1] > c:\n        return knapsack_dfs(wgt, val, i - 1, c)\n    # \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    no = knapsack_dfs(wgt, val, i - 1, c)\n    yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]\n    # \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    return max(no, yes)\n
        knapsack.cpp
        /* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(vector<int> &wgt, vector<int> &val, int i, int c) {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    int no = knapsackDFS(wgt, val, i - 1, c);\n    int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    return max(no, yes);\n}\n
        knapsack.java
        /* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(int[] wgt, int[] val, int i, int c) {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    int no = knapsackDFS(wgt, val, i - 1, c);\n    int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    return Math.max(no, yes);\n}\n
        knapsack.cs
        /* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint KnapsackDFS(int[] weight, int[] val, int i, int c) {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (weight[i - 1] > c) {\n        return KnapsackDFS(weight, val, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    int no = KnapsackDFS(weight, val, i - 1, c);\n    int yes = KnapsackDFS(weight, val, i - 1, c - weight[i - 1]) + val[i - 1];\n    // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    return Math.Max(no, yes);\n}\n
        knapsack.go
        /* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc knapsackDFS(wgt, val []int, i, c int) int {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if i == 0 || c == 0 {\n        return 0\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if wgt[i-1] > c {\n        return knapsackDFS(wgt, val, i-1, c)\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    no := knapsackDFS(wgt, val, i-1, c)\n    yes := knapsackDFS(wgt, val, i-1, c-wgt[i-1]) + val[i-1]\n    // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    return int(math.Max(float64(no), float64(yes)))\n}\n
        knapsack.swift
        /* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc knapsackDFS(wgt: [Int], val: [Int], i: Int, c: Int) -> Int {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if i == 0 || c == 0 {\n        return 0\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if wgt[i - 1] > c {\n        return knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    let no = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)\n    let yes = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]\n    // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    return max(no, yes)\n}\n
        knapsack.js
        /* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction knapsackDFS(wgt, val, i, c) {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i === 0 || c === 0) {\n        return 0;\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    const no = knapsackDFS(wgt, val, i - 1, c);\n    const yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    return Math.max(no, yes);\n}\n
        knapsack.ts
        /* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction knapsackDFS(\n    wgt: Array<number>,\n    val: Array<number>,\n    i: number,\n    c: number\n): number {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i === 0 || c === 0) {\n        return 0;\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    const no = knapsackDFS(wgt, val, i - 1, c);\n    const yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    return Math.max(no, yes);\n}\n
        knapsack.dart
        /* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(List<int> wgt, List<int> val, int i, int c) {\n  // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n  if (i == 0 || c == 0) {\n    return 0;\n  }\n  // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n  if (wgt[i - 1] > c) {\n    return knapsackDFS(wgt, val, i - 1, c);\n  }\n  // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n  int no = knapsackDFS(wgt, val, i - 1, c);\n  int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n  // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n  return max(no, yes);\n}\n
        knapsack.rs
        /* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfn knapsack_dfs(wgt: &[i32], val: &[i32], i: usize, c: usize) -> i32 {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if i == 0 || c == 0 {\n        return 0;\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if wgt[i - 1] > c as i32 {\n        return knapsack_dfs(wgt, val, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    let no = knapsack_dfs(wgt, val, i - 1, c);\n    let yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1] as usize) + val[i - 1];\n    // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    std::cmp::max(no, yes)\n}\n
        knapsack.c
        /* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(int wgt[], int val[], int i, int c) {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    int no = knapsackDFS(wgt, val, i - 1, c);\n    int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    return myMax(no, yes);\n}\n
        knapsack.kt
        /* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfun knapsackDFS(\n    wgt: IntArray,\n    _val: IntArray,\n    i: Int,\n    c: Int\n): Int {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i == 0 || c == 0) {\n        return 0\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, _val, i - 1, c)\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    val no = knapsackDFS(wgt, _val, i - 1, c)\n    val yes = knapsackDFS(wgt, _val, i - 1, c - wgt[i - 1]) + _val[i - 1]\n    // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    return max(no, yes)\n}\n
        knapsack.rb
        ### 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 ###\ndef knapsack_dfs(wgt, val, i, c)\n  # \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n  return 0 if i == 0 || c == 0\n  # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n  return knapsack_dfs(wgt, val, i - 1, c) if wgt[i - 1] > c\n  # \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n  no = knapsack_dfs(wgt, val, i - 1, c)\n  yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]\n  # \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n  [no, yes].max\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5982\u56fe 14-18 \u6240\u793a\uff0c\u7531\u4e8e\u6bcf\u4e2a\u7269\u54c1\u90fd\u4f1a\u4ea7\u751f\u4e0d\u9009\u548c\u9009\u4e24\u6761\u641c\u7d22\u5206\u652f\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002

        \u89c2\u5bdf\u9012\u5f52\u6811\uff0c\u5bb9\u6613\u53d1\u73b0\u5176\u4e2d\u5b58\u5728\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u4f8b\u5982 \\(dp[1, 10]\\) \u7b49\u3002\u800c\u5f53\u7269\u54c1\u8f83\u591a\u3001\u80cc\u5305\u5bb9\u91cf\u8f83\u5927\uff0c\u5c24\u5176\u662f\u76f8\u540c\u91cd\u91cf\u7684\u7269\u54c1\u8f83\u591a\u65f6\uff0c\u91cd\u53e0\u5b50\u95ee\u9898\u7684\u6570\u91cf\u5c06\u4f1a\u5927\u5e45\u589e\u591a\u3002

        \u56fe 14-18 \u00a0 0-1 \u80cc\u5305\u95ee\u9898\u7684\u66b4\u529b\u641c\u7d22\u9012\u5f52\u6811

        "},{"location":"chapter_dynamic_programming/knapsack_problem/#2","title":"2. \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22","text":"

        \u4e3a\u4e86\u4fdd\u8bc1\u91cd\u53e0\u5b50\u95ee\u9898\u53ea\u88ab\u8ba1\u7b97\u4e00\u6b21\uff0c\u6211\u4eec\u501f\u52a9\u8bb0\u5fc6\u5217\u8868 mem \u6765\u8bb0\u5f55\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5176\u4e2d mem[i][c] \u5bf9\u5e94 \\(dp[i, c]\\) \u3002

        \u5f15\u5165\u8bb0\u5fc6\u5316\u4e4b\u540e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u5b50\u95ee\u9898\u6570\u91cf\uff0c\u4e5f\u5c31\u662f \\(O(n \\times cap)\\) \u3002\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
        def knapsack_dfs_mem(\n    wgt: list[int], val: list[int], mem: list[list[int]], i: int, c: int\n) -> int:\n    \"\"\"0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n    # \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if i == 0 or c == 0:\n        return 0\n    # \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if mem[i][c] != -1:\n        return mem[i][c]\n    # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if wgt[i - 1] > c:\n        return knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n    # \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    no = knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n    yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1]\n    # \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    mem[i][c] = max(no, yes)\n    return mem[i][c]\n
        knapsack.cpp
        /* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(vector<int> &wgt, vector<int> &val, vector<vector<int>> &mem, int i, int c) {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][c] != -1) {\n        return mem[i][c];\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, mem, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n    int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    mem[i][c] = max(no, yes);\n    return mem[i][c];\n}\n
        knapsack.java
        /* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(int[] wgt, int[] val, int[][] mem, int i, int c) {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][c] != -1) {\n        return mem[i][c];\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, mem, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n    int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    mem[i][c] = Math.max(no, yes);\n    return mem[i][c];\n}\n
        knapsack.cs
        /* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint KnapsackDFSMem(int[] weight, int[] val, int[][] mem, int i, int c) {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][c] != -1) {\n        return mem[i][c];\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (weight[i - 1] > c) {\n        return KnapsackDFSMem(weight, val, mem, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    int no = KnapsackDFSMem(weight, val, mem, i - 1, c);\n    int yes = KnapsackDFSMem(weight, val, mem, i - 1, c - weight[i - 1]) + val[i - 1];\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    mem[i][c] = Math.Max(no, yes);\n    return mem[i][c];\n}\n
        knapsack.go
        /* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc knapsackDFSMem(wgt, val []int, mem [][]int, i, c int) int {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if i == 0 || c == 0 {\n        return 0\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if mem[i][c] != -1 {\n        return mem[i][c]\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if wgt[i-1] > c {\n        return knapsackDFSMem(wgt, val, mem, i-1, c)\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    no := knapsackDFSMem(wgt, val, mem, i-1, c)\n    yes := knapsackDFSMem(wgt, val, mem, i-1, c-wgt[i-1]) + val[i-1]\n    // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    mem[i][c] = int(math.Max(float64(no), float64(yes)))\n    return mem[i][c]\n}\n
        knapsack.swift
        /* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc knapsackDFSMem(wgt: [Int], val: [Int], mem: inout [[Int]], i: Int, c: Int) -> Int {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if i == 0 || c == 0 {\n        return 0\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if mem[i][c] != -1 {\n        return mem[i][c]\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if wgt[i - 1] > c {\n        return knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c)\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    let no = knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c)\n    let yes = knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    mem[i][c] = max(no, yes)\n    return mem[i][c]\n}\n
        knapsack.js
        /* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction knapsackDFSMem(wgt, val, mem, i, c) {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i === 0 || c === 0) {\n        return 0;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][c] !== -1) {\n        return mem[i][c];\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, mem, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    const no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n    const yes =\n        knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    mem[i][c] = Math.max(no, yes);\n    return mem[i][c];\n}\n
        knapsack.ts
        /* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction knapsackDFSMem(\n    wgt: Array<number>,\n    val: Array<number>,\n    mem: Array<Array<number>>,\n    i: number,\n    c: number\n): number {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i === 0 || c === 0) {\n        return 0;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][c] !== -1) {\n        return mem[i][c];\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, mem, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    const no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n    const yes =\n        knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    mem[i][c] = Math.max(no, yes);\n    return mem[i][c];\n}\n
        knapsack.dart
        /* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(\n  List<int> wgt,\n  List<int> val,\n  List<List<int>> mem,\n  int i,\n  int c,\n) {\n  // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n  if (i == 0 || c == 0) {\n    return 0;\n  }\n  // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n  if (mem[i][c] != -1) {\n    return mem[i][c];\n  }\n  // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n  if (wgt[i - 1] > c) {\n    return knapsackDFSMem(wgt, val, mem, i - 1, c);\n  }\n  // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n  int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n  int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n  // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n  mem[i][c] = max(no, yes);\n  return mem[i][c];\n}\n
        knapsack.rs
        /* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn knapsack_dfs_mem(wgt: &[i32], val: &[i32], mem: &mut Vec<Vec<i32>>, i: usize, c: usize) -> i32 {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if i == 0 || c == 0 {\n        return 0;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if mem[i][c] != -1 {\n        return mem[i][c];\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if wgt[i - 1] > c as i32 {\n        return knapsack_dfs_mem(wgt, val, mem, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    let no = knapsack_dfs_mem(wgt, val, mem, i - 1, c);\n    let yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1] as usize) + val[i - 1];\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    mem[i][c] = std::cmp::max(no, yes);\n    mem[i][c]\n}\n
        knapsack.c
        /* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(int wgt[], int val[], int memCols, int **mem, int i, int c) {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][c] != -1) {\n        return mem[i][c];\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, memCols, mem, i - 1, c);\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    int no = knapsackDFSMem(wgt, val, memCols, mem, i - 1, c);\n    int yes = knapsackDFSMem(wgt, val, memCols, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    mem[i][c] = myMax(no, yes);\n    return mem[i][c];\n}\n
        knapsack.kt
        /* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfun knapsackDFSMem(\n    wgt: IntArray,\n    _val: IntArray,\n    mem: Array<IntArray>,\n    i: Int,\n    c: Int\n): Int {\n    // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n    if (i == 0 || c == 0) {\n        return 0\n    }\n    // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (mem[i][c] != -1) {\n        return mem[i][c]\n    }\n    // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, _val, mem, i - 1, c)\n    }\n    // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n    val no = knapsackDFSMem(wgt, _val, mem, i - 1, c)\n    val yes = knapsackDFSMem(wgt, _val, mem, i - 1, c - wgt[i - 1]) + _val[i - 1]\n    // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n    mem[i][c] = max(no, yes)\n    return mem[i][c]\n}\n
        knapsack.rb
        ### 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 ###\ndef knapsack_dfs_mem(wgt, val, mem, i, c)\n  # \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n  return 0 if i == 0 || c == 0\n  # \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n  return mem[i][c] if mem[i][c] != -1\n  # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n  return knapsack_dfs_mem(wgt, val, mem, i - 1, c) if wgt[i - 1] > c\n  # \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n  no = knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n  yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1]\n  # \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n  mem[i][c] = [no, yes].max\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 14-19 \u5c55\u793a\u4e86\u5728\u8bb0\u5fc6\u5316\u641c\u7d22\u4e2d\u88ab\u526a\u6389\u7684\u641c\u7d22\u5206\u652f\u3002

        \u56fe 14-19 \u00a0 0-1 \u80cc\u5305\u95ee\u9898\u7684\u8bb0\u5fc6\u5316\u641c\u7d22\u9012\u5f52\u6811

        "},{"location":"chapter_dynamic_programming/knapsack_problem/#3","title":"3. \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52a8\u6001\u89c4\u5212","text":"

        \u52a8\u6001\u89c4\u5212\u5b9e\u8d28\u4e0a\u5c31\u662f\u5728\u72b6\u6001\u8f6c\u79fb\u4e2d\u586b\u5145 \\(dp\\) \u8868\u7684\u8fc7\u7a0b\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
        def knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n    n = len(wgt)\n    # \u521d\u59cb\u5316 dp \u8868\n    dp = [[0] * (cap + 1) for _ in range(n + 1)]\n    # \u72b6\u6001\u8f6c\u79fb\n    for i in range(1, n + 1):\n        for c in range(1, cap + 1):\n            if wgt[i - 1] > c:\n                # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c]\n            else:\n                # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\n    return dp[n][cap]\n
        knapsack.cpp
        /* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // \u521d\u59cb\u5316 dp \u8868\n    vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        knapsack.java
        /* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[][] dp = new int[n + 1][cap + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = Math.max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        knapsack.cs
        /* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint KnapsackDP(int[] weight, int[] val, int cap) {\n    int n = weight.Length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[,] dp = new int[n + 1, cap + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (weight[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i, c] = dp[i - 1, c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i, c] = Math.Max(dp[i - 1, c - weight[i - 1]] + val[i - 1], dp[i - 1, c]);\n            }\n        }\n    }\n    return dp[n, cap];\n}\n
        knapsack.go
        /* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDP(wgt, val []int, cap int) int {\n    n := len(wgt)\n    // \u521d\u59cb\u5316 dp \u8868\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, cap+1)\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for i := 1; i <= n; i++ {\n        for c := 1; c <= cap; c++ {\n            if wgt[i-1] > c {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i-1][c]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i-1][c-wgt[i-1]]+val[i-1])))\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
        knapsack.swift
        /* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDP(wgt: [Int], val: [Int], cap: Int) -> Int {\n    let n = wgt.count\n    // \u521d\u59cb\u5316 dp \u8868\n    var dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1 ... n {\n        for c in 1 ... cap {\n            if wgt[i - 1] > c {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
        knapsack.js
        /* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDP(wgt, val, cap) {\n    const n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array(n + 1)\n        .fill(0)\n        .map(() => Array(cap + 1).fill(0));\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = Math.max(\n                    dp[i - 1][c],\n                    dp[i - 1][c - wgt[i - 1]] + val[i - 1]\n                );\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        knapsack.ts
        /* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDP(\n    wgt: Array<number>,\n    val: Array<number>,\n    cap: number\n): number {\n    const n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: cap + 1 }, () => 0)\n    );\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = Math.max(\n                    dp[i - 1][c],\n                    dp[i - 1][c - wgt[i - 1]] + val[i - 1]\n                );\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        knapsack.dart
        /* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(List<int> wgt, List<int> val, int cap) {\n  int n = wgt.length;\n  // \u521d\u59cb\u5316 dp \u8868\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(cap + 1, 0));\n  // \u72b6\u6001\u8f6c\u79fb\n  for (int i = 1; i <= n; i++) {\n    for (int c = 1; c <= cap; c++) {\n      if (wgt[i - 1] > c) {\n        // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n        dp[i][c] = dp[i - 1][c];\n      } else {\n        // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n        dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n      }\n    }\n  }\n  return dp[n][cap];\n}\n
        knapsack.rs
        /* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfn knapsack_dp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n    let n = wgt.len();\n    // \u521d\u59cb\u5316 dp \u8868\n    let mut dp = vec![vec![0; cap + 1]; n + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1..=n {\n        for c in 1..=cap {\n            if wgt[i - 1] > c as i32 {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = std::cmp::max(\n                    dp[i - 1][c],\n                    dp[i - 1][c - wgt[i - 1] as usize] + val[i - 1],\n                );\n            }\n        }\n    }\n    dp[n][cap]\n}\n
        knapsack.c
        /* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(int wgt[], int val[], int cap, int wgtSize) {\n    int n = wgtSize;\n    // \u521d\u59cb\u5316 dp \u8868\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(cap + 1, sizeof(int));\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = myMax(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    int res = dp[n][cap];\n    // \u91ca\u653e\u5185\u5b58\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    return res;\n}\n
        knapsack.kt
        /* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfun knapsackDP(wgt: IntArray, _val: IntArray, cap: Int): Int {\n    val n = wgt.size\n    // \u521d\u59cb\u5316 dp \u8868\n    val dp = Array(n + 1) { IntArray(cap + 1) }\n    // \u72b6\u6001\u8f6c\u79fb\n    for (i in 1..n) {\n        for (c in 1..cap) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + _val[i - 1])\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
        knapsack.rb
        ### 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 ###\ndef knapsack_dp(wgt, val, cap)\n  n = wgt.length\n  # \u521d\u59cb\u5316 dp \u8868\n  dp = Array.new(n + 1) { Array.new(cap + 1, 0) }\n  # \u72b6\u6001\u8f6c\u79fb\n  for i in 1...(n + 1)\n    for c in 1...(cap + 1)\n      if wgt[i - 1] > c\n        # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n        dp[i][c] = dp[i - 1][c]\n      else\n        # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n        dp[i][c] = [dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[n][cap]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5982\u56fe 14-20 \u6240\u793a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u90fd\u7531\u6570\u7ec4 dp \u5927\u5c0f\u51b3\u5b9a\uff0c\u5373 \\(O(n \\times cap)\\) \u3002

        <1><2><3><4><5><6><7><8><9><10><11><12><13><14>

        \u56fe 14-20 \u00a0 0-1 \u80cc\u5305\u95ee\u9898\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

        "},{"location":"chapter_dynamic_programming/knapsack_problem/#4","title":"4. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

        \u7531\u4e8e\u6bcf\u4e2a\u72b6\u6001\u90fd\u53ea\u4e0e\u5176\u4e0a\u4e00\u884c\u7684\u72b6\u6001\u6709\u5173\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e24\u4e2a\u6570\u7ec4\u6eda\u52a8\u524d\u8fdb\uff0c\u5c06\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n^2)\\) \u964d\u81f3 \\(O(n)\\) \u3002

        \u8fdb\u4e00\u6b65\u601d\u8003\uff0c\u6211\u4eec\u80fd\u5426\u4ec5\u7528\u4e00\u4e2a\u6570\u7ec4\u5b9e\u73b0\u7a7a\u95f4\u4f18\u5316\u5462\uff1f\u89c2\u5bdf\u53ef\u77e5\uff0c\u6bcf\u4e2a\u72b6\u6001\u90fd\u662f\u7531\u6b63\u4e0a\u65b9\u6216\u5de6\u4e0a\u65b9\u7684\u683c\u5b50\u8f6c\u79fb\u8fc7\u6765\u7684\u3002\u5047\u8bbe\u53ea\u6709\u4e00\u4e2a\u6570\u7ec4\uff0c\u5f53\u5f00\u59cb\u904d\u5386\u7b2c \\(i\\) \u884c\u65f6\uff0c\u8be5\u6570\u7ec4\u5b58\u50a8\u7684\u4ecd\u7136\u662f\u7b2c \\(i-1\\) \u884c\u7684\u72b6\u6001\u3002

        • \u5982\u679c\u91c7\u53d6\u6b63\u5e8f\u904d\u5386\uff0c\u90a3\u4e48\u904d\u5386\u5230 \\(dp[i, j]\\) \u65f6\uff0c\u5de6\u4e0a\u65b9 \\(dp[i-1, 1]\\) ~ \\(dp[i-1, j-1]\\) \u503c\u53ef\u80fd\u5df2\u7ecf\u88ab\u8986\u76d6\uff0c\u6b64\u65f6\u5c31\u65e0\u6cd5\u5f97\u5230\u6b63\u786e\u7684\u72b6\u6001\u8f6c\u79fb\u7ed3\u679c\u3002
        • \u5982\u679c\u91c7\u53d6\u5012\u5e8f\u904d\u5386\uff0c\u5219\u4e0d\u4f1a\u53d1\u751f\u8986\u76d6\u95ee\u9898\uff0c\u72b6\u6001\u8f6c\u79fb\u53ef\u4ee5\u6b63\u786e\u8fdb\u884c\u3002

        \u56fe 14-21 \u5c55\u793a\u4e86\u5728\u5355\u4e2a\u6570\u7ec4\u4e0b\u4ece\u7b2c \\(i = 1\\) \u884c\u8f6c\u6362\u81f3\u7b2c \\(i = 2\\) \u884c\u7684\u8fc7\u7a0b\u3002\u8bf7\u601d\u8003\u6b63\u5e8f\u904d\u5386\u548c\u5012\u5e8f\u904d\u5386\u7684\u533a\u522b\u3002

        <1><2><3><4><5><6>

        \u56fe 14-21 \u00a0 0-1 \u80cc\u5305\u7684\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

        \u5728\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u4ec5\u9700\u5c06\u6570\u7ec4 dp \u7684\u7b2c\u4e00\u7ef4 \\(i\\) \u76f4\u63a5\u5220\u9664\uff0c\u5e76\u4e14\u628a\u5185\u5faa\u73af\u66f4\u6539\u4e3a\u5012\u5e8f\u904d\u5386\u5373\u53ef\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
        def knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n    n = len(wgt)\n    # \u521d\u59cb\u5316 dp \u8868\n    dp = [0] * (cap + 1)\n    # \u72b6\u6001\u8f6c\u79fb\n    for i in range(1, n + 1):\n        # \u5012\u5e8f\u904d\u5386\n        for c in range(cap, 0, -1):\n            if wgt[i - 1] > c:\n                # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c]\n            else:\n                # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n    return dp[cap]\n
        knapsack.cpp
        /* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // \u521d\u59cb\u5316 dp \u8868\n    vector<int> dp(cap + 1, 0);\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        // \u5012\u5e8f\u904d\u5386\n        for (int c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        knapsack.java
        /* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[] dp = new int[cap + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        // \u5012\u5e8f\u904d\u5386\n        for (int c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        knapsack.cs
        /* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint KnapsackDPComp(int[] weight, int[] val, int cap) {\n    int n = weight.Length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[] dp = new int[cap + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        // \u5012\u5e8f\u904d\u5386\n        for (int c = cap; c > 0; c--) {\n            if (weight[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = Math.Max(dp[c], dp[c - weight[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        knapsack.go
        /* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDPComp(wgt, val []int, cap int) int {\n    n := len(wgt)\n    // \u521d\u59cb\u5316 dp \u8868\n    dp := make([]int, cap+1)\n    // \u72b6\u6001\u8f6c\u79fb\n    for i := 1; i <= n; i++ {\n        // \u5012\u5e8f\u904d\u5386\n        for c := cap; c >= 1; c-- {\n            if wgt[i-1] <= c {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))\n            }\n        }\n    }\n    return dp[cap]\n}\n
        knapsack.swift
        /* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDPComp(wgt: [Int], val: [Int], cap: Int) -> Int {\n    let n = wgt.count\n    // \u521d\u59cb\u5316 dp \u8868\n    var dp = Array(repeating: 0, count: cap + 1)\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1 ... n {\n        // \u5012\u5e8f\u904d\u5386\n        for c in (1 ... cap).reversed() {\n            if wgt[i - 1] <= c {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n            }\n        }\n    }\n    return dp[cap]\n}\n
        knapsack.js
        /* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDPComp(wgt, val, cap) {\n    const n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array(cap + 1).fill(0);\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        // \u5012\u5e8f\u904d\u5386\n        for (let c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        knapsack.ts
        /* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDPComp(\n    wgt: Array<number>,\n    val: Array<number>,\n    cap: number\n): number {\n    const n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array(cap + 1).fill(0);\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        // \u5012\u5e8f\u904d\u5386\n        for (let c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        knapsack.dart
        /* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(List<int> wgt, List<int> val, int cap) {\n  int n = wgt.length;\n  // \u521d\u59cb\u5316 dp \u8868\n  List<int> dp = List.filled(cap + 1, 0);\n  // \u72b6\u6001\u8f6c\u79fb\n  for (int i = 1; i <= n; i++) {\n    // \u5012\u5e8f\u904d\u5386\n    for (int c = cap; c >= 1; c--) {\n      if (wgt[i - 1] <= c) {\n        // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n        dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n      }\n    }\n  }\n  return dp[cap];\n}\n
        knapsack.rs
        /* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn knapsack_dp_comp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n    let n = wgt.len();\n    // \u521d\u59cb\u5316 dp \u8868\n    let mut dp = vec![0; cap + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1..=n {\n        // \u5012\u5e8f\u904d\u5386\n        for c in (1..=cap).rev() {\n            if wgt[i - 1] <= c as i32 {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);\n            }\n        }\n    }\n    dp[cap]\n}\n
        knapsack.c
        /* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(int wgt[], int val[], int cap, int wgtSize) {\n    int n = wgtSize;\n    // \u521d\u59cb\u5316 dp \u8868\n    int *dp = calloc(cap + 1, sizeof(int));\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        // \u5012\u5e8f\u904d\u5386\n        for (int c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = myMax(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    int res = dp[cap];\n    // \u91ca\u653e\u5185\u5b58\n    free(dp);\n    return res;\n}\n
        knapsack.kt
        /* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfun knapsackDPComp(wgt: IntArray, _val: IntArray, cap: Int): Int {\n    val n = wgt.size\n    // \u521d\u59cb\u5316 dp \u8868\n    val dp = IntArray(cap + 1)\n    // \u72b6\u6001\u8f6c\u79fb\n    for (i in 1..n) {\n        // \u5012\u5e8f\u904d\u5386\n        for (c in cap downTo 1) {\n            if (wgt[i - 1] <= c) {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])\n            }\n        }\n    }\n    return dp[cap]\n}\n
        knapsack.rb
        ### 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 ###\ndef knapsack_dp_comp(wgt, val, cap)\n  n = wgt.length\n  # \u521d\u59cb\u5316 dp \u8868\n  dp = Array.new(cap + 1, 0)\n  # \u72b6\u6001\u8f6c\u79fb\n  for i in 1...(n + 1)\n    # \u5012\u5e8f\u904d\u5386\n    for c in cap.downto(1)\n      if wgt[i - 1] > c\n        # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n        dp[c] = dp[c]\n      else\n        # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n        dp[c] = [dp[c], dp[c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[cap]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_dynamic_programming/summary/","title":"14.7 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_dynamic_programming/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u52a8\u6001\u89c4\u5212\u5bf9\u95ee\u9898\u8fdb\u884c\u5206\u89e3\uff0c\u5e76\u901a\u8fc7\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\u6765\u89c4\u907f\u91cd\u590d\u8ba1\u7b97\uff0c\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002
        • \u4e0d\u8003\u8651\u65f6\u95f4\u7684\u524d\u63d0\u4e0b\uff0c\u6240\u6709\u52a8\u6001\u89c4\u5212\u95ee\u9898\u90fd\u53ef\u4ee5\u7528\u56de\u6eaf\uff08\u66b4\u529b\u641c\u7d22\uff09\u8fdb\u884c\u6c42\u89e3\uff0c\u4f46\u9012\u5f52\u6811\u4e2d\u5b58\u5728\u5927\u91cf\u7684\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u6548\u7387\u6781\u4f4e\u3002\u901a\u8fc7\u5f15\u5165\u8bb0\u5fc6\u5316\u5217\u8868\uff0c\u53ef\u4ee5\u5b58\u50a8\u6240\u6709\u8ba1\u7b97\u8fc7\u7684\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u4ece\u800c\u4fdd\u8bc1\u91cd\u53e0\u5b50\u95ee\u9898\u53ea\u88ab\u8ba1\u7b97\u4e00\u6b21\u3002
        • \u8bb0\u5fc6\u5316\u641c\u7d22\u662f\u4e00\u79cd\u4ece\u9876\u81f3\u5e95\u7684\u9012\u5f52\u5f0f\u89e3\u6cd5\uff0c\u800c\u4e0e\u4e4b\u5bf9\u5e94\u7684\u52a8\u6001\u89c4\u5212\u662f\u4e00\u79cd\u4ece\u5e95\u81f3\u9876\u7684\u9012\u63a8\u5f0f\u89e3\u6cd5\uff0c\u5176\u5982\u540c\u201c\u586b\u5199\u8868\u683c\u201d\u4e00\u6837\u3002\u7531\u4e8e\u5f53\u524d\u72b6\u6001\u4ec5\u4f9d\u8d56\u67d0\u4e9b\u5c40\u90e8\u72b6\u6001\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u6d88\u9664 \\(dp\\) \u8868\u7684\u4e00\u4e2a\u7ef4\u5ea6\uff0c\u4ece\u800c\u964d\u4f4e\u7a7a\u95f4\u590d\u6742\u5ea6\u3002
        • \u5b50\u95ee\u9898\u5206\u89e3\u662f\u4e00\u79cd\u901a\u7528\u7684\u7b97\u6cd5\u601d\u8def\uff0c\u5728\u5206\u6cbb\u3001\u52a8\u6001\u89c4\u5212\u3001\u56de\u6eaf\u4e2d\u5177\u6709\u4e0d\u540c\u7684\u6027\u8d28\u3002
        • \u52a8\u6001\u89c4\u5212\u95ee\u9898\u6709\u4e09\u5927\u7279\u6027\uff1a\u91cd\u53e0\u5b50\u95ee\u9898\u3001\u6700\u4f18\u5b50\u7ed3\u6784\u3001\u65e0\u540e\u6548\u6027\u3002
        • \u5982\u679c\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u53ef\u4ee5\u4ece\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u6784\u5efa\u5f97\u6765\uff0c\u5219\u5b83\u5c31\u5177\u6709\u6700\u4f18\u5b50\u7ed3\u6784\u3002
        • \u65e0\u540e\u6548\u6027\u6307\u5bf9\u4e8e\u4e00\u4e2a\u72b6\u6001\uff0c\u5176\u672a\u6765\u53d1\u5c55\u53ea\u4e0e\u8be5\u72b6\u6001\u6709\u5173\uff0c\u800c\u4e0e\u8fc7\u53bb\u7ecf\u5386\u7684\u6240\u6709\u72b6\u6001\u65e0\u5173\u3002\u8bb8\u591a\u7ec4\u5408\u4f18\u5316\u95ee\u9898\u4e0d\u5177\u6709\u65e0\u540e\u6548\u6027\uff0c\u65e0\u6cd5\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u5feb\u901f\u6c42\u89e3\u3002

        \u80cc\u5305\u95ee\u9898

        • \u80cc\u5305\u95ee\u9898\u662f\u6700\u5178\u578b\u7684\u52a8\u6001\u89c4\u5212\u95ee\u9898\u4e4b\u4e00\uff0c\u5177\u6709 0-1 \u80cc\u5305\u3001\u5b8c\u5168\u80cc\u5305\u3001\u591a\u91cd\u80cc\u5305\u7b49\u53d8\u79cd\u3002
        • 0-1 \u80cc\u5305\u7684\u72b6\u6001\u5b9a\u4e49\u4e3a\u524d \\(i\\) \u4e2a\u7269\u54c1\u5728\u5bb9\u91cf\u4e3a \\(c\\) \u7684\u80cc\u5305\u4e2d\u7684\u6700\u5927\u4ef7\u503c\u3002\u6839\u636e\u4e0d\u653e\u5165\u80cc\u5305\u548c\u653e\u5165\u80cc\u5305\u4e24\u79cd\u51b3\u7b56\uff0c\u53ef\u5f97\u5230\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u5e76\u6784\u5efa\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u3002\u5728\u7a7a\u95f4\u4f18\u5316\u4e2d\uff0c\u7531\u4e8e\u6bcf\u4e2a\u72b6\u6001\u4f9d\u8d56\u6b63\u4e0a\u65b9\u548c\u5de6\u4e0a\u65b9\u7684\u72b6\u6001\uff0c\u56e0\u6b64\u9700\u8981\u5012\u5e8f\u904d\u5386\u5217\u8868\uff0c\u907f\u514d\u5de6\u4e0a\u65b9\u72b6\u6001\u88ab\u8986\u76d6\u3002
        • \u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u6bcf\u79cd\u7269\u54c1\u7684\u9009\u53d6\u6570\u91cf\u65e0\u9650\u5236\uff0c\u56e0\u6b64\u9009\u62e9\u653e\u5165\u7269\u54c1\u7684\u72b6\u6001\u8f6c\u79fb\u4e0e 0-1 \u80cc\u5305\u95ee\u9898\u4e0d\u540c\u3002\u7531\u4e8e\u72b6\u6001\u4f9d\u8d56\u6b63\u4e0a\u65b9\u548c\u6b63\u5de6\u65b9\u7684\u72b6\u6001\uff0c\u56e0\u6b64\u5728\u7a7a\u95f4\u4f18\u5316\u4e2d\u5e94\u5f53\u6b63\u5e8f\u904d\u5386\u3002
        • \u96f6\u94b1\u5151\u6362\u95ee\u9898\u662f\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u4e00\u4e2a\u53d8\u79cd\u3002\u5b83\u4ece\u6c42\u201c\u6700\u5927\u201d\u4ef7\u503c\u53d8\u4e3a\u6c42\u201c\u6700\u5c0f\u201d\u786c\u5e01\u6570\u91cf\uff0c\u56e0\u6b64\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e2d\u7684 \\(\\max()\\) \u5e94\u6539\u4e3a \\(\\min()\\) \u3002\u4ece\u8ffd\u6c42\u201c\u4e0d\u8d85\u8fc7\u201d\u80cc\u5305\u5bb9\u91cf\u5230\u8ffd\u6c42\u201c\u6070\u597d\u201d\u51d1\u51fa\u76ee\u6807\u91d1\u989d\uff0c\u56e0\u6b64\u4f7f\u7528 \\(amt + 1\\) \u6765\u8868\u793a\u201c\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u201d\u7684\u65e0\u6548\u89e3\u3002
        • \u96f6\u94b1\u5151\u6362\u95ee\u9898 II \u4ece\u6c42\u201c\u6700\u5c11\u786c\u5e01\u6570\u91cf\u201d\u6539\u4e3a\u6c42\u201c\u786c\u5e01\u7ec4\u5408\u6570\u91cf\u201d\uff0c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u76f8\u5e94\u5730\u4ece \\(\\min()\\) \u6539\u4e3a\u6c42\u548c\u8fd0\u7b97\u7b26\u3002

        \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898

        • \u7f16\u8f91\u8ddd\u79bb\uff08Levenshtein \u8ddd\u79bb\uff09\u7528\u4e8e\u8861\u91cf\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e4b\u95f4\u7684\u76f8\u4f3c\u5ea6\uff0c\u5176\u5b9a\u4e49\u4e3a\u4ece\u4e00\u4e2a\u5b57\u7b26\u4e32\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\uff0c\u7f16\u8f91\u64cd\u4f5c\u5305\u62ec\u6dfb\u52a0\u3001\u5220\u9664\u3001\u66ff\u6362\u3002
        • \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898\u7684\u72b6\u6001\u5b9a\u4e49\u4e3a\u5c06 \\(s\\) \u7684\u524d \\(i\\) \u4e2a\u5b57\u7b26\u66f4\u6539\u4e3a \\(t\\) \u7684\u524d \\(j\\) \u4e2a\u5b57\u7b26\u6240\u9700\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u3002\u5f53 \\(s[i] \\ne t[j]\\) \u65f6\uff0c\u5177\u6709\u4e09\u79cd\u51b3\u7b56\uff1a\u6dfb\u52a0\u3001\u5220\u9664\u3001\u66ff\u6362\uff0c\u5b83\u4eec\u90fd\u6709\u76f8\u5e94\u7684\u5269\u4f59\u5b50\u95ee\u9898\u3002\u636e\u6b64\u4fbf\u53ef\u4ee5\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\u4e0e\u6784\u5efa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u3002\u800c\u5f53 \\(s[i] = t[j]\\) \u65f6\uff0c\u65e0\u987b\u7f16\u8f91\u5f53\u524d\u5b57\u7b26\u3002
        • \u5728\u7f16\u8f91\u8ddd\u79bb\u4e2d\uff0c\u72b6\u6001\u4f9d\u8d56\u5176\u6b63\u4e0a\u65b9\u3001\u6b63\u5de6\u65b9\u3001\u5de6\u4e0a\u65b9\u7684\u72b6\u6001\uff0c\u56e0\u6b64\u7a7a\u95f4\u4f18\u5316\u540e\u6b63\u5e8f\u6216\u5012\u5e8f\u904d\u5386\u90fd\u65e0\u6cd5\u6b63\u786e\u5730\u8fdb\u884c\u72b6\u6001\u8f6c\u79fb\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5229\u7528\u4e00\u4e2a\u53d8\u91cf\u6682\u5b58\u5de6\u4e0a\u65b9\u72b6\u6001\uff0c\u4ece\u800c\u8f6c\u5316\u5230\u4e0e\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7b49\u4ef7\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u5728\u7a7a\u95f4\u4f18\u5316\u540e\u8fdb\u884c\u6b63\u5e8f\u904d\u5386\u3002
        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/","title":"14.5 \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898","text":"

        \u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5148\u6c42\u89e3\u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u80cc\u5305\u95ee\u9898\uff1a\u5b8c\u5168\u80cc\u5305\uff0c\u518d\u4e86\u89e3\u5b83\u7684\u4e00\u79cd\u7279\u4f8b\uff1a\u96f6\u94b1\u5151\u6362\u3002

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1451","title":"14.5.1 \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898","text":"

        Question

        \u7ed9\u5b9a \\(n\\) \u4e2a\u7269\u54c1\uff0c\u7b2c \\(i\\) \u4e2a\u7269\u54c1\u7684\u91cd\u91cf\u4e3a \\(wgt[i-1]\\)\u3001\u4ef7\u503c\u4e3a \\(val[i-1]\\) \uff0c\u548c\u4e00\u4e2a\u5bb9\u91cf\u4e3a \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u4e2a\u7269\u54c1\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u80fd\u653e\u5165\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u3002\u793a\u4f8b\u5982\u56fe 14-22 \u6240\u793a\u3002

        \u56fe 14-22 \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"

        \u5b8c\u5168\u80cc\u5305\u95ee\u9898\u548c 0-1 \u80cc\u5305\u95ee\u9898\u975e\u5e38\u76f8\u4f3c\uff0c\u533a\u522b\u4ec5\u5728\u4e8e\u4e0d\u9650\u5236\u7269\u54c1\u7684\u9009\u62e9\u6b21\u6570\u3002

        • \u5728 0-1 \u80cc\u5305\u95ee\u9898\u4e2d\uff0c\u6bcf\u79cd\u7269\u54c1\u53ea\u6709\u4e00\u4e2a\uff0c\u56e0\u6b64\u5c06\u7269\u54c1 \\(i\\) \u653e\u5165\u80cc\u5305\u540e\uff0c\u53ea\u80fd\u4ece\u524d \\(i-1\\) \u4e2a\u7269\u54c1\u4e2d\u9009\u62e9\u3002
        • \u5728\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u4e2d\uff0c\u6bcf\u79cd\u7269\u54c1\u7684\u6570\u91cf\u662f\u65e0\u9650\u7684\uff0c\u56e0\u6b64\u5c06\u7269\u54c1 \\(i\\) \u653e\u5165\u80cc\u5305\u540e\uff0c\u4ecd\u53ef\u4ee5\u4ece\u524d \\(i\\) \u4e2a\u7269\u54c1\u4e2d\u9009\u62e9\u3002

        \u5728\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u89c4\u5b9a\u4e0b\uff0c\u72b6\u6001 \\([i, c]\\) \u7684\u53d8\u5316\u5206\u4e3a\u4e24\u79cd\u60c5\u51b5\u3002

        • \u4e0d\u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u4e0e 0-1 \u80cc\u5305\u95ee\u9898\u76f8\u540c\uff0c\u8f6c\u79fb\u81f3 \\([i-1, c]\\) \u3002
        • \u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u4e0e 0-1 \u80cc\u5305\u95ee\u9898\u4e0d\u540c\uff0c\u8f6c\u79fb\u81f3 \\([i, c-wgt[i-1]]\\) \u3002

        \u4ece\u800c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u53d8\u4e3a\uff1a

        \\[ dp[i, c] = \\max(dp[i-1, c], dp[i, c - wgt[i-1]] + val[i-1]) \\]"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u5bf9\u6bd4\u4e24\u9053\u9898\u76ee\u7684\u4ee3\u7801\uff0c\u72b6\u6001\u8f6c\u79fb\u4e2d\u6709\u4e00\u5904\u4ece \\(i-1\\) \u53d8\u4e3a \\(i\\) \uff0c\u5176\u4f59\u5b8c\u5168\u4e00\u81f4\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby unbounded_knapsack.py
        def unbounded_knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"\u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n    n = len(wgt)\n    # \u521d\u59cb\u5316 dp \u8868\n    dp = [[0] * (cap + 1) for _ in range(n + 1)]\n    # \u72b6\u6001\u8f6c\u79fb\n    for i in range(1, n + 1):\n        for c in range(1, cap + 1):\n            if wgt[i - 1] > c:\n                # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c]\n            else:\n                # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\n    return dp[n][cap]\n
        unbounded_knapsack.cpp
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // \u521d\u59cb\u5316 dp \u8868\n    vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        unbounded_knapsack.java
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[][] dp = new int[n + 1][cap + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = Math.max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        unbounded_knapsack.cs
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint UnboundedKnapsackDP(int[] wgt, int[] val, int cap) {\n    int n = wgt.Length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[,] dp = new int[n + 1, cap + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i, c] = dp[i - 1, c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i, c] = Math.Max(dp[i - 1, c], dp[i, c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n, cap];\n}\n
        unbounded_knapsack.go
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDP(wgt, val []int, cap int) int {\n    n := len(wgt)\n    // \u521d\u59cb\u5316 dp \u8868\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, cap+1)\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for i := 1; i <= n; i++ {\n        for c := 1; c <= cap; c++ {\n            if wgt[i-1] > c {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i-1][c]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i][c-wgt[i-1]]+val[i-1])))\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
        unbounded_knapsack.swift
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDP(wgt: [Int], val: [Int], cap: Int) -> Int {\n    let n = wgt.count\n    // \u521d\u59cb\u5316 dp \u8868\n    var dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1 ... n {\n        for c in 1 ... cap {\n            if wgt[i - 1] > c {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
        unbounded_knapsack.js
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDP(wgt, val, cap) {\n    const n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: cap + 1 }, () => 0)\n    );\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = Math.max(\n                    dp[i - 1][c],\n                    dp[i][c - wgt[i - 1]] + val[i - 1]\n                );\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        unbounded_knapsack.ts
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDP(\n    wgt: Array<number>,\n    val: Array<number>,\n    cap: number\n): number {\n    const n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: cap + 1 }, () => 0)\n    );\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = Math.max(\n                    dp[i - 1][c],\n                    dp[i][c - wgt[i - 1]] + val[i - 1]\n                );\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        unbounded_knapsack.dart
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(List<int> wgt, List<int> val, int cap) {\n  int n = wgt.length;\n  // \u521d\u59cb\u5316 dp \u8868\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(cap + 1, 0));\n  // \u72b6\u6001\u8f6c\u79fb\n  for (int i = 1; i <= n; i++) {\n    for (int c = 1; c <= cap; c++) {\n      if (wgt[i - 1] > c) {\n        // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n        dp[i][c] = dp[i - 1][c];\n      } else {\n        // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n        dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n      }\n    }\n  }\n  return dp[n][cap];\n}\n
        unbounded_knapsack.rs
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfn unbounded_knapsack_dp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n    let n = wgt.len();\n    // \u521d\u59cb\u5316 dp \u8868\n    let mut dp = vec![vec![0; cap + 1]; n + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1..=n {\n        for c in 1..=cap {\n            if wgt[i - 1] > c as i32 {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = std::cmp::max(dp[i - 1][c], dp[i][c - wgt[i - 1] as usize] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
        unbounded_knapsack.c
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(int wgt[], int val[], int cap, int wgtSize) {\n    int n = wgtSize;\n    // \u521d\u59cb\u5316 dp \u8868\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(cap + 1, sizeof(int));\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = myMax(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    int res = dp[n][cap];\n    // \u91ca\u653e\u5185\u5b58\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    return res;\n}\n
        unbounded_knapsack.kt
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfun unboundedKnapsackDP(wgt: IntArray, _val: IntArray, cap: Int): Int {\n    val n = wgt.size\n    // \u521d\u59cb\u5316 dp \u8868\n    val dp = Array(n + 1) { IntArray(cap + 1) }\n    // \u72b6\u6001\u8f6c\u79fb\n    for (i in 1..n) {\n        for (c in 1..cap) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[i][c] = dp[i - 1][c]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + _val[i - 1])\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
        unbounded_knapsack.rb
        ### \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 ###\ndef unbounded_knapsack_dp(wgt, val, cap)\n  n = wgt.length\n  # \u521d\u59cb\u5316 dp \u8868\n  dp = Array.new(n + 1) { Array.new(cap + 1, 0) }\n  # \u72b6\u6001\u8f6c\u79fb\n  for i in 1...(n + 1)\n    for c in 1...(cap + 1)\n      if wgt[i - 1] > c\n        # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n        dp[i][c] = dp[i - 1][c]\n      else\n        # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n        dp[i][c] = [dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[n][cap]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

        \u7531\u4e8e\u5f53\u524d\u72b6\u6001\u662f\u4ece\u5de6\u8fb9\u548c\u4e0a\u8fb9\u7684\u72b6\u6001\u8f6c\u79fb\u800c\u6765\u7684\uff0c\u56e0\u6b64\u7a7a\u95f4\u4f18\u5316\u540e\u5e94\u8be5\u5bf9 \\(dp\\) \u8868\u4e2d\u7684\u6bcf\u4e00\u884c\u8fdb\u884c\u6b63\u5e8f\u904d\u5386\u3002

        \u8fd9\u4e2a\u904d\u5386\u987a\u5e8f\u4e0e 0-1 \u80cc\u5305\u6b63\u597d\u76f8\u53cd\u3002\u8bf7\u501f\u52a9\u56fe 14-23 \u6765\u7406\u89e3\u4e24\u8005\u7684\u533a\u522b\u3002

        <1><2><3><4><5><6>

        \u56fe 14-23 \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898\u5728\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

        \u4ee3\u7801\u5b9e\u73b0\u6bd4\u8f83\u7b80\u5355\uff0c\u4ec5\u9700\u5c06\u6570\u7ec4 dp \u7684\u7b2c\u4e00\u7ef4\u5220\u9664\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby unbounded_knapsack.py
        def unbounded_knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"\u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n    n = len(wgt)\n    # \u521d\u59cb\u5316 dp \u8868\n    dp = [0] * (cap + 1)\n    # \u72b6\u6001\u8f6c\u79fb\n    for i in range(1, n + 1):\n        # \u6b63\u5e8f\u904d\u5386\n        for c in range(1, cap + 1):\n            if wgt[i - 1] > c:\n                # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c]\n            else:\n                # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n    return dp[cap]\n
        unbounded_knapsack.cpp
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // \u521d\u59cb\u5316 dp \u8868\n    vector<int> dp(cap + 1, 0);\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        unbounded_knapsack.java
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[] dp = new int[cap + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        unbounded_knapsack.cs
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint UnboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\n    int n = wgt.Length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[] dp = new int[cap + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = Math.Max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        unbounded_knapsack.go
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDPComp(wgt, val []int, cap int) int {\n    n := len(wgt)\n    // \u521d\u59cb\u5316 dp \u8868\n    dp := make([]int, cap+1)\n    // \u72b6\u6001\u8f6c\u79fb\n    for i := 1; i <= n; i++ {\n        for c := 1; c <= cap; c++ {\n            if wgt[i-1] > c {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))\n            }\n        }\n    }\n    return dp[cap]\n}\n
        unbounded_knapsack.swift
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDPComp(wgt: [Int], val: [Int], cap: Int) -> Int {\n    let n = wgt.count\n    // \u521d\u59cb\u5316 dp \u8868\n    var dp = Array(repeating: 0, count: cap + 1)\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1 ... n {\n        for c in 1 ... cap {\n            if wgt[i - 1] > c {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n            }\n        }\n    }\n    return dp[cap]\n}\n
        unbounded_knapsack.js
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDPComp(wgt, val, cap) {\n    const n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: cap + 1 }, () => 0);\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        unbounded_knapsack.ts
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDPComp(\n    wgt: Array<number>,\n    val: Array<number>,\n    cap: number\n): number {\n    const n = wgt.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: cap + 1 }, () => 0);\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
        unbounded_knapsack.dart
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(List<int> wgt, List<int> val, int cap) {\n  int n = wgt.length;\n  // \u521d\u59cb\u5316 dp \u8868\n  List<int> dp = List.filled(cap + 1, 0);\n  // \u72b6\u6001\u8f6c\u79fb\n  for (int i = 1; i <= n; i++) {\n    for (int c = 1; c <= cap; c++) {\n      if (wgt[i - 1] > c) {\n        // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n        dp[c] = dp[c];\n      } else {\n        // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n        dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n      }\n    }\n  }\n  return dp[cap];\n}\n
        unbounded_knapsack.rs
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn unbounded_knapsack_dp_comp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n    let n = wgt.len();\n    // \u521d\u59cb\u5316 dp \u8868\n    let mut dp = vec![0; cap + 1];\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1..=n {\n        for c in 1..=cap {\n            if wgt[i - 1] > c as i32 {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);\n            }\n        }\n    }\n    dp[cap]\n}\n
        unbounded_knapsack.c
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(int wgt[], int val[], int cap, int wgtSize) {\n    int n = wgtSize;\n    // \u521d\u59cb\u5316 dp \u8868\n    int *dp = calloc(cap + 1, sizeof(int));\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = myMax(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    int res = dp[cap];\n    // \u91ca\u653e\u5185\u5b58\n    free(dp);\n    return res;\n}\n
        unbounded_knapsack.kt
        /* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfun unboundedKnapsackDPComp(\n    wgt: IntArray,\n    _val: IntArray,\n    cap: Int\n): Int {\n    val n = wgt.size\n    // \u521d\u59cb\u5316 dp \u8868\n    val dp = IntArray(cap + 1)\n    // \u72b6\u6001\u8f6c\u79fb\n    for (i in 1..n) {\n        for (c in 1..cap) {\n            if (wgt[i - 1] > c) {\n                // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n                dp[c] = dp[c]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])\n            }\n        }\n    }\n    return dp[cap]\n}\n
        unbounded_knapsack.rb
        ### \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 ###\ndef unbounded_knapsack_dp(wgt, val, cap)\n  n = wgt.length\n  # \u521d\u59cb\u5316 dp \u8868\n  dp = Array.new(n + 1) { Array.new(cap + 1, 0) }\n  # \u72b6\u6001\u8f6c\u79fb\n  for i in 1...(n + 1)\n    for c in 1...(cap + 1)\n      if wgt[i - 1] > c\n        # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n        dp[i][c] = dp[i - 1][c]\n      else\n        # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n        dp[i][c] = [dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[n][cap]\nend\n\n### \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 ##3\ndef unbounded_knapsack_dp_comp(wgt, val, cap)\n  n = wgt.length\n  # \u521d\u59cb\u5316 dp \u8868\n  dp = Array.new(cap + 1, 0)\n  # \u72b6\u6001\u8f6c\u79fb\n  for i in 1...(n + 1)\n    # \u6b63\u5e8f\u904d\u5386\n    for c in 1...(cap + 1)\n      if wgt[i -1] > c\n        # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n        dp[c] = dp[c]\n      else\n        # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n        dp[c] = [dp[c], dp[c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[cap]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1452","title":"14.5.2 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898","text":"

        \u80cc\u5305\u95ee\u9898\u662f\u4e00\u5927\u7c7b\u52a8\u6001\u89c4\u5212\u95ee\u9898\u7684\u4ee3\u8868\uff0c\u5176\u62e5\u6709\u5f88\u591a\u53d8\u79cd\uff0c\u4f8b\u5982\u96f6\u94b1\u5151\u6362\u95ee\u9898\u3002

        Question

        \u7ed9\u5b9a \\(n\\) \u79cd\u786c\u5e01\uff0c\u7b2c \\(i\\) \u79cd\u786c\u5e01\u7684\u9762\u503c\u4e3a \\(coins[i - 1]\\) \uff0c\u76ee\u6807\u91d1\u989d\u4e3a \\(amt\\) \uff0c\u6bcf\u79cd\u786c\u5e01\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u80fd\u591f\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\u3002\u5982\u679c\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\uff0c\u5219\u8fd4\u56de \\(-1\\) \u3002\u793a\u4f8b\u5982\u56fe 14-24 \u6240\u793a\u3002

        \u56fe 14-24 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1_1","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"

        \u96f6\u94b1\u5151\u6362\u53ef\u4ee5\u770b\u4f5c\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u4e00\u79cd\u7279\u6b8a\u60c5\u51b5\uff0c\u4e24\u8005\u5177\u6709\u4ee5\u4e0b\u8054\u7cfb\u4e0e\u4e0d\u540c\u70b9\u3002

        • \u4e24\u9053\u9898\u53ef\u4ee5\u76f8\u4e92\u8f6c\u6362\uff0c\u201c\u7269\u54c1\u201d\u5bf9\u5e94\u201c\u786c\u5e01\u201d\u3001\u201c\u7269\u54c1\u91cd\u91cf\u201d\u5bf9\u5e94\u201c\u786c\u5e01\u9762\u503c\u201d\u3001\u201c\u80cc\u5305\u5bb9\u91cf\u201d\u5bf9\u5e94\u201c\u76ee\u6807\u91d1\u989d\u201d\u3002
        • \u4f18\u5316\u76ee\u6807\u76f8\u53cd\uff0c\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u662f\u8981\u6700\u5927\u5316\u7269\u54c1\u4ef7\u503c\uff0c\u96f6\u94b1\u5151\u6362\u95ee\u9898\u662f\u8981\u6700\u5c0f\u5316\u786c\u5e01\u6570\u91cf\u3002
        • \u5b8c\u5168\u80cc\u5305\u95ee\u9898\u662f\u6c42\u201c\u4e0d\u8d85\u8fc7\u201d\u80cc\u5305\u5bb9\u91cf\u4e0b\u7684\u89e3\uff0c\u96f6\u94b1\u5151\u6362\u662f\u6c42\u201c\u6070\u597d\u201d\u51d1\u5230\u76ee\u6807\u91d1\u989d\u7684\u89e3\u3002

        \u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868

        \u72b6\u6001 \\([i, a]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u4e3a\uff1a\u524d \\(i\\) \u79cd\u786c\u5e01\u80fd\u591f\u51d1\u51fa\u91d1\u989d \\(a\\) \u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\uff0c\u8bb0\u4e3a \\(dp[i, a]\\) \u3002

        \u4e8c\u7ef4 \\(dp\\) \u8868\u7684\u5c3a\u5bf8\u4e3a \\((n+1) \\times (amt+1)\\) \u3002

        \u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b

        \u672c\u9898\u4e0e\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u5b58\u5728\u4ee5\u4e0b\u4e24\u70b9\u5dee\u5f02\u3002

        • \u672c\u9898\u8981\u6c42\u6700\u5c0f\u503c\uff0c\u56e0\u6b64\u9700\u5c06\u8fd0\u7b97\u7b26 \\(\\max()\\) \u66f4\u6539\u4e3a \\(\\min()\\) \u3002
        • \u4f18\u5316\u4e3b\u4f53\u662f\u786c\u5e01\u6570\u91cf\u800c\u975e\u5546\u54c1\u4ef7\u503c\uff0c\u56e0\u6b64\u5728\u9009\u4e2d\u786c\u5e01\u65f6\u6267\u884c \\(+1\\) \u5373\u53ef\u3002
        \\[ dp[i, a] = \\min(dp[i-1, a], dp[i, a - coins[i-1]] + 1) \\]

        \u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f

        \u5f53\u76ee\u6807\u91d1\u989d\u4e3a \\(0\\) \u65f6\uff0c\u51d1\u51fa\u5b83\u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\u4e3a \\(0\\) \uff0c\u5373\u9996\u5217\u6240\u6709 \\(dp[i, 0]\\) \u90fd\u7b49\u4e8e \\(0\\) \u3002

        \u5f53\u65e0\u786c\u5e01\u65f6\uff0c\u65e0\u6cd5\u51d1\u51fa\u4efb\u610f \\(> 0\\) \u7684\u76ee\u6807\u91d1\u989d\uff0c\u5373\u662f\u65e0\u6548\u89e3\u3002\u4e3a\u4f7f\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e2d\u7684 \\(\\min()\\) \u51fd\u6570\u80fd\u591f\u8bc6\u522b\u5e76\u8fc7\u6ee4\u65e0\u6548\u89e3\uff0c\u6211\u4eec\u8003\u8651\u4f7f\u7528 \\(+ \\infty\\) \u6765\u8868\u793a\u5b83\u4eec\uff0c\u5373\u4ee4\u9996\u884c\u6240\u6709 \\(dp[0, a]\\) \u90fd\u7b49\u4e8e \\(+ \\infty\\) \u3002

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2_1","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u5e76\u672a\u63d0\u4f9b \\(+ \\infty\\) \u53d8\u91cf\uff0c\u53ea\u80fd\u4f7f\u7528\u6574\u578b int \u7684\u6700\u5927\u503c\u6765\u4ee3\u66ff\u3002\u800c\u8fd9\u53c8\u4f1a\u5bfc\u81f4\u5927\u6570\u8d8a\u754c\uff1a\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e2d\u7684 \\(+ 1\\) \u64cd\u4f5c\u53ef\u80fd\u53d1\u751f\u6ea2\u51fa\u3002

        \u4e3a\u6b64\uff0c\u6211\u4eec\u91c7\u7528\u6570\u5b57 \\(amt + 1\\) \u6765\u8868\u793a\u65e0\u6548\u89e3\uff0c\u56e0\u4e3a\u51d1\u51fa \\(amt\\) \u7684\u786c\u5e01\u6570\u91cf\u6700\u591a\u4e3a \\(amt\\) \u3002\u6700\u540e\u8fd4\u56de\u524d\uff0c\u5224\u65ad \\(dp[n, amt]\\) \u662f\u5426\u7b49\u4e8e \\(amt + 1\\) \uff0c\u82e5\u662f\u5219\u8fd4\u56de \\(-1\\) \uff0c\u4ee3\u8868\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change.py
        def coin_change_dp(coins: list[int], amt: int) -> int:\n    \"\"\"\u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n    n = len(coins)\n    MAX = amt + 1\n    # \u521d\u59cb\u5316 dp \u8868\n    dp = [[0] * (amt + 1) for _ in range(n + 1)]\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for a in range(1, amt + 1):\n        dp[0][a] = MAX\n    # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i in range(1, n + 1):\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a]\n            else:\n                # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n    return dp[n][amt] if dp[n][amt] != MAX else -1\n
        coin_change.cpp
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(vector<int> &coins, int amt) {\n    int n = coins.size();\n    int MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    vector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (int a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
        coin_change.java
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(int[] coins, int amt) {\n    int n = coins.length;\n    int MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[][] dp = new int[n + 1][amt + 1];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (int a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
        coin_change.cs
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint CoinChangeDP(int[] coins, int amt) {\n    int n = coins.Length;\n    int MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[,] dp = new int[n + 1, amt + 1];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (int a = 1; a <= amt; a++) {\n        dp[0, a] = MAX;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i, a] = dp[i - 1, a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[i, a] = Math.Min(dp[i - 1, a], dp[i, a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n, amt] != MAX ? dp[n, amt] : -1;\n}\n
        coin_change.go
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDP(coins []int, amt int) int {\n    n := len(coins)\n    max := amt + 1\n    // \u521d\u59cb\u5316 dp \u8868\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, amt+1)\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for a := 1; a <= amt; a++ {\n        dp[0][a] = max\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i := 1; i <= n; i++ {\n        for a := 1; a <= amt; a++ {\n            if coins[i-1] > a {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i-1][a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[i][a] = int(math.Min(float64(dp[i-1][a]), float64(dp[i][a-coins[i-1]]+1)))\n            }\n        }\n    }\n    if dp[n][amt] != max {\n        return dp[n][amt]\n    }\n    return -1\n}\n
        coin_change.swift
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDP(coins: [Int], amt: Int) -> Int {\n    let n = coins.count\n    let MAX = amt + 1\n    // \u521d\u59cb\u5316 dp \u8868\n    var dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for a in 1 ... amt {\n        dp[0][a] = MAX\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i in 1 ... n {\n        for a in 1 ... amt {\n            if coins[i - 1] > a {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n            }\n        }\n    }\n    return dp[n][amt] != MAX ? dp[n][amt] : -1\n}\n
        coin_change.js
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDP(coins, amt) {\n    const n = coins.length;\n    const MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: amt + 1 }, () => 0)\n    );\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (let a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] !== MAX ? dp[n][amt] : -1;\n}\n
        coin_change.ts
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDP(coins: Array<number>, amt: number): number {\n    const n = coins.length;\n    const MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: amt + 1 }, () => 0)\n    );\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (let a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] !== MAX ? dp[n][amt] : -1;\n}\n
        coin_change.dart
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(List<int> coins, int amt) {\n  int n = coins.length;\n  int MAX = amt + 1;\n  // \u521d\u59cb\u5316 dp \u8868\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(amt + 1, 0));\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n  for (int a = 1; a <= amt; a++) {\n    dp[0][a] = MAX;\n  }\n  // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n  for (int i = 1; i <= n; i++) {\n    for (int a = 1; a <= amt; a++) {\n      if (coins[i - 1] > a) {\n        // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n        dp[i][a] = dp[i - 1][a];\n      } else {\n        // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n        dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n      }\n    }\n  }\n  return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
        coin_change.rs
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfn coin_change_dp(coins: &[i32], amt: usize) -> i32 {\n    let n = coins.len();\n    let max = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    let mut dp = vec![vec![0; amt + 1]; n + 1];\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for a in 1..=amt {\n        dp[0][a] = max;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i in 1..=n {\n        for a in 1..=amt {\n            if coins[i - 1] > a as i32 {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[i][a] = std::cmp::min(dp[i - 1][a], dp[i][a - coins[i - 1] as usize] + 1);\n            }\n        }\n    }\n    if dp[n][amt] != max {\n        return dp[n][amt] as i32;\n    } else {\n        -1\n    }\n}\n
        coin_change.c
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(int coins[], int amt, int coinsSize) {\n    int n = coinsSize;\n    int MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(amt + 1, sizeof(int));\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (int a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[i][a] = myMin(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    int res = dp[n][amt] != MAX ? dp[n][amt] : -1;\n    // \u91ca\u653e\u5185\u5b58\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    free(dp);\n    return res;\n}\n
        coin_change.kt
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfun coinChangeDP(coins: IntArray, amt: Int): Int {\n    val n = coins.size\n    val MAX = amt + 1\n    // \u521d\u59cb\u5316 dp \u8868\n    val dp = Array(n + 1) { IntArray(amt + 1) }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n    for (a in 1..amt) {\n        dp[0][a] = MAX\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for (i in 1..n) {\n        for (a in 1..amt) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n            }\n        }\n    }\n    return if (dp[n][amt] != MAX) dp[n][amt] else -1\n}\n
        coin_change.rb
        ### \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 ###\ndef coin_change_dp(coins, amt)\n  n = coins.length\n  _MAX = amt + 1\n  # \u521d\u59cb\u5316 dp \u8868\n  dp = Array.new(n + 1) { Array.new(amt + 1, 0) }\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n  (1...(amt + 1)).each { |a| dp[0][a] = _MAX }\n  # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n  for i in 1...(n + 1)\n    for a in 1...(amt + 1)\n      if coins[i - 1] > a\n        # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n        dp[i][a] = dp[i - 1][a]\n      else\n        # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n        dp[i][a] = [dp[i - 1][a], dp[i][a - coins[i - 1]] + 1].min\n      end\n    end\n  end\n  dp[n][amt] != _MAX ? dp[n][amt] : -1\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 14-25 \u5c55\u793a\u4e86\u96f6\u94b1\u5151\u6362\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b\uff0c\u548c\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u975e\u5e38\u76f8\u4f3c\u3002

        <1><2><3><4><5><6><7><8><9><10><11><12><13><14><15>

        \u56fe 14-25 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3_1","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

        \u96f6\u94b1\u5151\u6362\u7684\u7a7a\u95f4\u4f18\u5316\u7684\u5904\u7406\u65b9\u5f0f\u548c\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u4e00\u81f4\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change.py
        def coin_change_dp_comp(coins: list[int], amt: int) -> int:\n    \"\"\"\u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n    n = len(coins)\n    MAX = amt + 1\n    # \u521d\u59cb\u5316 dp \u8868\n    dp = [MAX] * (amt + 1)\n    dp[0] = 0\n    # \u72b6\u6001\u8f6c\u79fb\n    for i in range(1, n + 1):\n        # \u6b63\u5e8f\u904d\u5386\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a]\n            else:\n                # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n    return dp[amt] if dp[amt] != MAX else -1\n
        coin_change.cpp
        /* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(vector<int> &coins, int amt) {\n    int n = coins.size();\n    int MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    vector<int> dp(amt + 1, MAX);\n    dp[0] = 0;\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1;\n}\n
        coin_change.java
        /* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(int[] coins, int amt) {\n    int n = coins.length;\n    int MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[] dp = new int[amt + 1];\n    Arrays.fill(dp, MAX);\n    dp[0] = 0;\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1;\n}\n
        coin_change.cs
        /* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint CoinChangeDPComp(int[] coins, int amt) {\n    int n = coins.Length;\n    int MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[] dp = new int[amt + 1];\n    Array.Fill(dp, MAX);\n    dp[0] = 0;\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[a] = Math.Min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1;\n}\n
        coin_change.go
        /* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDPComp(coins []int, amt int) int {\n    n := len(coins)\n    max := amt + 1\n    // \u521d\u59cb\u5316 dp \u8868\n    dp := make([]int, amt+1)\n    for i := 1; i <= amt; i++ {\n        dp[i] = max\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for i := 1; i <= n; i++ {\n        // \u6b63\u5e8f\u904d\u5386\n        for a := 1; a <= amt; a++ {\n            if coins[i-1] > a {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[a] = int(math.Min(float64(dp[a]), float64(dp[a-coins[i-1]]+1)))\n            }\n        }\n    }\n    if dp[amt] != max {\n        return dp[amt]\n    }\n    return -1\n}\n
        coin_change.swift
        /* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDPComp(coins: [Int], amt: Int) -> Int {\n    let n = coins.count\n    let MAX = amt + 1\n    // \u521d\u59cb\u5316 dp \u8868\n    var dp = Array(repeating: MAX, count: amt + 1)\n    dp[0] = 0\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1 ... n {\n        for a in 1 ... amt {\n            if coins[i - 1] > a {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1\n}\n
        coin_change.js
        /* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDPComp(coins, amt) {\n    const n = coins.length;\n    const MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: amt + 1 }, () => MAX);\n    dp[0] = 0;\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] !== MAX ? dp[amt] : -1;\n}\n
        coin_change.ts
        /* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDPComp(coins: Array<number>, amt: number): number {\n    const n = coins.length;\n    const MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: amt + 1 }, () => MAX);\n    dp[0] = 0;\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] !== MAX ? dp[amt] : -1;\n}\n
        coin_change.dart
        /* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(List<int> coins, int amt) {\n  int n = coins.length;\n  int MAX = amt + 1;\n  // \u521d\u59cb\u5316 dp \u8868\n  List<int> dp = List.filled(amt + 1, MAX);\n  dp[0] = 0;\n  // \u72b6\u6001\u8f6c\u79fb\n  for (int i = 1; i <= n; i++) {\n    for (int a = 1; a <= amt; a++) {\n      if (coins[i - 1] > a) {\n        // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n        dp[a] = dp[a];\n      } else {\n        // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n        dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n      }\n    }\n  }\n  return dp[amt] != MAX ? dp[amt] : -1;\n}\n
        coin_change.rs
        /* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn coin_change_dp_comp(coins: &[i32], amt: usize) -> i32 {\n    let n = coins.len();\n    let max = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    let mut dp = vec![0; amt + 1];\n    dp.fill(max);\n    dp[0] = 0;\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1..=n {\n        for a in 1..=amt {\n            if coins[i - 1] > a as i32 {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[a] = std::cmp::min(dp[a], dp[a - coins[i - 1] as usize] + 1);\n            }\n        }\n    }\n    if dp[amt] != max {\n        return dp[amt] as i32;\n    } else {\n        -1\n    }\n}\n
        coin_change.c
        /* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(int coins[], int amt, int coinsSize) {\n    int n = coinsSize;\n    int MAX = amt + 1;\n    // \u521d\u59cb\u5316 dp \u8868\n    int *dp = malloc((amt + 1) * sizeof(int));\n    for (int j = 1; j <= amt; j++) {\n        dp[j] = MAX;\n    } \n    dp[0] = 0;\n\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[a] = myMin(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    int res = dp[amt] != MAX ? dp[amt] : -1;\n    // \u91ca\u653e\u5185\u5b58\n    free(dp);\n    return res;\n}\n
        coin_change.kt
        /* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfun coinChangeDPComp(coins: IntArray, amt: Int): Int {\n    val n = coins.size\n    val MAX = amt + 1\n    // \u521d\u59cb\u5316 dp \u8868\n    val dp = IntArray(amt + 1)\n    dp.fill(MAX)\n    dp[0] = 0\n    // \u72b6\u6001\u8f6c\u79fb\n    for (i in 1..n) {\n        for (a in 1..amt) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n            }\n        }\n    }\n    return if (dp[amt] != MAX) dp[amt] else -1\n}\n
        coin_change.rb
        ### \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 ###\ndef coin_change_dp_comp(coins, amt)\n  n = coins.length\n  _MAX = amt + 1\n  # \u521d\u59cb\u5316 dp \u8868\n  dp = Array.new(amt + 1, _MAX)\n  dp[0] = 0\n  # \u72b6\u6001\u8f6c\u79fb\n  for i in 1...(n + 1)\n    # \u6b63\u5e8f\u904d\u5386\n    for a in 1...(amt + 1)\n      if coins[i - 1] > a\n        # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n        dp[a] = dp[a]\n      else\n        # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n        dp[a] = [dp[a], dp[a - coins[i - 1]] + 1].min\n      end\n    end\n  end\n  dp[amt] != _MAX ? dp[amt] : -1\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1453-ii","title":"14.5.3 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898 II","text":"

        Question

        \u7ed9\u5b9a \\(n\\) \u79cd\u786c\u5e01\uff0c\u7b2c \\(i\\) \u79cd\u786c\u5e01\u7684\u9762\u503c\u4e3a \\(coins[i - 1]\\) \uff0c\u76ee\u6807\u91d1\u989d\u4e3a \\(amt\\) \uff0c\u6bcf\u79cd\u786c\u5e01\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u7684\u786c\u5e01\u7ec4\u5408\u6570\u91cf\u3002\u793a\u4f8b\u5982\u56fe 14-26 \u6240\u793a\u3002

        \u56fe 14-26 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898 II \u7684\u793a\u4f8b\u6570\u636e

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1_2","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"

        \u76f8\u6bd4\u4e8e\u4e0a\u4e00\u9898\uff0c\u672c\u9898\u76ee\u6807\u662f\u6c42\u7ec4\u5408\u6570\u91cf\uff0c\u56e0\u6b64\u5b50\u95ee\u9898\u53d8\u4e3a\uff1a\u524d \\(i\\) \u79cd\u786c\u5e01\u80fd\u591f\u51d1\u51fa\u91d1\u989d \\(a\\) \u7684\u7ec4\u5408\u6570\u91cf\u3002\u800c \\(dp\\) \u8868\u4ecd\u7136\u662f\u5c3a\u5bf8\u4e3a \\((n+1) \\times (amt + 1)\\) \u7684\u4e8c\u7ef4\u77e9\u9635\u3002

        \u5f53\u524d\u72b6\u6001\u7684\u7ec4\u5408\u6570\u91cf\u7b49\u4e8e\u4e0d\u9009\u5f53\u524d\u786c\u5e01\u4e0e\u9009\u5f53\u524d\u786c\u5e01\u8fd9\u4e24\u79cd\u51b3\u7b56\u7684\u7ec4\u5408\u6570\u91cf\u4e4b\u548c\u3002\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a

        \\[ dp[i, a] = dp[i-1, a] + dp[i, a - coins[i-1]] \\]

        \u5f53\u76ee\u6807\u91d1\u989d\u4e3a \\(0\\) \u65f6\uff0c\u65e0\u987b\u9009\u62e9\u4efb\u4f55\u786c\u5e01\u5373\u53ef\u51d1\u51fa\u76ee\u6807\u91d1\u989d\uff0c\u56e0\u6b64\u5e94\u5c06\u9996\u5217\u6240\u6709 \\(dp[i, 0]\\) \u90fd\u521d\u59cb\u5316\u4e3a \\(1\\) \u3002\u5f53\u65e0\u786c\u5e01\u65f6\uff0c\u65e0\u6cd5\u51d1\u51fa\u4efb\u4f55 \\(>0\\) \u7684\u76ee\u6807\u91d1\u989d\uff0c\u56e0\u6b64\u9996\u884c\u6240\u6709 \\(dp[0, a]\\) \u90fd\u7b49\u4e8e \\(0\\) \u3002

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2_2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change_ii.py
        def coin_change_ii_dp(coins: list[int], amt: int) -> int:\n    \"\"\"\u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n    n = len(coins)\n    # \u521d\u59cb\u5316 dp \u8868\n    dp = [[0] * (amt + 1) for _ in range(n + 1)]\n    # \u521d\u59cb\u5316\u9996\u5217\n    for i in range(n + 1):\n        dp[i][0] = 1\n    # \u72b6\u6001\u8f6c\u79fb\n    for i in range(1, n + 1):\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a]\n            else:\n                # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n    return dp[n][amt]\n
        coin_change_ii.cpp
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(vector<int> &coins, int amt) {\n    int n = coins.size();\n    // \u521d\u59cb\u5316 dp \u8868\n    vector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n    // \u521d\u59cb\u5316\u9996\u5217\n    for (int i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
        coin_change_ii.java
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(int[] coins, int amt) {\n    int n = coins.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[][] dp = new int[n + 1][amt + 1];\n    // \u521d\u59cb\u5316\u9996\u5217\n    for (int i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
        coin_change_ii.cs
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint CoinChangeIIDP(int[] coins, int amt) {\n    int n = coins.Length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[,] dp = new int[n + 1, amt + 1];\n    // \u521d\u59cb\u5316\u9996\u5217\n    for (int i = 0; i <= n; i++) {\n        dp[i, 0] = 1;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i, a] = dp[i - 1, a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[i, a] = dp[i - 1, a] + dp[i, a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n, amt];\n}\n
        coin_change_ii.go
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDP(coins []int, amt int) int {\n    n := len(coins)\n    // \u521d\u59cb\u5316 dp \u8868\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, amt+1)\n    }\n    // \u521d\u59cb\u5316\u9996\u5217\n    for i := 0; i <= n; i++ {\n        dp[i][0] = 1\n    }\n    // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n    for i := 1; i <= n; i++ {\n        for a := 1; a <= amt; a++ {\n            if coins[i-1] > a {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i-1][a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[i][a] = dp[i-1][a] + dp[i][a-coins[i-1]]\n            }\n        }\n    }\n    return dp[n][amt]\n}\n
        coin_change_ii.swift
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDP(coins: [Int], amt: Int) -> Int {\n    let n = coins.count\n    // \u521d\u59cb\u5316 dp \u8868\n    var dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)\n    // \u521d\u59cb\u5316\u9996\u5217\n    for i in 0 ... n {\n        dp[i][0] = 1\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1 ... n {\n        for a in 1 ... amt {\n            if coins[i - 1] > a {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n            }\n        }\n    }\n    return dp[n][amt]\n}\n
        coin_change_ii.js
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDP(coins, amt) {\n    const n = coins.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: amt + 1 }, () => 0)\n    );\n    // \u521d\u59cb\u5316\u9996\u5217\n    for (let i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
        coin_change_ii.ts
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDP(coins: Array<number>, amt: number): number {\n    const n = coins.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: amt + 1 }, () => 0)\n    );\n    // \u521d\u59cb\u5316\u9996\u5217\n    for (let i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
        coin_change_ii.dart
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(List<int> coins, int amt) {\n  int n = coins.length;\n  // \u521d\u59cb\u5316 dp \u8868\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(amt + 1, 0));\n  // \u521d\u59cb\u5316\u9996\u5217\n  for (int i = 0; i <= n; i++) {\n    dp[i][0] = 1;\n  }\n  // \u72b6\u6001\u8f6c\u79fb\n  for (int i = 1; i <= n; i++) {\n    for (int a = 1; a <= amt; a++) {\n      if (coins[i - 1] > a) {\n        // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n        dp[i][a] = dp[i - 1][a];\n      } else {\n        // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n        dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n      }\n    }\n  }\n  return dp[n][amt];\n}\n
        coin_change_ii.rs
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfn coin_change_ii_dp(coins: &[i32], amt: usize) -> i32 {\n    let n = coins.len();\n    // \u521d\u59cb\u5316 dp \u8868\n    let mut dp = vec![vec![0; amt + 1]; n + 1];\n    // \u521d\u59cb\u5316\u9996\u5217\n    for i in 0..=n {\n        dp[i][0] = 1;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1..=n {\n        for a in 1..=amt {\n            if coins[i - 1] > a as i32 {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1] as usize];\n            }\n        }\n    }\n    dp[n][amt]\n}\n
        coin_change_ii.c
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(int coins[], int amt, int coinsSize) {\n    int n = coinsSize;\n    // \u521d\u59cb\u5316 dp \u8868\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(amt + 1, sizeof(int));\n    }\n    // \u521d\u59cb\u5316\u9996\u5217\n    for (int i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    int res = dp[n][amt];\n    // \u91ca\u653e\u5185\u5b58\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    free(dp);\n    return res;\n}\n
        coin_change_ii.kt
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfun coinChangeIIDP(coins: IntArray, amt: Int): Int {\n    val n = coins.size\n    // \u521d\u59cb\u5316 dp \u8868\n    val dp = Array(n + 1) { IntArray(amt + 1) }\n    // \u521d\u59cb\u5316\u9996\u5217\n    for (i in 0..n) {\n        dp[i][0] = 1\n    }\n    // \u72b6\u6001\u8f6c\u79fb\n    for (i in 1..n) {\n        for (a in 1..amt) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[i][a] = dp[i - 1][a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n            }\n        }\n    }\n    return dp[n][amt]\n}\n
        coin_change_ii.rb
        ### \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 ###\ndef coin_change_ii_dp(coins, amt)\n  n = coins.length\n  # \u521d\u59cb\u5316 dp \u8868\n  dp = Array.new(n + 1) { Array.new(amt + 1, 0) }\n  # \u521d\u59cb\u5316\u9996\u5217\n  (0...(n + 1)).each { |i| dp[i][0] = 1 }\n  # \u72b6\u6001\u8f6c\u79fb\n  for i in 1...(n + 1)\n    for a in 1...(amt + 1)\n      if coins[i - 1] > a\n        # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n        dp[i][a] = dp[i - 1][a]\n      else\n        # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n        dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n      end\n    end\n  end\n  dp[n][amt]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3_2","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

        \u7a7a\u95f4\u4f18\u5316\u5904\u7406\u65b9\u5f0f\u76f8\u540c\uff0c\u5220\u9664\u786c\u5e01\u7ef4\u5ea6\u5373\u53ef\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change_ii.py
        def coin_change_ii_dp_comp(coins: list[int], amt: int) -> int:\n    \"\"\"\u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n    n = len(coins)\n    # \u521d\u59cb\u5316 dp \u8868\n    dp = [0] * (amt + 1)\n    dp[0] = 1\n    # \u72b6\u6001\u8f6c\u79fb\n    for i in range(1, n + 1):\n        # \u6b63\u5e8f\u904d\u5386\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a]\n            else:\n                # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[a] = dp[a] + dp[a - coins[i - 1]]\n    return dp[amt]\n
        coin_change_ii.cpp
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(vector<int> &coins, int amt) {\n    int n = coins.size();\n    // \u521d\u59cb\u5316 dp \u8868\n    vector<int> dp(amt + 1, 0);\n    dp[0] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
        coin_change_ii.java
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(int[] coins, int amt) {\n    int n = coins.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[] dp = new int[amt + 1];\n    dp[0] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
        coin_change_ii.cs
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint CoinChangeIIDPComp(int[] coins, int amt) {\n    int n = coins.Length;\n    // \u521d\u59cb\u5316 dp \u8868\n    int[] dp = new int[amt + 1];\n    dp[0] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
        coin_change_ii.go
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDPComp(coins []int, amt int) int {\n    n := len(coins)\n    // \u521d\u59cb\u5316 dp \u8868\n    dp := make([]int, amt+1)\n    dp[0] = 1\n    // \u72b6\u6001\u8f6c\u79fb\n    for i := 1; i <= n; i++ {\n        // \u6b63\u5e8f\u904d\u5386\n        for a := 1; a <= amt; a++ {\n            if coins[i-1] > a {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[a] = dp[a] + dp[a-coins[i-1]]\n            }\n        }\n    }\n    return dp[amt]\n}\n
        coin_change_ii.swift
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDPComp(coins: [Int], amt: Int) -> Int {\n    let n = coins.count\n    // \u521d\u59cb\u5316 dp \u8868\n    var dp = Array(repeating: 0, count: amt + 1)\n    dp[0] = 1\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1 ... n {\n        for a in 1 ... amt {\n            if coins[i - 1] > a {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[a] = dp[a] + dp[a - coins[i - 1]]\n            }\n        }\n    }\n    return dp[amt]\n}\n
        coin_change_ii.js
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDPComp(coins, amt) {\n    const n = coins.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: amt + 1 }, () => 0);\n    dp[0] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
        coin_change_ii.ts
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDPComp(coins: Array<number>, amt: number): number {\n    const n = coins.length;\n    // \u521d\u59cb\u5316 dp \u8868\n    const dp = Array.from({ length: amt + 1 }, () => 0);\n    dp[0] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
        coin_change_ii.dart
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(List<int> coins, int amt) {\n  int n = coins.length;\n  // \u521d\u59cb\u5316 dp \u8868\n  List<int> dp = List.filled(amt + 1, 0);\n  dp[0] = 1;\n  // \u72b6\u6001\u8f6c\u79fb\n  for (int i = 1; i <= n; i++) {\n    for (int a = 1; a <= amt; a++) {\n      if (coins[i - 1] > a) {\n        // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n        dp[a] = dp[a];\n      } else {\n        // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n        dp[a] = dp[a] + dp[a - coins[i - 1]];\n      }\n    }\n  }\n  return dp[amt];\n}\n
        coin_change_ii.rs
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn coin_change_ii_dp_comp(coins: &[i32], amt: usize) -> i32 {\n    let n = coins.len();\n    // \u521d\u59cb\u5316 dp \u8868\n    let mut dp = vec![0; amt + 1];\n    dp[0] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\n    for i in 1..=n {\n        for a in 1..=amt {\n            if coins[i - 1] > a as i32 {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[a] = dp[a] + dp[a - coins[i - 1] as usize];\n            }\n        }\n    }\n    dp[amt]\n}\n
        coin_change_ii.c
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(int coins[], int amt, int coinsSize) {\n    int n = coinsSize;\n    // \u521d\u59cb\u5316 dp \u8868\n    int *dp = calloc(amt + 1, sizeof(int));\n    dp[0] = 1;\n    // \u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a];\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    int res = dp[amt];\n    // \u91ca\u653e\u5185\u5b58\n    free(dp);\n    return res;\n}\n
        coin_change_ii.kt
        /* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfun coinChangeIIDPComp(coins: IntArray, amt: Int): Int {\n    val n = coins.size\n    // \u521d\u59cb\u5316 dp \u8868\n    val dp = IntArray(amt + 1)\n    dp[0] = 1\n    // \u72b6\u6001\u8f6c\u79fb\n    for (i in 1..n) {\n        for (a in 1..amt) {\n            if (coins[i - 1] > a) {\n                // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n                dp[a] = dp[a]\n            } else {\n                // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n                dp[a] = dp[a] + dp[a - coins[i - 1]]\n            }\n        }\n    }\n    return dp[amt]\n}\n
        coin_change_ii.rb
        ### \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 ###\ndef coin_change_ii_dp_comp(coins, amt)\n  n = coins.length\n  # \u521d\u59cb\u5316 dp \u8868\n  dp = Array.new(amt + 1, 0)\n  dp[0] = 1\n  # \u72b6\u6001\u8f6c\u79fb\n  for i in 1...(n + 1)\n    # \u6b63\u5e8f\u904d\u5386\n    for a in 1...(amt + 1)\n      if coins[i - 1] > a\n        # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n        dp[a] = dp[a]\n      else\n        # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n        dp[a] = dp[a] + dp[a - coins[i - 1]]\n      end\n    end\n  end\n  dp[amt]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_graph/","title":"\u7b2c 9 \u7ae0 \u00a0 \u56fe","text":"

        Abstract

        \u5728\u751f\u547d\u65c5\u9014\u4e2d\uff0c\u6211\u4eec\u5c31\u50cf\u662f\u4e00\u4e2a\u4e2a\u8282\u70b9\uff0c\u88ab\u65e0\u6570\u770b\u4e0d\u89c1\u7684\u8fb9\u76f8\u8fde\u3002

        \u6bcf\u4e00\u6b21\u7684\u76f8\u8bc6\u4e0e\u76f8\u79bb\uff0c\u90fd\u5728\u8fd9\u5f20\u5de8\u5927\u7684\u7f51\u7edc\u56fe\u4e2d\u7559\u4e0b\u72ec\u7279\u7684\u5370\u8bb0\u3002

        "},{"location":"chapter_graph/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 9.1 \u00a0 \u56fe
        • 9.2 \u00a0 \u56fe\u57fa\u7840\u64cd\u4f5c
        • 9.3 \u00a0 \u56fe\u7684\u904d\u5386
        • 9.4 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_graph/graph/","title":"9.1 \u00a0 \u56fe","text":"

        \u56fe\uff08graph\uff09\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u7531\u9876\u70b9\uff08vertex\uff09\u548c\u8fb9\uff08edge\uff09\u7ec4\u6210\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u56fe \\(G\\) \u62bd\u8c61\u5730\u8868\u793a\u4e3a\u4e00\u7ec4\u9876\u70b9 \\(V\\) \u548c\u4e00\u7ec4\u8fb9 \\(E\\) \u7684\u96c6\u5408\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u4e00\u4e2a\u5305\u542b 5 \u4e2a\u9876\u70b9\u548c 7 \u6761\u8fb9\u7684\u56fe\u3002

        \\[ \\begin{aligned} V & = \\{ 1, 2, 3, 4, 5 \\} \\newline E & = \\{ (1,2), (1,3), (1,5), (2,3), (2,4), (2,5), (4,5) \\} \\newline G & = \\{ V, E \\} \\newline \\end{aligned} \\]

        \u5982\u679c\u5c06\u9876\u70b9\u770b\u4f5c\u8282\u70b9\uff0c\u5c06\u8fb9\u770b\u4f5c\u8fde\u63a5\u5404\u4e2a\u8282\u70b9\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u56fe\u770b\u4f5c\u4e00\u79cd\u4ece\u94fe\u8868\u62d3\u5c55\u800c\u6765\u7684\u6570\u636e\u7ed3\u6784\u3002\u5982\u56fe 9-1 \u6240\u793a\uff0c\u76f8\u8f83\u4e8e\u7ebf\u6027\u5173\u7cfb\uff08\u94fe\u8868\uff09\u548c\u5206\u6cbb\u5173\u7cfb\uff08\u6811\uff09\uff0c\u7f51\u7edc\u5173\u7cfb\uff08\u56fe\uff09\u7684\u81ea\u7531\u5ea6\u66f4\u9ad8\uff0c\u56e0\u800c\u66f4\u4e3a\u590d\u6742\u3002

        \u56fe 9-1 \u00a0 \u94fe\u8868\u3001\u6811\u3001\u56fe\u4e4b\u95f4\u7684\u5173\u7cfb

        "},{"location":"chapter_graph/graph/#911","title":"9.1.1 \u00a0 \u56fe\u7684\u5e38\u89c1\u7c7b\u578b\u4e0e\u672f\u8bed","text":"

        \u6839\u636e\u8fb9\u662f\u5426\u5177\u6709\u65b9\u5411\uff0c\u53ef\u5206\u4e3a\u65e0\u5411\u56fe\uff08undirected graph\uff09\u548c\u6709\u5411\u56fe\uff08directed graph\uff09\uff0c\u5982\u56fe 9-2 \u6240\u793a\u3002

        • \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u8fb9\u8868\u793a\u4e24\u9876\u70b9\u4e4b\u95f4\u7684\u201c\u53cc\u5411\u201d\u8fde\u63a5\u5173\u7cfb\uff0c\u4f8b\u5982\u5fae\u4fe1\u6216 QQ \u4e2d\u7684\u201c\u597d\u53cb\u5173\u7cfb\u201d\u3002
        • \u5728\u6709\u5411\u56fe\u4e2d\uff0c\u8fb9\u5177\u6709\u65b9\u5411\u6027\uff0c\u5373 \\(A \\rightarrow B\\) \u548c \\(A \\leftarrow B\\) \u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u662f\u76f8\u4e92\u72ec\u7acb\u7684\uff0c\u4f8b\u5982\u5fae\u535a\u6216\u6296\u97f3\u4e0a\u7684\u201c\u5173\u6ce8\u201d\u4e0e\u201c\u88ab\u5173\u6ce8\u201d\u5173\u7cfb\u3002

        \u56fe 9-2 \u00a0 \u6709\u5411\u56fe\u4e0e\u65e0\u5411\u56fe

        \u6839\u636e\u6240\u6709\u9876\u70b9\u662f\u5426\u8fde\u901a\uff0c\u53ef\u5206\u4e3a\u8fde\u901a\u56fe\uff08connected graph\uff09\u548c\u975e\u8fde\u901a\u56fe\uff08disconnected graph\uff09\uff0c\u5982\u56fe 9-3 \u6240\u793a\u3002

        • \u5bf9\u4e8e\u8fde\u901a\u56fe\uff0c\u4ece\u67d0\u4e2a\u9876\u70b9\u51fa\u53d1\uff0c\u53ef\u4ee5\u5230\u8fbe\u5176\u4f59\u4efb\u610f\u9876\u70b9\u3002
        • \u5bf9\u4e8e\u975e\u8fde\u901a\u56fe\uff0c\u4ece\u67d0\u4e2a\u9876\u70b9\u51fa\u53d1\uff0c\u81f3\u5c11\u6709\u4e00\u4e2a\u9876\u70b9\u65e0\u6cd5\u5230\u8fbe\u3002

        \u56fe 9-3 \u00a0 \u8fde\u901a\u56fe\u4e0e\u975e\u8fde\u901a\u56fe

        \u6211\u4eec\u8fd8\u53ef\u4ee5\u4e3a\u8fb9\u6dfb\u52a0\u201c\u6743\u91cd\u201d\u53d8\u91cf\uff0c\u4ece\u800c\u5f97\u5230\u5982\u56fe 9-4 \u6240\u793a\u7684\u6709\u6743\u56fe\uff08weighted graph\uff09\u3002\u4f8b\u5982\u5728\u300a\u738b\u8005\u8363\u8000\u300b\u7b49\u624b\u6e38\u4e2d\uff0c\u7cfb\u7edf\u4f1a\u6839\u636e\u5171\u540c\u6e38\u620f\u65f6\u95f4\u6765\u8ba1\u7b97\u73a9\u5bb6\u4e4b\u95f4\u7684\u201c\u4eb2\u5bc6\u5ea6\u201d\uff0c\u8fd9\u79cd\u4eb2\u5bc6\u5ea6\u7f51\u7edc\u5c31\u53ef\u4ee5\u7528\u6709\u6743\u56fe\u6765\u8868\u793a\u3002

        \u56fe 9-4 \u00a0 \u6709\u6743\u56fe\u4e0e\u65e0\u6743\u56fe

        \u56fe\u6570\u636e\u7ed3\u6784\u5305\u542b\u4ee5\u4e0b\u5e38\u7528\u672f\u8bed\u3002

        • \u90bb\u63a5\uff08adjacency\uff09\uff1a\u5f53\u4e24\u9876\u70b9\u4e4b\u95f4\u5b58\u5728\u8fb9\u76f8\u8fde\u65f6\uff0c\u79f0\u8fd9\u4e24\u9876\u70b9\u201c\u90bb\u63a5\u201d\u3002\u5728\u56fe 9-4 \u4e2d\uff0c\u9876\u70b9 1 \u7684\u90bb\u63a5\u9876\u70b9\u4e3a\u9876\u70b9 2\u30013\u30015\u3002
        • \u8def\u5f84\uff08path\uff09\uff1a\u4ece\u9876\u70b9 A \u5230\u9876\u70b9 B \u7ecf\u8fc7\u7684\u8fb9\u6784\u6210\u7684\u5e8f\u5217\u88ab\u79f0\u4e3a\u4ece A \u5230 B \u7684\u201c\u8def\u5f84\u201d\u3002\u5728\u56fe 9-4 \u4e2d\uff0c\u8fb9\u5e8f\u5217 1-5-2-4 \u662f\u9876\u70b9 1 \u5230\u9876\u70b9 4 \u7684\u4e00\u6761\u8def\u5f84\u3002
        • \u5ea6\uff08degree\uff09\uff1a\u4e00\u4e2a\u9876\u70b9\u62e5\u6709\u7684\u8fb9\u6570\u3002\u5bf9\u4e8e\u6709\u5411\u56fe\uff0c\u5165\u5ea6\uff08in-degree\uff09\u8868\u793a\u6709\u591a\u5c11\u6761\u8fb9\u6307\u5411\u8be5\u9876\u70b9\uff0c\u51fa\u5ea6\uff08out-degree\uff09\u8868\u793a\u6709\u591a\u5c11\u6761\u8fb9\u4ece\u8be5\u9876\u70b9\u6307\u51fa\u3002
        "},{"location":"chapter_graph/graph/#912","title":"9.1.2 \u00a0 \u56fe\u7684\u8868\u793a","text":"

        \u56fe\u7684\u5e38\u7528\u8868\u793a\u65b9\u5f0f\u5305\u62ec\u201c\u90bb\u63a5\u77e9\u9635\u201d\u548c\u201c\u90bb\u63a5\u8868\u201d\u3002\u4ee5\u4e0b\u4f7f\u7528\u65e0\u5411\u56fe\u8fdb\u884c\u4e3e\u4f8b\u3002

        "},{"location":"chapter_graph/graph/#1","title":"1. \u00a0 \u90bb\u63a5\u77e9\u9635","text":"

        \u8bbe\u56fe\u7684\u9876\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u90bb\u63a5\u77e9\u9635\uff08adjacency matrix\uff09\u4f7f\u7528\u4e00\u4e2a \\(n \\times n\\) \u5927\u5c0f\u7684\u77e9\u9635\u6765\u8868\u793a\u56fe\uff0c\u6bcf\u4e00\u884c\uff08\u5217\uff09\u4ee3\u8868\u4e00\u4e2a\u9876\u70b9\uff0c\u77e9\u9635\u5143\u7d20\u4ee3\u8868\u8fb9\uff0c\u7528 \\(1\\) \u6216 \\(0\\) \u8868\u793a\u4e24\u4e2a\u9876\u70b9\u4e4b\u95f4\u662f\u5426\u5b58\u5728\u8fb9\u3002

        \u5982\u56fe 9-5 \u6240\u793a\uff0c\u8bbe\u90bb\u63a5\u77e9\u9635\u4e3a \\(M\\)\u3001\u9876\u70b9\u5217\u8868\u4e3a \\(V\\) \uff0c\u90a3\u4e48\u77e9\u9635\u5143\u7d20 \\(M[i, j] = 1\\) \u8868\u793a\u9876\u70b9 \\(V[i]\\) \u5230\u9876\u70b9 \\(V[j]\\) \u4e4b\u95f4\u5b58\u5728\u8fb9\uff0c\u53cd\u4e4b \\(M[i, j] = 0\\) \u8868\u793a\u4e24\u9876\u70b9\u4e4b\u95f4\u65e0\u8fb9\u3002

        \u56fe 9-5 \u00a0 \u56fe\u7684\u90bb\u63a5\u77e9\u9635\u8868\u793a

        \u90bb\u63a5\u77e9\u9635\u5177\u6709\u4ee5\u4e0b\u7279\u6027\u3002

        • \u5728\u7b80\u5355\u56fe\u4e2d\uff0c\u9876\u70b9\u4e0d\u80fd\u4e0e\u81ea\u8eab\u76f8\u8fde\uff0c\u6b64\u65f6\u90bb\u63a5\u77e9\u9635\u4e3b\u5bf9\u89d2\u7ebf\u5143\u7d20\u6ca1\u6709\u610f\u4e49\u3002
        • \u5bf9\u4e8e\u65e0\u5411\u56fe\uff0c\u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u7b49\u4ef7\uff0c\u6b64\u65f6\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\u3002
        • \u5c06\u90bb\u63a5\u77e9\u9635\u7684\u5143\u7d20\u4ece \\(1\\) \u548c \\(0\\) \u66ff\u6362\u4e3a\u6743\u91cd\uff0c\u5219\u53ef\u8868\u793a\u6709\u6743\u56fe\u3002

        \u4f7f\u7528\u90bb\u63a5\u77e9\u9635\u8868\u793a\u56fe\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u77e9\u9635\u5143\u7d20\u4ee5\u83b7\u53d6\u8fb9\uff0c\u56e0\u6b64\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u7684\u6548\u7387\u5f88\u9ad8\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(1)\\) \u3002\u7136\u800c\uff0c\u77e9\u9635\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u5185\u5b58\u5360\u7528\u8f83\u591a\u3002

        "},{"location":"chapter_graph/graph/#2","title":"2. \u00a0 \u90bb\u63a5\u8868","text":"

        \u90bb\u63a5\u8868\uff08adjacency list\uff09\u4f7f\u7528 \\(n\\) \u4e2a\u94fe\u8868\u6765\u8868\u793a\u56fe\uff0c\u94fe\u8868\u8282\u70b9\u8868\u793a\u9876\u70b9\u3002\u7b2c \\(i\\) \u4e2a\u94fe\u8868\u5bf9\u5e94\u9876\u70b9 \\(i\\) \uff0c\u5176\u4e2d\u5b58\u50a8\u4e86\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff08\u4e0e\u8be5\u9876\u70b9\u76f8\u8fde\u7684\u9876\u70b9\uff09\u3002\u56fe 9-6 \u5c55\u793a\u4e86\u4e00\u4e2a\u4f7f\u7528\u90bb\u63a5\u8868\u5b58\u50a8\u7684\u56fe\u7684\u793a\u4f8b\u3002

        \u56fe 9-6 \u00a0 \u56fe\u7684\u90bb\u63a5\u8868\u8868\u793a

        \u90bb\u63a5\u8868\u4ec5\u5b58\u50a8\u5b9e\u9645\u5b58\u5728\u7684\u8fb9\uff0c\u800c\u8fb9\u7684\u603b\u6570\u901a\u5e38\u8fdc\u5c0f\u4e8e \\(n^2\\) \uff0c\u56e0\u6b64\u5b83\u66f4\u52a0\u8282\u7701\u7a7a\u95f4\u3002\u7136\u800c\uff0c\u5728\u90bb\u63a5\u8868\u4e2d\u9700\u8981\u901a\u8fc7\u904d\u5386\u94fe\u8868\u6765\u67e5\u627e\u8fb9\uff0c\u56e0\u6b64\u5176\u65f6\u95f4\u6548\u7387\u4e0d\u5982\u90bb\u63a5\u77e9\u9635\u3002

        \u89c2\u5bdf\u56fe 9-6 \uff0c\u90bb\u63a5\u8868\u7ed3\u6784\u4e0e\u54c8\u5e0c\u8868\u4e2d\u7684\u201c\u94fe\u5f0f\u5730\u5740\u201d\u975e\u5e38\u76f8\u4f3c\uff0c\u56e0\u6b64\u6211\u4eec\u4e5f\u53ef\u4ee5\u91c7\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u6765\u4f18\u5316\u6548\u7387\u3002\u6bd4\u5982\u5f53\u94fe\u8868\u8f83\u957f\u65f6\uff0c\u53ef\u4ee5\u5c06\u94fe\u8868\u8f6c\u5316\u4e3a AVL \u6811\u6216\u7ea2\u9ed1\u6811\uff0c\u4ece\u800c\u5c06\u65f6\u95f4\u6548\u7387\u4ece \\(O(n)\\) \u4f18\u5316\u81f3 \\(O(\\log n)\\) \uff1b\u8fd8\u53ef\u4ee5\u628a\u94fe\u8868\u8f6c\u6362\u4e3a\u54c8\u5e0c\u8868\uff0c\u4ece\u800c\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u964d\u81f3 \\(O(1)\\) \u3002

        "},{"location":"chapter_graph/graph/#913","title":"9.1.3 \u00a0 \u56fe\u7684\u5e38\u89c1\u5e94\u7528","text":"

        \u5982\u8868 9-1 \u6240\u793a\uff0c\u8bb8\u591a\u73b0\u5b9e\u7cfb\u7edf\u53ef\u4ee5\u7528\u56fe\u6765\u5efa\u6a21\uff0c\u76f8\u5e94\u7684\u95ee\u9898\u4e5f\u53ef\u4ee5\u7ea6\u5316\u4e3a\u56fe\u8ba1\u7b97\u95ee\u9898\u3002

        \u8868 9-1 \u00a0 \u73b0\u5b9e\u751f\u6d3b\u4e2d\u5e38\u89c1\u7684\u56fe

        \u9876\u70b9 \u8fb9 \u56fe\u8ba1\u7b97\u95ee\u9898 \u793e\u4ea4\u7f51\u7edc \u7528\u6237 \u597d\u53cb\u5173\u7cfb \u6f5c\u5728\u597d\u53cb\u63a8\u8350 \u5730\u94c1\u7ebf\u8def \u7ad9\u70b9 \u7ad9\u70b9\u95f4\u7684\u8fde\u901a\u6027 \u6700\u77ed\u8def\u7ebf\u63a8\u8350 \u592a\u9633\u7cfb \u661f\u4f53 \u661f\u4f53\u95f4\u7684\u4e07\u6709\u5f15\u529b\u4f5c\u7528 \u884c\u661f\u8f68\u9053\u8ba1\u7b97"},{"location":"chapter_graph/graph_operations/","title":"9.2 \u00a0 \u56fe\u7684\u57fa\u7840\u64cd\u4f5c","text":"

        \u56fe\u7684\u57fa\u7840\u64cd\u4f5c\u53ef\u5206\u4e3a\u5bf9\u201c\u8fb9\u201d\u7684\u64cd\u4f5c\u548c\u5bf9\u201c\u9876\u70b9\u201d\u7684\u64cd\u4f5c\u3002\u5728\u201c\u90bb\u63a5\u77e9\u9635\u201d\u548c\u201c\u90bb\u63a5\u8868\u201d\u4e24\u79cd\u8868\u793a\u65b9\u6cd5\u4e0b\uff0c\u5b9e\u73b0\u65b9\u5f0f\u6709\u6240\u4e0d\u540c\u3002

        "},{"location":"chapter_graph/graph_operations/#921","title":"9.2.1 \u00a0 \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u7684\u5b9e\u73b0","text":"

        \u7ed9\u5b9a\u4e00\u4e2a\u9876\u70b9\u6570\u91cf\u4e3a \\(n\\) \u7684\u65e0\u5411\u56fe\uff0c\u5219\u5404\u79cd\u64cd\u4f5c\u7684\u5b9e\u73b0\u65b9\u5f0f\u5982\u56fe 9-7 \u6240\u793a\u3002

        • \u6dfb\u52a0\u6216\u5220\u9664\u8fb9\uff1a\u76f4\u63a5\u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u4fee\u6539\u6307\u5b9a\u7684\u8fb9\u5373\u53ef\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002\u800c\u7531\u4e8e\u662f\u65e0\u5411\u56fe\uff0c\u56e0\u6b64\u9700\u8981\u540c\u65f6\u66f4\u65b0\u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u3002
        • \u6dfb\u52a0\u9876\u70b9\uff1a\u5728\u90bb\u63a5\u77e9\u9635\u7684\u5c3e\u90e8\u6dfb\u52a0\u4e00\u884c\u4e00\u5217\uff0c\u5e76\u5168\u90e8\u586b \\(0\\) \u5373\u53ef\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002
        • \u5220\u9664\u9876\u70b9\uff1a\u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u4e00\u884c\u4e00\u5217\u3002\u5f53\u5220\u9664\u9996\u884c\u9996\u5217\u65f6\u8fbe\u5230\u6700\u5dee\u60c5\u51b5\uff0c\u9700\u8981\u5c06 \\((n-1)^2\\) \u4e2a\u5143\u7d20\u201c\u5411\u5de6\u4e0a\u79fb\u52a8\u201d\uff0c\u4ece\u800c\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002
        • \u521d\u59cb\u5316\uff1a\u4f20\u5165 \\(n\\) \u4e2a\u9876\u70b9\uff0c\u521d\u59cb\u5316\u957f\u5ea6\u4e3a \\(n\\) \u7684\u9876\u70b9\u5217\u8868 vertices \uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff1b\u521d\u59cb\u5316 \\(n \\times n\\) \u5927\u5c0f\u7684\u90bb\u63a5\u77e9\u9635 adjMat \uff0c\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002
        \u521d\u59cb\u5316\u90bb\u63a5\u77e9\u9635\u6dfb\u52a0\u8fb9\u5220\u9664\u8fb9\u6dfb\u52a0\u9876\u70b9\u5220\u9664\u9876\u70b9

        \u56fe 9-7 \u00a0 \u90bb\u63a5\u77e9\u9635\u7684\u521d\u59cb\u5316\u3001\u589e\u5220\u8fb9\u3001\u589e\u5220\u9876\u70b9

        \u4ee5\u4e0b\u662f\u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u8868\u793a\u56fe\u7684\u5b9e\u73b0\u4ee3\u7801\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_adjacency_matrix.py
        class GraphAdjMat:\n    \"\"\"\u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\"\"\"\n\n    def __init__(self, vertices: list[int], edges: list[list[int]]):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        # \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n        self.vertices: list[int] = []\n        # \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n        self.adj_mat: list[list[int]] = []\n        # \u6dfb\u52a0\u9876\u70b9\n        for val in vertices:\n            self.add_vertex(val)\n        # \u6dfb\u52a0\u8fb9\n        # \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        for e in edges:\n            self.add_edge(e[0], e[1])\n\n    def size(self) -> int:\n        \"\"\"\u83b7\u53d6\u9876\u70b9\u6570\u91cf\"\"\"\n        return len(self.vertices)\n\n    def add_vertex(self, val: int):\n        \"\"\"\u6dfb\u52a0\u9876\u70b9\"\"\"\n        n = self.size()\n        # \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n        self.vertices.append(val)\n        # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n        new_row = [0] * n\n        self.adj_mat.append(new_row)\n        # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n        for row in self.adj_mat:\n            row.append(0)\n\n    def remove_vertex(self, index: int):\n        \"\"\"\u5220\u9664\u9876\u70b9\"\"\"\n        if index >= self.size():\n            raise IndexError()\n        # \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n        self.vertices.pop(index)\n        # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n        self.adj_mat.pop(index)\n        # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n        for row in self.adj_mat:\n            row.pop(index)\n\n    def add_edge(self, i: int, j: int):\n        \"\"\"\u6dfb\u52a0\u8fb9\"\"\"\n        # \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        # \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\n            raise IndexError()\n        # \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n        self.adj_mat[i][j] = 1\n        self.adj_mat[j][i] = 1\n\n    def remove_edge(self, i: int, j: int):\n        \"\"\"\u5220\u9664\u8fb9\"\"\"\n        # \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        # \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\n            raise IndexError()\n        self.adj_mat[i][j] = 0\n        self.adj_mat[j][i] = 0\n\n    def print(self):\n        \"\"\"\u6253\u5370\u90bb\u63a5\u77e9\u9635\"\"\"\n        print(\"\u9876\u70b9\u5217\u8868 =\", self.vertices)\n        print(\"\u90bb\u63a5\u77e9\u9635 =\")\n        print_matrix(self.adj_mat)\n
        graph_adjacency_matrix.cpp
        /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n    vector<int> vertices;       // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    vector<vector<int>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n  public:\n    /* \u6784\u9020\u65b9\u6cd5 */\n    GraphAdjMat(const vector<int> &vertices, const vector<vector<int>> &edges) {\n        // \u6dfb\u52a0\u9876\u70b9\n        for (int val : vertices) {\n            addVertex(val);\n        }\n        // \u6dfb\u52a0\u8fb9\n        // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        for (const vector<int> &edge : edges) {\n            addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    int size() const {\n        return vertices.size();\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    void addVertex(int val) {\n        int n = size();\n        // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n        vertices.push_back(val);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n        adjMat.emplace_back(vector<int>(n, 0));\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n        for (vector<int> &row : adjMat) {\n            row.push_back(0);\n        }\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    void removeVertex(int index) {\n        if (index >= size()) {\n            throw out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n        }\n        // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n        vertices.erase(vertices.begin() + index);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n        adjMat.erase(adjMat.begin() + index);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n        for (vector<int> &row : adjMat) {\n            row.erase(row.begin() + index);\n        }\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    void addEdge(int i, int j) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n            throw out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n        }\n        // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n        adjMat[i][j] = 1;\n        adjMat[j][i] = 1;\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    void removeEdge(int i, int j) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n            throw out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n        }\n        adjMat[i][j] = 0;\n        adjMat[j][i] = 0;\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n    void print() {\n        cout << \"\u9876\u70b9\u5217\u8868 = \";\n        printVector(vertices);\n        cout << \"\u90bb\u63a5\u77e9\u9635 =\" << endl;\n        printVectorMatrix(adjMat);\n    }\n};\n
        graph_adjacency_matrix.java
        /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n    List<Integer> vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    List<List<Integer>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public GraphAdjMat(int[] vertices, int[][] edges) {\n        this.vertices = new ArrayList<>();\n        this.adjMat = new ArrayList<>();\n        // \u6dfb\u52a0\u9876\u70b9\n        for (int val : vertices) {\n            addVertex(val);\n        }\n        // \u6dfb\u52a0\u8fb9\n        // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        for (int[] e : edges) {\n            addEdge(e[0], e[1]);\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    public int size() {\n        return vertices.size();\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    public void addVertex(int val) {\n        int n = size();\n        // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n        vertices.add(val);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n        List<Integer> newRow = new ArrayList<>(n);\n        for (int j = 0; j < n; j++) {\n            newRow.add(0);\n        }\n        adjMat.add(newRow);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n        for (List<Integer> row : adjMat) {\n            row.add(0);\n        }\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    public void removeVertex(int index) {\n        if (index >= size())\n            throw new IndexOutOfBoundsException();\n        // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n        vertices.remove(index);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n        adjMat.remove(index);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n        for (List<Integer> row : adjMat) {\n            row.remove(index);\n        }\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    public void addEdge(int i, int j) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw new IndexOutOfBoundsException();\n        // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n        adjMat.get(i).set(j, 1);\n        adjMat.get(j).set(i, 1);\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    public void removeEdge(int i, int j) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw new IndexOutOfBoundsException();\n        adjMat.get(i).set(j, 0);\n        adjMat.get(j).set(i, 0);\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n    public void print() {\n        System.out.print(\"\u9876\u70b9\u5217\u8868 = \");\n        System.out.println(vertices);\n        System.out.println(\"\u90bb\u63a5\u77e9\u9635 =\");\n        PrintUtil.printMatrix(adjMat);\n    }\n}\n
        graph_adjacency_matrix.cs
        /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n    List<int> vertices;     // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    List<List<int>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n    /* \u6784\u9020\u51fd\u6570 */\n    public GraphAdjMat(int[] vertices, int[][] edges) {\n        this.vertices = [];\n        this.adjMat = [];\n        // \u6dfb\u52a0\u9876\u70b9\n        foreach (int val in vertices) {\n            AddVertex(val);\n        }\n        // \u6dfb\u52a0\u8fb9\n        // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        foreach (int[] e in edges) {\n            AddEdge(e[0], e[1]);\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    int Size() {\n        return vertices.Count;\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    public void AddVertex(int val) {\n        int n = Size();\n        // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n        vertices.Add(val);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n        List<int> newRow = new(n);\n        for (int j = 0; j < n; j++) {\n            newRow.Add(0);\n        }\n        adjMat.Add(newRow);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n        foreach (List<int> row in adjMat) {\n            row.Add(0);\n        }\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    public void RemoveVertex(int index) {\n        if (index >= Size())\n            throw new IndexOutOfRangeException();\n        // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n        vertices.RemoveAt(index);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n        adjMat.RemoveAt(index);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n        foreach (List<int> row in adjMat) {\n            row.RemoveAt(index);\n        }\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    public void AddEdge(int i, int j) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)\n            throw new IndexOutOfRangeException();\n        // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n        adjMat[i][j] = 1;\n        adjMat[j][i] = 1;\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    public void RemoveEdge(int i, int j) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)\n            throw new IndexOutOfRangeException();\n        adjMat[i][j] = 0;\n        adjMat[j][i] = 0;\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n    public void Print() {\n        Console.Write(\"\u9876\u70b9\u5217\u8868 = \");\n        PrintUtil.PrintList(vertices);\n        Console.WriteLine(\"\u90bb\u63a5\u77e9\u9635 =\");\n        PrintUtil.PrintMatrix(adjMat);\n    }\n}\n
        graph_adjacency_matrix.go
        /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\ntype graphAdjMat struct {\n    // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    vertices []int\n    // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    adjMat [][]int\n}\n\n/* \u6784\u9020\u51fd\u6570 */\nfunc newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat {\n    // \u6dfb\u52a0\u9876\u70b9\n    n := len(vertices)\n    adjMat := make([][]int, n)\n    for i := range adjMat {\n        adjMat[i] = make([]int, n)\n    }\n    // \u521d\u59cb\u5316\u56fe\n    g := &graphAdjMat{\n        vertices: vertices,\n        adjMat:   adjMat,\n    }\n    // \u6dfb\u52a0\u8fb9\n    // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    for i := range edges {\n        g.addEdge(edges[i][0], edges[i][1])\n    }\n    return g\n}\n\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc (g *graphAdjMat) size() int {\n    return len(g.vertices)\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc (g *graphAdjMat) addVertex(val int) {\n    n := g.size()\n    // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n    g.vertices = append(g.vertices, val)\n    // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n    newRow := make([]int, n)\n    g.adjMat = append(g.adjMat, newRow)\n    // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n    for i := range g.adjMat {\n        g.adjMat[i] = append(g.adjMat[i], 0)\n    }\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nfunc (g *graphAdjMat) removeVertex(index int) {\n    if index >= g.size() {\n        return\n    }\n    // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n    g.vertices = append(g.vertices[:index], g.vertices[index+1:]...)\n    // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n    g.adjMat = append(g.adjMat[:index], g.adjMat[index+1:]...)\n    // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n    for i := range g.adjMat {\n        g.adjMat[i] = append(g.adjMat[i][:index], g.adjMat[i][index+1:]...)\n    }\n}\n\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc (g *graphAdjMat) addEdge(i, j int) {\n    // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n    if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\n        fmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n    }\n    // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n    g.adjMat[i][j] = 1\n    g.adjMat[j][i] = 1\n}\n\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc (g *graphAdjMat) removeEdge(i, j int) {\n    // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n    if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\n        fmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n    }\n    g.adjMat[i][j] = 0\n    g.adjMat[j][i] = 0\n}\n\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nfunc (g *graphAdjMat) print() {\n    fmt.Printf(\"\\t\u9876\u70b9\u5217\u8868 = %v\\n\", g.vertices)\n    fmt.Printf(\"\\t\u90bb\u63a5\u77e9\u9635 = \\n\")\n    for i := range g.adjMat {\n        fmt.Printf(\"\\t\\t\\t%v\\n\", g.adjMat[i])\n    }\n}\n
        graph_adjacency_matrix.swift
        /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n    private var vertices: [Int] // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    private var adjMat: [[Int]] // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    init(vertices: [Int], edges: [[Int]]) {\n        self.vertices = []\n        adjMat = []\n        // \u6dfb\u52a0\u9876\u70b9\n        for val in vertices {\n            addVertex(val: val)\n        }\n        // \u6dfb\u52a0\u8fb9\n        // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        for e in edges {\n            addEdge(i: e[0], j: e[1])\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    func size() -> Int {\n        vertices.count\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    func addVertex(val: Int) {\n        let n = size()\n        // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n        vertices.append(val)\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n        let newRow = Array(repeating: 0, count: n)\n        adjMat.append(newRow)\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n        for i in adjMat.indices {\n            adjMat[i].append(0)\n        }\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    func removeVertex(index: Int) {\n        if index >= size() {\n            fatalError(\"\u8d8a\u754c\")\n        }\n        // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n        vertices.remove(at: index)\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n        adjMat.remove(at: index)\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n        for i in adjMat.indices {\n            adjMat[i].remove(at: index)\n        }\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    func addEdge(i: Int, j: Int) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if i < 0 || j < 0 || i >= size() || j >= size() || i == j {\n            fatalError(\"\u8d8a\u754c\")\n        }\n        // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n        adjMat[i][j] = 1\n        adjMat[j][i] = 1\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    func removeEdge(i: Int, j: Int) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if i < 0 || j < 0 || i >= size() || j >= size() || i == j {\n            fatalError(\"\u8d8a\u754c\")\n        }\n        adjMat[i][j] = 0\n        adjMat[j][i] = 0\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n    func print() {\n        Swift.print(\"\u9876\u70b9\u5217\u8868 = \", terminator: \"\")\n        Swift.print(vertices)\n        Swift.print(\"\u90bb\u63a5\u77e9\u9635 =\")\n        PrintUtil.printMatrix(matrix: adjMat)\n    }\n}\n
        graph_adjacency_matrix.js
        /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n    vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n    /* \u6784\u9020\u51fd\u6570 */\n    constructor(vertices, edges) {\n        this.vertices = [];\n        this.adjMat = [];\n        // \u6dfb\u52a0\u9876\u70b9\n        for (const val of vertices) {\n            this.addVertex(val);\n        }\n        // \u6dfb\u52a0\u8fb9\n        // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        for (const e of edges) {\n            this.addEdge(e[0], e[1]);\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    size() {\n        return this.vertices.length;\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    addVertex(val) {\n        const n = this.size();\n        // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n        this.vertices.push(val);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n        const newRow = [];\n        for (let j = 0; j < n; j++) {\n            newRow.push(0);\n        }\n        this.adjMat.push(newRow);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n        for (const row of this.adjMat) {\n            row.push(0);\n        }\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    removeVertex(index) {\n        if (index >= this.size()) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n        this.vertices.splice(index, 1);\n\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n        this.adjMat.splice(index, 1);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n        for (const row of this.adjMat) {\n            row.splice(index, 1);\n        }\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    addEdge(i, j) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) === (j, i)\n        this.adjMat[i][j] = 1;\n        this.adjMat[j][i] = 1;\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    removeEdge(i, j) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        this.adjMat[i][j] = 0;\n        this.adjMat[j][i] = 0;\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n    print() {\n        console.log('\u9876\u70b9\u5217\u8868 = ', this.vertices);\n        console.log('\u90bb\u63a5\u77e9\u9635 =', this.adjMat);\n    }\n}\n
        graph_adjacency_matrix.ts
        /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n    vertices: number[]; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    adjMat: number[][]; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n    /* \u6784\u9020\u51fd\u6570 */\n    constructor(vertices: number[], edges: number[][]) {\n        this.vertices = [];\n        this.adjMat = [];\n        // \u6dfb\u52a0\u9876\u70b9\n        for (const val of vertices) {\n            this.addVertex(val);\n        }\n        // \u6dfb\u52a0\u8fb9\n        // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        for (const e of edges) {\n            this.addEdge(e[0], e[1]);\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    size(): number {\n        return this.vertices.length;\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    addVertex(val: number): void {\n        const n: number = this.size();\n        // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n        this.vertices.push(val);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n        const newRow: number[] = [];\n        for (let j: number = 0; j < n; j++) {\n            newRow.push(0);\n        }\n        this.adjMat.push(newRow);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n        for (const row of this.adjMat) {\n            row.push(0);\n        }\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    removeVertex(index: number): void {\n        if (index >= this.size()) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n        this.vertices.splice(index, 1);\n\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n        this.adjMat.splice(index, 1);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n        for (const row of this.adjMat) {\n            row.splice(index, 1);\n        }\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    addEdge(i: number, j: number): void {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) === (j, i)\n        this.adjMat[i][j] = 1;\n        this.adjMat[j][i] = 1;\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    removeEdge(i: number, j: number): void {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        this.adjMat[i][j] = 0;\n        this.adjMat[j][i] = 0;\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n    print(): void {\n        console.log('\u9876\u70b9\u5217\u8868 = ', this.vertices);\n        console.log('\u90bb\u63a5\u77e9\u9635 =', this.adjMat);\n    }\n}\n
        graph_adjacency_matrix.dart
        /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n  List<int> vertices = []; // \u9876\u70b9\u5143\u7d20\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n  List<List<int>> adjMat = []; //\u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n  /* \u6784\u9020\u65b9\u6cd5 */\n  GraphAdjMat(List<int> vertices, List<List<int>> edges) {\n    this.vertices = [];\n    this.adjMat = [];\n    // \u6dfb\u52a0\u9876\u70b9\n    for (int val in vertices) {\n      addVertex(val);\n    }\n    // \u6dfb\u52a0\u8fb9\n    // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    for (List<int> e in edges) {\n      addEdge(e[0], e[1]);\n    }\n  }\n\n  /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n  int size() {\n    return vertices.length;\n  }\n\n  /* \u6dfb\u52a0\u9876\u70b9 */\n  void addVertex(int val) {\n    int n = size();\n    // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n    vertices.add(val);\n    // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n    List<int> newRow = List.filled(n, 0, growable: true);\n    adjMat.add(newRow);\n    // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n    for (List<int> row in adjMat) {\n      row.add(0);\n    }\n  }\n\n  /* \u5220\u9664\u9876\u70b9 */\n  void removeVertex(int index) {\n    if (index >= size()) {\n      throw IndexError;\n    }\n    // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n    vertices.removeAt(index);\n    // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n    adjMat.removeAt(index);\n    // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n    for (List<int> row in adjMat) {\n      row.removeAt(index);\n    }\n  }\n\n  /* \u6dfb\u52a0\u8fb9 */\n  // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n  void addEdge(int i, int j) {\n    // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n    if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n      throw IndexError;\n    }\n    // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n    adjMat[i][j] = 1;\n    adjMat[j][i] = 1;\n  }\n\n  /* \u5220\u9664\u8fb9 */\n  // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n  void removeEdge(int i, int j) {\n    // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n    if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n      throw IndexError;\n    }\n    adjMat[i][j] = 0;\n    adjMat[j][i] = 0;\n  }\n\n  /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n  void printAdjMat() {\n    print(\"\u9876\u70b9\u5217\u8868 = $vertices\");\n    print(\"\u90bb\u63a5\u77e9\u9635 = \");\n    printMatrix(adjMat);\n  }\n}\n
        graph_adjacency_matrix.rs
        /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u578b */\npub struct GraphAdjMat {\n    // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    pub vertices: Vec<i32>,\n    // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    pub adj_mat: Vec<Vec<i32>>,\n}\n\nimpl GraphAdjMat {\n    /* \u6784\u9020\u65b9\u6cd5 */\n    pub fn new(vertices: Vec<i32>, edges: Vec<[usize; 2]>) -> Self {\n        let mut graph = GraphAdjMat {\n            vertices: vec![],\n            adj_mat: vec![],\n        };\n        // \u6dfb\u52a0\u9876\u70b9\n        for val in vertices {\n            graph.add_vertex(val);\n        }\n        // \u6dfb\u52a0\u8fb9\n        // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        for edge in edges {\n            graph.add_edge(edge[0], edge[1])\n        }\n\n        graph\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    pub fn size(&self) -> usize {\n        self.vertices.len()\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    pub fn add_vertex(&mut self, val: i32) {\n        let n = self.size();\n        // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n        self.vertices.push(val);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n        self.adj_mat.push(vec![0; n]);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n        for row in self.adj_mat.iter_mut() {\n            row.push(0);\n        }\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    pub fn remove_vertex(&mut self, index: usize) {\n        if index >= self.size() {\n            panic!(\"index error\")\n        }\n        // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n        self.vertices.remove(index);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n        self.adj_mat.remove(index);\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n        for row in self.adj_mat.iter_mut() {\n            row.remove(index);\n        }\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    pub fn add_edge(&mut self, i: usize, j: usize) {\n        // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if i >= self.size() || j >= self.size() || i == j {\n            panic!(\"index error\")\n        }\n        // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n        self.adj_mat[i][j] = 1;\n        self.adj_mat[j][i] = 1;\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    pub fn remove_edge(&mut self, i: usize, j: usize) {\n        // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if i >= self.size() || j >= self.size() || i == j {\n            panic!(\"index error\")\n        }\n        self.adj_mat[i][j] = 0;\n        self.adj_mat[j][i] = 0;\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n    pub fn print(&self) {\n        println!(\"\u9876\u70b9\u5217\u8868 = {:?}\", self.vertices);\n        println!(\"\u90bb\u63a5\u77e9\u9635 =\");\n        println!(\"[\");\n        for row in &self.adj_mat {\n            println!(\"  {:?},\", row);\n        }\n        println!(\"]\")\n    }\n}\n
        graph_adjacency_matrix.c
        /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7ed3\u6784\u4f53 */\ntypedef struct {\n    int vertices[MAX_SIZE];\n    int adjMat[MAX_SIZE][MAX_SIZE];\n    int size;\n} GraphAdjMat;\n\n/* \u6784\u9020\u51fd\u6570 */\nGraphAdjMat *newGraphAdjMat() {\n    GraphAdjMat *graph = (GraphAdjMat *)malloc(sizeof(GraphAdjMat));\n    graph->size = 0;\n    for (int i = 0; i < MAX_SIZE; i++) {\n        for (int j = 0; j < MAX_SIZE; j++) {\n            graph->adjMat[i][j] = 0;\n        }\n    }\n    return graph;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delGraphAdjMat(GraphAdjMat *graph) {\n    free(graph);\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(GraphAdjMat *graph, int val) {\n    if (graph->size == MAX_SIZE) {\n        fprintf(stderr, \"\u56fe\u7684\u9876\u70b9\u6570\u91cf\u5df2\u8fbe\u6700\u5927\u503c\\n\");\n        return;\n    }\n    // \u6dfb\u52a0\u7b2c n \u4e2a\u9876\u70b9\uff0c\u5e76\u5c06\u7b2c n \u884c\u548c\u5217\u7f6e\u96f6\n    int n = graph->size;\n    graph->vertices[n] = val;\n    for (int i = 0; i <= n; i++) {\n        graph->adjMat[n][i] = graph->adjMat[i][n] = 0;\n    }\n    graph->size++;\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(GraphAdjMat *graph, int index) {\n    if (index < 0 || index >= graph->size) {\n        fprintf(stderr, \"\u9876\u70b9\u7d22\u5f15\u8d8a\u754c\\n\");\n        return;\n    }\n    // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n    for (int i = index; i < graph->size - 1; i++) {\n        graph->vertices[i] = graph->vertices[i + 1];\n    }\n    // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n    for (int i = index; i < graph->size - 1; i++) {\n        for (int j = 0; j < graph->size; j++) {\n            graph->adjMat[i][j] = graph->adjMat[i + 1][j];\n        }\n    }\n    // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n    for (int i = 0; i < graph->size; i++) {\n        for (int j = index; j < graph->size - 1; j++) {\n            graph->adjMat[i][j] = graph->adjMat[i][j + 1];\n        }\n    }\n    graph->size--;\n}\n\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid addEdge(GraphAdjMat *graph, int i, int j) {\n    if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n        fprintf(stderr, \"\u8fb9\u7d22\u5f15\u8d8a\u754c\u6216\u76f8\u7b49\\n\");\n        return;\n    }\n    graph->adjMat[i][j] = 1;\n    graph->adjMat[j][i] = 1;\n}\n\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid removeEdge(GraphAdjMat *graph, int i, int j) {\n    if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n        fprintf(stderr, \"\u8fb9\u7d22\u5f15\u8d8a\u754c\u6216\u76f8\u7b49\\n\");\n        return;\n    }\n    graph->adjMat[i][j] = 0;\n    graph->adjMat[j][i] = 0;\n}\n\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nvoid printGraphAdjMat(GraphAdjMat *graph) {\n    printf(\"\u9876\u70b9\u5217\u8868 = \");\n    printArray(graph->vertices, graph->size);\n    printf(\"\u90bb\u63a5\u77e9\u9635 =\\n\");\n    for (int i = 0; i < graph->size; i++) {\n        printArray(graph->adjMat[i], graph->size);\n    }\n}\n
        graph_adjacency_matrix.kt
        /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {\n    val vertices = mutableListOf<Int>() // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    val adjMat = mutableListOf<MutableList<Int>>() // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    init {\n        // \u6dfb\u52a0\u9876\u70b9\n        for (vertex in vertices) {\n            addVertex(vertex)\n        }\n        // \u6dfb\u52a0\u8fb9\n        // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n        for (edge in edges) {\n            addEdge(edge[0], edge[1])\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    fun size(): Int {\n        return vertices.size\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    fun addVertex(_val: Int) {\n        val n = size()\n        // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n        vertices.add(_val)\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n        val newRow = mutableListOf<Int>()\n        for (j in 0..<n) {\n            newRow.add(0)\n        }\n        adjMat.add(newRow)\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n        for (row in adjMat) {\n            row.add(0)\n        }\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    fun removeVertex(index: Int) {\n        if (index >= size())\n            throw IndexOutOfBoundsException()\n        // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n        vertices.removeAt(index)\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n        adjMat.removeAt(index)\n        // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n        for (row in adjMat) {\n            row.removeAt(index)\n        }\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    fun addEdge(i: Int, j: Int) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw IndexOutOfBoundsException()\n        // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n        adjMat[i][j] = 1\n        adjMat[j][i] = 1\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    fun removeEdge(i: Int, j: Int) {\n        // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw IndexOutOfBoundsException()\n        adjMat[i][j] = 0\n        adjMat[j][i] = 0\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n    fun print() {\n        print(\"\u9876\u70b9\u5217\u8868 = \")\n        println(vertices)\n        println(\"\u90bb\u63a5\u77e9\u9635 =\")\n        printMatrix(adjMat)\n    }\n}\n
        graph_adjacency_matrix.rb
        ### \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b ###\nclass GraphAdjMat\n  def initialize(vertices, edges)\n    ### \u6784\u9020\u65b9\u6cd5 ###\n    # \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    @vertices = []\n    # \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n    @adj_mat = []\n    # \u6dfb\u52a0\u9876\u70b9\n    vertices.each { |val| add_vertex(val) }\n    # \u6dfb\u52a0\u8fb9\n    # \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    edges.each { |e| add_edge(e[0], e[1]) }\n  end\n\n  ### \u83b7\u53d6\u9876\u70b9\u6570\u91cf ###\n  def size\n    @vertices.length\n  end\n\n  ### \u6dfb\u52a0\u9876\u70b9 ###\n  def add_vertex(val)\n    n = size\n    # \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n    @vertices << val\n    # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n    new_row = Array.new(n, 0)\n    @adj_mat << new_row\n    # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n    @adj_mat.each { |row| row << 0 }\n  end\n\n  ### \u5220\u9664\u9876\u70b9 ###\n  def remove_vertex(index)\n    raise IndexError if index >= size\n\n    # \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n    @vertices.delete_at(index)\n    # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n    @adj_mat.delete_at(index)\n    # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n    @adj_mat.each { |row| row.delete_at(index) }\n  end\n\n  ### \u6dfb\u52a0\u8fb9 ###\n  def add_edge(i, j)\n    # \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    # \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n    if i < 0 || j < 0 || i >= size || j >= size || i == j\n      raise IndexError\n    end\n    # \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n    @adj_mat[i][j] = 1\n    @adj_mat[j][i] = 1\n  end\n\n  ### \u5220\u9664\u8fb9 ###\n  def remove_edge(i, j)\n    # \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n    # \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n    if i < 0 || j < 0 || i >= size || j >= size || i == j\n      raise IndexError\n    end\n    @adj_mat[i][j] = 0\n    @adj_mat[j][i] = 0\n  end\n\n  ### \u6253\u5370\u90bb\u63a5\u77e9\u9635 ###\n  def __print__\n    puts \"\u9876\u70b9\u5217\u8868 = #{@vertices}\"\n    puts '\u90bb\u63a5\u77e9\u9635 ='\n    print_matrix(@adj_mat)\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_graph/graph_operations/#922","title":"9.2.2 \u00a0 \u57fa\u4e8e\u90bb\u63a5\u8868\u7684\u5b9e\u73b0","text":"

        \u8bbe\u65e0\u5411\u56fe\u7684\u9876\u70b9\u603b\u6570\u4e3a \\(n\\)\u3001\u8fb9\u603b\u6570\u4e3a \\(m\\) \uff0c\u5219\u53ef\u6839\u636e\u56fe 9-8 \u6240\u793a\u7684\u65b9\u6cd5\u5b9e\u73b0\u5404\u79cd\u64cd\u4f5c\u3002

        • \u6dfb\u52a0\u8fb9\uff1a\u5728\u9876\u70b9\u5bf9\u5e94\u94fe\u8868\u7684\u672b\u5c3e\u6dfb\u52a0\u8fb9\u5373\u53ef\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002\u56e0\u4e3a\u662f\u65e0\u5411\u56fe\uff0c\u6240\u4ee5\u9700\u8981\u540c\u65f6\u6dfb\u52a0\u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u3002
        • \u5220\u9664\u8fb9\uff1a\u5728\u9876\u70b9\u5bf9\u5e94\u94fe\u8868\u4e2d\u67e5\u627e\u5e76\u5220\u9664\u6307\u5b9a\u8fb9\uff0c\u4f7f\u7528 \\(O(m)\\) \u65f6\u95f4\u3002\u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u9700\u8981\u540c\u65f6\u5220\u9664\u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u3002
        • \u6dfb\u52a0\u9876\u70b9\uff1a\u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u94fe\u8868\uff0c\u5e76\u5c06\u65b0\u589e\u9876\u70b9\u4f5c\u4e3a\u94fe\u8868\u5934\u8282\u70b9\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002
        • \u5220\u9664\u9876\u70b9\uff1a\u9700\u904d\u5386\u6574\u4e2a\u90bb\u63a5\u8868\uff0c\u5220\u9664\u5305\u542b\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u8fb9\uff0c\u4f7f\u7528 \\(O(n + m)\\) \u65f6\u95f4\u3002
        • \u521d\u59cb\u5316\uff1a\u5728\u90bb\u63a5\u8868\u4e2d\u521b\u5efa \\(n\\) \u4e2a\u9876\u70b9\u548c \\(2m\\) \u6761\u8fb9\uff0c\u4f7f\u7528 \\(O(n + m)\\) \u65f6\u95f4\u3002
        \u521d\u59cb\u5316\u90bb\u63a5\u8868\u6dfb\u52a0\u8fb9\u5220\u9664\u8fb9\u6dfb\u52a0\u9876\u70b9\u5220\u9664\u9876\u70b9

        \u56fe 9-8 \u00a0 \u90bb\u63a5\u8868\u7684\u521d\u59cb\u5316\u3001\u589e\u5220\u8fb9\u3001\u589e\u5220\u9876\u70b9

        \u4ee5\u4e0b\u662f\u90bb\u63a5\u8868\u7684\u4ee3\u7801\u5b9e\u73b0\u3002\u5bf9\u6bd4\u56fe 9-8 \uff0c\u5b9e\u9645\u4ee3\u7801\u6709\u4ee5\u4e0b\u4e0d\u540c\u3002

        • \u4e3a\u4e86\u65b9\u4fbf\u6dfb\u52a0\u4e0e\u5220\u9664\u9876\u70b9\uff0c\u4ee5\u53ca\u7b80\u5316\u4ee3\u7801\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\u6765\u4ee3\u66ff\u94fe\u8868\u3002
        • \u4f7f\u7528\u54c8\u5e0c\u8868\u6765\u5b58\u50a8\u90bb\u63a5\u8868\uff0ckey \u4e3a\u9876\u70b9\u5b9e\u4f8b\uff0cvalue \u4e3a\u8be5\u9876\u70b9\u7684\u90bb\u63a5\u9876\u70b9\u5217\u8868\uff08\u94fe\u8868\uff09\u3002

        \u53e6\u5916\uff0c\u6211\u4eec\u5728\u90bb\u63a5\u8868\u4e2d\u4f7f\u7528 Vertex \u7c7b\u6765\u8868\u793a\u9876\u70b9\uff0c\u8fd9\u6837\u505a\u7684\u539f\u56e0\u662f\uff1a\u5982\u679c\u4e0e\u90bb\u63a5\u77e9\u9635\u4e00\u6837\uff0c\u7528\u5217\u8868\u7d22\u5f15\u6765\u533a\u5206\u4e0d\u540c\u9876\u70b9\uff0c\u90a3\u4e48\u5047\u8bbe\u8981\u5220\u9664\u7d22\u5f15\u4e3a \\(i\\) \u7684\u9876\u70b9\uff0c\u5219\u9700\u904d\u5386\u6574\u4e2a\u90bb\u63a5\u8868\uff0c\u5c06\u6240\u6709\u5927\u4e8e \\(i\\) \u7684\u7d22\u5f15\u5168\u90e8\u51cf \\(1\\) \uff0c\u6548\u7387\u5f88\u4f4e\u3002\u800c\u5982\u679c\u6bcf\u4e2a\u9876\u70b9\u90fd\u662f\u552f\u4e00\u7684 Vertex \u5b9e\u4f8b\uff0c\u5220\u9664\u67d0\u4e00\u9876\u70b9\u4e4b\u540e\u5c31\u65e0\u987b\u6539\u52a8\u5176\u4ed6\u9876\u70b9\u4e86\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_adjacency_list.py
        class GraphAdjList:\n    \"\"\"\u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\"\"\"\n\n    def __init__(self, edges: list[list[Vertex]]):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        # \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n        self.adj_list = dict[Vertex, list[Vertex]]()\n        # \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n        for edge in edges:\n            self.add_vertex(edge[0])\n            self.add_vertex(edge[1])\n            self.add_edge(edge[0], edge[1])\n\n    def size(self) -> int:\n        \"\"\"\u83b7\u53d6\u9876\u70b9\u6570\u91cf\"\"\"\n        return len(self.adj_list)\n\n    def add_edge(self, vet1: Vertex, vet2: Vertex):\n        \"\"\"\u6dfb\u52a0\u8fb9\"\"\"\n        if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n            raise ValueError()\n        # \u6dfb\u52a0\u8fb9 vet1 - vet2\n        self.adj_list[vet1].append(vet2)\n        self.adj_list[vet2].append(vet1)\n\n    def remove_edge(self, vet1: Vertex, vet2: Vertex):\n        \"\"\"\u5220\u9664\u8fb9\"\"\"\n        if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n            raise ValueError()\n        # \u5220\u9664\u8fb9 vet1 - vet2\n        self.adj_list[vet1].remove(vet2)\n        self.adj_list[vet2].remove(vet1)\n\n    def add_vertex(self, vet: Vertex):\n        \"\"\"\u6dfb\u52a0\u9876\u70b9\"\"\"\n        if vet in self.adj_list:\n            return\n        # \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n        self.adj_list[vet] = []\n\n    def remove_vertex(self, vet: Vertex):\n        \"\"\"\u5220\u9664\u9876\u70b9\"\"\"\n        if vet not in self.adj_list:\n            raise ValueError()\n        # \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n        self.adj_list.pop(vet)\n        # \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n        for vertex in self.adj_list:\n            if vet in self.adj_list[vertex]:\n                self.adj_list[vertex].remove(vet)\n\n    def print(self):\n        \"\"\"\u6253\u5370\u90bb\u63a5\u8868\"\"\"\n        print(\"\u90bb\u63a5\u8868 =\")\n        for vertex in self.adj_list:\n            tmp = [v.val for v in self.adj_list[vertex]]\n            print(f\"{vertex.val}: {tmp},\")\n
        graph_adjacency_list.cpp
        /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n  public:\n    // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    unordered_map<Vertex *, vector<Vertex *>> adjList;\n\n    /* \u5728 vector \u4e2d\u5220\u9664\u6307\u5b9a\u8282\u70b9 */\n    void remove(vector<Vertex *> &vec, Vertex *vet) {\n        for (int i = 0; i < vec.size(); i++) {\n            if (vec[i] == vet) {\n                vec.erase(vec.begin() + i);\n                break;\n            }\n        }\n    }\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    GraphAdjList(const vector<vector<Vertex *>> &edges) {\n        // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n        for (const vector<Vertex *> &edge : edges) {\n            addVertex(edge[0]);\n            addVertex(edge[1]);\n            addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    int size() {\n        return adjList.size();\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    void addEdge(Vertex *vet1, Vertex *vet2) {\n        if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n            throw invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n        // \u6dfb\u52a0\u8fb9 vet1 - vet2\n        adjList[vet1].push_back(vet2);\n        adjList[vet2].push_back(vet1);\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    void removeEdge(Vertex *vet1, Vertex *vet2) {\n        if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n            throw invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n        // \u5220\u9664\u8fb9 vet1 - vet2\n        remove(adjList[vet1], vet2);\n        remove(adjList[vet2], vet1);\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    void addVertex(Vertex *vet) {\n        if (adjList.count(vet))\n            return;\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n        adjList[vet] = vector<Vertex *>();\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    void removeVertex(Vertex *vet) {\n        if (!adjList.count(vet))\n            throw invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n        adjList.erase(vet);\n        // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n        for (auto &adj : adjList) {\n            remove(adj.second, vet);\n        }\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u8868 */\n    void print() {\n        cout << \"\u90bb\u63a5\u8868 =\" << endl;\n        for (auto &adj : adjList) {\n            const auto &key = adj.first;\n            const auto &vec = adj.second;\n            cout << key->val << \": \";\n            printVector(vetsToVals(vec));\n        }\n    }\n};\n
        graph_adjacency_list.java
        /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n    // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    Map<Vertex, List<Vertex>> adjList;\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public GraphAdjList(Vertex[][] edges) {\n        this.adjList = new HashMap<>();\n        // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n        for (Vertex[] edge : edges) {\n            addVertex(edge[0]);\n            addVertex(edge[1]);\n            addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    public int size() {\n        return adjList.size();\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    public void addEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw new IllegalArgumentException();\n        // \u6dfb\u52a0\u8fb9 vet1 - vet2\n        adjList.get(vet1).add(vet2);\n        adjList.get(vet2).add(vet1);\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    public void removeEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw new IllegalArgumentException();\n        // \u5220\u9664\u8fb9 vet1 - vet2\n        adjList.get(vet1).remove(vet2);\n        adjList.get(vet2).remove(vet1);\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    public void addVertex(Vertex vet) {\n        if (adjList.containsKey(vet))\n            return;\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n        adjList.put(vet, new ArrayList<>());\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    public void removeVertex(Vertex vet) {\n        if (!adjList.containsKey(vet))\n            throw new IllegalArgumentException();\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n        adjList.remove(vet);\n        // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n        for (List<Vertex> list : adjList.values()) {\n            list.remove(vet);\n        }\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u8868 */\n    public void print() {\n        System.out.println(\"\u90bb\u63a5\u8868 =\");\n        for (Map.Entry<Vertex, List<Vertex>> pair : adjList.entrySet()) {\n            List<Integer> tmp = new ArrayList<>();\n            for (Vertex vertex : pair.getValue())\n                tmp.add(vertex.val);\n            System.out.println(pair.getKey().val + \": \" + tmp + \",\");\n        }\n    }\n}\n
        graph_adjacency_list.cs
        /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n    // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    public Dictionary<Vertex, List<Vertex>> adjList;\n\n    /* \u6784\u9020\u51fd\u6570 */\n    public GraphAdjList(Vertex[][] edges) {\n        adjList = [];\n        // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n        foreach (Vertex[] edge in edges) {\n            AddVertex(edge[0]);\n            AddVertex(edge[1]);\n            AddEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    int Size() {\n        return adjList.Count;\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    public void AddEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\n            throw new InvalidOperationException();\n        // \u6dfb\u52a0\u8fb9 vet1 - vet2\n        adjList[vet1].Add(vet2);\n        adjList[vet2].Add(vet1);\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    public void RemoveEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\n            throw new InvalidOperationException();\n        // \u5220\u9664\u8fb9 vet1 - vet2\n        adjList[vet1].Remove(vet2);\n        adjList[vet2].Remove(vet1);\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    public void AddVertex(Vertex vet) {\n        if (adjList.ContainsKey(vet))\n            return;\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n        adjList.Add(vet, []);\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    public void RemoveVertex(Vertex vet) {\n        if (!adjList.ContainsKey(vet))\n            throw new InvalidOperationException();\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n        adjList.Remove(vet);\n        // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n        foreach (List<Vertex> list in adjList.Values) {\n            list.Remove(vet);\n        }\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u8868 */\n    public void Print() {\n        Console.WriteLine(\"\u90bb\u63a5\u8868 =\");\n        foreach (KeyValuePair<Vertex, List<Vertex>> pair in adjList) {\n            List<int> tmp = [];\n            foreach (Vertex vertex in pair.Value)\n                tmp.Add(vertex.val);\n            Console.WriteLine(pair.Key.val + \": [\" + string.Join(\", \", tmp) + \"],\");\n        }\n    }\n}\n
        graph_adjacency_list.go
        /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\ntype graphAdjList struct {\n    // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    adjList map[Vertex][]Vertex\n}\n\n/* \u6784\u9020\u51fd\u6570 */\nfunc newGraphAdjList(edges [][]Vertex) *graphAdjList {\n    g := &graphAdjList{\n        adjList: make(map[Vertex][]Vertex),\n    }\n    // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n    for _, edge := range edges {\n        g.addVertex(edge[0])\n        g.addVertex(edge[1])\n        g.addEdge(edge[0], edge[1])\n    }\n    return g\n}\n\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc (g *graphAdjList) size() int {\n    return len(g.adjList)\n}\n\n/* \u6dfb\u52a0\u8fb9 */\nfunc (g *graphAdjList) addEdge(vet1 Vertex, vet2 Vertex) {\n    _, ok1 := g.adjList[vet1]\n    _, ok2 := g.adjList[vet2]\n    if !ok1 || !ok2 || vet1 == vet2 {\n        panic(\"error\")\n    }\n    // \u6dfb\u52a0\u8fb9 vet1 - vet2, \u6dfb\u52a0\u533f\u540d struct{},\n    g.adjList[vet1] = append(g.adjList[vet1], vet2)\n    g.adjList[vet2] = append(g.adjList[vet2], vet1)\n}\n\n/* \u5220\u9664\u8fb9 */\nfunc (g *graphAdjList) removeEdge(vet1 Vertex, vet2 Vertex) {\n    _, ok1 := g.adjList[vet1]\n    _, ok2 := g.adjList[vet2]\n    if !ok1 || !ok2 || vet1 == vet2 {\n        panic(\"error\")\n    }\n    // \u5220\u9664\u8fb9 vet1 - vet2\n    g.adjList[vet1] = DeleteSliceElms(g.adjList[vet1], vet2)\n    g.adjList[vet2] = DeleteSliceElms(g.adjList[vet2], vet1)\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc (g *graphAdjList) addVertex(vet Vertex) {\n    _, ok := g.adjList[vet]\n    if ok {\n        return\n    }\n    // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n    g.adjList[vet] = make([]Vertex, 0)\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nfunc (g *graphAdjList) removeVertex(vet Vertex) {\n    _, ok := g.adjList[vet]\n    if !ok {\n        panic(\"error\")\n    }\n    // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n    delete(g.adjList, vet)\n    // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n    for v, list := range g.adjList {\n        g.adjList[v] = DeleteSliceElms(list, vet)\n    }\n}\n\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nfunc (g *graphAdjList) print() {\n    var builder strings.Builder\n    fmt.Printf(\"\u90bb\u63a5\u8868 = \\n\")\n    for k, v := range g.adjList {\n        builder.WriteString(\"\\t\\t\" + strconv.Itoa(k.Val) + \": \")\n        for _, vet := range v {\n            builder.WriteString(strconv.Itoa(vet.Val) + \" \")\n        }\n        fmt.Println(builder.String())\n        builder.Reset()\n    }\n}\n
        graph_adjacency_list.swift
        /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n    // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    public private(set) var adjList: [Vertex: [Vertex]]\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public init(edges: [[Vertex]]) {\n        adjList = [:]\n        // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n        for edge in edges {\n            addVertex(vet: edge[0])\n            addVertex(vet: edge[1])\n            addEdge(vet1: edge[0], vet2: edge[1])\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    public func size() -> Int {\n        adjList.count\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    public func addEdge(vet1: Vertex, vet2: Vertex) {\n        if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\n            fatalError(\"\u53c2\u6570\u9519\u8bef\")\n        }\n        // \u6dfb\u52a0\u8fb9 vet1 - vet2\n        adjList[vet1]?.append(vet2)\n        adjList[vet2]?.append(vet1)\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    public func removeEdge(vet1: Vertex, vet2: Vertex) {\n        if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\n            fatalError(\"\u53c2\u6570\u9519\u8bef\")\n        }\n        // \u5220\u9664\u8fb9 vet1 - vet2\n        adjList[vet1]?.removeAll { $0 == vet2 }\n        adjList[vet2]?.removeAll { $0 == vet1 }\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    public func addVertex(vet: Vertex) {\n        if adjList[vet] != nil {\n            return\n        }\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n        adjList[vet] = []\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    public func removeVertex(vet: Vertex) {\n        if adjList[vet] == nil {\n            fatalError(\"\u53c2\u6570\u9519\u8bef\")\n        }\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n        adjList.removeValue(forKey: vet)\n        // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n        for key in adjList.keys {\n            adjList[key]?.removeAll { $0 == vet }\n        }\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u8868 */\n    public func print() {\n        Swift.print(\"\u90bb\u63a5\u8868 =\")\n        for (vertex, list) in adjList {\n            let list = list.map { $0.val }\n            Swift.print(\"\\(vertex.val): \\(list),\")\n        }\n    }\n}\n
        graph_adjacency_list.js
        /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n    // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    adjList;\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor(edges) {\n        this.adjList = new Map();\n        // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n        for (const edge of edges) {\n            this.addVertex(edge[0]);\n            this.addVertex(edge[1]);\n            this.addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    size() {\n        return this.adjList.size;\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    addEdge(vet1, vet2) {\n        if (\n            !this.adjList.has(vet1) ||\n            !this.adjList.has(vet2) ||\n            vet1 === vet2\n        ) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // \u6dfb\u52a0\u8fb9 vet1 - vet2\n        this.adjList.get(vet1).push(vet2);\n        this.adjList.get(vet2).push(vet1);\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    removeEdge(vet1, vet2) {\n        if (\n            !this.adjList.has(vet1) ||\n            !this.adjList.has(vet2) ||\n            vet1 === vet2 ||\n            this.adjList.get(vet1).indexOf(vet2) === -1\n        ) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // \u5220\u9664\u8fb9 vet1 - vet2\n        this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\n        this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    addVertex(vet) {\n        if (this.adjList.has(vet)) return;\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n        this.adjList.set(vet, []);\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    removeVertex(vet) {\n        if (!this.adjList.has(vet)) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n        this.adjList.delete(vet);\n        // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n        for (const set of this.adjList.values()) {\n            const index = set.indexOf(vet);\n            if (index > -1) {\n                set.splice(index, 1);\n            }\n        }\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u8868 */\n    print() {\n        console.log('\u90bb\u63a5\u8868 =');\n        for (const [key, value] of this.adjList) {\n            const tmp = [];\n            for (const vertex of value) {\n                tmp.push(vertex.val);\n            }\n            console.log(key.val + ': ' + tmp.join());\n        }\n    }\n}\n
        graph_adjacency_list.ts
        /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n    // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    adjList: Map<Vertex, Vertex[]>;\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor(edges: Vertex[][]) {\n        this.adjList = new Map();\n        // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n        for (const edge of edges) {\n            this.addVertex(edge[0]);\n            this.addVertex(edge[1]);\n            this.addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    size(): number {\n        return this.adjList.size;\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    addEdge(vet1: Vertex, vet2: Vertex): void {\n        if (\n            !this.adjList.has(vet1) ||\n            !this.adjList.has(vet2) ||\n            vet1 === vet2\n        ) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // \u6dfb\u52a0\u8fb9 vet1 - vet2\n        this.adjList.get(vet1).push(vet2);\n        this.adjList.get(vet2).push(vet1);\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    removeEdge(vet1: Vertex, vet2: Vertex): void {\n        if (\n            !this.adjList.has(vet1) ||\n            !this.adjList.has(vet2) ||\n            vet1 === vet2 ||\n            this.adjList.get(vet1).indexOf(vet2) === -1\n        ) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // \u5220\u9664\u8fb9 vet1 - vet2\n        this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\n        this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    addVertex(vet: Vertex): void {\n        if (this.adjList.has(vet)) return;\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n        this.adjList.set(vet, []);\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    removeVertex(vet: Vertex): void {\n        if (!this.adjList.has(vet)) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n        this.adjList.delete(vet);\n        // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n        for (const set of this.adjList.values()) {\n            const index: number = set.indexOf(vet);\n            if (index > -1) {\n                set.splice(index, 1);\n            }\n        }\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u8868 */\n    print(): void {\n        console.log('\u90bb\u63a5\u8868 =');\n        for (const [key, value] of this.adjList.entries()) {\n            const tmp = [];\n            for (const vertex of value) {\n                tmp.push(vertex.val);\n            }\n            console.log(key.val + ': ' + tmp.join());\n        }\n    }\n}\n
        graph_adjacency_list.dart
        /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n  // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n  Map<Vertex, List<Vertex>> adjList = {};\n\n  /* \u6784\u9020\u65b9\u6cd5 */\n  GraphAdjList(List<List<Vertex>> edges) {\n    for (List<Vertex> edge in edges) {\n      addVertex(edge[0]);\n      addVertex(edge[1]);\n      addEdge(edge[0], edge[1]);\n    }\n  }\n\n  /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n  int size() {\n    return adjList.length;\n  }\n\n  /* \u6dfb\u52a0\u8fb9 */\n  void addEdge(Vertex vet1, Vertex vet2) {\n    if (!adjList.containsKey(vet1) ||\n        !adjList.containsKey(vet2) ||\n        vet1 == vet2) {\n      throw ArgumentError;\n    }\n    // \u6dfb\u52a0\u8fb9 vet1 - vet2\n    adjList[vet1]!.add(vet2);\n    adjList[vet2]!.add(vet1);\n  }\n\n  /* \u5220\u9664\u8fb9 */\n  void removeEdge(Vertex vet1, Vertex vet2) {\n    if (!adjList.containsKey(vet1) ||\n        !adjList.containsKey(vet2) ||\n        vet1 == vet2) {\n      throw ArgumentError;\n    }\n    // \u5220\u9664\u8fb9 vet1 - vet2\n    adjList[vet1]!.remove(vet2);\n    adjList[vet2]!.remove(vet1);\n  }\n\n  /* \u6dfb\u52a0\u9876\u70b9 */\n  void addVertex(Vertex vet) {\n    if (adjList.containsKey(vet)) return;\n    // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n    adjList[vet] = [];\n  }\n\n  /* \u5220\u9664\u9876\u70b9 */\n  void removeVertex(Vertex vet) {\n    if (!adjList.containsKey(vet)) {\n      throw ArgumentError;\n    }\n    // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n    adjList.remove(vet);\n    // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n    adjList.forEach((key, value) {\n      value.remove(vet);\n    });\n  }\n\n  /* \u6253\u5370\u90bb\u63a5\u8868 */\n  void printAdjList() {\n    print(\"\u90bb\u63a5\u8868 =\");\n    adjList.forEach((key, value) {\n      List<int> tmp = [];\n      for (Vertex vertex in value) {\n        tmp.add(vertex.val);\n      }\n      print(\"${key.val}: $tmp,\");\n    });\n  }\n}\n
        graph_adjacency_list.rs
        /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u578b */\npub struct GraphAdjList {\n    // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    pub adj_list: HashMap<Vertex, Vec<Vertex>>, // maybe HashSet<Vertex> for value part is better?\n}\n\nimpl GraphAdjList {\n    /* \u6784\u9020\u65b9\u6cd5 */\n    pub fn new(edges: Vec<[Vertex; 2]>) -> Self {\n        let mut graph = GraphAdjList {\n            adj_list: HashMap::new(),\n        };\n        // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n        for edge in edges {\n            graph.add_vertex(edge[0]);\n            graph.add_vertex(edge[1]);\n            graph.add_edge(edge[0], edge[1]);\n        }\n\n        graph\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    #[allow(unused)]\n    pub fn size(&self) -> usize {\n        self.adj_list.len()\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    pub fn add_edge(&mut self, vet1: Vertex, vet2: Vertex) {\n        if vet1 == vet2 {\n            panic!(\"value error\");\n        }\n        // \u6dfb\u52a0\u8fb9 vet1 - vet2\n        self.adj_list.entry(vet1).or_default().push(vet2);\n        self.adj_list.entry(vet2).or_default().push(vet1);\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    #[allow(unused)]\n    pub fn remove_edge(&mut self, vet1: Vertex, vet2: Vertex) {\n        if vet1 == vet2 {\n            panic!(\"value error\");\n        }\n        // \u5220\u9664\u8fb9 vet1 - vet2\n        self.adj_list\n            .entry(vet1)\n            .and_modify(|v| v.retain(|&e| e != vet2));\n        self.adj_list\n            .entry(vet2)\n            .and_modify(|v| v.retain(|&e| e != vet1));\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    pub fn add_vertex(&mut self, vet: Vertex) {\n        if self.adj_list.contains_key(&vet) {\n            return;\n        }\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n        self.adj_list.insert(vet, vec![]);\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    #[allow(unused)]\n    pub fn remove_vertex(&mut self, vet: Vertex) {\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n        self.adj_list.remove(&vet);\n        // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n        for list in self.adj_list.values_mut() {\n            list.retain(|&v| v != vet);\n        }\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u8868 */\n    pub fn print(&self) {\n        println!(\"\u90bb\u63a5\u8868 =\");\n        for (vertex, list) in &self.adj_list {\n            let list = list.iter().map(|vertex| vertex.val).collect::<Vec<i32>>();\n            println!(\"{}: {:?},\", vertex.val, list);\n        }\n    }\n}\n
        graph_adjacency_list.c
        /* \u8282\u70b9\u7ed3\u6784\u4f53 */\ntypedef struct AdjListNode {\n    Vertex *vertex;           // \u9876\u70b9\n    struct AdjListNode *next; // \u540e\u7ee7\u8282\u70b9\n} AdjListNode;\n\n/* \u67e5\u627e\u9876\u70b9\u5bf9\u5e94\u7684\u8282\u70b9 */\nAdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {\n    for (int i = 0; i < graph->size; i++) {\n        if (graph->heads[i]->vertex == vet) {\n            return graph->heads[i];\n        }\n    }\n    return NULL;\n}\n\n/* \u6dfb\u52a0\u8fb9\u8f85\u52a9\u51fd\u6570 */\nvoid addEdgeHelper(AdjListNode *head, Vertex *vet) {\n    AdjListNode *node = (AdjListNode *)malloc(sizeof(AdjListNode));\n    node->vertex = vet;\n    // \u5934\u63d2\u6cd5\n    node->next = head->next;\n    head->next = node;\n}\n\n/* \u5220\u9664\u8fb9\u8f85\u52a9\u51fd\u6570 */\nvoid removeEdgeHelper(AdjListNode *head, Vertex *vet) {\n    AdjListNode *pre = head;\n    AdjListNode *cur = head->next;\n    // \u5728\u94fe\u8868\u4e2d\u641c\u7d22 vet \u5bf9\u5e94\u8282\u70b9\n    while (cur != NULL && cur->vertex != vet) {\n        pre = cur;\n        cur = cur->next;\n    }\n    if (cur == NULL)\n        return;\n    // \u5c06 vet \u5bf9\u5e94\u8282\u70b9\u4ece\u94fe\u8868\u4e2d\u5220\u9664\n    pre->next = cur->next;\n    // \u91ca\u653e\u5185\u5b58\n    free(cur);\n}\n\n/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\ntypedef struct {\n    AdjListNode *heads[MAX_SIZE]; // \u8282\u70b9\u6570\u7ec4\n    int size;                     // \u8282\u70b9\u6570\u91cf\n} GraphAdjList;\n\n/* \u6784\u9020\u51fd\u6570 */\nGraphAdjList *newGraphAdjList() {\n    GraphAdjList *graph = (GraphAdjList *)malloc(sizeof(GraphAdjList));\n    if (!graph) {\n        return NULL;\n    }\n    graph->size = 0;\n    for (int i = 0; i < MAX_SIZE; i++) {\n        graph->heads[i] = NULL;\n    }\n    return graph;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delGraphAdjList(GraphAdjList *graph) {\n    for (int i = 0; i < graph->size; i++) {\n        AdjListNode *cur = graph->heads[i];\n        while (cur != NULL) {\n            AdjListNode *next = cur->next;\n            if (cur != graph->heads[i]) {\n                free(cur);\n            }\n            cur = next;\n        }\n        free(graph->heads[i]->vertex);\n        free(graph->heads[i]);\n    }\n    free(graph);\n}\n\n/* \u67e5\u627e\u9876\u70b9\u5bf9\u5e94\u7684\u8282\u70b9 */\nAdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {\n    for (int i = 0; i < graph->size; i++) {\n        if (graph->heads[i]->vertex == vet) {\n            return graph->heads[i];\n        }\n    }\n    return NULL;\n}\n\n/* \u6dfb\u52a0\u8fb9 */\nvoid addEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) {\n    AdjListNode *head1 = findNode(graph, vet1);\n    AdjListNode *head2 = findNode(graph, vet2);\n    assert(head1 != NULL && head2 != NULL && head1 != head2);\n    // \u6dfb\u52a0\u8fb9 vet1 - vet2\n    addEdgeHelper(head1, vet2);\n    addEdgeHelper(head2, vet1);\n}\n\n/* \u5220\u9664\u8fb9 */\nvoid removeEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) {\n    AdjListNode *head1 = findNode(graph, vet1);\n    AdjListNode *head2 = findNode(graph, vet2);\n    assert(head1 != NULL && head2 != NULL);\n    // \u5220\u9664\u8fb9 vet1 - vet2\n    removeEdgeHelper(head1, head2->vertex);\n    removeEdgeHelper(head2, head1->vertex);\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(GraphAdjList *graph, Vertex *vet) {\n    assert(graph != NULL && graph->size < MAX_SIZE);\n    AdjListNode *head = (AdjListNode *)malloc(sizeof(AdjListNode));\n    head->vertex = vet;\n    head->next = NULL;\n    // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n    graph->heads[graph->size++] = head;\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(GraphAdjList *graph, Vertex *vet) {\n    AdjListNode *node = findNode(graph, vet);\n    assert(node != NULL);\n    // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n    AdjListNode *cur = node, *pre = NULL;\n    while (cur) {\n        pre = cur;\n        cur = cur->next;\n        free(pre);\n    }\n    // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n    for (int i = 0; i < graph->size; i++) {\n        cur = graph->heads[i];\n        pre = NULL;\n        while (cur) {\n            pre = cur;\n            cur = cur->next;\n            if (cur && cur->vertex == vet) {\n                pre->next = cur->next;\n                free(cur);\n                break;\n            }\n        }\n    }\n    // \u5c06\u8be5\u9876\u70b9\u4e4b\u540e\u7684\u9876\u70b9\u5411\u524d\u79fb\u52a8\uff0c\u4ee5\u586b\u8865\u7a7a\u7f3a\n    int i;\n    for (i = 0; i < graph->size; i++) {\n        if (graph->heads[i] == node)\n            break;\n    }\n    for (int j = i; j < graph->size - 1; j++) {\n        graph->heads[j] = graph->heads[j + 1];\n    }\n    graph->size--;\n    free(vet);\n}\n
        graph_adjacency_list.kt
        /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList(edges: Array<Array<Vertex?>>) {\n    // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    val adjList = HashMap<Vertex, MutableList<Vertex>>()\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    init {\n        // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n        for (edge in edges) {\n            addVertex(edge[0]!!)\n            addVertex(edge[1]!!)\n            addEdge(edge[0]!!, edge[1]!!)\n        }\n    }\n\n    /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n    fun size(): Int {\n        return adjList.size\n    }\n\n    /* \u6dfb\u52a0\u8fb9 */\n    fun addEdge(vet1: Vertex, vet2: Vertex) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw IllegalArgumentException()\n        // \u6dfb\u52a0\u8fb9 vet1 - vet2\n        adjList[vet1]?.add(vet2)\n        adjList[vet2]?.add(vet1)\n    }\n\n    /* \u5220\u9664\u8fb9 */\n    fun removeEdge(vet1: Vertex, vet2: Vertex) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw IllegalArgumentException()\n        // \u5220\u9664\u8fb9 vet1 - vet2\n        adjList[vet1]?.remove(vet2)\n        adjList[vet2]?.remove(vet1)\n    }\n\n    /* \u6dfb\u52a0\u9876\u70b9 */\n    fun addVertex(vet: Vertex) {\n        if (adjList.containsKey(vet))\n            return\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n        adjList[vet] = mutableListOf()\n    }\n\n    /* \u5220\u9664\u9876\u70b9 */\n    fun removeVertex(vet: Vertex) {\n        if (!adjList.containsKey(vet))\n            throw IllegalArgumentException()\n        // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n        adjList.remove(vet)\n        // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n        for (list in adjList.values) {\n            list.remove(vet)\n        }\n    }\n\n    /* \u6253\u5370\u90bb\u63a5\u8868 */\n    fun print() {\n        println(\"\u90bb\u63a5\u8868 =\")\n        for (pair in adjList.entries) {\n            val tmp = mutableListOf<Int>()\n            for (vertex in pair.value) {\n                tmp.add(vertex._val)\n            }\n            println(\"${pair.key._val}: $tmp,\")\n        }\n    }\n}\n
        graph_adjacency_list.rb
        ### \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b ###\nclass GraphAdjList\n  attr_reader :adj_list\n\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize(edges)\n    # \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    @adj_list = {}\n    # \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n    for edge in edges\n      add_vertex(edge[0])\n      add_vertex(edge[1])\n      add_edge(edge[0], edge[1])\n    end\n  end\n\n  ### \u83b7\u53d6\u9876\u70b9\u6570\u91cf ###\n  def size\n    @adj_list.length\n  end\n\n  ### \u6dfb\u52a0\u8fb9 ###\n  def add_edge(vet1, vet2)\n    raise ArgumentError if !@adj_list.include?(vet1) || !@adj_list.include?(vet2)\n\n    @adj_list[vet1] << vet2\n    @adj_list[vet2] << vet1\n  end\n\n  ### \u5220\u9664\u8fb9 ###\n  def remove_edge(vet1, vet2)\n    raise ArgumentError if !@adj_list.include?(vet1) || !@adj_list.include?(vet2)\n\n    # \u5220\u9664\u8fb9 vet1 - vet2\n    @adj_list[vet1].delete(vet2)\n    @adj_list[vet2].delete(vet1)\n  end\n\n  ### \u6dfb\u52a0\u9876\u70b9 ###\n  def add_vertex(vet)\n    return if @adj_list.include?(vet)\n\n    # \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n    @adj_list[vet] = []\n  end\n\n  ### \u5220\u9664\u9876\u70b9 ###\n  def remove_vertex(vet)\n    raise ArgumentError unless @adj_list.include?(vet)\n\n    # \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n    @adj_list.delete(vet)\n    # \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n    for vertex in @adj_list\n      @adj_list[vertex.first].delete(vet) if @adj_list[vertex.first].include?(vet)\n    end\n  end\n\n  ### \u6253\u5370\u90bb\u63a5\u8868 ###\n  def __print__\n    puts '\u90bb\u63a5\u8868 ='\n    for vertex in @adj_list\n      tmp = @adj_list[vertex.first].map { |v| v.val }\n      puts \"#{vertex.first.val}: #{tmp},\"\n    end\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_graph/graph_operations/#923","title":"9.2.3 \u00a0 \u6548\u7387\u5bf9\u6bd4","text":"

        \u8bbe\u56fe\u4e2d\u5171\u6709 \\(n\\) \u4e2a\u9876\u70b9\u548c \\(m\\) \u6761\u8fb9\uff0c\u8868 9-2 \u5bf9\u6bd4\u4e86\u90bb\u63a5\u77e9\u9635\u548c\u90bb\u63a5\u8868\u7684\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u3002\u8bf7\u6ce8\u610f\uff0c\u90bb\u63a5\u8868\uff08\u94fe\u8868\uff09\u5bf9\u5e94\u672c\u6587\u5b9e\u73b0\uff0c\u800c\u90bb\u63a5\u8868\uff08\u54c8\u5e0c\u8868\uff09\u4e13\u6307\u5c06\u6240\u6709\u94fe\u8868\u66ff\u6362\u4e3a\u54c8\u5e0c\u8868\u540e\u7684\u5b9e\u73b0\u3002

        \u8868 9-2 \u00a0 \u90bb\u63a5\u77e9\u9635\u4e0e\u90bb\u63a5\u8868\u5bf9\u6bd4

        \u90bb\u63a5\u77e9\u9635 \u90bb\u63a5\u8868\uff08\u94fe\u8868\uff09 \u90bb\u63a5\u8868\uff08\u54c8\u5e0c\u8868\uff09 \u5224\u65ad\u662f\u5426\u90bb\u63a5 \\(O(1)\\) \\(O(n)\\) \\(O(1)\\) \u6dfb\u52a0\u8fb9 \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u8fb9 \\(O(1)\\) \\(O(n)\\) \\(O(1)\\) \u6dfb\u52a0\u9876\u70b9 \\(O(n)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u9876\u70b9 \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n)\\) \u5185\u5b58\u7a7a\u95f4\u5360\u7528 \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n + m)\\)

        \u89c2\u5bdf\u8868 9-2 \uff0c\u4f3c\u4e4e\u90bb\u63a5\u8868\uff08\u54c8\u5e0c\u8868\uff09\u7684\u65f6\u95f4\u6548\u7387\u4e0e\u7a7a\u95f4\u6548\u7387\u6700\u4f18\u3002\u4f46\u5b9e\u9645\u4e0a\uff0c\u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u64cd\u4f5c\u8fb9\u7684\u6548\u7387\u66f4\u9ad8\uff0c\u53ea\u9700\u4e00\u6b21\u6570\u7ec4\u8bbf\u95ee\u6216\u8d4b\u503c\u64cd\u4f5c\u5373\u53ef\u3002\u7efc\u5408\u6765\u770b\uff0c\u90bb\u63a5\u77e9\u9635\u4f53\u73b0\u4e86\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\u7684\u539f\u5219\uff0c\u800c\u90bb\u63a5\u8868\u4f53\u73b0\u4e86\u201c\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4\u201d\u7684\u539f\u5219\u3002

        "},{"location":"chapter_graph/graph_traversal/","title":"9.3 \u00a0 \u56fe\u7684\u904d\u5386","text":"

        \u6811\u4ee3\u8868\u7684\u662f\u201c\u4e00\u5bf9\u591a\u201d\u7684\u5173\u7cfb\uff0c\u800c\u56fe\u5219\u5177\u6709\u66f4\u9ad8\u7684\u81ea\u7531\u5ea6\uff0c\u53ef\u4ee5\u8868\u793a\u4efb\u610f\u7684\u201c\u591a\u5bf9\u591a\u201d\u5173\u7cfb\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u6811\u770b\u4f5c\u56fe\u7684\u4e00\u79cd\u7279\u4f8b\u3002\u663e\u7136\uff0c\u6811\u7684\u904d\u5386\u64cd\u4f5c\u4e5f\u662f\u56fe\u7684\u904d\u5386\u64cd\u4f5c\u7684\u4e00\u79cd\u7279\u4f8b\u3002

        \u56fe\u548c\u6811\u90fd\u9700\u8981\u5e94\u7528\u641c\u7d22\u7b97\u6cd5\u6765\u5b9e\u73b0\u904d\u5386\u64cd\u4f5c\u3002\u56fe\u7684\u904d\u5386\u65b9\u5f0f\u4e5f\u53ef\u5206\u4e3a\u4e24\u79cd\uff1a\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u548c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u3002

        "},{"location":"chapter_graph/graph_traversal/#931","title":"9.3.1 \u00a0 \u5e7f\u5ea6\u4f18\u5148\u904d\u5386","text":"

        \u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u7531\u8fd1\u53ca\u8fdc\u7684\u904d\u5386\u65b9\u5f0f\uff0c\u4ece\u67d0\u4e2a\u8282\u70b9\u51fa\u53d1\uff0c\u59cb\u7ec8\u4f18\u5148\u8bbf\u95ee\u8ddd\u79bb\u6700\u8fd1\u7684\u9876\u70b9\uff0c\u5e76\u4e00\u5c42\u5c42\u5411\u5916\u6269\u5f20\u3002\u5982\u56fe 9-9 \u6240\u793a\uff0c\u4ece\u5de6\u4e0a\u89d2\u9876\u70b9\u51fa\u53d1\uff0c\u9996\u5148\u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff0c\u7136\u540e\u904d\u5386\u4e0b\u4e00\u4e2a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u6240\u6709\u9876\u70b9\u8bbf\u95ee\u5b8c\u6bd5\u3002

        \u56fe 9-9 \u00a0 \u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386

        "},{"location":"chapter_graph/graph_traversal/#1","title":"1. \u00a0 \u7b97\u6cd5\u5b9e\u73b0","text":"

        BFS \u901a\u5e38\u501f\u52a9\u961f\u5217\u6765\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002\u961f\u5217\u5177\u6709\u201c\u5148\u5165\u5148\u51fa\u201d\u7684\u6027\u8d28\uff0c\u8fd9\u4e0e BFS \u7684\u201c\u7531\u8fd1\u53ca\u8fdc\u201d\u7684\u601d\u60f3\u5f02\u66f2\u540c\u5de5\u3002

        1. \u5c06\u904d\u5386\u8d77\u59cb\u9876\u70b9 startVet \u52a0\u5165\u961f\u5217\uff0c\u5e76\u5f00\u542f\u5faa\u73af\u3002
        2. \u5728\u5faa\u73af\u7684\u6bcf\u8f6e\u8fed\u4ee3\u4e2d\uff0c\u5f39\u51fa\u961f\u9996\u9876\u70b9\u5e76\u8bb0\u5f55\u8bbf\u95ee\uff0c\u7136\u540e\u5c06\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\u52a0\u5165\u5230\u961f\u5217\u5c3e\u90e8\u3002
        3. \u5faa\u73af\u6b65\u9aa4 2. \uff0c\u76f4\u5230\u6240\u6709\u9876\u70b9\u88ab\u8bbf\u95ee\u5b8c\u6bd5\u540e\u7ed3\u675f\u3002

        \u4e3a\u4e86\u9632\u6b62\u91cd\u590d\u904d\u5386\u9876\u70b9\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u96c6\u5408 visited \u6765\u8bb0\u5f55\u54ea\u4e9b\u8282\u70b9\u5df2\u88ab\u8bbf\u95ee\u3002

        Tip

        \u54c8\u5e0c\u96c6\u5408\u53ef\u4ee5\u770b\u4f5c\u4e00\u4e2a\u53ea\u5b58\u50a8 key \u800c\u4e0d\u5b58\u50a8 value \u7684\u54c8\u5e0c\u8868\uff0c\u5b83\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u590d\u6742\u5ea6\u4e0b\u8fdb\u884c key \u7684\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u3002\u6839\u636e key \u7684\u552f\u4e00\u6027\uff0c\u54c8\u5e0c\u96c6\u5408\u901a\u5e38\u7528\u4e8e\u6570\u636e\u53bb\u91cd\u7b49\u573a\u666f\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_bfs.py
        def graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n    \"\"\"\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\"\"\"\n    # \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    # \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    res = []\n    # \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    visited = set[Vertex]([start_vet])\n    # \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n    que = deque[Vertex]([start_vet])\n    # \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n    while len(que) > 0:\n        vet = que.popleft()  # \u961f\u9996\u9876\u70b9\u51fa\u961f\n        res.append(vet)  # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n        # \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n        for adj_vet in graph.adj_list[vet]:\n            if adj_vet in visited:\n                continue  # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n            que.append(adj_vet)  # \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n            visited.add(adj_vet)  # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n    # \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n    return res\n
        graph_bfs.cpp
        /* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvector<Vertex *> graphBFS(GraphAdjList &graph, Vertex *startVet) {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    vector<Vertex *> res;\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    unordered_set<Vertex *> visited = {startVet};\n    // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n    queue<Vertex *> que;\n    que.push(startVet);\n    // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n    while (!que.empty()) {\n        Vertex *vet = que.front();\n        que.pop();          // \u961f\u9996\u9876\u70b9\u51fa\u961f\n        res.push_back(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n        // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n        for (auto adjVet : graph.adjList[vet]) {\n            if (visited.count(adjVet))\n                continue;            // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n            que.push(adjVet);        // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n            visited.emplace(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n        }\n    }\n    // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n    return res;\n}\n
        graph_bfs.java
        /* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    List<Vertex> res = new ArrayList<>();\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    Set<Vertex> visited = new HashSet<>();\n    visited.add(startVet);\n    // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n    Queue<Vertex> que = new LinkedList<>();\n    que.offer(startVet);\n    // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n    while (!que.isEmpty()) {\n        Vertex vet = que.poll(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n        res.add(vet);            // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n        // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n        for (Vertex adjVet : graph.adjList.get(vet)) {\n            if (visited.contains(adjVet))\n                continue;        // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n            que.offer(adjVet);   // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n            visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n        }\n    }\n    // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n    return res;\n}\n
        graph_bfs.cs
        /* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> GraphBFS(GraphAdjList graph, Vertex startVet) {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    List<Vertex> res = [];\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    HashSet<Vertex> visited = [startVet];\n    // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n    Queue<Vertex> que = new();\n    que.Enqueue(startVet);\n    // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n    while (que.Count > 0) {\n        Vertex vet = que.Dequeue(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n        res.Add(vet);               // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n        foreach (Vertex adjVet in graph.adjList[vet]) {\n            if (visited.Contains(adjVet)) {\n                continue;          // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n            }\n            que.Enqueue(adjVet);   // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n            visited.Add(adjVet);   // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n        }\n    }\n\n    // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n    return res;\n}\n
        graph_bfs.go
        /* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphBFS(g *graphAdjList, startVet Vertex) []Vertex {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    res := make([]Vertex, 0)\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    visited := make(map[Vertex]struct{})\n    visited[startVet] = struct{}{}\n    // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS, \u4f7f\u7528\u5207\u7247\u6a21\u62df\u961f\u5217\n    queue := make([]Vertex, 0)\n    queue = append(queue, startVet)\n    // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n    for len(queue) > 0 {\n        // \u961f\u9996\u9876\u70b9\u51fa\u961f\n        vet := queue[0]\n        queue = queue[1:]\n        // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n        res = append(res, vet)\n        // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n        for _, adjVet := range g.adjList[vet] {\n            _, isExist := visited[adjVet]\n            // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n            if !isExist {\n                queue = append(queue, adjVet)\n                visited[adjVet] = struct{}{}\n            }\n        }\n    }\n    // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n    return res\n}\n
        graph_bfs.swift
        /* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphBFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    var res: [Vertex] = []\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    var visited: Set<Vertex> = [startVet]\n    // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n    var que: [Vertex] = [startVet]\n    // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n    while !que.isEmpty {\n        let vet = que.removeFirst() // \u961f\u9996\u9876\u70b9\u51fa\u961f\n        res.append(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n        // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n        for adjVet in graph.adjList[vet] ?? [] {\n            if visited.contains(adjVet) {\n                continue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n            }\n            que.append(adjVet) // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n            visited.insert(adjVet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n        }\n    }\n    // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n    return res\n}\n
        graph_bfs.js
        /* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphBFS(graph, startVet) {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    const res = [];\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    const visited = new Set();\n    visited.add(startVet);\n    // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n    const que = [startVet];\n    // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n    while (que.length) {\n        const vet = que.shift(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n        res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n        // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n        for (const adjVet of graph.adjList.get(vet) ?? []) {\n            if (visited.has(adjVet)) {\n                continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n            }\n            que.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n            visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n        }\n    }\n    // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n    return res;\n}\n
        graph_bfs.ts
        /* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphBFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    const res: Vertex[] = [];\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    const visited: Set<Vertex> = new Set();\n    visited.add(startVet);\n    // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n    const que = [startVet];\n    // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n    while (que.length) {\n        const vet = que.shift(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n        res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n        // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n        for (const adjVet of graph.adjList.get(vet) ?? []) {\n            if (visited.has(adjVet)) {\n                continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n            }\n            que.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\n            visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n        }\n    }\n    // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n    return res;\n}\n
        graph_bfs.dart
        /* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\nList<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {\n  // \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n  // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n  List<Vertex> res = [];\n  // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n  Set<Vertex> visited = {};\n  visited.add(startVet);\n  // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n  Queue<Vertex> que = Queue();\n  que.add(startVet);\n  // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n  while (que.isNotEmpty) {\n    Vertex vet = que.removeFirst(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n    res.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    for (Vertex adjVet in graph.adjList[vet]!) {\n      if (visited.contains(adjVet)) {\n        continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n      }\n      que.add(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n      visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n    }\n  }\n  // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n  return res;\n}\n
        graph_bfs.rs
        /* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfn graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    let mut res = vec![];\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    let mut visited = HashSet::new();\n    visited.insert(start_vet);\n    // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n    let mut que = VecDeque::new();\n    que.push_back(start_vet);\n    // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n    while let Some(vet) = que.pop_front() {\n        res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n\n        // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n        if let Some(adj_vets) = graph.adj_list.get(&vet) {\n            for &adj_vet in adj_vets {\n                if visited.contains(&adj_vet) {\n                    continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n                }\n                que.push_back(adj_vet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n                visited.insert(adj_vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n            }\n        }\n    }\n    // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n    res\n}\n
        graph_bfs.c
        /* \u8282\u70b9\u961f\u5217\u7ed3\u6784\u4f53 */\ntypedef struct {\n    Vertex *vertices[MAX_SIZE];\n    int front, rear, size;\n} Queue;\n\n/* \u6784\u9020\u51fd\u6570 */\nQueue *newQueue() {\n    Queue *q = (Queue *)malloc(sizeof(Queue));\n    q->front = q->rear = q->size = 0;\n    return q;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nint isEmpty(Queue *q) {\n    return q->size == 0;\n}\n\n/* \u5165\u961f\u64cd\u4f5c */\nvoid enqueue(Queue *q, Vertex *vet) {\n    q->vertices[q->rear] = vet;\n    q->rear = (q->rear + 1) % MAX_SIZE;\n    q->size++;\n}\n\n/* \u51fa\u961f\u64cd\u4f5c */\nVertex *dequeue(Queue *q) {\n    Vertex *vet = q->vertices[q->front];\n    q->front = (q->front + 1) % MAX_SIZE;\n    q->size--;\n    return vet;\n}\n\n/* \u68c0\u67e5\u9876\u70b9\u662f\u5426\u5df2\u88ab\u8bbf\u95ee */\nint isVisited(Vertex **visited, int size, Vertex *vet) {\n    // \u904d\u5386\u67e5\u627e\u8282\u70b9\uff0c\u4f7f\u7528 O(n) \u65f6\u95f4\n    for (int i = 0; i < size; i++) {\n        if (visited[i] == vet)\n            return 1;\n    }\n    return 0;\n}\n\n/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvoid graphBFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize, Vertex **visited, int *visitedSize) {\n    // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n    Queue *queue = newQueue();\n    enqueue(queue, startVet);\n    visited[(*visitedSize)++] = startVet;\n    // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n    while (!isEmpty(queue)) {\n        Vertex *vet = dequeue(queue); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n        res[(*resSize)++] = vet;      // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n        // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n        AdjListNode *node = findNode(graph, vet);\n        while (node != NULL) {\n            // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n            if (!isVisited(visited, *visitedSize, node->vertex)) {\n                enqueue(queue, node->vertex);             // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n                visited[(*visitedSize)++] = node->vertex; // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n            }\n            node = node->next;\n        }\n    }\n    // \u91ca\u653e\u5185\u5b58\n    free(queue);\n}\n
        graph_bfs.kt
        /* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfun graphBFS(graph: GraphAdjList, startVet: Vertex): MutableList<Vertex?> {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    val res = mutableListOf<Vertex?>()\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    val visited = HashSet<Vertex>()\n    visited.add(startVet)\n    // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n    val que = LinkedList<Vertex>()\n    que.offer(startVet)\n    // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n    while (!que.isEmpty()) {\n        val vet = que.poll() // \u961f\u9996\u9876\u70b9\u51fa\u961f\n        res.add(vet)         // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n        // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n        for (adjVet in graph.adjList[vet]!!) {\n            if (visited.contains(adjVet))\n                continue        // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n            que.offer(adjVet)   // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n            visited.add(adjVet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n        }\n    }\n    // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n    return res\n}\n
        graph_bfs.rb
        ### \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 ###\ndef graph_bfs(graph, start_vet)\n  # \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n  # \u9876\u70b9\u904d\u5386\u5e8f\u5217\n  res = []\n  # \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n  visited = Set.new([start_vet])\n  # \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n  que = [start_vet]\n  # \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n  while que.length > 0\n    vet = que.shift # \u961f\u9996\u9876\u70b9\u51fa\u961f\n    res << vet # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    # \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    for adj_vet in graph.adj_list[vet]\n      next if visited.include?(adj_vet) # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n      que << adj_vet # \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n      visited.add(adj_vet) # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n    end\n  end\n  # \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u4ee3\u7801\u76f8\u5bf9\u62bd\u8c61\uff0c\u5efa\u8bae\u5bf9\u7167\u56fe 9-10 \u6765\u52a0\u6df1\u7406\u89e3\u3002

        <1><2><3><4><5><6><7><8><9><10><11>

        \u56fe 9-10 \u00a0 \u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u6b65\u9aa4

        \u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u7684\u5e8f\u5217\u662f\u5426\u552f\u4e00\uff1f

        \u4e0d\u552f\u4e00\u3002\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u53ea\u8981\u6c42\u6309\u201c\u7531\u8fd1\u53ca\u8fdc\u201d\u7684\u987a\u5e8f\u904d\u5386\uff0c\u800c\u591a\u4e2a\u76f8\u540c\u8ddd\u79bb\u7684\u9876\u70b9\u7684\u904d\u5386\u987a\u5e8f\u5141\u8bb8\u88ab\u4efb\u610f\u6253\u4e71\u3002\u4ee5\u56fe 9-10 \u4e3a\u4f8b\uff0c\u9876\u70b9 \\(1\\)\u3001\\(3\\) \u7684\u8bbf\u95ee\u987a\u5e8f\u53ef\u4ee5\u4ea4\u6362\uff0c\u9876\u70b9 \\(2\\)\u3001\\(4\\)\u3001\\(6\\) \u7684\u8bbf\u95ee\u987a\u5e8f\u4e5f\u53ef\u4ee5\u4efb\u610f\u4ea4\u6362\u3002

        "},{"location":"chapter_graph/graph_traversal/#2","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"

        \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u6240\u6709\u9876\u70b9\u90fd\u4f1a\u5165\u961f\u5e76\u51fa\u961f\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(|V|)\\) \u65f6\u95f4\uff1b\u5728\u904d\u5386\u90bb\u63a5\u9876\u70b9\u7684\u8fc7\u7a0b\u4e2d\uff0c\u7531\u4e8e\u662f\u65e0\u5411\u56fe\uff0c\u56e0\u6b64\u6240\u6709\u8fb9\u90fd\u4f1a\u88ab\u8bbf\u95ee \\(2\\) \u6b21\uff0c\u4f7f\u7528 \\(O(2|E|)\\) \u65f6\u95f4\uff1b\u603b\u4f53\u4f7f\u7528 \\(O(|V| + |E|)\\) \u65f6\u95f4\u3002

        \u7a7a\u95f4\u590d\u6742\u5ea6\uff1a\u5217\u8868 res \uff0c\u54c8\u5e0c\u96c6\u5408 visited \uff0c\u961f\u5217 que \u4e2d\u7684\u9876\u70b9\u6570\u91cf\u6700\u591a\u4e3a \\(|V|\\) \uff0c\u4f7f\u7528 \\(O(|V|)\\) \u7a7a\u95f4\u3002

        "},{"location":"chapter_graph/graph_traversal/#932","title":"9.3.2 \u00a0 \u6df1\u5ea6\u4f18\u5148\u904d\u5386","text":"

        \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u4f18\u5148\u8d70\u5230\u5e95\u3001\u65e0\u8def\u53ef\u8d70\u518d\u56de\u5934\u7684\u904d\u5386\u65b9\u5f0f\u3002\u5982\u56fe 9-11 \u6240\u793a\uff0c\u4ece\u5de6\u4e0a\u89d2\u9876\u70b9\u51fa\u53d1\uff0c\u8bbf\u95ee\u5f53\u524d\u9876\u70b9\u7684\u67d0\u4e2a\u90bb\u63a5\u9876\u70b9\uff0c\u76f4\u5230\u8d70\u5230\u5c3d\u5934\u65f6\u8fd4\u56de\uff0c\u518d\u7ee7\u7eed\u8d70\u5230\u5c3d\u5934\u5e76\u8fd4\u56de\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u6240\u6709\u9876\u70b9\u904d\u5386\u5b8c\u6210\u3002

        \u56fe 9-11 \u00a0 \u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386

        "},{"location":"chapter_graph/graph_traversal/#1_1","title":"1. \u00a0 \u7b97\u6cd5\u5b9e\u73b0","text":"

        \u8fd9\u79cd\u201c\u8d70\u5230\u5c3d\u5934\u518d\u8fd4\u56de\u201d\u7684\u7b97\u6cd5\u8303\u5f0f\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u6765\u5b9e\u73b0\u3002\u4e0e\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u7c7b\u4f3c\uff0c\u5728\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u4e2d\uff0c\u6211\u4eec\u4e5f\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u96c6\u5408 visited \u6765\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\uff0c\u4ee5\u907f\u514d\u91cd\u590d\u8bbf\u95ee\u9876\u70b9\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_dfs.py
        def dfs(graph: GraphAdjList, visited: set[Vertex], res: list[Vertex], vet: Vertex):\n    \"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570\"\"\"\n    res.append(vet)  # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    visited.add(vet)  # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n    # \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    for adjVet in graph.adj_list[vet]:\n        if adjVet in visited:\n            continue  # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n        # \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n        dfs(graph, visited, res, adjVet)\n\ndef graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n    \"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386\"\"\"\n    # \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    # \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    res = []\n    # \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    visited = set[Vertex]()\n    dfs(graph, visited, res, start_vet)\n    return res\n
        graph_dfs.cpp
        /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList &graph, unordered_set<Vertex *> &visited, vector<Vertex *> &res, Vertex *vet) {\n    res.push_back(vet);   // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    visited.emplace(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n    // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    for (Vertex *adjVet : graph.adjList[vet]) {\n        if (visited.count(adjVet))\n            continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n        // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvector<Vertex *> graphDFS(GraphAdjList &graph, Vertex *startVet) {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    vector<Vertex *> res;\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    unordered_set<Vertex *> visited;\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
        graph_dfs.java
        /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList graph, Set<Vertex> visited, List<Vertex> res, Vertex vet) {\n    res.add(vet);     // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n    // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    for (Vertex adjVet : graph.adjList.get(vet)) {\n        if (visited.contains(adjVet))\n            continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n        // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    List<Vertex> res = new ArrayList<>();\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    Set<Vertex> visited = new HashSet<>();\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
        graph_dfs.cs
        /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nvoid DFS(GraphAdjList graph, HashSet<Vertex> visited, List<Vertex> res, Vertex vet) {\n    res.Add(vet);     // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    visited.Add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n    // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    foreach (Vertex adjVet in graph.adjList[vet]) {\n        if (visited.Contains(adjVet)) {\n            continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9                             \n        }\n        // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n        DFS(graph, visited, res, adjVet);\n    }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> GraphDFS(GraphAdjList graph, Vertex startVet) {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    List<Vertex> res = [];\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    HashSet<Vertex> visited = [];\n    DFS(graph, visited, res, startVet);\n    return res;\n}\n
        graph_dfs.go
        /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nfunc dfs(g *graphAdjList, visited map[Vertex]struct{}, res *[]Vertex, vet Vertex) {\n    // append \u64cd\u4f5c\u4f1a\u8fd4\u56de\u65b0\u7684\u7684\u5f15\u7528\uff0c\u5fc5\u987b\u8ba9\u539f\u5f15\u7528\u91cd\u65b0\u8d4b\u503c\u4e3a\u65b0slice\u7684\u5f15\u7528\n    *res = append(*res, vet)\n    visited[vet] = struct{}{}\n    // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    for _, adjVet := range g.adjList[vet] {\n        _, isExist := visited[adjVet]\n        // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n        if !isExist {\n            dfs(g, visited, res, adjVet)\n        }\n    }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphDFS(g *graphAdjList, startVet Vertex) []Vertex {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    res := make([]Vertex, 0)\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    visited := make(map[Vertex]struct{})\n    dfs(g, visited, &res, startVet)\n    // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n    return res\n}\n
        graph_dfs.swift
        /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nfunc dfs(graph: GraphAdjList, visited: inout Set<Vertex>, res: inout [Vertex], vet: Vertex) {\n    res.append(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    visited.insert(vet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n    // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    for adjVet in graph.adjList[vet] ?? [] {\n        if visited.contains(adjVet) {\n            continue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n        }\n        // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n        dfs(graph: graph, visited: &visited, res: &res, vet: adjVet)\n    }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphDFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    var res: [Vertex] = []\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    var visited: Set<Vertex> = []\n    dfs(graph: graph, visited: &visited, res: &res, vet: startVet)\n    return res\n}\n
        graph_dfs.js
        /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction dfs(graph, visited, res, vet) {\n    res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n    // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    for (const adjVet of graph.adjList.get(vet)) {\n        if (visited.has(adjVet)) {\n            continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n        }\n        // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphDFS(graph, startVet) {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    const res = [];\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    const visited = new Set();\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
        graph_dfs.ts
        /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nfunction dfs(\n    graph: GraphAdjList,\n    visited: Set<Vertex>,\n    res: Vertex[],\n    vet: Vertex\n): void {\n    res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n    // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    for (const adjVet of graph.adjList.get(vet)) {\n        if (visited.has(adjVet)) {\n            continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n        }\n        // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphDFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    const res: Vertex[] = [];\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    const visited: Set<Vertex> = new Set();\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
        graph_dfs.dart
        /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nvoid dfs(\n  GraphAdjList graph,\n  Set<Vertex> visited,\n  List<Vertex> res,\n  Vertex vet,\n) {\n  res.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n  visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n  // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n  for (Vertex adjVet in graph.adjList[vet]!) {\n    if (visited.contains(adjVet)) {\n      continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n    }\n    // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n    dfs(graph, visited, res, adjVet);\n  }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n  // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n  List<Vertex> res = [];\n  // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n  Set<Vertex> visited = {};\n  dfs(graph, visited, res, startVet);\n  return res;\n}\n
        graph_dfs.rs
        /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nfn dfs(graph: &GraphAdjList, visited: &mut HashSet<Vertex>, res: &mut Vec<Vertex>, vet: Vertex) {\n    res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    visited.insert(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n                         // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    if let Some(adj_vets) = graph.adj_list.get(&vet) {\n        for &adj_vet in adj_vets {\n            if visited.contains(&adj_vet) {\n                continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n            }\n            // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n            dfs(graph, visited, res, adj_vet);\n        }\n    }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfn graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    let mut res = vec![];\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    let mut visited = HashSet::new();\n    dfs(&graph, &mut visited, &mut res, start_vet);\n\n    res\n}\n
        graph_dfs.c
        /* \u68c0\u67e5\u9876\u70b9\u662f\u5426\u5df2\u88ab\u8bbf\u95ee */\nint isVisited(Vertex **res, int size, Vertex *vet) {\n    // \u904d\u5386\u67e5\u627e\u8282\u70b9\uff0c\u4f7f\u7528 O(n) \u65f6\u95f4\n    for (int i = 0; i < size; i++) {\n        if (res[i] == vet) {\n            return 1;\n        }\n    }\n    return 0;\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList *graph, Vertex **res, int *resSize, Vertex *vet) {\n    // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    res[(*resSize)++] = vet;\n    // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    AdjListNode *node = findNode(graph, vet);\n    while (node != NULL) {\n        // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n        if (!isVisited(res, *resSize, node->vertex)) {\n            // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n            dfs(graph, res, resSize, node->vertex);\n        }\n        node = node->next;\n    }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvoid graphDFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize) {\n    dfs(graph, res, resSize, startVet);\n}\n
        graph_dfs.kt
        /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nfun dfs(\n    graph: GraphAdjList,\n    visited: MutableSet<Vertex?>,\n    res: MutableList<Vertex?>,\n    vet: Vertex?\n) {\n    res.add(vet)     // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n    visited.add(vet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n    // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n    for (adjVet in graph.adjList[vet]!!) {\n        if (visited.contains(adjVet))\n            continue  // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n        // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n        dfs(graph, visited, res, adjVet)\n    }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfun graphDFS(graph: GraphAdjList, startVet: Vertex?): MutableList<Vertex?> {\n    // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n    val res = mutableListOf<Vertex?>()\n    // \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n    val visited = HashSet<Vertex?>()\n    dfs(graph, visited, res, startVet)\n    return res\n}\n
        graph_dfs.rb
        ### \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 ###\ndef dfs(graph, visited, res, vet)\n  res << vet # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n  visited.add(vet) # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n  # \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n  for adj_vet in graph.adj_list[vet]\n    next if visited.include?(adj_vet) # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n    # \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n    dfs(graph, visited, res, adj_vet)\n  end\nend\n\n### \u6df1\u5ea6\u4f18\u5148\u904d\u5386 ###\ndef graph_dfs(graph, start_vet)\n  # \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n  # \u9876\u70b9\u904d\u5386\u5e8f\u5217\n  res = []\n  # \u54c8\u5e0c\u96c6\u5408\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n  visited = Set.new\n  dfs(graph, visited, res, start_vet)\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u7b97\u6cd5\u6d41\u7a0b\u5982\u56fe 9-12 \u6240\u793a\u3002

        • \u76f4\u865a\u7ebf\u4ee3\u8868\u5411\u4e0b\u9012\u63a8\uff0c\u8868\u793a\u5f00\u542f\u4e86\u4e00\u4e2a\u65b0\u7684\u9012\u5f52\u65b9\u6cd5\u6765\u8bbf\u95ee\u65b0\u9876\u70b9\u3002
        • \u66f2\u865a\u7ebf\u4ee3\u8868\u5411\u4e0a\u56de\u6eaf\uff0c\u8868\u793a\u6b64\u9012\u5f52\u65b9\u6cd5\u5df2\u7ecf\u8fd4\u56de\uff0c\u56de\u6eaf\u5230\u4e86\u5f00\u542f\u6b64\u65b9\u6cd5\u7684\u4f4d\u7f6e\u3002

        \u4e3a\u4e86\u52a0\u6df1\u7406\u89e3\uff0c\u5efa\u8bae\u5c06\u56fe 9-12 \u4e0e\u4ee3\u7801\u7ed3\u5408\u8d77\u6765\uff0c\u5728\u8111\u4e2d\u6a21\u62df\uff08\u6216\u8005\u7528\u7b14\u753b\u4e0b\u6765\uff09\u6574\u4e2a DFS \u8fc7\u7a0b\uff0c\u5305\u62ec\u6bcf\u4e2a\u9012\u5f52\u65b9\u6cd5\u4f55\u65f6\u5f00\u542f\u3001\u4f55\u65f6\u8fd4\u56de\u3002

        <1><2><3><4><5><6><7><8><9><10><11>

        \u56fe 9-12 \u00a0 \u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u6b65\u9aa4

        \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u5e8f\u5217\u662f\u5426\u552f\u4e00\uff1f

        \u4e0e\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u7c7b\u4f3c\uff0c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u5e8f\u5217\u7684\u987a\u5e8f\u4e5f\u4e0d\u662f\u552f\u4e00\u7684\u3002\u7ed9\u5b9a\u67d0\u9876\u70b9\uff0c\u5148\u5f80\u54ea\u4e2a\u65b9\u5411\u63a2\u7d22\u90fd\u53ef\u4ee5\uff0c\u5373\u90bb\u63a5\u9876\u70b9\u7684\u987a\u5e8f\u53ef\u4ee5\u4efb\u610f\u6253\u4e71\uff0c\u90fd\u662f\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u3002

        \u4ee5\u6811\u7684\u904d\u5386\u4e3a\u4f8b\uff0c\u201c\u6839 \\(\\rightarrow\\) \u5de6 \\(\\rightarrow\\) \u53f3\u201d\u201c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u201d\u201c\u5de6 \\(\\rightarrow\\) \u53f3 \\(\\rightarrow\\) \u6839\u201d\u5206\u522b\u5bf9\u5e94\u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386\uff0c\u5b83\u4eec\u5c55\u793a\u4e86\u4e09\u79cd\u904d\u5386\u4f18\u5148\u7ea7\uff0c\u7136\u800c\u8fd9\u4e09\u8005\u90fd\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u3002

        "},{"location":"chapter_graph/graph_traversal/#2_1","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"

        \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u6240\u6709\u9876\u70b9\u90fd\u4f1a\u88ab\u8bbf\u95ee \\(1\\) \u6b21\uff0c\u4f7f\u7528 \\(O(|V|)\\) \u65f6\u95f4\uff1b\u6240\u6709\u8fb9\u90fd\u4f1a\u88ab\u8bbf\u95ee \\(2\\) \u6b21\uff0c\u4f7f\u7528 \\(O(2|E|)\\) \u65f6\u95f4\uff1b\u603b\u4f53\u4f7f\u7528 \\(O(|V| + |E|)\\) \u65f6\u95f4\u3002

        \u7a7a\u95f4\u590d\u6742\u5ea6\uff1a\u5217\u8868 res \uff0c\u54c8\u5e0c\u96c6\u5408 visited \u9876\u70b9\u6570\u91cf\u6700\u591a\u4e3a \\(|V|\\) \uff0c\u9012\u5f52\u6df1\u5ea6\u6700\u5927\u4e3a \\(|V|\\) \uff0c\u56e0\u6b64\u4f7f\u7528 \\(O(|V|)\\) \u7a7a\u95f4\u3002

        "},{"location":"chapter_graph/summary/","title":"9.4 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_graph/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u56fe\u7531\u9876\u70b9\u548c\u8fb9\u7ec4\u6210\uff0c\u53ef\u4ee5\u8868\u793a\u4e3a\u4e00\u7ec4\u9876\u70b9\u548c\u4e00\u7ec4\u8fb9\u6784\u6210\u7684\u96c6\u5408\u3002
        • \u76f8\u8f83\u4e8e\u7ebf\u6027\u5173\u7cfb\uff08\u94fe\u8868\uff09\u548c\u5206\u6cbb\u5173\u7cfb\uff08\u6811\uff09\uff0c\u7f51\u7edc\u5173\u7cfb\uff08\u56fe\uff09\u5177\u6709\u66f4\u9ad8\u7684\u81ea\u7531\u5ea6\uff0c\u56e0\u800c\u66f4\u4e3a\u590d\u6742\u3002
        • \u6709\u5411\u56fe\u7684\u8fb9\u5177\u6709\u65b9\u5411\u6027\uff0c\u8fde\u901a\u56fe\u4e2d\u7684\u4efb\u610f\u9876\u70b9\u5747\u53ef\u8fbe\uff0c\u6709\u6743\u56fe\u7684\u6bcf\u6761\u8fb9\u90fd\u5305\u542b\u6743\u91cd\u53d8\u91cf\u3002
        • \u90bb\u63a5\u77e9\u9635\u5229\u7528\u77e9\u9635\u6765\u8868\u793a\u56fe\uff0c\u6bcf\u4e00\u884c\uff08\u5217\uff09\u4ee3\u8868\u4e00\u4e2a\u9876\u70b9\uff0c\u77e9\u9635\u5143\u7d20\u4ee3\u8868\u8fb9\uff0c\u7528 \\(1\\) \u6216 \\(0\\) \u8868\u793a\u4e24\u4e2a\u9876\u70b9\u4e4b\u95f4\u6709\u8fb9\u6216\u65e0\u8fb9\u3002\u90bb\u63a5\u77e9\u9635\u5728\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u4e0a\u6548\u7387\u5f88\u9ad8\uff0c\u4f46\u7a7a\u95f4\u5360\u7528\u8f83\u591a\u3002
        • \u90bb\u63a5\u8868\u4f7f\u7528\u591a\u4e2a\u94fe\u8868\u6765\u8868\u793a\u56fe\uff0c\u7b2c \\(i\\) \u4e2a\u94fe\u8868\u5bf9\u5e94\u9876\u70b9 \\(i\\) \uff0c\u5176\u4e2d\u5b58\u50a8\u4e86\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\u3002\u90bb\u63a5\u8868\u76f8\u5bf9\u4e8e\u90bb\u63a5\u77e9\u9635\u66f4\u52a0\u8282\u7701\u7a7a\u95f4\uff0c\u4f46\u7531\u4e8e\u9700\u8981\u904d\u5386\u94fe\u8868\u6765\u67e5\u627e\u8fb9\uff0c\u56e0\u6b64\u65f6\u95f4\u6548\u7387\u8f83\u4f4e\u3002
        • \u5f53\u90bb\u63a5\u8868\u4e2d\u7684\u94fe\u8868\u8fc7\u957f\u65f6\uff0c\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811\u6216\u54c8\u5e0c\u8868\uff0c\u4ece\u800c\u63d0\u5347\u67e5\u8be2\u6548\u7387\u3002
        • \u4ece\u7b97\u6cd5\u601d\u60f3\u7684\u89d2\u5ea6\u5206\u6790\uff0c\u90bb\u63a5\u77e9\u9635\u4f53\u73b0\u4e86\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\uff0c\u90bb\u63a5\u8868\u4f53\u73b0\u4e86\u201c\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4\u201d\u3002
        • \u56fe\u53ef\u7528\u4e8e\u5efa\u6a21\u5404\u7c7b\u73b0\u5b9e\u7cfb\u7edf\uff0c\u5982\u793e\u4ea4\u7f51\u7edc\u3001\u5730\u94c1\u7ebf\u8def\u7b49\u3002
        • \u6811\u662f\u56fe\u7684\u4e00\u79cd\u7279\u4f8b\uff0c\u6811\u7684\u904d\u5386\u4e5f\u662f\u56fe\u7684\u904d\u5386\u7684\u4e00\u79cd\u7279\u4f8b\u3002
        • \u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u7531\u8fd1\u53ca\u8fdc\u3001\u5c42\u5c42\u6269\u5f20\u7684\u641c\u7d22\u65b9\u5f0f\uff0c\u901a\u5e38\u501f\u52a9\u961f\u5217\u5b9e\u73b0\u3002
        • \u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u4f18\u5148\u8d70\u5230\u5e95\u3001\u65e0\u8def\u53ef\u8d70\u65f6\u518d\u56de\u6eaf\u7684\u641c\u7d22\u65b9\u5f0f\uff0c\u5e38\u57fa\u4e8e\u9012\u5f52\u6765\u5b9e\u73b0\u3002
        "},{"location":"chapter_graph/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u8def\u5f84\u7684\u5b9a\u4e49\u662f\u9876\u70b9\u5e8f\u5217\u8fd8\u662f\u8fb9\u5e8f\u5217\uff1f

        \u7ef4\u57fa\u767e\u79d1\u4e0a\u4e0d\u540c\u8bed\u8a00\u7248\u672c\u7684\u5b9a\u4e49\u4e0d\u4e00\u81f4\uff1a\u82f1\u6587\u7248\u662f\u201c\u8def\u5f84\u662f\u4e00\u4e2a\u8fb9\u5e8f\u5217\u201d\uff0c\u800c\u4e2d\u6587\u7248\u662f\u201c\u8def\u5f84\u662f\u4e00\u4e2a\u9876\u70b9\u5e8f\u5217\u201d\u3002\u4ee5\u4e0b\u662f\u82f1\u6587\u7248\u539f\u6587\uff1aIn graph theory, a path in a graph is a finite or infinite sequence of edges which joins a sequence of vertices.

        \u5728\u672c\u6587\u4e2d\uff0c\u8def\u5f84\u88ab\u89c6\u4e3a\u4e00\u4e2a\u8fb9\u5e8f\u5217\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u9876\u70b9\u5e8f\u5217\u3002\u8fd9\u662f\u56e0\u4e3a\u4e24\u4e2a\u9876\u70b9\u4e4b\u95f4\u53ef\u80fd\u5b58\u5728\u591a\u6761\u8fb9\u8fde\u63a5\uff0c\u6b64\u65f6\u6bcf\u6761\u8fb9\u90fd\u5bf9\u5e94\u4e00\u6761\u8def\u5f84\u3002

        Q\uff1a\u975e\u8fde\u901a\u56fe\u4e2d\u662f\u5426\u4f1a\u6709\u65e0\u6cd5\u904d\u5386\u5230\u7684\u70b9\uff1f

        \u5728\u975e\u8fde\u901a\u56fe\u4e2d\uff0c\u4ece\u67d0\u4e2a\u9876\u70b9\u51fa\u53d1\uff0c\u81f3\u5c11\u6709\u4e00\u4e2a\u9876\u70b9\u65e0\u6cd5\u5230\u8fbe\u3002\u904d\u5386\u975e\u8fde\u901a\u56fe\u9700\u8981\u8bbe\u7f6e\u591a\u4e2a\u8d77\u70b9\uff0c\u4ee5\u904d\u5386\u5230\u56fe\u7684\u6240\u6709\u8fde\u901a\u5206\u91cf\u3002

        Q\uff1a\u5728\u90bb\u63a5\u8868\u4e2d\uff0c\u201c\u4e0e\u8be5\u9876\u70b9\u76f8\u8fde\u7684\u6240\u6709\u9876\u70b9\u201d\u7684\u9876\u70b9\u987a\u5e8f\u662f\u5426\u6709\u8981\u6c42\uff1f

        \u53ef\u4ee5\u662f\u4efb\u610f\u987a\u5e8f\u3002\u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u6309\u7167\u6307\u5b9a\u89c4\u5219\u6765\u6392\u5e8f\uff0c\u6bd4\u5982\u6309\u7167\u9876\u70b9\u6dfb\u52a0\u7684\u6b21\u5e8f\uff0c\u6216\u8005\u6309\u7167\u9876\u70b9\u503c\u5927\u5c0f\u7684\u987a\u5e8f\u7b49\uff0c\u8fd9\u6837\u6709\u52a9\u4e8e\u5feb\u901f\u67e5\u627e\u201c\u5e26\u6709\u67d0\u79cd\u6781\u503c\u201d\u7684\u9876\u70b9\u3002

        "},{"location":"chapter_greedy/","title":"\u7b2c 15 \u7ae0 \u00a0 \u8d2a\u5fc3","text":"

        Abstract

        \u5411\u65e5\u8475\u671d\u7740\u592a\u9633\u8f6c\u52a8\uff0c\u65f6\u523b\u8ffd\u6c42\u81ea\u8eab\u6210\u957f\u7684\u6700\u5927\u53ef\u80fd\u3002

        \u8d2a\u5fc3\u7b56\u7565\u5728\u4e00\u8f6e\u8f6e\u7684\u7b80\u5355\u9009\u62e9\u4e2d\uff0c\u9010\u6b65\u5bfc\u5411\u6700\u4f73\u7b54\u6848\u3002

        "},{"location":"chapter_greedy/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 15.1 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5
        • 15.2 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898
        • 15.3 \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898
        • 15.4 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u95ee\u9898
        • 15.5 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_greedy/fractional_knapsack_problem/","title":"15.2 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898","text":"

        Question

        \u7ed9\u5b9a \\(n\\) \u4e2a\u7269\u54c1\uff0c\u7b2c \\(i\\) \u4e2a\u7269\u54c1\u7684\u91cd\u91cf\u4e3a \\(wgt[i-1]\\)\u3001\u4ef7\u503c\u4e3a \\(val[i-1]\\) \uff0c\u548c\u4e00\u4e2a\u5bb9\u91cf\u4e3a \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u4e2a\u7269\u54c1\u53ea\u80fd\u9009\u62e9\u4e00\u6b21\uff0c\u4f46\u53ef\u4ee5\u9009\u62e9\u7269\u54c1\u7684\u4e00\u90e8\u5206\uff0c\u4ef7\u503c\u6839\u636e\u9009\u62e9\u7684\u91cd\u91cf\u6bd4\u4f8b\u8ba1\u7b97\uff0c\u95ee\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u80cc\u5305\u4e2d\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u3002\u793a\u4f8b\u5982\u56fe 15-3 \u6240\u793a\u3002

        \u56fe 15-3 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e

        \u5206\u6570\u80cc\u5305\u95ee\u9898\u548c 0-1 \u80cc\u5305\u95ee\u9898\u6574\u4f53\u4e0a\u975e\u5e38\u76f8\u4f3c\uff0c\u72b6\u6001\u5305\u542b\u5f53\u524d\u7269\u54c1 \\(i\\) \u548c\u5bb9\u91cf \\(c\\) \uff0c\u76ee\u6807\u662f\u6c42\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u7684\u6700\u5927\u4ef7\u503c\u3002

        \u4e0d\u540c\u70b9\u5728\u4e8e\uff0c\u672c\u9898\u5141\u8bb8\u53ea\u9009\u62e9\u7269\u54c1\u7684\u4e00\u90e8\u5206\u3002\u5982\u56fe 15-4 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u7269\u54c1\u4efb\u610f\u5730\u8fdb\u884c\u5207\u5206\uff0c\u5e76\u6309\u7167\u91cd\u91cf\u6bd4\u4f8b\u6765\u8ba1\u7b97\u76f8\u5e94\u4ef7\u503c\u3002

        1. \u5bf9\u4e8e\u7269\u54c1 \\(i\\) \uff0c\u5b83\u5728\u5355\u4f4d\u91cd\u91cf\u4e0b\u7684\u4ef7\u503c\u4e3a \\(val[i-1] / wgt[i-1]\\) \uff0c\u7b80\u79f0\u5355\u4f4d\u4ef7\u503c\u3002
        2. \u5047\u8bbe\u653e\u5165\u4e00\u90e8\u5206\u7269\u54c1 \\(i\\) \uff0c\u91cd\u91cf\u4e3a \\(w\\) \uff0c\u5219\u80cc\u5305\u589e\u52a0\u7684\u4ef7\u503c\u4e3a \\(w \\times val[i-1] / wgt[i-1]\\) \u3002

        \u56fe 15-4 \u00a0 \u7269\u54c1\u5728\u5355\u4f4d\u91cd\u91cf\u4e0b\u7684\u4ef7\u503c

        "},{"location":"chapter_greedy/fractional_knapsack_problem/#1","title":"1. \u00a0 \u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a","text":"

        \u6700\u5927\u5316\u80cc\u5305\u5185\u7269\u54c1\u603b\u4ef7\u503c\uff0c\u672c\u8d28\u4e0a\u662f\u6700\u5927\u5316\u5355\u4f4d\u91cd\u91cf\u4e0b\u7684\u7269\u54c1\u4ef7\u503c\u3002\u7531\u6b64\u4fbf\u53ef\u63a8\u7406\u51fa\u56fe 15-5 \u6240\u793a\u7684\u8d2a\u5fc3\u7b56\u7565\u3002

        1. \u5c06\u7269\u54c1\u6309\u7167\u5355\u4f4d\u4ef7\u503c\u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\u3002
        2. \u904d\u5386\u6240\u6709\u7269\u54c1\uff0c\u6bcf\u8f6e\u8d2a\u5fc3\u5730\u9009\u62e9\u5355\u4f4d\u4ef7\u503c\u6700\u9ad8\u7684\u7269\u54c1\u3002
        3. \u82e5\u5269\u4f59\u80cc\u5305\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u4f7f\u7528\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u586b\u6ee1\u80cc\u5305\u3002

        \u56fe 15-5 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898\u7684\u8d2a\u5fc3\u7b56\u7565

        "},{"location":"chapter_greedy/fractional_knapsack_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u6211\u4eec\u5efa\u7acb\u4e86\u4e00\u4e2a\u7269\u54c1\u7c7b Item \uff0c\u4ee5\u4fbf\u5c06\u7269\u54c1\u6309\u7167\u5355\u4f4d\u4ef7\u503c\u8fdb\u884c\u6392\u5e8f\u3002\u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u5f53\u80cc\u5305\u5df2\u6ee1\u65f6\u8df3\u51fa\u5e76\u8fd4\u56de\u89e3\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby fractional_knapsack.py
        class Item:\n    \"\"\"\u7269\u54c1\"\"\"\n\n    def __init__(self, w: int, v: int):\n        self.w = w  # \u7269\u54c1\u91cd\u91cf\n        self.v = v  # \u7269\u54c1\u4ef7\u503c\n\ndef fractional_knapsack(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"\u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3\"\"\"\n    # \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n    items = [Item(w, v) for w, v in zip(wgt, val)]\n    # \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n    items.sort(key=lambda item: item.v / item.w, reverse=True)\n    # \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n    res = 0\n    for item in items:\n        if item.w <= cap:\n            # \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n            res += item.v\n            cap -= item.w\n        else:\n            # \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n            res += (item.v / item.w) * cap\n            # \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n            break\n    return res\n
        fractional_knapsack.cpp
        /* \u7269\u54c1 */\nclass Item {\n  public:\n    int w; // \u7269\u54c1\u91cd\u91cf\n    int v; // \u7269\u54c1\u4ef7\u503c\n\n    Item(int w, int v) : w(w), v(v) {\n    }\n};\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble fractionalKnapsack(vector<int> &wgt, vector<int> &val, int cap) {\n    // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n    vector<Item> items;\n    for (int i = 0; i < wgt.size(); i++) {\n        items.push_back(Item(wgt[i], val[i]));\n    }\n    // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n    sort(items.begin(), items.end(), [](Item &a, Item &b) { return (double)a.v / a.w > (double)b.v / b.w; });\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n    double res = 0;\n    for (auto &item : items) {\n        if (item.w <= cap) {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n            res += (double)item.v / item.w * cap;\n            // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n            break;\n        }\n    }\n    return res;\n}\n
        fractional_knapsack.java
        /* \u7269\u54c1 */\nclass Item {\n    int w; // \u7269\u54c1\u91cd\u91cf\n    int v; // \u7269\u54c1\u4ef7\u503c\n\n    public Item(int w, int v) {\n        this.w = w;\n        this.v = v;\n    }\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble fractionalKnapsack(int[] wgt, int[] val, int cap) {\n    // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n    Item[] items = new Item[wgt.length];\n    for (int i = 0; i < wgt.length; i++) {\n        items[i] = new Item(wgt[i], val[i]);\n    }\n    // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n    Arrays.sort(items, Comparator.comparingDouble(item -> -((double) item.v / item.w)));\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n    double res = 0;\n    for (Item item : items) {\n        if (item.w <= cap) {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n            res += (double) item.v / item.w * cap;\n            // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n            break;\n        }\n    }\n    return res;\n}\n
        fractional_knapsack.cs
        /* \u7269\u54c1 */\nclass Item(int w, int v) {\n    public int w = w; // \u7269\u54c1\u91cd\u91cf\n    public int v = v; // \u7269\u54c1\u4ef7\u503c\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble FractionalKnapsack(int[] wgt, int[] val, int cap) {\n    // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n    Item[] items = new Item[wgt.Length];\n    for (int i = 0; i < wgt.Length; i++) {\n        items[i] = new Item(wgt[i], val[i]);\n    }\n    // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n    Array.Sort(items, (x, y) => (y.v / y.w).CompareTo(x.v / x.w));\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n    double res = 0;\n    foreach (Item item in items) {\n        if (item.w <= cap) {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n            res += (double)item.v / item.w * cap;\n            // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n            break;\n        }\n    }\n    return res;\n}\n
        fractional_knapsack.go
        /* \u7269\u54c1 */\ntype Item struct {\n    w int // \u7269\u54c1\u91cd\u91cf\n    v int // \u7269\u54c1\u4ef7\u503c\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunc fractionalKnapsack(wgt []int, val []int, cap int) float64 {\n    // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n    items := make([]Item, len(wgt))\n    for i := 0; i < len(wgt); i++ {\n        items[i] = Item{wgt[i], val[i]}\n    }\n    // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n    sort.Slice(items, func(i, j int) bool {\n        return float64(items[i].v)/float64(items[i].w) > float64(items[j].v)/float64(items[j].w)\n    })\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n    res := 0.0\n    for _, item := range items {\n        if item.w <= cap {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n            res += float64(item.v)\n            cap -= item.w\n        } else {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n            res += float64(item.v) / float64(item.w) * float64(cap)\n            // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n            break\n        }\n    }\n    return res\n}\n
        fractional_knapsack.swift
        /* \u7269\u54c1 */\nclass Item {\n    var w: Int // \u7269\u54c1\u91cd\u91cf\n    var v: Int // \u7269\u54c1\u4ef7\u503c\n\n    init(w: Int, v: Int) {\n        self.w = w\n        self.v = v\n    }\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunc fractionalKnapsack(wgt: [Int], val: [Int], cap: Int) -> Double {\n    // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n    var items = zip(wgt, val).map { Item(w: $0, v: $1) }\n    // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n    items.sort { -(Double($0.v) / Double($0.w)) < -(Double($1.v) / Double($1.w)) }\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n    var res = 0.0\n    var cap = cap\n    for item in items {\n        if item.w <= cap {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n            res += Double(item.v)\n            cap -= item.w\n        } else {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n            res += Double(item.v) / Double(item.w) * Double(cap)\n            // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n            break\n        }\n    }\n    return res\n}\n
        fractional_knapsack.js
        /* \u7269\u54c1 */\nclass Item {\n    constructor(w, v) {\n        this.w = w; // \u7269\u54c1\u91cd\u91cf\n        this.v = v; // \u7269\u54c1\u4ef7\u503c\n    }\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunction fractionalKnapsack(wgt, val, cap) {\n    // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n    const items = wgt.map((w, i) => new Item(w, val[i]));\n    // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n    items.sort((a, b) => b.v / b.w - a.v / a.w);\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n    let res = 0;\n    for (const item of items) {\n        if (item.w <= cap) {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n            res += (item.v / item.w) * cap;\n            // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n            break;\n        }\n    }\n    return res;\n}\n
        fractional_knapsack.ts
        /* \u7269\u54c1 */\nclass Item {\n    w: number; // \u7269\u54c1\u91cd\u91cf\n    v: number; // \u7269\u54c1\u4ef7\u503c\n\n    constructor(w: number, v: number) {\n        this.w = w;\n        this.v = v;\n    }\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunction fractionalKnapsack(wgt: number[], val: number[], cap: number): number {\n    // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n    const items: Item[] = wgt.map((w, i) => new Item(w, val[i]));\n    // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n    items.sort((a, b) => b.v / b.w - a.v / a.w);\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n    let res = 0;\n    for (const item of items) {\n        if (item.w <= cap) {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n            res += (item.v / item.w) * cap;\n            // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n            break;\n        }\n    }\n    return res;\n}\n
        fractional_knapsack.dart
        /* \u7269\u54c1 */\nclass Item {\n  int w; // \u7269\u54c1\u91cd\u91cf\n  int v; // \u7269\u54c1\u4ef7\u503c\n\n  Item(this.w, this.v);\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble fractionalKnapsack(List<int> wgt, List<int> val, int cap) {\n  // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n  List<Item> items = List.generate(wgt.length, (i) => Item(wgt[i], val[i]));\n  // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n  items.sort((a, b) => (b.v / b.w).compareTo(a.v / a.w));\n  // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n  double res = 0;\n  for (Item item in items) {\n    if (item.w <= cap) {\n      // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n      res += item.v;\n      cap -= item.w;\n    } else {\n      // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n      res += item.v / item.w * cap;\n      // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n      break;\n    }\n  }\n  return res;\n}\n
        fractional_knapsack.rs
        /* \u7269\u54c1 */\nstruct Item {\n    w: i32, // \u7269\u54c1\u91cd\u91cf\n    v: i32, // \u7269\u54c1\u4ef7\u503c\n}\n\nimpl Item {\n    fn new(w: i32, v: i32) -> Self {\n        Self { w, v }\n    }\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfn fractional_knapsack(wgt: &[i32], val: &[i32], mut cap: i32) -> f64 {\n    // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n    let mut items = wgt\n        .iter()\n        .zip(val.iter())\n        .map(|(&w, &v)| Item::new(w, v))\n        .collect::<Vec<Item>>();\n    // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n    items.sort_by(|a, b| {\n        (b.v as f64 / b.w as f64)\n            .partial_cmp(&(a.v as f64 / a.w as f64))\n            .unwrap()\n    });\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n    let mut res = 0.0;\n    for item in &items {\n        if item.w <= cap {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n            res += item.v as f64;\n            cap -= item.w;\n        } else {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n            res += item.v as f64 / item.w as f64 * cap as f64;\n            // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n            break;\n        }\n    }\n    res\n}\n
        fractional_knapsack.c
        /* \u7269\u54c1 */\ntypedef struct {\n    int w; // \u7269\u54c1\u91cd\u91cf\n    int v; // \u7269\u54c1\u4ef7\u503c\n} Item;\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfloat fractionalKnapsack(int wgt[], int val[], int itemCount, int cap) {\n    // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n    Item *items = malloc(sizeof(Item) * itemCount);\n    for (int i = 0; i < itemCount; i++) {\n        items[i] = (Item){.w = wgt[i], .v = val[i]};\n    }\n    // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n    qsort(items, (size_t)itemCount, sizeof(Item), sortByValueDensity);\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n    float res = 0.0;\n    for (int i = 0; i < itemCount; i++) {\n        if (items[i].w <= cap) {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n            res += items[i].v;\n            cap -= items[i].w;\n        } else {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n            res += (float)cap / items[i].w * items[i].v;\n            cap = 0;\n            break;\n        }\n    }\n    free(items);\n    return res;\n}\n
        fractional_knapsack.kt
        /* \u7269\u54c1 */\nclass Item(\n    val w: Int, // \u7269\u54c1\n    val v: Int  // \u7269\u54c1\u4ef7\u503c\n)\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfun fractionalKnapsack(wgt: IntArray, _val: IntArray, c: Int): Double {\n    // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n    var cap = c\n    val items = arrayOfNulls<Item>(wgt.size)\n    for (i in wgt.indices) {\n        items[i] = Item(wgt[i], _val[i])\n    }\n    // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n    items.sortBy { item: Item? -> -(item!!.v.toDouble() / item.w) }\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n    var res = 0.0\n    for (item in items) {\n        if (item!!.w <= cap) {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n            res += item.v\n            cap -= item.w\n        } else {\n            // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n            res += item.v.toDouble() / item.w * cap\n            // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n            break\n        }\n    }\n    return res\n}\n
        fractional_knapsack.rb
        ### \u7269\u54c1 ###\nclass Item\n  attr_accessor :w # \u7269\u54c1\u91cd\u91cf\n  attr_accessor :v # \u7269\u54c1\u4ef7\u503c\n\n  def initialize(w, v)\n    @w = w\n    @v = v\n  end\nend\n\n### \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 ###\ndef fractional_knapsack(wgt, val, cap)\n  # \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\uff0c\u4ef7\u503c\n  items = wgt.each_with_index.map { |w, i| Item.new(w, val[i]) }\n  # \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n  items.sort! { |a, b| (b.v.to_f / b.w) <=> (a.v.to_f / a.w) }\n  # \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n  res = 0\n  for item in items\n    if item.w <= cap\n      # \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n      res += item.v\n      cap -= item.w\n    else\n      # \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n      res += (item.v.to_f / item.w) * cap\n      # \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n      break\n    end\n  end\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5185\u7f6e\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3a \\(O(\\log n)\\) \uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3a \\(O(\\log n)\\) \u6216 \\(O(n)\\) \uff0c\u53d6\u51b3\u4e8e\u7f16\u7a0b\u8bed\u8a00\u7684\u5177\u4f53\u5b9e\u73b0\u3002

        \u9664\u6392\u5e8f\u4e4b\u5916\uff0c\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u904d\u5386\u6574\u4e2a\u7269\u54c1\u5217\u8868\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u7269\u54c1\u6570\u91cf\u3002

        \u7531\u4e8e\u521d\u59cb\u5316\u4e86\u4e00\u4e2a Item \u5bf9\u8c61\u5217\u8868\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

        "},{"location":"chapter_greedy/fractional_knapsack_problem/#3","title":"3. \u00a0 \u6b63\u786e\u6027\u8bc1\u660e","text":"

        \u91c7\u7528\u53cd\u8bc1\u6cd5\u3002\u5047\u8bbe\u7269\u54c1 \\(x\\) \u662f\u5355\u4f4d\u4ef7\u503c\u6700\u9ad8\u7684\u7269\u54c1\uff0c\u4f7f\u7528\u67d0\u7b97\u6cd5\u6c42\u5f97\u6700\u5927\u4ef7\u503c\u4e3a res \uff0c\u4f46\u8be5\u89e3\u4e2d\u4e0d\u5305\u542b\u7269\u54c1 \\(x\\) \u3002

        \u73b0\u5728\u4ece\u80cc\u5305\u4e2d\u62ff\u51fa\u5355\u4f4d\u91cd\u91cf\u7684\u4efb\u610f\u7269\u54c1\uff0c\u5e76\u66ff\u6362\u4e3a\u5355\u4f4d\u91cd\u91cf\u7684\u7269\u54c1 \\(x\\) \u3002\u7531\u4e8e\u7269\u54c1 \\(x\\) \u7684\u5355\u4f4d\u4ef7\u503c\u6700\u9ad8\uff0c\u56e0\u6b64\u66ff\u6362\u540e\u7684\u603b\u4ef7\u503c\u4e00\u5b9a\u5927\u4e8e res \u3002\u8fd9\u4e0e res \u662f\u6700\u4f18\u89e3\u77db\u76fe\uff0c\u8bf4\u660e\u6700\u4f18\u89e3\u4e2d\u5fc5\u987b\u5305\u542b\u7269\u54c1 \\(x\\) \u3002

        \u5bf9\u4e8e\u8be5\u89e3\u4e2d\u7684\u5176\u4ed6\u7269\u54c1\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u6784\u5efa\u51fa\u4e0a\u8ff0\u77db\u76fe\u3002\u603b\u800c\u8a00\u4e4b\uff0c\u5355\u4f4d\u4ef7\u503c\u66f4\u5927\u7684\u7269\u54c1\u603b\u662f\u66f4\u4f18\u9009\u62e9\uff0c\u8fd9\u8bf4\u660e\u8d2a\u5fc3\u7b56\u7565\u662f\u6709\u6548\u7684\u3002

        \u5982\u56fe 15-6 \u6240\u793a\uff0c\u5982\u679c\u5c06\u7269\u54c1\u91cd\u91cf\u548c\u7269\u54c1\u5355\u4f4d\u4ef7\u503c\u5206\u522b\u770b\u4f5c\u4e00\u5f20\u4e8c\u7ef4\u56fe\u8868\u7684\u6a2a\u8f74\u548c\u7eb5\u8f74\uff0c\u5219\u5206\u6570\u80cc\u5305\u95ee\u9898\u53ef\u8f6c\u5316\u4e3a\u201c\u6c42\u5728\u6709\u9650\u6a2a\u8f74\u533a\u95f4\u4e0b\u56f4\u6210\u7684\u6700\u5927\u9762\u79ef\u201d\u3002\u8fd9\u4e2a\u7c7b\u6bd4\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4ece\u51e0\u4f55\u89d2\u5ea6\u7406\u89e3\u8d2a\u5fc3\u7b56\u7565\u7684\u6709\u6548\u6027\u3002

        \u56fe 15-6 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898\u7684\u51e0\u4f55\u8868\u793a

        "},{"location":"chapter_greedy/greedy_algorithm/","title":"15.1 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5","text":"

        \u8d2a\u5fc3\u7b97\u6cd5\uff08greedy algorithm\uff09\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u89e3\u51b3\u4f18\u5316\u95ee\u9898\u7684\u7b97\u6cd5\uff0c\u5176\u57fa\u672c\u601d\u60f3\u662f\u5728\u95ee\u9898\u7684\u6bcf\u4e2a\u51b3\u7b56\u9636\u6bb5\uff0c\u90fd\u9009\u62e9\u5f53\u524d\u770b\u8d77\u6765\u6700\u4f18\u7684\u9009\u62e9\uff0c\u5373\u8d2a\u5fc3\u5730\u505a\u51fa\u5c40\u90e8\u6700\u4f18\u7684\u51b3\u7b56\uff0c\u4ee5\u671f\u83b7\u5f97\u5168\u5c40\u6700\u4f18\u89e3\u3002\u8d2a\u5fc3\u7b97\u6cd5\u7b80\u6d01\u4e14\u9ad8\u6548\uff0c\u5728\u8bb8\u591a\u5b9e\u9645\u95ee\u9898\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002

        \u8d2a\u5fc3\u7b97\u6cd5\u548c\u52a8\u6001\u89c4\u5212\u90fd\u5e38\u7528\u4e8e\u89e3\u51b3\u4f18\u5316\u95ee\u9898\u3002\u5b83\u4eec\u4e4b\u95f4\u5b58\u5728\u4e00\u4e9b\u76f8\u4f3c\u4e4b\u5904\uff0c\u6bd4\u5982\u90fd\u4f9d\u8d56\u6700\u4f18\u5b50\u7ed3\u6784\u6027\u8d28\uff0c\u4f46\u5de5\u4f5c\u539f\u7406\u4e0d\u540c\u3002

        • \u52a8\u6001\u89c4\u5212\u4f1a\u6839\u636e\u4e4b\u524d\u9636\u6bb5\u7684\u6240\u6709\u51b3\u7b56\u6765\u8003\u8651\u5f53\u524d\u51b3\u7b56\uff0c\u5e76\u4f7f\u7528\u8fc7\u53bb\u5b50\u95ee\u9898\u7684\u89e3\u6765\u6784\u5efa\u5f53\u524d\u5b50\u95ee\u9898\u7684\u89e3\u3002
        • \u8d2a\u5fc3\u7b97\u6cd5\u4e0d\u4f1a\u8003\u8651\u8fc7\u53bb\u7684\u51b3\u7b56\uff0c\u800c\u662f\u4e00\u8def\u5411\u524d\u5730\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u4e0d\u65ad\u7f29\u5c0f\u95ee\u9898\u8303\u56f4\uff0c\u76f4\u81f3\u95ee\u9898\u88ab\u89e3\u51b3\u3002

        \u6211\u4eec\u5148\u901a\u8fc7\u4f8b\u9898\u201c\u96f6\u94b1\u5151\u6362\u201d\u4e86\u89e3\u8d2a\u5fc3\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u3002\u8fd9\u9053\u9898\u5df2\u7ecf\u5728\u201c\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u201d\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u76f8\u4fe1\u4f60\u5bf9\u5b83\u5e76\u4e0d\u964c\u751f\u3002

        Question

        \u7ed9\u5b9a \\(n\\) \u79cd\u786c\u5e01\uff0c\u7b2c \\(i\\) \u79cd\u786c\u5e01\u7684\u9762\u503c\u4e3a \\(coins[i - 1]\\) \uff0c\u76ee\u6807\u91d1\u989d\u4e3a \\(amt\\) \uff0c\u6bcf\u79cd\u786c\u5e01\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u80fd\u591f\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\u3002\u5982\u679c\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\uff0c\u5219\u8fd4\u56de \\(-1\\) \u3002

        \u672c\u9898\u91c7\u53d6\u7684\u8d2a\u5fc3\u7b56\u7565\u5982\u56fe 15-1 \u6240\u793a\u3002\u7ed9\u5b9a\u76ee\u6807\u91d1\u989d\uff0c\u6211\u4eec\u8d2a\u5fc3\u5730\u9009\u62e9\u4e0d\u5927\u4e8e\u4e14\u6700\u63a5\u8fd1\u5b83\u7684\u786c\u5e01\uff0c\u4e0d\u65ad\u5faa\u73af\u8be5\u6b65\u9aa4\uff0c\u76f4\u81f3\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u4e3a\u6b62\u3002

        \u56fe 15-1 \u00a0 \u96f6\u94b1\u5151\u6362\u7684\u8d2a\u5fc3\u7b56\u7565

        \u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change_greedy.py
        def coin_change_greedy(coins: list[int], amt: int) -> int:\n    \"\"\"\u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3\"\"\"\n    # \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n    i = len(coins) - 1\n    count = 0\n    # \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n    while amt > 0:\n        # \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n        while i > 0 and coins[i] > amt:\n            i -= 1\n        # \u9009\u62e9 coins[i]\n        amt -= coins[i]\n        count += 1\n    # \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n    return count if amt == 0 else -1\n
        coin_change_greedy.cpp
        /* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(vector<int> &coins, int amt) {\n    // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n    int i = coins.size() - 1;\n    int count = 0;\n    // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n    while (amt > 0) {\n        // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // \u9009\u62e9 coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n    return amt == 0 ? count : -1;\n}\n
        coin_change_greedy.java
        /* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(int[] coins, int amt) {\n    // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n    int i = coins.length - 1;\n    int count = 0;\n    // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n    while (amt > 0) {\n        // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // \u9009\u62e9 coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n    return amt == 0 ? count : -1;\n}\n
        coin_change_greedy.cs
        /* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint CoinChangeGreedy(int[] coins, int amt) {\n    // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n    int i = coins.Length - 1;\n    int count = 0;\n    // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n    while (amt > 0) {\n        // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // \u9009\u62e9 coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n    return amt == 0 ? count : -1;\n}\n
        coin_change_greedy.go
        /* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunc coinChangeGreedy(coins []int, amt int) int {\n    // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n    i := len(coins) - 1\n    count := 0\n    // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n    for amt > 0 {\n        // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n        for i > 0 && coins[i] > amt {\n            i--\n        }\n        // \u9009\u62e9 coins[i]\n        amt -= coins[i]\n        count++\n    }\n    // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n    if amt != 0 {\n        return -1\n    }\n    return count\n}\n
        coin_change_greedy.swift
        /* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunc coinChangeGreedy(coins: [Int], amt: Int) -> Int {\n    // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n    var i = coins.count - 1\n    var count = 0\n    var amt = amt\n    // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n    while amt > 0 {\n        // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n        while i > 0 && coins[i] > amt {\n            i -= 1\n        }\n        // \u9009\u62e9 coins[i]\n        amt -= coins[i]\n        count += 1\n    }\n    // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n    return amt == 0 ? count : -1\n}\n
        coin_change_greedy.js
        /* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunction coinChangeGreedy(coins, amt) {\n    // \u5047\u8bbe coins \u6570\u7ec4\u6709\u5e8f\n    let i = coins.length - 1;\n    let count = 0;\n    // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n    while (amt > 0) {\n        // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // \u9009\u62e9 coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n    return amt === 0 ? count : -1;\n}\n
        coin_change_greedy.ts
        /* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunction coinChangeGreedy(coins: number[], amt: number): number {\n    // \u5047\u8bbe coins \u6570\u7ec4\u6709\u5e8f\n    let i = coins.length - 1;\n    let count = 0;\n    // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n    while (amt > 0) {\n        // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // \u9009\u62e9 coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n    return amt === 0 ? count : -1;\n}\n
        coin_change_greedy.dart
        /* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(List<int> coins, int amt) {\n  // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n  int i = coins.length - 1;\n  int count = 0;\n  // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n  while (amt > 0) {\n    // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n    while (i > 0 && coins[i] > amt) {\n      i--;\n    }\n    // \u9009\u62e9 coins[i]\n    amt -= coins[i];\n    count++;\n  }\n  // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n  return amt == 0 ? count : -1;\n}\n
        coin_change_greedy.rs
        /* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfn coin_change_greedy(coins: &[i32], mut amt: i32) -> i32 {\n    // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n    let mut i = coins.len() - 1;\n    let mut count = 0;\n    // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n    while amt > 0 {\n        // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n        while i > 0 && coins[i] > amt {\n            i -= 1;\n        }\n        // \u9009\u62e9 coins[i]\n        amt -= coins[i];\n        count += 1;\n    }\n    // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n    if amt == 0 {\n        count\n    } else {\n        -1\n    }\n}\n
        coin_change_greedy.c
        /* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(int *coins, int size, int amt) {\n    // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n    int i = size - 1;\n    int count = 0;\n    // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n    while (amt > 0) {\n        // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // \u9009\u62e9 coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n    return amt == 0 ? count : -1;\n}\n
        coin_change_greedy.kt
        /* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfun coinChangeGreedy(coins: IntArray, amt: Int): Int {\n    // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n    var am = amt\n    var i = coins.size - 1\n    var count = 0\n    // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n    while (am > 0) {\n        // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n        while (i > 0 && coins[i] > am) {\n            i--\n        }\n        // \u9009\u62e9 coins[i]\n        am -= coins[i]\n        count++\n    }\n    // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n    return if (am == 0) count else -1\n}\n
        coin_change_greedy.rb
        ### \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 ###\ndef coin_change_greedy(coins, amt)\n  # \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n  i = coins.length - 1\n  count = 0\n  # \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n  while amt > 0\n    # \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n    while i > 0 && coins[i] > amt\n      i -= 1\n    end\n    # \u9009\u62e9 coins[i]\n    amt -= coins[i]\n    count += 1\n  end\n  # \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c \u5219\u8fd4\u56de -1\n  amt == 0 ? count : -1\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u4f60\u53ef\u80fd\u4f1a\u4e0d\u7531\u5730\u53d1\u51fa\u611f\u53f9\uff1aSo clean \uff01\u8d2a\u5fc3\u7b97\u6cd5\u4ec5\u7528\u7ea6\u5341\u884c\u4ee3\u7801\u5c31\u89e3\u51b3\u4e86\u96f6\u94b1\u5151\u6362\u95ee\u9898\u3002

        "},{"location":"chapter_greedy/greedy_algorithm/#1511","title":"15.1.1 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5\u7684\u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"

        \u8d2a\u5fc3\u7b97\u6cd5\u4e0d\u4ec5\u64cd\u4f5c\u76f4\u63a5\u3001\u5b9e\u73b0\u7b80\u5355\uff0c\u800c\u4e14\u901a\u5e38\u6548\u7387\u4e5f\u5f88\u9ad8\u3002\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u8bb0\u786c\u5e01\u6700\u5c0f\u9762\u503c\u4e3a \\(\\min(coins)\\) \uff0c\u5219\u8d2a\u5fc3\u9009\u62e9\u6700\u591a\u5faa\u73af \\(amt / \\min(coins)\\) \u6b21\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(amt / \\min(coins))\\) \u3002\u8fd9\u6bd4\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n \\times amt)\\) \u5c0f\u4e86\u4e00\u4e2a\u6570\u91cf\u7ea7\u3002

        \u7136\u800c\uff0c\u5bf9\u4e8e\u67d0\u4e9b\u786c\u5e01\u9762\u503c\u7ec4\u5408\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u5e76\u4e0d\u80fd\u627e\u5230\u6700\u4f18\u89e3\u3002\u56fe 15-2 \u7ed9\u51fa\u4e86\u4e24\u4e2a\u793a\u4f8b\u3002

        • \u6b63\u4f8b \\(coins = [1, 5, 10, 20, 50, 100]\\)\uff1a\u5728\u8be5\u786c\u5e01\u7ec4\u5408\u4e0b\uff0c\u7ed9\u5b9a\u4efb\u610f \\(amt\\) \uff0c\u8d2a\u5fc3\u7b97\u6cd5\u90fd\u53ef\u4ee5\u627e\u5230\u6700\u4f18\u89e3\u3002
        • \u53cd\u4f8b \\(coins = [1, 20, 50]\\)\uff1a\u5047\u8bbe \\(amt = 60\\) \uff0c\u8d2a\u5fc3\u7b97\u6cd5\u53ea\u80fd\u627e\u5230 \\(50 + 1 \\times 10\\) \u7684\u5151\u6362\u7ec4\u5408\uff0c\u5171\u8ba1 \\(11\\) \u679a\u786c\u5e01\uff0c\u4f46\u52a8\u6001\u89c4\u5212\u53ef\u4ee5\u627e\u5230\u6700\u4f18\u89e3 \\(20 + 20 + 20\\) \uff0c\u4ec5\u9700 \\(3\\) \u679a\u786c\u5e01\u3002
        • \u53cd\u4f8b \\(coins = [1, 49, 50]\\)\uff1a\u5047\u8bbe \\(amt = 98\\) \uff0c\u8d2a\u5fc3\u7b97\u6cd5\u53ea\u80fd\u627e\u5230 \\(50 + 1 \\times 48\\) \u7684\u5151\u6362\u7ec4\u5408\uff0c\u5171\u8ba1 \\(49\\) \u679a\u786c\u5e01\uff0c\u4f46\u52a8\u6001\u89c4\u5212\u53ef\u4ee5\u627e\u5230\u6700\u4f18\u89e3 \\(49 + 49\\) \uff0c\u4ec5\u9700 \\(2\\) \u679a\u786c\u5e01\u3002

        \u56fe 15-2 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5\u65e0\u6cd5\u627e\u51fa\u6700\u4f18\u89e3\u7684\u793a\u4f8b

        \u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u4e8e\u96f6\u94b1\u5151\u6362\u95ee\u9898\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u65e0\u6cd5\u4fdd\u8bc1\u627e\u5230\u5168\u5c40\u6700\u4f18\u89e3\uff0c\u5e76\u4e14\u6709\u53ef\u80fd\u627e\u5230\u975e\u5e38\u5dee\u7684\u89e3\u3002\u5b83\u66f4\u9002\u5408\u7528\u52a8\u6001\u89c4\u5212\u89e3\u51b3\u3002

        \u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u7684\u9002\u7528\u60c5\u51b5\u5206\u4ee5\u4e0b\u4e24\u79cd\u3002

        1. \u53ef\u4ee5\u4fdd\u8bc1\u627e\u5230\u6700\u4f18\u89e3\uff1a\u8d2a\u5fc3\u7b97\u6cd5\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5f80\u5f80\u662f\u6700\u4f18\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u5f80\u5f80\u6bd4\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\u66f4\u9ad8\u6548\u3002
        2. \u53ef\u4ee5\u627e\u5230\u8fd1\u4f3c\u6700\u4f18\u89e3\uff1a\u8d2a\u5fc3\u7b97\u6cd5\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e5f\u662f\u53ef\u7528\u7684\u3002\u5bf9\u4e8e\u5f88\u591a\u590d\u6742\u95ee\u9898\u6765\u8bf4\uff0c\u5bfb\u627e\u5168\u5c40\u6700\u4f18\u89e3\u975e\u5e38\u56f0\u96be\uff0c\u80fd\u4ee5\u8f83\u9ad8\u6548\u7387\u627e\u5230\u6b21\u4f18\u89e3\u4e5f\u662f\u975e\u5e38\u4e0d\u9519\u7684\u3002
        "},{"location":"chapter_greedy/greedy_algorithm/#1512","title":"15.1.2 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5\u7279\u6027","text":"

        \u90a3\u4e48\u95ee\u9898\u6765\u4e86\uff0c\u4ec0\u4e48\u6837\u7684\u95ee\u9898\u9002\u5408\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\u5462\uff1f\u6216\u8005\u8bf4\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4fdd\u8bc1\u627e\u5230\u6700\u4f18\u89e3\uff1f

        \u76f8\u8f83\u4e8e\u52a8\u6001\u89c4\u5212\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u7684\u4f7f\u7528\u6761\u4ef6\u66f4\u52a0\u82db\u523b\uff0c\u5176\u4e3b\u8981\u5173\u6ce8\u95ee\u9898\u7684\u4e24\u4e2a\u6027\u8d28\u3002

        • \u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\uff1a\u53ea\u6709\u5f53\u5c40\u90e8\u6700\u4f18\u9009\u62e9\u59cb\u7ec8\u53ef\u4ee5\u5bfc\u81f4\u5168\u5c40\u6700\u4f18\u89e3\u65f6\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u624d\u80fd\u4fdd\u8bc1\u5f97\u5230\u6700\u4f18\u89e3\u3002
        • \u6700\u4f18\u5b50\u7ed3\u6784\uff1a\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u5305\u542b\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u3002

        \u6700\u4f18\u5b50\u7ed3\u6784\u5df2\u7ecf\u5728\u201c\u52a8\u6001\u89c4\u5212\u201d\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u8fd9\u91cc\u4e0d\u518d\u8d58\u8ff0\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u4e00\u4e9b\u95ee\u9898\u7684\u6700\u4f18\u5b50\u7ed3\u6784\u5e76\u4e0d\u660e\u663e\uff0c\u4f46\u4ecd\u7136\u53ef\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u89e3\u51b3\u3002

        \u6211\u4eec\u4e3b\u8981\u63a2\u7a76\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u7684\u5224\u65ad\u65b9\u6cd5\u3002\u867d\u7136\u5b83\u7684\u63cf\u8ff0\u770b\u4e0a\u53bb\u6bd4\u8f83\u7b80\u5355\uff0c\u4f46\u5b9e\u9645\u4e0a\u5bf9\u4e8e\u8bb8\u591a\u95ee\u9898\uff0c\u8bc1\u660e\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u5e76\u975e\u6613\u4e8b\u3002

        \u4f8b\u5982\u96f6\u94b1\u5151\u6362\u95ee\u9898\uff0c\u6211\u4eec\u867d\u7136\u80fd\u591f\u5bb9\u6613\u5730\u4e3e\u51fa\u53cd\u4f8b\uff0c\u5bf9\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u8fdb\u884c\u8bc1\u4f2a\uff0c\u4f46\u8bc1\u5b9e\u7684\u96be\u5ea6\u8f83\u5927\u3002\u5982\u679c\u95ee\uff1a\u6ee1\u8db3\u4ec0\u4e48\u6761\u4ef6\u7684\u786c\u5e01\u7ec4\u5408\u53ef\u4ee5\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\uff1f\u6211\u4eec\u5f80\u5f80\u53ea\u80fd\u51ed\u501f\u76f4\u89c9\u6216\u4e3e\u4f8b\u5b50\u6765\u7ed9\u51fa\u4e00\u4e2a\u6a21\u68f1\u4e24\u53ef\u7684\u7b54\u6848\uff0c\u800c\u96be\u4ee5\u7ed9\u51fa\u4e25\u8c28\u7684\u6570\u5b66\u8bc1\u660e\u3002

        Quote

        \u6709\u4e00\u7bc7\u8bba\u6587\u7ed9\u51fa\u4e86\u4e00\u4e2a \\(O(n^3)\\) \u65f6\u95f4\u590d\u6742\u5ea6\u7684\u7b97\u6cd5\uff0c\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u786c\u5e01\u7ec4\u5408\u80fd\u5426\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u627e\u51fa\u4efb\u610f\u91d1\u989d\u7684\u6700\u4f18\u89e3\u3002

        Pearson, D. A polynomial-time algorithm for the change-making problem[J]. Operations Research Letters, 2005, 33(3): 231-234.

        "},{"location":"chapter_greedy/greedy_algorithm/#1513","title":"15.1.3 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5\u89e3\u9898\u6b65\u9aa4","text":"

        \u8d2a\u5fc3\u95ee\u9898\u7684\u89e3\u51b3\u6d41\u7a0b\u5927\u4f53\u53ef\u5206\u4e3a\u4ee5\u4e0b\u4e09\u6b65\u3002

        1. \u95ee\u9898\u5206\u6790\uff1a\u68b3\u7406\u4e0e\u7406\u89e3\u95ee\u9898\u7279\u6027\uff0c\u5305\u62ec\u72b6\u6001\u5b9a\u4e49\u3001\u4f18\u5316\u76ee\u6807\u548c\u7ea6\u675f\u6761\u4ef6\u7b49\u3002\u8fd9\u4e00\u6b65\u5728\u56de\u6eaf\u548c\u52a8\u6001\u89c4\u5212\u4e2d\u90fd\u6709\u6d89\u53ca\u3002
        2. \u786e\u5b9a\u8d2a\u5fc3\u7b56\u7565\uff1a\u786e\u5b9a\u5982\u4f55\u5728\u6bcf\u4e00\u6b65\u4e2d\u505a\u51fa\u8d2a\u5fc3\u9009\u62e9\u3002\u8fd9\u4e2a\u7b56\u7565\u80fd\u591f\u5728\u6bcf\u4e00\u6b65\u51cf\u5c0f\u95ee\u9898\u7684\u89c4\u6a21\uff0c\u5e76\u6700\u7ec8\u89e3\u51b3\u6574\u4e2a\u95ee\u9898\u3002
        3. \u6b63\u786e\u6027\u8bc1\u660e\uff1a\u901a\u5e38\u9700\u8981\u8bc1\u660e\u95ee\u9898\u5177\u6709\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u548c\u6700\u4f18\u5b50\u7ed3\u6784\u3002\u8fd9\u4e2a\u6b65\u9aa4\u53ef\u80fd\u9700\u8981\u7528\u5230\u6570\u5b66\u8bc1\u660e\uff0c\u4f8b\u5982\u5f52\u7eb3\u6cd5\u6216\u53cd\u8bc1\u6cd5\u7b49\u3002

        \u786e\u5b9a\u8d2a\u5fc3\u7b56\u7565\u662f\u6c42\u89e3\u95ee\u9898\u7684\u6838\u5fc3\u6b65\u9aa4\uff0c\u4f46\u5b9e\u65bd\u8d77\u6765\u53ef\u80fd\u5e76\u4e0d\u5bb9\u6613\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002

        • \u4e0d\u540c\u95ee\u9898\u7684\u8d2a\u5fc3\u7b56\u7565\u7684\u5dee\u5f02\u8f83\u5927\u3002\u5bf9\u4e8e\u8bb8\u591a\u95ee\u9898\u6765\u8bf4\uff0c\u8d2a\u5fc3\u7b56\u7565\u6bd4\u8f83\u6d45\u663e\uff0c\u6211\u4eec\u901a\u8fc7\u4e00\u4e9b\u5927\u6982\u7684\u601d\u8003\u4e0e\u5c1d\u8bd5\u5c31\u80fd\u5f97\u51fa\u3002\u800c\u5bf9\u4e8e\u4e00\u4e9b\u590d\u6742\u95ee\u9898\uff0c\u8d2a\u5fc3\u7b56\u7565\u53ef\u80fd\u975e\u5e38\u9690\u853d\uff0c\u8fd9\u79cd\u60c5\u51b5\u5c31\u975e\u5e38\u8003\u9a8c\u4e2a\u4eba\u7684\u89e3\u9898\u7ecf\u9a8c\u4e0e\u7b97\u6cd5\u80fd\u529b\u4e86\u3002
        • \u67d0\u4e9b\u8d2a\u5fc3\u7b56\u7565\u5177\u6709\u8f83\u5f3a\u7684\u8ff7\u60d1\u6027\u3002\u5f53\u6211\u4eec\u6ee1\u6000\u4fe1\u5fc3\u8bbe\u8ba1\u597d\u8d2a\u5fc3\u7b56\u7565\uff0c\u5199\u51fa\u89e3\u9898\u4ee3\u7801\u5e76\u63d0\u4ea4\u8fd0\u884c\uff0c\u5f88\u53ef\u80fd\u53d1\u73b0\u90e8\u5206\u6d4b\u8bd5\u6837\u4f8b\u65e0\u6cd5\u901a\u8fc7\u3002\u8fd9\u662f\u56e0\u4e3a\u8bbe\u8ba1\u7684\u8d2a\u5fc3\u7b56\u7565\u53ea\u662f\u201c\u90e8\u5206\u6b63\u786e\u201d\u7684\uff0c\u4e0a\u6587\u4ecb\u7ecd\u7684\u96f6\u94b1\u5151\u6362\u5c31\u662f\u4e00\u4e2a\u5178\u578b\u6848\u4f8b\u3002

        \u4e3a\u4e86\u4fdd\u8bc1\u6b63\u786e\u6027\uff0c\u6211\u4eec\u5e94\u8be5\u5bf9\u8d2a\u5fc3\u7b56\u7565\u8fdb\u884c\u4e25\u8c28\u7684\u6570\u5b66\u8bc1\u660e\uff0c\u901a\u5e38\u9700\u8981\u7528\u5230\u53cd\u8bc1\u6cd5\u6216\u6570\u5b66\u5f52\u7eb3\u6cd5\u3002

        \u7136\u800c\uff0c\u6b63\u786e\u6027\u8bc1\u660e\u4e5f\u5f88\u53ef\u80fd\u4e0d\u662f\u4e00\u4ef6\u6613\u4e8b\u3002\u5982\u82e5\u6ca1\u6709\u5934\u7eea\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u9009\u62e9\u9762\u5411\u6d4b\u8bd5\u7528\u4f8b\u8fdb\u884c\u4ee3\u7801\u8c03\u8bd5\uff0c\u4e00\u6b65\u6b65\u4fee\u6539\u4e0e\u9a8c\u8bc1\u8d2a\u5fc3\u7b56\u7565\u3002

        "},{"location":"chapter_greedy/greedy_algorithm/#1514","title":"15.1.4 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5\u5178\u578b\u4f8b\u9898","text":"

        \u8d2a\u5fc3\u7b97\u6cd5\u5e38\u5e38\u5e94\u7528\u5728\u6ee1\u8db3\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u548c\u6700\u4f18\u5b50\u7ed3\u6784\u7684\u4f18\u5316\u95ee\u9898\u4e2d\uff0c\u4ee5\u4e0b\u5217\u4e3e\u4e86\u4e00\u4e9b\u5178\u578b\u7684\u8d2a\u5fc3\u7b97\u6cd5\u95ee\u9898\u3002

        • \u786c\u5e01\u627e\u96f6\u95ee\u9898\uff1a\u5728\u67d0\u4e9b\u786c\u5e01\u7ec4\u5408\u4e0b\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u603b\u662f\u53ef\u4ee5\u5f97\u5230\u6700\u4f18\u89e3\u3002
        • \u533a\u95f4\u8c03\u5ea6\u95ee\u9898\uff1a\u5047\u8bbe\u4f60\u6709\u4e00\u4e9b\u4efb\u52a1\uff0c\u6bcf\u4e2a\u4efb\u52a1\u5728\u4e00\u6bb5\u65f6\u95f4\u5185\u8fdb\u884c\uff0c\u4f60\u7684\u76ee\u6807\u662f\u5b8c\u6210\u5c3d\u53ef\u80fd\u591a\u7684\u4efb\u52a1\u3002\u5982\u679c\u6bcf\u6b21\u90fd\u9009\u62e9\u7ed3\u675f\u65f6\u95f4\u6700\u65e9\u7684\u4efb\u52a1\uff0c\u90a3\u4e48\u8d2a\u5fc3\u7b97\u6cd5\u5c31\u53ef\u4ee5\u5f97\u5230\u6700\u4f18\u89e3\u3002
        • \u5206\u6570\u80cc\u5305\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u7ec4\u7269\u54c1\u548c\u4e00\u4e2a\u8f7d\u91cd\u91cf\uff0c\u4f60\u7684\u76ee\u6807\u662f\u9009\u62e9\u4e00\u7ec4\u7269\u54c1\uff0c\u4f7f\u5f97\u603b\u91cd\u91cf\u4e0d\u8d85\u8fc7\u8f7d\u91cd\u91cf\uff0c\u4e14\u603b\u4ef7\u503c\u6700\u5927\u3002\u5982\u679c\u6bcf\u6b21\u90fd\u9009\u62e9\u6027\u4ef7\u6bd4\u6700\u9ad8\uff08\u4ef7\u503c / \u91cd\u91cf\uff09\u7684\u7269\u54c1\uff0c\u90a3\u4e48\u8d2a\u5fc3\u7b97\u6cd5\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\u53ef\u4ee5\u5f97\u5230\u6700\u4f18\u89e3\u3002
        • \u80a1\u7968\u4e70\u5356\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u7ec4\u80a1\u7968\u7684\u5386\u53f2\u4ef7\u683c\uff0c\u4f60\u53ef\u4ee5\u8fdb\u884c\u591a\u6b21\u4e70\u5356\uff0c\u4f46\u5982\u679c\u4f60\u5df2\u7ecf\u6301\u6709\u80a1\u7968\uff0c\u90a3\u4e48\u5728\u5356\u51fa\u4e4b\u524d\u4e0d\u80fd\u518d\u4e70\uff0c\u76ee\u6807\u662f\u83b7\u53d6\u6700\u5927\u5229\u6da6\u3002
        • \u970d\u592b\u66fc\u7f16\u7801\uff1a\u970d\u592b\u66fc\u7f16\u7801\u662f\u4e00\u79cd\u7528\u4e8e\u65e0\u635f\u6570\u636e\u538b\u7f29\u7684\u8d2a\u5fc3\u7b97\u6cd5\u3002\u901a\u8fc7\u6784\u5efa\u970d\u592b\u66fc\u6811\uff0c\u6bcf\u6b21\u9009\u62e9\u51fa\u73b0\u9891\u7387\u6700\u4f4e\u7684\u4e24\u4e2a\u8282\u70b9\u5408\u5e76\uff0c\u6700\u540e\u5f97\u5230\u7684\u970d\u592b\u66fc\u6811\u7684\u5e26\u6743\u8def\u5f84\u957f\u5ea6\uff08\u7f16\u7801\u957f\u5ea6\uff09\u6700\u5c0f\u3002
        • Dijkstra \u7b97\u6cd5\uff1a\u5b83\u662f\u4e00\u79cd\u89e3\u51b3\u7ed9\u5b9a\u6e90\u9876\u70b9\u5230\u5176\u4f59\u5404\u9876\u70b9\u7684\u6700\u77ed\u8def\u5f84\u95ee\u9898\u7684\u8d2a\u5fc3\u7b97\u6cd5\u3002
        "},{"location":"chapter_greedy/max_capacity_problem/","title":"15.3 \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898","text":"

        Question

        \u8f93\u5165\u4e00\u4e2a\u6570\u7ec4 \\(ht\\) \uff0c\u5176\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u4ee3\u8868\u4e00\u4e2a\u5782\u76f4\u9694\u677f\u7684\u9ad8\u5ea6\u3002\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e24\u4e2a\u9694\u677f\uff0c\u4ee5\u53ca\u5b83\u4eec\u4e4b\u95f4\u7684\u7a7a\u95f4\u53ef\u4ee5\u7ec4\u6210\u4e00\u4e2a\u5bb9\u5668\u3002

        \u5bb9\u5668\u7684\u5bb9\u91cf\u7b49\u4e8e\u9ad8\u5ea6\u548c\u5bbd\u5ea6\u7684\u4e58\u79ef\uff08\u9762\u79ef\uff09\uff0c\u5176\u4e2d\u9ad8\u5ea6\u7531\u8f83\u77ed\u7684\u9694\u677f\u51b3\u5b9a\uff0c\u5bbd\u5ea6\u662f\u4e24\u4e2a\u9694\u677f\u7684\u6570\u7ec4\u7d22\u5f15\u4e4b\u5dee\u3002

        \u8bf7\u5728\u6570\u7ec4\u4e2d\u9009\u62e9\u4e24\u4e2a\u9694\u677f\uff0c\u4f7f\u5f97\u7ec4\u6210\u7684\u5bb9\u5668\u7684\u5bb9\u91cf\u6700\u5927\uff0c\u8fd4\u56de\u6700\u5927\u5bb9\u91cf\u3002\u793a\u4f8b\u5982\u56fe 15-7 \u6240\u793a\u3002

        \u56fe 15-7 \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e

        \u5bb9\u5668\u7531\u4efb\u610f\u4e24\u4e2a\u9694\u677f\u56f4\u6210\uff0c\u56e0\u6b64\u672c\u9898\u7684\u72b6\u6001\u4e3a\u4e24\u4e2a\u9694\u677f\u7684\u7d22\u5f15\uff0c\u8bb0\u4e3a \\([i, j]\\) \u3002

        \u6839\u636e\u9898\u610f\uff0c\u5bb9\u91cf\u7b49\u4e8e\u9ad8\u5ea6\u4e58\u4ee5\u5bbd\u5ea6\uff0c\u5176\u4e2d\u9ad8\u5ea6\u7531\u77ed\u677f\u51b3\u5b9a\uff0c\u5bbd\u5ea6\u662f\u4e24\u9694\u677f\u7684\u6570\u7ec4\u7d22\u5f15\u4e4b\u5dee\u3002\u8bbe\u5bb9\u91cf\u4e3a \\(cap[i, j]\\) \uff0c\u5219\u53ef\u5f97\u8ba1\u7b97\u516c\u5f0f\uff1a

        \\[ cap[i, j] = \\min(ht[i], ht[j]) \\times (j - i) \\]

        \u8bbe\u6570\u7ec4\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u4e24\u4e2a\u9694\u677f\u7684\u7ec4\u5408\u6570\u91cf\uff08\u72b6\u6001\u603b\u6570\uff09\u4e3a \\(C_n^2 = \\frac{n(n - 1)}{2}\\) \u4e2a\u3002\u6700\u76f4\u63a5\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u7a77\u4e3e\u6240\u6709\u72b6\u6001\uff0c\u4ece\u800c\u6c42\u5f97\u6700\u5927\u5bb9\u91cf\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002

        "},{"location":"chapter_greedy/max_capacity_problem/#1","title":"1. \u00a0 \u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a","text":"

        \u8fd9\u9053\u9898\u8fd8\u6709\u66f4\u9ad8\u6548\u7387\u7684\u89e3\u6cd5\u3002\u5982\u56fe 15-8 \u6240\u793a\uff0c\u73b0\u9009\u53d6\u4e00\u4e2a\u72b6\u6001 \\([i, j]\\) \uff0c\u5176\u6ee1\u8db3\u7d22\u5f15 \\(i < j\\) \u4e14\u9ad8\u5ea6 \\(ht[i] < ht[j]\\) \uff0c\u5373 \\(i\\) \u4e3a\u77ed\u677f\u3001\\(j\\) \u4e3a\u957f\u677f\u3002

        \u56fe 15-8 \u00a0 \u521d\u59cb\u72b6\u6001

        \u5982\u56fe 15-9 \u6240\u793a\uff0c\u82e5\u6b64\u65f6\u5c06\u957f\u677f \\(j\\) \u5411\u77ed\u677f \\(i\\) \u9760\u8fd1\uff0c\u5219\u5bb9\u91cf\u4e00\u5b9a\u53d8\u5c0f\u3002

        \u8fd9\u662f\u56e0\u4e3a\u5728\u79fb\u52a8\u957f\u677f \\(j\\) \u540e\uff0c\u5bbd\u5ea6 \\(j-i\\) \u80af\u5b9a\u53d8\u5c0f\uff1b\u800c\u9ad8\u5ea6\u7531\u77ed\u677f\u51b3\u5b9a\uff0c\u56e0\u6b64\u9ad8\u5ea6\u53ea\u53ef\u80fd\u4e0d\u53d8\uff08 \\(i\\) \u4ecd\u4e3a\u77ed\u677f\uff09\u6216\u53d8\u5c0f\uff08\u79fb\u52a8\u540e\u7684 \\(j\\) \u6210\u4e3a\u77ed\u677f\uff09\u3002

        \u56fe 15-9 \u00a0 \u5411\u5185\u79fb\u52a8\u957f\u677f\u540e\u7684\u72b6\u6001

        \u53cd\u5411\u601d\u8003\uff0c\u6211\u4eec\u53ea\u6709\u5411\u5185\u6536\u7f29\u77ed\u677f \\(i\\) \uff0c\u624d\u6709\u53ef\u80fd\u4f7f\u5bb9\u91cf\u53d8\u5927\u3002\u56e0\u4e3a\u867d\u7136\u5bbd\u5ea6\u4e00\u5b9a\u53d8\u5c0f\uff0c\u4f46\u9ad8\u5ea6\u53ef\u80fd\u4f1a\u53d8\u5927\uff08\u79fb\u52a8\u540e\u7684\u77ed\u677f \\(i\\) \u53ef\u80fd\u4f1a\u53d8\u957f\uff09\u3002\u4f8b\u5982\u5728\u56fe 15-10 \u4e2d\uff0c\u79fb\u52a8\u77ed\u677f\u540e\u9762\u79ef\u53d8\u5927\u3002

        \u56fe 15-10 \u00a0 \u5411\u5185\u79fb\u52a8\u77ed\u677f\u540e\u7684\u72b6\u6001

        \u7531\u6b64\u4fbf\u53ef\u63a8\u51fa\u672c\u9898\u7684\u8d2a\u5fc3\u7b56\u7565\uff1a\u521d\u59cb\u5316\u4e24\u6307\u9488\uff0c\u4f7f\u5176\u5206\u5217\u5bb9\u5668\u4e24\u7aef\uff0c\u6bcf\u8f6e\u5411\u5185\u6536\u7f29\u77ed\u677f\u5bf9\u5e94\u7684\u6307\u9488\uff0c\u76f4\u81f3\u4e24\u6307\u9488\u76f8\u9047\u3002

        \u56fe 15-11 \u5c55\u793a\u4e86\u8d2a\u5fc3\u7b56\u7565\u7684\u6267\u884c\u8fc7\u7a0b\u3002

        1. \u521d\u59cb\u72b6\u6001\u4e0b\uff0c\u6307\u9488 \\(i\\) \u548c \\(j\\) \u5206\u5217\u6570\u7ec4\u4e24\u7aef\u3002
        2. \u8ba1\u7b97\u5f53\u524d\u72b6\u6001\u7684\u5bb9\u91cf \\(cap[i, j]\\) \uff0c\u5e76\u66f4\u65b0\u6700\u5927\u5bb9\u91cf\u3002
        3. \u6bd4\u8f83\u677f \\(i\\) \u548c\u677f \\(j\\) \u7684\u9ad8\u5ea6\uff0c\u5e76\u5c06\u77ed\u677f\u5411\u5185\u79fb\u52a8\u4e00\u683c\u3002
        4. \u5faa\u73af\u6267\u884c\u7b2c 2. \u6b65\u548c\u7b2c 3. \u6b65\uff0c\u76f4\u81f3 \\(i\\) \u548c \\(j\\) \u76f8\u9047\u65f6\u7ed3\u675f\u3002
        <1><2><3><4><5><6><7><8><9>

        \u56fe 15-11 \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898\u7684\u8d2a\u5fc3\u8fc7\u7a0b

        "},{"location":"chapter_greedy/max_capacity_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u4ee3\u7801\u5faa\u73af\u6700\u591a \\(n\\) \u8f6e\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

        \u53d8\u91cf \\(i\\)\u3001\\(j\\)\u3001\\(res\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby max_capacity.py
        def max_capacity(ht: list[int]) -> int:\n    \"\"\"\u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3\"\"\"\n    # \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n    i, j = 0, len(ht) - 1\n    # \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n    res = 0\n    # \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n    while i < j:\n        # \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n        cap = min(ht[i], ht[j]) * (j - i)\n        res = max(res, cap)\n        # \u5411\u5185\u79fb\u52a8\u77ed\u677f\n        if ht[i] < ht[j]:\n            i += 1\n        else:\n            j -= 1\n    return res\n
        max_capacity.cpp
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(vector<int> &ht) {\n    // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n    int i = 0, j = ht.size() - 1;\n    // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n    int res = 0;\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n    while (i < j) {\n        // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n        int cap = min(ht[i], ht[j]) * (j - i);\n        res = max(res, cap);\n        // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
        max_capacity.java
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(int[] ht) {\n    // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n    int i = 0, j = ht.length - 1;\n    // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n    int res = 0;\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n    while (i < j) {\n        // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n        int cap = Math.min(ht[i], ht[j]) * (j - i);\n        res = Math.max(res, cap);\n        // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
        max_capacity.cs
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint MaxCapacity(int[] ht) {\n    // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n    int i = 0, j = ht.Length - 1;\n    // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n    int res = 0;\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n    while (i < j) {\n        // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n        int cap = Math.Min(ht[i], ht[j]) * (j - i);\n        res = Math.Max(res, cap);\n        // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
        max_capacity.go
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunc maxCapacity(ht []int) int {\n    // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n    i, j := 0, len(ht)-1\n    // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n    res := 0\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n    for i < j {\n        // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n        capacity := int(math.Min(float64(ht[i]), float64(ht[j]))) * (j - i)\n        res = int(math.Max(float64(res), float64(capacity)))\n        // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n        if ht[i] < ht[j] {\n            i++\n        } else {\n            j--\n        }\n    }\n    return res\n}\n
        max_capacity.swift
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunc maxCapacity(ht: [Int]) -> Int {\n    // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n    var i = ht.startIndex, j = ht.endIndex - 1\n    // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n    var res = 0\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n    while i < j {\n        // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n        let cap = min(ht[i], ht[j]) * (j - i)\n        res = max(res, cap)\n        // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n        if ht[i] < ht[j] {\n            i += 1\n        } else {\n            j -= 1\n        }\n    }\n    return res\n}\n
        max_capacity.js
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunction maxCapacity(ht) {\n    // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n    let i = 0,\n        j = ht.length - 1;\n    // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n    let res = 0;\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n    while (i < j) {\n        // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n        const cap = Math.min(ht[i], ht[j]) * (j - i);\n        res = Math.max(res, cap);\n        // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n        if (ht[i] < ht[j]) {\n            i += 1;\n        } else {\n            j -= 1;\n        }\n    }\n    return res;\n}\n
        max_capacity.ts
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunction maxCapacity(ht: number[]): number {\n    // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n    let i = 0,\n        j = ht.length - 1;\n    // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n    let res = 0;\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n    while (i < j) {\n        // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n        const cap: number = Math.min(ht[i], ht[j]) * (j - i);\n        res = Math.max(res, cap);\n        // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n        if (ht[i] < ht[j]) {\n            i += 1;\n        } else {\n            j -= 1;\n        }\n    }\n    return res;\n}\n
        max_capacity.dart
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(List<int> ht) {\n  // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n  int i = 0, j = ht.length - 1;\n  // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n  int res = 0;\n  // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n  while (i < j) {\n    // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n    int cap = min(ht[i], ht[j]) * (j - i);\n    res = max(res, cap);\n    // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n    if (ht[i] < ht[j]) {\n      i++;\n    } else {\n      j--;\n    }\n  }\n  return res;\n}\n
        max_capacity.rs
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfn max_capacity(ht: &[i32]) -> i32 {\n    // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n    let mut i = 0;\n    let mut j = ht.len() - 1;\n    // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n    let mut res = 0;\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n    while i < j {\n        // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n        let cap = std::cmp::min(ht[i], ht[j]) * (j - i) as i32;\n        res = std::cmp::max(res, cap);\n        // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n        if ht[i] < ht[j] {\n            i += 1;\n        } else {\n            j -= 1;\n        }\n    }\n    res\n}\n
        max_capacity.c
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(int ht[], int htLength) {\n    // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n    int i = 0;\n    int j = htLength - 1;\n    // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n    int res = 0;\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n    while (i < j) {\n        // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n        int capacity = myMin(ht[i], ht[j]) * (j - i);\n        res = myMax(res, capacity);\n        // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
        max_capacity.kt
        /* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfun maxCapacity(ht: IntArray): Int {\n    // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n    var i = 0\n    var j = ht.size - 1\n    // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n    var res = 0\n    // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n    while (i < j) {\n        // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n        val cap = min(ht[i], ht[j]) * (j - i)\n        res = max(res, cap)\n        // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n        if (ht[i] < ht[j]) {\n            i++\n        } else {\n            j--\n        }\n    }\n    return res\n}\n
        max_capacity.rb
        ### \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 ###\ndef max_capacity(ht)\n  # \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u6570\u7ec4\u4e24\u7aef\n  i, j = 0, ht.length - 1\n  # \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n  res = 0\n\n  # \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n  while i < j\n    # \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n    cap = [ht[i], ht[j]].min * (j - i)\n    res = [res, cap].max\n    # \u5411\u5185\u79fb\u52a8\u77ed\u677f\n    if ht[i] < ht[j]\n      i += 1\n    else\n      j -= 1\n    end\n  end\n\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_greedy/max_capacity_problem/#3","title":"3. \u00a0 \u6b63\u786e\u6027\u8bc1\u660e","text":"

        \u4e4b\u6240\u4ee5\u8d2a\u5fc3\u6bd4\u7a77\u4e3e\u66f4\u5feb\uff0c\u662f\u56e0\u4e3a\u6bcf\u8f6e\u7684\u8d2a\u5fc3\u9009\u62e9\u90fd\u4f1a\u201c\u8df3\u8fc7\u201d\u4e00\u4e9b\u72b6\u6001\u3002

        \u6bd4\u5982\u5728\u72b6\u6001 \\(cap[i, j]\\) \u4e0b\uff0c\\(i\\) \u4e3a\u77ed\u677f\u3001\\(j\\) \u4e3a\u957f\u677f\u3002\u82e5\u8d2a\u5fc3\u5730\u5c06\u77ed\u677f \\(i\\) \u5411\u5185\u79fb\u52a8\u4e00\u683c\uff0c\u4f1a\u5bfc\u81f4\u56fe 15-12 \u6240\u793a\u7684\u72b6\u6001\u88ab\u201c\u8df3\u8fc7\u201d\u3002\u8fd9\u610f\u5473\u7740\u4e4b\u540e\u65e0\u6cd5\u9a8c\u8bc1\u8fd9\u4e9b\u72b6\u6001\u7684\u5bb9\u91cf\u5927\u5c0f\u3002

        \\[ cap[i, i+1], cap[i, i+2], \\dots, cap[i, j-2], cap[i, j-1] \\]

        \u56fe 15-12 \u00a0 \u79fb\u52a8\u77ed\u677f\u5bfc\u81f4\u88ab\u8df3\u8fc7\u7684\u72b6\u6001

        \u89c2\u5bdf\u53d1\u73b0\uff0c\u8fd9\u4e9b\u88ab\u8df3\u8fc7\u7684\u72b6\u6001\u5b9e\u9645\u4e0a\u5c31\u662f\u5c06\u957f\u677f \\(j\\) \u5411\u5185\u79fb\u52a8\u7684\u6240\u6709\u72b6\u6001\u3002\u524d\u9762\u6211\u4eec\u5df2\u7ecf\u8bc1\u660e\u5185\u79fb\u957f\u677f\u4e00\u5b9a\u4f1a\u5bfc\u81f4\u5bb9\u91cf\u53d8\u5c0f\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u88ab\u8df3\u8fc7\u7684\u72b6\u6001\u90fd\u4e0d\u53ef\u80fd\u662f\u6700\u4f18\u89e3\uff0c\u8df3\u8fc7\u5b83\u4eec\u4e0d\u4f1a\u5bfc\u81f4\u9519\u8fc7\u6700\u4f18\u89e3\u3002

        \u4ee5\u4e0a\u5206\u6790\u8bf4\u660e\uff0c\u79fb\u52a8\u77ed\u677f\u7684\u64cd\u4f5c\u662f\u201c\u5b89\u5168\u201d\u7684\uff0c\u8d2a\u5fc3\u7b56\u7565\u662f\u6709\u6548\u7684\u3002

        "},{"location":"chapter_greedy/max_product_cutting_problem/","title":"15.4 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u95ee\u9898","text":"

        Question

        \u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570 \\(n\\) \uff0c\u5c06\u5176\u5207\u5206\u4e3a\u81f3\u5c11\u4e24\u4e2a\u6b63\u6574\u6570\u7684\u548c\uff0c\u6c42\u5207\u5206\u540e\u6240\u6709\u6574\u6570\u7684\u4e58\u79ef\u6700\u5927\u662f\u591a\u5c11\uff0c\u5982\u56fe 15-13 \u6240\u793a\u3002

        \u56fe 15-13 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u7684\u95ee\u9898\u5b9a\u4e49

        \u5047\u8bbe\u6211\u4eec\u5c06 \\(n\\) \u5207\u5206\u4e3a \\(m\\) \u4e2a\u6574\u6570\u56e0\u5b50\uff0c\u5176\u4e2d\u7b2c \\(i\\) \u4e2a\u56e0\u5b50\u8bb0\u4e3a \\(n_i\\) \uff0c\u5373

        \\[ n = \\sum_{i=1}^{m}n_i \\]

        \u672c\u9898\u7684\u76ee\u6807\u662f\u6c42\u5f97\u6240\u6709\u6574\u6570\u56e0\u5b50\u7684\u6700\u5927\u4e58\u79ef\uff0c\u5373

        \\[ \\max(\\prod_{i=1}^{m}n_i) \\]

        \u6211\u4eec\u9700\u8981\u601d\u8003\u7684\u662f\uff1a\u5207\u5206\u6570\u91cf \\(m\\) \u5e94\u8be5\u591a\u5927\uff0c\u6bcf\u4e2a \\(n_i\\) \u5e94\u8be5\u662f\u591a\u5c11\uff1f

        "},{"location":"chapter_greedy/max_product_cutting_problem/#1","title":"1. \u00a0 \u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a","text":"

        \u6839\u636e\u7ecf\u9a8c\uff0c\u4e24\u4e2a\u6574\u6570\u7684\u4e58\u79ef\u5f80\u5f80\u6bd4\u5b83\u4eec\u7684\u52a0\u548c\u66f4\u5927\u3002\u5047\u8bbe\u4ece \\(n\\) \u4e2d\u5206\u51fa\u4e00\u4e2a\u56e0\u5b50 \\(2\\) \uff0c\u5219\u5b83\u4eec\u7684\u4e58\u79ef\u4e3a \\(2(n-2)\\) \u3002\u6211\u4eec\u5c06\u8be5\u4e58\u79ef\u4e0e \\(n\\) \u4f5c\u6bd4\u8f83\uff1a

        \\[ \\begin{aligned} 2(n-2) & \\geq n \\newline 2n - n - 4 & \\geq 0 \\newline n & \\geq 4 \\end{aligned} \\]

        \u5982\u56fe 15-14 \u6240\u793a\uff0c\u5f53 \\(n \\geq 4\\) \u65f6\uff0c\u5207\u5206\u51fa\u4e00\u4e2a \\(2\\) \u540e\u4e58\u79ef\u4f1a\u53d8\u5927\uff0c\u8fd9\u8bf4\u660e\u5927\u4e8e\u7b49\u4e8e \\(4\\) \u7684\u6574\u6570\u90fd\u5e94\u8be5\u88ab\u5207\u5206\u3002

        \u8d2a\u5fc3\u7b56\u7565\u4e00\uff1a\u5982\u679c\u5207\u5206\u65b9\u6848\u4e2d\u5305\u542b \\(\\geq 4\\) \u7684\u56e0\u5b50\uff0c\u90a3\u4e48\u5b83\u5c31\u5e94\u8be5\u88ab\u7ee7\u7eed\u5207\u5206\u3002\u6700\u7ec8\u7684\u5207\u5206\u65b9\u6848\u53ea\u5e94\u51fa\u73b0 \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u8fd9\u4e09\u79cd\u56e0\u5b50\u3002

        \u56fe 15-14 \u00a0 \u5207\u5206\u5bfc\u81f4\u4e58\u79ef\u53d8\u5927

        \u63a5\u4e0b\u6765\u601d\u8003\u54ea\u4e2a\u56e0\u5b50\u662f\u6700\u4f18\u7684\u3002\u5728 \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u8fd9\u4e09\u4e2a\u56e0\u5b50\u4e2d\uff0c\u663e\u7136 \\(1\\) \u662f\u6700\u5dee\u7684\uff0c\u56e0\u4e3a \\(1 \\times (n-1) < n\\) \u6052\u6210\u7acb\uff0c\u5373\u5207\u5206\u51fa \\(1\\) \u53cd\u800c\u4f1a\u5bfc\u81f4\u4e58\u79ef\u51cf\u5c0f\u3002

        \u5982\u56fe 15-15 \u6240\u793a\uff0c\u5f53 \\(n = 6\\) \u65f6\uff0c\u6709 \\(3 \\times 3 > 2 \\times 2 \\times 2\\) \u3002\u8fd9\u610f\u5473\u7740\u5207\u5206\u51fa \\(3\\) \u6bd4\u5207\u5206\u51fa \\(2\\) \u66f4\u4f18\u3002

        \u8d2a\u5fc3\u7b56\u7565\u4e8c\uff1a\u5728\u5207\u5206\u65b9\u6848\u4e2d\uff0c\u6700\u591a\u53ea\u5e94\u5b58\u5728\u4e24\u4e2a \\(2\\) \u3002\u56e0\u4e3a\u4e09\u4e2a \\(2\\) \u603b\u662f\u53ef\u4ee5\u66ff\u6362\u4e3a\u4e24\u4e2a \\(3\\) \uff0c\u4ece\u800c\u83b7\u5f97\u66f4\u5927\u7684\u4e58\u79ef\u3002

        \u56fe 15-15 \u00a0 \u6700\u4f18\u5207\u5206\u56e0\u5b50

        \u7efc\u4e0a\u6240\u8ff0\uff0c\u53ef\u63a8\u7406\u51fa\u4ee5\u4e0b\u8d2a\u5fc3\u7b56\u7565\u3002

        1. \u8f93\u5165\u6574\u6570 \\(n\\) \uff0c\u4ece\u5176\u4e0d\u65ad\u5730\u5207\u5206\u51fa\u56e0\u5b50 \\(3\\) \uff0c\u76f4\u81f3\u4f59\u6570\u4e3a \\(0\\)\u3001\\(1\\)\u3001\\(2\\) \u3002
        2. \u5f53\u4f59\u6570\u4e3a \\(0\\) \u65f6\uff0c\u4ee3\u8868 \\(n\\) \u662f \\(3\\) \u7684\u500d\u6570\uff0c\u56e0\u6b64\u4e0d\u505a\u4efb\u4f55\u5904\u7406\u3002
        3. \u5f53\u4f59\u6570\u4e3a \\(2\\) \u65f6\uff0c\u4e0d\u7ee7\u7eed\u5212\u5206\uff0c\u4fdd\u7559\u3002
        4. \u5f53\u4f59\u6570\u4e3a \\(1\\) \u65f6\uff0c\u7531\u4e8e \\(2 \\times 2 > 1 \\times 3\\) \uff0c\u56e0\u6b64\u5e94\u5c06\u6700\u540e\u4e00\u4e2a \\(3\\) \u66ff\u6362\u4e3a \\(2\\) \u3002
        "},{"location":"chapter_greedy/max_product_cutting_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u5982\u56fe 15-16 \u6240\u793a\uff0c\u6211\u4eec\u65e0\u987b\u901a\u8fc7\u5faa\u73af\u6765\u5207\u5206\u6574\u6570\uff0c\u800c\u53ef\u4ee5\u5229\u7528\u5411\u4e0b\u6574\u9664\u8fd0\u7b97\u5f97\u5230 \\(3\\) \u7684\u4e2a\u6570 \\(a\\) \uff0c\u7528\u53d6\u6a21\u8fd0\u7b97\u5f97\u5230\u4f59\u6570 \\(b\\) \uff0c\u6b64\u65f6\u6709\uff1a

        \\[ n = 3 a + b \\]

        \u8bf7\u6ce8\u610f\uff0c\u5bf9\u4e8e \\(n \\leq 3\\) \u7684\u8fb9\u754c\u60c5\u51b5\uff0c\u5fc5\u987b\u62c6\u5206\u51fa\u4e00\u4e2a \\(1\\) \uff0c\u4e58\u79ef\u4e3a \\(1 \\times (n - 1)\\) \u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby max_product_cutting.py
        def max_product_cutting(n: int) -> int:\n    \"\"\"\u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3\"\"\"\n    # \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n    if n <= 3:\n        return 1 * (n - 1)\n    # \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n    a, b = n // 3, n % 3\n    if b == 1:\n        # \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n        return int(math.pow(3, a - 1)) * 2 * 2\n    if b == 2:\n        # \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        return int(math.pow(3, a)) * 2\n    # \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n    return int(math.pow(3, a))\n
        max_product_cutting.cpp
        /* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n    // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n        return (int)pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        return (int)pow(3, a) * 2;\n    }\n    // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n    return (int)pow(3, a);\n}\n
        max_product_cutting.java
        /* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n    // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n        return (int) Math.pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        return (int) Math.pow(3, a) * 2;\n    }\n    // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n    return (int) Math.pow(3, a);\n}\n
        max_product_cutting.cs
        /* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint MaxProductCutting(int n) {\n    // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n        return (int)Math.Pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        return (int)Math.Pow(3, a) * 2;\n    }\n    // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n    return (int)Math.Pow(3, a);\n}\n
        max_product_cutting.go
        /* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunc maxProductCutting(n int) int {\n    // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n    if n <= 3 {\n        return 1 * (n - 1)\n    }\n    // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n    a := n / 3\n    b := n % 3\n    if b == 1 {\n        // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n        return int(math.Pow(3, float64(a-1))) * 2 * 2\n    }\n    if b == 2 {\n        // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        return int(math.Pow(3, float64(a))) * 2\n    }\n    // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n    return int(math.Pow(3, float64(a)))\n}\n
        max_product_cutting.swift
        /* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunc maxProductCutting(n: Int) -> Int {\n    // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n    if n <= 3 {\n        return 1 * (n - 1)\n    }\n    // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n    let a = n / 3\n    let b = n % 3\n    if b == 1 {\n        // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n        return pow(3, a - 1) * 2 * 2\n    }\n    if b == 2 {\n        // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        return pow(3, a) * 2\n    }\n    // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n    return pow(3, a)\n}\n
        max_product_cutting.js
        /* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunction maxProductCutting(n) {\n    // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n    let a = Math.floor(n / 3);\n    let b = n % 3;\n    if (b === 1) {\n        // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n        return Math.pow(3, a - 1) * 2 * 2;\n    }\n    if (b === 2) {\n        // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        return Math.pow(3, a) * 2;\n    }\n    // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n    return Math.pow(3, a);\n}\n
        max_product_cutting.ts
        /* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunction maxProductCutting(n: number): number {\n    // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n    let a: number = Math.floor(n / 3);\n    let b: number = n % 3;\n    if (b === 1) {\n        // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n        return Math.pow(3, a - 1) * 2 * 2;\n    }\n    if (b === 2) {\n        // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        return Math.pow(3, a) * 2;\n    }\n    // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n    return Math.pow(3, a);\n}\n
        max_product_cutting.dart
        /* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n  // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n  if (n <= 3) {\n    return 1 * (n - 1);\n  }\n  // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n  int a = n ~/ 3;\n  int b = n % 3;\n  if (b == 1) {\n    // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n    return (pow(3, a - 1) * 2 * 2).toInt();\n  }\n  if (b == 2) {\n    // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n    return (pow(3, a) * 2).toInt();\n  }\n  // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n  return pow(3, a).toInt();\n}\n
        max_product_cutting.rs
        /* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfn max_product_cutting(n: i32) -> i32 {\n    // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n    if n <= 3 {\n        return 1 * (n - 1);\n    }\n    // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n    let a = n / 3;\n    let b = n % 3;\n    if b == 1 {\n        // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n        3_i32.pow(a as u32 - 1) * 2 * 2\n    } else if b == 2 {\n        // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        3_i32.pow(a as u32) * 2\n    } else {\n        // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        3_i32.pow(a as u32)\n    }\n}\n
        max_product_cutting.c
        /* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n    // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n        return pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        return pow(3, a) * 2;\n    }\n    // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n    return pow(3, a);\n}\n
        max_product_cutting.kt
        /* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfun maxProductCutting(n: Int): Int {\n    // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n    if (n <= 3) {\n        return 1 * (n - 1)\n    }\n    // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n    val a = n / 3\n    val b = n % 3\n    if (b == 1) {\n        // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n        return 3.0.pow((a - 1)).toInt() * 2 * 2\n    }\n    if (b == 2) {\n        // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n        return 3.0.pow(a).toInt() * 2 * 2\n    }\n    // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n    return 3.0.pow(a).toInt()\n}\n
        max_product_cutting.rb
        ### \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 ###\ndef max_product_cutting(n)\n  # \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n  return 1 * (n - 1) if n <= 3\n  # \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n  a, b = n / 3, n % 3\n  # \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n  return (3.pow(a - 1) * 2 * 2).to_i if b == 1\n  # \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n  return (3.pow(a) * 2).to_i if b == 2\n  # \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n  3.pow(a).to_i\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u56fe 15-16 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u7684\u8ba1\u7b97\u65b9\u6cd5

        \u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u7f16\u7a0b\u8bed\u8a00\u7684\u5e42\u8fd0\u7b97\u7684\u5b9e\u73b0\u65b9\u6cd5\u3002\u4ee5 Python \u4e3a\u4f8b\uff0c\u5e38\u7528\u7684\u5e42\u8ba1\u7b97\u51fd\u6570\u6709\u4e09\u79cd\u3002

        • \u8fd0\u7b97\u7b26 ** \u548c\u51fd\u6570 pow() \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(\\log\u2061 a)\\) \u3002
        • \u51fd\u6570 math.pow() \u5185\u90e8\u8c03\u7528 C \u8bed\u8a00\u5e93\u7684 pow() \u51fd\u6570\uff0c\u5176\u6267\u884c\u6d6e\u70b9\u53d6\u5e42\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002

        \u53d8\u91cf \\(a\\) \u548c \\(b\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002

        "},{"location":"chapter_greedy/max_product_cutting_problem/#3","title":"3. \u00a0 \u6b63\u786e\u6027\u8bc1\u660e","text":"

        \u4f7f\u7528\u53cd\u8bc1\u6cd5\uff0c\u53ea\u5206\u6790 \\(n \\geq 4\\) \u7684\u60c5\u51b5\u3002

        1. \u6240\u6709\u56e0\u5b50 \\(\\leq 3\\) \uff1a\u5047\u8bbe\u6700\u4f18\u5207\u5206\u65b9\u6848\u4e2d\u5b58\u5728 \\(\\geq 4\\) \u7684\u56e0\u5b50 \\(x\\) \uff0c\u90a3\u4e48\u4e00\u5b9a\u53ef\u4ee5\u5c06\u5176\u7ee7\u7eed\u5212\u5206\u4e3a \\(2(x-2)\\) \uff0c\u4ece\u800c\u83b7\u5f97\u66f4\u5927\uff08\u6216\u76f8\u7b49\uff09\u7684\u4e58\u79ef\u3002\u8fd9\u4e0e\u5047\u8bbe\u77db\u76fe\u3002
        2. \u5207\u5206\u65b9\u6848\u4e0d\u5305\u542b \\(1\\) \uff1a\u5047\u8bbe\u6700\u4f18\u5207\u5206\u65b9\u6848\u4e2d\u5b58\u5728\u4e00\u4e2a\u56e0\u5b50 \\(1\\) \uff0c\u90a3\u4e48\u5b83\u4e00\u5b9a\u53ef\u4ee5\u5408\u5e76\u5165\u53e6\u5916\u4e00\u4e2a\u56e0\u5b50\u4e2d\uff0c\u4ee5\u83b7\u5f97\u66f4\u5927\u7684\u4e58\u79ef\u3002\u8fd9\u4e0e\u5047\u8bbe\u77db\u76fe\u3002
        3. \u5207\u5206\u65b9\u6848\u6700\u591a\u5305\u542b\u4e24\u4e2a \\(2\\) \uff1a\u5047\u8bbe\u6700\u4f18\u5207\u5206\u65b9\u6848\u4e2d\u5305\u542b\u4e09\u4e2a \\(2\\) \uff0c\u90a3\u4e48\u4e00\u5b9a\u53ef\u4ee5\u66ff\u6362\u4e3a\u4e24\u4e2a \\(3\\) \uff0c\u4e58\u79ef\u66f4\u5927\u3002\u8fd9\u4e0e\u5047\u8bbe\u77db\u76fe\u3002
        "},{"location":"chapter_greedy/summary/","title":"15.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_greedy/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u8d2a\u5fc3\u7b97\u6cd5\u901a\u5e38\u7528\u4e8e\u89e3\u51b3\u6700\u4f18\u5316\u95ee\u9898\uff0c\u5176\u539f\u7406\u662f\u5728\u6bcf\u4e2a\u51b3\u7b56\u9636\u6bb5\u90fd\u505a\u51fa\u5c40\u90e8\u6700\u4f18\u7684\u51b3\u7b56\uff0c\u4ee5\u671f\u83b7\u5f97\u5168\u5c40\u6700\u4f18\u89e3\u3002
        • \u8d2a\u5fc3\u7b97\u6cd5\u4f1a\u8fed\u4ee3\u5730\u505a\u51fa\u4e00\u4e2a\u53c8\u4e00\u4e2a\u7684\u8d2a\u5fc3\u9009\u62e9\uff0c\u6bcf\u8f6e\u90fd\u5c06\u95ee\u9898\u8f6c\u5316\u6210\u4e00\u4e2a\u89c4\u6a21\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u76f4\u5230\u95ee\u9898\u88ab\u89e3\u51b3\u3002
        • \u8d2a\u5fc3\u7b97\u6cd5\u4e0d\u4ec5\u5b9e\u73b0\u7b80\u5355\uff0c\u8fd8\u5177\u6709\u5f88\u9ad8\u7684\u89e3\u9898\u6548\u7387\u3002\u76f8\u6bd4\u4e8e\u52a8\u6001\u89c4\u5212\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u66f4\u4f4e\u3002
        • \u5728\u96f6\u94b1\u5151\u6362\u95ee\u9898\u4e2d\uff0c\u5bf9\u4e8e\u67d0\u4e9b\u786c\u5e01\u7ec4\u5408\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u53ef\u4ee5\u4fdd\u8bc1\u627e\u5230\u6700\u4f18\u89e3\uff1b\u5bf9\u4e8e\u53e6\u5916\u4e00\u4e9b\u786c\u5e01\u7ec4\u5408\u5219\u4e0d\u7136\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u53ef\u80fd\u627e\u5230\u5f88\u5dee\u7684\u89e3\u3002
        • \u9002\u5408\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\u7684\u95ee\u9898\u5177\u6709\u4e24\u5927\u6027\u8d28\uff1a\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u548c\u6700\u4f18\u5b50\u7ed3\u6784\u3002\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u4ee3\u8868\u8d2a\u5fc3\u7b56\u7565\u7684\u6709\u6548\u6027\u3002
        • \u5bf9\u4e8e\u67d0\u4e9b\u590d\u6742\u95ee\u9898\uff0c\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u7684\u8bc1\u660e\u5e76\u4e0d\u7b80\u5355\u3002\u76f8\u5bf9\u6765\u8bf4\uff0c\u8bc1\u4f2a\u66f4\u52a0\u5bb9\u6613\uff0c\u4f8b\u5982\u96f6\u94b1\u5151\u6362\u95ee\u9898\u3002
        • \u6c42\u89e3\u8d2a\u5fc3\u95ee\u9898\u4e3b\u8981\u5206\u4e3a\u4e09\u6b65\uff1a\u95ee\u9898\u5206\u6790\u3001\u786e\u5b9a\u8d2a\u5fc3\u7b56\u7565\u3001\u6b63\u786e\u6027\u8bc1\u660e\u3002\u5176\u4e2d\uff0c\u786e\u5b9a\u8d2a\u5fc3\u7b56\u7565\u662f\u6838\u5fc3\u6b65\u9aa4\uff0c\u6b63\u786e\u6027\u8bc1\u660e\u5f80\u5f80\u662f\u96be\u70b9\u3002
        • \u5206\u6570\u80cc\u5305\u95ee\u9898\u5728 0-1 \u80cc\u5305\u7684\u57fa\u7840\u4e0a\uff0c\u5141\u8bb8\u9009\u62e9\u7269\u54c1\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u53ef\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\u3002\u8d2a\u5fc3\u7b56\u7565\u7684\u6b63\u786e\u6027\u53ef\u4ee5\u4f7f\u7528\u53cd\u8bc1\u6cd5\u6765\u8bc1\u660e\u3002
        • \u6700\u5927\u5bb9\u91cf\u95ee\u9898\u53ef\u4f7f\u7528\u7a77\u4e3e\u6cd5\u6c42\u89e3\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002\u901a\u8fc7\u8bbe\u8ba1\u8d2a\u5fc3\u7b56\u7565\uff0c\u6bcf\u8f6e\u5411\u5185\u79fb\u52a8\u77ed\u677f\uff0c\u53ef\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(n)\\) \u3002
        • \u5728\u6700\u5927\u5207\u5206\u4e58\u79ef\u95ee\u9898\u4e2d\uff0c\u6211\u4eec\u5148\u540e\u63a8\u7406\u51fa\u4e24\u4e2a\u8d2a\u5fc3\u7b56\u7565\uff1a\\(\\geq 4\\) \u7684\u6574\u6570\u90fd\u5e94\u8be5\u7ee7\u7eed\u5207\u5206\uff0c\u6700\u4f18\u5207\u5206\u56e0\u5b50\u4e3a \\(3\\) \u3002\u4ee3\u7801\u4e2d\u5305\u542b\u5e42\u8fd0\u7b97\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u5e42\u8fd0\u7b97\u5b9e\u73b0\u65b9\u6cd5\uff0c\u901a\u5e38\u4e3a \\(O(1)\\) \u6216 \\(O(\\log n)\\) \u3002
        "},{"location":"chapter_hashing/","title":"\u7b2c 6 \u7ae0 \u00a0 \u54c8\u5e0c\u8868","text":"

        Abstract

        \u5728\u8ba1\u7b97\u673a\u4e16\u754c\u4e2d\uff0c\u54c8\u5e0c\u8868\u5982\u540c\u4e00\u4f4d\u806a\u6167\u7684\u56fe\u4e66\u7ba1\u7406\u5458\u3002

        \u4ed6\u77e5\u9053\u5982\u4f55\u8ba1\u7b97\u7d22\u4e66\u53f7\uff0c\u4ece\u800c\u53ef\u4ee5\u5feb\u901f\u627e\u5230\u76ee\u6807\u56fe\u4e66\u3002

        "},{"location":"chapter_hashing/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 6.1 \u00a0 \u54c8\u5e0c\u8868
        • 6.2 \u00a0 \u54c8\u5e0c\u51b2\u7a81
        • 6.3 \u00a0 \u54c8\u5e0c\u7b97\u6cd5
        • 6.4 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_hashing/hash_algorithm/","title":"6.3 \u00a0 \u54c8\u5e0c\u7b97\u6cd5","text":"

        \u524d\u4e24\u8282\u4ecb\u7ecd\u4e86\u54c8\u5e0c\u8868\u7684\u5de5\u4f5c\u539f\u7406\u548c\u54c8\u5e0c\u51b2\u7a81\u7684\u5904\u7406\u65b9\u6cd5\u3002\u7136\u800c\u65e0\u8bba\u662f\u5f00\u653e\u5bfb\u5740\u8fd8\u662f\u94fe\u5f0f\u5730\u5740\uff0c\u5b83\u4eec\u53ea\u80fd\u4fdd\u8bc1\u54c8\u5e0c\u8868\u53ef\u4ee5\u5728\u53d1\u751f\u51b2\u7a81\u65f6\u6b63\u5e38\u5de5\u4f5c\uff0c\u800c\u65e0\u6cd5\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u7684\u53d1\u751f\u3002

        \u5982\u679c\u54c8\u5e0c\u51b2\u7a81\u8fc7\u4e8e\u9891\u7e41\uff0c\u54c8\u5e0c\u8868\u7684\u6027\u80fd\u5219\u4f1a\u6025\u5267\u52a3\u5316\u3002\u5982\u56fe 6-8 \u6240\u793a\uff0c\u5bf9\u4e8e\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\uff0c\u7406\u60f3\u60c5\u51b5\u4e0b\u952e\u503c\u5bf9\u5747\u5300\u5206\u5e03\u5728\u5404\u4e2a\u6876\u4e2d\uff0c\u8fbe\u5230\u6700\u4f73\u67e5\u8be2\u6548\u7387\uff1b\u6700\u5dee\u60c5\u51b5\u4e0b\u6240\u6709\u952e\u503c\u5bf9\u90fd\u5b58\u50a8\u5230\u540c\u4e00\u4e2a\u6876\u4e2d\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u9000\u5316\u81f3 \\(O(n)\\) \u3002

        \u56fe 6-8 \u00a0 \u54c8\u5e0c\u51b2\u7a81\u7684\u6700\u4f73\u60c5\u51b5\u4e0e\u6700\u5dee\u60c5\u51b5

        \u952e\u503c\u5bf9\u7684\u5206\u5e03\u60c5\u51b5\u7531\u54c8\u5e0c\u51fd\u6570\u51b3\u5b9a\u3002\u56de\u5fc6\u54c8\u5e0c\u51fd\u6570\u7684\u8ba1\u7b97\u6b65\u9aa4\uff0c\u5148\u8ba1\u7b97\u54c8\u5e0c\u503c\uff0c\u518d\u5bf9\u6570\u7ec4\u957f\u5ea6\u53d6\u6a21\uff1a

        index = hash(key) % capacity\n

        \u89c2\u5bdf\u4ee5\u4e0a\u516c\u5f0f\uff0c\u5f53\u54c8\u5e0c\u8868\u5bb9\u91cf capacity \u56fa\u5b9a\u65f6\uff0c\u54c8\u5e0c\u7b97\u6cd5 hash() \u51b3\u5b9a\u4e86\u8f93\u51fa\u503c\uff0c\u8fdb\u800c\u51b3\u5b9a\u4e86\u952e\u503c\u5bf9\u5728\u54c8\u5e0c\u8868\u4e2d\u7684\u5206\u5e03\u60c5\u51b5\u3002

        \u8fd9\u610f\u5473\u7740\uff0c\u4e3a\u4e86\u964d\u4f4e\u54c8\u5e0c\u51b2\u7a81\u7684\u53d1\u751f\u6982\u7387\uff0c\u6211\u4eec\u5e94\u5f53\u5c06\u6ce8\u610f\u529b\u96c6\u4e2d\u5728\u54c8\u5e0c\u7b97\u6cd5 hash() \u7684\u8bbe\u8ba1\u4e0a\u3002

        "},{"location":"chapter_hashing/hash_algorithm/#631","title":"6.3.1 \u00a0 \u54c8\u5e0c\u7b97\u6cd5\u7684\u76ee\u6807","text":"

        \u4e3a\u4e86\u5b9e\u73b0\u201c\u65e2\u5feb\u53c8\u7a33\u201d\u7684\u54c8\u5e0c\u8868\u6570\u636e\u7ed3\u6784\uff0c\u54c8\u5e0c\u7b97\u6cd5\u5e94\u5177\u5907\u4ee5\u4e0b\u7279\u70b9\u3002

        • \u786e\u5b9a\u6027\uff1a\u5bf9\u4e8e\u76f8\u540c\u7684\u8f93\u5165\uff0c\u54c8\u5e0c\u7b97\u6cd5\u5e94\u59cb\u7ec8\u4ea7\u751f\u76f8\u540c\u7684\u8f93\u51fa\u3002\u8fd9\u6837\u624d\u80fd\u786e\u4fdd\u54c8\u5e0c\u8868\u662f\u53ef\u9760\u7684\u3002
        • \u6548\u7387\u9ad8\uff1a\u8ba1\u7b97\u54c8\u5e0c\u503c\u7684\u8fc7\u7a0b\u5e94\u8be5\u8db3\u591f\u5feb\u3002\u8ba1\u7b97\u5f00\u9500\u8d8a\u5c0f\uff0c\u54c8\u5e0c\u8868\u7684\u5b9e\u7528\u6027\u8d8a\u9ad8\u3002
        • \u5747\u5300\u5206\u5e03\uff1a\u54c8\u5e0c\u7b97\u6cd5\u5e94\u4f7f\u5f97\u952e\u503c\u5bf9\u5747\u5300\u5206\u5e03\u5728\u54c8\u5e0c\u8868\u4e2d\u3002\u5206\u5e03\u8d8a\u5747\u5300\uff0c\u54c8\u5e0c\u51b2\u7a81\u7684\u6982\u7387\u5c31\u8d8a\u4f4e\u3002

        \u5b9e\u9645\u4e0a\uff0c\u54c8\u5e0c\u7b97\u6cd5\u9664\u4e86\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u54c8\u5e0c\u8868\uff0c\u8fd8\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5176\u4ed6\u9886\u57df\u4e2d\u3002

        • \u5bc6\u7801\u5b58\u50a8\uff1a\u4e3a\u4e86\u4fdd\u62a4\u7528\u6237\u5bc6\u7801\u7684\u5b89\u5168\uff0c\u7cfb\u7edf\u901a\u5e38\u4e0d\u4f1a\u76f4\u63a5\u5b58\u50a8\u7528\u6237\u7684\u660e\u6587\u5bc6\u7801\uff0c\u800c\u662f\u5b58\u50a8\u5bc6\u7801\u7684\u54c8\u5e0c\u503c\u3002\u5f53\u7528\u6237\u8f93\u5165\u5bc6\u7801\u65f6\uff0c\u7cfb\u7edf\u4f1a\u5bf9\u8f93\u5165\u7684\u5bc6\u7801\u8ba1\u7b97\u54c8\u5e0c\u503c\uff0c\u7136\u540e\u4e0e\u5b58\u50a8\u7684\u54c8\u5e0c\u503c\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u4e24\u8005\u5339\u914d\uff0c\u90a3\u4e48\u5bc6\u7801\u5c31\u88ab\u89c6\u4e3a\u6b63\u786e\u3002
        • \u6570\u636e\u5b8c\u6574\u6027\u68c0\u67e5\uff1a\u6570\u636e\u53d1\u9001\u65b9\u53ef\u4ee5\u8ba1\u7b97\u6570\u636e\u7684\u54c8\u5e0c\u503c\u5e76\u5c06\u5176\u4e00\u540c\u53d1\u9001\uff1b\u63a5\u6536\u65b9\u53ef\u4ee5\u91cd\u65b0\u8ba1\u7b97\u63a5\u6536\u5230\u7684\u6570\u636e\u7684\u54c8\u5e0c\u503c\uff0c\u5e76\u4e0e\u63a5\u6536\u5230\u7684\u54c8\u5e0c\u503c\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u4e24\u8005\u5339\u914d\uff0c\u90a3\u4e48\u6570\u636e\u5c31\u88ab\u89c6\u4e3a\u5b8c\u6574\u3002

        \u5bf9\u4e8e\u5bc6\u7801\u5b66\u7684\u76f8\u5173\u5e94\u7528\uff0c\u4e3a\u4e86\u9632\u6b62\u4ece\u54c8\u5e0c\u503c\u63a8\u5bfc\u51fa\u539f\u59cb\u5bc6\u7801\u7b49\u9006\u5411\u5de5\u7a0b\uff0c\u54c8\u5e0c\u7b97\u6cd5\u9700\u8981\u5177\u5907\u66f4\u9ad8\u7b49\u7ea7\u7684\u5b89\u5168\u7279\u6027\u3002

        • \u5355\u5411\u6027\uff1a\u65e0\u6cd5\u901a\u8fc7\u54c8\u5e0c\u503c\u53cd\u63a8\u51fa\u5173\u4e8e\u8f93\u5165\u6570\u636e\u7684\u4efb\u4f55\u4fe1\u606f\u3002
        • \u6297\u78b0\u649e\u6027\uff1a\u5e94\u5f53\u6781\u96be\u627e\u5230\u4e24\u4e2a\u4e0d\u540c\u7684\u8f93\u5165\uff0c\u4f7f\u5f97\u5b83\u4eec\u7684\u54c8\u5e0c\u503c\u76f8\u540c\u3002
        • \u96ea\u5d29\u6548\u5e94\uff1a\u8f93\u5165\u7684\u5fae\u5c0f\u53d8\u5316\u5e94\u5f53\u5bfc\u81f4\u8f93\u51fa\u7684\u663e\u8457\u4e14\u4e0d\u53ef\u9884\u6d4b\u7684\u53d8\u5316\u3002

        \u8bf7\u6ce8\u610f\uff0c\u201c\u5747\u5300\u5206\u5e03\u201d\u4e0e\u201c\u6297\u78b0\u649e\u6027\u201d\u662f\u4e24\u4e2a\u72ec\u7acb\u7684\u6982\u5ff5\uff0c\u6ee1\u8db3\u5747\u5300\u5206\u5e03\u4e0d\u4e00\u5b9a\u6ee1\u8db3\u6297\u78b0\u649e\u6027\u3002\u4f8b\u5982\uff0c\u5728\u968f\u673a\u8f93\u5165 key \u4e0b\uff0c\u54c8\u5e0c\u51fd\u6570 key % 100 \u53ef\u4ee5\u4ea7\u751f\u5747\u5300\u5206\u5e03\u7684\u8f93\u51fa\u3002\u7136\u800c\u8be5\u54c8\u5e0c\u7b97\u6cd5\u8fc7\u4e8e\u7b80\u5355\uff0c\u6240\u6709\u540e\u4e24\u4f4d\u76f8\u7b49\u7684 key \u7684\u8f93\u51fa\u90fd\u76f8\u540c\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u4ece\u54c8\u5e0c\u503c\u53cd\u63a8\u51fa\u53ef\u7528\u7684 key \uff0c\u4ece\u800c\u7834\u89e3\u5bc6\u7801\u3002

        "},{"location":"chapter_hashing/hash_algorithm/#632","title":"6.3.2 \u00a0 \u54c8\u5e0c\u7b97\u6cd5\u7684\u8bbe\u8ba1","text":"

        \u54c8\u5e0c\u7b97\u6cd5\u7684\u8bbe\u8ba1\u662f\u4e00\u4e2a\u9700\u8981\u8003\u8651\u8bb8\u591a\u56e0\u7d20\u7684\u590d\u6742\u95ee\u9898\u3002\u7136\u800c\u5bf9\u4e8e\u67d0\u4e9b\u8981\u6c42\u4e0d\u9ad8\u7684\u573a\u666f\uff0c\u6211\u4eec\u4e5f\u80fd\u8bbe\u8ba1\u4e00\u4e9b\u7b80\u5355\u7684\u54c8\u5e0c\u7b97\u6cd5\u3002

        • \u52a0\u6cd5\u54c8\u5e0c\uff1a\u5bf9\u8f93\u5165\u7684\u6bcf\u4e2a\u5b57\u7b26\u7684 ASCII \u7801\u8fdb\u884c\u76f8\u52a0\uff0c\u5c06\u5f97\u5230\u7684\u603b\u548c\u4f5c\u4e3a\u54c8\u5e0c\u503c\u3002
        • \u4e58\u6cd5\u54c8\u5e0c\uff1a\u5229\u7528\u4e58\u6cd5\u7684\u4e0d\u76f8\u5173\u6027\uff0c\u6bcf\u8f6e\u4e58\u4ee5\u4e00\u4e2a\u5e38\u6570\uff0c\u5c06\u5404\u4e2a\u5b57\u7b26\u7684 ASCII \u7801\u7d2f\u79ef\u5230\u54c8\u5e0c\u503c\u4e2d\u3002
        • \u5f02\u6216\u54c8\u5e0c\uff1a\u5c06\u8f93\u5165\u6570\u636e\u7684\u6bcf\u4e2a\u5143\u7d20\u901a\u8fc7\u5f02\u6216\u64cd\u4f5c\u7d2f\u79ef\u5230\u4e00\u4e2a\u54c8\u5e0c\u503c\u4e2d\u3002
        • \u65cb\u8f6c\u54c8\u5e0c\uff1a\u5c06\u6bcf\u4e2a\u5b57\u7b26\u7684 ASCII \u7801\u7d2f\u79ef\u5230\u4e00\u4e2a\u54c8\u5e0c\u503c\u4e2d\uff0c\u6bcf\u6b21\u7d2f\u79ef\u4e4b\u524d\u90fd\u4f1a\u5bf9\u54c8\u5e0c\u503c\u8fdb\u884c\u65cb\u8f6c\u64cd\u4f5c\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby simple_hash.py
        def add_hash(key: str) -> int:\n    \"\"\"\u52a0\u6cd5\u54c8\u5e0c\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash += ord(c)\n    return hash % modulus\n\ndef mul_hash(key: str) -> int:\n    \"\"\"\u4e58\u6cd5\u54c8\u5e0c\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash = 31 * hash + ord(c)\n    return hash % modulus\n\ndef xor_hash(key: str) -> int:\n    \"\"\"\u5f02\u6216\u54c8\u5e0c\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash ^= ord(c)\n    return hash % modulus\n\ndef rot_hash(key: str) -> int:\n    \"\"\"\u65cb\u8f6c\u54c8\u5e0c\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash = (hash << 4) ^ (hash >> 28) ^ ord(c)\n    return hash % modulus\n
        simple_hash.cpp
        /* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(string key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash = (hash + (int)c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(string key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash = (31 * hash + (int)c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(string key) {\n    int hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash ^= (int)c;\n    }\n    return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(string key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ (int)c) % MODULUS;\n    }\n    return (int)hash;\n}\n
        simple_hash.java
        /* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(String key) {\n    long hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash = (hash + (int) c) % MODULUS;\n    }\n    return (int) hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(String key) {\n    long hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash = (31 * hash + (int) c) % MODULUS;\n    }\n    return (int) hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(String key) {\n    int hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash ^= (int) c;\n    }\n    return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(String key) {\n    long hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ (int) c) % MODULUS;\n    }\n    return (int) hash;\n}\n
        simple_hash.cs
        /* \u52a0\u6cd5\u54c8\u5e0c */\nint AddHash(string key) {\n    long hash = 0;\n    const int MODULUS = 1000000007;\n    foreach (char c in key) {\n        hash = (hash + c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint MulHash(string key) {\n    long hash = 0;\n    const int MODULUS = 1000000007;\n    foreach (char c in key) {\n        hash = (31 * hash + c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint XorHash(string key) {\n    int hash = 0;\n    const int MODULUS = 1000000007;\n    foreach (char c in key) {\n        hash ^= c;\n    }\n    return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint RotHash(string key) {\n    long hash = 0;\n    const int MODULUS = 1000000007;\n    foreach (char c in key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ c) % MODULUS;\n    }\n    return (int)hash;\n}\n
        simple_hash.go
        /* \u52a0\u6cd5\u54c8\u5e0c */\nfunc addHash(key string) int {\n    var hash int64\n    var modulus int64\n\n    modulus = 1000000007\n    for _, b := range []byte(key) {\n        hash = (hash + int64(b)) % modulus\n    }\n    return int(hash)\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunc mulHash(key string) int {\n    var hash int64\n    var modulus int64\n\n    modulus = 1000000007\n    for _, b := range []byte(key) {\n        hash = (31*hash + int64(b)) % modulus\n    }\n    return int(hash)\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunc xorHash(key string) int {\n    hash := 0\n    modulus := 1000000007\n    for _, b := range []byte(key) {\n        fmt.Println(int(b))\n        hash ^= int(b)\n        hash = (31*hash + int(b)) % modulus\n    }\n    return hash & modulus\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunc rotHash(key string) int {\n    var hash int64\n    var modulus int64\n\n    modulus = 1000000007\n    for _, b := range []byte(key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ int64(b)) % modulus\n    }\n    return int(hash)\n}\n
        simple_hash.swift
        /* \u52a0\u6cd5\u54c8\u5e0c */\nfunc addHash(key: String) -> Int {\n    var hash = 0\n    let MODULUS = 1_000_000_007\n    for c in key {\n        for scalar in c.unicodeScalars {\n            hash = (hash + Int(scalar.value)) % MODULUS\n        }\n    }\n    return hash\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunc mulHash(key: String) -> Int {\n    var hash = 0\n    let MODULUS = 1_000_000_007\n    for c in key {\n        for scalar in c.unicodeScalars {\n            hash = (31 * hash + Int(scalar.value)) % MODULUS\n        }\n    }\n    return hash\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunc xorHash(key: String) -> Int {\n    var hash = 0\n    let MODULUS = 1_000_000_007\n    for c in key {\n        for scalar in c.unicodeScalars {\n            hash ^= Int(scalar.value)\n        }\n    }\n    return hash & MODULUS\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunc rotHash(key: String) -> Int {\n    var hash = 0\n    let MODULUS = 1_000_000_007\n    for c in key {\n        for scalar in c.unicodeScalars {\n            hash = ((hash << 4) ^ (hash >> 28) ^ Int(scalar.value)) % MODULUS\n        }\n    }\n    return hash\n}\n
        simple_hash.js
        /* \u52a0\u6cd5\u54c8\u5e0c */\nfunction addHash(key) {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = (hash + c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunction mulHash(key) {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunction xorHash(key) {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash ^= c.charCodeAt(0);\n    }\n    return hash % MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunction rotHash(key) {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n
        simple_hash.ts
        /* \u52a0\u6cd5\u54c8\u5e0c */\nfunction addHash(key: string): number {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = (hash + c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunction mulHash(key: string): number {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunction xorHash(key: string): number {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash ^= c.charCodeAt(0);\n    }\n    return hash % MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunction rotHash(key: string): number {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n
        simple_hash.dart
        /* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(String key) {\n  int hash = 0;\n  final int MODULUS = 1000000007;\n  for (int i = 0; i < key.length; i++) {\n    hash = (hash + key.codeUnitAt(i)) % MODULUS;\n  }\n  return hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(String key) {\n  int hash = 0;\n  final int MODULUS = 1000000007;\n  for (int i = 0; i < key.length; i++) {\n    hash = (31 * hash + key.codeUnitAt(i)) % MODULUS;\n  }\n  return hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(String key) {\n  int hash = 0;\n  final int MODULUS = 1000000007;\n  for (int i = 0; i < key.length; i++) {\n    hash ^= key.codeUnitAt(i);\n  }\n  return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(String key) {\n  int hash = 0;\n  final int MODULUS = 1000000007;\n  for (int i = 0; i < key.length; i++) {\n    hash = ((hash << 4) ^ (hash >> 28) ^ key.codeUnitAt(i)) % MODULUS;\n  }\n  return hash;\n}\n
        simple_hash.rs
        /* \u52a0\u6cd5\u54c8\u5e0c */\nfn add_hash(key: &str) -> i32 {\n    let mut hash = 0_i64;\n    const MODULUS: i64 = 1000000007;\n\n    for c in key.chars() {\n        hash = (hash + c as i64) % MODULUS;\n    }\n\n    hash as i32\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfn mul_hash(key: &str) -> i32 {\n    let mut hash = 0_i64;\n    const MODULUS: i64 = 1000000007;\n\n    for c in key.chars() {\n        hash = (31 * hash + c as i64) % MODULUS;\n    }\n\n    hash as i32\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfn xor_hash(key: &str) -> i32 {\n    let mut hash = 0_i64;\n    const MODULUS: i64 = 1000000007;\n\n    for c in key.chars() {\n        hash ^= c as i64;\n    }\n\n    (hash & MODULUS) as i32\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfn rot_hash(key: &str) -> i32 {\n    let mut hash = 0_i64;\n    const MODULUS: i64 = 1000000007;\n\n    for c in key.chars() {\n        hash = ((hash << 4) ^ (hash >> 28) ^ c as i64) % MODULUS;\n    }\n\n    hash as i32\n}\n
        simple_hash.c
        /* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(char *key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (int i = 0; i < strlen(key); i++) {\n        hash = (hash + (unsigned char)key[i]) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(char *key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (int i = 0; i < strlen(key); i++) {\n        hash = (31 * hash + (unsigned char)key[i]) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(char *key) {\n    int hash = 0;\n    const int MODULUS = 1000000007;\n\n    for (int i = 0; i < strlen(key); i++) {\n        hash ^= (unsigned char)key[i];\n    }\n    return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(char *key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (int i = 0; i < strlen(key); i++) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ (unsigned char)key[i]) % MODULUS;\n    }\n\n    return (int)hash;\n}\n
        simple_hash.kt
        /* \u52a0\u6cd5\u54c8\u5e0c */\nfun addHash(key: String): Int {\n    var hash = 0L\n    val MODULUS = 1000000007\n    for (c in key.toCharArray()) {\n        hash = (hash + c.code) % MODULUS\n    }\n    return hash.toInt()\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfun mulHash(key: String): Int {\n    var hash = 0L\n    val MODULUS = 1000000007\n    for (c in key.toCharArray()) {\n        hash = (31 * hash + c.code) % MODULUS\n    }\n    return hash.toInt()\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfun xorHash(key: String): Int {\n    var hash = 0\n    val MODULUS = 1000000007\n    for (c in key.toCharArray()) {\n        hash = hash xor c.code\n    }\n    return hash and MODULUS\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfun rotHash(key: String): Int {\n    var hash = 0L\n    val MODULUS = 1000000007\n    for (c in key.toCharArray()) {\n        hash = ((hash shl 4) xor (hash shr 28) xor c.code.toLong()) % MODULUS\n    }\n    return hash.toInt()\n}\n
        simple_hash.rb
        ### \u52a0\u6cd5\u54c8\u5e0c ###\ndef add_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash += c.ord }\n\n  hash % modulus\nend\n\n### \u4e58\u6cd5\u54c8\u5e0c ###\ndef mul_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash = 31 * hash + c.ord }\n\n  hash % modulus\nend\n\n### \u5f02\u6216\u54c8\u5e0c ###\ndef xor_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash ^= c.ord }\n\n  hash % modulus\nend\n\n### \u65cb\u8f6c\u54c8\u5e0c ###\ndef rot_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash = (hash << 4) ^ (hash >> 28) ^ c.ord }\n\n  hash % modulus\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u89c2\u5bdf\u53d1\u73b0\uff0c\u6bcf\u79cd\u54c8\u5e0c\u7b97\u6cd5\u7684\u6700\u540e\u4e00\u6b65\u90fd\u662f\u5bf9\u5927\u8d28\u6570 \\(1000000007\\) \u53d6\u6a21\uff0c\u4ee5\u786e\u4fdd\u54c8\u5e0c\u503c\u5728\u5408\u9002\u7684\u8303\u56f4\u5185\u3002\u503c\u5f97\u601d\u8003\u7684\u662f\uff0c\u4e3a\u4ec0\u4e48\u8981\u5f3a\u8c03\u5bf9\u8d28\u6570\u53d6\u6a21\uff0c\u6216\u8005\u8bf4\u5bf9\u5408\u6570\u53d6\u6a21\u7684\u5f0a\u7aef\u662f\u4ec0\u4e48\uff1f\u8fd9\u662f\u4e00\u4e2a\u6709\u8da3\u7684\u95ee\u9898\u3002

        \u5148\u7ed9\u51fa\u7ed3\u8bba\uff1a\u4f7f\u7528\u5927\u8d28\u6570\u4f5c\u4e3a\u6a21\u6570\uff0c\u53ef\u4ee5\u6700\u5927\u5316\u5730\u4fdd\u8bc1\u54c8\u5e0c\u503c\u7684\u5747\u5300\u5206\u5e03\u3002\u56e0\u4e3a\u8d28\u6570\u4e0d\u4e0e\u5176\u4ed6\u6570\u5b57\u5b58\u5728\u516c\u7ea6\u6570\uff0c\u53ef\u4ee5\u51cf\u5c11\u56e0\u53d6\u6a21\u64cd\u4f5c\u800c\u4ea7\u751f\u7684\u5468\u671f\u6027\u6a21\u5f0f\uff0c\u4ece\u800c\u907f\u514d\u54c8\u5e0c\u51b2\u7a81\u3002

        \u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u6211\u4eec\u9009\u62e9\u5408\u6570 \\(9\\) \u4f5c\u4e3a\u6a21\u6570\uff0c\u5b83\u53ef\u4ee5\u88ab \\(3\\) \u6574\u9664\uff0c\u90a3\u4e48\u6240\u6709\u53ef\u4ee5\u88ab \\(3\\) \u6574\u9664\u7684 key \u90fd\u4f1a\u88ab\u6620\u5c04\u5230 \\(0\\)\u3001\\(3\\)\u3001\\(6\\) \u8fd9\u4e09\u4e2a\u54c8\u5e0c\u503c\u3002

        \\[ \\begin{aligned} \\text{modulus} & = 9 \\newline \\text{key} & = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\dots \\} \\newline \\text{hash} & = \\{ 0, 3, 6, 0, 3, 6, 0, 3, 6, 0, 3, 6,\\dots \\} \\end{aligned} \\]

        \u5982\u679c\u8f93\u5165 key \u6070\u597d\u6ee1\u8db3\u8fd9\u79cd\u7b49\u5dee\u6570\u5217\u7684\u6570\u636e\u5206\u5e03\uff0c\u90a3\u4e48\u54c8\u5e0c\u503c\u5c31\u4f1a\u51fa\u73b0\u805a\u5806\uff0c\u4ece\u800c\u52a0\u91cd\u54c8\u5e0c\u51b2\u7a81\u3002\u73b0\u5728\uff0c\u5047\u8bbe\u5c06 modulus \u66ff\u6362\u4e3a\u8d28\u6570 \\(13\\) \uff0c\u7531\u4e8e key \u548c modulus \u4e4b\u95f4\u4e0d\u5b58\u5728\u516c\u7ea6\u6570\uff0c\u56e0\u6b64\u8f93\u51fa\u7684\u54c8\u5e0c\u503c\u7684\u5747\u5300\u6027\u4f1a\u660e\u663e\u63d0\u5347\u3002

        \\[ \\begin{aligned} \\text{modulus} & = 13 \\newline \\text{key} & = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\dots \\} \\newline \\text{hash} & = \\{ 0, 3, 6, 9, 12, 2, 5, 8, 11, 1, 4, 7, \\dots \\} \\end{aligned} \\]

        \u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u5982\u679c\u80fd\u591f\u4fdd\u8bc1 key \u662f\u968f\u673a\u5747\u5300\u5206\u5e03\u7684\uff0c\u90a3\u4e48\u9009\u62e9\u8d28\u6570\u6216\u8005\u5408\u6570\u4f5c\u4e3a\u6a21\u6570\u90fd\u53ef\u4ee5\uff0c\u5b83\u4eec\u90fd\u80fd\u8f93\u51fa\u5747\u5300\u5206\u5e03\u7684\u54c8\u5e0c\u503c\u3002\u800c\u5f53 key \u7684\u5206\u5e03\u5b58\u5728\u67d0\u79cd\u5468\u671f\u6027\u65f6\uff0c\u5bf9\u5408\u6570\u53d6\u6a21\u66f4\u5bb9\u6613\u51fa\u73b0\u805a\u96c6\u73b0\u8c61\u3002

        \u603b\u800c\u8a00\u4e4b\uff0c\u6211\u4eec\u901a\u5e38\u9009\u53d6\u8d28\u6570\u4f5c\u4e3a\u6a21\u6570\uff0c\u5e76\u4e14\u8fd9\u4e2a\u8d28\u6570\u6700\u597d\u8db3\u591f\u5927\uff0c\u4ee5\u5c3d\u53ef\u80fd\u6d88\u9664\u5468\u671f\u6027\u6a21\u5f0f\uff0c\u63d0\u5347\u54c8\u5e0c\u7b97\u6cd5\u7684\u7a33\u5065\u6027\u3002

        "},{"location":"chapter_hashing/hash_algorithm/#633","title":"6.3.3 \u00a0 \u5e38\u89c1\u54c8\u5e0c\u7b97\u6cd5","text":"

        \u4e0d\u96be\u53d1\u73b0\uff0c\u4ee5\u4e0a\u4ecb\u7ecd\u7684\u7b80\u5355\u54c8\u5e0c\u7b97\u6cd5\u90fd\u6bd4\u8f83\u201c\u8106\u5f31\u201d\uff0c\u8fdc\u8fdc\u6ca1\u6709\u8fbe\u5230\u54c8\u5e0c\u7b97\u6cd5\u7684\u8bbe\u8ba1\u76ee\u6807\u3002\u4f8b\u5982\uff0c\u7531\u4e8e\u52a0\u6cd5\u548c\u5f02\u6216\u6ee1\u8db3\u4ea4\u6362\u5f8b\uff0c\u56e0\u6b64\u52a0\u6cd5\u54c8\u5e0c\u548c\u5f02\u6216\u54c8\u5e0c\u65e0\u6cd5\u533a\u5206\u5185\u5bb9\u76f8\u540c\u4f46\u987a\u5e8f\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\uff0c\u8fd9\u53ef\u80fd\u4f1a\u52a0\u5267\u54c8\u5e0c\u51b2\u7a81\uff0c\u5e76\u5f15\u8d77\u4e00\u4e9b\u5b89\u5168\u95ee\u9898\u3002

        \u5728\u5b9e\u9645\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u7528\u4e00\u4e9b\u6807\u51c6\u54c8\u5e0c\u7b97\u6cd5\uff0c\u4f8b\u5982 MD5\u3001SHA-1\u3001SHA-2 \u548c SHA-3 \u7b49\u3002\u5b83\u4eec\u53ef\u4ee5\u5c06\u4efb\u610f\u957f\u5ea6\u7684\u8f93\u5165\u6570\u636e\u6620\u5c04\u5230\u6052\u5b9a\u957f\u5ea6\u7684\u54c8\u5e0c\u503c\u3002

        \u8fd1\u4e00\u4e2a\u4e16\u7eaa\u4ee5\u6765\uff0c\u54c8\u5e0c\u7b97\u6cd5\u5904\u5728\u4e0d\u65ad\u5347\u7ea7\u4e0e\u4f18\u5316\u7684\u8fc7\u7a0b\u4e2d\u3002\u4e00\u90e8\u5206\u7814\u7a76\u4eba\u5458\u52aa\u529b\u63d0\u5347\u54c8\u5e0c\u7b97\u6cd5\u7684\u6027\u80fd\uff0c\u53e6\u4e00\u90e8\u5206\u7814\u7a76\u4eba\u5458\u548c\u9ed1\u5ba2\u5219\u81f4\u529b\u4e8e\u5bfb\u627e\u54c8\u5e0c\u7b97\u6cd5\u7684\u5b89\u5168\u6027\u95ee\u9898\u3002\u8868 6-2 \u5c55\u793a\u4e86\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u5e38\u89c1\u7684\u54c8\u5e0c\u7b97\u6cd5\u3002

        • MD5 \u548c SHA-1 \u5df2\u591a\u6b21\u88ab\u6210\u529f\u653b\u51fb\uff0c\u56e0\u6b64\u5b83\u4eec\u88ab\u5404\u7c7b\u5b89\u5168\u5e94\u7528\u5f03\u7528\u3002
        • SHA-2 \u7cfb\u5217\u4e2d\u7684 SHA-256 \u662f\u6700\u5b89\u5168\u7684\u54c8\u5e0c\u7b97\u6cd5\u4e4b\u4e00\uff0c\u4ecd\u672a\u51fa\u73b0\u6210\u529f\u7684\u653b\u51fb\u6848\u4f8b\uff0c\u56e0\u6b64\u5e38\u7528\u5728\u5404\u7c7b\u5b89\u5168\u5e94\u7528\u4e0e\u534f\u8bae\u4e2d\u3002
        • SHA-3 \u76f8\u8f83 SHA-2 \u7684\u5b9e\u73b0\u5f00\u9500\u66f4\u4f4e\u3001\u8ba1\u7b97\u6548\u7387\u66f4\u9ad8\uff0c\u4f46\u76ee\u524d\u4f7f\u7528\u8986\u76d6\u5ea6\u4e0d\u5982 SHA-2 \u7cfb\u5217\u3002

        \u8868 6-2 \u00a0 \u5e38\u89c1\u7684\u54c8\u5e0c\u7b97\u6cd5

        MD5 SHA-1 SHA-2 SHA-3 \u63a8\u51fa\u65f6\u95f4 1992 1995 2002 2008 \u8f93\u51fa\u957f\u5ea6 128 bit 160 bit 256/512 bit 224/256/384/512 bit \u54c8\u5e0c\u51b2\u7a81 \u8f83\u591a \u8f83\u591a \u5f88\u5c11 \u5f88\u5c11 \u5b89\u5168\u7b49\u7ea7 \u4f4e\uff0c\u5df2\u88ab\u6210\u529f\u653b\u51fb \u4f4e\uff0c\u5df2\u88ab\u6210\u529f\u653b\u51fb \u9ad8 \u9ad8 \u5e94\u7528 \u5df2\u88ab\u5f03\u7528\uff0c\u4ecd\u7528\u4e8e\u6570\u636e\u5b8c\u6574\u6027\u68c0\u67e5 \u5df2\u88ab\u5f03\u7528 \u52a0\u5bc6\u8d27\u5e01\u4ea4\u6613\u9a8c\u8bc1\u3001\u6570\u5b57\u7b7e\u540d\u7b49 \u53ef\u7528\u4e8e\u66ff\u4ee3 SHA-2"},{"location":"chapter_hashing/hash_algorithm/#634","title":"6.3.4 \u00a0 \u6570\u636e\u7ed3\u6784\u7684\u54c8\u5e0c\u503c","text":"

        \u6211\u4eec\u77e5\u9053\uff0c\u54c8\u5e0c\u8868\u7684 key \u53ef\u4ee5\u662f\u6574\u6570\u3001\u5c0f\u6570\u6216\u5b57\u7b26\u4e32\u7b49\u6570\u636e\u7c7b\u578b\u3002\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u4f1a\u4e3a\u8fd9\u4e9b\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u5185\u7f6e\u7684\u54c8\u5e0c\u7b97\u6cd5\uff0c\u7528\u4e8e\u8ba1\u7b97\u54c8\u5e0c\u8868\u4e2d\u7684\u6876\u7d22\u5f15\u3002\u4ee5 Python \u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528 hash() \u51fd\u6570\u6765\u8ba1\u7b97\u5404\u79cd\u6570\u636e\u7c7b\u578b\u7684\u54c8\u5e0c\u503c\u3002

        • \u6574\u6570\u548c\u5e03\u5c14\u91cf\u7684\u54c8\u5e0c\u503c\u5c31\u662f\u5176\u672c\u8eab\u3002
        • \u6d6e\u70b9\u6570\u548c\u5b57\u7b26\u4e32\u7684\u54c8\u5e0c\u503c\u8ba1\u7b97\u8f83\u4e3a\u590d\u6742\uff0c\u6709\u5174\u8da3\u7684\u8bfb\u8005\u8bf7\u81ea\u884c\u5b66\u4e60\u3002
        • \u5143\u7ec4\u7684\u54c8\u5e0c\u503c\u662f\u5bf9\u5176\u4e2d\u6bcf\u4e00\u4e2a\u5143\u7d20\u8fdb\u884c\u54c8\u5e0c\uff0c\u7136\u540e\u5c06\u8fd9\u4e9b\u54c8\u5e0c\u503c\u7ec4\u5408\u8d77\u6765\uff0c\u5f97\u5230\u5355\u4e00\u7684\u54c8\u5e0c\u503c\u3002
        • \u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u57fa\u4e8e\u5176\u5185\u5b58\u5730\u5740\u751f\u6210\u3002\u901a\u8fc7\u91cd\u5199\u5bf9\u8c61\u7684\u54c8\u5e0c\u65b9\u6cd5\uff0c\u53ef\u5b9e\u73b0\u57fa\u4e8e\u5185\u5bb9\u751f\u6210\u54c8\u5e0c\u503c\u3002

        Tip

        \u8bf7\u6ce8\u610f\uff0c\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u7684\u5185\u7f6e\u54c8\u5e0c\u503c\u8ba1\u7b97\u51fd\u6570\u7684\u5b9a\u4e49\u548c\u65b9\u6cd5\u4e0d\u540c\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby built_in_hash.py
        num = 3\nhash_num = hash(num)\n# \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\n\nbol = True\nhash_bol = hash(bol)\n# \u5e03\u5c14\u91cf True \u7684\u54c8\u5e0c\u503c\u4e3a 1\n\ndec = 3.14159\nhash_dec = hash(dec)\n# \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 326484311674566659\n\nstr = \"Hello \u7b97\u6cd5\"\nhash_str = hash(str)\n# \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a 4617003410720528961\n\ntup = (12836, \"\u5c0f\u54c8\")\nhash_tup = hash(tup)\n# \u5143\u7ec4 (12836, '\u5c0f\u54c8') \u7684\u54c8\u5e0c\u503c\u4e3a 1029005403108185979\n\nobj = ListNode(0)\nhash_obj = hash(obj)\n# \u8282\u70b9\u5bf9\u8c61 <ListNode object at 0x1058fd810> \u7684\u54c8\u5e0c\u503c\u4e3a 274267521\n
        built_in_hash.cpp
        int num = 3;\nsize_t hashNum = hash<int>()(num);\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\n\nbool bol = true;\nsize_t hashBol = hash<bool>()(bol);\n// \u5e03\u5c14\u91cf 1 \u7684\u54c8\u5e0c\u503c\u4e3a 1\n\ndouble dec = 3.14159;\nsize_t hashDec = hash<double>()(dec);\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 4614256650576692846\n\nstring str = \"Hello \u7b97\u6cd5\";\nsize_t hashStr = hash<string>()(str);\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a 15466937326284535026\n\n// \u5728 C++ \u4e2d\uff0c\u5185\u7f6e std:hash() \u4ec5\u63d0\u4f9b\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u54c8\u5e0c\u503c\u8ba1\u7b97\n// \u6570\u7ec4\u3001\u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u8ba1\u7b97\u9700\u8981\u81ea\u884c\u5b9e\u73b0\n
        built_in_hash.java
        int num = 3;\nint hashNum = Integer.hashCode(num);\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\n\nboolean bol = true;\nint hashBol = Boolean.hashCode(bol);\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 1231\n\ndouble dec = 3.14159;\nint hashDec = Double.hashCode(dec);\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -1340954729\n\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode();\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a -727081396\n\nObject[] arr = { 12836, \"\u5c0f\u54c8\" };\nint hashTup = Arrays.hashCode(arr);\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 1151158\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode();\n// \u8282\u70b9\u5bf9\u8c61 utils.ListNode@7dc5e7b4 \u7684\u54c8\u5e0c\u503c\u4e3a 2110121908\n
        built_in_hash.cs
        int num = 3;\nint hashNum = num.GetHashCode();\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3;\n\nbool bol = true;\nint hashBol = bol.GetHashCode();\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 1;\n\ndouble dec = 3.14159;\nint hashDec = dec.GetHashCode();\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -1340954729;\n\nstring str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.GetHashCode();\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a -586107568;\n\nobject[] arr = [12836, \"\u5c0f\u54c8\"];\nint hashTup = arr.GetHashCode();\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 42931033;\n\nListNode obj = new(0);\nint hashObj = obj.GetHashCode();\n// \u8282\u70b9\u5bf9\u8c61 0 \u7684\u54c8\u5e0c\u503c\u4e3a 39053774;\n
        built_in_hash.go
        // Go \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n
        built_in_hash.swift
        let num = 3\nlet hashNum = num.hashValue\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 9047044699613009734\n\nlet bol = true\nlet hashBol = bol.hashValue\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a -4431640247352757451\n\nlet dec = 3.14159\nlet hashDec = dec.hashValue\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -2465384235396674631\n\nlet str = \"Hello \u7b97\u6cd5\"\nlet hashStr = str.hashValue\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a -7850626797806988787\n\nlet arr = [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")]\nlet hashTup = arr.hashValue\n// \u6570\u7ec4 [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")] \u7684\u54c8\u5e0c\u503c\u4e3a -2308633508154532996\n\nlet obj = ListNode(x: 0)\nlet hashObj = obj.hashValue\n// \u8282\u70b9\u5bf9\u8c61 utils.ListNode \u7684\u54c8\u5e0c\u503c\u4e3a -2434780518035996159\n
        built_in_hash.js
        // JavaScript \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n
        built_in_hash.ts
        // TypeScript \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n
        built_in_hash.dart
        int num = 3;\nint hashNum = num.hashCode;\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 34803\n\nbool bol = true;\nint hashBol = bol.hashCode;\n// \u5e03\u5c14\u503c true \u7684\u54c8\u5e0c\u503c\u4e3a 1231\n\ndouble dec = 3.14159;\nint hashDec = dec.hashCode;\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 2570631074981783\n\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode;\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a 468167534\n\nList arr = [12836, \"\u5c0f\u54c8\"];\nint hashArr = arr.hashCode;\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 976512528\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode;\n// \u8282\u70b9\u5bf9\u8c61 Instance of 'ListNode' \u7684\u54c8\u5e0c\u503c\u4e3a 1033450432\n
        built_in_hash.rs
        use std::collections::hash_map::DefaultHasher;\nuse std::hash::{Hash, Hasher};\n\nlet num = 3;\nlet mut num_hasher = DefaultHasher::new();\nnum.hash(&mut num_hasher);\nlet hash_num = num_hasher.finish();\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 568126464209439262\n\nlet bol = true;\nlet mut bol_hasher = DefaultHasher::new();\nbol.hash(&mut bol_hasher);\nlet hash_bol = bol_hasher.finish();\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 4952851536318644461\n\nlet dec: f32 = 3.14159;\nlet mut dec_hasher = DefaultHasher::new();\ndec.to_bits().hash(&mut dec_hasher);\nlet hash_dec = dec_hasher.finish();\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 2566941990314602357\n\nlet str = \"Hello \u7b97\u6cd5\";\nlet mut str_hasher = DefaultHasher::new();\nstr.hash(&mut str_hasher);\nlet hash_str = str_hasher.finish();\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a 16092673739211250988\n\nlet arr = (&12836, &\"\u5c0f\u54c8\");\nlet mut tup_hasher = DefaultHasher::new();\narr.hash(&mut tup_hasher);\nlet hash_tup = tup_hasher.finish();\n// \u5143\u7ec4 (12836, \"\u5c0f\u54c8\") \u7684\u54c8\u5e0c\u503c\u4e3a 1885128010422702749\n\nlet node = ListNode::new(42);\nlet mut hasher = DefaultHasher::new();\nnode.borrow().val.hash(&mut hasher);\nlet hash = hasher.finish();\n// \u8282\u70b9\u5bf9\u8c61 RefCell { value: ListNode { val: 42, next: None } } \u7684\u54c8\u5e0c\u503c\u4e3a15387811073369036852\n
        built_in_hash.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n
        built_in_hash.kt
        val num = 3\nval hashNum = num.hashCode()\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\n\nval bol = true\nval hashBol = bol.hashCode()\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 1231\n\nval dec = 3.14159\nval hashDec = dec.hashCode()\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -1340954729\n\nval str = \"Hello \u7b97\u6cd5\"\nval hashStr = str.hashCode()\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a -727081396\n\nval arr = arrayOf<Any>(12836, \"\u5c0f\u54c8\")\nval hashTup = arr.hashCode()\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 189568618\n\nval obj = ListNode(0)\nval hashObj = obj.hashCode()\n// \u8282\u70b9\u5bf9\u8c61 utils.ListNode@1d81eb93 \u7684\u54c8\u5e0c\u503c\u4e3a 495053715\n
        built_in_hash.rb
        num = 3\nhash_num = num.hash\n# \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a -4385856518450339636\n\nbol = true\nhash_bol = bol.hash\n# \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a -1617938112149317027\n\ndec = 3.14159\nhash_dec = dec.hash\n# \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -1479186995943067893\n\nstr = \"Hello \u7b97\u6cd5\"\nhash_str = str.hash\n# \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a -4075943250025831763\n\ntup = [12836, '\u5c0f\u54c8']\nhash_tup = tup.hash\n# \u5143\u7ec4 (12836, '\u5c0f\u54c8') \u7684\u54c8\u5e0c\u503c\u4e3a 1999544809202288822\n\nobj = ListNode.new(0)\nhash_obj = obj.hash\n# \u8282\u70b9\u5bf9\u8c61 #<ListNode:0x000078133140ab70> \u7684\u54c8\u5e0c\u503c\u4e3a 4302940560806366381\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5728\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u53ea\u6709\u4e0d\u53ef\u53d8\u5bf9\u8c61\u624d\u53ef\u4f5c\u4e3a\u54c8\u5e0c\u8868\u7684 key \u3002\u5047\u5982\u6211\u4eec\u5c06\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\u4f5c\u4e3a key \uff0c\u5f53\u5217\u8868\u7684\u5185\u5bb9\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u5b83\u7684\u54c8\u5e0c\u503c\u4e5f\u968f\u4e4b\u6539\u53d8\uff0c\u6211\u4eec\u5c31\u65e0\u6cd5\u5728\u54c8\u5e0c\u8868\u4e2d\u67e5\u8be2\u5230\u539f\u5148\u7684 value \u4e86\u3002

        \u867d\u7136\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff08\u6bd4\u5982\u94fe\u8868\u8282\u70b9\uff09\u7684\u6210\u5458\u53d8\u91cf\u662f\u53ef\u53d8\u7684\uff0c\u4f46\u5b83\u662f\u53ef\u54c8\u5e0c\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u901a\u5e38\u662f\u57fa\u4e8e\u5185\u5b58\u5730\u5740\u751f\u6210\u7684\uff0c\u5373\u4f7f\u5bf9\u8c61\u7684\u5185\u5bb9\u53d1\u751f\u4e86\u53d8\u5316\uff0c\u4f46\u5b83\u7684\u5185\u5b58\u5730\u5740\u4e0d\u53d8\uff0c\u54c8\u5e0c\u503c\u4ecd\u7136\u662f\u4e0d\u53d8\u7684\u3002

        \u7ec6\u5fc3\u7684\u4f60\u53ef\u80fd\u53d1\u73b0\u5728\u4e0d\u540c\u63a7\u5236\u53f0\u4e2d\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u8f93\u51fa\u7684\u54c8\u5e0c\u503c\u662f\u4e0d\u540c\u7684\u3002\u8fd9\u662f\u56e0\u4e3a Python \u89e3\u91ca\u5668\u5728\u6bcf\u6b21\u542f\u52a8\u65f6\uff0c\u90fd\u4f1a\u4e3a\u5b57\u7b26\u4e32\u54c8\u5e0c\u51fd\u6570\u52a0\u5165\u4e00\u4e2a\u968f\u673a\u7684\u76d0\uff08salt\uff09\u503c\u3002\u8fd9\u79cd\u505a\u6cd5\u53ef\u4ee5\u6709\u6548\u9632\u6b62 HashDoS \u653b\u51fb\uff0c\u63d0\u5347\u54c8\u5e0c\u7b97\u6cd5\u7684\u5b89\u5168\u6027\u3002

        "},{"location":"chapter_hashing/hash_collision/","title":"6.2 \u00a0 \u54c8\u5e0c\u51b2\u7a81","text":"

        \u4e0a\u4e00\u8282\u63d0\u5230\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u54c8\u5e0c\u51fd\u6570\u7684\u8f93\u5165\u7a7a\u95f4\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\uff0c\u56e0\u6b64\u7406\u8bba\u4e0a\u54c8\u5e0c\u51b2\u7a81\u662f\u4e0d\u53ef\u907f\u514d\u7684\u3002\u6bd4\u5982\uff0c\u8f93\u5165\u7a7a\u95f4\u4e3a\u5168\u4f53\u6574\u6570\uff0c\u8f93\u51fa\u7a7a\u95f4\u4e3a\u6570\u7ec4\u5bb9\u91cf\u5927\u5c0f\uff0c\u5219\u5fc5\u7136\u6709\u591a\u4e2a\u6574\u6570\u6620\u5c04\u81f3\u540c\u4e00\u6876\u7d22\u5f15\u3002

        \u54c8\u5e0c\u51b2\u7a81\u4f1a\u5bfc\u81f4\u67e5\u8be2\u7ed3\u679c\u9519\u8bef\uff0c\u4e25\u91cd\u5f71\u54cd\u54c8\u5e0c\u8868\u7684\u53ef\u7528\u6027\u3002\u4e3a\u4e86\u89e3\u51b3\u8be5\u95ee\u9898\uff0c\u6bcf\u5f53\u9047\u5230\u54c8\u5e0c\u51b2\u7a81\u65f6\uff0c\u6211\u4eec\u5c31\u8fdb\u884c\u54c8\u5e0c\u8868\u6269\u5bb9\uff0c\u76f4\u81f3\u51b2\u7a81\u6d88\u5931\u4e3a\u6b62\u3002\u6b64\u65b9\u6cd5\u7b80\u5355\u7c97\u66b4\u4e14\u6709\u6548\uff0c\u4f46\u6548\u7387\u592a\u4f4e\uff0c\u56e0\u4e3a\u54c8\u5e0c\u8868\u6269\u5bb9\u9700\u8981\u8fdb\u884c\u5927\u91cf\u7684\u6570\u636e\u642c\u8fd0\u4e0e\u54c8\u5e0c\u503c\u8ba1\u7b97\u3002\u4e3a\u4e86\u63d0\u5347\u6548\u7387\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u7b56\u7565\u3002

        1. \u6539\u826f\u54c8\u5e0c\u8868\u6570\u636e\u7ed3\u6784\uff0c\u4f7f\u5f97\u54c8\u5e0c\u8868\u53ef\u4ee5\u5728\u51fa\u73b0\u54c8\u5e0c\u51b2\u7a81\u65f6\u6b63\u5e38\u5de5\u4f5c\u3002
        2. \u4ec5\u5728\u5fc5\u8981\u65f6\uff0c\u5373\u5f53\u54c8\u5e0c\u51b2\u7a81\u6bd4\u8f83\u4e25\u91cd\u65f6\uff0c\u624d\u6267\u884c\u6269\u5bb9\u64cd\u4f5c\u3002

        \u54c8\u5e0c\u8868\u7684\u7ed3\u6784\u6539\u826f\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u201c\u94fe\u5f0f\u5730\u5740\u201d\u548c\u201c\u5f00\u653e\u5bfb\u5740\u201d\u3002

        "},{"location":"chapter_hashing/hash_collision/#621","title":"6.2.1 \u00a0 \u94fe\u5f0f\u5730\u5740","text":"

        \u5728\u539f\u59cb\u54c8\u5e0c\u8868\u4e2d\uff0c\u6bcf\u4e2a\u6876\u4ec5\u80fd\u5b58\u50a8\u4e00\u4e2a\u952e\u503c\u5bf9\u3002\u94fe\u5f0f\u5730\u5740\uff08separate chaining\uff09\u5c06\u5355\u4e2a\u5143\u7d20\u8f6c\u6362\u4e3a\u94fe\u8868\uff0c\u5c06\u952e\u503c\u5bf9\u4f5c\u4e3a\u94fe\u8868\u8282\u70b9\uff0c\u5c06\u6240\u6709\u53d1\u751f\u51b2\u7a81\u7684\u952e\u503c\u5bf9\u90fd\u5b58\u50a8\u5728\u540c\u4e00\u94fe\u8868\u4e2d\u3002\u56fe 6-5 \u5c55\u793a\u4e86\u4e00\u4e2a\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\u7684\u4f8b\u5b50\u3002

        \u56fe 6-5 \u00a0 \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868

        \u57fa\u4e8e\u94fe\u5f0f\u5730\u5740\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\u7684\u64cd\u4f5c\u65b9\u6cd5\u53d1\u751f\u4e86\u4ee5\u4e0b\u53d8\u5316\u3002

        • \u67e5\u8be2\u5143\u7d20\uff1a\u8f93\u5165 key \uff0c\u7ecf\u8fc7\u54c8\u5e0c\u51fd\u6570\u5f97\u5230\u6876\u7d22\u5f15\uff0c\u5373\u53ef\u8bbf\u95ee\u94fe\u8868\u5934\u8282\u70b9\uff0c\u7136\u540e\u904d\u5386\u94fe\u8868\u5e76\u5bf9\u6bd4 key \u4ee5\u67e5\u627e\u76ee\u6807\u952e\u503c\u5bf9\u3002
        • \u6dfb\u52a0\u5143\u7d20\uff1a\u9996\u5148\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u8bbf\u95ee\u94fe\u8868\u5934\u8282\u70b9\uff0c\u7136\u540e\u5c06\u8282\u70b9\uff08\u952e\u503c\u5bf9\uff09\u6dfb\u52a0\u5230\u94fe\u8868\u4e2d\u3002
        • \u5220\u9664\u5143\u7d20\uff1a\u6839\u636e\u54c8\u5e0c\u51fd\u6570\u7684\u7ed3\u679c\u8bbf\u95ee\u94fe\u8868\u5934\u90e8\uff0c\u63a5\u7740\u904d\u5386\u94fe\u8868\u4ee5\u67e5\u627e\u76ee\u6807\u8282\u70b9\u5e76\u5c06\u5176\u5220\u9664\u3002

        \u94fe\u5f0f\u5730\u5740\u5b58\u5728\u4ee5\u4e0b\u5c40\u9650\u6027\u3002

        • \u5360\u7528\u7a7a\u95f4\u589e\u5927\uff1a\u94fe\u8868\u5305\u542b\u8282\u70b9\u6307\u9488\uff0c\u5b83\u76f8\u6bd4\u6570\u7ec4\u66f4\u52a0\u8017\u8d39\u5185\u5b58\u7a7a\u95f4\u3002
        • \u67e5\u8be2\u6548\u7387\u964d\u4f4e\uff1a\u56e0\u4e3a\u9700\u8981\u7ebf\u6027\u904d\u5386\u94fe\u8868\u6765\u67e5\u627e\u5bf9\u5e94\u5143\u7d20\u3002

        \u4ee5\u4e0b\u4ee3\u7801\u7ed9\u51fa\u4e86\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\u7684\u7b80\u5355\u5b9e\u73b0\uff0c\u9700\u8981\u6ce8\u610f\u4e24\u70b9\u3002

        • \u4f7f\u7528\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\u4ee3\u66ff\u94fe\u8868\uff0c\u4ece\u800c\u7b80\u5316\u4ee3\u7801\u3002\u5728\u8fd9\u79cd\u8bbe\u5b9a\u4e0b\uff0c\u54c8\u5e0c\u8868\uff08\u6570\u7ec4\uff09\u5305\u542b\u591a\u4e2a\u6876\uff0c\u6bcf\u4e2a\u6876\u90fd\u662f\u4e00\u4e2a\u5217\u8868\u3002
        • \u4ee5\u4e0b\u5b9e\u73b0\u5305\u542b\u54c8\u5e0c\u8868\u6269\u5bb9\u65b9\u6cd5\u3002\u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7 \\(\\frac{2}{3}\\) \u65f6\uff0c\u6211\u4eec\u5c06\u54c8\u5e0c\u8868\u6269\u5bb9\u81f3\u539f\u5148\u7684 \\(2\\) \u500d\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map_chaining.py
        class HashMapChaining:\n    \"\"\"\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\"\"\"\n\n    def __init__(self):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        self.size = 0  # \u952e\u503c\u5bf9\u6570\u91cf\n        self.capacity = 4  # \u54c8\u5e0c\u8868\u5bb9\u91cf\n        self.load_thres = 2.0 / 3.0  # \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n        self.extend_ratio = 2  # \u6269\u5bb9\u500d\u6570\n        self.buckets = [[] for _ in range(self.capacity)]  # \u6876\u6570\u7ec4\n\n    def hash_func(self, key: int) -> int:\n        \"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\n        return key % self.capacity\n\n    def load_factor(self) -> float:\n        \"\"\"\u8d1f\u8f7d\u56e0\u5b50\"\"\"\n        return self.size / self.capacity\n\n    def get(self, key: int) -> str | None:\n        \"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\n        index = self.hash_func(key)\n        bucket = self.buckets[index]\n        # \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        for pair in bucket:\n            if pair.key == key:\n                return pair.val\n        # \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de None\n        return None\n\n    def put(self, key: int, val: str):\n        \"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"\n        # \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if self.load_factor() > self.load_thres:\n            self.extend()\n        index = self.hash_func(key)\n        bucket = self.buckets[index]\n        # \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n        for pair in bucket:\n            if pair.key == key:\n                pair.val = val\n                return\n        # \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n        pair = Pair(key, val)\n        bucket.append(pair)\n        self.size += 1\n\n    def remove(self, key: int):\n        \"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\n        index = self.hash_func(key)\n        bucket = self.buckets[index]\n        # \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n        for pair in bucket:\n            if pair.key == key:\n                bucket.remove(pair)\n                self.size -= 1\n                break\n\n    def extend(self):\n        \"\"\"\u6269\u5bb9\u54c8\u5e0c\u8868\"\"\"\n        # \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        buckets = self.buckets\n        # \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        self.capacity *= self.extend_ratio\n        self.buckets = [[] for _ in range(self.capacity)]\n        self.size = 0\n        # \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for bucket in buckets:\n            for pair in bucket:\n                self.put(pair.key, pair.val)\n\n    def print(self):\n        \"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\n        for bucket in self.buckets:\n            res = []\n            for pair in bucket:\n                res.append(str(pair.key) + \" -> \" + pair.val)\n            print(res)\n
        hash_map_chaining.cpp
        /* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n  private:\n    int size;                       // \u952e\u503c\u5bf9\u6570\u91cf\n    int capacity;                   // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    double loadThres;               // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    int extendRatio;                // \u6269\u5bb9\u500d\u6570\n    vector<vector<Pair *>> buckets; // \u6876\u6570\u7ec4\n\n  public:\n    /* \u6784\u9020\u65b9\u6cd5 */\n    HashMapChaining() : size(0), capacity(4), loadThres(2.0 / 3.0), extendRatio(2) {\n        buckets.resize(capacity);\n    }\n\n    /* \u6790\u6784\u65b9\u6cd5 */\n    ~HashMapChaining() {\n        for (auto &bucket : buckets) {\n            for (Pair *pair : bucket) {\n                // \u91ca\u653e\u5185\u5b58\n                delete pair;\n            }\n        }\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    double loadFactor() {\n        return (double)size / (double)capacity;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    string get(int key) {\n        int index = hashFunc(key);\n        // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        for (Pair *pair : buckets[index]) {\n            if (pair->key == key) {\n                return pair->val;\n            }\n        }\n        // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n        return \"\";\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    void put(int key, string val) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        int index = hashFunc(key);\n        // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n        for (Pair *pair : buckets[index]) {\n            if (pair->key == key) {\n                pair->val = val;\n                return;\n            }\n        }\n        // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n        buckets[index].push_back(new Pair(key, val));\n        size++;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    void remove(int key) {\n        int index = hashFunc(key);\n        auto &bucket = buckets[index];\n        // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n        for (int i = 0; i < bucket.size(); i++) {\n            if (bucket[i]->key == key) {\n                Pair *tmp = bucket[i];\n                bucket.erase(bucket.begin() + i); // \u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n                delete tmp;                       // \u91ca\u653e\u5185\u5b58\n                size--;\n                return;\n            }\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    void extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        vector<vector<Pair *>> bucketsTmp = buckets;\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        capacity *= extendRatio;\n        buckets.clear();\n        buckets.resize(capacity);\n        size = 0;\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for (auto &bucket : bucketsTmp) {\n            for (Pair *pair : bucket) {\n                put(pair->key, pair->val);\n                // \u91ca\u653e\u5185\u5b58\n                delete pair;\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    void print() {\n        for (auto &bucket : buckets) {\n            cout << \"[\";\n            for (Pair *pair : bucket) {\n                cout << pair->key << \" -> \" << pair->val << \", \";\n            }\n            cout << \"]\\n\";\n        }\n    }\n};\n
        hash_map_chaining.java
        /* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n    int size; // \u952e\u503c\u5bf9\u6570\u91cf\n    int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    int extendRatio; // \u6269\u5bb9\u500d\u6570\n    List<List<Pair>> buckets; // \u6876\u6570\u7ec4\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public HashMapChaining() {\n        size = 0;\n        capacity = 4;\n        loadThres = 2.0 / 3.0;\n        extendRatio = 2;\n        buckets = new ArrayList<>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.add(new ArrayList<>());\n        }\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    double loadFactor() {\n        return (double) size / capacity;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    String get(int key) {\n        int index = hashFunc(key);\n        List<Pair> bucket = buckets.get(index);\n        // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        for (Pair pair : bucket) {\n            if (pair.key == key) {\n                return pair.val;\n            }\n        }\n        // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n        return null;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    void put(int key, String val) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        int index = hashFunc(key);\n        List<Pair> bucket = buckets.get(index);\n        // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n        for (Pair pair : bucket) {\n            if (pair.key == key) {\n                pair.val = val;\n                return;\n            }\n        }\n        // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n        Pair pair = new Pair(key, val);\n        bucket.add(pair);\n        size++;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    void remove(int key) {\n        int index = hashFunc(key);\n        List<Pair> bucket = buckets.get(index);\n        // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n        for (Pair pair : bucket) {\n            if (pair.key == key) {\n                bucket.remove(pair);\n                size--;\n                break;\n            }\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    void extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        List<List<Pair>> bucketsTmp = buckets;\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        capacity *= extendRatio;\n        buckets = new ArrayList<>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.add(new ArrayList<>());\n        }\n        size = 0;\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for (List<Pair> bucket : bucketsTmp) {\n            for (Pair pair : bucket) {\n                put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    void print() {\n        for (List<Pair> bucket : buckets) {\n            List<String> res = new ArrayList<>();\n            for (Pair pair : bucket) {\n                res.add(pair.key + \" -> \" + pair.val);\n            }\n            System.out.println(res);\n        }\n    }\n}\n
        hash_map_chaining.cs
        /* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n    int size; // \u952e\u503c\u5bf9\u6570\u91cf\n    int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    int extendRatio; // \u6269\u5bb9\u500d\u6570\n    List<List<Pair>> buckets; // \u6876\u6570\u7ec4\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public HashMapChaining() {\n        size = 0;\n        capacity = 4;\n        loadThres = 2.0 / 3.0;\n        extendRatio = 2;\n        buckets = new List<List<Pair>>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.Add([]);\n        }\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    int HashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    double LoadFactor() {\n        return (double)size / capacity;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    public string? Get(int key) {\n        int index = HashFunc(key);\n        // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        foreach (Pair pair in buckets[index]) {\n            if (pair.key == key) {\n                return pair.val;\n            }\n        }\n        // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n        return null;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    public void Put(int key, string val) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (LoadFactor() > loadThres) {\n            Extend();\n        }\n        int index = HashFunc(key);\n        // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n        foreach (Pair pair in buckets[index]) {\n            if (pair.key == key) {\n                pair.val = val;\n                return;\n            }\n        }\n        // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n        buckets[index].Add(new Pair(key, val));\n        size++;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    public void Remove(int key) {\n        int index = HashFunc(key);\n        // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n        foreach (Pair pair in buckets[index].ToList()) {\n            if (pair.key == key) {\n                buckets[index].Remove(pair);\n                size--;\n                break;\n            }\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    void Extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        List<List<Pair>> bucketsTmp = buckets;\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        capacity *= extendRatio;\n        buckets = new List<List<Pair>>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.Add([]);\n        }\n        size = 0;\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        foreach (List<Pair> bucket in bucketsTmp) {\n            foreach (Pair pair in bucket) {\n                Put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    public void Print() {\n        foreach (List<Pair> bucket in buckets) {\n            List<string> res = [];\n            foreach (Pair pair in bucket) {\n                res.Add(pair.key + \" -> \" + pair.val);\n            }\n            foreach (string kv in res) {\n                Console.WriteLine(kv);\n            }\n        }\n    }\n}\n
        hash_map_chaining.go
        /* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\ntype hashMapChaining struct {\n    size        int      // \u952e\u503c\u5bf9\u6570\u91cf\n    capacity    int      // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    loadThres   float64  // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    extendRatio int      // \u6269\u5bb9\u500d\u6570\n    buckets     [][]pair // \u6876\u6570\u7ec4\n}\n\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newHashMapChaining() *hashMapChaining {\n    buckets := make([][]pair, 4)\n    for i := 0; i < 4; i++ {\n        buckets[i] = make([]pair, 0)\n    }\n    return &hashMapChaining{\n        size:        0,\n        capacity:    4,\n        loadThres:   2.0 / 3.0,\n        extendRatio: 2,\n        buckets:     buckets,\n    }\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (m *hashMapChaining) hashFunc(key int) int {\n    return key % m.capacity\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc (m *hashMapChaining) loadFactor() float64 {\n    return float64(m.size) / float64(m.capacity)\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (m *hashMapChaining) get(key int) string {\n    idx := m.hashFunc(key)\n    bucket := m.buckets[idx]\n    // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n    for _, p := range bucket {\n        if p.key == key {\n            return p.val\n        }\n    }\n    // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n    return \"\"\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (m *hashMapChaining) put(key int, val string) {\n    // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n    if m.loadFactor() > m.loadThres {\n        m.extend()\n    }\n    idx := m.hashFunc(key)\n    // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n    for i := range m.buckets[idx] {\n        if m.buckets[idx][i].key == key {\n            m.buckets[idx][i].val = val\n            return\n        }\n    }\n    // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n    p := pair{\n        key: key,\n        val: val,\n    }\n    m.buckets[idx] = append(m.buckets[idx], p)\n    m.size += 1\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (m *hashMapChaining) remove(key int) {\n    idx := m.hashFunc(key)\n    // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n    for i, p := range m.buckets[idx] {\n        if p.key == key {\n            // \u5207\u7247\u5220\u9664\n            m.buckets[idx] = append(m.buckets[idx][:i], m.buckets[idx][i+1:]...)\n            m.size -= 1\n            break\n        }\n    }\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc (m *hashMapChaining) extend() {\n    // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n    tmpBuckets := make([][]pair, len(m.buckets))\n    for i := 0; i < len(m.buckets); i++ {\n        tmpBuckets[i] = make([]pair, len(m.buckets[i]))\n        copy(tmpBuckets[i], m.buckets[i])\n    }\n    // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n    m.capacity *= m.extendRatio\n    m.buckets = make([][]pair, m.capacity)\n    for i := 0; i < m.capacity; i++ {\n        m.buckets[i] = make([]pair, 0)\n    }\n    m.size = 0\n    // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n    for _, bucket := range tmpBuckets {\n        for _, p := range bucket {\n            m.put(p.key, p.val)\n        }\n    }\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (m *hashMapChaining) print() {\n    var builder strings.Builder\n\n    for _, bucket := range m.buckets {\n        builder.WriteString(\"[\")\n        for _, p := range bucket {\n            builder.WriteString(strconv.Itoa(p.key) + \" -> \" + p.val + \" \")\n        }\n        builder.WriteString(\"]\")\n        fmt.Println(builder.String())\n        builder.Reset()\n    }\n}\n
        hash_map_chaining.swift
        /* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n    var size: Int // \u952e\u503c\u5bf9\u6570\u91cf\n    var capacity: Int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    var loadThres: Double // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    var extendRatio: Int // \u6269\u5bb9\u500d\u6570\n    var buckets: [[Pair]] // \u6876\u6570\u7ec4\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    init() {\n        size = 0\n        capacity = 4\n        loadThres = 2.0 / 3.0\n        extendRatio = 2\n        buckets = Array(repeating: [], count: capacity)\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    func hashFunc(key: Int) -> Int {\n        key % capacity\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    func loadFactor() -> Double {\n        Double(size) / Double(capacity)\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    func get(key: Int) -> String? {\n        let index = hashFunc(key: key)\n        let bucket = buckets[index]\n        // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        for pair in bucket {\n            if pair.key == key {\n                return pair.val\n            }\n        }\n        // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de nil\n        return nil\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    func put(key: Int, val: String) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if loadFactor() > loadThres {\n            extend()\n        }\n        let index = hashFunc(key: key)\n        let bucket = buckets[index]\n        // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n        for pair in bucket {\n            if pair.key == key {\n                pair.val = val\n                return\n            }\n        }\n        // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n        let pair = Pair(key: key, val: val)\n        buckets[index].append(pair)\n        size += 1\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    func remove(key: Int) {\n        let index = hashFunc(key: key)\n        let bucket = buckets[index]\n        // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n        for (pairIndex, pair) in bucket.enumerated() {\n            if pair.key == key {\n                buckets[index].remove(at: pairIndex)\n                size -= 1\n                break\n            }\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    func extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        let bucketsTmp = buckets\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        capacity *= extendRatio\n        buckets = Array(repeating: [], count: capacity)\n        size = 0\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for bucket in bucketsTmp {\n            for pair in bucket {\n                put(key: pair.key, val: pair.val)\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    func print() {\n        for bucket in buckets {\n            let res = bucket.map { \"\\($0.key) -> \\($0.val)\" }\n            Swift.print(res)\n        }\n    }\n}\n
        hash_map_chaining.js
        /* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n    #size; // \u952e\u503c\u5bf9\u6570\u91cf\n    #capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    #loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    #extendRatio; // \u6269\u5bb9\u500d\u6570\n    #buckets; // \u6876\u6570\u7ec4\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor() {\n        this.#size = 0;\n        this.#capacity = 4;\n        this.#loadThres = 2.0 / 3.0;\n        this.#extendRatio = 2;\n        this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    #hashFunc(key) {\n        return key % this.#capacity;\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    #loadFactor() {\n        return this.#size / this.#capacity;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    get(key) {\n        const index = this.#hashFunc(key);\n        const bucket = this.#buckets[index];\n        // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        for (const pair of bucket) {\n            if (pair.key === key) {\n                return pair.val;\n            }\n        }\n        // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n        return null;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    put(key, val) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (this.#loadFactor() > this.#loadThres) {\n            this.#extend();\n        }\n        const index = this.#hashFunc(key);\n        const bucket = this.#buckets[index];\n        // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n        for (const pair of bucket) {\n            if (pair.key === key) {\n                pair.val = val;\n                return;\n            }\n        }\n        // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n        const pair = new Pair(key, val);\n        bucket.push(pair);\n        this.#size++;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    remove(key) {\n        const index = this.#hashFunc(key);\n        let bucket = this.#buckets[index];\n        // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n        for (let i = 0; i < bucket.length; i++) {\n            if (bucket[i].key === key) {\n                bucket.splice(i, 1);\n                this.#size--;\n                break;\n            }\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    #extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        const bucketsTmp = this.#buckets;\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        this.#capacity *= this.#extendRatio;\n        this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n        this.#size = 0;\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for (const bucket of bucketsTmp) {\n            for (const pair of bucket) {\n                this.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    print() {\n        for (const bucket of this.#buckets) {\n            let res = [];\n            for (const pair of bucket) {\n                res.push(pair.key + ' -> ' + pair.val);\n            }\n            console.log(res);\n        }\n    }\n}\n
        hash_map_chaining.ts
        /* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n    #size: number; // \u952e\u503c\u5bf9\u6570\u91cf\n    #capacity: number; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    #loadThres: number; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    #extendRatio: number; // \u6269\u5bb9\u500d\u6570\n    #buckets: Pair[][]; // \u6876\u6570\u7ec4\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor() {\n        this.#size = 0;\n        this.#capacity = 4;\n        this.#loadThres = 2.0 / 3.0;\n        this.#extendRatio = 2;\n        this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    #hashFunc(key: number): number {\n        return key % this.#capacity;\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    #loadFactor(): number {\n        return this.#size / this.#capacity;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    get(key: number): string | null {\n        const index = this.#hashFunc(key);\n        const bucket = this.#buckets[index];\n        // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        for (const pair of bucket) {\n            if (pair.key === key) {\n                return pair.val;\n            }\n        }\n        // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n        return null;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    put(key: number, val: string): void {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (this.#loadFactor() > this.#loadThres) {\n            this.#extend();\n        }\n        const index = this.#hashFunc(key);\n        const bucket = this.#buckets[index];\n        // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n        for (const pair of bucket) {\n            if (pair.key === key) {\n                pair.val = val;\n                return;\n            }\n        }\n        // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n        const pair = new Pair(key, val);\n        bucket.push(pair);\n        this.#size++;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    remove(key: number): void {\n        const index = this.#hashFunc(key);\n        let bucket = this.#buckets[index];\n        // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n        for (let i = 0; i < bucket.length; i++) {\n            if (bucket[i].key === key) {\n                bucket.splice(i, 1);\n                this.#size--;\n                break;\n            }\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    #extend(): void {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        const bucketsTmp = this.#buckets;\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        this.#capacity *= this.#extendRatio;\n        this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n        this.#size = 0;\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for (const bucket of bucketsTmp) {\n            for (const pair of bucket) {\n                this.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    print(): void {\n        for (const bucket of this.#buckets) {\n            let res = [];\n            for (const pair of bucket) {\n                res.push(pair.key + ' -> ' + pair.val);\n            }\n            console.log(res);\n        }\n    }\n}\n
        hash_map_chaining.dart
        /* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n  late int size; // \u952e\u503c\u5bf9\u6570\u91cf\n  late int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n  late double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n  late int extendRatio; // \u6269\u5bb9\u500d\u6570\n  late List<List<Pair>> buckets; // \u6876\u6570\u7ec4\n\n  /* \u6784\u9020\u65b9\u6cd5 */\n  HashMapChaining() {\n    size = 0;\n    capacity = 4;\n    loadThres = 2.0 / 3.0;\n    extendRatio = 2;\n    buckets = List.generate(capacity, (_) => []);\n  }\n\n  /* \u54c8\u5e0c\u51fd\u6570 */\n  int hashFunc(int key) {\n    return key % capacity;\n  }\n\n  /* \u8d1f\u8f7d\u56e0\u5b50 */\n  double loadFactor() {\n    return size / capacity;\n  }\n\n  /* \u67e5\u8be2\u64cd\u4f5c */\n  String? get(int key) {\n    int index = hashFunc(key);\n    List<Pair> bucket = buckets[index];\n    // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n    for (Pair pair in bucket) {\n      if (pair.key == key) {\n        return pair.val;\n      }\n    }\n    // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n    return null;\n  }\n\n  /* \u6dfb\u52a0\u64cd\u4f5c */\n  void put(int key, String val) {\n    // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n    if (loadFactor() > loadThres) {\n      extend();\n    }\n    int index = hashFunc(key);\n    List<Pair> bucket = buckets[index];\n    // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n    for (Pair pair in bucket) {\n      if (pair.key == key) {\n        pair.val = val;\n        return;\n      }\n    }\n    // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n    Pair pair = Pair(key, val);\n    bucket.add(pair);\n    size++;\n  }\n\n  /* \u5220\u9664\u64cd\u4f5c */\n  void remove(int key) {\n    int index = hashFunc(key);\n    List<Pair> bucket = buckets[index];\n    // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n    for (Pair pair in bucket) {\n      if (pair.key == key) {\n        bucket.remove(pair);\n        size--;\n        break;\n      }\n    }\n  }\n\n  /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n  void extend() {\n    // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n    List<List<Pair>> bucketsTmp = buckets;\n    // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n    capacity *= extendRatio;\n    buckets = List.generate(capacity, (_) => []);\n    size = 0;\n    // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n    for (List<Pair> bucket in bucketsTmp) {\n      for (Pair pair in bucket) {\n        put(pair.key, pair.val);\n      }\n    }\n  }\n\n  /* \u6253\u5370\u54c8\u5e0c\u8868 */\n  void printHashMap() {\n    for (List<Pair> bucket in buckets) {\n      List<String> res = [];\n      for (Pair pair in bucket) {\n        res.add(\"${pair.key} -> ${pair.val}\");\n      }\n      print(res);\n    }\n  }\n}\n
        hash_map_chaining.rs
        /* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nstruct HashMapChaining {\n    size: usize,\n    capacity: usize,\n    load_thres: f32,\n    extend_ratio: usize,\n    buckets: Vec<Vec<Pair>>,\n}\n\nimpl HashMapChaining {\n    /* \u6784\u9020\u65b9\u6cd5 */\n    fn new() -> Self {\n        Self {\n            size: 0,\n            capacity: 4,\n            load_thres: 2.0 / 3.0,\n            extend_ratio: 2,\n            buckets: vec![vec![]; 4],\n        }\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    fn hash_func(&self, key: i32) -> usize {\n        key as usize % self.capacity\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    fn load_factor(&self) -> f32 {\n        self.size as f32 / self.capacity as f32\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    fn remove(&mut self, key: i32) -> Option<String> {\n        let index = self.hash_func(key);\n\n        // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n        for (i, p) in self.buckets[index].iter_mut().enumerate() {\n            if p.key == key {\n                let pair = self.buckets[index].remove(i);\n                self.size -= 1;\n                return Some(pair.val);\n            }\n        }\n\n        // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de None\n        None\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    fn extend(&mut self) {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        let buckets_tmp = std::mem::take(&mut self.buckets);\n\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        self.capacity *= self.extend_ratio;\n        self.buckets = vec![Vec::new(); self.capacity as usize];\n        self.size = 0;\n\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for bucket in buckets_tmp {\n            for pair in bucket {\n                self.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    fn print(&self) {\n        for bucket in &self.buckets {\n            let mut res = Vec::new();\n            for pair in bucket {\n                res.push(format!(\"{} -> {}\", pair.key, pair.val));\n            }\n            println!(\"{:?}\", res);\n        }\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    fn put(&mut self, key: i32, val: String) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if self.load_factor() > self.load_thres {\n            self.extend();\n        }\n\n        let index = self.hash_func(key);\n\n        // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n        for pair in self.buckets[index].iter_mut() {\n            if pair.key == key {\n                pair.val = val;\n                return;\n            }\n        }\n\n        // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n        let pair = Pair { key, val };\n        self.buckets[index].push(pair);\n        self.size += 1;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    fn get(&self, key: i32) -> Option<&str> {\n        let index = self.hash_func(key);\n\n        // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        for pair in self.buckets[index].iter() {\n            if pair.key == key {\n                return Some(&pair.val);\n            }\n        }\n\n        // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de None\n        None\n    }\n}\n
        hash_map_chaining.c
        /* \u94fe\u8868\u8282\u70b9 */\ntypedef struct Node {\n    Pair *pair;\n    struct Node *next;\n} Node;\n\n/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\ntypedef struct {\n    int size;         // \u952e\u503c\u5bf9\u6570\u91cf\n    int capacity;     // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    int extendRatio;  // \u6269\u5bb9\u500d\u6570\n    Node **buckets;   // \u6876\u6570\u7ec4\n} HashMapChaining;\n\n/* \u6784\u9020\u51fd\u6570 */\nHashMapChaining *newHashMapChaining() {\n    HashMapChaining *hashMap = (HashMapChaining *)malloc(sizeof(HashMapChaining));\n    hashMap->size = 0;\n    hashMap->capacity = 4;\n    hashMap->loadThres = 2.0 / 3.0;\n    hashMap->extendRatio = 2;\n    hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));\n    for (int i = 0; i < hashMap->capacity; i++) {\n        hashMap->buckets[i] = NULL;\n    }\n    return hashMap;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delHashMapChaining(HashMapChaining *hashMap) {\n    for (int i = 0; i < hashMap->capacity; i++) {\n        Node *cur = hashMap->buckets[i];\n        while (cur) {\n            Node *tmp = cur;\n            cur = cur->next;\n            free(tmp->pair);\n            free(tmp);\n        }\n    }\n    free(hashMap->buckets);\n    free(hashMap);\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(HashMapChaining *hashMap, int key) {\n    return key % hashMap->capacity;\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor(HashMapChaining *hashMap) {\n    return (double)hashMap->size / (double)hashMap->capacity;\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nchar *get(HashMapChaining *hashMap, int key) {\n    int index = hashFunc(hashMap, key);\n    // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n    Node *cur = hashMap->buckets[index];\n    while (cur) {\n        if (cur->pair->key == key) {\n            return cur->pair->val;\n        }\n        cur = cur->next;\n    }\n    return \"\"; // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(HashMapChaining *hashMap, int key, const char *val) {\n    // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n    if (loadFactor(hashMap) > hashMap->loadThres) {\n        extend(hashMap);\n    }\n    int index = hashFunc(hashMap, key);\n    // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n    Node *cur = hashMap->buckets[index];\n    while (cur) {\n        if (cur->pair->key == key) {\n            strcpy(cur->pair->val, val); // \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n            return;\n        }\n        cur = cur->next;\n    }\n    // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n    Pair *newPair = (Pair *)malloc(sizeof(Pair));\n    newPair->key = key;\n    strcpy(newPair->val, val);\n    Node *newNode = (Node *)malloc(sizeof(Node));\n    newNode->pair = newPair;\n    newNode->next = hashMap->buckets[index];\n    hashMap->buckets[index] = newNode;\n    hashMap->size++;\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend(HashMapChaining *hashMap) {\n    // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n    int oldCapacity = hashMap->capacity;\n    Node **oldBuckets = hashMap->buckets;\n    // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n    hashMap->capacity *= hashMap->extendRatio;\n    hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));\n    for (int i = 0; i < hashMap->capacity; i++) {\n        hashMap->buckets[i] = NULL;\n    }\n    hashMap->size = 0;\n    // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n    for (int i = 0; i < oldCapacity; i++) {\n        Node *cur = oldBuckets[i];\n        while (cur) {\n            put(hashMap, cur->pair->key, cur->pair->val);\n            Node *temp = cur;\n            cur = cur->next;\n            // \u91ca\u653e\u5185\u5b58\n            free(temp->pair);\n            free(temp);\n        }\n    }\n\n    free(oldBuckets);\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeItem(HashMapChaining *hashMap, int key) {\n    int index = hashFunc(hashMap, key);\n    Node *cur = hashMap->buckets[index];\n    Node *pre = NULL;\n    while (cur) {\n        if (cur->pair->key == key) {\n            // \u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n            if (pre) {\n                pre->next = cur->next;\n            } else {\n                hashMap->buckets[index] = cur->next;\n            }\n            // \u91ca\u653e\u5185\u5b58\n            free(cur->pair);\n            free(cur);\n            hashMap->size--;\n            return;\n        }\n        pre = cur;\n        cur = cur->next;\n    }\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(HashMapChaining *hashMap) {\n    for (int i = 0; i < hashMap->capacity; i++) {\n        Node *cur = hashMap->buckets[i];\n        printf(\"[\");\n        while (cur) {\n            printf(\"%d -> %s, \", cur->pair->key, cur->pair->val);\n            cur = cur->next;\n        }\n        printf(\"]\\n\");\n    }\n}\n
        hash_map_chaining.kt
        /* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n    var size: Int // \u952e\u503c\u5bf9\u6570\u91cf\n    var capacity: Int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    val loadThres: Double // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    val extendRatio: Int // \u6269\u5bb9\u500d\u6570\n    var buckets: MutableList<MutableList<Pair>> // \u6876\u6570\u7ec4\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    init {\n        size = 0\n        capacity = 4\n        loadThres = 2.0 / 3.0\n        extendRatio = 2\n        buckets = mutableListOf()\n        for (i in 0..<capacity) {\n            buckets.add(mutableListOf())\n        }\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    fun hashFunc(key: Int): Int {\n        return key % capacity\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    fun loadFactor(): Double {\n        return (size / capacity).toDouble()\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    fun get(key: Int): String? {\n        val index = hashFunc(key)\n        val bucket = buckets[index]\n        // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        for (pair in bucket) {\n            if (pair.key == key) return pair._val\n        }\n        // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n        return null\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    fun put(key: Int, _val: String) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (loadFactor() > loadThres) {\n            extend()\n        }\n        val index = hashFunc(key)\n        val bucket = buckets[index]\n        // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n        for (pair in bucket) {\n            if (pair.key == key) {\n                pair._val = _val\n                return\n            }\n        }\n        // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n        val pair = Pair(key, _val)\n        bucket.add(pair)\n        size++\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    fun remove(key: Int) {\n        val index = hashFunc(key)\n        val bucket = buckets[index]\n        // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n        for (pair in bucket) {\n            if (pair.key == key) {\n                bucket.remove(pair)\n                size--\n                break\n            }\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    fun extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        val bucketsTmp = buckets\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        capacity *= extendRatio\n        // mutablelist \u65e0\u56fa\u5b9a\u5927\u5c0f\n        buckets = mutableListOf()\n        for (i in 0..<capacity) {\n            buckets.add(mutableListOf())\n        }\n        size = 0\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for (bucket in bucketsTmp) {\n            for (pair in bucket) {\n                put(pair.key, pair._val)\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    fun print() {\n        for (bucket in buckets) {\n            val res = mutableListOf<String>()\n            for (pair in bucket) {\n                val k = pair.key\n                val v = pair._val\n                res.add(\"$k -> $v\")\n            }\n            println(res)\n        }\n    }\n}\n
        hash_map_chaining.rb
        ### \u952e\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 ###\nclass HashMapChaining\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize\n    @size = 0 # \u952e\u503c\u5bf9\u6570\u91cf\n    @capacity = 4 # \u54c8\u5e0c\u8868\u5bb9\u91cf\n    @load_thres = 2.0 / 3.0 # \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    @extend_ratio = 2 # \u6269\u5bb9\u500d\u6570\n    @buckets = Array.new(@capacity) { [] } # \u6876\u6570\u7ec4\n  end\n\n  ### \u54c8\u5e0c\u51fd\u6570 ###\n  def hash_func(key)\n    key % @capacity\n  end\n\n  ### \u8d1f\u8f7d\u56e0\u5b50 ###\n  def load_factor\n    @size / @capacity\n  end\n\n  ### \u67e5\u8be2\u64cd\u4f5c ###\n  def get(key)\n    index = hash_func(key)\n    bucket = @buckets[index]\n    # \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n    for pair in bucket\n      return pair.val if pair.key == key\n    end\n    # \u82e5\u672a\u627e\u5230 key , \u5219\u8fd4\u56de nil\n    nil\n  end\n\n  ### \u6dfb\u52a0\u64cd\u4f5c ###\n  def put(key, val)\n    # \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n    extend if load_factor > @load_thres\n    index = hash_func(key)\n    bucket = @buckets[index]\n    # \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n    for pair in bucket\n      if pair.key == key\n        pair.val = val\n        return\n      end\n    end\n    # \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n    pair = Pair.new(key, val)\n    bucket << pair\n    @size += 1\n  end\n\n  ### \u5220\u9664\u64cd\u4f5c ###\n  def remove(key)\n    index = hash_func(key)\n    bucket = @buckets[index]\n    # \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n    for pair in bucket\n      if pair.key == key\n        bucket.delete(pair)\n        @size -= 1\n        break\n      end\n    end\n  end\n\n  ### \u6269\u5bb9\u54c8\u5e0c\u8868 ###\n  def extend\n    # \u66ab\u5b58\u539f\u54c8\u5e0c\u8868\n    buckets = @buckets\n    # \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n    @capacity *= @extend_ratio\n    @buckets = Array.new(@capacity) { [] }\n    @size = 0\n    # \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n    for bucket in buckets\n      for pair in bucket\n        put(pair.key, pair.val)\n      end\n    end\n  end\n\n  ### \u6253\u5370\u54c8\u5e0c\u8868 ###\n  def print\n    for bucket in @buckets\n      res = []\n      for pair in bucket\n        res << \"#{pair.key} -> #{pair.val}\"\n      end\n      pp res\n    end\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u94fe\u8868\u5f88\u957f\u65f6\uff0c\u67e5\u8be2\u6548\u7387 \\(O(n)\\) \u5f88\u5dee\u3002\u6b64\u65f6\u53ef\u4ee5\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u201cAVL \u6811\u201d\u6216\u201c\u7ea2\u9ed1\u6811\u201d\uff0c\u4ece\u800c\u5c06\u67e5\u8be2\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(\\log n)\\) \u3002

        "},{"location":"chapter_hashing/hash_collision/#622","title":"6.2.2 \u00a0 \u5f00\u653e\u5bfb\u5740","text":"

        \u5f00\u653e\u5bfb\u5740\uff08open addressing\uff09\u4e0d\u5f15\u5165\u989d\u5916\u7684\u6570\u636e\u7ed3\u6784\uff0c\u800c\u662f\u901a\u8fc7\u201c\u591a\u6b21\u63a2\u6d4b\u201d\u6765\u5904\u7406\u54c8\u5e0c\u51b2\u7a81\uff0c\u63a2\u6d4b\u65b9\u5f0f\u4e3b\u8981\u5305\u62ec\u7ebf\u6027\u63a2\u6d4b\u3001\u5e73\u65b9\u63a2\u6d4b\u548c\u591a\u6b21\u54c8\u5e0c\u7b49\u3002

        \u4e0b\u9762\u4ee5\u7ebf\u6027\u63a2\u6d4b\u4e3a\u4f8b\uff0c\u4ecb\u7ecd\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\u7684\u5de5\u4f5c\u673a\u5236\u3002

        "},{"location":"chapter_hashing/hash_collision/#1","title":"1. \u00a0 \u7ebf\u6027\u63a2\u6d4b","text":"

        \u7ebf\u6027\u63a2\u6d4b\u91c7\u7528\u56fa\u5b9a\u6b65\u957f\u7684\u7ebf\u6027\u641c\u7d22\u6765\u8fdb\u884c\u63a2\u6d4b\uff0c\u5176\u64cd\u4f5c\u65b9\u6cd5\u4e0e\u666e\u901a\u54c8\u5e0c\u8868\u6709\u6240\u4e0d\u540c\u3002

        • \u63d2\u5165\u5143\u7d20\uff1a\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u82e5\u53d1\u73b0\u6876\u5185\u5df2\u6709\u5143\u7d20\uff0c\u5219\u4ece\u51b2\u7a81\u4f4d\u7f6e\u5411\u540e\u7ebf\u6027\u904d\u5386\uff08\u6b65\u957f\u901a\u5e38\u4e3a \\(1\\) \uff09\uff0c\u76f4\u81f3\u627e\u5230\u7a7a\u6876\uff0c\u5c06\u5143\u7d20\u63d2\u5165\u5176\u4e2d\u3002
        • \u67e5\u627e\u5143\u7d20\uff1a\u82e5\u53d1\u73b0\u54c8\u5e0c\u51b2\u7a81\uff0c\u5219\u4f7f\u7528\u76f8\u540c\u6b65\u957f\u5411\u540e\u8fdb\u884c\u7ebf\u6027\u904d\u5386\uff0c\u76f4\u5230\u627e\u5230\u5bf9\u5e94\u5143\u7d20\uff0c\u8fd4\u56de value \u5373\u53ef\uff1b\u5982\u679c\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u76ee\u6807\u5143\u7d20\u4e0d\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u8fd4\u56de None \u3002

        \u56fe 6-6 \u5c55\u793a\u4e86\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\uff09\u54c8\u5e0c\u8868\u7684\u952e\u503c\u5bf9\u5206\u5e03\u3002\u6839\u636e\u6b64\u54c8\u5e0c\u51fd\u6570\uff0c\u6700\u540e\u4e24\u4f4d\u76f8\u540c\u7684 key \u90fd\u4f1a\u88ab\u6620\u5c04\u5230\u76f8\u540c\u7684\u6876\u3002\u800c\u901a\u8fc7\u7ebf\u6027\u63a2\u6d4b\uff0c\u5b83\u4eec\u88ab\u4f9d\u6b21\u5b58\u50a8\u5728\u8be5\u6876\u4ee5\u53ca\u4e4b\u4e0b\u7684\u6876\u4e2d\u3002

        \u56fe 6-6 \u00a0 \u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\uff09\u54c8\u5e0c\u8868\u7684\u952e\u503c\u5bf9\u5206\u5e03

        \u7136\u800c\uff0c\u7ebf\u6027\u63a2\u6d4b\u5bb9\u6613\u4ea7\u751f\u201c\u805a\u96c6\u73b0\u8c61\u201d\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u6570\u7ec4\u4e2d\u8fde\u7eed\u88ab\u5360\u7528\u7684\u4f4d\u7f6e\u8d8a\u957f\uff0c\u8fd9\u4e9b\u8fde\u7eed\u4f4d\u7f6e\u53d1\u751f\u54c8\u5e0c\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u8d8a\u5927\uff0c\u4ece\u800c\u8fdb\u4e00\u6b65\u4fc3\u4f7f\u8be5\u4f4d\u7f6e\u7684\u805a\u5806\u751f\u957f\uff0c\u5f62\u6210\u6076\u6027\u5faa\u73af\uff0c\u6700\u7ec8\u5bfc\u81f4\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u6548\u7387\u52a3\u5316\u3002

        \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6211\u4eec\u4e0d\u80fd\u5728\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\u4e2d\u76f4\u63a5\u5220\u9664\u5143\u7d20\u3002\u8fd9\u662f\u56e0\u4e3a\u5220\u9664\u5143\u7d20\u4f1a\u5728\u6570\u7ec4\u5185\u4ea7\u751f\u4e00\u4e2a\u7a7a\u6876 None \uff0c\u800c\u5f53\u67e5\u8be2\u5143\u7d20\u65f6\uff0c\u7ebf\u6027\u63a2\u6d4b\u5230\u8be5\u7a7a\u6876\u5c31\u4f1a\u8fd4\u56de\uff0c\u56e0\u6b64\u5728\u8be5\u7a7a\u6876\u4e4b\u4e0b\u7684\u5143\u7d20\u90fd\u65e0\u6cd5\u518d\u88ab\u8bbf\u95ee\u5230\uff0c\u7a0b\u5e8f\u53ef\u80fd\u8bef\u5224\u8fd9\u4e9b\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u5982\u56fe 6-7 \u6240\u793a\u3002

        \u56fe 6-7 \u00a0 \u5728\u5f00\u653e\u5bfb\u5740\u4e2d\u5220\u9664\u5143\u7d20\u5bfc\u81f4\u7684\u67e5\u8be2\u95ee\u9898

        \u4e3a\u4e86\u89e3\u51b3\u8be5\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u7528\u61d2\u5220\u9664\uff08lazy deletion\uff09\u673a\u5236\uff1a\u5b83\u4e0d\u76f4\u63a5\u4ece\u54c8\u5e0c\u8868\u4e2d\u79fb\u9664\u5143\u7d20\uff0c\u800c\u662f\u5229\u7528\u4e00\u4e2a\u5e38\u91cf TOMBSTONE \u6765\u6807\u8bb0\u8fd9\u4e2a\u6876\u3002\u5728\u8be5\u673a\u5236\u4e0b\uff0cNone \u548c TOMBSTONE \u90fd\u4ee3\u8868\u7a7a\u6876\uff0c\u90fd\u53ef\u4ee5\u653e\u7f6e\u952e\u503c\u5bf9\u3002\u4f46\u4e0d\u540c\u7684\u662f\uff0c\u7ebf\u6027\u63a2\u6d4b\u5230 TOMBSTONE \u65f6\u5e94\u8be5\u7ee7\u7eed\u904d\u5386\uff0c\u56e0\u4e3a\u5176\u4e4b\u4e0b\u53ef\u80fd\u8fd8\u5b58\u5728\u952e\u503c\u5bf9\u3002

        \u7136\u800c\uff0c\u61d2\u5220\u9664\u53ef\u80fd\u4f1a\u52a0\u901f\u54c8\u5e0c\u8868\u7684\u6027\u80fd\u9000\u5316\u3002\u8fd9\u662f\u56e0\u4e3a\u6bcf\u6b21\u5220\u9664\u64cd\u4f5c\u90fd\u4f1a\u4ea7\u751f\u4e00\u4e2a\u5220\u9664\u6807\u8bb0\uff0c\u968f\u7740 TOMBSTONE \u7684\u589e\u52a0\uff0c\u641c\u7d22\u65f6\u95f4\u4e5f\u4f1a\u589e\u52a0\uff0c\u56e0\u4e3a\u7ebf\u6027\u63a2\u6d4b\u53ef\u80fd\u9700\u8981\u8df3\u8fc7\u591a\u4e2a TOMBSTONE \u624d\u80fd\u627e\u5230\u76ee\u6807\u5143\u7d20\u3002

        \u4e3a\u6b64\uff0c\u8003\u8651\u5728\u7ebf\u6027\u63a2\u6d4b\u4e2d\u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a TOMBSTONE \u7684\u7d22\u5f15\uff0c\u5e76\u5c06\u641c\u7d22\u5230\u7684\u76ee\u6807\u5143\u7d20\u4e0e\u8be5 TOMBSTONE \u4ea4\u6362\u4f4d\u7f6e\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u5f53\u6bcf\u6b21\u67e5\u8be2\u6216\u6dfb\u52a0\u5143\u7d20\u65f6\uff0c\u5143\u7d20\u4f1a\u88ab\u79fb\u52a8\u81f3\u8ddd\u79bb\u7406\u60f3\u4f4d\u7f6e\uff08\u63a2\u6d4b\u8d77\u59cb\u70b9\uff09\u66f4\u8fd1\u7684\u6876\uff0c\u4ece\u800c\u4f18\u5316\u67e5\u8be2\u6548\u7387\u3002

        \u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u5305\u542b\u61d2\u5220\u9664\u7684\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\uff09\u54c8\u5e0c\u8868\u3002\u4e3a\u4e86\u66f4\u52a0\u5145\u5206\u5730\u4f7f\u7528\u54c8\u5e0c\u8868\u7684\u7a7a\u95f4\uff0c\u6211\u4eec\u5c06\u54c8\u5e0c\u8868\u770b\u4f5c\u4e00\u4e2a\u201c\u73af\u5f62\u6570\u7ec4\u201d\uff0c\u5f53\u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u56de\u5230\u5934\u90e8\u7ee7\u7eed\u904d\u5386\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map_open_addressing.py
        class HashMapOpenAddressing:\n    \"\"\"\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\"\"\"\n\n    def __init__(self):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        self.size = 0  # \u952e\u503c\u5bf9\u6570\u91cf\n        self.capacity = 4  # \u54c8\u5e0c\u8868\u5bb9\u91cf\n        self.load_thres = 2.0 / 3.0  # \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n        self.extend_ratio = 2  # \u6269\u5bb9\u500d\u6570\n        self.buckets: list[Pair | None] = [None] * self.capacity  # \u6876\u6570\u7ec4\n        self.TOMBSTONE = Pair(-1, \"-1\")  # \u5220\u9664\u6807\u8bb0\n\n    def hash_func(self, key: int) -> int:\n        \"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\n        return key % self.capacity\n\n    def load_factor(self) -> float:\n        \"\"\"\u8d1f\u8f7d\u56e0\u5b50\"\"\"\n        return self.size / self.capacity\n\n    def find_bucket(self, key: int) -> int:\n        \"\"\"\u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\"\"\"\n        index = self.hash_func(key)\n        first_tombstone = -1\n        # \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n        while self.buckets[index] is not None:\n            # \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n            if self.buckets[index].key == key:\n                # \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n                if first_tombstone != -1:\n                    self.buckets[first_tombstone] = self.buckets[index]\n                    self.buckets[index] = self.TOMBSTONE\n                    return first_tombstone  # \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n                return index  # \u8fd4\u56de\u6876\u7d22\u5f15\n            # \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n            if first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE:\n                first_tombstone = index\n            # \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n            index = (index + 1) % self.capacity\n        # \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n        return index if first_tombstone == -1 else first_tombstone\n\n    def get(self, key: int) -> str:\n        \"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\n        # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        index = self.find_bucket(key)\n        # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        if self.buckets[index] not in [None, self.TOMBSTONE]:\n            return self.buckets[index].val\n        # \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de None\n        return None\n\n    def put(self, key: int, val: str):\n        \"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"\n        # \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if self.load_factor() > self.load_thres:\n            self.extend()\n        # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        index = self.find_bucket(key)\n        # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n        if self.buckets[index] not in [None, self.TOMBSTONE]:\n            self.buckets[index].val = val\n            return\n        # \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n        self.buckets[index] = Pair(key, val)\n        self.size += 1\n\n    def remove(self, key: int):\n        \"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\n        # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        index = self.find_bucket(key)\n        # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n        if self.buckets[index] not in [None, self.TOMBSTONE]:\n            self.buckets[index] = self.TOMBSTONE\n            self.size -= 1\n\n    def extend(self):\n        \"\"\"\u6269\u5bb9\u54c8\u5e0c\u8868\"\"\"\n        # \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        buckets_tmp = self.buckets\n        # \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        self.capacity *= self.extend_ratio\n        self.buckets = [None] * self.capacity\n        self.size = 0\n        # \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for pair in buckets_tmp:\n            if pair not in [None, self.TOMBSTONE]:\n                self.put(pair.key, pair.val)\n\n    def print(self):\n        \"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\n        for pair in self.buckets:\n            if pair is None:\n                print(\"None\")\n            elif pair is self.TOMBSTONE:\n                print(\"TOMBSTONE\")\n            else:\n                print(pair.key, \"->\", pair.val)\n
        hash_map_open_addressing.cpp
        /* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n  private:\n    int size;                             // \u952e\u503c\u5bf9\u6570\u91cf\n    int capacity = 4;                     // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    const double loadThres = 2.0 / 3.0;     // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    const int extendRatio = 2;            // \u6269\u5bb9\u500d\u6570\n    vector<Pair *> buckets;               // \u6876\u6570\u7ec4\n    Pair *TOMBSTONE = new Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n  public:\n    /* \u6784\u9020\u65b9\u6cd5 */\n    HashMapOpenAddressing() : size(0), buckets(capacity, nullptr) {\n    }\n\n    /* \u6790\u6784\u65b9\u6cd5 */\n    ~HashMapOpenAddressing() {\n        for (Pair *pair : buckets) {\n            if (pair != nullptr && pair != TOMBSTONE) {\n                delete pair;\n            }\n        }\n        delete TOMBSTONE;\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    double loadFactor() {\n        return (double)size / capacity;\n    }\n\n    /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n    int findBucket(int key) {\n        int index = hashFunc(key);\n        int firstTombstone = -1;\n        // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n        while (buckets[index] != nullptr) {\n            // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n            if (buckets[index]->key == key) {\n                // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index];\n                    buckets[index] = TOMBSTONE;\n                    return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n                }\n                return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n            }\n            // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index;\n            }\n            // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n            index = (index + 1) % capacity;\n        }\n        // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n        return firstTombstone == -1 ? index : firstTombstone;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    string get(int key) {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        int index = findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n            return buckets[index]->val;\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n        return \"\";\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    void put(int key, string val) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        int index = findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n        if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n            buckets[index]->val = val;\n            return;\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n        buckets[index] = new Pair(key, val);\n        size++;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    void remove(int key) {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        int index = findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n        if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n            delete buckets[index];\n            buckets[index] = TOMBSTONE;\n            size--;\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    void extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        vector<Pair *> bucketsTmp = buckets;\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        capacity *= extendRatio;\n        buckets = vector<Pair *>(capacity, nullptr);\n        size = 0;\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for (Pair *pair : bucketsTmp) {\n            if (pair != nullptr && pair != TOMBSTONE) {\n                put(pair->key, pair->val);\n                delete pair;\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    void print() {\n        for (Pair *pair : buckets) {\n            if (pair == nullptr) {\n                cout << \"nullptr\" << endl;\n            } else if (pair == TOMBSTONE) {\n                cout << \"TOMBSTONE\" << endl;\n            } else {\n                cout << pair->key << \" -> \" << pair->val << endl;\n            }\n        }\n    }\n};\n
        hash_map_open_addressing.java
        /* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n    private int size; // \u952e\u503c\u5bf9\u6570\u91cf\n    private int capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    private final double loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    private final int extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n    private Pair[] buckets; // \u6876\u6570\u7ec4\n    private final Pair TOMBSTONE = new Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public HashMapOpenAddressing() {\n        size = 0;\n        buckets = new Pair[capacity];\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    private int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    private double loadFactor() {\n        return (double) size / capacity;\n    }\n\n    /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n    private int findBucket(int key) {\n        int index = hashFunc(key);\n        int firstTombstone = -1;\n        // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n        while (buckets[index] != null) {\n            // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n            if (buckets[index].key == key) {\n                // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index];\n                    buckets[index] = TOMBSTONE;\n                    return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n                }\n                return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n            }\n            // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index;\n            }\n            // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n            index = (index + 1) % capacity;\n        }\n        // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n        return firstTombstone == -1 ? index : firstTombstone;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    public String get(int key) {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        int index = findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            return buckets[index].val;\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n        return null;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    public void put(int key, String val) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        int index = findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index].val = val;\n            return;\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n        buckets[index] = new Pair(key, val);\n        size++;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    public void remove(int key) {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        int index = findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index] = TOMBSTONE;\n            size--;\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    private void extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        Pair[] bucketsTmp = buckets;\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        capacity *= extendRatio;\n        buckets = new Pair[capacity];\n        size = 0;\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for (Pair pair : bucketsTmp) {\n            if (pair != null && pair != TOMBSTONE) {\n                put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    public void print() {\n        for (Pair pair : buckets) {\n            if (pair == null) {\n                System.out.println(\"null\");\n            } else if (pair == TOMBSTONE) {\n                System.out.println(\"TOMBSTONE\");\n            } else {\n                System.out.println(pair.key + \" -> \" + pair.val);\n            }\n        }\n    }\n}\n
        hash_map_open_addressing.cs
        /* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n    int size; // \u952e\u503c\u5bf9\u6570\u91cf\n    int capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    double loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    int extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n    Pair[] buckets; // \u6876\u6570\u7ec4\n    Pair TOMBSTONE = new(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public HashMapOpenAddressing() {\n        size = 0;\n        buckets = new Pair[capacity];\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    int HashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    double LoadFactor() {\n        return (double)size / capacity;\n    }\n\n    /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n    int FindBucket(int key) {\n        int index = HashFunc(key);\n        int firstTombstone = -1;\n        // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n        while (buckets[index] != null) {\n            // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n            if (buckets[index].key == key) {\n                // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index];\n                    buckets[index] = TOMBSTONE;\n                    return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n                }\n                return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n            }\n            // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index;\n            }\n            // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n            index = (index + 1) % capacity;\n        }\n        // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n        return firstTombstone == -1 ? index : firstTombstone;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    public string? Get(int key) {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        int index = FindBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            return buckets[index].val;\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n        return null;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    public void Put(int key, string val) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (LoadFactor() > loadThres) {\n            Extend();\n        }\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        int index = FindBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index].val = val;\n            return;\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n        buckets[index] = new Pair(key, val);\n        size++;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    public void Remove(int key) {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        int index = FindBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index] = TOMBSTONE;\n            size--;\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    void Extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        Pair[] bucketsTmp = buckets;\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        capacity *= extendRatio;\n        buckets = new Pair[capacity];\n        size = 0;\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        foreach (Pair pair in bucketsTmp) {\n            if (pair != null && pair != TOMBSTONE) {\n                Put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    public void Print() {\n        foreach (Pair pair in buckets) {\n            if (pair == null) {\n                Console.WriteLine(\"null\");\n            } else if (pair == TOMBSTONE) {\n                Console.WriteLine(\"TOMBSTONE\");\n            } else {\n                Console.WriteLine(pair.key + \" -> \" + pair.val);\n            }\n        }\n    }\n}\n
        hash_map_open_addressing.go
        /* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\ntype hashMapOpenAddressing struct {\n    size        int     // \u952e\u503c\u5bf9\u6570\u91cf\n    capacity    int     // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    loadThres   float64 // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    extendRatio int     // \u6269\u5bb9\u500d\u6570\n    buckets     []*pair // \u6876\u6570\u7ec4\n    TOMBSTONE   *pair   // \u5220\u9664\u6807\u8bb0\n}\n\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newHashMapOpenAddressing() *hashMapOpenAddressing {\n    return &hashMapOpenAddressing{\n        size:        0,\n        capacity:    4,\n        loadThres:   2.0 / 3.0,\n        extendRatio: 2,\n        buckets:     make([]*pair, 4),\n        TOMBSTONE:   &pair{-1, \"-1\"},\n    }\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (h *hashMapOpenAddressing) hashFunc(key int) int {\n    return key % h.capacity // \u6839\u636e\u952e\u8ba1\u7b97\u54c8\u5e0c\u503c\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc (h *hashMapOpenAddressing) loadFactor() float64 {\n    return float64(h.size) / float64(h.capacity) // \u8ba1\u7b97\u5f53\u524d\u8d1f\u8f7d\u56e0\u5b50\n}\n\n/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nfunc (h *hashMapOpenAddressing) findBucket(key int) int {\n    index := h.hashFunc(key) // \u83b7\u53d6\u521d\u59cb\u7d22\u5f15\n    firstTombstone := -1     // \u8bb0\u5f55\u9047\u5230\u7684\u7b2c\u4e00\u4e2aTOMBSTONE\u7684\u4f4d\u7f6e\n    for h.buckets[index] != nil {\n        if h.buckets[index].key == key {\n            if firstTombstone != -1 {\n                // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n                h.buckets[firstTombstone] = h.buckets[index]\n                h.buckets[index] = h.TOMBSTONE\n                return firstTombstone // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n            }\n            return index // \u8fd4\u56de\u627e\u5230\u7684\u7d22\u5f15\n        }\n        if firstTombstone == -1 && h.buckets[index] == h.TOMBSTONE {\n            firstTombstone = index // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\u7684\u4f4d\u7f6e\n        }\n        index = (index + 1) % h.capacity // \u7ebf\u6027\u63a2\u6d4b\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n    }\n    // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n    if firstTombstone != -1 {\n        return firstTombstone\n    }\n    return index\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (h *hashMapOpenAddressing) get(key int) string {\n    index := h.findBucket(key) // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    if h.buckets[index] != nil && h.buckets[index] != h.TOMBSTONE {\n        return h.buckets[index].val // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n    }\n    return \"\" // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de \"\"\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (h *hashMapOpenAddressing) put(key int, val string) {\n    if h.loadFactor() > h.loadThres {\n        h.extend() // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n    }\n    index := h.findBucket(key) // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    if h.buckets[index] == nil || h.buckets[index] == h.TOMBSTONE {\n        h.buckets[index] = &pair{key, val} // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n        h.size++\n    } else {\n        h.buckets[index].val = val // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val\n    }\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (h *hashMapOpenAddressing) remove(key int) {\n    index := h.findBucket(key) // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    if h.buckets[index] != nil && h.buckets[index] != h.TOMBSTONE {\n        h.buckets[index] = h.TOMBSTONE // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n        h.size--\n    }\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc (h *hashMapOpenAddressing) extend() {\n    oldBuckets := h.buckets               // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n    h.capacity *= h.extendRatio           // \u66f4\u65b0\u5bb9\u91cf\n    h.buckets = make([]*pair, h.capacity) // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n    h.size = 0                            // \u91cd\u7f6e\u5927\u5c0f\n    // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n    for _, pair := range oldBuckets {\n        if pair != nil && pair != h.TOMBSTONE {\n            h.put(pair.key, pair.val)\n        }\n    }\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (h *hashMapOpenAddressing) print() {\n    for _, pair := range h.buckets {\n        if pair == nil {\n            fmt.Println(\"nil\")\n        } else if pair == h.TOMBSTONE {\n            fmt.Println(\"TOMBSTONE\")\n        } else {\n            fmt.Printf(\"%d -> %s\\n\", pair.key, pair.val)\n        }\n    }\n}\n
        hash_map_open_addressing.swift
        /* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n    var size: Int // \u952e\u503c\u5bf9\u6570\u91cf\n    var capacity: Int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    var loadThres: Double // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    var extendRatio: Int // \u6269\u5bb9\u500d\u6570\n    var buckets: [Pair?] // \u6876\u6570\u7ec4\n    var TOMBSTONE: Pair // \u5220\u9664\u6807\u8bb0\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    init() {\n        size = 0\n        capacity = 4\n        loadThres = 2.0 / 3.0\n        extendRatio = 2\n        buckets = Array(repeating: nil, count: capacity)\n        TOMBSTONE = Pair(key: -1, val: \"-1\")\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    func hashFunc(key: Int) -> Int {\n        key % capacity\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    func loadFactor() -> Double {\n        Double(size) / Double(capacity)\n    }\n\n    /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n    func findBucket(key: Int) -> Int {\n        var index = hashFunc(key: key)\n        var firstTombstone = -1\n        // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n        while buckets[index] != nil {\n            // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n            if buckets[index]!.key == key {\n                // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n                if firstTombstone != -1 {\n                    buckets[firstTombstone] = buckets[index]\n                    buckets[index] = TOMBSTONE\n                    return firstTombstone // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n                }\n                return index // \u8fd4\u56de\u6876\u7d22\u5f15\n            }\n            // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n            if firstTombstone == -1 && buckets[index] == TOMBSTONE {\n                firstTombstone = index\n            }\n            // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n            index = (index + 1) % capacity\n        }\n        // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n        return firstTombstone == -1 ? index : firstTombstone\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    func get(key: Int) -> String? {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        let index = findBucket(key: key)\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        if buckets[index] != nil, buckets[index] != TOMBSTONE {\n            return buckets[index]!.val\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n        return nil\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    func put(key: Int, val: String) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if loadFactor() > loadThres {\n            extend()\n        }\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        let index = findBucket(key: key)\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n        if buckets[index] != nil, buckets[index] != TOMBSTONE {\n            buckets[index]!.val = val\n            return\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n        buckets[index] = Pair(key: key, val: val)\n        size += 1\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    func remove(key: Int) {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        let index = findBucket(key: key)\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n        if buckets[index] != nil, buckets[index] != TOMBSTONE {\n            buckets[index] = TOMBSTONE\n            size -= 1\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    func extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        let bucketsTmp = buckets\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        capacity *= extendRatio\n        buckets = Array(repeating: nil, count: capacity)\n        size = 0\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for pair in bucketsTmp {\n            if let pair, pair != TOMBSTONE {\n                put(key: pair.key, val: pair.val)\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    func print() {\n        for pair in buckets {\n            if pair == nil {\n                Swift.print(\"null\")\n            } else if pair == TOMBSTONE {\n                Swift.print(\"TOMBSTONE\")\n            } else {\n                Swift.print(\"\\(pair!.key) -> \\(pair!.val)\")\n            }\n        }\n    }\n}\n
        hash_map_open_addressing.js
        /* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n    #size; // \u952e\u503c\u5bf9\u6570\u91cf\n    #capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    #loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    #extendRatio; // \u6269\u5bb9\u500d\u6570\n    #buckets; // \u6876\u6570\u7ec4\n    #TOMBSTONE; // \u5220\u9664\u6807\u8bb0\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor() {\n        this.#size = 0; // \u952e\u503c\u5bf9\u6570\u91cf\n        this.#capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n        this.#loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n        this.#extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n        this.#buckets = Array(this.#capacity).fill(null); // \u6876\u6570\u7ec4\n        this.#TOMBSTONE = new Pair(-1, '-1'); // \u5220\u9664\u6807\u8bb0\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    #hashFunc(key) {\n        return key % this.#capacity;\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    #loadFactor() {\n        return this.#size / this.#capacity;\n    }\n\n    /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n    #findBucket(key) {\n        let index = this.#hashFunc(key);\n        let firstTombstone = -1;\n        // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n        while (this.#buckets[index] !== null) {\n            // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n            if (this.#buckets[index].key === key) {\n                // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n                if (firstTombstone !== -1) {\n                    this.#buckets[firstTombstone] = this.#buckets[index];\n                    this.#buckets[index] = this.#TOMBSTONE;\n                    return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n                }\n                return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n            }\n            // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n            if (\n                firstTombstone === -1 &&\n                this.#buckets[index] === this.#TOMBSTONE\n            ) {\n                firstTombstone = index;\n            }\n            // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n            index = (index + 1) % this.#capacity;\n        }\n        // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n        return firstTombstone === -1 ? index : firstTombstone;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    get(key) {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        const index = this.#findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        if (\n            this.#buckets[index] !== null &&\n            this.#buckets[index] !== this.#TOMBSTONE\n        ) {\n            return this.#buckets[index].val;\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n        return null;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    put(key, val) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (this.#loadFactor() > this.#loadThres) {\n            this.#extend();\n        }\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        const index = this.#findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n        if (\n            this.#buckets[index] !== null &&\n            this.#buckets[index] !== this.#TOMBSTONE\n        ) {\n            this.#buckets[index].val = val;\n            return;\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n        this.#buckets[index] = new Pair(key, val);\n        this.#size++;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    remove(key) {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        const index = this.#findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n        if (\n            this.#buckets[index] !== null &&\n            this.#buckets[index] !== this.#TOMBSTONE\n        ) {\n            this.#buckets[index] = this.#TOMBSTONE;\n            this.#size--;\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    #extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        const bucketsTmp = this.#buckets;\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        this.#capacity *= this.#extendRatio;\n        this.#buckets = Array(this.#capacity).fill(null);\n        this.#size = 0;\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for (const pair of bucketsTmp) {\n            if (pair !== null && pair !== this.#TOMBSTONE) {\n                this.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    print() {\n        for (const pair of this.#buckets) {\n            if (pair === null) {\n                console.log('null');\n            } else if (pair === this.#TOMBSTONE) {\n                console.log('TOMBSTONE');\n            } else {\n                console.log(pair.key + ' -> ' + pair.val);\n            }\n        }\n    }\n}\n
        hash_map_open_addressing.ts
        /* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n    private size: number; // \u952e\u503c\u5bf9\u6570\u91cf\n    private capacity: number; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    private loadThres: number; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    private extendRatio: number; // \u6269\u5bb9\u500d\u6570\n    private buckets: Array<Pair | null>; // \u6876\u6570\u7ec4\n    private TOMBSTONE: Pair; // \u5220\u9664\u6807\u8bb0\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor() {\n        this.size = 0; // \u952e\u503c\u5bf9\u6570\u91cf\n        this.capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n        this.loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n        this.extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n        this.buckets = Array(this.capacity).fill(null); // \u6876\u6570\u7ec4\n        this.TOMBSTONE = new Pair(-1, '-1'); // \u5220\u9664\u6807\u8bb0\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    private hashFunc(key: number): number {\n        return key % this.capacity;\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    private loadFactor(): number {\n        return this.size / this.capacity;\n    }\n\n    /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n    private findBucket(key: number): number {\n        let index = this.hashFunc(key);\n        let firstTombstone = -1;\n        // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n        while (this.buckets[index] !== null) {\n            // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n            if (this.buckets[index]!.key === key) {\n                // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n                if (firstTombstone !== -1) {\n                    this.buckets[firstTombstone] = this.buckets[index];\n                    this.buckets[index] = this.TOMBSTONE;\n                    return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n                }\n                return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n            }\n            // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n            if (\n                firstTombstone === -1 &&\n                this.buckets[index] === this.TOMBSTONE\n            ) {\n                firstTombstone = index;\n            }\n            // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n            index = (index + 1) % this.capacity;\n        }\n        // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n        return firstTombstone === -1 ? index : firstTombstone;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    get(key: number): string | null {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        const index = this.findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        if (\n            this.buckets[index] !== null &&\n            this.buckets[index] !== this.TOMBSTONE\n        ) {\n            return this.buckets[index]!.val;\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n        return null;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    put(key: number, val: string): void {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (this.loadFactor() > this.loadThres) {\n            this.extend();\n        }\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        const index = this.findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n        if (\n            this.buckets[index] !== null &&\n            this.buckets[index] !== this.TOMBSTONE\n        ) {\n            this.buckets[index]!.val = val;\n            return;\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n        this.buckets[index] = new Pair(key, val);\n        this.size++;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    remove(key: number): void {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        const index = this.findBucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n        if (\n            this.buckets[index] !== null &&\n            this.buckets[index] !== this.TOMBSTONE\n        ) {\n            this.buckets[index] = this.TOMBSTONE;\n            this.size--;\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    private extend(): void {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        const bucketsTmp = this.buckets;\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        this.capacity *= this.extendRatio;\n        this.buckets = Array(this.capacity).fill(null);\n        this.size = 0;\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for (const pair of bucketsTmp) {\n            if (pair !== null && pair !== this.TOMBSTONE) {\n                this.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    print(): void {\n        for (const pair of this.buckets) {\n            if (pair === null) {\n                console.log('null');\n            } else if (pair === this.TOMBSTONE) {\n                console.log('TOMBSTONE');\n            } else {\n                console.log(pair.key + ' -> ' + pair.val);\n            }\n        }\n    }\n}\n
        hash_map_open_addressing.dart
        /* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n  late int _size; // \u952e\u503c\u5bf9\u6570\u91cf\n  int _capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n  double _loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n  int _extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n  late List<Pair?> _buckets; // \u6876\u6570\u7ec4\n  Pair _TOMBSTONE = Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n  /* \u6784\u9020\u65b9\u6cd5 */\n  HashMapOpenAddressing() {\n    _size = 0;\n    _buckets = List.generate(_capacity, (index) => null);\n  }\n\n  /* \u54c8\u5e0c\u51fd\u6570 */\n  int hashFunc(int key) {\n    return key % _capacity;\n  }\n\n  /* \u8d1f\u8f7d\u56e0\u5b50 */\n  double loadFactor() {\n    return _size / _capacity;\n  }\n\n  /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n  int findBucket(int key) {\n    int index = hashFunc(key);\n    int firstTombstone = -1;\n    // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n    while (_buckets[index] != null) {\n      // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n      if (_buckets[index]!.key == key) {\n        // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n        if (firstTombstone != -1) {\n          _buckets[firstTombstone] = _buckets[index];\n          _buckets[index] = _TOMBSTONE;\n          return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n        }\n        return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n      }\n      // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n      if (firstTombstone == -1 && _buckets[index] == _TOMBSTONE) {\n        firstTombstone = index;\n      }\n      // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n      index = (index + 1) % _capacity;\n    }\n    // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n    return firstTombstone == -1 ? index : firstTombstone;\n  }\n\n  /* \u67e5\u8be2\u64cd\u4f5c */\n  String? get(int key) {\n    // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    int index = findBucket(key);\n    // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n    if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n      return _buckets[index]!.val;\n    }\n    // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n    return null;\n  }\n\n  /* \u6dfb\u52a0\u64cd\u4f5c */\n  void put(int key, String val) {\n    // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n    if (loadFactor() > _loadThres) {\n      extend();\n    }\n    // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    int index = findBucket(key);\n    // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n    if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n      _buckets[index]!.val = val;\n      return;\n    }\n    // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n    _buckets[index] = new Pair(key, val);\n    _size++;\n  }\n\n  /* \u5220\u9664\u64cd\u4f5c */\n  void remove(int key) {\n    // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    int index = findBucket(key);\n    // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n    if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n      _buckets[index] = _TOMBSTONE;\n      _size--;\n    }\n  }\n\n  /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n  void extend() {\n    // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n    List<Pair?> bucketsTmp = _buckets;\n    // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n    _capacity *= _extendRatio;\n    _buckets = List.generate(_capacity, (index) => null);\n    _size = 0;\n    // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n    for (Pair? pair in bucketsTmp) {\n      if (pair != null && pair != _TOMBSTONE) {\n        put(pair.key, pair.val);\n      }\n    }\n  }\n\n  /* \u6253\u5370\u54c8\u5e0c\u8868 */\n  void printHashMap() {\n    for (Pair? pair in _buckets) {\n      if (pair == null) {\n        print(\"null\");\n      } else if (pair == _TOMBSTONE) {\n        print(\"TOMBSTONE\");\n      } else {\n        print(\"${pair.key} -> ${pair.val}\");\n      }\n    }\n  }\n}\n
        hash_map_open_addressing.rs
        /* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nstruct HashMapOpenAddressing {\n    size: usize,                // \u952e\u503c\u5bf9\u6570\u91cf\n    capacity: usize,            // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    load_thres: f64,            // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    extend_ratio: usize,        // \u6269\u5bb9\u500d\u6570\n    buckets: Vec<Option<Pair>>, // \u6876\u6570\u7ec4\n    TOMBSTONE: Option<Pair>,    // \u5220\u9664\u6807\u8bb0\n}\n\nimpl HashMapOpenAddressing {\n    /* \u6784\u9020\u65b9\u6cd5 */\n    fn new() -> Self {\n        Self {\n            size: 0,\n            capacity: 4,\n            load_thres: 2.0 / 3.0,\n            extend_ratio: 2,\n            buckets: vec![None; 4],\n            TOMBSTONE: Some(Pair {\n                key: -1,\n                val: \"-1\".to_string(),\n            }),\n        }\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    fn hash_func(&self, key: i32) -> usize {\n        (key % self.capacity as i32) as usize\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    fn load_factor(&self) -> f64 {\n        self.size as f64 / self.capacity as f64\n    }\n\n    /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n    fn find_bucket(&mut self, key: i32) -> usize {\n        let mut index = self.hash_func(key);\n        let mut first_tombstone = -1;\n        // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n        while self.buckets[index].is_some() {\n            // \u82e5\u9047\u5230 key\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n            if self.buckets[index].as_ref().unwrap().key == key {\n                // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u5efa\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n                if first_tombstone != -1 {\n                    self.buckets[first_tombstone as usize] = self.buckets[index].take();\n                    self.buckets[index] = self.TOMBSTONE.clone();\n                    return first_tombstone as usize; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n                }\n                return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n            }\n            // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n            if first_tombstone == -1 && self.buckets[index] == self.TOMBSTONE {\n                first_tombstone = index as i32;\n            }\n            // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n            index = (index + 1) % self.capacity;\n        }\n        // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n        if first_tombstone == -1 {\n            index\n        } else {\n            first_tombstone as usize\n        }\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    fn get(&mut self, key: i32) -> Option<&str> {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        let index = self.find_bucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n            return self.buckets[index].as_ref().map(|pair| &pair.val as &str);\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n        None\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    fn put(&mut self, key: i32, val: String) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if self.load_factor() > self.load_thres {\n            self.extend();\n        }\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        let index = self.find_bucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n        if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n            self.buckets[index].as_mut().unwrap().val = val;\n            return;\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n        self.buckets[index] = Some(Pair { key, val });\n        self.size += 1;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    fn remove(&mut self, key: i32) {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        let index = self.find_bucket(key);\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n        if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n            self.buckets[index] = self.TOMBSTONE.clone();\n            self.size -= 1;\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    fn extend(&mut self) {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        let buckets_tmp = self.buckets.clone();\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        self.capacity *= self.extend_ratio;\n        self.buckets = vec![None; self.capacity];\n        self.size = 0;\n\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for pair in buckets_tmp {\n            if pair.is_none() || pair == self.TOMBSTONE {\n                continue;\n            }\n            let pair = pair.unwrap();\n\n            self.put(pair.key, pair.val);\n        }\n    }\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    fn print(&self) {\n        for pair in &self.buckets {\n            if pair.is_none() {\n                println!(\"null\");\n            } else if pair == &self.TOMBSTONE {\n                println!(\"TOMBSTONE\");\n            } else {\n                let pair = pair.as_ref().unwrap();\n                println!(\"{} -> {}\", pair.key, pair.val);\n            }\n        }\n    }\n}\n
        hash_map_open_addressing.c
        /* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\ntypedef struct {\n    int size;         // \u952e\u503c\u5bf9\u6570\u91cf\n    int capacity;     // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    int extendRatio;  // \u6269\u5bb9\u500d\u6570\n    Pair **buckets;   // \u6876\u6570\u7ec4\n    Pair *TOMBSTONE;  // \u5220\u9664\u6807\u8bb0\n} HashMapOpenAddressing;\n\n/* \u6784\u9020\u51fd\u6570 */\nHashMapOpenAddressing *newHashMapOpenAddressing() {\n    HashMapOpenAddressing *hashMap = (HashMapOpenAddressing *)malloc(sizeof(HashMapOpenAddressing));\n    hashMap->size = 0;\n    hashMap->capacity = 4;\n    hashMap->loadThres = 2.0 / 3.0;\n    hashMap->extendRatio = 2;\n    hashMap->buckets = (Pair **)calloc(hashMap->capacity, sizeof(Pair *));\n    hashMap->TOMBSTONE = (Pair *)malloc(sizeof(Pair));\n    hashMap->TOMBSTONE->key = -1;\n    hashMap->TOMBSTONE->val = \"-1\";\n\n    return hashMap;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delHashMapOpenAddressing(HashMapOpenAddressing *hashMap) {\n    for (int i = 0; i < hashMap->capacity; i++) {\n        Pair *pair = hashMap->buckets[i];\n        if (pair != NULL && pair != hashMap->TOMBSTONE) {\n            free(pair->val);\n            free(pair);\n        }\n    }\n    free(hashMap->buckets);\n    free(hashMap->TOMBSTONE);\n    free(hashMap);\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(HashMapOpenAddressing *hashMap, int key) {\n    return key % hashMap->capacity;\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor(HashMapOpenAddressing *hashMap) {\n    return (double)hashMap->size / (double)hashMap->capacity;\n}\n\n/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nint findBucket(HashMapOpenAddressing *hashMap, int key) {\n    int index = hashFunc(hashMap, key);\n    int firstTombstone = -1;\n    // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n    while (hashMap->buckets[index] != NULL) {\n        // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        if (hashMap->buckets[index]->key == key) {\n            // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n            if (firstTombstone != -1) {\n                hashMap->buckets[firstTombstone] = hashMap->buckets[index];\n                hashMap->buckets[index] = hashMap->TOMBSTONE;\n                return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n            }\n            return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n        }\n        // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n        if (firstTombstone == -1 && hashMap->buckets[index] == hashMap->TOMBSTONE) {\n            firstTombstone = index;\n        }\n        // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n        index = (index + 1) % hashMap->capacity;\n    }\n    // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n    return firstTombstone == -1 ? index : firstTombstone;\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nchar *get(HashMapOpenAddressing *hashMap, int key) {\n    // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    int index = findBucket(hashMap, key);\n    // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n    if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n        return hashMap->buckets[index]->val;\n    }\n    // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n    return \"\";\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(HashMapOpenAddressing *hashMap, int key, char *val) {\n    // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n    if (loadFactor(hashMap) > hashMap->loadThres) {\n        extend(hashMap);\n    }\n    // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    int index = findBucket(hashMap, key);\n    // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n    if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n        free(hashMap->buckets[index]->val);\n        hashMap->buckets[index]->val = (char *)malloc(sizeof(strlen(val) + 1));\n        strcpy(hashMap->buckets[index]->val, val);\n        hashMap->buckets[index]->val[strlen(val)] = '\\0';\n        return;\n    }\n    // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n    Pair *pair = (Pair *)malloc(sizeof(Pair));\n    pair->key = key;\n    pair->val = (char *)malloc(sizeof(strlen(val) + 1));\n    strcpy(pair->val, val);\n    pair->val[strlen(val)] = '\\0';\n\n    hashMap->buckets[index] = pair;\n    hashMap->size++;\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeItem(HashMapOpenAddressing *hashMap, int key) {\n    // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    int index = findBucket(hashMap, key);\n    // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n    if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n        Pair *pair = hashMap->buckets[index];\n        free(pair->val);\n        free(pair);\n        hashMap->buckets[index] = hashMap->TOMBSTONE;\n        hashMap->size--;\n    }\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend(HashMapOpenAddressing *hashMap) {\n    // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n    Pair **bucketsTmp = hashMap->buckets;\n    int oldCapacity = hashMap->capacity;\n    // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n    hashMap->capacity *= hashMap->extendRatio;\n    hashMap->buckets = (Pair **)calloc(hashMap->capacity, sizeof(Pair *));\n    hashMap->size = 0;\n    // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n    for (int i = 0; i < oldCapacity; i++) {\n        Pair *pair = bucketsTmp[i];\n        if (pair != NULL && pair != hashMap->TOMBSTONE) {\n            put(hashMap, pair->key, pair->val);\n            free(pair->val);\n            free(pair);\n        }\n    }\n    free(bucketsTmp);\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(HashMapOpenAddressing *hashMap) {\n    for (int i = 0; i < hashMap->capacity; i++) {\n        Pair *pair = hashMap->buckets[i];\n        if (pair == NULL) {\n            printf(\"NULL\\n\");\n        } else if (pair == hashMap->TOMBSTONE) {\n            printf(\"TOMBSTONE\\n\");\n        } else {\n            printf(\"%d -> %s\\n\", pair->key, pair->val);\n        }\n    }\n}\n
        hash_map_open_addressing.kt
        /* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n    private var size: Int               // \u952e\u503c\u5bf9\u6570\u91cf\n    private var capacity: Int           // \u54c8\u5e0c\u8868\u5bb9\u91cf\n    private val loadThres: Double       // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    private val extendRatio: Int        // \u6269\u5bb9\u500d\u6570\n    private var buckets: Array<Pair?>   // \u6876\u6570\u7ec4\n    private val TOMBSTONE: Pair         // \u5220\u9664\u6807\u8bb0\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    init {\n        size = 0\n        capacity = 4\n        loadThres = 2.0 / 3.0\n        extendRatio = 2\n        buckets = arrayOfNulls(capacity)\n        TOMBSTONE = Pair(-1, \"-1\")\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    fun hashFunc(key: Int): Int {\n        return key % capacity\n    }\n\n    /* \u8d1f\u8f7d\u56e0\u5b50 */\n    fun loadFactor(): Double {\n        return (size / capacity).toDouble()\n    }\n\n    /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n    fun findBucket(key: Int): Int {\n        var index = hashFunc(key)\n        var firstTombstone = -1\n        // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n        while (buckets[index] != null) {\n            // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n            if (buckets[index]?.key == key) {\n                // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index]\n                    buckets[index] = TOMBSTONE\n                    return firstTombstone // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n                }\n                return index // \u8fd4\u56de\u6876\u7d22\u5f15\n            }\n            // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index\n            }\n            // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n            index = (index + 1) % capacity\n        }\n        // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n        return if (firstTombstone == -1) index else firstTombstone\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    fun get(key: Int): String? {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        val index = findBucket(key)\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            return buckets[index]?._val\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n        return null\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    fun put(key: Int, _val: String) {\n        // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n        if (loadFactor() > loadThres) {\n            extend()\n        }\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        val index = findBucket(key)\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index]!!._val = _val\n            return\n        }\n        // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n        buckets[index] = Pair(key, _val)\n        size++\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    fun remove(key: Int) {\n        // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n        val index = findBucket(key)\n        // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index] = TOMBSTONE\n            size--\n        }\n    }\n\n    /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n    fun extend() {\n        // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n        val bucketsTmp = buckets\n        // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n        capacity *= extendRatio\n        buckets = arrayOfNulls(capacity)\n        size = 0\n        // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n        for (pair in bucketsTmp) {\n            if (pair != null && pair != TOMBSTONE) {\n                put(pair.key, pair._val)\n            }\n        }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    fun print() {\n        for (pair in buckets) {\n            if (pair == null) {\n                println(\"null\")\n            } else if (pair == TOMBSTONE) {\n                println(\"TOMESTOME\")\n            } else {\n                println(\"${pair.key} -> ${pair._val}\")\n            }\n        }\n    }\n}\n
        hash_map_open_addressing.rb
        ### \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 ###\nclass HashMapOpenAddressing\n  TOMBSTONE = Pair.new(-1, '-1') # \u5220\u9664\u6807\u8bb0\n\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize\n    @size = 0 # \u952e\u503c\u5bf9\u6570\u91cf\n    @capacity = 4 # \u54c8\u5e0c\u8868\u5bb9\u91cf\n    @load_thres = 2.0 / 3.0 # \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n    @extend_ratio = 2 # \u6269\u5bb9\u500d\u6570\n    @buckets = Array.new(@capacity) # \u6876\u6570\u7ec4\n  end\n\n  ### \u54c8\u5e0c\u51fd\u6570 ###\n  def hash_func(key)\n    key % @capacity\n  end\n\n  ### \u8d1f\u8f7d\u56e0\u5b50 ###\n  def load_factor\n    @size / @capacity\n  end\n\n  ### \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 ###\n  def find_bucket(key)\n    index = hash_func(key)\n    first_tombstone = -1\n    # \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n    while !@buckets[index].nil?\n      # \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n      if @buckets[index].key == key\n        # \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n        if first_tombstone != -1\n          @buckets[first_tombstone] = @buckets[index]\n          @buckets[index] = TOMBSTONE\n          return first_tombstone # \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n        end\n        return index # \u8fd4\u56de\u6876\u7d22\u5f15\n      end\n      # \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n      first_tombstone = index if first_tombstone == -1 && @buckets[index] == TOMBSTONE\n      # \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n      index = (index + 1) % @capacity\n    end\n    # \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n    first_tombstone == -1 ? index : first_tombstone\n  end\n\n  ### \u67e5\u8be2\u64cd\u4f5c ###\n  def get(key)\n    # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    index = find_bucket(key)\n    # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n    return @buckets[index].val unless [nil, TOMBSTONE].include?(@buckets[index])\n    # \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de nil\n    nil\n  end\n\n  ### \u6dfb\u52a0\u64cd\u4f5c ###\n  def put(key, val)\n    # \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n    extend if load_factor > @load_thres\n    # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    index = find_bucket(key)\n    # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5f00\u8fd4\u56de\n    unless [nil, TOMBSTONE].include?(@buckets[index])\n      @buckets[index].val = val\n      return\n    end\n    # \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n    @buckets[index] = Pair.new(key, val)\n    @size += 1\n  end\n\n  ### \u5220\u9664\u64cd\u4f5c ###\n  def remove(key)\n    # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n    index = find_bucket(key)\n    # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n    unless [nil, TOMBSTONE].include?(@buckets[index])\n      @buckets[index] = TOMBSTONE\n      @size -= 1\n    end\n  end\n\n  ### \u6269\u5bb9\u54c8\u5e0c\u8868 ###\n  def extend\n    # \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n    buckets_tmp = @buckets\n    # \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n    @capacity *= @extend_ratio\n    @buckets = Array.new(@capacity)\n    @size = 0\n    # \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n    for pair in buckets_tmp\n      put(pair.key, pair.val) unless [nil, TOMBSTONE].include?(pair)\n    end\n  end\n\n  ### \u6253\u5370\u54c8\u5e0c\u8868 ###\n  def print\n    for pair in @buckets\n      if pair.nil?\n        puts \"Nil\"\n      elsif pair == TOMBSTONE\n        puts \"TOMBSTONE\"\n      else\n        puts \"#{pair.key} -> #{pair.val}\"\n      end\n    end\n  end\nend\n
        "},{"location":"chapter_hashing/hash_collision/#2","title":"2. \u00a0 \u5e73\u65b9\u63a2\u6d4b","text":"

        \u5e73\u65b9\u63a2\u6d4b\u4e0e\u7ebf\u6027\u63a2\u6d4b\u7c7b\u4f3c\uff0c\u90fd\u662f\u5f00\u653e\u5bfb\u5740\u7684\u5e38\u89c1\u7b56\u7565\u4e4b\u4e00\u3002\u5f53\u53d1\u751f\u51b2\u7a81\u65f6\uff0c\u5e73\u65b9\u63a2\u6d4b\u4e0d\u662f\u7b80\u5355\u5730\u8df3\u8fc7\u4e00\u4e2a\u56fa\u5b9a\u7684\u6b65\u6570\uff0c\u800c\u662f\u8df3\u8fc7\u201c\u63a2\u6d4b\u6b21\u6570\u7684\u5e73\u65b9\u201d\u7684\u6b65\u6570\uff0c\u5373 \\(1, 4, 9, \\dots\\) \u6b65\u3002

        \u5e73\u65b9\u63a2\u6d4b\u4e3b\u8981\u5177\u6709\u4ee5\u4e0b\u4f18\u52bf\u3002

        • \u5e73\u65b9\u63a2\u6d4b\u901a\u8fc7\u8df3\u8fc7\u63a2\u6d4b\u6b21\u6570\u5e73\u65b9\u7684\u8ddd\u79bb\uff0c\u8bd5\u56fe\u7f13\u89e3\u7ebf\u6027\u63a2\u6d4b\u7684\u805a\u96c6\u6548\u5e94\u3002
        • \u5e73\u65b9\u63a2\u6d4b\u4f1a\u8df3\u8fc7\u66f4\u5927\u7684\u8ddd\u79bb\u6765\u5bfb\u627e\u7a7a\u4f4d\u7f6e\uff0c\u6709\u52a9\u4e8e\u6570\u636e\u5206\u5e03\u5f97\u66f4\u52a0\u5747\u5300\u3002

        \u7136\u800c\uff0c\u5e73\u65b9\u63a2\u6d4b\u5e76\u4e0d\u662f\u5b8c\u7f8e\u7684\u3002

        • \u4ecd\u7136\u5b58\u5728\u805a\u96c6\u73b0\u8c61\uff0c\u5373\u67d0\u4e9b\u4f4d\u7f6e\u6bd4\u5176\u4ed6\u4f4d\u7f6e\u66f4\u5bb9\u6613\u88ab\u5360\u7528\u3002
        • \u7531\u4e8e\u5e73\u65b9\u7684\u589e\u957f\uff0c\u5e73\u65b9\u63a2\u6d4b\u53ef\u80fd\u4e0d\u4f1a\u63a2\u6d4b\u6574\u4e2a\u54c8\u5e0c\u8868\uff0c\u8fd9\u610f\u5473\u7740\u5373\u4f7f\u54c8\u5e0c\u8868\u4e2d\u6709\u7a7a\u6876\uff0c\u5e73\u65b9\u63a2\u6d4b\u4e5f\u53ef\u80fd\u65e0\u6cd5\u8bbf\u95ee\u5230\u5b83\u3002
        "},{"location":"chapter_hashing/hash_collision/#3","title":"3. \u00a0 \u591a\u6b21\u54c8\u5e0c","text":"

        \u987e\u540d\u601d\u4e49\uff0c\u591a\u6b21\u54c8\u5e0c\u65b9\u6cd5\u4f7f\u7528\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570 \\(f_1(x)\\)\u3001\\(f_2(x)\\)\u3001\\(f_3(x)\\)\u3001\\(\\dots\\) \u8fdb\u884c\u63a2\u6d4b\u3002

        • \u63d2\u5165\u5143\u7d20\uff1a\u82e5\u54c8\u5e0c\u51fd\u6570 \\(f_1(x)\\) \u51fa\u73b0\u51b2\u7a81\uff0c\u5219\u5c1d\u8bd5 \\(f_2(x)\\) \uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u5230\u627e\u5230\u7a7a\u4f4d\u540e\u63d2\u5165\u5143\u7d20\u3002
        • \u67e5\u627e\u5143\u7d20\uff1a\u5728\u76f8\u540c\u7684\u54c8\u5e0c\u51fd\u6570\u987a\u5e8f\u4e0b\u8fdb\u884c\u67e5\u627e\uff0c\u76f4\u5230\u627e\u5230\u76ee\u6807\u5143\u7d20\u65f6\u8fd4\u56de\uff1b\u82e5\u9047\u5230\u7a7a\u4f4d\u6216\u5df2\u5c1d\u8bd5\u6240\u6709\u54c8\u5e0c\u51fd\u6570\uff0c\u8bf4\u660e\u54c8\u5e0c\u8868\u4e2d\u4e0d\u5b58\u5728\u8be5\u5143\u7d20\uff0c\u5219\u8fd4\u56de None \u3002

        \u4e0e\u7ebf\u6027\u63a2\u6d4b\u76f8\u6bd4\uff0c\u591a\u6b21\u54c8\u5e0c\u65b9\u6cd5\u4e0d\u6613\u4ea7\u751f\u805a\u96c6\uff0c\u4f46\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570\u4f1a\u5e26\u6765\u989d\u5916\u7684\u8ba1\u7b97\u91cf\u3002

        Tip

        \u8bf7\u6ce8\u610f\uff0c\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\u3001\u5e73\u65b9\u63a2\u6d4b\u548c\u591a\u6b21\u54c8\u5e0c\uff09\u54c8\u5e0c\u8868\u90fd\u5b58\u5728\u201c\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u201d\u7684\u95ee\u9898\u3002

        "},{"location":"chapter_hashing/hash_collision/#623","title":"6.2.3 \u00a0 \u7f16\u7a0b\u8bed\u8a00\u7684\u9009\u62e9","text":"

        \u5404\u79cd\u7f16\u7a0b\u8bed\u8a00\u91c7\u53d6\u4e86\u4e0d\u540c\u7684\u54c8\u5e0c\u8868\u5b9e\u73b0\u7b56\u7565\uff0c\u4e0b\u9762\u4e3e\u51e0\u4e2a\u4f8b\u5b50\u3002

        • Python \u91c7\u7528\u5f00\u653e\u5bfb\u5740\u3002\u5b57\u5178 dict \u4f7f\u7528\u4f2a\u968f\u673a\u6570\u8fdb\u884c\u63a2\u6d4b\u3002
        • Java \u91c7\u7528\u94fe\u5f0f\u5730\u5740\u3002\u81ea JDK 1.8 \u4ee5\u6765\uff0c\u5f53 HashMap \u5185\u6570\u7ec4\u957f\u5ea6\u8fbe\u5230 64 \u4e14\u94fe\u8868\u957f\u5ea6\u8fbe\u5230 8 \u65f6\uff0c\u94fe\u8868\u4f1a\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811\u4ee5\u63d0\u5347\u67e5\u627e\u6027\u80fd\u3002
        • Go \u91c7\u7528\u94fe\u5f0f\u5730\u5740\u3002Go \u89c4\u5b9a\u6bcf\u4e2a\u6876\u6700\u591a\u5b58\u50a8 8 \u4e2a\u952e\u503c\u5bf9\uff0c\u8d85\u51fa\u5bb9\u91cf\u5219\u8fde\u63a5\u4e00\u4e2a\u6ea2\u51fa\u6876\uff1b\u5f53\u6ea2\u51fa\u6876\u8fc7\u591a\u65f6\uff0c\u4f1a\u6267\u884c\u4e00\u6b21\u7279\u6b8a\u7684\u7b49\u91cf\u6269\u5bb9\u64cd\u4f5c\uff0c\u4ee5\u786e\u4fdd\u6027\u80fd\u3002
        "},{"location":"chapter_hashing/hash_map/","title":"6.1 \u00a0 \u54c8\u5e0c\u8868","text":"

        \u54c8\u5e0c\u8868\uff08hash table\uff09\uff0c\u53c8\u79f0\u6563\u5217\u8868\uff0c\u5b83\u901a\u8fc7\u5efa\u7acb\u952e key \u4e0e\u503c value \u4e4b\u95f4\u7684\u6620\u5c04\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u5143\u7d20\u67e5\u8be2\u3002\u5177\u4f53\u800c\u8a00\uff0c\u6211\u4eec\u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u4e00\u4e2a\u952e key \uff0c\u5219\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u83b7\u53d6\u5bf9\u5e94\u7684\u503c value \u3002

        \u5982\u56fe 6-1 \u6240\u793a\uff0c\u7ed9\u5b9a \\(n\\) \u4e2a\u5b66\u751f\uff0c\u6bcf\u4e2a\u5b66\u751f\u90fd\u6709\u201c\u59d3\u540d\u201d\u548c\u201c\u5b66\u53f7\u201d\u4e24\u9879\u6570\u636e\u3002\u5047\u5982\u6211\u4eec\u5e0c\u671b\u5b9e\u73b0\u201c\u8f93\u5165\u4e00\u4e2a\u5b66\u53f7\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u59d3\u540d\u201d\u7684\u67e5\u8be2\u529f\u80fd\uff0c\u5219\u53ef\u4ee5\u91c7\u7528\u56fe 6-1 \u6240\u793a\u7684\u54c8\u5e0c\u8868\u6765\u5b9e\u73b0\u3002

        \u56fe 6-1 \u00a0 \u54c8\u5e0c\u8868\u7684\u62bd\u8c61\u8868\u793a

        \u9664\u54c8\u5e0c\u8868\u5916\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u67e5\u8be2\u529f\u80fd\uff0c\u5b83\u4eec\u7684\u6548\u7387\u5bf9\u6bd4\u5982\u8868 6-1 \u6240\u793a\u3002

        • \u6dfb\u52a0\u5143\u7d20\uff1a\u4ec5\u9700\u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u6570\u7ec4\uff08\u94fe\u8868\uff09\u7684\u5c3e\u90e8\u5373\u53ef\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002
        • \u67e5\u8be2\u5143\u7d20\uff1a\u7531\u4e8e\u6570\u7ec4\uff08\u94fe\u8868\uff09\u662f\u4e71\u5e8f\u7684\uff0c\u56e0\u6b64\u9700\u8981\u904d\u5386\u5176\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002
        • \u5220\u9664\u5143\u7d20\uff1a\u9700\u8981\u5148\u67e5\u8be2\u5230\u5143\u7d20\uff0c\u518d\u4ece\u6570\u7ec4\uff08\u94fe\u8868\uff09\u4e2d\u5220\u9664\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002

        \u8868 6-1 \u00a0 \u5143\u7d20\u67e5\u8be2\u6548\u7387\u5bf9\u6bd4

        \u6570\u7ec4 \u94fe\u8868 \u54c8\u5e0c\u8868 \u67e5\u627e\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(1)\\) \u6dfb\u52a0\u5143\u7d20 \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(1)\\)

        \u89c2\u5bdf\u53d1\u73b0\uff0c\u5728\u54c8\u5e0c\u8868\u4e2d\u8fdb\u884c\u589e\u5220\u67e5\u6539\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f \\(O(1)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002

        "},{"location":"chapter_hashing/hash_map/#611","title":"6.1.1 \u00a0 \u54c8\u5e0c\u8868\u5e38\u7528\u64cd\u4f5c","text":"

        \u54c8\u5e0c\u8868\u7684\u5e38\u89c1\u64cd\u4f5c\u5305\u62ec\uff1a\u521d\u59cb\u5316\u3001\u67e5\u8be2\u64cd\u4f5c\u3001\u6dfb\u52a0\u952e\u503c\u5bf9\u548c\u5220\u9664\u952e\u503c\u5bf9\u7b49\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map.py
        # \u521d\u59cb\u5316\u54c8\u5e0c\u8868\nhmap: dict = {}\n\n# \u6dfb\u52a0\u64cd\u4f5c\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u5570\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9e2d\"\n\n# \u67e5\u8be2\u64cd\u4f5c\n# \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nname: str = hmap[15937]\n\n# \u5220\u9664\u64cd\u4f5c\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nhmap.pop(10583)\n
        hash_map.cpp
        /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nunordered_map<int, string> map;\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap[12836] = \"\u5c0f\u54c8\";\nmap[15937] = \"\u5c0f\u5570\";\nmap[16750] = \"\u5c0f\u7b97\";\nmap[13276] = \"\u5c0f\u6cd5\";\nmap[10583] = \"\u5c0f\u9e2d\";\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nstring name = map[15937];\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.erase(10583);\n
        hash_map.java
        /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nMap<Integer, String> map = new HashMap<>();\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.put(12836, \"\u5c0f\u54c8\");\nmap.put(15937, \"\u5c0f\u5570\");\nmap.put(16750, \"\u5c0f\u7b97\");\nmap.put(13276, \"\u5c0f\u6cd5\");\nmap.put(10583, \"\u5c0f\u9e2d\");\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nString name = map.get(15937);\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.remove(10583);\n
        hash_map.cs
        /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nDictionary<int, string> map = new() {\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    // \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\n    { 12836, \"\u5c0f\u54c8\" },\n    { 15937, \"\u5c0f\u5570\" },\n    { 16750, \"\u5c0f\u7b97\" },\n    { 13276, \"\u5c0f\u6cd5\" },\n    { 10583, \"\u5c0f\u9e2d\" }\n};\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nstring name = map[15937];\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.Remove(10583);\n
        hash_map_test.go
        /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nhmap := make(map[int]string)\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u5570\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9e2d\"\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nname := hmap[15937]\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\ndelete(hmap, 10583)\n
        hash_map.swift
        /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nvar map: [Int: String] = [:]\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap[12836] = \"\u5c0f\u54c8\"\nmap[15937] = \"\u5c0f\u5570\"\nmap[16750] = \"\u5c0f\u7b97\"\nmap[13276] = \"\u5c0f\u6cd5\"\nmap[10583] = \"\u5c0f\u9e2d\"\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet name = map[15937]!\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.removeValue(forKey: 10583)\n
        hash_map.js
        /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nconst map = new Map();\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.set(12836, '\u5c0f\u54c8');\nmap.set(15937, '\u5c0f\u5570');\nmap.set(16750, '\u5c0f\u7b97');\nmap.set(13276, '\u5c0f\u6cd5');\nmap.set(10583, '\u5c0f\u9e2d');\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet name = map.get(15937);\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.delete(10583);\n
        hash_map.ts
        /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nconst map = new Map<number, string>();\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.set(12836, '\u5c0f\u54c8');\nmap.set(15937, '\u5c0f\u5570');\nmap.set(16750, '\u5c0f\u7b97');\nmap.set(13276, '\u5c0f\u6cd5');\nmap.set(10583, '\u5c0f\u9e2d');\nconsole.info('\\n\u6dfb\u52a0\u5b8c\u6210\u540e\uff0c\u54c8\u5e0c\u8868\u4e3a\\nKey -> Value');\nconsole.info(map);\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet name = map.get(15937);\nconsole.info('\\n\u8f93\u5165\u5b66\u53f7 15937 \uff0c\u67e5\u8be2\u5230\u59d3\u540d ' + name);\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.delete(10583);\nconsole.info('\\n\u5220\u9664 10583 \u540e\uff0c\u54c8\u5e0c\u8868\u4e3a\\nKey -> Value');\nconsole.info(map);\n
        hash_map.dart
        /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nMap<int, String> map = {};\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap[12836] = \"\u5c0f\u54c8\";\nmap[15937] = \"\u5c0f\u5570\";\nmap[16750] = \"\u5c0f\u7b97\";\nmap[13276] = \"\u5c0f\u6cd5\";\nmap[10583] = \"\u5c0f\u9e2d\";\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nString name = map[15937];\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.remove(10583);\n
        hash_map.rs
        use std::collections::HashMap;\n\n/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nlet mut map: HashMap<i32, String> = HashMap::new();\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.insert(12836, \"\u5c0f\u54c8\".to_string());\nmap.insert(15937, \"\u5c0f\u5570\".to_string());\nmap.insert(16750, \"\u5c0f\u7b97\".to_string());\nmap.insert(13279, \"\u5c0f\u6cd5\".to_string());\nmap.insert(10583, \"\u5c0f\u9e2d\".to_string());\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet _name: Option<&String> = map.get(&15937);\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nlet _removed_value: Option<String> = map.remove(&10583);\n
        hash_map.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u8868\n
        hash_map.kt
        /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nval map = HashMap<Int,String>()\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap[12836] = \"\u5c0f\u54c8\"\nmap[15937] = \"\u5c0f\u5570\"\nmap[16750] = \"\u5c0f\u7b97\"\nmap[13276] = \"\u5c0f\u6cd5\"\nmap[10583] = \"\u5c0f\u9e2d\"\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nval name = map[15937]\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.remove(10583)\n
        hash_map.rb
        # \u521d\u59cb\u5316\u54c8\u5e0c\u8868\nhmap = {}\n\n# \u6dfb\u52a0\u64cd\u4f5c\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u5570\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9e2d\"\n\n# \u67e5\u8be2\u64cd\u4f5c\n# \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nname = hmap[15937]\n\n# \u5220\u9664\u64cd\u4f5c\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nhmap.delete(10583)\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u54c8\u5e0c\u8868\u6709\u4e09\u79cd\u5e38\u7528\u7684\u904d\u5386\u65b9\u5f0f\uff1a\u904d\u5386\u952e\u503c\u5bf9\u3001\u904d\u5386\u952e\u548c\u904d\u5386\u503c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map.py
        # \u904d\u5386\u54c8\u5e0c\u8868\n# \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor key, value in hmap.items():\n    print(key, \"->\", value)\n# \u5355\u72ec\u904d\u5386\u952e key\nfor key in hmap.keys():\n    print(key)\n# \u5355\u72ec\u904d\u5386\u503c value\nfor value in hmap.values():\n    print(value)\n
        hash_map.cpp
        /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor (auto kv: map) {\n    cout << kv.first << \" -> \" << kv.second << endl;\n}\n// \u4f7f\u7528\u8fed\u4ee3\u5668\u904d\u5386 key->value\nfor (auto iter = map.begin(); iter != map.end(); iter++) {\n    cout << iter->first << \"->\" << iter->second << endl;\n}\n
        hash_map.java
        /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor (Map.Entry <Integer, String> kv: map.entrySet()) {\n    System.out.println(kv.getKey() + \" -> \" + kv.getValue());\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor (int key: map.keySet()) {\n    System.out.println(key);\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor (String val: map.values()) {\n    System.out.println(val);\n}\n
        hash_map.cs
        /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nforeach (var kv in map) {\n    Console.WriteLine(kv.Key + \" -> \" + kv.Value);\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nforeach (int key in map.Keys) {\n    Console.WriteLine(key);\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nforeach (string val in map.Values) {\n    Console.WriteLine(val);\n}\n
        hash_map_test.go
        /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor key, value := range hmap {\n    fmt.Println(key, \"->\", value)\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor key := range hmap {\n    fmt.Println(key)\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor _, value := range hmap {\n    fmt.Println(value)\n}\n
        hash_map.swift
        /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nfor (key, value) in map {\n    print(\"\\(key) -> \\(value)\")\n}\n// \u5355\u72ec\u904d\u5386\u952e Key\nfor key in map.keys {\n    print(key)\n}\n// \u5355\u72ec\u904d\u5386\u503c Value\nfor value in map.values {\n    print(value)\n}\n
        hash_map.js
        /* \u904d\u5386\u54c8\u5e0c\u8868 */\nconsole.info('\\n\u904d\u5386\u952e\u503c\u5bf9 Key->Value');\nfor (const [k, v] of map.entries()) {\n    console.info(k + ' -> ' + v);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u952e Key');\nfor (const k of map.keys()) {\n    console.info(k);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u503c Value');\nfor (const v of map.values()) {\n    console.info(v);\n}\n
        hash_map.ts
        /* \u904d\u5386\u54c8\u5e0c\u8868 */\nconsole.info('\\n\u904d\u5386\u952e\u503c\u5bf9 Key->Value');\nfor (const [k, v] of map.entries()) {\n    console.info(k + ' -> ' + v);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u952e Key');\nfor (const k of map.keys()) {\n    console.info(k);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u503c Value');\nfor (const v of map.values()) {\n    console.info(v);\n}\n
        hash_map.dart
        /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nmap.forEach((key, value) {\n  print('$key -> $value');\n});\n\n// \u5355\u72ec\u904d\u5386\u952e Key\nmap.keys.forEach((key) {\n  print(key);\n});\n\n// \u5355\u72ec\u904d\u5386\u503c Value\nmap.values.forEach((value) {\n  print(value);\n});\n
        hash_map.rs
        /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nfor (key, value) in &map {\n    println!(\"{key} -> {value}\");\n}\n\n// \u5355\u72ec\u904d\u5386\u952e Key\nfor key in map.keys() {\n    println!(\"{key}\");\n}\n\n// \u5355\u72ec\u904d\u5386\u503c Value\nfor value in map.values() {\n    println!(\"{value}\");\n}\n
        hash_map.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u8868\n
        hash_map.kt
        /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor ((key, value) in map) {\n    println(\"$key -> $value\")\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor (key in map.keys) {\n    println(key)\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor (_val in map.values) {\n    println(_val)\n}\n
        hash_map.rb
        # \u904d\u5386\u54c8\u5e0c\u8868\n# \u904d\u5386\u952e\u503c\u5bf9 key->value\nhmap.entries.each { |key, value| puts \"#{key} -> #{value}\" }\n\n# \u5355\u72ec\u904d\u5386\u952e key\nhmap.keys.each { |key| puts key }\n\n# \u5355\u72ec\u904d\u5386\u503c value\nhmap.values.each { |val| puts val }\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_hashing/hash_map/#612","title":"6.1.2 \u00a0 \u54c8\u5e0c\u8868\u7b80\u5355\u5b9e\u73b0","text":"

        \u6211\u4eec\u5148\u8003\u8651\u6700\u7b80\u5355\u7684\u60c5\u51b5\uff0c\u4ec5\u7528\u4e00\u4e2a\u6570\u7ec4\u6765\u5b9e\u73b0\u54c8\u5e0c\u8868\u3002\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u6211\u4eec\u5c06\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u7a7a\u4f4d\u79f0\u4e3a\u6876\uff08bucket\uff09\uff0c\u6bcf\u4e2a\u6876\u53ef\u5b58\u50a8\u4e00\u4e2a\u952e\u503c\u5bf9\u3002\u56e0\u6b64\uff0c\u67e5\u8be2\u64cd\u4f5c\u5c31\u662f\u627e\u5230 key \u5bf9\u5e94\u7684\u6876\uff0c\u5e76\u5728\u6876\u4e2d\u83b7\u53d6 value \u3002

        \u90a3\u4e48\uff0c\u5982\u4f55\u57fa\u4e8e key \u5b9a\u4f4d\u5bf9\u5e94\u7684\u6876\u5462\uff1f\u8fd9\u662f\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\uff08hash function\uff09\u5b9e\u73b0\u7684\u3002\u54c8\u5e0c\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5c06\u4e00\u4e2a\u8f83\u5927\u7684\u8f93\u5165\u7a7a\u95f4\u6620\u5c04\u5230\u4e00\u4e2a\u8f83\u5c0f\u7684\u8f93\u51fa\u7a7a\u95f4\u3002\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u8f93\u5165\u7a7a\u95f4\u662f\u6240\u6709 key \uff0c\u8f93\u51fa\u7a7a\u95f4\u662f\u6240\u6709\u6876\uff08\u6570\u7ec4\u7d22\u5f15\uff09\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u8f93\u5165\u4e00\u4e2a key \uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u5f97\u5230\u8be5 key \u5bf9\u5e94\u7684\u952e\u503c\u5bf9\u5728\u6570\u7ec4\u4e2d\u7684\u5b58\u50a8\u4f4d\u7f6e\u3002

        \u8f93\u5165\u4e00\u4e2a key \uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u8ba1\u7b97\u8fc7\u7a0b\u5206\u4e3a\u4ee5\u4e0b\u4e24\u6b65\u3002

        1. \u901a\u8fc7\u67d0\u79cd\u54c8\u5e0c\u7b97\u6cd5 hash() \u8ba1\u7b97\u5f97\u5230\u54c8\u5e0c\u503c\u3002
        2. \u5c06\u54c8\u5e0c\u503c\u5bf9\u6876\u6570\u91cf\uff08\u6570\u7ec4\u957f\u5ea6\uff09capacity \u53d6\u6a21\uff0c\u4ece\u800c\u83b7\u53d6\u8be5 key \u5bf9\u5e94\u7684\u6876\uff08\u6570\u7ec4\u7d22\u5f15\uff09index \u3002
        index = hash(key) % capacity\n

        \u968f\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5229\u7528 index \u5728\u54c8\u5e0c\u8868\u4e2d\u8bbf\u95ee\u5bf9\u5e94\u7684\u6876\uff0c\u4ece\u800c\u83b7\u53d6 value \u3002

        \u8bbe\u6570\u7ec4\u957f\u5ea6 capacity = 100\u3001\u54c8\u5e0c\u7b97\u6cd5 hash(key) = key \uff0c\u6613\u5f97\u54c8\u5e0c\u51fd\u6570\u4e3a key % 100 \u3002\u56fe 6-2 \u4ee5 key \u5b66\u53f7\u548c value \u59d3\u540d\u4e3a\u4f8b\uff0c\u5c55\u793a\u4e86\u54c8\u5e0c\u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406\u3002

        \u56fe 6-2 \u00a0 \u54c8\u5e0c\u51fd\u6570\u5de5\u4f5c\u539f\u7406

        \u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u54c8\u5e0c\u8868\u3002\u5176\u4e2d\uff0c\u6211\u4eec\u5c06 key \u548c value \u5c01\u88c5\u6210\u4e00\u4e2a\u7c7b Pair \uff0c\u4ee5\u8868\u793a\u952e\u503c\u5bf9\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_hash_map.py
        class Pair:\n    \"\"\"\u952e\u503c\u5bf9\"\"\"\n\n    def __init__(self, key: int, val: str):\n        self.key = key\n        self.val = val\n\nclass ArrayHashMap:\n    \"\"\"\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\"\"\"\n\n    def __init__(self):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        # \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n        self.buckets: list[Pair | None] = [None] * 100\n\n    def hash_func(self, key: int) -> int:\n        \"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\n        index = key % 100\n        return index\n\n    def get(self, key: int) -> str | None:\n        \"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\n        index: int = self.hash_func(key)\n        pair: Pair = self.buckets[index]\n        if pair is None:\n            return None\n        return pair.val\n\n    def put(self, key: int, val: str):\n        \"\"\"\u6dfb\u52a0\u548c\u66f4\u65b0\u64cd\u4f5c\"\"\"\n        pair = Pair(key, val)\n        index: int = self.hash_func(key)\n        self.buckets[index] = pair\n\n    def remove(self, key: int):\n        \"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\n        index: int = self.hash_func(key)\n        # \u7f6e\u4e3a None \uff0c\u4ee3\u8868\u5220\u9664\n        self.buckets[index] = None\n\n    def entry_set(self) -> list[Pair]:\n        \"\"\"\u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9\"\"\"\n        result: list[Pair] = []\n        for pair in self.buckets:\n            if pair is not None:\n                result.append(pair)\n        return result\n\n    def key_set(self) -> list[int]:\n        \"\"\"\u83b7\u53d6\u6240\u6709\u952e\"\"\"\n        result = []\n        for pair in self.buckets:\n            if pair is not None:\n                result.append(pair.key)\n        return result\n\n    def value_set(self) -> list[str]:\n        \"\"\"\u83b7\u53d6\u6240\u6709\u503c\"\"\"\n        result = []\n        for pair in self.buckets:\n            if pair is not None:\n                result.append(pair.val)\n        return result\n\n    def print(self):\n        \"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\n        for pair in self.buckets:\n            if pair is not None:\n                print(pair.key, \"->\", pair.val)\n
        array_hash_map.cpp
        /* \u952e\u503c\u5bf9 */\nstruct Pair {\n  public:\n    int key;\n    string val;\n    Pair(int key, string val) {\n        this->key = key;\n        this->val = val;\n    }\n};\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n  private:\n    vector<Pair *> buckets;\n\n  public:\n    ArrayHashMap() {\n        // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n        buckets = vector<Pair *>(100);\n    }\n\n    ~ArrayHashMap() {\n        // \u91ca\u653e\u5185\u5b58\n        for (const auto &bucket : buckets) {\n            delete bucket;\n        }\n        buckets.clear();\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    int hashFunc(int key) {\n        int index = key % 100;\n        return index;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    string get(int key) {\n        int index = hashFunc(key);\n        Pair *pair = buckets[index];\n        if (pair == nullptr)\n            return \"\";\n        return pair->val;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    void put(int key, string val) {\n        Pair *pair = new Pair(key, val);\n        int index = hashFunc(key);\n        buckets[index] = pair;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    void remove(int key) {\n        int index = hashFunc(key);\n        // \u91ca\u653e\u5185\u5b58\u5e76\u7f6e\u4e3a nullptr\n        delete buckets[index];\n        buckets[index] = nullptr;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n    vector<Pair *> pairSet() {\n        vector<Pair *> pairSet;\n        for (Pair *pair : buckets) {\n            if (pair != nullptr) {\n                pairSet.push_back(pair);\n            }\n        }\n        return pairSet;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e */\n    vector<int> keySet() {\n        vector<int> keySet;\n        for (Pair *pair : buckets) {\n            if (pair != nullptr) {\n                keySet.push_back(pair->key);\n            }\n        }\n        return keySet;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u503c */\n    vector<string> valueSet() {\n        vector<string> valueSet;\n        for (Pair *pair : buckets) {\n            if (pair != nullptr) {\n                valueSet.push_back(pair->val);\n            }\n        }\n        return valueSet;\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    void print() {\n        for (Pair *kv : pairSet()) {\n            cout << kv->key << \" -> \" << kv->val << endl;\n        }\n    }\n};\n
        array_hash_map.java
        /* \u952e\u503c\u5bf9 */\nclass Pair {\n    public int key;\n    public String val;\n\n    public Pair(int key, String val) {\n        this.key = key;\n        this.val = val;\n    }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n    private List<Pair> buckets;\n\n    public ArrayHashMap() {\n        // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n        buckets = new ArrayList<>();\n        for (int i = 0; i < 100; i++) {\n            buckets.add(null);\n        }\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    private int hashFunc(int key) {\n        int index = key % 100;\n        return index;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    public String get(int key) {\n        int index = hashFunc(key);\n        Pair pair = buckets.get(index);\n        if (pair == null)\n            return null;\n        return pair.val;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    public void put(int key, String val) {\n        Pair pair = new Pair(key, val);\n        int index = hashFunc(key);\n        buckets.set(index, pair);\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    public void remove(int key) {\n        int index = hashFunc(key);\n        // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n        buckets.set(index, null);\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n    public List<Pair> pairSet() {\n        List<Pair> pairSet = new ArrayList<>();\n        for (Pair pair : buckets) {\n            if (pair != null)\n                pairSet.add(pair);\n        }\n        return pairSet;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e */\n    public List<Integer> keySet() {\n        List<Integer> keySet = new ArrayList<>();\n        for (Pair pair : buckets) {\n            if (pair != null)\n                keySet.add(pair.key);\n        }\n        return keySet;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u503c */\n    public List<String> valueSet() {\n        List<String> valueSet = new ArrayList<>();\n        for (Pair pair : buckets) {\n            if (pair != null)\n                valueSet.add(pair.val);\n        }\n        return valueSet;\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    public void print() {\n        for (Pair kv : pairSet()) {\n            System.out.println(kv.key + \" -> \" + kv.val);\n        }\n    }\n}\n
        array_hash_map.cs
        /* \u952e\u503c\u5bf9 int->string */\nclass Pair(int key, string val) {\n    public int key = key;\n    public string val = val;\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n    List<Pair?> buckets;\n    public ArrayHashMap() {\n        // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n        buckets = [];\n        for (int i = 0; i < 100; i++) {\n            buckets.Add(null);\n        }\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    int HashFunc(int key) {\n        int index = key % 100;\n        return index;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    public string? Get(int key) {\n        int index = HashFunc(key);\n        Pair? pair = buckets[index];\n        if (pair == null) return null;\n        return pair.val;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    public void Put(int key, string val) {\n        Pair pair = new(key, val);\n        int index = HashFunc(key);\n        buckets[index] = pair;\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    public void Remove(int key) {\n        int index = HashFunc(key);\n        // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n        buckets[index] = null;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n    public List<Pair> PairSet() {\n        List<Pair> pairSet = [];\n        foreach (Pair? pair in buckets) {\n            if (pair != null)\n                pairSet.Add(pair);\n        }\n        return pairSet;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e */\n    public List<int> KeySet() {\n        List<int> keySet = [];\n        foreach (Pair? pair in buckets) {\n            if (pair != null)\n                keySet.Add(pair.key);\n        }\n        return keySet;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u503c */\n    public List<string> ValueSet() {\n        List<string> valueSet = [];\n        foreach (Pair? pair in buckets) {\n            if (pair != null)\n                valueSet.Add(pair.val);\n        }\n        return valueSet;\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    public void Print() {\n        foreach (Pair kv in PairSet()) {\n            Console.WriteLine(kv.key + \" -> \" + kv.val);\n        }\n    }\n}\n
        array_hash_map.go
        /* \u952e\u503c\u5bf9 */\ntype pair struct {\n    key int\n    val string\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\ntype arrayHashMap struct {\n    buckets []*pair\n}\n\n/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nfunc newArrayHashMap() *arrayHashMap {\n    // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n    buckets := make([]*pair, 100)\n    return &arrayHashMap{buckets: buckets}\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (a *arrayHashMap) hashFunc(key int) int {\n    index := key % 100\n    return index\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (a *arrayHashMap) get(key int) string {\n    index := a.hashFunc(key)\n    pair := a.buckets[index]\n    if pair == nil {\n        return \"Not Found\"\n    }\n    return pair.val\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (a *arrayHashMap) put(key int, val string) {\n    pair := &pair{key: key, val: val}\n    index := a.hashFunc(key)\n    a.buckets[index] = pair\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (a *arrayHashMap) remove(key int) {\n    index := a.hashFunc(key)\n    // \u7f6e\u4e3a nil \uff0c\u4ee3\u8868\u5220\u9664\n    a.buckets[index] = nil\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e\u5bf9 */\nfunc (a *arrayHashMap) pairSet() []*pair {\n    var pairs []*pair\n    for _, pair := range a.buckets {\n        if pair != nil {\n            pairs = append(pairs, pair)\n        }\n    }\n    return pairs\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e */\nfunc (a *arrayHashMap) keySet() []int {\n    var keys []int\n    for _, pair := range a.buckets {\n        if pair != nil {\n            keys = append(keys, pair.key)\n        }\n    }\n    return keys\n}\n\n/* \u83b7\u53d6\u6240\u6709\u503c */\nfunc (a *arrayHashMap) valueSet() []string {\n    var values []string\n    for _, pair := range a.buckets {\n        if pair != nil {\n            values = append(values, pair.val)\n        }\n    }\n    return values\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (a *arrayHashMap) print() {\n    for _, pair := range a.buckets {\n        if pair != nil {\n            fmt.Println(pair.key, \"->\", pair.val)\n        }\n    }\n}\n
        array_hash_map.swift
        /* \u952e\u503c\u5bf9 */\nclass Pair: Equatable {\n    public var key: Int\n    public var val: String\n\n    public init(key: Int, val: String) {\n        self.key = key\n        self.val = val\n    }\n\n    public static func == (lhs: Pair, rhs: Pair) -> Bool {\n        lhs.key == rhs.key && lhs.val == rhs.val\n    }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n    private var buckets: [Pair?]\n\n    init() {\n        // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n        buckets = Array(repeating: nil, count: 100)\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    private func hashFunc(key: Int) -> Int {\n        let index = key % 100\n        return index\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    func get(key: Int) -> String? {\n        let index = hashFunc(key: key)\n        let pair = buckets[index]\n        return pair?.val\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    func put(key: Int, val: String) {\n        let pair = Pair(key: key, val: val)\n        let index = hashFunc(key: key)\n        buckets[index] = pair\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    func remove(key: Int) {\n        let index = hashFunc(key: key)\n        // \u7f6e\u4e3a nil \uff0c\u4ee3\u8868\u5220\u9664\n        buckets[index] = nil\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n    func pairSet() -> [Pair] {\n        buckets.compactMap { $0 }\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e */\n    func keySet() -> [Int] {\n        buckets.compactMap { $0?.key }\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u503c */\n    func valueSet() -> [String] {\n        buckets.compactMap { $0?.val }\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    func print() {\n        for pair in pairSet() {\n            Swift.print(\"\\(pair.key) -> \\(pair.val)\")\n        }\n    }\n}\n
        array_hash_map.js
        /* \u952e\u503c\u5bf9 Number -> String */\nclass Pair {\n    constructor(key, val) {\n        this.key = key;\n        this.val = val;\n    }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n    #buckets;\n    constructor() {\n        // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n        this.#buckets = new Array(100).fill(null);\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    #hashFunc(key) {\n        return key % 100;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    get(key) {\n        let index = this.#hashFunc(key);\n        let pair = this.#buckets[index];\n        if (pair === null) return null;\n        return pair.val;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    set(key, val) {\n        let index = this.#hashFunc(key);\n        this.#buckets[index] = new Pair(key, val);\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    delete(key) {\n        let index = this.#hashFunc(key);\n        // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n        this.#buckets[index] = null;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n    entries() {\n        let arr = [];\n        for (let i = 0; i < this.#buckets.length; i++) {\n            if (this.#buckets[i]) {\n                arr.push(this.#buckets[i]);\n            }\n        }\n        return arr;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e */\n    keys() {\n        let arr = [];\n        for (let i = 0; i < this.#buckets.length; i++) {\n            if (this.#buckets[i]) {\n                arr.push(this.#buckets[i].key);\n            }\n        }\n        return arr;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u503c */\n    values() {\n        let arr = [];\n        for (let i = 0; i < this.#buckets.length; i++) {\n            if (this.#buckets[i]) {\n                arr.push(this.#buckets[i].val);\n            }\n        }\n        return arr;\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    print() {\n        let pairSet = this.entries();\n        for (const pair of pairSet) {\n            console.info(`${pair.key} -> ${pair.val}`);\n        }\n    }\n}\n
        array_hash_map.ts
        /* \u952e\u503c\u5bf9 Number -> String */\nclass Pair {\n    public key: number;\n    public val: string;\n\n    constructor(key: number, val: string) {\n        this.key = key;\n        this.val = val;\n    }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n    private readonly buckets: (Pair | null)[];\n\n    constructor() {\n        // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n        this.buckets = new Array(100).fill(null);\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    private hashFunc(key: number): number {\n        return key % 100;\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    public get(key: number): string | null {\n        let index = this.hashFunc(key);\n        let pair = this.buckets[index];\n        if (pair === null) return null;\n        return pair.val;\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    public set(key: number, val: string) {\n        let index = this.hashFunc(key);\n        this.buckets[index] = new Pair(key, val);\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    public delete(key: number) {\n        let index = this.hashFunc(key);\n        // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n        this.buckets[index] = null;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n    public entries(): (Pair | null)[] {\n        let arr: (Pair | null)[] = [];\n        for (let i = 0; i < this.buckets.length; i++) {\n            if (this.buckets[i]) {\n                arr.push(this.buckets[i]);\n            }\n        }\n        return arr;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e */\n    public keys(): (number | undefined)[] {\n        let arr: (number | undefined)[] = [];\n        for (let i = 0; i < this.buckets.length; i++) {\n            if (this.buckets[i]) {\n                arr.push(this.buckets[i].key);\n            }\n        }\n        return arr;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u503c */\n    public values(): (string | undefined)[] {\n        let arr: (string | undefined)[] = [];\n        for (let i = 0; i < this.buckets.length; i++) {\n            if (this.buckets[i]) {\n                arr.push(this.buckets[i].val);\n            }\n        }\n        return arr;\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    public print() {\n        let pairSet = this.entries();\n        for (const pair of pairSet) {\n            console.info(`${pair.key} -> ${pair.val}`);\n        }\n    }\n}\n
        array_hash_map.dart
        /* \u952e\u503c\u5bf9 */\nclass Pair {\n  int key;\n  String val;\n  Pair(this.key, this.val);\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n  late List<Pair?> _buckets;\n\n  ArrayHashMap() {\n    // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n    _buckets = List.filled(100, null);\n  }\n\n  /* \u54c8\u5e0c\u51fd\u6570 */\n  int _hashFunc(int key) {\n    final int index = key % 100;\n    return index;\n  }\n\n  /* \u67e5\u8be2\u64cd\u4f5c */\n  String? get(int key) {\n    final int index = _hashFunc(key);\n    final Pair? pair = _buckets[index];\n    if (pair == null) {\n      return null;\n    }\n    return pair.val;\n  }\n\n  /* \u6dfb\u52a0\u64cd\u4f5c */\n  void put(int key, String val) {\n    final Pair pair = Pair(key, val);\n    final int index = _hashFunc(key);\n    _buckets[index] = pair;\n  }\n\n  /* \u5220\u9664\u64cd\u4f5c */\n  void remove(int key) {\n    final int index = _hashFunc(key);\n    _buckets[index] = null;\n  }\n\n  /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n  List<Pair> pairSet() {\n    List<Pair> pairSet = [];\n    for (final Pair? pair in _buckets) {\n      if (pair != null) {\n        pairSet.add(pair);\n      }\n    }\n    return pairSet;\n  }\n\n  /* \u83b7\u53d6\u6240\u6709\u952e */\n  List<int> keySet() {\n    List<int> keySet = [];\n    for (final Pair? pair in _buckets) {\n      if (pair != null) {\n        keySet.add(pair.key);\n      }\n    }\n    return keySet;\n  }\n\n  /* \u83b7\u53d6\u6240\u6709\u503c */\n  List<String> values() {\n    List<String> valueSet = [];\n    for (final Pair? pair in _buckets) {\n      if (pair != null) {\n        valueSet.add(pair.val);\n      }\n    }\n    return valueSet;\n  }\n\n  /* \u6253\u5370\u54c8\u5e0c\u8868 */\n  void printHashMap() {\n    for (final Pair kv in pairSet()) {\n      print(\"${kv.key} -> ${kv.val}\");\n    }\n  }\n}\n
        array_hash_map.rs
        /* \u952e\u503c\u5bf9 */\n#[derive(Debug, Clone, PartialEq)]\npub struct Pair {\n    pub key: i32,\n    pub val: String,\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\npub struct ArrayHashMap {\n    buckets: Vec<Option<Pair>>,\n}\n\nimpl ArrayHashMap {\n    pub fn new() -> ArrayHashMap {\n        // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n        Self {\n            buckets: vec![None; 100],\n        }\n    }\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    fn hash_func(&self, key: i32) -> usize {\n        key as usize % 100\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    pub fn get(&self, key: i32) -> Option<&String> {\n        let index = self.hash_func(key);\n        self.buckets[index].as_ref().map(|pair| &pair.val)\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    pub fn put(&mut self, key: i32, val: &str) {\n        let index = self.hash_func(key);\n        self.buckets[index] = Some(Pair {\n            key,\n            val: val.to_string(),\n        });\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    pub fn remove(&mut self, key: i32) {\n        let index = self.hash_func(key);\n        // \u7f6e\u4e3a None \uff0c\u4ee3\u8868\u5220\u9664\n        self.buckets[index] = None;\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n    pub fn entry_set(&self) -> Vec<&Pair> {\n        self.buckets\n            .iter()\n            .filter_map(|pair| pair.as_ref())\n            .collect()\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e */\n    pub fn key_set(&self) -> Vec<&i32> {\n        self.buckets\n            .iter()\n            .filter_map(|pair| pair.as_ref().map(|pair| &pair.key))\n            .collect()\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u503c */\n    pub fn value_set(&self) -> Vec<&String> {\n        self.buckets\n            .iter()\n            .filter_map(|pair| pair.as_ref().map(|pair| &pair.val))\n            .collect()\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    pub fn print(&self) {\n        for pair in self.entry_set() {\n            println!(\"{} -> {}\", pair.key, pair.val);\n        }\n    }\n}\n
        array_hash_map.c
        /* \u952e\u503c\u5bf9 int->string */\ntypedef struct {\n    int key;\n    char *val;\n} Pair;\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\ntypedef struct {\n    Pair *buckets[MAX_SIZE];\n} ArrayHashMap;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayHashMap *newArrayHashMap() {\n    ArrayHashMap *hmap = malloc(sizeof(ArrayHashMap));\n    for (int i=0; i < MAX_SIZE; i++) {\n        hmap->buckets[i] = NULL;\n    }\n    return hmap;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayHashMap(ArrayHashMap *hmap) {\n    for (int i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            free(hmap->buckets[i]->val);\n            free(hmap->buckets[i]);\n        }\n    }\n    free(hmap);\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(ArrayHashMap *hmap, const int key, const char *val) {\n    Pair *Pair = malloc(sizeof(Pair));\n    Pair->key = key;\n    Pair->val = malloc(strlen(val) + 1);\n    strcpy(Pair->val, val);\n\n    int index = hashFunc(key);\n    hmap->buckets[index] = Pair;\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeItem(ArrayHashMap *hmap, const int key) {\n    int index = hashFunc(key);\n    free(hmap->buckets[index]->val);\n    free(hmap->buckets[index]);\n    hmap->buckets[index] = NULL;\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nvoid pairSet(ArrayHashMap *hmap, MapSet *set) {\n    Pair *entries;\n    int i = 0, index = 0;\n    int total = 0;\n    /* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            total++;\n        }\n    }\n    entries = malloc(sizeof(Pair) * total);\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            entries[index].key = hmap->buckets[i]->key;\n            entries[index].val = malloc(strlen(hmap->buckets[i]->val) + 1);\n            strcpy(entries[index].val, hmap->buckets[i]->val);\n            index++;\n        }\n    }\n    set->set = entries;\n    set->len = total;\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e */\nvoid keySet(ArrayHashMap *hmap, MapSet *set) {\n    int *keys;\n    int i = 0, index = 0;\n    int total = 0;\n    /* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            total++;\n        }\n    }\n    keys = malloc(total * sizeof(int));\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            keys[index] = hmap->buckets[i]->key;\n            index++;\n        }\n    }\n    set->set = keys;\n    set->len = total;\n}\n\n/* \u83b7\u53d6\u6240\u6709\u503c */\nvoid valueSet(ArrayHashMap *hmap, MapSet *set) {\n    char **vals;\n    int i = 0, index = 0;\n    int total = 0;\n    /* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            total++;\n        }\n    }\n    vals = malloc(total * sizeof(char *));\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            vals[index] = hmap->buckets[i]->val;\n            index++;\n        }\n    }\n    set->set = vals;\n    set->len = total;\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(ArrayHashMap *hmap) {\n    int i;\n    MapSet set;\n    pairSet(hmap, &set);\n    Pair *entries = (Pair *)set.set;\n    for (i = 0; i < set.len; i++) {\n        printf(\"%d -> %s\\n\", entries[i].key, entries[i].val);\n    }\n    free(set.set);\n}\n
        array_hash_map.kt
        /* \u952e\u503c\u5bf9 */\nclass Pair(\n    var key: Int,\n    var _val: String\n)\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n    // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n    private val buckets = arrayOfNulls<Pair>(100)\n\n    /* \u54c8\u5e0c\u51fd\u6570 */\n    fun hashFunc(key: Int): Int {\n        val index = key % 100\n        return index\n    }\n\n    /* \u67e5\u8be2\u64cd\u4f5c */\n    fun get(key: Int): String? {\n        val index = hashFunc(key)\n        val pair = buckets[index] ?: return null\n        return pair._val\n    }\n\n    /* \u6dfb\u52a0\u64cd\u4f5c */\n    fun put(key: Int, _val: String) {\n        val pair = Pair(key, _val)\n        val index = hashFunc(key)\n        buckets[index] = pair\n    }\n\n    /* \u5220\u9664\u64cd\u4f5c */\n    fun remove(key: Int) {\n        val index = hashFunc(key)\n        // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n        buckets[index] = null\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n    fun pairSet(): MutableList<Pair> {\n        val pairSet = mutableListOf<Pair>()\n        for (pair in buckets) {\n            if (pair != null)\n                pairSet.add(pair)\n        }\n        return pairSet\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u952e */\n    fun keySet(): MutableList<Int> {\n        val keySet = mutableListOf<Int>()\n        for (pair in buckets) {\n            if (pair != null)\n                keySet.add(pair.key)\n        }\n        return keySet\n    }\n\n    /* \u83b7\u53d6\u6240\u6709\u503c */\n    fun valueSet(): MutableList<String> {\n        val valueSet = mutableListOf<String>()\n        for (pair in buckets) {\n            if (pair != null)\n                valueSet.add(pair._val)\n        }\n        return valueSet\n    }\n\n    /* \u6253\u5370\u54c8\u5e0c\u8868 */\n    fun print() {\n        for (kv in pairSet()) {\n            val key = kv.key\n            val _val = kv._val\n            println(\"$key -> $_val\")\n        }\n    }\n}\n
        array_hash_map.rb
        ### \u952e\u503c\u5bf9 ###\nclass Pair\n  attr_accessor :key, :val\n\n  def initialize(key, val)\n    @key = key\n    @val = val\n  end\nend\n\n### \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 ###\nclass ArrayHashMap\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize\n    # \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n    @buckets = Array.new(100)\n  end\n\n  ### \u54c8\u5e0c\u51fd\u6570 ###\n  def hash_func(key)\n    index = key % 100\n  end\n\n  ### \u67e5\u8be2\u64cd\u4f5c ###\n  def get(key)\n    index = hash_func(key)\n    pair = @buckets[index]\n\n    return if pair.nil?\n    pair.val\n  end\n\n  ### \u6dfb\u52a0\u64cd\u4f5c ###\n  def put(key, val)\n    pair = Pair.new(key, val)\n    index = hash_func(key)\n    @buckets[index] = pair\n  end\n\n  ### \u5220\u9664\u64cd\u4f5c ###\n  def remove(key)\n    index = hash_func(key)\n    # \u7f6e\u4e3a nil \uff0c\u4ee3\u8868\u5220\u9664\n    @buckets[index] = nil\n  end\n\n  ### \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 ###\n  def entry_set\n    result = []\n    @buckets.each { |pair| result << pair unless pair.nil? }\n    result\n  end\n\n  ### \u83b7\u53d6\u6240\u6709\u952e ###\n  def key_set\n    result = []\n    @buckets.each { |pair| result << pair.key unless pair.nil? }\n    result\n  end\n\n  ### \u83b7\u53d6\u6240\u6709\u503c ###\n  def value_set\n    result = []\n    @buckets.each { |pair| result << pair.val unless pair.nil? }\n    result\n  end\n\n  ### \u6253\u5370\u54c8\u5e0c\u8868 ###\n  def print\n    @buckets.each { |pair| puts \"#{pair.key} -> #{pair.val}\" unless pair.nil? }\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_hashing/hash_map/#613","title":"6.1.3 \u00a0 \u54c8\u5e0c\u51b2\u7a81\u4e0e\u6269\u5bb9","text":"

        \u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5c06\u6240\u6709 key \u6784\u6210\u7684\u8f93\u5165\u7a7a\u95f4\u6620\u5c04\u5230\u6570\u7ec4\u6240\u6709\u7d22\u5f15\u6784\u6210\u7684\u8f93\u51fa\u7a7a\u95f4\uff0c\u800c\u8f93\u5165\u7a7a\u95f4\u5f80\u5f80\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u7406\u8bba\u4e0a\u4e00\u5b9a\u5b58\u5728\u201c\u591a\u4e2a\u8f93\u5165\u5bf9\u5e94\u76f8\u540c\u8f93\u51fa\u201d\u7684\u60c5\u51b5\u3002

        \u5bf9\u4e8e\u4e0a\u8ff0\u793a\u4f8b\u4e2d\u7684\u54c8\u5e0c\u51fd\u6570\uff0c\u5f53\u8f93\u5165\u7684 key \u540e\u4e24\u4f4d\u76f8\u540c\u65f6\uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u8f93\u51fa\u7ed3\u679c\u4e5f\u76f8\u540c\u3002\u4f8b\u5982\uff0c\u67e5\u8be2\u5b66\u53f7\u4e3a 12836 \u548c 20336 \u7684\u4e24\u4e2a\u5b66\u751f\u65f6\uff0c\u6211\u4eec\u5f97\u5230\uff1a

        12836 % 100 = 36\n20336 % 100 = 36\n

        \u5982\u56fe 6-3 \u6240\u793a\uff0c\u4e24\u4e2a\u5b66\u53f7\u6307\u5411\u4e86\u540c\u4e00\u4e2a\u59d3\u540d\uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u5bf9\u7684\u3002\u6211\u4eec\u5c06\u8fd9\u79cd\u591a\u4e2a\u8f93\u5165\u5bf9\u5e94\u540c\u4e00\u8f93\u51fa\u7684\u60c5\u51b5\u79f0\u4e3a\u54c8\u5e0c\u51b2\u7a81\uff08hash collision\uff09\u3002

        \u56fe 6-3 \u00a0 \u54c8\u5e0c\u51b2\u7a81\u793a\u4f8b

        \u5bb9\u6613\u60f3\u5230\uff0c\u54c8\u5e0c\u8868\u5bb9\u91cf \\(n\\) \u8d8a\u5927\uff0c\u591a\u4e2a key \u88ab\u5206\u914d\u5230\u540c\u4e00\u4e2a\u6876\u4e2d\u7684\u6982\u7387\u5c31\u8d8a\u4f4e\uff0c\u51b2\u7a81\u5c31\u8d8a\u5c11\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6269\u5bb9\u54c8\u5e0c\u8868\u6765\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u3002

        \u5982\u56fe 6-4 \u6240\u793a\uff0c\u6269\u5bb9\u524d\u952e\u503c\u5bf9 (136, A) \u548c (236, D) \u53d1\u751f\u51b2\u7a81\uff0c\u6269\u5bb9\u540e\u51b2\u7a81\u6d88\u5931\u3002

        \u56fe 6-4 \u00a0 \u54c8\u5e0c\u8868\u6269\u5bb9

        \u7c7b\u4f3c\u4e8e\u6570\u7ec4\u6269\u5bb9\uff0c\u54c8\u5e0c\u8868\u6269\u5bb9\u9700\u5c06\u6240\u6709\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u8fc1\u79fb\u81f3\u65b0\u54c8\u5e0c\u8868\uff0c\u975e\u5e38\u8017\u65f6\uff1b\u5e76\u4e14\u7531\u4e8e\u54c8\u5e0c\u8868\u5bb9\u91cf capacity \u6539\u53d8\uff0c\u6211\u4eec\u9700\u8981\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u6765\u91cd\u65b0\u8ba1\u7b97\u6240\u6709\u952e\u503c\u5bf9\u7684\u5b58\u50a8\u4f4d\u7f6e\uff0c\u8fd9\u8fdb\u4e00\u6b65\u589e\u52a0\u4e86\u6269\u5bb9\u8fc7\u7a0b\u7684\u8ba1\u7b97\u5f00\u9500\u3002\u4e3a\u6b64\uff0c\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u4f1a\u9884\u7559\u8db3\u591f\u5927\u7684\u54c8\u5e0c\u8868\u5bb9\u91cf\uff0c\u9632\u6b62\u9891\u7e41\u6269\u5bb9\u3002

        \u8d1f\u8f7d\u56e0\u5b50\uff08load factor\uff09\u662f\u54c8\u5e0c\u8868\u7684\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\uff0c\u5176\u5b9a\u4e49\u4e3a\u54c8\u5e0c\u8868\u7684\u5143\u7d20\u6570\u91cf\u9664\u4ee5\u6876\u6570\u91cf\uff0c\u7528\u4e8e\u8861\u91cf\u54c8\u5e0c\u51b2\u7a81\u7684\u4e25\u91cd\u7a0b\u5ea6\uff0c\u4e5f\u5e38\u4f5c\u4e3a\u54c8\u5e0c\u8868\u6269\u5bb9\u7684\u89e6\u53d1\u6761\u4ef6\u3002\u4f8b\u5982\u5728 Java \u4e2d\uff0c\u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7 \\(0.75\\) \u65f6\uff0c\u7cfb\u7edf\u4f1a\u5c06\u54c8\u5e0c\u8868\u6269\u5bb9\u81f3\u539f\u5148\u7684 \\(2\\) \u500d\u3002

        "},{"location":"chapter_hashing/summary/","title":"6.4 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_hashing/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u8f93\u5165 key \uff0c\u54c8\u5e0c\u8868\u80fd\u591f\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u67e5\u8be2\u5230 value \uff0c\u6548\u7387\u975e\u5e38\u9ad8\u3002
        • \u5e38\u89c1\u7684\u54c8\u5e0c\u8868\u64cd\u4f5c\u5305\u62ec\u67e5\u8be2\u3001\u6dfb\u52a0\u952e\u503c\u5bf9\u3001\u5220\u9664\u952e\u503c\u5bf9\u548c\u904d\u5386\u54c8\u5e0c\u8868\u7b49\u3002
        • \u54c8\u5e0c\u51fd\u6570\u5c06 key \u6620\u5c04\u4e3a\u6570\u7ec4\u7d22\u5f15\uff0c\u4ece\u800c\u8bbf\u95ee\u5bf9\u5e94\u6876\u5e76\u83b7\u53d6 value \u3002
        • \u4e24\u4e2a\u4e0d\u540c\u7684 key \u53ef\u80fd\u5728\u7ecf\u8fc7\u54c8\u5e0c\u51fd\u6570\u540e\u5f97\u5230\u76f8\u540c\u7684\u6570\u7ec4\u7d22\u5f15\uff0c\u5bfc\u81f4\u67e5\u8be2\u7ed3\u679c\u51fa\u9519\uff0c\u8fd9\u79cd\u73b0\u8c61\u88ab\u79f0\u4e3a\u54c8\u5e0c\u51b2\u7a81\u3002
        • \u54c8\u5e0c\u8868\u5bb9\u91cf\u8d8a\u5927\uff0c\u54c8\u5e0c\u51b2\u7a81\u7684\u6982\u7387\u5c31\u8d8a\u4f4e\u3002\u56e0\u6b64\u53ef\u4ee5\u901a\u8fc7\u6269\u5bb9\u54c8\u5e0c\u8868\u6765\u7f13\u89e3\u54c8\u5e0c\u51b2\u7a81\u3002\u4e0e\u6570\u7ec4\u6269\u5bb9\u7c7b\u4f3c\uff0c\u54c8\u5e0c\u8868\u6269\u5bb9\u64cd\u4f5c\u7684\u5f00\u9500\u5f88\u5927\u3002
        • \u8d1f\u8f7d\u56e0\u5b50\u5b9a\u4e49\u4e3a\u54c8\u5e0c\u8868\u4e2d\u5143\u7d20\u6570\u91cf\u9664\u4ee5\u6876\u6570\u91cf\uff0c\u53cd\u6620\u4e86\u54c8\u5e0c\u51b2\u7a81\u7684\u4e25\u91cd\u7a0b\u5ea6\uff0c\u5e38\u7528\u4f5c\u89e6\u53d1\u54c8\u5e0c\u8868\u6269\u5bb9\u7684\u6761\u4ef6\u3002
        • \u94fe\u5f0f\u5730\u5740\u901a\u8fc7\u5c06\u5355\u4e2a\u5143\u7d20\u8f6c\u5316\u4e3a\u94fe\u8868\uff0c\u5c06\u6240\u6709\u51b2\u7a81\u5143\u7d20\u5b58\u50a8\u5728\u540c\u4e00\u4e2a\u94fe\u8868\u4e2d\u3002\u7136\u800c\uff0c\u94fe\u8868\u8fc7\u957f\u4f1a\u964d\u4f4e\u67e5\u8be2\u6548\u7387\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fdb\u4e00\u6b65\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811\u6765\u63d0\u9ad8\u6548\u7387\u3002
        • \u5f00\u653e\u5bfb\u5740\u901a\u8fc7\u591a\u6b21\u63a2\u6d4b\u6765\u5904\u7406\u54c8\u5e0c\u51b2\u7a81\u3002\u7ebf\u6027\u63a2\u6d4b\u4f7f\u7528\u56fa\u5b9a\u6b65\u957f\uff0c\u7f3a\u70b9\u662f\u4e0d\u80fd\u5220\u9664\u5143\u7d20\uff0c\u4e14\u5bb9\u6613\u4ea7\u751f\u805a\u96c6\u3002\u591a\u6b21\u54c8\u5e0c\u4f7f\u7528\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570\u8fdb\u884c\u63a2\u6d4b\uff0c\u76f8\u8f83\u7ebf\u6027\u63a2\u6d4b\u66f4\u4e0d\u6613\u4ea7\u751f\u805a\u96c6\uff0c\u4f46\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570\u589e\u52a0\u4e86\u8ba1\u7b97\u91cf\u3002
        • \u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u91c7\u53d6\u4e86\u4e0d\u540c\u7684\u54c8\u5e0c\u8868\u5b9e\u73b0\u3002\u4f8b\u5982\uff0cJava \u7684 HashMap \u4f7f\u7528\u94fe\u5f0f\u5730\u5740\uff0c\u800c Python \u7684 Dict \u91c7\u7528\u5f00\u653e\u5bfb\u5740\u3002
        • \u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u6211\u4eec\u5e0c\u671b\u54c8\u5e0c\u7b97\u6cd5\u5177\u6709\u786e\u5b9a\u6027\u3001\u9ad8\u6548\u7387\u548c\u5747\u5300\u5206\u5e03\u7684\u7279\u70b9\u3002\u5728\u5bc6\u7801\u5b66\u4e2d\uff0c\u54c8\u5e0c\u7b97\u6cd5\u8fd8\u5e94\u8be5\u5177\u5907\u6297\u78b0\u649e\u6027\u548c\u96ea\u5d29\u6548\u5e94\u3002
        • \u54c8\u5e0c\u7b97\u6cd5\u901a\u5e38\u91c7\u7528\u5927\u8d28\u6570\u4f5c\u4e3a\u6a21\u6570\uff0c\u4ee5\u6700\u5927\u5316\u5730\u4fdd\u8bc1\u54c8\u5e0c\u503c\u5747\u5300\u5206\u5e03\uff0c\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u3002
        • \u5e38\u89c1\u7684\u54c8\u5e0c\u7b97\u6cd5\u5305\u62ec MD5\u3001SHA-1\u3001SHA-2 \u548c SHA-3 \u7b49\u3002MD5 \u5e38\u7528\u4e8e\u6821\u9a8c\u6587\u4ef6\u5b8c\u6574\u6027\uff0cSHA-2 \u5e38\u7528\u4e8e\u5b89\u5168\u5e94\u7528\u4e0e\u534f\u8bae\u3002
        • \u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u4f1a\u4e3a\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u7b97\u6cd5\uff0c\u7528\u4e8e\u8ba1\u7b97\u54c8\u5e0c\u8868\u4e2d\u7684\u6876\u7d22\u5f15\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u53ea\u6709\u4e0d\u53ef\u53d8\u5bf9\u8c61\u662f\u53ef\u54c8\u5e0c\u7684\u3002
        "},{"location":"chapter_hashing/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u662f \\(O(n)\\) \uff1f

        \u5f53\u54c8\u5e0c\u51b2\u7a81\u6bd4\u8f83\u4e25\u91cd\u65f6\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u9000\u5316\u81f3 \\(O(n)\\) \u3002\u5f53\u54c8\u5e0c\u51fd\u6570\u8bbe\u8ba1\u5f97\u6bd4\u8f83\u597d\u3001\u5bb9\u91cf\u8bbe\u7f6e\u6bd4\u8f83\u5408\u7406\u3001\u51b2\u7a81\u6bd4\u8f83\u5e73\u5747\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u3002\u6211\u4eec\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u5185\u7f6e\u7684\u54c8\u5e0c\u8868\u65f6\uff0c\u901a\u5e38\u8ba4\u4e3a\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u3002

        Q\uff1a\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528\u54c8\u5e0c\u51fd\u6570 \\(f(x) = x\\) \u5462\uff1f\u8fd9\u6837\u5c31\u4e0d\u4f1a\u6709\u51b2\u7a81\u4e86\u3002

        \u5728 \\(f(x) = x\\) \u54c8\u5e0c\u51fd\u6570\u4e0b\uff0c\u6bcf\u4e2a\u5143\u7d20\u5bf9\u5e94\u552f\u4e00\u7684\u6876\u7d22\u5f15\uff0c\u8fd9\u4e0e\u6570\u7ec4\u7b49\u4ef7\u3002\u7136\u800c\uff0c\u8f93\u5165\u7a7a\u95f4\u901a\u5e38\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\uff08\u6570\u7ec4\u957f\u5ea6\uff09\uff0c\u56e0\u6b64\u54c8\u5e0c\u51fd\u6570\u7684\u6700\u540e\u4e00\u6b65\u5f80\u5f80\u662f\u5bf9\u6570\u7ec4\u957f\u5ea6\u53d6\u6a21\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u54c8\u5e0c\u8868\u7684\u76ee\u6807\u662f\u5c06\u4e00\u4e2a\u8f83\u5927\u7684\u72b6\u6001\u7a7a\u95f4\u6620\u5c04\u5230\u4e00\u4e2a\u8f83\u5c0f\u7684\u7a7a\u95f4\uff0c\u5e76\u63d0\u4f9b \\(O(1)\\) \u7684\u67e5\u8be2\u6548\u7387\u3002

        Q\uff1a\u54c8\u5e0c\u8868\u5e95\u5c42\u5b9e\u73b0\u662f\u6570\u7ec4\u3001\u94fe\u8868\u3001\u4e8c\u53c9\u6811\uff0c\u4f46\u4e3a\u4ec0\u4e48\u6548\u7387\u53ef\u4ee5\u6bd4\u5b83\u4eec\u66f4\u9ad8\u5462\uff1f

        \u9996\u5148\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u6548\u7387\u53d8\u9ad8\uff0c\u4f46\u7a7a\u95f4\u6548\u7387\u53d8\u4f4e\u4e86\u3002\u54c8\u5e0c\u8868\u6709\u76f8\u5f53\u4e00\u90e8\u5206\u5185\u5b58\u672a\u4f7f\u7528\u3002

        \u5176\u6b21\uff0c\u53ea\u662f\u5728\u7279\u5b9a\u4f7f\u7528\u573a\u666f\u4e0b\u65f6\u95f4\u6548\u7387\u53d8\u9ad8\u4e86\u3002\u5982\u679c\u4e00\u4e2a\u529f\u80fd\u80fd\u591f\u5728\u76f8\u540c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e0b\u4f7f\u7528\u6570\u7ec4\u6216\u94fe\u8868\u5b9e\u73b0\uff0c\u90a3\u4e48\u901a\u5e38\u6bd4\u54c8\u5e0c\u8868\u66f4\u5feb\u3002\u8fd9\u662f\u56e0\u4e3a\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u9700\u8981\u5f00\u9500\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u5e38\u6570\u9879\u66f4\u5927\u3002

        \u6700\u540e\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u80fd\u53d1\u751f\u52a3\u5316\u3002\u4f8b\u5982\u5728\u94fe\u5f0f\u5730\u5740\u4e2d\uff0c\u6211\u4eec\u91c7\u53d6\u5728\u94fe\u8868\u6216\u7ea2\u9ed1\u6811\u4e2d\u6267\u884c\u67e5\u627e\u64cd\u4f5c\uff0c\u4ecd\u7136\u6709\u9000\u5316\u81f3 \\(O(n)\\) \u65f6\u95f4\u7684\u98ce\u9669\u3002

        Q\uff1a\u591a\u6b21\u54c8\u5e0c\u6709\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u7684\u7f3a\u9677\u5417\uff1f\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u7a7a\u95f4\u8fd8\u80fd\u518d\u6b21\u4f7f\u7528\u5417\uff1f

        \u591a\u6b21\u54c8\u5e0c\u662f\u5f00\u653e\u5bfb\u5740\u7684\u4e00\u79cd\uff0c\u5f00\u653e\u5bfb\u5740\u6cd5\u90fd\u6709\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u7684\u7f3a\u9677\uff0c\u9700\u8981\u901a\u8fc7\u6807\u8bb0\u5220\u9664\u3002\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u7a7a\u95f4\u53ef\u4ee5\u518d\u6b21\u4f7f\u7528\u3002\u5f53\u5c06\u65b0\u5143\u7d20\u63d2\u5165\u54c8\u5e0c\u8868\uff0c\u5e76\u4e14\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u627e\u5230\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u4f4d\u7f6e\u65f6\uff0c\u8be5\u4f4d\u7f6e\u53ef\u4ee5\u88ab\u65b0\u5143\u7d20\u4f7f\u7528\u3002\u8fd9\u6837\u505a\u65e2\u80fd\u4fdd\u6301\u54c8\u5e0c\u8868\u7684\u63a2\u6d4b\u5e8f\u5217\u4e0d\u53d8\uff0c\u53c8\u80fd\u4fdd\u8bc1\u54c8\u5e0c\u8868\u7684\u7a7a\u95f4\u4f7f\u7528\u7387\u3002

        Q\uff1a\u4e3a\u4ec0\u4e48\u5728\u7ebf\u6027\u63a2\u6d4b\u4e2d\uff0c\u67e5\u627e\u5143\u7d20\u7684\u65f6\u5019\u4f1a\u51fa\u73b0\u54c8\u5e0c\u51b2\u7a81\u5462\uff1f

        \u67e5\u627e\u7684\u65f6\u5019\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u627e\u5230\u5bf9\u5e94\u7684\u6876\u548c\u952e\u503c\u5bf9\uff0c\u53d1\u73b0 key \u4e0d\u5339\u914d\uff0c\u8fd9\u5c31\u4ee3\u8868\u6709\u54c8\u5e0c\u51b2\u7a81\u3002\u56e0\u6b64\uff0c\u7ebf\u6027\u63a2\u6d4b\u6cd5\u4f1a\u6839\u636e\u9884\u5148\u8bbe\u5b9a\u7684\u6b65\u957f\u4f9d\u6b21\u5411\u4e0b\u67e5\u627e\uff0c\u76f4\u81f3\u627e\u5230\u6b63\u786e\u7684\u952e\u503c\u5bf9\u6216\u65e0\u6cd5\u627e\u5230\u8df3\u51fa\u4e3a\u6b62\u3002

        Q\uff1a\u4e3a\u4ec0\u4e48\u54c8\u5e0c\u8868\u6269\u5bb9\u80fd\u591f\u7f13\u89e3\u54c8\u5e0c\u51b2\u7a81\uff1f

        \u54c8\u5e0c\u51fd\u6570\u7684\u6700\u540e\u4e00\u6b65\u5f80\u5f80\u662f\u5bf9\u6570\u7ec4\u957f\u5ea6 \\(n\\) \u53d6\u6a21\uff08\u53d6\u4f59\uff09\uff0c\u8ba9\u8f93\u51fa\u503c\u843d\u5728\u6570\u7ec4\u7d22\u5f15\u8303\u56f4\u5185\uff1b\u5728\u6269\u5bb9\u540e\uff0c\u6570\u7ec4\u957f\u5ea6 \\(n\\) \u53d1\u751f\u53d8\u5316\uff0c\u800c key \u5bf9\u5e94\u7684\u7d22\u5f15\u4e5f\u53ef\u80fd\u53d1\u751f\u53d8\u5316\u3002\u539f\u5148\u843d\u5728\u540c\u4e00\u4e2a\u6876\u7684\u591a\u4e2a key \uff0c\u5728\u6269\u5bb9\u540e\u53ef\u80fd\u4f1a\u88ab\u5206\u914d\u5230\u591a\u4e2a\u6876\u4e2d\uff0c\u4ece\u800c\u5b9e\u73b0\u54c8\u5e0c\u51b2\u7a81\u7684\u7f13\u89e3\u3002

        Q\uff1a\u5982\u679c\u4e3a\u4e86\u9ad8\u6548\u7684\u5b58\u53d6\uff0c\u90a3\u4e48\u76f4\u63a5\u4f7f\u7528\u6570\u7ec4\u4e0d\u5c31\u597d\u4e86\u5417\uff1f

        \u5f53\u6570\u636e\u7684 key \u662f\u8fde\u7eed\u7684\u5c0f\u8303\u56f4\u6574\u6570\u65f6\uff0c\u76f4\u63a5\u7528\u6570\u7ec4\u5373\u53ef\uff0c\u7b80\u5355\u9ad8\u6548\u3002\u4f46\u5f53 key \u662f\u5176\u4ed6\u7c7b\u578b\uff08\u4f8b\u5982\u5b57\u7b26\u4e32\uff09\u65f6\uff0c\u5c31\u9700\u8981\u501f\u52a9\u54c8\u5e0c\u51fd\u6570\u5c06 key \u6620\u5c04\u4e3a\u6570\u7ec4\u7d22\u5f15\uff0c\u518d\u901a\u8fc7\u6876\u6570\u7ec4\u5b58\u50a8\u5143\u7d20\uff0c\u8fd9\u6837\u7684\u7ed3\u6784\u5c31\u662f\u54c8\u5e0c\u8868\u3002

        "},{"location":"chapter_heap/","title":"\u7b2c 8 \u7ae0 \u00a0 \u5806","text":"

        Abstract

        \u5806\u5c31\u50cf\u662f\u5c71\u5cb3\u5cf0\u5ce6\uff0c\u5c42\u53e0\u8d77\u4f0f\u3001\u5f62\u6001\u5404\u5f02\u3002

        \u5ea7\u5ea7\u5c71\u5cf0\u9ad8\u4f4e\u9519\u843d\uff0c\u800c\u6700\u9ad8\u7684\u5c71\u5cf0\u603b\u662f\u6700\u5148\u6620\u5165\u773c\u5e18\u3002

        "},{"location":"chapter_heap/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 8.1 \u00a0 \u5806
        • 8.2 \u00a0 \u5efa\u5806\u64cd\u4f5c
        • 8.3 \u00a0 Top-k \u95ee\u9898
        • 8.4 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_heap/build_heap/","title":"8.2 \u00a0 \u5efa\u5806\u64cd\u4f5c","text":"

        \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u4f7f\u7528\u4e00\u4e2a\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\u6765\u6784\u5efa\u4e00\u4e2a\u5806\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u201c\u5efa\u5806\u64cd\u4f5c\u201d\u3002

        "},{"location":"chapter_heap/build_heap/#821","title":"8.2.1 \u00a0 \u501f\u52a9\u5165\u5806\u64cd\u4f5c\u5b9e\u73b0","text":"

        \u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u7a7a\u5806\uff0c\u7136\u540e\u904d\u5386\u5217\u8868\uff0c\u4f9d\u6b21\u5bf9\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u201c\u5165\u5806\u64cd\u4f5c\u201d\uff0c\u5373\u5148\u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u5806\u7684\u5c3e\u90e8\uff0c\u518d\u5bf9\u8be5\u5143\u7d20\u6267\u884c\u201c\u4ece\u5e95\u81f3\u9876\u201d\u5806\u5316\u3002

        \u6bcf\u5f53\u4e00\u4e2a\u5143\u7d20\u5165\u5806\uff0c\u5806\u7684\u957f\u5ea6\u5c31\u52a0\u4e00\u3002\u7531\u4e8e\u8282\u70b9\u662f\u4ece\u9876\u5230\u5e95\u4f9d\u6b21\u88ab\u6dfb\u52a0\u8fdb\u4e8c\u53c9\u6811\u7684\uff0c\u56e0\u6b64\u5806\u662f\u201c\u81ea\u4e0a\u800c\u4e0b\u201d\u6784\u5efa\u7684\u3002

        \u8bbe\u5143\u7d20\u6570\u91cf\u4e3a \\(n\\) \uff0c\u6bcf\u4e2a\u5143\u7d20\u7684\u5165\u5806\u64cd\u4f5c\u4f7f\u7528 \\(O(\\log{n})\\) \u65f6\u95f4\uff0c\u56e0\u6b64\u8be5\u5efa\u5806\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002

        "},{"location":"chapter_heap/build_heap/#822","title":"8.2.2 \u00a0 \u901a\u8fc7\u904d\u5386\u5806\u5316\u5b9e\u73b0","text":"

        \u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4e00\u79cd\u66f4\u4e3a\u9ad8\u6548\u7684\u5efa\u5806\u65b9\u6cd5\uff0c\u5171\u5206\u4e3a\u4e24\u6b65\u3002

        1. \u5c06\u5217\u8868\u6240\u6709\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u5730\u6dfb\u52a0\u5230\u5806\u4e2d\uff0c\u6b64\u65f6\u5806\u7684\u6027\u8d28\u5c1a\u672a\u5f97\u5230\u6ee1\u8db3\u3002
        2. \u5012\u5e8f\u904d\u5386\u5806\uff08\u5c42\u5e8f\u904d\u5386\u7684\u5012\u5e8f\uff09\uff0c\u4f9d\u6b21\u5bf9\u6bcf\u4e2a\u975e\u53f6\u8282\u70b9\u6267\u884c\u201c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u201d\u3002

        \u6bcf\u5f53\u5806\u5316\u4e00\u4e2a\u8282\u70b9\u540e\uff0c\u4ee5\u8be5\u8282\u70b9\u4e3a\u6839\u8282\u70b9\u7684\u5b50\u6811\u5c31\u5f62\u6210\u4e00\u4e2a\u5408\u6cd5\u7684\u5b50\u5806\u3002\u800c\u7531\u4e8e\u662f\u5012\u5e8f\u904d\u5386\uff0c\u56e0\u6b64\u5806\u662f\u201c\u81ea\u4e0b\u800c\u4e0a\u201d\u6784\u5efa\u7684\u3002

        \u4e4b\u6240\u4ee5\u9009\u62e9\u5012\u5e8f\u904d\u5386\uff0c\u662f\u56e0\u4e3a\u8fd9\u6837\u80fd\u591f\u4fdd\u8bc1\u5f53\u524d\u8282\u70b9\u4e4b\u4e0b\u7684\u5b50\u6811\u5df2\u7ecf\u662f\u5408\u6cd5\u7684\u5b50\u5806\uff0c\u8fd9\u6837\u5806\u5316\u5f53\u524d\u8282\u70b9\u624d\u662f\u6709\u6548\u7684\u3002

        \u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u7531\u4e8e\u53f6\u8282\u70b9\u6ca1\u6709\u5b50\u8282\u70b9\uff0c\u56e0\u6b64\u5b83\u4eec\u5929\u7136\u5c31\u662f\u5408\u6cd5\u7684\u5b50\u5806\uff0c\u65e0\u987b\u5806\u5316\u3002\u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u6700\u540e\u4e00\u4e2a\u975e\u53f6\u8282\u70b9\u662f\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u7684\u7236\u8282\u70b9\uff0c\u6211\u4eec\u4ece\u5b83\u5f00\u59cb\u5012\u5e8f\u904d\u5386\u5e76\u6267\u884c\u5806\u5316\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
        def __init__(self, nums: list[int]):\n    \"\"\"\u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806\"\"\"\n    # \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n    self.max_heap = nums\n    # \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for i in range(self.parent(self.size() - 1), -1, -1):\n        self.sift_down(i)\n
        my_heap.cpp
        /* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(vector<int> nums) {\n    // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n    maxHeap = nums;\n    // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for (int i = parent(size() - 1); i >= 0; i--) {\n        siftDown(i);\n    }\n}\n
        my_heap.java
        /* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(List<Integer> nums) {\n    // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n    maxHeap = new ArrayList<>(nums);\n    // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for (int i = parent(size() - 1); i >= 0; i--) {\n        siftDown(i);\n    }\n}\n
        my_heap.cs
        /* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(IEnumerable<int> nums) {\n    // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n    maxHeap = new List<int>(nums);\n    // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    var size = Parent(this.Size() - 1);\n    for (int i = size; i >= 0; i--) {\n        SiftDown(i);\n    }\n}\n
        my_heap.go
        /* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u5207\u7247\u5efa\u5806 */\nfunc newMaxHeap(nums []any) *maxHeap {\n    // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n    h := &maxHeap{data: nums}\n    for i := h.parent(len(h.data) - 1); i >= 0; i-- {\n        // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n        h.siftDown(i)\n    }\n    return h\n}\n
        my_heap.swift
        /* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\ninit(nums: [Int]) {\n    // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n    maxHeap = nums\n    // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for i in (0 ... parent(i: size() - 1)).reversed() {\n        siftDown(i: i)\n    }\n}\n
        my_heap.js
        /* \u6784\u9020\u65b9\u6cd5\uff0c\u5efa\u7acb\u7a7a\u5806\u6216\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nconstructor(nums) {\n    // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n    this.#maxHeap = nums === undefined ? [] : [...nums];\n    // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for (let i = this.#parent(this.size() - 1); i >= 0; i--) {\n        this.#siftDown(i);\n    }\n}\n
        my_heap.ts
        /* \u6784\u9020\u65b9\u6cd5\uff0c\u5efa\u7acb\u7a7a\u5806\u6216\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nconstructor(nums?: number[]) {\n    // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n    this.maxHeap = nums === undefined ? [] : [...nums];\n    // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for (let i = this.parent(this.size() - 1); i >= 0; i--) {\n        this.siftDown(i);\n    }\n}\n
        my_heap.dart
        /* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(List<int> nums) {\n  // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n  _maxHeap = nums;\n  // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n  for (int i = _parent(size() - 1); i >= 0; i--) {\n    siftDown(i);\n  }\n}\n
        my_heap.rs
        /* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nfn new(nums: Vec<i32>) -> Self {\n    // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n    let mut heap = MaxHeap { max_heap: nums };\n    // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for i in (0..=Self::parent(heap.size() - 1)).rev() {\n        heap.sift_down(i);\n    }\n    heap\n}\n
        my_heap.c
        /* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u5207\u7247\u5efa\u5806 */\nMaxHeap *newMaxHeap(int nums[], int size) {\n    // \u6240\u6709\u5143\u7d20\u5165\u5806\n    MaxHeap *maxHeap = (MaxHeap *)malloc(sizeof(MaxHeap));\n    maxHeap->size = size;\n    memcpy(maxHeap->data, nums, size * sizeof(int));\n    for (int i = parent(maxHeap, size - 1); i >= 0; i--) {\n        // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n        siftDown(maxHeap, i);\n    }\n    return maxHeap;\n}\n
        my_heap.kt
        /* \u5927\u9876\u5806 */\nclass MaxHeap(nums: MutableList<Int>?) {\n    // \u4f7f\u7528\u5217\u8868\u800c\u975e\u6570\u7ec4\uff0c\u8fd9\u6837\u65e0\u987b\u8003\u8651\u6269\u5bb9\u95ee\u9898\n    private val maxHeap = mutableListOf<Int>()\n\n    /* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\n    init {\n        // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n        maxHeap.addAll(nums!!)\n        // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n        for (i in parent(size() - 1) downTo 0) {\n            siftDown(i)\n        }\n    }\n\n    /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    private fun left(i: Int): Int {\n        return 2 * i + 1\n    }\n\n    /* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    private fun right(i: Int): Int {\n        return 2 * i + 2\n    }\n\n    /* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n    private fun parent(i: Int): Int {\n        return (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n    }\n\n    /* \u4ea4\u6362\u5143\u7d20 */\n    private fun swap(i: Int, j: Int) {\n        val temp = maxHeap[i]\n        maxHeap[i] = maxHeap[j]\n        maxHeap[j] = temp\n    }\n\n    /* \u83b7\u53d6\u5806\u5927\u5c0f */\n    fun size(): Int {\n        return maxHeap.size\n    }\n\n    /* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\n    fun isEmpty(): Boolean {\n        /* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\n        return size() == 0\n    }\n\n    /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\n    fun peek(): Int {\n        return maxHeap[0]\n    }\n\n    /* \u5143\u7d20\u5165\u5806 */\n    fun push(_val: Int) {\n        // \u6dfb\u52a0\u8282\u70b9\n        maxHeap.add(_val)\n        // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n        siftUp(size() - 1)\n    }\n\n    /* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\n    private fun siftUp(it: Int) {\n        // Kotlin\u7684\u51fd\u6570\u53c2\u6570\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u521b\u5efa\u4e34\u65f6\u53d8\u91cf\n        var i = it\n        while (true) {\n            // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n            val p = parent(i)\n            // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n            if (p < 0 || maxHeap[i] <= maxHeap[p]) break\n            // \u4ea4\u6362\u4e24\u8282\u70b9\n            swap(i, p)\n            // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n            i = p\n        }\n    }\n\n    /* \u5143\u7d20\u51fa\u5806 */\n    fun pop(): Int {\n        // \u5224\u7a7a\u5904\u7406\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        swap(0, size() - 1)\n        // \u5220\u9664\u8282\u70b9\n        val _val = maxHeap.removeAt(size() - 1)\n        // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n        siftDown(0)\n        // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n        return _val\n    }\n\n    /* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\n    private fun siftDown(it: Int) {\n        // Kotlin\u7684\u51fd\u6570\u53c2\u6570\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u521b\u5efa\u4e34\u65f6\u53d8\u91cf\n        var i = it\n        while (true) {\n            // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n            val l = left(i)\n            val r = right(i)\n            var ma = i\n            if (l < size() && maxHeap[l] > maxHeap[ma]) ma = l\n            if (r < size() && maxHeap[r] > maxHeap[ma]) ma = r\n            // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n            if (ma == i) break\n            // \u4ea4\u6362\u4e24\u8282\u70b9\n            swap(i, ma)\n            // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n            i = ma\n        }\n    }\n\n    /* \u6253\u5370\u5806\uff08\u4e8c\u53c9\u6811\uff09 */\n    fun print() {\n        val queue = PriorityQueue { a: Int, b: Int -> b - a }\n        queue.addAll(maxHeap)\n        printHeap(queue)\n    }\n}\n
        my_heap.rb
        ### \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 ###\ndef initialize(nums)\n  # \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n  @max_heap = nums\n  # \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n  parent(size - 1).downto(0) do |i|\n    sift_down(i)\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_heap/build_heap/#823","title":"8.2.3 \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"

        \u4e0b\u9762\uff0c\u6211\u4eec\u6765\u5c1d\u8bd5\u63a8\u7b97\u7b2c\u4e8c\u79cd\u5efa\u5806\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002

        • \u5047\u8bbe\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u5219\u53f6\u8282\u70b9\u6570\u91cf\u4e3a \\((n + 1) / 2\\) \uff0c\u5176\u4e2d \\(/\\) \u4e3a\u5411\u4e0b\u6574\u9664\u3002\u56e0\u6b64\u9700\u8981\u5806\u5316\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\((n - 1) / 2\\) \u3002
        • \u5728\u4ece\u9876\u81f3\u5e95\u5806\u5316\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6bcf\u4e2a\u8282\u70b9\u6700\u591a\u5806\u5316\u5230\u53f6\u8282\u70b9\uff0c\u56e0\u6b64\u6700\u5927\u8fed\u4ee3\u6b21\u6570\u4e3a\u4e8c\u53c9\u6811\u9ad8\u5ea6 \\(\\log n\\) \u3002

        \u5c06\u4e0a\u8ff0\u4e24\u8005\u76f8\u4e58\uff0c\u53ef\u5f97\u5230\u5efa\u5806\u8fc7\u7a0b\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002\u4f46\u8fd9\u4e2a\u4f30\u7b97\u7ed3\u679c\u5e76\u4e0d\u51c6\u786e\uff0c\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u8003\u8651\u5230\u4e8c\u53c9\u6811\u5e95\u5c42\u8282\u70b9\u6570\u91cf\u8fdc\u591a\u4e8e\u9876\u5c42\u8282\u70b9\u7684\u6027\u8d28\u3002

        \u63a5\u4e0b\u6765\u6211\u4eec\u6765\u8fdb\u884c\u66f4\u4e3a\u51c6\u786e\u7684\u8ba1\u7b97\u3002\u4e3a\u4e86\u964d\u4f4e\u8ba1\u7b97\u96be\u5ea6\uff0c\u5047\u8bbe\u7ed9\u5b9a\u4e00\u4e2a\u8282\u70b9\u6570\u91cf\u4e3a \\(n\\) \u3001\u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u201c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u201d\uff0c\u8be5\u5047\u8bbe\u4e0d\u4f1a\u5f71\u54cd\u8ba1\u7b97\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002

        \u56fe 8-5 \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u5404\u5c42\u8282\u70b9\u6570\u91cf

        \u5982\u56fe 8-5 \u6240\u793a\uff0c\u8282\u70b9\u201c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u201d\u7684\u6700\u5927\u8fed\u4ee3\u6b21\u6570\u7b49\u4e8e\u8be5\u8282\u70b9\u5230\u53f6\u8282\u70b9\u7684\u8ddd\u79bb\uff0c\u800c\u8be5\u8ddd\u79bb\u6b63\u662f\u201c\u8282\u70b9\u9ad8\u5ea6\u201d\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u5404\u5c42\u7684\u201c\u8282\u70b9\u6570\u91cf \\(\\times\\) \u8282\u70b9\u9ad8\u5ea6\u201d\u6c42\u548c\uff0c\u5f97\u5230\u6240\u6709\u8282\u70b9\u7684\u5806\u5316\u8fed\u4ee3\u6b21\u6570\u7684\u603b\u548c\u3002

        \\[ T(h) = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{(h-1)}\\times1 \\]

        \u5316\u7b80\u4e0a\u5f0f\u9700\u8981\u501f\u52a9\u4e2d\u5b66\u7684\u6570\u5217\u77e5\u8bc6\uff0c\u5148\u5c06 \\(T(h)\\) \u4e58\u4ee5 \\(2\\) \uff0c\u5f97\u5230\uff1a

        \\[ \\begin{aligned} T(h) & = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{h-1}\\times1 \\newline 2 T(h) & = 2^1h + 2^2(h-1) + 2^3(h-2) + \\dots + 2^{h}\\times1 \\newline \\end{aligned} \\]

        \u4f7f\u7528\u9519\u4f4d\u76f8\u51cf\u6cd5\uff0c\u7528\u4e0b\u5f0f \\(2 T(h)\\) \u51cf\u53bb\u4e0a\u5f0f \\(T(h)\\) \uff0c\u53ef\u5f97\uff1a

        \\[ 2T(h) - T(h) = T(h) = -2^0h + 2^1 + 2^2 + \\dots + 2^{h-1} + 2^h \\]

        \u89c2\u5bdf\u4e0a\u5f0f\uff0c\u53d1\u73b0 \\(T(h)\\) \u662f\u4e00\u4e2a\u7b49\u6bd4\u6570\u5217\uff0c\u53ef\u76f4\u63a5\u4f7f\u7528\u6c42\u548c\u516c\u5f0f\uff0c\u5f97\u5230\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\uff1a

        \\[ \\begin{aligned} T(h) & = 2 \\frac{1 - 2^h}{1 - 2} - h \\newline & = 2^{h+1} - h - 2 \\newline & = O(2^h) \\end{aligned} \\]

        \u8fdb\u4e00\u6b65\uff0c\u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(n = 2^{h+1} - 1\\) \uff0c\u6613\u5f97\u590d\u6742\u5ea6\u4e3a \\(O(2^h) = O(n)\\) \u3002\u4ee5\u4e0a\u63a8\u7b97\u8868\u660e\uff0c\u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002

        "},{"location":"chapter_heap/heap/","title":"8.1 \u00a0 \u5806","text":"

        \u5806\uff08heap\uff09\u662f\u4e00\u79cd\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u4e3b\u8981\u53ef\u5206\u4e3a\u4e24\u79cd\u7c7b\u578b\uff0c\u5982\u56fe 8-1 \u6240\u793a\u3002

        • \u5c0f\u9876\u5806\uff08min heap\uff09\uff1a\u4efb\u610f\u8282\u70b9\u7684\u503c \\(\\leq\\) \u5176\u5b50\u8282\u70b9\u7684\u503c\u3002
        • \u5927\u9876\u5806\uff08max heap\uff09\uff1a\u4efb\u610f\u8282\u70b9\u7684\u503c \\(\\geq\\) \u5176\u5b50\u8282\u70b9\u7684\u503c\u3002

        \u56fe 8-1 \u00a0 \u5c0f\u9876\u5806\u4e0e\u5927\u9876\u5806

        \u5806\u4f5c\u4e3a\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u4e00\u4e2a\u7279\u4f8b\uff0c\u5177\u6709\u4ee5\u4e0b\u7279\u6027\u3002

        • \u6700\u5e95\u5c42\u8282\u70b9\u9760\u5de6\u586b\u5145\uff0c\u5176\u4ed6\u5c42\u7684\u8282\u70b9\u90fd\u88ab\u586b\u6ee1\u3002
        • \u6211\u4eec\u5c06\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\u79f0\u4e3a\u201c\u5806\u9876\u201d\uff0c\u5c06\u5e95\u5c42\u6700\u9760\u53f3\u7684\u8282\u70b9\u79f0\u4e3a\u201c\u5806\u5e95\u201d\u3002
        • \u5bf9\u4e8e\u5927\u9876\u5806\uff08\u5c0f\u9876\u5806\uff09\uff0c\u5806\u9876\u5143\u7d20\uff08\u6839\u8282\u70b9\uff09\u7684\u503c\u662f\u6700\u5927\uff08\u6700\u5c0f\uff09\u7684\u3002
        "},{"location":"chapter_heap/heap/#811","title":"8.1.1 \u00a0 \u5806\u7684\u5e38\u7528\u64cd\u4f5c","text":"

        \u9700\u8981\u6307\u51fa\u7684\u662f\uff0c\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u7684\u662f\u4f18\u5148\u961f\u5217\uff08priority queue\uff09\uff0c\u8fd9\u662f\u4e00\u79cd\u62bd\u8c61\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b9a\u4e49\u4e3a\u5177\u6709\u4f18\u5148\u7ea7\u6392\u5e8f\u7684\u961f\u5217\u3002

        \u5b9e\u9645\u4e0a\uff0c\u5806\u901a\u5e38\u7528\u4e8e\u5b9e\u73b0\u4f18\u5148\u961f\u5217\uff0c\u5927\u9876\u5806\u76f8\u5f53\u4e8e\u5143\u7d20\u6309\u4ece\u5927\u5230\u5c0f\u7684\u987a\u5e8f\u51fa\u961f\u7684\u4f18\u5148\u961f\u5217\u3002\u4ece\u4f7f\u7528\u89d2\u5ea6\u6765\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u201c\u4f18\u5148\u961f\u5217\u201d\u548c\u201c\u5806\u201d\u770b\u4f5c\u7b49\u4ef7\u7684\u6570\u636e\u7ed3\u6784\u3002\u56e0\u6b64\uff0c\u672c\u4e66\u5bf9\u4e24\u8005\u4e0d\u505a\u7279\u522b\u533a\u5206\uff0c\u7edf\u4e00\u79f0\u4f5c\u201c\u5806\u201d\u3002

        \u5806\u7684\u5e38\u7528\u64cd\u4f5c\u89c1\u8868 8-1 \uff0c\u65b9\u6cd5\u540d\u9700\u8981\u6839\u636e\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002

        \u8868 8-1 \u00a0 \u5806\u7684\u64cd\u4f5c\u6548\u7387

        \u65b9\u6cd5\u540d \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push() \u5143\u7d20\u5165\u5806 \\(O(\\log n)\\) pop() \u5806\u9876\u5143\u7d20\u51fa\u5806 \\(O(\\log n)\\) peek() \u8bbf\u95ee\u5806\u9876\u5143\u7d20\uff08\u5bf9\u4e8e\u5927 / \u5c0f\u9876\u5806\u5206\u522b\u4e3a\u6700\u5927 / \u5c0f\u503c\uff09 \\(O(1)\\) size() \u83b7\u53d6\u5806\u7684\u5143\u7d20\u6570\u91cf \\(O(1)\\) isEmpty() \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a \\(O(1)\\)

        \u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u7684\u5806\u7c7b\uff08\u6216\u4f18\u5148\u961f\u5217\u7c7b\uff09\u3002

        \u7c7b\u4f3c\u4e8e\u6392\u5e8f\u7b97\u6cd5\u4e2d\u7684\u201c\u4ece\u5c0f\u5230\u5927\u6392\u5217\u201d\u548c\u201c\u4ece\u5927\u5230\u5c0f\u6392\u5217\u201d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a flag \u6216\u4fee\u6539 Comparator \u5b9e\u73b0\u201c\u5c0f\u9876\u5806\u201d\u4e0e\u201c\u5927\u9876\u5806\u201d\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby heap.py
        # \u521d\u59cb\u5316\u5c0f\u9876\u5806\nmin_heap, flag = [], 1\n# \u521d\u59cb\u5316\u5927\u9876\u5806\nmax_heap, flag = [], -1\n\n# Python \u7684 heapq \u6a21\u5757\u9ed8\u8ba4\u5b9e\u73b0\u5c0f\u9876\u5806\n# \u8003\u8651\u5c06\u201c\u5143\u7d20\u53d6\u8d1f\u201d\u540e\u518d\u5165\u5806\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06\u5927\u5c0f\u5173\u7cfb\u98a0\u5012\uff0c\u4ece\u800c\u5b9e\u73b0\u5927\u9876\u5806\n# \u5728\u672c\u793a\u4f8b\u4e2d\uff0cflag = 1 \u65f6\u5bf9\u5e94\u5c0f\u9876\u5806\uff0cflag = -1 \u65f6\u5bf9\u5e94\u5927\u9876\u5806\n\n# \u5143\u7d20\u5165\u5806\nheapq.heappush(max_heap, flag * 1)\nheapq.heappush(max_heap, flag * 3)\nheapq.heappush(max_heap, flag * 2)\nheapq.heappush(max_heap, flag * 5)\nheapq.heappush(max_heap, flag * 4)\n\n# \u83b7\u53d6\u5806\u9876\u5143\u7d20\npeek: int = flag * max_heap[0] # 5\n\n# \u5806\u9876\u5143\u7d20\u51fa\u5806\n# \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nval = flag * heapq.heappop(max_heap) # 5\nval = flag * heapq.heappop(max_heap) # 4\nval = flag * heapq.heappop(max_heap) # 3\nval = flag * heapq.heappop(max_heap) # 2\nval = flag * heapq.heappop(max_heap) # 1\n\n# \u83b7\u53d6\u5806\u5927\u5c0f\nsize: int = len(max_heap)\n\n# \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = not max_heap\n\n# \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806\nmin_heap: list[int] = [1, 3, 2, 5, 4]\nheapq.heapify(min_heap)\n
        heap.cpp
        /* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\npriority_queue<int, vector<int>, greater<int>> minHeap;\n// \u521d\u59cb\u5316\u5927\u9876\u5806\npriority_queue<int, vector<int>, less<int>> maxHeap;\n\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.push(1);\nmaxHeap.push(3);\nmaxHeap.push(2);\nmaxHeap.push(5);\nmaxHeap.push(4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.top(); // 5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nmaxHeap.pop(); // 5\nmaxHeap.pop(); // 4\nmaxHeap.pop(); // 3\nmaxHeap.pop(); // 2\nmaxHeap.pop(); // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.size();\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = maxHeap.empty();\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nvector<int> input{1, 3, 2, 5, 4};\npriority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());\n
        heap.java
        /* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nQueue<Integer> minHeap = new PriorityQueue<>();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\uff08\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);\n\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.offer(1);\nmaxHeap.offer(3);\nmaxHeap.offer(2);\nmaxHeap.offer(5);\nmaxHeap.offer(4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.peek(); // 5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = maxHeap.poll(); // 5\npeek = maxHeap.poll(); // 4\npeek = maxHeap.poll(); // 3\npeek = maxHeap.poll(); // 2\npeek = maxHeap.poll(); // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.size();\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = maxHeap.isEmpty();\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));\n
        heap.cs
        /* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nPriorityQueue<int, int> minHeap = new();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\uff08\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u4fee\u6539 Comparer \u5373\u53ef\uff09\nPriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y.CompareTo(x)));\n\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.Enqueue(1, 1);\nmaxHeap.Enqueue(3, 3);\nmaxHeap.Enqueue(2, 2);\nmaxHeap.Enqueue(5, 5);\nmaxHeap.Enqueue(4, 4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.Peek();//5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = maxHeap.Dequeue();  // 5\npeek = maxHeap.Dequeue();  // 4\npeek = maxHeap.Dequeue();  // 3\npeek = maxHeap.Dequeue();  // 2\npeek = maxHeap.Dequeue();  // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.Count;\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = maxHeap.Count == 0;\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = new PriorityQueue<int, int>([(1, 1), (3, 3), (2, 2), (5, 5), (4, 4)]);\n
        heap.go
        // Go \u8bed\u8a00\u4e2d\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0 heap.Interface \u6765\u6784\u5efa\u6574\u6570\u5927\u9876\u5806\n// \u5b9e\u73b0 heap.Interface \u9700\u8981\u540c\u65f6\u5b9e\u73b0 sort.Interface\ntype intHeap []any\n\n// Push heap.Interface \u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u63a8\u5165\u5143\u7d20\u5230\u5806\nfunc (h *intHeap) Push(x any) {\n    // Push \u548c Pop \u4f7f\u7528 pointer receiver \u4f5c\u4e3a\u53c2\u6570\n    // \u56e0\u4e3a\u5b83\u4eec\u4e0d\u4ec5\u4f1a\u5bf9\u5207\u7247\u7684\u5185\u5bb9\u8fdb\u884c\u8c03\u6574\uff0c\u8fd8\u4f1a\u4fee\u6539\u5207\u7247\u7684\u957f\u5ea6\u3002\n    *h = append(*h, x.(int))\n}\n\n// Pop heap.Interface \u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u5f39\u51fa\u5806\u9876\u5143\u7d20\nfunc (h *intHeap) Pop() any {\n    // \u5f85\u51fa\u5806\u5143\u7d20\u5b58\u653e\u5728\u6700\u540e\n    last := (*h)[len(*h)-1]\n    *h = (*h)[:len(*h)-1]\n    return last\n}\n\n// Len sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Len() int {\n    return len(*h)\n}\n\n// Less sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Less(i, j int) bool {\n    // \u5982\u679c\u5b9e\u73b0\u5c0f\u9876\u5806\uff0c\u5219\u9700\u8981\u8c03\u6574\u4e3a\u5c0f\u4e8e\u53f7\n    return (*h)[i].(int) > (*h)[j].(int)\n}\n\n// Swap sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Swap(i, j int) {\n    (*h)[i], (*h)[j] = (*h)[j], (*h)[i]\n}\n\n// Top \u83b7\u53d6\u5806\u9876\u5143\u7d20\nfunc (h *intHeap) Top() any {\n    return (*h)[0]\n}\n\n/* Driver Code */\nfunc TestHeap(t *testing.T) {\n    /* \u521d\u59cb\u5316\u5806 */\n    // \u521d\u59cb\u5316\u5927\u9876\u5806\n    maxHeap := &intHeap{}\n    heap.Init(maxHeap)\n    /* \u5143\u7d20\u5165\u5806 */\n    // \u8c03\u7528 heap.Interface \u7684\u65b9\u6cd5\uff0c\u6765\u6dfb\u52a0\u5143\u7d20\n    heap.Push(maxHeap, 1)\n    heap.Push(maxHeap, 3)\n    heap.Push(maxHeap, 2)\n    heap.Push(maxHeap, 4)\n    heap.Push(maxHeap, 5)\n\n    /* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\n    top := maxHeap.Top()\n    fmt.Printf(\"\u5806\u9876\u5143\u7d20\u4e3a %d\\n\", top)\n\n    /* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n    // \u8c03\u7528 heap.Interface \u7684\u65b9\u6cd5\uff0c\u6765\u79fb\u9664\u5143\u7d20\n    heap.Pop(maxHeap) // 5\n    heap.Pop(maxHeap) // 4\n    heap.Pop(maxHeap) // 3\n    heap.Pop(maxHeap) // 2\n    heap.Pop(maxHeap) // 1\n\n    /* \u83b7\u53d6\u5806\u5927\u5c0f */\n    size := len(*maxHeap)\n    fmt.Printf(\"\u5806\u5143\u7d20\u6570\u91cf\u4e3a %d\\n\", size)\n\n    /* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\n    isEmpty := len(*maxHeap) == 0\n    fmt.Printf(\"\u5806\u662f\u5426\u4e3a\u7a7a %t\\n\", isEmpty)\n}\n
        heap.swift
        /* \u521d\u59cb\u5316\u5806 */\n// Swift \u7684 Heap \u7c7b\u578b\u540c\u65f6\u652f\u6301\u6700\u5927\u5806\u548c\u6700\u5c0f\u5806\uff0c\u4e14\u9700\u8981\u5f15\u5165 swift-collections\nvar heap = Heap<Int>()\n\n/* \u5143\u7d20\u5165\u5806 */\nheap.insert(1)\nheap.insert(3)\nheap.insert(2)\nheap.insert(5)\nheap.insert(4)\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nvar peek = heap.max()!\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\npeek = heap.removeMax() // 5\npeek = heap.removeMax() // 4\npeek = heap.removeMax() // 3\npeek = heap.removeMax() // 2\npeek = heap.removeMax() // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nlet size = heap.count\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = heap.isEmpty\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nlet heap2 = Heap([1, 3, 2, 5, 4])\n
        heap.js
        // JavaScript \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
        heap.ts
        // TypeScript \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
        heap.dart
        // Dart \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
        heap.rs
        use std::collections::BinaryHeap;\nuse std::cmp::Reverse;\n\n/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nlet mut min_heap = BinaryHeap::<Reverse<i32>>::new();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\nlet mut max_heap = BinaryHeap::new();\n\n/* \u5143\u7d20\u5165\u5806 */\nmax_heap.push(1);\nmax_heap.push(3);\nmax_heap.push(2);\nmax_heap.push(5);\nmax_heap.push(4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nlet peek = max_heap.peek().unwrap();  // 5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nlet peek = max_heap.pop().unwrap();   // 5\nlet peek = max_heap.pop().unwrap();   // 4\nlet peek = max_heap.pop().unwrap();   // 3\nlet peek = max_heap.pop().unwrap();   // 2\nlet peek = max_heap.pop().unwrap();   // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nlet size = max_heap.len();\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = max_heap.is_empty();\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nlet min_heap = BinaryHeap::from(vec![Reverse(1), Reverse(3), Reverse(2), Reverse(5), Reverse(4)]);\n
        heap.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
        heap.kt
        /* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nvar minHeap = PriorityQueue<Int>()\n// \u521d\u59cb\u5316\u5927\u9876\u5806\uff08\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nval maxHeap = PriorityQueue { a: Int, b: Int -> b - a }\n\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.offer(1)\nmaxHeap.offer(3)\nmaxHeap.offer(2)\nmaxHeap.offer(5)\nmaxHeap.offer(4)\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nvar peek = maxHeap.peek() // 5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = maxHeap.poll() // 5\npeek = maxHeap.poll() // 4\npeek = maxHeap.poll() // 3\npeek = maxHeap.poll() // 2\npeek = maxHeap.poll() // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nval size = maxHeap.size\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nval isEmpty = maxHeap.isEmpty()\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = PriorityQueue(mutableListOf(1, 3, 2, 5, 4))\n
        heap.rb
        # Ruby \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_heap/heap/#812","title":"8.1.2 \u00a0 \u5806\u7684\u5b9e\u73b0","text":"

        \u4e0b\u6587\u5b9e\u73b0\u7684\u662f\u5927\u9876\u5806\u3002\u82e5\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u5c0f\u9876\u5806\uff0c\u53ea\u9700\u5c06\u6240\u6709\u5927\u5c0f\u903b\u8f91\u5224\u65ad\u8fdb\u884c\u9006\u8f6c\uff08\u4f8b\u5982\uff0c\u5c06 \\(\\geq\\) \u66ff\u6362\u4e3a \\(\\leq\\) \uff09\u3002\u611f\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u3002

        "},{"location":"chapter_heap/heap/#1","title":"1. \u00a0 \u5806\u7684\u5b58\u50a8\u4e0e\u8868\u793a","text":"

        \u201c\u4e8c\u53c9\u6811\u201d\u7ae0\u8282\u8bb2\u8fc7\uff0c\u5b8c\u5168\u4e8c\u53c9\u6811\u975e\u5e38\u9002\u5408\u7528\u6570\u7ec4\u6765\u8868\u793a\u3002\u7531\u4e8e\u5806\u6b63\u662f\u4e00\u79cd\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u56e0\u6b64\u6211\u4eec\u5c06\u91c7\u7528\u6570\u7ec4\u6765\u5b58\u50a8\u5806\u3002

        \u5f53\u4f7f\u7528\u6570\u7ec4\u8868\u793a\u4e8c\u53c9\u6811\u65f6\uff0c\u5143\u7d20\u4ee3\u8868\u8282\u70b9\u503c\uff0c\u7d22\u5f15\u4ee3\u8868\u8282\u70b9\u5728\u4e8c\u53c9\u6811\u4e2d\u7684\u4f4d\u7f6e\u3002\u8282\u70b9\u6307\u9488\u901a\u8fc7\u7d22\u5f15\u6620\u5c04\u516c\u5f0f\u6765\u5b9e\u73b0\u3002

        \u5982\u56fe 8-2 \u6240\u793a\uff0c\u7ed9\u5b9a\u7d22\u5f15 \\(i\\) \uff0c\u5176\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15\u4e3a \\(2i + 1\\) \uff0c\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15\u4e3a \\(2i + 2\\) \uff0c\u7236\u8282\u70b9\u7684\u7d22\u5f15\u4e3a \\((i - 1) / 2\\)\uff08\u5411\u4e0b\u6574\u9664\uff09\u3002\u5f53\u7d22\u5f15\u8d8a\u754c\u65f6\uff0c\u8868\u793a\u7a7a\u8282\u70b9\u6216\u8282\u70b9\u4e0d\u5b58\u5728\u3002

        \u56fe 8-2 \u00a0 \u5806\u7684\u8868\u793a\u4e0e\u5b58\u50a8

        \u6211\u4eec\u53ef\u4ee5\u5c06\u7d22\u5f15\u6620\u5c04\u516c\u5f0f\u5c01\u88c5\u6210\u51fd\u6570\uff0c\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
        def left(self, i: int) -> int:\n    \"\"\"\u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n    return 2 * i + 1\n\ndef right(self, i: int) -> int:\n    \"\"\"\u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n    return 2 * i + 2\n\ndef parent(self, i: int) -> int:\n    \"\"\"\u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n    return (i - 1) // 2  # \u5411\u4e0b\u6574\u9664\n
        my_heap.cpp
        /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint left(int i) {\n    return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint right(int i) {\n    return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint parent(int i) {\n    return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n
        my_heap.java
        /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint left(int i) {\n    return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint right(int i) {\n    return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint parent(int i) {\n    return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n
        my_heap.cs
        /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint Left(int i) {\n    return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint Right(int i) {\n    return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint Parent(int i) {\n    return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n
        my_heap.go
        /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (h *maxHeap) left(i int) int {\n    return 2*i + 1\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (h *maxHeap) right(i int) int {\n    return 2*i + 2\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (h *maxHeap) parent(i int) int {\n    // \u5411\u4e0b\u6574\u9664\n    return (i - 1) / 2\n}\n
        my_heap.swift
        /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc left(i: Int) -> Int {\n    2 * i + 1\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc right(i: Int) -> Int {\n    2 * i + 2\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc parent(i: Int) -> Int {\n    (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n
        my_heap.js
        /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n#left(i) {\n    return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n#right(i) {\n    return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n#parent(i) {\n    return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n}\n
        my_heap.ts
        /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nleft(i: number): number {\n    return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nright(i: number): number {\n    return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nparent(i: number): number {\n    return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n}\n
        my_heap.dart
        /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint _left(int i) {\n  return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint _right(int i) {\n  return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint _parent(int i) {\n  return (i - 1) ~/ 2; // \u5411\u4e0b\u6574\u9664\n}\n
        my_heap.rs
        /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfn left(i: usize) -> usize {\n    2 * i + 1\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfn right(i: usize) -> usize {\n    2 * i + 2\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfn parent(i: usize) -> usize {\n    (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n
        my_heap.c
        /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint left(MaxHeap *maxHeap, int i) {\n    return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint right(MaxHeap *maxHeap, int i) {\n    return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint parent(MaxHeap *maxHeap, int i) {\n    return (i - 1) / 2; // \u5411\u4e0b\u53d6\u6574\n}\n
        my_heap.kt
        /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfun left(i: Int): Int {\n    return 2 * i + 1\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfun right(i: Int): Int {\n    return 2 * i + 2\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfun parent(i: Int): Int {\n    return (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n
        my_heap.rb
        ### \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 ###\ndef left(i)\n  2 * i + 1\nend\n\n### \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 ###\ndef right(i)\n  2 * i + 2\nend\n\n### \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 ###\ndef parent(i)\n  (i - 1) / 2     # \u5411\u4e0b\u6574\u9664\nend\n
        "},{"location":"chapter_heap/heap/#2","title":"2. \u00a0 \u8bbf\u95ee\u5806\u9876\u5143\u7d20","text":"

        \u5806\u9876\u5143\u7d20\u5373\u4e3a\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\uff0c\u4e5f\u5c31\u662f\u5217\u8868\u7684\u9996\u4e2a\u5143\u7d20\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
        def peek(self) -> int:\n    \"\"\"\u8bbf\u95ee\u5806\u9876\u5143\u7d20\"\"\"\n    return self.max_heap[0]\n
        my_heap.cpp
        /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\n    return maxHeap[0];\n}\n
        my_heap.java
        /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\n    return maxHeap.get(0);\n}\n
        my_heap.cs
        /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint Peek() {\n    return maxHeap[0];\n}\n
        my_heap.go
        /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc (h *maxHeap) peek() any {\n    return h.data[0]\n}\n
        my_heap.swift
        /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc peek() -> Int {\n    maxHeap[0]\n}\n
        my_heap.js
        /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\npeek() {\n    return this.#maxHeap[0];\n}\n
        my_heap.ts
        /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\npeek(): number {\n    return this.maxHeap[0];\n}\n
        my_heap.dart
        /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\n  return _maxHeap[0];\n}\n
        my_heap.rs
        /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfn peek(&self) -> Option<i32> {\n    self.max_heap.first().copied()\n}\n
        my_heap.c
        /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek(MaxHeap *maxHeap) {\n    return maxHeap->data[0];\n}\n
        my_heap.kt
        /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfun peek(): Int {\n    return maxHeap[0]\n}\n
        my_heap.rb
        ### \u8bbf\u95ee\u5806\u9876\u5143\u7d20 ###\ndef peek\n  @max_heap[0]\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_heap/heap/#3","title":"3. \u00a0 \u5143\u7d20\u5165\u5806","text":"

        \u7ed9\u5b9a\u5143\u7d20 val \uff0c\u6211\u4eec\u9996\u5148\u5c06\u5176\u6dfb\u52a0\u5230\u5806\u5e95\u3002\u6dfb\u52a0\u4e4b\u540e\uff0c\u7531\u4e8e val \u53ef\u80fd\u5927\u4e8e\u5806\u4e2d\u5176\u4ed6\u5143\u7d20\uff0c\u5806\u7684\u6210\u7acb\u6761\u4ef6\u53ef\u80fd\u5df2\u88ab\u7834\u574f\uff0c\u56e0\u6b64\u9700\u8981\u4fee\u590d\u4ece\u63d2\u5165\u8282\u70b9\u5230\u6839\u8282\u70b9\u7684\u8def\u5f84\u4e0a\u7684\u5404\u4e2a\u8282\u70b9\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u88ab\u79f0\u4e3a\u5806\u5316\uff08heapify\uff09\u3002

        \u8003\u8651\u4ece\u5165\u5806\u8282\u70b9\u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u6267\u884c\u5806\u5316\u3002\u5982\u56fe 8-3 \u6240\u793a\uff0c\u6211\u4eec\u6bd4\u8f83\u63d2\u5165\u8282\u70b9\u4e0e\u5176\u7236\u8282\u70b9\u7684\u503c\uff0c\u5982\u679c\u63d2\u5165\u8282\u70b9\u66f4\u5927\uff0c\u5219\u5c06\u5b83\u4eec\u4ea4\u6362\u3002\u7136\u540e\u7ee7\u7eed\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u4ece\u5e95\u81f3\u9876\u4fee\u590d\u5806\u4e2d\u7684\u5404\u4e2a\u8282\u70b9\uff0c\u76f4\u81f3\u8d8a\u8fc7\u6839\u8282\u70b9\u6216\u9047\u5230\u65e0\u987b\u4ea4\u6362\u7684\u8282\u70b9\u65f6\u7ed3\u675f\u3002

        <1><2><3><4><5><6><7><8><9>

        \u56fe 8-3 \u00a0 \u5143\u7d20\u5165\u5806\u6b65\u9aa4

        \u8bbe\u8282\u70b9\u603b\u6570\u4e3a \\(n\\) \uff0c\u5219\u6811\u7684\u9ad8\u5ea6\u4e3a \\(O(\\log n)\\) \u3002\u7531\u6b64\u53ef\u77e5\uff0c\u5806\u5316\u64cd\u4f5c\u7684\u5faa\u73af\u8f6e\u6570\u6700\u591a\u4e3a \\(O(\\log n)\\) \uff0c\u5143\u7d20\u5165\u5806\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
        def push(self, val: int):\n    \"\"\"\u5143\u7d20\u5165\u5806\"\"\"\n    # \u6dfb\u52a0\u8282\u70b9\n    self.max_heap.append(val)\n    # \u4ece\u5e95\u81f3\u9876\u5806\u5316\n    self.sift_up(self.size() - 1)\n\ndef sift_up(self, i: int):\n    \"\"\"\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316\"\"\"\n    while True:\n        # \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n        p = self.parent(i)\n        # \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n        if p < 0 or self.max_heap[i] <= self.max_heap[p]:\n            break\n        # \u4ea4\u6362\u4e24\u8282\u70b9\n        self.swap(i, p)\n        # \u5faa\u73af\u5411\u4e0a\u5806\u5316\n        i = p\n
        my_heap.cpp
        /* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n    // \u6dfb\u52a0\u8282\u70b9\n    maxHeap.push_back(val);\n    // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n    siftUp(size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\n    while (true) {\n        // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n        int p = parent(i);\n        // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n        if (p < 0 || maxHeap[i] <= maxHeap[p])\n            break;\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        swap(maxHeap[i], maxHeap[p]);\n        // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n        i = p;\n    }\n}\n
        my_heap.java
        /* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n    // \u6dfb\u52a0\u8282\u70b9\n    maxHeap.add(val);\n    // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n    siftUp(size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\n    while (true) {\n        // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n        int p = parent(i);\n        // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n        if (p < 0 || maxHeap.get(i) <= maxHeap.get(p))\n            break;\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        swap(i, p);\n        // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n        i = p;\n    }\n}\n
        my_heap.cs
        /* \u5143\u7d20\u5165\u5806 */\nvoid Push(int val) {\n    // \u6dfb\u52a0\u8282\u70b9\n    maxHeap.Add(val);\n    // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n    SiftUp(Size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid SiftUp(int i) {\n    while (true) {\n        // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n        int p = Parent(i);\n        // \u82e5\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\uff0c\u5219\u7ed3\u675f\u5806\u5316\n        if (p < 0 || maxHeap[i] <= maxHeap[p])\n            break;\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        Swap(i, p);\n        // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n        i = p;\n    }\n}\n
        my_heap.go
        /* \u5143\u7d20\u5165\u5806 */\nfunc (h *maxHeap) push(val any) {\n    // \u6dfb\u52a0\u8282\u70b9\n    h.data = append(h.data, val)\n    // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n    h.siftUp(len(h.data) - 1)\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfunc (h *maxHeap) siftUp(i int) {\n    for true {\n        // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n        p := h.parent(i)\n        // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n        if p < 0 || h.data[i].(int) <= h.data[p].(int) {\n            break\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        h.swap(i, p)\n        // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n        i = p\n    }\n}\n
        my_heap.swift
        /* \u5143\u7d20\u5165\u5806 */\nfunc push(val: Int) {\n    // \u6dfb\u52a0\u8282\u70b9\n    maxHeap.append(val)\n    // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n    siftUp(i: size() - 1)\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfunc siftUp(i: Int) {\n    var i = i\n    while true {\n        // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n        let p = parent(i: i)\n        // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n        if p < 0 || maxHeap[i] <= maxHeap[p] {\n            break\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        swap(i: i, j: p)\n        // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n        i = p\n    }\n}\n
        my_heap.js
        /* \u5143\u7d20\u5165\u5806 */\npush(val) {\n    // \u6dfb\u52a0\u8282\u70b9\n    this.#maxHeap.push(val);\n    // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n    this.#siftUp(this.size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\n#siftUp(i) {\n    while (true) {\n        // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n        const p = this.#parent(i);\n        // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n        if (p < 0 || this.#maxHeap[i] <= this.#maxHeap[p]) break;\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        this.#swap(i, p);\n        // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n        i = p;\n    }\n}\n
        my_heap.ts
        /* \u5143\u7d20\u5165\u5806 */\npush(val: number): void {\n    // \u6dfb\u52a0\u8282\u70b9\n    this.maxHeap.push(val);\n    // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n    this.siftUp(this.size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nsiftUp(i: number): void {\n    while (true) {\n        // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n        const p = this.parent(i);\n        // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n        if (p < 0 || this.maxHeap[i] <= this.maxHeap[p]) break;\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        this.swap(i, p);\n        // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n        i = p;\n    }\n}\n
        my_heap.dart
        /* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n  // \u6dfb\u52a0\u8282\u70b9\n  _maxHeap.add(val);\n  // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n  siftUp(size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\n  while (true) {\n    // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n    int p = _parent(i);\n    // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n    if (p < 0 || _maxHeap[i] <= _maxHeap[p]) {\n      break;\n    }\n    // \u4ea4\u6362\u4e24\u8282\u70b9\n    _swap(i, p);\n    // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n    i = p;\n  }\n}\n
        my_heap.rs
        /* \u5143\u7d20\u5165\u5806 */\nfn push(&mut self, val: i32) {\n    // \u6dfb\u52a0\u8282\u70b9\n    self.max_heap.push(val);\n    // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n    self.sift_up(self.size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfn sift_up(&mut self, mut i: usize) {\n    loop {\n        // \u8282\u70b9 i \u5df2\u7ecf\u662f\u5806\u9876\u8282\u70b9\u4e86\uff0c\u7ed3\u675f\u5806\u5316\n        if i == 0 {\n            break;\n        }\n        // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n        let p = Self::parent(i);\n        // \u5f53\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n        if self.max_heap[i] <= self.max_heap[p] {\n            break;\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        self.swap(i, p);\n        // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n        i = p;\n    }\n}\n
        my_heap.c
        /* \u5143\u7d20\u5165\u5806 */\nvoid push(MaxHeap *maxHeap, int val) {\n    // \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4e0d\u5e94\u8be5\u6dfb\u52a0\u8fd9\u4e48\u591a\u8282\u70b9\n    if (maxHeap->size == MAX_SIZE) {\n        printf(\"heap is full!\");\n        return;\n    }\n    // \u6dfb\u52a0\u8282\u70b9\n    maxHeap->data[maxHeap->size] = val;\n    maxHeap->size++;\n\n    // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n    siftUp(maxHeap, maxHeap->size - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(MaxHeap *maxHeap, int i) {\n    while (true) {\n        // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n        int p = parent(maxHeap, i);\n        // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n        if (p < 0 || maxHeap->data[i] <= maxHeap->data[p]) {\n            break;\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        swap(maxHeap, i, p);\n        // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n        i = p;\n    }\n}\n
        my_heap.kt
        /* \u5143\u7d20\u5165\u5806 */\nfun push(_val: Int) {\n    // \u6dfb\u52a0\u8282\u70b9\n    maxHeap.add(_val)\n    // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n    siftUp(size() - 1)\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfun siftUp(it: Int) {\n    // Kotlin\u7684\u51fd\u6570\u53c2\u6570\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u521b\u5efa\u4e34\u65f6\u53d8\u91cf\n    var i = it\n    while (true) {\n        // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n        val p = parent(i)\n        // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n        if (p < 0 || maxHeap[i] <= maxHeap[p]) break\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        swap(i, p)\n        // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n        i = p\n    }\n}\n
        my_heap.rb
        ### \u5143\u7d20\u5165\u5806 ###\ndef push(val)\n  # \u6dfb\u52a0\u8282\u70b9\n  @max_heap << val\n  # \u4ece\u5e95\u81f3\u9876\u5806\u5316\n  sift_up(size - 1)\nend\n\n### \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 ###\ndef sift_up(i)\n  loop do\n    # \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n    p = parent(i)\n    # \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n    break if p < 0 || @max_heap[i] <= @max_heap[p]\n    # \u4ea4\u6362\u4e24\u8282\u70b9\n    swap(i, p)\n    # \u5faa\u73af\u5411\u4e0a\u5806\u5316\n    i = p\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_heap/heap/#4","title":"4. \u00a0 \u5806\u9876\u5143\u7d20\u51fa\u5806","text":"

        \u5806\u9876\u5143\u7d20\u662f\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\uff0c\u5373\u5217\u8868\u9996\u5143\u7d20\u3002\u5982\u679c\u6211\u4eec\u76f4\u63a5\u4ece\u5217\u8868\u4e2d\u5220\u9664\u9996\u5143\u7d20\uff0c\u90a3\u4e48\u4e8c\u53c9\u6811\u4e2d\u6240\u6709\u8282\u70b9\u7684\u7d22\u5f15\u90fd\u4f1a\u53d1\u751f\u53d8\u5316\uff0c\u8fd9\u5c06\u4f7f\u5f97\u540e\u7eed\u4f7f\u7528\u5806\u5316\u8fdb\u884c\u4fee\u590d\u53d8\u5f97\u56f0\u96be\u3002\u4e3a\u4e86\u5c3d\u91cf\u51cf\u5c11\u5143\u7d20\u7d22\u5f15\u7684\u53d8\u52a8\uff0c\u6211\u4eec\u91c7\u7528\u4ee5\u4e0b\u64cd\u4f5c\u6b65\u9aa4\u3002

        1. \u4ea4\u6362\u5806\u9876\u5143\u7d20\u4e0e\u5806\u5e95\u5143\u7d20\uff08\u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff09\u3002
        2. \u4ea4\u6362\u5b8c\u6210\u540e\uff0c\u5c06\u5806\u5e95\u4ece\u5217\u8868\u4e2d\u5220\u9664\uff08\u6ce8\u610f\uff0c\u7531\u4e8e\u5df2\u7ecf\u4ea4\u6362\uff0c\u56e0\u6b64\u5b9e\u9645\u4e0a\u5220\u9664\u7684\u662f\u539f\u6765\u7684\u5806\u9876\u5143\u7d20\uff09\u3002
        3. \u4ece\u6839\u8282\u70b9\u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u6267\u884c\u5806\u5316\u3002

        \u5982\u56fe 8-4 \u6240\u793a\uff0c\u201c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u201d\u7684\u64cd\u4f5c\u65b9\u5411\u4e0e\u201c\u4ece\u5e95\u81f3\u9876\u5806\u5316\u201d\u76f8\u53cd\uff0c\u6211\u4eec\u5c06\u6839\u8282\u70b9\u7684\u503c\u4e0e\u5176\u4e24\u4e2a\u5b50\u8282\u70b9\u7684\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u5c06\u6700\u5927\u7684\u5b50\u8282\u70b9\u4e0e\u6839\u8282\u70b9\u4ea4\u6362\u3002\u7136\u540e\u5faa\u73af\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u76f4\u5230\u8d8a\u8fc7\u53f6\u8282\u70b9\u6216\u9047\u5230\u65e0\u987b\u4ea4\u6362\u7684\u8282\u70b9\u65f6\u7ed3\u675f\u3002

        <1><2><3><4><5><6><7><8><9><10>

        \u56fe 8-4 \u00a0 \u5806\u9876\u5143\u7d20\u51fa\u5806\u6b65\u9aa4

        \u4e0e\u5143\u7d20\u5165\u5806\u64cd\u4f5c\u76f8\u4f3c\uff0c\u5806\u9876\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4e3a \\(O(\\log n)\\) \u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
        def pop(self) -> int:\n    \"\"\"\u5143\u7d20\u51fa\u5806\"\"\"\n    # \u5224\u7a7a\u5904\u7406\n    if self.is_empty():\n        raise IndexError(\"\u5806\u4e3a\u7a7a\")\n    # \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    self.swap(0, self.size() - 1)\n    # \u5220\u9664\u8282\u70b9\n    val = self.max_heap.pop()\n    # \u4ece\u9876\u81f3\u5e95\u5806\u5316\n    self.sift_down(0)\n    # \u8fd4\u56de\u5806\u9876\u5143\u7d20\n    return val\n\ndef sift_down(self, i: int):\n    \"\"\"\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\"\"\"\n    while True:\n        # \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        l, r, ma = self.left(i), self.right(i), i\n        if l < self.size() and self.max_heap[l] > self.max_heap[ma]:\n            ma = l\n        if r < self.size() and self.max_heap[r] > self.max_heap[ma]:\n            ma = r\n        # \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if ma == i:\n            break\n        # \u4ea4\u6362\u4e24\u8282\u70b9\n        self.swap(i, ma)\n        # \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma\n
        my_heap.cpp
        /* \u5143\u7d20\u51fa\u5806 */\nvoid pop() {\n    // \u5224\u7a7a\u5904\u7406\n    if (isEmpty()) {\n        throw out_of_range(\"\u5806\u4e3a\u7a7a\");\n    }\n    // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    swap(maxHeap[0], maxHeap[size() - 1]);\n    // \u5220\u9664\u8282\u70b9\n    maxHeap.pop_back();\n    // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n    siftDown(0);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        int l = left(i), r = right(i), ma = i;\n        if (l < size() && maxHeap[l] > maxHeap[ma])\n            ma = l;\n        if (r < size() && maxHeap[r] > maxHeap[ma])\n            ma = r;\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma == i)\n            break;\n        swap(maxHeap[i], maxHeap[ma]);\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n
        my_heap.java
        /* \u5143\u7d20\u51fa\u5806 */\nint pop() {\n    // \u5224\u7a7a\u5904\u7406\n    if (isEmpty())\n        throw new IndexOutOfBoundsException();\n    // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    swap(0, size() - 1);\n    // \u5220\u9664\u8282\u70b9\n    int val = maxHeap.remove(size() - 1);\n    // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n    siftDown(0);\n    // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n    return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        int l = left(i), r = right(i), ma = i;\n        if (l < size() && maxHeap.get(l) > maxHeap.get(ma))\n            ma = l;\n        if (r < size() && maxHeap.get(r) > maxHeap.get(ma))\n            ma = r;\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma == i)\n            break;\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        swap(i, ma);\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n
        my_heap.cs
        /* \u5143\u7d20\u51fa\u5806 */\nint Pop() {\n    // \u5224\u7a7a\u5904\u7406\n    if (IsEmpty())\n        throw new IndexOutOfRangeException();\n    // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    Swap(0, Size() - 1);\n    // \u5220\u9664\u8282\u70b9\n    int val = maxHeap.Last();\n    maxHeap.RemoveAt(Size() - 1);\n    // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n    SiftDown(0);\n    // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n    return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid SiftDown(int i) {\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        int l = Left(i), r = Right(i), ma = i;\n        if (l < Size() && maxHeap[l] > maxHeap[ma])\n            ma = l;\n        if (r < Size() && maxHeap[r] > maxHeap[ma])\n            ma = r;\n        // \u82e5\u201c\u8282\u70b9 i \u6700\u5927\u201d\u6216\u201c\u8d8a\u8fc7\u53f6\u8282\u70b9\u201d\uff0c\u5219\u7ed3\u675f\u5806\u5316\n        if (ma == i) break;\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        Swap(i, ma);\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n
        my_heap.go
        /* \u5143\u7d20\u51fa\u5806 */\nfunc (h *maxHeap) pop() any {\n    // \u5224\u7a7a\u5904\u7406\n    if h.isEmpty() {\n        fmt.Println(\"error\")\n        return nil\n    }\n    // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    h.swap(0, h.size()-1)\n    // \u5220\u9664\u8282\u70b9\n    val := h.data[len(h.data)-1]\n    h.data = h.data[:len(h.data)-1]\n    // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n    h.siftDown(0)\n\n    // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n    return val\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc (h *maxHeap) siftDown(i int) {\n    for true {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a max\n        l, r, max := h.left(i), h.right(i), i\n        if l < h.size() && h.data[l].(int) > h.data[max].(int) {\n            max = l\n        }\n        if r < h.size() && h.data[r].(int) > h.data[max].(int) {\n            max = r\n        }\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if max == i {\n            break\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        h.swap(i, max)\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = max\n    }\n}\n
        my_heap.swift
        /* \u5143\u7d20\u51fa\u5806 */\nfunc pop() -> Int {\n    // \u5224\u7a7a\u5904\u7406\n    if isEmpty() {\n        fatalError(\"\u5806\u4e3a\u7a7a\")\n    }\n    // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    swap(i: 0, j: size() - 1)\n    // \u5220\u9664\u8282\u70b9\n    let val = maxHeap.remove(at: size() - 1)\n    // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n    siftDown(i: 0)\n    // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n    return val\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc siftDown(i: Int) {\n    var i = i\n    while true {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        let l = left(i: i)\n        let r = right(i: i)\n        var ma = i\n        if l < size(), maxHeap[l] > maxHeap[ma] {\n            ma = l\n        }\n        if r < size(), maxHeap[r] > maxHeap[ma] {\n            ma = r\n        }\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if ma == i {\n            break\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        swap(i: i, j: ma)\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma\n    }\n}\n
        my_heap.js
        /* \u5143\u7d20\u51fa\u5806 */\npop() {\n    // \u5224\u7a7a\u5904\u7406\n    if (this.isEmpty()) throw new Error('\u5806\u4e3a\u7a7a');\n    // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    this.#swap(0, this.size() - 1);\n    // \u5220\u9664\u8282\u70b9\n    const val = this.#maxHeap.pop();\n    // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n    this.#siftDown(0);\n    // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n    return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\n#siftDown(i) {\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        const l = this.#left(i),\n            r = this.#right(i);\n        let ma = i;\n        if (l < this.size() && this.#maxHeap[l] > this.#maxHeap[ma]) ma = l;\n        if (r < this.size() && this.#maxHeap[r] > this.#maxHeap[ma]) ma = r;\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma === i) break;\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        this.#swap(i, ma);\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n
        my_heap.ts
        /* \u5143\u7d20\u51fa\u5806 */\npop(): number {\n    // \u5224\u7a7a\u5904\u7406\n    if (this.isEmpty()) throw new RangeError('Heap is empty.');\n    // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    this.swap(0, this.size() - 1);\n    // \u5220\u9664\u8282\u70b9\n    const val = this.maxHeap.pop();\n    // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n    this.siftDown(0);\n    // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n    return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nsiftDown(i: number): void {\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        const l = this.left(i),\n            r = this.right(i);\n        let ma = i;\n        if (l < this.size() && this.maxHeap[l] > this.maxHeap[ma]) ma = l;\n        if (r < this.size() && this.maxHeap[r] > this.maxHeap[ma]) ma = r;\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma === i) break;\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        this.swap(i, ma);\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n
        my_heap.dart
        /* \u5143\u7d20\u51fa\u5806 */\nint pop() {\n  // \u5224\u7a7a\u5904\u7406\n  if (isEmpty()) throw Exception('\u5806\u4e3a\u7a7a');\n  // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n  _swap(0, size() - 1);\n  // \u5220\u9664\u8282\u70b9\n  int val = _maxHeap.removeLast();\n  // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n  siftDown(0);\n  // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n  return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\n  while (true) {\n    // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n    int l = _left(i);\n    int r = _right(i);\n    int ma = i;\n    if (l < size() && _maxHeap[l] > _maxHeap[ma]) ma = l;\n    if (r < size() && _maxHeap[r] > _maxHeap[ma]) ma = r;\n    // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n    if (ma == i) break;\n    // \u4ea4\u6362\u4e24\u8282\u70b9\n    _swap(i, ma);\n    // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n    i = ma;\n  }\n}\n
        my_heap.rs
        /* \u5143\u7d20\u51fa\u5806 */\nfn pop(&mut self) -> i32 {\n    // \u5224\u7a7a\u5904\u7406\n    if self.is_empty() {\n        panic!(\"index out of bounds\");\n    }\n    // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    self.swap(0, self.size() - 1);\n    // \u5220\u9664\u8282\u70b9\n    let val = self.max_heap.pop().unwrap();\n    // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n    self.sift_down(0);\n    // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n    val\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfn sift_down(&mut self, mut i: usize) {\n    loop {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        let (l, r, mut ma) = (Self::left(i), Self::right(i), i);\n        if l < self.size() && self.max_heap[l] > self.max_heap[ma] {\n            ma = l;\n        }\n        if r < self.size() && self.max_heap[r] > self.max_heap[ma] {\n            ma = r;\n        }\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if ma == i {\n            break;\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        self.swap(i, ma);\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n
        my_heap.c
        /* \u5143\u7d20\u51fa\u5806 */\nint pop(MaxHeap *maxHeap) {\n    // \u5224\u7a7a\u5904\u7406\n    if (isEmpty(maxHeap)) {\n        printf(\"heap is empty!\");\n        return INT_MAX;\n    }\n    // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    swap(maxHeap, 0, size(maxHeap) - 1);\n    // \u5220\u9664\u8282\u70b9\n    int val = maxHeap->data[maxHeap->size - 1];\n    maxHeap->size--;\n    // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n    siftDown(maxHeap, 0);\n\n    // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n    return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(MaxHeap *maxHeap, int i) {\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a max\n        int l = left(maxHeap, i);\n        int r = right(maxHeap, i);\n        int max = i;\n        if (l < size(maxHeap) && maxHeap->data[l] > maxHeap->data[max]) {\n            max = l;\n        }\n        if (r < size(maxHeap) && maxHeap->data[r] > maxHeap->data[max]) {\n            max = r;\n        }\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (max == i) {\n            break;\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        swap(maxHeap, i, max);\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = max;\n    }\n}\n
        my_heap.kt
        /* \u5143\u7d20\u51fa\u5806 */\nfun pop(): Int {\n    // \u5224\u7a7a\u5904\u7406\n    if (isEmpty()) throw IndexOutOfBoundsException()\n    // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    swap(0, size() - 1)\n    // \u5220\u9664\u8282\u70b9\n    val _val = maxHeap.removeAt(size() - 1)\n    // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n    siftDown(0)\n    // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n    return _val\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfun siftDown(it: Int) {\n    // Kotlin\u7684\u51fd\u6570\u53c2\u6570\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u521b\u5efa\u4e34\u65f6\u53d8\u91cf\n    var i = it\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        val l = left(i)\n        val r = right(i)\n        var ma = i\n        if (l < size() && maxHeap[l] > maxHeap[ma]) ma = l\n        if (r < size() && maxHeap[r] > maxHeap[ma]) ma = r\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma == i) break\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        swap(i, ma)\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma\n    }\n}\n
        my_heap.rb
        ### \u5143\u7d20\u51fa\u5806 ###\ndef pop\n  # \u5224\u7a7a\u5904\u7406\n  raise IndexError, \"\u5806\u4e3a\u7a7a\" if is_empty?\n  # \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n  swap(0, size - 1)\n  # \u5220\u9664\u8282\u70b9\n  val = @max_heap.pop\n  # \u4ece\u9876\u81f3\u5e95\u5806\u5316\n  sift_down(0)\n  # \u8fd4\u56de\u5806\u9876\u5143\u7d20\n  val\nend\n\n### \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 ###\ndef sift_down(i)\n  loop do\n    # \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n    l, r, ma = left(i), right(i), i\n    ma = l if l < size && @max_heap[l] > @max_heap[ma]\n    ma = r if r < size && @max_heap[r] > @max_heap[ma]\n\n    # \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n    break if ma == i\n\n    # \u4ea4\u6362\u4e24\u8282\u70b9\n    swap(i, ma)\n    # \u5faa\u73af\u5411\u4e0b\u5806\u5316\n    i = ma\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_heap/heap/#813","title":"8.1.3 \u00a0 \u5806\u7684\u5e38\u89c1\u5e94\u7528","text":"
        • \u4f18\u5148\u961f\u5217\uff1a\u5806\u901a\u5e38\u4f5c\u4e3a\u5b9e\u73b0\u4f18\u5148\u961f\u5217\u7684\u9996\u9009\u6570\u636e\u7ed3\u6784\uff0c\u5176\u5165\u961f\u548c\u51fa\u961f\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(\\log n)\\) \uff0c\u800c\u5efa\u5806\u64cd\u4f5c\u4e3a \\(O(n)\\) \uff0c\u8fd9\u4e9b\u64cd\u4f5c\u90fd\u975e\u5e38\u9ad8\u6548\u3002
        • \u5806\u6392\u5e8f\uff1a\u7ed9\u5b9a\u4e00\u7ec4\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u5b83\u4eec\u5efa\u7acb\u4e00\u4e2a\u5806\uff0c\u7136\u540e\u4e0d\u65ad\u5730\u6267\u884c\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\uff0c\u4ece\u800c\u5f97\u5230\u6709\u5e8f\u6570\u636e\u3002\u7136\u800c\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528\u4e00\u79cd\u66f4\u4f18\u96c5\u7684\u65b9\u5f0f\u5b9e\u73b0\u5806\u6392\u5e8f\uff0c\u8be6\u89c1\u201c\u5806\u6392\u5e8f\u201d\u7ae0\u8282\u3002
        • \u83b7\u53d6\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\uff1a\u8fd9\u662f\u4e00\u4e2a\u7ecf\u5178\u7684\u7b97\u6cd5\u95ee\u9898\uff0c\u540c\u65f6\u4e5f\u662f\u4e00\u79cd\u5178\u578b\u5e94\u7528\uff0c\u4f8b\u5982\u9009\u62e9\u70ed\u5ea6\u524d 10 \u7684\u65b0\u95fb\u4f5c\u4e3a\u5fae\u535a\u70ed\u641c\uff0c\u9009\u53d6\u9500\u91cf\u524d 10 \u7684\u5546\u54c1\u7b49\u3002
        "},{"location":"chapter_heap/summary/","title":"8.4 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_heap/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u5806\u662f\u4e00\u68f5\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u6839\u636e\u6210\u7acb\u6761\u4ef6\u53ef\u5206\u4e3a\u5927\u9876\u5806\u548c\u5c0f\u9876\u5806\u3002\u5927\uff08\u5c0f\uff09\u9876\u5806\u7684\u5806\u9876\u5143\u7d20\u662f\u6700\u5927\uff08\u5c0f\uff09\u7684\u3002
        • \u4f18\u5148\u961f\u5217\u7684\u5b9a\u4e49\u662f\u5177\u6709\u51fa\u961f\u4f18\u5148\u7ea7\u7684\u961f\u5217\uff0c\u901a\u5e38\u4f7f\u7528\u5806\u6765\u5b9e\u73b0\u3002
        • \u5806\u7684\u5e38\u7528\u64cd\u4f5c\u53ca\u5176\u5bf9\u5e94\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5305\u62ec\uff1a\u5143\u7d20\u5165\u5806 \\(O(\\log n)\\)\u3001\u5806\u9876\u5143\u7d20\u51fa\u5806 \\(O(\\log n)\\) \u548c\u8bbf\u95ee\u5806\u9876\u5143\u7d20 \\(O(1)\\) \u7b49\u3002
        • \u5b8c\u5168\u4e8c\u53c9\u6811\u975e\u5e38\u9002\u5408\u7528\u6570\u7ec4\u8868\u793a\uff0c\u56e0\u6b64\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u6570\u7ec4\u6765\u5b58\u50a8\u5806\u3002
        • \u5806\u5316\u64cd\u4f5c\u7528\u4e8e\u7ef4\u62a4\u5806\u7684\u6027\u8d28\uff0c\u5728\u5165\u5806\u548c\u51fa\u5806\u64cd\u4f5c\u4e2d\u90fd\u4f1a\u7528\u5230\u3002
        • \u8f93\u5165 \\(n\\) \u4e2a\u5143\u7d20\u5e76\u5efa\u5806\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f18\u5316\u81f3 \\(O(n)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002
        • Top-k \u662f\u4e00\u4e2a\u7ecf\u5178\u7b97\u6cd5\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u5806\u6570\u636e\u7ed3\u6784\u9ad8\u6548\u89e3\u51b3\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log k)\\) \u3002
        "},{"location":"chapter_heap/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u6570\u636e\u7ed3\u6784\u7684\u201c\u5806\u201d\u4e0e\u5185\u5b58\u7ba1\u7406\u7684\u201c\u5806\u201d\u662f\u540c\u4e00\u4e2a\u6982\u5ff5\u5417\uff1f

        \u4e24\u8005\u4e0d\u662f\u540c\u4e00\u4e2a\u6982\u5ff5\uff0c\u53ea\u662f\u78b0\u5de7\u90fd\u53eb\u201c\u5806\u201d\u3002\u8ba1\u7b97\u673a\u7cfb\u7edf\u5185\u5b58\u4e2d\u7684\u5806\u662f\u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u4e00\u90e8\u5206\uff0c\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5b58\u50a8\u6570\u636e\u3002\u7a0b\u5e8f\u53ef\u4ee5\u8bf7\u6c42\u4e00\u5b9a\u91cf\u7684\u5806\u5185\u5b58\uff0c\u7528\u4e8e\u5b58\u50a8\u5982\u5bf9\u8c61\u548c\u6570\u7ec4\u7b49\u590d\u6742\u7ed3\u6784\u3002\u5f53\u8fd9\u4e9b\u6570\u636e\u4e0d\u518d\u9700\u8981\u65f6\uff0c\u7a0b\u5e8f\u9700\u8981\u91ca\u653e\u8fd9\u4e9b\u5185\u5b58\uff0c\u4ee5\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\u3002\u76f8\u8f83\u4e8e\u6808\u5185\u5b58\uff0c\u5806\u5185\u5b58\u7684\u7ba1\u7406\u548c\u4f7f\u7528\u9700\u8981\u66f4\u8c28\u614e\uff0c\u4f7f\u7528\u4e0d\u5f53\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u548c\u91ce\u6307\u9488\u7b49\u95ee\u9898\u3002

        "},{"location":"chapter_heap/top_k/","title":"8.3 \u00a0 Top-k \u95ee\u9898","text":"

        Question

        \u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u65e0\u5e8f\u6570\u7ec4 nums \uff0c\u8bf7\u8fd4\u56de\u6570\u7ec4\u4e2d\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\u3002

        \u5bf9\u4e8e\u8be5\u95ee\u9898\uff0c\u6211\u4eec\u5148\u4ecb\u7ecd\u4e24\u79cd\u601d\u8def\u6bd4\u8f83\u76f4\u63a5\u7684\u89e3\u6cd5\uff0c\u518d\u4ecb\u7ecd\u6548\u7387\u66f4\u9ad8\u7684\u5806\u89e3\u6cd5\u3002

        "},{"location":"chapter_heap/top_k/#831","title":"8.3.1 \u00a0 \u65b9\u6cd5\u4e00\uff1a\u904d\u5386\u9009\u62e9","text":"

        \u6211\u4eec\u53ef\u4ee5\u8fdb\u884c\u56fe 8-6 \u6240\u793a\u7684 \\(k\\) \u8f6e\u904d\u5386\uff0c\u5206\u522b\u5728\u6bcf\u8f6e\u4e2d\u63d0\u53d6\u7b2c \\(1\\)\u3001\\(2\\)\u3001\\(\\dots\\)\u3001\\(k\\) \u5927\u7684\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nk)\\) \u3002

        \u6b64\u65b9\u6cd5\u53ea\u9002\u7528\u4e8e \\(k \\ll n\\) \u7684\u60c5\u51b5\uff0c\u56e0\u4e3a\u5f53 \\(k\\) \u4e0e \\(n\\) \u6bd4\u8f83\u63a5\u8fd1\u65f6\uff0c\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u5411\u4e8e \\(O(n^2)\\) \uff0c\u975e\u5e38\u8017\u65f6\u3002

        \u56fe 8-6 \u00a0 \u904d\u5386\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20

        Tip

        \u5f53 \\(k = n\\) \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u5b8c\u6574\u7684\u6709\u5e8f\u5e8f\u5217\uff0c\u6b64\u65f6\u7b49\u4ef7\u4e8e\u201c\u9009\u62e9\u6392\u5e8f\u201d\u7b97\u6cd5\u3002

        "},{"location":"chapter_heap/top_k/#832","title":"8.3.2 \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u6392\u5e8f","text":"

        \u5982\u56fe 8-7 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u5bf9\u6570\u7ec4 nums \u8fdb\u884c\u6392\u5e8f\uff0c\u518d\u8fd4\u56de\u6700\u53f3\u8fb9\u7684 \\(k\\) \u4e2a\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002

        \u663e\u7136\uff0c\u8be5\u65b9\u6cd5\u201c\u8d85\u989d\u201d\u5b8c\u6210\u4efb\u52a1\u4e86\uff0c\u56e0\u4e3a\u6211\u4eec\u53ea\u9700\u627e\u51fa\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\u5373\u53ef\uff0c\u800c\u4e0d\u9700\u8981\u6392\u5e8f\u5176\u4ed6\u5143\u7d20\u3002

        \u56fe 8-7 \u00a0 \u6392\u5e8f\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20

        "},{"location":"chapter_heap/top_k/#833","title":"8.3.3 \u00a0 \u65b9\u6cd5\u4e09\uff1a\u5806","text":"

        \u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u5806\u66f4\u52a0\u9ad8\u6548\u5730\u89e3\u51b3 Top-k \u95ee\u9898\uff0c\u6d41\u7a0b\u5982\u56fe 8-8 \u6240\u793a\u3002

        1. \u521d\u59cb\u5316\u4e00\u4e2a\u5c0f\u9876\u5806\uff0c\u5176\u5806\u9876\u5143\u7d20\u6700\u5c0f\u3002
        2. \u5148\u5c06\u6570\u7ec4\u7684\u524d \\(k\\) \u4e2a\u5143\u7d20\u4f9d\u6b21\u5165\u5806\u3002
        3. \u4ece\u7b2c \\(k + 1\\) \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\uff0c\u5e76\u5c06\u5f53\u524d\u5143\u7d20\u5165\u5806\u3002
        4. \u904d\u5386\u5b8c\u6210\u540e\uff0c\u5806\u4e2d\u4fdd\u5b58\u7684\u5c31\u662f\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\u3002
        <1><2><3><4><5><6><7><8><9>

        \u56fe 8-8 \u00a0 \u57fa\u4e8e\u5806\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20

        \u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby top_k.py
        def top_k_heap(nums: list[int], k: int) -> list[int]:\n    \"\"\"\u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20\"\"\"\n    # \u521d\u59cb\u5316\u5c0f\u9876\u5806\n    heap = []\n    # \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n    for i in range(k):\n        heapq.heappush(heap, nums[i])\n    # \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n    for i in range(k, len(nums)):\n        # \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n        if nums[i] > heap[0]:\n            heapq.heappop(heap)\n            heapq.heappush(heap, nums[i])\n    return heap\n
        top_k.cpp
        /* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\npriority_queue<int, vector<int>, greater<int>> topKHeap(vector<int> &nums, int k) {\n    // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n    priority_queue<int, vector<int>, greater<int>> heap;\n    // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n    for (int i = 0; i < k; i++) {\n        heap.push(nums[i]);\n    }\n    // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n    for (int i = k; i < nums.size(); i++) {\n        // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n        if (nums[i] > heap.top()) {\n            heap.pop();\n            heap.push(nums[i]);\n        }\n    }\n    return heap;\n}\n
        top_k.java
        /* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nQueue<Integer> topKHeap(int[] nums, int k) {\n    // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n    Queue<Integer> heap = new PriorityQueue<Integer>();\n    // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n    for (int i = 0; i < k; i++) {\n        heap.offer(nums[i]);\n    }\n    // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n    for (int i = k; i < nums.length; i++) {\n        // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n        if (nums[i] > heap.peek()) {\n            heap.poll();\n            heap.offer(nums[i]);\n        }\n    }\n    return heap;\n}\n
        top_k.cs
        /* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nPriorityQueue<int, int> TopKHeap(int[] nums, int k) {\n    // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n    PriorityQueue<int, int> heap = new();\n    // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n    for (int i = 0; i < k; i++) {\n        heap.Enqueue(nums[i], nums[i]);\n    }\n    // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n    for (int i = k; i < nums.Length; i++) {\n        // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n        if (nums[i] > heap.Peek()) {\n            heap.Dequeue();\n            heap.Enqueue(nums[i], nums[i]);\n        }\n    }\n    return heap;\n}\n
        top_k.go
        /* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunc topKHeap(nums []int, k int) *minHeap {\n    // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n    h := &minHeap{}\n    heap.Init(h)\n    // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n    for i := 0; i < k; i++ {\n        heap.Push(h, nums[i])\n    }\n    // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n    for i := k; i < len(nums); i++ {\n        // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n        if nums[i] > h.Top().(int) {\n            heap.Pop(h)\n            heap.Push(h, nums[i])\n        }\n    }\n    return h\n}\n
        top_k.swift
        /* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunc topKHeap(nums: [Int], k: Int) -> [Int] {\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5c0f\u9876\u5806\uff0c\u5e76\u5c06\u524d k \u4e2a\u5143\u7d20\u5efa\u5806\n    var heap = Heap(nums.prefix(k))\n    // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n    for i in nums.indices.dropFirst(k) {\n        // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n        if nums[i] > heap.min()! {\n            _ = heap.removeMin()\n            heap.insert(nums[i])\n        }\n    }\n    return heap.unordered\n}\n
        top_k.js
        /* \u5143\u7d20\u5165\u5806 */\nfunction pushMinHeap(maxHeap, val) {\n    // \u5143\u7d20\u53d6\u53cd\n    maxHeap.push(-val);\n}\n\n/* \u5143\u7d20\u51fa\u5806 */\nfunction popMinHeap(maxHeap) {\n    // \u5143\u7d20\u53d6\u53cd\n    return -maxHeap.pop();\n}\n\n/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunction peekMinHeap(maxHeap) {\n    // \u5143\u7d20\u53d6\u53cd\n    return -maxHeap.peek();\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nfunction getMinHeap(maxHeap) {\n    // \u5143\u7d20\u53d6\u53cd\n    return maxHeap.getMaxHeap().map((num) => -num);\n}\n\n/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunction topKHeap(nums, k) {\n    // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n    // \u8bf7\u6ce8\u610f\uff1a\u6211\u4eec\u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u7528\u5927\u9876\u5806\u6765\u6a21\u62df\u5c0f\u9876\u5806\n    const maxHeap = new MaxHeap([]);\n    // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n    for (let i = 0; i < k; i++) {\n        pushMinHeap(maxHeap, nums[i]);\n    }\n    // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n    for (let i = k; i < nums.length; i++) {\n        // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n        if (nums[i] > peekMinHeap(maxHeap)) {\n            popMinHeap(maxHeap);\n            pushMinHeap(maxHeap, nums[i]);\n        }\n    }\n    // \u8fd4\u56de\u5806\u4e2d\u5143\u7d20\n    return getMinHeap(maxHeap);\n}\n
        top_k.ts
        /* \u5143\u7d20\u5165\u5806 */\nfunction pushMinHeap(maxHeap: MaxHeap, val: number): void {\n    // \u5143\u7d20\u53d6\u53cd\n    maxHeap.push(-val);\n}\n\n/* \u5143\u7d20\u51fa\u5806 */\nfunction popMinHeap(maxHeap: MaxHeap): number {\n    // \u5143\u7d20\u53d6\u53cd\n    return -maxHeap.pop();\n}\n\n/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunction peekMinHeap(maxHeap: MaxHeap): number {\n    // \u5143\u7d20\u53d6\u53cd\n    return -maxHeap.peek();\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nfunction getMinHeap(maxHeap: MaxHeap): number[] {\n    // \u5143\u7d20\u53d6\u53cd\n    return maxHeap.getMaxHeap().map((num: number) => -num);\n}\n\n/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunction topKHeap(nums: number[], k: number): number[] {\n    // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n    // \u8bf7\u6ce8\u610f\uff1a\u6211\u4eec\u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u7528\u5927\u9876\u5806\u6765\u6a21\u62df\u5c0f\u9876\u5806\n    const maxHeap = new MaxHeap([]);\n    // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n    for (let i = 0; i < k; i++) {\n        pushMinHeap(maxHeap, nums[i]);\n    }\n    // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n    for (let i = k; i < nums.length; i++) {\n        // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n        if (nums[i] > peekMinHeap(maxHeap)) {\n            popMinHeap(maxHeap);\n            pushMinHeap(maxHeap, nums[i]);\n        }\n    }\n    // \u8fd4\u56de\u5806\u4e2d\u5143\u7d20\n    return getMinHeap(maxHeap);\n}\n
        top_k.dart
        /* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nMinHeap topKHeap(List<int> nums, int k) {\n  // \u521d\u59cb\u5316\u5c0f\u9876\u5806\uff0c\u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n  MinHeap heap = MinHeap(nums.sublist(0, k));\n  // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n  for (int i = k; i < nums.length; i++) {\n    // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n    if (nums[i] > heap.peek()) {\n      heap.pop();\n      heap.push(nums[i]);\n    }\n  }\n  return heap;\n}\n
        top_k.rs
        /* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfn top_k_heap(nums: Vec<i32>, k: usize) -> BinaryHeap<Reverse<i32>> {\n    // BinaryHeap \u662f\u5927\u9876\u5806\uff0c\u4f7f\u7528 Reverse \u5c06\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u5b9e\u73b0\u5c0f\u9876\u5806\n    let mut heap = BinaryHeap::<Reverse<i32>>::new();\n    // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n    for &num in nums.iter().take(k) {\n        heap.push(Reverse(num));\n    }\n    // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n    for &num in nums.iter().skip(k) {\n        // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n        if num > heap.peek().unwrap().0 {\n            heap.pop();\n            heap.push(Reverse(num));\n        }\n    }\n    heap\n}\n
        top_k.c
        /* \u5143\u7d20\u5165\u5806 */\nvoid pushMinHeap(MaxHeap *maxHeap, int val) {\n    // \u5143\u7d20\u53d6\u53cd\n    push(maxHeap, -val);\n}\n\n/* \u5143\u7d20\u51fa\u5806 */\nint popMinHeap(MaxHeap *maxHeap) {\n    // \u5143\u7d20\u53d6\u53cd\n    return -pop(maxHeap);\n}\n\n/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peekMinHeap(MaxHeap *maxHeap) {\n    // \u5143\u7d20\u53d6\u53cd\n    return -peek(maxHeap);\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nint *getMinHeap(MaxHeap *maxHeap) {\n    // \u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\u5e76\u5b58\u5165 res \u6570\u7ec4\n    int *res = (int *)malloc(maxHeap->size * sizeof(int));\n    for (int i = 0; i < maxHeap->size; i++) {\n        res[i] = -maxHeap->data[i];\n    }\n    return res;\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nint *getMinHeap(MaxHeap *maxHeap) {\n    // \u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\u5e76\u5b58\u5165 res \u6570\u7ec4\n    int *res = (int *)malloc(maxHeap->size * sizeof(int));\n    for (int i = 0; i < maxHeap->size; i++) {\n        res[i] = -maxHeap->data[i];\n    }\n    return res;\n}\n\n// \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20\u7684\u51fd\u6570\nint *topKHeap(int *nums, int sizeNums, int k) {\n    // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n    // \u8bf7\u6ce8\u610f\uff1a\u6211\u4eec\u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u7528\u5927\u9876\u5806\u6765\u6a21\u62df\u5c0f\u9876\u5806\n    int *empty = (int *)malloc(0);\n    MaxHeap *maxHeap = newMaxHeap(empty, 0);\n    // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n    for (int i = 0; i < k; i++) {\n        pushMinHeap(maxHeap, nums[i]);\n    }\n    // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n    for (int i = k; i < sizeNums; i++) {\n        // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n        if (nums[i] > peekMinHeap(maxHeap)) {\n            popMinHeap(maxHeap);\n            pushMinHeap(maxHeap, nums[i]);\n        }\n    }\n    int *res = getMinHeap(maxHeap);\n    // \u91ca\u653e\u5185\u5b58\n    delMaxHeap(maxHeap);\n    return res;\n}\n
        top_k.kt
        /* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfun topKHeap(nums: IntArray, k: Int): Queue<Int> {\n    // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n    val heap = PriorityQueue<Int>()\n    // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n    for (i in 0..<k) {\n        heap.offer(nums[i])\n    }\n    // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n    for (i in k..<nums.size) {\n        // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n        if (nums[i] > heap.peek()) {\n            heap.poll()\n            heap.offer(nums[i])\n        }\n    }\n    return heap\n}\n
        top_k.rb
        ### \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 ###\ndef top_k_heap(nums, k)\n  # \u521d\u59cb\u5316\u5c0f\u9876\u5806\n  # \u8bf7\u6ce8\u610f\uff1a\u6211\u4eec\u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u7528\u5927\u9876\u5806\u6765\u6a21\u62df\u5c0f\u9876\u5806\n  max_heap = MaxHeap.new([])\n\n  # \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n  for i in 0...k\n    push_min_heap(max_heap, nums[i])\n  end\n\n  # \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n  for i in k...nums.length\n    # \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n    if nums[i] > peek_min_heap(max_heap)\n      pop_min_heap(max_heap)\n      push_min_heap(max_heap, nums[i])\n    end\n  end\n\n  get_min_heap(max_heap)\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u603b\u5171\u6267\u884c\u4e86 \\(n\\) \u8f6e\u5165\u5806\u548c\u51fa\u5806\uff0c\u5806\u7684\u6700\u5927\u957f\u5ea6\u4e3a \\(k\\) \uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log k)\\) \u3002\u8be5\u65b9\u6cd5\u7684\u6548\u7387\u5f88\u9ad8\uff0c\u5f53 \\(k\\) \u8f83\u5c0f\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u5411 \\(O(n)\\) \uff1b\u5f53 \\(k\\) \u8f83\u5927\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u4f1a\u8d85\u8fc7 \\(O(n \\log n)\\) \u3002

        \u53e6\u5916\uff0c\u8be5\u65b9\u6cd5\u9002\u7528\u4e8e\u52a8\u6001\u6570\u636e\u6d41\u7684\u4f7f\u7528\u573a\u666f\u3002\u5728\u4e0d\u65ad\u52a0\u5165\u6570\u636e\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u6301\u7eed\u7ef4\u62a4\u5806\u5185\u7684\u5143\u7d20\uff0c\u4ece\u800c\u5b9e\u73b0\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\u7684\u52a8\u6001\u66f4\u65b0\u3002

        "},{"location":"chapter_hello_algo/","title":"\u5e8f","text":"

        \u51e0\u5e74\u524d\uff0c\u6211\u5728\u529b\u6263\u4e0a\u5206\u4eab\u4e86\u201c\u5251\u6307 Offer\u201d\u7cfb\u5217\u9898\u89e3\uff0c\u53d7\u5230\u4e86\u8bb8\u591a\u8bfb\u8005\u7684\u9f13\u52b1\u548c\u652f\u6301\u3002\u5728\u4e0e\u8bfb\u8005\u4ea4\u6d41\u671f\u95f4\uff0c\u6211\u6700\u5e38\u88ab\u95ee\u7684\u4e00\u4e2a\u95ee\u9898\u662f\u201c\u5982\u4f55\u5165\u95e8\u7b97\u6cd5\u201d\u3002\u9010\u6e10\u5730\uff0c\u6211\u5bf9\u8fd9\u4e2a\u95ee\u9898\u4ea7\u751f\u4e86\u6d53\u539a\u7684\u5174\u8da3\u3002

        \u4e24\u773c\u4e00\u62b9\u9ed1\u5730\u5237\u9898\u4f3c\u4e4e\u662f\u6700\u53d7\u6b22\u8fce\u7684\u65b9\u6cd5\uff0c\u7b80\u5355\u3001\u76f4\u63a5\u4e14\u6709\u6548\u3002\u7136\u800c\u5237\u9898\u5c31\u5982\u540c\u73a9\u201c\u626b\u96f7\u201d\u6e38\u620f\uff0c\u81ea\u5b66\u80fd\u529b\u5f3a\u7684\u4eba\u80fd\u591f\u987a\u5229\u5c06\u5730\u96f7\u9010\u4e2a\u6392\u6389\uff0c\u800c\u57fa\u7840\u4e0d\u8db3\u7684\u4eba\u5f88\u53ef\u80fd\u88ab\u70b8\u5f97\u6ee1\u5934\u662f\u5305\uff0c\u5e76\u5728\u632b\u6298\u4e2d\u6b65\u6b65\u9000\u7f29\u3002\u901a\u8bfb\u6559\u6750\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u505a\u6cd5\uff0c\u4f46\u5bf9\u4e8e\u9762\u5411\u6c42\u804c\u7684\u4eba\u6765\u8bf4\uff0c\u6bd5\u4e1a\u8bba\u6587\u3001\u6295\u9012\u7b80\u5386\u3001\u51c6\u5907\u7b14\u8bd5\u548c\u9762\u8bd5\u5df2\u7ecf\u6d88\u8017\u4e86\u5927\u90e8\u5206\u7cbe\u529b\uff0c\u5543\u539a\u91cd\u7684\u4e66\u5f80\u5f80\u53d8\u6210\u4e86\u4e00\u9879\u8270\u5de8\u7684\u6311\u6218\u3002

        \u5982\u679c\u4f60\u4e5f\u9762\u4e34\u7c7b\u4f3c\u7684\u56f0\u6270\uff0c\u90a3\u4e48\u5f88\u5e78\u8fd0\u8fd9\u672c\u4e66\u201c\u627e\u201d\u5230\u4e86\u4f60\u3002\u672c\u4e66\u662f\u6211\u5bf9\u8fd9\u4e2a\u95ee\u9898\u7ed9\u51fa\u7684\u7b54\u6848\uff0c\u5373\u4f7f\u4e0d\u662f\u6700\u4f18\u89e3\uff0c\u4e5f\u81f3\u5c11\u662f\u4e00\u6b21\u79ef\u6781\u7684\u5c1d\u8bd5\u3002\u672c\u4e66\u867d\u7136\u4e0d\u8db3\u4ee5\u8ba9\u4f60\u76f4\u63a5\u62ff\u5230 Offer\uff0c\u4f46\u4f1a\u5f15\u5bfc\u4f60\u63a2\u7d22\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u201c\u77e5\u8bc6\u5730\u56fe\u201d\uff0c\u5e26\u4f60\u4e86\u89e3\u4e0d\u540c\u201c\u5730\u96f7\u201d\u7684\u5f62\u72b6\u3001\u5927\u5c0f\u548c\u5206\u5e03\u4f4d\u7f6e\uff0c\u8ba9\u4f60\u638c\u63e1\u5404\u79cd\u201c\u6392\u96f7\u65b9\u6cd5\u201d\u3002\u6709\u4e86\u8fd9\u4e9b\u672c\u9886\uff0c\u76f8\u4fe1\u4f60\u53ef\u4ee5\u66f4\u52a0\u81ea\u5982\u5730\u5237\u9898\u548c\u9605\u8bfb\u6587\u732e\uff0c\u9010\u6b65\u6784\u5efa\u8d77\u5b8c\u6574\u7684\u77e5\u8bc6\u4f53\u7cfb\u3002

        \u6211\u6df1\u6df1\u8d5e\u540c\u8d39\u66fc\u6559\u6388\u6240\u8a00\uff1a\u201cKnowledge isn't free. You have to pay attention.\u201d\u4ece\u8fd9\u4e2a\u610f\u4e49\u4e0a\u770b\uff0c\u8fd9\u672c\u4e66\u5e76\u975e\u5b8c\u5168\u201c\u514d\u8d39\u201d\u3002\u4e3a\u4e86\u4e0d\u8f9c\u8d1f\u4f60\u4e3a\u672c\u4e66\u6240\u4ed8\u51fa\u7684\u5b9d\u8d35\u201c\u6ce8\u610f\u529b\u201d\uff0c\u6211\u4f1a\u7aed\u5c3d\u6240\u80fd\uff0c\u6295\u5165\u6700\u5927\u7684\u201c\u6ce8\u610f\u529b\u201d\u6765\u5b8c\u6210\u672c\u4e66\u7684\u521b\u4f5c\u3002

        \u672c\u4eba\u81ea\u77e5\u5b66\u758f\u624d\u6d45\uff0c\u4e66\u4e2d\u5185\u5bb9\u867d\u7136\u5df2\u7ecf\u8fc7\u4e00\u6bb5\u65f6\u95f4\u7684\u6253\u78e8\uff0c\u4f46\u4e00\u5b9a\u4ecd\u6709\u8bb8\u591a\u9519\u8bef\uff0c\u6073\u8bf7\u5404\u4f4d\u8001\u5e08\u548c\u540c\u5b66\u6279\u8bc4\u6307\u6b63\u3002

        Hello\uff0c\u7b97\u6cd5\uff01

        \u8ba1\u7b97\u673a\u7684\u51fa\u73b0\u7ed9\u4e16\u754c\u5e26\u6765\u4e86\u5de8\u5927\u53d8\u9769\uff0c\u5b83\u51ed\u501f\u9ad8\u901f\u7684\u8ba1\u7b97\u80fd\u529b\u548c\u51fa\u8272\u7684\u53ef\u7f16\u7a0b\u6027\uff0c\u6210\u4e3a\u4e86\u6267\u884c\u7b97\u6cd5\u4e0e\u5904\u7406\u6570\u636e\u7684\u7406\u60f3\u5a92\u4ecb\u3002\u65e0\u8bba\u662f\u7535\u5b50\u6e38\u620f\u7684\u903c\u771f\u753b\u9762\u3001\u81ea\u52a8\u9a7e\u9a76\u7684\u667a\u80fd\u51b3\u7b56\uff0c\u8fd8\u662f AlphaGo \u7684\u7cbe\u5f69\u68cb\u5c40\u3001ChatGPT \u7684\u81ea\u7136\u4ea4\u4e92\uff0c\u8fd9\u4e9b\u5e94\u7528\u90fd\u662f\u7b97\u6cd5\u5728\u8ba1\u7b97\u673a\u4e0a\u7684\u7cbe\u5999\u6f14\u7ece\u3002

        \u4e8b\u5b9e\u4e0a\uff0c\u5728\u8ba1\u7b97\u673a\u95ee\u4e16\u4e4b\u524d\uff0c\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u5c31\u5df2\u7ecf\u5b58\u5728\u4e8e\u4e16\u754c\u7684\u5404\u4e2a\u89d2\u843d\u3002\u65e9\u671f\u7684\u7b97\u6cd5\u76f8\u5bf9\u7b80\u5355\uff0c\u4f8b\u5982\u53e4\u4ee3\u7684\u8ba1\u6570\u65b9\u6cd5\u548c\u5de5\u5177\u5236\u4f5c\u6b65\u9aa4\u7b49\u3002\u968f\u7740\u6587\u660e\u7684\u8fdb\u6b65\uff0c\u7b97\u6cd5\u9010\u6e10\u53d8\u5f97\u66f4\u52a0\u7cbe\u7ec6\u548c\u590d\u6742\u3002\u4ece\u5de7\u593a\u5929\u5de5\u7684\u5320\u4eba\u6280\u827a\u3001\u5230\u89e3\u653e\u751f\u4ea7\u529b\u7684\u5de5\u4e1a\u4ea7\u54c1\u3001\u518d\u5230\u5b87\u5b99\u8fd0\u884c\u7684\u79d1\u5b66\u89c4\u5f8b\uff0c\u51e0\u4e4e\u6bcf\u4e00\u4ef6\u5e73\u51e1\u6216\u4ee4\u4eba\u60ca\u53f9\u7684\u4e8b\u7269\u80cc\u540e\uff0c\u90fd\u9690\u85cf\u7740\u7cbe\u5999\u7684\u7b97\u6cd5\u601d\u60f3\u3002

        \u540c\u6837\uff0c\u6570\u636e\u7ed3\u6784\u65e0\u5904\u4e0d\u5728\uff1a\u5927\u5230\u793e\u4f1a\u7f51\u7edc\uff0c\u5c0f\u5230\u5730\u94c1\u7ebf\u8def\uff0c\u8bb8\u591a\u7cfb\u7edf\u90fd\u53ef\u4ee5\u5efa\u6a21\u4e3a\u201c\u56fe\u201d\uff1b\u5927\u5230\u4e00\u4e2a\u56fd\u5bb6\uff0c\u5c0f\u5230\u4e00\u4e2a\u5bb6\u5ead\uff0c\u793e\u4f1a\u7684\u4e3b\u8981\u7ec4\u7ec7\u5f62\u5f0f\u5448\u73b0\u51fa\u201c\u6811\u201d\u7684\u7279\u5f81\uff1b\u51ac\u5929\u7684\u8863\u670d\u5c31\u50cf\u201c\u6808\u201d\uff0c\u6700\u5148\u7a7f\u4e0a\u7684\u6700\u540e\u624d\u80fd\u8131\u4e0b\uff1b\u7fbd\u6bdb\u7403\u7b52\u5219\u5982\u540c\u201c\u961f\u5217\u201d\uff0c\u4e00\u7aef\u653e\u5165\u3001\u53e6\u4e00\u7aef\u53d6\u51fa\uff1b\u5b57\u5178\u5c31\u50cf\u4e00\u4e2a\u201c\u54c8\u5e0c\u8868\u201d\uff0c\u80fd\u591f\u5feb\u901f\u67e5\u627e\u76ee\u6807\u8bcd\u6761\u3002

        \u672c\u4e66\u65e8\u5728\u901a\u8fc7\u6e05\u6670\u6613\u61c2\u7684\u52a8\u753b\u56fe\u89e3\u548c\u53ef\u8fd0\u884c\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u4f7f\u8bfb\u8005\u7406\u89e3\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u5e76\u80fd\u591f\u901a\u8fc7\u7f16\u7a0b\u6765\u5b9e\u73b0\u5b83\u4eec\u3002\u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u672c\u4e66\u81f4\u529b\u4e8e\u63ed\u793a\u7b97\u6cd5\u5728\u590d\u6742\u4e16\u754c\u4e2d\u7684\u751f\u52a8\u4f53\u73b0\uff0c\u5c55\u73b0\u7b97\u6cd5\u4e4b\u7f8e\u3002\u5e0c\u671b\u672c\u4e66\u80fd\u591f\u5e2e\u52a9\u5230\u4f60\uff01

        "},{"location":"chapter_introduction/","title":"\u7b2c 1 \u7ae0 \u00a0 \u521d\u8bc6\u7b97\u6cd5","text":"

        Abstract

        \u4e00\u4f4d\u5c11\u5973\u7fe9\u7fe9\u8d77\u821e\uff0c\u4e0e\u6570\u636e\u4ea4\u7ec7\u5728\u4e00\u8d77\uff0c\u88d9\u6446\u4e0a\u98d8\u626c\u7740\u7b97\u6cd5\u7684\u65cb\u5f8b\u3002

        \u5979\u9080\u8bf7\u4f60\u5171\u821e\uff0c\u8bf7\u7d27\u8ddf\u5979\u7684\u6b65\u4f10\uff0c\u8e0f\u5165\u5145\u6ee1\u903b\u8f91\u4e0e\u7f8e\u611f\u7684\u7b97\u6cd5\u4e16\u754c\u3002

        "},{"location":"chapter_introduction/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 1.1 \u00a0 \u7b97\u6cd5\u65e0\u5904\u4e0d\u5728
        • 1.2 \u00a0 \u7b97\u6cd5\u662f\u4ec0\u4e48
        • 1.3 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_introduction/algorithms_are_everywhere/","title":"1.1 \u00a0 \u7b97\u6cd5\u65e0\u5904\u4e0d\u5728","text":"

        \u5f53\u6211\u4eec\u542c\u5230\u201c\u7b97\u6cd5\u201d\u8fd9\u4e2a\u8bcd\u65f6\uff0c\u5f88\u81ea\u7136\u5730\u4f1a\u60f3\u5230\u6570\u5b66\u3002\u7136\u800c\u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u7b97\u6cd5\u5e76\u4e0d\u6d89\u53ca\u590d\u6742\u6570\u5b66\uff0c\u800c\u662f\u66f4\u591a\u5730\u4f9d\u8d56\u57fa\u672c\u903b\u8f91\uff0c\u8fd9\u4e9b\u903b\u8f91\u5728\u6211\u4eec\u7684\u65e5\u5e38\u751f\u6d3b\u4e2d\u5904\u5904\u53ef\u89c1\u3002

        \u5728\u6b63\u5f0f\u63a2\u8ba8\u7b97\u6cd5\u4e4b\u524d\uff0c\u6709\u4e00\u4e2a\u6709\u8da3\u7684\u4e8b\u5b9e\u503c\u5f97\u5206\u4eab\uff1a\u4f60\u5df2\u7ecf\u5728\u4e0d\u77e5\u4e0d\u89c9\u4e2d\u5b66\u4f1a\u4e86\u8bb8\u591a\u7b97\u6cd5\uff0c\u5e76\u4e60\u60ef\u5c06\u5b83\u4eec\u5e94\u7528\u5230\u65e5\u5e38\u751f\u6d3b\u4e2d\u4e86\u3002\u4e0b\u9762\u6211\u5c06\u4e3e\u51e0\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u6765\u8bc1\u5b9e\u8fd9\u4e00\u70b9\u3002

        \u4f8b\u4e00\uff1a\u67e5\u5b57\u5178\u3002\u5728\u5b57\u5178\u91cc\uff0c\u6bcf\u4e2a\u6c49\u5b57\u90fd\u5bf9\u5e94\u4e00\u4e2a\u62fc\u97f3\uff0c\u800c\u5b57\u5178\u662f\u6309\u7167\u62fc\u97f3\u5b57\u6bcd\u987a\u5e8f\u6392\u5217\u7684\u3002\u5047\u8bbe\u6211\u4eec\u9700\u8981\u67e5\u627e\u4e00\u4e2a\u62fc\u97f3\u9996\u5b57\u6bcd\u4e3a \\(r\\) \u7684\u5b57\uff0c\u901a\u5e38\u4f1a\u6309\u7167\u56fe 1-1 \u6240\u793a\u7684\u65b9\u5f0f\u5b9e\u73b0\u3002

        1. \u7ffb\u5f00\u5b57\u5178\u7ea6\u4e00\u534a\u7684\u9875\u6570\uff0c\u67e5\u770b\u8be5\u9875\u7684\u9996\u5b57\u6bcd\u662f\u4ec0\u4e48\uff0c\u5047\u8bbe\u9996\u5b57\u6bcd\u4e3a \\(m\\) \u3002
        2. \u7531\u4e8e\u5728\u62fc\u97f3\u5b57\u6bcd\u8868\u4e2d \\(r\\) \u4f4d\u4e8e \\(m\\) \u4e4b\u540e\uff0c\u6240\u4ee5\u6392\u9664\u5b57\u5178\u524d\u534a\u90e8\u5206\uff0c\u67e5\u627e\u8303\u56f4\u7f29\u5c0f\u5230\u540e\u534a\u90e8\u5206\u3002
        3. \u4e0d\u65ad\u91cd\u590d\u6b65\u9aa4 1. \u548c\u6b65\u9aa4 2. \uff0c\u76f4\u81f3\u627e\u5230\u62fc\u97f3\u9996\u5b57\u6bcd\u4e3a \\(r\\) \u7684\u9875\u7801\u4e3a\u6b62\u3002
        <1><2><3><4><5>

        \u56fe 1-1 \u00a0 \u67e5\u5b57\u5178\u6b65\u9aa4

        \u67e5\u5b57\u5178\u8fd9\u4e2a\u5c0f\u5b66\u751f\u5fc5\u5907\u6280\u80fd\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u8457\u540d\u7684\u201c\u4e8c\u5206\u67e5\u627e\u201d\u7b97\u6cd5\u3002\u4ece\u6570\u636e\u7ed3\u6784\u7684\u89d2\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u5b57\u5178\u89c6\u4e3a\u4e00\u4e2a\u5df2\u6392\u5e8f\u7684\u201c\u6570\u7ec4\u201d\uff1b\u4ece\u7b97\u6cd5\u7684\u89d2\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e0a\u8ff0\u67e5\u5b57\u5178\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\u770b\u4f5c\u201c\u4e8c\u5206\u67e5\u627e\u201d\u3002

        \u4f8b\u4e8c\uff1a\u6574\u7406\u6251\u514b\u3002\u6211\u4eec\u5728\u6253\u724c\u65f6\uff0c\u6bcf\u5c40\u90fd\u9700\u8981\u6574\u7406\u624b\u4e2d\u7684\u6251\u514b\u724c\uff0c\u4f7f\u5176\u4ece\u5c0f\u5230\u5927\u6392\u5217\uff0c\u5b9e\u73b0\u6d41\u7a0b\u5982\u56fe 1-2 \u6240\u793a\u3002

        1. \u5c06\u6251\u514b\u724c\u5212\u5206\u4e3a\u201c\u6709\u5e8f\u201d\u548c\u201c\u65e0\u5e8f\u201d\u4e24\u90e8\u5206\uff0c\u5e76\u5047\u8bbe\u521d\u59cb\u72b6\u6001\u4e0b\u6700\u5de6 1 \u5f20\u6251\u514b\u724c\u5df2\u7ecf\u6709\u5e8f\u3002
        2. \u5728\u65e0\u5e8f\u90e8\u5206\u62bd\u51fa\u4e00\u5f20\u6251\u514b\u724c\uff0c\u63d2\u5165\u81f3\u6709\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\uff1b\u5b8c\u6210\u540e\u6700\u5de6 2 \u5f20\u6251\u514b\u5df2\u7ecf\u6709\u5e8f\u3002
        3. \u4e0d\u65ad\u5faa\u73af\u6b65\u9aa4 2. \uff0c\u6bcf\u4e00\u8f6e\u5c06\u4e00\u5f20\u6251\u514b\u724c\u4ece\u65e0\u5e8f\u90e8\u5206\u63d2\u5165\u81f3\u6709\u5e8f\u90e8\u5206\uff0c\u76f4\u81f3\u6240\u6709\u6251\u514b\u724c\u90fd\u6709\u5e8f\u3002

        \u56fe 1-2 \u00a0 \u6251\u514b\u6392\u5e8f\u6b65\u9aa4

        \u4e0a\u8ff0\u6574\u7406\u6251\u514b\u724c\u7684\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u201c\u63d2\u5165\u6392\u5e8f\u201d\u7b97\u6cd5\uff0c\u5b83\u5728\u5904\u7406\u5c0f\u578b\u6570\u636e\u96c6\u65f6\u975e\u5e38\u9ad8\u6548\u3002\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u7684\u6392\u5e8f\u5e93\u51fd\u6570\u4e2d\u90fd\u6709\u63d2\u5165\u6392\u5e8f\u7684\u8eab\u5f71\u3002

        \u4f8b\u4e09\uff1a\u8d27\u5e01\u627e\u96f6\u3002\u5047\u8bbe\u6211\u4eec\u5728\u8d85\u5e02\u8d2d\u4e70\u4e86 \\(69\\) \u5143\u7684\u5546\u54c1\uff0c\u7ed9\u4e86\u6536\u94f6\u5458 \\(100\\) \u5143\uff0c\u5219\u6536\u94f6\u5458\u9700\u8981\u627e\u6211\u4eec \\(31\\) \u5143\u3002\u4ed6\u4f1a\u5f88\u81ea\u7136\u5730\u5b8c\u6210\u5982\u56fe 1-3 \u6240\u793a\u7684\u601d\u8003\u3002

        1. \u53ef\u9009\u9879\u662f\u6bd4 \\(31\\) \u5143\u9762\u503c\u66f4\u5c0f\u7684\u8d27\u5e01\uff0c\u5305\u62ec \\(1\\) \u5143\u3001\\(5\\) \u5143\u3001\\(10\\) \u5143\u3001\\(20\\) \u5143\u3002
        2. \u4ece\u53ef\u9009\u9879\u4e2d\u62ff\u51fa\u6700\u5927\u7684 \\(20\\) \u5143\uff0c\u5269\u4f59 \\(31 - 20 = 11\\) \u5143\u3002
        3. \u4ece\u5269\u4f59\u53ef\u9009\u9879\u4e2d\u62ff\u51fa\u6700\u5927\u7684 \\(10\\) \u5143\uff0c\u5269\u4f59 \\(11 - 10 = 1\\) \u5143\u3002
        4. \u4ece\u5269\u4f59\u53ef\u9009\u9879\u4e2d\u62ff\u51fa\u6700\u5927\u7684 \\(1\\) \u5143\uff0c\u5269\u4f59 \\(1 - 1 = 0\\) \u5143\u3002
        5. \u5b8c\u6210\u627e\u96f6\uff0c\u65b9\u6848\u4e3a \\(20 + 10 + 1 = 31\\) \u5143\u3002

        \u56fe 1-3 \u00a0 \u8d27\u5e01\u627e\u96f6\u8fc7\u7a0b

        \u5728\u4ee5\u4e0a\u6b65\u9aa4\u4e2d\uff0c\u6211\u4eec\u6bcf\u4e00\u6b65\u90fd\u91c7\u53d6\u5f53\u524d\u770b\u6765\u6700\u597d\u7684\u9009\u62e9\uff08\u5c3d\u53ef\u80fd\u7528\u5927\u9762\u989d\u7684\u8d27\u5e01\uff09\uff0c\u6700\u7ec8\u5f97\u5230\u4e86\u53ef\u884c\u7684\u627e\u96f6\u65b9\u6848\u3002\u4ece\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\u8fd9\u79cd\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u201c\u8d2a\u5fc3\u201d\u7b97\u6cd5\u3002

        \u5c0f\u5230\u70f9\u996a\u4e00\u9053\u83dc\uff0c\u5927\u5230\u661f\u9645\u822a\u884c\uff0c\u51e0\u4e4e\u6240\u6709\u95ee\u9898\u7684\u89e3\u51b3\u90fd\u79bb\u4e0d\u5f00\u7b97\u6cd5\u3002\u8ba1\u7b97\u673a\u7684\u51fa\u73b0\u4f7f\u5f97\u6211\u4eec\u80fd\u591f\u901a\u8fc7\u7f16\u7a0b\u5c06\u6570\u636e\u7ed3\u6784\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\uff0c\u540c\u65f6\u7f16\u5199\u4ee3\u7801\u8c03\u7528 CPU \u548c GPU \u6267\u884c\u7b97\u6cd5\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u80fd\u628a\u751f\u6d3b\u4e2d\u7684\u95ee\u9898\u8f6c\u79fb\u5230\u8ba1\u7b97\u673a\u4e0a\uff0c\u4ee5\u66f4\u9ad8\u6548\u7684\u65b9\u5f0f\u89e3\u51b3\u5404\u79cd\u590d\u6742\u95ee\u9898\u3002

        Tip

        \u5982\u679c\u4f60\u5bf9\u6570\u636e\u7ed3\u6784\u3001\u7b97\u6cd5\u3001\u6570\u7ec4\u548c\u4e8c\u5206\u67e5\u627e\u7b49\u6982\u5ff5\u4ecd\u611f\u5230\u4e00\u77e5\u534a\u89e3\uff0c\u8bf7\u7ee7\u7eed\u5f80\u4e0b\u9605\u8bfb\uff0c\u672c\u4e66\u5c06\u5f15\u5bfc\u4f60\u8fc8\u5165\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u77e5\u8bc6\u6bbf\u5802\u3002

        "},{"location":"chapter_introduction/summary/","title":"1.3 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_introduction/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u7b97\u6cd5\u5728\u65e5\u5e38\u751f\u6d3b\u4e2d\u65e0\u5904\u4e0d\u5728\uff0c\u5e76\u4e0d\u662f\u9065\u4e0d\u53ef\u53ca\u7684\u9ad8\u6df1\u77e5\u8bc6\u3002\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u5df2\u7ecf\u5728\u4e0d\u77e5\u4e0d\u89c9\u4e2d\u5b66\u4f1a\u4e86\u8bb8\u591a\u7b97\u6cd5\uff0c\u7528\u4ee5\u89e3\u51b3\u751f\u6d3b\u4e2d\u7684\u5927\u5c0f\u95ee\u9898\u3002
        • \u67e5\u5b57\u5178\u7684\u539f\u7406\u4e0e\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u76f8\u4e00\u81f4\u3002\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u4f53\u73b0\u4e86\u5206\u800c\u6cbb\u4e4b\u7684\u91cd\u8981\u7b97\u6cd5\u601d\u60f3\u3002
        • \u6574\u7406\u6251\u514b\u7684\u8fc7\u7a0b\u4e0e\u63d2\u5165\u6392\u5e8f\u7b97\u6cd5\u975e\u5e38\u7c7b\u4f3c\u3002\u63d2\u5165\u6392\u5e8f\u7b97\u6cd5\u9002\u5408\u6392\u5e8f\u5c0f\u578b\u6570\u636e\u96c6\u3002
        • \u8d27\u5e01\u627e\u96f6\u7684\u6b65\u9aa4\u672c\u8d28\u4e0a\u662f\u8d2a\u5fc3\u7b97\u6cd5\uff0c\u6bcf\u4e00\u6b65\u90fd\u91c7\u53d6\u5f53\u524d\u770b\u6765\u6700\u597d\u7684\u9009\u62e9\u3002
        • \u7b97\u6cd5\u662f\u5728\u6709\u9650\u65f6\u95f4\u5185\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u7684\u4e00\u7ec4\u6307\u4ee4\u6216\u64cd\u4f5c\u6b65\u9aa4\uff0c\u800c\u6570\u636e\u7ed3\u6784\u662f\u8ba1\u7b97\u673a\u4e2d\u7ec4\u7ec7\u548c\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\u3002
        • \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7d27\u5bc6\u76f8\u8fde\u3002\u6570\u636e\u7ed3\u6784\u662f\u7b97\u6cd5\u7684\u57fa\u77f3\uff0c\u800c\u7b97\u6cd5\u4e3a\u6570\u636e\u7ed3\u6784\u6ce8\u5165\u751f\u547d\u529b\u3002
        • \u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7c7b\u6bd4\u4e3a\u62fc\u88c5\u79ef\u6728\uff0c\u79ef\u6728\u4ee3\u8868\u6570\u636e\uff0c\u79ef\u6728\u7684\u5f62\u72b6\u548c\u8fde\u63a5\u65b9\u5f0f\u7b49\u4ee3\u8868\u6570\u636e\u7ed3\u6784\uff0c\u62fc\u88c5\u79ef\u6728\u7684\u6b65\u9aa4\u5219\u5bf9\u5e94\u7b97\u6cd5\u3002
        "},{"location":"chapter_introduction/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u4f5c\u4e3a\u4e00\u540d\u7a0b\u5e8f\u5458\uff0c\u6211\u5728\u65e5\u5e38\u5de5\u4f5c\u4e2d\u4ece\u672a\u7528\u7b97\u6cd5\u89e3\u51b3\u8fc7\u95ee\u9898\uff0c\u5e38\u7528\u7b97\u6cd5\u90fd\u88ab\u7f16\u7a0b\u8bed\u8a00\u5c01\u88c5\u597d\u4e86\uff0c\u76f4\u63a5\u7528\u5c31\u53ef\u4ee5\u4e86\uff1b\u8fd9\u662f\u5426\u610f\u5473\u7740\u6211\u4eec\u5de5\u4f5c\u4e2d\u7684\u95ee\u9898\u8fd8\u6ca1\u6709\u5230\u8fbe\u9700\u8981\u7b97\u6cd5\u7684\u7a0b\u5ea6\uff1f

        \u5982\u679c\u628a\u5177\u4f53\u7684\u5de5\u4f5c\u6280\u80fd\u6bd4\u4f5c\u662f\u6b66\u529f\u7684\u201c\u62db\u5f0f\u201d\u7684\u8bdd\uff0c\u90a3\u4e48\u57fa\u7840\u79d1\u76ee\u5e94\u8be5\u66f4\u50cf\u662f\u201c\u5185\u529f\u201d\u3002

        \u6211\u8ba4\u4e3a\u5b66\u7b97\u6cd5\uff08\u4ee5\u53ca\u5176\u4ed6\u57fa\u7840\u79d1\u76ee\uff09\u7684\u610f\u4e49\u4e0d\u662f\u5728\u4e8e\u5728\u5de5\u4f5c\u4e2d\u4ece\u96f6\u5b9e\u73b0\u5b83\uff0c\u800c\u662f\u57fa\u4e8e\u5b66\u5230\u7684\u77e5\u8bc6\uff0c\u5728\u89e3\u51b3\u95ee\u9898\u65f6\u80fd\u591f\u4f5c\u51fa\u4e13\u4e1a\u7684\u53cd\u5e94\u548c\u5224\u65ad\uff0c\u4ece\u800c\u63d0\u5347\u5de5\u4f5c\u7684\u6574\u4f53\u8d28\u91cf\u3002\u4e3e\u4e00\u4e2a\u7b80\u5355\u4f8b\u5b50\uff0c\u6bcf\u79cd\u7f16\u7a0b\u8bed\u8a00\u90fd\u5185\u7f6e\u4e86\u6392\u5e8f\u51fd\u6570\uff1a

        • \u5982\u679c\u6211\u4eec\u6ca1\u6709\u5b66\u8fc7\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\uff0c\u90a3\u4e48\u7ed9\u5b9a\u4efb\u4f55\u6570\u636e\uff0c\u6211\u4eec\u53ef\u80fd\u90fd\u585e\u7ed9\u8fd9\u4e2a\u6392\u5e8f\u51fd\u6570\u53bb\u505a\u4e86\u3002\u8fd0\u884c\u987a\u7545\u3001\u6027\u80fd\u4e0d\u9519\uff0c\u770b\u4e0a\u53bb\u5e76\u6ca1\u6709\u4ec0\u4e48\u95ee\u9898\u3002
        • \u4f46\u5982\u679c\u5b66\u8fc7\u7b97\u6cd5\uff0c\u6211\u4eec\u5c31\u4f1a\u77e5\u9053\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(n \\log n)\\) \uff1b\u800c\u5982\u679c\u7ed9\u5b9a\u7684\u6570\u636e\u662f\u56fa\u5b9a\u4f4d\u6570\u7684\u6574\u6570\uff08\u4f8b\u5982\u5b66\u53f7\uff09\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u53ef\u4ee5\u7528\u6548\u7387\u66f4\u9ad8\u7684\u201c\u57fa\u6570\u6392\u5e8f\u201d\u6765\u505a\uff0c\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u964d\u4e3a \\(O(nk)\\) \uff0c\u5176\u4e2d \\(k\\) \u4e3a\u4f4d\u6570\u3002\u5f53\u6570\u636e\u4f53\u91cf\u5f88\u5927\u65f6\uff0c\u8282\u7701\u51fa\u6765\u7684\u8fd0\u884c\u65f6\u95f4\u5c31\u80fd\u521b\u9020\u8f83\u5927\u4ef7\u503c\uff08\u6210\u672c\u964d\u4f4e\u3001\u4f53\u9a8c\u53d8\u597d\u7b49\uff09\u3002

        \u5728\u5de5\u7a0b\u9886\u57df\u4e2d\uff0c\u5927\u91cf\u95ee\u9898\u662f\u96be\u4ee5\u8fbe\u5230\u6700\u4f18\u89e3\u7684\uff0c\u8bb8\u591a\u95ee\u9898\u53ea\u662f\u88ab\u201c\u5dee\u4e0d\u591a\u201d\u5730\u89e3\u51b3\u4e86\u3002\u95ee\u9898\u7684\u96be\u6613\u7a0b\u5ea6\u4e00\u65b9\u9762\u53d6\u51b3\u4e8e\u95ee\u9898\u672c\u8eab\u7684\u6027\u8d28\uff0c\u53e6\u4e00\u65b9\u9762\u4e5f\u53d6\u51b3\u4e8e\u89c2\u6d4b\u95ee\u9898\u7684\u4eba\u7684\u77e5\u8bc6\u50a8\u5907\u3002\u4eba\u7684\u77e5\u8bc6\u8d8a\u5b8c\u5907\u3001\u7ecf\u9a8c\u8d8a\u591a\uff0c\u5206\u6790\u95ee\u9898\u5c31\u4f1a\u8d8a\u6df1\u5165\uff0c\u95ee\u9898\u5c31\u80fd\u88ab\u89e3\u51b3\u5f97\u66f4\u4f18\u96c5\u3002

        "},{"location":"chapter_introduction/what_is_dsa/","title":"1.2 \u00a0 \u7b97\u6cd5\u662f\u4ec0\u4e48","text":""},{"location":"chapter_introduction/what_is_dsa/#121","title":"1.2.1 \u00a0 \u7b97\u6cd5\u5b9a\u4e49","text":"

        \u7b97\u6cd5\uff08algorithm\uff09\u662f\u5728\u6709\u9650\u65f6\u95f4\u5185\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u7684\u4e00\u7ec4\u6307\u4ee4\u6216\u64cd\u4f5c\u6b65\u9aa4\uff0c\u5b83\u5177\u6709\u4ee5\u4e0b\u7279\u6027\u3002

        • \u95ee\u9898\u662f\u660e\u786e\u7684\uff0c\u5305\u542b\u6e05\u6670\u7684\u8f93\u5165\u548c\u8f93\u51fa\u5b9a\u4e49\u3002
        • \u5177\u6709\u53ef\u884c\u6027\uff0c\u80fd\u591f\u5728\u6709\u9650\u6b65\u9aa4\u3001\u65f6\u95f4\u548c\u5185\u5b58\u7a7a\u95f4\u4e0b\u5b8c\u6210\u3002
        • \u5404\u6b65\u9aa4\u90fd\u6709\u786e\u5b9a\u7684\u542b\u4e49\uff0c\u5728\u76f8\u540c\u7684\u8f93\u5165\u548c\u8fd0\u884c\u6761\u4ef6\u4e0b\uff0c\u8f93\u51fa\u59cb\u7ec8\u76f8\u540c\u3002
        "},{"location":"chapter_introduction/what_is_dsa/#122","title":"1.2.2 \u00a0 \u6570\u636e\u7ed3\u6784\u5b9a\u4e49","text":"

        \u6570\u636e\u7ed3\u6784\uff08data structure\uff09\u662f\u7ec4\u7ec7\u548c\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\uff0c\u6db5\u76d6\u6570\u636e\u5185\u5bb9\u3001\u6570\u636e\u4e4b\u95f4\u5173\u7cfb\u548c\u6570\u636e\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5b83\u5177\u6709\u4ee5\u4e0b\u8bbe\u8ba1\u76ee\u6807\u3002

        • \u7a7a\u95f4\u5360\u7528\u5c3d\u91cf\u5c11\uff0c\u4ee5\u8282\u7701\u8ba1\u7b97\u673a\u5185\u5b58\u3002
        • \u6570\u636e\u64cd\u4f5c\u5c3d\u53ef\u80fd\u5feb\u901f\uff0c\u6db5\u76d6\u6570\u636e\u8bbf\u95ee\u3001\u6dfb\u52a0\u3001\u5220\u9664\u3001\u66f4\u65b0\u7b49\u3002
        • \u63d0\u4f9b\u7b80\u6d01\u7684\u6570\u636e\u8868\u793a\u548c\u903b\u8f91\u4fe1\u606f\uff0c\u4ee5\u4fbf\u7b97\u6cd5\u9ad8\u6548\u8fd0\u884c\u3002

        \u6570\u636e\u7ed3\u6784\u8bbe\u8ba1\u662f\u4e00\u4e2a\u5145\u6ee1\u6743\u8861\u7684\u8fc7\u7a0b\u3002\u5982\u679c\u60f3\u5728\u67d0\u65b9\u9762\u53d6\u5f97\u63d0\u5347\uff0c\u5f80\u5f80\u9700\u8981\u5728\u53e6\u4e00\u65b9\u9762\u4f5c\u51fa\u59a5\u534f\u3002\u4e0b\u9762\u4e3e\u4e24\u4e2a\u4f8b\u5b50\u3002

        • \u94fe\u8868\u76f8\u8f83\u4e8e\u6570\u7ec4\uff0c\u5728\u6570\u636e\u6dfb\u52a0\u548c\u5220\u9664\u64cd\u4f5c\u4e0a\u66f4\u52a0\u4fbf\u6377\uff0c\u4f46\u727a\u7272\u4e86\u6570\u636e\u8bbf\u95ee\u901f\u5ea6\u3002
        • \u56fe\u76f8\u8f83\u4e8e\u94fe\u8868\uff0c\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u903b\u8f91\u4fe1\u606f\uff0c\u4f46\u9700\u8981\u5360\u7528\u66f4\u5927\u7684\u5185\u5b58\u7a7a\u95f4\u3002
        "},{"location":"chapter_introduction/what_is_dsa/#123","title":"1.2.3 \u00a0 \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u5173\u7cfb","text":"

        \u5982\u56fe 1-4 \u6240\u793a\uff0c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u9ad8\u5ea6\u76f8\u5173\u3001\u7d27\u5bc6\u7ed3\u5408\uff0c\u5177\u4f53\u8868\u73b0\u5728\u4ee5\u4e0b\u4e09\u4e2a\u65b9\u9762\u3002

        • \u6570\u636e\u7ed3\u6784\u662f\u7b97\u6cd5\u7684\u57fa\u77f3\u3002\u6570\u636e\u7ed3\u6784\u4e3a\u7b97\u6cd5\u63d0\u4f9b\u4e86\u7ed3\u6784\u5316\u5b58\u50a8\u7684\u6570\u636e\uff0c\u4ee5\u53ca\u64cd\u4f5c\u6570\u636e\u7684\u65b9\u6cd5\u3002
        • \u7b97\u6cd5\u4e3a\u6570\u636e\u7ed3\u6784\u6ce8\u5165\u751f\u547d\u529b\u3002\u6570\u636e\u7ed3\u6784\u672c\u8eab\u4ec5\u5b58\u50a8\u6570\u636e\u4fe1\u606f\uff0c\u7ed3\u5408\u7b97\u6cd5\u624d\u80fd\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u3002
        • \u7b97\u6cd5\u901a\u5e38\u53ef\u4ee5\u57fa\u4e8e\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\u5b9e\u73b0\uff0c\u4f46\u6267\u884c\u6548\u7387\u53ef\u80fd\u76f8\u5dee\u5f88\u5927\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u662f\u5173\u952e\u3002

        \u56fe 1-4 \u00a0 \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u5173\u7cfb

        \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u72b9\u5982\u56fe 1-5 \u6240\u793a\u7684\u62fc\u88c5\u79ef\u6728\u3002\u4e00\u5957\u79ef\u6728\uff0c\u9664\u4e86\u5305\u542b\u8bb8\u591a\u96f6\u4ef6\u4e4b\u5916\uff0c\u8fd8\u9644\u6709\u8be6\u7ec6\u7684\u7ec4\u88c5\u8bf4\u660e\u4e66\u3002\u6211\u4eec\u6309\u7167\u8bf4\u660e\u4e66\u4e00\u6b65\u6b65\u64cd\u4f5c\uff0c\u5c31\u80fd\u7ec4\u88c5\u51fa\u7cbe\u7f8e\u7684\u79ef\u6728\u6a21\u578b\u3002

        \u56fe 1-5 \u00a0 \u62fc\u88c5\u79ef\u6728

        \u4e24\u8005\u7684\u8be6\u7ec6\u5bf9\u5e94\u5173\u7cfb\u5982\u8868 1-1 \u6240\u793a\u3002

        \u8868 1-1 \u00a0 \u5c06\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7c7b\u6bd4\u4e3a\u62fc\u88c5\u79ef\u6728

        \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5 \u62fc\u88c5\u79ef\u6728 \u8f93\u5165\u6570\u636e \u672a\u62fc\u88c5\u7684\u79ef\u6728 \u6570\u636e\u7ed3\u6784 \u79ef\u6728\u7ec4\u7ec7\u5f62\u5f0f\uff0c\u5305\u62ec\u5f62\u72b6\u3001\u5927\u5c0f\u3001\u8fde\u63a5\u65b9\u5f0f\u7b49 \u7b97\u6cd5 \u628a\u79ef\u6728\u62fc\u6210\u76ee\u6807\u5f62\u6001\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\u6b65\u9aa4 \u8f93\u51fa\u6570\u636e \u79ef\u6728\u6a21\u578b

        \u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u662f\u72ec\u7acb\u4e8e\u7f16\u7a0b\u8bed\u8a00\u7684\u3002\u6b63\u56e0\u5982\u6b64\uff0c\u672c\u4e66\u5f97\u4ee5\u63d0\u4f9b\u57fa\u4e8e\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u7684\u5b9e\u73b0\u3002

        \u7ea6\u5b9a\u4fd7\u6210\u7684\u7b80\u79f0

        \u5728\u5b9e\u9645\u8ba8\u8bba\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u201c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u201d\u7b80\u79f0\u4e3a\u201c\u7b97\u6cd5\u201d\u3002\u6bd4\u5982\u4f17\u6240\u5468\u77e5\u7684 LeetCode \u7b97\u6cd5\u9898\u76ee\uff0c\u5b9e\u9645\u4e0a\u540c\u65f6\u8003\u67e5\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u4e24\u65b9\u9762\u7684\u77e5\u8bc6\u3002

        "},{"location":"chapter_paperbook/","title":"\u7eb8\u8d28\u4e66","text":"

        \u7ecf\u8fc7\u957f\u65f6\u95f4\u7684\u6253\u78e8\uff0c\u300aHello \u7b97\u6cd5\u300b\u7eb8\u8d28\u4e66\u7ec8\u4e8e\u53d1\u5e03\u4e86\uff01\u6b64\u65f6\u7684\u5fc3\u60c5\u53ef\u4ee5\u7528\u4e00\u53e5\u8bd7\u6765\u5f62\u5bb9\uff1a

        \u8ffd\u98ce\u8d76\u6708\u83ab\u505c\u7559\uff0c\u5e73\u829c\u5c3d\u5904\u662f\u6625\u5c71\u3002

        \u4ee5\u4e0b\u89c6\u9891\u5c55\u793a\u4e86\u7eb8\u8d28\u4e66\uff0c\u5e76\u4e14\u5305\u542b\u6211\u7684\u4e00\u4e9b\u601d\u8003\uff1a

        • \u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u91cd\u8981\u6027\u3002
        • \u4e3a\u4ec0\u4e48\u5728\u7eb8\u8d28\u4e66\u4e2d\u9009\u62e9 Python\u3002
        • \u5bf9\u77e5\u8bc6\u5206\u4eab\u7684\u7406\u89e3\u3002

        \u65b0\u4eba UP \u4e3b\uff0c\u8bf7\u591a\u591a\u5173\u7167\u3001\u4e00\u952e\u4e09\u8fde\uff5e\u8c22\u8c22\uff01

        \u9644\u7eb8\u8d28\u4e66\u5feb\u7167\uff1a

        "},{"location":"chapter_paperbook/#_2","title":"\u4f18\u52bf\u4e0e\u4e0d\u8db3","text":"

        \u603b\u7ed3\u4e00\u4e0b\u7eb8\u8d28\u4e66\u53ef\u80fd\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u60ca\u559c\u7684\u5730\u65b9\uff1a

        • \u91c7\u7528\u5168\u5f69\u5370\u5237\uff0c\u80fd\u591f\u539f\u6c41\u539f\u5473\u5730\u53d1\u6325\u51fa\u672c\u4e66\u201c\u52a8\u753b\u56fe\u89e3\u201d\u7684\u4f18\u52bf\u3002
        • \u8003\u7a76\u7eb8\u5f20\u6750\u8d28\uff0c\u65e2\u4fdd\u8bc1\u8272\u5f69\u9ad8\u5ea6\u8fd8\u539f\uff0c\u4e5f\u4fdd\u7559\u7eb8\u8d28\u4e66\u7279\u6709\u7684\u8d28\u611f\u3002
        • \u7eb8\u8d28\u7248\u6bd4\u7f51\u9875\u7248\u7684\u683c\u5f0f\u66f4\u52a0\u89c4\u8303\uff0c\u4f8b\u5982\u56fe\u4e2d\u7684\u516c\u5f0f\u4f7f\u7528\u659c\u4f53\u3002
        • \u5728\u4e0d\u63d0\u5347\u5b9a\u4ef7\u7684\u524d\u63d0\u4e0b\uff0c\u9644\u8d60\u601d\u7ef4\u5bfc\u56fe\u6298\u9875\u3001\u4e66\u7b7e\u3002
        • \u7eb8\u8d28\u4e66\u3001\u7f51\u9875\u7248\u3001PDF \u7248\u5185\u5bb9\u540c\u6b65\uff0c\u968f\u610f\u5207\u6362\u9605\u8bfb\u3002

        Tip

        \u7531\u4e8e\u7eb8\u8d28\u4e66\u548c\u7f51\u9875\u7248\u7684\u540c\u6b65\u96be\u5ea6\u8f83\u5927\uff0c\u56e0\u6b64\u53ef\u80fd\u4f1a\u6709\u4e00\u4e9b\u7ec6\u8282\u4e0a\u7684\u4e0d\u540c\uff0c\u8bf7\u60a8\u89c1\u8c05\uff01

        \u5f53\u7136\uff0c\u7eb8\u8d28\u4e66\u4e5f\u6709\u4e00\u4e9b\u503c\u5f97\u5927\u5bb6\u5165\u624b\u524d\u8003\u8651\u7684\u5730\u65b9\uff1a

        • \u4f7f\u7528 Python \u8bed\u8a00\uff0c\u53ef\u80fd\u4e0d\u5339\u914d\u4f60\u7684\u4e3b\u8bed\u8a00\uff08\u53ef\u4ee5\u628a Python \u770b\u4f5c\u4f2a\u4ee3\u7801\uff0c\u91cd\u5728\u7406\u89e3\u601d\u8def\uff09\u3002
        • \u5168\u5f69\u5370\u5237\u867d\u7136\u5927\u5e45\u63d0\u5347\u4e86\u56fe\u89e3\u548c\u4ee3\u7801\u7684\u9605\u8bfb\u4f53\u9a8c\uff0c\u4f46\u4ef7\u683c\u4f1a\u6bd4\u9ed1\u767d\u5370\u5237\u9ad8\u4e00\u4e9b\u3002

        Tip

        \u201c\u5370\u5237\u8d28\u91cf\u201d\u548c\u201c\u4ef7\u683c\u201d\u5c31\u50cf\u7b97\u6cd5\u4e2d\u7684\u201c\u65f6\u95f4\u6548\u7387\u201d\u548c\u201c\u7a7a\u95f4\u6548\u7387\u201d\uff0c\u96be\u4ee5\u4e24\u5168\u3002\u800c\u6211\u8ba4\u4e3a\uff0c\u201c\u5370\u5237\u8d28\u91cf\u201d\u5bf9\u5e94\u7684\u662f\u201c\u65f6\u95f4\u6548\u7387\u201d\uff0c\u66f4\u5e94\u8be5\u88ab\u6ce8\u91cd\u3002

        "},{"location":"chapter_paperbook/#_3","title":"\u8d2d\u4e70\u94fe\u63a5","text":"

        \u5982\u679c\u4f60\u5bf9\u7eb8\u8d28\u4e66\u611f\u5174\u8da3\uff0c\u53ef\u4ee5\u8003\u8651\u5165\u624b\u4e00\u672c\u3002\u6211\u4eec\u4e3a\u5927\u5bb6\u4e89\u53d6\u5230\u4e86\u65b0\u4e66 5 \u6298\u4f18\u60e0\uff0c\u8bf7\u89c1\u6b64\u94fe\u63a5\u6216\u626b\u63cf\u4ee5\u4e0b\u4e8c\u7ef4\u7801\uff1a

        "},{"location":"chapter_paperbook/#_4","title":"\u5c3e\u8bb0","text":"

        \u8d77\u521d\uff0c\u6211\u4f4e\u4f30\u4e86\u7eb8\u8d28\u4e66\u51fa\u7248\u7684\u5de5\u4f5c\u91cf\uff0c\u4ee5\u4e3a\u53ea\u8981\u7ef4\u62a4\u597d\u4e86\u5f00\u6e90\u9879\u76ee\uff0c\u7eb8\u8d28\u7248\u5c31\u53ef\u4ee5\u901a\u8fc7\u67d0\u4e9b\u81ea\u52a8\u5316\u624b\u6bb5\u751f\u6210\u51fa\u6765\u3002\u5b9e\u8df5\u8bc1\u660e\uff0c\u7eb8\u8d28\u4e66\u7684\u751f\u4ea7\u6d41\u7a0b\u4e0e\u5f00\u6e90\u9879\u76ee\u7684\u66f4\u65b0\u673a\u5236\u5b58\u5728\u5f88\u5927\u7684\u4e0d\u540c\uff0c\u4e24\u8005\u4e4b\u95f4\u7684\u8f6c\u5316\u9700\u8981\u505a\u8bb8\u591a\u989d\u5916\u5de5\u4f5c\u3002

        \u4e00\u672c\u4e66\u7684\u521d\u7a3f\u4e0e\u8fbe\u5230\u51fa\u7248\u6807\u51c6\u7684\u5b9a\u7a3f\u4e4b\u95f4\u4ecd\u6709\u8f83\u957f\u8ddd\u79bb\uff0c\u9700\u8981\u51fa\u7248\u793e\uff08\u7b56\u5212\u3001\u7f16\u8f91\u3001\u8bbe\u8ba1\u3001\u5e02\u573a\u7b49\uff09\u4e0e\u4f5c\u8005\u7684\u901a\u529b\u5408\u4f5c\u3001\u957f\u671f\u96d5\u7422\u3002\u5728\u6b64\u611f\u8c22\u56fe\u7075\u7b56\u5212\u7f16\u8f91\u738b\u519b\u82b1\u3001\u4ee5\u53ca\u4eba\u6c11\u90ae\u7535\u51fa\u7248\u793e\u548c\u56fe\u7075\u793e\u533a\u6bcf\u4f4d\u53c2\u4e0e\u672c\u4e66\u51fa\u7248\u6d41\u7a0b\u7684\u5de5\u4f5c\u4eba\u5458\uff01

        \u5e0c\u671b\u8fd9\u672c\u4e66\u80fd\u591f\u5e2e\u52a9\u5230\u4f60\uff01

        "},{"location":"chapter_preface/","title":"\u7b2c 0 \u7ae0 \u00a0 \u524d\u8a00","text":"

        Abstract

        \u7b97\u6cd5\u72b9\u5982\u7f8e\u5999\u7684\u4ea4\u54cd\u4e50\uff0c\u6bcf\u4e00\u884c\u4ee3\u7801\u90fd\u50cf\u97f5\u5f8b\u822c\u6d41\u6dcc\u3002

        \u613f\u8fd9\u672c\u4e66\u5728\u4f60\u7684\u8111\u6d77\u4e2d\u8f7b\u8f7b\u54cd\u8d77\uff0c\u7559\u4e0b\u72ec\u7279\u800c\u6df1\u523b\u7684\u65cb\u5f8b\u3002

        "},{"location":"chapter_preface/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 0.1 \u00a0 \u5173\u4e8e\u672c\u4e66
        • 0.2 \u00a0 \u5982\u4f55\u4f7f\u7528\u672c\u4e66
        • 0.3 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_preface/about_the_book/","title":"0.1 \u00a0 \u5173\u4e8e\u672c\u4e66","text":"

        \u672c\u9879\u76ee\u65e8\u5728\u521b\u5efa\u4e00\u672c\u5f00\u6e90\u3001\u514d\u8d39\u3001\u5bf9\u65b0\u624b\u53cb\u597d\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u5165\u95e8\u6559\u7a0b\u3002

        • \u5168\u4e66\u91c7\u7528\u52a8\u753b\u56fe\u89e3\uff0c\u5185\u5bb9\u6e05\u6670\u6613\u61c2\u3001\u5b66\u4e60\u66f2\u7ebf\u5e73\u6ed1\uff0c\u5f15\u5bfc\u521d\u5b66\u8005\u63a2\u7d22\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u77e5\u8bc6\u5730\u56fe\u3002
        • \u6e90\u4ee3\u7801\u53ef\u4e00\u952e\u8fd0\u884c\uff0c\u5e2e\u52a9\u8bfb\u8005\u5728\u7ec3\u4e60\u4e2d\u63d0\u5347\u7f16\u7a0b\u6280\u80fd\uff0c\u4e86\u89e3\u7b97\u6cd5\u5de5\u4f5c\u539f\u7406\u548c\u6570\u636e\u7ed3\u6784\u5e95\u5c42\u5b9e\u73b0\u3002
        • \u63d0\u5021\u8bfb\u8005\u4e92\u52a9\u5b66\u4e60\uff0c\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u533a\u63d0\u51fa\u95ee\u9898\u4e0e\u5206\u4eab\u89c1\u89e3\uff0c\u5728\u4ea4\u6d41\u8ba8\u8bba\u4e2d\u5171\u540c\u8fdb\u6b65\u3002
        "},{"location":"chapter_preface/about_the_book/#011","title":"0.1.1 \u00a0 \u8bfb\u8005\u5bf9\u8c61","text":"

        \u82e5\u4f60\u662f\u7b97\u6cd5\u521d\u5b66\u8005\uff0c\u4ece\u672a\u63a5\u89e6\u8fc7\u7b97\u6cd5\uff0c\u6216\u8005\u5df2\u7ecf\u6709\u4e00\u4e9b\u5237\u9898\u7ecf\u9a8c\uff0c\u5bf9\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u6709\u6a21\u7cca\u7684\u8ba4\u8bc6\uff0c\u5728\u4f1a\u4e0e\u4e0d\u4f1a\u4e4b\u95f4\u53cd\u590d\u6a2a\u8df3\uff0c\u90a3\u4e48\u672c\u4e66\u6b63\u662f\u4e3a\u4f60\u91cf\u8eab\u5b9a\u5236\u7684\uff01

        \u5982\u679c\u4f60\u5df2\u7ecf\u79ef\u7d2f\u4e00\u5b9a\u7684\u5237\u9898\u91cf\uff0c\u719f\u6089\u5927\u90e8\u5206\u9898\u578b\uff0c\u90a3\u4e48\u672c\u4e66\u53ef\u52a9\u4f60\u56de\u987e\u4e0e\u68b3\u7406\u7b97\u6cd5\u77e5\u8bc6\u4f53\u7cfb\uff0c\u4ed3\u5e93\u6e90\u4ee3\u7801\u53ef\u4ee5\u5f53\u4f5c\u201c\u5237\u9898\u5de5\u5177\u5e93\u201d\u6216\u201c\u7b97\u6cd5\u5b57\u5178\u201d\u6765\u4f7f\u7528\u3002

        \u82e5\u4f60\u662f\u7b97\u6cd5\u201c\u5927\u795e\u201d\uff0c\u6211\u4eec\u671f\u5f85\u6536\u5230\u4f60\u7684\u5b9d\u8d35\u5efa\u8bae\uff0c\u6216\u8005\u4e00\u8d77\u53c2\u4e0e\u521b\u4f5c\u3002

        \u524d\u7f6e\u6761\u4ef6

        \u4f60\u9700\u8981\u81f3\u5c11\u5177\u5907\u4efb\u4e00\u8bed\u8a00\u7684\u7f16\u7a0b\u57fa\u7840\uff0c\u80fd\u591f\u9605\u8bfb\u548c\u7f16\u5199\u7b80\u5355\u4ee3\u7801\u3002

        "},{"location":"chapter_preface/about_the_book/#012","title":"0.1.2 \u00a0 \u5185\u5bb9\u7ed3\u6784","text":"

        \u672c\u4e66\u7684\u4e3b\u8981\u5185\u5bb9\u5982\u56fe 0-1 \u6240\u793a\u3002

        • \u590d\u6742\u5ea6\u5206\u6790\uff1a\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u7684\u8bc4\u4ef7\u7ef4\u5ea6\u4e0e\u65b9\u6cd5\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u3001\u5e38\u89c1\u7c7b\u578b\u3001\u793a\u4f8b\u7b49\u3002
        • \u6570\u636e\u7ed3\u6784\uff1a\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u6570\u636e\u7ed3\u6784\u7684\u5206\u7c7b\u65b9\u6cd5\u3002\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\u7b49\u6570\u636e\u7ed3\u6784\u7684\u5b9a\u4e49\u3001\u4f18\u7f3a\u70b9\u3001\u5e38\u7528\u64cd\u4f5c\u3001\u5e38\u89c1\u7c7b\u578b\u3001\u5178\u578b\u5e94\u7528\u3001\u5b9e\u73b0\u65b9\u6cd5\u7b49\u3002
        • \u7b97\u6cd5\uff1a\u641c\u7d22\u3001\u6392\u5e8f\u3001\u5206\u6cbb\u3001\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\u3001\u8d2a\u5fc3\u7b49\u7b97\u6cd5\u7684\u5b9a\u4e49\u3001\u4f18\u7f3a\u70b9\u3001\u6548\u7387\u3001\u5e94\u7528\u573a\u666f\u3001\u89e3\u9898\u6b65\u9aa4\u548c\u793a\u4f8b\u95ee\u9898\u7b49\u3002

        \u56fe 0-1 \u00a0 \u672c\u4e66\u4e3b\u8981\u5185\u5bb9

        "},{"location":"chapter_preface/about_the_book/#013","title":"0.1.3 \u00a0 \u81f4\u8c22","text":"

        \u672c\u4e66\u5728\u5f00\u6e90\u793e\u533a\u4f17\u591a\u8d21\u732e\u8005\u7684\u5171\u540c\u52aa\u529b\u4e0b\u4e0d\u65ad\u5b8c\u5584\u3002\u611f\u8c22\u6bcf\u4e00\u4f4d\u6295\u5165\u65f6\u95f4\u4e0e\u7cbe\u529b\u7684\u64b0\u7a3f\u4eba\uff0c\u4ed6\u4eec\u662f\uff08\u6309\u7167 GitHub \u81ea\u52a8\u751f\u6210\u7684\u987a\u5e8f\uff09\uff1akrahets\u3001coderonion\u3001Gonglja\u3001nuomi1\u3001Reanon\u3001justin-tse\u3001hpstory\u3001danielsss\u3001curtishd\u3001night-cruise\u3001S-N-O-R-L-A-X\u3001rongyi\u3001msk397\u3001gvenusleo\u3001khoaxuantu\u3001rivertwilight\u3001K3v123\u3001gyt95\u3001zhuoqinyue\u3001yuelinxin\u3001Zuoxun\u3001mingXta\u3001Phoenix0415\u3001FangYuan33\u3001GN-Yu\u3001longsizhuo\u3001IsChristina\u3001xBLACKICEx\u3001guowei-gong\u3001Cathay-Chen\u3001pengchzn\u3001QiLOL\u3001magentaqin\u3001hello-ikun\u3001JoseHung\u3001qualifier1024\u3001thomasq0\u3001sunshinesDL\u3001L-Super\u3001Guanngxu\u3001Transmigration-zhou\u3001WSL0809\u3001Slone123c\u3001lhxsm\u3001yuan0221\u3001what-is-me\u3001Shyam-Chen\u3001theNefelibatas\u3001longranger2\u3001codeberg-user\u3001xiongsp\u3001JeffersonHuang\u3001prinpal\u3001seven1240\u3001Wonderdch\u3001malone6\u3001xiaomiusa87\u3001gaofer\u3001bluebean-cloud\u3001a16su\u3001SamJin98\u3001hongyun-robot\u3001nanlei\u3001XiaChuerwu\u3001yd-j\u3001iron-irax\u3001mgisr\u3001steventimes\u3001junminhong\u3001heshuyue\u3001danny900714\u3001MolDuM\u3001Nigh\u3001Dr-XYZ\u3001XC-Zero\u3001reeswell\u3001PXG-XPG\u3001NI-SW\u3001Horbin-Magician\u3001Enlightenus\u3001YangXuanyi\u3001beatrix-chan\u3001DullSword\u3001xjr7670\u3001jiaxianhua\u3001qq909244296\u3001iStig\u3001boloboloda\u3001hts0000\u3001gledfish\u3001wenjianmin\u3001keshida\u3001kilikilikid\u3001lclc6\u3001lwbaptx\u3001linyejoe2\u3001liuxjerry\u3001llql1211\u3001fbigm\u3001echo1937\u3001szu17dmy\u3001dshlstarr\u3001Yucao-cy\u3001coderlef\u3001czruby\u3001bongbongbakudan\u3001beintentional\u3001ZongYangL\u3001ZhongYuuu\u3001ZhongGuanbin\u3001hezhizhen\u3001linzeyan\u3001ZJKung\u3001luluxia\u3001xb534\u3001ztkuaikuai\u3001yw-1021\u3001ElaBosak233\u3001baagod\u3001zhouLion\u3001yishangzhang\u3001yi427\u3001yanedie\u3001yabo083\u3001weibk\u3001wangwang105\u3001th1nk3r-ing\u3001tao363\u30014yDX3906\u3001syd168\u3001sslmj2020\u3001smilelsb\u3001siqyka\u3001selear\u3001sdshaoda\u3001Xi-Row\u3001popozhu\u3001nuquist19\u3001noobcodemaker\u3001XiaoK29\u3001chadyi\u3001lyl625760\u3001lucaswangdev\u30010130w\u3001shanghai-Jerry\u3001EJackYang\u3001Javesun99\u3001eltociear\u3001lipusheng\u3001KNChiu\u3001BlindTerran\u3001ShiMaRing\u3001lovelock\u3001FreddieLi\u3001FloranceYeh\u3001fanchenggang\u3001gltianwen\u3001goerll\u3001nedchu\u3001curly210102\u3001CuB3y0nd\u3001KraHsu\u3001CarrotDLaw\u3001youshaoXG\u3001bubble9um\u3001Asashishi\u3001Asa0oo0o0o\u3001fanenr\u3001eagleanurag\u3001akshiterate\u300152coder\u3001foursevenlove\u3001KorsChen\u3001GaochaoZhu\u3001hopkings2008\u3001yang-le\u3001realwujing\u3001Evilrabbit520\u3001Umer-Jahangir\u3001Turing-1024-Lee\u3001Suremotoo\u3001paoxiaomooo\u3001Chieko-Seren\u3001Allen-Scai\u3001ymmmas\u3001Risuntsy\u3001Richard-Zhang1019\u3001RafaelCaso\u3001qingpeng9802\u3001primexiao\u3001Urbaner3\u3001zhongfq\u3001nidhoggfgg\u3001MwumLi\u3001CreatorMetaSky\u3001martinx\u3001ZnYang2018\u3001hugtyftg\u3001logan-qiu\u3001psychelzh\u3001Keynman\u3001KeiichiKasai \u548c KawaiiAsh\u3002

        \u672c\u4e66\u7684\u4ee3\u7801\u5ba1\u9605\u5de5\u4f5c\u7531 coderonion\u3001curtishd\u3001Gonglja\u3001gvenusleo\u3001hpstory\u3001justin-tse\u3001khoaxuantu\u3001krahets\u3001night-cruise\u3001nuomi1\u3001Reanon \u548c rongyi \u5b8c\u6210\uff08\u6309\u7167\u9996\u5b57\u6bcd\u987a\u5e8f\u6392\u5217\uff09\u3002\u611f\u8c22\u4ed6\u4eec\u4ed8\u51fa\u7684\u65f6\u95f4\u4e0e\u7cbe\u529b\uff0c\u6b63\u662f\u4ed6\u4eec\u786e\u4fdd\u4e86\u5404\u8bed\u8a00\u4ee3\u7801\u7684\u89c4\u8303\u4e0e\u7edf\u4e00\u3002

        \u672c\u4e66\u7684\u7e41\u4f53\u4e2d\u6587\u7248\u7531 Shyam-Chen \u548c Dr-XYZ \u5ba1\u9605\uff0c\u82f1\u6587\u7248\u7531 yuelinxin\u3001K3v123\u3001QiLOL\u3001Phoenix0415\u3001SamJin98\u3001yanedie\u3001RafaelCaso\u3001pengchzn\u3001thomasq0 \u548c magentaqin \u5ba1\u9605\uff0c\u65e5\u6587\u7248\u7531 eltociear \u5ba1\u9605\u3002\u6b63\u662f\u56e0\u4e3a\u4ed6\u4eec\u7684\u6301\u7eed\u8d21\u732e\uff0c\u8fd9\u672c\u4e66\u624d\u80fd\u591f\u670d\u52a1\u4e8e\u66f4\u5e7f\u6cdb\u7684\u8bfb\u8005\u7fa4\u4f53\uff0c\u611f\u8c22\u4ed6\u4eec\u3002

        \u672c\u4e66\u7684 ePub \u7535\u5b50\u4e66\u751f\u6210\u5de5\u5177\u7531 zhongfq \u5f00\u53d1\u3002\u611f\u8c22\u4ed6\u7684\u8d21\u732e\uff0c\u4e3a\u8bfb\u8005\u63d0\u4f9b\u4e86\u66f4\u52a0\u81ea\u7531\u7684\u9605\u8bfb\u65b9\u5f0f\u3002

        \u5728\u672c\u4e66\u7684\u521b\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u6211\u5f97\u5230\u4e86\u8bb8\u591a\u4eba\u7684\u5e2e\u52a9\u3002

        • \u611f\u8c22\u6211\u5728\u516c\u53f8\u7684\u5bfc\u5e08\u674e\u6c50\u535a\u58eb\uff0c\u5728\u4e00\u6b21\u7545\u8c08\u4e2d\u4f60\u9f13\u52b1\u6211\u201c\u5feb\u884c\u52a8\u8d77\u6765\u201d\uff0c\u575a\u5b9a\u4e86\u6211\u5199\u8fd9\u672c\u4e66\u7684\u51b3\u5fc3\uff1b
        • \u611f\u8c22\u6211\u7684\u5973\u670b\u53cb\u6ce1\u6ce1\u4f5c\u4e3a\u672c\u4e66\u7684\u9996\u4f4d\u8bfb\u8005\uff0c\u4ece\u7b97\u6cd5\u5c0f\u767d\u7684\u89d2\u5ea6\u63d0\u51fa\u8bb8\u591a\u5b9d\u8d35\u5efa\u8bae\uff0c\u4f7f\u5f97\u672c\u4e66\u66f4\u9002\u5408\u65b0\u624b\u9605\u8bfb\uff1b
        • \u611f\u8c22\u817e\u5b9d\u3001\u7426\u5b9d\u3001\u98de\u5b9d\u4e3a\u672c\u4e66\u8d77\u4e86\u4e00\u4e2a\u5bcc\u6709\u521b\u610f\u7684\u540d\u5b57\uff0c\u5524\u8d77\u5927\u5bb6\u5199\u4e0b\u7b2c\u4e00\u884c\u4ee3\u7801\u201cHello World!\u201d\u7684\u7f8e\u597d\u56de\u5fc6\uff1b
        • \u611f\u8c22\u6821\u94e8\u5728\u77e5\u8bc6\u4ea7\u6743\u65b9\u9762\u63d0\u4f9b\u7684\u4e13\u4e1a\u5e2e\u52a9\uff0c\u8fd9\u5bf9\u672c\u5f00\u6e90\u4e66\u7684\u5b8c\u5584\u8d77\u5230\u4e86\u91cd\u8981\u4f5c\u7528\uff1b
        • \u611f\u8c22\u82cf\u6f7c\u4e3a\u672c\u4e66\u8bbe\u8ba1\u4e86\u7cbe\u7f8e\u7684\u5c01\u9762\u548c logo \uff0c\u5e76\u5728\u6211\u7684\u5f3a\u8feb\u75c7\u7684\u9a71\u4f7f\u4e0b\u591a\u6b21\u8010\u5fc3\u4fee\u6539\uff1b
        • \u611f\u8c22 @squidfunk \u63d0\u4f9b\u7684\u6392\u7248\u5efa\u8bae\uff0c\u4ee5\u53ca\u4ed6\u5f00\u53d1\u7684\u5f00\u6e90\u6587\u6863\u4e3b\u9898 Material-for-MkDocs \u3002

        \u5728\u5199\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u6211\u9605\u8bfb\u4e86\u8bb8\u591a\u5173\u4e8e\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u6559\u6750\u548c\u6587\u7ae0\u3002\u8fd9\u4e9b\u4f5c\u54c1\u4e3a\u672c\u4e66\u63d0\u4f9b\u4e86\u4f18\u79c0\u7684\u8303\u672c\uff0c\u786e\u4fdd\u4e86\u672c\u4e66\u5185\u5bb9\u7684\u51c6\u786e\u6027\u4e0e\u54c1\u8d28\u3002\u5728\u6b64\u611f\u8c22\u6240\u6709\u8001\u5e08\u548c\u524d\u8f88\u7684\u6770\u51fa\u8d21\u732e\uff01

        \u672c\u4e66\u5021\u5bfc\u624b\u8111\u5e76\u7528\u7684\u5b66\u4e60\u65b9\u5f0f\uff0c\u5728\u8fd9\u4e00\u70b9\u4e0a\u6211\u6df1\u53d7\u300a\u52a8\u624b\u5b66\u6df1\u5ea6\u5b66\u4e60\u300b\u7684\u542f\u53d1\u3002\u5728\u6b64\u5411\u5404\u4f4d\u8bfb\u8005\u5f3a\u70c8\u63a8\u8350\u8fd9\u672c\u4f18\u79c0\u7684\u8457\u4f5c\u3002

        \u8877\u5fc3\u611f\u8c22\u6211\u7684\u7236\u6bcd\uff0c\u6b63\u662f\u4f60\u4eec\u4e00\u76f4\u4ee5\u6765\u7684\u652f\u6301\u4e0e\u9f13\u52b1\uff0c\u8ba9\u6211\u6709\u673a\u4f1a\u505a\u8fd9\u4ef6\u5bcc\u6709\u8da3\u5473\u7684\u4e8b\u3002

        "},{"location":"chapter_preface/suggestions/","title":"0.2 \u00a0 \u5982\u4f55\u4f7f\u7528\u672c\u4e66","text":"

        Tip

        \u4e3a\u4e86\u83b7\u5f97\u6700\u4f73\u7684\u9605\u8bfb\u4f53\u9a8c\uff0c\u5efa\u8bae\u4f60\u901a\u8bfb\u672c\u8282\u5185\u5bb9\u3002

        "},{"location":"chapter_preface/suggestions/#021","title":"0.2.1 \u00a0 \u884c\u6587\u98ce\u683c\u7ea6\u5b9a","text":"
        • \u6807\u9898\u540e\u6807\u6ce8 * \u7684\u662f\u9009\u8bfb\u7ae0\u8282\uff0c\u5185\u5bb9\u76f8\u5bf9\u56f0\u96be\u3002\u5982\u679c\u4f60\u7684\u65f6\u95f4\u6709\u9650\uff0c\u53ef\u4ee5\u5148\u8df3\u8fc7\u3002
        • \u4e13\u4e1a\u672f\u8bed\u4f1a\u4f7f\u7528\u9ed1\u4f53\uff08\u7eb8\u8d28\u7248\u548c PDF \u7248\uff09\u6216\u6dfb\u52a0\u4e0b\u5212\u7ebf\uff08\u7f51\u9875\u7248\uff09\uff0c\u4f8b\u5982\u6570\u7ec4\uff08array\uff09\u3002\u5efa\u8bae\u8bb0\u4f4f\u5b83\u4eec\uff0c\u4ee5\u4fbf\u9605\u8bfb\u6587\u732e\u3002
        • \u91cd\u70b9\u5185\u5bb9\u548c\u603b\u7ed3\u6027\u8bed\u53e5\u4f1a \u52a0\u7c97\uff0c\u8fd9\u7c7b\u6587\u5b57\u503c\u5f97\u7279\u522b\u5173\u6ce8\u3002
        • \u6709\u7279\u6307\u542b\u4e49\u7684\u8bcd\u53e5\u4f1a\u4f7f\u7528\u201c\u5f15\u53f7\u201d\u6807\u6ce8\uff0c\u4ee5\u907f\u514d\u6b67\u4e49\u3002
        • \u5f53\u6d89\u53ca\u7f16\u7a0b\u8bed\u8a00\u4e4b\u95f4\u4e0d\u4e00\u81f4\u7684\u540d\u8bcd\u65f6\uff0c\u672c\u4e66\u5747\u4ee5 Python \u4e3a\u51c6\uff0c\u4f8b\u5982\u4f7f\u7528 None \u6765\u8868\u793a\u201c\u7a7a\u201d\u3002
        • \u672c\u4e66\u90e8\u5206\u653e\u5f03\u4e86\u7f16\u7a0b\u8bed\u8a00\u7684\u6ce8\u91ca\u89c4\u8303\uff0c\u4ee5\u6362\u53d6\u66f4\u52a0\u7d27\u51d1\u7684\u5185\u5bb9\u6392\u7248\u3002\u6ce8\u91ca\u4e3b\u8981\u5206\u4e3a\u4e09\u79cd\u7c7b\u578b\uff1a\u6807\u9898\u6ce8\u91ca\u3001\u5185\u5bb9\u6ce8\u91ca\u3001\u591a\u884c\u6ce8\u91ca\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        \"\"\"\u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49\"\"\"\n\n# \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n\"\"\"\n\u591a\u884c\n\u6ce8\u91ca\n\"\"\"\n
        /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
        /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
        /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
        /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
        /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
        /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
        /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
        /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
        /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
        /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
        /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
        ### \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 ###\n\n# \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n# \u591a\u884c\n# \u6ce8\u91ca\n
        "},{"location":"chapter_preface/suggestions/#022","title":"0.2.2 \u00a0 \u5728\u52a8\u753b\u56fe\u89e3\u4e2d\u9ad8\u6548\u5b66\u4e60","text":"

        \u76f8\u8f83\u4e8e\u6587\u5b57\uff0c\u89c6\u9891\u548c\u56fe\u7247\u5177\u6709\u66f4\u9ad8\u7684\u4fe1\u606f\u5bc6\u5ea6\u548c\u7ed3\u6784\u5316\u7a0b\u5ea6\uff0c\u66f4\u6613\u4e8e\u7406\u89e3\u3002\u5728\u672c\u4e66\u4e2d\uff0c\u91cd\u70b9\u548c\u96be\u70b9\u77e5\u8bc6\u5c06\u4e3b\u8981\u901a\u8fc7\u52a8\u753b\u4ee5\u56fe\u89e3\u5f62\u5f0f\u5c55\u793a\uff0c\u800c\u6587\u5b57\u5219\u4f5c\u4e3a\u89e3\u91ca\u4e0e\u8865\u5145\u3002

        \u5982\u679c\u4f60\u5728\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u53d1\u73b0\u67d0\u6bb5\u5185\u5bb9\u63d0\u4f9b\u4e86\u5982\u56fe 0-2 \u6240\u793a\u7684\u52a8\u753b\u56fe\u89e3\uff0c\u8bf7\u4ee5\u56fe\u4e3a\u4e3b\u3001\u4ee5\u6587\u5b57\u4e3a\u8f85\uff0c\u7efc\u5408\u4e24\u8005\u6765\u7406\u89e3\u5185\u5bb9\u3002

        \u56fe 0-2 \u00a0 \u52a8\u753b\u56fe\u89e3\u793a\u4f8b

        "},{"location":"chapter_preface/suggestions/#023","title":"0.2.3 \u00a0 \u5728\u4ee3\u7801\u5b9e\u8df5\u4e2d\u52a0\u6df1\u7406\u89e3","text":"

        \u672c\u4e66\u7684\u914d\u5957\u4ee3\u7801\u6258\u7ba1\u5728 GitHub \u4ed3\u5e93\u3002\u5982\u56fe 0-3 \u6240\u793a\uff0c\u6e90\u4ee3\u7801\u9644\u6709\u6d4b\u8bd5\u6837\u4f8b\uff0c\u53ef\u4e00\u952e\u8fd0\u884c\u3002

        \u5982\u679c\u65f6\u95f4\u5141\u8bb8\uff0c\u5efa\u8bae\u4f60\u53c2\u7167\u4ee3\u7801\u81ea\u884c\u6572\u4e00\u904d\u3002\u5982\u679c\u5b66\u4e60\u65f6\u95f4\u6709\u9650\uff0c\u8bf7\u81f3\u5c11\u901a\u8bfb\u5e76\u8fd0\u884c\u6240\u6709\u4ee3\u7801\u3002

        \u4e0e\u9605\u8bfb\u4ee3\u7801\u76f8\u6bd4\uff0c\u7f16\u5199\u4ee3\u7801\u7684\u8fc7\u7a0b\u5f80\u5f80\u80fd\u5e26\u6765\u66f4\u591a\u6536\u83b7\u3002\u52a8\u624b\u5b66\uff0c\u624d\u662f\u771f\u7684\u5b66\u3002

        \u56fe 0-3 \u00a0 \u8fd0\u884c\u4ee3\u7801\u793a\u4f8b

        \u8fd0\u884c\u4ee3\u7801\u7684\u524d\u7f6e\u5de5\u4f5c\u4e3b\u8981\u5206\u4e3a\u4e09\u6b65\u3002

        \u7b2c\u4e00\u6b65\uff1a\u5b89\u88c5\u672c\u5730\u7f16\u7a0b\u73af\u5883\u3002\u8bf7\u53c2\u7167\u9644\u5f55\u6240\u793a\u7684\u6559\u7a0b\u8fdb\u884c\u5b89\u88c5\uff0c\u5982\u679c\u5df2\u5b89\u88c5\uff0c\u5219\u53ef\u8df3\u8fc7\u6b64\u6b65\u9aa4\u3002

        \u7b2c\u4e8c\u6b65\uff1a\u514b\u9686\u6216\u4e0b\u8f7d\u4ee3\u7801\u4ed3\u5e93\u3002\u524d\u5f80 GitHub \u4ed3\u5e93\u3002\u5982\u679c\u5df2\u7ecf\u5b89\u88c5 Git \uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u514b\u9686\u672c\u4ed3\u5e93\uff1a

        git clone https://github.com/krahets/hello-algo.git\n

        \u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5728\u56fe 0-4 \u6240\u793a\u7684\u4f4d\u7f6e\uff0c\u70b9\u51fb\u201cDownload ZIP\u201d\u6309\u94ae\u76f4\u63a5\u4e0b\u8f7d\u4ee3\u7801\u538b\u7f29\u5305\uff0c\u7136\u540e\u5728\u672c\u5730\u89e3\u538b\u5373\u53ef\u3002

        \u56fe 0-4 \u00a0 \u514b\u9686\u4ed3\u5e93\u4e0e\u4e0b\u8f7d\u4ee3\u7801

        \u7b2c\u4e09\u6b65\uff1a\u8fd0\u884c\u6e90\u4ee3\u7801\u3002\u5982\u56fe 0-5 \u6240\u793a\uff0c\u5bf9\u4e8e\u9876\u90e8\u6807\u6709\u6587\u4ef6\u540d\u79f0\u7684\u4ee3\u7801\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4ed3\u5e93\u7684 codes \u6587\u4ef6\u5939\u5185\u627e\u5230\u5bf9\u5e94\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u3002\u6e90\u4ee3\u7801\u6587\u4ef6\u53ef\u4e00\u952e\u8fd0\u884c\uff0c\u5c06\u5e2e\u52a9\u4f60\u8282\u7701\u4e0d\u5fc5\u8981\u7684\u8c03\u8bd5\u65f6\u95f4\uff0c\u8ba9\u4f60\u80fd\u591f\u4e13\u6ce8\u4e8e\u5b66\u4e60\u5185\u5bb9\u3002

        \u56fe 0-5 \u00a0 \u4ee3\u7801\u5757\u4e0e\u5bf9\u5e94\u7684\u6e90\u4ee3\u7801\u6587\u4ef6

        \u9664\u4e86\u672c\u5730\u8fd0\u884c\u4ee3\u7801\uff0c\u7f51\u9875\u7248\u8fd8\u652f\u6301 Python \u4ee3\u7801\u7684\u53ef\u89c6\u5316\u8fd0\u884c\uff08\u57fa\u4e8e pythontutor \u5b9e\u73b0\uff09\u3002\u5982\u56fe 0-6 \u6240\u793a\uff0c\u4f60\u53ef\u4ee5\u70b9\u51fb\u4ee3\u7801\u5757\u4e0b\u65b9\u7684\u201c\u53ef\u89c6\u5316\u8fd0\u884c\u201d\u6765\u5c55\u5f00\u89c6\u56fe\uff0c\u89c2\u5bdf\u7b97\u6cd5\u4ee3\u7801\u7684\u6267\u884c\u8fc7\u7a0b\uff1b\u4e5f\u53ef\u4ee5\u70b9\u51fb\u201c\u5168\u5c4f\u89c2\u770b\u201d\uff0c\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u9605\u89c8\u4f53\u9a8c\u3002

        \u56fe 0-6 \u00a0 Python \u4ee3\u7801\u7684\u53ef\u89c6\u5316\u8fd0\u884c

        "},{"location":"chapter_preface/suggestions/#024","title":"0.2.4 \u00a0 \u5728\u63d0\u95ee\u8ba8\u8bba\u4e2d\u5171\u540c\u6210\u957f","text":"

        \u5728\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u8bf7\u4e0d\u8981\u8f7b\u6613\u8df3\u8fc7\u90a3\u4e9b\u6ca1\u5b66\u660e\u767d\u7684\u77e5\u8bc6\u70b9\u3002\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u63d0\u51fa\u4f60\u7684\u95ee\u9898\uff0c\u6211\u548c\u5c0f\u4f19\u4f34\u4eec\u5c06\u7aed\u8bda\u4e3a\u4f60\u89e3\u7b54\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u53ef\u5728\u4e24\u5929\u5185\u56de\u590d\u3002

        \u5982\u56fe 0-7 \u6240\u793a\uff0c\u7f51\u9875\u7248\u6bcf\u4e2a\u7ae0\u8282\u7684\u5e95\u90e8\u90fd\u914d\u6709\u8bc4\u8bba\u533a\u3002\u5e0c\u671b\u4f60\u80fd\u591a\u5173\u6ce8\u8bc4\u8bba\u533a\u7684\u5185\u5bb9\u3002\u4e00\u65b9\u9762\uff0c\u4f60\u53ef\u4ee5\u4e86\u89e3\u5927\u5bb6\u9047\u5230\u7684\u95ee\u9898\uff0c\u4ece\u800c\u67e5\u6f0f\u8865\u7f3a\uff0c\u6fc0\u53d1\u66f4\u6df1\u5165\u7684\u601d\u8003\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u671f\u5f85\u4f60\u80fd\u6177\u6168\u5730\u56de\u7b54\u5176\u4ed6\u5c0f\u4f19\u4f34\u7684\u95ee\u9898\uff0c\u5206\u4eab\u4f60\u7684\u89c1\u89e3\uff0c\u5e2e\u52a9\u4ed6\u4eba\u8fdb\u6b65\u3002

        \u56fe 0-7 \u00a0 \u8bc4\u8bba\u533a\u793a\u4f8b

        "},{"location":"chapter_preface/suggestions/#025","title":"0.2.5 \u00a0 \u7b97\u6cd5\u5b66\u4e60\u8def\u7ebf","text":"

        \u4ece\u603b\u4f53\u4e0a\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u8fc7\u7a0b\u5212\u5206\u4e3a\u4e09\u4e2a\u9636\u6bb5\u3002

        1. \u9636\u6bb5\u4e00\uff1a\u7b97\u6cd5\u5165\u95e8\u3002\u6211\u4eec\u9700\u8981\u719f\u6089\u5404\u79cd\u6570\u636e\u7ed3\u6784\u7684\u7279\u70b9\u548c\u7528\u6cd5\uff0c\u5b66\u4e60\u4e0d\u540c\u7b97\u6cd5\u7684\u539f\u7406\u3001\u6d41\u7a0b\u3001\u7528\u9014\u548c\u6548\u7387\u7b49\u65b9\u9762\u7684\u5185\u5bb9\u3002
        2. \u9636\u6bb5\u4e8c\uff1a\u5237\u7b97\u6cd5\u9898\u3002\u5efa\u8bae\u4ece\u70ed\u95e8\u9898\u76ee\u5f00\u5237\uff0c\u5148\u79ef\u7d2f\u81f3\u5c11 100 \u9053\u9898\u76ee\uff0c\u719f\u6089\u4e3b\u6d41\u7684\u7b97\u6cd5\u95ee\u9898\u3002\u521d\u6b21\u5237\u9898\u65f6\uff0c\u201c\u77e5\u8bc6\u9057\u5fd8\u201d\u53ef\u80fd\u662f\u4e00\u4e2a\u6311\u6218\uff0c\u4f46\u8bf7\u653e\u5fc3\uff0c\u8fd9\u662f\u5f88\u6b63\u5e38\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u6309\u7167\u201c\u827e\u5bbe\u6d69\u65af\u9057\u5fd8\u66f2\u7ebf\u201d\u6765\u590d\u4e60\u9898\u76ee\uff0c\u901a\u5e38\u5728\u8fdb\u884c 3\uff5e5 \u8f6e\u7684\u91cd\u590d\u540e\uff0c\u5c31\u80fd\u5c06\u5176\u7262\u8bb0\u5728\u5fc3\u3002\u63a8\u8350\u7684\u9898\u5355\u548c\u5237\u9898\u8ba1\u5212\u8bf7\u89c1\u6b64 GitHub \u4ed3\u5e93\u3002
        3. \u9636\u6bb5\u4e09\uff1a\u642d\u5efa\u77e5\u8bc6\u4f53\u7cfb\u3002\u5728\u5b66\u4e60\u65b9\u9762\uff0c\u6211\u4eec\u53ef\u4ee5\u9605\u8bfb\u7b97\u6cd5\u4e13\u680f\u6587\u7ae0\u3001\u89e3\u9898\u6846\u67b6\u548c\u7b97\u6cd5\u6559\u6750\uff0c\u4ee5\u4e0d\u65ad\u4e30\u5bcc\u77e5\u8bc6\u4f53\u7cfb\u3002\u5728\u5237\u9898\u65b9\u9762\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u91c7\u7528\u8fdb\u9636\u5237\u9898\u7b56\u7565\uff0c\u5982\u6309\u4e13\u9898\u5206\u7c7b\u3001\u4e00\u9898\u591a\u89e3\u3001\u4e00\u89e3\u591a\u9898\u7b49\uff0c\u76f8\u5173\u7684\u5237\u9898\u5fc3\u5f97\u53ef\u4ee5\u5728\u5404\u4e2a\u793e\u533a\u627e\u5230\u3002

        \u5982\u56fe 0-8 \u6240\u793a\uff0c\u672c\u4e66\u5185\u5bb9\u4e3b\u8981\u6db5\u76d6\u201c\u9636\u6bb5\u4e00\u201d\uff0c\u65e8\u5728\u5e2e\u52a9\u4f60\u66f4\u9ad8\u6548\u5730\u5c55\u5f00\u9636\u6bb5\u4e8c\u548c\u9636\u6bb5\u4e09\u7684\u5b66\u4e60\u3002

        \u56fe 0-8 \u00a0 \u7b97\u6cd5\u5b66\u4e60\u8def\u7ebf

        "},{"location":"chapter_preface/summary/","title":"0.3 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_preface/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u672c\u4e66\u7684\u4e3b\u8981\u53d7\u4f17\u662f\u7b97\u6cd5\u521d\u5b66\u8005\u3002\u5982\u679c\u4f60\u5df2\u6709\u4e00\u5b9a\u57fa\u7840\uff0c\u672c\u4e66\u80fd\u5e2e\u52a9\u4f60\u7cfb\u7edf\u56de\u987e\u7b97\u6cd5\u77e5\u8bc6\uff0c\u4e66\u4e2d\u6e90\u4ee3\u7801\u4e5f\u53ef\u4f5c\u4e3a\u201c\u5237\u9898\u5de5\u5177\u5e93\u201d\u4f7f\u7528\u3002
        • \u4e66\u4e2d\u5185\u5bb9\u4e3b\u8981\u5305\u62ec\u590d\u6742\u5ea6\u5206\u6790\u3001\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u4e09\u90e8\u5206\uff0c\u6db5\u76d6\u4e86\u8be5\u9886\u57df\u7684\u5927\u90e8\u5206\u4e3b\u9898\u3002
        • \u5bf9\u4e8e\u7b97\u6cd5\u65b0\u624b\uff0c\u5728\u521d\u5b66\u9636\u6bb5\u9605\u8bfb\u4e00\u672c\u5165\u95e8\u4e66\u81f3\u5173\u91cd\u8981\uff0c\u53ef\u4ee5\u5c11\u8d70\u8bb8\u591a\u5f2f\u8def\u3002
        • \u4e66\u4e2d\u7684\u52a8\u753b\u56fe\u89e3\u901a\u5e38\u7528\u4e8e\u4ecb\u7ecd\u91cd\u70b9\u548c\u96be\u70b9\u77e5\u8bc6\u3002\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u5e94\u7ed9\u4e88\u8fd9\u4e9b\u5185\u5bb9\u66f4\u591a\u5173\u6ce8\u3002
        • \u5b9e\u8df5\u4e43\u5b66\u4e60\u7f16\u7a0b\u4e4b\u6700\u4f73\u9014\u5f84\u3002\u5f3a\u70c8\u5efa\u8bae\u8fd0\u884c\u6e90\u4ee3\u7801\u5e76\u4eb2\u81ea\u6572\u4ee3\u7801\u3002
        • \u672c\u4e66\u7f51\u9875\u7248\u7684\u6bcf\u4e2a\u7ae0\u8282\u90fd\u8bbe\u6709\u8bc4\u8bba\u533a\uff0c\u6b22\u8fce\u968f\u65f6\u5206\u4eab\u4f60\u7684\u7591\u60d1\u4e0e\u89c1\u89e3\u3002
        "},{"location":"chapter_reference/","title":"\u53c2\u8003\u6587\u732e","text":"

        [1] Thomas H. Cormen, et al. Introduction to Algorithms (3rd Edition).

        [2] Aditya Bhargava. Grokking Algorithms: An Illustrated Guide for Programmers and Other Curious People (1st Edition).

        [3] Robert Sedgewick, et al. Algorithms (4th Edition).

        [4] \u4e25\u851a\u654f. \u6570\u636e\u7ed3\u6784\uff08C \u8bed\u8a00\u7248\uff09.

        [5] \u9093\u4fca\u8f89. \u6570\u636e\u7ed3\u6784\uff08C++ \u8bed\u8a00\u7248\uff0c\u7b2c\u4e09\u7248\uff09.

        [6] \u9a6c\u514b \u827e\u4f26 \u7ef4\u65af\u8457\uff0c\u9648\u8d8a\u8bd1. \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u5206\u6790\uff1aJava\u8bed\u8a00\u63cf\u8ff0\uff08\u7b2c\u4e09\u7248\uff09.

        [7] \u7a0b\u6770. \u5927\u8bdd\u6570\u636e\u7ed3\u6784.

        [8] \u738b\u4e89. \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u4e4b\u7f8e.

        [9] Gayle Laakmann McDowell. Cracking the Coding Interview: 189 Programming Questions and Solutions (6th Edition).

        [10] Aston Zhang, et al. Dive into Deep Learning.

        "},{"location":"chapter_searching/","title":"\u7b2c 10 \u7ae0 \u00a0 \u641c\u7d22","text":"

        Abstract

        \u641c\u7d22\u662f\u4e00\u573a\u672a\u77e5\u7684\u5192\u9669\uff0c\u6211\u4eec\u6216\u8bb8\u9700\u8981\u8d70\u904d\u795e\u79d8\u7a7a\u95f4\u7684\u6bcf\u4e2a\u89d2\u843d\uff0c\u53c8\u6216\u8bb8\u53ef\u4ee5\u5feb\u901f\u9501\u5b9a\u76ee\u6807\u3002

        \u5728\u8fd9\u573a\u5bfb\u89c5\u4e4b\u65c5\u4e2d\uff0c\u6bcf\u4e00\u6b21\u63a2\u7d22\u90fd\u53ef\u80fd\u5f97\u5230\u4e00\u4e2a\u672a\u66fe\u6599\u60f3\u7684\u7b54\u6848\u3002

        "},{"location":"chapter_searching/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 10.1 \u00a0 \u4e8c\u5206\u67e5\u627e
        • 10.2 \u00a0 \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9
        • 10.3 \u00a0 \u4e8c\u5206\u67e5\u627e\u8fb9\u754c
        • 10.4 \u00a0 \u54c8\u5e0c\u4f18\u5316\u7b56\u7565
        • 10.5 \u00a0 \u91cd\u8bc6\u641c\u7d22\u7b97\u6cd5
        • 10.6 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_searching/binary_search/","title":"10.1 \u00a0 \u4e8c\u5206\u67e5\u627e","text":"

        \u4e8c\u5206\u67e5\u627e\uff08binary search\uff09\u662f\u4e00\u79cd\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u9ad8\u6548\u641c\u7d22\u7b97\u6cd5\u3002\u5b83\u5229\u7528\u6570\u636e\u7684\u6709\u5e8f\u6027\uff0c\u6bcf\u8f6e\u7f29\u5c0f\u4e00\u534a\u641c\u7d22\u8303\u56f4\uff0c\u76f4\u81f3\u627e\u5230\u76ee\u6807\u5143\u7d20\u6216\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u4e3a\u6b62\u3002

        Question

        \u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4 nums \uff0c\u5143\u7d20\u6309\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u6392\u5217\u4e14\u4e0d\u91cd\u590d\u3002\u8bf7\u67e5\u627e\u5e76\u8fd4\u56de\u5143\u7d20 target \u5728\u8be5\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\u3002\u82e5\u6570\u7ec4\u4e0d\u5305\u542b\u8be5\u5143\u7d20\uff0c\u5219\u8fd4\u56de \\(-1\\) \u3002\u793a\u4f8b\u5982\u56fe 10-1 \u6240\u793a\u3002

        \u56fe 10-1 \u00a0 \u4e8c\u5206\u67e5\u627e\u793a\u4f8b\u6570\u636e

        \u5982\u56fe 10-2 \u6240\u793a\uff0c\u6211\u4eec\u5148\u521d\u59cb\u5316\u6307\u9488 \\(i = 0\\) \u548c \\(j = n - 1\\) \uff0c\u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u548c\u5c3e\u5143\u7d20\uff0c\u4ee3\u8868\u641c\u7d22\u533a\u95f4 \\([0, n - 1]\\) \u3002\u8bf7\u6ce8\u610f\uff0c\u4e2d\u62ec\u53f7\u8868\u793a\u95ed\u533a\u95f4\uff0c\u5176\u5305\u542b\u8fb9\u754c\u503c\u672c\u8eab\u3002

        \u63a5\u4e0b\u6765\uff0c\u5faa\u73af\u6267\u884c\u4ee5\u4e0b\u4e24\u6b65\u3002

        1. \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 \\(m = \\lfloor {(i + j) / 2} \\rfloor\\) \uff0c\u5176\u4e2d \\(\\lfloor \\: \\rfloor\\) \u8868\u793a\u5411\u4e0b\u53d6\u6574\u64cd\u4f5c\u3002
        2. \u5224\u65ad nums[m] \u548c target \u7684\u5927\u5c0f\u5173\u7cfb\uff0c\u5206\u4e3a\u4ee5\u4e0b\u4e09\u79cd\u60c5\u51b5\u3002
          1. \u5f53 nums[m] < target \u65f6\uff0c\u8bf4\u660e target \u5728\u533a\u95f4 \\([m + 1, j]\\) \u4e2d\uff0c\u56e0\u6b64\u6267\u884c \\(i = m + 1\\) \u3002
          2. \u5f53 nums[m] > target \u65f6\uff0c\u8bf4\u660e target \u5728\u533a\u95f4 \\([i, m - 1]\\) \u4e2d\uff0c\u56e0\u6b64\u6267\u884c \\(j = m - 1\\) \u3002
          3. \u5f53 nums[m] = target \u65f6\uff0c\u8bf4\u660e\u627e\u5230 target \uff0c\u56e0\u6b64\u8fd4\u56de\u7d22\u5f15 \\(m\\) \u3002

        \u82e5\u6570\u7ec4\u4e0d\u5305\u542b\u76ee\u6807\u5143\u7d20\uff0c\u641c\u7d22\u533a\u95f4\u6700\u7ec8\u4f1a\u7f29\u5c0f\u4e3a\u7a7a\u3002\u6b64\u65f6\u8fd4\u56de \\(-1\\) \u3002

        <1><2><3><4><5><6><7>

        \u56fe 10-2 \u00a0 \u4e8c\u5206\u67e5\u627e\u6d41\u7a0b

        \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e \\(i\\) \u548c \\(j\\) \u90fd\u662f int \u7c7b\u578b\uff0c\u56e0\u6b64 \\(i + j\\) \u53ef\u80fd\u4f1a\u8d85\u51fa int \u7c7b\u578b\u7684\u53d6\u503c\u8303\u56f4\u3002\u4e3a\u4e86\u907f\u514d\u5927\u6570\u8d8a\u754c\uff0c\u6211\u4eec\u901a\u5e38\u91c7\u7528\u516c\u5f0f \\(m = \\lfloor {i + (j - i) / 2} \\rfloor\\) \u6765\u8ba1\u7b97\u4e2d\u70b9\u3002

        \u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search.py
        def binary_search(nums: list[int], target: int) -> int:\n    \"\"\"\u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09\"\"\"\n    # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n    i, j = 0, len(nums) - 1\n    # \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n    while i <= j:\n        # \u7406\u8bba\u4e0a Python \u7684\u6570\u5b57\u53ef\u4ee5\u65e0\u9650\u5927\uff08\u53d6\u51b3\u4e8e\u5185\u5b58\u5927\u5c0f\uff09\uff0c\u65e0\u987b\u8003\u8651\u5927\u6570\u8d8a\u754c\u95ee\u9898\n        m = (i + j) // 2  # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m] < target:\n            i = m + 1  # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        elif nums[m] > target:\n            j = m - 1  # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        else:\n            return m  # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n    return -1  # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n
        binary_search.cpp
        /* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(vector<int> &nums, int target) {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n    int i = 0, j = nums.size() - 1;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target)    // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1;\n        else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1;\n        else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.java
        /* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(int[] nums, int target) {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n    int i = 0, j = nums.length - 1;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1;\n        else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1;\n        else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.cs
        /* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint BinarySearch(int[] nums, int target) {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n    int i = 0, j = nums.Length - 1;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n    while (i <= j) {\n        int m = i + (j - i) / 2;   // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target)      // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1;\n        else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1;\n        else                       // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.go
        /* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunc binarySearch(nums []int, target int) int {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n    i, j := 0, len(nums)-1\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n    for i <= j {\n        m := i + (j-i)/2      // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1\n        } else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1\n        } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m\n        }\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1\n}\n
        binary_search.swift
        /* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunc binarySearch(nums: [Int], target: Int) -> Int {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n    var i = nums.startIndex\n    var j = nums.endIndex - 1\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n    while i <= j {\n        let m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1\n        } else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1\n        } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m\n        }\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1\n}\n
        binary_search.js
        /* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunction binarySearch(nums, target) {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n    let i = 0,\n        j = nums.length - 1;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n    while (i <= j) {\n        // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m \uff0c\u4f7f\u7528 parseInt() \u5411\u4e0b\u53d6\u6574\n        const m = parseInt(i + (j - i) / 2);\n        if (nums[m] < target)\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1;\n        else if (nums[m] > target)\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1;\n        else return m; // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.ts
        /* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunction binarySearch(nums: number[], target: number): number {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n    let i = 0,\n        j = nums.length - 1;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n    while (i <= j) {\n        // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        const m = Math.floor(i + (j - i) / 2);\n        if (nums[m] < target) {\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1;\n        } else if (nums[m] > target) {\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1;\n        } else {\n            // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n        }\n    }\n    return -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\n
        binary_search.dart
        /* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(List<int> nums, int target) {\n  // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n  int i = 0, j = nums.length - 1;\n  // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n  while (i <= j) {\n    int m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    if (nums[m] < target) {\n      // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n      i = m + 1;\n    } else if (nums[m] > target) {\n      // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n      j = m - 1;\n    } else {\n      // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n      return m;\n    }\n  }\n  // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n  return -1;\n}\n
        binary_search.rs
        /* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfn binary_search(nums: &[i32], target: i32) -> i32 {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n    let mut i = 0;\n    let mut j = nums.len() as i32 - 1;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n    while i <= j {\n        let m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m as usize] < target {\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1;\n        } else if nums[m as usize] > target {\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1;\n        } else {\n            // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n        }\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.c
        /* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(int *nums, int len, int target) {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n    int i = 0, j = len - 1;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target)    // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1;\n        else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1;\n        else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.kt
        /* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfun binarySearch(nums: IntArray, target: Int): Int {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n    var i = 0\n    var j = nums.size - 1\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n    while (i <= j) {\n        val m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1\n        else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1\n        else  // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1\n}\n
        binary_search.rb
        ### \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 ###\ndef binary_search(nums, target)\n  # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n  i, j = 0, nums.length - 1\n\n  # \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n  while i <= j\n    # \u7406\u8bba\u4e0a Ruby \u7684\u6570\u5b57\u53ef\u4ee5\u65e0\u9650\u5927\uff08\u53d6\u51b3\u4e8e\u5185\u5b58\u5927\u5c0f\uff09\uff0c\u65e0\u987b\u8003\u8651\u5927\u6570\u8d8a\u754c\u95ee\u9898\n    m = (i + j) / 2   # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n\n    if nums[m] < target\n      i = m + 1 # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n    elsif nums[m] > target\n      j = m - 1 # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n    else\n      return m  # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n    end\n  end\n\n  -1  # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \uff1a\u5728\u4e8c\u5206\u5faa\u73af\u4e2d\uff0c\u533a\u95f4\u6bcf\u8f6e\u7f29\u5c0f\u4e00\u534a\uff0c\u56e0\u6b64\u5faa\u73af\u6b21\u6570\u4e3a \\(\\log_2 n\\) \u3002

        \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff1a\u6307\u9488 \\(i\\) \u548c \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7a7a\u95f4\u3002

        "},{"location":"chapter_searching/binary_search/#1011","title":"10.1.1 \u00a0 \u533a\u95f4\u8868\u793a\u65b9\u6cd5","text":"

        \u9664\u4e86\u4e0a\u8ff0\u53cc\u95ed\u533a\u95f4\u5916\uff0c\u5e38\u89c1\u7684\u533a\u95f4\u8868\u793a\u8fd8\u6709\u201c\u5de6\u95ed\u53f3\u5f00\u201d\u533a\u95f4\uff0c\u5b9a\u4e49\u4e3a \\([0, n)\\) \uff0c\u5373\u5de6\u8fb9\u754c\u5305\u542b\u81ea\u8eab\uff0c\u53f3\u8fb9\u754c\u4e0d\u5305\u542b\u81ea\u8eab\u3002\u5728\u8be5\u8868\u793a\u4e0b\uff0c\u533a\u95f4 \\([i, j)\\) \u5728 \\(i = j\\) \u65f6\u4e3a\u7a7a\u3002

        \u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u8be5\u8868\u793a\u5b9e\u73b0\u5177\u6709\u76f8\u540c\u529f\u80fd\u7684\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search.py
        def binary_search_lcro(nums: list[int], target: int) -> int:\n    \"\"\"\u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09\"\"\"\n    # \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n    i, j = 0, len(nums)\n    # \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n    while i < j:\n        m = (i + j) // 2  # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m] < target:\n            i = m + 1  # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n        elif nums[m] > target:\n            j = m  # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n        else:\n            return m  # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n    return -1  # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n
        binary_search.cpp
        /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nint binarySearchLCRO(vector<int> &nums, int target) {\n    // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n    int i = 0, j = nums.size();\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n    while (i < j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target)    // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n            i = m + 1;\n        else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n            j = m;\n        else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.java
        /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nint binarySearchLCRO(int[] nums, int target) {\n    // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n    int i = 0, j = nums.length;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n    while (i < j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n            i = m + 1;\n        else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n            j = m;\n        else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.cs
        /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nint BinarySearchLCRO(int[] nums, int target) {\n    // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n    int i = 0, j = nums.Length;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n    while (i < j) {\n        int m = i + (j - i) / 2;   // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target)      // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n            i = m + 1;\n        else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n            j = m;\n        else                       // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.go
        /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nfunc binarySearchLCRO(nums []int, target int) int {\n    // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n    i, j := 0, len(nums)\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n    for i < j {\n        m := i + (j-i)/2      // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n            i = m + 1\n        } else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n            j = m\n        } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m\n        }\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1\n}\n
        binary_search.swift
        /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nfunc binarySearchLCRO(nums: [Int], target: Int) -> Int {\n    // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n    var i = nums.startIndex\n    var j = nums.endIndex\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n    while i < j {\n        let m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n            i = m + 1\n        } else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n            j = m\n        } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m\n        }\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1\n}\n
        binary_search.js
        /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nfunction binarySearchLCRO(nums, target) {\n    // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n    let i = 0,\n        j = nums.length;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n    while (i < j) {\n        // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m \uff0c\u4f7f\u7528 parseInt() \u5411\u4e0b\u53d6\u6574\n        const m = parseInt(i + (j - i) / 2);\n        if (nums[m] < target)\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n            i = m + 1;\n        else if (nums[m] > target)\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n            j = m;\n        // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n        else return m;\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.ts
        /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nfunction binarySearchLCRO(nums: number[], target: number): number {\n    // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n    let i = 0,\n        j = nums.length;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n    while (i < j) {\n        // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        const m = Math.floor(i + (j - i) / 2);\n        if (nums[m] < target) {\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n            i = m + 1;\n        } else if (nums[m] > target) {\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n            j = m;\n        } else {\n            // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n        }\n    }\n    return -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\n
        binary_search.dart
        /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nint binarySearchLCRO(List<int> nums, int target) {\n  // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n  int i = 0, j = nums.length;\n  // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n  while (i < j) {\n    int m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    if (nums[m] < target) {\n      // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n      i = m + 1;\n    } else if (nums[m] > target) {\n      // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n      j = m;\n    } else {\n      // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n      return m;\n    }\n  }\n  // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n  return -1;\n}\n
        binary_search.rs
        /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nfn binary_search_lcro(nums: &[i32], target: i32) -> i32 {\n    // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n    let mut i = 0;\n    let mut j = nums.len() as i32;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n    while i < j {\n        let m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m as usize] < target {\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n            i = m + 1;\n        } else if nums[m as usize] > target {\n            // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n            j = m;\n        } else {\n            // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n        }\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.c
        /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nint binarySearchLCRO(int *nums, int len, int target) {\n    // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n    int i = 0, j = len;\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n    while (i < j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target)    // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n            i = m + 1;\n        else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n            j = m;\n        else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m;\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1;\n}\n
        binary_search.kt
        /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nfun binarySearchLCRO(nums: IntArray, target: Int): Int {\n    // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n    var i = 0\n    var j = nums.size\n    // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n    while (i < j) {\n        val m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n            i = m + 1\n        else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n            j = m\n        else  // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n            return m\n    }\n    // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n    return -1\n}\n
        binary_search.rb
        ### \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 ###\ndef binary_search_lcro(nums, target)\n  # \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n  i, j = 0, nums.length\n\n  # \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n  while i < j\n    # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    m = (i + j) / 2\n\n    if nums[m] < target\n      i = m + 1 # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n    elsif nums[m] > target\n      j = m - 1 # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n    else\n      return m  # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n    end\n  end\n\n  -1  # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u5982\u56fe 10-3 \u6240\u793a\uff0c\u5728\u4e24\u79cd\u533a\u95f4\u8868\u793a\u4e0b\uff0c\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u7684\u521d\u59cb\u5316\u3001\u5faa\u73af\u6761\u4ef6\u548c\u7f29\u5c0f\u533a\u95f4\u64cd\u4f5c\u7686\u6709\u6240\u4e0d\u540c\u3002

        \u7531\u4e8e\u201c\u53cc\u95ed\u533a\u95f4\u201d\u8868\u793a\u4e2d\u7684\u5de6\u53f3\u8fb9\u754c\u90fd\u88ab\u5b9a\u4e49\u4e3a\u95ed\u533a\u95f4\uff0c\u56e0\u6b64\u901a\u8fc7\u6307\u9488 \\(i\\) \u548c\u6307\u9488 \\(j\\) \u7f29\u5c0f\u533a\u95f4\u7684\u64cd\u4f5c\u4e5f\u662f\u5bf9\u79f0\u7684\u3002\u8fd9\u6837\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\uff0c\u56e0\u6b64\u4e00\u822c\u5efa\u8bae\u91c7\u7528\u201c\u53cc\u95ed\u533a\u95f4\u201d\u7684\u5199\u6cd5\u3002

        \u56fe 10-3 \u00a0 \u4e24\u79cd\u533a\u95f4\u5b9a\u4e49

        "},{"location":"chapter_searching/binary_search/#1012","title":"10.1.2 \u00a0 \u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"

        \u4e8c\u5206\u67e5\u627e\u5728\u65f6\u95f4\u548c\u7a7a\u95f4\u65b9\u9762\u90fd\u6709\u8f83\u597d\u7684\u6027\u80fd\u3002

        • \u4e8c\u5206\u67e5\u627e\u7684\u65f6\u95f4\u6548\u7387\u9ad8\u3002\u5728\u5927\u6570\u636e\u91cf\u4e0b\uff0c\u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5177\u6709\u663e\u8457\u4f18\u52bf\u3002\u4f8b\u5982\uff0c\u5f53\u6570\u636e\u5927\u5c0f \\(n = 2^{20}\\) \u65f6\uff0c\u7ebf\u6027\u67e5\u627e\u9700\u8981 \\(2^{20} = 1048576\\) \u8f6e\u5faa\u73af\uff0c\u800c\u4e8c\u5206\u67e5\u627e\u4ec5\u9700 \\(\\log_2 2^{20} = 20\\) \u8f6e\u5faa\u73af\u3002
        • \u4e8c\u5206\u67e5\u627e\u65e0\u987b\u989d\u5916\u7a7a\u95f4\u3002\u76f8\u8f83\u4e8e\u9700\u8981\u501f\u52a9\u989d\u5916\u7a7a\u95f4\u7684\u641c\u7d22\u7b97\u6cd5\uff08\u4f8b\u5982\u54c8\u5e0c\u67e5\u627e\uff09\uff0c\u4e8c\u5206\u67e5\u627e\u66f4\u52a0\u8282\u7701\u7a7a\u95f4\u3002

        \u7136\u800c\uff0c\u4e8c\u5206\u67e5\u627e\u5e76\u975e\u9002\u7528\u4e8e\u6240\u6709\u60c5\u51b5\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002

        • \u4e8c\u5206\u67e5\u627e\u4ec5\u9002\u7528\u4e8e\u6709\u5e8f\u6570\u636e\u3002\u82e5\u8f93\u5165\u6570\u636e\u65e0\u5e8f\uff0c\u4e3a\u4e86\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u800c\u4e13\u95e8\u8fdb\u884c\u6392\u5e8f\uff0c\u5f97\u4e0d\u507f\u5931\u3002\u56e0\u4e3a\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3a \\(O(n \\log n)\\) \uff0c\u6bd4\u7ebf\u6027\u67e5\u627e\u548c\u4e8c\u5206\u67e5\u627e\u90fd\u66f4\u9ad8\u3002\u5bf9\u4e8e\u9891\u7e41\u63d2\u5165\u5143\u7d20\u7684\u573a\u666f\uff0c\u4e3a\u4fdd\u6301\u6570\u7ec4\u6709\u5e8f\u6027\uff0c\u9700\u8981\u5c06\u5143\u7d20\u63d2\u5165\u5230\u7279\u5b9a\u4f4d\u7f6e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u4e5f\u662f\u975e\u5e38\u6602\u8d35\u7684\u3002
        • \u4e8c\u5206\u67e5\u627e\u4ec5\u9002\u7528\u4e8e\u6570\u7ec4\u3002\u4e8c\u5206\u67e5\u627e\u9700\u8981\u8df3\u8dc3\u5f0f\uff08\u975e\u8fde\u7eed\u5730\uff09\u8bbf\u95ee\u5143\u7d20\uff0c\u800c\u5728\u94fe\u8868\u4e2d\u6267\u884c\u8df3\u8dc3\u5f0f\u8bbf\u95ee\u7684\u6548\u7387\u8f83\u4f4e\uff0c\u56e0\u6b64\u4e0d\u9002\u5408\u5e94\u7528\u5728\u94fe\u8868\u6216\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u3002
        • \u5c0f\u6570\u636e\u91cf\u4e0b\uff0c\u7ebf\u6027\u67e5\u627e\u6027\u80fd\u66f4\u4f73\u3002\u5728\u7ebf\u6027\u67e5\u627e\u4e2d\uff0c\u6bcf\u8f6e\u53ea\u9700 1 \u6b21\u5224\u65ad\u64cd\u4f5c\uff1b\u800c\u5728\u4e8c\u5206\u67e5\u627e\u4e2d\uff0c\u9700\u8981 1 \u6b21\u52a0\u6cd5\u30011 \u6b21\u9664\u6cd5\u30011 ~ 3 \u6b21\u5224\u65ad\u64cd\u4f5c\u30011 \u6b21\u52a0\u6cd5\uff08\u51cf\u6cd5\uff09\uff0c\u5171 4 ~ 6 \u4e2a\u5355\u5143\u64cd\u4f5c\uff1b\u56e0\u6b64\uff0c\u5f53\u6570\u636e\u91cf \\(n\\) \u8f83\u5c0f\u65f6\uff0c\u7ebf\u6027\u67e5\u627e\u53cd\u800c\u6bd4\u4e8c\u5206\u67e5\u627e\u66f4\u5feb\u3002
        "},{"location":"chapter_searching/binary_search_edge/","title":"10.3 \u00a0 \u4e8c\u5206\u67e5\u627e\u8fb9\u754c","text":""},{"location":"chapter_searching/binary_search_edge/#1031","title":"10.3.1 \u00a0 \u67e5\u627e\u5de6\u8fb9\u754c","text":"

        Question

        \u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6709\u5e8f\u6570\u7ec4 nums \uff0c\u5176\u4e2d\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\u3002\u8bf7\u8fd4\u56de\u6570\u7ec4\u4e2d\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 target \u7684\u7d22\u5f15\u3002\u82e5\u6570\u7ec4\u4e2d\u4e0d\u5305\u542b\u8be5\u5143\u7d20\uff0c\u5219\u8fd4\u56de \\(-1\\) \u3002

        \u56de\u5fc6\u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\u7684\u65b9\u6cd5\uff0c\u641c\u7d22\u5b8c\u6210\u540e \\(i\\) \u6307\u5411\u6700\u5de6\u4e00\u4e2a target \uff0c\u56e0\u6b64\u67e5\u627e\u63d2\u5165\u70b9\u672c\u8d28\u4e0a\u662f\u5728\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target \u7684\u7d22\u5f15\u3002

        \u8003\u8651\u901a\u8fc7\u67e5\u627e\u63d2\u5165\u70b9\u7684\u51fd\u6570\u5b9e\u73b0\u67e5\u627e\u5de6\u8fb9\u754c\u3002\u8bf7\u6ce8\u610f\uff0c\u6570\u7ec4\u4e2d\u53ef\u80fd\u4e0d\u5305\u542b target \uff0c\u8fd9\u79cd\u60c5\u51b5\u53ef\u80fd\u5bfc\u81f4\u4ee5\u4e0b\u4e24\u79cd\u7ed3\u679c\u3002

        • \u63d2\u5165\u70b9\u7684\u7d22\u5f15 \\(i\\) \u8d8a\u754c\u3002
        • \u5143\u7d20 nums[i] \u4e0e target \u4e0d\u76f8\u7b49\u3002

        \u5f53\u9047\u5230\u4ee5\u4e0a\u4e24\u79cd\u60c5\u51b5\u65f6\uff0c\u76f4\u63a5\u8fd4\u56de \\(-1\\) \u5373\u53ef\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_edge.py
        def binary_search_left_edge(nums: list[int], target: int) -> int:\n    \"\"\"\u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target\"\"\"\n    # \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n    i = binary_search_insertion(nums, target)\n    # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if i == len(nums) or nums[i] != target:\n        return -1\n    # \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n    return i\n
        binary_search_edge.cpp
        /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(vector<int> &nums, int target) {\n    // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n    int i = binarySearchInsertion(nums, target);\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (i == nums.size() || nums[i] != target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n    return i;\n}\n
        binary_search_edge.java
        /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(int[] nums, int target) {\n    // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n    int i = binary_search_insertion.binarySearchInsertion(nums, target);\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (i == nums.length || nums[i] != target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n    return i;\n}\n
        binary_search_edge.cs
        /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint BinarySearchLeftEdge(int[] nums, int target) {\n    // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n    int i = binary_search_insertion.BinarySearchInsertion(nums, target);\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (i == nums.Length || nums[i] != target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n    return i;\n}\n
        binary_search_edge.go
        /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunc binarySearchLeftEdge(nums []int, target int) int {\n    // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n    i := binarySearchInsertion(nums, target)\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if i == len(nums) || nums[i] != target {\n        return -1\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n    return i\n}\n
        binary_search_edge.swift
        /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunc binarySearchLeftEdge(nums: [Int], target: Int) -> Int {\n    // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n    let i = binarySearchInsertion(nums: nums, target: target)\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if i == nums.endIndex || nums[i] != target {\n        return -1\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n    return i\n}\n
        binary_search_edge.js
        /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunction binarySearchLeftEdge(nums, target) {\n    // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n    const i = binarySearchInsertion(nums, target);\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (i === nums.length || nums[i] !== target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n    return i;\n}\n
        binary_search_edge.ts
        /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunction binarySearchLeftEdge(nums: Array<number>, target: number): number {\n    // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n    const i = binarySearchInsertion(nums, target);\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (i === nums.length || nums[i] !== target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n    return i;\n}\n
        binary_search_edge.dart
        /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(List<int> nums, int target) {\n  // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n  int i = binarySearchInsertion(nums, target);\n  // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n  if (i == nums.length || nums[i] != target) {\n    return -1;\n  }\n  // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n  return i;\n}\n
        binary_search_edge.rs
        /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfn binary_search_left_edge(nums: &[i32], target: i32) -> i32 {\n    // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n    let i = binary_search_insertion(nums, target);\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if i == nums.len() as i32 || nums[i as usize] != target {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n    i\n}\n
        binary_search_edge.c
        /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(int *nums, int numSize, int target) {\n    // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n    int i = binarySearchInsertion(nums, numSize, target);\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (i == numSize || nums[i] != target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n    return i;\n}\n
        binary_search_edge.kt
        /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfun binarySearchLeftEdge(nums: IntArray, target: Int): Int {\n    // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n    val i = binarySearchInsertion(nums, target)\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (i == nums.size || nums[i] != target) {\n        return -1\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n    return i\n}\n
        binary_search_edge.rb
        ### \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target ###\ndef binary_search_left_edge(nums, target)\n  # \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n  i = binary_search_insertion(nums, target)\n\n  # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n  return -1 if i == nums.length || nums[i] != target\n\n  i # \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_searching/binary_search_edge/#1032","title":"10.3.2 \u00a0 \u67e5\u627e\u53f3\u8fb9\u754c","text":"

        \u90a3\u4e48\u5982\u4f55\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target \u5462\uff1f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u662f\u4fee\u6539\u4ee3\u7801\uff0c\u66ff\u6362\u5728 nums[m] == target \u60c5\u51b5\u4e0b\u7684\u6307\u9488\u6536\u7f29\u64cd\u4f5c\u3002\u4ee3\u7801\u5728\u6b64\u7701\u7565\uff0c\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u3002

        \u4e0b\u9762\u6211\u4eec\u4ecb\u7ecd\u4e24\u79cd\u66f4\u52a0\u53d6\u5de7\u7684\u65b9\u6cd5\u3002

        "},{"location":"chapter_searching/binary_search_edge/#1","title":"1. \u00a0 \u590d\u7528\u67e5\u627e\u5de6\u8fb9\u754c","text":"

        \u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u67e5\u627e\u6700\u5de6\u5143\u7d20\u7684\u51fd\u6570\u6765\u67e5\u627e\u6700\u53f3\u5143\u7d20\uff0c\u5177\u4f53\u65b9\u6cd5\u4e3a\uff1a\u5c06\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\u3002

        \u5982\u56fe 10-7 \u6240\u793a\uff0c\u67e5\u627e\u5b8c\u6210\u540e\uff0c\u6307\u9488 \\(i\\) \u6307\u5411\u6700\u5de6\u4e00\u4e2a target + 1\uff08\u5982\u679c\u5b58\u5728\uff09\uff0c\u800c \\(j\\) \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0c\u56e0\u6b64\u8fd4\u56de \\(j\\) \u5373\u53ef\u3002

        \u56fe 10-7 \u00a0 \u5c06\u67e5\u627e\u53f3\u8fb9\u754c\u8f6c\u5316\u4e3a\u67e5\u627e\u5de6\u8fb9\u754c

        \u8bf7\u6ce8\u610f\uff0c\u8fd4\u56de\u7684\u63d2\u5165\u70b9\u662f \\(i\\) \uff0c\u56e0\u6b64\u9700\u8981\u5c06\u5176\u51cf \\(1\\) \uff0c\u4ece\u800c\u83b7\u5f97 \\(j\\) \uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_edge.py
        def binary_search_right_edge(nums: list[int], target: int) -> int:\n    \"\"\"\u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target\"\"\"\n    # \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n    i = binary_search_insertion(nums, target + 1)\n    # j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n    j = i - 1\n    # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if j == -1 or nums[j] != target:\n        return -1\n    # \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n    return j\n
        binary_search_edge.cpp
        /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(vector<int> &nums, int target) {\n    // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n    int i = binarySearchInsertion(nums, target + 1);\n    // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n    int j = i - 1;\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n    return j;\n}\n
        binary_search_edge.java
        /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(int[] nums, int target) {\n    // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n    int i = binary_search_insertion.binarySearchInsertion(nums, target + 1);\n    // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n    int j = i - 1;\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n    return j;\n}\n
        binary_search_edge.cs
        /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint BinarySearchRightEdge(int[] nums, int target) {\n    // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n    int i = binary_search_insertion.BinarySearchInsertion(nums, target + 1);\n    // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n    int j = i - 1;\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n    return j;\n}\n
        binary_search_edge.go
        /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunc binarySearchRightEdge(nums []int, target int) int {\n    // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n    i := binarySearchInsertion(nums, target+1)\n    // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n    j := i - 1\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if j == -1 || nums[j] != target {\n        return -1\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n    return j\n}\n
        binary_search_edge.swift
        /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunc binarySearchRightEdge(nums: [Int], target: Int) -> Int {\n    // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n    let i = binarySearchInsertion(nums: nums, target: target + 1)\n    // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n    let j = i - 1\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if j == -1 || nums[j] != target {\n        return -1\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n    return j\n}\n
        binary_search_edge.js
        /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunction binarySearchRightEdge(nums, target) {\n    // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n    const i = binarySearchInsertion(nums, target + 1);\n    // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n    const j = i - 1;\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (j === -1 || nums[j] !== target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n    return j;\n}\n
        binary_search_edge.ts
        /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunction binarySearchRightEdge(nums: Array<number>, target: number): number {\n    // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n    const i = binarySearchInsertion(nums, target + 1);\n    // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n    const j = i - 1;\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (j === -1 || nums[j] !== target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n    return j;\n}\n
        binary_search_edge.dart
        /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(List<int> nums, int target) {\n  // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n  int i = binarySearchInsertion(nums, target + 1);\n  // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n  int j = i - 1;\n  // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n  if (j == -1 || nums[j] != target) {\n    return -1;\n  }\n  // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n  return j;\n}\n
        binary_search_edge.rs
        /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfn binary_search_right_edge(nums: &[i32], target: i32) -> i32 {\n    // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n    let i = binary_search_insertion(nums, target + 1);\n    // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n    let j = i - 1;\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if j == -1 || nums[j as usize] != target {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n    j\n}\n
        binary_search_edge.c
        /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(int *nums, int numSize, int target) {\n    // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n    int i = binarySearchInsertion(nums, numSize, target + 1);\n    // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n    int j = i - 1;\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n    return j;\n}\n
        binary_search_edge.kt
        /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfun binarySearchRightEdge(nums: IntArray, target: Int): Int {\n    // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n    val i = binarySearchInsertion(nums, target + 1)\n    // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n    val j = i - 1\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n    if (j == -1 || nums[j] != target) {\n        return -1\n    }\n    // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n    return j\n}\n
        binary_search_edge.rb
        ### \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target ###\ndef binary_search_right_edge(nums, target)\n  # \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n  i = binary_search_insertion(nums, target + 1)\n\n  # j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n  j = i - 1\n\n  # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n  return -1 if j == -1 || nums[j] != target\n\n  j # \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_searching/binary_search_edge/#2","title":"2. \u00a0 \u8f6c\u5316\u4e3a\u67e5\u627e\u5143\u7d20","text":"

        \u6211\u4eec\u77e5\u9053\uff0c\u5f53\u6570\u7ec4\u4e0d\u5305\u542b target \u65f6\uff0c\u6700\u7ec8 \\(i\\) \u548c \\(j\\) \u4f1a\u5206\u522b\u6307\u5411\u9996\u4e2a\u5927\u4e8e\u3001\u5c0f\u4e8e target \u7684\u5143\u7d20\u3002

        \u56e0\u6b64\uff0c\u5982\u56fe 10-8 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u6784\u9020\u4e00\u4e2a\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728\u7684\u5143\u7d20\uff0c\u7528\u4e8e\u67e5\u627e\u5de6\u53f3\u8fb9\u754c\u3002

        • \u67e5\u627e\u6700\u5de6\u4e00\u4e2a target \uff1a\u53ef\u4ee5\u8f6c\u5316\u4e3a\u67e5\u627e target - 0.5 \uff0c\u5e76\u8fd4\u56de\u6307\u9488 \\(i\\) \u3002
        • \u67e5\u627e\u6700\u53f3\u4e00\u4e2a target \uff1a\u53ef\u4ee5\u8f6c\u5316\u4e3a\u67e5\u627e target + 0.5 \uff0c\u5e76\u8fd4\u56de\u6307\u9488 \\(j\\) \u3002

        \u56fe 10-8 \u00a0 \u5c06\u67e5\u627e\u8fb9\u754c\u8f6c\u5316\u4e3a\u67e5\u627e\u5143\u7d20

        \u4ee3\u7801\u5728\u6b64\u7701\u7565\uff0c\u4ee5\u4e0b\u4e24\u70b9\u503c\u5f97\u6ce8\u610f\u3002

        • \u7ed9\u5b9a\u6570\u7ec4\u4e0d\u5305\u542b\u5c0f\u6570\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u65e0\u987b\u5173\u5fc3\u5982\u4f55\u5904\u7406\u76f8\u7b49\u7684\u60c5\u51b5\u3002
        • \u56e0\u4e3a\u8be5\u65b9\u6cd5\u5f15\u5165\u4e86\u5c0f\u6570\uff0c\u6240\u4ee5\u9700\u8981\u5c06\u51fd\u6570\u4e2d\u7684\u53d8\u91cf target \u6539\u4e3a\u6d6e\u70b9\u6570\u7c7b\u578b\uff08Python \u65e0\u987b\u6539\u52a8\uff09\u3002
        "},{"location":"chapter_searching/binary_search_insertion/","title":"10.2 \u00a0 \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9","text":"

        \u4e8c\u5206\u67e5\u627e\u4e0d\u4ec5\u53ef\u7528\u4e8e\u641c\u7d22\u76ee\u6807\u5143\u7d20\uff0c\u8fd8\u53ef\u7528\u4e8e\u89e3\u51b3\u8bb8\u591a\u53d8\u79cd\u95ee\u9898\uff0c\u6bd4\u5982\u641c\u7d22\u76ee\u6807\u5143\u7d20\u7684\u63d2\u5165\u4f4d\u7f6e\u3002

        "},{"location":"chapter_searching/binary_search_insertion/#1021","title":"10.2.1 \u00a0 \u65e0\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"

        Question

        \u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6709\u5e8f\u6570\u7ec4 nums \u548c\u4e00\u4e2a\u5143\u7d20 target \uff0c\u6570\u7ec4\u4e0d\u5b58\u5728\u91cd\u590d\u5143\u7d20\u3002\u73b0\u5c06 target \u63d2\u5165\u6570\u7ec4 nums \u4e2d\uff0c\u5e76\u4fdd\u6301\u5176\u6709\u5e8f\u6027\u3002\u82e5\u6570\u7ec4\u4e2d\u5df2\u5b58\u5728\u5143\u7d20 target \uff0c\u5219\u63d2\u5165\u5230\u5176\u5de6\u65b9\u3002\u8bf7\u8fd4\u56de\u63d2\u5165\u540e target \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\u3002\u793a\u4f8b\u5982\u56fe 10-4 \u6240\u793a\u3002

        \u56fe 10-4 \u00a0 \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\u793a\u4f8b\u6570\u636e

        \u5982\u679c\u60f3\u590d\u7528\u4e0a\u4e00\u8282\u7684\u4e8c\u5206\u67e5\u627e\u4ee3\u7801\uff0c\u5219\u9700\u8981\u56de\u7b54\u4ee5\u4e0b\u4e24\u4e2a\u95ee\u9898\u3002

        \u95ee\u9898\u4e00\uff1a\u5f53\u6570\u7ec4\u4e2d\u5305\u542b target \u65f6\uff0c\u63d2\u5165\u70b9\u7684\u7d22\u5f15\u662f\u5426\u662f\u8be5\u5143\u7d20\u7684\u7d22\u5f15\uff1f

        \u9898\u76ee\u8981\u6c42\u5c06 target \u63d2\u5165\u5230\u76f8\u7b49\u5143\u7d20\u7684\u5de6\u8fb9\uff0c\u8fd9\u610f\u5473\u7740\u65b0\u63d2\u5165\u7684 target \u66ff\u6362\u4e86\u539f\u6765 target \u7684\u4f4d\u7f6e\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u6570\u7ec4\u5305\u542b target \u65f6\uff0c\u63d2\u5165\u70b9\u7684\u7d22\u5f15\u5c31\u662f\u8be5 target \u7684\u7d22\u5f15\u3002

        \u95ee\u9898\u4e8c\uff1a\u5f53\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728 target \u65f6\uff0c\u63d2\u5165\u70b9\u662f\u54ea\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff1f

        \u8fdb\u4e00\u6b65\u601d\u8003\u4e8c\u5206\u67e5\u627e\u8fc7\u7a0b\uff1a\u5f53 nums[m] < target \u65f6 \\(i\\) \u79fb\u52a8\uff0c\u8fd9\u610f\u5473\u7740\u6307\u9488 \\(i\\) \u5728\u5411\u5927\u4e8e\u7b49\u4e8e target \u7684\u5143\u7d20\u9760\u8fd1\u3002\u540c\u7406\uff0c\u6307\u9488 \\(j\\) \u59cb\u7ec8\u5728\u5411\u5c0f\u4e8e\u7b49\u4e8e target \u7684\u5143\u7d20\u9760\u8fd1\u3002

        \u56e0\u6b64\u4e8c\u5206\u7ed3\u675f\u65f6\u4e00\u5b9a\u6709\uff1a\\(i\\) \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\uff0c\\(j\\) \u6307\u5411\u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u3002\u6613\u5f97\u5f53\u6570\u7ec4\u4e0d\u5305\u542b target \u65f6\uff0c\u63d2\u5165\u7d22\u5f15\u4e3a \\(i\\) \u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_insertion.py
        def binary_search_insertion_simple(nums: list[int], target: int) -> int:\n    \"\"\"\u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09\"\"\"\n    i, j = 0, len(nums) - 1  # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while i <= j:\n        m = (i + j) // 2  # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m] < target:\n            i = m + 1  # target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        elif nums[m] > target:\n            j = m - 1  # target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        else:\n            return m  # \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n    # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i\n
        binary_search_insertion.cpp
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(vector<int> &nums, int target) {\n    int i = 0, j = nums.size() - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n        }\n    }\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.java
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(int[] nums, int target) {\n    int i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n        }\n    }\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.cs
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint BinarySearchInsertionSimple(int[] nums, int target) {\n    int i = 0, j = nums.Length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n        }\n    }\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.go
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertionSimple(nums []int, target int) int {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    i, j := 0, len(nums)-1\n    for i <= j {\n        // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        m := i + (j-i)/2\n        if nums[m] < target {\n            // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1\n        } else if nums[m] > target {\n            // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1\n        } else {\n            // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n            return m\n        }\n    }\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i\n}\n
        binary_search_insertion.swift
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertionSimple(nums: [Int], target: Int) -> Int {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    var i = nums.startIndex\n    var j = nums.endIndex - 1\n    while i <= j {\n        let m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m] < target {\n            i = m + 1 // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if nums[m] > target {\n            j = m - 1 // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            return m // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n        }\n    }\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i\n}\n
        binary_search_insertion.js
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertionSimple(nums, target) {\n    let i = 0,\n        j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        const m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n        }\n    }\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.ts
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertionSimple(\n    nums: Array<number>,\n    target: number\n): number {\n    let i = 0,\n        j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        const m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n        }\n    }\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.dart
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(List<int> nums, int target) {\n  int i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n  while (i <= j) {\n    int m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    if (nums[m] < target) {\n      i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n    } else if (nums[m] > target) {\n      j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n    } else {\n      return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n    }\n  }\n  // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n  return i;\n}\n
        binary_search_insertion.rs
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfn binary_search_insertion_simple(nums: &[i32], target: i32) -> i32 {\n    let (mut i, mut j) = (0, nums.len() as i32 - 1); // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while i <= j {\n        let m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m as usize] < target {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if nums[m as usize] > target {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            return m;\n        }\n    }\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n    i\n}\n
        binary_search_insertion.c
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(int *nums, int numSize, int target) {\n    int i = 0, j = numSize - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n        }\n    }\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.kt
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfun binarySearchInsertionSimple(nums: IntArray, target: Int): Int {\n    var i = 0\n    var j = nums.size - 1 // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        val m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) {\n            i = m + 1 // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1 // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            return m // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n        }\n    }\n    // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i\n}\n
        binary_search_insertion.rb
        ### \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 ###\ndef binary_search_insertion_simple(nums, target)\n  # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n  i, j = 0, nums.length - 1\n\n  while i <= j\n    # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    m = (i + j) / 2\n\n    if nums[m] < target\n      i = m + 1 # target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n    elsif nums[m] > target\n      j = m - 1 # target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n    else\n      return m  # \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n    end\n  end\n\n  i # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_searching/binary_search_insertion/#1022","title":"10.2.2 \u00a0 \u5b58\u5728\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"

        Question

        \u5728\u4e0a\u4e00\u9898\u7684\u57fa\u7840\u4e0a\uff0c\u89c4\u5b9a\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u5176\u4f59\u4e0d\u53d8\u3002

        \u5047\u8bbe\u6570\u7ec4\u4e2d\u5b58\u5728\u591a\u4e2a target \uff0c\u5219\u666e\u901a\u4e8c\u5206\u67e5\u627e\u53ea\u80fd\u8fd4\u56de\u5176\u4e2d\u4e00\u4e2a target \u7684\u7d22\u5f15\uff0c\u800c\u65e0\u6cd5\u786e\u5b9a\u8be5\u5143\u7d20\u7684\u5de6\u8fb9\u548c\u53f3\u8fb9\u8fd8\u6709\u591a\u5c11 target\u3002

        \u9898\u76ee\u8981\u6c42\u5c06\u76ee\u6807\u5143\u7d20\u63d2\u5165\u5230\u6700\u5de6\u8fb9\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5de6\u4e00\u4e2a target \u7684\u7d22\u5f15\u3002\u521d\u6b65\u8003\u8651\u901a\u8fc7\u56fe 10-5 \u6240\u793a\u7684\u6b65\u9aa4\u5b9e\u73b0\u3002

        1. \u6267\u884c\u4e8c\u5206\u67e5\u627e\uff0c\u5f97\u5230\u4efb\u610f\u4e00\u4e2a target \u7684\u7d22\u5f15\uff0c\u8bb0\u4e3a \\(k\\) \u3002
        2. \u4ece\u7d22\u5f15 \\(k\\) \u5f00\u59cb\uff0c\u5411\u5de6\u8fdb\u884c\u7ebf\u6027\u904d\u5386\uff0c\u5f53\u627e\u5230\u6700\u5de6\u8fb9\u7684 target \u65f6\u8fd4\u56de\u3002

        \u56fe 10-5 \u00a0 \u7ebf\u6027\u67e5\u627e\u91cd\u590d\u5143\u7d20\u7684\u63d2\u5165\u70b9

        \u6b64\u65b9\u6cd5\u867d\u7136\u53ef\u7528\uff0c\u4f46\u5176\u5305\u542b\u7ebf\u6027\u67e5\u627e\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002\u5f53\u6570\u7ec4\u4e2d\u5b58\u5728\u5f88\u591a\u91cd\u590d\u7684 target \u65f6\uff0c\u8be5\u65b9\u6cd5\u6548\u7387\u5f88\u4f4e\u3002

        \u73b0\u8003\u8651\u62d3\u5c55\u4e8c\u5206\u67e5\u627e\u4ee3\u7801\u3002\u5982\u56fe 10-6 \u6240\u793a\uff0c\u6574\u4f53\u6d41\u7a0b\u4fdd\u6301\u4e0d\u53d8\uff0c\u6bcf\u8f6e\u5148\u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 \\(m\\) \uff0c\u518d\u5224\u65ad target \u548c nums[m] \u7684\u5927\u5c0f\u5173\u7cfb\uff0c\u5206\u4e3a\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5\u3002

        • \u5f53 nums[m] < target \u6216 nums[m] > target \u65f6\uff0c\u8bf4\u660e\u8fd8\u6ca1\u6709\u627e\u5230 target \uff0c\u56e0\u6b64\u91c7\u7528\u666e\u901a\u4e8c\u5206\u67e5\u627e\u7684\u7f29\u5c0f\u533a\u95f4\u64cd\u4f5c\uff0c\u4ece\u800c\u4f7f\u6307\u9488 \\(i\\) \u548c \\(j\\) \u5411 target \u9760\u8fd1\u3002
        • \u5f53 nums[m] == target \u65f6\uff0c\u8bf4\u660e\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 \\([i, m - 1]\\) \u4e2d\uff0c\u56e0\u6b64\u91c7\u7528 \\(j = m - 1\\) \u6765\u7f29\u5c0f\u533a\u95f4\uff0c\u4ece\u800c\u4f7f\u6307\u9488 \\(j\\) \u5411\u5c0f\u4e8e target \u7684\u5143\u7d20\u9760\u8fd1\u3002

        \u5faa\u73af\u5b8c\u6210\u540e\uff0c\\(i\\) \u6307\u5411\u6700\u5de6\u8fb9\u7684 target \uff0c\\(j\\) \u6307\u5411\u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\uff0c\u56e0\u6b64\u7d22\u5f15 \\(i\\) \u5c31\u662f\u63d2\u5165\u70b9\u3002

        <1><2><3><4><5><6><7><8>

        \u56fe 10-6 \u00a0 \u4e8c\u5206\u67e5\u627e\u91cd\u590d\u5143\u7d20\u7684\u63d2\u5165\u70b9\u7684\u6b65\u9aa4

        \u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5224\u65ad\u5206\u652f nums[m] > target \u548c nums[m] == target \u7684\u64cd\u4f5c\u76f8\u540c\uff0c\u56e0\u6b64\u4e24\u8005\u53ef\u4ee5\u5408\u5e76\u3002

        \u5373\u4fbf\u5982\u6b64\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u5c06\u5224\u65ad\u6761\u4ef6\u4fdd\u6301\u5c55\u5f00\uff0c\u56e0\u4e3a\u5176\u903b\u8f91\u66f4\u52a0\u6e05\u6670\u3001\u53ef\u8bfb\u6027\u66f4\u597d\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_insertion.py
        def binary_search_insertion(nums: list[int], target: int) -> int:\n    \"\"\"\u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09\"\"\"\n    i, j = 0, len(nums) - 1  # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while i <= j:\n        m = (i + j) // 2  # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m] < target:\n            i = m + 1  # target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        elif nums[m] > target:\n            j = m - 1  # target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        else:\n            j = m - 1  # \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n    # \u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i\n
        binary_search_insertion.cpp
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(vector<int> &nums, int target) {\n    int i = 0, j = nums.size() - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n        }\n    }\n    // \u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.java
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(int[] nums, int target) {\n    int i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n        }\n    }\n    // \u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.cs
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint BinarySearchInsertion(int[] nums, int target) {\n    int i = 0, j = nums.Length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n        }\n    }\n    // \u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.go
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertion(nums []int, target int) int {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    i, j := 0, len(nums)-1\n    for i <= j {\n        // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        m := i + (j-i)/2\n        if nums[m] < target {\n            // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n            i = m + 1\n        } else if nums[m] > target {\n            // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1\n        } else {\n            // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n            j = m - 1\n        }\n    }\n    // \u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i\n}\n
        binary_search_insertion.swift
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertion(nums: [Int], target: Int) -> Int {\n    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    var i = nums.startIndex\n    var j = nums.endIndex - 1\n    while i <= j {\n        let m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m] < target {\n            i = m + 1 // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if nums[m] > target {\n            j = m - 1 // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            j = m - 1 // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n        }\n    }\n    // \u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i\n}\n
        binary_search_insertion.js
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertion(nums, target) {\n    let i = 0,\n        j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        const m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n        }\n    }\n    // \u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.ts
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertion(nums: Array<number>, target: number): number {\n    let i = 0,\n        j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        const m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n        }\n    }\n    // \u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.dart
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(List<int> nums, int target) {\n  int i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n  while (i <= j) {\n    int m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    if (nums[m] < target) {\n      i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n    } else if (nums[m] > target) {\n      j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n    } else {\n      j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n    }\n  }\n  // \u8fd4\u56de\u63d2\u5165\u70b9 i\n  return i;\n}\n
        binary_search_insertion.rs
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\npub fn binary_search_insertion(nums: &[i32], target: i32) -> i32 {\n    let (mut i, mut j) = (0, nums.len() as i32 - 1); // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while i <= j {\n        let m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if nums[m as usize] < target {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if nums[m as usize] > target {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n        }\n    }\n    // \u8fd4\u56de\u63d2\u5165\u70b9 i\n    i\n}\n
        binary_search_insertion.c
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(int *nums, int numSize, int target) {\n    int i = 0, j = numSize - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) {\n            i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n        }\n    }\n    // \u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i;\n}\n
        binary_search_insertion.kt
        /* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfun binarySearchInsertion(nums: IntArray, target: Int): Int {\n    var i = 0\n    var j = nums.size - 1 // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n    while (i <= j) {\n        val m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n        if (nums[m] < target) {\n            i = m + 1 // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n        } else if (nums[m] > target) {\n            j = m - 1 // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n        } else {\n            j = m - 1 // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n        }\n    }\n    // \u8fd4\u56de\u63d2\u5165\u70b9 i\n    return i\n}\n
        binary_search_insertion.rb
        ### \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 ###\ndef binary_search_insertion(nums, target)\n  # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n  i, j = 0, nums.length - 1\n\n  while i <= j\n    # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n    m = (i + j) / 2\n\n    if nums[m] < target\n      i = m + 1 # target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n    elsif nums[m] > target\n      j = m - 1 # target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n    else\n      j = m - 1 # \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n    end\n  end\n\n  i # \u8fd4\u56de\u63d2\u5165\u70b9 i\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        Tip

        \u672c\u8282\u7684\u4ee3\u7801\u90fd\u662f\u201c\u53cc\u95ed\u533a\u95f4\u201d\u5199\u6cd5\u3002\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u201c\u5de6\u95ed\u53f3\u5f00\u201d\u5199\u6cd5\u3002

        \u603b\u7684\u6765\u770b\uff0c\u4e8c\u5206\u67e5\u627e\u65e0\u975e\u5c31\u662f\u7ed9\u6307\u9488 \\(i\\) \u548c \\(j\\) \u5206\u522b\u8bbe\u5b9a\u641c\u7d22\u76ee\u6807\uff0c\u76ee\u6807\u53ef\u80fd\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u5143\u7d20\uff08\u4f8b\u5982 target \uff09\uff0c\u4e5f\u53ef\u80fd\u662f\u4e00\u4e2a\u5143\u7d20\u8303\u56f4\uff08\u4f8b\u5982\u5c0f\u4e8e target \u7684\u5143\u7d20\uff09\u3002

        \u5728\u4e0d\u65ad\u7684\u5faa\u73af\u4e8c\u5206\u4e2d\uff0c\u6307\u9488 \\(i\\) \u548c \\(j\\) \u90fd\u9010\u6e10\u903c\u8fd1\u9884\u5148\u8bbe\u5b9a\u7684\u76ee\u6807\u3002\u6700\u7ec8\uff0c\u5b83\u4eec\u6216\u662f\u6210\u529f\u627e\u5230\u7b54\u6848\uff0c\u6216\u662f\u8d8a\u8fc7\u8fb9\u754c\u540e\u505c\u6b62\u3002

        "},{"location":"chapter_searching/replace_linear_by_hashing/","title":"10.4 \u00a0 \u54c8\u5e0c\u4f18\u5316\u7b56\u7565","text":"

        \u5728\u7b97\u6cd5\u9898\u4e2d\uff0c\u6211\u4eec\u5e38\u901a\u8fc7\u5c06\u7ebf\u6027\u67e5\u627e\u66ff\u6362\u4e3a\u54c8\u5e0c\u67e5\u627e\u6765\u964d\u4f4e\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u6211\u4eec\u501f\u52a9\u4e00\u4e2a\u7b97\u6cd5\u9898\u6765\u52a0\u6df1\u7406\u89e3\u3002

        Question

        \u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums \u548c\u4e00\u4e2a\u76ee\u6807\u5143\u7d20 target \uff0c\u8bf7\u5728\u6570\u7ec4\u4e2d\u641c\u7d22\u201c\u548c\u201d\u4e3a target \u7684\u4e24\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u6570\u7ec4\u7d22\u5f15\u3002\u8fd4\u56de\u4efb\u610f\u4e00\u4e2a\u89e3\u5373\u53ef\u3002

        "},{"location":"chapter_searching/replace_linear_by_hashing/#1041","title":"10.4.1 \u00a0 \u7ebf\u6027\u67e5\u627e\uff1a\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4","text":"

        \u8003\u8651\u76f4\u63a5\u904d\u5386\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\u3002\u5982\u56fe 10-9 \u6240\u793a\uff0c\u6211\u4eec\u5f00\u542f\u4e00\u4e2a\u4e24\u5c42\u5faa\u73af\uff0c\u5728\u6bcf\u8f6e\u4e2d\u5224\u65ad\u4e24\u4e2a\u6574\u6570\u7684\u548c\u662f\u5426\u4e3a target \uff0c\u82e5\u662f\uff0c\u5219\u8fd4\u56de\u5b83\u4eec\u7684\u7d22\u5f15\u3002

        \u56fe 10-9 \u00a0 \u7ebf\u6027\u67e5\u627e\u6c42\u89e3\u4e24\u6570\u4e4b\u548c

        \u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby two_sum.py
        def two_sum_brute_force(nums: list[int], target: int) -> list[int]:\n    \"\"\"\u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e\"\"\"\n    # \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n    for i in range(len(nums) - 1):\n        for j in range(i + 1, len(nums)):\n            if nums[i] + nums[j] == target:\n                return [i, j]\n    return []\n
        two_sum.cpp
        /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nvector<int> twoSumBruteForce(vector<int> &nums, int target) {\n    int size = nums.size();\n    // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n    for (int i = 0; i < size - 1; i++) {\n        for (int j = i + 1; j < size; j++) {\n            if (nums[i] + nums[j] == target)\n                return {i, j};\n        }\n    }\n    return {};\n}\n
        two_sum.java
        /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint[] twoSumBruteForce(int[] nums, int target) {\n    int size = nums.length;\n    // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n    for (int i = 0; i < size - 1; i++) {\n        for (int j = i + 1; j < size; j++) {\n            if (nums[i] + nums[j] == target)\n                return new int[] { i, j };\n        }\n    }\n    return new int[0];\n}\n
        two_sum.cs
        /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint[] TwoSumBruteForce(int[] nums, int target) {\n    int size = nums.Length;\n    // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n    for (int i = 0; i < size - 1; i++) {\n        for (int j = i + 1; j < size; j++) {\n            if (nums[i] + nums[j] == target)\n                return [i, j];\n        }\n    }\n    return [];\n}\n
        two_sum.go
        /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunc twoSumBruteForce(nums []int, target int) []int {\n    size := len(nums)\n    // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n    for i := 0; i < size-1; i++ {\n        for j := i + 1; j < size; j++ {\n            if nums[i]+nums[j] == target {\n                return []int{i, j}\n            }\n        }\n    }\n    return nil\n}\n
        two_sum.swift
        /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunc twoSumBruteForce(nums: [Int], target: Int) -> [Int] {\n    // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n    for i in nums.indices.dropLast() {\n        for j in nums.indices.dropFirst(i + 1) {\n            if nums[i] + nums[j] == target {\n                return [i, j]\n            }\n        }\n    }\n    return [0]\n}\n
        two_sum.js
        /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunction twoSumBruteForce(nums, target) {\n    const n = nums.length;\n    // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n    for (let i = 0; i < n; i++) {\n        for (let j = i + 1; j < n; j++) {\n            if (nums[i] + nums[j] === target) {\n                return [i, j];\n            }\n        }\n    }\n    return [];\n}\n
        two_sum.ts
        /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunction twoSumBruteForce(nums: number[], target: number): number[] {\n    const n = nums.length;\n    // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n    for (let i = 0; i < n; i++) {\n        for (let j = i + 1; j < n; j++) {\n            if (nums[i] + nums[j] === target) {\n                return [i, j];\n            }\n        }\n    }\n    return [];\n}\n
        two_sum.dart
        /* \u65b9\u6cd5\u4e00\uff1a \u66b4\u529b\u679a\u4e3e */\nList<int> twoSumBruteForce(List<int> nums, int target) {\n  int size = nums.length;\n  // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n  for (var i = 0; i < size - 1; i++) {\n    for (var j = i + 1; j < size; j++) {\n      if (nums[i] + nums[j] == target) return [i, j];\n    }\n  }\n  return [0];\n}\n
        two_sum.rs
        /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\npub fn two_sum_brute_force(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {\n    let size = nums.len();\n    // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n    for i in 0..size - 1 {\n        for j in i + 1..size {\n            if nums[i] + nums[j] == target {\n                return Some(vec![i as i32, j as i32]);\n            }\n        }\n    }\n    None\n}\n
        two_sum.c
        /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint *twoSumBruteForce(int *nums, int numsSize, int target, int *returnSize) {\n    for (int i = 0; i < numsSize; ++i) {\n        for (int j = i + 1; j < numsSize; ++j) {\n            if (nums[i] + nums[j] == target) {\n                int *res = malloc(sizeof(int) * 2);\n                res[0] = i, res[1] = j;\n                *returnSize = 2;\n                return res;\n            }\n        }\n    }\n    *returnSize = 0;\n    return NULL;\n}\n
        two_sum.kt
        /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfun twoSumBruteForce(nums: IntArray, target: Int): IntArray {\n    val size = nums.size\n    // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n    for (i in 0..<size - 1) {\n        for (j in i + 1..<size) {\n            if (nums[i] + nums[j] == target) return intArrayOf(i, j)\n        }\n    }\n    return IntArray(0)\n}\n
        two_sum.rb
        ### \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e ###\ndef two_sum_brute_force(nums, target)\n  # \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n  for i in 0...(nums.length - 1)\n    for j in (i + 1)...nums.length\n      return [i, j] if nums[i] + nums[j] == target\n    end\n  end\n\n  []\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u6b64\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff0c\u5728\u5927\u6570\u636e\u91cf\u4e0b\u975e\u5e38\u8017\u65f6\u3002

        "},{"location":"chapter_searching/replace_linear_by_hashing/#1042","title":"10.4.2 \u00a0 \u54c8\u5e0c\u67e5\u627e\uff1a\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4","text":"

        \u8003\u8651\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868\uff0c\u952e\u503c\u5bf9\u5206\u522b\u4e3a\u6570\u7ec4\u5143\u7d20\u548c\u5143\u7d20\u7d22\u5f15\u3002\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u6bcf\u8f6e\u6267\u884c\u56fe 10-10 \u6240\u793a\u7684\u6b65\u9aa4\u3002

        1. \u5224\u65ad\u6570\u5b57 target - nums[i] \u662f\u5426\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u82e5\u662f\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u8fd9\u4e24\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u3002
        2. \u5c06\u952e\u503c\u5bf9 nums[i] \u548c\u7d22\u5f15 i \u6dfb\u52a0\u8fdb\u54c8\u5e0c\u8868\u3002
        <1><2><3>

        \u56fe 10-10 \u00a0 \u8f85\u52a9\u54c8\u5e0c\u8868\u6c42\u89e3\u4e24\u6570\u4e4b\u548c

        \u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff0c\u4ec5\u9700\u5355\u5c42\u5faa\u73af\u5373\u53ef\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby two_sum.py
        def two_sum_hash_table(nums: list[int], target: int) -> list[int]:\n    \"\"\"\u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868\"\"\"\n    # \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    dic = {}\n    # \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    for i in range(len(nums)):\n        if target - nums[i] in dic:\n            return [dic[target - nums[i]], i]\n        dic[nums[i]] = i\n    return []\n
        two_sum.cpp
        /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nvector<int> twoSumHashTable(vector<int> &nums, int target) {\n    int size = nums.size();\n    // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    unordered_map<int, int> dic;\n    // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    for (int i = 0; i < size; i++) {\n        if (dic.find(target - nums[i]) != dic.end()) {\n            return {dic[target - nums[i]], i};\n        }\n        dic.emplace(nums[i], i);\n    }\n    return {};\n}\n
        two_sum.java
        /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint[] twoSumHashTable(int[] nums, int target) {\n    int size = nums.length;\n    // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    Map<Integer, Integer> dic = new HashMap<>();\n    // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    for (int i = 0; i < size; i++) {\n        if (dic.containsKey(target - nums[i])) {\n            return new int[] { dic.get(target - nums[i]), i };\n        }\n        dic.put(nums[i], i);\n    }\n    return new int[0];\n}\n
        two_sum.cs
        /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint[] TwoSumHashTable(int[] nums, int target) {\n    int size = nums.Length;\n    // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    Dictionary<int, int> dic = [];\n    // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    for (int i = 0; i < size; i++) {\n        if (dic.ContainsKey(target - nums[i])) {\n            return [dic[target - nums[i]], i];\n        }\n        dic.Add(nums[i], i);\n    }\n    return [];\n}\n
        two_sum.go
        /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunc twoSumHashTable(nums []int, target int) []int {\n    // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    hashTable := map[int]int{}\n    // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    for idx, val := range nums {\n        if preIdx, ok := hashTable[target-val]; ok {\n            return []int{preIdx, idx}\n        }\n        hashTable[val] = idx\n    }\n    return nil\n}\n
        two_sum.swift
        /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunc twoSumHashTable(nums: [Int], target: Int) -> [Int] {\n    // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    var dic: [Int: Int] = [:]\n    // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    for i in nums.indices {\n        if let j = dic[target - nums[i]] {\n            return [j, i]\n        }\n        dic[nums[i]] = i\n    }\n    return [0]\n}\n
        two_sum.js
        /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunction twoSumHashTable(nums, target) {\n    // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    let m = {};\n    // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    for (let i = 0; i < nums.length; i++) {\n        if (m[target - nums[i]] !== undefined) {\n            return [m[target - nums[i]], i];\n        } else {\n            m[nums[i]] = i;\n        }\n    }\n    return [];\n}\n
        two_sum.ts
        /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunction twoSumHashTable(nums: number[], target: number): number[] {\n    // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    let m: Map<number, number> = new Map();\n    // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    for (let i = 0; i < nums.length; i++) {\n        let index = m.get(target - nums[i]);\n        if (index !== undefined) {\n            return [index, i];\n        } else {\n            m.set(nums[i], i);\n        }\n    }\n    return [];\n}\n
        two_sum.dart
        /* \u65b9\u6cd5\u4e8c\uff1a \u8f85\u52a9\u54c8\u5e0c\u8868 */\nList<int> twoSumHashTable(List<int> nums, int target) {\n  int size = nums.length;\n  // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n  Map<int, int> dic = HashMap();\n  // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n  for (var i = 0; i < size; i++) {\n    if (dic.containsKey(target - nums[i])) {\n      return [dic[target - nums[i]]!, i];\n    }\n    dic.putIfAbsent(nums[i], () => i);\n  }\n  return [0];\n}\n
        two_sum.rs
        /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\npub fn two_sum_hash_table(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {\n    // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    let mut dic = HashMap::new();\n    // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    for (i, num) in nums.iter().enumerate() {\n        match dic.get(&(target - num)) {\n            Some(v) => return Some(vec![*v as i32, i as i32]),\n            None => dic.insert(num, i as i32),\n        };\n    }\n    None\n}\n
        two_sum.c
        /* \u54c8\u5e0c\u8868 */\ntypedef struct {\n    int key;\n    int val;\n    UT_hash_handle hh; // \u57fa\u4e8e uthash.h \u5b9e\u73b0\n} HashTable;\n\n/* \u54c8\u5e0c\u8868\u67e5\u8be2 */\nHashTable *find(HashTable *h, int key) {\n    HashTable *tmp;\n    HASH_FIND_INT(h, &key, tmp);\n    return tmp;\n}\n\n/* \u54c8\u5e0c\u8868\u5143\u7d20\u63d2\u5165 */\nvoid insert(HashTable **h, int key, int val) {\n    HashTable *t = find(*h, key);\n    if (t == NULL) {\n        HashTable *tmp = malloc(sizeof(HashTable));\n        tmp->key = key, tmp->val = val;\n        HASH_ADD_INT(*h, key, tmp);\n    } else {\n        t->val = val;\n    }\n}\n\n/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {\n    HashTable *hashtable = NULL;\n    for (int i = 0; i < numsSize; i++) {\n        HashTable *t = find(hashtable, target - nums[i]);\n        if (t != NULL) {\n            int *res = malloc(sizeof(int) * 2);\n            res[0] = t->val, res[1] = i;\n            *returnSize = 2;\n            return res;\n        }\n        insert(&hashtable, nums[i], i);\n    }\n    *returnSize = 0;\n    return NULL;\n}\n
        two_sum.kt
        /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfun twoSumHashTable(nums: IntArray, target: Int): IntArray {\n    val size = nums.size\n    // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    val dic = HashMap<Int, Int>()\n    // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n    for (i in 0..<size) {\n        if (dic.containsKey(target - nums[i])) {\n            return intArrayOf(dic[target - nums[i]]!!, i)\n        }\n        dic[nums[i]] = i\n    }\n    return IntArray(0)\n}\n
        two_sum.rb
        ### \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 ###\ndef two_sum_hash_table(nums, target)\n  # \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n  dic = {}\n  # \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n  for i in 0...nums.length\n    return [dic[target - nums[i]], i] if dic.has_key?(target - nums[i])\n\n    dic[nums[i]] = i\n  end\n\n  []\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u6b64\u65b9\u6cd5\u901a\u8fc7\u54c8\u5e0c\u67e5\u627e\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n^2)\\) \u964d\u81f3 \\(O(n)\\) \uff0c\u5927\u5e45\u63d0\u5347\u8fd0\u884c\u6548\u7387\u3002

        \u7531\u4e8e\u9700\u8981\u7ef4\u62a4\u4e00\u4e2a\u989d\u5916\u7684\u54c8\u5e0c\u8868\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002\u5c3d\u7ba1\u5982\u6b64\uff0c\u8be5\u65b9\u6cd5\u7684\u6574\u4f53\u65f6\u7a7a\u6548\u7387\u66f4\u4e3a\u5747\u8861\uff0c\u56e0\u6b64\u5b83\u662f\u672c\u9898\u7684\u6700\u4f18\u89e3\u6cd5\u3002

        "},{"location":"chapter_searching/searching_algorithm_revisited/","title":"10.5 \u00a0 \u91cd\u8bc6\u641c\u7d22\u7b97\u6cd5","text":"

        \u641c\u7d22\u7b97\u6cd5\uff08searching algorithm\uff09\u7528\u4e8e\u5728\u6570\u636e\u7ed3\u6784\uff08\u4f8b\u5982\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6811\u6216\u56fe\uff09\u4e2d\u641c\u7d22\u4e00\u4e2a\u6216\u4e00\u7ec4\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002

        \u641c\u7d22\u7b97\u6cd5\u53ef\u6839\u636e\u5b9e\u73b0\u601d\u8def\u5206\u4e3a\u4ee5\u4e0b\u4e24\u7c7b\u3002

        • \u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784\u6765\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\uff0c\u4f8b\u5982\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6811\u548c\u56fe\u7684\u904d\u5386\u7b49\u3002
        • \u5229\u7528\u6570\u636e\u7ec4\u7ec7\u7ed3\u6784\u6216\u6570\u636e\u5305\u542b\u7684\u5148\u9a8c\u4fe1\u606f\uff0c\u5b9e\u73b0\u9ad8\u6548\u5143\u7d20\u67e5\u627e\uff0c\u4f8b\u5982\u4e8c\u5206\u67e5\u627e\u3001\u54c8\u5e0c\u67e5\u627e\u548c\u4e8c\u53c9\u641c\u7d22\u6811\u67e5\u627e\u7b49\u3002

        \u4e0d\u96be\u53d1\u73b0\uff0c\u8fd9\u4e9b\u77e5\u8bc6\u70b9\u90fd\u5df2\u5728\u524d\u9762\u7684\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u56e0\u6b64\u641c\u7d22\u7b97\u6cd5\u5bf9\u4e8e\u6211\u4eec\u6765\u8bf4\u5e76\u4e0d\u964c\u751f\u3002\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4ece\u66f4\u52a0\u7cfb\u7edf\u7684\u89c6\u89d2\u5207\u5165\uff0c\u91cd\u65b0\u5ba1\u89c6\u641c\u7d22\u7b97\u6cd5\u3002

        "},{"location":"chapter_searching/searching_algorithm_revisited/#1051","title":"10.5.1 \u00a0 \u66b4\u529b\u641c\u7d22","text":"

        \u66b4\u529b\u641c\u7d22\u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784\u7684\u6bcf\u4e2a\u5143\u7d20\u6765\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002

        • \u201c\u7ebf\u6027\u641c\u7d22\u201d\u9002\u7528\u4e8e\u6570\u7ec4\u548c\u94fe\u8868\u7b49\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002\u5b83\u4ece\u6570\u636e\u7ed3\u6784\u7684\u4e00\u7aef\u5f00\u59cb\uff0c\u9010\u4e2a\u8bbf\u95ee\u5143\u7d20\uff0c\u76f4\u5230\u627e\u5230\u76ee\u6807\u5143\u7d20\u6216\u5230\u8fbe\u53e6\u4e00\u7aef\u4ecd\u6ca1\u6709\u627e\u5230\u76ee\u6807\u5143\u7d20\u4e3a\u6b62\u3002
        • \u201c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u201d\u548c\u201c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u201d\u662f\u56fe\u548c\u6811\u7684\u4e24\u79cd\u904d\u5386\u7b56\u7565\u3002\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u4ece\u521d\u59cb\u8282\u70b9\u5f00\u59cb\u9010\u5c42\u641c\u7d22\uff0c\u7531\u8fd1\u53ca\u8fdc\u5730\u8bbf\u95ee\u5404\u4e2a\u8282\u70b9\u3002\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u4ece\u521d\u59cb\u8282\u70b9\u5f00\u59cb\uff0c\u6cbf\u7740\u4e00\u6761\u8def\u5f84\u8d70\u5230\u5934\uff0c\u518d\u56de\u6eaf\u5e76\u5c1d\u8bd5\u5176\u4ed6\u8def\u5f84\uff0c\u76f4\u5230\u904d\u5386\u5b8c\u6574\u4e2a\u6570\u636e\u7ed3\u6784\u3002

        \u66b4\u529b\u641c\u7d22\u7684\u4f18\u70b9\u662f\u7b80\u5355\u4e14\u901a\u7528\u6027\u597d\uff0c\u65e0\u987b\u5bf9\u6570\u636e\u505a\u9884\u5904\u7406\u548c\u501f\u52a9\u989d\u5916\u7684\u6570\u636e\u7ed3\u6784\u3002

        \u7136\u800c\uff0c\u6b64\u7c7b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u5143\u7d20\u6570\u91cf\uff0c\u56e0\u6b64\u5728\u6570\u636e\u91cf\u8f83\u5927\u7684\u60c5\u51b5\u4e0b\u6027\u80fd\u8f83\u5dee\u3002

        "},{"location":"chapter_searching/searching_algorithm_revisited/#1052","title":"10.5.2 \u00a0 \u81ea\u9002\u5e94\u641c\u7d22","text":"

        \u81ea\u9002\u5e94\u641c\u7d22\u5229\u7528\u6570\u636e\u7684\u7279\u6709\u5c5e\u6027\uff08\u4f8b\u5982\u6709\u5e8f\u6027\uff09\u6765\u4f18\u5316\u641c\u7d22\u8fc7\u7a0b\uff0c\u4ece\u800c\u66f4\u9ad8\u6548\u5730\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002

        • \u201c\u4e8c\u5206\u67e5\u627e\u201d\u5229\u7528\u6570\u636e\u7684\u6709\u5e8f\u6027\u5b9e\u73b0\u9ad8\u6548\u67e5\u627e\uff0c\u4ec5\u9002\u7528\u4e8e\u6570\u7ec4\u3002
        • \u201c\u54c8\u5e0c\u67e5\u627e\u201d\u5229\u7528\u54c8\u5e0c\u8868\u5c06\u641c\u7d22\u6570\u636e\u548c\u76ee\u6807\u6570\u636e\u5efa\u7acb\u4e3a\u952e\u503c\u5bf9\u6620\u5c04\uff0c\u4ece\u800c\u5b9e\u73b0\u67e5\u8be2\u64cd\u4f5c\u3002
        • \u201c\u6811\u67e5\u627e\u201d\u5728\u7279\u5b9a\u7684\u6811\u7ed3\u6784\uff08\u4f8b\u5982\u4e8c\u53c9\u641c\u7d22\u6811\uff09\u4e2d\uff0c\u57fa\u4e8e\u6bd4\u8f83\u8282\u70b9\u503c\u6765\u5feb\u901f\u6392\u9664\u8282\u70b9\uff0c\u4ece\u800c\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002

        \u6b64\u7c7b\u7b97\u6cd5\u7684\u4f18\u70b9\u662f\u6548\u7387\u9ad8\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe\u5230 \\(O(\\log n)\\) \u751a\u81f3 \\(O(1)\\) \u3002

        \u7136\u800c\uff0c\u4f7f\u7528\u8fd9\u4e9b\u7b97\u6cd5\u5f80\u5f80\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u3002\u4f8b\u5982\uff0c\u4e8c\u5206\u67e5\u627e\u9700\u8981\u9884\u5148\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u54c8\u5e0c\u67e5\u627e\u548c\u6811\u67e5\u627e\u90fd\u9700\u8981\u501f\u52a9\u989d\u5916\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7ef4\u62a4\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u4e5f\u9700\u8981\u989d\u5916\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u5f00\u9500\u3002

        Tip

        \u81ea\u9002\u5e94\u641c\u7d22\u7b97\u6cd5\u5e38\u88ab\u79f0\u4e3a\u67e5\u627e\u7b97\u6cd5\uff0c\u4e3b\u8981\u7528\u4e8e\u5728\u7279\u5b9a\u6570\u636e\u7ed3\u6784\u4e2d\u5feb\u901f\u68c0\u7d22\u76ee\u6807\u5143\u7d20\u3002

        "},{"location":"chapter_searching/searching_algorithm_revisited/#1053","title":"10.5.3 \u00a0 \u641c\u7d22\u65b9\u6cd5\u9009\u53d6","text":"

        \u7ed9\u5b9a\u5927\u5c0f\u4e3a \\(n\\) \u7684\u4e00\u7ec4\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7ebf\u6027\u641c\u7d22\u3001\u4e8c\u5206\u67e5\u627e\u3001\u6811\u67e5\u627e\u3001\u54c8\u5e0c\u67e5\u627e\u7b49\u591a\u79cd\u65b9\u6cd5\u4ece\u4e2d\u641c\u7d22\u76ee\u6807\u5143\u7d20\u3002\u5404\u4e2a\u65b9\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u5982\u56fe 10-11 \u6240\u793a\u3002

        \u56fe 10-11 \u00a0 \u591a\u79cd\u641c\u7d22\u7b56\u7565

        \u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\u7684\u64cd\u4f5c\u6548\u7387\u4e0e\u7279\u6027\u5982\u8868 10-1 \u6240\u793a\u3002

        \u8868 10-1 \u00a0 \u67e5\u627e\u7b97\u6cd5\u6548\u7387\u5bf9\u6bd4

        \u7ebf\u6027\u641c\u7d22 \u4e8c\u5206\u67e5\u627e \u6811\u67e5\u627e \u54c8\u5e0c\u67e5\u627e \u67e5\u627e\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u63d2\u5165\u5143\u7d20 \\(O(1)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u989d\u5916\u7a7a\u95f4 \\(O(1)\\) \\(O(1)\\) \\(O(n)\\) \\(O(n)\\) \u6570\u636e\u9884\u5904\u7406 / \u6392\u5e8f \\(O(n \\log n)\\) \u5efa\u6811 \\(O(n \\log n)\\) \u5efa\u54c8\u5e0c\u8868 \\(O(n)\\) \u6570\u636e\u662f\u5426\u6709\u5e8f \u65e0\u5e8f \u6709\u5e8f \u6709\u5e8f \u65e0\u5e8f

        \u641c\u7d22\u7b97\u6cd5\u7684\u9009\u62e9\u8fd8\u53d6\u51b3\u89c4\u6a21\u3001\u641c\u7d22\u6027\u80fd\u8981\u6c42\u3001\u6570\u636e\u67e5\u8be2\u4e0e\u66f4\u65b0\u9891\u7387\u7b49\u3002

        \u7ebf\u6027\u641c\u7d22

        • \u901a\u7528\u6027\u8f83\u597d\uff0c\u65e0\u987b\u4efb\u4f55\u6570\u636e\u9884\u5904\u7406\u64cd\u4f5c\u3002\u5047\u5982\u6211\u4eec\u4ec5\u9700\u67e5\u8be2\u4e00\u6b21\u6570\u636e\uff0c\u90a3\u4e48\u5176\u4ed6\u4e09\u79cd\u65b9\u6cd5\u7684\u6570\u636e\u9884\u5904\u7406\u7684\u65f6\u95f4\u6bd4\u7ebf\u6027\u641c\u7d22\u7684\u65f6\u95f4\u8fd8\u8981\u66f4\u957f\u3002
        • \u9002\u7528\u4e8e\u4f53\u91cf\u8f83\u5c0f\u7684\u6570\u636e\uff0c\u6b64\u60c5\u51b5\u4e0b\u65f6\u95f4\u590d\u6742\u5ea6\u5bf9\u6548\u7387\u5f71\u54cd\u8f83\u5c0f\u3002
        • \u9002\u7528\u4e8e\u6570\u636e\u66f4\u65b0\u9891\u7387\u8f83\u9ad8\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u8be5\u65b9\u6cd5\u4e0d\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u4efb\u4f55\u989d\u5916\u7ef4\u62a4\u3002

        \u4e8c\u5206\u67e5\u627e

        • \u9002\u7528\u4e8e\u5927\u6570\u636e\u91cf\u7684\u60c5\u51b5\uff0c\u6548\u7387\u8868\u73b0\u7a33\u5b9a\uff0c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u3002
        • \u6570\u636e\u91cf\u4e0d\u80fd\u8fc7\u5927\uff0c\u56e0\u4e3a\u5b58\u50a8\u6570\u7ec4\u9700\u8981\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u3002
        • \u4e0d\u9002\u7528\u4e8e\u9ad8\u9891\u589e\u5220\u6570\u636e\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u7ef4\u62a4\u6709\u5e8f\u6570\u7ec4\u7684\u5f00\u9500\u8f83\u5927\u3002

        \u54c8\u5e0c\u67e5\u627e

        • \u9002\u5408\u5bf9\u67e5\u8be2\u6027\u80fd\u8981\u6c42\u5f88\u9ad8\u7684\u573a\u666f\uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002
        • \u4e0d\u9002\u5408\u9700\u8981\u6709\u5e8f\u6570\u636e\u6216\u8303\u56f4\u67e5\u627e\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u54c8\u5e0c\u8868\u65e0\u6cd5\u7ef4\u62a4\u6570\u636e\u7684\u6709\u5e8f\u6027\u3002
        • \u5bf9\u54c8\u5e0c\u51fd\u6570\u548c\u54c8\u5e0c\u51b2\u7a81\u5904\u7406\u7b56\u7565\u7684\u4f9d\u8d56\u6027\u8f83\u9ad8\uff0c\u5177\u6709\u8f83\u5927\u7684\u6027\u80fd\u52a3\u5316\u98ce\u9669\u3002
        • \u4e0d\u9002\u5408\u6570\u636e\u91cf\u8fc7\u5927\u7684\u60c5\u51b5\uff0c\u56e0\u4e3a\u54c8\u5e0c\u8868\u9700\u8981\u989d\u5916\u7a7a\u95f4\u6765\u6700\u5927\u7a0b\u5ea6\u5730\u51cf\u5c11\u51b2\u7a81\uff0c\u4ece\u800c\u63d0\u4f9b\u826f\u597d\u7684\u67e5\u8be2\u6027\u80fd\u3002

        \u6811\u67e5\u627e

        • \u9002\u7528\u4e8e\u6d77\u91cf\u6570\u636e\uff0c\u56e0\u4e3a\u6811\u8282\u70b9\u5728\u5185\u5b58\u4e2d\u662f\u5206\u6563\u5b58\u50a8\u7684\u3002
        • \u9002\u5408\u9700\u8981\u7ef4\u62a4\u6709\u5e8f\u6570\u636e\u6216\u8303\u56f4\u67e5\u627e\u7684\u573a\u666f\u3002
        • \u5728\u6301\u7eed\u589e\u5220\u8282\u70b9\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u53ef\u80fd\u4ea7\u751f\u503e\u659c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(n)\\) \u3002
        • \u82e5\u4f7f\u7528 AVL \u6811\u6216\u7ea2\u9ed1\u6811\uff0c\u5219\u5404\u9879\u64cd\u4f5c\u53ef\u5728 \\(O(\\log n)\\) \u6548\u7387\u4e0b\u7a33\u5b9a\u8fd0\u884c\uff0c\u4f46\u7ef4\u62a4\u6811\u5e73\u8861\u7684\u64cd\u4f5c\u4f1a\u589e\u52a0\u989d\u5916\u7684\u5f00\u9500\u3002
        "},{"location":"chapter_searching/summary/","title":"10.6 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_searching/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u4e8c\u5206\u67e5\u627e\u4f9d\u8d56\u6570\u636e\u7684\u6709\u5e8f\u6027\uff0c\u901a\u8fc7\u5faa\u73af\u9010\u6b65\u7f29\u51cf\u4e00\u534a\u641c\u7d22\u533a\u95f4\u6765\u8fdb\u884c\u67e5\u627e\u3002\u5b83\u8981\u6c42\u8f93\u5165\u6570\u636e\u6709\u5e8f\uff0c\u4e14\u4ec5\u9002\u7528\u4e8e\u6570\u7ec4\u6216\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u3002
        • \u66b4\u529b\u641c\u7d22\u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784\u6765\u5b9a\u4f4d\u6570\u636e\u3002\u7ebf\u6027\u641c\u7d22\u9002\u7528\u4e8e\u6570\u7ec4\u548c\u94fe\u8868\uff0c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u548c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u9002\u7528\u4e8e\u56fe\u548c\u6811\u3002\u6b64\u7c7b\u7b97\u6cd5\u901a\u7528\u6027\u597d\uff0c\u65e0\u987b\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\uff0c\u4f46\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u8f83\u9ad8\u3002
        • \u54c8\u5e0c\u67e5\u627e\u3001\u6811\u67e5\u627e\u548c\u4e8c\u5206\u67e5\u627e\u5c5e\u4e8e\u9ad8\u6548\u641c\u7d22\u65b9\u6cd5\uff0c\u53ef\u5728\u7279\u5b9a\u6570\u636e\u7ed3\u6784\u4e2d\u5feb\u901f\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002\u6b64\u7c7b\u7b97\u6cd5\u6548\u7387\u9ad8\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe \\(O(\\log n)\\) \u751a\u81f3 \\(O(1)\\) \uff0c\u4f46\u901a\u5e38\u9700\u8981\u501f\u52a9\u989d\u5916\u6570\u636e\u7ed3\u6784\u3002
        • \u5b9e\u9645\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u6570\u636e\u89c4\u6a21\u3001\u641c\u7d22\u6027\u80fd\u8981\u6c42\u3001\u6570\u636e\u67e5\u8be2\u548c\u66f4\u65b0\u9891\u7387\u7b49\u56e0\u7d20\u8fdb\u884c\u5177\u4f53\u5206\u6790\uff0c\u4ece\u800c\u9009\u62e9\u5408\u9002\u7684\u641c\u7d22\u65b9\u6cd5\u3002
        • \u7ebf\u6027\u641c\u7d22\u9002\u7528\u4e8e\u5c0f\u578b\u6216\u9891\u7e41\u66f4\u65b0\u7684\u6570\u636e\uff1b\u4e8c\u5206\u67e5\u627e\u9002\u7528\u4e8e\u5927\u578b\u3001\u6392\u5e8f\u7684\u6570\u636e\uff1b\u54c8\u5e0c\u67e5\u627e\u9002\u7528\u4e8e\u5bf9\u67e5\u8be2\u6548\u7387\u8981\u6c42\u8f83\u9ad8\u4e14\u65e0\u987b\u8303\u56f4\u67e5\u8be2\u7684\u6570\u636e\uff1b\u6811\u67e5\u627e\u9002\u7528\u4e8e\u9700\u8981\u7ef4\u62a4\u987a\u5e8f\u548c\u652f\u6301\u8303\u56f4\u67e5\u8be2\u7684\u5927\u578b\u52a8\u6001\u6570\u636e\u3002
        • \u7528\u54c8\u5e0c\u67e5\u627e\u66ff\u6362\u7ebf\u6027\u67e5\u627e\u662f\u4e00\u79cd\u5e38\u7528\u7684\u4f18\u5316\u8fd0\u884c\u65f6\u95f4\u7684\u7b56\u7565\uff0c\u53ef\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n)\\) \u964d\u81f3 \\(O(1)\\) \u3002
        "},{"location":"chapter_sorting/","title":"\u7b2c 11 \u7ae0 \u00a0 \u6392\u5e8f","text":"

        Abstract

        \u6392\u5e8f\u72b9\u5982\u4e00\u628a\u5c06\u6df7\u4e71\u53d8\u4e3a\u79e9\u5e8f\u7684\u9b54\u6cd5\u94a5\u5319\uff0c\u4f7f\u6211\u4eec\u80fd\u4ee5\u66f4\u9ad8\u6548\u7684\u65b9\u5f0f\u7406\u89e3\u4e0e\u5904\u7406\u6570\u636e\u3002

        \u65e0\u8bba\u662f\u7b80\u5355\u7684\u5347\u5e8f\uff0c\u8fd8\u662f\u590d\u6742\u7684\u5206\u7c7b\u6392\u5217\uff0c\u6392\u5e8f\u90fd\u5411\u6211\u4eec\u5c55\u793a\u4e86\u6570\u636e\u7684\u548c\u8c10\u7f8e\u611f\u3002

        "},{"location":"chapter_sorting/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 11.1 \u00a0 \u6392\u5e8f\u7b97\u6cd5
        • 11.2 \u00a0 \u9009\u62e9\u6392\u5e8f
        • 11.3 \u00a0 \u5192\u6ce1\u6392\u5e8f
        • 11.4 \u00a0 \u63d2\u5165\u6392\u5e8f
        • 11.5 \u00a0 \u5feb\u901f\u6392\u5e8f
        • 11.6 \u00a0 \u5f52\u5e76\u6392\u5e8f
        • 11.7 \u00a0 \u5806\u6392\u5e8f
        • 11.8 \u00a0 \u6876\u6392\u5e8f
        • 11.9 \u00a0 \u8ba1\u6570\u6392\u5e8f
        • 11.10 \u00a0 \u57fa\u6570\u6392\u5e8f
        • 11.11 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_sorting/bubble_sort/","title":"11.3 \u00a0 \u5192\u6ce1\u6392\u5e8f","text":"

        \u5192\u6ce1\u6392\u5e8f\uff08bubble sort\uff09\u901a\u8fc7\u8fde\u7eed\u5730\u6bd4\u8f83\u4e0e\u4ea4\u6362\u76f8\u90bb\u5143\u7d20\u5b9e\u73b0\u6392\u5e8f\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5c31\u50cf\u6c14\u6ce1\u4ece\u5e95\u90e8\u5347\u5230\u9876\u90e8\u4e00\u6837\uff0c\u56e0\u6b64\u5f97\u540d\u5192\u6ce1\u6392\u5e8f\u3002

        \u5982\u56fe 11-4 \u6240\u793a\uff0c\u5192\u6ce1\u8fc7\u7a0b\u53ef\u4ee5\u5229\u7528\u5143\u7d20\u4ea4\u6362\u64cd\u4f5c\u6765\u6a21\u62df\uff1a\u4ece\u6570\u7ec4\u6700\u5de6\u7aef\u5f00\u59cb\u5411\u53f3\u904d\u5386\uff0c\u4f9d\u6b21\u6bd4\u8f83\u76f8\u90bb\u5143\u7d20\u5927\u5c0f\uff0c\u5982\u679c\u201c\u5de6\u5143\u7d20 > \u53f3\u5143\u7d20\u201d\u5c31\u4ea4\u6362\u4e8c\u8005\u3002\u904d\u5386\u5b8c\u6210\u540e\uff0c\u6700\u5927\u7684\u5143\u7d20\u4f1a\u88ab\u79fb\u52a8\u5230\u6570\u7ec4\u7684\u6700\u53f3\u7aef\u3002

        <1><2><3><4><5><6><7>

        \u56fe 11-4 \u00a0 \u5229\u7528\u5143\u7d20\u4ea4\u6362\u64cd\u4f5c\u6a21\u62df\u5192\u6ce1

        "},{"location":"chapter_sorting/bubble_sort/#1131","title":"11.3.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

        \u8bbe\u6570\u7ec4\u7684\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u5192\u6ce1\u6392\u5e8f\u7684\u6b65\u9aa4\u5982\u56fe 11-5 \u6240\u793a\u3002

        1. \u9996\u5148\uff0c\u5bf9 \\(n\\) \u4e2a\u5143\u7d20\u6267\u884c\u201c\u5192\u6ce1\u201d\uff0c\u5c06\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u6b63\u786e\u4f4d\u7f6e\u3002
        2. \u63a5\u4e0b\u6765\uff0c\u5bf9\u5269\u4f59 \\(n - 1\\) \u4e2a\u5143\u7d20\u6267\u884c\u201c\u5192\u6ce1\u201d\uff0c\u5c06\u7b2c\u4e8c\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u6b63\u786e\u4f4d\u7f6e\u3002
        3. \u4ee5\u6b64\u7c7b\u63a8\uff0c\u7ecf\u8fc7 \\(n - 1\\) \u8f6e\u201c\u5192\u6ce1\u201d\u540e\uff0c\u524d \\(n - 1\\) \u5927\u7684\u5143\u7d20\u90fd\u88ab\u4ea4\u6362\u81f3\u6b63\u786e\u4f4d\u7f6e\u3002
        4. \u4ec5\u5269\u7684\u4e00\u4e2a\u5143\u7d20\u5fc5\u5b9a\u662f\u6700\u5c0f\u5143\u7d20\uff0c\u65e0\u987b\u6392\u5e8f\uff0c\u56e0\u6b64\u6570\u7ec4\u6392\u5e8f\u5b8c\u6210\u3002

        \u56fe 11-5 \u00a0 \u5192\u6ce1\u6392\u5e8f\u6d41\u7a0b

        \u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby bubble_sort.py
        def bubble_sort(nums: list[int]):\n    \"\"\"\u5192\u6ce1\u6392\u5e8f\"\"\"\n    n = len(nums)\n    # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i in range(n - 1, 0, -1):\n        # \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for j in range(i):\n            if nums[j] > nums[j + 1]:\n                # \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                nums[j], nums[j + 1] = nums[j + 1], nums[j]\n
        bubble_sort.cpp
        /* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(vector<int> &nums) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = nums.size() - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                // \u8fd9\u91cc\u4f7f\u7528\u4e86 std::swap() \u51fd\u6570\n                swap(nums[j], nums[j + 1]);\n            }\n        }\n    }\n}\n
        bubble_sort.java
        /* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(int[] nums) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = nums.length - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n            }\n        }\n    }\n}\n
        bubble_sort.cs
        /* \u5192\u6ce1\u6392\u5e8f */\nvoid BubbleSort(int[] nums) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = nums.Length - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n            }\n        }\n    }\n}\n
        bubble_sort.go
        /* \u5192\u6ce1\u6392\u5e8f */\nfunc bubbleSort(nums []int) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i := len(nums) - 1; i > 0; i-- {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for j := 0; j < i; j++ {\n            if nums[j] > nums[j+1] {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                nums[j], nums[j+1] = nums[j+1], nums[j]\n            }\n        }\n    }\n}\n
        bubble_sort.swift
        /* \u5192\u6ce1\u6392\u5e8f */\nfunc bubbleSort(nums: inout [Int]) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i in nums.indices.dropFirst().reversed() {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for j in 0 ..< i {\n            if nums[j] > nums[j + 1] {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                nums.swapAt(j, j + 1)\n            }\n        }\n    }\n}\n
        bubble_sort.js
        /* \u5192\u6ce1\u6392\u5e8f */\nfunction bubbleSort(nums) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n            }\n        }\n    }\n}\n
        bubble_sort.ts
        /* \u5192\u6ce1\u6392\u5e8f */\nfunction bubbleSort(nums: number[]): void {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n            }\n        }\n    }\n}\n
        bubble_sort.dart
        /* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(List<int> nums) {\n  // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n  for (int i = nums.length - 1; i > 0; i--) {\n    // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n    for (int j = 0; j < i; j++) {\n      if (nums[j] > nums[j + 1]) {\n        // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n        int tmp = nums[j];\n        nums[j] = nums[j + 1];\n        nums[j + 1] = tmp;\n      }\n    }\n  }\n}\n
        bubble_sort.rs
        /* \u5192\u6ce1\u6392\u5e8f */\nfn bubble_sort(nums: &mut [i32]) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i in (1..nums.len()).rev() {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for j in 0..i {\n            if nums[j] > nums[j + 1] {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                nums.swap(j, j + 1);\n            }\n        }\n    }\n}\n
        bubble_sort.c
        /* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(int nums[], int size) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = size - 1; i > 0; i--) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                int temp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = temp;\n            }\n        }\n    }\n}\n
        bubble_sort.kt
        /* \u5192\u6ce1\u6392\u5e8f */\nfun bubbleSort(nums: IntArray) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (i in nums.size - 1 downTo 1) {\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (j in 0..<i) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                val temp = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = temp\n            }\n        }\n    }\n}\n
        bubble_sort.rb
        ### \u5192\u6ce1\u6392\u5e8f ###\ndef bubble_sort(nums)\n  n = nums.length\n  # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n  for i in (n - 1).downto(1)\n    # \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n    for j in 0...i\n      if nums[j] > nums[j + 1]\n        # \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n        nums[j], nums[j + 1] = nums[j + 1], nums[j]\n      end\n    end\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/bubble_sort/#1132","title":"11.3.2 \u00a0 \u6548\u7387\u4f18\u5316","text":"

        \u6211\u4eec\u53d1\u73b0\uff0c\u5982\u679c\u67d0\u8f6e\u201c\u5192\u6ce1\u201d\u4e2d\u6ca1\u6709\u6267\u884c\u4efb\u4f55\u4ea4\u6362\u64cd\u4f5c\uff0c\u8bf4\u660e\u6570\u7ec4\u5df2\u7ecf\u5b8c\u6210\u6392\u5e8f\uff0c\u53ef\u76f4\u63a5\u8fd4\u56de\u7ed3\u679c\u3002\u56e0\u6b64\uff0c\u53ef\u4ee5\u589e\u52a0\u4e00\u4e2a\u6807\u5fd7\u4f4d flag \u6765\u76d1\u6d4b\u8fd9\u79cd\u60c5\u51b5\uff0c\u4e00\u65e6\u51fa\u73b0\u5c31\u7acb\u5373\u8fd4\u56de\u3002

        \u7ecf\u8fc7\u4f18\u5316\uff0c\u5192\u6ce1\u6392\u5e8f\u7684\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(n^2)\\) \uff1b\u4f46\u5f53\u8f93\u5165\u6570\u7ec4\u5b8c\u5168\u6709\u5e8f\u65f6\uff0c\u53ef\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby bubble_sort.py
        def bubble_sort_with_flag(nums: list[int]):\n    \"\"\"\u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09\"\"\"\n    n = len(nums)\n    # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i in range(n - 1, 0, -1):\n        flag = False  # \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n        # \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for j in range(i):\n            if nums[j] > nums[j + 1]:\n                # \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                nums[j], nums[j + 1] = nums[j + 1], nums[j]\n                flag = True  # \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n        if not flag:\n            break  # \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n
        bubble_sort.cpp
        /* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid bubbleSortWithFlag(vector<int> &nums) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = nums.size() - 1; i > 0; i--) {\n        bool flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                // \u8fd9\u91cc\u4f7f\u7528\u4e86 std::swap() \u51fd\u6570\n                swap(nums[j], nums[j + 1]);\n                flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n            }\n        }\n        if (!flag)\n            break; // \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n    }\n}\n
        bubble_sort.java
        /* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09 */\nvoid bubbleSortWithFlag(int[] nums) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = nums.length - 1; i > 0; i--) {\n        boolean flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n            }\n        }\n        if (!flag)\n            break; // \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n    }\n}\n
        bubble_sort.cs
        /* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid BubbleSortWithFlag(int[] nums) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = nums.Length - 1; i > 0; i--) {\n        bool flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n                flag = true;  // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n            }\n        }\n        if (!flag) break;     // \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n    }\n}\n
        bubble_sort.go
        /* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunc bubbleSortWithFlag(nums []int) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i := len(nums) - 1; i > 0; i-- {\n        flag := false // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for j := 0; j < i; j++ {\n            if nums[j] > nums[j+1] {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                nums[j], nums[j+1] = nums[j+1], nums[j]\n                flag = true // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n            }\n        }\n        if flag == false { // \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n            break\n        }\n    }\n}\n
        bubble_sort.swift
        /* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunc bubbleSortWithFlag(nums: inout [Int]) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i in nums.indices.dropFirst().reversed() {\n        var flag = false // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n        for j in 0 ..< i {\n            if nums[j] > nums[j + 1] {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                nums.swapAt(j, j + 1)\n                flag = true // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n            }\n        }\n        if !flag { // \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n            break\n        }\n    }\n}\n
        bubble_sort.js
        /* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunction bubbleSortWithFlag(nums) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        let flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n            }\n        }\n        if (!flag) break; // \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n    }\n}\n
        bubble_sort.ts
        /* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunction bubbleSortWithFlag(nums: number[]): void {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        let flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n            }\n        }\n        if (!flag) break; // \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n    }\n}\n
        bubble_sort.dart
        /* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid bubbleSortWithFlag(List<int> nums) {\n  // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n  for (int i = nums.length - 1; i > 0; i--) {\n    bool flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n    // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n    for (int j = 0; j < i; j++) {\n      if (nums[j] > nums[j + 1]) {\n        // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n        int tmp = nums[j];\n        nums[j] = nums[j + 1];\n        nums[j + 1] = tmp;\n        flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n      }\n    }\n    if (!flag) break; // \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n  }\n}\n
        bubble_sort.rs
        /* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09 */\nfn bubble_sort_with_flag(nums: &mut [i32]) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for i in (1..nums.len()).rev() {\n        let mut flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for j in 0..i {\n            if nums[j] > nums[j + 1] {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                nums.swap(j, j + 1);\n                flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n            }\n        }\n        if !flag {\n            break; // \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n        };\n    }\n}\n
        bubble_sort.c
        /* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid bubbleSortWithFlag(int nums[], int size) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (int i = size - 1; i > 0; i--) {\n        bool flag = false;\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                int temp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = temp;\n                flag = true;\n            }\n        }\n        if (!flag)\n            break;\n    }\n}\n
        bubble_sort.kt
        /* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09 */\nfun bubbleSortWithFlag(nums: IntArray) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n    for (i in nums.size - 1 downTo 1) {\n        var flag = false // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n        // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n        for (j in 0..<i) {\n            if (nums[j] > nums[j + 1]) {\n                // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n                val temp = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = temp\n                flag = true // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n            }\n        }\n        if (!flag) break // \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n    }\n}\n
        bubble_sort.rb
        ### \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09###\ndef bubble_sort_with_flag(nums)\n  n = nums.length\n  # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n  for i in (n - 1).downto(1)\n    flag = false # \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n\n    # \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n    for j in 0...i\n      if nums[j] > nums[j + 1]\n        # \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n        nums[j], nums[j + 1] = nums[j + 1], nums[j]\n        flag = true # \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n      end\n    end\n\n    break unless flag # \u6b64\u8f6e\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/bubble_sort/#1133","title":"11.3.3 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\)\u3001\u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u5404\u8f6e\u201c\u5192\u6ce1\u201d\u904d\u5386\u7684\u6570\u7ec4\u957f\u5ea6\u4f9d\u6b21\u4e3a \\(n - 1\\)\u3001\\(n - 2\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \uff0c\u603b\u548c\u4e3a \\((n - 1) n / 2\\) \u3002\u5728\u5f15\u5165 flag \u4f18\u5316\u540e\uff0c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe\u5230 \\(O(n)\\) \u3002
        • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\)\u3001\u539f\u5730\u6392\u5e8f\uff1a\u6307\u9488 \\(i\\) \u548c \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002
        • \u7a33\u5b9a\u6392\u5e8f\uff1a\u7531\u4e8e\u5728\u201c\u5192\u6ce1\u201d\u4e2d\u9047\u5230\u76f8\u7b49\u5143\u7d20\u4e0d\u4ea4\u6362\u3002
        "},{"location":"chapter_sorting/bucket_sort/","title":"11.8 \u00a0 \u6876\u6392\u5e8f","text":"

        \u524d\u8ff0\u51e0\u79cd\u6392\u5e8f\u7b97\u6cd5\u90fd\u5c5e\u4e8e\u201c\u57fa\u4e8e\u6bd4\u8f83\u7684\u6392\u5e8f\u7b97\u6cd5\u201d\uff0c\u5b83\u4eec\u901a\u8fc7\u6bd4\u8f83\u5143\u7d20\u95f4\u7684\u5927\u5c0f\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u6b64\u7c7b\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u65e0\u6cd5\u8d85\u8d8a \\(O(n \\log n)\\) \u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u51e0\u79cd\u201c\u975e\u6bd4\u8f83\u6392\u5e8f\u7b97\u6cd5\u201d\uff0c\u5b83\u4eec\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u8fbe\u5230\u7ebf\u6027\u9636\u3002

        \u6876\u6392\u5e8f\uff08bucket sort\uff09\u662f\u5206\u6cbb\u7b56\u7565\u7684\u4e00\u4e2a\u5178\u578b\u5e94\u7528\u3002\u5b83\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e9b\u5177\u6709\u5927\u5c0f\u987a\u5e8f\u7684\u6876\uff0c\u6bcf\u4e2a\u6876\u5bf9\u5e94\u4e00\u4e2a\u6570\u636e\u8303\u56f4\uff0c\u5c06\u6570\u636e\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\uff1b\u7136\u540e\uff0c\u5728\u6bcf\u4e2a\u6876\u5185\u90e8\u5206\u522b\u6267\u884c\u6392\u5e8f\uff1b\u6700\u7ec8\u6309\u7167\u6876\u7684\u987a\u5e8f\u5c06\u6240\u6709\u6570\u636e\u5408\u5e76\u3002

        "},{"location":"chapter_sorting/bucket_sort/#1181","title":"11.8.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

        \u8003\u8651\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\uff0c\u5176\u5143\u7d20\u662f\u8303\u56f4 \\([0, 1)\\) \u5185\u7684\u6d6e\u70b9\u6570\u3002\u6876\u6392\u5e8f\u7684\u6d41\u7a0b\u5982\u56fe 11-13 \u6240\u793a\u3002

        1. \u521d\u59cb\u5316 \\(k\\) \u4e2a\u6876\uff0c\u5c06 \\(n\\) \u4e2a\u5143\u7d20\u5206\u914d\u5230 \\(k\\) \u4e2a\u6876\u4e2d\u3002
        2. \u5bf9\u6bcf\u4e2a\u6876\u5206\u522b\u6267\u884c\u6392\u5e8f\uff08\u8fd9\u91cc\u91c7\u7528\u7f16\u7a0b\u8bed\u8a00\u7684\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff09\u3002
        3. \u6309\u7167\u6876\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u5408\u5e76\u7ed3\u679c\u3002

        \u56fe 11-13 \u00a0 \u6876\u6392\u5e8f\u7b97\u6cd5\u6d41\u7a0b

        \u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby bucket_sort.py
        def bucket_sort(nums: list[float]):\n    \"\"\"\u6876\u6392\u5e8f\"\"\"\n    # \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n    k = len(nums) // 2\n    buckets = [[] for _ in range(k)]\n    # 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n    for num in nums:\n        # \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n        i = int(num * k)\n        # \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n        buckets[i].append(num)\n    # 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n    for bucket in buckets:\n        # \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n        bucket.sort()\n    # 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n    i = 0\n    for bucket in buckets:\n        for num in bucket:\n            nums[i] = num\n            i += 1\n
        bucket_sort.cpp
        /* \u6876\u6392\u5e8f */\nvoid bucketSort(vector<float> &nums) {\n    // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n    int k = nums.size() / 2;\n    vector<vector<float>> buckets(k);\n    // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n    for (float num : nums) {\n        // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n        int i = num * k;\n        // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 bucket_idx\n        buckets[i].push_back(num);\n    }\n    // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n    for (vector<float> &bucket : buckets) {\n        // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n        sort(bucket.begin(), bucket.end());\n    }\n    // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n    int i = 0;\n    for (vector<float> &bucket : buckets) {\n        for (float num : bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
        bucket_sort.java
        /* \u6876\u6392\u5e8f */\nvoid bucketSort(float[] nums) {\n    // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n    int k = nums.length / 2;\n    List<List<Float>> buckets = new ArrayList<>();\n    for (int i = 0; i < k; i++) {\n        buckets.add(new ArrayList<>());\n    }\n    // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n    for (float num : nums) {\n        // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n        int i = (int) (num * k);\n        // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n        buckets.get(i).add(num);\n    }\n    // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n    for (List<Float> bucket : buckets) {\n        // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n        Collections.sort(bucket);\n    }\n    // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n    int i = 0;\n    for (List<Float> bucket : buckets) {\n        for (float num : bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
        bucket_sort.cs
        /* \u6876\u6392\u5e8f */\nvoid BucketSort(float[] nums) {\n    // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n    int k = nums.Length / 2;\n    List<List<float>> buckets = [];\n    for (int i = 0; i < k; i++) {\n        buckets.Add([]);\n    }\n    // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n    foreach (float num in nums) {\n        // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n        int i = (int)(num * k);\n        // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n        buckets[i].Add(num);\n    }\n    // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n    foreach (List<float> bucket in buckets) {\n        // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n        bucket.Sort();\n    }\n    // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n    int j = 0;\n    foreach (List<float> bucket in buckets) {\n        foreach (float num in bucket) {\n            nums[j++] = num;\n        }\n    }\n}\n
        bucket_sort.go
        /* \u6876\u6392\u5e8f */\nfunc bucketSort(nums []float64) {\n    // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n    k := len(nums) / 2\n    buckets := make([][]float64, k)\n    for i := 0; i < k; i++ {\n        buckets[i] = make([]float64, 0)\n    }\n    // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n    for _, num := range nums {\n        // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n        i := int(num * float64(k))\n        // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n        buckets[i] = append(buckets[i], num)\n    }\n    // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n    for i := 0; i < k; i++ {\n        // \u4f7f\u7528\u5185\u7f6e\u5207\u7247\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n        sort.Float64s(buckets[i])\n    }\n    // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n    i := 0\n    for _, bucket := range buckets {\n        for _, num := range bucket {\n            nums[i] = num\n            i++\n        }\n    }\n}\n
        bucket_sort.swift
        /* \u6876\u6392\u5e8f */\nfunc bucketSort(nums: inout [Double]) {\n    // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n    let k = nums.count / 2\n    var buckets = (0 ..< k).map { _ in [Double]() }\n    // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n    for num in nums {\n        // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n        let i = Int(num * Double(k))\n        // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n        buckets[i].append(num)\n    }\n    // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n    for i in buckets.indices {\n        // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n        buckets[i].sort()\n    }\n    // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n    var i = nums.startIndex\n    for bucket in buckets {\n        for num in bucket {\n            nums[i] = num\n            i += 1\n        }\n    }\n}\n
        bucket_sort.js
        /* \u6876\u6392\u5e8f */\nfunction bucketSort(nums) {\n    // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n    const k = nums.length / 2;\n    const buckets = [];\n    for (let i = 0; i < k; i++) {\n        buckets.push([]);\n    }\n    // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n    for (const num of nums) {\n        // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n        const i = Math.floor(num * k);\n        // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n        buckets[i].push(num);\n    }\n    // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n    for (const bucket of buckets) {\n        // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n        bucket.sort((a, b) => a - b);\n    }\n    // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n    let i = 0;\n    for (const bucket of buckets) {\n        for (const num of bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
        bucket_sort.ts
        /* \u6876\u6392\u5e8f */\nfunction bucketSort(nums: number[]): void {\n    // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n    const k = nums.length / 2;\n    const buckets: number[][] = [];\n    for (let i = 0; i < k; i++) {\n        buckets.push([]);\n    }\n    // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n    for (const num of nums) {\n        // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n        const i = Math.floor(num * k);\n        // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n        buckets[i].push(num);\n    }\n    // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n    for (const bucket of buckets) {\n        // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n        bucket.sort((a, b) => a - b);\n    }\n    // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n    let i = 0;\n    for (const bucket of buckets) {\n        for (const num of bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
        bucket_sort.dart
        /* \u6876\u6392\u5e8f */\nvoid bucketSort(List<double> nums) {\n  // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n  int k = nums.length ~/ 2;\n  List<List<double>> buckets = List.generate(k, (index) => []);\n\n  // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n  for (double _num in nums) {\n    // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 _num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n    int i = (_num * k).toInt();\n    // \u5c06 _num \u6dfb\u52a0\u8fdb\u6876 bucket_idx\n    buckets[i].add(_num);\n  }\n  // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n  for (List<double> bucket in buckets) {\n    bucket.sort();\n  }\n  // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n  int i = 0;\n  for (List<double> bucket in buckets) {\n    for (double _num in bucket) {\n      nums[i++] = _num;\n    }\n  }\n}\n
        bucket_sort.rs
        /* \u6876\u6392\u5e8f */\nfn bucket_sort(nums: &mut [f64]) {\n    // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n    let k = nums.len() / 2;\n    let mut buckets = vec![vec![]; k];\n    // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n    for &num in nums.iter() {\n        // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n        let i = (num * k as f64) as usize;\n        // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n        buckets[i].push(num);\n    }\n    // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n    for bucket in &mut buckets {\n        // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n        bucket.sort_by(|a, b| a.partial_cmp(b).unwrap());\n    }\n    // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n    let mut i = 0;\n    for bucket in buckets.iter() {\n        for &num in bucket.iter() {\n            nums[i] = num;\n            i += 1;\n        }\n    }\n}\n
        bucket_sort.c
        /* \u6876\u6392\u5e8f */\nvoid bucketSort(float nums[], int n) {\n    int k = n / 2;                                 // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\n    int *sizes = malloc(k * sizeof(int));          // \u8bb0\u5f55\u6bcf\u4e2a\u6876\u7684\u5927\u5c0f\n    float **buckets = malloc(k * sizeof(float *)); // \u52a8\u6001\u6570\u7ec4\u7684\u6570\u7ec4\uff08\u6876\uff09\n    // \u4e3a\u6bcf\u4e2a\u6876\u9884\u5206\u914d\u8db3\u591f\u7684\u7a7a\u95f4\n    for (int i = 0; i < k; ++i) {\n        buckets[i] = (float *)malloc(n * sizeof(float));\n        sizes[i] = 0;\n    }\n    // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n    for (int i = 0; i < n; ++i) {\n        int idx = (int)(nums[i] * k);\n        buckets[idx][sizes[idx]++] = nums[i];\n    }\n    // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n    for (int i = 0; i < k; ++i) {\n        qsort(buckets[i], sizes[i], sizeof(float), compare);\n    }\n    // 3. \u5408\u5e76\u6392\u5e8f\u540e\u7684\u6876\n    int idx = 0;\n    for (int i = 0; i < k; ++i) {\n        for (int j = 0; j < sizes[i]; ++j) {\n            nums[idx++] = buckets[i][j];\n        }\n        // \u91ca\u653e\u5185\u5b58\n        free(buckets[i]);\n    }\n}\n
        bucket_sort.kt
        /* \u6876\u6392\u5e8f */\nfun bucketSort(nums: FloatArray) {\n    // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n    val k = nums.size / 2\n    val buckets = mutableListOf<MutableList<Float>>()\n    for (i in 0..<k) {\n        buckets.add(mutableListOf())\n    }\n    // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n    for (num in nums) {\n        // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n        val i = (num * k).toInt()\n        // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n        buckets[i].add(num)\n    }\n    // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n    for (bucket in buckets) {\n        // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n        bucket.sort()\n    }\n    // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n    var i = 0\n    for (bucket in buckets) {\n        for (num in bucket) {\n            nums[i++] = num\n        }\n    }\n}\n
        bucket_sort.rb
        ### \u6876\u6392\u5e8f ###\ndef bucket_sort(nums)\n  # \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n  k = nums.length / 2\n  buckets = Array.new(k) { [] }\n\n  # 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n  nums.each do |num|\n    # \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n    i = (num * k).to_i\n    # \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n    buckets[i] << num\n  end\n\n  # 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n  buckets.each do |bucket|\n    # \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n    bucket.sort!\n  end\n\n  # 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n  i = 0\n  buckets.each do |bucket|\n    bucket.each do |num|\n      nums[i] = num\n      i += 1\n    end\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/bucket_sort/#1182","title":"11.8.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"

        \u6876\u6392\u5e8f\u9002\u7528\u4e8e\u5904\u7406\u4f53\u91cf\u5f88\u5927\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u8f93\u5165\u6570\u636e\u5305\u542b 100 \u4e07\u4e2a\u5143\u7d20\uff0c\u7531\u4e8e\u7a7a\u95f4\u9650\u5236\uff0c\u7cfb\u7edf\u5185\u5b58\u65e0\u6cd5\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5206\u6210 1000 \u4e2a\u6876\uff0c\u7136\u540e\u5206\u522b\u5bf9\u6bcf\u4e2a\u6876\u8fdb\u884c\u6392\u5e8f\uff0c\u6700\u540e\u5c06\u7ed3\u679c\u5408\u5e76\u3002

        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n + k)\\) \uff1a\u5047\u8bbe\u5143\u7d20\u5728\u5404\u4e2a\u6876\u5185\u5e73\u5747\u5206\u5e03\uff0c\u90a3\u4e48\u6bcf\u4e2a\u6876\u5185\u7684\u5143\u7d20\u6570\u91cf\u4e3a \\(\\frac{n}{k}\\) \u3002\u5047\u8bbe\u6392\u5e8f\u5355\u4e2a\u6876\u4f7f\u7528 \\(O(\\frac{n}{k} \\log\\frac{n}{k})\\) \u65f6\u95f4\uff0c\u5219\u6392\u5e8f\u6240\u6709\u6876\u4f7f\u7528 \\(O(n \\log\\frac{n}{k})\\) \u65f6\u95f4\u3002\u5f53\u6876\u6570\u91cf \\(k\\) \u6bd4\u8f83\u5927\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5219\u8d8b\u5411\u4e8e \\(O(n)\\) \u3002\u5408\u5e76\u7ed3\u679c\u65f6\u9700\u8981\u904d\u5386\u6240\u6709\u6876\u548c\u5143\u7d20\uff0c\u82b1\u8d39 \\(O(n + k)\\) \u65f6\u95f4\u3002\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u6570\u636e\u88ab\u5206\u914d\u5230\u4e00\u4e2a\u6876\u4e2d\uff0c\u4e14\u6392\u5e8f\u8be5\u6876\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002
        • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n + k)\\)\u3001\u975e\u539f\u5730\u6392\u5e8f\uff1a\u9700\u8981\u501f\u52a9 \\(k\\) \u4e2a\u6876\u548c\u603b\u5171 \\(n\\) \u4e2a\u5143\u7d20\u7684\u989d\u5916\u7a7a\u95f4\u3002
        • \u6876\u6392\u5e8f\u662f\u5426\u7a33\u5b9a\u53d6\u51b3\u4e8e\u6392\u5e8f\u6876\u5185\u5143\u7d20\u7684\u7b97\u6cd5\u662f\u5426\u7a33\u5b9a\u3002
        "},{"location":"chapter_sorting/bucket_sort/#1183","title":"11.8.3 \u00a0 \u5982\u4f55\u5b9e\u73b0\u5e73\u5747\u5206\u914d","text":"

        \u6876\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7406\u8bba\u4e0a\u53ef\u4ee5\u8fbe\u5230 \\(O(n)\\) \uff0c\u5173\u952e\u5728\u4e8e\u5c06\u5143\u7d20\u5747\u5300\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\uff0c\u56e0\u4e3a\u5b9e\u9645\u6570\u636e\u5f80\u5f80\u4e0d\u662f\u5747\u5300\u5206\u5e03\u7684\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u8981\u5c06\u6dd8\u5b9d\u4e0a\u7684\u6240\u6709\u5546\u54c1\u6309\u4ef7\u683c\u8303\u56f4\u5e73\u5747\u5206\u914d\u5230 10 \u4e2a\u6876\u4e2d\uff0c\u4f46\u5546\u54c1\u4ef7\u683c\u5206\u5e03\u4e0d\u5747\uff0c\u4f4e\u4e8e 100 \u5143\u7684\u975e\u5e38\u591a\uff0c\u9ad8\u4e8e 1000 \u5143\u7684\u975e\u5e38\u5c11\u3002\u82e5\u5c06\u4ef7\u683c\u533a\u95f4\u5e73\u5747\u5212\u5206\u4e3a 10 \u4e2a\uff0c\u5404\u4e2a\u6876\u4e2d\u7684\u5546\u54c1\u6570\u91cf\u5dee\u8ddd\u4f1a\u975e\u5e38\u5927\u3002

        \u4e3a\u5b9e\u73b0\u5e73\u5747\u5206\u914d\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u8bbe\u5b9a\u4e00\u6761\u5927\u81f4\u7684\u5206\u754c\u7ebf\uff0c\u5c06\u6570\u636e\u7c97\u7565\u5730\u5206\u5230 3 \u4e2a\u6876\u4e2d\u3002\u5206\u914d\u5b8c\u6bd5\u540e\uff0c\u518d\u5c06\u5546\u54c1\u8f83\u591a\u7684\u6876\u7ee7\u7eed\u5212\u5206\u4e3a 3 \u4e2a\u6876\uff0c\u76f4\u81f3\u6240\u6709\u6876\u4e2d\u7684\u5143\u7d20\u6570\u91cf\u5927\u81f4\u76f8\u7b49\u3002

        \u5982\u56fe 11-14 \u6240\u793a\uff0c\u8fd9\u79cd\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u521b\u5efa\u4e00\u68f5\u9012\u5f52\u6811\uff0c\u76ee\u6807\u662f\u8ba9\u53f6\u8282\u70b9\u7684\u503c\u5c3d\u53ef\u80fd\u5e73\u5747\u3002\u5f53\u7136\uff0c\u4e0d\u4e00\u5b9a\u8981\u6bcf\u8f6e\u5c06\u6570\u636e\u5212\u5206\u4e3a 3 \u4e2a\u6876\uff0c\u5177\u4f53\u5212\u5206\u65b9\u5f0f\u53ef\u6839\u636e\u6570\u636e\u7279\u70b9\u7075\u6d3b\u9009\u62e9\u3002

        \u56fe 11-14 \u00a0 \u9012\u5f52\u5212\u5206\u6876

        \u5982\u679c\u6211\u4eec\u63d0\u524d\u77e5\u9053\u5546\u54c1\u4ef7\u683c\u7684\u6982\u7387\u5206\u5e03\uff0c\u5219\u53ef\u4ee5\u6839\u636e\u6570\u636e\u6982\u7387\u5206\u5e03\u8bbe\u7f6e\u6bcf\u4e2a\u6876\u7684\u4ef7\u683c\u5206\u754c\u7ebf\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6570\u636e\u5206\u5e03\u5e76\u4e0d\u4e00\u5b9a\u9700\u8981\u7279\u610f\u7edf\u8ba1\uff0c\u4e5f\u53ef\u4ee5\u6839\u636e\u6570\u636e\u7279\u70b9\u91c7\u7528\u67d0\u79cd\u6982\u7387\u6a21\u578b\u8fdb\u884c\u8fd1\u4f3c\u3002

        \u5982\u56fe 11-15 \u6240\u793a\uff0c\u6211\u4eec\u5047\u8bbe\u5546\u54c1\u4ef7\u683c\u670d\u4ece\u6b63\u6001\u5206\u5e03\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5408\u7406\u5730\u8bbe\u5b9a\u4ef7\u683c\u533a\u95f4\uff0c\u4ece\u800c\u5c06\u5546\u54c1\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\u3002

        \u56fe 11-15 \u00a0 \u6839\u636e\u6982\u7387\u5206\u5e03\u5212\u5206\u6876

        "},{"location":"chapter_sorting/counting_sort/","title":"11.9 \u00a0 \u8ba1\u6570\u6392\u5e8f","text":"

        \u8ba1\u6570\u6392\u5e8f\uff08counting sort\uff09\u901a\u8fc7\u7edf\u8ba1\u5143\u7d20\u6570\u91cf\u6765\u5b9e\u73b0\u6392\u5e8f\uff0c\u901a\u5e38\u5e94\u7528\u4e8e\u6574\u6570\u6570\u7ec4\u3002

        "},{"location":"chapter_sorting/counting_sort/#1191","title":"11.9.1 \u00a0 \u7b80\u5355\u5b9e\u73b0","text":"

        \u5148\u6765\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u3002\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4 nums \uff0c\u5176\u4e2d\u7684\u5143\u7d20\u90fd\u662f\u201c\u975e\u8d1f\u6574\u6570\u201d\uff0c\u8ba1\u6570\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u56fe 11-16 \u6240\u793a\u3002

        1. \u904d\u5386\u6570\u7ec4\uff0c\u627e\u51fa\u5176\u4e2d\u7684\u6700\u5927\u6570\u5b57\uff0c\u8bb0\u4e3a \\(m\\) \uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(m + 1\\) \u7684\u8f85\u52a9\u6570\u7ec4 counter \u3002
        2. \u501f\u52a9 counter \u7edf\u8ba1 nums \u4e2d\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u5176\u4e2d counter[num] \u5bf9\u5e94\u6570\u5b57 num \u7684\u51fa\u73b0\u6b21\u6570\u3002\u7edf\u8ba1\u65b9\u6cd5\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u904d\u5386 nums\uff08\u8bbe\u5f53\u524d\u6570\u5b57\u4e3a num\uff09\uff0c\u6bcf\u8f6e\u5c06 counter[num] \u589e\u52a0 \\(1\\) \u5373\u53ef\u3002
        3. \u7531\u4e8e counter \u7684\u5404\u4e2a\u7d22\u5f15\u5929\u7136\u6709\u5e8f\uff0c\u56e0\u6b64\u76f8\u5f53\u4e8e\u6240\u6709\u6570\u5b57\u5df2\u7ecf\u6392\u5e8f\u597d\u4e86\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u904d\u5386 counter \uff0c\u6839\u636e\u5404\u6570\u5b57\u51fa\u73b0\u6b21\u6570\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u586b\u5165 nums \u5373\u53ef\u3002

        \u56fe 11-16 \u00a0 \u8ba1\u6570\u6392\u5e8f\u6d41\u7a0b

        \u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby counting_sort.py
        def counting_sort_naive(nums: list[int]):\n    \"\"\"\u8ba1\u6570\u6392\u5e8f\"\"\"\n    # \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\n    # 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    m = max(nums)\n    # 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    # counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    counter = [0] * (m + 1)\n    for num in nums:\n        counter[num] += 1\n    # 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n    i = 0\n    for num in range(m + 1):\n        for _ in range(counter[num]):\n            nums[i] = num\n            i += 1\n
        counting_sort.cpp
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(vector<int> &nums) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    int m = 0;\n    for (int num : nums) {\n        m = max(m, num);\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    vector<int> counter(m + 1, 0);\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
        counting_sort.java
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(int[] nums) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    int m = 0;\n    for (int num : nums) {\n        m = Math.max(m, num);\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    int[] counter = new int[m + 1];\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
        counting_sort.cs
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid CountingSortNaive(int[] nums) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    int m = 0;\n    foreach (int num in nums) {\n        m = Math.Max(m, num);\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    int[] counter = new int[m + 1];\n    foreach (int num in nums) {\n        counter[num]++;\n    }\n    // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
        counting_sort.go
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfunc countingSortNaive(nums []int) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    m := 0\n    for _, num := range nums {\n        if num > m {\n            m = num\n        }\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    counter := make([]int, m+1)\n    for _, num := range nums {\n        counter[num]++\n    }\n    // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n    for i, num := 0, 0; num < m+1; num++ {\n        for j := 0; j < counter[num]; j++ {\n            nums[i] = num\n            i++\n        }\n    }\n}\n
        counting_sort.swift
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfunc countingSortNaive(nums: inout [Int]) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    let m = nums.max()!\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    var counter = Array(repeating: 0, count: m + 1)\n    for num in nums {\n        counter[num] += 1\n    }\n    // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n    var i = 0\n    for num in 0 ..< m + 1 {\n        for _ in 0 ..< counter[num] {\n            nums[i] = num\n            i += 1\n        }\n    }\n}\n
        counting_sort.js
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfunction countingSortNaive(nums) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    let m = Math.max(...nums);\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    const counter = new Array(m + 1).fill(0);\n    for (const num of nums) {\n        counter[num]++;\n    }\n    // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n    let i = 0;\n    for (let num = 0; num < m + 1; num++) {\n        for (let j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
        counting_sort.ts
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfunction countingSortNaive(nums: number[]): void {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    let m: number = Math.max(...nums);\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    const counter: number[] = new Array<number>(m + 1).fill(0);\n    for (const num of nums) {\n        counter[num]++;\n    }\n    // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n    let i = 0;\n    for (let num = 0; num < m + 1; num++) {\n        for (let j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
        counting_sort.dart
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(List<int> nums) {\n  // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n  int m = 0;\n  for (int _num in nums) {\n    m = max(m, _num);\n  }\n  // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n  // counter[_num] \u4ee3\u8868 _num \u7684\u51fa\u73b0\u6b21\u6570\n  List<int> counter = List.filled(m + 1, 0);\n  for (int _num in nums) {\n    counter[_num]++;\n  }\n  // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n  int i = 0;\n  for (int _num = 0; _num < m + 1; _num++) {\n    for (int j = 0; j < counter[_num]; j++, i++) {\n      nums[i] = _num;\n    }\n  }\n}\n
        counting_sort.rs
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfn counting_sort_naive(nums: &mut [i32]) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    let m = *nums.iter().max().unwrap();\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    let mut counter = vec![0; m as usize + 1];\n    for &num in nums.iter() {\n        counter[num as usize] += 1;\n    }\n    // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n    let mut i = 0;\n    for num in 0..m + 1 {\n        for _ in 0..counter[num as usize] {\n            nums[i] = num;\n            i += 1;\n        }\n    }\n}\n
        counting_sort.c
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(int nums[], int size) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    int m = 0;\n    for (int i = 0; i < size; i++) {\n        if (nums[i] > m) {\n            m = nums[i];\n        }\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    int *counter = calloc(m + 1, sizeof(int));\n    for (int i = 0; i < size; i++) {\n        counter[nums[i]]++;\n    }\n    // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n    // 4. \u91ca\u653e\u5185\u5b58\n    free(counter);\n}\n
        counting_sort.kt
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfun countingSortNaive(nums: IntArray) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    var m = 0\n    for (num in nums) {\n        m = max(m, num)\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    val counter = IntArray(m + 1)\n    for (num in nums) {\n        counter[num]++\n    }\n    // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n    var i = 0\n    for (num in 0..<m + 1) {\n        var j = 0\n        while (j < counter[num]) {\n            nums[i] = num\n            j++\n            i++\n        }\n    }\n}\n
        counting_sort.rb
        ### \u8ba1\u6570\u6392\u5e8f ###\ndef counting_sort_naive(nums)\n  # \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\n  # 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n  m = 0\n  nums.each { |num| m = [m, num].max }\n  # 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n  # counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n  counter = Array.new(m + 1, 0)\n  nums.each { |num| counter[num] += 1 }\n  # 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n  i = 0\n  for num in 0...(m + 1)\n    (0...counter[num]).each do\n      nums[i] = num\n      i += 1\n    end\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u8ba1\u6570\u6392\u5e8f\u4e0e\u6876\u6392\u5e8f\u7684\u8054\u7cfb

        \u4ece\u6876\u6392\u5e8f\u7684\u89d2\u5ea6\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8ba1\u6570\u6392\u5e8f\u4e2d\u7684\u8ba1\u6570\u6570\u7ec4 counter \u7684\u6bcf\u4e2a\u7d22\u5f15\u89c6\u4e3a\u4e00\u4e2a\u6876\uff0c\u5c06\u7edf\u8ba1\u6570\u91cf\u7684\u8fc7\u7a0b\u770b\u4f5c\u5c06\u5404\u4e2a\u5143\u7d20\u5206\u914d\u5230\u5bf9\u5e94\u7684\u6876\u4e2d\u3002\u672c\u8d28\u4e0a\uff0c\u8ba1\u6570\u6392\u5e8f\u662f\u6876\u6392\u5e8f\u5728\u6574\u578b\u6570\u636e\u4e0b\u7684\u4e00\u4e2a\u7279\u4f8b\u3002

        "},{"location":"chapter_sorting/counting_sort/#1192","title":"11.9.2 \u00a0 \u5b8c\u6574\u5b9e\u73b0","text":"

        \u7ec6\u5fc3\u7684\u8bfb\u8005\u53ef\u80fd\u53d1\u73b0\u4e86\uff0c\u5982\u679c\u8f93\u5165\u6570\u636e\u662f\u5bf9\u8c61\uff0c\u4e0a\u8ff0\u6b65\u9aa4 3. \u5c31\u5931\u6548\u4e86\u3002\u5047\u8bbe\u8f93\u5165\u6570\u636e\u662f\u5546\u54c1\u5bf9\u8c61\uff0c\u6211\u4eec\u60f3\u6309\u7167\u5546\u54c1\u4ef7\u683c\uff08\u7c7b\u7684\u6210\u5458\u53d8\u91cf\uff09\u5bf9\u5546\u54c1\u8fdb\u884c\u6392\u5e8f\uff0c\u800c\u4e0a\u8ff0\u7b97\u6cd5\u53ea\u80fd\u7ed9\u51fa\u4ef7\u683c\u7684\u6392\u5e8f\u7ed3\u679c\u3002

        \u90a3\u4e48\u5982\u4f55\u624d\u80fd\u5f97\u5230\u539f\u6570\u636e\u7684\u6392\u5e8f\u7ed3\u679c\u5462\uff1f\u6211\u4eec\u9996\u5148\u8ba1\u7b97 counter \u7684\u201c\u524d\u7f00\u548c\u201d\u3002\u987e\u540d\u601d\u4e49\uff0c\u7d22\u5f15 i \u5904\u7684\u524d\u7f00\u548c prefix[i] \u7b49\u4e8e\u6570\u7ec4\u524d i \u4e2a\u5143\u7d20\u4e4b\u548c\uff1a

        \\[ \\text{prefix}[i] = \\sum_{j=0}^i \\text{counter[j]} \\]

        \u524d\u7f00\u548c\u5177\u6709\u660e\u786e\u7684\u610f\u4e49\uff0cprefix[num] - 1 \u4ee3\u8868\u5143\u7d20 num \u5728\u7ed3\u679c\u6570\u7ec4 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u3002\u8fd9\u4e2a\u4fe1\u606f\u975e\u5e38\u5173\u952e\uff0c\u56e0\u4e3a\u5b83\u544a\u8bc9\u6211\u4eec\u5404\u4e2a\u5143\u7d20\u5e94\u8be5\u51fa\u73b0\u5728\u7ed3\u679c\u6570\u7ec4\u7684\u54ea\u4e2a\u4f4d\u7f6e\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5012\u5e8f\u904d\u5386\u539f\u6570\u7ec4 nums \u7684\u6bcf\u4e2a\u5143\u7d20 num \uff0c\u5728\u6bcf\u8f6e\u8fed\u4ee3\u4e2d\u6267\u884c\u4ee5\u4e0b\u4e24\u6b65\u3002

        1. \u5c06 num \u586b\u5165\u6570\u7ec4 res \u7684\u7d22\u5f15 prefix[num] - 1 \u5904\u3002
        2. \u4ee4\u524d\u7f00\u548c prefix[num] \u51cf\u5c0f \\(1\\) \uff0c\u4ece\u800c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\u3002

        \u904d\u5386\u5b8c\u6210\u540e\uff0c\u6570\u7ec4 res \u4e2d\u5c31\u662f\u6392\u5e8f\u597d\u7684\u7ed3\u679c\uff0c\u6700\u540e\u4f7f\u7528 res \u8986\u76d6\u539f\u6570\u7ec4 nums \u5373\u53ef\u3002\u56fe 11-17 \u5c55\u793a\u4e86\u5b8c\u6574\u7684\u8ba1\u6570\u6392\u5e8f\u6d41\u7a0b\u3002

        <1><2><3><4><5><6><7><8>

        \u56fe 11-17 \u00a0 \u8ba1\u6570\u6392\u5e8f\u6b65\u9aa4

        \u8ba1\u6570\u6392\u5e8f\u7684\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby counting_sort.py
        def counting_sort(nums: list[int]):\n    \"\"\"\u8ba1\u6570\u6392\u5e8f\"\"\"\n    # \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\n    # 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    m = max(nums)\n    # 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    # counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    counter = [0] * (m + 1)\n    for num in nums:\n        counter[num] += 1\n    # 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n    # \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n    for i in range(m):\n        counter[i + 1] += counter[i]\n    # 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n    # \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n    n = len(nums)\n    res = [0] * n\n    for i in range(n - 1, -1, -1):\n        num = nums[i]\n        res[counter[num] - 1] = num  # \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n        counter[num] -= 1  # \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n    # \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n    for i in range(n):\n        nums[i] = res[i]\n
        counting_sort.cpp
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid countingSort(vector<int> &nums) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    int m = 0;\n    for (int num : nums) {\n        m = max(m, num);\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    vector<int> counter(m + 1, 0);\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n    // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n    // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n    int n = nums.size();\n    vector<int> res(n);\n    for (int i = n - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n        counter[num]--;              // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n    nums = res;\n}\n
        counting_sort.java
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid countingSort(int[] nums) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    int m = 0;\n    for (int num : nums) {\n        m = Math.max(m, num);\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    int[] counter = new int[m + 1];\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n    // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n    // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n    int n = nums.length;\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n        counter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (int i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n
        counting_sort.cs
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid CountingSort(int[] nums) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    int m = 0;\n    foreach (int num in nums) {\n        m = Math.Max(m, num);\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    int[] counter = new int[m + 1];\n    foreach (int num in nums) {\n        counter[num]++;\n    }\n    // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n    // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n    // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n    int n = nums.Length;\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n        counter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (int i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n
        counting_sort.go
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfunc countingSort(nums []int) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    m := 0\n    for _, num := range nums {\n        if num > m {\n            m = num\n        }\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    counter := make([]int, m+1)\n    for _, num := range nums {\n        counter[num]++\n    }\n    // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n    // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n    for i := 0; i < m; i++ {\n        counter[i+1] += counter[i]\n    }\n    // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n    // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n    n := len(nums)\n    res := make([]int, n)\n    for i := n - 1; i >= 0; i-- {\n        num := nums[i]\n        // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n        res[counter[num]-1] = num\n        // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n        counter[num]--\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n    copy(nums, res)\n}\n
        counting_sort.swift
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfunc countingSort(nums: inout [Int]) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    let m = nums.max()!\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    var counter = Array(repeating: 0, count: m + 1)\n    for num in nums {\n        counter[num] += 1\n    }\n    // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n    // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n    for i in 0 ..< m {\n        counter[i + 1] += counter[i]\n    }\n    // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n    // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n    var res = Array(repeating: 0, count: nums.count)\n    for i in nums.indices.reversed() {\n        let num = nums[i]\n        res[counter[num] - 1] = num // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n        counter[num] -= 1 // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n    for i in nums.indices {\n        nums[i] = res[i]\n    }\n}\n
        counting_sort.js
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfunction countingSort(nums) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    let m = Math.max(...nums);\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    const counter = new Array(m + 1).fill(0);\n    for (const num of nums) {\n        counter[num]++;\n    }\n    // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n    // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n    for (let i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n    // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n    const n = nums.length;\n    const res = new Array(n);\n    for (let i = n - 1; i >= 0; i--) {\n        const num = nums[i];\n        res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n        counter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (let i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n
        counting_sort.ts
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfunction countingSort(nums: number[]): void {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    let m: number = Math.max(...nums);\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    const counter: number[] = new Array<number>(m + 1).fill(0);\n    for (const num of nums) {\n        counter[num]++;\n    }\n    // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n    // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n    for (let i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n    // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n    const n = nums.length;\n    const res: number[] = new Array<number>(n);\n    for (let i = n - 1; i >= 0; i--) {\n        const num = nums[i];\n        res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n        counter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (let i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n
        counting_sort.dart
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid countingSort(List<int> nums) {\n  // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n  int m = 0;\n  for (int _num in nums) {\n    m = max(m, _num);\n  }\n  // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n  // counter[_num] \u4ee3\u8868 _num \u7684\u51fa\u73b0\u6b21\u6570\n  List<int> counter = List.filled(m + 1, 0);\n  for (int _num in nums) {\n    counter[_num]++;\n  }\n  // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n  // \u5373 counter[_num]-1 \u662f _num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n  for (int i = 0; i < m; i++) {\n    counter[i + 1] += counter[i];\n  }\n  // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n  // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n  int n = nums.length;\n  List<int> res = List.filled(n, 0);\n  for (int i = n - 1; i >= 0; i--) {\n    int _num = nums[i];\n    res[counter[_num] - 1] = _num; // \u5c06 _num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n    counter[_num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e _num \u7684\u7d22\u5f15\n  }\n  // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n  nums.setAll(0, res);\n}\n
        counting_sort.rs
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfn counting_sort(nums: &mut [i32]) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    let m = *nums.iter().max().unwrap() as usize;\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    let mut counter = vec![0; m + 1];\n    for &num in nums.iter() {\n        counter[num as usize] += 1;\n    }\n    // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n    // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n    for i in 0..m {\n        counter[i + 1] += counter[i];\n    }\n    // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n    // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n    let n = nums.len();\n    let mut res = vec![0; n];\n    for i in (0..n).rev() {\n        let num = nums[i];\n        res[counter[num as usize] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n        counter[num as usize] -= 1; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n    nums.copy_from_slice(&res)\n}\n
        counting_sort.c
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid countingSort(int nums[], int size) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    int m = 0;\n    for (int i = 0; i < size; i++) {\n        if (nums[i] > m) {\n            m = nums[i];\n        }\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    int *counter = calloc(m, sizeof(int));\n    for (int i = 0; i < size; i++) {\n        counter[nums[i]]++;\n    }\n    // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n    // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n    // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n    int *res = malloc(sizeof(int) * size);\n    for (int i = size - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n        counter[num]--;              // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n    memcpy(nums, res, size * sizeof(int));\n    // 5. \u91ca\u653e\u5185\u5b58\n    free(res);\n    free(counter);\n}\n
        counting_sort.kt
        /* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfun countingSort(nums: IntArray) {\n    // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n    var m = 0\n    for (num in nums) {\n        m = max(m, num)\n    }\n    // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n    val counter = IntArray(m + 1)\n    for (num in nums) {\n        counter[num]++\n    }\n    // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n    // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n    for (i in 0..<m) {\n        counter[i + 1] += counter[i]\n    }\n    // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n    // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n    val n = nums.size\n    val res = IntArray(n)\n    for (i in n - 1 downTo 0) {\n        val num = nums[i]\n        res[counter[num] - 1] = num // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n        counter[num]-- // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (i in 0..<n) {\n        nums[i] = res[i]\n    }\n}\n
        counting_sort.rb
        ### \u8ba1\u6570\u6392\u5e8f ###\ndef counting_sort(nums)\n  # \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\n  # 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n  m = nums.max\n  # 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n  # counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n  counter = Array.new(m + 1, 0)\n  nums.each { |num| counter[num] += 1 }\n  # 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n  # \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n  (0...m).each { |i| counter[i + 1] += counter[i] }\n  # 4. \u5012\u5e8f\u904d\u5386 nums, \u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n  # \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n  n = nums.length\n  res = Array.new(n, 0)\n  (n - 1).downto(0).each do |i|\n    num = nums[i]\n    res[counter[num] - 1] = num # \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n    counter[num] -= 1 # \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n  end\n  # \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n  (0...n).each { |i| nums[i] = res[i] }\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/counting_sort/#1193","title":"11.9.3 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n + m)\\)\u3001\u975e\u81ea\u9002\u5e94\u6392\u5e8f \uff1a\u6d89\u53ca\u904d\u5386 nums \u548c\u904d\u5386 counter \uff0c\u90fd\u4f7f\u7528\u7ebf\u6027\u65f6\u95f4\u3002\u4e00\u822c\u60c5\u51b5\u4e0b \\(n \\gg m\\) \uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u4e8e \\(O(n)\\) \u3002
        • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n + m)\\)\u3001\u975e\u539f\u5730\u6392\u5e8f\uff1a\u501f\u52a9\u4e86\u957f\u5ea6\u5206\u522b\u4e3a \\(n\\) \u548c \\(m\\) \u7684\u6570\u7ec4 res \u548c counter \u3002
        • \u7a33\u5b9a\u6392\u5e8f\uff1a\u7531\u4e8e\u5411 res \u4e2d\u586b\u5145\u5143\u7d20\u7684\u987a\u5e8f\u662f\u201c\u4ece\u53f3\u5411\u5de6\u201d\u7684\uff0c\u56e0\u6b64\u5012\u5e8f\u904d\u5386 nums \u53ef\u4ee5\u907f\u514d\u6539\u53d8\u76f8\u7b49\u5143\u7d20\u4e4b\u95f4\u7684\u76f8\u5bf9\u4f4d\u7f6e\uff0c\u4ece\u800c\u5b9e\u73b0\u7a33\u5b9a\u6392\u5e8f\u3002\u5b9e\u9645\u4e0a\uff0c\u6b63\u5e8f\u904d\u5386 nums \u4e5f\u53ef\u4ee5\u5f97\u5230\u6b63\u786e\u7684\u6392\u5e8f\u7ed3\u679c\uff0c\u4f46\u7ed3\u679c\u662f\u975e\u7a33\u5b9a\u7684\u3002
        "},{"location":"chapter_sorting/counting_sort/#1194","title":"11.9.4 \u00a0 \u5c40\u9650\u6027","text":"

        \u770b\u5230\u8fd9\u91cc\uff0c\u4f60\u4e5f\u8bb8\u4f1a\u89c9\u5f97\u8ba1\u6570\u6392\u5e8f\u975e\u5e38\u5de7\u5999\uff0c\u4ec5\u901a\u8fc7\u7edf\u8ba1\u6570\u91cf\u5c31\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6548\u7684\u6392\u5e8f\u3002\u7136\u800c\uff0c\u4f7f\u7528\u8ba1\u6570\u6392\u5e8f\u7684\u524d\u7f6e\u6761\u4ef6\u76f8\u5bf9\u8f83\u4e3a\u4e25\u683c\u3002

        \u8ba1\u6570\u6392\u5e8f\u53ea\u9002\u7528\u4e8e\u975e\u8d1f\u6574\u6570\u3002\u82e5\u60f3\u5c06\u5176\u7528\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\uff0c\u9700\u8981\u786e\u4fdd\u8fd9\u4e9b\u6570\u636e\u53ef\u4ee5\u8f6c\u6362\u4e3a\u975e\u8d1f\u6574\u6570\uff0c\u5e76\u4e14\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u4e0d\u80fd\u6539\u53d8\u5404\u4e2a\u5143\u7d20\u4e4b\u95f4\u7684\u76f8\u5bf9\u5927\u5c0f\u5173\u7cfb\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5305\u542b\u8d1f\u6570\u7684\u6574\u6570\u6570\u7ec4\uff0c\u53ef\u4ee5\u5148\u7ed9\u6240\u6709\u6570\u5b57\u52a0\u4e0a\u4e00\u4e2a\u5e38\u6570\uff0c\u5c06\u5168\u90e8\u6570\u5b57\u8f6c\u5316\u4e3a\u6b63\u6570\uff0c\u6392\u5e8f\u5b8c\u6210\u540e\u518d\u8f6c\u6362\u56de\u53bb\u3002

        \u8ba1\u6570\u6392\u5e8f\u9002\u7528\u4e8e\u6570\u636e\u91cf\u5927\u4f46\u6570\u636e\u8303\u56f4\u8f83\u5c0f\u7684\u60c5\u51b5\u3002\u6bd4\u5982\uff0c\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d \\(m\\) \u4e0d\u80fd\u592a\u5927\uff0c\u5426\u5219\u4f1a\u5360\u7528\u8fc7\u591a\u7a7a\u95f4\u3002\u800c\u5f53 \\(n \\ll m\\) \u65f6\uff0c\u8ba1\u6570\u6392\u5e8f\u4f7f\u7528 \\(O(m)\\) \u65f6\u95f4\uff0c\u53ef\u80fd\u6bd4 \\(O(n \\log n)\\) \u7684\u6392\u5e8f\u7b97\u6cd5\u8fd8\u8981\u6162\u3002

        "},{"location":"chapter_sorting/heap_sort/","title":"11.7 \u00a0 \u5806\u6392\u5e8f","text":"

        Tip

        \u9605\u8bfb\u672c\u8282\u524d\uff0c\u8bf7\u786e\u4fdd\u5df2\u5b66\u5b8c\u201c\u5806\u201d\u7ae0\u8282\u3002

        \u5806\u6392\u5e8f\uff08heap sort\uff09\u662f\u4e00\u79cd\u57fa\u4e8e\u5806\u6570\u636e\u7ed3\u6784\u5b9e\u73b0\u7684\u9ad8\u6548\u6392\u5e8f\u7b97\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5df2\u7ecf\u5b66\u8fc7\u7684\u201c\u5efa\u5806\u64cd\u4f5c\u201d\u548c\u201c\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\u201d\u5b9e\u73b0\u5806\u6392\u5e8f\u3002

        1. \u8f93\u5165\u6570\u7ec4\u5e76\u5efa\u7acb\u5c0f\u9876\u5806\uff0c\u6b64\u65f6\u6700\u5c0f\u5143\u7d20\u4f4d\u4e8e\u5806\u9876\u3002
        2. \u4e0d\u65ad\u6267\u884c\u51fa\u5806\u64cd\u4f5c\uff0c\u4f9d\u6b21\u8bb0\u5f55\u51fa\u5806\u5143\u7d20\uff0c\u5373\u53ef\u5f97\u5230\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\u7684\u5e8f\u5217\u3002

        \u4ee5\u4e0a\u65b9\u6cd5\u867d\u7136\u53ef\u884c\uff0c\u4f46\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u989d\u5916\u6570\u7ec4\u6765\u4fdd\u5b58\u5f39\u51fa\u7684\u5143\u7d20\uff0c\u6bd4\u8f83\u6d6a\u8d39\u7a7a\u95f4\u3002\u5728\u5b9e\u9645\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u4e00\u79cd\u66f4\u52a0\u4f18\u96c5\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002

        "},{"location":"chapter_sorting/heap_sort/#1171","title":"11.7.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

        \u8bbe\u6570\u7ec4\u7684\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u5806\u6392\u5e8f\u7684\u6d41\u7a0b\u5982\u56fe 11-12 \u6240\u793a\u3002

        1. \u8f93\u5165\u6570\u7ec4\u5e76\u5efa\u7acb\u5927\u9876\u5806\u3002\u5b8c\u6210\u540e\uff0c\u6700\u5927\u5143\u7d20\u4f4d\u4e8e\u5806\u9876\u3002
        2. \u5c06\u5806\u9876\u5143\u7d20\uff08\u7b2c\u4e00\u4e2a\u5143\u7d20\uff09\u4e0e\u5806\u5e95\u5143\u7d20\uff08\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff09\u4ea4\u6362\u3002\u5b8c\u6210\u4ea4\u6362\u540e\uff0c\u5806\u7684\u957f\u5ea6\u51cf \\(1\\) \uff0c\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u52a0 \\(1\\) \u3002
        3. \u4ece\u5806\u9876\u5143\u7d20\u5f00\u59cb\uff0c\u4ece\u9876\u5230\u5e95\u6267\u884c\u5806\u5316\u64cd\u4f5c\uff08sift down\uff09\u3002\u5b8c\u6210\u5806\u5316\u540e\uff0c\u5806\u7684\u6027\u8d28\u5f97\u5230\u4fee\u590d\u3002
        4. \u5faa\u73af\u6267\u884c\u7b2c 2. \u6b65\u548c\u7b2c 3. \u6b65\u3002\u5faa\u73af \\(n - 1\\) \u8f6e\u540e\uff0c\u5373\u53ef\u5b8c\u6210\u6570\u7ec4\u6392\u5e8f\u3002

        Tip

        \u5b9e\u9645\u4e0a\uff0c\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\u4e2d\u4e5f\u5305\u542b\u7b2c 2. \u6b65\u548c\u7b2c 3. \u6b65\uff0c\u53ea\u662f\u591a\u4e86\u4e00\u4e2a\u5f39\u51fa\u5143\u7d20\u7684\u6b65\u9aa4\u3002

        <1><2><3><4><5><6><7><8><9><10><11><12>

        \u56fe 11-12 \u00a0 \u5806\u6392\u5e8f\u6b65\u9aa4

        \u5728\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e0e\u201c\u5806\u201d\u7ae0\u8282\u76f8\u540c\u7684\u4ece\u9876\u81f3\u5e95\u5806\u5316 sift_down() \u51fd\u6570\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e\u5806\u7684\u957f\u5ea6\u4f1a\u968f\u7740\u63d0\u53d6\u6700\u5927\u5143\u7d20\u800c\u51cf\u5c0f\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u7ed9 sift_down() \u51fd\u6570\u6dfb\u52a0\u4e00\u4e2a\u957f\u5ea6\u53c2\u6570 \\(n\\) \uff0c\u7528\u4e8e\u6307\u5b9a\u5806\u7684\u5f53\u524d\u6709\u6548\u957f\u5ea6\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby heap_sort.py
        def sift_down(nums: list[int], n: int, i: int):\n    \"\"\"\u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\"\"\"\n    while True:\n        # \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        l = 2 * i + 1\n        r = 2 * i + 2\n        ma = i\n        if l < n and nums[l] > nums[ma]:\n            ma = l\n        if r < n and nums[r] > nums[ma]:\n            ma = r\n        # \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if ma == i:\n            break\n        # \u4ea4\u6362\u4e24\u8282\u70b9\n        nums[i], nums[ma] = nums[ma], nums[i]\n        # \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma\n\ndef heap_sort(nums: list[int]):\n    \"\"\"\u5806\u6392\u5e8f\"\"\"\n    # \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for i in range(len(nums) // 2 - 1, -1, -1):\n        sift_down(nums, len(nums), i)\n    # \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n    for i in range(len(nums) - 1, 0, -1):\n        # \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        nums[0], nums[i] = nums[i], nums[0]\n        # \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n        sift_down(nums, i, 0)\n
        heap_sort.cpp
        /* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(vector<int> &nums, int n, int i) {\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma == i) {\n            break;\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        swap(nums[i], nums[ma]);\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n\n/* \u5806\u6392\u5e8f */\nvoid heapSort(vector<int> &nums) {\n    // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for (int i = nums.size() / 2 - 1; i >= 0; --i) {\n        siftDown(nums, nums.size(), i);\n    }\n    // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n    for (int i = nums.size() - 1; i > 0; --i) {\n        // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        swap(nums[0], nums[i]);\n        // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n        siftDown(nums, i, 0);\n    }\n}\n
        heap_sort.java
        /* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int[] nums, int n, int i) {\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma == i)\n            break;\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        int temp = nums[i];\n        nums[i] = nums[ma];\n        nums[ma] = temp;\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n\n/* \u5806\u6392\u5e8f */\nvoid heapSort(int[] nums) {\n    // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for (int i = nums.length / 2 - 1; i >= 0; i--) {\n        siftDown(nums, nums.length, i);\n    }\n    // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n    for (int i = nums.length - 1; i > 0; i--) {\n        // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        int tmp = nums[0];\n        nums[0] = nums[i];\n        nums[i] = tmp;\n        // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n        siftDown(nums, i, 0);\n    }\n}\n
        heap_sort.cs
        /* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid SiftDown(int[] nums, int n, int i) {\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma == i)\n            break;\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        (nums[ma], nums[i]) = (nums[i], nums[ma]);\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n\n/* \u5806\u6392\u5e8f */\nvoid HeapSort(int[] nums) {\n    // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for (int i = nums.Length / 2 - 1; i >= 0; i--) {\n        SiftDown(nums, nums.Length, i);\n    }\n    // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n    for (int i = nums.Length - 1; i > 0; i--) {\n        // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        (nums[i], nums[0]) = (nums[0], nums[i]);\n        // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n        SiftDown(nums, i, 0);\n    }\n}\n
        heap_sort.go
        /* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc siftDown(nums *[]int, n, i int) {\n    for true {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        l := 2*i + 1\n        r := 2*i + 2\n        ma := i\n        if l < n && (*nums)[l] > (*nums)[ma] {\n            ma = l\n        }\n        if r < n && (*nums)[r] > (*nums)[ma] {\n            ma = r\n        }\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if ma == i {\n            break\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        (*nums)[i], (*nums)[ma] = (*nums)[ma], (*nums)[i]\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma\n    }\n}\n\n/* \u5806\u6392\u5e8f */\nfunc heapSort(nums *[]int) {\n    // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for i := len(*nums)/2 - 1; i >= 0; i-- {\n        siftDown(nums, len(*nums), i)\n    }\n    // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n    for i := len(*nums) - 1; i > 0; i-- {\n        // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        (*nums)[0], (*nums)[i] = (*nums)[i], (*nums)[0]\n        // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n        siftDown(nums, i, 0)\n    }\n}\n
        heap_sort.swift
        /* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc siftDown(nums: inout [Int], n: Int, i: Int) {\n    var i = i\n    while true {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        let l = 2 * i + 1\n        let r = 2 * i + 2\n        var ma = i\n        if l < n, nums[l] > nums[ma] {\n            ma = l\n        }\n        if r < n, nums[r] > nums[ma] {\n            ma = r\n        }\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if ma == i {\n            break\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        nums.swapAt(i, ma)\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma\n    }\n}\n\n/* \u5806\u6392\u5e8f */\nfunc heapSort(nums: inout [Int]) {\n    // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for i in stride(from: nums.count / 2 - 1, through: 0, by: -1) {\n        siftDown(nums: &nums, n: nums.count, i: i)\n    }\n    // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n    for i in nums.indices.dropFirst().reversed() {\n        // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        nums.swapAt(0, i)\n        // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n        siftDown(nums: &nums, n: i, i: 0)\n    }\n}\n
        heap_sort.js
        /* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunction siftDown(nums, n, i) {\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        let l = 2 * i + 1;\n        let r = 2 * i + 2;\n        let ma = i;\n        if (l < n && nums[l] > nums[ma]) {\n            ma = l;\n        }\n        if (r < n && nums[r] > nums[ma]) {\n            ma = r;\n        }\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma === i) {\n            break;\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        [nums[i], nums[ma]] = [nums[ma], nums[i]];\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n\n/* \u5806\u6392\u5e8f */\nfunction heapSort(nums) {\n    // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for (let i = Math.floor(nums.length / 2) - 1; i >= 0; i--) {\n        siftDown(nums, nums.length, i);\n    }\n    // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n    for (let i = nums.length - 1; i > 0; i--) {\n        // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        [nums[0], nums[i]] = [nums[i], nums[0]];\n        // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n        siftDown(nums, i, 0);\n    }\n}\n
        heap_sort.ts
        /* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunction siftDown(nums: number[], n: number, i: number): void {\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        let l = 2 * i + 1;\n        let r = 2 * i + 2;\n        let ma = i;\n        if (l < n && nums[l] > nums[ma]) {\n            ma = l;\n        }\n        if (r < n && nums[r] > nums[ma]) {\n            ma = r;\n        }\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma === i) {\n            break;\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        [nums[i], nums[ma]] = [nums[ma], nums[i]];\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n\n/* \u5806\u6392\u5e8f */\nfunction heapSort(nums: number[]): void {\n    // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for (let i = Math.floor(nums.length / 2) - 1; i >= 0; i--) {\n        siftDown(nums, nums.length, i);\n    }\n    // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n    for (let i = nums.length - 1; i > 0; i--) {\n        // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        [nums[0], nums[i]] = [nums[i], nums[0]];\n        // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n        siftDown(nums, i, 0);\n    }\n}\n
        heap_sort.dart
        /* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(List<int> nums, int n, int i) {\n  while (true) {\n    // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n    int l = 2 * i + 1;\n    int r = 2 * i + 2;\n    int ma = i;\n    if (l < n && nums[l] > nums[ma]) ma = l;\n    if (r < n && nums[r] > nums[ma]) ma = r;\n    // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n    if (ma == i) break;\n    // \u4ea4\u6362\u4e24\u8282\u70b9\n    int temp = nums[i];\n    nums[i] = nums[ma];\n    nums[ma] = temp;\n    // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n    i = ma;\n  }\n}\n\n/* \u5806\u6392\u5e8f */\nvoid heapSort(List<int> nums) {\n  // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n  for (int i = nums.length ~/ 2 - 1; i >= 0; i--) {\n    siftDown(nums, nums.length, i);\n  }\n  // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n  for (int i = nums.length - 1; i > 0; i--) {\n    // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    int tmp = nums[0];\n    nums[0] = nums[i];\n    nums[i] = tmp;\n    // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n    siftDown(nums, i, 0);\n  }\n}\n
        heap_sort.rs
        /* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfn sift_down(nums: &mut [i32], n: usize, mut i: usize) {\n    loop {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        let l = 2 * i + 1;\n        let r = 2 * i + 2;\n        let mut ma = i;\n        if l < n && nums[l] > nums[ma] {\n            ma = l;\n        }\n        if r < n && nums[r] > nums[ma] {\n            ma = r;\n        }\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if ma == i {\n            break;\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        nums.swap(i, ma);\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n\n/* \u5806\u6392\u5e8f */\nfn heap_sort(nums: &mut [i32]) {\n    // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for i in (0..nums.len() / 2).rev() {\n        sift_down(nums, nums.len(), i);\n    }\n    // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n    for i in (1..nums.len()).rev() {\n        // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        nums.swap(0, i);\n        // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n        sift_down(nums, i, 0);\n    }\n}\n
        heap_sort.c
        /* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int nums[], int n, int i) {\n    while (1) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma == i) {\n            break;\n        }\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        int temp = nums[i];\n        nums[i] = nums[ma];\n        nums[ma] = temp;\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma;\n    }\n}\n\n/* \u5806\u6392\u5e8f */\nvoid heapSort(int nums[], int n) {\n    // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for (int i = n / 2 - 1; i >= 0; --i) {\n        siftDown(nums, n, i);\n    }\n    // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n    for (int i = n - 1; i > 0; --i) {\n        // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        int tmp = nums[0];\n        nums[0] = nums[i];\n        nums[i] = tmp;\n        // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n        siftDown(nums, i, 0);\n    }\n}\n
        heap_sort.kt
        /* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfun siftDown(nums: IntArray, n: Int, li: Int) {\n    var i = li\n    while (true) {\n        // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n        val l = 2 * i + 1\n        val r = 2 * i + 2\n        var ma = i\n        if (l < n && nums[l] > nums[ma]) \n            ma = l\n        if (r < n && nums[r] > nums[ma]) \n            ma = r\n        // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n        if (ma == i) \n            break\n        // \u4ea4\u6362\u4e24\u8282\u70b9\n        val temp = nums[i]\n        nums[i] = nums[ma]\n        nums[ma] = temp\n        // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n        i = ma\n    }\n}\n\n/* \u5806\u6392\u5e8f */\nfun heapSort(nums: IntArray) {\n    // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n    for (i in nums.size / 2 - 1 downTo 0) {\n        siftDown(nums, nums.size, i)\n    }\n    // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n    for (i in nums.size - 1 downTo 1) {\n        // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n        val temp = nums[0]\n        nums[0] = nums[i]\n        nums[i] = temp\n        // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n        siftDown(nums, i, 0)\n    }\n}\n
        heap_sort.rb
        ### \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 ###\ndef sift_down(nums, n, i)\n  while true\n    # \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n    l = 2 * i + 1\n    r = 2 * i + 2\n    ma = i\n    ma = l if l < n && nums[l] > nums[ma]\n    ma = r if r < n && nums[r] > nums[ma]\n    # \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n    break if ma == i\n    # \u4ea4\u6362\u4e24\u8282\u70b9\n    nums[i], nums[ma] = nums[ma], nums[i]\n    # \u5faa\u73af\u5411\u4e0b\u5806\u5316\n    i = ma\n  end\nend\n\n### \u5806\u6392\u5e8f ###\ndef heap_sort(nums)\n  # \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n  (nums.length / 2 - 1).downto(0) do |i|\n    sift_down(nums, nums.length, i)\n  end\n  # \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n  (nums.length - 1).downto(1) do |i|\n    # \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n    nums[0], nums[i] = nums[i], nums[0]\n    # \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n    sift_down(nums, i, 0)\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/heap_sort/#1172","title":"11.7.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\)\u3001\u975e\u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u5efa\u5806\u64cd\u4f5c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002\u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \uff0c\u5171\u5faa\u73af \\(n - 1\\) \u8f6e\u3002
        • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\)\u3001\u539f\u5730\u6392\u5e8f\uff1a\u51e0\u4e2a\u6307\u9488\u53d8\u91cf\u4f7f\u7528 \\(O(1)\\) \u7a7a\u95f4\u3002\u5143\u7d20\u4ea4\u6362\u548c\u5806\u5316\u64cd\u4f5c\u90fd\u662f\u5728\u539f\u6570\u7ec4\u4e0a\u8fdb\u884c\u7684\u3002
        • \u975e\u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u4ea4\u6362\u5806\u9876\u5143\u7d20\u548c\u5806\u5e95\u5143\u7d20\u65f6\uff0c\u76f8\u7b49\u5143\u7d20\u7684\u76f8\u5bf9\u4f4d\u7f6e\u53ef\u80fd\u53d1\u751f\u53d8\u5316\u3002
        "},{"location":"chapter_sorting/insertion_sort/","title":"11.4 \u00a0 \u63d2\u5165\u6392\u5e8f","text":"

        \u63d2\u5165\u6392\u5e8f\uff08insertion sort\uff09\u662f\u4e00\u79cd\u7b80\u5355\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5b83\u7684\u5de5\u4f5c\u539f\u7406\u4e0e\u624b\u52a8\u6574\u7406\u4e00\u526f\u724c\u7684\u8fc7\u7a0b\u975e\u5e38\u76f8\u4f3c\u3002

        \u5177\u4f53\u6765\u8bf4\uff0c\u6211\u4eec\u5728\u672a\u6392\u5e8f\u533a\u95f4\u9009\u62e9\u4e00\u4e2a\u57fa\u51c6\u5143\u7d20\uff0c\u5c06\u8be5\u5143\u7d20\u4e0e\u5176\u5de6\u4fa7\u5df2\u6392\u5e8f\u533a\u95f4\u7684\u5143\u7d20\u9010\u4e00\u6bd4\u8f83\u5927\u5c0f\uff0c\u5e76\u5c06\u8be5\u5143\u7d20\u63d2\u5165\u5230\u6b63\u786e\u7684\u4f4d\u7f6e\u3002

        \u56fe 11-6 \u5c55\u793a\u4e86\u6570\u7ec4\u63d2\u5165\u5143\u7d20\u7684\u64cd\u4f5c\u6d41\u7a0b\u3002\u8bbe\u57fa\u51c6\u5143\u7d20\u4e3a base \uff0c\u6211\u4eec\u9700\u8981\u5c06\u4ece\u76ee\u6807\u7d22\u5f15\u5230 base \u4e4b\u95f4\u7684\u6240\u6709\u5143\u7d20\u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\uff0c\u7136\u540e\u5c06 base \u8d4b\u503c\u7ed9\u76ee\u6807\u7d22\u5f15\u3002

        \u56fe 11-6 \u00a0 \u5355\u6b21\u63d2\u5165\u64cd\u4f5c

        "},{"location":"chapter_sorting/insertion_sort/#1141","title":"11.4.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

        \u63d2\u5165\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u56fe 11-7 \u6240\u793a\u3002

        1. \u521d\u59cb\u72b6\u6001\u4e0b\uff0c\u6570\u7ec4\u7684\u7b2c 1 \u4e2a\u5143\u7d20\u5df2\u5b8c\u6210\u6392\u5e8f\u3002
        2. \u9009\u53d6\u6570\u7ec4\u7684\u7b2c 2 \u4e2a\u5143\u7d20\u4f5c\u4e3a base \uff0c\u5c06\u5176\u63d2\u5165\u5230\u6b63\u786e\u4f4d\u7f6e\u540e\uff0c\u6570\u7ec4\u7684\u524d 2 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002
        3. \u9009\u53d6\u7b2c 3 \u4e2a\u5143\u7d20\u4f5c\u4e3a base \uff0c\u5c06\u5176\u63d2\u5165\u5230\u6b63\u786e\u4f4d\u7f6e\u540e\uff0c\u6570\u7ec4\u7684\u524d 3 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002
        4. \u4ee5\u6b64\u7c7b\u63a8\uff0c\u5728\u6700\u540e\u4e00\u8f6e\u4e2d\uff0c\u9009\u53d6\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a base \uff0c\u5c06\u5176\u63d2\u5165\u5230\u6b63\u786e\u4f4d\u7f6e\u540e\uff0c\u6240\u6709\u5143\u7d20\u5747\u5df2\u6392\u5e8f\u3002

        \u56fe 11-7 \u00a0 \u63d2\u5165\u6392\u5e8f\u6d41\u7a0b

        \u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby insertion_sort.py
        def insertion_sort(nums: list[int]):\n    \"\"\"\u63d2\u5165\u6392\u5e8f\"\"\"\n    # \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n    for i in range(1, len(nums)):\n        base = nums[i]\n        j = i - 1\n        # \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n        while j >= 0 and nums[j] > base:\n            nums[j + 1] = nums[j]  # \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n            j -= 1\n        nums[j + 1] = base  # \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n
        insertion_sort.cpp
        /* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(vector<int> &nums) {\n    // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n    for (int i = 1; i < nums.size(); i++) {\n        int base = nums[i], j = i - 1;\n        // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n            j--;\n        }\n        nums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n    }\n}\n
        insertion_sort.java
        /* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(int[] nums) {\n    // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n    for (int i = 1; i < nums.length; i++) {\n        int base = nums[i], j = i - 1;\n        // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n            j--;\n        }\n        nums[j + 1] = base;        // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n    }\n}\n
        insertion_sort.cs
        /* \u63d2\u5165\u6392\u5e8f */\nvoid InsertionSort(int[] nums) {\n    // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n    for (int i = 1; i < nums.Length; i++) {\n        int bas = nums[i], j = i - 1;\n        // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n        while (j >= 0 && nums[j] > bas) {\n            nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n            j--;\n        }\n        nums[j + 1] = bas;         // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n    }\n}\n
        insertion_sort.go
        /* \u63d2\u5165\u6392\u5e8f */\nfunc insertionSort(nums []int) {\n    // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n    for i := 1; i < len(nums); i++ {\n        base := nums[i]\n        j := i - 1\n        // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n        for j >= 0 && nums[j] > base {\n            nums[j+1] = nums[j] // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n            j--\n        }\n        nums[j+1] = base // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n    }\n}\n
        insertion_sort.swift
        /* \u63d2\u5165\u6392\u5e8f */\nfunc insertionSort(nums: inout [Int]) {\n    // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n    for i in nums.indices.dropFirst() {\n        let base = nums[i]\n        var j = i - 1\n        // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n        while j >= 0, nums[j] > base {\n            nums[j + 1] = nums[j] // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n            j -= 1\n        }\n        nums[j + 1] = base // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n    }\n}\n
        insertion_sort.js
        /* \u63d2\u5165\u6392\u5e8f */\nfunction insertionSort(nums) {\n    // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n    for (let i = 1; i < nums.length; i++) {\n        let base = nums[i],\n            j = i - 1;\n        // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n            j--;\n        }\n        nums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n    }\n}\n
        insertion_sort.ts
        /* \u63d2\u5165\u6392\u5e8f */\nfunction insertionSort(nums: number[]): void {\n    // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n    for (let i = 1; i < nums.length; i++) {\n        const base = nums[i];\n        let j = i - 1;\n        // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n            j--;\n        }\n        nums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n    }\n}\n
        insertion_sort.dart
        /* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(List<int> nums) {\n  // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n  for (int i = 1; i < nums.length; i++) {\n    int base = nums[i], j = i - 1;\n    // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n    while (j >= 0 && nums[j] > base) {\n      nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n      j--;\n    }\n    nums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n  }\n}\n
        insertion_sort.rs
        /* \u63d2\u5165\u6392\u5e8f */\nfn insertion_sort(nums: &mut [i32]) {\n    // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n    for i in 1..nums.len() {\n        let (base, mut j) = (nums[i], (i - 1) as i32);\n        // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n        while j >= 0 && nums[j as usize] > base {\n            nums[(j + 1) as usize] = nums[j as usize]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n            j -= 1;\n        }\n        nums[(j + 1) as usize] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n    }\n}\n
        insertion_sort.c
        /* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(int nums[], int size) {\n    // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n    for (int i = 1; i < size; i++) {\n        int base = nums[i], j = i - 1;\n        // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n        while (j >= 0 && nums[j] > base) {\n            // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n            nums[j + 1] = nums[j];\n            j--;\n        }\n        // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n        nums[j + 1] = base;\n    }\n}\n
        insertion_sort.kt
        /* \u63d2\u5165\u6392\u5e8f */\nfun insertionSort(nums: IntArray) {\n    //\u5916\u5faa\u73af: \u5df2\u6392\u5e8f\u5143\u7d20\u4e3a 1, 2, ..., n\n    for (i in nums.indices) {\n        val base = nums[i]\n        var j = i - 1\n        // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j] // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n            j--\n        }\n        nums[j + 1] = base        // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n    }\n}\n
        insertion_sort.rb
        ### \u63d2\u5165\u6392\u5e8f ###\ndef insertion_sort(nums)\n  n = nums.length\n  # \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n  for i in 1...n\n    base = nums[i]\n    j = i - 1\n    # \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n    while j >= 0 && nums[j] > base\n      nums[j + 1] = nums[j] # \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n      j -= 1\n    end\n    nums[j + 1] = base # \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/insertion_sort/#1142","title":"11.4.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\)\u3001\u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u6bcf\u6b21\u63d2\u5165\u64cd\u4f5c\u5206\u522b\u9700\u8981\u5faa\u73af \\(n - 1\\)\u3001\\(n-2\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u6b21\uff0c\u6c42\u548c\u5f97\u5230 \\((n - 1) n / 2\\) \uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002\u5728\u9047\u5230\u6709\u5e8f\u6570\u636e\u65f6\uff0c\u63d2\u5165\u64cd\u4f5c\u4f1a\u63d0\u524d\u7ec8\u6b62\u3002\u5f53\u8f93\u5165\u6570\u7ec4\u5b8c\u5168\u6709\u5e8f\u65f6\uff0c\u63d2\u5165\u6392\u5e8f\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3002
        • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\)\u3001\u539f\u5730\u6392\u5e8f\uff1a\u6307\u9488 \\(i\\) \u548c \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002
        • \u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u63d2\u5165\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f1a\u5c06\u5143\u7d20\u63d2\u5165\u5230\u76f8\u7b49\u5143\u7d20\u7684\u53f3\u4fa7\uff0c\u4e0d\u4f1a\u6539\u53d8\u5b83\u4eec\u7684\u987a\u5e8f\u3002
        "},{"location":"chapter_sorting/insertion_sort/#1143","title":"11.4.3 \u00a0 \u63d2\u5165\u6392\u5e8f\u7684\u4f18\u52bf","text":"

        \u63d2\u5165\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u800c\u6211\u4eec\u5373\u5c06\u5b66\u4e60\u7684\u5feb\u901f\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002\u5c3d\u7ba1\u63d2\u5165\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u66f4\u9ad8\uff0c\u4f46\u5728\u6570\u636e\u91cf\u8f83\u5c0f\u7684\u60c5\u51b5\u4e0b\uff0c\u63d2\u5165\u6392\u5e8f\u901a\u5e38\u66f4\u5feb\u3002

        \u8fd9\u4e2a\u7ed3\u8bba\u4e0e\u7ebf\u6027\u67e5\u627e\u548c\u4e8c\u5206\u67e5\u627e\u7684\u9002\u7528\u60c5\u51b5\u7684\u7ed3\u8bba\u7c7b\u4f3c\u3002\u5feb\u901f\u6392\u5e8f\u8fd9\u7c7b \\(O(n \\log n)\\) \u7684\u7b97\u6cd5\u5c5e\u4e8e\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5f80\u5f80\u5305\u542b\u66f4\u591a\u5355\u5143\u8ba1\u7b97\u64cd\u4f5c\u3002\u800c\u5728\u6570\u636e\u91cf\u8f83\u5c0f\u65f6\uff0c\\(n^2\\) \u548c \\(n \\log n\\) \u7684\u6570\u503c\u6bd4\u8f83\u63a5\u8fd1\uff0c\u590d\u6742\u5ea6\u4e0d\u5360\u4e3b\u5bfc\u5730\u4f4d\uff0c\u6bcf\u8f6e\u4e2d\u7684\u5355\u5143\u64cd\u4f5c\u6570\u91cf\u8d77\u5230\u51b3\u5b9a\u6027\u4f5c\u7528\u3002

        \u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\uff08\u4f8b\u5982 Java\uff09\u7684\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\u91c7\u7528\u4e86\u63d2\u5165\u6392\u5e8f\uff0c\u5927\u81f4\u601d\u8def\u4e3a\uff1a\u5bf9\u4e8e\u957f\u6570\u7ec4\uff0c\u91c7\u7528\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u4f8b\u5982\u5feb\u901f\u6392\u5e8f\uff1b\u5bf9\u4e8e\u77ed\u6570\u7ec4\uff0c\u76f4\u63a5\u4f7f\u7528\u63d2\u5165\u6392\u5e8f\u3002

        \u867d\u7136\u5192\u6ce1\u6392\u5e8f\u3001\u9009\u62e9\u6392\u5e8f\u548c\u63d2\u5165\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n^2)\\) \uff0c\u4f46\u5728\u5b9e\u9645\u60c5\u51b5\u4e2d\uff0c\u63d2\u5165\u6392\u5e8f\u7684\u4f7f\u7528\u9891\u7387\u663e\u8457\u9ad8\u4e8e\u5192\u6ce1\u6392\u5e8f\u548c\u9009\u62e9\u6392\u5e8f\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002

        • \u5192\u6ce1\u6392\u5e8f\u57fa\u4e8e\u5143\u7d20\u4ea4\u6362\u5b9e\u73b0\uff0c\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\uff0c\u5171\u6d89\u53ca 3 \u4e2a\u5355\u5143\u64cd\u4f5c\uff1b\u63d2\u5165\u6392\u5e8f\u57fa\u4e8e\u5143\u7d20\u8d4b\u503c\u5b9e\u73b0\uff0c\u4ec5\u9700 1 \u4e2a\u5355\u5143\u64cd\u4f5c\u3002\u56e0\u6b64\uff0c\u5192\u6ce1\u6392\u5e8f\u7684\u8ba1\u7b97\u5f00\u9500\u901a\u5e38\u6bd4\u63d2\u5165\u6392\u5e8f\u66f4\u9ad8\u3002
        • \u9009\u62e9\u6392\u5e8f\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n^2)\\) \u3002\u5982\u679c\u7ed9\u5b9a\u4e00\u7ec4\u90e8\u5206\u6709\u5e8f\u7684\u6570\u636e\uff0c\u63d2\u5165\u6392\u5e8f\u901a\u5e38\u6bd4\u9009\u62e9\u6392\u5e8f\u6548\u7387\u66f4\u9ad8\u3002
        • \u9009\u62e9\u6392\u5e8f\u4e0d\u7a33\u5b9a\uff0c\u65e0\u6cd5\u5e94\u7528\u4e8e\u591a\u7ea7\u6392\u5e8f\u3002
        "},{"location":"chapter_sorting/merge_sort/","title":"11.6 \u00a0 \u5f52\u5e76\u6392\u5e8f","text":"

        \u5f52\u5e76\u6392\u5e8f\uff08merge sort\uff09\u662f\u4e00\u79cd\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5305\u542b\u56fe 11-10 \u6240\u793a\u7684\u201c\u5212\u5206\u201d\u548c\u201c\u5408\u5e76\u201d\u9636\u6bb5\u3002

        1. \u5212\u5206\u9636\u6bb5\uff1a\u901a\u8fc7\u9012\u5f52\u4e0d\u65ad\u5730\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5904\u5206\u5f00\uff0c\u5c06\u957f\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u8f6c\u6362\u4e3a\u77ed\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u3002
        2. \u5408\u5e76\u9636\u6bb5\uff1a\u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u5212\u5206\uff0c\u5f00\u59cb\u5408\u5e76\uff0c\u6301\u7eed\u5730\u5c06\u5de6\u53f3\u4e24\u4e2a\u8f83\u77ed\u7684\u6709\u5e8f\u6570\u7ec4\u5408\u5e76\u4e3a\u4e00\u4e2a\u8f83\u957f\u7684\u6709\u5e8f\u6570\u7ec4\uff0c\u76f4\u81f3\u7ed3\u675f\u3002

        \u56fe 11-10 \u00a0 \u5f52\u5e76\u6392\u5e8f\u7684\u5212\u5206\u4e0e\u5408\u5e76\u9636\u6bb5

        "},{"location":"chapter_sorting/merge_sort/#1161","title":"11.6.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

        \u5982\u56fe 11-11 \u6240\u793a\uff0c\u201c\u5212\u5206\u9636\u6bb5\u201d\u4ece\u9876\u81f3\u5e95\u9012\u5f52\u5730\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5207\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\u3002

        1. \u8ba1\u7b97\u6570\u7ec4\u4e2d\u70b9 mid \uff0c\u9012\u5f52\u5212\u5206\u5de6\u5b50\u6570\u7ec4\uff08\u533a\u95f4 [left, mid] \uff09\u548c\u53f3\u5b50\u6570\u7ec4\uff08\u533a\u95f4 [mid + 1, right] \uff09\u3002
        2. \u9012\u5f52\u6267\u884c\u6b65\u9aa4 1. \uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u533a\u95f4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u3002

        \u201c\u5408\u5e76\u9636\u6bb5\u201d\u4ece\u5e95\u81f3\u9876\u5730\u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u5408\u5e76\u4e3a\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4ece\u957f\u5ea6\u4e3a 1 \u7684\u5b50\u6570\u7ec4\u5f00\u59cb\u5408\u5e76\uff0c\u5408\u5e76\u9636\u6bb5\u4e2d\u7684\u6bcf\u4e2a\u5b50\u6570\u7ec4\u90fd\u662f\u6709\u5e8f\u7684\u3002

        <1><2><3><4><5><6><7><8><9><10>

        \u56fe 11-11 \u00a0 \u5f52\u5e76\u6392\u5e8f\u6b65\u9aa4

        \u89c2\u5bdf\u53d1\u73b0\uff0c\u5f52\u5e76\u6392\u5e8f\u4e0e\u4e8c\u53c9\u6811\u540e\u5e8f\u904d\u5386\u7684\u9012\u5f52\u987a\u5e8f\u662f\u4e00\u81f4\u7684\u3002

        • \u540e\u5e8f\u904d\u5386\uff1a\u5148\u9012\u5f52\u5de6\u5b50\u6811\uff0c\u518d\u9012\u5f52\u53f3\u5b50\u6811\uff0c\u6700\u540e\u5904\u7406\u6839\u8282\u70b9\u3002
        • \u5f52\u5e76\u6392\u5e8f\uff1a\u5148\u9012\u5f52\u5de6\u5b50\u6570\u7ec4\uff0c\u518d\u9012\u5f52\u53f3\u5b50\u6570\u7ec4\uff0c\u6700\u540e\u5904\u7406\u5408\u5e76\u3002

        \u5f52\u5e76\u6392\u5e8f\u7684\u5b9e\u73b0\u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\u3002\u8bf7\u6ce8\u610f\uff0cnums \u7684\u5f85\u5408\u5e76\u533a\u95f4\u4e3a [left, right] \uff0c\u800c tmp \u7684\u5bf9\u5e94\u533a\u95f4\u4e3a [0, right - left] \u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby merge_sort.py
        def merge(nums: list[int], left: int, mid: int, right: int):\n    \"\"\"\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\"\"\"\n    # \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n    # \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n    tmp = [0] * (right - left + 1)\n    # \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n    i, j, k = left, mid + 1, 0\n    # \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while i <= mid and j <= right:\n        if nums[i] <= nums[j]:\n            tmp[k] = nums[i]\n            i += 1\n        else:\n            tmp[k] = nums[j]\n            j += 1\n        k += 1\n    # \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while i <= mid:\n        tmp[k] = nums[i]\n        i += 1\n        k += 1\n    while j <= right:\n        tmp[k] = nums[j]\n        j += 1\n        k += 1\n    # \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n    for k in range(0, len(tmp)):\n        nums[left + k] = tmp[k]\n\ndef merge_sort(nums: list[int], left: int, right: int):\n    \"\"\"\u5f52\u5e76\u6392\u5e8f\"\"\"\n    # \u7ec8\u6b62\u6761\u4ef6\n    if left >= right:\n        return  # \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    # \u5212\u5206\u9636\u6bb5\n    mid = (left + right) // 2 # \u8ba1\u7b97\u4e2d\u70b9\n    merge_sort(nums, left, mid)  # \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n    merge_sort(nums, mid + 1, right)  # \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n    # \u5408\u5e76\u9636\u6bb5\n    merge(nums, left, mid, right)\n
        merge_sort.cpp
        /* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nvoid merge(vector<int> &nums, int left, int mid, int right) {\n    // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n    // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n    vector<int> tmp(right - left + 1);\n    // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n    int i = left, j = mid + 1, k = 0;\n    // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++];\n        else\n            tmp[k++] = nums[j++];\n    }\n    // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n    for (k = 0; k < tmp.size(); k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(vector<int> &nums, int left, int right) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (left >= right)\n        return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    // \u5212\u5206\u9636\u6bb5\n    int mid = left + (right - left) / 2;    // \u8ba1\u7b97\u4e2d\u70b9\n    mergeSort(nums, left, mid);      // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n    mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n    // \u5408\u5e76\u9636\u6bb5\n    merge(nums, left, mid, right);\n}\n
        merge_sort.java
        /* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nvoid merge(int[] nums, int left, int mid, int right) {\n    // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n    // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n    int[] tmp = new int[right - left + 1];\n    // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n    int i = left, j = mid + 1, k = 0;\n    // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++];\n        else\n            tmp[k++] = nums[j++];\n    }\n    // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n    for (k = 0; k < tmp.length; k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(int[] nums, int left, int right) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (left >= right)\n        return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    // \u5212\u5206\u9636\u6bb5\n    int mid = left + (right - left) / 2; // \u8ba1\u7b97\u4e2d\u70b9\n    mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n    mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n    // \u5408\u5e76\u9636\u6bb5\n    merge(nums, left, mid, right);\n}\n
        merge_sort.cs
        /* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nvoid Merge(int[] nums, int left, int mid, int right) {\n    // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n    // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n    int[] tmp = new int[right - left + 1];\n    // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n    int i = left, j = mid + 1, k = 0;\n    // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++];\n        else\n            tmp[k++] = nums[j++];\n    }\n    // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n    for (k = 0; k < tmp.Length; ++k) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid MergeSort(int[] nums, int left, int right) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (left >= right) return;       // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    // \u5212\u5206\u9636\u6bb5\n    int mid = left + (right - left) / 2;    // \u8ba1\u7b97\u4e2d\u70b9\n    MergeSort(nums, left, mid);      // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n    MergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n    // \u5408\u5e76\u9636\u6bb5\n    Merge(nums, left, mid, right);\n}\n
        merge_sort.go
        /* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nfunc merge(nums []int, left, mid, right int) {\n    // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n    // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n    tmp := make([]int, right-left+1)\n    // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n    i, j, k := left, mid+1, 0\n    // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    for i <= mid && j <= right {\n        if nums[i] <= nums[j] {\n            tmp[k] = nums[i]\n            i++\n        } else {\n            tmp[k] = nums[j]\n            j++\n        }\n        k++\n    }\n    // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    for i <= mid {\n        tmp[k] = nums[i]\n        i++\n        k++\n    }\n    for j <= right {\n        tmp[k] = nums[j]\n        j++\n        k++\n    }\n    // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n    for k := 0; k < len(tmp); k++ {\n        nums[left+k] = tmp[k]\n    }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nfunc mergeSort(nums []int, left, right int) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if left >= right {\n        return\n    }\n    // \u5212\u5206\u9636\u6bb5\n    mid := left + (right - left) / 2\n    mergeSort(nums, left, mid)\n    mergeSort(nums, mid+1, right)\n    // \u5408\u5e76\u9636\u6bb5\n    merge(nums, left, mid, right)\n}\n
        merge_sort.swift
        /* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nfunc merge(nums: inout [Int], left: Int, mid: Int, right: Int) {\n    // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n    // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n    var tmp = Array(repeating: 0, count: right - left + 1)\n    // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n    var i = left, j = mid + 1, k = 0\n    // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while i <= mid, j <= right {\n        if nums[i] <= nums[j] {\n            tmp[k] = nums[i]\n            i += 1\n        } else {\n            tmp[k] = nums[j]\n            j += 1\n        }\n        k += 1\n    }\n    // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while i <= mid {\n        tmp[k] = nums[i]\n        i += 1\n        k += 1\n    }\n    while j <= right {\n        tmp[k] = nums[j]\n        j += 1\n        k += 1\n    }\n    // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n    for k in tmp.indices {\n        nums[left + k] = tmp[k]\n    }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nfunc mergeSort(nums: inout [Int], left: Int, right: Int) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if left >= right { // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n        return\n    }\n    // \u5212\u5206\u9636\u6bb5\n    let mid = left + (right - left) / 2 // \u8ba1\u7b97\u4e2d\u70b9\n    mergeSort(nums: &nums, left: left, right: mid) // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n    mergeSort(nums: &nums, left: mid + 1, right: right) // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n    // \u5408\u5e76\u9636\u6bb5\n    merge(nums: &nums, left: left, mid: mid, right: right)\n}\n
        merge_sort.js
        /* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nfunction merge(nums, left, mid, right) {\n    // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n    // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n    const tmp = new Array(right - left + 1);\n    // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n    let i = left,\n        j = mid + 1,\n        k = 0;\n    // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j]) {\n            tmp[k++] = nums[i++];\n        } else {\n            tmp[k++] = nums[j++];\n        }\n    }\n    // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n    for (k = 0; k < tmp.length; k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nfunction mergeSort(nums, left, right) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    // \u5212\u5206\u9636\u6bb5\n    let mid = Math.floor(left + (right - left) / 2); // \u8ba1\u7b97\u4e2d\u70b9\n    mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n    mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n    // \u5408\u5e76\u9636\u6bb5\n    merge(nums, left, mid, right);\n}\n
        merge_sort.ts
        /* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nfunction merge(nums: number[], left: number, mid: number, right: number): void {\n    // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n    // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n    const tmp = new Array(right - left + 1);\n    // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n    let i = left,\n        j = mid + 1,\n        k = 0;\n    // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j]) {\n            tmp[k++] = nums[i++];\n        } else {\n            tmp[k++] = nums[j++];\n        }\n    }\n    // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n    for (k = 0; k < tmp.length; k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nfunction mergeSort(nums: number[], left: number, right: number): void {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    // \u5212\u5206\u9636\u6bb5\n    let mid = Math.floor(left + (right - left) / 2); // \u8ba1\u7b97\u4e2d\u70b9\n    mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n    mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n    // \u5408\u5e76\u9636\u6bb5\n    merge(nums, left, mid, right);\n}\n
        merge_sort.dart
        /* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nvoid merge(List<int> nums, int left, int mid, int right) {\n  // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n  // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n  List<int> tmp = List.filled(right - left + 1, 0);\n  // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n  int i = left, j = mid + 1, k = 0;\n  // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n  while (i <= mid && j <= right) {\n    if (nums[i] <= nums[j])\n      tmp[k++] = nums[i++];\n    else\n      tmp[k++] = nums[j++];\n  }\n  // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n  while (i <= mid) {\n    tmp[k++] = nums[i++];\n  }\n  while (j <= right) {\n    tmp[k++] = nums[j++];\n  }\n  // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n  for (k = 0; k < tmp.length; k++) {\n    nums[left + k] = tmp[k];\n  }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(List<int> nums, int left, int right) {\n  // \u7ec8\u6b62\u6761\u4ef6\n  if (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n  // \u5212\u5206\u9636\u6bb5\n  int mid = left + (right - left) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\n  mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n  mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n  // \u5408\u5e76\u9636\u6bb5\n  merge(nums, left, mid, right);\n}\n
        merge_sort.rs
        /* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nfn merge(nums: &mut [i32], left: usize, mid: usize, right: usize) {\n    // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n    // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n    let tmp_size = right - left + 1;\n    let mut tmp = vec![0; tmp_size];\n    // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n    let (mut i, mut j, mut k) = (left, mid + 1, 0);\n    // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while i <= mid && j <= right {\n        if nums[i] <= nums[j] {\n            tmp[k] = nums[i];\n            i += 1;\n        } else {\n            tmp[k] = nums[j];\n            j += 1;\n        }\n        k += 1;\n    }\n    // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while i <= mid {\n        tmp[k] = nums[i];\n        k += 1;\n        i += 1;\n    }\n    while j <= right {\n        tmp[k] = nums[j];\n        k += 1;\n        j += 1;\n    }\n    // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n    for k in 0..tmp_size {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nfn merge_sort(nums: &mut [i32], left: usize, right: usize) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if left >= right {\n        return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    }\n\n    // \u5212\u5206\u9636\u6bb5\n    let mid = left + (right - left) / 2; // \u8ba1\u7b97\u4e2d\u70b9\n    merge_sort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n    merge_sort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n\n    // \u5408\u5e76\u9636\u6bb5\n    merge(nums, left, mid, right);\n}\n
        merge_sort.c
        /* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nvoid merge(int *nums, int left, int mid, int right) {\n    // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n    // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n    int tmpSize = right - left + 1;\n    int *tmp = (int *)malloc(tmpSize * sizeof(int));\n    // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n    int i = left, j = mid + 1, k = 0;\n    // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j]) {\n            tmp[k++] = nums[i++];\n        } else {\n            tmp[k++] = nums[j++];\n        }\n    }\n    // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n    for (k = 0; k < tmpSize; ++k) {\n        nums[left + k] = tmp[k];\n    }\n    // \u91ca\u653e\u5185\u5b58\n    free(tmp);\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(int *nums, int left, int right) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (left >= right)\n        return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    // \u5212\u5206\u9636\u6bb5\n    int mid = left + (right - left) / 2;    // \u8ba1\u7b97\u4e2d\u70b9\n    mergeSort(nums, left, mid);      // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n    mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n    // \u5408\u5e76\u9636\u6bb5\n    merge(nums, left, mid, right);\n}\n
        merge_sort.kt
        /* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nfun merge(nums: IntArray, left: Int, mid: Int, right: Int) {\n    // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n    // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n    val tmp = IntArray(right - left + 1)\n    // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n    var i = left\n    var j = mid + 1\n    var k = 0\n    // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++]\n        else\n            tmp[k++] = nums[j++]\n    }\n    // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n    while (i <= mid) {\n        tmp[k++] = nums[i++]\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++]\n    }\n    // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n    for (l in tmp.indices) {\n        nums[left + l] = tmp[l]\n    }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nfun mergeSort(nums: IntArray, left: Int, right: Int) {\n    // \u7ec8\u6b62\u6761\u4ef6\n    if (left >= right) return  // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    // \u5212\u5206\u9636\u6bb5\n    val mid = left + (right - left) / 2 // \u8ba1\u7b97\u4e2d\u70b9\n    mergeSort(nums, left, mid) // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n    mergeSort(nums, mid + 1, right) // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n    // \u5408\u5e76\u9636\u6bb5\n    merge(nums, left, mid, right)\n}\n
        merge_sort.rb
        ### \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 ###\ndef merge(nums, left, mid, right)\n  # \u5de6\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4\u4e3a [mid+1, right]\n  # \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp\uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n  tmp = Array.new(right - left + 1, 0)\n  # \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n  i, j, k = left, mid + 1, 0\n  # \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u8fdb\u884c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n  while i <= mid && j <= right\n    if nums[i] <= nums[j]\n      tmp[k] = nums[i]\n      i += 1\n    else\n      tmp[k] = nums[j]\n      j += 1\n    end\n    k += 1\n  end\n  # \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n  while i <= mid\n    tmp[k] = nums[i]\n    i += 1\n    k += 1\n  end\n  while j <= right\n    tmp[k] = nums[j]\n    j += 1\n    k += 1\n  end\n  # \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n  (0...tmp.length).each do |k|\n    nums[left + k] = tmp[k]\n  end\nend\n\n### \u5f52\u5e76\u6392\u5e8f ###\ndef merge_sort(nums, left, right)\n  # \u7ec8\u6b62\u6761\u4ef6\n  # \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n  return if left >= right\n  # \u5212\u5206\u9636\u6bb5\n  mid = left + (right - left) / 2 # \u8ba1\u7b97\u4e2d\u70b9\n  merge_sort(nums, left, mid) # \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n  merge_sort(nums, mid + 1, right) # \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n  # \u5408\u5e76\u9636\u6bb5\n  merge(nums, left, mid, right)\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/merge_sort/#1162","title":"11.6.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\)\u3001\u975e\u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u5212\u5206\u4ea7\u751f\u9ad8\u5ea6\u4e3a \\(\\log n\\) \u7684\u9012\u5f52\u6811\uff0c\u6bcf\u5c42\u5408\u5e76\u7684\u603b\u64cd\u4f5c\u6570\u91cf\u4e3a \\(n\\) \uff0c\u56e0\u6b64\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002
        • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\)\u3001\u975e\u539f\u5730\u6392\u5e8f\uff1a\u9012\u5f52\u6df1\u5ea6\u4e3a \\(\\log n\\) \uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\u3002\u5408\u5e76\u64cd\u4f5c\u9700\u8981\u501f\u52a9\u8f85\u52a9\u6570\u7ec4\u5b9e\u73b0\uff0c\u4f7f\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002
        • \u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u5408\u5e76\u8fc7\u7a0b\u4e2d\uff0c\u76f8\u7b49\u5143\u7d20\u7684\u6b21\u5e8f\u4fdd\u6301\u4e0d\u53d8\u3002
        "},{"location":"chapter_sorting/merge_sort/#1163","title":"11.6.3 \u00a0 \u94fe\u8868\u6392\u5e8f","text":"

        \u5bf9\u4e8e\u94fe\u8868\uff0c\u5f52\u5e76\u6392\u5e8f\u76f8\u8f83\u4e8e\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\u5177\u6709\u663e\u8457\u4f18\u52bf\uff0c\u53ef\u4ee5\u5c06\u94fe\u8868\u6392\u5e8f\u4efb\u52a1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(1)\\) \u3002

        • \u5212\u5206\u9636\u6bb5\uff1a\u53ef\u4ee5\u4f7f\u7528\u201c\u8fed\u4ee3\u201d\u66ff\u4ee3\u201c\u9012\u5f52\u201d\u6765\u5b9e\u73b0\u94fe\u8868\u5212\u5206\u5de5\u4f5c\uff0c\u4ece\u800c\u7701\u53bb\u9012\u5f52\u4f7f\u7528\u7684\u6808\u5e27\u7a7a\u95f4\u3002
        • \u5408\u5e76\u9636\u6bb5\uff1a\u5728\u94fe\u8868\u4e2d\uff0c\u8282\u70b9\u589e\u5220\u64cd\u4f5c\u4ec5\u9700\u6539\u53d8\u5f15\u7528\uff08\u6307\u9488\uff09\u5373\u53ef\u5b9e\u73b0\uff0c\u56e0\u6b64\u5408\u5e76\u9636\u6bb5\uff08\u5c06\u4e24\u4e2a\u77ed\u6709\u5e8f\u94fe\u8868\u5408\u5e76\u4e3a\u4e00\u4e2a\u957f\u6709\u5e8f\u94fe\u8868\uff09\u65e0\u987b\u521b\u5efa\u989d\u5916\u94fe\u8868\u3002

        \u5177\u4f53\u5b9e\u73b0\u7ec6\u8282\u6bd4\u8f83\u590d\u6742\uff0c\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u67e5\u9605\u76f8\u5173\u8d44\u6599\u8fdb\u884c\u5b66\u4e60\u3002

        "},{"location":"chapter_sorting/quick_sort/","title":"11.5 \u00a0 \u5feb\u901f\u6392\u5e8f","text":"

        \u5feb\u901f\u6392\u5e8f\uff08quick sort\uff09\u662f\u4e00\u79cd\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u8fd0\u884c\u9ad8\u6548\uff0c\u5e94\u7528\u5e7f\u6cdb\u3002

        \u5feb\u901f\u6392\u5e8f\u7684\u6838\u5fc3\u64cd\u4f5c\u662f\u201c\u54e8\u5175\u5212\u5206\u201d\uff0c\u5176\u76ee\u6807\u662f\uff1a\u9009\u62e9\u6570\u7ec4\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u4f5c\u4e3a\u201c\u57fa\u51c6\u6570\u201d\uff0c\u5c06\u6240\u6709\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\u79fb\u5230\u5176\u5de6\u4fa7\uff0c\u800c\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\u79fb\u5230\u5176\u53f3\u4fa7\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u54e8\u5175\u5212\u5206\u7684\u6d41\u7a0b\u5982\u56fe 11-8 \u6240\u793a\u3002

        1. \u9009\u53d6\u6570\u7ec4\u6700\u5de6\u7aef\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\u6570\uff0c\u521d\u59cb\u5316\u4e24\u4e2a\u6307\u9488 i \u548c j \u5206\u522b\u6307\u5411\u6570\u7ec4\u7684\u4e24\u7aef\u3002
        2. \u8bbe\u7f6e\u4e00\u4e2a\u5faa\u73af\uff0c\u5728\u6bcf\u8f6e\u4e2d\u4f7f\u7528 i\uff08j\uff09\u5206\u522b\u5bfb\u627e\u7b2c\u4e00\u4e2a\u6bd4\u57fa\u51c6\u6570\u5927\uff08\u5c0f\uff09\u7684\u5143\u7d20\uff0c\u7136\u540e\u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\u3002
        3. \u5faa\u73af\u6267\u884c\u6b65\u9aa4 2. \uff0c\u76f4\u5230 i \u548c j \u76f8\u9047\u65f6\u505c\u6b62\uff0c\u6700\u540e\u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u4e2a\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\u3002
        <1><2><3><4><5><6><7><8><9>

        \u56fe 11-8 \u00a0 \u54e8\u5175\u5212\u5206\u6b65\u9aa4

        \u54e8\u5175\u5212\u5206\u5b8c\u6210\u540e\uff0c\u539f\u6570\u7ec4\u88ab\u5212\u5206\u6210\u4e09\u90e8\u5206\uff1a\u5de6\u5b50\u6570\u7ec4\u3001\u57fa\u51c6\u6570\u3001\u53f3\u5b50\u6570\u7ec4\uff0c\u4e14\u6ee1\u8db3\u201c\u5de6\u5b50\u6570\u7ec4\u4efb\u610f\u5143\u7d20 \\(\\leq\\) \u57fa\u51c6\u6570 \\(\\leq\\) \u53f3\u5b50\u6570\u7ec4\u4efb\u610f\u5143\u7d20\u201d\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u63a5\u4e0b\u6765\u53ea\u9700\u5bf9\u8fd9\u4e24\u4e2a\u5b50\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002

        \u5feb\u901f\u6392\u5e8f\u7684\u5206\u6cbb\u7b56\u7565

        \u54e8\u5175\u5212\u5206\u7684\u5b9e\u8d28\u662f\u5c06\u4e00\u4e2a\u8f83\u957f\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u7b80\u5316\u4e3a\u4e24\u4e2a\u8f83\u77ed\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
        def partition(self, nums: list[int], left: int, right: int) -> int:\n    \"\"\"\u54e8\u5175\u5212\u5206\"\"\"\n    # \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    i, j = left, right\n    while i < j:\n        while i < j and nums[j] >= nums[left]:\n            j -= 1  # \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while i < j and nums[i] <= nums[left]:\n            i += 1  # \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        # \u5143\u7d20\u4ea4\u6362\n        nums[i], nums[j] = nums[j], nums[i]\n    # \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    nums[i], nums[left] = nums[left], nums[i]\n    return i  # \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n
        quick_sort.cpp
        /* \u54e8\u5175\u5212\u5206 */\nint partition(vector<int> &nums, int left, int right) {\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;                // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while (i < j && nums[i] <= nums[left])\n            i++;                // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        swap(nums[i], nums[j]); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    swap(nums[i], nums[left]);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i;                   // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.java
        /* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(int[] nums, int i, int j) {\n    int tmp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5212\u5206 */\nint partition(int[] nums, int left, int right) {\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    swap(nums, i, left);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i;             // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.cs
        /* \u5143\u7d20\u4ea4\u6362 */\nvoid Swap(int[] nums, int i, int j) {\n    (nums[j], nums[i]) = (nums[i], nums[j]);\n}\n\n/* \u54e8\u5175\u5212\u5206 */\nint Partition(int[] nums, int left, int right) {\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        Swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    Swap(nums, i, left);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i;             // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.go
        /* \u54e8\u5175\u5212\u5206 */\nfunc (q *quickSort) partition(nums []int, left, right int) int {\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    i, j := left, right\n    for i < j {\n        for i < j && nums[j] >= nums[left] {\n            j-- // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        for i < j && nums[i] <= nums[left] {\n            i++ // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        // \u5143\u7d20\u4ea4\u6362\n        nums[i], nums[j] = nums[j], nums[i]\n    }\n    // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    nums[i], nums[left] = nums[left], nums[i]\n    return i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.swift
        /* \u54e8\u5175\u5212\u5206 */\nfunc partition(nums: inout [Int], left: Int, right: Int) -> Int {\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    var i = left\n    var j = right\n    while i < j {\n        while i < j, nums[j] >= nums[left] {\n            j -= 1 // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        while i < j, nums[i] <= nums[left] {\n            i += 1 // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        nums.swapAt(i, j) // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    nums.swapAt(i, left) // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.js
        /* \u5143\u7d20\u4ea4\u6362 */\nswap(nums, i, j) {\n    let tmp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5212\u5206 */\npartition(nums, left, right) {\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    let i = left,\n        j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) {\n            j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        while (i < j && nums[i] <= nums[left]) {\n            i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        // \u5143\u7d20\u4ea4\u6362\n        this.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    this.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.ts
        /* \u5143\u7d20\u4ea4\u6362 */\nswap(nums: number[], i: number, j: number): void {\n    let tmp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5212\u5206 */\npartition(nums: number[], left: number, right: number): number {\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    let i = left,\n        j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) {\n            j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        while (i < j && nums[i] <= nums[left]) {\n            i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        // \u5143\u7d20\u4ea4\u6362\n        this.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    this.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.dart
        /* \u5143\u7d20\u4ea4\u6362 */\nvoid _swap(List<int> nums, int i, int j) {\n  int tmp = nums[i];\n  nums[i] = nums[j];\n  nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5212\u5206 */\nint _partition(List<int> nums, int left, int right) {\n  // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n  int i = left, j = right;\n  while (i < j) {\n    while (i < j && nums[j] >= nums[left]) j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n    while (i < j && nums[i] <= nums[left]) i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n    _swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n  }\n  _swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n  return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.rs
        /* \u54e8\u5175\u5212\u5206 */\nfn partition(nums: &mut [i32], left: usize, right: usize) -> usize {\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    let (mut i, mut j) = (left, right);\n    while i < j {\n        while i < j && nums[j] >= nums[left] {\n            j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        while i < j && nums[i] <= nums[left] {\n            i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        nums.swap(i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    nums.swap(i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.c
        /* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(int nums[], int i, int j) {\n    int tmp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5212\u5206 */\nint partition(int nums[], int left, int right) {\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) {\n            j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        while (i < j && nums[i] <= nums[left]) {\n            i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n        swap(nums, i, j);\n    }\n    // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    swap(nums, i, left);\n    // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n    return i;\n}\n
        quick_sort.kt
        /* \u5143\u7d20\u4ea4\u6362 */\nfun swap(nums: IntArray, i: Int, j: Int) {\n    val temp = nums[i]\n    nums[i] = nums[j]\n    nums[j] = temp\n}\n\n/* \u54e8\u5175\u5212\u5206 */\nfun partition(nums: IntArray, left: Int, right: Int): Int {\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    var i = left\n    var j = right\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--           // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while (i < j && nums[i] <= nums[left])\n            i++           // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        swap(nums, i, j)  // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    swap(nums, i, left)   // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i              // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.rb
        ### \u54e8\u5175\u5212\u5206 ###\ndef partition(nums, left, right)\n  # \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n  i, j = left, right\n  while i < j\n    while i < j && nums[j] >= nums[left]\n      j -= 1 # \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n    end\n    while i < j && nums[i] <= nums[left]\n      i += 1 # \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n    end\n    # \u5143\u7d20\u4ea4\u6362\n    nums[i], nums[j] = nums[j], nums[i]\n  end\n  # \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n  nums[i], nums[left] = nums[left], nums[i]\n  i # \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/quick_sort/#1151","title":"11.5.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

        \u5feb\u901f\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u56fe 11-9 \u6240\u793a\u3002

        1. \u9996\u5148\uff0c\u5bf9\u539f\u6570\u7ec4\u6267\u884c\u4e00\u6b21\u201c\u54e8\u5175\u5212\u5206\u201d\uff0c\u5f97\u5230\u672a\u6392\u5e8f\u7684\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u3002
        2. \u7136\u540e\uff0c\u5bf9\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u5206\u522b\u9012\u5f52\u6267\u884c\u201c\u54e8\u5175\u5212\u5206\u201d\u3002
        3. \u6301\u7eed\u9012\u5f52\uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\uff0c\u4ece\u800c\u5b8c\u6210\u6574\u4e2a\u6570\u7ec4\u7684\u6392\u5e8f\u3002

        \u56fe 11-9 \u00a0 \u5feb\u901f\u6392\u5e8f\u6d41\u7a0b

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
        def quick_sort(self, nums: list[int], left: int, right: int):\n    \"\"\"\u5feb\u901f\u6392\u5e8f\"\"\"\n    # \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    if left >= right:\n        return\n    # \u54e8\u5175\u5212\u5206\n    pivot = self.partition(nums, left, right)\n    # \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    self.quick_sort(nums, left, pivot - 1)\n    self.quick_sort(nums, pivot + 1, right)\n
        quick_sort.cpp
        /* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(vector<int> &nums, int left, int right) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    if (left >= right)\n        return;\n    // \u54e8\u5175\u5212\u5206\n    int pivot = partition(nums, left, right);\n    // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    quickSort(nums, left, pivot - 1);\n    quickSort(nums, pivot + 1, right);\n}\n
        quick_sort.java
        /* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(int[] nums, int left, int right) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    if (left >= right)\n        return;\n    // \u54e8\u5175\u5212\u5206\n    int pivot = partition(nums, left, right);\n    // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    quickSort(nums, left, pivot - 1);\n    quickSort(nums, pivot + 1, right);\n}\n
        quick_sort.cs
        /* \u5feb\u901f\u6392\u5e8f */\nvoid QuickSort(int[] nums, int left, int right) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    if (left >= right)\n        return;\n    // \u54e8\u5175\u5212\u5206\n    int pivot = Partition(nums, left, right);\n    // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    QuickSort(nums, left, pivot - 1);\n    QuickSort(nums, pivot + 1, right);\n}\n
        quick_sort.go
        /* \u5feb\u901f\u6392\u5e8f */\nfunc (q *quickSort) quickSort(nums []int, left, right int) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    if left >= right {\n        return\n    }\n    // \u54e8\u5175\u5212\u5206\n    pivot := q.partition(nums, left, right)\n    // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    q.quickSort(nums, left, pivot-1)\n    q.quickSort(nums, pivot+1, right)\n}\n
        quick_sort.swift
        /* \u5feb\u901f\u6392\u5e8f */\nfunc quickSort(nums: inout [Int], left: Int, right: Int) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    if left >= right {\n        return\n    }\n    // \u54e8\u5175\u5212\u5206\n    let pivot = partition(nums: &nums, left: left, right: right)\n    // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    quickSort(nums: &nums, left: left, right: pivot - 1)\n    quickSort(nums: &nums, left: pivot + 1, right: right)\n}\n
        quick_sort.js
        /* \u5feb\u901f\u6392\u5e8f */\nquickSort(nums, left, right) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    if (left >= right) return;\n    // \u54e8\u5175\u5212\u5206\n    const pivot = this.partition(nums, left, right);\n    // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    this.quickSort(nums, left, pivot - 1);\n    this.quickSort(nums, pivot + 1, right);\n}\n
        quick_sort.ts
        /* \u5feb\u901f\u6392\u5e8f */\nquickSort(nums: number[], left: number, right: number): void {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    if (left >= right) {\n        return;\n    }\n    // \u54e8\u5175\u5212\u5206\n    const pivot = this.partition(nums, left, right);\n    // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    this.quickSort(nums, left, pivot - 1);\n    this.quickSort(nums, pivot + 1, right);\n}\n
        quick_sort.dart
        /* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(List<int> nums, int left, int right) {\n  // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n  if (left >= right) return;\n  // \u54e8\u5175\u5212\u5206\n  int pivot = _partition(nums, left, right);\n  // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n  quickSort(nums, left, pivot - 1);\n  quickSort(nums, pivot + 1, right);\n}\n
        quick_sort.rs
        /* \u5feb\u901f\u6392\u5e8f */\npub fn quick_sort(left: i32, right: i32, nums: &mut [i32]) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    if left >= right {\n        return;\n    }\n    // \u54e8\u5175\u5212\u5206\n    let pivot = Self::partition(nums, left as usize, right as usize) as i32;\n    // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    Self::quick_sort(left, pivot - 1, nums);\n    Self::quick_sort(pivot + 1, right, nums);\n}\n
        quick_sort.c
        /* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(int nums[], int left, int right) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    if (left >= right) {\n        return;\n    }\n    // \u54e8\u5175\u5212\u5206\n    int pivot = partition(nums, left, right);\n    // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    quickSort(nums, left, pivot - 1);\n    quickSort(nums, pivot + 1, right);\n}\n
        quick_sort.kt
        /* \u5feb\u901f\u6392\u5e8f */\nfun quickSort(nums: IntArray, left: Int, right: Int) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n    if (left >= right) return\n    // \u54e8\u5175\u5212\u5206\n    val pivot = partition(nums, left, right)\n    // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    quickSort(nums, left, pivot - 1)\n    quickSort(nums, pivot + 1, right)\n}\n
        quick_sort.rb
        ### \u5feb\u901f\u6392\u5e8f\u7c7b ###\ndef quick_sort(nums, left, right)\n  # \u5b50\u6570\u7ec4\u957f\u5ea6\u4e0d\u4e3a 1 \u65f6\u9012\u5f52\n  if left < right\n    # \u54e8\u5175\u5212\u5206\n    pivot = partition(nums, left, right)\n    # \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n    quick_sort(nums, left, pivot - 1)\n    quick_sort(nums, pivot + 1, right)\n  end\n  nums\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/quick_sort/#1152","title":"11.5.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\)\u3001\u975e\u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u5728\u5e73\u5747\u60c5\u51b5\u4e0b\uff0c\u54e8\u5175\u5212\u5206\u7684\u9012\u5f52\u5c42\u6570\u4e3a \\(\\log n\\) \uff0c\u6bcf\u5c42\u4e2d\u7684\u603b\u5faa\u73af\u6570\u4e3a \\(n\\) \uff0c\u603b\u4f53\u4f7f\u7528 \\(O(n \\log n)\\) \u65f6\u95f4\u3002\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u64cd\u4f5c\u90fd\u5c06\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\u5212\u5206\u4e3a\u957f\u5ea6\u4e3a \\(0\\) \u548c \\(n - 1\\) \u7684\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u6b64\u65f6\u9012\u5f52\u5c42\u6570\u8fbe\u5230 \\(n\\) \uff0c\u6bcf\u5c42\u4e2d\u7684\u5faa\u73af\u6570\u4e3a \\(n\\) \uff0c\u603b\u4f53\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002
        • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\)\u3001\u539f\u5730\u6392\u5e8f\uff1a\u5728\u8f93\u5165\u6570\u7ec4\u5b8c\u5168\u5012\u5e8f\u7684\u60c5\u51b5\u4e0b\uff0c\u8fbe\u5230\u6700\u5dee\u9012\u5f52\u6df1\u5ea6 \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002\u6392\u5e8f\u64cd\u4f5c\u662f\u5728\u539f\u6570\u7ec4\u4e0a\u8fdb\u884c\u7684\uff0c\u672a\u501f\u52a9\u989d\u5916\u6570\u7ec4\u3002
        • \u975e\u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u54e8\u5175\u5212\u5206\u7684\u6700\u540e\u4e00\u6b65\uff0c\u57fa\u51c6\u6570\u53ef\u80fd\u4f1a\u88ab\u4ea4\u6362\u81f3\u76f8\u7b49\u5143\u7d20\u7684\u53f3\u4fa7\u3002
        "},{"location":"chapter_sorting/quick_sort/#1153","title":"11.5.3 \u00a0 \u5feb\u901f\u6392\u5e8f\u4e3a\u4ec0\u4e48\u5feb","text":"

        \u4ece\u540d\u79f0\u4e0a\u5c31\u80fd\u770b\u51fa\uff0c\u5feb\u901f\u6392\u5e8f\u5728\u6548\u7387\u65b9\u9762\u5e94\u8be5\u5177\u6709\u4e00\u5b9a\u7684\u4f18\u52bf\u3002\u5c3d\u7ba1\u5feb\u901f\u6392\u5e8f\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e0e\u201c\u5f52\u5e76\u6392\u5e8f\u201d\u548c\u201c\u5806\u6392\u5e8f\u201d\u76f8\u540c\uff0c\u4f46\u901a\u5e38\u5feb\u901f\u6392\u5e8f\u7684\u6548\u7387\u66f4\u9ad8\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002

        • \u51fa\u73b0\u6700\u5dee\u60c5\u51b5\u7684\u6982\u7387\u5f88\u4f4e\uff1a\u867d\u7136\u5feb\u901f\u6392\u5e8f\u7684\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u6ca1\u6709\u5f52\u5e76\u6392\u5e8f\u7a33\u5b9a\uff0c\u4f46\u5728\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5feb\u901f\u6392\u5e8f\u80fd\u5728 \\(O(n \\log n)\\) \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e0b\u8fd0\u884c\u3002
        • \u7f13\u5b58\u4f7f\u7528\u6548\u7387\u9ad8\uff1a\u5728\u6267\u884c\u54e8\u5175\u5212\u5206\u64cd\u4f5c\u65f6\uff0c\u7cfb\u7edf\u53ef\u5c06\u6574\u4e2a\u5b50\u6570\u7ec4\u52a0\u8f7d\u5230\u7f13\u5b58\uff0c\u56e0\u6b64\u8bbf\u95ee\u5143\u7d20\u7684\u6548\u7387\u8f83\u9ad8\u3002\u800c\u50cf\u201c\u5806\u6392\u5e8f\u201d\u8fd9\u7c7b\u7b97\u6cd5\u9700\u8981\u8df3\u8dc3\u5f0f\u8bbf\u95ee\u5143\u7d20\uff0c\u4ece\u800c\u7f3a\u4e4f\u8fd9\u4e00\u7279\u6027\u3002
        • \u590d\u6742\u5ea6\u7684\u5e38\u6570\u7cfb\u6570\u5c0f\uff1a\u5728\u4e0a\u8ff0\u4e09\u79cd\u7b97\u6cd5\u4e2d\uff0c\u5feb\u901f\u6392\u5e8f\u7684\u6bd4\u8f83\u3001\u8d4b\u503c\u3001\u4ea4\u6362\u7b49\u64cd\u4f5c\u7684\u603b\u6570\u91cf\u6700\u5c11\u3002\u8fd9\u4e0e\u201c\u63d2\u5165\u6392\u5e8f\u201d\u6bd4\u201c\u5192\u6ce1\u6392\u5e8f\u201d\u66f4\u5feb\u7684\u539f\u56e0\u7c7b\u4f3c\u3002
        "},{"location":"chapter_sorting/quick_sort/#1154","title":"11.5.4 \u00a0 \u57fa\u51c6\u6570\u4f18\u5316","text":"

        \u5feb\u901f\u6392\u5e8f\u5728\u67d0\u4e9b\u8f93\u5165\u4e0b\u7684\u65f6\u95f4\u6548\u7387\u53ef\u80fd\u964d\u4f4e\u3002\u4e3e\u4e00\u4e2a\u6781\u7aef\u4f8b\u5b50\uff0c\u5047\u8bbe\u8f93\u5165\u6570\u7ec4\u662f\u5b8c\u5168\u5012\u5e8f\u7684\uff0c\u7531\u4e8e\u6211\u4eec\u9009\u62e9\u6700\u5de6\u7aef\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\u6570\uff0c\u90a3\u4e48\u5728\u54e8\u5175\u5212\u5206\u5b8c\u6210\u540e\uff0c\u57fa\u51c6\u6570\u88ab\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u53f3\u7aef\uff0c\u5bfc\u81f4\u5de6\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a \\(n - 1\\)\u3001\u53f3\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a \\(0\\) \u3002\u5982\u6b64\u9012\u5f52\u4e0b\u53bb\uff0c\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u540e\u90fd\u6709\u4e00\u4e2a\u5b50\u6570\u7ec4\u7684\u957f\u5ea6\u4e3a \\(0\\) \uff0c\u5206\u6cbb\u7b56\u7565\u5931\u6548\uff0c\u5feb\u901f\u6392\u5e8f\u9000\u5316\u4e3a\u201c\u5192\u6ce1\u6392\u5e8f\u201d\u7684\u8fd1\u4f3c\u5f62\u5f0f\u3002

        \u4e3a\u4e86\u5c3d\u91cf\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f18\u5316\u54e8\u5175\u5212\u5206\u4e2d\u7684\u57fa\u51c6\u6570\u7684\u9009\u53d6\u7b56\u7565\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u968f\u673a\u9009\u53d6\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\u6570\u3002\u7136\u800c\uff0c\u5982\u679c\u8fd0\u6c14\u4e0d\u4f73\uff0c\u6bcf\u6b21\u90fd\u9009\u5230\u4e0d\u7406\u60f3\u7684\u57fa\u51c6\u6570\uff0c\u6548\u7387\u4ecd\u7136\u4e0d\u5c3d\u5982\u4eba\u610f\u3002

        \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u751f\u6210\u7684\u662f\u201c\u4f2a\u968f\u673a\u6570\u201d\u3002\u5982\u679c\u6211\u4eec\u9488\u5bf9\u4f2a\u968f\u673a\u6570\u5e8f\u5217\u6784\u5efa\u4e00\u4e2a\u7279\u5b9a\u7684\u6d4b\u8bd5\u6837\u4f8b\uff0c\u90a3\u4e48\u5feb\u901f\u6392\u5e8f\u7684\u6548\u7387\u4ecd\u7136\u53ef\u80fd\u52a3\u5316\u3002

        \u4e3a\u4e86\u8fdb\u4e00\u6b65\u6539\u8fdb\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6570\u7ec4\u4e2d\u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\uff08\u901a\u5e38\u4e3a\u6570\u7ec4\u7684\u9996\u3001\u5c3e\u3001\u4e2d\u70b9\u5143\u7d20\uff09\uff0c\u5e76\u5c06\u8fd9\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\u4f5c\u4e3a\u57fa\u51c6\u6570\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u57fa\u51c6\u6570\u201c\u65e2\u4e0d\u592a\u5c0f\u4e5f\u4e0d\u592a\u5927\u201d\u7684\u6982\u7387\u5c06\u5927\u5e45\u63d0\u5347\u3002\u5f53\u7136\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u9009\u53d6\u66f4\u591a\u5019\u9009\u5143\u7d20\uff0c\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u7b97\u6cd5\u7684\u7a33\u5065\u6027\u3002\u91c7\u7528\u8fd9\u79cd\u65b9\u6cd5\u540e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(n^2)\\) \u7684\u6982\u7387\u5927\u5927\u964d\u4f4e\u3002

        \u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
        def median_three(self, nums: list[int], left: int, mid: int, right: int) -> int:\n    \"\"\"\u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\"\"\"\n    l, m, r = nums[left], nums[mid], nums[right]\n    if (l <= m <= r) or (r <= m <= l):\n        return mid  # m \u5728 l \u548c r \u4e4b\u95f4\n    if (m <= l <= r) or (r <= l <= m):\n        return left  # l \u5728 m \u548c r \u4e4b\u95f4\n    return right\n\ndef partition(self, nums: list[int], left: int, right: int) -> int:\n    \"\"\"\u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09\"\"\"\n    # \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    med = self.median_three(nums, left, (left + right) // 2, right)\n    # \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n    nums[left], nums[med] = nums[med], nums[left]\n    # \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    i, j = left, right\n    while i < j:\n        while i < j and nums[j] >= nums[left]:\n            j -= 1  # \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while i < j and nums[i] <= nums[left]:\n            i += 1  # \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        # \u5143\u7d20\u4ea4\u6362\n        nums[i], nums[j] = nums[j], nums[i]\n    # \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    nums[i], nums[left] = nums[left], nums[i]\n    return i  # \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n
        quick_sort.cpp
        /* \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint medianThree(vector<int> &nums, int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m \u5728 l \u548c r \u4e4b\u95f4\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l \u5728 m \u548c r \u4e4b\u95f4\n    return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint partition(vector<int> &nums, int left, int right) {\n    // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n    int med = medianThree(nums, left, (left + right) / 2, right);\n    // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n    swap(nums[left], nums[med]);\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;                // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while (i < j && nums[i] <= nums[left])\n            i++;                // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        swap(nums[i], nums[j]); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    swap(nums[i], nums[left]);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i;                   // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.java
        /* \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint medianThree(int[] nums, int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m \u5728 l \u548c r \u4e4b\u95f4\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l \u5728 m \u548c r \u4e4b\u95f4\n    return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint partition(int[] nums, int left, int right) {\n    // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n    int med = medianThree(nums, left, (left + right) / 2, right);\n    // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n    swap(nums, left, med);\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    swap(nums, i, left);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i;             // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.cs
        /* \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint MedianThree(int[] nums, int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m \u5728 l \u548c r \u4e4b\u95f4\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l \u5728 m \u548c r \u4e4b\u95f4\n    return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint Partition(int[] nums, int left, int right) {\n    // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n    int med = MedianThree(nums, left, (left + right) / 2, right);\n    // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n    Swap(nums, left, med);\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        Swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    Swap(nums, i, left);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i;             // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.go
        /* \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nfunc (q *quickSortMedian) medianThree(nums []int, left, mid, right int) int {\n    l, m, r := nums[left], nums[mid], nums[right]\n    if (l <= m && m <= r) || (r <= m && m <= l) {\n        return mid // m \u5728 l \u548c r \u4e4b\u95f4\n    }\n    if (m <= l && l <= r) || (r <= l && l <= m) {\n        return left // l \u5728 m \u548c r \u4e4b\u95f4\n    }\n    return right\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09*/\nfunc (q *quickSortMedian) partition(nums []int, left, right int) int {\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    med := q.medianThree(nums, left, (left+right)/2, right)\n    // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n    nums[left], nums[med] = nums[med], nums[left]\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    i, j := left, right\n    for i < j {\n        for i < j && nums[j] >= nums[left] {\n            j-- //\u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        for i < j && nums[i] <= nums[left] {\n            i++ //\u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        //\u5143\u7d20\u4ea4\u6362\n        nums[i], nums[j] = nums[j], nums[i]\n    }\n    //\u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    nums[i], nums[left] = nums[left], nums[i]\n    return i //\u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.swift
        /* \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nfunc medianThree(nums: [Int], left: Int, mid: Int, right: Int) -> Int {\n    let l = nums[left]\n    let m = nums[mid]\n    let r = nums[right]\n    if (l <= m && m <= r) || (r <= m && m <= l) {\n        return mid // m \u5728 l \u548c r \u4e4b\u95f4\n    }\n    if (m <= l && l <= r) || (r <= l && l <= m) {\n        return left // l \u5728 m \u548c r \u4e4b\u95f4\n    }\n    return right\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nfunc partitionMedian(nums: inout [Int], left: Int, right: Int) -> Int {\n    // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n    let med = medianThree(nums: nums, left: left, mid: left + (right - left) / 2, right: right)\n    // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n    nums.swapAt(left, med)\n    return partition(nums: &nums, left: left, right: right)\n}\n
        quick_sort.js
        /* \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nmedianThree(nums, left, mid, right) {\n    let l = nums[left],\n        m = nums[mid],\n        r = nums[right];\n    // m \u5728 l \u548c r \u4e4b\u95f4\n    if ((l <= m && m <= r) || (r <= m && m <= l)) return mid;\n    // l \u5728 m \u548c r \u4e4b\u95f4\n    if ((m <= l && l <= r) || (r <= l && l <= m)) return left;\n    return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\npartition(nums, left, right) {\n    // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n    let med = this.medianThree(\n        nums,\n        left,\n        Math.floor((left + right) / 2),\n        right\n    );\n    // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n    this.swap(nums, left, med);\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    let i = left,\n        j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while (i < j && nums[i] <= nums[left]) i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        this.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    this.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.ts
        /* \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nmedianThree(\n    nums: number[],\n    left: number,\n    mid: number,\n    right: number\n): number {\n    let l = nums[left],\n        m = nums[mid],\n        r = nums[right];\n    // m \u5728 l \u548c r \u4e4b\u95f4\n    if ((l <= m && m <= r) || (r <= m && m <= l)) return mid;\n    // l \u5728 m \u548c r \u4e4b\u95f4\n    if ((m <= l && l <= r) || (r <= l && l <= m)) return left;\n    return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\npartition(nums: number[], left: number, right: number): number {\n    // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n    let med = this.medianThree(\n        nums,\n        left,\n        Math.floor((left + right) / 2),\n        right\n    );\n    // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n    this.swap(nums, left, med);\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    let i = left,\n        j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) {\n            j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        while (i < j && nums[i] <= nums[left]) {\n            i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        this.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    this.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.dart
        /* \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint _medianThree(List<int> nums, int left, int mid, int right) {\n  int l = nums[left], m = nums[mid], r = nums[right];\n  if ((l <= m && m <= r) || (r <= m && m <= l))\n    return mid; // m \u5728 l \u548c r \u4e4b\u95f4\n  if ((m <= l && l <= r) || (r <= l && l <= m))\n    return left; // l \u5728 m \u548c r \u4e4b\u95f4\n  return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint _partition(List<int> nums, int left, int right) {\n  // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n  int med = _medianThree(nums, left, (left + right) ~/ 2, right);\n  // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n  _swap(nums, left, med);\n  // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n  int i = left, j = right;\n  while (i < j) {\n    while (i < j && nums[j] >= nums[left]) j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n    while (i < j && nums[i] <= nums[left]) i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n    _swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n  }\n  _swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n  return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.rs
        /* \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nfn median_three(nums: &mut [i32], left: usize, mid: usize, right: usize) -> usize {\n    let (l, m, r) = (nums[left], nums[mid], nums[right]);\n    if (l <= m && m <= r) || (r <= m && m <= l) {\n        return mid; // m \u5728 l \u548c r \u4e4b\u95f4\n    }\n    if (m <= l && l <= r) || (r <= l && l <= m) {\n        return left; // l \u5728 m \u548c r \u4e4b\u95f4\n    }\n    right\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nfn partition(nums: &mut [i32], left: usize, right: usize) -> usize {\n    // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n    let med = Self::median_three(nums, left, (left + right) / 2, right);\n    // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n    nums.swap(left, med);\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    let (mut i, mut j) = (left, right);\n    while i < j {\n        while i < j && nums[j] >= nums[left] {\n            j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        while i < j && nums[i] <= nums[left] {\n            i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        }\n        nums.swap(i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    nums.swap(i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.c
        /* \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint medianThree(int nums[], int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m \u5728 l \u548c r \u4e4b\u95f4\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l \u5728 m \u548c r \u4e4b\u95f4\n    return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint partitionMedian(int nums[], int left, int right) {\n    // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n    int med = medianThree(nums, left, (left + right) / 2, right);\n    // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n    swap(nums, left, med);\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i;            // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.kt
        /* \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nfun medianThree(nums: IntArray, left: Int, mid: Int, right: Int): Int {\n    val l = nums[left]\n    val m = nums[mid]\n    val r = nums[right]\n    if ((m in l..r) || (m in r..l))\n        return mid  // m \u5728 l \u548c r \u4e4b\u95f4\n    if ((l in m..r) || (l in r..m))\n        return left // l \u5728 m \u548c r \u4e4b\u95f4\n    return right\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nfun partitionMedian(nums: IntArray, left: Int, right: Int): Int {\n    // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n    val med = medianThree(nums, left, (left + right) / 2, right)\n    // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n    swap(nums, left, med)\n    // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n    var i = left\n    var j = right\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--                      // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        while (i < j && nums[i] <= nums[left])\n            i++                      // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n        swap(nums, i, j)             // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n    }\n    swap(nums, i, left)              // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n    return i                         // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
        quick_sort.rb
        ### \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 ###\ndef median_three(nums, left, mid, right)\n  # \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n  _l, _m, _r = nums[left], nums[mid], nums[right]\n  # m \u5728 l \u548c r \u4e4b\u95f4\n  return mid if (_l <= _m && _m <= _r) || (_r <= _m && _m <= _l)\n  # l \u5728 m \u548c r \u4e4b\u95f4\n  return left if (_m <= _l && _l <= _r) || (_r <= _l && _l <= _m)\n  return right\nend\n\n### \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09###\ndef partition(nums, left, right)\n  ### \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n  med = median_three(nums, left, (left + right) / 2, right)\n  # \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u65ad\n  nums[left], nums[med] = nums[med], nums[left]\n  i, j = left, right\n  while i < j\n    while i < j && nums[j] >= nums[left]\n      j -= 1 # \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n    end\n    while i < j && nums[i] <= nums[left]\n      i += 1 # \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n    end\n    # \u5143\u7d20\u4ea4\u6362\n    nums[i], nums[j] = nums[j], nums[i]\n  end\n  # \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n  nums[i], nums[left] = nums[left], nums[i]\n  i # \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/quick_sort/#1155","title":"11.5.5 \u00a0 \u9012\u5f52\u6df1\u5ea6\u4f18\u5316","text":"

        \u5728\u67d0\u4e9b\u8f93\u5165\u4e0b\uff0c\u5feb\u901f\u6392\u5e8f\u53ef\u80fd\u5360\u7528\u7a7a\u95f4\u8f83\u591a\u3002\u4ee5\u5b8c\u5168\u6709\u5e8f\u7684\u8f93\u5165\u6570\u7ec4\u4e3a\u4f8b\uff0c\u8bbe\u9012\u5f52\u4e2d\u7684\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a \\(m\\) \uff0c\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u64cd\u4f5c\u90fd\u5c06\u4ea7\u751f\u957f\u5ea6\u4e3a \\(0\\) \u7684\u5de6\u5b50\u6570\u7ec4\u548c\u957f\u5ea6\u4e3a \\(m - 1\\) \u7684\u53f3\u5b50\u6570\u7ec4\uff0c\u8fd9\u610f\u5473\u7740\u6bcf\u4e00\u5c42\u9012\u5f52\u8c03\u7528\u51cf\u5c11\u7684\u95ee\u9898\u89c4\u6a21\u975e\u5e38\u5c0f\uff08\u53ea\u51cf\u5c11\u4e00\u4e2a\u5143\u7d20\uff09\uff0c\u9012\u5f52\u6811\u7684\u9ad8\u5ea6\u4f1a\u8fbe\u5230 \\(n - 1\\) \uff0c\u6b64\u65f6\u9700\u8981\u5360\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\u3002

        \u4e3a\u4e86\u9632\u6b62\u6808\u5e27\u7a7a\u95f4\u7684\u7d2f\u79ef\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6bcf\u8f6e\u54e8\u5175\u6392\u5e8f\u5b8c\u6210\u540e\uff0c\u6bd4\u8f83\u4e24\u4e2a\u5b50\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u4ec5\u5bf9\u8f83\u77ed\u7684\u5b50\u6570\u7ec4\u8fdb\u884c\u9012\u5f52\u3002\u7531\u4e8e\u8f83\u77ed\u5b50\u6570\u7ec4\u7684\u957f\u5ea6\u4e0d\u4f1a\u8d85\u8fc7 \\(n / 2\\) \uff0c\u56e0\u6b64\u8fd9\u79cd\u65b9\u6cd5\u80fd\u786e\u4fdd\u9012\u5f52\u6df1\u5ea6\u4e0d\u8d85\u8fc7 \\(\\log n\\) \uff0c\u4ece\u800c\u5c06\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(\\log n)\\) \u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
        def quick_sort(self, nums: list[int], left: int, right: int):\n    \"\"\"\u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09\"\"\"\n    # \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n    while left < right:\n        # \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n        pivot = self.partition(nums, left, right)\n        # \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n        if pivot - left < right - pivot:\n            self.quick_sort(nums, left, pivot - 1)  # \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n            left = pivot + 1  # \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n        else:\n            self.quick_sort(nums, pivot + 1, right)  # \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n            right = pivot - 1  # \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n
        quick_sort.cpp
        /* \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09 */\nvoid quickSort(vector<int> &nums, int left, int right) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n    while (left < right) {\n        // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n        int pivot = partition(nums, left, right);\n        // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n        if (pivot - left < right - pivot) {\n            quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n            left = pivot + 1;                 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n        } else {\n            quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n            right = pivot - 1;                 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n        }\n    }\n}\n
        quick_sort.java
        /* \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09 */\nvoid quickSort(int[] nums, int left, int right) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n    while (left < right) {\n        // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n        int pivot = partition(nums, left, right);\n        // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n        if (pivot - left < right - pivot) {\n            quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n            left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n        } else {\n            quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n            right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n        }\n    }\n}\n
        quick_sort.cs
        /* \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09 */\nvoid QuickSort(int[] nums, int left, int right) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n    while (left < right) {\n        // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n        int pivot = Partition(nums, left, right);\n        // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n        if (pivot - left < right - pivot) {\n            QuickSort(nums, left, pivot - 1);  // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n            left = pivot + 1;  // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n        } else {\n            QuickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n            right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n        }\n    }\n}\n
        quick_sort.go
        /* \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09*/\nfunc (q *quickSortTailCall) quickSort(nums []int, left, right int) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n    for left < right {\n        // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n        pivot := q.partition(nums, left, right)\n        // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n        if pivot-left < right-pivot {\n            q.quickSort(nums, left, pivot-1) // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n            left = pivot + 1                 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n        } else {\n            q.quickSort(nums, pivot+1, right) // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n            right = pivot - 1                 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n        }\n    }\n}\n
        quick_sort.swift
        /* \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09 */\nfunc quickSortTailCall(nums: inout [Int], left: Int, right: Int) {\n    var left = left\n    var right = right\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n    while left < right {\n        // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n        let pivot = partition(nums: &nums, left: left, right: right)\n        // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n        if (pivot - left) < (right - pivot) {\n            quickSortTailCall(nums: &nums, left: left, right: pivot - 1) // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n            left = pivot + 1 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n        } else {\n            quickSortTailCall(nums: &nums, left: pivot + 1, right: right) // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n            right = pivot - 1 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n        }\n    }\n}\n
        quick_sort.js
        /* \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09 */\nquickSort(nums, left, right) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n    while (left < right) {\n        // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n        let pivot = this.partition(nums, left, right);\n        // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n        if (pivot - left < right - pivot) {\n            this.quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n            left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n        } else {\n            this.quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n            right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n        }\n    }\n}\n
        quick_sort.ts
        /* \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09 */\nquickSort(nums: number[], left: number, right: number): void {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n    while (left < right) {\n        // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n        let pivot = this.partition(nums, left, right);\n        // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n        if (pivot - left < right - pivot) {\n            this.quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n            left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n        } else {\n            this.quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n            right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n        }\n    }\n}\n
        quick_sort.dart
        /* \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09 */\nvoid quickSort(List<int> nums, int left, int right) {\n  // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n  while (left < right) {\n    // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n    int pivot = _partition(nums, left, right);\n    // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n    if (pivot - left < right - pivot) {\n      quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n      left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n    } else {\n      quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n      right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n    }\n  }\n}\n
        quick_sort.rs
        /* \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09 */\npub fn quick_sort(mut left: i32, mut right: i32, nums: &mut [i32]) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n    while left < right {\n        // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n        let pivot = Self::partition(nums, left as usize, right as usize) as i32;\n        // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n        if pivot - left < right - pivot {\n            Self::quick_sort(left, pivot - 1, nums); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n            left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n        } else {\n            Self::quick_sort(pivot + 1, right, nums); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n            right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n        }\n    }\n}\n
        quick_sort.c
        /* \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09 */\nvoid quickSortTailCall(int nums[], int left, int right) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n    while (left < right) {\n        // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n        int pivot = partition(nums, left, right);\n        // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n        if (pivot - left < right - pivot) {\n            // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n            quickSortTailCall(nums, left, pivot - 1);\n            // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n            left = pivot + 1;\n        } else {\n            // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n            quickSortTailCall(nums, pivot + 1, right);\n            // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n            right = pivot - 1;\n        }\n    }\n}\n
        quick_sort.kt
        /* \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09 */\nfun quickSortTailCall(nums: IntArray, left: Int, right: Int) {\n    // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n    var l = left\n    var r = right\n    while (l < r) {\n        // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n        val pivot = partition(nums, l, r)\n        // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n        if (pivot - l < r - pivot) {\n            quickSort(nums, l, pivot - 1) // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n            l = pivot + 1 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n        } else {\n            quickSort(nums, pivot + 1, r) // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n            r = pivot - 1 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n        }\n    }\n}\n
        quick_sort.rb
        ### \u5feb\u901f\u6392\u5e8f\uff08\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff09###\ndef quick_sort(nums, left, right)\n  # \u5b50\u6570\u7ec4\u957f\u5ea6\u4e0d\u4e3a 1 \u65f6\u9012\u5f52\n  while left < right\n    # \u54e8\u5175\u5212\u5206\n    pivot = partition(nums, left, right)\n    # \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n    if pivot - left < right - pivot\n      quick_sort(nums, left, pivot - 1)\n      left = pivot + 1 # \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n    else\n      quick_sort(nums, pivot + 1, right)\n      right = pivot - 1 # \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n    end\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/radix_sort/","title":"11.10 \u00a0 \u57fa\u6570\u6392\u5e8f","text":"

        \u4e0a\u4e00\u8282\u4ecb\u7ecd\u4e86\u8ba1\u6570\u6392\u5e8f\uff0c\u5b83\u9002\u7528\u4e8e\u6570\u636e\u91cf \\(n\\) \u8f83\u5927\u4f46\u6570\u636e\u8303\u56f4 \\(m\\) \u8f83\u5c0f\u7684\u60c5\u51b5\u3002\u5047\u8bbe\u6211\u4eec\u9700\u8981\u5bf9 \\(n = 10^6\\) \u4e2a\u5b66\u53f7\u8fdb\u884c\u6392\u5e8f\uff0c\u800c\u5b66\u53f7\u662f\u4e00\u4e2a \\(8\\) \u4f4d\u6570\u5b57\uff0c\u8fd9\u610f\u5473\u7740\u6570\u636e\u8303\u56f4 \\(m = 10^8\\) \u975e\u5e38\u5927\uff0c\u4f7f\u7528\u8ba1\u6570\u6392\u5e8f\u9700\u8981\u5206\u914d\u5927\u91cf\u5185\u5b58\u7a7a\u95f4\uff0c\u800c\u57fa\u6570\u6392\u5e8f\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002

        \u57fa\u6570\u6392\u5e8f\uff08radix sort\uff09\u7684\u6838\u5fc3\u601d\u60f3\u4e0e\u8ba1\u6570\u6392\u5e8f\u4e00\u81f4\uff0c\u4e5f\u901a\u8fc7\u7edf\u8ba1\u4e2a\u6570\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u57fa\u6570\u6392\u5e8f\u5229\u7528\u6570\u5b57\u5404\u4f4d\u4e4b\u95f4\u7684\u9012\u8fdb\u5173\u7cfb\uff0c\u4f9d\u6b21\u5bf9\u6bcf\u4e00\u4f4d\u8fdb\u884c\u6392\u5e8f\uff0c\u4ece\u800c\u5f97\u5230\u6700\u7ec8\u7684\u6392\u5e8f\u7ed3\u679c\u3002

        "},{"location":"chapter_sorting/radix_sort/#11101","title":"11.10.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

        \u4ee5\u5b66\u53f7\u6570\u636e\u4e3a\u4f8b\uff0c\u5047\u8bbe\u6570\u5b57\u7684\u6700\u4f4e\u4f4d\u662f\u7b2c \\(1\\) \u4f4d\uff0c\u6700\u9ad8\u4f4d\u662f\u7b2c \\(8\\) \u4f4d\uff0c\u57fa\u6570\u6392\u5e8f\u7684\u6d41\u7a0b\u5982\u56fe 11-18 \u6240\u793a\u3002

        1. \u521d\u59cb\u5316\u4f4d\u6570 \\(k = 1\\) \u3002
        2. \u5bf9\u5b66\u53f7\u7684\u7b2c \\(k\\) \u4f4d\u6267\u884c\u201c\u8ba1\u6570\u6392\u5e8f\u201d\u3002\u5b8c\u6210\u540e\uff0c\u6570\u636e\u4f1a\u6839\u636e\u7b2c \\(k\\) \u4f4d\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\u3002
        3. \u5c06 \\(k\\) \u589e\u52a0 \\(1\\) \uff0c\u7136\u540e\u8fd4\u56de\u6b65\u9aa4 2. \u7ee7\u7eed\u8fed\u4ee3\uff0c\u76f4\u5230\u6240\u6709\u4f4d\u90fd\u6392\u5e8f\u5b8c\u6210\u540e\u7ed3\u675f\u3002

        \u56fe 11-18 \u00a0 \u57fa\u6570\u6392\u5e8f\u7b97\u6cd5\u6d41\u7a0b

        \u4e0b\u9762\u5256\u6790\u4ee3\u7801\u5b9e\u73b0\u3002\u5bf9\u4e8e\u4e00\u4e2a \\(d\\) \u8fdb\u5236\u7684\u6570\u5b57 \\(x\\) \uff0c\u8981\u83b7\u53d6\u5176\u7b2c \\(k\\) \u4f4d \\(x_k\\) \uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8ba1\u7b97\u516c\u5f0f\uff1a

        \\[ x_k = \\lfloor\\frac{x}{d^{k-1}}\\rfloor \\bmod d \\]

        \u5176\u4e2d \\(\\lfloor a \\rfloor\\) \u8868\u793a\u5bf9\u6d6e\u70b9\u6570 \\(a\\) \u5411\u4e0b\u53d6\u6574\uff0c\u800c \\(\\bmod \\: d\\) \u8868\u793a\u5bf9 \\(d\\) \u53d6\u6a21\uff08\u53d6\u4f59\uff09\u3002\u5bf9\u4e8e\u5b66\u53f7\u6570\u636e\uff0c\\(d = 10\\) \u4e14 \\(k \\in [1, 8]\\) \u3002

        \u6b64\u5916\uff0c\u6211\u4eec\u9700\u8981\u5c0f\u5e45\u6539\u52a8\u8ba1\u6570\u6392\u5e8f\u4ee3\u7801\uff0c\u4f7f\u4e4b\u53ef\u4ee5\u6839\u636e\u6570\u5b57\u7684\u7b2c \\(k\\) \u4f4d\u8fdb\u884c\u6392\u5e8f\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby radix_sort.py
        def digit(num: int, exp: int) -> int:\n    \"\"\"\u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1)\"\"\"\n    # \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n    return (num // exp) % 10\n\ndef counting_sort_digit(nums: list[int], exp: int):\n    \"\"\"\u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09\"\"\"\n    # \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n    counter = [0] * 10\n    n = len(nums)\n    # \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    for i in range(n):\n        d = digit(nums[i], exp)  # \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n        counter[d] += 1  # \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n    # \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n    for i in range(1, 10):\n        counter[i] += counter[i - 1]\n    # \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n    res = [0] * n\n    for i in range(n - 1, -1, -1):\n        d = digit(nums[i], exp)\n        j = counter[d] - 1  # \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n        res[j] = nums[i]  # \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n        counter[d] -= 1  # \u5c06 d \u7684\u6570\u91cf\u51cf 1\n    # \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n    for i in range(n):\n        nums[i] = res[i]\n\ndef radix_sort(nums: list[int]):\n    \"\"\"\u57fa\u6570\u6392\u5e8f\"\"\"\n    # \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n    m = max(nums)\n    # \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n    exp = 1\n    while exp <= m:\n        # \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n        # k = 1 -> exp = 1\n        # k = 2 -> exp = 10\n        # \u5373 exp = 10^(k-1)\n        counting_sort_digit(nums, exp)\n        exp *= 10\n
        radix_sort.cpp
        /* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int num, int exp) {\n    // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n    return (num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(vector<int> &nums, int exp) {\n    // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n    vector<int> counter(10, 0);\n    int n = nums.size();\n    // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    for (int i = 0; i < n; i++) {\n        int d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n        counter[d]++;                // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n    }\n    // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n    vector<int> res(n, 0);\n    for (int i = n - 1; i >= 0; i--) {\n        int d = digit(nums[i], exp);\n        int j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n        res[j] = nums[i];       // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n        counter[d]--;           // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (int i = 0; i < n; i++)\n        nums[i] = res[i];\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nvoid radixSort(vector<int> &nums) {\n    // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n    int m = *max_element(nums.begin(), nums.end());\n    // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n    for (int exp = 1; exp <= m; exp *= 10)\n        // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u5373 exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n}\n
        radix_sort.java
        /* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int num, int exp) {\n    // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n    return (num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(int[] nums, int exp) {\n    // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n    int[] counter = new int[10];\n    int n = nums.length;\n    // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    for (int i = 0; i < n; i++) {\n        int d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n        counter[d]++;                // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n    }\n    // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int d = digit(nums[i], exp);\n        int j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n        res[j] = nums[i];       // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n        counter[d]--;           // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (int i = 0; i < n; i++)\n        nums[i] = res[i];\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nvoid radixSort(int[] nums) {\n    // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n    int m = Integer.MIN_VALUE;\n    for (int num : nums)\n        if (num > m)\n            m = num;\n    // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n    for (int exp = 1; exp <= m; exp *= 10) {\n        // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u5373 exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n    }\n}\n
        radix_sort.cs
        /* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint Digit(int num, int exp) {\n    // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n    return (num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid CountingSortDigit(int[] nums, int exp) {\n    // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n    int[] counter = new int[10];\n    int n = nums.Length;\n    // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    for (int i = 0; i < n; i++) {\n        int d = Digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n        counter[d]++;                // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n    }\n    // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int d = Digit(nums[i], exp);\n        int j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n        res[j] = nums[i];       // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n        counter[d]--;           // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (int i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nvoid RadixSort(int[] nums) {\n    // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n    int m = int.MinValue;\n    foreach (int num in nums) {\n        if (num > m) m = num;\n    }\n    // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n    for (int exp = 1; exp <= m; exp *= 10) {\n        // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u5373 exp = 10^(k-1)\n        CountingSortDigit(nums, exp);\n    }\n}\n
        radix_sort.go
        /* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunc digit(num, exp int) int {\n    // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n    return (num / exp) % 10\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunc countingSortDigit(nums []int, exp int) {\n    // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n    counter := make([]int, 10)\n    n := len(nums)\n    // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    for i := 0; i < n; i++ {\n        d := digit(nums[i], exp) // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n        counter[d]++             // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n    }\n    // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n    for i := 1; i < 10; i++ {\n        counter[i] += counter[i-1]\n    }\n    // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n    res := make([]int, n)\n    for i := n - 1; i >= 0; i-- {\n        d := digit(nums[i], exp)\n        j := counter[d] - 1 // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n        res[j] = nums[i]    // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n        counter[d]--        // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n    for i := 0; i < n; i++ {\n        nums[i] = res[i]\n    }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nfunc radixSort(nums []int) {\n    // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n    max := math.MinInt\n    for _, num := range nums {\n        if num > max {\n            max = num\n        }\n    }\n    // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n    for exp := 1; max >= exp; exp *= 10 {\n        // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u5373 exp = 10^(k-1)\n        countingSortDigit(nums, exp)\n    }\n}\n
        radix_sort.swift
        /* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunc digit(num: Int, exp: Int) -> Int {\n    // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n    (num / exp) % 10\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunc countingSortDigit(nums: inout [Int], exp: Int) {\n    // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n    var counter = Array(repeating: 0, count: 10)\n    // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    for i in nums.indices {\n        let d = digit(num: nums[i], exp: exp) // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n        counter[d] += 1 // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n    }\n    // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n    for i in 1 ..< 10 {\n        counter[i] += counter[i - 1]\n    }\n    // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n    var res = Array(repeating: 0, count: nums.count)\n    for i in nums.indices.reversed() {\n        let d = digit(num: nums[i], exp: exp)\n        let j = counter[d] - 1 // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n        res[j] = nums[i] // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n        counter[d] -= 1 // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n    for i in nums.indices {\n        nums[i] = res[i]\n    }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nfunc radixSort(nums: inout [Int]) {\n    // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n    var m = Int.min\n    for num in nums {\n        if num > m {\n            m = num\n        }\n    }\n    // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n    for exp in sequence(first: 1, next: { m >= ($0 * 10) ? $0 * 10 : nil }) {\n        // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u5373 exp = 10^(k-1)\n        countingSortDigit(nums: &nums, exp: exp)\n    }\n}\n
        radix_sort.js
        /* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunction digit(num, exp) {\n    // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n    return Math.floor(num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunction countingSortDigit(nums, exp) {\n    // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n    const counter = new Array(10).fill(0);\n    const n = nums.length;\n    // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    for (let i = 0; i < n; i++) {\n        const d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n        counter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n    }\n    // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n    for (let i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n    const res = new Array(n).fill(0);\n    for (let i = n - 1; i >= 0; i--) {\n        const d = digit(nums[i], exp);\n        const j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n        res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n        counter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (let i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nfunction radixSort(nums) {\n    // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n    let m = Math.max(... nums);\n    // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n    for (let exp = 1; exp <= m; exp *= 10) {\n        // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u5373 exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n    }\n}\n
        radix_sort.ts
        /* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunction digit(num: number, exp: number): number {\n    // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n    return Math.floor(num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunction countingSortDigit(nums: number[], exp: number): void {\n    // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n    const counter = new Array(10).fill(0);\n    const n = nums.length;\n    // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    for (let i = 0; i < n; i++) {\n        const d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n        counter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n    }\n    // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n    for (let i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n    const res = new Array(n).fill(0);\n    for (let i = n - 1; i >= 0; i--) {\n        const d = digit(nums[i], exp);\n        const j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n        res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n        counter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (let i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nfunction radixSort(nums: number[]): void {\n    // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n    let m: number = Math.max(... nums);\n    // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n    for (let exp = 1; exp <= m; exp *= 10) {\n        // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u5373 exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n    }\n}\n
        radix_sort.dart
        /* \u83b7\u53d6\u5143\u7d20 _num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int _num, int exp) {\n  // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n  return (_num ~/ exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(List<int> nums, int exp) {\n  // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n  List<int> counter = List<int>.filled(10, 0);\n  int n = nums.length;\n  // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n  for (int i = 0; i < n; i++) {\n    int d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n    counter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n  }\n  // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n  for (int i = 1; i < 10; i++) {\n    counter[i] += counter[i - 1];\n  }\n  // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n  List<int> res = List<int>.filled(n, 0);\n  for (int i = n - 1; i >= 0; i--) {\n    int d = digit(nums[i], exp);\n    int j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n    res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n    counter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n  }\n  // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n  for (int i = 0; i < n; i++) nums[i] = res[i];\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nvoid radixSort(List<int> nums) {\n  // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n  // dart \u4e2d int \u7684\u957f\u5ea6\u662f 64 \u4f4d\u7684\n  int m = -1 << 63;\n  for (int _num in nums) if (_num > m) m = _num;\n  // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n  for (int exp = 1; exp <= m; exp *= 10)\n    // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n    // k = 1 -> exp = 1\n    // k = 2 -> exp = 10\n    // \u5373 exp = 10^(k-1)\n    countingSortDigit(nums, exp);\n}\n
        radix_sort.rs
        /* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfn digit(num: i32, exp: i32) -> usize {\n    // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n    return ((num / exp) % 10) as usize;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfn counting_sort_digit(nums: &mut [i32], exp: i32) {\n    // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n    let mut counter = [0; 10];\n    let n = nums.len();\n    // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    for i in 0..n {\n        let d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n        counter[d] += 1; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n    }\n    // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n    for i in 1..10 {\n        counter[i] += counter[i - 1];\n    }\n    // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n    let mut res = vec![0; n];\n    for i in (0..n).rev() {\n        let d = digit(nums[i], exp);\n        let j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n        res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n        counter[d] -= 1; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n    nums.copy_from_slice(&res);\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nfn radix_sort(nums: &mut [i32]) {\n    // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n    let m = *nums.into_iter().max().unwrap();\n    // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n    let mut exp = 1;\n    while exp <= m {\n        counting_sort_digit(nums, exp);\n        exp *= 10;\n    }\n}\n
        radix_sort.c
        /* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int num, int exp) {\n    // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n    return (num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(int nums[], int size, int exp) {\n    // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n    int *counter = (int *)malloc((sizeof(int) * 10));\n    memset(counter, 0, sizeof(int) * 10); // \u521d\u59cb\u5316\u4e3a 0 \u4ee5\u652f\u6301\u540e\u7eed\u5185\u5b58\u91ca\u653e\n    // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    for (int i = 0; i < size; i++) {\n        // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n        int d = digit(nums[i], exp);\n        // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n        counter[d]++;\n    }\n    // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n    int *res = (int *)malloc(sizeof(int) * size);\n    for (int i = size - 1; i >= 0; i--) {\n        int d = digit(nums[i], exp);\n        int j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n        res[j] = nums[i];       // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n        counter[d]--;           // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (int i = 0; i < size; i++) {\n        nums[i] = res[i];\n    }\n    // \u91ca\u653e\u5185\u5b58\n    free(res);\n    free(counter);\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nvoid radixSort(int nums[], int size) {\n    // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n    int max = INT32_MIN;\n    for (int i = 0; i < size; i++) {\n        if (nums[i] > max) {\n            max = nums[i];\n        }\n    }\n    // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n    for (int exp = 1; max >= exp; exp *= 10)\n        // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u5373 exp = 10^(k-1)\n        countingSortDigit(nums, size, exp);\n}\n
        radix_sort.kt
        /* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfun digit(num: Int, exp: Int): Int {\n    // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n    return (num / exp) % 10\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfun countingSortDigit(nums: IntArray, exp: Int) {\n    // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n    val counter = IntArray(10)\n    val n = nums.size\n    // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n    for (i in 0..<n) {\n        val d = digit(nums[i], exp) // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n        counter[d]++                // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n    }\n    // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n    for (i in 1..9) {\n        counter[i] += counter[i - 1]\n    }\n    // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n    val res = IntArray(n)\n    for (i in n - 1 downTo 0) {\n        val d = digit(nums[i], exp)\n        val j = counter[d] - 1 // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n        res[j] = nums[i]       // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n        counter[d]--           // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n    }\n    // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n    for (i in 0..<n)\n        nums[i] = res[i]\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nfun radixSort(nums: IntArray) {\n    // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n    var m = Int.MIN_VALUE\n    for (num in nums) if (num > m) m = num\n    var exp = 1\n    // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n    while (exp <= m) {\n        // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u5373 exp = 10^(k-1)\n        countingSortDigit(nums, exp)\n        exp *= 10\n    }\n}\n
        radix_sort.rb
        ### \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) ###\ndef digit(num, exp)\n  # \u8f6c\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n  (num / exp) % 10\nend\n\n### \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09###\ndef counting_sort_digit(nums, exp)\n  # \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n  counter = Array.new(10, 0)\n  n = nums.length\n  # \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n  for i in 0...n\n    d = digit(nums[i], exp) # \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n    counter[d] += 1 # \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n  end\n  # \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n  (1...10).each { |i| counter[i] += counter[i - 1] }\n  # \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n  res = Array.new(n, 0)\n  for i in (n - 1).downto(0)\n    d = digit(nums[i], exp)\n    j = counter[d] - 1 # \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n    res[j] = nums[i] # \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n    counter[d] -= 1 # \u5c06 d \u7684\u6570\u91cf\u51cf 1\n  end\n  # \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n  (0...n).each { |i| nums[i] = res[i] }\nend\n\n### \u57fa\u6570\u6392\u5e8f ###\ndef radix_sort(nums)\n  # \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n  m = nums.max\n  # \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n  exp = 1\n  while exp <= m\n    # \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n    # k = 1 -> exp = 1\n    # k = 2 -> exp = 10\n    # \u5373 exp = 10^(k-1)\n    counting_sort_digit(nums, exp)\n    exp *= 10\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u4e3a\u4ec0\u4e48\u4ece\u6700\u4f4e\u4f4d\u5f00\u59cb\u6392\u5e8f\uff1f

        \u5728\u8fde\u7eed\u7684\u6392\u5e8f\u8f6e\u6b21\u4e2d\uff0c\u540e\u4e00\u8f6e\u6392\u5e8f\u4f1a\u8986\u76d6\u524d\u4e00\u8f6e\u6392\u5e8f\u7684\u7ed3\u679c\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u5982\u679c\u7b2c\u4e00\u8f6e\u6392\u5e8f\u7ed3\u679c \\(a < b\\) \uff0c\u800c\u7b2c\u4e8c\u8f6e\u6392\u5e8f\u7ed3\u679c \\(a > b\\) \uff0c\u90a3\u4e48\u7b2c\u4e8c\u8f6e\u7684\u7ed3\u679c\u5c06\u53d6\u4ee3\u7b2c\u4e00\u8f6e\u7684\u7ed3\u679c\u3002\u7531\u4e8e\u6570\u5b57\u7684\u9ad8\u4f4d\u4f18\u5148\u7ea7\u9ad8\u4e8e\u4f4e\u4f4d\uff0c\u56e0\u6b64\u5e94\u8be5\u5148\u6392\u5e8f\u4f4e\u4f4d\u518d\u6392\u5e8f\u9ad8\u4f4d\u3002

        "},{"location":"chapter_sorting/radix_sort/#11102","title":"11.10.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"

        \u76f8\u8f83\u4e8e\u8ba1\u6570\u6392\u5e8f\uff0c\u57fa\u6570\u6392\u5e8f\u9002\u7528\u4e8e\u6570\u503c\u8303\u56f4\u8f83\u5927\u7684\u60c5\u51b5\uff0c\u4f46\u524d\u63d0\u662f\u6570\u636e\u5fc5\u987b\u53ef\u4ee5\u8868\u793a\u4e3a\u56fa\u5b9a\u4f4d\u6570\u7684\u683c\u5f0f\uff0c\u4e14\u4f4d\u6570\u4e0d\u80fd\u8fc7\u5927\u3002\u4f8b\u5982\uff0c\u6d6e\u70b9\u6570\u4e0d\u9002\u5408\u4f7f\u7528\u57fa\u6570\u6392\u5e8f\uff0c\u56e0\u4e3a\u5176\u4f4d\u6570 \\(k\\) \u8fc7\u5927\uff0c\u53ef\u80fd\u5bfc\u81f4\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(nk) \\gg O(n^2)\\) \u3002

        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nk)\\)\u3001\u975e\u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u8bbe\u6570\u636e\u91cf\u4e3a \\(n\\)\u3001\u6570\u636e\u4e3a \\(d\\) \u8fdb\u5236\u3001\u6700\u5927\u4f4d\u6570\u4e3a \\(k\\) \uff0c\u5219\u5bf9\u67d0\u4e00\u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\u4f7f\u7528 \\(O(n + d)\\) \u65f6\u95f4\uff0c\u6392\u5e8f\u6240\u6709 \\(k\\) \u4f4d\u4f7f\u7528 \\(O((n + d)k)\\) \u65f6\u95f4\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\\(d\\) \u548c \\(k\\) \u90fd\u76f8\u5bf9\u8f83\u5c0f\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u5411 \\(O(n)\\) \u3002
        • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n + d)\\)\u3001\u975e\u539f\u5730\u6392\u5e8f\uff1a\u4e0e\u8ba1\u6570\u6392\u5e8f\u76f8\u540c\uff0c\u57fa\u6570\u6392\u5e8f\u9700\u8981\u501f\u52a9\u957f\u5ea6\u4e3a \\(n\\) \u548c \\(d\\) \u7684\u6570\u7ec4 res \u548c counter \u3002
        • \u7a33\u5b9a\u6392\u5e8f\uff1a\u5f53\u8ba1\u6570\u6392\u5e8f\u7a33\u5b9a\u65f6\uff0c\u57fa\u6570\u6392\u5e8f\u4e5f\u7a33\u5b9a\uff1b\u5f53\u8ba1\u6570\u6392\u5e8f\u4e0d\u7a33\u5b9a\u65f6\uff0c\u57fa\u6570\u6392\u5e8f\u65e0\u6cd5\u4fdd\u8bc1\u5f97\u5230\u6b63\u786e\u7684\u6392\u5e8f\u7ed3\u679c\u3002
        "},{"location":"chapter_sorting/selection_sort/","title":"11.2 \u00a0 \u9009\u62e9\u6392\u5e8f","text":"

        \u9009\u62e9\u6392\u5e8f\uff08selection sort\uff09\u7684\u5de5\u4f5c\u539f\u7406\u975e\u5e38\u7b80\u5355\uff1a\u5f00\u542f\u4e00\u4e2a\u5faa\u73af\uff0c\u6bcf\u8f6e\u4ece\u672a\u6392\u5e8f\u533a\u95f4\u9009\u62e9\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u5c06\u5176\u653e\u5230\u5df2\u6392\u5e8f\u533a\u95f4\u7684\u672b\u5c3e\u3002

        \u8bbe\u6570\u7ec4\u7684\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u9009\u62e9\u6392\u5e8f\u7684\u7b97\u6cd5\u6d41\u7a0b\u5982\u56fe 11-2 \u6240\u793a\u3002

        1. \u521d\u59cb\u72b6\u6001\u4e0b\uff0c\u6240\u6709\u5143\u7d20\u672a\u6392\u5e8f\uff0c\u5373\u672a\u6392\u5e8f\uff08\u7d22\u5f15\uff09\u533a\u95f4\u4e3a \\([0, n-1]\\) \u3002
        2. \u9009\u53d6\u533a\u95f4 \\([0, n-1]\\) \u4e2d\u7684\u6700\u5c0f\u5143\u7d20\uff0c\u5c06\u5176\u4e0e\u7d22\u5f15 \\(0\\) \u5904\u7684\u5143\u7d20\u4ea4\u6362\u3002\u5b8c\u6210\u540e\uff0c\u6570\u7ec4\u524d 1 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002
        3. \u9009\u53d6\u533a\u95f4 \\([1, n-1]\\) \u4e2d\u7684\u6700\u5c0f\u5143\u7d20\uff0c\u5c06\u5176\u4e0e\u7d22\u5f15 \\(1\\) \u5904\u7684\u5143\u7d20\u4ea4\u6362\u3002\u5b8c\u6210\u540e\uff0c\u6570\u7ec4\u524d 2 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002
        4. \u4ee5\u6b64\u7c7b\u63a8\u3002\u7ecf\u8fc7 \\(n - 1\\) \u8f6e\u9009\u62e9\u4e0e\u4ea4\u6362\u540e\uff0c\u6570\u7ec4\u524d \\(n - 1\\) \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002
        5. \u4ec5\u5269\u7684\u4e00\u4e2a\u5143\u7d20\u5fc5\u5b9a\u662f\u6700\u5927\u5143\u7d20\uff0c\u65e0\u987b\u6392\u5e8f\uff0c\u56e0\u6b64\u6570\u7ec4\u6392\u5e8f\u5b8c\u6210\u3002
        <1><2><3><4><5><6><7><8><9><10><11>

        \u56fe 11-2 \u00a0 \u9009\u62e9\u6392\u5e8f\u6b65\u9aa4

        \u5728\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u7528 \\(k\\) \u6765\u8bb0\u5f55\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby selection_sort.py
        def selection_sort(nums: list[int]):\n    \"\"\"\u9009\u62e9\u6392\u5e8f\"\"\"\n    n = len(nums)\n    # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n    for i in range(n - 1):\n        # \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n        k = i\n        for j in range(i + 1, n):\n            if nums[j] < nums[k]:\n                k = j  # \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n        # \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n        nums[i], nums[k] = nums[k], nums[i]\n
        selection_sort.cpp
        /* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(vector<int> &nums) {\n    int n = nums.size();\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n        }\n        // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n        swap(nums[i], nums[k]);\n    }\n}\n
        selection_sort.java
        /* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(int[] nums) {\n    int n = nums.length;\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n        }\n        // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n        int temp = nums[i];\n        nums[i] = nums[k];\n        nums[k] = temp;\n    }\n}\n
        selection_sort.cs
        /* \u9009\u62e9\u6392\u5e8f */\nvoid SelectionSort(int[] nums) {\n    int n = nums.Length;\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n        }\n        // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n        (nums[k], nums[i]) = (nums[i], nums[k]);\n    }\n}\n
        selection_sort.go
        /* \u9009\u62e9\u6392\u5e8f */\nfunc selectionSort(nums []int) {\n    n := len(nums)\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n    for i := 0; i < n-1; i++ {\n        // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n        k := i\n        for j := i + 1; j < n; j++ {\n            if nums[j] < nums[k] {\n                // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n                k = j\n            }\n        }\n        // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n        nums[i], nums[k] = nums[k], nums[i]\n\n    }\n}\n
        selection_sort.swift
        /* \u9009\u62e9\u6392\u5e8f */\nfunc selectionSort(nums: inout [Int]) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n    for i in nums.indices.dropLast() {\n        // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n        var k = i\n        for j in nums.indices.dropFirst(i + 1) {\n            if nums[j] < nums[k] {\n                k = j // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n            }\n        }\n        // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n        nums.swapAt(i, k)\n    }\n}\n
        selection_sort.js
        /* \u9009\u62e9\u6392\u5e8f */\nfunction selectionSort(nums) {\n    let n = nums.length;\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n    for (let i = 0; i < n - 1; i++) {\n        // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n        let k = i;\n        for (let j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k]) {\n                k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n            }\n        }\n        // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n        [nums[i], nums[k]] = [nums[k], nums[i]];\n    }\n}\n
        selection_sort.ts
        /* \u9009\u62e9\u6392\u5e8f */\nfunction selectionSort(nums: number[]): void {\n    let n = nums.length;\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n    for (let i = 0; i < n - 1; i++) {\n        // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n        let k = i;\n        for (let j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k]) {\n                k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n            }\n        }\n        // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n        [nums[i], nums[k]] = [nums[k], nums[i]];\n    }\n}\n
        selection_sort.dart
        /* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(List<int> nums) {\n  int n = nums.length;\n  // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n  for (int i = 0; i < n - 1; i++) {\n    // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n    int k = i;\n    for (int j = i + 1; j < n; j++) {\n      if (nums[j] < nums[k]) k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n    }\n    // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n    int temp = nums[i];\n    nums[i] = nums[k];\n    nums[k] = temp;\n  }\n}\n
        selection_sort.rs
        /* \u9009\u62e9\u6392\u5e8f */\nfn selection_sort(nums: &mut [i32]) {\n    if nums.is_empty() {\n        return;\n    }\n    let n = nums.len();\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n    for i in 0..n - 1 {\n        // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n        let mut k = i;\n        for j in i + 1..n {\n            if nums[j] < nums[k] {\n                k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n            }\n        }\n        // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n        nums.swap(i, k);\n    }\n}\n
        selection_sort.c
        /* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(int nums[], int n) {\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n        }\n        // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n        int temp = nums[i];\n        nums[i] = nums[k];\n        nums[k] = temp;\n    }\n}\n
        selection_sort.kt
        /* \u9009\u62e9\u6392\u5e8f */\nfun selectionSort(nums: IntArray) {\n    val n = nums.size\n    // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n    for (i in 0..<n - 1) {\n        var k = i\n        // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n        for (j in i + 1..<n) {\n            if (nums[j] < nums[k])\n                k = j // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n        }\n        // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n        val temp = nums[i]\n        nums[i] = nums[k]\n        nums[k] = temp\n    }\n}\n
        selection_sort.rb
        ### \u9009\u62e9\u6392\u5e8f ###\ndef selection_sort(nums)\n  n = nums.length\n  # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n  for i in 0...(n - 1)\n    # \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n    k = i\n    for j in (i + 1)...n\n      if nums[j] < nums[k]\n        k = j # \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n      end\n    end\n    # \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n    nums[i], nums[k] = nums[k], nums[i]\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_sorting/selection_sort/#1121","title":"11.2.1 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\)\u3001\u975e\u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u5916\u5faa\u73af\u5171 \\(n - 1\\) \u8f6e\uff0c\u7b2c\u4e00\u8f6e\u7684\u672a\u6392\u5e8f\u533a\u95f4\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u6700\u540e\u4e00\u8f6e\u7684\u672a\u6392\u5e8f\u533a\u95f4\u957f\u5ea6\u4e3a \\(2\\) \uff0c\u5373\u5404\u8f6e\u5916\u5faa\u73af\u5206\u522b\u5305\u542b \\(n\\)\u3001\\(n - 1\\)\u3001\\(\\dots\\)\u3001\\(3\\)\u3001\\(2\\) \u8f6e\u5185\u5faa\u73af\uff0c\u6c42\u548c\u4e3a \\(\\frac{(n - 1)(n + 2)}{2}\\) \u3002
        • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\)\u3001\u539f\u5730\u6392\u5e8f\uff1a\u6307\u9488 \\(i\\) \u548c \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002
        • \u975e\u7a33\u5b9a\u6392\u5e8f\uff1a\u5982\u56fe 11-3 \u6240\u793a\uff0c\u5143\u7d20 nums[i] \u6709\u53ef\u80fd\u88ab\u4ea4\u6362\u81f3\u4e0e\u5176\u76f8\u7b49\u7684\u5143\u7d20\u7684\u53f3\u8fb9\uff0c\u5bfc\u81f4\u4e24\u8005\u7684\u76f8\u5bf9\u987a\u5e8f\u53d1\u751f\u6539\u53d8\u3002

        \u56fe 11-3 \u00a0 \u9009\u62e9\u6392\u5e8f\u975e\u7a33\u5b9a\u793a\u4f8b

        "},{"location":"chapter_sorting/sorting_algorithm/","title":"11.1 \u00a0 \u6392\u5e8f\u7b97\u6cd5","text":"

        \u6392\u5e8f\u7b97\u6cd5\uff08sorting algorithm\uff09\u7528\u4e8e\u5bf9\u4e00\u7ec4\u6570\u636e\u6309\u7167\u7279\u5b9a\u987a\u5e8f\u8fdb\u884c\u6392\u5217\u3002\u6392\u5e8f\u7b97\u6cd5\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u56e0\u4e3a\u6709\u5e8f\u6570\u636e\u901a\u5e38\u80fd\u591f\u88ab\u66f4\u9ad8\u6548\u5730\u67e5\u627e\u3001\u5206\u6790\u548c\u5904\u7406\u3002

        \u5982\u56fe 11-1 \u6240\u793a\uff0c\u6392\u5e8f\u7b97\u6cd5\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u662f\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u7b49\u3002\u6392\u5e8f\u7684\u5224\u65ad\u89c4\u5219\u53ef\u6839\u636e\u9700\u6c42\u8bbe\u5b9a\uff0c\u5982\u6570\u5b57\u5927\u5c0f\u3001\u5b57\u7b26 ASCII \u7801\u987a\u5e8f\u6216\u81ea\u5b9a\u4e49\u89c4\u5219\u3002

        \u56fe 11-1 \u00a0 \u6570\u636e\u7c7b\u578b\u548c\u5224\u65ad\u89c4\u5219\u793a\u4f8b

        "},{"location":"chapter_sorting/sorting_algorithm/#1111","title":"11.1.1 \u00a0 \u8bc4\u4ef7\u7ef4\u5ea6","text":"

        \u8fd0\u884c\u6548\u7387\uff1a\u6211\u4eec\u671f\u671b\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5c3d\u91cf\u4f4e\uff0c\u4e14\u603b\u4f53\u64cd\u4f5c\u6570\u91cf\u8f83\u5c11\uff08\u65f6\u95f4\u590d\u6742\u5ea6\u4e2d\u7684\u5e38\u6570\u9879\u53d8\u5c0f\uff09\u3002\u5bf9\u4e8e\u5927\u6570\u636e\u91cf\u7684\u60c5\u51b5\uff0c\u8fd0\u884c\u6548\u7387\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002

        \u5c31\u5730\u6027\uff1a\u987e\u540d\u601d\u4e49\uff0c\u539f\u5730\u6392\u5e8f\u901a\u8fc7\u5728\u539f\u6570\u7ec4\u4e0a\u76f4\u63a5\u64cd\u4f5c\u5b9e\u73b0\u6392\u5e8f\uff0c\u65e0\u987b\u501f\u52a9\u989d\u5916\u7684\u8f85\u52a9\u6570\u7ec4\uff0c\u4ece\u800c\u8282\u7701\u5185\u5b58\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u539f\u5730\u6392\u5e8f\u7684\u6570\u636e\u642c\u8fd0\u64cd\u4f5c\u8f83\u5c11\uff0c\u8fd0\u884c\u901f\u5ea6\u4e5f\u66f4\u5feb\u3002

        \u7a33\u5b9a\u6027\uff1a\u7a33\u5b9a\u6392\u5e8f\u5728\u5b8c\u6210\u6392\u5e8f\u540e\uff0c\u76f8\u7b49\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u76f8\u5bf9\u987a\u5e8f\u4e0d\u53d1\u751f\u6539\u53d8\u3002

        \u7a33\u5b9a\u6392\u5e8f\u662f\u591a\u7ea7\u6392\u5e8f\u573a\u666f\u7684\u5fc5\u8981\u6761\u4ef6\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b58\u50a8\u5b66\u751f\u4fe1\u606f\u7684\u8868\u683c\uff0c\u7b2c 1 \u5217\u548c\u7b2c 2 \u5217\u5206\u522b\u662f\u59d3\u540d\u548c\u5e74\u9f84\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u975e\u7a33\u5b9a\u6392\u5e8f\u53ef\u80fd\u5bfc\u81f4\u8f93\u5165\u6570\u636e\u7684\u6709\u5e8f\u6027\u4e27\u5931\uff1a

        # \u8f93\u5165\u6570\u636e\u662f\u6309\u7167\u59d3\u540d\u6392\u5e8f\u597d\u7684\n# (name, age)\n  ('A', 19)\n  ('B', 18)\n  ('C', 21)\n  ('D', 19)\n  ('E', 23)\n\n# \u5047\u8bbe\u4f7f\u7528\u975e\u7a33\u5b9a\u6392\u5e8f\u7b97\u6cd5\u6309\u5e74\u9f84\u6392\u5e8f\u5217\u8868\uff0c\n# \u7ed3\u679c\u4e2d ('D', 19) \u548c ('A', 19) \u7684\u76f8\u5bf9\u4f4d\u7f6e\u6539\u53d8\uff0c\n# \u8f93\u5165\u6570\u636e\u6309\u59d3\u540d\u6392\u5e8f\u7684\u6027\u8d28\u4e22\u5931\n  ('B', 18)\n  ('D', 19)\n  ('A', 19)\n  ('C', 21)\n  ('E', 23)\n

        \u81ea\u9002\u5e94\u6027\uff1a\u81ea\u9002\u5e94\u6392\u5e8f\u80fd\u591f\u5229\u7528\u8f93\u5165\u6570\u636e\u5df2\u6709\u7684\u987a\u5e8f\u4fe1\u606f\u6765\u51cf\u5c11\u8ba1\u7b97\u91cf\uff0c\u8fbe\u5230\u66f4\u4f18\u7684\u65f6\u95f4\u6548\u7387\u3002\u81ea\u9002\u5e94\u6392\u5e8f\u7b97\u6cd5\u7684\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4f18\u4e8e\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u3002

        \u662f\u5426\u57fa\u4e8e\u6bd4\u8f83\uff1a\u57fa\u4e8e\u6bd4\u8f83\u7684\u6392\u5e8f\u4f9d\u8d56\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff08\\(<\\)\u3001\\(=\\)\u3001\\(>\\)\uff09\u6765\u5224\u65ad\u5143\u7d20\u7684\u76f8\u5bf9\u987a\u5e8f\uff0c\u4ece\u800c\u6392\u5e8f\u6574\u4e2a\u6570\u7ec4\uff0c\u7406\u8bba\u6700\u4f18\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002\u800c\u975e\u6bd4\u8f83\u6392\u5e8f\u4e0d\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe \\(O(n)\\) \uff0c\u4f46\u5176\u901a\u7528\u6027\u76f8\u5bf9\u8f83\u5dee\u3002

        "},{"location":"chapter_sorting/sorting_algorithm/#1112","title":"11.1.2 \u00a0 \u7406\u60f3\u6392\u5e8f\u7b97\u6cd5","text":"

        \u8fd0\u884c\u5feb\u3001\u539f\u5730\u3001\u7a33\u5b9a\u3001\u81ea\u9002\u5e94\u3001\u901a\u7528\u6027\u597d\u3002\u663e\u7136\uff0c\u8fc4\u4eca\u4e3a\u6b62\u5c1a\u672a\u53d1\u73b0\u517c\u5177\u4ee5\u4e0a\u6240\u6709\u7279\u6027\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u56e0\u6b64\uff0c\u5728\u9009\u62e9\u6392\u5e8f\u7b97\u6cd5\u65f6\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u6570\u636e\u7279\u70b9\u548c\u95ee\u9898\u9700\u6c42\u6765\u51b3\u5b9a\u3002

        \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u5171\u540c\u5b66\u4e60\u5404\u79cd\u6392\u5e8f\u7b97\u6cd5\uff0c\u5e76\u57fa\u4e8e\u4e0a\u8ff0\u8bc4\u4ef7\u7ef4\u5ea6\u5bf9\u5404\u4e2a\u6392\u5e8f\u7b97\u6cd5\u7684\u4f18\u7f3a\u70b9\u8fdb\u884c\u5206\u6790\u3002

        "},{"location":"chapter_sorting/summary/","title":"11.11 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_sorting/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u5192\u6ce1\u6392\u5e8f\u901a\u8fc7\u4ea4\u6362\u76f8\u90bb\u5143\u7d20\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u901a\u8fc7\u6dfb\u52a0\u4e00\u4e2a\u6807\u5fd7\u4f4d\u6765\u5b9e\u73b0\u63d0\u524d\u8fd4\u56de\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5192\u6ce1\u6392\u5e8f\u7684\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u5230 \\(O(n)\\) \u3002
        • \u63d2\u5165\u6392\u5e8f\u6bcf\u8f6e\u5c06\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u5143\u7d20\u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4\u7684\u6b63\u786e\u4f4d\u7f6e\uff0c\u4ece\u800c\u5b8c\u6210\u6392\u5e8f\u3002\u867d\u7136\u63d2\u5165\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u4f46\u7531\u4e8e\u5355\u5143\u64cd\u4f5c\u76f8\u5bf9\u8f83\u5c11\uff0c\u56e0\u6b64\u5728\u5c0f\u6570\u636e\u91cf\u7684\u6392\u5e8f\u4efb\u52a1\u4e2d\u975e\u5e38\u53d7\u6b22\u8fce\u3002
        • \u5feb\u901f\u6392\u5e8f\u57fa\u4e8e\u54e8\u5175\u5212\u5206\u64cd\u4f5c\u5b9e\u73b0\u6392\u5e8f\u3002\u5728\u54e8\u5175\u5212\u5206\u4e2d\uff0c\u6709\u53ef\u80fd\u6bcf\u6b21\u90fd\u9009\u53d6\u5230\u6700\u5dee\u7684\u57fa\u51c6\u6570\uff0c\u5bfc\u81f4\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(n^2)\\) \u3002\u5f15\u5165\u4e2d\u4f4d\u6570\u57fa\u51c6\u6570\u6216\u968f\u673a\u57fa\u51c6\u6570\u53ef\u4ee5\u964d\u4f4e\u8fd9\u79cd\u52a3\u5316\u7684\u6982\u7387\u3002\u901a\u8fc7\u4f18\u5148\u9012\u5f52\u8f83\u77ed\u5b50\u533a\u95f4\uff0c\u53ef\u6709\u6548\u51cf\u5c0f\u9012\u5f52\u6df1\u5ea6\uff0c\u5c06\u7a7a\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u5230 \\(O(\\log n)\\) \u3002
        • \u5f52\u5e76\u6392\u5e8f\u5305\u62ec\u5212\u5206\u548c\u5408\u5e76\u4e24\u4e2a\u9636\u6bb5\uff0c\u5178\u578b\u5730\u4f53\u73b0\u4e86\u5206\u6cbb\u7b56\u7565\u3002\u5728\u5f52\u5e76\u6392\u5e8f\u4e2d\uff0c\u6392\u5e8f\u6570\u7ec4\u9700\u8981\u521b\u5efa\u8f85\u52a9\u6570\u7ec4\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff1b\u7136\u800c\u6392\u5e8f\u94fe\u8868\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f18\u5316\u81f3 \\(O(1)\\) \u3002
        • \u6876\u6392\u5e8f\u5305\u542b\u4e09\u4e2a\u6b65\u9aa4\uff1a\u6570\u636e\u5206\u6876\u3001\u6876\u5185\u6392\u5e8f\u548c\u5408\u5e76\u7ed3\u679c\u3002\u5b83\u540c\u6837\u4f53\u73b0\u4e86\u5206\u6cbb\u7b56\u7565\uff0c\u9002\u7528\u4e8e\u6570\u636e\u4f53\u91cf\u5f88\u5927\u7684\u60c5\u51b5\u3002\u6876\u6392\u5e8f\u7684\u5173\u952e\u5728\u4e8e\u5bf9\u6570\u636e\u8fdb\u884c\u5e73\u5747\u5206\u914d\u3002
        • \u8ba1\u6570\u6392\u5e8f\u662f\u6876\u6392\u5e8f\u7684\u4e00\u4e2a\u7279\u4f8b\uff0c\u5b83\u901a\u8fc7\u7edf\u8ba1\u6570\u636e\u51fa\u73b0\u7684\u6b21\u6570\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u8ba1\u6570\u6392\u5e8f\u9002\u7528\u4e8e\u6570\u636e\u91cf\u5927\u4f46\u6570\u636e\u8303\u56f4\u6709\u9650\u7684\u60c5\u51b5\uff0c\u5e76\u4e14\u8981\u6c42\u6570\u636e\u80fd\u591f\u8f6c\u6362\u4e3a\u6b63\u6574\u6570\u3002
        • \u57fa\u6570\u6392\u5e8f\u901a\u8fc7\u9010\u4f4d\u6392\u5e8f\u6765\u5b9e\u73b0\u6570\u636e\u6392\u5e8f\uff0c\u8981\u6c42\u6570\u636e\u80fd\u591f\u8868\u793a\u4e3a\u56fa\u5b9a\u4f4d\u6570\u7684\u6570\u5b57\u3002
        • \u603b\u7684\u6765\u8bf4\uff0c\u6211\u4eec\u5e0c\u671b\u627e\u5230\u4e00\u79cd\u6392\u5e8f\u7b97\u6cd5\uff0c\u5177\u6709\u9ad8\u6548\u7387\u3001\u7a33\u5b9a\u3001\u539f\u5730\u4ee5\u53ca\u81ea\u9002\u5e94\u6027\u7b49\u4f18\u70b9\u3002\u7136\u800c\uff0c\u6b63\u5982\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u4e00\u6837\uff0c\u6ca1\u6709\u4e00\u79cd\u6392\u5e8f\u7b97\u6cd5\u80fd\u591f\u540c\u65f6\u6ee1\u8db3\u6240\u6709\u8fd9\u4e9b\u6761\u4ef6\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u6570\u636e\u7684\u7279\u6027\u6765\u9009\u62e9\u5408\u9002\u7684\u6392\u5e8f\u7b97\u6cd5\u3002
        • \u56fe 11-19 \u5bf9\u6bd4\u4e86\u4e3b\u6d41\u6392\u5e8f\u7b97\u6cd5\u7684\u6548\u7387\u3001\u7a33\u5b9a\u6027\u3001\u5c31\u5730\u6027\u548c\u81ea\u9002\u5e94\u6027\u7b49\u3002

        \u56fe 11-19 \u00a0 \u6392\u5e8f\u7b97\u6cd5\u5bf9\u6bd4

        "},{"location":"chapter_sorting/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u6392\u5e8f\u7b97\u6cd5\u7a33\u5b9a\u6027\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u662f\u5fc5\u9700\u7684\uff1f

        \u5728\u73b0\u5b9e\u4e2d\uff0c\u6211\u4eec\u6709\u53ef\u80fd\u57fa\u4e8e\u5bf9\u8c61\u7684\u67d0\u4e2a\u5c5e\u6027\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u5b66\u751f\u6709\u59d3\u540d\u548c\u8eab\u9ad8\u4e24\u4e2a\u5c5e\u6027\uff0c\u6211\u4eec\u5e0c\u671b\u5b9e\u73b0\u4e00\u4e2a\u591a\u7ea7\u6392\u5e8f\uff1a\u5148\u6309\u7167\u59d3\u540d\u8fdb\u884c\u6392\u5e8f\uff0c\u5f97\u5230 (A, 180) (B, 185) (C, 170) (D, 170) \uff1b\u518d\u5bf9\u8eab\u9ad8\u8fdb\u884c\u6392\u5e8f\u3002\u7531\u4e8e\u6392\u5e8f\u7b97\u6cd5\u4e0d\u7a33\u5b9a\uff0c\u56e0\u6b64\u53ef\u80fd\u5f97\u5230 (D, 170) (C, 170) (A, 180) (B, 185) \u3002

        \u53ef\u4ee5\u53d1\u73b0\uff0c\u5b66\u751f D \u548c C \u7684\u4f4d\u7f6e\u53d1\u751f\u4e86\u4ea4\u6362\uff0c\u59d3\u540d\u7684\u6709\u5e8f\u6027\u88ab\u7834\u574f\u4e86\uff0c\u800c\u8fd9\u662f\u6211\u4eec\u4e0d\u5e0c\u671b\u770b\u5230\u7684\u3002

        Q\uff1a\u54e8\u5175\u5212\u5206\u4e2d\u201c\u4ece\u53f3\u5f80\u5de6\u67e5\u627e\u201d\u4e0e\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\u7684\u987a\u5e8f\u53ef\u4ee5\u4ea4\u6362\u5417\uff1f

        \u4e0d\u884c\uff0c\u5f53\u6211\u4eec\u4ee5\u6700\u5de6\u7aef\u5143\u7d20\u4e3a\u57fa\u51c6\u6570\u65f6\uff0c\u5fc5\u987b\u5148\u201c\u4ece\u53f3\u5f80\u5de6\u67e5\u627e\u201d\u518d\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\u3002\u8fd9\u4e2a\u7ed3\u8bba\u6709\u4e9b\u53cd\u76f4\u89c9\uff0c\u6211\u4eec\u6765\u5256\u6790\u4e00\u4e0b\u539f\u56e0\u3002

        \u54e8\u5175\u5212\u5206 partition() \u7684\u6700\u540e\u4e00\u6b65\u662f\u4ea4\u6362 nums[left] \u548c nums[i] \u3002\u5b8c\u6210\u4ea4\u6362\u540e\uff0c\u57fa\u51c6\u6570\u5de6\u8fb9\u7684\u5143\u7d20\u90fd <= \u57fa\u51c6\u6570\uff0c\u8fd9\u5c31\u8981\u6c42\u6700\u540e\u4e00\u6b65\u4ea4\u6362\u524d nums[left] >= nums[i] \u5fc5\u987b\u6210\u7acb\u3002\u5047\u8bbe\u6211\u4eec\u5148\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\uff0c\u90a3\u4e48\u5982\u679c\u627e\u4e0d\u5230\u6bd4\u57fa\u51c6\u6570\u66f4\u5927\u7684\u5143\u7d20\uff0c\u5219\u4f1a\u5728 i == j \u65f6\u8df3\u51fa\u5faa\u73af\uff0c\u6b64\u65f6\u53ef\u80fd nums[j] == nums[i] > nums[left]\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6b64\u65f6\u6700\u540e\u4e00\u6b65\u4ea4\u6362\u64cd\u4f5c\u4f1a\u628a\u4e00\u4e2a\u6bd4\u57fa\u51c6\u6570\u66f4\u5927\u7684\u5143\u7d20\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\uff0c\u5bfc\u81f4\u54e8\u5175\u5212\u5206\u5931\u8d25\u3002

        \u4e3e\u4e2a\u4f8b\u5b50\uff0c\u7ed9\u5b9a\u6570\u7ec4 [0, 0, 0, 0, 1] \uff0c\u5982\u679c\u5148\u201c\u4ece\u5de6\u5411\u53f3\u67e5\u627e\u201d\uff0c\u54e8\u5175\u5212\u5206\u540e\u6570\u7ec4\u4e3a [1, 0, 0, 0, 0] \uff0c\u8fd9\u4e2a\u7ed3\u679c\u662f\u4e0d\u6b63\u786e\u7684\u3002

        \u518d\u6df1\u5165\u601d\u8003\u4e00\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u9009\u62e9 nums[right] \u4e3a\u57fa\u51c6\u6570\uff0c\u90a3\u4e48\u6b63\u597d\u53cd\u8fc7\u6765\uff0c\u5fc5\u987b\u5148\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\u3002

        Q\uff1a\u5173\u4e8e\u5feb\u901f\u6392\u5e8f\u7684\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\uff0c\u4e3a\u4ec0\u4e48\u9009\u77ed\u7684\u6570\u7ec4\u80fd\u4fdd\u8bc1\u9012\u5f52\u6df1\u5ea6\u4e0d\u8d85\u8fc7 \\(\\log n\\) \uff1f

        \u9012\u5f52\u6df1\u5ea6\u5c31\u662f\u5f53\u524d\u672a\u8fd4\u56de\u7684\u9012\u5f52\u65b9\u6cd5\u7684\u6570\u91cf\u3002\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u6211\u4eec\u5c06\u539f\u6570\u7ec4\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\u3002\u5728\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\u540e\uff0c\u5411\u4e0b\u9012\u5f52\u7684\u5b50\u6570\u7ec4\u957f\u5ea6\u6700\u5927\u4e3a\u539f\u6570\u7ec4\u957f\u5ea6\u7684\u4e00\u534a\u3002\u5047\u8bbe\u6700\u5dee\u60c5\u51b5\uff0c\u4e00\u76f4\u4e3a\u4e00\u534a\u957f\u5ea6\uff0c\u90a3\u4e48\u6700\u7ec8\u7684\u9012\u5f52\u6df1\u5ea6\u5c31\u662f \\(\\log n\\) \u3002

        \u56de\u987e\u539f\u59cb\u7684\u5feb\u901f\u6392\u5e8f\uff0c\u6211\u4eec\u6709\u53ef\u80fd\u4f1a\u8fde\u7eed\u5730\u9012\u5f52\u957f\u5ea6\u8f83\u5927\u7684\u6570\u7ec4\uff0c\u6700\u5dee\u60c5\u51b5\u4e0b\u4e3a \\(n\\)\u3001\\(n - 1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \uff0c\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \u3002\u9012\u5f52\u6df1\u5ea6\u4f18\u5316\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u51fa\u73b0\u3002

        Q\uff1a\u5f53\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u76f8\u7b49\u65f6\uff0c\u5feb\u901f\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(n^2)\\) \u5417\uff1f\u8be5\u5982\u4f55\u5904\u7406\u8fd9\u79cd\u9000\u5316\u60c5\u51b5\uff1f

        \u662f\u7684\u3002\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u8003\u8651\u901a\u8fc7\u54e8\u5175\u5212\u5206\u5c06\u6570\u7ec4\u5212\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\uff1a\u5c0f\u4e8e\u3001\u7b49\u4e8e\u3001\u5927\u4e8e\u57fa\u51c6\u6570\u3002\u4ec5\u5411\u4e0b\u9012\u5f52\u5c0f\u4e8e\u548c\u5927\u4e8e\u7684\u4e24\u90e8\u5206\u3002\u5728\u8be5\u65b9\u6cd5\u4e0b\uff0c\u8f93\u5165\u5143\u7d20\u5168\u90e8\u76f8\u7b49\u7684\u6570\u7ec4\uff0c\u4ec5\u4e00\u8f6e\u54e8\u5175\u5212\u5206\u5373\u53ef\u5b8c\u6210\u6392\u5e8f\u3002

        Q\uff1a\u6876\u6392\u5e8f\u7684\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\u4ec0\u4e48\u662f \\(O(n^2)\\) \uff1f

        \u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u5143\u7d20\u88ab\u5206\u81f3\u540c\u4e00\u4e2a\u6876\u4e2d\u3002\u5982\u679c\u6211\u4eec\u91c7\u7528\u4e00\u4e2a \\(O(n^2)\\) \u7b97\u6cd5\u6765\u6392\u5e8f\u8fd9\u4e9b\u5143\u7d20\uff0c\u5219\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002

        "},{"location":"chapter_stack_and_queue/","title":"\u7b2c 5 \u7ae0 \u00a0 \u6808\u4e0e\u961f\u5217","text":"

        Abstract

        \u6808\u5982\u540c\u53e0\u732b\u732b\uff0c\u800c\u961f\u5217\u5c31\u50cf\u732b\u732b\u6392\u961f\u3002

        \u4e24\u8005\u5206\u522b\u4ee3\u8868\u5148\u5165\u540e\u51fa\u548c\u5148\u5165\u5148\u51fa\u7684\u903b\u8f91\u5173\u7cfb\u3002

        "},{"location":"chapter_stack_and_queue/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 5.1 \u00a0 \u6808
        • 5.2 \u00a0 \u961f\u5217
        • 5.3 \u00a0 \u53cc\u5411\u961f\u5217
        • 5.4 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_stack_and_queue/deque/","title":"5.3 \u00a0 \u53cc\u5411\u961f\u5217","text":"

        \u5728\u961f\u5217\u4e2d\uff0c\u6211\u4eec\u4ec5\u80fd\u5220\u9664\u5934\u90e8\u5143\u7d20\u6216\u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u3002\u5982\u56fe 5-7 \u6240\u793a\uff0c\u53cc\u5411\u961f\u5217\uff08double-ended queue\uff09\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\uff0c\u5141\u8bb8\u5728\u5934\u90e8\u548c\u5c3e\u90e8\u6267\u884c\u5143\u7d20\u7684\u6dfb\u52a0\u6216\u5220\u9664\u64cd\u4f5c\u3002

        \u56fe 5-7 \u00a0 \u53cc\u5411\u961f\u5217\u7684\u64cd\u4f5c

        "},{"location":"chapter_stack_and_queue/deque/#531","title":"5.3.1 \u00a0 \u53cc\u5411\u961f\u5217\u5e38\u7528\u64cd\u4f5c","text":"

        \u53cc\u5411\u961f\u5217\u7684\u5e38\u7528\u64cd\u4f5c\u5982\u8868 5-3 \u6240\u793a\uff0c\u5177\u4f53\u7684\u65b9\u6cd5\u540d\u79f0\u9700\u8981\u6839\u636e\u6240\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002

        \u8868 5-3 \u00a0 \u53cc\u5411\u961f\u5217\u64cd\u4f5c\u6548\u7387

        \u65b9\u6cd5\u540d \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push_first() \u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u961f\u9996 \\(O(1)\\) push_last() \u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u961f\u5c3e \\(O(1)\\) pop_first() \u5220\u9664\u961f\u9996\u5143\u7d20 \\(O(1)\\) pop_last() \u5220\u9664\u961f\u5c3e\u5143\u7d20 \\(O(1)\\) peek_first() \u8bbf\u95ee\u961f\u9996\u5143\u7d20 \\(O(1)\\) peek_last() \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 \\(O(1)\\)

        \u540c\u6837\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u4e2d\u5df2\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\u7c7b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby deque.py
        from collections import deque\n\n# \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217\ndeq: deque[int] = deque()\n\n# \u5143\u7d20\u5165\u961f\ndeq.append(2)      # \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeq.append(5)\ndeq.append(4)\ndeq.appendleft(3)  # \u6dfb\u52a0\u81f3\u961f\u9996\ndeq.appendleft(1)\n\n# \u8bbf\u95ee\u5143\u7d20\nfront: int = deq[0]  # \u961f\u9996\u5143\u7d20\nrear: int = deq[-1]  # \u961f\u5c3e\u5143\u7d20\n\n# \u5143\u7d20\u51fa\u961f\npop_front: int = deq.popleft()  # \u961f\u9996\u5143\u7d20\u51fa\u961f\npop_rear: int = deq.pop()       # \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n# \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\nsize: int = len(deq)\n\n# \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = len(deq) == 0\n
        deque.cpp
        /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\ndeque<int> deque;\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.push_back(2);   // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3);  // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.push_front(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nint front = deque.front(); // \u961f\u9996\u5143\u7d20\nint back = deque.back();   // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\ndeque.pop_front();  // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.pop_back();   // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.size();\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty = deque.empty();\n
        deque.java
        /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\nDeque<Integer> deque = new LinkedList<>();\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.offerLast(2);   // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.offerLast(5);\ndeque.offerLast(4);\ndeque.offerFirst(3);  // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.offerFirst(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.peekFirst();  // \u961f\u9996\u5143\u7d20\nint peekLast = deque.peekLast();    // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\nint popFirst = deque.pollFirst();  // \u961f\u9996\u5143\u7d20\u51fa\u961f\nint popLast = deque.pollLast();    // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.size();\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = deque.isEmpty();\n
        deque.cs
        /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 C# \u4e2d\uff0c\u5c06\u94fe\u8868 LinkedList \u770b\u4f5c\u53cc\u5411\u961f\u5217\u6765\u4f7f\u7528\nLinkedList<int> deque = new();\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.AddLast(2);   // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.AddLast(5);\ndeque.AddLast(4);\ndeque.AddFirst(3);  // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.AddFirst(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.First.Value;  // \u961f\u9996\u5143\u7d20\nint peekLast = deque.Last.Value;    // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\ndeque.RemoveFirst();  // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.RemoveLast();   // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.Count;\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = deque.Count == 0;\n
        deque_test.go
        /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 Go \u4e2d\uff0c\u5c06 list \u4f5c\u4e3a\u53cc\u5411\u961f\u5217\u4f7f\u7528\ndeque := list.New()\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.PushBack(2)      // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.PushBack(5)\ndeque.PushBack(4)\ndeque.PushFront(3)     // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.PushFront(1)\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nfront := deque.Front() // \u961f\u9996\u5143\u7d20\nrear := deque.Back()   // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\ndeque.Remove(front)    // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.Remove(rear)     // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nsize := deque.Len()\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty := deque.Len() == 0\n
        deque.swift
        /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// Swift \u6ca1\u6709\u5185\u7f6e\u7684\u53cc\u5411\u961f\u5217\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u53cc\u5411\u961f\u5217\u6765\u4f7f\u7528\nvar deque: [Int] = []\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.append(2) // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.append(5)\ndeque.append(4)\ndeque.insert(3, at: 0) // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.insert(1, at: 0)\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nlet peekFirst = deque.first! // \u961f\u9996\u5143\u7d20\nlet peekLast = deque.last! // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u4f7f\u7528 Array \u6a21\u62df\u65f6 popFirst \u7684\u590d\u6742\u5ea6\u4e3a O(n)\nlet popFirst = deque.removeFirst() // \u961f\u9996\u5143\u7d20\u51fa\u961f\nlet popLast = deque.removeLast() // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = deque.count\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = deque.isEmpty\n
        deque.js
        /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// JavaScript \u6ca1\u6709\u5185\u7f6e\u7684\u53cc\u7aef\u961f\u5217\uff0c\u53ea\u80fd\u628a Array \u5f53\u4f5c\u53cc\u7aef\u961f\u5217\u6765\u4f7f\u7528\nconst deque = [];\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cunshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\ndeque.unshift(3);\ndeque.unshift(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nconst peekFirst = deque[0];\nconst peekLast = deque[deque.length - 1];\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst popFront = deque.shift();\nconst popBack = deque.pop();\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = deque.length;\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst isEmpty = size === 0;\n
        deque.ts
        /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// TypeScript \u6ca1\u6709\u5185\u7f6e\u7684\u53cc\u7aef\u961f\u5217\uff0c\u53ea\u80fd\u628a Array \u5f53\u4f5c\u53cc\u7aef\u961f\u5217\u6765\u4f7f\u7528\nconst deque: number[] = [];\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cunshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\ndeque.unshift(3);\ndeque.unshift(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nconst peekFirst: number = deque[0];\nconst peekLast: number = deque[deque.length - 1];\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst popFront: number = deque.shift() as number;\nconst popBack: number = deque.pop() as number;\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nconst size: number = deque.length;\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst isEmpty: boolean = size === 0;\n
        deque.dart
        /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 Dart \u4e2d\uff0cQueue \u88ab\u5b9a\u4e49\u4e3a\u53cc\u5411\u961f\u5217\nQueue<int> deque = Queue<int>();\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.addLast(2);  // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.addLast(5);\ndeque.addLast(4);\ndeque.addFirst(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.addFirst(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.first; // \u961f\u9996\u5143\u7d20\nint peekLast = deque.last;   // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\nint popFirst = deque.removeFirst(); // \u961f\u9996\u5143\u7d20\u51fa\u961f\nint popLast = deque.removeLast();   // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.length;\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = deque.isEmpty;\n
        deque.rs
        /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\nlet mut deque: VecDeque<u32> = VecDeque::new();\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.push_back(2);  // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.push_front(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nif let Some(front) = deque.front() { // \u961f\u9996\u5143\u7d20\n}\nif let Some(rear) = deque.back() {   // \u961f\u5c3e\u5143\u7d20\n}\n\n/* \u5143\u7d20\u51fa\u961f */\nif let Some(pop_front) = deque.pop_front() { // \u961f\u9996\u5143\u7d20\u51fa\u961f\n}\nif let Some(pop_rear) = deque.pop_back() {   // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n}\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = deque.len();\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = deque.is_empty();\n
        deque.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e\u53cc\u5411\u961f\u5217\n
        deque.kt
        /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\nval deque = LinkedList<Int>()\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.offerLast(2)  // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.offerLast(5)\ndeque.offerLast(4)\ndeque.offerFirst(3) // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.offerFirst(1)\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nval peekFirst = deque.peekFirst() // \u961f\u9996\u5143\u7d20\nval peekLast = deque.peekLast()   // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\nval popFirst = deque.pollFirst() // \u961f\u9996\u5143\u7d20\u51fa\u961f\nval popLast = deque.pollLast()   // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nval size = deque.size\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nval isEmpty = deque.isEmpty()\n
        deque.rb
        # \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217\n# Ruby \u6ca1\u6709\u5185\u76f4\u7684\u53cc\u7aef\u961f\u5217\uff0c\u53ea\u80fd\u628a Array \u5f53\u4f5c\u53cc\u7aef\u961f\u5217\u6765\u4f7f\u7528\ndeque = []\n\n# \u5143\u7d20\u5982\u961f\ndeque << 2\ndeque << 5\ndeque << 4\n# \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cArray#unshift \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\ndeque.unshift(3)\ndeque.unshift(1)\n\n# \u8bbf\u95ee\u5143\u7d20\npeek_first = deque.first\npeek_last = deque.last\n\n# \u5143\u7d20\u51fa\u961f\n# \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0c Array#shift \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\npop_front = deque.shift\npop_back = deque.pop\n\n# \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\nsize = deque.length\n\n# \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\nis_empty = size.zero?\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_stack_and_queue/deque/#532","title":"5.3.2 \u00a0 \u53cc\u5411\u961f\u5217\u5b9e\u73b0 *","text":"

        \u53cc\u5411\u961f\u5217\u7684\u5b9e\u73b0\u4e0e\u961f\u5217\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u9009\u62e9\u94fe\u8868\u6216\u6570\u7ec4\u4f5c\u4e3a\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002

        "},{"location":"chapter_stack_and_queue/deque/#1","title":"1. \u00a0 \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u7684\u5b9e\u73b0","text":"

        \u56de\u987e\u4e0a\u4e00\u8282\u5185\u5bb9\uff0c\u6211\u4eec\u4f7f\u7528\u666e\u901a\u5355\u5411\u94fe\u8868\u6765\u5b9e\u73b0\u961f\u5217\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u5220\u9664\u5934\u8282\u70b9\uff08\u5bf9\u5e94\u51fa\u961f\u64cd\u4f5c\uff09\u548c\u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0\u65b0\u8282\u70b9\uff08\u5bf9\u5e94\u5165\u961f\u64cd\u4f5c\uff09\u3002

        \u5bf9\u4e8e\u53cc\u5411\u961f\u5217\u800c\u8a00\uff0c\u5934\u90e8\u548c\u5c3e\u90e8\u90fd\u53ef\u4ee5\u6267\u884c\u5165\u961f\u548c\u51fa\u961f\u64cd\u4f5c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u53cc\u5411\u961f\u5217\u9700\u8981\u5b9e\u73b0\u53e6\u4e00\u4e2a\u5bf9\u79f0\u65b9\u5411\u7684\u64cd\u4f5c\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u91c7\u7528\u201c\u53cc\u5411\u94fe\u8868\u201d\u4f5c\u4e3a\u53cc\u5411\u961f\u5217\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002

        \u5982\u56fe 5-8 \u6240\u793a\uff0c\u6211\u4eec\u5c06\u53cc\u5411\u94fe\u8868\u7684\u5934\u8282\u70b9\u548c\u5c3e\u8282\u70b9\u89c6\u4e3a\u53cc\u5411\u961f\u5217\u7684\u961f\u9996\u548c\u961f\u5c3e\uff0c\u540c\u65f6\u5b9e\u73b0\u5728\u4e24\u7aef\u6dfb\u52a0\u548c\u5220\u9664\u8282\u70b9\u7684\u529f\u80fd\u3002

        LinkedListDequepush_last()push_first()pop_last()pop_first()

        \u56fe 5-8 \u00a0 \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c

        \u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linkedlist_deque.py
        class ListNode:\n    \"\"\"\u53cc\u5411\u94fe\u8868\u8282\u70b9\"\"\"\n\n    def __init__(self, val: int):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        self.val: int = val\n        self.next: ListNode | None = None  # \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n        self.prev: ListNode | None = None  # \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\nclass LinkedListDeque:\n    \"\"\"\u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\"\"\"\n\n    def __init__(self):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        self._front: ListNode | None = None  # \u5934\u8282\u70b9 front\n        self._rear: ListNode | None = None  # \u5c3e\u8282\u70b9 rear\n        self._size: int = 0  # \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n    def size(self) -> int:\n        \"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n        return self._size == 0\n\n    def push(self, num: int, is_front: bool):\n        \"\"\"\u5165\u961f\u64cd\u4f5c\"\"\"\n        node = ListNode(num)\n        # \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n        if self.is_empty():\n            self._front = self._rear = node\n        # \u961f\u9996\u5165\u961f\u64cd\u4f5c\n        elif is_front:\n            # \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n            self._front.prev = node\n            node.next = self._front\n            self._front = node  # \u66f4\u65b0\u5934\u8282\u70b9\n        # \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n        else:\n            # \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n            self._rear.next = node\n            node.prev = self._rear\n            self._rear = node  # \u66f4\u65b0\u5c3e\u8282\u70b9\n        self._size += 1  # \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n\n    def push_first(self, num: int):\n        \"\"\"\u961f\u9996\u5165\u961f\"\"\"\n        self.push(num, True)\n\n    def push_last(self, num: int):\n        \"\"\"\u961f\u5c3e\u5165\u961f\"\"\"\n        self.push(num, False)\n\n    def pop(self, is_front: bool) -> int:\n        \"\"\"\u51fa\u961f\u64cd\u4f5c\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        # \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n        if is_front:\n            val: int = self._front.val  # \u6682\u5b58\u5934\u8282\u70b9\u503c\n            # \u5220\u9664\u5934\u8282\u70b9\n            fnext: ListNode | None = self._front.next\n            if fnext is not None:\n                fnext.prev = None\n                self._front.next = None\n            self._front = fnext  # \u66f4\u65b0\u5934\u8282\u70b9\n        # \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n        else:\n            val: int = self._rear.val  # \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n            # \u5220\u9664\u5c3e\u8282\u70b9\n            rprev: ListNode | None = self._rear.prev\n            if rprev is not None:\n                rprev.next = None\n                self._rear.prev = None\n            self._rear = rprev  # \u66f4\u65b0\u5c3e\u8282\u70b9\n        self._size -= 1  # \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n        return val\n\n    def pop_first(self) -> int:\n        \"\"\"\u961f\u9996\u51fa\u961f\"\"\"\n        return self.pop(True)\n\n    def pop_last(self) -> int:\n        \"\"\"\u961f\u5c3e\u51fa\u961f\"\"\"\n        return self.pop(False)\n\n    def peek_first(self) -> int:\n        \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        return self._front.val\n\n    def peek_last(self) -> int:\n        \"\"\"\u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        return self._rear.val\n\n    def to_array(self) -> list[int]:\n        \"\"\"\u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\"\"\"\n        node = self._front\n        res = [0] * self.size()\n        for i in range(self.size()):\n            res[i] = node.val\n            node = node.next\n        return res\n
        linkedlist_deque.cpp
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nstruct DoublyListNode {\n    int val;              // \u8282\u70b9\u503c\n    DoublyListNode *next; // \u540e\u7ee7\u8282\u70b9\u6307\u9488\n    DoublyListNode *prev; // \u524d\u9a71\u8282\u70b9\u6307\u9488\n    DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {\n    }\n};\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n  private:\n    DoublyListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n    int queSize = 0;              // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n  public:\n    /* \u6784\u9020\u65b9\u6cd5 */\n    LinkedListDeque() : front(nullptr), rear(nullptr) {\n    }\n\n    /* \u6790\u6784\u65b9\u6cd5 */\n    ~LinkedListDeque() {\n        // \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\n        DoublyListNode *pre, *cur = front;\n        while (cur != nullptr) {\n            pre = cur;\n            cur = cur->next;\n            delete pre;\n        }\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    int size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    bool isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u5165\u961f\u64cd\u4f5c */\n    void push(int num, bool isFront) {\n        DoublyListNode *node = new DoublyListNode(num);\n        // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n        if (isEmpty())\n            front = rear = node;\n        // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n        else if (isFront) {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n            front->prev = node;\n            node->next = front;\n            front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n        // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n        } else {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n            rear->next = node;\n            node->prev = rear;\n            rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n        queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    void pushFirst(int num) {\n        push(num, true);\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    void pushLast(int num) {\n        push(num, false);\n    }\n\n    /* \u51fa\u961f\u64cd\u4f5c */\n    int pop(bool isFront) {\n        if (isEmpty())\n            throw out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\n        int val;\n        // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n        if (isFront) {\n            val = front->val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n            // \u5220\u9664\u5934\u8282\u70b9\n            DoublyListNode *fNext = front->next;\n            if (fNext != nullptr) {\n                fNext->prev = nullptr;\n                front->next = nullptr;\n            }\n            delete front;\n            front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n        // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n        } else {\n            val = rear->val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n            // \u5220\u9664\u5c3e\u8282\u70b9\n            DoublyListNode *rPrev = rear->prev;\n            if (rPrev != nullptr) {\n                rPrev->next = nullptr;\n                rear->prev = nullptr;\n            }\n            delete rear;\n            rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n        queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n        return val;\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    int popFirst() {\n        return pop(true);\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    int popLast() {\n        return pop(false);\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    int peekFirst() {\n        if (isEmpty())\n            throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n        return front->val;\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    int peekLast() {\n        if (isEmpty())\n            throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n        return rear->val;\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    vector<int> toVector() {\n        DoublyListNode *node = front;\n        vector<int> res(size());\n        for (int i = 0; i < res.size(); i++) {\n            res[i] = node->val;\n            node = node->next;\n        }\n        return res;\n    }\n};\n
        linkedlist_deque.java
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n    int val; // \u8282\u70b9\u503c\n    ListNode next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n    ListNode prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\n    ListNode(int val) {\n        this.val = val;\n        prev = next = null;\n    }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n    private ListNode front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n    private int queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n    public LinkedListDeque() {\n        front = rear = null;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    public int size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u5165\u961f\u64cd\u4f5c */\n    private void push(int num, boolean isFront) {\n        ListNode node = new ListNode(num);\n        // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n        if (isEmpty())\n            front = rear = node;\n        // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n        else if (isFront) {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n            front.prev = node;\n            node.next = front;\n            front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n        // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n        } else {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n            rear.next = node;\n            node.prev = rear;\n            rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n        queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    public void pushFirst(int num) {\n        push(num, true);\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    public void pushLast(int num) {\n        push(num, false);\n    }\n\n    /* \u51fa\u961f\u64cd\u4f5c */\n    private int pop(boolean isFront) {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        int val;\n        // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n        if (isFront) {\n            val = front.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n            // \u5220\u9664\u5934\u8282\u70b9\n            ListNode fNext = front.next;\n            if (fNext != null) {\n                fNext.prev = null;\n                front.next = null;\n            }\n            front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n        // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n        } else {\n            val = rear.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n            // \u5220\u9664\u5c3e\u8282\u70b9\n            ListNode rPrev = rear.prev;\n            if (rPrev != null) {\n                rPrev.next = null;\n                rear.prev = null;\n            }\n            rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n        queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n        return val;\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    public int popFirst() {\n        return pop(true);\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    public int popLast() {\n        return pop(false);\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    public int peekFirst() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return front.val;\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    public int peekLast() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return rear.val;\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    public int[] toArray() {\n        ListNode node = front;\n        int[] res = new int[size()];\n        for (int i = 0; i < res.length; i++) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_deque.cs
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode(int val) {\n    public int val = val;       // \u8282\u70b9\u503c\n    public ListNode? next = null; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n    public ListNode? prev = null; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n    ListNode? front, rear; // \u5934\u8282\u70b9 front, \u5c3e\u8282\u70b9 rear\n    int queSize = 0;      // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n    public LinkedListDeque() {\n        front = null;\n        rear = null;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    public int Size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    public bool IsEmpty() {\n        return Size() == 0;\n    }\n\n    /* \u5165\u961f\u64cd\u4f5c */\n    void Push(int num, bool isFront) {\n        ListNode node = new(num);\n        // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n        if (IsEmpty()) {\n            front = node;\n            rear = node;\n        }\n        // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n        else if (isFront) {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n            front!.prev = node;\n            node.next = front;\n            front = node; // \u66f4\u65b0\u5934\u8282\u70b9                           \n        }\n        // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n        else {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n            rear!.next = node;\n            node.prev = rear;\n            rear = node;  // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n\n        queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    public void PushFirst(int num) {\n        Push(num, true);\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    public void PushLast(int num) {\n        Push(num, false);\n    }\n\n    /* \u51fa\u961f\u64cd\u4f5c */\n    int? Pop(bool isFront) {\n        if (IsEmpty())\n            throw new Exception();\n        int? val;\n        // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n        if (isFront) {\n            val = front?.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n            // \u5220\u9664\u5934\u8282\u70b9\n            ListNode? fNext = front?.next;\n            if (fNext != null) {\n                fNext.prev = null;\n                front!.next = null;\n            }\n            front = fNext;   // \u66f4\u65b0\u5934\u8282\u70b9\n        }\n        // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n        else {\n            val = rear?.val;  // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n            // \u5220\u9664\u5c3e\u8282\u70b9\n            ListNode? rPrev = rear?.prev;\n            if (rPrev != null) {\n                rPrev.next = null;\n                rear!.prev = null;\n            }\n            rear = rPrev;    // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n\n        queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n        return val;\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    public int? PopFirst() {\n        return Pop(true);\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    public int? PopLast() {\n        return Pop(false);\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    public int? PeekFirst() {\n        if (IsEmpty())\n            throw new Exception();\n        return front?.val;\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    public int? PeekLast() {\n        if (IsEmpty())\n            throw new Exception();\n        return rear?.val;\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    public int?[] ToArray() {\n        ListNode? node = front;\n        int?[] res = new int?[Size()];\n        for (int i = 0; i < res.Length; i++) {\n            res[i] = node?.val;\n            node = node?.next;\n        }\n\n        return res;\n    }\n}\n
        linkedlist_deque.go
        /* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntype linkedListDeque struct {\n    // \u4f7f\u7528\u5185\u7f6e\u5305 list\n    data *list.List\n}\n\n/* \u521d\u59cb\u5316\u53cc\u7aef\u961f\u5217 */\nfunc newLinkedListDeque() *linkedListDeque {\n    return &linkedListDeque{\n        data: list.New(),\n    }\n}\n\n/* \u961f\u9996\u5143\u7d20\u5165\u961f */\nfunc (s *linkedListDeque) pushFirst(value any) {\n    s.data.PushFront(value)\n}\n\n/* \u961f\u5c3e\u5143\u7d20\u5165\u961f */\nfunc (s *linkedListDeque) pushLast(value any) {\n    s.data.PushBack(value)\n}\n\n/* \u961f\u9996\u5143\u7d20\u51fa\u961f */\nfunc (s *linkedListDeque) popFirst() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Front()\n    s.data.Remove(e)\n    return e.Value\n}\n\n/* \u961f\u5c3e\u5143\u7d20\u51fa\u961f */\nfunc (s *linkedListDeque) popLast() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Back()\n    s.data.Remove(e)\n    return e.Value\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (s *linkedListDeque) peekFirst() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Front()\n    return e.Value\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc (s *linkedListDeque) peekLast() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Back()\n    return e.Value\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (s *linkedListDeque) size() int {\n    return s.data.Len()\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListDeque) isEmpty() bool {\n    return s.data.Len() == 0\n}\n\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListDeque) toList() *list.List {\n    return s.data\n}\n
        linkedlist_deque.swift
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n    var val: Int // \u8282\u70b9\u503c\n    var next: ListNode? // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n    weak var prev: ListNode? // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\n    init(val: Int) {\n        self.val = val\n    }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n    private var front: ListNode? // \u5934\u8282\u70b9 front\n    private var rear: ListNode? // \u5c3e\u8282\u70b9 rear\n    private var _size: Int // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n    init() {\n        _size = 0\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    func size() -> Int {\n        _size\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* \u5165\u961f\u64cd\u4f5c */\n    private func push(num: Int, isFront: Bool) {\n        let node = ListNode(val: num)\n        // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n        if isEmpty() {\n            front = node\n            rear = node\n        }\n        // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n        else if isFront {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n            front?.prev = node\n            node.next = front\n            front = node // \u66f4\u65b0\u5934\u8282\u70b9\n        }\n        // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n        else {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n            rear?.next = node\n            node.prev = rear\n            rear = node // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n        _size += 1 // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    func pushFirst(num: Int) {\n        push(num: num, isFront: true)\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    func pushLast(num: Int) {\n        push(num: num, isFront: false)\n    }\n\n    /* \u51fa\u961f\u64cd\u4f5c */\n    private func pop(isFront: Bool) -> Int {\n        if isEmpty() {\n            fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        }\n        let val: Int\n        // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n        if isFront {\n            val = front!.val // \u6682\u5b58\u5934\u8282\u70b9\u503c\n            // \u5220\u9664\u5934\u8282\u70b9\n            let fNext = front?.next\n            if fNext != nil {\n                fNext?.prev = nil\n                front?.next = nil\n            }\n            front = fNext // \u66f4\u65b0\u5934\u8282\u70b9\n        }\n        // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n        else {\n            val = rear!.val // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n            // \u5220\u9664\u5c3e\u8282\u70b9\n            let rPrev = rear?.prev\n            if rPrev != nil {\n                rPrev?.next = nil\n                rear?.prev = nil\n            }\n            rear = rPrev // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n        _size -= 1 // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n        return val\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    func popFirst() -> Int {\n        pop(isFront: true)\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    func popLast() -> Int {\n        pop(isFront: false)\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    func peekFirst() -> Int {\n        if isEmpty() {\n            fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        }\n        return front!.val\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    func peekLast() -> Int {\n        if isEmpty() {\n            fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        }\n        return rear!.val\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    func toArray() -> [Int] {\n        var node = front\n        var res = Array(repeating: 0, count: size())\n        for i in res.indices {\n            res[i] = node!.val\n            node = node?.next\n        }\n        return res\n    }\n}\n
        linkedlist_deque.js
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n    prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n    next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n    val; // \u8282\u70b9\u503c\n\n    constructor(val) {\n        this.val = val;\n        this.next = null;\n        this.prev = null;\n    }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n    #front; // \u5934\u8282\u70b9 front\n    #rear; // \u5c3e\u8282\u70b9 rear\n    #queSize; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n    constructor() {\n        this.#front = null;\n        this.#rear = null;\n        this.#queSize = 0;\n    }\n\n    /* \u961f\u5c3e\u5165\u961f\u64cd\u4f5c */\n    pushLast(val) {\n        const node = new ListNode(val);\n        // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n        if (this.#queSize === 0) {\n            this.#front = node;\n            this.#rear = node;\n        } else {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n            this.#rear.next = node;\n            node.prev = this.#rear;\n            this.#rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n        this.#queSize++;\n    }\n\n    /* \u961f\u9996\u5165\u961f\u64cd\u4f5c */\n    pushFirst(val) {\n        const node = new ListNode(val);\n        // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n        if (this.#queSize === 0) {\n            this.#front = node;\n            this.#rear = node;\n        } else {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n            this.#front.prev = node;\n            node.next = this.#front;\n            this.#front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n        }\n        this.#queSize++;\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c */\n    popLast() {\n        if (this.#queSize === 0) {\n            return null;\n        }\n        const value = this.#rear.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n        // \u5220\u9664\u5c3e\u8282\u70b9\n        let temp = this.#rear.prev;\n        if (temp !== null) {\n            temp.next = null;\n            this.#rear.prev = null;\n        }\n        this.#rear = temp; // \u66f4\u65b0\u5c3e\u8282\u70b9\n        this.#queSize--;\n        return value;\n    }\n\n    /* \u961f\u9996\u51fa\u961f\u64cd\u4f5c */\n    popFirst() {\n        if (this.#queSize === 0) {\n            return null;\n        }\n        const value = this.#front.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n        // \u5220\u9664\u5934\u8282\u70b9\n        let temp = this.#front.next;\n        if (temp !== null) {\n            temp.prev = null;\n            this.#front.next = null;\n        }\n        this.#front = temp; // \u66f4\u65b0\u5934\u8282\u70b9\n        this.#queSize--;\n        return value;\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    peekLast() {\n        return this.#queSize === 0 ? null : this.#rear.val;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    peekFirst() {\n        return this.#queSize === 0 ? null : this.#front.val;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    size() {\n        return this.#queSize;\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    isEmpty() {\n        return this.#queSize === 0;\n    }\n\n    /* \u6253\u5370\u53cc\u5411\u961f\u5217 */\n    print() {\n        const arr = [];\n        let temp = this.#front;\n        while (temp !== null) {\n            arr.push(temp.val);\n            temp = temp.next;\n        }\n        console.log('[' + arr.join(', ') + ']');\n    }\n}\n
        linkedlist_deque.ts
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n    prev: ListNode; // \u524d\u9a71\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n    next: ListNode; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n    val: number; // \u8282\u70b9\u503c\n\n    constructor(val: number) {\n        this.val = val;\n        this.next = null;\n        this.prev = null;\n    }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n    private front: ListNode; // \u5934\u8282\u70b9 front\n    private rear: ListNode; // \u5c3e\u8282\u70b9 rear\n    private queSize: number; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n    constructor() {\n        this.front = null;\n        this.rear = null;\n        this.queSize = 0;\n    }\n\n    /* \u961f\u5c3e\u5165\u961f\u64cd\u4f5c */\n    pushLast(val: number): void {\n        const node: ListNode = new ListNode(val);\n        // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n        if (this.queSize === 0) {\n            this.front = node;\n            this.rear = node;\n        } else {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n            this.rear.next = node;\n            node.prev = this.rear;\n            this.rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n        this.queSize++;\n    }\n\n    /* \u961f\u9996\u5165\u961f\u64cd\u4f5c */\n    pushFirst(val: number): void {\n        const node: ListNode = new ListNode(val);\n        // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n        if (this.queSize === 0) {\n            this.front = node;\n            this.rear = node;\n        } else {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n            this.front.prev = node;\n            node.next = this.front;\n            this.front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n        }\n        this.queSize++;\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c */\n    popLast(): number {\n        if (this.queSize === 0) {\n            return null;\n        }\n        const value: number = this.rear.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n        // \u5220\u9664\u5c3e\u8282\u70b9\n        let temp: ListNode = this.rear.prev;\n        if (temp !== null) {\n            temp.next = null;\n            this.rear.prev = null;\n        }\n        this.rear = temp; // \u66f4\u65b0\u5c3e\u8282\u70b9\n        this.queSize--;\n        return value;\n    }\n\n    /* \u961f\u9996\u51fa\u961f\u64cd\u4f5c */\n    popFirst(): number {\n        if (this.queSize === 0) {\n            return null;\n        }\n        const value: number = this.front.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n        // \u5220\u9664\u5934\u8282\u70b9\n        let temp: ListNode = this.front.next;\n        if (temp !== null) {\n            temp.prev = null;\n            this.front.next = null;\n        }\n        this.front = temp; // \u66f4\u65b0\u5934\u8282\u70b9\n        this.queSize--;\n        return value;\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    peekLast(): number {\n        return this.queSize === 0 ? null : this.rear.val;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    peekFirst(): number {\n        return this.queSize === 0 ? null : this.front.val;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    size(): number {\n        return this.queSize;\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    isEmpty(): boolean {\n        return this.queSize === 0;\n    }\n\n    /* \u6253\u5370\u53cc\u5411\u961f\u5217 */\n    print(): void {\n        const arr: number[] = [];\n        let temp: ListNode = this.front;\n        while (temp !== null) {\n            arr.push(temp.val);\n            temp = temp.next;\n        }\n        console.log('[' + arr.join(', ') + ']');\n    }\n}\n
        linkedlist_deque.dart
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n  int val; // \u8282\u70b9\u503c\n  ListNode? next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n  ListNode? prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\n  ListNode(this.val, {this.next, this.prev});\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u5bf9\u5217 */\nclass LinkedListDeque {\n  late ListNode? _front; // \u5934\u8282\u70b9 _front\n  late ListNode? _rear; // \u5c3e\u8282\u70b9 _rear\n  int _queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n  LinkedListDeque() {\n    this._front = null;\n    this._rear = null;\n  }\n\n  /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u957f\u5ea6 */\n  int size() {\n    return this._queSize;\n  }\n\n  /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n  bool isEmpty() {\n    return size() == 0;\n  }\n\n  /* \u5165\u961f\u64cd\u4f5c */\n  void push(int _num, bool isFront) {\n    final ListNode node = ListNode(_num);\n    if (isEmpty()) {\n      // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 _front \u548c _rear \u90fd\u6307\u5411 node\n      _front = _rear = node;\n    } else if (isFront) {\n      // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n      // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n      _front!.prev = node;\n      node.next = _front;\n      _front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n    } else {\n      // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n      // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n      _rear!.next = node;\n      node.prev = _rear;\n      _rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n    }\n    _queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n  }\n\n  /* \u961f\u9996\u5165\u961f */\n  void pushFirst(int _num) {\n    push(_num, true);\n  }\n\n  /* \u961f\u5c3e\u5165\u961f */\n  void pushLast(int _num) {\n    push(_num, false);\n  }\n\n  /* \u51fa\u961f\u64cd\u4f5c */\n  int? pop(bool isFront) {\n    // \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de null\n    if (isEmpty()) {\n      return null;\n    }\n    final int val;\n    if (isFront) {\n      // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n      val = _front!.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n      // \u5220\u9664\u5934\u8282\u70b9\n      ListNode? fNext = _front!.next;\n      if (fNext != null) {\n        fNext.prev = null;\n        _front!.next = null;\n      }\n      _front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n    } else {\n      // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n      val = _rear!.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n      // \u5220\u9664\u5c3e\u8282\u70b9\n      ListNode? rPrev = _rear!.prev;\n      if (rPrev != null) {\n        rPrev.next = null;\n        _rear!.prev = null;\n      }\n      _rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n    }\n    _queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n    return val;\n  }\n\n  /* \u961f\u9996\u51fa\u961f */\n  int? popFirst() {\n    return pop(true);\n  }\n\n  /* \u961f\u5c3e\u51fa\u961f */\n  int? popLast() {\n    return pop(false);\n  }\n\n  /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n  int? peekFirst() {\n    return _front?.val;\n  }\n\n  /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n  int? peekLast() {\n    return _rear?.val;\n  }\n\n  /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n  List<int> toArray() {\n    ListNode? node = _front;\n    final List<int> res = [];\n    for (int i = 0; i < _queSize; i++) {\n      res.add(node!.val);\n      node = node.next;\n    }\n    return res;\n  }\n}\n
        linkedlist_deque.rs
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\npub struct ListNode<T> {\n    pub val: T,                                 // \u8282\u70b9\u503c\n    pub next: Option<Rc<RefCell<ListNode<T>>>>, // \u540e\u7ee7\u8282\u70b9\u6307\u9488\n    pub prev: Option<Rc<RefCell<ListNode<T>>>>, // \u524d\u9a71\u8282\u70b9\u6307\u9488\n}\n\nimpl<T> ListNode<T> {\n    pub fn new(val: T) -> Rc<RefCell<ListNode<T>>> {\n        Rc::new(RefCell::new(ListNode {\n            val,\n            next: None,\n            prev: None,\n        }))\n    }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\n#[allow(dead_code)]\npub struct LinkedListDeque<T> {\n    front: Option<Rc<RefCell<ListNode<T>>>>, // \u5934\u8282\u70b9 front\n    rear: Option<Rc<RefCell<ListNode<T>>>>,  // \u5c3e\u8282\u70b9 rear\n    que_size: usize,                         // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n}\n\nimpl<T: Copy> LinkedListDeque<T> {\n    pub fn new() -> Self {\n        Self {\n            front: None,\n            rear: None,\n            que_size: 0,\n        }\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    pub fn size(&self) -> usize {\n        return self.que_size;\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    pub fn is_empty(&self) -> bool {\n        return self.que_size == 0;\n    }\n\n    /* \u5165\u961f\u64cd\u4f5c */\n    fn push(&mut self, num: T, is_front: bool) {\n        let node = ListNode::new(num);\n        // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n        if is_front {\n            match self.front.take() {\n                // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n                None => {\n                    self.rear = Some(node.clone());\n                    self.front = Some(node);\n                }\n                // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n                Some(old_front) => {\n                    old_front.borrow_mut().prev = Some(node.clone());\n                    node.borrow_mut().next = Some(old_front);\n                    self.front = Some(node); // \u66f4\u65b0\u5934\u8282\u70b9\n                }\n            }\n        }\n        // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n        else {\n            match self.rear.take() {\n                // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n                None => {\n                    self.front = Some(node.clone());\n                    self.rear = Some(node);\n                }\n                // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n                Some(old_rear) => {\n                    old_rear.borrow_mut().next = Some(node.clone());\n                    node.borrow_mut().prev = Some(old_rear);\n                    self.rear = Some(node); // \u66f4\u65b0\u5c3e\u8282\u70b9\n                }\n            }\n        }\n        self.que_size += 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    pub fn push_first(&mut self, num: T) {\n        self.push(num, true);\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    pub fn push_last(&mut self, num: T) {\n        self.push(num, false);\n    }\n\n    /* \u51fa\u961f\u64cd\u4f5c */\n    fn pop(&mut self, is_front: bool) -> Option<T> {\n        // \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de None\n        if self.is_empty() {\n            return None;\n        };\n        // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n        if is_front {\n            self.front.take().map(|old_front| {\n                match old_front.borrow_mut().next.take() {\n                    Some(new_front) => {\n                        new_front.borrow_mut().prev.take();\n                        self.front = Some(new_front); // \u66f4\u65b0\u5934\u8282\u70b9\n                    }\n                    None => {\n                        self.rear.take();\n                    }\n                }\n                self.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n                old_front.borrow().val\n            })\n        }\n        // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n        else {\n            self.rear.take().map(|old_rear| {\n                match old_rear.borrow_mut().prev.take() {\n                    Some(new_rear) => {\n                        new_rear.borrow_mut().next.take();\n                        self.rear = Some(new_rear); // \u66f4\u65b0\u5c3e\u8282\u70b9\n                    }\n                    None => {\n                        self.front.take();\n                    }\n                }\n                self.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n                old_rear.borrow().val\n            })\n        }\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    pub fn pop_first(&mut self) -> Option<T> {\n        return self.pop(true);\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    pub fn pop_last(&mut self) -> Option<T> {\n        return self.pop(false);\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    pub fn peek_first(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n        self.front.as_ref()\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    pub fn peek_last(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n        self.rear.as_ref()\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n        let mut res: Vec<T> = Vec::new();\n        fn recur<T: Copy>(cur: Option<&Rc<RefCell<ListNode<T>>>>, res: &mut Vec<T>) {\n            if let Some(cur) = cur {\n                res.push(cur.borrow().val);\n                recur(cur.borrow().next.as_ref(), res);\n            }\n        }\n\n        recur(head, &mut res);\n        res\n    }\n}\n
        linkedlist_deque.c
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\ntypedef struct DoublyListNode {\n    int val;                     // \u8282\u70b9\u503c\n    struct DoublyListNode *next; // \u540e\u7ee7\u8282\u70b9\n    struct DoublyListNode *prev; // \u524d\u9a71\u8282\u70b9\n} DoublyListNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nDoublyListNode *newDoublyListNode(int num) {\n    DoublyListNode *new = (DoublyListNode *)malloc(sizeof(DoublyListNode));\n    new->val = num;\n    new->next = NULL;\n    new->prev = NULL;\n    return new;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delDoublyListNode(DoublyListNode *node) {\n    free(node);\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntypedef struct {\n    DoublyListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n    int queSize;                  // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n} LinkedListDeque;\n\n/* \u6784\u9020\u51fd\u6570 */\nLinkedListDeque *newLinkedListDeque() {\n    LinkedListDeque *deque = (LinkedListDeque *)malloc(sizeof(LinkedListDeque));\n    deque->front = NULL;\n    deque->rear = NULL;\n    deque->queSize = 0;\n    return deque;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListdeque(LinkedListDeque *deque) {\n    // \u91ca\u653e\u6240\u6709\u8282\u70b9\n    for (int i = 0; i < deque->queSize && deque->front != NULL; i++) {\n        DoublyListNode *tmp = deque->front;\n        deque->front = deque->front->next;\n        free(tmp);\n    }\n    // \u91ca\u653e deque \u7ed3\u6784\u4f53\n    free(deque);\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(LinkedListDeque *deque) {\n    return deque->queSize;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(LinkedListDeque *deque) {\n    return (size(deque) == 0);\n}\n\n/* \u5165\u961f */\nvoid push(LinkedListDeque *deque, int num, bool isFront) {\n    DoublyListNode *node = newDoublyListNode(num);\n    // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411node\n    if (empty(deque)) {\n        deque->front = deque->rear = node;\n    }\n    // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n    else if (isFront) {\n        // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n        deque->front->prev = node;\n        node->next = deque->front;\n        deque->front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n    }\n    // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n    else {\n        // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n        deque->rear->next = node;\n        node->prev = deque->rear;\n        deque->rear = node;\n    }\n    deque->queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(LinkedListDeque *deque, int num) {\n    push(deque, num, true);\n}\n\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(LinkedListDeque *deque, int num) {\n    push(deque, num, false);\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst(LinkedListDeque *deque) {\n    assert(size(deque) && deque->front);\n    return deque->front->val;\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast(LinkedListDeque *deque) {\n    assert(size(deque) && deque->rear);\n    return deque->rear->val;\n}\n\n/* \u51fa\u961f */\nint pop(LinkedListDeque *deque, bool isFront) {\n    if (empty(deque))\n        return -1;\n    int val;\n    // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n    if (isFront) {\n        val = peekFirst(deque); // \u6682\u5b58\u5934\u8282\u70b9\u503c\n        DoublyListNode *fNext = deque->front->next;\n        if (fNext) {\n            fNext->prev = NULL;\n            deque->front->next = NULL;\n        }\n        delDoublyListNode(deque->front);\n        deque->front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n    }\n    // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n    else {\n        val = peekLast(deque); // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n        DoublyListNode *rPrev = deque->rear->prev;\n        if (rPrev) {\n            rPrev->next = NULL;\n            deque->rear->prev = NULL;\n        }\n        delDoublyListNode(deque->rear);\n        deque->rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n    }\n    deque->queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n    return val;\n}\n\n/* \u961f\u9996\u51fa\u961f */\nint popFirst(LinkedListDeque *deque) {\n    return pop(deque, true);\n}\n\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast(LinkedListDeque *deque) {\n    return pop(deque, false);\n}\n\n/* \u6253\u5370\u961f\u5217 */\nvoid printLinkedListDeque(LinkedListDeque *deque) {\n    int *arr = malloc(sizeof(int) * deque->queSize);\n    // \u62f7\u8d1d\u94fe\u8868\u4e2d\u7684\u6570\u636e\u5230\u6570\u7ec4\n    int i;\n    DoublyListNode *node;\n    for (i = 0, node = deque->front; i < deque->queSize; i++) {\n        arr[i] = node->val;\n        node = node->next;\n    }\n    printArray(arr, deque->queSize);\n    free(arr);\n}\n
        linkedlist_deque.kt
        /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode(var _val: Int) {\n    // \u8282\u70b9\u503c\n    var next: ListNode? = null // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n    var prev: ListNode? = null // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n    private var front: ListNode? = null // \u5934\u8282\u70b9 front\n    private var rear: ListNode? = null // \u5c3e\u8282\u70b9 rear\n    private var queSize: Int = 0 // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    fun size(): Int {\n        return queSize\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    fun isEmpty(): Boolean {\n        return size() == 0\n    }\n\n    /* \u5165\u961f\u64cd\u4f5c */\n    fun push(num: Int, isFront: Boolean) {\n        val node = ListNode(num)\n        // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n        if (isEmpty()) {\n            rear = node\n            front = rear\n            // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n        } else if (isFront) {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n            front?.prev = node\n            node.next = front\n            front = node // \u66f4\u65b0\u5934\u8282\u70b9\n            // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n        } else {\n            // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n            rear?.next = node\n            node.prev = rear\n            rear = node // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n        queSize++ // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    fun pushFirst(num: Int) {\n        push(num, true)\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    fun pushLast(num: Int) {\n        push(num, false)\n    }\n\n    /* \u51fa\u961f\u64cd\u4f5c */\n    fun pop(isFront: Boolean): Int {\n        if (isEmpty()) \n            throw IndexOutOfBoundsException()\n        val _val: Int\n        // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n        if (isFront) {\n            _val = front!!._val // \u6682\u5b58\u5934\u8282\u70b9\u503c\n            // \u5220\u9664\u5934\u8282\u70b9\n            val fNext = front!!.next\n            if (fNext != null) {\n                fNext.prev = null\n                front!!.next = null\n            }\n            front = fNext // \u66f4\u65b0\u5934\u8282\u70b9\n            // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n        } else {\n            _val = rear!!._val // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n            // \u5220\u9664\u5c3e\u8282\u70b9\n            val rPrev = rear!!.prev\n            if (rPrev != null) {\n                rPrev.next = null\n                rear!!.prev = null\n            }\n            rear = rPrev // \u66f4\u65b0\u5c3e\u8282\u70b9\n        }\n        queSize-- // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n        return _val\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    fun popFirst(): Int {\n        return pop(true)\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    fun popLast(): Int {\n        return pop(false)\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    fun peekFirst(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return front!!._val\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    fun peekLast(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return rear!!._val\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    fun toArray(): IntArray {\n        var node = front\n        val res = IntArray(size())\n        for (i in res.indices) {\n            res[i] = node!!._val\n            node = node.next\n        }\n        return res\n    }\n}\n
        linkedlist_deque.rb
        =begin\nFile: linkedlist_deque.rb\nCreated Time: 2024-04-06\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n### \u53cc\u5411\u94fe\u8868\u8282\u70b9\nclass ListNode\n  attr_accessor :val\n  attr_accessor :next # \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n  attr_accessor :prev # \u524d\u8eaf\u8282\u70b9\u5f15\u7528\n\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize(val)\n    @val = val\n  end\nend\n\n### \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 ###\nclass LinkedListDeque\n  ### \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 ###\n  attr_reader :size\n\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize\n    @front = nil  # \u5934\u8282\u70b9 front\n    @rear = nil   # \u5c3e\u8282\u70b9 rear\n    @size = 0     # \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n  end\n\n  ### \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a ###\n  def is_empty?\n    size.zero?\n  end\n\n  ### \u5165\u961f\u64cd\u4f5c ###\n  def push(num, is_front)\n    node = ListNode.new(num)\n    # \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c \u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n    if is_empty?\n      @front = @rear = node\n    # \u961f\u9996\u5165\u961f\u64cd\u4f5c\n    elsif is_front\n      # \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n      @front.prev = node\n      node.next = @front\n      @front = node # \u66f4\u65b0\u5934\u8282\u70b9\n    # \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n    else\n      # \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n      @rear.next = node\n      node.prev = @rear\n      @rear = node # \u66f4\u65b0\u5c3e\u8282\u70b9\n    end\n    @size += 1 # \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n  end\n\n  ### \u961f\u9996\u5165\u961f ###\n  def push_first(num)\n    push(num, true)\n  end\n\n  ### \u961f\u5c3e\u5165\u961f ###\n  def push_last(num)\n    push(num, false)\n  end\n\n  ### \u51fa\u961f\u64cd\u4f5c ###\n  def pop(is_front)\n    raise IndexError, '\u53cc\u5411\u961f\u5217\u4e3a\u7a7a' if is_empty?\n\n    # \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n    if is_front\n      val = @front.val # \u6682\u5b58\u5934\u8282\u70b9\u503c\n      # \u5220\u9664\u5934\u8282\u70b9\n      fnext = @front.next\n      unless fnext.nil?\n        fnext.prev = nil\n        @front.next = nil\n      end\n      @front = fnext # \u66f4\u65b0\u5934\u8282\u70b9\n    # \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n    else\n      val = @rear.val # \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n      # \u5220\u9664\u5c3e\u8282\u70b9\n      rprev = @rear.prev\n      unless rprev.nil?\n        rprev.next = nil\n        @rear.prev = nil\n      end\n      @rear = rprev # \u66f4\u65b0\u5c3e\u8282\u70b9\n    end\n    @size -= 1 # \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n\n    val\n  end\n\n  ### \u961f\u9996\u51fa\u961f ###\n  def pop_first\n    pop(true)\n  end\n\n  ### \u961f\u9996\u51fa\u961f ###\n  def pop_last\n    pop(false)\n  end\n\n  ### \u8bbf\u95ee\u961f\u9996\u5143\u7d20 ###\n  def peek_first\n    raise IndexError, '\u53cc\u5411\u961f\u5217\u4e3a\u7a7a' if is_empty?\n\n    @front.val\n  end\n\n  ### \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 ###\n  def peek_last\n    raise IndexError, '\u53cc\u5411\u961f\u5217\u4e3a\u7a7a' if is_empty?\n\n    @rear.val\n  end\n\n  ### \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 ###\n  def to_array\n    node = @front\n    res = Array.new(size, 0)\n    for i in 0...size\n      res[i] = node.val\n      node = node.next\n    end\n    res\n  end\nend\n
        "},{"location":"chapter_stack_and_queue/deque/#2","title":"2. \u00a0 \u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"

        \u5982\u56fe 5-9 \u6240\u793a\uff0c\u4e0e\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u961f\u5217\u7c7b\u4f3c\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u73af\u5f62\u6570\u7ec4\u6765\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u3002

        ArrayDequepush_last()push_first()pop_last()pop_first()

        \u56fe 5-9 \u00a0 \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c

        \u5728\u961f\u5217\u7684\u5b9e\u73b0\u57fa\u7840\u4e0a\uff0c\u4ec5\u9700\u589e\u52a0\u201c\u961f\u9996\u5165\u961f\u201d\u548c\u201c\u961f\u5c3e\u51fa\u961f\u201d\u7684\u65b9\u6cd5\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_deque.py
        class ArrayDeque:\n    \"\"\"\u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\"\"\"\n\n    def __init__(self, capacity: int):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        self._nums: list[int] = [0] * capacity\n        self._front: int = 0\n        self._size: int = 0\n\n    def capacity(self) -> int:\n        \"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf\"\"\"\n        return len(self._nums)\n\n    def size(self) -> int:\n        \"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n        return self._size == 0\n\n    def index(self, i: int) -> int:\n        \"\"\"\u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15\"\"\"\n        # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n        # \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n        # \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n        return (i + self.capacity()) % self.capacity()\n\n    def push_first(self, num: int):\n        \"\"\"\u961f\u9996\u5165\u961f\"\"\"\n        if self._size == self.capacity():\n            print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n            return\n        # \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n        # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n        self._front = self.index(self._front - 1)\n        # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n        self._nums[self._front] = num\n        self._size += 1\n\n    def push_last(self, num: int):\n        \"\"\"\u961f\u5c3e\u5165\u961f\"\"\"\n        if self._size == self.capacity():\n            print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n            return\n        # \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        rear = self.index(self._front + self._size)\n        # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        self._nums[rear] = num\n        self._size += 1\n\n    def pop_first(self) -> int:\n        \"\"\"\u961f\u9996\u51fa\u961f\"\"\"\n        num = self.peek_first()\n        # \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        self._front = self.index(self._front + 1)\n        self._size -= 1\n        return num\n\n    def pop_last(self) -> int:\n        \"\"\"\u961f\u5c3e\u51fa\u961f\"\"\"\n        num = self.peek_last()\n        self._size -= 1\n        return num\n\n    def peek_first(self) -> int:\n        \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        return self._nums[self._front]\n\n    def peek_last(self) -> int:\n        \"\"\"\u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        # \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n        last = self.index(self._front + self._size - 1)\n        return self._nums[last]\n\n    def to_array(self) -> list[int]:\n        \"\"\"\u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\"\"\"\n        # \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        res = []\n        for i in range(self._size):\n            res.append(self._nums[self.index(self._front + i)])\n        return res\n
        array_deque.cpp
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n  private:\n    vector<int> nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    int front;        // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    int queSize;      // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n  public:\n    /* \u6784\u9020\u65b9\u6cd5 */\n    ArrayDeque(int capacity) {\n        nums.resize(capacity);\n        front = queSize = 0;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n    int capacity() {\n        return nums.size();\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    int size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    bool isEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n    int index(int i) {\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n        return (i + capacity()) % capacity();\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    void pushFirst(int num) {\n        if (queSize == capacity()) {\n            cout << \"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\" << endl;\n            return;\n        }\n        // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n        front = index(front - 1);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n        nums[front] = num;\n        queSize++;\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    void pushLast(int num) {\n        if (queSize == capacity()) {\n            cout << \"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\" << endl;\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        int rear = index(front + queSize);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    int popFirst() {\n        int num = peekFirst();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        front = index(front + 1);\n        queSize--;\n        return num;\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    int popLast() {\n        int num = peekLast();\n        queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    int peekFirst() {\n        if (isEmpty())\n            throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n        return nums[front];\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    int peekLast() {\n        if (isEmpty())\n            throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n        // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n        int last = index(front + queSize - 1);\n        return nums[last];\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    vector<int> toVector() {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        vector<int> res(queSize);\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[index(j)];\n        }\n        return res;\n    }\n};\n
        array_deque.java
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n    private int[] nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    private int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    private int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public ArrayDeque(int capacity) {\n        this.nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n    public int capacity() {\n        return nums.length;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    public int size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    public boolean isEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n    private int index(int i) {\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n        return (i + capacity()) % capacity();\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    public void pushFirst(int num) {\n        if (queSize == capacity()) {\n            System.out.println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n            return;\n        }\n        // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n        front = index(front - 1);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n        nums[front] = num;\n        queSize++;\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    public void pushLast(int num) {\n        if (queSize == capacity()) {\n            System.out.println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        int rear = index(front + queSize);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    public int popFirst() {\n        int num = peekFirst();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        front = index(front + 1);\n        queSize--;\n        return num;\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    public int popLast() {\n        int num = peekLast();\n        queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    public int peekFirst() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return nums[front];\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    public int peekLast() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n        int last = index(front + queSize - 1);\n        return nums[last];\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    public int[] toArray() {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[index(j)];\n        }\n        return res;\n    }\n}\n
        array_deque.cs
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n    int[] nums;  // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    int front;   // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public ArrayDeque(int capacity) {\n        nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n    int Capacity() {\n        return nums.Length;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    public int Size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    public bool IsEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n    int Index(int i) {\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n        return (i + Capacity()) % Capacity();\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    public void PushFirst(int num) {\n        if (queSize == Capacity()) {\n            Console.WriteLine(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n            return;\n        }\n        // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n        front = Index(front - 1);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n        nums[front] = num;\n        queSize++;\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    public void PushLast(int num) {\n        if (queSize == Capacity()) {\n            Console.WriteLine(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        int rear = Index(front + queSize);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    public int PopFirst() {\n        int num = PeekFirst();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        front = Index(front + 1);\n        queSize--;\n        return num;\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    public int PopLast() {\n        int num = PeekLast();\n        queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    public int PeekFirst() {\n        if (IsEmpty()) {\n            throw new InvalidOperationException();\n        }\n        return nums[front];\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    public int PeekLast() {\n        if (IsEmpty()) {\n            throw new InvalidOperationException();\n        }\n        // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n        int last = Index(front + queSize - 1);\n        return nums[last];\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    public int[] ToArray() {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[Index(j)];\n        }\n        return res;\n    }\n}\n
        array_deque.go
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntype arrayDeque struct {\n    nums        []int // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    front       int   // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    queSize     int   // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n    queCapacity int   // \u961f\u5217\u5bb9\u91cf\uff08\u5373\u6700\u5927\u5bb9\u7eb3\u5143\u7d20\u6570\u91cf\uff09\n}\n\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newArrayDeque(queCapacity int) *arrayDeque {\n    return &arrayDeque{\n        nums:        make([]int, queCapacity),\n        queCapacity: queCapacity,\n        front:       0,\n        queSize:     0,\n    }\n}\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (q *arrayDeque) size() int {\n    return q.queSize\n}\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (q *arrayDeque) isEmpty() bool {\n    return q.queSize == 0\n}\n\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nfunc (q *arrayDeque) index(i int) int {\n    // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n    // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n    // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n    return (i + q.queCapacity) % q.queCapacity\n}\n\n/* \u961f\u9996\u5165\u961f */\nfunc (q *arrayDeque) pushFirst(num int) {\n    if q.queSize == q.queCapacity {\n        fmt.Println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n        return\n    }\n    // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n    // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n    q.front = q.index(q.front - 1)\n    // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n    q.nums[q.front] = num\n    q.queSize++\n}\n\n/* \u961f\u5c3e\u5165\u961f */\nfunc (q *arrayDeque) pushLast(num int) {\n    if q.queSize == q.queCapacity {\n        fmt.Println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n        return\n    }\n    // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n    rear := q.index(q.front + q.queSize)\n    // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n    q.nums[rear] = num\n    q.queSize++\n}\n\n/* \u961f\u9996\u51fa\u961f */\nfunc (q *arrayDeque) popFirst() any {\n    num := q.peekFirst()\n    if num == nil {\n        return nil\n    }\n    // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    q.front = q.index(q.front + 1)\n    q.queSize--\n    return num\n}\n\n/* \u961f\u5c3e\u51fa\u961f */\nfunc (q *arrayDeque) popLast() any {\n    num := q.peekLast()\n    if num == nil {\n        return nil\n    }\n    q.queSize--\n    return num\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (q *arrayDeque) peekFirst() any {\n    if q.isEmpty() {\n        return nil\n    }\n    return q.nums[q.front]\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc (q *arrayDeque) peekLast() any {\n    if q.isEmpty() {\n        return nil\n    }\n    // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n    last := q.index(q.front + q.queSize - 1)\n    return q.nums[last]\n}\n\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (q *arrayDeque) toSlice() []int {\n    // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n    res := make([]int, q.queSize)\n    for i, j := 0, q.front; i < q.queSize; i++ {\n        res[i] = q.nums[q.index(j)]\n        j++\n    }\n    return res\n}\n
        array_deque.swift
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n    private var nums: [Int] // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    private var front: Int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    private var _size: Int // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    init(capacity: Int) {\n        nums = Array(repeating: 0, count: capacity)\n        front = 0\n        _size = 0\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n    func capacity() -> Int {\n        nums.count\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    func size() -> Int {\n        _size\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n    private func index(i: Int) -> Int {\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n        (i + capacity()) % capacity()\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    func pushFirst(num: Int) {\n        if size() == capacity() {\n            print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n            return\n        }\n        // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n        front = index(i: front - 1)\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n        nums[front] = num\n        _size += 1\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    func pushLast(num: Int) {\n        if size() == capacity() {\n            print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n            return\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        let rear = index(i: front + size())\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        nums[rear] = num\n        _size += 1\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    func popFirst() -> Int {\n        let num = peekFirst()\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        front = index(i: front + 1)\n        _size -= 1\n        return num\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    func popLast() -> Int {\n        let num = peekLast()\n        _size -= 1\n        return num\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    func peekFirst() -> Int {\n        if isEmpty() {\n            fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        }\n        return nums[front]\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    func peekLast() -> Int {\n        if isEmpty() {\n            fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        }\n        // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n        let last = index(i: front + size() - 1)\n        return nums[last]\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    func toArray() -> [Int] {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        (front ..< front + size()).map { nums[index(i: $0)] }\n    }\n}\n
        array_deque.js
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n    #nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    #front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    #queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor(capacity) {\n        this.#nums = new Array(capacity);\n        this.#front = 0;\n        this.#queSize = 0;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n    capacity() {\n        return this.#nums.length;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    size() {\n        return this.#queSize;\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    isEmpty() {\n        return this.#queSize === 0;\n    }\n\n    /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n    index(i) {\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n        return (i + this.capacity()) % this.capacity();\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    pushFirst(num) {\n        if (this.#queSize === this.capacity()) {\n            console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n            return;\n        }\n        // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n        this.#front = this.index(this.#front - 1);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n        this.#nums[this.#front] = num;\n        this.#queSize++;\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    pushLast(num) {\n        if (this.#queSize === this.capacity()) {\n            console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        const rear = this.index(this.#front + this.#queSize);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        this.#nums[rear] = num;\n        this.#queSize++;\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    popFirst() {\n        const num = this.peekFirst();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        this.#front = this.index(this.#front + 1);\n        this.#queSize--;\n        return num;\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    popLast() {\n        const num = this.peekLast();\n        this.#queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    peekFirst() {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        return this.#nums[this.#front];\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    peekLast() {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n        const last = this.index(this.#front + this.#queSize - 1);\n        return this.#nums[last];\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    toArray() {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        const res = [];\n        for (let i = 0, j = this.#front; i < this.#queSize; i++, j++) {\n            res[i] = this.#nums[this.index(j)];\n        }\n        return res;\n    }\n}\n
        array_deque.ts
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n    private nums: number[]; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    private front: number; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    private queSize: number; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor(capacity: number) {\n        this.nums = new Array(capacity);\n        this.front = 0;\n        this.queSize = 0;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n    capacity(): number {\n        return this.nums.length;\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    size(): number {\n        return this.queSize;\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    isEmpty(): boolean {\n        return this.queSize === 0;\n    }\n\n    /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n    index(i: number): number {\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n        return (i + this.capacity()) % this.capacity();\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    pushFirst(num: number): void {\n        if (this.queSize === this.capacity()) {\n            console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n            return;\n        }\n        // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n        this.front = this.index(this.front - 1);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n        this.nums[this.front] = num;\n        this.queSize++;\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    pushLast(num: number): void {\n        if (this.queSize === this.capacity()) {\n            console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        const rear: number = this.index(this.front + this.queSize);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        this.nums[rear] = num;\n        this.queSize++;\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    popFirst(): number {\n        const num: number = this.peekFirst();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        this.front = this.index(this.front + 1);\n        this.queSize--;\n        return num;\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    popLast(): number {\n        const num: number = this.peekLast();\n        this.queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    peekFirst(): number {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        return this.nums[this.front];\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    peekLast(): number {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n        const last = this.index(this.front + this.queSize - 1);\n        return this.nums[last];\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    toArray(): number[] {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        const res: number[] = [];\n        for (let i = 0, j = this.front; i < this.queSize; i++, j++) {\n            res[i] = this.nums[this.index(j)];\n        }\n        return res;\n    }\n}\n
        array_deque.dart
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n  late List<int> _nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n  late int _front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n  late int _queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n  /* \u6784\u9020\u65b9\u6cd5 */\n  ArrayDeque(int capacity) {\n    this._nums = List.filled(capacity, 0);\n    this._front = this._queSize = 0;\n  }\n\n  /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n  int capacity() {\n    return _nums.length;\n  }\n\n  /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n  int size() {\n    return _queSize;\n  }\n\n  /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n  bool isEmpty() {\n    return _queSize == 0;\n  }\n\n  /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n  int index(int i) {\n    // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n    // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n    // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n    return (i + capacity()) % capacity();\n  }\n\n  /* \u961f\u9996\u5165\u961f */\n  void pushFirst(int _num) {\n    if (_queSize == capacity()) {\n      throw Exception(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n    }\n    // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n    // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 _front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n    _front = index(_front - 1);\n    // \u5c06 _num \u6dfb\u52a0\u81f3\u961f\u9996\n    _nums[_front] = _num;\n    _queSize++;\n  }\n\n  /* \u961f\u5c3e\u5165\u961f */\n  void pushLast(int _num) {\n    if (_queSize == capacity()) {\n      throw Exception(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n    }\n    // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n    int rear = index(_front + _queSize);\n    // \u5c06 _num \u6dfb\u52a0\u81f3\u961f\u5c3e\n    _nums[rear] = _num;\n    _queSize++;\n  }\n\n  /* \u961f\u9996\u51fa\u961f */\n  int popFirst() {\n    int _num = peekFirst();\n    // \u961f\u9996\u6307\u9488\u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n    _front = index(_front + 1);\n    _queSize--;\n    return _num;\n  }\n\n  /* \u961f\u5c3e\u51fa\u961f */\n  int popLast() {\n    int _num = peekLast();\n    _queSize--;\n    return _num;\n  }\n\n  /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n  int peekFirst() {\n    if (isEmpty()) {\n      throw Exception(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n    }\n    return _nums[_front];\n  }\n\n  /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n  int peekLast() {\n    if (isEmpty()) {\n      throw Exception(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n    }\n    // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n    int last = index(_front + _queSize - 1);\n    return _nums[last];\n  }\n\n  /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n  List<int> toArray() {\n    // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n    List<int> res = List.filled(_queSize, 0);\n    for (int i = 0, j = _front; i < _queSize; i++, j++) {\n      res[i] = _nums[index(j)];\n    }\n    return res;\n  }\n}\n
        array_deque.rs
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nstruct ArrayDeque<T> {\n    nums: Vec<T>,    // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    front: usize,    // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    que_size: usize, // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n}\n\nimpl<T: Copy + Default> ArrayDeque<T> {\n    /* \u6784\u9020\u65b9\u6cd5 */\n    pub fn new(capacity: usize) -> Self {\n        Self {\n            nums: vec![T::default(); capacity],\n            front: 0,\n            que_size: 0,\n        }\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n    pub fn capacity(&self) -> usize {\n        self.nums.len()\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    pub fn size(&self) -> usize {\n        self.que_size\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    pub fn is_empty(&self) -> bool {\n        self.que_size == 0\n    }\n\n    /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n    fn index(&self, i: i32) -> usize {\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n        ((i + self.capacity() as i32) % self.capacity() as i32) as usize\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    pub fn push_first(&mut self, num: T) {\n        if self.que_size == self.capacity() {\n            println!(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n            return;\n        }\n        // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n        self.front = self.index(self.front as i32 - 1);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n        self.nums[self.front] = num;\n        self.que_size += 1;\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    pub fn push_last(&mut self, num: T) {\n        if self.que_size == self.capacity() {\n            println!(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        let rear = self.index(self.front as i32 + self.que_size as i32);\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        self.nums[rear] = num;\n        self.que_size += 1;\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    fn pop_first(&mut self) -> T {\n        let num = self.peek_first();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        self.front = self.index(self.front as i32 + 1);\n        self.que_size -= 1;\n        num\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    fn pop_last(&mut self) -> T {\n        let num = self.peek_last();\n        self.que_size -= 1;\n        num\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    fn peek_first(&self) -> T {\n        if self.is_empty() {\n            panic!(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        };\n        self.nums[self.front]\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    fn peek_last(&self) -> T {\n        if self.is_empty() {\n            panic!(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n        };\n        // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n        let last = self.index(self.front as i32 + self.que_size as i32 - 1);\n        self.nums[last]\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    fn to_array(&self) -> Vec<T> {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        let mut res = vec![T::default(); self.que_size];\n        let mut j = self.front;\n        for i in 0..self.que_size {\n            res[i] = self.nums[self.index(j as i32)];\n            j += 1;\n        }\n        res\n    }\n}\n
        array_deque.c
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntypedef struct {\n    int *nums;       // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    int front;       // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    int queSize;     // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\n    int queCapacity; // \u961f\u5217\u5bb9\u91cf\n} ArrayDeque;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayDeque *newArrayDeque(int capacity) {\n    ArrayDeque *deque = (ArrayDeque *)malloc(sizeof(ArrayDeque));\n    // \u521d\u59cb\u5316\u6570\u7ec4\n    deque->queCapacity = capacity;\n    deque->nums = (int *)malloc(sizeof(int) * deque->queCapacity);\n    deque->front = deque->queSize = 0;\n    return deque;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayDeque(ArrayDeque *deque) {\n    free(deque->nums);\n    free(deque);\n}\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity(ArrayDeque *deque) {\n    return deque->queCapacity;\n}\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size(ArrayDeque *deque) {\n    return deque->queSize;\n}\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(ArrayDeque *deque) {\n    return deque->queSize == 0;\n}\n\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nint dequeIndex(ArrayDeque *deque, int i) {\n    // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n    // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u56de\u5230\u5934\u90e8\n    // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n    return ((i + capacity(deque)) % capacity(deque));\n}\n\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(ArrayDeque *deque, int num) {\n    if (deque->queSize == capacity(deque)) {\n        printf(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\\r\\n\");\n        return;\n    }\n    // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n    // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u56de\u5230\u5c3e\u90e8\n    deque->front = dequeIndex(deque, deque->front - 1);\n    // \u5c06 num \u6dfb\u52a0\u5230\u961f\u9996\n    deque->nums[deque->front] = num;\n    deque->queSize++;\n}\n\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(ArrayDeque *deque, int num) {\n    if (deque->queSize == capacity(deque)) {\n        printf(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\\r\\n\");\n        return;\n    }\n    // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n    int rear = dequeIndex(deque, deque->front + deque->queSize);\n    // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n    deque->nums[rear] = num;\n    deque->queSize++;\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst(ArrayDeque *deque) {\n    // \u8bbf\u95ee\u5f02\u5e38\uff1a\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\n    assert(empty(deque) == 0);\n    return deque->nums[deque->front];\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast(ArrayDeque *deque) {\n    // \u8bbf\u95ee\u5f02\u5e38\uff1a\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\n    assert(empty(deque) == 0);\n    int last = dequeIndex(deque, deque->front + deque->queSize - 1);\n    return deque->nums[last];\n}\n\n/* \u961f\u9996\u51fa\u961f */\nint popFirst(ArrayDeque *deque) {\n    int num = peekFirst(deque);\n    // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    deque->front = dequeIndex(deque, deque->front + 1);\n    deque->queSize--;\n    return num;\n}\n\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast(ArrayDeque *deque) {\n    int num = peekLast(deque);\n    deque->queSize--;\n    return num;\n}\n\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nint *toArray(ArrayDeque *deque, int *queSize) {\n    *queSize = deque->queSize;\n    int *res = (int *)calloc(deque->queSize, sizeof(int));\n    int j = deque->front;\n    for (int i = 0; i < deque->queSize; i++) {\n        res[i] = deque->nums[j % deque->queCapacity];\n        j++;\n    }\n    return res;\n}\n
        array_deque.kt
        /* \u6784\u9020\u65b9\u6cd5 */\nclass ArrayDeque(capacity: Int) {\n    private var nums: IntArray = IntArray(capacity) // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    private var front: Int = 0 // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    private var queSize: Int = 0 // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n    fun capacity(): Int {\n        return nums.size\n    }\n\n    /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n    fun size(): Int {\n        return queSize\n    }\n\n    /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    fun isEmpty(): Boolean {\n        return queSize == 0\n    }\n\n    /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n    private fun index(i: Int): Int {\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n        // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n        return (i + capacity()) % capacity()\n    }\n\n    /* \u961f\u9996\u5165\u961f */\n    fun pushFirst(num: Int) {\n        if (queSize == capacity()) {\n            println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n            return\n        }\n        // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n        front = index(front - 1)\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n        nums[front] = num\n        queSize++\n    }\n\n    /* \u961f\u5c3e\u5165\u961f */\n    fun pushLast(num: Int) {\n        if (queSize == capacity()) {\n            println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n            return\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        val rear = index(front + queSize)\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        nums[rear] = num\n        queSize++\n    }\n\n    /* \u961f\u9996\u51fa\u961f */\n    fun popFirst(): Int {\n        val num = peekFirst()\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n        front = index(front + 1)\n        queSize--\n        return num\n    }\n\n    /* \u961f\u5c3e\u51fa\u961f */\n    fun popLast(): Int {\n        val num = peekLast()\n        queSize--\n        return num\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    fun peekFirst(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return nums[front]\n    }\n\n    /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n    fun peekLast(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n        val last = index(front + queSize - 1)\n        return nums[last]\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n    fun toArray(): IntArray {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        val res = IntArray(queSize)\n        var i = 0\n        var j = front\n        while (i < queSize) {\n            res[i] = nums[index(j)]\n            i++\n            j++\n        }\n        return res\n    }\n}\n
        array_deque.rb
        ### \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 ###\nclass ArrayDeque\n  ### \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 ###\n  attr_reader :size\n\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize(capacity)\n    @nums = Array.new(capacity, 0)\n    @front = 0\n    @size = 0\n  end\n\n  ### \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf ###\n  def capacity\n    @nums.length\n  end\n\n  ### \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a ###\n  def is_empty?\n    size.zero?\n  end\n\n  ### \u961f\u9996\u5165\u961f ###\n  def push_first(num)\n    if size == capacity\n      puts '\u53cc\u5411\u961f\u5217\u5df2\u6ee1'\n      return\n    end\n\n    # \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n    # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n    @front = index(@front - 1)\n    # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n    @nums[@front] = num\n    @size += 1\n  end\n\n  ### \u961f\u5c3e\u5165\u961f ###\n  def push_last(num)\n    if size == capacity\n      puts '\u53cc\u5411\u961f\u5217\u5df2\u6ee1'\n      return\n    end\n\n    # \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n    rear = index(@front + size)\n    # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n    @nums[rear] = num\n    @size += 1\n  end\n\n  ### \u961f\u9996\u51fa\u961f ###\n  def pop_first\n    num = peek_first\n    # \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    @front = index(@front + 1)\n    @size -= 1\n    num\n  end\n\n  ### \u961f\u5c3e\u51fa\u961f ###\n  def pop_last\n    num = peek_last\n    @size -= 1\n    num\n  end\n\n  ### \u8bbf\u95ee\u961f\u9996\u5143\u7d20 ###\n  def peek_first\n    raise IndexError, '\u53cc\u5411\u961f\u5217\u4e3a\u7a7a' if is_empty?\n\n    @nums[@front]\n  end\n\n  ### \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 ###\n  def peek_last\n    raise IndexError, '\u53cc\u5411\u961f\u5217\u4e3a\u7a7a' if is_empty?\n\n    # \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n    last = index(@front + size - 1)\n    @nums[last]\n  end\n\n  ### \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 ###\n  def to_array\n    # \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n    res = []\n    for i in 0...size\n      res << @nums[index(@front + i)]\n    end\n    res\n  end\n\n  private\n\n  ### \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 ###\n  def index(i)\n    # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n    # \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n    # \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n    (i + capacity) % capacity\n  end\nend\n
        "},{"location":"chapter_stack_and_queue/deque/#533","title":"5.3.3 \u00a0 \u53cc\u5411\u961f\u5217\u5e94\u7528","text":"

        \u53cc\u5411\u961f\u5217\u517c\u5177\u6808\u4e0e\u961f\u5217\u7684\u903b\u8f91\uff0c\u56e0\u6b64\u5b83\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e24\u8005\u7684\u6240\u6709\u5e94\u7528\u573a\u666f\uff0c\u540c\u65f6\u63d0\u4f9b\u66f4\u9ad8\u7684\u81ea\u7531\u5ea6\u3002

        \u6211\u4eec\u77e5\u9053\uff0c\u8f6f\u4ef6\u7684\u201c\u64a4\u9500\u201d\u529f\u80fd\u901a\u5e38\u4f7f\u7528\u6808\u6765\u5b9e\u73b0\uff1a\u7cfb\u7edf\u5c06\u6bcf\u6b21\u66f4\u6539\u64cd\u4f5c push \u5230\u6808\u4e2d\uff0c\u7136\u540e\u901a\u8fc7 pop \u5b9e\u73b0\u64a4\u9500\u3002\u7136\u800c\uff0c\u8003\u8651\u5230\u7cfb\u7edf\u8d44\u6e90\u7684\u9650\u5236\uff0c\u8f6f\u4ef6\u901a\u5e38\u4f1a\u9650\u5236\u64a4\u9500\u7684\u6b65\u6570\uff08\u4f8b\u5982\u4ec5\u5141\u8bb8\u4fdd\u5b58 \\(50\\) \u6b65\uff09\u3002\u5f53\u6808\u7684\u957f\u5ea6\u8d85\u8fc7 \\(50\\) \u65f6\uff0c\u8f6f\u4ef6\u9700\u8981\u5728\u6808\u5e95\uff08\u961f\u9996\uff09\u6267\u884c\u5220\u9664\u64cd\u4f5c\u3002\u4f46\u6808\u65e0\u6cd5\u5b9e\u73b0\u8be5\u529f\u80fd\uff0c\u6b64\u65f6\u5c31\u9700\u8981\u4f7f\u7528\u53cc\u5411\u961f\u5217\u6765\u66ff\u4ee3\u6808\u3002\u8bf7\u6ce8\u610f\uff0c\u201c\u64a4\u9500\u201d\u7684\u6838\u5fc3\u903b\u8f91\u4ecd\u7136\u9075\u5faa\u6808\u7684\u5148\u5165\u540e\u51fa\u539f\u5219\uff0c\u53ea\u662f\u53cc\u5411\u961f\u5217\u80fd\u591f\u66f4\u52a0\u7075\u6d3b\u5730\u5b9e\u73b0\u4e00\u4e9b\u989d\u5916\u903b\u8f91\u3002

        "},{"location":"chapter_stack_and_queue/queue/","title":"5.2 \u00a0 \u961f\u5217","text":"

        \u961f\u5217\uff08queue\uff09\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u5148\u51fa\u89c4\u5219\u7684\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002\u987e\u540d\u601d\u4e49\uff0c\u961f\u5217\u6a21\u62df\u4e86\u6392\u961f\u73b0\u8c61\uff0c\u5373\u65b0\u6765\u7684\u4eba\u4e0d\u65ad\u52a0\u5165\u961f\u5217\u5c3e\u90e8\uff0c\u800c\u4f4d\u4e8e\u961f\u5217\u5934\u90e8\u7684\u4eba\u9010\u4e2a\u79bb\u5f00\u3002

        \u5982\u56fe 5-4 \u6240\u793a\uff0c\u6211\u4eec\u5c06\u961f\u5217\u5934\u90e8\u79f0\u4e3a\u201c\u961f\u9996\u201d\uff0c\u5c3e\u90e8\u79f0\u4e3a\u201c\u961f\u5c3e\u201d\uff0c\u5c06\u628a\u5143\u7d20\u52a0\u5165\u961f\u5c3e\u7684\u64cd\u4f5c\u79f0\u4e3a\u201c\u5165\u961f\u201d\uff0c\u5220\u9664\u961f\u9996\u5143\u7d20\u7684\u64cd\u4f5c\u79f0\u4e3a\u201c\u51fa\u961f\u201d\u3002

        \u56fe 5-4 \u00a0 \u961f\u5217\u7684\u5148\u5165\u5148\u51fa\u89c4\u5219

        "},{"location":"chapter_stack_and_queue/queue/#521","title":"5.2.1 \u00a0 \u961f\u5217\u5e38\u7528\u64cd\u4f5c","text":"

        \u961f\u5217\u7684\u5e38\u89c1\u64cd\u4f5c\u5982\u8868 5-2 \u6240\u793a\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u7684\u65b9\u6cd5\u540d\u79f0\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u6211\u4eec\u5728\u6b64\u91c7\u7528\u4e0e\u6808\u76f8\u540c\u7684\u65b9\u6cd5\u547d\u540d\u3002

        \u8868 5-2 \u00a0 \u961f\u5217\u64cd\u4f5c\u6548\u7387

        \u65b9\u6cd5\u540d \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push() \u5143\u7d20\u5165\u961f\uff0c\u5373\u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u961f\u5c3e \\(O(1)\\) pop() \u961f\u9996\u5143\u7d20\u51fa\u961f \\(O(1)\\) peek() \u8bbf\u95ee\u961f\u9996\u5143\u7d20 \\(O(1)\\)

        \u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u4e2d\u73b0\u6210\u7684\u961f\u5217\u7c7b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby queue.py
        from collections import deque\n\n# \u521d\u59cb\u5316\u961f\u5217\n# \u5728 Python \u4e2d\uff0c\u6211\u4eec\u4e00\u822c\u5c06\u53cc\u5411\u961f\u5217\u7c7b deque \u5f53\u4f5c\u961f\u5217\u4f7f\u7528\n# \u867d\u7136 queue.Queue() \u662f\u7eaf\u6b63\u7684\u961f\u5217\u7c7b\uff0c\u4f46\u4e0d\u592a\u597d\u7528\uff0c\u56e0\u6b64\u4e0d\u63a8\u8350\nque: deque[int] = deque()\n\n# \u5143\u7d20\u5165\u961f\nque.append(1)\nque.append(3)\nque.append(2)\nque.append(5)\nque.append(4)\n\n# \u8bbf\u95ee\u961f\u9996\u5143\u7d20\nfront: int = que[0]\n\n# \u5143\u7d20\u51fa\u961f\npop: int = que.popleft()\n\n# \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\nsize: int = len(que)\n\n# \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = len(que) == 0\n
        queue.cpp
        /* \u521d\u59cb\u5316\u961f\u5217 */\nqueue<int> queue;\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint front = queue.front();\n\n/* \u5143\u7d20\u51fa\u961f */\nqueue.pop();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.size();\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty = queue.empty();\n
        queue.java
        /* \u521d\u59cb\u5316\u961f\u5217 */\nQueue<Integer> queue = new LinkedList<>();\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.offer(1);\nqueue.offer(3);\nqueue.offer(2);\nqueue.offer(5);\nqueue.offer(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.peek();\n\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.poll();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.size();\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = queue.isEmpty();\n
        queue.cs
        /* \u521d\u59cb\u5316\u961f\u5217 */\nQueue<int> queue = new();\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.Enqueue(1);\nqueue.Enqueue(3);\nqueue.Enqueue(2);\nqueue.Enqueue(5);\nqueue.Enqueue(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.Peek();\n\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.Dequeue();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.Count;\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = queue.Count == 0;\n
        queue_test.go
        /* \u521d\u59cb\u5316\u961f\u5217 */\n// \u5728 Go \u4e2d\uff0c\u5c06 list \u4f5c\u4e3a\u961f\u5217\u6765\u4f7f\u7528\nqueue := list.New()\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.PushBack(1)\nqueue.PushBack(3)\nqueue.PushBack(2)\nqueue.PushBack(5)\nqueue.PushBack(4)\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek := queue.Front()\n\n/* \u5143\u7d20\u51fa\u961f */\npop := queue.Front()\nqueue.Remove(pop)\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nsize := queue.Len()\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty := queue.Len() == 0\n
        queue.swift
        /* \u521d\u59cb\u5316\u961f\u5217 */\n// Swift \u6ca1\u6709\u5185\u7f6e\u7684\u961f\u5217\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u961f\u5217\u6765\u4f7f\u7528\nvar queue: [Int] = []\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.append(1)\nqueue.append(3)\nqueue.append(2)\nqueue.append(5)\nqueue.append(4)\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nlet peek = queue.first!\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u7531\u4e8e\u662f\u6570\u7ec4\uff0c\u56e0\u6b64 removeFirst \u7684\u590d\u6742\u5ea6\u4e3a O(n)\nlet pool = queue.removeFirst()\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = queue.count\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = queue.isEmpty\n
        queue.js
        /* \u521d\u59cb\u5316\u961f\u5217 */\n// JavaScript \u6ca1\u6709\u5185\u7f6e\u7684\u961f\u5217\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u961f\u5217\u6765\u4f7f\u7528\nconst queue = [];\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nconst peek = queue[0];\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u5e95\u5c42\u662f\u6570\u7ec4\uff0c\u56e0\u6b64 shift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst pop = queue.shift();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = queue.length;\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst empty = queue.length === 0;\n
        queue.ts
        /* \u521d\u59cb\u5316\u961f\u5217 */\n// TypeScript \u6ca1\u6709\u5185\u7f6e\u7684\u961f\u5217\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u961f\u5217\u6765\u4f7f\u7528\nconst queue: number[] = [];\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nconst peek = queue[0];\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u5e95\u5c42\u662f\u6570\u7ec4\uff0c\u56e0\u6b64 shift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst pop = queue.shift();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = queue.length;\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst empty = queue.length === 0;\n
        queue.dart
        /* \u521d\u59cb\u5316\u961f\u5217 */\n// \u5728 Dart \u4e2d\uff0c\u961f\u5217\u7c7b Qeque \u662f\u53cc\u5411\u961f\u5217\uff0c\u4e5f\u53ef\u4f5c\u4e3a\u961f\u5217\u4f7f\u7528\nQueue<int> queue = Queue();\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.add(1);\nqueue.add(3);\nqueue.add(2);\nqueue.add(5);\nqueue.add(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.first;\n\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.removeFirst();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.length;\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = queue.isEmpty;\n
        queue.rs
        /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 Rust \u4e2d\u4f7f\u7528\u53cc\u5411\u961f\u5217\u4f5c\u4e3a\u666e\u901a\u961f\u5217\u6765\u4f7f\u7528\nlet mut deque: VecDeque<u32> = VecDeque::new();\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.push_back(1);\ndeque.push_back(3);\ndeque.push_back(2);\ndeque.push_back(5);\ndeque.push_back(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nif let Some(front) = deque.front() {\n}\n\n/* \u5143\u7d20\u51fa\u961f */\nif let Some(pop) = deque.pop_front() {\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = deque.len();\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = deque.is_empty();\n
        queue.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e\u961f\u5217\n
        queue.kt
        /* \u521d\u59cb\u5316\u961f\u5217 */\nval queue = LinkedList<Int>()\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.offer(1)\nqueue.offer(3)\nqueue.offer(2)\nqueue.offer(5)\nqueue.offer(4)\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nval peek = queue.peek()\n\n/* \u5143\u7d20\u51fa\u961f */\nval pop = queue.poll()\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nval size = queue.size\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nval isEmpty = queue.isEmpty()\n
        queue.rb
        # \u521d\u59cb\u5316\u961f\u5217\n# Ruby \u5185\u7f6e\u7684\u961f\u5217\uff08Thread::Queue) \u6ca1\u6709 peek \u548c\u904d\u5386\u65b9\u6cd5\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u961f\u5217\u6765\u4f7f\u7528\nqueue = []\n\n# \u5143\u7d20\u5165\u961f\nqueue.push(1)\nqueue.push(3)\nqueue.push(2)\nqueue.push(5)\nqueue.push(4)\n\n# \u8bbf\u95ee\u961f\u5217\u5143\u7d20\npeek = queue.first\n\n# \u5143\u7d20\u51fa\u961f\n# \u6e05\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cArray#shift \u65b9\u6cd5\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\npop = queue.shift\n\n# \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\nsize = queue.length\n\n# \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\nis_empty = queue.empty?\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_stack_and_queue/queue/#522","title":"5.2.2 \u00a0 \u961f\u5217\u5b9e\u73b0","text":"

        \u4e3a\u4e86\u5b9e\u73b0\u961f\u5217\uff0c\u6211\u4eec\u9700\u8981\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5728\u4e00\u7aef\u6dfb\u52a0\u5143\u7d20\uff0c\u5e76\u5728\u53e6\u4e00\u7aef\u5220\u9664\u5143\u7d20\uff0c\u94fe\u8868\u548c\u6570\u7ec4\u90fd\u7b26\u5408\u8981\u6c42\u3002

        "},{"location":"chapter_stack_and_queue/queue/#1","title":"1. \u00a0 \u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0","text":"

        \u5982\u56fe 5-5 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u94fe\u8868\u7684\u201c\u5934\u8282\u70b9\u201d\u548c\u201c\u5c3e\u8282\u70b9\u201d\u5206\u522b\u89c6\u4e3a\u201c\u961f\u9996\u201d\u548c\u201c\u961f\u5c3e\u201d\uff0c\u89c4\u5b9a\u961f\u5c3e\u4ec5\u53ef\u6dfb\u52a0\u8282\u70b9\uff0c\u961f\u9996\u4ec5\u53ef\u5220\u9664\u8282\u70b9\u3002

        LinkedListQueuepush()pop()

        \u56fe 5-5 \u00a0 \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c

        \u4ee5\u4e0b\u662f\u7528\u94fe\u8868\u5b9e\u73b0\u961f\u5217\u7684\u4ee3\u7801\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linkedlist_queue.py
        class LinkedListQueue:\n    \"\"\"\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217\"\"\"\n\n    def __init__(self):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        self._front: ListNode | None = None  # \u5934\u8282\u70b9 front\n        self._rear: ListNode | None = None  # \u5c3e\u8282\u70b9 rear\n        self._size: int = 0\n\n    def size(self) -> int:\n        \"\"\"\u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n        return self._size == 0\n\n    def push(self, num: int):\n        \"\"\"\u5165\u961f\"\"\"\n        # \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n        node = ListNode(num)\n        # \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n        if self._front is None:\n            self._front = node\n            self._rear = node\n        # \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n        else:\n            self._rear.next = node\n            self._rear = node\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"\u51fa\u961f\"\"\"\n        num = self.peek()\n        # \u5220\u9664\u5934\u8282\u70b9\n        self._front = self._front.next\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u961f\u5217\u4e3a\u7a7a\")\n        return self._front.val\n\n    def to_list(self) -> list[int]:\n        \"\"\"\u8f6c\u5316\u4e3a\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n        queue = []\n        temp = self._front\n        while temp:\n            queue.append(temp.val)\n            temp = temp.next\n        return queue\n
        linkedlist_queue.cpp
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n  private:\n    ListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n    int queSize;\n\n  public:\n    LinkedListQueue() {\n        front = nullptr;\n        rear = nullptr;\n        queSize = 0;\n    }\n\n    ~LinkedListQueue() {\n        // \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\n        freeMemoryLinkedList(front);\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    int size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    bool isEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u5165\u961f */\n    void push(int num) {\n        // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n        ListNode *node = new ListNode(num);\n        // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n        if (front == nullptr) {\n            front = node;\n            rear = node;\n        }\n        // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n        else {\n            rear->next = node;\n            rear = node;\n        }\n        queSize++;\n    }\n\n    /* \u51fa\u961f */\n    int pop() {\n        int num = peek();\n        // \u5220\u9664\u5934\u8282\u70b9\n        ListNode *tmp = front;\n        front = front->next;\n        // \u91ca\u653e\u5185\u5b58\n        delete tmp;\n        queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    int peek() {\n        if (size() == 0)\n            throw out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\n        return front->val;\n    }\n\n    /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Vector \u5e76\u8fd4\u56de */\n    vector<int> toVector() {\n        ListNode *node = front;\n        vector<int> res(size());\n        for (int i = 0; i < res.size(); i++) {\n            res[i] = node->val;\n            node = node->next;\n        }\n        return res;\n    }\n};\n
        linkedlist_queue.java
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n    private ListNode front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n    private int queSize = 0;\n\n    public LinkedListQueue() {\n        front = null;\n        rear = null;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    public int size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u5165\u961f */\n    public void push(int num) {\n        // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n        ListNode node = new ListNode(num);\n        // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n        if (front == null) {\n            front = node;\n            rear = node;\n        // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n        } else {\n            rear.next = node;\n            rear = node;\n        }\n        queSize++;\n    }\n\n    /* \u51fa\u961f */\n    public int pop() {\n        int num = peek();\n        // \u5220\u9664\u5934\u8282\u70b9\n        front = front.next;\n        queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return front.val;\n    }\n\n    /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    public int[] toArray() {\n        ListNode node = front;\n        int[] res = new int[size()];\n        for (int i = 0; i < res.length; i++) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_queue.cs
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n    ListNode? front, rear;  // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear \n    int queSize = 0;\n\n    public LinkedListQueue() {\n        front = null;\n        rear = null;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    public int Size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    public bool IsEmpty() {\n        return Size() == 0;\n    }\n\n    /* \u5165\u961f */\n    public void Push(int num) {\n        // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n        ListNode node = new(num);\n        // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n        if (front == null) {\n            front = node;\n            rear = node;\n            // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n        } else if (rear != null) {\n            rear.next = node;\n            rear = node;\n        }\n        queSize++;\n    }\n\n    /* \u51fa\u961f */\n    public int Pop() {\n        int num = Peek();\n        // \u5220\u9664\u5934\u8282\u70b9\n        front = front?.next;\n        queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    public int Peek() {\n        if (IsEmpty())\n            throw new Exception();\n        return front!.val;\n    }\n\n    /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    public int[] ToArray() {\n        if (front == null)\n            return [];\n\n        ListNode? node = front;\n        int[] res = new int[Size()];\n        for (int i = 0; i < res.Length; i++) {\n            res[i] = node!.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_queue.go
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\ntype linkedListQueue struct {\n    // \u4f7f\u7528\u5185\u7f6e\u5305 list \u6765\u5b9e\u73b0\u961f\u5217\n    data *list.List\n}\n\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newLinkedListQueue() *linkedListQueue {\n    return &linkedListQueue{\n        data: list.New(),\n    }\n}\n\n/* \u5165\u961f */\nfunc (s *linkedListQueue) push(value any) {\n    s.data.PushBack(value)\n}\n\n/* \u51fa\u961f */\nfunc (s *linkedListQueue) pop() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Front()\n    s.data.Remove(e)\n    return e.Value\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (s *linkedListQueue) peek() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Front()\n    return e.Value\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (s *linkedListQueue) size() int {\n    return s.data.Len()\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListQueue) isEmpty() bool {\n    return s.data.Len() == 0\n}\n\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListQueue) toList() *list.List {\n    return s.data\n}\n
        linkedlist_queue.swift
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n    private var front: ListNode? // \u5934\u8282\u70b9\n    private var rear: ListNode? // \u5c3e\u8282\u70b9\n    private var _size: Int\n\n    init() {\n        _size = 0\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    func size() -> Int {\n        _size\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* \u5165\u961f */\n    func push(num: Int) {\n        // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n        let node = ListNode(x: num)\n        // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n        if front == nil {\n            front = node\n            rear = node\n        }\n        // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n        else {\n            rear?.next = node\n            rear = node\n        }\n        _size += 1\n    }\n\n    /* \u51fa\u961f */\n    @discardableResult\n    func pop() -> Int {\n        let num = peek()\n        // \u5220\u9664\u5934\u8282\u70b9\n        front = front?.next\n        _size -= 1\n        return num\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    func peek() -> Int {\n        if isEmpty() {\n            fatalError(\"\u961f\u5217\u4e3a\u7a7a\")\n        }\n        return front!.val\n    }\n\n    /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    func toArray() -> [Int] {\n        var node = front\n        var res = Array(repeating: 0, count: size())\n        for i in res.indices {\n            res[i] = node!.val\n            node = node?.next\n        }\n        return res\n    }\n}\n
        linkedlist_queue.js
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n    #front; // \u5934\u8282\u70b9 #front\n    #rear; // \u5c3e\u8282\u70b9 #rear\n    #queSize = 0;\n\n    constructor() {\n        this.#front = null;\n        this.#rear = null;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    get size() {\n        return this.#queSize;\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    isEmpty() {\n        return this.size === 0;\n    }\n\n    /* \u5165\u961f */\n    push(num) {\n        // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n        const node = new ListNode(num);\n        // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n        if (!this.#front) {\n            this.#front = node;\n            this.#rear = node;\n            // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n        } else {\n            this.#rear.next = node;\n            this.#rear = node;\n        }\n        this.#queSize++;\n    }\n\n    /* \u51fa\u961f */\n    pop() {\n        const num = this.peek();\n        // \u5220\u9664\u5934\u8282\u70b9\n        this.#front = this.#front.next;\n        this.#queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    peek() {\n        if (this.size === 0) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n        return this.#front.val;\n    }\n\n    /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    toArray() {\n        let node = this.#front;\n        const res = new Array(this.size);\n        for (let i = 0; i < res.length; i++) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_queue.ts
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n    private front: ListNode | null; // \u5934\u8282\u70b9 front\n    private rear: ListNode | null; // \u5c3e\u8282\u70b9 rear\n    private queSize: number = 0;\n\n    constructor() {\n        this.front = null;\n        this.rear = null;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    get size(): number {\n        return this.queSize;\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    isEmpty(): boolean {\n        return this.size === 0;\n    }\n\n    /* \u5165\u961f */\n    push(num: number): void {\n        // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n        const node = new ListNode(num);\n        // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n        if (!this.front) {\n            this.front = node;\n            this.rear = node;\n            // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n        } else {\n            this.rear!.next = node;\n            this.rear = node;\n        }\n        this.queSize++;\n    }\n\n    /* \u51fa\u961f */\n    pop(): number {\n        const num = this.peek();\n        if (!this.front) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n        // \u5220\u9664\u5934\u8282\u70b9\n        this.front = this.front.next;\n        this.queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    peek(): number {\n        if (this.size === 0) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n        return this.front!.val;\n    }\n\n    /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    toArray(): number[] {\n        let node = this.front;\n        const res = new Array<number>(this.size);\n        for (let i = 0; i < res.length; i++) {\n            res[i] = node!.val;\n            node = node!.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_queue.dart
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n  ListNode? _front; // \u5934\u8282\u70b9 _front\n  ListNode? _rear; // \u5c3e\u8282\u70b9 _rear\n  int _queSize = 0; // \u961f\u5217\u957f\u5ea6\n\n  LinkedListQueue() {\n    _front = null;\n    _rear = null;\n  }\n\n  /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n  int size() {\n    return _queSize;\n  }\n\n  /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n  bool isEmpty() {\n    return _queSize == 0;\n  }\n\n  /* \u5165\u961f */\n  void push(int _num) {\n    // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 _num\n    final node = ListNode(_num);\n    // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n    if (_front == null) {\n      _front = node;\n      _rear = node;\n    } else {\n      // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n      _rear!.next = node;\n      _rear = node;\n    }\n    _queSize++;\n  }\n\n  /* \u51fa\u961f */\n  int pop() {\n    final int _num = peek();\n    // \u5220\u9664\u5934\u8282\u70b9\n    _front = _front!.next;\n    _queSize--;\n    return _num;\n  }\n\n  /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n  int peek() {\n    if (_queSize == 0) {\n      throw Exception('\u961f\u5217\u4e3a\u7a7a');\n    }\n    return _front!.val;\n  }\n\n  /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n  List<int> toArray() {\n    ListNode? node = _front;\n    final List<int> queue = [];\n    while (node != null) {\n      queue.add(node.val);\n      node = node.next;\n    }\n    return queue;\n  }\n}\n
        linkedlist_queue.rs
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\n#[allow(dead_code)]\npub struct LinkedListQueue<T> {\n    front: Option<Rc<RefCell<ListNode<T>>>>, // \u5934\u8282\u70b9 front\n    rear: Option<Rc<RefCell<ListNode<T>>>>,  // \u5c3e\u8282\u70b9 rear\n    que_size: usize,                         // \u961f\u5217\u7684\u957f\u5ea6\n}\n\nimpl<T: Copy> LinkedListQueue<T> {\n    pub fn new() -> Self {\n        Self {\n            front: None,\n            rear: None,\n            que_size: 0,\n        }\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    pub fn size(&self) -> usize {\n        return self.que_size;\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    pub fn is_empty(&self) -> bool {\n        return self.que_size == 0;\n    }\n\n    /* \u5165\u961f */\n    pub fn push(&mut self, num: T) {\n        // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n        let new_rear = ListNode::new(num);\n        match self.rear.take() {\n            // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n            Some(old_rear) => {\n                old_rear.borrow_mut().next = Some(new_rear.clone());\n                self.rear = Some(new_rear);\n            }\n            // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n            None => {\n                self.front = Some(new_rear.clone());\n                self.rear = Some(new_rear);\n            }\n        }\n        self.que_size += 1;\n    }\n\n    /* \u51fa\u961f */\n    pub fn pop(&mut self) -> Option<T> {\n        self.front.take().map(|old_front| {\n            match old_front.borrow_mut().next.take() {\n                Some(new_front) => {\n                    self.front = Some(new_front);\n                }\n                None => {\n                    self.rear.take();\n                }\n            }\n            self.que_size -= 1;\n            old_front.borrow().val\n        })\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n        self.front.as_ref()\n    }\n\n    /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n        let mut res: Vec<T> = Vec::new();\n\n        fn recur<T: Copy>(cur: Option<&Rc<RefCell<ListNode<T>>>>, res: &mut Vec<T>) {\n            if let Some(cur) = cur {\n                res.push(cur.borrow().val);\n                recur(cur.borrow().next.as_ref(), res);\n            }\n        }\n\n        recur(head, &mut res);\n\n        res\n    }\n}\n
        linkedlist_queue.c
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\ntypedef struct {\n    ListNode *front, *rear;\n    int queSize;\n} LinkedListQueue;\n\n/* \u6784\u9020\u51fd\u6570 */\nLinkedListQueue *newLinkedListQueue() {\n    LinkedListQueue *queue = (LinkedListQueue *)malloc(sizeof(LinkedListQueue));\n    queue->front = NULL;\n    queue->rear = NULL;\n    queue->queSize = 0;\n    return queue;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListQueue(LinkedListQueue *queue) {\n    // \u91ca\u653e\u6240\u6709\u8282\u70b9\n    while (queue->front != NULL) {\n        ListNode *tmp = queue->front;\n        queue->front = queue->front->next;\n        free(tmp);\n    }\n    // \u91ca\u653e queue \u7ed3\u6784\u4f53\n    free(queue);\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(LinkedListQueue *queue) {\n    return queue->queSize;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(LinkedListQueue *queue) {\n    return (size(queue) == 0);\n}\n\n/* \u5165\u961f */\nvoid push(LinkedListQueue *queue, int num) {\n    // \u5c3e\u8282\u70b9\u5904\u6dfb\u52a0 node\n    ListNode *node = newListNode(num);\n    // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n    if (queue->front == NULL) {\n        queue->front = node;\n        queue->rear = node;\n    }\n    // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n    else {\n        queue->rear->next = node;\n        queue->rear = node;\n    }\n    queue->queSize++;\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek(LinkedListQueue *queue) {\n    assert(size(queue) && queue->front);\n    return queue->front->val;\n}\n\n/* \u51fa\u961f */\nint pop(LinkedListQueue *queue) {\n    int num = peek(queue);\n    ListNode *tmp = queue->front;\n    queue->front = queue->front->next;\n    free(tmp);\n    queue->queSize--;\n    return num;\n}\n\n/* \u6253\u5370\u961f\u5217 */\nvoid printLinkedListQueue(LinkedListQueue *queue) {\n    int *arr = malloc(sizeof(int) * queue->queSize);\n    // \u62f7\u8d1d\u94fe\u8868\u4e2d\u7684\u6570\u636e\u5230\u6570\u7ec4\n    int i;\n    ListNode *node;\n    for (i = 0, node = queue->front; i < queue->queSize; i++) {\n        arr[i] = node->val;\n        node = node->next;\n    }\n    printArray(arr, queue->queSize);\n    free(arr);\n}\n
        linkedlist_queue.kt
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue(\n    // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n    private var front: ListNode? = null,\n    private var rear: ListNode? = null,\n    private var queSize: Int = 0\n) {\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    fun size(): Int {\n        return queSize\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    fun isEmpty(): Boolean {\n        return size() == 0\n    }\n\n    /* \u5165\u961f */\n    fun push(num: Int) {\n        // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n        val node = ListNode(num)\n        // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n        if (front == null) {\n            front = node\n            rear = node\n            // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n        } else {\n            rear?.next = node\n            rear = node\n        }\n        queSize++\n    }\n\n    /* \u51fa\u961f */\n    fun pop(): Int {\n        val num = peek()\n        // \u5220\u9664\u5934\u8282\u70b9\n        front = front?.next\n        queSize--\n        return num\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    fun peek(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return front!!._val\n    }\n\n    /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    fun toArray(): IntArray {\n        var node = front\n        val res = IntArray(size())\n        for (i in res.indices) {\n            res[i] = node!!._val\n            node = node.next\n        }\n        return res\n    }\n}\n
        linkedlist_queue.rb
        ### \u57fa\u4e8e\u94fe\u8868\u5934\u73b0\u7684\u961f\u5217 ###\nclass LinkedListQueue\n  ### \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 ###\n  attr_reader :size\n\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize\n    @front = nil  # \u5934\u8282\u70b9 front\n    @rear = nil   # \u5c3e\u8282\u70b9 rear\n    @size = 0\n  end\n\n  ### \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a ###\n  def is_empty?\n    @front.nil?\n  end\n\n  ### \u5165\u961f ###\n  def push(num)\n    # \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n    node = ListNode.new(num)\n\n    # \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\uff0c\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n    if @front.nil?\n      @front = node\n      @rear = node\n    # \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u4ee4\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n    else\n      @rear.next = node\n      @rear = node\n    end\n\n    @size += 1\n  end\n\n  ### \u51fa\u961f ###\n  def pop\n    num = peek\n    # \u5220\u9664\u5934\u8282\u70b9\n    @front = @front.next\n    @size -= 1\n    num\n  end\n\n  ### \u8bbf\u95ee\u961f\u9996\u5143\u7d20 ###\n  def peek\n    raise IndexError, '\u961f\u5217\u4e3a\u7a7a' if is_empty?\n\n    @front.val\n  end\n\n  ### \u5c06\u94fe\u8868\u4e3a Array \u5e76\u8fd4\u56de ###\n  def to_array\n    queue = []\n    temp = @front\n    while temp\n      queue << temp.val\n      temp = temp.next\n    end\n    queue\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_stack_and_queue/queue/#2","title":"2. \u00a0 \u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"

        \u5728\u6570\u7ec4\u4e2d\u5220\u9664\u9996\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u8fd9\u4f1a\u5bfc\u81f4\u51fa\u961f\u64cd\u4f5c\u6548\u7387\u8f83\u4f4e\u3002\u7136\u800c\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u5de7\u5999\u65b9\u6cd5\u6765\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\u3002

        \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u53d8\u91cf front \u6307\u5411\u961f\u9996\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u5e76\u7ef4\u62a4\u4e00\u4e2a\u53d8\u91cf size \u7528\u4e8e\u8bb0\u5f55\u961f\u5217\u957f\u5ea6\u3002\u5b9a\u4e49 rear = front + size \uff0c\u8fd9\u4e2a\u516c\u5f0f\u8ba1\u7b97\u51fa\u7684 rear \u6307\u5411\u961f\u5c3e\u5143\u7d20\u4e4b\u540e\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\u3002

        \u57fa\u4e8e\u6b64\u8bbe\u8ba1\uff0c\u6570\u7ec4\u4e2d\u5305\u542b\u5143\u7d20\u7684\u6709\u6548\u533a\u95f4\u4e3a [front, rear - 1]\uff0c\u5404\u79cd\u64cd\u4f5c\u7684\u5b9e\u73b0\u65b9\u6cd5\u5982\u56fe 5-6 \u6240\u793a\u3002

        • \u5165\u961f\u64cd\u4f5c\uff1a\u5c06\u8f93\u5165\u5143\u7d20\u8d4b\u503c\u7ed9 rear \u7d22\u5f15\u5904\uff0c\u5e76\u5c06 size \u589e\u52a0 1 \u3002
        • \u51fa\u961f\u64cd\u4f5c\uff1a\u53ea\u9700\u5c06 front \u589e\u52a0 1 \uff0c\u5e76\u5c06 size \u51cf\u5c11 1 \u3002

        \u53ef\u4ee5\u770b\u5230\uff0c\u5165\u961f\u548c\u51fa\u961f\u64cd\u4f5c\u90fd\u53ea\u9700\u8fdb\u884c\u4e00\u6b21\u64cd\u4f5c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(1)\\) \u3002

        ArrayQueuepush()pop()

        \u56fe 5-6 \u00a0 \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c

        \u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\u4e00\u4e2a\u95ee\u9898\uff1a\u5728\u4e0d\u65ad\u8fdb\u884c\u5165\u961f\u548c\u51fa\u961f\u7684\u8fc7\u7a0b\u4e2d\uff0cfront \u548c rear \u90fd\u5728\u5411\u53f3\u79fb\u52a8\uff0c\u5f53\u5b83\u4eec\u5230\u8fbe\u6570\u7ec4\u5c3e\u90e8\u65f6\u5c31\u65e0\u6cd5\u7ee7\u7eed\u79fb\u52a8\u4e86\u3002\u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u89c6\u4e3a\u9996\u5c3e\u76f8\u63a5\u7684\u201c\u73af\u5f62\u6570\u7ec4\u201d\u3002

        \u5bf9\u4e8e\u73af\u5f62\u6570\u7ec4\uff0c\u6211\u4eec\u9700\u8981\u8ba9 front \u6216 rear \u5728\u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u76f4\u63a5\u56de\u5230\u6570\u7ec4\u5934\u90e8\u7ee7\u7eed\u904d\u5386\u3002\u8fd9\u79cd\u5468\u671f\u6027\u89c4\u5f8b\u53ef\u4ee5\u901a\u8fc7\u201c\u53d6\u4f59\u64cd\u4f5c\u201d\u6765\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_queue.py
        class ArrayQueue:\n    \"\"\"\u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217\"\"\"\n\n    def __init__(self, size: int):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        self._nums: list[int] = [0] * size  # \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n        self._front: int = 0  # \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n        self._size: int = 0  # \u961f\u5217\u957f\u5ea6\n\n    def capacity(self) -> int:\n        \"\"\"\u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf\"\"\"\n        return len(self._nums)\n\n    def size(self) -> int:\n        \"\"\"\u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n        return self._size == 0\n\n    def push(self, num: int):\n        \"\"\"\u5165\u961f\"\"\"\n        if self._size == self.capacity():\n            raise IndexError(\"\u961f\u5217\u5df2\u6ee1\")\n        # \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n        rear: int = (self._front + self._size) % self.capacity()\n        # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        self._nums[rear] = num\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"\u51fa\u961f\"\"\"\n        num: int = self.peek()\n        # \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n        self._front = (self._front + 1) % self.capacity()\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u961f\u5217\u4e3a\u7a7a\")\n        return self._nums[self._front]\n\n    def to_list(self) -> list[int]:\n        \"\"\"\u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n        res = [0] * self.size()\n        j: int = self._front\n        for i in range(self.size()):\n            res[i] = self._nums[(j % self.capacity())]\n            j += 1\n        return res\n
        array_queue.cpp
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n  private:\n    int *nums;       // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    int front;       // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    int queSize;     // \u961f\u5217\u957f\u5ea6\n    int queCapacity; // \u961f\u5217\u5bb9\u91cf\n\n  public:\n    ArrayQueue(int capacity) {\n        // \u521d\u59cb\u5316\u6570\u7ec4\n        nums = new int[capacity];\n        queCapacity = capacity;\n        front = queSize = 0;\n    }\n\n    ~ArrayQueue() {\n        delete[] nums;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n    int capacity() {\n        return queCapacity;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    int size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    bool isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u5165\u961f */\n    void push(int num) {\n        if (queSize == queCapacity) {\n            cout << \"\u961f\u5217\u5df2\u6ee1\" << endl;\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n        int rear = (front + queSize) % queCapacity;\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u51fa\u961f */\n    int pop() {\n        int num = peek();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n        front = (front + 1) % queCapacity;\n        queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    int peek() {\n        if (isEmpty())\n            throw out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\n        return nums[front];\n    }\n\n    /* \u5c06\u6570\u7ec4\u8f6c\u5316\u4e3a Vector \u5e76\u8fd4\u56de */\n    vector<int> toVector() {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        vector<int> arr(queSize);\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            arr[i] = nums[j % queCapacity];\n        }\n        return arr;\n    }\n};\n
        array_queue.java
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n    private int[] nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    private int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    private int queSize; // \u961f\u5217\u957f\u5ea6\n\n    public ArrayQueue(int capacity) {\n        nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n    public int capacity() {\n        return nums.length;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    public int size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    public boolean isEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u5165\u961f */\n    public void push(int num) {\n        if (queSize == capacity()) {\n            System.out.println(\"\u961f\u5217\u5df2\u6ee1\");\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n        int rear = (front + queSize) % capacity();\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u51fa\u961f */\n    public int pop() {\n        int num = peek();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n        front = (front + 1) % capacity();\n        queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return nums[front];\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4 */\n    public int[] toArray() {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[j % capacity()];\n        }\n        return res;\n    }\n}\n
        array_queue.cs
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n    int[] nums;  // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    int front;   // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    int queSize; // \u961f\u5217\u957f\u5ea6\n\n    public ArrayQueue(int capacity) {\n        nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n    int Capacity() {\n        return nums.Length;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    public int Size() {\n        return queSize;\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    public bool IsEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u5165\u961f */\n    public void Push(int num) {\n        if (queSize == Capacity()) {\n            Console.WriteLine(\"\u961f\u5217\u5df2\u6ee1\");\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n        int rear = (front + queSize) % Capacity();\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u51fa\u961f */\n    public int Pop() {\n        int num = Peek();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n        front = (front + 1) % Capacity();\n        queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    public int Peek() {\n        if (IsEmpty())\n            throw new Exception();\n        return nums[front];\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4 */\n    public int[] ToArray() {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[j % this.Capacity()];\n        }\n        return res;\n    }\n}\n
        array_queue.go
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\ntype arrayQueue struct {\n    nums        []int // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    front       int   // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    queSize     int   // \u961f\u5217\u957f\u5ea6\n    queCapacity int   // \u961f\u5217\u5bb9\u91cf\uff08\u5373\u6700\u5927\u5bb9\u7eb3\u5143\u7d20\u6570\u91cf\uff09\n}\n\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newArrayQueue(queCapacity int) *arrayQueue {\n    return &arrayQueue{\n        nums:        make([]int, queCapacity),\n        queCapacity: queCapacity,\n        front:       0,\n        queSize:     0,\n    }\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (q *arrayQueue) size() int {\n    return q.queSize\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (q *arrayQueue) isEmpty() bool {\n    return q.queSize == 0\n}\n\n/* \u5165\u961f */\nfunc (q *arrayQueue) push(num int) {\n    // \u5f53 rear == queCapacity \u8868\u793a\u961f\u5217\u5df2\u6ee1\n    if q.queSize == q.queCapacity {\n        return\n    }\n    // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n    // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n    rear := (q.front + q.queSize) % q.queCapacity\n    // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n    q.nums[rear] = num\n    q.queSize++\n}\n\n/* \u51fa\u961f */\nfunc (q *arrayQueue) pop() any {\n    num := q.peek()\n    if num == nil {\n        return nil\n    }\n\n    // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n    q.front = (q.front + 1) % q.queCapacity\n    q.queSize--\n    return num\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (q *arrayQueue) peek() any {\n    if q.isEmpty() {\n        return nil\n    }\n    return q.nums[q.front]\n}\n\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (q *arrayQueue) toSlice() []int {\n    rear := (q.front + q.queSize)\n    if rear >= q.queCapacity {\n        rear %= q.queCapacity\n        return append(q.nums[q.front:], q.nums[:rear]...)\n    }\n    return q.nums[q.front:rear]\n}\n
        array_queue.swift
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n    private var nums: [Int] // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    private var front: Int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    private var _size: Int // \u961f\u5217\u957f\u5ea6\n\n    init(capacity: Int) {\n        // \u521d\u59cb\u5316\u6570\u7ec4\n        nums = Array(repeating: 0, count: capacity)\n        front = 0\n        _size = 0\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n    func capacity() -> Int {\n        nums.count\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    func size() -> Int {\n        _size\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* \u5165\u961f */\n    func push(num: Int) {\n        if size() == capacity() {\n            print(\"\u961f\u5217\u5df2\u6ee1\")\n            return\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n        let rear = (front + size()) % capacity()\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        nums[rear] = num\n        _size += 1\n    }\n\n    /* \u51fa\u961f */\n    @discardableResult\n    func pop() -> Int {\n        let num = peek()\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n        front = (front + 1) % capacity()\n        _size -= 1\n        return num\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    func peek() -> Int {\n        if isEmpty() {\n            fatalError(\"\u961f\u5217\u4e3a\u7a7a\")\n        }\n        return nums[front]\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4 */\n    func toArray() -> [Int] {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        (front ..< front + size()).map { nums[$0 % capacity()] }\n    }\n}\n
        array_queue.js
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n    #nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    #front = 0; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    #queSize = 0; // \u961f\u5217\u957f\u5ea6\n\n    constructor(capacity) {\n        this.#nums = new Array(capacity);\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n    get capacity() {\n        return this.#nums.length;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    get size() {\n        return this.#queSize;\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    isEmpty() {\n        return this.#queSize === 0;\n    }\n\n    /* \u5165\u961f */\n    push(num) {\n        if (this.size === this.capacity) {\n            console.log('\u961f\u5217\u5df2\u6ee1');\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n        const rear = (this.#front + this.size) % this.capacity;\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        this.#nums[rear] = num;\n        this.#queSize++;\n    }\n\n    /* \u51fa\u961f */\n    pop() {\n        const num = this.peek();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n        this.#front = (this.#front + 1) % this.capacity;\n        this.#queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    peek() {\n        if (this.isEmpty()) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n        return this.#nums[this.#front];\n    }\n\n    /* \u8fd4\u56de Array */\n    toArray() {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        const arr = new Array(this.size);\n        for (let i = 0, j = this.#front; i < this.size; i++, j++) {\n            arr[i] = this.#nums[j % this.capacity];\n        }\n        return arr;\n    }\n}\n
        array_queue.ts
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n    private nums: number[]; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    private front: number; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    private queSize: number; // \u961f\u5217\u957f\u5ea6\n\n    constructor(capacity: number) {\n        this.nums = new Array(capacity);\n        this.front = this.queSize = 0;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n    get capacity(): number {\n        return this.nums.length;\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    get size(): number {\n        return this.queSize;\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    isEmpty(): boolean {\n        return this.queSize === 0;\n    }\n\n    /* \u5165\u961f */\n    push(num: number): void {\n        if (this.size === this.capacity) {\n            console.log('\u961f\u5217\u5df2\u6ee1');\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n        const rear = (this.front + this.queSize) % this.capacity;\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        this.nums[rear] = num;\n        this.queSize++;\n    }\n\n    /* \u51fa\u961f */\n    pop(): number {\n        const num = this.peek();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n        this.front = (this.front + 1) % this.capacity;\n        this.queSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    peek(): number {\n        if (this.isEmpty()) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n        return this.nums[this.front];\n    }\n\n    /* \u8fd4\u56de Array */\n    toArray(): number[] {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        const arr = new Array(this.size);\n        for (let i = 0, j = this.front; i < this.size; i++, j++) {\n            arr[i] = this.nums[j % this.capacity];\n        }\n        return arr;\n    }\n}\n
        array_queue.dart
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n  late List<int> _nums; // \u7528\u4e8e\u50a8\u5b58\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n  late int _front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n  late int _queSize; // \u961f\u5217\u957f\u5ea6\n\n  ArrayQueue(int capacity) {\n    _nums = List.filled(capacity, 0);\n    _front = _queSize = 0;\n  }\n\n  /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n  int capaCity() {\n    return _nums.length;\n  }\n\n  /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n  int size() {\n    return _queSize;\n  }\n\n  /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n  bool isEmpty() {\n    return _queSize == 0;\n  }\n\n  /* \u5165\u961f */\n  void push(int _num) {\n    if (_queSize == capaCity()) {\n      throw Exception(\"\u961f\u5217\u5df2\u6ee1\");\n    }\n    // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n    // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n    int rear = (_front + _queSize) % capaCity();\n    // \u5c06 _num \u6dfb\u52a0\u81f3\u961f\u5c3e\n    _nums[rear] = _num;\n    _queSize++;\n  }\n\n  /* \u51fa\u961f */\n  int pop() {\n    int _num = peek();\n    // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n    _front = (_front + 1) % capaCity();\n    _queSize--;\n    return _num;\n  }\n\n  /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n  int peek() {\n    if (isEmpty()) {\n      throw Exception(\"\u961f\u5217\u4e3a\u7a7a\");\n    }\n    return _nums[_front];\n  }\n\n  /* \u8fd4\u56de Array */\n  List<int> toArray() {\n    // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n    final List<int> res = List.filled(_queSize, 0);\n    for (int i = 0, j = _front; i < _queSize; i++, j++) {\n      res[i] = _nums[j % capaCity()];\n    }\n    return res;\n  }\n}\n
        array_queue.rs
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nstruct ArrayQueue<T> {\n    nums: Vec<T>,      // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    front: i32,        // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    que_size: i32,     // \u961f\u5217\u957f\u5ea6\n    que_capacity: i32, // \u961f\u5217\u5bb9\u91cf\n}\n\nimpl<T: Copy + Default> ArrayQueue<T> {\n    /* \u6784\u9020\u65b9\u6cd5 */\n    fn new(capacity: i32) -> ArrayQueue<T> {\n        ArrayQueue {\n            nums: vec![T::default(); capacity as usize],\n            front: 0,\n            que_size: 0,\n            que_capacity: capacity,\n        }\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n    fn capacity(&self) -> i32 {\n        self.que_capacity\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    fn size(&self) -> i32 {\n        self.que_size\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    fn is_empty(&self) -> bool {\n        self.que_size == 0\n    }\n\n    /* \u5165\u961f */\n    fn push(&mut self, num: T) {\n        if self.que_size == self.capacity() {\n            println!(\"\u961f\u5217\u5df2\u6ee1\");\n            return;\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n        let rear = (self.front + self.que_size) % self.que_capacity;\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        self.nums[rear as usize] = num;\n        self.que_size += 1;\n    }\n\n    /* \u51fa\u961f */\n    fn pop(&mut self) -> T {\n        let num = self.peek();\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n        self.front = (self.front + 1) % self.que_capacity;\n        self.que_size -= 1;\n        num\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    fn peek(&self) -> T {\n        if self.is_empty() {\n            panic!(\"index out of bounds\");\n        }\n        self.nums[self.front as usize]\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4 */\n    fn to_vector(&self) -> Vec<T> {\n        let cap = self.que_capacity;\n        let mut j = self.front;\n        let mut arr = vec![T::default(); cap as usize];\n        for i in 0..self.que_size {\n            arr[i as usize] = self.nums[(j % cap) as usize];\n            j += 1;\n        }\n        arr\n    }\n}\n
        array_queue.c
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\ntypedef struct {\n    int *nums;       // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    int front;       // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    int queSize;     // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\n    int queCapacity; // \u961f\u5217\u5bb9\u91cf\n} ArrayQueue;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayQueue *newArrayQueue(int capacity) {\n    ArrayQueue *queue = (ArrayQueue *)malloc(sizeof(ArrayQueue));\n    // \u521d\u59cb\u5316\u6570\u7ec4\n    queue->queCapacity = capacity;\n    queue->nums = (int *)malloc(sizeof(int) * queue->queCapacity);\n    queue->front = queue->queSize = 0;\n    return queue;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayQueue(ArrayQueue *queue) {\n    free(queue->nums);\n    free(queue);\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity(ArrayQueue *queue) {\n    return queue->queCapacity;\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(ArrayQueue *queue) {\n    return queue->queSize;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(ArrayQueue *queue) {\n    return queue->queSize == 0;\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek(ArrayQueue *queue) {\n    assert(size(queue) != 0);\n    return queue->nums[queue->front];\n}\n\n/* \u5165\u961f */\nvoid push(ArrayQueue *queue, int num) {\n    if (size(queue) == capacity(queue)) {\n        printf(\"\u961f\u5217\u5df2\u6ee1\\r\\n\");\n        return;\n    }\n    // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n    // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n    int rear = (queue->front + queue->queSize) % queue->queCapacity;\n    // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n    queue->nums[rear] = num;\n    queue->queSize++;\n}\n\n/* \u51fa\u961f */\nint pop(ArrayQueue *queue) {\n    int num = peek(queue);\n    // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n    queue->front = (queue->front + 1) % queue->queCapacity;\n    queue->queSize--;\n    return num;\n}\n\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nint *toArray(ArrayQueue *queue, int *queSize) {\n    *queSize = queue->queSize;\n    int *res = (int *)calloc(queue->queSize, sizeof(int));\n    int j = queue->front;\n    for (int i = 0; i < queue->queSize; i++) {\n        res[i] = queue->nums[j % queue->queCapacity];\n        j++;\n    }\n    return res;\n}\n
        array_queue.kt
        /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue(capacity: Int) {\n    private val nums: IntArray = IntArray(capacity) // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    private var front: Int = 0 // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    private var queSize: Int = 0 // \u961f\u5217\u957f\u5ea6\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n    fun capacity(): Int {\n        return nums.size\n    }\n\n    /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n    fun size(): Int {\n        return queSize\n    }\n\n    /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n    fun isEmpty(): Boolean {\n        return queSize == 0\n    }\n\n    /* \u5165\u961f */\n    fun push(num: Int) {\n        if (queSize == capacity()) {\n            println(\"\u961f\u5217\u5df2\u6ee1\")\n            return\n        }\n        // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n        // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n        val rear = (front + queSize) % capacity()\n        // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n        nums[rear] = num\n        queSize++\n    }\n\n    /* \u51fa\u961f */\n    fun pop(): Int {\n        val num = peek()\n        // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n        front = (front + 1) % capacity()\n        queSize--\n        return num\n    }\n\n    /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n    fun peek(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return nums[front]\n    }\n\n    /* \u8fd4\u56de\u6570\u7ec4 */\n    fun toArray(): IntArray {\n        // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n        val res = IntArray(queSize)\n        var i = 0\n        var j = front\n        while (i < queSize) {\n            res[i] = nums[j % capacity()]\n            i++\n            j++\n        }\n        return res\n    }\n}\n
        array_queue.rb
        ### \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 ###\nclass ArrayQueue\n  ### \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 ###\n  attr_reader :size\n\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize(size)\n    @nums = Array.new(size, 0) # \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n    @front = 0 # \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n    @size = 0 # \u961f\u5217\u957f\u5ea6\n  end\n\n  ### \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf ###\n  def capacity\n    @nums.length\n  end\n\n  ### \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a ###\n  def is_empty?\n    size.zero?\n  end\n\n  ### \u5165\u961f ###\n  def push(num)\n    raise IndexError, '\u961f\u5217\u5df2\u6ee1' if size == capacity\n\n    # \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n    # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n    rear = (@front + size) % capacity\n    # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n    @nums[rear] = num\n    @size += 1\n  end\n\n  ### \u51fa\u961f ###\n  def pop\n    num = peek\n    # \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n    @front = (@front + 1) % capacity\n    @size -= 1\n    num\n  end\n\n  ### \u8bbf\u95ee\u961f\u9996\u5143\u7d20 ###\n  def peek\n    raise IndexError, '\u961f\u5217\u4e3a\u7a7a' if is_empty?\n\n    @nums[@front]\n  end\n\n  ### \u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370 ###\n  def to_array\n    res = Array.new(size, 0)\n    j = @front\n\n    for i in 0...size\n      res[i] = @nums[j % capacity]\n      j += 1\n    end\n\n    res\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u4ee5\u4e0a\u5b9e\u73b0\u7684\u961f\u5217\u4ecd\u7136\u5177\u6709\u5c40\u9650\u6027\uff1a\u5176\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002\u7136\u800c\uff0c\u8fd9\u4e2a\u95ee\u9898\u4e0d\u96be\u89e3\u51b3\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u66ff\u6362\u4e3a\u52a8\u6001\u6570\u7ec4\uff0c\u4ece\u800c\u5f15\u5165\u6269\u5bb9\u673a\u5236\u3002\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u5c1d\u8bd5\u81ea\u884c\u5b9e\u73b0\u3002

        \u4e24\u79cd\u5b9e\u73b0\u7684\u5bf9\u6bd4\u7ed3\u8bba\u4e0e\u6808\u4e00\u81f4\uff0c\u5728\u6b64\u4e0d\u518d\u8d58\u8ff0\u3002

        "},{"location":"chapter_stack_and_queue/queue/#523","title":"5.2.3 \u00a0 \u961f\u5217\u5178\u578b\u5e94\u7528","text":"
        • \u6dd8\u5b9d\u8ba2\u5355\u3002\u8d2d\u7269\u8005\u4e0b\u5355\u540e\uff0c\u8ba2\u5355\u5c06\u52a0\u5165\u961f\u5217\u4e2d\uff0c\u7cfb\u7edf\u968f\u540e\u4f1a\u6839\u636e\u987a\u5e8f\u5904\u7406\u961f\u5217\u4e2d\u7684\u8ba2\u5355\u3002\u5728\u53cc\u5341\u4e00\u671f\u95f4\uff0c\u77ed\u65f6\u95f4\u5185\u4f1a\u4ea7\u751f\u6d77\u91cf\u8ba2\u5355\uff0c\u9ad8\u5e76\u53d1\u6210\u4e3a\u5de5\u7a0b\u5e08\u4eec\u9700\u8981\u91cd\u70b9\u653b\u514b\u7684\u95ee\u9898\u3002
        • \u5404\u7c7b\u5f85\u529e\u4e8b\u9879\u3002\u4efb\u4f55\u9700\u8981\u5b9e\u73b0\u201c\u5148\u6765\u540e\u5230\u201d\u529f\u80fd\u7684\u573a\u666f\uff0c\u4f8b\u5982\u6253\u5370\u673a\u7684\u4efb\u52a1\u961f\u5217\u3001\u9910\u5385\u7684\u51fa\u9910\u961f\u5217\u7b49\uff0c\u961f\u5217\u5728\u8fd9\u4e9b\u573a\u666f\u4e2d\u53ef\u4ee5\u6709\u6548\u5730\u7ef4\u62a4\u5904\u7406\u987a\u5e8f\u3002
        "},{"location":"chapter_stack_and_queue/stack/","title":"5.1 \u00a0 \u6808","text":"

        \u6808\uff08stack\uff09\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u540e\u51fa\u903b\u8f91\u7684\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002

        \u6211\u4eec\u53ef\u4ee5\u5c06\u6808\u7c7b\u6bd4\u4e3a\u684c\u9762\u4e0a\u7684\u4e00\u645e\u76d8\u5b50\uff0c\u89c4\u5b9a\u6bcf\u6b21\u53ea\u80fd\u79fb\u52a8\u4e00\u4e2a\u76d8\u5b50\uff0c\u90a3\u4e48\u60f3\u53d6\u51fa\u5e95\u90e8\u7684\u76d8\u5b50\uff0c\u5219\u9700\u8981\u5148\u5c06\u4e0a\u9762\u7684\u76d8\u5b50\u4f9d\u6b21\u79fb\u8d70\u3002\u6211\u4eec\u5c06\u76d8\u5b50\u66ff\u6362\u4e3a\u5404\u79cd\u7c7b\u578b\u7684\u5143\u7d20\uff08\u5982\u6574\u6570\u3001\u5b57\u7b26\u3001\u5bf9\u8c61\u7b49\uff09\uff0c\u5c31\u5f97\u5230\u4e86\u6808\u8fd9\u79cd\u6570\u636e\u7ed3\u6784\u3002

        \u5982\u56fe 5-1 \u6240\u793a\uff0c\u6211\u4eec\u628a\u5806\u53e0\u5143\u7d20\u7684\u9876\u90e8\u79f0\u4e3a\u201c\u6808\u9876\u201d\uff0c\u5e95\u90e8\u79f0\u4e3a\u201c\u6808\u5e95\u201d\u3002\u5c06\u628a\u5143\u7d20\u6dfb\u52a0\u5230\u6808\u9876\u7684\u64cd\u4f5c\u53eb\u4f5c\u201c\u5165\u6808\u201d\uff0c\u5220\u9664\u6808\u9876\u5143\u7d20\u7684\u64cd\u4f5c\u53eb\u4f5c\u201c\u51fa\u6808\u201d\u3002

        \u56fe 5-1 \u00a0 \u6808\u7684\u5148\u5165\u540e\u51fa\u89c4\u5219

        "},{"location":"chapter_stack_and_queue/stack/#511","title":"5.1.1 \u00a0 \u6808\u7684\u5e38\u7528\u64cd\u4f5c","text":"

        \u6808\u7684\u5e38\u7528\u64cd\u4f5c\u5982\u8868 5-1 \u6240\u793a\uff0c\u5177\u4f53\u7684\u65b9\u6cd5\u540d\u9700\u8981\u6839\u636e\u6240\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002\u5728\u6b64\uff0c\u6211\u4eec\u4ee5\u5e38\u89c1\u7684 push()\u3001pop()\u3001peek() \u547d\u540d\u4e3a\u4f8b\u3002

        \u8868 5-1 \u00a0 \u6808\u7684\u64cd\u4f5c\u6548\u7387

        \u65b9\u6cd5 \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push() \u5143\u7d20\u5165\u6808\uff08\u6dfb\u52a0\u81f3\u6808\u9876\uff09 \\(O(1)\\) pop() \u6808\u9876\u5143\u7d20\u51fa\u6808 \\(O(1)\\) peek() \u8bbf\u95ee\u6808\u9876\u5143\u7d20 \\(O(1)\\)

        \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u5185\u7f6e\u7684\u6808\u7c7b\u3002\u7136\u800c\uff0c\u67d0\u4e9b\u8bed\u8a00\u53ef\u80fd\u6ca1\u6709\u4e13\u95e8\u63d0\u4f9b\u6808\u7c7b\uff0c\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u5c06\u8be5\u8bed\u8a00\u7684\u201c\u6570\u7ec4\u201d\u6216\u201c\u94fe\u8868\u201d\u5f53\u4f5c\u6808\u6765\u4f7f\u7528\uff0c\u5e76\u5728\u7a0b\u5e8f\u903b\u8f91\u4e0a\u5ffd\u7565\u4e0e\u6808\u65e0\u5173\u7684\u64cd\u4f5c\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby stack.py
        # \u521d\u59cb\u5316\u6808\n# Python \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a list \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nstack: list[int] = []\n\n# \u5143\u7d20\u5165\u6808\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n# \u8bbf\u95ee\u6808\u9876\u5143\u7d20\npeek: int = stack[-1]\n\n# \u5143\u7d20\u51fa\u6808\npop: int = stack.pop()\n\n# \u83b7\u53d6\u6808\u7684\u957f\u5ea6\nsize: int = len(stack)\n\n# \u5224\u65ad\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = len(stack) == 0\n
        stack.cpp
        /* \u521d\u59cb\u5316\u6808 */\nstack<int> stack;\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint top = stack.top();\n\n/* \u5143\u7d20\u51fa\u6808 */\nstack.pop(); // \u65e0\u8fd4\u56de\u503c\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.size();\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool empty = stack.empty();\n
        stack.java
        /* \u521d\u59cb\u5316\u6808 */\nStack<Integer> stack = new Stack<>();\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.peek();\n\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.pop();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.size();\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = stack.isEmpty();\n
        stack.cs
        /* \u521d\u59cb\u5316\u6808 */\nStack<int> stack = new();\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.Push(1);\nstack.Push(3);\nstack.Push(2);\nstack.Push(5);\nstack.Push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.Peek();\n\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.Pop();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.Count;\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = stack.Count == 0;\n
        stack_test.go
        /* \u521d\u59cb\u5316\u6808 */\n// \u5728 Go \u4e2d\uff0c\u63a8\u8350\u5c06 Slice \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nvar stack []int\n\n/* \u5143\u7d20\u5165\u6808 */\nstack = append(stack, 1)\nstack = append(stack, 3)\nstack = append(stack, 2)\nstack = append(stack, 5)\nstack = append(stack, 4)\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npeek := stack[len(stack)-1]\n\n/* \u5143\u7d20\u51fa\u6808 */\npop := stack[len(stack)-1]\nstack = stack[:len(stack)-1]\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nsize := len(stack)\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nisEmpty := len(stack) == 0\n
        stack.swift
        /* \u521d\u59cb\u5316\u6808 */\n// Swift \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nvar stack: [Int] = []\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nlet peek = stack.last!\n\n/* \u5143\u7d20\u51fa\u6808 */\nlet pop = stack.removeLast()\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nlet size = stack.count\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = stack.isEmpty\n
        stack.js
        /* \u521d\u59cb\u5316\u6808 */\n// JavaScript \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nconst stack = [];\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nconst peek = stack[stack.length-1];\n\n/* \u5143\u7d20\u51fa\u6808 */\nconst pop = stack.pop();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nconst size = stack.length;\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nconst is_empty = stack.length === 0;\n
        stack.ts
        /* \u521d\u59cb\u5316\u6808 */\n// TypeScript \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nconst stack: number[] = [];\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nconst peek = stack[stack.length - 1];\n\n/* \u5143\u7d20\u51fa\u6808 */\nconst pop = stack.pop();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nconst size = stack.length;\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nconst is_empty = stack.length === 0;\n
        stack.dart
        /* \u521d\u59cb\u5316\u6808 */\n// Dart \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a List \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nList<int> stack = [];\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.add(1);\nstack.add(3);\nstack.add(2);\nstack.add(5);\nstack.add(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.last;\n\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.removeLast();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.length;\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = stack.isEmpty;\n
        stack.rs
        /* \u521d\u59cb\u5316\u6808 */\n// \u628a Vec \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nlet mut stack: Vec<i32> = Vec::new();\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nlet top = stack.last().unwrap();\n\n/* \u5143\u7d20\u51fa\u6808 */\nlet pop = stack.pop().unwrap();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nlet size = stack.len();\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = stack.is_empty();\n
        stack.c
        // C \u672a\u63d0\u4f9b\u5185\u7f6e\u6808\n
        stack.kt
        /* \u521d\u59cb\u5316\u6808 */\nval stack = Stack<Int>()\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1)\nstack.push(3)\nstack.push(2)\nstack.push(5)\nstack.push(4)\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nval peek = stack.peek()\n\n/* \u5143\u7d20\u51fa\u6808 */\nval pop = stack.pop()\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nval size = stack.size\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nval isEmpty = stack.isEmpty()\n
        stack.rb
        # \u521d\u59cb\u5316\u6808\n# Ruby \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nstack = []\n\n# \u5143\u7d20\u5165\u6808\nstack << 1\nstack << 3\nstack << 2\nstack << 5\nstack << 4\n\n# \u8bbf\u95ee\u6808\u9876\u5143\u7d20\npeek = stack.last\n\n# \u5143\u7d20\u51fa\u6808\npop = stack.pop\n\n# \u83b7\u53d6\u6808\u7684\u957f\u5ea6\nsize = stack.length\n\n# \u5224\u65ad\u662f\u5426\u4e3a\u7a7a\nis_empty = stack.empty?\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_stack_and_queue/stack/#512","title":"5.1.2 \u00a0 \u6808\u7684\u5b9e\u73b0","text":"

        \u4e3a\u4e86\u6df1\u5165\u4e86\u89e3\u6808\u7684\u8fd0\u884c\u673a\u5236\uff0c\u6211\u4eec\u6765\u5c1d\u8bd5\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2a\u6808\u7c7b\u3002

        \u6808\u9075\u5faa\u5148\u5165\u540e\u51fa\u7684\u539f\u5219\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u80fd\u5728\u6808\u9876\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\u3002\u7136\u800c\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u90fd\u53ef\u4ee5\u5728\u4efb\u610f\u4f4d\u7f6e\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\uff0c\u56e0\u6b64\u6808\u53ef\u4ee5\u89c6\u4e3a\u4e00\u79cd\u53d7\u9650\u5236\u7684\u6570\u7ec4\u6216\u94fe\u8868\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u201c\u5c4f\u853d\u201d\u6570\u7ec4\u6216\u94fe\u8868\u7684\u90e8\u5206\u65e0\u5173\u64cd\u4f5c\uff0c\u4f7f\u5176\u5bf9\u5916\u8868\u73b0\u7684\u903b\u8f91\u7b26\u5408\u6808\u7684\u7279\u6027\u3002

        "},{"location":"chapter_stack_and_queue/stack/#1","title":"1. \u00a0 \u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0","text":"

        \u4f7f\u7528\u94fe\u8868\u5b9e\u73b0\u6808\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u94fe\u8868\u7684\u5934\u8282\u70b9\u89c6\u4e3a\u6808\u9876\uff0c\u5c3e\u8282\u70b9\u89c6\u4e3a\u6808\u5e95\u3002

        \u5982\u56fe 5-2 \u6240\u793a\uff0c\u5bf9\u4e8e\u5165\u6808\u64cd\u4f5c\uff0c\u6211\u4eec\u53ea\u9700\u5c06\u5143\u7d20\u63d2\u5165\u94fe\u8868\u5934\u90e8\uff0c\u8fd9\u79cd\u8282\u70b9\u63d2\u5165\u65b9\u6cd5\u88ab\u79f0\u4e3a\u201c\u5934\u63d2\u6cd5\u201d\u3002\u800c\u5bf9\u4e8e\u51fa\u6808\u64cd\u4f5c\uff0c\u53ea\u9700\u5c06\u5934\u8282\u70b9\u4ece\u94fe\u8868\u4e2d\u5220\u9664\u5373\u53ef\u3002

        LinkedListStackpush()pop()

        \u56fe 5-2 \u00a0 \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u6808\u7684\u5165\u6808\u51fa\u6808\u64cd\u4f5c

        \u4ee5\u4e0b\u662f\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u6808\u7684\u793a\u4f8b\u4ee3\u7801\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linkedlist_stack.py
        class LinkedListStack:\n    \"\"\"\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\"\"\"\n\n    def __init__(self):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        self._peek: ListNode | None = None\n        self._size: int = 0\n\n    def size(self) -> int:\n        \"\"\"\u83b7\u53d6\u6808\u7684\u957f\u5ea6\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\"\"\"\n        return self._size == 0\n\n    def push(self, val: int):\n        \"\"\"\u5165\u6808\"\"\"\n        node = ListNode(val)\n        node.next = self._peek\n        self._peek = node\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"\u51fa\u6808\"\"\"\n        num = self.peek()\n        self._peek = self._peek.next\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"\u8bbf\u95ee\u6808\u9876\u5143\u7d20\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u6808\u4e3a\u7a7a\")\n        return self._peek.val\n\n    def to_list(self) -> list[int]:\n        \"\"\"\u8f6c\u5316\u4e3a\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n        arr = []\n        node = self._peek\n        while node:\n            arr.append(node.val)\n            node = node.next\n        arr.reverse()\n        return arr\n
        linkedlist_stack.cpp
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n  private:\n    ListNode *stackTop; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n    int stkSize;        // \u6808\u7684\u957f\u5ea6\n\n  public:\n    LinkedListStack() {\n        stackTop = nullptr;\n        stkSize = 0;\n    }\n\n    ~LinkedListStack() {\n        // \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\n        freeMemoryLinkedList(stackTop);\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    int size() {\n        return stkSize;\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    bool isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u5165\u6808 */\n    void push(int num) {\n        ListNode *node = new ListNode(num);\n        node->next = stackTop;\n        stackTop = node;\n        stkSize++;\n    }\n\n    /* \u51fa\u6808 */\n    int pop() {\n        int num = top();\n        ListNode *tmp = stackTop;\n        stackTop = stackTop->next;\n        // \u91ca\u653e\u5185\u5b58\n        delete tmp;\n        stkSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    int top() {\n        if (isEmpty())\n            throw out_of_range(\"\u6808\u4e3a\u7a7a\");\n        return stackTop->val;\n    }\n\n    /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    vector<int> toVector() {\n        ListNode *node = stackTop;\n        vector<int> res(size());\n        for (int i = res.size() - 1; i >= 0; i--) {\n            res[i] = node->val;\n            node = node->next;\n        }\n        return res;\n    }\n};\n
        linkedlist_stack.java
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n    private ListNode stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n    private int stkSize = 0; // \u6808\u7684\u957f\u5ea6\n\n    public LinkedListStack() {\n        stackPeek = null;\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    public int size() {\n        return stkSize;\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u5165\u6808 */\n    public void push(int num) {\n        ListNode node = new ListNode(num);\n        node.next = stackPeek;\n        stackPeek = node;\n        stkSize++;\n    }\n\n    /* \u51fa\u6808 */\n    public int pop() {\n        int num = peek();\n        stackPeek = stackPeek.next;\n        stkSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return stackPeek.val;\n    }\n\n    /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    public int[] toArray() {\n        ListNode node = stackPeek;\n        int[] res = new int[size()];\n        for (int i = res.length - 1; i >= 0; i--) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_stack.cs
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n    ListNode? stackPeek;  // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n    int stkSize = 0;   // \u6808\u7684\u957f\u5ea6\n\n    public LinkedListStack() {\n        stackPeek = null;\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    public int Size() {\n        return stkSize;\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    public bool IsEmpty() {\n        return Size() == 0;\n    }\n\n    /* \u5165\u6808 */\n    public void Push(int num) {\n        ListNode node = new(num) {\n            next = stackPeek\n        };\n        stackPeek = node;\n        stkSize++;\n    }\n\n    /* \u51fa\u6808 */\n    public int Pop() {\n        int num = Peek();\n        stackPeek = stackPeek!.next;\n        stkSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    public int Peek() {\n        if (IsEmpty())\n            throw new Exception();\n        return stackPeek!.val;\n    }\n\n    /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    public int[] ToArray() {\n        if (stackPeek == null)\n            return [];\n\n        ListNode? node = stackPeek;\n        int[] res = new int[Size()];\n        for (int i = res.Length - 1; i >= 0; i--) {\n            res[i] = node!.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_stack.go
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\ntype linkedListStack struct {\n    // \u4f7f\u7528\u5185\u7f6e\u5305 list \u6765\u5b9e\u73b0\u6808\n    data *list.List\n}\n\n/* \u521d\u59cb\u5316\u6808 */\nfunc newLinkedListStack() *linkedListStack {\n    return &linkedListStack{\n        data: list.New(),\n    }\n}\n\n/* \u5165\u6808 */\nfunc (s *linkedListStack) push(value int) {\n    s.data.PushBack(value)\n}\n\n/* \u51fa\u6808 */\nfunc (s *linkedListStack) pop() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Back()\n    s.data.Remove(e)\n    return e.Value\n}\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nfunc (s *linkedListStack) peek() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Back()\n    return e.Value\n}\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nfunc (s *linkedListStack) size() int {\n    return s.data.Len()\n}\n\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListStack) isEmpty() bool {\n    return s.data.Len() == 0\n}\n\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListStack) toList() *list.List {\n    return s.data\n}\n
        linkedlist_stack.swift
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n    private var _peek: ListNode? // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n    private var _size: Int // \u6808\u7684\u957f\u5ea6\n\n    init() {\n        _size = 0\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    func size() -> Int {\n        _size\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* \u5165\u6808 */\n    func push(num: Int) {\n        let node = ListNode(x: num)\n        node.next = _peek\n        _peek = node\n        _size += 1\n    }\n\n    /* \u51fa\u6808 */\n    @discardableResult\n    func pop() -> Int {\n        let num = peek()\n        _peek = _peek?.next\n        _size -= 1\n        return num\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    func peek() -> Int {\n        if isEmpty() {\n            fatalError(\"\u6808\u4e3a\u7a7a\")\n        }\n        return _peek!.val\n    }\n\n    /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    func toArray() -> [Int] {\n        var node = _peek\n        var res = Array(repeating: 0, count: size())\n        for i in res.indices.reversed() {\n            res[i] = node!.val\n            node = node?.next\n        }\n        return res\n    }\n}\n
        linkedlist_stack.js
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n    #stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n    #stkSize = 0; // \u6808\u7684\u957f\u5ea6\n\n    constructor() {\n        this.#stackPeek = null;\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    get size() {\n        return this.#stkSize;\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    isEmpty() {\n        return this.size === 0;\n    }\n\n    /* \u5165\u6808 */\n    push(num) {\n        const node = new ListNode(num);\n        node.next = this.#stackPeek;\n        this.#stackPeek = node;\n        this.#stkSize++;\n    }\n\n    /* \u51fa\u6808 */\n    pop() {\n        const num = this.peek();\n        this.#stackPeek = this.#stackPeek.next;\n        this.#stkSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    peek() {\n        if (!this.#stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\n        return this.#stackPeek.val;\n    }\n\n    /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    toArray() {\n        let node = this.#stackPeek;\n        const res = new Array(this.size);\n        for (let i = res.length - 1; i >= 0; i--) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_stack.ts
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n    private stackPeek: ListNode | null; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n    private stkSize: number = 0; // \u6808\u7684\u957f\u5ea6\n\n    constructor() {\n        this.stackPeek = null;\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    get size(): number {\n        return this.stkSize;\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    isEmpty(): boolean {\n        return this.size === 0;\n    }\n\n    /* \u5165\u6808 */\n    push(num: number): void {\n        const node = new ListNode(num);\n        node.next = this.stackPeek;\n        this.stackPeek = node;\n        this.stkSize++;\n    }\n\n    /* \u51fa\u6808 */\n    pop(): number {\n        const num = this.peek();\n        if (!this.stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\n        this.stackPeek = this.stackPeek.next;\n        this.stkSize--;\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    peek(): number {\n        if (!this.stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\n        return this.stackPeek.val;\n    }\n\n    /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    toArray(): number[] {\n        let node = this.stackPeek;\n        const res = new Array<number>(this.size);\n        for (let i = res.length - 1; i >= 0; i--) {\n            res[i] = node!.val;\n            node = node!.next;\n        }\n        return res;\n    }\n}\n
        linkedlist_stack.dart
        /* \u57fa\u4e8e\u94fe\u8868\u7c7b\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n  ListNode? _stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n  int _stkSize = 0; // \u6808\u7684\u957f\u5ea6\n\n  LinkedListStack() {\n    _stackPeek = null;\n  }\n\n  /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n  int size() {\n    return _stkSize;\n  }\n\n  /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n  bool isEmpty() {\n    return _stkSize == 0;\n  }\n\n  /* \u5165\u6808 */\n  void push(int _num) {\n    final ListNode node = ListNode(_num);\n    node.next = _stackPeek;\n    _stackPeek = node;\n    _stkSize++;\n  }\n\n  /* \u51fa\u6808 */\n  int pop() {\n    final int _num = peek();\n    _stackPeek = _stackPeek!.next;\n    _stkSize--;\n    return _num;\n  }\n\n  /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n  int peek() {\n    if (_stackPeek == null) {\n      throw Exception(\"\u6808\u4e3a\u7a7a\");\n    }\n    return _stackPeek!.val;\n  }\n\n  /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a List \u5e76\u8fd4\u56de */\n  List<int> toList() {\n    ListNode? node = _stackPeek;\n    List<int> list = [];\n    while (node != null) {\n      list.add(node.val);\n      node = node.next;\n    }\n    list = list.reversed.toList();\n    return list;\n  }\n}\n
        linkedlist_stack.rs
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\n#[allow(dead_code)]\npub struct LinkedListStack<T> {\n    stack_peek: Option<Rc<RefCell<ListNode<T>>>>, // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n    stk_size: usize,                              // \u6808\u7684\u957f\u5ea6\n}\n\nimpl<T: Copy> LinkedListStack<T> {\n    pub fn new() -> Self {\n        Self {\n            stack_peek: None,\n            stk_size: 0,\n        }\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    pub fn size(&self) -> usize {\n        return self.stk_size;\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    pub fn is_empty(&self) -> bool {\n        return self.size() == 0;\n    }\n\n    /* \u5165\u6808 */\n    pub fn push(&mut self, num: T) {\n        let node = ListNode::new(num);\n        node.borrow_mut().next = self.stack_peek.take();\n        self.stack_peek = Some(node);\n        self.stk_size += 1;\n    }\n\n    /* \u51fa\u6808 */\n    pub fn pop(&mut self) -> Option<T> {\n        self.stack_peek.take().map(|old_head| {\n            self.stack_peek = old_head.borrow_mut().next.take();\n            self.stk_size -= 1;\n\n            old_head.borrow().val\n        })\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n        self.stack_peek.as_ref()\n    }\n\n    /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    pub fn to_array(&self) -> Vec<T> {\n        fn _to_array<T: Sized + Copy>(head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n            if let Some(node) = head {\n                let mut nums = _to_array(node.borrow().next.as_ref());\n                nums.push(node.borrow().val);\n                return nums;\n            }\n            return Vec::new();\n        }\n\n        _to_array(self.peek())\n    }\n}\n
        linkedlist_stack.c
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\ntypedef struct {\n    ListNode *top; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n    int size;      // \u6808\u7684\u957f\u5ea6\n} LinkedListStack;\n\n/* \u6784\u9020\u51fd\u6570 */\nLinkedListStack *newLinkedListStack() {\n    LinkedListStack *s = malloc(sizeof(LinkedListStack));\n    s->top = NULL;\n    s->size = 0;\n    return s;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListStack(LinkedListStack *s) {\n    while (s->top) {\n        ListNode *n = s->top->next;\n        free(s->top);\n        s->top = n;\n    }\n    free(s);\n}\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size(LinkedListStack *s) {\n    return s->size;\n}\n\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty(LinkedListStack *s) {\n    return size(s) == 0;\n}\n\n/* \u5165\u6808 */\nvoid push(LinkedListStack *s, int num) {\n    ListNode *node = (ListNode *)malloc(sizeof(ListNode));\n    node->next = s->top; // \u66f4\u65b0\u65b0\u52a0\u8282\u70b9\u6307\u9488\u57df\n    node->val = num;     // \u66f4\u65b0\u65b0\u52a0\u8282\u70b9\u6570\u636e\u57df\n    s->top = node;       // \u66f4\u65b0\u6808\u9876\n    s->size++;           // \u66f4\u65b0\u6808\u5927\u5c0f\n}\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek(LinkedListStack *s) {\n    if (s->size == 0) {\n        printf(\"\u6808\u4e3a\u7a7a\\n\");\n        return INT_MAX;\n    }\n    return s->top->val;\n}\n\n/* \u51fa\u6808 */\nint pop(LinkedListStack *s) {\n    int val = peek(s);\n    ListNode *tmp = s->top;\n    s->top = s->top->next;\n    // \u91ca\u653e\u5185\u5b58\n    free(tmp);\n    s->size--;\n    return val;\n}\n
        linkedlist_stack.kt
        /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack(\n    private var stackPeek: ListNode? = null, // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n    private var stkSize: Int = 0 // \u6808\u7684\u957f\u5ea6\n) {\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    fun size(): Int {\n        return stkSize\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    fun isEmpty(): Boolean {\n        return size() == 0\n    }\n\n    /* \u5165\u6808 */\n    fun push(num: Int) {\n        val node = ListNode(num)\n        node.next = stackPeek\n        stackPeek = node\n        stkSize++\n    }\n\n    /* \u51fa\u6808 */\n    fun pop(): Int? {\n        val num = peek()\n        stackPeek = stackPeek?.next\n        stkSize--\n        return num\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    fun peek(): Int? {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return stackPeek?._val\n    }\n\n    /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    fun toArray(): IntArray {\n        var node = stackPeek\n        val res = IntArray(size())\n        for (i in res.size - 1 downTo 0) {\n            res[i] = node?._val!!\n            node = node.next\n        }\n        return res\n    }\n}\n
        linkedlist_stack.rb
        ### \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 ###\nclass LinkedListStack\n  attr_reader :size\n\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize\n    @size = 0\n  end\n\n  ### \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a ###\n  def is_empty?\n    @peek.nil?\n  end\n\n  ### \u5165\u6808 ###\n  def push(val)\n    node = ListNode.new(val)\n    node.next = @peek\n    @peek = node\n    @size += 1\n  end\n\n  ### \u51fa\u6808 ###\n  def pop\n    num = peek\n    @peek = @peek.next\n    @size -= 1\n    num\n  end\n\n  ### \u8bbf\u95ee\u6808\u9876\u5143\u7d20 ###\n  def peek\n    raise IndexError, '\u6808\u4e3a\u7a7a' if is_empty?\n\n    @peek.val\n  end\n\n  ### \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u53cd\u56de ###\n  def to_array\n    arr = []\n    node = @peek\n    while node\n      arr << node.val\n      node = node.next\n    end\n    arr.reverse\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_stack_and_queue/stack/#2","title":"2. \u00a0 \u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"

        \u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u6808\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u7684\u5c3e\u90e8\u4f5c\u4e3a\u6808\u9876\u3002\u5982\u56fe 5-3 \u6240\u793a\uff0c\u5165\u6808\u4e0e\u51fa\u6808\u64cd\u4f5c\u5206\u522b\u5bf9\u5e94\u5728\u6570\u7ec4\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u4e0e\u5220\u9664\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(1)\\) \u3002

        ArrayStackpush()pop()

        \u56fe 5-3 \u00a0 \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u6808\u7684\u5165\u6808\u51fa\u6808\u64cd\u4f5c

        \u7531\u4e8e\u5165\u6808\u7684\u5143\u7d20\u53ef\u80fd\u4f1a\u6e90\u6e90\u4e0d\u65ad\u5730\u589e\u52a0\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\uff0c\u8fd9\u6837\u5c31\u65e0\u987b\u81ea\u884c\u5904\u7406\u6570\u7ec4\u6269\u5bb9\u95ee\u9898\u3002\u4ee5\u4e0b\u4e3a\u793a\u4f8b\u4ee3\u7801\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_stack.py
        class ArrayStack:\n    \"\"\"\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\"\"\"\n\n    def __init__(self):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        self._stack: list[int] = []\n\n    def size(self) -> int:\n        \"\"\"\u83b7\u53d6\u6808\u7684\u957f\u5ea6\"\"\"\n        return len(self._stack)\n\n    def is_empty(self) -> bool:\n        \"\"\"\u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\"\"\"\n        return self.size() == 0\n\n    def push(self, item: int):\n        \"\"\"\u5165\u6808\"\"\"\n        self._stack.append(item)\n\n    def pop(self) -> int:\n        \"\"\"\u51fa\u6808\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u6808\u4e3a\u7a7a\")\n        return self._stack.pop()\n\n    def peek(self) -> int:\n        \"\"\"\u8bbf\u95ee\u6808\u9876\u5143\u7d20\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u6808\u4e3a\u7a7a\")\n        return self._stack[-1]\n\n    def to_list(self) -> list[int]:\n        \"\"\"\u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n        return self._stack\n
        array_stack.cpp
        /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n  private:\n    vector<int> stack;\n\n  public:\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    int size() {\n        return stack.size();\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    bool isEmpty() {\n        return stack.size() == 0;\n    }\n\n    /* \u5165\u6808 */\n    void push(int num) {\n        stack.push_back(num);\n    }\n\n    /* \u51fa\u6808 */\n    int pop() {\n        int num = top();\n        stack.pop_back();\n        return num;\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    int top() {\n        if (isEmpty())\n            throw out_of_range(\"\u6808\u4e3a\u7a7a\");\n        return stack.back();\n    }\n\n    /* \u8fd4\u56de Vector */\n    vector<int> toVector() {\n        return stack;\n    }\n};\n
        array_stack.java
        /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n    private ArrayList<Integer> stack;\n\n    public ArrayStack() {\n        // \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\n        stack = new ArrayList<>();\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    public int size() {\n        return stack.size();\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u5165\u6808 */\n    public void push(int num) {\n        stack.add(num);\n    }\n\n    /* \u51fa\u6808 */\n    public int pop() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return stack.remove(size() - 1);\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return stack.get(size() - 1);\n    }\n\n    /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    public Object[] toArray() {\n        return stack.toArray();\n    }\n}\n
        array_stack.cs
        /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n    List<int> stack;\n    public ArrayStack() {\n        // \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\n        stack = [];\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    public int Size() {\n        return stack.Count;\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    public bool IsEmpty() {\n        return Size() == 0;\n    }\n\n    /* \u5165\u6808 */\n    public void Push(int num) {\n        stack.Add(num);\n    }\n\n    /* \u51fa\u6808 */\n    public int Pop() {\n        if (IsEmpty())\n            throw new Exception();\n        var val = Peek();\n        stack.RemoveAt(Size() - 1);\n        return val;\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    public int Peek() {\n        if (IsEmpty())\n            throw new Exception();\n        return stack[Size() - 1];\n    }\n\n    /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    public int[] ToArray() {\n        return [.. stack];\n    }\n}\n
        array_stack.go
        /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\ntype arrayStack struct {\n    data []int // \u6570\u636e\n}\n\n/* \u521d\u59cb\u5316\u6808 */\nfunc newArrayStack() *arrayStack {\n    return &arrayStack{\n        // \u8bbe\u7f6e\u6808\u7684\u957f\u5ea6\u4e3a 0\uff0c\u5bb9\u91cf\u4e3a 16\n        data: make([]int, 0, 16),\n    }\n}\n\n/* \u6808\u7684\u957f\u5ea6 */\nfunc (s *arrayStack) size() int {\n    return len(s.data)\n}\n\n/* \u6808\u662f\u5426\u4e3a\u7a7a */\nfunc (s *arrayStack) isEmpty() bool {\n    return s.size() == 0\n}\n\n/* \u5165\u6808 */\nfunc (s *arrayStack) push(v int) {\n    // \u5207\u7247\u4f1a\u81ea\u52a8\u6269\u5bb9\n    s.data = append(s.data, v)\n}\n\n/* \u51fa\u6808 */\nfunc (s *arrayStack) pop() any {\n    val := s.peek()\n    s.data = s.data[:len(s.data)-1]\n    return val\n}\n\n/* \u83b7\u53d6\u6808\u9876\u5143\u7d20 */\nfunc (s *arrayStack) peek() any {\n    if s.isEmpty() {\n        return nil\n    }\n    val := s.data[len(s.data)-1]\n    return val\n}\n\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (s *arrayStack) toSlice() []int {\n    return s.data\n}\n
        array_stack.swift
        /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n    private var stack: [Int]\n\n    init() {\n        // \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\n        stack = []\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    func size() -> Int {\n        stack.count\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    func isEmpty() -> Bool {\n        stack.isEmpty\n    }\n\n    /* \u5165\u6808 */\n    func push(num: Int) {\n        stack.append(num)\n    }\n\n    /* \u51fa\u6808 */\n    @discardableResult\n    func pop() -> Int {\n        if isEmpty() {\n            fatalError(\"\u6808\u4e3a\u7a7a\")\n        }\n        return stack.removeLast()\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    func peek() -> Int {\n        if isEmpty() {\n            fatalError(\"\u6808\u4e3a\u7a7a\")\n        }\n        return stack.last!\n    }\n\n    /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    func toArray() -> [Int] {\n        stack\n    }\n}\n
        array_stack.js
        /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n    #stack;\n    constructor() {\n        this.#stack = [];\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    get size() {\n        return this.#stack.length;\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    isEmpty() {\n        return this.#stack.length === 0;\n    }\n\n    /* \u5165\u6808 */\n    push(num) {\n        this.#stack.push(num);\n    }\n\n    /* \u51fa\u6808 */\n    pop() {\n        if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n        return this.#stack.pop();\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    top() {\n        if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n        return this.#stack[this.#stack.length - 1];\n    }\n\n    /* \u8fd4\u56de Array */\n    toArray() {\n        return this.#stack;\n    }\n}\n
        array_stack.ts
        /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n    private stack: number[];\n    constructor() {\n        this.stack = [];\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    get size(): number {\n        return this.stack.length;\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    isEmpty(): boolean {\n        return this.stack.length === 0;\n    }\n\n    /* \u5165\u6808 */\n    push(num: number): void {\n        this.stack.push(num);\n    }\n\n    /* \u51fa\u6808 */\n    pop(): number | undefined {\n        if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n        return this.stack.pop();\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    top(): number | undefined {\n        if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n        return this.stack[this.stack.length - 1];\n    }\n\n    /* \u8fd4\u56de Array */\n    toArray() {\n        return this.stack;\n    }\n}\n
        array_stack.dart
        /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n  late List<int> _stack;\n  ArrayStack() {\n    _stack = [];\n  }\n\n  /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n  int size() {\n    return _stack.length;\n  }\n\n  /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n  bool isEmpty() {\n    return _stack.isEmpty;\n  }\n\n  /* \u5165\u6808 */\n  void push(int _num) {\n    _stack.add(_num);\n  }\n\n  /* \u51fa\u6808 */\n  int pop() {\n    if (isEmpty()) {\n      throw Exception(\"\u6808\u4e3a\u7a7a\");\n    }\n    return _stack.removeLast();\n  }\n\n  /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n  int peek() {\n    if (isEmpty()) {\n      throw Exception(\"\u6808\u4e3a\u7a7a\");\n    }\n    return _stack.last;\n  }\n\n  /* \u5c06\u6808\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n  List<int> toArray() => _stack;\n}\n
        array_stack.rs
        /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nstruct ArrayStack<T> {\n    stack: Vec<T>,\n}\n\nimpl<T> ArrayStack<T> {\n    /* \u521d\u59cb\u5316\u6808 */\n    fn new() -> ArrayStack<T> {\n        ArrayStack::<T> {\n            stack: Vec::<T>::new(),\n        }\n    }\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    fn size(&self) -> usize {\n        self.stack.len()\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    fn is_empty(&self) -> bool {\n        self.size() == 0\n    }\n\n    /* \u5165\u6808 */\n    fn push(&mut self, num: T) {\n        self.stack.push(num);\n    }\n\n    /* \u51fa\u6808 */\n    fn pop(&mut self) -> Option<T> {\n        self.stack.pop()\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    fn peek(&self) -> Option<&T> {\n        if self.is_empty() {\n            panic!(\"\u6808\u4e3a\u7a7a\")\n        };\n        self.stack.last()\n    }\n\n    /* \u8fd4\u56de &Vec */\n    fn to_array(&self) -> &Vec<T> {\n        &self.stack\n    }\n}\n
        array_stack.c
        /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\ntypedef struct {\n    int *data;\n    int size;\n} ArrayStack;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayStack *newArrayStack() {\n    ArrayStack *stack = malloc(sizeof(ArrayStack));\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5927\u5bb9\u91cf\uff0c\u907f\u514d\u6269\u5bb9\n    stack->data = malloc(sizeof(int) * MAX_SIZE);\n    stack->size = 0;\n    return stack;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayStack(ArrayStack *stack) {\n    free(stack->data);\n    free(stack);\n}\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size(ArrayStack *stack) {\n    return stack->size;\n}\n\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty(ArrayStack *stack) {\n    return stack->size == 0;\n}\n\n/* \u5165\u6808 */\nvoid push(ArrayStack *stack, int num) {\n    if (stack->size == MAX_SIZE) {\n        printf(\"\u6808\u5df2\u6ee1\\n\");\n        return;\n    }\n    stack->data[stack->size] = num;\n    stack->size++;\n}\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek(ArrayStack *stack) {\n    if (stack->size == 0) {\n        printf(\"\u6808\u4e3a\u7a7a\\n\");\n        return INT_MAX;\n    }\n    return stack->data[stack->size - 1];\n}\n\n/* \u51fa\u6808 */\nint pop(ArrayStack *stack) {\n    int val = peek(stack);\n    stack->size--;\n    return val;\n}\n
        array_stack.kt
        /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n    // \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\n    private val stack = mutableListOf<Int>()\n\n    /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n    fun size(): Int {\n        return stack.size\n    }\n\n    /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n    fun isEmpty(): Boolean {\n        return size() == 0\n    }\n\n    /* \u5165\u6808 */\n    fun push(num: Int) {\n        stack.add(num)\n    }\n\n    /* \u51fa\u6808 */\n    fun pop(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return stack.removeAt(size() - 1)\n    }\n\n    /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n    fun peek(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return stack[size() - 1]\n    }\n\n    /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n    fun toArray(): Array<Any> {\n        return stack.toTypedArray()\n    }\n}\n
        array_stack.rb
        ### \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 ###\nclass ArrayStack\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize\n    @stack = []\n  end\n\n  ### \u83b7\u53d6\u6808\u7684\u957f\u5ea6 ###\n  def size\n    @stack.length\n  end\n\n  ### \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a ###\n  def is_empty?\n    @stack.empty?\n  end\n\n  ### \u5165\u6808 ###\n  def push(item)\n    @stack << item\n  end\n\n  ### \u51fa\u6808 ###\n  def pop\n    raise IndexError, '\u6808\u4e3a\u7a7a' if is_empty?\n\n    @stack.pop\n  end\n\n  ### \u8bbf\u95ee\u6808\u9876\u5143\u7d20 ###\n  def peek\n    raise IndexError, '\u6808\u4e3a\u7a7a' if is_empty?\n\n    @stack.last\n  end\n\n  ### \u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370 ###\n  def to_array\n    @stack\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_stack_and_queue/stack/#513","title":"5.1.3 \u00a0 \u4e24\u79cd\u5b9e\u73b0\u5bf9\u6bd4","text":"

        \u652f\u6301\u64cd\u4f5c

        \u4e24\u79cd\u5b9e\u73b0\u90fd\u652f\u6301\u6808\u5b9a\u4e49\u4e2d\u7684\u5404\u9879\u64cd\u4f5c\u3002\u6570\u7ec4\u5b9e\u73b0\u989d\u5916\u652f\u6301\u968f\u673a\u8bbf\u95ee\uff0c\u4f46\u8fd9\u5df2\u8d85\u51fa\u4e86\u6808\u7684\u5b9a\u4e49\u8303\u7574\uff0c\u56e0\u6b64\u4e00\u822c\u4e0d\u4f1a\u7528\u5230\u3002

        \u65f6\u95f4\u6548\u7387

        \u5728\u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0\u4e2d\uff0c\u5165\u6808\u548c\u51fa\u6808\u64cd\u4f5c\u90fd\u5728\u9884\u5148\u5206\u914d\u597d\u7684\u8fde\u7eed\u5185\u5b58\u4e2d\u8fdb\u884c\uff0c\u5177\u6709\u5f88\u597d\u7684\u7f13\u5b58\u672c\u5730\u6027\uff0c\u56e0\u6b64\u6548\u7387\u8f83\u9ad8\u3002\u7136\u800c\uff0c\u5982\u679c\u5165\u6808\u65f6\u8d85\u51fa\u6570\u7ec4\u5bb9\u91cf\uff0c\u4f1a\u89e6\u53d1\u6269\u5bb9\u673a\u5236\uff0c\u5bfc\u81f4\u8be5\u6b21\u5165\u6808\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53d8\u4e3a \\(O(n)\\) \u3002

        \u5728\u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0\u4e2d\uff0c\u94fe\u8868\u7684\u6269\u5bb9\u975e\u5e38\u7075\u6d3b\uff0c\u4e0d\u5b58\u5728\u4e0a\u8ff0\u6570\u7ec4\u6269\u5bb9\u65f6\u6548\u7387\u964d\u4f4e\u7684\u95ee\u9898\u3002\u4f46\u662f\uff0c\u5165\u6808\u64cd\u4f5c\u9700\u8981\u521d\u59cb\u5316\u8282\u70b9\u5bf9\u8c61\u5e76\u4fee\u6539\u6307\u9488\uff0c\u56e0\u6b64\u6548\u7387\u76f8\u5bf9\u8f83\u4f4e\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u5165\u6808\u5143\u7d20\u672c\u8eab\u5c31\u662f\u8282\u70b9\u5bf9\u8c61\uff0c\u90a3\u4e48\u53ef\u4ee5\u7701\u53bb\u521d\u59cb\u5316\u6b65\u9aa4\uff0c\u4ece\u800c\u63d0\u9ad8\u6548\u7387\u3002

        \u7efc\u4e0a\u6240\u8ff0\uff0c\u5f53\u5165\u6808\u4e0e\u51fa\u6808\u64cd\u4f5c\u7684\u5143\u7d20\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b\u65f6\uff0c\u4f8b\u5982 int \u6216 double \uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\u3002

        • \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\u5728\u89e6\u53d1\u6269\u5bb9\u65f6\u6548\u7387\u4f1a\u964d\u4f4e\uff0c\u4f46\u7531\u4e8e\u6269\u5bb9\u662f\u4f4e\u9891\u64cd\u4f5c\uff0c\u56e0\u6b64\u5e73\u5747\u6548\u7387\u66f4\u9ad8\u3002
        • \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\u53ef\u4ee5\u63d0\u4f9b\u66f4\u52a0\u7a33\u5b9a\u7684\u6548\u7387\u8868\u73b0\u3002

        \u7a7a\u95f4\u6548\u7387

        \u5728\u521d\u59cb\u5316\u5217\u8868\u65f6\uff0c\u7cfb\u7edf\u4f1a\u4e3a\u5217\u8868\u5206\u914d\u201c\u521d\u59cb\u5bb9\u91cf\u201d\uff0c\u8be5\u5bb9\u91cf\u53ef\u80fd\u8d85\u51fa\u5b9e\u9645\u9700\u6c42\uff1b\u5e76\u4e14\uff0c\u6269\u5bb9\u673a\u5236\u901a\u5e38\u662f\u6309\u7167\u7279\u5b9a\u500d\u7387\uff08\u4f8b\u5982 2 \u500d\uff09\u8fdb\u884c\u6269\u5bb9\u7684\uff0c\u6269\u5bb9\u540e\u7684\u5bb9\u91cf\u4e5f\u53ef\u80fd\u8d85\u51fa\u5b9e\u9645\u9700\u6c42\u3002\u56e0\u6b64\uff0c\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\u53ef\u80fd\u9020\u6210\u4e00\u5b9a\u7684\u7a7a\u95f4\u6d6a\u8d39\u3002

        \u7136\u800c\uff0c\u7531\u4e8e\u94fe\u8868\u8282\u70b9\u9700\u8981\u989d\u5916\u5b58\u50a8\u6307\u9488\uff0c\u56e0\u6b64\u94fe\u8868\u8282\u70b9\u5360\u7528\u7684\u7a7a\u95f4\u76f8\u5bf9\u8f83\u5927\u3002

        \u7efc\u4e0a\uff0c\u6211\u4eec\u4e0d\u80fd\u7b80\u5355\u5730\u786e\u5b9a\u54ea\u79cd\u5b9e\u73b0\u66f4\u52a0\u8282\u7701\u5185\u5b58\uff0c\u9700\u8981\u9488\u5bf9\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u5206\u6790\u3002

        "},{"location":"chapter_stack_and_queue/stack/#514","title":"5.1.4 \u00a0 \u6808\u7684\u5178\u578b\u5e94\u7528","text":"
        • \u6d4f\u89c8\u5668\u4e2d\u7684\u540e\u9000\u4e0e\u524d\u8fdb\u3001\u8f6f\u4ef6\u4e2d\u7684\u64a4\u9500\u4e0e\u53cd\u64a4\u9500\u3002\u6bcf\u5f53\u6211\u4eec\u6253\u5f00\u65b0\u7684\u7f51\u9875\uff0c\u6d4f\u89c8\u5668\u5c31\u4f1a\u5bf9\u4e0a\u4e00\u4e2a\u7f51\u9875\u6267\u884c\u5165\u6808\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u540e\u9000\u64cd\u4f5c\u56de\u5230\u4e0a\u4e00\u4e2a\u7f51\u9875\u3002\u540e\u9000\u64cd\u4f5c\u5b9e\u9645\u4e0a\u662f\u5728\u6267\u884c\u51fa\u6808\u3002\u5982\u679c\u8981\u540c\u65f6\u652f\u6301\u540e\u9000\u548c\u524d\u8fdb\uff0c\u90a3\u4e48\u9700\u8981\u4e24\u4e2a\u6808\u6765\u914d\u5408\u5b9e\u73b0\u3002
        • \u7a0b\u5e8f\u5185\u5b58\u7ba1\u7406\u3002\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u7cfb\u7edf\u90fd\u4f1a\u5728\u6808\u9876\u6dfb\u52a0\u4e00\u4e2a\u6808\u5e27\uff0c\u7528\u4e8e\u8bb0\u5f55\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002\u5728\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u5411\u4e0b\u9012\u63a8\u9636\u6bb5\u4f1a\u4e0d\u65ad\u6267\u884c\u5165\u6808\u64cd\u4f5c\uff0c\u800c\u5411\u4e0a\u56de\u6eaf\u9636\u6bb5\u5219\u4f1a\u4e0d\u65ad\u6267\u884c\u51fa\u6808\u64cd\u4f5c\u3002
        "},{"location":"chapter_stack_and_queue/summary/","title":"5.4 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_stack_and_queue/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u6808\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u540e\u51fa\u539f\u5219\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u901a\u8fc7\u6570\u7ec4\u6216\u94fe\u8868\u6765\u5b9e\u73b0\u3002
        • \u5728\u65f6\u95f4\u6548\u7387\u65b9\u9762\uff0c\u6808\u7684\u6570\u7ec4\u5b9e\u73b0\u5177\u6709\u8f83\u9ad8\u7684\u5e73\u5747\u6548\u7387\uff0c\u4f46\u5728\u6269\u5bb9\u8fc7\u7a0b\u4e2d\uff0c\u5355\u6b21\u5165\u6808\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u52a3\u5316\u81f3 \\(O(n)\\) \u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u6808\u7684\u94fe\u8868\u5b9e\u73b0\u5177\u6709\u66f4\u4e3a\u7a33\u5b9a\u7684\u6548\u7387\u8868\u73b0\u3002
        • \u5728\u7a7a\u95f4\u6548\u7387\u65b9\u9762\uff0c\u6808\u7684\u6570\u7ec4\u5b9e\u73b0\u53ef\u80fd\u5bfc\u81f4\u4e00\u5b9a\u7a0b\u5ea6\u7684\u7a7a\u95f4\u6d6a\u8d39\u3002\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u94fe\u8868\u8282\u70b9\u6240\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u6bd4\u6570\u7ec4\u5143\u7d20\u66f4\u5927\u3002
        • \u961f\u5217\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u5148\u51fa\u539f\u5219\u7684\u6570\u636e\u7ed3\u6784\uff0c\u540c\u6837\u53ef\u4ee5\u901a\u8fc7\u6570\u7ec4\u6216\u94fe\u8868\u6765\u5b9e\u73b0\u3002\u5728\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u7684\u5bf9\u6bd4\u4e0a\uff0c\u961f\u5217\u7684\u7ed3\u8bba\u4e0e\u524d\u8ff0\u6808\u7684\u7ed3\u8bba\u76f8\u4f3c\u3002
        • \u53cc\u5411\u961f\u5217\u662f\u4e00\u79cd\u5177\u6709\u66f4\u9ad8\u81ea\u7531\u5ea6\u7684\u961f\u5217\uff0c\u5b83\u5141\u8bb8\u5728\u4e24\u7aef\u8fdb\u884c\u5143\u7d20\u7684\u6dfb\u52a0\u548c\u5220\u9664\u64cd\u4f5c\u3002
        "},{"location":"chapter_stack_and_queue/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u6d4f\u89c8\u5668\u7684\u524d\u8fdb\u540e\u9000\u662f\u5426\u662f\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\uff1f

        \u6d4f\u89c8\u5668\u7684\u524d\u8fdb\u540e\u9000\u529f\u80fd\u672c\u8d28\u4e0a\u662f\u201c\u6808\u201d\u7684\u4f53\u73b0\u3002\u5f53\u7528\u6237\u8bbf\u95ee\u4e00\u4e2a\u65b0\u9875\u9762\u65f6\uff0c\u8be5\u9875\u9762\u4f1a\u88ab\u6dfb\u52a0\u5230\u6808\u9876\uff1b\u5f53\u7528\u6237\u70b9\u51fb\u540e\u9000\u6309\u94ae\u65f6\uff0c\u8be5\u9875\u9762\u4f1a\u4ece\u6808\u9876\u5f39\u51fa\u3002\u4f7f\u7528\u53cc\u5411\u961f\u5217\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u4e00\u4e9b\u989d\u5916\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u5728\u201c\u53cc\u5411\u961f\u5217\u201d\u7ae0\u8282\u6709\u63d0\u5230\u3002

        Q\uff1a\u5728\u51fa\u6808\u540e\uff0c\u662f\u5426\u9700\u8981\u91ca\u653e\u51fa\u6808\u8282\u70b9\u7684\u5185\u5b58\uff1f

        \u5982\u679c\u540e\u7eed\u4ecd\u9700\u8981\u4f7f\u7528\u5f39\u51fa\u8282\u70b9\uff0c\u5219\u4e0d\u9700\u8981\u91ca\u653e\u5185\u5b58\u3002\u82e5\u4e4b\u540e\u4e0d\u9700\u8981\u7528\u5230\uff0cJava \u548c Python \u7b49\u8bed\u8a00\u62e5\u6709\u81ea\u52a8\u5783\u573e\u56de\u6536\u673a\u5236\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u624b\u52a8\u91ca\u653e\u5185\u5b58\uff1b\u5728 C \u548c C++ \u4e2d\u9700\u8981\u624b\u52a8\u91ca\u653e\u5185\u5b58\u3002

        Q\uff1a\u53cc\u5411\u961f\u5217\u50cf\u662f\u4e24\u4e2a\u6808\u62fc\u63a5\u5728\u4e86\u4e00\u8d77\uff0c\u5b83\u7684\u7528\u9014\u662f\u4ec0\u4e48\uff1f

        \u53cc\u5411\u961f\u5217\u5c31\u50cf\u662f\u6808\u548c\u961f\u5217\u7684\u7ec4\u5408\u6216\u4e24\u4e2a\u6808\u62fc\u5728\u4e86\u4e00\u8d77\u3002\u5b83\u8868\u73b0\u7684\u662f\u6808 + \u961f\u5217\u7684\u903b\u8f91\uff0c\u56e0\u6b64\u53ef\u4ee5\u5b9e\u73b0\u6808\u4e0e\u961f\u5217\u7684\u6240\u6709\u5e94\u7528\uff0c\u5e76\u4e14\u66f4\u52a0\u7075\u6d3b\u3002

        Q\uff1a\u64a4\u9500\uff08undo\uff09\u548c\u53cd\u64a4\u9500\uff08redo\uff09\u5177\u4f53\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1f

        \u4f7f\u7528\u4e24\u4e2a\u6808\uff0c\u6808 A \u7528\u4e8e\u64a4\u9500\uff0c\u6808 B \u7528\u4e8e\u53cd\u64a4\u9500\u3002

        1. \u6bcf\u5f53\u7528\u6237\u6267\u884c\u4e00\u4e2a\u64cd\u4f5c\uff0c\u5c06\u8fd9\u4e2a\u64cd\u4f5c\u538b\u5165\u6808 A \uff0c\u5e76\u6e05\u7a7a\u6808 B \u3002
        2. \u5f53\u7528\u6237\u6267\u884c\u201c\u64a4\u9500\u201d\u65f6\uff0c\u4ece\u6808 A \u4e2d\u5f39\u51fa\u6700\u8fd1\u7684\u64cd\u4f5c\uff0c\u5e76\u5c06\u5176\u538b\u5165\u6808 B \u3002
        3. \u5f53\u7528\u6237\u6267\u884c\u201c\u53cd\u64a4\u9500\u201d\u65f6\uff0c\u4ece\u6808 B \u4e2d\u5f39\u51fa\u6700\u8fd1\u7684\u64cd\u4f5c\uff0c\u5e76\u5c06\u5176\u538b\u5165\u6808 A \u3002
        "},{"location":"chapter_tree/","title":"\u7b2c 7 \u7ae0 \u00a0 \u6811","text":"

        Abstract

        \u53c2\u5929\u5927\u6811\u5145\u6ee1\u751f\u547d\u529b\uff0c\u6839\u6df1\u53f6\u8302\uff0c\u5206\u679d\u6276\u758f\u3002

        \u5b83\u4e3a\u6211\u4eec\u5c55\u73b0\u4e86\u6570\u636e\u5206\u6cbb\u7684\u751f\u52a8\u5f62\u6001\u3002

        "},{"location":"chapter_tree/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
        • 7.1 \u00a0 \u4e8c\u53c9\u6811
        • 7.2 \u00a0 \u4e8c\u53c9\u6811\u904d\u5386
        • 7.3 \u00a0 \u4e8c\u53c9\u6811\u6570\u7ec4\u8868\u793a
        • 7.4 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811
        • 7.5 \u00a0 AVL \u6811 *
        • 7.6 \u00a0 \u5c0f\u7ed3
        "},{"location":"chapter_tree/array_representation_of_tree/","title":"7.3 \u00a0 \u4e8c\u53c9\u6811\u6570\u7ec4\u8868\u793a","text":"

        \u5728\u94fe\u8868\u8868\u793a\u4e0b\uff0c\u4e8c\u53c9\u6811\u7684\u5b58\u50a8\u5355\u5143\u4e3a\u8282\u70b9 TreeNode \uff0c\u8282\u70b9\u4e4b\u95f4\u901a\u8fc7\u6307\u9488\u76f8\u8fde\u63a5\u3002\u4e0a\u4e00\u8282\u4ecb\u7ecd\u4e86\u94fe\u8868\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7684\u5404\u9879\u57fa\u672c\u64cd\u4f5c\u3002

        \u90a3\u4e48\uff0c\u6211\u4eec\u80fd\u5426\u7528\u6570\u7ec4\u6765\u8868\u793a\u4e8c\u53c9\u6811\u5462\uff1f\u7b54\u6848\u662f\u80af\u5b9a\u7684\u3002

        "},{"location":"chapter_tree/array_representation_of_tree/#731","title":"7.3.1 \u00a0 \u8868\u793a\u5b8c\u7f8e\u4e8c\u53c9\u6811","text":"

        \u5148\u5206\u6790\u4e00\u4e2a\u7b80\u5355\u6848\u4f8b\u3002\u7ed9\u5b9a\u4e00\u68f5\u5b8c\u7f8e\u4e8c\u53c9\u6811\uff0c\u6211\u4eec\u5c06\u6240\u6709\u8282\u70b9\u6309\u7167\u5c42\u5e8f\u904d\u5386\u7684\u987a\u5e8f\u5b58\u50a8\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff0c\u5219\u6bcf\u4e2a\u8282\u70b9\u90fd\u5bf9\u5e94\u552f\u4e00\u7684\u6570\u7ec4\u7d22\u5f15\u3002

        \u6839\u636e\u5c42\u5e8f\u904d\u5386\u7684\u7279\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u63a8\u5bfc\u51fa\u7236\u8282\u70b9\u7d22\u5f15\u4e0e\u5b50\u8282\u70b9\u7d22\u5f15\u4e4b\u95f4\u7684\u201c\u6620\u5c04\u516c\u5f0f\u201d\uff1a\u82e5\u67d0\u8282\u70b9\u7684\u7d22\u5f15\u4e3a \\(i\\) \uff0c\u5219\u8be5\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 1\\) \uff0c\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 2\\) \u3002\u56fe 7-12 \u5c55\u793a\u4e86\u5404\u4e2a\u8282\u70b9\u7d22\u5f15\u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb\u3002

        \u56fe 7-12 \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a

        \u6620\u5c04\u516c\u5f0f\u7684\u89d2\u8272\u76f8\u5f53\u4e8e\u94fe\u8868\u4e2d\u7684\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\u3002\u7ed9\u5b9a\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u8282\u70b9\uff0c\u6211\u4eec\u90fd\u53ef\u4ee5\u901a\u8fc7\u6620\u5c04\u516c\u5f0f\u6765\u8bbf\u95ee\u5b83\u7684\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\u3002

        "},{"location":"chapter_tree/array_representation_of_tree/#732","title":"7.3.2 \u00a0 \u8868\u793a\u4efb\u610f\u4e8c\u53c9\u6811","text":"

        \u5b8c\u7f8e\u4e8c\u53c9\u6811\u662f\u4e00\u4e2a\u7279\u4f8b\uff0c\u5728\u4e8c\u53c9\u6811\u7684\u4e2d\u95f4\u5c42\u901a\u5e38\u5b58\u5728\u8bb8\u591a None \u3002\u7531\u4e8e\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5e76\u4e0d\u5305\u542b\u8fd9\u4e9b None \uff0c\u56e0\u6b64\u6211\u4eec\u65e0\u6cd5\u4ec5\u51ed\u8be5\u5e8f\u5217\u6765\u63a8\u6d4b None \u7684\u6570\u91cf\u548c\u5206\u5e03\u4f4d\u7f6e\u3002\u8fd9\u610f\u5473\u7740\u5b58\u5728\u591a\u79cd\u4e8c\u53c9\u6811\u7ed3\u6784\u90fd\u7b26\u5408\u8be5\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u3002

        \u5982\u56fe 7-13 \u6240\u793a\uff0c\u7ed9\u5b9a\u4e00\u68f5\u975e\u5b8c\u7f8e\u4e8c\u53c9\u6811\uff0c\u4e0a\u8ff0\u6570\u7ec4\u8868\u793a\u65b9\u6cd5\u5df2\u7ecf\u5931\u6548\u3002

        \u56fe 7-13 \u00a0 \u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5bf9\u5e94\u591a\u79cd\u4e8c\u53c9\u6811\u53ef\u80fd\u6027

        \u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u5728\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u4e2d\u663e\u5f0f\u5730\u5199\u51fa\u6240\u6709 None \u3002\u5982\u56fe 7-14 \u6240\u793a\uff0c\u8fd9\u6837\u5904\u7406\u540e\uff0c\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5c31\u53ef\u4ee5\u552f\u4e00\u8868\u793a\u4e8c\u53c9\u6811\u4e86\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        # \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a\n# \u4f7f\u7528 None \u6765\u8868\u793a\u7a7a\u4f4d\ntree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]\n
        /* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int \u6700\u5927\u503c INT_MAX \u6807\u8bb0\u7a7a\u4f4d\nvector<int> tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
        /* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int \u7684\u5305\u88c5\u7c7b Integer \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nInteger[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };\n
        /* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nint?[] tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
        /* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 any \u7c7b\u578b\u7684\u5207\u7247, \u5c31\u53ef\u4ee5\u4f7f\u7528 nil \u6765\u6807\u8bb0\u7a7a\u4f4d\ntree := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15}\n
        /* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 Int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 nil \u6765\u6807\u8bb0\u7a7a\u4f4d\nlet tree: [Int?] = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]\n
        /* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 null \u6765\u8868\u793a\u7a7a\u4f4d\nlet tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
        /* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 null \u6765\u8868\u793a\u7a7a\u4f4d\nlet tree: (number | null)[] = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
        /* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nList<int?> tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
        /* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 None \u6765\u6807\u8bb0\u7a7a\u4f4d\nlet tree = [Some(1), Some(2), Some(3), Some(4), None, Some(6), Some(7), Some(8), Some(9), None, None, Some(12), None, None, Some(15)];\n
        /* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int \u6700\u5927\u503c\u6807\u8bb0\u7a7a\u4f4d\uff0c\u56e0\u6b64\u8981\u6c42\u8282\u70b9\u503c\u4e0d\u80fd\u4e3a INT_MAX\nint tree[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
        /* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 null \u6765\u8868\u793a\u7a7a\u4f4d\nval tree = arrayOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 )\n
        ### \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a ###\n# \u4f7f\u7528 nil \u6765\u8868\u793a\u7a7a\u4f4d\ntree = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]\n

        \u56fe 7-14 \u00a0 \u4efb\u610f\u7c7b\u578b\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a

        \u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u5b8c\u5168\u4e8c\u53c9\u6811\u975e\u5e38\u9002\u5408\u4f7f\u7528\u6570\u7ec4\u6765\u8868\u793a\u3002\u56de\u987e\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u5b9a\u4e49\uff0cNone \u53ea\u51fa\u73b0\u5728\u6700\u5e95\u5c42\u4e14\u9760\u53f3\u7684\u4f4d\u7f6e\uff0c\u56e0\u6b64\u6240\u6709 None \u4e00\u5b9a\u51fa\u73b0\u5728\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u7684\u672b\u5c3e\u3002

        \u8fd9\u610f\u5473\u7740\u4f7f\u7528\u6570\u7ec4\u8868\u793a\u5b8c\u5168\u4e8c\u53c9\u6811\u65f6\uff0c\u53ef\u4ee5\u7701\u7565\u5b58\u50a8\u6240\u6709 None \uff0c\u975e\u5e38\u65b9\u4fbf\u3002\u56fe 7-15 \u7ed9\u51fa\u4e86\u4e00\u4e2a\u4f8b\u5b50\u3002

        \u56fe 7-15 \u00a0 \u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a

        \u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u68f5\u57fa\u4e8e\u6570\u7ec4\u8868\u793a\u7684\u4e8c\u53c9\u6811\uff0c\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\u64cd\u4f5c\u3002

        • \u7ed9\u5b9a\u67d0\u8282\u70b9\uff0c\u83b7\u53d6\u5b83\u7684\u503c\u3001\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\u3001\u7236\u8282\u70b9\u3002
        • \u83b7\u53d6\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u3001\u540e\u5e8f\u904d\u5386\u3001\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_binary_tree.py
        class ArrayBinaryTree:\n    \"\"\"\u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b\"\"\"\n\n    def __init__(self, arr: list[int | None]):\n        \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n        self._tree = list(arr)\n\n    def size(self):\n        \"\"\"\u5217\u8868\u5bb9\u91cf\"\"\"\n        return len(self._tree)\n\n    def val(self, i: int) -> int | None:\n        \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c\"\"\"\n        # \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de None \uff0c\u4ee3\u8868\u7a7a\u4f4d\n        if i < 0 or i >= self.size():\n            return None\n        return self._tree[i]\n\n    def left(self, i: int) -> int | None:\n        \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n        return 2 * i + 1\n\n    def right(self, i: int) -> int | None:\n        \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n        return 2 * i + 2\n\n    def parent(self, i: int) -> int | None:\n        \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n        return (i - 1) // 2\n\n    def level_order(self) -> list[int]:\n        \"\"\"\u5c42\u5e8f\u904d\u5386\"\"\"\n        self.res = []\n        # \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n        for i in range(self.size()):\n            if self.val(i) is not None:\n                self.res.append(self.val(i))\n        return self.res\n\n    def dfs(self, i: int, order: str):\n        \"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386\"\"\"\n        if self.val(i) is None:\n            return\n        # \u524d\u5e8f\u904d\u5386\n        if order == \"pre\":\n            self.res.append(self.val(i))\n        self.dfs(self.left(i), order)\n        # \u4e2d\u5e8f\u904d\u5386\n        if order == \"in\":\n            self.res.append(self.val(i))\n        self.dfs(self.right(i), order)\n        # \u540e\u5e8f\u904d\u5386\n        if order == \"post\":\n            self.res.append(self.val(i))\n\n    def pre_order(self) -> list[int]:\n        \"\"\"\u524d\u5e8f\u904d\u5386\"\"\"\n        self.res = []\n        self.dfs(0, order=\"pre\")\n        return self.res\n\n    def in_order(self) -> list[int]:\n        \"\"\"\u4e2d\u5e8f\u904d\u5386\"\"\"\n        self.res = []\n        self.dfs(0, order=\"in\")\n        return self.res\n\n    def post_order(self) -> list[int]:\n        \"\"\"\u540e\u5e8f\u904d\u5386\"\"\"\n        self.res = []\n        self.dfs(0, order=\"post\")\n        return self.res\n
        array_binary_tree.cpp
        /* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n  public:\n    /* \u6784\u9020\u65b9\u6cd5 */\n    ArrayBinaryTree(vector<int> arr) {\n        tree = arr;\n    }\n\n    /* \u5217\u8868\u5bb9\u91cf */\n    int size() {\n        return tree.size();\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n    int val(int i) {\n        // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de INT_MAX \uff0c\u4ee3\u8868\u7a7a\u4f4d\n        if (i < 0 || i >= size())\n            return INT_MAX;\n        return tree[i];\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    int left(int i) {\n        return 2 * i + 1;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    int right(int i) {\n        return 2 * i + 2;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n    int parent(int i) {\n        return (i - 1) / 2;\n    }\n\n    /* \u5c42\u5e8f\u904d\u5386 */\n    vector<int> levelOrder() {\n        vector<int> res;\n        // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n        for (int i = 0; i < size(); i++) {\n            if (val(i) != INT_MAX)\n                res.push_back(val(i));\n        }\n        return res;\n    }\n\n    /* \u524d\u5e8f\u904d\u5386 */\n    vector<int> preOrder() {\n        vector<int> res;\n        dfs(0, \"pre\", res);\n        return res;\n    }\n\n    /* \u4e2d\u5e8f\u904d\u5386 */\n    vector<int> inOrder() {\n        vector<int> res;\n        dfs(0, \"in\", res);\n        return res;\n    }\n\n    /* \u540e\u5e8f\u904d\u5386 */\n    vector<int> postOrder() {\n        vector<int> res;\n        dfs(0, \"post\", res);\n        return res;\n    }\n\n  private:\n    vector<int> tree;\n\n    /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n    void dfs(int i, string order, vector<int> &res) {\n        // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n        if (val(i) == INT_MAX)\n            return;\n        // \u524d\u5e8f\u904d\u5386\n        if (order == \"pre\")\n            res.push_back(val(i));\n        dfs(left(i), order, res);\n        // \u4e2d\u5e8f\u904d\u5386\n        if (order == \"in\")\n            res.push_back(val(i));\n        dfs(right(i), order, res);\n        // \u540e\u5e8f\u904d\u5386\n        if (order == \"post\")\n            res.push_back(val(i));\n    }\n};\n
        array_binary_tree.java
        /* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n    private List<Integer> tree;\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    public ArrayBinaryTree(List<Integer> arr) {\n        tree = new ArrayList<>(arr);\n    }\n\n    /* \u5217\u8868\u5bb9\u91cf */\n    public int size() {\n        return tree.size();\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n    public Integer val(int i) {\n        // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n        if (i < 0 || i >= size())\n            return null;\n        return tree.get(i);\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    public Integer left(int i) {\n        return 2 * i + 1;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    public Integer right(int i) {\n        return 2 * i + 2;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n    public Integer parent(int i) {\n        return (i - 1) / 2;\n    }\n\n    /* \u5c42\u5e8f\u904d\u5386 */\n    public List<Integer> levelOrder() {\n        List<Integer> res = new ArrayList<>();\n        // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n        for (int i = 0; i < size(); i++) {\n            if (val(i) != null)\n                res.add(val(i));\n        }\n        return res;\n    }\n\n    /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n    private void dfs(Integer i, String order, List<Integer> res) {\n        // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n        if (val(i) == null)\n            return;\n        // \u524d\u5e8f\u904d\u5386\n        if (\"pre\".equals(order))\n            res.add(val(i));\n        dfs(left(i), order, res);\n        // \u4e2d\u5e8f\u904d\u5386\n        if (\"in\".equals(order))\n            res.add(val(i));\n        dfs(right(i), order, res);\n        // \u540e\u5e8f\u904d\u5386\n        if (\"post\".equals(order))\n            res.add(val(i));\n    }\n\n    /* \u524d\u5e8f\u904d\u5386 */\n    public List<Integer> preOrder() {\n        List<Integer> res = new ArrayList<>();\n        dfs(0, \"pre\", res);\n        return res;\n    }\n\n    /* \u4e2d\u5e8f\u904d\u5386 */\n    public List<Integer> inOrder() {\n        List<Integer> res = new ArrayList<>();\n        dfs(0, \"in\", res);\n        return res;\n    }\n\n    /* \u540e\u5e8f\u904d\u5386 */\n    public List<Integer> postOrder() {\n        List<Integer> res = new ArrayList<>();\n        dfs(0, \"post\", res);\n        return res;\n    }\n}\n
        array_binary_tree.cs
        /* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree(List<int?> arr) {\n    List<int?> tree = new(arr);\n\n    /* \u5217\u8868\u5bb9\u91cf */\n    public int Size() {\n        return tree.Count;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n    public int? Val(int i) {\n        // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n        if (i < 0 || i >= Size())\n            return null;\n        return tree[i];\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    public int Left(int i) {\n        return 2 * i + 1;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    public int Right(int i) {\n        return 2 * i + 2;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n    public int Parent(int i) {\n        return (i - 1) / 2;\n    }\n\n    /* \u5c42\u5e8f\u904d\u5386 */\n    public List<int> LevelOrder() {\n        List<int> res = [];\n        // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n        for (int i = 0; i < Size(); i++) {\n            if (Val(i).HasValue)\n                res.Add(Val(i)!.Value);\n        }\n        return res;\n    }\n\n    /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n    void DFS(int i, string order, List<int> res) {\n        // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n        if (!Val(i).HasValue)\n            return;\n        // \u524d\u5e8f\u904d\u5386\n        if (order == \"pre\")\n            res.Add(Val(i)!.Value);\n        DFS(Left(i), order, res);\n        // \u4e2d\u5e8f\u904d\u5386\n        if (order == \"in\")\n            res.Add(Val(i)!.Value);\n        DFS(Right(i), order, res);\n        // \u540e\u5e8f\u904d\u5386\n        if (order == \"post\")\n            res.Add(Val(i)!.Value);\n    }\n\n    /* \u524d\u5e8f\u904d\u5386 */\n    public List<int> PreOrder() {\n        List<int> res = [];\n        DFS(0, \"pre\", res);\n        return res;\n    }\n\n    /* \u4e2d\u5e8f\u904d\u5386 */\n    public List<int> InOrder() {\n        List<int> res = [];\n        DFS(0, \"in\", res);\n        return res;\n    }\n\n    /* \u540e\u5e8f\u904d\u5386 */\n    public List<int> PostOrder() {\n        List<int> res = [];\n        DFS(0, \"post\", res);\n        return res;\n    }\n}\n
        array_binary_tree.go
        /* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\ntype arrayBinaryTree struct {\n    tree []any\n}\n\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newArrayBinaryTree(arr []any) *arrayBinaryTree {\n    return &arrayBinaryTree{\n        tree: arr,\n    }\n}\n\n/* \u5217\u8868\u5bb9\u91cf */\nfunc (abt *arrayBinaryTree) size() int {\n    return len(abt.tree)\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nfunc (abt *arrayBinaryTree) val(i int) any {\n    // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n    if i < 0 || i >= abt.size() {\n        return nil\n    }\n    return abt.tree[i]\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) left(i int) int {\n    return 2*i + 1\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) right(i int) int {\n    return 2*i + 2\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) parent(i int) int {\n    return (i - 1) / 2\n}\n\n/* \u5c42\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) levelOrder() []any {\n    var res []any\n    // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n    for i := 0; i < abt.size(); i++ {\n        if abt.val(i) != nil {\n            res = append(res, abt.val(i))\n        }\n    }\n    return res\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nfunc (abt *arrayBinaryTree) dfs(i int, order string, res *[]any) {\n    // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n    if abt.val(i) == nil {\n        return\n    }\n    // \u524d\u5e8f\u904d\u5386\n    if order == \"pre\" {\n        *res = append(*res, abt.val(i))\n    }\n    abt.dfs(abt.left(i), order, res)\n    // \u4e2d\u5e8f\u904d\u5386\n    if order == \"in\" {\n        *res = append(*res, abt.val(i))\n    }\n    abt.dfs(abt.right(i), order, res)\n    // \u540e\u5e8f\u904d\u5386\n    if order == \"post\" {\n        *res = append(*res, abt.val(i))\n    }\n}\n\n/* \u524d\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) preOrder() []any {\n    var res []any\n    abt.dfs(0, \"pre\", &res)\n    return res\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) inOrder() []any {\n    var res []any\n    abt.dfs(0, \"in\", &res)\n    return res\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) postOrder() []any {\n    var res []any\n    abt.dfs(0, \"post\", &res)\n    return res\n}\n
        array_binary_tree.swift
        /* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n    private var tree: [Int?]\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    init(arr: [Int?]) {\n        tree = arr\n    }\n\n    /* \u5217\u8868\u5bb9\u91cf */\n    func size() -> Int {\n        tree.count\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n    func val(i: Int) -> Int? {\n        // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n        if i < 0 || i >= size() {\n            return nil\n        }\n        return tree[i]\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    func left(i: Int) -> Int {\n        2 * i + 1\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    func right(i: Int) -> Int {\n        2 * i + 2\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n    func parent(i: Int) -> Int {\n        (i - 1) / 2\n    }\n\n    /* \u5c42\u5e8f\u904d\u5386 */\n    func levelOrder() -> [Int] {\n        var res: [Int] = []\n        // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n        for i in 0 ..< size() {\n            if let val = val(i: i) {\n                res.append(val)\n            }\n        }\n        return res\n    }\n\n    /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n    private func dfs(i: Int, order: String, res: inout [Int]) {\n        // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n        guard let val = val(i: i) else {\n            return\n        }\n        // \u524d\u5e8f\u904d\u5386\n        if order == \"pre\" {\n            res.append(val)\n        }\n        dfs(i: left(i: i), order: order, res: &res)\n        // \u4e2d\u5e8f\u904d\u5386\n        if order == \"in\" {\n            res.append(val)\n        }\n        dfs(i: right(i: i), order: order, res: &res)\n        // \u540e\u5e8f\u904d\u5386\n        if order == \"post\" {\n            res.append(val)\n        }\n    }\n\n    /* \u524d\u5e8f\u904d\u5386 */\n    func preOrder() -> [Int] {\n        var res: [Int] = []\n        dfs(i: 0, order: \"pre\", res: &res)\n        return res\n    }\n\n    /* \u4e2d\u5e8f\u904d\u5386 */\n    func inOrder() -> [Int] {\n        var res: [Int] = []\n        dfs(i: 0, order: \"in\", res: &res)\n        return res\n    }\n\n    /* \u540e\u5e8f\u904d\u5386 */\n    func postOrder() -> [Int] {\n        var res: [Int] = []\n        dfs(i: 0, order: \"post\", res: &res)\n        return res\n    }\n}\n
        array_binary_tree.js
        /* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n    #tree;\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor(arr) {\n        this.#tree = arr;\n    }\n\n    /* \u5217\u8868\u5bb9\u91cf */\n    size() {\n        return this.#tree.length;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n    val(i) {\n        // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n        if (i < 0 || i >= this.size()) return null;\n        return this.#tree[i];\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    left(i) {\n        return 2 * i + 1;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    right(i) {\n        return 2 * i + 2;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n    parent(i) {\n        return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n    }\n\n    /* \u5c42\u5e8f\u904d\u5386 */\n    levelOrder() {\n        let res = [];\n        // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n        for (let i = 0; i < this.size(); i++) {\n            if (this.val(i) !== null) res.push(this.val(i));\n        }\n        return res;\n    }\n\n    /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n    #dfs(i, order, res) {\n        // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n        if (this.val(i) === null) return;\n        // \u524d\u5e8f\u904d\u5386\n        if (order === 'pre') res.push(this.val(i));\n        this.#dfs(this.left(i), order, res);\n        // \u4e2d\u5e8f\u904d\u5386\n        if (order === 'in') res.push(this.val(i));\n        this.#dfs(this.right(i), order, res);\n        // \u540e\u5e8f\u904d\u5386\n        if (order === 'post') res.push(this.val(i));\n    }\n\n    /* \u524d\u5e8f\u904d\u5386 */\n    preOrder() {\n        const res = [];\n        this.#dfs(0, 'pre', res);\n        return res;\n    }\n\n    /* \u4e2d\u5e8f\u904d\u5386 */\n    inOrder() {\n        const res = [];\n        this.#dfs(0, 'in', res);\n        return res;\n    }\n\n    /* \u540e\u5e8f\u904d\u5386 */\n    postOrder() {\n        const res = [];\n        this.#dfs(0, 'post', res);\n        return res;\n    }\n}\n
        array_binary_tree.ts
        /* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n    #tree: (number | null)[];\n\n    /* \u6784\u9020\u65b9\u6cd5 */\n    constructor(arr: (number | null)[]) {\n        this.#tree = arr;\n    }\n\n    /* \u5217\u8868\u5bb9\u91cf */\n    size(): number {\n        return this.#tree.length;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n    val(i: number): number | null {\n        // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n        if (i < 0 || i >= this.size()) return null;\n        return this.#tree[i];\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    left(i: number): number {\n        return 2 * i + 1;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    right(i: number): number {\n        return 2 * i + 2;\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n    parent(i: number): number {\n        return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n    }\n\n    /* \u5c42\u5e8f\u904d\u5386 */\n    levelOrder(): number[] {\n        let res = [];\n        // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n        for (let i = 0; i < this.size(); i++) {\n            if (this.val(i) !== null) res.push(this.val(i));\n        }\n        return res;\n    }\n\n    /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n    #dfs(i: number, order: Order, res: (number | null)[]): void {\n        // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n        if (this.val(i) === null) return;\n        // \u524d\u5e8f\u904d\u5386\n        if (order === 'pre') res.push(this.val(i));\n        this.#dfs(this.left(i), order, res);\n        // \u4e2d\u5e8f\u904d\u5386\n        if (order === 'in') res.push(this.val(i));\n        this.#dfs(this.right(i), order, res);\n        // \u540e\u5e8f\u904d\u5386\n        if (order === 'post') res.push(this.val(i));\n    }\n\n    /* \u524d\u5e8f\u904d\u5386 */\n    preOrder(): (number | null)[] {\n        const res = [];\n        this.#dfs(0, 'pre', res);\n        return res;\n    }\n\n    /* \u4e2d\u5e8f\u904d\u5386 */\n    inOrder(): (number | null)[] {\n        const res = [];\n        this.#dfs(0, 'in', res);\n        return res;\n    }\n\n    /* \u540e\u5e8f\u904d\u5386 */\n    postOrder(): (number | null)[] {\n        const res = [];\n        this.#dfs(0, 'post', res);\n        return res;\n    }\n}\n
        array_binary_tree.dart
        /* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n  late List<int?> _tree;\n\n  /* \u6784\u9020\u65b9\u6cd5 */\n  ArrayBinaryTree(this._tree);\n\n  /* \u5217\u8868\u5bb9\u91cf */\n  int size() {\n    return _tree.length;\n  }\n\n  /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n  int? val(int i) {\n    // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n    if (i < 0 || i >= size()) {\n      return null;\n    }\n    return _tree[i];\n  }\n\n  /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n  int? left(int i) {\n    return 2 * i + 1;\n  }\n\n  /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n  int? right(int i) {\n    return 2 * i + 2;\n  }\n\n  /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n  int? parent(int i) {\n    return (i - 1) ~/ 2;\n  }\n\n  /* \u5c42\u5e8f\u904d\u5386 */\n  List<int> levelOrder() {\n    List<int> res = [];\n    for (int i = 0; i < size(); i++) {\n      if (val(i) != null) {\n        res.add(val(i)!);\n      }\n    }\n    return res;\n  }\n\n  /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n  void dfs(int i, String order, List<int?> res) {\n    // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n    if (val(i) == null) {\n      return;\n    }\n    // \u524d\u5e8f\u904d\u5386\n    if (order == 'pre') {\n      res.add(val(i));\n    }\n    dfs(left(i)!, order, res);\n    // \u4e2d\u5e8f\u904d\u5386\n    if (order == 'in') {\n      res.add(val(i));\n    }\n    dfs(right(i)!, order, res);\n    // \u540e\u5e8f\u904d\u5386\n    if (order == 'post') {\n      res.add(val(i));\n    }\n  }\n\n  /* \u524d\u5e8f\u904d\u5386 */\n  List<int?> preOrder() {\n    List<int?> res = [];\n    dfs(0, 'pre', res);\n    return res;\n  }\n\n  /* \u4e2d\u5e8f\u904d\u5386 */\n  List<int?> inOrder() {\n    List<int?> res = [];\n    dfs(0, 'in', res);\n    return res;\n  }\n\n  /* \u540e\u5e8f\u904d\u5386 */\n  List<int?> postOrder() {\n    List<int?> res = [];\n    dfs(0, 'post', res);\n    return res;\n  }\n}\n
        array_binary_tree.rs
        /* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nstruct ArrayBinaryTree {\n    tree: Vec<Option<i32>>,\n}\n\nimpl ArrayBinaryTree {\n    /* \u6784\u9020\u65b9\u6cd5 */\n    fn new(arr: Vec<Option<i32>>) -> Self {\n        Self { tree: arr }\n    }\n\n    /* \u5217\u8868\u5bb9\u91cf */\n    fn size(&self) -> i32 {\n        self.tree.len() as i32\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n    fn val(&self, i: i32) -> Option<i32> {\n        // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de None \uff0c\u4ee3\u8868\u7a7a\u4f4d\n        if i < 0 || i >= self.size() {\n            None\n        } else {\n            self.tree[i as usize]\n        }\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    fn left(&self, i: i32) -> i32 {\n        2 * i + 1\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    fn right(&self, i: i32) -> i32 {\n        2 * i + 2\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n    fn parent(&self, i: i32) -> i32 {\n        (i - 1) / 2\n    }\n\n    /* \u5c42\u5e8f\u904d\u5386 */\n    fn level_order(&self) -> Vec<i32> {\n        self.tree.iter().filter_map(|&x| x).collect()\n    }\n\n    /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n    fn dfs(&self, i: i32, order: &'static str, res: &mut Vec<i32>) {\n        if self.val(i).is_none() {\n            return;\n        }\n        let val = self.val(i).unwrap();\n        // \u524d\u5e8f\u904d\u5386\n        if order == \"pre\" {\n            res.push(val);\n        }\n        self.dfs(self.left(i), order, res);\n        // \u4e2d\u5e8f\u904d\u5386\n        if order == \"in\" {\n            res.push(val);\n        }\n        self.dfs(self.right(i), order, res);\n        // \u540e\u5e8f\u904d\u5386\n        if order == \"post\" {\n            res.push(val);\n        }\n    }\n\n    /* \u524d\u5e8f\u904d\u5386 */\n    fn pre_order(&self) -> Vec<i32> {\n        let mut res = vec![];\n        self.dfs(0, \"pre\", &mut res);\n        res\n    }\n\n    /* \u4e2d\u5e8f\u904d\u5386 */\n    fn in_order(&self) -> Vec<i32> {\n        let mut res = vec![];\n        self.dfs(0, \"in\", &mut res);\n        res\n    }\n\n    /* \u540e\u5e8f\u904d\u5386 */\n    fn post_order(&self) -> Vec<i32> {\n        let mut res = vec![];\n        self.dfs(0, \"post\", &mut res);\n        res\n    }\n}\n
        array_binary_tree.c
        /* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7ed3\u6784\u4f53 */\ntypedef struct {\n    int *tree;\n    int size;\n} ArrayBinaryTree;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayBinaryTree *newArrayBinaryTree(int *arr, int arrSize) {\n    ArrayBinaryTree *abt = (ArrayBinaryTree *)malloc(sizeof(ArrayBinaryTree));\n    abt->tree = malloc(sizeof(int) * arrSize);\n    memcpy(abt->tree, arr, sizeof(int) * arrSize);\n    abt->size = arrSize;\n    return abt;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayBinaryTree(ArrayBinaryTree *abt) {\n    free(abt->tree);\n    free(abt);\n}\n\n/* \u5217\u8868\u5bb9\u91cf */\nint size(ArrayBinaryTree *abt) {\n    return abt->size;\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nint val(ArrayBinaryTree *abt, int i) {\n    // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de INT_MAX \uff0c\u4ee3\u8868\u7a7a\u4f4d\n    if (i < 0 || i >= size(abt))\n        return INT_MAX;\n    return abt->tree[i];\n}\n\n/* \u5c42\u5e8f\u904d\u5386 */\nint *levelOrder(ArrayBinaryTree *abt, int *returnSize) {\n    int *res = (int *)malloc(sizeof(int) * size(abt));\n    int index = 0;\n    // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n    for (int i = 0; i < size(abt); i++) {\n        if (val(abt, i) != INT_MAX)\n            res[index++] = val(abt, i);\n    }\n    *returnSize = index;\n    return res;\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nvoid dfs(ArrayBinaryTree *abt, int i, char *order, int *res, int *index) {\n    // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n    if (val(abt, i) == INT_MAX)\n        return;\n    // \u524d\u5e8f\u904d\u5386\n    if (strcmp(order, \"pre\") == 0)\n        res[(*index)++] = val(abt, i);\n    dfs(abt, left(i), order, res, index);\n    // \u4e2d\u5e8f\u904d\u5386\n    if (strcmp(order, \"in\") == 0)\n        res[(*index)++] = val(abt, i);\n    dfs(abt, right(i), order, res, index);\n    // \u540e\u5e8f\u904d\u5386\n    if (strcmp(order, \"post\") == 0)\n        res[(*index)++] = val(abt, i);\n}\n\n/* \u524d\u5e8f\u904d\u5386 */\nint *preOrder(ArrayBinaryTree *abt, int *returnSize) {\n    int *res = (int *)malloc(sizeof(int) * size(abt));\n    int index = 0;\n    dfs(abt, 0, \"pre\", res, &index);\n    *returnSize = index;\n    return res;\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nint *inOrder(ArrayBinaryTree *abt, int *returnSize) {\n    int *res = (int *)malloc(sizeof(int) * size(abt));\n    int index = 0;\n    dfs(abt, 0, \"in\", res, &index);\n    *returnSize = index;\n    return res;\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nint *postOrder(ArrayBinaryTree *abt, int *returnSize) {\n    int *res = (int *)malloc(sizeof(int) * size(abt));\n    int index = 0;\n    dfs(abt, 0, \"post\", res, &index);\n    *returnSize = index;\n    return res;\n}\n
        array_binary_tree.kt
        /* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree(val tree: MutableList<Int?>) {\n    /* \u5217\u8868\u5bb9\u91cf */\n    fun size(): Int {\n        return tree.size\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n    fun _val(i: Int): Int? {\n        // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n        if (i < 0 || i >= size()) return null\n        return tree[i]\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    fun left(i: Int): Int {\n        return 2 * i + 1\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n    fun right(i: Int): Int {\n        return 2 * i + 2\n    }\n\n    /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n    fun parent(i: Int): Int {\n        return (i - 1) / 2\n    }\n\n    /* \u5c42\u5e8f\u904d\u5386 */\n    fun levelOrder(): MutableList<Int?> {\n        val res = mutableListOf<Int?>()\n        // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n        for (i in 0..<size()) {\n            if (_val(i) != null)\n                res.add(_val(i))\n        }\n        return res\n    }\n\n    /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n    fun dfs(i: Int, order: String, res: MutableList<Int?>) {\n        // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n        if (_val(i) == null)\n            return\n        // \u524d\u5e8f\u904d\u5386\n        if (\"pre\" == order)\n            res.add(_val(i))\n        dfs(left(i), order, res)\n        // \u4e2d\u5e8f\u904d\u5386\n        if (\"in\" == order)\n            res.add(_val(i))\n        dfs(right(i), order, res)\n        // \u540e\u5e8f\u904d\u5386\n        if (\"post\" == order)\n            res.add(_val(i))\n    }\n\n    /* \u524d\u5e8f\u904d\u5386 */\n    fun preOrder(): MutableList<Int?> {\n        val res = mutableListOf<Int?>()\n        dfs(0, \"pre\", res)\n        return res\n    }\n\n    /* \u4e2d\u5e8f\u904d\u5386 */\n    fun inOrder(): MutableList<Int?> {\n        val res = mutableListOf<Int?>()\n        dfs(0, \"in\", res)\n        return res\n    }\n\n    /* \u540e\u5e8f\u904d\u5386 */\n    fun postOrder(): MutableList<Int?> {\n        val res = mutableListOf<Int?>()\n        dfs(0, \"post\", res)\n        return res\n    }\n}\n
        array_binary_tree.rb
        ### \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b ###\nclass ArrayBinaryTree\n  ### \u6784\u9020\u65b9\u6cd5 ###\n  def initialize(arr)\n    @tree = arr.to_a\n  end\n\n  ### \u5217\u8868\u5bb9\u91cf ###\n  def size\n    @tree.length\n  end\n\n  ### \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c ###\n  def val(i)\n    # \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de nil \uff0c\u4ee3\u8868\u7a7a\u4f4d\n    return if i < 0 || i >= size\n\n    @tree[i]\n  end\n\n  ### \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 ###\n  def left(i)\n    2 * i + 1\n  end\n\n  ### \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 ###\n  def right(i)\n    2 * i + 2\n  end\n\n  ### \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 ###\n  def parent(i)\n    (i - 1) / 2\n  end\n\n  ### \u5c42\u5e8f\u904d\u5386 ###\n  def level_order\n    @res = []\n\n    # \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n    for i in 0...size\n      @res << val(i) unless val(i).nil?\n    end\n\n    @res\n  end\n\n  ### \u6df1\u5ea6\u4f18\u5148\u904d\u5386 ###\n  def dfs(i, order)\n    return if val(i).nil?\n    # \u524d\u5e8f\u904d\u5386\n    @res << val(i) if order == :pre\n    dfs(left(i), order)\n    # \u4e2d\u5e8f\u904d\u5386\n    @res << val(i) if order == :in\n    dfs(right(i), order)\n    # \u540e\u5e8f\u904d\u5386\n    @res << val(i) if order == :post\n  end\n\n  ### \u524d\u5e8f\u904d\u5386 ###\n  def pre_order\n    @res = []\n    dfs(0, :pre)\n    @res\n  end\n\n  ### \u4e2d\u5e8f\u904d\u5386 ###\n  def in_order\n    @res = []\n    dfs(0, :in)\n    @res\n  end\n\n  ### \u540e\u5e8f\u904d\u5386 ###\n  def post_order\n    @res = []\n    dfs(0, :post)\n    @res\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_tree/array_representation_of_tree/#733","title":"7.3.3 \u00a0 \u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"

        \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a\u4e3b\u8981\u6709\u4ee5\u4e0b\u4f18\u70b9\u3002

        • \u6570\u7ec4\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\uff0c\u5bf9\u7f13\u5b58\u53cb\u597d\uff0c\u8bbf\u95ee\u4e0e\u904d\u5386\u901f\u5ea6\u8f83\u5feb\u3002
        • \u4e0d\u9700\u8981\u5b58\u50a8\u6307\u9488\uff0c\u6bd4\u8f83\u8282\u7701\u7a7a\u95f4\u3002
        • \u5141\u8bb8\u968f\u673a\u8bbf\u95ee\u8282\u70b9\u3002

        \u7136\u800c\uff0c\u6570\u7ec4\u8868\u793a\u4e5f\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027\u3002

        • \u6570\u7ec4\u5b58\u50a8\u9700\u8981\u8fde\u7eed\u5185\u5b58\u7a7a\u95f4\uff0c\u56e0\u6b64\u4e0d\u9002\u5408\u5b58\u50a8\u6570\u636e\u91cf\u8fc7\u5927\u7684\u6811\u3002
        • \u589e\u5220\u8282\u70b9\u9700\u8981\u901a\u8fc7\u6570\u7ec4\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u5b9e\u73b0\uff0c\u6548\u7387\u8f83\u4f4e\u3002
        • \u5f53\u4e8c\u53c9\u6811\u4e2d\u5b58\u5728\u5927\u91cf None \u65f6\uff0c\u6570\u7ec4\u4e2d\u5305\u542b\u7684\u8282\u70b9\u6570\u636e\u6bd4\u91cd\u8f83\u4f4e\uff0c\u7a7a\u95f4\u5229\u7528\u7387\u8f83\u4f4e\u3002
        "},{"location":"chapter_tree/avl_tree/","title":"7.5 \u00a0 AVL \u6811 *","text":"

        \u5728\u201c\u4e8c\u53c9\u641c\u7d22\u6811\u201d\u7ae0\u8282\u4e2d\u6211\u4eec\u63d0\u5230\uff0c\u5728\u591a\u6b21\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u540e\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u53ef\u80fd\u9000\u5316\u4e3a\u94fe\u8868\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5c06\u4ece \\(O(\\log n)\\) \u52a3\u5316\u4e3a \\(O(n)\\) \u3002

        \u5982\u56fe 7-24 \u6240\u793a\uff0c\u7ecf\u8fc7\u4e24\u6b21\u5220\u9664\u8282\u70b9\u64cd\u4f5c\uff0c\u8fd9\u68f5\u4e8c\u53c9\u641c\u7d22\u6811\u4fbf\u4f1a\u9000\u5316\u4e3a\u94fe\u8868\u3002

        \u56fe 7-24 \u00a0 AVL \u6811\u5728\u5220\u9664\u8282\u70b9\u540e\u53d1\u751f\u9000\u5316

        \u518d\u4f8b\u5982\uff0c\u5728\u56fe 7-25 \u6240\u793a\u7684\u5b8c\u7f8e\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e24\u4e2a\u8282\u70b9\u540e\uff0c\u6811\u5c06\u4e25\u91cd\u5411\u5de6\u503e\u659c\uff0c\u67e5\u627e\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u968f\u4e4b\u52a3\u5316\u3002

        \u56fe 7-25 \u00a0 AVL \u6811\u5728\u63d2\u5165\u8282\u70b9\u540e\u53d1\u751f\u9000\u5316

        1962 \u5e74 G. M. Adelson-Velsky \u548c E. M. Landis \u5728\u8bba\u6587\u201cAn algorithm for the organization of information\u201d\u4e2d\u63d0\u51fa\u4e86 AVL \u6811\u3002\u8bba\u6587\u4e2d\u8be6\u7ec6\u63cf\u8ff0\u4e86\u4e00\u7cfb\u5217\u64cd\u4f5c\uff0c\u786e\u4fdd\u5728\u6301\u7eed\u6dfb\u52a0\u548c\u5220\u9664\u8282\u70b9\u540e\uff0cAVL \u6811\u4e0d\u4f1a\u9000\u5316\uff0c\u4ece\u800c\u4f7f\u5f97\u5404\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4fdd\u6301\u5728 \\(O(\\log n)\\) \u7ea7\u522b\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5728\u9700\u8981\u9891\u7e41\u8fdb\u884c\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u7684\u573a\u666f\u4e2d\uff0cAVL \u6811\u80fd\u59cb\u7ec8\u4fdd\u6301\u9ad8\u6548\u7684\u6570\u636e\u64cd\u4f5c\u6027\u80fd\uff0c\u5177\u6709\u5f88\u597d\u7684\u5e94\u7528\u4ef7\u503c\u3002

        "},{"location":"chapter_tree/avl_tree/#751-avl","title":"7.5.1 \u00a0 AVL \u6811\u5e38\u89c1\u672f\u8bed","text":"

        AVL \u6811\u65e2\u662f\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u4e5f\u662f\u5e73\u8861\u4e8c\u53c9\u6811\uff0c\u540c\u65f6\u6ee1\u8db3\u8fd9\u4e24\u7c7b\u4e8c\u53c9\u6811\u7684\u6240\u6709\u6027\u8d28\uff0c\u56e0\u6b64\u662f\u4e00\u79cd\u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811\uff08balanced binary search tree\uff09\u3002

        "},{"location":"chapter_tree/avl_tree/#1","title":"1. \u00a0 \u8282\u70b9\u9ad8\u5ea6","text":"

        \u7531\u4e8e AVL \u6811\u7684\u76f8\u5173\u64cd\u4f5c\u9700\u8981\u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u4e3a\u8282\u70b9\u7c7b\u6dfb\u52a0 height \u53d8\u91cf\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        class TreeNode:\n    \"\"\"AVL \u6811\u8282\u70b9\u7c7b\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val                 # \u8282\u70b9\u503c\n        self.height: int = 0                # \u8282\u70b9\u9ad8\u5ea6\n        self.left: TreeNode | None = None   # \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n        self.right: TreeNode | None = None  # \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n
        /* AVL \u6811\u8282\u70b9\u7c7b */\nstruct TreeNode {\n    int val{};          // \u8282\u70b9\u503c\n    int height = 0;     // \u8282\u70b9\u9ad8\u5ea6\n    TreeNode *left{};   // \u5de6\u5b50\u8282\u70b9\n    TreeNode *right{};  // \u53f3\u5b50\u8282\u70b9\n    TreeNode() = default;\n    explicit TreeNode(int x) : val(x){}\n};\n
        /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n    public int val;        // \u8282\u70b9\u503c\n    public int height;     // \u8282\u70b9\u9ad8\u5ea6\n    public TreeNode left;  // \u5de6\u5b50\u8282\u70b9\n    public TreeNode right; // \u53f3\u5b50\u8282\u70b9\n    public TreeNode(int x) { val = x; }\n}\n
        /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode(int? x) {\n    public int? val = x;    // \u8282\u70b9\u503c\n    public int height;      // \u8282\u70b9\u9ad8\u5ea6\n    public TreeNode? left;  // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n    public TreeNode? right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n
        /* AVL \u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype TreeNode struct {\n    Val    int       // \u8282\u70b9\u503c\n    Height int       // \u8282\u70b9\u9ad8\u5ea6\n    Left   *TreeNode // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n    Right  *TreeNode // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n
        /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n    var val: Int // \u8282\u70b9\u503c\n    var height: Int // \u8282\u70b9\u9ad8\u5ea6\n    var left: TreeNode? // \u5de6\u5b50\u8282\u70b9\n    var right: TreeNode? // \u53f3\u5b50\u8282\u70b9\n\n    init(x: Int) {\n        val = x\n        height = 0\n    }\n}\n
        /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n    val; // \u8282\u70b9\u503c\n    height; //\u8282\u70b9\u9ad8\u5ea6\n    left; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n    right; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n    constructor(val, left, right, height) {\n        this.val = val === undefined ? 0 : val;\n        this.height = height === undefined ? 0 : height;\n        this.left = left === undefined ? null : left;\n        this.right = right === undefined ? null : right;\n    }\n}\n
        /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n    val: number;            // \u8282\u70b9\u503c\n    height: number;         // \u8282\u70b9\u9ad8\u5ea6\n    left: TreeNode | null;  // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n    right: TreeNode | null; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n    constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {\n        this.val = val === undefined ? 0 : val;\n        this.height = height === undefined ? 0 : height;\n        this.left = left === undefined ? null : left;\n        this.right = right === undefined ? null : right;\n    }\n}\n
        /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n  int val;         // \u8282\u70b9\u503c\n  int height;      // \u8282\u70b9\u9ad8\u5ea6\n  TreeNode? left;  // \u5de6\u5b50\u8282\u70b9\n  TreeNode? right; // \u53f3\u5b50\u8282\u70b9\n  TreeNode(this.val, [this.height = 0, this.left, this.right]);\n}\n
        use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* AVL \u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\n    val: i32,                               // \u8282\u70b9\u503c\n    height: i32,                            // \u8282\u70b9\u9ad8\u5ea6\n    left: Option<Rc<RefCell<TreeNode>>>,    // \u5de6\u5b50\u8282\u70b9\n    right: Option<Rc<RefCell<TreeNode>>>,   // \u53f3\u5b50\u8282\u70b9\n}\n\nimpl TreeNode {\n    /* \u6784\u9020\u65b9\u6cd5 */\n    fn new(val: i32) -> Rc<RefCell<Self>> {\n        Rc::new(RefCell::new(Self {\n            val,\n            height: 0,\n            left: None,\n            right: None\n        }))\n    }\n}\n
        /* AVL \u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\ntypedef struct TreeNode {\n    int val;\n    int height;\n    struct TreeNode *left;\n    struct TreeNode *right;\n} TreeNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nTreeNode *newTreeNode(int val) {\n    TreeNode *node;\n\n    node = (TreeNode *)malloc(sizeof(TreeNode));\n    node->val = val;\n    node->height = 0;\n    node->left = NULL;\n    node->right = NULL;\n    return node;\n}\n
        /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode(val _val: Int) {  // \u8282\u70b9\u503c\n    val height: Int = 0          // \u8282\u70b9\u9ad8\u5ea6\n    val left: TreeNode? = null   // \u5de6\u5b50\u8282\u70b9\n    val right: TreeNode? = null  // \u53f3\u5b50\u8282\u70b9\n}\n
        ### AVL \u6811\u8282\u70b9\u7c7b ###\nclass TreeNode\n  attr_accessor :val    # \u8282\u70b9\u503c\n  attr_accessor :height # \u8282\u70b9\u9ad8\u5ea6\n  attr_accessor :left   # \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n  attr_accessor :right  # \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n\n  def initialize(val)\n    @val = val\n    @height = 0\n  end\nend\n

        \u201c\u8282\u70b9\u9ad8\u5ea6\u201d\u662f\u6307\u4ece\u8be5\u8282\u70b9\u5230\u5b83\u7684\u6700\u8fdc\u53f6\u8282\u70b9\u7684\u8ddd\u79bb\uff0c\u5373\u6240\u7ecf\u8fc7\u7684\u201c\u8fb9\u201d\u7684\u6570\u91cf\u3002\u9700\u8981\u7279\u522b\u6ce8\u610f\u7684\u662f\uff0c\u53f6\u8282\u70b9\u7684\u9ad8\u5ea6\u4e3a \\(0\\) \uff0c\u800c\u7a7a\u8282\u70b9\u7684\u9ad8\u5ea6\u4e3a \\(-1\\) \u3002\u6211\u4eec\u5c06\u521b\u5efa\u4e24\u4e2a\u5de5\u5177\u51fd\u6570\uff0c\u5206\u522b\u7528\u4e8e\u83b7\u53d6\u548c\u66f4\u65b0\u8282\u70b9\u7684\u9ad8\u5ea6\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def height(self, node: TreeNode | None) -> int:\n    \"\"\"\u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6\"\"\"\n    # \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n    if node is not None:\n        return node.height\n    return -1\n\ndef update_height(self, node: TreeNode | None):\n    \"\"\"\u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\"\"\"\n    # \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n    node.height = max([self.height(node.left), self.height(node.right)]) + 1\n
        avl_tree.cpp
        /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode *node) {\n    // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n    return node == nullptr ? -1 : node->height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode *node) {\n    // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n    node->height = max(height(node->left), height(node->right)) + 1;\n}\n
        avl_tree.java
        /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode node) {\n    // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n    return node == null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode node) {\n    // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n    node.height = Math.max(height(node.left), height(node.right)) + 1;\n}\n
        avl_tree.cs
        /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint Height(TreeNode? node) {\n    // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n    return node == null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid UpdateHeight(TreeNode node) {\n    // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n    node.height = Math.Max(Height(node.left), Height(node.right)) + 1;\n}\n
        avl_tree.go
        /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfunc (t *aVLTree) height(node *TreeNode) int {\n    // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n    if node != nil {\n        return node.Height\n    }\n    return -1\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfunc (t *aVLTree) updateHeight(node *TreeNode) {\n    lh := t.height(node.Left)\n    rh := t.height(node.Right)\n    // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n    if lh > rh {\n        node.Height = lh + 1\n    } else {\n        node.Height = rh + 1\n    }\n}\n
        avl_tree.swift
        /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfunc height(node: TreeNode?) -> Int {\n    // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n    node?.height ?? -1\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfunc updateHeight(node: TreeNode?) {\n    // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n    node?.height = max(height(node: node?.left), height(node: node?.right)) + 1\n}\n
        avl_tree.js
        /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nheight(node) {\n    // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n    return node === null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\n#updateHeight(node) {\n    // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n    node.height =\n        Math.max(this.height(node.left), this.height(node.right)) + 1;\n}\n
        avl_tree.ts
        /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nheight(node: TreeNode): number {\n    // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n    return node === null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nupdateHeight(node: TreeNode): void {\n    // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n    node.height =\n        Math.max(this.height(node.left), this.height(node.right)) + 1;\n}\n
        avl_tree.dart
        /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode? node) {\n  // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n  return node == null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode? node) {\n  // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n  node!.height = max(height(node.left), height(node.right)) + 1;\n}\n
        avl_tree.rs
        /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfn height(node: OptionTreeNodeRc) -> i32 {\n    // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n    match node {\n        Some(node) => node.borrow().height,\n        None => -1,\n    }\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfn update_height(node: OptionTreeNodeRc) {\n    if let Some(node) = node {\n        let left = node.borrow().left.clone();\n        let right = node.borrow().right.clone();\n        // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n        node.borrow_mut().height = std::cmp::max(Self::height(left), Self::height(right)) + 1;\n    }\n}\n
        avl_tree.c
        /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode *node) {\n    // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n    if (node != NULL) {\n        return node->height;\n    }\n    return -1;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode *node) {\n    int lh = height(node->left);\n    int rh = height(node->right);\n    // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n    if (lh > rh) {\n        node->height = lh + 1;\n    } else {\n        node->height = rh + 1;\n    }\n}\n
        avl_tree.kt
        /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfun height(node: TreeNode?): Int {\n    // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n    return node?.height ?: -1\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfun updateHeight(node: TreeNode?) {\n    // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n    node?.height = max(height(node?.left), height(node?.right)) + 1\n}\n
        avl_tree.rb
        ### \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 ###\ndef height(node)\n  # \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n  return node.height unless node.nil?\n\n  -1\nend\n\n### \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 ###\ndef update_height(node)\n  # \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n  node.height = [height(node.left), height(node.right)].max + 1\nend\n
        "},{"location":"chapter_tree/avl_tree/#2","title":"2. \u00a0 \u8282\u70b9\u5e73\u8861\u56e0\u5b50","text":"

        \u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\uff08balance factor\uff09\u5b9a\u4e49\u4e3a\u8282\u70b9\u5de6\u5b50\u6811\u7684\u9ad8\u5ea6\u51cf\u53bb\u53f3\u5b50\u6811\u7684\u9ad8\u5ea6\uff0c\u540c\u65f6\u89c4\u5b9a\u7a7a\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u4e3a \\(0\\) \u3002\u6211\u4eec\u540c\u6837\u5c06\u83b7\u53d6\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u7684\u529f\u80fd\u5c01\u88c5\u6210\u51fd\u6570\uff0c\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def balance_factor(self, node: TreeNode | None) -> int:\n    \"\"\"\u83b7\u53d6\u5e73\u8861\u56e0\u5b50\"\"\"\n    # \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n    if node is None:\n        return 0\n    # \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n    return self.height(node.left) - self.height(node.right)\n
        avl_tree.cpp
        /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode *node) {\n    // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n    if (node == nullptr)\n        return 0;\n    // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n    return height(node->left) - height(node->right);\n}\n
        avl_tree.java
        /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode node) {\n    // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n    if (node == null)\n        return 0;\n    // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n    return height(node.left) - height(node.right);\n}\n
        avl_tree.cs
        /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint BalanceFactor(TreeNode? node) {\n    // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n    if (node == null) return 0;\n    // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n    return Height(node.left) - Height(node.right);\n}\n
        avl_tree.go
        /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfunc (t *aVLTree) balanceFactor(node *TreeNode) int {\n    // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n    if node == nil {\n        return 0\n    }\n    // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n    return t.height(node.Left) - t.height(node.Right)\n}\n
        avl_tree.swift
        /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfunc balanceFactor(node: TreeNode?) -> Int {\n    // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n    guard let node = node else { return 0 }\n    // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n    return height(node: node.left) - height(node: node.right)\n}\n
        avl_tree.js
        /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nbalanceFactor(node) {\n    // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n    if (node === null) return 0;\n    // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n    return this.height(node.left) - this.height(node.right);\n}\n
        avl_tree.ts
        /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nbalanceFactor(node: TreeNode): number {\n    // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n    if (node === null) return 0;\n    // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n    return this.height(node.left) - this.height(node.right);\n}\n
        avl_tree.dart
        /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode? node) {\n  // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n  if (node == null) return 0;\n  // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n  return height(node.left) - height(node.right);\n}\n
        avl_tree.rs
        /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfn balance_factor(node: OptionTreeNodeRc) -> i32 {\n    match node {\n        // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n        None => 0,\n        // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n        Some(node) => {\n            Self::height(node.borrow().left.clone()) - Self::height(node.borrow().right.clone())\n        }\n    }\n}\n
        avl_tree.c
        /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode *node) {\n    // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n    if (node == NULL) {\n        return 0;\n    }\n    // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n    return height(node->left) - height(node->right);\n}\n
        avl_tree.kt
        /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfun balanceFactor(node: TreeNode?): Int {\n    // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n    if (node == null) return 0\n    // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n    return height(node.left) - height(node.right)\n}\n
        avl_tree.rb
        ### \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 ###\ndef balance_factor(node)\n  # \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n  return 0 if node.nil?\n\n  # \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n  height(node.left) - height(node.right)\nend\n

        Tip

        \u8bbe\u5e73\u8861\u56e0\u5b50\u4e3a \\(f\\) \uff0c\u5219\u4e00\u68f5 AVL \u6811\u7684\u4efb\u610f\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u7686\u6ee1\u8db3 \\(-1 \\le f \\le 1\\) \u3002

        "},{"location":"chapter_tree/avl_tree/#752-avl","title":"7.5.2 \u00a0 AVL \u6811\u65cb\u8f6c","text":"

        AVL \u6811\u7684\u7279\u70b9\u5728\u4e8e\u201c\u65cb\u8f6c\u201d\u64cd\u4f5c\uff0c\u5b83\u80fd\u591f\u5728\u4e0d\u5f71\u54cd\u4e8c\u53c9\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u7684\u524d\u63d0\u4e0b\uff0c\u4f7f\u5931\u8861\u8282\u70b9\u91cd\u65b0\u6062\u590d\u5e73\u8861\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u65cb\u8f6c\u64cd\u4f5c\u65e2\u80fd\u4fdd\u6301\u201c\u4e8c\u53c9\u641c\u7d22\u6811\u201d\u7684\u6027\u8d28\uff0c\u4e5f\u80fd\u4f7f\u6811\u91cd\u65b0\u53d8\u4e3a\u201c\u5e73\u8861\u4e8c\u53c9\u6811\u201d\u3002

        \u6211\u4eec\u5c06\u5e73\u8861\u56e0\u5b50\u7edd\u5bf9\u503c \\(> 1\\) \u7684\u8282\u70b9\u79f0\u4e3a\u201c\u5931\u8861\u8282\u70b9\u201d\u3002\u6839\u636e\u8282\u70b9\u5931\u8861\u60c5\u51b5\u7684\u4e0d\u540c\uff0c\u65cb\u8f6c\u64cd\u4f5c\u5206\u4e3a\u56db\u79cd\uff1a\u53f3\u65cb\u3001\u5de6\u65cb\u3001\u5148\u53f3\u65cb\u540e\u5de6\u65cb\u3001\u5148\u5de6\u65cb\u540e\u53f3\u65cb\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65cb\u8f6c\u64cd\u4f5c\u3002

        "},{"location":"chapter_tree/avl_tree/#1_1","title":"1. \u00a0 \u53f3\u65cb","text":"

        \u5982\u56fe 7-26 \u6240\u793a\uff0c\u8282\u70b9\u4e0b\u65b9\u4e3a\u5e73\u8861\u56e0\u5b50\u3002\u4ece\u5e95\u81f3\u9876\u770b\uff0c\u4e8c\u53c9\u6811\u4e2d\u9996\u4e2a\u5931\u8861\u8282\u70b9\u662f\u201c\u8282\u70b9 3\u201d\u3002\u6211\u4eec\u5173\u6ce8\u4ee5\u8be5\u5931\u8861\u8282\u70b9\u4e3a\u6839\u8282\u70b9\u7684\u5b50\u6811\uff0c\u5c06\u8be5\u8282\u70b9\u8bb0\u4e3a node \uff0c\u5176\u5de6\u5b50\u8282\u70b9\u8bb0\u4e3a child \uff0c\u6267\u884c\u201c\u53f3\u65cb\u201d\u64cd\u4f5c\u3002\u5b8c\u6210\u53f3\u65cb\u540e\uff0c\u5b50\u6811\u6062\u590d\u5e73\u8861\uff0c\u5e76\u4e14\u4ecd\u7136\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6027\u8d28\u3002

        <1><2><3><4>

        \u56fe 7-26 \u00a0 \u53f3\u65cb\u64cd\u4f5c\u6b65\u9aa4

        \u5982\u56fe 7-27 \u6240\u793a\uff0c\u5f53\u8282\u70b9 child \u6709\u53f3\u5b50\u8282\u70b9\uff08\u8bb0\u4e3a grand_child \uff09\u65f6\uff0c\u9700\u8981\u5728\u53f3\u65cb\u4e2d\u6dfb\u52a0\u4e00\u6b65\uff1a\u5c06 grand_child \u4f5c\u4e3a node \u7684\u5de6\u5b50\u8282\u70b9\u3002

        \u56fe 7-27 \u00a0 \u6709 grand_child \u7684\u53f3\u65cb\u64cd\u4f5c

        \u201c\u5411\u53f3\u65cb\u8f6c\u201d\u662f\u4e00\u79cd\u5f62\u8c61\u5316\u7684\u8bf4\u6cd5\uff0c\u5b9e\u9645\u4e0a\u9700\u8981\u901a\u8fc7\u4fee\u6539\u8282\u70b9\u6307\u9488\u6765\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def right_rotate(self, node: TreeNode | None) -> TreeNode | None:\n    \"\"\"\u53f3\u65cb\u64cd\u4f5c\"\"\"\n    child = node.left\n    grand_child = child.right\n    # \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n    child.right = node\n    node.left = grand_child\n    # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    self.update_height(node)\n    self.update_height(child)\n    # \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child\n
        avl_tree.cpp
        /* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode *rightRotate(TreeNode *node) {\n    TreeNode *child = node->left;\n    TreeNode *grandChild = child->right;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n    child->right = node;\n    node->left = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node);\n    updateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.java
        /* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode rightRotate(TreeNode node) {\n    TreeNode child = node.left;\n    TreeNode grandChild = child.right;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n    child.right = node;\n    node.left = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node);\n    updateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.cs
        /* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode? RightRotate(TreeNode? node) {\n    TreeNode? child = node?.left;\n    TreeNode? grandChild = child?.right;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n    child.right = node;\n    node.left = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    UpdateHeight(node);\n    UpdateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.go
        /* \u53f3\u65cb\u64cd\u4f5c */\nfunc (t *aVLTree) rightRotate(node *TreeNode) *TreeNode {\n    child := node.Left\n    grandChild := child.Right\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n    child.Right = node\n    node.Left = grandChild\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    t.updateHeight(node)\n    t.updateHeight(child)\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child\n}\n
        avl_tree.swift
        /* \u53f3\u65cb\u64cd\u4f5c */\nfunc rightRotate(node: TreeNode?) -> TreeNode? {\n    let child = node?.left\n    let grandChild = child?.right\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n    child?.right = node\n    node?.left = grandChild\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node: node)\n    updateHeight(node: child)\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child\n}\n
        avl_tree.js
        /* \u53f3\u65cb\u64cd\u4f5c */\n#rightRotate(node) {\n    const child = node.left;\n    const grandChild = child.right;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n    child.right = node;\n    node.left = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    this.#updateHeight(node);\n    this.#updateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.ts
        /* \u53f3\u65cb\u64cd\u4f5c */\nrightRotate(node: TreeNode): TreeNode {\n    const child = node.left;\n    const grandChild = child.right;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n    child.right = node;\n    node.left = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    this.updateHeight(node);\n    this.updateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.dart
        /* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode? rightRotate(TreeNode? node) {\n  TreeNode? child = node!.left;\n  TreeNode? grandChild = child!.right;\n  // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n  child.right = node;\n  node.left = grandChild;\n  // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n  updateHeight(node);\n  updateHeight(child);\n  // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n  return child;\n}\n
        avl_tree.rs
        /* \u53f3\u65cb\u64cd\u4f5c */\nfn right_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n    match node {\n        Some(node) => {\n            let child = node.borrow().left.clone().unwrap();\n            let grand_child = child.borrow().right.clone();\n            // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n            child.borrow_mut().right = Some(node.clone());\n            node.borrow_mut().left = grand_child;\n            // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n            Self::update_height(Some(node));\n            Self::update_height(Some(child.clone()));\n            // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n            Some(child)\n        }\n        None => None,\n    }\n}\n
        avl_tree.c
        /* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode *rightRotate(TreeNode *node) {\n    TreeNode *child, *grandChild;\n    child = node->left;\n    grandChild = child->right;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n    child->right = node;\n    node->left = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node);\n    updateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.kt
        /* \u53f3\u65cb\u64cd\u4f5c */\nfun rightRotate(node: TreeNode?): TreeNode {\n    val child = node!!.left\n    val grandChild = child!!.right\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n    child.right = node\n    node.left = grandChild\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node)\n    updateHeight(child)\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child\n}\n
        avl_tree.rb
        ### \u53f3\u65cb\u64cd\u4f5c ###\ndef right_rotate(node)\n  child = node.left\n  grand_child = child.right\n  # \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n  child.right = node\n  node.left = grand_child\n  # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n  update_height(node)\n  update_height(child)\n  # \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n  child\nend\n
        "},{"location":"chapter_tree/avl_tree/#2_1","title":"2. \u00a0 \u5de6\u65cb","text":"

        \u76f8\u5e94\u5730\uff0c\u5982\u679c\u8003\u8651\u4e0a\u8ff0\u5931\u8861\u4e8c\u53c9\u6811\u7684\u201c\u955c\u50cf\u201d\uff0c\u5219\u9700\u8981\u6267\u884c\u56fe 7-28 \u6240\u793a\u7684\u201c\u5de6\u65cb\u201d\u64cd\u4f5c\u3002

        \u56fe 7-28 \u00a0 \u5de6\u65cb\u64cd\u4f5c

        \u540c\u7406\uff0c\u5982\u56fe 7-29 \u6240\u793a\uff0c\u5f53\u8282\u70b9 child \u6709\u5de6\u5b50\u8282\u70b9\uff08\u8bb0\u4e3a grand_child \uff09\u65f6\uff0c\u9700\u8981\u5728\u5de6\u65cb\u4e2d\u6dfb\u52a0\u4e00\u6b65\uff1a\u5c06 grand_child \u4f5c\u4e3a node \u7684\u53f3\u5b50\u8282\u70b9\u3002

        \u56fe 7-29 \u00a0 \u6709 grand_child \u7684\u5de6\u65cb\u64cd\u4f5c

        \u53ef\u4ee5\u89c2\u5bdf\u5230\uff0c\u53f3\u65cb\u548c\u5de6\u65cb\u64cd\u4f5c\u5728\u903b\u8f91\u4e0a\u662f\u955c\u50cf\u5bf9\u79f0\u7684\uff0c\u5b83\u4eec\u5206\u522b\u89e3\u51b3\u7684\u4e24\u79cd\u5931\u8861\u60c5\u51b5\u4e5f\u662f\u5bf9\u79f0\u7684\u3002\u57fa\u4e8e\u5bf9\u79f0\u6027\uff0c\u6211\u4eec\u53ea\u9700\u5c06\u53f3\u65cb\u7684\u5b9e\u73b0\u4ee3\u7801\u4e2d\u7684\u6240\u6709\u7684 left \u66ff\u6362\u4e3a right \uff0c\u5c06\u6240\u6709\u7684 right \u66ff\u6362\u4e3a left \uff0c\u5373\u53ef\u5f97\u5230\u5de6\u65cb\u7684\u5b9e\u73b0\u4ee3\u7801\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def left_rotate(self, node: TreeNode | None) -> TreeNode | None:\n    \"\"\"\u5de6\u65cb\u64cd\u4f5c\"\"\"\n    child = node.right\n    grand_child = child.left\n    # \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n    child.left = node\n    node.right = grand_child\n    # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    self.update_height(node)\n    self.update_height(child)\n    # \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child\n
        avl_tree.cpp
        /* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode *leftRotate(TreeNode *node) {\n    TreeNode *child = node->right;\n    TreeNode *grandChild = child->left;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n    child->left = node;\n    node->right = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node);\n    updateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.java
        /* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode leftRotate(TreeNode node) {\n    TreeNode child = node.right;\n    TreeNode grandChild = child.left;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n    child.left = node;\n    node.right = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node);\n    updateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.cs
        /* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode? LeftRotate(TreeNode? node) {\n    TreeNode? child = node?.right;\n    TreeNode? grandChild = child?.left;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n    child.left = node;\n    node.right = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    UpdateHeight(node);\n    UpdateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.go
        /* \u5de6\u65cb\u64cd\u4f5c */\nfunc (t *aVLTree) leftRotate(node *TreeNode) *TreeNode {\n    child := node.Right\n    grandChild := child.Left\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n    child.Left = node\n    node.Right = grandChild\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    t.updateHeight(node)\n    t.updateHeight(child)\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child\n}\n
        avl_tree.swift
        /* \u5de6\u65cb\u64cd\u4f5c */\nfunc leftRotate(node: TreeNode?) -> TreeNode? {\n    let child = node?.right\n    let grandChild = child?.left\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n    child?.left = node\n    node?.right = grandChild\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node: node)\n    updateHeight(node: child)\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child\n}\n
        avl_tree.js
        /* \u5de6\u65cb\u64cd\u4f5c */\n#leftRotate(node) {\n    const child = node.right;\n    const grandChild = child.left;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n    child.left = node;\n    node.right = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    this.#updateHeight(node);\n    this.#updateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.ts
        /* \u5de6\u65cb\u64cd\u4f5c */\nleftRotate(node: TreeNode): TreeNode {\n    const child = node.right;\n    const grandChild = child.left;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n    child.left = node;\n    node.right = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    this.updateHeight(node);\n    this.updateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.dart
        /* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode? leftRotate(TreeNode? node) {\n  TreeNode? child = node!.right;\n  TreeNode? grandChild = child!.left;\n  // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n  child.left = node;\n  node.right = grandChild;\n  // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n  updateHeight(node);\n  updateHeight(child);\n  // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n  return child;\n}\n
        avl_tree.rs
        /* \u5de6\u65cb\u64cd\u4f5c */\nfn left_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n    match node {\n        Some(node) => {\n            let child = node.borrow().right.clone().unwrap();\n            let grand_child = child.borrow().left.clone();\n            // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n            child.borrow_mut().left = Some(node.clone());\n            node.borrow_mut().right = grand_child;\n            // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n            Self::update_height(Some(node));\n            Self::update_height(Some(child.clone()));\n            // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n            Some(child)\n        }\n        None => None,\n    }\n}\n
        avl_tree.c
        /* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode *leftRotate(TreeNode *node) {\n    TreeNode *child, *grandChild;\n    child = node->right;\n    grandChild = child->left;\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n    child->left = node;\n    node->right = grandChild;\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node);\n    updateHeight(child);\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child;\n}\n
        avl_tree.kt
        /* \u5de6\u65cb\u64cd\u4f5c */\nfun leftRotate(node: TreeNode?): TreeNode {\n    val child = node!!.right\n    val grandChild = child!!.left\n    // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n    child.left = node\n    node.right = grandChild\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node)\n    updateHeight(child)\n    // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return child\n}\n
        avl_tree.rb
        ### \u5de6\u65cb\u64cd\u4f5c ###\ndef left_rotate(node)\n  child = node.right\n  grand_child = child.left\n  # \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n  child.left = node\n  node.right = grand_child\n  # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n  update_height(node)\n  update_height(child)\n  # \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n  child\nend\n
        "},{"location":"chapter_tree/avl_tree/#3","title":"3. \u00a0 \u5148\u5de6\u65cb\u540e\u53f3\u65cb","text":"

        \u5bf9\u4e8e\u56fe 7-30 \u4e2d\u7684\u5931\u8861\u8282\u70b9 3 \uff0c\u4ec5\u4f7f\u7528\u5de6\u65cb\u6216\u53f3\u65cb\u90fd\u65e0\u6cd5\u4f7f\u5b50\u6811\u6062\u590d\u5e73\u8861\u3002\u6b64\u65f6\u9700\u8981\u5148\u5bf9 child \u6267\u884c\u201c\u5de6\u65cb\u201d\uff0c\u518d\u5bf9 node \u6267\u884c\u201c\u53f3\u65cb\u201d\u3002

        \u56fe 7-30 \u00a0 \u5148\u5de6\u65cb\u540e\u53f3\u65cb

        "},{"location":"chapter_tree/avl_tree/#4","title":"4. \u00a0 \u5148\u53f3\u65cb\u540e\u5de6\u65cb","text":"

        \u5982\u56fe 7-31 \u6240\u793a\uff0c\u5bf9\u4e8e\u4e0a\u8ff0\u5931\u8861\u4e8c\u53c9\u6811\u7684\u955c\u50cf\u60c5\u51b5\uff0c\u9700\u8981\u5148\u5bf9 child \u6267\u884c\u201c\u53f3\u65cb\u201d\uff0c\u518d\u5bf9 node \u6267\u884c\u201c\u5de6\u65cb\u201d\u3002

        \u56fe 7-31 \u00a0 \u5148\u53f3\u65cb\u540e\u5de6\u65cb

        "},{"location":"chapter_tree/avl_tree/#5","title":"5. \u00a0 \u65cb\u8f6c\u7684\u9009\u62e9","text":"

        \u56fe 7-32 \u5c55\u793a\u7684\u56db\u79cd\u5931\u8861\u60c5\u51b5\u4e0e\u4e0a\u8ff0\u6848\u4f8b\u9010\u4e2a\u5bf9\u5e94\uff0c\u5206\u522b\u9700\u8981\u91c7\u7528\u53f3\u65cb\u3001\u5148\u5de6\u65cb\u540e\u53f3\u65cb\u3001\u5148\u53f3\u65cb\u540e\u5de6\u65cb\u3001\u5de6\u65cb\u7684\u64cd\u4f5c\u3002

        \u56fe 7-32 \u00a0 AVL \u6811\u7684\u56db\u79cd\u65cb\u8f6c\u60c5\u51b5

        \u5982\u4e0b\u8868\u6240\u793a\uff0c\u6211\u4eec\u901a\u8fc7\u5224\u65ad\u5931\u8861\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u4ee5\u53ca\u8f83\u9ad8\u4e00\u4fa7\u5b50\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u7684\u6b63\u8d1f\u53f7\uff0c\u6765\u786e\u5b9a\u5931\u8861\u8282\u70b9\u5c5e\u4e8e\u56fe 7-32 \u4e2d\u7684\u54ea\u79cd\u60c5\u51b5\u3002

        \u8868 7-3 \u00a0 \u56db\u79cd\u65cb\u8f6c\u60c5\u51b5\u7684\u9009\u62e9\u6761\u4ef6

        \u5931\u8861\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50 \u5b50\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50 \u5e94\u91c7\u7528\u7684\u65cb\u8f6c\u65b9\u6cd5 \\(> 1\\) \uff08\u5de6\u504f\u6811\uff09 \\(\\geq 0\\) \u53f3\u65cb \\(> 1\\) \uff08\u5de6\u504f\u6811\uff09 \\(<0\\) \u5148\u5de6\u65cb\u540e\u53f3\u65cb \\(< -1\\) \uff08\u53f3\u504f\u6811\uff09 \\(\\leq 0\\) \u5de6\u65cb \\(< -1\\) \uff08\u53f3\u504f\u6811\uff09 \\(>0\\) \u5148\u53f3\u65cb\u540e\u5de6\u65cb

        \u4e3a\u4e86\u4fbf\u4e8e\u4f7f\u7528\uff0c\u6211\u4eec\u5c06\u65cb\u8f6c\u64cd\u4f5c\u5c01\u88c5\u6210\u4e00\u4e2a\u51fd\u6570\u3002\u6709\u4e86\u8fd9\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u5c31\u80fd\u5bf9\u5404\u79cd\u5931\u8861\u60c5\u51b5\u8fdb\u884c\u65cb\u8f6c\uff0c\u4f7f\u5931\u8861\u8282\u70b9\u91cd\u65b0\u6062\u590d\u5e73\u8861\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def rotate(self, node: TreeNode | None) -> TreeNode | None:\n    \"\"\"\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\"\"\"\n    # \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n    balance_factor = self.balance_factor(node)\n    # \u5de6\u504f\u6811\n    if balance_factor > 1:\n        if self.balance_factor(node.left) >= 0:\n            # \u53f3\u65cb\n            return self.right_rotate(node)\n        else:\n            # \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n            node.left = self.left_rotate(node.left)\n            return self.right_rotate(node)\n    # \u53f3\u504f\u6811\n    elif balance_factor < -1:\n        if self.balance_factor(node.right) <= 0:\n            # \u5de6\u65cb\n            return self.left_rotate(node)\n        else:\n            # \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n            node.right = self.right_rotate(node.right)\n            return self.left_rotate(node)\n    # \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n    return node\n
        avl_tree.cpp
        /* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode *rotate(TreeNode *node) {\n    // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n    int _balanceFactor = balanceFactor(node);\n    // \u5de6\u504f\u6811\n    if (_balanceFactor > 1) {\n        if (balanceFactor(node->left) >= 0) {\n            // \u53f3\u65cb\n            return rightRotate(node);\n        } else {\n            // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n            node->left = leftRotate(node->left);\n            return rightRotate(node);\n        }\n    }\n    // \u53f3\u504f\u6811\n    if (_balanceFactor < -1) {\n        if (balanceFactor(node->right) <= 0) {\n            // \u5de6\u65cb\n            return leftRotate(node);\n        } else {\n            // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n            node->right = rightRotate(node->right);\n            return leftRotate(node);\n        }\n    }\n    // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n    return node;\n}\n
        avl_tree.java
        /* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode rotate(TreeNode node) {\n    // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n    int balanceFactor = balanceFactor(node);\n    // \u5de6\u504f\u6811\n    if (balanceFactor > 1) {\n        if (balanceFactor(node.left) >= 0) {\n            // \u53f3\u65cb\n            return rightRotate(node);\n        } else {\n            // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n            node.left = leftRotate(node.left);\n            return rightRotate(node);\n        }\n    }\n    // \u53f3\u504f\u6811\n    if (balanceFactor < -1) {\n        if (balanceFactor(node.right) <= 0) {\n            // \u5de6\u65cb\n            return leftRotate(node);\n        } else {\n            // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n            node.right = rightRotate(node.right);\n            return leftRotate(node);\n        }\n    }\n    // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n    return node;\n}\n
        avl_tree.cs
        /* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode? Rotate(TreeNode? node) {\n    // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n    int balanceFactorInt = BalanceFactor(node);\n    // \u5de6\u504f\u6811\n    if (balanceFactorInt > 1) {\n        if (BalanceFactor(node?.left) >= 0) {\n            // \u53f3\u65cb\n            return RightRotate(node);\n        } else {\n            // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n            node!.left = LeftRotate(node!.left);\n            return RightRotate(node);\n        }\n    }\n    // \u53f3\u504f\u6811\n    if (balanceFactorInt < -1) {\n        if (BalanceFactor(node?.right) <= 0) {\n            // \u5de6\u65cb\n            return LeftRotate(node);\n        } else {\n            // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n            node!.right = RightRotate(node!.right);\n            return LeftRotate(node);\n        }\n    }\n    // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n    return node;\n}\n
        avl_tree.go
        /* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfunc (t *aVLTree) rotate(node *TreeNode) *TreeNode {\n    // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n    // Go \u63a8\u8350\u77ed\u53d8\u91cf\uff0c\u8fd9\u91cc bf \u6307\u4ee3 t.balanceFactor\n    bf := t.balanceFactor(node)\n    // \u5de6\u504f\u6811\n    if bf > 1 {\n        if t.balanceFactor(node.Left) >= 0 {\n            // \u53f3\u65cb\n            return t.rightRotate(node)\n        } else {\n            // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n            node.Left = t.leftRotate(node.Left)\n            return t.rightRotate(node)\n        }\n    }\n    // \u53f3\u504f\u6811\n    if bf < -1 {\n        if t.balanceFactor(node.Right) <= 0 {\n            // \u5de6\u65cb\n            return t.leftRotate(node)\n        } else {\n            // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n            node.Right = t.rightRotate(node.Right)\n            return t.leftRotate(node)\n        }\n    }\n    // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n    return node\n}\n
        avl_tree.swift
        /* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfunc rotate(node: TreeNode?) -> TreeNode? {\n    // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n    let balanceFactor = balanceFactor(node: node)\n    // \u5de6\u504f\u6811\n    if balanceFactor > 1 {\n        if self.balanceFactor(node: node?.left) >= 0 {\n            // \u53f3\u65cb\n            return rightRotate(node: node)\n        } else {\n            // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n            node?.left = leftRotate(node: node?.left)\n            return rightRotate(node: node)\n        }\n    }\n    // \u53f3\u504f\u6811\n    if balanceFactor < -1 {\n        if self.balanceFactor(node: node?.right) <= 0 {\n            // \u5de6\u65cb\n            return leftRotate(node: node)\n        } else {\n            // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n            node?.right = rightRotate(node: node?.right)\n            return leftRotate(node: node)\n        }\n    }\n    // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n    return node\n}\n
        avl_tree.js
        /* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n#rotate(node) {\n    // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n    const balanceFactor = this.balanceFactor(node);\n    // \u5de6\u504f\u6811\n    if (balanceFactor > 1) {\n        if (this.balanceFactor(node.left) >= 0) {\n            // \u53f3\u65cb\n            return this.#rightRotate(node);\n        } else {\n            // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n            node.left = this.#leftRotate(node.left);\n            return this.#rightRotate(node);\n        }\n    }\n    // \u53f3\u504f\u6811\n    if (balanceFactor < -1) {\n        if (this.balanceFactor(node.right) <= 0) {\n            // \u5de6\u65cb\n            return this.#leftRotate(node);\n        } else {\n            // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n            node.right = this.#rightRotate(node.right);\n            return this.#leftRotate(node);\n        }\n    }\n    // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n    return node;\n}\n
        avl_tree.ts
        /* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nrotate(node: TreeNode): TreeNode {\n    // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n    const balanceFactor = this.balanceFactor(node);\n    // \u5de6\u504f\u6811\n    if (balanceFactor > 1) {\n        if (this.balanceFactor(node.left) >= 0) {\n            // \u53f3\u65cb\n            return this.rightRotate(node);\n        } else {\n            // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n            node.left = this.leftRotate(node.left);\n            return this.rightRotate(node);\n        }\n    }\n    // \u53f3\u504f\u6811\n    if (balanceFactor < -1) {\n        if (this.balanceFactor(node.right) <= 0) {\n            // \u5de6\u65cb\n            return this.leftRotate(node);\n        } else {\n            // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n            node.right = this.rightRotate(node.right);\n            return this.leftRotate(node);\n        }\n    }\n    // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n    return node;\n}\n
        avl_tree.dart
        /* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode? rotate(TreeNode? node) {\n  // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n  int factor = balanceFactor(node);\n  // \u5de6\u504f\u6811\n  if (factor > 1) {\n    if (balanceFactor(node!.left) >= 0) {\n      // \u53f3\u65cb\n      return rightRotate(node);\n    } else {\n      // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n      node.left = leftRotate(node.left);\n      return rightRotate(node);\n    }\n  }\n  // \u53f3\u504f\u6811\n  if (factor < -1) {\n    if (balanceFactor(node!.right) <= 0) {\n      // \u5de6\u65cb\n      return leftRotate(node);\n    } else {\n      // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n      node.right = rightRotate(node.right);\n      return leftRotate(node);\n    }\n  }\n  // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n  return node;\n}\n
        avl_tree.rs
        /* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfn rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n    // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n    let balance_factor = Self::balance_factor(node.clone());\n    // \u5de6\u504f\u6811\n    if balance_factor > 1 {\n        let node = node.unwrap();\n        if Self::balance_factor(node.borrow().left.clone()) >= 0 {\n            // \u53f3\u65cb\n            Self::right_rotate(Some(node))\n        } else {\n            // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n            let left = node.borrow().left.clone();\n            node.borrow_mut().left = Self::left_rotate(left);\n            Self::right_rotate(Some(node))\n        }\n    }\n    // \u53f3\u504f\u6811\n    else if balance_factor < -1 {\n        let node = node.unwrap();\n        if Self::balance_factor(node.borrow().right.clone()) <= 0 {\n            // \u5de6\u65cb\n            Self::left_rotate(Some(node))\n        } else {\n            // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n            let right = node.borrow().right.clone();\n            node.borrow_mut().right = Self::right_rotate(right);\n            Self::left_rotate(Some(node))\n        }\n    } else {\n        // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n        node\n    }\n}\n
        avl_tree.c
        /* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode *rotate(TreeNode *node) {\n    // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n    int bf = balanceFactor(node);\n    // \u5de6\u504f\u6811\n    if (bf > 1) {\n        if (balanceFactor(node->left) >= 0) {\n            // \u53f3\u65cb\n            return rightRotate(node);\n        } else {\n            // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n            node->left = leftRotate(node->left);\n            return rightRotate(node);\n        }\n    }\n    // \u53f3\u504f\u6811\n    if (bf < -1) {\n        if (balanceFactor(node->right) <= 0) {\n            // \u5de6\u65cb\n            return leftRotate(node);\n        } else {\n            // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n            node->right = rightRotate(node->right);\n            return leftRotate(node);\n        }\n    }\n    // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n    return node;\n}\n
        avl_tree.kt
        /* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfun rotate(node: TreeNode): TreeNode {\n    // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n    val balanceFactor = balanceFactor(node)\n    // \u5de6\u504f\u6811\n    if (balanceFactor > 1) {\n        if (balanceFactor(node.left) >= 0) {\n            // \u53f3\u65cb\n            return rightRotate(node)\n        } else {\n            // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n            node.left = leftRotate(node.left)\n            return rightRotate(node)\n        }\n    }\n    // \u53f3\u504f\u6811\n    if (balanceFactor < -1) {\n        if (balanceFactor(node.right) <= 0) {\n            // \u5de6\u65cb\n            return leftRotate(node)\n        } else {\n            // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n            node.right = rightRotate(node.right)\n            return leftRotate(node)\n        }\n    }\n    // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n    return node\n}\n
        avl_tree.rb
        ### \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 ###\ndef rotate(node)\n  # \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n  balance_factor = balance_factor(node)\n  # \u5de6\u904d\u6811\n  if balance_factor > 1\n    if balance_factor(node.left) >= 0\n      # \u53f3\u65cb\n      return right_rotate(node)\n    else\n      # \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n      node.left = left_rotate(node.left)\n      return right_rotate(node)\n    end\n  # \u53f3\u904d\u6811\n  elsif balance_factor < -1\n    if balance_factor(node.right) <= 0\n      # \u5de6\u65cb\n      return left_rotate(node)\n    else\n      # \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n      node.right = right_rotate(node.right)\n      return left_rotate(node)\n    end\n  end\n  # \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n  node\nend\n
        "},{"location":"chapter_tree/avl_tree/#753-avl","title":"7.5.3 \u00a0 AVL \u6811\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_tree/avl_tree/#1_2","title":"1. \u00a0 \u63d2\u5165\u8282\u70b9","text":"

        AVL \u6811\u7684\u8282\u70b9\u63d2\u5165\u64cd\u4f5c\u4e0e\u4e8c\u53c9\u641c\u7d22\u6811\u5728\u4e3b\u4f53\u4e0a\u7c7b\u4f3c\u3002\u552f\u4e00\u7684\u533a\u522b\u5728\u4e8e\uff0c\u5728 AVL \u6811\u4e2d\u63d2\u5165\u8282\u70b9\u540e\uff0c\u4ece\u8be5\u8282\u70b9\u5230\u6839\u8282\u70b9\u7684\u8def\u5f84\u4e0a\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e00\u7cfb\u5217\u5931\u8861\u8282\u70b9\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4ece\u8fd9\u4e2a\u8282\u70b9\u5f00\u59cb\uff0c\u81ea\u5e95\u5411\u4e0a\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6240\u6709\u5931\u8861\u8282\u70b9\u6062\u590d\u5e73\u8861\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def insert(self, val):\n    \"\"\"\u63d2\u5165\u8282\u70b9\"\"\"\n    self._root = self.insert_helper(self._root, val)\n\ndef insert_helper(self, node: TreeNode | None, val: int) -> TreeNode:\n    \"\"\"\u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\"\"\"\n    if node is None:\n        return TreeNode(val)\n    # 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9\n    if val < node.val:\n        node.left = self.insert_helper(node.left, val)\n    elif val > node.val:\n        node.right = self.insert_helper(node.right, val)\n    else:\n        # \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n        return node\n    # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    self.update_height(node)\n    # 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n    return self.rotate(node)\n
        avl_tree.cpp
        /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\n    root = insertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *insertHelper(TreeNode *node, int val) {\n    if (node == nullptr)\n        return new TreeNode(val);\n    /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n    if (val < node->val)\n        node->left = insertHelper(node->left, val);\n    else if (val > node->val)\n        node->right = insertHelper(node->right, val);\n    else\n        return node;    // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n    updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.java
        /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\n    root = insertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode insertHelper(TreeNode node, int val) {\n    if (node == null)\n        return new TreeNode(val);\n    /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n    if (val < node.val)\n        node.left = insertHelper(node.left, val);\n    else if (val > node.val)\n        node.right = insertHelper(node.right, val);\n    else\n        return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n    updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.cs
        /* \u63d2\u5165\u8282\u70b9 */\nvoid Insert(int val) {\n    root = InsertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? InsertHelper(TreeNode? node, int val) {\n    if (node == null) return new TreeNode(val);\n    /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n    if (val < node.val)\n        node.left = InsertHelper(node.left, val);\n    else if (val > node.val)\n        node.right = InsertHelper(node.right, val);\n    else\n        return node;     // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n    UpdateHeight(node);  // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = Rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.go
        /* \u63d2\u5165\u8282\u70b9 */\nfunc (t *aVLTree) insert(val int) {\n    t.root = t.insertHelper(t.root, val)\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nfunc (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode {\n    if node == nil {\n        return NewTreeNode(val)\n    }\n    /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n    if val < node.Val.(int) {\n        node.Left = t.insertHelper(node.Left, val)\n    } else if val > node.Val.(int) {\n        node.Right = t.insertHelper(node.Right, val)\n    } else {\n        // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n        return node\n    }\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    t.updateHeight(node)\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = t.rotate(node)\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node\n}\n
        avl_tree.swift
        /* \u63d2\u5165\u8282\u70b9 */\nfunc insert(val: Int) {\n    root = insertHelper(node: root, val: val)\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc insertHelper(node: TreeNode?, val: Int) -> TreeNode? {\n    var node = node\n    if node == nil {\n        return TreeNode(x: val)\n    }\n    /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n    if val < node!.val {\n        node?.left = insertHelper(node: node?.left, val: val)\n    } else if val > node!.val {\n        node?.right = insertHelper(node: node?.right, val: val)\n    } else {\n        return node // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n    }\n    updateHeight(node: node) // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = rotate(node: node)\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node\n}\n
        avl_tree.js
        /* \u63d2\u5165\u8282\u70b9 */\ninsert(val) {\n    this.root = this.#insertHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\n#insertHelper(node, val) {\n    if (node === null) return new TreeNode(val);\n    /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n    if (val < node.val) node.left = this.#insertHelper(node.left, val);\n    else if (val > node.val)\n        node.right = this.#insertHelper(node.right, val);\n    else return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n    this.#updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = this.#rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.ts
        /* \u63d2\u5165\u8282\u70b9 */\ninsert(val: number): void {\n    this.root = this.insertHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\ninsertHelper(node: TreeNode, val: number): TreeNode {\n    if (node === null) return new TreeNode(val);\n    /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n    if (val < node.val) {\n        node.left = this.insertHelper(node.left, val);\n    } else if (val > node.val) {\n        node.right = this.insertHelper(node.right, val);\n    } else {\n        return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n    }\n    this.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = this.rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.dart
        /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\n  root = insertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? insertHelper(TreeNode? node, int val) {\n  if (node == null) return TreeNode(val);\n  /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n  if (val < node.val)\n    node.left = insertHelper(node.left, val);\n  else if (val > node.val)\n    node.right = insertHelper(node.right, val);\n  else\n    return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n  updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n  /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n  node = rotate(node);\n  // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n  return node;\n}\n
        avl_tree.rs
        /* \u63d2\u5165\u8282\u70b9 */\nfn insert(&mut self, val: i32) {\n    self.root = Self::insert_helper(self.root.clone(), val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfn insert_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {\n    match node {\n        Some(mut node) => {\n            /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n            match {\n                let node_val = node.borrow().val;\n                node_val\n            }\n            .cmp(&val)\n            {\n                Ordering::Greater => {\n                    let left = node.borrow().left.clone();\n                    node.borrow_mut().left = Self::insert_helper(left, val);\n                }\n                Ordering::Less => {\n                    let right = node.borrow().right.clone();\n                    node.borrow_mut().right = Self::insert_helper(right, val);\n                }\n                Ordering::Equal => {\n                    return Some(node); // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n                }\n            }\n            Self::update_height(Some(node.clone())); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n\n            /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n            node = Self::rotate(Some(node)).unwrap();\n            // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n            Some(node)\n        }\n        None => Some(TreeNode::new(val)),\n    }\n}\n
        avl_tree.c
        /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(AVLTree *tree, int val) {\n    tree->root = insertHelper(tree->root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nTreeNode *insertHelper(TreeNode *node, int val) {\n    if (node == NULL) {\n        return newTreeNode(val);\n    }\n    /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n    if (val < node->val) {\n        node->left = insertHelper(node->left, val);\n    } else if (val > node->val) {\n        node->right = insertHelper(node->right, val);\n    } else {\n        // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n        return node;\n    }\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node);\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.kt
        /* \u63d2\u5165\u8282\u70b9 */\nfun insert(_val: Int) {\n    root = insertHelper(root, _val)\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfun insertHelper(n: TreeNode?, _val: Int): TreeNode {\n    if (n == null)\n        return TreeNode(_val)\n    var node = n\n    /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n    if (_val < node._val)\n        node.left = insertHelper(node.left, _val)\n    else if (_val > node._val)\n        node.right = insertHelper(node.right, _val)\n    else\n        return node // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n    updateHeight(node) // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = rotate(node)\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node\n}\n
        avl_tree.rb
        ### \u63d2\u5165\u8282\u70b9 ###\ndef insert(val)\n  @root = insert_helper(@root, val)\nend\n\n### \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09###\ndef insert_helper(node, val)\n  return TreeNode.new(val) if node.nil?\n  # 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9\n  if val < node.val\n    node.left = insert_helper(node.left, val)\n  elsif val > node.val\n    node.right = insert_helper(node.right, val)\n  else\n    # \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n    return node\n  end\n  # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n  update_height(node)\n  # 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n  rotate(node)\nend\n
        "},{"location":"chapter_tree/avl_tree/#2_2","title":"2. \u00a0 \u5220\u9664\u8282\u70b9","text":"

        \u7c7b\u4f3c\u5730\uff0c\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5220\u9664\u8282\u70b9\u65b9\u6cd5\u7684\u57fa\u7840\u4e0a\uff0c\u9700\u8981\u4ece\u5e95\u81f3\u9876\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6240\u6709\u5931\u8861\u8282\u70b9\u6062\u590d\u5e73\u8861\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
        def remove(self, val: int):\n    \"\"\"\u5220\u9664\u8282\u70b9\"\"\"\n    self._root = self.remove_helper(self._root, val)\n\ndef remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None:\n    \"\"\"\u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\"\"\"\n    if node is None:\n        return None\n    # 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664\n    if val < node.val:\n        node.left = self.remove_helper(node.left, val)\n    elif val > node.val:\n        node.right = self.remove_helper(node.right, val)\n    else:\n        if node.left is None or node.right is None:\n            child = node.left or node.right\n            # \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n            if child is None:\n                return None\n            # \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n            else:\n                node = child\n        else:\n            # \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n            temp = node.right\n            while temp.left is not None:\n                temp = temp.left\n            node.right = self.remove_helper(node.right, temp.val)\n            node.val = temp.val\n    # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    self.update_height(node)\n    # 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n    return self.rotate(node)\n
        avl_tree.cpp
        /* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\n    root = removeHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *removeHelper(TreeNode *node, int val) {\n    if (node == nullptr)\n        return nullptr;\n    /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n    if (val < node->val)\n        node->left = removeHelper(node->left, val);\n    else if (val > node->val)\n        node->right = removeHelper(node->right, val);\n    else {\n        if (node->left == nullptr || node->right == nullptr) {\n            TreeNode *child = node->left != nullptr ? node->left : node->right;\n            // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n            if (child == nullptr) {\n                delete node;\n                return nullptr;\n            }\n            // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n            else {\n                delete node;\n                node = child;\n            }\n        } else {\n            // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n            TreeNode *temp = node->right;\n            while (temp->left != nullptr) {\n                temp = temp->left;\n            }\n            int tempVal = temp->val;\n            node->right = removeHelper(node->right, temp->val);\n            node->val = tempVal;\n        }\n    }\n    updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.java
        /* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\n    root = removeHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode removeHelper(TreeNode node, int val) {\n    if (node == null)\n        return null;\n    /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n    if (val < node.val)\n        node.left = removeHelper(node.left, val);\n    else if (val > node.val)\n        node.right = removeHelper(node.right, val);\n    else {\n        if (node.left == null || node.right == null) {\n            TreeNode child = node.left != null ? node.left : node.right;\n            // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n            if (child == null)\n                return null;\n            // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n            else\n                node = child;\n        } else {\n            // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n            TreeNode temp = node.right;\n            while (temp.left != null) {\n                temp = temp.left;\n            }\n            node.right = removeHelper(node.right, temp.val);\n            node.val = temp.val;\n        }\n    }\n    updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.cs
        /* \u5220\u9664\u8282\u70b9 */\nvoid Remove(int val) {\n    root = RemoveHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? RemoveHelper(TreeNode? node, int val) {\n    if (node == null) return null;\n    /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n    if (val < node.val)\n        node.left = RemoveHelper(node.left, val);\n    else if (val > node.val)\n        node.right = RemoveHelper(node.right, val);\n    else {\n        if (node.left == null || node.right == null) {\n            TreeNode? child = node.left ?? node.right;\n            // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n            if (child == null)\n                return null;\n            // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n            else\n                node = child;\n        } else {\n            // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n            TreeNode? temp = node.right;\n            while (temp.left != null) {\n                temp = temp.left;\n            }\n            node.right = RemoveHelper(node.right, temp.val!.Value);\n            node.val = temp.val;\n        }\n    }\n    UpdateHeight(node);  // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = Rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.go
        /* \u5220\u9664\u8282\u70b9 */\nfunc (t *aVLTree) remove(val int) {\n    t.root = t.removeHelper(t.root, val)\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nfunc (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {\n    if node == nil {\n        return nil\n    }\n    /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n    if val < node.Val.(int) {\n        node.Left = t.removeHelper(node.Left, val)\n    } else if val > node.Val.(int) {\n        node.Right = t.removeHelper(node.Right, val)\n    } else {\n        if node.Left == nil || node.Right == nil {\n            child := node.Left\n            if node.Right != nil {\n                child = node.Right\n            }\n            if child == nil {\n                // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n                return nil\n            } else {\n                // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n                node = child\n            }\n        } else {\n            // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n            temp := node.Right\n            for temp.Left != nil {\n                temp = temp.Left\n            }\n            node.Right = t.removeHelper(node.Right, temp.Val.(int))\n            node.Val = temp.Val\n        }\n    }\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    t.updateHeight(node)\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = t.rotate(node)\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node\n}\n
        avl_tree.swift
        /* \u5220\u9664\u8282\u70b9 */\nfunc remove(val: Int) {\n    root = removeHelper(node: root, val: val)\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc removeHelper(node: TreeNode?, val: Int) -> TreeNode? {\n    var node = node\n    if node == nil {\n        return nil\n    }\n    /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n    if val < node!.val {\n        node?.left = removeHelper(node: node?.left, val: val)\n    } else if val > node!.val {\n        node?.right = removeHelper(node: node?.right, val: val)\n    } else {\n        if node?.left == nil || node?.right == nil {\n            let child = node?.left ?? node?.right\n            // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n            if child == nil {\n                return nil\n            }\n            // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n            else {\n                node = child\n            }\n        } else {\n            // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n            var temp = node?.right\n            while temp?.left != nil {\n                temp = temp?.left\n            }\n            node?.right = removeHelper(node: node?.right, val: temp!.val)\n            node?.val = temp!.val\n        }\n    }\n    updateHeight(node: node) // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = rotate(node: node)\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node\n}\n
        avl_tree.js
        /* \u5220\u9664\u8282\u70b9 */\nremove(val) {\n    this.root = this.#removeHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\n#removeHelper(node, val) {\n    if (node === null) return null;\n    /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n    if (val < node.val) node.left = this.#removeHelper(node.left, val);\n    else if (val > node.val)\n        node.right = this.#removeHelper(node.right, val);\n    else {\n        if (node.left === null || node.right === null) {\n            const child = node.left !== null ? node.left : node.right;\n            // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n            if (child === null) return null;\n            // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n            else node = child;\n        } else {\n            // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n            let temp = node.right;\n            while (temp.left !== null) {\n                temp = temp.left;\n            }\n            node.right = this.#removeHelper(node.right, temp.val);\n            node.val = temp.val;\n        }\n    }\n    this.#updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = this.#rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.ts
        /* \u5220\u9664\u8282\u70b9 */\nremove(val: number): void {\n    this.root = this.removeHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nremoveHelper(node: TreeNode, val: number): TreeNode {\n    if (node === null) return null;\n    /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n    if (val < node.val) {\n        node.left = this.removeHelper(node.left, val);\n    } else if (val > node.val) {\n        node.right = this.removeHelper(node.right, val);\n    } else {\n        if (node.left === null || node.right === null) {\n            const child = node.left !== null ? node.left : node.right;\n            // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n            if (child === null) {\n                return null;\n            } else {\n                // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n                node = child;\n            }\n        } else {\n            // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n            let temp = node.right;\n            while (temp.left !== null) {\n                temp = temp.left;\n            }\n            node.right = this.removeHelper(node.right, temp.val);\n            node.val = temp.val;\n        }\n    }\n    this.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = this.rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.dart
        /* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\n  root = removeHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? removeHelper(TreeNode? node, int val) {\n  if (node == null) return null;\n  /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n  if (val < node.val)\n    node.left = removeHelper(node.left, val);\n  else if (val > node.val)\n    node.right = removeHelper(node.right, val);\n  else {\n    if (node.left == null || node.right == null) {\n      TreeNode? child = node.left ?? node.right;\n      // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n      if (child == null)\n        return null;\n      // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n      else\n        node = child;\n    } else {\n      // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n      TreeNode? temp = node.right;\n      while (temp!.left != null) {\n        temp = temp.left;\n      }\n      node.right = removeHelper(node.right, temp.val);\n      node.val = temp.val;\n    }\n  }\n  updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n  /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n  node = rotate(node);\n  // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n  return node;\n}\n
        avl_tree.rs
        /* \u5220\u9664\u8282\u70b9 */\nfn remove(&self, val: i32) {\n    Self::remove_helper(self.root.clone(), val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfn remove_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {\n    match node {\n        Some(mut node) => {\n            /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n            if val < node.borrow().val {\n                let left = node.borrow().left.clone();\n                node.borrow_mut().left = Self::remove_helper(left, val);\n            } else if val > node.borrow().val {\n                let right = node.borrow().right.clone();\n                node.borrow_mut().right = Self::remove_helper(right, val);\n            } else if node.borrow().left.is_none() || node.borrow().right.is_none() {\n                let child = if node.borrow().left.is_some() {\n                    node.borrow().left.clone()\n                } else {\n                    node.borrow().right.clone()\n                };\n                match child {\n                    // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n                    None => {\n                        return None;\n                    }\n                    // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n                    Some(child) => node = child,\n                }\n            } else {\n                // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n                let mut temp = node.borrow().right.clone().unwrap();\n                loop {\n                    let temp_left = temp.borrow().left.clone();\n                    if temp_left.is_none() {\n                        break;\n                    }\n                    temp = temp_left.unwrap();\n                }\n                let right = node.borrow().right.clone();\n                node.borrow_mut().right = Self::remove_helper(right, temp.borrow().val);\n                node.borrow_mut().val = temp.borrow().val;\n            }\n            Self::update_height(Some(node.clone())); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n\n            /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n            node = Self::rotate(Some(node)).unwrap();\n            // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n            Some(node)\n        }\n        None => None,\n    }\n}\n
        avl_tree.c
        /* \u5220\u9664\u8282\u70b9 */\n// \u7531\u4e8e\u5f15\u5165\u4e86 stdio.h \uff0c\u6b64\u5904\u65e0\u6cd5\u4f7f\u7528 remove \u5173\u952e\u8bcd\nvoid removeItem(AVLTree *tree, int val) {\n    TreeNode *root = removeHelper(tree->root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nTreeNode *removeHelper(TreeNode *node, int val) {\n    TreeNode *child, *grandChild;\n    if (node == NULL) {\n        return NULL;\n    }\n    /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n    if (val < node->val) {\n        node->left = removeHelper(node->left, val);\n    } else if (val > node->val) {\n        node->right = removeHelper(node->right, val);\n    } else {\n        if (node->left == NULL || node->right == NULL) {\n            child = node->left;\n            if (node->right != NULL) {\n                child = node->right;\n            }\n            // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n            if (child == NULL) {\n                return NULL;\n            } else {\n                // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n                node = child;\n            }\n        } else {\n            // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n            TreeNode *temp = node->right;\n            while (temp->left != NULL) {\n                temp = temp->left;\n            }\n            int tempVal = temp->val;\n            node->right = removeHelper(node->right, temp->val);\n            node->val = tempVal;\n        }\n    }\n    // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    updateHeight(node);\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = rotate(node);\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node;\n}\n
        avl_tree.kt
        /* \u5220\u9664\u8282\u70b9 */\nfun remove(_val: Int) {\n    root = removeHelper(root, _val)\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfun removeHelper(n: TreeNode?, _val: Int): TreeNode? {\n    var node = n ?: return null\n    /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n    if (_val < node._val)\n        node.left = removeHelper(node.left, _val)\n    else if (_val > node._val)\n        node.right = removeHelper(node.right, _val)\n    else {\n        if (node.left == null || node.right == null) {\n            val child = if (node.left != null)\n                node.left\n            else\n                node.right\n            // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n            if (child == null)\n                return null\n            // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n            else\n                node = child\n        } else {\n            // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n            var temp = node.right\n            while (temp!!.left != null) {\n                temp = temp.left\n            }\n            node.right = removeHelper(node.right, temp._val)\n            node._val = temp._val\n        }\n    }\n    updateHeight(node) // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n    /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n    node = rotate(node)\n    // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n    return node\n}\n
        avl_tree.rb
        ### \u5220\u9664\u8282\u70b9 ###\ndef remove(val)\n  @root = remove_helper(@root, val)\nend\n\n### \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09###\ndef remove_helper(node, val)\n  return if node.nil?\n  # 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664\n  if val < node.val\n    node.left = remove_helper(node.left, val)\n  elsif val > node.val\n    node.right = remove_helper(node.right, val)\n  else\n    if node.left.nil? || node.right.nil?\n      child = node.left || node.right\n      # \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n      return if child.nil?\n      # \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n      node = child\n    else\n      # \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n      temp = node.right\n      while !temp.left.nil?\n        temp = temp.left\n      end\n      node.right = remove_helper(node.right, temp.val)\n      node.val = temp.val\n    end\n  end\n  # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n  update_height(node)\n  # 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n  rotate(node)\nend\n
        "},{"location":"chapter_tree/avl_tree/#3_1","title":"3. \u00a0 \u67e5\u627e\u8282\u70b9","text":"

        AVL \u6811\u7684\u8282\u70b9\u67e5\u627e\u64cd\u4f5c\u4e0e\u4e8c\u53c9\u641c\u7d22\u6811\u4e00\u81f4\uff0c\u5728\u6b64\u4e0d\u518d\u8d58\u8ff0\u3002

        "},{"location":"chapter_tree/avl_tree/#754-avl","title":"7.5.4 \u00a0 AVL \u6811\u5178\u578b\u5e94\u7528","text":"
        • \u7ec4\u7ec7\u548c\u5b58\u50a8\u5927\u578b\u6570\u636e\uff0c\u9002\u7528\u4e8e\u9ad8\u9891\u67e5\u627e\u3001\u4f4e\u9891\u589e\u5220\u7684\u573a\u666f\u3002
        • \u7528\u4e8e\u6784\u5efa\u6570\u636e\u5e93\u4e2d\u7684\u7d22\u5f15\u7cfb\u7edf\u3002
        • \u7ea2\u9ed1\u6811\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811\u3002\u76f8\u8f83\u4e8e AVL \u6811\uff0c\u7ea2\u9ed1\u6811\u7684\u5e73\u8861\u6761\u4ef6\u66f4\u5bbd\u677e\uff0c\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u6240\u9700\u7684\u65cb\u8f6c\u64cd\u4f5c\u66f4\u5c11\uff0c\u8282\u70b9\u589e\u5220\u64cd\u4f5c\u7684\u5e73\u5747\u6548\u7387\u66f4\u9ad8\u3002
        "},{"location":"chapter_tree/binary_search_tree/","title":"7.4 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811","text":"

        \u5982\u56fe 7-16 \u6240\u793a\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\uff08binary search tree\uff09\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\u3002

        1. \u5bf9\u4e8e\u6839\u8282\u70b9\uff0c\u5de6\u5b50\u6811\u4e2d\u6240\u6709\u8282\u70b9\u7684\u503c \\(<\\) \u6839\u8282\u70b9\u7684\u503c \\(<\\) \u53f3\u5b50\u6811\u4e2d\u6240\u6709\u8282\u70b9\u7684\u503c\u3002
        2. \u4efb\u610f\u8282\u70b9\u7684\u5de6\u3001\u53f3\u5b50\u6811\u4e5f\u662f\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u5373\u540c\u6837\u6ee1\u8db3\u6761\u4ef6 1. \u3002

        \u56fe 7-16 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811

        "},{"location":"chapter_tree/binary_search_tree/#741","title":"7.4.1 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u64cd\u4f5c","text":"

        \u6211\u4eec\u5c06\u4e8c\u53c9\u641c\u7d22\u6811\u5c01\u88c5\u4e3a\u4e00\u4e2a\u7c7b BinarySearchTree \uff0c\u5e76\u58f0\u660e\u4e00\u4e2a\u6210\u5458\u53d8\u91cf root \uff0c\u6307\u5411\u6811\u7684\u6839\u8282\u70b9\u3002

        "},{"location":"chapter_tree/binary_search_tree/#1","title":"1. \u00a0 \u67e5\u627e\u8282\u70b9","text":"

        \u7ed9\u5b9a\u76ee\u6807\u8282\u70b9\u503c num \uff0c\u53ef\u4ee5\u6839\u636e\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6027\u8d28\u6765\u67e5\u627e\u3002\u5982\u56fe 7-17 \u6240\u793a\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u8282\u70b9 cur \uff0c\u4ece\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9 root \u51fa\u53d1\uff0c\u5faa\u73af\u6bd4\u8f83\u8282\u70b9\u503c cur.val \u548c num \u4e4b\u95f4\u7684\u5927\u5c0f\u5173\u7cfb\u3002

        • \u82e5 cur.val < num \uff0c\u8bf4\u660e\u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\uff0c\u56e0\u6b64\u6267\u884c cur = cur.right \u3002
        • \u82e5 cur.val > num \uff0c\u8bf4\u660e\u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\uff0c\u56e0\u6b64\u6267\u884c cur = cur.left \u3002
        • \u82e5 cur.val = num \uff0c\u8bf4\u660e\u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\u5e76\u8fd4\u56de\u8be5\u8282\u70b9\u3002
        <1><2><3><4>

        \u56fe 7-17 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u67e5\u627e\u8282\u70b9\u793a\u4f8b

        \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u67e5\u627e\u64cd\u4f5c\u4e0e\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u4e00\u81f4\uff0c\u90fd\u662f\u6bcf\u8f6e\u6392\u9664\u4e00\u534a\u60c5\u51b5\u3002\u5faa\u73af\u6b21\u6570\u6700\u591a\u4e3a\u4e8c\u53c9\u6811\u7684\u9ad8\u5ea6\uff0c\u5f53\u4e8c\u53c9\u6811\u5e73\u8861\u65f6\uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_tree.py
        def search(self, num: int) -> TreeNode | None:\n    \"\"\"\u67e5\u627e\u8282\u70b9\"\"\"\n    cur = self._root\n    # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while cur is not None:\n        # \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if cur.val < num:\n            cur = cur.right\n        # \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        elif cur.val > num:\n            cur = cur.left\n        # \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        else:\n            break\n    return cur\n
        binary_search_tree.cpp
        /* \u67e5\u627e\u8282\u70b9 */\nTreeNode *search(int num) {\n    TreeNode *cur = root;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != nullptr) {\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur->val < num)\n            cur = cur->right;\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else if (cur->val > num)\n            cur = cur->left;\n        // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        else\n            break;\n    }\n    // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n    return cur;\n}\n
        binary_search_tree.java
        /* \u67e5\u627e\u8282\u70b9 */\nTreeNode search(int num) {\n    TreeNode cur = root;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != null) {\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num)\n            cur = cur.right;\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else if (cur.val > num)\n            cur = cur.left;\n        // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        else\n            break;\n    }\n    // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n    return cur;\n}\n
        binary_search_tree.cs
        /* \u67e5\u627e\u8282\u70b9 */\nTreeNode? Search(int num) {\n    TreeNode? cur = root;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != null) {\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num) cur =\n            cur.right;\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else if (cur.val > num)\n            cur = cur.left;\n        // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        else\n            break;\n    }\n    // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n    return cur;\n}\n
        binary_search_tree.go
        /* \u67e5\u627e\u8282\u70b9 */\nfunc (bst *binarySearchTree) search(num int) *TreeNode {\n    node := bst.root\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    for node != nil {\n        if node.Val.(int) < num {\n            // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n            node = node.Right\n        } else if node.Val.(int) > num {\n            // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n            node = node.Left\n        } else {\n            // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n            break\n        }\n    }\n    // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n    return node\n}\n
        binary_search_tree.swift
        /* \u67e5\u627e\u8282\u70b9 */\nfunc search(num: Int) -> TreeNode? {\n    var cur = root\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while cur != nil {\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if cur!.val < num {\n            cur = cur?.right\n        }\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else if cur!.val > num {\n            cur = cur?.left\n        }\n        // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        else {\n            break\n        }\n    }\n    // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n    return cur\n}\n
        binary_search_tree.js
        /* \u67e5\u627e\u8282\u70b9 */\nsearch(num) {\n    let cur = this.root;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur !== null) {\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num) cur = cur.right;\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else if (cur.val > num) cur = cur.left;\n        // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        else break;\n    }\n    // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n    return cur;\n}\n
        binary_search_tree.ts
        /* \u67e5\u627e\u8282\u70b9 */\nsearch(num: number): TreeNode | null {\n    let cur = this.root;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur !== null) {\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num) cur = cur.right;\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else if (cur.val > num) cur = cur.left;\n        // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        else break;\n    }\n    // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n    return cur;\n}\n
        binary_search_tree.dart
        /* \u67e5\u627e\u8282\u70b9 */\nTreeNode? search(int _num) {\n  TreeNode? cur = _root;\n  // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n  while (cur != null) {\n    // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n    if (cur.val < _num)\n      cur = cur.right;\n    // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n    else if (cur.val > _num)\n      cur = cur.left;\n    // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n    else\n      break;\n  }\n  // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n  return cur;\n}\n
        binary_search_tree.rs
        /* \u67e5\u627e\u8282\u70b9 */\npub fn search(&self, num: i32) -> OptionTreeNodeRc {\n    let mut cur = self.root.clone();\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while let Some(node) = cur.clone() {\n        match num.cmp(&node.borrow().val) {\n            // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n            Ordering::Greater => cur = node.borrow().right.clone(),\n            // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n            Ordering::Less => cur = node.borrow().left.clone(),\n            // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n            Ordering::Equal => break,\n        }\n    }\n\n    // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n    cur\n}\n
        binary_search_tree.c
        /* \u67e5\u627e\u8282\u70b9 */\nTreeNode *search(BinarySearchTree *bst, int num) {\n    TreeNode *cur = bst->root;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != NULL) {\n        if (cur->val < num) {\n            // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n            cur = cur->right;\n        } else if (cur->val > num) {\n            // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n            cur = cur->left;\n        } else {\n            // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n            break;\n        }\n    }\n    // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n    return cur;\n}\n
        binary_search_tree.kt
        /* \u67e5\u627e\u8282\u70b9 */\nfun search(num: Int): TreeNode? {\n    var cur = root\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != null) {\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        cur = if (cur._val < num)\n            cur.right\n        // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else if (cur._val > num)\n            cur.left\n        // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        else\n            break\n    }\n    // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n    return cur\n}\n
        binary_search_tree.rb
        ### \u67e5\u627e\u8282\u70b9 ###\ndef search(num)\n  cur = @root\n\n  # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n  while !cur.nil?\n    # \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n    if cur.val < num\n      cur = cur.right\n    # \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n    elsif cur.val > num\n      cur = cur.left\n    # \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n    else\n      break\n    end\n  end\n\n  cur\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_tree/binary_search_tree/#2","title":"2. \u00a0 \u63d2\u5165\u8282\u70b9","text":"

        \u7ed9\u5b9a\u4e00\u4e2a\u5f85\u63d2\u5165\u5143\u7d20 num \uff0c\u4e3a\u4e86\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u201c\u5de6\u5b50\u6811 < \u6839\u8282\u70b9 < \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\uff0c\u63d2\u5165\u64cd\u4f5c\u6d41\u7a0b\u5982\u56fe 7-18 \u6240\u793a\u3002

        1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff1a\u4e0e\u67e5\u627e\u64cd\u4f5c\u76f8\u4f3c\uff0c\u4ece\u6839\u8282\u70b9\u51fa\u53d1\uff0c\u6839\u636e\u5f53\u524d\u8282\u70b9\u503c\u548c num \u7684\u5927\u5c0f\u5173\u7cfb\u5faa\u73af\u5411\u4e0b\u641c\u7d22\uff0c\u76f4\u5230\u8d8a\u8fc7\u53f6\u8282\u70b9\uff08\u904d\u5386\u81f3 None \uff09\u65f6\u8df3\u51fa\u5faa\u73af\u3002
        2. \u5728\u8be5\u4f4d\u7f6e\u63d2\u5165\u8282\u70b9\uff1a\u521d\u59cb\u5316\u8282\u70b9 num \uff0c\u5c06\u8be5\u8282\u70b9\u7f6e\u4e8e None \u7684\u4f4d\u7f6e\u3002

        \u56fe 7-18 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u63d2\u5165\u8282\u70b9

        \u5728\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u4e24\u70b9\u3002

        • \u4e8c\u53c9\u641c\u7d22\u6811\u4e0d\u5141\u8bb8\u5b58\u5728\u91cd\u590d\u8282\u70b9\uff0c\u5426\u5219\u5c06\u8fdd\u53cd\u5176\u5b9a\u4e49\u3002\u56e0\u6b64\uff0c\u82e5\u5f85\u63d2\u5165\u8282\u70b9\u5728\u6811\u4e2d\u5df2\u5b58\u5728\uff0c\u5219\u4e0d\u6267\u884c\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\u3002
        • \u4e3a\u4e86\u5b9e\u73b0\u63d2\u5165\u8282\u70b9\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u8282\u70b9 pre \u4fdd\u5b58\u4e0a\u4e00\u8f6e\u5faa\u73af\u7684\u8282\u70b9\u3002\u8fd9\u6837\u5728\u904d\u5386\u81f3 None \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u5230\u5176\u7236\u8282\u70b9\uff0c\u4ece\u800c\u5b8c\u6210\u8282\u70b9\u63d2\u5165\u64cd\u4f5c\u3002
        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_tree.py
        def insert(self, num: int):\n    \"\"\"\u63d2\u5165\u8282\u70b9\"\"\"\n    # \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n    if self._root is None:\n        self._root = TreeNode(num)\n        return\n    # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    cur, pre = self._root, None\n    while cur is not None:\n        # \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n        if cur.val == num:\n            return\n        pre = cur\n        # \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if cur.val < num:\n            cur = cur.right\n        # \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else:\n            cur = cur.left\n    # \u63d2\u5165\u8282\u70b9\n    node = TreeNode(num)\n    if pre.val < num:\n        pre.right = node\n    else:\n        pre.left = node\n
        binary_search_tree.cpp
        /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n    if (root == nullptr) {\n        root = new TreeNode(num);\n        return;\n    }\n    TreeNode *cur = root, *pre = nullptr;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != nullptr) {\n        // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n        if (cur->val == num)\n            return;\n        pre = cur;\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur->val < num)\n            cur = cur->right;\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else\n            cur = cur->left;\n    }\n    // \u63d2\u5165\u8282\u70b9\n    TreeNode *node = new TreeNode(num);\n    if (pre->val < num)\n        pre->right = node;\n    else\n        pre->left = node;\n}\n
        binary_search_tree.java
        /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n    if (root == null) {\n        root = new TreeNode(num);\n        return;\n    }\n    TreeNode cur = root, pre = null;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != null) {\n        // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n        if (cur.val == num)\n            return;\n        pre = cur;\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num)\n            cur = cur.right;\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else\n            cur = cur.left;\n    }\n    // \u63d2\u5165\u8282\u70b9\n    TreeNode node = new TreeNode(num);\n    if (pre.val < num)\n        pre.right = node;\n    else\n        pre.left = node;\n}\n
        binary_search_tree.cs
        /* \u63d2\u5165\u8282\u70b9 */\nvoid Insert(int num) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n    if (root == null) {\n        root = new TreeNode(num);\n        return;\n    }\n    TreeNode? cur = root, pre = null;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != null) {\n        // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n        if (cur.val == num)\n            return;\n        pre = cur;\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num)\n            cur = cur.right;\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else\n            cur = cur.left;\n    }\n\n    // \u63d2\u5165\u8282\u70b9\n    TreeNode node = new(num);\n    if (pre != null) {\n        if (pre.val < num)\n            pre.right = node;\n        else\n            pre.left = node;\n    }\n}\n
        binary_search_tree.go
        /* \u63d2\u5165\u8282\u70b9 */\nfunc (bst *binarySearchTree) insert(num int) {\n    cur := bst.root\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n    if cur == nil {\n        bst.root = NewTreeNode(num)\n        return\n    }\n    // \u5f85\u63d2\u5165\u8282\u70b9\u4e4b\u524d\u7684\u8282\u70b9\u4f4d\u7f6e\n    var pre *TreeNode = nil\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    for cur != nil {\n        if cur.Val == num {\n            return\n        }\n        pre = cur\n        if cur.Val.(int) < num {\n            cur = cur.Right\n        } else {\n            cur = cur.Left\n        }\n    }\n    // \u63d2\u5165\u8282\u70b9\n    node := NewTreeNode(num)\n    if pre.Val.(int) < num {\n        pre.Right = node\n    } else {\n        pre.Left = node\n    }\n}\n
        binary_search_tree.swift
        /* \u63d2\u5165\u8282\u70b9 */\nfunc insert(num: Int) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n    if root == nil {\n        root = TreeNode(x: num)\n        return\n    }\n    var cur = root\n    var pre: TreeNode?\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while cur != nil {\n        // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n        if cur!.val == num {\n            return\n        }\n        pre = cur\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if cur!.val < num {\n            cur = cur?.right\n        }\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else {\n            cur = cur?.left\n        }\n    }\n    // \u63d2\u5165\u8282\u70b9\n    let node = TreeNode(x: num)\n    if pre!.val < num {\n        pre?.right = node\n    } else {\n        pre?.left = node\n    }\n}\n
        binary_search_tree.js
        /* \u63d2\u5165\u8282\u70b9 */\ninsert(num) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n    if (this.root === null) {\n        this.root = new TreeNode(num);\n        return;\n    }\n    let cur = this.root,\n        pre = null;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur !== null) {\n        // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n        if (cur.val === num) return;\n        pre = cur;\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num) cur = cur.right;\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else cur = cur.left;\n    }\n    // \u63d2\u5165\u8282\u70b9\n    const node = new TreeNode(num);\n    if (pre.val < num) pre.right = node;\n    else pre.left = node;\n}\n
        binary_search_tree.ts
        /* \u63d2\u5165\u8282\u70b9 */\ninsert(num: number): void {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n    if (this.root === null) {\n        this.root = new TreeNode(num);\n        return;\n    }\n    let cur: TreeNode | null = this.root,\n        pre: TreeNode | null = null;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur !== null) {\n        // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n        if (cur.val === num) return;\n        pre = cur;\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num) cur = cur.right;\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else cur = cur.left;\n    }\n    // \u63d2\u5165\u8282\u70b9\n    const node = new TreeNode(num);\n    if (pre!.val < num) pre!.right = node;\n    else pre!.left = node;\n}\n
        binary_search_tree.dart
        /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int _num) {\n  // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n  if (_root == null) {\n    _root = TreeNode(_num);\n    return;\n  }\n  TreeNode? cur = _root;\n  TreeNode? pre = null;\n  // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n  while (cur != null) {\n    // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n    if (cur.val == _num) return;\n    pre = cur;\n    // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n    if (cur.val < _num)\n      cur = cur.right;\n    // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n    else\n      cur = cur.left;\n  }\n  // \u63d2\u5165\u8282\u70b9\n  TreeNode? node = TreeNode(_num);\n  if (pre!.val < _num)\n    pre.right = node;\n  else\n    pre.left = node;\n}\n
        binary_search_tree.rs
        /* \u63d2\u5165\u8282\u70b9 */\npub fn insert(&mut self, num: i32) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n    if self.root.is_none() {\n        self.root = Some(TreeNode::new(num));\n        return;\n    }\n    let mut cur = self.root.clone();\n    let mut pre = None;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while let Some(node) = cur.clone() {\n        match num.cmp(&node.borrow().val) {\n            // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n            Ordering::Equal => return,\n            // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n            Ordering::Greater => {\n                pre = cur.clone();\n                cur = node.borrow().right.clone();\n            }\n            // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n            Ordering::Less => {\n                pre = cur.clone();\n                cur = node.borrow().left.clone();\n            }\n        }\n    }\n    // \u63d2\u5165\u8282\u70b9\n    let pre = pre.unwrap();\n    let node = Some(TreeNode::new(num));\n    if num > pre.borrow().val {\n        pre.borrow_mut().right = node;\n    } else {\n        pre.borrow_mut().left = node;\n    }\n}\n
        binary_search_tree.c
        /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(BinarySearchTree *bst, int num) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n    if (bst->root == NULL) {\n        bst->root = newTreeNode(num);\n        return;\n    }\n    TreeNode *cur = bst->root, *pre = NULL;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != NULL) {\n        // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n        if (cur->val == num) {\n            return;\n        }\n        pre = cur;\n        if (cur->val < num) {\n            // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n            cur = cur->right;\n        } else {\n            // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n            cur = cur->left;\n        }\n    }\n    // \u63d2\u5165\u8282\u70b9\n    TreeNode *node = newTreeNode(num);\n    if (pre->val < num) {\n        pre->right = node;\n    } else {\n        pre->left = node;\n    }\n}\n
        binary_search_tree.kt
        /* \u63d2\u5165\u8282\u70b9 */\nfun insert(num: Int) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n    if (root == null) {\n        root = TreeNode(num)\n        return\n    }\n    var cur = root\n    var pre: TreeNode? = null\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != null) {\n        // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n        if (cur._val == num)\n            return\n        pre = cur\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        cur = if (cur._val < num)\n            cur.right\n        // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else\n            cur.left\n    }\n    // \u63d2\u5165\u8282\u70b9\n    val node = TreeNode(num)\n    if (pre?._val!! < num)\n        pre.right = node\n    else\n        pre.left = node\n}\n
        binary_search_tree.rb
        ### \u63d2\u5165\u8282\u70b9 ###\ndef insert(num)\n  # \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n  if @root.nil?\n    @root = TreeNode.new(num)\n    return\n  end\n\n  # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n  cur, pre = @root, nil\n  while !cur.nil?\n    # \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n    return if cur.val == num\n\n    pre = cur\n    # \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n    if cur.val < num\n      cur = cur.right\n    # \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n    else\n      cur = cur.left\n    end\n  end\n\n  # \u63d2\u5165\u8282\u70b9\n  node = TreeNode.new(num)\n  if pre.val < num\n    pre.right = node\n  else\n    pre.left = node\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        \u4e0e\u67e5\u627e\u8282\u70b9\u76f8\u540c\uff0c\u63d2\u5165\u8282\u70b9\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\u3002

        "},{"location":"chapter_tree/binary_search_tree/#3","title":"3. \u00a0 \u5220\u9664\u8282\u70b9","text":"

        \u5148\u5728\u4e8c\u53c9\u6811\u4e2d\u67e5\u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u518d\u5c06\u5176\u5220\u9664\u3002\u4e0e\u63d2\u5165\u8282\u70b9\u7c7b\u4f3c\uff0c\u6211\u4eec\u9700\u8981\u4fdd\u8bc1\u5728\u5220\u9664\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u201c\u5de6\u5b50\u6811 < \u6839\u8282\u70b9 < \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\u4ecd\u7136\u6ee1\u8db3\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u6839\u636e\u76ee\u6807\u8282\u70b9\u7684\u5b50\u8282\u70b9\u6570\u91cf\uff0c\u5206 0\u30011 \u548c 2 \u4e09\u79cd\u60c5\u51b5\uff0c\u6267\u884c\u5bf9\u5e94\u7684\u5220\u9664\u8282\u70b9\u64cd\u4f5c\u3002

        \u5982\u56fe 7-19 \u6240\u793a\uff0c\u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(0\\) \u65f6\uff0c\u8868\u793a\u8be5\u8282\u70b9\u662f\u53f6\u8282\u70b9\uff0c\u53ef\u4ee5\u76f4\u63a5\u5220\u9664\u3002

        \u56fe 7-19 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 0 \uff09

        \u5982\u56fe 7-20 \u6240\u793a\uff0c\u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(1\\) \u65f6\uff0c\u5c06\u5f85\u5220\u9664\u8282\u70b9\u66ff\u6362\u4e3a\u5176\u5b50\u8282\u70b9\u5373\u53ef\u3002

        \u56fe 7-20 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 1 \uff09

        \u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(2\\) \u65f6\uff0c\u6211\u4eec\u65e0\u6cd5\u76f4\u63a5\u5220\u9664\u5b83\uff0c\u800c\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u8282\u70b9\u66ff\u6362\u8be5\u8282\u70b9\u3002\u7531\u4e8e\u8981\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u201c\u5de6\u5b50\u6811 \\(<\\) \u6839\u8282\u70b9 \\(<\\) \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\uff0c\u56e0\u6b64\u8fd9\u4e2a\u8282\u70b9\u53ef\u4ee5\u662f\u53f3\u5b50\u6811\u7684\u6700\u5c0f\u8282\u70b9\u6216\u5de6\u5b50\u6811\u7684\u6700\u5927\u8282\u70b9\u3002

        \u5047\u8bbe\u6211\u4eec\u9009\u62e9\u53f3\u5b50\u6811\u7684\u6700\u5c0f\u8282\u70b9\uff08\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\uff09\uff0c\u5219\u5220\u9664\u64cd\u4f5c\u6d41\u7a0b\u5982\u56fe 7-21 \u6240\u793a\u3002

        1. \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\u5728\u201c\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u201d\u4e2d\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\uff0c\u8bb0\u4e3a tmp \u3002
        2. \u7528 tmp \u7684\u503c\u8986\u76d6\u5f85\u5220\u9664\u8282\u70b9\u7684\u503c\uff0c\u5e76\u5728\u6811\u4e2d\u9012\u5f52\u5220\u9664\u8282\u70b9 tmp \u3002
        <1><2><3><4>

        \u56fe 7-21 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 2 \uff09

        \u5220\u9664\u8282\u70b9\u64cd\u4f5c\u540c\u6837\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\uff0c\u5176\u4e2d\u67e5\u627e\u5f85\u5220\u9664\u8282\u70b9\u9700\u8981 \\(O(\\log n)\\) \u65f6\u95f4\uff0c\u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u540e\u7ee7\u8282\u70b9\u9700\u8981 \\(O(\\log n)\\) \u65f6\u95f4\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_tree.py
        def remove(self, num: int):\n    \"\"\"\u5220\u9664\u8282\u70b9\"\"\"\n    # \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n    if self._root is None:\n        return\n    # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    cur, pre = self._root, None\n    while cur is not None:\n        # \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        if cur.val == num:\n            break\n        pre = cur\n        # \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if cur.val < num:\n            cur = cur.right\n        # \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else:\n            cur = cur.left\n    # \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if cur is None:\n        return\n\n    # \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n    if cur.left is None or cur.right is None:\n        # \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n        child = cur.left or cur.right\n        # \u5220\u9664\u8282\u70b9 cur\n        if cur != self._root:\n            if pre.left == cur:\n                pre.left = child\n            else:\n                pre.right = child\n        else:\n            # \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n            self._root = child\n    # \u5b50\u8282\u70b9\u6570\u91cf = 2\n    else:\n        # \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n        tmp: TreeNode = cur.right\n        while tmp.left is not None:\n            tmp = tmp.left\n        # \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n        self.remove(tmp.val)\n        # \u7528 tmp \u8986\u76d6 cur\n        cur.val = tmp.val\n
        binary_search_tree.cpp
        /* \u5220\u9664\u8282\u70b9 */\nvoid remove(int num) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n    if (root == nullptr)\n        return;\n    TreeNode *cur = root, *pre = nullptr;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != nullptr) {\n        // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        if (cur->val == num)\n            break;\n        pre = cur;\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur->val < num)\n            cur = cur->right;\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else\n            cur = cur->left;\n    }\n    // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (cur == nullptr)\n        return;\n    // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n    if (cur->left == nullptr || cur->right == nullptr) {\n        // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\n        TreeNode *child = cur->left != nullptr ? cur->left : cur->right;\n        // \u5220\u9664\u8282\u70b9 cur\n        if (cur != root) {\n            if (pre->left == cur)\n                pre->left = child;\n            else\n                pre->right = child;\n        } else {\n            // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n            root = child;\n        }\n        // \u91ca\u653e\u5185\u5b58\n        delete cur;\n    }\n    // \u5b50\u8282\u70b9\u6570\u91cf = 2\n    else {\n        // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n        TreeNode *tmp = cur->right;\n        while (tmp->left != nullptr) {\n            tmp = tmp->left;\n        }\n        int tmpVal = tmp->val;\n        // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n        remove(tmp->val);\n        // \u7528 tmp \u8986\u76d6 cur\n        cur->val = tmpVal;\n    }\n}\n
        binary_search_tree.java
        /* \u5220\u9664\u8282\u70b9 */\nvoid remove(int num) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n    if (root == null)\n        return;\n    TreeNode cur = root, pre = null;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != null) {\n        // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        if (cur.val == num)\n            break;\n        pre = cur;\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num)\n            cur = cur.right;\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else\n            cur = cur.left;\n    }\n    // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (cur == null)\n        return;\n    // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n    if (cur.left == null || cur.right == null) {\n        // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n        TreeNode child = cur.left != null ? cur.left : cur.right;\n        // \u5220\u9664\u8282\u70b9 cur\n        if (cur != root) {\n            if (pre.left == cur)\n                pre.left = child;\n            else\n                pre.right = child;\n        } else {\n            // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n            root = child;\n        }\n    }\n    // \u5b50\u8282\u70b9\u6570\u91cf = 2\n    else {\n        // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n        TreeNode tmp = cur.right;\n        while (tmp.left != null) {\n            tmp = tmp.left;\n        }\n        // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n        remove(tmp.val);\n        // \u7528 tmp \u8986\u76d6 cur\n        cur.val = tmp.val;\n    }\n}\n
        binary_search_tree.cs
        /* \u5220\u9664\u8282\u70b9 */\nvoid Remove(int num) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n    if (root == null)\n        return;\n    TreeNode? cur = root, pre = null;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != null) {\n        // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        if (cur.val == num)\n            break;\n        pre = cur;\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num)\n            cur = cur.right;\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else\n            cur = cur.left;\n    }\n    // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (cur == null)\n        return;\n    // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n    if (cur.left == null || cur.right == null) {\n        // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n        TreeNode? child = cur.left ?? cur.right;\n        // \u5220\u9664\u8282\u70b9 cur\n        if (cur != root) {\n            if (pre!.left == cur)\n                pre.left = child;\n            else\n                pre.right = child;\n        } else {\n            // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n            root = child;\n        }\n    }\n    // \u5b50\u8282\u70b9\u6570\u91cf = 2\n    else {\n        // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n        TreeNode? tmp = cur.right;\n        while (tmp.left != null) {\n            tmp = tmp.left;\n        }\n        // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n        Remove(tmp.val!.Value);\n        // \u7528 tmp \u8986\u76d6 cur\n        cur.val = tmp.val;\n    }\n}\n
        binary_search_tree.go
        /* \u5220\u9664\u8282\u70b9 */\nfunc (bst *binarySearchTree) remove(num int) {\n    cur := bst.root\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n    if cur == nil {\n        return\n    }\n    // \u5f85\u5220\u9664\u8282\u70b9\u4e4b\u524d\u7684\u8282\u70b9\u4f4d\u7f6e\n    var pre *TreeNode = nil\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    for cur != nil {\n        if cur.Val == num {\n            break\n        }\n        pre = cur\n        if cur.Val.(int) < num {\n            // \u5f85\u5220\u9664\u8282\u70b9\u5728\u53f3\u5b50\u6811\u4e2d\n            cur = cur.Right\n        } else {\n            // \u5f85\u5220\u9664\u8282\u70b9\u5728\u5de6\u5b50\u6811\u4e2d\n            cur = cur.Left\n        }\n    }\n    // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if cur == nil {\n        return\n    }\n    // \u5b50\u8282\u70b9\u6570\u4e3a 0 \u6216 1\n    if cur.Left == nil || cur.Right == nil {\n        var child *TreeNode = nil\n        // \u53d6\u51fa\u5f85\u5220\u9664\u8282\u70b9\u7684\u5b50\u8282\u70b9\n        if cur.Left != nil {\n            child = cur.Left\n        } else {\n            child = cur.Right\n        }\n        // \u5220\u9664\u8282\u70b9 cur\n        if cur != bst.root {\n            if pre.Left == cur {\n                pre.Left = child\n            } else {\n                pre.Right = child\n            }\n        } else {\n            // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n            bst.root = child\n        }\n        // \u5b50\u8282\u70b9\u6570\u4e3a 2\n    } else {\n        // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u5f85\u5220\u9664\u8282\u70b9 cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n        tmp := cur.Right\n        for tmp.Left != nil {\n            tmp = tmp.Left\n        }\n        // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n        bst.remove(tmp.Val.(int))\n        // \u7528 tmp \u8986\u76d6 cur\n        cur.Val = tmp.Val\n    }\n}\n
        binary_search_tree.swift
        /* \u5220\u9664\u8282\u70b9 */\nfunc remove(num: Int) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n    if root == nil {\n        return\n    }\n    var cur = root\n    var pre: TreeNode?\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while cur != nil {\n        // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        if cur!.val == num {\n            break\n        }\n        pre = cur\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if cur!.val < num {\n            cur = cur?.right\n        }\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else {\n            cur = cur?.left\n        }\n    }\n    // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if cur == nil {\n        return\n    }\n    // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n    if cur?.left == nil || cur?.right == nil {\n        // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n        let child = cur?.left ?? cur?.right\n        // \u5220\u9664\u8282\u70b9 cur\n        if cur !== root {\n            if pre?.left === cur {\n                pre?.left = child\n            } else {\n                pre?.right = child\n            }\n        } else {\n            // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n            root = child\n        }\n    }\n    // \u5b50\u8282\u70b9\u6570\u91cf = 2\n    else {\n        // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n        var tmp = cur?.right\n        while tmp?.left != nil {\n            tmp = tmp?.left\n        }\n        // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n        remove(num: tmp!.val)\n        // \u7528 tmp \u8986\u76d6 cur\n        cur?.val = tmp!.val\n    }\n}\n
        binary_search_tree.js
        /* \u5220\u9664\u8282\u70b9 */\nremove(num) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n    if (this.root === null) return;\n    let cur = this.root,\n        pre = null;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur !== null) {\n        // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        if (cur.val === num) break;\n        pre = cur;\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num) cur = cur.right;\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else cur = cur.left;\n    }\n    // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (cur === null) return;\n    // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n    if (cur.left === null || cur.right === null) {\n        // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n        const child = cur.left !== null ? cur.left : cur.right;\n        // \u5220\u9664\u8282\u70b9 cur\n        if (cur !== this.root) {\n            if (pre.left === cur) pre.left = child;\n            else pre.right = child;\n        } else {\n            // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n            this.root = child;\n        }\n    }\n    // \u5b50\u8282\u70b9\u6570\u91cf = 2\n    else {\n        // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n        let tmp = cur.right;\n        while (tmp.left !== null) {\n            tmp = tmp.left;\n        }\n        // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n        this.remove(tmp.val);\n        // \u7528 tmp \u8986\u76d6 cur\n        cur.val = tmp.val;\n    }\n}\n
        binary_search_tree.ts
        /* \u5220\u9664\u8282\u70b9 */\nremove(num: number): void {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n    if (this.root === null) return;\n    let cur: TreeNode | null = this.root,\n        pre: TreeNode | null = null;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur !== null) {\n        // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        if (cur.val === num) break;\n        pre = cur;\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        if (cur.val < num) cur = cur.right;\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else cur = cur.left;\n    }\n    // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (cur === null) return;\n    // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n    if (cur.left === null || cur.right === null) {\n        // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n        const child: TreeNode | null =\n            cur.left !== null ? cur.left : cur.right;\n        // \u5220\u9664\u8282\u70b9 cur\n        if (cur !== this.root) {\n            if (pre!.left === cur) pre!.left = child;\n            else pre!.right = child;\n        } else {\n            // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n            this.root = child;\n        }\n    }\n    // \u5b50\u8282\u70b9\u6570\u91cf = 2\n    else {\n        // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n        let tmp: TreeNode | null = cur.right;\n        while (tmp!.left !== null) {\n            tmp = tmp!.left;\n        }\n        // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n        this.remove(tmp!.val);\n        // \u7528 tmp \u8986\u76d6 cur\n        cur.val = tmp!.val;\n    }\n}\n
        binary_search_tree.dart
        /* \u5220\u9664\u8282\u70b9 */\nvoid remove(int _num) {\n  // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n  if (_root == null) return;\n  TreeNode? cur = _root;\n  TreeNode? pre = null;\n  // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n  while (cur != null) {\n    // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n    if (cur.val == _num) break;\n    pre = cur;\n    // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n    if (cur.val < _num)\n      cur = cur.right;\n    // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n    else\n      cur = cur.left;\n  }\n  // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n  if (cur == null) return;\n  // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n  if (cur.left == null || cur.right == null) {\n    // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n    TreeNode? child = cur.left ?? cur.right;\n    // \u5220\u9664\u8282\u70b9 cur\n    if (cur != _root) {\n      if (pre!.left == cur)\n        pre.left = child;\n      else\n        pre.right = child;\n    } else {\n      // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n      _root = child;\n    }\n  } else {\n    // \u5b50\u8282\u70b9\u6570\u91cf = 2\n    // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n    TreeNode? tmp = cur.right;\n    while (tmp!.left != null) {\n      tmp = tmp.left;\n    }\n    // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n    remove(tmp.val);\n    // \u7528 tmp \u8986\u76d6 cur\n    cur.val = tmp.val;\n  }\n}\n
        binary_search_tree.rs
        /* \u5220\u9664\u8282\u70b9 */\npub fn remove(&mut self, num: i32) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n    if self.root.is_none() {\n        return;\n    }\n    let mut cur = self.root.clone();\n    let mut pre = None;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while let Some(node) = cur.clone() {\n        match num.cmp(&node.borrow().val) {\n            // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n            Ordering::Equal => break,\n            // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n            Ordering::Greater => {\n                pre = cur.clone();\n                cur = node.borrow().right.clone();\n            }\n            // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n            Ordering::Less => {\n                pre = cur.clone();\n                cur = node.borrow().left.clone();\n            }\n        }\n    }\n    // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if cur.is_none() {\n        return;\n    }\n    let cur = cur.unwrap();\n    let (left_child, right_child) = (cur.borrow().left.clone(), cur.borrow().right.clone());\n    match (left_child.clone(), right_child.clone()) {\n        // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n        (None, None) | (Some(_), None) | (None, Some(_)) => {\n            // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\n            let child = left_child.or(right_child);\n            let pre = pre.unwrap();\n            // \u5220\u9664\u8282\u70b9 cur\n            if !Rc::ptr_eq(&cur, self.root.as_ref().unwrap()) {\n                let left = pre.borrow().left.clone();\n                if left.is_some() && Rc::ptr_eq(left.as_ref().unwrap(), &cur) {\n                    pre.borrow_mut().left = child;\n                } else {\n                    pre.borrow_mut().right = child;\n                }\n            } else {\n                // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n                self.root = child;\n            }\n        }\n        // \u5b50\u8282\u70b9\u6570\u91cf = 2\n        (Some(_), Some(_)) => {\n            // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n            let mut tmp = cur.borrow().right.clone();\n            while let Some(node) = tmp.clone() {\n                if node.borrow().left.is_some() {\n                    tmp = node.borrow().left.clone();\n                } else {\n                    break;\n                }\n            }\n            let tmp_val = tmp.unwrap().borrow().val;\n            // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n            self.remove(tmp_val);\n            // \u7528 tmp \u8986\u76d6 cur\n            cur.borrow_mut().val = tmp_val;\n        }\n    }\n}\n
        binary_search_tree.c
        /* \u5220\u9664\u8282\u70b9 */\n// \u7531\u4e8e\u5f15\u5165\u4e86 stdio.h \uff0c\u6b64\u5904\u65e0\u6cd5\u4f7f\u7528 remove \u5173\u952e\u8bcd\nvoid removeItem(BinarySearchTree *bst, int num) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n    if (bst->root == NULL)\n        return;\n    TreeNode *cur = bst->root, *pre = NULL;\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != NULL) {\n        // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        if (cur->val == num)\n            break;\n        pre = cur;\n        if (cur->val < num) {\n            // \u5f85\u5220\u9664\u8282\u70b9\u5728 root \u7684\u53f3\u5b50\u6811\u4e2d\n            cur = cur->right;\n        } else {\n            // \u5f85\u5220\u9664\u8282\u70b9\u5728 root \u7684\u5de6\u5b50\u6811\u4e2d\n            cur = cur->left;\n        }\n    }\n    // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (cur == NULL)\n        return;\n    // \u5224\u65ad\u5f85\u5220\u9664\u8282\u70b9\u662f\u5426\u5b58\u5728\u5b50\u8282\u70b9\n    if (cur->left == NULL || cur->right == NULL) {\n        /* \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1 */\n        // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\n        TreeNode *child = cur->left != NULL ? cur->left : cur->right;\n        // \u5220\u9664\u8282\u70b9 cur\n        if (pre->left == cur) {\n            pre->left = child;\n        } else {\n            pre->right = child;\n        }\n        // \u91ca\u653e\u5185\u5b58\n        free(cur);\n    } else {\n        /* \u5b50\u8282\u70b9\u6570\u91cf = 2 */\n        // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n        TreeNode *tmp = cur->right;\n        while (tmp->left != NULL) {\n            tmp = tmp->left;\n        }\n        int tmpVal = tmp->val;\n        // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n        removeItem(bst, tmp->val);\n        // \u7528 tmp \u8986\u76d6 cur\n        cur->val = tmpVal;\n    }\n}\n
        binary_search_tree.kt
        /* \u5220\u9664\u8282\u70b9 */\nfun remove(num: Int) {\n    // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n    if (root == null)\n        return\n    var cur = root\n    var pre: TreeNode? = null\n    // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n    while (cur != null) {\n        // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n        if (cur._val == num)\n            break\n        pre = cur\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n        cur = if (cur._val < num)\n            cur.right\n        // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n        else\n            cur.left\n    }\n    // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (cur == null)\n        return\n    // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n    if (cur.left == null || cur.right == null) {\n        // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n        val child = if (cur.left != null)\n            cur.left\n        else\n            cur.right\n        // \u5220\u9664\u8282\u70b9 cur\n        if (cur != root) {\n            if (pre!!.left == cur)\n                pre.left = child\n            else\n                pre.right = child\n        } else {\n            // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n            root = child\n        }\n        // \u5b50\u8282\u70b9\u6570\u91cf = 2\n    } else {\n        // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n        var tmp = cur.right\n        while (tmp!!.left != null) {\n            tmp = tmp.left\n        }\n        // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n        remove(tmp._val)\n        // \u7528 tmp \u8986\u76d6 cur\n        cur._val = tmp._val\n    }\n}\n
        binary_search_tree.rb
        ### \u5220\u9664\u8282\u70b9 ###\ndef remove(num)\n  # \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n  return if @root.nil?\n\n  # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n  cur, pre = @root, nil\n  while !cur.nil?\n    # \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n    break if cur.val == num\n\n    pre = cur\n    # \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n    if cur.val < num\n      cur = cur.right\n    # \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n    else\n      cur = cur.left\n    end\n  end\n  # \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n  return if cur.nil?\n\n  # \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n  if cur.left.nil? || cur.right.nil?\n    # \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n    child = cur.left || cur.right\n    # \u5220\u9664\u8282\u70b9 cur\n    if cur != @root\n      if pre.left == cur\n        pre.left = child\n      else\n        pre.right = child\n      end\n    else\n      # \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n      @root = child\n    end\n  # \u5b50\u8282\u70b9\u6570\u91cf = 2\n  else\n    # \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n    tmp = cur.right\n    while !tmp.left.nil?\n      tmp = tmp.left\n    end\n    # \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n    remove(tmp.val)\n    # \u7528 tmp \u8986\u76d6 cur\n    cur.val = tmp.val\n  end\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_tree/binary_search_tree/#4","title":"4. \u00a0 \u4e2d\u5e8f\u904d\u5386\u6709\u5e8f","text":"

        \u5982\u56fe 7-22 \u6240\u793a\uff0c\u4e8c\u53c9\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u9075\u5faa\u201c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u201d\u7684\u904d\u5386\u987a\u5e8f\uff0c\u800c\u4e8c\u53c9\u641c\u7d22\u6811\u6ee1\u8db3\u201c\u5de6\u5b50\u8282\u70b9 \\(<\\) \u6839\u8282\u70b9 \\(<\\) \u53f3\u5b50\u8282\u70b9\u201d\u7684\u5927\u5c0f\u5173\u7cfb\u3002

        \u8fd9\u610f\u5473\u7740\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u8fdb\u884c\u4e2d\u5e8f\u904d\u5386\u65f6\uff0c\u603b\u662f\u4f1a\u4f18\u5148\u904d\u5386\u4e0b\u4e00\u4e2a\u6700\u5c0f\u8282\u70b9\uff0c\u4ece\u800c\u5f97\u51fa\u4e00\u4e2a\u91cd\u8981\u6027\u8d28\uff1a\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u662f\u5347\u5e8f\u7684\u3002

        \u5229\u7528\u4e2d\u5e8f\u904d\u5386\u5347\u5e8f\u7684\u6027\u8d28\uff0c\u6211\u4eec\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u83b7\u53d6\u6709\u5e8f\u6570\u636e\u4ec5\u9700 \\(O(n)\\) \u65f6\u95f4\uff0c\u65e0\u987b\u8fdb\u884c\u989d\u5916\u7684\u6392\u5e8f\u64cd\u4f5c\uff0c\u975e\u5e38\u9ad8\u6548\u3002

        \u56fe 7-22 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217

        "},{"location":"chapter_tree/binary_search_tree/#742","title":"7.4.2 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6548\u7387","text":"

        \u7ed9\u5b9a\u4e00\u7ec4\u6570\u636e\uff0c\u6211\u4eec\u8003\u8651\u4f7f\u7528\u6570\u7ec4\u6216\u4e8c\u53c9\u641c\u7d22\u6811\u5b58\u50a8\u3002\u89c2\u5bdf\u8868 7-2 \uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5404\u9879\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f\u5bf9\u6570\u9636\uff0c\u5177\u6709\u7a33\u5b9a\u4e14\u9ad8\u6548\u7684\u6027\u80fd\u3002\u53ea\u6709\u5728\u9ad8\u9891\u6dfb\u52a0\u3001\u4f4e\u9891\u67e5\u627e\u5220\u9664\u6570\u636e\u7684\u573a\u666f\u4e0b\uff0c\u6570\u7ec4\u6bd4\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6548\u7387\u66f4\u9ad8\u3002

        \u8868 7-2 \u00a0 \u6570\u7ec4\u4e0e\u641c\u7d22\u6811\u7684\u6548\u7387\u5bf9\u6bd4

        \u65e0\u5e8f\u6570\u7ec4 \u4e8c\u53c9\u641c\u7d22\u6811 \u67e5\u627e\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\) \u63d2\u5165\u5143\u7d20 \\(O(1)\\) \\(O(\\log n)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\)

        \u5728\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u662f\u201c\u5e73\u8861\u201d\u7684\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728 \\(\\log n\\) \u8f6e\u5faa\u73af\u5185\u67e5\u627e\u4efb\u610f\u8282\u70b9\u3002

        \u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u4e0d\u65ad\u5730\u63d2\u5165\u548c\u5220\u9664\u8282\u70b9\uff0c\u53ef\u80fd\u5bfc\u81f4\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u56fe 7-23 \u6240\u793a\u7684\u94fe\u8868\uff0c\u8fd9\u65f6\u5404\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4f1a\u9000\u5316\u4e3a \\(O(n)\\) \u3002

        \u56fe 7-23 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u9000\u5316

        "},{"location":"chapter_tree/binary_search_tree/#743","title":"7.4.3 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u5e38\u89c1\u5e94\u7528","text":"
        • \u7528\u4f5c\u7cfb\u7edf\u4e2d\u7684\u591a\u7ea7\u7d22\u5f15\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u67e5\u627e\u3001\u63d2\u5165\u3001\u5220\u9664\u64cd\u4f5c\u3002
        • \u4f5c\u4e3a\u67d0\u4e9b\u641c\u7d22\u7b97\u6cd5\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002
        • \u7528\u4e8e\u5b58\u50a8\u6570\u636e\u6d41\uff0c\u4ee5\u4fdd\u6301\u5176\u6709\u5e8f\u72b6\u6001\u3002
        "},{"location":"chapter_tree/binary_tree/","title":"7.1 \u00a0 \u4e8c\u53c9\u6811","text":"

        \u4e8c\u53c9\u6811\uff08binary tree\uff09\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u4ee3\u8868\u201c\u7956\u5148\u201d\u4e0e\u201c\u540e\u4ee3\u201d\u4e4b\u95f4\u7684\u6d3e\u751f\u5173\u7cfb\uff0c\u4f53\u73b0\u4e86\u201c\u4e00\u5206\u4e3a\u4e8c\u201d\u7684\u5206\u6cbb\u903b\u8f91\u3002\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u4e8c\u53c9\u6811\u7684\u57fa\u672c\u5355\u5143\u662f\u8282\u70b9\uff0c\u6bcf\u4e2a\u8282\u70b9\u5305\u542b\u503c\u3001\u5de6\u5b50\u8282\u70b9\u5f15\u7528\u548c\u53f3\u5b50\u8282\u70b9\u5f15\u7528\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
        class TreeNode:\n    \"\"\"\u4e8c\u53c9\u6811\u8282\u70b9\u7c7b\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val                # \u8282\u70b9\u503c\n        self.left: TreeNode | None = None  # \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n        self.right: TreeNode | None = None # \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n
        /* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\n    int val;          // \u8282\u70b9\u503c\n    TreeNode *left;   // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n    TreeNode *right;  // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n};\n
        /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n    int val;         // \u8282\u70b9\u503c\n    TreeNode left;   // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n    TreeNode right;  // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n    TreeNode(int x) { val = x; }\n}\n
        /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode(int? x) {\n    public int? val = x;    // \u8282\u70b9\u503c\n    public TreeNode? left;  // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n    public TreeNode? right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n
        /* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype TreeNode struct {\n    Val   int\n    Left  *TreeNode\n    Right *TreeNode\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc NewTreeNode(v int) *TreeNode {\n    return &TreeNode{\n        Left:  nil, // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n        Right: nil, // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n        Val:   v,   // \u8282\u70b9\u503c\n    }\n}\n
        /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n    var val: Int // \u8282\u70b9\u503c\n    var left: TreeNode? // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n    var right: TreeNode? // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n\n    init(x: Int) {\n        val = x\n    }\n}\n
        /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n    val; // \u8282\u70b9\u503c\n    left; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n    right; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n    constructor(val, left, right) {\n        this.val = val === undefined ? 0 : val;\n        this.left = left === undefined ? null : left;\n        this.right = right === undefined ? null : right;\n    }\n}\n
        /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n    val: number;\n    left: TreeNode | null;\n    right: TreeNode | null;\n\n    constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {\n        this.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\n        this.left = left === undefined ? null : left; // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n        this.right = right === undefined ? null : right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n    }\n}\n
        /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n  int val;         // \u8282\u70b9\u503c\n  TreeNode? left;  // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n  TreeNode? right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n  TreeNode(this.val, [this.left, this.right]);\n}\n
        use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\n    val: i32,                               // \u8282\u70b9\u503c\n    left: Option<Rc<RefCell<TreeNode>>>,    // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n    right: Option<Rc<RefCell<TreeNode>>>,   // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n\nimpl TreeNode {\n    /* \u6784\u9020\u65b9\u6cd5 */\n    fn new(val: i32) -> Rc<RefCell<Self>> {\n        Rc::new(RefCell::new(Self {\n            val,\n            left: None,\n            right: None\n        }))\n    }\n}\n
        /* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\ntypedef struct TreeNode {\n    int val;                // \u8282\u70b9\u503c\n    int height;             // \u8282\u70b9\u9ad8\u5ea6\n    struct TreeNode *left;  // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n    struct TreeNode *right; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n} TreeNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nTreeNode *newTreeNode(int val) {\n    TreeNode *node;\n\n    node = (TreeNode *)malloc(sizeof(TreeNode));\n    node->val = val;\n    node->height = 0;\n    node->left = NULL;\n    node->right = NULL;\n    return node;\n}\n
        /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode(val _val: Int) {  // \u8282\u70b9\u503c\n    val left: TreeNode? = null   // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n    val right: TreeNode? = null  // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n
        ### \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b ###\nclass TreeNode\n  attr_accessor :val    # \u8282\u70b9\u503c\n  attr_accessor :left   # \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n  attr_accessor :right  # \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n\n  def initialize(val)\n    @val = val\n  end\nend\n

        \u6bcf\u4e2a\u8282\u70b9\u90fd\u6709\u4e24\u4e2a\u5f15\u7528\uff08\u6307\u9488\uff09\uff0c\u5206\u522b\u6307\u5411\u5de6\u5b50\u8282\u70b9\uff08left-child node\uff09\u548c\u53f3\u5b50\u8282\u70b9\uff08right-child node\uff09\uff0c\u8be5\u8282\u70b9\u88ab\u79f0\u4e3a\u8fd9\u4e24\u4e2a\u5b50\u8282\u70b9\u7684\u7236\u8282\u70b9\uff08parent node\uff09\u3002\u5f53\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u65f6\uff0c\u6211\u4eec\u5c06\u8be5\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u79f0\u4e3a\u8be5\u8282\u70b9\u7684\u5de6\u5b50\u6811\uff08left subtree\uff09\uff0c\u540c\u7406\u53ef\u5f97\u53f3\u5b50\u6811\uff08right subtree\uff09\u3002

        \u5728\u4e8c\u53c9\u6811\u4e2d\uff0c\u9664\u53f6\u8282\u70b9\u5916\uff0c\u5176\u4ed6\u6240\u6709\u8282\u70b9\u90fd\u5305\u542b\u5b50\u8282\u70b9\u548c\u975e\u7a7a\u5b50\u6811\u3002\u5982\u56fe 7-1 \u6240\u793a\uff0c\u5982\u679c\u5c06\u201c\u8282\u70b9 2\u201d\u89c6\u4e3a\u7236\u8282\u70b9\uff0c\u5219\u5176\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\u5206\u522b\u662f\u201c\u8282\u70b9 4\u201d\u548c\u201c\u8282\u70b9 5\u201d\uff0c\u5de6\u5b50\u6811\u662f\u201c\u8282\u70b9 4 \u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u201d\uff0c\u53f3\u5b50\u6811\u662f\u201c\u8282\u70b9 5 \u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u201d\u3002

        \u56fe 7-1 \u00a0 \u7236\u8282\u70b9\u3001\u5b50\u8282\u70b9\u3001\u5b50\u6811

        "},{"location":"chapter_tree/binary_tree/#711","title":"7.1.1 \u00a0 \u4e8c\u53c9\u6811\u5e38\u89c1\u672f\u8bed","text":"

        \u4e8c\u53c9\u6811\u7684\u5e38\u7528\u672f\u8bed\u5982\u56fe 7-2 \u6240\u793a\u3002

        • \u6839\u8282\u70b9\uff08root node\uff09\uff1a\u4f4d\u4e8e\u4e8c\u53c9\u6811\u9876\u5c42\u7684\u8282\u70b9\uff0c\u6ca1\u6709\u7236\u8282\u70b9\u3002
        • \u53f6\u8282\u70b9\uff08leaf node\uff09\uff1a\u6ca1\u6709\u5b50\u8282\u70b9\u7684\u8282\u70b9\uff0c\u5176\u4e24\u4e2a\u6307\u9488\u5747\u6307\u5411 None \u3002
        • \u8fb9\uff08edge\uff09\uff1a\u8fde\u63a5\u4e24\u4e2a\u8282\u70b9\u7684\u7ebf\u6bb5\uff0c\u5373\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\u3002
        • \u8282\u70b9\u6240\u5728\u7684\u5c42\uff08level\uff09\uff1a\u4ece\u9876\u81f3\u5e95\u9012\u589e\uff0c\u6839\u8282\u70b9\u6240\u5728\u5c42\u4e3a 1 \u3002
        • \u8282\u70b9\u7684\u5ea6\uff08degree\uff09\uff1a\u8282\u70b9\u7684\u5b50\u8282\u70b9\u7684\u6570\u91cf\u3002\u5728\u4e8c\u53c9\u6811\u4e2d\uff0c\u5ea6\u7684\u53d6\u503c\u8303\u56f4\u662f 0\u30011\u30012 \u3002
        • \u4e8c\u53c9\u6811\u7684\u9ad8\u5ea6\uff08height\uff09\uff1a\u4ece\u6839\u8282\u70b9\u5230\u6700\u8fdc\u53f6\u8282\u70b9\u6240\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\u3002
        • \u8282\u70b9\u7684\u6df1\u5ea6\uff08depth\uff09\uff1a\u4ece\u6839\u8282\u70b9\u5230\u8be5\u8282\u70b9\u6240\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\u3002
        • \u8282\u70b9\u7684\u9ad8\u5ea6\uff08height\uff09\uff1a\u4ece\u8ddd\u79bb\u8be5\u8282\u70b9\u6700\u8fdc\u7684\u53f6\u8282\u70b9\u5230\u8be5\u8282\u70b9\u6240\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\u3002

        \u56fe 7-2 \u00a0 \u4e8c\u53c9\u6811\u7684\u5e38\u7528\u672f\u8bed

        Tip

        \u8bf7\u6ce8\u610f\uff0c\u6211\u4eec\u901a\u5e38\u5c06\u201c\u9ad8\u5ea6\u201d\u548c\u201c\u6df1\u5ea6\u201d\u5b9a\u4e49\u4e3a\u201c\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\u201d\uff0c\u4f46\u6709\u4e9b\u9898\u76ee\u6216\u6559\u6750\u53ef\u80fd\u4f1a\u5c06\u5176\u5b9a\u4e49\u4e3a\u201c\u7ecf\u8fc7\u7684\u8282\u70b9\u7684\u6570\u91cf\u201d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u9ad8\u5ea6\u548c\u6df1\u5ea6\u90fd\u9700\u8981\u52a0 1 \u3002

        "},{"location":"chapter_tree/binary_tree/#712","title":"7.1.2 \u00a0 \u4e8c\u53c9\u6811\u57fa\u672c\u64cd\u4f5c","text":""},{"location":"chapter_tree/binary_tree/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u4e8c\u53c9\u6811","text":"

        \u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u9996\u5148\u521d\u59cb\u5316\u8282\u70b9\uff0c\u7136\u540e\u6784\u5efa\u5f15\u7528\uff08\u6307\u9488\uff09\u3002

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree.py
        # \u521d\u59cb\u5316\u4e8c\u53c9\u6811\n# \u521d\u59cb\u5316\u8282\u70b9\nn1 = TreeNode(val=1)\nn2 = TreeNode(val=2)\nn3 = TreeNode(val=3)\nn4 = TreeNode(val=4)\nn5 = TreeNode(val=5)\n# \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
        binary_tree.cpp
        /* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode* n1 = new TreeNode(1);\nTreeNode* n2 = new TreeNode(2);\nTreeNode* n3 = new TreeNode(3);\nTreeNode* n4 = new TreeNode(4);\nTreeNode* n5 = new TreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
        binary_tree.java
        // \u521d\u59cb\u5316\u8282\u70b9\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
        binary_tree.cs
        /* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode n1 = new(1);\nTreeNode n2 = new(2);\nTreeNode n3 = new(3);\nTreeNode n4 = new(4);\nTreeNode n5 = new(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
        binary_tree.go
        /* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nn1 := NewTreeNode(1)\nn2 := NewTreeNode(2)\nn3 := NewTreeNode(3)\nn4 := NewTreeNode(4)\nn5 := NewTreeNode(5)\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.Left = n2\nn1.Right = n3\nn2.Left = n4\nn2.Right = n5\n
        binary_tree.swift
        // \u521d\u59cb\u5316\u8282\u70b9\nlet n1 = TreeNode(x: 1)\nlet n2 = TreeNode(x: 2)\nlet n3 = TreeNode(x: 3)\nlet n4 = TreeNode(x: 4)\nlet n5 = TreeNode(x: 5)\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
        binary_tree.js
        /* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nlet n1 = new TreeNode(1),\n    n2 = new TreeNode(2),\n    n3 = new TreeNode(3),\n    n4 = new TreeNode(4),\n    n5 = new TreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
        binary_tree.ts
        /* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nlet n1 = new TreeNode(1),\n    n2 = new TreeNode(2),\n    n3 = new TreeNode(3),\n    n4 = new TreeNode(4),\n    n5 = new TreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
        binary_tree.dart
        /* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
        binary_tree.rs
        // \u521d\u59cb\u5316\u8282\u70b9\nlet n1 = TreeNode::new(1);\nlet n2 = TreeNode::new(2);\nlet n3 = TreeNode::new(3);\nlet n4 = TreeNode::new(4);\nlet n5 = TreeNode::new(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.borrow_mut().left = Some(n2.clone());\nn1.borrow_mut().right = Some(n3);\nn2.borrow_mut().left = Some(n4);\nn2.borrow_mut().right = Some(n5);\n
        binary_tree.c
        /* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode *n1 = newTreeNode(1);\nTreeNode *n2 = newTreeNode(2);\nTreeNode *n3 = newTreeNode(3);\nTreeNode *n4 = newTreeNode(4);\nTreeNode *n5 = newTreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
        binary_tree.kt
        // \u521d\u59cb\u5316\u8282\u70b9\nval n1 = TreeNode(1)\nval n2 = TreeNode(2)\nval n3 = TreeNode(3)\nval n4 = TreeNode(4)\nval n5 = TreeNode(5)\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
        binary_tree.rb
        # \u521d\u59cb\u5316\u4e8c\u53c9\u6811\n# \u521d\u59cb\u5316\u8282\u70b9\nn1 = TreeNode.new(1)\nn2 = TreeNode.new(2)\nn3 = TreeNode.new(3)\nn4 = TreeNode.new(4)\nn5 = TreeNode.new(5)\n# \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_tree/binary_tree/#2","title":"2. \u00a0 \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9","text":"

        \u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u5728\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u6307\u9488\u6765\u5b9e\u73b0\u3002\u56fe 7-3 \u7ed9\u51fa\u4e86\u4e00\u4e2a\u793a\u4f8b\u3002

        \u56fe 7-3 \u00a0 \u5728\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree.py
        # \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\np = TreeNode(0)\n# \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = p\np.left = n2\n# \u5220\u9664\u8282\u70b9 P\nn1.left = n2\n
        binary_tree.cpp
        /* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode* P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1->left = P;\nP->left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1->left = n2;\n// \u91ca\u653e\u5185\u5b58\ndelete P;\n
        binary_tree.java
        TreeNode P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
        binary_tree.cs
        /* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode P = new(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
        binary_tree.go
        /* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\np := NewTreeNode(0)\nn1.Left = p\np.Left = n2\n// \u5220\u9664\u8282\u70b9 P\nn1.Left = n2\n
        binary_tree.swift
        let P = TreeNode(x: 0)\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P\nP.left = n2\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2\n
        binary_tree.js
        /* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nlet P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
        binary_tree.ts
        /* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nconst P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
        binary_tree.dart
        /* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
        binary_tree.rs
        let p = TreeNode::new(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.borrow_mut().left = Some(p.clone());\np.borrow_mut().left = Some(n2.clone());\n// \u5220\u9664\u8282\u70b9 p\nn1.borrow_mut().left = Some(n2);\n
        binary_tree.c
        /* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode *P = newTreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1->left = P;\nP->left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1->left = n2;\n// \u91ca\u653e\u5185\u5b58\nfree(P);\n
        binary_tree.kt
        val P = TreeNode(0)\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P\nP.left = n2\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2\n
        binary_tree.rb
        # \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\n_p = TreeNode.new(0)\n# \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 _p\nn1.left = _p\n_p.left = n2\n# \u5220\u9664\u8282\u70b9\nn1.left = n2\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        Tip

        \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u63d2\u5165\u8282\u70b9\u53ef\u80fd\u4f1a\u6539\u53d8\u4e8c\u53c9\u6811\u7684\u539f\u6709\u903b\u8f91\u7ed3\u6784\uff0c\u800c\u5220\u9664\u8282\u70b9\u901a\u5e38\u610f\u5473\u7740\u5220\u9664\u8be5\u8282\u70b9\u53ca\u5176\u6240\u6709\u5b50\u6811\u3002\u56e0\u6b64\uff0c\u5728\u4e8c\u53c9\u6811\u4e2d\uff0c\u63d2\u5165\u4e0e\u5220\u9664\u901a\u5e38\u662f\u7531\u4e00\u5957\u64cd\u4f5c\u914d\u5408\u5b8c\u6210\u7684\uff0c\u4ee5\u5b9e\u73b0\u6709\u5b9e\u9645\u610f\u4e49\u7684\u64cd\u4f5c\u3002

        "},{"location":"chapter_tree/binary_tree/#713","title":"7.1.3 \u00a0 \u5e38\u89c1\u4e8c\u53c9\u6811\u7c7b\u578b","text":""},{"location":"chapter_tree/binary_tree/#1_1","title":"1. \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811","text":"

        \u5982\u56fe 7-4 \u6240\u793a\uff0c\u5b8c\u7f8e\u4e8c\u53c9\u6811\uff08perfect binary tree\uff09\u6240\u6709\u5c42\u7684\u8282\u70b9\u90fd\u88ab\u5b8c\u5168\u586b\u6ee1\u3002\u5728\u5b8c\u7f8e\u4e8c\u53c9\u6811\u4e2d\uff0c\u53f6\u8282\u70b9\u7684\u5ea6\u4e3a \\(0\\) \uff0c\u5176\u4f59\u6240\u6709\u8282\u70b9\u7684\u5ea6\u90fd\u4e3a \\(2\\) \uff1b\u82e5\u6811\u7684\u9ad8\u5ea6\u4e3a \\(h\\) \uff0c\u5219\u8282\u70b9\u603b\u6570\u4e3a \\(2^{h+1} - 1\\) \uff0c\u5448\u73b0\u6807\u51c6\u7684\u6307\u6570\u7ea7\u5173\u7cfb\uff0c\u53cd\u6620\u4e86\u81ea\u7136\u754c\u4e2d\u5e38\u89c1\u7684\u7ec6\u80de\u5206\u88c2\u73b0\u8c61\u3002

        Tip

        \u8bf7\u6ce8\u610f\uff0c\u5728\u4e2d\u6587\u793e\u533a\u4e2d\uff0c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u5e38\u88ab\u79f0\u4e3a\u6ee1\u4e8c\u53c9\u6811\u3002

        \u56fe 7-4 \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811

        "},{"location":"chapter_tree/binary_tree/#2_1","title":"2. \u00a0 \u5b8c\u5168\u4e8c\u53c9\u6811","text":"

        \u5982\u56fe 7-5 \u6240\u793a\uff0c\u5b8c\u5168\u4e8c\u53c9\u6811\uff08complete binary tree\uff09\u4ec5\u5141\u8bb8\u6700\u5e95\u5c42\u7684\u8282\u70b9\u4e0d\u5b8c\u5168\u586b\u6ee1\uff0c\u4e14\u6700\u5e95\u5c42\u7684\u8282\u70b9\u5fc5\u987b\u4ece\u5de6\u81f3\u53f3\u4f9d\u6b21\u8fde\u7eed\u586b\u5145\u3002\u8bf7\u6ce8\u610f\uff0c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u4e5f\u662f\u4e00\u68f5\u5b8c\u5168\u4e8c\u53c9\u6811\u3002

        \u56fe 7-5 \u00a0 \u5b8c\u5168\u4e8c\u53c9\u6811

        "},{"location":"chapter_tree/binary_tree/#3","title":"3. \u00a0 \u5b8c\u6ee1\u4e8c\u53c9\u6811","text":"

        \u5982\u56fe 7-6 \u6240\u793a\uff0c\u5b8c\u6ee1\u4e8c\u53c9\u6811\uff08full binary tree\uff09\u9664\u4e86\u53f6\u8282\u70b9\u4e4b\u5916\uff0c\u5176\u4f59\u6240\u6709\u8282\u70b9\u90fd\u6709\u4e24\u4e2a\u5b50\u8282\u70b9\u3002

        \u56fe 7-6 \u00a0 \u5b8c\u6ee1\u4e8c\u53c9\u6811

        "},{"location":"chapter_tree/binary_tree/#4","title":"4. \u00a0 \u5e73\u8861\u4e8c\u53c9\u6811","text":"

        \u5982\u56fe 7-7 \u6240\u793a\uff0c\u5e73\u8861\u4e8c\u53c9\u6811\uff08balanced binary tree\uff09\u4e2d\u4efb\u610f\u8282\u70b9\u7684\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u7684\u9ad8\u5ea6\u4e4b\u5dee\u7684\u7edd\u5bf9\u503c\u4e0d\u8d85\u8fc7 1 \u3002

        \u56fe 7-7 \u00a0 \u5e73\u8861\u4e8c\u53c9\u6811

        "},{"location":"chapter_tree/binary_tree/#714","title":"7.1.4 \u00a0 \u4e8c\u53c9\u6811\u7684\u9000\u5316","text":"

        \u56fe 7-8 \u5c55\u793a\u4e86\u4e8c\u53c9\u6811\u7684\u7406\u60f3\u7ed3\u6784\u4e0e\u9000\u5316\u7ed3\u6784\u3002\u5f53\u4e8c\u53c9\u6811\u7684\u6bcf\u5c42\u8282\u70b9\u90fd\u88ab\u586b\u6ee1\u65f6\uff0c\u8fbe\u5230\u201c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u201d\uff1b\u800c\u5f53\u6240\u6709\u8282\u70b9\u90fd\u504f\u5411\u4e00\u4fa7\u65f6\uff0c\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u201c\u94fe\u8868\u201d\u3002

        • \u5b8c\u7f8e\u4e8c\u53c9\u6811\u662f\u7406\u60f3\u60c5\u51b5\uff0c\u53ef\u4ee5\u5145\u5206\u53d1\u6325\u4e8c\u53c9\u6811\u201c\u5206\u6cbb\u201d\u7684\u4f18\u52bf\u3002
        • \u94fe\u8868\u5219\u662f\u53e6\u4e00\u4e2a\u6781\u7aef\uff0c\u5404\u9879\u64cd\u4f5c\u90fd\u53d8\u4e3a\u7ebf\u6027\u64cd\u4f5c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u9000\u5316\u81f3 \\(O(n)\\) \u3002

        \u56fe 7-8 \u00a0 \u4e8c\u53c9\u6811\u7684\u6700\u4f73\u7ed3\u6784\u4e0e\u6700\u5dee\u7ed3\u6784

        \u5982\u8868 7-1 \u6240\u793a\uff0c\u5728\u6700\u4f73\u7ed3\u6784\u548c\u6700\u5dee\u7ed3\u6784\u4e0b\uff0c\u4e8c\u53c9\u6811\u7684\u53f6\u8282\u70b9\u6570\u91cf\u3001\u8282\u70b9\u603b\u6570\u3001\u9ad8\u5ea6\u7b49\u8fbe\u5230\u6781\u5927\u503c\u6216\u6781\u5c0f\u503c\u3002

        \u8868 7-1 \u00a0 \u4e8c\u53c9\u6811\u7684\u6700\u4f73\u7ed3\u6784\u4e0e\u6700\u5dee\u7ed3\u6784

        \u5b8c\u7f8e\u4e8c\u53c9\u6811 \u94fe\u8868 \u7b2c \\(i\\) \u5c42\u7684\u8282\u70b9\u6570\u91cf \\(2^{i-1}\\) \\(1\\) \u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u6811\u7684\u53f6\u8282\u70b9\u6570\u91cf \\(2^h\\) \\(1\\) \u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u6811\u7684\u8282\u70b9\u603b\u6570 \\(2^{h+1} - 1\\) \\(h + 1\\) \u8282\u70b9\u603b\u6570\u4e3a \\(n\\) \u7684\u6811\u7684\u9ad8\u5ea6 \\(\\log_2 (n+1) - 1\\) \\(n - 1\\)"},{"location":"chapter_tree/binary_tree_traversal/","title":"7.2 \u00a0 \u4e8c\u53c9\u6811\u904d\u5386","text":"

        \u4ece\u7269\u7406\u7ed3\u6784\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u6811\u662f\u4e00\u79cd\u57fa\u4e8e\u94fe\u8868\u7684\u6570\u636e\u7ed3\u6784\uff0c\u56e0\u6b64\u5176\u904d\u5386\u65b9\u5f0f\u662f\u901a\u8fc7\u6307\u9488\u9010\u4e2a\u8bbf\u95ee\u8282\u70b9\u3002\u7136\u800c\uff0c\u6811\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u8fd9\u4f7f\u5f97\u904d\u5386\u6811\u6bd4\u904d\u5386\u94fe\u8868\u66f4\u52a0\u590d\u6742\uff0c\u9700\u8981\u501f\u52a9\u641c\u7d22\u7b97\u6cd5\u6765\u5b9e\u73b0\u3002

        \u4e8c\u53c9\u6811\u5e38\u89c1\u7684\u904d\u5386\u65b9\u5f0f\u5305\u62ec\u5c42\u5e8f\u904d\u5386\u3001\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u548c\u540e\u5e8f\u904d\u5386\u7b49\u3002

        "},{"location":"chapter_tree/binary_tree_traversal/#721","title":"7.2.1 \u00a0 \u5c42\u5e8f\u904d\u5386","text":"

        \u5982\u56fe 7-9 \u6240\u793a\uff0c\u5c42\u5e8f\u904d\u5386\uff08level-order traversal\uff09\u4ece\u9876\u90e8\u5230\u5e95\u90e8\u9010\u5c42\u904d\u5386\u4e8c\u53c9\u6811\uff0c\u5e76\u5728\u6bcf\u4e00\u5c42\u6309\u7167\u4ece\u5de6\u5230\u53f3\u7684\u987a\u5e8f\u8bbf\u95ee\u8282\u70b9\u3002

        \u5c42\u5e8f\u904d\u5386\u672c\u8d28\u4e0a\u5c5e\u4e8e\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\uff08breadth-first traversal\uff09\uff0c\u4e5f\u79f0\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff08breadth-first search, BFS\uff09\uff0c\u5b83\u4f53\u73b0\u4e86\u4e00\u79cd\u201c\u4e00\u5708\u4e00\u5708\u5411\u5916\u6269\u5c55\u201d\u7684\u9010\u5c42\u904d\u5386\u65b9\u5f0f\u3002

        \u56fe 7-9 \u00a0 \u4e8c\u53c9\u6811\u7684\u5c42\u5e8f\u904d\u5386

        "},{"location":"chapter_tree/binary_tree_traversal/#1","title":"1. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u901a\u5e38\u501f\u52a9\u201c\u961f\u5217\u201d\u6765\u5b9e\u73b0\u3002\u961f\u5217\u9075\u5faa\u201c\u5148\u8fdb\u5148\u51fa\u201d\u7684\u89c4\u5219\uff0c\u800c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u5219\u9075\u5faa\u201c\u9010\u5c42\u63a8\u8fdb\u201d\u7684\u89c4\u5219\uff0c\u4e24\u8005\u80cc\u540e\u7684\u601d\u60f3\u662f\u4e00\u81f4\u7684\u3002\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree_bfs.py
        def level_order(root: TreeNode | None) -> list[int]:\n    \"\"\"\u5c42\u5e8f\u904d\u5386\"\"\"\n    # \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n    queue: deque[TreeNode] = deque()\n    queue.append(root)\n    # \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n    res = []\n    while queue:\n        node: TreeNode = queue.popleft()  # \u961f\u5217\u51fa\u961f\n        res.append(node.val)  # \u4fdd\u5b58\u8282\u70b9\u503c\n        if node.left is not None:\n            queue.append(node.left)  # \u5de6\u5b50\u8282\u70b9\u5165\u961f\n        if node.right is not None:\n            queue.append(node.right)  # \u53f3\u5b50\u8282\u70b9\u5165\u961f\n    return res\n
        binary_tree_bfs.cpp
        /* \u5c42\u5e8f\u904d\u5386 */\nvector<int> levelOrder(TreeNode *root) {\n    // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n    queue<TreeNode *> queue;\n    queue.push(root);\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n    vector<int> vec;\n    while (!queue.empty()) {\n        TreeNode *node = queue.front();\n        queue.pop();              // \u961f\u5217\u51fa\u961f\n        vec.push_back(node->val); // \u4fdd\u5b58\u8282\u70b9\u503c\n        if (node->left != nullptr)\n            queue.push(node->left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n        if (node->right != nullptr)\n            queue.push(node->right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n    }\n    return vec;\n}\n
        binary_tree_bfs.java
        /* \u5c42\u5e8f\u904d\u5386 */\nList<Integer> levelOrder(TreeNode root) {\n    // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n    Queue<TreeNode> queue = new LinkedList<>();\n    queue.add(root);\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n    List<Integer> list = new ArrayList<>();\n    while (!queue.isEmpty()) {\n        TreeNode node = queue.poll(); // \u961f\u5217\u51fa\u961f\n        list.add(node.val);           // \u4fdd\u5b58\u8282\u70b9\u503c\n        if (node.left != null)\n            queue.offer(node.left);   // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n        if (node.right != null)\n            queue.offer(node.right);  // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n    }\n    return list;\n}\n
        binary_tree_bfs.cs
        /* \u5c42\u5e8f\u904d\u5386 */\nList<int> LevelOrder(TreeNode root) {\n    // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n    Queue<TreeNode> queue = new();\n    queue.Enqueue(root);\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n    List<int> list = [];\n    while (queue.Count != 0) {\n        TreeNode node = queue.Dequeue(); // \u961f\u5217\u51fa\u961f\n        list.Add(node.val!.Value);       // \u4fdd\u5b58\u8282\u70b9\u503c\n        if (node.left != null)\n            queue.Enqueue(node.left);    // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n        if (node.right != null)\n            queue.Enqueue(node.right);   // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n    }\n    return list;\n}\n
        binary_tree_bfs.go
        /* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder(root *TreeNode) []any {\n    // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n    queue := list.New()\n    queue.PushBack(root)\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5207\u7247\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n    nums := make([]any, 0)\n    for queue.Len() > 0 {\n        // \u961f\u5217\u51fa\u961f\n        node := queue.Remove(queue.Front()).(*TreeNode)\n        // \u4fdd\u5b58\u8282\u70b9\u503c\n        nums = append(nums, node.Val)\n        if node.Left != nil {\n            // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n            queue.PushBack(node.Left)\n        }\n        if node.Right != nil {\n            // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n            queue.PushBack(node.Right)\n        }\n    }\n    return nums\n}\n
        binary_tree_bfs.swift
        /* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder(root: TreeNode) -> [Int] {\n    // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n    var queue: [TreeNode] = [root]\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n    var list: [Int] = []\n    while !queue.isEmpty {\n        let node = queue.removeFirst() // \u961f\u5217\u51fa\u961f\n        list.append(node.val) // \u4fdd\u5b58\u8282\u70b9\u503c\n        if let left = node.left {\n            queue.append(left) // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n        }\n        if let right = node.right {\n            queue.append(right) // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n        }\n    }\n    return list\n}\n
        binary_tree_bfs.js
        /* \u5c42\u5e8f\u904d\u5386 */\nfunction levelOrder(root) {\n    // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n    const queue = [root];\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n    const list = [];\n    while (queue.length) {\n        let node = queue.shift(); // \u961f\u5217\u51fa\u961f\n        list.push(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n        if (node.left) queue.push(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n        if (node.right) queue.push(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n    }\n    return list;\n}\n
        binary_tree_bfs.ts
        /* \u5c42\u5e8f\u904d\u5386 */\nfunction levelOrder(root: TreeNode | null): number[] {\n    // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n    const queue = [root];\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n    const list: number[] = [];\n    while (queue.length) {\n        let node = queue.shift() as TreeNode; // \u961f\u5217\u51fa\u961f\n        list.push(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n        if (node.left) {\n            queue.push(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n        }\n        if (node.right) {\n            queue.push(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n        }\n    }\n    return list;\n}\n
        binary_tree_bfs.dart
        /* \u5c42\u5e8f\u904d\u5386 */\nList<int> levelOrder(TreeNode? root) {\n  // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n  Queue<TreeNode?> queue = Queue();\n  queue.add(root);\n  // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n  List<int> res = [];\n  while (queue.isNotEmpty) {\n    TreeNode? node = queue.removeFirst(); // \u961f\u5217\u51fa\u961f\n    res.add(node!.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n    if (node.left != null) queue.add(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n    if (node.right != null) queue.add(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n  }\n  return res;\n}\n
        binary_tree_bfs.rs
        /* \u5c42\u5e8f\u904d\u5386 */\nfn level_order(root: &Rc<RefCell<TreeNode>>) -> Vec<i32> {\n    // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n    let mut que = VecDeque::new();\n    que.push_back(root.clone());\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n    let mut vec = Vec::new();\n\n    while let Some(node) = que.pop_front() {\n        // \u961f\u5217\u51fa\u961f\n        vec.push(node.borrow().val); // \u4fdd\u5b58\u8282\u70b9\u503c\n        if let Some(left) = node.borrow().left.as_ref() {\n            que.push_back(left.clone()); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n        }\n        if let Some(right) = node.borrow().right.as_ref() {\n            que.push_back(right.clone()); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n        };\n    }\n    vec\n}\n
        binary_tree_bfs.c
        /* \u5c42\u5e8f\u904d\u5386 */\nint *levelOrder(TreeNode *root, int *size) {\n    /* \u8f85\u52a9\u961f\u5217 */\n    int front, rear;\n    int index, *arr;\n    TreeNode *node;\n    TreeNode **queue;\n\n    /* \u8f85\u52a9\u961f\u5217 */\n    queue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_SIZE);\n    // \u961f\u5217\u6307\u9488\n    front = 0, rear = 0;\n    // \u52a0\u5165\u6839\u8282\u70b9\n    queue[rear++] = root;\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n    /* \u8f85\u52a9\u6570\u7ec4 */\n    arr = (int *)malloc(sizeof(int) * MAX_SIZE);\n    // \u6570\u7ec4\u6307\u9488\n    index = 0;\n    while (front < rear) {\n        // \u961f\u5217\u51fa\u961f\n        node = queue[front++];\n        // \u4fdd\u5b58\u8282\u70b9\u503c\n        arr[index++] = node->val;\n        if (node->left != NULL) {\n            // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n            queue[rear++] = node->left;\n        }\n        if (node->right != NULL) {\n            // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n            queue[rear++] = node->right;\n        }\n    }\n    // \u66f4\u65b0\u6570\u7ec4\u957f\u5ea6\u7684\u503c\n    *size = index;\n    arr = realloc(arr, sizeof(int) * (*size));\n\n    // \u91ca\u653e\u8f85\u52a9\u6570\u7ec4\u7a7a\u95f4\n    free(queue);\n    return arr;\n}\n
        binary_tree_bfs.kt
        /* \u5c42\u5e8f\u904d\u5386 */\nfun levelOrder(root: TreeNode?): MutableList<Int> {\n    // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n    val queue = LinkedList<TreeNode?>()\n    queue.add(root)\n    // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n    val list = mutableListOf<Int>()\n    while (queue.isNotEmpty()) {\n        val node = queue.poll()      // \u961f\u5217\u51fa\u961f\n        list.add(node?._val!!)       // \u4fdd\u5b58\u8282\u70b9\u503c\n        if (node.left != null)\n            queue.offer(node.left)   // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n        if (node.right != null)\n            queue.offer(node.right)  // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n    }\n    return list\n}\n
        binary_tree_bfs.rb
        ### \u5c42\u5e8f\u904d\u5386 ###\ndef level_order(root)\n  # \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n  queue = [root]\n  # \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n  res = []\n  while !queue.empty?\n    node = queue.shift # \u961f\u5217\u51fa\u961f\n    res << node.val # \u4fdd\u5b58\u8282\u70b9\u503c\n    queue << node.left unless node.left.nil? # \u5de6\u5b50\u8282\u70b9\u5165\u961f\n    queue << node.right unless node.right.nil? # \u53f3\u5b50\u8282\u70b9\u5165\u961f\n  end\n  res\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        "},{"location":"chapter_tree/binary_tree_traversal/#2","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"
        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff1a\u6240\u6709\u8282\u70b9\u88ab\u8bbf\u95ee\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff0c\u5176\u4e2d \\(n\\) \u4e3a\u8282\u70b9\u6570\u91cf\u3002
        • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff1a\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u5373\u6ee1\u4e8c\u53c9\u6811\u65f6\uff0c\u904d\u5386\u5230\u6700\u5e95\u5c42\u4e4b\u524d\uff0c\u961f\u5217\u4e2d\u6700\u591a\u540c\u65f6\u5b58\u5728 \\((n + 1) / 2\\) \u4e2a\u8282\u70b9\uff0c\u5360\u7528 \\(O(n)\\) \u7a7a\u95f4\u3002
        "},{"location":"chapter_tree/binary_tree_traversal/#722","title":"7.2.2 \u00a0 \u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386","text":"

        \u76f8\u5e94\u5730\uff0c\u524d\u5e8f\u3001\u4e2d\u5e8f\u548c\u540e\u5e8f\u904d\u5386\u90fd\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u904d\u5386\uff08depth-first traversal\uff09\uff0c\u4e5f\u79f0\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff08depth-first search, DFS\uff09\uff0c\u5b83\u4f53\u73b0\u4e86\u4e00\u79cd\u201c\u5148\u8d70\u5230\u5c3d\u5934\uff0c\u518d\u56de\u6eaf\u7ee7\u7eed\u201d\u7684\u904d\u5386\u65b9\u5f0f\u3002

        \u56fe 7-10 \u5c55\u793a\u4e86\u5bf9\u4e8c\u53c9\u6811\u8fdb\u884c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u5de5\u4f5c\u539f\u7406\u3002\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u5c31\u50cf\u662f\u7ed5\u7740\u6574\u68f5\u4e8c\u53c9\u6811\u7684\u5916\u56f4\u201c\u8d70\u201d\u4e00\u5708\uff0c\u5728\u6bcf\u4e2a\u8282\u70b9\u90fd\u4f1a\u9047\u5230\u4e09\u4e2a\u4f4d\u7f6e\uff0c\u5206\u522b\u5bf9\u5e94\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u548c\u540e\u5e8f\u904d\u5386\u3002

        \u56fe 7-10 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386

        "},{"location":"chapter_tree/binary_tree_traversal/#1_1","title":"1. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

        \u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\uff1a

        PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree_dfs.py
        def pre_order(root: TreeNode | None):\n    \"\"\"\u524d\u5e8f\u904d\u5386\"\"\"\n    if root is None:\n        return\n    # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n    res.append(root.val)\n    pre_order(root=root.left)\n    pre_order(root=root.right)\n\ndef in_order(root: TreeNode | None):\n    \"\"\"\u4e2d\u5e8f\u904d\u5386\"\"\"\n    if root is None:\n        return\n    # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n    in_order(root=root.left)\n    res.append(root.val)\n    in_order(root=root.right)\n\ndef post_order(root: TreeNode | None):\n    \"\"\"\u540e\u5e8f\u904d\u5386\"\"\"\n    if root is None:\n        return\n    # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n    post_order(root=root.left)\n    post_order(root=root.right)\n    res.append(root.val)\n
        binary_tree_dfs.cpp
        /* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode *root) {\n    if (root == nullptr)\n        return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n    vec.push_back(root->val);\n    preOrder(root->left);\n    preOrder(root->right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode *root) {\n    if (root == nullptr)\n        return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n    inOrder(root->left);\n    vec.push_back(root->val);\n    inOrder(root->right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode *root) {\n    if (root == nullptr)\n        return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n    postOrder(root->left);\n    postOrder(root->right);\n    vec.push_back(root->val);\n}\n
        binary_tree_dfs.java
        /* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode root) {\n    if (root == null)\n        return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n    list.add(root.val);\n    preOrder(root.left);\n    preOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode root) {\n    if (root == null)\n        return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n    inOrder(root.left);\n    list.add(root.val);\n    inOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode root) {\n    if (root == null)\n        return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n    postOrder(root.left);\n    postOrder(root.right);\n    list.add(root.val);\n}\n
        binary_tree_dfs.cs
        /* \u524d\u5e8f\u904d\u5386 */\nvoid PreOrder(TreeNode? root) {\n    if (root == null) return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n    list.Add(root.val!.Value);\n    PreOrder(root.left);\n    PreOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid InOrder(TreeNode? root) {\n    if (root == null) return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n    InOrder(root.left);\n    list.Add(root.val!.Value);\n    InOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid PostOrder(TreeNode? root) {\n    if (root == null) return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n    PostOrder(root.left);\n    PostOrder(root.right);\n    list.Add(root.val!.Value);\n}\n
        binary_tree_dfs.go
        /* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder(node *TreeNode) {\n    if node == nil {\n        return\n    }\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n    nums = append(nums, node.Val)\n    preOrder(node.Left)\n    preOrder(node.Right)\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder(node *TreeNode) {\n    if node == nil {\n        return\n    }\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n    inOrder(node.Left)\n    nums = append(nums, node.Val)\n    inOrder(node.Right)\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder(node *TreeNode) {\n    if node == nil {\n        return\n    }\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n    postOrder(node.Left)\n    postOrder(node.Right)\n    nums = append(nums, node.Val)\n}\n
        binary_tree_dfs.swift
        /* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n    list.append(root.val)\n    preOrder(root: root.left)\n    preOrder(root: root.right)\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n    inOrder(root: root.left)\n    list.append(root.val)\n    inOrder(root: root.right)\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n    postOrder(root: root.left)\n    postOrder(root: root.right)\n    list.append(root.val)\n}\n
        binary_tree_dfs.js
        /* \u524d\u5e8f\u904d\u5386 */\nfunction preOrder(root) {\n    if (root === null) return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n    list.push(root.val);\n    preOrder(root.left);\n    preOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunction inOrder(root) {\n    if (root === null) return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n    inOrder(root.left);\n    list.push(root.val);\n    inOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunction postOrder(root) {\n    if (root === null) return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n    postOrder(root.left);\n    postOrder(root.right);\n    list.push(root.val);\n}\n
        binary_tree_dfs.ts
        /* \u524d\u5e8f\u904d\u5386 */\nfunction preOrder(root: TreeNode | null): void {\n    if (root === null) {\n        return;\n    }\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n    list.push(root.val);\n    preOrder(root.left);\n    preOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunction inOrder(root: TreeNode | null): void {\n    if (root === null) {\n        return;\n    }\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n    inOrder(root.left);\n    list.push(root.val);\n    inOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunction postOrder(root: TreeNode | null): void {\n    if (root === null) {\n        return;\n    }\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n    postOrder(root.left);\n    postOrder(root.right);\n    list.push(root.val);\n}\n
        binary_tree_dfs.dart
        /* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode? node) {\n  if (node == null) return;\n  // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n  list.add(node.val);\n  preOrder(node.left);\n  preOrder(node.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode? node) {\n  if (node == null) return;\n  // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n  inOrder(node.left);\n  list.add(node.val);\n  inOrder(node.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode? node) {\n  if (node == null) return;\n  // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n  postOrder(node.left);\n  postOrder(node.right);\n  list.add(node.val);\n}\n
        binary_tree_dfs.rs
        /* \u524d\u5e8f\u904d\u5386 */\nfn pre_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n    let mut result = vec![];\n\n    fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {\n        if let Some(node) = root {\n            // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n            let node = node.borrow();\n            res.push(node.val);\n            dfs(node.left.as_ref(), res);\n            dfs(node.right.as_ref(), res);\n        }\n    }\n    dfs(root, &mut result);\n\n    result\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfn in_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n    let mut result = vec![];\n\n    fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {\n        if let Some(node) = root {\n            // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n            let node = node.borrow();\n            dfs(node.left.as_ref(), res);\n            res.push(node.val);\n            dfs(node.right.as_ref(), res);\n        }\n    }\n    dfs(root, &mut result);\n\n    result\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfn post_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n    let mut result = vec![];\n\n    fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {\n        if let Some(node) = root {\n            // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n            let node = node.borrow();\n            dfs(node.left.as_ref(), res);\n            dfs(node.right.as_ref(), res);\n            res.push(node.val);\n        }\n    }\n\n    dfs(root, &mut result);\n\n    result\n}\n
        binary_tree_dfs.c
        /* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode *root, int *size) {\n    if (root == NULL)\n        return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n    arr[(*size)++] = root->val;\n    preOrder(root->left, size);\n    preOrder(root->right, size);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode *root, int *size) {\n    if (root == NULL)\n        return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n    inOrder(root->left, size);\n    arr[(*size)++] = root->val;\n    inOrder(root->right, size);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode *root, int *size) {\n    if (root == NULL)\n        return;\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n    postOrder(root->left, size);\n    postOrder(root->right, size);\n    arr[(*size)++] = root->val;\n}\n
        binary_tree_dfs.kt
        /* \u524d\u5e8f\u904d\u5386 */\nfun preOrder(root: TreeNode?) {\n    if (root == null) return\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n    list.add(root._val)\n    preOrder(root.left)\n    preOrder(root.right)\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfun inOrder(root: TreeNode?) {\n    if (root == null) return\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n    inOrder(root.left)\n    list.add(root._val)\n    inOrder(root.right)\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfun postOrder(root: TreeNode?) {\n    if (root == null) return\n    // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n    postOrder(root.left)\n    postOrder(root.right)\n    list.add(root._val)\n}\n
        binary_tree_dfs.rb
        ### \u524d\u5e8f\u904d\u5386 ###\ndef pre_order(root)\n  return if root.nil?\n\n  # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n  $res << root.val\n  pre_order(root.left)\n  pre_order(root.right)\nend\n\n### \u4e2d\u5e8f\u904d\u5386 ###\ndef in_order(root)\n  return if root.nil?\n\n  # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n  in_order(root.left)\n  $res << root.val\n  in_order(root.right)\nend\n\n### \u540e\u5e8f\u904d\u5386 ###\ndef post_order(root)\n  return if root.nil?\n\n  # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n  post_order(root.left)\n  post_order(root.right)\n  $res << root.val\nend\n
        \u53ef\u89c6\u5316\u8fd0\u884c

        \u5168\u5c4f\u89c2\u770b >

        Tip

        \u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u4e5f\u53ef\u4ee5\u57fa\u4e8e\u8fed\u4ee3\u5b9e\u73b0\uff0c\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u7814\u7a76\u3002

        \u56fe 7-11 \u5c55\u793a\u4e86\u524d\u5e8f\u904d\u5386\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b\uff0c\u5176\u53ef\u5206\u4e3a\u201c\u9012\u201d\u548c\u201c\u5f52\u201d\u4e24\u4e2a\u9006\u5411\u7684\u90e8\u5206\u3002

        1. \u201c\u9012\u201d\u8868\u793a\u5f00\u542f\u65b0\u65b9\u6cd5\uff0c\u7a0b\u5e8f\u5728\u6b64\u8fc7\u7a0b\u4e2d\u8bbf\u95ee\u4e0b\u4e00\u4e2a\u8282\u70b9\u3002
        2. \u201c\u5f52\u201d\u8868\u793a\u51fd\u6570\u8fd4\u56de\uff0c\u4ee3\u8868\u5f53\u524d\u8282\u70b9\u5df2\u7ecf\u8bbf\u95ee\u5b8c\u6bd5\u3002
        <1><2><3><4><5><6><7><8><9><10><11>

        \u56fe 7-11 \u00a0 \u524d\u5e8f\u904d\u5386\u7684\u9012\u5f52\u8fc7\u7a0b

        "},{"location":"chapter_tree/binary_tree_traversal/#2_1","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"
        • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff1a\u6240\u6709\u8282\u70b9\u88ab\u8bbf\u95ee\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002
        • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff1a\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u5373\u6811\u9000\u5316\u4e3a\u94fe\u8868\u65f6\uff0c\u9012\u5f52\u6df1\u5ea6\u8fbe\u5230 \\(n\\) \uff0c\u7cfb\u7edf\u5360\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002
        "},{"location":"chapter_tree/summary/","title":"7.6 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_tree/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
        • \u4e8c\u53c9\u6811\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u4f53\u73b0\u201c\u4e00\u5206\u4e3a\u4e8c\u201d\u7684\u5206\u6cbb\u903b\u8f91\u3002\u6bcf\u4e2a\u4e8c\u53c9\u6811\u8282\u70b9\u5305\u542b\u4e00\u4e2a\u503c\u4ee5\u53ca\u4e24\u4e2a\u6307\u9488\uff0c\u5206\u522b\u6307\u5411\u5176\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\u3002
        • \u5bf9\u4e8e\u4e8c\u53c9\u6811\u4e2d\u7684\u67d0\u4e2a\u8282\u70b9\uff0c\u5176\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\u53ca\u5176\u4ee5\u4e0b\u5f62\u6210\u7684\u6811\u88ab\u79f0\u4e3a\u8be5\u8282\u70b9\u7684\u5de6\uff08\u53f3\uff09\u5b50\u6811\u3002
        • \u4e8c\u53c9\u6811\u7684\u76f8\u5173\u672f\u8bed\u5305\u62ec\u6839\u8282\u70b9\u3001\u53f6\u8282\u70b9\u3001\u5c42\u3001\u5ea6\u3001\u8fb9\u3001\u9ad8\u5ea6\u548c\u6df1\u5ea6\u7b49\u3002
        • \u4e8c\u53c9\u6811\u7684\u521d\u59cb\u5316\u3001\u8282\u70b9\u63d2\u5165\u548c\u8282\u70b9\u5220\u9664\u64cd\u4f5c\u4e0e\u94fe\u8868\u64cd\u4f5c\u65b9\u6cd5\u7c7b\u4f3c\u3002
        • \u5e38\u89c1\u7684\u4e8c\u53c9\u6811\u7c7b\u578b\u6709\u5b8c\u7f8e\u4e8c\u53c9\u6811\u3001\u5b8c\u5168\u4e8c\u53c9\u6811\u3001\u5b8c\u6ee1\u4e8c\u53c9\u6811\u548c\u5e73\u8861\u4e8c\u53c9\u6811\u3002\u5b8c\u7f8e\u4e8c\u53c9\u6811\u662f\u6700\u7406\u60f3\u7684\u72b6\u6001\uff0c\u800c\u94fe\u8868\u662f\u9000\u5316\u540e\u7684\u6700\u5dee\u72b6\u6001\u3002
        • \u4e8c\u53c9\u6811\u53ef\u4ee5\u7528\u6570\u7ec4\u8868\u793a\uff0c\u65b9\u6cd5\u662f\u5c06\u8282\u70b9\u503c\u548c\u7a7a\u4f4d\u6309\u5c42\u5e8f\u904d\u5386\u987a\u5e8f\u6392\u5217\uff0c\u5e76\u6839\u636e\u7236\u8282\u70b9\u4e0e\u5b50\u8282\u70b9\u4e4b\u95f4\u7684\u7d22\u5f15\u6620\u5c04\u5173\u7cfb\u6765\u5b9e\u73b0\u6307\u9488\u3002
        • \u4e8c\u53c9\u6811\u7684\u5c42\u5e8f\u904d\u5386\u662f\u4e00\u79cd\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u65b9\u6cd5\uff0c\u5b83\u4f53\u73b0\u4e86\u201c\u4e00\u5708\u4e00\u5708\u5411\u5916\u6269\u5c55\u201d\u7684\u9010\u5c42\u904d\u5386\u65b9\u5f0f\uff0c\u901a\u5e38\u901a\u8fc7\u961f\u5217\u6765\u5b9e\u73b0\u3002
        • \u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386\u7686\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff0c\u5b83\u4eec\u4f53\u73b0\u4e86\u201c\u5148\u8d70\u5230\u5c3d\u5934\uff0c\u518d\u56de\u6eaf\u7ee7\u7eed\u201d\u7684\u904d\u5386\u65b9\u5f0f\uff0c\u901a\u5e38\u4f7f\u7528\u9012\u5f52\u6765\u5b9e\u73b0\u3002
        • \u4e8c\u53c9\u641c\u7d22\u6811\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u5143\u7d20\u67e5\u627e\u6570\u636e\u7ed3\u6784\uff0c\u5176\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(\\log n)\\) \u3002\u5f53\u4e8c\u53c9\u641c\u7d22\u6811\u9000\u5316\u4e3a\u94fe\u8868\u65f6\uff0c\u5404\u9879\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u52a3\u5316\u81f3 \\(O(n)\\) \u3002
        • AVL \u6811\uff0c\u4e5f\u79f0\u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u5b83\u901a\u8fc7\u65cb\u8f6c\u64cd\u4f5c\u786e\u4fdd\u5728\u4e0d\u65ad\u63d2\u5165\u548c\u5220\u9664\u8282\u70b9\u540e\u6811\u4ecd\u7136\u4fdd\u6301\u5e73\u8861\u3002
        • AVL \u6811\u7684\u65cb\u8f6c\u64cd\u4f5c\u5305\u62ec\u53f3\u65cb\u3001\u5de6\u65cb\u3001\u5148\u53f3\u65cb\u518d\u5de6\u65cb\u3001\u5148\u5de6\u65cb\u518d\u53f3\u65cb\u3002\u5728\u63d2\u5165\u6216\u5220\u9664\u8282\u70b9\u540e\uff0cAVL \u6811\u4f1a\u4ece\u5e95\u5411\u9876\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\u3002
        "},{"location":"chapter_tree/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

        Q\uff1a\u5bf9\u4e8e\u53ea\u6709\u4e00\u4e2a\u8282\u70b9\u7684\u4e8c\u53c9\u6811\uff0c\u6811\u7684\u9ad8\u5ea6\u548c\u6839\u8282\u70b9\u7684\u6df1\u5ea6\u90fd\u662f \\(0\\) \u5417\uff1f

        \u662f\u7684\uff0c\u56e0\u4e3a\u9ad8\u5ea6\u548c\u6df1\u5ea6\u901a\u5e38\u5b9a\u4e49\u4e3a\u201c\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\u201d\u3002

        Q\uff1a\u4e8c\u53c9\u6811\u4e2d\u7684\u63d2\u5165\u4e0e\u5220\u9664\u4e00\u822c\u7531\u4e00\u5957\u64cd\u4f5c\u914d\u5408\u5b8c\u6210\uff0c\u8fd9\u91cc\u7684\u201c\u4e00\u5957\u64cd\u4f5c\u201d\u6307\u4ec0\u4e48\u5462\uff1f\u53ef\u4ee5\u7406\u89e3\u4e3a\u8d44\u6e90\u7684\u5b50\u8282\u70b9\u7684\u8d44\u6e90\u91ca\u653e\u5417\uff1f

        \u62ff\u4e8c\u53c9\u641c\u7d22\u6811\u6765\u4e3e\u4f8b\uff0c\u5220\u9664\u8282\u70b9\u64cd\u4f5c\u8981\u5206\u4e09\u79cd\u60c5\u51b5\u5904\u7406\uff0c\u5176\u4e2d\u6bcf\u79cd\u60c5\u51b5\u90fd\u9700\u8981\u8fdb\u884c\u591a\u4e2a\u6b65\u9aa4\u7684\u8282\u70b9\u64cd\u4f5c\u3002

        Q\uff1a\u4e3a\u4ec0\u4e48 DFS \u904d\u5386\u4e8c\u53c9\u6811\u6709\u524d\u3001\u4e2d\u3001\u540e\u4e09\u79cd\u987a\u5e8f\uff0c\u5206\u522b\u6709\u4ec0\u4e48\u7528\u5462\uff1f

        \u4e0e\u987a\u5e8f\u548c\u9006\u5e8f\u904d\u5386\u6570\u7ec4\u7c7b\u4f3c\uff0c\u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386\u662f\u4e09\u79cd\u4e8c\u53c9\u6811\u904d\u5386\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u5f97\u5230\u4e00\u4e2a\u7279\u5b9a\u987a\u5e8f\u7684\u904d\u5386\u7ed3\u679c\u3002\u4f8b\u5982\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\uff0c\u7531\u4e8e\u8282\u70b9\u5927\u5c0f\u6ee1\u8db3 \u5de6\u5b50\u8282\u70b9\u503c < \u6839\u8282\u70b9\u503c < \u53f3\u5b50\u8282\u70b9\u503c \uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u8981\u6309\u7167\u201c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u201d\u7684\u4f18\u5148\u7ea7\u904d\u5386\u6811\uff0c\u5c31\u53ef\u4ee5\u83b7\u5f97\u6709\u5e8f\u7684\u8282\u70b9\u5e8f\u5217\u3002

        Q\uff1a\u53f3\u65cb\u64cd\u4f5c\u662f\u5904\u7406\u5931\u8861\u8282\u70b9 node\u3001child\u3001grand_child \u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u90a3 node \u7684\u7236\u8282\u70b9\u548c node \u539f\u6765\u7684\u8fde\u63a5\u4e0d\u9700\u8981\u7ef4\u62a4\u5417\uff1f\u53f3\u65cb\u64cd\u4f5c\u540e\u5c82\u4e0d\u662f\u65ad\u6389\u4e86\uff1f

        \u6211\u4eec\u9700\u8981\u4ece\u9012\u5f52\u7684\u89c6\u89d2\u6765\u770b\u8fd9\u4e2a\u95ee\u9898\u3002\u53f3\u65cb\u64cd\u4f5c right_rotate(root) \u4f20\u5165\u7684\u662f\u5b50\u6811\u7684\u6839\u8282\u70b9\uff0c\u6700\u7ec8 return child \u8fd4\u56de\u65cb\u8f6c\u4e4b\u540e\u7684\u5b50\u6811\u7684\u6839\u8282\u70b9\u3002\u5b50\u6811\u7684\u6839\u8282\u70b9\u548c\u5176\u7236\u8282\u70b9\u7684\u8fde\u63a5\u662f\u5728\u8be5\u51fd\u6570\u8fd4\u56de\u540e\u5b8c\u6210\u7684\uff0c\u4e0d\u5c5e\u4e8e\u53f3\u65cb\u64cd\u4f5c\u7684\u7ef4\u62a4\u8303\u56f4\u3002

        Q\uff1a\u5728 C++ \u4e2d\uff0c\u51fd\u6570\u88ab\u5212\u5206\u5230 private \u548c public \u4e2d\uff0c\u8fd9\u65b9\u9762\u6709\u4ec0\u4e48\u8003\u91cf\u5417\uff1f\u4e3a\u4ec0\u4e48\u8981\u5c06 height() \u51fd\u6570\u548c updateHeight() \u51fd\u6570\u5206\u522b\u653e\u5728 public \u548c private \u4e2d\u5462\uff1f

        \u4e3b\u8981\u770b\u65b9\u6cd5\u7684\u4f7f\u7528\u8303\u56f4\uff0c\u5982\u679c\u65b9\u6cd5\u53ea\u5728\u7c7b\u5185\u90e8\u4f7f\u7528\uff0c\u90a3\u4e48\u5c31\u8bbe\u8ba1\u4e3a private \u3002\u4f8b\u5982\uff0c\u7528\u6237\u5355\u72ec\u8c03\u7528 updateHeight() \u662f\u6ca1\u6709\u610f\u4e49\u7684\uff0c\u5b83\u53ea\u662f\u63d2\u5165\u3001\u5220\u9664\u64cd\u4f5c\u4e2d\u7684\u4e00\u6b65\u3002\u800c height() \u662f\u8bbf\u95ee\u8282\u70b9\u9ad8\u5ea6\uff0c\u7c7b\u4f3c\u4e8e vector.size() \uff0c\u56e0\u6b64\u8bbe\u7f6e\u6210 public \u4ee5\u4fbf\u4f7f\u7528\u3002

        Q\uff1a\u5982\u4f55\u4ece\u4e00\u7ec4\u8f93\u5165\u6570\u636e\u6784\u5efa\u4e00\u68f5\u4e8c\u53c9\u641c\u7d22\u6811\uff1f\u6839\u8282\u70b9\u7684\u9009\u62e9\u662f\u4e0d\u662f\u5f88\u91cd\u8981\uff1f

        \u662f\u7684\uff0c\u6784\u5efa\u6811\u7684\u65b9\u6cd5\u5df2\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4ee3\u7801\u4e2d\u7684 build_tree() \u65b9\u6cd5\u4e2d\u7ed9\u51fa\u3002\u81f3\u4e8e\u6839\u8282\u70b9\u7684\u9009\u62e9\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u8f93\u5165\u6570\u636e\u6392\u5e8f\uff0c\u7136\u540e\u5c06\u4e2d\u70b9\u5143\u7d20\u4f5c\u4e3a\u6839\u8282\u70b9\uff0c\u518d\u9012\u5f52\u5730\u6784\u5efa\u5de6\u53f3\u5b50\u6811\u3002\u8fd9\u6837\u505a\u53ef\u4ee5\u6700\u5927\u7a0b\u5ea6\u4fdd\u8bc1\u6811\u7684\u5e73\u8861\u6027\u3002

        Q\uff1a\u5728 Java \u4e2d\uff0c\u5b57\u7b26\u4e32\u5bf9\u6bd4\u662f\u5426\u4e00\u5b9a\u8981\u7528 equals() \u65b9\u6cd5\uff1f

        \u5728 Java \u4e2d\uff0c\u5bf9\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c== \u7528\u4e8e\u5bf9\u6bd4\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002\u5bf9\u4e8e\u5f15\u7528\u7c7b\u578b\uff0c\u4e24\u79cd\u7b26\u53f7\u7684\u5de5\u4f5c\u539f\u7406\u662f\u4e0d\u540c\u7684\u3002

        • == \uff1a\u7528\u6765\u6bd4\u8f83\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5373\u5b83\u4eec\u5728\u5185\u5b58\u4e2d\u7684\u4f4d\u7f6e\u662f\u5426\u76f8\u540c\u3002
        • equals()\uff1a\u7528\u6765\u5bf9\u6bd4\u4e24\u4e2a\u5bf9\u8c61\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002

        \u56e0\u6b64\uff0c\u5982\u679c\u8981\u5bf9\u6bd4\u503c\uff0c\u6211\u4eec\u5e94\u8be5\u4f7f\u7528 equals() \u3002\u7136\u800c\uff0c\u901a\u8fc7 String a = \"hi\"; String b = \"hi\"; \u521d\u59cb\u5316\u7684\u5b57\u7b26\u4e32\u90fd\u5b58\u50a8\u5728\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u4e2d\uff0c\u5b83\u4eec\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u56e0\u6b64\u4e5f\u53ef\u4ee5\u7528 a == b \u6765\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u3002

        Q\uff1a\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u5230\u6700\u5e95\u5c42\u4e4b\u524d\uff0c\u961f\u5217\u4e2d\u7684\u8282\u70b9\u6570\u91cf\u662f \\(2^h\\) \u5417\uff1f

        \u662f\u7684\uff0c\u4f8b\u5982\u9ad8\u5ea6 \\(h = 2\\) \u7684\u6ee1\u4e8c\u53c9\u6811\uff0c\u5176\u8282\u70b9\u603b\u6570 \\(n = 7\\) \uff0c\u5219\u5e95\u5c42\u8282\u70b9\u6570\u91cf \\(4 = 2^h = (n + 1) / 2\\) \u3002

        "}]} \ No newline at end of file diff --git a/stylesheets/extra.css b/stylesheets/extra.css index fca2ef236..1082805bd 100644 --- a/stylesheets/extra.css +++ b/stylesheets/extra.css @@ -567,4 +567,4 @@ a:hover .text-button span { left: 50%; transform: translate(-50%, -50%); } -/*! update cache: 20260104155727 */ +/*! update cache: 20260104161427 */ diff --git a/vercel.json b/vercel.json index f1f8da586..3f4a0ad61 100644 --- a/vercel.json +++ b/vercel.json @@ -1,4 +1,5 @@ { + "trailingSlash": true, "headers": [ { "source": "(/en|/zh-hant)?/assets/javascripts/bundle.(.+).min.js", diff --git a/zh-hant/assets/javascripts/bundle.79ae519e.min.js b/zh-hant/assets/javascripts/bundle.79ae519e.min.js index 1ce9e8660..023e5f357 100644 --- a/zh-hant/assets/javascripts/bundle.79ae519e.min.js +++ b/zh-hant/assets/javascripts/bundle.79ae519e.min.js @@ -14,4 +14,4 @@ `):"",this.name="UnsubscriptionError",this.errors=r}});function Ze(e,t){if(e){var r=e.indexOf(t);0<=r&&e.splice(r,1)}}var qe=(function(){function e(t){this.initialTeardown=t,this.closed=!1,this._parentage=null,this._finalizers=null}return e.prototype.unsubscribe=function(){var t,r,o,n,i;if(!this.closed){this.closed=!0;var s=this._parentage;if(s)if(this._parentage=null,Array.isArray(s))try{for(var a=Oe(s),c=a.next();!c.done;c=a.next()){var p=c.value;p.remove(this)}}catch(S){t={error:S}}finally{try{c&&!c.done&&(r=a.return)&&r.call(a)}finally{if(t)throw t.error}}else s.remove(this);var l=this.initialTeardown;if(I(l))try{l()}catch(S){i=S instanceof Jt?S.errors:[S]}var f=this._finalizers;if(f){this._finalizers=null;try{for(var u=Oe(f),d=u.next();!d.done;d=u.next()){var v=d.value;try{So(v)}catch(S){i=i!=null?i:[],S instanceof Jt?i=B(B([],K(i)),K(S.errors)):i.push(S)}}}catch(S){o={error:S}}finally{try{d&&!d.done&&(n=u.return)&&n.call(u)}finally{if(o)throw o.error}}}if(i)throw new Jt(i)}},e.prototype.add=function(t){var r;if(t&&t!==this)if(this.closed)So(t);else{if(t instanceof e){if(t.closed||t._hasParent(this))return;t._addParent(this)}(this._finalizers=(r=this._finalizers)!==null&&r!==void 0?r:[]).push(t)}},e.prototype._hasParent=function(t){var r=this._parentage;return r===t||Array.isArray(r)&&r.includes(t)},e.prototype._addParent=function(t){var r=this._parentage;this._parentage=Array.isArray(r)?(r.push(t),r):r?[r,t]:t},e.prototype._removeParent=function(t){var r=this._parentage;r===t?this._parentage=null:Array.isArray(r)&&Ze(r,t)},e.prototype.remove=function(t){var r=this._finalizers;r&&Ze(r,t),t instanceof e&&t._removeParent(this)},e.EMPTY=(function(){var t=new e;return t.closed=!0,t})(),e})();var $r=qe.EMPTY;function Xt(e){return e instanceof qe||e&&"closed"in e&&I(e.remove)&&I(e.add)&&I(e.unsubscribe)}function So(e){I(e)?e():e.unsubscribe()}var De={onUnhandledError:null,onStoppedNotification:null,Promise:void 0,useDeprecatedSynchronousErrorHandling:!1,useDeprecatedNextContext:!1};var xt={setTimeout:function(e,t){for(var r=[],o=2;o0},enumerable:!1,configurable:!0}),t.prototype._trySubscribe=function(r){return this._throwIfClosed(),e.prototype._trySubscribe.call(this,r)},t.prototype._subscribe=function(r){return this._throwIfClosed(),this._checkFinalizedStatuses(r),this._innerSubscribe(r)},t.prototype._innerSubscribe=function(r){var o=this,n=this,i=n.hasError,s=n.isStopped,a=n.observers;return i||s?$r:(this.currentObservers=null,a.push(r),new qe(function(){o.currentObservers=null,Ze(a,r)}))},t.prototype._checkFinalizedStatuses=function(r){var o=this,n=o.hasError,i=o.thrownError,s=o.isStopped;n?r.error(i):s&&r.complete()},t.prototype.asObservable=function(){var r=new F;return r.source=this,r},t.create=function(r,o){return new Ho(r,o)},t})(F);var Ho=(function(e){ie(t,e);function t(r,o){var n=e.call(this)||this;return n.destination=r,n.source=o,n}return t.prototype.next=function(r){var o,n;(n=(o=this.destination)===null||o===void 0?void 0:o.next)===null||n===void 0||n.call(o,r)},t.prototype.error=function(r){var o,n;(n=(o=this.destination)===null||o===void 0?void 0:o.error)===null||n===void 0||n.call(o,r)},t.prototype.complete=function(){var r,o;(o=(r=this.destination)===null||r===void 0?void 0:r.complete)===null||o===void 0||o.call(r)},t.prototype._subscribe=function(r){var o,n;return(n=(o=this.source)===null||o===void 0?void 0:o.subscribe(r))!==null&&n!==void 0?n:$r},t})(T);var jr=(function(e){ie(t,e);function t(r){var o=e.call(this)||this;return o._value=r,o}return Object.defineProperty(t.prototype,"value",{get:function(){return this.getValue()},enumerable:!1,configurable:!0}),t.prototype._subscribe=function(r){var o=e.prototype._subscribe.call(this,r);return!o.closed&&r.next(this._value),o},t.prototype.getValue=function(){var r=this,o=r.hasError,n=r.thrownError,i=r._value;if(o)throw n;return this._throwIfClosed(),i},t.prototype.next=function(r){e.prototype.next.call(this,this._value=r)},t})(T);var Rt={now:function(){return(Rt.delegate||Date).now()},delegate:void 0};var It=(function(e){ie(t,e);function t(r,o,n){r===void 0&&(r=1/0),o===void 0&&(o=1/0),n===void 0&&(n=Rt);var i=e.call(this)||this;return i._bufferSize=r,i._windowTime=o,i._timestampProvider=n,i._buffer=[],i._infiniteTimeWindow=!0,i._infiniteTimeWindow=o===1/0,i._bufferSize=Math.max(1,r),i._windowTime=Math.max(1,o),i}return t.prototype.next=function(r){var o=this,n=o.isStopped,i=o._buffer,s=o._infiniteTimeWindow,a=o._timestampProvider,c=o._windowTime;n||(i.push(r),!s&&i.push(a.now()+c)),this._trimBuffer(),e.prototype.next.call(this,r)},t.prototype._subscribe=function(r){this._throwIfClosed(),this._trimBuffer();for(var o=this._innerSubscribe(r),n=this,i=n._infiniteTimeWindow,s=n._buffer,a=s.slice(),c=0;c0?e.prototype.schedule.call(this,r,o):(this.delay=o,this.state=r,this.scheduler.flush(this),this)},t.prototype.execute=function(r,o){return o>0||this.closed?e.prototype.execute.call(this,r,o):this._execute(r,o)},t.prototype.requestAsyncId=function(r,o,n){return n===void 0&&(n=0),n!=null&&n>0||n==null&&this.delay>0?e.prototype.requestAsyncId.call(this,r,o,n):(r.flush(this),0)},t})(St);var Ro=(function(e){ie(t,e);function t(){return e!==null&&e.apply(this,arguments)||this}return t})(Ot);var Dr=new Ro(Po);var Io=(function(e){ie(t,e);function t(r,o){var n=e.call(this,r,o)||this;return n.scheduler=r,n.work=o,n}return t.prototype.requestAsyncId=function(r,o,n){return n===void 0&&(n=0),n!==null&&n>0?e.prototype.requestAsyncId.call(this,r,o,n):(r.actions.push(this),r._scheduled||(r._scheduled=Tt.requestAnimationFrame(function(){return r.flush(void 0)})))},t.prototype.recycleAsyncId=function(r,o,n){var i;if(n===void 0&&(n=0),n!=null?n>0:this.delay>0)return e.prototype.recycleAsyncId.call(this,r,o,n);var s=r.actions;o!=null&&o===r._scheduled&&((i=s[s.length-1])===null||i===void 0?void 0:i.id)!==o&&(Tt.cancelAnimationFrame(o),r._scheduled=void 0)},t})(St);var Fo=(function(e){ie(t,e);function t(){return e!==null&&e.apply(this,arguments)||this}return t.prototype.flush=function(r){this._active=!0;var o;r?o=r.id:(o=this._scheduled,this._scheduled=void 0);var n=this.actions,i;r=r||n.shift();do if(i=r.execute(r.state,r.delay))break;while((r=n[0])&&r.id===o&&n.shift());if(this._active=!1,i){for(;(r=n[0])&&r.id===o&&n.shift();)r.unsubscribe();throw i}},t})(Ot);var ye=new Fo(Io);var y=new F(function(e){return e.complete()});function tr(e){return e&&I(e.schedule)}function Vr(e){return e[e.length-1]}function pt(e){return I(Vr(e))?e.pop():void 0}function Fe(e){return tr(Vr(e))?e.pop():void 0}function rr(e,t){return typeof Vr(e)=="number"?e.pop():t}var Lt=(function(e){return e&&typeof e.length=="number"&&typeof e!="function"});function or(e){return I(e==null?void 0:e.then)}function nr(e){return I(e[wt])}function ir(e){return Symbol.asyncIterator&&I(e==null?void 0:e[Symbol.asyncIterator])}function ar(e){return new TypeError("You provided "+(e!==null&&typeof e=="object"?"an invalid object":"'"+e+"'")+" where a stream was expected. You can provide an Observable, Promise, ReadableStream, Array, AsyncIterable, or Iterable.")}function fa(){return typeof Symbol!="function"||!Symbol.iterator?"@@iterator":Symbol.iterator}var sr=fa();function cr(e){return I(e==null?void 0:e[sr])}function pr(e){return wo(this,arguments,function(){var r,o,n,i;return Gt(this,function(s){switch(s.label){case 0:r=e.getReader(),s.label=1;case 1:s.trys.push([1,,9,10]),s.label=2;case 2:return[4,dt(r.read())];case 3:return o=s.sent(),n=o.value,i=o.done,i?[4,dt(void 0)]:[3,5];case 4:return[2,s.sent()];case 5:return[4,dt(n)];case 6:return[4,s.sent()];case 7:return s.sent(),[3,2];case 8:return[3,10];case 9:return r.releaseLock(),[7];case 10:return[2]}})})}function lr(e){return I(e==null?void 0:e.getReader)}function U(e){if(e instanceof F)return e;if(e!=null){if(nr(e))return ua(e);if(Lt(e))return da(e);if(or(e))return ha(e);if(ir(e))return jo(e);if(cr(e))return ba(e);if(lr(e))return va(e)}throw ar(e)}function ua(e){return new F(function(t){var r=e[wt]();if(I(r.subscribe))return r.subscribe(t);throw new TypeError("Provided object does not correctly implement Symbol.observable")})}function da(e){return new F(function(t){for(var r=0;r=2;return function(o){return o.pipe(e?g(function(n,i){return e(n,i,o)}):be,Ee(1),r?Qe(t):tn(function(){return new fr}))}}function Yr(e){return e<=0?function(){return y}:E(function(t,r){var o=[];t.subscribe(w(r,function(n){o.push(n),e=2,!0))}function le(e){e===void 0&&(e={});var t=e.connector,r=t===void 0?function(){return new T}:t,o=e.resetOnError,n=o===void 0?!0:o,i=e.resetOnComplete,s=i===void 0?!0:i,a=e.resetOnRefCountZero,c=a===void 0?!0:a;return function(p){var l,f,u,d=0,v=!1,S=!1,X=function(){f==null||f.unsubscribe(),f=void 0},re=function(){X(),l=u=void 0,v=S=!1},ee=function(){var k=l;re(),k==null||k.unsubscribe()};return E(function(k,ut){d++,!S&&!v&&X();var je=u=u!=null?u:r();ut.add(function(){d--,d===0&&!S&&!v&&(f=Br(ee,c))}),je.subscribe(ut),!l&&d>0&&(l=new bt({next:function(R){return je.next(R)},error:function(R){S=!0,X(),f=Br(re,n,R),je.error(R)},complete:function(){v=!0,X(),f=Br(re,s),je.complete()}}),U(k).subscribe(l))})(p)}}function Br(e,t){for(var r=[],o=2;oe.next(document)),e}function M(e,t=document){return Array.from(t.querySelectorAll(e))}function j(e,t=document){let r=ue(e,t);if(typeof r=="undefined")throw new ReferenceError(`Missing element: expected "${e}" to be present`);return r}function ue(e,t=document){return t.querySelector(e)||void 0}function Ne(){var e,t,r,o;return(o=(r=(t=(e=document.activeElement)==null?void 0:e.shadowRoot)==null?void 0:t.activeElement)!=null?r:document.activeElement)!=null?o:void 0}var Ra=L(h(document.body,"focusin"),h(document.body,"focusout")).pipe(Ae(1),Q(void 0),m(()=>Ne()||document.body),Z(1));function Ye(e){return Ra.pipe(m(t=>e.contains(t)),Y())}function it(e,t){return H(()=>L(h(e,"mouseenter").pipe(m(()=>!0)),h(e,"mouseleave").pipe(m(()=>!1))).pipe(t?jt(r=>He(+!r*t)):be,Q(e.matches(":hover"))))}function sn(e,t){if(typeof t=="string"||typeof t=="number")e.innerHTML+=t.toString();else if(t instanceof Node)e.appendChild(t);else if(Array.isArray(t))for(let r of t)sn(e,r)}function x(e,t,...r){let o=document.createElement(e);if(t)for(let n of Object.keys(t))typeof t[n]!="undefined"&&(typeof t[n]!="boolean"?o.setAttribute(n,t[n]):o.setAttribute(n,""));for(let n of r)sn(o,n);return o}function br(e){if(e>999){let t=+((e-950)%1e3>99);return`${((e+1e-6)/1e3).toFixed(t)}k`}else return e.toString()}function _t(e){let t=x("script",{src:e});return H(()=>(document.head.appendChild(t),L(h(t,"load"),h(t,"error").pipe(b(()=>Nr(()=>new ReferenceError(`Invalid script: ${e}`))))).pipe(m(()=>{}),A(()=>document.head.removeChild(t)),Ee(1))))}var cn=new T,Ia=H(()=>typeof ResizeObserver=="undefined"?_t("https://unpkg.com/resize-observer-polyfill"):$(void 0)).pipe(m(()=>new ResizeObserver(e=>e.forEach(t=>cn.next(t)))),b(e=>L(tt,$(e)).pipe(A(()=>e.disconnect()))),Z(1));function de(e){return{width:e.offsetWidth,height:e.offsetHeight}}function Le(e){let t=e;for(;t.clientWidth===0&&t.parentElement;)t=t.parentElement;return Ia.pipe(O(r=>r.observe(t)),b(r=>cn.pipe(g(o=>o.target===t),A(()=>r.unobserve(t)))),m(()=>de(e)),Q(de(e)))}function At(e){return{width:e.scrollWidth,height:e.scrollHeight}}function vr(e){let t=e.parentElement;for(;t&&(e.scrollWidth<=t.scrollWidth&&e.scrollHeight<=t.scrollHeight);)t=(e=t).parentElement;return t?e:void 0}function pn(e){let t=[],r=e.parentElement;for(;r;)(e.clientWidth>r.clientWidth||e.clientHeight>r.clientHeight)&&t.push(r),r=(e=r).parentElement;return t.length===0&&t.push(document.documentElement),t}function Be(e){return{x:e.offsetLeft,y:e.offsetTop}}function ln(e){let t=e.getBoundingClientRect();return{x:t.x+window.scrollX,y:t.y+window.scrollY}}function mn(e){return L(h(window,"load"),h(window,"resize")).pipe($e(0,ye),m(()=>Be(e)),Q(Be(e)))}function gr(e){return{x:e.scrollLeft,y:e.scrollTop}}function Ge(e){return L(h(e,"scroll"),h(window,"scroll"),h(window,"resize")).pipe($e(0,ye),m(()=>gr(e)),Q(gr(e)))}var fn=new T,Fa=H(()=>$(new IntersectionObserver(e=>{for(let t of e)fn.next(t)},{threshold:0}))).pipe(b(e=>L(tt,$(e)).pipe(A(()=>e.disconnect()))),Z(1));function mt(e){return Fa.pipe(O(t=>t.observe(e)),b(t=>fn.pipe(g(({target:r})=>r===e),A(()=>t.unobserve(e)),m(({isIntersecting:r})=>r))))}function un(e,t=16){return Ge(e).pipe(m(({y:r})=>{let o=de(e),n=At(e);return r>=n.height-o.height-t}),Y())}var yr={drawer:j("[data-md-toggle=drawer]"),search:j("[data-md-toggle=search]")};function dn(e){return yr[e].checked}function at(e,t){yr[e].checked!==t&&yr[e].click()}function Je(e){let t=yr[e];return h(t,"change").pipe(m(()=>t.checked),Q(t.checked))}function ja(e,t){switch(e.constructor){case HTMLInputElement:return e.type==="radio"?/^Arrow/.test(t):!0;case HTMLSelectElement:case HTMLTextAreaElement:return!0;default:return e.isContentEditable}}function Ua(){return L(h(window,"compositionstart").pipe(m(()=>!0)),h(window,"compositionend").pipe(m(()=>!1))).pipe(Q(!1))}function hn(){let e=h(window,"keydown").pipe(g(t=>!(t.metaKey||t.ctrlKey)),m(t=>({mode:dn("search")?"search":"global",type:t.key,claim(){t.preventDefault(),t.stopPropagation()}})),g(({mode:t,type:r})=>{if(t==="global"){let o=Ne();if(typeof o!="undefined")return!ja(o,r)}return!0}),le());return Ua().pipe(b(t=>t?y:e))}function we(){return new URL(location.href)}function st(e,t=!1){if(V("navigation.instant")&&!t){let r=x("a",{href:e.href});document.body.appendChild(r),r.click(),r.remove()}else location.href=e.href}function bn(){return new T}function vn(){return location.hash.slice(1)}function gn(e){let t=x("a",{href:e});t.addEventListener("click",r=>r.stopPropagation()),t.click()}function Zr(e){return L(h(window,"hashchange"),e).pipe(m(vn),Q(vn()),g(t=>t.length>0),Z(1))}function yn(e){return Zr(e).pipe(m(t=>ue(`[id="${t}"]`)),g(t=>typeof t!="undefined"))}function Wt(e){let t=matchMedia(e);return ur(r=>t.addListener(()=>r(t.matches))).pipe(Q(t.matches))}function xn(){let e=matchMedia("print");return L(h(window,"beforeprint").pipe(m(()=>!0)),h(window,"afterprint").pipe(m(()=>!1))).pipe(Q(e.matches))}function eo(e,t){return e.pipe(b(r=>r?t():y))}function to(e,t){return new F(r=>{let o=new XMLHttpRequest;return o.open("GET",`${e}`),o.responseType="blob",o.addEventListener("load",()=>{o.status>=200&&o.status<300?(r.next(o.response),r.complete()):r.error(new Error(o.statusText))}),o.addEventListener("error",()=>{r.error(new Error("Network error"))}),o.addEventListener("abort",()=>{r.complete()}),typeof(t==null?void 0:t.progress$)!="undefined"&&(o.addEventListener("progress",n=>{var i;if(n.lengthComputable)t.progress$.next(n.loaded/n.total*100);else{let s=(i=o.getResponseHeader("Content-Length"))!=null?i:0;t.progress$.next(n.loaded/+s*100)}}),t.progress$.next(5)),o.send(),()=>o.abort()})}function ze(e,t){return to(e,t).pipe(b(r=>r.text()),m(r=>JSON.parse(r)),Z(1))}function xr(e,t){let r=new DOMParser;return to(e,t).pipe(b(o=>o.text()),m(o=>r.parseFromString(o,"text/html")),Z(1))}function En(e,t){let r=new DOMParser;return to(e,t).pipe(b(o=>o.text()),m(o=>r.parseFromString(o,"text/xml")),Z(1))}function wn(){return{x:Math.max(0,scrollX),y:Math.max(0,scrollY)}}function Tn(){return L(h(window,"scroll",{passive:!0}),h(window,"resize",{passive:!0})).pipe(m(wn),Q(wn()))}function Sn(){return{width:innerWidth,height:innerHeight}}function On(){return h(window,"resize",{passive:!0}).pipe(m(Sn),Q(Sn()))}function Ln(){return z([Tn(),On()]).pipe(m(([e,t])=>({offset:e,size:t})),Z(1))}function Er(e,{viewport$:t,header$:r}){let o=t.pipe(ne("size")),n=z([o,r]).pipe(m(()=>Be(e)));return z([r,t,n]).pipe(m(([{height:i},{offset:s,size:a},{x:c,y:p}])=>({offset:{x:s.x-c,y:s.y-p+i},size:a})))}function Wa(e){return h(e,"message",t=>t.data)}function Da(e){let t=new T;return t.subscribe(r=>e.postMessage(r)),t}function Mn(e,t=new Worker(e)){let r=Wa(t),o=Da(t),n=new T;n.subscribe(o);let i=o.pipe(oe(),ae(!0));return n.pipe(oe(),Ve(r.pipe(W(i))),le())}var Va=j("#__config"),Ct=JSON.parse(Va.textContent);Ct.base=`${new URL(Ct.base,we())}`;function Te(){return Ct}function V(e){return Ct.features.includes(e)}function Me(e,t){return typeof t!="undefined"?Ct.translations[e].replace("#",t.toString()):Ct.translations[e]}function Ce(e,t=document){return j(`[data-md-component=${e}]`,t)}function me(e,t=document){return M(`[data-md-component=${e}]`,t)}function Na(e){let t=j(".md-typeset > :first-child",e);return h(t,"click",{once:!0}).pipe(m(()=>j(".md-typeset",e)),m(r=>({hash:__md_hash(r.innerHTML)})))}function _n(e){if(!V("announce.dismiss")||!e.childElementCount)return y;if(!e.hidden){let t=j(".md-typeset",e);__md_hash(t.innerHTML)===__md_get("__announce")&&(e.hidden=!0)}return H(()=>{let t=new T;return t.subscribe(({hash:r})=>{e.hidden=!0,__md_set("__announce",r)}),Na(e).pipe(O(r=>t.next(r)),A(()=>t.complete()),m(r=>P({ref:e},r)))})}function za(e,{target$:t}){return t.pipe(m(r=>({hidden:r!==e})))}function An(e,t){let r=new T;return r.subscribe(({hidden:o})=>{e.hidden=o}),za(e,t).pipe(O(o=>r.next(o)),A(()=>r.complete()),m(o=>P({ref:e},o)))}function Dt(e,t){return t==="inline"?x("div",{class:"md-tooltip md-tooltip--inline",id:e,role:"tooltip"},x("div",{class:"md-tooltip__inner md-typeset"})):x("div",{class:"md-tooltip",id:e,role:"tooltip"},x("div",{class:"md-tooltip__inner md-typeset"}))}function wr(...e){return x("div",{class:"md-tooltip2",role:"dialog"},x("div",{class:"md-tooltip2__inner md-typeset"},e))}function Cn(...e){return x("div",{class:"md-tooltip2",role:"tooltip"},x("div",{class:"md-tooltip2__inner md-typeset"},e))}function kn(e,t){if(t=t?`${t}_annotation_${e}`:void 0,t){let r=t?`#${t}`:void 0;return x("aside",{class:"md-annotation",tabIndex:0},Dt(t),x("a",{href:r,class:"md-annotation__index",tabIndex:-1},x("span",{"data-md-annotation-id":e})))}else return x("aside",{class:"md-annotation",tabIndex:0},Dt(t),x("span",{class:"md-annotation__index",tabIndex:-1},x("span",{"data-md-annotation-id":e})))}function Hn(e){return x("button",{class:"md-code__button",title:Me("clipboard.copy"),"data-clipboard-target":`#${e} > code`,"data-md-type":"copy"})}function $n(){return x("button",{class:"md-code__button",title:"Toggle line selection","data-md-type":"select"})}function Pn(){return x("nav",{class:"md-code__nav"})}var In=$t(ro());function oo(e,t){let r=t&2,o=t&1,n=Object.keys(e.terms).filter(c=>!e.terms[c]).reduce((c,p)=>[...c,x("del",null,(0,In.default)(p))," "],[]).slice(0,-1),i=Te(),s=new URL(e.location,i.base);V("search.highlight")&&s.searchParams.set("h",Object.entries(e.terms).filter(([,c])=>c).reduce((c,[p])=>`${c} ${p}`.trim(),""));let{tags:a}=Te();return x("a",{href:`${s}`,class:"md-search-result__link",tabIndex:-1},x("article",{class:"md-search-result__article md-typeset","data-md-score":e.score.toFixed(2)},r>0&&x("div",{class:"md-search-result__icon md-icon"}),r>0&&x("h1",null,e.title),r<=0&&x("h2",null,e.title),o>0&&e.text.length>0&&e.text,e.tags&&x("nav",{class:"md-tags"},e.tags.map(c=>{let p=a?c in a?`md-tag-icon md-tag--${a[c]}`:"md-tag-icon":"";return x("span",{class:`md-tag ${p}`},c)})),o>0&&n.length>0&&x("p",{class:"md-search-result__terms"},Me("search.result.term.missing"),": ",...n)))}function Fn(e){let t=e[0].score,r=[...e],o=Te(),n=r.findIndex(l=>!`${new URL(l.location,o.base)}`.includes("#")),[i]=r.splice(n,1),s=r.findIndex(l=>l.scoreoo(l,1)),...c.length?[x("details",{class:"md-search-result__more"},x("summary",{tabIndex:-1},x("div",null,c.length>0&&c.length===1?Me("search.result.more.one"):Me("search.result.more.other",c.length))),...c.map(l=>oo(l,1)))]:[]];return x("li",{class:"md-search-result__item"},p)}function jn(e){return x("ul",{class:"md-source__facts"},Object.entries(e).map(([t,r])=>x("li",{class:`md-source__fact md-source__fact--${t}`},typeof r=="number"?br(r):r)))}function no(e){let t=`tabbed-control tabbed-control--${e}`;return x("div",{class:t,hidden:!0},x("button",{class:"tabbed-button",tabIndex:-1,"aria-hidden":"true"}))}function Un(e){return x("div",{class:"md-typeset__scrollwrap"},x("div",{class:"md-typeset__table"},e))}function Qa(e){var o;let t=Te(),r=new URL(`../${e.version}/`,t.base);return x("li",{class:"md-version__item"},x("a",{href:`${r}`,class:"md-version__link"},e.title,((o=t.version)==null?void 0:o.alias)&&e.aliases.length>0&&x("span",{class:"md-version__alias"},e.aliases[0])))}function Wn(e,t){var o;let r=Te();return e=e.filter(n=>{var i;return!((i=n.properties)!=null&&i.hidden)}),x("div",{class:"md-version"},x("button",{class:"md-version__current","aria-label":Me("select.version")},t.title,((o=r.version)==null?void 0:o.alias)&&t.aliases.length>0&&x("span",{class:"md-version__alias"},t.aliases[0])),x("ul",{class:"md-version__list"},e.map(Qa)))}var Ya=0;function Ba(e,t=250){let r=z([Ye(e),it(e,t)]).pipe(m(([n,i])=>n||i),Y()),o=H(()=>pn(e)).pipe(J(Ge),gt(1),Pe(r),m(()=>ln(e)));return r.pipe(Re(n=>n),b(()=>z([r,o])),m(([n,i])=>({active:n,offset:i})),le())}function Vt(e,t,r=250){let{content$:o,viewport$:n}=t,i=`__tooltip2_${Ya++}`;return H(()=>{let s=new T,a=new jr(!1);s.pipe(oe(),ae(!1)).subscribe(a);let c=a.pipe(jt(l=>He(+!l*250,Dr)),Y(),b(l=>l?o:y),O(l=>l.id=i),le());z([s.pipe(m(({active:l})=>l)),c.pipe(b(l=>it(l,250)),Q(!1))]).pipe(m(l=>l.some(f=>f))).subscribe(a);let p=a.pipe(g(l=>l),te(c,n),m(([l,f,{size:u}])=>{let d=e.getBoundingClientRect(),v=d.width/2;if(f.role==="tooltip")return{x:v,y:8+d.height};if(d.y>=u.height/2){let{height:S}=de(f);return{x:v,y:-16-S}}else return{x:v,y:16+d.height}}));return z([c,s,p]).subscribe(([l,{offset:f},u])=>{l.style.setProperty("--md-tooltip-host-x",`${f.x}px`),l.style.setProperty("--md-tooltip-host-y",`${f.y}px`),l.style.setProperty("--md-tooltip-x",`${u.x}px`),l.style.setProperty("--md-tooltip-y",`${u.y}px`),l.classList.toggle("md-tooltip2--top",u.y<0),l.classList.toggle("md-tooltip2--bottom",u.y>=0)}),a.pipe(g(l=>l),te(c,(l,f)=>f),g(l=>l.role==="tooltip")).subscribe(l=>{let f=de(j(":scope > *",l));l.style.setProperty("--md-tooltip-width",`${f.width}px`),l.style.setProperty("--md-tooltip-tail","0px")}),a.pipe(Y(),xe(ye),te(c)).subscribe(([l,f])=>{f.classList.toggle("md-tooltip2--active",l)}),z([a.pipe(g(l=>l)),c]).subscribe(([l,f])=>{f.role==="dialog"?(e.setAttribute("aria-controls",i),e.setAttribute("aria-haspopup","dialog")):e.setAttribute("aria-describedby",i)}),a.pipe(g(l=>!l)).subscribe(()=>{e.removeAttribute("aria-controls"),e.removeAttribute("aria-describedby"),e.removeAttribute("aria-haspopup")}),Ba(e,r).pipe(O(l=>s.next(l)),A(()=>s.complete()),m(l=>P({ref:e},l)))})}function Xe(e,{viewport$:t},r=document.body){return Vt(e,{content$:new F(o=>{let n=e.title,i=Cn(n);return o.next(i),e.removeAttribute("title"),r.append(i),()=>{i.remove(),e.setAttribute("title",n)}}),viewport$:t},0)}function Ga(e,t){let r=H(()=>z([mn(e),Ge(t)])).pipe(m(([{x:o,y:n},i])=>{let{width:s,height:a}=de(e);return{x:o-i.x+s/2,y:n-i.y+a/2}}));return Ye(e).pipe(b(o=>r.pipe(m(n=>({active:o,offset:n})),Ee(+!o||1/0))))}function Dn(e,t,{target$:r}){let[o,n]=Array.from(e.children);return H(()=>{let i=new T,s=i.pipe(oe(),ae(!0));return i.subscribe({next({offset:a}){e.style.setProperty("--md-tooltip-x",`${a.x}px`),e.style.setProperty("--md-tooltip-y",`${a.y}px`)},complete(){e.style.removeProperty("--md-tooltip-x"),e.style.removeProperty("--md-tooltip-y")}}),mt(e).pipe(W(s)).subscribe(a=>{e.toggleAttribute("data-md-visible",a)}),L(i.pipe(g(({active:a})=>a)),i.pipe(Ae(250),g(({active:a})=>!a))).subscribe({next({active:a}){a?e.prepend(o):o.remove()},complete(){e.prepend(o)}}),i.pipe($e(16,ye)).subscribe(({active:a})=>{o.classList.toggle("md-tooltip--active",a)}),i.pipe(gt(125,ye),g(()=>!!e.offsetParent),m(()=>e.offsetParent.getBoundingClientRect()),m(({x:a})=>a)).subscribe({next(a){a?e.style.setProperty("--md-tooltip-0",`${-a}px`):e.style.removeProperty("--md-tooltip-0")},complete(){e.style.removeProperty("--md-tooltip-0")}}),h(n,"click").pipe(W(s),g(a=>!(a.metaKey||a.ctrlKey))).subscribe(a=>{a.stopPropagation(),a.preventDefault()}),h(n,"mousedown").pipe(W(s),te(i)).subscribe(([a,{active:c}])=>{var p;if(a.button!==0||a.metaKey||a.ctrlKey)a.preventDefault();else if(c){a.preventDefault();let l=e.parentElement.closest(".md-annotation");l instanceof HTMLElement?l.focus():(p=Ne())==null||p.blur()}}),r.pipe(W(s),g(a=>a===o),nt(125)).subscribe(()=>e.focus()),Ga(e,t).pipe(O(a=>i.next(a)),A(()=>i.complete()),m(a=>P({ref:e},a)))})}function Ja(e){let t=Te();if(e.tagName!=="CODE")return[e];let r=[".c",".c1",".cm"];if(t.annotate&&typeof t.annotate=="object"){let o=e.closest("[class|=language]");if(o)for(let n of Array.from(o.classList)){if(!n.startsWith("language-"))continue;let[,i]=n.split("-");i in t.annotate&&r.push(...t.annotate[i])}}return M(r.join(", "),e)}function Xa(e){let t=[];for(let r of Ja(e)){let o=[],n=document.createNodeIterator(r,NodeFilter.SHOW_TEXT);for(let i=n.nextNode();i;i=n.nextNode())o.push(i);for(let i of o){let s;for(;s=/(\(\d+\))(!)?/.exec(i.textContent);){let[,a,c]=s;if(typeof c=="undefined"){let p=i.splitText(s.index);i=p.splitText(a.length),t.push(p)}else{i.textContent=a,t.push(i);break}}}}return t}function Vn(e,t){t.append(...Array.from(e.childNodes))}function Tr(e,t,{target$:r,print$:o}){let n=t.closest("[id]"),i=n==null?void 0:n.id,s=new Map;for(let a of Xa(t)){let[,c]=a.textContent.match(/\((\d+)\)/);ue(`:scope > li:nth-child(${c})`,e)&&(s.set(c,kn(c,i)),a.replaceWith(s.get(c)))}return s.size===0?y:H(()=>{let a=new T,c=a.pipe(oe(),ae(!0)),p=[];for(let[l,f]of s)p.push([j(".md-typeset",f),j(`:scope > li:nth-child(${l})`,e)]);return o.pipe(W(c)).subscribe(l=>{e.hidden=!l,e.classList.toggle("md-annotation-list",l);for(let[f,u]of p)l?Vn(f,u):Vn(u,f)}),L(...[...s].map(([,l])=>Dn(l,t,{target$:r}))).pipe(A(()=>a.complete()),le())})}function Nn(e){if(e.nextElementSibling){let t=e.nextElementSibling;if(t.tagName==="OL")return t;if(t.tagName==="P"&&!t.children.length)return Nn(t)}}function zn(e,t){return H(()=>{let r=Nn(e);return typeof r!="undefined"?Tr(r,e,t):y})}var Kn=$t(ao());var Za=0,qn=L(h(window,"keydown").pipe(m(()=>!0)),L(h(window,"keyup"),h(window,"contextmenu")).pipe(m(()=>!1))).pipe(Q(!1),Z(1));function Qn(e){if(e.nextElementSibling){let t=e.nextElementSibling;if(t.tagName==="OL")return t;if(t.tagName==="P"&&!t.children.length)return Qn(t)}}function es(e){return Le(e).pipe(m(({width:t})=>({scrollable:At(e).width>t})),ne("scrollable"))}function Yn(e,t){let{matches:r}=matchMedia("(hover)"),o=H(()=>{let n=new T,i=n.pipe(Yr(1));n.subscribe(({scrollable:d})=>{d&&r?e.setAttribute("tabindex","0"):e.removeAttribute("tabindex")});let s=[],a=e.closest("pre"),c=a.closest("[id]"),p=c?c.id:Za++;a.id=`__code_${p}`;let l=[],f=e.closest(".highlight");if(f instanceof HTMLElement){let d=Qn(f);if(typeof d!="undefined"&&(f.classList.contains("annotate")||V("content.code.annotate"))){let v=Tr(d,e,t);l.push(Le(f).pipe(W(i),m(({width:S,height:X})=>S&&X),Y(),b(S=>S?v:y)))}}let u=M(":scope > span[id]",e);if(u.length&&(e.classList.add("md-code__content"),e.closest(".select")||V("content.code.select")&&!e.closest(".no-select"))){let d=+u[0].id.split("-").pop(),v=$n();s.push(v),V("content.tooltips")&&l.push(Xe(v,{viewport$}));let S=h(v,"click").pipe(Ut(R=>!R,!1),O(()=>v.blur()),le());S.subscribe(R=>{v.classList.toggle("md-code__button--active",R)});let X=fe(u).pipe(J(R=>it(R).pipe(m(se=>[R,se]))));S.pipe(b(R=>R?X:y)).subscribe(([R,se])=>{let ce=ue(".hll.select",R);if(ce&&!se)ce.replaceWith(...Array.from(ce.childNodes));else if(!ce&&se){let he=document.createElement("span");he.className="hll select",he.append(...Array.from(R.childNodes).slice(1)),R.append(he)}});let re=fe(u).pipe(J(R=>h(R,"mousedown").pipe(O(se=>se.preventDefault()),m(()=>R)))),ee=S.pipe(b(R=>R?re:y),te(qn),m(([R,se])=>{var he;let ce=u.indexOf(R)+d;if(se===!1)return[ce,ce];{let Se=M(".hll",e).map(Ue=>u.indexOf(Ue.parentElement)+d);return(he=window.getSelection())==null||he.removeAllRanges(),[Math.min(ce,...Se),Math.max(ce,...Se)]}})),k=Zr(y).pipe(g(R=>R.startsWith(`__codelineno-${p}-`)));k.subscribe(R=>{let[,,se]=R.split("-"),ce=se.split(":").map(Se=>+Se-d+1);ce.length===1&&ce.push(ce[0]);for(let Se of M(".hll:not(.select)",e))Se.replaceWith(...Array.from(Se.childNodes));let he=u.slice(ce[0]-1,ce[1]);for(let Se of he){let Ue=document.createElement("span");Ue.className="hll",Ue.append(...Array.from(Se.childNodes).slice(1)),Se.append(Ue)}}),k.pipe(Ee(1),xe(pe)).subscribe(R=>{if(R.includes(":")){let se=document.getElementById(R.split(":")[0]);se&&setTimeout(()=>{let ce=se,he=-64;for(;ce!==document.body;)he+=ce.offsetTop,ce=ce.offsetParent;window.scrollTo({top:he})},1)}});let je=fe(M('a[href^="#__codelineno"]',f)).pipe(J(R=>h(R,"click").pipe(O(se=>se.preventDefault()),m(()=>R)))).pipe(W(i),te(qn),m(([R,se])=>{let he=+j(`[id="${R.hash.slice(1)}"]`).parentElement.id.split("-").pop();if(se===!1)return[he,he];{let Se=M(".hll",e).map(Ue=>+Ue.parentElement.id.split("-").pop());return[Math.min(he,...Se),Math.max(he,...Se)]}}));L(ee,je).subscribe(R=>{let se=`#__codelineno-${p}-`;R[0]===R[1]?se+=R[0]:se+=`${R[0]}:${R[1]}`,history.replaceState({},"",se),window.dispatchEvent(new HashChangeEvent("hashchange",{newURL:window.location.origin+window.location.pathname+se,oldURL:window.location.href}))})}if(Kn.default.isSupported()&&(e.closest(".copy")||V("content.code.copy")&&!e.closest(".no-copy"))){let d=Hn(a.id);s.push(d),V("content.tooltips")&&l.push(Xe(d,{viewport$}))}if(s.length){let d=Pn();d.append(...s),a.insertBefore(d,e)}return es(e).pipe(O(d=>n.next(d)),A(()=>n.complete()),m(d=>P({ref:e},d)),Ve(L(...l).pipe(W(i))))});return V("content.lazy")?mt(e).pipe(g(n=>n),Ee(1),b(()=>o)):o}function ts(e,{target$:t,print$:r}){let o=!0;return L(t.pipe(m(n=>n.closest("details:not([open])")),g(n=>e===n),m(()=>({action:"open",reveal:!0}))),r.pipe(g(n=>n||!o),O(()=>o=e.open),m(n=>({action:n?"open":"close"}))))}function Bn(e,t){return H(()=>{let r=new T;return r.subscribe(({action:o,reveal:n})=>{e.toggleAttribute("open",o==="open"),n&&e.scrollIntoView()}),ts(e,t).pipe(O(o=>r.next(o)),A(()=>r.complete()),m(o=>P({ref:e},o)))})}var Gn=0;function rs(e){let t=document.createElement("h3");t.innerHTML=e.innerHTML;let r=[t],o=e.nextElementSibling;for(;o&&!(o instanceof HTMLHeadingElement);)r.push(o),o=o.nextElementSibling;return r}function os(e,t){for(let r of M("[href], [src]",e))for(let o of["href","src"]){let n=r.getAttribute(o);if(n&&!/^(?:[a-z]+:)?\/\//i.test(n)){r[o]=new URL(r.getAttribute(o),t).toString();break}}for(let r of M("[name^=__], [for]",e))for(let o of["id","for","name"]){let n=r.getAttribute(o);n&&r.setAttribute(o,`${n}$preview_${Gn}`)}return Gn++,$(e)}function Jn(e,t){let{sitemap$:r}=t;if(!(e instanceof HTMLAnchorElement))return y;if(!(V("navigation.instant.preview")||e.hasAttribute("data-preview")))return y;e.removeAttribute("title");let o=z([Ye(e),it(e)]).pipe(m(([i,s])=>i||s),Y(),g(i=>i));return rt([r,o]).pipe(b(([i])=>{let s=new URL(e.href);return s.search=s.hash="",i.has(`${s}`)?$(s):y}),b(i=>xr(i).pipe(b(s=>os(s,i)))),b(i=>{let s=e.hash?`article [id="${e.hash.slice(1)}"]`:"article h1",a=ue(s,i);return typeof a=="undefined"?y:$(rs(a))})).pipe(b(i=>{let s=new F(a=>{let c=wr(...i);return a.next(c),document.body.append(c),()=>c.remove()});return Vt(e,P({content$:s},t))}))}var Xn=".node circle,.node ellipse,.node path,.node polygon,.node rect{fill:var(--md-mermaid-node-bg-color);stroke:var(--md-mermaid-node-fg-color)}marker{fill:var(--md-mermaid-edge-color)!important}.edgeLabel .label rect{fill:#0000}.flowchartTitleText{fill:var(--md-mermaid-label-fg-color)}.label{color:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}.label foreignObject{line-height:normal;overflow:visible}.label div .edgeLabel{color:var(--md-mermaid-label-fg-color)}.edgeLabel,.edgeLabel p,.label div .edgeLabel{background-color:var(--md-mermaid-label-bg-color)}.edgeLabel,.edgeLabel p{fill:var(--md-mermaid-label-bg-color);color:var(--md-mermaid-edge-color)}.edgePath .path,.flowchart-link{stroke:var(--md-mermaid-edge-color)}.edgePath .arrowheadPath{fill:var(--md-mermaid-edge-color);stroke:none}.cluster rect{fill:var(--md-default-fg-color--lightest);stroke:var(--md-default-fg-color--lighter)}.cluster span{color:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}g #flowchart-circleEnd,g #flowchart-circleStart,g #flowchart-crossEnd,g #flowchart-crossStart,g #flowchart-pointEnd,g #flowchart-pointStart{stroke:none}.classDiagramTitleText{fill:var(--md-mermaid-label-fg-color)}g.classGroup line,g.classGroup rect{fill:var(--md-mermaid-node-bg-color);stroke:var(--md-mermaid-node-fg-color)}g.classGroup text{fill:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}.classLabel .box{fill:var(--md-mermaid-label-bg-color);background-color:var(--md-mermaid-label-bg-color);opacity:1}.classLabel .label{fill:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}.node .divider{stroke:var(--md-mermaid-node-fg-color)}.relation{stroke:var(--md-mermaid-edge-color)}.cardinality{fill:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}.cardinality text{fill:inherit!important}defs marker.marker.composition.class path,defs marker.marker.dependency.class path,defs marker.marker.extension.class path{fill:var(--md-mermaid-edge-color)!important;stroke:var(--md-mermaid-edge-color)!important}defs marker.marker.aggregation.class path{fill:var(--md-mermaid-label-bg-color)!important;stroke:var(--md-mermaid-edge-color)!important}.statediagramTitleText{fill:var(--md-mermaid-label-fg-color)}g.stateGroup rect{fill:var(--md-mermaid-node-bg-color);stroke:var(--md-mermaid-node-fg-color)}g.stateGroup .state-title{fill:var(--md-mermaid-label-fg-color)!important;font-family:var(--md-mermaid-font-family)}g.stateGroup .composit{fill:var(--md-mermaid-label-bg-color)}.nodeLabel,.nodeLabel p{color:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}a .nodeLabel{text-decoration:underline}.node circle.state-end,.node circle.state-start,.start-state{fill:var(--md-mermaid-edge-color);stroke:none}.end-state-inner,.end-state-outer{fill:var(--md-mermaid-edge-color)}.end-state-inner,.node circle.state-end{stroke:var(--md-mermaid-label-bg-color)}.transition{stroke:var(--md-mermaid-edge-color)}[id^=state-fork] rect,[id^=state-join] rect{fill:var(--md-mermaid-edge-color)!important;stroke:none!important}.statediagram-cluster.statediagram-cluster .inner{fill:var(--md-default-bg-color)}.statediagram-cluster rect{fill:var(--md-mermaid-node-bg-color);stroke:var(--md-mermaid-node-fg-color)}.statediagram-state rect.divider{fill:var(--md-default-fg-color--lightest);stroke:var(--md-default-fg-color--lighter)}defs #statediagram-barbEnd{stroke:var(--md-mermaid-edge-color)}[id^=entity] path,[id^=entity] rect{fill:var(--md-default-bg-color)}.relationshipLine{stroke:var(--md-mermaid-edge-color)}defs .marker.oneOrMore.er *,defs .marker.onlyOne.er *,defs .marker.zeroOrMore.er *,defs .marker.zeroOrOne.er *{stroke:var(--md-mermaid-edge-color)!important}text:not([class]):last-child{fill:var(--md-mermaid-label-fg-color)}.actor{fill:var(--md-mermaid-sequence-actor-bg-color);stroke:var(--md-mermaid-sequence-actor-border-color)}text.actor>tspan{fill:var(--md-mermaid-sequence-actor-fg-color);font-family:var(--md-mermaid-font-family)}line{stroke:var(--md-mermaid-sequence-actor-line-color)}.actor-man circle,.actor-man line{fill:var(--md-mermaid-sequence-actorman-bg-color);stroke:var(--md-mermaid-sequence-actorman-line-color)}.messageLine0,.messageLine1{stroke:var(--md-mermaid-sequence-message-line-color)}.note{fill:var(--md-mermaid-sequence-note-bg-color);stroke:var(--md-mermaid-sequence-note-border-color)}.loopText,.loopText>tspan,.messageText,.noteText>tspan{stroke:none;font-family:var(--md-mermaid-font-family)!important}.messageText{fill:var(--md-mermaid-sequence-message-fg-color)}.loopText,.loopText>tspan{fill:var(--md-mermaid-sequence-loop-fg-color)}.noteText>tspan{fill:var(--md-mermaid-sequence-note-fg-color)}#arrowhead path{fill:var(--md-mermaid-sequence-message-line-color);stroke:none}.loopLine{fill:var(--md-mermaid-sequence-loop-bg-color);stroke:var(--md-mermaid-sequence-loop-border-color)}.labelBox{fill:var(--md-mermaid-sequence-label-bg-color);stroke:none}.labelText,.labelText>span{fill:var(--md-mermaid-sequence-label-fg-color);font-family:var(--md-mermaid-font-family)}.sequenceNumber{fill:var(--md-mermaid-sequence-number-fg-color)}rect.rect{fill:var(--md-mermaid-sequence-box-bg-color);stroke:none}rect.rect+text.text{fill:var(--md-mermaid-sequence-box-fg-color)}defs #sequencenumber{fill:var(--md-mermaid-sequence-number-bg-color)!important}";var so,is=0;function as(){return typeof mermaid=="undefined"||mermaid instanceof Element?_t("https://unpkg.com/mermaid@11/dist/mermaid.min.js"):$(void 0)}function Zn(e){return e.classList.remove("mermaid"),so||(so=as().pipe(O(()=>mermaid.initialize({startOnLoad:!1,themeCSS:Xn,sequence:{actorFontSize:"16px",messageFontSize:"16px",noteFontSize:"16px"}})),m(()=>{}),Z(1))),so.subscribe(()=>go(null,null,function*(){e.classList.add("mermaid");let t=`__mermaid_${is++}`,r=x("div",{class:"mermaid"}),o=e.textContent,{svg:n,fn:i}=yield mermaid.render(t,o),s=r.attachShadow({mode:"closed"});s.innerHTML=n,e.replaceWith(r),i==null||i(s)})),so.pipe(m(()=>({ref:e})))}var ei=x("table");function ti(e){return e.replaceWith(ei),ei.replaceWith(Un(e)),$({ref:e})}function ss(e){let t=e.find(r=>r.checked)||e[0];return L(...e.map(r=>h(r,"change").pipe(m(()=>j(`label[for="${r.id}"]`))))).pipe(Q(j(`label[for="${t.id}"]`)),m(r=>({active:r})))}function ri(e,{viewport$:t,target$:r}){let o=j(".tabbed-labels",e),n=M(":scope > input",e),i=no("prev");e.append(i);let s=no("next");return e.append(s),H(()=>{let a=new T,c=a.pipe(oe(),ae(!0));z([a,Le(e),mt(e)]).pipe(W(c),$e(1,ye)).subscribe({next([{active:p},l]){let f=Be(p),{width:u}=de(p);e.style.setProperty("--md-indicator-x",`${f.x}px`),e.style.setProperty("--md-indicator-width",`${u}px`);let d=gr(o);(f.xd.x+l.width)&&o.scrollTo({left:Math.max(0,f.x-16),behavior:"smooth"})},complete(){e.style.removeProperty("--md-indicator-x"),e.style.removeProperty("--md-indicator-width")}}),z([Ge(o),Le(o)]).pipe(W(c)).subscribe(([p,l])=>{let f=At(o);i.hidden=p.x<16,s.hidden=p.x>f.width-l.width-16}),L(h(i,"click").pipe(m(()=>-1)),h(s,"click").pipe(m(()=>1))).pipe(W(c)).subscribe(p=>{let{width:l}=de(o);o.scrollBy({left:l*p,behavior:"smooth"})}),r.pipe(W(c),g(p=>n.includes(p))).subscribe(p=>p.click()),o.classList.add("tabbed-labels--linked");for(let p of n){let l=j(`label[for="${p.id}"]`);l.replaceChildren(x("a",{href:`#${l.htmlFor}`,tabIndex:-1},...Array.from(l.childNodes))),h(l.firstElementChild,"click").pipe(W(c),g(f=>!(f.metaKey||f.ctrlKey)),O(f=>{f.preventDefault(),f.stopPropagation()})).subscribe(()=>{history.replaceState({},"",`#${l.htmlFor}`),l.click()})}return V("content.tabs.link")&&a.pipe(Ie(1),te(t)).subscribe(([{active:p},{offset:l}])=>{let f=p.innerText.trim();if(p.hasAttribute("data-md-switching"))p.removeAttribute("data-md-switching");else{let u=e.offsetTop-l.y;for(let v of M("[data-tabs]"))for(let S of M(":scope > input",v)){let X=j(`label[for="${S.id}"]`);if(X!==p&&X.innerText.trim()===f){X.setAttribute("data-md-switching",""),S.click();break}}window.scrollTo({top:e.offsetTop-u});let d=__md_get("__tabs")||[];__md_set("__tabs",[...new Set([f,...d])])}}),a.pipe(W(c)).subscribe(()=>{for(let p of M("audio, video",e))p.offsetWidth&&p.autoplay?p.play().catch(()=>{}):p.pause()}),ss(n).pipe(O(p=>a.next(p)),A(()=>a.complete()),m(p=>P({ref:e},p)))}).pipe(et(pe))}function oi(e,t){let{viewport$:r,target$:o,print$:n}=t;return L(...M(".annotate:not(.highlight)",e).map(i=>zn(i,{target$:o,print$:n})),...M("pre:not(.mermaid) > code",e).map(i=>Yn(i,{target$:o,print$:n})),...M("a",e).map(i=>Jn(i,t)),...M("pre.mermaid",e).map(i=>Zn(i)),...M("table:not([class])",e).map(i=>ti(i)),...M("details",e).map(i=>Bn(i,{target$:o,print$:n})),...M("[data-tabs]",e).map(i=>ri(i,{viewport$:r,target$:o})),...M("[title]:not([data-preview])",e).filter(()=>V("content.tooltips")).map(i=>Xe(i,{viewport$:r})),...M(".footnote-ref",e).filter(()=>V("content.footnote.tooltips")).map(i=>Vt(i,{content$:new F(s=>{let a=new URL(i.href).hash.slice(1),c=Array.from(document.getElementById(a).cloneNode(!0).children),p=wr(...c);return s.next(p),document.body.append(p),()=>p.remove()}),viewport$:r})))}function cs(e,{alert$:t}){return t.pipe(b(r=>L($(!0),$(!1).pipe(nt(2e3))).pipe(m(o=>({message:r,active:o})))))}function ni(e,t){let r=j(".md-typeset",e);return H(()=>{let o=new T;return o.subscribe(({message:n,active:i})=>{e.classList.toggle("md-dialog--active",i),r.textContent=n}),cs(e,t).pipe(O(n=>o.next(n)),A(()=>o.complete()),m(n=>P({ref:e},n)))})}var ps=0;function ls(e,t){document.body.append(e);let{width:r}=de(e);e.style.setProperty("--md-tooltip-width",`${r}px`),e.remove();let o=vr(t),n=typeof o!="undefined"?Ge(o):$({x:0,y:0}),i=L(Ye(t),it(t)).pipe(Y());return z([i,n]).pipe(m(([s,a])=>{let{x:c,y:p}=Be(t),l=de(t),f=t.closest("table");return f&&t.parentElement&&(c+=f.offsetLeft+t.parentElement.offsetLeft,p+=f.offsetTop+t.parentElement.offsetTop),{active:s,offset:{x:c-a.x+l.width/2-r/2,y:p-a.y+l.height+8}}}))}function ii(e){let t=e.title;if(!t.length)return y;let r=`__tooltip_${ps++}`,o=Dt(r,"inline"),n=j(".md-typeset",o);return n.innerHTML=t,H(()=>{let i=new T;return i.subscribe({next({offset:s}){o.style.setProperty("--md-tooltip-x",`${s.x}px`),o.style.setProperty("--md-tooltip-y",`${s.y}px`)},complete(){o.style.removeProperty("--md-tooltip-x"),o.style.removeProperty("--md-tooltip-y")}}),L(i.pipe(g(({active:s})=>s)),i.pipe(Ae(250),g(({active:s})=>!s))).subscribe({next({active:s}){s?(e.insertAdjacentElement("afterend",o),e.setAttribute("aria-describedby",r),e.removeAttribute("title")):(o.remove(),e.removeAttribute("aria-describedby"),e.setAttribute("title",t))},complete(){o.remove(),e.removeAttribute("aria-describedby"),e.setAttribute("title",t)}}),i.pipe($e(16,ye)).subscribe(({active:s})=>{o.classList.toggle("md-tooltip--active",s)}),i.pipe(gt(125,ye),g(()=>!!e.offsetParent),m(()=>e.offsetParent.getBoundingClientRect()),m(({x:s})=>s)).subscribe({next(s){s?o.style.setProperty("--md-tooltip-0",`${-s}px`):o.style.removeProperty("--md-tooltip-0")},complete(){o.style.removeProperty("--md-tooltip-0")}}),ls(o,e).pipe(O(s=>i.next(s)),A(()=>i.complete()),m(s=>P({ref:e},s)))}).pipe(et(pe))}function ms({viewport$:e}){if(!V("header.autohide"))return $(!1);let t=e.pipe(m(({offset:{y:n}})=>n),ot(2,1),m(([n,i])=>[nMath.abs(i-n.y)>100),m(([,[n]])=>n),Y()),o=Je("search");return z([e,o]).pipe(m(([{offset:n},i])=>n.y>400&&!i),Y(),b(n=>n?r:$(!1)),Q(!1))}function ai(e,t){return H(()=>z([Le(e),ms(t)])).pipe(m(([{height:r},o])=>({height:r,hidden:o})),Y((r,o)=>r.height===o.height&&r.hidden===o.hidden),Z(1))}function si(e,{header$:t,main$:r}){return H(()=>{let o=new T,n=o.pipe(oe(),ae(!0));o.pipe(ne("active"),Pe(t)).subscribe(([{active:s},{hidden:a}])=>{e.classList.toggle("md-header--shadow",s&&!a),e.hidden=a});let i=fe(M("[title]",e)).pipe(g(()=>V("content.tooltips")),J(s=>ii(s)));return r.subscribe(o),t.pipe(W(n),m(s=>P({ref:e},s)),Ve(i.pipe(W(n))))})}function fs(e,{viewport$:t,header$:r}){return Er(e,{viewport$:t,header$:r}).pipe(m(({offset:{y:o}})=>{let{height:n}=de(e);return{active:n>0&&o>=n}}),ne("active"))}function ci(e,t){return H(()=>{let r=new T;r.subscribe({next({active:n}){e.classList.toggle("md-header__title--active",n)},complete(){e.classList.remove("md-header__title--active")}});let o=ue(".md-content h1");return typeof o=="undefined"?y:fs(o,t).pipe(O(n=>r.next(n)),A(()=>r.complete()),m(n=>P({ref:e},n)))})}function pi(e,{viewport$:t,header$:r}){let o=r.pipe(m(({height:i})=>i),Y()),n=o.pipe(b(()=>Le(e).pipe(m(({height:i})=>({top:e.offsetTop,bottom:e.offsetTop+i})),ne("bottom"))));return z([o,n,t]).pipe(m(([i,{top:s,bottom:a},{offset:{y:c},size:{height:p}}])=>(p=Math.max(0,p-Math.max(0,s-c,i)-Math.max(0,p+c-a)),{offset:s-i,height:p,active:s-i<=c})),Y((i,s)=>i.offset===s.offset&&i.height===s.height&&i.active===s.active))}function us(e){let t=__md_get("__palette")||{index:e.findIndex(o=>matchMedia(o.getAttribute("data-md-color-media")).matches)},r=Math.max(0,Math.min(t.index,e.length-1));return $(...e).pipe(J(o=>h(o,"change").pipe(m(()=>o))),Q(e[r]),m(o=>({index:e.indexOf(o),color:{media:o.getAttribute("data-md-color-media"),scheme:o.getAttribute("data-md-color-scheme"),primary:o.getAttribute("data-md-color-primary"),accent:o.getAttribute("data-md-color-accent")}})),Z(1))}function li(e){let t=M("input",e),r=x("meta",{name:"theme-color"});document.head.appendChild(r);let o=x("meta",{name:"color-scheme"});document.head.appendChild(o);let n=Wt("(prefers-color-scheme: light)");return H(()=>{let i=new T;return i.subscribe(s=>{if(document.body.setAttribute("data-md-color-switching",""),s.color.media==="(prefers-color-scheme)"){let a=matchMedia("(prefers-color-scheme: light)"),c=document.querySelector(a.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");s.color.scheme=c.getAttribute("data-md-color-scheme"),s.color.primary=c.getAttribute("data-md-color-primary"),s.color.accent=c.getAttribute("data-md-color-accent")}for(let[a,c]of Object.entries(s.color))document.body.setAttribute(`data-md-color-${a}`,c);for(let a=0;as.key==="Enter"),te(i,(s,a)=>a)).subscribe(({index:s})=>{s=(s+1)%t.length,t[s].click(),t[s].focus()}),i.pipe(m(()=>{let s=Ce("header"),a=window.getComputedStyle(s);return o.content=a.colorScheme,a.backgroundColor.match(/\d+/g).map(c=>(+c).toString(16).padStart(2,"0")).join("")})).subscribe(s=>r.content=`#${s}`),i.pipe(xe(pe)).subscribe(()=>{document.body.removeAttribute("data-md-color-switching")}),us(t).pipe(W(n.pipe(Ie(1))),vt(),O(s=>i.next(s)),A(()=>i.complete()),m(s=>P({ref:e},s)))})}function mi(e,{progress$:t}){return H(()=>{let r=new T;return r.subscribe(({value:o})=>{e.style.setProperty("--md-progress-value",`${o}`)}),t.pipe(O(o=>r.next({value:o})),A(()=>r.complete()),m(o=>({ref:e,value:o})))})}function fi(e,t){return e.protocol=t.protocol,e.hostname=t.hostname,e}function ds(e,t){let r=new Map;for(let o of M("url",e)){let n=j("loc",o),i=[fi(new URL(n.textContent),t)];r.set(`${i[0]}`,i);for(let s of M("[rel=alternate]",o)){let a=s.getAttribute("href");a!=null&&i.push(fi(new URL(a),t))}}return r}function kt(e){return En(new URL("sitemap.xml",e)).pipe(m(t=>ds(t,new URL(e))),ve(()=>$(new Map)),le())}function ui({document$:e}){let t=new Map;e.pipe(b(()=>M("link[rel=alternate]")),m(r=>new URL(r.href)),g(r=>!t.has(r.toString())),J(r=>kt(r).pipe(m(o=>[r,o]),ve(()=>y)))).subscribe(([r,o])=>{t.set(r.toString().replace(/\/$/,""),o)}),h(document.body,"click").pipe(g(r=>!r.metaKey&&!r.ctrlKey),b(r=>{if(r.target instanceof Element){let o=r.target.closest("a");if(o&&!o.target){let n=[...t].find(([f])=>o.href.startsWith(`${f}/`));if(typeof n=="undefined")return y;let[i,s]=n,a=we();if(a.href.startsWith(i))return y;let c=Te(),p=a.href.replace(c.base,"");p=`${i}/${p}`;let l=s.has(p.split("#")[0])?new URL(p,c.base):new URL(i);return r.preventDefault(),$(l)}}return y})).subscribe(r=>st(r,!0))}var co=$t(ao());function hs(e){e.setAttribute("data-md-copying","");let t=e.closest("[data-copy]"),r=t?t.getAttribute("data-copy"):e.innerText;return e.removeAttribute("data-md-copying"),r.trimEnd()}function di({alert$:e}){co.default.isSupported()&&new F(t=>{new co.default("[data-clipboard-target], [data-clipboard-text]",{text:r=>r.getAttribute("data-clipboard-text")||hs(j(r.getAttribute("data-clipboard-target")))}).on("success",r=>t.next(r))}).pipe(O(t=>{t.trigger.focus()}),m(()=>Me("clipboard.copied"))).subscribe(e)}function hi(e,t){if(!(e.target instanceof Element))return y;let r=e.target.closest("a");if(r===null)return y;if(r.target||e.metaKey||e.ctrlKey)return y;let o=new URL(r.href);return o.search=o.hash="",t.has(`${o}`)?(e.preventDefault(),$(r)):y}function bi(e){let t=new Map;for(let r of M(":scope > *",e.head))t.set(r.outerHTML,r);return t}function vi(e){for(let t of M("[href], [src]",e))for(let r of["href","src"]){let o=t.getAttribute(r);if(o&&!/^(?:[a-z]+:)?\/\//i.test(o)){t[r]=t[r];break}}return $(e)}function bs(e){for(let o of["[data-md-component=announce]","[data-md-component=container]","[data-md-component=header-topic]","[data-md-component=outdated]","[data-md-component=logo]","[data-md-component=skip]",...V("navigation.tabs.sticky")?["[data-md-component=tabs]"]:[]]){let n=ue(o),i=ue(o,e);typeof n!="undefined"&&typeof i!="undefined"&&n.replaceWith(i)}let t=bi(document);for(let[o,n]of bi(e))t.has(o)?t.delete(o):document.head.appendChild(n);for(let o of t.values()){let n=o.getAttribute("name");n!=="theme-color"&&n!=="color-scheme"&&o.remove()}let r=Ce("container");return Ke(M("script",r)).pipe(b(o=>{let n=e.createElement("script");if(o.src){for(let i of o.getAttributeNames())n.setAttribute(i,o.getAttribute(i));return o.replaceWith(n),new F(i=>{n.onload=()=>i.complete()})}else return n.textContent=o.textContent,o.replaceWith(n),y}),oe(),ae(document))}function gi({sitemap$:e,location$:t,viewport$:r,progress$:o}){if(location.protocol==="file:")return y;$(document).subscribe(vi);let n=h(document.body,"click").pipe(Pe(e),b(([a,c])=>hi(a,c)),m(({href:a})=>new URL(a)),le()),i=h(window,"popstate").pipe(m(we),le());n.pipe(te(r)).subscribe(([a,{offset:c}])=>{history.replaceState(c,""),history.pushState(null,"",a)}),L(n,i).subscribe(t);let s=t.pipe(ne("pathname"),b(a=>xr(a,{progress$:o}).pipe(ve(()=>(st(a,!0),y)))),b(vi),b(bs),le());return L(s.pipe(te(t,(a,c)=>c)),s.pipe(b(()=>t),ne("hash")),t.pipe(Y((a,c)=>a.pathname===c.pathname&&a.hash===c.hash),b(()=>n),O(()=>history.back()))).subscribe(a=>{var c,p;history.state!==null||!a.hash?window.scrollTo(0,(p=(c=history.state)==null?void 0:c.y)!=null?p:0):(history.scrollRestoration="auto",gn(a.hash),history.scrollRestoration="manual")}),t.subscribe(()=>{history.scrollRestoration="manual"}),h(window,"beforeunload").subscribe(()=>{history.scrollRestoration="auto"}),r.pipe(ne("offset"),Ae(100)).subscribe(({offset:a})=>{history.replaceState(a,"")}),V("navigation.instant.prefetch")&&L(h(document.body,"mousemove"),h(document.body,"focusin")).pipe(Pe(e),b(([a,c])=>hi(a,c)),Ae(25),Qr(({href:a})=>a),hr(a=>{let c=document.createElement("link");return c.rel="prefetch",c.href=a.toString(),document.head.appendChild(c),h(c,"load").pipe(m(()=>c),Ee(1))})).subscribe(a=>a.remove()),s}var yi=$t(ro());function xi(e){let t=e.separator.split("|").map(n=>n.replace(/(\(\?[!=<][^)]+\))/g,"").length===0?"\uFFFD":n).join("|"),r=new RegExp(t,"img"),o=(n,i,s)=>`${i}${s}`;return n=>{n=n.replace(/[\s*+\-:~^]+/g," ").replace(/&/g,"&").trim();let i=new RegExp(`(^|${e.separator}|)(${n.replace(/[|\\{}()[\]^$+*?.-]/g,"\\$&").replace(r,"|")})`,"img");return s=>(0,yi.default)(s).replace(i,o).replace(/<\/mark>(\s+)]*>/img,"$1")}}function zt(e){return e.type===1}function Sr(e){return e.type===3}function Ei(e,t){let r=Mn(e);return L($(location.protocol!=="file:"),Je("search")).pipe(Re(o=>o),b(()=>t)).subscribe(({config:o,docs:n})=>r.next({type:0,data:{config:o,docs:n,options:{suggest:V("search.suggest")}}})),r}function wi(e){var l;let{selectedVersionSitemap:t,selectedVersionBaseURL:r,currentLocation:o,currentBaseURL:n}=e,i=(l=po(n))==null?void 0:l.pathname;if(i===void 0)return;let s=ys(o.pathname,i);if(s===void 0)return;let a=Es(t.keys());if(!t.has(a))return;let c=po(s,a);if(!c||!t.has(c.href))return;let p=po(s,r);if(p)return p.hash=o.hash,p.search=o.search,p}function po(e,t){try{return new URL(e,t)}catch(r){return}}function ys(e,t){if(e.startsWith(t))return e.slice(t.length)}function xs(e,t){let r=Math.min(e.length,t.length),o;for(o=0;oy)),o=r.pipe(m(n=>{let[,i]=t.base.match(/([^/]+)\/?$/);return n.find(({version:s,aliases:a})=>s===i||a.includes(i))||n[0]}));r.pipe(m(n=>new Map(n.map(i=>[`${new URL(`../${i.version}/`,t.base)}`,i]))),b(n=>h(document.body,"click").pipe(g(i=>!i.metaKey&&!i.ctrlKey),te(o),b(([i,s])=>{if(i.target instanceof Element){let a=i.target.closest("a");if(a&&!a.target&&n.has(a.href)){let c=a.href;return!i.target.closest(".md-version")&&n.get(c)===s?y:(i.preventDefault(),$(new URL(c)))}}return y}),b(i=>kt(i).pipe(m(s=>{var a;return(a=wi({selectedVersionSitemap:s,selectedVersionBaseURL:i,currentLocation:we(),currentBaseURL:t.base}))!=null?a:i})))))).subscribe(n=>st(n,!0)),z([r,o]).subscribe(([n,i])=>{j(".md-header__topic").appendChild(Wn(n,i))}),e.pipe(b(()=>o)).subscribe(n=>{var a;let i=new URL(t.base),s=__md_get("__outdated",sessionStorage,i);if(s===null){s=!0;let c=((a=t.version)==null?void 0:a.default)||"latest";Array.isArray(c)||(c=[c]);e:for(let p of c)for(let l of n.aliases.concat(n.version))if(new RegExp(p,"i").test(l)){s=!1;break e}__md_set("__outdated",s,sessionStorage,i)}if(s)for(let c of me("outdated"))c.hidden=!1})}function ws(e,{worker$:t}){let{searchParams:r}=we();r.has("q")&&(at("search",!0),e.value=r.get("q"),e.focus(),Je("search").pipe(Re(i=>!i)).subscribe(()=>{let i=we();i.searchParams.delete("q"),history.replaceState({},"",`${i}`)}));let o=Ye(e),n=L(t.pipe(Re(zt)),h(e,"keyup"),o).pipe(m(()=>e.value),Y());return z([n,o]).pipe(m(([i,s])=>({value:i,focus:s})),Z(1))}function Si(e,{worker$:t}){let r=new T,o=r.pipe(oe(),ae(!0));z([t.pipe(Re(zt)),r],(i,s)=>s).pipe(ne("value")).subscribe(({value:i})=>t.next({type:2,data:i})),r.pipe(ne("focus")).subscribe(({focus:i})=>{i&&at("search",i)}),h(e.form,"reset").pipe(W(o)).subscribe(()=>e.focus());let n=j("header [for=__search]");return h(n,"click").subscribe(()=>e.focus()),ws(e,{worker$:t}).pipe(O(i=>r.next(i)),A(()=>r.complete()),m(i=>P({ref:e},i)),Z(1))}function Oi(e,{worker$:t,query$:r}){let o=new T,n=un(e.parentElement).pipe(g(Boolean)),i=e.parentElement,s=j(":scope > :first-child",e),a=j(":scope > :last-child",e);Je("search").subscribe(l=>{a.setAttribute("role",l?"list":"presentation"),a.hidden=!l}),o.pipe(te(r),Gr(t.pipe(Re(zt)))).subscribe(([{items:l},{value:f}])=>{switch(l.length){case 0:s.textContent=f.length?Me("search.result.none"):Me("search.result.placeholder");break;case 1:s.textContent=Me("search.result.one");break;default:let u=br(l.length);s.textContent=Me("search.result.other",u)}});let c=o.pipe(O(()=>a.innerHTML=""),b(({items:l})=>L($(...l.slice(0,10)),$(...l.slice(10)).pipe(ot(4),Xr(n),b(([f])=>f)))),m(Fn),le());return c.subscribe(l=>a.appendChild(l)),c.pipe(J(l=>{let f=ue("details",l);return typeof f=="undefined"?y:h(f,"toggle").pipe(W(o),m(()=>f))})).subscribe(l=>{l.open===!1&&l.offsetTop<=i.scrollTop&&i.scrollTo({top:l.offsetTop})}),t.pipe(g(Sr),m(({data:l})=>l)).pipe(O(l=>o.next(l)),A(()=>o.complete()),m(l=>P({ref:e},l)))}function Ts(e,{query$:t}){return t.pipe(m(({value:r})=>{let o=we();return o.hash="",r=r.replace(/\s+/g,"+").replace(/&/g,"%26").replace(/=/g,"%3D"),o.search=`q=${r}`,{url:o}}))}function Li(e,t){let r=new T,o=r.pipe(oe(),ae(!0));return r.subscribe(({url:n})=>{e.setAttribute("data-clipboard-text",e.href),e.href=`${n}`}),h(e,"click").pipe(W(o)).subscribe(n=>n.preventDefault()),Ts(e,t).pipe(O(n=>r.next(n)),A(()=>r.complete()),m(n=>P({ref:e},n)))}function Mi(e,{worker$:t,keyboard$:r}){let o=new T,n=Ce("search-query"),i=L(h(n,"keydown"),h(n,"focus")).pipe(xe(pe),m(()=>n.value),Y());return o.pipe(Pe(i),m(([{suggest:a},c])=>{let p=c.split(/([\s-]+)/);if(a!=null&&a.length&&p[p.length-1]){let l=a[a.length-1];l.startsWith(p[p.length-1])&&(p[p.length-1]=l)}else p.length=0;return p})).subscribe(a=>e.innerHTML=a.join("").replace(/\s/g," ")),r.pipe(g(({mode:a})=>a==="search")).subscribe(a=>{a.type==="ArrowRight"&&e.innerText.length&&n.selectionStart===n.value.length&&(n.value=e.innerText)}),t.pipe(g(Sr),m(({data:a})=>a)).pipe(O(a=>o.next(a)),A(()=>o.complete()),m(()=>({ref:e})))}function _i(e,{index$:t,keyboard$:r}){let o=Te();try{let n=Ei(o.search,t),i=Ce("search-query",e),s=Ce("search-result",e);h(e,"click").pipe(g(({target:c})=>c instanceof Element&&!!c.closest("a"))).subscribe(()=>at("search",!1)),r.pipe(g(({mode:c})=>c==="search")).subscribe(c=>{let p=Ne();switch(c.type){case"Enter":if(p===i){let l=new Map;for(let f of M(":first-child [href]",s)){let u=f.firstElementChild;l.set(f,parseFloat(u.getAttribute("data-md-score")))}if(l.size){let[[f]]=[...l].sort(([,u],[,d])=>d-u);f.click()}c.claim()}break;case"Escape":case"Tab":at("search",!1),i.blur();break;case"ArrowUp":case"ArrowDown":if(typeof p=="undefined")i.focus();else{let l=[i,...M(":not(details) > [href], summary, details[open] [href]",s)],f=Math.max(0,(Math.max(0,l.indexOf(p))+l.length+(c.type==="ArrowUp"?-1:1))%l.length);l[f].focus()}c.claim();break;default:i!==Ne()&&i.focus()}}),r.pipe(g(({mode:c})=>c==="global")).subscribe(c=>{switch(c.type){case"f":case"s":case"/":i.focus(),i.select(),c.claim();break}});let a=Si(i,{worker$:n});return L(a,Oi(s,{worker$:n,query$:a})).pipe(Ve(...me("search-share",e).map(c=>Li(c,{query$:a})),...me("search-suggest",e).map(c=>Mi(c,{worker$:n,keyboard$:r}))))}catch(n){return e.hidden=!0,tt}}function Ai(e,{index$:t,location$:r}){return z([t,r.pipe(Q(we()),g(o=>!!o.searchParams.get("h")))]).pipe(m(([o,n])=>xi(o.config)(n.searchParams.get("h"))),m(o=>{var s;let n=new Map,i=document.createNodeIterator(e,NodeFilter.SHOW_TEXT);for(let a=i.nextNode();a;a=i.nextNode())if((s=a.parentElement)!=null&&s.offsetHeight){let c=a.textContent,p=o(c);p.length>c.length&&n.set(a,p)}for(let[a,c]of n){let{childNodes:p}=x("span",null,c);a.replaceWith(...Array.from(p))}return{ref:e,nodes:n}}))}function Ss(e,{viewport$:t,main$:r}){let o=e.closest(".md-grid"),n=o.offsetTop-o.parentElement.offsetTop;return z([r,t]).pipe(m(([{offset:i,height:s},{offset:{y:a}}])=>(s=s+Math.min(n,Math.max(0,a-i))-n,{height:s,locked:a>=i+n})),Y((i,s)=>i.height===s.height&&i.locked===s.locked))}function lo(e,o){var n=o,{header$:t}=n,r=vo(n,["header$"]);let i=j(".md-sidebar__scrollwrap",e),{y:s}=Be(i);return H(()=>{let a=new T,c=a.pipe(oe(),ae(!0)),p=a.pipe($e(0,ye));return p.pipe(te(t)).subscribe({next([{height:l},{height:f}]){i.style.height=`${l-2*s}px`,e.style.top=`${f}px`},complete(){i.style.height="",e.style.top=""}}),p.pipe(Re()).subscribe(()=>{for(let l of M(".md-nav__link--active[href]",e)){if(!l.clientHeight)continue;let f=l.closest(".md-sidebar__scrollwrap");if(typeof f!="undefined"){let u=l.offsetTop-f.offsetTop,{height:d}=de(f);f.scrollTo({top:u-d/2})}}}),fe(M("label[tabindex]",e)).pipe(J(l=>h(l,"click").pipe(xe(pe),m(()=>l),W(c)))).subscribe(l=>{let f=j(`[id="${l.htmlFor}"]`);j(`[aria-labelledby="${l.id}"]`).setAttribute("aria-expanded",`${f.checked}`)}),V("content.tooltips")&&fe(M("abbr[title]",e)).pipe(J(l=>Xe(l,{viewport$})),W(c)).subscribe(),Ss(e,r).pipe(O(l=>a.next(l)),A(()=>a.complete()),m(l=>P({ref:e},l)))})}function Ci(e,t){if(typeof t!="undefined"){let r=`https://api.github.com/repos/${e}/${t}`;return rt(ze(`${r}/releases/latest`).pipe(ve(()=>y),m(o=>({version:o.tag_name})),Qe({})),ze(r).pipe(ve(()=>y),m(o=>({stars:o.stargazers_count,forks:o.forks_count})),Qe({}))).pipe(m(([o,n])=>P(P({},o),n)))}else{let r=`https://api.github.com/users/${e}`;return ze(r).pipe(m(o=>({repositories:o.public_repos})),Qe({}))}}function ki(e,t){let r=`https://${e}/api/v4/projects/${encodeURIComponent(t)}`;return rt(ze(`${r}/releases/permalink/latest`).pipe(ve(()=>y),m(({tag_name:o})=>({version:o})),Qe({})),ze(r).pipe(ve(()=>y),m(({star_count:o,forks_count:n})=>({stars:o,forks:n})),Qe({}))).pipe(m(([o,n])=>P(P({},o),n)))}function Hi(e){let t=e.match(/^.+github\.com\/([^/]+)\/?([^/]+)?/i);if(t){let[,r,o]=t;return Ci(r,o)}if(t=e.match(/^.+?([^/]*gitlab[^/]+)\/(.+?)\/?$/i),t){let[,r,o]=t;return ki(r,o)}return y}var Os;function Ls(e){return Os||(Os=H(()=>{let t=__md_get("__source",sessionStorage);if(t)return $(t);if(me("consent").length){let o=__md_get("__consent");if(!(o&&o.github))return y}return Hi(e.href).pipe(O(o=>__md_set("__source",o,sessionStorage)))}).pipe(ve(()=>y),g(t=>Object.keys(t).length>0),m(t=>({facts:t})),Z(1)))}function $i(e){let t=j(":scope > :last-child",e);return H(()=>{let r=new T;return r.subscribe(({facts:o})=>{t.appendChild(jn(o)),t.classList.add("md-source__repository--active")}),Ls(e).pipe(O(o=>r.next(o)),A(()=>r.complete()),m(o=>P({ref:e},o)))})}function Ms(e,{viewport$:t,header$:r}){return Le(document.body).pipe(b(()=>Er(e,{header$:r,viewport$:t})),m(({offset:{y:o}})=>({hidden:o>=10})),ne("hidden"))}function Pi(e,t){return H(()=>{let r=new T;return r.subscribe({next({hidden:o}){e.hidden=o},complete(){e.hidden=!1}}),(V("navigation.tabs.sticky")?$({hidden:!1}):Ms(e,t)).pipe(O(o=>r.next(o)),A(()=>r.complete()),m(o=>P({ref:e},o)))})}function _s(e,{viewport$:t,header$:r}){let o=new Map,n=M(".md-nav__link",e);for(let a of n){let c=decodeURIComponent(a.hash.substring(1)),p=ue(`[id="${c}"]`);typeof p!="undefined"&&o.set(a,p)}let i=r.pipe(ne("height"),m(({height:a})=>{let c=Ce("main"),p=j(":scope > :first-child",c);return a+.8*(p.offsetTop-c.offsetTop)}),le());return Le(document.body).pipe(ne("height"),b(a=>H(()=>{let c=[];return $([...o].reduce((p,[l,f])=>{for(;c.length&&o.get(c[c.length-1]).tagName>=f.tagName;)c.pop();let u=f.offsetTop;for(;!u&&f.parentElement;)f=f.parentElement,u=f.offsetTop;let d=f.offsetParent;for(;d;d=d.offsetParent)u+=d.offsetTop;return p.set([...c=[...c,l]].reverse(),u)},new Map))}).pipe(m(c=>new Map([...c].sort(([,p],[,l])=>p-l))),Pe(i),b(([c,p])=>t.pipe(Ut(([l,f],{offset:{y:u},size:d})=>{let v=u+d.height>=Math.floor(a.height);for(;f.length;){let[,S]=f[0];if(S-p=u&&!v)f=[l.pop(),...f];else break}return[l,f]},[[],[...c]]),Y((l,f)=>l[0]===f[0]&&l[1]===f[1])))))).pipe(m(([a,c])=>({prev:a.map(([p])=>p),next:c.map(([p])=>p)})),Q({prev:[],next:[]}),ot(2,1),m(([a,c])=>a.prev.length{let i=new T,s=i.pipe(oe(),ae(!0));if(i.subscribe(({prev:a,next:c})=>{for(let[p]of c)p.classList.remove("md-nav__link--passed"),p.classList.remove("md-nav__link--active");for(let[p,[l]]of a.entries())l.classList.add("md-nav__link--passed"),l.classList.toggle("md-nav__link--active",p===a.length-1)}),V("toc.follow")){let a=L(t.pipe(Ae(1),m(()=>{})),t.pipe(Ae(250),m(()=>"smooth")));i.pipe(g(({prev:c})=>c.length>0),Pe(o.pipe(xe(pe))),te(a)).subscribe(([[{prev:c}],p])=>{let[l]=c[c.length-1];if(l.offsetHeight){let f=vr(l);if(typeof f!="undefined"){let u=l.offsetTop-f.offsetTop,{height:d}=de(f);f.scrollTo({top:u-d/2,behavior:p})}}})}return V("navigation.tracking")&&t.pipe(W(s),ne("offset"),Ae(250),Ie(1),W(n.pipe(Ie(1))),vt({delay:250}),te(i)).subscribe(([,{prev:a}])=>{let c=we(),p=a[a.length-1];if(p&&p.length){let[l]=p,{hash:f}=new URL(l.href);c.hash!==f&&(c.hash=f,history.replaceState({},"",`${c}`))}else c.hash="",history.replaceState({},"",`${c}`)}),_s(e,{viewport$:t,header$:r}).pipe(O(a=>i.next(a)),A(()=>i.complete()),m(a=>P({ref:e},a)))})}function As(e,{viewport$:t,main$:r,target$:o}){let n=t.pipe(m(({offset:{y:s}})=>s),ot(2,1),m(([s,a])=>s>a&&a>0),Y()),i=r.pipe(m(({active:s})=>s));return z([i,n]).pipe(m(([s,a])=>!(s&&a)),Y(),W(o.pipe(Ie(1))),ae(!0),vt({delay:250}),m(s=>({hidden:s})))}function Ii(e,{viewport$:t,header$:r,main$:o,target$:n}){let i=new T,s=i.pipe(oe(),ae(!0));return i.subscribe({next({hidden:a}){e.hidden=a,a?(e.setAttribute("tabindex","-1"),e.blur()):e.removeAttribute("tabindex")},complete(){e.style.top="",e.hidden=!0,e.removeAttribute("tabindex")}}),r.pipe(W(s),ne("height")).subscribe(({height:a})=>{e.style.top=`${a+16}px`}),h(e,"click").subscribe(a=>{a.preventDefault(),window.scrollTo({top:0})}),As(e,{viewport$:t,main$:o,target$:n}).pipe(O(a=>i.next(a)),A(()=>i.complete()),m(a=>P({ref:e},a)))}function Fi({document$:e,viewport$:t}){e.pipe(b(()=>M(".md-ellipsis")),J(r=>mt(r).pipe(W(e.pipe(Ie(1))),g(o=>o),m(()=>r),Ee(1))),g(r=>r.offsetWidth{let o=r.innerText,n=r.closest("a")||r;return n.title=o,V("content.tooltips")?Xe(n,{viewport$:t}).pipe(W(e.pipe(Ie(1))),A(()=>n.removeAttribute("title"))):y})).subscribe(),V("content.tooltips")&&e.pipe(b(()=>M(".md-status")),J(r=>Xe(r,{viewport$:t}))).subscribe()}function ji({document$:e,tablet$:t}){e.pipe(b(()=>M(".md-toggle--indeterminate")),O(r=>{r.indeterminate=!0,r.checked=!1}),J(r=>h(r,"change").pipe(Jr(()=>r.classList.contains("md-toggle--indeterminate")),m(()=>r))),te(t)).subscribe(([r,o])=>{r.classList.remove("md-toggle--indeterminate"),o&&(r.checked=!1)})}function Cs(){return/(iPad|iPhone|iPod)/.test(navigator.userAgent)}function Ui({document$:e}){e.pipe(b(()=>M("[data-md-scrollfix]")),O(t=>t.removeAttribute("data-md-scrollfix")),g(Cs),J(t=>h(t,"touchstart").pipe(m(()=>t)))).subscribe(t=>{let r=t.scrollTop;r===0?t.scrollTop=1:r+t.offsetHeight===t.scrollHeight&&(t.scrollTop=r-1)})}function Wi({viewport$:e,tablet$:t}){z([Je("search"),t]).pipe(m(([r,o])=>r&&!o),b(r=>$(r).pipe(nt(r?400:100))),te(e)).subscribe(([r,{offset:{y:o}}])=>{if(r)document.body.setAttribute("data-md-scrolllock",""),document.body.style.top=`-${o}px`;else{let n=-1*parseInt(document.body.style.top,10);document.body.removeAttribute("data-md-scrolllock"),document.body.style.top="",n&&window.scrollTo(0,n)}})}Object.entries||(Object.entries=function(e){let t=[];for(let r of Object.keys(e))t.push([r,e[r]]);return t});Object.values||(Object.values=function(e){let t=[];for(let r of Object.keys(e))t.push(e[r]);return t});typeof Element!="undefined"&&(Element.prototype.scrollTo||(Element.prototype.scrollTo=function(e,t){typeof e=="object"?(this.scrollLeft=e.left,this.scrollTop=e.top):(this.scrollLeft=e,this.scrollTop=t)}),Element.prototype.replaceWith||(Element.prototype.replaceWith=function(...e){let t=this.parentNode;if(t){e.length===0&&t.removeChild(this);for(let r=e.length-1;r>=0;r--){let o=e[r];typeof o=="string"?o=document.createTextNode(o):o.parentNode&&o.parentNode.removeChild(o),r?t.insertBefore(this.previousSibling,o):t.replaceChild(o,this)}}}));function ks(){return location.protocol==="file:"?_t(`${new URL("search/search_index.js",Or.base)}`).pipe(m(()=>__index),Z(1)):ze(new URL("search/search_index.json",Or.base))}document.documentElement.classList.remove("no-js");document.documentElement.classList.add("js");var ct=an(),Kt=bn(),Ht=yn(Kt),mo=hn(),ke=Ln(),Lr=Wt("(min-width: 60em)"),Vi=Wt("(min-width: 76.25em)"),Ni=xn(),Or=Te(),zi=document.forms.namedItem("search")?ks():tt,fo=new T;di({alert$:fo});ui({document$:ct});var uo=new T,qi=kt(Or.base);V("navigation.instant")&&gi({sitemap$:qi,location$:Kt,viewport$:ke,progress$:uo}).subscribe(ct);var Di;((Di=Or.version)==null?void 0:Di.provider)==="mike"&&Ti({document$:ct});L(Kt,Ht).pipe(nt(125)).subscribe(()=>{at("drawer",!1),at("search",!1)});mo.pipe(g(({mode:e})=>e==="global")).subscribe(e=>{switch(e.type){case"p":case",":let t=ue("link[rel=prev]");typeof t!="undefined"&&st(t);break;case"n":case".":let r=ue("link[rel=next]");typeof r!="undefined"&&st(r);break;case"Enter":let o=Ne();o instanceof HTMLLabelElement&&o.click()}});Fi({viewport$:ke,document$:ct});ji({document$:ct,tablet$:Lr});Ui({document$:ct});Wi({viewport$:ke,tablet$:Lr});var ft=ai(Ce("header"),{viewport$:ke}),qt=ct.pipe(m(()=>Ce("main")),b(e=>pi(e,{viewport$:ke,header$:ft})),Z(1)),Hs=L(...me("consent").map(e=>An(e,{target$:Ht})),...me("dialog").map(e=>ni(e,{alert$:fo})),...me("palette").map(e=>li(e)),...me("progress").map(e=>mi(e,{progress$:uo})),...me("search").map(e=>_i(e,{index$:zi,keyboard$:mo})),...me("source").map(e=>$i(e))),$s=H(()=>L(...me("announce").map(e=>_n(e)),...me("content").map(e=>oi(e,{sitemap$:qi,viewport$:ke,target$:Ht,print$:Ni})),...me("content").map(e=>V("search.highlight")?Ai(e,{index$:zi,location$:Kt}):y),...me("header").map(e=>si(e,{viewport$:ke,header$:ft,main$:qt})),...me("header-title").map(e=>ci(e,{viewport$:ke,header$:ft})),...me("sidebar").map(e=>e.getAttribute("data-md-type")==="navigation"?eo(Vi,()=>lo(e,{viewport$:ke,header$:ft,main$:qt})):eo(Lr,()=>lo(e,{viewport$:ke,header$:ft,main$:qt}))),...me("tabs").map(e=>Pi(e,{viewport$:ke,header$:ft})),...me("toc").map(e=>Ri(e,{viewport$:ke,header$:ft,main$:qt,target$:Ht})),...me("top").map(e=>Ii(e,{viewport$:ke,header$:ft,main$:qt,target$:Ht})))),Ki=ct.pipe(b(()=>$s),Ve(Hs),Z(1));Ki.subscribe();window.document$=ct;window.location$=Kt;window.target$=Ht;window.keyboard$=mo;window.viewport$=ke;window.tablet$=Lr;window.screen$=Vi;window.print$=Ni;window.alert$=fo;window.progress$=uo;window.component$=Ki;})(); //# sourceMappingURL=bundle.79ae519e.min.js.map -/*! update cache: 20260104155739 */ +/*! update cache: 20260104161439 */ diff --git a/zh-hant/assets/javascripts/glightbox.min.js b/zh-hant/assets/javascripts/glightbox.min.js index dd5e867ac..108fd965d 100644 --- a/zh-hant/assets/javascripts/glightbox.min.js +++ b/zh-hant/assets/javascripts/glightbox.min.js @@ -1,2 +1,2 @@ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).GLightbox=t()}(this,(function(){"use strict";function e(t){return(e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(t)}function t(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){for(var i=0;i1&&void 0!==arguments[1]?arguments[1]:null,i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null,n=e[s]=e[s]||[],l={all:n,evt:null,found:null};return t&&i&&P(n)>0&&o(n,(function(e,n){if(e.eventName==t&&e.fn.toString()==i.toString())return l.found=!0,l.evt=n,!1})),l}function a(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=t.onElement,n=t.withCallback,s=t.avoidDuplicate,l=void 0===s||s,a=t.once,h=void 0!==a&&a,d=t.useCapture,c=void 0!==d&&d,u=arguments.length>2?arguments[2]:void 0,g=i||[];function v(e){T(n)&&n.call(u,e,this),h&&v.destroy()}return C(g)&&(g=document.querySelectorAll(g)),v.destroy=function(){o(g,(function(t){var i=r(t,e,v);i.found&&i.all.splice(i.evt,1),t.removeEventListener&&t.removeEventListener(e,v,c)}))},o(g,(function(t){var i=r(t,e,v);(t.addEventListener&&l&&!i.found||!l)&&(t.addEventListener(e,v,c),i.all.push({eventName:e,fn:v}))})),v}function h(e,t){o(t.split(" "),(function(t){return e.classList.add(t)}))}function d(e,t){o(t.split(" "),(function(t){return e.classList.remove(t)}))}function c(e,t){return e.classList.contains(t)}function u(e,t){for(;e!==document.body;){if(!(e=e.parentElement))return!1;if("function"==typeof e.matches?e.matches(t):e.msMatchesSelector(t))return e}}function g(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",i=arguments.length>2&&void 0!==arguments[2]&&arguments[2];if(!e||""===t)return!1;if("none"==t)return T(i)&&i(),!1;var n=x(),s=t.split(" ");o(s,(function(t){h(e,"g"+t)})),a(n,{onElement:e,avoidDuplicate:!1,once:!0,withCallback:function(e,t){o(s,(function(e){d(t,"g"+e)})),T(i)&&i()}})}function v(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";if(""==t)return e.style.webkitTransform="",e.style.MozTransform="",e.style.msTransform="",e.style.OTransform="",e.style.transform="",!1;e.style.webkitTransform=t,e.style.MozTransform=t,e.style.msTransform=t,e.style.OTransform=t,e.style.transform=t}function f(e){e.style.display="block"}function p(e){e.style.display="none"}function m(e){var t=document.createDocumentFragment(),i=document.createElement("div");for(i.innerHTML=e;i.firstChild;)t.appendChild(i.firstChild);return t}function y(){return{width:window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth,height:window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight}}function x(){var e,t=document.createElement("fakeelement"),i={animation:"animationend",OAnimation:"oAnimationEnd",MozAnimation:"animationend",WebkitAnimation:"webkitAnimationEnd"};for(e in i)if(void 0!==t.style[e])return i[e]}function b(e,t,i,n){if(e())t();else{var s;i||(i=100);var l=setInterval((function(){e()&&(clearInterval(l),s&&clearTimeout(s),t())}),i);n&&(s=setTimeout((function(){clearInterval(l)}),n))}}function S(e,t,i){if(I(e))console.error("Inject assets error");else if(T(t)&&(i=t,t=!1),C(t)&&t in window)T(i)&&i();else{var n;if(-1!==e.indexOf(".css")){if((n=document.querySelectorAll('link[href="'+e+'"]'))&&n.length>0)return void(T(i)&&i());var s=document.getElementsByTagName("head")[0],l=s.querySelectorAll('link[rel="stylesheet"]'),o=document.createElement("link");return o.rel="stylesheet",o.type="text/css",o.href=e,o.media="all",l?s.insertBefore(o,l[0]):s.appendChild(o),void(T(i)&&i())}if((n=document.querySelectorAll('script[src="'+e+'"]'))&&n.length>0){if(T(i)){if(C(t))return b((function(){return void 0!==window[t]}),(function(){i()})),!1;i()}}else{var r=document.createElement("script");r.type="text/javascript",r.src=e,r.onload=function(){if(T(i)){if(C(t))return b((function(){return void 0!==window[t]}),(function(){i()})),!1;i()}},document.body.appendChild(r)}}}function w(){return"navigator"in window&&window.navigator.userAgent.match(/(iPad)|(iPhone)|(iPod)|(Android)|(PlayBook)|(BB10)|(BlackBerry)|(Opera Mini)|(IEMobile)|(webOS)|(MeeGo)/i)}function T(e){return"function"==typeof e}function C(e){return"string"==typeof e}function k(e){return!(!e||!e.nodeType||1!=e.nodeType)}function E(e){return Array.isArray(e)}function A(e){return e&&e.length&&isFinite(e.length)}function L(t){return"object"===e(t)&&null!=t&&!T(t)&&!E(t)}function I(e){return null==e}function O(e,t){return null!==e&&hasOwnProperty.call(e,t)}function P(e){if(L(e)){if(e.keys)return e.keys().length;var t=0;for(var i in e)O(e,i)&&t++;return t}return e.length}function M(e){return!isNaN(parseFloat(e))&&isFinite(e)}function z(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:-1,t=document.querySelectorAll(".gbtn[data-taborder]:not(.disabled)");if(!t.length)return!1;if(1==t.length)return t[0];"string"==typeof e&&(e=parseInt(e));var i=[];o(t,(function(e){i.push(e.getAttribute("data-taborder"))}));var n=Math.max.apply(Math,i.map((function(e){return parseInt(e)}))),s=e<0?1:e+1;s>n&&(s="1");var l=i.filter((function(e){return e>=parseInt(s)})),r=l.sort()[0];return document.querySelector('.gbtn[data-taborder="'.concat(r,'"]'))}function X(e){if(e.events.hasOwnProperty("keyboard"))return!1;e.events.keyboard=a("keydown",{onElement:window,withCallback:function(t,i){var n=(t=t||window.event).keyCode;if(9==n){var s=document.querySelector(".gbtn.focused");if(!s){var l=!(!document.activeElement||!document.activeElement.nodeName)&&document.activeElement.nodeName.toLocaleLowerCase();if("input"==l||"textarea"==l||"button"==l)return}t.preventDefault();var o=document.querySelectorAll(".gbtn[data-taborder]");if(!o||o.length<=0)return;if(!s){var r=z();return void(r&&(r.focus(),h(r,"focused")))}var a=z(s.getAttribute("data-taborder"));d(s,"focused"),a&&(a.focus(),h(a,"focused"))}39==n&&e.nextSlide(),37==n&&e.prevSlide(),27==n&&e.close()}})}function Y(e){return Math.sqrt(e.x*e.x+e.y*e.y)}function q(e,t){var i=function(e,t){var i=Y(e)*Y(t);if(0===i)return 0;var n=function(e,t){return e.x*t.x+e.y*t.y}(e,t)/i;return n>1&&(n=1),Math.acos(n)}(e,t);return function(e,t){return e.x*t.y-t.x*e.y}(e,t)>0&&(i*=-1),180*i/Math.PI}var N=function(){function e(i){t(this,e),this.handlers=[],this.el=i}return n(e,[{key:"add",value:function(e){this.handlers.push(e)}},{key:"del",value:function(e){e||(this.handlers=[]);for(var t=this.handlers.length;t>=0;t--)this.handlers[t]===e&&this.handlers.splice(t,1)}},{key:"dispatch",value:function(){for(var e=0,t=this.handlers.length;e=0)console.log("ignore drag for this touched element",e.target.nodeName.toLowerCase());else{this.now=Date.now(),this.x1=e.touches[0].pageX,this.y1=e.touches[0].pageY,this.delta=this.now-(this.last||this.now),this.touchStart.dispatch(e,this.element),null!==this.preTapPosition.x&&(this.isDoubleTap=this.delta>0&&this.delta<=250&&Math.abs(this.preTapPosition.x-this.x1)<30&&Math.abs(this.preTapPosition.y-this.y1)<30,this.isDoubleTap&&clearTimeout(this.singleTapTimeout)),this.preTapPosition.x=this.x1,this.preTapPosition.y=this.y1,this.last=this.now;var t=this.preV;if(e.touches.length>1){this._cancelLongTap(),this._cancelSingleTap();var i={x:e.touches[1].pageX-this.x1,y:e.touches[1].pageY-this.y1};t.x=i.x,t.y=i.y,this.pinchStartLen=Y(t),this.multipointStart.dispatch(e,this.element)}this._preventTap=!1,this.longTapTimeout=setTimeout(function(){this.longTap.dispatch(e,this.element),this._preventTap=!0}.bind(this),750)}}}},{key:"move",value:function(e){if(e.touches){var t=this.preV,i=e.touches.length,n=e.touches[0].pageX,s=e.touches[0].pageY;if(this.isDoubleTap=!1,i>1){var l=e.touches[1].pageX,o=e.touches[1].pageY,r={x:e.touches[1].pageX-n,y:e.touches[1].pageY-s};null!==t.x&&(this.pinchStartLen>0&&(e.zoom=Y(r)/this.pinchStartLen,this.pinch.dispatch(e,this.element)),e.angle=q(r,t),this.rotate.dispatch(e,this.element)),t.x=r.x,t.y=r.y,null!==this.x2&&null!==this.sx2?(e.deltaX=(n-this.x2+l-this.sx2)/2,e.deltaY=(s-this.y2+o-this.sy2)/2):(e.deltaX=0,e.deltaY=0),this.twoFingerPressMove.dispatch(e,this.element),this.sx2=l,this.sy2=o}else{if(null!==this.x2){e.deltaX=n-this.x2,e.deltaY=s-this.y2;var a=Math.abs(this.x1-this.x2),h=Math.abs(this.y1-this.y2);(a>10||h>10)&&(this._preventTap=!0)}else e.deltaX=0,e.deltaY=0;this.pressMove.dispatch(e,this.element)}this.touchMove.dispatch(e,this.element),this._cancelLongTap(),this.x2=n,this.y2=s,i>1&&e.preventDefault()}}},{key:"end",value:function(e){if(e.changedTouches){this._cancelLongTap();var t=this;e.touches.length<2&&(this.multipointEnd.dispatch(e,this.element),this.sx2=this.sy2=null),this.x2&&Math.abs(this.x1-this.x2)>30||this.y2&&Math.abs(this.y1-this.y2)>30?(e.direction=this._swipeDirection(this.x1,this.x2,this.y1,this.y2),this.swipeTimeout=setTimeout((function(){t.swipe.dispatch(e,t.element)}),0)):(this.tapTimeout=setTimeout((function(){t._preventTap||t.tap.dispatch(e,t.element),t.isDoubleTap&&(t.doubleTap.dispatch(e,t.element),t.isDoubleTap=!1)}),0),t.isDoubleTap||(t.singleTapTimeout=setTimeout((function(){t.singleTap.dispatch(e,t.element)}),250))),this.touchEnd.dispatch(e,this.element),this.preV.x=0,this.preV.y=0,this.zoom=1,this.pinchStartLen=null,this.x1=this.x2=this.y1=this.y2=null}}},{key:"cancelAll",value:function(){this._preventTap=!0,clearTimeout(this.singleTapTimeout),clearTimeout(this.tapTimeout),clearTimeout(this.longTapTimeout),clearTimeout(this.swipeTimeout)}},{key:"cancel",value:function(e){this.cancelAll(),this.touchCancel.dispatch(e,this.element)}},{key:"_cancelLongTap",value:function(){clearTimeout(this.longTapTimeout)}},{key:"_cancelSingleTap",value:function(){clearTimeout(this.singleTapTimeout)}},{key:"_swipeDirection",value:function(e,t,i,n){return Math.abs(e-t)>=Math.abs(i-n)?e-t>0?"Left":"Right":i-n>0?"Up":"Down"}},{key:"on",value:function(e,t){this[e]&&this[e].add(t)}},{key:"off",value:function(e,t){this[e]&&this[e].del(t)}},{key:"destroy",value:function(){return this.singleTapTimeout&&clearTimeout(this.singleTapTimeout),this.tapTimeout&&clearTimeout(this.tapTimeout),this.longTapTimeout&&clearTimeout(this.longTapTimeout),this.swipeTimeout&&clearTimeout(this.swipeTimeout),this.element.removeEventListener("touchstart",this.start),this.element.removeEventListener("touchmove",this.move),this.element.removeEventListener("touchend",this.end),this.element.removeEventListener("touchcancel",this.cancel),this.rotate.del(),this.touchStart.del(),this.multipointStart.del(),this.multipointEnd.del(),this.pinch.del(),this.swipe.del(),this.tap.del(),this.doubleTap.del(),this.longTap.del(),this.singleTap.del(),this.pressMove.del(),this.twoFingerPressMove.del(),this.touchMove.del(),this.touchEnd.del(),this.touchCancel.del(),this.preV=this.pinchStartLen=this.zoom=this.isDoubleTap=this.delta=this.last=this.now=this.tapTimeout=this.singleTapTimeout=this.longTapTimeout=this.swipeTimeout=this.x1=this.x2=this.y1=this.y2=this.preTapPosition=this.rotate=this.touchStart=this.multipointStart=this.multipointEnd=this.pinch=this.swipe=this.tap=this.doubleTap=this.longTap=this.singleTap=this.pressMove=this.touchMove=this.touchEnd=this.touchCancel=this.twoFingerPressMove=null,window.removeEventListener("scroll",this._cancelAllHandler),null}}]),e}();function W(e){var t=function(){var e,t=document.createElement("fakeelement"),i={transition:"transitionend",OTransition:"oTransitionEnd",MozTransition:"transitionend",WebkitTransition:"webkitTransitionEnd"};for(e in i)if(void 0!==t.style[e])return i[e]}(),i=window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth,n=c(e,"gslide-media")?e:e.querySelector(".gslide-media"),s=u(n,".ginner-container"),l=e.querySelector(".gslide-description");i>769&&(n=s),h(n,"greset"),v(n,"translate3d(0, 0, 0)"),a(t,{onElement:n,once:!0,withCallback:function(e,t){d(n,"greset")}}),n.style.opacity="",l&&(l.style.opacity="")}function B(e){if(e.events.hasOwnProperty("touch"))return!1;var t,i,n,s=y(),l=s.width,o=s.height,r=!1,a=null,g=null,f=null,p=!1,m=1,x=1,b=!1,S=!1,w=null,T=null,C=null,k=null,E=0,A=0,L=!1,I=!1,O={},P={},M=0,z=0,X=document.getElementById("glightbox-slider"),Y=document.querySelector(".goverlay"),q=new _(X,{touchStart:function(t){if(r=!0,(c(t.targetTouches[0].target,"ginner-container")||u(t.targetTouches[0].target,".gslide-desc")||"a"==t.targetTouches[0].target.nodeName.toLowerCase())&&(r=!1),u(t.targetTouches[0].target,".gslide-inline")&&!c(t.targetTouches[0].target.parentNode,"gslide-inline")&&(r=!1),r){if(P=t.targetTouches[0],O.pageX=t.targetTouches[0].pageX,O.pageY=t.targetTouches[0].pageY,M=t.targetTouches[0].clientX,z=t.targetTouches[0].clientY,a=e.activeSlide,g=a.querySelector(".gslide-media"),n=a.querySelector(".gslide-inline"),f=null,c(g,"gslide-image")&&(f=g.querySelector("img")),(window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth)>769&&(g=a.querySelector(".ginner-container")),d(Y,"greset"),t.pageX>20&&t.pageXo){var a=O.pageX-P.pageX;if(Math.abs(a)<=13)return!1}p=!0;var h,d=s.targetTouches[0].clientX,c=s.targetTouches[0].clientY,u=M-d,m=z-c;if(Math.abs(u)>Math.abs(m)?(L=!1,I=!0):(I=!1,L=!0),t=P.pageX-O.pageX,E=100*t/l,i=P.pageY-O.pageY,A=100*i/o,L&&f&&(h=1-Math.abs(i)/o,Y.style.opacity=h,e.settings.touchFollowAxis&&(E=0)),I&&(h=1-Math.abs(t)/l,g.style.opacity=h,e.settings.touchFollowAxis&&(A=0)),!f)return v(g,"translate3d(".concat(E,"%, 0, 0)"));v(g,"translate3d(".concat(E,"%, ").concat(A,"%, 0)"))}},touchEnd:function(){if(r){if(p=!1,S||b)return C=w,void(k=T);var t=Math.abs(parseInt(A)),i=Math.abs(parseInt(E));if(!(t>29&&f))return t<29&&i<25?(h(Y,"greset"),Y.style.opacity=1,W(g)):void 0;e.close()}},multipointEnd:function(){setTimeout((function(){b=!1}),50)},multipointStart:function(){b=!0,m=x||1},pinch:function(e){if(!f||p)return!1;b=!0,f.scaleX=f.scaleY=m*e.zoom;var t=m*e.zoom;if(S=!0,t<=1)return S=!1,t=1,k=null,C=null,w=null,T=null,void f.setAttribute("style","");t>4.5&&(t=4.5),f.style.transform="scale3d(".concat(t,", ").concat(t,", 1)"),x=t},pressMove:function(e){if(S&&!b){var t=P.pageX-O.pageX,i=P.pageY-O.pageY;C&&(t+=C),k&&(i+=k),w=t,T=i;var n="translate3d(".concat(t,"px, ").concat(i,"px, 0)");x&&(n+=" scale3d(".concat(x,", ").concat(x,", 1)")),v(f,n)}},swipe:function(t){if(!S)if(b)b=!1;else{if("Left"==t.direction){if(e.index==e.elements.length-1)return W(g);e.nextSlide()}if("Right"==t.direction){if(0==e.index)return W(g);e.prevSlide()}}}});e.events.touch=q}var H=function(){function e(i,n){var s=this,l=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;if(t(this,e),this.img=i,this.slide=n,this.onclose=l,this.img.setZoomEvents)return!1;this.active=!1,this.zoomedIn=!1,this.dragging=!1,this.currentX=null,this.currentY=null,this.initialX=null,this.initialY=null,this.xOffset=0,this.yOffset=0,this.img.addEventListener("mousedown",(function(e){return s.dragStart(e)}),!1),this.img.addEventListener("mouseup",(function(e){return s.dragEnd(e)}),!1),this.img.addEventListener("mousemove",(function(e){return s.drag(e)}),!1),this.img.addEventListener("click",(function(e){return s.slide.classList.contains("dragging-nav")?(s.zoomOut(),!1):s.zoomedIn?void(s.zoomedIn&&!s.dragging&&s.zoomOut()):s.zoomIn()}),!1),this.img.setZoomEvents=!0}return n(e,[{key:"zoomIn",value:function(){var e=this.widowWidth();if(!(this.zoomedIn||e<=768)){var t=this.img;if(t.setAttribute("data-style",t.getAttribute("style")),t.style.maxWidth=t.naturalWidth+"px",t.style.maxHeight=t.naturalHeight+"px",t.naturalWidth>e){var i=e/2-t.naturalWidth/2;this.setTranslate(this.img.parentNode,i,0)}this.slide.classList.add("zoomed"),this.zoomedIn=!0}}},{key:"zoomOut",value:function(){this.img.parentNode.setAttribute("style",""),this.img.setAttribute("style",this.img.getAttribute("data-style")),this.slide.classList.remove("zoomed"),this.zoomedIn=!1,this.currentX=null,this.currentY=null,this.initialX=null,this.initialY=null,this.xOffset=0,this.yOffset=0,this.onclose&&"function"==typeof this.onclose&&this.onclose()}},{key:"dragStart",value:function(e){e.preventDefault(),this.zoomedIn?("touchstart"===e.type?(this.initialX=e.touches[0].clientX-this.xOffset,this.initialY=e.touches[0].clientY-this.yOffset):(this.initialX=e.clientX-this.xOffset,this.initialY=e.clientY-this.yOffset),e.target===this.img&&(this.active=!0,this.img.classList.add("dragging"))):this.active=!1}},{key:"dragEnd",value:function(e){var t=this;e.preventDefault(),this.initialX=this.currentX,this.initialY=this.currentY,this.active=!1,setTimeout((function(){t.dragging=!1,t.img.isDragging=!1,t.img.classList.remove("dragging")}),100)}},{key:"drag",value:function(e){this.active&&(e.preventDefault(),"touchmove"===e.type?(this.currentX=e.touches[0].clientX-this.initialX,this.currentY=e.touches[0].clientY-this.initialY):(this.currentX=e.clientX-this.initialX,this.currentY=e.clientY-this.initialY),this.xOffset=this.currentX,this.yOffset=this.currentY,this.img.isDragging=!0,this.dragging=!0,this.setTranslate(this.img,this.currentX,this.currentY))}},{key:"onMove",value:function(e){if(this.zoomedIn){var t=e.clientX-this.img.naturalWidth/2,i=e.clientY-this.img.naturalHeight/2;this.setTranslate(this.img,t,i)}}},{key:"setTranslate",value:function(e,t,i){e.style.transform="translate3d("+t+"px, "+i+"px, 0)"}},{key:"widowWidth",value:function(){return window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth}}]),e}(),V=function(){function e(){var i=this,n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,e);var s=n.dragEl,l=n.toleranceX,o=void 0===l?40:l,r=n.toleranceY,a=void 0===r?65:r,h=n.slide,d=void 0===h?null:h,c=n.instance,u=void 0===c?null:c;this.el=s,this.active=!1,this.dragging=!1,this.currentX=null,this.currentY=null,this.initialX=null,this.initialY=null,this.xOffset=0,this.yOffset=0,this.direction=null,this.lastDirection=null,this.toleranceX=o,this.toleranceY=a,this.toleranceReached=!1,this.dragContainer=this.el,this.slide=d,this.instance=u,this.el.addEventListener("mousedown",(function(e){return i.dragStart(e)}),!1),this.el.addEventListener("mouseup",(function(e){return i.dragEnd(e)}),!1),this.el.addEventListener("mousemove",(function(e){return i.drag(e)}),!1)}return n(e,[{key:"dragStart",value:function(e){if(this.slide.classList.contains("zoomed"))this.active=!1;else{"touchstart"===e.type?(this.initialX=e.touches[0].clientX-this.xOffset,this.initialY=e.touches[0].clientY-this.yOffset):(this.initialX=e.clientX-this.xOffset,this.initialY=e.clientY-this.yOffset);var t=e.target.nodeName.toLowerCase();e.target.classList.contains("nodrag")||u(e.target,".nodrag")||-1!==["input","select","textarea","button","a"].indexOf(t)?this.active=!1:(e.preventDefault(),(e.target===this.el||"img"!==t&&u(e.target,".gslide-inline"))&&(this.active=!0,this.el.classList.add("dragging"),this.dragContainer=u(e.target,".ginner-container")))}}},{key:"dragEnd",value:function(e){var t=this;e&&e.preventDefault(),this.initialX=0,this.initialY=0,this.currentX=null,this.currentY=null,this.initialX=null,this.initialY=null,this.xOffset=0,this.yOffset=0,this.active=!1,this.doSlideChange&&(this.instance.preventOutsideClick=!0,"right"==this.doSlideChange&&this.instance.prevSlide(),"left"==this.doSlideChange&&this.instance.nextSlide()),this.doSlideClose&&this.instance.close(),this.toleranceReached||this.setTranslate(this.dragContainer,0,0,!0),setTimeout((function(){t.instance.preventOutsideClick=!1,t.toleranceReached=!1,t.lastDirection=null,t.dragging=!1,t.el.isDragging=!1,t.el.classList.remove("dragging"),t.slide.classList.remove("dragging-nav"),t.dragContainer.style.transform="",t.dragContainer.style.transition=""}),100)}},{key:"drag",value:function(e){if(this.active){e.preventDefault(),this.slide.classList.add("dragging-nav"),"touchmove"===e.type?(this.currentX=e.touches[0].clientX-this.initialX,this.currentY=e.touches[0].clientY-this.initialY):(this.currentX=e.clientX-this.initialX,this.currentY=e.clientY-this.initialY),this.xOffset=this.currentX,this.yOffset=this.currentY,this.el.isDragging=!0,this.dragging=!0,this.doSlideChange=!1,this.doSlideClose=!1;var t=Math.abs(this.currentX),i=Math.abs(this.currentY);if(t>0&&t>=Math.abs(this.currentY)&&(!this.lastDirection||"x"==this.lastDirection)){this.yOffset=0,this.lastDirection="x",this.setTranslate(this.dragContainer,this.currentX,0);var n=this.shouldChange();if(!this.instance.settings.dragAutoSnap&&n&&(this.doSlideChange=n),this.instance.settings.dragAutoSnap&&n)return this.instance.preventOutsideClick=!0,this.toleranceReached=!0,this.active=!1,this.instance.preventOutsideClick=!0,this.dragEnd(null),"right"==n&&this.instance.prevSlide(),void("left"==n&&this.instance.nextSlide())}if(this.toleranceY>0&&i>0&&i>=t&&(!this.lastDirection||"y"==this.lastDirection)){this.xOffset=0,this.lastDirection="y",this.setTranslate(this.dragContainer,0,this.currentY);var s=this.shouldClose();return!this.instance.settings.dragAutoSnap&&s&&(this.doSlideClose=!0),void(this.instance.settings.dragAutoSnap&&s&&this.instance.close())}}}},{key:"shouldChange",value:function(){var e=!1;if(Math.abs(this.currentX)>=this.toleranceX){var t=this.currentX>0?"right":"left";("left"==t&&this.slide!==this.slide.parentNode.lastChild||"right"==t&&this.slide!==this.slide.parentNode.firstChild)&&(e=t)}return e}},{key:"shouldClose",value:function(){var e=!1;return Math.abs(this.currentY)>=this.toleranceY&&(e=!0),e}},{key:"setTranslate",value:function(e,t,i){var n=arguments.length>3&&void 0!==arguments[3]&&arguments[3];e.style.transition=n?"all .2s ease":"",e.style.transform="translate3d(".concat(t,"px, ").concat(i,"px, 0)")}}]),e}();function j(e,t,i,n){var s=e.querySelector(".gslide-media"),l=new Image,o="gSlideTitle_"+i,r="gSlideDesc_"+i;l.addEventListener("load",(function(){T(n)&&n()}),!1),l.src=t.href,""!=t.sizes&&""!=t.srcset&&(l.sizes=t.sizes,l.srcset=t.srcset),l.alt="",I(t.alt)||""===t.alt||(l.alt=t.alt),""!==t.title&&l.setAttribute("aria-labelledby",o),""!==t.description&&l.setAttribute("aria-describedby",r),t.hasOwnProperty("_hasCustomWidth")&&t._hasCustomWidth&&(l.style.width=t.width),t.hasOwnProperty("_hasCustomHeight")&&t._hasCustomHeight&&(l.style.height=t.height),s.insertBefore(l,s.firstChild)}function F(e,t,i,n){var s=this,l=e.querySelector(".ginner-container"),o="gvideo"+i,r=e.querySelector(".gslide-media"),a=this.getAllPlayers();h(l,"gvideo-container"),r.insertBefore(m('
        '),r.firstChild);var d=e.querySelector(".gvideo-wrapper");S(this.settings.plyr.css,"Plyr");var c=t.href,u=location.protocol.replace(":",""),g="",v="",f=!1;"file"==u&&(u="http"),r.style.maxWidth=t.width,S(this.settings.plyr.js,"Plyr",(function(){if(c.match(/vimeo\.com\/([0-9]*)/)){var l=/vimeo.*\/(\d+)/i.exec(c);g="vimeo",v=l[1]}if(c.match(/(youtube\.com|youtube-nocookie\.com)\/watch\?v=([a-zA-Z0-9\-_]+)/)||c.match(/youtu\.be\/([a-zA-Z0-9\-_]+)/)||c.match(/(youtube\.com|youtube-nocookie\.com)\/embed\/([a-zA-Z0-9\-_]+)/)){var r=function(e){var t="";t=void 0!==(e=e.replace(/(>|<)/gi,"").split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/))[2]?(t=e[2].split(/[^0-9a-z_\-]/i))[0]:e;return t}(c);g="youtube",v=r}if(null!==c.match(/\.(mp4|ogg|webm|mov)$/)){g="local";var u='")}var w=f||m('
        '));h(d,"".concat(g,"-video gvideo")),d.appendChild(w),d.setAttribute("data-id",o),d.setAttribute("data-index",i);var C=O(s.settings.plyr,"config")?s.settings.plyr.config:{},k=new Plyr("#"+o,C);k.on("ready",(function(e){var t=e.detail.plyr;a[o]=t,T(n)&&n()})),b((function(){return e.querySelector("iframe")&&"true"==e.querySelector("iframe").dataset.ready}),(function(){s.resize(e)})),k.on("enterfullscreen",R),k.on("exitfullscreen",R)}))}function R(e){var t=u(e.target,".gslide-media");"enterfullscreen"==e.type&&h(t,"fullscreen"),"exitfullscreen"==e.type&&d(t,"fullscreen")}function G(e,t,i,n){var s,l=this,o=e.querySelector(".gslide-media"),r=!(!O(t,"href")||!t.href)&&t.href.split("#").pop().trim(),d=!(!O(t,"content")||!t.content)&&t.content;if(d&&(C(d)&&(s=m('
        '.concat(d,"
        "))),k(d))){"none"==d.style.display&&(d.style.display="block");var c=document.createElement("div");c.className="ginlined-content",c.appendChild(d),s=c}if(r){var u=document.getElementById(r);if(!u)return!1;var g=u.cloneNode(!0);g.style.height=t.height,g.style.maxWidth=t.width,h(g,"ginlined-content"),s=g}if(!s)return console.error("Unable to append inline slide content",t),!1;o.style.height=t.height,o.style.width=t.width,o.appendChild(s),this.events["inlineclose"+r]=a("click",{onElement:o.querySelectorAll(".gtrigger-close"),withCallback:function(e){e.preventDefault(),l.close()}}),T(n)&&n()}function Z(e,t,i,n){var s=e.querySelector(".gslide-media"),l=function(e){var t=e.url,i=e.allow,n=e.callback,s=e.appendTo,l=document.createElement("iframe");return l.className="vimeo-video gvideo",l.src=t,l.style.width="100%",l.style.height="100%",i&&l.setAttribute("allow",i),l.onload=function(){h(l,"node-ready"),T(n)&&n()},s&&s.appendChild(l),l}({url:t.href,callback:n});s.parentNode.style.maxWidth=t.width,s.parentNode.style.height=t.height,s.appendChild(l)}var $=function(){function e(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,e),this.defaults={href:"",sizes:"",srcset:"",title:"",type:"",description:"",alt:"",descPosition:"bottom",effect:"",width:"",height:"",content:!1,zoomable:!0,draggable:!0},L(i)&&(this.defaults=l(this.defaults,i))}return n(e,[{key:"sourceType",value:function(e){var t=e;if(null!==(e=e.toLowerCase()).match(/\.(jpeg|jpg|jpe|gif|png|apn|webp|avif|svg)/))return"image";if(e.match(/(youtube\.com|youtube-nocookie\.com)\/watch\?v=([a-zA-Z0-9\-_]+)/)||e.match(/youtu\.be\/([a-zA-Z0-9\-_]+)/)||e.match(/(youtube\.com|youtube-nocookie\.com)\/embed\/([a-zA-Z0-9\-_]+)/))return"video";if(e.match(/vimeo\.com\/([0-9]*)/))return"video";if(null!==e.match(/\.(mp4|ogg|webm|mov)/))return"video";if(null!==e.match(/\.(mp3|wav|wma|aac|ogg)/))return"audio";if(e.indexOf("#")>-1&&""!==t.split("#").pop().trim())return"inline";return e.indexOf("goajax=true")>-1?"ajax":"external"}},{key:"parseConfig",value:function(e,t){var i=this,n=l({descPosition:t.descPosition},this.defaults);if(L(e)&&!k(e)){O(e,"type")||(O(e,"content")&&e.content?e.type="inline":O(e,"href")&&(e.type=this.sourceType(e.href)));var s=l(n,e);return this.setSize(s,t),s}var r="",a=e.getAttribute("data-glightbox"),h=e.nodeName.toLowerCase();if("a"===h&&(r=e.href),"img"===h&&(r=e.src,n.alt=e.alt),n.href=r,o(n,(function(s,l){O(t,l)&&"width"!==l&&(n[l]=t[l]);var o=e.dataset[l];I(o)||(n[l]=i.sanitizeValue(o))})),n.content&&(n.type="inline"),!n.type&&r&&(n.type=this.sourceType(r)),I(a)){if(!n.title&&"a"==h){var d=e.title;I(d)||""===d||(n.title=d)}if(!n.title&&"img"==h){var c=e.alt;I(c)||""===c||(n.title=c)}}else{var u=[];o(n,(function(e,t){u.push(";\\s?"+t)})),u=u.join("\\s?:|"),""!==a.trim()&&o(n,(function(e,t){var s=a,l=new RegExp("s?"+t+"s?:s?(.*?)("+u+"s?:|$)"),o=s.match(l);if(o&&o.length&&o[1]){var r=o[1].trim().replace(/;\s*$/,"");n[t]=i.sanitizeValue(r)}}))}if(n.description&&"."===n.description.substring(0,1)){var g;try{g=document.querySelector(n.description).innerHTML}catch(e){if(!(e instanceof DOMException))throw e}g&&(n.description=g)}if(!n.description){var v=e.querySelector(".glightbox-desc");v&&(n.description=v.innerHTML)}return this.setSize(n,t,e),this.slideConfig=n,n}},{key:"setSize",value:function(e,t){var i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null,n="video"==e.type?this.checkSize(t.videosWidth):this.checkSize(t.width),s=this.checkSize(t.height);return e.width=O(e,"width")&&""!==e.width?this.checkSize(e.width):n,e.height=O(e,"height")&&""!==e.height?this.checkSize(e.height):s,i&&"image"==e.type&&(e._hasCustomWidth=!!i.dataset.width,e._hasCustomHeight=!!i.dataset.height),e}},{key:"checkSize",value:function(e){return M(e)?"".concat(e,"px"):e}},{key:"sanitizeValue",value:function(e){return"true"!==e&&"false"!==e?e:"true"===e}}]),e}(),U=function(){function e(i,n,s){t(this,e),this.element=i,this.instance=n,this.index=s}return n(e,[{key:"setContent",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,i=arguments.length>1&&void 0!==arguments[1]&&arguments[1];if(c(t,"loaded"))return!1;var n=this.instance.settings,s=this.slideConfig,l=w();T(n.beforeSlideLoad)&&n.beforeSlideLoad({index:this.index,slide:t,player:!1});var o=s.type,r=s.descPosition,a=t.querySelector(".gslide-media"),d=t.querySelector(".gslide-title"),u=t.querySelector(".gslide-desc"),g=t.querySelector(".gdesc-inner"),v=i,f="gSlideTitle_"+this.index,p="gSlideDesc_"+this.index;if(T(n.afterSlideLoad)&&(v=function(){T(i)&&i(),n.afterSlideLoad({index:e.index,slide:t,player:e.instance.getSlidePlayerInstance(e.index)})}),""==s.title&&""==s.description?g&&g.parentNode.parentNode.removeChild(g.parentNode):(d&&""!==s.title?(d.id=f,d.innerHTML=s.title):d.parentNode.removeChild(d),u&&""!==s.description?(u.id=p,l&&n.moreLength>0?(s.smallDescription=this.slideShortDesc(s.description,n.moreLength,n.moreText),u.innerHTML=s.smallDescription,this.descriptionEvents(u,s)):u.innerHTML=s.description):u.parentNode.removeChild(u),h(a.parentNode,"desc-".concat(r)),h(g.parentNode,"description-".concat(r))),h(a,"gslide-".concat(o)),h(t,"loaded"),"video"!==o){if("external"!==o)return"inline"===o?(G.apply(this.instance,[t,s,this.index,v]),void(s.draggable&&new V({dragEl:t.querySelector(".gslide-inline"),toleranceX:n.dragToleranceX,toleranceY:n.dragToleranceY,slide:t,instance:this.instance}))):void("image"!==o?T(v)&&v():j(t,s,this.index,(function(){var i=t.querySelector("img");s.draggable&&new V({dragEl:i,toleranceX:n.dragToleranceX,toleranceY:n.dragToleranceY,slide:t,instance:e.instance}),s.zoomable&&i.naturalWidth>i.offsetWidth&&(h(i,"zoomable"),new H(i,t,(function(){e.instance.resize()}))),T(v)&&v()})));Z.apply(this,[t,s,this.index,v])}else F.apply(this.instance,[t,s,this.index,v])}},{key:"slideShortDesc",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:50,i=arguments.length>2&&void 0!==arguments[2]&&arguments[2],n=document.createElement("div");n.innerHTML=e;var s=n.innerText,l=i;if((e=s.trim()).length<=t)return e;var o=e.substr(0,t-1);return l?(n=null,o+'... '+i+""):o}},{key:"descriptionEvents",value:function(e,t){var i=this,n=e.querySelector(".desc-more");if(!n)return!1;a("click",{onElement:n,withCallback:function(e,n){e.preventDefault();var s=document.body,l=u(n,".gslide-desc");if(!l)return!1;l.innerHTML=t.description,h(s,"gdesc-open");var o=a("click",{onElement:[s,u(l,".gslide-description")],withCallback:function(e,n){"a"!==e.target.nodeName.toLowerCase()&&(d(s,"gdesc-open"),h(s,"gdesc-closed"),l.innerHTML=t.smallDescription,i.descriptionEvents(l,t),setTimeout((function(){d(s,"gdesc-closed")}),400),o.destroy())}})}})}},{key:"create",value:function(){return m(this.instance.settings.slideHTML)}},{key:"getConfig",value:function(){k(this.element)||this.element.hasOwnProperty("draggable")||(this.element.draggable=this.instance.settings.draggable);var e=new $(this.instance.settings.slideExtraAttributes);return this.slideConfig=e.parseConfig(this.element,this.instance.settings),this.slideConfig}}]),e}(),J=w(),K=null!==w()||void 0!==document.createTouch||"ontouchstart"in window||"onmsgesturechange"in window||navigator.msMaxTouchPoints,Q=document.getElementsByTagName("html")[0],ee={selector:".glightbox",elements:null,skin:"clean",theme:"clean",closeButton:!0,startAt:null,autoplayVideos:!0,autofocusVideos:!0,descPosition:"bottom",width:"900px",height:"506px",videosWidth:"960px",beforeSlideChange:null,afterSlideChange:null,beforeSlideLoad:null,afterSlideLoad:null,slideInserted:null,slideRemoved:null,slideExtraAttributes:null,onOpen:null,onClose:null,loop:!1,zoomable:!0,draggable:!0,dragAutoSnap:!1,dragToleranceX:40,dragToleranceY:65,preload:!0,oneSlidePerOpen:!1,touchNavigation:!0,touchFollowAxis:!0,keyboardNavigation:!0,closeOnOutsideClick:!0,plugins:!1,plyr:{css:"https://cdn.plyr.io/3.6.8/plyr.css",js:"https://cdn.plyr.io/3.6.8/plyr.js",config:{ratio:"16:9",fullscreen:{enabled:!0,iosNative:!0},youtube:{noCookie:!0,rel:0,showinfo:0,iv_load_policy:3},vimeo:{byline:!1,portrait:!1,title:!1,transparent:!1}}},openEffect:"zoom",closeEffect:"zoom",slideEffect:"slide",moreText:"See more",moreLength:60,cssEfects:{fade:{in:"fadeIn",out:"fadeOut"},zoom:{in:"zoomIn",out:"zoomOut"},slide:{in:"slideInRight",out:"slideOutLeft"},slideBack:{in:"slideInLeft",out:"slideOutRight"},none:{in:"none",out:"none"}},svg:{close:'',next:' ',prev:''},slideHTML:'
        \n
        \n
        \n
        \n
        \n
        \n
        \n

        \n
        \n
        \n
        \n
        \n
        \n
        ',lightboxHTML:''},te=function(){function e(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,e),this.customOptions=i,this.settings=l(ee,i),this.effectsClasses=this.getAnimationClasses(),this.videoPlayers={},this.apiEvents=[],this.fullElementsList=!1}return n(e,[{key:"init",value:function(){var e=this,t=this.getSelector();t&&(this.baseEvents=a("click",{onElement:t,withCallback:function(t,i){t.preventDefault(),e.open(i)}})),this.elements=this.getElements()}},{key:"open",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;if(0==this.elements.length)return!1;this.activeSlide=null,this.prevActiveSlideIndex=null,this.prevActiveSlide=null;var i=M(t)?t:this.settings.startAt;if(k(e)){var n=e.getAttribute("data-gallery");n&&(this.fullElementsList=this.elements,this.elements=this.getGalleryElements(this.elements,n)),I(i)&&(i=this.getElementIndex(e))<0&&(i=0)}M(i)||(i=0),this.build(),g(this.overlay,"none"==this.settings.openEffect?"none":this.settings.cssEfects.fade.in);var s=document.body,l=window.innerWidth-document.documentElement.clientWidth;if(l>0){var o=document.createElement("style");o.type="text/css",o.className="gcss-styles",o.innerText=".gscrollbar-fixer {margin-right: ".concat(l,"px}"),document.head.appendChild(o),h(s,"gscrollbar-fixer")}h(s,"glightbox-open"),h(Q,"glightbox-open"),J&&(h(document.body,"glightbox-mobile"),this.settings.slideEffect="slide"),this.showSlide(i,!0),1==this.elements.length?(h(this.prevButton,"glightbox-button-hidden"),h(this.nextButton,"glightbox-button-hidden")):(d(this.prevButton,"glightbox-button-hidden"),d(this.nextButton,"glightbox-button-hidden")),this.lightboxOpen=!0,this.trigger("open"),T(this.settings.onOpen)&&this.settings.onOpen(),K&&this.settings.touchNavigation&&B(this),this.settings.keyboardNavigation&&X(this)}},{key:"openAt",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;this.open(null,e)}},{key:"showSlide",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,i=arguments.length>1&&void 0!==arguments[1]&&arguments[1];f(this.loader),this.index=parseInt(t);var n=this.slidesContainer.querySelector(".current");n&&d(n,"current"),this.slideAnimateOut();var s=this.slidesContainer.querySelectorAll(".gslide")[t];if(c(s,"loaded"))this.slideAnimateIn(s,i),p(this.loader);else{f(this.loader);var l=this.elements[t],o={index:this.index,slide:s,slideNode:s,slideConfig:l.slideConfig,slideIndex:this.index,trigger:l.node,player:null};this.trigger("slide_before_load",o),l.instance.setContent(s,(function(){p(e.loader),e.resize(),e.slideAnimateIn(s,i),e.trigger("slide_after_load",o)}))}this.slideDescription=s.querySelector(".gslide-description"),this.slideDescriptionContained=this.slideDescription&&c(this.slideDescription.parentNode,"gslide-media"),this.settings.preload&&(this.preloadSlide(t+1),this.preloadSlide(t-1)),this.updateNavigationClasses(),this.activeSlide=s}},{key:"preloadSlide",value:function(e){var t=this;if(e<0||e>this.elements.length-1)return!1;if(I(this.elements[e]))return!1;var i=this.slidesContainer.querySelectorAll(".gslide")[e];if(c(i,"loaded"))return!1;var n=this.elements[e],s=n.type,l={index:e,slide:i,slideNode:i,slideConfig:n.slideConfig,slideIndex:e,trigger:n.node,player:null};this.trigger("slide_before_load",l),"video"==s||"external"==s?setTimeout((function(){n.instance.setContent(i,(function(){t.trigger("slide_after_load",l)}))}),200):n.instance.setContent(i,(function(){t.trigger("slide_after_load",l)}))}},{key:"prevSlide",value:function(){this.goToSlide(this.index-1)}},{key:"nextSlide",value:function(){this.goToSlide(this.index+1)}},{key:"goToSlide",value:function(){var e=arguments.length>0&&void 0!==arguments[0]&&arguments[0];if(this.prevActiveSlide=this.activeSlide,this.prevActiveSlideIndex=this.index,!this.loop()&&(e<0||e>this.elements.length-1))return!1;e<0?e=this.elements.length-1:e>=this.elements.length&&(e=0),this.showSlide(e)}},{key:"insertSlide",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:-1;t<0&&(t=this.elements.length);var i=new U(e,this,t),n=i.getConfig(),s=l({},n),o=i.create(),r=this.elements.length-1;s.index=t,s.node=!1,s.instance=i,s.slideConfig=n,this.elements.splice(t,0,s);var a=null,h=null;if(this.slidesContainer){if(t>r)this.slidesContainer.appendChild(o);else{var d=this.slidesContainer.querySelectorAll(".gslide")[t];this.slidesContainer.insertBefore(o,d)}(this.settings.preload&&0==this.index&&0==t||this.index-1==t||this.index+1==t)&&this.preloadSlide(t),0==this.index&&0==t&&(this.index=1),this.updateNavigationClasses(),a=this.slidesContainer.querySelectorAll(".gslide")[t],h=this.getSlidePlayerInstance(t),s.slideNode=a}this.trigger("slide_inserted",{index:t,slide:a,slideNode:a,slideConfig:n,slideIndex:t,trigger:null,player:h}),T(this.settings.slideInserted)&&this.settings.slideInserted({index:t,slide:a,player:h})}},{key:"removeSlide",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:-1;if(e<0||e>this.elements.length-1)return!1;var t=this.slidesContainer&&this.slidesContainer.querySelectorAll(".gslide")[e];t&&(this.getActiveSlideIndex()==e&&(e==this.elements.length-1?this.prevSlide():this.nextSlide()),t.parentNode.removeChild(t)),this.elements.splice(e,1),this.trigger("slide_removed",e),T(this.settings.slideRemoved)&&this.settings.slideRemoved(e)}},{key:"slideAnimateIn",value:function(e,t){var i=this,n=e.querySelector(".gslide-media"),s=e.querySelector(".gslide-description"),l={index:this.prevActiveSlideIndex,slide:this.prevActiveSlide,slideNode:this.prevActiveSlide,slideIndex:this.prevActiveSlide,slideConfig:I(this.prevActiveSlideIndex)?null:this.elements[this.prevActiveSlideIndex].slideConfig,trigger:I(this.prevActiveSlideIndex)?null:this.elements[this.prevActiveSlideIndex].node,player:this.getSlidePlayerInstance(this.prevActiveSlideIndex)},o={index:this.index,slide:this.activeSlide,slideNode:this.activeSlide,slideConfig:this.elements[this.index].slideConfig,slideIndex:this.index,trigger:this.elements[this.index].node,player:this.getSlidePlayerInstance(this.index)};if(n.offsetWidth>0&&s&&(p(s),s.style.display=""),d(e,this.effectsClasses),t)g(e,this.settings.cssEfects[this.settings.openEffect].in,(function(){i.settings.autoplayVideos&&i.slidePlayerPlay(e),i.trigger("slide_changed",{prev:l,current:o}),T(i.settings.afterSlideChange)&&i.settings.afterSlideChange.apply(i,[l,o])}));else{var r=this.settings.slideEffect,a="none"!==r?this.settings.cssEfects[r].in:r;this.prevActiveSlideIndex>this.index&&"slide"==this.settings.slideEffect&&(a=this.settings.cssEfects.slideBack.in),g(e,a,(function(){i.settings.autoplayVideos&&i.slidePlayerPlay(e),i.trigger("slide_changed",{prev:l,current:o}),T(i.settings.afterSlideChange)&&i.settings.afterSlideChange.apply(i,[l,o])}))}setTimeout((function(){i.resize(e)}),100),h(e,"current")}},{key:"slideAnimateOut",value:function(){if(!this.prevActiveSlide)return!1;var e=this.prevActiveSlide;d(e,this.effectsClasses),h(e,"prev");var t=this.settings.slideEffect,i="none"!==t?this.settings.cssEfects[t].out:t;this.slidePlayerPause(e),this.trigger("slide_before_change",{prev:{index:this.prevActiveSlideIndex,slide:this.prevActiveSlide,slideNode:this.prevActiveSlide,slideIndex:this.prevActiveSlideIndex,slideConfig:I(this.prevActiveSlideIndex)?null:this.elements[this.prevActiveSlideIndex].slideConfig,trigger:I(this.prevActiveSlideIndex)?null:this.elements[this.prevActiveSlideIndex].node,player:this.getSlidePlayerInstance(this.prevActiveSlideIndex)},current:{index:this.index,slide:this.activeSlide,slideNode:this.activeSlide,slideIndex:this.index,slideConfig:this.elements[this.index].slideConfig,trigger:this.elements[this.index].node,player:this.getSlidePlayerInstance(this.index)}}),T(this.settings.beforeSlideChange)&&this.settings.beforeSlideChange.apply(this,[{index:this.prevActiveSlideIndex,slide:this.prevActiveSlide,player:this.getSlidePlayerInstance(this.prevActiveSlideIndex)},{index:this.index,slide:this.activeSlide,player:this.getSlidePlayerInstance(this.index)}]),this.prevActiveSlideIndex>this.index&&"slide"==this.settings.slideEffect&&(i=this.settings.cssEfects.slideBack.out),g(e,i,(function(){var t=e.querySelector(".ginner-container"),i=e.querySelector(".gslide-media"),n=e.querySelector(".gslide-description");t.style.transform="",i.style.transform="",d(i,"greset"),i.style.opacity="",n&&(n.style.opacity=""),d(e,"prev")}))}},{key:"getAllPlayers",value:function(){return this.videoPlayers}},{key:"getSlidePlayerInstance",value:function(e){var t="gvideo"+e,i=this.getAllPlayers();return!(!O(i,t)||!i[t])&&i[t]}},{key:"stopSlideVideo",value:function(e){if(k(e)){var t=e.querySelector(".gvideo-wrapper");t&&(e=t.getAttribute("data-index"))}console.log("stopSlideVideo is deprecated, use slidePlayerPause");var i=this.getSlidePlayerInstance(e);i&&i.playing&&i.pause()}},{key:"slidePlayerPause",value:function(e){if(k(e)){var t=e.querySelector(".gvideo-wrapper");t&&(e=t.getAttribute("data-index"))}var i=this.getSlidePlayerInstance(e);i&&i.playing&&i.pause()}},{key:"playSlideVideo",value:function(e){if(k(e)){var t=e.querySelector(".gvideo-wrapper");t&&(e=t.getAttribute("data-index"))}console.log("playSlideVideo is deprecated, use slidePlayerPlay");var i=this.getSlidePlayerInstance(e);i&&!i.playing&&i.play()}},{key:"slidePlayerPlay",value:function(e){if(k(e)){var t=e.querySelector(".gvideo-wrapper");t&&(e=t.getAttribute("data-index"))}var i=this.getSlidePlayerInstance(e);i&&!i.playing&&(i.play(),this.settings.autofocusVideos&&i.elements.container.focus())}},{key:"setElements",value:function(e){var t=this;this.settings.elements=!1;var i=[];e&&e.length&&o(e,(function(e,n){var s=new U(e,t,n),o=s.getConfig(),r=l({},o);r.slideConfig=o,r.instance=s,r.index=n,i.push(r)})),this.elements=i,this.lightboxOpen&&(this.slidesContainer.innerHTML="",this.elements.length&&(o(this.elements,(function(){var e=m(t.settings.slideHTML);t.slidesContainer.appendChild(e)})),this.showSlide(0,!0)))}},{key:"getElementIndex",value:function(e){var t=!1;return o(this.elements,(function(i,n){if(O(i,"node")&&i.node==e)return t=n,!0})),t}},{key:"getElements",value:function(){var e=this,t=[];this.elements=this.elements?this.elements:[],!I(this.settings.elements)&&E(this.settings.elements)&&this.settings.elements.length&&o(this.settings.elements,(function(i,n){var s=new U(i,e,n),o=s.getConfig(),r=l({},o);r.node=!1,r.index=n,r.instance=s,r.slideConfig=o,t.push(r)}));var i=!1;return this.getSelector()&&(i=document.querySelectorAll(this.getSelector())),i?(o(i,(function(i,n){var s=new U(i,e,n),o=s.getConfig(),r=l({},o);r.node=i,r.index=n,r.instance=s,r.slideConfig=o,r.gallery=i.getAttribute("data-gallery"),t.push(r)})),t):t}},{key:"getGalleryElements",value:function(e,t){return e.filter((function(e){return e.gallery==t}))}},{key:"getSelector",value:function(){return!this.settings.elements&&(this.settings.selector&&"data-"==this.settings.selector.substring(0,5)?"*[".concat(this.settings.selector,"]"):this.settings.selector)}},{key:"getActiveSlide",value:function(){return this.slidesContainer.querySelectorAll(".gslide")[this.index]}},{key:"getActiveSlideIndex",value:function(){return this.index}},{key:"getAnimationClasses",value:function(){var e=[];for(var t in this.settings.cssEfects)if(this.settings.cssEfects.hasOwnProperty(t)){var i=this.settings.cssEfects[t];e.push("g".concat(i.in)),e.push("g".concat(i.out))}return e.join(" ")}},{key:"build",value:function(){var e=this;if(this.built)return!1;var t=document.body.childNodes,i=[];o(t,(function(e){e.parentNode==document.body&&"#"!==e.nodeName.charAt(0)&&e.hasAttribute&&!e.hasAttribute("aria-hidden")&&(i.push(e),e.setAttribute("aria-hidden","true"))}));var n=O(this.settings.svg,"next")?this.settings.svg.next:"",s=O(this.settings.svg,"prev")?this.settings.svg.prev:"",l=O(this.settings.svg,"close")?this.settings.svg.close:"",r=this.settings.lightboxHTML;r=m(r=(r=(r=r.replace(/{nextSVG}/g,n)).replace(/{prevSVG}/g,s)).replace(/{closeSVG}/g,l)),document.body.appendChild(r);var d=document.getElementById("glightbox-body");this.modal=d;var g=d.querySelector(".gclose");this.prevButton=d.querySelector(".gprev"),this.nextButton=d.querySelector(".gnext"),this.overlay=d.querySelector(".goverlay"),this.loader=d.querySelector(".gloader"),this.slidesContainer=document.getElementById("glightbox-slider"),this.bodyHiddenChildElms=i,this.events={},h(this.modal,"glightbox-"+this.settings.skin),this.settings.closeButton&&g&&(this.events.close=a("click",{onElement:g,withCallback:function(t,i){t.preventDefault(),e.close()}})),g&&!this.settings.closeButton&&g.parentNode.removeChild(g),this.nextButton&&(this.events.next=a("click",{onElement:this.nextButton,withCallback:function(t,i){t.preventDefault(),e.nextSlide()}})),this.prevButton&&(this.events.prev=a("click",{onElement:this.prevButton,withCallback:function(t,i){t.preventDefault(),e.prevSlide()}})),this.settings.closeOnOutsideClick&&(this.events.outClose=a("click",{onElement:d,withCallback:function(t,i){e.preventOutsideClick||c(document.body,"glightbox-mobile")||u(t.target,".ginner-container")||u(t.target,".gbtn")||c(t.target,"gnext")||c(t.target,"gprev")||e.close()}})),o(this.elements,(function(t,i){e.slidesContainer.appendChild(t.instance.create()),t.slideNode=e.slidesContainer.querySelectorAll(".gslide")[i]})),K&&h(document.body,"glightbox-touch"),this.events.resize=a("resize",{onElement:window,withCallback:function(){e.resize()}}),this.built=!0}},{key:"resize",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;if((e=e||this.activeSlide)&&!c(e,"zoomed")){var t=y(),i=e.querySelector(".gvideo-wrapper"),n=e.querySelector(".gslide-image"),s=this.slideDescription,l=t.width,o=t.height;if(l<=768?h(document.body,"glightbox-mobile"):d(document.body,"glightbox-mobile"),i||n){var r=!1;if(s&&(c(s,"description-bottom")||c(s,"description-top"))&&!c(s,"gabsolute")&&(r=!0),n)if(l<=768)n.querySelector("img");else if(r){var a=s.offsetHeight,u=n.querySelector("img");u.setAttribute("style","max-height: calc(100vh - ".concat(a,"px)")),s.setAttribute("style","max-width: ".concat(u.offsetWidth,"px;"))}if(i){var g=O(this.settings.plyr.config,"ratio")?this.settings.plyr.config.ratio:"";if(!g){var v=i.clientWidth,f=i.clientHeight,p=v/f;g="".concat(v/p,":").concat(f/p)}var m=g.split(":"),x=this.settings.videosWidth,b=this.settings.videosWidth,S=(b=M(x)||-1!==x.indexOf("px")?parseInt(x):-1!==x.indexOf("vw")?l*parseInt(x)/100:-1!==x.indexOf("vh")?o*parseInt(x)/100:-1!==x.indexOf("%")?l*parseInt(x)/100:parseInt(i.clientWidth))/(parseInt(m[0])/parseInt(m[1]));if(S=Math.floor(S),r&&(o-=s.offsetHeight),b>l||S>o||ob){var w=i.offsetWidth,T=i.offsetHeight,C=o/T,k={width:w*C,height:T*C};i.parentNode.setAttribute("style","max-width: ".concat(k.width,"px")),r&&s.setAttribute("style","max-width: ".concat(k.width,"px;"))}else i.parentNode.style.maxWidth="".concat(x),r&&s.setAttribute("style","max-width: ".concat(x,";"))}}}}},{key:"reload",value:function(){this.init()}},{key:"updateNavigationClasses",value:function(){var e=this.loop();d(this.nextButton,"disabled"),d(this.prevButton,"disabled"),0==this.index&&this.elements.length-1==0?(h(this.prevButton,"disabled"),h(this.nextButton,"disabled")):0!==this.index||e?this.index!==this.elements.length-1||e||h(this.nextButton,"disabled"):h(this.prevButton,"disabled")}},{key:"loop",value:function(){var e=O(this.settings,"loopAtEnd")?this.settings.loopAtEnd:null;return e=O(this.settings,"loop")?this.settings.loop:e,e}},{key:"close",value:function(){var e=this;if(!this.lightboxOpen){if(this.events){for(var t in this.events)this.events.hasOwnProperty(t)&&this.events[t].destroy();this.events=null}return!1}if(this.closing)return!1;this.closing=!0,this.slidePlayerPause(this.activeSlide),this.fullElementsList&&(this.elements=this.fullElementsList),this.bodyHiddenChildElms.length&&o(this.bodyHiddenChildElms,(function(e){e.removeAttribute("aria-hidden")})),h(this.modal,"glightbox-closing"),g(this.overlay,"none"==this.settings.openEffect?"none":this.settings.cssEfects.fade.out),g(this.activeSlide,this.settings.cssEfects[this.settings.closeEffect].out,(function(){if(e.activeSlide=null,e.prevActiveSlideIndex=null,e.prevActiveSlide=null,e.built=!1,e.events){for(var t in e.events)e.events.hasOwnProperty(t)&&e.events[t].destroy();e.events=null}var i=document.body;d(Q,"glightbox-open"),d(i,"glightbox-open touching gdesc-open glightbox-touch glightbox-mobile gscrollbar-fixer"),e.modal.parentNode.removeChild(e.modal),e.trigger("close"),T(e.settings.onClose)&&e.settings.onClose();var n=document.querySelector(".gcss-styles");n&&n.parentNode.removeChild(n),e.lightboxOpen=!1,e.closing=null}))}},{key:"destroy",value:function(){this.close(),this.clearAllEvents(),this.baseEvents&&this.baseEvents.destroy()}},{key:"on",value:function(e,t){var i=arguments.length>2&&void 0!==arguments[2]&&arguments[2];if(!e||!T(t))throw new TypeError("Event name and callback must be defined");this.apiEvents.push({evt:e,once:i,callback:t})}},{key:"once",value:function(e,t){this.on(e,t,!0)}},{key:"trigger",value:function(e){var t=this,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,n=[];o(this.apiEvents,(function(t,s){var l=t.evt,o=t.once,r=t.callback;l==e&&(r(i),o&&n.push(s))})),n.length&&o(n,(function(e){return t.apiEvents.splice(e,1)}))}},{key:"clearAllEvents",value:function(){this.apiEvents.splice(0,this.apiEvents.length)}},{key:"version",value:function(){return"3.1.1"}}]),e}();return function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=new te(e);return t.init(),t}})); -/*! update cache: 20260104155739 */ +/*! update cache: 20260104161439 */ diff --git a/zh-hant/assets/stylesheets/glightbox.min.css b/zh-hant/assets/stylesheets/glightbox.min.css index f1f306424..0175e854e 100644 --- a/zh-hant/assets/stylesheets/glightbox.min.css +++ b/zh-hant/assets/stylesheets/glightbox.min.css @@ -1,2 +1,2 @@ .glightbox-container{width:100%;height:100%;position:fixed;top:0;left:0;z-index:999999!important;overflow:hidden;-ms-touch-action:none;touch-action:none;-webkit-text-size-adjust:100%;-moz-text-size-adjust:100%;-ms-text-size-adjust:100%;text-size-adjust:100%;-webkit-backface-visibility:hidden;backface-visibility:hidden;outline:0}.glightbox-container.inactive{display:none}.glightbox-container .gcontainer{position:relative;width:100%;height:100%;z-index:9999;overflow:hidden}.glightbox-container .gslider{-webkit-transition:-webkit-transform .4s ease;transition:-webkit-transform .4s ease;transition:transform .4s ease;transition:transform .4s ease,-webkit-transform .4s ease;height:100%;left:0;top:0;width:100%;position:relative;overflow:hidden;display:-webkit-box!important;display:-ms-flexbox!important;display:flex!important;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}.glightbox-container .gslide{width:100%;position:absolute;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;opacity:0}.glightbox-container .gslide.current{opacity:1;z-index:99999;position:relative}.glightbox-container .gslide.prev{opacity:1;z-index:9999}.glightbox-container .gslide-inner-content{width:100%}.glightbox-container .ginner-container{position:relative;width:100%;display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column;max-width:100%;margin:auto;height:100vh}.glightbox-container .ginner-container.gvideo-container{width:100%}.glightbox-container .ginner-container.desc-bottom,.glightbox-container .ginner-container.desc-top{-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column}.glightbox-container .ginner-container.desc-left,.glightbox-container .ginner-container.desc-right{max-width:100%!important}.gslide iframe,.gslide video{outline:0!important;border:none;min-height:165px;-webkit-overflow-scrolling:touch;-ms-touch-action:auto;touch-action:auto}.gslide:not(.current){pointer-events:none}.gslide-image{-webkit-box-align:center;-ms-flex-align:center;align-items:center}.gslide-image img{max-height:100vh;display:block;padding:0;float:none;outline:0;border:none;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;max-width:100vw;width:auto;height:auto;-o-object-fit:cover;object-fit:cover;-ms-touch-action:none;touch-action:none;margin:auto;min-width:200px}.desc-bottom .gslide-image img,.desc-top .gslide-image img{width:auto}.desc-left .gslide-image img,.desc-right .gslide-image img{width:auto;max-width:100%}.gslide-image img.zoomable{position:relative}.gslide-image img.dragging{cursor:-webkit-grabbing!important;cursor:grabbing!important;-webkit-transition:none;transition:none}.gslide-video{position:relative;max-width:100vh;width:100%!important}.gslide-video .plyr__poster-enabled.plyr--loading .plyr__poster{display:none}.gslide-video .gvideo-wrapper{width:100%;margin:auto}.gslide-video::before{content:'';position:absolute;width:100%;height:100%;background:rgba(255,0,0,.34);display:none}.gslide-video.playing::before{display:none}.gslide-video.fullscreen{max-width:100%!important;min-width:100%;height:75vh}.gslide-video.fullscreen video{max-width:100%!important;width:100%!important}.gslide-inline{background:#fff;text-align:left;max-height:calc(100vh - 40px);overflow:auto;max-width:100%;margin:auto}.gslide-inline .ginlined-content{padding:20px;width:100%}.gslide-inline .dragging{cursor:-webkit-grabbing!important;cursor:grabbing!important;-webkit-transition:none;transition:none}.ginlined-content{overflow:auto;display:block!important;opacity:1}.gslide-external{display:-webkit-box;display:-ms-flexbox;display:flex;width:100%;min-width:100%;background:#fff;padding:0;overflow:auto;max-height:75vh;height:100%}.gslide-media{display:-webkit-box;display:-ms-flexbox;display:flex;width:auto}.zoomed .gslide-media{-webkit-box-shadow:none!important;box-shadow:none!important}.desc-bottom .gslide-media,.desc-top .gslide-media{margin:0 auto;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column}.gslide-description{position:relative;-webkit-box-flex:1;-ms-flex:1 0 100%;flex:1 0 100%}.gslide-description.description-left,.gslide-description.description-right{max-width:100%}.gslide-description.description-bottom,.gslide-description.description-top{margin:0 auto;width:100%}.gslide-description p{margin-bottom:12px}.gslide-description p:last-child{margin-bottom:0}.zoomed .gslide-description{display:none}.glightbox-button-hidden{display:none}.glightbox-mobile .glightbox-container .gslide-description{height:auto!important;width:100%;position:absolute;bottom:0;padding:19px 11px;max-width:100vw!important;-webkit-box-ordinal-group:3!important;-ms-flex-order:2!important;order:2!important;max-height:78vh;overflow:auto!important;background:-webkit-gradient(linear,left top,left bottom,from(rgba(0,0,0,0)),to(rgba(0,0,0,.75)));background:linear-gradient(to bottom,rgba(0,0,0,0) 0,rgba(0,0,0,.75) 100%);-webkit-transition:opacity .3s linear;transition:opacity .3s linear;padding-bottom:50px}.glightbox-mobile .glightbox-container .gslide-title{color:#fff;font-size:1em}.glightbox-mobile .glightbox-container .gslide-desc{color:#a1a1a1}.glightbox-mobile .glightbox-container .gslide-desc a{color:#fff;font-weight:700}.glightbox-mobile .glightbox-container .gslide-desc *{color:inherit}.glightbox-mobile .glightbox-container .gslide-desc .desc-more{color:#fff;opacity:.4}.gdesc-open .gslide-media{-webkit-transition:opacity .5s ease;transition:opacity .5s ease;opacity:.4}.gdesc-open .gdesc-inner{padding-bottom:30px}.gdesc-closed .gslide-media{-webkit-transition:opacity .5s ease;transition:opacity .5s ease;opacity:1}.greset{-webkit-transition:all .3s ease;transition:all .3s ease}.gabsolute{position:absolute}.grelative{position:relative}.glightbox-desc{display:none!important}.glightbox-open{overflow:hidden}.gloader{height:25px;width:25px;-webkit-animation:lightboxLoader .8s infinite linear;animation:lightboxLoader .8s infinite linear;border:2px solid #fff;border-right-color:transparent;border-radius:50%;position:absolute;display:block;z-index:9999;left:0;right:0;margin:0 auto;top:47%}.goverlay{width:100%;height:calc(100vh + 1px);position:fixed;top:-1px;left:0;background:#000;will-change:opacity}.glightbox-mobile .goverlay{background:#000}.gclose,.gnext,.gprev{z-index:99999;cursor:pointer;width:26px;height:44px;border:none;display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column}.gclose svg,.gnext svg,.gprev svg{display:block;width:25px;height:auto;margin:0;padding:0}.gclose.disabled,.gnext.disabled,.gprev.disabled{opacity:.1}.gclose .garrow,.gnext .garrow,.gprev .garrow{stroke:#fff}.gbtn.focused{outline:2px solid #0f3d81}iframe.wait-autoplay{opacity:0}.glightbox-closing .gclose,.glightbox-closing .gnext,.glightbox-closing .gprev{opacity:0!important}.glightbox-clean .gslide-description{background:#fff}.glightbox-clean .gdesc-inner{padding:22px 20px}.glightbox-clean .gslide-title{font-size:1em;font-weight:400;font-family:arial;color:#000;margin-bottom:19px;line-height:1.4em}.glightbox-clean .gslide-desc{font-size:.86em;margin-bottom:0;font-family:arial;line-height:1.4em}.glightbox-clean .gslide-video{background:#000}.glightbox-clean .gclose,.glightbox-clean .gnext,.glightbox-clean .gprev{background-color:rgba(0,0,0,.75);border-radius:4px}.glightbox-clean .gclose path,.glightbox-clean .gnext path,.glightbox-clean .gprev path{fill:#fff}.glightbox-clean .gprev{position:absolute;top:-100%;left:30px;width:40px;height:50px}.glightbox-clean .gnext{position:absolute;top:-100%;right:30px;width:40px;height:50px}.glightbox-clean .gclose{width:35px;height:35px;top:15px;right:10px;position:absolute}.glightbox-clean .gclose svg{width:18px;height:auto}.glightbox-clean .gclose:hover{opacity:1}.gfadeIn{-webkit-animation:gfadeIn .5s ease;animation:gfadeIn .5s ease}.gfadeOut{-webkit-animation:gfadeOut .5s ease;animation:gfadeOut .5s ease}.gslideOutLeft{-webkit-animation:gslideOutLeft .3s ease;animation:gslideOutLeft .3s ease}.gslideInLeft{-webkit-animation:gslideInLeft .3s ease;animation:gslideInLeft .3s ease}.gslideOutRight{-webkit-animation:gslideOutRight .3s ease;animation:gslideOutRight .3s ease}.gslideInRight{-webkit-animation:gslideInRight .3s ease;animation:gslideInRight .3s ease}.gzoomIn{-webkit-animation:gzoomIn .5s ease;animation:gzoomIn .5s ease}.gzoomOut{-webkit-animation:gzoomOut .5s ease;animation:gzoomOut .5s ease}@-webkit-keyframes lightboxLoader{0%{-webkit-transform:rotate(0);transform:rotate(0)}100%{-webkit-transform:rotate(360deg);transform:rotate(360deg)}}@keyframes lightboxLoader{0%{-webkit-transform:rotate(0);transform:rotate(0)}100%{-webkit-transform:rotate(360deg);transform:rotate(360deg)}}@-webkit-keyframes gfadeIn{from{opacity:0}to{opacity:1}}@keyframes gfadeIn{from{opacity:0}to{opacity:1}}@-webkit-keyframes gfadeOut{from{opacity:1}to{opacity:0}}@keyframes gfadeOut{from{opacity:1}to{opacity:0}}@-webkit-keyframes gslideInLeft{from{opacity:0;-webkit-transform:translate3d(-60%,0,0);transform:translate3d(-60%,0,0)}to{visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0);opacity:1}}@keyframes gslideInLeft{from{opacity:0;-webkit-transform:translate3d(-60%,0,0);transform:translate3d(-60%,0,0)}to{visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0);opacity:1}}@-webkit-keyframes gslideOutLeft{from{opacity:1;visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}to{-webkit-transform:translate3d(-60%,0,0);transform:translate3d(-60%,0,0);opacity:0;visibility:hidden}}@keyframes gslideOutLeft{from{opacity:1;visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}to{-webkit-transform:translate3d(-60%,0,0);transform:translate3d(-60%,0,0);opacity:0;visibility:hidden}}@-webkit-keyframes gslideInRight{from{opacity:0;visibility:visible;-webkit-transform:translate3d(60%,0,0);transform:translate3d(60%,0,0)}to{-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0);opacity:1}}@keyframes gslideInRight{from{opacity:0;visibility:visible;-webkit-transform:translate3d(60%,0,0);transform:translate3d(60%,0,0)}to{-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0);opacity:1}}@-webkit-keyframes gslideOutRight{from{opacity:1;visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}to{-webkit-transform:translate3d(60%,0,0);transform:translate3d(60%,0,0);opacity:0}}@keyframes gslideOutRight{from{opacity:1;visibility:visible;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}to{-webkit-transform:translate3d(60%,0,0);transform:translate3d(60%,0,0);opacity:0}}@-webkit-keyframes gzoomIn{from{opacity:0;-webkit-transform:scale3d(.3,.3,.3);transform:scale3d(.3,.3,.3)}to{opacity:1}}@keyframes gzoomIn{from{opacity:0;-webkit-transform:scale3d(.3,.3,.3);transform:scale3d(.3,.3,.3)}to{opacity:1}}@-webkit-keyframes gzoomOut{from{opacity:1}50%{opacity:0;-webkit-transform:scale3d(.3,.3,.3);transform:scale3d(.3,.3,.3)}to{opacity:0}}@keyframes gzoomOut{from{opacity:1}50%{opacity:0;-webkit-transform:scale3d(.3,.3,.3);transform:scale3d(.3,.3,.3)}to{opacity:0}}@media (min-width:769px){.glightbox-container .ginner-container{width:auto;height:auto;-webkit-box-orient:horizontal;-webkit-box-direction:normal;-ms-flex-direction:row;flex-direction:row}.glightbox-container .ginner-container.desc-top .gslide-description{-webkit-box-ordinal-group:1;-ms-flex-order:0;order:0}.glightbox-container .ginner-container.desc-top .gslide-image,.glightbox-container .ginner-container.desc-top .gslide-image img{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}.glightbox-container .ginner-container.desc-left .gslide-description{-webkit-box-ordinal-group:1;-ms-flex-order:0;order:0}.glightbox-container .ginner-container.desc-left .gslide-image{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}.gslide-image img{max-height:97vh;max-width:100%}.gslide-image img.zoomable{cursor:-webkit-zoom-in;cursor:zoom-in}.zoomed .gslide-image img.zoomable{cursor:-webkit-grab;cursor:grab}.gslide-inline{max-height:95vh}.gslide-external{max-height:100vh}.gslide-description.description-left,.gslide-description.description-right{max-width:275px}.glightbox-open{height:auto}.goverlay{background:rgba(0,0,0,.92)}.glightbox-clean .gslide-media{-webkit-box-shadow:1px 2px 9px 0 rgba(0,0,0,.65);box-shadow:1px 2px 9px 0 rgba(0,0,0,.65)}.glightbox-clean .description-left .gdesc-inner,.glightbox-clean .description-right .gdesc-inner{position:absolute;height:100%;overflow-y:auto}.glightbox-clean .gclose,.glightbox-clean .gnext,.glightbox-clean .gprev{background-color:rgba(0,0,0,.32)}.glightbox-clean .gclose:hover,.glightbox-clean .gnext:hover,.glightbox-clean .gprev:hover{background-color:rgba(0,0,0,.7)}.glightbox-clean .gprev{top:45%}.glightbox-clean .gnext{top:45%}}@media (min-width:992px){.glightbox-clean .gclose{opacity:.7;right:20px}}@media screen and (max-height:420px){.goverlay{background:#000}} -/*! update cache: 20260104155739 */ +/*! update cache: 20260104161439 */ diff --git a/zh-hant/assets/stylesheets/main.484c7ddc.min.css b/zh-hant/assets/stylesheets/main.484c7ddc.min.css index 3f4236544..54357f0f5 100644 --- a/zh-hant/assets/stylesheets/main.484c7ddc.min.css +++ b/zh-hant/assets/stylesheets/main.484c7ddc.min.css @@ -1,2 +1,2 @@ @charset "UTF-8";html{-webkit-text-size-adjust:none;-moz-text-size-adjust:none;text-size-adjust:none;box-sizing:border-box}*,:after,:before{box-sizing:inherit}@media (prefers-reduced-motion){*,:after,:before{transition:none!important}}body{margin:0}a,button,input,label{-webkit-tap-highlight-color:transparent}a{color:inherit;text-decoration:none}hr{border:0;box-sizing:initial;display:block;height:.05rem;overflow:visible;padding:0}small{font-size:80%}sub,sup{line-height:1em}img{border-style:none}table{border-collapse:initial;border-spacing:0}td,th{font-weight:400;vertical-align:top}button{background:#0000;border:0;font-family:inherit;font-size:inherit;margin:0;padding:0}input{border:0;outline:none}:root{--md-primary-fg-color:#4051b5;--md-primary-fg-color--light:#5d6cc0;--md-primary-fg-color--dark:#303fa1;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3;--md-accent-fg-color:#526cfe;--md-accent-fg-color--transparent:#526cfe1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-scheme=default]{color-scheme:light}[data-md-color-scheme=default] img[src$="#gh-dark-mode-only"],[data-md-color-scheme=default] img[src$="#only-dark"]{display:none}:root,[data-md-color-scheme=default]{--md-hue:225deg;--md-default-fg-color:#000000de;--md-default-fg-color--light:#0000008a;--md-default-fg-color--lighter:#00000052;--md-default-fg-color--lightest:#00000012;--md-default-bg-color:#fff;--md-default-bg-color--light:#ffffffb3;--md-default-bg-color--lighter:#ffffff4d;--md-default-bg-color--lightest:#ffffff1f;--md-code-fg-color:#36464e;--md-code-bg-color:#f5f5f5;--md-code-bg-color--light:#f5f5f5b3;--md-code-bg-color--lighter:#f5f5f54d;--md-code-hl-color:#4287ff;--md-code-hl-color--light:#4287ff1a;--md-code-hl-number-color:#d52a2a;--md-code-hl-special-color:#db1457;--md-code-hl-function-color:#a846b9;--md-code-hl-constant-color:#6e59d9;--md-code-hl-keyword-color:#3f6ec6;--md-code-hl-string-color:#1c7d4d;--md-code-hl-name-color:var(--md-code-fg-color);--md-code-hl-operator-color:var(--md-default-fg-color--light);--md-code-hl-punctuation-color:var(--md-default-fg-color--light);--md-code-hl-comment-color:var(--md-default-fg-color--light);--md-code-hl-generic-color:var(--md-default-fg-color--light);--md-code-hl-variable-color:var(--md-default-fg-color--light);--md-typeset-color:var(--md-default-fg-color);--md-typeset-a-color:var(--md-primary-fg-color);--md-typeset-del-color:#f5503d26;--md-typeset-ins-color:#0bd57026;--md-typeset-kbd-color:#fafafa;--md-typeset-kbd-accent-color:#fff;--md-typeset-kbd-border-color:#b8b8b8;--md-typeset-mark-color:#ffff0080;--md-typeset-table-color:#0000001f;--md-typeset-table-color--light:rgba(0,0,0,.035);--md-admonition-fg-color:var(--md-default-fg-color);--md-admonition-bg-color:var(--md-default-bg-color);--md-warning-fg-color:#000000de;--md-warning-bg-color:#ff9;--md-footer-fg-color:#fff;--md-footer-fg-color--light:#ffffffb3;--md-footer-fg-color--lighter:#ffffff73;--md-footer-bg-color:#000000de;--md-footer-bg-color--dark:#00000052;--md-shadow-z1:0 0.2rem 0.5rem #0000000d,0 0 0.05rem #0000001a;--md-shadow-z2:0 0.2rem 0.5rem #0000001a,0 0 0.05rem #00000040;--md-shadow-z3:0 0.2rem 0.5rem #0003,0 0 0.05rem #00000059}.md-icon svg{fill:currentcolor;display:block;height:1.2rem;width:1.2rem}body{-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;--md-text-font-family:var(--md-text-font,_),-apple-system,BlinkMacSystemFont,Helvetica,Arial,sans-serif;--md-code-font-family:var(--md-code-font,_),SFMono-Regular,Consolas,Menlo,monospace}aside,body,input{font-feature-settings:"kern","liga";color:var(--md-typeset-color);font-family:var(--md-text-font-family)}code,kbd,pre{font-feature-settings:"kern";font-family:var(--md-code-font-family)}:root{--md-typeset-table-sort-icon:url('data:image/svg+xml;charset=utf-8,');--md-typeset-table-sort-icon--asc:url('data:image/svg+xml;charset=utf-8,');--md-typeset-table-sort-icon--desc:url('data:image/svg+xml;charset=utf-8,')}.md-typeset{-webkit-print-color-adjust:exact;color-adjust:exact;font-size:.8rem;line-height:1.6;overflow-wrap:break-word}@media print{.md-typeset{font-size:.68rem}}.md-typeset blockquote,.md-typeset dl,.md-typeset figure,.md-typeset ol,.md-typeset pre,.md-typeset ul{margin-bottom:1em;margin-top:1em}.md-typeset h1{color:var(--md-default-fg-color--light);font-size:2em;line-height:1.3;margin:0 0 1.25em}.md-typeset h1,.md-typeset h2{font-weight:300;letter-spacing:-.01em}.md-typeset h2{font-size:1.5625em;line-height:1.4;margin:1.6em 0 .64em}.md-typeset h3{font-size:1.25em;font-weight:400;letter-spacing:-.01em;line-height:1.5;margin:1.6em 0 .8em}.md-typeset h2+h3{margin-top:.8em}.md-typeset h4{font-weight:700;letter-spacing:-.01em;margin:1em 0}.md-typeset h5,.md-typeset h6{color:var(--md-default-fg-color--light);font-size:.8em;font-weight:700;letter-spacing:-.01em;margin:1.25em 0}.md-typeset h5{text-transform:uppercase}.md-typeset h5 code{text-transform:none}.md-typeset hr{border-bottom:.05rem solid var(--md-default-fg-color--lightest);display:flow-root;margin:1.5em 0}.md-typeset a{color:var(--md-typeset-a-color);word-break:break-word}.md-typeset a,.md-typeset a:before{transition:color 125ms}.md-typeset a:focus,.md-typeset a:hover{color:var(--md-accent-fg-color)}.md-typeset a:focus code,.md-typeset a:hover code{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}.md-typeset a code{color:var(--md-typeset-a-color)}.md-typeset a.focus-visible{outline-color:var(--md-accent-fg-color);outline-offset:.2rem}.md-typeset code,.md-typeset kbd,.md-typeset pre{color:var(--md-code-fg-color);direction:ltr;font-variant-ligatures:none;transition:background-color 125ms}@media print{.md-typeset code,.md-typeset kbd,.md-typeset pre{white-space:pre-wrap}}.md-typeset code{background-color:var(--md-code-bg-color);border-radius:.1rem;-webkit-box-decoration-break:clone;box-decoration-break:clone;font-size:.85em;padding:0 .2941176471em;transition:color 125ms,background-color 125ms;word-break:break-word}.md-typeset code:not(.focus-visible){-webkit-tap-highlight-color:transparent;outline:none}.md-typeset pre{display:flow-root;line-height:1.4;position:relative}.md-typeset pre>code{-webkit-box-decoration-break:slice;box-decoration-break:slice;box-shadow:none;display:block;margin:0;outline-color:var(--md-accent-fg-color);overflow:auto;padding:.7720588235em 1.1764705882em;scrollbar-color:var(--md-default-fg-color--lighter) #0000;scrollbar-width:thin;touch-action:auto;word-break:normal}.md-typeset pre>code:hover{scrollbar-color:var(--md-accent-fg-color) #0000}.md-typeset pre>code::-webkit-scrollbar{height:.2rem;width:.2rem}.md-typeset pre>code::-webkit-scrollbar-thumb{background-color:var(--md-default-fg-color--lighter)}.md-typeset pre>code::-webkit-scrollbar-thumb:hover{background-color:var(--md-accent-fg-color)}.md-typeset kbd{background-color:var(--md-typeset-kbd-color);border-radius:.1rem;box-shadow:0 .1rem 0 .05rem var(--md-typeset-kbd-border-color),0 .1rem 0 var(--md-typeset-kbd-border-color),0 -.1rem .2rem var(--md-typeset-kbd-accent-color) inset;color:var(--md-default-fg-color);display:inline-block;font-size:.75em;padding:0 .6666666667em;vertical-align:text-top;word-break:break-word}.md-typeset mark{background-color:var(--md-typeset-mark-color);-webkit-box-decoration-break:clone;box-decoration-break:clone;color:inherit;word-break:break-word}.md-typeset abbr{cursor:help;text-decoration:none}.md-typeset [data-preview],.md-typeset abbr{border-bottom:.05rem dotted var(--md-default-fg-color--light)}.md-typeset small{opacity:.75}[dir=ltr] .md-typeset sub,[dir=ltr] .md-typeset sup{margin-left:.078125em}[dir=rtl] .md-typeset sub,[dir=rtl] .md-typeset sup{margin-right:.078125em}[dir=ltr] .md-typeset blockquote{padding-left:.6rem}[dir=rtl] .md-typeset blockquote{padding-right:.6rem}[dir=ltr] .md-typeset blockquote{border-left:.2rem solid var(--md-default-fg-color--lighter)}[dir=rtl] .md-typeset blockquote{border-right:.2rem solid var(--md-default-fg-color--lighter)}.md-typeset blockquote{color:var(--md-default-fg-color--light);margin-left:0;margin-right:0}.md-typeset ul{list-style-type:disc}.md-typeset ul[type]{list-style-type:revert-layer}[dir=ltr] .md-typeset ol,[dir=ltr] .md-typeset ul{margin-left:.625em}[dir=rtl] .md-typeset ol,[dir=rtl] .md-typeset ul{margin-right:.625em}.md-typeset ol,.md-typeset ul{padding:0}.md-typeset ol:not([hidden]),.md-typeset ul:not([hidden]){display:flow-root}.md-typeset ol ol,.md-typeset ul ol{list-style-type:lower-alpha}.md-typeset ol ol ol,.md-typeset ul ol ol{list-style-type:lower-roman}.md-typeset ol ol ol ol,.md-typeset ul ol ol ol{list-style-type:upper-alpha}.md-typeset ol ol ol ol ol,.md-typeset ul ol ol ol ol{list-style-type:upper-roman}.md-typeset ol[type],.md-typeset ul[type]{list-style-type:revert-layer}[dir=ltr] .md-typeset ol li,[dir=ltr] .md-typeset ul li{margin-left:1.25em}[dir=rtl] .md-typeset ol li,[dir=rtl] .md-typeset ul li{margin-right:1.25em}.md-typeset ol li,.md-typeset ul li{margin-bottom:.5em}.md-typeset ol li blockquote,.md-typeset ol li p,.md-typeset ul li blockquote,.md-typeset ul li p{margin:.5em 0}.md-typeset ol li:last-child,.md-typeset ul li:last-child{margin-bottom:0}[dir=ltr] .md-typeset ol li ol,[dir=ltr] .md-typeset ol li ul,[dir=ltr] .md-typeset ul li ol,[dir=ltr] .md-typeset ul li ul{margin-left:.625em}[dir=rtl] .md-typeset ol li ol,[dir=rtl] .md-typeset ol li ul,[dir=rtl] .md-typeset ul li ol,[dir=rtl] .md-typeset ul li ul{margin-right:.625em}.md-typeset ol li ol,.md-typeset ol li ul,.md-typeset ul li ol,.md-typeset ul li ul{margin-bottom:.5em;margin-top:.5em}[dir=ltr] .md-typeset dd{margin-left:1.875em}[dir=rtl] .md-typeset dd{margin-right:1.875em}.md-typeset dd{margin-bottom:1.5em;margin-top:1em}.md-typeset img,.md-typeset svg,.md-typeset video{height:auto;max-width:100%}.md-typeset img[align=left]{margin:1em 1em 1em 0}.md-typeset img[align=right]{margin:1em 0 1em 1em}.md-typeset img[align]:only-child{margin-top:0}.md-typeset figure{display:flow-root;margin:1em auto;max-width:100%;text-align:center;width:fit-content}.md-typeset figure img{display:block;margin:0 auto}.md-typeset figcaption{font-style:italic;margin:1em auto;max-width:24rem}.md-typeset iframe{max-width:100%}.md-typeset table:not([class]){background-color:var(--md-default-bg-color);border:.05rem solid var(--md-typeset-table-color);border-radius:.1rem;display:inline-block;font-size:.64rem;max-width:100%;overflow:auto;touch-action:auto}@media print{.md-typeset table:not([class]){display:table}}.md-typeset table:not([class])+*{margin-top:1.5em}.md-typeset table:not([class]) td>:first-child,.md-typeset table:not([class]) th>:first-child{margin-top:0}.md-typeset table:not([class]) td>:last-child,.md-typeset table:not([class]) th>:last-child{margin-bottom:0}.md-typeset table:not([class]) td:not([align]),.md-typeset table:not([class]) th:not([align]){text-align:left}[dir=rtl] .md-typeset table:not([class]) td:not([align]),[dir=rtl] .md-typeset table:not([class]) th:not([align]){text-align:right}.md-typeset table:not([class]) th{font-weight:700;min-width:5rem;padding:.9375em 1.25em;vertical-align:top}.md-typeset table:not([class]) td{border-top:.05rem solid var(--md-typeset-table-color);padding:.9375em 1.25em;vertical-align:top}.md-typeset table:not([class]) tbody tr{transition:background-color 125ms}.md-typeset table:not([class]) tbody tr:hover{background-color:var(--md-typeset-table-color--light);box-shadow:0 .05rem 0 var(--md-default-bg-color) inset}.md-typeset table:not([class]) a{word-break:normal}.md-typeset table th[role=columnheader]{cursor:pointer}[dir=ltr] .md-typeset table th[role=columnheader]:after{margin-left:.5em}[dir=rtl] .md-typeset table th[role=columnheader]:after{margin-right:.5em}.md-typeset table th[role=columnheader]:after{content:"";display:inline-block;height:1.2em;-webkit-mask-image:var(--md-typeset-table-sort-icon);mask-image:var(--md-typeset-table-sort-icon);-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;transition:background-color 125ms;vertical-align:text-bottom;width:1.2em}.md-typeset table th[role=columnheader]:hover:after{background-color:var(--md-default-fg-color--lighter)}.md-typeset table th[role=columnheader][aria-sort=ascending]:after{background-color:var(--md-default-fg-color--light);-webkit-mask-image:var(--md-typeset-table-sort-icon--asc);mask-image:var(--md-typeset-table-sort-icon--asc)}.md-typeset table th[role=columnheader][aria-sort=descending]:after{background-color:var(--md-default-fg-color--light);-webkit-mask-image:var(--md-typeset-table-sort-icon--desc);mask-image:var(--md-typeset-table-sort-icon--desc)}.md-typeset__scrollwrap{margin:1em -.8rem;overflow-x:auto;touch-action:auto}.md-typeset__table{display:inline-block;margin-bottom:.5em;padding:0 .8rem}@media print{.md-typeset__table{display:block}}html .md-typeset__table table{display:table;margin:0;overflow:hidden;width:100%}@media screen and (max-width:44.984375em){.md-content__inner>pre{margin:1em -.8rem}.md-content__inner>pre code{border-radius:0}}.md-typeset .md-author{border-radius:100%;display:block;flex-shrink:0;height:1.6rem;overflow:hidden;position:relative;transition:color 125ms,transform 125ms;width:1.6rem}.md-typeset .md-author img{display:block}.md-typeset .md-author--more{background:var(--md-default-fg-color--lightest);color:var(--md-default-fg-color--lighter);font-size:.6rem;font-weight:700;line-height:1.6rem;text-align:center}.md-typeset .md-author--long{height:2.4rem;width:2.4rem}.md-typeset a.md-author{transform:scale(1)}.md-typeset a.md-author img{border-radius:100%;filter:grayscale(100%) opacity(75%);transition:filter 125ms}.md-typeset a.md-author:focus,.md-typeset a.md-author:hover{transform:scale(1.1);z-index:1}.md-typeset a.md-author:focus img,.md-typeset a.md-author:hover img{filter:grayscale(0)}.md-banner{background-color:var(--md-footer-bg-color);color:var(--md-footer-fg-color);overflow:auto}@media print{.md-banner{display:none}}.md-banner--warning{background-color:var(--md-warning-bg-color);color:var(--md-warning-fg-color)}.md-banner__inner{font-size:.7rem;margin:.6rem auto;padding:0 .8rem}[dir=ltr] .md-banner__button{float:right}[dir=rtl] .md-banner__button{float:left}.md-banner__button{color:inherit;cursor:pointer;transition:opacity .25s}.no-js .md-banner__button{display:none}.md-banner__button:hover{opacity:.7}html{font-size:125%;height:100%;overflow-x:hidden}@media screen and (min-width:100em){html{font-size:137.5%}}@media screen and (min-width:125em){html{font-size:150%}}body{background-color:var(--md-default-bg-color);display:flex;flex-direction:column;font-size:.5rem;min-height:100%;position:relative;width:100%}@media print{body{display:block}}@media screen and (max-width:59.984375em){body[data-md-scrolllock]{position:fixed}}.md-grid{margin-left:auto;margin-right:auto;max-width:61rem}.md-container{display:flex;flex-direction:column;flex-grow:1}@media print{.md-container{display:block}}.md-main{flex-grow:1}.md-main__inner{display:flex;height:100%;margin-top:1.5rem}.md-ellipsis{overflow:hidden;text-overflow:ellipsis}.md-toggle{display:none}.md-option{height:0;opacity:0;position:absolute;width:0}.md-option:checked+label:not([hidden]){display:block}.md-option.focus-visible+label{outline-color:var(--md-accent-fg-color);outline-style:auto}.md-skip{background-color:var(--md-default-fg-color);border-radius:.1rem;color:var(--md-default-bg-color);font-size:.64rem;margin:.5rem;opacity:0;outline-color:var(--md-accent-fg-color);padding:.3rem .5rem;position:fixed;transform:translateY(.4rem);z-index:-1}.md-skip:focus{opacity:1;transform:translateY(0);transition:transform .25s cubic-bezier(.4,0,.2,1),opacity 175ms 75ms;z-index:10}@page{margin:25mm}:root{--md-clipboard-icon:url('data:image/svg+xml;charset=utf-8,')}.md-clipboard{border-radius:.1rem;color:var(--md-default-fg-color--lightest);cursor:pointer;height:1.5em;outline-color:var(--md-accent-fg-color);outline-offset:.1rem;transition:color .25s;width:1.5em;z-index:1}@media print{.md-clipboard{display:none}}.md-clipboard:not(.focus-visible){-webkit-tap-highlight-color:transparent;outline:none}:hover>.md-clipboard{color:var(--md-default-fg-color--light)}.md-clipboard:focus,.md-clipboard:hover{color:var(--md-accent-fg-color)}.md-clipboard:after{background-color:currentcolor;content:"";display:block;height:1.125em;margin:0 auto;-webkit-mask-image:var(--md-clipboard-icon);mask-image:var(--md-clipboard-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:1.125em}.md-clipboard--inline{cursor:pointer}.md-clipboard--inline code{transition:color .25s,background-color .25s}.md-clipboard--inline:focus code,.md-clipboard--inline:hover code{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}:root{--md-code-select-icon:url('data:image/svg+xml;charset=utf-8,');--md-code-copy-icon:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .md-code__content{display:grid}.md-code__nav{background-color:var(--md-code-bg-color--lighter);border-radius:.1rem;display:flex;gap:.2rem;padding:.2rem;position:absolute;right:.25em;top:.25em;transition:background-color .25s;z-index:1}:hover>.md-code__nav{background-color:var(--md-code-bg-color--light)}.md-code__button{color:var(--md-default-fg-color--lightest);cursor:pointer;display:block;height:1.5em;outline-color:var(--md-accent-fg-color);outline-offset:.1rem;transition:color .25s;width:1.5em}:hover>*>.md-code__button{color:var(--md-default-fg-color--light)}.md-code__button.focus-visible,.md-code__button:hover{color:var(--md-accent-fg-color)}.md-code__button--active{color:var(--md-default-fg-color)!important}.md-code__button:after{background-color:currentcolor;content:"";display:block;height:1.125em;margin:0 auto;-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:1.125em}.md-code__button[data-md-type=select]:after{-webkit-mask-image:var(--md-code-select-icon);mask-image:var(--md-code-select-icon)}.md-code__button[data-md-type=copy]:after{-webkit-mask-image:var(--md-code-copy-icon);mask-image:var(--md-code-copy-icon)}@keyframes consent{0%{opacity:0;transform:translateY(100%)}to{opacity:1;transform:translateY(0)}}@keyframes overlay{0%{opacity:0}to{opacity:1}}.md-consent__overlay{animation:overlay .25s both;-webkit-backdrop-filter:blur(.1rem);backdrop-filter:blur(.1rem);background-color:#0000008a;height:100%;opacity:1;position:fixed;top:0;width:100%;z-index:5}.md-consent__inner{animation:consent .5s cubic-bezier(.1,.7,.1,1) both;background-color:var(--md-default-bg-color);border:0;border-radius:.1rem;bottom:0;box-shadow:0 0 .2rem #0000001a,0 .2rem .4rem #0003;max-height:100%;overflow:auto;padding:0;position:fixed;width:100%;z-index:5}.md-consent__form{padding:.8rem}.md-consent__settings{display:none;margin:1em 0}input:checked+.md-consent__settings{display:block}.md-consent__controls{margin-bottom:.8rem}.md-typeset .md-consent__controls .md-button{display:inline}@media screen and (max-width:44.984375em){.md-typeset .md-consent__controls .md-button{display:block;margin-top:.4rem;text-align:center;width:100%}}.md-consent label{cursor:pointer}.md-content{flex-grow:1;min-width:0}.md-content__inner{margin:0 .8rem 1.2rem;padding-top:.6rem}@media screen and (min-width:76.25em){[dir=ltr] .md-sidebar--primary:not([hidden])~.md-content>.md-content__inner{margin-left:1.2rem}[dir=ltr] .md-sidebar--secondary:not([hidden])~.md-content>.md-content__inner,[dir=rtl] .md-sidebar--primary:not([hidden])~.md-content>.md-content__inner{margin-right:1.2rem}[dir=rtl] .md-sidebar--secondary:not([hidden])~.md-content>.md-content__inner{margin-left:1.2rem}}.md-content__inner:before{content:"";display:block;height:.4rem}.md-content__inner>:last-child{margin-bottom:0}[dir=ltr] .md-content__button{float:right}[dir=rtl] .md-content__button{float:left}[dir=ltr] .md-content__button{margin-left:.4rem}[dir=rtl] .md-content__button{margin-right:.4rem}.md-content__button{margin:.4rem 0;padding:0}@media print{.md-content__button{display:none}}.md-typeset .md-content__button{color:var(--md-default-fg-color--lighter)}.md-content__button svg{display:inline;vertical-align:top}[dir=rtl] .md-content__button svg{transform:scaleX(-1)}[dir=ltr] .md-dialog{right:.8rem}[dir=rtl] .md-dialog{left:.8rem}.md-dialog{background-color:var(--md-default-fg-color);border-radius:.1rem;bottom:.8rem;box-shadow:var(--md-shadow-z3);min-width:11.1rem;opacity:0;padding:.4rem .6rem;pointer-events:none;position:fixed;transform:translateY(100%);transition:transform 0ms .4s,opacity .4s;z-index:4}@media print{.md-dialog{display:none}}.md-dialog--active{opacity:1;pointer-events:auto;transform:translateY(0);transition:transform .4s cubic-bezier(.075,.85,.175,1),opacity .4s}.md-dialog__inner{color:var(--md-default-bg-color);font-size:.7rem}.md-feedback{margin:2em 0 1em;text-align:center}.md-feedback fieldset{border:none;margin:0;padding:0}.md-feedback__title{font-weight:700;margin:1em auto}.md-feedback__inner{position:relative}.md-feedback__list{display:flex;flex-wrap:wrap;place-content:baseline center;position:relative}.md-feedback__list:hover .md-icon:not(:disabled){color:var(--md-default-fg-color--lighter)}:disabled .md-feedback__list{min-height:1.8rem}.md-feedback__icon{color:var(--md-default-fg-color--light);cursor:pointer;flex-shrink:0;margin:0 .1rem;transition:color 125ms}.md-feedback__icon:not(:disabled).md-icon:hover{color:var(--md-accent-fg-color)}.md-feedback__icon:disabled{color:var(--md-default-fg-color--lightest);pointer-events:none}.md-feedback__note{opacity:0;position:relative;transform:translateY(.4rem);transition:transform .4s cubic-bezier(.1,.7,.1,1),opacity .15s}.md-feedback__note>*{margin:0 auto;max-width:16rem}:disabled .md-feedback__note{opacity:1;transform:translateY(0)}@media print{.md-feedback{display:none}}.md-footer{background-color:var(--md-footer-bg-color);color:var(--md-footer-fg-color)}@media print{.md-footer{display:none}}.md-footer__inner{justify-content:space-between;overflow:auto;padding:.2rem}.md-footer__inner:not([hidden]){display:flex}.md-footer__link{align-items:end;display:flex;flex-grow:0.01;margin-bottom:.4rem;margin-top:1rem;max-width:100%;outline-color:var(--md-accent-fg-color);overflow:hidden;transition:opacity .25s}.md-footer__link:focus,.md-footer__link:hover{opacity:.7}[dir=rtl] .md-footer__link svg{transform:scaleX(-1)}@media screen and (max-width:44.984375em){.md-footer__link--prev{flex-shrink:0}.md-footer__link--prev .md-footer__title{display:none}}[dir=ltr] .md-footer__link--next{margin-left:auto}[dir=rtl] .md-footer__link--next{margin-right:auto}.md-footer__link--next{text-align:right}[dir=rtl] .md-footer__link--next{text-align:left}.md-footer__title{flex-grow:1;font-size:.9rem;margin-bottom:.7rem;max-width:calc(100% - 2.4rem);padding:0 1rem;white-space:nowrap}.md-footer__button{margin:.2rem;padding:.4rem}.md-footer__direction{font-size:.64rem;opacity:.7}.md-footer-meta{background-color:var(--md-footer-bg-color--dark)}.md-footer-meta__inner{display:flex;flex-wrap:wrap;justify-content:space-between;padding:.2rem}html .md-footer-meta.md-typeset a{color:var(--md-footer-fg-color--light)}html .md-footer-meta.md-typeset a:focus,html .md-footer-meta.md-typeset a:hover{color:var(--md-footer-fg-color)}.md-copyright{color:var(--md-footer-fg-color--lighter);font-size:.64rem;margin:auto .6rem;padding:.4rem 0;width:100%}@media screen and (min-width:45em){.md-copyright{width:auto}}.md-copyright__highlight{color:var(--md-footer-fg-color--light)}.md-social{display:inline-flex;gap:.2rem;margin:0 .4rem;padding:.2rem 0 .6rem}@media screen and (min-width:45em){.md-social{padding:.6rem 0}}.md-social__link{display:inline-block;height:1.6rem;text-align:center;width:1.6rem}.md-social__link:before{line-height:1.9}.md-social__link svg{fill:currentcolor;max-height:.8rem;vertical-align:-25%}.md-typeset .md-button{border:.1rem solid;border-radius:.1rem;color:var(--md-primary-fg-color);cursor:pointer;display:inline-block;font-weight:700;padding:.625em 2em;transition:color 125ms,background-color 125ms,border-color 125ms}.md-typeset .md-button--primary{background-color:var(--md-primary-fg-color);border-color:var(--md-primary-fg-color);color:var(--md-primary-bg-color)}.md-typeset .md-button:focus,.md-typeset .md-button:hover{background-color:var(--md-accent-fg-color);border-color:var(--md-accent-fg-color);color:var(--md-accent-bg-color)}[dir=ltr] .md-typeset .md-input{border-top-left-radius:.1rem}[dir=ltr] .md-typeset .md-input,[dir=rtl] .md-typeset .md-input{border-top-right-radius:.1rem}[dir=rtl] .md-typeset .md-input{border-top-left-radius:.1rem}.md-typeset .md-input{border-bottom:.1rem solid var(--md-default-fg-color--lighter);box-shadow:var(--md-shadow-z1);font-size:.8rem;height:1.8rem;padding:0 .6rem;transition:border .25s,box-shadow .25s}.md-typeset .md-input:focus,.md-typeset .md-input:hover{border-bottom-color:var(--md-accent-fg-color);box-shadow:var(--md-shadow-z2)}.md-typeset .md-input--stretch{width:100%}.md-header{background-color:var(--md-primary-fg-color);box-shadow:0 0 .2rem #0000,0 .2rem .4rem #0000;color:var(--md-primary-bg-color);display:block;left:0;position:sticky;right:0;top:0;z-index:4}@media print{.md-header{display:none}}.md-header[hidden]{transform:translateY(-100%);transition:transform .25s cubic-bezier(.8,0,.6,1),box-shadow .25s}.md-header--shadow{box-shadow:0 0 .2rem #0000001a,0 .2rem .4rem #0003;transition:transform .25s cubic-bezier(.1,.7,.1,1),box-shadow .25s}.md-header__inner{align-items:center;display:flex;padding:0 .2rem}.md-header__button{color:currentcolor;cursor:pointer;margin:.2rem;outline-color:var(--md-accent-fg-color);padding:.4rem;position:relative;transition:opacity .25s;vertical-align:middle;z-index:1}.md-header__button:hover{opacity:.7}.md-header__button:not([hidden]){display:inline-block}.md-header__button:not(.focus-visible){-webkit-tap-highlight-color:transparent;outline:none}.md-header__button.md-logo{margin:.2rem;padding:.4rem}@media screen and (max-width:76.234375em){.md-header__button.md-logo{display:none}}.md-header__button.md-logo img,.md-header__button.md-logo svg{fill:currentcolor;display:block;height:1.2rem;width:auto}@media screen and (min-width:60em){.md-header__button[for=__search]{display:none}}.no-js .md-header__button[for=__search]{display:none}[dir=rtl] .md-header__button[for=__search] svg{transform:scaleX(-1)}@media screen and (min-width:76.25em){.md-header__button[for=__drawer]{display:none}}.md-header__topic{display:flex;max-width:100%;position:absolute;transition:transform .4s cubic-bezier(.1,.7,.1,1),opacity .15s;white-space:nowrap}.md-header__topic+.md-header__topic{opacity:0;pointer-events:none;transform:translateX(1.25rem);transition:transform .4s cubic-bezier(1,.7,.1,.1),opacity .15s;z-index:-1}[dir=rtl] .md-header__topic+.md-header__topic{transform:translateX(-1.25rem)}.md-header__topic:first-child{font-weight:700}[dir=ltr] .md-header__title{margin-left:1rem;margin-right:.4rem}[dir=rtl] .md-header__title{margin-left:.4rem;margin-right:1rem}.md-header__title{flex-grow:1;font-size:.9rem;height:2.4rem;line-height:2.4rem}.md-header__title--active .md-header__topic{opacity:0;pointer-events:none;transform:translateX(-1.25rem);transition:transform .4s cubic-bezier(1,.7,.1,.1),opacity .15s;z-index:-1}[dir=rtl] .md-header__title--active .md-header__topic{transform:translateX(1.25rem)}.md-header__title--active .md-header__topic+.md-header__topic{opacity:1;pointer-events:auto;transform:translateX(0);transition:transform .4s cubic-bezier(.1,.7,.1,1),opacity .15s;z-index:0}.md-header__title>.md-header__ellipsis{height:100%;position:relative;width:100%}.md-header__option{display:flex;flex-shrink:0;max-width:100%;transition:max-width 0ms .25s,opacity .25s .25s;white-space:nowrap}[data-md-toggle=search]:checked~.md-header .md-header__option{max-width:0;opacity:0;transition:max-width 0ms,opacity 0ms}.md-header__option>input{bottom:0}.md-header__source{display:none}@media screen and (min-width:60em){[dir=ltr] .md-header__source{margin-left:1rem}[dir=rtl] .md-header__source{margin-right:1rem}.md-header__source{display:block;max-width:11.7rem;width:11.7rem}}@media screen and (min-width:76.25em){[dir=ltr] .md-header__source{margin-left:1.4rem}[dir=rtl] .md-header__source{margin-right:1.4rem}}.md-meta{color:var(--md-default-fg-color--light);font-size:.7rem;line-height:1.3}.md-meta__list{display:inline-flex;flex-wrap:wrap;list-style:none;margin:0;padding:0}.md-meta__item:not(:last-child):after{content:"·";margin-left:.2rem;margin-right:.2rem}.md-meta__link{color:var(--md-typeset-a-color)}.md-meta__link:focus,.md-meta__link:hover{color:var(--md-accent-fg-color)}.md-draft{background-color:#ff1744;border-radius:.125em;color:#fff;display:inline-block;font-weight:700;padding-left:.5714285714em;padding-right:.5714285714em}:root{--md-nav-icon--prev:url('data:image/svg+xml;charset=utf-8,');--md-nav-icon--next:url('data:image/svg+xml;charset=utf-8,');--md-toc-icon:url('data:image/svg+xml;charset=utf-8,')}.md-nav{font-size:.7rem;line-height:1.3}.md-nav__title{color:var(--md-default-fg-color--light);display:block;font-weight:700;overflow:hidden;padding:0 .6rem;text-overflow:ellipsis}.md-nav__title .md-nav__button{display:none}.md-nav__title .md-nav__button img{height:100%;width:auto}.md-nav__title .md-nav__button.md-logo img,.md-nav__title .md-nav__button.md-logo svg{fill:currentcolor;display:block;height:2.4rem;max-width:100%;object-fit:contain;width:auto}.md-nav__list{list-style:none;margin:0;padding:0}.md-nav__link{align-items:flex-start;display:flex;gap:.4rem;margin-top:.625em;scroll-snap-align:start;transition:color 125ms}.md-nav__link--passed,.md-nav__link--passed code{color:var(--md-default-fg-color--light)}.md-nav__item .md-nav__link--active,.md-nav__item .md-nav__link--active code{color:var(--md-typeset-a-color)}.md-nav__link .md-ellipsis{position:relative}.md-nav__link .md-ellipsis code{word-break:normal}[dir=ltr] .md-nav__link .md-icon:last-child{margin-left:auto}[dir=rtl] .md-nav__link .md-icon:last-child{margin-right:auto}.md-nav__link .md-typeset{font-size:.7rem;line-height:1.3}.md-nav__link svg{fill:currentcolor;flex-shrink:0;height:1.3em;position:relative}.md-nav__link[for]:focus,.md-nav__link[for]:hover,.md-nav__link[href]:focus,.md-nav__link[href]:hover{color:var(--md-accent-fg-color);cursor:pointer}.md-nav__link[for]:focus code,.md-nav__link[for]:hover code,.md-nav__link[href]:focus code,.md-nav__link[href]:hover code{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}.md-nav__link.focus-visible{outline-color:var(--md-accent-fg-color);outline-offset:.2rem}.md-nav--primary .md-nav__link[for=__toc]{display:none}.md-nav--primary .md-nav__link[for=__toc] .md-icon:after{background-color:currentcolor;display:block;height:100%;-webkit-mask-image:var(--md-toc-icon);mask-image:var(--md-toc-icon);width:100%}.md-nav--primary .md-nav__link[for=__toc]~.md-nav{display:none}.md-nav__container>.md-nav__link{margin-top:0}.md-nav__container>.md-nav__link:first-child{flex-grow:1;min-width:0}.md-nav__icon{flex-shrink:0}.md-nav__source{display:none}@media screen and (max-width:76.234375em){.md-nav--primary,.md-nav--primary .md-nav{background-color:var(--md-default-bg-color);display:flex;flex-direction:column;height:100%;left:0;position:absolute;right:0;top:0;z-index:1}.md-nav--primary .md-nav__item,.md-nav--primary .md-nav__title{font-size:.8rem;line-height:1.5}.md-nav--primary .md-nav__title{background-color:var(--md-default-fg-color--lightest);color:var(--md-default-fg-color--light);cursor:pointer;height:5.6rem;line-height:2.4rem;padding:3rem .8rem .2rem;position:relative;white-space:nowrap}[dir=ltr] .md-nav--primary .md-nav__title .md-nav__icon{left:.4rem}[dir=rtl] .md-nav--primary .md-nav__title .md-nav__icon{right:.4rem}.md-nav--primary .md-nav__title .md-nav__icon{display:block;height:1.2rem;margin:.2rem;position:absolute;top:.4rem;width:1.2rem}.md-nav--primary .md-nav__title .md-nav__icon:after{background-color:currentcolor;content:"";display:block;height:100%;-webkit-mask-image:var(--md-nav-icon--prev);mask-image:var(--md-nav-icon--prev);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:100%}.md-nav--primary .md-nav__title~.md-nav__list{background-color:var(--md-default-bg-color);box-shadow:0 .05rem 0 var(--md-default-fg-color--lightest) inset;overflow-y:auto;scroll-snap-type:y mandatory;touch-action:pan-y}.md-nav--primary .md-nav__title~.md-nav__list>:first-child{border-top:0}.md-nav--primary .md-nav__title[for=__drawer]{background-color:var(--md-primary-fg-color);color:var(--md-primary-bg-color);font-weight:700}.md-nav--primary .md-nav__title .md-logo{display:block;left:.2rem;margin:.2rem;padding:.4rem;position:absolute;right:.2rem;top:.2rem}.md-nav--primary .md-nav__list{flex:1}.md-nav--primary .md-nav__item{border-top:.05rem solid var(--md-default-fg-color--lightest)}.md-nav--primary .md-nav__item--active>.md-nav__link{color:var(--md-typeset-a-color)}.md-nav--primary .md-nav__item--active>.md-nav__link:focus,.md-nav--primary .md-nav__item--active>.md-nav__link:hover{color:var(--md-accent-fg-color)}.md-nav--primary .md-nav__link{margin-top:0;padding:.6rem .8rem}.md-nav--primary .md-nav__link svg{margin-top:.1em}.md-nav--primary .md-nav__link>.md-nav__link{padding:0}[dir=ltr] .md-nav--primary .md-nav__link .md-nav__icon{margin-right:-.2rem}[dir=rtl] .md-nav--primary .md-nav__link .md-nav__icon{margin-left:-.2rem}.md-nav--primary .md-nav__link .md-nav__icon{font-size:1.2rem;height:1.2rem;width:1.2rem}.md-nav--primary .md-nav__link .md-nav__icon:after{background-color:currentcolor;content:"";display:block;height:100%;-webkit-mask-image:var(--md-nav-icon--next);mask-image:var(--md-nav-icon--next);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:100%}[dir=rtl] .md-nav--primary .md-nav__icon:after{transform:scale(-1)}.md-nav--primary .md-nav--secondary .md-nav{background-color:initial;position:static}[dir=ltr] .md-nav--primary .md-nav--secondary .md-nav .md-nav__link{padding-left:1.4rem}[dir=rtl] .md-nav--primary .md-nav--secondary .md-nav .md-nav__link{padding-right:1.4rem}[dir=ltr] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav__link{padding-left:2rem}[dir=rtl] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav__link{padding-right:2rem}[dir=ltr] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav .md-nav__link{padding-left:2.6rem}[dir=rtl] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav .md-nav__link{padding-right:2.6rem}[dir=ltr] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav .md-nav .md-nav__link{padding-left:3.2rem}[dir=rtl] .md-nav--primary .md-nav--secondary .md-nav .md-nav .md-nav .md-nav .md-nav__link{padding-right:3.2rem}.md-nav--secondary{background-color:initial}.md-nav__toggle~.md-nav{display:flex;opacity:0;transform:translateX(100%);transition:transform .25s cubic-bezier(.8,0,.6,1),opacity 125ms 50ms}[dir=rtl] .md-nav__toggle~.md-nav{transform:translateX(-100%)}.md-nav__toggle:checked~.md-nav{opacity:1;transform:translateX(0);transition:transform .25s cubic-bezier(.4,0,.2,1),opacity 125ms 125ms}.md-nav__toggle:checked~.md-nav>.md-nav__list{backface-visibility:hidden}}@media screen and (max-width:59.984375em){.md-nav--primary .md-nav__link[for=__toc]{display:flex}.md-nav--primary .md-nav__link[for=__toc] .md-icon:after{content:""}.md-nav--primary .md-nav__link[for=__toc]+.md-nav__link{display:none}.md-nav--primary .md-nav__link[for=__toc]~.md-nav{display:flex}.md-nav__source{background-color:var(--md-primary-fg-color--dark);color:var(--md-primary-bg-color);display:block;padding:0 .2rem}}@media screen and (min-width:60em) and (max-width:76.234375em){.md-nav--integrated .md-nav__link[for=__toc]{display:flex}.md-nav--integrated .md-nav__link[for=__toc] .md-icon:after{content:""}.md-nav--integrated .md-nav__link[for=__toc]+.md-nav__link{display:none}.md-nav--integrated .md-nav__link[for=__toc]~.md-nav{display:flex}}@media screen and (min-width:60em){.md-nav{margin-bottom:-.4rem}.md-nav--secondary .md-nav__title{background:var(--md-default-bg-color);box-shadow:0 0 .4rem .4rem var(--md-default-bg-color);position:sticky;top:0;z-index:1}.md-nav--secondary .md-nav__title[for=__toc]{scroll-snap-align:start}.md-nav--secondary .md-nav__title .md-nav__icon{display:none}[dir=ltr] .md-nav--secondary .md-nav__list{padding-left:.6rem}[dir=rtl] .md-nav--secondary .md-nav__list{padding-right:.6rem}.md-nav--secondary .md-nav__list{padding-bottom:.4rem}[dir=ltr] .md-nav--secondary .md-nav__item>.md-nav__link{margin-right:.4rem}[dir=rtl] .md-nav--secondary .md-nav__item>.md-nav__link{margin-left:.4rem}}@media screen and (min-width:76.25em){.md-nav{margin-bottom:-.4rem;transition:max-height .25s cubic-bezier(.86,0,.07,1)}.md-nav--primary .md-nav__title{background:var(--md-default-bg-color);box-shadow:0 0 .4rem .4rem var(--md-default-bg-color);position:sticky;top:0;z-index:1}.md-nav--primary .md-nav__title[for=__drawer]{scroll-snap-align:start}.md-nav--primary .md-nav__title .md-nav__icon{display:none}[dir=ltr] .md-nav--primary .md-nav__list{padding-left:.6rem}[dir=rtl] .md-nav--primary .md-nav__list{padding-right:.6rem}.md-nav--primary .md-nav__list{padding-bottom:.4rem}[dir=ltr] .md-nav--primary .md-nav__item>.md-nav__link{margin-right:.4rem}[dir=rtl] .md-nav--primary .md-nav__item>.md-nav__link{margin-left:.4rem}.md-nav__toggle~.md-nav{display:grid;grid-template-rows:minmax(.4rem,0fr);opacity:0;transition:grid-template-rows .25s cubic-bezier(.86,0,.07,1),opacity .25s,visibility 0ms .25s;visibility:collapse}.md-nav__toggle~.md-nav>.md-nav__list{overflow:hidden}.md-nav__toggle.md-toggle--indeterminate~.md-nav,.md-nav__toggle:checked~.md-nav{grid-template-rows:minmax(.4rem,1fr);opacity:1;transition:grid-template-rows .25s cubic-bezier(.86,0,.07,1),opacity .15s .1s,visibility 0ms;visibility:visible}.md-nav__toggle.md-toggle--indeterminate~.md-nav{transition:none}.md-nav__item--nested>.md-nav>.md-nav__title{display:none}.md-nav__item--section{display:block;margin:1.25em 0}.md-nav__item--section:last-child{margin-bottom:0}.md-nav__item--section>.md-nav__link{font-weight:700}.md-nav__item--section>.md-nav__link[for]{color:var(--md-default-fg-color--light)}.md-nav__item--section>.md-nav__link:not(.md-nav__container){pointer-events:none}.md-nav__item--section>.md-nav__link .md-icon,.md-nav__item--section>.md-nav__link>[for]{display:none}[dir=ltr] .md-nav__item--section>.md-nav{margin-left:-.6rem}[dir=rtl] .md-nav__item--section>.md-nav{margin-right:-.6rem}.md-nav__item--section>.md-nav{display:block;opacity:1;visibility:visible}.md-nav__item--section>.md-nav>.md-nav__list>.md-nav__item{padding:0}.md-nav__icon{border-radius:100%;height:.9rem;transition:background-color .25s;width:.9rem}.md-nav__icon:hover{background-color:var(--md-accent-fg-color--transparent)}.md-nav__icon:after{background-color:currentcolor;border-radius:100%;content:"";display:inline-block;height:100%;-webkit-mask-image:var(--md-nav-icon--next);mask-image:var(--md-nav-icon--next);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;transition:transform .25s;vertical-align:-.1rem;width:100%}[dir=rtl] .md-nav__icon:after{transform:rotate(180deg)}.md-nav__item--nested .md-nav__toggle:checked~.md-nav__link .md-nav__icon:after,.md-nav__item--nested .md-toggle--indeterminate~.md-nav__link .md-nav__icon:after{transform:rotate(90deg)}.md-nav--lifted>.md-nav__list>.md-nav__item,.md-nav--lifted>.md-nav__title{display:none}.md-nav--lifted>.md-nav__list>.md-nav__item--active{display:block}.md-nav--lifted>.md-nav__list>.md-nav__item--active>.md-nav__link{background:var(--md-default-bg-color);box-shadow:0 0 .4rem .4rem var(--md-default-bg-color);margin-top:0;position:sticky;top:0;z-index:1}.md-nav--lifted>.md-nav__list>.md-nav__item--active>.md-nav__link:not(.md-nav__container){pointer-events:none}.md-nav--lifted>.md-nav__list>.md-nav__item--active.md-nav__item--section{margin:0}[dir=ltr] .md-nav--lifted>.md-nav__list>.md-nav__item>.md-nav:not(.md-nav--secondary){margin-left:-.6rem}[dir=rtl] .md-nav--lifted>.md-nav__list>.md-nav__item>.md-nav:not(.md-nav--secondary){margin-right:-.6rem}.md-nav--lifted>.md-nav__list>.md-nav__item>[for]{color:var(--md-default-fg-color--light)}.md-nav--lifted .md-nav[data-md-level="1"]{grid-template-rows:minmax(.4rem,1fr);opacity:1;visibility:visible}[dir=ltr] .md-nav--integrated>.md-nav__list>.md-nav__item--active .md-nav--secondary{border-left:.05rem solid var(--md-primary-fg-color)}[dir=rtl] .md-nav--integrated>.md-nav__list>.md-nav__item--active .md-nav--secondary{border-right:.05rem solid var(--md-primary-fg-color)}.md-nav--integrated>.md-nav__list>.md-nav__item--active .md-nav--secondary{display:block;margin-bottom:1.25em;opacity:1;visibility:visible}.md-nav--integrated>.md-nav__list>.md-nav__item--active .md-nav--secondary>.md-nav__list{overflow:visible;padding-bottom:0}.md-nav--integrated>.md-nav__list>.md-nav__item--active .md-nav--secondary>.md-nav__title{display:none}}.md-pagination{font-size:.8rem;font-weight:700;gap:.4rem}.md-pagination,.md-pagination>*{align-items:center;display:flex;justify-content:center}.md-pagination>*{border-radius:.2rem;height:1.8rem;min-width:1.8rem;text-align:center}.md-pagination__current{background-color:var(--md-default-fg-color--lightest);color:var(--md-default-fg-color--light)}.md-pagination__link{transition:color 125ms,background-color 125ms}.md-pagination__link:focus,.md-pagination__link:hover{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}.md-pagination__link:focus svg,.md-pagination__link:hover svg{color:var(--md-accent-fg-color)}.md-pagination__link.focus-visible{outline-color:var(--md-accent-fg-color);outline-offset:.2rem}.md-pagination__link svg{fill:currentcolor;color:var(--md-default-fg-color--lighter);display:block;max-height:100%;width:1.2rem}:root{--md-path-icon:url('data:image/svg+xml;charset=utf-8,')}.md-path{font-size:.7rem;margin:0 .8rem;overflow:auto;padding-top:1.2rem}.md-path:not([hidden]){display:block}@media screen and (min-width:76.25em){.md-path{margin:0 1.2rem}}.md-path__list{align-items:center;display:flex;gap:.2rem;list-style:none;margin:0;padding:0}.md-path__item:not(:first-child){display:inline-flex;gap:.2rem;white-space:nowrap}.md-path__item:not(:first-child):before{background-color:var(--md-default-fg-color--lighter);content:"";display:inline;height:.8rem;-webkit-mask-image:var(--md-path-icon);mask-image:var(--md-path-icon);width:.8rem}.md-path__link{align-items:center;color:var(--md-default-fg-color--light);display:flex}.md-path__link:focus,.md-path__link:hover{color:var(--md-accent-fg-color)}:root{--md-post-pin-icon:url('data:image/svg+xml;charset=utf-8,')}.md-post__back{border-bottom:.05rem solid var(--md-default-fg-color--lightest);margin-bottom:1.2rem;padding-bottom:1.2rem}@media screen and (max-width:76.234375em){.md-post__back{display:none}}[dir=rtl] .md-post__back svg{transform:scaleX(-1)}.md-post__authors{display:flex;flex-direction:column;gap:.6rem;margin:0 .6rem 1.2rem}.md-post .md-post__meta a{transition:color 125ms}.md-post .md-post__meta a:focus,.md-post .md-post__meta a:hover{color:var(--md-accent-fg-color)}.md-post__title{color:var(--md-default-fg-color--light);font-weight:700}.md-post--excerpt{margin-bottom:3.2rem}.md-post--excerpt .md-post__header{align-items:center;display:flex;gap:.6rem;min-height:1.6rem}.md-post--excerpt .md-post__authors{align-items:center;display:inline-flex;flex-direction:row;gap:.2rem;margin:0;min-height:2.4rem}[dir=ltr] .md-post--excerpt .md-post__meta .md-meta__list{margin-right:.4rem}[dir=rtl] .md-post--excerpt .md-post__meta .md-meta__list{margin-left:.4rem}.md-post--excerpt .md-post__content>:first-child{--md-scroll-margin:6rem;margin-top:0}.md-post>.md-nav--secondary{margin:1em 0}.md-pin{background:var(--md-default-fg-color--lightest);border-radius:1rem;margin-top:-.05rem;padding:.2rem}.md-pin:after{background-color:currentcolor;content:"";display:block;height:.6rem;margin:0 auto;-webkit-mask-image:var(--md-post-pin-icon);mask-image:var(--md-post-pin-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:.6rem}.md-profile{align-items:center;display:flex;font-size:.7rem;gap:.6rem;line-height:1.4;width:100%}.md-profile__description{flex-grow:1}.md-content--post{display:flex}@media screen and (max-width:76.234375em){.md-content--post{flex-flow:column-reverse}}.md-content--post>.md-content__inner{flex-grow:1;min-width:0}@media screen and (min-width:76.25em){[dir=ltr] .md-content--post>.md-content__inner{margin-left:1.2rem}[dir=rtl] .md-content--post>.md-content__inner{margin-right:1.2rem}}@media screen and (max-width:76.234375em){.md-sidebar.md-sidebar--post{padding:0;position:static;width:100%}.md-sidebar.md-sidebar--post .md-sidebar__scrollwrap{overflow:visible}.md-sidebar.md-sidebar--post .md-sidebar__inner{padding:0}.md-sidebar.md-sidebar--post .md-post__meta{margin-left:.6rem;margin-right:.6rem}.md-sidebar.md-sidebar--post .md-nav__item{border:none;display:inline}.md-sidebar.md-sidebar--post .md-nav__list{display:inline-flex;flex-wrap:wrap;gap:.6rem;padding-bottom:.6rem;padding-top:.6rem}.md-sidebar.md-sidebar--post .md-nav__link{padding:0}.md-sidebar.md-sidebar--post .md-nav{height:auto;margin-bottom:0;position:static}}:root{--md-progress-value:0;--md-progress-delay:400ms}.md-progress{background:var(--md-primary-bg-color);height:.075rem;opacity:min(clamp(0,var(--md-progress-value),1),clamp(0,100 - var(--md-progress-value),1));position:fixed;top:0;transform:scaleX(calc(var(--md-progress-value)*1%));transform-origin:left;transition:transform .5s cubic-bezier(.19,1,.22,1),opacity .25s var(--md-progress-delay);width:100%;z-index:4}:root{--md-search-result-icon:url('data:image/svg+xml;charset=utf-8,')}.md-search{position:relative}@media screen and (min-width:60em){.md-search{padding:.2rem 0}}.no-js .md-search{display:none}.md-search__overlay{opacity:0;z-index:1}@media screen and (max-width:59.984375em){[dir=ltr] .md-search__overlay{left:-2.2rem}[dir=rtl] .md-search__overlay{right:-2.2rem}.md-search__overlay{background-color:var(--md-default-bg-color);border-radius:1rem;height:2rem;overflow:hidden;pointer-events:none;position:absolute;top:-1rem;transform-origin:center;transition:transform .3s .1s,opacity .2s .2s;width:2rem}[data-md-toggle=search]:checked~.md-header .md-search__overlay{opacity:1;transition:transform .4s,opacity .1s}}@media screen and (min-width:60em){[dir=ltr] .md-search__overlay{left:0}[dir=rtl] .md-search__overlay{right:0}.md-search__overlay{background-color:#0000008a;cursor:pointer;height:0;position:fixed;top:0;transition:width 0ms .25s,height 0ms .25s,opacity .25s;width:0}[data-md-toggle=search]:checked~.md-header .md-search__overlay{height:200vh;opacity:1;transition:width 0ms,height 0ms,opacity .25s;width:100%}}@media screen and (max-width:29.984375em){[data-md-toggle=search]:checked~.md-header .md-search__overlay{transform:scale(45)}}@media screen and (min-width:30em) and (max-width:44.984375em){[data-md-toggle=search]:checked~.md-header .md-search__overlay{transform:scale(60)}}@media screen and (min-width:45em) and (max-width:59.984375em){[data-md-toggle=search]:checked~.md-header .md-search__overlay{transform:scale(75)}}.md-search__inner{backface-visibility:hidden}@media screen and (max-width:59.984375em){[dir=ltr] .md-search__inner{left:0}[dir=rtl] .md-search__inner{right:0}.md-search__inner{height:0;opacity:0;overflow:hidden;position:fixed;top:0;transform:translateX(5%);transition:width 0ms .3s,height 0ms .3s,transform .15s cubic-bezier(.4,0,.2,1) .15s,opacity .15s .15s;width:0;z-index:2}[dir=rtl] .md-search__inner{transform:translateX(-5%)}[data-md-toggle=search]:checked~.md-header .md-search__inner{height:100%;opacity:1;transform:translateX(0);transition:width 0ms 0ms,height 0ms 0ms,transform .15s cubic-bezier(.1,.7,.1,1) .15s,opacity .15s .15s;width:100%}}@media screen and (min-width:60em){[dir=ltr] .md-search__inner{float:right}[dir=rtl] .md-search__inner{float:left}.md-search__inner{padding:.1rem 0;position:relative;transition:width .25s cubic-bezier(.1,.7,.1,1);width:11.7rem}}@media screen and (min-width:60em) and (max-width:76.234375em){[data-md-toggle=search]:checked~.md-header .md-search__inner{width:23.4rem}}@media screen and (min-width:76.25em){[data-md-toggle=search]:checked~.md-header .md-search__inner{width:34.4rem}}.md-search__form{background-color:var(--md-default-bg-color);box-shadow:0 0 .6rem #0000;height:2.4rem;position:relative;transition:color .25s,background-color .25s;z-index:2}@media screen and (min-width:60em){.md-search__form{background-color:#00000042;border-radius:.1rem;height:1.8rem}.md-search__form:hover{background-color:#ffffff1f}}[data-md-toggle=search]:checked~.md-header .md-search__form{background-color:var(--md-default-bg-color);border-radius:.1rem .1rem 0 0;box-shadow:0 0 .6rem #00000012;color:var(--md-default-fg-color)}[dir=ltr] .md-search__input{padding-left:3.6rem;padding-right:2.2rem}[dir=rtl] .md-search__input{padding-left:2.2rem;padding-right:3.6rem}.md-search__input{background:#0000;font-size:.9rem;height:100%;position:relative;text-overflow:ellipsis;width:100%;z-index:2}.md-search__input::placeholder{transition:color .25s}.md-search__input::placeholder,.md-search__input~.md-search__icon{color:var(--md-default-fg-color--light)}.md-search__input::-ms-clear{display:none}@media screen and (max-width:59.984375em){.md-search__input{font-size:.9rem;height:2.4rem;width:100%}}@media screen and (min-width:60em){[dir=ltr] .md-search__input{padding-left:2.2rem}[dir=rtl] .md-search__input{padding-right:2.2rem}.md-search__input{color:inherit;font-size:.8rem}.md-search__input::placeholder{color:var(--md-primary-bg-color--light)}.md-search__input+.md-search__icon{color:var(--md-primary-bg-color)}[data-md-toggle=search]:checked~.md-header .md-search__input{text-overflow:clip}[data-md-toggle=search]:checked~.md-header .md-search__input+.md-search__icon{color:var(--md-default-fg-color--light)}[data-md-toggle=search]:checked~.md-header .md-search__input::placeholder{color:#0000}}.md-search__icon{cursor:pointer;display:inline-block;height:1.2rem;transition:color .25s,opacity .25s;width:1.2rem}.md-search__icon:hover{opacity:.7}[dir=ltr] .md-search__icon[for=__search]{left:.5rem}[dir=rtl] .md-search__icon[for=__search]{right:.5rem}.md-search__icon[for=__search]{position:absolute;top:.3rem;z-index:2}[dir=rtl] .md-search__icon[for=__search] svg{transform:scaleX(-1)}@media screen and (max-width:59.984375em){[dir=ltr] .md-search__icon[for=__search]{left:.8rem}[dir=rtl] .md-search__icon[for=__search]{right:.8rem}.md-search__icon[for=__search]{top:.6rem}.md-search__icon[for=__search] svg:first-child{display:none}}@media screen and (min-width:60em){.md-search__icon[for=__search]{pointer-events:none}.md-search__icon[for=__search] svg:last-child{display:none}}[dir=ltr] .md-search__options{right:.5rem}[dir=rtl] .md-search__options{left:.5rem}.md-search__options{pointer-events:none;position:absolute;top:.3rem;z-index:2}@media screen and (max-width:59.984375em){[dir=ltr] .md-search__options{right:.8rem}[dir=rtl] .md-search__options{left:.8rem}.md-search__options{top:.6rem}}[dir=ltr] .md-search__options>.md-icon{margin-left:.2rem}[dir=rtl] .md-search__options>.md-icon{margin-right:.2rem}.md-search__options>.md-icon{color:var(--md-default-fg-color--light);opacity:0;transform:scale(.75);transition:transform .15s cubic-bezier(.1,.7,.1,1),opacity .15s}.md-search__options>.md-icon:not(.focus-visible){-webkit-tap-highlight-color:transparent;outline:none}[data-md-toggle=search]:checked~.md-header .md-search__input:valid~.md-search__options>.md-icon{opacity:1;pointer-events:auto;transform:scale(1)}[data-md-toggle=search]:checked~.md-header .md-search__input:valid~.md-search__options>.md-icon:hover{opacity:.7}[dir=ltr] .md-search__suggest{padding-left:3.6rem;padding-right:2.2rem}[dir=rtl] .md-search__suggest{padding-left:2.2rem;padding-right:3.6rem}.md-search__suggest{align-items:center;color:var(--md-default-fg-color--lighter);display:flex;font-size:.9rem;height:100%;opacity:0;position:absolute;top:0;transition:opacity 50ms;white-space:nowrap;width:100%}@media screen and (min-width:60em){[dir=ltr] .md-search__suggest{padding-left:2.2rem}[dir=rtl] .md-search__suggest{padding-right:2.2rem}.md-search__suggest{font-size:.8rem}}[data-md-toggle=search]:checked~.md-header .md-search__suggest{opacity:1;transition:opacity .3s .1s}[dir=ltr] .md-search__output{border-bottom-left-radius:.1rem}[dir=ltr] .md-search__output,[dir=rtl] .md-search__output{border-bottom-right-radius:.1rem}[dir=rtl] .md-search__output{border-bottom-left-radius:.1rem}.md-search__output{overflow:hidden;position:absolute;width:100%;z-index:1}@media screen and (max-width:59.984375em){.md-search__output{bottom:0;top:2.4rem}}@media screen and (min-width:60em){.md-search__output{opacity:0;top:1.9rem;transition:opacity .4s}[data-md-toggle=search]:checked~.md-header .md-search__output{box-shadow:var(--md-shadow-z3);opacity:1}}.md-search__scrollwrap{backface-visibility:hidden;background-color:var(--md-default-bg-color);height:100%;overflow-y:auto;touch-action:pan-y}@media (-webkit-max-device-pixel-ratio:1),(max-resolution:1dppx){.md-search__scrollwrap{transform:translateZ(0)}}@media screen and (min-width:60em) and (max-width:76.234375em){.md-search__scrollwrap{width:23.4rem}}@media screen and (min-width:76.25em){.md-search__scrollwrap{width:34.4rem}}@media screen and (min-width:60em){.md-search__scrollwrap{max-height:0;scrollbar-color:var(--md-default-fg-color--lighter) #0000;scrollbar-width:thin}[data-md-toggle=search]:checked~.md-header .md-search__scrollwrap{max-height:75vh}.md-search__scrollwrap:hover{scrollbar-color:var(--md-accent-fg-color) #0000}.md-search__scrollwrap::-webkit-scrollbar{height:.2rem;width:.2rem}.md-search__scrollwrap::-webkit-scrollbar-thumb{background-color:var(--md-default-fg-color--lighter)}.md-search__scrollwrap::-webkit-scrollbar-thumb:hover{background-color:var(--md-accent-fg-color)}}.md-search-result{color:var(--md-default-fg-color);word-break:break-word}.md-search-result__meta{background-color:var(--md-default-fg-color--lightest);color:var(--md-default-fg-color--light);font-size:.64rem;line-height:1.8rem;padding:0 .8rem;scroll-snap-align:start}@media screen and (min-width:60em){[dir=ltr] .md-search-result__meta{padding-left:2.2rem}[dir=rtl] .md-search-result__meta{padding-right:2.2rem}}.md-search-result__list{list-style:none;margin:0;padding:0;-webkit-user-select:none;user-select:none}.md-search-result__item{box-shadow:0 -.05rem var(--md-default-fg-color--lightest)}.md-search-result__item:first-child{box-shadow:none}.md-search-result__link{display:block;outline:none;scroll-snap-align:start;transition:background-color .25s}.md-search-result__link:focus,.md-search-result__link:hover{background-color:var(--md-accent-fg-color--transparent)}.md-search-result__link:last-child p:last-child{margin-bottom:.6rem}.md-search-result__more>summary{cursor:pointer;display:block;outline:none;position:sticky;scroll-snap-align:start;top:0;z-index:1}.md-search-result__more>summary::marker{display:none}.md-search-result__more>summary::-webkit-details-marker{display:none}.md-search-result__more>summary>div{color:var(--md-typeset-a-color);font-size:.64rem;padding:.75em .8rem;transition:color .25s,background-color .25s}@media screen and (min-width:60em){[dir=ltr] .md-search-result__more>summary>div{padding-left:2.2rem}[dir=rtl] .md-search-result__more>summary>div{padding-right:2.2rem}}.md-search-result__more>summary:focus>div,.md-search-result__more>summary:hover>div{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}.md-search-result__more[open]>summary{background-color:var(--md-default-bg-color)}.md-search-result__article{overflow:hidden;padding:0 .8rem;position:relative}@media screen and (min-width:60em){[dir=ltr] .md-search-result__article{padding-left:2.2rem}[dir=rtl] .md-search-result__article{padding-right:2.2rem}}[dir=ltr] .md-search-result__icon{left:0}[dir=rtl] .md-search-result__icon{right:0}.md-search-result__icon{color:var(--md-default-fg-color--light);height:1.2rem;margin:.5rem;position:absolute;width:1.2rem}@media screen and (max-width:59.984375em){.md-search-result__icon{display:none}}.md-search-result__icon:after{background-color:currentcolor;content:"";display:inline-block;height:100%;-webkit-mask-image:var(--md-search-result-icon);mask-image:var(--md-search-result-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:100%}[dir=rtl] .md-search-result__icon:after{transform:scaleX(-1)}.md-search-result .md-typeset{color:var(--md-default-fg-color--light);font-size:.64rem;line-height:1.6}.md-search-result .md-typeset h1{color:var(--md-default-fg-color);font-size:.8rem;font-weight:400;line-height:1.4;margin:.55rem 0}.md-search-result .md-typeset h1 mark{text-decoration:none}.md-search-result .md-typeset h2{color:var(--md-default-fg-color);font-size:.64rem;font-weight:700;line-height:1.6;margin:.5em 0}.md-search-result .md-typeset h2 mark{text-decoration:none}.md-search-result__terms{color:var(--md-default-fg-color);display:block;font-size:.64rem;font-style:italic;margin:.5em 0}.md-search-result mark{background-color:initial;color:var(--md-accent-fg-color);text-decoration:underline}.md-select{position:relative;z-index:1}.md-select__inner{background-color:var(--md-default-bg-color);border-radius:.1rem;box-shadow:var(--md-shadow-z2);color:var(--md-default-fg-color);left:50%;margin-top:.2rem;max-height:0;opacity:0;position:absolute;top:calc(100% - .2rem);transform:translate3d(-50%,.3rem,0);transition:transform .25s 375ms,opacity .25s .25s,max-height 0ms .5s}.md-select:focus-within .md-select__inner,.md-select:hover .md-select__inner{max-height:10rem;opacity:1;transform:translate3d(-50%,0,0);transition:transform .25s cubic-bezier(.1,.7,.1,1),opacity .25s,max-height 0ms}.md-select__inner:after{border-bottom:.2rem solid #0000;border-bottom-color:var(--md-default-bg-color);border-left:.2rem solid #0000;border-right:.2rem solid #0000;border-top:0;content:"";height:0;left:50%;margin-left:-.2rem;margin-top:-.2rem;position:absolute;top:0;width:0}.md-select__list{border-radius:.1rem;font-size:.8rem;list-style-type:none;margin:0;max-height:inherit;overflow:auto;padding:0}.md-select__item{line-height:1.8rem}[dir=ltr] .md-select__link{padding-left:.6rem;padding-right:1.2rem}[dir=rtl] .md-select__link{padding-left:1.2rem;padding-right:.6rem}.md-select__link{cursor:pointer;display:block;outline:none;scroll-snap-align:start;transition:background-color .25s,color .25s;width:100%}.md-select__link:focus,.md-select__link:hover{color:var(--md-accent-fg-color)}.md-select__link:focus{background-color:var(--md-default-fg-color--lightest)}.md-sidebar{align-self:flex-start;flex-shrink:0;padding:1.2rem 0;position:sticky;top:2.4rem;width:12.1rem}@media print{.md-sidebar{display:none}}@media screen and (max-width:76.234375em){[dir=ltr] .md-sidebar--primary{left:-12.1rem}[dir=rtl] .md-sidebar--primary{right:-12.1rem}.md-sidebar--primary{background-color:var(--md-default-bg-color);display:block;height:100%;position:fixed;top:0;transform:translateX(0);transition:transform .25s cubic-bezier(.4,0,.2,1),box-shadow .25s;width:12.1rem;z-index:5}[data-md-toggle=drawer]:checked~.md-container .md-sidebar--primary{box-shadow:var(--md-shadow-z3);transform:translateX(12.1rem)}[dir=rtl] [data-md-toggle=drawer]:checked~.md-container .md-sidebar--primary{transform:translateX(-12.1rem)}.md-sidebar--primary .md-sidebar__scrollwrap{bottom:0;left:0;margin:0;overflow:hidden;position:absolute;right:0;scroll-snap-type:none;top:0}}@media screen and (min-width:76.25em){.md-sidebar{height:0}.no-js .md-sidebar{height:auto}.md-header--lifted~.md-container .md-sidebar{top:4.8rem}}.md-sidebar--secondary{display:none;order:2}@media screen and (min-width:60em){.md-sidebar--secondary{height:0}.no-js .md-sidebar--secondary{height:auto}.md-sidebar--secondary:not([hidden]){display:block}.md-sidebar--secondary .md-sidebar__scrollwrap{touch-action:pan-y}}.md-sidebar__scrollwrap{backface-visibility:hidden;margin:0 .2rem;overflow-y:auto;scrollbar-color:var(--md-default-fg-color--lighter) #0000}@media screen and (min-width:60em){.md-sidebar__scrollwrap{scrollbar-gutter:stable;scrollbar-width:thin}}.md-sidebar__scrollwrap::-webkit-scrollbar{height:.2rem;width:.2rem}.md-sidebar__scrollwrap:focus-within,.md-sidebar__scrollwrap:hover{scrollbar-color:var(--md-accent-fg-color) #0000}.md-sidebar__scrollwrap:focus-within::-webkit-scrollbar-thumb,.md-sidebar__scrollwrap:hover::-webkit-scrollbar-thumb{background-color:var(--md-default-fg-color--lighter)}.md-sidebar__scrollwrap:focus-within::-webkit-scrollbar-thumb:hover,.md-sidebar__scrollwrap:hover::-webkit-scrollbar-thumb:hover{background-color:var(--md-accent-fg-color)}@supports selector(::-webkit-scrollbar){.md-sidebar__scrollwrap{scrollbar-gutter:auto}[dir=ltr] .md-sidebar__inner{padding-right:calc(100% - 11.5rem)}[dir=rtl] .md-sidebar__inner{padding-left:calc(100% - 11.5rem)}}@media screen and (max-width:76.234375em){.md-overlay{background-color:#0000008a;height:0;opacity:0;position:fixed;top:0;transition:width 0ms .25s,height 0ms .25s,opacity .25s;width:0;z-index:5}[data-md-toggle=drawer]:checked~.md-overlay{height:100%;opacity:1;transition:width 0ms,height 0ms,opacity .25s;width:100%}}@keyframes facts{0%{height:0}to{height:.65rem}}@keyframes fact{0%{opacity:0;transform:translateY(100%)}50%{opacity:0}to{opacity:1;transform:translateY(0)}}:root{--md-source-forks-icon:url('data:image/svg+xml;charset=utf-8,');--md-source-repositories-icon:url('data:image/svg+xml;charset=utf-8,');--md-source-stars-icon:url('data:image/svg+xml;charset=utf-8,');--md-source-version-icon:url('data:image/svg+xml;charset=utf-8,')}.md-source{backface-visibility:hidden;display:block;font-size:.65rem;line-height:1.2;outline-color:var(--md-accent-fg-color);transition:opacity .25s;white-space:nowrap}.md-source:hover{opacity:.7}.md-source__icon{display:inline-block;height:2.4rem;vertical-align:middle;width:2rem}[dir=ltr] .md-source__icon svg{margin-left:.6rem}[dir=rtl] .md-source__icon svg{margin-right:.6rem}.md-source__icon svg{margin-top:.6rem}[dir=ltr] .md-source__icon+.md-source__repository{padding-left:2rem}[dir=rtl] .md-source__icon+.md-source__repository{padding-right:2rem}[dir=ltr] .md-source__icon+.md-source__repository{margin-left:-2rem}[dir=rtl] .md-source__icon+.md-source__repository{margin-right:-2rem}[dir=ltr] .md-source__repository{margin-left:.6rem}[dir=rtl] .md-source__repository{margin-right:.6rem}.md-source__repository{display:inline-block;max-width:calc(100% - 1.2rem);overflow:hidden;text-overflow:ellipsis;vertical-align:middle}.md-source__facts{display:flex;font-size:.55rem;gap:.4rem;list-style-type:none;margin:.1rem 0 0;opacity:.75;overflow:hidden;padding:0;width:100%}.md-source__repository--active .md-source__facts{animation:facts .25s ease-in}.md-source__fact{overflow:hidden;text-overflow:ellipsis}.md-source__repository--active .md-source__fact{animation:fact .4s ease-out}[dir=ltr] .md-source__fact:before{margin-right:.1rem}[dir=rtl] .md-source__fact:before{margin-left:.1rem}.md-source__fact:before{background-color:currentcolor;content:"";display:inline-block;height:.6rem;-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;vertical-align:text-top;width:.6rem}.md-source__fact:nth-child(1n+2){flex-shrink:0}.md-source__fact--version:before{-webkit-mask-image:var(--md-source-version-icon);mask-image:var(--md-source-version-icon)}.md-source__fact--stars:before{-webkit-mask-image:var(--md-source-stars-icon);mask-image:var(--md-source-stars-icon)}.md-source__fact--forks:before{-webkit-mask-image:var(--md-source-forks-icon);mask-image:var(--md-source-forks-icon)}.md-source__fact--repositories:before{-webkit-mask-image:var(--md-source-repositories-icon);mask-image:var(--md-source-repositories-icon)}.md-source-file{margin:1em 0}[dir=ltr] .md-source-file__fact{margin-right:.6rem}[dir=rtl] .md-source-file__fact{margin-left:.6rem}.md-source-file__fact{align-items:center;color:var(--md-default-fg-color--light);display:inline-flex;font-size:.68rem;gap:.3rem}.md-source-file__fact .md-icon{flex-shrink:0;margin-bottom:.05rem}[dir=ltr] .md-source-file__fact .md-author{float:left}[dir=rtl] .md-source-file__fact .md-author{float:right}.md-source-file__fact .md-author{margin-right:.2rem}.md-source-file__fact svg{width:.9rem}:root{--md-status:url('data:image/svg+xml;charset=utf-8,');--md-status--new:url('data:image/svg+xml;charset=utf-8,');--md-status--deprecated:url('data:image/svg+xml;charset=utf-8,');--md-status--encrypted:url('data:image/svg+xml;charset=utf-8,')}.md-status:after{background-color:var(--md-default-fg-color--light);content:"";display:inline-block;height:1.125em;-webkit-mask-image:var(--md-status);mask-image:var(--md-status);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;vertical-align:text-bottom;width:1.125em}.md-status:hover:after{background-color:currentcolor}.md-status--new:after{-webkit-mask-image:var(--md-status--new);mask-image:var(--md-status--new)}.md-status--deprecated:after{-webkit-mask-image:var(--md-status--deprecated);mask-image:var(--md-status--deprecated)}.md-status--encrypted:after{-webkit-mask-image:var(--md-status--encrypted);mask-image:var(--md-status--encrypted)}.md-tabs{background-color:var(--md-primary-fg-color);color:var(--md-primary-bg-color);display:block;line-height:1.3;overflow:auto;width:100%;z-index:3}@media print{.md-tabs{display:none}}@media screen and (max-width:76.234375em){.md-tabs{display:none}}.md-tabs[hidden]{pointer-events:none}[dir=ltr] .md-tabs__list{margin-left:.2rem}[dir=rtl] .md-tabs__list{margin-right:.2rem}.md-tabs__list{contain:content;display:flex;list-style:none;margin:0;overflow:auto;padding:0;scrollbar-width:none;white-space:nowrap}.md-tabs__list::-webkit-scrollbar{display:none}.md-tabs__item{height:2.4rem;padding-left:.6rem;padding-right:.6rem}.md-tabs__item--active .md-tabs__link{color:inherit;opacity:1}.md-tabs__link{backface-visibility:hidden;display:flex;font-size:.7rem;margin-top:.8rem;opacity:.7;outline-color:var(--md-accent-fg-color);outline-offset:.2rem;transition:transform .4s cubic-bezier(.1,.7,.1,1),opacity .25s}.md-tabs__link:focus,.md-tabs__link:hover{color:inherit;opacity:1}[dir=ltr] .md-tabs__link svg{margin-right:.4rem}[dir=rtl] .md-tabs__link svg{margin-left:.4rem}.md-tabs__link svg{fill:currentcolor;height:1.3em}.md-tabs__item:nth-child(2) .md-tabs__link{transition-delay:20ms}.md-tabs__item:nth-child(3) .md-tabs__link{transition-delay:40ms}.md-tabs__item:nth-child(4) .md-tabs__link{transition-delay:60ms}.md-tabs__item:nth-child(5) .md-tabs__link{transition-delay:80ms}.md-tabs__item:nth-child(6) .md-tabs__link{transition-delay:.1s}.md-tabs__item:nth-child(7) .md-tabs__link{transition-delay:.12s}.md-tabs__item:nth-child(8) .md-tabs__link{transition-delay:.14s}.md-tabs__item:nth-child(9) .md-tabs__link{transition-delay:.16s}.md-tabs__item:nth-child(10) .md-tabs__link{transition-delay:.18s}.md-tabs__item:nth-child(11) .md-tabs__link{transition-delay:.2s}.md-tabs__item:nth-child(12) .md-tabs__link{transition-delay:.22s}.md-tabs__item:nth-child(13) .md-tabs__link{transition-delay:.24s}.md-tabs__item:nth-child(14) .md-tabs__link{transition-delay:.26s}.md-tabs__item:nth-child(15) .md-tabs__link{transition-delay:.28s}.md-tabs__item:nth-child(16) .md-tabs__link{transition-delay:.3s}.md-tabs[hidden] .md-tabs__link{opacity:0;transform:translateY(50%);transition:transform 0ms .1s,opacity .1s}:root{--md-tag-icon:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .md-tags:not([hidden]){display:inline-flex;flex-wrap:wrap;gap:.5em;margin-bottom:.75em;margin-top:-.125em}.md-typeset .md-tag{align-items:center;background:var(--md-default-fg-color--lightest);border-radius:2.4rem;display:inline-flex;font-size:.64rem;font-size:min(.8em,.64rem);font-weight:700;gap:.5em;letter-spacing:normal;line-height:1.6;padding:.3125em .78125em}.md-typeset .md-tag[href]{-webkit-tap-highlight-color:transparent;color:inherit;outline:none;transition:color 125ms,background-color 125ms}.md-typeset .md-tag[href]:focus,.md-typeset .md-tag[href]:hover{background-color:var(--md-accent-fg-color);color:var(--md-accent-bg-color)}[id]>.md-typeset .md-tag{vertical-align:text-top}.md-typeset .md-tag-shadow{opacity:.5}.md-typeset .md-tag-icon:before{background-color:var(--md-default-fg-color--lighter);content:"";display:inline-block;height:1.2em;-webkit-mask-image:var(--md-tag-icon);mask-image:var(--md-tag-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;transition:background-color 125ms;vertical-align:text-bottom;width:1.2em}.md-typeset .md-tag-icon[href]:focus:before,.md-typeset .md-tag-icon[href]:hover:before{background-color:var(--md-accent-bg-color)}@keyframes pulse{0%{transform:scale(.95)}75%{transform:scale(1)}to{transform:scale(.95)}}:root{--md-annotation-bg-icon:url('data:image/svg+xml;charset=utf-8,');--md-annotation-icon:url('data:image/svg+xml;charset=utf-8,')}.md-tooltip{backface-visibility:hidden;background-color:var(--md-default-bg-color);border-radius:.1rem;box-shadow:var(--md-shadow-z2);color:var(--md-default-fg-color);font-family:var(--md-text-font-family);left:clamp(var(--md-tooltip-0,0rem) + .8rem,var(--md-tooltip-x),100vw + var(--md-tooltip-0,0rem) + .8rem - var(--md-tooltip-width) - 2 * .8rem);max-width:calc(100vw - 1.6rem);opacity:0;position:absolute;top:var(--md-tooltip-y);transform:translateY(-.4rem);transition:transform 0ms .25s,opacity .25s,z-index .25s;width:var(--md-tooltip-width);z-index:0}.md-tooltip--active{opacity:1;transform:translateY(0);transition:transform .25s cubic-bezier(.1,.7,.1,1),opacity .25s,z-index 0ms;z-index:2}.md-tooltip--inline{font-weight:700;-webkit-user-select:none;user-select:none;width:auto}.md-tooltip--inline:not(.md-tooltip--active){transform:translateY(.2rem) scale(.9)}.md-tooltip--inline .md-tooltip__inner{font-size:.5rem;padding:.2rem .4rem}[hidden]+.md-tooltip--inline{display:none}.focus-visible>.md-tooltip,.md-tooltip:target{outline:var(--md-accent-fg-color) auto}.md-tooltip__inner{font-size:.64rem;padding:.8rem}.md-tooltip__inner.md-typeset>:first-child{margin-top:0}.md-tooltip__inner.md-typeset>:last-child{margin-bottom:0}.md-annotation{font-style:normal;font-weight:400;outline:none;text-align:initial;vertical-align:text-bottom;white-space:normal}[dir=rtl] .md-annotation{direction:rtl}code .md-annotation{font-family:var(--md-code-font-family);font-size:inherit}.md-annotation:not([hidden]){display:inline-block;line-height:1.25}.md-annotation__index{border-radius:.01px;cursor:pointer;display:inline-block;margin-left:.4ch;margin-right:.4ch;outline:none;overflow:hidden;position:relative;-webkit-user-select:none;user-select:none;vertical-align:text-top;z-index:0}.md-annotation .md-annotation__index{transition:z-index .25s}@media screen{.md-annotation__index{width:2.2ch}[data-md-visible]>.md-annotation__index{animation:pulse 2s infinite}.md-annotation__index:before{background:var(--md-default-bg-color);-webkit-mask-image:var(--md-annotation-bg-icon);mask-image:var(--md-annotation-bg-icon)}.md-annotation__index:after,.md-annotation__index:before{content:"";height:2.2ch;-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;position:absolute;top:-.1ch;width:2.2ch;z-index:-1}.md-annotation__index:after{background-color:var(--md-default-fg-color--lighter);-webkit-mask-image:var(--md-annotation-icon);mask-image:var(--md-annotation-icon);transform:scale(1.0001);transition:background-color .25s,transform .25s}.md-tooltip--active+.md-annotation__index:after{transform:rotate(45deg)}.md-tooltip--active+.md-annotation__index:after,:hover>.md-annotation__index:after{background-color:var(--md-accent-fg-color)}}.md-tooltip--active+.md-annotation__index{animation-play-state:paused;transition-duration:0ms;z-index:2}.md-annotation__index [data-md-annotation-id]{display:inline-block}@media print{.md-annotation__index [data-md-annotation-id]{background:var(--md-default-fg-color--lighter);border-radius:2ch;color:var(--md-default-bg-color);font-weight:700;padding:0 .6ch;white-space:nowrap}.md-annotation__index [data-md-annotation-id]:after{content:attr(data-md-annotation-id)}}.md-typeset .md-annotation-list{counter-reset:annotation;list-style:none!important}.md-typeset .md-annotation-list li{position:relative}[dir=ltr] .md-typeset .md-annotation-list li:before{left:-2.125em}[dir=rtl] .md-typeset .md-annotation-list li:before{right:-2.125em}.md-typeset .md-annotation-list li:before{background:var(--md-default-fg-color--lighter);border-radius:2ch;color:var(--md-default-bg-color);content:counter(annotation);counter-increment:annotation;font-size:.8875em;font-weight:700;height:2ch;line-height:1.25;min-width:2ch;padding:0 .6ch;position:absolute;text-align:center;top:.25em}:root{--md-tooltip-width:20rem;--md-tooltip-tail:0.3rem}.md-tooltip2{backface-visibility:hidden;color:var(--md-default-fg-color);font-family:var(--md-text-font-family);opacity:0;pointer-events:none;position:absolute;top:calc(var(--md-tooltip-host-y) + var(--md-tooltip-y));transform:translateY(-.4rem);transform-origin:calc(var(--md-tooltip-host-x) + var(--md-tooltip-x)) 0;transition:transform 0ms .25s,opacity .25s,z-index .25s;width:100%;z-index:0}.md-tooltip2:before{border-left:var(--md-tooltip-tail) solid #0000;border-right:var(--md-tooltip-tail) solid #0000;content:"";display:block;left:clamp(1.5 * .8rem,var(--md-tooltip-host-x) + var(--md-tooltip-x) - var(--md-tooltip-tail),100vw - 2 * var(--md-tooltip-tail) - 1.5 * .8rem);position:absolute;z-index:1}.md-tooltip2--top:before{border-top:var(--md-tooltip-tail) solid var(--md-default-bg-color);bottom:calc(var(--md-tooltip-tail)*-1 + .025rem);filter:drop-shadow(0 1px 0 hsla(0,0%,0%,.05))}.md-tooltip2--bottom:before{border-bottom:var(--md-tooltip-tail) solid var(--md-default-bg-color);filter:drop-shadow(0 -1px 0 hsla(0,0%,0%,.05));top:calc(var(--md-tooltip-tail)*-1 + .025rem)}.md-tooltip2--active{opacity:1;transform:translateY(0);transition:transform .4s cubic-bezier(0,1,.5,1),opacity .25s,z-index 0ms;z-index:2}.md-tooltip2__inner{scrollbar-gutter:stable;background-color:var(--md-default-bg-color);border-radius:.1rem;box-shadow:var(--md-shadow-z2);left:clamp(.8rem,var(--md-tooltip-host-x) - .8rem,100vw - var(--md-tooltip-width) - .8rem);max-height:40vh;max-width:calc(100vw - 1.6rem);position:relative;scrollbar-width:thin}.md-tooltip2__inner::-webkit-scrollbar{height:.2rem;width:.2rem}.md-tooltip2__inner::-webkit-scrollbar-thumb{background-color:var(--md-default-fg-color--lighter)}.md-tooltip2__inner::-webkit-scrollbar-thumb:hover{background-color:var(--md-accent-fg-color)}[role=dialog]>.md-tooltip2__inner{font-size:.64rem;overflow:auto;padding:0 .8rem;pointer-events:auto;width:var(--md-tooltip-width)}[role=dialog]>.md-tooltip2__inner:after,[role=dialog]>.md-tooltip2__inner:before{content:"";display:block;height:.8rem;position:sticky;width:100%;z-index:10}[role=dialog]>.md-tooltip2__inner:before{background:linear-gradient(var(--md-default-bg-color),#0000 75%);top:0}[role=dialog]>.md-tooltip2__inner:after{background:linear-gradient(#0000,var(--md-default-bg-color) 75%);bottom:0}[role=tooltip]>.md-tooltip2__inner{font-size:.5rem;font-weight:700;left:clamp(.8rem,var(--md-tooltip-host-x) + var(--md-tooltip-x) - var(--md-tooltip-width)/2,100vw - var(--md-tooltip-width) - .8rem);max-width:min(100vw - 2 * .8rem,400px);padding:.2rem .4rem;-webkit-user-select:none;user-select:none;width:fit-content}.md-tooltip2__inner.md-typeset>:first-child{margin-top:0}.md-tooltip2__inner.md-typeset>:last-child{margin-bottom:0}[dir=ltr] .md-top{margin-left:50%}[dir=rtl] .md-top{margin-right:50%}.md-top{background-color:var(--md-default-bg-color);border-radius:1.6rem;box-shadow:var(--md-shadow-z2);color:var(--md-default-fg-color--light);cursor:pointer;display:block;font-size:.7rem;outline:none;padding:.4rem .8rem;position:fixed;top:3.2rem;transform:translate(-50%);transition:color 125ms,background-color 125ms,transform 125ms cubic-bezier(.4,0,.2,1),opacity 125ms;z-index:2}@media print{.md-top{display:none}}[dir=rtl] .md-top{transform:translate(50%)}.md-top[hidden]{opacity:0;pointer-events:none;transform:translate(-50%,.2rem);transition-duration:0ms}[dir=rtl] .md-top[hidden]{transform:translate(50%,.2rem)}.md-top:focus,.md-top:hover{background-color:var(--md-accent-fg-color);color:var(--md-accent-bg-color)}.md-top svg{display:inline-block;vertical-align:-.5em}@keyframes hoverfix{0%{pointer-events:none}}:root{--md-version-icon:url('data:image/svg+xml;charset=utf-8,')}.md-version{flex-shrink:0;font-size:.8rem;height:2.4rem}[dir=ltr] .md-version__current{margin-left:1.4rem;margin-right:.4rem}[dir=rtl] .md-version__current{margin-left:.4rem;margin-right:1.4rem}.md-version__current{color:inherit;cursor:pointer;outline:none;position:relative;top:.05rem}[dir=ltr] .md-version__current:after{margin-left:.4rem}[dir=rtl] .md-version__current:after{margin-right:.4rem}.md-version__current:after{background-color:currentcolor;content:"";display:inline-block;height:.6rem;-webkit-mask-image:var(--md-version-icon);mask-image:var(--md-version-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:.4rem}.md-version__alias{margin-left:.3rem;opacity:.7}.md-version__list{background-color:var(--md-default-bg-color);border-radius:.1rem;box-shadow:var(--md-shadow-z2);color:var(--md-default-fg-color);list-style-type:none;margin:.2rem .8rem;max-height:0;opacity:0;overflow:auto;padding:0;position:absolute;scroll-snap-type:y mandatory;top:.15rem;transition:max-height 0ms .5s,opacity .25s .25s;z-index:3}.md-version:focus-within .md-version__list,.md-version:hover .md-version__list{max-height:10rem;opacity:1;transition:max-height 0ms,opacity .25s}@media (hover:none),(pointer:coarse){.md-version:hover .md-version__list{animation:hoverfix .25s forwards}.md-version:focus-within .md-version__list{animation:none}}.md-version__item{line-height:1.8rem}[dir=ltr] .md-version__link{padding-left:.6rem;padding-right:1.2rem}[dir=rtl] .md-version__link{padding-left:1.2rem;padding-right:.6rem}.md-version__link{cursor:pointer;display:block;outline:none;scroll-snap-align:start;transition:color .25s,background-color .25s;white-space:nowrap;width:100%}.md-version__link:focus,.md-version__link:hover{color:var(--md-accent-fg-color)}.md-version__link:focus{background-color:var(--md-default-fg-color--lightest)}:root{--md-admonition-icon--note:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--abstract:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--info:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--tip:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--success:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--question:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--warning:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--failure:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--danger:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--bug:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--example:url('data:image/svg+xml;charset=utf-8,');--md-admonition-icon--quote:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .admonition,.md-typeset details{background-color:var(--md-admonition-bg-color);border:.075rem solid #448aff;border-radius:.2rem;box-shadow:var(--md-shadow-z1);color:var(--md-admonition-fg-color);display:flow-root;font-size:.64rem;margin:1.5625em 0;padding:0 .6rem;page-break-inside:avoid;transition:box-shadow 125ms}@media print{.md-typeset .admonition,.md-typeset details{box-shadow:none}}.md-typeset .admonition:focus-within,.md-typeset details:focus-within{box-shadow:0 0 0 .2rem #448aff1a}.md-typeset .admonition>*,.md-typeset details>*{box-sizing:border-box}.md-typeset .admonition .admonition,.md-typeset .admonition details,.md-typeset details .admonition,.md-typeset details details{margin-bottom:1em;margin-top:1em}.md-typeset .admonition .md-typeset__scrollwrap,.md-typeset details .md-typeset__scrollwrap{margin:1em -.6rem}.md-typeset .admonition .md-typeset__table,.md-typeset details .md-typeset__table{padding:0 .6rem}.md-typeset .admonition>.tabbed-set:only-child,.md-typeset details>.tabbed-set:only-child{margin-top:0}html .md-typeset .admonition>:last-child,html .md-typeset details>:last-child{margin-bottom:.6rem}[dir=ltr] .md-typeset .admonition-title,[dir=ltr] .md-typeset summary{padding-left:2rem;padding-right:.6rem}[dir=rtl] .md-typeset .admonition-title,[dir=rtl] .md-typeset summary{padding-left:.6rem;padding-right:2rem}[dir=ltr] .md-typeset .admonition-title,[dir=ltr] .md-typeset summary{border-left-width:.2rem}[dir=rtl] .md-typeset .admonition-title,[dir=rtl] .md-typeset summary{border-right-width:.2rem}[dir=ltr] .md-typeset .admonition-title,[dir=ltr] .md-typeset summary{border-top-left-radius:.1rem}[dir=ltr] .md-typeset .admonition-title,[dir=ltr] .md-typeset summary,[dir=rtl] .md-typeset .admonition-title,[dir=rtl] .md-typeset summary{border-top-right-radius:.1rem}[dir=rtl] .md-typeset .admonition-title,[dir=rtl] .md-typeset summary{border-top-left-radius:.1rem}.md-typeset .admonition-title,.md-typeset summary{background-color:#448aff1a;border:none;font-weight:700;margin:0 -.6rem;padding-bottom:.4rem;padding-top:.4rem;position:relative}html .md-typeset .admonition-title:last-child,html .md-typeset summary:last-child{margin-bottom:0}[dir=ltr] .md-typeset .admonition-title:before,[dir=ltr] .md-typeset summary:before{left:.6rem}[dir=rtl] .md-typeset .admonition-title:before,[dir=rtl] .md-typeset summary:before{right:.6rem}.md-typeset .admonition-title:before,.md-typeset summary:before{background-color:#448aff;content:"";height:1rem;-webkit-mask-image:var(--md-admonition-icon--note);mask-image:var(--md-admonition-icon--note);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;position:absolute;top:.625em;width:1rem}.md-typeset .admonition-title code,.md-typeset summary code{box-shadow:0 0 0 .05rem var(--md-default-fg-color--lightest)}.md-typeset .admonition.note,.md-typeset details.note{border-color:#448aff}.md-typeset .admonition.note:focus-within,.md-typeset details.note:focus-within{box-shadow:0 0 0 .2rem #448aff1a}.md-typeset .note>.admonition-title,.md-typeset .note>summary{background-color:#448aff1a}.md-typeset .note>.admonition-title:before,.md-typeset .note>summary:before{background-color:#448aff;-webkit-mask-image:var(--md-admonition-icon--note);mask-image:var(--md-admonition-icon--note)}.md-typeset .note>.admonition-title:after,.md-typeset .note>summary:after{color:#448aff}.md-typeset .admonition.abstract,.md-typeset details.abstract{border-color:#00b0ff}.md-typeset .admonition.abstract:focus-within,.md-typeset details.abstract:focus-within{box-shadow:0 0 0 .2rem #00b0ff1a}.md-typeset .abstract>.admonition-title,.md-typeset .abstract>summary{background-color:#00b0ff1a}.md-typeset .abstract>.admonition-title:before,.md-typeset .abstract>summary:before{background-color:#00b0ff;-webkit-mask-image:var(--md-admonition-icon--abstract);mask-image:var(--md-admonition-icon--abstract)}.md-typeset .abstract>.admonition-title:after,.md-typeset .abstract>summary:after{color:#00b0ff}.md-typeset .admonition.info,.md-typeset details.info{border-color:#00b8d4}.md-typeset .admonition.info:focus-within,.md-typeset details.info:focus-within{box-shadow:0 0 0 .2rem #00b8d41a}.md-typeset .info>.admonition-title,.md-typeset .info>summary{background-color:#00b8d41a}.md-typeset .info>.admonition-title:before,.md-typeset .info>summary:before{background-color:#00b8d4;-webkit-mask-image:var(--md-admonition-icon--info);mask-image:var(--md-admonition-icon--info)}.md-typeset .info>.admonition-title:after,.md-typeset .info>summary:after{color:#00b8d4}.md-typeset .admonition.tip,.md-typeset details.tip{border-color:#00bfa5}.md-typeset .admonition.tip:focus-within,.md-typeset details.tip:focus-within{box-shadow:0 0 0 .2rem #00bfa51a}.md-typeset .tip>.admonition-title,.md-typeset .tip>summary{background-color:#00bfa51a}.md-typeset .tip>.admonition-title:before,.md-typeset .tip>summary:before{background-color:#00bfa5;-webkit-mask-image:var(--md-admonition-icon--tip);mask-image:var(--md-admonition-icon--tip)}.md-typeset .tip>.admonition-title:after,.md-typeset .tip>summary:after{color:#00bfa5}.md-typeset .admonition.success,.md-typeset details.success{border-color:#00c853}.md-typeset .admonition.success:focus-within,.md-typeset details.success:focus-within{box-shadow:0 0 0 .2rem #00c8531a}.md-typeset .success>.admonition-title,.md-typeset .success>summary{background-color:#00c8531a}.md-typeset .success>.admonition-title:before,.md-typeset .success>summary:before{background-color:#00c853;-webkit-mask-image:var(--md-admonition-icon--success);mask-image:var(--md-admonition-icon--success)}.md-typeset .success>.admonition-title:after,.md-typeset .success>summary:after{color:#00c853}.md-typeset .admonition.question,.md-typeset details.question{border-color:#64dd17}.md-typeset .admonition.question:focus-within,.md-typeset details.question:focus-within{box-shadow:0 0 0 .2rem #64dd171a}.md-typeset .question>.admonition-title,.md-typeset .question>summary{background-color:#64dd171a}.md-typeset .question>.admonition-title:before,.md-typeset .question>summary:before{background-color:#64dd17;-webkit-mask-image:var(--md-admonition-icon--question);mask-image:var(--md-admonition-icon--question)}.md-typeset .question>.admonition-title:after,.md-typeset .question>summary:after{color:#64dd17}.md-typeset .admonition.warning,.md-typeset details.warning{border-color:#ff9100}.md-typeset .admonition.warning:focus-within,.md-typeset details.warning:focus-within{box-shadow:0 0 0 .2rem #ff91001a}.md-typeset .warning>.admonition-title,.md-typeset .warning>summary{background-color:#ff91001a}.md-typeset .warning>.admonition-title:before,.md-typeset .warning>summary:before{background-color:#ff9100;-webkit-mask-image:var(--md-admonition-icon--warning);mask-image:var(--md-admonition-icon--warning)}.md-typeset .warning>.admonition-title:after,.md-typeset .warning>summary:after{color:#ff9100}.md-typeset .admonition.failure,.md-typeset details.failure{border-color:#ff5252}.md-typeset .admonition.failure:focus-within,.md-typeset details.failure:focus-within{box-shadow:0 0 0 .2rem #ff52521a}.md-typeset .failure>.admonition-title,.md-typeset .failure>summary{background-color:#ff52521a}.md-typeset .failure>.admonition-title:before,.md-typeset .failure>summary:before{background-color:#ff5252;-webkit-mask-image:var(--md-admonition-icon--failure);mask-image:var(--md-admonition-icon--failure)}.md-typeset .failure>.admonition-title:after,.md-typeset .failure>summary:after{color:#ff5252}.md-typeset .admonition.danger,.md-typeset details.danger{border-color:#ff1744}.md-typeset .admonition.danger:focus-within,.md-typeset details.danger:focus-within{box-shadow:0 0 0 .2rem #ff17441a}.md-typeset .danger>.admonition-title,.md-typeset .danger>summary{background-color:#ff17441a}.md-typeset .danger>.admonition-title:before,.md-typeset .danger>summary:before{background-color:#ff1744;-webkit-mask-image:var(--md-admonition-icon--danger);mask-image:var(--md-admonition-icon--danger)}.md-typeset .danger>.admonition-title:after,.md-typeset .danger>summary:after{color:#ff1744}.md-typeset .admonition.bug,.md-typeset details.bug{border-color:#f50057}.md-typeset .admonition.bug:focus-within,.md-typeset details.bug:focus-within{box-shadow:0 0 0 .2rem #f500571a}.md-typeset .bug>.admonition-title,.md-typeset .bug>summary{background-color:#f500571a}.md-typeset .bug>.admonition-title:before,.md-typeset .bug>summary:before{background-color:#f50057;-webkit-mask-image:var(--md-admonition-icon--bug);mask-image:var(--md-admonition-icon--bug)}.md-typeset .bug>.admonition-title:after,.md-typeset .bug>summary:after{color:#f50057}.md-typeset .admonition.example,.md-typeset details.example{border-color:#7c4dff}.md-typeset .admonition.example:focus-within,.md-typeset details.example:focus-within{box-shadow:0 0 0 .2rem #7c4dff1a}.md-typeset .example>.admonition-title,.md-typeset .example>summary{background-color:#7c4dff1a}.md-typeset .example>.admonition-title:before,.md-typeset .example>summary:before{background-color:#7c4dff;-webkit-mask-image:var(--md-admonition-icon--example);mask-image:var(--md-admonition-icon--example)}.md-typeset .example>.admonition-title:after,.md-typeset .example>summary:after{color:#7c4dff}.md-typeset .admonition.quote,.md-typeset details.quote{border-color:#9e9e9e}.md-typeset .admonition.quote:focus-within,.md-typeset details.quote:focus-within{box-shadow:0 0 0 .2rem #9e9e9e1a}.md-typeset .quote>.admonition-title,.md-typeset .quote>summary{background-color:#9e9e9e1a}.md-typeset .quote>.admonition-title:before,.md-typeset .quote>summary:before{background-color:#9e9e9e;-webkit-mask-image:var(--md-admonition-icon--quote);mask-image:var(--md-admonition-icon--quote)}.md-typeset .quote>.admonition-title:after,.md-typeset .quote>summary:after{color:#9e9e9e}:root{--md-footnotes-icon:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .footnote{color:var(--md-default-fg-color--light);font-size:.64rem}[dir=ltr] .md-typeset .footnote>ol{margin-left:0}[dir=rtl] .md-typeset .footnote>ol{margin-right:0}.md-typeset .footnote>ol>li{transition:color 125ms}.md-typeset .footnote>ol>li:target{color:var(--md-default-fg-color)}.md-typeset .footnote>ol>li:focus-within .footnote-backref{opacity:1;transform:translateX(0);transition:none}.md-typeset .footnote>ol>li:hover .footnote-backref,.md-typeset .footnote>ol>li:target .footnote-backref{opacity:1;transform:translateX(0)}.md-typeset .footnote>ol>li>:first-child{margin-top:0}.md-typeset .footnote-ref{font-size:.75em;font-weight:700}html .md-typeset .footnote-ref{outline-offset:.1rem}.md-typeset [id^="fnref:"]:target>.footnote-ref{outline:auto}.md-typeset .footnote-backref{color:var(--md-typeset-a-color);display:inline-block;font-size:0;opacity:0;transform:translateX(.25rem);transition:color .25s,transform .25s .25s,opacity 125ms .25s;vertical-align:text-bottom}@media print{.md-typeset .footnote-backref{color:var(--md-typeset-a-color);opacity:1;transform:translateX(0)}}[dir=rtl] .md-typeset .footnote-backref{transform:translateX(-.25rem)}.md-typeset .footnote-backref:hover{color:var(--md-accent-fg-color)}.md-typeset .footnote-backref:before{background-color:currentcolor;content:"";display:inline-block;height:.8rem;-webkit-mask-image:var(--md-footnotes-icon);mask-image:var(--md-footnotes-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;width:.8rem}[dir=rtl] .md-typeset .footnote-backref:before{transform:scaleX(-1)}[dir=ltr] .md-typeset .headerlink{margin-left:.5rem}[dir=rtl] .md-typeset .headerlink{margin-right:.5rem}.md-typeset .headerlink{color:var(--md-default-fg-color--lighter);display:inline-block;opacity:0;transition:color .25s,opacity 125ms}@media print{.md-typeset .headerlink{display:none}}.md-typeset .headerlink:focus,.md-typeset :hover>.headerlink,.md-typeset :target>.headerlink{opacity:1;transition:color .25s,opacity 125ms}.md-typeset .headerlink:focus,.md-typeset .headerlink:hover,.md-typeset :target>.headerlink{color:var(--md-accent-fg-color)}.md-typeset :target{--md-scroll-margin:3.6rem;--md-scroll-offset:0rem;scroll-margin-top:calc(var(--md-scroll-margin) - var(--md-scroll-offset))}@media screen and (min-width:76.25em){.md-header--lifted~.md-container .md-typeset :target{--md-scroll-margin:6rem}}.md-typeset h1:target,.md-typeset h2:target,.md-typeset h3:target{--md-scroll-offset:0.2rem}.md-typeset h4:target{--md-scroll-offset:0.15rem}.md-typeset div.arithmatex{overflow:auto}@media screen and (max-width:44.984375em){.md-typeset div.arithmatex{margin:0 -.8rem}.md-typeset div.arithmatex>*{width:min-content}}.md-typeset div.arithmatex>*{margin-left:auto!important;margin-right:auto!important;padding:0 .8rem;touch-action:auto}.md-typeset div.arithmatex>* mjx-container{margin:0!important}.md-typeset div.arithmatex mjx-assistive-mml{height:0}.md-typeset .katex-html svg{max-width:revert}.md-typeset del.critic{background-color:var(--md-typeset-del-color)}.md-typeset del.critic,.md-typeset ins.critic{-webkit-box-decoration-break:clone;box-decoration-break:clone}.md-typeset ins.critic{background-color:var(--md-typeset-ins-color)}.md-typeset .critic.comment{-webkit-box-decoration-break:clone;box-decoration-break:clone;color:var(--md-code-hl-comment-color)}.md-typeset .critic.comment:before{content:"/* "}.md-typeset .critic.comment:after{content:" */"}.md-typeset .critic.block{box-shadow:none;display:block;margin:1em 0;overflow:auto;padding-left:.8rem;padding-right:.8rem}.md-typeset .critic.block>:first-child{margin-top:.5em}.md-typeset .critic.block>:last-child{margin-bottom:.5em}:root{--md-details-icon:url('data:image/svg+xml;charset=utf-8,')}.md-typeset details{display:flow-root;overflow:visible;padding-top:0}.md-typeset details[open]>summary:after{transform:rotate(90deg)}.md-typeset details:not([open]){box-shadow:none;padding-bottom:0}.md-typeset details:not([open])>summary{border-radius:.1rem}[dir=ltr] .md-typeset summary{padding-right:1.8rem}[dir=rtl] .md-typeset summary{padding-left:1.8rem}[dir=ltr] .md-typeset summary{border-top-left-radius:.1rem}[dir=ltr] .md-typeset summary,[dir=rtl] .md-typeset summary{border-top-right-radius:.1rem}[dir=rtl] .md-typeset summary{border-top-left-radius:.1rem}.md-typeset summary{cursor:pointer;display:block;min-height:1rem;overflow:hidden}.md-typeset summary.focus-visible{outline-color:var(--md-accent-fg-color);outline-offset:.2rem}.md-typeset summary:not(.focus-visible){-webkit-tap-highlight-color:transparent;outline:none}[dir=ltr] .md-typeset summary:after{right:.4rem}[dir=rtl] .md-typeset summary:after{left:.4rem}.md-typeset summary:after{background-color:currentcolor;content:"";height:1rem;-webkit-mask-image:var(--md-details-icon);mask-image:var(--md-details-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;position:absolute;top:.625em;transform:rotate(0deg);transition:transform .25s;width:1rem}[dir=rtl] .md-typeset summary:after{transform:rotate(180deg)}.md-typeset summary::marker{display:none}.md-typeset summary::-webkit-details-marker{display:none}.md-typeset .emojione,.md-typeset .gemoji,.md-typeset .twemoji{--md-icon-size:1.125em;display:inline-flex;height:var(--md-icon-size);vertical-align:text-top}.md-typeset .emojione svg,.md-typeset .gemoji svg,.md-typeset .twemoji svg{fill:currentcolor;max-height:100%;width:var(--md-icon-size)}.md-typeset .lg,.md-typeset .xl,.md-typeset .xxl,.md-typeset .xxxl{vertical-align:text-bottom}.md-typeset .middle{vertical-align:middle}.md-typeset .lg{--md-icon-size:1.5em}.md-typeset .xl{--md-icon-size:2.25em}.md-typeset .xxl{--md-icon-size:3em}.md-typeset .xxxl{--md-icon-size:4em}.highlight .o,.highlight .ow{color:var(--md-code-hl-operator-color)}.highlight .p{color:var(--md-code-hl-punctuation-color)}.highlight .cpf,.highlight .l,.highlight .s,.highlight .s1,.highlight .s2,.highlight .sb,.highlight .sc,.highlight .si,.highlight .ss{color:var(--md-code-hl-string-color)}.highlight .cp,.highlight .se,.highlight .sh,.highlight .sr,.highlight .sx{color:var(--md-code-hl-special-color)}.highlight .il,.highlight .m,.highlight .mb,.highlight .mf,.highlight .mh,.highlight .mi,.highlight .mo{color:var(--md-code-hl-number-color)}.highlight .k,.highlight .kd,.highlight .kn,.highlight .kp,.highlight .kr,.highlight .kt{color:var(--md-code-hl-keyword-color)}.highlight .kc,.highlight .n{color:var(--md-code-hl-name-color)}.highlight .bp,.highlight .nb,.highlight .no{color:var(--md-code-hl-constant-color)}.highlight .nc,.highlight .ne,.highlight .nf,.highlight .nn{color:var(--md-code-hl-function-color)}.highlight .nd,.highlight .ni,.highlight .nl,.highlight .nt{color:var(--md-code-hl-keyword-color)}.highlight .c,.highlight .c1,.highlight .ch,.highlight .cm,.highlight .cs,.highlight .sd{color:var(--md-code-hl-comment-color)}.highlight .na,.highlight .nv,.highlight .vc,.highlight .vg,.highlight .vi{color:var(--md-code-hl-variable-color)}.highlight .ge,.highlight .gh,.highlight .go,.highlight .gp,.highlight .gr,.highlight .gs,.highlight .gt,.highlight .gu{color:var(--md-code-hl-generic-color)}.highlight .gd,.highlight .gi{border-radius:.1rem;margin:0 -.125em;padding:0 .125em}.highlight .gd{background-color:var(--md-typeset-del-color)}.highlight .gi{background-color:var(--md-typeset-ins-color)}.highlight .hll{background-color:var(--md-code-hl-color--light);box-shadow:2px 0 0 0 var(--md-code-hl-color) inset;display:block;margin:0 -1.1764705882em;padding:0 1.1764705882em}.highlight span.filename{background-color:var(--md-code-bg-color);border-bottom:.05rem solid var(--md-default-fg-color--lightest);border-top-left-radius:.1rem;border-top-right-radius:.1rem;display:flow-root;font-size:.85em;font-weight:700;margin-top:1em;padding:.6617647059em 1.1764705882em;position:relative}.highlight span.filename+pre{margin-top:0}.highlight span.filename+pre>code{border-top-left-radius:0;border-top-right-radius:0}.highlight [data-linenos]:before{background-color:var(--md-code-bg-color);box-shadow:-.05rem 0 var(--md-default-fg-color--lightest) inset;color:var(--md-default-fg-color--light);content:attr(data-linenos);float:left;left:-1.1764705882em;margin-left:-1.1764705882em;margin-right:1.1764705882em;padding-left:1.1764705882em;position:sticky;-webkit-user-select:none;user-select:none;z-index:3}.highlight code a[id]{position:absolute;visibility:hidden}.highlight code[data-md-copying]{display:initial}.highlight code[data-md-copying] .hll{display:contents}.highlight code[data-md-copying] .md-annotation{display:none}.highlighttable{display:flow-root}.highlighttable tbody,.highlighttable td{display:block;padding:0}.highlighttable tr{display:flex}.highlighttable pre{margin:0}.highlighttable th.filename{flex-grow:1;padding:0;text-align:left}.highlighttable th.filename span.filename{margin-top:0}.highlighttable .linenos{background-color:var(--md-code-bg-color);border-bottom-left-radius:.1rem;border-top-left-radius:.1rem;font-size:.85em;padding:.7720588235em 0 .7720588235em 1.1764705882em;-webkit-user-select:none;user-select:none}.highlighttable .linenodiv{box-shadow:-.05rem 0 var(--md-default-fg-color--lightest) inset}.highlighttable .linenodiv pre{color:var(--md-default-fg-color--light);text-align:right}.highlighttable .linenodiv span[class]{padding-right:.5882352941em}.highlighttable .code{flex:1;min-width:0}.linenodiv a{color:inherit}.md-typeset .highlighttable{direction:ltr;margin:1em 0}.md-typeset .highlighttable>tbody>tr>.code>div>pre>code{border-bottom-left-radius:0;border-top-left-radius:0}.md-typeset .highlight+.result{border:.05rem solid var(--md-code-bg-color);border-bottom-left-radius:.1rem;border-bottom-right-radius:.1rem;border-top-width:.1rem;margin-top:-1.125em;overflow:visible;padding:0 1em}.md-typeset .highlight+.result:after{clear:both;content:"";display:block}@media screen and (max-width:44.984375em){.md-content__inner>.highlight{margin:1em -.8rem}.md-content__inner>.highlight>.filename,.md-content__inner>.highlight>.highlighttable>tbody>tr>.code>div>pre>code,.md-content__inner>.highlight>.highlighttable>tbody>tr>.filename span.filename,.md-content__inner>.highlight>.highlighttable>tbody>tr>.linenos,.md-content__inner>.highlight>pre>code{border-radius:0}.md-content__inner>.highlight+.result{border-left-width:0;border-radius:0;border-right-width:0;margin-left:-.8rem;margin-right:-.8rem}}.md-typeset .keys kbd:after,.md-typeset .keys kbd:before{-moz-osx-font-smoothing:initial;-webkit-font-smoothing:initial;color:inherit;margin:0;position:relative}.md-typeset .keys span{color:var(--md-default-fg-color--light);padding:0 .2em}.md-typeset .keys .key-alt:before,.md-typeset .keys .key-left-alt:before,.md-typeset .keys .key-right-alt:before{content:"⎇";padding-right:.4em}.md-typeset .keys .key-command:before,.md-typeset .keys .key-left-command:before,.md-typeset .keys .key-right-command:before{content:"⌘";padding-right:.4em}.md-typeset .keys .key-control:before,.md-typeset .keys .key-left-control:before,.md-typeset .keys .key-right-control:before{content:"⌃";padding-right:.4em}.md-typeset .keys .key-left-meta:before,.md-typeset .keys .key-meta:before,.md-typeset .keys .key-right-meta:before{content:"◆";padding-right:.4em}.md-typeset .keys .key-left-option:before,.md-typeset .keys .key-option:before,.md-typeset .keys .key-right-option:before{content:"⌥";padding-right:.4em}.md-typeset .keys .key-left-shift:before,.md-typeset .keys .key-right-shift:before,.md-typeset .keys .key-shift:before{content:"⇧";padding-right:.4em}.md-typeset .keys .key-left-super:before,.md-typeset .keys .key-right-super:before,.md-typeset .keys .key-super:before{content:"❖";padding-right:.4em}.md-typeset .keys .key-left-windows:before,.md-typeset .keys .key-right-windows:before,.md-typeset .keys .key-windows:before{content:"⊞";padding-right:.4em}.md-typeset .keys .key-arrow-down:before{content:"↓";padding-right:.4em}.md-typeset .keys .key-arrow-left:before{content:"←";padding-right:.4em}.md-typeset .keys .key-arrow-right:before{content:"→";padding-right:.4em}.md-typeset .keys .key-arrow-up:before{content:"↑";padding-right:.4em}.md-typeset .keys .key-backspace:before{content:"⌫";padding-right:.4em}.md-typeset .keys .key-backtab:before{content:"⇤";padding-right:.4em}.md-typeset .keys .key-caps-lock:before{content:"⇪";padding-right:.4em}.md-typeset .keys .key-clear:before{content:"⌧";padding-right:.4em}.md-typeset .keys .key-context-menu:before{content:"☰";padding-right:.4em}.md-typeset .keys .key-delete:before{content:"⌦";padding-right:.4em}.md-typeset .keys .key-eject:before{content:"⏏";padding-right:.4em}.md-typeset .keys .key-end:before{content:"⤓";padding-right:.4em}.md-typeset .keys .key-escape:before{content:"⎋";padding-right:.4em}.md-typeset .keys .key-home:before{content:"⤒";padding-right:.4em}.md-typeset .keys .key-insert:before{content:"⎀";padding-right:.4em}.md-typeset .keys .key-page-down:before{content:"⇟";padding-right:.4em}.md-typeset .keys .key-page-up:before{content:"⇞";padding-right:.4em}.md-typeset .keys .key-print-screen:before{content:"⎙";padding-right:.4em}.md-typeset .keys .key-tab:after{content:"⇥";padding-left:.4em}.md-typeset .keys .key-num-enter:after{content:"⌤";padding-left:.4em}.md-typeset .keys .key-enter:after{content:"⏎";padding-left:.4em}:root{--md-tabbed-icon--prev:url('data:image/svg+xml;charset=utf-8,');--md-tabbed-icon--next:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .tabbed-set{border-radius:.1rem;display:flex;flex-flow:column wrap;margin:1em 0;position:relative}.md-typeset .tabbed-set>input{height:0;opacity:0;position:absolute;width:0}.md-typeset .tabbed-set>input:target{--md-scroll-offset:0.625em}.md-typeset .tabbed-set>input.focus-visible~.tabbed-labels:before{background-color:var(--md-accent-fg-color)}.md-typeset .tabbed-labels{-ms-overflow-style:none;box-shadow:0 -.05rem var(--md-default-fg-color--lightest) inset;display:flex;max-width:100%;overflow:auto;scrollbar-width:none}@media print{.md-typeset .tabbed-labels{display:contents}}@media screen{.js .md-typeset .tabbed-labels{position:relative}.js .md-typeset .tabbed-labels:before{background:var(--md-default-fg-color);bottom:0;content:"";display:block;height:2px;left:0;position:absolute;transform:translateX(var(--md-indicator-x));transition:width 225ms,background-color .25s,transform .25s;transition-timing-function:cubic-bezier(.4,0,.2,1);width:var(--md-indicator-width)}}.md-typeset .tabbed-labels::-webkit-scrollbar{display:none}.md-typeset .tabbed-labels>label{border-bottom:.1rem solid #0000;border-radius:.1rem .1rem 0 0;color:var(--md-default-fg-color--light);cursor:pointer;flex-shrink:0;font-size:.64rem;font-weight:700;padding:.78125em 1.25em .625em;scroll-margin-inline-start:1rem;transition:background-color .25s,color .25s;white-space:nowrap;width:auto}@media print{.md-typeset .tabbed-labels>label:first-child{order:1}.md-typeset .tabbed-labels>label:nth-child(2){order:2}.md-typeset .tabbed-labels>label:nth-child(3){order:3}.md-typeset .tabbed-labels>label:nth-child(4){order:4}.md-typeset .tabbed-labels>label:nth-child(5){order:5}.md-typeset .tabbed-labels>label:nth-child(6){order:6}.md-typeset .tabbed-labels>label:nth-child(7){order:7}.md-typeset .tabbed-labels>label:nth-child(8){order:8}.md-typeset .tabbed-labels>label:nth-child(9){order:9}.md-typeset .tabbed-labels>label:nth-child(10){order:10}.md-typeset .tabbed-labels>label:nth-child(11){order:11}.md-typeset .tabbed-labels>label:nth-child(12){order:12}.md-typeset .tabbed-labels>label:nth-child(13){order:13}.md-typeset .tabbed-labels>label:nth-child(14){order:14}.md-typeset .tabbed-labels>label:nth-child(15){order:15}.md-typeset .tabbed-labels>label:nth-child(16){order:16}.md-typeset .tabbed-labels>label:nth-child(17){order:17}.md-typeset .tabbed-labels>label:nth-child(18){order:18}.md-typeset .tabbed-labels>label:nth-child(19){order:19}.md-typeset .tabbed-labels>label:nth-child(20){order:20}}.md-typeset .tabbed-labels>label:hover{color:var(--md-default-fg-color)}.md-typeset .tabbed-labels>label>[href]:first-child{color:inherit}.md-typeset .tabbed-labels--linked>label{padding:0}.md-typeset .tabbed-labels--linked>label>a{display:block;padding:.78125em 1.25em .625em}.md-typeset .tabbed-content{width:100%}@media print{.md-typeset .tabbed-content{display:contents}}.md-typeset .tabbed-block{display:none}@media print{.md-typeset .tabbed-block{display:block}.md-typeset .tabbed-block:first-child{order:1}.md-typeset .tabbed-block:nth-child(2){order:2}.md-typeset .tabbed-block:nth-child(3){order:3}.md-typeset .tabbed-block:nth-child(4){order:4}.md-typeset .tabbed-block:nth-child(5){order:5}.md-typeset .tabbed-block:nth-child(6){order:6}.md-typeset .tabbed-block:nth-child(7){order:7}.md-typeset .tabbed-block:nth-child(8){order:8}.md-typeset .tabbed-block:nth-child(9){order:9}.md-typeset .tabbed-block:nth-child(10){order:10}.md-typeset .tabbed-block:nth-child(11){order:11}.md-typeset .tabbed-block:nth-child(12){order:12}.md-typeset .tabbed-block:nth-child(13){order:13}.md-typeset .tabbed-block:nth-child(14){order:14}.md-typeset .tabbed-block:nth-child(15){order:15}.md-typeset .tabbed-block:nth-child(16){order:16}.md-typeset .tabbed-block:nth-child(17){order:17}.md-typeset .tabbed-block:nth-child(18){order:18}.md-typeset .tabbed-block:nth-child(19){order:19}.md-typeset .tabbed-block:nth-child(20){order:20}}.md-typeset .tabbed-block>.highlight:first-child>pre,.md-typeset .tabbed-block>pre:first-child{margin:0}.md-typeset .tabbed-block>.highlight:first-child>pre>code,.md-typeset .tabbed-block>pre:first-child>code{border-top-left-radius:0;border-top-right-radius:0}.md-typeset .tabbed-block>.highlight:first-child>.filename{border-top-left-radius:0;border-top-right-radius:0;margin:0}.md-typeset .tabbed-block>.highlight:first-child>.highlighttable{margin:0}.md-typeset .tabbed-block>.highlight:first-child>.highlighttable>tbody>tr>.filename span.filename,.md-typeset .tabbed-block>.highlight:first-child>.highlighttable>tbody>tr>.linenos{border-top-left-radius:0;border-top-right-radius:0;margin:0}.md-typeset .tabbed-block>.highlight:first-child>.highlighttable>tbody>tr>.code>div>pre>code{border-top-left-radius:0;border-top-right-radius:0}.md-typeset .tabbed-block>.highlight:first-child+.result{margin-top:-.125em}.md-typeset .tabbed-block>.tabbed-set{margin:0}.md-typeset .tabbed-button{align-self:center;border-radius:100%;color:var(--md-default-fg-color--light);cursor:pointer;display:block;height:.9rem;margin-top:.1rem;pointer-events:auto;transition:background-color .25s;width:.9rem}.md-typeset .tabbed-button:hover{background-color:var(--md-accent-fg-color--transparent);color:var(--md-accent-fg-color)}.md-typeset .tabbed-button:after{background-color:currentcolor;content:"";display:block;height:100%;-webkit-mask-image:var(--md-tabbed-icon--prev);mask-image:var(--md-tabbed-icon--prev);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;transition:background-color .25s,transform .25s;width:100%}.md-typeset .tabbed-control{background:linear-gradient(to right,var(--md-default-bg-color) 60%,#0000);display:flex;height:1.9rem;justify-content:start;pointer-events:none;position:absolute;transition:opacity 125ms;width:1.2rem}[dir=rtl] .md-typeset .tabbed-control{transform:rotate(180deg)}.md-typeset .tabbed-control[hidden]{opacity:0}.md-typeset .tabbed-control--next{background:linear-gradient(to left,var(--md-default-bg-color) 60%,#0000);justify-content:end;right:0}.md-typeset .tabbed-control--next .tabbed-button:after{-webkit-mask-image:var(--md-tabbed-icon--next);mask-image:var(--md-tabbed-icon--next)}@media screen and (max-width:44.984375em){[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels{padding-left:.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels{padding-right:.8rem}.md-content__inner>.tabbed-set .tabbed-labels{margin:0 -.8rem;max-width:100vw;scroll-padding-inline-start:.8rem}[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels:after{padding-right:.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels:after{padding-left:.8rem}.md-content__inner>.tabbed-set .tabbed-labels:after{content:""}[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--prev{padding-left:.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--prev{padding-right:.8rem}[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--prev{margin-left:-.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--prev{margin-right:-.8rem}.md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--prev{width:2rem}[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--next{padding-right:.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--next{padding-left:.8rem}[dir=ltr] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--next{margin-right:-.8rem}[dir=rtl] .md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--next{margin-left:-.8rem}.md-content__inner>.tabbed-set .tabbed-labels~.tabbed-control--next{width:2rem}}@media screen{.md-typeset .tabbed-set>input:first-child:checked~.tabbed-labels>:first-child,.md-typeset .tabbed-set>input:nth-child(10):checked~.tabbed-labels>:nth-child(10),.md-typeset .tabbed-set>input:nth-child(11):checked~.tabbed-labels>:nth-child(11),.md-typeset .tabbed-set>input:nth-child(12):checked~.tabbed-labels>:nth-child(12),.md-typeset .tabbed-set>input:nth-child(13):checked~.tabbed-labels>:nth-child(13),.md-typeset .tabbed-set>input:nth-child(14):checked~.tabbed-labels>:nth-child(14),.md-typeset .tabbed-set>input:nth-child(15):checked~.tabbed-labels>:nth-child(15),.md-typeset .tabbed-set>input:nth-child(16):checked~.tabbed-labels>:nth-child(16),.md-typeset .tabbed-set>input:nth-child(17):checked~.tabbed-labels>:nth-child(17),.md-typeset .tabbed-set>input:nth-child(18):checked~.tabbed-labels>:nth-child(18),.md-typeset .tabbed-set>input:nth-child(19):checked~.tabbed-labels>:nth-child(19),.md-typeset .tabbed-set>input:nth-child(2):checked~.tabbed-labels>:nth-child(2),.md-typeset .tabbed-set>input:nth-child(20):checked~.tabbed-labels>:nth-child(20),.md-typeset .tabbed-set>input:nth-child(3):checked~.tabbed-labels>:nth-child(3),.md-typeset .tabbed-set>input:nth-child(4):checked~.tabbed-labels>:nth-child(4),.md-typeset .tabbed-set>input:nth-child(5):checked~.tabbed-labels>:nth-child(5),.md-typeset .tabbed-set>input:nth-child(6):checked~.tabbed-labels>:nth-child(6),.md-typeset .tabbed-set>input:nth-child(7):checked~.tabbed-labels>:nth-child(7),.md-typeset .tabbed-set>input:nth-child(8):checked~.tabbed-labels>:nth-child(8),.md-typeset .tabbed-set>input:nth-child(9):checked~.tabbed-labels>:nth-child(9){color:var(--md-default-fg-color)}.md-typeset .no-js .tabbed-set>input:first-child:checked~.tabbed-labels>:first-child,.md-typeset .no-js .tabbed-set>input:nth-child(10):checked~.tabbed-labels>:nth-child(10),.md-typeset .no-js .tabbed-set>input:nth-child(11):checked~.tabbed-labels>:nth-child(11),.md-typeset .no-js .tabbed-set>input:nth-child(12):checked~.tabbed-labels>:nth-child(12),.md-typeset .no-js .tabbed-set>input:nth-child(13):checked~.tabbed-labels>:nth-child(13),.md-typeset .no-js .tabbed-set>input:nth-child(14):checked~.tabbed-labels>:nth-child(14),.md-typeset .no-js .tabbed-set>input:nth-child(15):checked~.tabbed-labels>:nth-child(15),.md-typeset .no-js .tabbed-set>input:nth-child(16):checked~.tabbed-labels>:nth-child(16),.md-typeset .no-js .tabbed-set>input:nth-child(17):checked~.tabbed-labels>:nth-child(17),.md-typeset .no-js .tabbed-set>input:nth-child(18):checked~.tabbed-labels>:nth-child(18),.md-typeset .no-js .tabbed-set>input:nth-child(19):checked~.tabbed-labels>:nth-child(19),.md-typeset .no-js .tabbed-set>input:nth-child(2):checked~.tabbed-labels>:nth-child(2),.md-typeset .no-js .tabbed-set>input:nth-child(20):checked~.tabbed-labels>:nth-child(20),.md-typeset .no-js .tabbed-set>input:nth-child(3):checked~.tabbed-labels>:nth-child(3),.md-typeset .no-js .tabbed-set>input:nth-child(4):checked~.tabbed-labels>:nth-child(4),.md-typeset .no-js .tabbed-set>input:nth-child(5):checked~.tabbed-labels>:nth-child(5),.md-typeset .no-js .tabbed-set>input:nth-child(6):checked~.tabbed-labels>:nth-child(6),.md-typeset .no-js .tabbed-set>input:nth-child(7):checked~.tabbed-labels>:nth-child(7),.md-typeset .no-js .tabbed-set>input:nth-child(8):checked~.tabbed-labels>:nth-child(8),.md-typeset .no-js .tabbed-set>input:nth-child(9):checked~.tabbed-labels>:nth-child(9),.md-typeset [role=dialog] .tabbed-set>input:first-child:checked~.tabbed-labels>:first-child,.md-typeset [role=dialog] .tabbed-set>input:nth-child(10):checked~.tabbed-labels>:nth-child(10),.md-typeset [role=dialog] .tabbed-set>input:nth-child(11):checked~.tabbed-labels>:nth-child(11),.md-typeset [role=dialog] .tabbed-set>input:nth-child(12):checked~.tabbed-labels>:nth-child(12),.md-typeset [role=dialog] .tabbed-set>input:nth-child(13):checked~.tabbed-labels>:nth-child(13),.md-typeset [role=dialog] .tabbed-set>input:nth-child(14):checked~.tabbed-labels>:nth-child(14),.md-typeset [role=dialog] .tabbed-set>input:nth-child(15):checked~.tabbed-labels>:nth-child(15),.md-typeset [role=dialog] .tabbed-set>input:nth-child(16):checked~.tabbed-labels>:nth-child(16),.md-typeset [role=dialog] .tabbed-set>input:nth-child(17):checked~.tabbed-labels>:nth-child(17),.md-typeset [role=dialog] .tabbed-set>input:nth-child(18):checked~.tabbed-labels>:nth-child(18),.md-typeset [role=dialog] .tabbed-set>input:nth-child(19):checked~.tabbed-labels>:nth-child(19),.md-typeset [role=dialog] .tabbed-set>input:nth-child(2):checked~.tabbed-labels>:nth-child(2),.md-typeset [role=dialog] .tabbed-set>input:nth-child(20):checked~.tabbed-labels>:nth-child(20),.md-typeset [role=dialog] .tabbed-set>input:nth-child(3):checked~.tabbed-labels>:nth-child(3),.md-typeset [role=dialog] .tabbed-set>input:nth-child(4):checked~.tabbed-labels>:nth-child(4),.md-typeset [role=dialog] .tabbed-set>input:nth-child(5):checked~.tabbed-labels>:nth-child(5),.md-typeset [role=dialog] .tabbed-set>input:nth-child(6):checked~.tabbed-labels>:nth-child(6),.md-typeset [role=dialog] .tabbed-set>input:nth-child(7):checked~.tabbed-labels>:nth-child(7),.md-typeset [role=dialog] .tabbed-set>input:nth-child(8):checked~.tabbed-labels>:nth-child(8),.md-typeset [role=dialog] .tabbed-set>input:nth-child(9):checked~.tabbed-labels>:nth-child(9),.no-js .md-typeset .tabbed-set>input:first-child:checked~.tabbed-labels>:first-child,.no-js .md-typeset .tabbed-set>input:nth-child(10):checked~.tabbed-labels>:nth-child(10),.no-js .md-typeset .tabbed-set>input:nth-child(11):checked~.tabbed-labels>:nth-child(11),.no-js .md-typeset .tabbed-set>input:nth-child(12):checked~.tabbed-labels>:nth-child(12),.no-js .md-typeset .tabbed-set>input:nth-child(13):checked~.tabbed-labels>:nth-child(13),.no-js .md-typeset .tabbed-set>input:nth-child(14):checked~.tabbed-labels>:nth-child(14),.no-js .md-typeset .tabbed-set>input:nth-child(15):checked~.tabbed-labels>:nth-child(15),.no-js .md-typeset .tabbed-set>input:nth-child(16):checked~.tabbed-labels>:nth-child(16),.no-js .md-typeset .tabbed-set>input:nth-child(17):checked~.tabbed-labels>:nth-child(17),.no-js .md-typeset .tabbed-set>input:nth-child(18):checked~.tabbed-labels>:nth-child(18),.no-js .md-typeset .tabbed-set>input:nth-child(19):checked~.tabbed-labels>:nth-child(19),.no-js .md-typeset .tabbed-set>input:nth-child(2):checked~.tabbed-labels>:nth-child(2),.no-js .md-typeset .tabbed-set>input:nth-child(20):checked~.tabbed-labels>:nth-child(20),.no-js .md-typeset .tabbed-set>input:nth-child(3):checked~.tabbed-labels>:nth-child(3),.no-js .md-typeset .tabbed-set>input:nth-child(4):checked~.tabbed-labels>:nth-child(4),.no-js .md-typeset .tabbed-set>input:nth-child(5):checked~.tabbed-labels>:nth-child(5),.no-js .md-typeset .tabbed-set>input:nth-child(6):checked~.tabbed-labels>:nth-child(6),.no-js .md-typeset .tabbed-set>input:nth-child(7):checked~.tabbed-labels>:nth-child(7),.no-js .md-typeset .tabbed-set>input:nth-child(8):checked~.tabbed-labels>:nth-child(8),.no-js .md-typeset .tabbed-set>input:nth-child(9):checked~.tabbed-labels>:nth-child(9),[role=dialog] .md-typeset .tabbed-set>input:first-child:checked~.tabbed-labels>:first-child,[role=dialog] .md-typeset .tabbed-set>input:nth-child(10):checked~.tabbed-labels>:nth-child(10),[role=dialog] .md-typeset .tabbed-set>input:nth-child(11):checked~.tabbed-labels>:nth-child(11),[role=dialog] .md-typeset .tabbed-set>input:nth-child(12):checked~.tabbed-labels>:nth-child(12),[role=dialog] .md-typeset .tabbed-set>input:nth-child(13):checked~.tabbed-labels>:nth-child(13),[role=dialog] .md-typeset .tabbed-set>input:nth-child(14):checked~.tabbed-labels>:nth-child(14),[role=dialog] .md-typeset .tabbed-set>input:nth-child(15):checked~.tabbed-labels>:nth-child(15),[role=dialog] .md-typeset .tabbed-set>input:nth-child(16):checked~.tabbed-labels>:nth-child(16),[role=dialog] .md-typeset .tabbed-set>input:nth-child(17):checked~.tabbed-labels>:nth-child(17),[role=dialog] .md-typeset .tabbed-set>input:nth-child(18):checked~.tabbed-labels>:nth-child(18),[role=dialog] .md-typeset .tabbed-set>input:nth-child(19):checked~.tabbed-labels>:nth-child(19),[role=dialog] .md-typeset .tabbed-set>input:nth-child(2):checked~.tabbed-labels>:nth-child(2),[role=dialog] .md-typeset .tabbed-set>input:nth-child(20):checked~.tabbed-labels>:nth-child(20),[role=dialog] .md-typeset .tabbed-set>input:nth-child(3):checked~.tabbed-labels>:nth-child(3),[role=dialog] .md-typeset .tabbed-set>input:nth-child(4):checked~.tabbed-labels>:nth-child(4),[role=dialog] .md-typeset .tabbed-set>input:nth-child(5):checked~.tabbed-labels>:nth-child(5),[role=dialog] .md-typeset .tabbed-set>input:nth-child(6):checked~.tabbed-labels>:nth-child(6),[role=dialog] .md-typeset .tabbed-set>input:nth-child(7):checked~.tabbed-labels>:nth-child(7),[role=dialog] .md-typeset .tabbed-set>input:nth-child(8):checked~.tabbed-labels>:nth-child(8),[role=dialog] .md-typeset .tabbed-set>input:nth-child(9):checked~.tabbed-labels>:nth-child(9){border-color:var(--md-default-fg-color)}}.md-typeset .tabbed-set>input:first-child.focus-visible~.tabbed-labels>:first-child,.md-typeset .tabbed-set>input:nth-child(10).focus-visible~.tabbed-labels>:nth-child(10),.md-typeset .tabbed-set>input:nth-child(11).focus-visible~.tabbed-labels>:nth-child(11),.md-typeset .tabbed-set>input:nth-child(12).focus-visible~.tabbed-labels>:nth-child(12),.md-typeset .tabbed-set>input:nth-child(13).focus-visible~.tabbed-labels>:nth-child(13),.md-typeset .tabbed-set>input:nth-child(14).focus-visible~.tabbed-labels>:nth-child(14),.md-typeset .tabbed-set>input:nth-child(15).focus-visible~.tabbed-labels>:nth-child(15),.md-typeset .tabbed-set>input:nth-child(16).focus-visible~.tabbed-labels>:nth-child(16),.md-typeset .tabbed-set>input:nth-child(17).focus-visible~.tabbed-labels>:nth-child(17),.md-typeset .tabbed-set>input:nth-child(18).focus-visible~.tabbed-labels>:nth-child(18),.md-typeset .tabbed-set>input:nth-child(19).focus-visible~.tabbed-labels>:nth-child(19),.md-typeset .tabbed-set>input:nth-child(2).focus-visible~.tabbed-labels>:nth-child(2),.md-typeset .tabbed-set>input:nth-child(20).focus-visible~.tabbed-labels>:nth-child(20),.md-typeset .tabbed-set>input:nth-child(3).focus-visible~.tabbed-labels>:nth-child(3),.md-typeset .tabbed-set>input:nth-child(4).focus-visible~.tabbed-labels>:nth-child(4),.md-typeset .tabbed-set>input:nth-child(5).focus-visible~.tabbed-labels>:nth-child(5),.md-typeset .tabbed-set>input:nth-child(6).focus-visible~.tabbed-labels>:nth-child(6),.md-typeset .tabbed-set>input:nth-child(7).focus-visible~.tabbed-labels>:nth-child(7),.md-typeset .tabbed-set>input:nth-child(8).focus-visible~.tabbed-labels>:nth-child(8),.md-typeset .tabbed-set>input:nth-child(9).focus-visible~.tabbed-labels>:nth-child(9){color:var(--md-accent-fg-color)}.md-typeset .tabbed-set>input:first-child:checked~.tabbed-content>:first-child,.md-typeset .tabbed-set>input:nth-child(10):checked~.tabbed-content>:nth-child(10),.md-typeset .tabbed-set>input:nth-child(11):checked~.tabbed-content>:nth-child(11),.md-typeset .tabbed-set>input:nth-child(12):checked~.tabbed-content>:nth-child(12),.md-typeset .tabbed-set>input:nth-child(13):checked~.tabbed-content>:nth-child(13),.md-typeset .tabbed-set>input:nth-child(14):checked~.tabbed-content>:nth-child(14),.md-typeset .tabbed-set>input:nth-child(15):checked~.tabbed-content>:nth-child(15),.md-typeset .tabbed-set>input:nth-child(16):checked~.tabbed-content>:nth-child(16),.md-typeset .tabbed-set>input:nth-child(17):checked~.tabbed-content>:nth-child(17),.md-typeset .tabbed-set>input:nth-child(18):checked~.tabbed-content>:nth-child(18),.md-typeset .tabbed-set>input:nth-child(19):checked~.tabbed-content>:nth-child(19),.md-typeset .tabbed-set>input:nth-child(2):checked~.tabbed-content>:nth-child(2),.md-typeset .tabbed-set>input:nth-child(20):checked~.tabbed-content>:nth-child(20),.md-typeset .tabbed-set>input:nth-child(3):checked~.tabbed-content>:nth-child(3),.md-typeset .tabbed-set>input:nth-child(4):checked~.tabbed-content>:nth-child(4),.md-typeset .tabbed-set>input:nth-child(5):checked~.tabbed-content>:nth-child(5),.md-typeset .tabbed-set>input:nth-child(6):checked~.tabbed-content>:nth-child(6),.md-typeset .tabbed-set>input:nth-child(7):checked~.tabbed-content>:nth-child(7),.md-typeset .tabbed-set>input:nth-child(8):checked~.tabbed-content>:nth-child(8),.md-typeset .tabbed-set>input:nth-child(9):checked~.tabbed-content>:nth-child(9){display:block}:root{--md-tasklist-icon:url('data:image/svg+xml;charset=utf-8,');--md-tasklist-icon--checked:url('data:image/svg+xml;charset=utf-8,')}.md-typeset .task-list-item{list-style-type:none;position:relative}[dir=ltr] .md-typeset .task-list-item [type=checkbox]{left:-2em}[dir=rtl] .md-typeset .task-list-item [type=checkbox]{right:-2em}.md-typeset .task-list-item [type=checkbox]{position:absolute;top:.45em}.md-typeset .task-list-control [type=checkbox]{opacity:0;z-index:-1}[dir=ltr] .md-typeset .task-list-indicator:before{left:-1.5em}[dir=rtl] .md-typeset .task-list-indicator:before{right:-1.5em}.md-typeset .task-list-indicator:before{background-color:var(--md-default-fg-color--lightest);content:"";height:1.25em;-webkit-mask-image:var(--md-tasklist-icon);mask-image:var(--md-tasklist-icon);-webkit-mask-position:center;mask-position:center;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat;-webkit-mask-size:contain;mask-size:contain;position:absolute;top:.15em;width:1.25em}.md-typeset [type=checkbox]:checked+.task-list-indicator:before{background-color:#00e676;-webkit-mask-image:var(--md-tasklist-icon--checked);mask-image:var(--md-tasklist-icon--checked)}@media print{.giscus,[id=__comments]{display:none}}:root>*{--md-mermaid-font-family:var(--md-text-font-family),sans-serif;--md-mermaid-edge-color:var(--md-code-fg-color);--md-mermaid-node-bg-color:var(--md-accent-fg-color--transparent);--md-mermaid-node-fg-color:var(--md-accent-fg-color);--md-mermaid-label-bg-color:var(--md-default-bg-color);--md-mermaid-label-fg-color:var(--md-code-fg-color);--md-mermaid-sequence-actor-bg-color:var(--md-mermaid-label-bg-color);--md-mermaid-sequence-actor-fg-color:var(--md-mermaid-label-fg-color);--md-mermaid-sequence-actor-border-color:var(--md-mermaid-node-fg-color);--md-mermaid-sequence-actor-line-color:var(--md-default-fg-color--lighter);--md-mermaid-sequence-actorman-bg-color:var(--md-mermaid-label-bg-color);--md-mermaid-sequence-actorman-line-color:var(--md-mermaid-node-fg-color);--md-mermaid-sequence-box-bg-color:var(--md-mermaid-node-bg-color);--md-mermaid-sequence-box-fg-color:var(--md-mermaid-edge-color);--md-mermaid-sequence-label-bg-color:var(--md-mermaid-node-bg-color);--md-mermaid-sequence-label-fg-color:var(--md-mermaid-node-fg-color);--md-mermaid-sequence-loop-bg-color:var(--md-mermaid-node-bg-color);--md-mermaid-sequence-loop-fg-color:var(--md-mermaid-edge-color);--md-mermaid-sequence-loop-border-color:var(--md-mermaid-node-fg-color);--md-mermaid-sequence-message-fg-color:var(--md-mermaid-edge-color);--md-mermaid-sequence-message-line-color:var(--md-mermaid-edge-color);--md-mermaid-sequence-note-bg-color:var(--md-mermaid-label-bg-color);--md-mermaid-sequence-note-fg-color:var(--md-mermaid-edge-color);--md-mermaid-sequence-note-border-color:var(--md-mermaid-label-fg-color);--md-mermaid-sequence-number-bg-color:var(--md-mermaid-node-fg-color);--md-mermaid-sequence-number-fg-color:var(--md-accent-bg-color)}.mermaid{line-height:normal;margin:1em 0}.md-typeset .grid{grid-gap:.4rem;display:grid;grid-template-columns:repeat(auto-fit,minmax(min(100%,16rem),1fr));margin:1em 0}.md-typeset .grid.cards>ol,.md-typeset .grid.cards>ul{display:contents}.md-typeset .grid.cards>ol>li,.md-typeset .grid.cards>ul>li,.md-typeset .grid>.card{border:.05rem solid var(--md-default-fg-color--lightest);border-radius:.1rem;display:block;margin:0;padding:.8rem;transition:border .25s,box-shadow .25s}.md-typeset .grid.cards>ol>li:focus-within,.md-typeset .grid.cards>ol>li:hover,.md-typeset .grid.cards>ul>li:focus-within,.md-typeset .grid.cards>ul>li:hover,.md-typeset .grid>.card:focus-within,.md-typeset .grid>.card:hover{border-color:#0000;box-shadow:var(--md-shadow-z2)}.md-typeset .grid.cards>ol>li>hr,.md-typeset .grid.cards>ul>li>hr,.md-typeset .grid>.card>hr{margin-bottom:1em;margin-top:1em}.md-typeset .grid.cards>ol>li>:first-child,.md-typeset .grid.cards>ul>li>:first-child,.md-typeset .grid>.card>:first-child{margin-top:0}.md-typeset .grid.cards>ol>li>:last-child,.md-typeset .grid.cards>ul>li>:last-child,.md-typeset .grid>.card>:last-child{margin-bottom:0}.md-typeset .grid>*,.md-typeset .grid>.admonition,.md-typeset .grid>.highlight>*,.md-typeset .grid>.highlighttable,.md-typeset .grid>.md-typeset details,.md-typeset .grid>details,.md-typeset .grid>pre{margin-bottom:0;margin-top:0}.md-typeset .grid>.highlight>pre:only-child,.md-typeset .grid>.highlight>pre>code,.md-typeset .grid>.highlighttable,.md-typeset .grid>.highlighttable>tbody,.md-typeset .grid>.highlighttable>tbody>tr,.md-typeset .grid>.highlighttable>tbody>tr>.code,.md-typeset .grid>.highlighttable>tbody>tr>.code>.highlight,.md-typeset .grid>.highlighttable>tbody>tr>.code>.highlight>pre,.md-typeset .grid>.highlighttable>tbody>tr>.code>.highlight>pre>code{height:100%}.md-typeset .grid>.tabbed-set{margin-bottom:0;margin-top:0}@media screen and (min-width:45em){[dir=ltr] .md-typeset .inline{float:left}[dir=rtl] .md-typeset .inline{float:right}[dir=ltr] .md-typeset .inline{margin-right:.8rem}[dir=rtl] .md-typeset .inline{margin-left:.8rem}.md-typeset .inline{margin-bottom:.8rem;margin-top:0;width:11.7rem}[dir=ltr] .md-typeset .inline.end{float:right}[dir=rtl] .md-typeset .inline.end{float:left}[dir=ltr] .md-typeset .inline.end{margin-left:.8rem;margin-right:0}[dir=rtl] .md-typeset .inline.end{margin-left:0;margin-right:.8rem}} -/*! update cache: 20260104155739 */ +/*! update cache: 20260104161439 */ diff --git a/zh-hant/assets/stylesheets/palette.ab4e12ef.min.css b/zh-hant/assets/stylesheets/palette.ab4e12ef.min.css index b0a6bc88d..f9e88964a 100644 --- a/zh-hant/assets/stylesheets/palette.ab4e12ef.min.css +++ b/zh-hant/assets/stylesheets/palette.ab4e12ef.min.css @@ -1,2 +1,2 @@ @media screen{[data-md-color-scheme=slate]{--md-default-fg-color:hsla(var(--md-hue),15%,90%,0.82);--md-default-fg-color--light:hsla(var(--md-hue),15%,90%,0.56);--md-default-fg-color--lighter:hsla(var(--md-hue),15%,90%,0.32);--md-default-fg-color--lightest:hsla(var(--md-hue),15%,90%,0.12);--md-default-bg-color:hsla(var(--md-hue),15%,14%,1);--md-default-bg-color--light:hsla(var(--md-hue),15%,14%,0.54);--md-default-bg-color--lighter:hsla(var(--md-hue),15%,14%,0.26);--md-default-bg-color--lightest:hsla(var(--md-hue),15%,14%,0.07);--md-code-fg-color:hsla(var(--md-hue),18%,86%,0.82);--md-code-bg-color:hsla(var(--md-hue),15%,18%,1);--md-code-bg-color--light:hsla(var(--md-hue),15%,18%,0.9);--md-code-bg-color--lighter:hsla(var(--md-hue),15%,18%,0.54);--md-code-hl-color:#2977ff;--md-code-hl-color--light:#2977ff1a;--md-code-hl-number-color:#e6695b;--md-code-hl-special-color:#f06090;--md-code-hl-function-color:#c973d9;--md-code-hl-constant-color:#9383e2;--md-code-hl-keyword-color:#6791e0;--md-code-hl-string-color:#2fb170;--md-code-hl-name-color:var(--md-code-fg-color);--md-code-hl-operator-color:var(--md-default-fg-color--light);--md-code-hl-punctuation-color:var(--md-default-fg-color--light);--md-code-hl-comment-color:var(--md-default-fg-color--light);--md-code-hl-generic-color:var(--md-default-fg-color--light);--md-code-hl-variable-color:var(--md-default-fg-color--light);--md-typeset-color:var(--md-default-fg-color);--md-typeset-a-color:var(--md-primary-fg-color);--md-typeset-kbd-color:hsla(var(--md-hue),15%,90%,0.12);--md-typeset-kbd-accent-color:hsla(var(--md-hue),15%,90%,0.2);--md-typeset-kbd-border-color:hsla(var(--md-hue),15%,14%,1);--md-typeset-mark-color:#4287ff4d;--md-typeset-table-color:hsla(var(--md-hue),15%,95%,0.12);--md-typeset-table-color--light:hsla(var(--md-hue),15%,95%,0.035);--md-admonition-fg-color:var(--md-default-fg-color);--md-admonition-bg-color:var(--md-default-bg-color);--md-footer-bg-color:hsla(var(--md-hue),15%,10%,0.87);--md-footer-bg-color--dark:hsla(var(--md-hue),15%,8%,1);--md-shadow-z1:0 0.2rem 0.5rem #0000000d,0 0 0.05rem #0000001a;--md-shadow-z2:0 0.2rem 0.5rem #00000040,0 0 0.05rem #00000040;--md-shadow-z3:0 0.2rem 0.5rem #0006,0 0 0.05rem #00000059;color-scheme:dark}[data-md-color-scheme=slate] img[src$="#gh-light-mode-only"],[data-md-color-scheme=slate] img[src$="#only-light"]{display:none}[data-md-color-scheme=slate][data-md-color-primary=pink]{--md-typeset-a-color:#ed5487}[data-md-color-scheme=slate][data-md-color-primary=purple]{--md-typeset-a-color:#c46fd3}[data-md-color-scheme=slate][data-md-color-primary=deep-purple]{--md-typeset-a-color:#a47bea}[data-md-color-scheme=slate][data-md-color-primary=indigo]{--md-typeset-a-color:#5488e8}[data-md-color-scheme=slate][data-md-color-primary=teal]{--md-typeset-a-color:#00ccb8}[data-md-color-scheme=slate][data-md-color-primary=green]{--md-typeset-a-color:#71c174}[data-md-color-scheme=slate][data-md-color-primary=deep-orange]{--md-typeset-a-color:#ff764d}[data-md-color-scheme=slate][data-md-color-primary=brown]{--md-typeset-a-color:#c1775c}[data-md-color-scheme=slate][data-md-color-primary=black],[data-md-color-scheme=slate][data-md-color-primary=blue-grey],[data-md-color-scheme=slate][data-md-color-primary=grey],[data-md-color-scheme=slate][data-md-color-primary=white]{--md-typeset-a-color:#5e8bde}[data-md-color-switching] *,[data-md-color-switching] :after,[data-md-color-switching] :before{transition-duration:0ms!important}}[data-md-color-accent=red]{--md-accent-fg-color:#ff1947;--md-accent-fg-color--transparent:#ff19471a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=pink]{--md-accent-fg-color:#f50056;--md-accent-fg-color--transparent:#f500561a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=purple]{--md-accent-fg-color:#df41fb;--md-accent-fg-color--transparent:#df41fb1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=deep-purple]{--md-accent-fg-color:#7c4dff;--md-accent-fg-color--transparent:#7c4dff1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=indigo]{--md-accent-fg-color:#526cfe;--md-accent-fg-color--transparent:#526cfe1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=blue]{--md-accent-fg-color:#4287ff;--md-accent-fg-color--transparent:#4287ff1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=light-blue]{--md-accent-fg-color:#0091eb;--md-accent-fg-color--transparent:#0091eb1a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=cyan]{--md-accent-fg-color:#00bad6;--md-accent-fg-color--transparent:#00bad61a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=teal]{--md-accent-fg-color:#00bda4;--md-accent-fg-color--transparent:#00bda41a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=green]{--md-accent-fg-color:#00c753;--md-accent-fg-color--transparent:#00c7531a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=light-green]{--md-accent-fg-color:#63de17;--md-accent-fg-color--transparent:#63de171a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-accent=lime]{--md-accent-fg-color:#b0eb00;--md-accent-fg-color--transparent:#b0eb001a;--md-accent-bg-color:#000000de;--md-accent-bg-color--light:#0000008a}[data-md-color-accent=yellow]{--md-accent-fg-color:#ffd500;--md-accent-fg-color--transparent:#ffd5001a;--md-accent-bg-color:#000000de;--md-accent-bg-color--light:#0000008a}[data-md-color-accent=amber]{--md-accent-fg-color:#fa0;--md-accent-fg-color--transparent:#ffaa001a;--md-accent-bg-color:#000000de;--md-accent-bg-color--light:#0000008a}[data-md-color-accent=orange]{--md-accent-fg-color:#ff9100;--md-accent-fg-color--transparent:#ff91001a;--md-accent-bg-color:#000000de;--md-accent-bg-color--light:#0000008a}[data-md-color-accent=deep-orange]{--md-accent-fg-color:#ff6e42;--md-accent-fg-color--transparent:#ff6e421a;--md-accent-bg-color:#fff;--md-accent-bg-color--light:#ffffffb3}[data-md-color-primary=red]{--md-primary-fg-color:#ef5552;--md-primary-fg-color--light:#e57171;--md-primary-fg-color--dark:#e53734;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=pink]{--md-primary-fg-color:#e92063;--md-primary-fg-color--light:#ec417a;--md-primary-fg-color--dark:#c3185d;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=purple]{--md-primary-fg-color:#ab47bd;--md-primary-fg-color--light:#bb69c9;--md-primary-fg-color--dark:#8c24a8;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=deep-purple]{--md-primary-fg-color:#7e56c2;--md-primary-fg-color--light:#9574cd;--md-primary-fg-color--dark:#673ab6;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=indigo]{--md-primary-fg-color:#4051b5;--md-primary-fg-color--light:#5d6cc0;--md-primary-fg-color--dark:#303fa1;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=blue]{--md-primary-fg-color:#2094f3;--md-primary-fg-color--light:#42a5f5;--md-primary-fg-color--dark:#1975d2;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=light-blue]{--md-primary-fg-color:#02a6f2;--md-primary-fg-color--light:#28b5f6;--md-primary-fg-color--dark:#0287cf;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=cyan]{--md-primary-fg-color:#00bdd6;--md-primary-fg-color--light:#25c5da;--md-primary-fg-color--dark:#0097a8;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=teal]{--md-primary-fg-color:#009485;--md-primary-fg-color--light:#26a699;--md-primary-fg-color--dark:#007a6c;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=green]{--md-primary-fg-color:#4cae4f;--md-primary-fg-color--light:#68bb6c;--md-primary-fg-color--dark:#398e3d;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=light-green]{--md-primary-fg-color:#8bc34b;--md-primary-fg-color--light:#9ccc66;--md-primary-fg-color--dark:#689f38;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=lime]{--md-primary-fg-color:#cbdc38;--md-primary-fg-color--light:#d3e156;--md-primary-fg-color--dark:#b0b52c;--md-primary-bg-color:#000000de;--md-primary-bg-color--light:#0000008a}[data-md-color-primary=yellow]{--md-primary-fg-color:#ffec3d;--md-primary-fg-color--light:#ffee57;--md-primary-fg-color--dark:#fbc02d;--md-primary-bg-color:#000000de;--md-primary-bg-color--light:#0000008a}[data-md-color-primary=amber]{--md-primary-fg-color:#ffc105;--md-primary-fg-color--light:#ffc929;--md-primary-fg-color--dark:#ffa200;--md-primary-bg-color:#000000de;--md-primary-bg-color--light:#0000008a}[data-md-color-primary=orange]{--md-primary-fg-color:#ffa724;--md-primary-fg-color--light:#ffa724;--md-primary-fg-color--dark:#fa8900;--md-primary-bg-color:#000000de;--md-primary-bg-color--light:#0000008a}[data-md-color-primary=deep-orange]{--md-primary-fg-color:#ff6e42;--md-primary-fg-color--light:#ff8a66;--md-primary-fg-color--dark:#f4511f;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=brown]{--md-primary-fg-color:#795649;--md-primary-fg-color--light:#8d6e62;--md-primary-fg-color--dark:#5d4037;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3}[data-md-color-primary=grey]{--md-primary-fg-color:#757575;--md-primary-fg-color--light:#9e9e9e;--md-primary-fg-color--dark:#616161;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3;--md-typeset-a-color:#4051b5}[data-md-color-primary=blue-grey]{--md-primary-fg-color:#546d78;--md-primary-fg-color--light:#607c8a;--md-primary-fg-color--dark:#455a63;--md-primary-bg-color:#fff;--md-primary-bg-color--light:#ffffffb3;--md-typeset-a-color:#4051b5}[data-md-color-primary=light-green]:not([data-md-color-scheme=slate]){--md-typeset-a-color:#72ad2e}[data-md-color-primary=lime]:not([data-md-color-scheme=slate]){--md-typeset-a-color:#8b990a}[data-md-color-primary=yellow]:not([data-md-color-scheme=slate]){--md-typeset-a-color:#b8a500}[data-md-color-primary=amber]:not([data-md-color-scheme=slate]){--md-typeset-a-color:#d19d00}[data-md-color-primary=orange]:not([data-md-color-scheme=slate]){--md-typeset-a-color:#e68a00}[data-md-color-primary=white]{--md-primary-fg-color:hsla(var(--md-hue),0%,100%,1);--md-primary-fg-color--light:hsla(var(--md-hue),0%,100%,0.7);--md-primary-fg-color--dark:hsla(var(--md-hue),0%,0%,0.07);--md-primary-bg-color:hsla(var(--md-hue),0%,0%,0.87);--md-primary-bg-color--light:hsla(var(--md-hue),0%,0%,0.54);--md-typeset-a-color:#4051b5}[data-md-color-primary=white] .md-button{color:var(--md-typeset-a-color)}[data-md-color-primary=white] .md-button--primary{background-color:var(--md-typeset-a-color);border-color:var(--md-typeset-a-color);color:hsla(var(--md-hue),0%,100%,1)}@media screen and (min-width:60em){[data-md-color-primary=white] .md-search__form{background-color:hsla(var(--md-hue),0%,0%,.07)}[data-md-color-primary=white] .md-search__form:hover{background-color:hsla(var(--md-hue),0%,0%,.32)}[data-md-color-primary=white] .md-search__input+.md-search__icon{color:hsla(var(--md-hue),0%,0%,.87)}}@media screen and (min-width:76.25em){[data-md-color-primary=white] .md-tabs{border-bottom:.05rem solid #00000012}}[data-md-color-primary=black]{--md-primary-fg-color:hsla(var(--md-hue),15%,9%,1);--md-primary-fg-color--light:hsla(var(--md-hue),15%,9%,0.54);--md-primary-fg-color--dark:hsla(var(--md-hue),15%,9%,1);--md-primary-bg-color:hsla(var(--md-hue),15%,100%,1);--md-primary-bg-color--light:hsla(var(--md-hue),15%,100%,0.7);--md-typeset-a-color:#4051b5}[data-md-color-primary=black] .md-button{color:var(--md-typeset-a-color)}[data-md-color-primary=black] .md-button--primary{background-color:var(--md-typeset-a-color);border-color:var(--md-typeset-a-color);color:hsla(var(--md-hue),0%,100%,1)}[data-md-color-primary=black] .md-header{background-color:hsla(var(--md-hue),15%,9%,1)}@media screen and (max-width:59.984375em){[data-md-color-primary=black] .md-nav__source{background-color:hsla(var(--md-hue),15%,11%,.87)}}@media screen and (max-width:76.234375em){html [data-md-color-primary=black] .md-nav--primary .md-nav__title[for=__drawer]{background-color:hsla(var(--md-hue),15%,9%,1)}}@media screen and (min-width:76.25em){[data-md-color-primary=black] .md-tabs{background-color:hsla(var(--md-hue),15%,9%,1)}} -/*! update cache: 20260104155739 */ +/*! update cache: 20260104161439 */ diff --git a/zh-hant/javascripts/katex.js b/zh-hant/javascripts/katex.js index 322175bde..0bf0f798a 100644 --- a/zh-hant/javascripts/katex.js +++ b/zh-hant/javascripts/katex.js @@ -8,4 +8,4 @@ document$.subscribe(({ body }) => { ], }); }); -/*! update cache: 20260104155739 */ +/*! update cache: 20260104161439 */ diff --git a/zh-hant/javascripts/mathjax.js b/zh-hant/javascripts/mathjax.js index fba80b21f..279b4b617 100644 --- a/zh-hant/javascripts/mathjax.js +++ b/zh-hant/javascripts/mathjax.js @@ -15,4 +15,4 @@ window.MathJax = { document$.subscribe(() => { MathJax.typesetPromise(); }); -/*! update cache: 20260104155739 */ +/*! update cache: 20260104161439 */ diff --git a/zh-hant/javascripts/starfield.js b/zh-hant/javascripts/starfield.js index 21257afdc..a5a418f20 100644 --- a/zh-hant/javascripts/starfield.js +++ b/zh-hant/javascripts/starfield.js @@ -469,4 +469,4 @@ return Starfield; }); -/*! update cache: 20260104155739 */ +/*! update cache: 20260104161439 */ diff --git a/zh-hant/stylesheets/extra.css b/zh-hant/stylesheets/extra.css index 98ac89875..15ee5cffb 100644 --- a/zh-hant/stylesheets/extra.css +++ b/zh-hant/stylesheets/extra.css @@ -567,4 +567,4 @@ a:hover .text-button span { left: 50%; transform: translate(-50%, -50%); } -/*! update cache: 20260104155739 */ +/*! update cache: 20260104161439 */